public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
From: Antoni Boucher <bouanto@zoho.com>
To: David Malcolm <dmalcolm@redhat.com>
Cc: Antoni Boucher via Jit <jit@gcc.gnu.org>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] libgccjit: add some reflection functions in the jit C api
Date: Mon, 19 Jul 2021 12:10:23 -0400	[thread overview]
Message-ID: <d380f51e0301f46ffcd3ca80fde7f63bdbebf2a5.camel@zoho.com> (raw)
In-Reply-To: <d0143955f50749434e1918b44a3c1d36a91d3239.camel@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 2194 bytes --]

I'm sending the patch once again for review/approval.

I fixed the doc to use the new function names.

Le vendredi 18 juin 2021 à 16:37 -0400, David Malcolm a écrit :
> On Fri, 2021-06-18 at 15:41 -0400, Antoni Boucher wrote:
> > I have write access now.
> 
> Great.
> 
> > I'm not sure how I'm supposed to send my patches:
> > should I put it in personal branches and you'll merge them?
> 
> Please send them to this mailing list for review; once they're
> approved
> you can merge them.
> 
> > 
> > And for the MAINTAINERS file, should I just push to master right
> > away,
> > after sending it to the mailing list?
> 
> I think people just push the MAINTAINERS change and then let the list
> know, since it makes a good test that write access is working
> correctly.
> 
> Dave
> 
> > 
> > Thanks for your help!
> > 
> > Le vendredi 18 juin 2021 à 12:09 -0400, David Malcolm a écrit :
> > > On Fri, 2021-06-18 at 11:55 -0400, Antoni Boucher wrote:
> > > > Le vendredi 11 juin 2021 à 14:00 -0400, David Malcolm a écrit :
> > > > > On Fri, 2021-06-11 at 08:15 -0400, Antoni Boucher wrote:
> > > > > > Thank you for your answer.
> > > > > > I attached the updated patch.
> > > > > 
> > > > > BTW you (or possibly me) dropped the mailing lists; was that
> > > > > deliberate?
> > > > 
> > > > Oh, my bad.
> > > > 
> > > 
> > > [...]
> > > 
> > > 
> > > > > 
> > > > > 
> > > > > > I have signed the FSF copyright attribution.
> > > > > 
> > > > > I can push changes on your behalf, but I'd prefer it if you
> > > > > did
> > > > > it,
> > > > > especially given that you have various other patches you want
> > > > > to
> > > > > get
> > > > > in.
> > > > > 
> > > > > Instructions on how to get push rights to the git repo are
> > > > > here:
> > > > >   https://gcc.gnu.org/gitwrite.html
> > > > > 
> > > > > I can sponsor you.
> > > > 
> > > > Thanks.
> > > > I did sign up to get push rights.
> > > > Have you accepted my request to get those?
> > > 
> > > I did, but I didn't see any kind of notification.  Did you get an
> > > email
> > > about it?
> > > 
> > > 
> > > Dave
> > > 
> > 
> > 
> 
> 


