From c6c7baa3b470be4f117efdb02e7ee791a0702ab1 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 5 Jan 2025 15:45:39 +0100 Subject: [PATCH] Refactor casts, removing SLBOOL and PTRBOOL. --- src/compiler/enums.h | 2 -- src/compiler/expr.c | 2 -- src/compiler/llvm_codegen_expr.c | 9 ------- src/compiler/sema_casts.c | 13 ++++++++-- test/test_suite/cast/cast_ptr_vec_to_bool.c3t | 25 +++++++++++++++++++ 5 files changed, 36 insertions(+), 15 deletions(-) create mode 100644 test/test_suite/cast/cast_ptr_vec_to_bool.c3t diff --git a/src/compiler/enums.h b/src/compiler/enums.h index bfcd938f2..5f408c9ac 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -548,9 +548,7 @@ typedef enum CAST_EUER, CAST_FPFP, CAST_INTENUM, - CAST_PTRBOOL, CAST_PTRINT, - CAST_SLBOOL, CAST_SLARR, CAST_VECARR, CAST_EXPVEC, diff --git a/src/compiler/expr.c b/src/compiler/expr.c index 5cd945bae..75b98193c 100644 --- a/src/compiler/expr.c +++ b/src/compiler/expr.c @@ -363,9 +363,7 @@ static inline bool expr_cast_is_runtime_const(Expr *expr) UNREACHABLE case CAST_INTENUM: case CAST_EUER: - case CAST_PTRBOOL: case CAST_FPFP: - case CAST_SLBOOL: case CAST_VECARR: return exprid_is_runtime_const(expr->cast_expr.expr); case CAST_APTSA: diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 55d4511ca..7b687bb27 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -1470,11 +1470,6 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu case CAST_EUER: REMINDER("Improve fault to err comparison"); break; - case CAST_PTRBOOL: - llvm_value_rvalue(c, value); - value->value = LLVMBuildIsNotNull(c->builder, value->value, "ptrbool"); - value->kind = BE_BOOLEAN; - break; case CAST_FPFP: llvm_value_rvalue(c, value); value->value = type_convert_will_trunc(to_type, from_type) @@ -1511,10 +1506,6 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu } value->type = type_lowering(to_type); return; - case CAST_SLBOOL: - llvm_emit_slice_len(c, value, value); - llvm_emit_int_comp_zero(c, value, value, BINARYOP_NE); - break; } value->type = type_lowering(to_type); } diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index 9fd562741..8906e39ac 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -1795,8 +1795,13 @@ static void cast_vec_to_vec(SemaContext *context, Expr *expr, Type *to_type) UNREACHABLE return; case TYPE_BOOL: - insert_runtime_cast(expr, CAST_PTRBOOL, to_type); + { + Expr *inner = expr_copy(expr); + expr->expr_kind = EXPR_INT_TO_BOOL; + expr->int_to_bool_expr = (ExprIntToBool) { .inner = inner, .negate = false }; + expr->type = to_type; return; + } case ALL_INTS: expr_rewrite_to_int_to_ptr(expr, to_type); return; @@ -1996,7 +2001,11 @@ static void cast_slice_to_bool(SemaContext *context, Expr *expr, Type *type) expr_rewrite_const_bool(expr, type, expr->const_expr.slice_init != NULL); return; } - insert_runtime_cast(expr, CAST_SLBOOL, type); + Expr *inner = expr_copy(expr); + Expr *len = expr_copy(expr); + expr_rewrite_slice_len(len, inner, type_usz); + expr_rewrite_to_binary(expr, len, expr_new_const_int(expr->span, type_usz, 0), BINARYOP_NE); + expr->type = type; } /** diff --git a/test/test_suite/cast/cast_ptr_vec_to_bool.c3t b/test/test_suite/cast/cast_ptr_vec_to_bool.c3t new file mode 100644 index 000000000..2865abb8a --- /dev/null +++ b/test/test_suite/cast/cast_ptr_vec_to_bool.c3t @@ -0,0 +1,25 @@ +// #target: macos-x64 +module test; +fn void main() +{ + int xd; + void*[<2>] x = { &xd, null }; + bool[<2>] y = (bool[<2>])x; +} + +/* #expect: test.ll + +entry: + %xd = alloca i32, align 4 + %x = alloca <2 x ptr>, align 16 + %y = alloca <2 x i8>, align 2 + store i32 0, ptr %xd, align 4 + %0 = insertelement <2 x ptr> undef, ptr %xd, i64 0 + %1 = insertelement <2 x ptr> %0, ptr null, i64 1 + store <2 x ptr> %1, ptr %x, align 16 + %2 = load <2 x ptr>, ptr %x, align 16 + %i2b = icmp ne <2 x ptr> %2, zeroinitializer + %3 = sext <2 x i1> %i2b to <2 x i8> + store <2 x i8> %3, ptr %y, align 2 + ret void +}