aicasm_symbol.c revision 1.3 1 1.3 fvdl /* $NetBSD: aicasm_symbol.c,v 1.3 2003/04/19 19:26:11 fvdl Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
5 1.1 fvdl *
6 1.1 fvdl * Copyright (c) 1997 Justin T. Gibbs.
7 1.3 fvdl * Copyright (c) 2002 Adaptec Inc.
8 1.1 fvdl * All rights reserved.
9 1.1 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1 fvdl * modification, are permitted provided that the following conditions
12 1.1 fvdl * are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions, and the following disclaimer,
15 1.1 fvdl * without modification.
16 1.3 fvdl * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 1.3 fvdl * substantially similar to the "NO WARRANTY" disclaimer below
18 1.3 fvdl * ("Disclaimer") and any redistribution must be conditioned upon
19 1.3 fvdl * including a substantially similar Disclaimer requirement for further
20 1.3 fvdl * binary redistribution.
21 1.3 fvdl * 3. Neither the names of the above-listed copyright holders nor the names
22 1.3 fvdl * of any contributors may be used to endorse or promote products derived
23 1.3 fvdl * from this software without specific prior written permission.
24 1.1 fvdl *
25 1.3 fvdl * Alternatively, this software may be distributed under the terms of the
26 1.3 fvdl * GNU General Public License ("GPL") version 2 as published by the Free
27 1.3 fvdl * Software Foundation.
28 1.3 fvdl *
29 1.3 fvdl * NO WARRANTY
30 1.3 fvdl * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 1.3 fvdl * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 1.3 fvdl * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 1.3 fvdl * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 1.3 fvdl * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.3 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 1.3 fvdl * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 1.3 fvdl * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.3 fvdl * POSSIBILITY OF SUCH DAMAGES.
41 1.1 fvdl *
42 1.3 fvdl * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_symbol.c,v 1.23 2003/01/20 18:01:37 gibbs Exp $
43 1.1 fvdl */
44 1.1 fvdl
45 1.1 fvdl #include <sys/types.h>
46 1.1 fvdl
47 1.3 fvdl #ifdef __linux__
48 1.3 fvdl #include "aicdb.h"
49 1.3 fvdl #else
50 1.1 fvdl #include <db.h>
51 1.3 fvdl #endif
52 1.1 fvdl #include <fcntl.h>
53 1.3 fvdl #include <inttypes.h>
54 1.3 fvdl #include <regex.h>
55 1.1 fvdl #include <stdio.h>
56 1.1 fvdl #include <stdlib.h>
57 1.1 fvdl #include <string.h>
58 1.1 fvdl #include <sysexits.h>
59 1.1 fvdl
60 1.3 fvdl #include "aicasm_symbol.h"
61 1.1 fvdl #include "aicasm.h"
62 1.1 fvdl
63 1.1 fvdl static DB *symtable;
64 1.1 fvdl
65 1.1 fvdl symbol_t *
66 1.3 fvdl symbol_create(char *name)
67 1.1 fvdl {
68 1.1 fvdl symbol_t *new_symbol;
69 1.1 fvdl
70 1.1 fvdl new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
71 1.1 fvdl if (new_symbol == NULL) {
72 1.1 fvdl perror("Unable to create new symbol");
73 1.1 fvdl exit(EX_SOFTWARE);
74 1.1 fvdl }
75 1.1 fvdl memset(new_symbol, 0, sizeof(*new_symbol));
76 1.1 fvdl new_symbol->name = strdup(name);
77 1.3 fvdl if (new_symbol->name == NULL)
78 1.3 fvdl stop("Unable to strdup symbol name", EX_SOFTWARE);
79 1.1 fvdl new_symbol->type = UNINITIALIZED;
80 1.1 fvdl return (new_symbol);
81 1.1 fvdl }
82 1.1 fvdl
83 1.1 fvdl void
84 1.3 fvdl symbol_delete(symbol_t *symbol)
85 1.1 fvdl {
86 1.1 fvdl if (symtable != NULL) {
87 1.1 fvdl DBT key;
88 1.1 fvdl
89 1.1 fvdl key.data = symbol->name;
90 1.1 fvdl key.size = strlen(symbol->name);
91 1.1 fvdl symtable->del(symtable, &key, /*flags*/0);
92 1.1 fvdl }
93 1.1 fvdl switch(symbol->type) {
94 1.1 fvdl case SCBLOC:
95 1.1 fvdl case SRAMLOC:
96 1.1 fvdl case REGISTER:
97 1.1 fvdl if (symbol->info.rinfo != NULL)
98 1.1 fvdl free(symbol->info.rinfo);
99 1.1 fvdl break;
100 1.1 fvdl case ALIAS:
101 1.1 fvdl if (symbol->info.ainfo != NULL)
102 1.1 fvdl free(symbol->info.ainfo);
103 1.1 fvdl break;
104 1.1 fvdl case MASK:
105 1.3 fvdl case FIELD:
106 1.3 fvdl case ENUM:
107 1.3 fvdl case ENUM_ENTRY:
108 1.3 fvdl if (symbol->info.finfo != NULL) {
109 1.3 fvdl symlist_free(&symbol->info.finfo->symrefs);
110 1.3 fvdl free(symbol->info.finfo);
111 1.1 fvdl }
112 1.1 fvdl break;
113 1.1 fvdl case DOWNLOAD_CONST:
114 1.1 fvdl case CONST:
115 1.1 fvdl if (symbol->info.cinfo != NULL)
116 1.1 fvdl free(symbol->info.cinfo);
117 1.1 fvdl break;
118 1.1 fvdl case LABEL:
119 1.1 fvdl if (symbol->info.linfo != NULL)
120 1.1 fvdl free(symbol->info.linfo);
121 1.1 fvdl break;
122 1.1 fvdl case UNINITIALIZED:
123 1.1 fvdl default:
124 1.1 fvdl break;
125 1.1 fvdl }
126 1.1 fvdl free(symbol->name);
127 1.1 fvdl free(symbol);
128 1.1 fvdl }
129 1.1 fvdl
130 1.1 fvdl void
131 1.1 fvdl symtable_open()
132 1.1 fvdl {
133 1.1 fvdl symtable = dbopen(/*filename*/NULL,
134 1.1 fvdl O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
135 1.1 fvdl /*openinfo*/NULL);
136 1.1 fvdl
137 1.1 fvdl if (symtable == NULL) {
138 1.1 fvdl perror("Symbol table creation failed");
139 1.1 fvdl exit(EX_SOFTWARE);
140 1.1 fvdl /* NOTREACHED */
141 1.1 fvdl }
142 1.1 fvdl }
143 1.1 fvdl
144 1.1 fvdl void
145 1.1 fvdl symtable_close()
146 1.1 fvdl {
147 1.1 fvdl if (symtable != NULL) {
148 1.1 fvdl DBT key;
149 1.1 fvdl DBT data;
150 1.1 fvdl
151 1.1 fvdl while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
152 1.1 fvdl symbol_t *stored_ptr;
153 1.1 fvdl
154 1.1 fvdl memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
155 1.1 fvdl symbol_delete(stored_ptr);
156 1.1 fvdl }
157 1.1 fvdl symtable->close(symtable);
158 1.1 fvdl }
159 1.1 fvdl }
160 1.1 fvdl
161 1.1 fvdl /*
162 1.1 fvdl * The semantics of get is to return an uninitialized symbol entry
163 1.1 fvdl * if a lookup fails.
164 1.1 fvdl */
165 1.1 fvdl symbol_t *
166 1.3 fvdl symtable_get(char *name)
167 1.1 fvdl {
168 1.1 fvdl symbol_t *stored_ptr;
169 1.1 fvdl DBT key;
170 1.1 fvdl DBT data;
171 1.1 fvdl int retval;
172 1.1 fvdl
173 1.1 fvdl key.data = (void *)name;
174 1.1 fvdl key.size = strlen(name);
175 1.1 fvdl
176 1.1 fvdl if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
177 1.1 fvdl if (retval == -1) {
178 1.1 fvdl perror("Symbol table get operation failed");
179 1.1 fvdl exit(EX_SOFTWARE);
180 1.1 fvdl /* NOTREACHED */
181 1.1 fvdl } else if (retval == 1) {
182 1.1 fvdl /* Symbol wasn't found, so create a new one */
183 1.1 fvdl symbol_t *new_symbol;
184 1.1 fvdl
185 1.1 fvdl new_symbol = symbol_create(name);
186 1.1 fvdl data.data = &new_symbol;
187 1.1 fvdl data.size = sizeof(new_symbol);
188 1.1 fvdl if (symtable->put(symtable, &key, &data,
189 1.1 fvdl /*flags*/0) !=0) {
190 1.1 fvdl perror("Symtable put failed");
191 1.1 fvdl exit(EX_SOFTWARE);
192 1.1 fvdl }
193 1.1 fvdl return (new_symbol);
194 1.1 fvdl } else {
195 1.1 fvdl perror("Unexpected return value from db get routine");
196 1.1 fvdl exit(EX_SOFTWARE);
197 1.1 fvdl /* NOTREACHED */
198 1.1 fvdl }
199 1.1 fvdl }
200 1.1 fvdl memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
201 1.1 fvdl return (stored_ptr);
202 1.1 fvdl }
203 1.1 fvdl
204 1.1 fvdl symbol_node_t *
205 1.3 fvdl symlist_search(symlist_t *symlist, char *symname)
206 1.1 fvdl {
207 1.1 fvdl symbol_node_t *curnode;
208 1.1 fvdl
209 1.2 lukem curnode = SLIST_FIRST(symlist);
210 1.1 fvdl while(curnode != NULL) {
211 1.1 fvdl if (strcmp(symname, curnode->symbol->name) == 0)
212 1.1 fvdl break;
213 1.2 lukem curnode = SLIST_NEXT(curnode, links);
214 1.1 fvdl }
215 1.1 fvdl return (curnode);
216 1.1 fvdl }
217 1.1 fvdl
218 1.1 fvdl void
219 1.3 fvdl symlist_add(symlist_t *symlist, symbol_t *symbol, int how)
220 1.1 fvdl {
221 1.1 fvdl symbol_node_t *newnode;
222 1.1 fvdl
223 1.1 fvdl newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
224 1.1 fvdl if (newnode == NULL) {
225 1.1 fvdl stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
226 1.1 fvdl /* NOTREACHED */
227 1.1 fvdl }
228 1.1 fvdl newnode->symbol = symbol;
229 1.1 fvdl if (how == SYMLIST_SORT) {
230 1.1 fvdl symbol_node_t *curnode;
231 1.3 fvdl int field;
232 1.1 fvdl
233 1.3 fvdl field = FALSE;
234 1.1 fvdl switch(symbol->type) {
235 1.1 fvdl case REGISTER:
236 1.1 fvdl case SCBLOC:
237 1.1 fvdl case SRAMLOC:
238 1.1 fvdl break;
239 1.3 fvdl case FIELD:
240 1.1 fvdl case MASK:
241 1.3 fvdl case ENUM:
242 1.3 fvdl case ENUM_ENTRY:
243 1.3 fvdl field = TRUE;
244 1.1 fvdl break;
245 1.1 fvdl default:
246 1.1 fvdl stop("symlist_add: Invalid symbol type for sorting",
247 1.1 fvdl EX_SOFTWARE);
248 1.1 fvdl /* NOTREACHED */
249 1.1 fvdl }
250 1.1 fvdl
251 1.2 lukem curnode = SLIST_FIRST(symlist);
252 1.1 fvdl if (curnode == NULL
253 1.3 fvdl || (field
254 1.3 fvdl && (curnode->symbol->type > newnode->symbol->type
255 1.3 fvdl || (curnode->symbol->type == newnode->symbol->type
256 1.3 fvdl && (curnode->symbol->info.finfo->value >
257 1.3 fvdl newnode->symbol->info.finfo->value))))
258 1.3 fvdl || (!field && (curnode->symbol->info.rinfo->address >
259 1.1 fvdl newnode->symbol->info.rinfo->address))) {
260 1.1 fvdl SLIST_INSERT_HEAD(symlist, newnode, links);
261 1.1 fvdl return;
262 1.1 fvdl }
263 1.1 fvdl
264 1.1 fvdl while (1) {
265 1.2 lukem if (SLIST_NEXT(curnode, links) == NULL) {
266 1.1 fvdl SLIST_INSERT_AFTER(curnode, newnode,
267 1.1 fvdl links);
268 1.1 fvdl break;
269 1.1 fvdl } else {
270 1.1 fvdl symbol_t *cursymbol;
271 1.1 fvdl
272 1.2 lukem cursymbol = SLIST_NEXT(curnode, links)->symbol;
273 1.3 fvdl if ((field
274 1.3 fvdl && (cursymbol->type > symbol->type
275 1.3 fvdl || (cursymbol->type == symbol->type
276 1.3 fvdl && (cursymbol->info.finfo->value >
277 1.3 fvdl symbol->info.finfo->value))))
278 1.3 fvdl || (!field
279 1.3 fvdl && (cursymbol->info.rinfo->address >
280 1.3 fvdl symbol->info.rinfo->address))) {
281 1.1 fvdl SLIST_INSERT_AFTER(curnode, newnode,
282 1.1 fvdl links);
283 1.1 fvdl break;
284 1.1 fvdl }
285 1.1 fvdl }
286 1.2 lukem curnode = SLIST_NEXT(curnode, links);
287 1.1 fvdl }
288 1.1 fvdl } else {
289 1.1 fvdl SLIST_INSERT_HEAD(symlist, newnode, links);
290 1.1 fvdl }
291 1.1 fvdl }
292 1.1 fvdl
293 1.1 fvdl void
294 1.3 fvdl symlist_free(symlist_t *symlist)
295 1.1 fvdl {
296 1.1 fvdl symbol_node_t *node1, *node2;
297 1.1 fvdl
298 1.2 lukem node1 = SLIST_FIRST(symlist);
299 1.1 fvdl while (node1 != NULL) {
300 1.2 lukem node2 = SLIST_NEXT(node1, links);
301 1.1 fvdl free(node1);
302 1.1 fvdl node1 = node2;
303 1.1 fvdl }
304 1.1 fvdl SLIST_INIT(symlist);
305 1.1 fvdl }
306 1.1 fvdl
307 1.1 fvdl void
308 1.3 fvdl symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
309 1.3 fvdl symlist_t *symlist_src2)
310 1.1 fvdl {
311 1.1 fvdl symbol_node_t *node;
312 1.1 fvdl
313 1.1 fvdl *symlist_dest = *symlist_src1;
314 1.2 lukem while((node = SLIST_FIRST(symlist_src2)) != NULL) {
315 1.1 fvdl SLIST_REMOVE_HEAD(symlist_src2, links);
316 1.1 fvdl SLIST_INSERT_HEAD(symlist_dest, node, links);
317 1.1 fvdl }
318 1.1 fvdl
319 1.1 fvdl /* These are now empty */
320 1.1 fvdl SLIST_INIT(symlist_src1);
321 1.1 fvdl SLIST_INIT(symlist_src2);
322 1.1 fvdl }
323 1.1 fvdl
324 1.1 fvdl void
325 1.3 fvdl aic_print_file_prologue(FILE *ofile)
326 1.3 fvdl {
327 1.3 fvdl
328 1.3 fvdl if (ofile == NULL)
329 1.3 fvdl return;
330 1.3 fvdl
331 1.3 fvdl fprintf(ofile,
332 1.3 fvdl "/*\n"
333 1.3 fvdl " * DO NOT EDIT - This file is automatically generated\n"
334 1.3 fvdl " * from the following source files:\n"
335 1.3 fvdl " *\n"
336 1.3 fvdl "%s */\n",
337 1.3 fvdl versions);
338 1.3 fvdl }
339 1.3 fvdl
340 1.3 fvdl void
341 1.3 fvdl aic_print_include(FILE *dfile, char *include_file)
342 1.3 fvdl {
343 1.3 fvdl
344 1.3 fvdl if (dfile == NULL)
345 1.3 fvdl return;
346 1.3 fvdl fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
347 1.3 fvdl }
348 1.3 fvdl
349 1.3 fvdl void
350 1.3 fvdl aic_print_reg_dump_types(FILE *ofile)
351 1.3 fvdl {
352 1.3 fvdl if (ofile == NULL)
353 1.3 fvdl return;
354 1.3 fvdl
355 1.3 fvdl fprintf(ofile,
356 1.3 fvdl "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
357 1.3 fvdl "typedef struct %sreg_parse_entry {\n"
358 1.3 fvdl " char *name;\n"
359 1.3 fvdl " uint8_t value;\n"
360 1.3 fvdl " uint8_t mask;\n"
361 1.3 fvdl "} %sreg_parse_entry_t;\n"
362 1.3 fvdl "\n",
363 1.3 fvdl prefix, prefix, prefix);
364 1.3 fvdl }
365 1.3 fvdl
366 1.3 fvdl static void
367 1.3 fvdl aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
368 1.3 fvdl {
369 1.3 fvdl if (dfile == NULL)
370 1.3 fvdl return;
371 1.3 fvdl
372 1.3 fvdl fprintf(dfile,
373 1.3 fvdl "static %sreg_parse_entry_t %s_parse_table[] = {\n",
374 1.3 fvdl prefix,
375 1.3 fvdl regnode->symbol->name);
376 1.3 fvdl }
377 1.3 fvdl
378 1.3 fvdl static void
379 1.3 fvdl aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
380 1.3 fvdl symbol_node_t *regnode, u_int num_entries)
381 1.3 fvdl {
382 1.3 fvdl char *lower_name;
383 1.3 fvdl char *letter;
384 1.3 fvdl
385 1.3 fvdl lower_name = strdup(regnode->symbol->name);
386 1.3 fvdl if (lower_name == NULL)
387 1.3 fvdl stop("Unable to strdup symbol name", EX_SOFTWARE);
388 1.3 fvdl
389 1.3 fvdl for (letter = lower_name; *letter != '\0'; letter++)
390 1.3 fvdl *letter = tolower(*letter);
391 1.3 fvdl
392 1.3 fvdl if (dfile != NULL) {
393 1.3 fvdl if (num_entries != 0)
394 1.3 fvdl fprintf(dfile,
395 1.3 fvdl "\n"
396 1.3 fvdl "};\n"
397 1.3 fvdl "\n");
398 1.3 fvdl
399 1.3 fvdl fprintf(dfile,
400 1.3 fvdl "int\n"
401 1.3 fvdl "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
402 1.3 fvdl "{\n"
403 1.3 fvdl " return (%sprint_register(%s%s, %d, \"%s\",\n"
404 1.3 fvdl " 0x%02x, regvalue, cur_col, wrap));\n"
405 1.3 fvdl "}\n"
406 1.3 fvdl "\n",
407 1.3 fvdl prefix,
408 1.3 fvdl lower_name,
409 1.3 fvdl prefix,
410 1.3 fvdl num_entries != 0 ? regnode->symbol->name : "NULL",
411 1.3 fvdl num_entries != 0 ? "_parse_table" : "",
412 1.3 fvdl num_entries,
413 1.3 fvdl regnode->symbol->name,
414 1.3 fvdl regnode->symbol->info.rinfo->address);
415 1.3 fvdl }
416 1.3 fvdl
417 1.3 fvdl fprintf(ofile,
418 1.3 fvdl "#if AIC_DEBUG_REGISTERS\n"
419 1.3 fvdl "%sreg_print_t %s%s_print;\n"
420 1.3 fvdl "#else\n"
421 1.3 fvdl "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
422 1.3 fvdl " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
423 1.3 fvdl "#endif\n"
424 1.3 fvdl "\n",
425 1.3 fvdl prefix,
426 1.3 fvdl prefix,
427 1.3 fvdl lower_name,
428 1.3 fvdl prefix,
429 1.3 fvdl lower_name,
430 1.3 fvdl prefix,
431 1.3 fvdl regnode->symbol->name,
432 1.3 fvdl regnode->symbol->info.rinfo->address);
433 1.3 fvdl }
434 1.3 fvdl
435 1.3 fvdl static void
436 1.3 fvdl aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
437 1.3 fvdl {
438 1.3 fvdl int num_tabs;
439 1.3 fvdl
440 1.3 fvdl if (dfile == NULL)
441 1.3 fvdl return;
442 1.3 fvdl
443 1.3 fvdl fprintf(dfile,
444 1.3 fvdl " { \"%s\",",
445 1.3 fvdl curnode->symbol->name);
446 1.3 fvdl
447 1.3 fvdl num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
448 1.3 fvdl
449 1.3 fvdl while (num_tabs-- > 0)
450 1.3 fvdl fputc('\t', dfile);
451 1.3 fvdl fprintf(dfile, "0x%02x, 0x%02x }",
452 1.3 fvdl curnode->symbol->info.finfo->value,
453 1.3 fvdl curnode->symbol->info.finfo->mask);
454 1.3 fvdl }
455 1.3 fvdl
456 1.3 fvdl void
457 1.3 fvdl symtable_dump(FILE *ofile, FILE *dfile)
458 1.1 fvdl {
459 1.1 fvdl /*
460 1.1 fvdl * Sort the registers by address with a simple insertion sort.
461 1.1 fvdl * Put bitmasks next to the first register that defines them.
462 1.1 fvdl * Put constants at the end.
463 1.1 fvdl */
464 1.3 fvdl symlist_t registers;
465 1.3 fvdl symlist_t masks;
466 1.3 fvdl symlist_t constants;
467 1.3 fvdl symlist_t download_constants;
468 1.3 fvdl symlist_t aliases;
469 1.3 fvdl symlist_t exported_labels;
470 1.3 fvdl symbol_node_t *curnode;
471 1.3 fvdl symbol_node_t *regnode;
472 1.3 fvdl DBT key;
473 1.3 fvdl DBT data;
474 1.3 fvdl int flag;
475 1.3 fvdl u_int i;
476 1.3 fvdl
477 1.3 fvdl if (symtable == NULL)
478 1.3 fvdl return;
479 1.1 fvdl
480 1.1 fvdl SLIST_INIT(®isters);
481 1.1 fvdl SLIST_INIT(&masks);
482 1.1 fvdl SLIST_INIT(&constants);
483 1.1 fvdl SLIST_INIT(&download_constants);
484 1.1 fvdl SLIST_INIT(&aliases);
485 1.3 fvdl SLIST_INIT(&exported_labels);
486 1.3 fvdl flag = R_FIRST;
487 1.3 fvdl while (symtable->seq(symtable, &key, &data, flag) == 0) {
488 1.3 fvdl symbol_t *cursym;
489 1.1 fvdl
490 1.3 fvdl memcpy(&cursym, data.data, sizeof(cursym));
491 1.3 fvdl switch(cursym->type) {
492 1.3 fvdl case REGISTER:
493 1.3 fvdl case SCBLOC:
494 1.3 fvdl case SRAMLOC:
495 1.3 fvdl symlist_add(®isters, cursym, SYMLIST_SORT);
496 1.3 fvdl break;
497 1.3 fvdl case MASK:
498 1.3 fvdl case FIELD:
499 1.3 fvdl case ENUM:
500 1.3 fvdl case ENUM_ENTRY:
501 1.3 fvdl symlist_add(&masks, cursym, SYMLIST_SORT);
502 1.3 fvdl break;
503 1.3 fvdl case CONST:
504 1.3 fvdl symlist_add(&constants, cursym,
505 1.3 fvdl SYMLIST_INSERT_HEAD);
506 1.3 fvdl break;
507 1.3 fvdl case DOWNLOAD_CONST:
508 1.3 fvdl symlist_add(&download_constants, cursym,
509 1.3 fvdl SYMLIST_INSERT_HEAD);
510 1.3 fvdl break;
511 1.3 fvdl case ALIAS:
512 1.3 fvdl symlist_add(&aliases, cursym,
513 1.3 fvdl SYMLIST_INSERT_HEAD);
514 1.3 fvdl break;
515 1.3 fvdl case LABEL:
516 1.3 fvdl if (cursym->info.linfo->exported == 0)
517 1.3 fvdl break;
518 1.3 fvdl symlist_add(&exported_labels, cursym,
519 1.3 fvdl SYMLIST_INSERT_HEAD);
520 1.3 fvdl break;
521 1.3 fvdl default:
522 1.3 fvdl break;
523 1.3 fvdl }
524 1.3 fvdl flag = R_NEXT;
525 1.3 fvdl }
526 1.1 fvdl
527 1.3 fvdl /* Register dianostic functions/declarations first. */
528 1.3 fvdl aic_print_file_prologue(ofile);
529 1.3 fvdl aic_print_reg_dump_types(ofile);
530 1.3 fvdl aic_print_file_prologue(dfile);
531 1.3 fvdl aic_print_include(dfile, stock_include_file);
532 1.3 fvdl SLIST_FOREACH(curnode, ®isters, links) {
533 1.1 fvdl
534 1.3 fvdl switch(curnode->symbol->type) {
535 1.3 fvdl case REGISTER:
536 1.3 fvdl case SCBLOC:
537 1.3 fvdl case SRAMLOC:
538 1.3 fvdl {
539 1.3 fvdl symlist_t *fields;
540 1.3 fvdl symbol_node_t *fieldnode;
541 1.3 fvdl int num_entries;
542 1.3 fvdl
543 1.3 fvdl num_entries = 0;
544 1.3 fvdl fields = &curnode->symbol->info.rinfo->fields;
545 1.3 fvdl SLIST_FOREACH(fieldnode, fields, links) {
546 1.3 fvdl if (num_entries == 0)
547 1.3 fvdl aic_print_reg_dump_start(dfile,
548 1.3 fvdl curnode);
549 1.3 fvdl else if (dfile != NULL)
550 1.3 fvdl fputs(",\n", dfile);
551 1.3 fvdl num_entries++;
552 1.3 fvdl aic_print_reg_dump_entry(dfile, fieldnode);
553 1.1 fvdl }
554 1.3 fvdl aic_print_reg_dump_end(ofile, dfile,
555 1.3 fvdl curnode, num_entries);
556 1.3 fvdl }
557 1.3 fvdl default:
558 1.3 fvdl break;
559 1.1 fvdl }
560 1.3 fvdl }
561 1.3 fvdl
562 1.3 fvdl /* Fold in the masks and bits */
563 1.3 fvdl while (SLIST_FIRST(&masks) != NULL) {
564 1.3 fvdl char *regname;
565 1.3 fvdl
566 1.3 fvdl curnode = SLIST_FIRST(&masks);
567 1.3 fvdl SLIST_REMOVE_HEAD(&masks, links);
568 1.3 fvdl
569 1.3 fvdl regnode = SLIST_FIRST(&curnode->symbol->info.finfo->symrefs);
570 1.3 fvdl regname = regnode->symbol->name;
571 1.3 fvdl regnode = symlist_search(®isters, regname);
572 1.3 fvdl SLIST_INSERT_AFTER(regnode, curnode, links);
573 1.3 fvdl }
574 1.3 fvdl
575 1.3 fvdl /* Add the aliases */
576 1.3 fvdl while (SLIST_FIRST(&aliases) != NULL) {
577 1.3 fvdl char *regname;
578 1.3 fvdl
579 1.3 fvdl curnode = SLIST_FIRST(&aliases);
580 1.3 fvdl SLIST_REMOVE_HEAD(&aliases, links);
581 1.1 fvdl
582 1.3 fvdl regname = curnode->symbol->info.ainfo->parent->name;
583 1.3 fvdl regnode = symlist_search(®isters, regname);
584 1.3 fvdl SLIST_INSERT_AFTER(regnode, curnode, links);
585 1.3 fvdl }
586 1.3 fvdl
587 1.3 fvdl /* Output generated #defines. */
588 1.3 fvdl while (SLIST_FIRST(®isters) != NULL) {
589 1.3 fvdl symbol_node_t *curnode;
590 1.3 fvdl u_int value;
591 1.3 fvdl char *tab_str;
592 1.3 fvdl char *tab_str2;
593 1.3 fvdl
594 1.3 fvdl curnode = SLIST_FIRST(®isters);
595 1.3 fvdl SLIST_REMOVE_HEAD(®isters, links);
596 1.3 fvdl switch(curnode->symbol->type) {
597 1.3 fvdl case REGISTER:
598 1.3 fvdl case SCBLOC:
599 1.3 fvdl case SRAMLOC:
600 1.3 fvdl fprintf(ofile, "\n");
601 1.3 fvdl value = curnode->symbol->info.rinfo->address;
602 1.3 fvdl tab_str = "\t";
603 1.3 fvdl tab_str2 = "\t\t";
604 1.3 fvdl break;
605 1.3 fvdl case ALIAS:
606 1.3 fvdl {
607 1.3 fvdl symbol_t *parent;
608 1.3 fvdl
609 1.3 fvdl parent = curnode->symbol->info.ainfo->parent;
610 1.3 fvdl value = parent->info.rinfo->address;
611 1.3 fvdl tab_str = "\t";
612 1.3 fvdl tab_str2 = "\t\t";
613 1.3 fvdl break;
614 1.3 fvdl }
615 1.3 fvdl case MASK:
616 1.3 fvdl case FIELD:
617 1.3 fvdl case ENUM:
618 1.3 fvdl case ENUM_ENTRY:
619 1.3 fvdl value = curnode->symbol->info.finfo->value;
620 1.3 fvdl tab_str = "\t\t";
621 1.3 fvdl tab_str2 = "\t";
622 1.3 fvdl break;
623 1.3 fvdl default:
624 1.3 fvdl value = 0; /* Quiet compiler */
625 1.3 fvdl tab_str = NULL;
626 1.3 fvdl tab_str2 = NULL;
627 1.3 fvdl stop("symtable_dump: Invalid symbol type "
628 1.3 fvdl "encountered", EX_SOFTWARE);
629 1.3 fvdl break;
630 1.1 fvdl }
631 1.3 fvdl fprintf(ofile, "#define%s%-16s%s0x%02x\n",
632 1.3 fvdl tab_str, curnode->symbol->name, tab_str2,
633 1.3 fvdl value);
634 1.3 fvdl free(curnode);
635 1.3 fvdl }
636 1.3 fvdl fprintf(ofile, "\n\n");
637 1.3 fvdl
638 1.3 fvdl while (SLIST_FIRST(&constants) != NULL) {
639 1.3 fvdl symbol_node_t *curnode;
640 1.1 fvdl
641 1.3 fvdl curnode = SLIST_FIRST(&constants);
642 1.3 fvdl SLIST_REMOVE_HEAD(&constants, links);
643 1.3 fvdl fprintf(ofile, "#define\t%-8s\t0x%02x\n",
644 1.3 fvdl curnode->symbol->name,
645 1.3 fvdl curnode->symbol->info.cinfo->value);
646 1.3 fvdl free(curnode);
647 1.3 fvdl }
648 1.3 fvdl
649 1.3 fvdl
650 1.3 fvdl fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
651 1.3 fvdl
652 1.3 fvdl for (i = 0; SLIST_FIRST(&download_constants) != NULL; i++) {
653 1.3 fvdl symbol_node_t *curnode;
654 1.3 fvdl
655 1.3 fvdl curnode = SLIST_FIRST(&download_constants);
656 1.3 fvdl SLIST_REMOVE_HEAD(&download_constants, links);
657 1.3 fvdl fprintf(ofile, "#define\t%-8s\t0x%02x\n",
658 1.3 fvdl curnode->symbol->name,
659 1.3 fvdl curnode->symbol->info.cinfo->value);
660 1.3 fvdl free(curnode);
661 1.3 fvdl }
662 1.3 fvdl fprintf(ofile, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i);
663 1.3 fvdl
664 1.3 fvdl fprintf(ofile, "\n\n/* Exported Labels */\n");
665 1.1 fvdl
666 1.3 fvdl while (SLIST_FIRST(&exported_labels) != NULL) {
667 1.3 fvdl symbol_node_t *curnode;
668 1.1 fvdl
669 1.3 fvdl curnode = SLIST_FIRST(&exported_labels);
670 1.3 fvdl SLIST_REMOVE_HEAD(&exported_labels, links);
671 1.3 fvdl fprintf(ofile, "#define\tLABEL_%-8s\t0x%02x\n",
672 1.3 fvdl curnode->symbol->name,
673 1.3 fvdl curnode->symbol->info.linfo->address);
674 1.3 fvdl free(curnode);
675 1.1 fvdl }
676 1.1 fvdl }
677 1.1 fvdl
678