|
40 | 40 | */
|
41 | 41 | package com.oracle.graal.python.compiler;
|
42 | 42 |
|
| 43 | +import java.util.ArrayDeque; |
43 | 44 | import java.util.ArrayList;
|
44 | 45 | import java.util.Arrays;
|
45 | 46 | import java.util.HashMap;
|
@@ -556,31 +557,120 @@ public int lineToBci(int line) {
|
556 | 557 | return afterFirst ? bestBci : -2;
|
557 | 558 | }
|
558 | 559 |
|
| 560 | + public enum JumpBlock { |
| 561 | + None(0, null), |
| 562 | + With(1, "the body of a with statement"), |
| 563 | + Loop(2, "the body of a for loop"), |
| 564 | + Try(3, "the body of a try statement"), |
| 565 | + Except(4, "an 'except' block as there's no exception"); |
| 566 | + |
| 567 | + public final int i; |
| 568 | + public final String error; |
| 569 | + public static final long BITS = 3; |
| 570 | + |
| 571 | + JumpBlock(int i, String error) { |
| 572 | + this.error = error; |
| 573 | + this.i = i; |
| 574 | + } |
| 575 | + |
| 576 | + private static final JumpBlock[] VALUES = JumpBlock.values(); |
| 577 | + |
| 578 | + public long push(long stack) { |
| 579 | + return stack << BITS | this.i; |
| 580 | + } |
| 581 | + |
| 582 | + public static long pop(long stack) { |
| 583 | + return stack >> BITS; |
| 584 | + } |
| 585 | + |
| 586 | + public static JumpBlock peek(long stack) { |
| 587 | + return VALUES[(int) (stack & ((1 << BITS) - 1))]; |
| 588 | + } |
| 589 | + } |
| 590 | + |
| 591 | + @CompilerDirectives.TruffleBoundary |
| 592 | + public void computeStackLevels(long[] blocks, int[] stackLevels) { |
| 593 | + assert code.length + 1 == blocks.length; |
| 594 | + assert stackLevels.length == code.length; |
| 595 | + Arrays.fill(stackLevels, -1); |
| 596 | + // stackLevels has the stack depth before the corresponding opcode executes |
| 597 | + stackLevels[0] = 0; |
| 598 | + // deque of bci |
| 599 | + ArrayDeque<Integer> todo = new ArrayDeque<>(); |
| 600 | + todo.addFirst(0); |
| 601 | + for (int i = 0; i < exceptionHandlerRanges.length; i += 4) { |
| 602 | + int handler = exceptionHandlerRanges[i + 2]; |
| 603 | + int stackAtHandler = exceptionHandlerRanges[i + 3]; |
| 604 | + // stack at handler + the exception |
| 605 | + stackLevels[handler] = stackAtHandler + 1; |
| 606 | + todo.addFirst(handler); |
| 607 | + } |
| 608 | + while (!todo.isEmpty()) { |
| 609 | + int bci = todo.removeLast(); |
| 610 | + int stackHere = stackLevels[bci]; |
| 611 | + assert stackHere >= 0; |
| 612 | + opCodeAt(code, bci, (ignored, op, oparg, followingArgs) -> { |
| 613 | + assert stackHere >= op.getNumberOfConsumedStackItems(oparg, followingArgs, true) : "failed at:" + op + ":" + bci; |
| 614 | + assert stackHere >= op.getNumberOfConsumedStackItems(oparg, followingArgs, false) : "failed at:" + op + ":" + bci; |
| 615 | + int stackWJump = op.getStackEffect(oparg, followingArgs, true); |
| 616 | + int stackWOJump = op.getStackEffect(oparg, followingArgs, false); |
| 617 | + int bciWJump = op.getNextBci(bci, oparg, true); |
| 618 | + int bciWOJump = op.getNextBci(bci, oparg, false); |
| 619 | + if (bciWJump == -1) { |
| 620 | + assert bciWOJump == bciWJump; |
| 621 | + return; |
| 622 | + } |
| 623 | + if (stackLevels[bciWJump] < 0) { |
| 624 | + stackLevels[bciWJump] = stackHere + stackWJump; |
| 625 | + if (op == OpCodes.GET_ITER) { |
| 626 | + System.out.println(stackLevels[bciWJump]); |
| 627 | + } |
| 628 | + todo.addFirst(bciWJump); |
| 629 | + } else { |
| 630 | + assert stackLevels[bciWJump] == stackHere + stackWJump; |
| 631 | + } |
| 632 | + if (bciWJump != bciWOJump) { |
| 633 | + if (stackLevels[bciWOJump] < 0) { |
| 634 | + stackLevels[bciWOJump] = stackHere + stackWOJump; |
| 635 | + todo.addFirst(bciWOJump); |
| 636 | + } else { |
| 637 | + assert stackLevels[bciWOJump] == stackHere + stackWOJump; |
| 638 | + } |
| 639 | + } |
| 640 | + }); |
| 641 | + } |
| 642 | + } |
| 643 | + |
559 | 644 | @FunctionalInterface
|
560 | 645 | public interface BytecodeAction {
|
561 | 646 | void run(int bci, OpCodes op, int oparg, byte[] followingArgs);
|
562 | 647 | }
|
563 | 648 |
|
564 |
| - public static void iterateBytecode(byte[] bytecode, BytecodeAction action) { |
| 649 | + // returns the following bci |
| 650 | + private static int opCodeAt(byte[] bytecode, int bci, BytecodeAction action) { |
565 | 651 | int oparg = 0;
|
566 |
| - for (int bci = 0; bci < bytecode.length;) { |
567 |
| - OpCodes op = OpCodes.fromOpCode(bytecode[bci]); |
568 |
| - if (op == OpCodes.EXTENDED_ARG) { |
569 |
| - oparg |= Byte.toUnsignedInt(bytecode[bci + 1]); |
570 |
| - oparg <<= 8; |
571 |
| - } else { |
572 |
| - byte[] followingArgs = null; |
573 |
| - if (op.argLength > 0) { |
574 |
| - oparg |= Byte.toUnsignedInt(bytecode[bci + 1]); |
575 |
| - if (op.argLength > 1) { |
576 |
| - followingArgs = new byte[op.argLength - 1]; |
577 |
| - System.arraycopy(bytecode, bci + 2, followingArgs, 0, followingArgs.length); |
578 |
| - } |
579 |
| - } |
580 |
| - action.run(bci, op, oparg, followingArgs); |
581 |
| - oparg = 0; |
| 652 | + OpCodes op = OpCodes.fromOpCode(bytecode[bci]); |
| 653 | + while (op == OpCodes.EXTENDED_ARG) { |
| 654 | + oparg |= Byte.toUnsignedInt(bytecode[bci + 1]); |
| 655 | + oparg <<= 8; |
| 656 | + bci += 2; |
| 657 | + op = OpCodes.fromOpCode(bytecode[bci]); |
| 658 | + } |
| 659 | + byte[] followingArgs = null; |
| 660 | + if (op.argLength > 0) { |
| 661 | + oparg |= Byte.toUnsignedInt(bytecode[bci + 1]); |
| 662 | + if (op.argLength > 1) { |
| 663 | + followingArgs = new byte[op.argLength - 1]; |
| 664 | + System.arraycopy(bytecode, bci + 2, followingArgs, 0, followingArgs.length); |
582 | 665 | }
|
583 |
| - bci += op.length(); |
| 666 | + } |
| 667 | + action.run(bci, op, oparg, followingArgs); |
| 668 | + return bci + op.length(); |
| 669 | + } |
| 670 | + |
| 671 | + public static void iterateBytecode(byte[] bytecode, BytecodeAction action) { |
| 672 | + for (int bci = 0; bci < bytecode.length;) { |
| 673 | + bci = opCodeAt(bytecode, bci, action); |
584 | 674 | }
|
585 | 675 | }
|
586 | 676 |
|
|
0 commit comments