| OLD | NEW |
| 1 library dart2js.unsugar_cps; | 1 library dart2js.unsugar_cps; |
| 2 | 2 |
| 3 import '../../cps_ir/cps_ir_nodes.dart'; | 3 import '../../cps_ir/cps_ir_nodes.dart'; |
| 4 | 4 |
| 5 // TODO(karlklose): share the [ParentVisitor]. | 5 // TODO(karlklose): share the [ParentVisitor]. |
| 6 import '../../cps_ir/optimizers.dart'; | 6 import '../../cps_ir/optimizers.dart'; |
| 7 import '../../constants/expressions.dart'; | 7 import '../../constants/expressions.dart'; |
| 8 import '../../constants/values.dart'; | 8 import '../../constants/values.dart'; |
| 9 import '../../elements/elements.dart' show ClassElement; |
| 10 import '../../js_backend/codegen/glue.dart'; |
| 11 import '../../dart2jslib.dart' show Selector; |
| 9 | 12 |
| 10 /// Rewrites the initial CPS IR to make Dart semantics explicit and inserts | 13 /// Rewrites the initial CPS IR to make Dart semantics explicit and inserts |
| 11 /// special nodes that respect JavaScript behavior. | 14 /// special nodes that respect JavaScript behavior. |
| 12 /// | 15 /// |
| 13 /// Performs the following rewrites: | 16 /// Performs the following rewrites: |
| 14 /// - rewrite [IsTrue] in a [Branch] to do boolean conversion. | 17 /// - rewrite [IsTrue] in a [Branch] to do boolean conversion. |
| 15 class UnsugarVisitor extends RecursiveVisitor { | 18 class UnsugarVisitor extends RecursiveVisitor { |
| 16 const UnsugarVisitor(); | 19 Glue _glue; |
| 20 |
| 21 UnsugarVisitor(this._glue); |
| 17 | 22 |
| 18 void rewrite(FunctionDefinition function) { | 23 void rewrite(FunctionDefinition function) { |
| 19 // Set all parent pointers. | 24 // Set all parent pointers. |
| 20 new ParentVisitor().visit(function); | 25 new ParentVisitor().visit(function); |
| 21 visit(function); | 26 visit(function); |
| 22 } | 27 } |
| 23 | 28 |
| 24 @override | 29 @override |
| 25 visit(Node node) { | 30 visit(Node node) { |
| 26 Node result = node.accept(this); | 31 Node result = node.accept(this); |
| 27 return result != null ? result : node; | 32 return result != null ? result : node; |
| 28 } | 33 } |
| 29 | 34 |
| 30 Constant get trueConstant { | 35 Constant get trueConstant { |
| 31 return new Constant( | 36 return new Constant( |
| 32 new PrimitiveConstantExpression( | 37 new PrimitiveConstantExpression( |
| 33 new TrueConstantValue())); | 38 new TrueConstantValue())); |
| 34 } | 39 } |
| 35 | 40 |
| 41 processInvokeMethod(InvokeMethod node) { |
| 42 Selector selector = node.selector; |
| 43 if (!_glue.isInterceptedSelector(selector)) return; |
| 44 |
| 45 if (!selector.isCall && !selector.isOperator) { |
| 46 // TODO(karlklose): handle special selectors. |
| 47 return; |
| 48 } |
| 49 |
| 50 Set<ClassElement> interceptedClasses = |
| 51 _glue.getInterceptedClassesOn(selector); |
| 52 _glue.registerSpecializedGetInterceptor(interceptedClasses); |
| 53 InteriorNode parent = node.parent; |
| 54 Primitive receiver = node.receiver.definition; |
| 55 Primitive intercepted = new Interceptor(receiver, interceptedClasses); |
| 56 List<Reference<Primitive>> arguments = |
| 57 new List<Reference<Primitive>>.generate(node.arguments.length + 1, |
| 58 (int index) { |
| 59 return index == 0 ? new Reference<Primitive>(receiver) |
| 60 : node.arguments[index - 1]; |
| 61 }); |
| 62 LetPrim newNode = new LetPrim(intercepted, |
| 63 new InvokeMethod.internal(new Reference<Primitive>(intercepted), |
| 64 selector, |
| 65 new Reference<Continuation>(node.continuation.definition), |
| 66 arguments)); |
| 67 node.continuation.unlink(); |
| 68 node.receiver.unlink(); |
| 69 parent.body = newNode; |
| 70 } |
| 71 |
| 36 processBranch(Branch node) { | 72 processBranch(Branch node) { |
| 37 // TODO(karlklose): implement the checked mode part of boolean conversion. | 73 // TODO(karlklose): implement the checked mode part of boolean conversion. |
| 38 InteriorNode parent = node.parent; | 74 InteriorNode parent = node.parent; |
| 39 IsTrue condition = node.condition; | 75 IsTrue condition = node.condition; |
| 40 Primitive t = trueConstant; | 76 Primitive t = trueConstant; |
| 41 Primitive i = new Identical(condition.value.definition, t); | 77 Primitive i = new Identical(condition.value.definition, t); |
| 42 LetPrim newNode = new LetPrim(t, | 78 LetPrim newNode = new LetPrim(t, |
| 43 new LetPrim(i, | 79 new LetPrim(i, |
| 44 new Branch(new IsTrue(i), | 80 new Branch(new IsTrue(i), |
| 45 node.trueContinuation.definition, | 81 node.trueContinuation.definition, |
| 46 node.falseContinuation.definition))); | 82 node.falseContinuation.definition))); |
| 47 condition.value.unlink(); | 83 condition.value.unlink(); |
| 48 node.trueContinuation.unlink(); | 84 node.trueContinuation.unlink(); |
| 49 node.falseContinuation.unlink(); | 85 node.falseContinuation.unlink(); |
| 50 parent.body = newNode; | 86 parent.body = newNode; |
| 51 } | 87 } |
| 52 } | 88 } |
| OLD | NEW |