| author | |
| committer | |
| log | 031c768cc8399ccdf5440df87d37c03a238315d5 |
| tree | d9299c0405ae7beb0633b406aa99f473f94bf26d |
| parent | 3981250b84b4eb4a34832e3fa5888aa3442e8a74 |
7 files changed, 131 insertions(+), 36 deletions(-)
src/arch/aarch64/abi.zig+19-6| ... | ... | @@ -5,7 +5,14 @@ const Register = bits.Register; |
| 5 | 5 | const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; |
| 6 | 6 | const Type = @import("../../type.zig").Type; |
| 7 | 7 | |
| 8 | pub const Class = union(enum) { memory, integer, double_integer, none, float_array: u8 }; | |
| 8 | pub const Class = union(enum) { | |
| 9 | memory, | |
| 10 | byval, | |
| 11 | integer, | |
| 12 | double_integer, | |
| 13 | none, | |
| 14 | float_array: u8, | |
| 15 | }; | |
| 9 | 16 | |
| 10 | 17 | /// For `float_array` the second element will be the amount of floats. |
| 11 | 18 | pub fn classifyType(ty: Type, target: std.Target) Class { |
| ... | ... | @@ -13,7 +20,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class { |
| 13 | 20 | var maybe_float_bits: ?u16 = null; |
| 14 | 21 | switch (ty.zigTypeTag()) { |
| 15 | 22 | .Struct => { |
| 16 | if (ty.containerLayout() == .Packed) return .integer; | |
| 23 | if (ty.containerLayout() == .Packed) return .byval; | |
| 17 | 24 | const float_count = countFloats(ty, target, &maybe_float_bits); |
| 18 | 25 | if (float_count <= sret_float_count) return .{ .float_array = float_count }; |
| 19 | 26 | |
| ... | ... | @@ -23,7 +30,7 @@ pub fn classifyType(ty: Type, target: std.Target) Class { |
| 23 | 30 | return .integer; |
| 24 | 31 | }, |
| 25 | 32 | .Union => { |
| 26 | if (ty.containerLayout() == .Packed) return .integer; | |
| 33 | if (ty.containerLayout() == .Packed) return .byval; | |
| 27 | 34 | const float_count = countFloats(ty, target, &maybe_float_bits); |
| 28 | 35 | if (float_count <= sret_float_count) return .{ .float_array = float_count }; |
| 29 | 36 | |
| ... | ... | @@ -32,14 +39,20 @@ pub fn classifyType(ty: Type, target: std.Target) Class { |
| 32 | 39 | if (bit_size > 64) return .double_integer; |
| 33 | 40 | return .integer; |
| 34 | 41 | }, |
| 35 | .Int, .Enum, .ErrorSet, .Vector, .Float, .Bool => return .integer, | |
| 42 | .Int, .Enum, .ErrorSet, .Float, .Bool => return .byval, | |
| 43 | .Vector => { | |
| 44 | const bit_size = ty.bitSize(target); | |
| 45 | // TODO is this controlled by a cpu feature? | |
| 46 | if (bit_size > 128) return .memory; | |
| 47 | return .byval; | |
| 48 | }, | |
| 36 | 49 | .Optional => { |
| 37 | 50 | std.debug.assert(ty.isPtrLikeOptional()); |
| 38 | return .integer; | |
| 51 | return .byval; | |
| 39 | 52 | }, |
| 40 | 53 | .Pointer => { |
| 41 | 54 | std.debug.assert(!ty.isSlice()); |
| 42 | return .integer; | |
| 55 | return .byval; | |
| 43 | 56 | }, |
| 44 | 57 | .ErrorUnion, |
| 45 | 58 | .Frame, |
src/arch/arm/abi.zig+9-4| ... | ... | @@ -21,7 +21,9 @@ pub const Class = union(enum) { |
| 21 | 21 | } |
| 22 | 22 | }; |
| 23 | 23 | |
| 24 | pub fn classifyType(ty: Type, target: std.Target) Class { | |
| 24 | pub const Context = enum { ret, arg }; | |
| 25 | ||
| 26 | pub fn classifyType(ty: Type, target: std.Target, ctx: Context) Class { | |
| 25 | 27 | if (!ty.hasRuntimeBitsIgnoreComptime()) return .none; |
| 26 | 28 | |
| 27 | 29 | var maybe_float_bits: ?u16 = null; |
| ... | ... | @@ -66,14 +68,17 @@ pub fn classifyType(ty: Type, target: std.Target) Class { |
| 66 | 68 | } |
| 67 | 69 | return Class.arrSize(bit_size, 32); |
| 68 | 70 | }, |
| 69 | .Int, .Enum => { | |
| 71 | .Bool, .Float => return .byval, | |
| 72 | .Int, .Enum, .ErrorSet => { | |
| 70 | 73 | const bit_size = ty.bitSize(target); |
| 71 | 74 | if (bit_size > 64) return .memory; |
| 72 | 75 | return .byval; |
| 73 | 76 | }, |
| 74 | .ErrorSet, .Vector, .Float, .Bool => { | |
| 77 | .Vector => { | |
| 75 | 78 | const bit_size = ty.bitSize(target); |
| 76 | if (bit_size > 128) return .memory; | |
| 79 | // TODO is this controlled by a cpu feature? | |
| 80 | if (ctx == .ret and bit_size > 128) return .memory; | |
| 81 | if (bit_size > 512) return .memory; | |
| 77 | 82 | return .byval; |
| 78 | 83 | }, |
| 79 | 84 | .Optional => { |
src/arch/x86_64/CodeGen.zig+1-1| ... | ... | @@ -7143,7 +7143,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 7143 | 7143 | |
| 7144 | 7144 | const classes: []const abi.Class = switch (self.target.os.tag) { |
| 7145 | 7145 | .windows => &[1]abi.Class{abi.classifyWindows(ty, self.target.*)}, |
| 7146 | else => mem.sliceTo(&abi.classifySystemV(ty, self.target.*), .none), | |
| 7146 | else => mem.sliceTo(&abi.classifySystemV(ty, self.target.*, .arg), .none), | |
| 7147 | 7147 | }; |
| 7148 | 7148 | if (classes.len > 1) { |
| 7149 | 7149 | return self.fail("TODO handle multiple classes per type", .{}); |
src/arch/x86_64/abi.zig+21-3| ... | ... | @@ -60,9 +60,11 @@ pub fn classifyWindows(ty: Type, target: Target) Class { |
| 60 | 60 | } |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | pub const Context = enum { ret, arg }; | |
| 64 | ||
| 63 | 65 | /// There are a maximum of 8 possible return slots. Returned values are in |
| 64 | 66 | /// the beginning of the array; unused slots are filled with .none. |
| 65 | pub fn classifySystemV(ty: Type, target: Target) [8]Class { | |
| 67 | pub fn classifySystemV(ty: Type, target: Target, ctx: Context) [8]Class { | |
| 66 | 68 | const memory_class = [_]Class{ |
| 67 | 69 | .memory, .none, .none, .none, |
| 68 | 70 | .none, .none, .none, .none, |
| ... | ... | @@ -134,6 +136,22 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { |
| 134 | 136 | }, |
| 135 | 137 | .Vector => { |
| 136 | 138 | const elem_ty = ty.childType(); |
| 139 | if (ctx == .arg) { | |
| 140 | const bit_size = ty.bitSize(target); | |
| 141 | if (bit_size > 128) return memory_class; | |
| 142 | if (bit_size > 80) return .{ | |
| 143 | .integer, .integer, .none, .none, | |
| 144 | .none, .none, .none, .none, | |
| 145 | }; | |
| 146 | if (bit_size > 64) return .{ | |
| 147 | .x87, .none, .none, .none, | |
| 148 | .none, .none, .none, .none, | |
| 149 | }; | |
| 150 | return .{ | |
| 151 | .integer, .none, .none, .none, | |
| 152 | .none, .none, .none, .none, | |
| 153 | }; | |
| 154 | } | |
| 137 | 155 | const bits = elem_ty.bitSize(target) * ty.arrayLen(); |
| 138 | 156 | if (bits <= 64) return .{ |
| 139 | 157 | .sse, .none, .none, .none, |
| ... | ... | @@ -201,7 +219,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { |
| 201 | 219 | } |
| 202 | 220 | } |
| 203 | 221 | const field_size = field.ty.abiSize(target); |
| 204 | const field_class_array = classifySystemV(field.ty, target); | |
| 222 | const field_class_array = classifySystemV(field.ty, target, .arg); | |
| 205 | 223 | const field_class = std.mem.sliceTo(&field_class_array, .none); |
| 206 | 224 | if (byte_i + field_size <= 8) { |
| 207 | 225 | // Combine this field with the previous one. |
| ... | ... | @@ -315,7 +333,7 @@ pub fn classifySystemV(ty: Type, target: Target) [8]Class { |
| 315 | 333 | } |
| 316 | 334 | } |
| 317 | 335 | // Combine this field with the previous one. |
| 318 | const field_class = classifySystemV(field.ty, target); | |
| 336 | const field_class = classifySystemV(field.ty, target, .arg); | |
| 319 | 337 | for (result) |*result_item, i| { |
| 320 | 338 | const field_item = field_class[i]; |
| 321 | 339 | // "If both classes are equal, this is the resulting class." |
src/codegen/llvm.zig+13-17| ... | ... | @@ -10110,11 +10110,11 @@ fn firstParamSRet(fn_info: Type.Payload.Function.Data, target: std.Target) bool |
| 10110 | 10110 | .mips, .mipsel => return false, |
| 10111 | 10111 | .x86_64 => switch (target.os.tag) { |
| 10112 | 10112 | .windows => return x86_64_abi.classifyWindows(fn_info.return_type, target) == .memory, |
| 10113 | else => return x86_64_abi.classifySystemV(fn_info.return_type, target)[0] == .memory, | |
| 10113 | else => return x86_64_abi.classifySystemV(fn_info.return_type, target, .ret)[0] == .memory, | |
| 10114 | 10114 | }, |
| 10115 | 10115 | .wasm32 => return wasm_c_abi.classifyType(fn_info.return_type, target)[0] == .indirect, |
| 10116 | 10116 | .aarch64, .aarch64_be => return aarch64_c_abi.classifyType(fn_info.return_type, target) == .memory, |
| 10117 | .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target)) { | |
| 10117 | .arm, .armeb => switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) { | |
| 10118 | 10118 | .memory, .i64_array => return true, |
| 10119 | 10119 | .i32_array => |size| return size != 1, |
| 10120 | 10120 | .none, .byval => return false, |
| ... | ... | @@ -10171,7 +10171,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { |
| 10171 | 10171 | if (is_scalar) { |
| 10172 | 10172 | return dg.lowerType(fn_info.return_type); |
| 10173 | 10173 | } |
| 10174 | const classes = x86_64_abi.classifySystemV(fn_info.return_type, target); | |
| 10174 | const classes = x86_64_abi.classifySystemV(fn_info.return_type, target, .ret); | |
| 10175 | 10175 | if (classes[0] == .memory) { |
| 10176 | 10176 | return dg.context.voidType(); |
| 10177 | 10177 | } |
| ... | ... | @@ -10229,12 +10229,10 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { |
| 10229 | 10229 | return dg.context.intType(@intCast(c_uint, abi_size * 8)); |
| 10230 | 10230 | }, |
| 10231 | 10231 | .aarch64, .aarch64_be => { |
| 10232 | if (is_scalar) { | |
| 10233 | return dg.lowerType(fn_info.return_type); | |
| 10234 | } | |
| 10235 | 10232 | switch (aarch64_c_abi.classifyType(fn_info.return_type, target)) { |
| 10236 | 10233 | .memory, .none => return dg.context.voidType(), |
| 10237 | 10234 | .float_array => return dg.lowerType(fn_info.return_type), |
| 10235 | .byval => return dg.lowerType(fn_info.return_type), | |
| 10238 | 10236 | .integer => { |
| 10239 | 10237 | const bit_size = fn_info.return_type.bitSize(target); |
| 10240 | 10238 | return dg.context.intType(@intCast(c_uint, bit_size)); |
| ... | ... | @@ -10243,7 +10241,7 @@ fn lowerFnRetTy(dg: *DeclGen, fn_info: Type.Payload.Function.Data) !*llvm.Type { |
| 10243 | 10241 | } |
| 10244 | 10242 | }, |
| 10245 | 10243 | .arm, .armeb => { |
| 10246 | switch (arm_c_abi.classifyType(fn_info.return_type, target)) { | |
| 10244 | switch (arm_c_abi.classifyType(fn_info.return_type, target, .ret)) { | |
| 10247 | 10245 | .memory, .i64_array => return dg.context.voidType(), |
| 10248 | 10246 | .i32_array => |len| if (len == 1) { |
| 10249 | 10247 | return dg.context.intType(32); |
| ... | ... | @@ -10376,18 +10374,18 @@ const ParamTypeIterator = struct { |
| 10376 | 10374 | else => unreachable, |
| 10377 | 10375 | }, |
| 10378 | 10376 | else => { |
| 10379 | if (is_scalar) { | |
| 10380 | it.zig_index += 1; | |
| 10381 | it.llvm_index += 1; | |
| 10382 | return .byval; | |
| 10383 | } | |
| 10384 | const classes = x86_64_abi.classifySystemV(ty, it.target); | |
| 10377 | const classes = x86_64_abi.classifySystemV(ty, it.target, .arg); | |
| 10385 | 10378 | if (classes[0] == .memory) { |
| 10386 | 10379 | it.zig_index += 1; |
| 10387 | 10380 | it.llvm_index += 1; |
| 10388 | 10381 | it.byval_attr = true; |
| 10389 | 10382 | return .byref; |
| 10390 | 10383 | } |
| 10384 | if (is_scalar) { | |
| 10385 | it.zig_index += 1; | |
| 10386 | it.llvm_index += 1; | |
| 10387 | return .byval; | |
| 10388 | } | |
| 10391 | 10389 | var llvm_types_buffer: [8]u16 = undefined; |
| 10392 | 10390 | var llvm_types_index: u32 = 0; |
| 10393 | 10391 | for (classes) |class| { |
| ... | ... | @@ -10452,13 +10450,11 @@ const ParamTypeIterator = struct { |
| 10452 | 10450 | .aarch64, .aarch64_be => { |
| 10453 | 10451 | it.zig_index += 1; |
| 10454 | 10452 | it.llvm_index += 1; |
| 10455 | if (is_scalar) { | |
| 10456 | return .byval; | |
| 10457 | } | |
| 10458 | 10453 | switch (aarch64_c_abi.classifyType(ty, it.target)) { |
| 10459 | 10454 | .none => unreachable, |
| 10460 | 10455 | .memory => return .byref, |
| 10461 | 10456 | .float_array => |len| return Lowering{ .float_array = len }, |
| 10457 | .byval => return .byval, | |
| 10462 | 10458 | .integer => { |
| 10463 | 10459 | it.llvm_types_len = 1; |
| 10464 | 10460 | it.llvm_types_buffer[0] = 64; |
| ... | ... | @@ -10470,7 +10466,7 @@ const ParamTypeIterator = struct { |
| 10470 | 10466 | .arm, .armeb => { |
| 10471 | 10467 | it.zig_index += 1; |
| 10472 | 10468 | it.llvm_index += 1; |
| 10473 | switch (arm_c_abi.classifyType(ty, it.target)) { | |
| 10469 | switch (arm_c_abi.classifyType(ty, it.target, .arg)) { | |
| 10474 | 10470 | .none => unreachable, |
| 10475 | 10471 | .memory => { |
| 10476 | 10472 | it.byval_attr = true; |
test/c_abi/cfuncs.c+33-5| ... | ... | @@ -1,8 +1,8 @@ |
| 1 | #include <complex.h> | |
| 1 | 2 | #include <inttypes.h> |
| 2 | #include <stdlib.h> | |
| 3 | 3 | #include <stdbool.h> |
| 4 | #include <stdlib.h> | |
| 4 | 5 | #include <string.h> |
| 5 | #include <complex.h> | |
| 6 | 6 | |
| 7 | 7 | void zig_panic(); |
| 8 | 8 | |
| ... | ... | @@ -210,7 +210,7 @@ void run_c_tests(void) { |
| 210 | 210 | zig_longdouble(12.34l); |
| 211 | 211 | zig_five_floats(1.0f, 2.0f, 3.0f, 4.0f, 5.0f); |
| 212 | 212 | |
| 213 | zig_ptr((void*)0xdeadbeefL); | |
| 213 | zig_ptr((void *)0xdeadbeefL); | |
| 214 | 214 | |
| 215 | 215 | zig_bool(true); |
| 216 | 216 | |
| ... | ... | @@ -408,7 +408,7 @@ void c_long_double(long double x) { |
| 408 | 408 | } |
| 409 | 409 | |
| 410 | 410 | void c_ptr(void *x) { |
| 411 | assert_or_panic(x == (void*)0xdeadbeefL); | |
| 411 | assert_or_panic(x == (void *)0xdeadbeefL); | |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | void c_bool(bool x) { |
| ... | ... | @@ -676,7 +676,7 @@ void c_struct_with_array(StructWithArray x) { |
| 676 | 676 | } |
| 677 | 677 | |
| 678 | 678 | StructWithArray c_ret_struct_with_array() { |
| 679 | return (StructWithArray) { 4, {}, 155 }; | |
| 679 | return (StructWithArray){4, {}, 155}; | |
| 680 | 680 | } |
| 681 | 681 | |
| 682 | 682 | typedef struct { |
| ... | ... | @@ -705,3 +705,31 @@ FloatArrayStruct c_ret_float_array_struct() { |
| 705 | 705 | x.size.height = 4; |
| 706 | 706 | return x; |
| 707 | 707 | } |
| 708 | ||
| 709 | typedef uint32_t SmallVec __attribute__((vector_size(2 * sizeof(uint32_t)))); | |
| 710 | ||
| 711 | void c_small_vec(SmallVec vec) { | |
| 712 | assert_or_panic(vec[0] == 1); | |
| 713 | assert_or_panic(vec[1] == 2); | |
| 714 | } | |
| 715 | ||
| 716 | SmallVec c_ret_small_vec(void) { | |
| 717 | return (SmallVec){3, 4}; | |
| 718 | } | |
| 719 | ||
| 720 | typedef size_t BigVec __attribute__((vector_size(8 * sizeof(size_t)))); | |
| 721 | ||
| 722 | void c_big_vec(BigVec vec) { | |
| 723 | assert_or_panic(vec[0] == 1); | |
| 724 | assert_or_panic(vec[1] == 2); | |
| 725 | assert_or_panic(vec[2] == 3); | |
| 726 | assert_or_panic(vec[3] == 4); | |
| 727 | assert_or_panic(vec[4] == 5); | |
| 728 | assert_or_panic(vec[5] == 6); | |
| 729 | assert_or_panic(vec[6] == 7); | |
| 730 | assert_or_panic(vec[7] == 8); | |
| 731 | } | |
| 732 | ||
| 733 | BigVec c_ret_big_vec(void) { | |
| 734 | return (BigVec){9, 10, 11, 12, 13, 14, 15, 16}; | |
| 735 | } |
test/c_abi/main.zig+35| ... | ... | @@ -766,3 +766,38 @@ test "Float array like struct" { |
| 766 | 766 | try std.testing.expect(x.size.width == 3); |
| 767 | 767 | try std.testing.expect(x.size.height == 4); |
| 768 | 768 | } |
| 769 | ||
| 770 | const SmallVec = @Vector(2, u32); | |
| 771 | ||
| 772 | extern fn c_small_vec(SmallVec) void; | |
| 773 | extern fn c_ret_small_vec() SmallVec; | |
| 774 | ||
| 775 | test "small simd vector" { | |
| 776 | if (builtin.cpu.arch == .i386) return error.SkipZigTest; | |
| 777 | if (comptime builtin.cpu.arch.isRISCV()) return error.SkipZigTest; | |
| 778 | ||
| 779 | c_small_vec(.{ 1, 2 }); | |
| 780 | ||
| 781 | var x = c_ret_small_vec(); | |
| 782 | try std.testing.expect(x[0] == 3); | |
| 783 | try std.testing.expect(x[1] == 4); | |
| 784 | } | |
| 785 | ||
| 786 | const BigVec = @Vector(8, usize); | |
| 787 | ||
| 788 | extern fn c_big_vec(BigVec) void; | |
| 789 | extern fn c_ret_big_vec() BigVec; | |
| 790 | ||
| 791 | test "big simd vector" { | |
| 792 | c_big_vec(.{ 1, 2, 3, 4, 5, 6, 7, 8 }); | |
| 793 | ||
| 794 | var x = c_ret_big_vec(); | |
| 795 | try std.testing.expect(x[0] == 9); | |
| 796 | try std.testing.expect(x[1] == 10); | |
| 797 | try std.testing.expect(x[2] == 11); | |
| 798 | try std.testing.expect(x[3] == 12); | |
| 799 | try std.testing.expect(x[4] == 13); | |
| 800 | try std.testing.expect(x[5] == 14); | |
| 801 | try std.testing.expect(x[6] == 15); | |
| 802 | try std.testing.expect(x[7] == 16); | |
| 803 | } |