mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Refactor casts, removing SLBOOL and PTRBOOL.
This commit is contained in:
@@ -548,9 +548,7 @@ typedef enum
|
||||
CAST_EUER,
|
||||
CAST_FPFP,
|
||||
CAST_INTENUM,
|
||||
CAST_PTRBOOL,
|
||||
CAST_PTRINT,
|
||||
CAST_SLBOOL,
|
||||
CAST_SLARR,
|
||||
CAST_VECARR,
|
||||
CAST_EXPVEC,
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
25
test/test_suite/cast/cast_ptr_vec_to_bool.c3t
Normal file
25
test/test_suite/cast/cast_ptr_vec_to_bool.c3t
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user