git.iko.soy/rowtype-yoga/purescript-native
on f8da0b9 help sign in
75 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
42import qualified Language.PureScript.Constants as C
44import Language.PureScript.Optimizer.Common
45import Language.PureScript.Optimizer.TCO
46import Language.PureScript.Optimizer.MagicDo
47import Language.PureScript.Optimizer.Inliner
48import Language.PureScript.Optimizer.Unused
49import Language.PureScript.Optimizer.Blocks
51-- |
52-- Apply a series of optimizer passes to simplified Javascript code
53--
54optimize :: Options -> JS -> JS
55optimize opts | optionsNoOptimizations opts = id
56 | otherwise = untilFixedPoint $ applyAll
57 [ collapseNestedBlocks
58 , tco opts
59 , magicDo opts
60 , removeUnusedVariables
61 , unThunk
62 , etaConvert
63 , inlineVariables
64 , inlineOperator (C.$) $ \f x -> JSApp f [x]
65 , inlineOperator (C.#) $ \x f -> JSApp f [x]
66 , inlineOperator (C.!!) $ flip JSIndexer
67 , inlineOperator (C.++) $ JSBinary Add
68 , inlineCommonOperators ]
70untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
71untilFixedPoint f = go
72 where
73 go a = let a' = f a in
74 if a' == a then a' else go a'