gcc/
[official-gcc.git] / gcc / genemit.c
blobec8c86cc9ec95bc71d70b6051b4fcce8db56001c
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
9 version.
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
14 for more details.
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/>. */
21 #include "bconfig.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "errors.h"
27 #include "read-md.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. */
38 struct clobber_pat
40 struct clobber_ent *insns;
41 rtx pattern;
42 int first_clobber;
43 struct clobber_pat *next;
44 int has_hard_reg;
45 } *clobber_list;
47 /* Records one insn that uses the clobber list. */
49 struct clobber_ent
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);
66 static void
67 print_code (RTX_CODE code)
69 const char *p1;
70 for (p1 = GET_RTX_NAME (code); *p1; p1++)
71 putchar (TOUPPER (*p1));
74 static void
75 gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
77 if (subroutine_type == DEFINE_PEEPHOLE2)
79 printf ("operand%d", XINT (x, 0));
81 else
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. */
90 static void
91 gen_exp (rtx x, enum rtx_code subroutine_type, char *used)
93 RTX_CODE code;
94 int i;
95 int len;
96 const char *fmt;
97 const char *sep = "";
99 if (x == 0)
101 printf ("NULL_RTX");
102 return;
105 code = GET_CODE (x);
107 switch (code)
109 case MATCH_OPERAND:
110 case MATCH_DUP:
111 if (used)
113 if (used[XINT (x, 0)])
115 printf ("copy_rtx (operand%d)", XINT (x, 0));
116 return;
118 used[XINT (x, 0)] = 1;
120 printf ("operand%d", XINT (x, 0));
121 return;
123 case MATCH_OP_DUP:
124 printf ("gen_rtx_fmt_");
125 for (i = 0; i < XVECLEN (x, 1); i++)
126 printf ("e");
127 printf (" (GET_CODE (operand%d), ", XINT (x, 0));
128 if (GET_MODE (x) == VOIDmode)
129 printf ("GET_MODE (operand%d)", XINT (x, 0));
130 else
131 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
132 for (i = 0; i < XVECLEN (x, 1); i++)
134 printf (",\n\t\t");
135 gen_exp (XVECEXP (x, 1, i), subroutine_type, used);
137 printf (")");
138 return;
140 case MATCH_OPERATOR:
141 printf ("gen_rtx_fmt_");
142 for (i = 0; i < XVECLEN (x, 2); i++)
143 printf ("e");
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++)
148 printf (",\n\t\t");
149 gen_exp (XVECEXP (x, 2, i), subroutine_type, used);
151 printf (")");
152 return;
154 case MATCH_PARALLEL:
155 case MATCH_PAR_DUP:
156 printf ("operand%d", XINT (x, 0));
157 return;
159 case MATCH_SCRATCH:
160 gen_rtx_scratch (x, subroutine_type);
161 return;
163 case PC:
164 printf ("pc_rtx");
165 return;
166 case RETURN:
167 printf ("ret_rtx");
168 return;
169 case SIMPLE_RETURN:
170 printf ("simple_return_rtx");
171 return;
172 case CLOBBER:
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)));
177 return;
179 break;
181 case CC0:
182 printf ("cc0_rtx");
183 return;
185 case CONST_INT:
186 if (INTVAL (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)]",
195 (int) INTVAL (x));
196 else if (INTVAL (x) == STORE_FLAG_VALUE)
197 printf ("const_true_rtx");
198 else
200 printf ("GEN_INT (");
201 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
202 printf (")");
204 return;
206 case CONST_DOUBLE:
207 case CONST_FIXED:
208 case CONST_WIDE_INT:
209 /* These shouldn't be written in MD files. Instead, the appropriate
210 routines in varasm.c should be called. */
211 gcc_unreachable ();
213 default:
214 break;
217 printf ("gen_rtx_");
218 print_code (code);
219 printf (" (");
220 if (!always_void_p (code))
222 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
223 sep = ",\n\t";
226 fmt = GET_RTX_FORMAT (code);
227 len = GET_RTX_LENGTH (code);
228 for (i = 0; i < len; i++)
230 if (fmt[i] == '0')
231 break;
232 fputs (sep, stdout);
233 switch (fmt[i])
235 case 'e': case 'u':
236 gen_exp (XEXP (x, i), subroutine_type, used);
237 break;
239 case 'i':
240 printf ("%u", XINT (x, i));
241 break;
243 case 's':
244 printf ("\"%s\"", XSTR (x, i));
245 break;
247 case 'E':
249 int j;
250 printf ("gen_rtvec (%d", XVECLEN (x, i));
251 for (j = 0; j < XVECLEN (x, i); j++)
253 printf (",\n\t\t");
254 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
256 printf (")");
257 break;
260 default:
261 gcc_unreachable ();
263 sep = ",\n\t";
265 printf (")");
268 /* Generate the `gen_...' function for a DEFINE_INSN. */
270 static void
271 gen_insn (rtx insn, int lineno)
273 struct pattern_stats stats;
274 int i;
276 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
277 registers or MATCH_SCRATCHes. If so, store away the information for
278 later. */
280 if (XVEC (insn, 1))
282 int has_hard_reg = 0;
284 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
286 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
287 break;
289 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
290 has_hard_reg = 1;
291 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
292 break;
295 if (i != XVECLEN (insn, 1) - 1)
297 struct clobber_pat *p;
298 struct clobber_ent *link = XNEW (struct clobber_ent);
299 int j;
301 link->code_number = insn_code_number;
303 /* See if any previous CLOBBER_LIST entry is the same as this
304 one. */
306 for (p = clobber_list; p; p = p->next)
308 if (p->first_clobber != i + 1
309 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
310 continue;
312 for (j = i + 1; j < XVECLEN (insn, 1); j++)
314 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
315 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
317 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
318 of the same mode,
319 or if both are registers of the same mode and number. */
320 if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx)
321 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
322 && GET_CODE (new_rtx) == MATCH_SCRATCH)
323 || (REG_P (old_rtx) && REG_P (new_rtx)
324 && REGNO (old_rtx) == REGNO (new_rtx)))))
325 break;
328 if (j == XVECLEN (insn, 1))
329 break;
332 if (p == 0)
334 p = XNEW (struct clobber_pat);
336 p->insns = 0;
337 p->pattern = insn;
338 p->first_clobber = i + 1;
339 p->next = clobber_list;
340 p->has_hard_reg = has_hard_reg;
341 clobber_list = p;
344 link->next = p->insns;
345 p->insns = link;
349 /* Don't mention instructions whose names are the null string
350 or begin with '*'. They are in the machine description just
351 to be recognized. */
352 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
353 return;
355 printf ("/* %s:%d */\n", read_md_filename, lineno);
357 /* Find out how many operands this function has. */
358 get_pattern_stats (&stats, XVEC (insn, 1));
359 if (stats.max_dup_opno > stats.max_opno)
360 fatal ("match_dup operand number has no match_operand");
362 /* Output the function name and argument declarations. */
363 printf ("rtx\ngen_%s (", XSTR (insn, 0));
364 if (stats.num_generator_args)
365 for (i = 0; i < stats.num_generator_args; i++)
366 if (i)
367 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
368 else
369 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
370 else
371 printf ("void");
372 printf (")\n");
373 printf ("{\n");
375 /* Output code to construct and return the rtl for the instruction body. */
377 if (XVECLEN (insn, 1) == 1)
379 printf (" return ");
380 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
381 printf (";\n}\n\n");
383 else
385 char *used = XCNEWVEC (char, stats.num_generator_args);
387 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
388 XVECLEN (insn, 1));
390 for (i = 0; i < XVECLEN (insn, 1); i++)
392 printf (",\n\t\t");
393 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, used);
395 printf ("));\n}\n\n");
396 XDELETEVEC (used);
400 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
402 static void
403 gen_expand (rtx expand)
405 struct pattern_stats stats;
406 int i;
407 char *used;
409 if (strlen (XSTR (expand, 0)) == 0)
410 fatal ("define_expand lacks a name");
411 if (XVEC (expand, 1) == 0)
412 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
414 /* Find out how many operands this function has. */
415 get_pattern_stats (&stats, XVEC (expand, 1));
417 /* Output the function name and argument declarations. */
418 printf ("rtx\ngen_%s (", XSTR (expand, 0));
419 if (stats.num_generator_args)
420 for (i = 0; i < stats.num_generator_args; i++)
421 if (i)
422 printf (",\n\trtx operand%d", i);
423 else
424 printf ("rtx operand%d", i);
425 else
426 printf ("void");
427 printf (")\n");
428 printf ("{\n");
430 /* If we don't have any C code to write, only one insn is being written,
431 and no MATCH_DUPs are present, we can just return the desired insn
432 like we do for a DEFINE_INSN. This saves memory. */
433 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
434 && stats.max_opno >= stats.max_dup_opno
435 && XVECLEN (expand, 1) == 1)
437 printf (" return ");
438 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
439 printf (";\n}\n\n");
440 return;
443 /* For each operand referred to only with MATCH_DUPs,
444 make a local variable. */
445 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
446 printf (" rtx operand%d;\n", i);
447 for (; i <= stats.max_scratch_opno; i++)
448 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
449 printf (" rtx _val = 0;\n");
450 printf (" start_sequence ();\n");
452 /* The fourth operand of DEFINE_EXPAND is some code to be executed
453 before the actual construction.
454 This code expects to refer to `operands'
455 just as the output-code in a DEFINE_INSN does,
456 but here `operands' is an automatic array.
457 So copy the operand values there before executing it. */
458 if (XSTR (expand, 3) && *XSTR (expand, 3))
460 printf (" {\n");
461 if (stats.num_operand_vars > 0)
462 printf (" rtx operands[%d];\n", stats.num_operand_vars);
464 /* Output code to copy the arguments into `operands'. */
465 for (i = 0; i < stats.num_generator_args; i++)
466 printf (" operands[%d] = operand%d;\n", i, i);
468 /* Output the special code to be executed before the sequence
469 is generated. */
470 print_md_ptr_loc (XSTR (expand, 3));
471 printf ("%s\n", XSTR (expand, 3));
473 /* Output code to copy the arguments back out of `operands'
474 (unless we aren't going to use them at all). */
475 if (XVEC (expand, 1) != 0)
477 for (i = 0; i < stats.num_operand_vars; i++)
479 printf (" operand%d = operands[%d];\n", i, i);
480 printf (" (void) operand%d;\n", i);
483 printf (" }\n");
486 /* Output code to construct the rtl for the instruction bodies.
487 Use emit_insn to add them to the sequence being accumulated.
488 But don't do this if the user's code has set `no_more' nonzero. */
490 used = XCNEWVEC (char, stats.num_operand_vars);
492 for (i = 0; i < XVECLEN (expand, 1); i++)
494 rtx next = XVECEXP (expand, 1, i);
495 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
496 || (GET_CODE (next) == PARALLEL
497 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
498 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
499 || ANY_RETURN_P (XVECEXP (next, 0, 0))))
500 || ANY_RETURN_P (next))
501 printf (" emit_jump_insn (");
502 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
503 || GET_CODE (next) == CALL
504 || (GET_CODE (next) == PARALLEL
505 && GET_CODE (XVECEXP (next, 0, 0)) == SET
506 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
507 || (GET_CODE (next) == PARALLEL
508 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
509 printf (" emit_call_insn (");
510 else if (LABEL_P (next))
511 printf (" emit_label (");
512 else if (GET_CODE (next) == MATCH_OPERAND
513 || GET_CODE (next) == MATCH_DUP
514 || GET_CODE (next) == MATCH_OPERATOR
515 || GET_CODE (next) == MATCH_OP_DUP
516 || GET_CODE (next) == MATCH_PARALLEL
517 || GET_CODE (next) == MATCH_PAR_DUP
518 || GET_CODE (next) == PARALLEL)
519 printf (" emit (");
520 else
521 printf (" emit_insn (");
522 gen_exp (next, DEFINE_EXPAND, used);
523 printf (");\n");
524 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
525 && GET_CODE (SET_SRC (next)) == LABEL_REF)
526 printf (" emit_barrier ();");
529 XDELETEVEC (used);
531 /* Call `get_insns' to extract the list of all the
532 insns emitted within this gen_... function. */
534 printf (" _val = get_insns ();\n");
535 printf (" end_sequence ();\n");
536 printf (" return _val;\n}\n\n");
539 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
541 static void
542 gen_split (rtx split)
544 struct pattern_stats stats;
545 int i;
546 const char *const name =
547 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
548 const char *unused;
549 char *used;
551 if (XVEC (split, 0) == 0)
552 fatal ("define_%s (definition %d) lacks a pattern", name,
553 insn_index_number);
554 else if (XVEC (split, 2) == 0)
555 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
556 insn_index_number);
558 /* Find out how many operands this function has. */
560 get_pattern_stats (&stats, XVEC (split, 2));
561 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
562 used = XCNEWVEC (char, stats.num_operand_vars);
564 /* Output the prototype, function name and argument declarations. */
565 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
567 printf ("extern rtx gen_%s_%d (rtx_insn *, rtx *);\n",
568 name, insn_code_number);
569 printf ("rtx\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
570 name, insn_code_number, unused);
572 else
574 printf ("extern rtx gen_split_%d (rtx_insn *, rtx *);\n", insn_code_number);
575 printf ("rtx\ngen_split_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
576 insn_code_number, unused);
578 printf ("{\n");
580 /* Declare all local variables. */
581 for (i = 0; i < stats.num_operand_vars; i++)
582 printf (" rtx operand%d;\n", i);
583 printf (" rtx _val = 0;\n");
585 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
586 output_peephole2_scratches (split);
588 printf (" if (dump_file)\n");
589 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n",
590 name, insn_code_number);
592 printf (" start_sequence ();\n");
594 /* The fourth operand of DEFINE_SPLIT is some code to be executed
595 before the actual construction. */
597 if (XSTR (split, 3))
599 print_md_ptr_loc (XSTR (split, 3));
600 printf ("%s\n", XSTR (split, 3));
603 /* Output code to copy the arguments back out of `operands' */
604 for (i = 0; i < stats.num_operand_vars; i++)
606 printf (" operand%d = operands[%d];\n", i, i);
607 printf (" (void) operand%d;\n", i);
610 /* Output code to construct the rtl for the instruction bodies.
611 Use emit_insn to add them to the sequence being accumulated.
612 But don't do this if the user's code has set `no_more' nonzero. */
614 for (i = 0; i < XVECLEN (split, 2); i++)
616 rtx next = XVECEXP (split, 2, i);
617 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
618 || (GET_CODE (next) == PARALLEL
619 && GET_CODE (XVECEXP (next, 0, 0)) == SET
620 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
621 || ANY_RETURN_P (next))
622 printf (" emit_jump_insn (");
623 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
624 || GET_CODE (next) == CALL
625 || (GET_CODE (next) == PARALLEL
626 && GET_CODE (XVECEXP (next, 0, 0)) == SET
627 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
628 || (GET_CODE (next) == PARALLEL
629 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
630 printf (" emit_call_insn (");
631 else if (LABEL_P (next))
632 printf (" emit_label (");
633 else if (GET_CODE (next) == MATCH_OPERAND
634 || GET_CODE (next) == MATCH_OPERATOR
635 || GET_CODE (next) == MATCH_PARALLEL
636 || GET_CODE (next) == MATCH_OP_DUP
637 || GET_CODE (next) == MATCH_DUP
638 || GET_CODE (next) == PARALLEL)
639 printf (" emit (");
640 else
641 printf (" emit_insn (");
642 gen_exp (next, GET_CODE (split), used);
643 printf (");\n");
644 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
645 && GET_CODE (SET_SRC (next)) == LABEL_REF)
646 printf (" emit_barrier ();");
649 /* Call `get_insns' to make a list of all the
650 insns emitted within this gen_... function. */
652 printf (" _val = get_insns ();\n");
653 printf (" end_sequence ();\n");
654 printf (" return _val;\n}\n\n");
656 free (used);
659 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
660 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
661 the end of the vector. */
663 static void
664 output_add_clobbers (void)
666 struct clobber_pat *clobber;
667 struct clobber_ent *ent;
668 int i;
670 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
671 printf ("{\n");
672 printf (" switch (insn_code_number)\n");
673 printf (" {\n");
675 for (clobber = clobber_list; clobber; clobber = clobber->next)
677 for (ent = clobber->insns; ent; ent = ent->next)
678 printf (" case %d:\n", ent->code_number);
680 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
682 printf (" XVECEXP (pattern, 0, %d) = ", i);
683 gen_exp (XVECEXP (clobber->pattern, 1, i),
684 GET_CODE (clobber->pattern), NULL);
685 printf (";\n");
688 printf (" break;\n\n");
691 printf (" default:\n");
692 printf (" gcc_unreachable ();\n");
693 printf (" }\n");
694 printf ("}\n");
697 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
698 number that will have clobbers added (as indicated by `recog') and returns
699 1 if those include a clobber of a hard reg or 0 if all of them just clobber
700 SCRATCH. */
702 static void
703 output_added_clobbers_hard_reg_p (void)
705 struct clobber_pat *clobber;
706 struct clobber_ent *ent;
707 int clobber_p, used;
709 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
710 printf ("{\n");
711 printf (" switch (insn_code_number)\n");
712 printf (" {\n");
714 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
716 used = 0;
717 for (clobber = clobber_list; clobber; clobber = clobber->next)
718 if (clobber->has_hard_reg == clobber_p)
719 for (ent = clobber->insns; ent; ent = ent->next)
721 printf (" case %d:\n", ent->code_number);
722 used++;
725 if (used)
726 printf (" return %d;\n\n", clobber_p);
729 printf (" default:\n");
730 printf (" gcc_unreachable ();\n");
731 printf (" }\n");
732 printf ("}\n");
735 /* Generate code to invoke find_free_register () as needed for the
736 scratch registers used by the peephole2 pattern in SPLIT. */
738 static void
739 output_peephole2_scratches (rtx split)
741 int i;
742 int insn_nr = 0;
743 bool first = true;
745 for (i = 0; i < XVECLEN (split, 0); i++)
747 rtx elt = XVECEXP (split, 0, i);
748 if (GET_CODE (elt) == MATCH_SCRATCH)
750 int last_insn_nr = insn_nr;
751 int cur_insn_nr = insn_nr;
752 int j;
753 for (j = i + 1; j < XVECLEN (split, 0); j++)
754 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
756 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
757 last_insn_nr = cur_insn_nr;
759 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
760 cur_insn_nr++;
762 if (first)
764 printf (" HARD_REG_SET _regs_allocated;\n");
765 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
766 first = false;
769 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
770 return NULL;\n",
771 XINT (elt, 0),
772 insn_nr, last_insn_nr,
773 XSTR (elt, 1),
774 GET_MODE_NAME (GET_MODE (elt)));
777 else if (GET_CODE (elt) != MATCH_DUP)
778 insn_nr++;
783 main (int argc, char **argv)
785 rtx desc;
787 progname = "genemit";
789 if (!init_rtx_reader_args (argc, argv))
790 return (FATAL_EXIT_CODE);
792 /* Assign sequential codes to all entries in the machine description
793 in parallel with the tables in insn-output.c. */
795 insn_code_number = 0;
796 insn_index_number = 0;
798 printf ("/* Generated automatically by the program `genemit'\n\
799 from the machine description file `md'. */\n\n");
801 printf ("#include \"config.h\"\n");
802 printf ("#include \"system.h\"\n");
803 printf ("#include \"coretypes.h\"\n");
804 printf ("#include \"tm.h\"\n");
805 printf ("#include \"hash-set.h\"\n");
806 printf ("#include \"machmode.h\"\n");
807 printf ("#include \"vec.h\"\n");
808 printf ("#include \"double-int.h\"\n");
809 printf ("#include \"input.h\"\n");
810 printf ("#include \"alias.h\"\n");
811 printf ("#include \"symtab.h\"\n");
812 printf ("#include \"wide-int.h\"\n");
813 printf ("#include \"inchash.h\"\n");
814 printf ("#include \"tree.h\"\n");
815 printf ("#include \"varasm.h\"\n");
816 printf ("#include \"stor-layout.h\"\n");
817 printf ("#include \"calls.h\"\n");
818 printf ("#include \"rtl.h\"\n");
819 printf ("#include \"tm_p.h\"\n");
820 printf ("#include \"hashtab.h\"\n");
821 printf ("#include \"hard-reg-set.h\"\n");
822 printf ("#include \"function.h\"\n");
823 printf ("#include \"flags.h\"\n");
824 printf ("#include \"statistics.h\"\n");
825 printf ("#include \"real.h\"\n");
826 printf ("#include \"fixed-value.h\"\n");
827 printf ("#include \"insn-config.h\"\n");
828 printf ("#include \"expmed.h\"\n");
829 printf ("#include \"dojump.h\"\n");
830 printf ("#include \"explow.h\"\n");
831 printf ("#include \"emit-rtl.h\"\n");
832 printf ("#include \"stmt.h\"\n");
833 printf ("#include \"expr.h\"\n");
834 printf ("#include \"insn-codes.h\"\n");
835 printf ("#include \"optabs.h\"\n");
836 printf ("#include \"dfp.h\"\n");
837 printf ("#include \"output.h\"\n");
838 printf ("#include \"recog.h\"\n");
839 printf ("#include \"predict.h\"\n");
840 printf ("#include \"basic-block.h\"\n");
841 printf ("#include \"resource.h\"\n");
842 printf ("#include \"reload.h\"\n");
843 printf ("#include \"diagnostic-core.h\"\n");
844 printf ("#include \"regs.h\"\n");
845 printf ("#include \"tm-constrs.h\"\n");
846 printf ("#include \"ggc.h\"\n");
847 printf ("#include \"basic-block.h\"\n");
848 printf ("#include \"dumpfile.h\"\n");
849 printf ("#include \"target.h\"\n\n");
850 printf ("#define FAIL return (end_sequence (), _val)\n");
851 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
853 /* Read the machine description. */
855 while (1)
857 int line_no;
859 desc = read_md_rtx (&line_no, &insn_code_number);
860 if (desc == NULL)
861 break;
863 switch (GET_CODE (desc))
865 case DEFINE_INSN:
866 gen_insn (desc, line_no);
867 break;
869 case DEFINE_EXPAND:
870 printf ("/* %s:%d */\n", read_md_filename, line_no);
871 gen_expand (desc);
872 break;
874 case DEFINE_SPLIT:
875 printf ("/* %s:%d */\n", read_md_filename, line_no);
876 gen_split (desc);
877 break;
879 case DEFINE_PEEPHOLE2:
880 printf ("/* %s:%d */\n", read_md_filename, line_no);
881 gen_split (desc);
882 break;
884 default:
885 break;
887 ++insn_index_number;
890 /* Write out the routines to add CLOBBERs to a pattern and say whether they
891 clobber a hard reg. */
892 output_add_clobbers ();
893 output_added_clobbers_hard_reg_p ();
895 fflush (stdout);
896 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);