Skip to content

Commit

Permalink
Driver: decouple assembly codegen from frontend in a cleaner way
Browse files Browse the repository at this point in the history
  • Loading branch information
ehaas committed Nov 9, 2024
1 parent e71d9ee commit 8d01979
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
21 changes: 12 additions & 9 deletions src/aro/Driver.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions src/assembly_backend.zig
Original file line number Diff line number Diff line change
@@ -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)}),
Expand Down
4 changes: 2 additions & 2 deletions src/assembly_backend/x86_64.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8d01979

Please sign in to comment.