on master help sign in
javascript: simplified inliner
b44ae8braichoo
src/IRTS/CodegenJavaScript.hs+36−75
@@ -444,78 +444,48 @@ isJSConstantConstructor constants js
444
444
445
445
446inlineJS :: JS -> JS
446inlineJS :: JS -> JS
447−inlineJS (JSReturn (JSApp (JSFunction [] err@(JSError _)) [])) = err
447+inlineJS = inlineError . inlineApply . inlineCaseMatch . inlineJSLet
448−inlineJS (JSReturn (JSApp (JSFunction ["cse"] body) [val@(JSVar _)])) =
448+ where
449+ inlineJSLet :: JS -> JS
450+ inlineJSLet (JSApp (JSFunction [arg] (JSReturn ret)) [val])
451+ | opt <- inlineJSLet val =
449− inlineJS $ jsSubst (JSIdent "cse") val body
452+ inlineJS $ jsSubst (JSIdent arg) opt ret
450
453
454+ inlineJSLet js = transformJS inlineJSLet js
451
455
452−inlineJS (JSReturn (JSApp (JSFunction [arg] cond@(JSCond _)) [val])) =
453− inlineJS $ JSSeq [ JSAlloc arg (Just val)
454− , cond
455− ]
456
456
457−inlineJS (JSApp (JSProj (JSFunction args (JSReturn body)) "apply") [
457+ inlineCaseMatch (JSReturn (JSApp (JSFunction ["cse"] body) [val]))
458− JSThis,JSProj var "vars"
458+ | opt <- inlineCaseMatch val =
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
459+ inlineCaseMatch $ jsSubst (JSIdent "cse") opt body
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− )
501
460
502− | JSIndex (JSProj obj field) idx <- ret
461+ inlineCaseMatch js = transformJS inlineCaseMatch js
503− , opt <- inlineJS val =
504− inlineJS $ JSIndex (JSProj (
505− inlineJS $ jsSubst (JSIdent arg) opt obj
506− ) field
507− ) (inlineJS $ jsSubst (JSIdent arg) opt idx)
508
462
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)
513
463
514− | JSApp (JSIdent fun) args <- ret
464+ inlineApply js
515− , opt <- inlineJS val =
465+ | JSApp (
516− inlineJS $ JSApp (JSIdent fun) $ map (inlineJS . jsSubst (JSIdent arg) opt) args
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+
517
484
485+ inlineError (JSReturn (JSApp (JSFunction [] error@(JSError _)) [])) =
486+ inlineError error
487+
518−inlineJS js = transformJS inlineJS js
488+ inlineError js = transformJS inlineError js
519
489
520
490
521reduceJS :: [JS] -> [JS]
491reduceJS :: [JS] -> [JS]
@@ -1121,15 +1091,6 @@ reduceLoop reduced (cons, program) =
1121 reduceCall funs js = transformJS (reduceCall funs) js
1091 reduceCall funs js = transformJS (reduceCall funs) js
1122
1092
1123
1093
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−
1133extractLocalConstructors :: [JS] -> [JS]
1094extractLocalConstructors :: [JS] -> [JS]
1134extractLocalConstructors js =
1095extractLocalConstructors js =
1135 concatMap extractLocalConstructors' js
1096 concatMap extractLocalConstructors' js
@@ -1343,7 +1304,7 @@ codegenJavaScript target definitions filename outputType = do
1343 map compileJS
1304 map compileJS
1344
1305
1345 opt =
1306 opt =
1346− [ map optimizeJS
1307+ [ map inlineJS
1347 , removeIDs
1308 , removeIDs
1348 , reduceJS
1309 , reduceJS
1349 , map reduceConstants
1310 , map reduceConstants