From 8b8a8d81dbbf62f45dc3be5e6ae76f7758baf507 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Mon, 21 Mar 2022 13:22:59 +0100 Subject: [PATCH] Fix of issue with "a ?? false" --- lib/std/libc.c3 | 1 + src/compiler/llvm_codegen_expr.c | 20 +++++++++++++--- src/version.h | 2 +- test/test_suite/errors/or_err_bool.c3t | 32 ++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 test/test_suite/errors/or_err_bool.c3t diff --git a/lib/std/libc.c3 b/lib/std/libc.c3 index b29239b7d..df7498bd5 100644 --- a/lib/std/libc.c3 +++ b/lib/std/libc.c3 @@ -296,6 +296,7 @@ extern fn CFile tmpnam(char* str); extern fn int fprintf(CFile stream, char* format, ...); extern fn int printf(char* format, ...); extern fn int sprintf(char* str, char* format, ...); +extern fn int snprintf(char* str, usize size, char* format, ...); extern fn int fscanf(CFile stream, char* format, ...); extern fn int scanf(char* format, ...); extern fn int sscanf(char* str, char* format, ...); diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 02b485128..bb5a96382 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -3523,13 +3523,27 @@ static void gencontext_emit_or_error(GenContext *c, BEValue *be_value, Expr *exp return; } - LLVMValueRef phi = LLVMBuildPhi(c->builder, llvm_get_type(c, expr->type), "val"); + if (expr->type->type_kind == TYPE_BOOL) + { + } LLVMValueRef logic_values[2] = { normal_value.value, else_value.value }; LLVMBasicBlockRef blocks[2] = { success_end_block, else_block_exit }; - LLVMAddIncoming(phi, logic_values, blocks, 2); - llvm_value_set(be_value, phi, expr->type); + + if (expr->type->type_kind == TYPE_BOOL) + { + LLVMValueRef phi = LLVMBuildPhi(c->builder, c->bool_type, "val"); + LLVMAddIncoming(phi, logic_values, blocks, 2); + llvm_value_set_bool(be_value, phi); + return; + } + else + { + LLVMValueRef phi = LLVMBuildPhi(c->builder, llvm_get_type(c, expr->type), "val"); + LLVMAddIncoming(phi, logic_values, blocks, 2); + llvm_value_set(be_value, phi, expr->type); + } } diff --git a/src/version.h b/src/version.h index 3f64e2252..f53adbdb1 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define COMPILER_VERSION "PRE.27" \ No newline at end of file +#define COMPILER_VERSION "PRE.28" \ No newline at end of file diff --git a/test/test_suite/errors/or_err_bool.c3t b/test/test_suite/errors/or_err_bool.c3t new file mode 100644 index 000000000..942d362e9 --- /dev/null +++ b/test/test_suite/errors/or_err_bool.c3t @@ -0,0 +1,32 @@ +module test; + +fn void tester() +{ + bool! x = false; + x ?? true; +} + +/* #expect: test.ll + +define void @test.tester() #0 { +entry: + %x = alloca i8, align 1 + %x.f = alloca i64, align 8 + store i8 0, i8* %x, align 1 + store i64 0, i64* %x.f, align 8 + %0 = load i64, i64* %x.f, align 8 + %not_err = icmp eq i64 %0, 0 + br i1 %not_err, label %after_check, label %else_block + +after_check: ; preds = %entry + %1 = load i8, i8* %x, align 1 + %2 = trunc i8 %1 to i1 + br label %phi_block + +else_block: ; preds = %entry + br label %phi_block + +phi_block: ; preds = %else_block, %after_check + %val = phi i1 [ %2, %after_check ], [ true, %else_block ] + ret void +} \ No newline at end of file