]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/doc/gccint/memory-management-and-type-information/the-inside-of-a-gty.rst
sphinx: add missing trailing newline
[thirdparty/gcc.git] / gcc / doc / gccint / memory-management-and-type-information / the-inside-of-a-gty.rst
1 ..
2 Copyright 1988-2022 Free Software Foundation, Inc.
3 This is part of the GCC manual.
4 For copying conditions, see the copyright.rst file.
5
6 .. _gty-options:
7
8 The Inside of a GTY(())
9 ***********************
10
11 Sometimes the C code is not enough to fully describe the type
12 structure. Extra information can be provided with ``GTY`` options
13 and additional markers. Some options take a parameter, which may be
14 either a string or a type name, depending on the parameter. If an
15 option takes no parameter, it is acceptable either to omit the
16 parameter entirely, or to provide an empty string as a parameter. For
17 example, ``GTY ((skip))`` and ``GTY ((skip ("")))`` are
18 equivalent.
19
20 When the parameter is a string, often it is a fragment of C code. Four
21 special escapes may be used in these strings, to refer to pieces of
22 the data structure being marked:
23
24 .. index:: % in GTY option
25
26 ``%h``
27 The current structure.
28
29 ``%1``
30 The structure that immediately contains the current structure.
31
32 ``%0``
33 The outermost structure that contains the current structure.
34
35 ``%a``
36 A partial expression of the form ``[i1][i2]...`` that indexes
37 the array item currently being marked.
38
39 For instance, suppose that you have a structure of the form
40
41 .. code-block:: c++
42
43 struct A {
44 ...
45 };
46 struct B {
47 struct A foo[12];
48 };
49
50 and ``b`` is a variable of type ``struct B``. When marking
51 :samp:`b.foo[11]`, ``%h`` would expand to :samp:`b.foo[11]`,
52 ``%0`` and ``%1`` would both expand to :samp:`b`, and ``%a``
53 would expand to :samp:`[11]`.
54
55 As in ordinary C, adjacent strings will be concatenated; this is
56 helpful when you have a complicated expression.
57
58 .. code-block:: c++
59
60 GTY ((chain_next ("TREE_CODE (&%h.generic) == INTEGER_TYPE"
61 " ? TYPE_NEXT_VARIANT (&%h.generic)"
62 " : TREE_CHAIN (&%h.generic)")))
63
64 The available options are:
65
66 .. index:: length
67
68 :samp:`length ("{expression}")`
69 There are two places the type machinery will need to be explicitly told
70 the length of an array of non-atomic objects. The first case is when a
71 structure ends in a variable-length array, like this:
72
73 .. code-block:: c++
74
75 struct GTY(()) rtvec_def {
76 int num_elem; /* number of elements */
77 rtx GTY ((length ("%h.num_elem"))) elem[1];
78 };
79
80 In this case, the ``length`` option is used to override the specified
81 array length (which should usually be ``1``). The parameter of the
82 option is a fragment of C code that calculates the length.
83
84 The second case is when a structure or a global variable contains a
85 pointer to an array, like this:
86
87 .. code-block:: c++
88
89 struct gimple_omp_for_iter * GTY((length ("%h.collapse"))) iter;
90
91 In this case, ``iter`` has been allocated by writing something like
92
93 .. code-block:: c++
94
95 x->iter = ggc_alloc_cleared_vec_gimple_omp_for_iter (collapse);
96
97 and the ``collapse`` provides the length of the field.
98
99 This second use of ``length`` also works on global variables, like:
100
101 .. code-block:: c++
102
103 static GTY((length("reg_known_value_size"))) rtx *reg_known_value;
104
105 Note that the ``length`` option is only meant for use with arrays of
106 non-atomic objects, that is, objects that contain pointers pointing to
107 other GTY-managed objects. For other GC-allocated arrays and strings
108 you should use ``atomic`` or ``string_length``.
109
110 .. index:: string_length
111
112 :samp:`string_length ("{expression}")`
113 In order to simplify production of PCH, a structure member that is a plain
114 array of bytes (an optionally ``const`` and/or ``unsigned`` ``char
115 *``) is treated specially by the infrastructure. Even if such an array has not
116 been allocated in GC-controlled memory, it will still be written properly into
117 a PCH. The machinery responsible for this needs to know the length of the
118 data; by default, the length is determined by calling ``strlen`` on the
119 pointer. The ``string_length`` option specifies an alternate way to
120 determine the length, such as by inspecting another struct member:
121
122 .. code-block:: c++
123
124 struct GTY(()) non_terminated_string {
125 size_t sz;
126 const char * GTY((string_length ("%h.sz"))) data;
127 };
128
129 .. index:: skip
130
131 ``skip``
132 If ``skip`` is applied to a field, the type machinery will ignore it.
133 This is somewhat dangerous; the only safe use is in a union when one
134 field really isn't ever used.
135
136 .. index:: callback
137
138 ``callback``
139 ``callback`` should be applied to fields with pointer to function type
140 and causes the field to be ignored similarly to ``skip``, except when
141 writing PCH and the field is non-NULL it will remember the field's address
142 for relocation purposes if the process writing PCH has different load base
143 from a process reading PCH.
144
145 .. index:: for_user
146
147 ``for_user``
148 Use this to mark types that need to be marked by user gc routines, but are not
149 refered to in a template argument. So if you have some user gc type T1 and a
150 non user gc type T2 you can give T2 the for_user option so that the marking
151 functions for T1 can call non mangled functions to mark T2.
152
153 .. index:: desc, tag, default
154
155 :samp:`desc ("{expression}")` :samp:`tag ("{constant}")` ``default``
156 The type machinery needs to be told which field of a ``union`` is
157 currently active. This is done by giving each field a constant
158 ``tag`` value, and then specifying a discriminator using ``desc``.
159 The value of the expression given by ``desc`` is compared against
160 each ``tag`` value, each of which should be different. If no
161 ``tag`` is matched, the field marked with ``default`` is used if
162 there is one, otherwise no field in the union will be marked.
163
164 In the ``desc`` option, the 'current structure' is the union that
165 it discriminates. Use ``%1`` to mean the structure containing it.
166 There are no escapes available to the ``tag`` option, since it is a
167 constant.
168
169 For example,
170
171 .. code-block:: c++
172
173 struct GTY(()) tree_binding
174 {
175 struct tree_common common;
176 union tree_binding_u {
177 tree GTY ((tag ("0"))) scope;
178 struct cp_binding_level * GTY ((tag ("1"))) level;
179 } GTY ((desc ("BINDING_HAS_LEVEL_P ((tree)&%0)"))) xscope;
180 tree value;
181 };
182
183 In this example, the value of BINDING_HAS_LEVEL_P when applied to a
184 ``struct tree_binding *`` is presumed to be 0 or 1. If 1, the type
185 mechanism will treat the field ``level`` as being present and if 0,
186 will treat the field ``scope`` as being present.
187
188 The ``desc`` and ``tag`` options can also be used for inheritance
189 to denote which subclass an instance is. See :ref:`inheritance-and-gty`
190 for more information.
191
192 .. index:: cache
193
194 ``cache``
195 When the ``cache`` option is applied to a global variable gt_cleare_cache is
196 called on that variable between the mark and sweep phases of garbage
197 collection. The gt_clear_cache function is free to mark blocks as used, or to
198 clear pointers in the variable.
199
200 .. index:: deletable
201
202 ``deletable``
203 ``deletable``, when applied to a global variable, indicates that when
204 garbage collection runs, there's no need to mark anything pointed to
205 by this variable, it can just be set to ``NULL`` instead. This is used
206 to keep a list of free structures around for re-use.
207
208 .. index:: maybe_undef
209
210 ``maybe_undef``
211 When applied to a field, ``maybe_undef`` indicates that it's OK if
212 the structure that this fields points to is never defined, so long as
213 this field is always ``NULL``. This is used to avoid requiring
214 backends to define certain optional structures. It doesn't work with
215 language frontends.
216
217 .. index:: nested_ptr
218
219 :samp:`nested_ptr ({type}, "{to expression}", "{from expression}")`
220 The type machinery expects all pointers to point to the start of an
221 object. Sometimes for abstraction purposes it's convenient to have
222 a pointer which points inside an object. So long as it's possible to
223 convert the original object to and from the pointer, such pointers
224 can still be used. :samp:`{type}` is the type of the original object,
225 the :samp:`{to expression}` returns the pointer given the original object,
226 and the :samp:`{from expression}` returns the original object given
227 the pointer. The pointer will be available using the ``%h``
228 escape.
229
230 .. index:: chain_next, chain_prev, chain_circular
231
232 :samp:`chain_next ("{expression}")` :samp:`chain_prev ("{expression}")` :samp:`chain_circular ("{expression}")`
233 It's helpful for the type machinery to know if objects are often
234 chained together in long lists; this lets it generate code that uses
235 less stack space by iterating along the list instead of recursing down
236 it. ``chain_next`` is an expression for the next item in the list,
237 ``chain_prev`` is an expression for the previous item. For singly
238 linked lists, use only ``chain_next`` ; for doubly linked lists, use
239 both. The machinery requires that taking the next item of the
240 previous item gives the original item. ``chain_circular`` is similar
241 to ``chain_next``, but can be used for circular single linked lists.
242
243 .. index:: reorder
244
245 :samp:`reorder ("{function name}")`
246 Some data structures depend on the relative ordering of pointers. If
247 the precompiled header machinery needs to change that ordering, it
248 will call the function referenced by the ``reorder`` option, before
249 changing the pointers in the object that's pointed to by the field the
250 option applies to. The function must take four arguments, with the
251 signature :samp:`void \*, void \*, gt_pointer_operator, void \*`.
252 The first parameter is a pointer to the structure that contains the
253 object being updated, or the object itself if there is no containing
254 structure. The second parameter is a cookie that should be ignored.
255 The third parameter is a routine that, given a pointer, will update it
256 to its correct new value. The fourth parameter is a cookie that must
257 be passed to the second parameter.
258
259 PCH cannot handle data structures that depend on the absolute values
260 of pointers. ``reorder`` functions can be expensive. When
261 possible, it is better to depend on properties of the data, like an ID
262 number or the hash of a string instead.
263
264 .. index:: atomic
265
266 ``atomic``
267 The ``atomic`` option can only be used with pointers. It informs
268 the GC machinery that the memory that the pointer points to does not
269 contain any pointers, and hence it should be treated by the GC and PCH
270 machinery as an 'atomic' block of memory that does not need to be
271 examined when scanning memory for pointers. In particular, the
272 machinery will not scan that memory for pointers to mark them as
273 reachable (when marking pointers for GC) or to relocate them (when
274 writing a PCH file).
275
276 The ``atomic`` option differs from the ``skip`` option.
277 ``atomic`` keeps the memory under Garbage Collection, but makes the
278 GC ignore the contents of the memory. ``skip`` is more drastic in
279 that it causes the pointer and the memory to be completely ignored by
280 the Garbage Collector. So, memory marked as ``atomic`` is
281 automatically freed when no longer reachable, while memory marked as
282 ``skip`` is not.
283
284 The ``atomic`` option must be used with great care, because all
285 sorts of problem can occur if used incorrectly, that is, if the memory
286 the pointer points to does actually contain a pointer.
287
288 Here is an example of how to use it:
289
290 .. code-block:: c++
291
292 struct GTY(()) my_struct {
293 int number_of_elements;
294 unsigned int * GTY ((atomic)) elements;
295 };
296
297 In this case, ``elements`` is a pointer under GC, and the memory it
298 points to needs to be allocated using the Garbage Collector, and will
299 be freed automatically by the Garbage Collector when it is no longer
300 referenced. But the memory that the pointer points to is an array of
301 ``unsigned int`` elements, and the GC must not try to scan it to
302 find pointers to mark or relocate, which is why it is marked with the
303 ``atomic`` option.
304
305 Note that, currently, global variables cannot be marked with
306 ``atomic`` ; only fields of a struct can. This is a known
307 limitation. It would be useful to be able to mark global pointers
308 with ``atomic`` to make the PCH machinery aware of them so that
309 they are saved and restored correctly to PCH files.
310
311 .. index:: special
312
313 :samp:`special ("{name}")`
314 The ``special`` option is used to mark types that have to be dealt
315 with by special case machinery. The parameter is the name of the
316 special case. See :samp:`gengtype.cc` for further details. Avoid
317 adding new special cases unless there is no other alternative.
318
319 .. index:: user
320
321 ``user``
322 The ``user`` option indicates that the code to mark structure
323 fields is completely handled by user-provided routines. See section
324 :ref:`user-gc` for details on what functions need to be provided.