on master help sign in
Merge pull request #523 from Ralith/fix-llvm-install

LLVM-related fixes

commita068cb596beb7df136df69f695f76390c95d91d8 authorEdwin Brady <edwin.brady@gmail.com> dateSun 13 Oct 2013, 08:10 (13 years ago) parentde0bcbdef09742
6 files changed+74−21
.travis.yml+1−1 Makefile+3 Setup.hs+1−1 llvm/defs.c+4 src/IRTS/CodegenLLVM.hs+32−19 test/Makefile+33
.travis.yml+1−1
@@ -13,4 +13,4 @@ script:
1313 - cabal build
1414 - cabal copy
1515 - make test
16−
16+ - make test_llvm
Makefile+3
@@ -19,6 +19,9 @@ test:
1919test_java:
2020 $(MAKE) -C test IDRIS=../dist/build/idris test_java
2121
22+test_llvm:
23+ $(MAKE) -C test IDRIS=../dist/build/idris test_llvm
24+
2225relib:
2326 $(MAKE) -C lib IDRIS=../dist/build/idris/idris RTS=../dist/build/rts/libidris_rts clean
2427 $(MAKE) -C effects IDRIS=../dist/build/idris/idris RTS=../dist/build/rts/libidris_rts DIST=../dist/build clean
Setup.hs+1−1
@@ -134,7 +134,7 @@ idrisInstall verbosity copy pkg local = do
134134 installLLVM = do
135135 let target' = target </> "llvm"
136136 putStrLn $ "Installing LLVM library in " ++ target
137− makeInstall "llvm" target
137+ makeInstall "llvm" target'
138138
139139 makeInstall src target =
140140 make verbosity [ "-C", src, "install" , "TARGET=" ++ target, "IDRIS=" ++ idrisCmd local]
llvm/defs.c+4
@@ -11,6 +11,10 @@ void putStr(const char *str) {
1111 fputs(str, stdout);
1212}
1313
14+void putErr(const char *str) {
15+ fputs(str, stderr);
16+}
17+
1418void mpz_init_set_ull(mpz_t n, unsigned long long ull)
1519{
1620 mpz_init_set_ui(n, (unsigned int)(ull >> 32)); /* n = (unsigned int)(ull >> 32) */
src/IRTS/CodegenLLVM.hs+32−19
@@ -20,7 +20,7 @@ import LLVM.General.Target ( TargetMachine
2020 )
2121import LLVM.General.AST.DataLayout
2222import qualified LLVM.General.PassManager as PM
23−import qualified LLVM.General.Module as M
23+import qualified LLVM.General.Module as MO
2424import qualified LLVM.General.AST.IntegerPredicate as IPred
2525import qualified LLVM.General.AST.Linkage as L
2626import qualified LLVM.General.AST.Visibility as V
@@ -36,9 +36,9 @@ import qualified LLVM.General.CodeGenOpt as CGO
3636import Data.List
3737import Data.Maybe
3838import Data.Word
39−import Data.Set (Set)
40−import qualified Data.Set as S
39+import Data.Map (Map)
4140import qualified Data.Map as M
41+import qualified Data.Set as S
4242import qualified Data.Vector.Unboxed as V
4343import Control.Applicative
4444import Control.Monad.RWS
@@ -70,7 +70,7 @@ codegenLLVM defs triple cpu optimize file outty = withContext $ \context -> do
7070 withTargetMachine target triple cpu S.empty options R.Default CM.Default CGO.Default $ \tm ->
7171 do layout <- getTargetMachineDataLayout tm
7272 let ast = codegen (Target triple layout) (map snd defs)
73− result <- runErrorT . M.withModuleFromAST context ast $ \m ->
73+ result <- runErrorT . MO.withModuleFromAST context ast $ \m ->
7474 do let opts = PM.defaultCuratedPassSetSpec
7575 { PM.optLevel = Just optimize
7676 , PM.simplifyLibCalls = Just True
@@ -85,9 +85,9 @@ codegenLLVM defs triple cpu optimize file outty = withContext $ \context -> do
8585failInIO :: ErrorT String IO a -> IO a
8686failInIO = either fail return <=< runErrorT
8787
88−outputModule :: TargetMachine -> FilePath -> OutputType -> M.Module -> IO ()
89−outputModule _ file Raw m = failInIO $ M.writeBitcodeToFile file m
90−outputModule tm file Object m = failInIO $ M.writeObjectToFile tm file m
88+outputModule :: TargetMachine -> FilePath -> OutputType -> MO.Module -> IO ()
89+outputModule _ file Raw m = failInIO $ MO.writeBitcodeToFile file m
90+outputModule tm file Object m = failInIO $ MO.writeObjectToFile tm file m
9191outputModule tm file Executable m = withTmpFile $ \obj -> do
9292 outputModule tm obj Object m
9393 cc <- getCC
@@ -193,6 +193,7 @@ initDefs tgt =
193193 , exfun "__idris_gmpFree" VoidType [ptrI8, intPtr] False
194194 , exfun "__idris_strRev" ptrI8 [ptrI8] False
195195 , exfun "strtoll" (IntegerType 64) [ptrI8, PointerType ptrI8 (AddrSpace 0), IntegerType 32] False
196+ , exfun "putErr" VoidType [ptrI8] False
196197 , exVar (stdinName tgt) ptrI8
197198 , exVar (stdoutName tgt) ptrI8
198199 , exVar (stderrName tgt) ptrI8
@@ -244,7 +245,7 @@ getStdErr = ConstantOperand . C.GlobalReference . Name . stderrName <$> asks tar
244245codegen :: Target -> [SDecl] -> Module
245246codegen tgt defs = Module "idris" (Just . dataLayout $ tgt) (Just . triple $ tgt) (initDefs tgt ++ globals ++ gendefs)
246247 where
247− (gendefs, _, globals) = runRWS (mapM cgDef defs) tgt 0
248+ (gendefs, _, globals) = runRWS (mapM cgDef defs) tgt initialMGS
248249
249250valueType :: Type
250251valueType = NamedTypeReference (Name "valTy")
@@ -264,24 +265,34 @@ conType nargs = StructureType False
264265 , ArrayType nargs (PointerType valueType (AddrSpace 0))
265266 ]
266267
267−type Modgen = RWS Target [Definition] Word
268+data MGS = MGS { mgsNextGlobalName :: Word
269+ , mgsForeignSyms :: Map String (FType, [FType])
270+ }
271+
272+type Modgen = RWS Target [Definition] MGS
273+
274+initialMGS :: MGS
275+initialMGS = MGS { mgsNextGlobalName = 0
276+ , mgsForeignSyms = M.empty
277+ }
268278
269279cgDef :: SDecl -> Modgen Definition
270280cgDef (SFun name argNames _ expr) = do
271− nextGlobal <- get
281+ nextGlobal <- gets mgsNextGlobalName
282+ existingForeignSyms <- gets mgsForeignSyms
272283 tgt <- ask
273− let (_, CGS { nextGlobalName = nextGlobal' }, (allocas, bbs, globals)) =
284+ let (_, CGS { nextGlobalName = nextGlobal', foreignSyms = foreignSyms' }, (allocas, bbs, globals)) =
274285 runRWS (do r <- cgExpr expr
275286 case r of
276287 Nothing -> terminate $ Unreachable []
277288 Just r' -> terminate $ Ret (Just r') [])
278289 (CGR tgt (show name))
279− (CGS 0 nextGlobal (Name "begin") [] (map (Just . LocalReference . Name . show) argNames) S.empty)
290+ (CGS 0 nextGlobal (Name "begin") [] (map (Just . LocalReference . Name . show) argNames) existingForeignSyms)
280291 entryTerm = case bbs of
281292 [] -> Do $ Ret Nothing []
282293 BasicBlock n _ _:_ -> Do $ Br n []
283294 tell globals
284− put nextGlobal'
295+ put (MGS { mgsNextGlobalName = nextGlobal', mgsForeignSyms = foreignSyms' })
285296 return . GlobalDefinition $ functionDefaults
286297 { G.linkage = L.Internal
287298 , G.callingConvention = CC.Fast
@@ -306,7 +317,7 @@ data CGS = CGS { nextName :: Word
306317 , currentBlockName :: Name
307318 , instAccum :: [Named Instruction]
308319 , lexenv :: Env
309− , foreignSyms :: Set String
320+ , foreignSyms :: Map String (FType, [FType])
310321 }
311322
312323data CGR = CGR { target :: Target
@@ -524,10 +535,10 @@ cgExpr (SOp fn args) = do
524535 Just ops -> Just <$> cgOp fn ops
525536 Nothing -> return Nothing
526537cgExpr SNothing = return . Just . ConstantOperand $ nullValue
527−cgExpr (SError msg) = do -- TODO: Print message
538+cgExpr (SError msg) = do
528539 str <- addGlobal' (ArrayType (2 + fromIntegral (length msg)) (IntegerType 8))
529540 (cgConst' (TT.Str (msg ++ "\n")))
530− inst' $ simpleCall "putStr" [ConstantOperand $ C.GetElementPtr True str [ C.Int 32 0
541+ inst' $ simpleCall "putErr" [ConstantOperand $ C.GetElementPtr True str [ C.Int 32 0
531542 , C.Int 32 0]]
532543 inst' Call { isTailCall = True
533544 , callingConvention = CC.C
@@ -799,9 +810,11 @@ addGlobal' ty val = do
799810ensureCDecl :: String -> FType -> [FType] -> Codegen Operand
800811ensureCDecl name rty argtys = do
801812 syms <- gets foreignSyms
802− unless (S.member name syms) $
803− do addGlobal (ffunDecl name rty argtys)
804− modify $ \s -> s { foreignSyms = S.insert name (foreignSyms s) }
813+ case M.lookup name syms of
814+ Nothing -> do addGlobal (ffunDecl name rty argtys)
815+ modify $ \s -> s { foreignSyms = M.insert name (rty, argtys) (foreignSyms s) }
816+ Just (rty', argtys') -> unless (rty == rty' && argtys == argtys') . fail $
817+ "Mismatched type declarations for foreign symbol \"" ++ name ++ "\": " ++ show (rty, argtys) ++ " vs " ++ show (rty', argtys')
805818 return $ ConstantOperand (C.GlobalReference (Name name))
806819
807820ffunDecl :: String -> FType -> [FType] -> Global
test/Makefile+33
@@ -61,6 +61,39 @@ test_js:
6161 @perl ./runtest.pl test030 --codegen node
6262 @perl ./runtest.pl test031 --codegen node
6363
64+test_llvm:
65+ @perl ./runtest.pl test001 --codegen llvm
66+ @perl ./runtest.pl test002 --codegen llvm
67+ @perl ./runtest.pl test003 --codegen llvm
68+ @perl ./runtest.pl test004 --codegen llvm
69+ @perl ./runtest.pl test005 --codegen llvm
70+ @perl ./runtest.pl test006 --codegen llvm
71+ @perl ./runtest.pl test007 --codegen llvm
72+ @perl ./runtest.pl test008 --codegen llvm
73+ @perl ./runtest.pl test009 --codegen llvm
74+ @perl ./runtest.pl test010 --codegen llvm
75+ @perl ./runtest.pl test011 --codegen llvm
76+ @perl ./runtest.pl test012 --codegen llvm
77+ @perl ./runtest.pl test013 --codegen llvm
78+ @perl ./runtest.pl test014 --codegen llvm
79+ @perl ./runtest.pl test015 --codegen llvm
80+ @perl ./runtest.pl test016 --codegen llvm
81+ @perl ./runtest.pl test017 --codegen llvm
82+ @#perl ./runtest.pl test018 --codegen llvm
83+ @perl ./runtest.pl test019 --codegen llvm
84+ @perl ./runtest.pl test020 --codegen llvm
85+ @perl ./runtest.pl test021 --codegen llvm
86+ @perl ./runtest.pl test022 --codegen llvm
87+ @perl ./runtest.pl test023 --codegen llvm
88+ @perl ./runtest.pl test024 --codegen llvm
89+ @perl ./runtest.pl test025 --codegen llvm
90+ @perl ./runtest.pl test026 --codegen llvm
91+ @perl ./runtest.pl test027 --codegen llvm
92+ @perl ./runtest.pl test028 --codegen llvm
93+ @perl ./runtest.pl test029 --codegen llvm
94+ @perl ./runtest.pl test030 --codegen llvm
95+ @perl ./runtest.pl test031 --codegen llvm
96+
6497update:
6598 perl ./runtest.pl all -u
6699