]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/genflags.c
defaults.h (obstack_chunk_alloc, [...]): Default definition.
[thirdparty/gcc.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 "hconfig.h"
26 #include "system.h"
27 #include "rtl.h"
28 #include "obstack.h"
29 #include "errors.h"
30 #include "gensupport.h"
31
32 /* Obstack to remember insns with. */
33 static struct obstack obstack;
34
35 /* Max size of names encountered. */
36 static int max_id_len;
37
38 /* Max operand encountered in a scan over some insn. */
39 static int max_opno;
40
41 static void max_operand_1 PARAMS ((rtx));
42 static int num_operands PARAMS ((rtx));
43 static void gen_proto PARAMS ((rtx));
44 static void gen_macro PARAMS ((const char *, int, int));
45 static void gen_insn PARAMS ((rtx));
46
47 /* Count the number of match_operand's found. */
48
49 static void
50 max_operand_1 (x)
51 rtx x;
52 {
53 RTX_CODE code;
54 int i;
55 int len;
56 const char *fmt;
57
58 if (x == 0)
59 return;
60
61 code = GET_CODE (x);
62
63 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
64 || code == MATCH_PARALLEL)
65 max_opno = MAX (max_opno, XINT (x, 0));
66
67 fmt = GET_RTX_FORMAT (code);
68 len = GET_RTX_LENGTH (code);
69 for (i = 0; i < len; i++)
70 {
71 if (fmt[i] == 'e' || fmt[i] == 'u')
72 max_operand_1 (XEXP (x, i));
73 else if (fmt[i] == 'E')
74 {
75 int j;
76 for (j = 0; j < XVECLEN (x, i); j++)
77 max_operand_1 (XVECEXP (x, i, j));
78 }
79 }
80 }
81
82 static int
83 num_operands (insn)
84 rtx insn;
85 {
86 int len = XVECLEN (insn, 1);
87 int i;
88
89 max_opno = -1;
90
91 for (i = 0; i < len; i++)
92 max_operand_1 (XVECEXP (insn, 1, i));
93
94 return max_opno + 1;
95 }
96
97 /* Print out a wrapper macro for a function which corrects the number
98 of arguments it takes. Any missing arguments are assumed to be at
99 the end. */
100 static void
101 gen_macro (name, real, expect)
102 const char *name;
103 int real, expect;
104 {
105 int i;
106
107 if (real > expect)
108 abort ();
109 if (real == 0)
110 abort ();
111
112 /* #define GEN_CALL(A, B, C, D) gen_call((A), (B)) */
113 fputs ("#define GEN_", stdout);
114 for (i = 0; name[i]; i++)
115 putchar (TOUPPER (name[i]));
116
117 putchar('(');
118 for (i = 0; i < expect - 1; i++)
119 printf ("%c, ", i + 'A');
120 printf ("%c) gen_%s (", i + 'A', name);
121
122 for (i = 0; i < real - 1; i++)
123 printf ("(%c), ", i + 'A');
124 printf ("(%c))\n", i + 'A');
125 }
126
127 /* Print out prototype information for a function. */
128
129 static void
130 gen_proto (insn)
131 rtx insn;
132 {
133 int num = num_operands (insn);
134 const char *name = XSTR (insn, 0);
135
136 /* Many md files don't refer to the last two operands passed to the
137 call patterns. This means their generator functions will be two
138 arguments too short. Instead of changing every md file to touch
139 those operands, we wrap the prototypes in macros that take the
140 correct number of arguments. */
141 if (name[0] == 'c' || name[0] == 's')
142 {
143 if (!strcmp (name, "call")
144 || !strcmp (name, "call_pop")
145 || !strcmp (name, "sibcall")
146 || !strcmp (name, "sibcall_pop"))
147 gen_macro (name, num, 4);
148 else if (!strcmp (name, "call_value")
149 || !strcmp (name, "call_value_pop")
150 || !strcmp (name, "sibcall_value")
151 || !strcmp (name, "sibcall_value_pop"))
152 gen_macro (name, num, 5);
153 }
154
155 printf ("extern struct rtx_def *gen_%-*s PARAMS ((", max_id_len, name);
156
157 if (num == 0)
158 printf ("void");
159 else
160 {
161 while (num-- > 1)
162 printf ("struct rtx_def *, ");
163
164 printf ("struct rtx_def *");
165 }
166
167 printf ("));\n");
168
169 }
170
171 static void
172 gen_insn (insn)
173 rtx insn;
174 {
175 const char *name = XSTR (insn, 0);
176 const char *p;
177 int len;
178
179 /* Don't mention instructions whose names are the null string
180 or begin with '*'. They are in the machine description just
181 to be recognized. */
182 if (name[0] == 0 || name[0] == '*')
183 return;
184
185 len = strlen (name);
186
187 if (len > max_id_len)
188 max_id_len = len;
189
190 printf ("#define HAVE_%s ", name);
191 if (strlen (XSTR (insn, 2)) == 0)
192 printf ("1\n");
193 else
194 {
195 /* Write the macro definition, putting \'s at the end of each line,
196 if more than one. */
197 printf ("(");
198 for (p = XSTR (insn, 2); *p; p++)
199 {
200 if (IS_VSPACE (*p))
201 printf (" \\\n");
202 else
203 printf ("%c", *p);
204 }
205 printf (")\n");
206 }
207
208 obstack_grow (&obstack, &insn, sizeof (rtx));
209 }
210
211 extern int main PARAMS ((int, char **));
212
213 int
214 main (argc, argv)
215 int argc;
216 char **argv;
217 {
218 rtx desc;
219 rtx dummy;
220 rtx *insns;
221 rtx *insn_ptr;
222
223 progname = "genflags";
224 obstack_init (&obstack);
225
226 if (argc <= 1)
227 fatal ("no input file name");
228
229 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
230 return (FATAL_EXIT_CODE);
231
232 puts ("/* Generated automatically by the program `genflags'");
233 puts (" from the machine description file `md'. */\n");
234 puts ("#ifndef GCC_INSN_FLAGS_H");
235 puts ("#define GCC_INSN_FLAGS_H\n");
236
237 /* Read the machine description. */
238
239 while (1)
240 {
241 int line_no, insn_code_number = 0;
242
243 desc = read_md_rtx (&line_no, &insn_code_number);
244 if (desc == NULL)
245 break;
246 if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
247 gen_insn (desc);
248 }
249
250 /* Print out the prototypes now. */
251 dummy = (rtx) 0;
252 obstack_grow (&obstack, &dummy, sizeof (rtx));
253 insns = (rtx *) obstack_finish (&obstack);
254
255 printf ("struct rtx_def;\n");
256 for (insn_ptr = insns; *insn_ptr; insn_ptr++)
257 gen_proto (*insn_ptr);
258
259 puts("\n#endif /* GCC_INSN_FLAGS_H */");
260
261 if (ferror (stdout) || fflush (stdout) || fclose (stdout))
262 return FATAL_EXIT_CODE;
263
264 return SUCCESS_EXIT_CODE;
265 }
266
267 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
268 const char *
269 get_insn_name (code)
270 int code ATTRIBUTE_UNUSED;
271 {
272 return NULL;
273 }