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

Simplify library usage #551

Merged
merged 3 commits into from
Nov 11, 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
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ pub fn build(b: *Build) !void {
.root_source_file = .{ .path = "src/main.zig" },
.optimize = mode,
.target = target,
.single_threaded = true,
});
exe.addModule("aro", aro_module);

Expand Down
8 changes: 6 additions & 2 deletions src/aro/Attribute.zig
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ fn ignoredAttrErr(p: *Parser, tok: TokenIndex, attr: Attribute.Tag, context: []c
defer p.strings.items.len = strings_top;

try p.strings.writer().print("attribute '{s}' ignored on {s}", .{ @tagName(attr), context });
const str = try p.comp.diag.arena.allocator().dupe(u8, p.strings.items[strings_top..]);
const str = try p.comp.diagnostics.arena.allocator().dupe(u8, p.strings.items[strings_top..]);
try p.errStr(.ignored_attribute, tok, str);
}

Expand Down Expand Up @@ -1030,7 +1030,11 @@ fn applyTransparentUnion(attr: Attribute, p: *Parser, tok: TokenIndex, ty: Type)
const field_size = field.ty.bitSizeof(p.comp).?;
if (field_size == first_field_size) continue;
const mapper = p.comp.string_interner.getSlowTypeMapper();
const str = try std.fmt.allocPrint(p.comp.diag.arena.allocator(), "'{s}' ({d}", .{ mapper.lookup(field.name), field_size });
const str = try std.fmt.allocPrint(
p.comp.diagnostics.arena.allocator(),
"'{s}' ({d}",
.{ mapper.lookup(field.name), field_size },
);
try p.errStr(.transparent_union_size, field.name_tok, str);
return p.errExtra(.transparent_union_size_note, fields[0].name_tok, .{ .unsigned = first_field_size });
}
Expand Down
4 changes: 2 additions & 2 deletions src/aro/Builtins.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ const Parser = @import("Parser.zig");
const Properties = @import("Builtins/Properties.zig");
pub const Builtin = @import("Builtins/Builtin.def").with(Properties);

const Builtins = @This();

const Expanded = struct {
ty: Type,
builtin: Builtin,
};

const NameToTypeMap = std.StringHashMapUnmanaged(Type);

const Builtins = @This();

_name_to_type_map: NameToTypeMap = .{},

