Skip to content

Commit e1e313e

Browse files
committed
Inline one and zero for numbers
1 parent 3f1eae5 commit e1e313e

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/Language/PureScript/CodeGen/JS/Optimizer.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
module 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+
8291
inlineOperator :: (String, String) -> (JS -> JS -> JS) -> JS -> JS
8392
inlineOperator (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

0 commit comments

Comments
 (0)