{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE RecursiveDo #-}
import AST as K -- K for Kaleidoscope
import Utils
import Data.String
import qualified Data.Map as Map
import qualified Data.Text.Lazy.IO as Text
+import Foreign.Ptr
import LLVM.AST.AddrSpace
import LLVM.AST.Constant
import LLVM.AST.Float
import System.IO.Error
import Text.Read (readMaybe)
+foreign import ccall "dynamic" mkFun :: FunPtr (IO Double) -> IO Double
+
data JITEnv = JITEnv
{ jitEnvContext :: Context
, jitEnvCompileLayer :: IRCompileLayer ObjectLinkingLayer
jit :: JITEnv -> Module -> IO Double
jit JITEnv{jitEnvCompileLayer=compLayer, jitEnvModuleKey=mdlKey} mdl =
- withModule compLayer mdlKey mdl $
- return 0
+ withModule compLayer mdlKey mdl $ do
+ mangled <- mangleSymbol compLayer "__anon_expr"
+ Right (JITSymbol fPtr _) <- findSymbolIn compLayer mdlKey mangled False
+ mkFun (castPtrToFunPtr (wordPtrToPtr fPtr))
type Binds = Map.Map String Operand
ptrTyp = Type.PointerType typ (AddrSpace 0)
ref = GlobalReference ptrTyp nam
call (ConstantOperand ref) (zip paramOps (repeat []))
+
+buildExpr (If cond thenE elseE) = mdo
+ _ifB <- block `named` "if"
+
+ -- since everything is a double, false == 0
+ let zero = ConstantOperand (Float (Double 0))
+ condV <- buildExpr cond
+ cmp <- fcmp ONE zero condV `named` "cmp"
+
+ condBr cmp thenB elseB
+
+ thenB <- block `named` "then"
+ thenOp <- buildExpr thenE
+ br mergeB
+
+ elseB <- block `named` "else"
+ elseOp <- buildExpr elseE
+ br mergeB
+
+ mergeB <- block `named` "ifcont"
+ phi [(thenOp, thenB), (elseOp, elseB)]