c-common.c: Include <stdlib.h> and <string.h>/<strings.h>.
[platform/upstream/gcc.git] / gcc / genpeep.c
1 /* Generate code from machine description to perform peephole optimizations.
2    Copyright (C) 1987, 1989, 1992, 1997 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 <stdio.h>
23 #include "hconfig.h"
24 #include "rtl.h"
25 #include "obstack.h"
26
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30
31 static struct obstack obstack;
32 struct obstack *rtl_obstack = &obstack;
33
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
36
37 extern void free ();
38 extern rtx read_rtx ();
39
40 /* While tree-walking an instruction pattern, we keep a chain
41    of these `struct link's to record how to get down to the
42    current position.  In each one, POS is the operand number,
43    and if the operand is a vector VEC is the element number.
44    VEC is -1 if the operand is not a vector.  */
45
46 struct link
47 {
48   struct link *next;
49   int pos;
50   int vecelt;
51 };
52
53 char *xmalloc ();
54 static void match_rtx ();
55 static void fatal ();
56 void fancy_abort ();
57
58 static int max_opno;
59
60 /* Number of operands used in current peephole definition.  */
61
62 static int n_operands;
63
64 /* Peephole optimizations get insn codes just like insn patterns.
65    Count them so we know the code of the define_peephole we are handling.  */
66
67 static int insn_code_number = 0;
68
69 static void print_path ();
70 static void print_code ();
71 \f
72 static void
73 gen_peephole (peep)
74      rtx peep;
75 {
76   int ninsns = XVECLEN (peep, 0);
77   int i;
78
79   n_operands = 0;
80
81   printf ("  insn = ins1;\n");
82 #if 0
83   printf ("  want_jump = 0;\n");
84 #endif
85
86   for (i = 0; i < ninsns; i++)
87     {
88       if (i > 0)
89         {
90           printf ("  do { insn = NEXT_INSN (insn);\n");
91           printf ("       if (insn == 0) goto L%d; }\n",
92                   insn_code_number);
93           printf ("  while (GET_CODE (insn) == NOTE\n");
94           printf ("\t || (GET_CODE (insn) == INSN\n");
95           printf ("\t     && (GET_CODE (PATTERN (insn)) == USE\n");
96           printf ("\t\t || GET_CODE (PATTERN (insn)) == CLOBBER)));\n");
97
98           printf ("  if (GET_CODE (insn) == CODE_LABEL\n\
99       || GET_CODE (insn) == BARRIER)\n    goto L%d;\n",
100                   insn_code_number);
101         }
102
103 #if 0
104       printf ("  if (GET_CODE (insn) == JUMP_INSN)\n");
105       printf ("    want_jump = JUMP_LABEL (insn);\n");
106 #endif
107
108       printf ("  pat = PATTERN (insn);\n");
109
110       /* Walk the insn's pattern, remembering at all times the path
111          down to the walking point.  */
112
113       match_rtx (XVECEXP (peep, 0, i), NULL_PTR, insn_code_number);
114     }
115
116   /* We get this far if the pattern matches.
117      Now test the extra condition.  */
118
119   if (XSTR (peep, 1) && XSTR (peep, 1)[0])
120     printf ("  if (! (%s)) goto L%d;\n",
121             XSTR (peep, 1), insn_code_number);
122
123   /* If that matches, construct new pattern and put it in the first insn.
124      This new pattern will never be matched.
125      It exists only so that insn-extract can get the operands back.
126      So use a simple regular form: a PARALLEL containing a vector
127      of all the operands.  */
128
129   printf ("  PATTERN (ins1) = gen_rtx_PARALLEL (VOIDmode, gen_rtvec_v (%d, operands));\n", n_operands);
130
131 #if 0
132   printf ("  if (want_jump && GET_CODE (ins1) != JUMP_INSN)\n");
133   printf ("    {\n");
134   printf ("      rtx insn2 = emit_jump_insn_before (PATTERN (ins1), ins1);\n");
135   printf ("      delete_insn (ins1);\n");
136   printf ("      ins1 = ins2;\n");
137   printf ("    }\n");
138 #endif
139
140   /* Record this define_peephole's insn code in the insn,
141      as if it had been recognized to match this.  */
142   printf ("  INSN_CODE (ins1) = %d;\n",
143           insn_code_number);
144
145   /* Delete the remaining insns.  */
146   if (ninsns > 1)
147     printf ("  delete_for_peephole (NEXT_INSN (ins1), insn);\n");
148
149   /* See reload1.c for insertion of NOTE which guarantees that this
150      cannot be zero.  */
151   printf ("  return NEXT_INSN (insn);\n");
152
153   printf (" L%d:\n\n", insn_code_number);
154 }
155 \f
156 static void
157 match_rtx (x, path, fail_label)
158      rtx x;
159      struct link *path;
160      int fail_label;
161 {
162   register RTX_CODE code;
163   register int i;
164   register int len;
165   register char *fmt;
166   struct link link;
167
168   if (x == 0)
169     return;
170
171
172   code = GET_CODE (x);
173
174   switch (code)
175     {
176     case MATCH_OPERAND:
177       if (XINT (x, 0) > max_opno)
178         max_opno = XINT (x, 0);
179       if (XINT (x, 0) >= n_operands)
180         n_operands = 1 + XINT (x, 0);
181
182       printf ("  x = ");
183       print_path (path);
184       printf (";\n");
185
186       printf ("  operands[%d] = x;\n", XINT (x, 0));
187       if (XSTR (x, 1) && XSTR (x, 1)[0])
188         printf ("  if (! %s (x, %smode)) goto L%d;\n",
189                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
190       return;
191
192     case MATCH_DUP:
193     case MATCH_PAR_DUP:
194       printf ("  x = ");
195       print_path (path);
196       printf (";\n");
197
198       printf ("  if (!rtx_equal_p (operands[%d], x)) goto L%d;\n",
199               XINT (x, 0), fail_label);
200       return;
201
202     case MATCH_OP_DUP:
203       printf ("  x = ");
204       print_path (path);
205       printf (";\n");
206
207       printf ("  if (GET_CODE (operands[%d]) != GET_CODE (x)\n", XINT (x, 0));
208       printf ("      || GET_MODE (operands[%d]) != GET_MODE (x)) goto L%d;\n",
209               XINT (x, 0), fail_label);
210       printf ("  operands[%d] = x;\n", XINT (x, 0));
211       link.next = path;
212       link.vecelt = -1;
213       for (i = 0; i < XVECLEN (x, 1); i++)
214         {
215           link.pos = i;
216           match_rtx (XVECEXP (x, 1, i), &link, fail_label);
217         }
218       return;
219
220     case MATCH_OPERATOR:
221       if (XINT (x, 0) > max_opno)
222         max_opno = XINT (x, 0);
223       if (XINT (x, 0) >= n_operands)
224         n_operands = 1 + XINT (x, 0);
225
226       printf ("  x = ");
227       print_path (path);
228       printf (";\n");
229
230       printf ("  operands[%d] = x;\n", XINT (x, 0));
231       if (XSTR (x, 1) && XSTR (x, 1)[0])
232         printf ("  if (! %s (x, %smode)) goto L%d;\n",
233                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
234       link.next = path;
235       link.vecelt = -1;
236       for (i = 0; i < XVECLEN (x, 2); i++)
237         {
238           link.pos = i;
239           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
240         }
241       return;
242
243     case MATCH_PARALLEL:
244       if (XINT (x, 0) > max_opno)
245         max_opno = XINT (x, 0);
246       if (XINT (x, 0) >= n_operands)
247         n_operands = 1 + XINT (x, 0);
248
249       printf ("  x = ");
250       print_path (path);
251       printf (";\n");
252
253       printf ("  if (GET_CODE (x) != PARALLEL) goto L%d;\n", fail_label);
254       printf ("  operands[%d] = x;\n", XINT (x, 0));
255       if (XSTR (x, 1) && XSTR (x, 1)[0])
256         printf ("  if (! %s (x, %smode)) goto L%d;\n",
257                 XSTR (x, 1), GET_MODE_NAME (GET_MODE (x)), fail_label);
258       link.next = path;
259       link.pos = 0;
260       for (i = 0; i < XVECLEN (x, 2); i++)
261         {
262           link.vecelt = i;
263           match_rtx (XVECEXP (x, 2, i), &link, fail_label);
264         }
265       return;
266
267     case ADDRESS:
268       match_rtx (XEXP (x, 0), path, fail_label);
269       return;
270       
271     default:
272       break;
273     }
274
275   printf ("  x = ");
276   print_path (path);
277   printf (";\n");
278
279   printf ("  if (GET_CODE (x) != ");
280   print_code (code);
281   printf (") goto L%d;\n", fail_label);
282
283   if (GET_MODE (x) != VOIDmode)
284     {
285       printf ("  if (GET_MODE (x) != %smode) goto L%d;\n",
286               GET_MODE_NAME (GET_MODE (x)), fail_label);
287     }
288
289   link.next = path;
290   link.vecelt = -1;
291   fmt = GET_RTX_FORMAT (code);
292   len = GET_RTX_LENGTH (code);
293   for (i = 0; i < len; i++)
294     {
295       link.pos = i;
296       if (fmt[i] == 'e' || fmt[i] == 'u')
297         match_rtx (XEXP (x, i), &link, fail_label);
298       else if (fmt[i] == 'E')
299         {
300           int j;
301           printf ("  if (XVECLEN (x, %d) != %d) goto L%d;\n",
302                   i, XVECLEN (x, i), fail_label);
303           for (j = 0; j < XVECLEN (x, i); j++)
304             {
305               link.vecelt = j;
306               match_rtx (XVECEXP (x, i, j), &link, fail_label);
307             }
308         }
309       else if (fmt[i] == 'i')
310         {
311           /* Make sure that at run time `x' is the RTX we want to test.  */
312           if (i != 0)
313             {
314               printf ("  x = ");
315               print_path (path);
316               printf (";\n");
317             }
318
319           printf ("  if (XINT (x, %d) != %d) goto L%d;\n",
320                   i, XINT (x, i), fail_label);
321         }
322       else if (fmt[i] == 'w')
323         {
324           /* Make sure that at run time `x' is the RTX we want to test.  */
325           if (i != 0)
326             {
327               printf ("  x = ");
328               print_path (path);
329               printf (";\n");
330             }
331
332           printf ("  if (XWINT (x, %d) != ", i);
333           printf (HOST_WIDE_INT_PRINT_DEC, XWINT (x, i));
334           printf (") goto L%d;\n", fail_label);
335         }
336       else if (fmt[i] == 's')
337         {
338           /* Make sure that at run time `x' is the RTX we want to test.  */
339           if (i != 0)
340             {
341               printf ("  x = ");
342               print_path (path);
343               printf (";\n");
344             }
345
346           printf ("  if (strcmp (XSTR (x, %d), \"%s\")) goto L%d;\n",
347                   i, XSTR (x, i), fail_label);
348         }
349     }
350 }
351
352 /* Given a PATH, representing a path down the instruction's
353    pattern from the root to a certain point, output code to
354    evaluate to the rtx at that point.  */
355
356 static void
357 print_path (path)
358      struct link *path;
359 {
360   if (path == 0)
361     printf ("pat");
362   else if (path->vecelt >= 0)
363     {
364       printf ("XVECEXP (");
365       print_path (path->next);
366       printf (", %d, %d)", path->pos, path->vecelt);
367     }
368   else
369     {
370       printf ("XEXP (");
371       print_path (path->next);
372       printf (", %d)", path->pos);
373     }
374 }
375 \f
376 static void
377 print_code (code)
378      RTX_CODE code;
379 {
380   register char *p1;
381   for (p1 = GET_RTX_NAME (code); *p1; p1++)
382     {
383       if (*p1 >= 'a' && *p1 <= 'z')
384         putchar (*p1 + 'A' - 'a');
385       else
386         putchar (*p1);
387     }
388 }
389 \f
390 char *
391 xmalloc (size)
392      unsigned size;
393 {
394   register char *val = (char *) malloc (size);
395
396   if (val == 0)
397     fatal ("virtual memory exhausted");
398   return val;
399 }
400
401 char *
402 xrealloc (ptr, size)
403      char *ptr;
404      unsigned size;
405 {
406   char *result = (char *) realloc (ptr, size);
407   if (!result)
408     fatal ("virtual memory exhausted");
409   return result;
410 }
411
412 static void
413 fatal (s, a1, a2)
414      char *s;
415 {
416   fprintf (stderr, "genpeep: ");
417   fprintf (stderr, s, a1, a2);
418   fprintf (stderr, "\n");
419   exit (FATAL_EXIT_CODE);
420 }
421
422 /* More 'friendly' abort that prints the line and file.
423    config.h can #define abort fancy_abort if you like that sort of thing.  */
424
425 void
426 fancy_abort ()
427 {
428   fatal ("Internal gcc abort.");
429 }
430 \f
431 int
432 main (argc, argv)
433      int argc;
434      char **argv;
435 {
436   rtx desc;
437   FILE *infile;
438   register int c;
439
440   max_opno = -1;
441
442   obstack_init (rtl_obstack);
443
444   if (argc <= 1)
445     fatal ("No input file name.");
446
447   infile = fopen (argv[1], "r");
448   if (infile == 0)
449     {
450       perror (argv[1]);
451       exit (FATAL_EXIT_CODE);
452     }
453
454   init_rtl ();
455
456   printf ("/* Generated automatically by the program `genpeep'\n\
457 from the machine description file `md'.  */\n\n");
458
459   printf ("#include \"config.h\"\n");
460   printf ("#include <stdio.h>\n");
461   printf ("#include \"rtl.h\"\n");
462   printf ("#include \"regs.h\"\n");
463   printf ("#include \"output.h\"\n");
464   printf ("#include \"real.h\"\n\n");
465
466   printf ("extern rtx peep_operand[];\n\n");
467   printf ("#define operands peep_operand\n\n");
468
469   printf ("rtx\npeephole (ins1)\n     rtx ins1;\n{\n");
470   printf ("  rtx insn, x, pat;\n");
471   printf ("  int i;\n\n");
472
473   /* Early out: no peepholes for insns followed by barriers.  */
474   printf ("  if (NEXT_INSN (ins1)\n");
475   printf ("      && GET_CODE (NEXT_INSN (ins1)) == BARRIER)\n");
476   printf ("    return 0;\n\n");
477
478   /* Read the machine description.  */
479
480   while (1)
481     {
482       c = read_skip_spaces (infile);
483       if (c == EOF)
484         break;
485       ungetc (c, infile);
486
487       desc = read_rtx (infile);
488       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
489         {
490           gen_peephole (desc);
491           insn_code_number++;
492         }
493       if (GET_CODE (desc) == DEFINE_INSN
494           || GET_CODE (desc) == DEFINE_EXPAND
495           || GET_CODE (desc) == DEFINE_SPLIT)
496         {
497           insn_code_number++;
498         }
499     }
500
501   printf ("  return 0;\n}\n\n");
502
503   if (max_opno == -1)
504     max_opno = 1;
505
506   printf ("rtx peep_operand[%d];\n", max_opno + 1);
507
508   fflush (stdout);
509   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
510   /* NOTREACHED */
511   return 0;
512 }