* Tactic T3: evict a neighbour instruction. */
| 652 | * Tactic T3: evict a neighbour instruction. |
| 653 | */ |
| 654 | static Patch *tactic_T3(Binary &B, Instr *I, const Trampoline *T) |
| 655 | { |
| 656 | if (I->size == 1) |
| 657 | return tactic_T3b(B, I, T); |
| 658 | if (I->size >= JMP_SIZE || !option_tactic_T3 || !canPatch(I)) |
| 659 | return nullptr; |
| 660 | |
| 661 | // Step (1): find nearest instruction at +SHORT_JMP_MAX (or |
| 662 | // -SHORT_JMP_MIN) bytes ahead. |
| 663 | Instr *J = I; |
| 664 | while (true) |
| 665 | { |
| 666 | Instr *K = J->next(); |
| 667 | if (K == nullptr) |
| 668 | break; |
| 669 | if (K->addr - (I->addr + /*sizeof(short jmp)=*/2) > SHORT_JMP_MAX) |
| 670 | break; |
| 671 | J = K; |
| 672 | } |
| 673 | |
| 674 | // Step (2): Iterate through all neighbour instructions: |
| 675 | Patch *P = nullptr; |
| 676 | const Alloc *A = nullptr; |
| 677 | intptr_t addr = 0; |
| 678 | for (; P == nullptr; J = J->prev()) |
| 679 | { |
| 680 | if (J == I) |
| 681 | continue; |
| 682 | if (J == nullptr) |
| 683 | break; |
| 684 | if (!option_tactic_backward_T3 && J->addr < I->addr) |
| 685 | break; |
| 686 | if ((I->addr + /*sizeof(short jmp)=*/2) - (J->addr + J->size -1) > |
| 687 | -SHORT_JMP_MIN) |
| 688 | { |
| 689 | // Out-of-range, so give up... :( |
| 690 | break; |
| 691 | } |
| 692 | |
| 693 | switch (J->STATE[0]) |
| 694 | { |
| 695 | case STATE_INSTRUCTION: case STATE_FREE: |
| 696 | break; |
| 697 | case STATE_PATCHED: |
| 698 | case STATE_PATCHED | STATE_LOCKED: |
| 699 | { |
| 700 | if (J->STATE[J->size-1] == STATE_FREE) |
| 701 | break; |
| 702 | continue; |
| 703 | } |
| 704 | default: |
| 705 | continue; |
| 706 | } |
| 707 | |
| 708 | for (int i = 0; i < (int)J->size && P == nullptr; i++) |
| 709 | { |
| 710 | if (i == 0 && J->STATE[0] != STATE_FREE) |
| 711 | { |
no test coverage detected