Dedupe Index & Branch

This commit is contained in:
ktkk 2026-09-06 17:14:54 +00:00
parent 176be5dad8
commit 76699b87e6

View file

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const Class = @import("../../Class.zig"); const Class = @import("../../Class.zig");
const ConstantPool = Class.ConstantPool;
const ParseError = Class.ParseError; const ParseError = Class.ParseError;
const Op = enum(u8) { const Op = enum(u8) {
@ -208,47 +209,29 @@ const Op = enum(u8) {
wide = 0xc4, wide = 0xc4,
}; };
const IndexU8 = struct { fn Index(comptime T: type) type {
index: u8, const bits = @typeInfo(T).int.bits;
if (bits % 8 != 0) {
@compileError("Bits should be multiple of 8");
}
const bytes = bits / 8;
return struct {
index: T,
const Self = @This(); const Self = @This();
pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self { pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self {
defer byte_index.* += 1; defer byte_index.* += bytes;
const index = try if (bytes == 1) input.takeByte() else input.takeInt(T, .big);
return .{ return .{
.index = try input.takeByte(), .index = index,
}; };
} }
};
const IndexU16 = struct {
index: u16,
const Self = @This();
pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self {
defer byte_index.* += 2;
return .{
.index = try input.takeInt(u16, .big),
}; };
} }
};
const IndexU32 = struct {
index: u32,
const Self = @This();
pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self {
defer byte_index.* += 4;
return .{
.index = try input.takeInt(u32, .big),
};
}
};
const Byte = struct { const Byte = struct {
byte: u8, byte: u8,
@ -264,33 +247,27 @@ const Byte = struct {
} }
}; };
const BranchU16 = struct { fn Branch(comptime T: type) type {
branch: u16, const bits = @typeInfo(T).int.bits;
if (bits % 8 != 0) {
@compileError("Bits should be multiple of 8");
}
const bytes = bits / 8;
return struct {
branch: T,
const Self = @This(); const Self = @This();
pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self { pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self {
defer byte_index.* += 2; defer byte_index.* += bytes;
return .{ return .{
.branch = try input.takeInt(u16, .big), .branch = try input.takeInt(u16, .big),
}; };
} }
};
const BranchU32 = struct {
branch: u32,
const Self = @This();
pub fn parse(input: *std.Io.Reader, byte_index: *usize) ParseError!Self {
defer byte_index.* += 4;
return .{
.branch = try input.takeInt(u32, .big),
}; };
} }
};
const IInc = struct { const IInc = struct {
index: u8, index: u8,
@ -537,15 +514,15 @@ pub const Instruction = union(Op) {
aaload, aaload,
aastore, aastore,
aconst_null, aconst_null,
aload: IndexU8, aload: Index(u8),
aload_0, aload_0,
aload_1, aload_1,
aload_2, aload_2,
aload_3, aload_3,
anewarray: IndexU16, anewarray: Index(u16),
areturn, areturn,
arraylength, arraylength,
astore: IndexU8, astore: Index(u8),
astore_0, astore_0,
astore_1, astore_1,
astore_2, astore_2,
@ -556,7 +533,7 @@ pub const Instruction = union(Op) {
bipush: Byte, bipush: Byte,
caload, caload,
castore, castore,
checkcast: IndexU16, checkcast: Index(u16),
d2f, d2f,
d2i, d2i,
d2l, d2l,
@ -568,7 +545,7 @@ pub const Instruction = union(Op) {
dconst_0, dconst_0,
dconst_1, dconst_1,
ddiv, ddiv,
dload: IndexU8, dload: Index(u8),
dload_0, dload_0,
dload_1, dload_1,
dload_2, dload_2,
@ -577,7 +554,7 @@ pub const Instruction = union(Op) {
dneg, dneg,
drem, drem,
dreturn, dreturn,
dstore: IndexU8, dstore: Index(u8),
dstore_0, dstore_0,
dstore_1, dstore_1,
dstore_2, dstore_2,
@ -601,7 +578,7 @@ pub const Instruction = union(Op) {
fconst_1, fconst_1,
fconst_2, fconst_2,
fdiv, fdiv,
fload: IndexU8, fload: Index(u8),
fload_0, fload_0,
fload_1, fload_1,
fload_2, fload_2,
@ -610,16 +587,16 @@ pub const Instruction = union(Op) {
fneg, fneg,
frem, frem,
freturn, freturn,
fstore: IndexU8, fstore: Index(u8),
fstore_0, fstore_0,
fstore_1, fstore_1,
fstore_2, fstore_2,
fstore_3, fstore_3,
fsub, fsub,
getfield: IndexU16, getfield: Index(u16),
getstatic: IndexU16, getstatic: Index(u16),
goto: IndexU16, goto: Index(u16),
goto_w: IndexU32, goto_w: Index(u32),
i2b, i2b,
i2c, i2c,
i2d, i2d,
@ -638,42 +615,42 @@ pub const Instruction = union(Op) {
iconst_4, iconst_4,
iconst_5, iconst_5,
idiv, idiv,
if_acmpeq: BranchU16, if_acmpeq: Branch(u16),
if_acmpne: BranchU16, if_acmpne: Branch(u16),
if_icmpeq: BranchU16, if_icmpeq: Branch(u16),
if_icmpne: BranchU16, if_icmpne: Branch(u16),
if_icmplt: BranchU16, if_icmplt: Branch(u16),
if_icmpge: BranchU16, if_icmpge: Branch(u16),
if_icmpgt: BranchU16, if_icmpgt: Branch(u16),
if_icmple: BranchU16, if_icmple: Branch(u16),
ifeq: BranchU16, ifeq: Branch(u16),
ifne: BranchU16, ifne: Branch(u16),
iflt: BranchU16, iflt: Branch(u16),
ifge: BranchU16, ifge: Branch(u16),
ifgt: BranchU16, ifgt: Branch(u16),
ifle: BranchU16, ifle: Branch(u16),
ifnonnull: BranchU16, ifnonnull: Branch(u16),
ifnull: BranchU16, ifnull: Branch(u16),
iinc: IInc, iinc: IInc,
iload: IndexU8, iload: Index(u8),
iload_0, iload_0,
iload_1, iload_1,
iload_2, iload_2,
iload_3, iload_3,
imul, imul,
ineg, ineg,
instanceof: IndexU16, instanceof: Index(u16),
invokedynamic: InvokeDynamic, invokedynamic: InvokeDynamic,
invokeinterface: InvokeInterface, invokeinterface: InvokeInterface,
invokespecial: IndexU16, invokespecial: Index(u16),
invokestatic: IndexU16, invokestatic: Index(u16),
invokevirtual: IndexU16, invokevirtual: Index(u16),
ior, ior,
irem, irem,
ireturn, ireturn,
ishl, ishl,
ishr, ishr,
istore: IndexU8, istore: Index(u8),
istore_0, istore_0,
istore_1, istore_1,
istore_2, istore_2,
@ -681,8 +658,8 @@ pub const Instruction = union(Op) {
isub, isub,
iushr, iushr,
ixor, ixor,
jsr: BranchU16, jsr: Branch(u16),
jsr_w: BranchU32, jsr_w: Branch(u32),
l2d, l2d,
l2f, l2f,
l2i, l2i,
@ -693,11 +670,11 @@ pub const Instruction = union(Op) {
lcmp, lcmp,
lconst_0, lconst_0,
lconst_1, lconst_1,
ldc: IndexU8, ldc: Index(u8),
ldc_w: IndexU16, ldc_w: Index(u16),
ldc2_w: IndexU16, ldc2_w: Index(u16),
ldiv, ldiv,
lload: IndexU8, lload: Index(u8),
lload_0, lload_0,
lload_1, lload_1,
lload_2, lload_2,
@ -710,7 +687,7 @@ pub const Instruction = union(Op) {
lreturn, lreturn,
lshl, lshl,
lshr, lshr,
lstore: IndexU8, lstore: Index(u8),
lstore_0, lstore_0,
lstore_1, lstore_1,
lstore_2, lstore_2,
@ -721,14 +698,14 @@ pub const Instruction = union(Op) {
monitorenter, monitorenter,
monitorexit, monitorexit,
multianewarray: MultiANewArray, multianewarray: MultiANewArray,
new: IndexU16, new: Index(u16),
newarray: NewArray, newarray: NewArray,
nop, nop,
pop, pop,
pop2, pop2,
putfield: IndexU16, putfield: Index(u16),
putstatic: IndexU16, putstatic: Index(u16),
ret: IndexU8, ret: Index(u8),
@"return", @"return",
saload, saload,
sastore, sastore,