xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/doc/plugins.texi (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1*8feb0f0bSmrg@c Copyright (C) 2009-2020 Free Software Foundation, Inc.
21debfc3dSmrg@c Free Software Foundation, Inc.
31debfc3dSmrg@c This is part of the GCC manual.
41debfc3dSmrg@c For copying conditions, see the file gcc.texi.
51debfc3dSmrg
61debfc3dSmrg@node Plugins
71debfc3dSmrg@chapter Plugins
81debfc3dSmrg@cindex Plugins
91debfc3dSmrg
101debfc3dSmrgGCC plugins are loadable modules that provide extra features to the
111debfc3dSmrgcompiler.  Like GCC itself they can be distributed in source and
121debfc3dSmrgbinary forms.
131debfc3dSmrg
141debfc3dSmrgGCC plugins provide developers with a rich subset of
151debfc3dSmrgthe GCC API to allow them to extend GCC as they see fit.
161debfc3dSmrgWhether it is writing an additional optimization pass,
171debfc3dSmrgtransforming code, or analyzing information, plugins
181debfc3dSmrgcan be quite useful.
191debfc3dSmrg
201debfc3dSmrg@menu
211debfc3dSmrg* Plugins loading::      How can we load plugins.
221debfc3dSmrg* Plugin API::           The APIs for plugins.
231debfc3dSmrg* Plugins pass::         How a plugin interact with the pass manager.
241debfc3dSmrg* Plugins GC::           How a plugin Interact with GCC Garbage Collector.
251debfc3dSmrg* Plugins description::  Giving information about a plugin itself.
261debfc3dSmrg* Plugins attr::         Registering custom attributes or pragmas.
271debfc3dSmrg* Plugins recording::    Recording information about pass execution.
281debfc3dSmrg* Plugins gate::         Controlling which passes are being run.
291debfc3dSmrg* Plugins tracking::     Keeping track of available passes.
301debfc3dSmrg* Plugins building::     How can we build a plugin.
311debfc3dSmrg@end menu
321debfc3dSmrg
331debfc3dSmrg@node Plugins loading
341debfc3dSmrg@section Loading Plugins
351debfc3dSmrg
361debfc3dSmrgPlugins are supported on platforms that support @option{-ldl
37a2dc1f3fSmrg-rdynamic} as well as Windows/MinGW. They are loaded by the compiler
38a2dc1f3fSmrgusing @code{dlopen} or equivalent and invoked at pre-determined
39a2dc1f3fSmrglocations in the compilation process.
401debfc3dSmrg
411debfc3dSmrgPlugins are loaded with
421debfc3dSmrg
43a2dc1f3fSmrg@option{-fplugin=/path/to/@var{name}.@var{ext}} @option{-fplugin-arg-@var{name}-@var{key1}[=@var{value1}]}
441debfc3dSmrg
45a2dc1f3fSmrgWhere @var{name} is the plugin name and @var{ext} is the platform-specific
46a2dc1f3fSmrgdynamic library extension. It should be @code{dll} on Windows/MinGW,
47a2dc1f3fSmrg@code{dylib} on Darwin/Mac OS X, and @code{so} on all other platforms.
481debfc3dSmrgThe plugin arguments are parsed by GCC and passed to respective
491debfc3dSmrgplugins as key-value pairs. Multiple plugins can be invoked by
501debfc3dSmrgspecifying multiple @option{-fplugin} arguments.
511debfc3dSmrg
521debfc3dSmrgA plugin can be simply given by its short name (no dots or
531debfc3dSmrgslashes). When simply passing @option{-fplugin=@var{name}}, the plugin is
541debfc3dSmrgloaded from the @file{plugin} directory, so @option{-fplugin=@var{name}} is
55a2dc1f3fSmrgthe same as @option{-fplugin=`gcc -print-file-name=plugin`/@var{name}.@var{ext}},
561debfc3dSmrgusing backquote shell syntax to query the @file{plugin} directory.
571debfc3dSmrg
581debfc3dSmrg@node Plugin API
591debfc3dSmrg@section Plugin API
601debfc3dSmrg
611debfc3dSmrgPlugins are activated by the compiler at specific events as defined in
621debfc3dSmrg@file{gcc-plugin.h}.  For each event of interest, the plugin should
631debfc3dSmrgcall @code{register_callback} specifying the name of the event and
641debfc3dSmrgaddress of the callback function that will handle that event.
651debfc3dSmrg
661debfc3dSmrgThe header @file{gcc-plugin.h} must be the first gcc header to be included.
671debfc3dSmrg
681debfc3dSmrg@subsection Plugin license check
691debfc3dSmrg
701debfc3dSmrgEvery plugin should define the global symbol @code{plugin_is_GPL_compatible}
711debfc3dSmrgto assert that it has been licensed under a GPL-compatible license.
721debfc3dSmrgIf this symbol does not exist, the compiler will emit a fatal error
731debfc3dSmrgand exit with the error message:
741debfc3dSmrg
751debfc3dSmrg@smallexample
761debfc3dSmrgfatal error: plugin @var{name} is not licensed under a GPL-compatible license
771debfc3dSmrg@var{name}: undefined symbol: plugin_is_GPL_compatible
781debfc3dSmrgcompilation terminated
791debfc3dSmrg@end smallexample
801debfc3dSmrg
811debfc3dSmrgThe declared type of the symbol should be int, to match a forward declaration
821debfc3dSmrgin @file{gcc-plugin.h} that suppresses C++ mangling.  It does not need to be in
831debfc3dSmrgany allocated section, though.  The compiler merely asserts that
841debfc3dSmrgthe symbol exists in the global scope.  Something like this is enough:
851debfc3dSmrg
861debfc3dSmrg@smallexample
871debfc3dSmrgint plugin_is_GPL_compatible;
881debfc3dSmrg@end smallexample
891debfc3dSmrg
901debfc3dSmrg@subsection Plugin initialization
911debfc3dSmrg
921debfc3dSmrgEvery plugin should export a function called @code{plugin_init} that
931debfc3dSmrgis called right after the plugin is loaded. This function is
941debfc3dSmrgresponsible for registering all the callbacks required by the plugin
951debfc3dSmrgand do any other required initialization.
961debfc3dSmrg
971debfc3dSmrgThis function is called from @code{compile_file} right before invoking
981debfc3dSmrgthe parser.  The arguments to @code{plugin_init} are:
991debfc3dSmrg
1001debfc3dSmrg@itemize @bullet
1011debfc3dSmrg@item @code{plugin_info}: Plugin invocation information.
1021debfc3dSmrg@item @code{version}: GCC version.
1031debfc3dSmrg@end itemize
1041debfc3dSmrg
1051debfc3dSmrgThe @code{plugin_info} struct is defined as follows:
1061debfc3dSmrg
1071debfc3dSmrg@smallexample
1081debfc3dSmrgstruct plugin_name_args
1091debfc3dSmrg@{
1101debfc3dSmrg  char *base_name;              /* Short name of the plugin
1111debfc3dSmrg                                   (filename without .so suffix). */
1121debfc3dSmrg  const char *full_name;        /* Path to the plugin as specified with
1131debfc3dSmrg                                   -fplugin=. */
1141debfc3dSmrg  int argc;                     /* Number of arguments specified with
1151debfc3dSmrg                                   -fplugin-arg-.... */
1161debfc3dSmrg  struct plugin_argument *argv; /* Array of ARGC key-value pairs. */
1171debfc3dSmrg  const char *version;          /* Version string provided by plugin. */
1181debfc3dSmrg  const char *help;             /* Help string provided by plugin. */
1191debfc3dSmrg@}
1201debfc3dSmrg@end smallexample
1211debfc3dSmrg
1221debfc3dSmrgIf initialization fails, @code{plugin_init} must return a non-zero
1231debfc3dSmrgvalue.  Otherwise, it should return 0.
1241debfc3dSmrg
1251debfc3dSmrgThe version of the GCC compiler loading the plugin is described by the
1261debfc3dSmrgfollowing structure:
1271debfc3dSmrg
1281debfc3dSmrg@smallexample
1291debfc3dSmrgstruct plugin_gcc_version
1301debfc3dSmrg@{
1311debfc3dSmrg  const char *basever;
1321debfc3dSmrg  const char *datestamp;
1331debfc3dSmrg  const char *devphase;
1341debfc3dSmrg  const char *revision;
1351debfc3dSmrg  const char *configuration_arguments;
1361debfc3dSmrg@};
1371debfc3dSmrg@end smallexample
1381debfc3dSmrg
1391debfc3dSmrgThe function @code{plugin_default_version_check} takes two pointers to
1401debfc3dSmrgsuch structure and compare them field by field. It can be used by the
1411debfc3dSmrgplugin's @code{plugin_init} function.
1421debfc3dSmrg
1431debfc3dSmrgThe version of GCC used to compile the plugin can be found in the symbol
1441debfc3dSmrg@code{gcc_version} defined in the header @file{plugin-version.h}. The
1451debfc3dSmrgrecommended version check to perform looks like
1461debfc3dSmrg
1471debfc3dSmrg@smallexample
1481debfc3dSmrg#include "plugin-version.h"
1491debfc3dSmrg...
1501debfc3dSmrg
1511debfc3dSmrgint
1521debfc3dSmrgplugin_init (struct plugin_name_args *plugin_info,
1531debfc3dSmrg             struct plugin_gcc_version *version)
1541debfc3dSmrg@{
1551debfc3dSmrg  if (!plugin_default_version_check (version, &gcc_version))
1561debfc3dSmrg    return 1;
1571debfc3dSmrg
1581debfc3dSmrg@}
1591debfc3dSmrg@end smallexample
1601debfc3dSmrg
1611debfc3dSmrgbut you can also check the individual fields if you want a less strict check.
1621debfc3dSmrg
1631debfc3dSmrg@subsection Plugin callbacks
1641debfc3dSmrg
1651debfc3dSmrgCallback functions have the following prototype:
1661debfc3dSmrg
1671debfc3dSmrg@smallexample
1681debfc3dSmrg/* The prototype for a plugin callback function.
1691debfc3dSmrg     gcc_data  - event-specific data provided by GCC
1701debfc3dSmrg     user_data - plugin-specific data provided by the plug-in.  */
1711debfc3dSmrgtypedef void (*plugin_callback_func)(void *gcc_data, void *user_data);
1721debfc3dSmrg@end smallexample
1731debfc3dSmrg
1741debfc3dSmrgCallbacks can be invoked at the following pre-determined events:
1751debfc3dSmrg
1761debfc3dSmrg
1771debfc3dSmrg@smallexample
1781debfc3dSmrgenum plugin_event
1791debfc3dSmrg@{
1801debfc3dSmrg  PLUGIN_START_PARSE_FUNCTION,  /* Called before parsing the body of a function. */
1811debfc3dSmrg  PLUGIN_FINISH_PARSE_FUNCTION, /* After finishing parsing a function. */
1821debfc3dSmrg  PLUGIN_PASS_MANAGER_SETUP,    /* To hook into pass manager.  */
1831debfc3dSmrg  PLUGIN_FINISH_TYPE,           /* After finishing parsing a type.  */
1841debfc3dSmrg  PLUGIN_FINISH_DECL,           /* After finishing parsing a declaration. */
1851debfc3dSmrg  PLUGIN_FINISH_UNIT,           /* Useful for summary processing.  */
1861debfc3dSmrg  PLUGIN_PRE_GENERICIZE,        /* Allows to see low level AST in C and C++ frontends.  */
1871debfc3dSmrg  PLUGIN_FINISH,                /* Called before GCC exits.  */
1881debfc3dSmrg  PLUGIN_INFO,                  /* Information about the plugin. */
1891debfc3dSmrg  PLUGIN_GGC_START,             /* Called at start of GCC Garbage Collection. */
1901debfc3dSmrg  PLUGIN_GGC_MARKING,           /* Extend the GGC marking. */
1911debfc3dSmrg  PLUGIN_GGC_END,               /* Called at end of GGC. */
1921debfc3dSmrg  PLUGIN_REGISTER_GGC_ROOTS,    /* Register an extra GGC root table. */
1931debfc3dSmrg  PLUGIN_ATTRIBUTES,            /* Called during attribute registration */
1941debfc3dSmrg  PLUGIN_START_UNIT,            /* Called before processing a translation unit.  */
1951debfc3dSmrg  PLUGIN_PRAGMAS,               /* Called during pragma registration. */
1961debfc3dSmrg  /* Called before first pass from all_passes.  */
1971debfc3dSmrg  PLUGIN_ALL_PASSES_START,
1981debfc3dSmrg  /* Called after last pass from all_passes.  */
1991debfc3dSmrg  PLUGIN_ALL_PASSES_END,
2001debfc3dSmrg  /* Called before first ipa pass.  */
2011debfc3dSmrg  PLUGIN_ALL_IPA_PASSES_START,
2021debfc3dSmrg  /* Called after last ipa pass.  */
2031debfc3dSmrg  PLUGIN_ALL_IPA_PASSES_END,
2041debfc3dSmrg  /* Allows to override pass gate decision for current_pass.  */
2051debfc3dSmrg  PLUGIN_OVERRIDE_GATE,
2061debfc3dSmrg  /* Called before executing a pass.  */
2071debfc3dSmrg  PLUGIN_PASS_EXECUTION,
2081debfc3dSmrg  /* Called before executing subpasses of a GIMPLE_PASS in
2091debfc3dSmrg     execute_ipa_pass_list.  */
2101debfc3dSmrg  PLUGIN_EARLY_GIMPLE_PASSES_START,
2111debfc3dSmrg  /* Called after executing subpasses of a GIMPLE_PASS in
2121debfc3dSmrg     execute_ipa_pass_list.  */
2131debfc3dSmrg  PLUGIN_EARLY_GIMPLE_PASSES_END,
2141debfc3dSmrg  /* Called when a pass is first instantiated.  */
2151debfc3dSmrg  PLUGIN_NEW_PASS,
2161debfc3dSmrg/* Called when a file is #include-d or given via the #line directive.
2171debfc3dSmrg   This could happen many times.  The event data is the included file path,
2181debfc3dSmrg   as a const char* pointer.  */
2191debfc3dSmrg  PLUGIN_INCLUDE_FILE,
2201debfc3dSmrg
2211debfc3dSmrg  PLUGIN_EVENT_FIRST_DYNAMIC    /* Dummy event used for indexing callback
2221debfc3dSmrg                                   array.  */
2231debfc3dSmrg@};
2241debfc3dSmrg@end smallexample
2251debfc3dSmrg
2261debfc3dSmrgIn addition, plugins can also look up the enumerator of a named event,
2271debfc3dSmrgand / or generate new events dynamically, by calling the function
2281debfc3dSmrg@code{get_named_event_id}.
2291debfc3dSmrg
2301debfc3dSmrgTo register a callback, the plugin calls @code{register_callback} with
2311debfc3dSmrgthe arguments:
2321debfc3dSmrg
2331debfc3dSmrg@itemize
2341debfc3dSmrg@item @code{char *name}: Plugin name.
2351debfc3dSmrg@item @code{int event}: The event code.
2361debfc3dSmrg@item @code{plugin_callback_func callback}: The function that handles @code{event}.
2371debfc3dSmrg@item @code{void *user_data}: Pointer to plugin-specific data.
2381debfc3dSmrg@end itemize
2391debfc3dSmrg
2401debfc3dSmrgFor the @i{PLUGIN_PASS_MANAGER_SETUP}, @i{PLUGIN_INFO}, and
2411debfc3dSmrg@i{PLUGIN_REGISTER_GGC_ROOTS} pseudo-events the @code{callback} should be null,
2421debfc3dSmrgand the @code{user_data} is specific.
2431debfc3dSmrg
2441debfc3dSmrgWhen the @i{PLUGIN_PRAGMAS} event is triggered (with a null pointer as
2451debfc3dSmrgdata from GCC), plugins may register their own pragmas.  Notice that
2461debfc3dSmrgpragmas are not available from @file{lto1}, so plugins used with
2471debfc3dSmrg@code{-flto} option to GCC during link-time optimization cannot use
2481debfc3dSmrgpragmas and do not even see functions like @code{c_register_pragma} or
2491debfc3dSmrg@code{pragma_lex}.
2501debfc3dSmrg
2511debfc3dSmrgThe @i{PLUGIN_INCLUDE_FILE} event, with a @code{const char*} file path as
2521debfc3dSmrgGCC data, is triggered for processing of @code{#include} or
2531debfc3dSmrg@code{#line} directives.
2541debfc3dSmrg
2551debfc3dSmrgThe @i{PLUGIN_FINISH} event is the last time that plugins can call GCC
2561debfc3dSmrgfunctions, notably emit diagnostics with @code{warning}, @code{error}
2571debfc3dSmrgetc.
2581debfc3dSmrg
2591debfc3dSmrg
2601debfc3dSmrg@node Plugins pass
2611debfc3dSmrg@section Interacting with the pass manager
2621debfc3dSmrg
2631debfc3dSmrgThere needs to be a way to add/reorder/remove passes dynamically. This
2641debfc3dSmrgis useful for both analysis plugins (plugging in after a certain pass
2651debfc3dSmrgsuch as CFG or an IPA pass) and optimization plugins.
2661debfc3dSmrg
2671debfc3dSmrgBasic support for inserting new passes or replacing existing passes is
2681debfc3dSmrgprovided. A plugin registers a new pass with GCC by calling
2691debfc3dSmrg@code{register_callback} with the @code{PLUGIN_PASS_MANAGER_SETUP}
2701debfc3dSmrgevent and a pointer to a @code{struct register_pass_info} object defined as follows
2711debfc3dSmrg
2721debfc3dSmrg@smallexample
2731debfc3dSmrgenum pass_positioning_ops
2741debfc3dSmrg@{
2751debfc3dSmrg  PASS_POS_INSERT_AFTER,  // Insert after the reference pass.
2761debfc3dSmrg  PASS_POS_INSERT_BEFORE, // Insert before the reference pass.
2771debfc3dSmrg  PASS_POS_REPLACE        // Replace the reference pass.
2781debfc3dSmrg@};
2791debfc3dSmrg
2801debfc3dSmrgstruct register_pass_info
2811debfc3dSmrg@{
2821debfc3dSmrg  struct opt_pass *pass;            /* New pass provided by the plugin.  */
2831debfc3dSmrg  const char *reference_pass_name;  /* Name of the reference pass for hooking
2841debfc3dSmrg                                       up the new pass.  */
2851debfc3dSmrg  int ref_pass_instance_number;     /* Insert the pass at the specified
2861debfc3dSmrg                                       instance number of the reference pass.  */
2871debfc3dSmrg                                    /* Do it for every instance if it is 0.  */
2881debfc3dSmrg  enum pass_positioning_ops pos_op; /* how to insert the new pass.  */
2891debfc3dSmrg@};
2901debfc3dSmrg
2911debfc3dSmrg
2921debfc3dSmrg/* Sample plugin code that registers a new pass.  */
2931debfc3dSmrgint
2941debfc3dSmrgplugin_init (struct plugin_name_args *plugin_info,
2951debfc3dSmrg             struct plugin_gcc_version *version)
2961debfc3dSmrg@{
2971debfc3dSmrg  struct register_pass_info pass_info;
2981debfc3dSmrg
2991debfc3dSmrg  ...
3001debfc3dSmrg
3011debfc3dSmrg  /* Code to fill in the pass_info object with new pass information.  */
3021debfc3dSmrg
3031debfc3dSmrg  ...
3041debfc3dSmrg
3051debfc3dSmrg  /* Register the new pass.  */
3061debfc3dSmrg  register_callback (plugin_info->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &pass_info);
3071debfc3dSmrg
3081debfc3dSmrg  ...
3091debfc3dSmrg@}
3101debfc3dSmrg@end smallexample
3111debfc3dSmrg
3121debfc3dSmrg
3131debfc3dSmrg@node Plugins GC
3141debfc3dSmrg@section Interacting with the GCC Garbage Collector
3151debfc3dSmrg
3161debfc3dSmrgSome plugins may want to be informed when GGC (the GCC Garbage
3171debfc3dSmrgCollector) is running. They can register callbacks for the
3181debfc3dSmrg@code{PLUGIN_GGC_START} and @code{PLUGIN_GGC_END} events (for which
3191debfc3dSmrgthe callback is called with a null @code{gcc_data}) to be notified of
3201debfc3dSmrgthe start or end of the GCC garbage collection.
3211debfc3dSmrg
3221debfc3dSmrgSome plugins may need to have GGC mark additional data. This can be
3231debfc3dSmrgdone by registering a callback (called with a null @code{gcc_data})
3241debfc3dSmrgfor the @code{PLUGIN_GGC_MARKING} event. Such callbacks can call the
3251debfc3dSmrg@code{ggc_set_mark} routine, preferably through the @code{ggc_mark} macro
3261debfc3dSmrg(and conversely, these routines should usually not be used in plugins
3271debfc3dSmrgoutside of the @code{PLUGIN_GGC_MARKING} event).  Plugins that wish to hold
3281debfc3dSmrgweak references to gc data may also use this event to drop weak references when
3291debfc3dSmrgthe object is about to be collected.  The @code{ggc_marked_p} function can be
3301debfc3dSmrgused to tell if an object is marked, or is about to  be collected.  The
3311debfc3dSmrg@code{gt_clear_cache} overloads which some types define may also be of use in
3321debfc3dSmrgmanaging weak references.
3331debfc3dSmrg
334c0a68be4SmrgSome plugins may need to add extra GGC root tables, e.g.@: to handle their own
3351debfc3dSmrg@code{GTY}-ed data. This can be done with the @code{PLUGIN_REGISTER_GGC_ROOTS}
3361debfc3dSmrgpseudo-event with a null callback and the extra root table (of type @code{struct
3371debfc3dSmrgggc_root_tab*}) as @code{user_data}.  Running the
3381debfc3dSmrg @code{gengtype -p @var{source-dir} @var{file-list} @var{plugin*.c} ...}
3391debfc3dSmrgutility generates these extra root tables.
3401debfc3dSmrg
3411debfc3dSmrgYou should understand the details of memory management inside GCC
3421debfc3dSmrgbefore using @code{PLUGIN_GGC_MARKING} or @code{PLUGIN_REGISTER_GGC_ROOTS}.
3431debfc3dSmrg
3441debfc3dSmrg
3451debfc3dSmrg@node Plugins description
3461debfc3dSmrg@section Giving information about a plugin
3471debfc3dSmrg
3481debfc3dSmrgA plugin should give some information to the user about itself. This
3491debfc3dSmrguses the following structure:
3501debfc3dSmrg
3511debfc3dSmrg@smallexample
3521debfc3dSmrgstruct plugin_info
3531debfc3dSmrg@{
3541debfc3dSmrg  const char *version;
3551debfc3dSmrg  const char *help;
3561debfc3dSmrg@};
3571debfc3dSmrg@end smallexample
3581debfc3dSmrg
3591debfc3dSmrgSuch a structure is passed as the @code{user_data} by the plugin's
3601debfc3dSmrginit routine using @code{register_callback} with the
3611debfc3dSmrg@code{PLUGIN_INFO} pseudo-event and a null callback.
3621debfc3dSmrg
3631debfc3dSmrg@node Plugins attr
3641debfc3dSmrg@section Registering custom attributes or pragmas
3651debfc3dSmrg
3661debfc3dSmrgFor analysis (or other) purposes it is useful to be able to add custom
3671debfc3dSmrgattributes or pragmas.
3681debfc3dSmrg
3691debfc3dSmrgThe @code{PLUGIN_ATTRIBUTES} callback is called during attribute
3701debfc3dSmrgregistration. Use the @code{register_attribute} function to register
3711debfc3dSmrgcustom attributes.
3721debfc3dSmrg
3731debfc3dSmrg@smallexample
3741debfc3dSmrg/* Attribute handler callback */
3751debfc3dSmrgstatic tree
3761debfc3dSmrghandle_user_attribute (tree *node, tree name, tree args,
3771debfc3dSmrg                       int flags, bool *no_add_attrs)
3781debfc3dSmrg@{
3791debfc3dSmrg  return NULL_TREE;
3801debfc3dSmrg@}
3811debfc3dSmrg
3821debfc3dSmrg/* Attribute definition */
3831debfc3dSmrgstatic struct attribute_spec user_attr =
384a2dc1f3fSmrg  @{ "user", 1, 1, false,  false, false, false, handle_user_attribute, NULL @};
3851debfc3dSmrg
3861debfc3dSmrg/* Plugin callback called during attribute registration.
3871debfc3dSmrgRegistered with register_callback (plugin_name, PLUGIN_ATTRIBUTES, register_attributes, NULL)
3881debfc3dSmrg*/
3891debfc3dSmrgstatic void
3901debfc3dSmrgregister_attributes (void *event_data, void *data)
3911debfc3dSmrg@{
3921debfc3dSmrg  warning (0, G_("Callback to register attributes"));
3931debfc3dSmrg  register_attribute (&user_attr);
3941debfc3dSmrg@}
3951debfc3dSmrg
3961debfc3dSmrg@end smallexample
3971debfc3dSmrg
3981debfc3dSmrg
3991debfc3dSmrgThe @i{PLUGIN_PRAGMAS} callback is called once during pragmas
4001debfc3dSmrgregistration. Use the @code{c_register_pragma},
4011debfc3dSmrg@code{c_register_pragma_with_data},
4021debfc3dSmrg@code{c_register_pragma_with_expansion},
4031debfc3dSmrg@code{c_register_pragma_with_expansion_and_data} functions to register
4041debfc3dSmrgcustom pragmas and their handlers (which often want to call
4051debfc3dSmrg@code{pragma_lex}) from @file{c-family/c-pragma.h}.
4061debfc3dSmrg
4071debfc3dSmrg@smallexample
4081debfc3dSmrg/* Plugin callback called during pragmas registration. Registered with
4091debfc3dSmrg     register_callback (plugin_name, PLUGIN_PRAGMAS,
4101debfc3dSmrg                        register_my_pragma, NULL);
4111debfc3dSmrg*/
4121debfc3dSmrgstatic void
4131debfc3dSmrgregister_my_pragma (void *event_data, void *data)
4141debfc3dSmrg@{
4151debfc3dSmrg  warning (0, G_("Callback to register pragmas"));
4161debfc3dSmrg  c_register_pragma ("GCCPLUGIN", "sayhello", handle_pragma_sayhello);
4171debfc3dSmrg@}
4181debfc3dSmrg@end smallexample
4191debfc3dSmrg
4201debfc3dSmrgIt is suggested to pass @code{"GCCPLUGIN"} (or a short name identifying
4211debfc3dSmrgyour plugin) as the ``space'' argument of your pragma.
4221debfc3dSmrg
4231debfc3dSmrgPragmas registered with @code{c_register_pragma_with_expansion} or
4241debfc3dSmrg@code{c_register_pragma_with_expansion_and_data} support
4251debfc3dSmrgpreprocessor expansions. For example:
4261debfc3dSmrg
4271debfc3dSmrg@smallexample
4281debfc3dSmrg#define NUMBER 10
4291debfc3dSmrg#pragma GCCPLUGIN foothreshold (NUMBER)
4301debfc3dSmrg@end smallexample
4311debfc3dSmrg
4321debfc3dSmrg@node Plugins recording
4331debfc3dSmrg@section Recording information about pass execution
4341debfc3dSmrg
4351debfc3dSmrgThe event PLUGIN_PASS_EXECUTION passes the pointer to the executed pass
4361debfc3dSmrg(the same as current_pass) as @code{gcc_data} to the callback.  You can also
4371debfc3dSmrginspect cfun to find out about which function this pass is executed for.
4381debfc3dSmrgNote that this event will only be invoked if the gate check (if
4391debfc3dSmrgapplicable, modified by PLUGIN_OVERRIDE_GATE) succeeds.
4401debfc3dSmrgYou can use other hooks, like @code{PLUGIN_ALL_PASSES_START},
4411debfc3dSmrg@code{PLUGIN_ALL_PASSES_END}, @code{PLUGIN_ALL_IPA_PASSES_START},
4421debfc3dSmrg@code{PLUGIN_ALL_IPA_PASSES_END}, @code{PLUGIN_EARLY_GIMPLE_PASSES_START},
4431debfc3dSmrgand/or @code{PLUGIN_EARLY_GIMPLE_PASSES_END} to manipulate global state
4441debfc3dSmrgin your plugin(s) in order to get context for the pass execution.
4451debfc3dSmrg
4461debfc3dSmrg
4471debfc3dSmrg@node Plugins gate
4481debfc3dSmrg@section Controlling which passes are being run
4491debfc3dSmrg
4501debfc3dSmrgAfter the original gate function for a pass is called, its result
4511debfc3dSmrg- the gate status - is stored as an integer.
4521debfc3dSmrgThen the event @code{PLUGIN_OVERRIDE_GATE} is invoked, with a pointer
4531debfc3dSmrgto the gate status in the @code{gcc_data} parameter to the callback function.
4541debfc3dSmrgA nonzero value of the gate status means that the pass is to be executed.
4551debfc3dSmrgYou can both read and write the gate status via the passed pointer.
4561debfc3dSmrg
4571debfc3dSmrg
4581debfc3dSmrg@node Plugins tracking
4591debfc3dSmrg@section Keeping track of available passes
4601debfc3dSmrg
4611debfc3dSmrgWhen your plugin is loaded, you can inspect the various
4621debfc3dSmrgpass lists to determine what passes are available.  However, other
4631debfc3dSmrgplugins might add new passes.  Also, future changes to GCC might cause
4641debfc3dSmrggeneric passes to be added after plugin loading.
4651debfc3dSmrgWhen a pass is first added to one of the pass lists, the event
4661debfc3dSmrg@code{PLUGIN_NEW_PASS} is invoked, with the callback parameter
4671debfc3dSmrg@code{gcc_data} pointing to the new pass.
4681debfc3dSmrg
4691debfc3dSmrg
4701debfc3dSmrg@node Plugins building
4711debfc3dSmrg@section Building GCC plugins
4721debfc3dSmrg
4731debfc3dSmrgIf plugins are enabled, GCC installs the headers needed to build a
474c0a68be4Smrgplugin (somewhere in the installation tree, e.g.@: under
4751debfc3dSmrg@file{/usr/local}).  In particular a @file{plugin/include} directory
4761debfc3dSmrgis installed, containing all the header files needed to build plugins.
4771debfc3dSmrg
4781debfc3dSmrgOn most systems, you can query this @code{plugin} directory by
4791debfc3dSmrginvoking @command{gcc -print-file-name=plugin} (replace if needed
4801debfc3dSmrg@command{gcc} with the appropriate program path).
4811debfc3dSmrg
4821debfc3dSmrgInside plugins, this @code{plugin} directory name can be queried by
4831debfc3dSmrgcalling @code{default_plugin_dir_name ()}.
4841debfc3dSmrg
4851debfc3dSmrgPlugins may know, when they are compiled, the GCC version for which
4861debfc3dSmrg@file{plugin-version.h} is provided.  The constant macros
4871debfc3dSmrg@code{GCCPLUGIN_VERSION_MAJOR}, @code{GCCPLUGIN_VERSION_MINOR},
4881debfc3dSmrg@code{GCCPLUGIN_VERSION_PATCHLEVEL}, @code{GCCPLUGIN_VERSION} are
4891debfc3dSmrginteger numbers, so a plugin could ensure it is built for GCC 4.7 with
4901debfc3dSmrg@smallexample
4911debfc3dSmrg#if GCCPLUGIN_VERSION != 4007
4921debfc3dSmrg#error this GCC plugin is for GCC 4.7
4931debfc3dSmrg#endif
4941debfc3dSmrg@end smallexample
4951debfc3dSmrg
4961debfc3dSmrgThe following GNU Makefile excerpt shows how to build a simple plugin:
4971debfc3dSmrg
4981debfc3dSmrg@smallexample
4991debfc3dSmrgHOST_GCC=g++
5001debfc3dSmrgTARGET_GCC=gcc
5011debfc3dSmrgPLUGIN_SOURCE_FILES= plugin1.c plugin2.cc
5021debfc3dSmrgGCCPLUGINS_DIR:= $(shell $(TARGET_GCC) -print-file-name=plugin)
5031debfc3dSmrgCXXFLAGS+= -I$(GCCPLUGINS_DIR)/include -fPIC -fno-rtti -O2
5041debfc3dSmrg
5051debfc3dSmrgplugin.so: $(PLUGIN_SOURCE_FILES)
5061debfc3dSmrg   $(HOST_GCC) -shared $(CXXFLAGS) $^ -o $@@
5071debfc3dSmrg@end smallexample
5081debfc3dSmrg
5091debfc3dSmrgA single source file plugin may be built with @code{g++ -I`gcc
5101debfc3dSmrg-print-file-name=plugin`/include -fPIC -shared -fno-rtti -O2 plugin.c -o
5111debfc3dSmrgplugin.so}, using backquote shell syntax to query the @file{plugin}
5121debfc3dSmrgdirectory.
5131debfc3dSmrg
514a2dc1f3fSmrgPlugin support on Windows/MinGW has a number of limitations and
515a2dc1f3fSmrgadditional requirements. When building a plugin on Windows we have to
516a2dc1f3fSmrglink an import library for the corresponding backend executable, for
517a2dc1f3fSmrgexample, @file{cc1.exe}, @file{cc1plus.exe}, etc., in order to gain
518a2dc1f3fSmrgaccess to the symbols provided by GCC. This means that on Windows a
519a2dc1f3fSmrgplugin is language-specific, for example, for C, C++, etc. If you wish
520a2dc1f3fSmrgto use your plugin with multiple languages, then you will need to
521a2dc1f3fSmrgbuild multiple plugin libraries and either instruct your users on how
522a2dc1f3fSmrgto load the correct version or provide a compiler wrapper that does
523a2dc1f3fSmrgthis automatically.
524a2dc1f3fSmrg
525a2dc1f3fSmrgAdditionally, on Windows the plugin library has to export the
526a2dc1f3fSmrg@code{plugin_is_GPL_compatible} and @code{plugin_init} symbols. If you
527a2dc1f3fSmrgdo not wish to modify the source code of your plugin, then you can use
528a2dc1f3fSmrgthe @option{-Wl,--export-all-symbols} option or provide a suitable DEF
529a2dc1f3fSmrgfile. Alternatively, you can export just these two symbols by decorating
530a2dc1f3fSmrgthem with @code{__declspec(dllexport)}, for example:
531a2dc1f3fSmrg
532a2dc1f3fSmrg@smallexample
533a2dc1f3fSmrg#ifdef _WIN32
534a2dc1f3fSmrg__declspec(dllexport)
535a2dc1f3fSmrg#endif
536a2dc1f3fSmrgint plugin_is_GPL_compatible;
537a2dc1f3fSmrg
538a2dc1f3fSmrg#ifdef _WIN32
539a2dc1f3fSmrg__declspec(dllexport)
540a2dc1f3fSmrg#endif
541a2dc1f3fSmrgint plugin_init (plugin_name_args *, plugin_gcc_version *)
542a2dc1f3fSmrg@end smallexample
543a2dc1f3fSmrg
544a2dc1f3fSmrgThe import libraries are installed into the @code{plugin} directory
545a2dc1f3fSmrgand their names are derived by appending the @code{.a} extension to
546a2dc1f3fSmrgthe backend executable names, for example, @file{cc1.exe.a},
547a2dc1f3fSmrg@file{cc1plus.exe.a}, etc. The following command line shows how to
548a2dc1f3fSmrgbuild the single source file plugin on Windows to be used with the C++
549a2dc1f3fSmrgcompiler:
550a2dc1f3fSmrg
551a2dc1f3fSmrg@smallexample
552a2dc1f3fSmrgg++ -I`gcc -print-file-name=plugin`/include -shared -Wl,--export-all-symbols \
553a2dc1f3fSmrg-o plugin.dll plugin.c `gcc -print-file-name=plugin`/cc1plus.exe.a
554a2dc1f3fSmrg@end smallexample
555a2dc1f3fSmrg
5561debfc3dSmrgWhen a plugin needs to use @command{gengtype}, be sure that both
5571debfc3dSmrg@file{gengtype} and @file{gtype.state} have the same version as the
5581debfc3dSmrgGCC for which the plugin is built.
559