55ac3147480584284a89353f53a963bfe3f2ff8a
[gcc.git] / gcc / gengenrtl.c
1 /* Generate code to allocate RTL structures.
2 Copyright (C) 1997 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include "hconfig.h"
23 #include "system.h"
24
25 #include "obstack.h"
26 #define obstack_chunk_alloc xmalloc
27 #define obstack_chunk_free free
28
29 #define NO_GENRTL_H
30 #include "rtl.h"
31
32
33 struct rtx_definition
34 {
35 const char *enumname, *name, *format;
36 };
37
38 #if defined(HAVE_CPP_STRINGIFY) || (defined(__GNUC__) && defined(__STDC__))
39 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { # ENUM, NAME, FORMAT },
40 #else
41 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { "ENUM", NAME, FORMAT },
42 #endif
43
44 struct rtx_definition defs[] =
45 {
46 #include "rtl.def" /* rtl expressions are documented here */
47 };
48
49 const char *formats[NUM_RTX_CODE];
50
51 static const char *
52 type_from_format (c)
53 char c;
54 {
55 switch (c)
56 {
57 case 'i':
58 return "int";
59 case 'w':
60 return "HOST_WIDE_INT";
61 case 's':
62 return "char *";
63 case 'e':
64 case 'u':
65 return "rtx";
66 case 'E':
67 return "rtvec";
68 default:
69 abort ();
70 }
71 }
72
73 static const char *
74 accessor_from_format (c)
75 char c;
76 {
77 switch (c)
78 {
79 case 'i':
80 return "XINT";
81 case 'w':
82 return "XWINT";
83 case 's':
84 return "XSTR";
85 case 'e':
86 case 'u':
87 return "XEXP";
88 case 'E':
89 return "XVEC";
90 default:
91 abort ();
92 }
93 }
94
95 static int
96 special_format (fmt)
97 const char *fmt;
98 {
99 return (strchr (fmt, '*') != 0
100 || strchr (fmt, 'V') != 0
101 || strchr (fmt, 'S') != 0
102 || strchr (fmt, 'n') != 0);
103 }
104
105 static int
106 special_rtx (idx)
107 int idx;
108 {
109 return (strcmp (defs[idx].enumname, "CONST_INT") == 0
110 || strcmp (defs[idx].enumname, "REG") == 0);
111 }
112
113 static void
114 find_formats ()
115 {
116 int i;
117
118 for (i = 0; i < NUM_RTX_CODE; ++i)
119 {
120 const char **f;
121
122 if (special_format (defs[i].format))
123 continue;
124
125 for (f = formats; *f ; ++f)
126 if (!strcmp(*f, defs[i].format))
127 break;
128
129 if (!*f)
130 *f = defs[i].format;
131 }
132 }
133
134 static void
135 gendecl (f, format)
136 FILE *f;
137 const char *format;
138 {
139 const char *p;
140 int i;
141
142 fprintf (f, "extern rtx gen_rtx_fmt_%s PROTO((RTX_CODE, enum machine_mode mode",
143 format);
144 for (p = format, i = 0; *p ; ++p)
145 if (*p != '0')
146 fprintf (f, ", %s arg%d", type_from_format (*p), i++);
147 fprintf (f, "));\n");
148 }
149
150 static void
151 genmacro (f, idx)
152 FILE *f;
153 int idx;
154 {
155 const char *p;
156 int i;
157
158 fprintf (f, "#define gen_rtx_%s%s(mode",
159 (special_rtx (idx) ? "raw_" : ""), defs[idx].enumname);
160
161 for (p = defs[idx].format, i = 0; *p ; ++p)
162 if (*p != '0')
163 fprintf (f, ", arg%d", i++);
164 fprintf (f, ") ");
165
166 fprintf (f, "gen_rtx_fmt_%s(%s,(mode)", defs[idx].format, defs[idx].enumname);
167 for (p = defs[idx].format, i = 0; *p ; ++p)
168 if (*p != '0')
169 fprintf (f, ",(arg%d)", i++);
170 fprintf (f, ")\n");
171 }
172
173 static void
174 gendef (f, format)
175 FILE *f;
176 const char *format;
177 {
178 const char *p;
179 int i, j;
180
181 fprintf (f, "rtx\ngen_rtx_fmt_%s (code, mode", format);
182 for (p = format, i = 0; *p ; ++p)
183 if (*p != '0')
184 fprintf (f, ", arg%d", i++);
185
186 fprintf (f, ")\n RTX_CODE code;\n enum machine_mode mode;\n");
187 for (p = format, i = 0; *p ; ++p)
188 if (*p != '0')
189 fprintf (f, " %s arg%d;\n", type_from_format (*p), i++);
190
191 /* See rtx_alloc in rtl.c for comments. */
192 fprintf (f, "{\n");
193 fprintf (f, " rtx rt = obstack_alloc_rtx (sizeof (struct rtx_def) + %d * sizeof (rtunion));\n",
194 (int) strlen (format) - 1);
195
196 fprintf (f, " PUT_CODE (rt, code);\n");
197 fprintf (f, " PUT_MODE (rt, mode);\n");
198
199 for (p = format, i = j = 0; *p ; ++p, ++i)
200 if (*p != '0')
201 {
202 fprintf (f, " %s (rt, %d) = arg%d;\n",
203 accessor_from_format (*p), i, j++);
204 }
205
206 fprintf (f, "\n return rt;\n}\n\n");
207 }
208
209 static void
210 genlegend (f)
211 FILE *f;
212 {
213 fprintf (f, "/* Generated automaticaly by the program `gengenrtl'\n");
214 fprintf (f, " from the RTL description file `rtl.def' */\n\n");
215 }
216
217 static void
218 genheader (f)
219 FILE *f;
220 {
221 int i;
222 const char **fmt;
223
224 for (fmt = formats; *fmt; ++fmt)
225 gendecl (f, *fmt);
226
227 fprintf(f, "\n");
228
229 for (i = 0; i < NUM_RTX_CODE; i++)
230 {
231 if (special_format (defs[i].format))
232 continue;
233 genmacro (f, i);
234 }
235 }
236
237 static void
238 gencode (f)
239 FILE *f;
240 {
241 const char **fmt;
242
243 fputs ("#include \"config.h\"\n", f);
244 fputs ("#include \"obstack.h\"\n", f);
245 fputs ("#include \"rtl.h\"\n\n", f);
246 fputs ("extern struct obstack *rtl_obstack;\n\n", f);
247 fputs ("static rtx obstack_alloc_rtx PROTO((int length));\n", f);
248 fputs ("static rtx obstack_alloc_rtx (length)\n", f);
249 fputs (" register int length;\n{\n", f);
250 fputs (" rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n", f);
251 fputs (" if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(int))\n", f);
252 fputs (" *(int *)rt = 0;\n", f);
253 fputs (" else if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(HOST_WIDE_INT))\n", f);
254 fputs (" *(HOST_WIDE_INT *)rt = 0;\n", f);
255 fputs (" else\n", f);
256 fputs (" bzero(rt, sizeof(struct rtx_def) - sizeof(rtunion));\n\n", f);
257 fputs (" return rt;\n}\n\n", f);
258
259 for (fmt = formats; *fmt; ++fmt)
260 gendef (f, *fmt);
261 }
262
263 #if defined(USE_C_ALLOCA) && !defined(__GNUC__)
264 char *
265 xmalloc (nbytes)
266 int nbytes;
267 {
268 char *tmp = (char *) malloc (nbytes);
269
270 if (!tmp)
271 {
272 fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n", nbytes);
273 exit (FATAL_EXIT_CODE);
274 }
275
276 return tmp;
277 }
278 #endif /* USE_C_ALLOCA && !__GNUC__ */
279
280 int
281 main(argc, argv)
282 int argc;
283 char **argv;
284 {
285 FILE *f;
286
287 if (argc != 3)
288 exit (1);
289
290 find_formats ();
291
292 f = fopen (argv[1], "w");
293 if (f == NULL)
294 {
295 perror(argv[1]);
296 exit (1);
297 }
298 genlegend (f);
299 genheader (f);
300 fclose(f);
301
302 f = fopen (argv[2], "w");
303 if (f == NULL)
304 {
305 perror(argv[2]);
306 exit (1);
307 }
308 genlegend (f);
309 gencode (f);
310 fclose(f);
311
312 exit (0);
313 }