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
28optimize opts = removeUnusedVariables . unThunk . etaConvert . inlineVariables . tco opts
30replaceIdent :: (Data d) => Ident -> JS -> d -> d
31replaceIdent var1 js = everywhere (mkT replace)
33 replace (JSVar var2) | var1 == var2 = js
36replaceIdents :: (Data d) => [(Ident, JS)] -> d -> d
37replaceIdents vars = everywhere (mkT replace)
39 replace v@(JSVar var) = fromMaybe v $ lookup var vars
42isReassigned :: (Data d) => Ident -> d -> Bool
43isReassigned var1 = everything (||) (mkQ False check)
46 check (JSAssignment (JSAssignVariable var2) _) | var1 == var2 = True
49isUsed :: (Data d) => Ident -> d -> Bool
50isUsed var1 = everything (||) (mkQ False check)
53 check (JSVar var2) | var1 == var2 = True
54 check (JSAssignment target _) | var1 == targetVariable target = True
56 targetVariable :: JSAssignment -> Ident
57 targetVariable (JSAssignVariable var) = var
58 targetVariable (JSAssignProperty _ tgt) = targetVariable tgt
60shouldInline :: JS -> Bool
61shouldInline (JSVar _) = True
62shouldInline (JSNumericLiteral _) = True
63shouldInline (JSStringLiteral _) = True
64shouldInline (JSBooleanLiteral _) = True
65shouldInline (JSAccessor _ val) = shouldInline val
66shouldInline (JSIndexer index val) = shouldInline index && shouldInline val
69inlineVariables :: JS -> JS
70inlineVariables = everywhere (mkT removeFromBlock)
72 removeFromBlock :: JS -> JS
73 removeFromBlock (JSBlock sts) = JSBlock (go sts)
74 removeFromBlock js = js
77 go (JSVariableIntroduction var (Just js) : sts) | shouldInline js && not (isReassigned var sts) = go (replaceIdent var js sts)
78 go (s:sts) = s : go sts
80removeUnusedVariables :: JS -> JS
81removeUnusedVariables = everywhere (mkT removeFromBlock)
83 removeFromBlock :: JS -> JS
84 removeFromBlock (JSBlock sts) = JSBlock (go sts)
85 removeFromBlock js = js
88 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
89 go (s:sts) = s : go sts
92etaConvert = everywhere (mkT convert)
95 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing idents (JSBlock body)) args)])
96 | all shouldInline args = JSBlock (replaceIdents (zip idents args) body)
100unThunk = everywhere (mkT convert)
103 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
106tco :: Options -> JS -> JS
107tco opts | optionsTco opts = tco'
111tco' = everywhere (mkT convert)
116 tcoRet = Ident "__tco_ret"
117 tcoVar :: Ident -> Ident
118 tcoVar (Ident arg) = Ident $ "__tco_" ++ arg
119 tcoVar _ = error "Invalid name in tcoVar"
121 convert (JSVariableIntroduction name (Just fn@(JSFunction Nothing _ body))) | isTailCall name body =
123 (argss, body', replace) = collectAllFunctionArgs [] id fn
124 allArgs = reverse $ concat argss
126 JSVariableIntroduction name (Just (replace (toLoop name allArgs body')))
128 collectAllFunctionArgs :: [[Ident]] -> (JS -> JS) -> JS -> ([[Ident]], JS, JS -> JS)
129 collectAllFunctionArgs allArgs f (JSFunction Nothing args (JSBlock [body])) =
130 collectAllFunctionArgs (args : allArgs) (\b -> f (JSFunction Nothing args (JSBlock [b]))) body
131 collectAllFunctionArgs allArgs f (JSReturn (JSFunction Nothing args (JSBlock [body]))) =
132 collectAllFunctionArgs (args : allArgs) (\b -> f (JSReturn (JSFunction Nothing args (JSBlock [b])))) body
133 collectAllFunctionArgs allArgs f body = (allArgs, body, f)
134 isTailCall :: Ident -> JS -> Bool
135 isTailCall ident js =
137 numSelfCalls = everything (+) (mkQ 0 countSelfCalls) js
138 numSelfCallsInTailPosition = everything (+) (mkQ 0 countSelfCallsInTailPosition) js
140 numSelfCalls > 0 && numSelfCalls == numSelfCallsInTailPosition
142 countSelfCalls :: JS -> Int
143 countSelfCalls (JSApp (JSVar ident') _) | ident == ident' = 1
145 countSelfCallsInTailPosition :: JS -> Int
146 countSelfCallsInTailPosition (JSReturn ret) | isSelfCall ident ret = 1
147 countSelfCallsInTailPosition _ = 0
148 toLoop :: Ident -> [Ident] -> JS -> JS
149 toLoop ident allArgs js = JSBlock
150 [ JSVariableIntroduction tcoRet Nothing
151 , JSLabel tcoLabel $ JSWhile (JSBooleanLiteral True) (JSBlock [ everywhere (mkT loopify) js ]) ]
154 loopify (JSReturn ret) | isSelfCall ident ret =
156 allArgumentValues = concat $ collectSelfCallArgs [] ret
158 JSBlock $ zipWith (\val arg ->
159 JSVariableIntroduction (tcoVar arg) (Just val)) allArgumentValues allArgs
161 JSAssignment (JSAssignVariable arg) (JSVar (tcoVar arg))) allArgs
162 ++ [ JSContinue tcoLabel ]
163 loopify other = other
164 collectSelfCallArgs :: [[JS]] -> JS -> [[JS]]
165 collectSelfCallArgs allArgumentValues (JSApp fn args') = collectSelfCallArgs (args' : allArgumentValues) fn
166 collectSelfCallArgs allArgumentValues _ = allArgumentValues
167 isSelfCall :: Ident -> JS -> Bool
168 isSelfCall ident (JSApp (JSVar ident') _) | ident == ident' = True
169 isSelfCall ident (JSApp fn _) = isSelfCall ident fn
170 isSelfCall _ _ = False
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