authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-11-03 13:43:10+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-01-09 13:45:06-07:00
loga7b8d011a3e3ae01765541439df7a67891f3b7b4
tree39c6c9f3a004a7f4cfa7b5f0fc79cd732faa5707
parentee8af8cffb42603ead90db6a3d7b7df9f2b2a683

Merge pull request #13420 from jacobly0/c-backend

cbe: enough fixes to bootstrap a compiler with a working c backend

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

lib/include/zig.h+52-51
......@@ -66,9 +66,9 @@
6666#endif
6767
6868#if defined(__cplusplus)
69#define zig_extern_c extern "C"
69#define zig_extern extern "C"
7070#else
71#define zig_extern_c
71#define zig_extern extern
7272#endif
7373
7474#if zig_has_builtin(debugtrap)
......@@ -265,8 +265,8 @@ typedef int64_t zig_i64;
265265#define zig_compiler_rt_abbrev_f80 xf
266266#define zig_compiler_rt_abbrev_f128 tf
267267
268zig_extern_c void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize);
269zig_extern_c void *memset (void *, int, zig_usize);
268zig_extern void *memcpy (void *zig_restrict, void const *zig_restrict, zig_usize);
269zig_extern void *memset (void *, int, zig_usize);
270270
271271/* ==================== 8/16/32/64-bit Integer Routines ===================== */
272272
......@@ -279,24 +279,21 @@ zig_extern_c void *memset (void *, int, zig_usize);
279279 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##RhsType rhs) { \
280280 return lhs operator rhs; \
281281 }
282#define zig_int_operators(w) \
283 zig_int_operator(u##w, u##w, and, &) \
284 zig_int_operator(i##w, i##w, and, &) \
285 zig_int_operator(u##w, u##w, or, |) \
286 zig_int_operator(i##w, i##w, or, |) \
287 zig_int_operator(u##w, u##w, xor, ^) \
288 zig_int_operator(i##w, i##w, xor, ^) \
289 zig_int_operator(u##w, u8, shl, <<) \
290 zig_int_operator(i##w, u8, shl, <<) \
291 zig_int_operator(u##w, u8, shr, >>) \
292 zig_int_operator(u##w, u##w, div_floor, /) \
293 zig_int_operator(u##w, u##w, mod, %)
294zig_int_operators(8)
295zig_int_operators(16)
296zig_int_operators(32)
297zig_int_operators(64)
298
282#define zig_int_basic_operator(Type, operation, operator) \
283 zig_int_operator(Type, Type, operation, operator)
284#define zig_int_shift_operator(Type, operation, operator) \
285 zig_int_operator(Type, u8, operation, operator)
299286#define zig_int_helpers(w) \
287 zig_int_basic_operator(u##w, and, &) \
288 zig_int_basic_operator(i##w, and, &) \
289 zig_int_basic_operator(u##w, or, |) \
290 zig_int_basic_operator(i##w, or, |) \
291 zig_int_basic_operator(u##w, xor, ^) \
292 zig_int_basic_operator(i##w, xor, ^) \
293 zig_int_shift_operator(u##w, shl, <<) \
294 zig_int_shift_operator(i##w, shl, <<) \
295 zig_int_shift_operator(u##w, shr, >>) \
296\
300297 static inline zig_i##w zig_shr_i##w(zig_i##w lhs, zig_u8 rhs) { \
301298 zig_i##w sign_mask = lhs < zig_as_i##w(0) ? -zig_as_i##w(1) : zig_as_i##w(0); \
302299 return ((lhs ^ sign_mask) >> rhs) ^ sign_mask; \
......@@ -319,10 +316,14 @@ zig_int_operators(64)
319316 return (val & zig_as_u##w(1) << (bits - zig_as_u8(1))) != 0 \
320317 ? val | zig_minInt(i##w, bits) : val & zig_maxInt(i##w, bits); \
321318 } \
319\
320 zig_int_basic_operator(u##w, div_floor, /) \
322321\
323322 static inline zig_i##w zig_div_floor_i##w(zig_i##w lhs, zig_i##w rhs) { \
324323 return lhs / rhs - (((lhs ^ rhs) & (lhs % rhs)) < zig_as_i##w(0)); \
325324 } \
325\
326 zig_int_basic_operator(u##w, mod, %) \
326327\
327328 static inline zig_i##w zig_mod_i##w(zig_i##w lhs, zig_i##w rhs) { \
328329 zig_i##w rem = lhs % rhs; \
......@@ -345,7 +346,7 @@ static inline zig_bool zig_addo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_
345346#endif
346347}
347348
348zig_extern_c zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
349zig_extern zig_i32 __addosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
349350static inline zig_bool zig_addo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
350351#if zig_has_builtin(add_overflow)
351352 zig_i32 full_res;
......@@ -371,7 +372,7 @@ static inline zig_bool zig_addo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_
371372#endif
372373}
373374
374zig_extern_c zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
375zig_extern zig_i64 __addodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
375376static inline zig_bool zig_addo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
376377#if zig_has_builtin(add_overflow)
377378 zig_i64 full_res;
......@@ -441,7 +442,7 @@ static inline zig_bool zig_subo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_
441442#endif
442443}
443444
444zig_extern_c zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
445zig_extern zig_i32 __subosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
445446static inline zig_bool zig_subo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
446447#if zig_has_builtin(sub_overflow)
447448 zig_i32 full_res;
......@@ -467,7 +468,7 @@ static inline zig_bool zig_subo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_
467468#endif
468469}
469470
470zig_extern_c zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
471zig_extern zig_i64 __subodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
471472static inline zig_bool zig_subo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
472473#if zig_has_builtin(sub_overflow)
473474 zig_i64 full_res;
......@@ -537,7 +538,7 @@ static inline zig_bool zig_mulo_u32(zig_u32 *res, zig_u32 lhs, zig_u32 rhs, zig_
537538#endif
538539}
539540
540zig_extern_c zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
541zig_extern zig_i32 __mulosi4(zig_i32 lhs, zig_i32 rhs, zig_c_int *overflow);
541542static inline zig_bool zig_mulo_i32(zig_i32 *res, zig_i32 lhs, zig_i32 rhs, zig_u8 bits) {
542543#if zig_has_builtin(mul_overflow)
543544 zig_i32 full_res;
......@@ -563,7 +564,7 @@ static inline zig_bool zig_mulo_u64(zig_u64 *res, zig_u64 lhs, zig_u64 rhs, zig_
563564#endif
564565}
565566
566zig_extern_c zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
567zig_extern zig_i64 __mulodi4(zig_i64 lhs, zig_i64 rhs, zig_c_int *overflow);
567568static inline zig_bool zig_mulo_i64(zig_i64 *res, zig_i64 lhs, zig_i64 rhs, zig_u8 bits) {
568569#if zig_has_builtin(mul_overflow)
569570 zig_i64 full_res;
......@@ -1210,7 +1211,7 @@ static inline zig_bool zig_addo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs,
12101211#endif
12111212}
12121213
1213zig_extern_c zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1214zig_extern zig_i128 __addoti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
12141215static inline zig_bool zig_addo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
12151216#if zig_has_builtin(add_overflow)
12161217 zig_i128 full_res;
......@@ -1236,7 +1237,7 @@ static inline zig_bool zig_subo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs,
12361237#endif
12371238}
12381239
1239zig_extern_c zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1240zig_extern zig_i128 __suboti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
12401241static inline zig_bool zig_subo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
12411242#if zig_has_builtin(sub_overflow)
12421243 zig_i128 full_res;
......@@ -1262,7 +1263,7 @@ static inline zig_bool zig_mulo_u128(zig_u128 *res, zig_u128 lhs, zig_u128 rhs,
12621263#endif
12631264}
12641265
1265zig_extern_c zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
1266zig_extern zig_i128 __muloti4(zig_i128 lhs, zig_i128 rhs, zig_c_int *overflow);
12661267static inline zig_bool zig_mulo_i128(zig_i128 *res, zig_i128 lhs, zig_i128 rhs, zig_u8 bits) {
12671268#if zig_has_builtin(mul_overflow)
12681269 zig_i128 full_res;
......@@ -1552,7 +1553,7 @@ typedef long double zig_c_longdouble;
15521553#define zig_as_special_c_longdouble(sign, name, arg, repr) sign __builtin_##name##l(arg)
15531554
15541555#define zig_convert_builtin(ResType, operation, ArgType, version) \
1555 zig_extern_c zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
1556 zig_extern zig_##ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
15561557 zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(zig_##ArgType);
15571558zig_convert_builtin(f16, trunc, f32, 2)
15581559zig_convert_builtin(f16, trunc, f64, 2)
......@@ -1585,7 +1586,7 @@ zig_convert_builtin(f128, extend, f80, 2)
15851586 }
15861587
15871588#define zig_float_less_builtin_0(Type, operation) \
1588 zig_extern_c zig_i8 zig_expand_concat(zig_expand_concat(__##operation, \
1589 zig_extern zig_i8 zig_expand_concat(zig_expand_concat(__##operation, \
15891590 zig_compiler_rt_abbrev_##Type), 2)(zig_##Type, zig_##Type); \
15901591 static inline zig_i8 zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
15911592 return (zig_i8)zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 2)(lhs, rhs); \
......@@ -1603,7 +1604,7 @@ zig_convert_builtin(f128, extend, f80, 2)
16031604 }
16041605
16051606#define zig_float_binary_builtin_0(Type, operation, operator) \
1606 zig_extern_c zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
1607 zig_extern zig_##Type zig_expand_concat(zig_expand_concat(__##operation, \
16071608 zig_compiler_rt_abbrev_##Type), 3)(zig_##Type, zig_##Type); \
16081609 static inline zig_##Type zig_##operation##_##Type(zig_##Type lhs, zig_##Type rhs) { \
16091610 return zig_expand_concat(zig_expand_concat(__##operation, zig_compiler_rt_abbrev_##Type), 3)(lhs, rhs); \
......@@ -1638,24 +1639,24 @@ zig_convert_builtin(f128, extend, f80, 2)
16381639 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, sub, -) \
16391640 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, mul, *) \
16401641 zig_expand_concat(zig_float_binary_builtin_, zig_has_##Type)(Type, div, /) \
1641 zig_extern_c zig_##Type zig_libc_name_##Type(sqrt)(zig_##Type); \
1642 zig_extern_c zig_##Type zig_libc_name_##Type(sin)(zig_##Type); \
1643 zig_extern_c zig_##Type zig_libc_name_##Type(cos)(zig_##Type); \
1644 zig_extern_c zig_##Type zig_libc_name_##Type(tan)(zig_##Type); \
1645 zig_extern_c zig_##Type zig_libc_name_##Type(exp)(zig_##Type); \
1646 zig_extern_c zig_##Type zig_libc_name_##Type(exp2)(zig_##Type); \
1647 zig_extern_c zig_##Type zig_libc_name_##Type(log)(zig_##Type); \
1648 zig_extern_c zig_##Type zig_libc_name_##Type(log2)(zig_##Type); \
1649 zig_extern_c zig_##Type zig_libc_name_##Type(log10)(zig_##Type); \
1650 zig_extern_c zig_##Type zig_libc_name_##Type(fabs)(zig_##Type); \
1651 zig_extern_c zig_##Type zig_libc_name_##Type(floor)(zig_##Type); \
1652 zig_extern_c zig_##Type zig_libc_name_##Type(ceil)(zig_##Type); \
1653 zig_extern_c zig_##Type zig_libc_name_##Type(round)(zig_##Type); \
1654 zig_extern_c zig_##Type zig_libc_name_##Type(trunc)(zig_##Type); \
1655 zig_extern_c zig_##Type zig_libc_name_##Type(fmod)(zig_##Type, zig_##Type); \
1656 zig_extern_c zig_##Type zig_libc_name_##Type(fmin)(zig_##Type, zig_##Type); \
1657 zig_extern_c zig_##Type zig_libc_name_##Type(fmax)(zig_##Type, zig_##Type); \
1658 zig_extern_c zig_##Type zig_libc_name_##Type(fma)(zig_##Type, zig_##Type, zig_##Type); \
1642 zig_extern zig_##Type zig_libc_name_##Type(sqrt)(zig_##Type); \
1643 zig_extern zig_##Type zig_libc_name_##Type(sin)(zig_##Type); \
1644 zig_extern zig_##Type zig_libc_name_##Type(cos)(zig_##Type); \
1645 zig_extern zig_##Type zig_libc_name_##Type(tan)(zig_##Type); \
1646 zig_extern zig_##Type zig_libc_name_##Type(exp)(zig_##Type); \
1647 zig_extern zig_##Type zig_libc_name_##Type(exp2)(zig_##Type); \
1648 zig_extern zig_##Type zig_libc_name_##Type(log)(zig_##Type); \
1649 zig_extern zig_##Type zig_libc_name_##Type(log2)(zig_##Type); \
1650 zig_extern zig_##Type zig_libc_name_##Type(log10)(zig_##Type); \
1651 zig_extern zig_##Type zig_libc_name_##Type(fabs)(zig_##Type); \
1652 zig_extern zig_##Type zig_libc_name_##Type(floor)(zig_##Type); \
1653 zig_extern zig_##Type zig_libc_name_##Type(ceil)(zig_##Type); \
1654 zig_extern zig_##Type zig_libc_name_##Type(round)(zig_##Type); \
1655 zig_extern zig_##Type zig_libc_name_##Type(trunc)(zig_##Type); \
1656 zig_extern zig_##Type zig_libc_name_##Type(fmod)(zig_##Type, zig_##Type); \
1657 zig_extern zig_##Type zig_libc_name_##Type(fmin)(zig_##Type, zig_##Type); \
1658 zig_extern zig_##Type zig_libc_name_##Type(fmax)(zig_##Type, zig_##Type); \
1659 zig_extern zig_##Type zig_libc_name_##Type(fma)(zig_##Type, zig_##Type, zig_##Type); \
16591660\
16601661 static inline zig_##Type zig_div_trunc_##Type(zig_##Type lhs, zig_##Type rhs) { \
16611662 return zig_libc_name_##Type(trunc)(zig_div_##Type(lhs, rhs)); \
src/codegen/c.zig+29-17
......@@ -704,9 +704,13 @@ pub const DeclGen = struct {
704704
705705 try writer.writeByte('{');
706706 if (ty.unionTagTypeSafety()) |tag_ty| {
707 try writer.writeAll(" .tag = ");
708 try dg.renderValue(writer, tag_ty, val, .Initializer);
709 try writer.writeAll(", .payload = {");
707 const layout = ty.unionGetLayout(target);
708 if (layout.tag_size != 0) {
709 try writer.writeAll(" .tag = ");
710 try dg.renderValue(writer, tag_ty, val, .Initializer);
711 try writer.writeByte(',');
712 }
713 try writer.writeAll(" .payload = {");
710714 }
711715 for (ty.unionFields().values()) |field| {
712716 if (!field.ty.hasRuntimeBits()) continue;
......@@ -1115,7 +1119,6 @@ pub const DeclGen = struct {
11151119 },
11161120 .Union => {
11171121 const union_obj = val.castTag(.@"union").?.data;
1118 const layout = ty.unionGetLayout(target);
11191122
11201123 if (location != .Initializer) {
11211124 try writer.writeByte('(');
......@@ -1125,6 +1128,7 @@ pub const DeclGen = struct {
11251128
11261129 try writer.writeByte('{');
11271130 if (ty.unionTagTypeSafety()) |tag_ty| {
1131 const layout = ty.unionGetLayout(target);
11281132 if (layout.tag_size != 0) {
11291133 try writer.writeAll(".tag = ");
11301134 try dg.renderValue(writer, tag_ty, union_obj.tag, .Initializer);
......@@ -2215,7 +2219,7 @@ pub fn genFunc(f: *Function) !void {
22152219
22162220 const is_global = o.dg.module.decl_exports.contains(f.func.owner_decl);
22172221 const fwd_decl_writer = o.dg.fwd_decl.writer();
2218 try fwd_decl_writer.writeAll(if (is_global) "zig_extern_c " else "static ");
2222 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
22192223 try o.dg.renderFunctionSignature(fwd_decl_writer, .Forward);
22202224 try fwd_decl_writer.writeAll(";\n");
22212225
......@@ -2252,9 +2256,10 @@ pub fn genDecl(o: *Object) !void {
22522256 .ty = o.dg.decl.ty,
22532257 .val = o.dg.decl.val,
22542258 };
2259 if (!tv.ty.isFnOrHasRuntimeBitsIgnoreComptime()) return;
22552260 if (tv.val.tag() == .extern_fn) {
22562261 const fwd_decl_writer = o.dg.fwd_decl.writer();
2257 try fwd_decl_writer.writeAll("zig_extern_c ");
2262 try fwd_decl_writer.writeAll("zig_extern ");
22582263 try o.dg.renderFunctionSignature(fwd_decl_writer, .Forward);
22592264 try fwd_decl_writer.writeAll(";\n");
22602265 } else if (tv.val.castTag(.variable)) |var_payload| {
......@@ -2268,22 +2273,19 @@ pub fn genDecl(o: *Object) !void {
22682273 .decl = o.dg.decl_index,
22692274 };
22702275
2271 if (is_global) try fwd_decl_writer.writeAll("zig_extern_c ");
2276 try fwd_decl_writer.writeAll(if (is_global) "zig_extern " else "static ");
22722277 if (variable.is_threadlocal) try fwd_decl_writer.writeAll("zig_threadlocal ");
22732278 try o.dg.renderTypeAndName(fwd_decl_writer, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align", .Complete);
22742279 try fwd_decl_writer.writeAll(";\n");
22752280
2276 if (variable.is_extern or variable.init.isUndefDeep()) {
2277 return;
2278 }
2281 if (variable.is_extern) return;
22792282
22802283 const w = o.writer();
2284 if (!is_global) try w.writeAll("static ");
22812285 if (variable.is_threadlocal) try w.writeAll("zig_threadlocal ");
22822286 try o.dg.renderTypeAndName(w, o.dg.decl.ty, decl_c_value, .Mut, o.dg.decl.@"align", .Complete);
22832287 try w.writeAll(" = ");
2284 if (variable.init.tag() != .unreachable_value) {
2285 try o.dg.renderValue(w, tv.ty, variable.init, .Initializer);
2286 }
2288 try o.dg.renderValue(w, tv.ty, variable.init, .Initializer);
22872289 try w.writeByte(';');
22882290 try o.indent_writer.insertNewline();
22892291 } else {
......@@ -2319,7 +2321,7 @@ pub fn genHeader(dg: *DeclGen) error{ AnalysisFail, OutOfMemory }!void {
23192321 .Fn => {
23202322 const is_global = dg.declIsGlobal(tv);
23212323 if (is_global) {
2322 try writer.writeAll("zig_extern_c ");
2324 try writer.writeAll("zig_extern ");
23232325 try dg.renderFunctionSignature(writer, .Complete);
23242326 try dg.fwd_decl.appendSlice(";\n");
23252327 }
......@@ -2432,7 +2434,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
24322434 .bool_or, .bit_or => try airBinOp(f, inst, "|", "or", .None),
24332435 .xor => try airBinOp(f, inst, "^", "xor", .None),
24342436 .shr, .shr_exact => try airBinBuiltinCall(f, inst, "shr", .None),
2435 .shl, => try airBinBuiltinCall(f, inst, "shl", .None),
2437 .shl, => try airBinBuiltinCall(f, inst, "shlw", .Bits),
24362438 .shl_exact => try airBinOp(f, inst, "<<", "shl", .None),
24372439 .not => try airNot (f, inst),
24382440
......@@ -3701,7 +3703,6 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
37013703 const local = try f.allocLocal(inst_ty, .Const);
37023704 try writer.writeAll(" = (");
37033705 try f.renderTypecast(writer, inst_ty);
3704
37053706 try writer.writeByte(')');
37063707 try f.writeCValue(writer, operand, .Other);
37073708 try writer.writeAll(";\n");
......@@ -3719,6 +3720,17 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue {
37193720 try f.renderTypecast(writer, inst_ty);
37203721 try writer.writeAll("));\n");
37213722
3723 // Ensure padding bits have the expected value.
3724 if (inst_ty.isAbiInt()) {
3725 try f.writeCValue(writer, local, .Other);
3726 try writer.writeAll(" = zig_wrap_");
3727 try f.object.dg.renderTypeForBuiltinFnName(writer, inst_ty);
3728 try writer.writeByte('(');
3729 try f.writeCValue(writer, local, .Other);
3730 try f.object.dg.renderBuiltinInfo(writer, inst_ty, .Bits);
3731 try writer.writeAll(");\n");
3732 }
3733
37223734 return local;
37233735}
37243736
......@@ -5307,7 +5319,6 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
53075319 const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data;
53085320 const union_ty = f.air.typeOfIndex(inst);
53095321 const target = f.object.dg.module.getTarget();
5310 const layout = union_ty.unionGetLayout(target);
53115322 const union_obj = union_ty.cast(Type.Payload.Union).?.data;
53125323 const field_name = union_obj.fields.keys()[extra.field_index];
53135324 const payload = try f.resolveInst(extra.init);
......@@ -5316,6 +5327,7 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue {
53165327 const local = try f.allocLocal(union_ty, .Const);
53175328 try writer.writeAll(" = {");
53185329 if (union_ty.unionTagTypeSafety()) |tag_ty| {
5330 const layout = union_ty.unionGetLayout(target);
53195331 if (layout.tag_size != 0) {
53205332 const field_index = tag_ty.enumFieldIndex(field_name).?;
53215333
test/stage2/cbe.zig+11-11
......@@ -951,7 +951,7 @@ pub fn addCases(ctx: *TestContext) !void {
951951 ctx.h("simple header", linux_x64,
952952 \\export fn start() void{}
953953 ,
954 \\zig_extern_c zig_void start(zig_void);
954 \\zig_extern zig_void start(zig_void);
955955 \\
956956 );
957957 ctx.h("header with single param function", linux_x64,
......@@ -959,7 +959,7 @@ pub fn addCases(ctx: *TestContext) !void {
959959 \\ _ = a;
960960 \\}
961961 ,
962 \\zig_extern_c zig_void start(zig_u8 const a0);
962 \\zig_extern zig_void start(zig_u8 const a0);
963963 \\
964964 );
965965 ctx.h("header with multiple param function", linux_x64,
......@@ -967,25 +967,25 @@ pub fn addCases(ctx: *TestContext) !void {
967967 \\ _ = a; _ = b; _ = c;
968968 \\}
969969 ,
970 \\zig_extern_c zig_void start(zig_u8 const a0, zig_u8 const a1, zig_u8 const a2);
970 \\zig_extern zig_void start(zig_u8 const a0, zig_u8 const a1, zig_u8 const a2);
971971 \\
972972 );
973973 ctx.h("header with u32 param function", linux_x64,
974974 \\export fn start(a: u32) void{ _ = a; }
975975 ,
976 \\zig_extern_c zig_void start(zig_u32 const a0);
976 \\zig_extern zig_void start(zig_u32 const a0);
977977 \\
978978 );
979979 ctx.h("header with usize param function", linux_x64,
980980 \\export fn start(a: usize) void{ _ = a; }
981981 ,
982 \\zig_extern_c zig_void start(zig_usize const a0);
982 \\zig_extern zig_void start(zig_usize const a0);
983983 \\
984984 );
985985 ctx.h("header with bool param function", linux_x64,
986986 \\export fn start(a: bool) void{_ = a;}
987987 ,
988 \\zig_extern_c zig_void start(zig_bool const a0);
988 \\zig_extern zig_void start(zig_bool const a0);
989989 \\
990990 );
991991 ctx.h("header with noreturn function", linux_x64,
......@@ -993,7 +993,7 @@ pub fn addCases(ctx: *TestContext) !void {
993993 \\ unreachable;
994994 \\}
995995 ,
996 \\zig_extern_c zig_noreturn start(zig_void);
996 \\zig_extern zig_noreturn start(zig_void);
997997 \\
998998 );
999999 ctx.h("header with multiple functions", linux_x64,
......@@ -1001,15 +1001,15 @@ pub fn addCases(ctx: *TestContext) !void {
10011001 \\export fn b() void{}
10021002 \\export fn c() void{}
10031003 ,
1004 \\zig_extern_c zig_void a(zig_void);
1005 \\zig_extern_c zig_void b(zig_void);
1006 \\zig_extern_c zig_void c(zig_void);
1004 \\zig_extern zig_void a(zig_void);
1005 \\zig_extern zig_void b(zig_void);
1006 \\zig_extern zig_void c(zig_void);
10071007 \\
10081008 );
10091009 ctx.h("header with multiple includes", linux_x64,
10101010 \\export fn start(a: u32, b: usize) void{ _ = a; _ = b; }
10111011 ,
1012 \\zig_extern_c zig_void start(zig_u32 const a0, zig_usize const a1);
1012 \\zig_extern zig_void start(zig_u32 const a0, zig_usize const a1);
10131013 \\
10141014 );
10151015}