module Main (main) where

import LLVM.ExecutionEngine (simpleFunction)
import LLVM.Core

import Foreign.Ptr (Ptr)
import Data.Word (Word8, Word32)


bldGreet :: CodeGenModule (Function (IO ()))
bldGreet = withStringNul "Hello, JIT!" (\greetz -> do
    puts <- newNamedFunction ExternalLinkage "puts" :: TFunction (Ptr Word8 -> IO Word32)
    func <- createFunction ExternalLinkage $ do
      tmp <- getElementPtr0 greetz (0::Word32, ())
      _ <- call puts tmp :: CodeGenFunction r (Value Word32)
      ret ()
    return func)

main :: IO ()
main = do
    initializeNativeTarget
    greet <- simpleFunction bldGreet
    greet
    greet
    greet
    return ()
