OSDN Git Service

2003-03-10 Franz Sirl <Franz.Sirl-kernel@lauterbach.com>
[pf3gnuchains/gcc-fork.git] / gcc / genflags.c
1 /* Generate from machine description:
2    - some flags HAVE_... saying which simple standard instructions are
3    available for this machine.
4    Copyright (C) 1987, 1991, 1995, 1998,
5    1999, 2000 Free Software Foundation, Inc.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING.  If not, write to the Free
21 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 02111-1307, USA.  */
23
24
25 #include "bconfig.h"
26 #include "system.h"
27 #include "coretypes.h"
28 #include "tm.h"
29 #include "rtl.h"
30 #include "obstack.h"
31 #include "errors.h"
32 #include "gensupport.h"
33
34 /* Obstack to remember insns with.  */
35 static struct obstack obstack;
36
37 /* Max size of names encountered.  */
38 static int max_id_len;
39
40 /* Max operand encountered in a scan over some insn.  */
41 static int max_opno;
42
43 static void max_operand_1       PARAMS ((rtx));
44 static int num_operands         PARAMS ((rtx));
45 static void gen_proto           PARAMS ((rtx));
46 static void gen_macro           PARAMS ((const char *, int, int));
47 static void gen_insn            PARAMS ((rtx));
48
49 /* Count the number of match_operand's found.  */
50
51 static void
52 max_operand_1 (x)
53      rtx x;
54 {
55   RTX_CODE code;
56   int i;
57   int len;
58   const char *fmt;
59
60   if (x == 0)
61     return;
62
63   code = GET_CODE (x);
64
65   if (code == MATCH_OPERAND || code == MATCH_OPERATOR
66       || code == MATCH_PARALLEL)
67     max_opno = MAX (max_opno, XINT (x, 0));
68
69   fmt = GET_RTX_FORMAT (code);
70   len = GET_RTX_LENGTH (code);
71   for (i = 0; i < len; i++)
72     {
73       if (fmt[i] == 'e' || fmt[i] == 'u')
74         max_operand_1 (XEXP (x, i));
75       else if (fmt[i] == 'E')
76         {
77           int j;
78           for (j = 0; j < XVECLEN (x, i); j++)
79             max_operand_1 (XVECEXP (x, i, j));
80         }
81     }
82 }
83
84 static int
85 num_operands (insn)
86      rtx insn;
87 {
88   int len = XVECLEN (insn, 1);
89   int i;
90
91   max_opno = -1;
92
93   for (i = 0; i < len; i++)
94     max_operand_1 (XVECEXP (insn, 1, i));
95
96   return max_opno + 1;
97 }
98
99 /* Print out a wrapper macro for a function which corrects the number
100    of arguments it takes.  Any missing arguments are assumed to be at
101    the end.  */
102 static void
103 gen_macro (name, real, expect)
104      const char *name;
105      int real, expect;
106 {
107   int i;
108
109   if (real > expect)
110     abort ();
111   if (real == 0)
112     abort ();
113
114   /* #define GEN_CALL(A, B, C, D) gen_call((A), (B)) */
115   fputs ("#define GEN_", stdout);
116   for (i = 0; name[i]; i++)
117     putchar (TOUPPER (name[i]));
118
119   putchar('(');
120   for (i = 0; i < expect - 1; i++)
121     printf ("%c, ", i + 'A');
122   printf ("%c) gen_%s (", i + 'A', name);
123
124   for (i = 0; i < real - 1; i++)
125     printf ("(%c), ", i + 'A');
126   printf ("(%c))\n", i + 'A');
127 }
128
129 /* Print out prototype information for a generator function.  If the
130    insn pattern has been elided, print out a dummy generator that
131    does nothing.  */
132
133 static void
134 gen_proto (insn)
135      rtx insn;
136 {
137   int num = num_operands (insn);
138   int i;
139   const char *name = XSTR (insn, 0);
140   int truth = maybe_eval_c_test (XSTR (insn, 2));
141
142   /* Many md files don't refer to the last two operands passed to the
143      call patterns.  This means their generator functions will be two
144      arguments too short.  Instead of changing every md file to touch
145      those operands, we wrap the prototypes in macros that take the
146      correct number of arguments.  */
147   if (name[0] == 'c' || name[0] == 's')
148     {
149       if (!strcmp (name, "call")
150           || !strcmp (name, "call_pop")
151           || !strcmp (name, "sibcall")
152           || !strcmp (name, "sibcall_pop"))
153         gen_macro (name, num, 4);
154       else if (!strcmp (name, "call_value")
155                || !strcmp (name, "call_value_pop")
156                || !strcmp (name, "sibcall_value")
157                || !strcmp (name, "sibcall_value_pop"))
158         gen_macro (name, num, 5);
159     }
160
161   if (truth != 0)
162     printf ("extern rtx        gen_%-*s PARAMS ((", max_id_len, name);
163   else
164     printf ("static inline rtx gen_%-*s PARAMS ((", max_id_len, name);
165
166   if (num == 0)
167     fputs ("void", stdout);
168   else
169     {
170       for (i = 1; i < num; i++)
171         fputs ("rtx, ", stdout);
172       
173       fputs ("rtx", stdout);
174     }
175
176   puts ("));");
177
178   /* Some back ends want to take the address of generator functions,
179      so we cannot simply use #define for these dummy definitions.  */
180   if (truth == 0)
181     {
182       printf ("static inline rtx\ngen_%s", name);
183       if (num > 0)
184         {
185           putchar ('(');
186           for (i = 0; i < num-1; i++)
187             printf ("%c, ", 'a' + i);
188           printf ("%c)\n", 'a' + i);
189           for (i = 0; i < num; i++)
190             printf ("     rtx %c ATTRIBUTE_UNUSED;\n", 'a' + i);
191         }
192       else
193         puts ("()");
194       puts ("{\n  return 0;\n}");
195     }
196
197 }
198
199 static void
200 gen_insn (insn)
201      rtx insn;
202 {
203   const char *name = XSTR (insn, 0);
204   const char *p;
205   int len;
206   int truth = maybe_eval_c_test (XSTR (insn, 2));
207
208   /* Don't mention instructions whose names are the null string
209      or begin with '*'.  They are in the machine description just
210      to be recognized.  */
211   if (name[0] == 0 || name[0] == '*')
212     return;
213
214   len = strlen (name);
215
216   if (len > max_id_len)
217     max_id_len = len;
218
219   if (truth == 0)
220     /* emit nothing */;
221   else if (truth == 1)
222     printf ("#define HAVE_%s 1\n", name);
223   else
224     {
225       /* Write the macro definition, putting \'s at the end of each line,
226          if more than one.  */
227       printf ("#define HAVE_%s (", name);
228       for (p = XSTR (insn, 2); *p; p++)
229         {
230           if (IS_VSPACE (*p))
231             fputs (" \\\n", stdout);
232           else
233             putchar (*p);
234         }
235       fputs (")\n", stdout);
236     }
237
238   obstack_grow (&obstack, &insn, sizeof (rtx));
239 }
240
241 extern int main PARAMS ((int, char **));
242
243 int
244 main (argc, argv)
245      int argc;
246      char **argv;
247 {
248   rtx desc;
249   rtx dummy;
250   rtx *insns;
251   rtx *insn_ptr;
252
253   progname = "genflags";
254   obstack_init (&obstack);
255
256   /* We need to see all the possibilities.  Elided insns may have
257      direct calls to their generators in C code.  */
258   insn_elision = 0;
259
260   if (argc <= 1)
261     fatal ("no input file name");
262
263   if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
264     return (FATAL_EXIT_CODE);
265   
266   puts ("/* Generated automatically by the program `genflags'");
267   puts ("   from the machine description file `md'.  */\n");
268   puts ("#ifndef GCC_INSN_FLAGS_H");
269   puts ("#define GCC_INSN_FLAGS_H\n");
270
271   /* Read the machine description.  */
272
273   while (1)
274     {
275       int line_no, insn_code_number = 0;
276
277       desc = read_md_rtx (&line_no, &insn_code_number);
278       if (desc == NULL)
279         break;
280       if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
281         gen_insn (desc);
282     }
283
284   /* Print out the prototypes now.  */
285   dummy = (rtx) 0;
286   obstack_grow (&obstack, &dummy, sizeof (rtx));
287   insns = (rtx *) obstack_finish (&obstack);
288
289   for (insn_ptr = insns; *insn_ptr; insn_ptr++)
290     gen_proto (*insn_ptr);
291
292   puts("\n#endif /* GCC_INSN_FLAGS_H */");
293
294   if (ferror (stdout) || fflush (stdout) || fclose (stdout))
295     return FATAL_EXIT_CODE;
296
297   return SUCCESS_EXIT_CODE;
298 }
299
300 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
301 const char *
302 get_insn_name (code)
303      int code ATTRIBUTE_UNUSED;
304 {
305   return NULL;
306 }