1 /* Generate code from to output assembler insns as recognized from rtl.
2 Copyright (C) 1987, 88, 92, 94-95, 97-98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* This program reads the machine description for the compiler target machine
24 and produces a file containing these things:
26 1. An array of `struct insn_data', which is indexed by insn code number,
29 a. `name' is the name for that pattern. Nameless patterns are
32 b. `template' is the template for output of that insn,
34 c. `outfun' is the function that returns a template to use for output of
35 that insn. This is used only in the cases where the template is not
36 constant. These cases are specified by a * or @ at the beginning of the
37 template string in the machine description. They are identified for the
38 sake of other parts of the compiler by a zero element in `template'.
40 d. `genfun' is the function to generate a body for that pattern,
41 given operands as arguments.
43 e. `n_operands' is the number of distinct operands in the pattern
46 f. `n_dups' is the number of match_dup's that appear in the insn's
47 pattern. This says how many elements of `recog_data.dup_loc' are
48 significant after an insn has been recognized.
50 g. `n_alternatives' is the number of alternatives in the constraints
53 h. `operand' is the base of an array of operand data for the insn.
55 2. An array of `struct insn_operand data', used by `operand' above.
57 a. `predicate', an int-valued function, is the match_operand predicate
60 b. `constraint' is the constraint for this operand. This exists
61 only if register constraints appear in match_operand rtx's.
63 c. `address_p' indicates that the operand appears within ADDRESS
64 rtx's. This exists only if there are *no* register constraints
65 in the match_operand rtx's.
67 d. `mode' is the machine mode that that operand is supposed to have.
69 e. `strict_low', is nonzero for operands contained in a STRICT_LOW_PART.
71 The code number of an insn is simply its position in the machine
72 description; code numbers are assigned sequentially to entries in
73 the description, starting with code number 0.
75 Thus, the following entry in the machine description
78 [(set (match_operand:DF 0 "general_operand" "")
83 assuming it is the 25th entry present, would cause
84 insn_data[24].template to be "clrd %0", and
85 insn_data[24].n_operands to be 1. */
93 /* No instruction can have more operands than this. Sorry for this
94 arbitrary limit, but what machine will have an instruction with
95 this many operands? */
97 #define MAX_MAX_OPERANDS 40
99 static struct obstack obstack;
100 struct obstack *rtl_obstack = &obstack;
102 #define obstack_chunk_alloc xmalloc
103 #define obstack_chunk_free free
105 static int n_occurrences PROTO((int, char *));
107 /* insns in the machine description are assigned sequential code numbers
108 that are used by insn-recog.c (produced by genrecog) to communicate
109 to insn-output.c (produced by this program). */
111 static int next_code_number;
113 /* This counts all definitions in the md file,
114 for the sake of error messages. */
116 static int next_index_number;
118 /* This counts all operands used in the md file. The first is null. */
120 static int next_operand_number = 1;
122 /* Record in this chain all information about the operands we will output. */
126 struct operand_data *next;
130 enum machine_mode mode;
131 unsigned char n_alternatives;
137 /* Begin with a null operand at index 0. */
139 static struct operand_data null_operand =
141 0, 0, "", "", VOIDmode, 0, 0, 0, 0
144 static struct operand_data *odata = &null_operand;
145 static struct operand_data **odata_end = &null_operand.next;
147 /* Record in this chain all information that we will output,
148 associated with the code number of the insn. */
154 char *template; /* string such as "movl %1,%0" */
157 int n_operands; /* Number of operands this insn recognizes */
158 int n_dups; /* Number times match_dup appears in pattern */
159 int n_alternatives; /* Number of alternatives in each constraint */
160 char outfun; /* Nonzero means this has an output function */
161 int operand_number; /* Operand index in the big array. */
162 struct operand_data operand[MAX_MAX_OPERANDS];
165 /* This variable points to the first link in the insn chain. */
167 static struct data *idata, **idata_end = &idata;
169 /* Nonzero if any match_operand has a constraint string; implies that
170 REGISTER_CONSTRAINTS will be defined for this machine description. */
172 static int have_constraints;
175 static char * name_for_index PROTO((int));
176 static void output_prologue PROTO((void));
177 static void output_predicate_decls PROTO((void));
178 static void output_operand_data PROTO((void));
179 static void output_insn_data PROTO((void));
180 static void output_get_insn_name PROTO((void));
181 static void scan_operands PROTO((struct data *, rtx, int, int));
182 static int compare_operands PROTO((struct operand_data *,
183 struct operand_data *));
184 static void place_operands PROTO((struct data *));
185 static void process_template PROTO((struct data *, char *));
186 static void validate_insn_alternatives PROTO((struct data *));
187 static void gen_insn PROTO((rtx));
188 static void gen_peephole PROTO((rtx));
189 static void gen_expand PROTO((rtx));
190 static void gen_split PROTO((rtx));
191 static int n_occurrences PROTO((int, char *));
194 get_insn_name (index)
197 static char buf[100];
199 struct data *i, *last_named = NULL;
200 for (i = idata; i ; i = i->next)
202 if (i->index_number == index)
209 sprintf(buf, "%s+%d", last_named->name, index - last_named->index_number);
211 sprintf(buf, "insn %d", index);
219 printf ("/* Generated automatically by the program `genoutput'\n\
220 from the machine description file `md'. */\n\n");
222 printf ("#define NO_MD_PROTOTYPES\n");
223 printf ("#include \"config.h\"\n");
224 printf ("#include \"system.h\"\n");
225 printf ("#include \"flags.h\"\n");
226 printf ("#include \"rtl.h\"\n");
227 printf ("#include \"function.h\"\n");
228 printf ("#include \"regs.h\"\n");
229 printf ("#include \"hard-reg-set.h\"\n");
230 printf ("#include \"real.h\"\n");
231 printf ("#include \"insn-config.h\"\n\n");
232 printf ("#include \"conditions.h\"\n");
233 printf ("#include \"insn-flags.h\"\n");
234 printf ("#include \"insn-attr.h\"\n\n");
235 printf ("#include \"insn-codes.h\"\n\n");
236 printf ("#include \"recog.h\"\n\n");
238 printf ("#include \"output.h\"\n");
242 /* We need to define all predicates used. Keep a list of those we
243 have defined so far. There normally aren't very many predicates
244 used, so a linked list should be fast enough. */
247 output_predicate_decls ()
249 struct predicate { char *name; struct predicate *next; } *predicates = 0;
250 register struct operand_data *d;
253 for (d = odata; d; d = d->next)
254 if (d->predicate && d->predicate[0])
256 for (p = predicates; p; p = p->next)
257 if (strcmp (p->name, d->predicate) == 0)
262 printf ("extern int %s PROTO ((rtx, enum machine_mode));\n",
264 p = (struct predicate *) alloca (sizeof (struct predicate));
265 p->name = d->predicate;
266 p->next = predicates;
275 output_operand_data ()
277 register struct operand_data *d;
279 printf ("\nstatic const struct insn_operand_data operand_data[] = \n{\n");
281 for (d = odata; d; d = d->next)
286 d->predicate && d->predicate[0] ? d->predicate : "0");
288 if (have_constraints)
290 printf (" \"%s\",\n",
291 d->constraint ? d->constraint : "");
294 printf (" %smode,\n", GET_MODE_NAME (d->mode));
296 if (! have_constraints)
297 printf (" %d,\n", d->address_p);
299 printf (" %d\n", d->strict_low);
309 register struct data *d;
311 int next_name_offset;
312 const char * last_name = 0;
313 const char * next_name = 0;
314 register struct data *n;
316 for (n = idata, next_name_offset = 1; n; n = n->next, next_name_offset++)
323 printf ("\nconst struct insn_data insn_data[] = \n{\n");
325 for (d = idata; d; d = d->next)
331 printf (" \"%s\",\n", d->name);
335 for (n = d->next, next_name_offset = 1; n;
336 n = n->next, next_name_offset++)
348 if (next_name && (last_name == 0
349 || name_offset > next_name_offset / 2))
350 printf (" \"%s-%d\",\n", next_name,
351 next_name_offset - name_offset);
353 printf (" \"%s+%d\",\n", last_name, name_offset);
357 printf (" \"%s\",\n", d->template);
362 printf (" output_%d,\n", d->code_number);
366 if (d->name && d->name[0] != '*')
367 printf (" gen_%s,\n", d->name);
371 printf (" &operand_data[%d],\n", d->operand_number);
372 printf (" %d,\n", d->n_operands);
373 printf (" %d,\n", d->n_dups);
374 printf (" %d\n", d->n_alternatives);
382 output_get_insn_name ()
384 printf ("const char *\n");
385 printf ("get_insn_name (code)\n");
386 printf (" int code;\n");
388 printf (" return insn_data[code].name;\n");
393 /* Stores in max_opno the largest operand number present in `part', if
394 that is larger than the previous value of max_opno, and the rest of
395 the operand data into `d->operand[i]'.
397 THIS_ADDRESS_P is nonzero if the containing rtx was an ADDRESS.
398 THIS_STRICT_LOW is nonzero if the containing rtx was a STRICT_LOW_PART. */
404 scan_operands (d, part, this_address_p, this_strict_low)
411 register const char *format_ptr;
417 switch (GET_CODE (part))
420 opno = XINT (part, 0);
423 if (max_opno >= MAX_MAX_OPERANDS)
425 error ("Too many operands (%d) in definition %s.\n",
426 max_opno + 1, get_insn_name (next_index_number));
429 if (d->operand[opno].seen)
430 error ("Definition %s specified operand number %d more than once.\n",
431 get_insn_name (next_index_number), opno);
432 d->operand[opno].seen = 1;
433 d->operand[opno].mode = GET_MODE (part);
434 d->operand[opno].strict_low = this_strict_low;
435 d->operand[opno].predicate = XSTR (part, 1);
436 d->operand[opno].constraint = XSTR (part, 2);
437 if (XSTR (part, 2) != 0 && *XSTR (part, 2) != 0)
439 d->operand[opno].n_alternatives
440 = n_occurrences (',', XSTR (part, 2)) + 1;
441 have_constraints = 1;
443 d->operand[opno].address_p = this_address_p;
447 opno = XINT (part, 0);
450 if (max_opno >= MAX_MAX_OPERANDS)
452 error ("Too many operands (%d) in definition %s.\n",
453 max_opno + 1, get_insn_name (next_index_number));
456 if (d->operand[opno].seen)
457 error ("Definition %s specified operand number %d more than once.\n",
458 get_insn_name (next_index_number), opno);
459 d->operand[opno].seen = 1;
460 d->operand[opno].mode = GET_MODE (part);
461 d->operand[opno].strict_low = 0;
462 d->operand[opno].predicate = "scratch_operand";
463 d->operand[opno].constraint = XSTR (part, 1);
464 if (XSTR (part, 1) != 0 && *XSTR (part, 1) != 0)
466 d->operand[opno].n_alternatives
467 = n_occurrences (',', XSTR (part, 1)) + 1;
468 have_constraints = 1;
470 d->operand[opno].address_p = 0;
475 opno = XINT (part, 0);
478 if (max_opno >= MAX_MAX_OPERANDS)
480 error ("Too many operands (%d) in definition %s.\n",
481 max_opno + 1, get_insn_name (next_index_number));
484 if (d->operand[opno].seen)
485 error ("Definition %s specified operand number %d more than once.\n",
486 get_insn_name (next_index_number), opno);
487 d->operand[opno].seen = 1;
488 d->operand[opno].mode = GET_MODE (part);
489 d->operand[opno].strict_low = 0;
490 d->operand[opno].predicate = XSTR (part, 1);
491 d->operand[opno].constraint = 0;
492 d->operand[opno].address_p = 0;
493 for (i = 0; i < XVECLEN (part, 2); i++)
494 scan_operands (d, XVECEXP (part, 2, i), 0, 0);
504 scan_operands (d, XEXP (part, 0), 1, 0);
507 case STRICT_LOW_PART:
508 scan_operands (d, XEXP (part, 0), 0, 1);
515 format_ptr = GET_RTX_FORMAT (GET_CODE (part));
517 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (part)); i++)
518 switch (*format_ptr++)
522 scan_operands (d, XEXP (part, i), 0, 0);
525 if (XVEC (part, i) != NULL)
526 for (j = 0; j < XVECLEN (part, i); j++)
527 scan_operands (d, XVECEXP (part, i, j), 0, 0);
532 /* Compare two operands for content equality. */
535 compare_operands (d0, d1)
536 struct operand_data *d0, *d1;
546 if (strcmp (p0, p1) != 0)
549 if (have_constraints)
557 if (strcmp (p0, p1) != 0)
561 if (d0->mode != d1->mode)
564 if (!have_constraints)
565 if (d0->address_p != d1->address_p)
568 if (d0->strict_low != d1->strict_low)
574 /* Scan the list of operands we've already committed to output and either
575 find a subsequence that is the same, or allocate a new one at the end. */
581 struct operand_data *od, *od2;
584 if (d->n_operands == 0)
586 d->operand_number = 0;
590 /* Brute force substring search. */
591 for (od = odata, i = 0; od; od = od->next, i = 0)
592 if (compare_operands (od, &d->operand[0]))
598 if (i == d->n_operands)
602 if (! compare_operands (od2, &d->operand[i]))
604 ++i, od2 = od2->next;
608 /* Either partial match at the end of the list, or no match. In either
609 case, we tack on what operands are remaining to the end of the list. */
611 d->operand_number = next_operand_number - i;
612 for (; i < d->n_operands; ++i)
614 od2 = &d->operand[i];
616 odata_end = &od2->next;
617 od2->index = next_operand_number++;
623 d->operand_number = od->index;
628 /* Process an assembler template from a define_insn or a define_peephole.
629 It is either the assembler code template, a list of assembler code
630 templates, or C code to generate the assembler code template. */
633 process_template (d, template)
640 /* We need to consider only the instructions whose assembler code template
641 starts with a * or @. These are the ones where C code is run to decide
642 on a template to use. So for all others just return now. */
644 if (template[0] != '*' && template[0] != '@')
646 d->template = template;
654 printf ("\nstatic const char *output_%d PROTO ((rtx *, rtx));\n",
656 printf ("\nstatic const char *\n");
657 printf ("output_%d (operands, insn)\n", d->code_number);
658 printf (" rtx *operands ATTRIBUTE_UNUSED;\n");
659 printf (" rtx insn ATTRIBUTE_UNUSED;\n");
662 /* If the assembler code template starts with a @ it is a newline-separated
663 list of assembler code templates, one for each alternative. So produce
664 a routine to select the correct one. */
666 if (template[0] == '@')
669 printf (" static const char *const strings_%d[] = {\n",
672 for (i = 0, cp = &template[1]; *cp; )
674 while (*cp == '\n' || *cp == ' ' || *cp== '\t')
678 while (*cp != '\n' && *cp != '\0')
689 printf (" return strings_%d[which_alternative];\n", d->code_number);
691 if (i != d->n_alternatives)
692 fatal ("Insn pattern %d has %d alternatives but %d assembler choices",
693 d->index_number, d->n_alternatives, i);
698 /* The following is done in a funny way to get around problems in
699 VAX-11 "C" on VMS. It is the equivalent of:
700 printf ("%s\n", &template[1])); */
713 /* Check insn D for consistency in number of constraint alternatives. */
716 validate_insn_alternatives (d)
719 register int n = 0, start;
721 /* Make sure all the operands have the same number of alternatives
722 in their constraints. Let N be that number. */
723 for (start = 0; start < d->n_operands; start++)
724 if (d->operand[start].n_alternatives > 0)
727 n = d->operand[start].n_alternatives;
728 else if (n != d->operand[start].n_alternatives)
729 error ("wrong number of alternatives in operand %d of insn %s",
730 start, get_insn_name (d->index_number));
733 /* Record the insn's overall number of alternatives. */
734 d->n_alternatives = n;
737 /* Look at a define_insn just read. Assign its code number. Record
738 on idata the template and the number of arguments. If the insn has
739 a hairy output action, output a function for now. */
745 register struct data *d = (struct data *) xmalloc (sizeof (struct data));
748 d->code_number = next_code_number++;
749 d->index_number = next_index_number;
750 if (XSTR (insn, 0)[0])
751 d->name = XSTR (insn, 0);
755 /* Build up the list in the same order as the insns are seen
756 in the machine description. */
759 idata_end = &d->next;
763 memset (d->operand, 0, sizeof (d->operand));
765 for (i = 0; i < XVECLEN (insn, 1); i++)
766 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
768 d->n_operands = max_opno + 1;
769 d->n_dups = num_dups;
771 validate_insn_alternatives (d);
773 process_template (d, XSTR (insn, 3));
776 /* Look at a define_peephole just read. Assign its code number.
777 Record on idata the template and the number of arguments.
778 If the insn has a hairy output action, output it now. */
784 register struct data *d = (struct data *) xmalloc (sizeof (struct data));
787 d->code_number = next_code_number++;
788 d->index_number = next_index_number;
791 /* Build up the list in the same order as the insns are seen
792 in the machine description. */
795 idata_end = &d->next;
799 memset (d->operand, 0, sizeof (d->operand));
801 /* Get the number of operands by scanning all the patterns of the
802 peephole optimizer. But ignore all the rest of the information
804 for (i = 0; i < XVECLEN (peep, 0); i++)
805 scan_operands (d, XVECEXP (peep, 0, i), 0, 0);
807 d->n_operands = max_opno + 1;
810 validate_insn_alternatives (d);
812 process_template (d, XSTR (peep, 2));
815 /* Process a define_expand just read. Assign its code number,
816 only for the purposes of `insn_gen_function'. */
822 register struct data *d = (struct data *) xmalloc (sizeof (struct data));
825 d->code_number = next_code_number++;
826 d->index_number = next_index_number;
827 if (XSTR (insn, 0)[0])
828 d->name = XSTR (insn, 0);
832 /* Build up the list in the same order as the insns are seen
833 in the machine description. */
836 idata_end = &d->next;
840 memset (d->operand, 0, sizeof (d->operand));
842 /* Scan the operands to get the specified predicates and modes,
843 since expand_binop needs to know them. */
846 for (i = 0; i < XVECLEN (insn, 1); i++)
847 scan_operands (d, XVECEXP (insn, 1, i), 0, 0);
849 d->n_operands = max_opno + 1;
850 d->n_dups = num_dups;
854 validate_insn_alternatives (d);
858 /* Process a define_split just read. Assign its code number,
859 only for reasons of consistency and to simplify genrecog. */
865 register struct data *d = (struct data *) xmalloc (sizeof (struct data));
868 d->code_number = next_code_number++;
869 d->index_number = next_index_number;
872 /* Build up the list in the same order as the insns are seen
873 in the machine description. */
876 idata_end = &d->next;
880 memset (d->operand, 0, sizeof (d->operand));
882 /* Get the number of operands by scanning all the patterns of the
883 split patterns. But ignore all the rest of the information thus
885 for (i = 0; i < XVECLEN (split, 0); i++)
886 scan_operands (d, XVECEXP (split, 0, i), 0, 0);
888 d->n_operands = max_opno + 1;
890 d->n_alternatives = 0;
901 register PTR val = (PTR) malloc (size);
904 fatal ("virtual memory exhausted");
915 ptr = (PTR) realloc (old, size);
917 ptr = (PTR) malloc (size);
919 fatal ("virtual memory exhausted");
932 progname = "genoutput";
933 obstack_init (rtl_obstack);
936 fatal ("No input file name.");
938 infile = fopen (argv[1], "r");
942 exit (FATAL_EXIT_CODE);
946 next_code_number = 0;
947 next_index_number = 0;
948 have_constraints = 0;
950 /* Read the machine description. */
954 c = read_skip_spaces (infile);
959 desc = read_rtx (infile);
960 if (GET_CODE (desc) == DEFINE_INSN)
962 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
964 if (GET_CODE (desc) == DEFINE_EXPAND)
966 if (GET_CODE (desc) == DEFINE_SPLIT
967 || GET_CODE (desc) == DEFINE_PEEPHOLE2)
973 output_predicate_decls ();
974 output_operand_data ();
976 output_get_insn_name ();
979 exit (ferror (stdout) != 0 || have_error
980 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);