gcc-plugin-api.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* GCC plugin APIs.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  12. #include "gml4gtk-plugin.h"
  13. /* plugin license check */
  14. int plugin_is_GPL_compatible;
  15. /* */
  16. static void
  17. help (void)
  18. {
  19. printf (
  20. "Usage: gcc -fplugin=/path/to/gml4gtk_plugin.so -fplugin-arg-gml4gtk_plugin-<option> ...\n" \
  21. "Options:\n" \
  22. " cgraph dump the call graph before IPA passes.\n" \
  23. " cgraph-callee dump the callee graph for each function.\n" \
  24. " cgraph-caller dump the caller graph for each function.\n" \
  25. " gimple-hierarchy dump the gimple hierarchy graph.\n" \
  26. " help show this help.\n" \
  27. " passes dump the passes graph.\n" \
  28. " pass-lists dump the pass lists graph.\n" \
  29. " tree-hierarchy dump the tree hierarchy graph.\n" \
  30. " viewer=name set the viewer, default is gml4gtk\n" \
  31. " \n" \
  32. "%s %s \n",
  33. dot_plugin_common.plugin_name, dot_plugin_common.version);
  34. exit (0);
  35. }
  36. /* Version check. */
  37. static bool
  38. version_check (struct plugin_gcc_version *gcc_version,
  39. struct plugin_gcc_version *plugin_version)
  40. {
  41. if (!gcc_version || !plugin_version)
  42. return false;
  43. if (strcmp (gcc_version->basever, plugin_version->basever))
  44. return false;
  45. return true;
  46. }
  47. /* Plugin initialization. */
  48. int
  49. plugin_init (struct plugin_name_args *plugin_info,
  50. struct plugin_gcc_version *version)
  51. {
  52. int i;
  53. int argc = plugin_info->argc;
  54. struct plugin_argument *argv = plugin_info->argv;
  55. if (!version_check (version, &gcc_version))
  56. {
  57. dot_plugin_common.error ("version check failed. plugin compiled by gcc %s and used by %s.",
  58. version->basever, gcc_version.basever);
  59. return 1;
  60. }
  61. dot_plugin_common.gcc_basever = (char *) version->basever;
  62. dot_plugin_common.info = concat ("GCC: (GNU) ", version->basever,
  63. " ", version->datestamp, " ",
  64. "(", version->devphase, ")\n", NULL);
  65. /* Initialize the dot plugin */
  66. for (i = 0; i < argc; i++)
  67. {
  68. /* Get the viewer tool, default is "mooigrap". */
  69. if (strcmp (argv[i].key, "viewer") == 0)
  70. {
  71. dot_plugin_common.dot_viewer = argv[i].value;
  72. }
  73. /* Dump call graph. */
  74. if (strcmp (argv[i].key, "cgraph") == 0)
  75. {
  76. register_callback (plugin_info->base_name,
  77. PLUGIN_ALL_IPA_PASSES_START,
  78. (plugin_callback_func) dot_plugin_callback_cgraph,
  79. NULL);
  80. }
  81. /* Dump callee graph. */
  82. if (strcmp (argv[i].key, "cgraph-callee") == 0)
  83. {
  84. register_callback (plugin_info->base_name,
  85. PLUGIN_ALL_IPA_PASSES_START,
  86. (plugin_callback_func) dot_plugin_callback_callee,
  87. NULL);
  88. }
  89. /* Dump caller graph. */
  90. if (strcmp (argv[i].key, "cgraph-caller") == 0)
  91. {
  92. register_callback (plugin_info->base_name,
  93. PLUGIN_ALL_IPA_PASSES_START,
  94. (plugin_callback_func) dot_plugin_callback_caller,
  95. NULL);
  96. }
  97. /* Dump passes. */
  98. if (strcmp (argv[i].key, "passes") == 0)
  99. {
  100. register_callback (plugin_info->base_name,
  101. PLUGIN_START_UNIT,
  102. (plugin_callback_func) dot_plugin_callback_passes_start,
  103. NULL);
  104. register_callback (plugin_info->base_name,
  105. PLUGIN_OVERRIDE_GATE,
  106. (plugin_callback_func) dot_plugin_callback_pass,
  107. NULL);
  108. register_callback (plugin_info->base_name,
  109. PLUGIN_FINISH_UNIT,
  110. (plugin_callback_func) dot_plugin_callback_passes_finish,
  111. NULL);
  112. }
  113. /* Dump gcc pass lists. */
  114. if (strcmp (argv[i].key, "pass-lists") == 0)
  115. {
  116. register_callback (plugin_info->base_name,
  117. PLUGIN_FINISH,
  118. (plugin_callback_func) dot_plugin_callback_pass_lists,
  119. NULL);
  120. }
  121. /* Dump gimple hierarchy graph. */
  122. if (strcmp (argv[i].key, "gimple-hierarchy") == 0)
  123. {
  124. register_callback (plugin_info->base_name,
  125. PLUGIN_FINISH,
  126. (plugin_callback_func) dot_plugin_callback_gimple_hierarchy,
  127. NULL);
  128. }
  129. /* Dump tree hierarchy graph. */
  130. if (strcmp (argv[i].key, "tree-hierarchy") == 0)
  131. {
  132. register_callback (plugin_info->base_name,
  133. PLUGIN_FINISH,
  134. (plugin_callback_func) dot_plugin_callback_tree_hierarchy,
  135. NULL);
  136. }
  137. if (strcmp (argv[i].key, "help") == 0)
  138. {
  139. help ();
  140. }
  141. }
  142. return 0;
  143. }
  144. /* end */