Find our JIT'ed function and run it
[kaleidoscope-hs.git] / Main.hs
diff --git a/Main.hs b/Main.hs
index bc7c3077f9b6bbdda78273381921826110acf62a..468573d865ab4fccb350bc78bf446d4e9d400563 100644 (file)
--- a/Main.hs
+++ b/Main.hs
@@ -9,6 +9,7 @@ import Control.Monad.IO.Class
 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
@@ -27,6 +28,8 @@ import System.IO
 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
@@ -83,8 +86,10 @@ repl = do
 
 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