plugins.texi 19 KB

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