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;
59 /****************************************************************/
70 typedef struct _spreg_table_entry spreg_table_entry;
71 struct _spreg_table_entry {
77 spreg_table_entry *next;
80 typedef struct _spreg_table spreg_table;
82 spreg_table_entry *sprs;
86 spreg_table_insert(spreg_table *table, table_entry *entry)
88 /* create a new spr entry */
89 spreg_table_entry *new_spr = ZALLOC(spreg_table_entry);
91 new_spr->entry = entry;
92 new_spr->spreg_nr = atoi(entry->fields[spreg_reg_nr]);
93 new_spr->is_readonly = (entry->fields[spreg_readonly]
94 ? atoi(entry->fields[spreg_readonly])
96 new_spr->length = atoi(entry->fields[spreg_length]);
97 new_spr->name = (char*)zalloc(strlen(entry->fields[spreg_name]) + 1);
98 ASSERT(new_spr->name != NULL);
101 for (i = 0; entry->fields[spreg_name][i] != '\0'; i++) {
102 if (isupper(entry->fields[spreg_name][i]))
103 new_spr->name[i] = tolower(entry->fields[spreg_name][i]);
105 new_spr->name[i] = entry->fields[spreg_name][i];
109 /* insert, by spreg_nr order */
111 spreg_table_entry **ptr_to_spreg_entry = &table->sprs;
112 spreg_table_entry *spreg_entry = *ptr_to_spreg_entry;
113 while (spreg_entry != NULL && spreg_entry->spreg_nr < new_spr->spreg_nr) {
114 ptr_to_spreg_entry = &spreg_entry->next;
115 spreg_entry = *ptr_to_spreg_entry;
117 ASSERT(spreg_entry == NULL || spreg_entry->spreg_nr != new_spr->spreg_nr);
118 *ptr_to_spreg_entry = new_spr;
119 new_spr->next = spreg_entry;
126 spreg_table_load(char *file_name)
128 table *file = table_open(file_name, nr_spreg_fields);
129 spreg_table *table = ZALLOC(spreg_table);
133 while ((entry = table_entry_read(file)) != NULL) {
134 spreg_table_insert(table, entry);
142 /****************************************************************/
144 char *spreg_attributes[] = {
154 gen_spreg_h(spreg_table *table, lf *file)
156 spreg_table_entry *entry;
159 lf_print_copyleft(file);
160 lf_printf(file, "\n");
161 lf_printf(file, "#ifndef _SPREG_H_\n");
162 lf_printf(file, "#define _SPREG_H_\n");
163 lf_printf(file, "\n");
164 lf_printf(file, "#ifndef INLINE_SPREG\n");
165 lf_printf(file, "#define INLINE_SPREG\n");
166 lf_printf(file, "#endif\n");
167 lf_printf(file, "\n");
168 lf_printf(file, "typedef unsigned_word spreg;\n");
169 lf_printf(file, "\n");
170 lf_printf(file, "typedef enum {\n");
172 for (entry = table->sprs;
174 entry = entry->next) {
175 lf_printf(file, " spr_%s = %d,\n", entry->name, entry->spreg_nr);
178 lf_printf(file, " nr_of_sprs = %d\n", nr_of_sprs);
179 lf_printf(file, "} sprs;\n");
180 lf_printf(file, "\n");
181 for (attribute = spreg_attributes;
184 if (strcmp(*attribute, "name") == 0)
185 lf_printf(file, "INLINE_SPREG char *spr_%s(sprs spr);\n",
188 lf_printf(file, "INLINE_SPREG int spr_%s(sprs spr);\n",
191 lf_printf(file, "\n");
192 lf_printf(file, "#endif /* _SPREG_H_ */\n");
197 gen_spreg_c(spreg_table *table, lf *file)
199 spreg_table_entry *entry;
203 lf_print_copyleft(file);
204 lf_printf(file, "\n");
205 lf_printf(file, "#ifndef _SPREG_C_\n");
206 lf_printf(file, "#define _SPREG_C_\n");
207 lf_printf(file, "\n");
208 lf_printf(file, "#include \"words.h\"\n");
209 lf_printf(file, "#include \"spreg.h\"\n");
211 lf_printf(file, "\n");
212 lf_printf(file, "typedef struct _spreg_info {\n");
213 lf_printf(file, " char *name;\n");
214 lf_printf(file, " int is_valid;\n");
215 lf_printf(file, " int length;\n");
216 lf_printf(file, " int is_readonly;\n");
217 lf_printf(file, " int index;\n");
218 lf_printf(file, "} spreg_info;\n");
219 lf_printf(file, "\n");
220 lf_printf(file, "static spreg_info spr_info[nr_of_sprs+1] = {\n");
222 for (spreg_nr = 0; spreg_nr < nr_of_sprs+1; spreg_nr++) {
223 if (entry == NULL || spreg_nr < entry->spreg_nr)
224 lf_printf(file, " { 0, 0, 0, 0, %d},\n", spreg_nr);
226 lf_printf(file, " { \"%s\", %d, %d, %d, spr_%s /*%d*/ },\n",
227 entry->name, 1, entry->length, entry->is_readonly,
228 entry->name, entry->spreg_nr);
232 lf_printf(file, "};\n");
234 for (attribute = spreg_attributes;
237 lf_printf(file, "\n");
238 if (strcmp(*attribute, "name") == 0)
239 lf_printf(file, "INLINE_SPREG char *\n");
241 lf_printf(file, "INLINE_SPREG int\n");
242 lf_printf(file, "spr_%s(sprs spr)\n", *attribute);
243 lf_printf(file, "{\n");
244 if (spreg_lookup_table
245 || strcmp(*attribute, "name") == 0
246 || strcmp(*attribute, "index") == 0)
247 lf_printf(file, " return spr_info[spr].%s;\n",
250 spreg_table_entry *entry;
251 lf_printf(file, " switch (spr) {\n");
252 for (entry = table->sprs; entry != NULL; entry = entry->next) {
253 lf_printf(file, " case %d:\n", entry->spreg_nr);
254 if (strcmp(*attribute, "is_valid") == 0)
255 lf_printf(file, " return 1;\n");
256 else if (strcmp(*attribute, "is_readonly") == 0)
257 lf_printf(file, " return %d;\n", entry->is_readonly);
258 else if (strcmp(*attribute, "length") == 0)
259 lf_printf(file, " return %d;\n", entry->length);
263 lf_printf(file, " default:\n");
264 lf_printf(file, " return 0;\n");
265 lf_printf(file, " }\n");
267 lf_printf(file, "}\n");
270 lf_printf(file, "\n");
271 lf_printf(file, "#endif /* _SPREG_C_ */\n");
276 /****************************************************************/
284 spreg_table *sprs = NULL;
285 char *real_file_name = NULL;
289 printf("Usage: dgen ...\n");
290 printf("-s Use switch instead of table\n");
291 printf("-n Use this as cpp line numbering name\n");
292 printf("-[Pp] <spreg> Output spreg.h(P) or spreg.c(p)\n");
293 printf("-l Suppress cpp line numbering in output files\n");
297 while ((ch = getopt(argc, argv, "lsn:r:P:p:")) != -1) {
298 fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : ""));
304 spreg_lookup_table = 0;
307 sprs = spreg_table_load(optarg);
310 real_file_name = strdup(optarg);
315 lf *file = lf_open(optarg, real_file_name, number_lines);
318 gen_spreg_h(sprs, file);
321 gen_spreg_c(sprs, file);
326 real_file_name = NULL;
329 error("unknown option\n");