re PR fortran/55618 (Failures with ISO_Varying_String test suite)
[gcc.git] / gcc / genemit.c
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, 2011, 2012
4 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22
23 #include "bconfig.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "errors.h"
29 #include "read-md.h"
30 #include "gensupport.h"
31
32
33 static int insn_code_number;
34 static int insn_index_number;
35
36 /* Data structure for recording the patterns of insns that have CLOBBERs.
37 We use this to output a function that adds these CLOBBERs to a
38 previously-allocated PARALLEL expression. */
39
40 struct clobber_pat
41 {
42 struct clobber_ent *insns;
43 rtx pattern;
44 int first_clobber;
45 struct clobber_pat *next;
46 int has_hard_reg;
47 } *clobber_list;
48
49 /* Records one insn that uses the clobber list. */
50
51 struct clobber_ent
52 {
53 int code_number; /* Counts only insns. */
54 struct clobber_ent *next;
55 };
56
57 static void print_code (RTX_CODE);
58 static void gen_exp (rtx, enum rtx_code, char *);
59 static void gen_insn (rtx, int);
60 static void gen_expand (rtx);
61 static void gen_split (rtx);
62 static void output_add_clobbers (void);
63 static void output_added_clobbers_hard_reg_p (void);
64 static void gen_rtx_scratch (rtx, enum rtx_code);
65 static void output_peephole2_scratches (rtx);
66
67 \f
68 static void
69 print_code (RTX_CODE code)
70 {
71 const char *p1;
72 for (p1 = GET_RTX_NAME (code); *p1; p1++)
73 putchar (TOUPPER(*p1));
74 }
75
76 static void
77 gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
78 {
79 if (subroutine_type == DEFINE_PEEPHOLE2)
80 {
81 printf ("operand%d", XINT (x, 0));
82 }
83 else
84 {
85 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
86 }
87 }
88
89 /* Print a C expression to construct an RTX just like X,
90 substituting any operand references appearing within. */
91
92 static void
93 gen_exp (rtx x, enum rtx_code subroutine_type, char *used)
94 {
95 RTX_CODE code;
96 int i;
97 int len;
98 const char *fmt;
99
100 if (x == 0)
101 {
102 printf ("NULL_RTX");
103 return;
104 }
105
106 code = GET_CODE (x);
107
108 switch (code)
109 {
110 case MATCH_OPERAND:
111 case MATCH_DUP:
112 if (used)
113 {
114 if (used[XINT (x, 0)])
115 {
116 printf ("copy_rtx (operand%d)", XINT (x, 0));
117 return;
118 }
119 used[XINT (x, 0)] = 1;
120 }
121 printf ("operand%d", XINT (x, 0));
122 return;
123
124 case MATCH_OP_DUP:
125 printf ("gen_rtx_fmt_");
126 for (i = 0; i < XVECLEN (x, 1); i++)
127 printf ("e");
128 printf (" (GET_CODE (operand%d), ", XINT (x, 0));
129 if (GET_MODE (x) == VOIDmode)
130 printf ("GET_MODE (operand%d)", XINT (x, 0));
131 else
132 printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
133 for (i = 0; i < XVECLEN (x, 1); i++)
134 {
135 printf (",\n\t\t");
136 gen_exp (XVECEXP (x, 1, i), subroutine_type, used);
137 }
138 printf (")");
139 return;
140
141 case MATCH_OPERATOR:
142 printf ("gen_rtx_fmt_");
143 for (i = 0; i < XVECLEN (x, 2); i++)
144 printf ("e");
145 printf (" (GET_CODE (operand%d)", XINT (x, 0));
146 printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
147 for (i = 0; i < XVECLEN (x, 2); i++)
148 {
149 printf (",\n\t\t");
150 gen_exp (XVECEXP (x, 2, i), subroutine_type, used);
151 }
152 printf (")");
153 return;
154
155 case MATCH_PARALLEL:
156 case MATCH_PAR_DUP:
157 printf ("operand%d", XINT (x, 0));
158 return;
159
160 case MATCH_SCRATCH:
161 gen_rtx_scratch (x, subroutine_type);
162 return;
163
164 case PC:
165 printf ("pc_rtx");
166 return;
167 case RETURN:
168 printf ("ret_rtx");
169 return;
170 case SIMPLE_RETURN:
171 printf ("simple_return_rtx");
172 return;
173 case CLOBBER:
174 if (REG_P (XEXP (x, 0)))
175 {
176 printf ("gen_hard_reg_clobber (%smode, %i)", GET_MODE_NAME (GET_MODE (XEXP (x, 0))),
177 REGNO (XEXP (x, 0)));
178 return;
179 }
180 break;
181
182 case CC0:
183 printf ("cc0_rtx");
184 return;
185
186 case CONST_INT:
187 if (INTVAL (x) == 0)
188 printf ("const0_rtx");
189 else if (INTVAL (x) == 1)
190 printf ("const1_rtx");
191 else if (INTVAL (x) == -1)
192 printf ("constm1_rtx");
193 else if (-MAX_SAVED_CONST_INT <= INTVAL (x)
194 && INTVAL (x) <= MAX_SAVED_CONST_INT)
195 printf ("const_int_rtx[MAX_SAVED_CONST_INT + (%d)]",
196 (int) INTVAL (x));
197 else if (INTVAL (x) == STORE_FLAG_VALUE)
198 printf ("const_true_rtx");
199 else
200 {
201 printf ("GEN_INT (");
202 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
203 printf (")");
204 }
205 return;
206
207 case CONST_DOUBLE:
208 case CONST_FIXED:
209 /* These shouldn't be written in MD files. Instead, the appropriate
210 routines in varasm.c should be called. */
211 gcc_unreachable ();
212
213 default:
214 break;
215 }
216
217 printf ("gen_rtx_");
218 print_code (code);
219 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
220
221 fmt = GET_RTX_FORMAT (code);
222 len = GET_RTX_LENGTH (code);
223 for (i = 0; i < len; i++)
224 {
225 if (fmt[i] == '0')
226 break;
227 printf (",\n\t");
228 switch (fmt[i])
229 {
230 case 'e': case 'u':
231 gen_exp (XEXP (x, i), subroutine_type, used);
232 break;
233
234 case 'i':
235 printf ("%u", XINT (x, i));
236 break;
237
238 case 's':
239 printf ("\"%s\"", XSTR (x, i));
240 break;
241
242 case 'E':
243 {
244 int j;
245 printf ("gen_rtvec (%d", XVECLEN (x, i));
246 for (j = 0; j < XVECLEN (x, i); j++)
247 {
248 printf (",\n\t\t");
249 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
250 }
251 printf (")");
252 break;
253 }
254
255 default:
256 gcc_unreachable ();
257 }
258 }
259 printf (")");
260 }
261 \f
262 /* Generate the `gen_...' function for a DEFINE_INSN. */
263
264 static void
265 gen_insn (rtx insn, int lineno)
266 {
267 struct pattern_stats stats;
268 int i;
269
270 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
271 registers or MATCH_SCRATCHes. If so, store away the information for
272 later. */
273
274 if (XVEC (insn, 1))
275 {
276 int has_hard_reg = 0;
277
278 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
279 {
280 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
281 break;
282
283 if (REG_P (XEXP (XVECEXP (insn, 1, i), 0)))
284 has_hard_reg = 1;
285 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
286 break;
287 }
288
289 if (i != XVECLEN (insn, 1) - 1)
290 {
291 struct clobber_pat *p;
292 struct clobber_ent *link = XNEW (struct clobber_ent);
293 int j;
294
295 link->code_number = insn_code_number;
296
297 /* See if any previous CLOBBER_LIST entry is the same as this
298 one. */
299
300 for (p = clobber_list; p; p = p->next)
301 {
302 if (p->first_clobber != i + 1
303 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
304 continue;
305
306 for (j = i + 1; j < XVECLEN (insn, 1); j++)
307 {
308 rtx old_rtx = XEXP (XVECEXP (p->pattern, 1, j), 0);
309 rtx new_rtx = XEXP (XVECEXP (insn, 1, j), 0);
310
311 /* OLD and NEW_INSN are the same if both are to be a SCRATCH
312 of the same mode,
313 or if both are registers of the same mode and number. */
314 if (! (GET_MODE (old_rtx) == GET_MODE (new_rtx)
315 && ((GET_CODE (old_rtx) == MATCH_SCRATCH
316 && GET_CODE (new_rtx) == MATCH_SCRATCH)
317 || (REG_P (old_rtx) && REG_P (new_rtx)
318 && REGNO (old_rtx) == REGNO (new_rtx)))))
319 break;
320 }
321
322 if (j == XVECLEN (insn, 1))
323 break;
324 }
325
326 if (p == 0)
327 {
328 p = XNEW (struct clobber_pat);
329
330 p->insns = 0;
331 p->pattern = insn;
332 p->first_clobber = i + 1;
333 p->next = clobber_list;
334 p->has_hard_reg = has_hard_reg;
335 clobber_list = p;
336 }
337
338 link->next = p->insns;
339 p->insns = link;
340 }
341 }
342
343 /* Don't mention instructions whose names are the null string
344 or begin with '*'. They are in the machine description just
345 to be recognized. */
346 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
347 return;
348
349 printf ("/* %s:%d */\n", read_md_filename, lineno);
350
351 /* Find out how many operands this function has. */
352 get_pattern_stats (&stats, XVEC (insn, 1));
353 if (stats.max_dup_opno > stats.max_opno)
354 fatal ("match_dup operand number has no match_operand");
355
356 /* Output the function name and argument declarations. */
357 printf ("rtx\ngen_%s (", XSTR (insn, 0));
358 if (stats.num_generator_args)
359 for (i = 0; i < stats.num_generator_args; i++)
360 if (i)
361 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
362 else
363 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
364 else
365 printf ("void");
366 printf (")\n");
367 printf ("{\n");
368
369 /* Output code to construct and return the rtl for the instruction body. */
370
371 if (XVECLEN (insn, 1) == 1)
372 {
373 printf (" return ");
374 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
375 printf (";\n}\n\n");
376 }
377 else
378 {
379 char *used = XCNEWVEC (char, stats.num_generator_args);
380
381 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
382 XVECLEN (insn, 1));
383
384 for (i = 0; i < XVECLEN (insn, 1); i++)
385 {
386 printf (",\n\t\t");
387 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, used);
388 }
389 printf ("));\n}\n\n");
390 XDELETEVEC (used);
391 }
392 }
393 \f
394 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
395
396 static void
397 gen_expand (rtx expand)
398 {
399 struct pattern_stats stats;
400 int i;
401 char *used;
402
403 if (strlen (XSTR (expand, 0)) == 0)
404 fatal ("define_expand lacks a name");
405 if (XVEC (expand, 1) == 0)
406 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
407
408 /* Find out how many operands this function has. */
409 get_pattern_stats (&stats, XVEC (expand, 1));
410
411 /* Output the function name and argument declarations. */
412 printf ("rtx\ngen_%s (", XSTR (expand, 0));
413 if (stats.num_generator_args)
414 for (i = 0; i < stats.num_generator_args; i++)
415 if (i)
416 printf (",\n\trtx operand%d", i);
417 else
418 printf ("rtx operand%d", i);
419 else
420 printf ("void");
421 printf (")\n");
422 printf ("{\n");
423
424 /* If we don't have any C code to write, only one insn is being written,
425 and no MATCH_DUPs are present, we can just return the desired insn
426 like we do for a DEFINE_INSN. This saves memory. */
427 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
428 && stats.max_opno >= stats.max_dup_opno
429 && XVECLEN (expand, 1) == 1)
430 {
431 printf (" return ");
432 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
433 printf (";\n}\n\n");
434 return;
435 }
436
437 /* For each operand referred to only with MATCH_DUPs,
438 make a local variable. */
439 for (i = stats.num_generator_args; i <= stats.max_dup_opno; i++)
440 printf (" rtx operand%d;\n", i);
441 for (; i <= stats.max_scratch_opno; i++)
442 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
443 printf (" rtx _val = 0;\n");
444 printf (" start_sequence ();\n");
445
446 /* The fourth operand of DEFINE_EXPAND is some code to be executed
447 before the actual construction.
448 This code expects to refer to `operands'
449 just as the output-code in a DEFINE_INSN does,
450 but here `operands' is an automatic array.
451 So copy the operand values there before executing it. */
452 if (XSTR (expand, 3) && *XSTR (expand, 3))
453 {
454 printf (" {\n");
455 if (stats.num_operand_vars > 0)
456 printf (" rtx operands[%d];\n", stats.num_operand_vars);
457
458 /* Output code to copy the arguments into `operands'. */
459 for (i = 0; i < stats.num_generator_args; i++)
460 printf (" operands[%d] = operand%d;\n", i, i);
461
462 /* Output the special code to be executed before the sequence
463 is generated. */
464 print_md_ptr_loc (XSTR (expand, 3));
465 printf ("%s\n", XSTR (expand, 3));
466
467 /* Output code to copy the arguments back out of `operands'
468 (unless we aren't going to use them at all). */
469 if (XVEC (expand, 1) != 0)
470 {
471 for (i = 0; i < stats.num_operand_vars; i++)
472 {
473 printf (" operand%d = operands[%d];\n", i, i);
474 printf (" (void) operand%d;\n", i);
475 }
476 }
477 printf (" }\n");
478 }
479
480 /* Output code to construct the rtl for the instruction bodies.
481 Use emit_insn to add them to the sequence being accumulated.
482 But don't do this if the user's code has set `no_more' nonzero. */
483
484 used = XCNEWVEC (char, stats.num_operand_vars);
485
486 for (i = 0; i < XVECLEN (expand, 1); i++)
487 {
488 rtx next = XVECEXP (expand, 1, i);
489 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
490 || (GET_CODE (next) == PARALLEL
491 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
492 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
493 || ANY_RETURN_P (XVECEXP (next, 0, 0))))
494 || ANY_RETURN_P (next))
495 printf (" emit_jump_insn (");
496 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
497 || GET_CODE (next) == CALL
498 || (GET_CODE (next) == PARALLEL
499 && GET_CODE (XVECEXP (next, 0, 0)) == SET
500 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
501 || (GET_CODE (next) == PARALLEL
502 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
503 printf (" emit_call_insn (");
504 else if (LABEL_P (next))
505 printf (" emit_label (");
506 else if (GET_CODE (next) == MATCH_OPERAND
507 || GET_CODE (next) == MATCH_DUP
508 || GET_CODE (next) == MATCH_OPERATOR
509 || GET_CODE (next) == MATCH_OP_DUP
510 || GET_CODE (next) == MATCH_PARALLEL
511 || GET_CODE (next) == MATCH_PAR_DUP
512 || GET_CODE (next) == PARALLEL)
513 printf (" emit (");
514 else
515 printf (" emit_insn (");
516 gen_exp (next, DEFINE_EXPAND, used);
517 printf (");\n");
518 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
519 && GET_CODE (SET_SRC (next)) == LABEL_REF)
520 printf (" emit_barrier ();");
521 }
522
523 XDELETEVEC (used);
524
525 /* Call `get_insns' to extract the list of all the
526 insns emitted within this gen_... function. */
527
528 printf (" _val = get_insns ();\n");
529 printf (" end_sequence ();\n");
530 printf (" return _val;\n}\n\n");
531 }
532
533 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
534
535 static void
536 gen_split (rtx split)
537 {
538 struct pattern_stats stats;
539 int i;
540 const char *const name =
541 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
542 const char *unused;
543 char *used;
544
545 if (XVEC (split, 0) == 0)
546 fatal ("define_%s (definition %d) lacks a pattern", name,
547 insn_index_number);
548 else if (XVEC (split, 2) == 0)
549 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
550 insn_index_number);
551
552 /* Find out how many operands this function has. */
553
554 get_pattern_stats (&stats, XVEC (split, 2));
555 unused = (stats.num_operand_vars == 0 ? " ATTRIBUTE_UNUSED" : "");
556 used = XCNEWVEC (char, stats.num_operand_vars);
557
558 /* Output the prototype, function name and argument declarations. */
559 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
560 {
561 printf ("extern rtx gen_%s_%d (rtx, rtx *);\n",
562 name, insn_code_number);
563 printf ("rtx\ngen_%s_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
564 name, insn_code_number, unused);
565 }
566 else
567 {
568 printf ("extern rtx gen_split_%d (rtx, rtx *);\n", insn_code_number);
569 printf ("rtx\ngen_split_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
570 insn_code_number, unused);
571 }
572 printf ("{\n");
573
574 /* Declare all local variables. */
575 for (i = 0; i < stats.num_operand_vars; i++)
576 printf (" rtx operand%d;\n", i);
577 printf (" rtx _val = 0;\n");
578
579 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
580 output_peephole2_scratches (split);
581
582 printf (" start_sequence ();\n");
583
584 /* The fourth operand of DEFINE_SPLIT is some code to be executed
585 before the actual construction. */
586
587 if (XSTR (split, 3))
588 {
589 print_md_ptr_loc (XSTR (split, 3));
590 printf ("%s\n", XSTR (split, 3));
591 }
592
593 /* Output code to copy the arguments back out of `operands' */
594 for (i = 0; i < stats.num_operand_vars; i++)
595 {
596 printf (" operand%d = operands[%d];\n", i, i);
597 printf (" (void) operand%d;\n", i);
598 }
599
600 /* Output code to construct the rtl for the instruction bodies.
601 Use emit_insn to add them to the sequence being accumulated.
602 But don't do this if the user's code has set `no_more' nonzero. */
603
604 for (i = 0; i < XVECLEN (split, 2); i++)
605 {
606 rtx next = XVECEXP (split, 2, i);
607 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
608 || (GET_CODE (next) == PARALLEL
609 && GET_CODE (XVECEXP (next, 0, 0)) == SET
610 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
611 || ANY_RETURN_P (next))
612 printf (" emit_jump_insn (");
613 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
614 || GET_CODE (next) == CALL
615 || (GET_CODE (next) == PARALLEL
616 && GET_CODE (XVECEXP (next, 0, 0)) == SET
617 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
618 || (GET_CODE (next) == PARALLEL
619 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
620 printf (" emit_call_insn (");
621 else if (LABEL_P (next))
622 printf (" emit_label (");
623 else if (GET_CODE (next) == MATCH_OPERAND
624 || GET_CODE (next) == MATCH_OPERATOR
625 || GET_CODE (next) == MATCH_PARALLEL
626 || GET_CODE (next) == MATCH_OP_DUP
627 || GET_CODE (next) == MATCH_DUP
628 || GET_CODE (next) == PARALLEL)
629 printf (" emit (");
630 else
631 printf (" emit_insn (");
632 gen_exp (next, GET_CODE (split), used);
633 printf (");\n");
634 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
635 && GET_CODE (SET_SRC (next)) == LABEL_REF)
636 printf (" emit_barrier ();");
637 }
638
639 /* Call `get_insns' to make a list of all the
640 insns emitted within this gen_... function. */
641
642 printf (" _val = get_insns ();\n");
643 printf (" end_sequence ();\n");
644 printf (" return _val;\n}\n\n");
645
646 free (used);
647 }
648 \f
649 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
650 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
651 the end of the vector. */
652
653 static void
654 output_add_clobbers (void)
655 {
656 struct clobber_pat *clobber;
657 struct clobber_ent *ent;
658 int i;
659
660 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
661 printf ("{\n");
662 printf (" switch (insn_code_number)\n");
663 printf (" {\n");
664
665 for (clobber = clobber_list; clobber; clobber = clobber->next)
666 {
667 for (ent = clobber->insns; ent; ent = ent->next)
668 printf (" case %d:\n", ent->code_number);
669
670 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
671 {
672 printf (" XVECEXP (pattern, 0, %d) = ", i);
673 gen_exp (XVECEXP (clobber->pattern, 1, i),
674 GET_CODE (clobber->pattern), NULL);
675 printf (";\n");
676 }
677
678 printf (" break;\n\n");
679 }
680
681 printf (" default:\n");
682 printf (" gcc_unreachable ();\n");
683 printf (" }\n");
684 printf ("}\n");
685 }
686 \f
687 /* Write a function, `added_clobbers_hard_reg_p' that is given an insn_code
688 number that will have clobbers added (as indicated by `recog') and returns
689 1 if those include a clobber of a hard reg or 0 if all of them just clobber
690 SCRATCH. */
691
692 static void
693 output_added_clobbers_hard_reg_p (void)
694 {
695 struct clobber_pat *clobber;
696 struct clobber_ent *ent;
697 int clobber_p, used;
698
699 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
700 printf ("{\n");
701 printf (" switch (insn_code_number)\n");
702 printf (" {\n");
703
704 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
705 {
706 used = 0;
707 for (clobber = clobber_list; clobber; clobber = clobber->next)
708 if (clobber->has_hard_reg == clobber_p)
709 for (ent = clobber->insns; ent; ent = ent->next)
710 {
711 printf (" case %d:\n", ent->code_number);
712 used++;
713 }
714
715 if (used)
716 printf (" return %d;\n\n", clobber_p);
717 }
718
719 printf (" default:\n");
720 printf (" gcc_unreachable ();\n");
721 printf (" }\n");
722 printf ("}\n");
723 }
724 \f
725 /* Generate code to invoke find_free_register () as needed for the
726 scratch registers used by the peephole2 pattern in SPLIT. */
727
728 static void
729 output_peephole2_scratches (rtx split)
730 {
731 int i;
732 int insn_nr = 0;
733 bool first = true;
734
735 for (i = 0; i < XVECLEN (split, 0); i++)
736 {
737 rtx elt = XVECEXP (split, 0, i);
738 if (GET_CODE (elt) == MATCH_SCRATCH)
739 {
740 int last_insn_nr = insn_nr;
741 int cur_insn_nr = insn_nr;
742 int j;
743 for (j = i + 1; j < XVECLEN (split, 0); j++)
744 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
745 {
746 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
747 last_insn_nr = cur_insn_nr;
748 }
749 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
750 cur_insn_nr++;
751
752 if (first)
753 {
754 printf (" HARD_REG_SET _regs_allocated;\n");
755 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
756 first = false;
757 }
758
759 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
760 return NULL;\n",
761 XINT (elt, 0),
762 insn_nr, last_insn_nr,
763 XSTR (elt, 1),
764 GET_MODE_NAME (GET_MODE (elt)));
765
766 }
767 else if (GET_CODE (elt) != MATCH_DUP)
768 insn_nr++;
769 }
770 }
771
772 int
773 main (int argc, char **argv)
774 {
775 rtx desc;
776
777 progname = "genemit";
778
779 if (!init_rtx_reader_args (argc, argv))
780 return (FATAL_EXIT_CODE);
781
782 /* Assign sequential codes to all entries in the machine description
783 in parallel with the tables in insn-output.c. */
784
785 insn_code_number = 0;
786 insn_index_number = 0;
787
788 printf ("/* Generated automatically by the program `genemit'\n\
789 from the machine description file `md'. */\n\n");
790
791 printf ("#include \"config.h\"\n");
792 printf ("#include \"system.h\"\n");
793 printf ("#include \"coretypes.h\"\n");
794 printf ("#include \"tm.h\"\n");
795 printf ("#include \"rtl.h\"\n");
796 printf ("#include \"tm_p.h\"\n");
797 printf ("#include \"function.h\"\n");
798 printf ("#include \"expr.h\"\n");
799 printf ("#include \"optabs.h\"\n");
800 printf ("#include \"dfp.h\"\n");
801 printf ("#include \"flags.h\"\n");
802 printf ("#include \"output.h\"\n");
803 printf ("#include \"insn-config.h\"\n");
804 printf ("#include \"hard-reg-set.h\"\n");
805 printf ("#include \"recog.h\"\n");
806 printf ("#include \"resource.h\"\n");
807 printf ("#include \"reload.h\"\n");
808 printf ("#include \"diagnostic-core.h\"\n");
809 printf ("#include \"regs.h\"\n");
810 printf ("#include \"tm-constrs.h\"\n");
811 printf ("#include \"ggc.h\"\n");
812 printf ("#include \"basic-block.h\"\n");
813 printf ("#include \"target.h\"\n\n");
814 printf ("#define FAIL return (end_sequence (), _val)\n");
815 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
816
817 /* Read the machine description. */
818
819 while (1)
820 {
821 int line_no;
822
823 desc = read_md_rtx (&line_no, &insn_code_number);
824 if (desc == NULL)
825 break;
826
827 switch (GET_CODE (desc))
828 {
829 case DEFINE_INSN:
830 gen_insn (desc, line_no);
831 break;
832
833 case DEFINE_EXPAND:
834 printf ("/* %s:%d */\n", read_md_filename, line_no);
835 gen_expand (desc);
836 break;
837
838 case DEFINE_SPLIT:
839 printf ("/* %s:%d */\n", read_md_filename, line_no);
840 gen_split (desc);
841 break;
842
843 case DEFINE_PEEPHOLE2:
844 printf ("/* %s:%d */\n", read_md_filename, line_no);
845 gen_split (desc);
846 break;
847
848 default:
849 break;
850 }
851 ++insn_index_number;
852 }
853
854 /* Write out the routines to add CLOBBERs to a pattern and say whether they
855 clobber a hard reg. */
856 output_add_clobbers ();
857 output_added_clobbers_hard_reg_p ();
858
859 fflush (stdout);
860 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
861 }