From 1b086e06f17c7974d4e7cc026e10f2a918e8aa82 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Thu, 7 Oct 2021 13:53:11 +0200 Subject: [PATCH] Fix of miscompilation --- src/compiler/llvm_codegen_expr.c | 2 +- src/compiler/sema_expr.c | 4 +- .../statements/comparison_widening.c3t | 37 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 test/test_suite/statements/comparison_widening.c3t diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index d8f6c06b8..a082e3765 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -1753,7 +1753,7 @@ static void gencontext_emit_logical_and_or(GenContext *c, BEValue *be_value, Exp // For vector implementation. // Set up basic blocks, following Cone - LLVMBasicBlockRef start_block = LLVMGetInsertBlock(c->builder); + LLVMBasicBlockRef start_block; LLVMBasicBlockRef phi_block = llvm_basic_block_new(c, op == BINARYOP_AND ? "and.phi" : "or.phi"); LLVMBasicBlockRef rhs_block = llvm_basic_block_new(c, op == BINARYOP_AND ? "and.rhs" : "or.rhs"); diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 55d515919..04953b57b 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4586,8 +4586,8 @@ static void cast_to_max_bit_size(Context *context, Expr *left, Expr *right, Type return; } Type *to = right->type->type_kind < TYPE_U8 - ? type_int_signed_by_bitsize(bit_size_right) - : type_int_unsigned_by_bitsize(bit_size_right); + ? type_int_signed_by_bitsize(bit_size_left) + : type_int_unsigned_by_bitsize(bit_size_left); bool success = cast_implicit(right, to); assert(success); } diff --git a/test/test_suite/statements/comparison_widening.c3t b/test/test_suite/statements/comparison_widening.c3t new file mode 100644 index 000000000..a83ab4a25 --- /dev/null +++ b/test/test_suite/statements/comparison_widening.c3t @@ -0,0 +1,37 @@ +// #target: x64-darwin + +func void test1() +{ + char a = 1; + int b = 2; + char c = b > a ? 1 : 0; +} + +// #expect: comparison_widening.ll + +define void @comparison_widening.test1() #0 { +entry: + %a = alloca i8, align 1 + %b = alloca i32, align 4 + %c = alloca i8, align 1 + store i8 1, i8* %a, align 1 + store i32 2, i32* %b, align 4 + %0 = load i32, i32* %b, align 4 + %1 = load i8, i8* %a, align 1 + %uiuiext = zext i8 %1 to i32 + %gt = icmp sgt i32 %0, %uiuiext + %check = icmp sge i32 %uiuiext, 0 + %siui-gt = and i1 %check, %gt + br i1 %siui-gt, label %cond.lhs, label %cond.rhs + +cond.lhs: ; preds = %entry + br label %cond.phi + +cond.rhs: ; preds = %entry + br label %cond.phi + +cond.phi: ; preds = %cond.rhs, %cond.lhs + %val = phi i8 [ 1, %cond.lhs ], [ 0, %cond.rhs ] + store i8 %val, i8* %c, align 1 + ret void +} \ No newline at end of file