From 8d01979723e4a178c06a0864ab8d005c018ba4d1 Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Fri, 8 Nov 2024 13:04:10 -0800 Subject: [PATCH] Driver: decouple assembly codegen from frontend in a cleaner way --- src/aro/Driver.zig | 21 ++++++++++++--------- src/assembly_backend.zig | 3 +-- src/assembly_backend/x86_64.zig | 4 ++-- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/aro/Driver.zig b/src/aro/Driver.zig index b46618cb..61c4a89f 100644 --- a/src/aro/Driver.zig +++ b/src/aro/Driver.zig @@ -4,6 +4,7 @@ const Allocator = mem.Allocator; const process = std.process; const backend = @import("backend"); +const Assembly = backend.Assembly; const Ir = backend.Ir; const Object = backend.Object; @@ -15,6 +16,9 @@ const Preprocessor = @import("Preprocessor.zig"); const Source = @import("Source.zig"); const target_util = @import("target.zig"); const Toolchain = @import("Toolchain.zig"); +const Tree = @import("Tree.zig"); + +const AsmCodeGenFn = fn (target: std.Target, tree: Tree) Compilation.Error!Assembly; pub const Linker = enum { ld, @@ -681,7 +685,7 @@ pub fn errorDescription(e: anyerror) []const u8 { /// The entry point of the Aro compiler. /// **MAY call `exit` if `fast_exit` is set.** -pub fn main(d: *Driver, tc: *Toolchain, args: []const []const u8, comptime fast_exit: bool, asm_gen_fn: anytype) !void { +pub fn main(d: *Driver, tc: *Toolchain, args: []const []const u8, comptime fast_exit: bool, asm_gen_fn: ?AsmCodeGenFn) !void { var macro_buf = std.ArrayList(u8).init(d.comp.gpa); defer macro_buf.deinit(); @@ -805,7 +809,7 @@ fn processSource( builtin: Source, user_macros: Source, comptime fast_exit: bool, - asm_gen_fn: anytype, + asm_gen_fn: ?AsmCodeGenFn, ) !void { d.comp.generated_buf.items.len = 0; var pp = try Preprocessor.initDefault(d.comp); @@ -889,13 +893,12 @@ fn processSource( const out_file_name = try d.getOutFileName(source, &name_buf); if (d.use_assembly_backend) { - const assembly = asm_gen_fn(d.comp.target, tree) catch |er| switch (er) { - error.CodegenFailed => { - d.renderErrors(); - d.exitWithCleanup(1); - }, - else => |e| return e, - }; + const asm_fn = asm_gen_fn orelse return d.fatal( + "Assembly codegen not supported", + .{}, + ); + + const assembly = try asm_fn(d.comp.target, tree); defer assembly.deinit(d.comp.gpa); if (d.only_preprocess_and_compile) { diff --git a/src/assembly_backend.zig b/src/assembly_backend.zig index 3babd695..6ee2d8b8 100644 --- a/src/assembly_backend.zig +++ b/src/assembly_backend.zig @@ -1,11 +1,10 @@ const std = @import("std"); const aro = @import("aro"); -pub const Error = aro.Compilation.Error || error{CodegenFailed}; pub const x86_64 = @import("assembly_backend/x86_64.zig"); -pub fn genAsm(target: std.Target, tree: aro.Tree) Error!aro.Assembly { +pub fn genAsm(target: std.Target, tree: aro.Tree) aro.Compilation.Error!aro.Assembly { return switch (target.cpu.arch) { .x86_64 => x86_64.genAsm(tree), else => std.debug.panic("genAsm not implemented: {s}", .{@tagName(target.cpu.arch)}), diff --git a/src/assembly_backend/x86_64.zig b/src/assembly_backend/x86_64.zig index 88b1d823..d3b90325 100644 --- a/src/assembly_backend/x86_64.zig +++ b/src/assembly_backend/x86_64.zig @@ -14,7 +14,7 @@ const Type = aro.Type; const Value = aro.Value; const AsmCodeGen = @This(); -const Error = @import("../assembly_backend.zig").Error; +const Error = aro.Compilation.Error; const Writer = std.ArrayListUnmanaged(u8).Writer; tree: Tree, @@ -81,7 +81,7 @@ pub fn todo(c: *AsmCodeGen, msg: []const u8, node: NodeIndex) Error { .loc = loc, .extra = .{ .str = msg }, }, &.{}); - return error.CodegenFailed; + return error.FatalError; } fn emitAggregate(c: *AsmCodeGen, ty: Type, node: NodeIndex) !void {