git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Add inlining of semigroupoidArr composition

The following program: f :: Boolean -> Boolean f = not <<< not <<< not Is now compiled to: var f = function (_44) { return !!!_44; };

commit82a1f79f62979751f8002a13827b5574c71163b2 authorBrian McKenna <brian@brianmckenna.org> dateMon 20 Apr 2015, 23:03 (12 years ago) parentbc79f06
2 files changed+15−3
src/Language/PureScript/CodeGen/JS/Optimizer.hs+4−3 src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+11
src/Language/PureScript/CodeGen/JS/Optimizer.hs+4−3
@@ -38,6 +38,7 @@ module Language.PureScript.CodeGen.JS.Optimizer (
3838) where
3939
4040import Control.Monad.Reader (MonadReader, ask, asks)
41+import Control.Monad.Supply.Class (MonadSupply)
4142
4243import Language.PureScript.CodeGen.JS.AST
4344import Language.PureScript.Options
@@ -53,15 +54,15 @@ import Language.PureScript.CodeGen.JS.Optimizer.Blocks
5354-- |
5455-- Apply a series of optimizer passes to simplified Javascript code
5556--
56−optimize :: (Monad m, MonadReader (Options mode) m) => JS -> m JS
57+optimize :: (Monad m, MonadReader (Options mode) m, MonadSupply m) => JS -> m JS
5758optimize js = do
5859 noOpt <- asks optionsNoOptimizations
5960 if noOpt then return js else optimize' js
6061
61−optimize' :: (Monad m, MonadReader (Options mode) m) => JS -> m JS
62+optimize' :: (Monad m, MonadReader (Options mode) m, MonadSupply m) => JS -> m JS
6263optimize' js = do
6364 opts <- ask
64− untilFixedPoint (return . applyAll
65+ untilFixedPoint (inlineArrComposition . applyAll
6566 [ collapseNestedBlocks
6667 , collapseNestedIfs
6768 , tco opts
src/Language/PureScript/CodeGen/JS/Optimizer/Inliner.hs+11
@@ -20,11 +20,13 @@ module Language.PureScript.CodeGen.JS.Optimizer.Inliner (
2020 inlineCommonOperators,
2121 inlineAppliedArrComposition,
2222 inlineAppliedVars,
23+ inlineArrComposition,
2324 etaConvert,
2425 unThunk,
2526 evaluateIifes
2627) where
2728
29+import Control.Monad.Supply.Class (MonadSupply, freshName)
2830import Data.Maybe (fromMaybe)
2931
3032import Language.PureScript.CodeGen.JS.AST
@@ -254,6 +256,15 @@ inlineAppliedVars = everywhereOnJS convert
254256 convert (JSApp (JSFunction Nothing [a] (JSBlock [JSReturn b])) [JSVar x]) = replaceIdent a (JSVar x) b
255257 convert other = other
256258
259+inlineArrComposition :: (MonadSupply m) => JS -> m JS
260+inlineArrComposition js = everywhereOnJSTopDownM convert js
261+ where
262+ convert :: (MonadSupply m) => JS -> m JS
263+ convert (JSApp (JSApp (JSApp fn [dict']) [x]) [y]) | isDict semigroupoidArr dict' && isPreludeFn (C.<<<) fn = do
264+ arg <- freshName
265+ return $ JSFunction Nothing [arg] (JSBlock [JSReturn $ JSApp x [JSApp y [JSVar arg]]])
266+ convert other = return other
267+
257268isDict :: (String, String) -> JS -> Bool
258269isDict (moduleName, dictName) (JSAccessor x (JSVar y)) = x == dictName && y == moduleName
259270isDict _ _ = False