Add default optimisation passes
[kaleidoscope-hs-old.git] / Main.hs
diff --git a/Main.hs b/Main.hs
index fcfe5dfff83e36cdf16525069f89620bf581e9df..f721911af3da8488e855ca46c966b736da2a5440 100644 (file)
--- a/Main.hs
+++ b/Main.hs
@@ -11,6 +11,7 @@ import System.IO
 import LLVM.Context
 import LLVM.ExecutionEngine
 import LLVM.Module
+import LLVM.PassManager
 import LLVM.IRBuilder
 import LLVM.AST.AddrSpace
 import LLVM.AST.Constant
@@ -18,17 +19,20 @@ import LLVM.AST.Float
 import LLVM.AST.Operand
 import LLVM.AST.Type as Type
 import LLVM.Pretty
+import           Control.Monad
 
 foreign import ccall "dynamic" exprFun :: FunPtr (IO Float) -> IO Float
 
 main :: IO ()
 main = do
-  program <- read <$> getContents
-  let mdl = buildModule "main" $ mapM buildAST program
-  Text.hPutStrLn stderr (ppllvm mdl)
+  AST.Program asts <- read <$> getContents
+  let mdl = buildModule "main" $ mapM buildAST asts
   withContext $ \ctx ->
     withMCJIT ctx Nothing Nothing Nothing Nothing $ \mcjit ->
       withModuleFromAST ctx mdl $ \mdl' ->
+        withPassManager defaultCuratedPassSetSpec $ \pm -> do
+          runPassManager pm mdl' >>= guard
+          Text.hPutStrLn stderr . ppllvm =<< moduleAST mdl'
           withModuleInEngine mcjit mdl' $ \emdl -> do
             Just f <- getFunction emdl "expr"
             let f' = castFunPtr f :: FunPtr (IO Float)