Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: pkg/compiler/lib/src/ssa/builder_kernel.dart

Issue 2360673003: kernel->ssa: Implement for-loops and while-loops (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « pkg/compiler/lib/src/ssa/builder.dart ('k') | pkg/compiler/lib/src/ssa/graph_builder.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.dart'; 7 import '../common.dart';
8 import '../common/codegen.dart' show CodegenRegistry, CodegenWorkItem; 8 import '../common/codegen.dart' show CodegenRegistry, CodegenWorkItem;
9 import '../common/tasks.dart' show CompilerTask; 9 import '../common/tasks.dart' show CompilerTask;
10 import '../compiler.dart'; 10 import '../compiler.dart';
11 import '../dart_types.dart'; 11 import '../dart_types.dart';
12 import '../elements/elements.dart'; 12 import '../elements/elements.dart';
13 import '../io/source_information.dart'; 13 import '../io/source_information.dart';
14 import '../js_backend/backend.dart' show JavaScriptBackend; 14 import '../js_backend/backend.dart' show JavaScriptBackend;
15 import '../kernel/kernel.dart'; 15 import '../kernel/kernel.dart';
16 import '../resolution/tree_elements.dart';
16 import '../tree/dartstring.dart'; 17 import '../tree/dartstring.dart';
17 import '../types/masks.dart'; 18 import '../types/masks.dart';
18 import '../universe/selector.dart'; 19 import '../universe/selector.dart';
19 20
20 import 'graph_builder.dart'; 21 import 'graph_builder.dart';
21 import 'kernel_ast_adapter.dart'; 22 import 'kernel_ast_adapter.dart';
22 import 'kernel_string_builder.dart'; 23 import 'kernel_string_builder.dart';
23 import 'locals_handler.dart'; 24 import 'locals_handler.dart';
25 import 'loop_handler.dart';
24 import 'nodes.dart'; 26 import 'nodes.dart';
25 import 'ssa_branch_builder.dart'; 27 import 'ssa_branch_builder.dart';
26 28
27 class SsaKernelBuilderTask extends CompilerTask { 29 class SsaKernelBuilderTask extends CompilerTask {
28 final JavaScriptBackend backend; 30 final JavaScriptBackend backend;
29 final SourceInformationStrategy sourceInformationFactory; 31 final SourceInformationStrategy sourceInformationFactory;
30 32
31 String get name => 'SSA kernel builder'; 33 String get name => 'SSA kernel builder';
32 34
33 SsaKernelBuilderTask(JavaScriptBackend backend, this.sourceInformationFactory) 35 SsaKernelBuilderTask(JavaScriptBackend backend, this.sourceInformationFactory)
34 : backend = backend, 36 : backend = backend,
35 super(backend.compiler.measurer); 37 super(backend.compiler.measurer);
36 38
37 HGraph build(CodegenWorkItem work) { 39 HGraph build(CodegenWorkItem work) {
38 return measure(() { 40 return measure(() {
39 AstElement element = work.element.implementation; 41 AstElement element = work.element.implementation;
40 Kernel kernel = backend.kernelTask.kernel; 42 Kernel kernel = backend.kernelTask.kernel;
41 KernelSsaBuilder builder = new KernelSsaBuilder(element, work.resolvedAst, 43 KernelSsaBuilder builder = new KernelSsaBuilder(element, work.resolvedAst,
42 backend.compiler, work.registry, sourceInformationFactory, kernel); 44 backend.compiler, work.registry, sourceInformationFactory, kernel);
43 return builder.build(); 45 return builder.build();
44 }); 46 });
45 } 47 }
46 } 48 }
47 49
48 class KernelSsaBuilder extends ir.Visitor with GraphBuilder { 50 class KernelSsaBuilder extends ir.Visitor with GraphBuilder {
49 ir.Node target; 51 ir.Node target;
50 final AstElement targetElement; 52 final AstElement targetElement;
51 final ResolvedAst resolvedAst; 53 final ResolvedAst resolvedAst;
52 final Compiler compiler;
53 final CodegenRegistry registry; 54 final CodegenRegistry registry;
54 55
55 JavaScriptBackend get backend => compiler.backend; 56 JavaScriptBackend get backend => compiler.backend;
56 57
58 TreeElements get elements => resolvedAst.elements;
59
57 SourceInformationBuilder sourceInformationBuilder; 60 SourceInformationBuilder sourceInformationBuilder;
58 KernelAstAdapter astAdapter; 61 KernelAstAdapter astAdapter;
62 LoopHandler<ir.Node> loopHandler;
59 63
60 KernelSsaBuilder( 64 KernelSsaBuilder(
61 this.targetElement, 65 this.targetElement,
62 this.resolvedAst, 66 this.resolvedAst,
63 this.compiler, 67 Compiler compiler,
64 this.registry, 68 this.registry,
65 SourceInformationStrategy sourceInformationFactory, 69 SourceInformationStrategy sourceInformationFactory,
66 Kernel kernel) { 70 Kernel kernel) {
71 this.compiler = compiler;
72 this.loopHandler = new KernelLoopHandler(this);
67 graph.element = targetElement; 73 graph.element = targetElement;
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 = new LocalsHandler(this, targetElement, null, compiler); 79 this.localsHandler = new LocalsHandler(this, targetElement, null, compiler);
74 this.astAdapter = new KernelAstAdapter(kernel, compiler.backend, 80 this.astAdapter = new KernelAstAdapter(kernel, compiler.backend,
75 resolvedAst, kernel.nodeToAst, kernel.nodeToElement); 81 resolvedAst, kernel.nodeToAst, kernel.nodeToElement);
76 Element originTarget = targetElement; 82 Element originTarget = targetElement;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 value = pop(); 199 value = pop();
194 // TODO(het): Check or trust the type of value 200 // TODO(het): Check or trust the type of value
195 } 201 }
196 // TODO(het): Add source information 202 // TODO(het): Add source information
197 // TODO(het): Set a return value instead of closing the function when we 203 // TODO(het): Set a return value instead of closing the function when we
198 // support inlining. 204 // support inlining.
199 closeAndGotoExit(new HReturn(value, null)); 205 closeAndGotoExit(new HReturn(value, null));
200 } 206 }
201 207
202 @override 208 @override
209 void visitForStatement(ir.ForStatement forStatement) {
210 assert(isReachable);
211 assert(forStatement.body != null);
212 void buildInitializer() {
213 for (ir.VariableDeclaration declaration in forStatement.variables) {
214 declaration.accept(this);
215 }
216 }
217
218 HInstruction buildCondition() {
219 if (forStatement.condition == null) {
220 return graph.addConstantBool(true, compiler);
221 }
222 forStatement.condition.accept(this);
223 return popBoolified();
224 }
225
226 void buildUpdate() {
227 for (ir.Expression expression in forStatement.updates) {
228 expression.accept(this);
229 assert(!isAborted());
230 // The result of the update instruction isn't used, and can just
231 // be dropped.
232 pop();
233 }
234 }
235
236 void buildBody() {
237 forStatement.body.accept(this);
238 }
239
240 loopHandler.handleLoop(
241 forStatement, buildInitializer, buildCondition, buildUpdate, buildBody);
242 }
243
244 @override
245 void visitWhileStatement(ir.WhileStatement whileStatement) {
246 assert(isReachable);
247 HInstruction buildCondition() {
248 whileStatement.condition.accept(this);
249 return popBoolified();
250 }
251
252 loopHandler.handleLoop(whileStatement, () {}, buildCondition, () {}, () {
253 whileStatement.body.accept(this);
254 });
255 }
256
257 @override
203 void visitIfStatement(ir.IfStatement ifStatement) { 258 void visitIfStatement(ir.IfStatement ifStatement) {
204 SsaBranchBuilder brancher = new SsaBranchBuilder(this, compiler); 259 SsaBranchBuilder brancher = new SsaBranchBuilder(this, compiler);
205 brancher.handleIf( 260 brancher.handleIf(
206 () => ifStatement.condition.accept(this), 261 () => ifStatement.condition.accept(this),
207 () => ifStatement.then.accept(this), 262 () => ifStatement.then.accept(this),
208 () => ifStatement.otherwise?.accept(this)); 263 () => ifStatement.otherwise?.accept(this));
209 } 264 }
210 265
211 @override 266 @override
212 void visitConditionalExpression(ir.ConditionalExpression conditional) { 267 void visitConditionalExpression(ir.ConditionalExpression conditional) {
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
567 push(new HNot(popBoolified(), backend.boolType)); 622 push(new HNot(popBoolified(), backend.boolType));
568 } 623 }
569 624
570 @override 625 @override
571 void visitStringConcatenation(ir.StringConcatenation stringConcat) { 626 void visitStringConcatenation(ir.StringConcatenation stringConcat) {
572 KernelStringBuilder stringBuilder = new KernelStringBuilder(this); 627 KernelStringBuilder stringBuilder = new KernelStringBuilder(this);
573 stringConcat.accept(stringBuilder); 628 stringConcat.accept(stringBuilder);
574 stack.add(stringBuilder.result); 629 stack.add(stringBuilder.result);
575 } 630 }
576 } 631 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/ssa/builder.dart ('k') | pkg/compiler/lib/src/ssa/graph_builder.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698