Andrew's latest changes & print all instruction counts if -I
[deliverable/binutils-gdb.git] / sim / ppc / dgen.c
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <getopt.h>
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdarg.h>
31 #include <string.h>
32
33 #include "misc.h"
34 #include "lf.h"
35 #include "table.h"
36
37 /****************************************************************/
38
39 int spreg_lookup_table = 1;
40 int number_lines = 1;
41 enum {
42 nr_of_sprs = 1024,
43 };
44
45 /****************************************************************/
46
47
48 typedef enum {
49 spreg_name,
50 spreg_reg_nr,
51 spreg_readonly,
52 spreg_length,
53 nr_spreg_fields,
54 } spreg_fields;
55
56 typedef struct _spreg_table_entry spreg_table_entry;
57 struct _spreg_table_entry {
58 char *name;
59 int spreg_nr;
60 int is_readonly;
61 int length;
62 table_entry *entry;
63 spreg_table_entry *next;
64 };
65
66 typedef struct _spreg_table spreg_table;
67 struct _spreg_table {
68 spreg_table_entry *sprs;
69 };
70
71 static void
72 spreg_table_insert(spreg_table *table, table_entry *entry)
73 {
74 /* create a new spr entry */
75 spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
76 new_spr->next = NULL;
77 new_spr->entry = entry;
78 new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
79 new_spr->is_readonly = (entry->fields[spreg_readonly]
80 ? atoi(entry->fields[spreg_readonly])
81 : 0);
82 new_spr->length = atoi(entry->fields[spreg_length]);
83 new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
84 ASSERT(new_spr->name != NULL);
85 {
86 int i;
87 for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
88 if (isupper(entry->fields[spreg_name][i]))
89 new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
90 else
91 new_spr->name[i] = entry->fields[spreg_name][i];
92 }
93 }
94
95 /* insert, by spreg_nr order */
96 {
97 spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
98 spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
99 while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
100 ptr_to_spreg_entry = &spreg_entry->next;
101 spreg_entry = *ptr_to_spreg_entry;
102 }
103 ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
104 *ptr_to_spreg_entry = new_spr;
105 new_spr->next = spreg_entry;
106 }
107
108 }
109
110
111 static spreg_table *
112 spreg_table_load(char *file_name)
113 {
114 table *file = table_open(file_name, nr_spreg_fields);
115 spreg_table *table = ZALLOC(spreg_table);
116
117 {
118 table_entry *entry;
119 while ((entry = table_entry_read(file)) != NULL) {
120 spreg_table_insert(table, entry);
121 }
122 }
123
124 return table;
125 }
126
127
128 /****************************************************************/
129
130 char *spreg_attributes[] = {
131 "is_valid",
132 "is_readonly",
133 "name",
134 "index",
135 "length",
136 0
137 };
138
139 static void
140 gen_spreg_h(spreg_table *table, lf *file)
141 {
142 spreg_table_entry *entry;
143 char **attribute;
144
145 lf_print_copyleft(file);
146 lf_printf(file, "\n");
147 lf_printf(file, "#ifndef _SPREG_H_\n");
148 lf_printf(file, "#define _SPREG_H_\n");
149 lf_printf(file, "\n");
150 lf_printf(file, "#ifndef INLINE_SPREG\n");
151 lf_printf(file, "#define INLINE_SPREG\n");
152 lf_printf(file, "#endif\n");
153 lf_printf(file, "\n");
154 lf_printf(file, "typedef unsigned_word spreg;\n");
155 lf_printf(file, "\n");
156 lf_printf(file, "typedef enum {\n");
157
158 for (entry = table->sprs;
159 entry != NULL ;
160 entry = entry->next) {
161 lf_printf(file, " spr_%s = %d,\n", entry->name, entry->spreg_nr);
162 }
163
164 lf_printf(file, " nr_of_sprs = %d\n", nr_of_sprs);
165 lf_printf(file, "} sprs;\n");
166 lf_printf(file, "\n");
167 for (attribute = spreg_attributes;
168 *attribute != NULL;
169 attribute++) {
170 if (strcmp(*attribute, "name") == 0)
171 lf_printf(file, "INLINE_SPREG char *spr_%s(sprs spr);\n",
172 *attribute);
173 else
174 lf_printf(file, "INLINE_SPREG int spr_%s(sprs spr);\n",
175 *attribute);
176 }
177 lf_printf(file, "\n");
178 lf_printf(file, "#endif /* _SPREG_H_ */\n");
179 }
180
181
182 static void
183 gen_spreg_c(spreg_table *table, lf *file)
184 {
185 spreg_table_entry *entry;
186 char **attribute;
187 int spreg_nr;
188
189 lf_print_copyleft(file);
190 lf_printf(file, "\n");
191 lf_printf(file, "#ifndef _SPREG_C_\n");
192 lf_printf(file, "#define _SPREG_C_\n");
193 lf_printf(file, "\n");
194 lf_printf(file, "#include \"words.h\"\n");
195 lf_printf(file, "#include \"spreg.h\"\n");
196
197 lf_printf(file, "\n");
198 lf_printf(file, "typedef struct _spreg_info {\n");
199 lf_printf(file, " char *name;\n");
200 lf_printf(file, " int is_valid;\n");
201 lf_printf(file, " int length;\n");
202 lf_printf(file, " int is_readonly;\n");
203 lf_printf(file, " int index;\n");
204 lf_printf(file, "} spreg_info;\n");
205 lf_printf(file, "\n");
206 lf_printf(file, "static spreg_info spr_info[nr_of_sprs+1] = {\n");
207 entry = table->sprs;
208 for (spreg_nr = 0; spreg_nr < nr_of_sprs+1; spreg_nr++) {
209 if (entry == NULL || spreg_nr < entry->spreg_nr)
210 lf_printf(file, " { 0, 0, 0, 0, %d},\n", spreg_nr);
211 else {
212 lf_printf(file, " { \"%s\", %d, %d, %d, spr_%s /*%d*/ },\n",
213 entry->name, 1, entry->length, entry->is_readonly,
214 entry->name, entry->spreg_nr);
215 entry = entry->next;
216 }
217 }
218 lf_printf(file, "};\n");
219
220 for (attribute = spreg_attributes;
221 *attribute != NULL;
222 attribute++) {
223 lf_printf(file, "\n");
224 if (strcmp(*attribute, "name") == 0)
225 lf_printf(file, "INLINE_SPREG char *\n");
226 else
227 lf_printf(file, "INLINE_SPREG int\n");
228 lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
229 lf_printf(file, "{\n");
230 if (spreg_lookup_table
231 || strcmp(*attribute, "name") == 0
232 || strcmp(*attribute, "index") == 0)
233 lf_printf(file, " return spr_info[spr].%s;\n",
234 *attribute);
235 else {
236 spreg_table_entry *entry;
237 lf_printf(file, " switch (spr) {\n");
238 for (entry = table->sprs; entry != NULL; entry = entry->next) {
239 lf_printf(file, " case %d:\n", entry->spreg_nr);
240 if (strcmp(*attribute, "is_valid") == 0)
241 lf_printf(file, " return 1;\n");
242 else if (strcmp(*attribute, "is_readonly") == 0)
243 lf_printf(file, " return %d;\n", entry->is_readonly);
244 else if (strcmp(*attribute, "length") == 0)
245 lf_printf(file, " return %d;\n", entry->length);
246 else
247 ASSERT(0);
248 }
249 lf_printf(file, " default:\n");
250 lf_printf(file, " return 0;\n");
251 lf_printf(file, " }\n");
252 }
253 lf_printf(file, "}\n");
254 }
255
256 lf_printf(file, "\n");
257 lf_printf(file, "#endif /* _SPREG_C_ */\n");
258 }
259
260
261
262 /****************************************************************/
263
264
265 int
266 main(int argc,
267 char **argv,
268 char **envp)
269 {
270 spreg_table *sprs = NULL;
271 char *real_file_name = NULL;
272 int ch;
273
274 if (argc <= 1) {
275 printf("Usage: dgen ...\n");
276 printf("-s Use switch instead of table\n");
277 printf("-n Use this as cpp line numbering name\n");
278 printf("-[Pp] <spreg> Output spreg.h(P) or spreg.c(p)\n");
279 printf("-l Suppress cpp line numbering in output files\n");
280 }
281
282
283 while ((ch = getopt(argc, argv, "lsn:r:P:p:")) != -1) {
284 fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
285 switch(ch) {
286 case 'l':
287 number_lines = 0;
288 break;
289 case 's':
290 spreg_lookup_table = 0;
291 break;
292 case 'r':
293 sprs = spreg_table_load(optarg);
294 break;
295 case 'n':
296 real_file_name = strdup(optarg);
297 break;
298 case 'P':
299 case 'p':
300 {
301 lf *file = lf_open(optarg, real_file_name, number_lines);
302 switch (ch) {
303 case 'P':
304 gen_spreg_h(sprs, file);
305 break;
306 case 'p':
307 gen_spreg_c(sprs, file);
308 break;
309 }
310 lf_close(file);
311 }
312 real_file_name = NULL;
313 break;
314 default:
315 error("unknown option\n");
316 }
317 }
318 return 0;
319 }
This page took 0.03719 seconds and 5 git commands to generate.