git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: convert var intros to 'auto' if possible
commit34bc1c8f3c47c02766c565a01f544d82129a2c7b authorAndy Arvanitis <andyarvanitis@gmail.com> dateMon 22 Aug 2016, 23:34 (10 years ago) parent07a6081
6 files changed+38−8
src/Language/PureScript/CodeGen/Cpp/File.hs+12−4 src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+1 src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs+20 src/Language/PureScript/CodeGen/Cpp/Optimizer/TCO.hs+2−2 src/Language/PureScript/CodeGen/Cpp/Types.hs+2−1 src/Language/PureScript/Pretty/Cpp.hs+1−1
src/Language/PureScript/CodeGen/Cpp/File.hs+12−4
@@ -50,16 +50,18 @@ toHeader = catMaybes . map go
5050 Nothing
5151 go (CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
5252 Nothing
53+ go (CppVariableIntroduction _ _ (Just (CppUnary _ (CppNumericLiteral {})))) =
54+ Nothing
5355 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty _))) =
5456 Just $ CppFunction name [] rty [] CppNoOp
5557
5658 -- Generate thunks for top-level values
57− go (CppVariableIntroduction (name, vty) _ (Just cpp)) =
59+ go (CppVariableIntroduction (name, _) _ (Just cpp)) =
5860 case cpp of
5961 CppObjectLiteral CppInstance vals ->
6062 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
6163 _ ->
62− Just $ CppVariableIntroduction (name, vty) [CppExtern] Nothing
64+ Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
6365 go cpp@(CppStruct {}) =
6466 Just cpp
6567 go (CppComment comms cpp')
@@ -87,6 +89,8 @@ toHeaderFns = catMaybes . map go
8789 Just cpp
8890 go cpp@(CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
8991 Just cpp
92+ go cpp@(CppVariableIntroduction _ _ (Just (CppUnary _ (CppNumericLiteral {})))) =
93+ Just cpp
9094 go (CppComment comms cpp') | Just cpp <- go cpp' = Just (CppComment comms cpp)
9195 go _ = Nothing
9296
@@ -118,14 +122,18 @@ toBody = catMaybes . map go
118122 Nothing
119123 go (CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
120124 Nothing
125+ go (CppVariableIntroduction _ _ (Just (CppUnary _ (CppNumericLiteral {})))) =
126+ Nothing
121127 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty body))) =
122128 Just $ CppFunction name [] rty [] body
123− go (CppVariableIntroduction (name, vty) _ (Just cpp)) =
129+
130+ -- Generate thunks for top-level values
131+ go (CppVariableIntroduction (name, _) _ (Just cpp)) =
124132 case cpp of
125133 CppObjectLiteral CppInstance vals ->
126134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
127135 _ ->
128− Just $ CppVariableIntroduction (name, vty) [] (Just lambda)
136+ Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
129137 where
130138 val = CppVariableIntroduction ("$value$", Just $ CppAny [CppConst]) [CppStatic] (Just $ addCaptures cpp)
131139 block = CppBlock [val, CppReturn (CppVar "$value$")]
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+1
@@ -72,6 +72,7 @@ optimize' cpp = do
7272 [ collapseNestedBlocks
7373 , collapseNestedIfs
7474 , collapseIfElses
75+ , toAutoVars
7576 , tco opts
7677 , magicDo opts
7778 , removeCodeAfterReturnStatements
src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs+20
@@ -21,6 +21,7 @@ module Language.PureScript.CodeGen.Cpp.Optimizer.Inliner
2121 , etaConvert
2222 , unThunk
2323 , evaluateIifes
24+ , toAutoVars
2425 ) where
2526
2627import Prelude.Compat
@@ -290,6 +291,25 @@ inlineFnComposition = everywhereOnCppTopDownM convert
290291 fnComposeFlipped :: (String, String)
291292 fnComposeFlipped = (C.controlSemigroupoid, C.composeFlipped)
292293
294+toAutoVars :: Cpp -> Cpp
295+toAutoVars = everywhereOnCpp convert
296+ where
297+ convert :: Cpp -> Cpp
298+ convert cpp@(CppVariableIntroduction (ident, (Just (CppAny tqs))) qs (Just value))
299+ | CppStatic `notElem` qs,
300+ CppRef `notElem` tqs =
301+ case value of
302+ CppNumericLiteral {} -> autovar
303+ CppBooleanLiteral {} -> autovar
304+ CppBinary {} -> autovar
305+ CppUnary {} -> autovar
306+ _ -> cpp
307+ where
308+ typ | CppConst `elem` tqs = CppConstAuto
309+ | otherwise = CppAuto
310+ autovar = CppVariableIntroduction (ident, (Just typ)) qs (Just value)
311+ convert cpp = cpp
312+
293313semiringNumber :: (String, String)
294314semiringNumber = (C.dataSemiring, C.semiringNumber)
295315
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 $ CppAny []) [] (Just (CppVar (copyVar arg)))) allArgs ++
114+ map (\arg -> CppVariableIntroduction (arg, Just CppAuto) [] (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 $ CppAny []) [] (Just val)) allArgumentValues allArgs
128+ CppVariableIntroduction (tcoVar arg, Just CppAuto) [] (Just val)) allArgumentValues allArgs
129129 ++ map (\arg ->
130130 CppAssignment (CppVar arg) (CppVar (tcoVar arg))) allArgs
131131 ++ [ CppContinue ]
src/Language/PureScript/CodeGen/Cpp/Types.hs+2−1
@@ -28,7 +28,7 @@ import qualified Language.PureScript.Constants as C
2828
2929-- import Debug.Trace
3030
31−data CppType = CppPrimitive String | CppAuto | CppAny [CppTypeQual]
31+data CppType = CppPrimitive String | CppAuto | CppConstAuto | CppAny [CppTypeQual]
3232 deriving (Show, Read, Eq)
3333
3434data CppTypeQual = CppConst | CppRef
@@ -79,6 +79,7 @@ data CppObjectType = CppInstance | CppRecord
7979runType :: CppType -> String
8080runType (CppPrimitive t) = t
8181runType CppAuto = "auto"
82+runType CppConstAuto = "const auto"
8283runType (CppAny qs)
8384 | CppConst `elem` qs = "const " ++ (runType . CppAny $ delete CppConst qs)
8485 | CppRef `elem` qs = runType (CppAny $ delete CppRef qs) ++ "&"
src/Language/PureScript/Pretty/Cpp.hs+1−1
@@ -246,7 +246,7 @@ literals = mkPattern' match
246246 match (CppVariableIntroduction (ident, typ) qs value) =
247247 fmap concat $ sequence
248248 [ return . concatMap (++ " ") . filter (not . null) $ runValueQual <$> qs
249− , return (maybe "any" runType typ ++ " ")
249+ , return $ runType (fromMaybe (CppAny [CppConst]) typ) ++ " "
250250 , return ident
251251 , maybe (return "") (fmap (" = " ++) . prettyPrintCpp') value
252252 ]