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