authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-14 19:57:42-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-15 01:04:21-04:00
logbb6b9c19e06320d8617f8858dde1ae6a7cf7fc5e
tree4c97b97424677a7edb129b9044e4855ce8946e62
parentc51930b060cf66b21c78b97e53fd71b153a5e60c

x86_64: fix lowering of non-pointer optional is null


1 files changed, 23 insertions(+), 20 deletions(-)

src/arch/x86_64/CodeGen.zig+23-20
......@@ -208,7 +208,7 @@ const Branch = struct {
208208};
209209
210210const StackAllocation = struct {
211 inst: Air.Inst.Index,
211 inst: ?Air.Inst.Index,
212212 /// TODO do we need size? should be determined by inst.ty.abiSize(self.target.*)
213213 size: u32,
214214};
......@@ -1109,7 +1109,7 @@ fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void {
11091109 try table.ensureUnusedCapacity(self.gpa, additional_count);
11101110}
11111111
1112fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: u32) !u32 {
1112fn allocMem(self: *Self, inst: ?Air.Inst.Index, abi_size: u32, abi_align: u32) !u32 {
11131113 if (abi_align > self.stack_align)
11141114 self.stack_align = abi_align;
11151115 // TODO find a free slot instead of always appending
......@@ -1142,7 +1142,14 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 {
11421142}
11431143
11441144fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue {
1145 const elem_ty = self.air.typeOfIndex(inst);
1145 return self.allocRegOrMemAdvanced(self.air.typeOfIndex(inst), inst, reg_ok);
1146}
1147
1148fn allocTempRegOrMem(self: *Self, elem_ty: Type, reg_ok: bool) !MCValue {
1149 return self.allocRegOrMemAdvanced(elem_ty, null, reg_ok);
1150}
1151
1152fn allocRegOrMemAdvanced(self: *Self, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue {
11461153 const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) orelse {
11471154 const mod = self.bin_file.options.module.?;
11481155 return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)});
......@@ -4571,18 +4578,16 @@ fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void {
45714578 };
45724579 defer if (operand_ptr_lock) |lock| self.register_manager.unlockReg(lock);
45734580
4574 const operand: MCValue = blk: {
4575 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
4576 // The MCValue that holds the pointer can be re-used as the value.
4577 break :blk operand_ptr;
4578 } else {
4579 break :blk try self.allocRegOrMem(inst, true);
4580 }
4581 };
45824581 const ptr_ty = self.air.typeOf(un_op);
4582 const elem_ty = ptr_ty.childType();
4583 const operand = if (elem_ty.isPtrLikeOptional() and self.reuseOperand(inst, un_op, 0, operand_ptr))
4584 // The MCValue that holds the pointer can be re-used as the value.
4585 operand_ptr
4586 else
4587 try self.allocTempRegOrMem(elem_ty, true);
45834588 try self.load(operand, operand_ptr, ptr_ty);
45844589
4585 const result = try self.isNull(inst, ptr_ty.elemType(), operand);
4590 const result = try self.isNull(inst, elem_ty, operand);
45864591
45874592 return self.finishAir(inst, result, .{ un_op, .none, .none });
45884593}
......@@ -4611,15 +4616,13 @@ fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void {
46114616 };
46124617 defer if (operand_ptr_lock) |lock| self.register_manager.unlockReg(lock);
46134618
4614 const operand: MCValue = blk: {
4615 if (self.reuseOperand(inst, un_op, 0, operand_ptr)) {
4616 // The MCValue that holds the pointer can be re-used as the value.
4617 break :blk operand_ptr;
4618 } else {
4619 break :blk try self.allocRegOrMem(inst, true);
4620 }
4621 };
46224619 const ptr_ty = self.air.typeOf(un_op);
4620 const elem_ty = ptr_ty.childType();
4621 const operand = if (elem_ty.isPtrLikeOptional() and self.reuseOperand(inst, un_op, 0, operand_ptr))
4622 // The MCValue that holds the pointer can be re-used as the value.
4623 operand_ptr
4624 else
4625 try self.allocTempRegOrMem(elem_ty, true);
46234626 try self.load(operand, operand_ptr, ptr_ty);
46244627
46254628 const result = try self.isNonNull(inst, ptr_ty.elemType(), operand);