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