1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987, 1988, 1991, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
3 2003, 2004, 2005, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "gensupport.h"
32 static int insn_code_number;
33 static int insn_index_number;
35 /* Data structure for recording the patterns of insns that have CLOBBERs.
36 We use this to output a function that adds these CLOBBERs to a
37 previously-allocated PARALLEL expression. */
41 struct clobber_ent *insns;
44 struct clobber_pat *next;
48 /* Records one insn that uses the clobber list. */
52 int code_number; /* Counts only insns. */
53 struct clobber_ent *next;
56 static void print_code (RTX_CODE);
57 static void gen_exp (rtx, enum rtx_code, char *);
58 static void gen_insn (rtx, int);
59 static void gen_expand (rtx);
60 static void gen_split (rtx);
61 static void output_add_clobbers (void);
62 static void output_added_clobbers_hard_reg_p (void);
63 static void gen_rtx_scratch (rtx, enum rtx_code);
64 static void output_peephole2_scratches (rtx);
68 print_code (RTX_CODE code)
71 for (p1 = GET_RTX_NAME (code); *p1; p1++)
72 putchar (TOUPPER(*p1));
76 gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
78 if (subroutine_type == DEFINE_PEEPHOLE2)
80 printf ("operand%d", XINT (x, 0));
84 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
88 /* Print a C expression to construct an RTX just like X,
89 substituting any operand references appearing within. */
92 gen_exp (rtx x, enum rtx_code subroutine_type, char *used)
113 if (used[XINT (x, 0)])
115 printf ("copy_rtx (operand%d)", XINT (x, 0));
118 used[XINT (x, 0)] = 1;
120 printf ("operand%d", XINT (x, 0));
124 printf ("gen_rtx_fmt_");
125 for (i = 0; i < XVECLEN (x, 1); i++)
127 printf (" (GET_CODE (operand%d), ", XINT (x, 0));
128 if (GET_MODE (x) == VOIDmode)
129 printf ("GET_MODE (operand%d)", XINT (x, 0));
131 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
132 for (i = 0; i < XVECLEN (x, 1); i++)
135 gen_exp (XVECEXP (x, 1, i), subroutine_type, used);
141 printf ("gen_rtx_fmt_");
142 for (i = 0; i < XVECLEN (x, 2); i++)
144 printf (" (GET_CODE (operand%d)", XINT (x, 0));
145 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
146 for (i = 0; i < XVECLEN (x, 2); i++)
149 gen_exp (XVECEXP (x, 2, i), subroutine_type, used);
156 printf ("operand%d", XINT (x, 0));
160 gen_rtx_scratch (x, subroutine_type);
164 fatal ("ADDRESS expression code used in named instruction pattern");
173 printf ("simple_return_rtx");
176 if (REG_P (XEXP (x, 0)))
178 printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
179 REGNO (XEXP (x, 0)));
190 printf ("const0_rtx");
191 else if (INTVAL (x) == 1)
192 printf ("const1_rtx");
193 else if (INTVAL (x) == -1)
194 printf ("constm1_rtx");
195 else if (-MAX_SAVED_CONST_INT <= INTVAL (x)
196 && INTVAL (x) <= MAX_SAVED_CONST_INT)
197 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
199 else if (INTVAL (x) == STORE_FLAG_VALUE)
200 printf ("const_true_rtx");
203 printf ("GEN_INT (");
204 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
211 /* These shouldn't be written in MD files. Instead, the appropriate
212 routines in varasm.c should be called. */
221 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
223 fmt = GET_RTX_FORMAT (code);
224 len = GET_RTX_LENGTH (code);
225 for (i = 0; i < len; i++)
233 gen_exp (XEXP (x, i), subroutine_type, used);
237 printf ("%u", XINT (x, i));
241 printf ("\"%s\"", XSTR (x, i));
247 printf ("gen_rtvec (%d", XVECLEN (x, i));
248 for (j = 0; j < XVECLEN (x, i); j++)
251 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
264 /* Generate the `gen_...' function for a DEFINE_INSN. */
267 gen_insn (rtx insn, int lineno)
269 struct pattern_stats stats;
272 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
273 registers or MATCH_SCRATCHes. If so, store away the information for
278 int has_hard_reg = 0;
280 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
282 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
285 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
287 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
291 if (i != XVECLEN (insn, 1) - 1)
293 struct clobber_pat *p;
294 struct clobber_ent *link = XNEW (struct clobber_ent);
297 link->code_number = insn_code_number;
299 /* See if any previous CLOBBER_LIST entry is the same as this
302 for (p = clobber_list; p; p = p->next)
304 if (p->first_clobber != i + 1
305 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
308 for (j = i + 1; j < XVECLEN (insn, 1); j++)
310 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
311 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
313 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
315 or if both are registers of the same mode and number. */
316 if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx)
317 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
318 && GET_CODE (new_rtx) == MATCH_SCRATCH)
319 || (REG_P (old_rtx) && REG_P (new_rtx)
320 && REGNO (old_rtx) == REGNO (new_rtx)))))
324 if (j == XVECLEN (insn, 1))
330 p = XNEW (struct clobber_pat);
334 p->first_clobber = i + 1;
335 p->next = clobber_list;
336 p->has_hard_reg = has_hard_reg;
340 link->next = p->insns;
345 /* Don't mention instructions whose names are the null string
346 or begin with '*'. They are in the machine description just
348 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
351 printf ("/* %s:%d */\n", read_md_filename, lineno);
353 /* Find out how many operands this function has. */
354 get_pattern_stats (&stats, XVEC (insn, 1));
355 if (stats.max_dup_opno > stats.max_opno)
356 fatal ("match_dup operand number has no match_operand");
358 /* Output the function name and argument declarations. */
359 printf ("rtx\ngen_%s (", XSTR (insn, 0));
360 if (stats.num_generator_args)
361 for (i = 0; i < stats.num_generator_args; i++)
363 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
365 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
371 /* Output code to construct and return the rtl for the instruction body. */
373 if (XVECLEN (insn, 1) == 1)
376 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
381 char *used = XCNEWVEC (char, stats.num_generator_args);
383 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
386 for (i = 0; i < XVECLEN (insn, 1); i++)
389 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, used);
391 printf ("));\n}\n\n");
396 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
399 gen_expand (rtx expand)
401 struct pattern_stats stats;
405 if (strlen (XSTR (expand, 0)) == 0)
406 fatal ("define_expand lacks a name");
407 if (XVEC (expand, 1) == 0)
408 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
410 /* Find out how many operands this function has. */
411 get_pattern_stats (&stats, XVEC (expand, 1));
413 /* Output the function name and argument declarations. */
414 printf ("rtx\ngen_%s (", XSTR (expand, 0));
415 if (stats.num_generator_args)
416 for (i = 0; i < stats.num_generator_args; i++)
418 printf (",\n\trtx operand%d", i);
420 printf ("rtx operand%d", i);
426 /* If we don't have any C code to write, only one insn is being written,
427 and no MATCH_DUPs are present, we can just return the desired insn
428 like we do for a DEFINE_INSN. This saves memory. */
429 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
430 && stats.max_opno >= stats.max_dup_opno
431 && XVECLEN (expand, 1) == 1)
434 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
439 /* For each operand referred to only with MATCH_DUPs,
440 make a local variable. */
441 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
442 printf (" rtx operand%d;\n", i);
443 for (; i <= stats.max_scratch_opno; i++)
444 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
445 printf (" rtx _val = 0;\n");
446 printf (" start_sequence ();\n");
448 /* The fourth operand of DEFINE_EXPAND is some code to be executed
449 before the actual construction.
450 This code expects to refer to `operands'
451 just as the output-code in a DEFINE_INSN does,
452 but here `operands' is an automatic array.
453 So copy the operand values there before executing it. */
454 if (XSTR (expand, 3) && *XSTR (expand, 3))
457 if (stats.num_operand_vars > 0)
458 printf (" rtx operands[%d];\n", stats.num_operand_vars);
460 /* Output code to copy the arguments into `operands'. */
461 for (i = 0; i < stats.num_generator_args; i++)
462 printf (" operands[%d] = operand%d;\n", i, i);
464 /* Output the special code to be executed before the sequence
466 print_md_ptr_loc (XSTR (expand, 3));
467 printf ("%s\n", XSTR (expand, 3));
469 /* Output code to copy the arguments back out of `operands'
470 (unless we aren't going to use them at all). */
471 if (XVEC (expand, 1) != 0)
473 for (i = 0; i < stats.num_operand_vars; i++)
475 printf (" operand%d = operands[%d];\n", i, i);
476 printf (" (void) operand%d;\n", i);
482 /* Output code to construct the rtl for the instruction bodies.
483 Use emit_insn to add them to the sequence being accumulated.
484 But don't do this if the user's code has set `no_more' nonzero. */
486 used = XCNEWVEC (char, stats.num_operand_vars);
488 for (i = 0; i < XVECLEN (expand, 1); i++)
490 rtx next = XVECEXP (expand, 1, i);
491 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
492 || (GET_CODE (next) == PARALLEL
493 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
494 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
495 || ANY_RETURN_P (XVECEXP (next, 0, 0))))
496 || ANY_RETURN_P (next))
497 printf (" emit_jump_insn (");
498 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
499 || GET_CODE (next) == CALL
500 || (GET_CODE (next) == PARALLEL
501 && GET_CODE (XVECEXP (next, 0, 0)) == SET
502 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
503 || (GET_CODE (next) == PARALLEL
504 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
505 printf (" emit_call_insn (");
506 else if (LABEL_P (next))
507 printf (" emit_label (");
508 else if (GET_CODE (next) == MATCH_OPERAND
509 || GET_CODE (next) == MATCH_DUP
510 || GET_CODE (next) == MATCH_OPERATOR
511 || GET_CODE (next) == MATCH_OP_DUP
512 || GET_CODE (next) == MATCH_PARALLEL
513 || GET_CODE (next) == MATCH_PAR_DUP
514 || GET_CODE (next) == PARALLEL)
517 printf (" emit_insn (");
518 gen_exp (next, DEFINE_EXPAND, used);
520 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
521 && GET_CODE (SET_SRC (next)) == LABEL_REF)
522 printf (" emit_barrier ();");
527 /* Call `get_insns' to extract the list of all the
528 insns emitted within this gen_... function. */
530 printf (" _val = get_insns ();\n");
531 printf (" end_sequence ();\n");
532 printf (" return _val;\n}\n\n");
535 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
538 gen_split (rtx split)
540 struct pattern_stats stats;
542 const char *const name =
543 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
547 if (XVEC (split, 0) == 0)
548 fatal ("define_%s (definition %d) lacks a pattern", name,
550 else if (XVEC (split, 2) == 0)
551 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
554 /* Find out how many operands this function has. */
556 get_pattern_stats (&stats, XVEC (split, 2));
557 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
558 used = XCNEWVEC (char, stats.num_operand_vars);
560 /* Output the prototype, function name and argument declarations. */
561 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
563 printf ("extern rtx gen_%s_%d (rtx, rtx *);\n",
564 name, insn_code_number);
565 printf ("rtx\ngen_%s_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
566 name, insn_code_number, unused);
570 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", insn_code_number);
571 printf ("rtx\ngen_split_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
572 insn_code_number, unused);
576 /* Declare all local variables. */
577 for (i = 0; i < stats.num_operand_vars; i++)
578 printf (" rtx operand%d;\n", i);
579 printf (" rtx _val = 0;\n");
581 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
582 output_peephole2_scratches (split);
584 printf (" start_sequence ();\n");
586 /* The fourth operand of DEFINE_SPLIT is some code to be executed
587 before the actual construction. */
591 print_md_ptr_loc (XSTR (split, 3));
592 printf ("%s\n", XSTR (split, 3));
595 /* Output code to copy the arguments back out of `operands' */
596 for (i = 0; i < stats.num_operand_vars; i++)
598 printf (" operand%d = operands[%d];\n", i, i);
599 printf (" (void) operand%d;\n", i);
602 /* Output code to construct the rtl for the instruction bodies.
603 Use emit_insn to add them to the sequence being accumulated.
604 But don't do this if the user's code has set `no_more' nonzero. */
606 for (i = 0; i < XVECLEN (split, 2); i++)
608 rtx next = XVECEXP (split, 2, i);
609 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
610 || (GET_CODE (next) == PARALLEL
611 && GET_CODE (XVECEXP (next, 0, 0)) == SET
612 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
613 || ANY_RETURN_P (next))
614 printf (" emit_jump_insn (");
615 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
616 || GET_CODE (next) == CALL
617 || (GET_CODE (next) == PARALLEL
618 && GET_CODE (XVECEXP (next, 0, 0)) == SET
619 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
620 || (GET_CODE (next) == PARALLEL
621 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
622 printf (" emit_call_insn (");
623 else if (LABEL_P (next))
624 printf (" emit_label (");
625 else if (GET_CODE (next) == MATCH_OPERAND
626 || GET_CODE (next) == MATCH_OPERATOR
627 || GET_CODE (next) == MATCH_PARALLEL
628 || GET_CODE (next) == MATCH_OP_DUP
629 || GET_CODE (next) == MATCH_DUP
630 || GET_CODE (next) == PARALLEL)
633 printf (" emit_insn (");
634 gen_exp (next, GET_CODE (split), used);
636 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
637 && GET_CODE (SET_SRC (next)) == LABEL_REF)
638 printf (" emit_barrier ();");
641 /* Call `get_insns' to make a list of all the
642 insns emitted within this gen_... function. */
644 printf (" _val = get_insns ();\n");
645 printf (" end_sequence ();\n");
646 printf (" return _val;\n}\n\n");
651 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
652 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
653 the end of the vector. */
656 output_add_clobbers (void)
658 struct clobber_pat *clobber;
659 struct clobber_ent *ent;
662 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
664 printf (" switch (insn_code_number)\n");
667 for (clobber = clobber_list; clobber; clobber = clobber->next)
669 for (ent = clobber->insns; ent; ent = ent->next)
670 printf (" case %d:\n", ent->code_number);
672 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
674 printf (" XVECEXP (pattern, 0, %d) = ", i);
675 gen_exp (XVECEXP (clobber->pattern, 1, i),
676 GET_CODE (clobber->pattern), NULL);
680 printf (" break;\n\n");
683 printf (" default:\n");
684 printf (" gcc_unreachable ();\n");
689 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
690 number that will have clobbers added (as indicated by `recog') and returns
691 1 if those include a clobber of a hard reg or 0 if all of them just clobber
695 output_added_clobbers_hard_reg_p (void)
697 struct clobber_pat *clobber;
698 struct clobber_ent *ent;
701 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
703 printf (" switch (insn_code_number)\n");
706 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
709 for (clobber = clobber_list; clobber; clobber = clobber->next)
710 if (clobber->has_hard_reg == clobber_p)
711 for (ent = clobber->insns; ent; ent = ent->next)
713 printf (" case %d:\n", ent->code_number);
718 printf (" return %d;\n\n", clobber_p);
721 printf (" default:\n");
722 printf (" gcc_unreachable ();\n");
727 /* Generate code to invoke find_free_register () as needed for the
728 scratch registers used by the peephole2 pattern in SPLIT. */
731 output_peephole2_scratches (rtx split)
737 for (i = 0; i < XVECLEN (split, 0); i++)
739 rtx elt = XVECEXP (split, 0, i);
740 if (GET_CODE (elt) == MATCH_SCRATCH)
742 int last_insn_nr = insn_nr;
743 int cur_insn_nr = insn_nr;
745 for (j = i + 1; j < XVECLEN (split, 0); j++)
746 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
748 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
749 last_insn_nr = cur_insn_nr;
751 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
756 printf (" HARD_REG_SET _regs_allocated;\n");
757 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
761 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
764 insn_nr, last_insn_nr,
766 GET_MODE_NAME (GET_MODE (elt)));
769 else if (GET_CODE (elt) != MATCH_DUP)
775 main (int argc, char **argv)
779 progname = "genemit";
781 if (!init_rtx_reader_args (argc, argv))
782 return (FATAL_EXIT_CODE);
784 /* Assign sequential codes to all entries in the machine description
785 in parallel with the tables in insn-output.c. */
787 insn_code_number = 0;
788 insn_index_number = 0;
790 printf ("/* Generated automatically by the program `genemit'\n\
791 from the machine description file `md'. */\n\n");
793 printf ("#include \"config.h\"\n");
794 printf ("#include \"system.h\"\n");
795 printf ("#include \"coretypes.h\"\n");
796 printf ("#include \"tm.h\"\n");
797 printf ("#include \"rtl.h\"\n");
798 printf ("#include \"tm_p.h\"\n");
799 printf ("#include \"function.h\"\n");
800 printf ("#include \"expr.h\"\n");
801 printf ("#include \"optabs.h\"\n");
802 printf ("#include \"dfp.h\"\n");
803 printf ("#include \"flags.h\"\n");
804 printf ("#include \"output.h\"\n");
805 printf ("#include \"insn-config.h\"\n");
806 printf ("#include \"hard-reg-set.h\"\n");
807 printf ("#include \"recog.h\"\n");
808 printf ("#include \"resource.h\"\n");
809 printf ("#include \"reload.h\"\n");
810 printf ("#include \"diagnostic-core.h\"\n");
811 printf ("#include \"regs.h\"\n");
812 printf ("#include \"tm-constrs.h\"\n");
813 printf ("#include \"ggc.h\"\n");
814 printf ("#include \"basic-block.h\"\n");
815 printf ("#include \"integrate.h\"\n\n");
816 printf ("#define FAIL return (end_sequence (), _val)\n");
817 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
819 /* Read the machine description. */
825 desc = read_md_rtx (&line_no, &insn_code_number);
829 switch (GET_CODE (desc))
832 gen_insn (desc, line_no);
836 printf ("/* %s:%d */\n", read_md_filename, line_no);
841 printf ("/* %s:%d */\n", read_md_filename, line_no);
845 case DEFINE_PEEPHOLE2:
846 printf ("/* %s:%d */\n", read_md_filename, line_no);
856 /* Write out the routines to add CLOBBERs to a pattern and say whether they
857 clobber a hard reg. */
858 output_add_clobbers ();
859 output_added_clobbers_hard_reg_p ();
862 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);