Skip to content

Commit

Permalink
Zir: refactor declaration instruction representation
Browse files Browse the repository at this point in the history
The new representation is often more compact. It is also more
straightforward to understand: for instance, `extern` is represented on
the `declaration` instruction itself rather than using a special
instruction. The same applies to `var`, making both of these far more
compact.

This commit also separates the type and value bodies of a `declaration`
instruction. This is a prerequisite for ziglang#131.

In general, `declaration` now directly encodes details of the syntax
form used, and the embedded ZIR bodies are for actual expressions. The
only exception to this is functions, where ZIR is effectively designed
as if we had ziglang#1717. `extern fn` declarations are modeled as
`extern const` with a function type, and normal `fn` definitions are
modeled as `const` with a `func{,_fancy,_inferred}` instruction. This
may change in the future, but improving on this was out of scope for
this commit.
  • Loading branch information
mlugg committed Dec 23, 2024
1 parent af5e731 commit 18362eb
Show file tree
Hide file tree
Showing 14 changed files with 1,248 additions and 1,119 deletions.
1,118 changes: 582 additions & 536 deletions lib/std/zig/AstGen.zig

Large diffs are not rendered by default.

487 changes: 392 additions & 95 deletions lib/std/zig/Zir.zig

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions src/InternPool.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2018,7 +2018,6 @@ pub const Key = union(enum) {
ty: Index,
init: Index,
owner_nav: Nav.Index,
lib_name: OptionalNullTerminatedString,
is_threadlocal: bool,
is_weak_linkage: bool,
};
Expand Down Expand Up @@ -2741,7 +2740,6 @@ pub const Key = union(enum) {
return a_info.owner_nav == b_info.owner_nav and
a_info.ty == b_info.ty and
a_info.init == b_info.init and
a_info.lib_name == b_info.lib_name and
a_info.is_threadlocal == b_info.is_threadlocal and
a_info.is_weak_linkage == b_info.is_weak_linkage;
},
Expand Down Expand Up @@ -5573,9 +5571,6 @@ pub const Tag = enum(u8) {
/// May be `none`.
init: Index,
owner_nav: Nav.Index,
/// Library name if specified.
/// For example `extern "c" var stderrp = ...` would have 'c' as library name.
lib_name: OptionalNullTerminatedString,
flags: Flags,

pub const Flags = packed struct(u32) {
Expand Down Expand Up @@ -6928,7 +6923,6 @@ pub fn indexToKey(ip: *const InternPool, index: Index) Key {
.ty = extra.ty,
.init = extra.init,
.owner_nav = extra.owner_nav,
.lib_name = extra.lib_name,
.is_threadlocal = extra.flags.is_threadlocal,
.is_weak_linkage = extra.flags.is_weak_linkage,
} };
Expand Down Expand Up @@ -7575,7 +7569,6 @@ pub fn get(ip: *InternPool, gpa: Allocator, tid: Zcu.PerThread.Id, key: Key) All
.ty = variable.ty,
.init = variable.init,
.owner_nav = variable.owner_nav,
.lib_name = variable.lib_name,
.flags = .{
.is_const = false,
.is_threadlocal = variable.is_threadlocal,
Expand Down
Loading

0 comments on commit 18362eb

Please sign in to comment.