1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 1988, 1991 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. */
26 static struct obstack obstack;
27 struct obstack *rtl_obstack = &obstack;
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
39 static int max_dup_opno;
40 static int register_constraints;
41 static int insn_code_number;
42 static int insn_index_number;
44 /* Data structure for recording the patterns of insns that have CLOBBERs.
45 We use this to output a function that adds these CLOBBERs to a
46 previously-allocated PARALLEL expression. */
50 struct clobber_ent *insns;
53 struct clobber_pat *next;
56 /* Records one insn that uses the clobber list. */
60 int code_number; /* Counts only insns. */
61 struct clobber_ent *next;
68 register RTX_CODE code;
78 if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
79 register_constraints = 1;
80 if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
81 register_constraints = 1;
82 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
83 || code == MATCH_PARALLEL)
84 max_opno = MAX (max_opno, XINT (x, 0));
85 if (code == MATCH_DUP || code == MATCH_OP_DUP)
86 max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
88 fmt = GET_RTX_FORMAT (code);
89 len = GET_RTX_LENGTH (code);
90 for (i = 0; i < len; i++)
92 if (fmt[i] == 'e' || fmt[i] == 'u')
93 max_operand_1 (XEXP (x, i));
94 else if (fmt[i] == 'E')
97 for (j = 0; j < XVECLEN (x, i); j++)
98 max_operand_1 (XVECEXP (x, i, j));
104 max_operand_vec (insn, arg)
108 register int len = XVECLEN (insn, arg);
114 for (i = 0; i < len; i++)
115 max_operand_1 (XVECEXP (insn, arg, i));
125 for (p1 = GET_RTX_NAME (code); *p1; p1++)
127 if (*p1 >= 'a' && *p1 <= 'z')
128 putchar (*p1 + 'A' - 'a');
134 /* Print a C expression to construct an RTX just like X,
135 substituting any operand references appearing within. */
141 register RTX_CODE code;
158 printf ("operand%d", XINT (x, 0));
162 printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
163 XINT (x, 0), XINT (x, 0));
164 for (i = 0; i < XVECLEN (x, 1); i++)
167 gen_exp (XVECEXP (x, 1, i));
173 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
174 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
175 for (i = 0; i < XVECLEN (x, 2); i++)
178 gen_exp (XVECEXP (x, 2, i));
184 printf ("operand%d", XINT (x, 0));
188 printf ("gen_rtx (SCRATCH, %smode, 0)", GET_MODE_NAME (GET_MODE (x)));
192 fatal ("ADDRESS expression code used in named instruction pattern");
205 printf ("const0_rtx");
210 printf ("const1_rtx");
213 if (INTVAL (x) == -1)
215 printf ("constm1_rtx");
218 if (INTVAL (x) == STORE_FLAG_VALUE)
220 printf ("const_true_rtx");
225 printf ("gen_rtx (");
227 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
229 fmt = GET_RTX_FORMAT (code);
230 len = GET_RTX_LENGTH (code);
231 for (i = 0; i < len; i++)
236 if (fmt[i] == 'e' || fmt[i] == 'u')
237 gen_exp (XEXP (x, i));
238 else if (fmt[i] == 'i')
239 printf ("%u", (unsigned) XINT (x, i));
240 else if (fmt[i] == 's')
241 printf ("\"%s\"", XSTR (x, i));
242 else if (fmt[i] == 'E')
245 printf ("gen_rtvec (%d", XVECLEN (x, i));
246 for (j = 0; j < XVECLEN (x, i); j++)
249 gen_exp (XVECEXP (x, i, j));
259 /* Generate the `gen_...' function for a DEFINE_INSN. */
268 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
269 registers or MATCH_SCRATCHes. If so, store away the information for
274 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
275 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
276 || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
277 && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
280 if (i != XVECLEN (insn, 1) - 1)
282 register struct clobber_pat *p;
283 register struct clobber_ent *link
284 = (struct clobber_ent *) xmalloc (sizeof (struct clobber_ent));
287 link->code_number = insn_code_number;
289 /* See if any previous CLOBBER_LIST entry is the same as this
292 for (p = clobber_list; p; p = p->next)
294 if (p->first_clobber != i + 1
295 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
298 for (j = i + 1; j < XVECLEN (insn, 1); j++)
300 rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
301 rtx new = XEXP (XVECEXP (insn, 1, j), 0);
303 /* OLD and NEW are the same if both are to be a SCRATCH
304 or if both are registers of the same mode and number. */
305 if (! ((GET_CODE (old) == MATCH_SCRATCH
306 && GET_CODE (new) == MATCH_SCRATCH)
307 || (GET_CODE (old) == REG && GET_CODE (new) == REG
308 && GET_MODE (old) == GET_MODE (new)
309 && REGNO (old) == REGNO (new))))
313 if (j == XVECLEN (insn, 1))
319 p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
323 p->first_clobber = i + 1;
324 p->next = clobber_list;
328 link->next = p->insns;
333 /* Don't mention instructions whose names are the null string.
334 They are in the machine description just to be recognized. */
335 if (strlen (XSTR (insn, 0)) == 0)
338 /* Find out how many operands this function has,
339 and also whether any of them have register constraints. */
340 register_constraints = 0;
341 operands = max_operand_vec (insn, 1);
342 if (max_dup_opno >= operands)
343 fatal ("match_dup operand number has no match_operand");
345 /* Output the function name and argument declarations. */
346 printf ("rtx\ngen_%s (", XSTR (insn, 0));
347 for (i = 0; i < operands; i++)
348 printf (i ? ", operand%d" : "operand%d", i);
350 for (i = 0; i < operands; i++)
351 printf (" rtx operand%d;\n", i);
354 /* Output code to construct and return the rtl for the instruction body */
356 if (XVECLEN (insn, 1) == 1)
359 gen_exp (XVECEXP (insn, 1, 0));
364 printf (" return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
365 for (i = 0; i < XVECLEN (insn, 1); i++)
368 gen_exp (XVECEXP (insn, 1, i));
370 printf ("));\n}\n\n");
374 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
383 if (strlen (XSTR (expand, 0)) == 0)
384 fatal ("define_expand lacks a name");
385 if (XVEC (expand, 1) == 0)
386 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
388 /* Find out how many operands this function has,
389 and also whether any of them have register constraints. */
390 register_constraints = 0;
392 operands = max_operand_vec (expand, 1);
394 /* Output the function name and argument declarations. */
395 printf ("rtx\ngen_%s (", XSTR (expand, 0));
396 for (i = 0; i < operands; i++)
397 printf (i ? ", operand%d" : "operand%d", i);
399 for (i = 0; i < operands; i++)
400 printf (" rtx operand%d;\n", i);
403 /* If we don't have any C code to write, only one insn is being written,
404 and no MATCH_DUPs are present, we can just return the desired insn
405 like we do for a DEFINE_INSN. This saves memory. */
406 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
407 && operands > max_dup_opno
408 && XVECLEN (expand, 1) == 1)
411 gen_exp (XVECEXP (expand, 1, 0));
416 /* For each operand referred to only with MATCH_DUPs,
417 make a local variable. */
418 for (i = operands; i <= max_dup_opno; i++)
419 printf (" rtx operand%d;\n", i);
420 if (operands > 0 || max_dup_opno >= 0)
421 printf (" rtx operands[%d];\n", MAX (operands, max_dup_opno + 1));
422 printf (" rtx _val = 0;\n");
423 printf (" start_sequence ();\n");
425 /* The fourth operand of DEFINE_EXPAND is some code to be executed
426 before the actual construction.
427 This code expects to refer to `operands'
428 just as the output-code in a DEFINE_INSN does,
429 but here `operands' is an automatic array.
430 So copy the operand values there before executing it. */
431 if (XSTR (expand, 3) && *XSTR (expand, 3))
433 /* Output code to copy the arguments into `operands'. */
434 for (i = 0; i < operands; i++)
435 printf (" operands[%d] = operand%d;\n", i, i);
437 /* Output the special code to be executed before the sequence
439 printf ("%s\n", XSTR (expand, 3));
441 /* Output code to copy the arguments back out of `operands'
442 (unless we aren't going to use them at all). */
443 if (XVEC (expand, 1) != 0)
445 for (i = 0; i < operands; i++)
446 printf (" operand%d = operands[%d];\n", i, i);
447 for (; i <= max_dup_opno; i++)
448 printf (" operand%d = operands[%d];\n", i, i);
452 /* Output code to construct the rtl for the instruction bodies.
453 Use emit_insn to add them to the sequence being accumulated.
454 But don't do this if the user's code has set `no_more' nonzero. */
456 for (i = 0; i < XVECLEN (expand, 1); i++)
458 rtx next = XVECEXP (expand, 1, i);
459 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
460 || (GET_CODE (next) == PARALLEL
461 && GET_CODE (XVECEXP (next, 0, 0)) == SET
462 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
463 || GET_CODE (next) == RETURN)
464 printf (" emit_jump_insn (");
465 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
466 || GET_CODE (next) == CALL
467 || (GET_CODE (next) == PARALLEL
468 && GET_CODE (XVECEXP (next, 0, 0)) == SET
469 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
470 || (GET_CODE (next) == PARALLEL
471 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
472 printf (" emit_call_insn (");
473 else if (GET_CODE (next) == CODE_LABEL)
474 printf (" emit_label (");
475 else if (GET_CODE (next) == MATCH_OPERAND
476 || GET_CODE (next) == MATCH_OPERATOR
477 || GET_CODE (next) == MATCH_PARALLEL
478 || GET_CODE (next) == MATCH_OP_DUP
479 || GET_CODE (next) == MATCH_DUP
480 || GET_CODE (next) == PARALLEL)
483 printf (" emit_insn (");
486 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
487 && GET_CODE (SET_SRC (next)) == LABEL_REF)
488 printf (" emit_barrier ();");
491 /* Call `gen_sequence' to make a SEQUENCE out of all the
492 insns emitted within this gen_... function. */
494 printf (" _done:\n");
495 printf (" _val = gen_sequence ();\n");
496 printf (" _fail:\n");
497 printf (" end_sequence ();\n");
498 printf (" return _val;\n}\n\n");
501 /* Like gen_expand, but generates a SEQUENCE. */
509 if (XVEC (split, 0) == 0)
510 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
511 else if (XVEC (split, 2) == 0)
512 fatal ("define_split (definition %d) lacks a replacement pattern",
515 /* Find out how many operands this function has. */
517 max_operand_vec (split, 2);
518 operands = MAX (max_opno, max_dup_opno) + 1;
520 /* Output the function name and argument declarations. */
521 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
525 /* Declare all local variables. */
526 for (i = 0; i < operands; i++)
527 printf (" rtx operand%d;\n", i);
528 printf (" rtx _val;\n");
529 printf (" start_sequence ();\n");
531 /* The fourth operand of DEFINE_SPLIT is some code to be executed
532 before the actual construction. */
535 printf ("%s\n", XSTR (split, 3));
537 /* Output code to copy the arguments back out of `operands' */
538 for (i = 0; i < operands; i++)
539 printf (" operand%d = operands[%d];\n", i, i);
541 /* Output code to construct the rtl for the instruction bodies.
542 Use emit_insn to add them to the sequence being accumulated.
543 But don't do this if the user's code has set `no_more' nonzero. */
545 for (i = 0; i < XVECLEN (split, 2); i++)
547 rtx next = XVECEXP (split, 2, i);
548 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
549 || (GET_CODE (next) == PARALLEL
550 && GET_CODE (XVECEXP (next, 0, 0)) == SET
551 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
552 || GET_CODE (next) == RETURN)
553 printf (" emit_jump_insn (");
554 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
555 || GET_CODE (next) == CALL
556 || (GET_CODE (next) == PARALLEL
557 && GET_CODE (XVECEXP (next, 0, 0)) == SET
558 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
559 || (GET_CODE (next) == PARALLEL
560 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
561 printf (" emit_call_insn (");
562 else if (GET_CODE (next) == CODE_LABEL)
563 printf (" emit_label (");
564 else if (GET_CODE (next) == MATCH_OPERAND
565 || GET_CODE (next) == MATCH_OPERATOR
566 || GET_CODE (next) == MATCH_PARALLEL
567 || GET_CODE (next) == MATCH_OP_DUP
568 || GET_CODE (next) == MATCH_DUP
569 || GET_CODE (next) == PARALLEL)
572 printf (" emit_insn (");
575 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
576 && GET_CODE (SET_SRC (next)) == LABEL_REF)
577 printf (" emit_barrier ();");
580 /* Call `gen_sequence' to make a SEQUENCE out of all the
581 insns emitted within this gen_... function. */
583 printf (" _done:\n");
584 printf (" _val = gen_sequence ();\n");
585 printf (" _fail:\n");
586 printf (" end_sequence ();\n");
587 printf (" return _val;\n}\n\n");
590 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
591 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
592 the end of the vector. */
595 output_add_clobbers ()
597 struct clobber_pat *clobber;
598 struct clobber_ent *ent;
601 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
602 printf (" rtx pattern;\n int insn_code_number;\n");
604 printf (" int i;\n\n");
605 printf (" switch (insn_code_number)\n");
608 for (clobber = clobber_list; clobber; clobber = clobber->next)
610 for (ent = clobber->insns; ent; ent = ent->next)
611 printf (" case %d:\n", ent->code_number);
613 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
615 printf (" XVECEXP (pattern, 0, %d) = ", i);
616 gen_exp (XVECEXP (clobber->pattern, 1, i));
620 printf (" break;\n\n");
623 printf (" default:\n");
624 printf (" abort ();\n");
629 /* Write a function, init_mov_optab, that is called to set up entries
630 in mov_optab for EXTRA_CC_MODES. */
633 output_init_mov_optab ()
635 #ifdef EXTRA_CC_NAMES
636 static char *cc_names[] = { EXTRA_CC_NAMES };
640 printf ("\nvoid\ninit_mov_optab ()\n{\n");
642 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
644 printf ("#ifdef HAVE_mov");
645 for (p = cc_names[i]; *p; p++)
646 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
648 printf (" if (HAVE_mov");
649 for (p = cc_names[i]; *p; p++)
650 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
652 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
654 for (p = cc_names[i]; *p; p++)
655 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
656 printf (";\n#endif\n");
667 register char *val = (char *) malloc (size);
670 fatal ("virtual memory exhausted");
680 char *result = (char *) realloc (ptr, size);
682 fatal ("virtual memory exhausted");
690 fprintf (stderr, "genemit: ");
691 fprintf (stderr, s, a1, a2);
692 fprintf (stderr, "\n");
693 exit (FATAL_EXIT_CODE);
696 /* More 'friendly' abort that prints the line and file.
697 config.h can #define abort fancy_abort if you like that sort of thing. */
702 fatal ("Internal gcc abort.");
712 extern rtx read_rtx ();
715 obstack_init (rtl_obstack);
718 fatal ("No input file name.");
720 infile = fopen (argv[1], "r");
724 exit (FATAL_EXIT_CODE);
729 /* Assign sequential codes to all entries in the machine description
730 in parallel with the tables in insn-output.c. */
732 insn_code_number = 0;
733 insn_index_number = 0;
735 printf ("/* Generated automatically by the program `genemit'\n\
736 from the machine description file `md'. */\n\n");
738 printf ("#include \"config.h\"\n");
739 printf ("#include \"rtl.h\"\n");
740 printf ("#include \"expr.h\"\n");
741 printf ("#include \"real.h\"\n");
742 printf ("#include \"output.h\"\n");
743 printf ("#include \"insn-config.h\"\n\n");
744 printf ("#include \"insn-flags.h\"\n\n");
745 printf ("#include \"insn-codes.h\"\n\n");
746 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
747 printf ("extern rtx recog_operand[];\n");
748 printf ("#define operands emit_operand\n\n");
749 printf ("#define FAIL goto _fail\n\n");
750 printf ("#define DONE goto _done\n\n");
752 /* Read the machine description. */
756 c = read_skip_spaces (infile);
761 desc = read_rtx (infile);
762 if (GET_CODE (desc) == DEFINE_INSN)
767 if (GET_CODE (desc) == DEFINE_EXPAND)
772 if (GET_CODE (desc) == DEFINE_SPLIT)
777 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
784 /* Write out the routine to add CLOBBERs to a pattern. */
785 output_add_clobbers ();
787 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
788 output_init_mov_optab ();
791 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);