mirror of
https://github.com/ziglang/zig.git
synced 2026-01-09 08:55:36 +00:00
stage2 llvm: implement aarch64 C ABI
... at least enough to pass all the current tests.
This commit is contained in:
parent
ae3a5ff7f9
commit
9ce841a0f0
@ -3,6 +3,69 @@ const builtin = @import("builtin");
|
||||
const bits = @import("bits.zig");
|
||||
const Register = bits.Register;
|
||||
const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;
|
||||
const Type = @import("../../type.zig").Type;
|
||||
|
||||
pub const Class = enum { memory, integer, none, float_array };
|
||||
|
||||
pub fn classifyType(ty: Type, target: std.Target) [2]Class {
|
||||
if (!ty.hasRuntimeBitsIgnoreComptime()) return .{ .none, .none };
|
||||
switch (ty.zigTypeTag()) {
|
||||
.Struct => {
|
||||
if (ty.containerLayout() == .Packed) return .{ .integer, .none };
|
||||
|
||||
if (ty.structFieldCount() <= 4) {
|
||||
const fields = ty.structFields();
|
||||
var float_size: ?u64 = null;
|
||||
for (fields.values()) |field| {
|
||||
if (field.ty.zigTypeTag() != .Float) break;
|
||||
const field_size = field.ty.bitSize(target);
|
||||
const prev_size = float_size orelse {
|
||||
float_size = field_size;
|
||||
continue;
|
||||
};
|
||||
if (field_size != prev_size) break;
|
||||
} else {
|
||||
return .{ .float_array, .none };
|
||||
}
|
||||
}
|
||||
const bit_size = ty.bitSize(target);
|
||||
if (bit_size > 128) return .{ .memory, .none };
|
||||
if (bit_size > 64) return .{ .integer, .integer };
|
||||
return .{ .integer, .none };
|
||||
},
|
||||
.Union => {
|
||||
const bit_size = ty.bitSize(target);
|
||||
if (bit_size > 128) return .{ .memory, .none };
|
||||
if (bit_size > 64) return .{ .integer, .integer };
|
||||
return .{ .integer, .none };
|
||||
},
|
||||
.Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .{ .integer, .none },
|
||||
.Array => return .{ .memory, .none },
|
||||
.Optional => {
|
||||
std.debug.assert(ty.isPtrLikeOptional());
|
||||
return .{ .integer, .none };
|
||||
},
|
||||
.Pointer => {
|
||||
std.debug.assert(!ty.isSlice());
|
||||
return .{ .integer, .none };
|
||||
},
|
||||
.ErrorUnion,
|
||||
.Frame,
|
||||
.AnyFrame,
|
||||
.NoReturn,
|
||||
.Void,
|
||||
.Type,
|
||||
.ComptimeFloat,
|
||||
.ComptimeInt,
|
||||
.Undefined,
|
||||
.Null,
|
||||
.BoundFn,
|
||||
.Fn,
|
||||
.Opaque,
|
||||
.EnumLiteral,
|
||||
=> unreachable,
|
||||
}
|
||||
}
|
||||
|
||||
const callee_preserved_regs_impl = if (builtin.os.tag.isDarwin()) struct {
|
||||
pub const callee_preserved_regs = [_]Register{
|
||||
|
||||
@ -23,6 +23,7 @@ const LazySrcLoc = Module.LazySrcLoc;
|
||||
const CType = @import("../type.zig").CType;
|
||||
const x86_64_abi = @import("../arch/x86_64/abi.zig");
|
||||
const wasm_c_abi = @import("../arch/wasm/abi.zig");
|
||||
const aarch64_c_abi = @import("../arch/aarch64/abi.zig");
|
||||
|
||||
const Error = error{ OutOfMemory, CodegenFail };
|
||||
|
||||
@ -1086,6 +1087,26 @@ pub const Object = struct {
|
||||
try args.ensureUnusedCapacity(1);
|
||||
args.appendAssumeCapacity(casted);
|
||||
},
|
||||
.float_array => {
|
||||
const param_ty = fn_info.param_types[it.zig_index - 1];
|
||||
const param_llvm_ty = try dg.lowerType(param_ty);
|
||||
const param = llvm_func.getParam(llvm_arg_i);
|
||||
llvm_arg_i += 1;
|
||||
|
||||
const alignment = param_ty.abiAlignment(target);
|
||||
const arg_ptr = buildAllocaInner(builder, llvm_func, false, param_llvm_ty);
|
||||
arg_ptr.setAlignment(alignment);
|
||||
const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), "");
|
||||
_ = builder.buildStore(param, casted_ptr);
|
||||
|
||||
if (isByRef(param_ty)) {
|
||||
try args.append(arg_ptr);
|
||||
} else {
|
||||
const load_inst = builder.buildLoad(arg_ptr, "");
|
||||
load_inst.setAlignment(alignment);
|
||||
try args.append(load_inst);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@ -3070,6 +3091,13 @@ pub const DeclGen = struct {
|
||||
.as_u16 => {
|
||||
try llvm_params.append(dg.context.intType(16));
|
||||
},
|
||||
.float_array => {
|
||||
const param_ty = fn_info.param_types[it.zig_index - 1];
|
||||
const float_ty = try dg.lowerType(param_ty.structFieldType(0));
|
||||
const field_count = @intCast(c_uint, param_ty.structFieldCount());
|
||||
const arr_ty = float_ty.arrayType(field_count);
|
||||
try llvm_params.append(arr_ty);
|
||||
},
|
||||
};
|
||||
|
||||
return llvm.functionType(
|
||||
@ -4621,6 +4649,27 @@ pub const FuncGen = struct {
|
||||
const casted = self.builder.buildBitCast(llvm_arg, self.dg.context.intType(16), "");
|
||||
try llvm_args.append(casted);
|
||||
},
|
||||
.float_array => {
|
||||
const arg = args[it.zig_index - 1];
|
||||
const arg_ty = self.air.typeOf(arg);
|
||||
var llvm_arg = try self.resolveInst(arg);
|
||||
if (!isByRef(arg_ty)) {
|
||||
const p = self.buildAlloca(llvm_arg.typeOf());
|
||||
const store_inst = self.builder.buildStore(llvm_arg, p);
|
||||
store_inst.setAlignment(arg_ty.abiAlignment(target));
|
||||
llvm_arg = store_inst;
|
||||
}
|
||||
|
||||
const float_ty = try self.dg.lowerType(arg_ty.structFieldType(0));
|
||||
const field_count = @intCast(u32, arg_ty.structFieldCount());
|
||||
const arr_ty = float_ty.arrayType(field_count);
|
||||
|
||||
const casted = self.builder.buildBitCast(llvm_arg, arr_ty.pointerType(0), "");
|
||||
const alignment = arg_ty.abiAlignment(target);
|
||||
const load_inst = self.builder.buildLoad(casted, "");
|
||||
load_inst.setAlignment(alignment);
|
||||
try llvm_args.append(load_inst);
|
||||
},
|
||||
};
|
||||
|
||||
const call = self.builder.buildCall(
|
||||
@ -9644,6 +9693,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,
|
||||
else => return false, // TODO investigate C ABI for other architectures
|
||||
},
|
||||
else => return false,
|
||||
@ -9753,6 +9803,24 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*const llvm.
|
||||
const abi_size = scalar_type.abiSize(target);
|
||||
return dg.context.intType(@intCast(c_uint, abi_size * 8));
|
||||
},
|
||||
.aarch64, .aarch64_be => {
|
||||
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();
|
||||
}
|
||||
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);
|
||||
},
|
||||
// TODO investigate C ABI for other architectures
|
||||
else => return dg.lowerType(fn_info.return_type),
|
||||
}
|
||||
@ -9780,6 +9848,7 @@ const ParamTypeIterator = struct {
|
||||
multiple_llvm_float,
|
||||
slice,
|
||||
as_u16,
|
||||
float_array,
|
||||
};
|
||||
|
||||
pub fn next(it: *ParamTypeIterator) ?Lowering {
|
||||
@ -9945,6 +10014,28 @@ const ParamTypeIterator = struct {
|
||||
}
|
||||
return .abi_sized_int;
|
||||
},
|
||||
.aarch64, .aarch64_be => {
|
||||
it.zig_index += 1;
|
||||
it.llvm_index += 1;
|
||||
if (is_scalar) {
|
||||
return .byval;
|
||||
}
|
||||
const classes = aarch64_c_abi.classifyType(ty, it.target);
|
||||
if (classes[0] == .memory) {
|
||||
return .byref;
|
||||
}
|
||||
if (classes[0] == .float_array) {
|
||||
return .float_array;
|
||||
}
|
||||
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;
|
||||
},
|
||||
// TODO investigate C ABI for other architectures
|
||||
else => {
|
||||
it.zig_index += 1;
|
||||
|
||||
@ -110,7 +110,7 @@ test "C ABI floats" {
|
||||
}
|
||||
|
||||
test "C ABI long double" {
|
||||
if (!builtin.cpu.arch.isWasm()) return error.SkipZigTest;
|
||||
if (!builtin.cpu.arch.isWasm() and !builtin.cpu.arch.isAARCH64()) return error.SkipZigTest;
|
||||
c_long_double(12.34);
|
||||
}
|
||||
|
||||
|
||||
@ -50,6 +50,9 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
|
||||
cases.addBuildFile("test/c_abi/build.zig", .{});
|
||||
}
|
||||
}
|
||||
if (builtin.cpu.arch.isAARCH64() and builtin.zig_backend == .stage2_llvm) {
|
||||
cases.addBuildFile("test/c_abi/build.zig", .{});
|
||||
}
|
||||
// C ABI tests only pass for the Wasm target when using stage2
|
||||
cases.addBuildFile("test/c_abi/build_wasm.zig", .{
|
||||
.requires_stage2 = true,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user