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

Side by Side Diff: frog/leg/ssa/builder.dart

Issue 8888010: First instantiations of objects. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added comment. Created 9 years 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 | Annotate | Revision Log
« no previous file with comments | « frog/leg/namer.dart ('k') | frog/leg/ssa/nodes.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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 class SsaBuilderTask extends CompilerTask { 5 class SsaBuilderTask extends CompilerTask {
6 SsaBuilderTask(Compiler compiler) : super(compiler); 6 SsaBuilderTask(Compiler compiler) : super(compiler);
7 String get name() => 'SSA builder'; 7 String get name() => 'SSA builder';
8 8
9 HGraph build(FunctionElement element, TreeElements elements) { 9 HGraph build(FunctionElement element, TreeElements elements) {
10 return measure(() { 10 return measure(() {
11 FunctionExpression function = element.node; 11 FunctionExpression function = element.node;
12 HInstruction.idCounter = 0; 12 HInstruction.idCounter = 0;
13 HGraph graph = 13 SsaBuilder builder = new SsaBuilder(compiler, elements);
14 compileMethod(function.parameters, function.body, elements); 14 HGraph graph;
15 switch (element.kind) {
16 case ElementKind.CONSTRUCTOR:
17 graph = compileConstructor(builder, function, element, elements);
18 break;
19 case ElementKind.CONSTRUCTOR_BODY:
20 graph = compileConstructorBody(builder, function, element, elements);
21 break;
22 case ElementKind.FUNCTION:
23 graph = compileMethod(builder, function, element, elements);
24 break;
25 }
15 assert(graph.isValid()); 26 assert(graph.isValid());
16 if (GENERATE_SSA_TRACE) { 27 if (GENERATE_SSA_TRACE) {
17 Identifier name = function.name; 28 Identifier name = function.name;
18 new HTracer.singleton().traceCompilation(name.source.toString()); 29 new HTracer.singleton().traceCompilation(name.source.toString());
19 new HTracer.singleton().traceGraph('builder', graph); 30 new HTracer.singleton().traceGraph('builder', graph);
20 } 31 }
21 return graph; 32 return graph;
22 }); 33 });
23 } 34 }
24 35
25 HGraph compileMethod(NodeList parameters, 36 HGraph compileConstructor(SsaBuilder builder,
26 Node body, 37 FunctionExpression function,
38 FunctionElement element,
39 TreeElements elements) {
40 // The body of the constructor will be generated in a separate function.
41 ConstructorBodyElement bodyElement = new ConstructorBodyElement(element);
42 compiler.worklist.add(new WorkElement.toCodegen(bodyElement, elements));
43 ClassElement classElement = element.enclosingElement;
44 classElement.backendMembers =
45 classElement.backendMembers.prepend(bodyElement);
46 // TODO(floitsch): pass initializer-list to builder.
47 return builder.buildFactory(classElement, bodyElement, function.parameters);
48 }
49
50 HGraph compileConstructorBody(SsaBuilder builder,
51 FunctionExpression function,
52 FunctionElement element,
53 TreeElements elements) {
54 // TODO(floitsch): find super call and pass it to the builder.
55 return builder.buildConstructorBody(null,
56 function.parameters,
57 function.body);
58 }
59
60 HGraph compileMethod(SsaBuilder builder,
61 FunctionExpression function,
62 FunctionElement element,
27 TreeElements elements) { 63 TreeElements elements) {
28 SsaBuilder builder = new SsaBuilder(compiler, elements); 64 return builder.buildMethod(function.parameters, function.body);
29 HGraph graph = builder.build(parameters, body);
30 return graph;
31 } 65 }
32 } 66 }
33 67
34 class SsaBuilder implements Visitor { 68 class SsaBuilder implements Visitor {
35 final Compiler compiler; 69 final Compiler compiler;
36 final TreeElements elements; 70 final TreeElements elements;
37 HGraph graph; 71 HGraph graph;
38 72
39 // We build the Ssa graph by simulating a stack machine. 73 // We build the Ssa graph by simulating a stack machine.
40 List<HInstruction> stack; 74 List<HInstruction> stack;
41 75
42 Map<Element, HInstruction> definitions; 76 Map<Element, HInstruction> definitions;
43 77
44 // The current block to add instructions to. Might be null, if we are 78 // The current block to add instructions to. Might be null, if we are
45 // visiting dead code. 79 // visiting dead code.
46 HBasicBlock current; 80 HBasicBlock current;
47 // Whether we are currently processing a condition expression (e.g., from 81 // Whether we are currently processing a condition expression (e.g., from
48 // an if, while, do-while, or for statement, or the conditional expression. 82 // an if, while, do-while, or for statement, or the conditional expression.
49 // (Currently, only if-statements are processed this way). 83 // (Currently, only if-statements are processed this way).
50 bool isConditionExpression = false; 84 bool isConditionExpression = false;
51 85
52 SsaBuilder(this.compiler, this.elements); 86 SsaBuilder(this.compiler, this.elements);
53 87
54 HGraph build(NodeList parameters, Node body) { 88 HGraph buildMethod(NodeList parameters, Node body) {
89 openFunction(parameters);
90 body.accept(this);
91 return closeFunction();
92 }
93
94 HGraph buildFactory(ClassElement classElement,
95 ConstructorBodyElement bodyElement,
96 NodeList parameters) {
97 openFunction(parameters);
98 HForeignNew newObject = new HForeignNew(classElement, []);
99 add(newObject);
100 String methodName =
101 compiler.namer.constructorBodyName(bodyElement.constructor);
102 List bodyCallInputs = <HInstruction>[];
103 bodyCallInputs.add(newObject);
104 Link link = parameters.nodes;
105 for (; !link.isEmpty(); link = link.tail) {
106 Element parameterElement = elements[link.head];
107 HInstruction currentValue = definitions[parameterElement];
108 bodyCallInputs.add(currentValue);
109 }
110 add(new HInvokeDynamic(methodName, bodyCallInputs));
111 close(new HReturn(newObject)).addSuccessor(graph.exit);
112 return closeFunction();
113 }
114
115 HGraph buildConstructorBody(Send superInvocation,
116 NodeList parameters,
117 Node body) {
118 openFunction(parameters);
119 if (superInvocation !== null) {
120 compiler.unimplemented('SsaBuilder.buildConstructorBody');
121 Element superElement = elements[superInvocation];
122 String methodName = compiler.namer.constructorBodyName(superElement);
123 List superInputs = <HInstruction>[];
124 superInputs.add(new HThis());
125 Link link = superInvocation.arguments.head;
126 for (; !link.isEmpty(); link = link.tail) {
127 visit(link.head);
128 superInputs.add(pop());
129 }
130 add(new HInvokeDynamic(methodName, superInputs));
131 }
132 body.accept(this);
133 return closeFunction();
134 }
135
136 void openFunction(NodeList parameters) {
55 stack = new List<HInstruction>(); 137 stack = new List<HInstruction>();
56 definitions = new Map<Element, HInstruction>(); 138 definitions = new Map<Element, HInstruction>();
57 139
58 graph = new HGraph(); 140 graph = new HGraph();
59 HBasicBlock block = graph.addNewBlock(); 141 HBasicBlock block = graph.addNewBlock();
60 142
61 open(graph.entry); 143 open(graph.entry);
62 visitParameterValues(parameters); 144 visitParameterValues(parameters);
63 close(new HGoto()).addSuccessor(block); 145 close(new HGoto()).addSuccessor(block);
64 146
65 open(block); 147 open(block);
66 body.accept(this); 148 }
67 149
150 HGraph closeFunction() {
68 // TODO(kasperl): Make this goto an implicit return. 151 // TODO(kasperl): Make this goto an implicit return.
69 if (!isAborted()) close(new HGoto()).addSuccessor(graph.exit); 152 if (!isAborted()) close(new HGoto()).addSuccessor(graph.exit);
70 graph.finalize(); 153 graph.finalize();
71 return graph; 154 return graph;
72 } 155 }
73 156
74 HBasicBlock addNewBlock() { 157 HBasicBlock addNewBlock() {
75 HBasicBlock block = graph.addNewBlock(); 158 HBasicBlock block = graph.addNewBlock();
76 if (isConditionExpression) block.isCondition = true; 159 if (isConditionExpression) block.isCondition = true;
77 return block; 160 return block;
(...skipping 504 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 } else { 665 } else {
583 HStatic target = new HStatic(element); 666 HStatic target = new HStatic(element);
584 add(target); 667 add(target);
585 List inputs = <HInstruction>[target]; 668 List inputs = <HInstruction>[target];
586 inputs.addAll(arguments); 669 inputs.addAll(arguments);
587 push(new HInvokeStatic(inputs)); 670 push(new HInvokeStatic(inputs));
588 } 671 }
589 } 672 }
590 } 673 }
591 674
592 visitNewExpression(NewExpression node) { 675 visitNewExpression(NewExpression node) => visitSend(node.send);
593 compiler.unimplemented("SsaBuilder: new expression");
594 }
595 676
596 HInstruction updateDefinition(Node node, HInstruction value) { 677 HInstruction updateDefinition(Node node, HInstruction value) {
597 VariableElement element = elements[node]; 678 VariableElement element = elements[node];
598 value = guard(element.type, value); 679 value = guard(element.type, value);
599 definitions[element] = value; 680 definitions[element] = value;
600 return value; 681 return value;
601 } 682 }
602 683
603 visitSendSet(SendSet node) { 684 visitSendSet(SendSet node) {
604 Operator op = node.assignmentOperator; 685 Operator op = node.assignmentOperator;
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
761 visit(link.head); 842 visit(link.head);
762 inputs.add(pop()); 843 inputs.add(pop());
763 } 844 }
764 push(new HLiteralList(inputs)); 845 push(new HLiteralList(inputs));
765 } 846 }
766 847
767 visitConditional(Conditional node) { 848 visitConditional(Conditional node) {
768 compiler.unimplemented("SsaBuilder: conditional"); 849 compiler.unimplemented("SsaBuilder: conditional");
769 } 850 }
770 } 851 }
OLDNEW
« no previous file with comments | « frog/leg/namer.dart ('k') | frog/leg/ssa/nodes.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698