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"
124 copyVar :: Ident -> Ident
125 copyVar (Ident arg) = Ident $ "__copy_" ++ arg
126 copyVar _ = error "Invalid name in copyVar"
128 convert js@(JSVariableIntroduction name (Just fn@(JSFunction Nothing _ _))) =
130 (argss, body', replace) = collectAllFunctionArgs [] id fn
132 _ | isTailCall name body' ->
134 allArgs = reverse $ concat argss
136 JSVariableIntroduction name (Just (replace (toLoop name allArgs body')))
139 collectAllFunctionArgs :: [[Ident]] -> (JS -> JS) -> JS -> ([[Ident]], JS, JS -> JS)
140 collectAllFunctionArgs allArgs f (JSFunction Nothing args (JSBlock (body@(JSReturn _):_))) =
141 collectAllFunctionArgs (args : allArgs) (\b -> f (JSFunction Nothing (map copyVar args) (JSBlock [b]))) body
142 collectAllFunctionArgs allArgs f (JSReturn (JSFunction Nothing args (JSBlock [body]))) =
143 collectAllFunctionArgs (args : allArgs) (\b -> f (JSReturn (JSFunction Nothing (map copyVar args) (JSBlock [b])))) body
144 collectAllFunctionArgs allArgs f (JSReturn (JSFunction Nothing args body@(JSBlock _))) =
145 (args : allArgs, body, \b -> f (JSReturn (JSFunction Nothing (map copyVar args) b)))
146 collectAllFunctionArgs allArgs f body = (allArgs, body, f)
147 isTailCall :: Ident -> JS -> Bool
148 isTailCall ident js =
150 numSelfCalls = everything (+) (mkQ 0 countSelfCalls) js
151 numSelfCallsInTailPosition = everything (+) (mkQ 0 countSelfCallsInTailPosition) js
152 numSelfCallsUnderFunctions = everything (+) (mkQ 0 countSelfCallsUnderFunctions) js
155 && numSelfCalls == numSelfCallsInTailPosition
156 && numSelfCallsUnderFunctions == 0
158 countSelfCalls :: JS -> Int
159 countSelfCalls (JSApp (JSVar ident') _) | ident == ident' = 1
161 countSelfCallsInTailPosition :: JS -> Int
162 countSelfCallsInTailPosition (JSReturn ret) | isSelfCall ident ret = 1
163 countSelfCallsInTailPosition _ = 0
164 countSelfCallsUnderFunctions (JSFunction _ _ js') = everything (+) (mkQ 0 countSelfCalls) js'
165 countSelfCallsUnderFunctions _ = 0
166 toLoop :: Ident -> [Ident] -> JS -> JS
167 toLoop ident allArgs js = JSBlock $
168 map (\arg -> JSVariableIntroduction arg (Just (JSVar (copyVar arg)))) allArgs ++
169 [ JSLabel tcoLabel $ JSWhile (JSBooleanLiteral True) (JSBlock [ everywhere (mkT loopify) js ]) ]
172 loopify (JSReturn ret) | isSelfCall ident ret =
174 allArgumentValues = concat $ collectSelfCallArgs [] ret
176 JSBlock $ zipWith (\val arg ->
177 JSVariableIntroduction (tcoVar arg) (Just val)) allArgumentValues allArgs
179 JSAssignment (JSAssignVariable arg) (JSVar (tcoVar arg))) allArgs
180 ++ [ JSContinue tcoLabel ]
181 loopify other = other
182 collectSelfCallArgs :: [[JS]] -> JS -> [[JS]]
183 collectSelfCallArgs allArgumentValues (JSApp fn args') = collectSelfCallArgs (args' : allArgumentValues) fn
184 collectSelfCallArgs allArgumentValues _ = allArgumentValues
185 isSelfCall :: Ident -> JS -> Bool
186 isSelfCall ident (JSApp (JSVar ident') _) | ident == ident' = True
187 isSelfCall ident (JSApp fn _) = isSelfCall ident fn
188 isSelfCall _ _ = False
190collapseNestedBlocks :: JS -> JS
191collapseNestedBlocks = everywhere (mkT collapse)
194 collapse (JSBlock sts) = JSBlock (concatMap go sts)
197 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