git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: optimize to 'auto' var intros where possible
commit630307096879055cf4e949f7fc3e5d8653bebebe authorAndy Arvanitis <andyarvanitis@gmail.com> dateThu 15 Sep 2016, 00:19 (10 years ago) parent82c82ca
6 files changed+35−24
src/Language/PureScript/CodeGen/Cpp/File.hs+4−4 src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+4−3 src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs+11−7 src/Language/PureScript/CodeGen/Cpp/Optimizer/TCO.hs+2−2 src/Language/PureScript/CodeGen/Cpp/Types.hs+13−7 src/Language/PureScript/Pretty/Cpp.hs+1−1
src/Language/PureScript/CodeGen/Cpp/File.hs+4−4
@@ -39,7 +39,7 @@ toHeader = catMaybes . map go
3939 go cpp@(CppUseNamespace{}) = Just cpp
4040 go (CppFunction _ _ _ qs _)
4141 | CppInline `elem` qs = Nothing
42− go (CppFunction _ [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp _ [_])])) | atyp == Just CppAuto
42+ go (CppFunction _ [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp _ [_])])) | atyp == Just (CppAuto [])
4343 = Nothing
4444 go (CppFunction name args rtyp qs _) =
4545 let args' = (\(_,t) -> ("", t)) <$> args in
@@ -58,7 +58,7 @@ toHeader = catMaybes . map go
5858 -- Generate thunks for top-level values
5959 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
6060 case cpp of
61− CppObjectLiteral CppInstance vals ->
61+ CppObjectLiteral CppInstance _ ->
6262 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
6363 _ ->
6464 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
@@ -81,7 +81,7 @@ toHeaderFns = catMaybes . map go
8181 go cpp@(CppFunction _ _ _ qs _)
8282 | CppInline `elem` qs = Just cpp
8383 go (CppFunction name [(_, atyp)] _ _ (CppBlock [CppReturn (CppApp cpp [_])]))
84− | atyp == Just CppAuto
84+ | Just CppAuto {} <- atyp
8585 = Just (CppVariableIntroduction (name, Nothing) [] (Just cpp))
8686 go cpp@(CppVariableIntroduction _ _ (Just CppNumericLiteral {})) =
8787 Just cpp
@@ -130,7 +130,7 @@ toBody = catMaybes . map go
130130 -- Generate thunks for top-level values
131131 go (CppVariableIntroduction (name, _) _ (Just cpp)) =
132132 case cpp of
133− CppObjectLiteral CppInstance vals ->
133+ CppObjectLiteral CppInstance _ ->
134134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
135135 _ ->
136136 Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+4−3
@@ -43,6 +43,7 @@ import Prelude.Compat
4343#if __GLASGOW_HASKELL__ < 710
4444import Control.Applicative (Applicative)
4545#endif
46+import Control.Monad (liftM)
4647import Control.Monad.Reader (MonadReader, ask, asks)
4748import Control.Monad.Supply.Class (MonadSupply)
4849
@@ -69,12 +70,11 @@ optimize nm cpp = do
6970optimize' :: (Monad m, MonadReader Options m, Applicative m, MonadSupply m) => NamesMap -> Cpp -> m Cpp
7071optimize' nm cpp = do
7172 opts <- ask
72− untilFixedPoint (inlineFnComposition . applyAll
73+ untilFixedPoint (liftM toAutoVars . inlineFnComposition . applyAll
7374 [ collapseNestedBlocks
7475 , collapseNestedIfs
7576 , collapseIfElses
7677 , removeCurrying nm
77− , toAutoVars
7878 , tco opts
7979 , magicDo opts
8080 , removeCodeAfterReturnStatements
@@ -90,7 +90,8 @@ optimize' nm cpp = do
9090 , inlineOperator (C.prelude, (C.#)) $ \x f -> CppApp f [x]
9191 , inlineOperator (C.dataFunction, C.applyFlipped) $ \x f -> CppApp f [x]
9292 , inlineOperator (C.dataArrayUnsafe, C.unsafeIndex) $ flip CppIndexer
93− , inlineCommonOperators ]) cpp
93+ , inlineCommonOperators
94+ ]) cpp
9495
9596untilFixedPoint :: (Monad m, Eq a) => (a -> m a) -> a -> m a
9697untilFixedPoint f = go
src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs+11−7
@@ -306,15 +306,19 @@ toAutoVars = everywhereOnCpp convert
306306 | CppStatic `notElem` qs,
307307 CppRef `notElem` tqs =
308308 case value of
309− CppNumericLiteral {} -> autovar
310− CppBooleanLiteral {} -> autovar
311− CppBinary {} -> autovar
312− CppUnary {} -> autovar
309+ CppNumericLiteral {} -> var auto'
310+ CppBooleanLiteral {} -> var auto'
311+ CppBinary {} -> var auto'
312+ CppUnary {} -> var auto'
313+ CppApp {} -> var auto'
314+ CppVar {} -> var autoref'
315+ CppAccessor (CppVar {}) _ -> var autoref'
313316 _ -> cpp
314317 where
315− typ | CppConst `elem` tqs = CppConstAuto
316− | otherwise = CppAuto
317− autovar = CppVariableIntroduction (ident, (Just typ)) qs (Just value)
318+ auto' = CppAuto tqs
319+ autoref' | CppConst `elem` tqs = CppAuto (CppRef : tqs)
320+ | otherwise = auto'
321+ var t = CppVariableIntroduction (ident, (Just t)) qs (Just value)
318322 convert cpp = cpp
319323
320324semiringNumber :: (String, String)
src/Language/PureScript/CodeGen/Cpp/Optimizer/TCO.hs+2−2
@@ -111,7 +111,7 @@ tco' = everywhereOnCpp convert
111111
112112 toLoop :: String -> [String] -> Cpp -> Cpp
113113 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 ++
115115 [ CppWhile (CppBooleanLiteral True) (CppBlock loop) ]
116116 where
117117 loop :: [Cpp]
@@ -125,7 +125,7 @@ tco' = everywhereOnCpp convert
125125 allArgumentValues = concat $ collectSelfCallArgs [] ret
126126 in
127127 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
129129 ++ map (\arg ->
130130 CppAssignment (CppVar arg) (CppVar (tcoVar arg))) allArgs
131131 ++ [ CppContinue ]
src/Language/PureScript/CodeGen/Cpp/Types.hs+13−7
@@ -28,7 +28,7 @@ import qualified Language.PureScript.Constants as C
2828
2929-- import Debug.Trace
3030
31−data CppType = CppPrimitive String | CppAuto | CppConstAuto | CppAny [CppTypeQual]
31+data CppType = CppPrimitive String | CppAuto [CppTypeQual] | CppAny [CppTypeQual]
3232 deriving (Show, Read, Eq)
3333
3434data CppTypeQual = CppConst | CppRef
@@ -78,12 +78,18 @@ data CppObjectType = CppInstance | CppRecord
7878
7979runType :: CppType -> String
8080runType (CppPrimitive t) = t
81−runType CppAuto = "auto"
82−runType CppConstAuto = "const auto"
83−runType (CppAny qs)
84− | CppConst `elem` qs = "const " ++ (runType . CppAny $ delete CppConst qs)
85− | CppRef `elem` qs = runType (CppAny $ delete CppRef qs) ++ "&"
86− | otherwise = "any"
81+runType (CppAuto []) = "auto"
82+runType (CppAny []) = "any"
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
90+ | CppConst `elem` qs = "const " ++ (runType . t $ delete CppConst qs)
91+ | CppRef `elem` qs = runType (t $ delete CppRef qs) ++ "&"
92+ | otherwise = runType (t qs)
8793
8894runValueQual :: CppValueQual -> String
8995runValueQual CppStatic = "static"
src/Language/PureScript/Pretty/Cpp.hs+1−1
@@ -514,7 +514,7 @@ dotsTo :: Char -> String -> String
514514dotsTo chr' = map (\c -> if c == '.' then chr' else c)
515515
516516argstr :: (String, Maybe CppType) -> String
517−argstr (name, Nothing) = argStr name CppAuto
517+argstr (name, Nothing) = argStr name (CppAuto [])
518518argstr (name, Just typ) | name == C.__unused = argStr [] typ
519519argstr (name, Just typ) = argStr name typ
520520