Refactor constant pool

This commit is contained in:
ktkk 2026-07-05 21:28:53 +02:00
parent 04cf7663af
commit 9135e95dbd
5 changed files with 689 additions and 659 deletions

581
src/Class/ConstantPool.zig Normal file
View file

@ -0,0 +1,581 @@
const std = @import("std");
const Class = @import("../Class.zig");
const AttributeInfo = Class.AttributeInfo;
const ParseError = Class.ParseError;
const ResolveError = Class.ResolveError;
pub const Info = union(Info.Tag) {
class: Info.Class,
field_ref: FieldRef,
method_ref: MethodRef,
interface_method_ref: InterfaceMethodRef,
string: String,
integer: Integer,
float: Float,
long: Long,
double: Double,
name_and_type: NameAndType,
utf8: Utf8,
method_handle: MethodHandle,
method_type: MethodType,
dynamic: Dynamic,
invoke_dynamic: InvokeDynamic,
module: Module,
package: Package,
pub const Tag = enum(u8) {
class = 7,
field_ref = 9,
method_ref = 10,
interface_method_ref = 11,
string = 8,
integer = 3,
float = 4,
long = 5,
double = 6,
name_and_type = 12,
utf8 = 1,
method_handle = 15,
method_type = 16,
dynamic = 17,
invoke_dynamic = 18,
module = 19,
package = 20,
pub fn loadable(self: Tag) bool {
return switch (self) {
.integer, .float, .long, .double, .class, .string, .method_handle, .method_type, .dynamic => true,
else => false,
};
}
};
pub const Class = struct {
name_index: u16,
const Self = @This();
pub fn name(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const FieldRef = struct {
class_index: u16,
name_and_type_index: u16,
const Self = @This();
pub fn class(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const class_index = self.class_index - 1;
if (class_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[class_index];
if (cp_info == null or @as(Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
}
pub fn nameAndType(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_and_type_index = self.name_and_type_index - 1;
if (name_and_type_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_and_type_index];
if (cp_info == null or @as(Tag, cp_info.?) != .name_and_type) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const MethodRef = struct {
class_index: u16,
name_and_type_index: u16,
const Self = @This();
pub fn class(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const class_index = self.class_index - 1;
if (class_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[class_index];
if (cp_info == null or @as(Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
}
pub fn nameAndType(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_and_type_index = self.name_and_type_index - 1;
if (name_and_type_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_and_type_index];
if (cp_info == null or @as(Tag, cp_info.?) != .name_and_type) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const InterfaceMethodRef = struct {
class_index: u16,
name_and_type_index: u16,
const Self = @This();
pub fn class(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const class_index = self.class_index - 1;
if (class_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[class_index];
if (cp_info == null or @as(Tag, cp_info.?) != .class) return ResolveError.InvalidConstantType;
return cp_info.?;
}
pub fn nameAndType(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_and_type_index = self.name_and_type_index - 1;
if (name_and_type_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_and_type_index];
if (cp_info == null or @as(Tag, cp_info.?) != .name_and_type) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const String = struct {
string_index: u16,
const Self = @This();
pub fn string(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const string_index = self.string_index - 1;
if (string_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[string_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const Integer = struct {
bytes: u32,
};
pub const Float = struct {
bytes: u32,
};
pub const Long = struct {
high_bytes: u32,
low_bytes: u32,
};
pub const Double = struct {
high_bytes: u32,
low_bytes: u32,
};
pub const NameAndType = struct {
name_index: u16,
descriptor_index: u16,
const Self = @This();
pub fn name(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
pub fn descriptor(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const descriptor_index = self.descriptor_index - 1;
if (descriptor_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[descriptor_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const Utf8 = struct {
bytes: []const u8,
};
pub const MethodHandle = struct {
reference_kind: Kind,
reference_index: u16,
const Self = @This();
pub const Kind = enum(u8) {
get_field = 1,
get_static = 2,
put_field = 3,
put_static = 4,
invoke_virtual = 5,
invoke_static = 6,
invoke_special = 7,
new_invoke_special = 8,
invoke_interface = 9,
};
pub fn reference(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const reference_index = self.reference_index - 1;
if (reference_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[reference_index];
return switch (self.reference_kind) {
.get_field, .get_static, .put_field, .put_static => blk: {
if (cp_info == null or @as(Tag, cp_info.?) != .field_ref) break :blk ResolveError.InvalidConstantType;
break :blk cp_info.?;
},
.invoke_virtual, .invoke_static, .invoke_special, .new_invoke_special => blk: {
if (cp_info == null or @as(Tag, cp_info.?) != .method_ref) break :blk ResolveError.InvalidConstantType;
break :blk cp_info.?;
},
.invoke_interface => blk: {
if (cp_info == null or @as(Tag, cp_info.?) != .interface_method_ref) break :blk ResolveError.InvalidConstantType;
break :blk cp_info.?;
},
};
}
};
pub const MethodType = struct {
descriptor_index: u16,
const Self = @This();
pub fn descriptor(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const descriptor_index = self.descriptor_index - 1;
if (descriptor_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[descriptor_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const Dynamic = struct {
bootstrap_method_attr_index: u16,
name_and_type_index: u16,
const Self = @This();
pub fn nameAndType(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_and_type_index = self.name_and_type_index - 1;
if (name_and_type_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_and_type_index];
if (cp_info == null or @as(Tag, cp_info.?) != .name_and_type) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const InvokeDynamic = struct {
bootstrap_method_attr_index: u16,
name_and_type_index: u16,
const Self = @This();
pub fn nameAndType(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_and_type_index = self.name_and_type_index - 1;
if (name_and_type_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_and_type_index];
if (cp_info == null or @as(Tag, cp_info.?) != .name_and_type) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const Module = struct {
name_index: u16,
const Self = @This();
pub fn name(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub const Package = struct {
name_index: u16,
const Self = @This();
pub fn name(self: *const Self, constant_pool: []const ?Info) ResolveError!Info {
const name_index = self.name_index - 1;
if (name_index >= constant_pool.len) return ResolveError.InvalidConstantPoolIndex;
const cp_info = constant_pool[name_index];
if (cp_info == null or @as(Tag, cp_info.?) != .utf8) return ResolveError.InvalidConstantType;
return cp_info.?;
}
};
pub fn parse(input: *std.Io.Reader, allocator: std.mem.Allocator) ParseError!Info {
const tag_byte = try input.takeByte();
const tag = std.enums.fromInt(Tag, tag_byte) orelse return ParseError.InvalidTag;
return switch (tag) {
.class => .{
.class = .{
.name_index = try input.takeInt(u16, .big),
},
},
.field_ref => .{
.field_ref = .{
.class_index = try input.takeInt(u16, .big),
.name_and_type_index = try input.takeInt(u16, .big),
},
},
.method_ref => .{
.method_ref = .{
.class_index = try input.takeInt(u16, .big),
.name_and_type_index = try input.takeInt(u16, .big),
},
},
.interface_method_ref => .{
.interface_method_ref = .{
.class_index = try input.takeInt(u16, .big),
.name_and_type_index = try input.takeInt(u16, .big),
},
},
.string => .{
.string = .{
.string_index = try input.takeInt(u16, .big),
},
},
.integer => .{
.integer = .{
.bytes = try input.takeInt(u32, .big),
},
},
.float => .{
.integer = .{
.bytes = try input.takeInt(u32, .big),
},
},
.long => .{
.long = .{
.high_bytes = try input.takeInt(u32, .big),
.low_bytes = try input.takeInt(u32, .big),
},
},
.double => .{
.double = .{
.high_bytes = try input.takeInt(u32, .big),
.low_bytes = try input.takeInt(u32, .big),
},
},
.name_and_type => .{
.name_and_type = .{
.name_index = try input.takeInt(u16, .big),
.descriptor_index = try input.takeInt(u16, .big),
},
},
.utf8 => blk: {
const length = try input.takeInt(u16, .big);
const bytes = try input.readAlloc(allocator, length);
break :blk .{
.utf8 = .{
.bytes = bytes,
},
};
},
.method_handle => blk: {
const kind_byte = try input.takeByte();
const kind = std.enums.fromInt(MethodHandle.Kind, kind_byte) orelse return ParseError.InvalidTag;
break :blk .{
.method_handle = .{
.reference_kind = kind,
.reference_index = try input.takeInt(u16, .big),
},
};
},
.method_type => .{
.method_type = .{
.descriptor_index = try input.takeInt(u16, .big),
},
},
.dynamic => .{
.dynamic = .{
.bootstrap_method_attr_index = try input.takeInt(u16, .big),
.name_and_type_index = try input.takeInt(u16, .big),
},
},
.invoke_dynamic => .{
.invoke_dynamic = .{
.bootstrap_method_attr_index = try input.takeInt(u16, .big),
.name_and_type_index = try input.takeInt(u16, .big),
},
},
.module => .{
.module = .{
.name_index = try input.takeInt(u16, .big),
},
},
.package => .{
.package = .{
.name_index = try input.takeInt(u16, .big),
},
},
};
}
pub fn deinit(self: *Info, allocator: std.mem.Allocator) void {
switch (self.*) {
.utf8 => |info| allocator.free(info.bytes),
else => {},
}
}
pub fn format(self: *const Info, w: *std.Io.Writer) std.Io.Writer.Error!void {
switch (self.*) {
.class => |class| try w.print("#{d}", .{ class.name_index }),
.field_ref => |field_ref| try w.print("#{d}.#{d}", .{ field_ref.class_index, field_ref.name_and_type_index }),
.method_ref => |method_ref| try w.print("#{d}.#{d}", .{ method_ref.class_index, method_ref.name_and_type_index }),
.interface_method_ref => |interface_method_ref| try w.print("#{d}.#{d}", .{ interface_method_ref.class_index, interface_method_ref.name_and_type_index }),
.string => |string| try w.print("#{d}", .{ string.string_index }),
.integer => |integer| try w.print("{d}", .{ integer.bytes }),
.float => |float| try w.print("{d}", .{ @as(f32, @bitCast(float.bytes)) }),
.long => |long| try w.print("{d}", .{ (@as(u64, long.high_bytes) << 32) | long.low_bytes }),
.double => |double| try w.print("{d}", .{ @as(f64, @bitCast((@as(u64, double.high_bytes) << 32) | double.low_bytes)) }),
.name_and_type => |name_and_type| try w.print("#{d}:#{d}", .{ name_and_type.name_index, name_and_type.descriptor_index }),
.utf8 => |utf8| try w.print("\"{s}\"", .{ utf8.bytes }),
.method_handle => |method_handle| try w.print("{s} #{d}", .{ @tagName(method_handle.reference_kind), method_handle.reference_index }),
.method_type => |method_type| try w.print("#{d}", .{ method_type.descriptor_index }),
.dynamic => |dynamic| try w.print("#{d} #{d}", .{ dynamic.bootstrap_method_attr_index, dynamic.name_and_type_index }),
.invoke_dynamic => |invoke_dynamic| try w.print("#{d} #{d}", .{ invoke_dynamic.bootstrap_method_attr_index, invoke_dynamic.name_and_type_index }),
.module => |module| try w.print("#{d}", .{ module.name_index }),
.package => |package| try w.print("#{d}", .{ package.name_index }),
}
}
pub fn indentedFormat(
self: *const Info,
w: *std.Io.Writer,
depth: usize,
indent: u8,
constant_pool: []const ?Info,
) std.Io.Writer.Error!void {
try w.splatByteAll(' ', depth * indent);
try w.print("constant_value_type: {s}\n", .{ @tagName(self.*) });
switch (self.*) {
.class => |class| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name:\n");
const class_name = class.name(constant_pool) catch return error.WriteFailed;
try class_name.indentedFormat(w, depth, indent + 1, constant_pool);
},
.field_ref => |field_ref| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("class:\n");
const class = field_ref.class(constant_pool) catch return error.WriteFailed;
try class.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = field_ref.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.method_ref => |method_ref| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("class:\n");
const class = method_ref.class(constant_pool) catch return error.WriteFailed;
try class.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = method_ref.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.interface_method_ref => |interface_method_ref| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("class:\n");
const class = interface_method_ref.class(constant_pool) catch return error.WriteFailed;
try class.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = interface_method_ref.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.string => |string| {
const s = string.string(constant_pool) catch return error.WriteFailed;
try s.indentedFormat(w, depth, indent + 1, constant_pool);
},
.integer => |integer| {
try w.splatByteAll(' ', depth * indent);
try w.print("integer: {d}\n", .{ integer.bytes });
},
.float => |float| {
const f: f32 = @bitCast(float.bytes);
try w.splatByteAll(' ', depth * indent);
try w.print("float: {d}\n", .{ f });
},
.long => |long| {
const l: u64 = (@as(u64, long.high_bytes) << 32) | long.low_bytes;
try w.splatByteAll(' ', depth * indent);
try w.print("long: {d}\n", .{ l });
},
.double => |double| {
const l: u64 = (@as(u64, double.high_bytes) << 32) | double.low_bytes;
const d: f64 = @bitCast(l);
try w.splatByteAll(' ', depth * indent);
try w.print("double: {d}\n", .{ d });
},
.name_and_type => |name_and_type| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name:\n");
const name = name_and_type.name(constant_pool) catch return error.WriteFailed;
try name.indentedFormat(w, depth, indent + 1, constant_pool);
try w.splatByteAll(' ', depth * indent);
_ = try w.write("descriptor:\n");
const descriptor = name_and_type.descriptor(constant_pool) catch return error.WriteFailed;
try descriptor.indentedFormat(w, depth, indent + 1, constant_pool);
},
.utf8 => |utf8| {
try w.splatByteAll(' ', depth * indent);
try w.print("bytes: \"{s}\"\n", .{ utf8.bytes });
},
.method_handle => |method_handle| {
try w.splatByteAll(' ', depth * indent);
try w.print("kind: {t}\n", .{ method_handle.reference_kind });
try w.splatByteAll(' ', depth * indent);
_ = try w.write("reference:\n");
const reference = method_handle.reference(constant_pool) catch return error.WriteFailed;
try reference.indentedFormat(w, depth, indent + 1, constant_pool);
},
.method_type => |method_type| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("descriptor:\n");
const descriptor = method_type.descriptor(constant_pool) catch return error.WriteFailed;
try descriptor.indentedFormat(w, depth, indent + 1, constant_pool);
},
.dynamic => |dynamic| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = dynamic.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.invoke_dynamic => |invoke_dynamic| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name_and_type:\n");
const name_and_type = invoke_dynamic.nameAndType(constant_pool) catch return error.WriteFailed;
try name_and_type.indentedFormat(w, depth, indent + 1, constant_pool);
},
.module => |module| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name:\n");
const name = module.name(constant_pool) catch return error.WriteFailed;
try name.indentedFormat(w, depth, indent + 1, constant_pool);
},
.package => |package| {
try w.splatByteAll(' ', depth * indent);
_ = try w.write("name:\n");
const name = package.name(constant_pool) catch return error.WriteFailed;
try name.indentedFormat(w, depth, indent + 1, constant_pool);
},
}
}
};