on master help sign in
javascript: make use of inliner
commit3177b987471dbd6c76c0c3ef8b95417868af1f36 authorraichoo <raichoo@googlemail.com> dateMon 3 Feb 2014, 00:02 (13 years ago) parentcf0ef4f
1 file changed+37−9
src/IRTS/CodegenJavaScript.hs+37−9
src/IRTS/CodegenJavaScript.hs+37−9
@@ -589,24 +589,52 @@ inlineFunctions js =
589589 inlineHelper (front , (JSAlloc fun (Just (JSFunction args body))):back)
590590 | countAll fun front + countAll fun back == 0 =
591591 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)
592+ | Just new <- inlineAble (
593+ countAll fun front + countAll fun back
594+ ) fun args body =
595+ let f = map (inline fun args new) in
596+ inlineHelper (f front, f back)
595597
596598 inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
597599 inlineHelper (front, []) = front
598600
599601
600− inlineAble :: Int -> String -> [String] -> JS -> Bool
602+ inlineAble :: Int -> String -> [String] -> JS -> Maybe JS
601603 inlineAble 1 fun args body
602− | nonRecur fun body = False
603− | otherwise = False
604+ | nonRecur fun body =
605+ inlineAble' body
606+ where
607+ inlineAble' :: JS -> Maybe JS
608+ inlineAble' (
609+ JSReturn js@(JSNew "__IDRRT__Con" [JSNum _, JSArray [JSIdent _]])
610+ ) = Just js
604611
605− inlineAble _ _ _ _ = False
612+ inlineAble' _ = Nothing
613+
614+ inlineAble _ _ _ _ = Nothing
606615
607616
608617 inline :: String -> [String] -> JS -> JS -> JS
609− inline fun args body js = js
618+ inline fun args body js = inline' js
619+ where
620+ inline' :: JS -> JS
621+ inline' (JSApp (JSIdent name) vals)
622+ | name == fun =
623+ let (js, phs) = insertPlaceHolders args body in
624+ inline' $ foldr (uncurry jsSubst) js (zip phs vals)
625+
626+ inline' js = transformJS inline' js
627+
628+ insertPlaceHolders :: [String] -> JS -> (JS, [JS])
629+ insertPlaceHolders args body = insertPlaceHolders' args body []
630+ where
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')
636+
637+ insertPlaceHolders' [] body ph = (body, ph)
610638
611639
612640 nonRecur :: String -> JS -> Bool
@@ -809,11 +837,11 @@ codegenJavaScript target definitions filename outputType = do
809837 , initConstructors
810838 , map removeAllocations
811839 , elimDeadLoop
812− , inlineFunctions
813840 , map elimDuplicateEvals
814841 , map (optimizeEvalTailcalls ("__IDR__mEVAL0", "__IDRRT__EVALTC"))
815842 , map (optimizeEvalTailcalls ("__IDR__mAPPLY0", "__IDRRT__APPLYTC"))
816843 , map removeInstanceChecks
844+ , inlineFunctions
817845 ]
818846
819847