From 218f293cd46b45c8239ee8a947501b88e12dfee0 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sun, 5 Jan 2025 15:26:20 +0100 Subject: [PATCH] Introducing int_to_ptr expr. --- src/compiler/c_codegen.c | 1 + src/compiler/compiler_internal.h | 9 +++++++++ src/compiler/copying.c | 1 + src/compiler/enums.h | 4 +--- src/compiler/expr.c | 6 +++--- src/compiler/json_output.c | 1 + src/compiler/llvm_codegen_expr.c | 11 +++++------ src/compiler/sema_casts.c | 10 +++++----- src/compiler/sema_expr.c | 4 ++++ src/compiler/sema_liveness.c | 1 + src/compiler/sema_stmts.c | 1 + 11 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/compiler/c_codegen.c b/src/compiler/c_codegen.c index 0f381b297..ce47d23ba 100644 --- a/src/compiler/c_codegen.c +++ b/src/compiler/c_codegen.c @@ -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: diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index 7d8a1623a..a7b0d572c 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -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); diff --git a/src/compiler/copying.c b/src/compiler/copying.c index 984e8717f..17bd94696 100644 --- a/src/compiler/copying.c +++ b/src/compiler/copying.c @@ -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: diff --git a/src/compiler/enums.h b/src/compiler/enums.h index 9d84f8385..bfcd938f2 100644 --- a/src/compiler/enums.h +++ b/src/compiler/enums.h @@ -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, diff --git a/src/compiler/expr.c b/src/compiler/expr.c index 9cb43e61b..5cd945bae 100644 --- a/src/compiler/expr.c +++ b/src/compiler/expr.c @@ -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: diff --git a/src/compiler/json_output.c b/src/compiler/json_output.c index be53e9171..ba4d57785 100644 --- a/src/compiler/json_output.c +++ b/src/compiler/json_output.c @@ -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: diff --git a/src/compiler/llvm_codegen_expr.c b/src/compiler/llvm_codegen_expr.c index 56082ee9b..55d4511ca 100644 --- a/src/compiler/llvm_codegen_expr.c +++ b/src/compiler/llvm_codegen_expr.c @@ -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); diff --git a/src/compiler/sema_casts.c b/src/compiler/sema_casts.c index d07440a01..9fd562741 100644 --- a/src/compiler/sema_casts.c +++ b/src/compiler/sema_casts.c @@ -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); } /** diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index df70e3da5..799417690 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -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; diff --git a/src/compiler/sema_liveness.c b/src/compiler/sema_liveness.c index 6b8a5824d..2e2ee3c42 100644 --- a/src/compiler/sema_liveness.c +++ b/src/compiler/sema_liveness.c @@ -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: diff --git a/src/compiler/sema_stmts.c b/src/compiler/sema_stmts.c index dca3d320f..a189e04c2 100644 --- a/src/compiler/sema_stmts.c +++ b/src/compiler/sema_stmts.c @@ -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: