@@ -444,78 +444,48 @@ isJSConstantConstructor constants js
444444
445445
446446inlineJS :: JS -> JS
447−inlineJS (JSReturn (JSApp (JSFunction [] err@(JSError _)) [])) = err
448−inlineJS (JSReturn (JSApp (JSFunction ["cse"] body) [val@(JSVar _)])) =
449− inlineJS $ jsSubst (JSIdent "cse") val body
447+inlineJS = inlineError . inlineApply . inlineCaseMatch . inlineJSLet
448+ where
449+ inlineJSLet :: JS -> JS
450+ inlineJSLet (JSApp (JSFunction [arg] (JSReturn ret)) [val])
451+ | opt <- inlineJSLet val =
452+ inlineJS $ jsSubst (JSIdent arg) opt ret
450453
454+ inlineJSLet js = transformJS inlineJSLet js
451455
452−inlineJS (JSReturn (JSApp (JSFunction [arg] cond@(JSCond _)) [val])) =
453− inlineJS $ JSSeq [ JSAlloc arg (Just val)
454− , cond
455− ]
456456
457−inlineJS (JSApp (JSProj (JSFunction args (JSReturn body)) "apply") [
458− JSThis,JSProj var "vars"
459− ])
460− | var /= JSIdent "cse" =
461− inlineJS $ inlineApply args body 0
462− where
463− inlineApply [] body _ = inlineJS body
464− inlineApply (a:as) body n =
465− inlineApply as (
466− jsSubst (JSIdent a) (JSIndex (JSProj var "vars") (JSNum (JSInt n))) body
467− ) (n + 1)
468−
469−inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
470− | isJSConstant val = val
471−
472−inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
473− JSFunction [] (JSReturn val)
474− ])
475− | isJSConstant val = val
476−
477−inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
478− | JSNew con [tag, vals] <- ret
479− , opt <- inlineJS val =
480− inlineJS $ JSNew con [tag, inlineJS $ jsSubst (JSIdent arg) opt vals]
481−
482− | JSNew con [JSFunction [] (JSReturn (JSApp fun vars))] <- ret
483− , opt <- inlineJS val =
484− inlineJS $ JSNew con [JSFunction [] (
485− JSReturn (
486− JSApp (
487− inlineJS $ jsSubst (JSIdent arg) opt fun
488− ) (
489− map (inlineJS . jsSubst (JSIdent arg) opt) vars
490− )
491− )
492− )]
493−
494− | JSApp (JSProj obj field) args <- ret
495− , opt <- inlineJS val =
496− inlineJS $ JSApp (
497− inlineJS $ JSProj (jsSubst (JSIdent arg) opt obj) field
498− ) (
499− map (inlineJS . jsSubst (JSIdent arg) opt) args
500− )
457+ inlineCaseMatch (JSReturn (JSApp (JSFunction ["cse"] body) [val]))
458+ | opt <- inlineCaseMatch val =
459+ inlineCaseMatch $ jsSubst (JSIdent "cse") opt body
501460
502− | JSIndex (JSProj obj field) idx <- ret
503− , opt <- inlineJS val =
504− inlineJS $ JSIndex (JSProj (
505− inlineJS $ jsSubst (JSIdent arg) opt obj
506− ) field
507− ) (inlineJS $ jsSubst (JSIdent arg) opt idx)
461+ inlineCaseMatch js = transformJS inlineCaseMatch js
508462
509− | JSBinOp op lhs rhs <- ret
510− , opt <- inlineJS val =
511− inlineJS $ JSBinOp op (inlineJS $ jsSubst (JSIdent arg) opt lhs) $
512− (inlineJS $ jsSubst (JSIdent arg) opt rhs)
513463
514− | JSApp (JSIdent fun) args <- ret
515− , opt <- inlineJS val =
516− inlineJS $ JSApp (JSIdent fun) $ map (inlineJS . jsSubst (JSIdent arg) opt) args
464+ inlineApply js
465+ | JSApp (
466+ JSProj (JSFunction args (JSReturn body)) "apply"
467+ ) [JSThis, JSProj var "vars"] <- js =
468+ inlineApply $ inlineApply' var args body 0
469+ | JSReturn (JSApp (
470+ JSProj (JSFunction args body@(JSCond _)) "apply"
471+ ) [JSThis, JSProj var "vars"]) <- js =
472+ inlineApply $ inlineApply' var args body 0
473+ where
474+ inlineApply' _ [] body _ = body
475+ inlineApply' var (a:as) body n =
476+ inlineApply' var as (
477+ jsSubst (JSIdent a) (
478+ JSIndex (JSProj var "vars") (JSNum (JSInt n))
479+ ) body
480+ ) (n + 1)
481+
482+ inlineApply js = transformJS inlineApply js
483+
517484
518−inlineJS js = transformJS inlineJS js
485+ inlineError (JSReturn (JSApp (JSFunction [] error@(JSError _)) [])) =
486+ inlineError error
487+
488+ inlineError js = transformJS inlineError js
519489
520490
521491reduceJS :: [JS] -> [JS]
@@ -1121,15 +1091,6 @@ reduceLoop reduced (cons, program) =
11211091 reduceCall funs js = transformJS (reduceCall funs) js
11221092
11231093
1124−optimizeJS :: JS -> JS
1125−optimizeJS = inlineLoop
1126− where inlineLoop :: JS -> JS
1127− inlineLoop js
1128− | opt <- inlineJS js
1129− , opt /= js = inlineLoop opt
1130− | otherwise = js
1131−
1132−
11331094extractLocalConstructors :: [JS] -> [JS]
11341095extractLocalConstructors js =
11351096 concatMap extractLocalConstructors' js
@@ -1343,7 +1304,7 @@ codegenJavaScript target definitions filename outputType = do
13431304 map compileJS
13441305
13451306 opt =
1346− [ map optimizeJS
1307+ [ map inlineJS
13471308 , removeIDs
13481309 , reduceJS
13491310 , map reduceConstants