print-rtl.c (print_rtx, case NOTE): Don't blow up if NOTE_BASIC_BLOCK not yet set.
[gcc.git] / gcc / print-rtl.c
1 /* Print RTL for GNU C Compiler.
2 Copyright (C) 1987, 1988, 1992, 1997, 1998, 1999, 2000
3 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "real.h"
27 #include "flags.h"
28 #include "basic-block.h"
29
30
31 /* How to print out a register name.
32 We don't use PRINT_REG because some definitions of PRINT_REG
33 don't work here. */
34 #ifndef DEBUG_PRINT_REG
35 #define DEBUG_PRINT_REG(RTX, CODE, FILE) \
36 fprintf ((FILE), "%d %s", REGNO (RTX), reg_names[REGNO (RTX)])
37 #endif
38
39 /* Array containing all of the register names */
40
41 #ifdef DEBUG_REGISTER_NAMES
42 static const char * const reg_names[] = DEBUG_REGISTER_NAMES;
43 #else
44 static const char * const reg_names[] = REGISTER_NAMES;
45 #endif
46
47 static FILE *outfile;
48
49 static const char xspaces[] = " ";
50
51 static int sawclose = 0;
52
53 static int indent;
54
55 static void print_rtx PARAMS ((rtx));
56
57 /* Nonzero means suppress output of instruction numbers and line number
58 notes in debugging dumps.
59 This must be defined here so that programs like gencodes can be linked. */
60 int flag_dump_unnumbered = 0;
61
62 /* Nonzero if we are dumping graphical description. */
63 int dump_for_graph;
64
65 /* Print IN_RTX onto OUTFILE. This is the recursive part of printing. */
66
67 static void
68 print_rtx (in_rtx)
69 register rtx in_rtx;
70 {
71 register int i = 0;
72 register int j;
73 register const char *format_ptr;
74 register int is_insn;
75 rtx tem;
76
77 if (sawclose)
78 {
79 fprintf (outfile, "\n%s",
80 (xspaces + (sizeof xspaces - 1 - indent * 2)));
81 sawclose = 0;
82 }
83
84 if (in_rtx == 0)
85 {
86 fputs ("(nil)", outfile);
87 sawclose = 1;
88 return;
89 }
90
91 is_insn = (GET_RTX_CLASS (GET_CODE (in_rtx)) == 'i');
92
93 /* When printing in VCG format we write INSNs, NOTE, LABEL, and BARRIER
94 in separate nodes and therefore have to handle them special here. */
95 if (dump_for_graph &&
96 (is_insn || GET_CODE (in_rtx) == NOTE || GET_CODE (in_rtx) == CODE_LABEL
97 || GET_CODE (in_rtx) == BARRIER))
98 {
99 i = 3;
100 indent = 0;
101 }
102 else
103 {
104 /* print name of expression code */
105 fprintf (outfile, "(%s", GET_RTX_NAME (GET_CODE (in_rtx)));
106
107 if (in_rtx->in_struct)
108 fputs ("/s", outfile);
109
110 if (in_rtx->volatil)
111 fputs ("/v", outfile);
112
113 if (in_rtx->unchanging)
114 fputs ("/u", outfile);
115
116 if (in_rtx->integrated)
117 fputs ("/i", outfile);
118
119 if (in_rtx->frame_related)
120 fputs ("/f", outfile);
121
122 if (in_rtx->jump)
123 fputs ("/j", outfile);
124
125 if (in_rtx->call)
126 fputs ("/c", outfile);
127
128 if (GET_MODE (in_rtx) != VOIDmode)
129 {
130 /* Print REG_NOTE names for EXPR_LIST and INSN_LIST. */
131 if (GET_CODE (in_rtx) == EXPR_LIST || GET_CODE (in_rtx) == INSN_LIST)
132 fprintf (outfile, ":%s", GET_REG_NOTE_NAME (GET_MODE (in_rtx)));
133 else
134 fprintf (outfile, ":%s", GET_MODE_NAME (GET_MODE (in_rtx)));
135 }
136 }
137
138 /* Get the format string and skip the first elements if we have handled
139 them already. */
140 format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx)) + i;
141
142 for (; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
143 switch (*format_ptr++)
144 {
145 case 'S':
146 case 's':
147 if (XSTR (in_rtx, i) == 0)
148 fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
149 else
150 {
151 if (dump_for_graph)
152 fprintf (outfile, " (\\\"%s\\\")", XSTR (in_rtx, i));
153 else
154 fprintf (outfile, " (\"%s\")", XSTR (in_rtx, i));
155 }
156 sawclose = 1;
157 break;
158
159 /* 0 indicates a field for internal use that should not be printed.
160 An exception is the third field of a NOTE, where it indicates
161 that the field has several different valid contents. */
162 case '0':
163 if (i == 3 && GET_CODE (in_rtx) == NOTE)
164 {
165 if (NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_EH_REGION_BEG
166 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_EH_REGION_END)
167 {
168 if (flag_dump_unnumbered)
169 fprintf (outfile, " #");
170 else
171 fprintf (outfile, " %d", NOTE_EH_HANDLER (in_rtx));
172 sawclose = 1;
173 }
174 else if (NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_BLOCK_BEG
175 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_BLOCK_END)
176 {
177 fprintf (outfile, " ");
178 if (flag_dump_unnumbered)
179 fprintf (outfile, "#");
180 else
181 fprintf (outfile, HOST_PTR_PRINTF,
182 (char *) NOTE_BLOCK (in_rtx));
183 sawclose = 1;
184 }
185 else if (NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_RANGE_START
186 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_RANGE_END
187 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_LIVE)
188 {
189 indent += 2;
190 if (!sawclose)
191 fprintf (outfile, " ");
192 print_rtx (NOTE_RANGE_INFO (in_rtx));
193 indent -= 2;
194 }
195 else if (NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_BASIC_BLOCK)
196 {
197 basic_block bb = NOTE_BASIC_BLOCK (in_rtx);
198
199 if (bb != 0)
200 fprintf (outfile, " [bb %d]", bb->index);
201 }
202 else
203 {
204 const char * const str = X0STR (in_rtx, i);
205 if (str == 0)
206 fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
207 else
208 {
209 if (dump_for_graph)
210 fprintf (outfile, " (\\\"%s\\\")", str);
211 else
212 fprintf (outfile, " (\"%s\")", str);
213 }
214 }
215 }
216 break;
217
218 case 'e':
219 do_e:
220 indent += 2;
221 if (!sawclose)
222 fprintf (outfile, " ");
223 print_rtx (XEXP (in_rtx, i));
224 indent -= 2;
225 break;
226
227 case 'E':
228 case 'V':
229 indent += 2;
230 if (sawclose)
231 {
232 fprintf (outfile, "\n%s",
233 (xspaces + (sizeof xspaces - 1 - indent * 2)));
234 sawclose = 0;
235 }
236 fputs ("[ ", outfile);
237 if (NULL != XVEC (in_rtx, i))
238 {
239 indent += 2;
240 if (XVECLEN (in_rtx, i))
241 sawclose = 1;
242
243 for (j = 0; j < XVECLEN (in_rtx, i); j++)
244 print_rtx (XVECEXP (in_rtx, i, j));
245
246 indent -= 2;
247 }
248 if (sawclose)
249 fprintf (outfile, "\n%s",
250 (xspaces + (sizeof xspaces - 1 - indent * 2)));
251
252 fputs ("] ", outfile);
253 sawclose = 1;
254 indent -= 2;
255 break;
256
257 case 'w':
258 fprintf (outfile, " ");
259 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, XWINT (in_rtx, i));
260 fprintf (outfile, " [");
261 fprintf (outfile, HOST_WIDE_INT_PRINT_HEX, XWINT (in_rtx, i));
262 fprintf (outfile, "]");
263 break;
264
265 case 'i':
266 {
267 register int value = XINT (in_rtx, i);
268 const char *name;
269
270 if (GET_CODE (in_rtx) == REG && value < FIRST_PSEUDO_REGISTER)
271 {
272 fputc (' ', outfile);
273 DEBUG_PRINT_REG (in_rtx, 0, outfile);
274 }
275 else if (GET_CODE (in_rtx) == REG && value <= LAST_VIRTUAL_REGISTER)
276 {
277 if (value == VIRTUAL_INCOMING_ARGS_REGNUM)
278 fprintf (outfile, " %d virtual-incoming-args", value);
279 else if (value == VIRTUAL_STACK_VARS_REGNUM)
280 fprintf (outfile, " %d virtual-stack-vars", value);
281 else if (value == VIRTUAL_STACK_DYNAMIC_REGNUM)
282 fprintf (outfile, " %d virtual-stack-dynamic", value);
283 else if (value == VIRTUAL_OUTGOING_ARGS_REGNUM)
284 fprintf (outfile, " %d virtual-outgoing-args", value);
285 else if (value == VIRTUAL_CFA_REGNUM)
286 fprintf (outfile, " %d virtual-cfa", value);
287 else
288 fprintf (outfile, " %d virtual-reg-%d", value,
289 value-FIRST_VIRTUAL_REGISTER);
290 }
291 else if (flag_dump_unnumbered
292 && (is_insn || GET_CODE (in_rtx) == NOTE))
293 fputc ('#', outfile);
294 else
295 fprintf (outfile, " %d", value);
296
297 if (is_insn && &INSN_CODE (in_rtx) == &XINT (in_rtx, i)
298 && XINT (in_rtx, i) >= 0
299 && (name = get_insn_name (XINT (in_rtx, i))) != NULL)
300 fprintf (outfile, " {%s}", name);
301 sawclose = 0;
302 }
303 break;
304
305 /* Print NOTE_INSN names rather than integer codes. */
306
307 case 'n':
308 if (XINT (in_rtx, i) <= 0)
309 fprintf (outfile, " %s", GET_NOTE_INSN_NAME (XINT (in_rtx, i)));
310 else
311 fprintf (outfile, " %d", XINT (in_rtx, i));
312 sawclose = 0;
313 break;
314
315 case 'u':
316 if (XEXP (in_rtx, i) != NULL)
317 {
318 rtx sub = XEXP (in_rtx, i);
319 enum rtx_code subc = GET_CODE (sub);
320
321 if (GET_CODE (in_rtx) == LABEL_REF && subc != CODE_LABEL)
322 goto do_e;
323
324 if (flag_dump_unnumbered)
325 fputc ('#', outfile);
326 else
327 fprintf (outfile, " %d", INSN_UID (sub));
328 }
329 else
330 fputs (" 0", outfile);
331 sawclose = 0;
332 break;
333
334 case 'b':
335 if (XBITMAP (in_rtx, i) == NULL)
336 fputs (" {null}", outfile);
337 else
338 bitmap_print (outfile, XBITMAP (in_rtx, i), " {", "}");
339 sawclose = 0;
340 break;
341
342 case 't':
343 putc (' ', outfile);
344 fprintf (outfile, HOST_PTR_PRINTF, (char *) XTREE (in_rtx, i));
345 break;
346
347 case '*':
348 fputs (" Unknown", outfile);
349 sawclose = 0;
350 break;
351
352 default:
353 fprintf (stderr,
354 "switch format wrong in rtl.print_rtx(). format was: %c.\n",
355 format_ptr[-1]);
356 abort ();
357 }
358
359 if (GET_CODE (in_rtx) == MEM)
360 fprintf (outfile, " %d", MEM_ALIAS_SET (in_rtx));
361
362 #if HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT && MAX_LONG_DOUBLE_TYPE_SIZE == 64
363 if (GET_CODE (in_rtx) == CONST_DOUBLE && FLOAT_MODE_P (GET_MODE (in_rtx)))
364 {
365 double val;
366 REAL_VALUE_FROM_CONST_DOUBLE (val, in_rtx);
367 fprintf (outfile, " [%.16g]", val);
368 }
369 #endif
370
371 if (GET_CODE (in_rtx) == CODE_LABEL)
372 {
373 fprintf (outfile, " [%d uses]", LABEL_NUSES (in_rtx));
374 if (LABEL_ALTERNATE_NAME (in_rtx))
375 fprintf (outfile, " [alternate name: %s]",
376 LABEL_ALTERNATE_NAME (in_rtx));
377 }
378
379 if (GET_CODE (in_rtx) == CALL_PLACEHOLDER)
380 for (tem = XEXP (in_rtx, 0); tem != 0; tem = NEXT_INSN (tem))
381 if (GET_CODE (tem) == CALL_INSN)
382 {
383 fprintf (outfile, " ");
384 print_rtx (tem);
385 break;
386 }
387
388 if (dump_for_graph
389 && (is_insn || GET_CODE (in_rtx) == NOTE
390 || GET_CODE (in_rtx) == CODE_LABEL || GET_CODE (in_rtx) == BARRIER))
391 sawclose = 0;
392 else
393 {
394 fputc (')', outfile);
395 sawclose = 1;
396 }
397 }
398
399 /* Print an rtx on the current line of FILE. Initially indent IND
400 characters. */
401
402 void
403 print_inline_rtx (outf, x, ind)
404 FILE *outf;
405 rtx x;
406 int ind;
407 {
408 int oldsaw = sawclose;
409 int oldindent = indent;
410
411 sawclose = 0;
412 indent = ind;
413 outfile = outf;
414 print_rtx (x);
415 sawclose = oldsaw;
416 indent = oldindent;
417 }
418
419 /* Call this function from the debugger to see what X looks like. */
420
421 void
422 debug_rtx (x)
423 rtx x;
424 {
425 outfile = stderr;
426 print_rtx (x);
427 fprintf (stderr, "\n");
428 }
429
430 /* Count of rtx's to print with debug_rtx_list.
431 This global exists because gdb user defined commands have no arguments. */
432
433 int debug_rtx_count = 0; /* 0 is treated as equivalent to 1 */
434
435 /* Call this function to print list from X on.
436
437 N is a count of the rtx's to print. Positive values print from the specified
438 rtx on. Negative values print a window around the rtx.
439 EG: -5 prints 2 rtx's on either side (in addition to the specified rtx). */
440
441 void
442 debug_rtx_list (x, n)
443 rtx x;
444 int n;
445 {
446 int i,count;
447 rtx insn;
448
449 count = n == 0 ? 1 : n < 0 ? -n : n;
450
451 /* If we are printing a window, back up to the start. */
452
453 if (n < 0)
454 for (i = count / 2; i > 0; i--)
455 {
456 if (PREV_INSN (x) == 0)
457 break;
458 x = PREV_INSN (x);
459 }
460
461 for (i = count, insn = x; i > 0 && insn != 0; i--, insn = NEXT_INSN (insn))
462 debug_rtx (insn);
463 }
464
465 /* Call this function to print an rtx list from START to END inclusive. */
466
467 void
468 debug_rtx_range (start, end)
469 rtx start, end;
470 {
471 while (1)
472 {
473 debug_rtx (start);
474 if (!start || start == end)
475 break;
476 start = NEXT_INSN (start);
477 }
478 }
479
480 /* Call this function to search an rtx list to find one with insn uid UID,
481 and then call debug_rtx_list to print it, using DEBUG_RTX_COUNT.
482 The found insn is returned to enable further debugging analysis. */
483
484 rtx
485 debug_rtx_find (x, uid)
486 rtx x;
487 int uid;
488 {
489 while (x != 0 && INSN_UID (x) != uid)
490 x = NEXT_INSN (x);
491 if (x != 0)
492 {
493 debug_rtx_list (x, debug_rtx_count);
494 return x;
495 }
496 else
497 {
498 fprintf (stderr, "insn uid %d not found\n", uid);
499 return 0;
500 }
501 }
502
503 /* External entry point for printing a chain of insns
504 starting with RTX_FIRST onto file OUTF.
505 A blank line separates insns.
506
507 If RTX_FIRST is not an insn, then it alone is printed, with no newline. */
508
509 void
510 print_rtl (outf, rtx_first)
511 FILE *outf;
512 rtx rtx_first;
513 {
514 register rtx tmp_rtx;
515
516 outfile = outf;
517 sawclose = 0;
518
519 if (rtx_first == 0)
520 fputs ("(nil)\n", outf);
521 else
522 switch (GET_CODE (rtx_first))
523 {
524 case INSN:
525 case JUMP_INSN:
526 case CALL_INSN:
527 case NOTE:
528 case CODE_LABEL:
529 case BARRIER:
530 for (tmp_rtx = rtx_first; tmp_rtx != 0; tmp_rtx = NEXT_INSN (tmp_rtx))
531 if (! flag_dump_unnumbered
532 || GET_CODE (tmp_rtx) != NOTE || NOTE_LINE_NUMBER (tmp_rtx) < 0)
533 {
534 print_rtx (tmp_rtx);
535 fprintf (outfile, "\n");
536 }
537 break;
538
539 default:
540 print_rtx (rtx_first);
541 }
542 }
543
544 /* Like print_rtx, except specify a file. */
545 /* Return nonzero if we actually printed anything. */
546
547 int
548 print_rtl_single (outf, x)
549 FILE *outf;
550 rtx x;
551 {
552 outfile = outf;
553 sawclose = 0;
554 if (! flag_dump_unnumbered
555 || GET_CODE (x) != NOTE || NOTE_LINE_NUMBER (x) < 0)
556 {
557 print_rtx (x);
558 putc ('\n', outf);
559 return 1;
560 }
561 return 0;
562 }