Skip to content
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

BuiltinFunction: Use a generated DAFSA instead of a generated enum #524

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
951 changes: 951 additions & 0 deletions scripts/generate_builtins_dafsa.zig

Large diffs are not rendered by default.

499 changes: 0 additions & 499 deletions scripts/process_builtins.py

This file was deleted.

35 changes: 22 additions & 13 deletions src/Builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ fn createType(desc: TypeDescription, it: *TypeDescription.TypeIterator, comp: *c
std.debug.assert(builder.specifier == .none);
builder.specifier = Type.Builder.fromType(comp.types.ns_constant_string.ty);
},
.G => {
// Todo: id
return .{ .specifier = .invalid };
},
.H => {
// Todo: SEL
return .{ .specifier = .invalid };
},
.M => {
// Todo: struct objc_super
return .{ .specifier = .invalid };
},
.a => {
std.debug.assert(builder.specifier == .none);
std.debug.assert(desc.suffix.len == 0);
Expand Down Expand Up @@ -260,8 +272,7 @@ fn createBuiltin(comp: *const Compilation, builtin: BuiltinFunction, type_arena:

/// Asserts that the builtin has already been created
pub fn lookup(b: *const Builtins, name: []const u8) Expanded {
@setEvalBranchQuota(10_000);
const builtin = BuiltinFunction.fromTag(std.meta.stringToEnum(BuiltinFunction.Tag, name).?);
const builtin = BuiltinFunction.fromName(name).?;
const ty = b._name_to_type_map.get(name).?;
return .{
.builtin = builtin,
Expand All @@ -271,9 +282,7 @@ pub fn lookup(b: *const Builtins, name: []const u8) Expanded {

pub fn getOrCreate(b: *Builtins, comp: *Compilation, name: []const u8, type_arena: std.mem.Allocator) !?Expanded {
const ty = b._name_to_type_map.get(name) orelse {
@setEvalBranchQuota(10_000);
const tag = std.meta.stringToEnum(BuiltinFunction.Tag, name) orelse return null;
const builtin = BuiltinFunction.fromTag(tag);
const builtin = BuiltinFunction.fromName(name) orelse return null;
if (!comp.hasBuiltinFunction(builtin)) return null;

try b._name_to_type_map.ensureUnusedCapacity(comp.gpa, 1);
Expand All @@ -285,7 +294,7 @@ pub fn getOrCreate(b: *Builtins, comp: *Compilation, name: []const u8, type_aren
.ty = ty,
};
};
const builtin = BuiltinFunction.fromTag(std.meta.stringToEnum(BuiltinFunction.Tag, name).?);
const builtin = BuiltinFunction.fromName(name).?;
return .{
.builtin = builtin,
.ty = ty,
Expand All @@ -301,9 +310,9 @@ test "All builtins" {

const type_arena = arena.allocator();

for (0..@typeInfo(BuiltinFunction.Tag).Enum.fields.len) |i| {
const tag: BuiltinFunction.Tag = @enumFromInt(i);
const name = @tagName(tag);
var builtin_it = BuiltinFunction.BuiltinsIterator{};
while (builtin_it.next()) |entry| {
const name = try type_arena.dupe(u8, entry.name);
if (try comp.builtins.getOrCreate(&comp, name, type_arena)) |func_ty| {
const get_again = (try comp.builtins.getOrCreate(&comp, name, std.testing.failing_allocator)).?;
const found_by_lookup = comp.builtins.lookup(name);
Expand All @@ -325,10 +334,10 @@ test "Allocation failures" {
const type_arena = arena.allocator();

const num_builtins = 40;
for (0..num_builtins) |i| {
const tag: BuiltinFunction.Tag = @enumFromInt(i);
const name = @tagName(tag);
_ = try comp.builtins.getOrCreate(&comp, name, type_arena);
var builtin_it = BuiltinFunction.BuiltinsIterator{};
for (0..num_builtins) |_| {
const entry = builtin_it.next().?;
_ = try comp.builtins.getOrCreate(&comp, entry.name, type_arena);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,7 @@ fn genBoolExpr(c: *CodeGen, base: NodeIndex, true_label: Ir.Ref, false_label: Ir
fn genBuiltinCall(c: *CodeGen, builtin: BuiltinFunction, arg_nodes: []const NodeIndex, ty: Type) Error!Ir.Ref {
_ = arg_nodes;
_ = ty;
return c.comp.diag.fatalNoSrc("TODO CodeGen.genBuiltinCall {s}\n", .{@tagName(builtin.tag)});
return c.comp.diag.fatalNoSrc("TODO CodeGen.genBuiltinCall {s}\n", .{BuiltinFunction.nameFromTag(builtin.tag).span()});
}

fn genCall(c: *CodeGen, fn_node: NodeIndex, arg_nodes: []const NodeIndex, ty: Type) Error!Ir.Ref {
Expand Down
4 changes: 1 addition & 3 deletions src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1400,9 +1400,7 @@ pub fn hasBuiltin(comp: *const Compilation, name: []const u8) bool {
std.mem.eql(u8, name, "__builtin_offsetof") or
std.mem.eql(u8, name, "__builtin_types_compatible_p")) return true;

@setEvalBranchQuota(10_000);
const tag = std.meta.stringToEnum(BuiltinFunction.Tag, name) orelse return false;
const builtin = BuiltinFunction.fromTag(tag);
const builtin = BuiltinFunction.fromName(name) orelse return false;
return comp.hasBuiltinFunction(builtin);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2677,7 +2677,7 @@ pub fn renderMessage(comp: *Compilation, m: anytype, msg: Message) void {
}),
.builtin_with_header => m.print(info.msg, .{
@tagName(msg.extra.builtin_with_header.header),
@tagName(msg.extra.builtin_with_header.builtin),
BuiltinFunction.nameFromTag(msg.extra.builtin_with_header.builtin).span(),
}),
else => @compileError("invalid extra kind " ++ @tagName(info.extra)),
}
Expand Down
23 changes: 16 additions & 7 deletions src/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4622,7 +4622,10 @@ const CallExpr = union(enum) {
return switch (self) {
.standard => true,
.builtin => |builtin| switch (builtin.tag) {
.__builtin_va_start, .__va_start, .va_start => arg_idx != 1,
BuiltinFunction.tagFromName("__builtin_va_start").?,
BuiltinFunction.tagFromName("__va_start").?,
BuiltinFunction.tagFromName("va_start").?,
=> arg_idx != 1,
else => true,
},
};
Expand All @@ -4632,8 +4635,11 @@ const CallExpr = union(enum) {
return switch (self) {
.standard => true,
.builtin => |builtin| switch (builtin.tag) {
.__builtin_va_start, .__va_start, .va_start => arg_idx != 1,
.__builtin_complex => false,
BuiltinFunction.tagFromName("__builtin_va_start").?,
BuiltinFunction.tagFromName("__va_start").?,
BuiltinFunction.tagFromName("va_start").?,
=> arg_idx != 1,
BuiltinFunction.tagFromName("__builtin_complex").? => false,
else => true,
},
};
Expand All @@ -4650,8 +4656,11 @@ const CallExpr = union(enum) {

const builtin_tok = p.nodes.items(.data)[@intFromEnum(self.builtin.node)].decl.name;
switch (self.builtin.tag) {
.__builtin_va_start, .__va_start, .va_start => return p.checkVaStartArg(builtin_tok, first_after, param_tok, arg, arg_idx),
.__builtin_complex => return p.checkComplexArg(builtin_tok, first_after, param_tok, arg, arg_idx),
BuiltinFunction.tagFromName("__builtin_va_start").?,
BuiltinFunction.tagFromName("__va_start").?,
BuiltinFunction.tagFromName("va_start").?,
=> return p.checkVaStartArg(builtin_tok, first_after, param_tok, arg, arg_idx),
BuiltinFunction.tagFromName("__builtin_complex").? => return p.checkComplexArg(builtin_tok, first_after, param_tok, arg, arg_idx),
else => {},
}
}
Expand All @@ -4665,7 +4674,7 @@ const CallExpr = union(enum) {
return switch (self) {
.standard => null,
.builtin => |builtin| switch (builtin.tag) {
.__builtin_complex => 2,
BuiltinFunction.tagFromName("__builtin_complex").? => 2,
else => null,
},
};
Expand All @@ -4675,7 +4684,7 @@ const CallExpr = union(enum) {
return switch (self) {
.standard => callable_ty.returnType(),
.builtin => |builtin| switch (builtin.tag) {
.__builtin_complex => {
BuiltinFunction.tagFromName("__builtin_complex").? => {
const last_param = p.list_buf.items[p.list_buf.items.len - 1];
return p.nodes.items(.ty)[@intFromEnum(last_param)].makeComplex();
},
Expand Down
Loading
Loading