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>
57 #include "aicasm_symbol.h"
63 symbol_create(char *name
)
67 new_symbol
= (symbol_t
*)malloc(sizeof(symbol_t
));
68 if (new_symbol
== NULL
) {
69 perror("Unable to create new symbol");
72 memset(new_symbol
, 0, sizeof(*new_symbol
));
73 new_symbol
->name
= strdup(name
);
74 if (new_symbol
->name
== NULL
)
75 stop("Unable to strdup symbol name", EX_SOFTWARE
);
76 new_symbol
->type
= UNINITIALIZED
;
77 new_symbol
->count
= 1;
82 symbol_delete(symbol_t
*symbol
)
84 if (symtable
!= NULL
) {
87 key
.data
= symbol
->name
;
88 key
.size
= strlen(symbol
->name
);
89 symtable
->del(symtable
, &key
, /*flags*/0);
91 switch(symbol
->type
) {
95 if (symbol
->info
.rinfo
!= NULL
)
96 free(symbol
->info
.rinfo
);
99 if (symbol
->info
.ainfo
!= NULL
)
100 free(symbol
->info
.ainfo
);
106 if (symbol
->info
.finfo
!= NULL
) {
107 symlist_free(&symbol
->info
.finfo
->symrefs
);
108 free(symbol
->info
.finfo
);
113 if (symbol
->info
.cinfo
!= NULL
)
114 free(symbol
->info
.cinfo
);
117 if (symbol
->info
.linfo
!= NULL
)
118 free(symbol
->info
.linfo
);
131 symtable
= dbopen(/*filename*/NULL
,
132 O_CREAT
| O_NONBLOCK
| O_RDWR
, /*mode*/0, DB_HASH
,
135 if (symtable
== NULL
) {
136 perror("Symbol table creation failed");
145 if (symtable
!= NULL
) {
149 while (symtable
->seq(symtable
, &key
, &data
, R_FIRST
) == 0) {
150 symbol_t
*stored_ptr
;
152 memcpy(&stored_ptr
, data
.data
, sizeof(stored_ptr
));
153 symbol_delete(stored_ptr
);
155 symtable
->close(symtable
);
160 * The semantics of get is to return an uninitialized symbol entry
164 symtable_get(char *name
)
166 symbol_t
*stored_ptr
;
171 key
.data
= (void *)name
;
172 key
.size
= strlen(name
);
174 if ((retval
= symtable
->get(symtable
, &key
, &data
, /*flags*/0)) != 0) {
176 perror("Symbol table get operation failed");
179 } else if (retval
== 1) {
180 /* Symbol wasn't found, so create a new one */
181 symbol_t
*new_symbol
;
183 new_symbol
= symbol_create(name
);
184 data
.data
= &new_symbol
;
185 data
.size
= sizeof(new_symbol
);
186 if (symtable
->put(symtable
, &key
, &data
,
188 perror("Symtable put failed");
193 perror("Unexpected return value from db get routine");
198 memcpy(&stored_ptr
, data
.data
, sizeof(stored_ptr
));
200 data
.data
= &stored_ptr
;
201 if (symtable
->put(symtable
, &key
, &data
, /*flags*/0) !=0) {
202 perror("Symtable put failed");
209 symlist_search(symlist_t
*symlist
, char *symname
)
211 symbol_node_t
*curnode
;
213 curnode
= SLIST_FIRST(symlist
);
214 while(curnode
!= NULL
) {
215 if (strcmp(symname
, curnode
->symbol
->name
) == 0)
217 curnode
= SLIST_NEXT(curnode
, links
);
223 symlist_add(symlist_t
*symlist
, symbol_t
*symbol
, int how
)
225 symbol_node_t
*newnode
;
227 newnode
= (symbol_node_t
*)malloc(sizeof(symbol_node_t
));
228 if (newnode
== NULL
) {
229 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE
);
232 newnode
->symbol
= symbol
;
233 if (how
== SYMLIST_SORT
) {
234 symbol_node_t
*curnode
;
238 switch(symbol
->type
) {
250 stop("symlist_add: Invalid symbol type for sorting",
255 curnode
= SLIST_FIRST(symlist
);
258 && (curnode
->symbol
->type
> newnode
->symbol
->type
259 || (curnode
->symbol
->type
== newnode
->symbol
->type
260 && (curnode
->symbol
->info
.finfo
->value
>
261 newnode
->symbol
->info
.finfo
->value
))))
262 || (!field
&& (curnode
->symbol
->info
.rinfo
->address
>
263 newnode
->symbol
->info
.rinfo
->address
))) {
264 SLIST_INSERT_HEAD(symlist
, newnode
, links
);
269 if (SLIST_NEXT(curnode
, links
) == NULL
) {
270 SLIST_INSERT_AFTER(curnode
, newnode
,
276 cursymbol
= SLIST_NEXT(curnode
, links
)->symbol
;
278 && (cursymbol
->type
> symbol
->type
279 || (cursymbol
->type
== symbol
->type
280 && (cursymbol
->info
.finfo
->value
>
281 symbol
->info
.finfo
->value
))))
283 && (cursymbol
->info
.rinfo
->address
>
284 symbol
->info
.rinfo
->address
))) {
285 SLIST_INSERT_AFTER(curnode
, newnode
,
290 curnode
= SLIST_NEXT(curnode
, links
);
293 SLIST_INSERT_HEAD(symlist
, newnode
, links
);
298 symlist_free(symlist_t
*symlist
)
300 symbol_node_t
*node1
, *node2
;
302 node1
= SLIST_FIRST(symlist
);
303 while (node1
!= NULL
) {
304 node2
= SLIST_NEXT(node1
, links
);
312 symlist_merge(symlist_t
*symlist_dest
, symlist_t
*symlist_src1
,
313 symlist_t
*symlist_src2
)
317 *symlist_dest
= *symlist_src1
;
318 while((node
= SLIST_FIRST(symlist_src2
)) != NULL
) {
319 SLIST_REMOVE_HEAD(symlist_src2
, links
);
320 SLIST_INSERT_HEAD(symlist_dest
, node
, links
);
323 /* These are now empty */
324 SLIST_INIT(symlist_src1
);
325 SLIST_INIT(symlist_src2
);
329 aic_print_file_prologue(FILE *ofile
)
337 " * DO NOT EDIT - This file is automatically generated\n"
338 " * from the following source files:\n"
345 aic_print_include(FILE *dfile
, char *include_file
)
350 fprintf(dfile
, "\n#include \"%s\"\n\n", include_file
);
354 aic_print_reg_dump_types(FILE *ofile
)
360 "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
361 "typedef struct %sreg_parse_entry {\n"
365 "} %sreg_parse_entry_t;\n"
367 prefix
, prefix
, prefix
);
371 aic_print_reg_dump_start(FILE *dfile
, symbol_node_t
*regnode
)
377 "static const %sreg_parse_entry_t %s_parse_table[] = {\n",
379 regnode
->symbol
->name
);
383 aic_print_reg_dump_end(FILE *ofile
, FILE *dfile
,
384 symbol_node_t
*regnode
, u_int num_entries
)
389 lower_name
= strdup(regnode
->symbol
->name
);
390 if (lower_name
== NULL
)
391 stop("Unable to strdup symbol name", EX_SOFTWARE
);
393 for (letter
= lower_name
; *letter
!= '\0'; letter
++)
394 *letter
= tolower(*letter
);
397 if (num_entries
!= 0)
405 "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
407 " return (%sprint_register(%s%s, %d, \"%s\",\n"
408 " 0x%02x, regvalue, cur_col, wrap));\n"
414 num_entries
!= 0 ? regnode
->symbol
->name
: "NULL",
415 num_entries
!= 0 ? "_parse_table" : "",
417 regnode
->symbol
->name
,
418 regnode
->symbol
->info
.rinfo
->address
);
422 "#if AIC_DEBUG_REGISTERS\n"
423 "%sreg_print_t %s%s_print;\n"
425 "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
426 " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
435 regnode
->symbol
->name
,
436 regnode
->symbol
->info
.rinfo
->address
);
440 aic_print_reg_dump_entry(FILE *dfile
, symbol_node_t
*curnode
)
449 curnode
->symbol
->name
);
451 num_tabs
= 3 - (strlen(curnode
->symbol
->name
) + 5) / 8;
453 while (num_tabs
-- > 0)
455 fprintf(dfile
, "0x%02x, 0x%02x }",
456 curnode
->symbol
->info
.finfo
->value
,
457 curnode
->symbol
->info
.finfo
->mask
);
461 symtable_dump(FILE *ofile
, FILE *dfile
)
464 * Sort the registers by address with a simple insertion sort.
465 * Put bitmasks next to the first register that defines them.
466 * Put constants at the end.
471 symlist_t download_constants
;
473 symlist_t exported_labels
;
474 symbol_node_t
*curnode
;
475 symbol_node_t
*regnode
;
479 int reg_count
= 0, reg_used
= 0;
482 if (symtable
== NULL
)
485 SLIST_INIT(®isters
);
487 SLIST_INIT(&constants
);
488 SLIST_INIT(&download_constants
);
489 SLIST_INIT(&aliases
);
490 SLIST_INIT(&exported_labels
);
492 while (symtable
->seq(symtable
, &key
, &data
, flag
) == 0) {
495 memcpy(&cursym
, data
.data
, sizeof(cursym
));
496 switch(cursym
->type
) {
500 symlist_add(®isters
, cursym
, SYMLIST_SORT
);
506 symlist_add(&masks
, cursym
, SYMLIST_SORT
);
509 symlist_add(&constants
, cursym
,
510 SYMLIST_INSERT_HEAD
);
513 symlist_add(&download_constants
, cursym
,
514 SYMLIST_INSERT_HEAD
);
517 symlist_add(&aliases
, cursym
,
518 SYMLIST_INSERT_HEAD
);
521 if (cursym
->info
.linfo
->exported
== 0)
523 symlist_add(&exported_labels
, cursym
,
524 SYMLIST_INSERT_HEAD
);
532 /* Register dianostic functions/declarations first. */
533 aic_print_file_prologue(ofile
);
534 aic_print_reg_dump_types(ofile
);
535 aic_print_file_prologue(dfile
);
536 aic_print_include(dfile
, stock_include_file
);
537 SLIST_FOREACH(curnode
, ®isters
, links
) {
539 if (curnode
->symbol
->dont_generate_debug_code
)
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
);