From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sender4-pp-o91.zoho.com (sender4-pp-o91.zoho.com [136.143.188.91]) by sourceware.org (Postfix) with ESMTPS id 31D9F38708A6; Wed, 2 Sep 2020 01:01:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 31D9F38708A6 ARC-Seal: i=1; a=rsa-sha256; t=1599008483; cv=none; d=zohomail.com; s=zohoarc; b=b1D+kTWMQUH8bMh0yVe+gB2DwDqWKIhTfonxzblhaUTlYNu61kuWvQoI0mdEu3yGr8iYOvkFwpJwoPvp3Hp7s3nWBUtV4PDzXOwgbaTjygTsdjaHbeEmyqjXIS1AiuAQidUzv+0OoFFU6O1Vj/zDwRI5Sc4EOtNOkj3MNh0yeAg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1599008483; h=Content-Type:Date:From:MIME-Version:Message-ID:Subject:To; bh=yxDjtrh8mBLZoG4fC6AjUU8rANtb9PGqnZ/GLtiFF2I=; b=S8Gk68VtzzC9457tGVRLoBBjeAn/iFFT0mG59poGId9VfJTa4dIFRJ3If3V7dXHhoIBx7X0YcWNGYwJs68ZhIJjqgJf4p5QJIOOFqWe1kEnZDmqqhmjRxLV1mJxW60LaApJIWd5ojCTGBxtAeDkuNxpfc3EKMgyQLsXpRt4zKIQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=zoho.com; spf=pass smtp.mailfrom=bouanto@zoho.com; dmarc=pass header.from= header.from= Received: from localhost (38.87.1.99 [38.87.1.99]) by mx.zohomail.com with SMTPS id 1599008482011887.6070147757732; Tue, 1 Sep 2020 18:01:22 -0700 (PDT) Date: Tue, 1 Sep 2020 21:01:20 -0400 From: Antoni Boucher To: gcc-patches@gcc.gnu.org, jit@gcc.gnu.org Subject: [PATCH] libgccjit: add some reflection functions in the jit C api Message-ID: <20200902010120.crnx55ev635ceiey@bouanto-desktop.localdomain> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="27e356a7qduwqprh" Content-Disposition: inline X-Zoho-Virus-Status: 1 X-ZohoMailClient: External X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: jit@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Jit mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 02 Sep 2020 01:01:28 -0000 --27e356a7qduwqprh Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Hello. This WIP patch implements new reflection functions in the C API as mentioned in bug 96889. I'm looking forward for feedbacks on this patch. It's WIP because I'll probably add a few more reflection functions. Thanks. --27e356a7qduwqprh Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-This-patch-add-some-reflection-functions-in-the-jit-.patch" >From 23ab738c0d9202f6798a38fb4aa15edfcc67d11c Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 1 Aug 2020 17:52:17 -0400 Subject: [PATCH] This patch add some reflection functions in the jit C api. 2020-09-1 Antoni Boucher gcc/jit/ PR target/96889 * docs/topics/compatibility.rst (LIBGCCJIT_ABI_14): New ABI tag. * docs/topics/functions.rst: Add documentation for the functions gcc_jit_function_get_return_type and gcc_jit_function_get_param_count * libgccjit.c (gcc_jit_function_get_param_count and gcc_jit_function_get_return_type): New functions. * libgccjit.h * libgccjit.map (LIBGCCJIT_ABI_14): New ABI tag. gcc/testsuite/ PR target/96889 * jit.dg/all-non-failing-tests.h: Add test-reflection.c. * jit.dg/test-reflection.c: New test. --- gcc/jit/docs/topics/compatibility.rst | 11 ++++++++ gcc/jit/docs/topics/functions.rst | 10 +++++++ gcc/jit/libgccjit.c | 28 ++++++++++++++++++++ gcc/jit/libgccjit.h | 24 +++++++++++++++++ gcc/jit/libgccjit.map | 8 +++++- gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 +++++++ gcc/testsuite/jit.dg/test-reflection.c | 27 +++++++++++++++++++ 7 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/jit.dg/test-reflection.c diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst index bb3387fa583..7e786194ded 100644 --- a/gcc/jit/docs/topics/compatibility.rst +++ b/gcc/jit/docs/topics/compatibility.rst @@ -219,3 +219,14 @@ entrypoints: * :func:`gcc_jit_version_minor` * :func:`gcc_jit_version_patchlevel` + +.. _LIBGCCJIT_ABI_14: + +``LIBGCCJIT_ABI_14`` +-------------------- +``LIBGCCJIT_ABI_14`` covers the addition of reflection functions via API +entrypoints: + + * :func:`gcc_jit_function_get_return_type` + + * :func:`gcc_jit_function_get_param_count` diff --git a/gcc/jit/docs/topics/functions.rst b/gcc/jit/docs/topics/functions.rst index eb40d64010e..9819c28cda2 100644 --- a/gcc/jit/docs/topics/functions.rst +++ b/gcc/jit/docs/topics/functions.rst @@ -171,6 +171,16 @@ Functions underlying string, so it is valid to pass in a pointer to an on-stack buffer. +.. function:: size_t \ + gcc_jit_function_get_param_count (gcc_jit_function *func) + + Get the number of parameters of the function. + +.. function:: gcc_jit_type \* + gcc_jit_function_get_return_type (gcc_jit_function *func) + + Get the return type of the function. + Blocks ------ .. type:: gcc_jit_block diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c index 50130fbbe00..e3f8dd33665 100644 --- a/gcc/jit/libgccjit.c +++ b/gcc/jit/libgccjit.c @@ -1012,6 +1012,34 @@ gcc_jit_function_get_param (gcc_jit_function *func, int index) return static_cast (func->get_param (index)); } +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, the real work is done by the + gcc::jit::recording::function::get_params method, in + jit-recording.h. + */ + +size_t +gcc_jit_function_get_param_count (gcc_jit_function *func) +{ + RETURN_NULL_IF_FAIL (func, NULL, NULL, "NULL function"); + gcc::jit::recording::context *ctxt = func->m_ctxt; + JIT_LOG_FUNC (ctxt->get_logger ()); + return func->get_params ().length (); +} + +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, the real work is done by the + gcc::jit::recording::function::get_return_type method, in + jit-recording.h. */ + +gcc_jit_type * +gcc_jit_function_get_return_type (gcc_jit_function *func) +{ + return (gcc_jit_type *)func->get_return_type (); +} + /* Public entrypoint. See description in libgccjit.h. After error-checking, the real work is done by the diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h index 1c5a12e9c01..6999ce25ca2 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -740,6 +740,14 @@ gcc_jit_function_as_object (gcc_jit_function *func); extern gcc_jit_param * gcc_jit_function_get_param (gcc_jit_function *func, int index); +/* Get the number of params of a function. */ +extern size_t +gcc_jit_function_get_param_count (gcc_jit_function *func); + +/* Get the return type of a function. */ +extern gcc_jit_type * +gcc_jit_function_get_return_type (gcc_jit_function *func); + /* Emit the function in graphviz format. */ extern void gcc_jit_function_dump_to_dot (gcc_jit_function *func, @@ -1503,6 +1511,22 @@ gcc_jit_version_minor (void); extern int gcc_jit_version_patchlevel (void); +#define LIBGCCJIT_HAVE_gcc_jit_function_reflection + +/* Reflection functions to get the number of parameters and return types of + a function from the C API. + + "vec_type" should be a vector type, created using gcc_jit_type_get_vector. + + This API entrypoint was added in LIBGCCJIT_ABI_14; you can test for its + presence using + #ifdef LIBGCCJIT_HAVE_gcc_jit_function_reflection +*/ +extern gcc_jit_type * +gcc_jit_function_get_return_type (gcc_jit_function *func); +extern size_t +gcc_jit_function_get_param_count (gcc_jit_function *func); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index 6137dd4b4b0..b28f81a7a32 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -186,4 +186,10 @@ LIBGCCJIT_ABI_13 { gcc_jit_version_major; gcc_jit_version_minor; gcc_jit_version_patchlevel; -} LIBGCCJIT_ABI_12; \ No newline at end of file +} LIBGCCJIT_ABI_12; + +LIBGCCJIT_ABI_14 { + global: + gcc_jit_function_get_return_type; + gcc_jit_function_get_param_count; +} LIBGCCJIT_ABI_13; diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index 632ab8cfb2e..c59af802f16 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -251,6 +251,13 @@ #undef create_code #undef verify_code +/* test-reflection.c */ +#define create_code create_code_reflection +#define verify_code verify_code_reflection +#include "test-reflection.c" +#undef create_code +#undef verify_code + /* test-string-literal.c */ #define create_code create_code_string_literal #define verify_code verify_code_string_literal @@ -417,6 +424,9 @@ const struct testcase testcases[] = { {"reading_struct ", create_code_reading_struct , verify_code_reading_struct }, + {"reflection", + create_code_reflection , + verify_code_reflection }, {"string_literal", create_code_string_literal, verify_code_string_literal}, diff --git a/gcc/testsuite/jit.dg/test-reflection.c b/gcc/testsuite/jit.dg/test-reflection.c new file mode 100644 index 00000000000..b39e9abab33 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-reflection.c @@ -0,0 +1,27 @@ +#include +#include + +#include "libgccjit.h" + +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Do nothing. */ +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + /* Get the built-in functions. */ + gcc_jit_function *builtin_sin = + gcc_jit_context_get_builtin_function (ctxt, "sin"); + + CHECK_VALUE (gcc_jit_function_get_param_count(builtin_sin), 1); + + gcc_jit_type *double_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE); + CHECK_VALUE (gcc_jit_function_get_return_type(builtin_sin), double_type); +} + -- 2.28.0 --27e356a7qduwqprh--