on master help sign in
javascript: remove redundant eval on ops
956a6dfraichoo
src/IRTS/CodegenJavaScript.hs+65−3
@@ -75,6 +75,7 @@ data JS = JSRaw String
75 | JSIndex JS JS
75 | JSIndex JS JS
76 | JSCond [(JS, JS)]
76 | JSCond [(JS, JS)]
77 | JSTernary JS JS JS
77 | JSTernary JS JS JS
78+ | JSParens JS
78 deriving Eq
79 deriving Eq
79
80
80
81
@@ -190,6 +191,9 @@ compileJS (JSTernary cond true false) =
190 f = compileJS false in
191 f = compileJS false in
191 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
192 "(" ++ c ++ ")?(" ++ t ++ "):(" ++ f ++ ")"
192
193
194+compileJS (JSParens js) =
195+ "(" ++ compileJS js ++ ")"
196+
193
197
194jsTailcall :: JS -> JS
198jsTailcall :: JS -> JS
195jsTailcall call =
199jsTailcall call =
@@ -326,6 +330,9 @@ jsSubst var new (JSAssign lhs rhs) =
326jsSubst var new (JSAlloc val (Just js)) =
330jsSubst var new (JSAlloc val (Just js)) =
327 JSAlloc val (Just (jsSubst var new js))
331 JSAlloc val (Just (jsSubst var new js))
328
332
333+jsSubst var new (JSParens js) =
334+ JSParens $ jsSubst var new js
335+
329jsSubst _ _ js = js
336jsSubst _ _ js = js
330
337
331
338
@@ -378,6 +385,9 @@ removeAllocations (JSCond conds) =
378removeAllocations (JSTernary c t f) =
385removeAllocations (JSTernary c t f) =
379 JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
386 JSTernary (removeAllocations c) (removeAllocations t) (removeAllocations f)
380
387
388+removeAllocations (JSParens js) =
389+ JSParens $ removeAllocations js
390+
381removeAllocations js = js
391removeAllocations js = js
382
392
383
393
@@ -497,6 +507,9 @@ inlineJS (JSCond cases) =
497inlineJS (JSOp op lhs rhs) =
507inlineJS (JSOp op lhs rhs) =
498 JSOp op (inlineJS lhs) (inlineJS rhs)
508 JSOp op (inlineJS lhs) (inlineJS rhs)
499
509
510+inlineJS (JSParens js) =
511+ JSParens $ inlineJS js
512+
500inlineJS js = js
513inlineJS js = js
501
514
502
515
@@ -549,6 +562,8 @@ deadEvalApplyCases js =
549
562
550 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
563 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
551
564
565+ getTags (JSParens js) = getTags js
566+
552 getTags js = []
567 getTags js = []
553
568
554
569
@@ -606,6 +621,8 @@ initConstructors js =
606
621
607 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
622 getTags (JSTernary c t f) = getTags c ++ getTags t ++ getTags f
608
623
624+ getTags (JSParens js) = getTags js
625+
609 getTags js = []
626 getTags js = []
610
627
611
628
@@ -656,6 +673,9 @@ initConstructors js =
656 replaceHelper tags f
673 replaceHelper tags f
657 )
674 )
658
675
676+ replaceHelper tags (JSParens js) =
677+ JSParens $ replaceHelper tags js
678+
659 replaceHelper tags js = js
679 replaceHelper tags js = js
660
680
661
681
@@ -730,6 +750,9 @@ removeIDs js =
730 removeIDCall ids (JSArray fields) =
750 removeIDCall ids (JSArray fields) =
731 JSArray $ map (removeIDCall ids) fields
751 JSArray $ map (removeIDCall ids) fields
732
752
753+ removeIDCall ids (JSParens js) =
754+ JSParens $ removeIDCall ids js
755+
733 removeIDCall _ js = js
756 removeIDCall _ js = js
734
757
735
758
@@ -816,13 +839,39 @@ inlineFunctions js =
816 , countInvokations name f
839 , countInvokations name f
817 ]
840 ]
818
841
842+ countInvokations name (JSParens js) =
843+ countInvokations name js
844+
819 countInvokations _ _ = 0
845 countInvokations _ _ = 0
820
846
821
847
822reduceConstant :: JS -> JS
848reduceConstant :: JS -> JS
849+reduceConstant
823−reduceConstant (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
850+ (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
824− JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [JSNum num])
851+ JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [val])
825− )]) = JSNum num
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)
826
875
827reduceConstant (JSReturn ret) =
876reduceConstant (JSReturn ret) =
828 JSReturn (reduceConstant ret)
877 JSReturn (reduceConstant ret)
@@ -851,8 +900,12 @@ reduceConstant (JSSeq seq) =
851reduceConstant (JSFunction args body) =
900reduceConstant (JSFunction args body) =
852 JSFunction args (reduceConstant body)
901 JSFunction args (reduceConstant body)
853
902
903+reduceConstant (JSParens js) =
904+ JSParens $ reduceConstant js
905+
854reduceConstant js = js
906reduceConstant js = js
855
907
908+
856reduceConstants :: JS -> JS
909reduceConstants :: JS -> JS
857reduceConstants js
910reduceConstants js
858 | ret <- reduceConstant js
911 | ret <- reduceConstant js
@@ -929,6 +982,9 @@ optimizeEvalTailcalls (fun, tc) js =
929 optHelper f
982 optHelper f
930 )
983 )
931
984
985+ optHelper (JSParens js) =
986+ JSParens $ optHelper js
987+
932 optHelper js = js
988 optHelper js = js
933
989
934
990
@@ -992,6 +1048,9 @@ removeInstanceChecks (JSTernary c t f) =
992 removeInstanceChecks f
1048 removeInstanceChecks f
993 )
1049 )
994
1050
1051+removeInstanceChecks (JSParens js) =
1052+ JSParens $ removeInstanceChecks js
1053+
995removeInstanceChecks js = js
1054removeInstanceChecks js = js
996
1055
997
1056
@@ -1071,6 +1130,9 @@ reduceLoop reduced (cons, program) =
1071 reduceCall funs (JSArray fields) =
1130 reduceCall funs (JSArray fields) =
1072 JSArray $ map (reduceCall funs) fields
1131 JSArray $ map (reduceCall funs) fields
1073
1132
1133+ reduceCall funs (JSParens js) =
1134+ JSParens $ reduceCall funs js
1135+
1074 reduceCall _ js = js
1136 reduceCall _ js = js
1075
1137
1076
1138