PR tree-optimization/43833
[official-gcc/alias-decl.git] / gcc / genemit.c
blob1a6502585db3c717f0ab79aca46be627f3bbadd9
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
10 version.
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
15 for more details.
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/>. */
22 #include "bconfig.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "errors.h"
28 #include "gensupport.h"
31 static int max_opno;
32 static int max_dup_opno;
33 static int max_scratch_opno;
34 static int insn_code_number;
35 static int insn_index_number;
37 /* Data structure for recording the patterns of insns that have CLOBBERs.
38 We use this to output a function that adds these CLOBBERs to a
39 previously-allocated PARALLEL expression. */
41 struct clobber_pat
43 struct clobber_ent *insns;
44 rtx pattern;
45 int first_clobber;
46 struct clobber_pat *next;
47 int has_hard_reg;
48 } *clobber_list;
50 /* Records one insn that uses the clobber list. */
52 struct clobber_ent
54 int code_number; /* Counts only insns. */
55 struct clobber_ent *next;
58 static void max_operand_1 (rtx);
59 static int max_operand_vec (rtx, int);
60 static void print_code (RTX_CODE);
61 static void gen_exp (rtx, enum rtx_code, char *);
62 static void gen_insn (rtx, int);
63 static void gen_expand (rtx);
64 static void gen_split (rtx);
65 static void output_add_clobbers (void);
66 static void output_added_clobbers_hard_reg_p (void);
67 static void gen_rtx_scratch (rtx, enum rtx_code);
68 static void output_peephole2_scratches (rtx);
71 static void
72 max_operand_1 (rtx x)
74 RTX_CODE code;
75 int i;
76 int len;
77 const char *fmt;
79 if (x == 0)
80 return;
82 code = GET_CODE (x);
84 if (code == MATCH_OPERAND || code == MATCH_OPERATOR
85 || code == MATCH_PARALLEL)
86 max_opno = MAX (max_opno, XINT (x, 0));
87 if (code == MATCH_DUP || code == MATCH_OP_DUP || code == MATCH_PAR_DUP)
88 max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
89 if (code == MATCH_SCRATCH)
90 max_scratch_opno = MAX (max_scratch_opno, XINT (x, 0));
92 fmt = GET_RTX_FORMAT (code);
93 len = GET_RTX_LENGTH (code);
94 for (i = 0; i < len; i++)
96 if (fmt[i] == 'e' || fmt[i] == 'u')
97 max_operand_1 (XEXP (x, i));
98 else if (fmt[i] == 'E')
100 int j;
101 for (j = 0; j < XVECLEN (x, i); j++)
102 max_operand_1 (XVECEXP (x, i, j));
107 static int
108 max_operand_vec (rtx insn, int arg)
110 int len = XVECLEN (insn, arg);
111 int i;
113 max_opno = -1;
114 max_dup_opno = -1;
115 max_scratch_opno = -1;
117 for (i = 0; i < len; i++)
118 max_operand_1 (XVECEXP (insn, arg, i));
120 return max_opno + 1;
123 static void
124 print_code (RTX_CODE code)
126 const char *p1;
127 for (p1 = GET_RTX_NAME (code); *p1; p1++)
128 putchar (TOUPPER(*p1));
131 static void
132 gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
134 if (subroutine_type == DEFINE_PEEPHOLE2)
136 printf ("operand%d", XINT (x, 0));
138 else
140 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
144 /* Print a C expression to construct an RTX just like X,
145 substituting any operand references appearing within. */
147 static void
148 gen_exp (rtx x, enum rtx_code subroutine_type, char *used)
150 RTX_CODE code;
151 int i;
152 int len;
153 const char *fmt;
155 if (x == 0)
157 printf ("NULL_RTX");
158 return;
161 code = GET_CODE (x);
163 switch (code)
165 case MATCH_OPERAND:
166 case MATCH_DUP:
167 if (used)
169 if (used[XINT (x, 0)])
171 printf ("copy_rtx (operand%d)", XINT (x, 0));
172 return;
174 used[XINT (x, 0)] = 1;
176 printf ("operand%d", XINT (x, 0));
177 return;
179 case MATCH_OP_DUP:
180 printf ("gen_rtx_fmt_");
181 for (i = 0; i < XVECLEN (x, 1); i++)
182 printf ("e");
183 printf (" (GET_CODE (operand%d), ", XINT (x, 0));
184 if (GET_MODE (x) == VOIDmode)
185 printf ("GET_MODE (operand%d)", XINT (x, 0));
186 else
187 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
188 for (i = 0; i < XVECLEN (x, 1); i++)
190 printf (",\n\t\t");
191 gen_exp (XVECEXP (x, 1, i), subroutine_type, used);
193 printf (")");
194 return;
196 case MATCH_OPERATOR:
197 printf ("gen_rtx_fmt_");
198 for (i = 0; i < XVECLEN (x, 2); i++)
199 printf ("e");
200 printf (" (GET_CODE (operand%d)", XINT (x, 0));
201 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
202 for (i = 0; i < XVECLEN (x, 2); i++)
204 printf (",\n\t\t");
205 gen_exp (XVECEXP (x, 2, i), subroutine_type, used);
207 printf (")");
208 return;
210 case MATCH_PARALLEL:
211 case MATCH_PAR_DUP:
212 printf ("operand%d", XINT (x, 0));
213 return;
215 case MATCH_SCRATCH:
216 gen_rtx_scratch (x, subroutine_type);
217 return;
219 case ADDRESS:
220 fatal ("ADDRESS expression code used in named instruction pattern");
222 case PC:
223 printf ("pc_rtx");
224 return;
225 case CLOBBER:
226 if (REG_P (XEXP (x, 0)))
228 printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
229 REGNO (XEXP (x, 0)));
230 return;
232 break;
234 case CC0:
235 printf ("cc0_rtx");
236 return;
238 case CONST_INT:
239 if (INTVAL (x) == 0)
240 printf ("const0_rtx");
241 else if (INTVAL (x) == 1)
242 printf ("const1_rtx");
243 else if (INTVAL (x) == -1)
244 printf ("constm1_rtx");
245 else if (-MAX_SAVED_CONST_INT <= INTVAL (x)
246 && INTVAL (x) <= MAX_SAVED_CONST_INT)
247 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
248 (int) INTVAL (x));
249 else if (INTVAL (x) == STORE_FLAG_VALUE)
250 printf ("const_true_rtx");
251 else
253 printf ("GEN_INT (");
254 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
255 printf (")");
257 return;
259 case CONST_DOUBLE:
260 case CONST_FIXED:
261 /* These shouldn't be written in MD files. Instead, the appropriate
262 routines in varasm.c should be called. */
263 gcc_unreachable ();
265 default:
266 break;
269 printf ("gen_rtx_");
270 print_code (code);
271 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
273 fmt = GET_RTX_FORMAT (code);
274 len = GET_RTX_LENGTH (code);
275 for (i = 0; i < len; i++)
277 if (fmt[i] == '0')
278 break;
279 printf (",\n\t");
280 switch (fmt[i])
282 case 'e': case 'u':
283 gen_exp (XEXP (x, i), subroutine_type, used);
284 break;
286 case 'i':
287 printf ("%u", XINT (x, i));
288 break;
290 case 's':
291 printf ("\"%s\"", XSTR (x, i));
292 break;
294 case 'E':
296 int j;
297 printf ("gen_rtvec (%d", XVECLEN (x, i));
298 for (j = 0; j < XVECLEN (x, i); j++)
300 printf (",\n\t\t");
301 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
303 printf (")");
304 break;
307 default:
308 gcc_unreachable ();
311 printf (")");
314 /* Generate the `gen_...' function for a DEFINE_INSN. */
316 static void
317 gen_insn (rtx insn, int lineno)
319 int operands;
320 int i;
322 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
323 registers or MATCH_SCRATCHes. If so, store away the information for
324 later. */
326 if (XVEC (insn, 1))
328 int has_hard_reg = 0;
330 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
332 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
333 break;
335 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
336 has_hard_reg = 1;
337 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
338 break;
341 if (i != XVECLEN (insn, 1) - 1)
343 struct clobber_pat *p;
344 struct clobber_ent *link = XNEW (struct clobber_ent);
345 int j;
347 link->code_number = insn_code_number;
349 /* See if any previous CLOBBER_LIST entry is the same as this
350 one. */
352 for (p = clobber_list; p; p = p->next)
354 if (p->first_clobber != i + 1
355 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
356 continue;
358 for (j = i + 1; j < XVECLEN (insn, 1); j++)
360 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
361 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
363 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
364 of the same mode,
365 or if both are registers of the same mode and number. */
366 if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx)
367 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
368 && GET_CODE (new_rtx) == MATCH_SCRATCH)
369 || (REG_P (old_rtx) && REG_P (new_rtx)
370 && REGNO (old_rtx) == REGNO (new_rtx)))))
371 break;
374 if (j == XVECLEN (insn, 1))
375 break;
378 if (p == 0)
380 p = XNEW (struct clobber_pat);
382 p->insns = 0;
383 p->pattern = insn;
384 p->first_clobber = i + 1;
385 p->next = clobber_list;
386 p->has_hard_reg = has_hard_reg;
387 clobber_list = p;
390 link->next = p->insns;
391 p->insns = link;
395 /* Don't mention instructions whose names are the null string
396 or begin with '*'. They are in the machine description just
397 to be recognized. */
398 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
399 return;
401 printf ("/* %s:%d */\n", read_rtx_filename, lineno);
403 /* Find out how many operands this function has. */
404 operands = max_operand_vec (insn, 1);
405 if (max_dup_opno >= operands)
406 fatal ("match_dup operand number has no match_operand");
408 /* Output the function name and argument declarations. */
409 printf ("rtx\ngen_%s (", XSTR (insn, 0));
410 if (operands)
411 for (i = 0; i < operands; i++)
412 if (i)
413 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
414 else
415 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
416 else
417 printf ("void");
418 printf (")\n");
419 printf ("{\n");
421 /* Output code to construct and return the rtl for the instruction body. */
423 if (XVECLEN (insn, 1) == 1)
425 printf (" return ");
426 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
427 printf (";\n}\n\n");
429 else
431 char *used = XCNEWVEC (char, operands);
433 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
434 XVECLEN (insn, 1));
436 for (i = 0; i < XVECLEN (insn, 1); i++)
438 printf (",\n\t\t");
439 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, used);
441 printf ("));\n}\n\n");
442 XDELETEVEC (used);
446 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
448 static void
449 gen_expand (rtx expand)
451 int operands;
452 int i;
453 char *used;
455 if (strlen (XSTR (expand, 0)) == 0)
456 fatal ("define_expand lacks a name");
457 if (XVEC (expand, 1) == 0)
458 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
460 /* Find out how many operands this function has. */
461 operands = max_operand_vec (expand, 1);
463 /* Output the function name and argument declarations. */
464 printf ("rtx\ngen_%s (", XSTR (expand, 0));
465 if (operands)
466 for (i = 0; i < operands; i++)
467 if (i)
468 printf (",\n\trtx operand%d", i);
469 else
470 printf ("rtx operand%d", i);
471 else
472 printf ("void");
473 printf (")\n");
474 printf ("{\n");
476 /* If we don't have any C code to write, only one insn is being written,
477 and no MATCH_DUPs are present, we can just return the desired insn
478 like we do for a DEFINE_INSN. This saves memory. */
479 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
480 && operands > max_dup_opno
481 && XVECLEN (expand, 1) == 1)
483 printf (" return ");
484 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
485 printf (";\n}\n\n");
486 return;
489 /* For each operand referred to only with MATCH_DUPs,
490 make a local variable. */
491 for (i = operands; i <= max_dup_opno; i++)
492 printf (" rtx operand%d;\n", i);
493 for (; i <= max_scratch_opno; i++)
494 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
495 printf (" rtx _val = 0;\n");
496 printf (" start_sequence ();\n");
498 /* The fourth operand of DEFINE_EXPAND is some code to be executed
499 before the actual construction.
500 This code expects to refer to `operands'
501 just as the output-code in a DEFINE_INSN does,
502 but here `operands' is an automatic array.
503 So copy the operand values there before executing it. */
504 if (XSTR (expand, 3) && *XSTR (expand, 3))
506 printf (" {\n");
507 if (operands > 0 || max_dup_opno >= 0 || max_scratch_opno >= 0)
508 printf (" rtx operands[%d];\n",
509 MAX (operands, MAX (max_scratch_opno, max_dup_opno) + 1));
510 /* Output code to copy the arguments into `operands'. */
511 for (i = 0; i < operands; i++)
512 printf (" operands[%d] = operand%d;\n", i, i);
514 /* Output the special code to be executed before the sequence
515 is generated. */
516 print_rtx_ptr_loc (XSTR (expand, 3));
517 printf ("%s\n", XSTR (expand, 3));
519 /* Output code to copy the arguments back out of `operands'
520 (unless we aren't going to use them at all). */
521 if (XVEC (expand, 1) != 0)
523 for (i = 0;
524 i < MAX (operands, MAX (max_scratch_opno, max_dup_opno) + 1);
525 i++)
527 printf (" operand%d = operands[%d];\n", i, i);
528 printf (" (void) operand%d;\n", i);
531 printf (" }\n");
534 /* Output code to construct the rtl for the instruction bodies.
535 Use emit_insn to add them to the sequence being accumulated.
536 But don't do this if the user's code has set `no_more' nonzero. */
538 used = XCNEWVEC (char,
539 MAX (operands, MAX (max_scratch_opno, max_dup_opno) + 1));
541 for (i = 0; i < XVECLEN (expand, 1); i++)
543 rtx next = XVECEXP (expand, 1, i);
544 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
545 || (GET_CODE (next) == PARALLEL
546 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
547 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
548 || GET_CODE (XVECEXP (next, 0, 0)) == RETURN))
549 || GET_CODE (next) == RETURN)
550 printf (" emit_jump_insn (");
551 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
552 || GET_CODE (next) == CALL
553 || (GET_CODE (next) == PARALLEL
554 && GET_CODE (XVECEXP (next, 0, 0)) == SET
555 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
556 || (GET_CODE (next) == PARALLEL
557 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
558 printf (" emit_call_insn (");
559 else if (LABEL_P (next))
560 printf (" emit_label (");
561 else if (GET_CODE (next) == MATCH_OPERAND
562 || GET_CODE (next) == MATCH_DUP
563 || GET_CODE (next) == MATCH_OPERATOR
564 || GET_CODE (next) == MATCH_OP_DUP
565 || GET_CODE (next) == MATCH_PARALLEL
566 || GET_CODE (next) == MATCH_PAR_DUP
567 || GET_CODE (next) == PARALLEL)
568 printf (" emit (");
569 else
570 printf (" emit_insn (");
571 gen_exp (next, DEFINE_EXPAND, used);
572 printf (");\n");
573 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
574 && GET_CODE (SET_SRC (next)) == LABEL_REF)
575 printf (" emit_barrier ();");
578 XDELETEVEC (used);
580 /* Call `get_insns' to extract the list of all the
581 insns emitted within this gen_... function. */
583 printf (" _val = get_insns ();\n");
584 printf (" end_sequence ();\n");
585 printf (" return _val;\n}\n\n");
588 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
590 static void
591 gen_split (rtx split)
593 int i;
594 int operands;
595 const char *const name =
596 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
597 const char *unused;
598 char *used;
600 if (XVEC (split, 0) == 0)
601 fatal ("define_%s (definition %d) lacks a pattern", name,
602 insn_index_number);
603 else if (XVEC (split, 2) == 0)
604 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
605 insn_index_number);
607 /* Find out how many operands this function has. */
609 max_operand_vec (split, 2);
610 operands = MAX (max_opno, MAX (max_dup_opno, max_scratch_opno)) + 1;
611 unused = (operands == 0 ? " ATTRIBUTE_UNUSED" : "");
612 used = XCNEWVEC (char, operands);
614 /* Output the prototype, function name and argument declarations. */
615 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
617 printf ("extern rtx gen_%s_%d (rtx, rtx *);\n",
618 name, insn_code_number);
619 printf ("rtx\ngen_%s_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
620 name, insn_code_number, unused);
622 else
624 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", insn_code_number);
625 printf ("rtx\ngen_split_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
626 insn_code_number, unused);
628 printf ("{\n");
630 /* Declare all local variables. */
631 for (i = 0; i < operands; i++)
632 printf (" rtx operand%d;\n", i);
633 printf (" rtx _val = 0;\n");
635 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
636 output_peephole2_scratches (split);
638 printf (" start_sequence ();\n");
640 /* The fourth operand of DEFINE_SPLIT is some code to be executed
641 before the actual construction. */
643 if (XSTR (split, 3))
645 print_rtx_ptr_loc (XSTR (split, 3));
646 printf ("%s\n", XSTR (split, 3));
649 /* Output code to copy the arguments back out of `operands' */
650 for (i = 0; i < operands; i++)
652 printf (" operand%d = operands[%d];\n", i, i);
653 printf (" (void) operand%d;\n", i);
656 /* Output code to construct the rtl for the instruction bodies.
657 Use emit_insn to add them to the sequence being accumulated.
658 But don't do this if the user's code has set `no_more' nonzero. */
660 for (i = 0; i < XVECLEN (split, 2); i++)
662 rtx next = XVECEXP (split, 2, i);
663 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
664 || (GET_CODE (next) == PARALLEL
665 && GET_CODE (XVECEXP (next, 0, 0)) == SET
666 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
667 || GET_CODE (next) == RETURN)
668 printf (" emit_jump_insn (");
669 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
670 || GET_CODE (next) == CALL
671 || (GET_CODE (next) == PARALLEL
672 && GET_CODE (XVECEXP (next, 0, 0)) == SET
673 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
674 || (GET_CODE (next) == PARALLEL
675 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
676 printf (" emit_call_insn (");
677 else if (LABEL_P (next))
678 printf (" emit_label (");
679 else if (GET_CODE (next) == MATCH_OPERAND
680 || GET_CODE (next) == MATCH_OPERATOR
681 || GET_CODE (next) == MATCH_PARALLEL
682 || GET_CODE (next) == MATCH_OP_DUP
683 || GET_CODE (next) == MATCH_DUP
684 || GET_CODE (next) == PARALLEL)
685 printf (" emit (");
686 else
687 printf (" emit_insn (");
688 gen_exp (next, GET_CODE (split), used);
689 printf (");\n");
690 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
691 && GET_CODE (SET_SRC (next)) == LABEL_REF)
692 printf (" emit_barrier ();");
695 /* Call `get_insns' to make a list of all the
696 insns emitted within this gen_... function. */
698 printf (" _val = get_insns ();\n");
699 printf (" end_sequence ();\n");
700 printf (" return _val;\n}\n\n");
702 free (used);
705 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
706 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
707 the end of the vector. */
709 static void
710 output_add_clobbers (void)
712 struct clobber_pat *clobber;
713 struct clobber_ent *ent;
714 int i;
716 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
717 printf ("{\n");
718 printf (" switch (insn_code_number)\n");
719 printf (" {\n");
721 for (clobber = clobber_list; clobber; clobber = clobber->next)
723 for (ent = clobber->insns; ent; ent = ent->next)
724 printf (" case %d:\n", ent->code_number);
726 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
728 printf (" XVECEXP (pattern, 0, %d) = ", i);
729 gen_exp (XVECEXP (clobber->pattern, 1, i),
730 GET_CODE (clobber->pattern), NULL);
731 printf (";\n");
734 printf (" break;\n\n");
737 printf (" default:\n");
738 printf (" gcc_unreachable ();\n");
739 printf (" }\n");
740 printf ("}\n");
743 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
744 number that will have clobbers added (as indicated by `recog') and returns
745 1 if those include a clobber of a hard reg or 0 if all of them just clobber
746 SCRATCH. */
748 static void
749 output_added_clobbers_hard_reg_p (void)
751 struct clobber_pat *clobber;
752 struct clobber_ent *ent;
753 int clobber_p, used;
755 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
756 printf ("{\n");
757 printf (" switch (insn_code_number)\n");
758 printf (" {\n");
760 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
762 used = 0;
763 for (clobber = clobber_list; clobber; clobber = clobber->next)
764 if (clobber->has_hard_reg == clobber_p)
765 for (ent = clobber->insns; ent; ent = ent->next)
767 printf (" case %d:\n", ent->code_number);
768 used++;
771 if (used)
772 printf (" return %d;\n\n", clobber_p);
775 printf (" default:\n");
776 printf (" gcc_unreachable ();\n");
777 printf (" }\n");
778 printf ("}\n");
781 /* Generate code to invoke find_free_register () as needed for the
782 scratch registers used by the peephole2 pattern in SPLIT. */
784 static void
785 output_peephole2_scratches (rtx split)
787 int i;
788 int insn_nr = 0;
789 bool first = true;
791 for (i = 0; i < XVECLEN (split, 0); i++)
793 rtx elt = XVECEXP (split, 0, i);
794 if (GET_CODE (elt) == MATCH_SCRATCH)
796 int last_insn_nr = insn_nr;
797 int cur_insn_nr = insn_nr;
798 int j;
799 for (j = i + 1; j < XVECLEN (split, 0); j++)
800 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
802 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
803 last_insn_nr = cur_insn_nr;
805 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
806 cur_insn_nr++;
808 if (first)
810 printf (" HARD_REG_SET _regs_allocated;\n");
811 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
812 first = false;
815 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
816 return NULL;\n",
817 XINT (elt, 0),
818 insn_nr, last_insn_nr,
819 XSTR (elt, 1),
820 GET_MODE_NAME (GET_MODE (elt)));
823 else if (GET_CODE (elt) != MATCH_DUP)
824 insn_nr++;
829 main (int argc, char **argv)
831 rtx desc;
833 progname = "genemit";
835 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
836 return (FATAL_EXIT_CODE);
838 /* Assign sequential codes to all entries in the machine description
839 in parallel with the tables in insn-output.c. */
841 insn_code_number = 0;
842 insn_index_number = 0;
844 printf ("/* Generated automatically by the program `genemit'\n\
845 from the machine description file `md'. */\n\n");
847 printf ("#include \"config.h\"\n");
848 printf ("#include \"system.h\"\n");
849 printf ("#include \"coretypes.h\"\n");
850 printf ("#include \"tm.h\"\n");
851 printf ("#include \"rtl.h\"\n");
852 printf ("#include \"tm_p.h\"\n");
853 printf ("#include \"function.h\"\n");
854 printf ("#include \"expr.h\"\n");
855 printf ("#include \"optabs.h\"\n");
856 printf ("#include \"real.h\"\n");
857 printf ("#include \"dfp.h\"\n");
858 printf ("#include \"flags.h\"\n");
859 printf ("#include \"output.h\"\n");
860 printf ("#include \"insn-config.h\"\n");
861 printf ("#include \"hard-reg-set.h\"\n");
862 printf ("#include \"recog.h\"\n");
863 printf ("#include \"resource.h\"\n");
864 printf ("#include \"reload.h\"\n");
865 printf ("#include \"toplev.h\"\n");
866 printf ("#include \"regs.h\"\n");
867 printf ("#include \"tm-constrs.h\"\n");
868 printf ("#include \"ggc.h\"\n");
869 printf ("#include \"basic-block.h\"\n");
870 printf ("#include \"integrate.h\"\n\n");
871 printf ("#define FAIL return (end_sequence (), _val)\n");
872 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
874 /* Read the machine description. */
876 while (1)
878 int line_no;
880 desc = read_md_rtx (&line_no, &insn_code_number);
881 if (desc == NULL)
882 break;
884 switch (GET_CODE (desc))
886 case DEFINE_INSN:
887 gen_insn (desc, line_no);
888 break;
890 case DEFINE_EXPAND:
891 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
892 gen_expand (desc);
893 break;
895 case DEFINE_SPLIT:
896 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
897 gen_split (desc);
898 break;
900 case DEFINE_PEEPHOLE2:
901 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
902 gen_split (desc);
903 break;
905 default:
906 break;
908 ++insn_index_number;
911 /* Write out the routines to add CLOBBERs to a pattern and say whether they
912 clobber a hard reg. */
913 output_add_clobbers ();
914 output_added_clobbers_hard_reg_p ();
916 fflush (stdout);
917 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);