rspasm.c 18.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794

/*
 * Copyright 1995, Silicon Graphics, Inc.
 * ALL RIGHTS RESERVED
 *
 * UNPUBLISHED -- Rights reserved under the copyright laws of the United
 * States.   Use of a copyright notice is precautionary only and does not
 * imply publication or disclosure.
 *
 * U.S. GOVERNMENT RESTRICTED RIGHTS LEGEND:
 * Use, duplication or disclosure by the Government is subject to restrictions
 * as set forth in FAR 52.227.19(c)(2) or subparagraph (c)(1)(ii) of the Rights
 * in Technical Data and Computer Software clause at DFARS 252.227-7013 and/or
 * in similar or successor clauses in the FAR, or the DOD or NASA FAR
 * Supplement.  Contractor/manufacturer is Silicon Graphics, Inc.,
 * 2011 N. Shoreline Blvd. Mountain View, CA 94039-7311.
 *
 * THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
 * INFORMATION OF SILICON GRAPHICS, INC. ANY DUPLICATION, MODIFICATION,
 * DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS STRICTLY
 * PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF SILICON
 * GRAPHICS, INC.
 *
 */

/*
 * File:	rspasm.c
 * Creator:	hsa@sgi.com
 * Create Date:	Thu Feb 24 15:18:11 PST 1994
 *
 * This file is the top-level of the RSP microcode assembler.
 *
 * A simple two-pass assembler:
 *	- parse source file, converting assembly language to machine language
 *	- go back and fix up jump/branch addresses
 *
 */

#include <stdio.h>
#include <time.h>
#include <string.h>

/* these are shared with the rsp simulator... */
#include "../rspsim/rsp.h"
#include "../rspsim/opcode.h"
#include "../rspsim/disasm.h"

#include "globals.h"
#include "defines.h"
#include "types.h"
#include "macros.h"

#define PROG_NAME		"rspasm"
#define RSPASM_VERSION		"2.3"
#define	DEFINE_SYMBOL		"_RSPASM_VERSION=230"

Instruction	InstructionSpace[MAX_INSTRUCTIONS*OVERDOSE];
unsigned char	DataSpace[MAX_DATA_BYTES*OVERDOSE];
DataSymbol	DataSymbols[MAX_DATA_BYTES*OVERDOSE];	/* for symbols */
Symbol		*LinearSymbols[MAX_DATA_BYTES*OVERDOSE];
int		symbol_count = 0;
unsigned int	DataBase = 0;
unsigned int	AssembleBase = 0;
unsigned int	LocalAssembleBase[MAX_DATA_BYTES*OVERDOSE];
unsigned int	PC = 0;
unsigned int	DataCount = 0;
unsigned int	MaxDataCount = 0;
unsigned int	OpcodeTable[MAX_OPCODES];
char		*(OpcodeStrings[64]);
char		*(SpecialStrings[64]);
char		*(RegimmStrings[32]);
char		*(Cop2Strings[32]);

int		ExternSymCount = 0;
char		ExternSymFiles[32][256];
extern unsigned int	ExternSymbolSearch(char *sym);

/* either RCP or MME instruction set... */
int	ins_set = RCP_INS_SET;

FILE 	*symbol_fp = NULL;
char	input_file[256], output_file[256], listing_file[255], data_file[255];
char	symbol_file[256];
char	curr_file[256];
char	line_buffer[4096];
int	mylineno = 1;
int	is_error = FALSE;
int	dry_run = FALSE;
int	forward_ref = FALSE;

#define MAXQ	8

int	sym_lineq[MAXQ];
char	sym_fileq[MAXQ][256];
int	sym_qhead, sym_qtail, sym_qlen;

#define FANCY_LISTING
#ifdef FANCY_LISTING
typedef struct
{
    char	filename[256];
    int		lineno;
} sym_listing_t;

sym_listing_t	listing_source_table[4096];
#endif

#define	PrintUsage()	fprintf(stderr,"usage : %s [options] srcfile\n",PROG_NAME);

