No empty .Rs/.Re
[netbsd-mini2440.git] / sys / dev / microcode / aic7xxx / aicasm_symbol.c
blob45e981b5d95b940351cb9058a2f8294e7bef9d73
1 /* $NetBSD: aicasm_symbol.c,v 1.5 2005/12/11 12:22:18 christos Exp $ */
3 /*
4 * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
6 * Copyright (c) 1997 Justin T. Gibbs.
7 * Copyright (c) 2002 Adaptec Inc.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
21 * 3. Neither the names of the above-listed copyright holders nor the names
22 * of any contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * Alternatively, this software may be distributed under the terms of the
26 * GNU General Public License ("GPL") version 2 as published by the Free
27 * Software Foundation.
29 * NO WARRANTY
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGES.
42 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_symbol.c,v 1.23 2003/01/20 18:01:37 gibbs Exp $
45 #include <sys/cdefs.h>
46 __RCSID("$NetBSD: aicasm_symbol.c,v 1.5 2005/12/11 12:22:18 christos Exp $");
48 #include <sys/types.h>
50 #ifdef __linux__
51 #include "aicdb.h"
52 #else
53 #include <db.h>
54 #endif
55 #include <fcntl.h>
56 #include <inttypes.h>
57 #include <regex.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <sysexits.h>
63 #include "aicasm_symbol.h"
64 #include "aicasm.h"
66 static DB *symtable;
68 symbol_t *
69 symbol_create(char *name)
71 symbol_t *new_symbol;
73 new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
74 if (new_symbol == NULL) {
75 perror("Unable to create new symbol");
76 exit(EX_SOFTWARE);
78 memset(new_symbol, 0, sizeof(*new_symbol));
79 new_symbol->name = strdup(name);
80 if (new_symbol->name == NULL)
81 stop("Unable to strdup symbol name", EX_SOFTWARE);
82 new_symbol->type = UNINITIALIZED;
83 return (new_symbol);
86 void
87 symbol_delete(symbol_t *symbol)
89 if (symtable != NULL) {
90 DBT key;
92 key.data = symbol->name;
93 key.size = strlen(symbol->name);
94 symtable->del(symtable, &key, /*flags*/0);
96 switch(symbol->type) {
97 case SCBLOC:
98 case SRAMLOC:
99 case REGISTER:
100 if (symbol->info.rinfo != NULL)
101 free(symbol->info.rinfo);
102 break;
103 case ALIAS:
104 if (symbol->info.ainfo != NULL)
105 free(symbol->info.ainfo);
106 break;
107 case MASK:
108 case FIELD:
109 case ENUM:
110 case ENUM_ENTRY:
111 if (symbol->info.finfo != NULL) {
112 symlist_free(&symbol->info.finfo->symrefs);
113 free(symbol->info.finfo);
115 break;
116 case DOWNLOAD_CONST:
117 case CONST:
118 if (symbol->info.cinfo != NULL)
119 free(symbol->info.cinfo);
120 break;
121 case LABEL:
122 if (symbol->info.linfo != NULL)
123 free(symbol->info.linfo);
124 break;
125 case UNINITIALIZED:
126 default:
127 break;
129 free(symbol->name);
130 free(symbol);
133 void
134 symtable_open(void)
136 symtable = dbopen(/*filename*/NULL,
137 O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
138 /*openinfo*/NULL);
140 if (symtable == NULL) {
141 perror("Symbol table creation failed");
142 exit(EX_SOFTWARE);
143 /* NOTREACHED */
147 void
148 symtable_close(void)
150 if (symtable != NULL) {
151 DBT key;
152 DBT data;
154 while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
155 symbol_t *stored_ptr;
157 memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
158 symbol_delete(stored_ptr);
160 symtable->close(symtable);
165 * The semantics of get is to return an uninitialized symbol entry
166 * if a lookup fails.
168 symbol_t *
169 symtable_get(char *name)
171 symbol_t *stored_ptr;
172 DBT key;
173 DBT data;
174 int retval;
176 key.data = (void *)name;
177 key.size = strlen(name);
179 if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
180 if (retval == -1) {
181 perror("Symbol table get operation failed");
182 exit(EX_SOFTWARE);
183 /* NOTREACHED */
184 } else if (retval == 1) {
185 /* Symbol wasn't found, so create a new one */
186 symbol_t *new_symbol;
188 new_symbol = symbol_create(name);
189 data.data = &new_symbol;
190 data.size = sizeof(new_symbol);
191 if (symtable->put(symtable, &key, &data,
192 /*flags*/0) !=0) {
193 perror("Symtable put failed");
194 exit(EX_SOFTWARE);
196 return (new_symbol);
197 } else {
198 perror("Unexpected return value from db get routine");
199 exit(EX_SOFTWARE);
200 /* NOTREACHED */
203 memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
204 return (stored_ptr);
207 symbol_node_t *
208 symlist_search(symlist_t *symlist, char *symname)
210 symbol_node_t *curnode;
212 curnode = SLIST_FIRST(symlist);
213 while(curnode != NULL) {
214 if (strcmp(symname, curnode->symbol->name) == 0)
215 break;
216 curnode = SLIST_NEXT(curnode, links);
218 return (curnode);
221 void
222 symlist_add(symlist_t *symlist, symbol_t *symbol, int how)
224 symbol_node_t *newnode;
226 newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
227 if (newnode == NULL) {
228 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
229 /* NOTREACHED */
231 newnode->symbol = symbol;
232 if (how == SYMLIST_SORT) {
233 symbol_node_t *curnode;
234 int field;
236 field = FALSE;
237 switch(symbol->type) {
238 case REGISTER:
239 case SCBLOC:
240 case SRAMLOC:
241 break;
242 case FIELD:
243 case MASK:
244 case ENUM:
245 case ENUM_ENTRY:
246 field = TRUE;
247 break;
248 default:
249 stop("symlist_add: Invalid symbol type for sorting",
250 EX_SOFTWARE);
251 /* NOTREACHED */
254 curnode = SLIST_FIRST(symlist);
255 if (curnode == NULL
256 || (field
257 && (curnode->symbol->type > newnode->symbol->type
258 || (curnode->symbol->type == newnode->symbol->type
259 && (curnode->symbol->info.finfo->value >
260 newnode->symbol->info.finfo->value))))
261 || (!field && (curnode->symbol->info.rinfo->address >
262 newnode->symbol->info.rinfo->address))) {
263 SLIST_INSERT_HEAD(symlist, newnode, links);
264 return;
267 while (1) {
268 if (SLIST_NEXT(curnode, links) == NULL) {
269 SLIST_INSERT_AFTER(curnode, newnode,
270 links);
271 break;
272 } else {
273 symbol_t *cursymbol;
275 cursymbol = SLIST_NEXT(curnode, links)->symbol;
276 if ((field
277 && (cursymbol->type > symbol->type
278 || (cursymbol->type == symbol->type
279 && (cursymbol->info.finfo->value >
280 symbol->info.finfo->value))))
281 || (!field
282 && (cursymbol->info.rinfo->address >
283 symbol->info.rinfo->address))) {
284 SLIST_INSERT_AFTER(curnode, newnode,
285 links);
286 break;
289 curnode = SLIST_NEXT(curnode, links);
291 } else {
292 SLIST_INSERT_HEAD(symlist, newnode, links);
296 void
297 symlist_free(symlist_t *symlist)
299 symbol_node_t *node1, *node2;
301 node1 = SLIST_FIRST(symlist);
302 while (node1 != NULL) {
303 node2 = SLIST_NEXT(node1, links);
304 free(node1);
305 node1 = node2;
307 SLIST_INIT(symlist);
310 void
311 symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
312 symlist_t *symlist_src2)
314 symbol_node_t *node;
316 *symlist_dest = *symlist_src1;
317 while((node = SLIST_FIRST(symlist_src2)) != NULL) {
318 SLIST_REMOVE_HEAD(symlist_src2, links);
319 SLIST_INSERT_HEAD(symlist_dest, node, links);
322 /* These are now empty */
323 SLIST_INIT(symlist_src1);
324 SLIST_INIT(symlist_src2);
327 void
328 aic_print_file_prologue(FILE *ofile)
331 if (ofile == NULL)
332 return;
334 fprintf(ofile,
335 "/*\n"
336 " * DO NOT EDIT - This file is automatically generated\n"
337 " * from the following source files:\n"
338 " *\n"
339 "%s */\n",
340 versions);
343 void
344 aic_print_include(FILE *dfile, char *include_file)
347 if (dfile == NULL)
348 return;
349 fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
352 void
353 aic_print_reg_dump_types(FILE *ofile)
355 if (ofile == NULL)
356 return;
358 fprintf(ofile,
359 "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
360 "typedef struct %sreg_parse_entry {\n"
361 " char *name;\n"
362 " uint8_t value;\n"
363 " uint8_t mask;\n"
364 "} %sreg_parse_entry_t;\n"
365 "\n",
366 prefix, prefix, prefix);
369 static void
370 aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
372 if (dfile == NULL)
373 return;
375 fprintf(dfile,
376 "static %sreg_parse_entry_t %s_parse_table[] = {\n",
377 prefix,
378 regnode->symbol->name);
381 static void
382 aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
383 symbol_node_t *regnode, u_int num_entries)
385 char *lower_name;
386 char *letter;
388 lower_name = strdup(regnode->symbol->name);
389 if (lower_name == NULL)
390 stop("Unable to strdup symbol name", EX_SOFTWARE);
392 for (letter = lower_name; *letter != '\0'; letter++)
393 *letter = tolower(*letter);
395 if (dfile != NULL) {
396 if (num_entries != 0)
397 fprintf(dfile,
398 "\n"
399 "};\n"
400 "\n");
402 fprintf(dfile,
403 "int\n"
404 "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
405 "{\n"
406 " return (%sprint_register(%s%s, %d, \"%s\",\n"
407 " 0x%02x, regvalue, cur_col, wrap));\n"
408 "}\n"
409 "\n",
410 prefix,
411 lower_name,
412 prefix,
413 num_entries != 0 ? regnode->symbol->name : "NULL",
414 num_entries != 0 ? "_parse_table" : "",
415 num_entries,
416 regnode->symbol->name,
417 regnode->symbol->info.rinfo->address);
420 fprintf(ofile,
421 "#if AIC_DEBUG_REGISTERS\n"
422 "%sreg_print_t %s%s_print;\n"
423 "#else\n"
424 "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
425 " %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
426 "#endif\n"
427 "\n",
428 prefix,
429 prefix,
430 lower_name,
431 prefix,
432 lower_name,
433 prefix,
434 regnode->symbol->name,
435 regnode->symbol->info.rinfo->address);
438 static void
439 aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
441 int num_tabs;
443 if (dfile == NULL)
444 return;
446 fprintf(dfile,
447 " { \"%s\",",
448 curnode->symbol->name);
450 num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
452 while (num_tabs-- > 0)
453 fputc('\t', dfile);
454 fprintf(dfile, "0x%02x, 0x%02x }",
455 curnode->symbol->info.finfo->value,
456 curnode->symbol->info.finfo->mask);
459 void
460 symtable_dump(FILE *ofile, FILE *dfile)
463 * Sort the registers by address with a simple insertion sort.
464 * Put bitmasks next to the first register that defines them.
465 * Put constants at the end.
467 symlist_t registers;
468 symlist_t masks;
469 symlist_t constants;
470 symlist_t download_constants;
471 symlist_t aliases;
472 symlist_t exported_labels;
473 symbol_node_t *curnode;
474 symbol_node_t *regnode;
475 DBT key;
476 DBT data;
477 int flag;
478 u_int i;
480 if (symtable == NULL)
481 return;
483 SLIST_INIT(&registers);
484 SLIST_INIT(&masks);
485 SLIST_INIT(&constants);
486 SLIST_INIT(&download_constants);
487 SLIST_INIT(&aliases);
488 SLIST_INIT(&exported_labels);
489 flag = R_FIRST;
490 while (symtable->seq(symtable, &key, &data, flag) == 0) {
491 symbol_t *cursym;
493 memcpy(&cursym, data.data, sizeof(cursym));
494 switch(cursym->type) {
495 case REGISTER:
496 case SCBLOC:
497 case SRAMLOC:
498 symlist_add(&registers, cursym, SYMLIST_SORT);
499 break;
500 case MASK:
501 case FIELD:
502 case ENUM:
503 case ENUM_ENTRY:
504 symlist_add(&masks, cursym, SYMLIST_SORT);
505 break;
506 case CONST:
507 symlist_add(&constants, cursym,
508 SYMLIST_INSERT_HEAD);
509 break;
510 case DOWNLOAD_CONST:
511 symlist_add(&download_constants, cursym,
512 SYMLIST_INSERT_HEAD);
513 break;
514 case ALIAS:
515 symlist_add(&aliases, cursym,
516 SYMLIST_INSERT_HEAD);
517 break;
518 case LABEL:
519 if (cursym->info.linfo->exported == 0)
520 break;
521 symlist_add(&exported_labels, cursym,
522 SYMLIST_INSERT_HEAD);
523 break;
524 default:
525 break;
527 flag = R_NEXT;
530 /* Register dianostic functions/declarations first. */
531 aic_print_file_prologue(ofile);
532 aic_print_reg_dump_types(ofile);
533 aic_print_file_prologue(dfile);
534 aic_print_include(dfile, stock_include_file);
535 SLIST_FOREACH(curnode, &registers, links) {
537 switch(curnode->symbol->type) {
538 case REGISTER:
539 case SCBLOC:
540 case SRAMLOC:
542 symlist_t *fields;
543 symbol_node_t *fieldnode;
544 int num_entries;
546 num_entries = 0;
547 fields = &curnode->symbol->info.rinfo->fields;
548 SLIST_FOREACH(fieldnode, fields, links) {
549 if (num_entries == 0)
550 aic_print_reg_dump_start(dfile,
551 curnode);
552 else if (dfile != NULL)
553 fputs(",\n", dfile);
554 num_entries++;
555 aic_print_reg_dump_entry(dfile, fieldnode);
557 aic_print_reg_dump_end(ofile, dfile,
558 curnode, num_entries);
560 default:
561 break;
565 /* Fold in the masks and bits */
566 while (SLIST_FIRST(&masks) != NULL) {
567 char *regname;
569 curnode = SLIST_FIRST(&masks);
570 SLIST_REMOVE_HEAD(&masks, links);
572 regnode = SLIST_FIRST(&curnode->symbol->info.finfo->symrefs);
573 regname = regnode->symbol->name;
574 regnode = symlist_search(&registers, regname);
575 SLIST_INSERT_AFTER(regnode, curnode, links);
578 /* Add the aliases */
579 while (SLIST_FIRST(&aliases) != NULL) {
580 char *regname;
582 curnode = SLIST_FIRST(&aliases);
583 SLIST_REMOVE_HEAD(&aliases, links);
585 regname = curnode->symbol->info.ainfo->parent->name;
586 regnode = symlist_search(&registers, regname);
587 SLIST_INSERT_AFTER(regnode, curnode, links);
590 /* Output generated #defines. */
591 while (SLIST_FIRST(&registers) != NULL) {
592 symbol_node_t *curnode;
593 u_int value;
594 char *tab_str;
595 char *tab_str2;
597 curnode = SLIST_FIRST(&registers);
598 SLIST_REMOVE_HEAD(&registers, links);
599 switch(curnode->symbol->type) {
600 case REGISTER:
601 case SCBLOC:
602 case SRAMLOC:
603 fprintf(ofile, "\n");
604 value = curnode->symbol->info.rinfo->address;
605 tab_str = "\t";
606 tab_str2 = "\t\t";
607 break;
608 case ALIAS:
610 symbol_t *parent;
612 parent = curnode->symbol->info.ainfo->parent;
613 value = parent->info.rinfo->address;
614 tab_str = "\t";
615 tab_str2 = "\t\t";
616 break;
618 case MASK:
619 case FIELD:
620 case ENUM:
621 case ENUM_ENTRY:
622 value = curnode->symbol->info.finfo->value;
623 tab_str = "\t\t";
624 tab_str2 = "\t";
625 break;
626 default:
627 value = 0; /* Quiet compiler */
628 tab_str = NULL;
629 tab_str2 = NULL;
630 stop("symtable_dump: Invalid symbol type "
631 "encountered", EX_SOFTWARE);
632 break;
634 fprintf(ofile, "#define%s%-16s%s0x%02x\n",
635 tab_str, curnode->symbol->name, tab_str2,
636 value);
637 free(curnode);
639 fprintf(ofile, "\n\n");
641 while (SLIST_FIRST(&constants) != NULL) {
642 symbol_node_t *curnode;
644 curnode = SLIST_FIRST(&constants);
645 SLIST_REMOVE_HEAD(&constants, links);
646 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
647 curnode->symbol->name,
648 curnode->symbol->info.cinfo->value);
649 free(curnode);
653 fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
655 for (i = 0; SLIST_FIRST(&download_constants) != NULL; i++) {
656 symbol_node_t *curnode;
658 curnode = SLIST_FIRST(&download_constants);
659 SLIST_REMOVE_HEAD(&download_constants, links);
660 fprintf(ofile, "#define\t%-8s\t0x%02x\n",
661 curnode->symbol->name,
662 curnode->symbol->info.cinfo->value);
663 free(curnode);
665 fprintf(ofile, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i);
667 fprintf(ofile, "\n\n/* Exported Labels */\n");
669 while (SLIST_FIRST(&exported_labels) != NULL) {
670 symbol_node_t *curnode;
672 curnode = SLIST_FIRST(&exported_labels);
673 SLIST_REMOVE_HEAD(&exported_labels, links);
674 fprintf(ofile, "#define\tLABEL_%-8s\t0x%02x\n",
675 curnode->symbol->name,
676 curnode->symbol->info.linfo->address);
677 free(curnode);