Skip to content

Commit

Permalink
Refactor casts, removing SLBOOL and PTRBOOL.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Jan 5, 2025
1 parent 218f293 commit c6c7baa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 15 deletions.
2 changes: 0 additions & 2 deletions src/compiler/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,7 @@ typedef enum
CAST_EUER,
CAST_FPFP,
CAST_INTENUM,
CAST_PTRBOOL,
CAST_PTRINT,
CAST_SLBOOL,
CAST_SLARR,
CAST_VECARR,
CAST_EXPVEC,
Expand Down
2 changes: 0 additions & 2 deletions src/compiler/expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 0 additions & 9 deletions src/compiler/llvm_codegen_expr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/sema_casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/test_suite/cast/cast_ptr_vec_to_bool.c3t
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit c6c7baa

Please sign in to comment.