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;|}
222 @section Source Files Containing Type Information
223 @cindex generated files
224 @cindex files, generated
226 Whenever you add @code{GTY} markers to a new source file, there are three
227 things you need to do:
231 You need to add the file to the list of source files the type
232 machinery scans. There are three cases:
236 For a back-end file, this is usually done
237 automatically; if not, you should add it to @code{config_gtfiles} in
238 the appropriate port's entries in @file{config.gcc}.
241 For files shared by all front ends, this is done by adding the
242 filename to the @code{GTFILES} variable in @file{Makefile.in}.
245 For any other file used by a front end, this is done by adding the
246 filename to the @code{gtfiles} variable defined in
247 @file{config-lang.in}. For C, the file is @file{c-config-lang.in}.
248 This list should include all files that have GTY macros in them that
249 are used in that front end, other than those defined in the previous
250 list items. For example, it is common for front end writers to use
251 @file{c-common.c} and other files from the C front end, and these
252 should be included in the @file{gtfiles} variable for such front ends.
257 You need to include the file that the type machinery will generate in
258 the source file you just changed. The file will be called
259 @file{gt-@var{path}.h} where @var{path} is the pathname from the
260 @file{gcc} directory with slashes replaced by @verb{|-|}. Don't forget
261 to mention this file as a dependency in the @file{Makefile}!
264 Finally, you need to arrange to add a @file{Makefile} rule that will
265 ensure this file can be built. This is done by making it a dependency
266 of @code{s-gtype}, like this:
268 gt-path.h : s-gtype ; @true
272 For language frontends, there is another file that needs to be included
273 somewhere. It will be called @file{gtype-@var{lang}.h}, where
274 @var{lang} is the name of the subdirectory the language is contained in.
275 It will need @file{Makefile} rules just like the other generated files.