Daily bump.
[official-gcc.git] / gcc / gengenrtl.c
blob778ef9b5414e5635445fa0aca7ee5bcf7858c4a7
1 /* Generate code to allocate RTL structures.
2 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
22 #include "hconfig.h"
23 #include "system.h"
25 #define NO_GENRTL_H
26 #include "rtl.h"
27 #undef abort
29 #include "real.h"
31 /* Calculate the format for CONST_DOUBLE. This depends on the relative
32 widths of HOST_WIDE_INT and REAL_VALUE_TYPE.
34 We need to go out to e0wwwww, since REAL_ARITHMETIC assumes 16-bits
35 per element in REAL_VALUE_TYPE.
37 This is duplicated in rtl.c.
39 A number of places assume that there are always at least two 'w'
40 slots in a CONST_DOUBLE, so we provide them even if one would suffice. */
42 #ifdef REAL_ARITHMETIC
43 #if LONG_DOUBLE_TYPE_SIZE == 96
44 #define REAL_WIDTH (11*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
45 #elif LONG_DOUBLE_TYPE_SIZE == 128
46 #define REAL_WIDTH (19*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
47 #elif HOST_FLOAT_FORMAT != TARGET_FLOAT_FORMAT
48 #define REAL_WIDTH (7*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
49 #endif
50 #endif /* REAL_ARITHMETIC */
52 #ifndef REAL_WIDTH
53 #if HOST_BITS_PER_WIDE_INT*2 >= LONG_DOUBLE_TYPE_SIZE
54 #define REAL_WIDTH 2
55 #elif HOST_BITS_PER_WIDE_INT*3 >= LONG_DOUBLE_TYPE_SIZE
56 #define REAL_WIDTH 3
57 #elif HOST_BITS_PER_WIDE_INT*4 >= LONG_DOUBLE_TYPE_SIZE
58 #define REAL_WIDTH 4
59 #endif
60 #endif /* REAL_WIDTH */
62 #if REAL_WIDTH == 1
63 #define CONST_DOUBLE_FORMAT "e0ww"
64 #elif REAL_WIDTH == 2
65 #define CONST_DOUBLE_FORMAT "e0ww"
66 #elif REAL_WIDTH == 3
67 #define CONST_DOUBLE_FORMAT "e0www"
68 #elif REAL_WIDTH == 4
69 #define CONST_DOUBLE_FORMAT "e0wwww"
70 #elif REAL_WIDTH == 5
71 #define CONST_DOUBLE_FORMAT "e0wwwww"
72 #else
73 #define CONST_DOUBLE_FORMAT /* nothing - will cause syntax error */
74 #endif
77 struct rtx_definition
79 const char *enumname, *name, *format;
82 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { STRINGIFY(ENUM), NAME, FORMAT },
84 struct rtx_definition defs[] =
86 #include "rtl.def" /* rtl expressions are documented here */
89 const char *formats[NUM_RTX_CODE];
91 static const char *type_from_format PROTO((int));
92 static const char *accessor_from_format PROTO((int));
93 static int special_format PROTO((const char *));
94 static int special_rtx PROTO((int));
95 static void find_formats PROTO((void));
96 static void gendecl PROTO((const char *));
97 static void genmacro PROTO((int));
98 static void gendef PROTO((const char *));
99 static void genlegend PROTO((void));
100 static void genheader PROTO((void));
101 static void gencode PROTO((void));
103 /* Decode a format letter into a C type string. */
105 static const char *
106 type_from_format (c)
107 int c;
109 switch (c)
111 case 'i':
112 return "int ";
114 case 'w':
115 return "HOST_WIDE_INT ";
117 case 's':
118 return "char *";
120 case 'e': case 'u':
121 return "rtx ";
123 case 'E':
124 return "rtvec ";
125 case 'b':
126 return "struct bitmap_head_def *"; /* bitmap - typedef not available */
127 case 't':
128 return "union tree_node *"; /* tree - typedef not available */
129 default:
130 abort ();
134 /* Decode a format letter into the proper accessor function. */
136 static const char *
137 accessor_from_format (c)
138 int c;
140 switch (c)
142 case 'i':
143 return "XINT";
145 case 'w':
146 return "XWINT";
148 case 's':
149 return "XSTR";
151 case 'e': case 'u':
152 return "XEXP";
154 case 'E':
155 return "XVEC";
157 case 'b':
158 return "XBITMAP";
160 case 't':
161 return "XTREE";
163 default:
164 abort ();
168 /* Return nonzero if we should ignore FMT, an RTL format, when making
169 the list of formats we write routines to create. */
171 static int
172 special_format (fmt)
173 const char *fmt;
175 return (strchr (fmt, '*') != 0
176 || strchr (fmt, 'V') != 0
177 || strchr (fmt, 'S') != 0
178 || strchr (fmt, 'n') != 0);
181 /* Return nonzero if the RTL code given by index IDX is one that we should not
182 generate a gen_RTX_FOO function foo (because that function is present
183 elsewhere in the compiler. */
185 static int
186 special_rtx (idx)
187 int idx;
189 return (strcmp (defs[idx].enumname, "CONST_INT") == 0
190 || strcmp (defs[idx].enumname, "CONST_DOUBLE") == 0
191 || strcmp (defs[idx].enumname, "REG") == 0
192 || strcmp (defs[idx].enumname, "MEM") == 0);
195 /* Place a list of all format specifiers we use into the array FORMAT. */
197 static void
198 find_formats ()
200 int i;
202 for (i = 0; i < NUM_RTX_CODE; i++)
204 const char **f;
206 if (special_format (defs[i].format))
207 continue;
209 for (f = formats; *f; f++)
210 if (! strcmp (*f, defs[i].format))
211 break;
213 if (*f == 0)
214 *f = defs[i].format;
218 /* Write the declarations for the routine to allocate RTL with FORMAT. */
220 static void
221 gendecl (format)
222 const char *format;
224 const char *p;
225 int i, pos;
227 printf ("extern rtx gen_rtx_fmt_%s\tPROTO((RTX_CODE, ", format);
228 printf ("enum machine_mode mode");
230 /* Write each parameter that is needed and start a new line when the line
231 would overflow. */
232 for (p = format, i = 0, pos = 75; *p != 0; p++)
233 if (*p != '0')
235 int ourlen = strlen (type_from_format (*p)) + 6 + (i > 9);
237 printf (",");
238 if (pos + ourlen > 76)
239 printf ("\n\t\t\t\t "), pos = 39;
241 printf (" %sarg%d", type_from_format (*p), i++);
242 pos += ourlen;
245 printf ("));\n");
248 /* Generate macros to generate RTL of code IDX using the functions we
249 write. */
251 static void
252 genmacro (idx)
253 int idx;
255 const char *p;
256 int i;
258 /* We write a macro that defines gen_rtx_RTLCODE to be an equivalent to
259 gen_rtx_fmt_FORMAT where FORMAT is the RTX_FORMAT of RTLCODE. */
261 printf ("#define gen_rtx_%s%s(MODE",
262 special_rtx (idx) ? "raw_" : "", defs[idx].enumname);
264 for (p = defs[idx].format, i = 0; *p != 0; p++)
265 if (*p != '0')
266 printf (", ARG%d", i++);
268 printf (") \\\n gen_rtx_fmt_%s (%s, (MODE)",
269 defs[idx].format, defs[idx].enumname);
271 for (p = defs[idx].format, i = 0; *p != 0; p++)
272 if (*p != '0')
273 printf (", (ARG%d)", i++);
275 printf (")\n");
278 /* Generate the code for the function to generate RTL whose
279 format is FORMAT. */
281 static void
282 gendef (format)
283 const char *format;
285 const char *p;
286 int i, j;
288 /* Start by writing the definition of the function name and the types
289 of the arguments. */
291 printf ("rtx\ngen_rtx_fmt_%s (code, mode", format);
292 for (p = format, i = 0; *p != 0; p++)
293 if (*p != '0')
294 printf (", arg%d", i++);
296 printf (")\n RTX_CODE code;\n enum machine_mode mode;\n");
297 for (p = format, i = 0; *p != 0; p++)
298 if (*p != '0')
299 printf (" %sarg%d;\n", type_from_format (*p), i++);
301 /* Now write out the body of the function itself, which allocates
302 the memory and initializes it. */
303 printf ("{\n");
304 printf (" rtx rt;\n");
305 printf (" if (ggc_p)\n");
306 printf (" rt = ggc_alloc_rtx (%d);\n",
307 (int) strlen (format));
308 printf (" else\n");
309 printf (" rt = obstack_alloc_rtx (sizeof (struct rtx_def) + %d * sizeof (rtunion));\n",
310 (int) strlen (format) - 1);
312 printf (" PUT_CODE (rt, code);\n");
313 printf (" PUT_MODE (rt, mode);\n");
315 for (p = format, i = j = 0; *p ; ++p, ++i)
316 if (*p != '0')
317 printf (" %s (rt, %d) = arg%d;\n", accessor_from_format (*p), i, j++);
319 printf ("\n return rt;\n}\n\n");
322 /* Generate the documentation header for files we write. */
324 static void
325 genlegend ()
327 printf ("/* Generated automaticaly by the program `gengenrtl'\n");
328 printf (" from the RTL description file `rtl.def' */\n\n");
331 /* Generate the text of the header file we make, genrtl.h. */
333 static void
334 genheader ()
336 int i;
337 const char **fmt;
339 for (fmt = formats; *fmt; ++fmt)
340 gendecl (*fmt);
342 printf ("\n");
344 for (i = 0; i < NUM_RTX_CODE; i++)
345 if (! special_format (defs[i].format))
346 genmacro (i);
349 /* Generate the text of the code file we write, genrtl.c. */
351 static void
352 gencode ()
354 const char **fmt;
356 puts ("#include \"config.h\"\n");
357 puts ("#include \"system.h\"\n");
358 puts ("#include \"obstack.h\"\n");
359 puts ("#include \"rtl.h\"\n");
360 puts ("#include \"ggc.h\"\n\n");
361 puts ("extern struct obstack *rtl_obstack;\n\n");
362 puts ("static rtx obstack_alloc_rtx PROTO((int length));\n");
363 puts ("static rtx obstack_alloc_rtx (length)\n");
364 puts (" register int length;\n{\n");
365 puts (" rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n");
366 puts (" memset(rt, 0, sizeof(struct rtx_def) - sizeof(rtunion));\n\n");
367 puts (" return rt;\n}\n\n");
369 for (fmt = formats; *fmt != 0; fmt++)
370 gendef (*fmt);
373 #if defined(USE_C_ALLOCA)
375 xmalloc (nbytes)
376 size_t nbytes;
378 register PTR tmp = (PTR) malloc (nbytes);
380 if (!tmp)
382 fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n",
383 nbytes);
384 exit (FATAL_EXIT_CODE);
387 return tmp;
389 #endif /* USE_C_ALLOCA */
391 /* This is the main program. We accept only one argument, "-h", which
392 says we are writing the genrtl.h file. Otherwise we are writing the
393 genrtl.c file. */
394 extern int main PROTO ((int, char **));
397 main (argc, argv)
398 int argc;
399 char **argv;
401 find_formats ();
402 genlegend ();
404 if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')
405 genheader ();
406 else
407 gencode ();
409 fflush (stdout);
410 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);