-
Notifications
You must be signed in to change notification settings - Fork 17
/
JIT.hs
55 lines (48 loc) · 1.66 KB
/
JIT.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{-# LANGUAGE OverloadedStrings #-}
module JIT where
import Control.Monad.Except
import qualified Data.ByteString.Char8 as ByteString
import Data.Int
import Data.Word
import Foreign.Ptr (FunPtr, castFunPtr)
import qualified LLVM.AST as AST
import LLVM.Analysis
import LLVM.CodeModel
import LLVM.Context
import qualified LLVM.ExecutionEngine as EE
import LLVM.Module as Mod
import LLVM.PassManager
import LLVM.Target
import LLVM.Transforms
foreign import ccall "dynamic" haskFun :: FunPtr (IO Double) -> (IO Double)
run :: FunPtr a -> IO Double
run fn = haskFun (castFunPtr fn :: FunPtr (IO Double))
jit :: Context -> (EE.MCJIT -> IO a) -> IO a
jit c = EE.withMCJIT c optlevel model ptrelim fastins
where
optlevel = Just 0 -- optimization level
model = Nothing -- code model ( Default )
ptrelim = Nothing -- frame pointer elimination
fastins = Nothing -- fast instruction selection
passes :: PassSetSpec
passes = defaultCuratedPassSetSpec {optLevel = Just 3}
runJIT :: AST.Module -> IO AST.Module
runJIT mod = do
withContext $ \context ->
jit context $ \executionEngine ->
withModuleFromAST context mod $ \m ->
withPassManager passes $ \pm -> do
-- Optimization Pass
{-runPassManager pm m-}
optmod <- moduleAST m
s <- moduleLLVMAssembly m
ByteString.putStrLn s
EE.withModuleInEngine executionEngine m $ \ee -> do
mainfn <- EE.getFunction ee "main"
case mainfn of
Just fn -> do
res <- run fn
putStrLn $ "Evaluated to: " ++ show res
Nothing -> return ()
-- Return the optimized module
return optmod