on master help sign in
javascript: move Con and Cont into codegen
aa623f9raichoo
jsrts/Runtime-common.js−12
@@ -14,12 +14,6 @@ var __IDRRT__Float = new __IDRRT__Type('Float');
14var __IDRRT__Ptr = new __IDRRT__Type('Pointer');
14var __IDRRT__Ptr = new __IDRRT__Type('Pointer');
15var __IDRRT__Forgot = new __IDRRT__Type('Forgot');
15var __IDRRT__Forgot = new __IDRRT__Type('Forgot');
16
16
17−
18−/** @constructor */
19−var __IDRRT__Cont = function(k) {
20− this.k = k;
21−};
22−
23var __IDRRT__ffiWrap = function(fid) {
17var __IDRRT__ffiWrap = function(fid) {
24 return function(){
18 return function(){
25 var res = fid;
19 var res = fid;
@@ -36,12 +30,6 @@ var __IDRRT__ffiWrap = function(fid) {
36 }
30 }
37};
31};
38
32
39−/** @constructor */
40−var __IDRRT__Con = function(tag,vars) {
41− this.tag = tag;
42− this.vars = vars;
43−};
44−
45var __IDRRT__tailcall = function(k) {
33var __IDRRT__tailcall = function(k) {
46 var ret = k();
34 var ret = k();
47 while (ret instanceof __IDRRT__Cont)
35 while (ret instanceof __IDRRT__Cont)
src/IRTS/CodegenJavaScript.hs+35−4
@@ -322,6 +322,17 @@ transformJS tr js =
322 | otherwise = js
322 | otherwise = js
323
323
324
324
325+moveJSDeclToTop :: String -> [JS] -> [JS]
326+moveJSDeclToTop decl js = move ([], js)
327+ where
328+ move :: ([JS], [JS]) -> [JS]
329+ move (front, js@(JSAlloc name _):back)
330+ | name == decl = js : front ++ back
331+
332+ move (front, js:back) =
333+ move (front ++ [js], back)
334+
335+
325jsSubst :: JS -> JS -> JS -> JS
336jsSubst :: JS -> JS -> JS -> JS
326jsSubst var new old
337jsSubst var new old
327 | var == old = new
338 | var == old = new
@@ -518,8 +529,12 @@ deadEvalApplyCases js =
518initConstructors :: [JS] -> [JS]
529initConstructors :: [JS] -> [JS]
519initConstructors js =
530initConstructors js =
520 let tags = nub $ sort $ concat (map getTags js) in
531 let tags = nub $ sort $ concat (map getTags js) in
521− map createConstant tags ++ replaceConstructors tags js
532+ rearrangePrelude $ map createConstant tags ++ replaceConstructors tags js
522 where
533 where
534+ rearrangePrelude :: [JS] -> [JS]
535+ rearrangePrelude = moveJSDeclToTop $ idrRTNamespace ++ "Con"
536+
537+
523 getTags :: JS -> [Int]
538 getTags :: JS -> [Int]
524 getTags = foldJS match (++) []
539 getTags = foldJS match (++) []
525 where
540 where
@@ -670,6 +685,8 @@ inlineFunctions js =
670 match js
685 match js
671 | JSApp (JSIdent ident) _ <- js
686 | JSApp (JSIdent ident) _ <- js
672 , name == ident = 1
687 , name == ident = 1
688+ | JSNew con _ <- js
689+ , name == con = 1
673 | otherwise = 0
690 | otherwise = 0
674
691
675
692
@@ -854,9 +871,12 @@ codegenJavaScript target definitions filename outputType = do
854 functions :: [String]
871 functions :: [String]
855 functions = translate >>> optimize >>> compile $ def
872 functions = translate >>> optimize >>> compile $ def
856 where
873 where
874+ translate p =
857− translate p = concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
875+ prelude ++ concatMap translateDeclaration p ++ [mainLoop, invokeLoop]
876+ optimize p =
858− optimize p = foldl' (flip ($)) p opt
877+ foldl' (flip ($)) p opt
878+ compile =
859− compile = map compileJS
879+ map compileJS
860
880
861 opt =
881 opt =
862 [ map optimizeJS
882 [ map optimizeJS
@@ -874,6 +894,17 @@ codegenJavaScript target definitions filename outputType = do
874 , map reduceContinuations
894 , map reduceContinuations
875 ]
895 ]
876
896
897+ prelude :: [JS]
898+ prelude =
899+ [ JSAlloc (idrRTNamespace ++ "Cont") (Just $ JSFunction ["k"] (
900+ JSAssign (JSProj JSThis "k") (JSIdent "k")
901+ ))
902+ , JSAlloc (idrRTNamespace ++ "Con") (Just $ JSFunction ["tag", "vars"] (
903+ JSSeq [ JSAssign (JSProj JSThis "tag") (JSIdent "tag")
904+ , JSAssign (JSProj JSThis "vars") (JSIdent "vars")
905+ ]
906+ ))
907+ ]
877
908
878 mainLoop :: JS
909 mainLoop :: JS
879 mainLoop =
910 mainLoop =