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 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 indent++; | 626 indent++; |
627 generateStatements(elseGraph); | 627 generateStatements(elseGraph); |
628 indent--; | 628 indent--; |
629 addIndented("}"); | 629 addIndented("}"); |
630 } | 630 } |
631 buffer.add("\n"); | 631 buffer.add("\n"); |
632 } | 632 } |
633 return true; | 633 return true; |
634 } | 634 } |
635 | 635 |
| 636 bool visitSwitchInfo(HSwitchBlockInformation info) { |
| 637 bool isExpression = isJSExpression(info.expression); |
| 638 if (!isExpression) { |
| 639 generateStatements(info.expression); |
| 640 } |
| 641 addIndentation(); |
| 642 for (LabelElement label in info.labels) { |
| 643 if (label.isTarget) { |
| 644 writeLabel(label); |
| 645 buffer.add(":"); |
| 646 } |
| 647 } |
| 648 addIndented("switch ("); |
| 649 if (isExpression) { |
| 650 generateExpression(info.expression); |
| 651 } else { |
| 652 use(info.expression.conditionExpression, |
| 653 JSPrecedence.EXPRESSION_PRECEDENCE); |
| 654 } |
| 655 buffer.add(") {\n"); |
| 656 indent++; |
| 657 for (int i = 0; i < info.matchExpressions.length; i++) { |
| 658 for (Constant constant in info.matchExpressions[i]) { |
| 659 addIndented("case "); |
| 660 generateConstant(constant); |
| 661 buffer.add(":\n"); |
| 662 } |
| 663 if (i == info.matchExpressions.length - 1 && info.hasDefault) { |
| 664 addIndented("default:\n"); |
| 665 } |
| 666 indent++; |
| 667 generateStatements(info.statements[i]); |
| 668 indent--; |
| 669 } |
| 670 indent--; |
| 671 addIndented("}\n"); |
| 672 return true; |
| 673 } |
| 674 |
636 bool visitSequenceInfo(HStatementSequenceInformation info) { | 675 bool visitSequenceInfo(HStatementSequenceInformation info) { |
637 return false; | 676 return false; |
638 } | 677 } |
639 | 678 |
640 bool visitSubGraphInfo(HSubGraphBlockInformation info) { | 679 bool visitSubGraphInfo(HSubGraphBlockInformation info) { |
641 visitSubGraph(info.subGraph); | 680 visitSubGraph(info.subGraph); |
642 return true; | 681 return true; |
643 } | 682 } |
644 | 683 |
645 bool visitSubExpressionInfo(HSubExpressionBlockInformation info) { | 684 bool visitSubExpressionInfo(HSubExpressionBlockInformation info) { |
(...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1558 // We can't use 'visitArguments', since our arguments start at input[0]. | 1597 // We can't use 'visitArguments', since our arguments start at input[0]. |
1559 List<HInstruction> inputs = node.inputs; | 1598 List<HInstruction> inputs = node.inputs; |
1560 for (int i = 0; i < inputs.length; i++) { | 1599 for (int i = 0; i < inputs.length; i++) { |
1561 if (i != 0) buffer.add(', '); | 1600 if (i != 0) buffer.add(', '); |
1562 use(inputs[i], JSPrecedence.ASSIGNMENT_PRECEDENCE); | 1601 use(inputs[i], JSPrecedence.ASSIGNMENT_PRECEDENCE); |
1563 } | 1602 } |
1564 buffer.add(')'); | 1603 buffer.add(')'); |
1565 endExpression(JSPrecedence.MEMBER_PRECEDENCE); | 1604 endExpression(JSPrecedence.MEMBER_PRECEDENCE); |
1566 } | 1605 } |
1567 | 1606 |
1568 visitConstant(HConstant node) { | 1607 void generateConstant(Constant constant) { |
1569 assert(isGenerateAtUseSite(node)); | |
1570 // TODO(floitsch): the compile-time constant handler and the codegen | 1608 // TODO(floitsch): the compile-time constant handler and the codegen |
1571 // need to work together to avoid the parenthesis. See r4928 for an | 1609 // need to work together to avoid the parenthesis. See r4928 for an |
1572 // implementation that still dealt with precedence. | 1610 // implementation that still dealt with precedence. |
1573 ConstantHandler handler = compiler.constantHandler; | 1611 ConstantHandler handler = compiler.constantHandler; |
1574 String name = handler.getNameForConstant(node.constant); | 1612 String name = handler.getNameForConstant(constant); |
1575 if (name === null) { | 1613 if (name === null) { |
1576 assert(!node.constant.isObject()); | 1614 assert(!constant.isObject()); |
1577 if (node.constant.isNum() | 1615 if (constant.isNum() |
1578 && expectedPrecedence == JSPrecedence.MEMBER_PRECEDENCE) { | 1616 && expectedPrecedence == JSPrecedence.MEMBER_PRECEDENCE) { |
1579 buffer.add('('); | 1617 buffer.add('('); |
1580 handler.writeConstant(buffer, node.constant); | 1618 handler.writeConstant(buffer, constant); |
1581 buffer.add(')'); | 1619 buffer.add(')'); |
1582 } else { | 1620 } else { |
1583 handler.writeConstant(buffer, node.constant); | 1621 handler.writeConstant(buffer, constant); |
1584 } | 1622 } |
1585 } else { | 1623 } else { |
1586 buffer.add(compiler.namer.CURRENT_ISOLATE); | 1624 buffer.add(compiler.namer.CURRENT_ISOLATE); |
1587 buffer.add("."); | 1625 buffer.add("."); |
1588 buffer.add(name); | 1626 buffer.add(name); |
1589 } | 1627 } |
| 1628 |
| 1629 } |
| 1630 |
| 1631 visitConstant(HConstant node) { |
| 1632 assert(isGenerateAtUseSite(node)); |
| 1633 generateConstant(node.constant); |
1590 } | 1634 } |
1591 | 1635 |
1592 visitLoopBranch(HLoopBranch node) { | 1636 visitLoopBranch(HLoopBranch node) { |
1593 if (subGraph !== null && node.block === subGraph.end) { | 1637 if (subGraph !== null && node.block === subGraph.end) { |
1594 // We are generating code for a loop condition. | 1638 // We are generating code for a loop condition. |
1595 // If doing this as part of a SubGraph traversal, the | 1639 // If doing this as part of a SubGraph traversal, the |
1596 // calling code will handle the control flow logic. | 1640 // calling code will handle the control flow logic. |
1597 | 1641 |
1598 // If we are generating the subgraph as an expression, the | 1642 // If we are generating the subgraph as an expression, the |
1599 // condition will be generated as the expression. | 1643 // condition will be generated as the expression. |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1787 for (int i = 0; i < indent; i++) { | 1831 for (int i = 0; i < indent; i++) { |
1788 buffer.add(' '); | 1832 buffer.add(' '); |
1789 } | 1833 } |
1790 } | 1834 } |
1791 | 1835 |
1792 void addIndented(String text) { | 1836 void addIndented(String text) { |
1793 addIndentation(); | 1837 addIndentation(); |
1794 buffer.add(text); | 1838 buffer.add(text); |
1795 } | 1839 } |
1796 | 1840 |
| 1841 void visitSwitch(HSwitch node) { |
| 1842 // Switches are handled using [visitSwitchInfo]. |
| 1843 } |
| 1844 |
1797 void visitStatic(HStatic node) { | 1845 void visitStatic(HStatic node) { |
1798 world.registerStaticUse(node.element); | 1846 world.registerStaticUse(node.element); |
1799 buffer.add(compiler.namer.isolateAccess(node.element)); | 1847 buffer.add(compiler.namer.isolateAccess(node.element)); |
1800 } | 1848 } |
1801 | 1849 |
1802 void visitStaticStore(HStaticStore node) { | 1850 void visitStaticStore(HStaticStore node) { |
1803 world.registerStaticUse(node.element); | 1851 world.registerStaticUse(node.element); |
1804 beginExpression(JSPrecedence.ASSIGNMENT_PRECEDENCE); | 1852 beginExpression(JSPrecedence.ASSIGNMENT_PRECEDENCE); |
1805 buffer.add(compiler.namer.isolateAccess(node.element)); | 1853 buffer.add(compiler.namer.isolateAccess(node.element)); |
1806 buffer.add(' = '); | 1854 buffer.add(' = '); |
(...skipping 764 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2571 startBailoutSwitch(); | 2619 startBailoutSwitch(); |
2572 } | 2620 } |
2573 } | 2621 } |
2574 | 2622 |
2575 void endLabeledBlock(HLabeledBlockInformation labeledBlockInfo) { | 2623 void endLabeledBlock(HLabeledBlockInformation labeledBlockInfo) { |
2576 if (labeledBlockInfo.body.start.hasGuards()) { | 2624 if (labeledBlockInfo.body.start.hasGuards()) { |
2577 endBailoutSwitch(); | 2625 endBailoutSwitch(); |
2578 } | 2626 } |
2579 } | 2627 } |
2580 } | 2628 } |
OLD | NEW |