authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2021-10-31 14:27:05+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2021-10-31 14:27:05+01:00
log8a55e6b6c4b4954474bc29eccdfda02b052ff71d
tree90660aff961e4ba42de397a452b09a67b2f25f63
parent0bdb367ee4330c12952642a3b9718e24430406cc
signaturelock-open Commit is signed but in an unrecognized format.

stage2 AArch64: introduce Emit.fail for handling errors in MIR emit


2 files changed, 25 insertions(+), 5 deletions(-)

src/arch/aarch64/CodeGen.zig+5-1
......@@ -309,12 +309,16 @@ pub fn generate(
309309 .bin_file = bin_file,
310310 .debug_output = debug_output,
311311 .target = &bin_file.options.target,
312 .src_loc = src_loc,
312313 .code = code,
313314 .prev_di_pc = 0,
314315 .prev_di_line = module_fn.lbrace_line,
315316 .prev_di_column = module_fn.lbrace_column,
316317 };
317 try emit.emitMir();
318 emit.emitMir() catch |err| switch (err) {
319 error.EmitFail => return FnResult{ .fail = emit.err_msg.? },
320 else => |e| return e,
321 };
318322
319323 if (function.err_msg) |em| {
320324 return FnResult{ .fail = em };
src/arch/aarch64/Emit.zig+20-4
......@@ -7,6 +7,8 @@ const math = std.math;
77const Mir = @import("Mir.zig");
88const bits = @import("bits.zig");
99const link = @import("../../link.zig");
10const Module = @import("../../Module.zig");
11const ErrorMsg = Module.ErrorMsg;
1012const assert = std.debug.assert;
1113const DW = std.dwarf;
1214const leb128 = std.leb;
......@@ -18,6 +20,8 @@ mir: Mir,
1820bin_file: *link.File,
1921debug_output: DebugInfoOutput,
2022target: *const std.Target,
23err_msg: ?*ErrorMsg = null,
24src_loc: Module.SrcLoc,
2125code: *std.ArrayList(u8),
2226
2327prev_di_line: u32,
......@@ -25,6 +29,11 @@ prev_di_column: u32,
2529/// Relative to the beginning of `code`.
2630prev_di_pc: usize,
2731
32const InnerError = error{
33 OutOfMemory,
34 EmitFail,
35};
36
2837pub fn emitMir(
2938 emit: *Emit,
3039) !void {
......@@ -80,6 +89,13 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void {
8089 std.mem.writeInt(u32, try emit.code.addManyAsArray(4), instruction.toU32(), endian);
8190}
8291
92fn fail(emit: *Emit, comptime format: []const u8, args: anytype) InnerError {
93 @setCold(true);
94 assert(emit.err_msg == null);
95 emit.err_msg = try ErrorMsg.create(emit.bin_file.allocator, emit.src_loc, format, args);
96 return error.EmitFail;
97}
98
8399fn moveImmediate(emit: *Emit, reg: Register, imm64: u64) !void {
84100 try emit.writeInstruction(Instruction.movz(reg, @truncate(u16, imm64), 0));
85101
......@@ -173,8 +189,8 @@ fn mirBranch(emit: *Emit, inst: Mir.Inst.Index) !void {
173189 _ = target_inst;
174190
175191 switch (tag) {
176 .b => @panic("Implement mirBranch"),
177 .bl => @panic("Implement mirBranch"),
192 .b => return emit.fail("Implement mirBranch", .{}),
193 .bl => return emit.fail("Implement mirBranch", .{}),
178194 else => unreachable,
179195 }
180196}
......@@ -255,7 +271,7 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
255271 .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),
256272 });
257273 } else {
258 @panic("Implement call_extern for linking backends != MachO");
274 return emit.fail("Implement call_extern for linking backends != MachO", .{});
259275 }
260276}
261277
......@@ -304,7 +320,7 @@ fn mirLoadMemory(emit: *Emit, inst: Mir.Inst.Index) !void {
304320 .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),
305321 });
306322 } else {
307 return @panic("TODO implement load_memory for PIE GOT indirection on this platform");
323 return emit.fail("TODO implement load_memory for PIE GOT indirection on this platform", .{});
308324 }
309325 } else {
310326 // The value is in memory at a hard-coded address.