1 /* 2 * Aic7xxx SCSI host adapter firmware asssembler symbol table implementation 3 * 4 * Copyright (c) 1997 Justin T. Gibbs. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * $Id: aicasm_symbol.c,v 1.4 1997/09/27 19:37:30 gibbs Exp $ 29 */ 30 31 32 #include <sys/types.h> 33 34 #include <db.h> 35 #include <fcntl.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sysexits.h> 40 41 #include "aicasm_symbol.h" 42 #include "aicasm.h" 43 44 static DB *symtable; 45 46 symbol_t * 47 symbol_create(name) 48 char *name; 49 { 50 symbol_t *new_symbol; 51 52 new_symbol = (symbol_t *)malloc(sizeof(symbol_t)); 53 if (new_symbol == NULL) { 54 perror("Unable to create new symbol"); 55 exit(EX_SOFTWARE); 56 } 57 memset(new_symbol, 0, sizeof(*new_symbol)); 58 new_symbol->name = strdup(name); 59 new_symbol->type = UNINITIALIZED; 60 return (new_symbol); 61 } 62 63 void 64 symbol_delete(symbol) 65 symbol_t *symbol; 66 { 67 if (symtable != NULL) { 68 DBT key; 69 70 key.data = symbol->name; 71 key.size = strlen(symbol->name); 72 symtable->del(symtable, &key, /*flags*/0); 73 } 74 switch(symbol->type) { 75 case SCBLOC: 76 case SRAMLOC: 77 case REGISTER: 78 if (symbol->info.rinfo != NULL) 79 free(symbol->info.rinfo); 80 break; 81 case ALIAS: 82 if (symbol->info.ainfo != NULL) 83 free(symbol->info.ainfo); 84 break; 85 case MASK: 86 case BIT: 87 if (symbol->info.minfo != NULL) { 88 symlist_free(&symbol->info.minfo->symrefs); 89 free(symbol->info.minfo); 90 } 91 break; 92 case DOWNLOAD_CONST: 93 case CONST: 94 if (symbol->info.cinfo != NULL) 95 free(symbol->info.cinfo); 96 break; 97 case LABEL: 98 if (symbol->info.linfo != NULL) 99 free(symbol->info.linfo); 100 break; 101 case UNINITIALIZED: 102 default: 103 break; 104 } 105 free(symbol->name); 106 free(symbol); 107 } 108 109 void 110 symtable_open() 111 { 112 symtable = dbopen(/*filename*/NULL, 113 O_CREAT | O_NONBLOCK | O_RDWR, /*mode*/0, DB_HASH, 114 /*openinfo*/NULL); 115 116 if (symtable == NULL) { 117 perror("Symbol table creation failed"); 118 exit(EX_SOFTWARE); 119 /* NOTREACHED */ 120 } 121 } 122 123 void 124 symtable_close() 125 { 126 if (symtable != NULL) { 127 DBT key; 128 DBT data; 129 130 while (symtable->seq(symtable, &key, &data, R_FIRST) == 0) { 131 symbol_t *cursym; 132 133 cursym = *(symbol_t **)data.data; 134 symbol_delete(cursym); 135 } 136 symtable->close(symtable); 137 } 138 } 139 140 /* 141 * The semantics of get is to return an uninitialized symbol entry 142 * if a lookup fails. 143 */ 144 symbol_t * 145 symtable_get(name) 146 char *name; 147 { 148 DBT key; 149 DBT data; 150 int retval; 151 152 key.data = (void *)name; 153 key.size = strlen(name); 154 155 if ((retval = symtable->get(symtable, &key, &data, /*flags*/0)) != 0) { 156 if (retval == -1) { 157 perror("Symbol table get operation failed"); 158 exit(EX_SOFTWARE); 159 /* NOTREACHED */ 160 } else if (retval == 1) { 161 /* Symbol wasn't found, so create a new one */ 162 symbol_t *new_symbol; 163 164 new_symbol = symbol_create(name); 165 data.data = &new_symbol; 166 data.size = sizeof(new_symbol); 167 if (symtable->put(symtable, &key, &data, 168 /*flags*/0) !=0) { 169 perror("Symtable put failed"); 170 exit(EX_SOFTWARE); 171 } 172 return (new_symbol); 173 } else { 174 perror("Unexpected return value from db get routine"); 175 exit(EX_SOFTWARE); 176 /* NOTREACHED */ 177 } 178 } 179 return (*(symbol_t **)data.data); 180 } 181 182 symbol_node_t * 183 symlist_search(symlist, symname) 184 symlist_t *symlist; 185 char *symname; 186 { 187 symbol_node_t *curnode; 188 189 curnode = symlist->slh_first; 190 while(curnode != NULL) { 191 if (strcmp(symname, curnode->symbol->name) == 0) 192 break; 193 curnode = curnode->links.sle_next; 194 } 195 return (curnode); 196 } 197 198 void 199 symlist_add(symlist, symbol, how) 200 symlist_t *symlist; 201 symbol_t *symbol; 202 int how; 203 { 204 symbol_node_t *newnode; 205 206 newnode = (symbol_node_t *)malloc(sizeof(symbol_node_t)); 207 if (newnode == NULL) { 208 stop("symlist_add: Unable to malloc symbol_node", EX_SOFTWARE); 209 /* NOTREACHED */ 210 } 211 newnode->symbol = symbol; 212 if (how == SYMLIST_SORT) { 213 symbol_node_t *curnode; 214 int mask; 215 216 mask = FALSE; 217 switch(symbol->type) { 218 case REGISTER: 219 case SCBLOC: 220 case SRAMLOC: 221 break; 222 case BIT: 223 case MASK: 224 mask = TRUE; 225 break; 226 default: 227 stop("symlist_add: Invalid symbol type for sorting", 228 EX_SOFTWARE); 229 /* NOTREACHED */ 230 } 231 232 curnode = symlist->slh_first; 233 if (curnode == NULL 234 || (mask && (curnode->symbol->info.minfo->mask > 235 newnode->symbol->info.minfo->mask)) 236 || (!mask && (curnode->symbol->info.rinfo->address > 237 newnode->symbol->info.rinfo->address))) { 238 SLIST_INSERT_HEAD(symlist, newnode, links); 239 return; 240 } 241 242 while (1) { 243 if (curnode->links.sle_next == NULL) { 244 SLIST_INSERT_AFTER(curnode, newnode, 245 links); 246 break; 247 } else { 248 symbol_t *cursymbol; 249 250 cursymbol = curnode->links.sle_next->symbol; 251 if ((mask && (cursymbol->info.minfo->mask > 252 symbol->info.minfo->mask)) 253 || (!mask &&(cursymbol->info.rinfo->address > 254 symbol->info.rinfo->address))){ 255 SLIST_INSERT_AFTER(curnode, newnode, 256 links); 257 break; 258 } 259 } 260 curnode = curnode->links.sle_next; 261 } 262 } else { 263 SLIST_INSERT_HEAD(symlist, newnode, links); 264 } 265 } 266 267 void 268 symlist_free(symlist) 269 symlist_t *symlist; 270 { 271 symbol_node_t *node1, *node2; 272 273 node1 = symlist->slh_first; 274 while (node1 != NULL) { 275 node2 = node1->links.sle_next; 276 free(node1); 277 node1 = node2; 278 } 279 SLIST_INIT(symlist); 280 } 281 282 void 283 symlist_merge(symlist_dest, symlist_src1, symlist_src2) 284 symlist_t *symlist_dest; 285 symlist_t *symlist_src1; 286 symlist_t *symlist_src2; 287 { 288 symbol_node_t *node; 289 290 *symlist_dest = *symlist_src1; 291 while((node = symlist_src2->slh_first) != NULL) { 292 SLIST_REMOVE_HEAD(symlist_src2, links); 293 SLIST_INSERT_HEAD(symlist_dest, node, links); 294 } 295 296 /* These are now empty */ 297 SLIST_INIT(symlist_src1); 298 SLIST_INIT(symlist_src2); 299 } 300 301 void 302 symtable_dump(ofile) 303 FILE *ofile; 304 { 305 /* 306 * Sort the registers by address with a simple insertion sort. 307 * Put bitmasks next to the first register that defines them. 308 * Put constants at the end. 309 */ 310 symlist_t registers; 311 symlist_t masks; 312 symlist_t constants; 313 symlist_t download_constants; 314 symlist_t aliases; 315 316 SLIST_INIT(®isters); 317 SLIST_INIT(&masks); 318 SLIST_INIT(&constants); 319 SLIST_INIT(&download_constants); 320 SLIST_INIT(&aliases); 321 322 if (symtable != NULL) { 323 DBT key; 324 DBT data; 325 int flag = R_FIRST; 326 327 while (symtable->seq(symtable, &key, &data, flag) == 0) { 328 symbol_t *cursym; 329 330 cursym = *(symbol_t **)data.data; 331 switch(cursym->type) { 332 case REGISTER: 333 case SCBLOC: 334 case SRAMLOC: 335 symlist_add(®isters, cursym, SYMLIST_SORT); 336 break; 337 case MASK: 338 case BIT: 339 symlist_add(&masks, cursym, SYMLIST_SORT); 340 break; 341 case CONST: 342 if (cursym->info.cinfo->define == FALSE) { 343 symlist_add(&constants, cursym, 344 SYMLIST_INSERT_HEAD); 345 } 346 break; 347 case DOWNLOAD_CONST: 348 symlist_add(&download_constants, cursym, 349 SYMLIST_INSERT_HEAD); 350 break; 351 case ALIAS: 352 symlist_add(&aliases, cursym, 353 SYMLIST_INSERT_HEAD); 354 break; 355 default: 356 break; 357 } 358 flag = R_NEXT; 359 } 360 361 /* Put in the masks and bits */ 362 while (masks.slh_first != NULL) { 363 symbol_node_t *curnode; 364 symbol_node_t *regnode; 365 char *regname; 366 367 curnode = masks.slh_first; 368 SLIST_REMOVE_HEAD(&masks, links); 369 370 regnode = 371 curnode->symbol->info.minfo->symrefs.slh_first; 372 regname = regnode->symbol->name; 373 regnode = symlist_search(®isters, regname); 374 SLIST_INSERT_AFTER(regnode, curnode, links); 375 } 376 377 /* Add the aliases */ 378 while (aliases.slh_first != NULL) { 379 symbol_node_t *curnode; 380 symbol_node_t *regnode; 381 char *regname; 382 383 curnode = aliases.slh_first; 384 SLIST_REMOVE_HEAD(&aliases, links); 385 386 regname = curnode->symbol->info.ainfo->parent->name; 387 regnode = symlist_search(®isters, regname); 388 SLIST_INSERT_AFTER(regnode, curnode, links); 389 } 390 391 /* Output what we have */ 392 fprintf(ofile, 393 "/* 394 * DO NOT EDIT - This file is automatically generated. 395 */\n"); 396 while (registers.slh_first != NULL) { 397 symbol_node_t *curnode; 398 u_int8_t value; 399 char *tab_str; 400 char *tab_str2; 401 402 curnode = registers.slh_first; 403 SLIST_REMOVE_HEAD(®isters, links); 404 switch(curnode->symbol->type) { 405 case REGISTER: 406 case SCBLOC: 407 case SRAMLOC: 408 fprintf(ofile, "\n"); 409 value = curnode->symbol->info.rinfo->address; 410 tab_str = "\t"; 411 tab_str2 = "\t\t"; 412 break; 413 case ALIAS: 414 { 415 symbol_t *parent; 416 417 parent = curnode->symbol->info.ainfo->parent; 418 value = parent->info.rinfo->address; 419 tab_str = "\t"; 420 tab_str2 = "\t\t"; 421 break; 422 } 423 case MASK: 424 case BIT: 425 value = curnode->symbol->info.minfo->mask; 426 tab_str = "\t\t"; 427 tab_str2 = "\t"; 428 break; 429 default: 430 value = 0; /* Quiet compiler */ 431 tab_str = NULL; 432 tab_str2 = NULL; 433 stop("symtable_dump: Invalid symbol type " 434 "encountered", EX_SOFTWARE); 435 break; 436 } 437 fprintf(ofile, "#define%s%-16s%s0x%02x\n", 438 tab_str, curnode->symbol->name, tab_str2, 439 value); 440 free(curnode); 441 } 442 fprintf(ofile, "\n\n"); 443 444 while (constants.slh_first != NULL) { 445 symbol_node_t *curnode; 446 447 curnode = constants.slh_first; 448 SLIST_REMOVE_HEAD(&constants, links); 449 fprintf(ofile, "#define\t%-8s\t0x%02x\n", 450 curnode->symbol->name, 451 curnode->symbol->info.cinfo->value); 452 free(curnode); 453 } 454 455 456 fprintf(ofile, "\n\n/* Downloaded Constant Definitions */\n"); 457 458 while (download_constants.slh_first != NULL) { 459 symbol_node_t *curnode; 460 461 curnode = download_constants.slh_first; 462 SLIST_REMOVE_HEAD(&download_constants, links); 463 fprintf(ofile, "#define\t%-8s\t0x%02x\n", 464 curnode->symbol->name, 465 curnode->symbol->info.cinfo->value); 466 free(curnode); 467 } 468 } 469 } 470 471