static void
do_help(void)
{
    fprintf(stderr,"\n");
    PrintUsage();

    fprintf(stderr,"\nRSP Assembler Command Line Options:\n\n");
    fprintf(stderr,
	    "\t-D<name>        : define symbol <name> to /usr/lib/cpp.\n");
    fprintf(stderr,
	    "\t-E              : only run the pre-processor(s); don't assemble.\n");
    fprintf(stderr,
	    "\t-I<dir>         : add directory to find include files.\n");
    fprintf(stderr,
	    "\t-O              : enable optimizations.\n");
    fprintf(stderr,
	    "\t-S <file>       : object file to search for external symbols.\n");
    fprintf(stderr,
	    "\t-c              : don't run source through /usr/lib/cpp.\n");
/*
    fprintf(stderr,
	    "\t-d              : turn on yacc debugging.\n");
*/
    fprintf(stderr,
	    "\t-h              : print this help message.\n");
    fprintf(stderr,
	    "\t-l              : don't generate a listing file.\n");
    fprintf(stderr,
	    "\t-m              : run source through m4 macro pre-processor after cpp.\n");
/*
    fprintf(stderr,
	    "\t-mme            : use the MME instruction set (default is Nintendo).\n");
*/
    fprintf(stderr,
	    "\t-o <file>       : name of output file.\n");
    fprintf(stderr,"\n");
}

void
sym_setthisline (int noskip)
{
    if (sym_qlen == MAXQ) {
	fprintf (stderr, "sym_setthisline(): queue length exceeded.\n");
    } else {
	sym_lineq[sym_qhead] = mylineno;
	strcpy (sym_fileq[sym_qhead], curr_file);
	if (!noskip) {
	    sym_qlen++;	/* replace */
	    sym_qhead = (sym_qhead + 1) % MAXQ;
	}
    }
}

void
sym_output(u32 addr, int two)
{
    if (symbol_fp != (FILE *)NULL) {
	if (sym_qlen == 0) {
	    fprintf (stderr, "sym_output(): queue is empty.\n");
	} else {
	    fprintf(symbol_fp, "line 0x%08x %s %d %d\n",
		    (addr << 2) + AssembleBase,
		    sym_fileq[sym_qtail],
		    sym_lineq[sym_qtail], PC);
	    if (!two) {
		/* Don't update queue if first of double instructions */
		sym_qlen--;
		sym_qtail = (sym_qtail + 1) % MAXQ;
	    }
	}
    }
}

void
sym_namescalar (char *name, int reg)
{
    if (symbol_fp != (FILE *)NULL) {
	fprintf(symbol_fp, "sname %s %d\n", name, reg);
    }
}

void
sym_namevector (char *name, int reg)
{
    if (symbol_fp != (FILE *)NULL) {
	fprintf(symbol_fp, "vname %s %d\n", name, reg);
    }
}

void
sym_namecontrol (char *name, int reg)
{
    if (symbol_fp != (FILE *)NULL) {
/*	fprintf(symbol_fp, "cname %s %d\n", name, reg);*/
    }
}

void
sym_unname (char *name)
{
    if (symbol_fp != (FILE *)NULL) {
	fprintf(symbol_fp, "unname %s\n", name);
    }
}

void
sym_entry (char *name)
{
    if (symbol_fp != (FILE *)NULL) {
	fprintf(symbol_fp, "entry %s\n", name);
    }
}

void
sym_end (char *name)
{
    if (symbol_fp != (FILE *)NULL) {
	fprintf(symbol_fp, "end %s\n", name);
    }
}

#ifdef FANCY_LISTING
static void
sym_load_table(char *name)
{
  FILE	*fp;
  char	line[256], filen[256];
  int	addr, lineno, cnt;

  if ((fp=fopen(name,"r")) == NULL) {
    fprintf(stderr,"ERROR : cannot open symbol file [%s]\n", name);
    return;
  }
  
  while (fgets(line,256,fp) != NULL) {
    if (line[0] == 'l' && line[1] == 'i' && 
	line[2] == 'n' && line[3] == 'e' && line[4] == ' ') {
      sscanf(line,"line 0x%08x %s %d %d\n",
	     &addr, &(filen[0]), &lineno, &cnt);
      strcpy(listing_source_table[cnt].filename, filen);
      listing_source_table[cnt].lineno = lineno;
    }
  }
  fclose(fp);
}

