authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-01-08 13:55:34-05:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2025-01-08 19:33:45-05:00
log3f95003d4c57650f9b4779f55c8d7368b137337c
tree2fbeaef5ca4774b3224d57e0b06e9607d8802248
parent5b5c60f433394c2b703fb8bfb8603395e66b0c5a

cbe: fix miscomps of x86_64 backend


3 files changed, 83 insertions(+), 19 deletions(-)

src/codegen/c.zig+67-19
......@@ -105,7 +105,7 @@ pub const CValue = union(enum) {
105105};
106106
107107const BlockData = struct {
108 block_id: usize,
108 block_id: u32,
109109 result: CValue,
110110};
111111
......@@ -359,8 +359,8 @@ pub const Function = struct {
359359 liveness: Liveness,
360360 value_map: CValueMap,
361361 blocks: std.AutoHashMapUnmanaged(Air.Inst.Index, BlockData) = .empty,
362 next_arg_index: usize = 0,
363 next_block_index: usize = 0,
362 next_arg_index: u32 = 0,
363 next_block_index: u32 = 0,
364364 object: Object,
365365 lazy_fns: LazyFnMap,
366366 func_index: InternPool.Index,
......@@ -663,6 +663,7 @@ pub const DeclGen = struct {
663663 mod: *Module,
664664 pass: Pass,
665665 is_naked_fn: bool,
666 expected_block: ?u32,
666667 /// This is a borrowed reference from `link.C`.
667668 fwd_decl: std.ArrayList(u8),
668669 error_msg: ?*Zcu.ErrorMsg,
......@@ -1399,12 +1400,24 @@ pub const DeclGen = struct {
13991400 .repeated_elem => |elem| elem,
14001401 };
14011402
1403 const field_int_info: std.builtin.Type.Int = if (field_ty.isAbiInt(zcu))
1404 field_ty.intInfo(zcu)
1405 else
1406 .{ .signedness = .unsigned, .bits = undefined };
1407 switch (field_int_info.signedness) {
1408 .signed => {
1409 try writer.writeByte('(');
1410 try dg.renderValue(writer, Value.fromInterned(field_val), .Other);
1411 try writer.writeAll(" & ");
1412 const field_uint_ty = try pt.intType(.unsigned, field_int_info.bits);
1413 try dg.renderValue(writer, try field_uint_ty.maxIntScalar(pt, field_uint_ty), .Other);
1414 try writer.writeByte(')');
1415 },
1416 .unsigned => try dg.renderValue(writer, Value.fromInterned(field_val), .Other),
1417 }
14021418 if (bit_offset != 0) {
1403 try dg.renderValue(writer, Value.fromInterned(field_val), .Other);
14041419 try writer.writeAll(" << ");
14051420 try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument);
1406 } else {
1407 try dg.renderValue(writer, Value.fromInterned(field_val), .Other);
14081421 }
14091422
14101423 bit_offset += field_ty.bitSize(zcu);
......@@ -2899,6 +2912,8 @@ pub fn genFunc(f: *Function) !void {
28992912 const main_body = f.air.getMainBody();
29002913 try genBodyResolveState(f, undefined, &.{}, main_body, false);
29012914 try o.indent_writer.insertNewline();
2915 if (o.dg.expected_block) |_|
2916 return f.fail("runtime code not allowed in naked function", .{});
29022917
29032918 // Take advantage of the free_locals map to bucket locals per type. All
29042919 // locals corresponding to AIR instructions should be in there due to
......@@ -3189,6 +3204,8 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
31893204 const air_datas = f.air.instructions.items(.data);
31903205
31913206 for (body) |inst| {
3207 if (f.object.dg.expected_block) |_|
3208 return f.fail("runtime code not allowed in naked function", .{});
31923209 if (f.liveness.isUnused(inst) and !f.air.mustLower(inst, ip))
31933210 continue;
31943211
......@@ -4517,6 +4534,7 @@ fn airCall(
45174534) !CValue {
45184535 const pt = f.object.dg.pt;
45194536 const zcu = pt.zcu;
4537 const ip = &zcu.intern_pool;
45204538 // Not even allowed to call panic in a naked function.
45214539 if (f.object.dg.is_naked_fn) return .none;
45224540
......@@ -4562,11 +4580,12 @@ fn airCall(
45624580 }
45634581
45644582 const callee_ty = f.typeOf(pl_op.operand);
4565 const fn_info = zcu.typeToFunc(switch (callee_ty.zigTypeTag(zcu)) {
4566 .@"fn" => callee_ty,
4567 .pointer => callee_ty.childType(zcu),
4583 const callee_is_ptr = switch (callee_ty.zigTypeTag(zcu)) {
4584 .@"fn" => false,
4585 .pointer => true,
45684586 else => unreachable,
4569 }).?;
4587 };
4588 const fn_info = zcu.typeToFunc(if (callee_is_ptr) callee_ty.childType(zcu) else callee_ty).?;
45704589 const ret_ty = Type.fromInterned(fn_info.return_type);
45714590 const ret_ctype: CType = if (ret_ty.isNoReturn(zcu))
45724591 CType.void
......@@ -4598,20 +4617,29 @@ fn airCall(
45984617 callee: {
45994618 known: {
46004619 const callee_val = (try f.air.value(pl_op.operand, pt)) orelse break :known;
4601 const fn_nav = switch (zcu.intern_pool.indexToKey(callee_val.toIntern())) {
4602 .@"extern" => |@"extern"| @"extern".owner_nav,
4603 .func => |func| func.owner_nav,
4620 const fn_nav, const need_cast = switch (ip.indexToKey(callee_val.toIntern())) {
4621 .@"extern" => |@"extern"| .{ @"extern".owner_nav, false },
4622 .func => |func| .{ func.owner_nav, Type.fromInterned(func.ty).fnCallingConvention(zcu) != .naked and
4623 Type.fromInterned(func.uncoerced_ty).fnCallingConvention(zcu) == .naked },
46044624 .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) {
4605 .nav => |nav| nav,
4625 .nav => |nav| .{ nav, Type.fromInterned(ptr.ty).childType(zcu).fnCallingConvention(zcu) != .naked and
4626 zcu.navValue(nav).typeOf(zcu).fnCallingConvention(zcu) == .naked },
46064627 else => break :known,
46074628 } else break :known,
46084629 else => break :known,
46094630 };
4631 if (need_cast) {
4632 try writer.writeAll("((");
4633 try f.renderType(writer, if (callee_is_ptr) callee_ty else try pt.singleConstPtrType(callee_ty));
4634 try writer.writeByte(')');
4635 if (!callee_is_ptr) try writer.writeByte('&');
4636 }
46104637 switch (modifier) {
46114638 .auto, .always_tail => try f.object.dg.renderNavName(writer, fn_nav),
46124639 inline .never_tail, .never_inline => |m| try writer.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
46134640 else => unreachable,
46144641 }
4642 if (need_cast) try writer.writeByte(')');
46154643 break :callee;
46164644 }
46174645 switch (modifier) {
......@@ -4712,7 +4740,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
47124740 const zcu = pt.zcu;
47134741 const liveness_block = f.liveness.getBlock(inst);
47144742
4715 const block_id: usize = f.next_block_index;
4743 const block_id = f.next_block_index;
47164744 f.next_block_index += 1;
47174745 const writer = f.object.writer();
47184746
......@@ -4739,7 +4767,13 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index)
47394767 try f.object.indent_writer.insertNewline();
47404768
47414769 // noreturn blocks have no `br` instructions reaching them, so we don't want a label
4742 if (!f.typeOfIndex(inst).isNoReturn(zcu)) {
4770 if (f.object.dg.is_naked_fn) {
4771 if (f.object.dg.expected_block) |expected_block| {
4772 if (block_id != expected_block)
4773 return f.fail("runtime code not allowed in naked function", .{});
4774 f.object.dg.expected_block = null;
4775 }
4776 } else if (!f.typeOfIndex(inst).isNoReturn(zcu)) {
47434777 // label must be followed by an expression, include an empty one.
47444778 try writer.print("zig_block_{d}:;\n", .{block_id});
47454779 }
......@@ -4803,6 +4837,8 @@ fn lowerTry(
48034837
48044838 try genBodyResolveState(f, inst, liveness_condbr.else_deaths, body, false);
48054839 try f.object.indent_writer.insertNewline();
4840 if (f.object.dg.expected_block) |_|
4841 return f.fail("runtime code not allowed in naked function", .{});
48064842 }
48074843
48084844 // Now we have the "then branch" (in terms of the liveness data); process any deaths.
......@@ -4820,9 +4856,7 @@ fn lowerTry(
48204856
48214857 try reap(f, inst, &.{operand});
48224858
4823 if (f.liveness.isUnused(inst)) {
4824 return .none;
4825 }
4859 if (f.liveness.isUnused(inst)) return .none;
48264860
48274861 const local = try f.allocLocal(inst, inst_ty);
48284862 const a = try Assignment.start(f, writer, try f.ctypeFromType(inst_ty, .complete));
......@@ -4842,6 +4876,12 @@ fn airBr(f: *Function, inst: Air.Inst.Index) !void {
48424876 const result = block.result;
48434877 const writer = f.object.writer();
48444878
4879 if (f.object.dg.is_naked_fn) {
4880 if (result != .none) return f.fail("runtime code not allowed in naked function", .{});
4881 f.object.dg.expected_block = block.block_id;
4882 return;
4883 }
4884
48454885 // If result is .none then the value of the block is unused.
48464886 if (result != .none) {
48474887 const operand_ty = f.typeOf(branch.operand);
......@@ -5096,6 +5136,8 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !void {
50965136
50975137 try genBodyResolveState(f, inst, liveness_condbr.then_deaths, then_body, false);
50985138 try writer.writeByte('\n');
5139 if (else_body.len > 0) if (f.object.dg.expected_block) |_|
5140 return f.fail("runtime code not allowed in naked function", .{});
50995141
51005142 // We don't need to use `genBodyResolveState` for the else block, because this instruction is
51015143 // noreturn so must terminate a body, therefore we don't need to leave `value_map` or
......@@ -5193,6 +5235,8 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
51935235 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
51945236 f.object.indent_writer.popIndent();
51955237 try writer.writeByte('}');
5238 if (f.object.dg.expected_block) |_|
5239 return f.fail("runtime code not allowed in naked function", .{});
51965240
51975241 // The case body must be noreturn so we don't need to insert a break.
51985242 }
......@@ -5236,6 +5280,8 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
52365280 try genBodyResolveState(f, inst, liveness.deaths[case.idx], case.body, true);
52375281 f.object.indent_writer.popIndent();
52385282 try writer.writeByte('}');
5283 if (f.object.dg.expected_block) |_|
5284 return f.fail("runtime code not allowed in naked function", .{});
52395285 }
52405286 }
52415287 if (is_dispatch_loop) {
......@@ -5248,6 +5294,8 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index, is_dispatch_loop: bool) !void
52485294 try die(f, inst, death.toRef());
52495295 }
52505296 try genBody(f, else_body);
5297 if (f.object.dg.expected_block) |_|
5298 return f.fail("runtime code not allowed in naked function", .{});
52515299 } else {
52525300 try writer.writeAll("zig_unreachable();");
52535301 }
src/link/C.zig+6
......@@ -218,6 +218,7 @@ pub fn updateFunc(
218218 .error_msg = null,
219219 .pass = .{ .nav = func.owner_nav },
220220 .is_naked_fn = Type.fromInterned(func.ty).fnCallingConvention(zcu) == .naked,
221 .expected_block = null,
221222 .fwd_decl = fwd_decl.toManaged(gpa),
222223 .ctype_pool = ctype_pool.*,
223224 .scratch = .{},
......@@ -272,6 +273,7 @@ fn updateUav(self: *C, pt: Zcu.PerThread, i: usize) !void {
272273 .error_msg = null,
273274 .pass = .{ .uav = uav },
274275 .is_naked_fn = false,
276 .expected_block = null,
275277 .fwd_decl = fwd_decl.toManaged(gpa),
276278 .ctype_pool = codegen.CType.Pool.empty,
277279 .scratch = .{},
......@@ -347,6 +349,7 @@ pub fn updateNav(self: *C, pt: Zcu.PerThread, nav_index: InternPool.Nav.Index) !
347349 .error_msg = null,
348350 .pass = .{ .nav = nav_index },
349351 .is_naked_fn = false,
352 .expected_block = null,
350353 .fwd_decl = fwd_decl.toManaged(gpa),
351354 .ctype_pool = ctype_pool.*,
352355 .scratch = .{},
......@@ -675,6 +678,7 @@ fn flushErrDecls(self: *C, pt: Zcu.PerThread, ctype_pool: *codegen.CType.Pool) F
675678 .error_msg = null,
676679 .pass = .flush,
677680 .is_naked_fn = false,
681 .expected_block = null,
678682 .fwd_decl = fwd_decl.toManaged(gpa),
679683 .ctype_pool = ctype_pool.*,
680684 .scratch = .{},
......@@ -722,6 +726,7 @@ fn flushLazyFn(
722726 .error_msg = null,
723727 .pass = .flush,
724728 .is_naked_fn = false,
729 .expected_block = null,
725730 .fwd_decl = fwd_decl.toManaged(gpa),
726731 .ctype_pool = ctype_pool.*,
727732 .scratch = .{},
......@@ -868,6 +873,7 @@ pub fn updateExports(
868873 .error_msg = null,
869874 .pass = pass,
870875 .is_naked_fn = false,
876 .expected_block = null,
871877 .fwd_decl = fwd_decl.toManaged(gpa),
872878 .ctype_pool = decl_block.ctype_pool,
873879 .scratch = .{},
test/behavior/packed-struct.zig+10
......@@ -1317,3 +1317,13 @@ test "packed struct equality" {
13171317 try S.doTest(x, y);
13181318 comptime try S.doTest(x, y);
13191319}
1320
1321test "packed struct with signed field" {
1322 var s: packed struct {
1323 a: i2,
1324 b: u6,
1325 } = .{ .a = -1, .b = 42 };
1326 s = s;
1327 try expect(s.a == -1);
1328 try expect(s.b == 42);
1329}