OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of dart_backend; | 5 part of dart_backend; |
6 | 6 |
7 // TODO(ahe): This class is simply wrong. This backend should use | 7 // TODO(ahe): This class is simply wrong. This backend should use |
8 // elements when it can, not AST nodes. Perhaps a [Map<Element, | 8 // elements when it can, not AST nodes. Perhaps a [Map<Element, |
9 // TreeElements>] is what is needed. | 9 // TreeElements>] is what is needed. |
10 class ElementAst { | 10 class ElementAst { |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } | 123 } |
124 // Enqueue the methods that the VM might invoke on user objects because | 124 // Enqueue the methods that the VM might invoke on user objects because |
125 // we don't trust the resolution to always get these included. | 125 // we don't trust the resolution to always get these included. |
126 world.registerInvocation(new Selector.call("toString", null, 0)); | 126 world.registerInvocation(new Selector.call("toString", null, 0)); |
127 world.registerInvokedGetter(new Selector.getter("hashCode", null)); | 127 world.registerInvokedGetter(new Selector.getter("hashCode", null)); |
128 world.registerInvocation(new Selector.binaryOperator("==")); | 128 world.registerInvocation(new Selector.binaryOperator("==")); |
129 world.registerInvocation(new Selector.call("compareTo", null, 1)); | 129 world.registerInvocation(new Selector.call("compareTo", null, 1)); |
130 } | 130 } |
131 | 131 |
132 void codegen(CodegenWorkItem work) { } | 132 void codegen(CodegenWorkItem work) { } |
133 | |
134 static bool checkTreeIntegrity(tree_ir.RootNode node) { | |
135 new CheckTreeIntegrity().check(node); | |
136 return true; // So this can be used from assert(). | |
137 } | |
138 | |
139 static bool checkCpsIntegrity(cps_ir.RootNode node) { | |
140 new CheckCpsIntegrity().check(node); | |
141 return true; // So this can be used from assert(). | |
142 } | |
143 | |
144 /// Create an [ElementAst] from the CPS IR. | |
145 static ElementAst createElementAst( | |
146 ElementAstCreationContext context, | |
147 Element element, | |
148 cps_ir.RootNode cpsRoot) { | |
149 context.traceCompilation(element.name); | |
150 context.traceGraph('CPS builder', cpsRoot); | |
151 assert(checkCpsIntegrity(cpsRoot)); | |
152 | |
153 // Transformations on the CPS IR. | |
154 void applyCpsPass(cps_opt.Pass pass) { | |
155 pass.rewrite(cpsRoot); | |
156 context.traceGraph(pass.passName, cpsRoot); | |
157 assert(checkCpsIntegrity(cpsRoot)); | |
158 } | |
159 | |
160 // TODO(karlklose): enable type propagation for dart2dart when constant | |
161 // types are correctly marked as instantiated (Issue 21880). | |
162 TypePropagator typePropagator = new TypePropagator( | |
163 context.dartTypes, | |
164 context.constantSystem, | |
165 new UnitTypeSystem(), | |
166 context.internalError); | |
167 applyCpsPass(typePropagator); | |
168 applyCpsPass(new RedundantPhiEliminator()); | |
169 applyCpsPass(new ShrinkingReducer()); | |
170 | |
171 tree_builder.Builder builder = | |
172 new tree_builder.Builder(context.internalError); | |
173 tree_ir.RootNode treeRoot = builder.build(cpsRoot); | |
174 assert(treeRoot != null); | |
175 context.traceGraph('Tree builder', treeRoot); | |
176 assert(checkTreeIntegrity(treeRoot)); | |
177 | |
178 // Transformations on the Tree IR. | |
179 void applyTreePass(tree_opt.Pass pass) { | |
180 pass.rewrite(treeRoot); | |
181 context.traceGraph(pass.passName, treeRoot); | |
182 assert(checkTreeIntegrity(treeRoot)); | |
183 } | |
184 | |
185 applyTreePass(new StatementRewriter(isDartMode: true)); | |
186 applyTreePass(new VariableMerger()); | |
187 applyTreePass(new LoopRewriter()); | |
188 applyTreePass(new LogicalRewriter()); | |
189 applyTreePass(new PullIntoInitializers()); | |
190 | |
191 // Backend-specific transformations. | |
192 new backend_ast_emitter.UnshadowParameters().unshadow(treeRoot); | |
193 context.traceGraph('Unshadow parameters', treeRoot); | |
194 | |
195 TreeElementMapping treeElements = new TreeElementMapping(element); | |
196 backend_ast.RootNode backendAst = backend_ast_emitter.emit(treeRoot); | |
197 Node frontend_ast = backend2frontend.emit(treeElements, backendAst); | |
198 return new ElementAst(frontend_ast, treeElements); | |
199 | |
200 } | |
201 | |
202 /** | 133 /** |
203 * Tells whether we should output given element. Corelib classes like | 134 * Tells whether we should output given element. Corelib classes like |
204 * Object should not be in the resulting code. | 135 * Object should not be in the resulting code. |
205 */ | 136 */ |
206 @override | 137 @override |
207 bool shouldOutput(Element element) { | 138 bool shouldOutput(Element element) { |
208 return (!element.library.isPlatformLibrary && | 139 return (!element.library.isPlatformLibrary && |
209 !element.isSynthesized && | 140 !element.isSynthesized && |
210 element is! AbstractFieldElement) | 141 element is! AbstractFieldElement) |
211 || mirrorRenamer.isMirrorHelperLibrary(element.library); | 142 || mirrorRenamer.isMirrorHelperLibrary(element.library); |
212 } | 143 } |
213 | 144 |
214 int assembleProgram() { | 145 int assembleProgram() { |
215 ElementAstCreationContext context = | 146 ElementAstCreationContext context = |
216 new _ElementAstCreationContext(compiler, constantSystem); | 147 new _ElementAstCreationContext(compiler, constantSystem); |
217 | 148 |
218 ElementAst computeElementAst(AstElement element) { | 149 ElementAst computeElementAst(AstElement element) { |
219 if (!compiler.irBuilder.hasIr(element)) { | 150 return new ElementAst(element.resolvedAst.node, |
220 return new ElementAst(element.resolvedAst.node, | 151 element.resolvedAst.elements); |
221 element.resolvedAst.elements); | |
222 } else { | |
223 cps_ir.RootNode irNode = compiler.irBuilder.getIr(element); | |
224 return createElementAst(context, element, irNode); | |
225 } | |
226 } | 152 } |
227 | 153 |
228 // TODO(johnniwinther): Remove the need for this method. | 154 // TODO(johnniwinther): Remove the need for this method. |
229 void postProcessElementAst( | 155 void postProcessElementAst( |
230 AstElement element, ElementAst elementAst, | 156 AstElement element, ElementAst elementAst, |
231 newTypedefElementCallback, | 157 newTypedefElementCallback, |
232 newClassElementCallback) { | 158 newClassElementCallback) { |
233 ReferencedElementCollector collector = | 159 ReferencedElementCollector collector = |
234 new ReferencedElementCollector(compiler, | 160 new ReferencedElementCollector(compiler, |
235 element, | 161 element, |
(...skipping 322 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
558 } | 484 } |
559 | 485 |
560 void traceGraph(String title, var irObject) { | 486 void traceGraph(String title, var irObject) { |
561 compiler.tracer.traceGraph(title, irObject); | 487 compiler.tracer.traceGraph(title, irObject); |
562 } | 488 } |
563 | 489 |
564 DartTypes get dartTypes => compiler.types; | 490 DartTypes get dartTypes => compiler.types; |
565 | 491 |
566 InternalErrorFunction get internalError => compiler.internalError; | 492 InternalErrorFunction get internalError => compiler.internalError; |
567 } | 493 } |
OLD | NEW |