@@ -1,5 +1,5 @@
1{-# OPTIONS_GHC -Wall #-}
1{-# OPTIONS_GHC -Wall #-}
2−module AST.Expression.Optimized
2+module AST.Optimized
@@ -14,7 +14,6 @@ import Data.Binary
14import qualified Data.Set as Set
14import qualified Data.Set as Set
15import Data.Text (Text)
15import Data.Text (Text)
17−import qualified AST.Effects as Effects
18import qualified AST.Literal as Literal
17import qualified AST.Literal as Literal
19import qualified AST.Module.Name as ModuleName
18import qualified AST.Module.Name as ModuleName
20import qualified AST.Variable as Var
19import qualified AST.Variable as Var
@@ -23,16 +22,31 @@ import qualified Reporting.Region as R
26−-- TOP LEVEL DECLARATIONS
25+-- EXPRESSIONS
29−data Decl =
28+data Expr
30− Decl
29+ = Literal Literal.Literal
31− { _direct :: Set.Set Var.Global
31+ | VarGlobal Var.Global
32− , _indirect :: Set.Set Var.Global
32+ | List [Expr]
33− , _effects :: Maybe Effects.ManagerType
33+ | Binop Var.Global Expr Expr
34− , _body :: !Def
34+ | Function [Text] Expr
35− }
35+ | Call Expr [Expr]
36+ | TailCall Text [Text] [Expr]
37+ | If [(Expr, Expr)] Expr
38+ | Let [(Text, Def)] Expr
39+ | Case Text (Decider Choice) [(Int, Expr)]
41+ | CtorAccess Expr Int
44+ | Update Expr [(Text, Expr)]
45+ | Record [(Text, Expr)]
47+ | Tuple Expr Expr [Expr]
49+ | Crash ModuleName.Canonical R.Region (Maybe Expr)
@@ -45,90 +59,86 @@ data Def
48−-- EXPRESSIONS
62+-- BRANCHING
52− = Literal Literal.Literal
54− | VarGlobal Var.Global
56− | Binop Var.Global Expr Expr
57− | Function [Text] Expr
59− | TailCall Text [Text] [Expr]
60− | If [(Expr, Expr)] Expr
61− | Let [(Text, Def)] Expr
62− | Case Text (Decider Choice) [(Int, Expr)]
64− | CtorAccess Expr Int
66− | Update Expr [(Text, Expr)]
67− | Record [(Text, Expr)]
68− | Cmd ModuleName.Canonical Effects.ManagerType
69− | Sub ModuleName.Canonical Effects.ManagerType
70− | OutgoingPort Text Expr
71− | IncomingPort Text Expr
74− | Crash ModuleName.Canonical R.Region (Maybe Expr)
82data Decider a
65data Decider a
85− { _testChain :: [(DT.Path, DT.Test)]
68+ { _testChain :: [(DT.Path, DT.Test)]
86− , _success :: Decider a
69+ , _success :: Decider a
87− , _failure :: Decider a
70+ , _failure :: Decider a
90− { _path :: DT.Path
73+ { _path :: DT.Path
91− , _tests :: [(DT.Test, Decider a)]
74+ , _tests :: [(DT.Test, Decider a)]
92− , _fallback :: Decider a
75+ , _fallback :: Decider a
94− deriving (Eq)
77+ deriving (Eq)
97data Choice
80data Choice
98− = Inline Expr
81+ = Inline Expr
99− | Jump Int
82+ | Jump Int
103−-- BINARY
86+-- OBJECT GRAPH
106−instance Binary Decl where
89+data Graph =
108− liftM4 Decl get get get get
91+ { _nodes :: Map.Map Var.Global Node
110− put (Decl direct indirect fx body) =
111− put direct >> put indirect >> put fx >> put body
114−instance Binary Def where
96+data Node
116− do word <- getWord8
98+ { _deps :: [Var.Global]
117− case word of
99+ , _body :: Expr
118− 0 -> Def <$> get
100+ }
119− 1 -> TailDef <$> get <*> get
101+ | TailCall
120− _ -> error "problem getting Opt.Def binary"
102+ { _deps :: [Var.Global]
108+ { _deps :: [Var.Global]
109+ , _values :: [(Var.Global, Expr)]
125− putWord8 0 >> put body
127− TailDef args body ->
114+-- BINARY
128− putWord8 1 >> put args >> put body
131instance Binary Expr where
117instance Binary Expr where
120+ Literal a -> putWord8 0 >> put a
121+ VarLocal a -> putWord8 1 >> put a
122+ VarGlobal a -> putWord8 2 >> put a
123+ List a -> putWord8 3 >> put a
124+ Binop a b c -> putWord8 4 >> put a >> put b >> put c
125+ Function a b -> putWord8 5 >> put a >> put b
126+ Call a b -> putWord8 6 >> put a >> put b
127+ TailCall a b c -> putWord8 7 >> put a >> put b >> put c
128+ If a b -> putWord8 8 >> put a >> put b
129+ Let a b -> putWord8 9 >> put a >> put b
130+ Case a b c -> putWord8 10 >> put a >> put b >> put c
131+ Ctor a b -> putWord8 11 >> put a >> put b
132+ CtorAccess a b -> putWord8 12 >> put a >> put b
133+ Accessor a -> putWord8 13 >> put a
134+ Access a b -> putWord8 14 >> put a >> put b
135+ Update a b -> putWord8 15 >> put a >> put b
136+ Record a -> putWord8 16 >> put a
138+ Tuple a b c -> putWord8 18 >> put a >> put b >> put c
139+ GLShader a -> putWord8 19 >> put a
140+ Crash a b c -> putWord8 20 >> put a >> put b >> put c
133 do word <- getWord8
143 do word <- getWord8
134 case word of
144 case word of
@@ -145,43 +155,37 @@ instance Binary Expr where
145 10 -> liftM3 Case get get get
155 10 -> liftM3 Case get get get
146 11 -> liftM2 Ctor get get
156 11 -> liftM2 Ctor get get
147 12 -> liftM2 CtorAccess get get
157 12 -> liftM2 CtorAccess get get
158+ 13 -> liftM Accessor get
148− 13 -> liftM2 Access get get
159+ 14 -> liftM2 Access get get
149− 14 -> liftM2 Update get get
160+ 15 -> liftM2 Update get get
150− 15 -> liftM Record get
161+ 16 -> liftM Record get
151− 16 -> liftM2 Cmd get get
162+ 17 -> pure Unit
152− 17 -> liftM2 Sub get get
163+ 18 -> liftM3 Tuple get get get
153− 18 -> liftM2 OutgoingPort get get
164+ 19 -> liftM GLShader get
154− 19 -> liftM2 IncomingPort get get
155− 20 -> liftM2 Program get get
156− 21 -> liftM GLShader get
157− 22 -> liftM3 Crash get get get
165+ 20 -> liftM3 Crash get get get
158 _ -> error "problem getting Opt.Expr binary"
166 _ -> error "problem getting Opt.Expr binary"
169+instance Binary Decl where
170+ put (Decl a b c d) =
171+ put a >> put b >> put c >> put d
174+ liftM4 Decl get get get get
177+instance Binary Def where
161− case expr of
179+ case def of
162− Literal a -> putWord8 0 >> put a
180+ Def body -> putWord8 0 >> put body
163− VarLocal a -> putWord8 1 >> put a
181+ TailDef args body -> putWord8 1 >> put args >> put body
164− VarGlobal a -> putWord8 2 >> put a
182+
165− List a -> putWord8 3 >> put a
183+ get =
166− Binop a b c -> putWord8 4 >> put a >> put b >> put c
184+ do word <- getWord8
167− Function a b -> putWord8 5 >> put a >> put b
185+ case word of
168− Call a b -> putWord8 6 >> put a >> put b
186+ 0 -> Def <$> get
169− TailCall a b c -> putWord8 7 >> put a >> put b >> put c
187+ 1 -> TailDef <$> get <*> get
170− If a b -> putWord8 8 >> put a >> put b
188+ _ -> error "problem getting Opt.Def binary"
171− Let a b -> putWord8 9 >> put a >> put b
172− Case a b c -> putWord8 10 >> put a >> put b >> put c
173− Ctor a b -> putWord8 11 >> put a >> put b
174− CtorAccess a b -> putWord8 12 >> put a >> put b
175− Access a b -> putWord8 13 >> put a >> put b
176− Update a b -> putWord8 14 >> put a >> put b
177− Record a -> putWord8 15 >> put a
178− Cmd a b -> putWord8 16 >> put a >> put b
179− Sub a b -> putWord8 17 >> put a >> put b
180− OutgoingPort a b -> putWord8 18 >> put a >> put b
181− IncomingPort a b -> putWord8 19 >> put a >> put b
182− Program a b -> putWord8 20 >> put a >> put b
183− GLShader a -> putWord8 21 >> put a
184− Crash a b c -> putWord8 22 >> put a >> put b >> put c
187instance Binary Main where
191instance Binary Main where
@@ -199,6 +203,12 @@ instance Binary Main where
201instance (Binary a) => Binary (Decider a) where
205instance (Binary a) => Binary (Decider a) where
208+ Leaf a -> putWord8 0 >> put a
209+ Chain a b c -> putWord8 1 >> put a >> put b >> put c
210+ FanOut a b c -> putWord8 2 >> put a >> put b >> put c
203 do word <- getWord8
213 do word <- getWord8
204 case word of
214 case word of
@@ -207,30 +217,12 @@ instance (Binary a) => Binary (Decider a) where
207 2 -> liftM3 FanOut get get get
217 2 -> liftM3 FanOut get get get
208 _ -> error "problem getting Opt.Decider binary"
218 _ -> error "problem getting Opt.Decider binary"
216− Chain testChain success failure ->
222− FanOut path tests fallback ->
229instance Binary Choice where
221instance Binary Choice where
230 put choice =
222 put choice =
231 case choice of
223 case choice of
232 Inline expr -> putWord8 0 >> put expr
224 Inline expr -> putWord8 0 >> put expr
233− Jump index -> putWord8 1 >> put index
225+ Jump index -> putWord8 1 >> put index
236 do word <- getWord8
228 do word <- getWord8
@@ -238,4 +230,3 @@ instance Binary Choice where
238 0 -> liftM Inline get
230 0 -> liftM Inline get
239 1 -> liftM Jump get
231 1 -> liftM Jump get
240 _ -> error "problem getting Opt.Choice binary"
232 _ -> error "problem getting Opt.Choice binary"