Skip to content

Commit 4e995bd

Browse files
Add lvalue::set_name (#78)
1 parent 0084a73 commit 4e995bd

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
@@ -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 *new_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 *new_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 *new_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 *new_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 *new_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+
string * get_name () const final override { return m_name; }
2546+
void set_name (const char *new_name) final override {
2547+
m_name = m_ctxt->new_string (new_name);
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: 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)