Deleted Added
full compact
aicasm_symbol.c (29897) aicasm_symbol.c (39220)
1/*
2 * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
1/*
2 * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
13 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
31 * $Id: aicasm_symbol.c,v 1.3 1997/09/03 03:44:44 gibbs Exp $
28 * $Id: aicasm_symbol.c,v 1.4 1997/09/27 19:37:30 gibbs Exp $
32 */
33
34
35#include <sys/types.h>
36
37#include <db.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <sysexits.h>
43
44#include "aicasm_symbol.h"
45#include "aicasm.h"
46
47static DB *symtable;
48
49symbol_t *
50symbol_create(name)
51 char *name;
52{
53 symbol_t *new_symbol;
54
55 new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
56 if (new_symbol == NULL) {
57 perror("Unable to create new symbol");
58 exit(EX_SOFTWARE);
59 }
60 memset(new_symbol, 0, sizeof(*new_symbol));
61 new_symbol->name = strdup(name);
62 new_symbol->type = UNINITIALIZED;
63 return (new_symbol);
64}
65
66void
67symbol_delete(symbol)
68 symbol_t *symbol;
69{
70 if (symtable != NULL) {
71 DBT key;
72
73 key.data = symbol->name;
74 key.size = strlen(symbol->name);
75 symtable->del(symtable, &key, /*flags*/0);
76 }
77 switch(symbol->type) {
78 case SCBLOC:
79 case SRAMLOC:
80 case REGISTER:
81 if (symbol->info.rinfo != NULL)
82 free(symbol->info.rinfo);
83 break;
84 case ALIAS:
85 if (symbol->info.ainfo != NULL)
86 free(symbol->info.ainfo);
87 break;
88 case MASK:
89 case BIT:
90 if (symbol->info.minfo != NULL) {
91 symlist_free(&symbol->info.minfo->symrefs);
92 free(symbol->info.minfo);
93 }
94 break;
95 case DOWNLOAD_CONST:
96 case CONST:
97 if (symbol->info.cinfo != NULL)
98 free(symbol->info.cinfo);
99 break;
100 case LABEL:
101 if (symbol->info.linfo != NULL)
102 free(symbol->info.linfo);
103 break;
104 case UNINITIALIZED:
105 default:
106 break;
107 }
108 free(symbol->name);
109 free(symbol);
110}
111
112void
113symtable_open()
114{
115 symtable = dbopen(/*filename*/NULL,
116 O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
117 /*openinfo*/NULL);
118
119 if (symtable == NULL) {
120 perror("Symbol table creation failed");
121 exit(EX_SOFTWARE);
122 /* NOTREACHED */
123 }
124}
125
126void
127symtable_close()
128{
129 if (symtable != NULL) {
130 DBT key;
131 DBT data;
132
133 while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
134 symbol_t *cursym;
135
136 cursym = *(symbol_t **)data.data;
137 symbol_delete(cursym);
138 }
139 symtable->close(symtable);
140 }
141}
142
143/*
144 * The semantics of get is to return an uninitialized symbol entry
145 * if a lookup fails.
146 */
147symbol_t *
148symtable_get(name)
149 char *name;
150{
151 DBT key;
152 DBT data;
153 int retval;
154
155 key.data = (void *)name;
156 key.size = strlen(name);
157
158 if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
159 if (retval == -1) {
160 perror("Symbol table get operation failed");
161 exit(EX_SOFTWARE);
162 /* NOTREACHED */
163 } else if (retval == 1) {
164 /* Symbol wasn't found, so create a new one */
165 symbol_t *new_symbol;
166
167 new_symbol = symbol_create(name);
168 data.data = &new_symbol;
169 data.size = sizeof(new_symbol);
170 if (symtable->put(symtable, &key, &data,
171 /*flags*/0) !=0) {
172 perror("Symtable put failed");
173 exit(EX_SOFTWARE);
174 }
175 return (new_symbol);
176 } else {
177 perror("Unexpected return value from db get routine");
178 exit(EX_SOFTWARE);
179 /* NOTREACHED */
180 }
181 }
182 return (*(symbol_t **)data.data);
183}
184
185symbol_node_t *
186symlist_search(symlist, symname)
187 symlist_t *symlist;
188 char *symname;
189{
190 symbol_node_t *curnode;
191
192 curnode = symlist->slh_first;
193 while(curnode != NULL) {
194 if (strcmp(symname, curnode->symbol->name) == 0)
195 break;
196 curnode = curnode->links.sle_next;
197 }
198 return (curnode);
199}
200
201void
202symlist_add(symlist, symbol, how)
203 symlist_t *symlist;
204 symbol_t *symbol;
205 int how;
206{
207 symbol_node_t *newnode;
208
209 newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
210 if (newnode == NULL) {
211 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
212 /* NOTREACHED */
213 }
214 newnode->symbol = symbol;
215 if (how == SYMLIST_SORT) {
216 symbol_node_t *curnode;
217 int mask;
218
219 mask = FALSE;
220 switch(symbol->type) {
221 case REGISTER:
222 case SCBLOC:
223 case SRAMLOC:
224 break;
225 case BIT:
226 case MASK:
227 mask = TRUE;
228 break;
229 default:
230 stop("symlist_add: Invalid symbol type for sorting",
231 EX_SOFTWARE);
232 /* NOTREACHED */
233 }
234
235 curnode = symlist->slh_first;
236 if (curnode == NULL
237 || (mask && (curnode->symbol->info.minfo->mask >
238 newnode->symbol->info.minfo->mask))
239 || (!mask && (curnode->symbol->info.rinfo->address >
240 newnode->symbol->info.rinfo->address))) {
241 SLIST_INSERT_HEAD(symlist, newnode, links);
242 return;
243 }
244
245 while (1) {
246 if (curnode->links.sle_next == NULL) {
247 SLIST_INSERT_AFTER(curnode, newnode,
248 links);
249 break;
250 } else {
251 symbol_t *cursymbol;
252
253 cursymbol = curnode->links.sle_next->symbol;
254 if ((mask && (cursymbol->info.minfo->mask >
255 symbol->info.minfo->mask))
256 || (!mask &&(cursymbol->info.rinfo->address >
257 symbol->info.rinfo->address))){
258 SLIST_INSERT_AFTER(curnode, newnode,
259 links);
260 break;
261 }
262 }
263 curnode = curnode->links.sle_next;
264 }
265 } else {
266 SLIST_INSERT_HEAD(symlist, newnode, links);
267 }
268}
269
270void
271symlist_free(symlist)
272 symlist_t *symlist;
273{
274 symbol_node_t *node1, *node2;
275
276 node1 = symlist->slh_first;
277 while (node1 != NULL) {
278 node2 = node1->links.sle_next;
279 free(node1);
280 node1 = node2;
281 }
282 SLIST_INIT(symlist);
283}
284
285void
286symlist_merge(symlist_dest, symlist_src1, symlist_src2)
287 symlist_t *symlist_dest;
288 symlist_t *symlist_src1;
289 symlist_t *symlist_src2;
290{
291 symbol_node_t *node;
292
293 *symlist_dest = *symlist_src1;
294 while((node = symlist_src2->slh_first) != NULL) {
295 SLIST_REMOVE_HEAD(symlist_src2, links);
296 SLIST_INSERT_HEAD(symlist_dest, node, links);
297 }
298
299 /* These are now empty */
300 SLIST_INIT(symlist_src1);
301 SLIST_INIT(symlist_src2);
302}
303
304void
305symtable_dump(ofile)
306 FILE *ofile;
307{
308 /*
309 * Sort the registers by address with a simple insertion sort.
310 * Put bitmasks next to the first register that defines them.
311 * Put constants at the end.
312 */
313 symlist_t registers;
314 symlist_t masks;
315 symlist_t constants;
316 symlist_t download_constants;
317 symlist_t aliases;
318
319 SLIST_INIT(&registers);
320 SLIST_INIT(&masks);
321 SLIST_INIT(&constants);
322 SLIST_INIT(&download_constants);
323 SLIST_INIT(&aliases);
324
325 if (symtable != NULL) {
326 DBT key;
327 DBT data;
328 int flag = R_FIRST;
329
330 while (symtable->seq(symtable, &key, &data, flag) == 0) {
331 symbol_t *cursym;
332
333 cursym = *(symbol_t **)data.data;
334 switch(cursym->type) {
335 case REGISTER:
336 case SCBLOC:
337 case SRAMLOC:
338 symlist_add(&registers, cursym, SYMLIST_SORT);
339 break;
340 case MASK:
341 case BIT:
342 symlist_add(&masks, cursym, SYMLIST_SORT);
343 break;
344 case CONST:
345 if (cursym->info.cinfo->define == FALSE) {
346 symlist_add(&constants, cursym,
347 SYMLIST_INSERT_HEAD);
348 }
349 break;
350 case DOWNLOAD_CONST:
351 symlist_add(&download_constants, cursym,
352 SYMLIST_INSERT_HEAD);
353 break;
354 case ALIAS:
355 symlist_add(&aliases, cursym,
356 SYMLIST_INSERT_HEAD);
357 break;
358 default:
359 break;
360 }
361 flag = R_NEXT;
362 }
363
364 /* Put in the masks and bits */
365 while (masks.slh_first != NULL) {
366 symbol_node_t *curnode;
367 symbol_node_t *regnode;
368 char *regname;
369
370 curnode = masks.slh_first;
371 SLIST_REMOVE_HEAD(&masks, links);
372
373 regnode =
374 curnode->symbol->info.minfo->symrefs.slh_first;
375 regname = regnode->symbol->name;
376 regnode = symlist_search(&registers, regname);
377 SLIST_INSERT_AFTER(regnode, curnode, links);
378 }
379
380 /* Add the aliases */
381 while (aliases.slh_first != NULL) {
382 symbol_node_t *curnode;
383 symbol_node_t *regnode;
384 char *regname;
385
386 curnode = aliases.slh_first;
387 SLIST_REMOVE_HEAD(&aliases, links);
388
389 regname = curnode->symbol->info.ainfo->parent->name;
390 regnode = symlist_search(&registers, regname);
391 SLIST_INSERT_AFTER(regnode, curnode, links);
392 }
393
394 /* Output what we have */
395 fprintf(ofile,
396"/*
397 * DO NOT EDIT - This file is automatically generated.
398 */\n");
399 while (registers.slh_first != NULL) {
400 symbol_node_t *curnode;
401 u_int8_t value;
402 char *tab_str;
403 char *tab_str2;
404
405 curnode = registers.slh_first;
406 SLIST_REMOVE_HEAD(&registers, links);
407 switch(curnode->symbol->type) {
408 case REGISTER:
409 case SCBLOC:
410 case SRAMLOC:
411 fprintf(ofile, "\n");
412 value = curnode->symbol->info.rinfo->address;
413 tab_str = "\t";
414 tab_str2 = "\t\t";
415 break;
416 case ALIAS:
417 {
418 symbol_t *parent;
419
420 parent = curnode->symbol->info.ainfo->parent;
421 value = parent->info.rinfo->address;
422 tab_str = "\t";
423 tab_str2 = "\t\t";
424 break;
425 }
426 case MASK:
427 case BIT:
428 value = curnode->symbol->info.minfo->mask;
429 tab_str = "\t\t";
430 tab_str2 = "\t";
431 break;
432 default:
433 value = 0; /* Quiet compiler */
434 tab_str = NULL;
435 tab_str2 = NULL;
436 stop("symtable_dump: Invalid symbol type "
437 "encountered", EX_SOFTWARE);
438 break;
439 }
440 fprintf(ofile, "#define%s%-16s%s0x%02x\n",
441 tab_str, curnode->symbol->name, tab_str2,
442 value);
443 free(curnode);
444 }
445 fprintf(ofile, "\n\n");
446
447 while (constants.slh_first != NULL) {
448 symbol_node_t *curnode;
449
450 curnode = constants.slh_first;
451 SLIST_REMOVE_HEAD(&constants, links);
452 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
453 curnode->symbol->name,
454 curnode->symbol->info.cinfo->value);
455 free(curnode);
456 }
457
458
459 fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
460
461 while (download_constants.slh_first != NULL) {
462 symbol_node_t *curnode;
463
464 curnode = download_constants.slh_first;
465 SLIST_REMOVE_HEAD(&download_constants, links);
466 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
467 curnode->symbol->name,
468 curnode->symbol->info.cinfo->value);
469 free(curnode);
470 }
471 }
472}
473
29 */
30
31
32#include <sys/types.h>
33
34#include <db.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <sysexits.h>
40
41#include "aicasm_symbol.h"
42#include "aicasm.h"
43
44static DB *symtable;
45
46symbol_t *
47symbol_create(name)
48 char *name;
49{
50 symbol_t *new_symbol;
51
52 new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
53 if (new_symbol == NULL) {
54 perror("Unable to create new symbol");
55 exit(EX_SOFTWARE);
56 }
57 memset(new_symbol, 0, sizeof(*new_symbol));
58 new_symbol->name = strdup(name);
59 new_symbol->type = UNINITIALIZED;
60 return (new_symbol);
61}
62
63void
64symbol_delete(symbol)
65 symbol_t *symbol;
66{
67 if (symtable != NULL) {
68 DBT key;
69
70 key.data = symbol->name;
71 key.size = strlen(symbol->name);
72 symtable->del(symtable, &key, /*flags*/0);
73 }
74 switch(symbol->type) {
75 case SCBLOC:
76 case SRAMLOC:
77 case REGISTER:
78 if (symbol->info.rinfo != NULL)
79 free(symbol->info.rinfo);
80 break;
81 case ALIAS:
82 if (symbol->info.ainfo != NULL)
83 free(symbol->info.ainfo);
84 break;
85 case MASK:
86 case BIT:
87 if (symbol->info.minfo != NULL) {
88 symlist_free(&symbol->info.minfo->symrefs);
89 free(symbol->info.minfo);
90 }
91 break;
92 case DOWNLOAD_CONST:
93 case CONST:
94 if (symbol->info.cinfo != NULL)
95 free(symbol->info.cinfo);
96 break;
97 case LABEL:
98 if (symbol->info.linfo != NULL)
99 free(symbol->info.linfo);
100 break;
101 case UNINITIALIZED:
102 default:
103 break;
104 }
105 free(symbol->name);
106 free(symbol);
107}
108
109void
110symtable_open()
111{
112 symtable = dbopen(/*filename*/NULL,
113 O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
114 /*openinfo*/NULL);
115
116 if (symtable == NULL) {
117 perror("Symbol table creation failed");
118 exit(EX_SOFTWARE);
119 /* NOTREACHED */
120 }
121}
122
123void
124symtable_close()
125{
126 if (symtable != NULL) {
127 DBT key;
128 DBT data;
129
130 while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
131 symbol_t *cursym;
132
133 cursym = *(symbol_t **)data.data;
134 symbol_delete(cursym);
135 }
136 symtable->close(symtable);
137 }
138}
139
140/*
141 * The semantics of get is to return an uninitialized symbol entry
142 * if a lookup fails.
143 */
144symbol_t *
145symtable_get(name)
146 char *name;
147{
148 DBT key;
149 DBT data;
150 int retval;
151
152 key.data = (void *)name;
153 key.size = strlen(name);
154
155 if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
156 if (retval == -1) {
157 perror("Symbol table get operation failed");
158 exit(EX_SOFTWARE);
159 /* NOTREACHED */
160 } else if (retval == 1) {
161 /* Symbol wasn't found, so create a new one */
162 symbol_t *new_symbol;
163
164 new_symbol = symbol_create(name);
165 data.data = &new_symbol;
166 data.size = sizeof(new_symbol);
167 if (symtable->put(symtable, &key, &data,
168 /*flags*/0) !=0) {
169 perror("Symtable put failed");
170 exit(EX_SOFTWARE);
171 }
172 return (new_symbol);
173 } else {
174 perror("Unexpected return value from db get routine");
175 exit(EX_SOFTWARE);
176 /* NOTREACHED */
177 }
178 }
179 return (*(symbol_t **)data.data);
180}
181
182symbol_node_t *
183symlist_search(symlist, symname)
184 symlist_t *symlist;
185 char *symname;
186{
187 symbol_node_t *curnode;
188
189 curnode = symlist->slh_first;
190 while(curnode != NULL) {
191 if (strcmp(symname, curnode->symbol->name) == 0)
192 break;
193 curnode = curnode->links.sle_next;
194 }
195 return (curnode);
196}
197
198void
199symlist_add(symlist, symbol, how)
200 symlist_t *symlist;
201 symbol_t *symbol;
202 int how;
203{
204 symbol_node_t *newnode;
205
206 newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
207 if (newnode == NULL) {
208 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
209 /* NOTREACHED */
210 }
211 newnode->symbol = symbol;
212 if (how == SYMLIST_SORT) {
213 symbol_node_t *curnode;
214 int mask;
215
216 mask = FALSE;
217 switch(symbol->type) {
218 case REGISTER:
219 case SCBLOC:
220 case SRAMLOC:
221 break;
222 case BIT:
223 case MASK:
224 mask = TRUE;
225 break;
226 default:
227 stop("symlist_add: Invalid symbol type for sorting",
228 EX_SOFTWARE);
229 /* NOTREACHED */
230 }
231
232 curnode = symlist->slh_first;
233 if (curnode == NULL
234 || (mask && (curnode->symbol->info.minfo->mask >
235 newnode->symbol->info.minfo->mask))
236 || (!mask && (curnode->symbol->info.rinfo->address >
237 newnode->symbol->info.rinfo->address))) {
238 SLIST_INSERT_HEAD(symlist, newnode, links);
239 return;
240 }
241
242 while (1) {
243 if (curnode->links.sle_next == NULL) {
244 SLIST_INSERT_AFTER(curnode, newnode,
245 links);
246 break;
247 } else {
248 symbol_t *cursymbol;
249
250 cursymbol = curnode->links.sle_next->symbol;
251 if ((mask && (cursymbol->info.minfo->mask >
252 symbol->info.minfo->mask))
253 || (!mask &&(cursymbol->info.rinfo->address >
254 symbol->info.rinfo->address))){
255 SLIST_INSERT_AFTER(curnode, newnode,
256 links);
257 break;
258 }
259 }
260 curnode = curnode->links.sle_next;
261 }
262 } else {
263 SLIST_INSERT_HEAD(symlist, newnode, links);
264 }
265}
266
267void
268symlist_free(symlist)
269 symlist_t *symlist;
270{
271 symbol_node_t *node1, *node2;
272
273 node1 = symlist->slh_first;
274 while (node1 != NULL) {
275 node2 = node1->links.sle_next;
276 free(node1);
277 node1 = node2;
278 }
279 SLIST_INIT(symlist);
280}
281
282void
283symlist_merge(symlist_dest, symlist_src1, symlist_src2)
284 symlist_t *symlist_dest;
285 symlist_t *symlist_src1;
286 symlist_t *symlist_src2;
287{
288 symbol_node_t *node;
289
290 *symlist_dest = *symlist_src1;
291 while((node = symlist_src2->slh_first) != NULL) {
292 SLIST_REMOVE_HEAD(symlist_src2, links);
293 SLIST_INSERT_HEAD(symlist_dest, node, links);
294 }
295
296 /* These are now empty */
297 SLIST_INIT(symlist_src1);
298 SLIST_INIT(symlist_src2);
299}
300
301void
302symtable_dump(ofile)
303 FILE *ofile;
304{
305 /*
306 * Sort the registers by address with a simple insertion sort.
307 * Put bitmasks next to the first register that defines them.
308 * Put constants at the end.
309 */
310 symlist_t registers;
311 symlist_t masks;
312 symlist_t constants;
313 symlist_t download_constants;
314 symlist_t aliases;
315
316 SLIST_INIT(&registers);
317 SLIST_INIT(&masks);
318 SLIST_INIT(&constants);
319 SLIST_INIT(&download_constants);
320 SLIST_INIT(&aliases);
321
322 if (symtable != NULL) {
323 DBT key;
324 DBT data;
325 int flag = R_FIRST;
326
327 while (symtable->seq(symtable, &key, &data, flag) == 0) {
328 symbol_t *cursym;
329
330 cursym = *(symbol_t **)data.data;
331 switch(cursym->type) {
332 case REGISTER:
333 case SCBLOC:
334 case SRAMLOC:
335 symlist_add(&registers, cursym, SYMLIST_SORT);
336 break;
337 case MASK:
338 case BIT:
339 symlist_add(&masks, cursym, SYMLIST_SORT);
340 break;
341 case CONST:
342 if (cursym->info.cinfo->define == FALSE) {
343 symlist_add(&constants, cursym,
344 SYMLIST_INSERT_HEAD);
345 }
346 break;
347 case DOWNLOAD_CONST:
348 symlist_add(&download_constants, cursym,
349 SYMLIST_INSERT_HEAD);
350 break;
351 case ALIAS:
352 symlist_add(&aliases, cursym,
353 SYMLIST_INSERT_HEAD);
354 break;
355 default:
356 break;
357 }
358 flag = R_NEXT;
359 }
360
361 /* Put in the masks and bits */
362 while (masks.slh_first != NULL) {
363 symbol_node_t *curnode;
364 symbol_node_t *regnode;
365 char *regname;
366
367 curnode = masks.slh_first;
368 SLIST_REMOVE_HEAD(&masks, links);
369
370 regnode =
371 curnode->symbol->info.minfo->symrefs.slh_first;
372 regname = regnode->symbol->name;
373 regnode = symlist_search(&registers, regname);
374 SLIST_INSERT_AFTER(regnode, curnode, links);
375 }
376
377 /* Add the aliases */
378 while (aliases.slh_first != NULL) {
379 symbol_node_t *curnode;
380 symbol_node_t *regnode;
381 char *regname;
382
383 curnode = aliases.slh_first;
384 SLIST_REMOVE_HEAD(&aliases, links);
385
386 regname = curnode->symbol->info.ainfo->parent->name;
387 regnode = symlist_search(&registers, regname);
388 SLIST_INSERT_AFTER(regnode, curnode, links);
389 }
390
391 /* Output what we have */
392 fprintf(ofile,
393"/*
394 * DO NOT EDIT - This file is automatically generated.
395 */\n");
396 while (registers.slh_first != NULL) {
397 symbol_node_t *curnode;
398 u_int8_t value;
399 char *tab_str;
400 char *tab_str2;
401
402 curnode = registers.slh_first;
403 SLIST_REMOVE_HEAD(&registers, links);
404 switch(curnode->symbol->type) {
405 case REGISTER:
406 case SCBLOC:
407 case SRAMLOC:
408 fprintf(ofile, "\n");
409 value = curnode->symbol->info.rinfo->address;
410 tab_str = "\t";
411 tab_str2 = "\t\t";
412 break;
413 case ALIAS:
414 {
415 symbol_t *parent;
416
417 parent = curnode->symbol->info.ainfo->parent;
418 value = parent->info.rinfo->address;
419 tab_str = "\t";
420 tab_str2 = "\t\t";
421 break;
422 }
423 case MASK:
424 case BIT:
425 value = curnode->symbol->info.minfo->mask;
426 tab_str = "\t\t";
427 tab_str2 = "\t";
428 break;
429 default:
430 value = 0; /* Quiet compiler */
431 tab_str = NULL;
432 tab_str2 = NULL;
433 stop("symtable_dump: Invalid symbol type "
434 "encountered", EX_SOFTWARE);
435 break;
436 }
437 fprintf(ofile, "#define%s%-16s%s0x%02x\n",
438 tab_str, curnode->symbol->name, tab_str2,
439 value);
440 free(curnode);
441 }
442 fprintf(ofile, "\n\n");
443
444 while (constants.slh_first != NULL) {
445 symbol_node_t *curnode;
446
447 curnode = constants.slh_first;
448 SLIST_REMOVE_HEAD(&constants, links);
449 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
450 curnode->symbol->name,
451 curnode->symbol->info.cinfo->value);
452 free(curnode);
453 }
454
455
456 fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
457
458 while (download_constants.slh_first != NULL) {
459 symbol_node_t *curnode;
460
461 curnode = download_constants.slh_first;
462 SLIST_REMOVE_HEAD(&download_constants, links);
463 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
464 curnode->symbol->name,
465 curnode->symbol->info.cinfo->value);
466 free(curnode);
467 }
468 }
469}
470