git.iko.soy/rowtype-yoga/purescript-native
on 74cdd72 help sign in
74 lines 2.0 kB history raw
1-----------------------------------------------------------------------------
2--
3-- Module : Language.PureScript.Optimizer
4-- Copyright : (c) Phil Freeman 2013
5-- License : MIT
6--
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
9-- Portability :
10--
11-- |
12-- This module optimizes code in the simplified-Javascript intermediate representation.
13--
14-- The following optimizations are supported:
15--
16-- * Collapsing nested blocks
17--
18-- * Tail call elimination
19--
20-- * Inlining of (>>=) and ret for the Eff monad
21--
22-- * Removal of unused variables
23--
24-- * Removal of unnecessary thunks
25--
26-- * Eta conversion
27--
28-- * Inlining variables
29--
30-- * Inline Prelude.($), Prelude.(#), Prelude.(++), Prelude.(!!)
31--
32-- * Inlining primitive Javascript operators
33--
34-----------------------------------------------------------------------------
36module Language.PureScript.Optimizer (
37 optimize
38) where
40import Language.PureScript.CodeGen.JS.AST
41import Language.PureScript.Options
43import Language.PureScript.Optimizer.Common
44import Language.PureScript.Optimizer.TCO
45import Language.PureScript.Optimizer.MagicDo
46import Language.PureScript.Optimizer.Inliner
47import Language.PureScript.Optimizer.Unused
48import Language.PureScript.Optimizer.Blocks
50-- |
51-- Apply a series of optimizer passes to simplified Javascript code
52--
53optimize :: Options -> JS -> JS
54optimize opts | optionsNoOptimizations opts = id
55 | otherwise = untilFixedPoint $ applyAll
56 [ collapseNestedBlocks
57 , tco opts
58 , magicDo opts
59 , removeUnusedVariables
60 , unThunk
61 , etaConvert
62 , inlineVariables
63 , inlineOperator "$" $ \f x -> JSApp f [x]
64 , inlineOperator "#" $ \x f -> JSApp f [x]
65 , inlineOperator "!!" $ flip JSIndexer
66 , inlineOperator "++" $ JSBinary Add
67 , inlineCommonOperators ]
69untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
70untilFixedPoint f = go
71 where
72 go a = let a' = f a in
73 if a' == a then a' else go a'