2006-03-25 Thomas Koenig <Thomas.Koenig@online.de>
[official-gcc.git] / gcc / genextract.c
blob06ed44ac4475c0fc88dc8a216250c6d065aa47b8
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
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 #include "bconfig.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "errors.h"
30 #include "gensupport.h"
31 #include "vec.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_I(int);
73 DEF_VEC_I(char);
74 DEF_VEC_P(locstr);
75 DEF_VEC_ALLOC_I(int,heap);
76 DEF_VEC_ALLOC_I(char,heap);
77 DEF_VEC_ALLOC_P(locstr,heap);
79 struct accum_extract
81 VEC(locstr,heap) *oplocs;
82 VEC(locstr,heap) *duplocs;
83 VEC(int,heap) *dupnums;
84 VEC(char,heap) *pathstr;
87 /* Forward declarations. */
88 static void walk_rtx (rtx, struct accum_extract *);
90 static void
91 gen_insn (rtx insn, int insn_code_number)
93 int i;
94 unsigned int op_count, dup_count, j;
95 struct extraction *p;
96 struct code_ptr *link;
97 struct accum_extract acc;
99 acc.oplocs = VEC_alloc (locstr,heap, 10);
100 acc.duplocs = VEC_alloc (locstr,heap, 10);
101 acc.dupnums = VEC_alloc (int,heap, 10);
102 acc.pathstr = VEC_alloc (char,heap, 20);
104 /* Walk the insn's pattern, remembering at all times the path
105 down to the walking point. */
107 if (XVECLEN (insn, 1) == 1)
108 walk_rtx (XVECEXP (insn, 1, 0), &acc);
109 else
110 for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
112 VEC_safe_push (char,heap, acc.pathstr, 'a' + i);
113 walk_rtx (XVECEXP (insn, 1, i), &acc);
114 VEC_pop (char, acc.pathstr);
117 link = XNEW (struct code_ptr);
118 link->insn_code = insn_code_number;
120 /* See if we find something that already had this extraction method. */
122 op_count = VEC_length (locstr, acc.oplocs);
123 dup_count = VEC_length (locstr, acc.duplocs);
124 gcc_assert (dup_count == VEC_length (int, acc.dupnums));
126 for (p = extractions; p; p = p->next)
128 if (p->op_count != op_count || p->dup_count != dup_count)
129 continue;
131 for (j = 0; j < op_count; j++)
133 char *a = p->oplocs[j];
134 char *b = VEC_index (locstr, acc.oplocs, j);
135 if (a != b && (!a || !b || strcmp (a, b)))
136 break;
139 if (j != op_count)
140 continue;
142 for (j = 0; j < dup_count; j++)
143 if (p->dupnums[j] != VEC_index (int, acc.dupnums, j)
144 || strcmp (p->duplocs[j], VEC_index (locstr, acc.duplocs, j)))
145 break;
147 if (j != dup_count)
148 continue;
150 /* This extraction is the same as ours. Just link us in. */
151 link->next = p->insns;
152 p->insns = link;
153 goto done;
156 /* Otherwise, make a new extraction method. We stash the arrays
157 after the extraction structure in memory. */
159 p = xmalloc (sizeof (struct extraction)
160 + op_count*sizeof (char *)
161 + dup_count*sizeof (char *)
162 + dup_count*sizeof (int));
163 p->op_count = op_count;
164 p->dup_count = dup_count;
165 p->next = extractions;
166 extractions = p;
167 p->insns = link;
168 link->next = 0;
170 p->oplocs = (char **)((char *)p + sizeof (struct extraction));
171 p->duplocs = p->oplocs + op_count;
172 p->dupnums = (int *)(p->duplocs + dup_count);
174 memcpy(p->oplocs, VEC_address(locstr,acc.oplocs), op_count*sizeof(locstr));
175 memcpy(p->duplocs, VEC_address(locstr,acc.duplocs), dup_count*sizeof(locstr));
176 memcpy(p->dupnums, VEC_address(int, acc.dupnums), dup_count*sizeof(int));
178 done:
179 VEC_free (locstr,heap, acc.oplocs);
180 VEC_free (locstr,heap, acc.duplocs);
181 VEC_free (int,heap, acc.dupnums);
182 VEC_free (char,heap, acc.pathstr);
185 /* Helper subroutine of walk_rtx: given a VEC(locstr), an index, and a
186 string, insert the string at the index, which should either already
187 exist and be NULL, or not yet exist within the vector. In the latter
188 case the vector is enlarged as appropriate. */
189 static void
190 VEC_safe_set_locstr (VEC(locstr,heap) **vp, unsigned int ix, char *str)
192 if (ix < VEC_length (locstr, *vp))
194 gcc_assert (VEC_index (locstr, *vp, ix) == 0);
195 VEC_replace (locstr, *vp, ix, str);
197 else
199 while (ix > VEC_length (locstr, *vp))
200 VEC_safe_push (locstr, heap, *vp, 0);
201 VEC_safe_push (locstr, heap, *vp, str);
205 /* Another helper subroutine of walk_rtx: given a VEC(char), convert it
206 to a NUL-terminated string in malloc memory. */
207 static char *
208 VEC_char_to_string (VEC(char,heap) *v)
210 size_t n = VEC_length (char, v);
211 char *s = XNEWVEC (char, n + 1);
212 memcpy (s, VEC_address (char, v), n);
213 s[n] = '\0';
214 return s;
217 static void
218 walk_rtx (rtx x, struct accum_extract *acc)
220 RTX_CODE code;
221 int i, len, base;
222 const char *fmt;
224 if (x == 0)
225 return;
227 code = GET_CODE (x);
228 switch (code)
230 case PC:
231 case CC0:
232 case CONST_INT:
233 case SYMBOL_REF:
234 return;
236 case MATCH_OPERAND:
237 case MATCH_SCRATCH:
238 VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
239 VEC_char_to_string (acc->pathstr));
240 break;
242 case MATCH_OPERATOR:
243 case MATCH_PARALLEL:
244 VEC_safe_set_locstr (&acc->oplocs, XINT (x, 0),
245 VEC_char_to_string (acc->pathstr));
247 base = (code == MATCH_OPERATOR ? '0' : 'a');
248 for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
250 VEC_safe_push (char,heap, acc->pathstr, base + i);
251 walk_rtx (XVECEXP (x, 2, i), acc);
252 VEC_pop (char, acc->pathstr);
254 return;
256 case MATCH_DUP:
257 case MATCH_PAR_DUP:
258 case MATCH_OP_DUP:
259 VEC_safe_push (locstr,heap, acc->duplocs,
260 VEC_char_to_string (acc->pathstr));
261 VEC_safe_push (int,heap, acc->dupnums, XINT (x, 0));
263 if (code == MATCH_DUP)
264 break;
266 base = (code == MATCH_OP_DUP ? '0' : 'a');
267 for (i = XVECLEN (x, 1) - 1; i >= 0; i--)
269 VEC_safe_push (char,heap, acc->pathstr, base + i);
270 walk_rtx (XVECEXP (x, 1, i), acc);
271 VEC_pop (char, acc->pathstr);
273 return;
275 default:
276 break;
279 fmt = GET_RTX_FORMAT (code);
280 len = GET_RTX_LENGTH (code);
281 for (i = 0; i < len; i++)
283 if (fmt[i] == 'e' || fmt[i] == 'u')
285 VEC_safe_push (char,heap, acc->pathstr, '0' + i);
286 walk_rtx (XEXP (x, i), acc);
287 VEC_pop (char, acc->pathstr);
289 else if (fmt[i] == 'E')
291 int j;
292 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
294 VEC_safe_push (char,heap, acc->pathstr, 'a' + j);
295 walk_rtx (XVECEXP (x, i, j), acc);
296 VEC_pop (char, acc->pathstr);
302 /* Given a PATH, representing a path down the instruction's
303 pattern from the root to a certain point, output code to
304 evaluate to the rtx at that point. */
306 static void
307 print_path (const char *path)
309 int len = strlen (path);
310 int i;
312 if (len == 0)
314 /* Don't emit "pat", since we may try to take the address of it,
315 which isn't what is intended. */
316 fputs ("PATTERN (insn)", stdout);
317 return;
320 /* We first write out the operations (XEXP or XVECEXP) in reverse
321 order, then write "pat", then the indices in forward order. */
323 for (i = len - 1; i >= 0 ; i--)
325 if (ISLOWER (path[i]))
326 fputs ("XVECEXP (", stdout);
327 else if (ISDIGIT (path[i]))
328 fputs ("XEXP (", stdout);
329 else
330 gcc_unreachable ();
333 fputs ("pat", stdout);
335 for (i = 0; i < len; i++)
337 if (ISLOWER (path[i]))
338 printf (", 0, %d)", path[i] - 'a');
339 else if (ISDIGIT(path[i]))
340 printf (", %d)", path[i] - '0');
341 else
342 gcc_unreachable ();
346 static void
347 print_header (void)
349 /* N.B. Code below avoids putting squiggle braces in column 1 inside
350 a string, because this confuses some editors' syntax highlighting
351 engines. */
353 puts ("\
354 /* Generated automatically by the program `genextract'\n\
355 from the machine description file `md'. */\n\
357 #include \"config.h\"\n\
358 #include \"system.h\"\n\
359 #include \"coretypes.h\"\n\
360 #include \"tm.h\"\n\
361 #include \"rtl.h\"\n\
362 #include \"insn-config.h\"\n\
363 #include \"recog.h\"\n\
364 #include \"toplev.h\"\n\
366 /* This variable is used as the \"location\" of any missing operand\n\
367 whose numbers are skipped by a given pattern. */\n\
368 static rtx junk ATTRIBUTE_UNUSED;\n");
370 puts ("\
371 void\n\
372 insn_extract (rtx insn)\n{\n\
373 rtx *ro = recog_data.operand;\n\
374 rtx **ro_loc = recog_data.operand_loc;\n\
375 rtx pat = PATTERN (insn);\n\
376 int i ATTRIBUTE_UNUSED; /* only for peepholes */\n\
378 #ifdef ENABLE_CHECKING\n\
379 memset (ro, 0xab, sizeof (*ro) * MAX_RECOG_OPERANDS);\n\
380 memset (ro_loc, 0xab, sizeof (*ro_loc) * MAX_RECOG_OPERANDS);\n\
381 #endif\n");
383 puts ("\
384 switch (INSN_CODE (insn))\n\
385 {\n\
386 default:\n\
387 /* Control reaches here if insn_extract has been called with an\n\
388 unrecognizable insn (code -1), or an insn whose INSN_CODE\n\
389 corresponds to a DEFINE_EXPAND in the machine description;\n\
390 either way, a bug. */\n\
391 if (INSN_CODE (insn) < 0)\n\
392 fatal_insn (\"unrecognizable insn:\", insn);\n\
393 else\n\
394 fatal_insn (\"insn with invalid code number:\", insn);\n");
398 main (int argc, char **argv)
400 rtx desc;
401 unsigned int i;
402 struct extraction *p;
403 struct code_ptr *link;
404 const char *name;
405 int insn_code_number;
406 int line_no;
408 progname = "genextract";
410 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
411 return (FATAL_EXIT_CODE);
413 /* Read the machine description. */
415 while ((desc = read_md_rtx (&line_no, &insn_code_number)) != NULL)
417 if (GET_CODE (desc) == DEFINE_INSN)
418 gen_insn (desc, insn_code_number);
420 else if (GET_CODE (desc) == DEFINE_PEEPHOLE)
422 struct code_ptr *link = XNEW (struct code_ptr);
424 link->insn_code = insn_code_number;
425 link->next = peepholes;
426 peepholes = link;
430 print_header ();
432 /* Write out code to handle peepholes and the insn_codes that it should
433 be called for. */
434 if (peepholes)
436 for (link = peepholes; link; link = link->next)
437 printf (" case %d:\n", link->insn_code);
439 /* The vector in the insn says how many operands it has.
440 And all it contains are operands. In fact, the vector was
441 created just for the sake of this function. We need to set the
442 location of the operands for sake of simplifications after
443 extraction, like eliminating subregs. */
444 puts (" for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)\n"
445 " ro[i] = *(ro_loc[i] = &XVECEXP (pat, 0, i));\n"
446 " break;\n");
449 /* Write out all the ways to extract insn operands. */
450 for (p = extractions; p; p = p->next)
452 for (link = p->insns; link; link = link->next)
454 i = link->insn_code;
455 name = get_insn_name (i);
456 if (name)
457 printf (" case %d: /* %s */\n", i, name);
458 else
459 printf (" case %d:\n", i);
462 for (i = 0; i < p->op_count; i++)
464 if (p->oplocs[i] == 0)
466 printf (" ro[%d] = const0_rtx;\n", i);
467 printf (" ro_loc[%d] = &junk;\n", i);
469 else
471 printf (" ro[%d] = *(ro_loc[%d] = &", i, i);
472 print_path (p->oplocs[i]);
473 puts (");");
477 for (i = 0; i < p->dup_count; i++)
479 printf (" recog_data.dup_loc[%d] = &", i);
480 print_path (p->duplocs[i]);
481 puts (";");
482 printf (" recog_data.dup_num[%d] = %d;\n", i, p->dupnums[i]);
485 puts (" break;\n");
488 puts (" }\n}");
489 fflush (stdout);
490 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);