Skip to content

Instantly share code, notes, and snippets.

@robstewart57
Created October 28, 2013 09:51
Show Gist options
  • Save robstewart57/7194104 to your computer and use it in GitHub Desktop.
Save robstewart57/7194104 to your computer and use it in GitHub Desktop.
generating hello world LLVM function from Haskell
import Control.Monad
import Data.Word
import LLVM.Core
import LLVM.Util.File
-- prints out "hello world"
bldGreet :: CodeGenModule (Function (IO ()))
bldGreet = do
puts <- newNamedFunction ExternalLinkage "puts" :: TFunction (Ptr Word8 -> IO Word32)
func <- withStringNul "Hello, World!" $ \greetz ->
createFunction ExternalLinkage $ do
tmp <- getElementPtr greetz (0::Word32, (0::Word32, ()))
void $ call puts tmp
ret ()
return func
main :: IO ()
main = writeCodeGenModule "hello.bc" (_main =<< bldGreet)
where
_main :: (Function (IO ())) -> CodeGenModule (Function (IO ()))
_main func = createNamedFunction ExternalLinkage "main" $ do
call func
ret ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment