1 /* Generate code to initialize optabs from machine description.
2 Copyright (C) 1993, 94-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)
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. */
32 static struct obstack obstack;
33 struct obstack *rtl_obstack = &obstack;
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
38 char *xmalloc PROTO((unsigned));
39 static void fatal PVPROTO ((char *, ...)) ATTRIBUTE_PRINTF_1;
40 void fancy_abort PROTO((void));
42 /* Many parts of GCC use arrays that are indexed by machine mode and
43 contain the insn codes for pattern in the MD file that perform a given
44 operation on operands of that mode.
46 These patterns are present in the MD file with names that contain
47 the mode(s) used and the name of the operation. This program
48 writes a function `init_all_optabs' that initializes the optabs with
49 all the insn codes of the relevant patterns present in the MD file.
51 This array contains a list of optabs that need to be initialized. Within
52 each string, the name of the pattern to be matched against is delimited
53 with %( and %). In the string, %a and %b are used to match a short mode
54 name (the part of the mode name not including `mode' and converted to
55 lower-case). When writing out the initializer, the entire string is
56 used. %A and %B are replaced with the full name of the mode; %a and %b
57 are replaced with the short form of the name, as above.
59 If %N is present in the pattern, it means the two modes must be consecutive
60 widths in the same mode class (e.g, QImode and HImode). %I means that
61 only integer modes should be considered for the next mode, and %F means
62 that only float modes should be considered.
64 For some optabs, we store the operation by RTL codes. These are only
65 used for comparisons. In that case, %c and %C are the lower-case and
66 upper-case forms of the comparison, respectively. */
68 /* The reason we use \% is to avoid sequences of the form %-capletter-%
69 which SCCS treats as magic. This gets warnings which you should ignore. */
72 { "extendtab[(int) %B][(int) %A][0] = CODE_FOR_%(extend%a\%b2%)",
73 "extendtab[(int) %B][(int) %A][1] = CODE_FOR_%(zero_extend%a\%b2%)",
74 "fixtab[(int) %A][(int) %B][0] = CODE_FOR_%(fix%F\%a%I\%b2%)",
75 "fixtab[(int) %A][(int) %B][1] = CODE_FOR_%(fixuns%F\%a%b2%)",
76 "fixtrunctab[(int) %A][(int) %B][0] = CODE_FOR_%(fix_trunc%F\%a%I\%b2%)",
77 "fixtrunctab[(int) %A][(int) %B][1] = CODE_FOR_%(fixuns_trunc%F\%a%I\%b2%)",
78 "floattab[(int) %B][(int) %A][0] = CODE_FOR_%(float%I\%a%F\%b2%)",
79 "floattab[(int) %B][(int) %A][1] = CODE_FOR_%(floatuns%I\%a%F\%b2%)",
80 "add_optab->handlers[(int) %A].insn_code = CODE_FOR_%(add%a3%)",
81 "sub_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sub%a3%)",
82 "smul_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mul%a3%)",
83 "umul_highpart_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umul%a3_highpart%)",
84 "smul_highpart_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smul%a3_highpart%)",
85 "smul_widen_optab->handlers[(int) %B].insn_code = CODE_FOR_%(mul%a%b3%)%N",
86 "umul_widen_optab->handlers[(int) %B].insn_code = CODE_FOR_%(umul%a%b3%)%N",
87 "sdiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(div%I\%a3%)",
88 "udiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(udiv%I\%a3%)",
89 "sdivmod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(divmod%a4%)",
90 "udivmod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(udivmod%a4%)",
91 "smod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mod%a3%)",
92 "umod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umod%a3%)",
93 "flodiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(div%F\%a3%)",
94 "ftrunc_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ftrunc%F\%a2%)",
95 "and_optab->handlers[(int) %A].insn_code = CODE_FOR_%(and%a3%)",
96 "ior_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ior%a3%)",
97 "xor_optab->handlers[(int) %A].insn_code = CODE_FOR_%(xor%a3%)",
98 "ashl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ashl%a3%)",
99 "ashr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ashr%a3%)",
100 "lshr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(lshr%a3%)",
101 "rotl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(rotl%a3%)",
102 "rotr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(rotr%a3%)",
103 "smin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smin%I\%a3%)",
104 "smin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(min%F\%a3%)",
105 "smax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smax%I\%a3%)",
106 "smax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(max%F\%a3%)",
107 "umin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umin%I\%a3%)",
108 "umax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umax%I\%a3%)",
109 "neg_optab->handlers[(int) %A].insn_code = CODE_FOR_%(neg%a2%)",
110 "abs_optab->handlers[(int) %A].insn_code = CODE_FOR_%(abs%a2%)",
111 "sqrt_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sqrt%a2%)",
112 "sin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sin%a2%)",
113 "cos_optab->handlers[(int) %A].insn_code = CODE_FOR_%(cos%a2%)",
114 "strlen_optab->handlers[(int) %A].insn_code = CODE_FOR_%(strlen%a%)",
115 "one_cmpl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(one_cmpl%a2%)",
116 "ffs_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ffs%a2%)",
117 "mov_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mov%a%)",
118 "movstrict_optab->handlers[(int) %A].insn_code = CODE_FOR_%(movstrict%a%)",
119 "cmp_optab->handlers[(int) %A].insn_code = CODE_FOR_%(cmp%a%)",
120 "tst_optab->handlers[(int) %A].insn_code = CODE_FOR_%(tst%a%)",
121 "bcc_gen_fctn[(int) %C] = gen_%(b%c%)",
122 "setcc_gen_code[(int) %C] = CODE_FOR_%(s%c%)",
123 "movcc_gen_code[(int) %A] = CODE_FOR_%(mov%acc%)",
124 "reload_in_optab[(int) %A] = CODE_FOR_%(reload_in%a%)",
125 "reload_out_optab[(int) %A] = CODE_FOR_%(reload_out%a%)",
126 "movstr_optab[(int) %A] = CODE_FOR_%(movstr%a%)",
127 "clrstr_optab[(int) %A] = CODE_FOR_%(clrstr%a%)" };
129 /* Allow linking with print-rtl.c. */
130 char **insn_name_ptr;
132 static void gen_insn PROTO((rtx));
138 char *name = XSTR (insn, 0);
142 char *np, *pp, *p, *q;
144 /* Don't mention instructions whose names are the null string.
145 They are in the machine description just to be recognized. */
149 /* See if NAME matches one of the patterns we have for the optabs we know
152 for (pindex = 0; pindex < sizeof optabs / sizeof optabs[0]; pindex++)
154 int force_float = 0, force_int = 0;
155 int force_consec = 0;
158 for (pp = optabs[pindex]; pp[0] != '%' || pp[1] != '('; pp++)
161 for (pp += 2, np = name; matches && ! (pp[0] == '%' && pp[1] == ')');
182 for (op = 0; op < NUM_RTX_CODE; op++)
184 for (p = rtx_name[op], q = np; *p; p++, q++)
188 /* We have to be concerned about matching "gt" and
189 missing "gtu", e.g., so verify we have reached the
190 end of thing we are to match. */
191 if (*p == 0 && *q == 0 && rtx_class[op] == '<')
195 if (op == NUM_RTX_CODE)
198 np += strlen (rtx_name[op]);
202 /* This loop will stop at the first prefix match, so
203 look through the modes in reverse order, in case
204 EXTRA_CC_MODES was used and CC is a prefix of the
205 CC modes (as it should be). */
206 for (i = ((int) MAX_MACHINE_MODE) - 1; i >= 0; i--)
208 for (p = mode_name[i], q = np; *p; p++, q++)
209 if (tolower (*p) != *q)
213 && (! force_int || mode_class[i] == MODE_INT)
214 && (! force_float || mode_class[i] == MODE_FLOAT))
221 m1 = i, np += strlen (mode_name[i]);
223 m2 = i, np += strlen (mode_name[i]);
225 force_int = force_float = 0;
233 if (matches && pp[0] == '%' && pp[1] == ')'
235 && (! force_consec || (int) GET_MODE_WIDER_MODE(m1) == m2))
239 if (pindex == sizeof optabs / sizeof optabs[0])
242 /* We found a match. If this pattern is only conditionally present,
243 write out the "if" and two extra blanks. */
245 if (*XSTR (insn, 2) != 0)
246 printf (" if (HAVE_%s)\n ", name);
250 /* Now write out the initialization, making all required substitutions. */
251 for (pp = optabs[pindex]; *pp; pp++)
259 case 'I': case 'F': case 'N':
262 for (np = mode_name[m1]; *np; np++)
263 printf ("%c", tolower (*np));
266 for (np = mode_name[m2]; *np; np++)
267 printf ("%c", tolower (*np));
270 printf ("%smode", mode_name[m1]);
273 printf ("%smode", mode_name[m2]);
276 printf ("%s", rtx_name[op]);
279 for (np = rtx_name[op]; *np; np++)
280 printf ("%c", toupper (*np));
292 register char *val = (char *) malloc (size);
295 fatal ("virtual memory exhausted");
305 char *result = (char *) realloc (ptr, size);
307 fatal ("virtual memory exhausted");
312 fatal VPROTO ((char *format, ...))
319 VA_START (ap, format);
322 format = va_arg (ap, char *);
325 fprintf (stderr, "genopinit: ");
326 vfprintf (stderr, format, ap);
328 fprintf (stderr, "\n");
329 exit (FATAL_EXIT_CODE);
332 /* More 'friendly' abort that prints the line and file.
333 config.h can #define abort fancy_abort if you like that sort of thing. */
338 fatal ("Internal gcc abort.");
350 obstack_init (rtl_obstack);
353 fatal ("No input file name.");
355 infile = fopen (argv[1], "r");
359 exit (FATAL_EXIT_CODE);
364 printf ("/* Generated automatically by the program `genopinit'\n\
365 from the machine description file `md'. */\n\n");
367 printf ("#include \"config.h\"\n");
368 printf ("#include \"system.h\"\n");
369 printf ("#include \"rtl.h\"\n");
370 printf ("#include \"flags.h\"\n");
371 printf ("#include \"insn-flags.h\"\n");
372 printf ("#include \"insn-codes.h\"\n");
373 printf ("#include \"insn-config.h\"\n");
374 printf ("#include \"recog.h\"\n");
375 printf ("#include \"expr.h\"\n");
376 printf ("#include \"reload.h\"\n\n");
378 printf ("void\ninit_all_optabs ()\n{\n");
380 /* Read the machine description. */
384 c = read_skip_spaces (infile);
389 desc = read_rtx (infile);
390 if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
397 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);