1 /* This file is part of the program psim.
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
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.
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.
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/>.
21 #include <sys/types.h>
29 #include "build-config.h"
50 /****************************************************************/
52 int spreg_lookup_table = 1;
57 /****************************************************************/
68 typedef struct _spreg_table_entry spreg_table_entry;
69 struct _spreg_table_entry {
75 spreg_table_entry *next;
78 typedef struct _spreg_table spreg_table;
80 spreg_table_entry *sprs;
84 spreg_table_insert(spreg_table *table, table_entry *entry)
86 /* create a new spr entry */
87 spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
89 new_spr->entry = entry;
90 new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
91 new_spr->is_readonly = (entry->fields[spreg_readonly]
92 ? atoi(entry->fields[spreg_readonly])
94 new_spr->length = atoi(entry->fields[spreg_length]);
95 new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
96 ASSERT(new_spr->name != NULL);
99 for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
100 if (isupper(entry->fields[spreg_name][i]))
101 new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
103 new_spr->name[i] = entry->fields[spreg_name][i];
107 /* insert, by spreg_nr order */
109 spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
110 spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
111 while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
112 ptr_to_spreg_entry = &spreg_entry->next;
113 spreg_entry = *ptr_to_spreg_entry;
115 ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
116 *ptr_to_spreg_entry = new_spr;
117 new_spr->next = spreg_entry;
124 spreg_table_load(char *file_name)
126 table *file = table_open(file_name, nr_spreg_fields, 0);
127 spreg_table *table = ZALLOC(spreg_table);
131 while ((entry = table_entry_read(file)) != NULL) {
132 spreg_table_insert(table, entry);
140 /****************************************************************/
142 char *spreg_attributes[] = {
152 gen_spreg_h(spreg_table *table, lf *file)
154 spreg_table_entry *entry;
157 lf_print__gnu_copyleft(file);
158 lf_printf(file, "\n");
159 lf_printf(file, "#ifndef _SPREG_H_\n");
160 lf_printf(file, "#define _SPREG_H_\n");
161 lf_printf(file, "\n");
162 lf_printf(file, "typedef unsigned_word spreg;\n");
163 lf_printf(file, "\n");
164 lf_printf(file, "typedef enum {\n");
166 for (entry = table->sprs;
168 entry = entry->next) {
169 lf_printf(file, " spr_%s = %d,\n", entry->name, entry->spreg_nr);
172 lf_printf(file, " nr_of_sprs = %d\n", nr_of_sprs);
173 lf_printf(file, "} sprs;\n");
174 lf_printf(file, "\n");
175 for (attribute = spreg_attributes;
178 if (strcmp(*attribute, "name") == 0) {
179 lf_print_function_type(file, "const char *", "INLINE_SPREG", " ");
180 lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
183 lf_print_function_type(file, "int", "INLINE_SPREG", " ");
184 lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
187 lf_printf(file, "\n");
188 lf_printf(file, "#endif /* _SPREG_H_ */\n");
193 gen_spreg_c(spreg_table *table, lf *file)
195 spreg_table_entry *entry;
199 lf_print__gnu_copyleft(file);
200 lf_printf(file, "\n");
201 lf_printf(file, "#ifndef _SPREG_C_\n");
202 lf_printf(file, "#define _SPREG_C_\n");
203 lf_printf(file, "\n");
204 lf_printf(file, "#include \"basics.h\"\n");
205 lf_printf(file, "#include \"spreg.h\"\n");
207 lf_printf(file, "\n");
208 lf_printf(file, "typedef struct _spreg_info {\n");
209 lf_printf(file, " char *name;\n");
210 lf_printf(file, " int is_valid;\n");
211 lf_printf(file, " int length;\n");
212 lf_printf(file, " int is_readonly;\n");
213 lf_printf(file, " int index;\n");
214 lf_printf(file, "} spreg_info;\n");
215 lf_printf(file, "\n");
216 lf_printf(file, "static spreg_info spr_info[nr_of_sprs+1] = {\n");
218 for (spreg_nr = 0; spreg_nr < nr_of_sprs+1; spreg_nr++) {
219 if (entry == NULL || spreg_nr < entry->spreg_nr)
220 lf_printf(file, " { 0, 0, 0, 0, %d},\n", spreg_nr);
222 lf_printf(file, " { \"%s\", %d, %d, %d, spr_%s /*%d*/ },\n",
223 entry->name, 1, entry->length, entry->is_readonly,
224 entry->name, entry->spreg_nr);
228 lf_printf(file, "};\n");
230 for (attribute = spreg_attributes;
233 lf_printf(file, "\n");
234 if (strcmp(*attribute, "name") == 0) {
235 lf_print_function_type(file, "const char *", "INLINE_SPREG", "\n");
238 lf_print_function_type(file, "int", "INLINE_SPREG", "\n");
240 lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
241 lf_printf(file, "{\n");
242 if (spreg_lookup_table
243 || strcmp(*attribute, "name") == 0
244 || strcmp(*attribute, "index") == 0)
245 lf_printf(file, " return spr_info[spr].%s;\n",
248 spreg_table_entry *entry;
249 lf_printf(file, " switch (spr) {\n");
250 for (entry = table->sprs; entry != NULL; entry = entry->next) {
251 lf_printf(file, " case %d:\n", entry->spreg_nr);
252 if (strcmp(*attribute, "is_valid") == 0)
253 lf_printf(file, " return 1;\n");
254 else if (strcmp(*attribute, "is_readonly") == 0)
255 lf_printf(file, " return %d;\n", entry->is_readonly);
256 else if (strcmp(*attribute, "length") == 0)
257 lf_printf(file, " return %d;\n", entry->length);
261 lf_printf(file, " default:\n");
262 lf_printf(file, " return 0;\n");
263 lf_printf(file, " }\n");
265 lf_printf(file, "}\n");
268 lf_printf(file, "\n");
269 lf_printf(file, "#endif /* _SPREG_C_ */\n");
274 /****************************************************************/
282 lf_file_references file_references = lf_include_references;
283 spreg_table *sprs = NULL;
284 char *real_file_name = NULL;
289 printf("Usage: dgen ...\n");
290 printf("-s Use switch instead of table\n");
291 printf("-n <file-name> Use this as cpp line numbering name\n");
292 printf("-h Output header file\n");
293 printf("-p <spreg-file> Output spreg.h(P) or spreg.c(p)\n");
294 printf("-L Suppress cpp line numbering in output files\n");
298 while ((ch = getopt(argc, argv, "hLsn:r:p:")) != -1) {
299 fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
302 spreg_lookup_table = 0;
305 sprs = spreg_table_load(optarg);
308 real_file_name = strdup(optarg);
311 file_references = lf_omit_references;
318 lf *file = lf_open(optarg, real_file_name, file_references,
319 (is_header ? lf_is_h : lf_is_c),
322 gen_spreg_h(sprs, file);
324 gen_spreg_c(sprs, file);
328 real_file_name = NULL;
331 error("unknown option\n");