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