1-----------------------------------------------------------------------------
3-- Module : Language.PureScript.Optimize
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
13-----------------------------------------------------------------------------
15module Language.PureScript.Optimize (
20import Data.Maybe (fromMaybe)
23import Language.PureScript.Names
24import Language.PureScript.CodeGen.JS.AST
25import Language.PureScript.Options
27optimize :: Options -> JS -> JS
31 . removeUnusedVariables
36replaceIdent :: (Data d) => Ident -> JS -> d -> d
37replaceIdent var1 js = everywhere (mkT replace)
39 replace (JSVar var2) | var1 == var2 = js
42replaceIdents :: (Data d) => [(Ident, JS)] -> d -> d
43replaceIdents vars = everywhere (mkT replace)
45 replace v@(JSVar var) = fromMaybe v $ lookup var vars
48isReassigned :: (Data d) => Ident -> d -> Bool
49isReassigned var1 = everything (||) (mkQ False check)
52 check (JSAssignment (JSAssignVariable var2) _) | var1 == var2 = True
55isUsed :: (Data d) => Ident -> d -> Bool
56isUsed var1 = everything (||) (mkQ False check)
59 check (JSVar var2) | var1 == var2 = True
60 check (JSAssignment target _) | var1 == targetVariable target = True
62 targetVariable :: JSAssignment -> Ident
63 targetVariable (JSAssignVariable var) = var
64 targetVariable (JSAssignProperty _ tgt) = targetVariable tgt
66shouldInline :: JS -> Bool
67shouldInline (JSVar _) = True
68shouldInline (JSNumericLiteral _) = True
69shouldInline (JSStringLiteral _) = True
70shouldInline (JSBooleanLiteral _) = True
71shouldInline (JSAccessor _ val) = shouldInline val
72shouldInline (JSIndexer index val) = shouldInline index && shouldInline val
75inlineVariables :: JS -> JS
76inlineVariables = everywhere (mkT removeFromBlock)
78 removeFromBlock :: JS -> JS
79 removeFromBlock (JSBlock sts) = JSBlock (go sts)
80 removeFromBlock js = js
83 go (JSVariableIntroduction var (Just js) : sts) | shouldInline js && not (isReassigned var sts) = go (replaceIdent var js sts)
84 go (s:sts) = s : go sts
86removeUnusedVariables :: JS -> JS
87removeUnusedVariables = everywhere (mkT removeFromBlock)
89 removeFromBlock :: JS -> JS
90 removeFromBlock (JSBlock sts) = JSBlock (go sts)
91 removeFromBlock js = js
94 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
95 go (s:sts) = s : go sts
98etaConvert = everywhere (mkT convert)
101 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing idents (JSBlock body)) args)])
102 | all shouldInline args = JSBlock (replaceIdents (zip idents args) body)
106unThunk = everywhere (mkT convert)
109 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
112tco :: Options -> JS -> JS
113tco opts | optionsTco opts = tco'
117tco' = everywhere (mkT convert)
121 tcoVar :: Ident -> Ident
122 tcoVar (Ident arg) = Ident $ "__tco_" ++ arg
123 tcoVar _ = error "Invalid name in tcoVar"
125 convert js@(JSVariableIntroduction name (Just fn@(JSFunction Nothing _ _))) =
127 (argss, body', replace) = collectAllFunctionArgs [] id fn
129 _ | isTailCall name body' ->
131 allArgs = reverse $ concat argss
133 JSVariableIntroduction name (Just (replace (toLoop name allArgs body')))
136 collectAllFunctionArgs :: [[Ident]] -> (JS -> JS) -> JS -> ([[Ident]], JS, JS -> JS)
137 collectAllFunctionArgs allArgs f (JSFunction Nothing args (JSBlock (body@(JSReturn _):_))) =
138 collectAllFunctionArgs (args : allArgs) (\b -> f (JSFunction Nothing args (JSBlock [b]))) body
139 collectAllFunctionArgs allArgs f (JSReturn (JSFunction Nothing args (JSBlock [body]))) =
140 collectAllFunctionArgs (args : allArgs) (\b -> f (JSReturn (JSFunction Nothing args (JSBlock [b])))) body
141 collectAllFunctionArgs allArgs f (JSReturn (JSFunction Nothing args body@(JSBlock _))) =
142 (args : allArgs, body, \b -> f (JSReturn (JSFunction Nothing args b)))
143 collectAllFunctionArgs allArgs f body = (allArgs, body, f)
144 isTailCall :: Ident -> JS -> Bool
145 isTailCall ident js =
147 numSelfCalls = everything (+) (mkQ 0 countSelfCalls) js
148 numSelfCallsInTailPosition = everything (+) (mkQ 0 countSelfCallsInTailPosition) js
149 numSelfCallsUnderFunctions = everything (+) (mkQ 0 countSelfCallsUnderFunctions) js
152 && numSelfCalls == numSelfCallsInTailPosition
153 && numSelfCallsUnderFunctions == 0
155 countSelfCalls :: JS -> Int
156 countSelfCalls (JSApp (JSVar ident') _) | ident == ident' = 1
158 countSelfCallsInTailPosition :: JS -> Int
159 countSelfCallsInTailPosition (JSReturn ret) | isSelfCall ident ret = 1
160 countSelfCallsInTailPosition _ = 0
161 countSelfCallsUnderFunctions (JSFunction _ _ js') = everything (+) (mkQ 0 countSelfCalls) js'
162 countSelfCallsUnderFunctions _ = 0
163 toLoop :: Ident -> [Ident] -> JS -> JS
164 toLoop ident allArgs js = JSBlock
165 [ JSLabel tcoLabel $ JSWhile (JSBooleanLiteral True) (JSBlock [ everywhere (mkT loopify) js ]) ]
168 loopify (JSReturn ret) | isSelfCall ident ret =
170 allArgumentValues = concat $ collectSelfCallArgs [] ret
172 JSBlock $ zipWith (\val arg ->
173 JSVariableIntroduction (tcoVar arg) (Just val)) allArgumentValues allArgs
175 JSAssignment (JSAssignVariable arg) (JSVar (tcoVar arg))) allArgs
176 ++ [ JSContinue tcoLabel ]
177 loopify other = other
178 collectSelfCallArgs :: [[JS]] -> JS -> [[JS]]
179 collectSelfCallArgs allArgumentValues (JSApp fn args') = collectSelfCallArgs (args' : allArgumentValues) fn
180 collectSelfCallArgs allArgumentValues _ = allArgumentValues
181 isSelfCall :: Ident -> JS -> Bool
182 isSelfCall ident (JSApp (JSVar ident') _) | ident == ident' = True
183 isSelfCall ident (JSApp fn _) = isSelfCall ident fn
184 isSelfCall _ _ = False
186collapseNestedBlocks :: JS -> JS
187collapseNestedBlocks = everywhere (mkT collapse)
190 collapse (JSBlock sts) = JSBlock (concatMap go sts)
193 go (JSBlock sts) = sts
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list