From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7855) id A096C384842A; Sun, 18 Jul 2021 14:10:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A096C384842A MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Antoni Boucher To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-2384] libgccjit: Handle truncation and extension for casts [PR95498] X-Act-Checkin: gcc X-Git-Author: Antoni Boucher X-Git-Refname: refs/heads/master X-Git-Oldrev: 853921378bfa149353b4e1c7dde5c02f80072ad7 X-Git-Newrev: 5cca4131e4aabf70a18e362620ad65a3cebf227a Message-Id: <20210718141026.A096C384842A@sourceware.org> Date: Sun, 18 Jul 2021 14:10:26 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 18 Jul 2021 14:10:26 -0000 https://gcc.gnu.org/g:5cca4131e4aabf70a18e362620ad65a3cebf227a commit r12-2384-g5cca4131e4aabf70a18e362620ad65a3cebf227a Author: Antoni Boucher Date: Sun Jul 5 19:07:30 2020 -0400 libgccjit: Handle truncation and extension for casts [PR95498] 2021-07-18 Antoni Boucher gcc/jit/ PR target/95498 * jit-playback.c (convert): Add support to handle truncation and extension in the convert function. gcc/testsuite/ PR target/95498 * jit.dg/all-non-failing-tests.h: New test. * jit.dg/test-cast.c: New test. Signed-off-by: Antoni Boucher Diff: --- gcc/jit/jit-playback.c | 32 +++++++++----- gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 +++++ gcc/testsuite/jit.dg/test-cast.c | 66 ++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 11 deletions(-) diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c index c6136301243..79ac525e5df 100644 --- a/gcc/jit/jit-playback.c +++ b/gcc/jit/jit-playback.c @@ -62,22 +62,32 @@ along with GCC; see the file COPYING3. If not see /* gcc::jit::playback::context::build_cast uses the convert.h API, which in turn requires the frontend to provide a "convert" - function, apparently as a fallback. - - Hence we provide this dummy one, with the requirement that any casts - are handled before reaching this. */ + function, apparently as a fallback for casts that can be simplified + (truncation, extension). */ extern tree convert (tree type, tree expr); tree convert (tree dst_type, tree expr) { - gcc_assert (gcc::jit::active_playback_ctxt); - gcc::jit::active_playback_ctxt->add_error (NULL, "unhandled conversion"); - fprintf (stderr, "input expression:\n"); - debug_tree (expr); - fprintf (stderr, "requested type:\n"); - debug_tree (dst_type); - return error_mark_node; + tree t_ret = NULL; + t_ret = targetm.convert_to_type (dst_type, expr); + if (t_ret) + return t_ret; + switch (TREE_CODE (dst_type)) + { + case INTEGER_TYPE: + case ENUMERAL_TYPE: + return fold (convert_to_integer (dst_type, expr)); + + default: + gcc_assert (gcc::jit::active_playback_ctxt); + gcc::jit::active_playback_ctxt->add_error (NULL, "unhandled conversion"); + fprintf (stderr, "input expression:\n"); + debug_tree (expr); + fprintf (stderr, "requested type:\n"); + debug_tree (dst_type); + return error_mark_node; + } } namespace gcc { diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index 4202eb7798b..84ef54a0386 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -98,6 +98,13 @@ #undef create_code #undef verify_code +/* test-cast.c */ +#define create_code create_code_cast +#define verify_code verify_code_cast +#include "test-cast.c" +#undef create_code +#undef verify_code + /* test-compound-assignment.c */ #define create_code create_code_compound_assignment #define verify_code verify_code_compound_assignment @@ -361,6 +368,9 @@ const struct testcase testcases[] = { {"calling_internal_function", create_code_calling_internal_function, verify_code_calling_internal_function}, + {"cast", + create_code_cast, + verify_code_cast}, {"compound_assignment", create_code_compound_assignment, verify_code_compound_assignment}, diff --git a/gcc/testsuite/jit.dg/test-cast.c b/gcc/testsuite/jit.dg/test-cast.c new file mode 100644 index 00000000000..2b1e385ae40 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-cast.c @@ -0,0 +1,66 @@ +#include +#include +#include + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: +char +my_casts (int x) +{ + return (char)(long) x; +} + */ + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *long_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG); + gcc_jit_type *return_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR); + + gcc_jit_param *x = + gcc_jit_context_new_param ( + ctxt, + NULL, + int_type, "x"); + gcc_jit_param *params[1] = {x}; + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, + NULL, + GCC_JIT_FUNCTION_EXPORTED, + return_type, + "my_casts", + 1, params, 0); + + gcc_jit_block *initial = + gcc_jit_function_new_block (func, "initial"); + + gcc_jit_block_end_with_return(initial, NULL, + gcc_jit_context_new_cast(ctxt, + NULL, + gcc_jit_context_new_cast(ctxt, + NULL, + gcc_jit_param_as_rvalue(x), + long_type + ), + return_type + )); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + typedef int (*my_casts_fn_type) (int); + CHECK_NON_NULL (result); + 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); +}