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

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

Issue 9632018: Switch-implementation. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add missing handling of default in unparser. Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 SsaCodeGeneratorTask extends CompilerTask { 5 class SsaCodeGeneratorTask extends CompilerTask {
6 SsaCodeGeneratorTask(Compiler compiler) : super(compiler); 6 SsaCodeGeneratorTask(Compiler compiler) : super(compiler);
7 String get name() => 'SSA code generator'; 7 String get name() => 'SSA code generator';
8 8
9 String generate(WorkItem work, HGraph graph) { 9 String generate(WorkItem work, HGraph graph) {
10 return measure(() { 10 return measure(() {
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 /** 208 /**
209 * Only visits the arguments starting at inputs[HInvoke.ARGUMENTS_OFFSET]. 209 * Only visits the arguments starting at inputs[HInvoke.ARGUMENTS_OFFSET].
210 */ 210 */
211 void visitArguments(List<HInstruction> inputs) { 211 void visitArguments(List<HInstruction> inputs) {
212 assert(inputs.length >= HInvoke.ARGUMENTS_OFFSET); 212 assert(inputs.length >= HInvoke.ARGUMENTS_OFFSET);
213 buffer.add('('); 213 buffer.add('(');
214 for (int i = HInvoke.ARGUMENTS_OFFSET; i < inputs.length; i++) { 214 for (int i = HInvoke.ARGUMENTS_OFFSET; i < inputs.length; i++) {
215 if (i != HInvoke.ARGUMENTS_OFFSET) buffer.add(', '); 215 if (i != HInvoke.ARGUMENTS_OFFSET) buffer.add(', ');
216 use(inputs[i], JSPrecedence.ASSIGNMENT_PRECEDENCE); 216 use(inputs[i], JSPrecedence.ASSIGNMENT_PRECEDENCE);
217 } 217 }
218 buffer.add(")"); 218 buffer.add(')');
219 } 219 }
220 220
221 void define(HInstruction instruction) { 221 void define(HInstruction instruction) {
222 buffer.add('var ${temporary(instruction)} = '); 222 buffer.add('var ${temporary(instruction)} = ');
223 visit(instruction, JSPrecedence.ASSIGNMENT_PRECEDENCE); 223 visit(instruction, JSPrecedence.ASSIGNMENT_PRECEDENCE);
224 } 224 }
225 225
226 void use(HInstruction argument, int expectedPrecedence) { 226 void use(HInstruction argument, int expectedPrecedence) {
227 if (argument.generateAtUseSite()) { 227 if (argument.generateAtUseSite()) {
228 visit(argument, expectedPrecedence); 228 visit(argument, expectedPrecedence);
229 } else { 229 } else {
230 buffer.add(temporary(argument)); 230 buffer.add(temporary(argument));
231 } 231 }
232 } 232 }
233 233
234 visit(HInstruction node, int expectedPrecedence) { 234 visit(HInstruction node, int expectedPrecedence) {
235 int oldPrecedence = this.expectedPrecedence; 235 int oldPrecedence = this.expectedPrecedence;
236 this.expectedPrecedence = expectedPrecedence; 236 this.expectedPrecedence = expectedPrecedence;
237 node.accept(this); 237 node.accept(this);
238 this.expectedPrecedence = oldPrecedence; 238 this.expectedPrecedence = oldPrecedence;
239 } 239 }
240 240
241 void handleLabeledBlock(HLabeledBlockInformation labeledBlockInfo) { 241 void handleLabeledBlock(HLabeledBlockInformation labeledBlockInfo) {
242 addIndentation(); 242 addIndentation();
243 for (SourceString label in labeledBlockInfo.labels) { 243 for (LabelElement label in labeledBlockInfo.labels) {
244 addLabel(label); 244 addLabel(label);
245 buffer.add(":"); 245 buffer.add(':');
246 } 246 }
247 buffer.add("{\n"); 247 String implicitLabel = labeledBlockInfo.target.implicitLabel();
248 if (implicitLabel !== null) {
249 buffer.add(@'$');
250 buffer.add(implicitLabel);
251 buffer.add(@':');
252 }
253 buffer.add('{\n');
248 indent++; 254 indent++;
249 255
250 visitSubGraph(labeledBlockInfo.body); 256 visitSubGraph(labeledBlockInfo.body);
251 257
252 indent--; 258 indent--;
253 addIndentation(); 259 addIndentation();
254 buffer.add("}\n"); 260 buffer.add('}\n');
255 261
256 visitBasicBlock(labeledBlockInfo.joinBlock); 262 if (labeledBlockInfo.joinBlock !== null) {
263 visitBasicBlock(labeledBlockInfo.joinBlock);
264 }
257 } 265 }
258 266
259 267
260 visitBasicBlock(HBasicBlock node) { 268 visitBasicBlock(HBasicBlock node) {
261 // Abort traversal if we are leaving the currently active sub-graph. 269 // Abort traversal if we are leaving the currently active sub-graph.
262 if (!subGraph.contains(node)) return; 270 if (!subGraph.contains(node)) return;
263 271
264 // If this node has special behavior attached, handle it. 272 // If this node has special behavior attached, handle it.
265 // If we reach here again while handling the attached information, 273 // If we reach here again while handling the attached information,
266 // e.g., because we call visitSubGraph on a subgraph starting here, 274 // e.g., because we call visitSubGraph on a subgraph starting here,
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 if (dominated.length == 2 && currentBlock !== currentGraph.entry) { 423 if (dominated.length == 2 && currentBlock !== currentGraph.entry) {
416 unreachable(); 424 unreachable();
417 } 425 }
418 assert(dominated[0] == currentBlock.successors[0]); 426 assert(dominated[0] == currentBlock.successors[0]);
419 visitBasicBlock(dominated[0]); 427 visitBasicBlock(dominated[0]);
420 } 428 }
421 429
422 // Used to write the name of labels. 430 // Used to write the name of labels.
423 // The default implementation uses the unmodified Dart label name. 431 // The default implementation uses the unmodified Dart label name.
424 // Specializations might change this. 432 // Specializations might change this.
425 void addLabel(SourceString label) { 433 void addLabel(LabelElement label) {
426 buffer.add(label.slowToString()); 434 buffer.add(@'$');
435 buffer.add(label.labelName);
427 } 436 }
428 437
429 visitBreak(HBreak node) { 438 visitBreak(HBreak node) {
430 assert(currentBlock.successors.length == 1); 439 assert(currentBlock.successors.length == 1);
431 // No block finishing with a 'break' can have more than 440 // No block finishing with a 'break' can have more than
432 // one dominated block (since it has only one successor). 441 // one dominated block (since it has only one successor).
433 // If the successor is dominated by another block, then the other block 442 // If the successor is dominated by another block, then the other block
434 // is responsible for visiting the successor. 443 // is responsible for visiting the successor.
435 List<HBasicBlock> dominated = currentBlock.dominatedBlocks; 444 List<HBasicBlock> dominated = currentBlock.dominatedBlocks;
436 assert(dominated.isEmpty()); 445 assert(dominated.isEmpty());
437 // Otherwise we would have bailed out in the builder. 446 // Otherwise we would have bailed out in the builder.
438 addIndentation(); 447 addIndentation();
439 buffer.add("break"); 448 buffer.add("break");
440 if (node.label !== null) { 449 if (node.target is LabelElement) {
450 LabelElement target = node.target;
441 buffer.add(" "); 451 buffer.add(" ");
442 addLabel(node.label); 452 addLabel(target);
453 } else {
454 StatementElement target = node.target;
455 String implicitLabel = target.implicitLabel();
456 if (implicitLabel !== null) {
457 buffer.add(@' $');
458 buffer.add(implicitLabel);
459 }
443 } 460 }
444 buffer.add(";\n"); 461 buffer.add(";\n");
445 } 462 }
446 463
447 visitTry(HTry node) { 464 visitTry(HTry node) {
448 addIndentation(); 465 addIndentation();
449 buffer.add('try {\n'); 466 buffer.add('try {\n');
450 indent++; 467 indent++;
451 List<HBasicBlock> successors = node.block.successors; 468 List<HBasicBlock> successors = node.block.successors;
452 visitBasicBlock(successors[0]); 469 visitBasicBlock(successors[0]);
(...skipping 680 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 checkArray(input, '!=='); 1150 checkArray(input, '!==');
1134 buffer.add(')) '); 1151 buffer.add(')) ');
1135 bailout(node, 'Not a string or array'); 1152 bailout(node, 'Not a string or array');
1136 } else { 1153 } else {
1137 unreachable(); 1154 unreachable();
1138 } 1155 }
1139 } 1156 }
1140 1157
1141 void beginLoop(HBasicBlock block) { 1158 void beginLoop(HBasicBlock block) {
1142 addIndentation(); 1159 addIndentation();
1143 for (SourceString label in block.loopInformation.labels) { 1160 for (LabelElement label in block.loopInformation.labels) {
1144 buffer.add("${label.slowToString()}:"); 1161 addLabel(label);
1162 buffer.add(":");
1145 } 1163 }
1146 buffer.add('while (true) {\n'); 1164 buffer.add('while (true) {\n');
1147 indent++; 1165 indent++;
1148 } 1166 }
1149 1167
1150 void endLoop(HBasicBlock block) { 1168 void endLoop(HBasicBlock block) {
1151 indent--; 1169 indent--;
1152 addIndentation(); 1170 addIndentation();
1153 buffer.add('}\n'); // Close 'while' loop. 1171 buffer.add('}\n'); // Close 'while' loop.
1154 } 1172 }
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
1300 void endBailoutSwitch() { 1318 void endBailoutSwitch() {
1301 indent--; // Close 'case'. 1319 indent--; // Close 'case'.
1302 indent--; 1320 indent--;
1303 addIndentation(); 1321 addIndentation();
1304 buffer.add('}\n'); // Close 'switch'. 1322 buffer.add('}\n'); // Close 'switch'.
1305 } 1323 }
1306 1324
1307 // Adds a "$" in front of names of labels from the original source. 1325 // Adds a "$" in front of names of labels from the original source.
1308 // This avoids conflicts with labels introduced by bailouts, which 1326 // This avoids conflicts with labels introduced by bailouts, which
1309 // starts with a non-"$" character. 1327 // starts with a non-"$" character.
1310 void addLabel(SourceString label) { 1328 void addLabel(LabelElement label) {
1311 buffer.add("\$$label"); 1329 buffer.add("\$${label.labelName}");
1312 } 1330 }
1313 1331
1314 void beginLoop(HBasicBlock block) { 1332 void beginLoop(HBasicBlock block) {
1315 // TODO(ngeoffray): Don't put labels on loops that don't bailout. 1333 // TODO(ngeoffray): Don't put labels on loops that don't bailout.
1316 String newLabel = pushLabel(); 1334 String newLabel = pushLabel();
1317 if (block.hasBailouts()) { 1335 if (block.hasBailouts()) {
1318 startBailoutCase(block.bailouts, const <HBailoutTarget>[]); 1336 startBailoutCase(block.bailouts, const <HBailoutTarget>[]);
1319 } 1337 }
1320 1338
1321 addIndentation(); 1339 addIndentation();
1322 for (SourceString label in block.loopInformation.labels) { 1340 for (LabelElement label in block.loopInformation.labels) {
1323 addLabel(label); 1341 addLabel(label);
1324 buffer.add(":"); 1342 buffer.add(":");
1325 } 1343 }
1326 buffer.add('$newLabel: while (true) {\n'); 1344 buffer.add('$newLabel: while (true) {\n');
1327 indent++; 1345 indent++;
1328 1346
1329 if (block.hasBailouts()) { 1347 if (block.hasBailouts()) {
1330 startBailoutSwitch(); 1348 startBailoutSwitch();
1331 } 1349 }
1332 } 1350 }
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
1405 startBailoutSwitch(); 1423 startBailoutSwitch();
1406 } 1424 }
1407 } 1425 }
1408 1426
1409 void endElse(HIf node) { 1427 void endElse(HIf node) {
1410 if (node.elseBlock.hasBailouts()) { 1428 if (node.elseBlock.hasBailouts()) {
1411 endBailoutSwitch(); 1429 endBailoutSwitch();
1412 } 1430 }
1413 } 1431 }
1414 } 1432 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698