| 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'; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 AstElement element = work.element.implementation; | 32 AstElement element = work.element.implementation; |
| 33 TreeElements treeElements = work.resolvedAst.elements; | 33 TreeElements treeElements = work.resolvedAst.elements; |
| 34 Kernel kernel = new Kernel(backend.compiler); | 34 Kernel kernel = new Kernel(backend.compiler); |
| 35 KernelVisitor visitor = new KernelVisitor(element, treeElements, kernel); | 35 KernelVisitor visitor = new KernelVisitor(element, treeElements, kernel); |
| 36 IrFunction function; | 36 IrFunction function; |
| 37 try { | 37 try { |
| 38 function = visitor.buildFunction(); | 38 function = visitor.buildFunction(); |
| 39 } catch (e) { | 39 } catch (e) { |
| 40 throw "Failed to convert to Kernel IR: $e"; | 40 throw "Failed to convert to Kernel IR: $e"; |
| 41 } | 41 } |
| 42 KernelSsaBuilder builder = new KernelSsaBuilder(function, element, | 42 KernelSsaBuilder builder = new KernelSsaBuilder( |
| 43 work.resolvedAst, backend.compiler, sourceInformationFactory); | 43 function, |
| 44 element, |
| 45 work.resolvedAst, |
| 46 backend.compiler, |
| 47 sourceInformationFactory, |
| 48 visitor.nodeToElement); |
| 44 return builder.build(); | 49 return builder.build(); |
| 45 }); | 50 }); |
| 46 } | 51 } |
| 47 } | 52 } |
| 48 | 53 |
| 49 // DESIGN NOTE: I am implementing this by essentially copying the methods in | |
| 50 // [SsaBuilder], but trying to use Kernel IR instead of our AST nodes. In places | |
| 51 // where there is functionality in the [SsaBuilder] that is not yet needed in | |
| 52 // this builder, I am adding a comment that tells what the [SsaBuilder] does at | |
| 53 // that location. | |
| 54 class KernelSsaBuilder extends ir.Visitor with GraphBuilder { | 54 class KernelSsaBuilder extends ir.Visitor with GraphBuilder { |
| 55 final IrFunction function; | 55 final IrFunction function; |
| 56 final FunctionElement functionElement; | 56 final FunctionElement functionElement; |
| 57 final ResolvedAst resolvedAst; | 57 final ResolvedAst resolvedAst; |
| 58 final Compiler compiler; | 58 final Compiler compiler; |
| 59 final Map<ir.Node, Element> nodeToElement; |
| 59 | 60 |
| 60 JavaScriptBackend get backend => compiler.backend; | 61 JavaScriptBackend get backend => compiler.backend; |
| 61 | 62 |
| 62 LocalsHandler localsHandler; | 63 LocalsHandler localsHandler; |
| 63 SourceInformationBuilder sourceInformationBuilder; | 64 SourceInformationBuilder sourceInformationBuilder; |
| 64 | 65 |
| 65 KernelSsaBuilder(this.function, this.functionElement, this.resolvedAst, | 66 KernelSsaBuilder( |
| 66 this.compiler, SourceInformationStrategy sourceInformationFactory) { | 67 this.function, |
| 68 this.functionElement, |
| 69 this.resolvedAst, |
| 70 this.compiler, |
| 71 SourceInformationStrategy sourceInformationFactory, |
| 72 this.nodeToElement) { |
| 67 graph.element = functionElement; | 73 graph.element = functionElement; |
| 68 // TODO(het): Should sourceInformationBuilder be in GraphBuilder? | 74 // TODO(het): Should sourceInformationBuilder be in GraphBuilder? |
| 69 this.sourceInformationBuilder = | 75 this.sourceInformationBuilder = |
| 70 sourceInformationFactory.createBuilderForContext(resolvedAst); | 76 sourceInformationFactory.createBuilderForContext(resolvedAst); |
| 71 graph.sourceInformation = | 77 graph.sourceInformation = |
| 72 sourceInformationBuilder.buildVariableDeclaration(); | 78 sourceInformationBuilder.buildVariableDeclaration(); |
| 73 this.localsHandler = | 79 this.localsHandler = |
| 74 new LocalsHandler(this, functionElement, null, compiler); | 80 new LocalsHandler(this, functionElement, null, compiler); |
| 75 } | 81 } |
| 76 | 82 |
| 77 HGraph build() { | 83 HGraph build() { |
| 84 // TODO(het): no reason to do this here... |
| 85 HInstruction.idCounter = 0; |
| 78 if (function.kind == ir.ProcedureKind.Method) { | 86 if (function.kind == ir.ProcedureKind.Method) { |
| 79 buildMethod(function, functionElement); | 87 buildMethod(function, functionElement); |
| 80 } else { | 88 } else { |
| 81 compiler.reporter.internalError( | 89 compiler.reporter.internalError( |
| 82 functionElement, | 90 functionElement, |
| 83 "Unable to convert this kind of Kernel " | 91 "Unable to convert this kind of Kernel " |
| 84 "procedure to SSA: ${function.kind}"); | 92 "procedure to SSA: ${function.kind}"); |
| 85 } | 93 } |
| 86 assert(graph.isValid()); | 94 assert(graph.isValid()); |
| 87 return graph; | 95 return graph; |
| 88 } | 96 } |
| 89 | 97 |
| 90 /// Builds a SSA graph for [method]. | 98 /// Builds a SSA graph for [method]. |
| 91 void buildMethod(IrFunction method, FunctionElement functionElement) { | 99 void buildMethod(IrFunction method, FunctionElement functionElement) { |
| 92 // TODO(het): Determine whether or not this method is called in a loop and | |
| 93 // set [graph.isCalledInLoop]. | |
| 94 openFunction(method, functionElement); | 100 openFunction(method, functionElement); |
| 101 method.node.body.accept(this); |
| 102 closeFunction(); |
| 95 } | 103 } |
| 96 | 104 |
| 97 void openFunction(IrFunction method, FunctionElement functionElement) { | 105 void openFunction(IrFunction method, FunctionElement functionElement) { |
| 98 HBasicBlock block = graph.addNewBlock(); | 106 HBasicBlock block = graph.addNewBlock(); |
| 99 open(graph.entry); | 107 open(graph.entry); |
| 100 // TODO(het): Register parameters with a locals handler | 108 // TODO(het): Register parameters with a locals handler |
| 101 localsHandler.startFunction(functionElement, resolvedAst.node); | 109 localsHandler.startFunction(functionElement, resolvedAst.node); |
| 102 close(new HGoto()).addSuccessor(block); | 110 close(new HGoto()).addSuccessor(block); |
| 103 | 111 |
| 104 open(block); | 112 open(block); |
| 113 } |
| 105 | 114 |
| 106 // TODO(het): If this is a constructor then add the type parameters of the | 115 void closeFunction() { |
| 107 // enclosing class as parameters to the method. This must be done before | 116 if (!isAborted()) closeAndGotoExit(new HGoto()); |
| 108 // adding normal parameters because their types may contain references to | 117 graph.finalize(); |
| 109 // the class type parameters. | |
| 110 } | 118 } |
| 111 } | 119 } |
| OLD | NEW |