FSF GCC merge 02/23/03
[official-gcc.git] / gcc / doc / gty.texi
blob6d4f8394f42bffb4bb6de4b78634053abdca395a
1 @c Copyright (C) 2002, 2003
2 @c Free Software Foundation, Inc.
3 @c This is part of the GCC manual.
4 @c For copying conditions, see the file gcc.texi.
6 @node Type Information
7 @chapter Memory Management and Type Information
8 @cindex GGC
9 @findex GTY
11 GCC uses some fairly sophisticated memory management techniques, which
12 involve determining information about GCC's data structures from GCC's
13 source code and using this information to perform garbage collection and
14 implement precompiled headers.
16 A full C parser would be too overcomplicated for this task, so a limited
17 subset of C is interpreted and special markers are used to determine
18 what parts of the source to look at.  The parser can also detect
19 simple typedefs of the form @code{typedef struct ID1 *ID2;} and
20 @code{typedef int ID3;}, and these don't need to be specially marked.
22 The two forms that do need to be marked are:
23 @verbatim
24 struct ID1 GTY(([options]))
26   [fields]
29 typedef struct ID2 GTY(([options]))
31   [fields]
32 } ID3;
33 @end verbatim
35 @menu
36 * GTY Options::         What goes inside a @code{GTY(())}.
37 * GGC Roots::           Making global variables GGC roots.
38 * Files::               How the generated files work.
39 @end menu
41 @node GTY Options
42 @section The Inside of a @code{GTY(())}
44 Sometimes the C code is not enough to fully describe the type structure.
45 Extra information can be provided by using more @code{GTY} markers.
46 These markers can be placed:
47 @itemize @bullet
48 @item
49 In a structure definition, before the open brace;
50 @item
51 In a global variable declaration, after the keyword @code{static} or 
52 @code{extern}; and
53 @item
54 In a structure field definition, before the name of the field.
55 @end itemize
57 The format of a marker is
58 @verbatim
59 GTY (([name] ([param]), [name] ([param]) ...))
60 @end verbatim
61 The parameter is either a string or a type name.
63 When the parameter is a string, often it is a fragment of C code.  Three
64 special escapes may be available:
66 @cindex % in GTY option
67 @table @code
68 @item %h
69 This expands to an expression that evaluates to the current structure.
70 @item %1
71 This expands to an expression that evaluates to the structure that
72 immediately contains the current structure.
73 @item %0
74 This expands to an expression that evaluates to the outermost structure
75 that contains the current structure.
76 @item %a
77 This expands to the string of the form @code{[i1][i2]...} that indexes
78 the array item currently being marked.  For instance, if the field
79 being marked is @code{foo}, then @code{%1.foo%a} is the same as @code{%h}.
80 @end table
82 The available options are:
84 @table @code
85 @findex length
86 @item length
88 There are two places the type machinery will need to be explicitly told
89 the length of an array.  The first case is when a structure ends in a
90 variable-length array, like this:
91 @verbatim
92 struct rtvec_def GTY(()) {
93   int num_elem;         /* number of elements */
94   rtx GTY ((length ("%h.num_elem"))) elem[1];
96 @end verbatim
97 In this case, the @code{length} option is used to override the specified
98 array length (which should usually be @code{1}).  The parameter of the
99 option is a fragment of C code that calculates the length.
101 The second case is when a structure or a global variable contains a
102 pointer to an array, like this:
103 @smallexample
104 tree *
105   GTY ((length ("%h.regno_pointer_align_length"))) regno_decl;
106 @end smallexample
107 In this case, @code{regno_decl} has been allocated by writing something like
108 @smallexample
109   x->regno_decl =
110     ggc_alloc (x->regno_pointer_align_length * sizeof (tree));
111 @end smallexample
112 and the @code{length} provides the length of the field.
114 This second use of @code{length} also works on global variables, like:
115 @verbatim
116   static GTY((length ("reg_base_value_size")))
117     rtx *reg_base_value;
118 @end verbatim
120 @findex skip
121 @item skip
123 If @code{skip} is applied to a field, the type machinery will ignore it.
124 This is somewhat dangerous; the only safe use is in a union when one
125 field really isn't ever used.
127 @findex desc
128 @findex tag
129 @findex default
130 @item desc
131 @itemx tag
132 @itemx default
134 The type machinery needs to be told which field of a @code{union} is
135 currently active.  This is done by giving each field a constant @code{tag}
136 value, and then specifying a discriminator using @code{desc}.  For example,
137 @smallexample
138 struct tree_binding GTY(())
140   struct tree_common common;
141   union tree_binding_u @{
142     tree GTY ((tag ("0"))) scope;
143     struct cp_binding_level * GTY ((tag ("1"))) level;
144   @} GTY ((desc ("BINDING_HAS_LEVEL_P ((tree)&%0)"))) scope;
145   tree value;
147 @end smallexample
149 In the @code{desc} option, the ``current structure'' is the union that
150 it discriminates.  Use @code{%1} to mean the structure containing it.
151 (There are no escapes available to the @code{tag} option, since it's
152 supposed to be a constant.)
154 Each @code{tag} should be different.  If no @code{tag} is matched,
155 the field marked with @code{default} is used if there is one, otherwise
156 no field in the union will be marked.
158 @findex param_is
159 @findex use_param
160 @item param_is
161 @itemx use_param
163 Sometimes it's convenient to define some data structure to work on
164 generic pointers (that is, @code{PTR}) and then use it with a specific
165 type.  @code{param_is} specifies the real type pointed to, and
166 @code{use_param} says where in the generic data structure that type
167 should be put.
169 For instance, to have a @code{htab_t} that points to trees, one should write
170 @verbatim
171   htab_t GTY ((param_is (union tree_node))) ict;
172 @end verbatim
174 @findex param@var{n}_is
175 @findex use_param@var{n}
176 @item param@var{n}_is
177 @itemx use_param@var{n}
179 In more complicated cases, the data structure might need to work on
180 several different types, which might not necessarily all be pointers.
181 For this, @code{param1_is} through @code{param9_is} may be used to
182 specify the real type of a field identified by @code{use_param1} through
183 @code{use_param9}.
185 @findex use_params
186 @item use_params
188 When a structure contains another structure that is parameterized,
189 there's no need to do anything special, the inner structure inherits the
190 parameters of the outer one.  When a structure contains a pointer to a
191 parameterized structure, the type machinery won't automatically detect
192 this (it could, it just doesn't yet), so it's necessary to tell it that
193 the pointed-to structure should use the same parameters as the outer
194 structure.  This is done by marking the pointer with the
195 @code{use_params} option.
197 @findex deletable
198 @item deletable
200 @code{deletable}, when applied to a global variable, indicates that when
201 garbage collection runs, there's no need to mark anything pointed to
202 by this variable, it can just be set to @code{NULL} instead.  This is used
203 to keep a list of free structures around for re-use.
205 @findex if_marked
206 @item if_marked
208 Suppose you want some kinds of object to be unique, and so you put them
209 in a hash table.  If garbage collection marks the hash table, these
210 objects will never be freed, even if the last other reference to them
211 goes away.  GGC has special handling to deal with this: if you use the
212 @code{if_marked} option on a global hash table, GGC will call the
213 routine whose name is the parameter to the option on each hash table
214 entry.  If the routine returns nonzero, the hash table entry will
215 be marked as usual.  If the routine returns zero, the hash table entry
216 will be deleted.
218 The routine @code{ggc_marked_p} can be used to determine if an element
219 has been marked already; in fact, the usual case is to use
220 @code{if_marked ("ggc_marked_p")}.
222 @findex maybe_undef
223 @item maybe_undef
225 When applied to a field, @code{maybe_undef} indicates that it's OK if
226 the structure that this fields points to is never defined, so long as
227 this field is always @code{NULL}.  This is used to avoid requiring
228 backends to define certain optional structures.  It doesn't work with
229 language frontends.
231 @findex chain_next
232 @findex chain_prev
233 @item chain_next
234 @itemx chain_prev
236 It's helpful for the type machinery to know if objects are often
237 chained together in long lists; this lets it generate code that uses
238 less stack space by iterating along the list instead of recursing down
239 it.  @code{chain_next} is an expression for the next item in the list,
240 @code{chain_prev} is an expression for the previous item.  The
241 machinery requires that taking the next item of the previous item
242 gives the original item.
244 @findex reorder
245 @item reorder
247 Some data structures depend on the relative ordering of pointers.  If
248 the type machinery needs to change that ordering, it will call the
249 function referenced by the @code{reorder} option, before changing the
250 pointers in the object that's pointed to by the field the option
251 applies to.  The function must be of the type @code{void ()(void *,
252 void *, gt_pointer_operator, void *)}.  The second parameter is the
253 pointed-to object; the third parameter is a routine that, given a
254 pointer, can update it to its new value.  The fourth parameter is a
255 cookie to be passed to the third parameter.  The first parameter is
256 the structure that contains the object, or the object itself if it is
257 a structure.
259 No data structure may depend on the absolute value of pointers.  Even
260 relying on relative orderings and using @code{reorder} functions can
261 be expensive.  It is better to depend on properties of the data, like
262 an ID number or the hash of a string instead.
264 @findex special
265 @item special
267 The @code{special} option is used for those bizarre cases that are just
268 too hard to deal with otherwise.  Don't use it for new code.
270 @end table
272 @node GGC Roots
273 @section Marking Roots for the Garbage Collector
274 @cindex roots, marking
275 @cindex marking roots
277 In addition to keeping track of types, the type machinery also locates
278 the global variables that the garbage collector starts at.  There are
279 two syntaxes it accepts to indicate a root:
281 @enumerate
282 @item
283 @verb{|extern GTY (([options])) [type] ID;|}
284 @item
285 @verb{|static GTY (([options])) [type] ID;|}
286 @end enumerate
288 These are the only syntaxes that are accepted.  In particular, if you
289 want to mark a variable that is only declared as
290 @verbatim
291 int ID;
292 @end verbatim
293 or similar, you should either make it @code{static} or you should create
294 a @code{extern} declaration in a header file somewhere.
296 @node Files
297 @section Source Files Containing Type Information
298 @cindex generated files
299 @cindex files, generated
301 Whenever you add @code{GTY} markers to a new source file, there are three
302 things you need to do:
304 @enumerate
305 @item
306 You need to add the file to the list of source files the type
307 machinery scans.  There are three cases: 
309 @enumerate a
310 @item
311 For a back-end file, this is usually done
312 automatically; if not, you should add it to @code{target_gtfiles} in
313 the appropriate port's entries in @file{config.gcc}. 
315 @item
316 For files shared by all front ends, this is done by adding the
317 filename to the @code{GTFILES} variable in @file{Makefile.in}.
319 @item 
320 For any other file used by a front end, this is done by adding the
321 filename to the @code{gtfiles} variable defined in
322 @file{config-lang.in}.  For C, the file is @file{c-config-lang.in}.
323 This list should include all files that have GTY macros in them that
324 are used in that front end, other than those defined in the previous
325 list items.  For example, it is common for front end writers to use
326 @file{c-common.c} and other files from the C front end, and these
327 should be included in the @file{gtfiles} variable for such front ends.
329 @end enumerate
331 @item
332 If the file was a header file, you'll need to check that it's included
333 in the right place to be visible to the generated files.  For a back-end
334 header file, this should be done automatically.  For a front-end header
335 file, it needs to be included by the same file that includes
336 @file{gtype-@var{lang}.h}.  For other header files, it needs to be
337 included in @file{gtype-desc.c}, which is a generated file, so add it to
338 @code{ifiles} in @code{open_base_file} in @file{gengtype.c}.  
340 For source files that aren't header files, the machinery will generate a
341 header file that should be included in the source file you just changed.
342 The file will be called @file{gt-@var{path}.h} where @var{path} is the
343 pathname relative to the @file{gcc} directory with slashes replaced by
344 @verb{|-|}, so for example the header file to be included in
345 @file{objc/objc-parse.c} is called @file{gt-objc-objc-parse.c}.  The
346 generated header file should be included after everything else in the
347 source file.  Don't forget to mention this file as a dependency in the
348 @file{Makefile}!
350 @item
351 If a new @file{gt-@var{path}.h} file is needed, you need to arrange to
352 add a @file{Makefile} rule that will ensure this file can be built.
353 This is done by making it a dependency of @code{s-gtype}, like this:
354 @verbatim
355 gt-path.h : s-gtype ; @true
356 @end verbatim
357 @end enumerate
359 For language frontends, there is another file that needs to be included
360 somewhere.  It will be called @file{gtype-@var{lang}.h}, where
361 @var{lang} is the name of the subdirectory the language is contained in.
362 It will need @file{Makefile} rules just like the other generated files.