git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Inline `one` and `zero` for numbers
e1e313eGary Burgess
src/Language/PureScript/CodeGen/JS/Optimizer.hs+1
@@ -63,6 +63,7 @@ optimize opts | optionsNoOptimizations opts = id
63 , etaConvert
63 , etaConvert
64 , evaluateIifes
64 , evaluateIifes
65 , inlineVariables
65 , inlineVariables
66+ , inlineValues
66 , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
67 , inlineOperator (C.prelude, (C.$)) $ \f x -> JSApp f [x]
67 , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
68 , inlineOperator (C.prelude, (C.#)) $ \x f -> JSApp f [x]
68 , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
69 , inlineOperator (C.preludeUnsafe, C.unsafeIndex) $ flip JSIndexer
src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+20−9
@@ -15,6 +15,7 @@
15
15
16module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
16module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
17 inlineVariables,
17 inlineVariables,
18+ inlineValues,
18 inlineOperator,
19 inlineOperator,
19 inlineCommonOperators,
20 inlineCommonOperators,
20 etaConvert,
21 etaConvert,
@@ -79,6 +80,14 @@ inlineVariables = everywhereOnJS $ removeFromBlock go
79 go (map (replaceIdent var js) sts)
80 go (map (replaceIdent var js) sts)
80 go (s:sts) = s : go sts
81 go (s:sts) = s : go sts
81
82
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+
82inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
91inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
83inlineOperator (m, op) f = everywhereOnJS convert
92inlineOperator (m, op) f = everywhereOnJS convert
84 where
93 where
@@ -131,7 +140,7 @@ inlineCommonOperators = applyAll $
131 binary dictName opString op = everywhereOnJS convert
140 binary dictName opString op = everywhereOnJS convert
132 where
141 where
133 convert :: JS -> JS
142 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
135 convert other = other
144 convert other = other
136 isOp (JSAccessor longForm (JSAccessor prelude (JSVar _))) = prelude == C.prelude && longForm == identToJs (Op opString)
145 isOp (JSAccessor longForm (JSAccessor prelude (JSVar _))) = prelude == C.prelude && longForm == identToJs (Op opString)
137 isOp (JSIndexer (JSStringLiteral op') (JSVar prelude)) = prelude == C.prelude && opString == op'
146 isOp (JSIndexer (JSStringLiteral op') (JSVar prelude)) = prelude == C.prelude && opString == op'
@@ -140,20 +149,14 @@ inlineCommonOperators = applyAll $
140 binaryFunction dictName fnName op = everywhereOnJS convert
149 binaryFunction dictName fnName op = everywhereOnJS convert
141 where
150 where
142 convert :: JS -> JS
151 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
144 convert other = other
153 convert other = other
145− isOp (JSAccessor fnName' (JSVar prelude)) = prelude == C.prelude && fnName == fnName'
146− isOp _ = False
147 unary :: String -> String -> UnaryOperator -> JS -> JS
154 unary :: String -> String -> UnaryOperator -> JS -> JS
148 unary dictName fnName op = everywhereOnJS convert
155 unary dictName fnName op = everywhereOnJS convert
149 where
156 where
150 convert :: JS -> JS
157 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
152 convert other = other
159 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
157 mkFn :: Int -> JS -> JS
160 mkFn :: Int -> JS -> JS
158 mkFn 0 = everywhereOnJS convert
161 mkFn 0 = everywhereOnJS convert
159 where
162 where
@@ -189,3 +192,11 @@ inlineCommonOperators = applyAll $
189 go 0 acc (JSApp runFnN [fn]) | isNFn C.runFn n runFnN && length acc == n = Just (JSApp fn acc)
192 go 0 acc (JSApp runFnN [fn]) | isNFn C.runFn n runFnN && length acc == n = Just (JSApp fn acc)
190 go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
193 go m acc (JSApp lhs [arg]) = go (m - 1) (arg : acc) lhs
191 go _ _ _ = Nothing
194 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