OSDN Git Service

* gengenrtl.c (genlegend): Fix misspelling.
[pf3gnuchains/gcc-fork.git] / gcc / gengenrtl.c
1 /* Generate code to allocate RTL structures.
2    Copyright (C) 1997, 1998, 1999, 2000 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
34    We need to go out to e0wwwww, since REAL_ARITHMETIC assumes 16-bits
35    per element in REAL_VALUE_TYPE.
36
37    This is duplicated in rtl.c.
38
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.  */
41
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 */
51
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 */
61
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
75
76
77 struct rtx_definition 
78 {
79   const char *enumname, *name, *format;
80 };
81
82 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { STRINGIFY(ENUM), NAME, FORMAT },
83
84 struct rtx_definition defs[] = 
85 {  
86 #include "rtl.def"              /* rtl expressions are documented here */
87 };
88
89 const char *formats[NUM_RTX_CODE];
90
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));
102 \f
103 /* Decode a format letter into a C type string.  */
104
105 static const char *
106 type_from_format (c)
107      int c;
108 {
109   switch (c)
110     {
111     case 'i':
112       return "int ";
113
114     case 'w':
115       return "HOST_WIDE_INT ";
116
117     case 's':
118       return "const char *";
119
120     case 'e':  case 'u':
121       return "rtx ";
122
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 ();
131     }
132 }
133
134 /* Decode a format letter into the proper accessor function.  */
135
136 static const char *
137 accessor_from_format (c)
138      int c;
139 {
140   switch (c)
141     {
142     case 'i':
143       return "XINT";
144
145     case 'w':
146       return "XWINT";
147
148     case 's':
149       return "XSTR";
150
151     case 'e':  case 'u':
152       return "XEXP";
153
154     case 'E':
155       return "XVEC";
156
157     case 'b':
158       return "XBITMAP";
159
160     case 't':
161       return "XTREE";
162
163     default:
164       abort ();
165     }
166 }
167
168 /* Return nonzero if we should ignore FMT, an RTL format, when making
169    the list of formats we write routines to create.  */
170
171 static int
172 special_format (fmt)
173      const char *fmt;
174 {
175   return (strchr (fmt, '*') != 0
176           || strchr (fmt, 'V') != 0
177           || strchr (fmt, 'S') != 0
178           || strchr (fmt, 'n') != 0);
179 }
180
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.  */
184
185 static int
186 special_rtx (idx)
187      int idx;
188 {
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);
193 }
194
195 /* Place a list of all format specifiers we use into the array FORMAT. */
196
197 static void
198 find_formats ()
199 {
200   int i;
201
202   for (i = 0; i < NUM_RTX_CODE; i++)
203     {
204       const char **f;
205
206       if (special_format (defs[i].format))
207         continue;
208
209       for (f = formats; *f; f++)
210         if (! strcmp (*f, defs[i].format))
211           break;
212
213       if (*f == 0)
214         *f = defs[i].format;
215     }
216 }
217
218 /* Write the declarations for the routine to allocate RTL with FORMAT.  */
219
220 static void
221 gendecl (format)
222      const char *format;
223 {
224   const char *p;
225   int i, pos;
226   
227   printf ("extern rtx gen_rtx_fmt_%s\tPARAMS ((RTX_CODE, ", format);
228   printf ("enum machine_mode mode");
229
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')
234       {
235         int ourlen = strlen (type_from_format (*p)) + 6 + (i > 9);
236
237         printf (",");
238         if (pos + ourlen > 76)
239           printf ("\n\t\t\t\t      "), pos = 39;
240
241         printf (" %sarg%d", type_from_format (*p), i++);
242         pos += ourlen;
243       }
244
245   printf ("));\n");
246 }
247
248 /* Generate macros to generate RTL of code IDX using the functions we
249    write.  */
250
251 static void 
252 genmacro (idx)
253      int idx;
254 {
255   const char *p;
256   int i;
257
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.  */
260
261   printf ("#define gen_rtx_%s%s(MODE",
262            special_rtx (idx) ? "raw_" : "", defs[idx].enumname);
263
264   for (p = defs[idx].format, i = 0; *p != 0; p++)
265     if (*p != '0')
266       printf (", ARG%d", i++);
267
268   printf (") \\\n  gen_rtx_fmt_%s (%s, (MODE)",
269           defs[idx].format, defs[idx].enumname);
270
271   for (p = defs[idx].format, i = 0; *p != 0; p++)
272     if (*p != '0')
273       printf (", (ARG%d)", i++);
274
275   printf (")\n");
276 }
277
278 /* Generate the code for the function to generate RTL whose
279    format is FORMAT.  */
280
281 static void
282 gendef (format)
283      const char *format;
284 {
285   const char *p;
286   int i, j;
287   
288   /* Start by writing the definition of the function name and the types
289      of the arguments.  */
290
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++);
295
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++);
300
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);
311
312   printf ("  PUT_CODE (rt, code);\n");
313   printf ("  PUT_MODE (rt, mode);\n");
314
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++);
318
319   printf ("\n  return rt;\n}\n\n");
320 }
321
322 /* Generate the documentation header for files we write.  */
323
324 static void
325 genlegend ()
326 {
327   printf ("/* Generated automatically by the program `gengenrtl'\n");
328   printf ("   from the RTL description file `rtl.def' */\n\n");
329 }
330
331 /* Generate the text of the header file we make, genrtl.h.  */
332
333 static void
334 genheader ()
335 {
336   int i;
337   const char **fmt;
338   
339   for (fmt = formats; *fmt; ++fmt)
340     gendecl (*fmt);
341
342   printf ("\n");
343
344   for (i = 0; i < NUM_RTX_CODE; i++)
345     if (! special_format (defs[i].format))
346       genmacro (i);
347 }
348
349 /* Generate the text of the code file we write, genrtl.c.  */
350
351 static void
352 gencode ()
353 {
354   const char **fmt;
355
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 PARAMS ((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");
368
369   for (fmt = formats; *fmt != 0; fmt++)
370     gendef (*fmt);
371 }
372
373 #if defined(USE_C_ALLOCA)
374 PTR
375 xmalloc (nbytes)
376   size_t nbytes;
377 {
378   register PTR tmp = (PTR) malloc (nbytes);
379
380   if (!tmp)
381     {
382       fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n",
383                nbytes);
384       exit (FATAL_EXIT_CODE);
385     }
386
387   return tmp;
388 }
389 #endif /* USE_C_ALLOCA */
390
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 PARAMS ((int, char **));
395
396 int
397 main (argc, argv)
398      int argc;
399      char **argv;
400 {
401   find_formats ();
402   genlegend ();
403
404   if (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'h')
405     genheader ();
406   else
407     gencode ();
408
409   fflush (stdout);
410   return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
411 }