static FILE	*last_file_fp = NULL;
static char 	last_filen[256], last_src_line[256*4];

static char *
sym_source_search(u32 addr)
{
    static int		curr_line;
    sym_listing_t	*lp;
    char		*cp, *retval;
    
    lp = &(listing_source_table[addr]);

    /* open file if new or different */
    if (last_file_fp == NULL || strcmp(last_filen, lp->filename) != 0) {
	if (last_file_fp != NULL)
	    fclose(last_file_fp);
	if ((last_file_fp=fopen(lp->filename,"r")) == NULL) {
	    return(NULL);
	}
	strcpy(last_filen, lp->filename);
	curr_line = 0;
    }

    /* find correct line */
    cp = &(last_src_line[0]);
    while ((cp != NULL) && (curr_line < lp->lineno)) {
	cp = fgets(last_src_line,256*4,last_file_fp);
	curr_line++;
    }

    /* handle EOF safely (will re-open file next time) */
    if (cp == NULL) {
	return(cp);
    }

    /* skip white space for compaction */
    while (*cp == ' ' || *cp == '\t')
	cp++;

    return(cp);
}
#endif

void
main(int argc, char *argv[])
{
    boolean	docpp = TRUE, dom4 = FALSE, eflag = FALSE, errflag;
    int		i, j, offset, debug = FALSE, dolisting = TRUE, tmp;
    FILE	*output, *listing, *data;
    char	buf[255], cppcmd[1024], cppdefs[512], *cp;
    time_t	this_time;
    Symbol	*symPtr;
    int		value;

    strcpy(output_file, "a.out");	/* default executable */
    strcpy(cppdefs, "");

    fprintf(stderr,"%s : RSP Assembler, Version %s, created %s %s\n",
	    PROG_NAME, RSPASM_VERSION, __DATE__, __TIME__);

    while ((argc > 1) && (argv[1][0] == '-')) {
	switch(argv[1][1]) {

	  case 'D':
	    sprintf(cppdefs, "%s %s", cppdefs, argv[1]);
	    break;
	    
	  case 'E':
	    eflag = TRUE;
	    break;
	    
	  case 'I':
	    sprintf(cppdefs, "%s %s", cppdefs, argv[1]);
	    break;
	    
	  case 'O':	/* optimize. Ha! */
	    break;

	  case 'S':
	    if (ExternSymCount < 32) {
		sprintf(ExternSymFiles[ExternSymCount++],"%s.dbg",argv[2]);
	    } else {
		fprintf(stderr,"ERROR : too many external symbol files.\n");
	    }
	    argv++;
	    argc--;
	    break;
	    
/*
	  case 'b':
	    AssembleBase = atoi(argv[2]);
	    argv++;
	    argc--;
	    break;
*/

	  case 'c':
	    docpp = FALSE;
	    break;

	  case 'd':
	    debug = TRUE;
	    break;
	    
	  case 'H':
	  case 'h':
	    do_help();
	    exit(0);
	    break;

	  case 'l':
	    dolisting = FALSE;
	    break;
	    
	  case 'm':
	    if (argv[1][2] == 'm' && argv[1][3] == 'e') {
/*
		ins_set = MME_INS_SET;
*/
	    } else {
		dom4 = TRUE;
	    }
	    break;

	  case 'o':
	    strcpy(output_file, argv[2]);
	    argv++;
	    argc--;
	    break;

	  case 't':
	    dry_run = TRUE;
	    break;

	  case 'F':
	    forward_ref = TRUE;
	    break;

	  default:
	    fprintf(stderr,"ERROR : unrecognized option %s\n",argv[1]);
	    PrintUsage();
	    do_help();
	    exit(-1);
	    break;
	}
	argc--;
	argv++;
    }

    /*
     *	make sure source file specified.
     */
    if (argc < 2) {
	fprintf(stderr,"ERROR : no source file specified.\n");
	PrintUsage();
	do_help();
	exit(-1);
    }

    strcpy(input_file, argv[1]);
    strcpy(curr_file, input_file);

    /*
     * make sure we don't blow away the source file...
     */
    if (strcmp(output_file, input_file)==0) {
	fprintf(stderr,"ERROR : output file [%s] same as input file [%s].\n",
		output_file, input_file);
	exit(-1);
    }

    /*
     * open source file.
     */
    if ((yyin=fopen(input_file,"r")) == NULL) {
	fprintf(stderr,"ERROR : cannot open source file [%s]\n",
		input_file);
	exit(-1);
    }

    sprintf(symbol_file,"%s.sym",output_file);
    if ((symbol_fp=fopen(symbol_file,"w")) == NULL) {
	fprintf(stderr,"ERROR : cannot open symbol file [%s]\n",
		symbol_file);
	exit(-1);
    }

    /*
     * run through the C preprocessor
     */
    if (!docpp && !dom4) {
	/* skip cpp step */
    } else {
	fclose(yyin);

	if (docpp) {
#ifdef __linux__
#if 0
	    sprintf(cppcmd,"/usr/bin/cc -x assembler-with-cpp -E -D%s -D_LANGUAGE_ASSEMBLY %s %s ",
#endif
	    sprintf(cppcmd,"%s/usr/sbin/cpp -lang-asm -D%s -D_LANGUAGE_ASSEMBLY %s %s ", getenv("ROOT"),
#else
	    sprintf(cppcmd,"/usr/bin/cc -E -D%s -D_LANGUAGE_ASSEMBLY %s %s ",
#endif
		    DEFINE_SYMBOL, cppdefs, input_file);
	} else {
	    sprintf(cppcmd,"cat %s ", input_file);
	}

	if (dom4) {
	    sprintf(cppcmd,"%s | m4 ",cppcmd);
	}

#if 0
fprintf(stderr, "%s\n", cppcmd);
#endif
	if ((yyin = (FILE *) popen(cppcmd, "r")) == NULL) {
	    fprintf(stderr,"ERROR : could not run cpp on [%s]\n",
		    input_file);
	    exit(-1);
	}
    }

    if (eflag) {
	if ((output=fopen(output_file,"w")) == NULL) {
	    fprintf(stderr,"ERROR : cannot open output file [%s]\n",
		    output_file);
	    exit(-1);
	}
	while (fgets(buf, 80, yyin) != NULL) {
	    fprintf(output,"%s",buf);
	}
	exit(0);
    }

    hcreate(SYMBOL_TABLE_SIZE);	/* init hash() functions */

    BuildOpcodeTable();

    for (i = 0; i < MAX_DATA_BYTES*OVERDOSE; i ++){
      LocalAssembleBase[i] = 0xffffffff;
    }
    
    /*
     * First pass: parse input.
     */
    yydebug = debug;
    if (yyparse())
	exit(-1);
    
    fclose(yyin);

    fclose(symbol_fp);

    /*
     * Second pass: fixup forward references.
     */

    /* Check for IMEM over flow before proceeding. */
    if (PC > (MAX_INSTRUCTIONS*OVERDOSE)) {
	fprintf(stderr,"WARNING : number of assembled instructions exceeds IMEM %dk size.\n", OVERDOSE);
    }
    
    for (i=0; i<PC; i++) {
        if (LocalAssembleBase[i] != 0xffffffff){
	  AssembleBase = LocalAssembleBase[i];
        } else {
	  LocalAssembleBase[i] = AssembleBase;
        }
	symPtr = InstructionSpace[i].symPtr;
	if (symPtr != NULL) {

	    value = 0xffffffff;
	    if (!IsSymDefined(symPtr)) {
		value = ExternSymbolSearch(symPtr->id);
		symPtr->value = value;
	    }

	    if (!IsSymDefined(symPtr) && value == 0xffffffff) {
		fprintf(stderr,"ERROR : symbol %s undefined.\n",symPtr->id);
	    } else {
		/*
		 * fix up reference
		 */
		if (symPtr->flag & SYM_LA) {
		    /* this is the 'la' macro/op, which generates two
		     * instructions, an lui and an ori. The symPtr
		     * is used to fix both...
		     */
		    tmp = ((unsigned)(symPtr->value & 0xffff0000) >> 16);
		    LoadField(InstructionSpace[i].instruction, 15, 0, tmp);
		    i++;
		    tmp = symPtr->value & 0x0000ffff;
		    LoadField(InstructionSpace[i].instruction, 15, 0, tmp);
		} else if (ExtractBits(InstructionSpace[i].instruction,31,27) 
			   == 0x01) {
		    /*
		     * J & JAL instruction
		     */
		    if (symPtr->value & 0x03) {
			yyerror("targeting nonword boundary");
		    } else if ((symPtr->value & 0xf0000000) != 
			       ((i+AssembleBase) & 0xf0000000)) {
			yyerror("target high order bits disagree with PC");
		    }
		    tmp = (symPtr->value & 0x0fffffff) >> 2;
		    LoadField(InstructionSpace[i].instruction, 25, 0, tmp);
		} else {
		    /*
		     * branch relative offset instruction
		     * symPtr->value is a byte address.
		     */
		    tmp = ((i+1)<<2) + AssembleBase;
		    offset = symPtr->value - tmp;
		    if (symPtr->value & 0x3) {
			yyerror("target to non word boundary");
		    } else if ((offset>32767) || (offset<-32768)) {
			yyerror("target out of range of current PC");
		    }
		    LoadField(InstructionSpace[i].instruction, 15, 0, (offset>>2));
		}
	    }
	}
    }

    /*
     * Second pass for data space: fixup forward references.
     */

    /* Check for DMEM over flow before proceeding. */
    if (MaxDataCount > MAX_DATA_BYTES * OVERDOSE) {
	fprintf(stderr,"WARNING : data exceeds DMEM 4KB*%d size.\n",
		OVERDOSE);
    }

    for (i=0; i<MaxDataCount; i++) {
	symPtr = DataSymbols[i].sym;
	if (symPtr != NULL) {

	    value = 0xffffffff;
	    if (!IsSymDefined(symPtr)) {
		value = ExternSymbolSearch(symPtr->id);
		symPtr->value = value;
	    }

	    if ((!IsSymDefined(symPtr)) && value == 0xffffffff
		&& forward_ref == FALSE){
		fprintf(stderr,"ERROR : data symbol %s undefined.\n",symPtr->id);
		is_error = TRUE;
	    } else {
		if (DataSymbols[i].flag & SYM_HALF) {
#if	1
			value = symPtr->value&0xffff;
			value += ntohs(*((unsigned short int *)&(DataSpace[i])));
			value <<= 16;
#else
			value = (symPtr->value&0xffff)<<16;
#endif
#if 0
			bcopy(&(value), &(DataSpace[i]), 
	      		sizeof(unsigned short int));
#else
			*((unsigned short int *)&(DataSpace[i])) = htons(value>>16);
#endif
		} else {
#if	1
			value = symPtr->value +
			        ntohl(*((unsigned long int *)&(DataSpace[i])));
			*((unsigned long int *)&(DataSpace[i])) = htonl(value);
#if 0
			bcopy(&(value), &(DataSpace[i]), 
	      		sizeof(unsigned long int));
#endif
#else
			bcopy(&(symPtr->value), &(DataSpace[i]), 
	      		sizeof(unsigned long int));
#endif
		}
	    }
	}
    }

    if (is_error) {
	if (dry_run)
	    fprintf(stderr,"%s : object size: %4d instructions (%d bytes)\n",
		    output_file, PC, PC << 2);
	exit(-1);
    }

    /*
     * write out executable.
     */
    if ((output=fopen(output_file,"w")) == NULL) {
	fprintf(stderr,"ERROR : cannot open output file [%s]\n",
		output_file);
	exit(-1);
    }
    for (i=0; i<PC; i++) {
	unsigned int tmp = htonl(InstructionSpace[i].instruction);
	fwrite(&tmp, sizeof(unsigned int), 1,
	       output);
    }
    fclose(output);

    /*
     * write out data.
     */
    sprintf(data_file,"%s.dat",output_file);
    if ((data=fopen(data_file,"w")) == NULL) {
	fprintf(stderr,"ERROR : cannot open data file [%s]\n",
		data_file);
	exit(-1);
    }
    for (i=0; i<MaxDataCount; i++) {
	fwrite(&(DataSpace[i]), sizeof(unsigned char), 1, data);
    }
    fclose(data);

    fprintf(stderr,"%s : object size: %4d instructions (%d bytes)\n",
	    output_file, PC, PC << 2);
    fprintf(stderr,"%s : data size:   %4d bytes, based at 0x%08x.\n",
	    data_file, MaxDataCount, DataBase);

    /*
     * write out symbols for debugger:
     */
    sprintf(data_file,"%s.dbg",output_file);
    if ((data=fopen(data_file,"w")) == NULL) {
	fprintf(stderr,"ERROR : cannot open debugger symbol file [%s]\n",
		data_file);
	exit(-1);
    }
    errflag = FALSE;

    for (i=0; i<symbol_count; i++) {
      if (LinearSymbols[i]->flag & (SYM_LABEL|SYM_DATA)){
	value = 0xffffffff;
	if (IsSymDefined(LinearSymbols[i])){
	  value = LinearSymbols[i]->value;
	} else {
	  value = ExternSymbolSearch(LinearSymbols[i]->id);
	}
	if (value != 0xffffffff){
	  if (IsSymLabel(LinearSymbols[i])) {
	    fprintf(data,"%s %08x I\n", LinearSymbols[i]->id, value);
	  }
	  if (IsSymData(LinearSymbols[i])) {
	    fprintf(data,"%s %08x D\n", LinearSymbols[i]->id, value);
	  }
	} else {
	  fprintf(stderr,"ERROR : symbol [%s] undefined.\n",
		  LinearSymbols[i]->id);
	  errflag = TRUE;
	}
      }
    }
    fclose(data);
    if (errflag) exit(-1);

    /*
     * write out listing (if requested).
     */
    if (dolisting) {
#ifdef FANCY_LISTING
	sym_load_table(symbol_file);
#endif
	sprintf(listing_file,"%s.lst",output_file);
	if ((listing=fopen(listing_file,"w")) == NULL) {
	    fprintf(stderr,"ERROR : cannot open listing file [%s]\n",
		    listing_file);
	    exit(-1);
	}

	this_time = time(NULL);
	fprintf(listing,"\n***\n");
	fprintf(listing,"*** Source file   : %s\n",input_file);
	fprintf(listing,"*** Object file   : %s\n",output_file);
	fprintf(listing,"*** Listing file  : %s\n",listing_file);
	fprintf(listing,"*** Created by    : %s\n",getenv("USER"));
	fprintf(listing,"*** Create time   : %s", asctime(localtime(&this_time)));
	fprintf(listing,"***\n\n");

	for (i=0; i<PC; i++) {
	  AssembleBase = LocalAssembleBase[i];
	  fprintf(listing,"0x%08x : 0x%08x        ",
		  (i<<2)+AssembleBase,InstructionSpace[i].instruction);
	  rsp_DisasmInst(InstructionSpace[i].instruction,
			 buf, (i<<2)+AssembleBase);
#ifdef FANCY_LISTING
	  fprintf(listing,"%s",buf);

	  for (j=strlen(buf); j<29; j++) {
	    fprintf(listing," ");
	  }
	  cp = sym_source_search(i);
	  fprintf(listing,"|| %s", (cp == NULL) ? "\n" : cp);
#else
	  fprintf(listing,"%s\n",buf);
#endif
	}
	fclose(listing);
    }
    exit(0);

}


unsigned int
ExternSymbolSearch(char *sym)
{
    char	junk1[80], junk2[80];
    FILE	*fp;
    int		i, j;
    unsigned int	value;
    
    if (forward_ref) return(0);
    
    for (i=0; i<ExternSymCount; i++) {

	if ((fp=fopen(ExternSymFiles[i],"r"))==NULL) {
	    fprintf(stderr,"ERROR : could not open symbol file [%s] to find  symbol [%s].\n",
		    ExternSymFiles[i], sym);
	} else {
	    value = 0xffffffff;
	    while (fgets(junk2, 80, fp) != NULL) {
		sscanf(junk2,"%s %x",&(junk1[0]),&j);
		if (strcmp(junk1,sym)==0) {
		    value = j;
		    fclose(fp);
		    return(value);
		}
	    }
	    fclose(fp);
	}
    }

    fprintf(stderr,"ERROR : could not find symbol [%s] in external symbol tables.\n",sym);
    return(0xffffffff);
}