diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-10-23 12:16:58 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-10-23 12:16:58 -0700 |
| commit | 04472af328632d80ea9ea089cfd5de8fac6dd039 (patch) | |
| tree | 7b8d46635c0a0accc82277a6209a87e4d4b9d13b /src/codegen/llvm.zig | |
| parent | a3033c7bd9ec26abd2f831cf75eb85f06590deed (diff) | |
| parent | f2a7aba586157482131611c57a283f3b4cfee98d (diff) | |
| download | zig-04472af328632d80ea9ea089cfd5de8fac6dd039.tar.gz zig-04472af328632d80ea9ea089cfd5de8fac6dd039.zip | |
Merge pull request #13251 from Vexu/c-abi
implement ARM C ABI, separate C ABI tests from standalone tests
Diffstat (limited to 'src/codegen/llvm.zig')
| -rw-r--r-- | src/codegen/llvm.zig | 194 |
1 files changed, 143 insertions, 51 deletions
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 3ebca13c20..ffc19cb6f6 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -24,6 +24,8 @@ 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 arm_c_abi = @import("../arch/arm/abi.zig"); +const riscv_c_abi = @import("../arch/riscv64/abi.zig"); const Error = error{ OutOfMemory, CodegenFail }; @@ -1138,6 +1140,25 @@ pub const Object = struct { try args.append(load_inst); } }, + .i32_array, .i64_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, alignment, target); + 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(param_llvm_ty, arg_ptr, ""); + load_inst.setAlignment(alignment); + try args.append(load_inst); + } + }, }; } @@ -2578,6 +2599,8 @@ pub const DeclGen = struct { .multiple_llvm_float, .as_u16, .float_array, + .i32_array, + .i64_array, => continue, .slice => unreachable, // extern functions do not support slice types. @@ -3132,6 +3155,11 @@ pub const DeclGen = struct { const arr_ty = float_ty.arrayType(field_count); try llvm_params.append(arr_ty); }, + .i32_array, .i64_array => |arr_len| { + const elem_size: u8 = if (lowering == .i32_array) 32 else 64; + const arr_ty = dg.context.intType(elem_size).arrayType(arr_len); + try llvm_params.append(arr_ty); + }, }; return llvm.functionType( @@ -4822,6 +4850,25 @@ pub const FuncGen = struct { load_inst.setAlignment(alignment); try llvm_args.append(load_inst); }, + .i32_array, .i64_array => |arr_len| { + const elem_size: u8 = if (lowering == .i32_array) 32 else 64; + 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(), null); + const store_inst = self.builder.buildStore(llvm_arg, p); + store_inst.setAlignment(arg_ty.abiAlignment(target)); + llvm_arg = store_inst; + } + + const array_llvm_ty = self.dg.context.intType(elem_size).arrayType(arr_len); + const casted = self.builder.buildBitCast(llvm_arg, array_llvm_ty.pointerType(0), ""); + const alignment = arg_ty.abiAlignment(target); + const load_inst = self.builder.buildLoad(array_llvm_ty, casted, ""); + load_inst.setAlignment(alignment); + try llvm_args.append(load_inst); + }, }; const call = self.builder.buildCall( @@ -10083,10 +10130,16 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool .mips, .mipsel => return false, .x86_64 => switch (target.os.tag) { .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory, - else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory, + else => return x86_64_abi.classifySystemV(fn_info.return_type, target, .ret)[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, .ret)) { + .memory, .i64_array => return true, + .i32_array => |size| return size != 1, + .byval => return false, + }, + .riscv32, .riscv64 => return riscv_c_abi.classifyType(fn_info.return_type, target) == .memory, else => return false, // TODO investigate C ABI for other architectures }, else => return false, @@ -10139,7 +10192,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { if (is_scalar) { return dg.lowerType(fn_info.return_type); } - const classes = x86_64_abi.classifySystemV(fn_info.return_type, target); + const classes = x86_64_abi.classifySystemV(fn_info.return_type, target, .ret); if (classes[0] == .memory) { return dg.context.voidType(); } @@ -10197,22 +10250,44 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { 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(); + switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) { + .memory => return dg.context.voidType(), + .float_array => return dg.lowerType(fn_info.return_type), + .byval => 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); + }, + .arm, .armeb => { + switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) { + .memory, .i64_array => return dg.context.voidType(), + .i32_array => |len| if (len == 1) { + return dg.context.intType(32); + } else { + return dg.context.voidType(); + }, + .byval => 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)); + }, + .riscv32, .riscv64 => { + switch (riscv_c_abi.classifyType(fn_info.return_type, target)) { + .memory => return dg.context.voidType(), + .integer => { + const bit_size = fn_info.return_type.bitSize(target); + return dg.context.intType(@intCast(c_uint, bit_size)); + }, + .double_integer => { + var llvm_types_buffer: [2]*llvm.Type = .{ + dg.context.intType(64), + dg.context.intType(64), + }; + return dg.context.structType(&llvm_types_buffer, 2, .False); + }, + .byval => return dg.lowerType(fn_info.return_type), } - - return dg.context.intType(64).arrayType(2); }, // TODO investigate C ABI for other architectures else => return dg.lowerType(fn_info.return_type), @@ -10242,6 +10317,8 @@ const ParamTypeIterator = struct { slice, as_u16, float_array: u8, + i32_array: u8, + i64_array: u8, }; pub fn next(it: *ParamTypeIterator) ?Lowering { @@ -10288,15 +10365,6 @@ const ParamTypeIterator = struct { .C => { const is_scalar = isScalar(ty); switch (it.target.cpu.arch) { - .riscv32, .riscv64 => { - it.zig_index += 1; - it.llvm_index += 1; - if (ty.tag() == .f16) { - return .as_u16; - } else { - return .byval; - } - }, .mips, .mipsel => { it.zig_index += 1; it.llvm_index += 1; @@ -10334,18 +10402,18 @@ const ParamTypeIterator = struct { else => unreachable, }, else => { - if (is_scalar) { - it.zig_index += 1; - it.llvm_index += 1; - return .byval; - } - const classes = x86_64_abi.classifySystemV(ty, it.target); + const classes = x86_64_abi.classifySystemV(ty, it.target, .arg); if (classes[0] == .memory) { it.zig_index += 1; it.llvm_index += 1; it.byval_attr = true; return .byref; } + if (is_scalar) { + it.zig_index += 1; + it.llvm_index += 1; + return .byval; + } var llvm_types_buffer: [8]u16 = undefined; var llvm_types_index: u32 = 0; for (classes) |class| { @@ -10383,11 +10451,6 @@ const ParamTypeIterator = struct { it.llvm_index += 1; return .abi_sized_int; } - if (classes[0] == .sse and classes[1] == .none) { - it.zig_index += 1; - it.llvm_index += 1; - return .byval; - } it.llvm_types_buffer = llvm_types_buffer; it.llvm_types_len = llvm_types_index; it.llvm_index += llvm_types_index; @@ -10410,24 +10473,45 @@ const ParamTypeIterator = struct { .aarch64, .aarch64_be => { it.zig_index += 1; it.llvm_index += 1; - if (is_scalar) { - return .byval; + switch (aarch64_c_abi.classifyType(ty, it.target)) { + .memory => return .byref, + .float_array => |len| return Lowering{ .float_array = len }, + .byval => return .byval, + .integer => { + it.llvm_types_len = 1; + it.llvm_types_buffer[0] = 64; + return .multiple_llvm_ints; + }, + .double_integer => return Lowering{ .i64_array = 2 }, } - const classes = aarch64_c_abi.classifyType(ty, it.target); - if (classes[0] == .memory) { - return .byref; + }, + .arm, .armeb => { + it.zig_index += 1; + it.llvm_index += 1; + switch (arm_c_abi.classifyType(ty, it.target, .arg)) { + .memory => { + it.byval_attr = true; + return .byref; + }, + .byval => return .byval, + .i32_array => |size| return Lowering{ .i32_array = size }, + .i64_array => |size| return Lowering{ .i64_array = size }, } - if (classes[0] == .float_array) { - return Lowering{ .float_array = @enumToInt(classes[1]) }; + }, + .riscv32, .riscv64 => { + it.zig_index += 1; + it.llvm_index += 1; + if (ty.tag() == .f16) { + return .as_u16; } - if (classes[1] == .none) { - it.llvm_types_len = 1; - } else { - it.llvm_types_len = 2; + switch (riscv_c_abi.classifyType(ty, it.target)) { + .memory => { + return .byref; + }, + .byval => return .byval, + .integer => return .abi_sized_int, + .double_integer => return Lowering{ .i64_array = 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 => { @@ -10475,8 +10559,16 @@ fn ccAbiPromoteInt( }; if (int_info.bits <= 16) return int_info.signedness; switch (target.cpu.arch) { + .riscv64 => { + if (int_info.bits == 32) { + // LLVM always signextends 32 bit ints, unsure if bug. + return .signed; + } + if (int_info.bits < 64) { + return int_info.signedness; + } + }, .sparc64, - .riscv64, .powerpc64, .powerpc64le, => { |
