Skip to content

Commit 1b37dc8

Browse files
committed
sema: instead of saving the init bodies ranges in the InternPool, rebuild them at analysis time
1 parent c6fab90 commit 1b37dc8

File tree

6 files changed

+191
-177
lines changed

6 files changed

+191
-177
lines changed

src/AstGen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4954,7 +4954,7 @@ fn structDeclInner(
49544954

49554955
// The decl_inst is used as here so that we can easily reconstruct a mapping
49564956
// between it and the field type when the fields inits are analzyed.
4957-
const ri: ResultInfo = .{ .rl = if (field_type == .none) .none else .{ .coerced_ty = Zir.indexToRef(decl_inst) } };
4957+
const ri: ResultInfo = .{ .rl = if (field_type == .none) .none else .{ .coerced_ty = decl_inst.toRef() } };
49584958

49594959
const default_inst = try expr(&block_scope, &namespace.base, ri, member.ast.value_expr);
49604960
if (!block_scope.endsWithNoReturn()) {

src/Autodoc.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3708,8 +3708,8 @@ fn walkInstruction(
37083708

37093709
// Inside field init bodies, the struct decl instruction is used to refer to the
37103710
// field type during the second pass of analysis.
3711-
try self.repurposed_insts.put(self.arena, @intCast(inst_index), {});
3712-
defer _ = self.repurposed_insts.remove(@intCast(inst_index));
3711+
try self.repurposed_insts.put(self.arena, inst, {});
3712+
defer _ = self.repurposed_insts.remove(inst);
37133713

37143714
var field_type_refs: std.ArrayListUnmanaged(DocData.Expr) = .{};
37153715
var field_default_refs: std.ArrayListUnmanaged(?DocData.Expr) = .{};

src/InternPool.zig

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ pub const Key = union(enum) {
370370
layout: std.builtin.Type.ContainerLayout,
371371
field_names: NullTerminatedString.Slice,
372372
field_types: Index.Slice,
373-
field_inits_bodies: InitBody.Slice,
374373
field_inits: Index.Slice,
375374
field_aligns: Alignment.Slice,
376375
runtime_order: RuntimeOrder.Slice,
@@ -401,23 +400,6 @@ pub const Key = union(enum) {
401400
}
402401
};
403402

404-
pub const InitBody = struct {
405-
start: Zir.Inst.Index,
406-
len: u32,
407-
408-
pub const Slice = struct {
409-
start: u32,
410-
len: u32,
411-
412-
pub fn get(this: @This(), ip: *const InternPool) []InitBody {
413-
// Workaround for not being able to cast between slices of different lengths
414-
assert(ip.extra.items.len >= this.start + this.len * 2);
415-
const bodies_ptr: [*]InitBody = @ptrCast(ip.extra.items[this.start..].ptr);
416-
return bodies_ptr[0..this.len];
417-
}
418-
};
419-
};
420-
421403
pub const Offsets = struct {
422404
start: u32,
423405
len: u32,
@@ -3057,10 +3039,7 @@ pub const Tag = enum(u8) {
30573039
/// Trailing:
30583040
/// 0. type: Index for each fields_len
30593041
/// 1. name: NullTerminatedString for each fields_len
3060-
///
3061-
/// if tag is type_struct_packed_inits:
3062-
/// 2. body_indices: Zir.Inst.Index // for each field in declared order, a pair of start index and length.
3063-
/// 3. init: Index for each fields_len
3042+
/// 2. init: Index for each fields_len // if tag is type_struct_packed_inits
30643043
pub const TypeStructPacked = struct {
30653044
decl: Module.Decl.Index,
30663045
zir_index: Zir.Inst.Index,
@@ -3098,7 +3077,6 @@ pub const Tag = enum(u8) {
30983077
/// names_map: MapIndex,
30993078
/// name: NullTerminatedString // for each field in declared order
31003079
/// 2. if any_default_inits:
3101-
/// body_indices: Zir.Inst.Index // for each field in declared order, a pair of start index and length.
31023080
/// init: Index // for each field in declared order
31033081
/// 3. if has_namespace:
31043082
/// namespace: Module.Namespace.Index
@@ -3779,7 +3757,6 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
37793757
.layout = .Auto,
37803758
.field_names = .{ .start = 0, .len = 0 },
37813759
.field_types = .{ .start = 0, .len = 0 },
3782-
.field_inits_bodies = .{ .start = 0, .len = 0 },
37833760
.field_inits = .{ .start = 0, .len = 0 },
37843761
.field_aligns = .{ .start = 0, .len = 0 },
37853762
.runtime_order = .{ .start = 0, .len = 0 },
@@ -3796,7 +3773,6 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
37963773
.layout = .Auto,
37973774
.field_names = .{ .start = 0, .len = 0 },
37983775
.field_types = .{ .start = 0, .len = 0 },
3799-
.field_inits_bodies = .{ .start = 0, .len = 0 },
38003776
.field_inits = .{ .start = 0, .len = 0 },
38013777
.field_aligns = .{ .start = 0, .len = 0 },
38023778
.runtime_order = .{ .start = 0, .len = 0 },
@@ -4309,12 +4285,6 @@ fn extraStructType(ip: *const InternPool, extra_index: u32) Key.StructType {
43094285
index += fields_len;
43104286
break :t .{ names_map.toOptional(), names };
43114287
};
4312-
const field_inits_bodies: Key.StructType.InitBody.Slice = t: {
4313-
if (!s.data.flags.any_default_inits) break :t .{ .start = 0, .len = 0 };
4314-
const offsets: Key.StructType.InitBody.Slice = .{ .start = index, .len = fields_len };
4315-
index += fields_len * 2;
4316-
break :t offsets;
4317-
};
43184288
const field_inits: Index.Slice = t: {
43194289
if (!s.data.flags.any_default_inits) break :t .{ .start = 0, .len = 0 };
43204290
const inits: Index.Slice = .{ .start = index, .len = fields_len };
@@ -4358,7 +4328,6 @@ fn extraStructType(ip: *const InternPool, extra_index: u32) Key.StructType {
43584328
.field_types = field_types,
43594329
.names_map = names_map,
43604330
.field_names = field_names,
4361-
.field_inits_bodies = field_inits_bodies,
43624331
.field_inits = field_inits,
43634332
.namespace = namespace,
43644333
.field_aligns = field_aligns,
@@ -4385,15 +4354,8 @@ fn extraPackedStructType(ip: *const InternPool, extra_index: u32, inits: bool) K
43854354
.start = type_struct_packed.end + fields_len,
43864355
.len = fields_len,
43874356
},
4388-
.field_inits_bodies = if (inits) .{
4389-
.start = type_struct_packed.end + fields_len * 2,
4390-
.len = fields_len,
4391-
} else .{
4392-
.start = 0,
4393-
.len = 0,
4394-
},
43954357
.field_inits = if (inits) .{
4396-
.start = type_struct_packed.end + fields_len * 4,
4358+
.start = type_struct_packed.end + fields_len * 2,
43974359
.len = fields_len,
43984360
} else .{
43994361
.start = 0,
@@ -5466,7 +5428,6 @@ pub fn getStructType(
54665428
.layout = undefined,
54675429
.field_names = undefined,
54685430
.field_types = undefined,
5469-
.field_inits_bodies = undefined,
54705431
.field_inits = undefined,
54715432
.field_aligns = undefined,
54725433
.runtime_order = undefined,
@@ -5489,7 +5450,6 @@ pub fn getStructType(
54895450
try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(Tag.TypeStructPacked).Struct.fields.len +
54905451
ini.fields_len + // types
54915452
ini.fields_len + // names
5492-
ini.fields_len * 2 + // init bodies
54935453
ini.fields_len); // inits
54945454
try ip.items.append(gpa, .{
54955455
.tag = if (ini.any_default_inits) .type_struct_packed_inits else .type_struct_packed,
@@ -5509,7 +5469,6 @@ pub fn getStructType(
55095469
ip.extra.appendNTimesAssumeCapacity(@intFromEnum(Index.none), ini.fields_len);
55105470
ip.extra.appendNTimesAssumeCapacity(@intFromEnum(OptionalNullTerminatedString.none), ini.fields_len);
55115471
if (ini.any_default_inits) {
5512-
ip.extra.appendNTimesAssumeCapacity(0, ini.fields_len * 2);
55135472
ip.extra.appendNTimesAssumeCapacity(@intFromEnum(Index.none), ini.fields_len);
55145473
}
55155474
return @enumFromInt(ip.items.len - 1);
@@ -5521,7 +5480,7 @@ pub fn getStructType(
55215480
const comptime_elements_len = if (ini.any_comptime_fields) (ini.fields_len + 31) / 32 else 0;
55225481

55235482
try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(Tag.TypeStruct).Struct.fields.len +
5524-
(ini.fields_len * 7) + // types, names, init bodies, inits, runtime order, offsets
5483+
(ini.fields_len * 5) + // types, names, inits, runtime order, offsets
55255484
align_elements_len + comptime_elements_len +
55265485
2); // names_map + namespace
55275486
try ip.items.append(gpa, .{
@@ -5559,7 +5518,6 @@ pub fn getStructType(
55595518
ip.extra.appendNTimesAssumeCapacity(@intFromEnum(OptionalNullTerminatedString.none), ini.fields_len);
55605519
}
55615520
if (ini.any_default_inits) {
5562-
ip.extra.appendNTimesAssumeCapacity(0, ini.fields_len * 2);
55635521
ip.extra.appendNTimesAssumeCapacity(@intFromEnum(Index.none), ini.fields_len);
55645522
}
55655523
if (ini.namespace.unwrap()) |namespace| {

0 commit comments

Comments
 (0)