[-- Attachment #2: 0001-libgccjit-Add-some-reflection-functions-PR96889.patch --]
[-- Type: text/x-patch, Size: 30879 bytes --]

From aaf77d109d74afb0f46d3e5d4d094914cb1b21de Mon Sep 17 00:00:00 2001
From: Antoni Boucher <bouanto@zoho.com>
Date: Sat, 1 Aug 2020 17:52:17 -0400
Subject: [PATCH] libgccjit: Add some reflection functions [PR96889]

2021-07-19  Antoni Boucher  <bouanto@zoho.com>

gcc/jit/
	PR target/96889
	* docs/topics/compatibility.rst (LIBGCCJIT_ABI_16): 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
	* docs/topics/types.rst: Add documentation for the functions
	gcc_jit_function_type_get_return_type,
	gcc_jit_function_type_get_param_count,
	gcc_jit_function_type_get_param_type,
	gcc_jit_type_unqualified, gcc_jit_type_dyncast_array,
	gcc_jit_type_is_bool,
	gcc_jit_type_dyncast_function_ptr_type,
	gcc_jit_type_is_integral, gcc_jit_type_is_pointer,
	gcc_jit_type_dyncast_vector,
	gcc_jit_vector_type_get_element_type,
	gcc_jit_vector_type_get_num_units,
	gcc_jit_struct_get_field, gcc_jit_type_is_struct,
	and gcc_jit_struct_get_field_count
	* libgccjit.c:
	(gcc_jit_function_get_return_type, gcc_jit_function_get_param_count,
	gcc_jit_function_type_get_return_type,
	gcc_jit_function_type_get_param_count,
	gcc_jit_function_type_get_param_type, gcc_jit_type_unqualified,
	gcc_jit_type_dyncast_array, gcc_jit_type_is_bool,
	gcc_jit_type_dyncast_function_ptr_type, gcc_jit_type_is_integral,
	gcc_jit_type_is_pointer, gcc_jit_type_dyncast_vector,
	gcc_jit_vector_type_get_element_type,
	gcc_jit_vector_type_get_num_units, gcc_jit_struct_get_field,
	gcc_jit_type_is_struct, gcc_jit_struct_get_field_count): New
	functions.
	(struct gcc_jit_function_type, struct gcc_jit_vector_type):
	New types.
	* libgccjit.h:
	(gcc_jit_function_get_return_type, gcc_jit_function_get_param_count,
	gcc_jit_function_type_get_return_type,
	gcc_jit_function_type_get_param_count,
	gcc_jit_function_type_get_param_type, gcc_jit_type_unqualified,
	gcc_jit_type_dyncast_array, gcc_jit_type_is_bool,
	gcc_jit_type_dyncast_function_ptr_type, gcc_jit_type_is_integral,
	gcc_jit_type_is_pointer, gcc_jit_type_dyncast_vector,
	gcc_jit_vector_type_get_element_type,
	gcc_jit_vector_type_get_num_units, gcc_jit_struct_get_field,
	gcc_jit_type_is_struct, gcc_jit_struct_get_field_count): New
	function declarations.
	(struct gcc_jit_function_type, struct gcc_jit_vector_type):
	New types.
	* jit-recording.h: New functions (is_struct and is_vector)
	* libgccjit.map (LIBGCCJIT_ABI_16): 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        |  43 ++-
 gcc/jit/docs/topics/functions.rst            |  26 ++
 gcc/jit/docs/topics/types.rst                | 122 +++++++++
 gcc/jit/jit-recording.h                      |   7 +
 gcc/jit/libgccjit.c                          | 261 +++++++++++++++++++
 gcc/jit/libgccjit.h                          |  89 +++++++
 gcc/jit/libgccjit.map                        |  21 ++
 gcc/testsuite/jit.dg/all-non-failing-tests.h |  10 +
 gcc/testsuite/jit.dg/test-reflection.c       |  95 +++++++
 9 files changed, 673 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 239b6aa1a92..52ee3f860a7 100644
--- a/gcc/jit/docs/topics/compatibility.rst
+++ b/gcc/jit/docs/topics/compatibility.rst
@@ -230,7 +230,7 @@ entrypoints:
 .. _LIBGCCJIT_ABI_15:
 
 ``LIBGCCJIT_ABI_15``
------------------------
+--------------------
 ``LIBGCCJIT_ABI_15`` covers the addition of API entrypoints for directly
 embedding assembler instructions:
 
@@ -243,3 +243,44 @@ embedding assembler instructions:
   * :func:`gcc_jit_extended_asm_add_input_operand`
   * :func:`gcc_jit_extended_asm_add_clobber`
   * :func:`gcc_jit_context_add_top_level_asm`
+
+.. _LIBGCCJIT_ABI_16:
+
+``LIBGCCJIT_ABI_16``
+--------------------
+``LIBGCCJIT_ABI_16`` covers the addition of reflection functions via API
+entrypoints:
+
+  * :func:`gcc_jit_function_get_return_type`
+
+  * :func:`gcc_jit_function_get_param_count`
+
+  * :func:`gcc_jit_type_dyncast_array`
+
+  * :func:`gcc_jit_type_is_bool`
+
+  * :func:`gcc_jit_type_is_integral`
+
+  * :func:`gcc_jit_type_is_pointer`
+
+  * :func:`gcc_jit_type_is_struct`
+
+  * :func:`gcc_jit_type_dyncast_vector`
+
+  * :func:`gcc_jit_type_unqualified`
+
+  * :func:`gcc_jit_type_dyncast_function_ptr_type`
+
+  * :func:`gcc_jit_function_type_get_return_type`
+
+  * :func:`gcc_jit_function_type_get_param_count`
+
+  * :func:`gcc_jit_function_type_get_param_type`
+
+  * :func:`gcc_jit_vector_type_get_num_units`
+
+  * :func:`gcc_jit_vector_type_get_element_type`
+
+  * :func:`gcc_jit_struct_get_field`
+
+  * :func:`gcc_jit_struct_get_field_count`
diff --git a/gcc/jit/docs/topics/functions.rst b/gcc/jit/docs/topics/functions.rst
index b2d9239aa0a..839934588da 100644
--- a/gcc/jit/docs/topics/functions.rst
+++ b/gcc/jit/docs/topics/functions.rst
@@ -171,6 +171,32 @@ 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.
+
+   The API entrypoints relating to getting info about parameters and return
+   types:
+
+      * :c:func:`gcc_jit_function_get_return_type`
+
+      * :c:func:`gcc_jit_function_get_param_count`
+
+   were added in :ref:`LIBGCCJIT_ABI_16`; you can test for their presence
+   using
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_REFLECTION
+
+   .. type:: gcc_jit_case
+
 Blocks
 ------
 .. type:: gcc_jit_block
diff --git a/gcc/jit/docs/topics/types.rst b/gcc/jit/docs/topics/types.rst
index 831f11b679a..6260e9afba4 100644
--- a/gcc/jit/docs/topics/types.rst
+++ b/gcc/jit/docs/topics/types.rst
@@ -345,3 +345,125 @@ Function pointer types
 
 Function pointer types can be created using
 :c:func:`gcc_jit_context_new_function_ptr_type`.
+
+Reflection API
+--------------
+
+.. function::  gcc_jit_type *\
+               gcc_jit_type_dyncast_array (gcc_jit_type *type)
+
+     Get the element type of an array type or NULL if it's not an array.
+
+.. function::  int\
+               gcc_jit_type_is_bool (gcc_jit_type *type)
+
+     Return non-zero if the type is a bool.
+
+.. function::  gcc_jit_function_type *\
+               gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
+
+     Return the function type if it is one or NULL.
+
+.. function::  gcc_jit_type *\
+               gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
+
+     Given a function type, return its return type.
+
+.. function::  size_t\
+               gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
+
+     Given a function type, return its number of parameters.
+
+.. function::  gcc_jit_type *\
+               gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
+                                                     size_t index)
+
+     Given a function type, return the type of the specified parameter.
+
+.. function::  int\
+               gcc_jit_type_is_integral (gcc_jit_type *type)
+
+     Return non-zero if the type is an integral.
+
+.. function::  gcc_jit_type *\
+               gcc_jit_type_is_pointer (gcc_jit_type *type)
+
+     Return the type pointed by the pointer type or NULL if it's not a pointer.
+
+.. function::  gcc_jit_vector_type *\
+               gcc_jit_type_dyncast_vector (gcc_jit_type *type)
+
+     Given a type, return a dynamic cast to a vector type or NULL.
+
+.. function::  gcc_jit_struct *\
+               gcc_jit_type_is_struct (gcc_jit_type *type)
+
+     Given a type, return a dynamic cast to a struct type or NULL.
+
+.. function::  size_t\
+               gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
+
+     Given a vector type, return the number of units it contains.
+
+.. function::  gcc_jit_type *\
+               gcc_jit_vector_type_get_element_type (gcc_jit_vector_type * vector_type)
+
+     Given a vector type, return the type of its elements.
+
+.. function::  gcc_jit_type *\
+               gcc_jit_type_unqualified (gcc_jit_type *type)
+
+     Given a type, return the unqualified type, removing "const", "volatile" and
+     alignment qualifiers.
+
+.. function::  gcc_jit_field *\
+               gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+                                         size_t index)
+
+     Get a struct field by index.
+
+.. function::  size_t\
+               gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
+
+     Get the number of fields in the struct.
+
+   The API entrypoints related to the reflection API:
+
+      * :c:func:`gcc_jit_function_type_get_return_type`
+
+      * :c:func:`gcc_jit_function_type_get_param_count`
+
+      * :c:func:`gcc_jit_function_type_get_param_type`
+
+      * :c:func:`gcc_jit_type_unqualified`
+
+      * :c:func:`gcc_jit_type_dyncast_array`
+
+      * :c:func:`gcc_jit_type_is_bool`
+
+      * :c:func:`gcc_jit_type_dyncast_function_ptr_type`
+
+      * :c:func:`gcc_jit_type_is_integral`
+
+      * :c:func:`gcc_jit_type_is_pointer`
+
+      * :c:func:`gcc_jit_type_dyncast_vector`
+
+      * :c:func:`gcc_jit_vector_type_get_element_type`
+
+      * :c:func:`gcc_jit_vector_type_get_num_units`
+
+      * :c:func:`gcc_jit_struct_get_field`
+
+      * :c:func:`gcc_jit_type_is_struct`
+
+      * :c:func:`gcc_jit_struct_get_field_count`
+
+   were added in :ref:`LIBGCCJIT_ABI_16`; you can test for their presence
+   using
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_REFLECTION
+
+   .. type:: gcc_jit_case
diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 03fa1160cf0..4a994fe7094 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -546,7 +546,9 @@ public:
   virtual bool is_bool () const = 0;
   virtual type *is_pointer () = 0;
   virtual type *is_array () = 0;
