OSDN Git Service

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