on master help sign in
javascript: moved EVALTC and APPLYTC into codegen
commitf182cf25ba0a5461f55d62953d8f33c6d8f32c01 authorraichoo <raichoo@googlemail.com> dateMon 3 Feb 2014, 20:19 (13 years ago) parent2b44cd9
2 files changed+61−22
jsrts/Runtime-common.js−17 src/IRTS/CodegenJavaScript.hs+61−5
jsrts/Runtime-common.js−17
@@ -37,20 +37,3 @@ var __IDRRT__tailcall = function(k) {
3737
3838 return ret;
3939};
40−
41−var __IDRRT__EVALTC = function(arg0) {
42− var ret = (arg0 instanceof __IDRRT__Con && __IDRLT__mEVAL0[arg0.tag])?(__IDRLT__mEVAL0[arg0.tag](arg0)):(arg0);
43− while (ret instanceof __IDRRT__Cont)
44− ret = ret.k();
45−
46− return ret;
47−};
48−
49−var __IDRRT__APPLYTC = function(fn0, arg0) {
50− var ev = __IDRRT__EVALTC(fn0);
51− var ret = (ev instanceof __IDRRT__Con && __IDRLT__mAPPLY0[ev.tag])?(__IDRLT__mAPPLY0[ev.tag](fn0,arg0,ev)):(null)
52− while (ret instanceof __IDRRT__Cont)
53− ret = ret.k();
54−
55− return ret;
56−}
src/IRTS/CodegenJavaScript.hs+61−5
@@ -766,9 +766,9 @@ elimDuplicateEvals (JSAlloc fun (Just (JSFunction args (JSSeq seq)))) =
766766elimDuplicateEvals js = js
767767
768768
769−optimizeEvalTailcalls :: (String, String) -> JS -> JS
770−optimizeEvalTailcalls (fun, tc) js =
771− optHelper js
769+optimizeRuntimeCalls :: String -> String -> [JS] -> [JS]
770+optimizeRuntimeCalls fun tc js =
771+ optTC tc : map optHelper js
772772 where
773773 optHelper :: JS -> JS
774774 optHelper (JSApp (JSIdent "__IDRRT__tailcall") [
@@ -779,6 +779,62 @@ optimizeEvalTailcalls (fun, tc) js =
779779 optHelper js = transformJS optHelper js
780780
781781
782+ optTC :: String -> JS
783+ optTC tc@"__IDRRT__EVALTC" =
784+ JSAlloc tc (Just $ JSFunction ["arg0"] (
785+ JSSeq [ JSAlloc "ret" $ Just (
786+ JSTernary (
787+ (JSIdent "arg0" `jsInstanceOf` jsCon) `jsAnd`
788+ (hasProp "__IDRLT__mEVAL0" "arg0")
789+ ) (JSApp
790+ (JSIndex
791+ (JSIdent "__IDRLT__mEVAL0")
792+ (JSProj (JSIdent "arg0") "tag")
793+ )
794+ [JSIdent "arg0"]
795+ ) (JSIdent "arg0")
796+ )
797+ , JSWhile (JSIdent "ret" `jsInstanceOf` (JSIdent "__IDRRT__Cont")) (
798+ JSAssign (JSIdent "ret") (
799+ JSApp (JSProj (JSIdent "ret") "k") []
800+ )
801+ )
802+ , JSReturn $ JSIdent "ret"
803+ ]
804+ ))
805+
806+ optTC tc@"__IDRRT__APPLYTC" =
807+ JSAlloc tc (Just $ JSFunction ["fn0", "arg0"] (
808+ JSSeq [ JSAlloc "ev" $ Just (JSApp
809+ (JSIdent "__IDRRT__EVALTC") [JSIdent "fn0"]
810+ )
811+ , JSAlloc "ret" $ Just (
812+ JSTernary (
813+ (JSIdent "ev" `jsInstanceOf` jsCon) `jsAnd`
814+ (hasProp "__IDRLT__mAPPLY0" "ev")
815+ ) (JSApp
816+ (JSIndex
817+ (JSIdent "__IDRLT__mAPPLY0")
818+ (JSProj (JSIdent "ev") "tag")
819+ )
820+ [JSIdent "fn0", JSIdent "arg0", JSIdent "ev"]
821+ ) JSNull
822+ )
823+ , JSWhile (JSIdent "ret" `jsInstanceOf` (JSIdent "__IDRRT__Cont")) (
824+ JSAssign (JSIdent "ret") (
825+ JSApp (JSProj (JSIdent "ret") "k") []
826+ )
827+ )
828+ , JSReturn $ JSIdent "ret"
829+ ]
830+ ))
831+
832+
833+ hasProp :: String -> String -> JS
834+ hasProp table var =
835+ JSIndex (JSIdent table) (JSProj (JSIdent var) "tag")
836+
837+
782838unfoldLookupTable :: [JS] -> [JS]
783839unfoldLookupTable input =
784840 let (evals, evalunfold) = unfoldLT "__IDRLT__mEVAL0" input
@@ -967,8 +1023,8 @@ codegenJavaScript target definitions filename outputType = do
9671023 , map removeAllocations
9681024 , elimDeadLoop
9691025 , map elimDuplicateEvals
970− , map (optimizeEvalTailcalls ("__IDR__mEVAL0", "__IDRRT__EVALTC"))
971− , map (optimizeEvalTailcalls ("__IDR__mAPPLY0", "__IDRRT__APPLYTC"))
1026+ , optimizeRuntimeCalls "__IDR__mEVAL0" "__IDRRT__EVALTC"
1027+ , optimizeRuntimeCalls "__IDR__mAPPLY0" "__IDRRT__APPLYTC"
9721028 , map removeInstanceChecks
9731029 , inlineFunctions
9741030 , map reduceContinuations