aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-05-17 01:52:02 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-05-17 23:50:38 -0700
commitb6798c26efc4689cf35c5f4ac0436b4510a1f813 (patch)
tree9507321f623264de9c5bef41ff53c19c88f09bd3 /src/codegen
parent95f5e17e49d32a301d6a9d6f9948719d65469b09 (diff)
downloadzig-b6798c26efc4689cf35c5f4ac0436b4510a1f813.tar.gz
zig-b6798c26efc4689cf35c5f4ac0436b4510a1f813.zip
stage2: fix pointer arithmetic result type
This makes it so the result of doing pointer arithmetic creates a new pointer type that has adjusted alignment.
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig36
-rw-r--r--src/codegen/llvm.zig6
2 files changed, 20 insertions, 22 deletions
diff --git a/src/codegen/c.zig b/src/codegen/c.zig
index 92770168f4..5f61f8586e 100644
--- a/src/codegen/c.zig
+++ b/src/codegen/c.zig
@@ -1711,21 +1711,18 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
.unreach => try airUnreach(f),
.fence => try airFence(f, inst),
- // TODO use a different strategy for add that communicates to the optimizer
- // that wrapping is UB.
- .add => try airBinOp (f, inst, " + "),
- .ptr_add => try airPtrAddSub (f, inst, " + "),
- // TODO use a different strategy for sub that communicates to the optimizer
- // that wrapping is UB.
- .sub => try airBinOp (f, inst, " - "),
- .ptr_sub => try airPtrAddSub (f, inst, " - "),
- // TODO use a different strategy for mul that communicates to the optimizer
- // that wrapping is UB.
- .mul => try airBinOp (f, inst, " * "),
- // TODO use a different strategy for div that communicates to the optimizer
- // that wrapping is UB.
+ .ptr_add => try airPtrAddSub(f, inst, " + "),
+ .ptr_sub => try airPtrAddSub(f, inst, " - "),
+
+ // TODO use a different strategy for add, sub, mul, div
+ // that communicates to the optimizer that wrapping is UB.
+ .add => try airBinOp (f, inst, " + "),
+ .sub => try airBinOp (f, inst, " - "),
+ .mul => try airBinOp (f, inst, " * "),
.div_float, .div_exact => try airBinOp( f, inst, " / "),
- .div_trunc => blk: {
+ .rem => try airBinOp( f, inst, " % "),
+
+ .div_trunc => blk: {
const bin_op = f.air.instructions.items(.data)[inst].bin_op;
const lhs_ty = f.air.typeOf(bin_op.lhs);
// For binary operations @TypeOf(lhs)==@TypeOf(rhs),
@@ -1735,9 +1732,8 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO
else
try airBinOpBuiltinCall(f, inst, "div_trunc");
},
- .div_floor => try airBinOpBuiltinCall(f, inst, "div_floor"),
- .rem => try airBinOp( f, inst, " % "),
- .mod => try airBinOpBuiltinCall(f, inst, "mod"),
+ .div_floor => try airBinOpBuiltinCall(f, inst, "div_floor"),
+ .mod => try airBinOpBuiltinCall(f, inst, "mod"),
.addwrap => try airWrapOp(f, inst, " + ", "addw_"),
.subwrap => try airWrapOp(f, inst, " - ", "subw_"),
@@ -2617,10 +2613,10 @@ fn airEquality(
}
fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: [*:0]const u8) !CValue {
- if (f.liveness.isUnused(inst))
- return CValue.none;
+ if (f.liveness.isUnused(inst)) return CValue.none;
- const bin_op = f.air.instructions.items(.data)[inst].bin_op;
+ const ty_pl = f.air.instructions.items(.data)[inst].ty_pl;
+ const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data;
const lhs = try f.resolveInst(bin_op.lhs);
const rhs = try f.resolveInst(bin_op.rhs);
diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig
index fb7a2c39bc..aba290060a 100644
--- a/src/codegen/llvm.zig
+++ b/src/codegen/llvm.zig
@@ -5679,7 +5679,8 @@ pub const FuncGen = struct {
fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;
- const bin_op = self.air.instructions.items(.data)[inst].bin_op;
+ const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
+ const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
const base_ptr = try self.resolveInst(bin_op.lhs);
const offset = try self.resolveInst(bin_op.rhs);
const ptr_ty = self.air.typeOf(bin_op.lhs);
@@ -5698,7 +5699,8 @@ pub const FuncGen = struct {
fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value {
if (self.liveness.isUnused(inst)) return null;
- const bin_op = self.air.instructions.items(.data)[inst].bin_op;
+ const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
+ const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
const base_ptr = try self.resolveInst(bin_op.lhs);
const offset = try self.resolveInst(bin_op.rhs);
const negative_offset = self.builder.buildNeg(offset, "");