mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-04 14:48:47 +00:00
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
This commit is contained in:
+28
-4
@@ -4499,11 +4499,35 @@ irValue *ir_emit_arith(irProcedure *proc, TokenKind op, irValue *left, irValue *
|
|||||||
handle_op:
|
handle_op:
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case Token_Shl:
|
case Token_Shl:
|
||||||
case Token_Shr:
|
{
|
||||||
left = ir_emit_conv(proc, left, type);
|
left = ir_emit_conv(proc, left, type);
|
||||||
right = ir_emit_conv(proc, right, type);
|
right = ir_emit_conv(proc, right, type);
|
||||||
|
ir_emit(proc, ir_instr_binary_op(proc, op, left, right, type));
|
||||||
|
|
||||||
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));
|
||||||
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
bool is_unsigned = is_type_unsigned(ir_type(left));
|
||||||
|
|
||||||
|
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: {
|
case Token_AndNot: {
|
||||||
// NOTE(bill): x &~ y == x & (~y) == x & (y ~ -1)
|
// NOTE(bill): x &~ y == x & (~y) == x & (y ~ -1)
|
||||||
|
|||||||
+30
-8
@@ -5546,17 +5546,39 @@ handle_op:
|
|||||||
res.value = LLVMBuildXor(p->builder, lhs.value, rhs.value, "");
|
res.value = LLVMBuildXor(p->builder, lhs.value, rhs.value, "");
|
||||||
return res;
|
return res;
|
||||||
case Token_Shl:
|
case Token_Shl:
|
||||||
rhs = lb_emit_conv(p, rhs, lhs.type);
|
{
|
||||||
res.value = LLVMBuildShl(p->builder, lhs.value, rhs.value, "");
|
rhs = lb_emit_conv(p, rhs, lhs.type);
|
||||||
return res;
|
LLVMValueRef lhsval = lhs.value;
|
||||||
|
LLVMValueRef bits = rhs.value;
|
||||||
|
|
||||||
|
LLVMValueRef max = LLVMConstInt(lb_type(p->module, rhs.type), 8*type_size_of(lhs.type) - 1, false);
|
||||||
|
|
||||||
|
LLVMValueRef less_equal_width = LLVMBuildICmp(p->builder, LLVMIntULE, bits, max, "");
|
||||||
|
|
||||||
|
res.value = LLVMBuildShl(p->builder, lhsval, bits, "");
|
||||||
|
LLVMValueRef zero = LLVMConstNull(lb_type(p->module, lhs.type));
|
||||||
|
res.value = LLVMBuildSelect(p->builder, less_equal_width, res.value, zero, "");
|
||||||
|
return res;
|
||||||
|
}
|
||||||
case Token_Shr:
|
case Token_Shr:
|
||||||
if (is_type_unsigned(type)) {
|
{
|
||||||
res.value = LLVMBuildLShr(p->builder, lhs.value, rhs.value, "");
|
rhs = lb_emit_conv(p, rhs, lhs.type);
|
||||||
|
LLVMValueRef lhsval = lhs.value;
|
||||||
|
LLVMValueRef bits = rhs.value;
|
||||||
|
bool is_unsigned = is_type_unsigned(type);
|
||||||
|
|
||||||
|
LLVMValueRef max = LLVMConstInt(lb_type(p->module, rhs.type), 8*type_size_of(lhs.type) - 1, false);
|
||||||
|
|
||||||
|
LLVMValueRef less_equal_width = LLVMBuildICmp(p->builder, LLVMIntULE, bits, max, "");
|
||||||
|
|
||||||
|
bits = LLVMBuildSelect(p->builder, less_equal_width, bits, max, "");
|
||||||
|
if (is_unsigned) {
|
||||||
|
res.value = LLVMBuildLShr(p->builder, lhs.value, bits, "");
|
||||||
|
} else {
|
||||||
|
res.value = LLVMBuildAShr(p->builder, lhsval, bits, "");
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
rhs = lb_emit_conv(p, rhs, lhs.type);
|
|
||||||
res.value = LLVMBuildAShr(p->builder, lhs.value, rhs.value, "");
|
|
||||||
return res;
|
|
||||||
case Token_AndNot:
|
case Token_AndNot:
|
||||||
{
|
{
|
||||||
LLVMValueRef new_rhs = LLVMBuildNot(p->builder, rhs.value, "");
|
LLVMValueRef new_rhs = LLVMBuildNot(p->builder, rhs.value, "");
|
||||||
|
|||||||
Reference in New Issue
Block a user