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!
import Data.String
import qualified Data.Map as Map
import qualified Data.Text.Lazy.IO as Text
import Data.String
import qualified Data.Map as Map
import qualified Data.Text.Lazy.IO as Text
import LLVM.AST.AddrSpace
import LLVM.AST.Constant
import LLVM.AST.Float
import LLVM.AST.AddrSpace
import LLVM.AST.Constant
import LLVM.AST.Float
import System.IO.Error
import Text.Read (readMaybe)
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
data JITEnv = JITEnv
{ jitEnvContext :: Context
, jitEnvCompileLayer :: IRCompileLayer ObjectLinkingLayer
jit :: JITEnv -> Module -> IO Double
jit JITEnv{jitEnvCompileLayer=compLayer, jitEnvModuleKey=mdlKey} mdl =
withModule compLayer mdlKey mdl $ do
jit :: JITEnv -> Module -> IO Double
jit JITEnv{jitEnvCompileLayer=compLayer, jitEnvModuleKey=mdlKey} mdl =
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
type Binds = Map.Map String Operand