You're calling newNamedFunction "putchar"
inside the CodeGenModule
monad twice, which obviously has the side-effect of adding putchar
to the module twice. The fact that this results in two declarations instead of an error is probably a bug, please consider reporting it. To fix this, just make putc
a parameter of simple
and easy
. This will look approximately as follows (not tested):
simple :: Function (Int32 -> IO Word32) -> TFunction (Int32 -> IO Word32)
simple putc =
createNamedFunction ExternalLinkage "simple" $ \x -> do
call putc x
call putc x
ret (0 :: Word32)
easy :: Function (Int32 -> IO Word32) -> Function (Int32 -> IO Word32)
-> TFunction (Int32 -> IO Word32)
easy putc simple' =
createNamedFunction ExternalLinkage "easy" $ \x -> do
call simple' x
y <- add x (42 :: Int32)
call putc y
ret (0 :: Word32)
main :: IO ()
main = do
m <- newNamedModule "Main"
defineModule m $ do
putc <- newNamedFunction ExternalLinkage "putchar"
simple' <- simple putc
easy putc simple'
writeBitcodeToFile "SillyLib" m