Skip to content

Commit cf544af

Browse files
committed
libgccjit: Add support for setting the comment ident
gcc/jit/ChangeLog: * docs/topics/compatibility.rst (LIBGCCJIT_ABI_34): New ABI tag. * docs/topics/contexts.rst: Document gcc_jit_context_set_output_ident. * jit-playback.cc (set_output_ident): New method. * jit-playback.h (set_output_ident): New method. * jit-recording.cc (recording::context::set_output_ident, recording::output_ident::output_ident, recording::output_ident::~output_ident, recording::output_ident::replay_into, recording::output_ident::make_debug_string, recording::output_ident::write_reproducer): New methods. * jit-recording.h (class output_ident): New class. * libgccjit.cc (gcc_jit_context_set_output_ident): New function. * libgccjit.h (gcc_jit_context_set_output_ident): New function. * libgccjit.map: New function. gcc/testsuite/ChangeLog: * jit.dg/all-non-failing-tests.h: New test. * jit.dg/test-output-ident.c: New test.
1 parent d8cf891 commit cf544af

File tree

11 files changed

+175
-0
lines changed

11 files changed

+175
-0
lines changed

gcc/jit/docs/topics/compatibility.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,11 @@ on functions and variables:
445445
temporary variable:
446446

447447
* :func:`gcc_jit_function_new_temp`
448+
449+
.. _LIBGCCJIT_ABI_34:
450+
451+
``LIBGCCJIT_ABI_34``
452+
--------------------
453+
``LIBGCCJIT_ABI_34`` covers the addition of
454+
455+
* :func:`gcc_jit_context_set_output_ident`

gcc/jit/docs/topics/contexts.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,3 +604,32 @@ Additional command-line options
604604
.. code-block:: c
605605
606606
#ifdef LIBGCCJIT_HAVE_gcc_jit_context_add_driver_option
607+
608+
Output options
609+
**************
610+
611+
.. function:: void gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,\
612+
const char* output_ident)
613+
614+
Set the identifier to write in the .comment section of the output file to
615+
``output_ident``.
616+
617+
The parameter ``output_ident`` must be non-NULL.
618+
619+
This only works on some target, as you can see here:
620+
https://gcc.gnu.org/onlinedocs/cpp/Other-Directives.html
621+
622+
Analogous to:
623+
624+
.. code-block:: c
625+
626+
#ident "My comment"
627+
628+
in C.
629+
630+
This entrypoint was added in :ref:`LIBGCCJIT_ABI_34`; you can test for
631+
its presence using
632+
633+
.. code-block:: c
634+
635+
#ifdef LIBGCCJIT_HAVE_gcc_jit_context_set_output_ident

gcc/jit/jit-playback.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ get_type (enum gcc_jit_types type_)
329329
return new type (type_node);
330330
}
331331

332+
void
333+
playback::context::
334+
set_output_ident (const char* ident)
335+
{
336+
targetm.asm_out.output_ident (ident);
337+
}
338+
332339
/* Construct a playback::type instance (wrapping a tree) for the given
333340
array type. */
334341

gcc/jit/jit-playback.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ class context : public log_user
7777
type *
7878
get_type (enum gcc_jit_types type);
7979

80+
void
81+
set_output_ident (const char* ident);
82+
8083
type *
8184
new_array_type (location *loc,
8285
type *element_type,

gcc/jit/jit-recording.cc

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,13 @@ recording::context::get_str_option (enum gcc_jit_str_option opt)
14861486
return m_str_options[opt];
14871487
}
14881488

1489+
void
1490+
recording::context::set_output_ident (const char *ident)
1491+
{
1492+
recording::output_ident *memento = new output_ident (this, ident);
1493+
record (memento);
1494+
}
1495+
14891496
/* Set the given integer option for this context, or add an error if
14901497
it's not recognized.
14911498
@@ -2326,6 +2333,52 @@ recording::string::write_reproducer (reproducer &)
23262333
/* Empty. */
23272334
}
23282335

