authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-07 13:51:11-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-07 13:51:11-04:00
log743b2e4afc72f436a73977f896b32e6041785795
tree60868daa6a79c9578a649c738df1aac2a15e9e35
parent421ca1523feec17db75e7c53b05e5abe4c88d58f
signaturelock-open Commit is signed but in an unrecognized format.

add C ABI test for big unions


3 files changed, 92 insertions(+), 5 deletions(-)

src/codegen.cpp+3-1
...@@ -1838,6 +1838,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty...@@ -1838,6 +1838,7 @@ static LLVMValueRef gen_assign_raw(CodeGen *g, LLVMValueRef ptr, ZigType *ptr_ty
1838}1838}
18391839
1840static void gen_var_debug_decl(CodeGen *g, ZigVar *var) {1840static void gen_var_debug_decl(CodeGen *g, ZigVar *var) {
1841 assert(var->di_loc_var != nullptr);
1841 AstNode *source_node = var->decl_node;1842 AstNode *source_node = var->decl_node;
1842 ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc((unsigned)source_node->line + 1,1843 ZigLLVMDILocation *debug_loc = ZigLLVMGetDebugLoc((unsigned)source_node->line + 1,
1843 (unsigned)source_node->column + 1, get_di_scope(g, var->parent_scope));1844 (unsigned)source_node->column + 1, get_di_scope(g, var->parent_scope));
...@@ -2119,11 +2120,12 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_...@@ -2119,11 +2120,12 @@ static bool iter_function_params_c_abi(CodeGen *g, ZigType *fn_type, FnWalk *fn_
2119 di_arg_index = fn_walk->data.vars.gen_i;2120 di_arg_index = fn_walk->data.vars.gen_i;
2120 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);2121 var->value_ref = build_alloca(g, ty, buf_ptr(&var->name), var->align_bytes);
2121 fn_walk->data.vars.gen_i += 1;2122 fn_walk->data.vars.gen_i += 1;
2123 dest_ty = ty;
2122 goto var_ok;2124 goto var_ok;
2123 }2125 }
2124 case FnWalkIdInits: {2126 case FnWalkIdInits: {
2125 clear_debug_source_node(g);2127 clear_debug_source_node(g);
2126 LLVMValueRef arg = LLVMGetParam(llvm_fn, fn_walk->data.inits.gen_i += 1);2128 LLVMValueRef arg = LLVMGetParam(llvm_fn, fn_walk->data.inits.gen_i);
2127 LLVMTypeRef ptr_to_int_type_ref = LLVMPointerType(LLVMIntType((unsigned)ty_size * 8), 0);2129 LLVMTypeRef ptr_to_int_type_ref = LLVMPointerType(LLVMIntType((unsigned)ty_size * 8), 0);
2128 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, var->value_ref, ptr_to_int_type_ref, "");2130 LLVMValueRef bitcasted = LLVMBuildBitCast(g->builder, var->value_ref, ptr_to_int_type_ref, "");
2129 gen_store_untyped(g, arg, bitcasted, var->align_bytes, false);2131 gen_store_untyped(g, arg, bitcasted, var->align_bytes, false);
test/stage1/c_abi/cfuncs.c+38-4
...@@ -28,8 +28,6 @@ void zig_bool(bool);...@@ -28,8 +28,6 @@ void zig_bool(bool);
2828
29void zig_array(uint8_t[10]);29void zig_array(uint8_t[10]);
3030
31static uint8_t array[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
32
33struct BigStruct {31struct BigStruct {
34 uint64_t a;32 uint64_t a;
35 uint64_t b;33 uint64_t b;
...@@ -40,7 +38,19 @@ struct BigStruct {...@@ -40,7 +38,19 @@ struct BigStruct {
4038
41void zig_big_struct(struct BigStruct);39void zig_big_struct(struct BigStruct);
4240
43static struct BigStruct s = {1, 2, 3, 4, 5};41union BigUnion {
42 struct BigStruct a;
43};
44
45void zig_big_union(union BigUnion);
46
47struct SmallStructInts {
48 uint8_t a;
49 uint8_t b;
50 uint8_t c;
51 uint8_t d;
52};
53void zig_small_struct_ints(struct SmallStructInts);
4454
45void run_c_tests(void) {55void run_c_tests(void) {
46 zig_u8(0xff);56 zig_u8(0xff);
...@@ -60,9 +70,19 @@ void run_c_tests(void) {...@@ -60,9 +70,19 @@ void run_c_tests(void) {
6070
61 zig_bool(true);71 zig_bool(true);
6272
73 // TODO making this non-static crashes for some reason
74 static uint8_t array[10] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
63 zig_array(array);75 zig_array(array);
6476
65 zig_big_struct(s);77 {
78 struct BigStruct s = {1, 2, 3, 4, 5};
79 zig_big_struct(s);
80 }
81
82 {
83 struct SmallStructInts s = {1, 2, 3, 4};
84 zig_small_struct_ints(s);
85 }
66}86}
6787
68void c_u8(uint8_t x) {88void c_u8(uint8_t x) {
...@@ -133,3 +153,17 @@ void c_big_struct(struct BigStruct x) {...@@ -133,3 +153,17 @@ void c_big_struct(struct BigStruct x) {
133 assert_or_panic(x.d == 4);153 assert_or_panic(x.d == 4);
134 assert_or_panic(x.e == 5);154 assert_or_panic(x.e == 5);
135}155}
156
157void c_big_union(union BigUnion x) {
158 assert_or_panic(x.a.a == 1);
159 assert_or_panic(x.a.b == 2);
160 assert_or_panic(x.a.c == 3);
161 assert_or_panic(x.a.d == 4);
162}
163
164void c_small_struct_ints(struct SmallStructInts x) {
165 assert_or_panic(x.a == 1);
166 assert_or_panic(x.b == 2);
167 assert_or_panic(x.c == 3);
168 assert_or_panic(x.d == 4);
169}
test/stage1/c_abi/main.zig+51
...@@ -130,3 +130,54 @@ export fn zig_big_struct(x: BigStruct) void {...@@ -130,3 +130,54 @@ export fn zig_big_struct(x: BigStruct) void {
130 assertOrPanic(x.d == 4);130 assertOrPanic(x.d == 4);
131 assertOrPanic(x.e == 5);131 assertOrPanic(x.e == 5);
132}132}
133
134const BigUnion = extern union {
135 a: BigStruct,
136};
137extern fn c_big_union(BigUnion) void;
138
139test "C ABI big union" {
140 var x = BigUnion{
141 .a = BigStruct{
142 .a = 1,
143 .b = 2,
144 .c = 3,
145 .d = 4,
146 .e = 5,
147 },
148 };
149 c_big_union(x);
150}
151
152export fn zig_big_union(x: BigUnion) void {
153 assertOrPanic(x.a.a == 1);
154 assertOrPanic(x.a.b == 2);
155 assertOrPanic(x.a.c == 3);
156 assertOrPanic(x.a.d == 4);
157 assertOrPanic(x.a.e == 5);
158}
159
160const SmallStructInts = extern struct {
161 a: u8,
162 b: u8,
163 c: u8,
164 d: u8,
165};
166extern fn c_small_struct_ints(SmallStructInts) void;
167
168test "C ABI small struct of ints" {
169 var s = SmallStructInts{
170 .a = 1,
171 .b = 2,
172 .c = 3,
173 .d = 4,
174 };
175 c_small_struct_ints(s);
176}
177
178export fn zig_small_struct_ints(x: SmallStructInts) void {
179 assertOrPanic(x.a == 1);
180 assertOrPanic(x.b == 2);
181 assertOrPanic(x.c == 3);
182 assertOrPanic(x.d == 4);
183}