Commit | Line | Data |
---|---|---|
a983c8f0 MM |
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 2 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, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | ||
19 | */ | |
20 | ||
21 | ||
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
a983c8f0 MM |
25 | #include <getopt.h> |
26 | #include <stdio.h> | |
27 | #include <ctype.h> | |
28 | #include <stdarg.h> | |
a983c8f0 | 29 | |
c494cadd | 30 | #include "config.h" |
a983c8f0 MM |
31 | #include "misc.h" |
32 | #include "lf.h" | |
33 | #include "table.h" | |
34 | ||
c494cadd MM |
35 | #ifdef HAVE_UNISTD_H |
36 | #include <unistd.h> | |
37 | #endif | |
38 | ||
39 | #ifdef HAVE_STDLIB_H | |
40 | #include <stdlib.h> | |
41 | #endif | |
42 | ||
43 | #ifdef HAVE_STRING_H | |
44 | #include <string.h> | |
45 | #else | |
46 | #ifdef HAVE_STRINGS_H | |
47 | #include <strings.h> | |
48 | #endif | |
49 | #endif | |
50 | ||
a983c8f0 MM |
51 | /****************************************************************/ |
52 | ||
53 | int spreg_lookup_table = 1; | |
54 | int number_lines = 1; | |
55 | enum { | |
56 | nr_of_sprs = 1024, | |
57 | }; | |
58 | ||
59 | /****************************************************************/ | |
60 | ||
61 | ||
62 | typedef enum { | |
63 | spreg_name, | |
64 | spreg_reg_nr, | |
65 | spreg_readonly, | |
66 | spreg_length, | |
67 | nr_spreg_fields, | |
68 | } spreg_fields; | |
69 | ||
70 | typedef struct _spreg_table_entry spreg_table_entry; | |
71 | struct _spreg_table_entry { | |
72 | char *name; | |
73 | int spreg_nr; | |
74 | int is_readonly; | |
75 | int length; | |
76 | table_entry *entry; | |
77 | spreg_table_entry *next; | |
78 | }; | |
79 | ||
80 | typedef struct _spreg_table spreg_table; | |
81 | struct _spreg_table { | |
82 | spreg_table_entry *sprs; | |
83 | }; | |
84 | ||
85 | static void | |
86 | spreg_table_insert(spreg_table *table, table_entry *entry) | |
87 | { | |
88 | /* create a new spr entry */ | |
89 | spreg_table_entry *new_spr = ZALLOC(spreg_table_entry); | |
90 | new_spr->next = NULL; | |
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]) | |
95 | : 0); | |
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); | |
99 | { | |
100 | int i; | |
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]); | |
104 | else | |
105 | new_spr->name[i] = entry->fields[spreg_name][i]; | |
106 | } | |
107 | } | |
108 | ||
109 | /* insert, by spreg_nr order */ | |
110 | { | |
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; | |
116 | } | |
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; | |
120 | } | |
121 | ||
122 | } | |
123 | ||
124 | ||
125 | static spreg_table * | |
126 | spreg_table_load(char *file_name) | |
127 | { | |
128 | table *file = table_open(file_name, nr_spreg_fields); | |
129 | spreg_table *table = ZALLOC(spreg_table); | |
130 | ||
131 | { | |
132 | table_entry *entry; | |
133 | while ((entry = table_entry_read(file)) != NULL) { | |
134 | spreg_table_insert(table, entry); | |
135 | } | |
136 | } | |
137 | ||
138 | return table; | |
139 | } | |
140 | ||
141 | ||
142 | /****************************************************************/ | |
143 | ||
144 | char *spreg_attributes[] = { | |
145 | "is_valid", | |
146 | "is_readonly", | |
147 | "name", | |
148 | "index", | |
149 | "length", | |
150 | 0 | |
151 | }; | |
152 | ||
153 | static void | |
154 | gen_spreg_h(spreg_table *table, lf *file) | |
155 | { | |
156 | spreg_table_entry *entry; | |
157 | char **attribute; | |
158 | ||
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"); | |
171 | ||
172 | for (entry = table->sprs; | |
173 | entry != NULL ; | |
174 | entry = entry->next) { | |
175 | lf_printf(file, " spr_%s = %d,\n", entry->name, entry->spreg_nr); | |
176 | } | |
177 | ||
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; | |
182 | *attribute != NULL; | |
183 | attribute++) { | |
184 | if (strcmp(*attribute, "name") == 0) | |
185 | lf_printf(file, "INLINE_SPREG char *spr_%s(sprs spr);\n", | |
186 | *attribute); | |
187 | else | |
188 | lf_printf(file, "INLINE_SPREG int spr_%s(sprs spr);\n", | |
189 | *attribute); | |
190 | } | |
191 | lf_printf(file, "\n"); | |
192 | lf_printf(file, "#endif /* _SPREG_H_ */\n"); | |
193 | } | |
194 | ||
195 | ||
196 | static void | |
197 | gen_spreg_c(spreg_table *table, lf *file) | |
198 | { | |
199 | spreg_table_entry *entry; | |
200 | char **attribute; | |
201 | int spreg_nr; | |
202 | ||
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"); | |
210 | ||
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"); | |
221 | entry = table->sprs; | |
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); | |
225 | else { | |
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); | |
229 | entry = entry->next; | |
230 | } | |
231 | } | |
232 | lf_printf(file, "};\n"); | |
233 | ||
234 | for (attribute = spreg_attributes; | |
235 | *attribute != NULL; | |
236 | attribute++) { | |
237 | lf_printf(file, "\n"); | |
238 | if (strcmp(*attribute, "name") == 0) | |
239 | lf_printf(file, "INLINE_SPREG char *\n"); | |
240 | else | |
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", | |
248 | *attribute); | |
249 | else { | |
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); | |
260 | else | |
261 | ASSERT(0); | |
262 | } | |
263 | lf_printf(file, " default:\n"); | |
264 | lf_printf(file, " return 0;\n"); | |
265 | lf_printf(file, " }\n"); | |
266 | } | |
267 | lf_printf(file, "}\n"); | |
268 | } | |
269 | ||
270 | lf_printf(file, "\n"); | |
271 | lf_printf(file, "#endif /* _SPREG_C_ */\n"); | |
272 | } | |
273 | ||
274 | ||
275 | ||
276 | /****************************************************************/ | |
277 | ||
278 | ||
279 | int | |
280 | main(int argc, | |
281 | char **argv, | |
282 | char **envp) | |
283 | { | |
284 | spreg_table *sprs = NULL; | |
285 | char *real_file_name = NULL; | |
286 | int ch; | |
287 | ||
288 | if (argc <= 1) { | |
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"); | |
294 | } | |
295 | ||
296 | ||
297 | while ((ch = getopt(argc, argv, "lsn:r:P:p:")) != -1) { | |
298 | fprintf(stderr, "\t-%c %s\n", ch, ( optarg ? optarg : "")); | |
299 | switch(ch) { | |
300 | case 'l': | |
301 | number_lines = 0; | |
302 | break; | |
303 | case 's': | |
304 | spreg_lookup_table = 0; | |
305 | break; | |
306 | case 'r': | |
307 | sprs = spreg_table_load(optarg); | |
308 | break; | |
309 | case 'n': | |
310 | real_file_name = strdup(optarg); | |
311 | break; | |
312 | case 'P': | |
313 | case 'p': | |
314 | { | |
315 | lf *file = lf_open(optarg, real_file_name, number_lines); | |
316 | switch (ch) { | |
317 | case 'P': | |
318 | gen_spreg_h(sprs, file); | |
319 | break; | |
320 | case 'p': | |
321 | gen_spreg_c(sprs, file); | |
322 | break; | |
323 | } | |
324 | lf_close(file); | |
325 | } | |
326 | real_file_name = NULL; | |
327 | break; | |
328 | default: | |
329 | error("unknown option\n"); | |
330 | } | |
331 | } | |
332 | return 0; | |
333 | } |