OSDN Git Service

2002-09-04 Janis Johnson <janis187@us.ibm.com>
[pf3gnuchains/gcc-fork.git] / gcc / doc / gty.texi
1 @c Copyright (C) 2002
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.
5
6 @node Type Information
7 @chapter Memory Management and Type Information
8 @cindex GGC
9 @findex GTY
10
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.
14
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.
20
21 The two forms that do need to be marked are:
22 @verbatim
23 struct ID1 GTY(([options]))
24 {
25   [fields]
26 };
27
28 typedef struct ID2 GTY(([options]))
29 {
30   [fields]
31 } ID3;
32 @end verbatim
33
34 @menu
35 * GTY Options::         What goes inside a @code{GTY(())}.
36 * GGC Roots::           Making global variables GGC roots.
37 * Files::               How the generated files work.
38 @end menu
39
40 @node GTY Options
41 @section The Inside of a @code{GTY(())}
42
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:
46 @itemize @bullet
47 @item
48 In a structure definition, before the open brace;
49 @item
50 In a global variable declaration, after the keyword @code{static} or 
51 @code{extern}; and
52 @item
53 In a structure field definition, before the name of the field.
54 @end itemize
55
56 The format of a marker is
57 @verbatim
58 GTY (([name] ([param]), [name] ([param]) ...))
59 @end verbatim
60 The parameter is either a string or a type name.
61
62 When the parameter is a string, often it is a fragment of C code.  Three
63 special escapes may be available:
64
65 @cindex % in GTY option
66 @table @code
67 @item %h
68 This expands to an expression that evaluates to the current structure.
69 @item %1
70 This expands to an expression that evaluates to the structure that
71 immediately contains the current structure.
72 @item %0
73 This expands to an expression that evaluates to the outermost structure
74 that contains the current structure.
75 @end table
76
77 The available options are:
78
79 @table @code
80 @findex length
81 @item length
82
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:
86 @verbatim
87 struct rtvec_def GTY(()) {
88   int num_elem;         /* number of elements */
89   rtx GTY ((length ("%h.num_elem"))) elem[1];
90 };
91 @end verbatim
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.
95
96 The second case is when a structure or a global variable contains a
97 pointer to an array, like this:
98 @smallexample
99 tree *
100   GTY ((length ("%h.regno_pointer_align_length"))) regno_decl;
101 @end smallexample
102 In this case, @code{regno_decl} has been allocated by writing something like
103 @smallexample
104   x->regno_decl =
105     ggc_alloc (x->regno_pointer_align_length * sizeof (tree));
106 @end smallexample
107 and the @code{length} provides the length of the field.
108
109 This second use of @code{length} also works on global variables, like:
110 @verbatim
111   static GTY((length ("reg_base_value_size")))
112     rtx *reg_base_value;
113 @end verbatim
114
115 @findex skip
116 @item skip
117
118 If @code{skip} is applied to a field, the type machinery will ignore it.
119 This is somewhat dangerous; the only safe use is in a union when one
120 field really isn't ever used.
121
122 @findex desc
123 @findex tag
124 @findex always
125 @item desc
126 @itemx tag
127 @itemx always
128
129 The type machinery needs to be told which field of a @code{union} is
130 currently active.  This is done by giving each field a constant @code{tag}
131 value, and then specifying a discriminator using @code{desc}.  For example,
132 @smallexample
133 struct tree_binding GTY(())
134 @{
135   struct tree_common common;
136   union tree_binding_u @{
137     tree GTY ((tag ("0"))) scope;
138     struct cp_binding_level * GTY ((tag ("1"))) level;
139   @} GTY ((desc ("BINDING_HAS_LEVEL_P ((tree)&%0)"))) scope;
140   tree value;
141 @};
142 @end smallexample
143
144 In the @code{desc} option, the ``current structure'' is the union that
145 it discriminates.  Use @code{%1} to mean the structure containing it.
146 (There are no escapes available to the @code{tag} option, since it's
147 supposed to be a constant.)
148
149 You can use @code{always} to mean that this field is always used.
150
151 @findex param_is
152 @findex use_param
153 @item param_is
154 @itemx use_param
155
156 Sometimes it's convenient to define some data structure to work on
157 generic pointers (that is, @code{PTR}), and then use it with specific types.
158 @code{param_is} specifies the real type pointed to, and @code{use_param}
159 says where in the generic data structure that type should be put.
160
161 For instance, to have a @code{htab_t} that points to trees, one should write
162 @verbatim
163   htab_t GTY ((param_is (union tree_node))) ict;
164 @end verbatim
165
166 @findex deletable
167 @item deletable
168
169 @code{deletable}, when applied to a global variable, indicates that when
170 garbage collection runs, there's no need to mark anything pointed to
171 by this variable, it can just be set to @code{NULL} instead.  This is used
172 to keep a list of free structures around for re-use.
173
174 @findex if_marked
175 @item if_marked
176
177 Suppose you want some kinds of object to be unique, and so you put them
178 in a hash table.  If garbage collection marks the hash table, these
179 objects will never be freed, even if the last other reference to them
180 goes away.  GGC has special handling to deal with this: if you use the
181 @code{if_marked} option on a global hash table, GGC will call the
182 routine whose name is the parameter to the option on each hash table
183 entry.  If the routine returns nonzero, the hash table entry will
184 be marked as usual.  If the routine returns zero, the hash table entry
185 will be deleted.
186
187 The routine @code{ggc_marked_p} can be used to determine if an element
188 has been marked already; in fact, the usual case is to use
189 @code{if_marked ("ggc_marked_p")}.
190
191 @findex maybe_undef
192 @item maybe_undef
193
194 When applied to a field, @code{maybe_undef} indicates that it's OK if
195 the structure that this fields points to is never defined, so long as
196 this field is always @code{NULL}.  This is used to avoid requiring
197 backends to define certain optional structures.  It doesn't work with
198 language frontends.
199
200 @findex special
201 @item special
202
203 The @code{special} option is used for those bizarre cases that are just
204 too hard to deal with otherwise.  Don't use it for new code.
205
206 @end table
207
208 @node GGC Roots
209 @section Marking Roots for the Garbage Collector
210 @cindex roots, marking
211 @cindex marking roots
212
213 In addition to keeping track of types, the type machinery also locates
214 the global variables that the garbage collector starts at.  There are
215 two syntaxes it accepts to indicate a root:
216
217 @enumerate
218 @item
219 @verb{|extern GTY (([options])) [type] ID;|}
220 @item
221 @verb{|static GTY (([options])) [type] ID;|}
222 @end enumerate
223
224 These are the only syntaxes that are accepted.  In particular, if you
225 want to mark a variable that is only declared as
226 @verbatim
227 int ID;
228 @end verbatim
229 or similar, you should either make it @code{static} or you should create
230 a @code{extern} declaration in a header file somewhere.
231
232 @node Files
233 @section Source Files Containing Type Information
234 @cindex generated files
235 @cindex files, generated
236
237 Whenever you add @code{GTY} markers to a new source file, there are three
238 things you need to do:
239
240 @enumerate
241 @item
242 You need to add the file to the list of source files the type
243 machinery scans.  There are three cases: 
244
245 @enumerate a
246 @item
247 For a back-end file, this is usually done
248 automatically; if not, you should add it to @code{target_gtfiles} in
249 the appropriate port's entries in @file{config.gcc}. 
250
251 @item
252 For files shared by all front ends, this is done by adding the
253 filename to the @code{GTFILES} variable in @file{Makefile.in}.
254
255 @item 
256 For any other file used by a front end, this is done by adding the
257 filename to the @code{gtfiles} variable defined in
258 @file{config-lang.in}.  For C, the file is @file{c-config-lang.in}.
259 This list should include all files that have GTY macros in them that
260 are used in that front end, other than those defined in the previous
261 list items.  For example, it is common for front end writers to use
262 @file{c-common.c} and other files from the C front end, and these
263 should be included in the @file{gtfiles} variable for such front ends.
264
265 @end enumerate
266
267 @item
268 If the file was a header file, you'll need to check that it's included
269 in the right place to be visible to the generated files.  For a back-end
270 header file, this should be done automatically.  For a front-end header
271 file, it needs to be included by the same file that includes
272 @file{gtype-@var{lang}.h}.  For other header files, it needs to be
273 included in @file{gtype-desc.c}, which is a generated file, so add it to
274 @code{ifiles} in @code{open_base_file} in @file{gengtype.c}.  
275
276 For source files that aren't header files, the machinery will generate a
277 header file that should be included in the source file you just changed.
278 The file will be called @file{gt-@var{path}.h} where @var{path} is the
279 pathname relative to the @file{gcc} directory with slashes replaced by
280 @verb{|-|}, so for example the header file to be included in
281 @file{objc/objc-parse.c} is called @file{gt-objc-objc-parse.c}.  The
282 generated header file should be included after everything else in the
283 source file.  Don't forget to mention this file as a dependency in the
284 @file{Makefile}!
285
286 @item
287 If a new @file{gt-@var{path}.h} file is needed, you need to arrange to
288 add a @file{Makefile} rule that will ensure this file can be built.
289 This is done by making it a dependency of @code{s-gtype}, like this:
290 @verbatim
291 gt-path.h : s-gtype ; @true
292 @end verbatim
293 @end enumerate
294
295 For language frontends, there is another file that needs to be included
296 somewhere.  It will be called @file{gtype-@var{lang}.h}, where
297 @var{lang} is the name of the subdirectory the language is contained in.
298 It will need @file{Makefile} rules just like the other generated files.