From 188f882bc8d46fe09c8b57f4abf782545fb81075 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 7 Aug 2025 17:51:59 +0200 Subject: [PATCH] Add `lvalue::set_name` --- gcc/jit/docs/topics/compatibility.rst | 7 +++ gcc/jit/docs/topics/expressions.rst | 11 +++- gcc/jit/jit-recording.h | 35 ++++++++++++ gcc/jit/libgccjit.cc | 13 +++++ gcc/jit/libgccjit.h | 6 ++ gcc/jit/libgccjit.map | 5 ++ gcc/testsuite/jit.dg/test-name.c | 81 +++++++++++++++++++++++++++ 7 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/jit.dg/test-name.c diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst index 0f2159072ea8b..790577f9cb686 100644 --- a/gcc/jit/docs/topics/compatibility.rst +++ b/gcc/jit/docs/topics/compatibility.rst @@ -460,3 +460,10 @@ temporary variable: of an lvalue. * :func:`gcc_jit_lvalue_get_name` + +``LIBGCCJIT_ABI_45`` +-------------------- +``LIBGCCJIT_ABI_45`` covers the addition of a function to set the name +of an lvalue. + + * :func:`gcc_jit_lvalue_set_name` diff --git a/gcc/jit/docs/topics/expressions.rst b/gcc/jit/docs/topics/expressions.rst index 2525e8836e468..7e60cd0f3b8c2 100644 --- a/gcc/jit/docs/topics/expressions.rst +++ b/gcc/jit/docs/topics/expressions.rst @@ -929,12 +929,21 @@ where the rvalue is computed by reading from the storage area. Returns the name of an lvalue. This entrypoint was added in :ref:`LIBGCCJIT_ABI_44`; you can test for - its present using + its presence using .. code-block:: c #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_get_name + Sets the name of an lvalue. + + This entrypoint was added in :ref:`LIBGCCJIT_ABI_45`; you can test for + its presence using + + .. code-block:: c + + #ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_name + Global variables **************** diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h index 8ee1987330264..72b2925d3ddfc 100644 --- a/gcc/jit/jit-recording.h +++ b/gcc/jit/jit-recording.h @@ -1500,6 +1500,7 @@ class lvalue : public rvalue void set_alignment (unsigned bytes); unsigned get_alignment () const { return m_alignment; } virtual string * get_name () const { return NULL; } + virtual void set_name (const char *new_name) = 0; protected: string *m_link_section; @@ -1541,6 +1542,9 @@ class param : public lvalue const char *access_as_lvalue (reproducer &r) final override; string * get_name () const final override { return m_name; } + void set_name (const char *new_name) final override { + m_name = m_ctxt->new_string (new_name); + } private: string * make_debug_string () final override { return m_name; } @@ -1811,6 +1815,9 @@ class global : public lvalue void set_rvalue_init (rvalue *val) { m_rvalue_init = val; } string * get_name () const final override { return m_name; } + void set_name (const char *new_name) final override { + m_name = m_ctxt->new_string (new_name); + } private: string * make_debug_string () final override { return m_name; } @@ -2255,6 +2262,10 @@ class array_access : public lvalue void replay_into (replayer *r) final override; + void set_name (const char *new_name) final override { + m_ctxt->add_error (NULL, "cannot change the name of type `array_access`"); + } + void visit_children (rvalue_visitor *v) final override; private: @@ -2316,6 +2327,10 @@ class vector_access : public lvalue void visit_children (rvalue_visitor *v) final override; + void set_name (const char *new_name) final override { + m_ctxt->add_error (NULL, "cannot change the name of type `vector_access`"); + } + private: string * make_debug_string () final override; void write_reproducer (reproducer &r) final override; @@ -2345,6 +2360,11 @@ class access_field_of_lvalue : public lvalue void visit_children (rvalue_visitor *v) final override; + void set_name (const char *new_name) final override { + m_ctxt->add_error ( + NULL, "cannot change the name of type `access_field_of_lvalue`"); + } + private: string * make_debug_string () final override; void write_reproducer (reproducer &r) final override; @@ -2403,6 +2423,11 @@ class dereference_field_rvalue : public lvalue void visit_children (rvalue_visitor *v) final override; + void set_name (const char *new_name) final override { + m_ctxt->add_error ( + NULL, "cannot change the name of type `dereference_field_rvalue`"); + } + private: string * make_debug_string () final override; void write_reproducer (reproducer &r) final override; @@ -2429,6 +2454,11 @@ class dereference_rvalue : public lvalue void visit_children (rvalue_visitor *v) final override; + void set_name (const char *new_name) final override { + m_ctxt->add_error ( + NULL, "cannot change the name of type `dereference_rvalue`"); + } + private: string * make_debug_string () final override; void write_reproducer (reproducer &r) final override; @@ -2512,6 +2542,11 @@ class local : public lvalue void write_to_dump (dump &d) final override; + string * get_name () const final override { return m_name; } + void set_name (const char *new_name) final override { + m_name = m_ctxt->new_string (new_name); + } + private: string * make_debug_string () final override { if (m_name) diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc index 9117e748bbb54..a13522a454996 100644 --- a/gcc/jit/libgccjit.cc +++ b/gcc/jit/libgccjit.cc @@ -4858,6 +4858,19 @@ gcc_jit_lvalue_get_name (gcc_jit_lvalue *lvalue) return name->c_str (); } +/* Public entrypoint. See description in libgccjit.h. + + After error-checking, this calls the trivial + gcc::jit::recording::lvalue::set_name method, in jit-recording.h. */ + +extern void +gcc_jit_lvalue_set_name (gcc_jit_lvalue *lvalue, const char *new_name) +{ + RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue"); + RETURN_IF_FAIL (new_name, NULL, NULL, "NULL new_name"); + lvalue->set_name (new_name); +} + /* Public entrypoint. See description in libgccjit.h. After error-checking, this calls the trivial diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h index 9218ca856a335..bbc508fd7d0d7 100644 --- a/gcc/jit/libgccjit.h +++ b/gcc/jit/libgccjit.h @@ -2242,6 +2242,12 @@ gcc_jit_lvalue_get_name (gcc_jit_lvalue *lvalue); #define LIBGCCJIT_HAVE_gcc_jit_lvalue_get_name +/* Set a new name to the `lvalue`. */ +extern void +gcc_jit_lvalue_set_name (gcc_jit_lvalue *lvalue, const char *new_name); + +#define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_name + extern void gcc_jit_context_set_output_ident (gcc_jit_context *ctxt, const char* output_ident); diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map index 6e5ad4d805a2b..e18df5a886d42 100644 --- a/gcc/jit/libgccjit.map +++ b/gcc/jit/libgccjit.map @@ -383,3 +383,8 @@ LIBGCCJIT_ABI_44{ global: gcc_jit_lvalue_get_name; } LIBGCCJIT_ABI_43; + +LIBGCCJIT_ABI_45{ + global: + gcc_jit_lvalue_set_name; +} LIBGCCJIT_ABI_44; diff --git a/gcc/testsuite/jit.dg/test-name.c b/gcc/testsuite/jit.dg/test-name.c new file mode 100644 index 0000000000000..3e1705c6f84bd --- /dev/null +++ b/gcc/testsuite/jit.dg/test-name.c @@ -0,0 +1,81 @@ +/* { dg-do compile { target x86_64-*-* } } */ + +#include +#include + +#include "libgccjit.h" + +// We want the debug info to check the function variable's name. +#define TEST_ESCHEWS_SET_OPTIONS +static void set_options (gcc_jit_context *ctxt, const char *argv0) +{ + gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DEBUGINFO, 1); +} + +#define TEST_COMPILING_TO_FILE +#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER +#define OUTPUT_FILENAME "output-of-test-name.c.s" +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + /* Let's try to inject the equivalent of: + +int original_foo = 10; + */ + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_lvalue *foo = + gcc_jit_context_new_global (ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED, + int_type, "original_foo"); + gcc_jit_rvalue *ten = gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10); + gcc_jit_global_set_initializer_rvalue (foo, ten); + + CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (foo), "original_foo"); + gcc_jit_lvalue_set_name (foo, "new_one"); + CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (foo), "new_one"); + + /* Let's try to inject te equivalent of: +int blabla() { + int the_var = 12; + + return the_var; +} + */ + gcc_jit_function *blabla_func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + int_type, + "blabla", + 0, NULL, + 0); + + gcc_jit_block *blabla_block = gcc_jit_function_new_block (blabla_func, NULL); + + /* Build locals: */ + gcc_jit_lvalue *the_var = + gcc_jit_function_new_local (blabla_func, NULL, int_type, "the_var"); + + /* int the_var = 0; */ + gcc_jit_block_add_assignment ( + blabla_block, NULL, + the_var, + gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0)); + + gcc_jit_block_end_with_return ( + blabla_block, NULL, + gcc_jit_lvalue_as_rvalue (the_var)); + + CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (the_var), "the_var"); + gcc_jit_lvalue_set_name (the_var, "confiture"); + CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (the_var), "confiture"); +} + +/* { dg-final { jit-verify-output-file-was-created "" } } */ +/* Check that the global was correctly renamed */ +/* { dg-final { jit-verify-assembler-output-not "original_foo:" } } */ +/* { dg-final { jit-verify-assembler-output "new_one:" } } */ + +/* { dg-final { jit-verify-assembler-output-not ".string\\s+\"the_var\"" } } */ +/* { dg-final { jit-verify-assembler-output ".string\\s+\"confiture\"" } } */