Skip to content

Commit 919b622

Browse files
committed
Add function to set the initial value of a global variable [PR96089]
TODO: fail if global kind is imported TODO: doc 2021-05-20 Antoni Boucher <[email protected]> gcc/jit/ PR target/96089 * docs/topics/compatibility.rst (LIBGCCJIT_ABI_19): New ABI tag. * docs/topics/expressions.rst: Add documentation for the function gcc_jit_global_set_initializer_value. * jit-playback.c: New functions (new_global_with_value, set_global_initial_value, new_rvalue_from_struct, new_rvalue_from_array). * jit-playback.h: New functions (new_global_with_value, set_global_initial_value, new_rvalue_from_struct, new_rvalue_from_array). * jit-recording.c: Add support for setting a value to a global variable and new methods (global_initializer::write_reproducer, global_initializer::make_debug_string, global_initializer::write_to_dump, global_initializer::replay_into, context::new_global_value_initializer, memento_of_new_rvalue_from_struct::write_reproducer, memento_of_new_rvalue_from_struct::make_debug_string, memento_of_new_rvalue_from_struct::visit_children, memento_of_new_rvalue_from_struct::replay_into, memento_of_new_rvalue_from_struct:: memento_of_new_rvalue_from_struct, context::new_rvalue_from_struct, memento_of_new_rvalue_from_array::write_reproducer, memento_of_new_rvalue_from_array::make_debug_string, memento_of_new_rvalue_from_array::visit_children, memento_of_new_rvalue_from_array::replay_into, memento_of_new_rvalue_from_array:: memento_of_new_rvalue_from_array, new_rvalue_from_array). * jit-recording.h: New functions (set_initializer_value, new_global_value_initializer, new_rvalue_from_struct, new_rvalue_from_array), new field m_initializer_value and new classes (global_initializer, memento_of_new_rvalue_from_struct, memento_of_new_rvalue_from_array). * libgccjit.c: New macro RETURN_IF_FAIL_PRINTF5 and new functions (gcc_jit_global_set_initializer_value, gcc_jit_context_new_rvalue_from_struct, gcc_jit_context_new_rvalue_from_array). * libgccjit.h: New functions (gcc_jit_global_set_initializer_value, gcc_jit_context_new_rvalue_from_struct, gcc_jit_context_new_rvalue_from_array). * libgccjit.map (LIBGCCJIT_ABI_19): New ABI tag. gcc/testsuite/ PR target/96089 * jit.dg/test-global-set-initializer.c: Add test for the new function (gcc_jit_global_set_initializer_value).
1 parent 6cce3df commit 919b622

11 files changed

+712
-0
lines changed

gcc/jit/docs/topics/compatibility.rst

+9
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,12 @@ thread-local storage model of a variable:
302302
section of a variable:
303303

304304
* :func:`gcc_jit_lvalue_set_link_section`
305+
306+
.. _LIBGCCJIT_ABI_19:
307+
308+
``LIBGCCJIT_ABI_19``
309+
-----------------------
310+
``LIBGCCJIT_ABI_19`` covers the addition of an API entrypoint to set the value
311+
of a global variable:
312+
313+
* :func:`gcc_jit_global_set_initializer_value`

gcc/jit/docs/topics/expressions.rst

+14
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,20 @@ Global variables
643643
644644
#ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
645645
646+
.. function:: void
647+
gcc_jit_global_set_initializer_value (gcc_jit_lvalue *global,\
648+
gcc_jit_rvalue *value)
649+
650+
Set an initializer for ``global`` using the specified value.
651+
``global`` must be the same type as ``value``.
652+
653+
This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for
654+
its presence using
655+
656+
.. code-block:: c
657+
658+
#ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer_value
659+
646660
Working with pointers, structs and unions
647661
-----------------------------------------
648662

