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(...@@ -309,12 +309,16 @@ pub fn generate(
309 .bin_file = bin_file,309 .bin_file = bin_file,
310 .debug_output = debug_output,310 .debug_output = debug_output,
311 .target = &bin_file.options.target,311 .target = &bin_file.options.target,
312 .src_loc = src_loc,
312 .code = code,313 .code = code,
313 .prev_di_pc = 0,314 .prev_di_pc = 0,
314 .prev_di_line = module_fn.lbrace_line,315 .prev_di_line = module_fn.lbrace_line,
315 .prev_di_column = module_fn.lbrace_column,316 .prev_di_column = module_fn.lbrace_column,
316 };317 };
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
319 if (function.err_msg) |em| {323 if (function.err_msg) |em| {
320 return FnResult{ .fail = em };324 return FnResult{ .fail = em };
src/arch/aarch64/Emit.zig+20-4
...@@ -7,6 +7,8 @@ const math = std.math;...@@ -7,6 +7,8 @@ const math = std.math;
7const Mir = @import("Mir.zig");7const Mir = @import("Mir.zig");
8const bits = @import("bits.zig");8const bits = @import("bits.zig");
9const link = @import("../../link.zig");9const link = @import("../../link.zig");
10const Module = @import("../../Module.zig");
11const ErrorMsg = Module.ErrorMsg;
10const assert = std.debug.assert;12const assert = std.debug.assert;
11const DW = std.dwarf;13const DW = std.dwarf;
12const leb128 = std.leb;14const leb128 = std.leb;
...@@ -18,6 +20,8 @@ mir: Mir,...@@ -18,6 +20,8 @@ mir: Mir,
18bin_file: *link.File,20bin_file: *link.File,
19debug_output: DebugInfoOutput,21debug_output: DebugInfoOutput,
20target: *const std.Target,22target: *const std.Target,
23err_msg: ?*ErrorMsg = null,
24src_loc: Module.SrcLoc,
21code: *std.ArrayList(u8),25code: *std.ArrayList(u8),
2226
23prev_di_line: u32,27prev_di_line: u32,
...@@ -25,6 +29,11 @@ prev_di_column: u32,...@@ -25,6 +29,11 @@ prev_di_column: u32,
25/// Relative to the beginning of `code`.29/// Relative to the beginning of `code`.
26prev_di_pc: usize,30prev_di_pc: usize,
2731
32const InnerError = error{
33 OutOfMemory,
34 EmitFail,
35};
36
28pub fn emitMir(37pub fn emitMir(
29 emit: *Emit,38 emit: *Emit,
30) !void {39) !void {
...@@ -80,6 +89,13 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void {...@@ -80,6 +89,13 @@ fn writeInstruction(emit: *Emit, instruction: Instruction) !void {
80 std.mem.writeInt(u32, try emit.code.addManyAsArray(4), instruction.toU32(), endian);89 std.mem.writeInt(u32, try emit.code.addManyAsArray(4), instruction.toU32(), endian);
81}90}
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
83fn moveImmediate(emit: *Emit, reg: Register, imm64: u64) !void {99fn moveImmediate(emit: *Emit, reg: Register, imm64: u64) !void {
84 try emit.writeInstruction(Instruction.movz(reg, @truncate(u16, imm64), 0));100 try emit.writeInstruction(Instruction.movz(reg, @truncate(u16, imm64), 0));
85101
...@@ -173,8 +189,8 @@ fn mirBranch(emit: *Emit, inst: Mir.Inst.Index) !void {...@@ -173,8 +189,8 @@ fn mirBranch(emit: *Emit, inst: Mir.Inst.Index) !void {
173 _ = target_inst;189 _ = target_inst;
174190
175 switch (tag) {191 switch (tag) {
176 .b => @panic("Implement mirBranch"),192 .b => return emit.fail("Implement mirBranch", .{}),
177 .bl => @panic("Implement mirBranch"),193 .bl => return emit.fail("Implement mirBranch", .{}),
178 else => unreachable,194 else => unreachable,
179 }195 }
180}196}
...@@ -255,7 +271,7 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {...@@ -255,7 +271,7 @@ fn mirCallExtern(emit: *Emit, inst: Mir.Inst.Index) !void {
255 .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),271 .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_BRANCH26),
256 });272 });
257 } else {273 } else {
258 @panic("Implement call_extern for linking backends != MachO");274 return emit.fail("Implement call_extern for linking backends != MachO", .{});
259 }275 }
260}276}
261277
...@@ -304,7 +320,7 @@ fn mirLoadMemory(emit: *Emit, inst: Mir.Inst.Index) !void {...@@ -304,7 +320,7 @@ fn mirLoadMemory(emit: *Emit, inst: Mir.Inst.Index) !void {
304 .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),320 .@"type" = @enumToInt(std.macho.reloc_type_arm64.ARM64_RELOC_GOT_LOAD_PAGEOFF12),
305 });321 });
306 } else {322 } 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", .{});
308 }324 }
309 } else {325 } else {
310 // The value is in memory at a hard-coded address.326 // The value is in memory at a hard-coded address.