OSDN Git Service

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