Import pre-release gcc-5.0 to new vendor branch
[dragonfly.git] / contrib / gcc-5.0 / gcc / doc / plugins.texi
1 @c Copyright (C) 2009-2015 Free Software Foundation, Inc.
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 Plugins
7 @chapter Plugins
8 @cindex Plugins
9
10 GCC plugin is a loadable module that provides extra
11 features to the compiler, which they can further pass
12 around as a shareable module.
13
14 GCC plugins provide developers with a rich subset of
15 the GCC API to allow them to extend GCC as they see fit.
16 Whether it is writing an additional optimization pass,
17 transforming code, or analyzing information, plugins
18 can be quite useful.
19
20 @menu
21 * Plugins loading::      How can we load plugins.
22 * Plugin API::           The APIs for plugins.
23 * Plugins pass::         How a plugin interact with the pass manager.
24 * Plugins GC::           How a plugin Interact with GCC Garbage Collector.
25 * Plugins description::  Giving information about a plugin itself.
26 * Plugins attr::         Registering custom attributes or pragmas.
27 * Plugins recording::    Recording information about pass execution.
28 * Plugins gate::         Controlling which passes are being run.
29 * Plugins tracking::     Keeping track of available passes.
30 * Plugins building::     How can we build a plugin.
31 @end menu
32
33 @node Plugins loading
34 @section Loading Plugins
35
36 Plugins are supported on platforms that support @option{-ldl
37 -rdynamic}.  They are loaded by the compiler using @code{dlopen}
38 and invoked at pre-determined locations in the compilation
39 process.
40
41 Plugins are loaded with
42
43 @option{-fplugin=/path/to/@var{name}.so} @option{-fplugin-arg-@var{name}-@var{key1}[=@var{value1}]}
44
45 The plugin arguments are parsed by GCC and passed to respective
46 plugins as key-value pairs. Multiple plugins can be invoked by
47 specifying multiple @option{-fplugin} arguments.
48
49 A plugin can be simply given by its short name (no dots or
50 slashes). When simply passing @option{-fplugin=@var{name}}, the plugin is
51 loaded from the @file{plugin} directory, so @option{-fplugin=@var{name}} is
52 the same as @option{-fplugin=`gcc -print-file-name=plugin`/@var{name}.so},
53 using backquote shell syntax to query the @file{plugin} directory.
54
55 @node Plugin API
56 @section Plugin API
57
58 Plugins are activated by the compiler at specific events as defined in
59 @file{gcc-plugin.h}.  For each event of interest, the plugin should
60 call @code{register_callback} specifying the name of the event and
61 address of the callback function that will handle that event.
62
63 The header @file{gcc-plugin.h} must be the first gcc header to be included.
64
65 @subsection Plugin license check
66
67 Every plugin should define the global symbol @code{plugin_is_GPL_compatible}
68 to assert that it has been licensed under a GPL-compatible license.
69 If this symbol does not exist, the compiler will emit a fatal error
70 and exit with the error message:
71
72 @smallexample
73 fatal error: plugin @var{name} is not licensed under a GPL-compatible license
74 @var{name}: undefined symbol: plugin_is_GPL_compatible
75 compilation terminated
76 @end smallexample
77
78 The declared type of the symbol should be int, to match a forward declaration
79 in @file{gcc-plugin.h} that suppresses C++ mangling.  It does not need to be in
80 any allocated section, though.  The compiler merely asserts that
81 the symbol exists in the global scope.  Something like this is enough:
82
83 @smallexample
84 int plugin_is_GPL_compatible;
85 @end smallexample
86
87 @subsection Plugin initialization
88
89 Every plugin should export a function called @code{plugin_init} that
90 is called right after the plugin is loaded. This function is
91 responsible for registering all the callbacks required by the plugin
92 and do any other required initialization.
93
94 This function is called from @code{compile_file} right before invoking
95 the parser.  The arguments to @code{plugin_init} are:
96
97 @itemize @bullet
98 @item @code{plugin_info}: Plugin invocation information.
99 @item @code{version}: GCC version.
100 @end itemize
101
102 The @code{plugin_info} struct is defined as follows:
103
104 @smallexample
105 struct plugin_name_args
106 @{
107   char *base_name;              /* Short name of the plugin
108                                    (filename without .so suffix). */
109   const char *full_name;        /* Path to the plugin as specified with
110                                    -fplugin=. */
111   int argc;                     /* Number of arguments specified with
112                                    -fplugin-arg-.... */
113   struct plugin_argument *argv; /* Array of ARGC key-value pairs. */
114   const char *version;          /* Version string provided by plugin. */
115   const char *help;             /* Help string provided by plugin. */
116 @}
117 @end smallexample
118
119 If initialization fails, @code{plugin_init} must return a non-zero
120 value.  Otherwise, it should return 0.
121
122 The version of the GCC compiler loading the plugin is described by the
123 following structure:
124
125 @smallexample
126 struct plugin_gcc_version
127 @{
128   const char *basever;
129   const char *datestamp;
130   const char *devphase;
131   const char *revision;
132   const char *configuration_arguments;
133 @};
134 @end smallexample
135
136 The function @code{plugin_default_version_check} takes two pointers to
137 such structure and compare them field by field. It can be used by the
138 plugin's @code{plugin_init} function.
139
140 The version of GCC used to compile the plugin can be found in the symbol
141 @code{gcc_version} defined in the header @file{plugin-version.h}. The
142 recommended version check to perform looks like
143
144 @smallexample
145 #include "plugin-version.h"
146 ...
147
148 int
149 plugin_init (struct plugin_name_args *plugin_info,
150              struct plugin_gcc_version *version)
151 @{
152   if (!plugin_default_version_check (version, &gcc_version))
153     return 1;
154
155 @}
156 @end smallexample
157
158 but you can also check the individual fields if you want a less strict check.
159
160 @subsection Plugin callbacks
161
162 Callback functions have the following prototype:
163
164 @smallexample
165 /* The prototype for a plugin callback function.
166      gcc_data  - event-specific data provided by GCC
167      user_data - plugin-specific data provided by the plug-in.  */
168 typedef void (*plugin_callback_func)(void *gcc_data, void *user_data);
169 @end smallexample
170
171 Callbacks can be invoked at the following pre-determined events:
172
173
174 @smallexample
175 enum plugin_event
176 @{
177   PLUGIN_PASS_MANAGER_SETUP,    /* To hook into pass manager.  */
178   PLUGIN_FINISH_TYPE,           /* After finishing parsing a type.  */
179   PLUGIN_FINISH_DECL,           /* After finishing parsing a declaration. */
180   PLUGIN_FINISH_UNIT,           /* Useful for summary processing.  */
181   PLUGIN_PRE_GENERICIZE,        /* Allows to see low level AST in C and C++ frontends.  */
182   PLUGIN_FINISH,                /* Called before GCC exits.  */
183   PLUGIN_INFO,                  /* Information about the plugin. */
184   PLUGIN_GGC_START,             /* Called at start of GCC Garbage Collection. */
185   PLUGIN_GGC_MARKING,           /* Extend the GGC marking. */
186   PLUGIN_GGC_END,               /* Called at end of GGC. */
187   PLUGIN_REGISTER_GGC_ROOTS,    /* Register an extra GGC root table. */
188   PLUGIN_ATTRIBUTES,            /* Called during attribute registration */
189   PLUGIN_START_UNIT,            /* Called before processing a translation unit.  */
190   PLUGIN_PRAGMAS,               /* Called during pragma registration. */
191   /* Called before first pass from all_passes.  */
192   PLUGIN_ALL_PASSES_START,
193   /* Called after last pass from all_passes.  */
194   PLUGIN_ALL_PASSES_END,
195   /* Called before first ipa pass.  */
196   PLUGIN_ALL_IPA_PASSES_START,
197   /* Called after last ipa pass.  */
198   PLUGIN_ALL_IPA_PASSES_END,
199   /* Allows to override pass gate decision for current_pass.  */
200   PLUGIN_OVERRIDE_GATE,
201   /* Called before executing a pass.  */
202   PLUGIN_PASS_EXECUTION,
203   /* Called before executing subpasses of a GIMPLE_PASS in
204      execute_ipa_pass_list.  */
205   PLUGIN_EARLY_GIMPLE_PASSES_START,
206   /* Called after executing subpasses of a GIMPLE_PASS in
207      execute_ipa_pass_list.  */
208   PLUGIN_EARLY_GIMPLE_PASSES_END,
209   /* Called when a pass is first instantiated.  */
210   PLUGIN_NEW_PASS,
211 /* Called when a file is #include-d or given via the #line directive.
212    This could happen many times.  The event data is the included file path,
213    as a const char* pointer.  */
214   PLUGIN_INCLUDE_FILE,
215
216   PLUGIN_EVENT_FIRST_DYNAMIC    /* Dummy event used for indexing callback
217                                    array.  */
218 @};
219 @end smallexample
220
221 In addition, plugins can also look up the enumerator of a named event,
222 and / or generate new events dynamically, by calling the function
223 @code{get_named_event_id}.
224
225 To register a callback, the plugin calls @code{register_callback} with
226 the arguments:
227
228 @itemize
229 @item @code{char *name}: Plugin name.
230 @item @code{int event}: The event code.
231 @item @code{plugin_callback_func callback}: The function that handles @code{event}.
232 @item @code{void *user_data}: Pointer to plugin-specific data.
233 @end itemize
234
235 For the @i{PLUGIN_PASS_MANAGER_SETUP}, @i{PLUGIN_INFO}, and
236 @i{PLUGIN_REGISTER_GGC_ROOTS} pseudo-events the @code{callback} should be null,
237 and the @code{user_data} is specific.
238
239 When the @i{PLUGIN_PRAGMAS} event is triggered (with a null pointer as
240 data from GCC), plugins may register their own pragmas.  Notice that
241 pragmas are not available from @file{lto1}, so plugins used with
242 @code{-flto} option to GCC during link-time optimization cannot use
243 pragmas and do not even see functions like @code{c_register_pragma} or
244 @code{pragma_lex}.
245
246 The @i{PLUGIN_INCLUDE_FILE} event, with a @code{const char*} file path as
247 GCC data, is triggered for processing of @code{#include} or
248 @code{#line} directives.
249
250 The @i{PLUGIN_FINISH} event is the last time that plugins can call GCC
251 functions, notably emit diagnostics with @code{warning}, @code{error}
252 etc.
253
254
255 @node Plugins pass
256 @section Interacting with the pass manager
257
258 There needs to be a way to add/reorder/remove passes dynamically. This
259 is useful for both analysis plugins (plugging in after a certain pass
260 such as CFG or an IPA pass) and optimization plugins.
261
262 Basic support for inserting new passes or replacing existing passes is
263 provided. A plugin registers a new pass with GCC by calling
264 @code{register_callback} with the @code{PLUGIN_PASS_MANAGER_SETUP}
265 event and a pointer to a @code{struct register_pass_info} object defined as follows
266
267 @smallexample
268 enum pass_positioning_ops
269 @{
270   PASS_POS_INSERT_AFTER,  // Insert after the reference pass.
271   PASS_POS_INSERT_BEFORE, // Insert before the reference pass.
272   PASS_POS_REPLACE        // Replace the reference pass.
273 @};
274
275 struct register_pass_info
276 @{
277   struct opt_pass *pass;            /* New pass provided by the plugin.  */
278   const char *reference_pass_name;  /* Name of the reference pass for hooking
279                                        up the new pass.  */
280   int ref_pass_instance_number;     /* Insert the pass at the specified
281                                        instance number of the reference pass.  */
282                                     /* Do it for every instance if it is 0.  */
283   enum pass_positioning_ops pos_op; /* how to insert the new pass.  */
284 @};
285
286
287 /* Sample plugin code that registers a new pass.  */
288 int
289 plugin_init (struct plugin_name_args *plugin_info,
290              struct plugin_gcc_version *version)
291 @{
292   struct register_pass_info pass_info;
293
294   ...
295
296   /* Code to fill in the pass_info object with new pass information.  */
297
298   ...
299
300   /* Register the new pass.  */
301   register_callback (plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &pass_info);
302
303   ...
304 @}
305 @end smallexample
306
307
308 @node Plugins GC
309 @section Interacting with the GCC Garbage Collector
310
311 Some plugins may want to be informed when GGC (the GCC Garbage
312 Collector) is running. They can register callbacks for the
313 @code{PLUGIN_GGC_START} and @code{PLUGIN_GGC_END} events (for which
314 the callback is called with a null @code{gcc_data}) to be notified of
315 the start or end of the GCC garbage collection.
316
317 Some plugins may need to have GGC mark additional data. This can be
318 done by registering a callback (called with a null @code{gcc_data})
319 for the @code{PLUGIN_GGC_MARKING} event. Such callbacks can call the
320 @code{ggc_set_mark} routine, preferably through the @code{ggc_mark} macro
321 (and conversely, these routines should usually not be used in plugins
322 outside of the @code{PLUGIN_GGC_MARKING} event).  Plugins that wish to hold
323 weak references to gc data may also use this event to drop weak references when
324 the object is about to be collected.  The @code{ggc_marked_p} function can be
325 used to tell if an object is marked, or is about to  be collected.  The
326 @code{gt_clear_cache} overloads which some types define may also be of use in
327 managing weak references.
328
329 Some plugins may need to add extra GGC root tables, e.g. to handle their own
330 @code{GTY}-ed data. This can be done with the @code{PLUGIN_REGISTER_GGC_ROOTS}
331 pseudo-event with a null callback and the extra root table (of type @code{struct
332 ggc_root_tab*}) as @code{user_data}.  Running the
333  @code{gengtype -p @var{source-dir} @var{file-list} @var{plugin*.c} ...}
334 utility generates these extra root tables.
335
336 You should understand the details of memory management inside GCC
337 before using @code{PLUGIN_GGC_MARKING} or @code{PLUGIN_REGISTER_GGC_ROOTS}.
338
339
340 @node Plugins description
341 @section Giving information about a plugin
342
343 A plugin should give some information to the user about itself. This
344 uses the following structure:
345
346 @smallexample
347 struct plugin_info
348 @{
349   const char *version;
350   const char *help;
351 @};
352 @end smallexample
353
354 Such a structure is passed as the @code{user_data} by the plugin's
355 init routine using @code{register_callback} with the
356 @code{PLUGIN_INFO} pseudo-event and a null callback.
357
358 @node Plugins attr
359 @section Registering custom attributes or pragmas
360
361 For analysis (or other) purposes it is useful to be able to add custom
362 attributes or pragmas.
363
364 The @code{PLUGIN_ATTRIBUTES} callback is called during attribute
365 registration. Use the @code{register_attribute} function to register
366 custom attributes.
367
368 @smallexample
369 /* Attribute handler callback */
370 static tree
371 handle_user_attribute (tree *node, tree name, tree args,
372                        int flags, bool *no_add_attrs)
373 @{
374   return NULL_TREE;
375 @}
376
377 /* Attribute definition */
378 static struct attribute_spec user_attr =
379   @{ "user", 1, 1, false,  false, false, handle_user_attribute, false @};
380
381 /* Plugin callback called during attribute registration.
382 Registered with register_callback (plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL)
383 */
384 static void
385 register_attributes (void *event_data, void *data)
386 @{
387   warning (0, G_("Callback to register attributes"));
388   register_attribute (&user_attr);
389 @}
390
391 @end smallexample
392
393
394 The @i{PLUGIN_PRAGMAS} callback is called once during pragmas
395 registration. Use the @code{c_register_pragma},
396 @code{c_register_pragma_with_data},
397 @code{c_register_pragma_with_expansion},
398 @code{c_register_pragma_with_expansion_and_data} functions to register
399 custom pragmas and their handlers (which often want to call
400 @code{pragma_lex}) from @file{c-family/c-pragma.h}.
401
402 @smallexample
403 /* Plugin callback called during pragmas registration. Registered with
404      register_callback (plugin_name, PLUGIN_PRAGMAS,
405                         register_my_pragma, NULL);
406 */
407 static void
408 register_my_pragma (void *event_data, void *data)
409 @{
410   warning (0, G_("Callback to register pragmas"));
411   c_register_pragma ("GCCPLUGIN", "sayhello", handle_pragma_sayhello);
412 @}
413 @end smallexample
414
415 It is suggested to pass @code{"GCCPLUGIN"} (or a short name identifying
416 your plugin) as the ``space'' argument of your pragma.
417
418 Pragmas registered with @code{c_register_pragma_with_expansion} or
419 @code{c_register_pragma_with_expansion_and_data} support
420 preprocessor expansions. For example:
421
422 @smallexample
423 #define NUMBER 10
424 #pragma GCCPLUGIN foothreshold (NUMBER)
425 @end smallexample
426
427 @node Plugins recording
428 @section Recording information about pass execution
429
430 The event PLUGIN_PASS_EXECUTION passes the pointer to the executed pass
431 (the same as current_pass) as @code{gcc_data} to the callback.  You can also
432 inspect cfun to find out about which function this pass is executed for.
433 Note that this event will only be invoked if the gate check (if
434 applicable, modified by PLUGIN_OVERRIDE_GATE) succeeds.
435 You can use other hooks, like @code{PLUGIN_ALL_PASSES_START},
436 @code{PLUGIN_ALL_PASSES_END}, @code{PLUGIN_ALL_IPA_PASSES_START},
437 @code{PLUGIN_ALL_IPA_PASSES_END}, @code{PLUGIN_EARLY_GIMPLE_PASSES_START},
438 and/or @code{PLUGIN_EARLY_GIMPLE_PASSES_END} to manipulate global state
439 in your plugin(s) in order to get context for the pass execution.
440
441
442 @node Plugins gate
443 @section Controlling which passes are being run
444
445 After the original gate function for a pass is called, its result
446 - the gate status - is stored as an integer.
447 Then the event @code{PLUGIN_OVERRIDE_GATE} is invoked, with a pointer
448 to the gate status in the @code{gcc_data} parameter to the callback function.
449 A nonzero value of the gate status means that the pass is to be executed.
450 You can both read and write the gate status via the passed pointer.
451
452
453 @node Plugins tracking
454 @section Keeping track of available passes
455
456 When your plugin is loaded, you can inspect the various
457 pass lists to determine what passes are available.  However, other
458 plugins might add new passes.  Also, future changes to GCC might cause
459 generic passes to be added after plugin loading.
460 When a pass is first added to one of the pass lists, the event
461 @code{PLUGIN_NEW_PASS} is invoked, with the callback parameter
462 @code{gcc_data} pointing to the new pass.
463
464
465 @node Plugins building
466 @section Building GCC plugins
467
468 If plugins are enabled, GCC installs the headers needed to build a
469 plugin (somewhere in the installation tree, e.g. under
470 @file{/usr/local}).  In particular a @file{plugin/include} directory
471 is installed, containing all the header files needed to build plugins.
472
473 On most systems, you can query this @code{plugin} directory by
474 invoking @command{gcc -print-file-name=plugin} (replace if needed
475 @command{gcc} with the appropriate program path).
476
477 Inside plugins, this @code{plugin} directory name can be queried by
478 calling @code{default_plugin_dir_name ()}.
479
480 Plugins may know, when they are compiled, the GCC version for which
481 @file{plugin-version.h} is provided.  The constant macros
482 @code{GCCPLUGIN_VERSION_MAJOR}, @code{GCCPLUGIN_VERSION_MINOR},
483 @code{GCCPLUGIN_VERSION_PATCHLEVEL}, @code{GCCPLUGIN_VERSION} are
484 integer numbers, so a plugin could ensure it is built for GCC 4.7 with 
485 @smallexample
486 #if GCCPLUGIN_VERSION != 4007
487 #error this GCC plugin is for GCC 4.7
488 #endif
489 @end smallexample
490
491 The following GNU Makefile excerpt shows how to build a simple plugin:
492
493 @smallexample
494 HOST_GCC=g++
495 TARGET_GCC=gcc
496 PLUGIN_SOURCE_FILES= plugin1.c plugin2.cc
497 GCCPLUGINS_DIR:= $(shell $(TARGET_GCC) -print-file-name=plugin)
498 CXXFLAGS+= -I$(GCCPLUGINS_DIR)/include -fPIC -fno-rtti -O2
499
500 plugin.so: $(PLUGIN_SOURCE_FILES)
501    $(HOST_GCC) -shared $(CXXFLAGS) $^ -o $@@
502 @end smallexample
503
504 A single source file plugin may be built with @code{g++ -I`gcc
505 -print-file-name=plugin`/include -fPIC -shared -fno-rtti -O2 plugin.c -o
506 plugin.so}, using backquote shell syntax to query the @file{plugin}
507 directory.
508
509 When a plugin needs to use @command{gengtype}, be sure that both
510 @file{gengtype} and @file{gtype.state} have the same version as the
511 GCC for which the plugin is built.