@@ -589,24 +589,52 @@ inlineFunctions js =
589 inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
589 inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
590 | countAll fun front + countAll fun back == 0 =
590 | countAll fun front + countAll fun back == 0 =
591 inlineHelper (front, back)
591 inlineHelper (front, back)
592+ | Just new <- inlineAble (
592− | inlineAble (countAll fun front + countAll fun back) fun args body =
593+ countAll fun front + countAll fun back
593− let f = map (inline fun args body) in
595+ let f = map (inline fun args new) in
594− inlineHelper (f front, f back)
596+ inlineHelper (f front, f back)
596 inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
598 inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
597 inlineHelper (front, []) = front
599 inlineHelper (front, []) = front
600− inlineAble :: Int -> String -> [String] -> JS -> Bool
602+ inlineAble :: Int -> String -> [String] -> JS -> Maybe JS
601 inlineAble 1 fun args body
603 inlineAble 1 fun args body
602− | nonRecur fun body = False
604+ | nonRecur fun body =
603− | otherwise = False
605+ inlineAble' body
607+ inlineAble' :: JS -> Maybe JS
609+ JSReturn js@(JSNew "__IDRRT__Con" [JSNum _, JSArray [JSIdent _]])
612+ inlineAble' _ = Nothing
605− inlineAble _ _ _ _ = False
614+ inlineAble _ _ _ _ = Nothing
608 inline :: String -> [String] -> JS -> JS -> JS
617 inline :: String -> [String] -> JS -> JS -> JS
609− inline fun args body js = js
618+ inline fun args body js = inline' js
621+ inline' (JSApp (JSIdent name) vals)
623+ let (js, phs) = insertPlaceHolders args body in
624+ inline' $ foldr (uncurry jsSubst) js (zip phs vals)
626+ inline' js = transformJS inline' js
628+ insertPlaceHolders :: [String] -> JS -> (JS, [JS])
629+ insertPlaceHolders args body = insertPlaceHolders' args body []
631+ insertPlaceHolders' :: [String] -> JS -> [JS] -> (JS, [JS])
632+ insertPlaceHolders' (a:as) body ph
633+ | (body', ph') <- insertPlaceHolders' as body ph =
634+ let phvar = JSIdent $ "__PH_" ++ show (length ph') in
635+ (jsSubst (JSIdent a) phvar body', phvar : ph')
637+ insertPlaceHolders' [] body ph = (body, ph)
612 nonRecur :: String -> JS -> Bool
640 nonRecur :: String -> JS -> Bool
@@ -809,11 +837,11 @@ codegenJavaScript target definitions filename outputType = do
809 , initConstructors
837 , initConstructors
810 , map removeAllocations
838 , map removeAllocations
811 , elimDeadLoop
839 , elimDeadLoop
813 , map elimDuplicateEvals
840 , map elimDuplicateEvals
814 , map (optimizeEvalTailcalls ("__IDR__mEVAL0", "__IDRRT__EVALTC"))
841 , map (optimizeEvalTailcalls ("__IDR__mEVAL0", "__IDRRT__EVALTC"))
815 , map (optimizeEvalTailcalls ("__IDR__mAPPLY0", "__IDRRT__APPLYTC"))
842 , map (optimizeEvalTailcalls ("__IDR__mAPPLY0", "__IDRRT__APPLYTC"))
816 , map removeInstanceChecks
843 , map removeInstanceChecks