OSDN Git Service

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