OSDN Git Service

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