git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Inline `one` and `zero` for numbers
commite1e313ecd73be782926d4834d739e52adaa55ef6 authorGary Burgess <gary.burgess@gmail.com> dateSun 15 Feb 2015, 01:38 (12 years ago) parent3f1eae5
2 files changed+21−9
src/Language/PureScript/CodeGen/JS/Optimizer.hs+1 src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+20−9
src/Language/PureScript/CodeGen/JS/Optimizer.hs+1
@@ -63,6 +63,7 @@ optimize opts | optionsNoOptimizations opts = id
6363 , etaConvert
6464 , evaluateIifes
6565 , inlineVariables
66+ , inlineValues
6667 , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
6768 , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
6869 , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+20−9
@@ -15,6 +15,7 @@
1515
1616module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
1717 inlineVariables,
18+ inlineValues,
1819 inlineOperator,
1920 inlineCommonOperators,
2021 etaConvert,
@@ -79,6 +80,14 @@ inlineVariables = everywhereOnJS $ removeFromBlock go
7980 go (map (replaceIdent var js) sts)
8081 go (s:sts) = s : go sts
8182
83+inlineValues :: JS -> JS
84+inlineValues = everywhereOnJS convert
85+ where
86+ convert :: JS -> JS
87+ convert (JSApp fn [dict]) | isPreludeDict C.semiringNumber dict && isPreludeFn C.zero fn = JSNumericLiteral (Left 0)
88+ convert (JSApp fn [dict]) | isPreludeDict C.semiringNumber dict && isPreludeFn C.one fn = JSNumericLiteral (Left 1)
89+ convert other = other
90+
8291inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
8392inlineOperator (m, op) f = everywhereOnJS convert
8493 where
@@ -131,7 +140,7 @@ inlineCommonOperators = applyAll $
131140 binary dictName opString op = everywhereOnJS convert
132141 where
133142 convert :: JS -> JS
134− convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName dict = JSBinary op x y
143+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isPreludeDict dictName dict = JSBinary op x y
135144 convert other = other
136145 isOp (JSAccessor longForm (JSAccessor prelude (JSVar _))) = prelude == C.prelude && longForm == identToJs (Op opString)
137146 isOp (JSIndexer (JSStringLiteral op') (JSVar prelude)) = prelude == C.prelude && opString == op'
@@ -140,20 +149,14 @@ inlineCommonOperators = applyAll $
140149 binaryFunction dictName fnName op = everywhereOnJS convert
141150 where
142151 convert :: JS -> JS
143− convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isOp fn && isOpDict dictName dict = JSBinary op x y
152+ convert (JSApp (JSApp (JSApp fn [dict]) [x]) [y]) | isPreludeFn fnName fn && isPreludeDict dictName dict = JSBinary op x y
144153 convert other = other
145− isOp (JSAccessor fnName' (JSVar prelude)) = prelude == C.prelude && fnName == fnName'
146− isOp _ = False
147154 unary :: String -> String -> UnaryOperator -> JS -> JS
148155 unary dictName fnName op = everywhereOnJS convert
149156 where
150157 convert :: JS -> JS
151− convert (JSApp (JSApp fn [dict]) [x]) | isOp fn && isOpDict dictName dict = JSUnary op x
158+ convert (JSApp (JSApp fn [dict]) [x]) | isPreludeFn fnName fn && isPreludeDict dictName dict = JSUnary op x
152159 convert other = other
153− isOp (JSAccessor fnName' (JSVar prelude)) = prelude == C.prelude && fnName' == fnName
154− isOp _ = False
155− isOpDict dictName (JSAccessor prop (JSVar prelude)) = prelude == C.prelude && prop == dictName
156− isOpDict _ _ = False
157160 mkFn :: Int -> JS -> JS
158161 mkFn 0 = everywhereOnJS convert
159162 where
@@ -189,3 +192,11 @@ inlineCommonOperators = applyAll $
189192 go 0 acc (JSApp runFnN [fn]) | isNFn C.runFn n runFnN && length acc == n = Just (JSApp fn acc)
190193 go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
191194 go _ _ _ = Nothing
195+
196+isPreludeDict :: String -> JS -> Bool
197+isPreludeDict dictName (JSAccessor prop (JSVar prelude)) = prelude == C.prelude && prop == dictName
198+isPreludeDict _ _ = False
199+
200+isPreludeFn :: String -> JS -> Bool
201+isPreludeFn fnName (JSAccessor fnName' (JSVar prelude)) = prelude == C.prelude && fnName' == fnName
202+isPreludeFn _ _ = False