aarch64 C ABI: return union instead of array of two enums

The result is much cleaner and the second element was unused most of the time.
This commit is contained in:
Veikka Tuominen 2022-10-21 19:20:48 +03:00
parent 9ae78a5890
commit 3981250b84
2 changed files with 40 additions and 48 deletions

View File

@ -5,42 +5,41 @@ const Register = bits.Register;
const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;
const Type = @import("../../type.zig").Type;
pub const Class = enum(u8) { memory, integer, none, float_array, _ };
pub const Class = union(enum) { memory, integer, double_integer, none, float_array: u8 };
/// For `float_array` the second element will be the amount of floats.
pub fn classifyType(ty: Type, target: std.Target) [2]Class {
if (!ty.hasRuntimeBitsIgnoreComptime()) return .{ .none, .none };
pub fn classifyType(ty: Type, target: std.Target) Class {
if (!ty.hasRuntimeBitsIgnoreComptime()) return .none;
var maybe_float_bits: ?u16 = null;
switch (ty.zigTypeTag()) {
.Struct => {
if (ty.containerLayout() == .Packed) return .{ .integer, .none };
if (ty.containerLayout() == .Packed) return .integer;
const float_count = countFloats(ty, target, &maybe_float_bits);
if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) };
if (float_count <= sret_float_count) return .{ .float_array = float_count };
const bit_size = ty.bitSize(target);
if (bit_size > 128) return .{ .memory, .none };
if (bit_size > 64) return .{ .integer, .integer };
return .{ .integer, .none };
if (bit_size > 128) return .memory;
if (bit_size > 64) return .double_integer;
return .integer;
},
.Union => {
if (ty.containerLayout() == .Packed) return .{ .integer, .none };
if (ty.containerLayout() == .Packed) return .integer;
const float_count = countFloats(ty, target, &maybe_float_bits);
if (float_count <= sret_float_count) return .{ .float_array, @intToEnum(Class, float_count) };
if (float_count <= sret_float_count) return .{ .float_array = float_count };
const bit_size = ty.bitSize(target);
if (bit_size > 128) return .{ .memory, .none };
if (bit_size > 64) return .{ .integer, .integer };
return .{ .integer, .none };
if (bit_size > 128) return .memory;
if (bit_size > 64) return .double_integer;
return .integer;
},
.Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .{ .integer, .none },
.Array => return .{ .memory, .none },
.Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .integer,
.Optional => {
std.debug.assert(ty.isPtrLikeOptional());
return .{ .integer, .none };
return .integer;
},
.Pointer => {
std.debug.assert(!ty.isSlice());
return .{ .integer, .none };
return .integer;
},
.ErrorUnion,
.Frame,
@ -56,17 +55,18 @@ pub fn classifyType(ty: Type, target: std.Target) [2]Class {
.Fn,
.Opaque,
.EnumLiteral,
.Array,
=> unreachable,
}
}
const sret_float_count = 4;
fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u32 {
const invalid = std.math.maxInt(u32);
fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u8 {
const invalid = std.math.maxInt(u8);
switch (ty.zigTypeTag()) {
.Union => {
const fields = ty.unionFields();
var max_count: u32 = 0;
var max_count: u8 = 0;
for (fields.values()) |field| {
const field_count = countFloats(field.ty, target, maybe_float_bits);
if (field_count == invalid) return invalid;
@ -77,7 +77,7 @@ fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u32 {
},
.Struct => {
const fields_len = ty.structFieldCount();
var count: u32 = 0;
var count: u8 = 0;
var i: u32 = 0;
while (i < fields_len) : (i += 1) {
const field_ty = ty.structFieldType(i);

View File

@ -10113,7 +10113,7 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool
else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory,
},
.wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,
.aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target)[0] == .memory,
.aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target) == .memory,
.arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) {
.memory, .i64_array => return true,
.i32_array => |size| return size != 1,
@ -10232,19 +10232,15 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
if (is_scalar) {
return dg.lowerType(fn_info.return_type);
}
const classes = aarch64_c_abi.classifyType(fn_info.return_type, target);
if (classes[0] == .memory or classes[0] == .none) {
return dg.context.voidType();
switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) {
.memory, .none => return dg.context.voidType(),
.float_array => return dg.lowerType(fn_info.return_type),
.integer => {
const bit_size = fn_info.return_type.bitSize(target);
return dg.context.intType(@intCast(c_uint, bit_size));
},
.double_integer => return dg.context.intType(64).arrayType(2),
}
if (classes[0] == .float_array) {
return dg.lowerType(fn_info.return_type);
}
if (classes[1] == .none) {
const bit_size = fn_info.return_type.bitSize(target);
return dg.context.intType(@intCast(c_uint, bit_size));
}
return dg.context.intType(64).arrayType(2);
},
.arm, .armeb => {
switch (arm_c_abi.classifyType(fn_info.return_type, target)) {
@ -10459,21 +10455,17 @@ const ParamTypeIterator = struct {
if (is_scalar) {
return .byval;
}
const classes = aarch64_c_abi.classifyType(ty, it.target);
if (classes[0] == .memory) {
return .byref;
switch (aarch64_c_abi.classifyType(ty, it.target)) {
.none => unreachable,
.memory => return .byref,
.float_array => |len| return Lowering{ .float_array = len },
.integer => {
it.llvm_types_len = 1;
it.llvm_types_buffer[0] = 64;
return .multiple_llvm_ints;
},
.double_integer => return Lowering{ .i64_array = 2 },
}
if (classes[0] == .float_array) {
return Lowering{ .float_array = @enumToInt(classes[1]) };
}
if (classes[1] == .none) {
it.llvm_types_len = 1;
} else {
it.llvm_types_len = 2;
}
it.llvm_types_buffer[0] = 64;
it.llvm_types_buffer[1] = 64;
return .multiple_llvm_ints;
},
.arm, .armeb => {
it.zig_index += 1;