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