Skip to content

Commit 3d4552d

Browse files
Add lvalue::set_name
1 parent 0084a73 commit 3d4552d

File tree

7 files changed

+90
-1
lines changed

7 files changed

+90
-1
lines changed

gcc/jit/docs/topics/compatibility.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,3 +460,10 @@ temporary variable:
460460
of an lvalue.
461461

462462
* :func:`gcc_jit_lvalue_get_name`
463+
464+
``LIBGCCJIT_ABI_45``
465+
--------------------
466+
``LIBGCCJIT_ABI_45`` covers the addition of a function to set the name
467+
of an lvalue.
468+
469+
* :func:`gcc_jit_lvalue_set_name`

gcc/jit/docs/topics/expressions.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,12 +929,21 @@ where the rvalue is computed by reading from the storage area.
929929
Returns the name of an lvalue.
930930

931931
This entrypoint was added in :ref:`LIBGCCJIT_ABI_44`; you can test for
932-
its present using
932+
its presence using
933933

934934
.. code-block:: c
935935
936936
#ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_get_name
937937
938+
Sets the name of an lvalue.
939+
940+
This entrypoint was added in :ref:`LIBGCCJIT_ABI_45`; you can test for
941+
its presence using
942+
943+
.. code-block:: c
944+
945+
#ifdef LIBGCCJIT_HAVE_gcc_jit_lvalue_set_name
946+
938947
Global variables
939948
****************
940949

gcc/jit/jit-recording.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,9 @@ class compound_type : public type
12271227
string *name);
12281228

12291229
string *get_name () const { return m_name; }
1230+
void set_name (const char *new_name) {
1231+
m_name = m_ctxt->new_string (new_name);
1232+
}
12301233
location *get_loc () const { return m_loc; }
12311234
fields * get_fields () { return m_fields; }
12321235

@@ -1500,6 +1503,7 @@ class lvalue : public rvalue
15001503
void set_alignment (unsigned bytes);
15011504
unsigned get_alignment () const { return m_alignment; }
15021505
virtual string * get_name () const { return NULL; }
1506+
virtual void set_name (const char *new_name) {}
15031507

15041508
protected:
15051509
string *m_link_section;
@@ -1541,6 +1545,9 @@ class param : public lvalue
15411545
const char *access_as_lvalue (reproducer &r) final override;
15421546

15431547
string * get_name () const final override { return m_name; }
1548+
void set_name (const char *new_name) final override {
1549+
m_name = m_ctxt->new_string (new_name);
1550+
}
15441551

15451552
private:
15461553
string * make_debug_string () final override { return m_name; }
@@ -1594,6 +1601,9 @@ class function : public memento
15941601
void set_loc (location * loc) { m_loc = loc; }
15951602
type *get_return_type () const { return m_return_type; }
15961603
string * get_name () const { return m_name; }
1604+
void set_name (const char *new_name) {
1605+
m_name = m_ctxt->new_string (new_name);
1606+
}
15971607
const vec<param *> &get_params () const { return m_params; }
15981608

15991609
/* Get the given param by index.
@@ -1811,6 +1821,9 @@ class global : public lvalue
18111821
void set_rvalue_init (rvalue *val) { m_rvalue_init = val; }
18121822

18131823
string * get_name () const final override { return m_name; }
1824+
void set_name (const char *new_name) final override {
1825+
m_name = m_ctxt->new_string (new_name);
1826+
}
18141827

18151828
private:
18161829
string * make_debug_string () final override { return m_name; }

gcc/jit/libgccjit.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4858,6 +4858,19 @@ gcc_jit_lvalue_get_name (gcc_jit_lvalue *lvalue)
48584858
return name->c_str ();
48594859
}
48604860

4861+
/* Public entrypoint. See description in libgccjit.h.
4862+
4863+
After error-checking, this calls the trivial
4864+
gcc::jit::recording::lvalue::set_name method, in jit-recording.h. */
4865+
4866+
extern void
4867+
gcc_jit_lvalue_set_name (gcc_jit_lvalue *lvalue, const char *new_name)
4868+
{
4869+
RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
4870+
RETURN_IF_FAIL (new_name, NULL, NULL, "NULL new_name");
4871+
lvalue->set_name (new_name);
4872+
}
4873+
48614874
/* Public entrypoint. See description in libgccjit.h.
48624875
48634876
After error-checking, this calls the trivial

gcc/jit/libgccjit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,6 +2242,12 @@ gcc_jit_lvalue_get_name (gcc_jit_lvalue *lvalue);
22422242

22432243
#define LIBGCCJIT_HAVE_gcc_jit_lvalue_get_name
22442244

2245+
/* Set a new name to the `lvalue`. */
2246+
extern void
2247+
gcc_jit_lvalue_set_name (gcc_jit_lvalue *lvalue, const char *new_name);
2248+
2249+
#define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_name
2250+
22452251
extern void
22462252
gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,
22472253
const char* output_ident);

gcc/jit/libgccjit.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,8 @@ LIBGCCJIT_ABI_44{
383383
global:
384384
gcc_jit_lvalue_get_name;
385385
} LIBGCCJIT_ABI_43;
386+
387+
LIBGCCJIT_ABI_45{
388+
global:
389+
gcc_jit_lvalue_set_name;
390+
} LIBGCCJIT_ABI_44;

gcc/testsuite/jit.dg/test-name.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* { dg-do compile { target x86_64-*-* } } */
2+
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
#include "libgccjit.h"
7+
8+
#define TEST_COMPILING_TO_FILE
9+
#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
10+
#define OUTPUT_FILENAME "output-of-test-name.c.s"
11+
#include "harness.h"
12+
13+
void
14+
create_code (gcc_jit_context *ctxt, void *user_data)
15+
{
16+
/* Let's try to inject the equivalent of:
17+
18+
int original_foo = 10;
19+
*/
20+
gcc_jit_type *int_type =
21+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
22+
gcc_jit_lvalue *foo =
23+
gcc_jit_context_new_global (ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
24+
int_type, "original_foo");
25+
gcc_jit_rvalue *ten = gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10);
26+
gcc_jit_global_set_initializer_rvalue (foo, ten);
27+
28+
CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (foo), "original_foo");
29+
gcc_jit_lvalue_set_name (foo, "new_one");
30+
CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (foo), "new_one");
31+
}
32+
33+
/* { dg-final { jit-verify-output-file-was-created "" } } */
34+
/* Check that the global was correctly renamed */
35+
/* { dg-final { jit-verify-assembler-output-not "original_foo:" } } */
36+
/* { dg-final { jit-verify-assembler-output "new_one:" } } */

0 commit comments

Comments
 (0)