git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: optimize to 'auto' var intros where possible
6303070Andy Arvanitis
src/Language/PureScript/CodeGen/Cpp/File.hs+4−4
@@ -39,7 +39,7 @@ toHeader = catMaybes . map go
39 go cpp@(CppUseNamespace{}) = Just cpp
39 go cpp@(CppUseNamespace{}) = Just cpp
40 go (CppFunction _ _ _ qs _)
40 go (CppFunction _ _ _ qs _)
41 | CppInline `elem` qs = Nothing
41 | CppInline `elem` qs = Nothing
42− go (CppFunction _ [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp _ [_])])) | atyp == Just CppAuto
42+ go (CppFunction _ [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp _ [_])])) | atyp == Just (CppAuto [])
43 = Nothing
43 = Nothing
44 go (CppFunction name args rtyp qs _) =
44 go (CppFunction name args rtyp qs _) =
45 let args' = (\(_,t) -> ("", t)) <$> args in
45 let args' = (\(_,t) -> ("", t)) <$> args in
@@ -58,7 +58,7 @@ toHeader = catMaybes . map go
58 -- Generate thunks for top-level values
58 -- Generate thunks for top-level values
59 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
59 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
60 case cpp of
60 case cpp of
61− CppObjectLiteral CppInstance vals ->
61+ CppObjectLiteral CppInstance _ ->
62 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
62 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
63 _ ->
63 _ ->
64 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
64 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
@@ -81,7 +81,7 @@ toHeaderFns = catMaybes . map go
81 go cpp@(CppFunction _ _ _ qs _)
81 go cpp@(CppFunction _ _ _ qs _)
82 | CppInline `elem` qs = Just cpp
82 | CppInline `elem` qs = Just cpp
83 go (CppFunction name [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp cpp [_])]))
83 go (CppFunction name [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp cpp [_])]))
84− | atyp == Just CppAuto
84+ | Just CppAuto {} <- atyp
85 = Just (CppVariableIntroduction (name, Nothing) [] (Just cpp))
85 = Just (CppVariableIntroduction (name, Nothing) [] (Just cpp))
86 go cpp@(CppVariableIntroduction _ _ (Just CppNumericLiteral {})) =
86 go cpp@(CppVariableIntroduction _ _ (Just CppNumericLiteral {})) =
87 Just cpp
87 Just cpp
@@ -130,7 +130,7 @@ toBody = catMaybes . map go
130 -- Generate thunks for top-level values
130 -- Generate thunks for top-level values
131 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
131 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
132 case cpp of
132 case cpp of
133− CppObjectLiteral CppInstance vals ->
133+ CppObjectLiteral CppInstance _ ->
134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
135 _ ->
135 _ ->
136 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
136 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+4−3
@@ -43,6 +43,7 @@ import Prelude.Compat
43#if __GLASGOW_HASKELL__ < 710
43#if __GLASGOW_HASKELL__ < 710
44import Control.Applicative (Applicative)
44import Control.Applicative (Applicative)
45#endif
45#endif
46+import Control.Monad (liftM)
46import Control.Monad.Reader (MonadReader, ask, asks)
47import Control.Monad.Reader (MonadReader, ask, asks)
47import Control.Monad.Supply.Class (MonadSupply)
48import Control.Monad.Supply.Class (MonadSupply)
48
49
@@ -69,12 +70,11 @@ optimize nm cpp = do
69optimize' :: (Monad m, MonadReader Options m, Applicative m, MonadSupply m) => NamesMap -> Cpp -> m Cpp
70optimize' :: (Monad m, MonadReader Options m, Applicative m, MonadSupply m) => NamesMap -> Cpp -> m Cpp
70optimize' nm cpp = do
71optimize' nm cpp = do
71 opts <- ask
72 opts <- ask
72− untilFixedPoint (inlineFnComposition . applyAll
73+ untilFixedPoint (liftM toAutoVars . inlineFnComposition . applyAll
73 [ collapseNestedBlocks
74 [ collapseNestedBlocks
74 , collapseNestedIfs
75 , collapseNestedIfs
75 , collapseIfElses
76 , collapseIfElses
76 , removeCurrying nm
77 , removeCurrying nm
77− , toAutoVars
78 , tco opts
78 , tco opts
79 , magicDo opts
79 , magicDo opts
80 , removeCodeAfterReturnStatements
80 , removeCodeAfterReturnStatements
@@ -90,7 +90,8 @@ optimize' nm cpp = do
90 , inlineOperator (C.prelude, (C.#)) $ \x f -> CppApp f [x]
90 , inlineOperator (C.prelude, (C.#)) $ \x f -> CppApp f [x]
91 , inlineOperator (C.dataFunction, C.applyFlipped) $ \x f -> CppApp f [x]
91 , inlineOperator (C.dataFunction, C.applyFlipped) $ \x f -> CppApp f [x]
92 , inlineOperator (C.dataArrayUnsafe, C.unsafeIndex) $ flip CppIndexer
92 , inlineOperator (C.dataArrayUnsafe, C.unsafeIndex) $ flip CppIndexer
93− , inlineCommonOperators ]) cpp
93+ , inlineCommonOperators
94+ ]) cpp
94
95
95untilFixedPoint :: (Monad m, Eq a) => (a -> m a) -> a -> m a
96untilFixedPoint :: (Monad m, Eq a) => (a -> m a) -> a -> m a
96untilFixedPoint f = go
97untilFixedPoint f = go
src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs+11−7
@@ -306,15 +306,19 @@ toAutoVars = everywhereOnCpp convert
306 | CppStatic `notElem` qs,
306 | CppStatic `notElem` qs,
307 CppRef `notElem` tqs =
307 CppRef `notElem` tqs =
308 case value of
308 case value of
309− CppNumericLiteral {} -> autovar
309+ CppNumericLiteral {} -> var auto'
310− CppBooleanLiteral {} -> autovar
310+ CppBooleanLiteral {} -> var auto'
311− CppBinary {} -> autovar
311+ CppBinary {} -> var auto'
312− CppUnary {} -> autovar
312+ CppUnary {} -> var auto'
313+ CppApp {} -> var auto'
314+ CppVar {} -> var autoref'
315+ CppAccessor (CppVar {}) _ -> var autoref'
313 _ -> cpp
316 _ -> cpp
314 where
317 where
318+ auto' = CppAuto tqs
315− typ | CppConst `elem` tqs = CppConstAuto
319+ autoref' | CppConst `elem` tqs = CppAuto (CppRef : tqs)
316− | otherwise = CppAuto
320+ | otherwise = auto'
317− autovar = CppVariableIntroduction (ident, (Just typ)) qs (Just value)
321+ var t = CppVariableIntroduction (ident, (Just t)) qs (Just value)
318 convert cpp = cpp
322 convert cpp = cpp
319
323
320semiringNumber :: (String, String)
324semiringNumber :: (String, String)
src/Language/PureScript/CodeGen/Cpp/Optimizer/TCO.hs+2−2
@@ -111,7 +111,7 @@ tco' = everywhereOnCpp convert
111
111
112 toLoop :: String -> [String] -> Cpp -> Cpp
112 toLoop :: String -> [String] -> Cpp -> Cpp
113 toLoop ident allArgs cpp = CppBlock $
113 toLoop ident allArgs cpp = CppBlock $
114− map (\arg -> CppVariableIntroduction (arg, Just CppAuto) [] (Just (CppVar (copyVar arg)))) allArgs ++
114+ map (\arg -> CppVariableIntroduction (arg, Just $ CppAny []) [] (Just (CppVar (copyVar arg)))) allArgs ++
115 [ CppWhile (CppBooleanLiteral True) (CppBlock loop) ]
115 [ CppWhile (CppBooleanLiteral True) (CppBlock loop) ]
116 where
116 where
117 loop :: [Cpp]
117 loop :: [Cpp]
@@ -125,7 +125,7 @@ tco' = everywhereOnCpp convert
125 allArgumentValues = concat $ collectSelfCallArgs [] ret
125 allArgumentValues = concat $ collectSelfCallArgs [] ret
126 in
126 in
127 CppBlock $ zipWith (\val arg ->
127 CppBlock $ zipWith (\val arg ->
128− CppVariableIntroduction (tcoVar arg, Just CppAuto) [] (Just val)) allArgumentValues allArgs
128+ CppVariableIntroduction (tcoVar arg, Just $ CppAny [CppConst]) [] (Just val)) allArgumentValues allArgs
129 ++ map (\arg ->
129 ++ map (\arg ->
130 CppAssignment (CppVar arg) (CppVar (tcoVar arg))) allArgs
130 CppAssignment (CppVar arg) (CppVar (tcoVar arg))) allArgs
131 ++ [ CppContinue ]
131 ++ [ CppContinue ]
src/Language/PureScript/CodeGen/Cpp/Types.hs+13−7
@@ -28,7 +28,7 @@ import qualified Language.PureScript.Constants as C
28
28
29-- import Debug.Trace
29-- import Debug.Trace
30
30
31−data CppType = CppPrimitive String | CppAuto | CppConstAuto | CppAny [CppTypeQual]
31+data CppType = CppPrimitive String | CppAuto [CppTypeQual] | CppAny [CppTypeQual]
32 deriving (Show, Read, Eq)
32 deriving (Show, Read, Eq)
33
33
34data CppTypeQual = CppConst | CppRef
34data CppTypeQual = CppConst | CppRef
@@ -78,12 +78,18 @@ data CppObjectType = CppInstance | CppRecord
78
78
79runType :: CppType -> String
79runType :: CppType -> String
80runType (CppPrimitive t) = t
80runType (CppPrimitive t) = t
81−runType CppAuto = "auto"
81+runType (CppAuto []) = "auto"
82−runType CppConstAuto = "const auto"
82+runType (CppAny []) = "any"
83−runType (CppAny qs)
83+runType typ =
84+ case typ of
85+ CppAuto qs -> rendered CppAuto qs
86+ CppAny qs -> rendered CppAny qs
87+ _ -> rendered CppAny []
88+ where
89+ rendered t qs
84− | CppConst `elem` qs = "const " ++ (runType . CppAny $ delete CppConst qs)
90+ | CppConst `elem` qs = "const " ++ (runType . t $ delete CppConst qs)
85− | CppRef `elem` qs = runType (CppAny $ delete CppRef qs) ++ "&"
91+ | CppRef `elem` qs = runType (t $ delete CppRef qs) ++ "&"
86− | otherwise = "any"
92+ | otherwise = runType (t qs)
87
93
88runValueQual :: CppValueQual -> String
94runValueQual :: CppValueQual -> String
89runValueQual CppStatic = "static"
95runValueQual CppStatic = "static"
src/Language/PureScript/Pretty/Cpp.hs+1−1
@@ -514,7 +514,7 @@ dotsTo :: Char -> String -> String
514dotsTo chr' = map (\c -> if c == '.' then chr' else c)
514dotsTo chr' = map (\c -> if c == '.' then chr' else c)
515
515
516argstr :: (String, Maybe CppType) -> String
516argstr :: (String, Maybe CppType) -> String
517−argstr (name, Nothing) = argStr name CppAuto
517+argstr (name, Nothing) = argStr name (CppAuto [])
518argstr (name, Just typ) | name == C.__unused = argStr [] typ
518argstr (name, Just typ) | name == C.__unused = argStr [] typ
519argstr (name, Just typ) = argStr name typ
519argstr (name, Just typ) = argStr name typ
520
520