OSDN Git Service

Ulrich Drepper <drepper@cygnus.com>
[pf3gnuchains/gcc-fork.git] / gcc / print-rtl.c
1 /* Print RTL for GNU C Compiler.
2    Copyright (C) 1987, 1988, 1992, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "bitmap.h"
26 #include "real.h"
27 #include "flags.h"
28
29
30 /* How to print out a register name.
31    We don't use PRINT_REG because some definitions of PRINT_REG
32    don't work here.  */
33 #ifndef DEBUG_PRINT_REG
34 #define DEBUG_PRINT_REG(RTX, CODE, FILE) \
35   fprintf ((FILE), "%d %s", REGNO (RTX), reg_names[REGNO (RTX)])
36 #endif
37
38 /* Array containing all of the register names */
39
40 #ifdef DEBUG_REGISTER_NAMES
41 static char *reg_names[] = DEBUG_REGISTER_NAMES;
42 #else
43 static char *reg_names[] = REGISTER_NAMES;
44 #endif
45
46 static FILE *outfile;
47
48 static char spaces[] = "                                                                                                                                                                ";
49
50 static int sawclose = 0;
51
52 static int indent;
53
54 /* Names for patterns.  Non-zero only when linked with insn-output.c.  */
55
56 extern char **insn_name_ptr;
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 char *format_ptr;
75   register int is_insn;
76
77   if (sawclose)
78     {
79       fprintf (outfile, "\n%s",
80                (spaces + (sizeof spaces - 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 (GET_MODE (in_rtx) != VOIDmode)
120         {
121           /* Print REG_NOTE names for EXPR_LIST and INSN_LIST.  */
122           if (GET_CODE (in_rtx) == EXPR_LIST || GET_CODE (in_rtx) == INSN_LIST)
123             fprintf (outfile, ":%s", GET_REG_NOTE_NAME (GET_MODE (in_rtx)));
124           else
125             fprintf (outfile, ":%s", GET_MODE_NAME (GET_MODE (in_rtx)));
126         }
127     }
128
129   /* Get the format string and skip the first elements if we have handled
130      them already.  */
131   format_ptr = GET_RTX_FORMAT (GET_CODE (in_rtx)) + i;
132
133   for (; i < GET_RTX_LENGTH (GET_CODE (in_rtx)); i++)
134     switch (*format_ptr++)
135       {
136       case 'S':
137       case 's':
138         if (i == 3 && GET_CODE (in_rtx) == NOTE
139             && (NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_EH_REGION_BEG
140                 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_EH_REGION_END
141                 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_BLOCK_BEG
142                 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_BLOCK_END))
143           {
144             fprintf (outfile, " %d", NOTE_BLOCK_NUMBER (in_rtx));
145             sawclose = 1;
146             break;
147           }
148
149         if (i == 3 && GET_CODE (in_rtx) == NOTE
150             && (NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_RANGE_START
151                 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_RANGE_END
152                 || NOTE_LINE_NUMBER (in_rtx) == NOTE_INSN_LIVE))
153           {
154             indent += 2;
155             if (!sawclose)
156               fprintf (outfile, " ");
157             print_rtx (NOTE_RANGE_INFO (in_rtx));
158             indent -= 2;
159             break;
160           }
161
162         if (XSTR (in_rtx, i) == 0)
163           fputs (dump_for_graph ? " \\\"\\\"" : " \"\"", outfile);
164         else
165           fprintf (outfile, dump_for_graph ? " (\\\"%s\\\")" : " (\"%s\")",
166                    XSTR (in_rtx, i));
167         sawclose = 1;
168         break;
169
170         /* 0 indicates a field for internal use that should not be printed.  */
171       case '0':
172         break;
173
174       case 'e':
175         indent += 2;
176         if (!sawclose)
177           fprintf (outfile, " ");
178         print_rtx (XEXP (in_rtx, i));
179         indent -= 2;
180         break;
181
182       case 'E':
183       case 'V':
184         indent += 2;
185         if (sawclose)
186           {
187             fprintf (outfile, "\n%s",
188                      (spaces + (sizeof spaces - 1 - indent * 2)));
189             sawclose = 0;
190           }
191         fputs ("[ ", outfile);
192         if (NULL != XVEC (in_rtx, i))
193           {
194             indent += 2;
195             if (XVECLEN (in_rtx, i))
196               sawclose = 1;
197
198             for (j = 0; j < XVECLEN (in_rtx, i); j++)
199               print_rtx (XVECEXP (in_rtx, i, j));
200
201             indent -= 2;
202           }
203         if (sawclose)
204           fprintf (outfile, "\n%s",
205                    (spaces + (sizeof spaces - 1 - indent * 2)));
206
207         fputs ("] ", outfile);
208         sawclose = 1;
209         indent -= 2;
210         break;
211
212       case 'w':
213         fprintf (outfile, " ");
214         fprintf (outfile, HOST_WIDE_INT_PRINT_DEC, XWINT (in_rtx, i));
215         break;
216
217       case 'i':
218         {
219           register int value = XINT (in_rtx, i);
220
221           if (GET_CODE (in_rtx) == REG && value < FIRST_PSEUDO_REGISTER)
222             {
223               fputc (' ', outfile);
224               DEBUG_PRINT_REG (in_rtx, 0, outfile);
225             }
226           else if (flag_dump_unnumbered
227                    && (is_insn || GET_CODE (in_rtx) == NOTE))
228             fputc ('#', outfile);
229           else
230             fprintf (outfile, " %d", value);
231         }
232         if (is_insn && &INSN_CODE (in_rtx) == &XINT (in_rtx, i)
233             && insn_name_ptr
234             && XINT (in_rtx, i) >= 0)
235           fprintf (outfile, " {%s}", insn_name_ptr[XINT (in_rtx, i)]);
236         sawclose = 0;
237         break;
238
239       /* Print NOTE_INSN names rather than integer codes.  */
240
241       case 'n':
242         if (XINT (in_rtx, i) <= 0)
243           fprintf (outfile, " %s", GET_NOTE_INSN_NAME (XINT (in_rtx, i)));
244         else
245           fprintf (outfile, " %d", XINT (in_rtx, i));
246         sawclose = 0;
247         break;
248
249       case 'u':
250         if (XEXP (in_rtx, i) != NULL)
251           {
252             if (flag_dump_unnumbered)
253               fputc ('#', outfile);
254             else
255               fprintf (outfile, " %d", INSN_UID (XEXP (in_rtx, i)));
256           }
257         else
258           fputs (" 0", outfile);
259         sawclose = 0;
260         break;
261
262       case 'b':
263         if (XBITMAP (in_rtx, i) == NULL)
264           fputs (" {null}", outfile);
265         else
266           bitmap_print (outfile, XBITMAP (in_rtx, i), " {", "}");
267         sawclose = 0;
268         break;
269
270       case 't':
271         putc (' ', outfile);
272         fprintf (outfile, HOST_PTR_PRINTF, (char *) XTREE (in_rtx, i));
273         break;
274
275       case '*':
276         fputs (" Unknown", outfile);
277         sawclose = 0;
278         break;
279
280       default:
281         fprintf (stderr,
282                  "switch format wrong in rtl.print_rtx(). format was: %c.\n",
283                  format_ptr[-1]);
284         abort ();
285       }
286
287   if (GET_CODE (in_rtx) == MEM)
288     fprintf (outfile, " %d", MEM_ALIAS_SET (in_rtx));
289
290 #if HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT && LONG_DOUBLE_TYPE_SIZE == 64
291   if (GET_CODE (in_rtx) == CONST_DOUBLE && FLOAT_MODE_P (GET_MODE (in_rtx)))
292     {
293       double val;
294       REAL_VALUE_FROM_CONST_DOUBLE (val, in_rtx);
295       fprintf (outfile, " [%.16g]", val);
296     }
297 #endif
298
299   if (dump_for_graph
300       && (is_insn || GET_CODE (in_rtx) == NOTE
301           || GET_CODE (in_rtx) == CODE_LABEL || GET_CODE (in_rtx) == BARRIER))
302     sawclose = 0;
303   else
304     {
305       fputc (')', outfile);
306       sawclose = 1;
307     }
308 }
309
310 /* Print an rtx on the current line of FILE.  Initially indent IND
311    characters.  */
312
313 void
314 print_inline_rtx (outf, x, ind)
315      FILE *outf;
316      rtx x;
317      int ind;
318 {
319   int oldsaw = sawclose;
320   int oldindent = indent;
321
322   sawclose = 0;
323   indent = ind;
324   outfile = outf;
325   print_rtx (x);
326   sawclose = oldsaw;
327   indent = oldindent;
328 }
329
330 /* Call this function from the debugger to see what X looks like.  */
331
332 void
333 debug_rtx (x)
334      rtx x;
335 {
336   outfile = stderr;
337   print_rtx (x);
338   fprintf (stderr, "\n");
339 }
340
341 /* Count of rtx's to print with debug_rtx_list.
342    This global exists because gdb user defined commands have no arguments.  */
343
344 int debug_rtx_count = 0;        /* 0 is treated as equivalent to 1 */
345
346 /* Call this function to print list from X on.
347
348    N is a count of the rtx's to print. Positive values print from the specified
349    rtx on.  Negative values print a window around the rtx.
350    EG: -5 prints 2 rtx's on either side (in addition to the specified rtx).  */
351
352 void
353 debug_rtx_list (x, n)
354      rtx x;
355      int n;
356 {
357   int i,count;
358   rtx insn;
359
360   count = n == 0 ? 1 : n < 0 ? -n : n;
361
362   /* If we are printing a window, back up to the start.  */
363
364   if (n < 0)
365     for (i = count / 2; i > 0; i--)
366       {
367         if (PREV_INSN (x) == 0)
368           break;
369         x = PREV_INSN (x);
370       }
371
372   for (i = count, insn = x; i > 0 && insn != 0; i--, insn = NEXT_INSN (insn))
373     debug_rtx (insn);
374 }
375
376 /* Call this function to search an rtx list to find one with insn uid UID,
377    and then call debug_rtx_list to print it, using DEBUG_RTX_COUNT.
378    The found insn is returned to enable further debugging analysis.  */
379
380 rtx
381 debug_rtx_find (x, uid)
382      rtx x;
383      int uid;
384 {
385   while (x != 0 && INSN_UID (x) != uid)
386     x = NEXT_INSN (x);
387   if (x != 0)
388     {
389       debug_rtx_list (x, debug_rtx_count);
390       return x;
391     }
392   else
393     {
394       fprintf (stderr, "insn uid %d not found\n", uid);
395       return 0;
396     }
397 }
398
399 /* External entry point for printing a chain of insns
400    starting with RTX_FIRST onto file OUTF.
401    A blank line separates insns.
402
403    If RTX_FIRST is not an insn, then it alone is printed, with no newline.  */
404
405 void
406 print_rtl (outf, rtx_first)
407      FILE *outf;
408      rtx rtx_first;
409 {
410   register rtx tmp_rtx;
411
412   outfile = outf;
413   sawclose = 0;
414
415   if (rtx_first == 0)
416     fputs ("(nil)\n", outf);
417   else
418     switch (GET_CODE (rtx_first))
419       {
420       case INSN:
421       case JUMP_INSN:
422       case CALL_INSN:
423       case NOTE:
424       case CODE_LABEL:
425       case BARRIER:
426         for (tmp_rtx = rtx_first; NULL != tmp_rtx; tmp_rtx = NEXT_INSN (tmp_rtx))
427           {
428             if (! flag_dump_unnumbered
429                 || GET_CODE (tmp_rtx) != NOTE
430                 || NOTE_LINE_NUMBER (tmp_rtx) < 0)
431               {
432                 print_rtx (tmp_rtx);
433                 fprintf (outfile, "\n");
434               }
435           }
436         break;
437
438       default:
439         print_rtx (rtx_first);
440       }
441 }
442
443 /* Like print_rtx, except specify a file.  */
444 /* Return nonzero if we actually printed anything.  */
445
446 int
447 print_rtl_single (outf, x)
448      FILE *outf;
449      rtx x;
450 {
451   outfile = outf;
452   sawclose = 0;
453   if (! flag_dump_unnumbered
454       || GET_CODE (x) != NOTE || NOTE_LINE_NUMBER (x) < 0)
455     {
456       print_rtx (x);
457       putc ('\n', outf);
458       return 1;
459     }
460   return 0;
461 }