OSDN Git Service

Merge in gcc2-ss-010999
[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 /* 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 insn_code_number;
40
41 static void gen_insn PROTO((rtx));
42
43 static void
44 gen_insn (insn)
45      rtx insn;
46 {
47   /* Don't mention instructions whose names are the null string
48      or begin with '*'.  They are in the machine description just
49      to be recognized.  */
50   if (XSTR (insn, 0)[0] != 0 && XSTR (insn, 0)[0] != '*')
51     printf ("  CODE_FOR_%s = %d,\n", XSTR (insn, 0),
52             insn_code_number);
53 }
54
55 PTR
56 xmalloc (size)
57   size_t size;
58 {
59   register PTR val = (PTR) malloc (size);
60
61   if (val == 0)
62     fatal ("virtual memory exhausted");
63   return val;
64 }
65
66 PTR
67 xrealloc (old, size)
68   PTR old;
69   size_t size;
70 {
71   register PTR ptr;
72   if (old)
73     ptr = (PTR) realloc (old, size);
74   else
75     ptr = (PTR) malloc (size);
76   if (!ptr)
77     fatal ("virtual memory exhausted");
78   return ptr;
79 }
80
81 int
82 main (argc, argv)
83      int argc;
84      char **argv;
85 {
86   rtx desc;
87   FILE *infile;
88   register int c;
89
90   progname = "gencodes";
91   obstack_init (rtl_obstack);
92
93   if (argc <= 1)
94     fatal ("No input file name.");
95
96   infile = fopen (argv[1], "r");
97   if (infile == 0)
98     {
99       perror (argv[1]);
100       exit (FATAL_EXIT_CODE);
101     }
102
103   printf ("/* Generated automatically by the program `gencodes'\n\
104 from the machine description file `md'.  */\n\n");
105
106   printf ("#ifndef MAX_INSN_CODE\n\n");
107
108   /* Read the machine description.  */
109
110   insn_code_number = 0;
111   printf ("enum insn_code {\n");
112
113   while (1)
114     {
115       c = read_skip_spaces (infile);
116       if (c == EOF)
117         break;
118       ungetc (c, infile);
119
120       desc = read_rtx (infile);
121       if (GET_CODE (desc) == DEFINE_INSN || GET_CODE (desc) == DEFINE_EXPAND)
122         {
123           gen_insn (desc);
124           insn_code_number++;
125         }
126       if (GET_CODE (desc) == DEFINE_PEEPHOLE
127           || GET_CODE (desc) == DEFINE_PEEPHOLE2
128           || GET_CODE (desc) == DEFINE_SPLIT)
129         {
130           insn_code_number++;
131         }
132     }
133
134   printf ("  CODE_FOR_nothing };\n");
135
136   printf ("\n#define MAX_INSN_CODE ((int) CODE_FOR_nothing)\n");
137
138   printf ("#endif /* MAX_INSN_CODE */\n");
139
140   fflush (stdout);
141   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
142   /* NOTREACHED */
143   return 0;
144 }