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"
40 /****************************************************************/
42 int spreg_lookup_table
= 1;
47 /****************************************************************/
58 typedef struct _spreg_table_entry spreg_table_entry
;
59 struct _spreg_table_entry
{
65 spreg_table_entry
*next
;
68 typedef struct _spreg_table spreg_table
;
70 spreg_table_entry
*sprs
;
74 spreg_table_insert(spreg_table
*table
, table_entry
*entry
)
76 /* create a new spr entry */
77 spreg_table_entry
*new_spr
= ZALLOC(spreg_table_entry
);
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
])
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
);
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
]);
93 new_spr
->name
[i
] = entry
->fields
[spreg_name
][i
];
97 /* insert, by spreg_nr order */
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
;
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
;
114 spreg_table_load(char *file_name
)
116 table
*file
= table_open(file_name
, nr_spreg_fields
, 0);
117 spreg_table
*table
= ZALLOC(spreg_table
);
121 while ((entry
= table_entry_read(file
)) != NULL
) {
122 spreg_table_insert(table
, entry
);
130 /****************************************************************/
132 char *spreg_attributes
[] = {
142 gen_spreg_h(spreg_table
*table
, lf
*file
)
144 spreg_table_entry
*entry
;
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");
156 for (entry
= table
->sprs
;
158 entry
= entry
->next
) {
159 lf_printf(file
, " spr_%s = %d,\n", entry
->name
, entry
->spreg_nr
);
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
;
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
);
173 lf_print_function_type(file
, "int", "INLINE_SPREG", " ");
174 lf_printf(file
, "spr_%s(sprs spr);\n", *attribute
);
177 lf_printf(file
, "\n");
178 lf_printf(file
, "#endif /* _SPREG_H_ */\n");
183 gen_spreg_c(spreg_table
*table
, lf
*file
)
185 spreg_table_entry
*entry
;
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");
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");
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
);
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
);
218 lf_printf(file
, "};\n");
220 for (attribute
= spreg_attributes
;
223 lf_printf(file
, "\n");
224 if (strcmp(*attribute
, "name") == 0) {
225 lf_print_function_type(file
, "const char *", "INLINE_SPREG", "\n");
228 lf_print_function_type(file
, "int", "INLINE_SPREG", "\n");
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",
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
);
251 lf_printf(file
, " default:\n");
252 lf_printf(file
, " return 0;\n");
253 lf_printf(file
, " }\n");
255 lf_printf(file
, "}\n");
258 lf_printf(file
, "\n");
259 lf_printf(file
, "#endif /* _SPREG_C_ */\n");
264 /****************************************************************/
272 lf_file_references file_references
= lf_include_references
;
273 spreg_table
*sprs
= NULL
;
274 char *real_file_name
= NULL
;
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");
288 while ((ch
= getopt(argc
, argv
, "hLsn:r:p:")) != -1) {
289 #if 0 /* For debugging. */
290 fprintf(stderr
, "\t-%c %s\n", ch
, ( optarg
? optarg
: ""));
294 spreg_lookup_table
= 0;
297 sprs
= spreg_table_load(optarg
);
300 real_file_name
= strdup(optarg
);
303 file_references
= lf_omit_references
;
310 lf
*file
= lf_open(optarg
, real_file_name
, file_references
,
311 (is_header
? lf_is_h
: lf_is_c
),
314 gen_spreg_h(sprs
, file
);
316 gen_spreg_c(sprs
, file
);
320 real_file_name
= NULL
;
323 error("unknown option\n");