@@ -193,6 +193,7 @@ initDefs tgt =
193 , exfun "__idris_gmpFree" VoidType [ptrI8, intPtr] False
193 , exfun "__idris_gmpFree" VoidType [ptrI8, intPtr] False
194 , exfun "__idris_strRev" ptrI8 [ptrI8] False
194 , exfun "__idris_strRev" ptrI8 [ptrI8] False
195 , exfun "strtoll" (IntegerType 64) [ptrI8, PointerType ptrI8 (AddrSpace 0), IntegerType 32] False
195 , exfun "strtoll" (IntegerType 64) [ptrI8, PointerType ptrI8 (AddrSpace 0), IntegerType 32] False
196+ , exfun "putStr" VoidType [ptrI8] False
196 , exVar (stdinName tgt) ptrI8
197 , exVar (stdinName tgt) ptrI8
197 , exVar (stdoutName tgt) ptrI8
198 , exVar (stdoutName tgt) ptrI8
198 , exVar (stderrName tgt) ptrI8
199 , exVar (stderrName tgt) ptrI8
@@ -264,24 +265,34 @@ conType nargs = StructureType False
264 , ArrayType nargs (PointerType valueType (AddrSpace 0))
265 , ArrayType nargs (PointerType valueType (AddrSpace 0))
268+data MGS = MGS { mgsNextGlobalName :: Word
269+ , mgsForeignSyms :: Set String
267−type Modgen = RWS Target [Definition] Word
272+type Modgen = RWS Target [Definition] MGS
275+initialMGS = MGS { mgsNextGlobalName = 0
276+ , mgsForeignSyms = S.empty
269cgDef :: SDecl -> Modgen Definition
279cgDef :: SDecl -> Modgen Definition
270cgDef (SFun name argNames _ expr) = do
280cgDef (SFun name argNames _ expr) = do
271− nextGlobal <- get
281+ nextGlobal <- gets mgsNextGlobalName
282+ existingForeignSyms <- gets mgsForeignSyms
272 tgt <- ask
283 tgt <- ask
273− let (_, CGS { nextGlobalName = nextGlobal' }, (allocas, bbs, globals)) =
284+ let (_, CGS { nextGlobalName = nextGlobal', foreignSyms = foreignSyms' }, (allocas, bbs, globals)) =
274 runRWS (do r <- cgExpr expr
285 runRWS (do r <- cgExpr expr
275 case r of
286 case r of
276 Nothing -> terminate $ Unreachable []
287 Nothing -> terminate $ Unreachable []
277 Just r' -> terminate $ Ret (Just r') [])
288 Just r' -> terminate $ Ret (Just r') [])
278 (CGR tgt (show name))
289 (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)
280 entryTerm = case bbs of
291 entryTerm = case bbs of
281 [] -> Do $ Ret Nothing []
292 [] -> Do $ Ret Nothing []
282 BasicBlock n _ _:_ -> Do $ Br n []
293 BasicBlock n _ _:_ -> Do $ Br n []
283 tell globals
294 tell globals
284− put nextGlobal'
295+ put (MGS { mgsNextGlobalName = nextGlobal', mgsForeignSyms = foreignSyms' })
285 return . GlobalDefinition $ functionDefaults
296 return . GlobalDefinition $ functionDefaults
286 { G.linkage = L.Internal
297 { G.linkage = L.Internal
287 , G.callingConvention = CC.Fast
298 , G.callingConvention = CC.Fast
@@ -524,7 +535,7 @@ cgExpr (SOp fn args) = do
524 Just ops -> Just <$> cgOp fn ops
535 Just ops -> Just <$> cgOp fn ops
525 Nothing -> return Nothing
536 Nothing -> return Nothing
526cgExpr SNothing = return . Just . ConstantOperand $ nullValue
537cgExpr SNothing = return . Just . ConstantOperand $ nullValue
527−cgExpr (SError msg) = do -- TODO: Print message
538+cgExpr (SError msg) = do
528 str <- addGlobal' (ArrayType (2 + fromIntegral (length msg)) (IntegerType 8))
539 str <- addGlobal' (ArrayType (2 + fromIntegral (length msg)) (IntegerType 8))
529 (cgConst' (TT.Str (msg ++ "\n")))
540 (cgConst' (TT.Str (msg ++ "\n")))
530 inst' $ simpleCall "putStr" [ConstantOperand $ C.GetElementPtr True str [ C.Int 32 0
541 inst' $ simpleCall "putStr" [ConstantOperand $ C.GetElementPtr True str [ C.Int 32 0