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