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 CodegenRegistry, CodegenWorkItem; | 7 import '../common/codegen.dart' show CodegenRegistry, 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 '../diagnostics/spannable.dart'; | 10 import '../diagnostics/spannable.dart'; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 sourceInformationBuilder.buildVariableDeclaration(); | 88 sourceInformationBuilder.buildVariableDeclaration(); |
89 this.localsHandler = | 89 this.localsHandler = |
90 new LocalsHandler(this, functionElement, null, compiler); | 90 new LocalsHandler(this, functionElement, null, compiler); |
91 this.astAdapter = new KernelAstAdapter(compiler.backend, resolvedAst, | 91 this.astAdapter = new KernelAstAdapter(compiler.backend, resolvedAst, |
92 visitor.nodeToAst, visitor.nodeToElement, kernel.functions); | 92 visitor.nodeToAst, visitor.nodeToElement, kernel.functions); |
93 } | 93 } |
94 | 94 |
95 HGraph build() { | 95 HGraph build() { |
96 // TODO(het): no reason to do this here... | 96 // TODO(het): no reason to do this here... |
97 HInstruction.idCounter = 0; | 97 HInstruction.idCounter = 0; |
98 if (function.kind == ir.ProcedureKind.Method) { | 98 if (function.kind == ir.ProcedureKind.Method || |
| 99 function.kind == ir.ProcedureKind.Operator) { |
99 buildMethod(function, functionElement); | 100 buildMethod(function, functionElement); |
100 } else { | 101 } else { |
101 compiler.reporter.internalError( | 102 compiler.reporter.internalError( |
102 functionElement, | 103 functionElement, |
103 "Unable to convert this kind of Kernel " | 104 "Unable to convert this kind of Kernel " |
104 "procedure to SSA: ${function.kind}"); | 105 "procedure to SSA: ${function.kind}"); |
105 } | 106 } |
106 assert(graph.isValid()); | 107 assert(graph.isValid()); |
107 return graph; | 108 return graph; |
108 } | 109 } |
109 | 110 |
110 /// Builds a SSA graph for [method]. | 111 /// Builds a SSA graph for [method]. |
111 void buildMethod(IrFunction method, FunctionElement functionElement) { | 112 void buildMethod(IrFunction method, FunctionElement functionElement) { |
112 openFunction(method, functionElement); | 113 openFunction(method, functionElement); |
113 method.node.body.accept(this); | 114 method.node.body.accept(this); |
114 closeFunction(); | 115 closeFunction(); |
115 } | 116 } |
116 | 117 |
117 void openFunction(IrFunction method, FunctionElement functionElement) { | 118 void openFunction(IrFunction method, FunctionElement functionElement) { |
118 HBasicBlock block = graph.addNewBlock(); | 119 HBasicBlock block = graph.addNewBlock(); |
119 open(graph.entry); | 120 open(graph.entry); |
120 // TODO(het): Register parameters with a locals handler | |
121 localsHandler.startFunction(functionElement, resolvedAst.node); | 121 localsHandler.startFunction(functionElement, resolvedAst.node); |
122 close(new HGoto()).addSuccessor(block); | 122 close(new HGoto()).addSuccessor(block); |
123 | 123 |
124 open(block); | 124 open(block); |
125 } | 125 } |
126 | 126 |
127 void closeFunction() { | 127 void closeFunction() { |
128 if (!isAborted()) closeAndGotoExit(new HGoto()); | 128 if (!isAborted()) closeAndGotoExit(new HGoto()); |
129 graph.finalize(); | 129 graph.finalize(); |
130 } | 130 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 visitNullLiteral(ir.NullLiteral nullLiteral) { | 198 visitNullLiteral(ir.NullLiteral nullLiteral) { |
199 stack.add(graph.addConstantNull(compiler)); | 199 stack.add(graph.addConstantNull(compiler)); |
200 } | 200 } |
201 | 201 |
202 @override | 202 @override |
203 visitVariableGet(ir.VariableGet variableGet) { | 203 visitVariableGet(ir.VariableGet variableGet) { |
204 LocalElement local = astAdapter.getElement(variableGet.variable); | 204 LocalElement local = astAdapter.getElement(variableGet.variable); |
205 stack.add(localsHandler.readLocal(local)); | 205 stack.add(localsHandler.readLocal(local)); |
206 } | 206 } |
207 | 207 |
| 208 // TODO(het): Also extract type arguments |
| 209 /// Extracts the list of instructions for the expressions in the arguments. |
| 210 List<HInstruction> _visitArguments(ir.Arguments arguments) { |
| 211 List<HInstruction> result = <HInstruction>[]; |
| 212 |
| 213 for (ir.Expression argument in arguments.positional) { |
| 214 argument.accept(this); |
| 215 result.add(pop()); |
| 216 } |
| 217 for (ir.NamedExpression argument in arguments.named) { |
| 218 argument.value.accept(this); |
| 219 result.add(pop()); |
| 220 } |
| 221 |
| 222 return result; |
| 223 } |
| 224 |
208 @override | 225 @override |
209 visitStaticInvocation(ir.StaticInvocation invocation) { | 226 visitStaticInvocation(ir.StaticInvocation invocation) { |
210 List<HInstruction> inputs = <HInstruction>[]; | |
211 | |
212 for (ir.Expression argument in invocation.arguments.positional) { | |
213 argument.accept(this); | |
214 inputs.add(pop()); | |
215 } | |
216 for (ir.NamedExpression argument in invocation.arguments.named) { | |
217 argument.value.accept(this); | |
218 inputs.add(pop()); | |
219 } | |
220 | |
221 ir.Procedure target = invocation.target; | 227 ir.Procedure target = invocation.target; |
222 bool targetCanThrow = astAdapter.getCanThrow(target); | 228 bool targetCanThrow = astAdapter.getCanThrow(target); |
223 TypeMask typeMask = astAdapter.returnTypeOf(target); | 229 TypeMask typeMask = astAdapter.returnTypeOf(target); |
224 | 230 |
| 231 var arguments = _visitArguments(invocation.arguments); |
| 232 |
225 HInstruction instruction = new HInvokeStatic( | 233 HInstruction instruction = new HInvokeStatic( |
226 astAdapter.getElement(target).declaration, inputs, typeMask, | 234 astAdapter.getElement(target).declaration, arguments, typeMask, |
227 targetCanThrow: targetCanThrow); | 235 targetCanThrow: targetCanThrow); |
228 instruction.sideEffects = astAdapter.getSideEffects(target); | 236 instruction.sideEffects = astAdapter.getSideEffects(target); |
229 | 237 |
230 push(instruction); | 238 push(instruction); |
231 } | 239 } |
232 | 240 |
| 241 // TODO(het): Decide when to inline |
| 242 @override |
| 243 visitMethodInvocation(ir.MethodInvocation invocation) { |
| 244 invocation.receiver.accept(this); |
| 245 HInstruction receiver = pop(); |
| 246 |
| 247 List<HInstruction> arguments = <HInstruction>[receiver] |
| 248 ..addAll(_visitArguments(invocation.arguments)); |
| 249 |
| 250 List<HInstruction> inputs = <HInstruction>[]; |
| 251 |
| 252 bool isIntercepted = astAdapter.isIntercepted(invocation); |
| 253 if (isIntercepted) { |
| 254 HInterceptor interceptor = |
| 255 new HInterceptor(receiver, backend.nonNullType); |
| 256 add(interceptor); |
| 257 inputs.add(interceptor); |
| 258 } |
| 259 inputs.addAll(arguments); |
| 260 |
| 261 TypeMask type = astAdapter.selectorTypeOf(invocation); |
| 262 |
| 263 push(new HInvokeDynamicMethod(astAdapter.getSelector(invocation), |
| 264 astAdapter.getTypeMask(invocation), inputs, type, isIntercepted)); |
| 265 } |
| 266 |
| 267 @override |
| 268 visitThisExpression(ir.ThisExpression thisExpression) { |
| 269 stack.add(localsHandler.readThis()); |
| 270 } |
| 271 |
233 @override | 272 @override |
234 visitExpressionStatement(ir.ExpressionStatement exprStatement) { | 273 visitExpressionStatement(ir.ExpressionStatement exprStatement) { |
235 exprStatement.expression.accept(this); | 274 exprStatement.expression.accept(this); |
236 pop(); | 275 pop(); |
237 } | 276 } |
238 } | 277 } |
OLD | NEW |