git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
C++: optimize mutually recursive let funcs
commiteddf4465b6940aa54dbd0cc022f4273c49d355fa authorAndy Arvanitis <andyarvanitis@gmail.com> dateSun 1 May 2016, 22:20 (11 years ago) parent6a7b3d7
1 file changed+26−12
src/Language/PureScript/CodeGen/Cpp.hs+26−12
src/Language/PureScript/CodeGen/Cpp.hs+26−12
@@ -29,7 +29,7 @@ module Language.PureScript.CodeGen.Cpp
2929import Data.Char (isLetter)
3030import Data.List
3131import Data.Function (on)
32−import Data.Maybe (catMaybes)
32+import Data.Maybe (catMaybes, fromJust)
3333import qualified Data.Map as M
3434
3535import Control.Monad (forM, replicateM)
@@ -314,14 +314,20 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
314314 go cpp = cpp
315315
316316 -------------------------------------------------------------------------------------------------
317− convertRecursiveFns :: [Cpp] -> [Cpp]
317+ convertRecursiveFns :: [Cpp] -> Cpp -> [Cpp]
318318 -------------------------------------------------------------------------------------------------
319− convertRecursiveFns cpps
320− | not $ null fns = dict : (accessor topdict <$> fns) ++ (everywhereOnCpp removeRec <$> cpps)
319+ convertRecursiveFns cpps otherCpp
320+ | not $ null fns = dict : (accessor topdict <$> filteredFns allCpps) ++ cpps'
321321 where
322+ cpps' = everywhereOnCpp removeRec <$> cpps
323+ allCpps = CppBlock $ otherCpp : cpps'
324+
322325 fns :: [(String, Cpp)]
323326 fns = toelem <$> concatMap (everythingOnCpp (++) recursive) cpps
324327
328+ filteredFns :: Cpp -> [(String, Cpp)]
329+ filteredFns cpp = filter (\(f,_) -> everythingOnCpp (||) (== CppVar f) cpp) fns
330+
325331 recursive :: Cpp -> [Cpp]
326332 recursive cpp'@(CppFunction _ _ _ qs _) | CppRecursive `elem` qs = [cpp']
327333 recursive _ = []
@@ -334,7 +340,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
334340 dict = CppVariableIntroduction
335341 (topdict, Just $ CppAny [CppConst])
336342 []
337− (Just $ CppObjectLiteral fns)
343+ (Just $ CppDataLiteral ((\(f,cpp) -> CppComment [LineComment f] cpp) <$> fns))
338344
339345 accessor :: String -> (String, Cpp) -> Cpp
340346 accessor dictname (name, _) =
@@ -344,15 +350,23 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
344350 (Just accessed)
345351 where
346352 dict' = CppVar dictname
347− accessed = CppApp (CppIndexer (CppStringLiteral name) dict') [dict']
353+ dict'' = CppCast dataType dict'
354+ accessed = CppApp
355+ (CppIndexer
356+ (CppNumericLiteral .
357+ Left .
358+ fromIntegral .
359+ fromJust $ findIndex ((== name) . fst) fns)
360+ dict'')
361+ [dict']
348362
349363 toelem :: Cpp -> (String, Cpp)
350− toelem (CppFunction name args rtyp _ (CppBlock body)) =
364+ toelem (CppFunction name args rtyp _ body'@(CppBlock body)) =
351365 (name, withdict . maybeRemCaps $ CppLambda
352366 [CppCaptureAll]
353367 args
354368 rtyp
355− (CppBlock $ (accessor localdict <$> fns) ++ body))
369+ (CppBlock $ (accessor localdict <$> filteredFns body') ++ body))
356370 toelem _ = error "not a function"
357371
358372 withdict :: Cpp -> Cpp
@@ -362,11 +376,11 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
362376 (Just $ CppAny [])
363377 (asReturnBlock cpp')
364378 topdict :: String
365− topdict = "__dict__"
379+ topdict = "_$dict$_"
366380 localdict :: String
367− localdict = "_dict_"
381+ localdict = "$dict$"
368382
369− convertRecursiveFns cpps = cpps
383+ convertRecursiveFns cpps _ = cpps
370384
371385 -- |
372386 -- Generate code in the simplified C++1x intermediate representation for a value or expression.
@@ -486,7 +500,7 @@ moduleToCpp env (Module _ mn imps _ foreigns decls) = do
486500 opts <- ask
487501 let ds'' = tco opts <$> ds'
488502 let rs = if hasRecursion ds''
489− then convertRecursiveFns ds''
503+ then convertRecursiveFns ds'' ret
490504 else ds''
491505 let cpps = convertNestedLambdas <$> rs
492506 return $ CppApp