git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: convert var intros to 'auto' if possible
34bc1c8Andy Arvanitis
src/Language/PureScript/CodeGen/Cpp/File.hs+12−4
@@ -50,16 +50,18 @@ toHeader = catMaybes . map go
50 Nothing
50 Nothing
51 go (CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
51 go (CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
52 Nothing
52 Nothing
53+ go (CppVariableIntroduction _ _ (Just (CppUnary _ (CppNumericLiteral {})))) =
54+ Nothing
53 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty _))) =
55 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty _))) =
54 Just $ CppFunction name [] rty [] CppNoOp
56 Just $ CppFunction name [] rty [] CppNoOp
55
57
56 -- Generate thunks for top-level values
58 -- Generate thunks for top-level values
57− go (CppVariableIntroduction (name, vty) _ (Just cpp)) =
59+ go (CppVariableIntroduction (name, _) _ (Just cpp)) =
58 case cpp of
60 case cpp of
59 CppObjectLiteral CppInstance vals ->
61 CppObjectLiteral CppInstance vals ->
60 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
62 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] CppNoOp
61 _ ->
63 _ ->
62− Just $ CppVariableIntroduction (name, vty) [CppExtern] Nothing
64+ Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [CppExtern] Nothing
63 go cpp@(CppStruct {}) =
65 go cpp@(CppStruct {}) =
64 Just cpp
66 Just cpp
65 go (CppComment comms cpp')
67 go (CppComment comms cpp')
@@ -87,6 +89,8 @@ toHeaderFns = catMaybes . map go
87 Just cpp
89 Just cpp
88 go cpp@(CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
90 go cpp@(CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
89 Just cpp
91 Just cpp
92+ go cpp@(CppVariableIntroduction _ _ (Just (CppUnary _ (CppNumericLiteral {})))) =
93+ Just cpp
90 go (CppComment comms cpp') | Just cpp <- go cpp' = Just (CppComment comms cpp)
94 go (CppComment comms cpp') | Just cpp <- go cpp' = Just (CppComment comms cpp)
91 go _ = Nothing
95 go _ = Nothing
92
96
@@ -118,14 +122,18 @@ toBody = catMaybes . map go
118 Nothing
122 Nothing
119 go (CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
123 go (CppVariableIntroduction _ _ (Just CppBooleanLiteral {})) =
120 Nothing
124 Nothing
125+ go (CppVariableIntroduction _ _ (Just (CppUnary _ (CppNumericLiteral {})))) =
126+ Nothing
121 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty body))) =
127 go (CppVariableIntroduction (name, _) _ (Just (CppLambda _ [] rty body))) =
122 Just $ CppFunction name [] rty [] body
128 Just $ CppFunction name [] rty [] body
129+
130+ -- Generate thunks for top-level values
123− go (CppVariableIntroduction (name, vty) _ (Just cpp)) =
131+ go (CppVariableIntroduction (name, _) _ (Just cpp)) =
124 case cpp of
132 case cpp of
125 CppObjectLiteral CppInstance vals ->
133 CppObjectLiteral CppInstance vals ->
126 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
134 Just $ CppFunction name [("", Just thunkMarkerType)] (Just $ CppAny [CppConst, CppRef]) [] block
127 _ ->
135 _ ->
128− Just $ CppVariableIntroduction (name, vty) [] (Just lambda)
136+ Just $ CppVariableIntroduction (name, Just $ CppAny [CppConst]) [] (Just lambda)
129 where
137 where
130 val = CppVariableIntroduction ("$value$", Just $ CppAny [CppConst]) [CppStatic] (Just $ addCaptures cpp)
138 val = CppVariableIntroduction ("$value$", Just $ CppAny [CppConst]) [CppStatic] (Just $ addCaptures cpp)
131 block = CppBlock [val, CppReturn (CppVar "$value$")]
139 block = CppBlock [val, CppReturn (CppVar "$value$")]
src/Language/PureScript/CodeGen/Cpp/Optimizer.hs+1
@@ -72,6 +72,7 @@ optimize' cpp = do
72 [ collapseNestedBlocks
72 [ collapseNestedBlocks
73 , collapseNestedIfs
73 , collapseNestedIfs
74 , collapseIfElses
74 , collapseIfElses
75+ , toAutoVars
75 , tco opts
76 , tco opts
76 , magicDo opts
77 , magicDo opts
77 , removeCodeAfterReturnStatements
78 , removeCodeAfterReturnStatements
src/Language/PureScript/CodeGen/Cpp/Optimizer/Inliner.hs+20
@@ -21,6 +21,7 @@ module Language.PureScript.CodeGen.Cpp.Optimizer.Inliner
21 , etaConvert
21 , etaConvert
22 , unThunk
22 , unThunk
23 , evaluateIifes
23 , evaluateIifes
24+ , toAutoVars
24 ) where
25 ) where
25
26
26import Prelude.Compat
27import Prelude.Compat
@@ -290,6 +291,25 @@ inlineFnComposition = everywhereOnCppTopDownM convert
290 fnComposeFlipped :: (String, String)
291 fnComposeFlipped :: (String, String)
291 fnComposeFlipped = (C.controlSemigroupoid, C.composeFlipped)
292 fnComposeFlipped = (C.controlSemigroupoid, C.composeFlipped)
292
293
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+
293semiringNumber :: (String, String)
313semiringNumber :: (String, String)
294semiringNumber = (C.dataSemiring, C.semiringNumber)
314semiringNumber = (C.dataSemiring, C.semiringNumber)
295
315
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 $ CppAny []) [] (Just (CppVar (copyVar arg)))) allArgs ++
114+ map (\arg -> CppVariableIntroduction (arg, Just CppAuto) [] (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 $ CppAny []) [] (Just val)) allArgumentValues allArgs
128+ CppVariableIntroduction (tcoVar arg, Just CppAuto) [] (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+2−1
@@ -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 | CppAny [CppTypeQual]
31+data CppType = CppPrimitive String | CppAuto | CppConstAuto | CppAny [CppTypeQual]
32 deriving (Show, Read, Eq)
32 deriving (Show, Read, Eq)
33
33
34data CppTypeQual = CppConst | CppRef
34data CppTypeQual = CppConst | CppRef
@@ -79,6 +79,7 @@ data CppObjectType = CppInstance | CppRecord
79runType :: CppType -> String
79runType :: CppType -> String
80runType (CppPrimitive t) = t
80runType (CppPrimitive t) = t
81runType CppAuto = "auto"
81runType CppAuto = "auto"
82+runType CppConstAuto = "const auto"
82runType (CppAny qs)
83runType (CppAny qs)
83 | CppConst `elem` qs = "const " ++ (runType . CppAny $ delete CppConst qs)
84 | CppConst `elem` qs = "const " ++ (runType . CppAny $ delete CppConst qs)
84 | CppRef `elem` qs = runType (CppAny $ delete CppRef qs) ++ "&"
85 | CppRef `elem` qs = runType (CppAny $ delete CppRef qs) ++ "&"
src/Language/PureScript/Pretty/Cpp.hs+1−1
@@ -246,7 +246,7 @@ literals = mkPattern' match
246 match (CppVariableIntroduction (ident, typ) qs value) =
246 match (CppVariableIntroduction (ident, typ) qs value) =
247 fmap concat $ sequence
247 fmap concat $ sequence
248 [ return . concatMap (++ " ") . filter (not . null) $ runValueQual <$> qs
248 [ return . concatMap (++ " ") . filter (not . null) $ runValueQual <$> qs
249− , return (maybe "any" runType typ ++ " ")
249+ , return $ runType (fromMaybe (CppAny [CppConst]) typ) ++ " "
250 , return ident
250 , return ident
251 , maybe (return "") (fmap (" = " ++) . prettyPrintCpp') value
251 , maybe (return "") (fmap (" = " ++) . prettyPrintCpp') value
252 ]
252 ]