Skip to content

Move Zir, InternPool, AstGen to std.zig #18127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

SuperAuguste
Copy link
Contributor

After some time living with their parents, the siblings must move to greener pastures. They leave their small hometown src with fond memories and many new adventures ahead in the big city of std.zig. 🥲

Can be rebased on #18124. :)

Comment on lines +38112 to +38381
ip.items.appendAssumeCapacity(.{
.tag = .type_error_union,
.data = ip.addExtraAssumeCapacity(InternPool.Tag.ErrorUnionType{
.error_set_type = error_set_type,
.payload_type = arg.bare_return_type,
}),
});
ip.items.appendAssumeCapacity(.{
.tag = .type_inferred_error_set,
.data = @intFromEnum(func_index),
});
ip.items.appendAssumeCapacity(.{
.tag = .type_function,
.data = func_type_extra_index,
});

const adapter: InternPool.KeyAdapter = .{ .intern_pool = ip };
const gop = ip.map.getOrPutAssumeCapacityAdapted(InternPool.Key{
.func = ip.extraFuncInstance(func_extra_index),
}, adapter);
if (gop.found_existing) {
// Hot path: undo the additions to our two arrays.
ip.items.len -= 4;
ip.extra.items.len = prev_extra_len;
return @enumFromInt(gop.index);
}

// Synchronize the map with items.
assert(!ip.map.getOrPutAssumeCapacityAdapted(InternPool.Key{ .error_union_type = .{
.error_set_type = error_set_type,
.payload_type = arg.bare_return_type,
} }, adapter).found_existing);
assert(!ip.map.getOrPutAssumeCapacityAdapted(InternPool.Key{
.inferred_error_set_type = func_index,
}, adapter).found_existing);
assert(!ip.map.getOrPutAssumeCapacityAdapted(InternPool.Key{
.func_type = ip.extraFuncType(func_type_extra_index),
}, adapter).found_existing);

return sema.finishFuncInstance(
generic_owner,
func_index,
func_extra_index,
arg.generation,
func_ty,
arg.section,
);
}

fn finishFuncInstance(
sema: *Sema,
generic_owner: InternPool.Index,
func_index: InternPool.Index,
func_extra_index: u32,
generation: u32,
func_ty: InternPool.Index,
section: InternPool.OptionalNullTerminatedString,
) Allocator.Error!InternPool.Index {
const gpa = sema.gpa;
const ip = &sema.mod.intern_pool;

const fn_owner_decl = sema.mod.declPtr(ip.funcDeclOwner(generic_owner));
const decl_index = try sema.mod.createDecl(.{
.name = undefined,
.src_namespace = fn_owner_decl.src_namespace,
.src_node = fn_owner_decl.src_node,
.src_line = fn_owner_decl.src_line,
.has_tv = true,
.owns_tv = true,
.ty = @import("type.zig").Type.fromInterned(func_ty),
.val = @import("value.zig").Value.fromInterned(func_index),
.alignment = .none,
.@"linksection" = section,
.@"addrspace" = fn_owner_decl.@"addrspace",
.analysis = .complete,
.zir_decl_index = fn_owner_decl.zir_decl_index,
.src_scope = fn_owner_decl.src_scope,
.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,
});
errdefer sema.mod.destroyDecl(decl_index);

// Populate the owner_decl field which was left undefined until now.
ip.extra.items[
func_extra_index + std.meta.fieldIndex(InternPool.Tag.FuncInstance, "owner_decl").?
] = @intFromEnum(decl_index);

// TODO: improve this name
const decl = sema.mod.declPtr(decl_index);
decl.name = try ip.getOrPutStringFmt(gpa, "{}__anon_{d}", .{
fn_owner_decl.name.fmt(ip), @intFromEnum(decl_index),
});

return func_index;
}
Copy link
Member

@andrewrk andrewrk Nov 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This really should all stay in InternPool. What was the motivation for moving it?

Edit: I see, it's the logic pertaining to Decl in finishFuncInstance.

Please wait on this changeset - I will work on a prerequisite change that actually makes functions not require an owner Decl at all - this is something that is now possible thanks to a bunch of other work done recently.

@andrewrk
Copy link
Member

Please wait for this change: #17796

After this I will migrate Decl into InternPool.

@tauoverpi
Copy link
Contributor

Is the intention for the move to expose the above modules to external tooling for analysis, optimization, or something else? I haven't been able to find a related/similar ticket on the issue tracker other than #5635

@SuperAuguste
Copy link
Contributor Author

SuperAuguste commented Dec 2, 2023

From my point of view @tauoverpi, this PR is for exposing Zig internals for external analysis tooling. In the future, with incremental compilation, this could be consumed by ZLS, but more immediately I want to use this in https://github.com/zigtools/scip-zig where static, non-incremental compiler information would be perfect. (It'd be a cool lil Zig demo for work :P)

@Vexu
Copy link
Member

Vexu commented Jan 21, 2024

Closing to clear up the pr queue.

@Vexu Vexu closed this Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants