https://gcc.gnu.org/g:88a676869a750d16ce6b752f82eaf5e2617bd0f3
commit r17-1452-g88a676869a750d16ce6b752f82eaf5e2617bd0f3 Author: Antoni Boucher <[email protected]> Date: Tue Dec 10 17:47:39 2024 -0500 libgccjit: Allow casts between integers and pointers gcc/jit/ChangeLog: * libgccjit.cc: Allow cast between integers and pointers in is_valid_cast. gcc/testsuite/ChangeLog: * jit.dg/test-cast.c: Add test case for pointer to int cast. Diff: --- gcc/jit/libgccjit.cc | 4 ++-- gcc/testsuite/jit.dg/test-cast.c | 43 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc index f45d2009263e..ff44fd4f7960 100644 --- a/gcc/jit/libgccjit.cc +++ b/gcc/jit/libgccjit.cc @@ -2513,10 +2513,10 @@ is_valid_cast (gcc::jit::recording::type *src_type, if (dst_is_int || dst_is_bool) return true; - /* Permit casts between pointer types. */ + /* Permit casts between pointer types and integers and pointers. */ gcc::jit::recording::type *deref_src_type = src_type->is_pointer (); gcc::jit::recording::type *deref_dst_type = dst_type->is_pointer (); - if (deref_src_type && deref_dst_type) + if ((deref_src_type || src_is_int) && (deref_dst_type || dst_is_int)) return true; return false; diff --git a/gcc/testsuite/jit.dg/test-cast.c b/gcc/testsuite/jit.dg/test-cast.c index 2b1e385ae405..9492d147a6d3 100644 --- a/gcc/testsuite/jit.dg/test-cast.c +++ b/gcc/testsuite/jit.dg/test-cast.c @@ -15,9 +15,17 @@ my_casts (int x) { return (char)(long) x; } + +int +ptrtoint (int *y) +{ + return (int) y; +} */ gcc_jit_type *int_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *int_ptr_type = + gcc_jit_type_get_pointer (int_type); gcc_jit_type *long_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG); gcc_jit_type *return_type = @@ -50,17 +58,50 @@ my_casts (int x) ), return_type )); + + gcc_jit_param *y = + gcc_jit_context_new_param ( + ctxt, + NULL, + int_ptr_type, "y"); + gcc_jit_param *params2[1] = {y}; + gcc_jit_function *ptrtoint_func = + gcc_jit_context_new_function (ctxt, + NULL, + GCC_JIT_FUNCTION_EXPORTED, + int_type, + "ptrtoint", + 1, params2, 0); + + gcc_jit_block *start_block = + gcc_jit_function_new_block (ptrtoint_func, "start_block"); + + gcc_jit_block_end_with_return (start_block, NULL, + gcc_jit_context_new_cast (ctxt, + NULL, + gcc_jit_param_as_rvalue (y), + int_type + )); } void verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) { - typedef int (*my_casts_fn_type) (int); CHECK_NON_NULL (result); + + typedef int (*my_casts_fn_type) (int); my_casts_fn_type my_casts = (my_casts_fn_type)gcc_jit_result_get_code (result, "my_casts"); CHECK_NON_NULL (my_casts); char val = my_casts (10); note ("my_casts returned: %d", val); CHECK_VALUE (val, 10); + + typedef int (*ptrtoint_fn_type) (int*); + ptrtoint_fn_type ptrtoint = + (ptrtoint_fn_type)gcc_jit_result_get_code (result, "ptrtoint"); + CHECK_NON_NULL (ptrtoint); + int int_val = ptrtoint (NULL); + note ("ptrtoint returned: %d", int_val); + CHECK_VALUE (int_val, 0); }
