authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-05-24 19:23:33+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-24 15:34:52-07:00
log8c49420928b29271429cc09b5d5f1447a942f8d6
treea6dfcb22fb8d86ba4159ed12491a67cc065e9b61
parentc043d57cabdc4db20a55a9877ec607c81d15442f

aarch64: update for new error union layout


4 files changed, 93 insertions(+), 60 deletions(-)

src/arch/aarch64/CodeGen.zig+69-41
......@@ -3,6 +3,7 @@ const builtin = @import("builtin");
33const mem = std.mem;
44const math = std.math;
55const assert = std.debug.assert;
6const codegen = @import("../../codegen.zig");
67const Air = @import("../../Air.zig");
78const Mir = @import("Mir.zig");
89const Emit = @import("Emit.zig");
......@@ -22,12 +23,14 @@ const leb128 = std.leb;
2223const log = std.log.scoped(.codegen);
2324const build_options = @import("build_options");
2425
25const GenerateSymbolError = @import("../../codegen.zig").GenerateSymbolError;
26const FnResult = @import("../../codegen.zig").FnResult;
27const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
26const GenerateSymbolError = codegen.GenerateSymbolError;
27const FnResult = codegen.FnResult;
28const DebugInfoOutput = codegen.DebugInfoOutput;
2829
2930const bits = @import("bits.zig");
3031const abi = @import("abi.zig");
32const errUnionPayloadOffset = codegen.errUnionPayloadOffset;
33const errUnionErrOffset = codegen.errUnionErrOffset;
3134const RegisterManager = abi.RegisterManager;
3235const RegisterLock = RegisterManager.RegisterLock;
3336const Register = bits.Register;
......@@ -3272,7 +3275,14 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.
32723275
32733276fn ret(self: *Self, mcv: MCValue) !void {
32743277 const ret_ty = self.fn_type.fnReturnType();
3275 try self.setRegOrMem(ret_ty, self.ret_mcv, mcv);
3278 switch (self.ret_mcv) {
3279 .immediate => {
3280 assert(ret_ty.isError());
3281 },
3282 else => {
3283 try self.setRegOrMem(ret_ty, self.ret_mcv, mcv);
3284 },
3285 }
32763286 // Just add space for an instruction, patch this later
32773287 const index = try self.addInst(.{
32783288 .tag = .nop,
......@@ -3601,30 +3611,39 @@ fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
36013611 const error_type = ty.errorUnionSet();
36023612 const payload_type = ty.errorUnionPayload();
36033613
3604 if (!error_type.hasRuntimeBits()) {
3614 if (error_type.errorSetCardinality() == .zero) {
36053615 return MCValue{ .immediate = 0 }; // always false
3606 } else if (!payload_type.hasRuntimeBits()) {
3607 if (error_type.abiSize(self.target.*) <= 8) {
3608 const reg_mcv: MCValue = switch (operand) {
3609 .register => operand,
3610 else => .{ .register = try self.copyToTmpRegister(error_type, operand) },
3611 };
3616 }
36123617
3618 const err_off = errUnionErrOffset(ty, self.target.*);
3619 switch (operand) {
3620 .stack_offset => |off| {
3621 const offset = off - @intCast(u32, err_off);
3622 const tmp_reg = try self.copyToTmpRegister(Type.anyerror, .{ .stack_offset = offset });
36133623 _ = try self.addInst(.{
36143624 .tag = .cmp_immediate,
36153625 .data = .{ .r_imm12_sh = .{
3616 .rn = reg_mcv.register,
3626 .rn = tmp_reg,
36173627 .imm12 = 0,
36183628 } },
36193629 });
3620
3621 return MCValue{ .compare_flags_unsigned = .gt };
3622 } else {
3623 return self.fail("TODO isErr for errors with size > 8", .{});
3624 }
3625 } else {
3626 return self.fail("TODO isErr for non-empty payloads", .{});
3630 },
3631 .register => |reg| {
3632 if (err_off > 0 or payload_type.hasRuntimeBitsIgnoreComptime()) {
3633 return self.fail("TODO implement isErr for register operand with payload bits", .{});
3634 }
3635 _ = try self.addInst(.{
3636 .tag = .cmp_immediate,
3637 .data = .{ .r_imm12_sh = .{
3638 .rn = reg,
3639 .imm12 = 0,
3640 } },
3641 });
3642 },
3643 else => return self.fail("TODO implement isErr for {}", .{operand}),
36273644 }
3645
3646 return MCValue{ .compare_flags_unsigned = .gt };
36283647}
36293648
36303649fn isNonErr(self: *Self, ty: Type, operand: MCValue) !MCValue {
......@@ -4483,7 +4502,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
44834502 const ref_int = @enumToInt(inst);
44844503 if (ref_int < Air.Inst.Ref.typed_value_map.len) {
44854504 const tv = Air.Inst.Ref.typed_value_map[ref_int];
4486 if (!tv.ty.hasRuntimeBits()) {
4505 if (!tv.ty.hasRuntimeBitsIgnoreComptime() and !tv.ty.isError()) {
44874506 return MCValue{ .none = {} };
44884507 }
44894508 return self.genTypedValue(tv);
......@@ -4491,7 +4510,7 @@ fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue {
44914510
44924511 // If the type has no codegen bits, no need to store it.
44934512 const inst_ty = self.air.typeOf(inst);
4494 if (!inst_ty.hasRuntimeBits())
4513 if (!inst_ty.hasRuntimeBitsIgnoreComptime() and !inst_ty.isError())
44954514 return MCValue{ .none = {} };
44964515
44974516 const inst_index = @intCast(Air.Inst.Index, ref_int - Air.Inst.Ref.typed_value_map.len);
......@@ -4674,32 +4693,38 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue {
46744693 }
46754694 },
46764695 .ErrorSet => {
4677 const err_name = typed_value.val.castTag(.@"error").?.data.name;
4678 const module = self.bin_file.options.module.?;
4679 const global_error_set = module.global_error_set;
4680 const error_index = global_error_set.get(err_name).?;
4681 return MCValue{ .immediate = error_index };
4696 switch (typed_value.val.tag()) {
4697 .@"error" => {
4698 const err_name = typed_value.val.castTag(.@"error").?.data.name;
4699 const module = self.bin_file.options.module.?;
4700 const global_error_set = module.global_error_set;
4701 const error_index = global_error_set.get(err_name).?;
4702 return MCValue{ .immediate = error_index };
4703 },
4704 else => {
4705 // In this case we are rendering an error union which has a 0 bits payload.
4706 return MCValue{ .immediate = 0 };
4707 },
4708 }
46824709 },
46834710 .ErrorUnion => {
46844711 const error_type = typed_value.ty.errorUnionSet();
46854712 const payload_type = typed_value.ty.errorUnionPayload();
46864713
4687 if (typed_value.val.castTag(.eu_payload)) |pl| {
4688 if (!payload_type.hasRuntimeBits()) {
4689 // We use the error type directly as the type.
4690 return MCValue{ .immediate = 0 };
4691 }
4714 if (error_type.errorSetCardinality() == .zero) {
4715 const payload_val = typed_value.val.castTag(.eu_payload).?.data;
4716 return self.genTypedValue(.{ .ty = payload_type, .val = payload_val });
4717 }
46924718
4693 _ = pl;
4694 return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty.fmtDebug()});
4695 } else {
4696 if (!payload_type.hasRuntimeBits()) {
4697 // We use the error type directly as the type.
4698 return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val });
4699 }
4719 const is_pl = typed_value.val.errorUnionIsPayload();
47004720
4701 return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty.fmtDebug()});
4721 if (!payload_type.hasRuntimeBitsIgnoreComptime()) {
4722 // We use the error type directly as the type.
4723 const err_val = if (!is_pl) typed_value.val else Value.initTag(.zero);
4724 return self.genTypedValue(.{ .ty = error_type, .val = err_val });
47024725 }
4726
4727 return self.lowerUnnamedConst(typed_value);
47034728 },
47044729 .Struct => {
47054730 return self.lowerUnnamedConst(typed_value);
......@@ -4796,13 +4821,16 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues {
47964821
47974822 if (ret_ty.zigTypeTag() == .NoReturn) {
47984823 result.return_value = .{ .unreach = {} };
4799 } else if (!ret_ty.hasRuntimeBits()) {
4824 } else if (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError()) {
48004825 result.return_value = .{ .none = {} };
48014826 } else switch (cc) {
48024827 .Naked => unreachable,
48034828 .Unspecified, .C => {
48044829 const ret_ty_size = @intCast(u32, ret_ty.abiSize(self.target.*));
4805 if (ret_ty_size <= 8) {
4830 if (ret_ty_size == 0) {
4831 assert(ret_ty.isError());
4832 result.return_value = .{ .immediate = 0 };
4833 } else if (ret_ty_size <= 8) {
48064834 result.return_value = .{ .register = registerAlias(c_abi_int_return_regs[0], ret_ty_size) };
48074835 } else {
48084836 return self.fail("TODO support more return types for ARM backend", .{});
src/arch/x86_64/CodeGen.zig+6-19
......@@ -2,6 +2,7 @@ const std = @import("std");
22const build_options = @import("build_options");
33const builtin = @import("builtin");
44const assert = std.debug.assert;
5const codegen = @import("../../codegen.zig");
56const leb128 = std.leb;
67const link = @import("../../link.zig");
78const log = std.log.scoped(.codegen);
......@@ -12,11 +13,11 @@ const trace = @import("../../tracy.zig").trace;
1213const Air = @import("../../Air.zig");
1314const Allocator = mem.Allocator;
1415const Compilation = @import("../../Compilation.zig");
15const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput;
16const DebugInfoOutput = codegen.DebugInfoOutput;
1617const DW = std.dwarf;
1718const ErrorMsg = Module.ErrorMsg;
18const FnResult = @import("../../codegen.zig").FnResult;
19const GenerateSymbolError = @import("../../codegen.zig").GenerateSymbolError;
19const FnResult = codegen.FnResult;
20const GenerateSymbolError = codegen.GenerateSymbolError;
2021const Emit = @import("Emit.zig");
2122const Liveness = @import("../../Liveness.zig");
2223const Mir = @import("Mir.zig");
......@@ -28,6 +29,8 @@ const Value = @import("../../value.zig").Value;
2829
2930const bits = @import("bits.zig");
3031const abi = @import("abi.zig");
32const errUnionPayloadOffset = codegen.errUnionPayloadOffset;
33const errUnionErrOffset = codegen.errUnionErrOffset;
3134
3235const callee_preserved_regs = abi.callee_preserved_regs;
3336const caller_preserved_regs = abi.caller_preserved_regs;
......@@ -7183,19 +7186,3 @@ fn intrinsicsAllowed(target: Target, ty: Type) bool {
71837186fn hasAvxSupport(target: Target) bool {
71847187 return Target.x86.featureSetHasAny(target.cpu.features, .{ .avx, .avx2 });
71857188}
7186
7187fn errUnionPayloadOffset(ty: Type, target: std.Target) u64 {
7188 const payload_ty = ty.errorUnionPayload();
7189 return if (Type.anyerror.abiAlignment(target) >= payload_ty.abiAlignment(target))
7190 Type.anyerror.abiSize(target)
7191 else
7192 0;
7193}
7194
7195fn errUnionErrOffset(ty: Type, target: std.Target) u64 {
7196 const payload_ty = ty.errorUnionPayload();
7197 return if (Type.anyerror.abiAlignment(target) >= payload_ty.abiAlignment(target))
7198 0
7199 else
7200 payload_ty.abiSize(target);
7201}
src/codegen.zig+16
......@@ -890,3 +890,19 @@ fn lowerDeclRef(
890890
891891 return Result{ .appended = {} };
892892}
893
894pub fn errUnionPayloadOffset(ty: Type, target: std.Target) u64 {
895 const payload_ty = ty.errorUnionPayload();
896 return if (Type.anyerror.abiAlignment(target) >= payload_ty.abiAlignment(target))
897 Type.anyerror.abiSize(target)
898 else
899 0;
900}
901
902pub fn errUnionErrOffset(ty: Type, target: std.Target) u64 {
903 const payload_ty = ty.errorUnionPayload();
904 return if (Type.anyerror.abiAlignment(target) >= payload_ty.abiAlignment(target))
905 0
906 else
907 payload_ty.abiSize(target);
908}
test/behavior/error.zig+2
......@@ -440,6 +440,8 @@ test "return function call to error set from error union function" {
440440}
441441
442442test "optional error set is the same size as error set" {
443 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
444
443445 comptime try expect(@sizeOf(?anyerror) == @sizeOf(anyerror));
444446 comptime try expect(@alignOf(?anyerror) == @alignOf(anyerror));
445447 const S = struct {