aboutsummaryrefslogtreecommitdiff
path: root/src/llvm_backend.cpp
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2020-05-31 13:50:17 +0100
committergingerBill <bill@gingerbill.org>2020-05-31 13:50:17 +0100
commitbf5ce04b24936a5b68223a0bfcbef5cb395c4951 (patch)
tree5ae390e08844819906d67e3fd7a860fc30422b81 /src/llvm_backend.cpp
parent8057af9e0929a7a2c010752212e0c94e923c0744 (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/llvm_backend.cpp')
-rw-r--r--src/llvm_backend.cpp38
1 files changed, 30 insertions, 8 deletions
diff --git a/src/llvm_backend.cpp b/src/llvm_backend.cpp
index 4266d5921..b064378b6 100644
--- a/src/llvm_backend.cpp
+++ b/src/llvm_backend.cpp
@@ -5546,17 +5546,39 @@ handle_op:
res.value = LLVMBuildXor(p->builder, lhs.value, rhs.value, "");
return res;
case Token_Shl:
- rhs = lb_emit_conv(p, rhs, lhs.type);
- res.value = LLVMBuildShl(p->builder, lhs.value, rhs.value, "");
- return res;
+ {
+ rhs = lb_emit_conv(p, rhs, lhs.type);
+ 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:
- 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;
}
- rhs = lb_emit_conv(p, rhs, lhs.type);
- res.value = LLVMBuildAShr(p->builder, lhs.value, rhs.value, "");
- return res;
case Token_AndNot:
{
LLVMValueRef new_rhs = LLVMBuildNot(p->builder, rhs.value, "");