Skip to content

Commit

Permalink
8339519: Remove size field from instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
liach committed Sep 5, 2024
1 parent 9e1af8c commit 9ed148f
Showing 1 changed file with 61 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ public abstract sealed class AbstractInstruction
FMT_Discontinued = "Discontinued[OP=%s]";

final Opcode op;
final int size;

@Override
public Opcode opcode() {
Expand All @@ -103,12 +102,12 @@ public Opcode opcode() {

@Override
public int sizeInBytes() {
return size;
// Note: only lookupswitch and tableswitch have variable sizes
return op.sizeIfFixed();
}

public AbstractInstruction(Opcode op, int size) {
AbstractInstruction(Opcode op) {
this.op = op;
this.size = size;
}

@Override
Expand All @@ -118,8 +117,8 @@ public abstract static sealed class BoundInstruction extends AbstractInstruction
final CodeImpl code;
final int pos;

protected BoundInstruction(Opcode op, int size, CodeImpl code, int pos) {
super(op, size);
protected BoundInstruction(Opcode op, CodeImpl code, int pos) {
super(op);
this.code = code;
this.pos = pos;
}
Expand All @@ -131,15 +130,15 @@ protected Label offsetToLabel(int offset) {
@Override
public void writeTo(DirectCodeBuilder writer) {
// Override this if the instruction has any CP references or labels!
code.classReader.copyBytesTo(writer.bytecodesBufWriter, pos, size);
code.classReader.copyBytesTo(writer.bytecodesBufWriter, pos, op.sizeIfFixed());
}
}

public static final class BoundLoadInstruction
extends BoundInstruction implements LoadInstruction {

public BoundLoadInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}


Expand All @@ -155,7 +154,7 @@ public String toString() {

@Override
public int slot() {
return switch (size) {
return switch (sizeInBytes()) {
case 2 -> code.classReader.readU1(pos + 1);
case 4 -> code.classReader.readU2(pos + 2);
default -> throw new IllegalArgumentException("Unexpected op size: " + op.sizeIfFixed() + " -- " + op);
Expand All @@ -168,7 +167,7 @@ public static final class BoundStoreInstruction
extends BoundInstruction implements StoreInstruction {

public BoundStoreInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -183,10 +182,10 @@ public String toString() {

@Override
public int slot() {
return switch (size) {
return switch (sizeInBytes()) {
case 2 -> code.classReader.readU1(pos + 1);
case 4 -> code.classReader.readU2(pos + 2);
default -> throw new IllegalArgumentException("Unexpected op size: " + size + " -- " + op);
default -> throw new IllegalArgumentException("Unexpected op size: " + sizeInBytes() + " -- " + op);
};
}

Expand All @@ -196,17 +195,17 @@ public static final class BoundIncrementInstruction
extends BoundInstruction implements IncrementInstruction {

public BoundIncrementInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
public int slot() {
return size == 6 ? code.classReader.readU2(pos + 2) : code.classReader.readU1(pos + 1);
return sizeInBytes() == 6 ? code.classReader.readU2(pos + 2) : code.classReader.readU1(pos + 1);
}

@Override
public int constant() {
return size == 6 ? code.classReader.readS2(pos + 4) : (byte) code.classReader.readS1(pos + 2);
return sizeInBytes() == 6 ? code.classReader.readS2(pos + 4) : code.classReader.readS1(pos + 2);
}

@Override
Expand All @@ -220,7 +219,7 @@ public static final class BoundBranchInstruction
extends BoundInstruction implements BranchInstruction {

public BoundBranchInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -229,8 +228,8 @@ public Label target() {
}

public int branchByteOffset() {
return size == 3
? (int) (short) code.classReader.readU2(pos + 1)
return sizeInBytes() == 3
? code.classReader.readS2(pos + 1)
: code.classReader.readInt(pos + 1);
}

Expand All @@ -256,33 +255,31 @@ public static final class BoundLookupSwitchInstruction
// will always need size, cache everything to there
private final int afterPad;
private final int npairs;
private final int size;

BoundLookupSwitchInstruction(Opcode op, CodeImpl code, int pos) {
super(op, size(code, code.codeStart, pos), code, pos);

this.afterPad = pos + 1 + ((4 - ((pos + 1 - code.codeStart) & 3)) & 3);
super(op, code, pos);
this.afterPad = code.codeStart + RawBytecodeHelper.align(pos + 1 - code.codeStart);
this.npairs = code.classReader.readInt(afterPad + 4);
if (npairs < 0 || npairs > code.codeLength >> 3) {
throw new IllegalArgumentException("Invalid lookupswitch npairs value: " + npairs);
}
}

static int size(CodeImpl code, int codeStart, int pos) {
int afterPad = pos + 1 + ((4 - ((pos + 1 - codeStart) & 3)) & 3);
int pad = afterPad - (pos + 1);
int npairs = code.classReader.readInt(afterPad + 4);
return 1 + pad + 8 + npairs * 8;
this.size = afterPad + 8 + npairs * 8 - pos;
}

private int defaultOffset() {
return code.classReader.readInt(afterPad);
}

@Override
public int sizeInBytes() {
return size;
}

@Override
public List<SwitchCase> cases() {
var cases = new SwitchCase[npairs];
for (int i = 0; i < npairs; ++i) {
int z = afterPad + 8 + 8 * i;
for (int i = 0, z = afterPad + 8; i < npairs; ++i, z += 8) {
cases[i] = SwitchCase.of(code.classReader.readInt(z), offsetToLabel(code.classReader.readInt(z + 4)));
}
return List.of(cases);
Expand All @@ -308,25 +305,26 @@ public String toString() {
public static final class BoundTableSwitchInstruction
extends BoundInstruction implements TableSwitchInstruction {

BoundTableSwitchInstruction(Opcode op, CodeImpl code, int pos) {
super(op, size(code, code.codeStart, pos), code, pos);
}
private final int afterPad;
private final int low;
private final int high;
private final int size;

static int size(CodeImpl code, int codeStart, int pos) {
int ap = pos + 1 + ((4 - ((pos + 1 - codeStart) & 3)) & 3);
int pad = ap - (pos + 1);
int low = code.classReader.readInt(ap + 4);
int high = code.classReader.readInt(ap + 8);
BoundTableSwitchInstruction(Opcode op, CodeImpl code, int pos) {
super(op, code, pos);
afterPad = code.codeStart + RawBytecodeHelper.align(pos + 1 - code.codeStart);
low = code.classReader.readInt(afterPad + 4);
high = code.classReader.readInt(afterPad + 8);
if (high < low || (long)high - low > code.codeLength >> 2) {
throw new IllegalArgumentException("Invalid tableswitch values low: " + low + " high: " + high);
}
int cnt = high - low + 1;
return 1 + pad + 12 + cnt * 4;
size = afterPad + 12 + cnt * 4 - pos;
}

private int afterPadding() {
int p = pos;
return p + 1 + ((4 - ((p + 1 - code.codeStart) & 3)) & 3);
@Override
public int sizeInBytes() {
return size;
}

@Override
Expand All @@ -336,12 +334,12 @@ public Label defaultTarget() {

@Override
public int lowValue() {
return code.classReader.readInt(afterPadding() + 4);
return low;
}

@Override
public int highValue() {
return code.classReader.readInt(afterPadding() + 8);
return high;
}

@Override
Expand All @@ -350,19 +348,17 @@ public List<SwitchCase> cases() {
int high = highValue();
int defOff = defaultOffset();
var cases = new ArrayList<SwitchCase>(high - low + 1);
int z = afterPadding() + 12;
for (int i = lowValue(); i <= high; ++i) {
for (int i = low, z = afterPad + 12; i <= high; ++i, z += 4) {
int off = code.classReader.readInt(z);
if (defOff != off) {
cases.add(SwitchCase.of(i, offsetToLabel(off)));
}
z += 4;
}
return Collections.unmodifiableList(cases);
}

private int defaultOffset() {
return code.classReader.readInt(afterPadding());
return code.classReader.readInt(afterPad);
}

@Override
Expand All @@ -383,7 +379,7 @@ public static final class BoundFieldInstruction
private FieldRefEntry fieldEntry;

public BoundFieldInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -413,7 +409,7 @@ public static final class BoundInvokeInstruction
MemberRefEntry methodEntry;

public BoundInvokeInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -453,7 +449,7 @@ public static final class BoundInvokeInterfaceInstruction
InterfaceMethodRefEntry methodEntry;

public BoundInvokeInterfaceInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -493,7 +489,7 @@ public static final class BoundInvokeDynamicInstruction
InvokeDynamicEntry indyEntry;

BoundInvokeDynamicInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -523,7 +519,7 @@ public static final class BoundNewObjectInstruction
ClassEntry classEntry;

BoundNewObjectInstruction(CodeImpl code, int pos) {
super(Opcode.NEW, Opcode.NEW.sizeIfFixed(), code, pos);
super(Opcode.NEW, code, pos);
}

@Override
Expand Down Expand Up @@ -552,7 +548,7 @@ public static final class BoundNewPrimitiveArrayInstruction
extends BoundInstruction implements NewPrimitiveArrayInstruction {

public BoundNewPrimitiveArrayInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -571,7 +567,7 @@ public static final class BoundNewReferenceArrayInstruction
extends BoundInstruction implements NewReferenceArrayInstruction {

public BoundNewReferenceArrayInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -597,7 +593,7 @@ public static final class BoundNewMultidimensionalArrayInstruction
extends BoundInstruction implements NewMultiArrayInstruction {

public BoundNewMultidimensionalArrayInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -630,7 +626,7 @@ public static final class BoundTypeCheckInstruction
ClassEntry typeEntry;

public BoundTypeCheckInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -659,7 +655,7 @@ public static final class BoundArgumentConstantInstruction
extends BoundInstruction implements ConstantInstruction.ArgumentConstantInstruction {

public BoundArgumentConstantInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -668,7 +664,7 @@ public Integer constantValue() {
}

public int constantInt() {
return size == 3 ? code.classReader.readS2(pos + 1) : code.classReader.readS1(pos + 1);
return sizeInBytes() == 3 ? code.classReader.readS2(pos + 1) : code.classReader.readS1(pos + 1);
}

@Override
Expand All @@ -682,7 +678,7 @@ public static final class BoundLoadConstantInstruction
extends BoundInstruction implements ConstantInstruction.LoadConstantInstruction {

public BoundLoadConstantInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand Down Expand Up @@ -717,7 +713,7 @@ public static final class BoundJsrInstruction
extends BoundInstruction implements DiscontinuedInstruction.JsrInstruction {

public BoundJsrInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -726,7 +722,7 @@ public Label target() {
}

public int branchByteOffset() {
return size == 3
return sizeInBytes() == 3
? code.classReader.readS2(pos + 1)
: code.classReader.readInt(pos + 1);
}
Expand All @@ -747,7 +743,7 @@ public static final class BoundRetInstruction
extends BoundInstruction implements DiscontinuedInstruction.RetInstruction {

public BoundRetInstruction(Opcode op, CodeImpl code, int pos) {
super(op, op.sizeIfFixed(), code, pos);
super(op, code, pos);
}

@Override
Expand All @@ -757,7 +753,7 @@ public String toString() {

@Override
public int slot() {
return switch (size) {
return switch (sizeInBytes()) {
case 2 -> code.classReader.readU1(pos + 1);
case 4 -> code.classReader.readU2(pos + 2);
default -> throw new IllegalArgumentException("Unexpected op size: " + op.sizeIfFixed() + " -- " + op);
Expand All @@ -769,7 +765,7 @@ public int slot() {
public abstract static sealed class UnboundInstruction extends AbstractInstruction {

UnboundInstruction(Opcode op) {
super(op, op.sizeIfFixed());
super(op);
}

@Override
Expand Down

0 comments on commit 9ed148f

Please sign in to comment.