Skip to content

Commit b187598

Browse files
GuillaumeGomezantoyo
authored andcommitted
Add lvalue::set_name (#78)
1 parent b1c729d commit b187598

File tree

7 files changed

+157
-1
lines changed

7 files changed

+157
-1
lines changed

gcc/jit/docs/topics/compatibility.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,10 @@ information:
473473
of an lvalue.
474474

475475
* :func:`gcc_jit_lvalue_get_name`
476+
477+
``LIBGCCJIT_ABI_45``
478+
--------------------
479+
``LIBGCCJIT_ABI_45`` covers the addition of a function to set the name
480+
of an lvalue.
481+
482+
* :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
@@ -1512,6 +1512,7 @@ class lvalue : public rvalue
15121512
void set_alignment (unsigned bytes);
15131513
unsigned get_alignment () const { return m_alignment; }
15141514
virtual string * get_name () const { return NULL; }
1515+
virtual void set_name (const char *new_name) = 0;
15151516

15161517
protected:
15171518
string *m_link_section;
@@ -1553,6 +1554,9 @@ class param : public lvalue
15531554
const char *access_as_lvalue (reproducer &r) final override;
15541555

15551556
string * get_name () const final override { return m_name; }
1557+
void set_name (const char *new_name) final override {
1558+
m_name = m_ctxt->new_string (new_name);
1559+
}
15561560

15571561
private:
15581562
string * make_debug_string () final override { return m_name; }
@@ -1823,6 +1827,9 @@ class global : public lvalue
18231827
void set_rvalue_init (rvalue *val) { m_rvalue_init = val; }
18241828

18251829
string * get_name () const final override { return m_name; }
1830+
void set_name (const char *new_name) final override {
1831+
m_name = m_ctxt->new_string (new_name);
1832+
}
18261833

18271834
private:
18281835
string * make_debug_string () final override { return m_name; }
@@ -2267,6 +2274,10 @@ class array_access : public lvalue
22672274

22682275
void replay_into (replayer *r) final override;
22692276

2277+
void set_name (const char *new_name) final override {
2278+
m_ctxt->add_error (NULL, "cannot change the name of type `array_access`");
2279+
}
2280+
22702281
void visit_children (rvalue_visitor *v) final override;
22712282

22722283
private:
@@ -2328,6 +2339,10 @@ class vector_access : public lvalue
23282339

23292340
void visit_children (rvalue_visitor *v) final override;
23302341

2342+
void set_name (const char *new_name) final override {
2343+
m_ctxt->add_error (NULL, "cannot change the name of type `vector_access`");
2344+
}
2345+
23312346
private:
23322347
string * make_debug_string () final override;
23332348
void write_reproducer (reproducer &r) final override;
@@ -2357,6 +2372,11 @@ class access_field_of_lvalue : public lvalue
23572372

23582373
void visit_children (rvalue_visitor *v) final override;
23592374

2375+
void set_name (const char *new_name) final override {
2376+
m_ctxt->add_error (
2377+
NULL, "cannot change the name of type `access_field_of_lvalue`");
2378+
}
2379+
23602380
private:
23612381
string * make_debug_string () final override;
23622382
void write_reproducer (reproducer &r) final override;
@@ -2415,6 +2435,11 @@ class dereference_field_rvalue : public lvalue
24152435

24162436
void visit_children (rvalue_visitor *v) final override;
24172437

2438+
void set_name (const char *new_name) final override {
2439+
m_ctxt->add_error (
2440+
NULL, "cannot change the name of type `dereference_field_rvalue`");
2441+
}
2442+
24182443
private:
24192444
string * make_debug_string () final override;
24202445
void write_reproducer (reproducer &r) final override;
@@ -2441,6 +2466,11 @@ class dereference_rvalue : public lvalue
24412466

24422467
void visit_children (rvalue_visitor *v) final override;
24432468

2469+
void set_name (const char *new_name) final override {
2470+
m_ctxt->add_error (
2471+
NULL, "cannot change the name of type `dereference_rvalue`");
2472+
}
2473+
24442474
private:
24452475
string * make_debug_string () final override;
24462476
void write_reproducer (reproducer &r) final override;
@@ -2524,6 +2554,11 @@ class local : public lvalue
25242554

25252555
void write_to_dump (dump &d) final override;
25262556

2557+
string * get_name () const final override { return m_name; }
2558+
void set_name (const char *new_name) final override {
2559+
m_name = m_ctxt->new_string (new_name);
2560+
}
2561+
25272562
private:
25282563
string * make_debug_string () final override {
25292564
if (m_name)

gcc/jit/libgccjit.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4865,6 +4865,19 @@ gcc_jit_lvalue_get_name (gcc_jit_lvalue *lvalue)
48654865
return name->c_str ();
48664866
}
48674867

4868+
/* Public entrypoint. See description in libgccjit.h.
4869+
4870+
After error-checking, this calls the trivial
4871+
gcc::jit::recording::lvalue::set_name method, in jit-recording.h. */
4872+
4873+
extern void
4874+
gcc_jit_lvalue_set_name (gcc_jit_lvalue *lvalue, const char *new_name)
4875+
{
4876+
RETURN_IF_FAIL (lvalue, NULL, NULL, "NULL lvalue");
4877+
RETURN_IF_FAIL (new_name, NULL, NULL, "NULL new_name");
4878+
lvalue->set_name (new_name);
4879+
}
4880+
48684881
/* Public entrypoint. See description in libgccjit.h.
48694882
48704883
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
@@ -2296,6 +2296,12 @@ gcc_jit_lvalue_get_name (gcc_jit_lvalue *lvalue);
22962296

22972297
#define LIBGCCJIT_HAVE_gcc_jit_lvalue_get_name
22982298

2299+
/* Set a new name to the `lvalue`. */
2300+
extern void
2301+
gcc_jit_lvalue_set_name (gcc_jit_lvalue *lvalue, const char *new_name);
2302+
2303+
#define LIBGCCJIT_HAVE_gcc_jit_lvalue_set_name
2304+
22992305
extern void
23002306
gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,
23012307
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: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* { dg-do compile { target x86_64-*-* } } */
2+
3+
#include <stdlib.h>
4+
#include <stdio.h>
5+
6+
#include "libgccjit.h"
7+
8+
// We want the debug info to check the function variable's name.
9+
#define TEST_ESCHEWS_SET_OPTIONS
10+
static void set_options (gcc_jit_context *ctxt, const char *argv0)
11+
{
12+
gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DEBUGINFO, 1);
13+
}
14+
15+
#define TEST_COMPILING_TO_FILE
16+
#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
17+
#define OUTPUT_FILENAME "output-of-test-name.c.s"
18+
#include "harness.h"
19+
20+
void
21+
create_code (gcc_jit_context *ctxt, void *user_data)
22+
{
23+
/* Let's try to inject the equivalent of:
24+
25+
int original_foo = 10;
26+
*/
27+
gcc_jit_type *int_type =
28+
gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
29+
gcc_jit_lvalue *foo =
30+
gcc_jit_context_new_global (ctxt, NULL, GCC_JIT_GLOBAL_EXPORTED,
31+
int_type, "original_foo");
32+
gcc_jit_rvalue *ten = gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10);
33+
gcc_jit_global_set_initializer_rvalue (foo, ten);
34+
35+
CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (foo), "original_foo");
36+
gcc_jit_lvalue_set_name (foo, "new_one");
37+
CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (foo), "new_one");
38+
39+
/* Let's try to inject te equivalent of:
40+
int blabla() {
41+
int the_var = 12;
42+
43+
return the_var;
44+
}
45+
*/
46+
gcc_jit_function *blabla_func =
47+
gcc_jit_context_new_function (ctxt, NULL,
48+
GCC_JIT_FUNCTION_EXPORTED,
49+
int_type,
50+
"blabla",
51+
0, NULL,
52+
0);
53+
54+
gcc_jit_block *blabla_block = gcc_jit_function_new_block (blabla_func, NULL);
55+
56+
/* Build locals: */
57+
gcc_jit_lvalue *the_var =
58+
gcc_jit_function_new_local (blabla_func, NULL, int_type, "the_var");
59+
60+
/* int the_var = 0; */
61+
gcc_jit_block_add_assignment (
62+
blabla_block, NULL,
63+
the_var,
64+
gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 0));
65+
66+
gcc_jit_block_end_with_return (
67+
blabla_block, NULL,
68+
gcc_jit_lvalue_as_rvalue (the_var));
69+
70+
CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (the_var), "the_var");
71+
gcc_jit_lvalue_set_name (the_var, "confiture");
72+
CHECK_STRING_VALUE (gcc_jit_lvalue_get_name (the_var), "confiture");
73+
}
74+
75+
/* { dg-final { jit-verify-output-file-was-created "" } } */
76+
/* Check that the global was correctly renamed */
77+
/* { dg-final { jit-verify-assembler-output-not "original_foo:" } } */
78+
/* { dg-final { jit-verify-assembler-output "new_one:" } } */
79+
80+
/* { dg-final { jit-verify-assembler-output-not ".string\\s+\"the_var\"" } } */
81+
/* { dg-final { jit-verify-assembler-output ".string\\s+\"confiture\"" } } */

0 commit comments

Comments
 (0)