aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorriverbl <94326797+riverbl@users.noreply.github.com>2021-12-21 12:45:48 +0000
committerVeikka Tuominen <git@vexu.eu>2022-01-29 18:12:28 +0200
commitaa29f4a8037ae74fb2d97793312ef8c5262d025a (patch)
tree440bd0236e200476ba5292b2d661a6546907fbb4 /src
parentaca665cebd2b6ec9ec3db669cf5446ee45bbb5d0 (diff)
downloadzig-aa29f4a8037ae74fb2d97793312ef8c5262d025a.tar.gz
zig-aa29f4a8037ae74fb2d97793312ef8c5262d025a.zip
stage1: fix saturating arithmetic producing incorrect results on type comptime_int, allow saturating left shift on type comptime int
Diffstat (limited to 'src')
-rw-r--r--src/stage1/ir.cpp38
1 files changed, 23 insertions, 15 deletions
diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp
index 1b9e9638e2..574d3a91a7 100644
--- a/src/stage1/ir.cpp
+++ b/src/stage1/ir.cpp
@@ -10230,13 +10230,7 @@ static Stage1AirInst *ir_analyze_bit_shift(IrAnalyze *ira, Stage1ZirInstBinOp *b
// comptime_int has no finite bit width
casted_op2 = op2;
- if (op_id == IrBinOpShlSat) {
- ir_add_error_node(ira, bin_op_instruction->base.source_node,
- buf_sprintf("saturating shift on a comptime_int which has unlimited bits"));
- return ira->codegen->invalid_inst_gen;
- }
-
- if (op_id == IrBinOpBitShiftLeftLossy) {
+ if (op_id == IrBinOpBitShiftLeftLossy || op_id == IrBinOpShlSat) {
op_id = IrBinOpBitShiftLeftExact;
}
@@ -10398,6 +10392,25 @@ static bool ok_float_op(IrBinOp op) {
zig_unreachable();
}
+static IrBinOp map_comptime_arithmetic_op(IrBinOp op) {
+ switch (op) {
+ case IrBinOpAddWrap:
+ case IrBinOpAddSat:
+ return IrBinOpAdd;
+
+ case IrBinOpSubWrap:
+ case IrBinOpSubSat:
+ return IrBinOpSub;
+
+ case IrBinOpMultWrap:
+ case IrBinOpMultSat:
+ return IrBinOpMult;
+
+ default:
+ return op;
+ }
+}
+
static bool is_pointer_arithmetic_allowed(ZigType *lhs_type, IrBinOp op) {
switch (op) {
case IrBinOpAdd:
@@ -10620,15 +10633,10 @@ static Stage1AirInst *ir_analyze_bin_op_math(IrAnalyze *ira, Stage1ZirInstBinOp
if (type_is_invalid(casted_op2->value->type))
return ira->codegen->invalid_inst_gen;
- // Comptime integers have no fixed size
+ // Comptime integers have no fixed size, so wrapping or saturating operations should be mapped
+ // to their non wrapping or saturating equivalents
if (scalar_type->id == ZigTypeIdComptimeInt) {
- if (op_id == IrBinOpAddWrap) {
- op_id = IrBinOpAdd;
- } else if (op_id == IrBinOpSubWrap) {
- op_id = IrBinOpSub;
- } else if (op_id == IrBinOpMultWrap) {
- op_id = IrBinOpMult;
- }
+ op_id = map_comptime_arithmetic_op(op_id);
}
if (instr_is_comptime(casted_op1) && instr_is_comptime(casted_op2)) {