From 300fceac6eca99fd858678b03a47357e72856e10 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 10 Mar 2020 20:54:05 +0100 Subject: ir: Implement more safety checks for shl/shr The checks are now valid on types whose size is not a power of two. Closes #2096 --- src/ir.cpp | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index e5b28f84c2..b9b42141bc 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16648,36 +16648,34 @@ static IrInstGen *ir_analyze_bit_shift(IrAnalyze *ira, IrInstSrcBinOp *bin_op_in return ira->codegen->invalid_inst_gen; } } else { + assert(op1->value->type->data.integral.bit_count > 0); ZigType *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen, - op1->value->type->data.integral.bit_count - 1); - if (bin_op_instruction->op_id == IrBinOpBitShiftLeftLossy && - op2->value->type->id == ZigTypeIdComptimeInt) { + op1->value->type->data.integral.bit_count - 1); - ZigValue *op2_val = ir_resolve_const(ira, op2, UndefBad); + casted_op2 = ir_implicit_cast(ira, op2, shift_amt_type); + if (type_is_invalid(casted_op2->value->type)) + return ira->codegen->invalid_inst_gen; + + if (instr_is_comptime(casted_op2)) { + ZigValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad); if (op2_val == nullptr) return ira->codegen->invalid_inst_gen; - if (!bigint_fits_in_bits(&op2_val->data.x_bigint, - shift_amt_type->data.integral.bit_count, - op2_val->data.x_bigint.is_negative)) { - Buf *val_buf = buf_alloc(); - bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10); + + BigInt bit_count_value = {0}; + bigint_init_unsigned(&bit_count_value, op1->value->type->data.integral.bit_count); + + if (bigint_cmp(&op2_val->data.x_bigint, &bit_count_value) != CmpLT) { ErrorMsg* msg = ir_add_error(ira, &bin_op_instruction->base.base, buf_sprintf("RHS of shift is too large for LHS type")); - add_error_note( - ira->codegen, - msg, - op2->base.source_node, - buf_sprintf("value %s cannot fit into type %s", - buf_ptr(val_buf), - buf_ptr(&shift_amt_type->name))); + add_error_note(ira->codegen, msg, op1->base.source_node, + buf_sprintf("type %s has only %u bits", + buf_ptr(&op1->value->type->name), + op1->value->type->data.integral.bit_count)); + return ira->codegen->invalid_inst_gen; } } - - casted_op2 = ir_implicit_cast(ira, op2, shift_amt_type); - if (type_is_invalid(casted_op2->value->type)) - return ira->codegen->invalid_inst_gen; } if (instr_is_comptime(op1) && instr_is_comptime(casted_op2)) { -- cgit v1.2.3 From 4ab13a359dfb14c93869e7f88320ec2aa438da9c Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Tue, 10 Mar 2020 23:04:49 +0100 Subject: ir: Fix shift code for u0 operands --- src/ir.cpp | 46 +++++++++++++++++++++++++++++-------------- test/stage1/behavior/math.zig | 19 ++++++++++++++++++ 2 files changed, 50 insertions(+), 15 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index b9b42141bc..bb2dc75c64 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16635,34 +16635,47 @@ static IrInstGen *ir_analyze_bit_shift(IrAnalyze *ira, IrInstSrcBinOp *bin_op_in IrInstGen *casted_op2; IrBinOp op_id = bin_op_instruction->op_id; if (op1->value->type->id == ZigTypeIdComptimeInt) { + // comptime_int has no finite bit width casted_op2 = op2; if (op_id == IrBinOpBitShiftLeftLossy) { op_id = IrBinOpBitShiftLeftExact; } - if (casted_op2->value->data.x_bigint.is_negative) { + if (!instr_is_comptime(op2)) { + ir_add_error(ira, &bin_op_instruction->base.base, + buf_sprintf("LHS of shift must be an integer type, or RHS must be compile-time known")); + return ira->codegen->invalid_inst_gen; + } + + ZigValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad); + if (op2_val == nullptr) + return ira->codegen->invalid_inst_gen; + + if (op2_val->data.x_bigint.is_negative) { Buf *val_buf = buf_alloc(); - bigint_append_buf(val_buf, &casted_op2->value->data.x_bigint, 10); - ir_add_error(ira, &casted_op2->base, buf_sprintf("shift by negative value %s", buf_ptr(val_buf))); + bigint_append_buf(val_buf, &op2_val->data.x_bigint, 10); + ir_add_error(ira, &casted_op2->base, + buf_sprintf("shift by negative value %s", buf_ptr(val_buf))); return ira->codegen->invalid_inst_gen; } } else { - assert(op1->value->type->data.integral.bit_count > 0); + const unsigned bit_count = op1->value->type->data.integral.bit_count; ZigType *shift_amt_type = get_smallest_unsigned_int_type(ira->codegen, - op1->value->type->data.integral.bit_count - 1); + bit_count > 0 ? bit_count - 1 : 0); casted_op2 = ir_implicit_cast(ira, op2, shift_amt_type); if (type_is_invalid(casted_op2->value->type)) return ira->codegen->invalid_inst_gen; - if (instr_is_comptime(casted_op2)) { + // This check is only valid iff op1 has at least one bit + if (bit_count > 0 && instr_is_comptime(casted_op2)) { ZigValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad); if (op2_val == nullptr) return ira->codegen->invalid_inst_gen; BigInt bit_count_value = {0}; - bigint_init_unsigned(&bit_count_value, op1->value->type->data.integral.bit_count); + bigint_init_unsigned(&bit_count_value, bit_count); if (bigint_cmp(&op2_val->data.x_bigint, &bit_count_value) != CmpLT) { ErrorMsg* msg = ir_add_error(ira, @@ -16670,14 +16683,23 @@ static IrInstGen *ir_analyze_bit_shift(IrAnalyze *ira, IrInstSrcBinOp *bin_op_in buf_sprintf("RHS of shift is too large for LHS type")); add_error_note(ira->codegen, msg, op1->base.source_node, buf_sprintf("type %s has only %u bits", - buf_ptr(&op1->value->type->name), - op1->value->type->data.integral.bit_count)); + buf_ptr(&op1->value->type->name), bit_count)); return ira->codegen->invalid_inst_gen; } } } + // Fast path for zero RHS + if (instr_is_comptime(casted_op2)) { + ZigValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad); + if (op2_val == nullptr) + return ira->codegen->invalid_inst_gen; + + if (bigint_cmp_zero(&op2_val->data.x_bigint) == CmpEQ) + return ir_analyze_cast(ira, &bin_op_instruction->base.base, op1->value->type, op1); + } + if (instr_is_comptime(op1) && instr_is_comptime(casted_op2)) { ZigValue *op1_val = ir_resolve_const(ira, op1, UndefBad); if (op1_val == nullptr) @@ -16688,12 +16710,6 @@ static IrInstGen *ir_analyze_bit_shift(IrAnalyze *ira, IrInstSrcBinOp *bin_op_in return ira->codegen->invalid_inst_gen; return ir_analyze_math_op(ira, &bin_op_instruction->base.base, op1->value->type, op1_val, op_id, op2_val); - } else if (op1->value->type->id == ZigTypeIdComptimeInt) { - ir_add_error(ira, &bin_op_instruction->base.base, - buf_sprintf("LHS of shift must be an integer type, or RHS must be compile-time known")); - return ira->codegen->invalid_inst_gen; - } else if (instr_is_comptime(casted_op2) && bigint_cmp_zero(&casted_op2->value->data.x_bigint) == CmpEQ) { - return ir_build_cast(ira, &bin_op_instruction->base.base, op1->value->type, op1, CastOpNoop); } return ir_build_bin_op_gen(ira, &bin_op_instruction->base.base, op1->value->type, diff --git a/test/stage1/behavior/math.zig b/test/stage1/behavior/math.zig index e657b5472b..fb70fb7e44 100644 --- a/test/stage1/behavior/math.zig +++ b/test/stage1/behavior/math.zig @@ -453,6 +453,25 @@ fn testShrExact(x: u8) void { expect(shifted == 0b00101101); } +test "shift left/right on u0 operand" { + const S = struct { + fn doTheTest() void { + var x: u0 = 0; + var y: u0 = 0; + expectEqual(@as(u0, 0), x << 0); + expectEqual(@as(u0, 0), x >> 0); + expectEqual(@as(u0, 0), x << y); + expectEqual(@as(u0, 0), x >> y); + expectEqual(@as(u0, 0), @shlExact(x, 0)); + expectEqual(@as(u0, 0), @shrExact(x, 0)); + expectEqual(@as(u0, 0), @shlExact(x, y)); + expectEqual(@as(u0, 0), @shrExact(x, y)); + } + }; + S.doTheTest(); + comptime S.doTheTest(); +} + test "comptime_int addition" { comptime { expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950); -- cgit v1.2.3