on master help sign in
javascript: reduce bitint constants
commit10863bf9fa4fe1d83dc85ad5f2cd78f1c08c870b authorraichoo <raichoo@googlemail.com> dateWed 16 Oct 2013, 19:32 (13 years ago) parent167e419
1 file changed+53−7
src/IRTS/CodegenJavaScript.hs+53−7
src/IRTS/CodegenJavaScript.hs+53−7
@@ -35,9 +35,14 @@ data JSType = JSIntTy
3535 | JSForgotTy
3636 deriving Eq
3737
38+data JSInteger = JSBigZero
39+ | JSBigOne
40+ | JSBigInt Integer
41+ deriving Eq
42+
3843data JSNum = JSInt Int
3944 | JSFloat Double
40− | JSInteger Integer
45+ | JSInteger JSInteger
4146 deriving Eq
4247
4348data JS = JSRaw String
@@ -149,9 +154,11 @@ compileJS (JSString str) =
149154 show str
150155
151156compileJS (JSNum num)
152− | JSInt i <- num = show i
153− | JSFloat f <- num = show f
154− | JSInteger i <- num = show i
157+ | JSInt i <- num = show i
158+ | JSFloat f <- num = show f
159+ | JSInteger JSBigZero <- num = "__IDRRT__ZERO"
160+ | JSInteger JSBigOne <- num = "__IDRRT__ONE"
161+ | JSInteger (JSBigInt i) <- num = show i
155162
156163compileJS (JSAssign lhs rhs) =
157164 compileJS lhs ++ "=" ++ compileJS rhs
@@ -211,8 +218,8 @@ jsTypeTag :: JS -> JS
211218jsTypeTag obj = JSProj obj "type"
212219
213220jsBigInt :: JS -> JS
214−jsBigInt (JSString "0") = JSIdent "__IDRRT__ZERO"
215−jsBigInt (JSString "1") = JSIdent "__IDRRT__ONE"
221+jsBigInt (JSString "0") = JSNum $ JSInteger JSBigZero
222+jsBigInt (JSString "1") = JSNum $ JSInteger JSBigOne
216223jsBigInt val = JSApp (JSIdent $ idrRTNamespace ++ "bigInt") [val]
217224
218225jsVar :: Int -> String
@@ -430,6 +437,45 @@ removeIDs js =
430437
431438 removeIDCall _ js = js
432439
440+reduceConstant :: JS -> JS
441+reduceConstant (JSApp (JSIdent "__IDRRT__tailcall") [JSFunction [] (
442+ JSReturn (JSApp (JSIdent "__IDR__mEVAL0") [JSNum num])
443+ )]) = JSNum num
444+
445+reduceConstant (JSReturn ret) =
446+ JSReturn (reduceConstant ret)
447+
448+reduceConstant (JSApp fun args) =
449+ JSApp (reduceConstant fun) (map reduceConstant args)
450+
451+reduceConstant (JSArray fields) =
452+ JSArray (map reduceConstant fields)
453+
454+reduceConstant (JSAlloc name (Just val)) =
455+ JSAlloc name $ Just (reduceConstant val)
456+
457+reduceConstant (JSNew con args) =
458+ JSNew con (map reduceConstant args)
459+
460+reduceConstant (JSProj obj field) =
461+ JSProj (reduceConstant obj) field
462+
463+reduceConstant (JSCond conds) =
464+ JSCond $ map (reduceConstant *** reduceConstant) conds
465+
466+reduceConstant (JSSeq seq) =
467+ JSSeq $ map reduceConstant seq
468+
469+reduceConstant (JSFunction args body) =
470+ JSFunction args (reduceConstant body)
471+
472+reduceConstant js = js
473+
474+reduceConstants :: JS -> JS
475+reduceConstants js
476+ | ret <- reduceConstant js
477+ , ret /= js = reduceConstants ret
478+ | otherwise = js
433479
434480reduceLoop :: [String] -> ([JS], [JS]) -> [JS]
435481reduceLoop reduced (cons, program) =
@@ -542,7 +588,7 @@ codegenJavaScript target definitions filename outputType = do
542588
543589 functions :: [String]
544590 functions =
545− map compileJS ((reduceJS . removeIDs) $ map (optimizeJS . translateDeclaration) def)
591+ map (compileJS . reduceConstants) ((reduceJS . removeIDs) $ map (optimizeJS . translateDeclaration) def)
546592
547593 mainLoop :: String
548594 mainLoop = compileJS $