1 /* Generate code from machine description to extract operands from insn as rtl.
2 Copyright (C) 1987, 1991, 1992, 1993 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "insn-config.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 /* Names for patterns. Need to allow linking with print-rtl. */
39 /* This structure contains all the information needed to describe one
40 set of extractions methods. Each method may be used by more than
41 one pattern if the operands are in the same place.
43 The string for each operand describes that path to the operand and
44 contains `0' through `9' when going into an expression and `a' through
45 `z' when going into a vector. We assume here that only the first operand
46 of an rtl expression is a vector. genrecog.c makes the same assumption
47 (and uses the same representation) and it is currently true. */
52 char *oplocs[MAX_RECOG_OPERANDS];
54 char *duplocs[MAX_DUP_OPERANDS];
55 int dupnums[MAX_DUP_OPERANDS];
56 struct code_ptr *insns;
57 struct extraction *next;
60 /* Holds a single insn code that use an extraction method. */
65 struct code_ptr *next;
68 static struct extraction *extractions;
70 /* Number instruction patterns handled, starting at 0 for first one. */
72 static int insn_code_number;
74 /* Records the large operand number in this insn. */
78 /* Records the location of any operands using the string format described
81 static char *oplocs[MAX_RECOG_OPERANDS];
83 /* Number the occurrences of MATCH_DUP in each instruction,
84 starting at 0 for the first occurrence. */
88 /* Records the location of any MATCH_DUP operands. */
90 static char *duplocs[MAX_DUP_OPERANDS];
92 /* Record the operand number of any MATCH_DUPs. */
94 static int dupnums[MAX_DUP_OPERANDS];
96 /* Record the list of insn_codes for peepholes. */
98 static struct code_ptr *peepholes;
100 static void walk_rtx ();
101 static void print_path ();
104 static void fatal ();
105 static char *copystr ();
106 static void mybzero ();
114 register struct extraction *p;
115 register struct code_ptr *link;
120 /* No operands seen so far in this pattern. */
121 mybzero (oplocs, sizeof oplocs);
123 /* Walk the insn's pattern, remembering at all times the path
124 down to the walking point. */
126 if (XVECLEN (insn, 1) == 1)
127 walk_rtx (XVECEXP (insn, 1, 0), "");
129 for (i = XVECLEN (insn, 1) - 1; i >= 0; i--)
131 char *path = (char *) alloca (2);
136 walk_rtx (XVECEXP (insn, 1, i), path);
139 link = (struct code_ptr *) xmalloc (sizeof (struct code_ptr));
140 link->insn_code = insn_code_number;
142 /* See if we find something that already had this extraction method. */
144 for (p = extractions; p; p = p->next)
146 if (p->op_count != op_count || p->dup_count != dup_count)
149 for (i = 0; i < op_count; i++)
150 if (p->oplocs[i] != oplocs[i]
151 && ! (p->oplocs[i] != 0 && oplocs[i] != 0
152 && ! strcmp (p->oplocs[i], oplocs[i])))
158 for (i = 0; i < dup_count; i++)
159 if (p->dupnums[i] != dupnums[i]
160 || strcmp (p->duplocs[i], duplocs[i]))
166 /* This extraction is the same as ours. Just link us in. */
167 link->next = p->insns;
172 /* Otherwise, make a new extraction method. */
174 p = (struct extraction *) xmalloc (sizeof (struct extraction));
175 p->op_count = op_count;
176 p->dup_count = dup_count;
177 p->next = extractions;
182 for (i = 0; i < op_count; i++)
183 p->oplocs[i] = oplocs[i];
185 for (i = 0; i < dup_count; i++)
186 p->dupnums[i] = dupnums[i], p->duplocs[i] = duplocs[i];
194 register RTX_CODE code;
198 register struct code_ptr *link;
199 int depth = strlen (path);
217 oplocs[XINT (x, 0)] = copystr (path);
218 op_count = MAX (op_count, XINT (x, 0) + 1);
223 duplocs[dup_count] = copystr (path);
224 dupnums[dup_count] = XINT (x, 0);
229 duplocs[dup_count] = copystr (path);
230 dupnums[dup_count] = XINT (x, 0);
233 newpath = (char *) alloca (depth + 2);
234 strcpy (newpath, path);
235 newpath[depth + 1] = 0;
237 for (i = XVECLEN (x, 1) - 1; i >= 0; i--)
239 newpath[depth] = '0' + i;
240 walk_rtx (XVECEXP (x, 1, i), newpath);
245 oplocs[XINT (x, 0)] = copystr (path);
246 op_count = MAX (op_count, XINT (x, 0) + 1);
248 newpath = (char *) alloca (depth + 2);
249 strcpy (newpath, path);
250 newpath[depth + 1] = 0;
252 for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
254 newpath[depth] = '0' + i;
255 walk_rtx (XVECEXP (x, 2, i), newpath);
260 oplocs[XINT (x, 0)] = copystr (path);
261 op_count = MAX (op_count, XINT (x, 0) + 1);
263 newpath = (char *) alloca (depth + 2);
264 strcpy (newpath, path);
265 newpath[depth + 1] = 0;
267 for (i = XVECLEN (x, 2) - 1; i >= 0; i--)
269 newpath[depth] = 'a' + i;
270 walk_rtx (XVECEXP (x, 2, i), newpath);
275 walk_rtx (XEXP (x, 0), path);
279 newpath = (char *) alloca (depth + 2);
280 strcpy (newpath, path);
281 newpath[depth + 1] = 0;
283 fmt = GET_RTX_FORMAT (code);
284 len = GET_RTX_LENGTH (code);
285 for (i = 0; i < len; i++)
287 if (fmt[i] == 'e' || fmt[i] == 'u')
289 newpath[depth] = '0' + i;
290 walk_rtx (XEXP (x, i), newpath);
292 else if (fmt[i] == 'E')
295 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
297 newpath[depth] = 'a' + j;
298 walk_rtx (XVECEXP (x, i, j), newpath);
304 /* Given a PATH, representing a path down the instruction's
305 pattern from the root to a certain point, output code to
306 evaluate to the rtx at that point. */
312 register int len = strlen (path);
315 /* We first write out the operations (XEXP or XVECEXP) in reverse
316 order, then write "insn", then the indices in forward order. */
318 for (i = len - 1; i >=0 ; i--)
320 if (path[i] >= 'a' && path[i] <= 'z')
321 printf ("XVECEXP (");
322 else if (path[i] >= '0' && path[i] <= '9')
330 for (i = 0; i < len; i++)
332 if (path[i] >= 'a' && path[i] <= 'z')
333 printf (", 0, %d)", path[i] - 'a');
334 else if (path[i] >= '0' && path[i] <= '9')
335 printf (", %d)", path[i] - '0');
345 register char *val = (char *) malloc (size);
348 fatal ("virtual memory exhausted");
357 char *result = (char *) realloc (ptr, size);
359 fatal ("virtual memory exhausted");
367 fprintf (stderr, "genextract: ");
368 fprintf (stderr, s, a1, a2);
369 fprintf (stderr, "\n");
370 exit (FATAL_EXIT_CODE);
373 /* More 'friendly' abort that prints the line and file.
374 config.h can #define abort fancy_abort if you like that sort of thing. */
379 fatal ("Internal gcc abort.");
391 tem = (char *) xmalloc (strlen (s1) + 1);
400 register unsigned length;
414 struct extraction *p;
415 struct code_ptr *link;
417 obstack_init (rtl_obstack);
420 fatal ("No input file name.");
422 infile = fopen (argv[1], "r");
426 exit (FATAL_EXIT_CODE);
431 /* Assign sequential codes to all entries in the machine description
432 in parallel with the tables in insn-output.c. */
434 insn_code_number = 0;
436 printf ("/* Generated automatically by the program `genextract'\n\
437 from the machine description file `md'. */\n\n");
439 printf ("#include \"config.h\"\n");
440 printf ("#include \"rtl.h\"\n\n");
442 /* This variable exists only so it can be the "location"
443 of any missing operand whose numbers are skipped by a given pattern. */
444 printf ("static rtx junk;\n");
446 printf ("extern rtx recog_operand[];\n");
447 printf ("extern rtx *recog_operand_loc[];\n");
448 printf ("extern rtx *recog_dup_loc[];\n");
449 printf ("extern char recog_dup_num[];\n");
451 printf ("void\ninsn_extract (insn)\n");
452 printf (" rtx insn;\n");
454 printf (" register rtx *ro = recog_operand;\n");
455 printf (" register rtx **ro_loc = recog_operand_loc;\n");
456 printf (" rtx pat = PATTERN (insn);\n");
457 printf (" switch (INSN_CODE (insn))\n");
459 printf (" case -1:\n");
460 printf (" fatal_insn_not_found (insn);\n\n");
462 /* Read the machine description. */
466 c = read_skip_spaces (infile);
471 desc = read_rtx (infile);
472 if (GET_CODE (desc) == DEFINE_INSN)
478 else if (GET_CODE (desc) == DEFINE_PEEPHOLE)
480 struct code_ptr *link
481 = (struct code_ptr *) xmalloc (sizeof (struct code_ptr));
483 link->insn_code = insn_code_number;
484 link->next = peepholes;
489 else if (GET_CODE (desc) == DEFINE_EXPAND
490 || GET_CODE (desc) == DEFINE_SPLIT)
494 /* Write out code to handle peepholes and the insn_codes that it should
498 for (link = peepholes; link; link = link->next)
499 printf (" case %d:\n", link->insn_code);
501 /* The vector in the insn says how many operands it has.
502 And all it contains are operands. In fact, the vector was
503 created just for the sake of this function. */
504 printf ("#if __GNUC__ > 1 && !defined (bcopy)\n");
505 printf ("#define bcopy(FROM,TO,COUNT) __builtin_memcpy(TO,FROM,COUNT)\n");
507 printf (" bcopy (&XVECEXP (pat, 0, 0), ro,\n");
508 printf (" sizeof (rtx) * XVECLEN (pat, 0));\n");
509 printf (" break;\n\n");
512 /* Write out all the ways to extract insn operands. */
513 for (p = extractions; p; p = p->next)
515 for (link = p->insns; link; link = link->next)
516 printf (" case %d:\n", link->insn_code);
518 for (i = 0; i < p->op_count; i++)
520 if (p->oplocs[i] == 0)
522 printf (" ro[%d] = const0_rtx;\n", i);
523 printf (" ro_loc[%d] = &junk;\n", i);
527 printf (" ro[%d] = *(ro_loc[%d] = &", i, i);
528 print_path (p->oplocs[i]);
533 for (i = 0; i < p->dup_count; i++)
535 printf (" recog_dup_loc[%d] = &", i);
536 print_path (p->duplocs[i]);
538 printf (" recog_dup_num[%d] = %d;\n", i, p->dupnums[i]);
541 printf (" break;\n\n");
544 /* This should never be reached. Note that we would also reach this abort
545 if we tried to extract something whose INSN_CODE was a DEFINE_EXPAND or
546 DEFINE_SPLIT, but that is correct. */
547 printf (" default:\n abort ();\n");
552 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);