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