git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix some TCO cases; includes inlining of apply
a5cbe85Andy Arvanitis
src/CodeGen/Cpp.hs+2−1
@@ -58,7 +58,7 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
58 interfaceImport <- importToCpp (renameImports [] [(emptyAnn, mn)]) mn
58 interfaceImport <- importToCpp (renameImports [] [(emptyAnn, mn)]) mn
59 let decls' = renameModules mnLookup decls
59 let decls' = renameModules mnLookup decls
60 cppDecls <- mapM (bindToCpp ModuleDecl) decls'
60 cppDecls <- mapM (bindToCpp ModuleDecl) decls'
61− optimized <- traverse (traverse optimize) cppDecls
61+ optimized <- traverse (traverse (optimize modName')) cppDecls
62 let optimized' = concat optimized
62 let optimized' = concat optimized
63 exports' = mapMaybe (export $ identToCpp <$> exports) optimized'
63 exports' = mapMaybe (export $ identToCpp <$> exports) optimized'
64 foreigns' = identToCpp <$> foreigns
64 foreigns' = identToCpp <$> foreigns
@@ -68,6 +68,7 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
68 return $ (interface, foreigns', optimized', implHeader, implFooter)
68 return $ (interface, foreigns', optimized', implHeader, implFooter)
69 where
69 where
70 modName = moduleNameToCpp mn
70 modName = moduleNameToCpp mn
71+ modName' = AST.Var Nothing modName
71
72
72 export :: [Text] -> AST -> Maybe (Text, Bool)
73 export :: [Text] -> AST -> Maybe (Text, Bool)
73 export names (AST.Function _ (Just name) [] (AST.Block _ [AST.Return _ ret]))
74 export names (AST.Function _ (Just name) [] (AST.Block _ [AST.Return _ ret]))
src/CodeGen/Cpp/Optimizer.hs+14−4
@@ -19,11 +19,11 @@ import qualified Language.PureScript.Constants as C
19
19
20
20
21-- | Apply a series of optimizer passes to simplified Objective-C code
21-- | Apply a series of optimizer passes to simplified Objective-C code
22−optimize :: MonadSupply m => AST -> m AST
22+optimize :: MonadSupply m => AST -> AST -> m AST
23-- optimize = untilFixedPoint $ return . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp . tco
23-- optimize = untilFixedPoint $ return . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp . tco
24−optimize cpp = do
24+optimize mn cpp = do
25− cpp' <- untilFixedPoint (return . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp) cpp
25+ cpp' <- untilFixedPoint (return . inlineApply . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp) cpp
26− untilFixedPoint (return . tidyUp) . tco
26+ untilFixedPoint (return . tidyUp) . tco mn
27 =<< untilFixedPoint (return . magicDo')
27 =<< untilFixedPoint (return . magicDo')
28 =<< untilFixedPoint (return . magicDo) cpp'
28 =<< untilFixedPoint (return . magicDo) cpp'
29
29
@@ -66,6 +66,16 @@ inlineUnsafeCoerce = everywhereTopDown convert where
66 = comp
66 = comp
67 convert other = other
67 convert other = other
68
68
69+inlineApply :: AST -> AST
70+inlineApply = everywhereTopDown convert where
71+ convert (App ss (App _ (Indexer _ (Var _ apply) (Var _ dataFunction)) [ f ]) [ arg ])
72+ | apply == C.apply && dataFunction == C.dataFunction
73+ = App ss f [ arg ]
74+ convert (App ss (App _ (Indexer _ (Var _ applyFlipped) (Var _ dataFunction)) [ arg ]) [ f ])
75+ | applyFlipped == C.applyFlipped && dataFunction == C.dataFunction
76+ = App ss f [ arg ]
77+ convert other = other
78+
69collapseIfChecks :: AST -> AST
79collapseIfChecks :: AST -> AST
70collapseIfChecks = everywhere collapse where
80collapseIfChecks = everywhere collapse where
71 collapse :: AST -> AST
81 collapse :: AST -> AST
src/CodeGen/Cpp/Optimizer/TCO.hs+4−5
@@ -7,13 +7,12 @@ import Data.Monoid ((<>))
7import Language.PureScript.CoreImp.AST
7import Language.PureScript.CoreImp.AST
8import Language.PureScript.AST.SourcePos (SourceSpan)
8import Language.PureScript.AST.SourcePos (SourceSpan)
9import Safe (headDef, tailSafe)
9import Safe (headDef, tailSafe)
10−
11import CodeGen.Cpp.Common
10import CodeGen.Cpp.Common
12
11
13import qualified Language.PureScript.Constants as C
12import qualified Language.PureScript.Constants as C
14
13
15−tco :: AST -> AST
14+tco :: AST -> AST -> AST
16−tco = everywhere convert where
15+tco mn = everywhere convert where
17 tcoVar :: Text -> Text
16 tcoVar :: Text -> Text
18 tcoVar arg = "$TCO_" <> arg <> "$"
17 tcoVar arg = "$TCO_" <> arg <> "$"
19
18
@@ -139,6 +138,6 @@ tco = everywhere convert where
139
138
140 isSelfCall :: Text -> AST -> Bool
139 isSelfCall :: Text -> AST -> Bool
141 isSelfCall ident (App _ (Var _ ident') _) = ident == ident'
140 isSelfCall ident (App _ (Var _ ident') _) = ident == ident'
142− isSelfCall ident (App _ (Indexer _ (Var _ ident') _) _) = ident == ident'
141+ isSelfCall ident (App _ (Indexer _ (Var _ ident') mn') _) = mn' == mn && ident == ident'
143 isSelfCall ident (App _ fn _) = isSelfCall ident fn
142 isSelfCall ident (App _ fn _) = isSelfCall ident fn
144− isSelfCall _ _ = False
143+ isSelfCall _ _ = False