2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / genemit.c
blob29437b95dae37aa21e6602143f449f937f661b38
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 'r':
244 printf ("%u", REGNO (x));
245 break;
247 case 's':
248 printf ("\"%s\"", XSTR (x, i));
249 break;
251 case 'E':
253 int j;
254 printf ("gen_rtvec (%d", XVECLEN (x, i));
255 for (j = 0; j < XVECLEN (x, i); j++)
257 printf (",\n\t\t");
258 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
260 printf (")");
261 break;
264 default:
265 gcc_unreachable ();
267 sep = ",\n\t";
269 printf (")");
272 /* Generate the `gen_...' function for a DEFINE_INSN. */
274 static void
275 gen_insn (rtx insn, int lineno)
277 struct pattern_stats stats;
278 int i;
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
282 later. */
284 if (XVEC (insn, 1))
286 int has_hard_reg = 0;
288 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
290 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
291 break;
293 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
294 has_hard_reg = 1;
295 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
296 break;
299 if (i != XVECLEN (insn, 1) - 1)
301 struct clobber_pat *p;
302 struct clobber_ent *link = XNEW (struct clobber_ent);
303 int j;
305 link->code_number = insn_code_number;
307 /* See if any previous CLOBBER_LIST entry is the same as this
308 one. */
310 for (p = clobber_list; p; p = p->next)
312 if (p->first_clobber != i + 1
313 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
314 continue;
316 for (j = i + 1; j < XVECLEN (insn, 1); j++)
318 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
319 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
321 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
322 of the same mode,
323 or if both are registers of the same mode and number. */
324 if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx)
325 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
326 && GET_CODE (new_rtx) == MATCH_SCRATCH)
327 || (REG_P (old_rtx) && REG_P (new_rtx)
328 && REGNO (old_rtx) == REGNO (new_rtx)))))
329 break;
332 if (j == XVECLEN (insn, 1))
333 break;
336 if (p == 0)
338 p = XNEW (struct clobber_pat);
340 p->insns = 0;
341 p->pattern = insn;
342 p->first_clobber = i + 1;
343 p->next = clobber_list;
344 p->has_hard_reg = has_hard_reg;
345 clobber_list = p;
348 link->next = p->insns;
349 p->insns = link;
353 /* Don't mention instructions whose names are the null string
354 or begin with '*'. They are in the machine description just
355 to be recognized. */
356 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
357 return;
359 printf ("/* %s:%d */\n", read_md_filename, lineno);
361 /* Find out how many operands this function has. */
362 get_pattern_stats (&stats, XVEC (insn, 1));
363 if (stats.max_dup_opno > stats.max_opno)
364 fatal ("match_dup operand number has no match_operand");
366 /* Output the function name and argument declarations. */
367 printf ("rtx\ngen_%s (", XSTR (insn, 0));
368 if (stats.num_generator_args)
369 for (i = 0; i < stats.num_generator_args; i++)
370 if (i)
371 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
372 else
373 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
374 else
375 printf ("void");
376 printf (")\n");
377 printf ("{\n");
379 /* Output code to construct and return the rtl for the instruction body. */
381 if (XVECLEN (insn, 1) == 1)
383 printf (" return ");
384 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
385 printf (";\n}\n\n");
387 else
389 char *used = XCNEWVEC (char, stats.num_generator_args);
391 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
392 XVECLEN (insn, 1));
394 for (i = 0; i < XVECLEN (insn, 1); i++)
396 printf (",\n\t\t");
397 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, used);
399 printf ("));\n}\n\n");
400 XDELETEVEC (used);
404 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
406 static void
407 gen_expand (rtx expand)
409 struct pattern_stats stats;
410 int i;
411 char *used;
413 if (strlen (XSTR (expand, 0)) == 0)
414 fatal ("define_expand lacks a name");
415 if (XVEC (expand, 1) == 0)
416 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
418 /* Find out how many operands this function has. */
419 get_pattern_stats (&stats, XVEC (expand, 1));
421 /* Output the function name and argument declarations. */
422 printf ("rtx\ngen_%s (", XSTR (expand, 0));
423 if (stats.num_generator_args)
424 for (i = 0; i < stats.num_generator_args; i++)
425 if (i)
426 printf (",\n\trtx operand%d", i);
427 else
428 printf ("rtx operand%d", i);
429 else
430 printf ("void");
431 printf (")\n");
432 printf ("{\n");
434 /* If we don't have any C code to write, only one insn is being written,
435 and no MATCH_DUPs are present, we can just return the desired insn
436 like we do for a DEFINE_INSN. This saves memory. */
437 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
438 && stats.max_opno >= stats.max_dup_opno
439 && XVECLEN (expand, 1) == 1)
441 printf (" return ");
442 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
443 printf (";\n}\n\n");
444 return;
447 /* For each operand referred to only with MATCH_DUPs,
448 make a local variable. */
449 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
450 printf (" rtx operand%d;\n", i);
451 for (; i <= stats.max_scratch_opno; i++)
452 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
453 printf (" rtx_insn *_val = 0;\n");
454 printf (" start_sequence ();\n");
456 /* The fourth operand of DEFINE_EXPAND is some code to be executed
457 before the actual construction.
458 This code expects to refer to `operands'
459 just as the output-code in a DEFINE_INSN does,
460 but here `operands' is an automatic array.
461 So copy the operand values there before executing it. */
462 if (XSTR (expand, 3) && *XSTR (expand, 3))
464 printf (" {\n");
465 if (stats.num_operand_vars > 0)
466 printf (" rtx operands[%d];\n", stats.num_operand_vars);
468 /* Output code to copy the arguments into `operands'. */
469 for (i = 0; i < stats.num_generator_args; i++)
470 printf (" operands[%d] = operand%d;\n", i, i);
472 /* Output the special code to be executed before the sequence
473 is generated. */
474 print_md_ptr_loc (XSTR (expand, 3));
475 printf ("%s\n", XSTR (expand, 3));
477 /* Output code to copy the arguments back out of `operands'
478 (unless we aren't going to use them at all). */
479 if (XVEC (expand, 1) != 0)
481 for (i = 0; i < stats.num_operand_vars; i++)
483 printf (" operand%d = operands[%d];\n", i, i);
484 printf (" (void) operand%d;\n", i);
487 printf (" }\n");
490 /* Output code to construct the rtl for the instruction bodies.
491 Use emit_insn to add them to the sequence being accumulated.
492 But don't do this if the user's code has set `no_more' nonzero. */
494 used = XCNEWVEC (char, stats.num_operand_vars);
496 for (i = 0; i < XVECLEN (expand, 1); i++)
498 rtx next = XVECEXP (expand, 1, i);
499 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
500 || (GET_CODE (next) == PARALLEL
501 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
502 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
503 || ANY_RETURN_P (XVECEXP (next, 0, 0))))
504 || ANY_RETURN_P (next))
505 printf (" emit_jump_insn (");
506 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
507 || GET_CODE (next) == CALL
508 || (GET_CODE (next) == PARALLEL
509 && GET_CODE (XVECEXP (next, 0, 0)) == SET
510 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
511 || (GET_CODE (next) == PARALLEL
512 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
513 printf (" emit_call_insn (");
514 else if (LABEL_P (next))
515 printf (" emit_label (");
516 else if (GET_CODE (next) == MATCH_OPERAND
517 || GET_CODE (next) == MATCH_DUP
518 || GET_CODE (next) == MATCH_OPERATOR
519 || GET_CODE (next) == MATCH_OP_DUP
520 || GET_CODE (next) == MATCH_PARALLEL
521 || GET_CODE (next) == MATCH_PAR_DUP
522 || GET_CODE (next) == PARALLEL)
523 printf (" emit (");
524 else
525 printf (" emit_insn (");
526 gen_exp (next, DEFINE_EXPAND, used);
527 printf (");\n");
528 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
529 && GET_CODE (SET_SRC (next)) == LABEL_REF)
530 printf (" emit_barrier ();");
533 XDELETEVEC (used);
535 /* Call `get_insns' to extract the list of all the
536 insns emitted within this gen_... function. */
538 printf (" _val = get_insns ();\n");
539 printf (" end_sequence ();\n");
540 printf (" return _val;\n}\n\n");
543 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
545 static void
546 gen_split (rtx split)
548 struct pattern_stats stats;
549 int i;
550 const char *const name =
551 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
552 const char *unused;
553 char *used;
555 if (XVEC (split, 0) == 0)
556 fatal ("define_%s (definition %d) lacks a pattern", name,
557 insn_index_number);
558 else if (XVEC (split, 2) == 0)
559 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
560 insn_index_number);
562 /* Find out how many operands this function has. */
564 get_pattern_stats (&stats, XVEC (split, 2));
565 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
566 used = XCNEWVEC (char, stats.num_operand_vars);
568 /* Output the prototype, function name and argument declarations. */
569 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
571 printf ("extern rtx_insn *gen_%s_%d (rtx_insn *, rtx *);\n",
572 name, insn_code_number);
573 printf ("rtx_insn *\ngen_%s_%d (rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
574 name, insn_code_number, unused);
576 else
578 printf ("extern rtx_insn *gen_split_%d (rtx_insn *, rtx *);\n",
579 insn_code_number);
580 printf ("rtx_insn *\ngen_split_%d "
581 "(rtx_insn *curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
582 insn_code_number, unused);
584 printf ("{\n");
586 /* Declare all local variables. */
587 for (i = 0; i < stats.num_operand_vars; i++)
588 printf (" rtx operand%d;\n", i);
589 printf (" rtx_insn *_val = NULL;\n");
591 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
592 output_peephole2_scratches (split);
594 printf (" if (dump_file)\n");
595 printf (" fprintf (dump_file, \"Splitting with gen_%s_%d\\n\");\n",
596 name, insn_code_number);
598 printf (" start_sequence ();\n");
600 /* The fourth operand of DEFINE_SPLIT is some code to be executed
601 before the actual construction. */
603 if (XSTR (split, 3))
605 print_md_ptr_loc (XSTR (split, 3));
606 printf ("%s\n", XSTR (split, 3));
609 /* Output code to copy the arguments back out of `operands' */
610 for (i = 0; i < stats.num_operand_vars; i++)
612 printf (" operand%d = operands[%d];\n", i, i);
613 printf (" (void) operand%d;\n", i);
616 /* Output code to construct the rtl for the instruction bodies.
617 Use emit_insn to add them to the sequence being accumulated.
618 But don't do this if the user's code has set `no_more' nonzero. */
620 for (i = 0; i < XVECLEN (split, 2); i++)
622 rtx next = XVECEXP (split, 2, i);
623 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
624 || (GET_CODE (next) == PARALLEL
625 && GET_CODE (XVECEXP (next, 0, 0)) == SET
626 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
627 || ANY_RETURN_P (next))
628 printf (" emit_jump_insn (");
629 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
630 || GET_CODE (next) == CALL
631 || (GET_CODE (next) == PARALLEL
632 && GET_CODE (XVECEXP (next, 0, 0)) == SET
633 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
634 || (GET_CODE (next) == PARALLEL
635 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
636 printf (" emit_call_insn (");
637 else if (LABEL_P (next))
638 printf (" emit_label (");
639 else if (GET_CODE (next) == MATCH_OPERAND
640 || GET_CODE (next) == MATCH_OPERATOR
641 || GET_CODE (next) == MATCH_PARALLEL
642 || GET_CODE (next) == MATCH_OP_DUP
643 || GET_CODE (next) == MATCH_DUP
644 || GET_CODE (next) == PARALLEL)
645 printf (" emit (");
646 else
647 printf (" emit_insn (");
648 gen_exp (next, GET_CODE (split), used);
649 printf (");\n");
650 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
651 && GET_CODE (SET_SRC (next)) == LABEL_REF)
652 printf (" emit_barrier ();");
655 /* Call `get_insns' to make a list of all the
656 insns emitted within this gen_... function. */
658 printf (" _val = get_insns ();\n");
659 printf (" end_sequence ();\n");
660 printf (" return _val;\n}\n\n");
662 free (used);
665 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
666 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
667 the end of the vector. */
669 static void
670 output_add_clobbers (void)
672 struct clobber_pat *clobber;
673 struct clobber_ent *ent;
674 int i;
676 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
677 printf ("{\n");
678 printf (" switch (insn_code_number)\n");
679 printf (" {\n");
681 for (clobber = clobber_list; clobber; clobber = clobber->next)
683 for (ent = clobber->insns; ent; ent = ent->next)
684 printf (" case %d:\n", ent->code_number);
686 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
688 printf (" XVECEXP (pattern, 0, %d) = ", i);
689 gen_exp (XVECEXP (clobber->pattern, 1, i),
690 GET_CODE (clobber->pattern), NULL);
691 printf (";\n");
694 printf (" break;\n\n");
697 printf (" default:\n");
698 printf (" gcc_unreachable ();\n");
699 printf (" }\n");
700 printf ("}\n");
703 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
704 number that will have clobbers added (as indicated by `recog') and returns
705 1 if those include a clobber of a hard reg or 0 if all of them just clobber
706 SCRATCH. */
708 static void
709 output_added_clobbers_hard_reg_p (void)
711 struct clobber_pat *clobber;
712 struct clobber_ent *ent;
713 int clobber_p, used;
715 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
716 printf ("{\n");
717 printf (" switch (insn_code_number)\n");
718 printf (" {\n");
720 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
722 used = 0;
723 for (clobber = clobber_list; clobber; clobber = clobber->next)
724 if (clobber->has_hard_reg == clobber_p)
725 for (ent = clobber->insns; ent; ent = ent->next)
727 printf (" case %d:\n", ent->code_number);
728 used++;
731 if (used)
732 printf (" return %d;\n\n", clobber_p);
735 printf (" default:\n");
736 printf (" gcc_unreachable ();\n");
737 printf (" }\n");
738 printf ("}\n");
741 /* Generate code to invoke find_free_register () as needed for the
742 scratch registers used by the peephole2 pattern in SPLIT. */
744 static void
745 output_peephole2_scratches (rtx split)
747 int i;
748 int insn_nr = 0;
749 bool first = true;
751 for (i = 0; i < XVECLEN (split, 0); i++)
753 rtx elt = XVECEXP (split, 0, i);
754 if (GET_CODE (elt) == MATCH_SCRATCH)
756 int last_insn_nr = insn_nr;
757 int cur_insn_nr = insn_nr;
758 int j;
759 for (j = i + 1; j < XVECLEN (split, 0); j++)
760 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
762 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
763 last_insn_nr = cur_insn_nr;
765 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
766 cur_insn_nr++;
768 if (first)
770 printf (" HARD_REG_SET _regs_allocated;\n");
771 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
772 first = false;
775 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
776 return NULL;\n",
777 XINT (elt, 0),
778 insn_nr, last_insn_nr,
779 XSTR (elt, 1),
780 GET_MODE_NAME (GET_MODE (elt)));
783 else if (GET_CODE (elt) != MATCH_DUP)
784 insn_nr++;
789 main (int argc, char **argv)
791 rtx desc;
793 progname = "genemit";
795 if (!init_rtx_reader_args (argc, argv))
796 return (FATAL_EXIT_CODE);
798 /* Assign sequential codes to all entries in the machine description
799 in parallel with the tables in insn-output.c. */
801 insn_code_number = 0;
802 insn_index_number = 0;
804 printf ("/* Generated automatically by the program `genemit'\n\
805 from the machine description file `md'. */\n\n");
807 printf ("#include \"config.h\"\n");
808 printf ("#include \"system.h\"\n");
809 printf ("#include \"coretypes.h\"\n");
810 printf ("#include \"tm.h\"\n");
811 printf ("#include \"input.h\"\n");
812 printf ("#include \"alias.h\"\n");
813 printf ("#include \"symtab.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 \"hard-reg-set.h\"\n");
821 printf ("#include \"function.h\"\n");
822 printf ("#include \"flags.h\"\n");
823 printf ("#include \"insn-config.h\"\n");
824 printf ("#include \"expmed.h\"\n");
825 printf ("#include \"dojump.h\"\n");
826 printf ("#include \"explow.h\"\n");
827 printf ("#include \"emit-rtl.h\"\n");
828 printf ("#include \"stmt.h\"\n");
829 printf ("#include \"expr.h\"\n");
830 printf ("#include \"insn-codes.h\"\n");
831 printf ("#include \"optabs.h\"\n");
832 printf ("#include \"dfp.h\"\n");
833 printf ("#include \"output.h\"\n");
834 printf ("#include \"recog.h\"\n");
835 printf ("#include \"predict.h\"\n");
836 printf ("#include \"basic-block.h\"\n");
837 printf ("#include \"resource.h\"\n");
838 printf ("#include \"reload.h\"\n");
839 printf ("#include \"diagnostic-core.h\"\n");
840 printf ("#include \"regs.h\"\n");
841 printf ("#include \"tm-constrs.h\"\n");
842 printf ("#include \"ggc.h\"\n");
843 printf ("#include \"basic-block.h\"\n");
844 printf ("#include \"dumpfile.h\"\n");
845 printf ("#include \"target.h\"\n\n");
846 printf ("#define FAIL return (end_sequence (), _val)\n");
847 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
849 /* Read the machine description. */
851 while (1)
853 int line_no;
855 desc = read_md_rtx (&line_no, &insn_code_number);
856 if (desc == NULL)
857 break;
859 switch (GET_CODE (desc))
861 case DEFINE_INSN:
862 gen_insn (desc, line_no);
863 break;
865 case DEFINE_EXPAND:
866 printf ("/* %s:%d */\n", read_md_filename, line_no);
867 gen_expand (desc);
868 break;
870 case DEFINE_SPLIT:
871 printf ("/* %s:%d */\n", read_md_filename, line_no);
872 gen_split (desc);
873 break;
875 case DEFINE_PEEPHOLE2:
876 printf ("/* %s:%d */\n", read_md_filename, line_no);
877 gen_split (desc);
878 break;
880 default:
881 break;
883 ++insn_index_number;
886 /* Write out the routines to add CLOBBERs to a pattern and say whether they
887 clobber a hard reg. */
888 output_add_clobbers ();
889 output_added_clobbers_hard_reg_p ();
891 fflush (stdout);
892 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);