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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
51 /****************************************************************/
53 int spreg_lookup_table = 1;
58 /****************************************************************/
69 typedef struct _spreg_table_entry spreg_table_entry;
70 struct _spreg_table_entry {
76 spreg_table_entry *next;
79 typedef struct _spreg_table spreg_table;
81 spreg_table_entry *sprs;
85 spreg_table_insert(spreg_table *table, table_entry *entry)
87 /* create a new spr entry */
88 spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
90 new_spr->entry = entry;
91 new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
92 new_spr->is_readonly = (entry->fields[spreg_readonly]
93 ? atoi(entry->fields[spreg_readonly])
95 new_spr->length = atoi(entry->fields[spreg_length]);
96 new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
97 ASSERT(new_spr->name != NULL);
100 for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
101 if (isupper(entry->fields[spreg_name][i]))
102 new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
104 new_spr->name[i] = entry->fields[spreg_name][i];
108 /* insert, by spreg_nr order */
110 spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
111 spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
112 while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
113 ptr_to_spreg_entry = &spreg_entry->next;
114 spreg_entry = *ptr_to_spreg_entry;
116 ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
117 *ptr_to_spreg_entry = new_spr;
118 new_spr->next = spreg_entry;
125 spreg_table_load(char *file_name)
127 table *file = table_open(file_name, nr_spreg_fields, 0);
128 spreg_table *table = ZALLOC(spreg_table);
132 while ((entry = table_entry_read(file)) != NULL) {
133 spreg_table_insert(table, entry);
141 /****************************************************************/
143 char *spreg_attributes[] = {
153 gen_spreg_h(spreg_table *table, lf *file)
155 spreg_table_entry *entry;
158 lf_print__gnu_copyleft(file);
159 lf_printf(file, "\n");
160 lf_printf(file, "#ifndef _SPREG_H_\n");
161 lf_printf(file, "#define _SPREG_H_\n");
162 lf_printf(file, "\n");
163 lf_printf(file, "typedef unsigned_word spreg;\n");
164 lf_printf(file, "\n");
165 lf_printf(file, "typedef enum {\n");
167 for (entry = table->sprs;
169 entry = entry->next) {
170 lf_printf(file, " spr_%s = %d,\n", entry->name, entry->spreg_nr);
173 lf_printf(file, " nr_of_sprs = %d\n", nr_of_sprs);
174 lf_printf(file, "} sprs;\n");
175 lf_printf(file, "\n");
176 for (attribute = spreg_attributes;
179 if (strcmp(*attribute, "name") == 0) {
180 lf_print_function_type(file, "const char *", "INLINE_SPREG", " ");
181 lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
184 lf_print_function_type(file, "int", "INLINE_SPREG", " ");
185 lf_printf(file, "spr_%s(sprs spr);\n", *attribute);
188 lf_printf(file, "\n");
189 lf_printf(file, "#endif /* _SPREG_H_ */\n");
194 gen_spreg_c(spreg_table *table, lf *file)
196 spreg_table_entry *entry;
200 lf_print__gnu_copyleft(file);
201 lf_printf(file, "\n");
202 lf_printf(file, "#ifndef _SPREG_C_\n");
203 lf_printf(file, "#define _SPREG_C_\n");
204 lf_printf(file, "\n");
205 lf_printf(file, "#include \"basics.h\"\n");
206 lf_printf(file, "#include \"spreg.h\"\n");
208 lf_printf(file, "\n");
209 lf_printf(file, "typedef struct _spreg_info {\n");
210 lf_printf(file, " char *name;\n");
211 lf_printf(file, " int is_valid;\n");
212 lf_printf(file, " int length;\n");
213 lf_printf(file, " int is_readonly;\n");
214 lf_printf(file, " int index;\n");
215 lf_printf(file, "} spreg_info;\n");
216 lf_printf(file, "\n");
217 lf_printf(file, "static spreg_info spr_info[nr_of_sprs+1] = {\n");
219 for (spreg_nr = 0; spreg_nr < nr_of_sprs+1; spreg_nr++) {
220 if (entry == NULL || spreg_nr < entry->spreg_nr)
221 lf_printf(file, " { 0, 0, 0, 0, %d},\n", spreg_nr);
223 lf_printf(file, " { \"%s\", %d, %d, %d, spr_%s /*%d*/ },\n",
224 entry->name, 1, entry->length, entry->is_readonly,
225 entry->name, entry->spreg_nr);
229 lf_printf(file, "};\n");
231 for (attribute = spreg_attributes;
234 lf_printf(file, "\n");
235 if (strcmp(*attribute, "name") == 0) {
236 lf_print_function_type(file, "const char *", "INLINE_SPREG", "\n");
239 lf_print_function_type(file, "int", "INLINE_SPREG", "\n");
241 lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
242 lf_printf(file, "{\n");
243 if (spreg_lookup_table
244 || strcmp(*attribute, "name") == 0
245 || strcmp(*attribute, "index") == 0)
246 lf_printf(file, " return spr_info[spr].%s;\n",
249 spreg_table_entry *entry;
250 lf_printf(file, " switch (spr) {\n");
251 for (entry = table->sprs; entry != NULL; entry = entry->next) {
252 lf_printf(file, " case %d:\n", entry->spreg_nr);
253 if (strcmp(*attribute, "is_valid") == 0)
254 lf_printf(file, " return 1;\n");
255 else if (strcmp(*attribute, "is_readonly") == 0)
256 lf_printf(file, " return %d;\n", entry->is_readonly);
257 else if (strcmp(*attribute, "length") == 0)
258 lf_printf(file, " return %d;\n", entry->length);
262 lf_printf(file, " default:\n");
263 lf_printf(file, " return 0;\n");
264 lf_printf(file, " }\n");
266 lf_printf(file, "}\n");
269 lf_printf(file, "\n");
270 lf_printf(file, "#endif /* _SPREG_C_ */\n");
275 /****************************************************************/
283 lf_file_references file_references = lf_include_references;
284 spreg_table *sprs = NULL;
285 char *real_file_name = NULL;
290 printf("Usage: dgen ...\n");
291 printf("-s Use switch instead of table\n");
292 printf("-n <file-name> Use this as cpp line numbering name\n");
293 printf("-h Output header file\n");
294 printf("-p <spreg-file> Output spreg.h(P) or spreg.c(p)\n");
295 printf("-L Suppress cpp line numbering in output files\n");
299 while ((ch = getopt(argc, argv, "hLsn:r:p:")) != -1) {
300 fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
303 spreg_lookup_table = 0;
306 sprs = spreg_table_load(optarg);
309 real_file_name = strdup(optarg);
312 file_references = lf_omit_references;
319 lf *file = lf_open(optarg, real_file_name, file_references,
320 (is_header ? lf_is_h : lf_is_c),
323 gen_spreg_h(sprs, file);
325 gen_spreg_c(sprs, file);
329 real_file_name = NULL;
332 error("unknown option\n");