Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / gcc / gencodes.c
1 /* Generate from machine description:
2
3    - some macros CODE_FOR_... giving the insn_code_number value
4    for each of the defined standard insn names.
5    Copyright (C) 1987, 1991, 1995, 1998, 1999 Free Software Foundation, Inc.
6
7 This file is part of GNU CC.
8
9 GNU CC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
13
14 GNU CC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GNU CC; see the file COPYING.  If not, write to
21 the Free Software Foundation, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.  */
23
24
25 #include "hconfig.h"
26 #include "system.h"
27 #include "rtl.h"
28 #include "obstack.h"
29
30 static struct obstack obstack;
31 struct obstack *rtl_obstack = &obstack;
32
33 #define obstack_chunk_alloc xmalloc
34 #define obstack_chunk_free free
35
36 void fatal PVPROTO ((const char *, ...))
37   ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
38 void fancy_abort PROTO((void)) ATTRIBUTE_NORETURN;
39
40 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
41 char **insn_name_ptr = 0;
42
43 static int insn_code_number;
44
45 static void gen_insn PROTO((rtx));
46
47 static void
48 gen_insn (insn)
49      rtx insn;
50 {
51   /* Don't mention instructions whose names are the null string
52      or begin with '*'.  They are in the machine description just
53      to be recognized.  */
54   if (XSTR (insn, 0)[0] != 0 && XSTR (insn, 0)[0] != '*')
55     printf ("  CODE_FOR_%s = %d,\n", XSTR (insn, 0),
56             insn_code_number);
57 }
58
59 PTR
60 xmalloc (size)
61   size_t size;
62 {
63   register PTR val = (PTR) malloc (size);
64
65   if (val == 0)
66     fatal ("virtual memory exhausted");
67   return val;
68 }
69
70 PTR
71 xrealloc (old, size)
72   PTR old;
73   size_t size;
74 {
75   register PTR ptr;
76   if (old)
77     ptr = (PTR) realloc (old, size);
78   else
79     ptr = (PTR) malloc (size);
80   if (!ptr)
81     fatal ("virtual memory exhausted");
82   return ptr;
83 }
84
85 void
86 fatal VPROTO ((const char *format, ...))
87 {
88 #ifndef ANSI_PROTOTYPES
89   const char *format;
90 #endif
91   va_list ap;
92
93   VA_START (ap, format);
94
95 #ifndef ANSI_PROTOTYPES
96   format = va_arg (ap, const char *);
97 #endif
98
99   fprintf (stderr, "gencodes: ");
100   vfprintf (stderr, format, ap);
101   va_end (ap);
102   fprintf (stderr, "\n");
103   exit (FATAL_EXIT_CODE);
104 }
105
106 /* More 'friendly' abort that prints the line and file.
107    config.h can #define abort fancy_abort if you like that sort of thing.  */
108
109 void
110 fancy_abort ()
111 {
112   fatal ("Internal gcc abort.");
113 }
114 \f
115 int
116 main (argc, argv)
117      int argc;
118      char **argv;
119 {
120   rtx desc;
121   FILE *infile;
122   register int c;
123
124   obstack_init (rtl_obstack);
125
126   if (argc <= 1)
127     fatal ("No input file name.");
128
129   infile = fopen (argv[1], "r");
130   if (infile == 0)
131     {
132       perror (argv[1]);
133       exit (FATAL_EXIT_CODE);
134     }
135
136   init_rtl ();
137
138   printf ("/* Generated automatically by the program `gencodes'\n\
139 from the machine description file `md'.  */\n\n");
140
141   printf ("#ifndef MAX_INSN_CODE\n\n");
142
143   /* Read the machine description.  */
144
145   insn_code_number = 0;
146   printf ("enum insn_code {\n");
147
148   while (1)
149     {
150       c = read_skip_spaces (infile);
151       if (c == EOF)
152         break;
153       ungetc (c, infile);
154
155       desc = read_rtx (infile);
156       if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
157         {
158           gen_insn (desc);
159           insn_code_number++;
160         }
161       if (GET_CODE (desc) == DEFINE_PEEPHOLE
162           || GET_CODE (desc) == DEFINE_SPLIT)
163         {
164           insn_code_number++;
165         }
166     }
167
168   printf ("  CODE_FOR_nothing };\n");
169
170   printf ("\n#define MAX_INSN_CODE ((int) CODE_FOR_nothing)\n");
171
172   printf ("#endif /* MAX_INSN_CODE */\n");
173
174   fflush (stdout);
175   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
176   /* NOTREACHED */
177   return 0;
178 }