[POWERPC] 4xx: Reorganize 4xx defconfigs
[linux/fpc-iii.git] / drivers / scsi / aic7xxx / aicasm / aicasm_symbol.c
blobf1f448dff569fef218055b4ecc6ad5aba0b70137
1 /*
2 * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * Copyright (c) 2002 Adaptec Inc.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
27 * NO WARRANTY
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 $
42 * $FreeBSD$
45 #include <sys/types.h>
47 #ifdef __linux__
48 #include "aicdb.h"
49 #else
50 #include <db.h>
51 #endif
52 #include <fcntl.h>
53 #include <inttypes.h>
54 #include <regex.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <sysexits.h>
60 #include "aicasm_symbol.h"
61 #include "aicasm.h"
63 static DB *symtable;
65 symbol_t *
66 symbol_create(char *name)
68 symbol_t *new_symbol;
70 new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
71 if (new_symbol == NULL) {
72 perror("Unable to create new symbol");
73 exit(EX_SOFTWARE);
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 return (new_symbol);
83 void
84 symbol_delete(symbol_t *symbol)
86 if (symtable != NULL) {
87 DBT key;
89 key.data = symbol->name;
90 key.size = strlen(symbol->name);
91 symtable->del(symtable, &key, /*flags*/0);
93 switch(symbol->type) {
94 case SCBLOC:
95 case SRAMLOC:
96 case REGISTER:
97 if (symbol->info.rinfo != NULL)
98 free(symbol->info.rinfo);
99 break;
100 case ALIAS:
101 if (symbol->info.ainfo != NULL)
102 free(symbol->info.ainfo);
103 break;
104 case MASK:
105 case FIELD:
106 case ENUM:
107 case ENUM_ENTRY:
108 if (symbol->info.finfo != NULL) {
109 symlist_free(&symbol->info.finfo->symrefs);
110 free(symbol->info.finfo);
112 break;
113 case DOWNLOAD_CONST:
114 case CONST:
115 if (symbol->info.cinfo != NULL)
116 free(symbol->info.cinfo);
117 break;
118 case LABEL:
119 if (symbol->info.linfo != NULL)
120 free(symbol->info.linfo);
121 break;
122 case UNINITIALIZED:
123 default:
124 break;
126 free(symbol->name);
127 free(symbol);
130 void
131 symtable_open()
133 symtable = dbopen(/*filename*/NULL,
134 O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
135 /*openinfo*/NULL);
137 if (symtable == NULL) {
138 perror("Symbol table creation failed");
139 exit(EX_SOFTWARE);
140 /* NOTREACHED */
144 void
145 symtable_close()
147 if (symtable != NULL) {
148 DBT key;
149 DBT data;
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
163 * if a lookup fails.
165 symbol_t *
166 symtable_get(char *name)
168 symbol_t *stored_ptr;
169 DBT key;
170 DBT data;
171 int retval;
173 key.data = (void *)name;
174 key.size = strlen(name);
176 if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
177 if (retval == -1) {
178 perror("Symbol table get operation failed");
179 exit(EX_SOFTWARE);
180 /* NOTREACHED */
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,
189 /*flags*/0) !=0) {
190 perror("Symtable put failed");
191 exit(EX_SOFTWARE);
193 return (new_symbol);
194 } else {
195 perror("Unexpected return value from db get routine");
196 exit(EX_SOFTWARE);
197 /* NOTREACHED */
200 memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
201 return (stored_ptr);
204 symbol_node_t *
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)
212 break;
213 curnode = SLIST_NEXT(curnode, links);
215 return (curnode);
218 void
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);
226 /* NOTREACHED */
228 newnode->symbol = symbol;
229 if (how == SYMLIST_SORT) {
230 symbol_node_t *curnode;
231 int field;
233 field = FALSE;
234 switch(symbol->type) {
235 case REGISTER:
236 case SCBLOC:
237 case SRAMLOC:
238 break;
239 case FIELD:
240 case MASK:
241 case ENUM:
242 case ENUM_ENTRY:
243 field = TRUE;
244 break;
245 default:
246 stop("symlist_add: Invalid symbol type for sorting",
247 EX_SOFTWARE);
248 /* NOTREACHED */
251 curnode = SLIST_FIRST(symlist);
252 if (curnode == NULL
253 || (field
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);
261 return;
264 while (1) {
265 if (SLIST_NEXT(curnode, links) == NULL) {
266 SLIST_INSERT_AFTER(curnode, newnode,
267 links);
268 break;
269 } else {
270 symbol_t *cursymbol;
272 cursymbol = SLIST_NEXT(curnode, links)->symbol;
273 if ((field
274 && (cursymbol->type > symbol->type
275 || (cursymbol->type == symbol->type
276 && (cursymbol->info.finfo->value >
277 symbol->info.finfo->value))))
278 || (!field
279 && (cursymbol->info.rinfo->address >
280 symbol->info.rinfo->address))) {
281 SLIST_INSERT_AFTER(curnode, newnode,
282 links);
283 break;
286 curnode = SLIST_NEXT(curnode, links);
288 } else {
289 SLIST_INSERT_HEAD(symlist, newnode, links);
293 void
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);
301 free(node1);
302 node1 = node2;
304 SLIST_INIT(symlist);
307 void
308 symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
309 symlist_t *symlist_src2)
311 symbol_node_t *node;
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);
324 void
325 aic_print_file_prologue(FILE *ofile)
328 if (ofile == NULL)
329 return;
331 fprintf(ofile,
332 "/*\n"
333 " * DO NOT EDIT - This file is automatically generated\n"
334 " * from the following source files:\n"
335 " *\n"
336 "%s */\n",
337 versions);
340 void
341 aic_print_include(FILE *dfile, char *include_file)
344 if (dfile == NULL)
345 return;
346 fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
349 void
350 aic_print_reg_dump_types(FILE *ofile)
352 if (ofile == NULL)
353 return;
355 fprintf(ofile,
356 "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
357 "typedef struct %sreg_parse_entry {\n"
358 " char *name;\n"
359 " uint8_t value;\n"
360 " uint8_t mask;\n"
361 "} %sreg_parse_entry_t;\n"
362 "\n",
363 prefix, prefix, prefix);
366 static void
367 aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
369 if (dfile == NULL)
370 return;
372 fprintf(dfile,
373 "static %sreg_parse_entry_t %s_parse_table[] = {\n",
374 prefix,
375 regnode->symbol->name);
378 static void
379 aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
380 symbol_node_t *regnode, u_int num_entries)
382 char *lower_name;
383 char *letter;
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);
392 if (dfile != NULL) {
393 if (num_entries != 0)
394 fprintf(dfile,
395 "\n"
396 "};\n"
397 "\n");
399 fprintf(dfile,
400 "int\n"
401 "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
402 "{\n"
403 " return (%sprint_register(%s%s, %d, \"%s\",\n"
404 " 0x%02x, regvalue, cur_col, wrap));\n"
405 "}\n"
406 "\n",
407 prefix,
408 lower_name,
409 prefix,
410 num_entries != 0 ? regnode->symbol->name : "NULL",
411 num_entries != 0 ? "_parse_table" : "",
412 num_entries,
413 regnode->symbol->name,
414 regnode->symbol->info.rinfo->address);
417 fprintf(ofile,
418 "#if AIC_DEBUG_REGISTERS\n"
419 "%sreg_print_t %s%s_print;\n"
420 "#else\n"
421 "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
422 " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
423 "#endif\n"
424 "\n",
425 prefix,
426 prefix,
427 lower_name,
428 prefix,
429 lower_name,
430 prefix,
431 regnode->symbol->name,
432 regnode->symbol->info.rinfo->address);
435 static void
436 aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
438 int num_tabs;
440 if (dfile == NULL)
441 return;
443 fprintf(dfile,
444 " { \"%s\",",
445 curnode->symbol->name);
447 num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
449 while (num_tabs-- > 0)
450 fputc('\t', dfile);
451 fprintf(dfile, "0x%02x, 0x%02x }",
452 curnode->symbol->info.finfo->value,
453 curnode->symbol->info.finfo->mask);
456 void
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.
464 symlist_t registers;
465 symlist_t masks;
466 symlist_t constants;
467 symlist_t download_constants;
468 symlist_t aliases;
469 symlist_t exported_labels;
470 symbol_node_t *curnode;
471 symbol_node_t *regnode;
472 DBT key;
473 DBT data;
474 int flag;
475 u_int i;
477 if (symtable == NULL)
478 return;
480 SLIST_INIT(&registers);
481 SLIST_INIT(&masks);
482 SLIST_INIT(&constants);
483 SLIST_INIT(&download_constants);
484 SLIST_INIT(&aliases);
485 SLIST_INIT(&exported_labels);
486 flag = R_FIRST;
487 while (symtable->seq(symtable, &key, &data, flag) == 0) {
488 symbol_t *cursym;
490 memcpy(&cursym, data.data, sizeof(cursym));
491 switch(cursym->type) {
492 case REGISTER:
493 case SCBLOC:
494 case SRAMLOC:
495 symlist_add(&registers, cursym, SYMLIST_SORT);
496 break;
497 case MASK:
498 case FIELD:
499 case ENUM:
500 case ENUM_ENTRY:
501 symlist_add(&masks, cursym, SYMLIST_SORT);
502 break;
503 case CONST:
504 symlist_add(&constants, cursym,
505 SYMLIST_INSERT_HEAD);
506 break;
507 case DOWNLOAD_CONST:
508 symlist_add(&download_constants, cursym,
509 SYMLIST_INSERT_HEAD);
510 break;
511 case ALIAS:
512 symlist_add(&aliases, cursym,
513 SYMLIST_INSERT_HEAD);
514 break;
515 case LABEL:
516 if (cursym->info.linfo->exported == 0)
517 break;
518 symlist_add(&exported_labels, cursym,
519 SYMLIST_INSERT_HEAD);
520 break;
521 default:
522 break;
524 flag = R_NEXT;
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, &registers, links) {
534 switch(curnode->symbol->type) {
535 case REGISTER:
536 case SCBLOC:
537 case SRAMLOC:
539 symlist_t *fields;
540 symbol_node_t *fieldnode;
541 int num_entries;
543 num_entries = 0;
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,
548 curnode);
549 else if (dfile != NULL)
550 fputs(",\n", dfile);
551 num_entries++;
552 aic_print_reg_dump_entry(dfile, fieldnode);
554 aic_print_reg_dump_end(ofile, dfile,
555 curnode, num_entries);
557 default:
558 break;
562 /* Fold in the masks and bits */
563 while (SLIST_FIRST(&masks) != NULL) {
564 char *regname;
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(&registers, regname);
572 SLIST_INSERT_AFTER(regnode, curnode, links);
575 /* Add the aliases */
576 while (SLIST_FIRST(&aliases) != NULL) {
577 char *regname;
579 curnode = SLIST_FIRST(&aliases);
580 SLIST_REMOVE_HEAD(&aliases, links);
582 regname = curnode->symbol->info.ainfo->parent->name;
583 regnode = symlist_search(&registers, regname);
584 SLIST_INSERT_AFTER(regnode, curnode, links);
587 /* Output generated #defines. */
588 while (SLIST_FIRST(&registers) != NULL) {
589 symbol_node_t *curnode;
590 u_int value;
591 char *tab_str;
592 char *tab_str2;
594 curnode = SLIST_FIRST(&registers);
595 SLIST_REMOVE_HEAD(&registers, links);
596 switch(curnode->symbol->type) {
597 case REGISTER:
598 case SCBLOC:
599 case SRAMLOC:
600 fprintf(ofile, "\n");
601 value = curnode->symbol->info.rinfo->address;
602 tab_str = "\t";
603 tab_str2 = "\t\t";
604 break;
605 case ALIAS:
607 symbol_t *parent;
609 parent = curnode->symbol->info.ainfo->parent;
610 value = parent->info.rinfo->address;
611 tab_str = "\t";
612 tab_str2 = "\t\t";
613 break;
615 case MASK:
616 case FIELD:
617 case ENUM:
618 case ENUM_ENTRY:
619 value = curnode->symbol->info.finfo->value;
620 tab_str = "\t\t";
621 tab_str2 = "\t";
622 break;
623 default:
624 value = 0; /* Quiet compiler */
625 tab_str = NULL;
626 tab_str2 = NULL;
627 stop("symtable_dump: Invalid symbol type "
628 "encountered", EX_SOFTWARE);
629 break;
631 fprintf(ofile, "#define%s%-16s%s0x%02x\n",
632 tab_str, curnode->symbol->name, tab_str2,
633 value);
634 free(curnode);
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);
646 free(curnode);
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);
660 free(curnode);
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);
674 free(curnode);