OSDN Git Service

6a4b3a46051fe1bfa716dda36c66768ac440271a
[pf3gnuchains/gcc-fork.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 "system.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 #if defined(HAVE_CPP_STRINGIFY) || (defined(__GNUC__) && defined(__STDC__))
39 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { # ENUM, NAME, FORMAT },
40 #else
41 #define DEF_RTL_EXPR(ENUM, NAME, FORMAT, CLASS) { "ENUM", NAME, FORMAT },
42 #endif
43
44 struct rtx_definition defs[] = 
45 {  
46 #include "rtl.def"              /* rtl expressions are documented here */
47 };
48
49 const char *formats[NUM_RTX_CODE];
50
51 static const char *type_from_format PROTO((char));
52 static const char *accessor_from_format PROTO((char));
53 static int special_format PROTO((const char *));
54 static int special_rtx PROTO((int));
55 static void find_formats PROTO((void));
56 static void gendecl PROTO((FILE *, const char *));
57 static void genmacro PROTO((FILE *, int));
58 static void gendef PROTO((FILE *, const char *));
59 static void genlegend PROTO((FILE *));
60 static void genheader PROTO((FILE *));
61 static void gencode PROTO((FILE *));
62
63 static const char *
64 type_from_format (c)
65      char c;
66 {
67   switch (c)
68     {
69     case 'i':
70       return "int";
71     case 'w':
72       return "HOST_WIDE_INT";
73     case 's':
74       return "char *";
75     case 'e':
76     case 'u':
77       return "rtx";
78     case 'E':
79       return "rtvec";
80     default:
81       abort ();
82     }
83 }
84
85 static const char *
86 accessor_from_format (c)
87      char c;
88 {
89   switch (c)
90     {
91     case 'i':
92       return "XINT";
93     case 'w':
94       return "XWINT";
95     case 's':
96       return "XSTR";
97     case 'e':
98     case 'u':
99       return "XEXP";
100     case 'E':
101       return "XVEC";
102     default:
103       abort ();
104     }
105 }
106
107 static int
108 special_format (fmt)
109      const char *fmt;
110 {
111   return (strchr (fmt, '*') != 0
112           || strchr (fmt, 'V') != 0
113           || strchr (fmt, 'S') != 0
114           || strchr (fmt, 'n') != 0);
115 }
116
117 static int
118 special_rtx (idx)
119      int idx;
120 {
121   return (strcmp (defs[idx].enumname, "CONST_INT") == 0
122           || strcmp (defs[idx].enumname, "REG") == 0);
123 }
124
125 static void
126 find_formats ()
127 {
128   int i;
129
130   for (i = 0; i < NUM_RTX_CODE; ++i)
131     {
132       const char **f;
133
134       if (special_format (defs[i].format))
135         continue;
136
137       for (f = formats; *f ; ++f)
138         if (!strcmp(*f, defs[i].format))
139           break;
140
141       if (!*f)
142         *f = defs[i].format;
143     }
144 }
145
146 static void
147 gendecl (f, format)
148      FILE *f;
149      const char *format;
150 {
151   const char *p;
152   int i;
153   
154   fprintf (f, "extern rtx gen_rtx_fmt_%s PROTO((RTX_CODE, enum machine_mode mode",
155            format);
156   for (p = format, i = 0; *p ; ++p)
157     if (*p != '0')
158       fprintf (f, ", %s arg%d", type_from_format (*p), i++);
159   fprintf (f, "));\n");
160 }
161
162 static void 
163 genmacro (f, idx)
164      FILE *f;
165      int idx;
166 {
167   const char *p;
168   int i;
169
170   fprintf (f, "#define gen_rtx_%s%s(mode",
171            (special_rtx (idx) ? "raw_" : ""), defs[idx].enumname);
172
173   for (p = defs[idx].format, i = 0; *p ; ++p)
174     if (*p != '0')
175       fprintf (f, ", arg%d", i++);
176   fprintf (f, ")   ");
177
178   fprintf (f, "gen_rtx_fmt_%s(%s,(mode)", defs[idx].format, defs[idx].enumname);
179   for (p = defs[idx].format, i = 0; *p ; ++p)
180     if (*p != '0')
181       fprintf (f, ",(arg%d)", i++);
182   fprintf (f, ")\n");
183 }
184
185 static void
186 gendef (f, format)
187      FILE *f;
188      const char *format;
189 {
190   const char *p;
191   int i, j;
192   
193   fprintf (f, "rtx\ngen_rtx_fmt_%s (code, mode", format);
194   for (p = format, i = 0; *p ; ++p)
195     if (*p != '0')
196       fprintf (f, ", arg%d", i++);
197
198   fprintf (f, ")\n     RTX_CODE code;\n     enum machine_mode mode;\n");
199   for (p = format, i = 0; *p ; ++p)
200     if (*p != '0')
201       fprintf (f, "     %s arg%d;\n", type_from_format (*p), i++);
202
203   /* See rtx_alloc in rtl.c for comments.  */
204   fprintf (f, "{\n");
205   fprintf (f, "  rtx rt = obstack_alloc_rtx (sizeof (struct rtx_def) + %d * sizeof (rtunion));\n",
206            (int) strlen (format) - 1);
207
208   fprintf (f, "  PUT_CODE (rt, code);\n");
209   fprintf (f, "  PUT_MODE (rt, mode);\n");
210
211   for (p = format, i = j = 0; *p ; ++p, ++i)
212     if (*p != '0')
213       {
214         fprintf (f, "  %s (rt, %d) = arg%d;\n",
215                  accessor_from_format (*p), i, j++);
216       }
217
218   fprintf (f, "\n  return rt;\n}\n\n");
219 }
220
221 static void
222 genlegend (f)
223      FILE *f;
224 {
225   fprintf (f, "/* Generated automaticaly by the program `gengenrtl'\n");
226   fprintf (f, "   from the RTL description file `rtl.def' */\n\n");
227 }
228
229 static void
230 genheader (f)
231      FILE *f;
232 {
233   int i;
234   const char **fmt;
235
236   for (fmt = formats; *fmt; ++fmt)
237     gendecl (f, *fmt);
238
239   fprintf(f, "\n");
240
241   for (i = 0; i < NUM_RTX_CODE; i++)
242     {
243       if (special_format (defs[i].format))
244         continue;
245       genmacro (f, i);
246     }
247 }
248
249 static void
250 gencode (f)
251      FILE *f;
252 {
253   const char **fmt;
254
255   fputs ("#include \"config.h\"\n", f);
256   fputs ("#include \"obstack.h\"\n", f);
257   fputs ("#include \"rtl.h\"\n\n", f);
258   fputs ("extern struct obstack *rtl_obstack;\n\n", f);
259   fputs ("static rtx obstack_alloc_rtx PROTO((int length));\n", f);
260   fputs ("static rtx obstack_alloc_rtx (length)\n", f);
261   fputs ("     register int length;\n{\n", f);
262   fputs ("  rtx rt = (rtx) obstack_alloc (rtl_obstack, length);\n\n", f);
263   fputs ("  if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(int))\n", f);
264   fputs ("    *(int *)rt = 0;\n", f);
265   fputs ("  else if (sizeof(struct rtx_def) - sizeof(rtunion) == sizeof(HOST_WIDE_INT))\n", f);
266   fputs ("    *(HOST_WIDE_INT *)rt = 0;\n", f);
267   fputs ("  else\n", f);
268   fputs ("    bzero(rt, sizeof(struct rtx_def) - sizeof(rtunion));\n\n", f);
269   fputs ("  return rt;\n}\n\n", f);
270
271   for (fmt = formats; *fmt; ++fmt)
272     gendef (f, *fmt);
273 }
274
275 #if defined(USE_C_ALLOCA) && !defined(__GNUC__)
276 char *
277 xmalloc (nbytes)
278      int nbytes;
279 {
280   char *tmp = (char *) malloc (nbytes);
281
282   if (!tmp)
283     {
284       fprintf (stderr, "can't allocate %d bytes (out of virtual memory)\n", nbytes);
285       exit (FATAL_EXIT_CODE);
286     }
287
288   return tmp;
289 }
290 #endif /* USE_C_ALLOCA && !__GNUC__ */
291
292 int
293 main(argc, argv)
294      int argc;
295      char **argv;
296 {
297   FILE *f;
298
299   if (argc != 3)
300     exit (1);
301
302   find_formats ();
303
304   f = fopen (argv[1], "w");
305   if (f == NULL)
306     {
307       perror(argv[1]);
308       exit (1);
309     }
310   genlegend (f);
311   genheader (f);
312   fclose(f);
313
314   f = fopen (argv[2], "w");
315   if (f == NULL)
316     {
317       perror(argv[2]);
318       exit (1);
319     }
320   genlegend (f);
321   gencode (f);
322   fclose(f);
323
324   exit (0);
325 }