]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gengenrtl.c
Merge in gcc2-ss-010999
[thirdparty/gcc.git] / gcc / gengenrtl.c
1 /* Generate code to allocate RTL structures.
2 Copyright (C) 1997, 1998, 1999 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 #define NO_GENRTL_H
26 #include "rtl.h"
27 #undef abort
28
29 #include "real.h"
30
31 /* Calculate the format for CONST_DOUBLE. This depends on the relative
32 widths of HOST_WIDE_INT and REAL_VALUE_TYPE.
33 We only need to go out to e0wwww, since min(HOST_WIDE_INT)==32 and
34 max(LONG_DOUBLE_TYPE_SIZE)==128.
35 This is duplicated in rtl.c.
36 A number of places assume that there are always at least two 'w'
37 slots in a CONST_DOUBLE, so we provide them even if one would suffice. */
38 #if HOST_BITS_PER_WIDE_INT >= LONG_DOUBLE_TYPE_SIZE
39 #define CONST_DOUBLE_FORMAT "e0ww"
40 #elif HOST_BITS_PER_WIDE_INT*2 >= LONG_DOUBLE_TYPE_SIZE
41 #define CONST_DOUBLE_FORMAT "e0ww"
42 #elif HOST_BITS_PER_WIDE_INT*3 >= LONG_DOUBLE_TYPE_SIZE
43 #define CONST_DOUBLE_FORMAT "e0www"
44 #elif HOST_BITS_PER_WIDE_INT*4 >= LONG_DOUBLE_TYPE_SIZE
45 #define CONST_DOUBLE_FORMAT "e0wwww"
46 #else
47 #define CONST_DOUBLE_FORMAT /* nothing - will cause syntax error */
48 #endif
49
50
51 struct rtx_definition
52 {
53 const char *enumname, *name, *format;
54 };
55
56 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { STRINGIFY(ENUM), NAME, FORMAT },
57
58 struct rtx_definition defs[] =
59 {
60 #include "rtl.def" /* rtl expressions are documented here */
61 };
62
63 const char *formats[NUM_RTX_CODE];
64
65 static const char *type_from_format PROTO((int));
66 static const char *accessor_from_format PROTO((int));
67 static int special_format PROTO((const char *));
68 static int special_rtx PROTO((int));
69 static void find_formats PROTO((void));
70 static void gendecl PROTO((const char *));
71 static void genmacro PROTO((int));
72 static void gendef PROTO((const char *));
73 static void genlegend PROTO((void));
74 static void genheader PROTO((void));
75 static void gencode PROTO((void));
76 \f
77 /* Decode a format letter into a C type string. */
78
79 static const char *
80 type_from_format (c)
81 int c;
82 {
83 switch (c)
84 {
85 case 'i':
86 return "int ";
87
88 case 'w':
89 return "HOST_WIDE_INT ";
90
91 case 's':
92 return "char *";
93
94 case 'e': case 'u':
95 return "rtx ";
96
97 case 'E':
98 return "rtvec ";
99 case 'b':
100 return "struct bitmap_head_def *"; /* bitmap - typedef not available */
101 case 't':
102 return "union tree_node *"; /* tree - typedef not available */
103 default:
104 abort ();
105 }
106 }
107
108 /* Decode a format letter into the proper accessor function. */
109
110 static const char *
111 accessor_from_format (c)
112 int c;
113 {
114 switch (c)
115 {
116 case 'i':
117 return "XINT";
118
119 case 'w':
120 return "XWINT";
121
122 case 's':
123 return "XSTR";
124
125 case 'e': case 'u':
126 return "XEXP";
127
128 case 'E':
129 return "XVEC";
130
131 case 'b':
132 return "XBITMAP";
133
134 case 't':
135 return "XTREE";
136
137 default:
138 abort ();
139 }
140 }
141
142 /* Return nonzero if we should ignore FMT, an RTL format, when making
143 the list of formats we write routines to create. */
144
145 static int
146 special_format (fmt)
147 const char *fmt;
148 {
149 return (strchr (fmt, '*') != 0
150 || strchr (fmt, 'V') != 0
151 || strchr (fmt, 'S') != 0
152 || strchr (fmt, 'n') != 0);
153 }
154
155 /* Return nonzero if the RTL code given by index IDX is one that we should not
156 generate a gen_RTX_FOO function foo (because that function is present
157 elsewhere in the compiler. */
158
159 static int
160 special_rtx (idx)
161 int idx;
162 {
163 return (strcmp (defs[idx].enumname, "CONST_INT") == 0
164 || strcmp (defs[idx].enumname, "CONST_DOUBLE") == 0
165 || strcmp (defs[idx].enumname, "REG") == 0
166 || strcmp (defs[idx].enumname, "MEM") == 0);
167 }
168
169 /* Place a list of all format specifiers we use into the array FORMAT. */
170
171 static void
172 find_formats ()
173 {
174 int i;
175
176 for (i = 0; i < NUM_RTX_CODE; i++)
177 {
178 const char **f;
179
180 if (special_format (defs[i].format))
181 continue;
182
183 for (f = formats; *f; f++)
184 if (! strcmp (*f, defs[i].format))
185 break;
186
187 if (*f == 0)
188 *f = defs[i].format;
189 }
190 }
191
192 /* Write the declarations for the routine to allocate RTL with FORMAT. */
193
194 static void
195 gendecl (format)
196 const char *format;
197 {
198 const char *p;
199 int i, pos;
200
201 printf ("extern rtx gen_rtx_fmt_%s\tPROTO((RTX_CODE, ", format);
202 printf ("enum machine_mode mode");
203
204 /* Write each parameter that is needed and start a new line when the line
205 would overflow. */
206 for (p = format, i = 0, pos = 75; *p != 0; p++)
207 if (*p != '0')
208 {
209 int ourlen = strlen (type_from_format (*p)) + 6 + (i > 9);
210
211 printf (",");
212 if (pos + ourlen > 76)
213 printf ("\n\t\t\t\t "), pos = 39;
214
215 printf (" %sarg%d", type_from_format (*p), i++);
216 pos += ourlen;
217 }
218
219 printf ("));\n");
220 }
221
222 /* Generate macros to generate RTL of code IDX using the functions we
223 write. */
224
225 static void
226 genmacro (idx)
227 int idx;
228 {
229 const char *p;
230 int i;
231
232 /* We write a macro that defines gen_rtx_RTLCODE to be an equivalent to
233 gen_rtx_fmt_FORMAT where FORMAT is the RTX_FORMAT of RTLCODE. */
234
235 printf ("#define gen_rtx_%s%s(MODE",
236 special_rtx (idx) ? "raw_" : "", defs[idx].enumname);
237
238 for (p = defs[idx].format, i = 0; *p != 0; p++)
239 if (*p != '0')
240 printf (", ARG%d", i++);
241
242 printf (") \\\n gen_rtx_fmt_%s (%s, (MODE)",
243 defs[idx].format, defs[idx].enumname);
244
245 for (p = defs[idx].format, i = 0; *p != 0; p++)
246 if (*p != '0')
247 printf (", (ARG%d)", i++);
248
249 printf (")\n");
250 }
251
252 /* Generate the code for the function to generate RTL whose
253 format is FORMAT. */
254
255 static void
256 gendef (format)
257 const char *format;
258 {
259 const char *p;
260 int i, j;
261
262 /* Start by writing the definition of the function name and the types
263 of the arguments. */
264
265 printf ("rtx\ngen_rtx_fmt_%s (code, mode", format);
266 for (p = format, i = 0; *p != 0; p++)
267 if (*p != '0')
268 printf (", arg%d", i++);
269
270 printf (")\n RTX_CODE code;\n enum machine_mode mode;\n");
271 for (p = format, i = 0; *p != 0; p++)
272 if (*p != '0')
273 printf (" %sarg%d;\n", type_from_format (*p), i++);
274
275 /* Now write out the body of the function itself, which allocates
276 the memory and initializes it. */
277 printf ("{\n");
278 printf (" rtx rt;\n");
279 printf (" if (ggc_p)\n");
280 printf (" rt = ggc_alloc_rtx (%d);\n",
281 (int) strlen (format));
282 printf (" else\n");
283 printf (" rt = obstack_alloc_rtx (sizeof (struct rtx_def) + %d * sizeof (rtunion));\n",
284 (int) strlen (format) - 1);
285
286 printf (" PUT_CODE (rt, code);\n");
287 printf (" PUT_MODE (rt, mode);\n");
288
289 for (p = format, i = j = 0; *p ; ++p, ++i)
290 if (*p != '0')
291 printf (" %s (rt, %d) = arg%d;\n", accessor_from_format (*p), i, j++);
292
293 printf ("\n return rt;\n}\n\n");
294 }
295
296 /* Generate the documentation header for files we write. */
297
298 static void
299 genlegend ()
300 {
301 printf ("/* Generated automaticaly by the program `gengenrtl'\n");
302 printf (" from the RTL description file `rtl.def' */\n\n");
303 }
304
305 /* Generate the text of the header file we make, genrtl.h. */
306
307 static void
308 genheader ()
309 {
310 int i;
311 const char **fmt;
312
313 for (fmt = formats; *fmt; ++fmt)
314 gendecl (*fmt);
315
316 printf ("\n");
317
318 for (i = 0; i < NUM_RTX_CODE; i++)
319 if (! special_format (defs[i].format))
320 genmacro (i);
321 }
322
323 /* Generate the text of the code file we write, genrtl.c. */
324
325 static void
326 gencode ()
327 {
328 const char **fmt;
329
330 puts ("#include \"config.h\"\n");
331 puts ("#include \"system.h\"\n");
332 puts ("#include \"obstack.h\"\n");
333 puts ("#include \"rtl.h\"\n");
334 puts ("#include \"ggc.h\"\n\n");
335 puts ("extern struct obstack *rtl_obstack;\n\n");
336 puts ("static rtx obstack_alloc_rtx PROTO((int length));\n");
337 puts ("static rtx obstack_alloc_rtx (length)\n");
338 puts (" register int length;\n{\n");
339 puts (" rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n");
340 puts (" memset(rt, 0, sizeof(struct rtx_def) - sizeof(rtunion));\n\n");
341 puts (" return rt;\n}\n\n");
342
343 for (fmt = formats; *fmt != 0; fmt++)
344 gendef (*fmt);
345 }
346
347 #if defined(USE_C_ALLOCA)
348 PTR
349 xmalloc (nbytes)
350 size_t nbytes;
351 {
352 register PTR tmp = (PTR) malloc (nbytes);
353
354 if (!tmp)
355 {
356 fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n",
357 nbytes);
358 exit (FATAL_EXIT_CODE);
359 }
360
361 return tmp;
362 }
363 #endif /* USE_C_ALLOCA */
364
365 /* This is the main program. We accept only one argument, "-h", which
366 says we are writing the genrtl.h file. Otherwise we are writing the
367 genrtl.c file. */
368
369 int
370 main (argc, argv)
371 int argc;
372 char **argv;
373 {
374 find_formats ();
375 genlegend ();
376
377 if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')
378 genheader ();
379 else
380 gencode ();
381
382 fflush (stdout);
383 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
384 /* NOTREACHED */
385 return 0;
386 }