pub fn deinit(b: *Builtins, gpa: std.mem.Allocator) void {
Expand Down
51 changes: 26 additions & 25 deletions src/aro/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const NodeIndex = Tree.NodeIndex;
const Type = @import("Type.zig");
const Value = @import("Value.zig");

const CodeGen = @This();

const WipSwitch = struct {
cases: Cases = .{},
default: ?Ir.Ref = null,
Expand All @@ -35,6 +33,8 @@ const Symbol = struct {

const Error = Compilation.Error;

const CodeGen = @This();

tree: Tree,
comp: *Compilation,
builder: Builder,
Expand All @@ -55,30 +55,31 @@ continue_label: Ir.Ref = undefined,
break_label: Ir.Ref = undefined,
return_label: Ir.Ref = undefined,

pub fn generateTree(comp: *Compilation, tree: Tree) Compilation.Error!Ir {
pub fn genIr(tree: Tree) Compilation.Error!Ir {
const gpa = tree.comp.gpa;
var c = CodeGen{
.builder = .{
.gpa = comp.gpa,
.interner = &comp.interner,
.arena = std.heap.ArenaAllocator.init(comp.gpa),
.gpa = tree.comp.gpa,
.interner = &tree.comp.interner,
.arena = std.heap.ArenaAllocator.init(gpa),
},
.tree = tree,
.comp = comp,
.comp = tree.comp,
.node_tag = tree.nodes.items(.tag),
.node_data = tree.nodes.items(.data),
.node_ty = tree.nodes.items(.ty),
};
defer c.symbols.deinit(c.comp.gpa);
defer c.ret_nodes.deinit(c.comp.gpa);
defer c.phi_nodes.deinit(c.comp.gpa);
defer c.record_elem_buf.deinit(c.comp.gpa);
defer c.record_cache.deinit(c.comp.gpa);
defer c.symbols.deinit(gpa);
defer c.ret_nodes.deinit(gpa);
defer c.phi_nodes.deinit(gpa);
defer c.record_elem_buf.deinit(gpa);
defer c.record_cache.deinit(gpa);
defer c.builder.deinit();

const node_tags = tree.nodes.items(.tag);
for (tree.root_decls) |decl| {
c.builder.arena.deinit();
c.builder.arena = std.heap.ArenaAllocator.init(comp.gpa);
c.builder.arena = std.heap.ArenaAllocator.init(gpa);

switch (node_tags[@intFromEnum(decl)]) {
.static_assert,
Expand Down Expand Up @@ -136,7 +137,7 @@ fn genType(c: *CodeGen, base_ty: Type) !Interner.Ref {

for (ty.data.record.fields) |field| {
if (!field.isRegularField()) {
return c.comp.diag.fatalNoSrc("TODO lower struct bitfields", .{});
return c.comp.diagnostics.fatalNoSrc("TODO lower struct bitfields", .{});
}
// TODO handle padding bits
const field_ref = try c.genType(field.ty);
Expand All @@ -148,13 +149,13 @@ fn genType(c: *CodeGen, base_ty: Type) !Interner.Ref {
});
},
.@"union" => {
return c.comp.diag.fatalNoSrc("TODO lower union types", .{});
return c.comp.diagnostics.fatalNoSrc("TODO lower union types", .{});
},
else => {},
}
if (ty.isPtr()) return .ptr;
if (ty.isFunc()) return .func;
if (!ty.isReal()) return c.comp.diag.fatalNoSrc("TODO lower complex types", .{});
if (!ty.isReal()) return c.comp.diagnostics.fatalNoSrc("TODO lower complex types", .{});
if (ty.isInt()) {
const bits = ty.bitSizeof(c.comp).?;
key = .{ .int_ty = @intCast(bits) };
Expand All @@ -168,7 +169,7 @@ fn genType(c: *CodeGen, base_ty: Type) !Interner.Ref {
const elem = try c.genType(ty.elemType());
key = .{ .vector_ty = .{ .child = elem, .len = @intCast(ty.data.array.len) } };
} else if (ty.is(.nullptr_t)) {
return c.comp.diag.fatalNoSrc("TODO lower nullptr_t", .{});
return c.comp.diagnostics.fatalNoSrc("TODO lower nullptr_t", .{});
}
return c.builder.interner.put(c.builder.gpa, key);
}
Expand Down Expand Up @@ -539,7 +540,7 @@ fn genExpr(c: *CodeGen, node: NodeIndex) Error!Ir.Ref {
.goto_stmt,
.computed_goto_stmt,
.nullptr_literal,
=> return c.comp.diag.fatalNoSrc("TODO CodeGen.genStmt {}\n", .{c.node_tag[@intFromEnum(node)]}),
=> return c.comp.diagnostics.fatalNoSrc("TODO CodeGen.genStmt {}\n", .{c.node_tag[@intFromEnum(node)]}),
.comma_expr => {
_ = try c.genExpr(data.bin.lhs);
return c.genExpr(data.bin.rhs);
Expand Down Expand Up @@ -733,7 +734,7 @@ fn genExpr(c: *CodeGen, node: NodeIndex) Error!Ir.Ref {
.null_to_pointer,
.union_cast,
.vector_splat,
=> return c.comp.diag.fatalNoSrc("TODO CodeGen gen CastKind {}\n", .{data.cast.kind}),
=> return c.comp.diagnostics.fatalNoSrc("TODO CodeGen gen CastKind {}\n", .{data.cast.kind}),
},
.binary_cond_expr => {
if (c.tree.value_map.get(data.if3.cond)) |cond| {
Expand Down Expand Up @@ -941,7 +942,7 @@ fn genExpr(c: *CodeGen, node: NodeIndex) Error!Ir.Ref {
.real_expr,
.sizeof_expr,
.special_builtin_call_one,
=> return c.comp.diag.fatalNoSrc("TODO CodeGen.genExpr {}\n", .{c.node_tag[@intFromEnum(node)]}),
=> return c.comp.diagnostics.fatalNoSrc("TODO CodeGen.genExpr {}\n", .{c.node_tag[@intFromEnum(node)]}),
else => unreachable, // Not an expression.
}
return .none;
Expand Down Expand Up @@ -996,7 +997,7 @@ fn genLval(c: *CodeGen, node: NodeIndex) Error!Ir.Ref {
.static_compound_literal_expr,
.thread_local_compound_literal_expr,
.static_thread_local_compound_literal_expr,
=> return c.comp.diag.fatalNoSrc("TODO CodeGen.genLval {}\n", .{c.node_tag[@intFromEnum(node)]}),
=> return c.comp.diagnostics.fatalNoSrc("TODO CodeGen.genLval {}\n", .{c.node_tag[@intFromEnum(node)]}),
else => unreachable, // Not an lval expression.
}
}
Expand Down Expand Up @@ -1156,7 +1157,7 @@ fn genBoolExpr(c: *CodeGen, base: NodeIndex, true_label: Ir.Ref, false_label: Ir
fn genBuiltinCall(c: *CodeGen, builtin: Builtin, arg_nodes: []const NodeIndex, ty: Type) Error!Ir.Ref {
_ = arg_nodes;
_ = ty;
return c.comp.diag.fatalNoSrc("TODO CodeGen.genBuiltinCall {s}\n", .{Builtin.nameFromTag(builtin.tag).span()});
return c.comp.diagnostics.fatalNoSrc("TODO CodeGen.genBuiltinCall {s}\n", .{Builtin.nameFromTag(builtin.tag).span()});
}

fn genCall(c: *CodeGen, fn_node: NodeIndex, arg_nodes: []const NodeIndex, ty: Type) Error!Ir.Ref {
Expand Down Expand Up @@ -1262,12 +1263,12 @@ fn genInitializer(c: *CodeGen, ptr: Ir.Ref, dest_ty: Type, initializer: NodeInde
.union_init_expr,
.array_filler_expr,
.default_init_expr,
=> return c.comp.diag.fatalNoSrc("TODO CodeGen.genInitializer {}\n", .{c.node_tag[@intFromEnum(initializer)]}),
=> return c.comp.diagnostics.fatalNoSrc("TODO CodeGen.genInitializer {}\n", .{c.node_tag[@intFromEnum(initializer)]}),
.string_literal_expr => {
const val = c.tree.value_map.get(initializer).?;
const str_ptr = try c.builder.addConstant(val.ref(), .ptr);
if (dest_ty.isArray()) {
return c.comp.diag.fatalNoSrc("TODO memcpy\n", .{});
return c.comp.diagnostics.fatalNoSrc("TODO memcpy\n", .{});
} else {
try c.builder.addStore(ptr, str_ptr);
}
Expand All @@ -1281,5 +1282,5 @@ fn genInitializer(c: *CodeGen, ptr: Ir.Ref, dest_ty: Type, initializer: NodeInde

fn genVar(c: *CodeGen, decl: NodeIndex) Error!void {
_ = decl;
return c.comp.diag.fatalNoSrc("TODO CodeGen.genVar\n", .{});
return c.comp.diagnostics.fatalNoSrc("TODO CodeGen.genVar\n", .{});
}
Loading
Loading