authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-21 17:53:29+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-21 18:07:11+03:00
log9ae78a5890602b89113e53884039ba537c7ef6f9
tree3b4caa1e90e64e4594a87b98eb301dbaa375adeb
parent76220781279a2dc42572b7819a834aebb85eb354

stage2: implement ARM C ABI

Six new passing tests and the previously incorrectly passing complex tests are now skipped.

4 files changed, 228 insertions(+), 7 deletions(-)

src/arch/arm/abi.zig+143
......@@ -2,6 +2,149 @@ const std = @import("std");
22const bits = @import("bits.zig");
33const Register = bits.Register;
44const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;
5const Type = @import("../../type.zig").Type;
6
7pub const Class = union(enum) {
8 memory,
9 byval,
10 none,
11 i32_array: u8,
12 i64_array: u8,
13
14 fn arrSize(total_size: u64, arr_size: u64) Class {
15 const count = @intCast(u8, std.mem.alignForward(total_size, arr_size) / arr_size);
16 if (arr_size == 32) {
17 return .{ .i32_array = count };
18 } else {
19 return .{ .i64_array = count };
20 }
21 }
22};
23
24pub fn classifyType(ty: Type, target: std.Target) Class {
25 if (!ty.hasRuntimeBitsIgnoreComptime()) return .none;
26
27 var maybe_float_bits: ?u16 = null;
28 const max_byval_size = 512;
29 switch (ty.zigTypeTag()) {
30 .Struct => {
31 const bit_size = ty.bitSize(target);
32 if (ty.containerLayout() == .Packed) {
33 if (bit_size > 64) return .memory;
34 return .byval;
35 }
36 if (bit_size > max_byval_size) return .memory;
37 const float_count = countFloats(ty, target, &maybe_float_bits);
38 if (float_count <= byval_float_count) return .byval;
39
40 const fields = ty.structFieldCount();
41 var i: u32 = 0;
42 while (i < fields) : (i += 1) {
43 const field_ty = ty.structFieldType(i);
44 const field_alignment = ty.structFieldAlign(i, target);
45 const field_size = field_ty.bitSize(target);
46 if (field_size > 32 or field_alignment > 32) {
47 return Class.arrSize(bit_size, 64);
48 }
49 }
50 return Class.arrSize(bit_size, 32);
51 },
52 .Union => {
53 const bit_size = ty.bitSize(target);
54 if (ty.containerLayout() == .Packed) {
55 if (bit_size > 64) return .memory;
56 return .byval;
57 }
58 if (bit_size > max_byval_size) return .memory;
59 const float_count = countFloats(ty, target, &maybe_float_bits);
60 if (float_count <= byval_float_count) return .byval;
61
62 for (ty.unionFields().values()) |field| {
63 if (field.ty.bitSize(target) > 32 or field.normalAlignment(target) > 32) {
64 return Class.arrSize(bit_size, 64);
65 }
66 }
67 return Class.arrSize(bit_size, 32);
68 },
69 .Int, .Enum => {
70 const bit_size = ty.bitSize(target);
71 if (bit_size > 64) return .memory;
72 return .byval;
73 },
74 .ErrorSet, .Vector, .Float, .Bool => {
75 const bit_size = ty.bitSize(target);
76 if (bit_size > 128) return .memory;
77 return .byval;
78 },
79 .Optional => {
80 std.debug.assert(ty.isPtrLikeOptional());
81 return .byval;
82 },
83 .Pointer => {
84 std.debug.assert(!ty.isSlice());
85 return .byval;
86 },
87 .ErrorUnion,
88 .Frame,
89 .AnyFrame,
90 .NoReturn,
91 .Void,
92 .Type,
93 .ComptimeFloat,
94 .ComptimeInt,
95 .Undefined,
96 .Null,
97 .BoundFn,
98 .Fn,
99 .Opaque,
100 .EnumLiteral,
101 .Array,
102 => unreachable,
103 }
104}
105
106const byval_float_count = 4;
107fn countFloats(ty: Type, target: std.Target, maybe_float_bits: *?u16) u32 {
108 const invalid = std.math.maxInt(u32);
109 switch (ty.zigTypeTag()) {
110 .Union => {
111 const fields = ty.unionFields();
112 var max_count: u32 = 0;
113 for (fields.values()) |field| {
114 const field_count = countFloats(field.ty, target, maybe_float_bits);
115 if (field_count == invalid) return invalid;
116 if (field_count > max_count) max_count = field_count;
117 if (max_count > byval_float_count) return invalid;
118 }
119 return max_count;
120 },
121 .Struct => {
122 const fields_len = ty.structFieldCount();
123 var count: u32 = 0;
124 var i: u32 = 0;
125 while (i < fields_len) : (i += 1) {
126 const field_ty = ty.structFieldType(i);
127 const field_count = countFloats(field_ty, target, maybe_float_bits);
128 if (field_count == invalid) return invalid;
129 count += field_count;
130 if (count > byval_float_count) return invalid;
131 }
132 return count;
133 },
134 .Float => {
135 const float_bits = maybe_float_bits.* orelse {
136 const float_bits = ty.floatBits(target);
137 if (float_bits != 32 and float_bits != 64) return invalid;
138 maybe_float_bits.* = float_bits;
139 return 1;
140 };
141 if (ty.floatBits(target) == float_bits) return 1;
142 return invalid;
143 },
144 .Void => return 0,
145 else => return invalid,
146 }
147}
5148
6149pub const callee_preserved_regs = [_]Register{ .r4, .r5, .r6, .r7, .r8, .r10 };
7150pub const caller_preserved_regs = [_]Register{ .r0, .r1, .r2, .r3 };
src/codegen/llvm.zig+79
......@@ -24,6 +24,7 @@ const CType = @import("../type.zig").CType;
2424const x86_64_abi = @import("../arch/x86_64/abi.zig");
2525const wasm_c_abi = @import("../arch/wasm/abi.zig");
2626const aarch64_c_abi = @import("../arch/aarch64/abi.zig");
27const arm_c_abi = @import("../arch/arm/abi.zig");
2728
2829const Error = error{ OutOfMemory, CodegenFail };
2930
......@@ -1130,6 +1131,25 @@ pub const Object = struct {
11301131 const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), "");
11311132 _ = builder.buildStore(param, casted_ptr);
11321133
1134 if (isByRef(param_ty)) {
1135 try args.append(arg_ptr);
1136 } else {
1137 const load_inst = builder.buildLoad(param_llvm_ty, arg_ptr, "");
1138 load_inst.setAlignment(alignment);
1139 try args.append(load_inst);
1140 }
1141 },
1142 .i32_array, .i64_array => {
1143 const param_ty = fn_info.param_types[it.zig_index - 1];
1144 const param_llvm_ty = try dg.lowerType(param_ty);
1145 const param = llvm_func.getParam(llvm_arg_i);
1146 llvm_arg_i += 1;
1147
1148 const alignment = param_ty.abiAlignment(target);
1149 const arg_ptr = buildAllocaInner(builder, llvm_func, false, param_llvm_ty, alignment, target);
1150 const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), "");
1151 _ = builder.buildStore(param, casted_ptr);
1152
11331153 if (isByRef(param_ty)) {
11341154 try args.append(arg_ptr);
11351155 } else {
......@@ -2578,6 +2598,8 @@ pub const DeclGen = struct {
25782598 .multiple_llvm_float,
25792599 .as_u16,
25802600 .float_array,
2601 .i32_array,
2602 .i64_array,
25812603 => continue,
25822604
25832605 .slice => unreachable, // extern functions do not support slice types.
......@@ -3132,6 +3154,11 @@ pub const DeclGen = struct {
31323154 const arr_ty = float_ty.arrayType(field_count);
31333155 try llvm_params.append(arr_ty);
31343156 },
3157 .i32_array, .i64_array => |arr_len| {
3158 const elem_size: u8 = if (lowering == .i32_array) 32 else 64;
3159 const arr_ty = dg.context.intType(elem_size).arrayType(arr_len);
3160 try llvm_params.append(arr_ty);
3161 },
31353162 };
31363163
31373164 return llvm.functionType(
......@@ -4821,6 +4848,25 @@ pub const FuncGen = struct {
48214848 load_inst.setAlignment(alignment);
48224849 try llvm_args.append(load_inst);
48234850 },
4851 .i32_array, .i64_array => |arr_len| {
4852 const elem_size: u8 = if (lowering == .i32_array) 32 else 64;
4853 const arg = args[it.zig_index - 1];
4854 const arg_ty = self.air.typeOf(arg);
4855 var llvm_arg = try self.resolveInst(arg);
4856 if (!isByRef(arg_ty)) {
4857 const p = self.buildAlloca(llvm_arg.typeOf(), null);
4858 const store_inst = self.builder.buildStore(llvm_arg, p);
4859 store_inst.setAlignment(arg_ty.abiAlignment(target));
4860 llvm_arg = store_inst;
4861 }
4862
4863 const array_llvm_ty = self.dg.context.intType(elem_size).arrayType(arr_len);
4864 const casted = self.builder.buildBitCast(llvm_arg, array_llvm_ty.pointerType(0), "");
4865 const alignment = arg_ty.abiAlignment(target);
4866 const load_inst = self.builder.buildLoad(array_llvm_ty, casted, "");
4867 load_inst.setAlignment(alignment);
4868 try llvm_args.append(load_inst);
4869 },
48244870 };
48254871
48264872 const call = self.builder.buildCall(
......@@ -10068,6 +10114,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool
1006810114 },
1006910115 .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,
1007010116 .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target)[0] == .memory,
10117 .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) {
10118 .memory, .i64_array => return true,
10119 .i32_array => |size| return size != 1,
10120 .none, .byval => return false,
10121 },
1007110122 else => return false, // TODO investigate C ABI for other architectures
1007210123 },
1007310124 else => return false,
......@@ -10195,6 +10246,18 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
1019510246
1019610247 return dg.context.intType(64).arrayType(2);
1019710248 },
10249 .arm, .armeb => {
10250 switch (arm_c_abi.classifyType(fn_info.return_type, target)) {
10251 .memory, .i64_array => return dg.context.voidType(),
10252 .i32_array => |len| if (len == 1) {
10253 return dg.context.intType(32);
10254 } else {
10255 return dg.context.voidType();
10256 },
10257 .byval => return dg.lowerType(fn_info.return_type),
10258 .none => unreachable,
10259 }
10260 },
1019810261 // TODO investigate C ABI for other architectures
1019910262 else => return dg.lowerType(fn_info.return_type),
1020010263 }
......@@ -10223,6 +10286,8 @@ const ParamTypeIterator = struct {
1022310286 slice,
1022410287 as_u16,
1022510288 float_array: u8,
10289 i32_array: u8,
10290 i64_array: u8,
1022610291 };
1022710292
1022810293 pub fn next(it: *ParamTypeIterator) ?Lowering {
......@@ -10410,6 +10475,20 @@ const ParamTypeIterator = struct {
1041010475 it.llvm_types_buffer[1] = 64;
1041110476 return .multiple_llvm_ints;
1041210477 },
10478 .arm, .armeb => {
10479 it.zig_index += 1;
10480 it.llvm_index += 1;
10481 switch (arm_c_abi.classifyType(ty, it.target)) {
10482 .none => unreachable,
10483 .memory => {
10484 it.byval_attr = true;
10485 return .byref;
10486 },
10487 .byval => return .byval,
10488 .i32_array => |size| return Lowering{ .i32_array = size },
10489 .i64_array => |size| return Lowering{ .i64_array = size },
10490 }
10491 },
1041310492 // TODO investigate C ABI for other architectures
1041410493 else => {
1041510494 it.zig_index += 1;
test/c_abi/cfuncs.c+4
......@@ -32,6 +32,10 @@ static void assert_or_panic(bool ok) {
3232# define ZIG_NO_COMPLEX
3333#endif
3434
35#ifdef __arm__
36# define ZIG_NO_COMPLEX
37#endif
38
3539#ifndef ZIG_NO_I128
3640struct i128 {
3741 __int128 value;
test/c_abi/main.zig+2-7
......@@ -167,7 +167,8 @@ extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble;
167167extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;
168168extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble;
169169
170const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS();
170const complex_abi_compatible = builtin.cpu.arch != .i386 and !builtin.cpu.arch.isMIPS() and
171 !builtin.cpu.arch.isARM();
171172
172173test "C ABI complex float" {
173174 if (!complex_abi_compatible) return error.SkipZigTest;
......@@ -320,7 +321,6 @@ extern fn c_ret_med_struct_mixed() MedStructMixed;
320321
321322test "C ABI medium struct of ints and floats" {
322323 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
323 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
324324 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
325325 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
326326
......@@ -353,7 +353,6 @@ extern fn c_ret_small_struct_ints() SmallStructInts;
353353
354354test "C ABI small struct of ints" {
355355 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
356 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
357356 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
358357 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
359358
......@@ -435,7 +434,6 @@ extern fn c_split_struct_ints(SplitStructInt) void;
435434
436435test "C ABI split struct of ints" {
437436 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
438 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
439437 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
440438 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
441439
......@@ -463,7 +461,6 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed;
463461
464462test "C ABI split struct of ints and floats" {
465463 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
466 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
467464 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
468465 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
469466
......@@ -543,7 +540,6 @@ const Vector5 = extern struct {
543540extern fn c_big_struct_floats(Vector5) void;
544541
545542test "C ABI structs of floats as parameter" {
546 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
547543 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
548544 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
549545
......@@ -725,7 +721,6 @@ extern fn c_ret_struct_with_array() StructWithArray;
725721
726722test "Struct with array as padding." {
727723 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
728 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
729724 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
730725 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
731726