@@ -583,46 +583,48 @@ removeIDs js =
584inlineFunctions :: [JS] -> [JS]
584inlineFunctions :: [JS] -> [JS]
585inlineFunctions js =
585inlineFunctions js =
586− let funs = collectFunctions js
586+ inlineHelper ([], js)
587− occurences = map ((id . fst) &&& countAll js) funs in
588− removeDeadFunctions occurences js
590− removeDeadFunctions :: [(String, (Int, JS))] -> [JS] -> [JS]
588+ inlineHelper :: ([JS], [JS]) -> [JS]
591− removeDeadFunctions _ [] = []
592− removeDeadFunctions funOccur ((JSAlloc fun (Just (JSFunction _ _))):js)
589+ inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
593− | Just (o, _) <- lookup fun funOccur
590+ | countAll fun front + countAll fun back == 0 =
594− , o == 0 = removeDeadFunctions funOccur js
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)
596− removeDeadFunctions funOccur (j:js) = j : removeDeadFunctions funOccur js
596+ inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
597+ inlineHelper (front, []) = front
599− collectFunctions :: [JS] -> [(String, JS)]
600+ inlineAble :: Int -> String -> [String] -> JS -> Bool
600− collectFunctions js =
601+ inlineAble 1 fun args body
601− catMaybes $ map collectHelper js
602+ | nonRecur fun body = False
605+ inlineAble _ _ _ _ = False
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
608+ inline :: String -> [String] -> JS -> JS -> JS
609+ inline fun args body js = js
610− nonRecur :: String -> JS -> Bool
611− nonRecur name body = countInvokations name body == 0
612+ nonRecur :: String -> JS -> Bool
613+ nonRecur name body = countInvokations name body == 0
614− countAll :: [JS] -> (String, JS) -> (Int, JS)
615− countAll js (name, fun) = (sum $ map (countInvokations name) js, fun)
616+ countAll :: String -> [JS] -> Int
617+ countAll name js = sum $ map (countInvokations name) js
618− countInvokations :: String -> JS -> Int
620+ countInvokations :: String -> JS -> Int
619− countInvokations name = foldJS match (+) 0
621+ countInvokations name = foldJS match (+) 0
621− match :: JS -> Int
623+ match :: JS -> Int
622− match js
624+ match js
623− | JSApp (JSIdent ident) _ <- js
625+ | JSApp (JSIdent ident) _ <- js
624− , name == ident = 1
626+ , name == ident = 1
625− | otherwise = 0
627+ | otherwise = 0
628reduceConstant :: JS -> JS
630reduceConstant :: JS -> JS