]>
Commit | Line | Data |
---|---|---|
af9e4a0c RK |
1 | /* Generate code to initialize optabs from machine description. |
2 | Copyright (C) 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | ||
21 | #include <stdio.h> | |
22 | #include "hconfig.h" | |
23 | #include "rtl.h" | |
24 | #include "obstack.h" | |
25 | #include <ctype.h> | |
26 | ||
27 | static struct obstack obstack; | |
28 | struct obstack *rtl_obstack = &obstack; | |
29 | ||
30 | #define obstack_chunk_alloc xmalloc | |
31 | #define obstack_chunk_free free | |
32 | ||
33 | extern void free (); | |
34 | extern rtx read_rtx (); | |
35 | ||
36 | char *xmalloc (); | |
37 | static void fatal (); | |
38 | void fancy_abort (); | |
39 | ||
40 | /* Many parts of GCC use arrays that are indexed by machine mode and | |
41 | contain the insn codes for pattern in the MD file that perform a given | |
42 | operation on operands of that mode. | |
43 | ||
44 | These patterns are present in the MD file with names that contain | |
45 | the mode(s) used and the name of the operation. This program | |
46 | writes a function `init_all_optabs' that initializes the optabs with | |
47 | all the insn codes of the relevant patterns present in the MD file. | |
48 | ||
49 | This array contains a list of optabs that need to be initialized. Within | |
50 | each string, the name of the pattern to be matched against is delimited | |
51 | with %( and %). In the string, %a and %b are used to match a short mode | |
52 | name (the part of the mode name not including `mode' and converted to | |
53 | lower-case). When writing out the initializer, the entire string is | |
54 | used. %A and %B are replaced with the full name of the mode; %a and %b | |
55 | are replaced with the short form of the name, as above. | |
56 | ||
57 | If %N is present in the pattern, it means the two modes must be consecutive | |
58 | widths in the same mode class (e.g, QImode and HImode). %I means that | |
59 | only integer modes should be considered for the next mode, and %F means | |
60 | that only float modes should be considered. | |
61 | ||
62 | For some optabs, we store the operation by RTL codes. These are only | |
63 | used for comparisons. In that case, %c and %C are the lower-case and | |
64 | upper-case forms of the comparison, respectively. */ | |
65 | ||
66 | char *optabs[] = | |
67 | { "extendtab[(int) %B][(int) %A][0] = CODE_FOR_%(extend%a%b2%)", | |
68 | "extendtab[(int) %B][(int) %A][1] = CODE_FOR_%(zero_extend%a%b2%)", | |
8e15629f | 69 | "fixtab[(int) %A][(int) %B][0] = CODE_FOR_%(fix%F%a%I%b2%)", |
af9e4a0c RK |
70 | "fixtab[(int) %A][(int) %B][1] = CODE_FOR_%(fixuns%F%a%b2%)", |
71 | "fixtrunctab[(int) %A][(int) %B][0] = CODE_FOR_%(fix_trunc%F%a%I%b2%)", | |
72 | "fixtrunctab[(int) %A][(int) %B][1] = CODE_FOR_%(fixuns_trunc%F%a%I%b2%)", | |
73 | "floattab[(int) %B][(int) %A][0] = CODE_FOR_%(float%I%a%F%b2%)", | |
74 | "floattab[(int) %B][(int) %A][1] = CODE_FOR_%(floatuns%I%a%F%b%)", | |
75 | "add_optab->handlers[(int) %A].insn_code = CODE_FOR_%(add%a3%)", | |
76 | "sub_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sub%a3%)", | |
77 | "smul_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mul%a3%)", | |
78 | "smul_widen_optab->handlers[(int) %B].insn_code = CODE_FOR_%(mul%a%b3%)%N", | |
79 | "umul_widen_optab->handlers[(int) %B].insn_code = CODE_FOR_%(umul%a%b3%)%N", | |
80 | "sdiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(div%I%a3%)", | |
81 | "udiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(udiv%I%a3%)", | |
82 | "sdivmod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(divmod%a4%)", | |
83 | "udivmod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(udivmod%a4%)", | |
84 | "smod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mod%a3%)", | |
85 | "umod_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umod%a3%)", | |
86 | "flodiv_optab->handlers[(int) %A].insn_code = CODE_FOR_%(div%F%a3%)", | |
87 | "ftrunc_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ftrunc%F%a2%)", | |
88 | "and_optab->handlers[(int) %A].insn_code = CODE_FOR_%(and%a3%)", | |
89 | "ior_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ior%a3%)", | |
90 | "xor_optab->handlers[(int) %A].insn_code = CODE_FOR_%(xor%a3%)", | |
91 | "ashl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ashl%a3%)", | |
92 | "ashr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ashr%a3%)", | |
93 | "lshl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(lshl%a3%)", | |
94 | "lshr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(lshr%a3%)", | |
95 | "rotl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(rotl%a3%)", | |
96 | "rotr_optab->handlers[(int) %A].insn_code = CODE_FOR_%(rotr%a3%)", | |
97 | "smin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smin%I%a3%)", | |
98 | "smin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(min%F%a3%)", | |
99 | "smax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(smax%I%a3%)", | |
100 | "smax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(max%F%a3%)", | |
101 | "umin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umin%I%a3%)", | |
102 | "umax_optab->handlers[(int) %A].insn_code = CODE_FOR_%(umax%I%a3%)", | |
103 | "neg_optab->handlers[(int) %A].insn_code = CODE_FOR_%(neg%a2%)", | |
104 | "abs_optab->handlers[(int) %A].insn_code = CODE_FOR_%(abs%a2%)", | |
105 | "sqrt_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sqrt%a2%)", | |
106 | "sin_optab->handlers[(int) %A].insn_code = CODE_FOR_%(sin%a2%)", | |
107 | "cos_optab->handlers[(int) %A].insn_code = CODE_FOR_%(cos%a2%)", | |
108 | "strlen_optab->handlers[(int) %A].insn_code = CODE_FOR_%(strlen%a2%)", | |
109 | "one_cmpl_optab->handlers[(int) %A].insn_code = CODE_FOR_%(one_cmpl%a2%)", | |
110 | "ffs_optab->handlers[(int) %A].insn_code = CODE_FOR_%(ffs%a2%)", | |
111 | "mov_optab->handlers[(int) %A].insn_code = CODE_FOR_%(mov%a%)", | |
112 | "movstrict_optab->handlers[(int) %A].insn_code = CODE_FOR_%(movstrict%a%)", | |
113 | "cmp_optab->handlers[(int) %A].insn_code = CODE_FOR_%(cmp%a%)", | |
114 | "tst_optab->handlers[(int) %A].insn_code = CODE_FOR_%(tst%a%)", | |
115 | "bcc_gen_fctn[(int) %C] = gen_%(b%c%)", | |
116 | "setcc_gen_code[(int) %C] = CODE_FOR_%(s%c%)", | |
117 | "reload_in_optab[(int) %A] = CODE_FOR_%(reload_in%a%)", | |
118 | "reload_out_optab[(int) %A] = CODE_FOR_%(reload_out%a%)", | |
119 | "movstr_optab[(int) %A] = CODE_FOR_%(movstr%a%)" }; | |
120 | ||
121 | static void | |
122 | gen_insn (insn) | |
123 | rtx insn; | |
124 | { | |
125 | char *name = XSTR (insn, 0); | |
126 | int m1, m2, op; | |
127 | int pindex; | |
128 | int i; | |
129 | char *np, *pp, *p, *q; | |
130 | struct obstack *obstack_ptr; | |
131 | ||
132 | /* Don't mention instructions whose names are the null string. | |
133 | They are in the machine description just to be recognized. */ | |
134 | if (*name == 0) | |
135 | return; | |
136 | ||
137 | /* See if NAME matches one of the patterns we have for the optabs we know | |
138 | about. */ | |
139 | ||
140 | for (pindex = 0; pindex < sizeof optabs / sizeof optabs[0]; pindex++) | |
141 | { | |
142 | int force_float = 0, force_int = 0; | |
143 | int force_consec = 0; | |
144 | int matches = 1; | |
145 | ||
146 | for (pp = optabs[pindex]; pp[0] != '%' || pp[1] != '('; pp++) | |
147 | ; | |
148 | ||
149 | for (pp += 2, np = name; matches && ! (pp[0] == '%' && pp[1] == ')'); | |
150 | pp++) | |
151 | { | |
152 | if (*pp != '%') | |
153 | { | |
154 | if (*pp != *np++) | |
155 | break; | |
156 | } | |
157 | else | |
158 | switch (*++pp) | |
159 | { | |
160 | case 'N': | |
161 | force_consec = 1; | |
162 | break; | |
163 | case 'I': | |
164 | force_int = 1; | |
165 | break; | |
166 | case 'F': | |
167 | force_float = 1; | |
168 | break; | |
169 | case 'c': | |
170 | for (op = 0; op < NUM_RTX_CODE; op++) | |
171 | { | |
172 | for (p = rtx_name[op], q = np; *p; p++, q++) | |
173 | if (*p != *q) | |
174 | break; | |
175 | ||
176 | /* We have to be concerned about matching "gt" and | |
177 | missing "gtu", e.g., so verify we have reached the | |
178 | end of thing we are to match. We do not have this | |
179 | problem with modes since no mode is a prefix of | |
180 | another. */ | |
181 | if (*p == 0 && *q == 0 && rtx_class[op] == '<') | |
182 | break; | |
183 | } | |
184 | ||
185 | if (op == NUM_RTX_CODE) | |
186 | matches = 0; | |
187 | else | |
188 | np += strlen (rtx_name[op]); | |
189 | break; | |
190 | case 'a': | |
191 | case 'b': | |
192 | for (i = 0; i < (int) MAX_MACHINE_MODE; i++) | |
193 | { | |
194 | for (p = mode_name[i], q = np; *p; p++, q++) | |
195 | if (tolower (*p) != *q) | |
196 | break; | |
197 | ||
198 | if (*p == 0 | |
199 | && (! force_int || mode_class[i] == MODE_INT) | |
200 | && (! force_float || mode_class[i] == MODE_FLOAT)) | |
201 | break; | |
202 | } | |
203 | ||
204 | if (i == (int) MAX_MACHINE_MODE) | |
205 | matches = 0; | |
206 | else if (*pp == 'a') | |
207 | m1 = i, np += strlen (mode_name[i]); | |
208 | else | |
209 | m2 = i, np += strlen (mode_name[i]); | |
210 | ||
211 | force_int = force_float = 0; | |
212 | break; | |
213 | ||
214 | default: | |
215 | abort (); | |
216 | } | |
217 | } | |
218 | ||
219 | if (matches && pp[0] == '%' && pp[1] == ')' | |
220 | && *np == 0 | |
221 | && (! force_consec || (int) mode_wider_mode[m1] == m2)) | |
222 | break; | |
223 | } | |
224 | ||
225 | if (pindex == sizeof optabs / sizeof optabs[0]) | |
226 | return; | |
227 | ||
228 | /* We found a match. If this pattern is only conditionally present, | |
229 | write out the "if" and two extra blanks. */ | |
230 | ||
231 | if (*XSTR (insn, 2) != 0) | |
232 | printf (" if (HAVE_%s)\n ", name); | |
233 | ||
234 | printf (" "); | |
235 | ||
236 | /* Now write out the initialization, making all required substitutions. */ | |
237 | for (pp = optabs[pindex]; *pp; pp++) | |
238 | { | |
239 | if (*pp != '%') | |
240 | printf ("%c", *pp); | |
241 | else | |
242 | switch (*++pp) | |
243 | { | |
244 | case '(': case ')': | |
245 | case 'I': case 'F': case 'N': | |
246 | break; | |
247 | case 'a': | |
248 | for (np = mode_name[m1]; *np; np++) | |
249 | printf ("%c", tolower (*np)); | |
250 | break; | |
251 | case 'b': | |
252 | for (np = mode_name[m2]; *np; np++) | |
253 | printf ("%c", tolower (*np)); | |
254 | break; | |
255 | case 'A': | |
256 | printf ("%smode", mode_name[m1]); | |
257 | break; | |
258 | case 'B': | |
259 | printf ("%smode", mode_name[m2]); | |
260 | break; | |
261 | case 'c': | |
262 | printf ("%s", rtx_name[op]); | |
263 | break; | |
264 | case 'C': | |
265 | for (np = rtx_name[op]; *np; np++) | |
266 | printf ("%c", toupper (*np)); | |
267 | break; | |
268 | } | |
269 | } | |
270 | ||
271 | printf (";\n"); | |
272 | } | |
273 | \f | |
274 | char * | |
275 | xmalloc (size) | |
276 | unsigned size; | |
277 | { | |
278 | register char *val = (char *) malloc (size); | |
279 | ||
280 | if (val == 0) | |
281 | fatal ("virtual memory exhausted"); | |
282 | ||
283 | return val; | |
284 | } | |
285 | ||
286 | char * | |
287 | xrealloc (ptr, size) | |
288 | char *ptr; | |
289 | unsigned size; | |
290 | { | |
291 | char *result = (char *) realloc (ptr, size); | |
292 | if (!result) | |
293 | fatal ("virtual memory exhausted"); | |
294 | return result; | |
295 | } | |
296 | ||
297 | static void | |
298 | fatal (s, a1, a2) | |
299 | char *s; | |
300 | { | |
301 | fprintf (stderr, "genopinit: "); | |
302 | fprintf (stderr, s, a1, a2); | |
303 | fprintf (stderr, "\n"); | |
304 | exit (FATAL_EXIT_CODE); | |
305 | } | |
306 | ||
307 | /* More 'friendly' abort that prints the line and file. | |
308 | config.h can #define abort fancy_abort if you like that sort of thing. */ | |
309 | ||
310 | void | |
311 | fancy_abort () | |
312 | { | |
313 | fatal ("Internal gcc abort."); | |
314 | } | |
315 | \f | |
316 | int | |
317 | main (argc, argv) | |
318 | int argc; | |
319 | char **argv; | |
320 | { | |
321 | rtx desc; | |
322 | rtx dummy; | |
323 | rtx *insn_ptr; | |
324 | FILE *infile; | |
325 | register int c; | |
326 | ||
327 | obstack_init (rtl_obstack); | |
328 | ||
329 | if (argc <= 1) | |
330 | fatal ("No input file name."); | |
331 | ||
332 | infile = fopen (argv[1], "r"); | |
333 | if (infile == 0) | |
334 | { | |
335 | perror (argv[1]); | |
336 | exit (FATAL_EXIT_CODE); | |
337 | } | |
338 | ||
339 | init_rtl (); | |
340 | ||
341 | printf ("/* Generated automatically by the program `genopinit'\n\ | |
342 | from the machine description file `md'. */\n\n"); | |
343 | ||
344 | printf ("#include \"config.h\"\n"); | |
345 | printf ("#include \"rtl.h\"\n"); | |
346 | printf ("#include \"flags.h\"\n"); | |
347 | printf ("#include \"insn-flags.h\"\n"); | |
348 | printf ("#include \"insn-codes.h\"\n"); | |
349 | printf ("#include \"insn-config.h\"\n"); | |
350 | printf ("#include \"recog.h\"\n"); | |
351 | printf ("#include \"expr.h\"\n"); | |
352 | printf ("#include \"reload.h\"\n\n"); | |
353 | ||
354 | printf ("void\ninit_all_optabs ()\n{\n"); | |
355 | ||
356 | /* Read the machine description. */ | |
357 | ||
358 | while (1) | |
359 | { | |
360 | c = read_skip_spaces (infile); | |
361 | if (c == EOF) | |
362 | break; | |
363 | ungetc (c, infile); | |
364 | ||
365 | desc = read_rtx (infile); | |
366 | if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND) | |
367 | gen_insn (desc); | |
368 | } | |
369 | ||
370 | printf ("}\n"); | |
371 | ||
372 | fflush (stdout); | |
373 | exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE); | |
374 | /* NOTREACHED */ | |
375 | return 0; | |
376 | } |