Skip to content

Commit 8033881

Browse files
Add lvalue::set_name
1 parent 0084a73 commit 8033881

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-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: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,6 +1500,7 @@ class lvalue : public rvalue
15001500
void set_alignment (unsigned bytes);
15011501
unsigned get_alignment () const { return m_alignment; }
15021502
virtual string * get_name () const { return NULL; }
1503+
virtual void set_name (const char *new_name) = 0;
15031504

15041505
protected:
15051506
string *m_link_section;
@@ -1541,6 +1542,9 @@ class param : public lvalue
15411542
const char *access_as_lvalue (reproducer &r) final override;
15421543

15431544
string * get_name () const final override { return m_name; }
1545+
void set_name (const char *new_name) final override {
1546+
m_name = m_ctxt->new_string (new_name);
1547+
}
15441548

15451549
private:
15461550
string * make_debug_string () final override { return m_name; }
@@ -1811,6 +1815,9 @@ class global : public lvalue
18111815
void set_rvalue_init (rvalue *val) { m_rvalue_init = val; }
18121816

18131817
string * get_name () const final override { return m_name; }
1818+
void set_name (const char *new_name) final override {
1819+
m_name = m_ctxt->new_string (new_name);
1820+
}
18141821

18151822
private:
18161823
string * make_debug_string () final override { return m_name; }
@@ -2255,6 +2262,10 @@ class array_access : public lvalue
22552262

22562263
void replay_into (replayer *r) final override;
22572264

2265+
void set_name (const char *name) final override {
2266+
m_ctxt->add_error (NULL, "cannot change the name of type `array_access`");
2267+
}
2268+
22582269
void visit_children (rvalue_visitor *v) final override;
22592270

22602271
private:
@@ -2316,6 +2327,10 @@ class vector_access : public lvalue
23162327

23172328
void visit_children (rvalue_visitor *v) final override;
23182329

2330+
void set_name (const char *name) final override {
2331+
m_ctxt->add_error (NULL, "cannot change the name of type `vector_access`");
2332+
}
2333+
23192334
private:
23202335
string * make_debug_string () final override;
23212336
void write_reproducer (reproducer &r) final override;
@@ -2345,6 +2360,11 @@ class access_field_of_lvalue : public lvalue
23452360

23462361
void visit_children (rvalue_visitor *v) final override;
23472362

2363+
void set_name (const char *name) final override {
2364+
m_ctxt->add_error (
2365+
NULL, "cannot change the name of type `access_field_of_lvalue`");
2366+
}
2367+
23482368
private:
23492369
string * make_debug_string () final override;
23502370
void write_reproducer (reproducer &r) final override;
@@ -2403,6 +2423,11 @@ class dereference_field_rvalue : public lvalue
24032423

24042424
void visit_children (rvalue_visitor *v) final override;
24052425

2426+
void set_name (const char *name) final override {
2427+
m_ctxt->add_error (
2428+
NULL, "cannot change the name of type `dereference_field_rvalue`");
2429+
}
2430+
24062431
private:
24072432
string * make_debug_string () final override;
24082433
void write_reproducer (reproducer &r) final override;
@@ -2429,6 +2454,11 @@ class dereference_rvalue : public lvalue
24292454

24302455
void visit_children (rvalue_visitor *v) final override;
24312456

2457+
void set_name (const char *name) final override {
2458+
m_ctxt->add_error (
2459+
NULL, "cannot change the name of type `dereference_rvalue`");
2460+
}
2461+
24322462
private:
24332463
string * make_debug_string () final override;
24342464
void write_reproducer (reproducer &r) final override;
@@ -2512,6 +2542,11 @@ class local : public lvalue
25122542

25132543
void write_to_dump (dump &d) final override;
25142544

2545+
void set_name (const char *name) final override {
2546+
m_ctxt->add_error (
2547+
NULL, "cannot change the name of type `local`");
2548+
}
2549+
25152550
private:
25162551
string * make_debug_string () final override {
25172552
if (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)