dwarf2out.c (dwarf2out_line): Constify `lastfile'.
[platform/upstream/gcc.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 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
35 char **insn_name_ptr = 0;
36
37 static int max_opno;
38 static int max_dup_opno;
39 static int max_scratch_opno;
40 static int register_constraints;
41 static int insn_code_number;
42 static int insn_index_number;
43
44 /* Data structure for recording the patterns of insns that have CLOBBERs.
45    We use this to output a function that adds these CLOBBERs to a 
46    previously-allocated PARALLEL expression.  */
47
48 struct clobber_pat
49 {
50   struct clobber_ent *insns;
51   rtx pattern;
52   int first_clobber;
53   struct clobber_pat *next;
54 } *clobber_list;
55
56 /* Records one insn that uses the clobber list.  */
57
58 struct clobber_ent
59 {
60   int code_number;              /* Counts only insns.  */
61   struct clobber_ent *next;
62 };
63
64 static void max_operand_1               PROTO((rtx));
65 static int max_operand_vec              PROTO((rtx, int));
66 static void print_code                  PROTO((RTX_CODE));
67 static void gen_exp                     PROTO((rtx, enum rtx_code));
68 static void gen_insn                    PROTO((rtx));
69 static void gen_expand                  PROTO((rtx));
70 static void gen_split                   PROTO((rtx));
71 static void output_add_clobbers         PROTO((void));
72 static void gen_rtx_scratch             PROTO((rtx, enum rtx_code));
73 static void output_peephole2_scratches  PROTO((rtx));
74
75 \f
76 static void
77 max_operand_1 (x)
78      rtx x;
79 {
80   register RTX_CODE code;
81   register int i;
82   register int len;
83   register const char *fmt;
84
85   if (x == 0)
86     return;
87
88   code = GET_CODE (x);
89
90   if (code == MATCH_OPERAND && XSTR (x, 2) != 0 && *XSTR (x, 2) != '\0')
91     register_constraints = 1;
92   if (code == MATCH_SCRATCH && XSTR (x, 1) != 0 && *XSTR (x, 1) != '\0')
93     register_constraints = 1;
94   if (code == MATCH_OPERAND || code == MATCH_OPERATOR
95       || code == MATCH_PARALLEL)
96     max_opno = MAX (max_opno, XINT (x, 0));
97   if (code == MATCH_DUP || code == MATCH_OP_DUP || code == MATCH_PAR_DUP)
98     max_dup_opno = MAX (max_dup_opno, XINT (x, 0));
99   if (code == MATCH_SCRATCH)
100     max_scratch_opno = MAX (max_scratch_opno, XINT (x, 0));
101
102   fmt = GET_RTX_FORMAT (code);
103   len = GET_RTX_LENGTH (code);
104   for (i = 0; i < len; i++)
105     {
106       if (fmt[i] == 'e' || fmt[i] == 'u')
107         max_operand_1 (XEXP (x, i));
108       else if (fmt[i] == 'E')
109         {
110           int j;
111           for (j = 0; j < XVECLEN (x, i); j++)
112             max_operand_1 (XVECEXP (x, i, j));
113         }
114     }
115 }
116
117 static int
118 max_operand_vec (insn, arg)
119      rtx insn;
120      int arg;
121 {
122   register int len = XVECLEN (insn, arg);
123   register int i;
124
125   max_opno = -1;
126   max_dup_opno = -1;
127   max_scratch_opno = -1;
128
129   for (i = 0; i < len; i++)
130     max_operand_1 (XVECEXP (insn, arg, i));
131
132   return max_opno + 1;
133 }
134 \f
135 static void
136 print_code (code)
137      RTX_CODE code;
138 {
139   register const char *p1;
140   for (p1 = GET_RTX_NAME (code); *p1; p1++)
141     {
142       if (*p1 >= 'a' && *p1 <= 'z')
143         putchar (*p1 + 'A' - 'a');
144       else
145         putchar (*p1);
146     }
147 }
148
149 static void
150 gen_rtx_scratch (x, subroutine_type)
151      rtx x;
152      enum rtx_code subroutine_type;
153 {
154   if (subroutine_type == DEFINE_PEEPHOLE2)
155     {
156       printf ("operand%d", XINT (x, 0));
157     }
158   else
159     {
160       printf ("gen_rtx_SCRATCH (%smode)", GET_MODE_NAME (GET_MODE (x)));
161     }
162 }
163
164 /* Print a C expression to construct an RTX just like X,
165    substituting any operand references appearing within.  */
166
167 static void
168 gen_exp (x, subroutine_type)
169      rtx x;
170      enum rtx_code subroutine_type;
171 {
172   register RTX_CODE code;
173   register int i;
174   register int len;
175   register const char *fmt;
176
177   if (x == 0)
178     {
179       printf ("NULL_RTX");
180       return;
181     }
182
183   code = GET_CODE (x);
184
185   switch (code)
186     {
187     case MATCH_OPERAND:
188     case MATCH_DUP:
189       printf ("operand%d", XINT (x, 0));
190       return;
191
192     case MATCH_OP_DUP:
193       printf ("gen_rtx (GET_CODE (operand%d), ", XINT (x, 0));
194       if (GET_MODE (x) == VOIDmode)
195         printf ("GET_MODE (operand%d)", XINT (x, 0));
196       else
197         printf ("%smode", GET_MODE_NAME (GET_MODE (x)));
198       for (i = 0; i < XVECLEN (x, 1); i++)
199         {
200           printf (",\n\t\t");
201           gen_exp (XVECEXP (x, 1, i), subroutine_type);
202         }
203       printf (")");
204       return;
205
206     case MATCH_OPERATOR:
207       printf ("gen_rtx (GET_CODE (operand%d)", XINT (x, 0));
208       printf (", %smode", GET_MODE_NAME (GET_MODE (x)));
209       for (i = 0; i < XVECLEN (x, 2); i++)
210         {
211           printf (",\n\t\t");
212           gen_exp (XVECEXP (x, 2, i), subroutine_type);
213         }
214       printf (")");
215       return;
216
217     case MATCH_PARALLEL:
218     case MATCH_PAR_DUP:
219       printf ("operand%d", XINT (x, 0));
220       return;
221
222     case MATCH_SCRATCH:
223       gen_rtx_scratch (x, subroutine_type);
224       return;
225
226     case ADDRESS:
227       fatal ("ADDRESS expression code used in named instruction pattern");
228
229     case PC:
230       printf ("pc_rtx");
231       return;
232
233     case CC0:
234       printf ("cc0_rtx");
235       return;
236
237     case CONST_INT:
238       if (INTVAL (x) == 0)
239         printf ("const0_rtx");
240       else if (INTVAL (x) == 1)
241         printf ("const1_rtx");
242       else if (INTVAL (x) == -1)
243         printf ("constm1_rtx");
244       else if (INTVAL (x) == STORE_FLAG_VALUE)
245         printf ("const_true_rtx");
246       else
247         {
248           printf ("GEN_INT (");
249           printf (HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
250           printf (")");
251         }
252       return;
253
254     case CONST_DOUBLE:
255       /* These shouldn't be written in MD files.  Instead, the appropriate
256          routines in varasm.c should be called.  */
257       abort ();
258
259     default:
260       break;
261     }
262
263   printf ("gen_rtx_");
264   print_code (code);
265   printf (" (%smode", GET_MODE_NAME (GET_MODE (x)));
266
267   fmt = GET_RTX_FORMAT (code);
268   len = GET_RTX_LENGTH (code);
269   for (i = 0; i < len; i++)
270     {
271       if (fmt[i] == '0')
272         break;
273       printf (",\n\t");
274       if (fmt[i] == 'e' || fmt[i] == 'u')
275         gen_exp (XEXP (x, i), subroutine_type);
276       else if (fmt[i] == 'i')
277         printf ("%u", XINT (x, i));
278       else if (fmt[i] == 's')
279         printf ("\"%s\"", XSTR (x, i));
280       else if (fmt[i] == 'E')
281         {
282           int j;
283           printf ("gen_rtvec (%d", XVECLEN (x, i));
284           for (j = 0; j < XVECLEN (x, i); j++)
285             {
286               printf (",\n\t\t");
287               gen_exp (XVECEXP (x, i, j), subroutine_type);
288             }
289           printf (")");
290         }
291       else
292         abort ();
293     }
294   printf (")");
295 }  
296 \f
297 /* Generate the `gen_...' function for a DEFINE_INSN.  */
298
299 static void
300 gen_insn (insn)
301      rtx insn;
302 {
303   int operands;
304   register int i;
305
306   /* See if the pattern for this insn ends with a group of CLOBBERs of (hard)
307      registers or MATCH_SCRATCHes.  If so, store away the information for
308      later.  */
309
310   if (XVEC (insn, 1))
311     {
312       for (i = XVECLEN (insn, 1) - 1; i > 0; i--)
313         if (GET_CODE (XVECEXP (insn, 1, i)) != CLOBBER
314             || (GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != REG
315                 && GET_CODE (XEXP (XVECEXP (insn, 1, i), 0)) != MATCH_SCRATCH))
316           break;
317
318       if (i != XVECLEN (insn, 1) - 1)
319         {
320           register struct clobber_pat *p;
321           register struct clobber_ent *link
322             = (struct clobber_ent *) xmalloc (sizeof (struct clobber_ent));
323           register int j;
324
325           link->code_number = insn_code_number;
326
327           /* See if any previous CLOBBER_LIST entry is the same as this
328              one.  */
329
330           for (p = clobber_list; p; p = p->next)
331             {
332               if (p->first_clobber != i + 1
333                   || XVECLEN (p->pattern, 1) != XVECLEN (insn, 1))
334                 continue;
335
336               for (j = i + 1; j < XVECLEN (insn, 1); j++)
337                 {
338                   rtx old = XEXP (XVECEXP (p->pattern, 1, j), 0);
339                   rtx new = XEXP (XVECEXP (insn, 1, j), 0);
340
341                   /* OLD and NEW are the same if both are to be a SCRATCH
342                      of the same mode, 
343                      or if both are registers of the same mode and number.  */
344                   if (! (GET_MODE (old) == GET_MODE (new)
345                          && ((GET_CODE (old) == MATCH_SCRATCH
346                               && GET_CODE (new) == MATCH_SCRATCH)
347                              || (GET_CODE (old) == REG && GET_CODE (new) == REG
348                                  && REGNO (old) == REGNO (new)))))
349                     break;
350                 }
351       
352               if (j == XVECLEN (insn, 1))
353                 break;
354             }
355
356           if (p == 0)
357             {
358               p = (struct clobber_pat *) xmalloc (sizeof (struct clobber_pat));
359           
360               p->insns = 0;
361               p->pattern = insn;
362               p->first_clobber = i + 1;
363               p->next = clobber_list;
364               clobber_list = p;
365             }
366
367           link->next = p->insns;
368           p->insns = link;
369         }
370     }
371
372   /* Don't mention instructions whose names are the null string
373      or begin with '*'.  They are in the machine description just
374      to be recognized.  */
375   if (XSTR (insn, 0)[0] == 0 || XSTR (insn, 0)[0] == '*')
376     return;
377
378   /* Find out how many operands this function has,
379      and also whether any of them have register constraints.  */
380   register_constraints = 0;
381   operands = max_operand_vec (insn, 1);
382   if (max_dup_opno >= operands)
383     fatal ("match_dup operand number has no match_operand");
384
385   /* Output the function name and argument declarations.  */
386   printf ("rtx\ngen_%s (", XSTR (insn, 0));
387   for (i = 0; i < operands; i++)
388     printf (i ? ", operand%d" : "operand%d", i);
389   printf (")\n");
390   for (i = 0; i < operands; i++)
391     printf ("     rtx operand%d;\n", i);
392   printf ("{\n");
393
394   /* Output code to construct and return the rtl for the instruction body */
395
396   if (XVECLEN (insn, 1) == 1)
397     {
398       printf ("  return ");
399       gen_exp (XVECEXP (insn, 1, 0), DEFINE_INSN);
400       printf (";\n}\n\n");
401     }
402   else
403     {
404       printf ("  return gen_rtx_PARALLEL (VOIDmode, gen_rtvec (%d", XVECLEN (insn, 1));
405       for (i = 0; i < XVECLEN (insn, 1); i++)
406         {
407           printf (",\n\t\t");
408           gen_exp (XVECEXP (insn, 1, i), DEFINE_INSN);
409         }
410       printf ("));\n}\n\n");
411     }
412 }
413 \f
414 /* Generate the `gen_...' function for a DEFINE_EXPAND.  */
415
416 static void
417 gen_expand (expand)
418      rtx expand;
419 {
420   int operands;
421   register int i;
422
423   if (strlen (XSTR (expand, 0)) == 0)
424     fatal ("define_expand lacks a name");
425   if (XVEC (expand, 1) == 0)
426     fatal ("define_expand for %s lacks a pattern", XSTR (expand, 0));
427
428   /* Find out how many operands this function has,
429      and also whether any of them have register constraints.  */
430   register_constraints = 0;
431
432   operands = max_operand_vec (expand, 1);
433
434   /* Output the function name and argument declarations.  */
435   printf ("rtx\ngen_%s (", XSTR (expand, 0));
436   for (i = 0; i < operands; i++)
437     printf (i ? ", operand%d" : "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   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 int
774 main (argc, argv)
775      int argc;
776      char **argv;
777 {
778   rtx desc;
779   FILE *infile;
780   register int c;
781
782   progname = "genemit";
783   obstack_init (rtl_obstack);
784
785   if (argc <= 1)
786     fatal ("No input file name.");
787
788   infile = fopen (argv[1], "r");
789   if (infile == 0)
790     {
791       perror (argv[1]);
792       exit (FATAL_EXIT_CODE);
793     }
794
795   /* Assign sequential codes to all entries in the machine description
796      in parallel with the tables in insn-output.c.  */
797
798   insn_code_number = 0;
799   insn_index_number = 0;
800
801   printf ("/* Generated automatically by the program `genemit'\n\
802 from the machine description file `md'.  */\n\n");
803
804   printf ("#include \"config.h\"\n");
805   printf ("#include \"system.h\"\n");
806   printf ("#include \"rtl.h\"\n");
807   printf ("#include \"function.h\"\n");
808   printf ("#include \"expr.h\"\n");
809   printf ("#include \"real.h\"\n");
810   printf ("#include \"flags.h\"\n");
811   printf ("#include \"output.h\"\n");
812   printf ("#include \"insn-config.h\"\n");
813   printf ("#include \"insn-flags.h\"\n");
814   printf ("#include \"insn-codes.h\"\n");
815   printf ("#include \"recog.h\"\n");
816   printf ("#include \"hard-reg-set.h\"\n");
817   printf ("#include \"resource.h\"\n");
818   printf ("#include \"reload.h\"\n\n");
819   printf ("extern rtx recog_operand[];\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   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
866   /* NOTREACHED */
867   return 0;
868 }