@@ -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