inclhack.def (hpux_imaginary_i): Remove spaces.
[gcc.git] / gcc / print-rtl.c
1 /* Print RTL for GCC.
2 Copyright (C) 1987, 1988, 1992, 1997, 1998, 1999, 2000, 2002, 2003,
3 2004, 2005, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* This file is compiled twice: once for the generator programs,
23 once for the compiler. */
24 #ifdef GENERATOR_FILE
25 #include "bconfig.h"
26 #else
27 #include "config.h"
28 #endif
29
30 #include "system.h"
31 #include "coretypes.h"
32 #include "tm.h"
33 #include "rtl.h"
34
35 /* These headers all define things which are not available in
36 generator programs. */
37 #ifndef GENERATOR_FILE
38 #include "tree.h"
39 #include "real.h"
40 #include "flags.h"
41 #include "hard-reg-set.h"
42 #include "basic-block.h"
43 #include "diagnostic.h"
44 #endif
45
46 static FILE *outfile;
47
48 static int sawclose = 0;
49
50 static int indent;
51
52 static void print_rtx (const_rtx);
53
54 /* String printed at beginning of each RTL when it is dumped.
55 This string is set to ASM_COMMENT_START when the RTL is dumped in
56 the assembly output file. */
57 const char *print_rtx_head = "";
58
59 /* Nonzero means suppress output of instruction numbers
60 in debugging dumps.
61 This must be defined here so that programs like gencodes can be linked. */
62 int flag_dump_unnumbered = 0;
63
64 /* Nonzero means suppress output of instruction numbers for previous
65 and next insns in debugging dumps.
66 This must be defined here so that programs like gencodes can be linked. */
67 int flag_dump_unnumbered_links = 0;
68
69 /* Nonzero means use simplified format without flags, modes, etc. */
70 int flag_simple = 0;
71
72 /* Nonzero if we are dumping graphical description. */
73 int dump_for_graph;
74
75 #ifndef GENERATOR_FILE
76 void
77 print_mem_expr (FILE *outfile, const_tree expr)
78 {
79 fputc (' ', outfile);
80 print_generic_expr (outfile, CONST_CAST_TREE (expr), 0);
81 }
82 #endif
83
84 /* Print IN_RTX onto OUTFILE. This is the recursive part of printing. */
85
86 static void
87 print_rtx (const_rtx in_rtx)
88 {
89 int i = 0;
90 int j;
91 const char *format_ptr;
92 int is_insn;
93
94 if (sawclose)
95 {
96 if (flag_simple)
97 fputc (' ', outfile);
98 else
99 fprintf (outfile, "\n%s%*s", print_rtx_head, indent * 2, "");
100 sawclose = 0;
101 }
102
103 if (in_rtx == 0)
104 {
105 fputs ("(nil)", outfile);
106 sawclose = 1;
107 return;
108 }
109 else if (GET_CODE (in_rtx) > NUM_RTX_CODE)
110 {
111 fprintf (outfile, "(??? bad code %d\n)", GET_CODE (in_rtx));
112 sawclose = 1;
113 return;
114 }
115
116 is_insn = INSN_P (in_rtx);
117
118 /* When printing in VCG format we write INSNs, NOTE, LABEL, and BARRIER
119 in separate nodes and therefore have to handle them special here. */
120 if (dump_for_graph
121 && (is_insn || NOTE_P (in_rtx)
122 || LABEL_P (in_rtx) || BARRIER_P (in_rtx)))
123 {
124 i = 3;
125 indent = 0;
126 }
127 else
128 {
129 /* Print name of expression code. */
130 if (flag_simple && CONST_INT_P (in_rtx))
131 fputc ('(', outfile);
132 else
133 fprintf (outfile, "(%s", GET_RTX_NAME (GET_CODE (in_rtx)));
134
135 if (! flag_simple)
136 {
137 if (RTX_FLAG (in_rtx, in_struct))
138 fputs ("/s", outfile);
139
140 if (RTX_FLAG (in_rtx, volatil))
141 fputs ("/v", outfile);
142
143 if (RTX_FLAG (in_rtx, unchanging))
144 fputs ("/u", outfile);
145
146 if (RTX_FLAG (in_rtx, frame_related))
147 fputs ("/f", outfile);
148
149 if (RTX_FLAG (in_rtx, jump))
150 fputs ("/j", outfile);
151
152 if (RTX_FLAG (in_rtx, call))
153 fputs ("/c", outfile);
154
155 if (RTX_FLAG (in_rtx, return_val))
156 fputs ("/i", outfile);
157
158 /* Print REG_NOTE names for EXPR_LIST and INSN_LIST. */
159 if ((GET_CODE (in_rtx) == EXPR_LIST
160 || GET_CODE (in_rtx) == INSN_LIST)
161 && (int)GET_MODE (in_rtx) < REG_NOTE_MAX)
162 fprintf (outfile, ":%s",
163 GET_REG_NOTE_NAME (GET_MODE (in_rtx)));
164
165 /* For other rtl, print the mode if it's not VOID. */
166 else if (GET_MODE (in_rtx) != VOIDmode)
167 fprintf (outfile, ":%s", GET_MODE_NAME (GET_MODE (in_rtx)));
168 }
169 }
170
171 #ifndef GENERATOR_FILE
172 if (GET_CODE (in_rtx) == CONST_DOUBLE && FLOAT_MODE_P (GET_MODE (in_rtx)))
173 i = 5;
174 #endif
175
176 /* Get the format string and skip the first elements if we have handled
177 them already. */
178 format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx)) + i;
179 for (; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
180 switch (*format_ptr++)
181 {
182 const char *str;
183
184 case 'T':
185 str = XTMPL (in_rtx, i);
186 goto string;
187
188 case 'S':
189 case 's':
190 str = XSTR (in_rtx, i);
191 string:
192
193 if (str == 0)
194 fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
195 else
196 {
197 if (dump_for_graph)
198 fprintf (outfile, " (\\\"%s\\\")", str);
199 else
200 fprintf (outfile, " (\"%s\")", str);
201 }
202 sawclose = 1;
203 break;
204
205 /* 0 indicates a field for internal use that should not be printed.
206 An exception is the third field of a NOTE, where it indicates
207 that the field has several different valid contents. */
208 case '0':
209 if (i == 1 && REG_P (in_rtx))
210 {
211 if (REGNO (in_rtx) != ORIGINAL_REGNO (in_rtx))
212 fprintf (outfile, " [%d]", ORIGINAL_REGNO (in_rtx));
213 }
214 #ifndef GENERATOR_FILE
215 else if (i == 1 && GET_CODE (in_rtx) == SYMBOL_REF)
216 {
217 int flags = SYMBOL_REF_FLAGS (in_rtx);
218 if (flags)
219 fprintf (outfile, " [flags 0x%x]", flags);
220 }
221 else if (i == 2 && GET_CODE (in_rtx) == SYMBOL_REF)
222 {
223 tree decl = SYMBOL_REF_DECL (in_rtx);
224 if (decl)
225 print_node_brief (outfile, "", decl, 0);
226 }
227 #endif
228 else if (i == 4 && NOTE_P (in_rtx))
229 {
230 switch (NOTE_KIND (in_rtx))
231 {
232 case NOTE_INSN_EH_REGION_BEG:
233 case NOTE_INSN_EH_REGION_END:
234 if (flag_dump_unnumbered)
235 fprintf (outfile, " #");
236 else
237 fprintf (outfile, " %d", NOTE_EH_HANDLER (in_rtx));
238 sawclose = 1;
239 break;
240
241 case NOTE_INSN_BLOCK_BEG:
242 case NOTE_INSN_BLOCK_END:
243 #ifndef GENERATOR_FILE
244 dump_addr (outfile, " ", NOTE_BLOCK (in_rtx));
245 #endif
246 sawclose = 1;
247 break;
248
249 case NOTE_INSN_BASIC_BLOCK:
250 {
251 #ifndef GENERATOR_FILE
252 basic_block bb = NOTE_BASIC_BLOCK (in_rtx);
253 if (bb != 0)
254 fprintf (outfile, " [bb %d]", bb->index);
255 #endif
256 break;
257 }
258
259 case NOTE_INSN_DELETED_LABEL:
260 {
261 const char *label = NOTE_DELETED_LABEL_NAME (in_rtx);
262 if (label)
263 fprintf (outfile, " (\"%s\")", label);
264 else
265 fprintf (outfile, " \"\"");
266 }
267 break;
268
269 case NOTE_INSN_SWITCH_TEXT_SECTIONS:
270 {
271 #ifndef GENERATOR_FILE
272 basic_block bb = NOTE_BASIC_BLOCK (in_rtx);
273 if (bb != 0)
274 fprintf (outfile, " [bb %d]", bb->index);
275 #endif
276 break;
277 }
278
279 case NOTE_INSN_VAR_LOCATION:
280 #ifndef GENERATOR_FILE
281 fprintf (outfile, " (");
282 print_mem_expr (outfile, NOTE_VAR_LOCATION_DECL (in_rtx));
283 fprintf (outfile, " ");
284 print_rtx (NOTE_VAR_LOCATION_LOC (in_rtx));
285 if (NOTE_VAR_LOCATION_STATUS (in_rtx) ==
286 VAR_INIT_STATUS_UNINITIALIZED)
287 fprintf (outfile, " [uninit]");
288 fprintf (outfile, ")");
289 #endif
290 break;
291
292 default:
293 break;
294 }
295 }
296 else if (i == 9 && JUMP_P (in_rtx) && XEXP (in_rtx, i) != NULL)
297 /* Output the JUMP_LABEL reference. */
298 fprintf (outfile, "\n -> %d", INSN_UID (XEXP (in_rtx, i)));
299 break;
300
301 case 'e':
302 do_e:
303 indent += 2;
304 if (!sawclose)
305 fprintf (outfile, " ");
306 print_rtx (XEXP (in_rtx, i));
307 indent -= 2;
308 break;
309
310 case 'E':
311 case 'V':
312 indent += 2;
313 if (sawclose)
314 {
315 fprintf (outfile, "\n%s%*s",
316 print_rtx_head, indent * 2, "");
317 sawclose = 0;
318 }
319 fputs (" [", outfile);
320 if (NULL != XVEC (in_rtx, i))
321 {
322 indent += 2;
323 if (XVECLEN (in_rtx, i))
324 sawclose = 1;
325
326 for (j = 0; j < XVECLEN (in_rtx, i); j++)
327 print_rtx (XVECEXP (in_rtx, i, j));
328
329 indent -= 2;
330 }
331 if (sawclose)
332 fprintf (outfile, "\n%s%*s", print_rtx_head, indent * 2, "");
333
334 fputs ("]", outfile);
335 sawclose = 1;
336 indent -= 2;
337 break;
338
339 case 'w':
340 if (! flag_simple)
341 fprintf (outfile, " ");
342 fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, XWINT (in_rtx, i));
343 if (! flag_simple)
344 fprintf (outfile, " [" HOST_WIDE_INT_PRINT_HEX "]",
345 (unsigned HOST_WIDE_INT) XWINT (in_rtx, i));
346 break;
347
348 case 'i':
349 if (i == 4 && INSN_P (in_rtx))
350 {
351 #ifndef GENERATOR_FILE
352 /* Pretty-print insn locators. Ignore scoping as it is mostly
353 redundant with line number information and do not print anything
354 when there is no location information available. */
355 if (INSN_LOCATOR (in_rtx) && insn_file (in_rtx))
356 fprintf(outfile, " %s:%i", insn_file (in_rtx), insn_line (in_rtx));
357 #endif
358 }
359 else if (i == 6 && NOTE_P (in_rtx))
360 {
361 /* This field is only used for NOTE_INSN_DELETED_LABEL, and
362 other times often contains garbage from INSN->NOTE death. */
363 if (NOTE_KIND (in_rtx) == NOTE_INSN_DELETED_LABEL)
364 fprintf (outfile, " %d", XINT (in_rtx, i));
365 }
366 else
367 {
368 int value = XINT (in_rtx, i);
369 const char *name;
370
371 #ifndef GENERATOR_FILE
372 if (REG_P (in_rtx) && value < FIRST_PSEUDO_REGISTER)
373 fprintf (outfile, " %d %s", REGNO (in_rtx),
374 reg_names[REGNO (in_rtx)]);
375 else if (REG_P (in_rtx)
376 && value <= LAST_VIRTUAL_REGISTER)
377 {
378 if (value == VIRTUAL_INCOMING_ARGS_REGNUM)
379 fprintf (outfile, " %d virtual-incoming-args", value);
380 else if (value == VIRTUAL_STACK_VARS_REGNUM)
381 fprintf (outfile, " %d virtual-stack-vars", value);
382 else if (value == VIRTUAL_STACK_DYNAMIC_REGNUM)
383 fprintf (outfile, " %d virtual-stack-dynamic", value);
384 else if (value == VIRTUAL_OUTGOING_ARGS_REGNUM)
385 fprintf (outfile, " %d virtual-outgoing-args", value);
386 else if (value == VIRTUAL_CFA_REGNUM)
387 fprintf (outfile, " %d virtual-cfa", value);
388 else
389 fprintf (outfile, " %d virtual-reg-%d", value,
390 value-FIRST_VIRTUAL_REGISTER);
391 }
392 else
393 #endif
394 if (flag_dump_unnumbered
395 && (is_insn || NOTE_P (in_rtx)))
396 fputc ('#', outfile);
397 else
398 fprintf (outfile, " %d", value);
399
400 #ifndef GENERATOR_FILE
401 if (REG_P (in_rtx) && REG_ATTRS (in_rtx))
402 {
403 fputs (" [", outfile);
404 if (ORIGINAL_REGNO (in_rtx) != REGNO (in_rtx))
405 fprintf (outfile, "orig:%i", ORIGINAL_REGNO (in_rtx));
406 if (REG_EXPR (in_rtx))
407 print_mem_expr (outfile, REG_EXPR (in_rtx));
408
409 if (REG_OFFSET (in_rtx))
410 fprintf (outfile, "+" HOST_WIDE_INT_PRINT_DEC,
411 REG_OFFSET (in_rtx));
412 fputs (" ]", outfile);
413 }
414 #endif
415
416 if (is_insn && &INSN_CODE (in_rtx) == &XINT (in_rtx, i)
417 && XINT (in_rtx, i) >= 0
418 && (name = get_insn_name (XINT (in_rtx, i))) != NULL)
419 fprintf (outfile, " {%s}", name);
420 sawclose = 0;
421 }
422 break;
423
424 /* Print NOTE_INSN names rather than integer codes. */
425
426 case 'n':
427 fprintf (outfile, " %s", GET_NOTE_INSN_NAME (XINT (in_rtx, i)));
428 sawclose = 0;
429 break;
430
431 case 'u':
432 if (XEXP (in_rtx, i) != NULL)
433 {
434 rtx sub = XEXP (in_rtx, i);
435 enum rtx_code subc = GET_CODE (sub);
436
437 if (GET_CODE (in_rtx) == LABEL_REF)
438 {
439 if (subc == NOTE
440 && NOTE_KIND (sub) == NOTE_INSN_DELETED_LABEL)
441 {
442 if (flag_dump_unnumbered)
443 fprintf (outfile, " [# deleted]");
444 else
445 fprintf (outfile, " [%d deleted]", INSN_UID (sub));
446 sawclose = 0;
447 break;
448 }
449
450 if (subc != CODE_LABEL)
451 goto do_e;
452 }
453
454 if (flag_dump_unnumbered
455 || (flag_dump_unnumbered_links && (i == 1 || i == 2)
456 && (INSN_P (in_rtx) || NOTE_P (in_rtx)
457 || LABEL_P (in_rtx) || BARRIER_P (in_rtx))))
458 fputs (" #", outfile);
459 else
460 fprintf (outfile, " %d", INSN_UID (sub));
461 }
462 else
463 fputs (" 0", outfile);
464 sawclose = 0;
465 break;
466
467 case 'b':
468 #ifndef GENERATOR_FILE
469 if (XBITMAP (in_rtx, i) == NULL)
470 fputs (" {null}", outfile);
471 else
472 bitmap_print (outfile, XBITMAP (in_rtx, i), " {", "}");
473 #endif
474 sawclose = 0;
475 break;
476
477 case 't':
478 #ifndef GENERATOR_FILE
479 dump_addr (outfile, " ", XTREE (in_rtx, i));
480 #endif
481 break;
482
483 case '*':
484 fputs (" Unknown", outfile);
485 sawclose = 0;
486 break;
487
488 case 'B':
489 #ifndef GENERATOR_FILE
490 if (XBBDEF (in_rtx, i))
491 fprintf (outfile, " %i", XBBDEF (in_rtx, i)->index);
492 #endif
493 break;
494
495 default:
496 gcc_unreachable ();
497 }
498
499 switch (GET_CODE (in_rtx))
500 {
501 #ifndef GENERATOR_FILE
502 case MEM:
503 fprintf (outfile, " [" HOST_WIDE_INT_PRINT_DEC,
504 (HOST_WIDE_INT) MEM_ALIAS_SET (in_rtx));
505
506 if (MEM_EXPR (in_rtx))
507 print_mem_expr (outfile, MEM_EXPR (in_rtx));
508
509 if (MEM_OFFSET (in_rtx))
510 fprintf (outfile, "+" HOST_WIDE_INT_PRINT_DEC,
511 INTVAL (MEM_OFFSET (in_rtx)));
512
513 if (MEM_SIZE (in_rtx))
514 fprintf (outfile, " S" HOST_WIDE_INT_PRINT_DEC,
515 INTVAL (MEM_SIZE (in_rtx)));
516
517 if (MEM_ALIGN (in_rtx) != 1)
518 fprintf (outfile, " A%u", MEM_ALIGN (in_rtx));
519
520 fputc (']', outfile);
521 break;
522
523 case CONST_DOUBLE:
524 if (FLOAT_MODE_P (GET_MODE (in_rtx)))
525 {
526 char s[60];
527
528 real_to_decimal (s, CONST_DOUBLE_REAL_VALUE (in_rtx),
529 sizeof (s), 0, 1);
530 fprintf (outfile, " %s", s);
531
532 real_to_hexadecimal (s, CONST_DOUBLE_REAL_VALUE (in_rtx),
533 sizeof (s), 0, 1);
534 fprintf (outfile, " [%s]", s);
535 }
536 break;
537 #endif
538
539 case CODE_LABEL:
540 fprintf (outfile, " [%d uses]", LABEL_NUSES (in_rtx));
541 switch (LABEL_KIND (in_rtx))
542 {
543 case LABEL_NORMAL: break;
544 case LABEL_STATIC_ENTRY: fputs (" [entry]", outfile); break;
545 case LABEL_GLOBAL_ENTRY: fputs (" [global entry]", outfile); break;
546 case LABEL_WEAK_ENTRY: fputs (" [weak entry]", outfile); break;
547 default: gcc_unreachable ();
548 }
549 break;
550
551 default:
552 break;
553 }
554
555 if (dump_for_graph
556 && (is_insn || NOTE_P (in_rtx)
557 || LABEL_P (in_rtx) || BARRIER_P (in_rtx)))
558 sawclose = 0;
559 else
560 {
561 fputc (')', outfile);
562 sawclose = 1;
563 }
564 }
565
566 /* Print an rtx on the current line of FILE. Initially indent IND
567 characters. */
568
569 void
570 print_inline_rtx (FILE *outf, const_rtx x, int ind)
571 {
572 int oldsaw = sawclose;
573 int oldindent = indent;
574
575 sawclose = 0;
576 indent = ind;
577 outfile = outf;
578 print_rtx (x);
579 sawclose = oldsaw;
580 indent = oldindent;
581 }
582
583 /* Call this function from the debugger to see what X looks like. */
584
585 void
586 debug_rtx (const_rtx x)
587 {
588 outfile = stderr;
589 sawclose = 0;
590 print_rtx (x);
591 fprintf (stderr, "\n");
592 }
593
594 /* Count of rtx's to print with debug_rtx_list.
595 This global exists because gdb user defined commands have no arguments. */
596
597 int debug_rtx_count = 0; /* 0 is treated as equivalent to 1 */
598
599 /* Call this function to print list from X on.
600
601 N is a count of the rtx's to print. Positive values print from the specified
602 rtx on. Negative values print a window around the rtx.
603 EG: -5 prints 2 rtx's on either side (in addition to the specified rtx). */
604
605 void
606 debug_rtx_list (const_rtx x, int n)
607 {
608 int i,count;
609 const_rtx insn;
610
611 count = n == 0 ? 1 : n < 0 ? -n : n;
612
613 /* If we are printing a window, back up to the start. */
614
615 if (n < 0)
616 for (i = count / 2; i > 0; i--)
617 {
618 if (PREV_INSN (x) == 0)
619 break;
620 x = PREV_INSN (x);
621 }
622
623 for (i = count, insn = x; i > 0 && insn != 0; i--, insn = NEXT_INSN (insn))
624 {
625 debug_rtx (insn);
626 fprintf (stderr, "\n");
627 }
628 }
629
630 /* Call this function to print an rtx list from START to END inclusive. */
631
632 void
633 debug_rtx_range (const_rtx start, const_rtx end)
634 {
635 while (1)
636 {
637 debug_rtx (start);
638 fprintf (stderr, "\n");
639 if (!start || start == end)
640 break;
641 start = NEXT_INSN (start);
642 }
643 }
644
645 /* Call this function to search an rtx list to find one with insn uid UID,
646 and then call debug_rtx_list to print it, using DEBUG_RTX_COUNT.
647 The found insn is returned to enable further debugging analysis. */
648
649 const_rtx
650 debug_rtx_find (const_rtx x, int uid)
651 {
652 while (x != 0 && INSN_UID (x) != uid)
653 x = NEXT_INSN (x);
654 if (x != 0)
655 {
656 debug_rtx_list (x, debug_rtx_count);
657 return x;
658 }
659 else
660 {
661 fprintf (stderr, "insn uid %d not found\n", uid);
662 return 0;
663 }
664 }
665
666 /* External entry point for printing a chain of insns
667 starting with RTX_FIRST onto file OUTF.
668 A blank line separates insns.
669
670 If RTX_FIRST is not an insn, then it alone is printed, with no newline. */
671
672 void
673 print_rtl (FILE *outf, const_rtx rtx_first)
674 {
675 const_rtx tmp_rtx;
676
677 outfile = outf;
678 sawclose = 0;
679
680 if (rtx_first == 0)
681 {
682 fputs (print_rtx_head, outf);
683 fputs ("(nil)\n", outf);
684 }
685 else
686 switch (GET_CODE (rtx_first))
687 {
688 case INSN:
689 case JUMP_INSN:
690 case CALL_INSN:
691 case NOTE:
692 case CODE_LABEL:
693 case BARRIER:
694 for (tmp_rtx = rtx_first; tmp_rtx != 0; tmp_rtx = NEXT_INSN (tmp_rtx))
695 {
696 fputs (print_rtx_head, outfile);
697 print_rtx (tmp_rtx);
698 fprintf (outfile, "\n");
699 }
700 break;
701
702 default:
703 fputs (print_rtx_head, outfile);
704 print_rtx (rtx_first);
705 }
706 }
707
708 /* Like print_rtx, except specify a file. */
709 /* Return nonzero if we actually printed anything. */
710
711 int
712 print_rtl_single (FILE *outf, const_rtx x)
713 {
714 outfile = outf;
715 sawclose = 0;
716 fputs (print_rtx_head, outfile);
717 print_rtx (x);
718 putc ('\n', outf);
719 return 1;
720 }
721
722
723 /* Like print_rtl except without all the detail; for example,
724 if RTX is a CONST_INT then print in decimal format. */
725
726 void
727 print_simple_rtl (FILE *outf, const_rtx x)
728 {
729 flag_simple = 1;
730 print_rtl (outf, x);
731 flag_simple = 0;
732 }