Introducing int_to_ptr expr.

This commit is contained in:
Christoffer Lerno
2025-01-05 15:26:20 +01:00
parent 4d641d193c
commit 218f293cd4
11 changed files with 32 additions and 17 deletions

View File

@@ -397,6 +397,7 @@ static void c_emit_expr(GenContext *c, CValue *value, Expr *expr)
{
case EXPR_PTR_ACCESS:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_FLOAT_TO_INT:
case EXPR_SLICE_LEN:
case EXPR_DISCARD:

View File

@@ -3372,6 +3372,7 @@ static inline void expr_set_span(Expr *expr, SourceSpan loc)
case EXPR_SPLAT:
case EXPR_PTR_ACCESS:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_FLOAT_TO_INT:
case EXPR_SLICE_LEN:
case EXPR_DISCARD:
@@ -3829,6 +3830,14 @@ INLINE void expr_rewrite_to_int_to_float(Expr *expr, Type *type)
expr->type = type;
}
INLINE void expr_rewrite_to_int_to_ptr(Expr *expr, Type *type)
{
Expr *inner = expr_copy(expr);
expr->expr_kind = EXPR_INT_TO_PTR;
expr->inner_expr = inner;
expr->type = type;
}
INLINE void expr_rewrite_to_float_to_int(Expr *expr, Type *type)
{
Expr *inner = expr_copy(expr);

View File

@@ -477,6 +477,7 @@ Expr *copy_expr(CopyStruct *c, Expr *source_expr)
case EXPR_STRINGIFY:
case EXPR_PTR_ACCESS:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_FLOAT_TO_INT:
case EXPR_SLICE_LEN:
case EXPR_DISCARD:

View File

@@ -544,18 +544,15 @@ typedef enum
{
CAST_APTSA,
CAST_BSBOOL,
CAST_ERPTR,
CAST_ERROR,
CAST_EUER,
CAST_FPFP,
CAST_INTENUM,
CAST_IDPTR,
CAST_PTRBOOL,
CAST_PTRINT,
CAST_SLBOOL,
CAST_SLARR,
CAST_VECARR,
CAST_INTPTR,
CAST_EXPVEC,
} CastKind;
@@ -774,6 +771,7 @@ typedef enum
EXPR_IDENTIFIER,
EXPR_INITIALIZER_LIST,
EXPR_INT_TO_FLOAT,
EXPR_INT_TO_PTR,
EXPR_LAMBDA,
EXPR_LAST_FAULT,
EXPR_MACRO_BLOCK,

View File

@@ -86,6 +86,7 @@ bool expr_may_addr(Expr *expr)
case EXPR_PTR_ACCESS:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_SLICE_LEN:
case EXPR_RVALUE:
case EXPR_RECAST:
@@ -206,6 +207,7 @@ bool expr_is_runtime_const(Expr *expr)
case EXPR_RECAST:
case EXPR_ADDR_CONVERSION:
case EXPR_DISCARD:
case EXPR_INT_TO_PTR:
return expr_is_runtime_const(expr->inner_expr);
case EXPR_MAKE_ANY:
if (!expr_is_runtime_const(expr->make_any_expr.typeid)) return false;
@@ -366,10 +368,7 @@ static inline bool expr_cast_is_runtime_const(Expr *expr)
case CAST_SLBOOL:
case CAST_VECARR:
return exprid_is_runtime_const(expr->cast_expr.expr);
case CAST_INTPTR:
case CAST_APTSA:
case CAST_ERPTR:
case CAST_IDPTR:
case CAST_EXPVEC:
return exprid_is_runtime_const(expr->cast_expr.expr);
case CAST_PTRINT:
@@ -610,6 +609,7 @@ bool expr_is_pure(Expr *expr)
return expr_is_pure(expr->make_any_expr.inner) && expr_is_pure(expr->make_any_expr.typeid);
case EXPR_PTR_ACCESS:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_FLOAT_TO_INT:
case EXPR_SLICE_LEN:
case EXPR_DISCARD:

View File

@@ -274,6 +274,7 @@ void print_var_expr(FILE *file, Expr *expr)
break;
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
TODO
break;
case EXPR_PTR_ACCESS:

View File

@@ -1481,12 +1481,6 @@ void llvm_emit_cast(GenContext *c, CastKind cast_kind, Expr *expr, BEValue *valu
? LLVMBuildFPTrunc(c->builder, value->value, llvm_get_type(c, to_type), "fpfptrunc")
: LLVMBuildFPExt(c->builder, value->value, llvm_get_type(c, to_type), "fpfpext");
break;
case CAST_IDPTR:
case CAST_ERPTR:
case CAST_INTPTR:
llvm_value_rvalue(c, value);
value->value = LLVMBuildIntToPtr(c->builder, value->value, llvm_get_type(c, to_type), "intptr");
break;
case CAST_INTENUM:
if (safe_mode_enabled() && c->builder != c->global_builder)
{
@@ -7294,6 +7288,11 @@ void llvm_emit_expr(GenContext *c, BEValue *value, Expr *expr)
llvm_value_set(value, NULL, type_void);
llvm_emit_ignored_expr(c, expr->inner_expr);
return;
case EXPR_INT_TO_PTR:
llvm_emit_expr(c, value, expr->inner_expr);
llvm_value_rvalue(c, value);
llvm_value_set(value, LLVMBuildIntToPtr(c->builder, value->value, llvm_get_type(c, expr->type), "intptr"), expr->type);
return;
case EXPR_ADDR_CONVERSION:
llvm_emit_expr(c, value, expr->inner_expr);
llvm_value_addr(c, value);

View File

@@ -1435,10 +1435,10 @@ static void cast_ptr_to_any(SemaContext *context, Expr *expr, Type *type)
}
static void cast_struct_to_inline(SemaContext *context, Expr *expr, Type *type) { expr_rewrite_addr_conversion(expr, type); }
static void cast_fault_to_anyfault(SemaContext *context, Expr *expr, Type *type) { expr->type = type; };
static void cast_fault_to_ptr(SemaContext *context, Expr *expr, Type *type) { insert_runtime_cast(expr, CAST_ERPTR, type); }
static void cast_fault_to_ptr(SemaContext *context, Expr *expr, Type *type) { expr_rewrite_to_int_to_ptr(expr, type); }
static void cast_typeid_to_int(SemaContext *context, Expr *expr, Type *type) { expr_rewrite_ext_trunc(expr, type, type_is_signed(type_flatten_to_int(type))); }
static void cast_fault_to_int(SemaContext *context, Expr *expr, Type *type) { cast_typeid_to_int(context, expr, type); }
static void cast_typeid_to_ptr(SemaContext *context, Expr *expr, Type *type) { insert_runtime_cast(expr, CAST_IDPTR, type); }
static void cast_typeid_to_ptr(SemaContext *context, Expr *expr, Type *type) { expr_rewrite_to_int_to_ptr(expr, type); }
static void cast_any_to_bool(SemaContext *context, Expr *expr, Type *type) {
expr_rewrite_ptr_access(expr, expr_copy(expr), type_voidptr);
expr_rewrite_int_to_bool(expr, false);
@@ -1783,7 +1783,7 @@ static void cast_vec_to_vec(SemaContext *context, Expr *expr, Type *to_type)
case TYPE_TYPEID:
case TYPE_ANYFAULT:
case TYPE_FAULTTYPE:
insert_runtime_cast(expr, CAST_INTPTR, to_type);
expr_rewrite_to_int_to_ptr(expr, to_type);
default:
UNREACHABLE;
}
@@ -1798,7 +1798,7 @@ static void cast_vec_to_vec(SemaContext *context, Expr *expr, Type *to_type)
insert_runtime_cast(expr, CAST_PTRBOOL, to_type);
return;
case ALL_INTS:
insert_runtime_cast(expr, CAST_INTPTR, to_type);
expr_rewrite_to_int_to_ptr(expr, to_type);
return;
case TYPE_POINTER:
case TYPE_FUNC_PTR:
@@ -1870,7 +1870,7 @@ static void cast_int_to_ptr(SemaContext *context, Expr *expr, Type *type)
}
// This may be a narrowing
cast_no_check(context, expr, type_uptr, IS_OPTIONAL(expr));
insert_runtime_cast(expr, CAST_INTPTR, type);
expr_rewrite_to_int_to_ptr(expr, type);
}
/**

View File

@@ -562,6 +562,7 @@ static bool sema_binary_is_expr_lvalue(SemaContext *context, Expr *top_expr, Exp
case EXPR_SLICE_LEN:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_RECAST:
case EXPR_RETHROW:
case EXPR_RETVAL:
@@ -610,6 +611,7 @@ static bool expr_may_ref(Expr *expr)
case EXPR_PTR_ACCESS:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_SLICE_LEN:
case EXPR_VECTOR_FROM_ARRAY:
case EXPR_INT_TO_BOOL:
@@ -9001,6 +9003,7 @@ static inline bool sema_expr_analyse_ct_defined(SemaContext *context, Expr *expr
case EXPR_ADDR_CONVERSION:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
if (!sema_analyse_expr(active_context, main_expr)) goto FAIL;
break;
}
@@ -9384,6 +9387,7 @@ static inline bool sema_analyse_expr_dispatch(SemaContext *context, Expr *expr,
case EXPR_TYPEID_INFO:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
UNREACHABLE
case EXPR_MAKE_ANY:
if (!sema_analyse_expr(context, expr->make_any_expr.typeid)) return false;

View File

@@ -332,6 +332,7 @@ RETRY:
case EXPR_PTR_ACCESS:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_SLICE_LEN:
case EXPR_VECTOR_FROM_ARRAY:
case EXPR_RVALUE:

View File

@@ -767,6 +767,7 @@ static inline bool sema_expr_valid_try_expression(Expr *expr)
case EXPR_PTR_ACCESS:
case EXPR_FLOAT_TO_INT:
case EXPR_INT_TO_FLOAT:
case EXPR_INT_TO_PTR:
case EXPR_SLICE_LEN:
case EXPR_VECTOR_FROM_ARRAY:
case EXPR_RVALUE: