OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library cps_ir.cps_fragment; | 5 library cps_ir.cps_fragment; |
6 | 6 |
7 import 'cps_ir_nodes.dart'; | 7 import 'cps_ir_nodes.dart'; |
8 import '../constants/values.dart'; | 8 import '../constants/values.dart'; |
9 import '../universe/selector.dart' show Selector; | 9 import '../universe/selector.dart' show Selector; |
10 import '../types/types.dart' show TypeMask; | 10 import '../types/types.dart' show TypeMask; |
11 import '../io/source_information.dart'; | 11 import '../io/source_information.dart'; |
12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
13 import 'parent_visitor.dart'; | |
13 | 14 |
14 /// Builds a CPS fragment that can be plugged into another CPS term. | 15 /// Builds a CPS fragment that can be plugged into another CPS term. |
15 /// | 16 /// |
16 /// A CPS fragment contains a CPS term, possibly with a "hole" in it denoting | 17 /// A CPS fragment contains a CPS term, possibly with a "hole" in it denoting |
17 /// where to insert new IR nodes. We say a fragment is "open" if it has such | 18 /// where to insert new IR nodes. We say a fragment is "open" if it has such |
18 /// a hole. Otherwise, the fragment is "closed" and cannot be extended further. | 19 /// a hole. Otherwise, the fragment is "closed" and cannot be extended further. |
19 /// | 20 /// |
20 /// This class is designed for building non-trivial CPS terms in a readable and | 21 /// This class is designed for building non-trivial CPS terms in a readable and |
21 /// non-error prone manner. It is not designed to manipulate existing IR nodes, | 22 /// non-error prone manner. It is not designed to manipulate existing IR nodes, |
22 /// nor is it intended to shield the user from every complexity in the IR. | 23 /// nor is it intended to shield the user from every complexity in the IR. |
(...skipping 23 matching lines...) Expand all Loading... | |
46 /// Continuation loop = cps.beginLoop(); | 47 /// Continuation loop = cps.beginLoop(); |
47 /// var result = cps.invokeMethod(receiver, selector, ...); | 48 /// var result = cps.invokeMethod(receiver, selector, ...); |
48 /// cps.ifFalsy(result).invokeContinuation(exit, []); | 49 /// cps.ifFalsy(result).invokeContinuation(exit, []); |
49 /// cps.continueLoop(loop); | 50 /// cps.continueLoop(loop); |
50 /// | 51 /// |
51 class CpsFragment { | 52 class CpsFragment { |
52 /// The root of the IR built using this fragment. | 53 /// The root of the IR built using this fragment. |
53 Expression root; | 54 Expression root; |
54 | 55 |
55 /// Node whose body is the hole in this CPS fragment. May be null. | 56 /// Node whose body is the hole in this CPS fragment. May be null. |
56 InteriorNode context; | 57 InteriorNode context; |
Kevin Millikin (Google)
2015/10/05 12:04:37
It's probably not worth changing, but this name is
asgerf
2015/10/12 13:18:31
I can't find a name I really like. I also consider
| |
57 | 58 |
58 /// Source information to attach to every IR node created in the fragment. | 59 /// Source information to attach to every IR node created in the fragment. |
59 SourceInformation sourceInformation; | 60 SourceInformation sourceInformation; |
60 | 61 |
61 CpsFragment([this.sourceInformation, this.context]); | 62 CpsFragment([this.sourceInformation, this.context]); |
62 | 63 |
63 bool get isOpen => root == null || context != null; | 64 bool get isOpen => root == null || context != null; |
64 bool get isClosed => !isOpen; | 65 bool get isClosed => !isOpen; |
65 bool get isEmpty => root == null; | 66 bool get isEmpty => root == null; |
66 | 67 |
67 /// Asserts that the fragment is non-empty and closed and returns the IR that | 68 /// Asserts that the fragment is non-empty and closed and returns the IR that |
68 /// was built. | 69 /// was built. |
69 Expression get result { | 70 Expression get result { |
70 assert(!isEmpty); | 71 assert(!isEmpty); |
71 assert(isClosed); | 72 assert(isClosed); |
72 return root; | 73 return root; |
73 } | 74 } |
74 | 75 |
75 /// Put the given expression into the fragment's hole. | 76 /// Put the given expression into the fragment's hole. |
76 /// | 77 /// |
77 /// Afterwards the fragment is closed and cannot be extended until a new | 78 /// Afterwards the fragment is closed and cannot be extended until a new |
78 /// [context] is set. | 79 /// [context] is set. |
79 void put(Expression node) { | 80 void put(Expression node) { |
80 assert(root == null || context != null); // We must put the node somewhere. | 81 assert(root == null || context != null); // We must put the node somewhere. |
81 if (root == null) { | 82 if (root == null) { |
82 root = node; | 83 root = node; |
83 } | 84 } |
84 if (context != null) { | 85 if (context != null) { |
85 context.body = node; | 86 context.body = node; |
87 node.parent = context; | |
86 } | 88 } |
87 context = null; | 89 context = null; |
88 } | 90 } |
89 | 91 |
90 /// Bind a primitive. Returns the same primitive for convenience. | 92 /// Bind a primitive. Returns the same primitive for convenience. |
91 Primitive letPrim(Primitive prim) { | 93 Primitive letPrim(Primitive prim) { |
92 assert(prim != null); | 94 assert(prim != null); |
93 LetPrim let = new LetPrim(prim); | 95 LetPrim let = new LetPrim(prim); |
94 put(let); | 96 put(let); |
95 context = let; | 97 context = let; |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 void setMutable(MutableVariable variable, Primitive value) { | 292 void setMutable(MutableVariable variable, Primitive value) { |
291 letPrim(new SetMutable(variable, value)); | 293 letPrim(new SetMutable(variable, value)); |
292 } | 294 } |
293 | 295 |
294 /// Declare a new mutable variable. | 296 /// Declare a new mutable variable. |
295 void letMutable(MutableVariable variable, Primitive initialValue) { | 297 void letMutable(MutableVariable variable, Primitive initialValue) { |
296 LetMutable let = new LetMutable(variable, initialValue); | 298 LetMutable let = new LetMutable(variable, initialValue); |
297 put(let); | 299 put(let); |
298 context = let; | 300 context = let; |
299 } | 301 } |
302 | |
303 void insertBelow(InteriorNode node) { | |
304 assert(isOpen); | |
305 if (isEmpty) return; | |
306 Expression child = node.body; | |
307 node.body = root; | |
308 root.parent = node; | |
309 context.body = child; | |
310 child.parent = context; | |
311 root = context = null; | |
312 } | |
313 | |
314 void insertAbove(InteriorExpression node) { | |
315 insertBelow(node.parent); | |
316 } | |
300 } | 317 } |
OLD | NEW |