@@ -755,6 +755,29 @@ removeIDs js =
755 removeIDCall ids js = transformJS (removeIDCall ids) js
755 removeIDCall ids js = transformJS (removeIDCall ids) js
758+inlineFunction :: String -> [String] -> JS -> JS -> JS
759+inlineFunction fun args body js = inline' js
762+ inline' (JSApp (JSIdent name) vals)
764+ let (js, phs) = insertPlaceHolders args body in
765+ inline' $ foldr (uncurry jsSubst) js (zip phs vals)
767+ inline' js = transformJS inline' js
769+ insertPlaceHolders :: [String] -> JS -> (JS, [JS])
770+ insertPlaceHolders args body = insertPlaceHolders' args body []
772+ insertPlaceHolders' :: [String] -> JS -> [JS] -> (JS, [JS])
773+ insertPlaceHolders' (a:as) body ph
774+ | (body', ph') <- insertPlaceHolders' as body ph =
775+ let phvar = JSIdent $ "__PH_" ++ show (length ph') in
776+ (jsSubst (JSIdent a) phvar body', phvar : ph')
778+ insertPlaceHolders' [] body ph = (body, ph)
758inlineFunctions :: [JS] -> [JS]
781inlineFunctions :: [JS] -> [JS]
759inlineFunctions js =
782inlineFunctions js =
760 inlineHelper ([], js)
783 inlineHelper ([], js)
@@ -766,7 +789,7 @@ inlineFunctions js =
766 | Just new <- inlineAble (
789 | Just new <- inlineAble (
767 countAll fun front + countAll fun back
790 countAll fun front + countAll fun back
768 ) fun args body =
791 ) fun args body =
769− let f = map (inline fun args new) in
792+ let f = map (inlineFunction fun args new) in
770 inlineHelper (f front, f back)
793 inlineHelper (f front, f back)
772 inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
795 inlineHelper (front, next:back) = inlineHelper (front ++ [next], back)
@@ -783,29 +806,6 @@ inlineFunctions js =
783 inlineAble _ _ _ _ = Nothing
806 inlineAble _ _ _ _ = Nothing
786− inline :: String -> [String] -> JS -> JS -> JS
787− inline fun args body js = inline' js
790− inline' (JSApp (JSIdent name) vals)
792− let (js, phs) = insertPlaceHolders args body in
793− inline' $ foldr (uncurry jsSubst) js (zip phs vals)
795− inline' js = transformJS inline' js
797− insertPlaceHolders :: [String] -> JS -> (JS, [JS])
798− insertPlaceHolders args body = insertPlaceHolders' args body []
800− insertPlaceHolders' :: [String] -> JS -> [JS] -> (JS, [JS])
801− insertPlaceHolders' (a:as) body ph
802− | (body', ph') <- insertPlaceHolders' as body ph =
803− let phvar = JSIdent $ "__PH_" ++ show (length ph') in
804− (jsSubst (JSIdent a) phvar body', phvar : ph')
806− insertPlaceHolders' [] body ph = (body, ph)
809 nonRecur :: String -> JS -> Bool
809 nonRecur :: String -> JS -> Bool
810 nonRecur name body = countInvokations name body == 0
810 nonRecur name body = countInvokations name body == 0
@@ -1416,6 +1416,54 @@ evalCons js =
1416 match js = transformJS match js
1416 match js = transformJS match js
1419+elimConds :: [JS] -> [JS]
1421+ let (conds, rest) = partition isCond js in
1422+ foldl' eraseCond rest conds
1424+ isCond :: JS -> Bool
1426+ fun (Just (JSFunction args (JSCond
1427+ [ (JSBinOp "==" (JSNum (JSInt tag)) _, JSReturn (JSIdent _))
1428+ , (JSNoop, JSReturn (JSIdent _))
1433+ fun (Just (JSFunction args (JSCond
1434+ [ (JSBinOp "==" _ (JSNum (JSInt tag)), JSReturn (JSIdent _))
1435+ , (JSNoop, JSReturn (JSIdent _))
1442+ eraseCond :: [JS] -> JS -> [JS]
1443+ eraseCond js (JSAlloc
1444+ fun (Just (JSFunction args (JSCond
1449+ )) = map (inlineFunction fun args (JSTernary c t f)) js
1452+removeUselessCons :: [JS] -> [JS]
1453+removeUselessCons js =
1454+ let (cons, rest) = partition isUseless js in
1455+ foldl' eraseCon rest cons
1457+ isUseless :: JS -> Bool
1458+ isUseless (JSAlloc fun (Just JSNull)) = True
1459+ isUseless (JSAlloc fun (Just (JSIdent _))) = True
1460+ isUseless _ = False
1463+ eraseCon :: [JS] -> JS -> [JS]
1464+ eraseCon js (JSAlloc fun (Just val)) = map (jsSubst (JSIdent fun) val) js
1419getGlobalCons :: JS -> [(String, JS)]
1467getGlobalCons :: JS -> [(String, JS)]
1420getGlobalCons js = foldJS match (++) [] js
1468getGlobalCons js = foldJS match (++) [] js
@@ -1517,6 +1565,8 @@ codegenJS_all target definitions includes filename outputType = do
1517 , extractLocalConstructors
1565 , extractLocalConstructors
1518 , unfoldLookupTable
1566 , unfoldLookupTable
1519 , evalCons
1567 , evalCons
1569+ , removeUselessCons
1522 functions :: [String]
1572 functions :: [String]