OLD | NEW |
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 import 'package:kernel/ast.dart' as ir; |
| 6 |
5 import '../common/codegen.dart' show CodegenWorkItem; | 7 import '../common/codegen.dart' show CodegenWorkItem; |
6 import '../common/tasks.dart' show CompilerTask; | 8 import '../common/tasks.dart' show CompilerTask; |
| 9 import '../compiler.dart'; |
7 import '../elements/elements.dart'; | 10 import '../elements/elements.dart'; |
8 import '../io/source_information.dart'; | 11 import '../io/source_information.dart'; |
9 import '../js_backend/backend.dart' show JavaScriptBackend, FunctionCompiler; | 12 import '../js_backend/backend.dart' show JavaScriptBackend; |
10 import '../kernel/kernel.dart'; | 13 import '../kernel/kernel.dart'; |
11 import '../kernel/kernel_visitor.dart'; | 14 import '../kernel/kernel_visitor.dart'; |
12 import '../resolution/tree_elements.dart'; | 15 import '../resolution/tree_elements.dart'; |
13 | 16 import 'graph_builder.dart'; |
14 import 'nodes.dart'; | 17 import 'nodes.dart'; |
15 | 18 |
16 class SsaKernelBuilderTask extends CompilerTask { | 19 class SsaKernelBuilderTask extends CompilerTask { |
17 final JavaScriptBackend backend; | 20 final JavaScriptBackend backend; |
18 final SourceInformationStrategy sourceInformationFactory; | 21 final SourceInformationStrategy sourceInformationFactory; |
19 | 22 |
20 String get name => 'SSA kernel builder'; | 23 String get name => 'SSA kernel builder'; |
21 | 24 |
22 SsaKernelBuilderTask(JavaScriptBackend backend, this.sourceInformationFactory) | 25 SsaKernelBuilderTask(JavaScriptBackend backend, this.sourceInformationFactory) |
23 : backend = backend, | 26 : backend = backend, |
24 super(backend.compiler.measurer); | 27 super(backend.compiler.measurer); |
25 | 28 |
26 HGraph build(CodegenWorkItem work) { | 29 HGraph build(CodegenWorkItem work) { |
27 return measure(() { | 30 return measure(() { |
28 AstElement element = work.element.implementation; | 31 AstElement element = work.element.implementation; |
29 TreeElements treeElements = work.resolvedAst.elements; | 32 TreeElements treeElements = work.resolvedAst.elements; |
30 Kernel kernel = new Kernel(backend.compiler); | 33 Kernel kernel = new Kernel(backend.compiler); |
31 KernelVisitor visitor = new KernelVisitor(element, treeElements, kernel); | 34 KernelVisitor visitor = new KernelVisitor(element, treeElements, kernel); |
32 IrFunction function; | 35 IrFunction function; |
33 try { | 36 try { |
34 function = visitor.buildFunction(); | 37 function = visitor.buildFunction(); |
35 } catch(e) { | 38 } catch (e) { |
36 throw "Failed to convert to Kernel IR: $e"; | 39 throw "Failed to convert to Kernel IR: $e"; |
37 } | 40 } |
38 throw "Successfully converted to Kernel IR: $function"; | 41 KernelSsaBuilder builder = |
| 42 new KernelSsaBuilder(function, element, backend.compiler); |
| 43 return builder.build(); |
39 }); | 44 }); |
40 } | 45 } |
41 } | 46 } |
| 47 |
| 48 // DESIGN NOTE: I am implementing this by essentially copying the methods in |
| 49 // [SsaBuilder], but trying to use Kernel IR instead of our AST nodes. In places |
| 50 // where there is functionality in the [SsaBuilder] that is not yet needed in |
| 51 // this builder, I am adding a comment that tells what the [SsaBuilder] does at |
| 52 // that location. |
| 53 class KernelSsaBuilder extends ir.Visitor with GraphBuilder { |
| 54 final IrFunction function; |
| 55 final FunctionElement functionElement; |
| 56 final Compiler compiler; |
| 57 |
| 58 KernelSsaBuilder(this.function, this.functionElement, this.compiler); |
| 59 |
| 60 HGraph build() { |
| 61 if (function.kind == ir.ProcedureKind.Method) { |
| 62 buildMethod(function); |
| 63 } else { |
| 64 compiler.reporter.internalError( |
| 65 functionElement, |
| 66 "Unable to convert this kind of Kernel " |
| 67 "procedure to SSA: ${function.kind}"); |
| 68 } |
| 69 assert(graph.isValid()); |
| 70 return graph; |
| 71 } |
| 72 |
| 73 /// Builds a SSA graph for [method]. |
| 74 void buildMethod(IrFunction method) { |
| 75 // TODO(het): Determine whether or not this method is called in a loop and |
| 76 // set [graph.isCalledInLoop]. |
| 77 openFunction(method); |
| 78 } |
| 79 |
| 80 void openFunction(IrFunction method) { |
| 81 HBasicBlock block = graph.addNewBlock(); |
| 82 open(graph.entry); |
| 83 // TODO(het): Register parameters with a locals handler |
| 84 close(new HGoto()).addSuccessor(block); |
| 85 |
| 86 open(block); |
| 87 |
| 88 // TODO(het): If this is a constructor then add the type parameters of the |
| 89 // enclosing class as parameters to the method. This must be done before |
| 90 // adding normal parameters because their types may contain references to |
| 91 // the class type parameters. |
| 92 |
| 93 } |
| 94 } |
OLD | NEW |