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
305 or if both are registers of the same mode and number. */
306 if (! (GET_MODE (old) == GET_MODE (new)
307 && ((GET_CODE (old) == MATCH_SCRATCH
308 && GET_CODE (new) == MATCH_SCRATCH)
309 || (GET_CODE (old) == REG && GET_CODE (new) == REG
310 && REGNO (old) == REGNO (new)))))
314 if (j == XVECLEN (insn, 1))
320 p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
324 p->first_clobber = i + 1;
325 p->next = clobber_list;
329 link->next = p->insns;
334 /* Don't mention instructions whose names are the null string.
335 They are in the machine description just to be recognized. */
336 if (strlen (XSTR (insn, 0)) == 0)
339 /* Find out how many operands this function has,
340 and also whether any of them have register constraints. */
341 register_constraints = 0;
342 operands = max_operand_vec (insn, 1);
343 if (max_dup_opno >= operands)
344 fatal ("match_dup operand number has no match_operand");
346 /* Output the function name and argument declarations. */
347 printf ("rtx\ngen_%s (", XSTR (insn, 0));
348 for (i = 0; i < operands; i++)
349 printf (i ? ", operand%d" : "operand%d", i);
351 for (i = 0; i < operands; i++)
352 printf (" rtx operand%d;\n", i);
355 /* Output code to construct and return the rtl for the instruction body */
357 if (XVECLEN (insn, 1) == 1)
360 gen_exp (XVECEXP (insn, 1, 0));
365 printf (" return gen_rtx (PARALLEL, VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
366 for (i = 0; i < XVECLEN (insn, 1); i++)
369 gen_exp (XVECEXP (insn, 1, i));
371 printf ("));\n}\n\n");
375 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
384 if (strlen (XSTR (expand, 0)) == 0)
385 fatal ("define_expand lacks a name");
386 if (XVEC (expand, 1) == 0)
387 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
389 /* Find out how many operands this function has,
390 and also whether any of them have register constraints. */
391 register_constraints = 0;
393 operands = max_operand_vec (expand, 1);
395 /* Output the function name and argument declarations. */
396 printf ("rtx\ngen_%s (", XSTR (expand, 0));
397 for (i = 0; i < operands; i++)
398 printf (i ? ", operand%d" : "operand%d", i);
400 for (i = 0; i < operands; i++)
401 printf (" rtx operand%d;\n", i);
404 /* If we don't have any C code to write, only one insn is being written,
405 and no MATCH_DUPs are present, we can just return the desired insn
406 like we do for a DEFINE_INSN. This saves memory. */
407 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
408 && operands > max_dup_opno
409 && XVECLEN (expand, 1) == 1)
412 gen_exp (XVECEXP (expand, 1, 0));
417 /* For each operand referred to only with MATCH_DUPs,
418 make a local variable. */
419 for (i = operands; i <= max_dup_opno; i++)
420 printf (" rtx operand%d;\n", i);
421 if (operands > 0 || max_dup_opno >= 0)
422 printf (" rtx operands[%d];\n", MAX (operands, max_dup_opno + 1));
423 printf (" rtx _val = 0;\n");
424 printf (" start_sequence ();\n");
426 /* The fourth operand of DEFINE_EXPAND is some code to be executed
427 before the actual construction.
428 This code expects to refer to `operands'
429 just as the output-code in a DEFINE_INSN does,
430 but here `operands' is an automatic array.
431 So copy the operand values there before executing it. */
432 if (XSTR (expand, 3) && *XSTR (expand, 3))
434 /* Output code to copy the arguments into `operands'. */
435 for (i = 0; i < operands; i++)
436 printf (" operands[%d] = operand%d;\n", i, i);
438 /* Output the special code to be executed before the sequence
440 printf ("%s\n", XSTR (expand, 3));
442 /* Output code to copy the arguments back out of `operands'
443 (unless we aren't going to use them at all). */
444 if (XVEC (expand, 1) != 0)
446 for (i = 0; i < operands; i++)
447 printf (" operand%d = operands[%d];\n", i, i);
448 for (; i <= max_dup_opno; i++)
449 printf (" operand%d = operands[%d];\n", i, i);
453 /* Output code to construct the rtl for the instruction bodies.
454 Use emit_insn to add them to the sequence being accumulated.
455 But don't do this if the user's code has set `no_more' nonzero. */
457 for (i = 0; i < XVECLEN (expand, 1); i++)
459 rtx next = XVECEXP (expand, 1, i);
460 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
461 || (GET_CODE (next) == PARALLEL
462 && GET_CODE (XVECEXP (next, 0, 0)) == SET
463 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
464 || GET_CODE (next) == RETURN)
465 printf (" emit_jump_insn (");
466 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
467 || GET_CODE (next) == CALL
468 || (GET_CODE (next) == PARALLEL
469 && GET_CODE (XVECEXP (next, 0, 0)) == SET
470 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
471 || (GET_CODE (next) == PARALLEL
472 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
473 printf (" emit_call_insn (");
474 else if (GET_CODE (next) == CODE_LABEL)
475 printf (" emit_label (");
476 else if (GET_CODE (next) == MATCH_OPERAND
477 || GET_CODE (next) == MATCH_OPERATOR
478 || GET_CODE (next) == MATCH_PARALLEL
479 || GET_CODE (next) == MATCH_OP_DUP
480 || GET_CODE (next) == MATCH_DUP
481 || GET_CODE (next) == PARALLEL)
484 printf (" emit_insn (");
487 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
488 && GET_CODE (SET_SRC (next)) == LABEL_REF)
489 printf (" emit_barrier ();");
492 /* Call `gen_sequence' to make a SEQUENCE out of all the
493 insns emitted within this gen_... function. */
495 printf (" _done:\n");
496 printf (" _val = gen_sequence ();\n");
497 printf (" _fail:\n");
498 printf (" end_sequence ();\n");
499 printf (" return _val;\n}\n\n");
502 /* Like gen_expand, but generates a SEQUENCE. */
510 if (XVEC (split, 0) == 0)
511 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
512 else if (XVEC (split, 2) == 0)
513 fatal ("define_split (definition %d) lacks a replacement pattern",
516 /* Find out how many operands this function has. */
518 max_operand_vec (split, 2);
519 operands = MAX (max_opno, max_dup_opno) + 1;
521 /* Output the function name and argument declarations. */
522 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
526 /* Declare all local variables. */
527 for (i = 0; i < operands; i++)
528 printf (" rtx operand%d;\n", i);
529 printf (" rtx _val;\n");
530 printf (" start_sequence ();\n");
532 /* The fourth operand of DEFINE_SPLIT is some code to be executed
533 before the actual construction. */
536 printf ("%s\n", XSTR (split, 3));
538 /* Output code to copy the arguments back out of `operands' */
539 for (i = 0; i < operands; i++)
540 printf (" operand%d = operands[%d];\n", i, i);
542 /* Output code to construct the rtl for the instruction bodies.
543 Use emit_insn to add them to the sequence being accumulated.
544 But don't do this if the user's code has set `no_more' nonzero. */
546 for (i = 0; i < XVECLEN (split, 2); i++)
548 rtx next = XVECEXP (split, 2, i);
549 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
550 || (GET_CODE (next) == PARALLEL
551 && GET_CODE (XVECEXP (next, 0, 0)) == SET
552 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
553 || GET_CODE (next) == RETURN)
554 printf (" emit_jump_insn (");
555 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
556 || GET_CODE (next) == CALL
557 || (GET_CODE (next) == PARALLEL
558 && GET_CODE (XVECEXP (next, 0, 0)) == SET
559 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
560 || (GET_CODE (next) == PARALLEL
561 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
562 printf (" emit_call_insn (");
563 else if (GET_CODE (next) == CODE_LABEL)
564 printf (" emit_label (");
565 else if (GET_CODE (next) == MATCH_OPERAND
566 || GET_CODE (next) == MATCH_OPERATOR
567 || GET_CODE (next) == MATCH_PARALLEL
568 || GET_CODE (next) == MATCH_OP_DUP
569 || GET_CODE (next) == MATCH_DUP
570 || GET_CODE (next) == PARALLEL)
573 printf (" emit_insn (");
576 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
577 && GET_CODE (SET_SRC (next)) == LABEL_REF)
578 printf (" emit_barrier ();");
581 /* Call `gen_sequence' to make a SEQUENCE out of all the
582 insns emitted within this gen_... function. */
584 printf (" _done:\n");
585 printf (" _val = gen_sequence ();\n");
586 printf (" _fail:\n");
587 printf (" end_sequence ();\n");
588 printf (" return _val;\n}\n\n");
591 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
592 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
593 the end of the vector. */
596 output_add_clobbers ()
598 struct clobber_pat *clobber;
599 struct clobber_ent *ent;
602 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
603 printf (" rtx pattern;\n int insn_code_number;\n");
605 printf (" int i;\n\n");
606 printf (" switch (insn_code_number)\n");
609 for (clobber = clobber_list; clobber; clobber = clobber->next)
611 for (ent = clobber->insns; ent; ent = ent->next)
612 printf (" case %d:\n", ent->code_number);
614 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
616 printf (" XVECEXP (pattern, 0, %d) = ", i);
617 gen_exp (XVECEXP (clobber->pattern, 1, i));
621 printf (" break;\n\n");
624 printf (" default:\n");
625 printf (" abort ();\n");
630 /* Write a function, init_mov_optab, that is called to set up entries
631 in mov_optab for EXTRA_CC_MODES. */
634 output_init_mov_optab ()
636 #ifdef EXTRA_CC_NAMES
637 static char *cc_names[] = { EXTRA_CC_NAMES };
641 printf ("\nvoid\ninit_mov_optab ()\n{\n");
643 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
645 printf ("#ifdef HAVE_mov");
646 for (p = cc_names[i]; *p; p++)
647 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
649 printf (" if (HAVE_mov");
650 for (p = cc_names[i]; *p; p++)
651 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
653 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
655 for (p = cc_names[i]; *p; p++)
656 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
657 printf (";\n#endif\n");
668 register char *val = (char *) malloc (size);
671 fatal ("virtual memory exhausted");
681 char *result = (char *) realloc (ptr, size);
683 fatal ("virtual memory exhausted");
691 fprintf (stderr, "genemit: ");
692 fprintf (stderr, s, a1, a2);
693 fprintf (stderr, "\n");
694 exit (FATAL_EXIT_CODE);
697 /* More 'friendly' abort that prints the line and file.
698 config.h can #define abort fancy_abort if you like that sort of thing. */
703 fatal ("Internal gcc abort.");
713 extern rtx read_rtx ();
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);