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, FieldElement, Element; | 9 import '../../elements/elements.dart' |
10 show ClassElement, FieldElement, FunctionElement, Element; | |
10 import '../../js_backend/codegen/glue.dart'; | 11 import '../../js_backend/codegen/glue.dart'; |
11 import '../../dart2jslib.dart' show Selector, World; | 12 import '../../dart2jslib.dart' show Selector, World; |
12 | 13 |
13 /// Rewrites the initial CPS IR to make Dart semantics explicit and inserts | 14 /// Rewrites the initial CPS IR to make Dart semantics explicit and inserts |
14 /// special nodes that respect JavaScript behavior. | 15 /// special nodes that respect JavaScript behavior. |
15 /// | 16 /// |
16 /// Performs the following rewrites: | 17 /// Performs the following rewrites: |
17 /// - rewrite [IsTrue] in a [Branch] to do boolean conversion. | 18 /// - rewrite [IsTrue] in a [Branch] to do boolean conversion. |
asgerf
2015/04/14 12:59:17
Stale doc comment.
Kevin Millikin (Google)
2015/04/15 07:52:22
Thanks, updated.
| |
18 class UnsugarVisitor extends RecursiveVisitor { | 19 class UnsugarVisitor extends RecursiveVisitor { |
19 Glue _glue; | 20 Glue _glue; |
21 ParentVisitor _parentVisitor = new ParentVisitor(); | |
20 | 22 |
21 UnsugarVisitor(this._glue); | 23 UnsugarVisitor(this._glue); |
22 | 24 |
23 void rewrite(FunctionDefinition function) { | 25 void rewrite(FunctionDefinition function) { |
24 // Set all parent pointers. | 26 // Set all parent pointers. |
25 new ParentVisitor().visit(function); | 27 _parentVisitor.visit(function); |
26 visit(function); | 28 visit(function); |
27 } | 29 } |
28 | 30 |
29 @override | 31 @override |
30 visit(Node node) { | 32 visit(Node node) { |
31 Node result = node.accept(this); | 33 Node result = node.accept(this); |
32 return result != null ? result : node; | 34 return result != null ? result : node; |
33 } | 35 } |
34 | 36 |
35 Constant get trueConstant { | 37 Constant get trueConstant { |
36 return new Constant( | 38 return new Constant( |
37 new PrimitiveConstantExpression( | 39 new PrimitiveConstantExpression( |
38 new TrueConstantValue())); | 40 new TrueConstantValue())); |
39 } | 41 } |
40 | 42 |
41 void insertLetPrim(Primitive primitive, Expression node) { | 43 void insertLetPrim(Primitive primitive, Expression node) { |
42 LetPrim let = new LetPrim(primitive); | 44 LetPrim let = new LetPrim(primitive); |
43 InteriorNode parent = node.parent; | 45 InteriorNode parent = node.parent; |
44 parent.body = let; | 46 parent.body = let; |
45 let.body = node; | 47 let.body = node; |
46 node.parent = let; | 48 node.parent = let; |
47 let.parent = parent; | 49 let.parent = parent; |
48 } | 50 } |
49 | 51 |
52 /// Insert a static call to [function] at the point of [node] with result | |
53 /// [result]. | |
54 /// | |
55 /// Rewrite [node] to | |
56 /// | |
57 /// let cont continuation(result) = node | |
58 /// in invoke function arguments continuation | |
59 /// | |
60 /// The result parameter passed to this function because a common pattern is | |
61 /// to replace all uses of one of the arguments with the result. To do that, | |
62 /// create a result parameter first, replace all uses of the arguments with | |
63 /// the result, and then insert the call with this function. | |
asgerf
2015/04/14 12:59:17
The result parameter *is* passed ...
I don't see
Kevin Millikin (Google)
2015/04/15 07:52:22
Fair enough, I'll just remove that bit of the comm
| |
64 void insertStaticCall(FunctionElement function, List<Primitive> arguments, | |
65 Parameter result, | |
66 Expression node) { | |
67 InteriorNode parent = node.parent; | |
68 Continuation continuation = new Continuation([result]); | |
69 continuation.body = node; | |
70 _parentVisitor.processContinuation(continuation); | |
71 | |
72 Selector selector = new Selector.fromElement(function); | |
73 // TODO(johnniwinther): Come up with an implementation of SourceInformation | |
74 // for calls such as this one that don't appear in the original source. | |
75 InvokeStatic invoke = | |
76 new InvokeStatic(function, selector, continuation, arguments, null); | |
77 _parentVisitor.processInvokeStatic(invoke); | |
78 | |
79 LetCont letCont = new LetCont(continuation, invoke); | |
80 _parentVisitor.processLetCont(letCont); | |
81 | |
82 parent.body = letCont; | |
83 letCont.parent = parent; | |
84 } | |
85 | |
86 processLetHandler(LetHandler node) { | |
87 // BEFORE: Handlers have two parameters, exception and stack trace. | |
88 // AFTER: Handlers have a single parameter, which is unwrapped to get | |
89 // the exception and stack trace. | |
90 assert(node.handler.parameters.length == 2); | |
91 Parameter exceptionParameter = node.handler.parameters.first; | |
92 Parameter stackTraceParameter = node.handler.parameters.last; | |
93 Expression body = node.handler.body; | |
94 if (exceptionParameter.hasAtLeastOneUse || | |
95 stackTraceParameter.hasAtLeastOneUse) { | |
96 Parameter exceptionValue = new Parameter(null); | |
97 exceptionValue.substituteFor(exceptionParameter); | |
98 insertStaticCall(_glue.getExceptionUnwrapper(), [exceptionParameter], | |
99 exceptionValue, body); | |
100 | |
101 if (stackTraceParameter.hasAtLeastOneUse) { | |
102 Parameter stackTraceValue = new Parameter(null); | |
103 stackTraceValue.substituteFor(stackTraceParameter); | |
104 insertStaticCall(_glue.getTraceFromException(), [exceptionValue], | |
105 stackTraceValue, body); | |
106 } | |
107 } | |
108 | |
109 assert(stackTraceParameter.hasNoUses); | |
110 node.handler.parameters.removeLast(); | |
111 } | |
112 | |
50 processInvokeMethod(InvokeMethod node) { | 113 processInvokeMethod(InvokeMethod node) { |
51 Selector selector = node.selector; | 114 Selector selector = node.selector; |
52 // TODO(karlklose): should we rewrite all selectors? | 115 // TODO(karlklose): should we rewrite all selectors? |
53 if (!_glue.isInterceptedSelector(selector)) return; | 116 if (!_glue.isInterceptedSelector(selector)) return; |
54 | 117 |
55 Primitive receiver = node.receiver.definition; | 118 Primitive receiver = node.receiver.definition; |
56 Set<ClassElement> interceptedClasses = | 119 Set<ClassElement> interceptedClasses = |
57 _glue.getInterceptedClassesOn(selector); | 120 _glue.getInterceptedClassesOn(selector); |
58 _glue.registerSpecializedGetInterceptor(interceptedClasses); | 121 _glue.registerSpecializedGetInterceptor(interceptedClasses); |
59 | 122 |
(...skipping 29 matching lines...) Expand all Loading... | |
89 new LetPrim(i, | 152 new LetPrim(i, |
90 new Branch(new IsTrue(i), | 153 new Branch(new IsTrue(i), |
91 node.trueContinuation.definition, | 154 node.trueContinuation.definition, |
92 node.falseContinuation.definition))); | 155 node.falseContinuation.definition))); |
93 condition.value.unlink(); | 156 condition.value.unlink(); |
94 node.trueContinuation.unlink(); | 157 node.trueContinuation.unlink(); |
95 node.falseContinuation.unlink(); | 158 node.falseContinuation.unlink(); |
96 parent.body = newNode; | 159 parent.body = newNode; |
97 } | 160 } |
98 } | 161 } |
OLD | NEW |