on master help sign in
javascript: initial code for function inliner
commit36204f61b902fc482fa0fba37796863fbb44b6dd authorraichoo <raichoo@googlemail.com> dateThu 23 Jan 2014, 23:12 (13 years ago) parent566648c
1 file changed+89−1
src/IRTS/CodegenJavaScript.hs+89−1
src/IRTS/CodegenJavaScript.hs+89−1
@@ -687,6 +687,93 @@ removeIDs js =
687687
688688 removeIDCall _ js = js
689689
690+inlineFunctions :: [JS] -> [JS]
691+inlineFunctions js =
692+ let funs = collectFunctions js
693+ occurences = map (id &&& countAll js) funs in
694+ -- (JSRaw $ "// " ++ show (map (id &&& countAll js) funs))
695+ removeDeadFunctions occurences js
696+ where
697+ removeDeadFunctions :: [(String, Int)] -> [JS] -> [JS]
698+ removeDeadFunctions _ [] = []
699+ removeDeadFunctions funOccur ((JSAlloc fun (Just (JSFunction _ _))):js)
700+ | Just o <- lookup fun funOccur
701+ , o == 0 = removeDeadFunctions funOccur js
702+
703+ removeDeadFunctions funOccur (j:js) = j : removeDeadFunctions funOccur js
704+
705+
706+ collectFunctions :: [JS] -> [String]
707+ collectFunctions js =
708+ catMaybes $ map collectHelper js
709+
710+
711+ collectHelper :: JS -> Maybe String
712+ collectHelper (JSAlloc fun (Just (JSFunction _ body)))
713+ | fun /= "main" && nonRecur fun body = Just fun
714+ collectHelper _ = Nothing
715+
716+
717+ nonRecur :: String -> JS -> Bool
718+ nonRecur name body = countInvokations name body == 0
719+
720+
721+ countAll :: [JS] -> String -> Int
722+ countAll js name = sum $ map (countInvokations name) js
723+
724+
725+ countInvokations :: String -> JS -> Int
726+ countInvokations name (JSApp (JSIdent ident) args)
727+ | name == ident = 1 + (sum $ map (countInvokations name) args)
728+ | otherwise = sum $ map (countInvokations name) args
729+
730+ countInvokations name (JSApp lhs rhs) =
731+ countInvokations name lhs + (sum $ map (countInvokations name) rhs)
732+
733+ countInvokations name (JSFunction _ body) =
734+ countInvokations name body
735+
736+ countInvokations name (JSSeq seq) =
737+ sum $ map (countInvokations name) seq
738+
739+ countInvokations name (JSReturn ret) =
740+ countInvokations name ret
741+
742+ countInvokations name (JSNew _ args) =
743+ sum $ map (countInvokations name) args
744+
745+ countInvokations name (JSOp _ lhs rhs) =
746+ countInvokations name lhs + countInvokations name rhs
747+
748+ countInvokations name (JSProj obj _) =
749+ countInvokations name obj
750+
751+ countInvokations name (JSArray vals) =
752+ sum $ map (countInvokations name) vals
753+
754+ countInvokations name (JSAssign _ rhs) =
755+ countInvokations name rhs
756+
757+ countInvokations name (JSAlloc _ (Just js)) =
758+ countInvokations name js
759+
760+ countInvokations name (JSIndex lhs rhs) =
761+ countInvokations name lhs + countInvokations name rhs
762+
763+ countInvokations name (JSCond conds) =
764+ sum $ map (uncurry (+)) (
765+ map (countInvokations name *** countInvokations name) conds
766+ )
767+
768+ countInvokations name (JSTernary c t f) =
769+ sum $ [ countInvokations name c
770+ , countInvokations name t
771+ , countInvokations name f
772+ ]
773+
774+ countInvokations _ _ = 0
775+
776+
690777reduceConstant :: JS -> JS
691778reduceConstant (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
692779 JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [JSNum num])
@@ -853,7 +940,8 @@ codegenJavaScript target definitions filename outputType = do
853940 constRemoved = map reduceConstants reduced
854941 constrInit = initConstructors constRemoved
855942 removeAlloc = map removeAllocations constrInit
856− js = deadEvalApplyCases removeAlloc in
943+ deadElim = deadEvalApplyCases removeAlloc
944+ js = inlineFunctions deadElim in
857945 map compileJS js
858946
859947 mainLoop :: JS