| 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 part of ssa; | 5 part of ssa; |
| 6 | 6 |
| 7 class SsaCodeGeneratorTask extends CompilerTask { | 7 class SsaCodeGeneratorTask extends CompilerTask { |
| 8 | 8 |
| 9 final JavaScriptBackend backend; | 9 final JavaScriptBackend backend; |
| 10 | 10 |
| (...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 HSubExpressionBlockInformation expressionSubGraph = expression; | 457 HSubExpressionBlockInformation expressionSubGraph = expression; |
| 458 visitSubGraph(expressionSubGraph.subExpression); | 458 visitSubGraph(expressionSubGraph.subExpression); |
| 459 expressionStack = oldExpressionStack; | 459 expressionStack = oldExpressionStack; |
| 460 isGeneratingExpression = oldIsGeneratingExpression; | 460 isGeneratingExpression = oldIsGeneratingExpression; |
| 461 if (sequenceElements.isEmpty) { | 461 if (sequenceElements.isEmpty) { |
| 462 // Happens when the initializer, condition or update of a loop is empty. | 462 // Happens when the initializer, condition or update of a loop is empty. |
| 463 return null; | 463 return null; |
| 464 } else if (sequenceElements.length == 1) { | 464 } else if (sequenceElements.length == 1) { |
| 465 return sequenceElements[0]; | 465 return sequenceElements[0]; |
| 466 } else { | 466 } else { |
| 467 return new js.Sequence(sequenceElements); | 467 js.Expression result = sequenceElements.removeLast(); |
| 468 while (sequenceElements.isNotEmpty) { |
| 469 result = new js.Binary(',', sequenceElements.removeLast(), result); |
| 470 } |
| 471 return result; |
| 468 } | 472 } |
| 469 } | 473 } |
| 470 | 474 |
| 471 /** | 475 /** |
| 472 * Only visits the arguments starting at inputs[HInvoke.ARGUMENTS_OFFSET]. | 476 * Only visits the arguments starting at inputs[HInvoke.ARGUMENTS_OFFSET]. |
| 473 */ | 477 */ |
| 474 List<js.Expression> visitArguments(List<HInstruction> inputs, | 478 List<js.Expression> visitArguments(List<HInstruction> inputs, |
| 475 {int start: HInvoke.ARGUMENTS_OFFSET}) { | 479 {int start: HInvoke.ARGUMENTS_OFFSET}) { |
| 476 assert(inputs.length >= start); | 480 assert(inputs.length >= start); |
| 477 List<js.Expression> result = <js.Expression>[]; | 481 List<js.Expression> result = <js.Expression>[]; |
| (...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 772 if (isConditionExpression && | 776 if (isConditionExpression && |
| 773 info.updates != null && isJSExpression(info.updates)) { | 777 info.updates != null && isJSExpression(info.updates)) { |
| 774 // If we have an updates graph, and it's expressible as an | 778 // If we have an updates graph, and it's expressible as an |
| 775 // expression, generate a for-loop. | 779 // expression, generate a for-loop. |
| 776 js.Expression jsInitialization = null; | 780 js.Expression jsInitialization = null; |
| 777 if (initialization != null) { | 781 if (initialization != null) { |
| 778 int delayedVariablesCount = collectedVariableDeclarations.length; | 782 int delayedVariablesCount = collectedVariableDeclarations.length; |
| 779 jsInitialization = generateExpression(initialization); | 783 jsInitialization = generateExpression(initialization); |
| 780 if (!shouldGroupVarDeclarations && | 784 if (!shouldGroupVarDeclarations && |
| 781 delayedVariablesCount < collectedVariableDeclarations.length) { | 785 delayedVariablesCount < collectedVariableDeclarations.length) { |
| 782 // We just added a new delayed variable-declaration. See if we | 786 // We just added a new delayed variable-declaration. See if we can |
| 783 // can put in a 'var' in front of the initialization to make it | 787 // put in a 'var' in front of the initialization to make it go |
| 784 // go away. | 788 // away. We walk the 'tree' of comma-operators to find the |
| 785 List<js.Expression> expressions; | 789 // expressions and see if they are all assignments that can be |
| 786 if (jsInitialization is js.Sequence) { | 790 // converted into declarations. |
| 787 js.Sequence sequence = jsInitialization; | 791 |
| 788 expressions = sequence.expressions; | 792 List<js.Assignment> assignments; |
| 789 } else { | 793 |
| 790 expressions = <js.Expression>[jsInitialization]; | 794 bool allSimpleAssignments(js.Expression expression) { |
| 791 } | |
| 792 bool canTransformToVariableDeclaration = true; | |
| 793 for (js.Expression expression in expressions) { | |
| 794 bool expressionIsVariableAssignment = false; | |
| 795 if (expression is js.Assignment) { | 795 if (expression is js.Assignment) { |
| 796 js.Assignment assignment = expression; | 796 js.Assignment assignment = expression; |
| 797 if (assignment.leftHandSide is js.VariableUse && | 797 if (assignment.leftHandSide is js.VariableUse && |
| 798 !assignment.isCompound) { | 798 !assignment.isCompound) { |
| 799 expressionIsVariableAssignment = true; | 799 if (assignments == null) assignments = <js.Assignment>[]; |
| 800 assignments.add(expression); |
| 801 return true; |
| 800 } | 802 } |
| 803 } else if (expression.isCommaOperator) { |
| 804 js.Binary binary = expression; |
| 805 return allSimpleAssignments(binary.left) |
| 806 && allSimpleAssignments(binary.right); |
| 801 } | 807 } |
| 802 if (!expressionIsVariableAssignment) { | 808 return false; |
| 803 canTransformToVariableDeclaration = false; | |
| 804 break; | |
| 805 } | |
| 806 } | 809 } |
| 807 if (canTransformToVariableDeclaration) { | 810 |
| 811 if (allSimpleAssignments(jsInitialization)) { |
| 808 List<js.VariableInitialization> inits = | 812 List<js.VariableInitialization> inits = |
| 809 <js.VariableInitialization>[]; | 813 <js.VariableInitialization>[]; |
| 810 for (js.Assignment assignment in expressions) { | 814 for (js.Assignment assignment in assignments) { |
| 811 String id = (assignment.leftHandSide as js.VariableUse).name; | 815 String id = (assignment.leftHandSide as js.VariableUse).name; |
| 812 js.Node declaration = new js.VariableDeclaration(id); | 816 js.Node declaration = new js.VariableDeclaration(id); |
| 813 inits.add(new js.VariableInitialization(declaration, | 817 inits.add(new js.VariableInitialization(declaration, |
| 814 assignment.value)); | 818 assignment.value)); |
| 815 collectedVariableDeclarations.remove(id); | 819 collectedVariableDeclarations.remove(id); |
| 816 declaredLocals.add(id); | 820 declaredLocals.add(id); |
| 817 } | 821 } |
| 818 jsInitialization = new js.VariableDeclarationList(inits); | 822 jsInitialization = new js.VariableDeclarationList(inits); |
| 819 } | 823 } |
| 820 } | 824 } |
| (...skipping 1854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2675 js.PropertyAccess accessHelper(String name) { | 2679 js.PropertyAccess accessHelper(String name) { |
| 2676 Element helper = compiler.findHelper(name); | 2680 Element helper = compiler.findHelper(name); |
| 2677 if (helper == null) { | 2681 if (helper == null) { |
| 2678 // For mocked-up tests. | 2682 // For mocked-up tests. |
| 2679 return js.js('(void 0).$name'); | 2683 return js.js('(void 0).$name'); |
| 2680 } | 2684 } |
| 2681 world.registerStaticUse(helper); | 2685 world.registerStaticUse(helper); |
| 2682 return backend.namer.elementAccess(helper); | 2686 return backend.namer.elementAccess(helper); |
| 2683 } | 2687 } |
| 2684 } | 2688 } |
| OLD | NEW |