on master help sign in
javascript: init local constructors
67a5dceraichoo
src/IRTS/CodegenJavaScript.hs+65−4
@@ -407,8 +407,8 @@ removeAllocations (JSSeq body) =
407removeAllocations js = transformJS removeAllocations js
407removeAllocations js = transformJS removeAllocations js
408
408
409
409
410−isJSConstant :: JS -> Bool
410+isJSConstant :: [String] -> JS -> Bool
411−isJSConstant js
411+isJSConstant constants js
412 | JSString _ <- js = True
412 | JSString _ <- js = True
413 | JSChar _ <- js = True
413 | JSChar _ <- js = True
414 | JSNum _ <- js = True
414 | JSNum _ <- js = True
@@ -416,6 +416,13 @@ isJSConstant js
416 | JSNull <- js = True
416 | JSNull <- js = True
417
417
418 | JSApp (JSIdent "__IDRRT__bigInt") _ <- js = True
418 | JSApp (JSIdent "__IDRRT__bigInt") _ <- js = True
419+
420+ | JSArray vals <- js = all (isJSConstant constants) vals
421+ | JSNew "__IDRRT__Con" args <- js = all (isJSConstant constants) args
422+
423+ | JSIdent name <- js
424+ , name `elem` constants = True
425+
419 | otherwise = False
426 | otherwise = False
420
427
421
428
@@ -443,12 +450,12 @@ inlineJS (JSApp (JSProj (JSFunction args (JSReturn body)) "apply") [
443 ) (n + 1)
450 ) (n + 1)
444
451
445inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
452inlineJS (JSApp (JSIdent "__IDR__mEVAL0") [val])
446− | isJSConstant val = val
453+ | isJSConstant [] val = val
447
454
448inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
455inlineJS (JSApp (JSIdent "__IDRRT__tailcall") [
449 JSFunction [] (JSReturn val)
456 JSFunction [] (JSReturn val)
450 ])
457 ])
451− | isJSConstant val = val
458+ | isJSConstant [] val = val
452
459
453inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
460inlineJS (JSApp (JSFunction [arg] (JSReturn ret)) [val])
454 | JSNew con [tag, vals] <- ret
461 | JSNew con [tag, vals] <- ret
@@ -1000,6 +1007,59 @@ optimizeJS = inlineLoop
1000 | otherwise = js
1007 | otherwise = js
1001
1008
1002
1009
1010+extractLocalConstructors :: [JS] -> [JS]
1011+extractLocalConstructors js =
1012+ concatMap extractLocalConstructors' js
1013+ where
1014+ globalCons :: [String]
1015+ globalCons = concatMap (getGlobalCons) js
1016+
1017+
1018+ extractLocalConstructors' :: JS -> [JS]
1019+ extractLocalConstructors' js@(JSAlloc fun (Just (JSFunction args body))) =
1020+ map allocCon cons ++ [foldr (uncurry jsSubst) js (reverse cons)]
1021+ where
1022+ cons :: [(JS, JS)]
1023+ cons = zipWith genName (foldJS match (++) [] body) [1..]
1024+ where
1025+ genName :: JS -> Int -> (JS, JS)
1026+ genName js id =
1027+ (js, JSIdent $ idrCTRNamespace ++ fun ++ "_" ++ show id)
1028+
1029+ match :: JS -> [JS]
1030+ match js
1031+ | JSNew "__IDRRT__Con" args <- js
1032+ , all isConstant args = [js]
1033+ | otherwise = []
1034+
1035+
1036+ allocCon :: (JS, JS) -> JS
1037+ allocCon (js, JSIdent name) =
1038+ JSAlloc name (Just js)
1039+
1040+
1041+ isConstant :: JS -> Bool
1042+ isConstant js
1043+ | JSNew "__IDRRT__Con" args <- js
1044+ , all isConstant args =
1045+ True
1046+ | otherwise =
1047+ isJSConstant globalCons js
1048+
1049+ extractLocalConstructors' js = [js]
1050+
1051+
1052+ getGlobalCons :: JS -> [String]
1053+ getGlobalCons js = foldJS match (++) [] js
1054+ where
1055+ match :: JS -> [String]
1056+ match js
1057+ | (JSAlloc name (Just (JSNew "__IDRRT__Con" _))) <- js =
1058+ [name]
1059+ | otherwise =
1060+ []
1061+
1062+
1003codegenJavaScript
1063codegenJavaScript
1004 :: JSTarget
1064 :: JSTarget
1005 -> [(Name, SDecl)]
1065 -> [(Name, SDecl)]
@@ -1056,6 +1116,7 @@ codegenJavaScript target definitions filename outputType = do
1056 , map removeInstanceChecks
1116 , map removeInstanceChecks
1057 , inlineFunctions
1117 , inlineFunctions
1058 , map reduceContinuations
1118 , map reduceContinuations
1119+ , extractLocalConstructors
1059 , unfoldLookupTable
1120 , unfoldLookupTable
1060 ]
1121 ]
1061
1122