cp/:
[official-gcc.git] / gcc / genextract.c
blobc414891bb6bfa6788696cd5c97488e89c85fa1c4
1 /* Generate code from machine description to extract operands from insn as rtl.
2 Copyright (C) 1987, 1991, 1992, 1993, 1997, 1998, 1999, 2000, 2003,
3 2004, 2005, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "gensupport.h"
30 #include "vec.h"
31 #include "vecprim.h"
33 /* This structure contains all the information needed to describe one
34 set of extractions methods. Each method may be used by more than
35 one pattern if the operands are in the same place.
37 The string for each operand describes that path to the operand and
38 contains `0' through `9' when going into an expression and `a' through
39 `z' when going into a vector. We assume here that only the first operand
40 of an rtl expression is a vector. genrecog.c makes the same assumption
41 (and uses the same representation) and it is currently true. */
43 typedef char *locstr;
45 struct extraction
47 unsigned int op_count;
48 unsigned int dup_count;
49 locstr *oplocs;
50 locstr *duplocs;
51 int *dupnums;
52 struct code_ptr *insns;
53 struct extraction *next;
56 /* Holds a single insn code that uses an extraction method. */
57 struct code_ptr
59 int insn_code;
60 struct code_ptr *next;
63 /* All extractions needed for this machine description. */
64 static struct extraction *extractions;
66 /* All insn codes for old-style peepholes. */
67 static struct code_ptr *peepholes;
69 /* This structure is used by gen_insn and walk_rtx to accumulate the
70 data that will be used to produce an extractions structure. */
72 DEF_VEC_P(locstr);
73 DEF_VEC_ALLOC_P(locstr,heap);
75 struct accum_extract
77 VEC(locstr,heap) *oplocs;
78 VEC(locstr,heap) *duplocs;
79 VEC(int,heap) *dupnums;
80 VEC(char,heap) *pathstr;
83 int line_no;
85 /* Forward declarations. */
86 static void walk_rtx (rtx, struct accum_extract *);
88 static void
89 gen_insn (rtx insn, int insn_code_number)
91 int i;
92 unsigned int op_count, dup_count, j;
93 struct extraction *p;
94 struct code_ptr *link;
95 struct accum_extract acc;
97 acc.oplocs = VEC_alloc (locstr,heap, 10);
98 acc.duplocs = VEC_alloc (locstr,heap, 10);
99 acc.dupnums = VEC_alloc (int,heap, 10);
100 acc.pathstr = VEC_alloc (char,heap, 20);
102 /* Walk the insn's pattern, remembering at all times the path
103 down to the walking point. */
105 if (XVECLEN (insn, 1) == 1)
106 walk_rtx (XVECEXP (insn, 1, 0), &acc);
107 else
108 for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
110 VEC_safe_push (char,heap, acc.pathstr, 'a' + i);
111 walk_rtx (XVECEXP (insn, 1, i), &acc);
112 VEC_pop (char, acc.pathstr);
115 link = XNEW (struct code_ptr);
116 link->insn_code = insn_code_number;
118 /* See if we find something that already had this extraction method. */
120 op_count = VEC_length (locstr, acc.oplocs);
121 dup_count = VEC_length (locstr, acc.duplocs);
122 gcc_assert (dup_count == VEC_length (int, acc.dupnums));
124 for (p = extractions; p; p = p->next)
126 if (p->op_count != op_count || p->dup_count != dup_count)
127 continue;
129 for (j = 0; j < op_count; j++)
131 char *a = p->oplocs[j];
132 char *b = VEC_index (locstr, acc.oplocs, j);
133 if (a != b && (!a || !b || strcmp (a, b)))
134 break;
137 if (j != op_count)
138 continue;
140 for (j = 0; j < dup_count; j++)
141 if (p->dupnums[j] != VEC_index (int, acc.dupnums, j)
142 || strcmp (p->duplocs[j], VEC_index (locstr, acc.duplocs, j)))
143 break;
145 if (j != dup_count)
146 continue;
148 /* This extraction is the same as ours. Just link us in. */
149 link->next = p->insns;
150 p->insns = link;
151 goto done;
154 /* Otherwise, make a new extraction method. We stash the arrays
155 after the extraction structure in memory. */
157 p = XNEWVAR (struct extraction, sizeof (struct extraction)
158 + op_count*sizeof (char *)
159 + dup_count*sizeof (char *)
160 + dup_count*sizeof (int));
161 p->op_count = op_count;
162 p->dup_count = dup_count;
163 p->next = extractions;
164 extractions = p;
165 p->insns = link;
166 link->next = 0;
168 p->oplocs = (char **)((char *)p + sizeof (struct extraction));
169 p->duplocs = p->oplocs + op_count;
170 p->dupnums = (int *)(p->duplocs + dup_count);
172 memcpy(p->oplocs, VEC_address(locstr,acc.oplocs), op_count*sizeof(locstr));
173 memcpy(p->duplocs, VEC_address(locstr,acc.duplocs), dup_count*sizeof(locstr));
174 memcpy(p->dupnums, VEC_address(int, acc.dupnums), dup_count*sizeof(int));
176 done:
177 VEC_free (locstr,heap, acc.oplocs);
178 VEC_free (locstr,heap, acc.duplocs);
179 VEC_free (int,heap, acc.dupnums);
180 VEC_free (char,heap, acc.pathstr);
183 /* Helper subroutine of walk_rtx: given a VEC(locstr), an index, and a
184 string, insert the string at the index, which should either already
185 exist and be NULL, or not yet exist within the vector. In the latter
186 case the vector is enlarged as appropriate. */
187 static void
188 VEC_safe_set_locstr (VEC(locstr,heap) **vp, unsigned int ix, char *str)
190 if (ix < VEC_length (locstr, *vp))
192 if (VEC_index (locstr, *vp, ix))
194 message_with_line (line_no, "repeated operand number %d", ix);
195 have_error = 1;
197 else
198 VEC_replace (locstr, *vp, ix, str);
200 else
202 while (ix > VEC_length (locstr, *vp))
203 VEC_safe_push (locstr, heap, *vp, 0);
204 VEC_safe_push (locstr, heap, *vp, str);
208 /* Another helper subroutine of walk_rtx: given a VEC(char), convert it
209 to a NUL-terminated string in malloc memory. */
210 static char *
211 VEC_char_to_string (VEC(char,heap) *v)
213 size_t n = VEC_length (char, v);
214 char *s = XNEWVEC (char, n + 1);
215 memcpy (s, VEC_address (char, v), n);
216 s[n] = '\0';
217 return s;
220 static void
221 walk_rtx (rtx x, struct accum_extract *acc)
223 RTX_CODE code;
224 int i, len, base;
225 const char *fmt;
227 if (x == 0)
228 return;
230 code = GET_CODE (x);
231 switch (code)
233 case PC:
234 case CC0:
235 case CONST_INT:
236 case SYMBOL_REF:
237 return;
239 case MATCH_OPERAND:
240 case MATCH_SCRATCH:
241 VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
242 VEC_char_to_string (acc->pathstr));
243 break;
245 case MATCH_OPERATOR:
246 case MATCH_PARALLEL:
247 VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
248 VEC_char_to_string (acc->pathstr));
250 base = (code == MATCH_OPERATOR ? '0' : 'a');
251 for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
253 VEC_safe_push (char,heap, acc->pathstr, base + i);
254 walk_rtx (XVECEXP (x, 2, i), acc);
255 VEC_pop (char, acc->pathstr);
257 return;
259 case MATCH_DUP:
260 case MATCH_PAR_DUP:
261 case MATCH_OP_DUP:
262 VEC_safe_push (locstr,heap, acc->duplocs,
263 VEC_char_to_string (acc->pathstr));
264 VEC_safe_push (int,heap, acc->dupnums, XINT (x, 0));
266 if (code == MATCH_DUP)
267 break;
269 base = (code == MATCH_OP_DUP ? '0' : 'a');
270 for (i = XVECLEN (x, 1) - 1; i >= 0; i--)
272 VEC_safe_push (char,heap, acc->pathstr, base + i);
273 walk_rtx (XVECEXP (x, 1, i), acc);
274 VEC_pop (char, acc->pathstr);
276 return;
278 default:
279 break;
282 fmt = GET_RTX_FORMAT (code);
283 len = GET_RTX_LENGTH (code);
284 for (i = 0; i < len; i++)
286 if (fmt[i] == 'e' || fmt[i] == 'u')
288 VEC_safe_push (char,heap, acc->pathstr, '0' + i);
289 walk_rtx (XEXP (x, i), acc);
290 VEC_pop (char, acc->pathstr);
292 else if (fmt[i] == 'E')
294 int j;
295 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
297 VEC_safe_push (char,heap, acc->pathstr, 'a' + j);
298 walk_rtx (XVECEXP (x, i, j), acc);
299 VEC_pop (char, acc->pathstr);
305 /* Given a PATH, representing a path down the instruction's
306 pattern from the root to a certain point, output code to
307 evaluate to the rtx at that point. */
309 static void
310 print_path (const char *path)
312 int len = strlen (path);
313 int i;
315 if (len == 0)
317 /* Don't emit "pat", since we may try to take the address of it,
318 which isn't what is intended. */
319 fputs ("PATTERN (insn)", stdout);
320 return;
323 /* We first write out the operations (XEXP or XVECEXP) in reverse
324 order, then write "pat", then the indices in forward order. */
326 for (i = len - 1; i >= 0 ; i--)
328 if (ISLOWER (path[i]))
329 fputs ("XVECEXP (", stdout);
330 else if (ISDIGIT (path[i]))
331 fputs ("XEXP (", stdout);
332 else
333 gcc_unreachable ();
336 fputs ("pat", stdout);
338 for (i = 0; i < len; i++)
340 if (ISLOWER (path[i]))
341 printf (", 0, %d)", path[i] - 'a');
342 else if (ISDIGIT(path[i]))
343 printf (", %d)", path[i] - '0');
344 else
345 gcc_unreachable ();
349 static void
350 print_header (void)
352 /* N.B. Code below avoids putting squiggle braces in column 1 inside
353 a string, because this confuses some editors' syntax highlighting
354 engines. */
356 puts ("\
357 /* Generated automatically by the program `genextract'\n\
358 from the machine description file `md'. */\n\
360 #include \"config.h\"\n\
361 #include \"system.h\"\n\
362 #include \"coretypes.h\"\n\
363 #include \"tm.h\"\n\
364 #include \"rtl.h\"\n\
365 #include \"insn-config.h\"\n\
366 #include \"recog.h\"\n\
367 #include \"toplev.h\"\n\
369 /* This variable is used as the \"location\" of any missing operand\n\
370 whose numbers are skipped by a given pattern. */\n\
371 static rtx junk ATTRIBUTE_UNUSED;\n");
373 puts ("\
374 void\n\
375 insn_extract (rtx insn)\n{\n\
376 rtx *ro = recog_data.operand;\n\
377 rtx **ro_loc = recog_data.operand_loc;\n\
378 rtx pat = PATTERN (insn);\n\
379 int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
381 #ifdef ENABLE_CHECKING\n\
382 memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
383 memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
384 #endif\n");
386 puts ("\
387 switch (INSN_CODE (insn))\n\
388 {\n\
389 default:\n\
390 /* Control reaches here if insn_extract has been called with an\n\
391 unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
392 corresponds to a DEFINE_EXPAND in the machine description;\n\
393 either way, a bug. */\n\
394 if (INSN_CODE (insn) < 0)\n\
395 fatal_insn (\"unrecognizable insn:\", insn);\n\
396 else\n\
397 fatal_insn (\"insn with invalid code number:\", insn);\n");
401 main (int argc, char **argv)
403 rtx desc;
404 unsigned int i;
405 struct extraction *p;
406 struct code_ptr *link;
407 const char *name;
408 int insn_code_number;
410 progname = "genextract";
412 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
413 return (FATAL_EXIT_CODE);
415 /* Read the machine description. */
417 while ((desc = read_md_rtx (&line_no, &insn_code_number)) != NULL)
419 if (GET_CODE (desc) == DEFINE_INSN)
420 gen_insn (desc, insn_code_number);
422 else if (GET_CODE (desc) == DEFINE_PEEPHOLE)
424 struct code_ptr *link = XNEW (struct code_ptr);
426 link->insn_code = insn_code_number;
427 link->next = peepholes;
428 peepholes = link;
432 if (have_error)
433 return FATAL_EXIT_CODE;
435 print_header ();
437 /* Write out code to handle peepholes and the insn_codes that it should
438 be called for. */
439 if (peepholes)
441 for (link = peepholes; link; link = link->next)
442 printf (" case %d:\n", link->insn_code);
444 /* The vector in the insn says how many operands it has.
445 And all it contains are operands. In fact, the vector was
446 created just for the sake of this function. We need to set the
447 location of the operands for sake of simplifications after
448 extraction, like eliminating subregs. */
449 puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
450 " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
451 " break;\n");
454 /* Write out all the ways to extract insn operands. */
455 for (p = extractions; p; p = p->next)
457 for (link = p->insns; link; link = link->next)
459 i = link->insn_code;
460 name = get_insn_name (i);
461 if (name)
462 printf (" case %d: /* %s */\n", i, name);
463 else
464 printf (" case %d:\n", i);
467 for (i = 0; i < p->op_count; i++)
469 if (p->oplocs[i] == 0)
471 printf (" ro[%d] = const0_rtx;\n", i);
472 printf (" ro_loc[%d] = &junk;\n", i);
474 else
476 printf (" ro[%d] = *(ro_loc[%d] = &", i, i);
477 print_path (p->oplocs[i]);
478 puts (");");
482 for (i = 0; i < p->dup_count; i++)
484 printf (" recog_data.dup_loc[%d] = &", i);
485 print_path (p->duplocs[i]);
486 puts (";");
487 printf (" recog_data.dup_num[%d] = %d;\n", i, p->dupnums[i]);
490 puts (" break;\n");
493 puts (" }\n}");
494 fflush (stdout);
495 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);