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.
7 @chapter Memory Management and Type Information
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.
15 A full C parser would be too overcomplicated for this task, so a limited
16 subset of C is interpreted and special markers are used to determine
17 what parts of the source to look at. The parser can also detect
18 simple typedefs of the form @code{typedef struct ID1 *ID2;} and
19 @code{typedef int ID3;}, and these don't need to be specially marked.
21 The two forms that do need to be marked are:
23 struct ID1 GTY(([options]))
28 typedef struct ID2 GTY(([options]))
35 * GTY Options:: What goes inside a @code{GTY(())}.
36 * GGC Roots:: Making global variables GGC roots.
37 * Files:: How the generated files work.
41 @section The Inside of a @code{GTY(())}
43 Sometimes the C code is not enough to fully describe the type structure.
44 Extra information can be provided by using more @code{GTY} markers.
45 These markers can be placed:
48 In a structure definition, before the open brace;
50 In a global variable declaration, after the keyword @code{static} or
53 In a structure field definition, before the name of the field.
56 The format of a marker is
58 GTY (([name] ([param]), [name] ([param]) ...))
60 The parameter is either a string or a type name.
62 When the parameter is a string, often it is a fragment of C code. Three
63 special escapes may be available:
65 @cindex % in GTY option
68 This expands to an expression that evaluates to the current structure.
70 This expands to an expression that evaluates to the structure that
71 immediately contains the current structure.
73 This expands to an expression that evaluates to the outermost structure
74 that contains the current structure.
77 The available options are:
83 There are two places the type machinery will need to be explicitly told
84 the length of an array. The first case is when a structure ends in a
85 variable-length array, like this:
87 struct rtvec_def GTY(()) {
88 int num_elem; /* number of elements */
89 rtx GTY ((length ("%h.num_elem"))) elem[1];
92 In this case, the @code{length} option is used to override the specified
93 array length (which should usually be @code{1}). The parameter of the
94 option is a fragment of C code that calculates the length.
96 The second case is when a structure or a global variable contains a
97 pointer to an array, like this:
99 tree * GTY ((length ("%h.regno_pointer_align_length"))) regno_decl;
101 In this case, @code{regno_decl} has been allocated by writing something like
103 x->regno_decl = ggc_alloc (x->regno_pointer_align_length * sizeof (tree));
105 and the @code{length} provides the length of the field.
107 This second use of @code{length} also works on global variables, like:
109 static GTY((length ("reg_base_value_size"))) rtx *reg_base_value;
115 If @code{skip} is applied to a field, the type machinery will ignore it.
116 This is somewhat dangerous; the only safe use is in a union when one
117 field really isn't ever used.
126 The type machinery needs to be told which field of a @code{union} is
127 currently active. This is done by giving each field a constant @code{tag}
128 value, and then specifying a discriminator using @code{desc}. For example,
130 struct tree_binding GTY(())
132 struct tree_common common;
133 union tree_binding_u {
134 tree GTY ((tag ("0"))) scope;
135 struct cp_binding_level * GTY ((tag ("1"))) level;
136 } GTY ((desc ("BINDING_HAS_LEVEL_P ((tree)&%0)"))) scope;
141 In the @code{desc} option, the ``current structure'' is the union that
142 it discriminates. Use @code{%1} to mean the structure containing it.
143 (There are no escapes available to the @code{tag} option, since it's
144 supposed to be a constant.)
146 You can use @code{always} to mean that this field is always used.
153 Sometimes it's convenient to define some data structure to work on
154 generic pointers (that is, @code{PTR}), and then use it with specific types.
155 @code{param_is} specifies the real type pointed to, and @code{use_param}
156 says where in the generic data structure that type should be put.
158 For instance, to have a @code{htab_t} that points to trees, one should write
160 htab_t GTY ((param_is (union tree_node))) ict;
166 @code{deletable}, when applied to a global variable, indicates that when
167 garbage collection runs, there's no need to mark anything pointed to
168 by this variable, it can just be set to @code{NULL} instead. This is used
169 to keep a list of free structures around for re-use.
174 Suppose you want some kinds of object to be unique, and so you put them
175 in a hash table. If garbage collection marks the hash table, these
176 objects will never be freed, even if the last other reference to them
177 goes away. GGC has special handling to deal with this: if you use the
178 @code{if_marked} option on a global hash table, GGC will call the
179 routine whose name is the parameter to the option on each hash table
180 entry. If the routine returns nonzero, the hash table entry will
181 be marked as usual. If the routine returns zero, the hash table entry
184 The routine @code{ggc_marked_p} can be used to determine if an element
185 has been marked already; in fact, the usual case is to use
186 @code{if_marked ("ggc_marked_p")}.
191 When applied to a field, @code{maybe_undef} indicates that it's OK if
192 the structure that this fields points to is never defined, so long as
193 this field is always @code{NULL}. This is used to avoid requiring
194 backends to define certain optional structures. It doesn't work with
200 The @code{special} option is used for those bizarre cases that are just
201 too hard to deal with otherwise. Don't use it for new code.
206 @section Marking Roots for the Garbage Collector
207 @cindex roots, marking
208 @cindex marking roots
210 In addition to keeping track of types, the type machinery also locates
211 the global variables that the garbage collector starts at. There are
212 two syntaxes it accepts to indicate a root:
216 @verb{|extern GTY (([options])) [type] ID;|}
218 @verb{|static GTY (([options])) [type] ID;|}
221 These are the only syntaxes that are accepted. In particular, if you
222 want to mark a variable that is only declared as
226 or similar, you should either make it @code{static} or you should create
227 a @code{extern} declaration in a header file somewhere.
230 @section Source Files Containing Type Information
231 @cindex generated files
232 @cindex files, generated
234 Whenever you add @code{GTY} markers to a new source file, there are three
235 things you need to do:
239 You need to add the file to the list of source files the type
240 machinery scans. There are three cases:
244 For a back-end file, this is usually done
245 automatically; if not, you should add it to @code{target_gtfiles} in
246 the appropriate port's entries in @file{config.gcc}.
249 For files shared by all front ends, this is done by adding the
250 filename to the @code{GTFILES} variable in @file{Makefile.in}.
253 For any other file used by a front end, this is done by adding the
254 filename to the @code{gtfiles} variable defined in
255 @file{config-lang.in}. For C, the file is @file{c-config-lang.in}.
256 This list should include all files that have GTY macros in them that
257 are used in that front end, other than those defined in the previous
258 list items. For example, it is common for front end writers to use
259 @file{c-common.c} and other files from the C front end, and these
260 should be included in the @file{gtfiles} variable for such front ends.
265 If the file was a header file, you'll need to check that it's included
266 in the right place to be visible to the generated files. For a back-end
267 header file, this should be done automatically. For a front-end header
268 file, it needs to be included by the same file that includes
269 @file{gtype-@var{lang}.h}. For other header files, it needs to be
270 included in @file{gtype-desc.c}, which is a generated file, so add it to
271 @code{ifiles} in @code{open_base_file} in @file{gengtype.c}.
273 For source files that aren't header files, the machinery will generate a
274 header file that should be included in the source file you just changed.
275 The file will be called @file{gt-@var{path}.h} where @var{path} is the
276 pathname relative to the @file{gcc} directory with slashes replaced by
277 @verb{|-|}, so for example the header file to be included in
278 @file{objc/objc-parse.c} is called @file{gt-objc-objc-parse.c}. The
279 generated header file should be included after everything else in the
280 source file. Don't forget to mention this file as a dependency in the
284 If a new @file{gt-@var{path}.h} file is needed, you need to arrange to
285 add a @file{Makefile} rule that will ensure this file can be built.
286 This is done by making it a dependency of @code{s-gtype}, like this:
288 gt-path.h : s-gtype ; @true
292 For language frontends, there is another file that needs to be included
293 somewhere. It will be called @file{gtype-@var{lang}.h}, where
294 @var{lang} is the name of the subdirectory the language is contained in.
295 It will need @file{Makefile} rules just like the other generated files.