OSDN Git Service

Mon May 25 03:34:42 1998 Craig Burley <burley@gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / genconfig.c
1 /* Generate from machine description:
2    - some #define configuration flags.
3    Copyright (C) 1987, 1991, 1997, 1998 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 "hconfig.h"
24 #ifdef __STDC__
25 #include <stdarg.h>
26 #else
27 #include <varargs.h>
28 #endif
29 #include "system.h"
30 #include "rtl.h"
31 #include "obstack.h"
32
33 static struct obstack obstack;
34 struct obstack *rtl_obstack = &obstack;
35
36 #define obstack_chunk_alloc xmalloc
37 #define obstack_chunk_free free
38
39 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
40 char **insn_name_ptr = 0;
41
42 /* flags to determine output of machine description dependent #define's.  */
43 static int max_recog_operands;  /* Largest operand number seen.  */
44 static int max_dup_operands;    /* Largest number of match_dup in any insn.  */
45 static int max_clobbers_per_insn;
46 static int register_constraint_flag;
47 static int have_cc0_flag;
48 static int have_cmove_flag;
49 static int have_lo_sum_flag;
50
51 /* Maximum number of insns seen in a split.  */
52 static int max_insns_per_split = 1;
53
54 static int clobbers_seen_this_insn;
55 static int dup_operands_seen_this_insn;
56
57 char *xmalloc PROTO((unsigned));
58 static void fatal PVPROTO ((char *, ...)) ATTRIBUTE_PRINTF_1;
59 void fancy_abort PROTO((void));
60
61 static void walk_insn_part PROTO((rtx, int, int));
62 static void gen_insn PROTO((rtx));
63 static void gen_expand PROTO((rtx));
64 static void gen_split PROTO((rtx));
65 static void gen_peephole PROTO((rtx));
66
67 /* RECOG_P will be non-zero if this pattern was seen in a context where it will
68    be used to recognize, rather than just generate an insn. 
69
70    NON_PC_SET_SRC will be non-zero if this pattern was seen in a SET_SRC
71    of a SET whose destination is not (pc).  */
72
73 static void
74 walk_insn_part (part, recog_p, non_pc_set_src)
75      rtx part;
76      int recog_p;
77      int non_pc_set_src;
78 {
79   register int i, j;
80   register RTX_CODE code;
81   register char *format_ptr;
82
83   if (part == 0)
84     return;
85
86   code = GET_CODE (part);
87   switch (code)
88     {
89     case CLOBBER:
90       clobbers_seen_this_insn++;
91       break;
92
93     case MATCH_OPERAND:
94       if (XINT (part, 0) > max_recog_operands)
95         max_recog_operands = XINT (part, 0);
96       if (XSTR (part, 2) && *XSTR (part, 2))
97         register_constraint_flag = 1;
98       return;
99
100     case MATCH_OP_DUP:
101     case MATCH_PAR_DUP:
102       ++dup_operands_seen_this_insn;
103     case MATCH_SCRATCH:
104     case MATCH_PARALLEL:
105     case MATCH_OPERATOR:
106       if (XINT (part, 0) > max_recog_operands)
107         max_recog_operands = XINT (part, 0);
108       /* Now scan the rtl's in the vector inside the MATCH_OPERATOR or
109          MATCH_PARALLEL.  */
110       break;
111
112     case LABEL_REF:
113       if (GET_CODE (XEXP (part, 0)) == MATCH_OPERAND)
114         break;
115       return;
116
117     case MATCH_DUP:
118       ++dup_operands_seen_this_insn;
119       if (XINT (part, 0) > max_recog_operands)
120         max_recog_operands = XINT (part, 0);
121       return;
122
123     case CC0:
124       if (recog_p)
125         have_cc0_flag = 1;
126       return;
127
128     case LO_SUM:
129       if (recog_p)
130         have_lo_sum_flag = 1;
131       return;
132
133     case SET:
134       walk_insn_part (SET_DEST (part), 0, recog_p);
135       walk_insn_part (SET_SRC (part), recog_p,
136                       GET_CODE (SET_DEST (part)) != PC);
137       return;
138
139     case IF_THEN_ELSE:
140       /* Only consider this machine as having a conditional move if the
141          two arms of the IF_THEN_ELSE are both MATCH_OPERAND.  Otherwise,
142          we have some specific IF_THEN_ELSE construct (like the doz
143          instruction on the RS/6000) that can't be used in the general
144          context we want it for.  */
145
146       if (recog_p && non_pc_set_src
147           && GET_CODE (XEXP (part, 1)) == MATCH_OPERAND
148           && GET_CODE (XEXP (part, 2)) == MATCH_OPERAND)
149         have_cmove_flag = 1;
150       break;
151
152     case REG: case CONST_INT: case SYMBOL_REF:
153     case PC:
154       return;
155
156     default:
157       break;
158     }
159
160   format_ptr = GET_RTX_FORMAT (GET_CODE (part));
161
162   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
163     switch (*format_ptr++)
164       {
165       case 'e':
166       case 'u':
167         walk_insn_part (XEXP (part, i), recog_p, non_pc_set_src);
168         break;
169       case 'E':
170         if (XVEC (part, i) != NULL)
171           for (j = 0; j < XVECLEN (part, i); j++)
172             walk_insn_part (XVECEXP (part, i, j), recog_p, non_pc_set_src);
173         break;
174       }
175 }
176
177 static void
178 gen_insn (insn)
179      rtx insn;
180 {
181   int i;
182
183   /* Walk the insn pattern to gather the #define's status.  */
184   clobbers_seen_this_insn = 0;
185   dup_operands_seen_this_insn = 0;
186   if (XVEC (insn, 1) != 0)
187     for (i = 0; i < XVECLEN (insn, 1); i++)
188       walk_insn_part (XVECEXP (insn, 1, i), 1, 0);
189
190   if (clobbers_seen_this_insn > max_clobbers_per_insn)
191     max_clobbers_per_insn = clobbers_seen_this_insn;
192   if (dup_operands_seen_this_insn > max_dup_operands)
193     max_dup_operands = dup_operands_seen_this_insn;
194 }
195
196 /* Similar but scan a define_expand.  */
197
198 static void
199 gen_expand (insn)
200      rtx insn;
201 {
202   int i;
203
204   /* Walk the insn pattern to gather the #define's status.  */
205
206   /* Note that we don't bother recording the number of MATCH_DUPs
207      that occur in a gen_expand, because only reload cares about that.  */
208   if (XVEC (insn, 1) != 0)
209     for (i = 0; i < XVECLEN (insn, 1); i++)
210       {
211         /* Compute the maximum SETs and CLOBBERS
212            in any one of the sub-insns;
213            don't sum across all of them.  */
214         clobbers_seen_this_insn = 0;
215
216         walk_insn_part (XVECEXP (insn, 1, i), 0, 0);
217
218         if (clobbers_seen_this_insn > max_clobbers_per_insn)
219           max_clobbers_per_insn = clobbers_seen_this_insn;
220       }
221 }
222
223 /* Similar but scan a define_split.  */
224
225 static void
226 gen_split (split)
227      rtx split;
228 {
229   int i;
230
231   /* Look through the patterns that are matched
232      to compute the maximum operand number.  */
233   for (i = 0; i < XVECLEN (split, 0); i++)
234     walk_insn_part (XVECEXP (split, 0, i), 1, 0);
235   /* Look at the number of insns this insn could split into.  */
236   if (XVECLEN (split, 2) > max_insns_per_split)
237     max_insns_per_split = XVECLEN (split, 2);
238 }
239
240 static void
241 gen_peephole (peep)
242      rtx peep;
243 {
244   int i;
245
246   /* Look through the patterns that are matched
247      to compute the maximum operand number.  */
248   for (i = 0; i < XVECLEN (peep, 0); i++)
249     walk_insn_part (XVECEXP (peep, 0, i), 1, 0);
250 }
251 \f
252 char *
253 xmalloc (size)
254      unsigned size;
255 {
256   register char *val = (char *) malloc (size);
257
258   if (val == 0)
259     fatal ("virtual memory exhausted");
260
261   return val;
262 }
263
264 char *
265 xrealloc (ptr, size)
266      char *ptr;
267      unsigned size;
268 {
269   char *result = (char *) realloc (ptr, size);
270   if (!result)
271     fatal ("virtual memory exhausted");
272   return result;
273 }
274
275 static void
276 fatal VPROTO ((char *format, ...))
277 {
278 #ifndef __STDC__
279   char *format;
280 #endif
281   va_list ap;
282
283   VA_START (ap, format);
284
285 #ifndef __STDC__
286   format = va_arg (ap, char *);
287 #endif
288
289   fprintf (stderr, "genconfig: ");
290   vfprintf (stderr, format, ap);
291   va_end (ap);
292   fprintf (stderr, "\n");
293   exit (FATAL_EXIT_CODE);
294 }
295
296 /* More 'friendly' abort that prints the line and file.
297    config.h can #define abort fancy_abort if you like that sort of thing.  */
298
299 void
300 fancy_abort ()
301 {
302   fatal ("Internal gcc abort.");
303 }
304 \f
305 int
306 main (argc, argv)
307      int argc;
308      char **argv;
309 {
310   rtx desc;
311   FILE *infile;
312   register int c;
313
314   obstack_init (rtl_obstack);
315
316   if (argc <= 1)
317     fatal ("No input file name.");
318
319   infile = fopen (argv[1], "r");
320   if (infile == 0)
321     {
322       perror (argv[1]);
323       exit (FATAL_EXIT_CODE);
324     }
325
326   init_rtl ();
327
328   printf ("/* Generated automatically by the program `genconfig'\n\
329 from the machine description file `md'.  */\n\n");
330
331   /* Allow at least 10 operands for the sake of asm constructs.  */
332   max_recog_operands = 9;  /* We will add 1 later.  */
333   max_dup_operands = 1;
334
335   /* Read the machine description.  */
336
337   while (1)
338     {
339       c = read_skip_spaces (infile);
340       if (c == EOF)
341         break;
342       ungetc (c, infile);
343
344       desc = read_rtx (infile);
345       if (GET_CODE (desc) == DEFINE_INSN)
346         gen_insn (desc);
347       if (GET_CODE (desc) == DEFINE_EXPAND)
348         gen_expand (desc);
349       if (GET_CODE (desc) == DEFINE_SPLIT)
350         gen_split (desc);
351       if (GET_CODE (desc) == DEFINE_PEEPHOLE)
352         gen_peephole (desc);
353     }
354
355   printf ("\n#define MAX_RECOG_OPERANDS %d\n", max_recog_operands + 1);
356
357   printf ("\n#define MAX_DUP_OPERANDS %d\n", max_dup_operands);
358
359   /* This is conditionally defined, in case the user writes code which emits
360      more splits than we can readily see (and knows s/he does it).  */
361   printf ("#ifndef MAX_INSNS_PER_SPLIT\n#define MAX_INSNS_PER_SPLIT %d\n#endif\n",
362           max_insns_per_split);
363
364   if (register_constraint_flag)
365     printf ("#define REGISTER_CONSTRAINTS\n");
366
367   if (have_cc0_flag)
368     printf ("#define HAVE_cc0\n");
369
370   if (have_cmove_flag)
371     printf ("#define HAVE_conditional_move\n");
372
373   if (have_lo_sum_flag)
374     printf ("#define HAVE_lo_sum\n");
375
376   fflush (stdout);
377   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
378   /* NOTREACHED */
379   return 0;
380 }