Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 final JavaScriptBackend backend; | 6 final JavaScriptBackend backend; |
| 7 SsaCodeGeneratorTask(JavaScriptBackend backend) | 7 SsaCodeGeneratorTask(JavaScriptBackend backend) |
| 8 : this.backend = backend, | 8 : this.backend = backend, |
| 9 super(backend.compiler); | 9 super(backend.compiler); |
| 10 String get name() => 'SSA code generator'; | 10 String get name() => 'SSA code generator'; |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 479 declaredVariables.add(variableName); | 479 declaredVariables.add(variableName); |
| 480 buffer.add("var "); | 480 buffer.add("var "); |
| 481 } | 481 } |
| 482 buffer.add(variableName); | 482 buffer.add(variableName); |
| 483 } | 483 } |
| 484 | 484 |
| 485 void declareInstruction(HInstruction instruction) { | 485 void declareInstruction(HInstruction instruction) { |
| 486 declareVariable(variableNames.getName(instruction)); | 486 declareVariable(variableNames.getName(instruction)); |
| 487 } | 487 } |
| 488 | 488 |
| 489 // For simple updates of the form 'i = i op constant' generate | |
| 490 // 'i op= constant' instead. | |
| 491 bool handleSimpleUpdateDefinition(HInstruction instruction, String name) { | |
| 492 // If the variable is not declared the short update syntax cannot | |
| 493 // be used since it is a declaration and not an update. | |
| 494 if (!isVariableDeclared(name)) return false; | |
|
ngeoffray
2012/06/04 13:24:00
I think you should check that it's not delayed eit
Mads Ager (google)
2012/06/04 15:27:18
Yes, went for marking a variable declared when we
| |
| 495 | |
| 496 // Extract the operation and whether or not it is commutative. | |
|
kasperl
2012/06/04 13:12:56
I guess you considered moving some of this code to
Mads Ager (google)
2012/06/04 13:21:45
Yes, I decided to keep it local since I only consi
| |
| 497 var operation; | |
|
ngeoffray
2012/06/04 13:24:00
Please use the Operation class in lib/compiler/imp
Mads Ager (google)
2012/06/04 15:27:18
In principle that would be very nice. In practice
| |
| 498 var isCommutative = instruction is HAdd || instruction is HMultiply; | |
|
kasperl
2012/06/04 13:12:56
Make this start out by being false and push the up
Mads Ager (google)
2012/06/04 13:21:45
Yes! Done.
| |
| 499 if (instruction is HAdd) { | |
| 500 operation = '+'; | |
| 501 } else if (instruction is HMultiply) { | |
| 502 operation = '*'; | |
| 503 } else if (instruction is HSubtract) { | |
| 504 operation = '-'; | |
| 505 } else if (instruction is HDivide) { | |
| 506 operation = '/'; | |
| 507 } else { | |
| 508 return false; | |
| 509 } | |
| 510 | |
| 511 // Is it a simple builtin operation involving constant numbers? | |
| 512 if (instruction.builtin && instruction.inputs.length == 3) { | |
| 513 var left = instruction.inputs[1]; | |
| 514 var right = instruction.inputs[2]; | |
| 515 if (left.isConstantNumber() && isCommutative) { | |
| 516 var tmp = right; | |
| 517 right = left; | |
| 518 left = tmp; | |
| 519 } else if (!right.isConstantNumber()) { | |
| 520 return false; | |
| 521 } | |
| 522 // Right is constant number. | |
| 523 var value = right.constant.value; | |
| 524 // Check that left is a phi with the same name as the definition | |
| 525 // and emit the short update definition if it is. | |
| 526 if (left is HPhi && | |
|
ngeoffray
2012/06/04 13:24:00
I think you can remove the check. You only need to
Mads Ager (google)
2012/06/04 15:27:18
Great, thanks!
| |
| 527 variableNames.hasName(left) && | |
| 528 variableNames.getName(left) == name) { | |
| 529 if ((operation == '+') && right.constant.value == 1) { | |
|
kasperl
2012/06/04 13:12:56
Looks a bit fishy that you have () around one == (
Mads Ager (google)
2012/06/04 13:21:45
Yes, that looks weird. Updated to consistently use
| |
| 530 buffer.add('++'); | |
| 531 declareInstruction(instruction); | |
| 532 } else if ((operation == '-') && right.constant.value == 1) { | |
|
kasperl
2012/06/04 13:12:56
Ditto.
Mads Ager (google)
2012/06/04 13:21:45
Thanks. Done.
| |
| 533 buffer.add('--'); | |
| 534 declareInstruction(instruction); | |
| 535 } else { | |
| 536 declareInstruction(instruction); | |
| 537 buffer.add(' ${operation}= ${value}'); | |
| 538 } | |
| 539 return true; | |
| 540 } | |
| 541 } | |
| 542 return false; | |
| 543 } | |
| 544 | |
| 489 void define(HInstruction instruction) { | 545 void define(HInstruction instruction) { |
| 490 if (instruction is !HCheck && variableNames.hasName(instruction)) { | 546 if (instruction is !HCheck && variableNames.hasName(instruction)) { |
| 491 declareInstruction(instruction); | 547 if (!handleSimpleUpdateDefinition(instruction, |
| 492 buffer.add(" = "); | 548 variableNames.getName(instruction))) { |
|
kasperl
2012/06/04 13:12:56
I would throw variableNames.getName(instruction) i
Mads Ager (google)
2012/06/04 13:21:45
Done.
| |
| 493 visit(instruction, JSPrecedence.ASSIGNMENT_PRECEDENCE); | 549 declareInstruction(instruction); |
| 550 buffer.add(" = "); | |
| 551 visit(instruction, JSPrecedence.ASSIGNMENT_PRECEDENCE); | |
| 552 } | |
| 494 } else { | 553 } else { |
| 495 visit(instruction, JSPrecedence.STATEMENT_PRECEDENCE); | 554 visit(instruction, JSPrecedence.STATEMENT_PRECEDENCE); |
| 496 } | 555 } |
| 497 } | 556 } |
| 498 | 557 |
| 499 void use(HInstruction argument, int expectedPrecedenceForArgument) { | 558 void use(HInstruction argument, int expectedPrecedenceForArgument) { |
| 500 if (isGenerateAtUseSite(argument)) { | 559 if (isGenerateAtUseSite(argument)) { |
| 501 visit(argument, expectedPrecedenceForArgument); | 560 visit(argument, expectedPrecedenceForArgument); |
| 502 } else if (argument is HCheck) { | 561 } else if (argument is HCheck) { |
| 503 HCheck check = argument; | 562 HCheck check = argument; |
| (...skipping 383 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 887 } | 946 } |
| 888 // Flow based traversal. | 947 // Flow based traversal. |
| 889 if (node.isLoopHeader() && | 948 if (node.isLoopHeader() && |
| 890 node.loopInformation.loopBlockInformation !== currentBlockInformation) { | 949 node.loopInformation.loopBlockInformation !== currentBlockInformation) { |
| 891 beginLoop(node); | 950 beginLoop(node); |
| 892 } | 951 } |
| 893 iterateBasicBlock(node); | 952 iterateBasicBlock(node); |
| 894 } | 953 } |
| 895 | 954 |
| 896 void emitAssignment(String destination, String source) { | 955 void emitAssignment(String destination, String source) { |
| 897 if (isGeneratingExpression()) { | 956 if (isGeneratingExpression()) { |
| 898 addExpressionSeparator(); | 957 addExpressionSeparator(); |
| 899 } else { | 958 } else { |
| 900 addIndentation(); | 959 addIndentation(); |
| 901 } | 960 } |
| 902 declareVariable(destination); | 961 declareVariable(destination); |
| 903 buffer.add(' = $source'); | 962 buffer.add(' = $source'); |
| 904 if (!isGeneratingExpression()) { | 963 if (!isGeneratingExpression()) { |
| 905 buffer.add(';\n'); | 964 buffer.add(';\n'); |
| 906 } | 965 } |
| 907 } | 966 } |
| (...skipping 1597 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2505 startBailoutSwitch(); | 2564 startBailoutSwitch(); |
| 2506 } | 2565 } |
| 2507 } | 2566 } |
| 2508 | 2567 |
| 2509 void endLabeledBlock(HLabeledBlockInformation labeledBlockInfo) { | 2568 void endLabeledBlock(HLabeledBlockInformation labeledBlockInfo) { |
| 2510 if (labeledBlockInfo.body.start.hasGuards()) { | 2569 if (labeledBlockInfo.body.start.hasGuards()) { |
| 2511 endBailoutSwitch(); | 2570 endBailoutSwitch(); |
| 2512 } | 2571 } |
| 2513 } | 2572 } |
| 2514 } | 2573 } |
| OLD | NEW |