OSDN Git Service

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