OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 library dart2js.ir_nodes; | 4 library dart2js.ir_nodes; |
5 | 5 |
| 6 import 'dart:collection'; |
6 import '../constants/values.dart' as values show ConstantValue; | 7 import '../constants/values.dart' as values show ConstantValue; |
7 import '../dart_types.dart' show DartType, InterfaceType, TypeVariableType; | 8 import '../dart_types.dart' show DartType, InterfaceType, TypeVariableType; |
8 import '../elements/elements.dart'; | 9 import '../elements/elements.dart'; |
9 import '../io/source_information.dart' show SourceInformation; | 10 import '../io/source_information.dart' show SourceInformation; |
10 import '../types/types.dart' show TypeMask; | 11 import '../types/types.dart' show TypeMask; |
11 import '../universe/universe.dart' show Selector; | 12 import '../universe/universe.dart' show Selector; |
12 | 13 |
13 import 'builtin_operator.dart'; | 14 import 'builtin_operator.dart'; |
14 export 'builtin_operator.dart'; | 15 export 'builtin_operator.dart'; |
15 | 16 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 previous = current; | 113 previous = current; |
113 current = current.next; | 114 current = current.next; |
114 } while (current != null); | 115 } while (current != null); |
115 previous.next = firstRef; | 116 previous.next = firstRef; |
116 if (firstRef != null) firstRef.previous = previous; | 117 if (firstRef != null) firstRef.previous = previous; |
117 firstRef = other.firstRef; | 118 firstRef = other.firstRef; |
118 other.firstRef = null; | 119 other.firstRef = null; |
119 } | 120 } |
120 } | 121 } |
121 | 122 |
| 123 class EffectiveUseIterator extends Iterator<Reference<Primitive>> { |
| 124 Reference<Primitive> current; |
| 125 Reference<Primitive> next; |
| 126 final List<Refinement> stack = <Refinement>[]; |
| 127 |
| 128 EffectiveUseIterator(Primitive prim) : next = prim.firstRef; |
| 129 |
| 130 bool moveNext() { |
| 131 Reference<Primitive> ref = next; |
| 132 while (true) { |
| 133 if (ref == null) { |
| 134 if (stack.isNotEmpty) { |
| 135 ref = stack.removeLast().firstRef; |
| 136 } else { |
| 137 current = null; |
| 138 return false; |
| 139 } |
| 140 } else if (ref.parent is Refinement) { |
| 141 stack.add(ref.parent); |
| 142 ref = ref.next; |
| 143 } else { |
| 144 current = ref; |
| 145 next = current.next; |
| 146 return true; |
| 147 } |
| 148 } |
| 149 } |
| 150 } |
| 151 |
| 152 class EffectiveUseIterable extends IterableBase<Reference<Primitive>> { |
| 153 Primitive primitive; |
| 154 EffectiveUseIterable(this.primitive); |
| 155 EffectiveUseIterator get iterator => new EffectiveUseIterator(primitive); |
| 156 } |
| 157 |
122 /// A named value. | 158 /// A named value. |
123 /// | 159 /// |
124 /// The identity of the [Primitive] object is the name of the value. | 160 /// The identity of the [Primitive] object is the name of the value. |
125 /// The subclass describes how to compute the value. | 161 /// The subclass describes how to compute the value. |
126 /// | 162 /// |
127 /// All primitives except [Parameter] must be bound by a [LetPrim]. | 163 /// All primitives except [Parameter] must be bound by a [LetPrim]. |
128 abstract class Primitive extends Definition<Primitive> { | 164 abstract class Primitive extends Definition<Primitive> { |
129 /// The [VariableElement] or [ParameterElement] from which the primitive | 165 /// The [VariableElement] or [ParameterElement] from which the primitive |
130 /// binding originated. | 166 /// binding originated. |
131 Entity hint; | 167 Entity hint; |
(...skipping 14 matching lines...) Expand all Loading... |
146 /// observable side-effects. | 182 /// observable side-effects. |
147 bool get isSafeForElimination; | 183 bool get isSafeForElimination; |
148 | 184 |
149 /// True if time-of-evaluation is irrelevant for the given primitive, | 185 /// True if time-of-evaluation is irrelevant for the given primitive, |
150 /// assuming its inputs are the same values. | 186 /// assuming its inputs are the same values. |
151 bool get isSafeForReordering; | 187 bool get isSafeForReordering; |
152 | 188 |
153 /// The source information associated with this primitive. | 189 /// The source information associated with this primitive. |
154 // TODO(johnniwinther): Require source information for all primitives. | 190 // TODO(johnniwinther): Require source information for all primitives. |
155 SourceInformation get sourceInformation => null; | 191 SourceInformation get sourceInformation => null; |
| 192 |
| 193 /// If this is a [Refinement] node, returns the value being refined. |
| 194 Primitive get effectiveDefinition => this; |
| 195 |
| 196 /// True if the two primitives are (refinements of) the same value. |
| 197 bool sameValue(Primitive other) { |
| 198 return effectiveDefinition == other.effectiveDefinition; |
| 199 } |
| 200 |
| 201 /// Iterates all non-refinement uses of the primitive and all uses of |
| 202 /// a [Refinement] of this primitive (transitively). |
| 203 /// |
| 204 /// Notes regarding concurrent modification: |
| 205 /// - The current reference may safely be unlinked. |
| 206 /// - Yet unvisited references may not be unlinked. |
| 207 /// - References to this primitive created during iteration will not be seen. |
| 208 /// - References to a refinement of this primitive may not be created during |
| 209 /// iteration. |
| 210 EffectiveUseIterable get effectiveUses => new EffectiveUseIterable(this); |
| 211 |
| 212 bool get hasMultipleEffectiveUses { |
| 213 Iterator it = effectiveUses.iterator; |
| 214 return it.moveNext() && it.moveNext(); |
| 215 } |
| 216 |
| 217 bool get hasNoEffectiveUses { |
| 218 return effectiveUses.isEmpty; |
| 219 } |
156 } | 220 } |
157 | 221 |
158 /// Operands to invocations and primitives are always variables. They point to | 222 /// Operands to invocations and primitives are always variables. They point to |
159 /// their definition and are doubly-linked into a list of occurrences. | 223 /// their definition and are doubly-linked into a list of occurrences. |
160 class Reference<T extends Definition<T>> { | 224 class Reference<T extends Definition<T>> { |
161 T definition; | 225 T definition; |
162 Reference<T> previous; | 226 Reference<T> previous; |
163 Reference<T> next; | 227 Reference<T> next; |
164 | 228 |
165 /// A pointer to the parent node. Is null until set by optimization passes. | 229 /// A pointer to the parent node. Is null until set by optimization passes. |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
432 this.selector, | 496 this.selector, |
433 List<Primitive> args, | 497 List<Primitive> args, |
434 Continuation cont, | 498 Continuation cont, |
435 this.sourceInformation) | 499 this.sourceInformation) |
436 : arguments = _referenceList(args), | 500 : arguments = _referenceList(args), |
437 continuation = new Reference<Continuation>(cont); | 501 continuation = new Reference<Continuation>(cont); |
438 | 502 |
439 accept(Visitor visitor) => visitor.visitInvokeConstructor(this); | 503 accept(Visitor visitor) => visitor.visitInvokeConstructor(this); |
440 } | 504 } |
441 | 505 |
| 506 /// An alias for [value] in a context where the value is known to satisfy |
| 507 /// [type]. |
| 508 /// |
| 509 /// Refinement nodes are inserted before the type propagator pass and removed |
| 510 /// afterwards, so as not to complicate passes that don't reason about types, |
| 511 /// but need to reason about value references being identical (i.e. referring |
| 512 /// to the same primitive). |
| 513 class Refinement extends Primitive { |
| 514 Reference<Primitive> value; |
| 515 final TypeMask type; |
| 516 |
| 517 Refinement(Primitive value, this.type) |
| 518 : value = new Reference<Primitive>(value); |
| 519 |
| 520 bool get isSafeForElimination => true; |
| 521 bool get isSafeForReordering => false; |
| 522 |
| 523 accept(Visitor visitor) => visitor.visitRefinement(this); |
| 524 |
| 525 Primitive get effectiveDefinition => value.definition.effectiveDefinition; |
| 526 } |
| 527 |
442 /// An "is" type test. | 528 /// An "is" type test. |
443 /// | 529 /// |
444 /// Returns `true` if [value] is an instance of [type]. | 530 /// Returns `true` if [value] is an instance of [type]. |
445 /// | 531 /// |
446 /// [type] must not be the [Object], `dynamic` or [Null] types (though it might | 532 /// [type] must not be the [Object], `dynamic` or [Null] types (though it might |
447 /// be a type variable containing one of these types). This design is chosen | 533 /// be a type variable containing one of these types). This design is chosen |
448 /// to simplify code generation for type tests. | 534 /// to simplify code generation for type tests. |
449 class TypeTest extends Primitive { | 535 class TypeTest extends Primitive { |
450 Reference<Primitive> value; | 536 Reference<Primitive> value; |
451 final DartType type; | 537 final DartType type; |
(...skipping 742 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1194 T visitReifyRuntimeType(ReifyRuntimeType node); | 1280 T visitReifyRuntimeType(ReifyRuntimeType node); |
1195 T visitReadTypeVariable(ReadTypeVariable node); | 1281 T visitReadTypeVariable(ReadTypeVariable node); |
1196 T visitTypeExpression(TypeExpression node); | 1282 T visitTypeExpression(TypeExpression node); |
1197 T visitCreateInvocationMirror(CreateInvocationMirror node); | 1283 T visitCreateInvocationMirror(CreateInvocationMirror node); |
1198 T visitTypeTest(TypeTest node); | 1284 T visitTypeTest(TypeTest node); |
1199 T visitApplyBuiltinOperator(ApplyBuiltinOperator node); | 1285 T visitApplyBuiltinOperator(ApplyBuiltinOperator node); |
1200 T visitApplyBuiltinMethod(ApplyBuiltinMethod node); | 1286 T visitApplyBuiltinMethod(ApplyBuiltinMethod node); |
1201 T visitGetLength(GetLength node); | 1287 T visitGetLength(GetLength node); |
1202 T visitGetIndex(GetIndex node); | 1288 T visitGetIndex(GetIndex node); |
1203 T visitSetIndex(SetIndex node); | 1289 T visitSetIndex(SetIndex node); |
| 1290 T visitRefinement(Refinement node); |
1204 | 1291 |
1205 // Support for literal foreign code. | 1292 // Support for literal foreign code. |
1206 T visitForeignCode(ForeignCode node); | 1293 T visitForeignCode(ForeignCode node); |
1207 } | 1294 } |
1208 | 1295 |
1209 /// Visits all non-recursive children of a CPS term, i.e. anything | 1296 /// Visits all non-recursive children of a CPS term, i.e. anything |
1210 /// not of type [Expression] or [Continuation]. | 1297 /// not of type [Expression] or [Continuation]. |
1211 /// | 1298 /// |
1212 /// The `process*` methods are called in pre-order for every node visited. | 1299 /// The `process*` methods are called in pre-order for every node visited. |
1213 /// These can be overridden without disrupting the visitor traversal. | 1300 /// These can be overridden without disrupting the visitor traversal. |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1497 processReference(node.index); | 1584 processReference(node.index); |
1498 } | 1585 } |
1499 | 1586 |
1500 processSetIndex(SetIndex node) {} | 1587 processSetIndex(SetIndex node) {} |
1501 visitSetIndex(SetIndex node) { | 1588 visitSetIndex(SetIndex node) { |
1502 processSetIndex(node); | 1589 processSetIndex(node); |
1503 processReference(node.object); | 1590 processReference(node.object); |
1504 processReference(node.index); | 1591 processReference(node.index); |
1505 processReference(node.value); | 1592 processReference(node.value); |
1506 } | 1593 } |
| 1594 |
| 1595 processRefinement(Refinement node) {} |
| 1596 visitRefinement(Refinement node) { |
| 1597 processRefinement(node); |
| 1598 processReference(node.value); |
| 1599 } |
1507 } | 1600 } |
1508 | 1601 |
1509 typedef void StackAction(); | 1602 typedef void StackAction(); |
1510 | 1603 |
1511 /// Calls `process*` for all nodes in a tree. | 1604 /// Calls `process*` for all nodes in a tree. |
1512 /// For simple usage, only override the `process*` methods. | 1605 /// For simple usage, only override the `process*` methods. |
1513 /// | 1606 /// |
1514 /// To avoid deep recursion, this class uses an "action stack" containing | 1607 /// To avoid deep recursion, this class uses an "action stack" containing |
1515 /// callbacks to be invoked after the processing of some term has finished. | 1608 /// callbacks to be invoked after the processing of some term has finished. |
1516 /// | 1609 /// |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1628 /// Visit a just-deleted subterm and unlink all [Reference]s in it. | 1721 /// Visit a just-deleted subterm and unlink all [Reference]s in it. |
1629 class RemovalVisitor extends RecursiveVisitor { | 1722 class RemovalVisitor extends RecursiveVisitor { |
1630 processReference(Reference reference) { | 1723 processReference(Reference reference) { |
1631 reference.unlink(); | 1724 reference.unlink(); |
1632 } | 1725 } |
1633 | 1726 |
1634 static void remove(Node node) { | 1727 static void remove(Node node) { |
1635 (new RemovalVisitor()).visit(node); | 1728 (new RemovalVisitor()).visit(node); |
1636 } | 1729 } |
1637 } | 1730 } |
OLD | NEW |