genflags.c revision 169689
11573Srgrimes/* Generate from machine description:
21573Srgrimes   - some flags HAVE_... saying which simple standard instructions are
31573Srgrimes   available for this machine.
41573Srgrimes   Copyright (C) 1987, 1991, 1995, 1998,
51573Srgrimes   1999, 2000, 2003, 2004 Free Software Foundation, Inc.
61573Srgrimes
71573SrgrimesThis file is part of GCC.
81573Srgrimes
91573SrgrimesGCC is free software; you can redistribute it and/or modify it under
101573Srgrimesthe terms of the GNU General Public License as published by the Free
111573SrgrimesSoftware Foundation; either version 2, or (at your option) any later
121573Srgrimesversion.
131573Srgrimes
141573SrgrimesGCC is distributed in the hope that it will be useful, but WITHOUT ANY
151573SrgrimesWARRANTY; without even the implied warranty of MERCHANTABILITY or
161573SrgrimesFITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
171573Srgrimesfor more details.
181573Srgrimes
191573SrgrimesYou should have received a copy of the GNU General Public License
201573Srgrimesalong with GCC; see the file COPYING.  If not, write to the Free
211573SrgrimesSoftware Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
221573Srgrimes02110-1301, USA.  */
231573Srgrimes
241573Srgrimes
251573Srgrimes#include "bconfig.h"
261573Srgrimes#include "system.h"
271573Srgrimes#include "coretypes.h"
281573Srgrimes#include "tm.h"
291573Srgrimes#include "rtl.h"
301573Srgrimes#include "obstack.h"
311573Srgrimes#include "errors.h"
321573Srgrimes#include "gensupport.h"
3392986Sobrien
3492986Sobrien/* Obstack to remember insns with.  */
351573Srgrimesstatic struct obstack obstack;
361573Srgrimes
371573Srgrimes/* Max size of names encountered.  */
381573Srgrimesstatic int max_id_len;
391573Srgrimes
40218285Sjilles/* Max operand encountered in a scan over some insn.  */
41218285Sjillesstatic int max_opno;
42218285Sjilles
43218285Sjillesstatic void max_operand_1 (rtx);
44218285Sjillesstatic int num_operands (rtx);
45218285Sjillesstatic void gen_proto (rtx);
46218285Sjillesstatic void gen_macro (const char *, int, int);
47218285Sjillesstatic void gen_insn (rtx);
48218285Sjilles
49218285Sjilles/* Count the number of match_operand's found.  */
50218285Sjilles
51218285Sjillesstatic void
52218285Sjillesmax_operand_1 (rtx x)
53218285Sjilles{
54218285Sjilles  RTX_CODE code;
55218285Sjilles  int i;
56218285Sjilles  int len;
57218285Sjilles  const char *fmt;
58218285Sjilles
59218285Sjilles  if (x == 0)
60218285Sjilles    return;
61218285Sjilles
62218285Sjilles  code = GET_CODE (x);
63218285Sjilles
64218285Sjilles  if (code == MATCH_OPERAND || code == MATCH_OPERATOR
65218285Sjilles      || code == MATCH_PARALLEL)
66218285Sjilles    max_opno = MAX (max_opno, XINT (x, 0));
67218285Sjilles
68218285Sjilles  fmt = GET_RTX_FORMAT (code);
69218285Sjilles  len = GET_RTX_LENGTH (code);
70218285Sjilles  for (i = 0; i < len; i++)
711573Srgrimes    {
721573Srgrimes      if (fmt[i] == 'e' || fmt[i] == 'u')
731573Srgrimes	max_operand_1 (XEXP (x, i));
741573Srgrimes      else if (fmt[i] == 'E')
751573Srgrimes	{
761573Srgrimes	  int j;
771573Srgrimes	  for (j = 0; j < XVECLEN (x, i); j++)
781573Srgrimes	    max_operand_1 (XVECEXP (x, i, j));
791573Srgrimes	}
801573Srgrimes    }
811573Srgrimes}
821573Srgrimes
831573Srgrimesstatic int
841573Srgrimesnum_operands (rtx insn)
851573Srgrimes{
861573Srgrimes  int len = XVECLEN (insn, 1);
871573Srgrimes  int i;
881573Srgrimes
891573Srgrimes  max_opno = -1;
901573Srgrimes
911573Srgrimes  for (i = 0; i < len; i++)
921573Srgrimes    max_operand_1 (XVECEXP (insn, 1, i));
931573Srgrimes
941573Srgrimes  return max_opno + 1;
951573Srgrimes}
961573Srgrimes
971573Srgrimes/* Print out a wrapper macro for a function which corrects the number
981573Srgrimes   of arguments it takes.  Any missing arguments are assumed to be at
991573Srgrimes   the end.  */
1001573Srgrimesstatic void
1011573Srgrimesgen_macro (const char *name, int real, int expect)
1021573Srgrimes{
1031573Srgrimes  int i;
1041573Srgrimes
1051573Srgrimes  gcc_assert (real <= expect);
1061573Srgrimes  gcc_assert (real);
10747289Speter
108  /* #define GEN_CALL(A, B, C, D) gen_call((A), (B)) */
109  fputs ("#define GEN_", stdout);
110  for (i = 0; name[i]; i++)
111    putchar (TOUPPER (name[i]));
112
113  putchar('(');
114  for (i = 0; i < expect - 1; i++)
115    printf ("%c, ", i + 'A');
116  printf ("%c) gen_%s (", i + 'A', name);
117
118  for (i = 0; i < real - 1; i++)
119    printf ("(%c), ", i + 'A');
120  printf ("(%c))\n", i + 'A');
121}
122
123/* Print out prototype information for a generator function.  If the
124   insn pattern has been elided, print out a dummy generator that
125   does nothing.  */
126
127static void
128gen_proto (rtx insn)
129{
130  int num = num_operands (insn);
131  int i;
132  const char *name = XSTR (insn, 0);
133  int truth = maybe_eval_c_test (XSTR (insn, 2));
134
135  /* Many md files don't refer to the last two operands passed to the
136     call patterns.  This means their generator functions will be two
137     arguments too short.  Instead of changing every md file to touch
138     those operands, we wrap the prototypes in macros that take the
139     correct number of arguments.  */
140  if (name[0] == 'c' || name[0] == 's')
141    {
142      if (!strcmp (name, "call")
143	  || !strcmp (name, "call_pop")
144	  || !strcmp (name, "sibcall")
145	  || !strcmp (name, "sibcall_pop"))
146	gen_macro (name, num, 4);
147      else if (!strcmp (name, "call_value")
148	       || !strcmp (name, "call_value_pop")
149	       || !strcmp (name, "sibcall_value")
150	       || !strcmp (name, "sibcall_value_pop"))
151	gen_macro (name, num, 5);
152    }
153
154  if (truth != 0)
155    printf ("extern rtx        gen_%-*s (", max_id_len, name);
156  else
157    printf ("static inline rtx gen_%-*s (", max_id_len, name);
158
159  if (num == 0)
160    fputs ("void", stdout);
161  else
162    {
163      for (i = 1; i < num; i++)
164	fputs ("rtx, ", stdout);
165
166      fputs ("rtx", stdout);
167    }
168
169  puts (");");
170
171  /* Some back ends want to take the address of generator functions,
172     so we cannot simply use #define for these dummy definitions.  */
173  if (truth == 0)
174    {
175      printf ("static inline rtx\ngen_%s", name);
176      if (num > 0)
177	{
178	  putchar ('(');
179	  for (i = 0; i < num-1; i++)
180	    printf ("rtx ARG_UNUSED (%c), ", 'a' + i);
181	  printf ("rtx ARG_UNUSED (%c))\n", 'a' + i);
182	}
183      else
184	puts ("(void)");
185      puts ("{\n  return 0;\n}");
186    }
187
188}
189
190static void
191gen_insn (rtx insn)
192{
193  const char *name = XSTR (insn, 0);
194  const char *p;
195  int len;
196  int truth = maybe_eval_c_test (XSTR (insn, 2));
197
198  /* Don't mention instructions whose names are the null string
199     or begin with '*'.  They are in the machine description just
200     to be recognized.  */
201  if (name[0] == 0 || name[0] == '*')
202    return;
203
204  len = strlen (name);
205
206  if (len > max_id_len)
207    max_id_len = len;
208
209  if (truth == 0)
210    /* Emit nothing.  */;
211  else if (truth == 1)
212    printf ("#define HAVE_%s 1\n", name);
213  else
214    {
215      /* Write the macro definition, putting \'s at the end of each line,
216	 if more than one.  */
217      printf ("#define HAVE_%s (", name);
218      for (p = XSTR (insn, 2); *p; p++)
219	{
220	  if (IS_VSPACE (*p))
221	    fputs (" \\\n", stdout);
222	  else
223	    putchar (*p);
224	}
225      fputs (")\n", stdout);
226    }
227
228  obstack_grow (&obstack, &insn, sizeof (rtx));
229}
230
231int
232main (int argc, char **argv)
233{
234  rtx desc;
235  rtx dummy;
236  rtx *insns;
237  rtx *insn_ptr;
238
239  progname = "genflags";
240  obstack_init (&obstack);
241
242  /* We need to see all the possibilities.  Elided insns may have
243     direct calls to their generators in C code.  */
244  insn_elision = 0;
245
246  if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
247    return (FATAL_EXIT_CODE);
248
249  puts ("/* Generated automatically by the program `genflags'");
250  puts ("   from the machine description file `md'.  */\n");
251  puts ("#ifndef GCC_INSN_FLAGS_H");
252  puts ("#define GCC_INSN_FLAGS_H\n");
253
254  /* Read the machine description.  */
255
256  while (1)
257    {
258      int line_no, insn_code_number = 0;
259
260      desc = read_md_rtx (&line_no, &insn_code_number);
261      if (desc == NULL)
262	break;
263      if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
264	gen_insn (desc);
265    }
266
267  /* Print out the prototypes now.  */
268  dummy = (rtx) 0;
269  obstack_grow (&obstack, &dummy, sizeof (rtx));
270  insns = XOBFINISH (&obstack, rtx *);
271
272  for (insn_ptr = insns; *insn_ptr; insn_ptr++)
273    gen_proto (*insn_ptr);
274
275  puts("\n#endif /* GCC_INSN_FLAGS_H */");
276
277  if (ferror (stdout) || fflush (stdout) || fclose (stdout))
278    return FATAL_EXIT_CODE;
279
280  return SUCCESS_EXIT_CODE;
281}
282