{{ message }}
Repository navigation
Expand file tree
/
Copy pathConstantPool.zig
More file actions
383 lines (303 loc) · 11.1 KB
/
Copy pathConstantPool.zig
File metadata and controls
383 lines (303 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
const std = @import("std");
const ConstantPool = @This();
allocator: std.mem.Allocator,
entries: std.ArrayListUnmanaged(Entry),
utf8_entries_map: std.StringHashMapUnmanaged(u16),
pub fn init(allocator: std.mem.Allocator, entry_count: u16) !*ConstantPool {
var c = try allocator.create(ConstantPool);
c.* = ConstantPool{
.allocator = allocator,
.entries = try std.ArrayListUnmanaged(Entry).initCapacity(allocator, entry_count),
.utf8_entries_map = std.StringHashMapUnmanaged(u16){},
};
c.entries.items.len = entry_count;
return c;
}
pub fn get(self: *const ConstantPool, index: u16) Entry {
// std.log.info("{any}", .{self.entries});
return self.entries.items[index - 1];
}
pub fn deinit(self: *ConstantPool) void {
for (self.entries.items) |entry| {
switch (entry) {
.utf8 => |info| self.allocator.free(info.bytes),
else => {},
}
}
self.entries.deinit(self.allocator);
self.utf8_entries_map.deinit(self.allocator);
self.allocator.destroy(self);
}
pub fn Serialize(comptime T: type) type {
return struct {
pub fn decode(constant_pool: *const ConstantPool, reader: anytype) !T {
var value: T = undefined;
value.constant_pool = constant_pool;
inline for (std.meta.fields(T)[1..]) |field| {
@field(value, field.name) = switch (@typeInfo(field.type)) {
.Int => try reader.readIntBig(field.type),
.Enum => |info| @intToEnum(field.type, try reader.readIntBig(info.tag_type)),
else => @compileError("Decode not implemented: " ++ @typeName(field.type)),
};
}
return value;
}
};
}
/// Locate a Utf8Info entry that has the value `bytes`
/// Useful for attributes that need an entry describing their name
pub fn locateUtf8Entry(self: *ConstantPool, bytes: []const u8) !u16 {
var get_or_put_output = try self.utf8_entries_map.getOrPut(self.allocator, bytes);
if (get_or_put_output.found_existing) {
return get_or_put_output.value_ptr.*;
} else {
var entry = try self.entries.addOne(self.allocator);
get_or_put_output.value_ptr.* = @intCast(u16, self.entries.items.len);
entry.* = Entry{ .utf8 = .{ .constant_pool = self, .bytes = try self.allocator.dupe(u8, bytes) } };
return get_or_put_output.value_ptr.*;
}
}
pub fn decodeEntries(self: *ConstantPool, reader: anytype) !void {
var constant_pool_index: u16 = 0;
while (constant_pool_index < self.entries.items.len) : (constant_pool_index += 1) {
var cp = try self.decodeEntry(reader);
if (cp == .utf8) {
var get_or_put_output = try self.utf8_entries_map.getOrPut(self.allocator, cp.utf8.bytes);
if (!get_or_put_output.found_existing) {
get_or_put_output.value_ptr.* = constant_pool_index + 1;
}
}
self.entries.items[constant_pool_index] = cp;
// Doubles and longs take up two slots because Java is bad (https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-4.html#jvms-4.10.2.3)
if (cp == .double or cp == .long) {
constant_pool_index += 1;
}
}
}
pub fn decodeEntry(self: *ConstantPool, reader: anytype) !Entry {
var tag = try reader.readIntBig(u8);
inline for (@typeInfo(Tag).Enum.fields) |f, i| {
const this_tag_value = @field(Tag, f.name);
if (tag == @enumToInt(this_tag_value)) {
const T = std.meta.fields(Entry)[i].type;
var value = if (@hasDecl(T, "decode")) try @field(T, "decode")(self, reader) else try Serialize(T).decode(self, reader);
return @unionInit(Entry, f.name, value);
}
}
unreachable;
}
pub const Tag = enum(u8) {
class = 7,
fieldref = 9,
methodref = 10,
interface_methodref = 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 const ClassInfo = struct {
constant_pool: *const ConstantPool,
/// Points to a `Utf8Info`
name_index: u16,
pub fn getName(self: ClassInfo) Utf8Info {
return self.constant_pool.get(self.name_index).utf8;
}
};
pub const RefInfo = struct {
constant_pool: *const ConstantPool,
/// Points to class or interface
class_index: u16,
/// Points to a `NameAndTypeInfo`
name_and_type_index: u16,
pub fn getClassInfo(self: RefInfo) ClassInfo {
return self.constant_pool.get(self.class_index).class;
}
pub fn getNameAndTypeInfo(self: RefInfo) NameAndTypeInfo {
return self.constant_pool.get(self.name_and_type_index).name_and_type;
}
};
/// Points to a `Utf8Info`
pub const StringInfo = struct {
constant_pool: *const ConstantPool,
string_index: u16,
};
/// Represents 4-byte (32 bit) integer
pub const IntegerInfo = struct {
constant_pool: *const ConstantPool,
bytes: u32,
};
/// Represents 4-byte (32 bit) float
pub const FloatInfo = struct {
constant_pool: *const ConstantPool,
bytes: u32,
};
pub const LongInfo = struct {
constant_pool: *const ConstantPool,
bytes: u64,
};
pub const DoubleInfo = struct {
constant_pool: *const ConstantPool,
bytes: u64,
};
pub const NameAndTypeInfo = struct {
const Self = @This();
constant_pool: *const ConstantPool,
/// Points to a `Utf8Info` describing a unique field or method name or <init>
name_index: u16,
/// Points to a `Utf8Info` representing a field or method descriptor
descriptor_index: u16,
pub fn getName(self: Self) Utf8Info {
return self.constant_pool.get(self.name_index).utf8;
}
pub fn getDescriptor(self: Self) Utf8Info {
return self.constant_pool.get(self.descriptor_index).utf8;
}
};
pub const Utf8Info = struct {
const Self = @This();
constant_pool: *const ConstantPool,
bytes: []u8,
pub fn decode(constant_pool: *const ConstantPool, reader: anytype) !Self {
var length = try reader.readIntBig(u16);
var bytes = try constant_pool.allocator.alloc(u8, length);
_ = try reader.readAll(bytes);
return Self{
.constant_pool = constant_pool,
.bytes = bytes,
};
}
pub fn encode(self: Utf8Info, writer: anytype) !void {
try writer.writeIntBig(u16, @intCast(u16, self.bytes.len));
try writer.writeAll(self.bytes);
}
pub fn format(value: Utf8Info, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
_ = fmt;
_ = options;
try writer.print("Utf8Info(\"{s}\")", .{value.bytes});
}
};
pub const ReferenceKind = 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 const MethodHandleInfo = struct {
const Self = @This();
constant_pool: *const ConstantPool,
reference_kind: ReferenceKind,
/// Based on ref kind:
/// 1, 2, 3, 4 - points to fieldref
/// 5, 8 - points to methodref
/// 6, 7 - points to methodref or interfacemethodref
/// 9 - Must point to interfacemethodref
reference_index: u16,
// fn parse(allocator: std.mem.Allocator, reader: anytype) !Self {
// return Self{
// .reference_kind = @intToEnum(ReferenceKind, try reader.readIntBig(u8)),
// .reference_index = try reader.readIntBig(u16),
// };
// }
pub fn getReference(self: Self, constant_pool: []Entry) Entry {
var ref = constant_pool[self.reference_index - 1];
switch (self.reference_kind) {
.get_field, .get_static, .put_field, .put_static => std.debug.assert(std.meta.activeTag(ref) == .fieldref),
.invoke_virtual, .new_invoke_special => std.debug.assert(std.meta.activeTag(ref) == .methodref),
.invoke_static, .invoke_special => std.debug.assert(std.meta.activeTag(ref) == .methodref or std.meta.activeTag(ref) == .interface_methodref),
.invoke_interface => std.debug.assert(std.meta.activeTag(ref) == .interface_methodref),
}
return ref;
}
};
pub const MethodTypeInfo = struct {
const Self = @This();
constant_pool: *const ConstantPool,
descriptor_index: u16,
pub fn getDescriptor(self: Self, constant_pool: []Entry) Utf8Info {
return constant_pool[self.descriptor_index - 1].utf8;
}
};
pub const DynamicInfo = struct {
constant_pool: *const ConstantPool,
bootstrap_method_attr_index: u16,
name_and_type_index: u16,
pub fn getNameAndTypeInfo(self: DynamicInfo, constant_pool: []Entry) NameAndTypeInfo {
return constant_pool[self.name_and_type_index - 1].name_and_type;
}
};
pub const InvokeDynamicInfo = struct {
constant_pool: *const ConstantPool,
bootstrap_method_attr_index: u16,
name_and_type_index: u16,
pub fn getNameAndTypeInfo(self: InvokeDynamicInfo, constant_pool: []Entry) NameAndTypeInfo {
return constant_pool[self.name_and_type_index - 1].name_and_type;
}
};
pub const ModuleInfo = struct {
constant_pool: *const ConstantPool,
name_index: u16,
pub fn getName(self: ModuleInfo, constant_pool: []Entry) Utf8Info {
return constant_pool[self.name_index - 1].utf8;
}
};
pub const PackageInfo = struct {
constant_pool: *const ConstantPool,
name_index: u16,
pub fn getName(self: PackageInfo, constant_pool: []Entry) Utf8Info {
return constant_pool[self.name_index - 1].utf8;
}
};
pub const Entry = union(Tag) {
const Self = @This();
class: ClassInfo,
fieldref: RefInfo,
methodref: RefInfo,
interface_methodref: RefInfo,
string: StringInfo,
integer: IntegerInfo,
float: FloatInfo,
long: LongInfo,
double: DoubleInfo,
name_and_type: NameAndTypeInfo,
utf8: Utf8Info,
method_handle: MethodHandleInfo,
method_type: MethodTypeInfo,
dynamic: DynamicInfo,
invoke_dynamic: InvokeDynamicInfo,
module: ModuleInfo,
package: PackageInfo,
pub fn encode(self: Entry, writer: anytype) !void {
inline for (@typeInfo(Tag).Enum.fields) |f, i| {
const this_tag_value = @field(Tag, f.name);
if (@enumToInt(self) == @enumToInt(this_tag_value)) {
try writer.writeByte(@enumToInt(self));
const T = std.meta.fields(Entry)[i].type;
var value = @field(self, f.name);
if (@hasDecl(T, "encode"))
return try @field(value, "encode")(writer);
inline for (std.meta.fields(T)[1..]) |field| {
switch (@typeInfo(field.type)) {
.Int => try writer.writeIntBig(field.type, @field(value, field.name)),
.Enum => |info| try writer.writeIntBig(info.tag_type, @enumToInt(@field(value, field.name))),
else => @compileError("Encode not implemented: " ++ @typeName(field.type)),
}
}
}
}
}
};
You can’t perform that action at this time.
