on master help sign in
javascript: remove redundant eval on ops
commit956a6df4b7e39398f7707f5858672aa11351b0bd authorraichoo <raichoo@googlemail.com> dateFri 31 Jan 2014, 20:59 (13 years ago) parent54b959e
1 file changed+65−3
src/IRTS/CodegenJavaScript.hs+65−3
src/IRTS/CodegenJavaScript.hs+65−3
@@ -75,6 +75,7 @@ data JS = JSRaw String
7575 | JSIndex JS JS
7676 | JSCond [(JS, JS)]
7777 | JSTernary JS JS JS
78+ | JSParens JS
7879 deriving Eq
7980
8081
@@ -190,6 +191,9 @@ compileJS (JSTernary cond true false) =
190191 f = compileJS false in
191192 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
192193
194+compileJS (JSParens js) =
195+ "(" ++ compileJS js ++ ")"
196+
193197
194198jsTailcall :: JS -> JS
195199jsTailcall call =
@@ -326,6 +330,9 @@ jsSubst var new (JSAssign lhs rhs) =
326330jsSubst var new (JSAlloc val (Just js)) =
327331 JSAlloc val (Just (jsSubst var new js))
328332
333+jsSubst var new (JSParens js) =
334+ JSParens $ jsSubst var new js
335+
329336jsSubst _ _ js = js
330337
331338
@@ -378,6 +385,9 @@ removeAllocations (JSCond conds) =
378385removeAllocations (JSTernary c t f) =
379386 JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
380387
388+removeAllocations (JSParens js) =
389+ JSParens $ removeAllocations js
390+
381391removeAllocations js = js
382392
383393
@@ -497,6 +507,9 @@ inlineJS (JSCond cases) =
497507inlineJS (JSOp op lhs rhs) =
498508 JSOp op (inlineJS lhs) (inlineJS rhs)
499509
510+inlineJS (JSParens js) =
511+ JSParens $ inlineJS js
512+
500513inlineJS js = js
501514
502515
@@ -549,6 +562,8 @@ deadEvalApplyCases js =
549562
550563 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
551564
565+ getTags (JSParens js) = getTags js
566+
552567 getTags js = []
553568
554569
@@ -606,6 +621,8 @@ initConstructors js =
606621
607622 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
608623
624+ getTags (JSParens js) = getTags js
625+
609626 getTags js = []
610627
611628
@@ -656,6 +673,9 @@ initConstructors js =
656673 replaceHelper tags f
657674 )
658675
676+ replaceHelper tags (JSParens js) =
677+ JSParens $ replaceHelper tags js
678+
659679 replaceHelper tags js = js
660680
661681
@@ -730,6 +750,9 @@ removeIDs js =
730750 removeIDCall ids (JSArray fields) =
731751 JSArray $ map (removeIDCall ids) fields
732752
753+ removeIDCall ids (JSParens js) =
754+ JSParens $ removeIDCall ids js
755+
733756 removeIDCall _ js = js
734757
735758
@@ -816,13 +839,39 @@ inlineFunctions js =
816839 , countInvokations name f
817840 ]
818841
842+ countInvokations name (JSParens js) =
843+ countInvokations name js
844+
819845 countInvokations _ _ = 0
820846
821847
822848reduceConstant :: JS -> JS
823−reduceConstant (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
824− JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [JSNum num])
825− )]) = JSNum num
849+reduceConstant
850+ (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
851+ JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [val])
852+ )])
853+ | JSNum num <- val = val
854+ | JSOp op lhs rhs <- val =
855+ JSParens $ JSOp op (reduceConstant lhs) (reduceConstant rhs)
856+
857+ | JSApp (JSProj lhs op) [rhs] <- val
858+ , op `elem` [ "subtract"
859+ , "add"
860+ , "multiply"
861+ , "divide"
862+ , "mod"
863+ , "equals"
864+ , "lesser"
865+ , "lesserOrEquals"
866+ , "greater"
867+ , "greaterOrEquals"
868+ ] = val
869+
870+reduceConstant (JSApp ident [(JSParens js)]) =
871+ JSApp ident [reduceConstant js]
872+
873+reduceConstant (JSOp op lhs rhs) =
874+ JSOp op (reduceConstant lhs) (reduceConstant rhs)
826875
827876reduceConstant (JSReturn ret) =
828877 JSReturn (reduceConstant ret)
@@ -851,8 +900,12 @@ reduceConstant (JSSeq seq) =
851900reduceConstant (JSFunction args body) =
852901 JSFunction args (reduceConstant body)
853902
903+reduceConstant (JSParens js) =
904+ JSParens $ reduceConstant js
905+
854906reduceConstant js = js
855907
908+
856909reduceConstants :: JS -> JS
857910reduceConstants js
858911 | ret <- reduceConstant js
@@ -929,6 +982,9 @@ optimizeEvalTailcalls (fun, tc) js =
929982 optHelper f
930983 )
931984
985+ optHelper (JSParens js) =
986+ JSParens $ optHelper js
987+
932988 optHelper js = js
933989
934990
@@ -992,6 +1048,9 @@ removeInstanceChecks (JSTernary c t f) =
9921048 removeInstanceChecks f
9931049 )
9941050
1051+removeInstanceChecks (JSParens js) =
1052+ JSParens $ removeInstanceChecks js
1053+
9951054removeInstanceChecks js = js
9961055
9971056
@@ -1071,6 +1130,9 @@ reduceLoop reduced (cons, program) =
10711130 reduceCall funs (JSArray fields) =
10721131 JSArray $ map (reduceCall funs) fields
10731132
1133+ reduceCall funs (JSParens js) =
1134+ JSParens $ reduceCall funs js
1135+
10741136 reduceCall _ js = js
10751137
10761138