diff options
| author | gingerBill <bill@gingerbill.org> | 2020-05-31 13:50:17 +0100 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2020-05-31 13:50:17 +0100 |
| commit | bf5ce04b24936a5b68223a0bfcbef5cb395c4951 (patch) | |
| tree | 5ae390e08844819906d67e3fd7a860fc30422b81 /src/ir.cpp | |
| parent | 8057af9e0929a7a2c010752212e0c94e923c0744 (diff) | |
Improve rules for shifting behaviour
Example:
x: u64 = 123;
assert(x >> 64 == 0); // In C this would be 123 because (64 & 0b111111) == 0
a: u64 123;
assert(a << 64 == 0); // In C this would be 123 because (64 & 0b111111) == 0
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index f08c3569d..066089211 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -4499,11 +4499,35 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue * handle_op: switch (op) { case Token_Shl: + { + left = ir_emit_conv(proc, left, type); + right = ir_emit_conv(proc, right, type); + ir_emit(proc, ir_instr_binary_op(proc, op, left, right, type)); + + irValue *bits = right; + + irValue *max = ir_value_constant(type, exact_value_i64(8*type_size_of(type) - 1)); + irValue *less_equal_width = ir_emit(proc, ir_instr_binary_op(proc, Token_LtEq, bits, max, t_llvm_bool)); + + + irValue *zero = ir_value_constant(type, exact_value_i64(0)); + irValue *res = ir_emit(proc, ir_instr_binary_op(proc, op, left, bits, type)); + return ir_emit_select(proc, less_equal_width, res, zero); + } case Token_Shr: - left = ir_emit_conv(proc, left, type); - right = ir_emit_conv(proc, right, type); + { + left = ir_emit_conv(proc, left, type); + right = ir_emit_conv(proc, right, type); + bool is_unsigned = is_type_unsigned(ir_type(left)); - break; + irValue *bits = right; + + irValue *max = ir_value_constant(type, exact_value_i64(8*type_size_of(type) - 1)); + irValue *less_equal_width = ir_emit(proc, ir_instr_binary_op(proc, Token_LtEq, bits, max, t_llvm_bool)); + + bits = ir_emit_select(proc, less_equal_width, bits, max); + return ir_emit(proc, ir_instr_binary_op(proc, op, left, bits, type)); + } case Token_AndNot: { // NOTE(bill): x &~ y == x & (~y) == x & (y ~ -1) |