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");...@@ -2,6 +2,149 @@ const std = @import("std");
2const bits = @import("bits.zig");2const bits = @import("bits.zig");
3const Register = bits.Register;3const Register = bits.Register;
4const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager;4const 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
6pub const callee_preserved_regs = [_]Register{ .r4, .r5, .r6, .r7, .r8, .r10 };149pub const callee_preserved_regs = [_]Register{ .r4, .r5, .r6, .r7, .r8, .r10 };
7pub const caller_preserved_regs = [_]Register{ .r0, .r1, .r2, .r3 };150pub const caller_preserved_regs = [_]Register{ .r0, .r1, .r2, .r3 };
src/codegen/llvm.zig+79
...@@ -24,6 +24,7 @@ const CType = @import("../type.zig").CType;...@@ -24,6 +24,7 @@ const CType = @import("../type.zig").CType;
24const x86_64_abi = @import("../arch/x86_64/abi.zig");24const x86_64_abi = @import("../arch/x86_64/abi.zig");
25const wasm_c_abi = @import("../arch/wasm/abi.zig");25const wasm_c_abi = @import("../arch/wasm/abi.zig");
26const aarch64_c_abi = @import("../arch/aarch64/abi.zig");26const aarch64_c_abi = @import("../arch/aarch64/abi.zig");
27const arm_c_abi = @import("../arch/arm/abi.zig");
2728
28const Error = error{ OutOfMemory, CodegenFail };29const Error = error{ OutOfMemory, CodegenFail };
2930
...@@ -1130,6 +1131,25 @@ pub const Object = struct {...@@ -1130,6 +1131,25 @@ pub const Object = struct {
1130 const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), "");1131 const casted_ptr = builder.buildBitCast(arg_ptr, param.typeOf().pointerType(0), "");
1131 _ = builder.buildStore(param, casted_ptr);1132 _ = 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
1133 if (isByRef(param_ty)) {1153 if (isByRef(param_ty)) {
1134 try args.append(arg_ptr);1154 try args.append(arg_ptr);
1135 } else {1155 } else {
...@@ -2578,6 +2598,8 @@ pub const DeclGen = struct {...@@ -2578,6 +2598,8 @@ pub const DeclGen = struct {
2578 .multiple_llvm_float,2598 .multiple_llvm_float,
2579 .as_u16,2599 .as_u16,
2580 .float_array,2600 .float_array,
2601 .i32_array,
2602 .i64_array,
2581 => continue,2603 => continue,
25822604
2583 .slice => unreachable, // extern functions do not support slice types.2605 .slice => unreachable, // extern functions do not support slice types.
...@@ -3132,6 +3154,11 @@ pub const DeclGen = struct {...@@ -3132,6 +3154,11 @@ pub const DeclGen = struct {
3132 const arr_ty = float_ty.arrayType(field_count);3154 const arr_ty = float_ty.arrayType(field_count);
3133 try llvm_params.append(arr_ty);3155 try llvm_params.append(arr_ty);
3134 },3156 },
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 },
3135 };3162 };
31363163
3137 return llvm.functionType(3164 return llvm.functionType(
...@@ -4821,6 +4848,25 @@ pub const FuncGen = struct {...@@ -4821,6 +4848,25 @@ pub const FuncGen = struct {
4821 load_inst.setAlignment(alignment);4848 load_inst.setAlignment(alignment);
4822 try llvm_args.append(load_inst);4849 try llvm_args.append(load_inst);
4823 },4850 },
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 },
4824 };4870 };
48254871
4826 const call = self.builder.buildCall(4872 const call = self.builder.buildCall(
...@@ -10068,6 +10114,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool...@@ -10068,6 +10114,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool
10068 },10114 },
10069 .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,10115 .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect,
10070 .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target)[0] == .memory,10116 .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 },
10071 else => return false, // TODO investigate C ABI for other architectures10122 else => return false, // TODO investigate C ABI for other architectures
10072 },10123 },
10073 else => return false,10124 else => return false,
...@@ -10195,6 +10246,18 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {...@@ -10195,6 +10246,18 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type {
1019510246
10196 return dg.context.intType(64).arrayType(2);10247 return dg.context.intType(64).arrayType(2);
10197 },10248 },
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 },
10198 // TODO investigate C ABI for other architectures10261 // TODO investigate C ABI for other architectures
10199 else => return dg.lowerType(fn_info.return_type),10262 else => return dg.lowerType(fn_info.return_type),
10200 }10263 }
...@@ -10223,6 +10286,8 @@ const ParamTypeIterator = struct {...@@ -10223,6 +10286,8 @@ const ParamTypeIterator = struct {
10223 slice,10286 slice,
10224 as_u16,10287 as_u16,
10225 float_array: u8,10288 float_array: u8,
10289 i32_array: u8,
10290 i64_array: u8,
10226 };10291 };
1022710292
10228 pub fn next(it: *ParamTypeIterator) ?Lowering {10293 pub fn next(it: *ParamTypeIterator) ?Lowering {
...@@ -10410,6 +10475,20 @@ const ParamTypeIterator = struct {...@@ -10410,6 +10475,20 @@ const ParamTypeIterator = struct {
10410 it.llvm_types_buffer[1] = 64;10475 it.llvm_types_buffer[1] = 64;
10411 return .multiple_llvm_ints;10476 return .multiple_llvm_ints;
10412 },10477 },
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 },
10413 // TODO investigate C ABI for other architectures10492 // TODO investigate C ABI for other architectures
10414 else => {10493 else => {
10415 it.zig_index += 1;10494 it.zig_index += 1;
test/c_abi/cfuncs.c+4
...@@ -32,6 +32,10 @@ static void assert_or_panic(bool ok) {...@@ -32,6 +32,10 @@ static void assert_or_panic(bool ok) {
32# define ZIG_NO_COMPLEX32# define ZIG_NO_COMPLEX
33#endif33#endif
3434
35#ifdef __arm__
36# define ZIG_NO_COMPLEX
37#endif
38
35#ifndef ZIG_NO_I12839#ifndef ZIG_NO_I128
36struct i128 {40struct i128 {
37 __int128 value;41 __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;...@@ -167,7 +167,8 @@ extern fn c_cmultd_comp(a_r: f64, a_i: f64, b_r: f64, b_i: f64) ComplexDouble;
167extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;167extern fn c_cmultf(a: ComplexFloat, b: ComplexFloat) ComplexFloat;
168extern fn c_cmultd(a: ComplexDouble, b: ComplexDouble) ComplexDouble;168extern 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
172test "C ABI complex float" {173test "C ABI complex float" {
173 if (!complex_abi_compatible) return error.SkipZigTest;174 if (!complex_abi_compatible) return error.SkipZigTest;
...@@ -320,7 +321,6 @@ extern fn c_ret_med_struct_mixed() MedStructMixed;...@@ -320,7 +321,6 @@ extern fn c_ret_med_struct_mixed() MedStructMixed;
320321
321test "C ABI medium struct of ints and floats" {322test "C ABI medium struct of ints and floats" {
322 if (builtin.cpu.arch == .i386) return error.SkipZigTest;323 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
323 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
324 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;324 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
325 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;325 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
326326
...@@ -353,7 +353,6 @@ extern fn c_ret_small_struct_ints() SmallStructInts;...@@ -353,7 +353,6 @@ extern fn c_ret_small_struct_ints() SmallStructInts;
353353
354test "C ABI small struct of ints" {354test "C ABI small struct of ints" {
355 if (builtin.cpu.arch == .i386) return error.SkipZigTest;355 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
356 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
357 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;356 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
358 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;357 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
359358
...@@ -435,7 +434,6 @@ extern fn c_split_struct_ints(SplitStructInt) void;...@@ -435,7 +434,6 @@ extern fn c_split_struct_ints(SplitStructInt) void;
435434
436test "C ABI split struct of ints" {435test "C ABI split struct of ints" {
437 if (builtin.cpu.arch == .i386) return error.SkipZigTest;436 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
438 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
439 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;437 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
440 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;438 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
441439
...@@ -463,7 +461,6 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed;...@@ -463,7 +461,6 @@ extern fn c_ret_split_struct_mixed() SplitStructMixed;
463461
464test "C ABI split struct of ints and floats" {462test "C ABI split struct of ints and floats" {
465 if (builtin.cpu.arch == .i386) return error.SkipZigTest;463 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
466 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
467 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;464 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
468 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;465 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
469466
...@@ -543,7 +540,6 @@ const Vector5 = extern struct {...@@ -543,7 +540,6 @@ const Vector5 = extern struct {
543extern fn c_big_struct_floats(Vector5) void;540extern fn c_big_struct_floats(Vector5) void;
544541
545test "C ABI structs of floats as parameter" {542test "C ABI structs of floats as parameter" {
546 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
547 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;543 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
548 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;544 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
549545
...@@ -725,7 +721,6 @@ extern fn c_ret_struct_with_array() StructWithArray;...@@ -725,7 +721,6 @@ extern fn c_ret_struct_with_array() StructWithArray;
725721
726test "Struct with array as padding." {722test "Struct with array as padding." {
727 if (builtin.cpu.arch == .i386) return error.SkipZigTest;723 if (builtin.cpu.arch == .i386) return error.SkipZigTest;
728 if (comptime builtin.cpu.arch.isARM()) return error.SkipZigTest;
729 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;724 if (comptime builtin.cpu.arch.isMIPS()) return error.SkipZigTest;
730 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;725 if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest;
731726