Skip to content

Commit 5b72bbf

Browse files
committed
8339519: Remove size field from instructions
Reviewed-by: asotona
1 parent 0df10bb commit 5b72bbf

File tree

1 file changed

+61
-65
lines changed

1 file changed

+61
-65
lines changed

src/java.base/share/classes/jdk/internal/classfile/impl/AbstractInstruction.java

Lines changed: 61 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ public abstract sealed class AbstractInstruction
9494
FMT_Discontinued = "Discontinued[OP=%s]";
9595

9696
final Opcode op;
97-
final int size;
9897

9998
@Override
10099
public Opcode opcode() {
@@ -103,12 +102,12 @@ public Opcode opcode() {
103102

104103
@Override
105104
public int sizeInBytes() {
106-
return size;
105+
// Note: only lookupswitch and tableswitch have variable sizes
106+
return op.sizeIfFixed();
107107
}
108108

109-
public AbstractInstruction(Opcode op, int size) {
109+
AbstractInstruction(Opcode op) {
110110
this.op = op;
111-
this.size = size;
112111
}
113112

114113
@Override
@@ -118,8 +117,8 @@ public abstract static sealed class BoundInstruction extends AbstractInstruction
118117
final CodeImpl code;
119118
final int pos;
120119

121-
protected BoundInstruction(Opcode op, int size, CodeImpl code, int pos) {
122-
super(op, size);
120+
protected BoundInstruction(Opcode op, CodeImpl code, int pos) {
121+
super(op);
123122
this.code = code;
124123
this.pos = pos;
125124
}
@@ -131,15 +130,15 @@ protected Label offsetToLabel(int offset) {
131130
@Override
132131
public void writeTo(DirectCodeBuilder writer) {
133132
// Override this if the instruction has any CP references or labels!
134-
code.classReader.copyBytesTo(writer.bytecodesBufWriter, pos, size);
133+
code.classReader.copyBytesTo(writer.bytecodesBufWriter, pos, op.sizeIfFixed());
135134
}
136135
}
137136

138137
public static final class BoundLoadInstruction
139138
extends BoundInstruction implements LoadInstruction {
140139

141140
public BoundLoadInstruction(Opcode op, CodeImpl code, int pos) {
142-
super(op, op.sizeIfFixed(), code, pos);
141+
super(op, code, pos);
143142
}
144143

145144

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

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

170169
public BoundStoreInstruction(Opcode op, CodeImpl code, int pos) {
171-
super(op, op.sizeIfFixed(), code, pos);
170+
super(op, code, pos);
172171
}
173172

174173
@Override
@@ -183,10 +182,10 @@ public String toString() {
183182

184183
@Override
185184
public int slot() {
186-
return switch (size) {
185+
return switch (sizeInBytes()) {
187186
case 2 -> code.classReader.readU1(pos + 1);
188187
case 4 -> code.classReader.readU2(pos + 2);
189-
default -> throw new IllegalArgumentException("Unexpected op size: " + size + " -- " + op);
188+
default -> throw new IllegalArgumentException("Unexpected op size: " + sizeInBytes() + " -- " + op);
190189
};
191190
}
192191

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

198197
public BoundIncrementInstruction(Opcode op, CodeImpl code, int pos) {
199-
super(op, op.sizeIfFixed(), code, pos);
198+
super(op, code, pos);
200199
}
201200

202201
@Override
203202
public int slot() {
204-
return size == 6 ? code.classReader.readU2(pos + 2) : code.classReader.readU1(pos + 1);
203+
return sizeInBytes() == 6 ? code.classReader.readU2(pos + 2) : code.classReader.readU1(pos + 1);
205204
}
206205

207206
@Override
208207
public int constant() {
209-
return size == 6 ? code.classReader.readS2(pos + 4) : (byte) code.classReader.readS1(pos + 2);
208+
return sizeInBytes() == 6 ? code.classReader.readS2(pos + 4) : code.classReader.readS1(pos + 2);
210209
}
211210

212211
@Override
@@ -220,7 +219,7 @@ public static final class BoundBranchInstruction
220219
extends BoundInstruction implements BranchInstruction {
221220

222221
public BoundBranchInstruction(Opcode op, CodeImpl code, int pos) {
223-
super(op, op.sizeIfFixed(), code, pos);
222+
super(op, code, pos);
224223
}
225224

226225
@Override
@@ -229,8 +228,8 @@ public Label target() {
229228
}
230229

231230
public int branchByteOffset() {
232-
return size == 3
233-
? (int) (short) code.classReader.readU2(pos + 1)
231+
return sizeInBytes() == 3
232+
? code.classReader.readS2(pos + 1)
234233
: code.classReader.readInt(pos + 1);
235234
}
236235

@@ -256,33 +255,31 @@ public static final class BoundLookupSwitchInstruction
256255
// will always need size, cache everything to there
257256
private final int afterPad;
258257
private final int npairs;
258+
private final int size;
259259

260260
BoundLookupSwitchInstruction(Opcode op, CodeImpl code, int pos) {
261-
super(op, size(code, code.codeStart, pos), code, pos);
262-
263-
this.afterPad = pos + 1 + ((4 - ((pos + 1 - code.codeStart) & 3)) & 3);
261+
super(op, code, pos);
262+
this.afterPad = code.codeStart + RawBytecodeHelper.align(pos + 1 - code.codeStart);
264263
this.npairs = code.classReader.readInt(afterPad + 4);
265264
if (npairs < 0 || npairs > code.codeLength >> 3) {
266265
throw new IllegalArgumentException("Invalid lookupswitch npairs value: " + npairs);
267266
}
268-
}
269-
270-
static int size(CodeImpl code, int codeStart, int pos) {
271-
int afterPad = pos + 1 + ((4 - ((pos + 1 - codeStart) & 3)) & 3);
272-
int pad = afterPad - (pos + 1);
273-
int npairs = code.classReader.readInt(afterPad + 4);
274-
return 1 + pad + 8 + npairs * 8;
267+
this.size = afterPad + 8 + npairs * 8 - pos;
275268
}
276269

277270
private int defaultOffset() {
278271
return code.classReader.readInt(afterPad);
279272
}
280273

274+
@Override
275+
public int sizeInBytes() {
276+
return size;
277+
}
278+
281279
@Override
282280
public List<SwitchCase> cases() {
283281
var cases = new SwitchCase[npairs];
284-
for (int i = 0; i < npairs; ++i) {
285-
int z = afterPad + 8 + 8 * i;
282+
for (int i = 0, z = afterPad + 8; i < npairs; ++i, z += 8) {
286283
cases[i] = SwitchCase.of(code.classReader.readInt(z), offsetToLabel(code.classReader.readInt(z + 4)));
287284
}
288285
return List.of(cases);
@@ -308,25 +305,26 @@ public String toString() {
308305
public static final class BoundTableSwitchInstruction
309306
extends BoundInstruction implements TableSwitchInstruction {
310307

311-
BoundTableSwitchInstruction(Opcode op, CodeImpl code, int pos) {
312-
super(op, size(code, code.codeStart, pos), code, pos);
313-
}
308+
private final int afterPad;
309+
private final int low;
310+
private final int high;
311+
private final int size;
314312

315-
static int size(CodeImpl code, int codeStart, int pos) {
316-
int ap = pos + 1 + ((4 - ((pos + 1 - codeStart) & 3)) & 3);
317-
int pad = ap - (pos + 1);
318-
int low = code.classReader.readInt(ap + 4);
319-
int high = code.classReader.readInt(ap + 8);
313+
BoundTableSwitchInstruction(Opcode op, CodeImpl code, int pos) {
314+
super(op, code, pos);
315+
afterPad = code.codeStart + RawBytecodeHelper.align(pos + 1 - code.codeStart);
316+
low = code.classReader.readInt(afterPad + 4);
317+
high = code.classReader.readInt(afterPad + 8);
320318
if (high < low || (long)high - low > code.codeLength >> 2) {
321319
throw new IllegalArgumentException("Invalid tableswitch values low: " + low + " high: " + high);
322320
}
323321
int cnt = high - low + 1;
324-
return 1 + pad + 12 + cnt * 4;
322+
size = afterPad + 12 + cnt * 4 - pos;
325323
}
326324

327-
private int afterPadding() {
328-
int p = pos;
329-
return p + 1 + ((4 - ((p + 1 - code.codeStart) & 3)) & 3);
325+
@Override
326+
public int sizeInBytes() {
327+
return size;
330328
}
331329

332330
@Override
@@ -336,12 +334,12 @@ public Label defaultTarget() {
336334

337335
@Override
338336
public int lowValue() {
339-
return code.classReader.readInt(afterPadding() + 4);
337+
return low;
340338
}
341339

342340
@Override
343341
public int highValue() {
344-
return code.classReader.readInt(afterPadding() + 8);
342+
return high;
345343
}
346344

347345
@Override
@@ -350,19 +348,17 @@ public List<SwitchCase> cases() {
350348
int high = highValue();
351349
int defOff = defaultOffset();
352350
var cases = new ArrayList<SwitchCase>(high - low + 1);
353-
int z = afterPadding() + 12;
354-
for (int i = lowValue(); i <= high; ++i) {
351+
for (int i = low, z = afterPad + 12; i <= high; ++i, z += 4) {
355352
int off = code.classReader.readInt(z);
356353
if (defOff != off) {
357354
cases.add(SwitchCase.of(i, offsetToLabel(off)));
358355
}
359-
z += 4;
360356
}
361357
return Collections.unmodifiableList(cases);
362358
}
363359

364360
private int defaultOffset() {
365-
return code.classReader.readInt(afterPadding());
361+
return code.classReader.readInt(afterPad);
366362
}
367363

368364
@Override
@@ -383,7 +379,7 @@ public static final class BoundFieldInstruction
383379
private FieldRefEntry fieldEntry;
384380

385381
public BoundFieldInstruction(Opcode op, CodeImpl code, int pos) {
386-
super(op, op.sizeIfFixed(), code, pos);
382+
super(op, code, pos);
387383
}
388384

389385
@Override
@@ -413,7 +409,7 @@ public static final class BoundInvokeInstruction
413409
MemberRefEntry methodEntry;
414410

415411
public BoundInvokeInstruction(Opcode op, CodeImpl code, int pos) {
416-
super(op, op.sizeIfFixed(), code, pos);
412+
super(op, code, pos);
417413
}
418414

419415
@Override
@@ -453,7 +449,7 @@ public static final class BoundInvokeInterfaceInstruction
453449
InterfaceMethodRefEntry methodEntry;
454450

455451
public BoundInvokeInterfaceInstruction(Opcode op, CodeImpl code, int pos) {
456-
super(op, op.sizeIfFixed(), code, pos);
452+
super(op, code, pos);
457453
}
458454

459455
@Override
@@ -493,7 +489,7 @@ public static final class BoundInvokeDynamicInstruction
493489
InvokeDynamicEntry indyEntry;
494490

495491
BoundInvokeDynamicInstruction(Opcode op, CodeImpl code, int pos) {
496-
super(op, op.sizeIfFixed(), code, pos);
492+
super(op, code, pos);
497493
}
498494

499495
@Override
@@ -523,7 +519,7 @@ public static final class BoundNewObjectInstruction
523519
ClassEntry classEntry;
524520

525521
BoundNewObjectInstruction(CodeImpl code, int pos) {
526-
super(Opcode.NEW, Opcode.NEW.sizeIfFixed(), code, pos);
522+
super(Opcode.NEW, code, pos);
527523
}
528524

529525
@Override
@@ -552,7 +548,7 @@ public static final class BoundNewPrimitiveArrayInstruction
552548
extends BoundInstruction implements NewPrimitiveArrayInstruction {
553549

554550
public BoundNewPrimitiveArrayInstruction(Opcode op, CodeImpl code, int pos) {
555-
super(op, op.sizeIfFixed(), code, pos);
551+
super(op, code, pos);
556552
}
557553

558554
@Override
@@ -571,7 +567,7 @@ public static final class BoundNewReferenceArrayInstruction
571567
extends BoundInstruction implements NewReferenceArrayInstruction {
572568

573569
public BoundNewReferenceArrayInstruction(Opcode op, CodeImpl code, int pos) {
574-
super(op, op.sizeIfFixed(), code, pos);
570+
super(op, code, pos);
575571
}
576572

577573
@Override
@@ -597,7 +593,7 @@ public static final class BoundNewMultidimensionalArrayInstruction
597593
extends BoundInstruction implements NewMultiArrayInstruction {
598594

599595
public BoundNewMultidimensionalArrayInstruction(Opcode op, CodeImpl code, int pos) {
600-
super(op, op.sizeIfFixed(), code, pos);
596+
super(op, code, pos);
601597
}
602598

603599
@Override
@@ -630,7 +626,7 @@ public static final class BoundTypeCheckInstruction
630626
ClassEntry typeEntry;
631627

632628
public BoundTypeCheckInstruction(Opcode op, CodeImpl code, int pos) {
633-
super(op, op.sizeIfFixed(), code, pos);
629+
super(op, code, pos);
634630
}
635631

636632
@Override
@@ -659,7 +655,7 @@ public static final class BoundArgumentConstantInstruction
659655
extends BoundInstruction implements ConstantInstruction.ArgumentConstantInstruction {
660656

661657
public BoundArgumentConstantInstruction(Opcode op, CodeImpl code, int pos) {
662-
super(op, op.sizeIfFixed(), code, pos);
658+
super(op, code, pos);
663659
}
664660

665661
@Override
@@ -668,7 +664,7 @@ public Integer constantValue() {
668664
}
669665

670666
public int constantInt() {
671-
return size == 3 ? code.classReader.readS2(pos + 1) : code.classReader.readS1(pos + 1);
667+
return sizeInBytes() == 3 ? code.classReader.readS2(pos + 1) : code.classReader.readS1(pos + 1);
672668
}
673669

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

684680
public BoundLoadConstantInstruction(Opcode op, CodeImpl code, int pos) {
685-
super(op, op.sizeIfFixed(), code, pos);
681+
super(op, code, pos);
686682
}
687683

688684
@Override
@@ -717,7 +713,7 @@ public static final class BoundJsrInstruction
717713
extends BoundInstruction implements DiscontinuedInstruction.JsrInstruction {
718714

719715
public BoundJsrInstruction(Opcode op, CodeImpl code, int pos) {
720-
super(op, op.sizeIfFixed(), code, pos);
716+
super(op, code, pos);
721717
}
722718

723719
@Override
@@ -726,7 +722,7 @@ public Label target() {
726722
}
727723

728724
public int branchByteOffset() {
729-
return size == 3
725+
return sizeInBytes() == 3
730726
? code.classReader.readS2(pos + 1)
731727
: code.classReader.readInt(pos + 1);
732728
}
@@ -747,7 +743,7 @@ public static final class BoundRetInstruction
747743
extends BoundInstruction implements DiscontinuedInstruction.RetInstruction {
748744

749745
public BoundRetInstruction(Opcode op, CodeImpl code, int pos) {
750-
super(op, op.sizeIfFixed(), code, pos);
746+
super(op, code, pos);
751747
}
752748

753749
@Override
@@ -757,7 +753,7 @@ public String toString() {
757753

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

771767
UnboundInstruction(Opcode op) {
772-
super(op, op.sizeIfFixed());
768+
super(op);
773769
}
774770

775771
@Override

0 commit comments

Comments
 (0)