Skip to content

Commit

Permalink
Zir: represent declarations via an instruction
Browse files Browse the repository at this point in the history
This commit changes how declarations (`const`, `fn`, `usingnamespace`,
etc) are represented in ZIR. Previously, these were represented in the
container type's extra data (e.g. as trailing data on a `struct_decl`).
However, this introduced the complexity of the ZIR mapping logic having
to also correlate some ZIR extra data indices. That isn't really a
problem today, but it's tricky for the introduction of `TrackedInst` in
the commit following this one. Instead, these type declarations now
simply contain a trailing list of ZIR indices to `declaration`
instructions, which directly encode all data related to the declaration
(including containing the declaration's body). Additionally, the ZIR for
`align` etc have been split out into their own bodies. This is not
strictly necessary, but it's much simpler to understand for an
insignificant cost in bytes, and will simplify the resolution of ziglang#131
(where we may need to evaluate the pointer type, including align etc,
without immediately evaluating the value body).
  • Loading branch information
mlugg committed Jan 23, 2024
1 parent 9e684e8 commit 2446df2
Show file tree
Hide file tree
Showing 8 changed files with 764 additions and 739 deletions.
384 changes: 225 additions & 159 deletions src/AstGen.zig

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/Autodoc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4078,6 +4078,9 @@ fn analyzeAllDecls(
priv_decl_indexes: *std.ArrayListUnmanaged(usize),
call_ctx: ?*const CallContext,
) AutodocErrors!usize {
//if (true) @compileError("TODO AUTODOC");
if (true) @panic("TODO AUTODOC");

const first_decl_indexes_slot = decl_indexes.items.len;
const original_it = file.zir.declIterator(parent_inst);

Expand Down
2 changes: 0 additions & 2 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6186,8 +6186,6 @@ fn finishFuncInstance(
.generation = generation,
.is_pub = fn_owner_decl.is_pub,
.is_exported = fn_owner_decl.is_exported,
.has_linksection_or_addrspace = fn_owner_decl.has_linksection_or_addrspace,
.has_align = fn_owner_decl.has_align,
.alive = true,
.kind = .anon,
});
Expand Down
476 changes: 233 additions & 243 deletions src/Module.zig

Large diffs are not rendered by default.

32 changes: 20 additions & 12 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,10 @@ fn analyzeBodyInner(
.trap => break sema.zirTrap(block, inst),
// zig fmt: on

// This instruction never exists in an analyzed body. It exists only in the declaration
// list for a container type.
.declaration => unreachable,

.extended => ext: {
const extended = datas[@intFromEnum(inst)].extended;
break :ext switch (extended.opcode) {
Expand Down Expand Up @@ -2692,7 +2696,9 @@ pub fn getStructType(
}
}

extra_index = try mod.scanNamespace(namespace, extra_index, decls_len, mod.declPtr(decl));
const decls = sema.code.bodySlice(extra_index, decls_len);
try mod.scanNamespace(namespace, decls, mod.declPtr(decl));
extra_index += decls_len;

const ty = try ip.getStructType(gpa, .{
.decl = decl,
Expand Down Expand Up @@ -2929,7 +2935,9 @@ fn zirEnumDecl(
const new_namespace = mod.namespacePtr(new_namespace_index);
errdefer if (!done) mod.destroyNamespace(new_namespace_index);

extra_index = try mod.scanNamespace(new_namespace_index, extra_index, decls_len, new_decl);
const decls = sema.code.bodySlice(extra_index, decls_len);
try mod.scanNamespace(new_namespace_index, decls, new_decl);
extra_index += decls_len;

const body = sema.code.bodySlice(extra_index, body_len);
extra_index += body.len;
Expand Down Expand Up @@ -3219,7 +3227,8 @@ fn zirUnionDecl(
new_decl.val = Value.fromInterned(union_ty);
new_namespace.ty = Type.fromInterned(union_ty);

_ = try mod.scanNamespace(new_namespace_index, extra_index, decls_len, new_decl);
const decls = sema.code.bodySlice(extra_index, decls_len);
try mod.scanNamespace(new_namespace_index, decls, new_decl);

const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
try mod.finalizeAnonDecl(new_decl_index);
Expand Down Expand Up @@ -3282,7 +3291,8 @@ fn zirOpaqueDecl(
new_decl.val = Value.fromInterned(opaque_ty);
new_namespace.ty = Type.fromInterned(opaque_ty);

extra_index = try mod.scanNamespace(new_namespace_index, extra_index, decls_len, new_decl);
const decls = sema.code.bodySlice(extra_index, decls_len);
try mod.scanNamespace(new_namespace_index, decls, new_decl);

const decl_val = sema.analyzeDeclVal(block, src, new_decl_index);
try mod.finalizeAnonDecl(new_decl_index);
Expand Down Expand Up @@ -36252,9 +36262,7 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct {
}

// Skip over decls.
var decls_it = zir.declIteratorInner(extra_index, decls_len);
while (decls_it.next()) |_| {}
extra_index = decls_it.extra_index;
extra_index += decls_len;

return .{ fields_len, small, extra_index };
}
Expand Down Expand Up @@ -36715,9 +36723,7 @@ fn semaUnionFields(mod: *Module, arena: Allocator, union_type: InternPool.Key.Un
} else 0;

// Skip over decls.
var decls_it = zir.declIteratorInner(extra_index, decls_len);
while (decls_it.next()) |_| {}
extra_index = decls_it.extra_index;
extra_index += decls_len;

const body = zir.bodySlice(extra_index, body_len);
extra_index += body.len;
Expand Down Expand Up @@ -37710,10 +37716,12 @@ pub fn analyzeAddressSpace(
ctx: AddressSpaceContext,
) !std.builtin.AddressSpace {
const mod = sema.mod;
const addrspace_tv = try sema.resolveInstConst(block, src, zir_ref, .{
const air_ref = try sema.resolveInst(zir_ref);
const coerced = try sema.coerce(block, Type.fromInterned(.address_space_type), air_ref, src);
const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{
.needed_comptime_reason = "address space must be comptime-known",
});
const address_space = mod.toEnum(std.builtin.AddressSpace, addrspace_tv.val);
const address_space = mod.toEnum(std.builtin.AddressSpace, addrspace_val);
const target = sema.mod.getTarget();
const arch = target.cpu.arch;

Expand Down
Loading

0 comments on commit 2446df2

Please sign in to comment.