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