OSDN Git Service

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