1 /* Generate code from machine description to emit insns as rtl.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "gensupport.h"
31 static int insn_code_number
;
32 static int insn_index_number
;
34 /* Data structure for recording the patterns of insns that have CLOBBERs.
35 We use this to output a function that adds these CLOBBERs to a
36 previously-allocated PARALLEL expression. */
40 struct clobber_ent
*insns
;
43 struct clobber_pat
*next
;
47 /* Records one insn that uses the clobber list. */
51 int code_number
; /* Counts only insns. */
52 struct clobber_ent
*next
;
55 static void print_code (RTX_CODE
);
56 static void gen_exp (rtx
, enum rtx_code
, char *);
57 static void gen_insn (rtx
, int);
58 static void gen_expand (rtx
);
59 static void gen_split (rtx
);
60 static void output_add_clobbers (void);
61 static void output_added_clobbers_hard_reg_p (void);
62 static void gen_rtx_scratch (rtx
, enum rtx_code
);
63 static void output_peephole2_scratches (rtx
);
67 print_code (RTX_CODE code
)
70 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
71 putchar (TOUPPER (*p1
));
75 gen_rtx_scratch (rtx x
, enum rtx_code subroutine_type
)
77 if (subroutine_type
== DEFINE_PEEPHOLE2
)
79 printf ("operand%d", XINT (x
, 0));
83 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x
)));
87 /* Print a C expression to construct an RTX just like X,
88 substituting any operand references appearing within. */
91 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
);
170 printf ("simple_return_rtx");
173 if (REG_P (XEXP (x
, 0)))
175 printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x
, 0))),
176 REGNO (XEXP (x
, 0)));
187 printf ("const0_rtx");
188 else if (INTVAL (x
) == 1)
189 printf ("const1_rtx");
190 else if (INTVAL (x
) == -1)
191 printf ("constm1_rtx");
192 else if (-MAX_SAVED_CONST_INT
<= INTVAL (x
)
193 && INTVAL (x
) <= MAX_SAVED_CONST_INT
)
194 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
196 else if (INTVAL (x
) == STORE_FLAG_VALUE
)
197 printf ("const_true_rtx");
200 printf ("GEN_INT (");
201 printf (HOST_WIDE_INT_PRINT_DEC_C
, INTVAL (x
));
209 /* These shouldn't be written in MD files. Instead, the appropriate
210 routines in varasm.c should be called. */
220 if (!always_void_p (code
))
222 printf ("%smode", GET_MODE_NAME (GET_MODE (x
)));
226 fmt
= GET_RTX_FORMAT (code
);
227 len
= GET_RTX_LENGTH (code
);
228 for (i
= 0; i
< len
; i
++)
236 gen_exp (XEXP (x
, i
), subroutine_type
, used
);
240 printf ("%u", XINT (x
, i
));
244 printf ("%u", REGNO (x
));
248 printf ("\"%s\"", XSTR (x
, i
));
254 printf ("gen_rtvec (%d", XVECLEN (x
, i
));
255 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
258 gen_exp (XVECEXP (x
, i
, j
), subroutine_type
, used
);
272 /* Output code to emit the instruction patterns in VEC, with each element
273 becoming a separate instruction. USED is as for gen_exp. */
276 gen_emit_seq (rtvec vec
, char *used
)
278 for (int i
= 0, len
= GET_NUM_ELEM (vec
); i
< len
; ++i
)
280 rtx next
= RTVEC_ELT (vec
, i
);
281 printf (" %s (", get_emit_function (next
));
282 gen_exp (next
, DEFINE_EXPAND
, used
);
284 if (needs_barrier_p (next
))
285 printf (" emit_barrier ();");
289 /* Generate the `gen_...' function for a DEFINE_INSN. */
292 gen_insn (rtx insn
, int lineno
)
294 struct pattern_stats stats
;
297 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
298 registers or MATCH_SCRATCHes. If so, store away the information for
303 int has_hard_reg
= 0;
305 for (i
= XVECLEN (insn
, 1) - 1; i
> 0; i
--)
307 if (GET_CODE (XVECEXP (insn
, 1, i
)) != CLOBBER
)
310 if (REG_P (XEXP (XVECEXP (insn
, 1, i
), 0)))
312 else if (GET_CODE (XEXP (XVECEXP (insn
, 1, i
), 0)) != MATCH_SCRATCH
)
316 if (i
!= XVECLEN (insn
, 1) - 1)
318 struct clobber_pat
*p
;
319 struct clobber_ent
*link
= XNEW (struct clobber_ent
);
322 link
->code_number
= insn_code_number
;
324 /* See if any previous CLOBBER_LIST entry is the same as this
327 for (p
= clobber_list
; p
; p
= p
->next
)
329 if (p
->first_clobber
!= i
+ 1
330 || XVECLEN (p
->pattern
, 1) != XVECLEN (insn
, 1))
333 for (j
= i
+ 1; j
< XVECLEN (insn
, 1); j
++)
335 rtx old_rtx
= XEXP (XVECEXP (p
->pattern
, 1, j
), 0);
336 rtx new_rtx
= XEXP (XVECEXP (insn
, 1, j
), 0);
338 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
340 or if both are registers of the same mode and number. */
341 if (! (GET_MODE (old_rtx
) == GET_MODE (new_rtx
)
342 && ((GET_CODE (old_rtx
) == MATCH_SCRATCH
343 && GET_CODE (new_rtx
) == MATCH_SCRATCH
)
344 || (REG_P (old_rtx
) && REG_P (new_rtx
)
345 && REGNO (old_rtx
) == REGNO (new_rtx
)))))
349 if (j
== XVECLEN (insn
, 1))
355 p
= XNEW (struct clobber_pat
);
359 p
->first_clobber
= i
+ 1;
360 p
->next
= clobber_list
;
361 p
->has_hard_reg
= has_hard_reg
;
365 link
->next
= p
->insns
;
370 /* Don't mention instructions whose names are the null string
371 or begin with '*'. They are in the machine description just
373 if (XSTR (insn
, 0)[0] == 0 || XSTR (insn
, 0)[0] == '*')
376 printf ("/* %s:%d */\n", read_md_filename
, lineno
);
378 /* Find out how many operands this function has. */
379 get_pattern_stats (&stats
, XVEC (insn
, 1));
380 if (stats
.max_dup_opno
> stats
.max_opno
)
381 fatal ("match_dup operand number has no match_operand");
383 /* Output the function name and argument declarations. */
384 printf ("rtx\ngen_%s (", XSTR (insn
, 0));
385 if (stats
.num_generator_args
)
386 for (i
= 0; i
< stats
.num_generator_args
; i
++)
388 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i
);
390 printf ("rtx operand%d ATTRIBUTE_UNUSED", i
);
396 /* Output code to construct and return the rtl for the instruction body. */
398 rtx pattern
= add_implicit_parallel (XVEC (insn
, 1));
399 /* ??? This is the traditional behavior, but seems suspect. */
400 char *used
= (XVECLEN (insn
, 1) == 1
402 : XCNEWVEC (char, stats
.num_generator_args
));
404 gen_exp (pattern
, DEFINE_INSN
, used
);
409 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
412 gen_expand (rtx expand
)
414 struct pattern_stats stats
;
418 if (strlen (XSTR (expand
, 0)) == 0)
419 fatal ("define_expand lacks a name");
420 if (XVEC (expand
, 1) == 0)
421 fatal ("define_expand for %s lacks a pattern", XSTR (expand
, 0));
423 /* Find out how many operands this function has. */
424 get_pattern_stats (&stats
, XVEC (expand
, 1));
426 /* Output the function name and argument declarations. */
427 printf ("rtx\ngen_%s (", XSTR (expand
, 0));
428 if (stats
.num_generator_args
)
429 for (i
= 0; i
< stats
.num_generator_args
; i
++)
431 printf (",\n\trtx operand%d", i
);
433 printf ("rtx operand%d", i
);
439 /* If we don't have any C code to write, only one insn is being written,
440 and no MATCH_DUPs are present, we can just return the desired insn
441 like we do for a DEFINE_INSN. This saves memory. */
442 if ((XSTR (expand
, 3) == 0 || *XSTR (expand
, 3) == '\0')
443 && stats
.max_opno
>= stats
.max_dup_opno
444 && XVECLEN (expand
, 1) == 1)
447 gen_exp (XVECEXP (expand
, 1, 0), DEFINE_EXPAND
, NULL
);
452 /* For each operand referred to only with MATCH_DUPs,
453 make a local variable. */
454 for (i
= stats
.num_generator_args
; i
<= stats
.max_dup_opno
; i
++)
455 printf (" rtx operand%d;\n", i
);
456 for (; i
<= stats
.max_scratch_opno
; i
++)
457 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i
);
458 printf (" rtx_insn *_val = 0;\n");
459 printf (" start_sequence ();\n");
461 /* The fourth operand of DEFINE_EXPAND is some code to be executed
462 before the actual construction.
463 This code expects to refer to `operands'
464 just as the output-code in a DEFINE_INSN does,
465 but here `operands' is an automatic array.
466 So copy the operand values there before executing it. */
467 if (XSTR (expand
, 3) && *XSTR (expand
, 3))
470 if (stats
.num_operand_vars
> 0)
471 printf (" rtx operands[%d];\n", stats
.num_operand_vars
);
473 /* Output code to copy the arguments into `operands'. */
474 for (i
= 0; i
< stats
.num_generator_args
; i
++)
475 printf (" operands[%d] = operand%d;\n", i
, i
);
477 /* Output the special code to be executed before the sequence
479 print_md_ptr_loc (XSTR (expand
, 3));
480 printf ("%s\n", XSTR (expand
, 3));
482 /* Output code to copy the arguments back out of `operands'
483 (unless we aren't going to use them at all). */
484 if (XVEC (expand
, 1) != 0)
486 for (i
= 0; i
< stats
.num_operand_vars
; i
++)
488 printf (" operand%d = operands[%d];\n", i
, i
);
489 printf (" (void) operand%d;\n", i
);
495 used
= XCNEWVEC (char, stats
.num_operand_vars
);
496 gen_emit_seq (XVEC (expand
, 1), used
);
499 /* Call `get_insns' to extract the list of all the
500 insns emitted within this gen_... function. */
502 printf (" _val = get_insns ();\n");
503 printf (" end_sequence ();\n");
504 printf (" return _val;\n}\n\n");
507 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
510 gen_split (rtx split
)
512 struct pattern_stats stats
;
514 const char *const name
=
515 ((GET_CODE (split
) == DEFINE_PEEPHOLE2
) ? "peephole2" : "split");
519 if (XVEC (split
, 0) == 0)
520 fatal ("define_%s (definition %d) lacks a pattern", name
,
522 else if (XVEC (split
, 2) == 0)
523 fatal ("define_%s (definition %d) lacks a replacement pattern", name
,
526 /* Find out how many operands this function has. */
528 get_pattern_stats (&stats
, XVEC (split
, 2));
529 unused
= (stats
.num_operand_vars
== 0 ? " ATTRIBUTE_UNUSED" : "");
530 used
= XCNEWVEC (char, stats
.num_operand_vars
);
532 /* Output the prototype, function name and argument declarations. */
533 if (GET_CODE (split
) == DEFINE_PEEPHOLE2
)
535 printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n",
536 name
, insn_code_number
);
537 printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
538 name
, insn_code_number
, unused
);
542 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
544 printf ("rtx_insn *\ngen_split_%d "
545 "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
546 insn_code_number
, unused
);
550 /* Declare all local variables. */
551 for (i
= 0; i
< stats
.num_operand_vars
; i
++)
552 printf (" rtx operand%d;\n", i
);
553 printf (" rtx_insn *_val = NULL;\n");
555 if (GET_CODE (split
) == DEFINE_PEEPHOLE2
)
556 output_peephole2_scratches (split
);
558 printf (" if (dump_file)\n");
559 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n",
560 name
, insn_code_number
);
562 printf (" start_sequence ();\n");
564 /* The fourth operand of DEFINE_SPLIT is some code to be executed
565 before the actual construction. */
569 print_md_ptr_loc (XSTR (split
, 3));
570 printf ("%s\n", XSTR (split
, 3));
573 /* Output code to copy the arguments back out of `operands' */
574 for (i
= 0; i
< stats
.num_operand_vars
; i
++)
576 printf (" operand%d = operands[%d];\n", i
, i
);
577 printf (" (void) operand%d;\n", i
);
580 gen_emit_seq (XVEC (split
, 2), used
);
582 /* Call `get_insns' to make a list of all the
583 insns emitted within this gen_... function. */
585 printf (" _val = get_insns ();\n");
586 printf (" end_sequence ();\n");
587 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 (void)
599 struct clobber_pat
*clobber
;
600 struct clobber_ent
*ent
;
603 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\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
),
617 GET_CODE (clobber
->pattern
), NULL
);
621 printf (" break;\n\n");
624 printf (" default:\n");
625 printf (" gcc_unreachable ();\n");
630 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
631 number that will have clobbers added (as indicated by `recog') and returns
632 1 if those include a clobber of a hard reg or 0 if all of them just clobber
636 output_added_clobbers_hard_reg_p (void)
638 struct clobber_pat
*clobber
;
639 struct clobber_ent
*ent
;
642 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
644 printf (" switch (insn_code_number)\n");
647 for (clobber_p
= 0; clobber_p
<= 1; clobber_p
++)
650 for (clobber
= clobber_list
; clobber
; clobber
= clobber
->next
)
651 if (clobber
->has_hard_reg
== clobber_p
)
652 for (ent
= clobber
->insns
; ent
; ent
= ent
->next
)
654 printf (" case %d:\n", ent
->code_number
);
659 printf (" return %d;\n\n", clobber_p
);
662 printf (" default:\n");
663 printf (" gcc_unreachable ();\n");
668 /* Generate code to invoke find_free_register () as needed for the
669 scratch registers used by the peephole2 pattern in SPLIT. */
672 output_peephole2_scratches (rtx split
)
678 for (i
= 0; i
< XVECLEN (split
, 0); i
++)
680 rtx elt
= XVECEXP (split
, 0, i
);
681 if (GET_CODE (elt
) == MATCH_SCRATCH
)
683 int last_insn_nr
= insn_nr
;
684 int cur_insn_nr
= insn_nr
;
686 for (j
= i
+ 1; j
< XVECLEN (split
, 0); j
++)
687 if (GET_CODE (XVECEXP (split
, 0, j
)) == MATCH_DUP
)
689 if (XINT (XVECEXP (split
, 0, j
), 0) == XINT (elt
, 0))
690 last_insn_nr
= cur_insn_nr
;
692 else if (GET_CODE (XVECEXP (split
, 0, j
)) != MATCH_SCRATCH
)
697 printf (" HARD_REG_SET _regs_allocated;\n");
698 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
702 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
705 insn_nr
, last_insn_nr
,
707 GET_MODE_NAME (GET_MODE (elt
)));
710 else if (GET_CODE (elt
) != MATCH_DUP
)
716 main (int argc
, char **argv
)
720 progname
= "genemit";
722 if (!init_rtx_reader_args (argc
, argv
))
723 return (FATAL_EXIT_CODE
);
725 /* Assign sequential codes to all entries in the machine description
726 in parallel with the tables in insn-output.c. */
728 insn_code_number
= 0;
729 insn_index_number
= 0;
731 printf ("/* Generated automatically by the program `genemit'\n\
732 from the machine description file `md'. */\n\n");
734 printf ("#include \"config.h\"\n");
735 printf ("#include \"system.h\"\n");
736 printf ("#include \"coretypes.h\"\n");
737 printf ("#include \"tm.h\"\n");
738 printf ("#include \"input.h\"\n");
739 printf ("#include \"alias.h\"\n");
740 printf ("#include \"symtab.h\"\n");
741 printf ("#include \"tree.h\"\n");
742 printf ("#include \"varasm.h\"\n");
743 printf ("#include \"stor-layout.h\"\n");
744 printf ("#include \"calls.h\"\n");
745 printf ("#include \"rtl.h\"\n");
746 printf ("#include \"tm_p.h\"\n");
747 printf ("#include \"hard-reg-set.h\"\n");
748 printf ("#include \"function.h\"\n");
749 printf ("#include \"flags.h\"\n");
750 printf ("#include \"insn-config.h\"\n");
751 printf ("#include \"expmed.h\"\n");
752 printf ("#include \"dojump.h\"\n");
753 printf ("#include \"explow.h\"\n");
754 printf ("#include \"emit-rtl.h\"\n");
755 printf ("#include \"stmt.h\"\n");
756 printf ("#include \"expr.h\"\n");
757 printf ("#include \"insn-codes.h\"\n");
758 printf ("#include \"optabs.h\"\n");
759 printf ("#include \"dfp.h\"\n");
760 printf ("#include \"output.h\"\n");
761 printf ("#include \"recog.h\"\n");
762 printf ("#include \"predict.h\"\n");
763 printf ("#include \"basic-block.h\"\n");
764 printf ("#include \"resource.h\"\n");
765 printf ("#include \"reload.h\"\n");
766 printf ("#include \"diagnostic-core.h\"\n");
767 printf ("#include \"regs.h\"\n");
768 printf ("#include \"tm-constrs.h\"\n");
769 printf ("#include \"ggc.h\"\n");
770 printf ("#include \"basic-block.h\"\n");
771 printf ("#include \"dumpfile.h\"\n");
772 printf ("#include \"target.h\"\n\n");
773 printf ("#define FAIL return (end_sequence (), _val)\n");
774 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
776 /* Read the machine description. */
782 desc
= read_md_rtx (&line_no
, &insn_code_number
);
786 switch (GET_CODE (desc
))
789 gen_insn (desc
, line_no
);
793 printf ("/* %s:%d */\n", read_md_filename
, line_no
);
798 printf ("/* %s:%d */\n", read_md_filename
, line_no
);
802 case DEFINE_PEEPHOLE2
:
803 printf ("/* %s:%d */\n", read_md_filename
, line_no
);
813 /* Write out the routines to add CLOBBERs to a pattern and say whether they
814 clobber a hard reg. */
815 output_add_clobbers ();
816 output_added_clobbers_hard_reg_p ();
819 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);