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
33 extern rtx read_rtx ();
40 static int max_dup_opno;
41 static int register_constraints;
42 static int insn_code_number;
43 static int insn_index_number;
45 /* Data structure for recording the patterns of insns that have CLOBBERs.
46 We use this to output a function that adds these CLOBBERs to a
47 previously-allocated PARALLEL expression. */
51 struct clobber_ent *insns;
54 struct clobber_pat *next;
57 /* Records one insn that uses the clobber list. */
61 int code_number; /* Counts only insns. */
62 struct clobber_ent *next;
69 register RTX_CODE code;
79 if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
80 register_constraints = 1;
81 if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
82 register_constraints = 1;
83 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
84 || code == MATCH_PARALLEL)
85 max_opno = MAX (max_opno, XINT (x, 0));
86 if (code == MATCH_DUP || code == MATCH_OP_DUP)
87 max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
89 fmt = GET_RTX_FORMAT (code);
90 len = GET_RTX_LENGTH (code);
91 for (i = 0; i < len; i++)
93 if (fmt[i] == 'e' || fmt[i] == 'u')
94 max_operand_1 (XEXP (x, i));
95 else if (fmt[i] == 'E')
98 for (j = 0; j < XVECLEN (x, i); j++)
99 max_operand_1 (XVECEXP (x, i, j));
105 max_operand_vec (insn, arg)
109 register int len = XVECLEN (insn, arg);
115 for (i = 0; i < len; i++)
116 max_operand_1 (XVECEXP (insn, arg, i));
126 for (p1 = GET_RTX_NAME (code); *p1; p1++)
128 if (*p1 >= 'a' && *p1 <= 'z')
129 putchar (*p1 + 'A' - 'a');
135 /* Print a C expression to construct an RTX just like X,
136 substituting any operand references appearing within. */
142 register RTX_CODE code;
159 printf ("operand%d", XINT (x, 0));
163 printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
164 XINT (x, 0), XINT (x, 0));
165 for (i = 0; i < XVECLEN (x, 1); i++)
168 gen_exp (XVECEXP (x, 1, i));
174 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
175 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
176 for (i = 0; i < XVECLEN (x, 2); i++)
179 gen_exp (XVECEXP (x, 2, i));
185 printf ("operand%d", XINT (x, 0));
189 printf ("gen_rtx (SCRATCH, %smode, 0)", GET_MODE_NAME (GET_MODE (x)));
193 fatal ("ADDRESS expression code used in named instruction pattern");
206 printf ("const0_rtx");
211 printf ("const1_rtx");
214 if (INTVAL (x) == -1)
216 printf ("constm1_rtx");
219 if (INTVAL (x) == STORE_FLAG_VALUE)
221 printf ("const_true_rtx");
226 printf ("gen_rtx (");
228 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
230 fmt = GET_RTX_FORMAT (code);
231 len = GET_RTX_LENGTH (code);
232 for (i = 0; i < len; i++)
237 if (fmt[i] == 'e' || fmt[i] == 'u')
238 gen_exp (XEXP (x, i));
239 else if (fmt[i] == 'i')
240 printf ("%u", (unsigned) XINT (x, i));
241 else if (fmt[i] == 's')
242 printf ("\"%s\"", XSTR (x, i));
243 else if (fmt[i] == 'E')
246 printf ("gen_rtvec (%d", XVECLEN (x, i));
247 for (j = 0; j < XVECLEN (x, i); j++)
250 gen_exp (XVECEXP (x, i, j));
260 /* Generate the `gen_...' function for a DEFINE_INSN. */
269 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
270 registers or MATCH_SCRATCHes. If so, store away the information for
275 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
276 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
277 || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
278 && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
281 if (i != XVECLEN (insn, 1) - 1)
283 register struct clobber_pat *p;
284 register struct clobber_ent *link
285 = (struct clobber_ent *) xmalloc (sizeof (struct clobber_ent));
288 link->code_number = insn_code_number;
290 /* See if any previous CLOBBER_LIST entry is the same as this
293 for (p = clobber_list; p; p = p->next)
295 if (p->first_clobber != i + 1
296 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
299 for (j = i + 1; j < XVECLEN (insn, 1); j++)
301 rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
302 rtx new = XEXP (XVECEXP (insn, 1, j), 0);
304 /* OLD and NEW are the same if both are to be a SCRATCH
306 or if both are registers of the same mode and number. */
307 if (! (GET_MODE (old) == GET_MODE (new)
308 && ((GET_CODE (old) == MATCH_SCRATCH
309 && GET_CODE (new) == MATCH_SCRATCH)
310 || (GET_CODE (old) == REG && GET_CODE (new) == REG
311 && REGNO (old) == REGNO (new)))))
315 if (j == XVECLEN (insn, 1))
321 p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
325 p->first_clobber = i + 1;
326 p->next = clobber_list;
330 link->next = p->insns;
335 /* Don't mention instructions whose names are the null string.
336 They are in the machine description just to be recognized. */
337 if (strlen (XSTR (insn, 0)) == 0)
340 /* Find out how many operands this function has,
341 and also whether any of them have register constraints. */
342 register_constraints = 0;
343 operands = max_operand_vec (insn, 1);
344 if (max_dup_opno >= operands)
345 fatal ("match_dup operand number has no match_operand");
347 /* Output the function name and argument declarations. */
348 printf ("rtx\ngen_%s (", XSTR (insn, 0));
349 for (i = 0; i < operands; i++)
350 printf (i ? ", operand%d" : "operand%d", i);
352 for (i = 0; i < operands; i++)
353 printf (" rtx operand%d;\n", i);
356 /* Output code to construct and return the rtl for the instruction body */
358 if (XVECLEN (insn, 1) == 1)
361 gen_exp (XVECEXP (insn, 1, 0));
366 printf (" return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
367 for (i = 0; i < XVECLEN (insn, 1); i++)
370 gen_exp (XVECEXP (insn, 1, i));
372 printf ("));\n}\n\n");
376 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
385 if (strlen (XSTR (expand, 0)) == 0)
386 fatal ("define_expand lacks a name");
387 if (XVEC (expand, 1) == 0)
388 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
390 /* Find out how many operands this function has,
391 and also whether any of them have register constraints. */
392 register_constraints = 0;
394 operands = max_operand_vec (expand, 1);
396 /* Output the function name and argument declarations. */
397 printf ("rtx\ngen_%s (", XSTR (expand, 0));
398 for (i = 0; i < operands; i++)
399 printf (i ? ", operand%d" : "operand%d", i);
401 for (i = 0; i < operands; i++)
402 printf (" rtx operand%d;\n", i);
405 /* If we don't have any C code to write, only one insn is being written,
406 and no MATCH_DUPs are present, we can just return the desired insn
407 like we do for a DEFINE_INSN. This saves memory. */
408 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
409 && operands > max_dup_opno
410 && XVECLEN (expand, 1) == 1)
413 gen_exp (XVECEXP (expand, 1, 0));
418 /* For each operand referred to only with MATCH_DUPs,
419 make a local variable. */
420 for (i = operands; i <= max_dup_opno; i++)
421 printf (" rtx operand%d;\n", i);
422 if (operands > 0 || max_dup_opno >= 0)
423 printf (" rtx operands[%d];\n", MAX (operands, max_dup_opno + 1));
424 printf (" rtx _val = 0;\n");
425 printf (" start_sequence ();\n");
427 /* The fourth operand of DEFINE_EXPAND is some code to be executed
428 before the actual construction.
429 This code expects to refer to `operands'
430 just as the output-code in a DEFINE_INSN does,
431 but here `operands' is an automatic array.
432 So copy the operand values there before executing it. */
433 if (XSTR (expand, 3) && *XSTR (expand, 3))
435 /* Output code to copy the arguments into `operands'. */
436 for (i = 0; i < operands; i++)
437 printf (" operands[%d] = operand%d;\n", i, i);
439 /* Output the special code to be executed before the sequence
441 printf ("%s\n", XSTR (expand, 3));
443 /* Output code to copy the arguments back out of `operands'
444 (unless we aren't going to use them at all). */
445 if (XVEC (expand, 1) != 0)
447 for (i = 0; i < operands; i++)
448 printf (" operand%d = operands[%d];\n", i, i);
449 for (; i <= max_dup_opno; i++)
450 printf (" operand%d = operands[%d];\n", i, i);
454 /* Output code to construct the rtl for the instruction bodies.
455 Use emit_insn to add them to the sequence being accumulated.
456 But don't do this if the user's code has set `no_more' nonzero. */
458 for (i = 0; i < XVECLEN (expand, 1); i++)
460 rtx next = XVECEXP (expand, 1, i);
461 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
462 || (GET_CODE (next) == PARALLEL
463 && GET_CODE (XVECEXP (next, 0, 0)) == SET
464 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
465 || GET_CODE (next) == RETURN)
466 printf (" emit_jump_insn (");
467 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
468 || GET_CODE (next) == CALL
469 || (GET_CODE (next) == PARALLEL
470 && GET_CODE (XVECEXP (next, 0, 0)) == SET
471 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
472 || (GET_CODE (next) == PARALLEL
473 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
474 printf (" emit_call_insn (");
475 else if (GET_CODE (next) == CODE_LABEL)
476 printf (" emit_label (");
477 else if (GET_CODE (next) == MATCH_OPERAND
478 || GET_CODE (next) == MATCH_OPERATOR
479 || GET_CODE (next) == MATCH_PARALLEL
480 || GET_CODE (next) == MATCH_OP_DUP
481 || GET_CODE (next) == MATCH_DUP
482 || GET_CODE (next) == PARALLEL)
485 printf (" emit_insn (");
488 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
489 && GET_CODE (SET_SRC (next)) == LABEL_REF)
490 printf (" emit_barrier ();");
493 /* Call `gen_sequence' to make a SEQUENCE out of all the
494 insns emitted within this gen_... function. */
496 printf (" _done:\n");
497 printf (" _val = gen_sequence ();\n");
498 printf (" _fail:\n");
499 printf (" end_sequence ();\n");
500 printf (" return _val;\n}\n\n");
503 /* Like gen_expand, but generates a SEQUENCE. */
511 if (XVEC (split, 0) == 0)
512 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
513 else if (XVEC (split, 2) == 0)
514 fatal ("define_split (definition %d) lacks a replacement pattern",
517 /* Find out how many operands this function has. */
519 max_operand_vec (split, 2);
520 operands = MAX (max_opno, max_dup_opno) + 1;
522 /* Output the function name and argument declarations. */
523 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
527 /* Declare all local variables. */
528 for (i = 0; i < operands; i++)
529 printf (" rtx operand%d;\n", i);
530 printf (" rtx _val;\n");
531 printf (" start_sequence ();\n");
533 /* The fourth operand of DEFINE_SPLIT is some code to be executed
534 before the actual construction. */
537 printf ("%s\n", XSTR (split, 3));
539 /* Output code to copy the arguments back out of `operands' */
540 for (i = 0; i < operands; i++)
541 printf (" operand%d = operands[%d];\n", i, i);
543 /* Output code to construct the rtl for the instruction bodies.
544 Use emit_insn to add them to the sequence being accumulated.
545 But don't do this if the user's code has set `no_more' nonzero. */
547 for (i = 0; i < XVECLEN (split, 2); i++)
549 rtx next = XVECEXP (split, 2, i);
550 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
551 || (GET_CODE (next) == PARALLEL
552 && GET_CODE (XVECEXP (next, 0, 0)) == SET
553 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
554 || GET_CODE (next) == RETURN)
555 printf (" emit_jump_insn (");
556 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
557 || GET_CODE (next) == CALL
558 || (GET_CODE (next) == PARALLEL
559 && GET_CODE (XVECEXP (next, 0, 0)) == SET
560 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
561 || (GET_CODE (next) == PARALLEL
562 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
563 printf (" emit_call_insn (");
564 else if (GET_CODE (next) == CODE_LABEL)
565 printf (" emit_label (");
566 else if (GET_CODE (next) == MATCH_OPERAND
567 || GET_CODE (next) == MATCH_OPERATOR
568 || GET_CODE (next) == MATCH_PARALLEL
569 || GET_CODE (next) == MATCH_OP_DUP
570 || GET_CODE (next) == MATCH_DUP
571 || GET_CODE (next) == PARALLEL)
574 printf (" emit_insn (");
577 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
578 && GET_CODE (SET_SRC (next)) == LABEL_REF)
579 printf (" emit_barrier ();");
582 /* Call `gen_sequence' to make a SEQUENCE out of all the
583 insns emitted within this gen_... function. */
585 printf (" _done:\n");
586 printf (" _val = gen_sequence ();\n");
587 printf (" _fail:\n");
588 printf (" end_sequence ();\n");
589 printf (" return _val;\n}\n\n");
592 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
593 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
594 the end of the vector. */
597 output_add_clobbers ()
599 struct clobber_pat *clobber;
600 struct clobber_ent *ent;
603 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
604 printf (" rtx pattern;\n int insn_code_number;\n");
606 printf (" int i;\n\n");
607 printf (" switch (insn_code_number)\n");
610 for (clobber = clobber_list; clobber; clobber = clobber->next)
612 for (ent = clobber->insns; ent; ent = ent->next)
613 printf (" case %d:\n", ent->code_number);
615 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
617 printf (" XVECEXP (pattern, 0, %d) = ", i);
618 gen_exp (XVECEXP (clobber->pattern, 1, i));
622 printf (" break;\n\n");
625 printf (" default:\n");
626 printf (" abort ();\n");
631 /* Write a function, init_mov_optab, that is called to set up entries
632 in mov_optab for EXTRA_CC_MODES. */
635 output_init_mov_optab ()
637 #ifdef EXTRA_CC_NAMES
638 static char *cc_names[] = { EXTRA_CC_NAMES };
642 printf ("\nvoid\ninit_mov_optab ()\n{\n");
644 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
646 printf ("#ifdef HAVE_mov");
647 for (p = cc_names[i]; *p; p++)
648 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
650 printf (" if (HAVE_mov");
651 for (p = cc_names[i]; *p; p++)
652 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
654 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
656 for (p = cc_names[i]; *p; p++)
657 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
658 printf (";\n#endif\n");
669 register char *val = (char *) malloc (size);
672 fatal ("virtual memory exhausted");
682 char *result = (char *) realloc (ptr, size);
684 fatal ("virtual memory exhausted");
692 fprintf (stderr, "genemit: ");
693 fprintf (stderr, s, a1, a2);
694 fprintf (stderr, "\n");
695 exit (FATAL_EXIT_CODE);
698 /* More 'friendly' abort that prints the line and file.
699 config.h can #define abort fancy_abort if you like that sort of thing. */
704 fatal ("Internal gcc abort.");
716 obstack_init (rtl_obstack);
719 fatal ("No input file name.");
721 infile = fopen (argv[1], "r");
725 exit (FATAL_EXIT_CODE);
730 /* Assign sequential codes to all entries in the machine description
731 in parallel with the tables in insn-output.c. */
733 insn_code_number = 0;
734 insn_index_number = 0;
736 printf ("/* Generated automatically by the program `genemit'\n\
737 from the machine description file `md'. */\n\n");
739 printf ("#include \"config.h\"\n");
740 printf ("#include \"rtl.h\"\n");
741 printf ("#include \"expr.h\"\n");
742 printf ("#include \"real.h\"\n");
743 printf ("#include \"output.h\"\n");
744 printf ("#include \"insn-config.h\"\n\n");
745 printf ("#include \"insn-flags.h\"\n\n");
746 printf ("#include \"insn-codes.h\"\n\n");
747 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
748 printf ("extern rtx recog_operand[];\n");
749 printf ("#define operands emit_operand\n\n");
750 printf ("#define FAIL goto _fail\n\n");
751 printf ("#define DONE goto _done\n\n");
753 /* Read the machine description. */
757 c = read_skip_spaces (infile);
762 desc = read_rtx (infile);
763 if (GET_CODE (desc) == DEFINE_INSN)
768 if (GET_CODE (desc) == DEFINE_EXPAND)
773 if (GET_CODE (desc) == DEFINE_SPLIT)
778 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
785 /* Write out the routine to add CLOBBERs to a pattern. */
786 output_add_clobbers ();
788 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
789 output_init_mov_optab ();
792 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);