[Haskell] "global variables" and code optimization
"Holzmüller, Bernd"
holzmueller at ICS-AG.de
Fri Sep 10 04:17:22 EDT 2004
Hi all,
in a compiler project I am using "global variables" of the form:
gNewLabelNr :: IORef Int
gNewLabelNr = unsafePerformIO $ newIORef 1
getAndUpdateVar var f = unsafePerformIO $
do oldVal <- readIORef var
writeIORef var (f oldVal)
return oldVal
makeBlockName = "Block_" ++ show (getAndUpdateVar gNewLabelNr (+1))
(I know I could use a Monad to avoid this kind of unsafe programming, but I
don't (didn't) want to rewrite existing code.)
The problem is, that with optimizations turned on (using ghc V6.2.1) the
label number is calulated only once, so each call to makeBlockName yields
the same value. I tried a) adding a dummy parameter to makeBlockName and b)
specifying an INLINE pragma to both getAndUpdateVar and makeBlockName, but
that doesn't help. What can I do to prevent optimization to optimize that
calculation away?
Thanks,
Bernd
More information about the Haskell
mailing list