OSDN Git Service

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