+  virtual struct_ *is_struct () { return NULL; }
   virtual bool is_void () const { return false; }
+  virtual vector_type *is_vector () { return NULL; }
   virtual bool has_known_size () const { return true; }
 
   bool is_numeric () const
@@ -663,6 +665,7 @@ public:
   bool is_bool () const FINAL OVERRIDE { return m_other_type->is_bool (); }
   type *is_pointer () FINAL OVERRIDE { return m_other_type->is_pointer (); }
   type *is_array () FINAL OVERRIDE { return m_other_type->is_array (); }
+  struct_ *is_struct () FINAL OVERRIDE { return m_other_type->is_struct (); }
 
 protected:
   type *m_other_type;
@@ -745,6 +748,8 @@ public:
 
   void replay_into (replayer *) FINAL OVERRIDE;
 
+  vector_type *is_vector () FINAL OVERRIDE { return this; }
+
 private:
   string * make_debug_string () FINAL OVERRIDE;
   void write_reproducer (reproducer &r) FINAL OVERRIDE;
@@ -951,6 +956,8 @@ public:
 
   const char *access_as_type (reproducer &r) FINAL OVERRIDE;
 
+  struct_ *is_struct () FINAL OVERRIDE { return this; }
+
 private:
   string * make_debug_string () FINAL OVERRIDE;
   void write_reproducer (reproducer &r) FINAL OVERRIDE;
