@@ -6,6 +6,9 @@ import IRTS.Lang
66import IRTS.Simplified
77import qualified Core.TT as TT
88
9+import Util.System
10+import Paths_idris
11+
912import LLVM.General.Context
1013import LLVM.General.Diagnostic
1114import LLVM.General.AST
@@ -31,6 +34,11 @@ import Control.Monad.Writer
3134import Control.Monad.State
3235
3336import System.Info (arch)
37+import System.IO
38+import System.Directory (removeFile)
39+import System.FilePath ((</>))
40+import System.Process (rawSystem)
41+import System.Exit (ExitCode(..))
3442import Debug.Trace
3543
3644codegenLLVM :: [(TT.Name, SDecl)] ->
@@ -39,11 +47,32 @@ codegenLLVM :: [(TT.Name, SDecl)] ->
3947 IO ()
4048codegenLLVM defs file outty = withContext $ \context -> do
4149 let ast = codegen arch (map snd defs)
42− result <- M.withModuleFromAST context ast (M.writeBitcodeToFile file)
50+ result <- M.withModuleFromAST context ast (outputModule file outty)
4351 case result of
4452 Right _ -> return ()
4553 Left msg -> ierror msg
4654
55+outputModule :: FilePath -> OutputType -> M.Module -> IO ()
56+outputModule file Raw m = M.writeBitcodeToFile file m
57+outputModule file Object m = withTmpFile $ \bc -> do
58+ outputModule bc Raw m
59+ exit <- rawSystem "llc" ["-filetype=obj", "--disable-fp-elim", "-o", file, bc]
60+ when (exit /= ExitSuccess) $ ierror "FAILURE: Object generation"
61+outputModule file Executable m = withTmpFile $ \obj -> do
62+ outputModule obj Object m
63+ cc <- getCC
64+ defs <- (</> "llvm" </> "libidris_rts.a") <$> getDataDir
65+ exit <- rawSystem cc [obj, defs, "-lm", "-lgmp", "-lgc", "-o", file]
66+ when (exit /= ExitSuccess) $ ierror "FAILURE: Linking"
67+
68+withTmpFile :: (FilePath -> IO a) -> IO a
69+withTmpFile f = do
70+ (path, handle) <- tempfile
71+ hClose handle
72+ result <- f path
73+ removeFile path
74+ return result
75+
4776ierror :: String -> a
4877ierror msg = error $ "INTERNAL ERROR: IRTS.CodegenLLVM: " ++ msg
4978