on master help sign in
Keep track of prototyped foreign symbols across definitions
commit7a43e68a21976a1003d94a9927c0750f9bba610e authorBenjamin Saunders <besaunde@sfu.ca> dateTue 8 Oct 2013, 15:49 (13 years ago) parent03adfb0
1 file changed+17−6
src/IRTS/CodegenLLVM.hs+17−6
src/IRTS/CodegenLLVM.hs+17−6
@@ -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 "putStr" VoidType [ptrI8] False
196197 , exVar (stdinName tgt) ptrI8
197198 , exVar (stdoutName tgt) ptrI8
198199 , exVar (stderrName tgt) ptrI8
@@ -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 :: Set String
270+ }
271+
272+type Modgen = RWS Target [Definition] MGS
273+
274+initialMGS :: MGS
275+initialMGS = MGS { mgsNextGlobalName = 0
276+ , mgsForeignSyms = S.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
@@ -524,7 +535,7 @@ 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")))
530541 inst' $ simpleCall "putStr" [ConstantOperand $ C.GetElementPtr True str [ C.Int 32 0