git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Fix some TCO cases; includes inlining of apply
commita5cbe8575835571e80f61f2067f3b00d9642537b authorAndy Arvanitis <andyarvanitis@gmail.com> dateSun 30 Sep 2018, 20:02 (8 years ago) parentea8164d
3 files changed+20−10
src/CodeGen/Cpp.hs+2−1 src/CodeGen/Cpp/Optimizer.hs+14−4 src/CodeGen/Cpp/Optimizer/TCO.hs+4−5
src/CodeGen/Cpp.hs+2−1
@@ -58,7 +58,7 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
5858 interfaceImport <- importToCpp (renameImports [] [(emptyAnn, mn)]) mn
5959 let decls' = renameModules mnLookup decls
6060 cppDecls <- mapM (bindToCpp ModuleDecl) decls'
61− optimized <- traverse (traverse optimize) cppDecls
61+ optimized <- traverse (traverse (optimize modName')) cppDecls
6262 let optimized' = concat optimized
6363 exports' = mapMaybe (export $ identToCpp <$> exports) optimized'
6464 foreigns' = identToCpp <$> foreigns
@@ -68,6 +68,7 @@ moduleToCpp (Module _ coms mn _ imps exports foreigns decls) _ =
6868 return $ (interface, foreigns', optimized', implHeader, implFooter)
6969 where
7070 modName = moduleNameToCpp mn
71+ modName' = AST.Var Nothing modName
7172
7273 export :: [Text] -> AST -> Maybe (Text, Bool)
7374 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
1919
2020
2121-- | 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
2323-- optimize = untilFixedPoint $ return . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp . tco
24−optimize cpp = do
25− cpp' <- untilFixedPoint (return . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp) cpp
26− untilFixedPoint (return . tidyUp) . tco
24+optimize mn cpp = do
25+ cpp' <- untilFixedPoint (return . inlineApply . inlineUnsafeCoerce . inlineUnsafePartial . tidyUp) cpp
26+ untilFixedPoint (return . tidyUp) . tco mn
2727 =<< untilFixedPoint (return . magicDo')
2828 =<< untilFixedPoint (return . magicDo) cpp'
2929
@@ -66,6 +66,16 @@ inlineUnsafeCoerce = everywhereTopDown convert where
6666 = comp
6767 convert other = other
6868
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+
6979collapseIfChecks :: AST -> AST
7080collapseIfChecks = everywhere collapse where
7181 collapse :: AST -> AST
src/CodeGen/Cpp/Optimizer/TCO.hs+4−5
@@ -7,13 +7,12 @@ import Data.Monoid ((<>))
77import Language.PureScript.CoreImp.AST
88import Language.PureScript.AST.SourcePos (SourceSpan)
99import Safe (headDef, tailSafe)
10−
1110import CodeGen.Cpp.Common
1211
1312import qualified Language.PureScript.Constants as C
1413
15−tco :: AST -> AST
16−tco = everywhere convert where
14+tco :: AST -> AST -> AST
15+tco mn = everywhere convert where
1716 tcoVar :: Text -> Text
1817 tcoVar arg = "$TCO_" <> arg <> "$"
1918
@@ -139,6 +138,6 @@ tco = everywhere convert where
139138
140139 isSelfCall :: Text -> AST -> Bool
141140 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'
143142 isSelfCall ident (App _ fn _) = isSelfCall ident fn
144− isSelfCall _ _ = False
143+ isSelfCall _ _ = False