2 * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * Copyright (c) 2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification.
14 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
15 * substantially similar to the "NO WARRANTY" disclaimer below
16 * ("Disclaimer") and any redistribution must be conditioned upon
17 * including a substantially similar Disclaimer requirement for further
18 * binary redistribution.
19 * 3. Neither the names of the above-listed copyright holders nor the names
20 * of any contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * Alternatively, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2 as published by the Free
25 * Software Foundation.
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
31 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
37 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGES.
40 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_symbol.c#24 $
45 #include <sys/types.h>
60 #include "aicasm_symbol.h"
66 symbol_create(char *name)
70 new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
71 if (new_symbol == NULL) {
72 perror("Unable to create new symbol");
75 memset(new_symbol, 0, sizeof(*new_symbol));
76 new_symbol->name = strdup(name);
77 if (new_symbol->name == NULL)
78 stop("Unable to strdup symbol name", EX_SOFTWARE);
79 new_symbol->type = UNINITIALIZED;
80 new_symbol->count = 1;
85 symbol_delete(symbol_t *symbol)
87 if (symtable != NULL) {
90 key.data = symbol->name;
91 key.size = strlen(symbol->name);
92 symtable->del(symtable, &key, /*flags*/0);
94 switch(symbol->type) {
98 if (symbol->info.rinfo != NULL)
99 free(symbol->info.rinfo);
102 if (symbol->info.ainfo != NULL)
103 free(symbol->info.ainfo);
109 if (symbol->info.finfo != NULL) {
110 symlist_free(&symbol->info.finfo->symrefs);
111 free(symbol->info.finfo);
116 if (symbol->info.cinfo != NULL)
117 free(symbol->info.cinfo);
120 if (symbol->info.linfo != NULL)
121 free(symbol->info.linfo);
134 symtable = dbopen(/*filename*/NULL,
135 O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
138 if (symtable == NULL) {
139 perror("Symbol table creation failed");
148 if (symtable != NULL) {
152 while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
153 symbol_t *stored_ptr;
155 memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
156 symbol_delete(stored_ptr);
158 symtable->close(symtable);
163 * The semantics of get is to return an uninitialized symbol entry
167 symtable_get(char *name)
169 symbol_t *stored_ptr;
174 key.data = (void *)name;
175 key.size = strlen(name);
177 if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
179 perror("Symbol table get operation failed");
182 } else if (retval == 1) {
183 /* Symbol wasn't found, so create a new one */
184 symbol_t *new_symbol;
186 new_symbol = symbol_create(name);
187 data.data = &new_symbol;
188 data.size = sizeof(new_symbol);
189 if (symtable->put(symtable, &key, &data,
191 perror("Symtable put failed");
196 perror("Unexpected return value from db get routine");
201 memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
203 data.data = &stored_ptr;
204 if (symtable->put(symtable, &key, &data, /*flags*/0) !=0) {
205 perror("Symtable put failed");
212 symlist_search(symlist_t *symlist, char *symname)
214 symbol_node_t *curnode;
216 curnode = SLIST_FIRST(symlist);
217 while(curnode != NULL) {
218 if (strcmp(symname, curnode->symbol->name) == 0)
220 curnode = SLIST_NEXT(curnode, links);
226 symlist_add(symlist_t *symlist, symbol_t *symbol, int how)
228 symbol_node_t *newnode;
230 newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
231 if (newnode == NULL) {
232 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
235 newnode->symbol = symbol;
236 if (how == SYMLIST_SORT) {
237 symbol_node_t *curnode;
241 switch(symbol->type) {
253 stop("symlist_add: Invalid symbol type for sorting",
258 curnode = SLIST_FIRST(symlist);
261 && (curnode->symbol->type > newnode->symbol->type
262 || (curnode->symbol->type == newnode->symbol->type
263 && (curnode->symbol->info.finfo->value >
264 newnode->symbol->info.finfo->value))))
265 || (!field && (curnode->symbol->info.rinfo->address >
266 newnode->symbol->info.rinfo->address))) {
267 SLIST_INSERT_HEAD(symlist, newnode, links);
272 if (SLIST_NEXT(curnode, links) == NULL) {
273 SLIST_INSERT_AFTER(curnode, newnode,
279 cursymbol = SLIST_NEXT(curnode, links)->symbol;
281 && (cursymbol->type > symbol->type
282 || (cursymbol->type == symbol->type
283 && (cursymbol->info.finfo->value >
284 symbol->info.finfo->value))))
286 && (cursymbol->info.rinfo->address >
287 symbol->info.rinfo->address))) {
288 SLIST_INSERT_AFTER(curnode, newnode,
293 curnode = SLIST_NEXT(curnode, links);
296 SLIST_INSERT_HEAD(symlist, newnode, links);
301 symlist_free(symlist_t *symlist)
303 symbol_node_t *node1, *node2;
305 node1 = SLIST_FIRST(symlist);
306 while (node1 != NULL) {
307 node2 = SLIST_NEXT(node1, links);
315 symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
316 symlist_t *symlist_src2)
320 *symlist_dest = *symlist_src1;
321 while((node = SLIST_FIRST(symlist_src2)) != NULL) {
322 SLIST_REMOVE_HEAD(symlist_src2, links);
323 SLIST_INSERT_HEAD(symlist_dest, node, links);
326 /* These are now empty */
327 SLIST_INIT(symlist_src1);
328 SLIST_INIT(symlist_src2);
332 aic_print_file_prologue(FILE *ofile)
340 " * DO NOT EDIT - This file is automatically generated\n"
341 " * from the following source files:\n"
348 aic_print_include(FILE *dfile, char *include_file)
353 fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
357 aic_print_reg_dump_types(FILE *ofile)
363 "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
364 "typedef struct %sreg_parse_entry {\n"
368 "} %sreg_parse_entry_t;\n"
370 prefix, prefix, prefix);
374 aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
380 "static const %sreg_parse_entry_t %s_parse_table[] = {\n",
382 regnode->symbol->name);
386 aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
387 symbol_node_t *regnode, u_int num_entries)
392 lower_name = strdup(regnode->symbol->name);
393 if (lower_name == NULL)
394 stop("Unable to strdup symbol name", EX_SOFTWARE);
396 for (letter = lower_name; *letter != '\0'; letter++)
397 *letter = tolower(*letter);
400 if (num_entries != 0)
408 "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
410 " return (%sprint_register(%s%s, %d, \"%s\",\n"
411 " 0x%02x, regvalue, cur_col, wrap));\n"
417 num_entries != 0 ? regnode->symbol->name : "NULL",
418 num_entries != 0 ? "_parse_table" : "",
420 regnode->symbol->name,
421 regnode->symbol->info.rinfo->address);
425 "#if AIC_DEBUG_REGISTERS\n"
426 "%sreg_print_t %s%s_print;\n"
428 "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
429 " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
438 regnode->symbol->name,
439 regnode->symbol->info.rinfo->address);
443 aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
452 curnode->symbol->name);
454 num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
456 while (num_tabs-- > 0)
458 fprintf(dfile, "0x%02x, 0x%02x }",
459 curnode->symbol->info.finfo->value,
460 curnode->symbol->info.finfo->mask);
464 symtable_dump(FILE *ofile, FILE *dfile)
467 * Sort the registers by address with a simple insertion sort.
468 * Put bitmasks next to the first register that defines them.
469 * Put constants at the end.
474 symlist_t download_constants;
476 symlist_t exported_labels;
477 symbol_node_t *curnode;
478 symbol_node_t *regnode;
482 int reg_count = 0, reg_used = 0;
485 if (symtable == NULL)
488 SLIST_INIT(®isters);
490 SLIST_INIT(&constants);
491 SLIST_INIT(&download_constants);
492 SLIST_INIT(&aliases);
493 SLIST_INIT(&exported_labels);
495 while (symtable->seq(symtable, &key, &data, flag) == 0) {
498 memcpy(&cursym, data.data, sizeof(cursym));
499 switch(cursym->type) {
503 symlist_add(®isters, cursym, SYMLIST_SORT);
509 symlist_add(&masks, cursym, SYMLIST_SORT);
512 symlist_add(&constants, cursym,
513 SYMLIST_INSERT_HEAD);
516 symlist_add(&download_constants, cursym,
517 SYMLIST_INSERT_HEAD);
520 symlist_add(&aliases, cursym,
521 SYMLIST_INSERT_HEAD);
524 if (cursym->info.linfo->exported == 0)
526 symlist_add(&exported_labels, cursym,
527 SYMLIST_INSERT_HEAD);
535 /* Register dianostic functions/declarations first. */
536 aic_print_file_prologue(ofile);
537 aic_print_reg_dump_types(ofile);
538 aic_print_file_prologue(dfile);
539 aic_print_include(dfile, stock_include_file);
540 SLIST_FOREACH(curnode, ®isters, links) {
542 switch(curnode->symbol->type) {
548 symbol_node_t *fieldnode;
553 if (curnode->symbol->count == 1)
555 fields = &curnode->symbol->info.rinfo->fields;
556 SLIST_FOREACH(fieldnode, fields, links) {
557 if (num_entries == 0)
558 aic_print_reg_dump_start(dfile,
560 else if (dfile != NULL)
563 aic_print_reg_dump_entry(dfile, fieldnode);
565 aic_print_reg_dump_end(ofile, dfile,
566 curnode, num_entries);
573 fprintf(stderr, "%s: %d of %d register definitions used\n", appname,
574 reg_used, reg_count);
576 /* Fold in the masks and bits */
577 while (SLIST_FIRST(&masks) != NULL) {
580 curnode = SLIST_FIRST(&masks);
581 SLIST_REMOVE_HEAD(&masks, links);
583 regnode = SLIST_FIRST(&curnode->symbol->info.finfo->symrefs);
584 regname = regnode->symbol->name;
585 regnode = symlist_search(®isters, regname);
586 SLIST_INSERT_AFTER(regnode, curnode, links);
589 /* Add the aliases */
590 while (SLIST_FIRST(&aliases) != NULL) {
593 curnode = SLIST_FIRST(&aliases);
594 SLIST_REMOVE_HEAD(&aliases, links);
596 regname = curnode->symbol->info.ainfo->parent->name;
597 regnode = symlist_search(®isters, regname);
598 SLIST_INSERT_AFTER(regnode, curnode, links);
601 /* Output generated #defines. */
602 while (SLIST_FIRST(®isters) != NULL) {
603 symbol_node_t *curnode;
608 curnode = SLIST_FIRST(®isters);
609 SLIST_REMOVE_HEAD(®isters, links);
610 switch(curnode->symbol->type) {
614 fprintf(ofile, "\n");
615 value = curnode->symbol->info.rinfo->address;
623 parent = curnode->symbol->info.ainfo->parent;
624 value = parent->info.rinfo->address;
633 value = curnode->symbol->info.finfo->value;
638 value = 0; /* Quiet compiler */
641 stop("symtable_dump: Invalid symbol type "
642 "encountered", EX_SOFTWARE);
645 fprintf(ofile, "#define%s%-16s%s0x%02x\n",
646 tab_str, curnode->symbol->name, tab_str2,
650 fprintf(ofile, "\n\n");
652 while (SLIST_FIRST(&constants) != NULL) {
653 symbol_node_t *curnode;
655 curnode = SLIST_FIRST(&constants);
656 SLIST_REMOVE_HEAD(&constants, links);
657 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
658 curnode->symbol->name,
659 curnode->symbol->info.cinfo->value);
663 fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
665 for (i = 0; SLIST_FIRST(&download_constants) != NULL; i++) {
666 symbol_node_t *curnode;
668 curnode = SLIST_FIRST(&download_constants);
669 SLIST_REMOVE_HEAD(&download_constants, links);
670 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
671 curnode->symbol->name,
672 curnode->symbol->info.cinfo->value);
675 fprintf(ofile, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i);
677 fprintf(ofile, "\n\n/* Exported Labels */\n");
679 while (SLIST_FIRST(&exported_labels) != NULL) {
680 symbol_node_t *curnode;
682 curnode = SLIST_FIRST(&exported_labels);
683 SLIST_REMOVE_HEAD(&exported_labels, links);
684 fprintf(ofile, "#define\tLABEL_%-8s\t0x%02x\n",
685 curnode->symbol->name,
686 curnode->symbol->info.linfo->address);