OSDN Git Service

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