Include ld-lib.exp from ctf-lib.exp
[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 3 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, see <http://www.gnu.org/licenses/>.
17
18 */
19
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <stdarg.h>
28
29 #include "build-config.h"
30 #include "misc.h"
31 #include "lf.h"
32 #include "table.h"
33
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37 #include <stdlib.h>
38 #include <string.h>
39
40 /****************************************************************/
41
42 int spreg_lookup_table = 1;
43 enum {
44 nr_of_sprs = 1024,
45 };
46
47 /****************************************************************/
48
49
50 typedef enum {
51 spreg_name,
52 spreg_reg_nr,
53 spreg_readonly,
54 spreg_length,
55 nr_spreg_fields,
56 } spreg_fields;
57
58 typedef struct _spreg_table_entry spreg_table_entry;
59 struct _spreg_table_entry {
60 char *name;
61 int spreg_nr;
62 int is_readonly;
63 int length;
64 table_entry *entry;
65 spreg_table_entry *next;
66 };
67
68 typedef struct _spreg_table spreg_table;
69 struct _spreg_table {
70 spreg_table_entry *sprs;
71 };
72
73 static void
74 spreg_table_insert(spreg_table *table, table_entry *entry)
75 {
76 /* create a new spr entry */
77 spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
78 new_spr->next = NULL;
79 new_spr->entry = entry;
80 new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
81 new_spr->is_readonly = (entry->fields[spreg_readonly]
82 ? atoi(entry->fields[spreg_readonly])
83 : 0);
84 new_spr->length = atoi(entry->fields[spreg_length]);
85 new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
86 ASSERT(new_spr->name != NULL);
87 {
88 int i;
89 for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
90 if (isupper(entry->fields[spreg_name][i]))
91 new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
92 else
93 new_spr->name[i] = entry->fields[spreg_name][i];
94 }
95 }
96
97 /* insert, by spreg_nr order */
98 {
99 spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
100 spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
101 while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
102 ptr_to_spreg_entry = &spreg_entry->next;
103 spreg_entry = *ptr_to_spreg_entry;
104 }
105 ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
106 *ptr_to_spreg_entry = new_spr;
107 new_spr->next = spreg_entry;
108 }
109
110 }
111
112
113 static spreg_table *
114 spreg_table_load(char *file_name)
115 {
116 table *file = table_open(file_name, nr_spreg_fields, 0);
117 spreg_table *table = ZALLOC(spreg_table);
118
119 {
120 table_entry *entry;
121 while ((entry = table_entry_read(file)) != NULL) {
122 spreg_table_insert(table, entry);
123 }
124 }
125
126 return table;
127 }
128
129
130 /****************************************************************/
131
132 char *spreg_attributes[] = {
133 "is_valid",
134 "is_readonly",
135 "name",
136 "index",
137 "length",
138 0
139 };
140
141 static void
142 gen_spreg_h(spreg_table *table, lf *file)
143 {
144 spreg_table_entry *entry;
145 char **attribute;
146
147 lf_print__gnu_copyleft(file);
148 lf_printf(file, "\n");
149 lf_printf(file, "#ifndef _SPREG_H_\n");
150 lf_printf(file, "#define _SPREG_H_\n");
151 lf_printf(file, "\n");
152 lf_printf(file, "typedef unsigned_word spreg;\n");
153 lf_printf(file, "\n");
154 lf_printf(file, "typedef enum {\n");
155
156 for (entry = table->sprs;
157 entry != NULL ;
158 entry = entry->next) {
159 lf_printf(file, " spr_%s = %d,\n", entry->name, entry->spreg_nr);
160 }
161
162 lf_printf(file, " nr_of_sprs = %d\n", nr_of_sprs);
163 lf_printf(file, "} sprs;\n");
164 lf_printf(file, "\n");
165 for (attribute = spreg_attributes;
166 *attribute != NULL;
167 attribute++) {
168 if (strcmp(*attribute, "name") == 0) {
169 lf_print_function_type(file, "const char *", "INLINE_SPREG", " ");
170 lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
171 }
172 else {
173 lf_print_function_type(file, "int", "INLINE_SPREG", " ");
174 lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
175 }
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__gnu_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 \"basics.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_print_function_type(file, "const char *", "INLINE_SPREG", "\n");
226 }
227 else {
228 lf_print_function_type(file, "int", "INLINE_SPREG", "\n");
229 }
230 lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
231 lf_printf(file, "{\n");
232 if (spreg_lookup_table
233 || strcmp(*attribute, "name") == 0
234 || strcmp(*attribute, "index") == 0)
235 lf_printf(file, " return spr_info[spr].%s;\n",
236 *attribute);
237 else {
238 spreg_table_entry *entry;
239 lf_printf(file, " switch (spr) {\n");
240 for (entry = table->sprs; entry != NULL; entry = entry->next) {
241 lf_printf(file, " case %d:\n", entry->spreg_nr);
242 if (strcmp(*attribute, "is_valid") == 0)
243 lf_printf(file, " return 1;\n");
244 else if (strcmp(*attribute, "is_readonly") == 0)
245 lf_printf(file, " return %d;\n", entry->is_readonly);
246 else if (strcmp(*attribute, "length") == 0)
247 lf_printf(file, " return %d;\n", entry->length);
248 else
249 ASSERT(0);
250 }
251 lf_printf(file, " default:\n");
252 lf_printf(file, " return 0;\n");
253 lf_printf(file, " }\n");
254 }
255 lf_printf(file, "}\n");
256 }
257
258 lf_printf(file, "\n");
259 lf_printf(file, "#endif /* _SPREG_C_ */\n");
260 }
261
262
263
264 /****************************************************************/
265
266
267 int
268 main(int argc,
269 char **argv,
270 char **envp)
271 {
272 lf_file_references file_references = lf_include_references;
273 spreg_table *sprs = NULL;
274 char *real_file_name = NULL;
275 int is_header = 0;
276 int ch;
277
278 if (argc <= 1) {
279 printf("Usage: dgen ...\n");
280 printf("-s Use switch instead of table\n");
281 printf("-n <file-name> Use this as cpp line numbering name\n");
282 printf("-h Output header file\n");
283 printf("-p <spreg-file> Output spreg.h(P) or spreg.c(p)\n");
284 printf("-L Suppress cpp line numbering in output files\n");
285 }
286
287
288 while ((ch = getopt(argc, argv, "hLsn:r:p:")) != -1) {
289 fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
290 switch(ch) {
291 case 's':
292 spreg_lookup_table = 0;
293 break;
294 case 'r':
295 sprs = spreg_table_load(optarg);
296 break;
297 case 'n':
298 real_file_name = strdup(optarg);
299 break;
300 case 'L':
301 file_references = lf_omit_references;
302 break;
303 case 'h':
304 is_header = 1;
305 break;
306 case 'p':
307 {
308 lf *file = lf_open(optarg, real_file_name, file_references,
309 (is_header ? lf_is_h : lf_is_c),
310 argv[0]);
311 if (is_header)
312 gen_spreg_h(sprs, file);
313 else
314 gen_spreg_c(sprs, file);
315 lf_close(file);
316 is_header = 0;
317 }
318 real_file_name = NULL;
319 break;
320 default:
321 error("unknown option\n");
322 }
323 }
324 return 0;
325 }