print-tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. /* Prints out tree in human readable form - GNU C-compiler
  2. Copyright (C) 1987 Free Software Foundation, Inc.
  3. This file is part of GNU CC.
  4. GNU CC is distributed in the hope that it will be useful,
  5. but WITHOUT ANY WARRANTY. No author or distributor
  6. accepts responsibility to anyone for the consequences of using it
  7. or for whether it serves any particular purpose or works at all,
  8. unless he says so in writing. Refer to the GNU CC General Public
  9. License for full details.
  10. Everyone is granted permission to copy, modify and redistribute
  11. GNU CC, but only under the conditions described in the
  12. GNU CC General Public License. A copy of this license is
  13. supposed to have been given to you along with GNU CC so you
  14. can know your rights and responsibilities. It should be in a
  15. file named COPYING. Among other things, the copyright notice
  16. and this notice must be preserved on all copies. */
  17. #include "config.h"
  18. #include "tree.h"
  19. #include <stdio.h>
  20. /* Names of tree components.
  21. Used for printing out the tree and error messages. */
  22. #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
  23. char *tree_code_name[] = {
  24. #include "tree.def"
  25. };
  26. #undef DEFTREECODE
  27. extern char *tree_code_type[];
  28. extern int tree_code_length[];
  29. extern char *mode_name[];
  30. extern char spaces[];
  31. #define MIN(x,y) ((x < y) ? x : y)
  32. static FILE *outfile;
  33. extern int tree_node_counter;
  34. /* markvec[i] is 1 if node number i has been seen already. */
  35. static char *markvec;
  36. static void dump ();
  37. void dump_tree ();
  38. void
  39. debug_dump_tree (root)
  40. tree root;
  41. {
  42. dump_tree (stderr, root);
  43. }
  44. void
  45. dump_tree (outf, root)
  46. FILE *outf;
  47. tree root;
  48. {
  49. markvec = (char *) alloca (tree_node_counter + 1);
  50. bzero (markvec, tree_node_counter + 1);
  51. outfile = outf;
  52. dump (root, 0);
  53. fflush (outf);
  54. }
  55. static
  56. void
  57. wruid (node)
  58. tree node;
  59. {
  60. if (node == NULL)
  61. fputs ("<>", outfile);
  62. else {
  63. fprintf (outfile, "%1d", TREE_UID (node));
  64. }
  65. }
  66. static
  67. void
  68. part (title, node)
  69. char title[];
  70. tree node;
  71. {
  72. fprintf (outfile, " %s = ", title);
  73. wruid (node);
  74. putc (';', outfile);
  75. }
  76. /* Similar to `part' but prefix with @ if value is not constant
  77. and print the constant value if it is constant. */
  78. static
  79. void
  80. cpart (title, ct, punct)
  81. char *title;
  82. tree ct;
  83. char punct;
  84. {
  85. fprintf (outfile, " %s = ", title);
  86. if (ct == NULL)
  87. fputs ("<>", outfile);
  88. else
  89. {
  90. if (!TREE_LITERAL (ct))
  91. {
  92. putc ('@', outfile);
  93. wruid (ct);
  94. }
  95. else
  96. fprintf (outfile, "%ld", TREE_INT_CST_LOW (ct));
  97. }
  98. putc(punct, outfile);
  99. }
  100. static
  101. void
  102. walk (node, leaf, indent)
  103. tree node;
  104. tree leaf;
  105. int indent;
  106. {
  107. if (node != NULL
  108. /* Don't walk any global nodes reached from local nodes!
  109. The global nodes will be dumped at the end, all together.
  110. Also don't mention a FUNCTION_DECL node that is marked local
  111. since it was fully described when it was dumped locally. */
  112. && (TREE_CODE (node) != FUNCTION_DECL
  113. || TREE_PERMANENT (node))
  114. && (TREE_PERMANENT (leaf) == TREE_PERMANENT (node)))
  115. dump (node, indent+1);
  116. }
  117. static
  118. void
  119. cwalk (s, leaf, indent)
  120. tree s;
  121. tree leaf;
  122. int indent;
  123. {
  124. if (s != NULL)
  125. if (!TREE_LITERAL (s))
  126. walk(s, leaf, indent);
  127. }
  128. static
  129. void
  130. prtypeinfo (node)
  131. register tree node;
  132. {
  133. int first;
  134. part ("type", TREE_TYPE (node));
  135. first = 1;
  136. fputs (" [", outfile);
  137. if (TREE_EXTERNAL (node))
  138. {
  139. if (!first) putc (' ', outfile);
  140. fputs ("external", outfile);
  141. first = 0;
  142. }
  143. if (TREE_PUBLIC (node))
  144. {
  145. if (!first) putc (' ', outfile);
  146. fputs ("public", outfile);
  147. first = 0;
  148. }
  149. if (TREE_STATIC (node))
  150. {
  151. if (!first) putc (' ', outfile);
  152. fputs ("static", outfile);
  153. first = 0;
  154. }
  155. if (TREE_VOLATILE (node))
  156. {
  157. if (!first) putc (' ', outfile);
  158. fputs ("volatile", outfile);
  159. first = 0;
  160. }
  161. if (TREE_PACKED (node))
  162. {
  163. if (!first) putc (' ', outfile);
  164. fputs ("packed", outfile);
  165. first = 0;
  166. }
  167. if (TREE_READONLY (node))
  168. {
  169. if (!first) putc (' ', outfile);
  170. fputs ("readonly", outfile);
  171. first = 0;
  172. }
  173. if (TREE_LITERAL (node))
  174. {
  175. if (!first) putc (' ', outfile);
  176. fputs ("literal", outfile);
  177. first = 0;
  178. }
  179. if (TREE_NONLOCAL (node))
  180. {
  181. if (!first) putc (' ', outfile);
  182. fputs ("nonlocal", outfile);
  183. first = 0;
  184. }
  185. if (TREE_ADDRESSABLE (node))
  186. {
  187. if (!first) putc (' ', outfile);
  188. fputs ("addressable", outfile);
  189. first = 0;
  190. }
  191. if (TREE_REGDECL (node))
  192. {
  193. if (!first) putc (' ', outfile);
  194. fputs ("regdecl", outfile);
  195. first = 0;
  196. }
  197. if (TREE_THIS_VOLATILE (node))
  198. {
  199. if (!first) putc (' ', outfile);
  200. fputs ("this_vol", outfile);
  201. first = 0;
  202. }
  203. if (TREE_UNSIGNED (node))
  204. {
  205. if (!first) putc (' ', outfile);
  206. fputs ("unsigned", outfile);
  207. first = 0;
  208. }
  209. if (TREE_ASM_WRITTEN (node))
  210. {
  211. if (!first) putc (' ', outfile);
  212. fputs ("asm_written", outfile);
  213. first = 0;
  214. }
  215. if (TREE_INLINE (node))
  216. {
  217. if (!first) putc (' ', outfile);
  218. fputs ("inline", outfile);
  219. first = 0;
  220. }
  221. fputs ("] ", outfile);
  222. }
  223. static
  224. void
  225. prdeclmodeinfo(node)
  226. tree node;
  227. {
  228. register enum machine_mode mode = DECL_MODE (node);
  229. fprintf (outfile, " %s;", mode_name[(int) mode]);
  230. cpart ("size", DECL_SIZE (node), '*');
  231. fprintf (outfile, "%d;", DECL_SIZE_UNIT (node));
  232. fprintf (outfile, " alignment = %1d;", DECL_ALIGN (node));
  233. }
  234. static
  235. void
  236. prtypemodeinfo(node)
  237. tree node;
  238. {
  239. register enum machine_mode mode = TYPE_MODE (node);
  240. fprintf (outfile, " %s;", mode_name[(int) mode]);
  241. cpart ("size", TYPE_SIZE (node), '*');
  242. fprintf (outfile, "%d;", TYPE_SIZE_UNIT (node));
  243. fprintf (outfile, " alignment = %1d;", TYPE_ALIGN (node));
  244. }
  245. static
  246. void
  247. skip (indent)
  248. int indent;
  249. {
  250. putc ('\n',outfile);
  251. fputs (spaces + (strlen (spaces) - (12 + MIN (40,(indent+1)*2))), outfile);
  252. }
  253. /* Output a description of the tree node NODE
  254. if its description has not been output already. */
  255. static
  256. void
  257. dump (node, indent)
  258. tree node;
  259. int indent;
  260. {
  261. register enum tree_code code = TREE_CODE (node);
  262. register int i;
  263. register int len;
  264. int nochain = 0;
  265. if (markvec[TREE_UID (node)])
  266. return;
  267. markvec[TREE_UID (node)] = 1;
  268. fputs (" ", outfile);
  269. fprintf (outfile, "%5d", TREE_UID (node));
  270. fputs (spaces + (strlen (spaces) - MIN (40, (indent+1)*2)), outfile);
  271. fputs (tree_code_name[(int) code], outfile);
  272. switch (*tree_code_type[(int) code])
  273. {
  274. case 'd':
  275. fputs (" name = ", outfile);
  276. if (DECL_NAME (node) == NULL)
  277. fputs("<>;", outfile);
  278. else
  279. fprintf (outfile, "%s;",
  280. IDENTIFIER_POINTER (DECL_NAME (node)));
  281. fprintf (outfile, " at %s line %d;",
  282. DECL_SOURCE_FILE (node), DECL_SOURCE_LINE (node));
  283. skip (indent);
  284. prdeclmodeinfo (node);
  285. prtypeinfo (node);
  286. skip (indent);
  287. fprintf (outfile, " offset = %1d;", DECL_OFFSET (node));
  288. if (DECL_VOFFSET (node) != NULL)
  289. {
  290. fputs ("voffset = ", outfile);
  291. wruid (DECL_VOFFSET (node));
  292. fprintf (outfile, "*%1d;", DECL_VOFFSET_UNIT (node));
  293. }
  294. part ("context", DECL_CONTEXT (node));
  295. if (DECL_ARGUMENTS (node) || DECL_RESULT (node)
  296. || DECL_INITIAL (node))
  297. {
  298. skip(indent);
  299. part ("arguments", DECL_ARGUMENTS (node));
  300. part ("result", DECL_RESULT (node));
  301. if ((int) (DECL_INITIAL (node)) == 1)
  302. fprintf (outfile, " initial = const 1;");
  303. else
  304. part ("initial", DECL_INITIAL (node));
  305. }
  306. part ("chain", TREE_CHAIN (node));
  307. fputc ('\n', outfile);
  308. cwalk (DECL_SIZE (node), node, indent);
  309. walk (TREE_TYPE (node), node, indent);
  310. walk (DECL_VOFFSET (node), node, indent);
  311. walk (DECL_CONTEXT (node), node, indent);
  312. walk (DECL_ARGUMENTS (node), node, indent);
  313. walk (DECL_RESULT (node), node, indent);
  314. if ((int) (DECL_INITIAL (node)) != 1)
  315. walk (DECL_INITIAL (node), node, indent);
  316. break;
  317. case 't':
  318. prtypemodeinfo (node);
  319. prtypeinfo (node);
  320. skip (indent);
  321. part ("pointers_to_this", TYPE_POINTER_TO (node));
  322. if (code == ARRAY_TYPE || code == SET_TYPE)
  323. {
  324. part ("domain", TYPE_DOMAIN (node));
  325. cpart ("separation", TYPE_SEP (node), '*');
  326. fprintf (outfile, "%d;", TYPE_SEP_UNIT (node));
  327. }
  328. else if (code == INTEGER_TYPE)
  329. {
  330. cpart ("min", TYPE_MIN_VALUE (node), ';');
  331. cpart ("max", TYPE_MAX_VALUE (node), ';');
  332. fprintf (outfile, "precision = %d;", TYPE_PRECISION (node));
  333. }
  334. else if (code == ENUMERAL_TYPE)
  335. {
  336. cpart ("min", TYPE_MIN_VALUE (node), ';');
  337. cpart ("max", TYPE_MAX_VALUE (node), ';');
  338. part ("values", TYPE_VALUES (node));
  339. fprintf (outfile, "precision = %d;", TYPE_PRECISION (node));
  340. }
  341. else if (code == REAL_TYPE)
  342. {
  343. fprintf (outfile, "precision = %d;", TYPE_PRECISION (node));
  344. }
  345. else if (code == RECORD_TYPE
  346. || code == UNION_TYPE)
  347. {
  348. part ("fields", TYPE_FIELDS (node));
  349. }
  350. else if (code == FUNCTION_TYPE)
  351. {
  352. part ("arg_types", TYPE_ARG_TYPES (node));
  353. }
  354. /* A type's chain is not printed because the chain of types
  355. is not part of the meaning of any particular type. */
  356. /* part ("chain", TREE_CHAIN (node)); */
  357. nochain = 1;
  358. fputc ('\n', outfile);
  359. cwalk (TYPE_SIZE (node), node, indent);
  360. walk (TREE_TYPE (node), node, indent);
  361. walk (TYPE_VALUES (node), node, indent);
  362. walk (TYPE_SEP (node), node, indent);
  363. walk (TYPE_POINTER_TO (node), node, indent);
  364. break;
  365. case 'e':
  366. case 'r':
  367. prtypeinfo (node);
  368. fputs (" ops =", outfile);
  369. len = tree_code_length[(int) code];
  370. for (i = 0; i < len; i++)
  371. {
  372. fputs (" ", outfile);
  373. wruid (TREE_OPERAND (node, i));
  374. fputs (";", outfile);
  375. }
  376. part ("chain", TREE_CHAIN (node));
  377. fputc ('\n', outfile);
  378. walk (TREE_TYPE (node), node, indent);
  379. for (i = 0; i < len; i++)
  380. walk (TREE_OPERAND (node, i), node, indent);
  381. break;
  382. case 's':
  383. prtypeinfo (node);
  384. fprintf (outfile, " at %s line %d;",
  385. STMT_SOURCE_FILE (node), STMT_SOURCE_LINE (node));
  386. fputs (" ops =", outfile);
  387. len = tree_code_length[(int) code];
  388. for (i = 0; i < len; i++)
  389. {
  390. fputs (" ", outfile);
  391. wruid (TREE_OPERAND (node, i+2));
  392. fputs (";", outfile);
  393. }
  394. part ("chain", TREE_CHAIN (node));
  395. fputc ('\n', outfile);
  396. walk (TREE_TYPE (node), node, indent);
  397. for (i = 0; i < len; i++)
  398. walk (TREE_OPERAND (node, i+2), node, indent);
  399. break;
  400. case 'c':
  401. switch (code)
  402. {
  403. case INTEGER_CST:
  404. if (TREE_INT_CST_HIGH (node) == 0)
  405. fprintf (outfile, " = %1u;", TREE_INT_CST_LOW (node));
  406. else if (TREE_INT_CST_HIGH (node) == -1
  407. && TREE_INT_CST_LOW (node) != 0)
  408. fprintf (outfile, " = -%1u;", -TREE_INT_CST_LOW (node));
  409. else
  410. fprintf (outfile, " = 0x%x%08x;",
  411. TREE_INT_CST_HIGH (node),
  412. TREE_INT_CST_LOW (node));
  413. break;
  414. case REAL_CST:
  415. fprintf (outfile, " = %e;", TREE_REAL_CST (node));
  416. break;
  417. case COMPLEX_CST:
  418. part ("realpart", TREE_REALPART (node));
  419. part ("imagpart", TREE_IMAGPART (node));
  420. walk (TREE_REALPART (node), node, indent);
  421. walk (TREE_IMAGPART (node), node, indent);
  422. break;
  423. case STRING_CST:
  424. fprintf (outfile, " = \"%s\";", TREE_STRING_POINTER (node));
  425. }
  426. prtypeinfo(node);
  427. part ("chain", TREE_CHAIN (node));
  428. fputc ('\n', outfile);
  429. walk (TREE_TYPE (node), node, indent);
  430. break;
  431. case 'x':
  432. if (code == IDENTIFIER_NODE)
  433. fprintf (outfile, " = %s;\n", IDENTIFIER_POINTER (node));
  434. else if (code == TREE_LIST)
  435. {
  436. prtypeinfo (node);
  437. part ("purpose", TREE_PURPOSE (node));
  438. part ("value", TREE_VALUE (node));
  439. part ("chain", TREE_CHAIN (node));
  440. fputc ('\n', outfile);
  441. walk (TREE_TYPE (node), node, indent);
  442. walk (TREE_PURPOSE (node), node, indent);
  443. walk (TREE_VALUE (node), node, indent);
  444. }
  445. else if (code == ERROR_MARK)
  446. fputc ('\n', outfile);
  447. else abort ();
  448. break;
  449. default:
  450. abort ();
  451. } /* switch */
  452. if (TREE_CHAIN (node) != NULL && ! nochain)
  453. dump(TREE_CHAIN (node), indent);
  454. }