Add D30V options
[official-gcc.git] / gcc / gengenrtl.c
blob9e8bb24f751979f0c42355d17e8ca3c23cb1093e
1 /* Generate code to allocate RTL structures.
2 Copyright (C) 1997, 1998, 1999, 2000 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 MAX_LONG_DOUBLE_TYPE_SIZE == 96
44 #define REAL_WIDTH (11*8 + HOST_BITS_PER_WIDE_INT)/HOST_BITS_PER_WIDE_INT
45 #elif MAX_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 >= MAX_LONG_DOUBLE_TYPE_SIZE
54 #define REAL_WIDTH 2
55 #elif HOST_BITS_PER_WIDE_INT*3 >= MAX_LONG_DOUBLE_TYPE_SIZE
56 #define REAL_WIDTH 3
57 #elif HOST_BITS_PER_WIDE_INT*4 >= MAX_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 PARAMS ((int));
92 static const char *accessor_from_format PARAMS ((int));
93 static int special_format PARAMS ((const char *));
94 static int special_rtx PARAMS ((int));
95 static void find_formats PARAMS ((void));
96 static void gendecl PARAMS ((const char *));
97 static void genmacro PARAMS ((int));
98 static void gendef PARAMS ((const char *));
99 static void genlegend PARAMS ((void));
100 static void genheader PARAMS ((void));
101 static void gencode PARAMS ((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 "const 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\tPARAMS ((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 puts (")");
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 puts (")\n RTX_CODE code;\n enum machine_mode mode;");
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 puts ("{");
304 puts (" rtx rt;");
305 puts (" if (ggc_p)");
306 printf (" rt = ggc_alloc_rtx (%d);\n", (int) strlen (format));
307 puts (" else");
308 printf (" rt = obstack_alloc_rtx (%d);\n", (int) strlen (format));
310 puts (" memset (rt, 0, sizeof (struct rtx_def) - sizeof (rtunion));\n");
311 puts (" PUT_CODE (rt, code);");
312 puts (" PUT_MODE (rt, mode);");
314 for (p = format, i = j = 0; *p ; ++p, ++i)
315 if (*p != '0')
316 printf (" %s (rt, %d) = arg%d;\n", accessor_from_format (*p), i, j++);
317 else
318 printf (" X0EXP (rt, %d) = NULL_RTX;\n", i);
320 puts ("\n return rt;\n}\n");
323 /* Generate the documentation header for files we write. */
325 static void
326 genlegend ()
328 puts ("/* Generated automatically by gengenrtl from rtl.def. */\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 putchar ('\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\"");
357 puts ("#include \"system.h\"");
358 puts ("#include \"obstack.h\"");
359 puts ("#include \"rtl.h\"");
360 puts ("#include \"ggc.h\"\n");
361 puts ("extern struct obstack *rtl_obstack;\n");
362 puts ("#define obstack_alloc_rtx(n) \\");
363 puts (" ((rtx) obstack_alloc (rtl_obstack, \\");
364 puts (" sizeof (struct rtx_def) \\");
365 puts (" + ((n) - 1) * sizeof (rtunion)))\n");
367 for (fmt = formats; *fmt != 0; fmt++)
368 gendef (*fmt);
371 #if defined(USE_C_ALLOCA)
373 xmalloc (nbytes)
374 size_t nbytes;
376 register PTR tmp = (PTR) malloc (nbytes);
378 if (!tmp)
380 fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n",
381 nbytes);
382 exit (FATAL_EXIT_CODE);
385 return tmp;
387 #endif /* USE_C_ALLOCA */
389 /* This is the main program. We accept only one argument, "-h", which
390 says we are writing the genrtl.h file. Otherwise we are writing the
391 genrtl.c file. */
392 extern int main PARAMS ((int, char **));
395 main (argc, argv)
396 int argc;
397 char **argv;
399 find_formats ();
400 genlegend ();
402 if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')
403 genheader ();
404 else
405 gencode ();
407 fflush (stdout);
408 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);