OSDN Git Service

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