on master help sign in
Merge pull request #3841 from be5invis/be5invis-codegen-js-opt

JS Codegen — Optimization, phase II

commit1be086f26656303da483d8dc033c455f2e5ff27d authorNiklas Larsson <metaniklas@gmail.com> dateTue 13 Jun 2017, 00:29 (9 years ago) parentff75058249f087
3 files changed+48−45
CHANGELOG.md+7−4 src/IRTS/JavaScript/Codegen.hs+36−36 src/IRTS/JavaScript/PrimOp.hs+5−5
CHANGELOG.md+7−4
@@ -1,13 +1,16 @@
11
22## Tool Updates
33
4−+ New javascript code generator that uses an higher level intermediate
4++ New JavaScript code generator that uses an higher level intermediate
55 representation.
6−
6+
7++ Various optimizations of the novel JavaScript code generator to make
8+ its results fast.
9+
710+ Names are now annotated with their representations over the IDE
811 protocol, which allows IDEs to provide commands that work on special
9− names that don't have syntax, such as case block names
10−
12+ names that don't have syntax, such as case block names.
13+
1114
1215# New in 1.0
1316
src/IRTS/JavaScript/Codegen.hs+36−36
@@ -80,12 +80,7 @@ isYes _ = False
8080codegenJs :: CGConf -> CodeGenerator
8181codegenJs conf ci =
8282 do
83− optim <- isYes <$> lookupEnv "IDRISJS_OPTIM"
8483 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
85− if debug then
86− if optim then putStrLn "compiling width idris-js optimizations"
87− else putStrLn "compiling widthout idris-js optimizations"
88− else pure ()
8984 let defs = Map.fromList $ liftDecls ci
9085 let used = Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
9186 if debug then
@@ -108,12 +103,15 @@ codegenJs conf ci =
108103 TIO.writeFile (outputFile ci) $ T.concat [ header conf
109104 , "\"use strict\";\n\n"
110105 , "(function(){\n\n"
106+ -- rts
111107 , runtimeCommon, "\n"
112108 , extraRT, "\n"
113109 , jsbn, "\n"
110+ -- external libraries
111+ , includes, "\n"
112+ -- user code
114113 , doPartials (partialApplications stats), "\n"
115114 , doHiddenClasses (hiddenClasses stats), "\n"
116− , includes, "\n"
117115 , out, "\n"
118116 , jsName (sMN 0 "runMain"), "();\n"
119117 , "}.call(this))"
@@ -135,6 +133,7 @@ doHiddenClasses :: Set HiddenClass -> Text
135133doHiddenClasses x =
136134 T.intercalate "\n" (map f $ Set.toList x)
137135 where
136+ f p@(HiddenClass n id 0) = jsStmt2Text $ JsDecConst (jsNameHiddenClass p) $ JsObj [("type", JsInt id)]
138137 f p@(HiddenClass n id arity) =
139138 let vars = map dataPartName $ take arity [1..]
140139 in jsStmt2Text $
@@ -349,27 +348,9 @@ cgBody' rt (LApp tailcall (LV (Glob fn)) args) =
349348 let ((y1,y2), y3) = tailCallOptimRefreshArgs (zip argN argVals) Set.empty
350349 addUsedArgsTailCallOptim y3
351350 pure (preDecs, y1 `JsSeq` y2)
352− _ ->
353− do
354− argsFn <- getArgList fn
355− case argsFn of
356− Nothing -> do
357− fn' <- cgName fn
358− pure (preDecs, addRT rt $ jsCurryApp fn' argVals )
359− Just agFn ->
360− do
361− let lenAgFn = length agFn
362− let lenArgs = length args
363− case compare lenAgFn lenArgs of
364− EQ ->
365− pure (preDecs, addRT rt $ jsAppN fname argVals)
366− LT ->
367− pure (preDecs, addRT rt $ jsCurryApp (jsAppN fname (take lenAgFn argVals)) (drop lenAgFn argVals) )
368− GT ->
369− do
370− let part = Partial fn lenArgs lenAgFn
371− addPartial part
372− pure (preDecs, addRT rt $ jsAppN (jsNamePartial part) argVals )
351+ _ -> do
352+ app <- formApp fn argVals
353+ pure (preDecs, addRT rt app)
373354
374355cgBody' rt (LForce (LLazyApp n args)) = cgBody rt (LApp False (LV (Glob n)) args)
375356cgBody' rt (LLazyApp n args) =
@@ -447,18 +428,36 @@ altHasNoProj :: LAlt -> Bool
447428altHasNoProj (LConCase _ _ args _) = args == []
448429altHasNoProj _ = True
449430
431+formApp :: Name -> [JsExpr] -> State CGBodyState JsExpr
432+formApp fn argVals = case specialCall fn of
433+ Just (arity, g) | arity == length argVals -> pure $ g argVals
434+ _ -> do
435+ argsFn <- getArgList fn
436+ fname <- cgName fn
437+ case argsFn of
438+ Nothing -> pure $ jsCurryApp fname argVals
439+ Just agFn -> do
440+ let lenAgFn = length agFn
441+ let lenArgs = length argVals
442+ case compare lenAgFn lenArgs of
443+ EQ -> pure $ JsApp fname argVals
444+ LT -> pure $ jsCurryApp (JsApp fname (take lenAgFn argVals)) (drop lenAgFn argVals)
445+ GT -> do
446+ let part = Partial fn lenArgs lenAgFn
447+ addPartial part
448+ pure $ jsAppN (jsNamePartial part) argVals
449+
450450formCon :: Name -> [JsExpr] -> State CGBodyState JsExpr
451451formCon n args = do
452452 case specialCased n of
453453 Just (ctor, test, match) -> pure $ ctor args
454454 Nothing -> do
455455 (conId, arity) <- getConsId n
456− if (arity > 0)
457− then do
458− let hc = HiddenClass n conId arity
459− addHiddenClass hc
460− pure $ JsNew (JsVar $ jsNameHiddenClass hc) args
461− else pure $ JsInt conId
456+ let hc = HiddenClass n conId arity
457+ addHiddenClass hc
458+ pure $ if (arity > 0)
459+ then JsNew (JsVar $ jsNameHiddenClass hc) args
460+ else JsVar $ jsNameHiddenClass hc
462461
463462formConTest :: Name -> JsExpr -> State CGBodyState JsExpr
464463formConTest n x = do
@@ -466,9 +465,10 @@ formConTest n x = do
466465 Just (ctor, test, match) -> pure $ test x
467466 Nothing -> do
468467 (conId, arity) <- getConsId n
469− if (arity > 0)
470− then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
471− else pure $ JsBinOp "===" x (JsInt conId)
468+ pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
469+ -- if (arity > 0)
470+ -- then pure $ JsBinOp "===" (JsProp x (T.pack "type")) (JsInt conId)
471+ -- else pure $ JsBinOp "===" x (JsInt conId)
472472
473473formProj :: Name -> JsExpr -> Int -> JsExpr
474474formProj n v i =
src/IRTS/JavaScript/PrimOp.hs+5−5
@@ -37,7 +37,7 @@ primDB =
3737 Map.fromList [
3838 item (LPlus ATFloat) False PTAny $ binop "+"
3939 , item (LPlus (ATInt ITChar)) False PTAny $ JsForeign "String.fromCharCode(%0.charCodeAt(0) + %1.charCodeAt(0))"
40− , item (LPlus (ATInt ITNative)) False PTAny $ JsForeign "%0+%1|0"
40+ , item (LPlus (ATInt ITNative)) False PTAny $ binop "+"
4141 , item (LPlus (ATInt (ITFixed IT8))) False PTAny $ JsForeign "%0 + %1 & 0xFF"
4242 , item (LPlus (ATInt (ITFixed IT16))) False PTAny $ JsForeign "%0 + %1 & 0xFFFF"
4343 , item (LPlus (ATInt (ITFixed IT32))) False PTAny $ JsForeign "%0+%1|0"
@@ -46,7 +46,7 @@ primDB =
4646 \[l, r] -> JsForeign "%0.add(%1).and(new $JSRTS.jsbn.BigInteger(%2))" [l,r, JsStr $ show 0xFFFFFFFFFFFFFFFF]
4747 , item (LMinus ATFloat) False PTAny $ binop "-"
4848 , item (LMinus (ATInt ITChar)) False PTAny $ JsForeign "String.fromCharCode(%0.charCodeAt(0) - %1.charCodeAt(0))"
49− , item (LMinus (ATInt ITNative)) False PTAny $ JsForeign "%0-%1|0"
49+ , item (LMinus (ATInt ITNative)) False PTAny $ binop "-"
5050 , item (LMinus (ATInt (ITFixed IT8))) False PTAny $ JsForeign "%0 - %1 & 0xFF"
5151 , item (LMinus (ATInt (ITFixed IT16))) False PTAny $ JsForeign "%0 - %1 & 0xFFFF"
5252 , item (LMinus (ATInt (ITFixed IT32))) False PTAny $ JsForeign "%0-%1|0"
@@ -55,7 +55,7 @@ primDB =
5555 \[l, r] -> JsForeign "%0.subtract(%1).and(new $JSRTS.jsbn.BigInteger(%2))" [l,r, JsStr $ show 0xFFFFFFFFFFFFFFFF]
5656 , item (LTimes ATFloat) False PTAny $ binop "*"
5757 , item (LTimes (ATInt ITChar)) False PTAny $ JsForeign "String.fromCharCode(%0.charCodeAt(0) * %1.charCodeAt(0))"
58− , item (LTimes (ATInt ITNative)) False PTAny $ JsForeign "%0*%1|0"
58+ , item (LTimes (ATInt ITNative)) False PTAny $ binop "*"
5959 , item (LTimes (ATInt (ITFixed IT8))) False PTAny $ JsForeign "%0 * %1 & 0xFF"
6060 , item (LTimes (ATInt (ITFixed IT16))) False PTAny $ JsForeign "%0 * %1 & 0xFFFF"
6161 , item (LTimes (ATInt (ITFixed IT32))) False PTAny $ JsForeign "%0*%1|0"
@@ -71,14 +71,14 @@ primDB =
7171 , item (LSDiv (ATInt (ITFixed IT16))) False PTAny $ JsForeign "%0 / %1"
7272 , item (LSDiv (ATInt (ITFixed IT32))) False PTAny $ JsForeign "%0 / %1 |0"
7373 , item (LSDiv (ATInt (ITFixed IT64))) True PTAny $ JsForeign "%0.divide(%1)"
74− , item (LSDiv (ATInt ITNative)) False PTAny $ JsForeign "%0/%1|0"
74+ , item (LSDiv (ATInt ITNative)) False PTAny $ JsForeign "%0/%1|0" -- we need "|0" in this
7575 , item (LSDiv (ATInt ITBig)) True PTAny $ method "divide"
7676 , item (LURem (ITFixed IT8)) False PTAny $ JsForeign "%0 % %1"
7777 , item (LURem (ITFixed IT16)) False PTAny $ JsForeign "%0 % %1"
7878 , item (LURem (ITFixed IT32)) False PTAny $ JsForeign "(%0>>>0) % (%1>>>0) |0"
7979 , item (LURem (ITFixed IT64)) True PTAny $ method "remainder"
8080 , item (LSRem ATFloat) False PTAny $ binop "%"
81− , item (LSRem (ATInt ITNative)) False PTAny $ JsForeign "%0 % %1 |0"
81+ , item (LSRem (ATInt ITNative)) False PTAny $ binop "%"
8282 , item (LSRem (ATInt (ITFixed IT8))) False PTAny $ JsForeign "%0 % %1"
8383 , item (LSRem (ATInt (ITFixed IT16))) False PTAny $ JsForeign "%0 % %1"
8484 , item (LSRem (ATInt (ITFixed IT32))) False PTAny $ JsForeign "%0 % %1 |0"