Skip to content

Commit

Permalink
Fix jump patching (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
smarr authored Aug 23, 2024
2 parents 54f8adc + 622a8cc commit 70ce0b7
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ private BytecodeMethodGenContext(final ClassGenerationContext holderGenc,
last4Bytecodes = new byte[4];
}

public void dump() {
Disassembler.dumpMethod(this);
}

public Object getConstant(final int bytecodeIdx) {
return literals.get(bytecode.get(bytecodeIdx + 1));
}
Expand Down Expand Up @@ -174,7 +178,7 @@ public void addBytecodeArgument(final byte code) {

public void patchJumpOffsetToPointToNextInstruction(final int idxOfOffset,
final ParserBc parser) throws ParseError {
int instructionStart = idxOfOffset - 1;
final int instructionStart = idxOfOffset - 1;
byte bytecodeBeforeOffset = bytecode.get(instructionStart);
assert isOneOf(bytecodeBeforeOffset,
JUMP_BYTECODES) : "Expected to patch a JUMP instruction, but got bc: "
Expand All @@ -189,10 +193,12 @@ assert isOneOf(bytecodeBeforeOffset,
bytecode.set(idxOfOffset, (byte) jumpOffset);
bytecode.set(idxOfOffset + 1, (byte) 0);
} else {
int offsetOfBytecode = idxOfOffset - 1;
// we need two bytes for the jump offset
bytecode.set(offsetOfBytecode,
(byte) (bytecode.get(offsetOfBytecode) + Bytecodes.NUM_1_BYTE_JUMP_BYTECODES));
if (bytecodeBeforeOffset < JUMP2) {
// we need to convert the JUMP to a JUMP2
bytecode.set(instructionStart,
(byte) (bytecodeBeforeOffset + Bytecodes.NUM_1_BYTE_JUMP_BYTECODES));
}

byte byte1 = (byte) jumpOffset;
byte byte2 = (byte) (jumpOffset >> 8);
Expand Down
6 changes: 3 additions & 3 deletions src/trufflesom/src/trufflesom/interpreter/bc/Bytecodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ public static String getPaddedBytecodeName(final byte bytecode) {
return PADDED_BYTECODE_NAMES[bytecode];
}

public static final byte LEN_NO_ARG = 1;
public static final byte LEN_TWO_ARGS = 2;
public static final byte LEN_THREE_ARGS = 3;
public static final byte LEN_NO_ARG = 1;
public static final byte LEN_ONE_ARG = 2;
public static final byte LEN_TWO_ARGS = 3;

public static int getBytecodeLength(final byte bytecode) {
return BYTECODE_LENGTH[bytecode];
Expand Down
Loading

0 comments on commit 70ce0b7

Please sign in to comment.