2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / genemit.c
blob0e695d2729c3badf22dda3ed502d2b2c26fa0453
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 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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"
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;
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. */
42 struct clobber_pat
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;
51 /* Records one insn that uses the clobber list. */
53 struct clobber_ent
55 int code_number; /* Counts only insns. */
56 struct clobber_ent *next;
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);
72 static void
73 max_operand_1 (rtx x)
75 RTX_CODE code;
76 int i;
77 int len;
78 const char *fmt;
80 if (x == 0)
81 return;
83 code = GET_CODE (x);
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));
93 fmt = GET_RTX_FORMAT (code);
94 len = GET_RTX_LENGTH (code);
95 for (i = 0; i < len; i++)
97 if (fmt[i] == 'e' || fmt[i] == 'u')
98 max_operand_1 (XEXP (x, i));
99 else if (fmt[i] == 'E')
101 int j;
102 for (j = 0; j < XVECLEN (x, i); j++)
103 max_operand_1 (XVECEXP (x, i, j));
108 static int
109 max_operand_vec (rtx insn, int arg)
111 int len = XVECLEN (insn, arg);
112 int i;
114 max_opno = -1;
115 max_dup_opno = -1;
116 max_scratch_opno = -1;
118 for (i = 0; i < len; i++)
119 max_operand_1 (XVECEXP (insn, arg, i));
121 return max_opno + 1;
124 static void
125 print_code (RTX_CODE code)
127 const char *p1;
128 for (p1 = GET_RTX_NAME (code); *p1; p1++)
129 putchar (TOUPPER(*p1));
132 static void
133 gen_rtx_scratch (rtx x, enum rtx_code subroutine_type)
135 if (subroutine_type == DEFINE_PEEPHOLE2)
137 printf ("operand%d", XINT (x, 0));
139 else
141 printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
145 /* Print a C expression to construct an RTX just like X,
146 substituting any operand references appearing within. */
148 static void
149 gen_exp (rtx x, enum rtx_code subroutine_type, char *used)
151 RTX_CODE code;
152 int i;
153 int len;
154 const char *fmt;
156 if (x == 0)
158 printf ("NULL_RTX");
159 return;
162 code = GET_CODE (x);
164 switch (code)
166 case MATCH_OPERAND:
167 case MATCH_DUP:
168 if (used)
170 if (used[XINT (x, 0)])
172 printf ("copy_rtx (operand%d)", XINT (x, 0));
173 return;
175 used[XINT (x, 0)] = 1;
177 printf ("operand%d", XINT (x, 0));
178 return;
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++)
188 printf (",\n\t\t");
189 gen_exp (XVECEXP (x, 1, i), subroutine_type, used);
191 printf (")");
192 return;
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++)
199 printf (",\n\t\t");
200 gen_exp (XVECEXP (x, 2, i), subroutine_type, used);
202 printf (")");
203 return;
205 case MATCH_PARALLEL:
206 case MATCH_PAR_DUP:
207 printf ("operand%d", XINT (x, 0));
208 return;
210 case MATCH_SCRATCH:
211 gen_rtx_scratch (x, subroutine_type);
212 return;
214 case ADDRESS:
215 fatal ("ADDRESS expression code used in named instruction pattern");
217 case PC:
218 printf ("pc_rtx");
219 return;
221 case CC0:
222 printf ("cc0_rtx");
223 return;
225 case CONST_INT:
226 if (INTVAL (x) == 0)
227 printf ("const0_rtx");
228 else if (INTVAL (x) == 1)
229 printf ("const1_rtx");
230 else if (INTVAL (x) == -1)
231 printf ("constm1_rtx");
232 else if (INTVAL (x) == STORE_FLAG_VALUE)
233 printf ("const_true_rtx");
234 else
236 printf ("GEN_INT (");
237 printf (HOST_WIDE_INT_PRINT_DEC_C, INTVAL (x));
238 printf (")");
240 return;
242 case CONST_DOUBLE:
243 /* These shouldn't be written in MD files. Instead, the appropriate
244 routines in varasm.c should be called. */
245 abort ();
247 default:
248 break;
251 printf ("gen_rtx_");
252 print_code (code);
253 printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
255 fmt = GET_RTX_FORMAT (code);
256 len = GET_RTX_LENGTH (code);
257 for (i = 0; i < len; i++)
259 if (fmt[i] == '0')
260 break;
261 printf (",\n\t");
262 if (fmt[i] == 'e' || fmt[i] == 'u')
263 gen_exp (XEXP (x, i), subroutine_type, used);
264 else if (fmt[i] == 'i')
265 printf ("%u", XINT (x, i));
266 else if (fmt[i] == 's')
267 printf ("\"%s\"", XSTR (x, i));
268 else if (fmt[i] == 'E')
270 int j;
271 printf ("gen_rtvec (%d", XVECLEN (x, i));
272 for (j = 0; j < XVECLEN (x, i); j++)
274 printf (",\n\t\t");
275 gen_exp (XVECEXP (x, i, j), subroutine_type, used);
277 printf (")");
279 else
280 abort ();
282 printf (")");
285 /* Generate the `gen_...' function for a DEFINE_INSN. */
287 static void
288 gen_insn (rtx insn, int lineno)
290 int operands;
291 int i;
293 /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
294 registers or MATCH_SCRATCHes. If so, store away the information for
295 later. */
297 if (XVEC (insn, 1))
299 int has_hard_reg = 0;
301 for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
303 if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER)
304 break;
306 if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) == REG)
307 has_hard_reg = 1;
308 else if (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH)
309 break;
312 if (i != XVECLEN (insn, 1) - 1)
314 struct clobber_pat *p;
315 struct clobber_ent *link = xmalloc (sizeof (struct clobber_ent));
316 int j;
318 link->code_number = insn_code_number;
320 /* See if any previous CLOBBER_LIST entry is the same as this
321 one. */
323 for (p = clobber_list; p; p = p->next)
325 if (p->first_clobber != i + 1
326 || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
327 continue;
329 for (j = i + 1; j < XVECLEN (insn, 1); j++)
331 rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
332 rtx new = XEXP (XVECEXP (insn, 1, j), 0);
334 /* OLD and NEW are the same if both are to be a SCRATCH
335 of the same mode,
336 or if both are registers of the same mode and number. */
337 if (! (GET_MODE (old) == GET_MODE (new)
338 && ((GET_CODE (old) == MATCH_SCRATCH
339 && GET_CODE (new) == MATCH_SCRATCH)
340 || (GET_CODE (old) == REG && GET_CODE (new) == REG
341 && REGNO (old) == REGNO (new)))))
342 break;
345 if (j == XVECLEN (insn, 1))
346 break;
349 if (p == 0)
351 p = xmalloc (sizeof (struct clobber_pat));
353 p->insns = 0;
354 p->pattern = insn;
355 p->first_clobber = i + 1;
356 p->next = clobber_list;
357 p->has_hard_reg = has_hard_reg;
358 clobber_list = p;
361 link->next = p->insns;
362 p->insns = link;
366 /* Don't mention instructions whose names are the null string
367 or begin with '*'. They are in the machine description just
368 to be recognized. */
369 if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
370 return;
372 printf ("/* %s:%d */\n", read_rtx_filename, lineno);
374 /* Find out how many operands this function has. */
375 operands = max_operand_vec (insn, 1);
376 if (max_dup_opno >= operands)
377 fatal ("match_dup operand number has no match_operand");
379 /* Output the function name and argument declarations. */
380 printf ("rtx\ngen_%s (", XSTR (insn, 0));
381 if (operands)
382 for (i = 0; i < operands; i++)
383 if (i)
384 printf (",\n\trtx operand%d ATTRIBUTE_UNUSED", i);
385 else
386 printf ("rtx operand%d ATTRIBUTE_UNUSED", i);
387 else
388 printf ("void");
389 printf (")\n");
390 printf ("{\n");
392 /* Output code to construct and return the rtl for the instruction body. */
394 if (XVECLEN (insn, 1) == 1)
396 printf (" return ");
397 gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN, NULL);
398 printf (";\n}\n\n");
400 else
402 printf (" return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d",
403 XVECLEN (insn, 1));
405 for (i = 0; i < XVECLEN (insn, 1); i++)
407 printf (",\n\t\t");
408 gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN, NULL);
410 printf ("));\n}\n\n");
414 /* Generate the `gen_...' function for a DEFINE_EXPAND. */
416 static void
417 gen_expand (rtx expand)
419 int operands;
420 int i;
422 if (strlen (XSTR (expand, 0)) == 0)
423 fatal ("define_expand lacks a name");
424 if (XVEC (expand, 1) == 0)
425 fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
427 /* Find out how many operands this function has. */
428 operands = max_operand_vec (expand, 1);
430 /* Output the function name and argument declarations. */
431 printf ("rtx\ngen_%s (", XSTR (expand, 0));
432 if (operands)
433 for (i = 0; i < operands; i++)
434 if (i)
435 printf (",\n\trtx operand%d", i);
436 else
437 printf ("rtx operand%d", i);
438 else
439 printf ("void");
440 printf (")\n");
441 printf ("{\n");
443 /* If we don't have any C code to write, only one insn is being written,
444 and no MATCH_DUPs are present, we can just return the desired insn
445 like we do for a DEFINE_INSN. This saves memory. */
446 if ((XSTR (expand, 3) == 0 || *XSTR (expand, 3) == '\0')
447 && operands > max_dup_opno
448 && XVECLEN (expand, 1) == 1)
450 printf (" return ");
451 gen_exp (XVECEXP (expand, 1, 0), DEFINE_EXPAND, NULL);
452 printf (";\n}\n\n");
453 return;
456 /* For each operand referred to only with MATCH_DUPs,
457 make a local variable. */
458 for (i = operands; i <= max_dup_opno; i++)
459 printf (" rtx operand%d;\n", i);
460 for (; i <= max_scratch_opno; i++)
461 printf (" rtx operand%d ATTRIBUTE_UNUSED;\n", i);
462 printf (" rtx _val = 0;\n");
463 printf (" start_sequence ();\n");
465 /* The fourth operand of DEFINE_EXPAND is some code to be executed
466 before the actual construction.
467 This code expects to refer to `operands'
468 just as the output-code in a DEFINE_INSN does,
469 but here `operands' is an automatic array.
470 So copy the operand values there before executing it. */
471 if (XSTR (expand, 3) && *XSTR (expand, 3))
473 printf (" {\n");
474 if (operands > 0 || max_dup_opno >= 0 || max_scratch_opno >= 0)
475 printf (" rtx operands[%d];\n",
476 MAX (operands, MAX (max_scratch_opno, max_dup_opno) + 1));
477 /* Output code to copy the arguments into `operands'. */
478 for (i = 0; i < operands; i++)
479 printf (" operands[%d] = operand%d;\n", i, i);
481 /* Output the special code to be executed before the sequence
482 is generated. */
483 printf ("%s\n", XSTR (expand, 3));
485 /* Output code to copy the arguments back out of `operands'
486 (unless we aren't going to use them at all). */
487 if (XVEC (expand, 1) != 0)
489 for (i = 0; i < operands; i++)
490 printf (" operand%d = operands[%d];\n", i, i);
491 for (; i <= max_dup_opno; i++)
492 printf (" operand%d = operands[%d];\n", i, i);
493 for (; i <= max_scratch_opno; i++)
494 printf (" operand%d = operands[%d];\n", i, i);
496 printf (" }\n");
499 /* Output code to construct the rtl for the instruction bodies.
500 Use emit_insn to add them to the sequence being accumulated.
501 But don't do this if the user's code has set `no_more' nonzero. */
503 for (i = 0; i < XVECLEN (expand, 1); i++)
505 rtx next = XVECEXP (expand, 1, i);
506 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
507 || (GET_CODE (next) == PARALLEL
508 && ((GET_CODE (XVECEXP (next, 0, 0)) == SET
509 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
510 || GET_CODE (XVECEXP (next, 0, 0)) == RETURN))
511 || GET_CODE (next) == RETURN)
512 printf (" emit_jump_insn (");
513 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
514 || GET_CODE (next) == CALL
515 || (GET_CODE (next) == PARALLEL
516 && GET_CODE (XVECEXP (next, 0, 0)) == SET
517 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
518 || (GET_CODE (next) == PARALLEL
519 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
520 printf (" emit_call_insn (");
521 else if (GET_CODE (next) == CODE_LABEL)
522 printf (" emit_label (");
523 else if (GET_CODE (next) == MATCH_OPERAND
524 || GET_CODE (next) == MATCH_DUP
525 || GET_CODE (next) == MATCH_OPERATOR
526 || GET_CODE (next) == MATCH_OP_DUP
527 || GET_CODE (next) == MATCH_PARALLEL
528 || GET_CODE (next) == MATCH_PAR_DUP
529 || GET_CODE (next) == PARALLEL)
530 printf (" emit (");
531 else
532 printf (" emit_insn (");
533 gen_exp (next, DEFINE_EXPAND, NULL);
534 printf (");\n");
535 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
536 && GET_CODE (SET_SRC (next)) == LABEL_REF)
537 printf (" emit_barrier ();");
540 /* Call `get_insns' to extract the list of all the
541 insns emitted within this gen_... function. */
543 printf (" _val = get_insns ();\n");
544 printf (" end_sequence ();\n");
545 printf (" return _val;\n}\n\n");
548 /* Like gen_expand, but generates insns resulting from splitting SPLIT. */
550 static void
551 gen_split (rtx split)
553 int i;
554 int operands;
555 const char *const name =
556 ((GET_CODE (split) == DEFINE_PEEPHOLE2) ? "peephole2" : "split");
557 const char *unused;
558 char *used;
560 if (XVEC (split, 0) == 0)
561 fatal ("define_%s (definition %d) lacks a pattern", name,
562 insn_index_number);
563 else if (XVEC (split, 2) == 0)
564 fatal ("define_%s (definition %d) lacks a replacement pattern", name,
565 insn_index_number);
567 /* Find out how many operands this function has. */
569 max_operand_vec (split, 2);
570 operands = MAX (max_opno, MAX (max_dup_opno, max_scratch_opno)) + 1;
571 unused = (operands == 0 ? " ATTRIBUTE_UNUSED" : "");
572 used = xcalloc (1, operands);
574 /* Output the prototype, function name and argument declarations. */
575 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
577 printf ("extern rtx gen_%s_%d (rtx, rtx *);\n",
578 name, insn_code_number);
579 printf ("rtx\ngen_%s_%d (rtx curr_insn ATTRIBUTE_UNUSED, rtx *operands%s)\n",
580 name, insn_code_number, unused);
582 else
584 printf ("extern rtx gen_split_%d (rtx *);\n", insn_code_number);
585 printf ("rtx\ngen_%s_%d (rtx *operands%s)\n", name, insn_code_number, unused);
587 printf ("{\n");
589 /* Declare all local variables. */
590 for (i = 0; i < operands; i++)
591 printf (" rtx operand%d;\n", i);
592 printf (" rtx _val = 0;\n");
594 if (GET_CODE (split) == DEFINE_PEEPHOLE2)
595 output_peephole2_scratches (split);
597 printf (" start_sequence ();\n");
599 /* The fourth operand of DEFINE_SPLIT is some code to be executed
600 before the actual construction. */
602 if (XSTR (split, 3))
603 printf ("%s\n", XSTR (split, 3));
605 /* Output code to copy the arguments back out of `operands' */
606 for (i = 0; i < operands; i++)
607 printf (" operand%d = operands[%d];\n", i, i);
609 /* Output code to construct the rtl for the instruction bodies.
610 Use emit_insn to add them to the sequence being accumulated.
611 But don't do this if the user's code has set `no_more' nonzero. */
613 for (i = 0; i < XVECLEN (split, 2); i++)
615 rtx next = XVECEXP (split, 2, i);
616 if ((GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC)
617 || (GET_CODE (next) == PARALLEL
618 && GET_CODE (XVECEXP (next, 0, 0)) == SET
619 && GET_CODE (SET_DEST (XVECEXP (next, 0, 0))) == PC)
620 || GET_CODE (next) == RETURN)
621 printf (" emit_jump_insn (");
622 else if ((GET_CODE (next) == SET && GET_CODE (SET_SRC (next)) == CALL)
623 || GET_CODE (next) == CALL
624 || (GET_CODE (next) == PARALLEL
625 && GET_CODE (XVECEXP (next, 0, 0)) == SET
626 && GET_CODE (SET_SRC (XVECEXP (next, 0, 0))) == CALL)
627 || (GET_CODE (next) == PARALLEL
628 && GET_CODE (XVECEXP (next, 0, 0)) == CALL))
629 printf (" emit_call_insn (");
630 else if (GET_CODE (next) == CODE_LABEL)
631 printf (" emit_label (");
632 else if (GET_CODE (next) == MATCH_OPERAND
633 || GET_CODE (next) == MATCH_OPERATOR
634 || GET_CODE (next) == MATCH_PARALLEL
635 || GET_CODE (next) == MATCH_OP_DUP
636 || GET_CODE (next) == MATCH_DUP
637 || GET_CODE (next) == PARALLEL)
638 printf (" emit (");
639 else
640 printf (" emit_insn (");
641 gen_exp (next, GET_CODE (split), used);
642 printf (");\n");
643 if (GET_CODE (next) == SET && GET_CODE (SET_DEST (next)) == PC
644 && GET_CODE (SET_SRC (next)) == LABEL_REF)
645 printf (" emit_barrier ();");
648 /* Call `get_insns' to make a list of all the
649 insns emitted within this gen_... function. */
651 printf (" _val = get_insns ();\n");
652 printf (" end_sequence ();\n");
653 printf (" return _val;\n}\n\n");
655 free (used);
658 /* Write a function, `add_clobbers', that is given a PARALLEL of sufficient
659 size for the insn and an INSN_CODE, and inserts the required CLOBBERs at
660 the end of the vector. */
662 static void
663 output_add_clobbers (void)
665 struct clobber_pat *clobber;
666 struct clobber_ent *ent;
667 int i;
669 printf ("\n\nvoid\nadd_clobbers (rtx pattern ATTRIBUTE_UNUSED, int insn_code_number)\n");
670 printf ("{\n");
671 printf (" switch (insn_code_number)\n");
672 printf (" {\n");
674 for (clobber = clobber_list; clobber; clobber = clobber->next)
676 for (ent = clobber->insns; ent; ent = ent->next)
677 printf (" case %d:\n", ent->code_number);
679 for (i = clobber->first_clobber; i < XVECLEN (clobber->pattern, 1); i++)
681 printf (" XVECEXP (pattern, 0, %d) = ", i);
682 gen_exp (XVECEXP (clobber->pattern, 1, i),
683 GET_CODE (clobber->pattern), NULL);
684 printf (";\n");
687 printf (" break;\n\n");
690 printf (" default:\n");
691 printf (" abort ();\n");
692 printf (" }\n");
693 printf ("}\n");
696 /* Write a function, `added_clobbers_hard_reg_p' this is given an insn_code
697 number that needs clobbers and returns 1 if they include a clobber of a
698 hard reg and 0 if they just clobber SCRATCH. */
700 static void
701 output_added_clobbers_hard_reg_p (void)
703 struct clobber_pat *clobber;
704 struct clobber_ent *ent;
705 int clobber_p, used;
707 printf ("\n\nint\nadded_clobbers_hard_reg_p (int insn_code_number)\n");
708 printf ("{\n");
709 printf (" switch (insn_code_number)\n");
710 printf (" {\n");
712 for (clobber_p = 0; clobber_p <= 1; clobber_p++)
714 used = 0;
715 for (clobber = clobber_list; clobber; clobber = clobber->next)
716 if (clobber->has_hard_reg == clobber_p)
717 for (ent = clobber->insns; ent; ent = ent->next)
719 printf (" case %d:\n", ent->code_number);
720 used++;
723 if (used)
724 printf (" return %d;\n\n", clobber_p);
727 printf (" default:\n");
728 printf (" abort ();\n");
729 printf (" }\n");
730 printf ("}\n");
733 /* Generate code to invoke find_free_register () as needed for the
734 scratch registers used by the peephole2 pattern in SPLIT. */
736 static void
737 output_peephole2_scratches (rtx split)
739 int i;
740 int insn_nr = 0;
742 printf (" HARD_REG_SET _regs_allocated;\n");
743 printf (" CLEAR_HARD_REG_SET (_regs_allocated);\n");
745 for (i = 0; i < XVECLEN (split, 0); i++)
747 rtx elt = XVECEXP (split, 0, i);
748 if (GET_CODE (elt) == MATCH_SCRATCH)
750 int last_insn_nr = insn_nr;
751 int cur_insn_nr = insn_nr;
752 int j;
753 for (j = i + 1; j < XVECLEN (split, 0); j++)
754 if (GET_CODE (XVECEXP (split, 0, j)) == MATCH_DUP)
756 if (XINT (XVECEXP (split, 0, j), 0) == XINT (elt, 0))
757 last_insn_nr = cur_insn_nr;
759 else if (GET_CODE (XVECEXP (split, 0, j)) != MATCH_SCRATCH)
760 cur_insn_nr++;
762 printf (" if ((operands[%d] = peep2_find_free_register (%d, %d, \"%s\", %smode, &_regs_allocated)) == NULL_RTX)\n\
763 return NULL;\n",
764 XINT (elt, 0),
765 insn_nr, last_insn_nr,
766 XSTR (elt, 1),
767 GET_MODE_NAME (GET_MODE (elt)));
770 else if (GET_CODE (elt) != MATCH_DUP)
771 insn_nr++;
776 main (int argc, char **argv)
778 rtx desc;
780 progname = "genemit";
782 if (argc <= 1)
783 fatal ("no input file name");
785 if (init_md_reader_args (argc, argv) != SUCCESS_EXIT_CODE)
786 return (FATAL_EXIT_CODE);
788 /* Assign sequential codes to all entries in the machine description
789 in parallel with the tables in insn-output.c. */
791 insn_code_number = 0;
792 insn_index_number = 0;
794 printf ("/* Generated automatically by the program `genemit'\n\
795 from the machine description file `md'. */\n\n");
797 printf ("#include \"config.h\"\n");
798 printf ("#include \"system.h\"\n");
799 printf ("#include \"coretypes.h\"\n");
800 printf ("#include \"tm.h\"\n");
801 printf ("#include \"rtl.h\"\n");
802 printf ("#include \"tm_p.h\"\n");
803 printf ("#include \"function.h\"\n");
804 printf ("#include \"expr.h\"\n");
805 printf ("#include \"optabs.h\"\n");
806 printf ("#include \"real.h\"\n");
807 printf ("#include \"flags.h\"\n");
808 printf ("#include \"output.h\"\n");
809 printf ("#include \"insn-config.h\"\n");
810 printf ("#include \"hard-reg-set.h\"\n");
811 printf ("#include \"recog.h\"\n");
812 printf ("#include \"resource.h\"\n");
813 printf ("#include \"reload.h\"\n");
814 printf ("#include \"toplev.h\"\n");
815 printf ("#include \"ggc.h\"\n\n");
816 printf ("#define FAIL return (end_sequence (), _val)\n");
817 printf ("#define DONE return (_val = get_insns (), end_sequence (), _val)\n\n");
819 /* Read the machine description. */
821 while (1)
823 int line_no;
825 desc = read_md_rtx (&line_no, &insn_code_number);
826 if (desc == NULL)
827 break;
829 switch (GET_CODE (desc))
831 case DEFINE_INSN:
832 gen_insn (desc, line_no);
833 break;
835 case DEFINE_EXPAND:
836 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
837 gen_expand (desc);
838 break;
840 case DEFINE_SPLIT:
841 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
842 gen_split (desc);
843 break;
845 case DEFINE_PEEPHOLE2:
846 printf ("/* %s:%d */\n", read_rtx_filename, line_no);
847 gen_split (desc);
848 break;
850 default:
851 break;
853 ++insn_index_number;
856 /* Write out the routines to add CLOBBERs to a pattern and say whether they
857 clobber a hard reg. */
858 output_add_clobbers ();
859 output_added_clobbers_hard_reg_p ();
861 fflush (stdout);
862 return (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
865 /* Define this so we can link with print-rtl.o to get debug_rtx function. */
866 const char *
867 get_insn_name (int code ATTRIBUTE_UNUSED)
869 return NULL;