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
;
84 symbol_delete(symbol_t
*symbol
)
86 if (symtable
!= NULL
) {
89 key
.data
= symbol
->name
;
90 key
.size
= strlen(symbol
->name
);
91 symtable
->del(symtable
, &key
, /*flags*/0);
93 switch(symbol
->type
) {
97 if (symbol
->info
.rinfo
!= NULL
)
98 free(symbol
->info
.rinfo
);
101 if (symbol
->info
.ainfo
!= NULL
)
102 free(symbol
->info
.ainfo
);
108 if (symbol
->info
.finfo
!= NULL
) {
109 symlist_free(&symbol
->info
.finfo
->symrefs
);
110 free(symbol
->info
.finfo
);
115 if (symbol
->info
.cinfo
!= NULL
)
116 free(symbol
->info
.cinfo
);
119 if (symbol
->info
.linfo
!= NULL
)
120 free(symbol
->info
.linfo
);
133 symtable
= dbopen(/*filename*/NULL
,
134 O_CREAT
| O_NONBLOCK
| O_RDWR
, /*mode*/0, DB_HASH
,
137 if (symtable
== NULL
) {
138 perror("Symbol table creation failed");
147 if (symtable
!= NULL
) {
151 while (symtable
->seq(symtable
, &key
, &data
, R_FIRST
) == 0) {
152 symbol_t
*stored_ptr
;
154 memcpy(&stored_ptr
, data
.data
, sizeof(stored_ptr
));
155 symbol_delete(stored_ptr
);
157 symtable
->close(symtable
);
162 * The semantics of get is to return an uninitialized symbol entry
166 symtable_get(char *name
)
168 symbol_t
*stored_ptr
;
173 key
.data
= (void *)name
;
174 key
.size
= strlen(name
);
176 if ((retval
= symtable
->get(symtable
, &key
, &data
, /*flags*/0)) != 0) {
178 perror("Symbol table get operation failed");
181 } else if (retval
== 1) {
182 /* Symbol wasn't found, so create a new one */
183 symbol_t
*new_symbol
;
185 new_symbol
= symbol_create(name
);
186 data
.data
= &new_symbol
;
187 data
.size
= sizeof(new_symbol
);
188 if (symtable
->put(symtable
, &key
, &data
,
190 perror("Symtable put failed");
195 perror("Unexpected return value from db get routine");
200 memcpy(&stored_ptr
, data
.data
, sizeof(stored_ptr
));
205 symlist_search(symlist_t
*symlist
, char *symname
)
207 symbol_node_t
*curnode
;
209 curnode
= SLIST_FIRST(symlist
);
210 while(curnode
!= NULL
) {
211 if (strcmp(symname
, curnode
->symbol
->name
) == 0)
213 curnode
= SLIST_NEXT(curnode
, links
);
219 symlist_add(symlist_t
*symlist
, symbol_t
*symbol
, int how
)
221 symbol_node_t
*newnode
;
223 newnode
= (symbol_node_t
*)malloc(sizeof(symbol_node_t
));
224 if (newnode
== NULL
) {
225 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE
);
228 newnode
->symbol
= symbol
;
229 if (how
== SYMLIST_SORT
) {
230 symbol_node_t
*curnode
;
234 switch(symbol
->type
) {
246 stop("symlist_add: Invalid symbol type for sorting",
251 curnode
= SLIST_FIRST(symlist
);
254 && (curnode
->symbol
->type
> newnode
->symbol
->type
255 || (curnode
->symbol
->type
== newnode
->symbol
->type
256 && (curnode
->symbol
->info
.finfo
->value
>
257 newnode
->symbol
->info
.finfo
->value
))))
258 || (!field
&& (curnode
->symbol
->info
.rinfo
->address
>
259 newnode
->symbol
->info
.rinfo
->address
))) {
260 SLIST_INSERT_HEAD(symlist
, newnode
, links
);
265 if (SLIST_NEXT(curnode
, links
) == NULL
) {
266 SLIST_INSERT_AFTER(curnode
, newnode
,
272 cursymbol
= SLIST_NEXT(curnode
, links
)->symbol
;
274 && (cursymbol
->type
> symbol
->type
275 || (cursymbol
->type
== symbol
->type
276 && (cursymbol
->info
.finfo
->value
>
277 symbol
->info
.finfo
->value
))))
279 && (cursymbol
->info
.rinfo
->address
>
280 symbol
->info
.rinfo
->address
))) {
281 SLIST_INSERT_AFTER(curnode
, newnode
,
286 curnode
= SLIST_NEXT(curnode
, links
);
289 SLIST_INSERT_HEAD(symlist
, newnode
, links
);
294 symlist_free(symlist_t
*symlist
)
296 symbol_node_t
*node1
, *node2
;
298 node1
= SLIST_FIRST(symlist
);
299 while (node1
!= NULL
) {
300 node2
= SLIST_NEXT(node1
, links
);
308 symlist_merge(symlist_t
*symlist_dest
, symlist_t
*symlist_src1
,
309 symlist_t
*symlist_src2
)
313 *symlist_dest
= *symlist_src1
;
314 while((node
= SLIST_FIRST(symlist_src2
)) != NULL
) {
315 SLIST_REMOVE_HEAD(symlist_src2
, links
);
316 SLIST_INSERT_HEAD(symlist_dest
, node
, links
);
319 /* These are now empty */
320 SLIST_INIT(symlist_src1
);
321 SLIST_INIT(symlist_src2
);
325 aic_print_file_prologue(FILE *ofile
)
333 " * DO NOT EDIT - This file is automatically generated\n"
334 " * from the following source files:\n"
341 aic_print_include(FILE *dfile
, char *include_file
)
346 fprintf(dfile
, "\n#include \"%s\"\n\n", include_file
);
350 aic_print_reg_dump_types(FILE *ofile
)
356 "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
357 "typedef struct %sreg_parse_entry {\n"
361 "} %sreg_parse_entry_t;\n"
363 prefix
, prefix
, prefix
);
367 aic_print_reg_dump_start(FILE *dfile
, symbol_node_t
*regnode
)
373 "static %sreg_parse_entry_t %s_parse_table[] = {\n",
375 regnode
->symbol
->name
);
379 aic_print_reg_dump_end(FILE *ofile
, FILE *dfile
,
380 symbol_node_t
*regnode
, u_int num_entries
)
385 lower_name
= strdup(regnode
->symbol
->name
);
386 if (lower_name
== NULL
)
387 stop("Unable to strdup symbol name", EX_SOFTWARE
);
389 for (letter
= lower_name
; *letter
!= '\0'; letter
++)
390 *letter
= tolower(*letter
);
393 if (num_entries
!= 0)
401 "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
403 " return (%sprint_register(%s%s, %d, \"%s\",\n"
404 " 0x%02x, regvalue, cur_col, wrap));\n"
410 num_entries
!= 0 ? regnode
->symbol
->name
: "NULL",
411 num_entries
!= 0 ? "_parse_table" : "",
413 regnode
->symbol
->name
,
414 regnode
->symbol
->info
.rinfo
->address
);
418 "#if AIC_DEBUG_REGISTERS\n"
419 "%sreg_print_t %s%s_print;\n"
421 "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
422 " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
431 regnode
->symbol
->name
,
432 regnode
->symbol
->info
.rinfo
->address
);
436 aic_print_reg_dump_entry(FILE *dfile
, symbol_node_t
*curnode
)
445 curnode
->symbol
->name
);
447 num_tabs
= 3 - (strlen(curnode
->symbol
->name
) + 5) / 8;
449 while (num_tabs
-- > 0)
451 fprintf(dfile
, "0x%02x, 0x%02x }",
452 curnode
->symbol
->info
.finfo
->value
,
453 curnode
->symbol
->info
.finfo
->mask
);
457 symtable_dump(FILE *ofile
, FILE *dfile
)
460 * Sort the registers by address with a simple insertion sort.
461 * Put bitmasks next to the first register that defines them.
462 * Put constants at the end.
467 symlist_t download_constants
;
469 symlist_t exported_labels
;
470 symbol_node_t
*curnode
;
471 symbol_node_t
*regnode
;
477 if (symtable
== NULL
)
480 SLIST_INIT(®isters
);
482 SLIST_INIT(&constants
);
483 SLIST_INIT(&download_constants
);
484 SLIST_INIT(&aliases
);
485 SLIST_INIT(&exported_labels
);
487 while (symtable
->seq(symtable
, &key
, &data
, flag
) == 0) {
490 memcpy(&cursym
, data
.data
, sizeof(cursym
));
491 switch(cursym
->type
) {
495 symlist_add(®isters
, cursym
, SYMLIST_SORT
);
501 symlist_add(&masks
, cursym
, SYMLIST_SORT
);
504 symlist_add(&constants
, cursym
,
505 SYMLIST_INSERT_HEAD
);
508 symlist_add(&download_constants
, cursym
,
509 SYMLIST_INSERT_HEAD
);
512 symlist_add(&aliases
, cursym
,
513 SYMLIST_INSERT_HEAD
);
516 if (cursym
->info
.linfo
->exported
== 0)
518 symlist_add(&exported_labels
, cursym
,
519 SYMLIST_INSERT_HEAD
);
527 /* Register dianostic functions/declarations first. */
528 aic_print_file_prologue(ofile
);
529 aic_print_reg_dump_types(ofile
);
530 aic_print_file_prologue(dfile
);
531 aic_print_include(dfile
, stock_include_file
);
532 SLIST_FOREACH(curnode
, ®isters
, links
) {
534 switch(curnode
->symbol
->type
) {
540 symbol_node_t
*fieldnode
;
544 fields
= &curnode
->symbol
->info
.rinfo
->fields
;
545 SLIST_FOREACH(fieldnode
, fields
, links
) {
546 if (num_entries
== 0)
547 aic_print_reg_dump_start(dfile
,
549 else if (dfile
!= NULL
)
552 aic_print_reg_dump_entry(dfile
, fieldnode
);
554 aic_print_reg_dump_end(ofile
, dfile
,
555 curnode
, num_entries
);
562 /* Fold in the masks and bits */
563 while (SLIST_FIRST(&masks
) != NULL
) {
566 curnode
= SLIST_FIRST(&masks
);
567 SLIST_REMOVE_HEAD(&masks
, links
);
569 regnode
= SLIST_FIRST(&curnode
->symbol
->info
.finfo
->symrefs
);
570 regname
= regnode
->symbol
->name
;
571 regnode
= symlist_search(®isters
, regname
);
572 SLIST_INSERT_AFTER(regnode
, curnode
, links
);
575 /* Add the aliases */
576 while (SLIST_FIRST(&aliases
) != NULL
) {
579 curnode
= SLIST_FIRST(&aliases
);
580 SLIST_REMOVE_HEAD(&aliases
, links
);
582 regname
= curnode
->symbol
->info
.ainfo
->parent
->name
;
583 regnode
= symlist_search(®isters
, regname
);
584 SLIST_INSERT_AFTER(regnode
, curnode
, links
);
587 /* Output generated #defines. */
588 while (SLIST_FIRST(®isters
) != NULL
) {
589 symbol_node_t
*curnode
;
594 curnode
= SLIST_FIRST(®isters
);
595 SLIST_REMOVE_HEAD(®isters
, links
);
596 switch(curnode
->symbol
->type
) {
600 fprintf(ofile
, "\n");
601 value
= curnode
->symbol
->info
.rinfo
->address
;
609 parent
= curnode
->symbol
->info
.ainfo
->parent
;
610 value
= parent
->info
.rinfo
->address
;
619 value
= curnode
->symbol
->info
.finfo
->value
;
624 value
= 0; /* Quiet compiler */
627 stop("symtable_dump: Invalid symbol type "
628 "encountered", EX_SOFTWARE
);
631 fprintf(ofile
, "#define%s%-16s%s0x%02x\n",
632 tab_str
, curnode
->symbol
->name
, tab_str2
,
636 fprintf(ofile
, "\n\n");
638 while (SLIST_FIRST(&constants
) != NULL
) {
639 symbol_node_t
*curnode
;
641 curnode
= SLIST_FIRST(&constants
);
642 SLIST_REMOVE_HEAD(&constants
, links
);
643 fprintf(ofile
, "#define\t%-8s\t0x%02x\n",
644 curnode
->symbol
->name
,
645 curnode
->symbol
->info
.cinfo
->value
);
650 fprintf(ofile
, "\n\n/* Downloaded Constant Definitions */\n");
652 for (i
= 0; SLIST_FIRST(&download_constants
) != NULL
; i
++) {
653 symbol_node_t
*curnode
;
655 curnode
= SLIST_FIRST(&download_constants
);
656 SLIST_REMOVE_HEAD(&download_constants
, links
);
657 fprintf(ofile
, "#define\t%-8s\t0x%02x\n",
658 curnode
->symbol
->name
,
659 curnode
->symbol
->info
.cinfo
->value
);
662 fprintf(ofile
, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i
);
664 fprintf(ofile
, "\n\n/* Exported Labels */\n");
666 while (SLIST_FIRST(&exported_labels
) != NULL
) {
667 symbol_node_t
*curnode
;
669 curnode
= SLIST_FIRST(&exported_labels
);
670 SLIST_REMOVE_HEAD(&exported_labels
, links
);
671 fprintf(ofile
, "#define\tLABEL_%-8s\t0x%02x\n",
672 curnode
->symbol
->name
,
673 curnode
->symbol
->info
.linfo
->address
);