1 {-# LANGUAGE OverloadedStrings #-}
6 import qualified Data.Text.Lazy.IO as Text
11 import LLVM.ExecutionEngine
14 import LLVM.AST.Constant
16 import LLVM.AST.Operand
20 foreign import ccall "dynamic" exprFun :: FunPtr (IO Float) -> IO Float
24 ast <- read <$> getContents
25 let mdl = buildModule "main" $
26 function "expr" [] float $ \_ -> build ast >>= ret
27 Text.hPutStrLn stderr (ppllvm mdl)
29 withMCJIT ctx Nothing Nothing Nothing Nothing $ \mcjit ->
30 withModuleFromAST ctx mdl $ \mdl' ->
31 withModuleInEngine mcjit mdl' $ \emdl -> do
32 Just f <- getFunction emdl "expr"
33 let f' = castFunPtr f :: FunPtr (IO Float)
36 build :: AST.Expr -> IRBuilderT ModuleBuilder Operand
37 build (AST.Num a) = pure $ ConstantOperand (Float (Single a))
38 build (AST.BinOp op a b) = do
41 let instr = case op of