Import final gcc2 snapshot (990109)
[official-gcc.git] / gcc / genopinit.c
blob1558c221329748435d6a059d362224674ef88d4d
1 /* Generate code to initialize optabs from machine description.
2 Copyright (C) 1993, 94, 95, 96, 97, 1998 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"
24 #include "rtl.h"
25 #include "obstack.h"
27 static struct obstack obstack;
28 struct obstack *rtl_obstack = &obstack;
30 #define obstack_chunk_alloc xmalloc
31 #define obstack_chunk_free free
34 extern rtx read_rtx ();
36 char *xmalloc ();
38 #ifdef HAVE_VPRINTF
39 void fatal PVPROTO((char *, ...));
40 #else
41 /* We must not provide any prototype here, even if ANSI C. */
42 void fatal PROTO(());
43 #endif
45 void fancy_abort ();
47 /* Many parts of GCC use arrays that are indexed by machine mode and
48 contain the insn codes for pattern in the MD file that perform a given
49 operation on operands of that mode.
51 These patterns are present in the MD file with names that contain
52 the mode(s) used and the name of the operation. This program
53 writes a function `init_all_optabs' that initializes the optabs with
54 all the insn codes of the relevant patterns present in the MD file.
56 This array contains a list of optabs that need to be initialized. Within
57 each string, the name of the pattern to be matched against is delimited
58 with %( and %). In the string, %a and %b are used to match a short mode
59 name (the part of the mode name not including `mode' and converted to
60 lower-case). When writing out the initializer, the entire string is
61 used. %A and %B are replaced with the full name of the mode; %a and %b
62 are replaced with the short form of the name, as above.
64 If %N is present in the pattern, it means the two modes must be consecutive
65 widths in the same mode class (e.g, QImode and HImode). %I means that
66 only integer modes should be considered for the next mode, and %F means
67 that only float modes should be considered.
69 For some optabs, we store the operation by RTL codes. These are only
70 used for comparisons. In that case, %c and %C are the lower-case and
71 upper-case forms of the comparison, respectively. */
73 /* The reason we use \% is to avoid sequences of the form %-capletter-%
74 which SCCS treats as magic. This gets warnings which you should ignore. */
76 char *optabs[] =
77 { "extendtab[(int) %B][(int) %A][0] = CODE_FOR_%(extend%a\%b2%)",
78 "extendtab[(int) %B][(int) %A][1] = CODE_FOR_%(zero_extend%a\%b2%)",
79 "fixtab[(int) %A][(int) %B][0] = CODE_FOR_%(fix%F\%a%I\%b2%)",
80 "fixtab[(int) %A][(int) %B][1] = CODE_FOR_%(fixuns%F\%a%b2%)",
81 "fixtrunctab[(int) %A][(int) %B][0] = CODE_FOR_%(fix_trunc%F\%a%I\%b2%)",
82 "fixtrunctab[(int) %A][(int) %B][1] = CODE_FOR_%(fixuns_trunc%F\%a%I\%b2%)",
83 "floattab[(int) %B][(int) %A][0] = CODE_FOR_%(float%I\%a%F\%b2%)",
84 "floattab[(int) %B][(int) %A][1] = CODE_FOR_%(floatuns%I\%a%F\%b2%)",
85 "add_optab->handlers[(int) %A].insn_code = CODE_FOR_%(add%a3%)",
86 "sub_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sub%a3%)",
87 "smul_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mul%a3%)",
88 "umul_highpart_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umul%a3_highpart%)",
89 "smul_highpart_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smul%a3_highpart%)",
90 "smul_widen_optab->handlers[(int) %B].insn_code = CODE_FOR_%(mul%a%b3%)%N",
91 "umul_widen_optab->handlers[(int) %B].insn_code = CODE_FOR_%(umul%a%b3%)%N",
92 "sdiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(div%I\%a3%)",
93 "udiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(udiv%I\%a3%)",
94 "sdivmod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(divmod%a4%)",
95 "udivmod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(udivmod%a4%)",
96 "smod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mod%a3%)",
97 "umod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umod%a3%)",
98 "flodiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(div%F\%a3%)",
99 "ftrunc_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ftrunc%F\%a2%)",
100 "and_optab->handlers[(int) %A].insn_code = CODE_FOR_%(and%a3%)",
101 "ior_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ior%a3%)",
102 "xor_optab->handlers[(int) %A].insn_code = CODE_FOR_%(xor%a3%)",
103 "ashl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ashl%a3%)",
104 "ashr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ashr%a3%)",
105 "lshr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(lshr%a3%)",
106 "rotl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(rotl%a3%)",
107 "rotr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(rotr%a3%)",
108 "smin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smin%I\%a3%)",
109 "smin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(min%F\%a3%)",
110 "smax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smax%I\%a3%)",
111 "smax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(max%F\%a3%)",
112 "umin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umin%I\%a3%)",
113 "umax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umax%I\%a3%)",
114 "neg_optab->handlers[(int) %A].insn_code = CODE_FOR_%(neg%a2%)",
115 "abs_optab->handlers[(int) %A].insn_code = CODE_FOR_%(abs%a2%)",
116 "sqrt_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sqrt%a2%)",
117 "sin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sin%a2%)",
118 "cos_optab->handlers[(int) %A].insn_code = CODE_FOR_%(cos%a2%)",
119 "strlen_optab->handlers[(int) %A].insn_code = CODE_FOR_%(strlen%a%)",
120 "one_cmpl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(one_cmpl%a2%)",
121 "ffs_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ffs%a2%)",
122 "mov_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mov%a%)",
123 "movstrict_optab->handlers[(int) %A].insn_code = CODE_FOR_%(movstrict%a%)",
124 "cmp_optab->handlers[(int) %A].insn_code = CODE_FOR_%(cmp%a%)",
125 "tst_optab->handlers[(int) %A].insn_code = CODE_FOR_%(tst%a%)",
126 "bcc_gen_fctn[(int) %C] = gen_%(b%c%)",
127 "setcc_gen_code[(int) %C] = CODE_FOR_%(s%c%)",
128 "movcc_gen_code[(int) %A] = CODE_FOR_%(mov%acc%)",
129 "reload_in_optab[(int) %A] = CODE_FOR_%(reload_in%a%)",
130 "reload_out_optab[(int) %A] = CODE_FOR_%(reload_out%a%)",
131 "movstr_optab[(int) %A] = CODE_FOR_%(movstr%a%)",
132 "clrstr_optab[(int) %A] = CODE_FOR_%(clrstr%a%)" };
134 /* Allow linking with print-rtl.c. */
135 char **insn_name_ptr;
137 static void
138 gen_insn (insn)
139 rtx insn;
141 char *name = XSTR (insn, 0);
142 int m1, m2, op;
143 int pindex;
144 int i;
145 char *np, *pp, *p, *q;
146 struct obstack *obstack_ptr;
148 /* Don't mention instructions whose names are the null string.
149 They are in the machine description just to be recognized. */
150 if (*name == 0)
151 return;
153 /* See if NAME matches one of the patterns we have for the optabs we know
154 about. */
156 for (pindex = 0; pindex < sizeof optabs / sizeof optabs[0]; pindex++)
158 int force_float = 0, force_int = 0;
159 int force_consec = 0;
160 int matches = 1;
162 for (pp = optabs[pindex]; pp[0] != '%' || pp[1] != '('; pp++)
165 for (pp += 2, np = name; matches && ! (pp[0] == '%' && pp[1] == ')');
166 pp++)
168 if (*pp != '%')
170 if (*pp != *np++)
171 break;
173 else
174 switch (*++pp)
176 case 'N':
177 force_consec = 1;
178 break;
179 case 'I':
180 force_int = 1;
181 break;
182 case 'F':
183 force_float = 1;
184 break;
185 case 'c':
186 for (op = 0; op < NUM_RTX_CODE; op++)
188 for (p = rtx_name[op], q = np; *p; p++, q++)
189 if (*p != *q)
190 break;
192 /* We have to be concerned about matching "gt" and
193 missing "gtu", e.g., so verify we have reached the
194 end of thing we are to match. */
195 if (*p == 0 && *q == 0 && rtx_class[op] == '<')
196 break;
199 if (op == NUM_RTX_CODE)
200 matches = 0;
201 else
202 np += strlen (rtx_name[op]);
203 break;
204 case 'a':
205 case 'b':
206 /* This loop will stop at the first prefix match, so
207 look through the modes in reverse order, in case
208 EXTRA_CC_MODES was used and CC is a prefix of the
209 CC modes (as it should be). */
210 for (i = ((int) MAX_MACHINE_MODE) - 1; i >= 0; i--)
212 for (p = mode_name[i], q = np; *p; p++, q++)
213 if (tolower (*p) != *q)
214 break;
216 if (*p == 0
217 && (! force_int || mode_class[i] == MODE_INT)
218 && (! force_float || mode_class[i] == MODE_FLOAT))
219 break;
222 if (i < 0)
223 matches = 0;
224 else if (*pp == 'a')
225 m1 = i, np += strlen (mode_name[i]);
226 else
227 m2 = i, np += strlen (mode_name[i]);
229 force_int = force_float = 0;
230 break;
232 default:
233 abort ();
237 if (matches && pp[0] == '%' && pp[1] == ')'
238 && *np == 0
239 && (! force_consec || (int) GET_MODE_WIDER_MODE(m1) == m2))
240 break;
243 if (pindex == sizeof optabs / sizeof optabs[0])
244 return;
246 /* We found a match. If this pattern is only conditionally present,
247 write out the "if" and two extra blanks. */
249 if (*XSTR (insn, 2) != 0)
250 printf (" if (HAVE_%s)\n ", name);
252 printf (" ");
254 /* Now write out the initialization, making all required substitutions. */
255 for (pp = optabs[pindex]; *pp; pp++)
257 if (*pp != '%')
258 printf ("%c", *pp);
259 else
260 switch (*++pp)
262 case '(': case ')':
263 case 'I': case 'F': case 'N':
264 break;
265 case 'a':
266 for (np = mode_name[m1]; *np; np++)
267 printf ("%c", tolower (*np));
268 break;
269 case 'b':
270 for (np = mode_name[m2]; *np; np++)
271 printf ("%c", tolower (*np));
272 break;
273 case 'A':
274 printf ("%smode", mode_name[m1]);
275 break;
276 case 'B':
277 printf ("%smode", mode_name[m2]);
278 break;
279 case 'c':
280 printf ("%s", rtx_name[op]);
281 break;
282 case 'C':
283 for (np = rtx_name[op]; *np; np++)
284 printf ("%c", toupper (*np));
285 break;
289 printf (";\n");
292 char *
293 xmalloc (size)
294 unsigned size;
296 register char *val = (char *) malloc (size);
298 if (val == 0)
299 fatal ("virtual memory exhausted");
301 return val;
304 char *
305 xrealloc (ptr, size)
306 char *ptr;
307 unsigned size;
309 char *result = (char *) realloc (ptr, size);
310 if (!result)
311 fatal ("virtual memory exhausted");
312 return result;
315 #ifdef HAVE_VPRINTF
316 void
317 fatal VPROTO((char *s, ...))
319 #ifndef ANSI_PROTOTYPES
320 char *s;
321 #endif
322 va_list ap;
324 VA_START (ap, s);
326 #ifndef ANSI_PROTOTYPES
327 s = va_arg (ap, char *);
328 #endif
330 fprintf (stderr, "genopinit: ");
331 vfprintf (stderr, s, ap);
332 va_end (ap);
333 fprintf (stderr, "\n");
334 exit (FATAL_EXIT_CODE);
336 #else /* not HAVE_VPRINTF */
338 void
339 fatal (s, a1, a2)
340 char *s;
342 fprintf (stderr, "genopinit: ");
343 fprintf (stderr, s, a1, a2);
344 fprintf (stderr, "\n");
345 exit (FATAL_EXIT_CODE);
347 #endif /* not HAVE_VPRINTF */
349 /* More 'friendly' abort that prints the line and file.
350 config.h can #define abort fancy_abort if you like that sort of thing. */
352 void
353 fancy_abort ()
355 fatal ("Internal gcc abort.");
359 main (argc, argv)
360 int argc;
361 char **argv;
363 rtx desc;
364 rtx dummy;
365 rtx *insn_ptr;
366 FILE *infile;
367 register int c;
369 obstack_init (rtl_obstack);
371 if (argc <= 1)
372 fatal ("No input file name.");
374 infile = fopen (argv[1], "r");
375 if (infile == 0)
377 perror (argv[1]);
378 exit (FATAL_EXIT_CODE);
381 init_rtl ();
383 printf ("/* Generated automatically by the program `genopinit'\n\
384 from the machine description file `md'. */\n\n");
386 printf ("#include \"config.h\"\n");
387 printf ("#include \"system.h\"\n");
388 printf ("#include \"rtl.h\"\n");
389 printf ("#include \"flags.h\"\n");
390 printf ("#include \"insn-flags.h\"\n");
391 printf ("#include \"insn-codes.h\"\n");
392 printf ("#include \"insn-config.h\"\n");
393 printf ("#include \"recog.h\"\n");
394 printf ("#include \"expr.h\"\n");
395 printf ("#include \"reload.h\"\n\n");
397 printf ("void\ninit_all_optabs ()\n{\n");
399 /* Read the machine description. */
401 while (1)
403 c = read_skip_spaces (infile);
404 if (c == EOF)
405 break;
406 ungetc (c, infile);
408 desc = read_rtx (infile);
409 if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
410 gen_insn (desc);
413 printf ("}\n");
415 fflush (stdout);
416 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
417 /* NOTREACHED */
418 return 0;