* gengenrtl.c (DEF_RTL_EXPR): Provide a K&R compliant version.
[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 <stdio.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 #ifdef __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 (char c)
53 {
54 switch (c)
55 {
56 case 'i':
57 return "int";
58 case 'w':
59 return "HOST_WIDE_INT";
60 case 's':
61 return "char *";
62 case 'e':
63 case 'u':
64 return "rtx";
65 case 'E':
66 return "rtvec";
67 default:
68 abort ();
69 }
70 }
71
72 static const char *
73 accessor_from_format (char c)
74 {
75 switch (c)
76 {
77 case 'i':
78 return "XINT";
79 case 'w':
80 return "XWINT";
81 case 's':
82 return "XSTR";
83 case 'e':
84 case 'u':
85 return "XEXP";
86 case 'E':
87 return "XVEC";
88 default:
89 abort ();
90 }
91 }
92
93 static int
94 special_format (fmt)
95 const char *fmt;
96 {
97 return (strchr (fmt, '*') != 0
98 || strchr (fmt, 'V') != 0
99 || strchr (fmt, 'S') != 0
100 || strchr (fmt, 'n') != 0);
101 }
102
103 static int
104 special_rtx (idx)
105 int idx;
106 {
107 return (strcmp (defs[idx].enumname, "CONST_INT") == 0
108 || strcmp (defs[idx].enumname, "REG") == 0);
109 }
110
111 static void
112 find_formats ()
113 {
114 int i;
115
116 for (i = 0; i < NUM_RTX_CODE; ++i)
117 {
118 const char **f;
119
120 if (special_format (defs[i].format))
121 continue;
122
123 for (f = formats; *f ; ++f)
124 if (!strcmp(*f, defs[i].format))
125 break;
126
127 if (!*f)
128 *f = defs[i].format;
129 }
130 }
131
132 static void
133 gendecl (f, format)
134 FILE *f;
135 const char *format;
136 {
137 const char *p;
138 int i;
139
140 fprintf (f, "extern rtx gen_rtx_fmt_%s PROTO((RTX_CODE, enum machine_mode mode",
141 format);
142 for (p = format, i = 0; *p ; ++p)
143 if (*p != '0')
144 fprintf (f, ", %s arg%d", type_from_format (*p), i++);
145 fprintf (f, "));\n");
146 }
147
148 static void
149 genmacro (f, idx)
150 FILE *f;
151 int idx;
152 {
153 const char *p;
154 int i;
155
156 fprintf (f, "#define gen_rtx_%s%s(mode",
157 (special_rtx (idx) ? "raw_" : ""), defs[idx].enumname);
158
159 for (p = defs[idx].format, i = 0; *p ; ++p)
160 if (*p != '0')
161 fprintf (f, ", arg%d", i++);
162 fprintf (f, ") ");
163
164 fprintf (f, "gen_rtx_fmt_%s(%s,(mode)", defs[idx].format, defs[idx].enumname);
165 for (p = defs[idx].format, i = 0; *p ; ++p)
166 if (*p != '0')
167 fprintf (f, ",(arg%d)", i++);
168 fprintf (f, ")\n");
169 }
170
171 static void
172 gendef (f, format)
173 FILE *f;
174 const char *format;
175 {
176 const char *p;
177 int i, j;
178
179 fprintf (f, "rtx\ngen_rtx_fmt_%s (code, mode", format);
180 for (p = format, i = 0; *p ; ++p)
181 if (*p != '0')
182 fprintf (f, ", arg%d", i++);
183
184 fprintf (f, ")\n RTX_CODE code;\n enum machine_mode mode;\n");
185 for (p = format, i = 0; *p ; ++p)
186 if (*p != '0')
187 fprintf (f, " %s arg%d;\n", type_from_format (*p), i++);
188
189 /* See rtx_alloc in rtl.c for comments. */
190 fprintf (f, "{\n");
191 fprintf (f, " rtx rt = obstack_alloc_rtx (sizeof (struct rtx_def) + %d * sizeof (rtunion));\n",
192 (int) strlen (format) - 1);
193
194 fprintf (f, " PUT_CODE (rt, code);\n");
195 fprintf (f, " PUT_MODE (rt, mode);\n");
196
197 for (p = format, i = j = 0; *p ; ++p, ++i)
198 if (*p != '0')
199 {
200 fprintf (f, " %s (rt, %d) = arg%d;\n",
201 accessor_from_format (*p), i, j++);
202 }
203
204 fprintf (f, "\n return rt;\n}\n\n");
205 }
206
207 static void
208 genlegend (f)
209 FILE *f;
210 {
211 fprintf (f, "/* Generated automaticaly by the program `gengenrtl'\n");
212 fprintf (f, " from the RTL description file `rtl.def' */\n\n");
213 }
214
215 static void
216 genheader (f)
217 FILE *f;
218 {
219 int i;
220 const char **fmt;
221
222 for (fmt = formats; *fmt; ++fmt)
223 gendecl (f, *fmt);
224
225 fprintf(f, "\n");
226
227 for (i = 0; i < NUM_RTX_CODE; i++)
228 {
229 if (special_format (defs[i].format))
230 continue;
231 genmacro (f, i);
232 }
233 }
234
235 static void
236 gencode (f)
237 FILE *f;
238 {
239 const char **fmt;
240
241 fputs ("#include \"config.h\"\n", f);
242 fputs ("#include \"obstack.h\"\n", f);
243 fputs ("#include \"rtl.h\"\n\n", f);
244 fputs ("extern struct obstack *rtl_obstack;\n\n", f);
245 fputs ("static rtx obstack_alloc_rtx PROTO((int length));\n", f);
246 fputs ("static rtx obstack_alloc_rtx (length)\n", f);
247 fputs (" register int length;\n{\n", f);
248 fputs (" rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n", f);
249 fputs (" if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(int))\n", f);
250 fputs (" *(int *)rt = 0;\n", f);
251 fputs (" else if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(HOST_WIDE_INT))\n", f);
252 fputs (" *(HOST_WIDE_INT *)rt = 0;\n", f);
253 fputs (" else\n", f);
254 fputs (" bzero(rt, sizeof(struct rtx_def) - sizeof(rtunion));\n\n", f);
255 fputs (" return rt;\n}\n\n", f);
256
257 for (fmt = formats; *fmt; ++fmt)
258 gendef (f, *fmt);
259 }
260
261 int
262 main(argc, argv)
263 int argc;
264 char **argv;
265 {
266 FILE *f;
267
268 if (argc != 3)
269 exit (1);
270
271 find_formats ();
272
273 f = fopen (argv[1], "w");
274 if (f == NULL)
275 {
276 perror(argv[1]);
277 exit (1);
278 }
279 genlegend (f);
280 genheader (f);
281 fclose(f);
282
283 f = fopen (argv[2], "w");
284 if (f == NULL)
285 {
286 perror(argv[2]);
287 exit (1);
288 }
289 genlegend (f);
290 gencode (f);
291 fclose(f);
292
293 exit (0);
294 }