2336+
/* The implementation of class gcc::jit::recording::output_ident. */
2337+
2338+
/* Constructor for gcc::jit::recording::output_ident, allocating a
2339+
copy of the given text using new char[]. */
2340+
2341+
recording::output_ident::output_ident (context *ctxt, const char *ident)
2342+
: memento (ctxt)
2343+
{
2344+
m_ident = ident ? xstrdup (ident) : NULL;
2345+
}
2346+
2347+
/* Destructor for gcc::jit::recording::output_ident. */
2348+
2349+
recording::output_ident::~output_ident ()
2350+
{
2351+
free (m_ident);
2352+
}
2353+
2354+
/* Implementation of pure virtual hook recording::memento::replay_into
2355+
for recording::output_ident. */
2356+
2357+
void
2358+
recording::output_ident::replay_into (replayer *r)
2359+
{
2360+
r->set_output_ident (m_ident);
2361+
}
2362+
2363+
/* Implementation of recording::memento::make_debug_string for output_ident. */
2364+
2365+
recording::string *
2366+
recording::output_ident::make_debug_string ()
2367+
{
2368+
return m_ctxt->new_string (m_ident);
2369+
}
2370+
2371+
/* Implementation of recording::memento::write_reproducer for output_ident. */
2372+
2373+
void
2374+
recording::output_ident::write_reproducer (reproducer &r)
2375+
{
2376+
r.write (" gcc_jit_context_set_output_ident (%s, \"%s\");",
2377+
r.get_identifier (get_context ()),
2378+
m_ident);
2379+
}
2380+
2381+
23292382
/* The implementation of class gcc::jit::recording::location. */
23302383

23312384
/* Implementation of recording::memento::replay_into for locations.

gcc/jit/jit-recording.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ class context : public log_user
266266
const char*
267267
get_str_option (enum gcc_jit_str_option opt);
268268

269+
void
270+
set_output_ident (const char *output_ident);
271+
269272
void
270273
set_int_option (enum gcc_jit_int_option opt,
271274
int value);
@@ -505,6 +508,25 @@ class string : public memento
505508
bool m_escaped;
506509
};
507510

511+
class output_ident : public memento
512+
{
513+
public:
514+
output_ident (context *ctxt, const char *text);
515+
~output_ident ();
516+
517+
void replay_into (replayer *) final override;
518+
519+
output_ident (const output_ident&) = delete;
520+
output_ident& operator= (const output_ident&) = delete;
521+
522+
private:
523+
string * make_debug_string () final override;
524+
void write_reproducer (reproducer &r) final override;
525+
526+
private:
527+
char *m_ident;
528+
};
529+
508530
class location : public memento
509531
{
510532
public:

gcc/jit/libgccjit.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,6 +3883,22 @@ gcc_jit_context_compile_to_file (gcc_jit_context *ctxt,
38833883
ctxt->compile_to_file (output_kind, output_path);
38843884
}
38853885

3886+
/* Public entrypoint. See description in libgccjit.h.
3887+
3888+
After error-checking, the real work is done by the
3889+
gcc::jit::recording::context::set_str_option method in
3890+
jit-recording.cc. */
3891+
3892+
void
3893+
gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,
3894+
const char* output_ident)
3895+
{
3896+
RETURN_IF_FAIL (ctxt, NULL, NULL, "NULL context");
3897+
RETURN_IF_FAIL (output_ident, ctxt, NULL, "NULL output_ident");
3898+
JIT_LOG_FUNC (ctxt->get_logger ());
3899+
3900+
ctxt->set_output_ident (output_ident);
3901+
}
38863902

38873903
/* Public entrypoint. See description in libgccjit.h.
38883904

gcc/jit/libgccjit.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,12 @@ gcc_jit_lvalue_add_string_attribute (gcc_jit_lvalue *variable,
21532153
enum gcc_jit_variable_attribute attribute,
21542154
const char* value);
21552155

2156+
extern void
2157+
gcc_jit_context_set_output_ident (gcc_jit_context *ctxt,
2158+
const char* output_ident);
2159+
2160+
#define LIBGCCJIT_HAVE_gcc_jit_context_set_output_ident
2161+
21562162
#ifdef __cplusplus
21572163
}
21582164
#endif /* __cplusplus */

gcc/jit/libgccjit.map

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,3 +320,8 @@ LIBGCCJIT_ABI_33 {
320320
global:
321321
gcc_jit_function_new_temp;
322322
} LIBGCCJIT_ABI_32;
323+
324+
LIBGCCJIT_ABI_34 {
325+
global:
326+
gcc_jit_context_set_output_ident;
327+
} LIBGCCJIT_ABI_33;

gcc/testsuite/jit.dg/all-non-failing-tests.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,9 @@
283283
#undef create_code
284284
#undef verify_code
285285

286+
/* test-output-ident.c: This can't be in the testcases array as it
287+
is target-specific. */
288+
286289
/* test-quadratic.c */
287290
#define create_code create_code_quadratic
288291
#define verify_code verify_code_quadratic

0 commit comments

Comments
 (0)