javascript: moved EVALTC and APPLYTC into codegen

This commit is contained in:
raichoo 2014-02-03 20:19:09 +01:00
parent 2b44cd963f
commit f182cf25ba
2 changed files with 61 additions and 22 deletions

View File

@ -37,20 +37,3 @@ var __IDRRT__tailcall = function(k) {
return ret;
};
var __IDRRT__EVALTC = function(arg0) {
var ret = (arg0 instanceof __IDRRT__Con && __IDRLT__mEVAL0[arg0.tag])?(__IDRLT__mEVAL0[arg0.tag](arg0)):(arg0);
while (ret instanceof __IDRRT__Cont)
ret = ret.k();
return ret;
};
var __IDRRT__APPLYTC = function(fn0, arg0) {
var ev = __IDRRT__EVALTC(fn0);
var ret = (ev instanceof __IDRRT__Con && __IDRLT__mAPPLY0[ev.tag])?(__IDRLT__mAPPLY0[ev.tag](fn0,arg0,ev)):(null)
while (ret instanceof __IDRRT__Cont)
ret = ret.k();
return ret;
}

View File

@ -766,9 +766,9 @@ elimDuplicateEvals (JSAlloc fun (Just (JSFunction args (JSSeq seq)))) =
elimDuplicateEvals js = js
optimizeEvalTailcalls :: (String, String) -> JS -> JS
optimizeEvalTailcalls (fun, tc) js =
optHelper js
optimizeRuntimeCalls :: String -> String -> [JS] -> [JS]
optimizeRuntimeCalls fun tc js =
optTC tc : map optHelper js
where
optHelper :: JS -> JS
optHelper (JSApp (JSIdent "__IDRRT__tailcall") [
@ -779,6 +779,62 @@ optimizeEvalTailcalls (fun, tc) js =
optHelper js = transformJS optHelper js
optTC :: String -> JS
optTC tc@"__IDRRT__EVALTC" =
JSAlloc tc (Just $ JSFunction ["arg0"] (
JSSeq [ JSAlloc "ret" $ Just (
JSTernary (
(JSIdent "arg0" `jsInstanceOf` jsCon) `jsAnd`
(hasProp "__IDRLT__mEVAL0" "arg0")
) (JSApp
(JSIndex
(JSIdent "__IDRLT__mEVAL0")
(JSProj (JSIdent "arg0") "tag")
)
[JSIdent "arg0"]
) (JSIdent "arg0")
)
, JSWhile (JSIdent "ret" `jsInstanceOf` (JSIdent "__IDRRT__Cont")) (
JSAssign (JSIdent "ret") (
JSApp (JSProj (JSIdent "ret") "k") []
)
)
, JSReturn $ JSIdent "ret"
]
))
optTC tc@"__IDRRT__APPLYTC" =
JSAlloc tc (Just $ JSFunction ["fn0", "arg0"] (
JSSeq [ JSAlloc "ev" $ Just (JSApp
(JSIdent "__IDRRT__EVALTC") [JSIdent "fn0"]
)
, JSAlloc "ret" $ Just (
JSTernary (
(JSIdent "ev" `jsInstanceOf` jsCon) `jsAnd`
(hasProp "__IDRLT__mAPPLY0" "ev")
) (JSApp
(JSIndex
(JSIdent "__IDRLT__mAPPLY0")
(JSProj (JSIdent "ev") "tag")
)
[JSIdent "fn0", JSIdent "arg0", JSIdent "ev"]
) JSNull
)
, JSWhile (JSIdent "ret" `jsInstanceOf` (JSIdent "__IDRRT__Cont")) (
JSAssign (JSIdent "ret") (
JSApp (JSProj (JSIdent "ret") "k") []
)
)
, JSReturn $ JSIdent "ret"
]
))
hasProp :: String -> String -> JS
hasProp table var =
JSIndex (JSIdent table) (JSProj (JSIdent var) "tag")
unfoldLookupTable :: [JS] -> [JS]
unfoldLookupTable input =
let (evals, evalunfold) = unfoldLT "__IDRLT__mEVAL0" input
@ -967,8 +1023,8 @@ codegenJavaScript target definitions filename outputType = do
, map removeAllocations
, elimDeadLoop
, map elimDuplicateEvals
, map (optimizeEvalTailcalls ("__IDR__mEVAL0", "__IDRRT__EVALTC"))
, map (optimizeEvalTailcalls ("__IDR__mAPPLY0", "__IDRRT__APPLYTC"))
, optimizeRuntimeCalls "__IDR__mEVAL0" "__IDRRT__EVALTC"
, optimizeRuntimeCalls "__IDR__mAPPLY0" "__IDRRT__APPLYTC"
, map removeInstanceChecks
, inlineFunctions
, map reduceContinuations