1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 88, 91, 94, 95, 97, 1998 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
31 static struct obstack obstack;
32 struct obstack *rtl_obstack = &obstack;
34 #define obstack_chunk_alloc xmalloc
35 #define obstack_chunk_free free
37 #ifdef NEED_DECLARATION_FREE
40 extern rtx read_rtx ();
47 static int max_dup_opno;
48 static int register_constraints;
49 static int insn_code_number;
50 static int insn_index_number;
52 /* Data structure for recording the patterns of insns that have CLOBBERs.
53 We use this to output a function that adds these CLOBBERs to a
54 previously-allocated PARALLEL expression. */
58 struct clobber_ent *insns;
61 struct clobber_pat *next;
64 /* Records one insn that uses the clobber list. */
68 int code_number; /* Counts only insns. */
69 struct clobber_ent *next;
76 register RTX_CODE code;
86 if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
87 register_constraints = 1;
88 if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
89 register_constraints = 1;
90 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
91 || code == MATCH_PARALLEL)
92 max_opno = MAX (max_opno, XINT (x, 0));
93 if (code == MATCH_DUP || code == MATCH_OP_DUP || code == MATCH_PAR_DUP)
94 max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
96 fmt = GET_RTX_FORMAT (code);
97 len = GET_RTX_LENGTH (code);
98 for (i = 0; i < len; i++)
100 if (fmt[i] == 'e' || fmt[i] == 'u')
101 max_operand_1 (XEXP (x, i));
102 else if (fmt[i] == 'E')
105 for (j = 0; j < XVECLEN (x, i); j++)
106 max_operand_1 (XVECEXP (x, i, j));
112 max_operand_vec (insn, arg)
116 register int len = XVECLEN (insn, arg);
122 for (i = 0; i < len; i++)
123 max_operand_1 (XVECEXP (insn, arg, i));
133 for (p1 = GET_RTX_NAME (code); *p1; p1++)
135 if (*p1 >= 'a' && *p1 <= 'z')
136 putchar (*p1 + 'A' - 'a');
142 /* Print a C expression to construct an RTX just like X,
143 substituting any operand references appearing within. */
149 register RTX_CODE code;
166 printf ("operand%d", XINT (x, 0));
170 printf ("gen_rtx (GET_CODE (operand%d), GET_MODE (operand%d)",
171 XINT (x, 0), XINT (x, 0));
172 for (i = 0; i < XVECLEN (x, 1); i++)
175 gen_exp (XVECEXP (x, 1, i));
181 printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
182 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
183 for (i = 0; i < XVECLEN (x, 2); i++)
186 gen_exp (XVECEXP (x, 2, i));
193 printf ("operand%d", XINT (x, 0));
197 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
201 fatal ("ADDRESS expression code used in named instruction pattern");
213 printf ("const0_rtx");
214 else if (INTVAL (x) == 1)
215 printf ("const1_rtx");
216 else if (INTVAL (x) == -1)
217 printf ("constm1_rtx");
218 else if (INTVAL (x) == STORE_FLAG_VALUE)
219 printf ("const_true_rtx");
222 printf ("GEN_INT (");
223 printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
229 /* These shouldn't be written in MD files. Instead, the appropriate
230 routines in varasm.c should be called. */
239 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
241 fmt = GET_RTX_FORMAT (code);
242 len = GET_RTX_LENGTH (code);
243 for (i = 0; i < len; i++)
248 if (fmt[i] == 'e' || fmt[i] == 'u')
249 gen_exp (XEXP (x, i));
250 else if (fmt[i] == 'i')
251 printf ("%u", XINT (x, i));
252 else if (fmt[i] == 's')
253 printf ("\"%s\"", XSTR (x, i));
254 else if (fmt[i] == 'E')
257 printf ("gen_rtvec (%d", XVECLEN (x, i));
258 for (j = 0; j < XVECLEN (x, i); j++)
261 gen_exp (XVECEXP (x, i, j));
271 /* Generate the `gen_...' function for a DEFINE_INSN. */
280 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
281 registers or MATCH_SCRATCHes. If so, store away the information for
286 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
287 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
288 || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
289 && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
292 if (i != XVECLEN (insn, 1) - 1)
294 register struct clobber_pat *p;
295 register struct clobber_ent *link
296 = (struct clobber_ent *) xmalloc (sizeof (struct clobber_ent));
299 link->code_number = insn_code_number;
301 /* See if any previous CLOBBER_LIST entry is the same as this
304 for (p = clobber_list; p; p = p->next)
306 if (p->first_clobber != i + 1
307 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
310 for (j = i + 1; j < XVECLEN (insn, 1); j++)
312 rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
313 rtx new = XEXP (XVECEXP (insn, 1, j), 0);
315 /* OLD and NEW are the same if both are to be a SCRATCH
317 or if both are registers of the same mode and number. */
318 if (! (GET_MODE (old) == GET_MODE (new)
319 && ((GET_CODE (old) == MATCH_SCRATCH
320 && GET_CODE (new) == MATCH_SCRATCH)
321 || (GET_CODE (old) == REG && GET_CODE (new) == REG
322 && REGNO (old) == REGNO (new)))))
326 if (j == XVECLEN (insn, 1))
332 p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
336 p->first_clobber = i + 1;
337 p->next = clobber_list;
341 link->next = p->insns;
346 /* Don't mention instructions whose names are the null string
347 or begin with '*'. They are in the machine description just
349 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
352 /* Find out how many operands this function has,
353 and also whether any of them have register constraints. */
354 register_constraints = 0;
355 operands = max_operand_vec (insn, 1);
356 if (max_dup_opno >= operands)
357 fatal ("match_dup operand number has no match_operand");
359 /* Output the function name and argument declarations. */
360 printf ("rtx\ngen_%s (", XSTR (insn, 0));
361 for (i = 0; i < operands; i++)
362 printf (i ? ", operand%d" : "operand%d", i);
364 for (i = 0; i < operands; i++)
365 printf (" rtx operand%d;\n", i);
368 /* Output code to construct and return the rtl for the instruction body */
370 if (XVECLEN (insn, 1) == 1)
373 gen_exp (XVECEXP (insn, 1, 0));
378 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
379 for (i = 0; i < XVECLEN (insn, 1); i++)
382 gen_exp (XVECEXP (insn, 1, i));
384 printf ("));\n}\n\n");
388 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
397 if (strlen (XSTR (expand, 0)) == 0)
398 fatal ("define_expand lacks a name");
399 if (XVEC (expand, 1) == 0)
400 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
402 /* Find out how many operands this function has,
403 and also whether any of them have register constraints. */
404 register_constraints = 0;
406 operands = max_operand_vec (expand, 1);
408 /* Output the function name and argument declarations. */
409 printf ("rtx\ngen_%s (", XSTR (expand, 0));
410 for (i = 0; i < operands; i++)
411 printf (i ? ", operand%d" : "operand%d", i);
413 for (i = 0; i < operands; i++)
414 printf (" rtx operand%d;\n", i);
417 /* If we don't have any C code to write, only one insn is being written,
418 and no MATCH_DUPs are present, we can just return the desired insn
419 like we do for a DEFINE_INSN. This saves memory. */
420 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
421 && operands > max_dup_opno
422 && XVECLEN (expand, 1) == 1)
425 gen_exp (XVECEXP (expand, 1, 0));
430 /* For each operand referred to only with MATCH_DUPs,
431 make a local variable. */
432 for (i = operands; i <= max_dup_opno; i++)
433 printf (" rtx operand%d;\n", i);
434 if (operands > 0 || max_dup_opno >= 0)
435 printf (" rtx operands[%d];\n", MAX (operands, max_dup_opno + 1));
436 printf (" rtx _val = 0;\n");
437 printf (" start_sequence ();\n");
439 /* The fourth operand of DEFINE_EXPAND is some code to be executed
440 before the actual construction.
441 This code expects to refer to `operands'
442 just as the output-code in a DEFINE_INSN does,
443 but here `operands' is an automatic array.
444 So copy the operand values there before executing it. */
445 if (XSTR (expand, 3) && *XSTR (expand, 3))
447 /* Output code to copy the arguments into `operands'. */
448 for (i = 0; i < operands; i++)
449 printf (" operands[%d] = operand%d;\n", i, i);
451 /* Output the special code to be executed before the sequence
453 printf ("%s\n", XSTR (expand, 3));
455 /* Output code to copy the arguments back out of `operands'
456 (unless we aren't going to use them at all). */
457 if (XVEC (expand, 1) != 0)
459 for (i = 0; i < operands; i++)
460 printf (" operand%d = operands[%d];\n", i, i);
461 for (; i <= max_dup_opno; i++)
462 printf (" operand%d = operands[%d];\n", i, i);
466 /* Output code to construct the rtl for the instruction bodies.
467 Use emit_insn to add them to the sequence being accumulated.
468 But don't do this if the user's code has set `no_more' nonzero. */
470 for (i = 0; i < XVECLEN (expand, 1); i++)
472 rtx next = XVECEXP (expand, 1, i);
473 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
474 || (GET_CODE (next) == PARALLEL
475 && GET_CODE (XVECEXP (next, 0, 0)) == SET
476 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
477 || GET_CODE (next) == RETURN)
478 printf (" emit_jump_insn (");
479 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
480 || GET_CODE (next) == CALL
481 || (GET_CODE (next) == PARALLEL
482 && GET_CODE (XVECEXP (next, 0, 0)) == SET
483 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
484 || (GET_CODE (next) == PARALLEL
485 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
486 printf (" emit_call_insn (");
487 else if (GET_CODE (next) == CODE_LABEL)
488 printf (" emit_label (");
489 else if (GET_CODE (next) == MATCH_OPERAND
490 || GET_CODE (next) == MATCH_OPERATOR
491 || GET_CODE (next) == MATCH_PARALLEL
492 || GET_CODE (next) == MATCH_OP_DUP
493 || GET_CODE (next) == MATCH_DUP
494 || GET_CODE (next) == PARALLEL)
497 printf (" emit_insn (");
500 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
501 && GET_CODE (SET_SRC (next)) == LABEL_REF)
502 printf (" emit_barrier ();");
505 /* Call `gen_sequence' to make a SEQUENCE out of all the
506 insns emitted within this gen_... function. */
508 printf (" _done:\n");
509 printf (" _val = gen_sequence ();\n");
510 printf (" _fail:\n");
511 printf (" end_sequence ();\n");
512 printf (" return _val;\n}\n\n");
515 /* Like gen_expand, but generates a SEQUENCE. */
524 if (XVEC (split, 0) == 0)
525 fatal ("define_split (definition %d) lacks a pattern", insn_index_number);
526 else if (XVEC (split, 2) == 0)
527 fatal ("define_split (definition %d) lacks a replacement pattern",
530 /* Find out how many operands this function has. */
532 max_operand_vec (split, 2);
533 operands = MAX (max_opno, max_dup_opno) + 1;
535 /* Output the function name and argument declarations. */
536 printf ("rtx\ngen_split_%d (operands)\n rtx *operands;\n",
540 /* Declare all local variables. */
541 for (i = 0; i < operands; i++)
542 printf (" rtx operand%d;\n", i);
543 printf (" rtx _val = 0;\n");
544 printf (" start_sequence ();\n");
546 /* The fourth operand of DEFINE_SPLIT is some code to be executed
547 before the actual construction. */
550 printf ("%s\n", XSTR (split, 3));
552 /* Output code to copy the arguments back out of `operands' */
553 for (i = 0; i < operands; i++)
554 printf (" operand%d = operands[%d];\n", i, i);
556 /* Output code to construct the rtl for the instruction bodies.
557 Use emit_insn to add them to the sequence being accumulated.
558 But don't do this if the user's code has set `no_more' nonzero. */
560 for (i = 0; i < XVECLEN (split, 2); i++)
562 rtx next = XVECEXP (split, 2, i);
563 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
564 || (GET_CODE (next) == PARALLEL
565 && GET_CODE (XVECEXP (next, 0, 0)) == SET
566 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
567 || GET_CODE (next) == RETURN)
568 printf (" emit_jump_insn (");
569 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
570 || GET_CODE (next) == CALL
571 || (GET_CODE (next) == PARALLEL
572 && GET_CODE (XVECEXP (next, 0, 0)) == SET
573 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
574 || (GET_CODE (next) == PARALLEL
575 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
576 printf (" emit_call_insn (");
577 else if (GET_CODE (next) == CODE_LABEL)
578 printf (" emit_label (");
579 else if (GET_CODE (next) == MATCH_OPERAND
580 || GET_CODE (next) == MATCH_OPERATOR
581 || GET_CODE (next) == MATCH_PARALLEL
582 || GET_CODE (next) == MATCH_OP_DUP
583 || GET_CODE (next) == MATCH_DUP
584 || GET_CODE (next) == PARALLEL)
587 printf (" emit_insn (");
590 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
591 && GET_CODE (SET_SRC (next)) == LABEL_REF)
592 printf (" emit_barrier ();");
595 /* Call `gen_sequence' to make a SEQUENCE out of all the
596 insns emitted within this gen_... function. */
598 printf (" _done:\n");
599 printf (" _val = gen_sequence ();\n");
600 printf (" _fail:\n");
601 printf (" end_sequence ();\n");
602 printf (" return _val;\n}\n\n");
605 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
606 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
607 the end of the vector. */
610 output_add_clobbers ()
612 struct clobber_pat *clobber;
613 struct clobber_ent *ent;
616 printf ("\n\nvoid\nadd_clobbers (pattern, insn_code_number)\n");
617 printf (" rtx pattern;\n int insn_code_number;\n");
619 printf (" int i;\n\n");
620 printf (" switch (insn_code_number)\n");
623 for (clobber = clobber_list; clobber; clobber = clobber->next)
625 for (ent = clobber->insns; ent; ent = ent->next)
626 printf (" case %d:\n", ent->code_number);
628 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
630 printf (" XVECEXP (pattern, 0, %d) = ", i);
631 gen_exp (XVECEXP (clobber->pattern, 1, i));
635 printf (" break;\n\n");
638 printf (" default:\n");
639 printf (" abort ();\n");
644 /* Write a function, init_mov_optab, that is called to set up entries
645 in mov_optab for EXTRA_CC_MODES. */
648 output_init_mov_optab ()
650 #ifdef EXTRA_CC_NAMES
651 static char *cc_names[] = { EXTRA_CC_NAMES };
655 printf ("\nvoid\ninit_mov_optab ()\n{\n");
657 for (i = 0; i < sizeof cc_names / sizeof cc_names[0]; i++)
659 printf ("#ifdef HAVE_mov");
660 for (p = cc_names[i]; *p; p++)
661 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
663 printf (" if (HAVE_mov");
664 for (p = cc_names[i]; *p; p++)
665 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
667 printf (" mov_optab->handlers[(int) %smode].insn_code = CODE_FOR_mov",
669 for (p = cc_names[i]; *p; p++)
670 printf ("%c", *p >= 'A' && *p <= 'Z' ? *p - 'A' + 'a' : *p);
671 printf (";\n#endif\n");
682 register char *val = (char *) malloc (size);
685 fatal ("virtual memory exhausted");
695 char *result = (char *) realloc (ptr, size);
697 fatal ("virtual memory exhausted");
705 fprintf (stderr, "genemit: ");
706 fprintf (stderr, s, a1, a2);
707 fprintf (stderr, "\n");
708 exit (FATAL_EXIT_CODE);
711 /* More 'friendly' abort that prints the line and file.
712 config.h can #define abort fancy_abort if you like that sort of thing. */
717 fatal ("Internal gcc abort.");
729 obstack_init (rtl_obstack);
732 fatal ("No input file name.");
734 infile = fopen (argv[1], "r");
738 exit (FATAL_EXIT_CODE);
743 /* Assign sequential codes to all entries in the machine description
744 in parallel with the tables in insn-output.c. */
746 insn_code_number = 0;
747 insn_index_number = 0;
749 printf ("/* Generated automatically by the program `genemit'\n\
750 from the machine description file `md'. */\n\n");
752 printf ("#include \"config.h\"\n");
753 printf ("#include <stdio.h>\n");
754 printf ("#include \"rtl.h\"\n");
755 printf ("#include \"expr.h\"\n");
756 printf ("#include \"real.h\"\n");
757 printf ("#include \"flags.h\"\n");
758 printf ("#include \"output.h\"\n");
759 printf ("#include \"insn-config.h\"\n\n");
760 printf ("#include \"insn-flags.h\"\n\n");
761 printf ("#include \"insn-codes.h\"\n\n");
762 printf ("extern char *insn_operand_constraint[][MAX_RECOG_OPERANDS];\n\n");
763 printf ("extern rtx recog_operand[];\n");
764 printf ("#define operands emit_operand\n\n");
765 printf ("#define FAIL goto _fail\n\n");
766 printf ("#define DONE goto _done\n\n");
768 /* Read the machine description. */
772 c = read_skip_spaces (infile);
777 desc = read_rtx (infile);
778 if (GET_CODE (desc) == DEFINE_INSN)
783 if (GET_CODE (desc) == DEFINE_EXPAND)
788 if (GET_CODE (desc) == DEFINE_SPLIT)
793 if (GET_CODE (desc) == DEFINE_PEEPHOLE)
800 /* Write out the routine to add CLOBBERs to a pattern. */
801 output_add_clobbers ();
803 /* Write the routine to initialize mov_optab for the EXTRA_CC_MODES. */
804 output_init_mov_optab ();
807 exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);