xref: /freebsd/sys/dev/aic7xxx/aicasm/aicasm_symbol.c (revision 39beb93c)
1 /*-
2  * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation
3  *
4  * Copyright (c) 1997 Justin T. Gibbs.
5  * Copyright (c) 2002 Adaptec Inc.
6  * All rights reserved.
7  *
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.
22  *
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.
26  *
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.
39  *
40  * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_symbol.c#24 $
41  *
42  * $FreeBSD$
43  */
44 
45 #include <sys/types.h>
46 
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>
59 
60 #include "aicasm_symbol.h"
61 #include "aicasm.h"
62 
63 static DB *symtable;
64 
65 symbol_t *
66 symbol_create(char *name)
67 {
68 	symbol_t *new_symbol;
69 
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);
74 	}
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);
81 }
82 
83 void
84 symbol_delete(symbol_t *symbol)
85 {
86 	if (symtable != NULL) {
87 		DBT	 key;
88 
89 		key.data = symbol->name;
90 		key.size = strlen(symbol->name);
91 		symtable->del(symtable, &key, /*flags*/0);
92 	}
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);
111 		}
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;
125 	}
126 	free(symbol->name);
127 	free(symbol);
128 }
129 
130 void
131 symtable_open()
132 {
133 	symtable = dbopen(/*filename*/NULL,
134 			  O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
135 			  /*openinfo*/NULL);
136 
137 	if (symtable == NULL) {
138 		perror("Symbol table creation failed");
139 		exit(EX_SOFTWARE);
140 		/* NOTREACHED */
141 	}
142 }
143 
144 void
145 symtable_close()
146 {
147 	if (symtable != NULL) {
148 		DBT	 key;
149 		DBT	 data;
150 
151 		while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
152 			symbol_t *stored_ptr;
153 
154 			memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
155 			symbol_delete(stored_ptr);
156 		}
157 		symtable->close(symtable);
158 	}
159 }
160 
161 /*
162  * The semantics of get is to return an uninitialized symbol entry
163  * if a lookup fails.
164  */
165 symbol_t *
166 symtable_get(char *name)
167 {
168 	symbol_t *stored_ptr;
169 	DBT	  key;
170 	DBT	  data;
171 	int	  retval;
172 
173 	key.data = (void *)name;
174 	key.size = strlen(name);
175 
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;
184 
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);
192 			}
193 			return (new_symbol);
194 		} else {
195 			perror("Unexpected return value from db get routine");
196 			exit(EX_SOFTWARE);
197 			/* NOTREACHED */
198 		}
199 	}
200 	memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
201 	return (stored_ptr);
202 }
203 
204 symbol_node_t *
205 symlist_search(symlist_t *symlist, char *symname)
206 {
207 	symbol_node_t *curnode;
208 
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);
214 	}
215 	return (curnode);
216 }
217 
218 void
219 symlist_add(symlist_t *symlist, symbol_t *symbol, int how)
220 {
221 	symbol_node_t *newnode;
222 
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 */
227 	}
228 	newnode->symbol = symbol;
229 	if (how == SYMLIST_SORT) {
230 		symbol_node_t *curnode;
231 		int field;
232 
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 */
249 		}
250 
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;
262 		}
263 
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;
271 
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;
284 				}
285 			}
286 			curnode = SLIST_NEXT(curnode, links);
287 		}
288 	} else {
289 		SLIST_INSERT_HEAD(symlist, newnode, links);
290 	}
291 }
292 
293 void
294 symlist_free(symlist_t *symlist)
295 {
296 	symbol_node_t *node1, *node2;
297 
298 	node1 = SLIST_FIRST(symlist);
299 	while (node1 != NULL) {
300 		node2 = SLIST_NEXT(node1, links);
301 		free(node1);
302 		node1 = node2;
303 	}
304 	SLIST_INIT(symlist);
305 }
306 
307 void
308 symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
309 	      symlist_t *symlist_src2)
310 {
311 	symbol_node_t *node;
312 
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);
317 	}
318 
319 	/* These are now empty */
320 	SLIST_INIT(symlist_src1);
321 	SLIST_INIT(symlist_src2);
322 }
323 
324 void
325 aic_print_file_prologue(FILE *ofile)
326 {
327 
328 	if (ofile == NULL)
329 		return;
330 
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);
338 }
339 
340 void
341 aic_print_include(FILE *dfile, char *include_file)
342 {
343 
344 	if (dfile == NULL)
345 		return;
346 	fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
347 }
348 
349 void
350 aic_print_reg_dump_types(FILE *ofile)
351 {
352 	if (ofile == NULL)
353 		return;
354 
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);
364 }
365 
366 static void
367 aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
368 {
369 	if (dfile == NULL)
370 		return;
371 
372 	fprintf(dfile,
373 "static %sreg_parse_entry_t %s_parse_table[] = {\n",
374 		prefix,
375 		regnode->symbol->name);
376 }
377 
378 static void
379 aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
380 		       symbol_node_t *regnode, u_int num_entries)
381 {
382 	char *lower_name;
383 	char *letter;
384 
385 	lower_name = strdup(regnode->symbol->name);
386 	if (lower_name == NULL)
387 		 stop("Unable to strdup symbol name", EX_SOFTWARE);
388 
389 	for (letter = lower_name; *letter != '\0'; letter++)
390 		*letter = tolower(*letter);
391 
392 	if (dfile != NULL) {
393 		if (num_entries != 0)
394 			fprintf(dfile,
395 "\n"
396 "};\n"
397 "\n");
398 
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);
415 	}
416 
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);
433 }
434 
435 static void
436 aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
437 {
438 	int num_tabs;
439 
440 	if (dfile == NULL)
441 		return;
442 
443 	fprintf(dfile,
444 "	{ \"%s\",",
445 		curnode->symbol->name);
446 
447 	num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
448 
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);
454 }
455 
456 void
457 symtable_dump(FILE *ofile, FILE *dfile)
458 {
459 	/*
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.
463 	 */
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;
476 
477 	if (symtable == NULL)
478 		return;
479 
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;
489 
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;
523 		}
524 		flag = R_NEXT;
525 	}
526 
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) {
533 
534 		switch(curnode->symbol->type) {
535 		case REGISTER:
536 		case SCBLOC:
537 		case SRAMLOC:
538 		{
539 			symlist_t	*fields;
540 			symbol_node_t	*fieldnode;
541 			int		 num_entries;
542 
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);
553 			}
554 			aic_print_reg_dump_end(ofile, dfile,
555 					       curnode, num_entries);
556 		}
557 		default:
558 			break;
559 		}
560 	}
561 
562 	/* Fold in the masks and bits */
563 	while (SLIST_FIRST(&masks) != NULL) {
564 		char *regname;
565 
566 		curnode = SLIST_FIRST(&masks);
567 		SLIST_REMOVE_HEAD(&masks, links);
568 
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);
573 	}
574 
575 	/* Add the aliases */
576 	while (SLIST_FIRST(&aliases) != NULL) {
577 		char *regname;
578 
579 		curnode = SLIST_FIRST(&aliases);
580 		SLIST_REMOVE_HEAD(&aliases, links);
581 
582 		regname = curnode->symbol->info.ainfo->parent->name;
583 		regnode = symlist_search(&registers, regname);
584 		SLIST_INSERT_AFTER(regnode, curnode, links);
585 	}
586 
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;
593 
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:
606 		{
607 			symbol_t *parent;
608 
609 			parent = curnode->symbol->info.ainfo->parent;
610 			value = parent->info.rinfo->address;
611 			tab_str = "\t";
612 			tab_str2 = "\t\t";
613 			break;
614 		}
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;
630 		}
631 		fprintf(ofile, "#define%s%-16s%s0x%02x\n",
632 			tab_str, curnode->symbol->name, tab_str2,
633 			value);
634 		free(curnode);
635 	}
636 	fprintf(ofile, "\n\n");
637 
638 	while (SLIST_FIRST(&constants) != NULL) {
639 		symbol_node_t *curnode;
640 
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);
647 	}
648 
649 
650 	fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
651 
652 	for (i = 0; SLIST_FIRST(&download_constants) != NULL; i++) {
653 		symbol_node_t *curnode;
654 
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);
661 	}
662 	fprintf(ofile, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i);
663 
664 	fprintf(ofile, "\n\n/* Exported Labels */\n");
665 
666 	while (SLIST_FIRST(&exported_labels) != NULL) {
667 		symbol_node_t *curnode;
668 
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);
675 	}
676 }
677 
678