Home | History | Annotate | Line # | Download | only in aic7xxx
aicasm_symbol.c revision 1.5.74.1
      1  1.5.74.1   yamt /*	$NetBSD: aicasm_symbol.c,v 1.5.74.1 2009/05/04 08:12:51 yamt Exp $	*/
      2       1.1   fvdl 
      3       1.1   fvdl /*
      4       1.1   fvdl  * Aic7xxx SCSI host adapter firmware asssembler 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.5.74.1   yamt __RCSID("$NetBSD: aicasm_symbol.c,v 1.5.74.1 2009/05/04 08:12:51 yamt 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.1   fvdl 
     63       1.3   fvdl #include "aicasm_symbol.h"
     64       1.1   fvdl #include "aicasm.h"
     65       1.1   fvdl 
     66       1.1   fvdl static DB *symtable;
     67       1.1   fvdl 
     68       1.1   fvdl symbol_t *
     69       1.3   fvdl symbol_create(char *name)
     70       1.1   fvdl {
     71       1.1   fvdl 	symbol_t *new_symbol;
     72       1.1   fvdl 
     73       1.1   fvdl 	new_symbol = (symbol_t *)malloc(sizeof(symbol_t));
     74       1.1   fvdl 	if (new_symbol == NULL) {
     75       1.1   fvdl 		perror("Unable to create new symbol");
     76       1.1   fvdl 		exit(EX_SOFTWARE);
     77       1.1   fvdl 	}
     78       1.1   fvdl 	memset(new_symbol, 0, sizeof(*new_symbol));
     79       1.1   fvdl 	new_symbol->name = strdup(name);
     80       1.3   fvdl 	if (new_symbol->name == NULL)
     81       1.3   fvdl 		 stop("Unable to strdup symbol name", EX_SOFTWARE);
     82       1.1   fvdl 	new_symbol->type = UNINITIALIZED;
     83       1.1   fvdl 	return (new_symbol);
     84       1.1   fvdl }
     85       1.1   fvdl 
     86       1.1   fvdl void
     87       1.3   fvdl symbol_delete(symbol_t *symbol)
     88       1.1   fvdl {
     89       1.1   fvdl 	if (symtable != NULL) {
     90       1.1   fvdl 		DBT	 key;
     91       1.1   fvdl 
     92       1.1   fvdl 		key.data = symbol->name;
     93       1.1   fvdl 		key.size = strlen(symbol->name);
     94       1.1   fvdl 		symtable->del(symtable, &key, /*flags*/0);
     95       1.1   fvdl 	}
     96       1.1   fvdl 	switch(symbol->type) {
     97       1.1   fvdl 	case SCBLOC:
     98       1.1   fvdl 	case SRAMLOC:
     99       1.1   fvdl 	case REGISTER:
    100       1.1   fvdl 		if (symbol->info.rinfo != NULL)
    101       1.1   fvdl 			free(symbol->info.rinfo);
    102       1.1   fvdl 		break;
    103       1.1   fvdl 	case ALIAS:
    104       1.1   fvdl 		if (symbol->info.ainfo != NULL)
    105       1.1   fvdl 			free(symbol->info.ainfo);
    106       1.1   fvdl 		break;
    107       1.1   fvdl 	case MASK:
    108       1.3   fvdl 	case FIELD:
    109       1.3   fvdl 	case ENUM:
    110       1.3   fvdl 	case ENUM_ENTRY:
    111       1.3   fvdl 		if (symbol->info.finfo != NULL) {
    112       1.3   fvdl 			symlist_free(&symbol->info.finfo->symrefs);
    113       1.3   fvdl 			free(symbol->info.finfo);
    114       1.1   fvdl 		}
    115       1.1   fvdl 		break;
    116       1.1   fvdl 	case DOWNLOAD_CONST:
    117       1.1   fvdl 	case CONST:
    118       1.1   fvdl 		if (symbol->info.cinfo != NULL)
    119       1.1   fvdl 			free(symbol->info.cinfo);
    120       1.1   fvdl 		break;
    121       1.1   fvdl 	case LABEL:
    122       1.1   fvdl 		if (symbol->info.linfo != NULL)
    123       1.1   fvdl 			free(symbol->info.linfo);
    124       1.1   fvdl 		break;
    125       1.1   fvdl 	case UNINITIALIZED:
    126       1.1   fvdl 	default:
    127       1.1   fvdl 		break;
    128       1.1   fvdl 	}
    129       1.1   fvdl 	free(symbol->name);
    130       1.1   fvdl 	free(symbol);
    131       1.1   fvdl }
    132       1.1   fvdl 
    133       1.1   fvdl void
    134  1.5.74.1   yamt symtable_open(void)
    135       1.1   fvdl {
    136       1.1   fvdl 	symtable = dbopen(/*filename*/NULL,
    137       1.1   fvdl 			  O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH,
    138       1.1   fvdl 			  /*openinfo*/NULL);
    139       1.1   fvdl 
    140       1.1   fvdl 	if (symtable == NULL) {
    141       1.1   fvdl 		perror("Symbol table creation failed");
    142       1.1   fvdl 		exit(EX_SOFTWARE);
    143       1.1   fvdl 		/* NOTREACHED */
    144       1.1   fvdl 	}
    145       1.1   fvdl }
    146       1.1   fvdl 
    147       1.1   fvdl void
    148  1.5.74.1   yamt symtable_close(void)
    149       1.1   fvdl {
    150       1.1   fvdl 	if (symtable != NULL) {
    151       1.1   fvdl 		DBT	 key;
    152       1.1   fvdl 		DBT	 data;
    153       1.1   fvdl 
    154       1.1   fvdl 		while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) {
    155       1.1   fvdl 			symbol_t *stored_ptr;
    156       1.1   fvdl 
    157       1.1   fvdl 			memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
    158       1.1   fvdl 			symbol_delete(stored_ptr);
    159       1.1   fvdl 		}
    160       1.1   fvdl 		symtable->close(symtable);
    161       1.1   fvdl 	}
    162       1.1   fvdl }
    163       1.1   fvdl 
    164       1.1   fvdl /*
    165       1.1   fvdl  * The semantics of get is to return an uninitialized symbol entry
    166       1.1   fvdl  * if a lookup fails.
    167       1.1   fvdl  */
    168       1.1   fvdl symbol_t *
    169       1.3   fvdl symtable_get(char *name)
    170       1.1   fvdl {
    171       1.1   fvdl 	symbol_t *stored_ptr;
    172       1.1   fvdl 	DBT	  key;
    173       1.1   fvdl 	DBT	  data;
    174       1.1   fvdl 	int	  retval;
    175       1.1   fvdl 
    176       1.1   fvdl 	key.data = (void *)name;
    177       1.1   fvdl 	key.size = strlen(name);
    178       1.1   fvdl 
    179       1.1   fvdl 	if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) {
    180       1.1   fvdl 		if (retval == -1) {
    181       1.1   fvdl 			perror("Symbol table get operation failed");
    182       1.1   fvdl 			exit(EX_SOFTWARE);
    183       1.1   fvdl 			/* NOTREACHED */
    184       1.1   fvdl 		} else if (retval == 1) {
    185       1.1   fvdl 			/* Symbol wasn't found, so create a new one */
    186       1.1   fvdl 			symbol_t *new_symbol;
    187       1.1   fvdl 
    188       1.1   fvdl 			new_symbol = symbol_create(name);
    189       1.1   fvdl 			data.data = &new_symbol;
    190       1.1   fvdl 			data.size = sizeof(new_symbol);
    191       1.1   fvdl 			if (symtable->put(symtable, &key, &data,
    192       1.1   fvdl 					  /*flags*/0) !=0) {
    193       1.1   fvdl 				perror("Symtable put failed");
    194       1.1   fvdl 				exit(EX_SOFTWARE);
    195       1.1   fvdl 			}
    196       1.1   fvdl 			return (new_symbol);
    197       1.1   fvdl 		} else {
    198       1.1   fvdl 			perror("Unexpected return value from db get routine");
    199       1.1   fvdl 			exit(EX_SOFTWARE);
    200       1.1   fvdl 			/* NOTREACHED */
    201       1.1   fvdl 		}
    202       1.1   fvdl 	}
    203       1.1   fvdl 	memcpy(&stored_ptr, data.data, sizeof(stored_ptr));
    204       1.1   fvdl 	return (stored_ptr);
    205       1.1   fvdl }
    206       1.1   fvdl 
    207       1.1   fvdl symbol_node_t *
    208       1.3   fvdl symlist_search(symlist_t *symlist, char *symname)
    209       1.1   fvdl {
    210       1.1   fvdl 	symbol_node_t *curnode;
    211       1.1   fvdl 
    212       1.2  lukem 	curnode = SLIST_FIRST(symlist);
    213       1.1   fvdl 	while(curnode != NULL) {
    214       1.1   fvdl 		if (strcmp(symname, curnode->symbol->name) == 0)
    215       1.1   fvdl 			break;
    216       1.2  lukem 		curnode = SLIST_NEXT(curnode, links);
    217       1.1   fvdl 	}
    218       1.1   fvdl 	return (curnode);
    219       1.1   fvdl }
    220       1.1   fvdl 
    221       1.1   fvdl void
    222       1.3   fvdl symlist_add(symlist_t *symlist, symbol_t *symbol, int how)
    223       1.1   fvdl {
    224       1.1   fvdl 	symbol_node_t *newnode;
    225       1.1   fvdl 
    226       1.1   fvdl 	newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t));
    227       1.1   fvdl 	if (newnode == NULL) {
    228       1.1   fvdl 		stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE);
    229       1.1   fvdl 		/* NOTREACHED */
    230       1.1   fvdl 	}
    231       1.1   fvdl 	newnode->symbol = symbol;
    232       1.1   fvdl 	if (how == SYMLIST_SORT) {
    233       1.1   fvdl 		symbol_node_t *curnode;
    234       1.3   fvdl 		int field;
    235       1.1   fvdl 
    236       1.3   fvdl 		field = FALSE;
    237       1.1   fvdl 		switch(symbol->type) {
    238       1.1   fvdl 		case REGISTER:
    239       1.1   fvdl 		case SCBLOC:
    240       1.1   fvdl 		case SRAMLOC:
    241       1.1   fvdl 			break;
    242       1.3   fvdl 		case FIELD:
    243       1.1   fvdl 		case MASK:
    244       1.3   fvdl 		case ENUM:
    245       1.3   fvdl 		case ENUM_ENTRY:
    246       1.3   fvdl 			field = TRUE;
    247       1.1   fvdl 			break;
    248       1.1   fvdl 		default:
    249       1.1   fvdl 			stop("symlist_add: Invalid symbol type for sorting",
    250       1.1   fvdl 			     EX_SOFTWARE);
    251       1.1   fvdl 			/* NOTREACHED */
    252       1.1   fvdl 		}
    253       1.1   fvdl 
    254       1.2  lukem 		curnode = SLIST_FIRST(symlist);
    255       1.1   fvdl 		if (curnode == NULL
    256       1.3   fvdl 		 || (field
    257       1.3   fvdl 		  && (curnode->symbol->type > newnode->symbol->type
    258       1.3   fvdl 		   || (curnode->symbol->type == newnode->symbol->type
    259       1.3   fvdl 		    && (curnode->symbol->info.finfo->value >
    260       1.3   fvdl 			newnode->symbol->info.finfo->value))))
    261       1.3   fvdl 		 || (!field && (curnode->symbol->info.rinfo->address >
    262       1.1   fvdl 		               newnode->symbol->info.rinfo->address))) {
    263       1.1   fvdl 			SLIST_INSERT_HEAD(symlist, newnode, links);
    264       1.1   fvdl 			return;
    265       1.1   fvdl 		}
    266       1.1   fvdl 
    267       1.1   fvdl 		while (1) {
    268       1.2  lukem 			if (SLIST_NEXT(curnode, links) == NULL) {
    269       1.1   fvdl 				SLIST_INSERT_AFTER(curnode, newnode,
    270       1.1   fvdl 						   links);
    271       1.1   fvdl 				break;
    272       1.1   fvdl 			} else {
    273       1.1   fvdl 				symbol_t *cursymbol;
    274       1.1   fvdl 
    275       1.2  lukem 				cursymbol = SLIST_NEXT(curnode, links)->symbol;
    276       1.3   fvdl 				if ((field
    277       1.3   fvdl 		  		  && (cursymbol->type > symbol->type
    278       1.3   fvdl 				   || (cursymbol->type == symbol->type
    279       1.3   fvdl 				    && (cursymbol->info.finfo->value >
    280       1.3   fvdl 					symbol->info.finfo->value))))
    281       1.3   fvdl 				 || (!field
    282       1.3   fvdl 				   && (cursymbol->info.rinfo->address >
    283       1.3   fvdl 				       symbol->info.rinfo->address))) {
    284       1.1   fvdl 					SLIST_INSERT_AFTER(curnode, newnode,
    285       1.1   fvdl 							   links);
    286       1.1   fvdl 					break;
    287       1.1   fvdl 				}
    288       1.1   fvdl 			}
    289       1.2  lukem 			curnode = SLIST_NEXT(curnode, links);
    290       1.1   fvdl 		}
    291       1.1   fvdl 	} else {
    292       1.1   fvdl 		SLIST_INSERT_HEAD(symlist, newnode, links);
    293       1.1   fvdl 	}
    294       1.1   fvdl }
    295       1.1   fvdl 
    296       1.1   fvdl void
    297       1.3   fvdl symlist_free(symlist_t *symlist)
    298       1.1   fvdl {
    299       1.1   fvdl 	symbol_node_t *node1, *node2;
    300       1.1   fvdl 
    301       1.2  lukem 	node1 = SLIST_FIRST(symlist);
    302       1.1   fvdl 	while (node1 != NULL) {
    303       1.2  lukem 		node2 = SLIST_NEXT(node1, links);
    304       1.1   fvdl 		free(node1);
    305       1.1   fvdl 		node1 = node2;
    306       1.1   fvdl 	}
    307       1.1   fvdl 	SLIST_INIT(symlist);
    308       1.1   fvdl }
    309       1.1   fvdl 
    310       1.1   fvdl void
    311       1.3   fvdl symlist_merge(symlist_t *symlist_dest, symlist_t *symlist_src1,
    312       1.3   fvdl 	      symlist_t *symlist_src2)
    313       1.1   fvdl {
    314       1.1   fvdl 	symbol_node_t *node;
    315       1.1   fvdl 
    316       1.1   fvdl 	*symlist_dest = *symlist_src1;
    317       1.2  lukem 	while((node = SLIST_FIRST(symlist_src2)) != NULL) {
    318       1.1   fvdl 		SLIST_REMOVE_HEAD(symlist_src2, links);
    319       1.1   fvdl 		SLIST_INSERT_HEAD(symlist_dest, node, links);
    320       1.1   fvdl 	}
    321       1.1   fvdl 
    322       1.1   fvdl 	/* These are now empty */
    323       1.1   fvdl 	SLIST_INIT(symlist_src1);
    324       1.1   fvdl 	SLIST_INIT(symlist_src2);
    325       1.1   fvdl }
    326       1.1   fvdl 
    327       1.1   fvdl void
    328       1.3   fvdl aic_print_file_prologue(FILE *ofile)
    329       1.3   fvdl {
    330       1.3   fvdl 
    331       1.3   fvdl 	if (ofile == NULL)
    332       1.3   fvdl 		return;
    333       1.3   fvdl 
    334       1.3   fvdl 	fprintf(ofile,
    335       1.3   fvdl "/*\n"
    336       1.3   fvdl " * DO NOT EDIT - This file is automatically generated\n"
    337       1.3   fvdl " *		 from the following source files:\n"
    338       1.3   fvdl " *\n"
    339       1.3   fvdl "%s */\n",
    340       1.3   fvdl 		versions);
    341       1.3   fvdl }
    342       1.3   fvdl 
    343       1.3   fvdl void
    344       1.3   fvdl aic_print_include(FILE *dfile, char *include_file)
    345       1.3   fvdl {
    346       1.3   fvdl 
    347       1.3   fvdl 	if (dfile == NULL)
    348       1.3   fvdl 		return;
    349       1.3   fvdl 	fprintf(dfile, "\n#include \"%s\"\n\n", include_file);
    350       1.3   fvdl }
    351       1.3   fvdl 
    352       1.3   fvdl void
    353       1.3   fvdl aic_print_reg_dump_types(FILE *ofile)
    354       1.3   fvdl {
    355       1.3   fvdl 	if (ofile == NULL)
    356       1.3   fvdl 		return;
    357       1.3   fvdl 
    358       1.3   fvdl 	fprintf(ofile,
    359       1.3   fvdl "typedef int (%sreg_print_t)(u_int, u_int *, u_int);\n"
    360       1.3   fvdl "typedef struct %sreg_parse_entry {\n"
    361       1.3   fvdl "	char	*name;\n"
    362       1.3   fvdl "	uint8_t	 value;\n"
    363       1.3   fvdl "	uint8_t	 mask;\n"
    364       1.3   fvdl "} %sreg_parse_entry_t;\n"
    365       1.3   fvdl "\n",
    366       1.3   fvdl 		prefix, prefix, prefix);
    367       1.3   fvdl }
    368       1.3   fvdl 
    369       1.3   fvdl static void
    370       1.3   fvdl aic_print_reg_dump_start(FILE *dfile, symbol_node_t *regnode)
    371       1.3   fvdl {
    372       1.3   fvdl 	if (dfile == NULL)
    373       1.3   fvdl 		return;
    374       1.3   fvdl 
    375       1.3   fvdl 	fprintf(dfile,
    376       1.3   fvdl "static %sreg_parse_entry_t %s_parse_table[] = {\n",
    377       1.3   fvdl 		prefix,
    378       1.3   fvdl 		regnode->symbol->name);
    379       1.3   fvdl }
    380       1.3   fvdl 
    381       1.3   fvdl static void
    382       1.3   fvdl aic_print_reg_dump_end(FILE *ofile, FILE *dfile,
    383       1.3   fvdl 		       symbol_node_t *regnode, u_int num_entries)
    384       1.3   fvdl {
    385       1.3   fvdl 	char *lower_name;
    386       1.3   fvdl 	char *letter;
    387       1.3   fvdl 
    388       1.3   fvdl 	lower_name = strdup(regnode->symbol->name);
    389       1.3   fvdl 	if (lower_name == NULL)
    390       1.3   fvdl 		 stop("Unable to strdup symbol name", EX_SOFTWARE);
    391       1.3   fvdl 
    392       1.3   fvdl 	for (letter = lower_name; *letter != '\0'; letter++)
    393       1.3   fvdl 		*letter = tolower(*letter);
    394       1.3   fvdl 
    395       1.3   fvdl 	if (dfile != NULL) {
    396       1.3   fvdl 		if (num_entries != 0)
    397       1.3   fvdl 			fprintf(dfile,
    398       1.3   fvdl "\n"
    399       1.3   fvdl "};\n"
    400       1.3   fvdl "\n");
    401       1.3   fvdl 
    402       1.3   fvdl 		fprintf(dfile,
    403       1.3   fvdl "int\n"
    404       1.3   fvdl "%s%s_print(u_int regvalue, u_int *cur_col, u_int wrap)\n"
    405       1.3   fvdl "{\n"
    406       1.3   fvdl "	return (%sprint_register(%s%s, %d, \"%s\",\n"
    407       1.3   fvdl "	    0x%02x, regvalue, cur_col, wrap));\n"
    408       1.3   fvdl "}\n"
    409       1.3   fvdl "\n",
    410       1.3   fvdl 			prefix,
    411       1.3   fvdl 			lower_name,
    412       1.3   fvdl 			prefix,
    413       1.3   fvdl 			num_entries != 0 ? regnode->symbol->name : "NULL",
    414       1.3   fvdl 			num_entries != 0 ? "_parse_table" : "",
    415       1.3   fvdl 			num_entries,
    416       1.3   fvdl 			regnode->symbol->name,
    417       1.3   fvdl 			regnode->symbol->info.rinfo->address);
    418       1.3   fvdl 	}
    419       1.3   fvdl 
    420       1.3   fvdl 	fprintf(ofile,
    421       1.3   fvdl "#if AIC_DEBUG_REGISTERS\n"
    422       1.3   fvdl "%sreg_print_t %s%s_print;\n"
    423       1.3   fvdl "#else\n"
    424       1.3   fvdl "#define %s%s_print(regvalue, cur_col, wrap) \\\n"
    425       1.3   fvdl "    %sprint_register(NULL, 0, \"%s\", 0x%02x, regvalue, cur_col, wrap)\n"
    426       1.3   fvdl "#endif\n"
    427       1.3   fvdl "\n",
    428       1.3   fvdl 		prefix,
    429       1.3   fvdl 		prefix,
    430       1.3   fvdl 		lower_name,
    431       1.3   fvdl 		prefix,
    432       1.3   fvdl 		lower_name,
    433       1.3   fvdl 		prefix,
    434       1.3   fvdl 		regnode->symbol->name,
    435       1.3   fvdl 		regnode->symbol->info.rinfo->address);
    436       1.3   fvdl }
    437       1.3   fvdl 
    438       1.3   fvdl static void
    439       1.3   fvdl aic_print_reg_dump_entry(FILE *dfile, symbol_node_t *curnode)
    440       1.3   fvdl {
    441       1.3   fvdl 	int num_tabs;
    442       1.3   fvdl 
    443       1.3   fvdl 	if (dfile == NULL)
    444       1.3   fvdl 		return;
    445       1.3   fvdl 
    446       1.3   fvdl 	fprintf(dfile,
    447       1.3   fvdl "	{ \"%s\",",
    448       1.3   fvdl 		curnode->symbol->name);
    449       1.3   fvdl 
    450       1.3   fvdl 	num_tabs = 3 - (strlen(curnode->symbol->name) + 5) / 8;
    451       1.3   fvdl 
    452       1.3   fvdl 	while (num_tabs-- > 0)
    453       1.3   fvdl 		fputc('\t', dfile);
    454       1.3   fvdl 	fprintf(dfile, "0x%02x, 0x%02x }",
    455       1.3   fvdl 		curnode->symbol->info.finfo->value,
    456       1.3   fvdl 		curnode->symbol->info.finfo->mask);
    457       1.3   fvdl }
    458       1.3   fvdl 
    459       1.3   fvdl void
    460       1.3   fvdl symtable_dump(FILE *ofile, FILE *dfile)
    461       1.1   fvdl {
    462       1.1   fvdl 	/*
    463       1.1   fvdl 	 * Sort the registers by address with a simple insertion sort.
    464       1.1   fvdl 	 * Put bitmasks next to the first register that defines them.
    465       1.1   fvdl 	 * Put constants at the end.
    466       1.1   fvdl 	 */
    467       1.3   fvdl 	symlist_t	 registers;
    468       1.3   fvdl 	symlist_t	 masks;
    469       1.3   fvdl 	symlist_t	 constants;
    470       1.3   fvdl 	symlist_t	 download_constants;
    471       1.3   fvdl 	symlist_t	 aliases;
    472       1.3   fvdl 	symlist_t	 exported_labels;
    473       1.3   fvdl 	symbol_node_t	*curnode;
    474       1.3   fvdl 	symbol_node_t	*regnode;
    475       1.3   fvdl 	DBT		 key;
    476       1.3   fvdl 	DBT		 data;
    477       1.3   fvdl 	int		 flag;
    478       1.3   fvdl 	u_int		 i;
    479       1.3   fvdl 
    480       1.3   fvdl 	if (symtable == NULL)
    481       1.3   fvdl 		return;
    482       1.1   fvdl 
    483       1.1   fvdl 	SLIST_INIT(&registers);
    484       1.1   fvdl 	SLIST_INIT(&masks);
    485       1.1   fvdl 	SLIST_INIT(&constants);
    486       1.1   fvdl 	SLIST_INIT(&download_constants);
    487       1.1   fvdl 	SLIST_INIT(&aliases);
    488       1.3   fvdl 	SLIST_INIT(&exported_labels);
    489       1.3   fvdl 	flag = R_FIRST;
    490       1.3   fvdl 	while (symtable->seq(symtable, &key, &data, flag) == 0) {
    491       1.3   fvdl 		symbol_t *cursym;
    492       1.1   fvdl 
    493       1.3   fvdl 		memcpy(&cursym, data.data, sizeof(cursym));
    494       1.3   fvdl 		switch(cursym->type) {
    495       1.3   fvdl 		case REGISTER:
    496       1.3   fvdl 		case SCBLOC:
    497       1.3   fvdl 		case SRAMLOC:
    498       1.3   fvdl 			symlist_add(&registers, cursym, SYMLIST_SORT);
    499       1.3   fvdl 			break;
    500       1.3   fvdl 		case MASK:
    501       1.3   fvdl 		case FIELD:
    502       1.3   fvdl 		case ENUM:
    503       1.3   fvdl 		case ENUM_ENTRY:
    504       1.3   fvdl 			symlist_add(&masks, cursym, SYMLIST_SORT);
    505       1.3   fvdl 			break;
    506       1.3   fvdl 		case CONST:
    507       1.3   fvdl 			symlist_add(&constants, cursym,
    508       1.3   fvdl 				    SYMLIST_INSERT_HEAD);
    509       1.3   fvdl 			break;
    510       1.3   fvdl 		case DOWNLOAD_CONST:
    511       1.3   fvdl 			symlist_add(&download_constants, cursym,
    512       1.3   fvdl 				    SYMLIST_INSERT_HEAD);
    513       1.3   fvdl 			break;
    514       1.3   fvdl 		case ALIAS:
    515       1.3   fvdl 			symlist_add(&aliases, cursym,
    516       1.3   fvdl 				    SYMLIST_INSERT_HEAD);
    517       1.3   fvdl 			break;
    518       1.3   fvdl 		case LABEL:
    519       1.3   fvdl 			if (cursym->info.linfo->exported == 0)
    520       1.3   fvdl 				break;
    521       1.3   fvdl 			symlist_add(&exported_labels, cursym,
    522       1.3   fvdl 				    SYMLIST_INSERT_HEAD);
    523       1.3   fvdl 			break;
    524       1.3   fvdl 		default:
    525       1.3   fvdl 			break;
    526       1.3   fvdl 		}
    527       1.3   fvdl 		flag = R_NEXT;
    528       1.3   fvdl 	}
    529       1.1   fvdl 
    530       1.3   fvdl 	/* Register dianostic functions/declarations first. */
    531       1.3   fvdl 	aic_print_file_prologue(ofile);
    532       1.3   fvdl 	aic_print_reg_dump_types(ofile);
    533       1.3   fvdl 	aic_print_file_prologue(dfile);
    534       1.3   fvdl 	aic_print_include(dfile, stock_include_file);
    535       1.3   fvdl 	SLIST_FOREACH(curnode, &registers, links) {
    536       1.1   fvdl 
    537       1.3   fvdl 		switch(curnode->symbol->type) {
    538       1.3   fvdl 		case REGISTER:
    539       1.3   fvdl 		case SCBLOC:
    540       1.3   fvdl 		case SRAMLOC:
    541       1.3   fvdl 		{
    542       1.3   fvdl 			symlist_t	*fields;
    543       1.3   fvdl 			symbol_node_t	*fieldnode;
    544       1.3   fvdl 			int		 num_entries;
    545       1.3   fvdl 
    546       1.3   fvdl 			num_entries = 0;
    547       1.3   fvdl 			fields = &curnode->symbol->info.rinfo->fields;
    548       1.3   fvdl 			SLIST_FOREACH(fieldnode, fields, links) {
    549       1.3   fvdl 				if (num_entries == 0)
    550       1.3   fvdl 					aic_print_reg_dump_start(dfile,
    551       1.3   fvdl 								 curnode);
    552       1.3   fvdl 				else if (dfile != NULL)
    553       1.3   fvdl 					fputs(",\n", dfile);
    554       1.3   fvdl 				num_entries++;
    555       1.3   fvdl 				aic_print_reg_dump_entry(dfile, fieldnode);
    556       1.1   fvdl 			}
    557       1.3   fvdl 			aic_print_reg_dump_end(ofile, dfile,
    558       1.3   fvdl 					       curnode, num_entries);
    559       1.3   fvdl 		}
    560       1.3   fvdl 		default:
    561       1.3   fvdl 			break;
    562       1.1   fvdl 		}
    563       1.3   fvdl 	}
    564       1.3   fvdl 
    565       1.3   fvdl 	/* Fold in the masks and bits */
    566       1.3   fvdl 	while (SLIST_FIRST(&masks) != NULL) {
    567       1.3   fvdl 		char *regname;
    568       1.3   fvdl 
    569       1.3   fvdl 		curnode = SLIST_FIRST(&masks);
    570       1.3   fvdl 		SLIST_REMOVE_HEAD(&masks, links);
    571       1.3   fvdl 
    572       1.3   fvdl 		regnode = SLIST_FIRST(&curnode->symbol->info.finfo->symrefs);
    573       1.3   fvdl 		regname = regnode->symbol->name;
    574       1.3   fvdl 		regnode = symlist_search(&registers, regname);
    575       1.3   fvdl 		SLIST_INSERT_AFTER(regnode, curnode, links);
    576       1.3   fvdl 	}
    577       1.3   fvdl 
    578       1.3   fvdl 	/* Add the aliases */
    579       1.3   fvdl 	while (SLIST_FIRST(&aliases) != NULL) {
    580       1.3   fvdl 		char *regname;
    581       1.3   fvdl 
    582       1.3   fvdl 		curnode = SLIST_FIRST(&aliases);
    583       1.3   fvdl 		SLIST_REMOVE_HEAD(&aliases, links);
    584       1.1   fvdl 
    585       1.3   fvdl 		regname = curnode->symbol->info.ainfo->parent->name;
    586       1.3   fvdl 		regnode = symlist_search(&registers, regname);
    587       1.3   fvdl 		SLIST_INSERT_AFTER(regnode, curnode, links);
    588       1.3   fvdl 	}
    589       1.3   fvdl 
    590       1.3   fvdl 	/* Output generated #defines. */
    591       1.3   fvdl 	while (SLIST_FIRST(&registers) != NULL) {
    592       1.3   fvdl 		symbol_node_t *curnode;
    593       1.3   fvdl 		u_int value;
    594       1.3   fvdl 		char *tab_str;
    595       1.3   fvdl 		char *tab_str2;
    596       1.3   fvdl 
    597       1.3   fvdl 		curnode = SLIST_FIRST(&registers);
    598       1.3   fvdl 		SLIST_REMOVE_HEAD(&registers, links);
    599       1.3   fvdl 		switch(curnode->symbol->type) {
    600       1.3   fvdl 		case REGISTER:
    601       1.3   fvdl 		case SCBLOC:
    602       1.3   fvdl 		case SRAMLOC:
    603       1.3   fvdl 			fprintf(ofile, "\n");
    604       1.3   fvdl 			value = curnode->symbol->info.rinfo->address;
    605       1.3   fvdl 			tab_str = "\t";
    606       1.3   fvdl 			tab_str2 = "\t\t";
    607       1.3   fvdl 			break;
    608       1.3   fvdl 		case ALIAS:
    609       1.3   fvdl 		{
    610       1.3   fvdl 			symbol_t *parent;
    611       1.3   fvdl 
    612       1.3   fvdl 			parent = curnode->symbol->info.ainfo->parent;
    613       1.3   fvdl 			value = parent->info.rinfo->address;
    614       1.3   fvdl 			tab_str = "\t";
    615       1.3   fvdl 			tab_str2 = "\t\t";
    616       1.3   fvdl 			break;
    617       1.3   fvdl 		}
    618       1.3   fvdl 		case MASK:
    619       1.3   fvdl 		case FIELD:
    620       1.3   fvdl 		case ENUM:
    621       1.3   fvdl 		case ENUM_ENTRY:
    622       1.3   fvdl 			value = curnode->symbol->info.finfo->value;
    623       1.3   fvdl 			tab_str = "\t\t";
    624       1.3   fvdl 			tab_str2 = "\t";
    625       1.3   fvdl 			break;
    626       1.3   fvdl 		default:
    627       1.3   fvdl 			value = 0; /* Quiet compiler */
    628       1.3   fvdl 			tab_str = NULL;
    629       1.3   fvdl 			tab_str2 = NULL;
    630       1.3   fvdl 			stop("symtable_dump: Invalid symbol type "
    631       1.3   fvdl 			     "encountered", EX_SOFTWARE);
    632       1.3   fvdl 			break;
    633       1.1   fvdl 		}
    634       1.3   fvdl 		fprintf(ofile, "#define%s%-16s%s0x%02x\n",
    635       1.3   fvdl 			tab_str, curnode->symbol->name, tab_str2,
    636       1.3   fvdl 			value);
    637       1.3   fvdl 		free(curnode);
    638       1.3   fvdl 	}
    639       1.3   fvdl 	fprintf(ofile, "\n\n");
    640       1.3   fvdl 
    641       1.3   fvdl 	while (SLIST_FIRST(&constants) != NULL) {
    642       1.3   fvdl 		symbol_node_t *curnode;
    643       1.1   fvdl 
    644       1.3   fvdl 		curnode = SLIST_FIRST(&constants);
    645       1.3   fvdl 		SLIST_REMOVE_HEAD(&constants, links);
    646       1.3   fvdl 		fprintf(ofile, "#define\t%-8s\t0x%02x\n",
    647       1.3   fvdl 			curnode->symbol->name,
    648       1.3   fvdl 			curnode->symbol->info.cinfo->value);
    649       1.3   fvdl 		free(curnode);
    650       1.3   fvdl 	}
    651       1.3   fvdl 
    652       1.3   fvdl 
    653       1.3   fvdl 	fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n");
    654       1.3   fvdl 
    655       1.3   fvdl 	for (i = 0; SLIST_FIRST(&download_constants) != NULL; i++) {
    656       1.3   fvdl 		symbol_node_t *curnode;
    657       1.3   fvdl 
    658       1.3   fvdl 		curnode = SLIST_FIRST(&download_constants);
    659       1.3   fvdl 		SLIST_REMOVE_HEAD(&download_constants, links);
    660       1.3   fvdl 		fprintf(ofile, "#define\t%-8s\t0x%02x\n",
    661       1.3   fvdl 			curnode->symbol->name,
    662       1.3   fvdl 			curnode->symbol->info.cinfo->value);
    663       1.3   fvdl 		free(curnode);
    664       1.3   fvdl 	}
    665       1.3   fvdl 	fprintf(ofile, "#define\tDOWNLOAD_CONST_COUNT\t0x%02x\n", i);
    666       1.3   fvdl 
    667       1.3   fvdl 	fprintf(ofile, "\n\n/* Exported Labels */\n");
    668       1.1   fvdl 
    669       1.3   fvdl 	while (SLIST_FIRST(&exported_labels) != NULL) {
    670       1.3   fvdl 		symbol_node_t *curnode;
    671       1.1   fvdl 
    672       1.3   fvdl 		curnode = SLIST_FIRST(&exported_labels);
    673       1.3   fvdl 		SLIST_REMOVE_HEAD(&exported_labels, links);
    674       1.3   fvdl 		fprintf(ofile, "#define\tLABEL_%-8s\t0x%02x\n",
    675       1.3   fvdl 			curnode->symbol->name,
    676       1.3   fvdl 			curnode->symbol->info.linfo->address);
    677       1.3   fvdl 		free(curnode);
    678       1.1   fvdl 	}
    679       1.1   fvdl }
    680       1.1   fvdl 
    681