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 /* Data structure for recording the patterns of insns that have CLOBBERs.
32 We use this to output a function that adds these CLOBBERs to a
33 previously-allocated PARALLEL expression. */
37 struct clobber_ent
*insns
;
40 struct clobber_pat
*next
;
44 /* Records one insn that uses the clobber list. */
48 int code_number
; /* Counts only insns. */
49 struct clobber_ent
*next
;
52 static void output_peephole2_scratches (rtx
);
56 print_code (RTX_CODE code
)
59 for (p1
= GET_RTX_NAME (code
); *p1
; p1
++)
60 putchar (TOUPPER (*p1
));
64 gen_rtx_scratch (rtx x
, enum rtx_code subroutine_type
)
66 if (subroutine_type
== DEFINE_PEEPHOLE2
)
68 printf ("operand%d", XINT (x
, 0));
72 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x
)));
76 /* Print a C expression to construct an RTX just like X,
77 substituting any operand references appearing within. */
80 gen_exp (rtx x
, enum rtx_code subroutine_type
, char *used
)
102 if (used
[XINT (x
, 0)])
104 printf ("copy_rtx (operand%d)", XINT (x
, 0));
107 used
[XINT (x
, 0)] = 1;
109 printf ("operand%d", XINT (x
, 0));
113 printf ("gen_rtx_fmt_");
114 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
116 printf (" (GET_CODE (operand%d), ", XINT (x
, 0));
117 if (GET_MODE (x
) == VOIDmode
)
118 printf ("GET_MODE (operand%d)", XINT (x
, 0));
120 printf ("%smode", GET_MODE_NAME (GET_MODE (x
)));
121 for (i
= 0; i
< XVECLEN (x
, 1); i
++)
124 gen_exp (XVECEXP (x
, 1, i
), subroutine_type
, used
);
130 printf ("gen_rtx_fmt_");
131 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
133 printf (" (GET_CODE (operand%d)", XINT (x
, 0));
134 printf (", %smode", GET_MODE_NAME (GET_MODE (x
)));
135 for (i
= 0; i
< XVECLEN (x
, 2); i
++)
138 gen_exp (XVECEXP (x
, 2, i
), subroutine_type
, used
);
145 printf ("operand%d", XINT (x
, 0));
149 gen_rtx_scratch (x
, subroutine_type
);
159 printf ("simple_return_rtx");
162 if (REG_P (XEXP (x
, 0)))
164 printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x
, 0))),
165 REGNO (XEXP (x
, 0)));
176 printf ("const0_rtx");
177 else if (INTVAL (x
) == 1)
178 printf ("const1_rtx");
179 else if (INTVAL (x
) == -1)
180 printf ("constm1_rtx");
181 else if (-MAX_SAVED_CONST_INT
<= INTVAL (x
)
182 && INTVAL (x
) <= MAX_SAVED_CONST_INT
)
183 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
185 else if (INTVAL (x
) == STORE_FLAG_VALUE
)
186 printf ("const_true_rtx");
189 printf ("GEN_INT (");
190 printf (HOST_WIDE_INT_PRINT_DEC_C
, INTVAL (x
));
198 /* These shouldn't be written in MD files. Instead, the appropriate
199 routines in varasm.c should be called. */
209 if (!always_void_p (code
))
211 printf ("%smode", GET_MODE_NAME (GET_MODE (x
)));
215 fmt
= GET_RTX_FORMAT (code
);
216 len
= GET_RTX_LENGTH (code
);
217 for (i
= 0; i
< len
; i
++)
225 gen_exp (XEXP (x
, i
), subroutine_type
, used
);
229 printf ("%u", XINT (x
, i
));
233 printf ("%u", REGNO (x
));
237 printf ("\"%s\"", XSTR (x
, i
));
243 printf ("gen_rtvec (%d", XVECLEN (x
, i
));
244 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
247 gen_exp (XVECEXP (x
, i
, j
), subroutine_type
, used
);
261 /* Output code to emit the instruction patterns in VEC, with each element
262 becoming a separate instruction. USED is as for gen_exp. */
265 gen_emit_seq (rtvec vec
, char *used
)
267 for (int i
= 0, len
= GET_NUM_ELEM (vec
); i
< len
; ++i
)
269 bool last_p
= (i
== len
- 1);
270 rtx next
= RTVEC_ELT (vec
, i
);
271 if (const char *name
= get_emit_function (next
))
273 printf (" %s (", name
);
274 gen_exp (next
, DEFINE_EXPAND
, used
);
276 if (!last_p
&& needs_barrier_p (next
))
277 printf (" emit_barrier ();");
282 gen_exp (next
, DEFINE_EXPAND
, used
);
283 printf (", %s);\n", last_p
? "false" : "true");
288 /* Generate the `gen_...' function for a DEFINE_INSN. */
291 gen_insn (md_rtx_info
*info
)
293 struct pattern_stats stats
;
296 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
297 registers or MATCH_SCRATCHes. If so, store away the information for
300 rtx insn
= info
->def
;
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
= info
->index
;
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", info
->loc
.filename
, info
->loc
.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_at (info
->loc
, "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 (md_rtx_info
*info
)
414 struct pattern_stats stats
;
418 rtx expand
= info
->def
;
419 if (strlen (XSTR (expand
, 0)) == 0)
420 fatal_at (info
->loc
, "define_expand lacks a name");
421 if (XVEC (expand
, 1) == 0)
422 fatal_at (info
->loc
, "define_expand for %s lacks a pattern",
425 /* Find out how many operands this function has. */
426 get_pattern_stats (&stats
, XVEC (expand
, 1));
428 /* Output the function name and argument declarations. */
429 printf ("rtx\ngen_%s (", XSTR (expand
, 0));
430 if (stats
.num_generator_args
)
431 for (i
= 0; i
< stats
.num_generator_args
; i
++)
433 printf (",\n\trtx operand%d", i
);
435 printf ("rtx operand%d", i
);
441 /* If we don't have any C code to write, only one insn is being written,
442 and no MATCH_DUPs are present, we can just return the desired insn
443 like we do for a DEFINE_INSN. This saves memory. */
444 if ((XSTR (expand
, 3) == 0 || *XSTR (expand
, 3) == '\0')
445 && stats
.max_opno
>= stats
.max_dup_opno
446 && XVECLEN (expand
, 1) == 1)
449 gen_exp (XVECEXP (expand
, 1, 0), DEFINE_EXPAND
, NULL
);
454 /* For each operand referred to only with MATCH_DUPs,
455 make a local variable. */
456 for (i
= stats
.num_generator_args
; i
<= stats
.max_dup_opno
; i
++)
457 printf (" rtx operand%d;\n", i
);
458 for (; i
<= stats
.max_scratch_opno
; i
++)
459 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i
);
460 printf (" rtx_insn *_val = 0;\n");
461 printf (" start_sequence ();\n");
463 /* The fourth operand of DEFINE_EXPAND is some code to be executed
464 before the actual construction.
465 This code expects to refer to `operands'
466 just as the output-code in a DEFINE_INSN does,
467 but here `operands' is an automatic array.
468 So copy the operand values there before executing it. */
469 if (XSTR (expand
, 3) && *XSTR (expand
, 3))
472 if (stats
.num_operand_vars
> 0)
473 printf (" rtx operands[%d];\n", stats
.num_operand_vars
);
475 /* Output code to copy the arguments into `operands'. */
476 for (i
= 0; i
< stats
.num_generator_args
; i
++)
477 printf (" operands[%d] = operand%d;\n", i
, i
);
479 /* Output the special code to be executed before the sequence
481 print_md_ptr_loc (XSTR (expand
, 3));
482 printf ("%s\n", XSTR (expand
, 3));
484 /* Output code to copy the arguments back out of `operands'
485 (unless we aren't going to use them at all). */
486 if (XVEC (expand
, 1) != 0)
488 for (i
= 0; i
< stats
.num_operand_vars
; i
++)
490 printf (" operand%d = operands[%d];\n", i
, i
);
491 printf (" (void) operand%d;\n", i
);
497 used
= XCNEWVEC (char, stats
.num_operand_vars
);
498 gen_emit_seq (XVEC (expand
, 1), used
);
501 /* Call `get_insns' to extract the list of all the
502 insns emitted within this gen_... function. */
504 printf (" _val = get_insns ();\n");
505 printf (" end_sequence ();\n");
506 printf (" return _val;\n}\n\n");
509 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
512 gen_split (md_rtx_info
*info
)
514 struct pattern_stats stats
;
516 rtx split
= info
->def
;
517 const char *const name
=
518 ((GET_CODE (split
) == DEFINE_PEEPHOLE2
) ? "peephole2" : "split");
522 if (XVEC (split
, 0) == 0)
523 fatal_at (info
->loc
, "%s lacks a pattern",
524 GET_RTX_NAME (GET_CODE (split
)));
525 else if (XVEC (split
, 2) == 0)
526 fatal_at (info
->loc
, "%s lacks a replacement pattern",
527 GET_RTX_NAME (GET_CODE (split
)));
529 /* Find out how many operands this function has. */
531 get_pattern_stats (&stats
, XVEC (split
, 2));
532 unused
= (stats
.num_operand_vars
== 0 ? " ATTRIBUTE_UNUSED" : "");
533 used
= XCNEWVEC (char, stats
.num_operand_vars
);
535 /* Output the prototype, function name and argument declarations. */
536 if (GET_CODE (split
) == DEFINE_PEEPHOLE2
)
538 printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n",
540 printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED,"
541 " rtx *operands%s)\n",
542 name
, info
->index
, unused
);
546 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
548 printf ("rtx_insn *\ngen_split_%d "
549 "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
550 info
->index
, unused
);
554 /* Declare all local variables. */
555 for (i
= 0; i
< stats
.num_operand_vars
; i
++)
556 printf (" rtx operand%d;\n", i
);
557 printf (" rtx_insn *_val = NULL;\n");
559 if (GET_CODE (split
) == DEFINE_PEEPHOLE2
)
560 output_peephole2_scratches (split
);
562 printf (" if (dump_file)\n");
563 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n",
566 printf (" start_sequence ();\n");
568 /* The fourth operand of DEFINE_SPLIT is some code to be executed
569 before the actual construction. */
573 print_md_ptr_loc (XSTR (split
, 3));
574 printf ("%s\n", XSTR (split
, 3));
577 /* Output code to copy the arguments back out of `operands' */
578 for (i
= 0; i
< stats
.num_operand_vars
; i
++)
580 printf (" operand%d = operands[%d];\n", i
, i
);
581 printf (" (void) operand%d;\n", i
);
584 gen_emit_seq (XVEC (split
, 2), used
);
586 /* Call `get_insns' to make a list of all the
587 insns emitted within this gen_... function. */
589 printf (" _val = get_insns ();\n");
590 printf (" end_sequence ();\n");
591 printf (" return _val;\n}\n\n");
596 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
597 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
598 the end of the vector. */
601 output_add_clobbers (void)
603 struct clobber_pat
*clobber
;
604 struct clobber_ent
*ent
;
607 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
609 printf (" switch (insn_code_number)\n");
612 for (clobber
= clobber_list
; clobber
; clobber
= clobber
->next
)
614 for (ent
= clobber
->insns
; ent
; ent
= ent
->next
)
615 printf (" case %d:\n", ent
->code_number
);
617 for (i
= clobber
->first_clobber
; i
< XVECLEN (clobber
->pattern
, 1); i
++)
619 printf (" XVECEXP (pattern, 0, %d) = ", i
);
620 gen_exp (XVECEXP (clobber
->pattern
, 1, i
),
621 GET_CODE (clobber
->pattern
), NULL
);
625 printf (" break;\n\n");
628 printf (" default:\n");
629 printf (" gcc_unreachable ();\n");
634 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
635 number that will have clobbers added (as indicated by `recog') and returns
636 1 if those include a clobber of a hard reg or 0 if all of them just clobber
640 output_added_clobbers_hard_reg_p (void)
642 struct clobber_pat
*clobber
;
643 struct clobber_ent
*ent
;
646 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
648 printf (" switch (insn_code_number)\n");
651 for (clobber_p
= 0; clobber_p
<= 1; clobber_p
++)
654 for (clobber
= clobber_list
; clobber
; clobber
= clobber
->next
)
655 if (clobber
->has_hard_reg
== clobber_p
)
656 for (ent
= clobber
->insns
; ent
; ent
= ent
->next
)
658 printf (" case %d:\n", ent
->code_number
);
663 printf (" return %d;\n\n", clobber_p
);
666 printf (" default:\n");
667 printf (" gcc_unreachable ();\n");
672 /* Generate code to invoke find_free_register () as needed for the
673 scratch registers used by the peephole2 pattern in SPLIT. */
676 output_peephole2_scratches (rtx split
)
682 for (i
= 0; i
< XVECLEN (split
, 0); i
++)
684 rtx elt
= XVECEXP (split
, 0, i
);
685 if (GET_CODE (elt
) == MATCH_SCRATCH
)
687 int last_insn_nr
= insn_nr
;
688 int cur_insn_nr
= insn_nr
;
690 for (j
= i
+ 1; j
< XVECLEN (split
, 0); j
++)
691 if (GET_CODE (XVECEXP (split
, 0, j
)) == MATCH_DUP
)
693 if (XINT (XVECEXP (split
, 0, j
), 0) == XINT (elt
, 0))
694 last_insn_nr
= cur_insn_nr
;
696 else if (GET_CODE (XVECEXP (split
, 0, j
)) != MATCH_SCRATCH
)
701 printf (" HARD_REG_SET _regs_allocated;\n");
702 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
706 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
709 insn_nr
, last_insn_nr
,
711 GET_MODE_NAME (GET_MODE (elt
)));
714 else if (GET_CODE (elt
) != MATCH_DUP
)
720 main (int argc
, char **argv
)
722 progname
= "genemit";
724 if (!init_rtx_reader_args (argc
, argv
))
725 return (FATAL_EXIT_CODE
);
727 /* Assign sequential codes to all entries in the machine description
728 in parallel with the tables in insn-output.c. */
730 printf ("/* Generated automatically by the program `genemit'\n\
731 from the machine description file `md'. */\n\n");
733 printf ("#include \"config.h\"\n");
734 printf ("#include \"system.h\"\n");
735 printf ("#include \"coretypes.h\"\n");
736 printf ("#include \"backend.h\"\n");
737 printf ("#include \"predict.h\"\n");
738 printf ("#include \"tree.h\"\n");
739 printf ("#include \"rtl.h\"\n");
740 printf ("#include \"alias.h\"\n");
741 printf ("#include \"varasm.h\"\n");
742 printf ("#include \"stor-layout.h\"\n");
743 printf ("#include \"calls.h\"\n");
744 printf ("#include \"tm_p.h\"\n");
745 printf ("#include \"flags.h\"\n");
746 printf ("#include \"insn-config.h\"\n");
747 printf ("#include \"expmed.h\"\n");
748 printf ("#include \"dojump.h\"\n");
749 printf ("#include \"explow.h\"\n");
750 printf ("#include \"emit-rtl.h\"\n");
751 printf ("#include \"stmt.h\"\n");
752 printf ("#include \"expr.h\"\n");
753 printf ("#include \"insn-codes.h\"\n");
754 printf ("#include \"optabs.h\"\n");
755 printf ("#include \"dfp.h\"\n");
756 printf ("#include \"output.h\"\n");
757 printf ("#include \"recog.h\"\n");
758 printf ("#include \"df.h\"\n");
759 printf ("#include \"resource.h\"\n");
760 printf ("#include \"reload.h\"\n");
761 printf ("#include \"diagnostic-core.h\"\n");
762 printf ("#include \"regs.h\"\n");
763 printf ("#include \"tm-constrs.h\"\n");
764 printf ("#include \"ggc.h\"\n");
765 printf ("#include \"dumpfile.h\"\n");
766 printf ("#include \"target.h\"\n\n");
767 printf ("#define FAIL return (end_sequence (), _val)\n");
768 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
770 /* Read the machine description. */
773 while (read_md_rtx (&info
))
774 switch (GET_CODE (info
.def
))
781 printf ("/* %s:%d */\n", info
.loc
.filename
, info
.loc
.lineno
);
786 printf ("/* %s:%d */\n", info
.loc
.filename
, info
.loc
.lineno
);
790 case DEFINE_PEEPHOLE2
:
791 printf ("/* %s:%d */\n", info
.loc
.filename
, info
.loc
.lineno
);
799 /* Write out the routines to add CLOBBERs to a pattern and say whether they
800 clobber a hard reg. */
801 output_add_clobbers ();
802 output_added_clobbers_hard_reg_p ();
805 return (ferror (stdout
) != 0 ? FATAL_EXIT_CODE
: SUCCESS_EXIT_CODE
);