Makefile.in (OBJ, GEN, RTL_H): Add genrtl.[oh] bits.
[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, " register int length = sizeof (struct rtx_def)");
188 fprintf (f, " + %d * sizeof (rtunion);\n", strlen (format) - 1);
189 fprintf (f, " rtx rt = (rtx)obstack_alloc (rtl_obstack, length);\n");
190
191 fprintf (f, " if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(int))\n");
192 fprintf (f, " *(int *)rt = 0;\n");
193 fprintf (f, " else if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(HOST_WIDE_INT))\n");
194 fprintf (f, " *(HOST_WIDE_INT *)rt = 0;\n");
195 fprintf (f, " else\n");
196 fprintf (f, " bzero(rt, sizeof(struct rtx_def) - sizeof(rtunion));\n\n");
197
198 fprintf (f, " PUT_CODE (rt, code);\n");
199 fprintf (f, " PUT_MODE (rt, mode);\n");
200
201 for (p = format, i = j = 0; *p ; ++p, ++i)
202 if (*p != '0')
203 {
204 fprintf (f, " %s (rt, %d) = arg%d;\n",
205 accessor_from_format (*p), i, j++);
206 }
207
208 fprintf (f, "\n return rt;\n}\n\n");
209 }
210
211 static void
212 genlegend (f)
213 FILE *f;
214 {
215 fprintf (f, "/* Generated automaticaly by the program `gengenrtl'\n");
216 fprintf (f, " from the RTL description file `rtl.def' */\n\n");
217 }
218
219 static void
220 genheader (f)
221 FILE *f;
222 {
223 int i;
224 const char **fmt;
225
226 for (fmt = formats; *fmt; ++fmt)
227 gendecl (f, *fmt);
228
229 fprintf(f, "\n");
230
231 for (i = 0; i < NUM_RTX_CODE; i++)
232 {
233 if (special_format (defs[i].format))
234 continue;
235 genmacro (f, i);
236 }
237 }
238
239 static void
240 gencode (f)
241 FILE *f;
242 {
243 const char **fmt;
244
245 fprintf(f, "#include \"config.h\"\n");
246 fprintf(f, "#include \"obstack.h\"\n");
247 fprintf(f, "#include \"rtl.h\"\n\n");
248 fprintf(f, "extern struct obstack *rtl_obstack;\n\n");
249
250 for (fmt = formats; *fmt; ++fmt)
251 gendef (f, *fmt);
252 }
253
254 int
255 main(argc, argv)
256 int argc;
257 char **argv;
258 {
259 FILE *f;
260
261 if (argc != 3)
262 exit (1);
263
264 find_formats ();
265
266 f = fopen (argv[1], "w");
267 if (f == NULL)
268 {
269 perror(argv[1]);
270 exit (1);
271 }
272 genlegend (f);
273 genheader (f);
274 fclose(f);
275
276 f = fopen (argv[2], "w");
277 if (f == NULL)
278 {
279 perror(argv[2]);
280 exit (1);
281 }
282 genlegend (f);
283 gencode (f);
284 fclose(f);
285
286 exit (0);
287 }