2 * Aic7xxx SCSI host adapter firmware assembler 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 if (curnode
->symbol
->dont_generate_debug_code
)
545 switch(curnode
->symbol
->type
) {
551 symbol_node_t
*fieldnode
;
556 if (curnode
->symbol
->count
== 1)
558 fields
= &curnode
->symbol
->info
.rinfo
->fields
;
559 SLIST_FOREACH(fieldnode
, fields
, links
) {
560 if (num_entries
== 0)
561 aic_print_reg_dump_start(dfile
,
563 else if (dfile
!= NULL
)
566 aic_print_reg_dump_entry(dfile
, fieldnode
);
568 aic_print_reg_dump_end(ofile
, dfile
,
569 curnode
, num_entries
);
576 fprintf(stderr
, "%s: %d of %d register definitions used\n", appname
,
577 reg_used
, reg_count
);
579 /* Fold in the masks and bits */
580 while (SLIST_FIRST(&masks
) != NULL
) {
583 curnode
= SLIST_FIRST(&masks
);
584 SLIST_REMOVE_HEAD(&masks
, links
);
586 regnode
= SLIST_FIRST(&curnode
->symbol
->info
.finfo
->symrefs
);
587 regname
= regnode
->symbol
->name
;
588 regnode
= symlist_search(®isters
, regname
);
589 SLIST_INSERT_AFTER(regnode
, curnode
, links
);
592 /* Add the aliases */
593 while (SLIST_FIRST(&aliases
) != NULL
) {
596 curnode
= SLIST_FIRST(&aliases
);
597 SLIST_REMOVE_HEAD(&aliases
, links
);
599 regname
= curnode
->symbol
->info
.ainfo
->parent
->name
;
600 regnode
= symlist_search(®isters
, regname
);
601 SLIST_INSERT_AFTER(regnode
, curnode
, links
);
604 /* Output generated #defines. */
605 while (SLIST_FIRST(®isters
) != NULL
) {
606 symbol_node_t
*curnode
;
611 curnode
= SLIST_FIRST(®isters
);
612 SLIST_REMOVE_HEAD(®isters
, links
);
613 switch(curnode
->symbol
->type
) {
617 fprintf(ofile
, "\n");
618 value
= curnode
->symbol
->info
.rinfo
->address
;
626 parent
= curnode
->symbol
->info
.ainfo
->parent
;
627 value
= parent
->info
.rinfo
->address
;
636 value
= curnode
->symbol
->info
.finfo
->value
;
641 value
= 0; /* Quiet compiler */
644 stop("symtable_dump: Invalid symbol type "
645 "encountered", EX_SOFTWARE
);
648 fprintf(ofile
, "#define%s%-16s%s0x%02x\n",
649 tab_str
, curnode
->symbol
->name
, tab_str2
,
653 fprintf(ofile
, "\n\n");
655 while (SLIST_FIRST(&constants
) != NULL
) {
656 symbol_node_t
*curnode
;
658 curnode
= SLIST_FIRST(&constants
);
659 SLIST_REMOVE_HEAD(&constants
, links
);
660 fprintf(ofile
, "#define\t%-8s\t0x%02x\n",
661 curnode
->symbol
->name
,
662 curnode
->symbol
->info
.cinfo
->value
);
666 fprintf(ofile
, "\n\n/* Downloaded Constant Definitions */\n");
668 for (i
= 0; SLIST_FIRST(&download_constants
) != NULL
; i
++) {
669 symbol_node_t
*curnode
;
671 curnode
= SLIST_FIRST(&download_constants
);
672 SLIST_REMOVE_HEAD(&download_constants
, links
);
673 fprintf(ofile
, "#define\t%-8s\t0x%02x\n",
674 curnode
->symbol
->name
,
675 curnode
->symbol
->info
.cinfo
->value
);
678 fprintf(ofile
, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i
);
680 fprintf(ofile
, "\n\n/* Exported Labels */\n");
682 while (SLIST_FIRST(&exported_labels
) != NULL
) {
683 symbol_node_t
*curnode
;
685 curnode
= SLIST_FIRST(&exported_labels
);
686 SLIST_REMOVE_HEAD(&exported_labels
, links
);
687 fprintf(ofile
, "#define\tLABEL_%-8s\t0x%02x\n",
688 curnode
->symbol
->name
,
689 curnode
->symbol
->info
.linfo
->address
);