@@ -583,46 +583,48 @@ removeIDs js =
583583
584584inlineFunctions :: [JS] -> [JS]
585585inlineFunctions js =
586− let funs = collectFunctions js
587− occurences = map ((id . fst) &&& countAll js) funs in
588− removeDeadFunctions occurences js
589− where
590− removeDeadFunctions :: [(String, (Int, JS))] -> [JS] -> [JS]
591− removeDeadFunctions _ [] = []
592− removeDeadFunctions funOccur ((JSAlloc fun (Just (JSFunction _ _))):js)
593− | Just (o, _) <- lookup fun funOccur
594− , o == 0 = removeDeadFunctions funOccur js
586+ inlineHelper ([], js)
587+ where
588+ inlineHelper :: ([JS], [JS]) -> [JS]
589+ inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
590+ | countAll fun front + countAll fun back == 0 =
591+ inlineHelper (front, back)
592+ | inlineAble (countAll fun front + countAll fun back) fun args body =
593+ let f = map (inline fun args body) in
594+ inlineHelper (f front, f back)
595595
596− removeDeadFunctions funOccur (j:js) = j : removeDeadFunctions funOccur js
596+ inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
597+ inlineHelper (front, []) = front
597598
598599
599− collectFunctions :: [JS] -> [(String, JS)]
600− collectFunctions js =
601− catMaybes $ map collectHelper js
600+ inlineAble :: Int -> String -> [String] -> JS -> Bool
601+ inlineAble 1 fun args body
602+ | nonRecur fun body = False
603+ | otherwise = False
602604
605+ inlineAble _ _ _ _ = False
603606
604− collectHelper :: JS -> Maybe (String, JS)
605− collectHelper (JSAlloc fun (Just js@(JSFunction _ body)))
606− | fun /= "main" && nonRecur fun body = Just (fun, js)
607− collectHelper _ = Nothing
608607
608+ inline :: String -> [String] -> JS -> JS -> JS
609+ inline fun args body js = js
609610
610− nonRecur :: String -> JS -> Bool
611− nonRecur name body = countInvokations name body == 0
612611
612+ nonRecur :: String -> JS -> Bool
613+ nonRecur name body = countInvokations name body == 0
613614
614− countAll :: [JS] -> (String, JS) -> (Int, JS)
615− countAll js (name, fun) = (sum $ map (countInvokations name) js, fun)
616615
616+ countAll :: String -> [JS] -> Int
617+ countAll name js = sum $ map (countInvokations name) js
617618
618− countInvokations :: String -> JS -> Int
619− countInvokations name = foldJS match (+) 0
620− where
621− match :: JS -> Int
622− match js
623− | JSApp (JSIdent ident) _ <- js
624− , name == ident = 1
625− | otherwise = 0
619+
620+ countInvokations :: String -> JS -> Int
621+ countInvokations name = foldJS match (+) 0
622+ where
623+ match :: JS -> Int
624+ match js
625+ | JSApp (JSIdent ident) _ <- js
626+ , name == ident = 1
627+ | otherwise = 0
626628
627629
628630reduceConstant :: JS -> JS