diff --git a/gcc/jit/libgccjit.c b/gcc/jit/libgccjit.c
index 7fa948007ad..740e614cd1f 100644
--- a/gcc/jit/libgccjit.c
+++ b/gcc/jit/libgccjit.c
@@ -60,6 +60,14 @@ struct gcc_jit_struct : public gcc::jit::recording::struct_
 {
 };
 
+struct gcc_jit_function_type : public gcc::jit::recording::function_type
+{
+};
+
+struct gcc_jit_vector_type : public gcc::jit::recording::vector_type
+{
+};
+
 struct gcc_jit_field : public gcc::jit::recording::field
 {
 };
@@ -515,6 +523,194 @@ gcc_jit_type_get_volatile (gcc_jit_type *type)
   return (gcc_jit_type *)type->get_volatile ();
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_array method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_dyncast_array (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->is_array ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_bool method, in
+   jit-recording.c.  */
+
+int
+gcc_jit_type_is_bool (gcc_jit_type *type)
+{
+  RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
+
+  return type->is_bool ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_pointer method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->is_pointer ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_int method, in
+   jit-recording.c.  */
+
+int
+gcc_jit_type_is_integral (gcc_jit_type *type)
+{
+  RETURN_VAL_IF_FAIL (type, FALSE, NULL, NULL, "NULL type");
+
+  return type->is_int ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_vector method, in
+   jit-recording.c.  */
+
+gcc_jit_vector_type *
+gcc_jit_type_dyncast_vector (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::vector_type *vector_type = type->is_vector ();
+  return (gcc_jit_vector_type *)vector_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::is_struct method, in
+   jit-recording.c.  */
+
+gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::struct_ *struct_type = type->is_struct ();
+  return (gcc_jit_struct *)struct_type;
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::vector_type::get_num_units method, in
+   jit-recording.c.  */
+
+size_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type)
+{
+  RETURN_VAL_IF_FAIL (vector_type, 0, NULL, NULL, "NULL vector_type");
+  return vector_type->get_num_units ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::vector_type::get_element_type method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type)
+{
+  RETURN_NULL_IF_FAIL (vector_type, NULL, NULL, "NULL vector_type");
+  return (gcc_jit_type *)vector_type->get_element_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::unqualified method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_type *)type->unqualified ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::type::dyn_cast_function_type method, in
+   jit-recording.c.  */
+
+gcc_jit_function_type *
+gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (type, NULL, NULL, "NULL type");
+  gcc::jit::recording::type *func_ptr_type = type->dereference ();
+  RETURN_NULL_IF_FAIL (func_ptr_type, NULL, NULL, "NULL type");
+
+  return (gcc_jit_function_type *)func_ptr_type->dyn_cast_function_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_return_type method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type)
+{
+  RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
+  return (gcc_jit_type *)function_type->get_return_type ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_param_types method, in
+   jit-recording.c.  */
+
+size_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type)
+{
+  RETURN_VAL_IF_FAIL (function_type, 0, NULL, NULL, "NULL function_type");
+  return function_type->get_param_types ().length ();
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function_type::get_param_types method, in
+   jit-recording.c.  */
+
+gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
+				size_t index)
+{
+  RETURN_NULL_IF_FAIL (function_type, NULL, NULL, "NULL function_type");
+  size_t num_params = function_type->get_param_types ().length ();
+  gcc::jit::recording::context *ctxt = function_type->m_ctxt;
+  RETURN_NULL_IF_FAIL_PRINTF3 (index < num_params,
+			       ctxt, NULL,
+			       "index of %ld is too large (%s has %ld params)",
+			       index,
+			       function_type->get_debug_string (),
+			       num_params);
+  return (gcc_jit_type *)function_type->get_param_types ()[index];
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
@@ -736,6 +932,42 @@ gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
 			   (gcc::jit::recording::field **)fields);
 }
 
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::fields::get_field method in
+   jit-recording.c.  */
+extern gcc_jit_field *
+gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+			   size_t index)
+{
+  RETURN_NULL_IF_FAIL (struct_type, NULL, NULL, "NULL struct type");
+  RETURN_NULL_IF_FAIL (struct_type->get_fields (), NULL, NULL,
+				"NULL struct fields");
+  size_t num_fields = struct_type->get_fields ()->length ();
+  RETURN_NULL_IF_FAIL_PRINTF3 (index < num_fields,
+			       NULL, NULL,
+			       "index of %ld is too large (%s has %ld fields)",
+			       index,
+			       struct_type->get_debug_string (),
+			       num_fields);
+  return (gcc_jit_field *)struct_type->get_fields ()->get_field (index);
+}
+
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, this calls the trivial
+   gcc::jit::recording::struct_::get_fields method in
+   jit-recording.h.  */
+
+size_t
+gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type)
+{
+  RETURN_VAL_IF_FAIL (struct_type, 0, NULL, NULL, "NULL struct type");
+  return struct_type->get_fields ()->length ();
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
@@ -1017,6 +1249,35 @@ gcc_jit_function_get_param (gcc_jit_function *func, int index)
   return static_cast <gcc_jit_param *> (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_VAL_IF_FAIL (func, 0, 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_NULL_IF_FAIL (func, NULL, NULL, "NULL function_type");
+    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 5c722c2c57f..a1c9436c545 100644
--- a/gcc/jit/libgccjit.h
+++ b/gcc/jit/libgccjit.h
@@ -61,6 +61,8 @@ typedef struct gcc_jit_result gcc_jit_result;
 	 +- gcc_jit_location
 	 +- gcc_jit_type
 	    +- gcc_jit_struct
+	    +- gcc_jit_function_type
+	    +- gcc_jit_vector_type
 	 +- gcc_jit_field
 	 +- gcc_jit_function
 	 +- gcc_jit_block
@@ -97,6 +99,12 @@ typedef struct gcc_jit_field gcc_jit_field;
    the layout for, or an opaque type.  */
 typedef struct gcc_jit_struct gcc_jit_struct;
 
+/* A gcc_jit_function_type encapsulates a function type.  */
+typedef struct gcc_jit_function_type gcc_jit_function_type;
+
+/* A gcc_jit_vector_type encapsulates a vector type.  */
+typedef struct gcc_jit_vector_type gcc_jit_vector_type;
+
 /* A gcc_jit_function encapsulates a function: either one that you're
    creating yourself, or a reference to one that you're dynamically
    linking to within the rest of the process.  */
@@ -654,6 +662,15 @@ gcc_jit_struct_set_fields (gcc_jit_struct *struct_type,
 			   int num_fields,
 			   gcc_jit_field **fields);
 
+/* Get a field by index.  */
+extern gcc_jit_field *
+gcc_jit_struct_get_field (gcc_jit_struct *struct_type,
+			   size_t index);
+
+/* Get the number of fields.  */
+extern size_t
+gcc_jit_struct_get_field_count (gcc_jit_struct *struct_type);
+
 /* Unions work similarly to structs.  */
 extern gcc_jit_type *
 gcc_jit_context_new_union_type (gcc_jit_context *ctxt,
@@ -1621,6 +1638,78 @@ gcc_jit_context_add_top_level_asm (gcc_jit_context *ctxt,
 				   gcc_jit_location *loc,
 				   const char *asm_stmts);
 
+#define LIBGCCJIT_HAVE_REFLECTION
+
+/* Reflection functions to get the number of parameters, return type of
+   a function and whether a type is a bool from the C API.
+
+   This API entrypoint was added in LIBGCCJIT_ABI_16; you can test for its
+   presence using
+     #ifdef LIBGCCJIT_HAVE_REFLECTION
+*/
+/* Get the return type of a function.  */
+extern gcc_jit_type *
+gcc_jit_function_get_return_type (gcc_jit_function *func);
+
+/* Get the number of params of a function.  */
+extern size_t
+gcc_jit_function_get_param_count (gcc_jit_function *func);
+
+/* Get the element type of an array type or NULL if it's not an array.  */
+extern gcc_jit_type *
+gcc_jit_type_dyncast_array (gcc_jit_type *type);
+
+/* Return non-zero if the type is a bool.  */
+extern int
+gcc_jit_type_is_bool (gcc_jit_type *type);
+
+/* Return the function type if it is one or NULL.  */
+extern gcc_jit_function_type *
+gcc_jit_type_dyncast_function_ptr_type (gcc_jit_type *type);
+
+/* Given a function type, return its return type.  */
+extern gcc_jit_type *
+gcc_jit_function_type_get_return_type (gcc_jit_function_type *function_type);
+
+/* Given a function type, return its number of parameters.  */
+extern size_t
+gcc_jit_function_type_get_param_count (gcc_jit_function_type *function_type);
+
+/* Given a function type, return the type of the specified parameter.  */
+extern gcc_jit_type *
+gcc_jit_function_type_get_param_type (gcc_jit_function_type *function_type,
+				size_t index);
+
+/* Return non-zero if the type is an integral.  */
+extern int
+gcc_jit_type_is_integral (gcc_jit_type *type);
+
+/* Return the type pointed by the pointer type or NULL if it's not a
+ * pointer.  */
+extern gcc_jit_type *
+gcc_jit_type_is_pointer (gcc_jit_type *type);
+
+/* Given a type, return a dynamic cast to a vector type or NULL.  */
+extern gcc_jit_vector_type *
+gcc_jit_type_dyncast_vector (gcc_jit_type *type);
+
+/* Given a type, return a dynamic cast to a struct type or NULL.  */
+extern gcc_jit_struct *
+gcc_jit_type_is_struct (gcc_jit_type *type);
+
+/* Given a vector type, return the number of units it contains.  */
+extern size_t
+gcc_jit_vector_type_get_num_units (gcc_jit_vector_type *vector_type);
+
+/* Given a vector type, return the type of its elements.  */
+extern gcc_jit_type *
+gcc_jit_vector_type_get_element_type (gcc_jit_vector_type *vector_type);
+
+/* Given a type, return the unqualified type, removing "const", "volatile"
+ * and alignment qualifiers.  */
+extern gcc_jit_type *
+gcc_jit_type_unqualified (gcc_jit_type *type);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map
index 337ea6c7fe4..64e790949e8 100644
--- a/gcc/jit/libgccjit.map
+++ b/gcc/jit/libgccjit.map
@@ -205,3 +205,24 @@ LIBGCCJIT_ABI_15 {
     gcc_jit_extended_asm_add_clobber;
     gcc_jit_context_add_top_level_asm;
 } LIBGCCJIT_ABI_14;
+
+LIBGCCJIT_ABI_16 {
+  global:
+    gcc_jit_function_get_return_type;
+    gcc_jit_function_get_param_count;
+    gcc_jit_function_type_get_return_type;
+    gcc_jit_function_type_get_param_count;
+    gcc_jit_function_type_get_param_type;
+    gcc_jit_type_unqualified;
+    gcc_jit_type_dyncast_array;
+    gcc_jit_type_is_bool;
+    gcc_jit_type_dyncast_function_ptr_type;
+    gcc_jit_type_is_integral;
+    gcc_jit_type_is_pointer;
+    gcc_jit_type_dyncast_vector;
+    gcc_jit_vector_type_get_element_type;
+    gcc_jit_vector_type_get_num_units;
+    gcc_jit_struct_get_field;
+    gcc_jit_type_is_struct;
+    gcc_jit_struct_get_field_count;
+} LIBGCCJIT_ABI_15;
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index 84ef54a0386..a7fddf96db8 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -265,6 +265,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
@@ -434,6 +441,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..112a2455c07
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-reflection.c
@@ -0,0 +1,95 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#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);
+  CHECK (!gcc_jit_type_is_integral(double_type));
+
+  gcc_jit_type *bool_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL);
+  CHECK (gcc_jit_type_is_bool(bool_type));
+  CHECK (!gcc_jit_type_is_integral(bool_type));
+
+  gcc_jit_type *aligned_bool_type =
+    gcc_jit_type_get_aligned(gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_BOOL), 8);
+  CHECK (gcc_jit_type_is_bool(aligned_bool_type));
+  CHECK (bool_type != aligned_bool_type);
+  CHECK_VALUE (gcc_jit_type_unqualified(aligned_bool_type), bool_type);
+
+  CHECK_VALUE (gcc_jit_type_unqualified(gcc_jit_type_get_const(bool_type)), bool_type);
+  CHECK_VALUE (gcc_jit_type_unqualified(gcc_jit_type_get_volatile(bool_type)), bool_type);
+
+  gcc_jit_type *int64 =
+    gcc_jit_context_get_int_type(ctxt, 8, 1);
+  CHECK (gcc_jit_type_is_integral(int64));
+  gcc_jit_type *uint64 =
+    gcc_jit_context_get_int_type(ctxt, 8, 0);
+  CHECK (gcc_jit_type_is_integral(uint64));
+  gcc_jit_type *int8 =
+    gcc_jit_context_get_int_type(ctxt, 1, 1);
+  CHECK (gcc_jit_type_is_integral(int8));
+  gcc_jit_type *uint8 =
+    gcc_jit_context_get_int_type(ctxt, 1, 0);
+  CHECK (gcc_jit_type_is_integral(uint8));
+
+  CHECK (!gcc_jit_type_dyncast_vector(double_type));
+  gcc_jit_type *vec_type = gcc_jit_type_get_vector (double_type, 4);
+  gcc_jit_vector_type *vector_type = gcc_jit_type_dyncast_vector(vec_type);
+  CHECK (vector_type);
+  CHECK (vec_type != double_type);
+  CHECK_VALUE (gcc_jit_vector_type_get_element_type(vector_type), double_type);
+  CHECK_VALUE (gcc_jit_vector_type_get_num_units(vector_type), 4);
+
+  CHECK (!gcc_jit_type_is_pointer(double_type));
+  CHECK_VALUE (gcc_jit_type_is_pointer(gcc_jit_type_get_pointer(double_type)), double_type);
+
+  gcc_jit_type* params[2] = {int8, uint64};
+  gcc_jit_type *function_ptr_type = gcc_jit_context_new_function_ptr_type(ctxt, NULL, int64, 2, params, 0);
+  CHECK (!gcc_jit_type_dyncast_function_ptr_type (int64));
+  gcc_jit_function_type *function_type = gcc_jit_type_dyncast_function_ptr_type (function_ptr_type);
+  CHECK (function_type);
+  int param_count = gcc_jit_function_type_get_param_count(function_type);
+  CHECK_VALUE (param_count, 2);
+  gcc_jit_type *return_type = gcc_jit_function_type_get_return_type(function_type);
+  CHECK_VALUE (return_type, int64);
+  gcc_jit_type *param1 = gcc_jit_function_type_get_param_type(function_type, 0);
+  CHECK_VALUE (param1, int8);
+  gcc_jit_type *param2 = gcc_jit_function_type_get_param_type(function_type, 1);
+  CHECK_VALUE (param2, uint64);
+
+  gcc_jit_field *field1 = gcc_jit_context_new_field (ctxt, NULL, uint64, "field1");
+  gcc_jit_field *field2 = gcc_jit_context_new_field (ctxt, NULL, double_type, "field2");
+  gcc_jit_field *fields[2] = { field1, field2 };
+  gcc_jit_struct *struct_type = gcc_jit_context_new_struct_type (ctxt, NULL, "testStruct", 2, fields);
+  CHECK_VALUE (gcc_jit_struct_get_field_count(struct_type), 2);
+  CHECK_VALUE (gcc_jit_struct_get_field(struct_type, 0), field1);
+  CHECK_VALUE (gcc_jit_struct_get_field(struct_type, 1), field2);
+  CHECK (!gcc_jit_type_is_struct(double_type));
+  gcc_jit_struct *struct_ty = gcc_jit_type_is_struct(gcc_jit_struct_as_type(struct_type));
+  CHECK_VALUE (struct_ty, struct_type);
+
+  CHECK (!gcc_jit_type_dyncast_array(double_type));
+  gcc_jit_type* array_type = gcc_jit_context_new_array_type(ctxt, NULL, double_type, 1);
+  CHECK_VALUE (gcc_jit_type_dyncast_array(array_type), double_type);
+}
+
-- 
2.32.0


  reply	other threads:[~2021-07-19 16:10 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-02  1:01 Antoni Boucher
2020-10-02 20:17 ` David Malcolm
2020-10-02 20:24   ` David Malcolm
2020-10-02 22:39     ` [PATCH] libgccjit: add some reflection functions in the jit C api [PR96889] Antoni Boucher
2020-10-03 18:14     ` [PATCH] libgccjit: add some reflection functions in the jit C api [WIP] Antoni Boucher
2020-10-15 16:02     ` [PATCH] libgccjit: add some reflection functions in the jit C api Antoni Boucher
2020-10-15 16:23       ` Andrea Corallo
2020-10-15 17:39         ` Antoni Boucher
2020-10-15 18:04           ` Andrea Corallo
2020-10-15 21:52           ` David Malcolm
2020-10-17  0:41             ` Antoni Boucher
2020-11-03 22:13             ` Antoni Boucher
2021-05-13  8:33               ` Martin Liška
2021-05-13 21:30               ` David Malcolm
2021-05-14  2:11                 ` Antoni Boucher
2021-05-26  0:19                 ` Antoni Boucher
2021-05-27 22:19                   ` David Malcolm
2021-05-28  1:51                     ` Antoni Boucher
2021-06-10 22:41                       ` David Malcolm
     [not found]                         ` <bc9e81dc3f0a68d6389c9765b5901a5dbd1dcd71.camel@zoho.com>
     [not found]                           ` <e962387aff72bce1ea29b0fc2cb04b84c26f9855.camel@redhat.com>
2021-06-18 15:55                             ` Antoni Boucher
2021-06-18 16:09                               ` David Malcolm
2021-06-18 19:41                                 ` Antoni Boucher
2021-06-18 20:37                                   ` David Malcolm
2021-07-19 16:10                                     ` Antoni Boucher [this message]
2021-07-29 12:59                                       ` Antoni Boucher
2021-08-31 12:34                                         ` Antoni Boucher
2021-09-28  0:53                                     ` Antoni Boucher
2021-10-13  2:09                                       ` Antoni Boucher
2021-10-23 21:20                                         ` SV: " Petter Tomner
2021-11-14 21:30                                         ` Antoni Boucher
2021-11-20  0:53                                       ` David Malcolm
2021-11-26 18:03                                         ` Gerald Pfeifer
2021-11-26 19:51                                           ` Gerald Pfeifer
2021-11-27 16:09                                           ` SV: " Petter Tomner
2021-12-02 18:04                                             ` Gerald Pfeifer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d380f51e0301f46ffcd3ca80fde7f63bdbebf2a5.camel@zoho.com \
    --to=bouanto@zoho.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jit@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).