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