From 4d4c0e626e0002e19345a1fd47145c1701583d4c Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Sun, 2 Jun 2019 19:33:35 +0100 Subject: [PATCH] Find our JIT'ed function and run it Here is where the magic happens. We first mangle the symbol before passing it to findSymbolIn: The compilation then happens here. It will spit back a FFI pointer, which we then need to invoke ourselves. In order to do this we have to specify the Haskell type of the function that we're pointing to, which resides in C-land, hence the ccall attribute in our foreign declaration. Try it out! --- Main.hs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Main.hs b/Main.hs index 5bc3a3e..48d93a2 100644 --- 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 @@ -84,7 +87,9 @@ repl = do jit :: JITEnv -> Module -> IO Double jit JITEnv{jitEnvCompileLayer=compLayer, jitEnvModuleKey=mdlKey} mdl = withModule compLayer mdlKey mdl $ do - return 0 + mangled <- mangleSymbol compLayer "__anon_expr" + Right (JITSymbol fPtr _) <- findSymbolIn compLayer mdlKey mangled False + mkFun (castPtrToFunPtr (wordPtrToPtr fPtr)) type Binds = Map.Map String Operand -- 2.30.2