| 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 /** | 7 /** |
| 8 * The [LiveRange] class covers a range where an instruction is live. | 8 * The [LiveRange] class covers a range where an instruction is live. |
| 9 */ | 9 */ |
| 10 class LiveRange { | 10 class LiveRange { |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 copyHandlers.putIfAbsent(block, () => new CopyHandler()); | 434 copyHandlers.putIfAbsent(block, () => new CopyHandler()); |
| 435 handler.addAssignment(source, destination); | 435 handler.addAssignment(source, destination); |
| 436 } | 436 } |
| 437 } | 437 } |
| 438 | 438 |
| 439 /** | 439 /** |
| 440 * Allocates variable names for instructions, making sure they don't collide. | 440 * Allocates variable names for instructions, making sure they don't collide. |
| 441 */ | 441 */ |
| 442 class VariableNamer { | 442 class VariableNamer { |
| 443 final VariableNames names; | 443 final VariableNames names; |
| 444 final Compiler compiler; |
| 444 final Set<String> usedNames; | 445 final Set<String> usedNames; |
| 445 final Map<Element, String> parameterNames; | 446 final Map<Element, String> parameterNames; |
| 446 final List<String> freeTemporaryNames; | 447 final List<String> freeTemporaryNames; |
| 447 int temporaryIndex = 0; | 448 int temporaryIndex = 0; |
| 448 | 449 |
| 449 VariableNamer(LiveEnvironment environment, this.names, this.parameterNames) | 450 VariableNamer(LiveEnvironment environment, |
| 451 this.names, |
| 452 this.parameterNames, |
| 453 this.compiler) |
| 450 : usedNames = new Set<String>(), | 454 : usedNames = new Set<String>(), |
| 451 freeTemporaryNames = new List<String>() { | 455 freeTemporaryNames = new List<String>() { |
| 452 // [VariableNames.swapTemp] is used when there is a cycle in a copy handler. | 456 // [VariableNames.swapTemp] is used when there is a cycle in a copy handler. |
| 453 // Therefore we make sure no one uses it. | 457 // Therefore we make sure no one uses it. |
| 454 usedNames.add(names.swapTemp); | 458 usedNames.add(names.swapTemp); |
| 455 // [VariableNames.stateName] is being used throughout a bailout function. | 459 // [VariableNames.stateName] is being used throughout a bailout function. |
| 456 // Whenever a bailout-target is reached we set the state-variable to 0. We | 460 // Whenever a bailout-target is reached we set the state-variable to 0. We |
| 457 // must therefore not have any local variable that could clash with the | 461 // must therefore not have any local variable that could clash with the |
| 458 // state variable. | 462 // state variable. |
| 459 // Therefore we make sure no one uses it at any time. | 463 // Therefore we make sure no one uses it at any time. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 // Special case this instruction to use the name of its | 508 // Special case this instruction to use the name of its |
| 505 // input if it has one. | 509 // input if it has one. |
| 506 var temp = instruction; | 510 var temp = instruction; |
| 507 do { | 511 do { |
| 508 temp = temp.checkedInput; | 512 temp = temp.checkedInput; |
| 509 name = names.ownName[temp]; | 513 name = names.ownName[temp]; |
| 510 } while (name == null && temp is HCheck); | 514 } while (name == null && temp is HCheck); |
| 511 if (name != null) return addAllocatedName(instruction, name); | 515 if (name != null) return addAllocatedName(instruction, name); |
| 512 } | 516 } |
| 513 | 517 |
| 514 // The dom/html libraries have inline JS code that reference» | 518 if (instruction is HThis) { |
| 515 // parameter names directly. Long-term such code will be rejected. | 519 name = "this"; |
| 516 // Now, just don't mangle the parameter name. | 520 if (instruction.sourceElement != null) { |
| 517 if (instruction is HParameterValue | 521 Element cls = instruction.sourceElement.getEnclosingClass(); |
| 522 // Change the name of [:this:] in a method of a foreign class |
| 523 // to use the first parameter of the method, which is the |
| 524 // actual receiver. |
| 525 if (compiler.isForeignClass(cls)) { |
| 526 name = "primitive"; |
| 527 } |
| 528 } |
| 529 } else if (instruction is HParameterValue |
| 518 && instruction.sourceElement.enclosingElement.isNative()) { | 530 && instruction.sourceElement.enclosingElement.isNative()) { |
| 531 // The dom/html libraries have inline JS code that reference |
| 532 // parameter names directly. Long-term such code will be rejected. |
| 533 // Now, just don't mangle the parameter name. |
| 519 name = instruction.sourceElement.name.slowToString(); | 534 name = instruction.sourceElement.name.slowToString(); |
| 520 } else if (instruction.sourceElement != null) { | 535 } else if (instruction.sourceElement != null) { |
| 521 name = allocateWithHint(instruction.sourceElement.name.slowToString()); | 536 name = allocateWithHint(instruction.sourceElement.name.slowToString()); |
| 522 } else { | 537 } else { |
| 523 // We could not find an element for the instruction. If the | 538 // We could not find an element for the instruction. If the |
| 524 // instruction is used by a phi, try to use the name of the phi. | 539 // instruction is used by a phi, try to use the name of the phi. |
| 525 // Otherwise, just allocate a temporary name. | 540 // Otherwise, just allocate a temporary name. |
| 526 HPhi phi = firstPhiUserWithElement(instruction); | 541 HPhi phi = firstPhiUserWithElement(instruction); |
| 527 if (phi != null) { | 542 if (phi != null) { |
| 528 name = allocateWithHint(phi.sourceElement.name.slowToString()); | 543 name = allocateWithHint(phi.sourceElement.name.slowToString()); |
| 529 } else { | 544 } else { |
| 530 name = allocateTemporary(); | 545 name = allocateTemporary(); |
| 531 } | 546 } |
| 532 } | 547 } |
| 533 | |
| 534 return addAllocatedName(instruction, name); | 548 return addAllocatedName(instruction, name); |
| 535 } | 549 } |
| 536 | 550 |
| 537 String addAllocatedName(HInstruction instruction, String name) { | 551 String addAllocatedName(HInstruction instruction, String name) { |
| 538 if (instruction is HParameterValue) { | 552 if (instruction is HParameterValue && name != 'this') { |
| 539 parameterNames[instruction.sourceElement] = name; | 553 parameterNames[instruction.sourceElement] = name; |
| 540 } | 554 } |
| 541 usedNames.add(name); | 555 usedNames.add(name); |
| 542 names.addNameUsed(name); | 556 names.addNameUsed(name); |
| 543 names.ownName[instruction] = name; | 557 names.ownName[instruction] = name; |
| 544 return name; | 558 return name; |
| 545 } | 559 } |
| 546 | 560 |
| 547 /** | 561 /** |
| 548 * Frees [instruction]'s name so it can be used for other instructions. | 562 * Frees [instruction]'s name so it can be used for other instructions. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 592 parameterNames) | 606 parameterNames) |
| 593 : this.names = new VariableNames(parameterNames), | 607 : this.names = new VariableNames(parameterNames), |
| 594 this.parameterNames = parameterNames; | 608 this.parameterNames = parameterNames; |
| 595 | 609 |
| 596 void visitGraph(HGraph graph) { | 610 void visitGraph(HGraph graph) { |
| 597 visitDominatorTree(graph); | 611 visitDominatorTree(graph); |
| 598 } | 612 } |
| 599 | 613 |
| 600 void visitBasicBlock(HBasicBlock block) { | 614 void visitBasicBlock(HBasicBlock block) { |
| 601 VariableNamer namer = new VariableNamer( | 615 VariableNamer namer = new VariableNamer( |
| 602 liveInstructions[block], names, parameterNames); | 616 liveInstructions[block], names, parameterNames, compiler); |
| 603 | 617 |
| 604 block.forEachPhi((HPhi phi) { | 618 block.forEachPhi((HPhi phi) { |
| 605 handlePhi(phi, namer); | 619 handlePhi(phi, namer); |
| 606 }); | 620 }); |
| 607 | 621 |
| 608 block.forEachInstruction((HInstruction instruction) { | 622 block.forEachInstruction((HInstruction instruction) { |
| 609 handleInstruction(instruction, namer); | 623 handleInstruction(instruction, namer); |
| 610 }); | 624 }); |
| 611 } | 625 } |
| 612 | 626 |
| 613 /** | 627 /** |
| 614 * Returns whether [instruction] needs a name. Instructions that | 628 * Returns whether [instruction] needs a name. Instructions that |
| 615 * have no users or that are generated at use site does not need a name. | 629 * have no users or that are generated at use site does not need a name. |
| 616 */ | 630 */ |
| 617 bool needsName(HInstruction instruction) { | 631 bool needsName(HInstruction instruction) { |
| 618 // TODO(ngeoffray): locals/parameters are being generated at use site, | 632 // TODO(ngeoffray): locals/parameters are being generated at use site, |
| 619 // but we need a name for them. We should probably not make | 633 // but we need a name for them. We should probably not make |
| 620 // them generate at use site to make things simpler. | 634 // them generate at use site to make things simpler. |
| 621 if (instruction is HLocalValue && instruction is !HThis) return true; | 635 if (instruction is HLocalValue) return true; |
| 622 if (instruction.usedBy.isEmpty) return false; | 636 if (instruction.usedBy.isEmpty) return false; |
| 623 if (generateAtUseSite.contains(instruction)) return false; | 637 if (generateAtUseSite.contains(instruction)) return false; |
| 624 // A [HCheck] instruction that has control flow needs a name only if its | 638 // A [HCheck] instruction that has control flow needs a name only if its |
| 625 // checked input needs a name (e.g. a check [HConstant] does not | 639 // checked input needs a name (e.g. a check [HConstant] does not |
| 626 // need a name). | 640 // need a name). |
| 627 if (instruction is HCheck && instruction.isControlFlow()) { | 641 if (instruction is HCheck && instruction.isControlFlow()) { |
| 628 HCheck check = instruction; | 642 HCheck check = instruction; |
| 629 return needsName(instruction.checkedInput); | 643 return needsName(instruction.checkedInput); |
| 630 } | 644 } |
| 631 return true; | 645 return true; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 675 if (!needsName(input)) { | 689 if (!needsName(input)) { |
| 676 names.addAssignment(predecessor, input, phi); | 690 names.addAssignment(predecessor, input, phi); |
| 677 } else { | 691 } else { |
| 678 names.addCopy(predecessor, input, phi); | 692 names.addCopy(predecessor, input, phi); |
| 679 } | 693 } |
| 680 } | 694 } |
| 681 | 695 |
| 682 namer.allocateName(phi); | 696 namer.allocateName(phi); |
| 683 } | 697 } |
| 684 } | 698 } |
| OLD | NEW |