gcc/jit/jit-common.h

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ namespace recording {
117117
class compound_type;
118118
class struct_;
119119
class union_;
120+
class array_type;
120121
class vector_type;
121122
class field;
122123
class bitfield;

gcc/jit/jit-playback.c

+73
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,39 @@ new_global_initialized (location *loc,
702702
return global_finalize_lvalue (inner);
703703
}
704704

705+
playback::lvalue *
706+
playback::context::
707+
new_global_with_value (location *loc,
708+
enum gcc_jit_global_kind kind,
709+
type *type,
710+
playback::rvalue *value,
711+
const char *name)
712+
{
713+
tree inner = global_new_decl (loc, kind, type, name);
714+
715+
tree inner_type = type->as_tree ();
716+
tree initial = value->as_tree ();
717+
gcc_assert (TREE_CONSTANT (initial));
718+
DECL_INITIAL (inner) = initial;
719+
720+
return global_finalize_lvalue (inner);
721+
}
722+
723+
void
724+
playback::context::
725+
set_global_initial_value (playback::lvalue *global,
726+
playback::rvalue *value)
727+
{
728+
tree initial = value->as_tree ();
729+
if (!TREE_CONSTANT(initial))
730+
{
731+
add_error (NULL, "initial value for global is not a constant");
732+
debug_tree (initial);
733+
gcc_assert(TREE_CONSTANT(initial));
734+
}
735+
DECL_INITIAL (global->as_tree ()) = initial;
736+
}
737+
705738
/* Implementation of the various
706739
gcc::jit::playback::context::new_rvalue_from_const <HOST_TYPE>
707740
methods.
@@ -852,6 +885,46 @@ playback::context::new_rvalue_from_vector (location *,
852885
return new rvalue (this, t_ctor);
853886
}
854887

888+
/* Construct a playback::rvalue instance (wrapping a tree) for a
889+
struct. */
890+
891+
playback::rvalue *
892+
playback::context::new_rvalue_from_struct (location *,
893+
type *type,
894+
const auto_vec<rvalue *> &fields)
895+
{
896+
vec<constructor_elt, va_gc> *v;
897+
vec_alloc (v, fields.length ());
898+
tree field_decl = TYPE_FIELDS (type->as_tree ());
899+
for (unsigned i = 0; i < fields.length (); ++i)
900+
{
901+
CONSTRUCTOR_APPEND_ELT (v, field_decl, fields[i]->as_tree ());
902+
field_decl = TREE_CHAIN (field_decl);
903+
}
904+
905+
tree t_ctor = build_constructor (type->as_tree (), v);
906+
return new rvalue (this, t_ctor);
907+
}
908+
909+
/* Construct a playback::rvalue instance (wrapping a tree) for a
910+
array. */
911+
912+
playback::rvalue *
913+
playback::context::new_rvalue_from_array (location *,
914+
type *type,
915+
const auto_vec<rvalue *> &elements)
916+
{
917+
vec<constructor_elt, va_gc> *v;
918+
vec_alloc (v, elements.length ());
919+
for (unsigned i = 0; i < elements.length (); ++i)
920+
{
921+
tree index = build_int_cst (long_unsigned_type_node, i);
922+
CONSTRUCTOR_APPEND_ELT (v, index, elements[i]->as_tree ());
923+
}
924+
tree t_ctor = build_constructor (type->as_tree (), v);
925+
return new rvalue (this, t_ctor);
926+
}
927+
855928
/* Coerce a tree expression into a boolean tree expression. */
856929

857930
tree

gcc/jit/jit-playback.h

+21
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ class context : public log_user
121121
const void *initializer,
122122
const char *name);
123123

124+
lvalue*
125+
new_global_with_value (location *loc,
126+
enum gcc_jit_global_kind kind,
127+
type *type,
128+
rvalue *value,
129+
const char *name);
130+
131+
void
132+
set_global_initial_value (playback::lvalue *global,
133+
playback::rvalue *value);
134+
124135
template <typename HOST_TYPE>
125136
rvalue *
126137
new_rvalue_from_const (type *type,
@@ -134,6 +145,16 @@ class context : public log_user
134145
type *type,
135146
const auto_vec<rvalue *> &elements);
136147

148+
rvalue *
149+
new_rvalue_from_struct (location *loc,
150+
type *type,
151+
const auto_vec<rvalue *> &fields);
152+
153+
rvalue *
154+
new_rvalue_from_array (location *loc,
155+
type *type,
156+
const auto_vec<rvalue *> &elements);
157+
137158
rvalue *
138159
new_unary_op (location *loc,
139160
enum gcc_jit_unary_op op,

0 commit comments

Comments
 (0)