OSDN Git Service

696dbfb28024b4c78e79eb61c571b764fd5547b3
[pf3gnuchains/gcc-fork.git] / 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 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 char *xmalloc PROTO((unsigned));
37 static void fatal PVPROTO ((char *, ...))
38   ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
39 void fancy_abort PROTO((void)) ATTRIBUTE_NORETURN;
40
41 /* Define this so we can link with print-rtl.o to get debug_rtx function.  */
42 char **insn_name_ptr = 0;
43
44 static int insn_code_number;
45
46 static void gen_insn PROTO((rtx));
47
48 static void
49 gen_insn (insn)
50      rtx insn;
51 {
52   /* Don't mention instructions whose names are the null string
53      or begin with '*'.  They are in the machine description just
54      to be recognized.  */
55   if (XSTR (insn, 0)[0] != 0 && XSTR (insn, 0)[0] != '*')
56     printf ("  CODE_FOR_%s = %d,\n", XSTR (insn, 0),
57             insn_code_number);
58 }
59
60 char *
61 xmalloc (size)
62      unsigned size;
63 {
64   register char *val = (char *) malloc (size);
65
66   if (val == 0)
67     fatal ("virtual memory exhausted");
68   return val;
69 }
70
71 char *
72 xrealloc (ptr, size)
73      char *ptr;
74      unsigned size;
75 {
76   char *result = (char *) realloc (ptr, size);
77   if (!result)
78     fatal ("virtual memory exhausted");
79   return result;
80 }
81
82 static void
83 fatal VPROTO ((char *format, ...))
84 {
85 #ifndef ANSI_PROTOTYPES
86   char *format;
87 #endif
88   va_list ap;
89
90   VA_START (ap, format);
91
92 #ifndef ANSI_PROTOTYPES
93   format = va_arg (ap, char *);
94 #endif
95
96   fprintf (stderr, "gencodes: ");
97   vfprintf (stderr, format, ap);
98   va_end (ap);
99   fprintf (stderr, "\n");
100   exit (FATAL_EXIT_CODE);
101 }
102
103 /* More 'friendly' abort that prints the line and file.
104    config.h can #define abort fancy_abort if you like that sort of thing.  */
105
106 void
107 fancy_abort ()
108 {
109   fatal ("Internal gcc abort.");
110 }
111 \f
112 int
113 main (argc, argv)
114      int argc;
115      char **argv;
116 {
117   rtx desc;
118   FILE *infile;
119   register int c;
120
121   obstack_init (rtl_obstack);
122
123   if (argc <= 1)
124     fatal ("No input file name.");
125
126   infile = fopen (argv[1], "r");
127   if (infile == 0)
128     {
129       perror (argv[1]);
130       exit (FATAL_EXIT_CODE);
131     }
132
133   init_rtl ();
134
135   printf ("/* Generated automatically by the program `gencodes'\n\
136 from the machine description file `md'.  */\n\n");
137
138   printf ("#ifndef MAX_INSN_CODE\n\n");
139
140   /* Read the machine description.  */
141
142   insn_code_number = 0;
143   printf ("enum insn_code {\n");
144
145   while (1)
146     {
147       c = read_skip_spaces (infile);
148       if (c == EOF)
149         break;
150       ungetc (c, infile);
151
152       desc = read_rtx (infile);
153       if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
154         {
155           gen_insn (desc);
156           insn_code_number++;
157         }
158       if (GET_CODE (desc) == DEFINE_PEEPHOLE
159           || GET_CODE (desc) == DEFINE_SPLIT)
160         {
161           insn_code_number++;
162         }
163     }
164
165   printf ("  CODE_FOR_nothing };\n");
166
167   printf ("\n#define MAX_INSN_CODE ((int) CODE_FOR_nothing)\n");
168
169   printf ("#endif /* MAX_INSN_CODE */\n");
170
171   fflush (stdout);
172   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
173   /* NOTREACHED */
174   return 0;
175 }