{-# LANGUAGE OverloadedStrings #-} module Main where import qualified AST import qualified Data.Text.Lazy.IO as Text import Foreign.Ptr import System.IO import LLVM.Context import LLVM.CodeModel import LLVM.ExecutionEngine import LLVM.Module import LLVM.IRBuilder import LLVM.AST.Constant import LLVM.AST.Float import LLVM.AST.Operand import LLVM.AST.Type import LLVM.Pretty foreign import ccall "dynamic" exprFun :: FunPtr (IO Float) -> IO Float main :: IO () main = do ast <- read <$> getContents let mdl = buildModule "main" $ function "expr" [] float $ \_ -> build ast >>= ret Text.hPutStrLn stderr (ppllvm mdl) withContext $ \ctx -> withMCJIT ctx Nothing Nothing Nothing Nothing $ \mcjit -> withModuleFromAST ctx mdl $ \mdl' -> withModuleInEngine mcjit mdl' $ \emdl -> do Just f <- getFunction emdl "expr" let f' = castFunPtr f :: FunPtr (IO Float) exprFun f' >>= print build :: AST.Expr -> IRBuilderT ModuleBuilder Operand build (AST.Num a) = pure $ ConstantOperand (Float (Single a)) build (AST.BinOp op a b) = do va <- build a vb <- build b let instr = case op of AST.Add -> fadd AST.Sub -> fsub AST.Mul -> fmul instr va vb