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>
37 /****************************************************************/
39 int spreg_lookup_table
= 1;
45 /****************************************************************/
56 typedef struct _spreg_table_entry spreg_table_entry
;
57 struct _spreg_table_entry
{
63 spreg_table_entry
*next
;
66 typedef struct _spreg_table spreg_table
;
68 spreg_table_entry
*sprs
;
72 spreg_table_insert(spreg_table
*table
, table_entry
*entry
)
74 /* create a new spr entry */
75 spreg_table_entry
*new_spr
= ZALLOC(spreg_table_entry
);
77 new_spr
->entry
= entry
;
78 new_spr
->spreg_nr
= atoi(entry
->fields
[spreg_reg_nr
]);
79 new_spr
->is_readonly
= (entry
->fields
[spreg_readonly
]
80 ? atoi(entry
->fields
[spreg_readonly
])
82 new_spr
->length
= atoi(entry
->fields
[spreg_length
]);
83 new_spr
->name
= (char*)zalloc(strlen(entry
->fields
[spreg_name
]) + 1);
84 ASSERT(new_spr
->name
!= NULL
);
87 for (i
= 0; entry
->fields
[spreg_name
][i
] != '\0'; i
++) {
88 if (isupper(entry
->fields
[spreg_name
][i
]))
89 new_spr
->name
[i
] = tolower(entry
->fields
[spreg_name
][i
]);
91 new_spr
->name
[i
] = entry
->fields
[spreg_name
][i
];
95 /* insert, by spreg_nr order */
97 spreg_table_entry
**ptr_to_spreg_entry
= &table
->sprs
;
98 spreg_table_entry
*spreg_entry
= *ptr_to_spreg_entry
;
99 while (spreg_entry
!= NULL
&& spreg_entry
->spreg_nr
< new_spr
->spreg_nr
) {
100 ptr_to_spreg_entry
= &spreg_entry
->next
;
101 spreg_entry
= *ptr_to_spreg_entry
;
103 ASSERT(spreg_entry
== NULL
|| spreg_entry
->spreg_nr
!= new_spr
->spreg_nr
);
104 *ptr_to_spreg_entry
= new_spr
;
105 new_spr
->next
= spreg_entry
;
112 spreg_table_load(char *file_name
)
114 table
*file
= table_open(file_name
, nr_spreg_fields
);
115 spreg_table
*table
= ZALLOC(spreg_table
);
119 while ((entry
= table_entry_read(file
)) != NULL
) {
120 spreg_table_insert(table
, entry
);
128 /****************************************************************/
130 char *spreg_attributes
[] = {
140 gen_spreg_h(spreg_table
*table
, lf
*file
)
142 spreg_table_entry
*entry
;
145 lf_print_copyleft(file
);
146 lf_printf(file
, "\n");
147 lf_printf(file
, "#ifndef _SPREG_H_\n");
148 lf_printf(file
, "#define _SPREG_H_\n");
149 lf_printf(file
, "\n");
150 lf_printf(file
, "#ifndef INLINE_SPREG\n");
151 lf_printf(file
, "#define INLINE_SPREG\n");
152 lf_printf(file
, "#endif\n");
153 lf_printf(file
, "\n");
154 lf_printf(file
, "typedef unsigned_word spreg;\n");
155 lf_printf(file
, "\n");
156 lf_printf(file
, "typedef enum {\n");
158 for (entry
= table
->sprs
;
160 entry
= entry
->next
) {
161 lf_printf(file
, " spr_%s = %d,\n", entry
->name
, entry
->spreg_nr
);
164 lf_printf(file
, " nr_of_sprs = %d\n", nr_of_sprs
);
165 lf_printf(file
, "} sprs;\n");
166 lf_printf(file
, "\n");
167 for (attribute
= spreg_attributes
;
170 if (strcmp(*attribute
, "name") == 0)
171 lf_printf(file
, "INLINE_SPREG char *spr_%s(sprs spr);\n",
174 lf_printf(file
, "INLINE_SPREG int spr_%s(sprs spr);\n",
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_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 \"words.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_printf(file
, "INLINE_SPREG char *\n");
227 lf_printf(file
, "INLINE_SPREG int\n");
228 lf_printf(file
, "spr_%s(sprs spr)\n", *attribute
);
229 lf_printf(file
, "{\n");
230 if (spreg_lookup_table
231 || strcmp(*attribute
, "name") == 0
232 || strcmp(*attribute
, "index") == 0)
233 lf_printf(file
, " return spr_info[spr].%s;\n",
236 spreg_table_entry
*entry
;
237 lf_printf(file
, " switch (spr) {\n");
238 for (entry
= table
->sprs
; entry
!= NULL
; entry
= entry
->next
) {
239 lf_printf(file
, " case %d:\n", entry
->spreg_nr
);
240 if (strcmp(*attribute
, "is_valid") == 0)
241 lf_printf(file
, " return 1;\n");
242 else if (strcmp(*attribute
, "is_readonly") == 0)
243 lf_printf(file
, " return %d;\n", entry
->is_readonly
);
244 else if (strcmp(*attribute
, "length") == 0)
245 lf_printf(file
, " return %d;\n", entry
->length
);
249 lf_printf(file
, " default:\n");
250 lf_printf(file
, " return 0;\n");
251 lf_printf(file
, " }\n");
253 lf_printf(file
, "}\n");
256 lf_printf(file
, "\n");
257 lf_printf(file
, "#endif /* _SPREG_C_ */\n");
262 /****************************************************************/
270 spreg_table
*sprs
= NULL
;
271 char *real_file_name
= NULL
;
275 printf("Usage: dgen ...\n");
276 printf("-s Use switch instead of table\n");
277 printf("-n Use this as cpp line numbering name\n");
278 printf("-[Pp] <spreg> Output spreg.h(P) or spreg.c(p)\n");
279 printf("-l Suppress cpp line numbering in output files\n");
283 while ((ch
= getopt(argc
, argv
, "lsn:r:P:p:")) != -1) {
284 fprintf(stderr
, "\t-%c %s\n", ch
, ( optarg
? optarg
: ""));
290 spreg_lookup_table
= 0;
293 sprs
= spreg_table_load(optarg
);
296 real_file_name
= strdup(optarg
);
301 lf
*file
= lf_open(optarg
, real_file_name
, number_lines
);
304 gen_spreg_h(sprs
, file
);
307 gen_spreg_c(sprs
, file
);
312 real_file_name
= NULL
;
315 error("unknown option\n");