Home | History | Annotate | Line # | Download | only in aic7xxx
aicasm.c revision 1.1
      1  1.1  fvdl /*	$NetBSD: aicasm.c,v 1.1 2000/03/15 02:09:13 fvdl Exp $	*/
      2  1.1  fvdl 
      3  1.1  fvdl /*
      4  1.1  fvdl  * Aic7xxx SCSI host adapter firmware asssembler
      5  1.1  fvdl  *
      6  1.1  fvdl  * Copyright (c) 1997, 1998 Justin T. Gibbs.
      7  1.1  fvdl  * All rights reserved.
      8  1.1  fvdl  *
      9  1.1  fvdl  * Redistribution and use in source and binary forms, with or without
     10  1.1  fvdl  * modification, are permitted provided that the following conditions
     11  1.1  fvdl  * are met:
     12  1.1  fvdl  * 1. Redistributions of source code must retain the above copyright
     13  1.1  fvdl  *    notice, this list of conditions, and the following disclaimer,
     14  1.1  fvdl  *    without modification, immediately at the beginning of the file.
     15  1.1  fvdl  * 2. The name of the author may not be used to endorse or promote products
     16  1.1  fvdl  *    derived from this software without specific prior written permission.
     17  1.1  fvdl  *
     18  1.1  fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  1.1  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  1.1  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  1.1  fvdl  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
     22  1.1  fvdl  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  1.1  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  1.1  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  1.1  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  1.1  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  1.1  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  1.1  fvdl  * SUCH DAMAGE.
     29  1.1  fvdl  *
     30  1.1  fvdl  * $FreeBSD: src/sys/dev/aic7xxx/aicasm.c,v 1.23 1999/08/28 00:41:25 peter Exp $
     31  1.1  fvdl  */
     32  1.1  fvdl #include <sys/types.h>
     33  1.1  fvdl #include <sys/mman.h>
     34  1.1  fvdl 
     35  1.1  fvdl #include <ctype.h>
     36  1.1  fvdl #include <stdio.h>
     37  1.1  fvdl #include <stdlib.h>
     38  1.1  fvdl #include <string.h>
     39  1.1  fvdl #include <sysexits.h>
     40  1.1  fvdl #include <unistd.h>
     41  1.1  fvdl 
     42  1.1  fvdl #include "aicasm.h"
     43  1.1  fvdl #include "aicasm_symbol.h"
     44  1.1  fvdl #include "sequencer.h"
     45  1.1  fvdl 
     46  1.1  fvdl typedef struct patch {
     47  1.1  fvdl 	STAILQ_ENTRY(patch) links;
     48  1.1  fvdl 	int		patch_func;
     49  1.1  fvdl 	u_int		begin;
     50  1.1  fvdl 	u_int		skip_instr;
     51  1.1  fvdl 	u_int		skip_patch;
     52  1.1  fvdl } patch_t;
     53  1.1  fvdl 
     54  1.1  fvdl STAILQ_HEAD(patch_list, patch) patches;
     55  1.1  fvdl 
     56  1.1  fvdl static void usage(void);
     57  1.1  fvdl static void back_patch(void);
     58  1.1  fvdl static void output_code(FILE *ofile);
     59  1.1  fvdl static void output_listing(FILE *listfile, char *ifilename);
     60  1.1  fvdl static void dump_scope(scope_t *scope);
     61  1.1  fvdl static void emit_patch(scope_t *scope, int patch);
     62  1.1  fvdl static int check_patch(patch_t **start_patch, int start_instr,
     63  1.1  fvdl 		       int *skip_addr, int *func_vals);
     64  1.1  fvdl 
     65  1.1  fvdl struct path_list search_path;
     66  1.1  fvdl int includes_search_curdir;
     67  1.1  fvdl char *appname;
     68  1.1  fvdl FILE *ofile;
     69  1.1  fvdl char *ofilename;
     70  1.1  fvdl char *regfilename;
     71  1.1  fvdl FILE *regfile;
     72  1.1  fvdl char *listfilename;
     73  1.1  fvdl FILE *listfile;
     74  1.1  fvdl 
     75  1.1  fvdl static STAILQ_HEAD(,instruction) seq_program;
     76  1.1  fvdl struct scope_list scope_stack;
     77  1.1  fvdl symlist_t patch_functions;
     78  1.1  fvdl 
     79  1.1  fvdl #if DEBUG
     80  1.1  fvdl extern int yy_flex_debug;
     81  1.1  fvdl extern int yydebug;
     82  1.1  fvdl #endif
     83  1.1  fvdl extern FILE *yyin;
     84  1.1  fvdl extern int yyparse __P((void));
     85  1.1  fvdl 
     86  1.1  fvdl int
     87  1.1  fvdl main(argc, argv)
     88  1.1  fvdl 	int argc;
     89  1.1  fvdl 	char *argv[];
     90  1.1  fvdl {
     91  1.1  fvdl 	extern char *optarg;
     92  1.1  fvdl 	extern int optind;
     93  1.1  fvdl 	int  ch;
     94  1.1  fvdl 	int  retval;
     95  1.1  fvdl 	char *inputfilename;
     96  1.1  fvdl 	scope_t *sentinal;
     97  1.1  fvdl 
     98  1.1  fvdl 	STAILQ_INIT(&patches);
     99  1.1  fvdl 	SLIST_INIT(&search_path);
    100  1.1  fvdl 	STAILQ_INIT(&seq_program);
    101  1.1  fvdl 	SLIST_INIT(&scope_stack);
    102  1.1  fvdl 
    103  1.1  fvdl 	/* Set Sentinal scope node */
    104  1.1  fvdl 	sentinal = scope_alloc();
    105  1.1  fvdl 	sentinal->type = SCOPE_ROOT;
    106  1.1  fvdl 
    107  1.1  fvdl 	includes_search_curdir = 1;
    108  1.1  fvdl 	appname = *argv;
    109  1.1  fvdl 	regfile = NULL;
    110  1.1  fvdl 	listfile = NULL;
    111  1.1  fvdl #if DEBUG
    112  1.1  fvdl 	yy_flex_debug = 0;
    113  1.1  fvdl 	yydebug = 0;
    114  1.1  fvdl #endif
    115  1.1  fvdl 	while ((ch = getopt(argc, argv, "d:l:n:o:r:I:O:")) != -1) {
    116  1.1  fvdl 		switch(ch) {
    117  1.1  fvdl 		case 'd':
    118  1.1  fvdl #if DEBUG
    119  1.1  fvdl 			if (strcmp(optarg, "s") == 0) {
    120  1.1  fvdl 				yy_flex_debug = 1;
    121  1.1  fvdl 			} else if (strcmp(optarg, "p") == 0) {
    122  1.1  fvdl 				yydebug = 1;
    123  1.1  fvdl 			} else {
    124  1.1  fvdl 				fprintf(stderr, "%s: -d Requires either an "
    125  1.1  fvdl 					"'s' or 'p' argument\n", appname);
    126  1.1  fvdl 				usage();
    127  1.1  fvdl 			}
    128  1.1  fvdl #else
    129  1.1  fvdl 			stop("-d: Assembler not built with debugging "
    130  1.1  fvdl 			     "information", EX_SOFTWARE);
    131  1.1  fvdl #endif
    132  1.1  fvdl 			break;
    133  1.1  fvdl 		case 'l':
    134  1.1  fvdl 			/* Create a program listing */
    135  1.1  fvdl 			if ((listfile = fopen(optarg, "w")) == NULL) {
    136  1.1  fvdl 				perror(optarg);
    137  1.1  fvdl 				stop(NULL, EX_CANTCREAT);
    138  1.1  fvdl 			}
    139  1.1  fvdl 			listfilename = optarg;
    140  1.1  fvdl 			break;
    141  1.1  fvdl 		case 'n':
    142  1.1  fvdl 			/* Don't complain about the -nostdinc directrive */
    143  1.1  fvdl 			if (strcmp(optarg, "ostdinc")) {
    144  1.1  fvdl 				fprintf(stderr, "%s: Unknown option -%c%s\n",
    145  1.1  fvdl 					appname, ch, optarg);
    146  1.1  fvdl 				usage();
    147  1.1  fvdl 				/* NOTREACHED */
    148  1.1  fvdl 			}
    149  1.1  fvdl 			break;
    150  1.1  fvdl 		case 'o':
    151  1.1  fvdl 			if ((ofile = fopen(optarg, "w")) == NULL) {
    152  1.1  fvdl 				perror(optarg);
    153  1.1  fvdl 				stop(NULL, EX_CANTCREAT);
    154  1.1  fvdl 			}
    155  1.1  fvdl 			ofilename = optarg;
    156  1.1  fvdl 			break;
    157  1.1  fvdl 		case 'r':
    158  1.1  fvdl 			if ((regfile = fopen(optarg, "w")) == NULL) {
    159  1.1  fvdl 				perror(optarg);
    160  1.1  fvdl 				stop(NULL, EX_CANTCREAT);
    161  1.1  fvdl 			}
    162  1.1  fvdl 			regfilename = optarg;
    163  1.1  fvdl 			break;
    164  1.1  fvdl 		case 'I':
    165  1.1  fvdl 		{
    166  1.1  fvdl 			path_entry_t include_dir;
    167  1.1  fvdl 
    168  1.1  fvdl 			if (strcmp(optarg, "-") == 0) {
    169  1.1  fvdl 				if (includes_search_curdir == 0) {
    170  1.1  fvdl 					fprintf(stderr, "%s: Warning - '-I-' "
    171  1.1  fvdl 							"specified multiple "
    172  1.1  fvdl 							"times\n", appname);
    173  1.1  fvdl 				}
    174  1.1  fvdl 				includes_search_curdir = 0;
    175  1.1  fvdl 				for (include_dir = SLIST_FIRST(&search_path);
    176  1.1  fvdl 				     include_dir != NULL;
    177  1.1  fvdl 				     include_dir =
    178  1.1  fvdl 					SLIST_NEXT(include_dir, links))
    179  1.1  fvdl 					/*
    180  1.1  fvdl 					 * All entries before a '-I-' only
    181  1.1  fvdl 					 * apply to includes specified with
    182  1.1  fvdl 					 * quotes instead of "<>".
    183  1.1  fvdl 					 */
    184  1.1  fvdl 					include_dir->quoted_includes_only = 1;
    185  1.1  fvdl 			} else {
    186  1.1  fvdl 				include_dir =
    187  1.1  fvdl 				    (path_entry_t)malloc(sizeof(*include_dir));
    188  1.1  fvdl 				if (include_dir == NULL) {
    189  1.1  fvdl 					perror(optarg);
    190  1.1  fvdl 					stop(NULL, EX_OSERR);
    191  1.1  fvdl 				}
    192  1.1  fvdl 				include_dir->directory = strdup(optarg);
    193  1.1  fvdl 				if (include_dir->directory == NULL) {
    194  1.1  fvdl 					perror(optarg);
    195  1.1  fvdl 					stop(NULL, EX_OSERR);
    196  1.1  fvdl 				}
    197  1.1  fvdl 				include_dir->quoted_includes_only = 0;
    198  1.1  fvdl 				SLIST_INSERT_HEAD(&search_path, include_dir,
    199  1.1  fvdl 						  links);
    200  1.1  fvdl 			}
    201  1.1  fvdl 			break;
    202  1.1  fvdl 		}
    203  1.1  fvdl 		case '?':
    204  1.1  fvdl 		default:
    205  1.1  fvdl 			usage();
    206  1.1  fvdl 			/* NOTREACHED */
    207  1.1  fvdl 		}
    208  1.1  fvdl 	}
    209  1.1  fvdl 	argc -= optind;
    210  1.1  fvdl 	argv += optind;
    211  1.1  fvdl 
    212  1.1  fvdl 	if (argc != 1) {
    213  1.1  fvdl 		fprintf(stderr, "%s: No input file specifiled\n", appname);
    214  1.1  fvdl 		usage();
    215  1.1  fvdl 		/* NOTREACHED */
    216  1.1  fvdl 	}
    217  1.1  fvdl 
    218  1.1  fvdl 	symtable_open();
    219  1.1  fvdl 	inputfilename = *argv;
    220  1.1  fvdl 	include_file(*argv, SOURCE_FILE);
    221  1.1  fvdl 	retval = yyparse();
    222  1.1  fvdl 	if (retval == 0) {
    223  1.1  fvdl 		if (SLIST_FIRST(&scope_stack) == NULL
    224  1.1  fvdl 		 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
    225  1.1  fvdl 			stop("Unterminated conditional expression",
    226  1.1  fvdl 			     EX_DATAERR);
    227  1.1  fvdl 			/* NOTREACHED */
    228  1.1  fvdl 		}
    229  1.1  fvdl 
    230  1.1  fvdl 		/* Process outmost scope */
    231  1.1  fvdl 		process_scope(SLIST_FIRST(&scope_stack));
    232  1.1  fvdl 		/*
    233  1.1  fvdl 		 * Decend the tree of scopes and insert/emit
    234  1.1  fvdl 		 * patches as appropriate.  We perform a depth first
    235  1.1  fvdl 		 * tranversal, recursively handling each scope.
    236  1.1  fvdl 		 */
    237  1.1  fvdl 		/* start at the root scope */
    238  1.1  fvdl 		dump_scope(SLIST_FIRST(&scope_stack));
    239  1.1  fvdl 
    240  1.1  fvdl 		/* Patch up forward jump addresses */
    241  1.1  fvdl 		back_patch();
    242  1.1  fvdl 
    243  1.1  fvdl 		if (ofile != NULL)
    244  1.1  fvdl 			output_code(ofile);
    245  1.1  fvdl 		if (regfile != NULL) {
    246  1.1  fvdl 			symtable_dump(regfile);
    247  1.1  fvdl 		}
    248  1.1  fvdl 		if (listfile != NULL)
    249  1.1  fvdl 			output_listing(listfile, inputfilename);
    250  1.1  fvdl 	}
    251  1.1  fvdl 
    252  1.1  fvdl 	stop(NULL, 0);
    253  1.1  fvdl 	/* NOTREACHED */
    254  1.1  fvdl 	return (0);
    255  1.1  fvdl }
    256  1.1  fvdl 
    257  1.1  fvdl static void
    258  1.1  fvdl usage()
    259  1.1  fvdl {
    260  1.1  fvdl 
    261  1.1  fvdl 	(void)fprintf(stderr,
    262  1.1  fvdl "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]
    263  1.1  fvdl 			[-r register_output_file] [-l program_list_file]
    264  1.1  fvdl 			input_file\n",
    265  1.1  fvdl 			appname);
    266  1.1  fvdl 	exit(EX_USAGE);
    267  1.1  fvdl }
    268  1.1  fvdl 
    269  1.1  fvdl static void
    270  1.1  fvdl back_patch()
    271  1.1  fvdl {
    272  1.1  fvdl 	struct instruction *cur_instr;
    273  1.1  fvdl 
    274  1.1  fvdl 	for(cur_instr = STAILQ_FIRST(&seq_program);
    275  1.1  fvdl 	    cur_instr != NULL;
    276  1.1  fvdl 	    cur_instr = STAILQ_NEXT(cur_instr, links)) {
    277  1.1  fvdl 		if (cur_instr->patch_label != NULL) {
    278  1.1  fvdl 			struct ins_format3 *f3_instr;
    279  1.1  fvdl 			u_int address;
    280  1.1  fvdl 
    281  1.1  fvdl 			if (cur_instr->patch_label->type != LABEL) {
    282  1.1  fvdl 				char buf[255];
    283  1.1  fvdl 
    284  1.1  fvdl 				snprintf(buf, sizeof(buf),
    285  1.1  fvdl 					 "Undefined label %s",
    286  1.1  fvdl 					 cur_instr->patch_label->name);
    287  1.1  fvdl 				stop(buf, EX_DATAERR);
    288  1.1  fvdl 				/* NOTREACHED */
    289  1.1  fvdl 			}
    290  1.1  fvdl 			f3_instr = &cur_instr->format.format3;
    291  1.1  fvdl 			address = f3_instr->address;
    292  1.1  fvdl 			address += cur_instr->patch_label->info.linfo->address;
    293  1.1  fvdl 			f3_instr->address = address;
    294  1.1  fvdl 		}
    295  1.1  fvdl 	}
    296  1.1  fvdl }
    297  1.1  fvdl 
    298  1.1  fvdl static void
    299  1.1  fvdl output_code(ofile)
    300  1.1  fvdl 	FILE *ofile;
    301  1.1  fvdl {
    302  1.1  fvdl 	struct instruction *cur_instr;
    303  1.1  fvdl 	patch_t *cur_patch;
    304  1.1  fvdl 	symbol_node_t *cur_node;
    305  1.1  fvdl 	int instrcount;
    306  1.1  fvdl 
    307  1.1  fvdl 	instrcount = 0;
    308  1.1  fvdl 	fprintf(ofile,
    309  1.1  fvdl "/*
    310  1.1  fvdl   * DO NOT EDIT - This file is automatically generated.
    311  1.1  fvdl   */\n");
    312  1.1  fvdl 
    313  1.1  fvdl 	fprintf(ofile, "static u_int8_t seqprog[] = {\n");
    314  1.1  fvdl 	for(cur_instr = STAILQ_FIRST(&seq_program);
    315  1.1  fvdl 	    cur_instr != NULL;
    316  1.1  fvdl 	    cur_instr = STAILQ_NEXT(cur_instr, links)) {
    317  1.1  fvdl 
    318  1.1  fvdl 		fprintf(ofile, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
    319  1.1  fvdl #if _BYTE_ORDER == _BIG_ENDIAN
    320  1.1  fvdl 			cur_instr->format.bytes[3],
    321  1.1  fvdl 			cur_instr->format.bytes[2],
    322  1.1  fvdl 			cur_instr->format.bytes[1],
    323  1.1  fvdl 			cur_instr->format.bytes[0]);
    324  1.1  fvdl #else
    325  1.1  fvdl 			cur_instr->format.bytes[0],
    326  1.1  fvdl 			cur_instr->format.bytes[1],
    327  1.1  fvdl 			cur_instr->format.bytes[2],
    328  1.1  fvdl 			cur_instr->format.bytes[3]);
    329  1.1  fvdl #endif
    330  1.1  fvdl 		instrcount++;
    331  1.1  fvdl 	}
    332  1.1  fvdl 	fprintf(ofile, "};\n\n");
    333  1.1  fvdl 
    334  1.1  fvdl 	/*
    335  1.1  fvdl 	 *  Output patch information.  Patch functions first.
    336  1.1  fvdl 	 */
    337  1.1  fvdl 	for(cur_node = SLIST_FIRST(&patch_functions);
    338  1.1  fvdl 	    cur_node != NULL;
    339  1.1  fvdl 	    cur_node = SLIST_NEXT(cur_node,links)) {
    340  1.1  fvdl 		fprintf(ofile,
    341  1.1  fvdl "static int ahc_patch%d_func(struct ahc_softc *ahc);
    342  1.1  fvdl 
    343  1.1  fvdl static int
    344  1.1  fvdl ahc_patch%d_func(struct ahc_softc *ahc)
    345  1.1  fvdl {
    346  1.1  fvdl 	return (%s);
    347  1.1  fvdl }\n\n",
    348  1.1  fvdl 			cur_node->symbol->info.condinfo->func_num,
    349  1.1  fvdl 			cur_node->symbol->info.condinfo->func_num,
    350  1.1  fvdl 			cur_node->symbol->name);
    351  1.1  fvdl 	}
    352  1.1  fvdl 
    353  1.1  fvdl 	fprintf(ofile,
    354  1.1  fvdl "typedef int patch_func_t __P((struct ahc_softc *));
    355  1.1  fvdl struct patch {
    356  1.1  fvdl 	patch_func_t	*patch_func;
    357  1.1  fvdl 	u_int32_t	begin	   :10,
    358  1.1  fvdl 			skip_instr :10,
    359  1.1  fvdl 			skip_patch :12;
    360  1.1  fvdl } patches[] = {\n");
    361  1.1  fvdl 
    362  1.1  fvdl 	for(cur_patch = STAILQ_FIRST(&patches);
    363  1.1  fvdl 	    cur_patch != NULL;
    364  1.1  fvdl 	    cur_patch = STAILQ_NEXT(cur_patch,links)) {
    365  1.1  fvdl 		fprintf(ofile, "\t{ ahc_patch%d_func, %d, %d, %d },\n",
    366  1.1  fvdl 			cur_patch->patch_func, cur_patch->begin,
    367  1.1  fvdl 			cur_patch->skip_instr, cur_patch->skip_patch);
    368  1.1  fvdl 	}
    369  1.1  fvdl 
    370  1.1  fvdl 	fprintf(ofile, "\n};\n");
    371  1.1  fvdl 
    372  1.1  fvdl 	fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
    373  1.1  fvdl }
    374  1.1  fvdl 
    375  1.1  fvdl static void
    376  1.1  fvdl dump_scope(scope_t *scope)
    377  1.1  fvdl {
    378  1.1  fvdl 	scope_t *cur_scope;
    379  1.1  fvdl 
    380  1.1  fvdl 	/*
    381  1.1  fvdl 	 * Emit the first patch for this scope
    382  1.1  fvdl 	 */
    383  1.1  fvdl 	emit_patch(scope, 0);
    384  1.1  fvdl 
    385  1.1  fvdl 	/*
    386  1.1  fvdl 	 * Dump each scope within this one.
    387  1.1  fvdl 	 */
    388  1.1  fvdl 	cur_scope = TAILQ_FIRST(&scope->inner_scope);
    389  1.1  fvdl 
    390  1.1  fvdl 	while (cur_scope != NULL) {
    391  1.1  fvdl 
    392  1.1  fvdl 		dump_scope(cur_scope);
    393  1.1  fvdl 
    394  1.1  fvdl 		cur_scope = TAILQ_NEXT(cur_scope, scope_links);
    395  1.1  fvdl 	}
    396  1.1  fvdl 
    397  1.1  fvdl 	/*
    398  1.1  fvdl 	 * Emit the second, closing, patch for this scope
    399  1.1  fvdl 	 */
    400  1.1  fvdl 	emit_patch(scope, 1);
    401  1.1  fvdl }
    402  1.1  fvdl 
    403  1.1  fvdl void
    404  1.1  fvdl emit_patch(scope_t *scope, int patch)
    405  1.1  fvdl {
    406  1.1  fvdl 	patch_info_t *pinfo;
    407  1.1  fvdl 	patch_t *new_patch;
    408  1.1  fvdl 
    409  1.1  fvdl 	pinfo = &scope->patches[patch];
    410  1.1  fvdl 
    411  1.1  fvdl 	if (pinfo->skip_instr == 0)
    412  1.1  fvdl 		/* No-Op patch */
    413  1.1  fvdl 		return;
    414  1.1  fvdl 
    415  1.1  fvdl 	new_patch = (patch_t *)malloc(sizeof(*new_patch));
    416  1.1  fvdl 
    417  1.1  fvdl 	if (new_patch == NULL)
    418  1.1  fvdl 		stop("Could not malloc patch structure", EX_OSERR);
    419  1.1  fvdl 
    420  1.1  fvdl 	memset(new_patch, 0, sizeof(*new_patch));
    421  1.1  fvdl 
    422  1.1  fvdl 	if (patch == 0) {
    423  1.1  fvdl 		new_patch->patch_func = scope->func_num;
    424  1.1  fvdl 		new_patch->begin = scope->begin_addr;
    425  1.1  fvdl 	} else {
    426  1.1  fvdl 		new_patch->patch_func = 0;
    427  1.1  fvdl 		new_patch->begin = scope->end_addr;
    428  1.1  fvdl 	}
    429  1.1  fvdl 	new_patch->skip_instr = pinfo->skip_instr;
    430  1.1  fvdl 	new_patch->skip_patch = pinfo->skip_patch;
    431  1.1  fvdl 	STAILQ_INSERT_TAIL(&patches, new_patch, links);
    432  1.1  fvdl }
    433  1.1  fvdl 
    434  1.1  fvdl void
    435  1.1  fvdl output_listing(FILE *listfile, char *ifilename)
    436  1.1  fvdl {
    437  1.1  fvdl 	char buf[1024];
    438  1.1  fvdl 	FILE *ifile;
    439  1.1  fvdl 	struct instruction *cur_instr;
    440  1.1  fvdl 	patch_t *cur_patch;
    441  1.1  fvdl 	symbol_node_t *cur_func;
    442  1.1  fvdl 	int *func_values;
    443  1.1  fvdl 	int instrcount;
    444  1.1  fvdl 	int instrptr;
    445  1.1  fvdl 	int line;
    446  1.1  fvdl 	int func_count;
    447  1.1  fvdl 	int skip_addr;
    448  1.1  fvdl 
    449  1.1  fvdl 	instrcount = 0;
    450  1.1  fvdl 	instrptr = 0;
    451  1.1  fvdl 	line = 1;
    452  1.1  fvdl 	skip_addr = 0;
    453  1.1  fvdl 	if ((ifile = fopen(ifilename, "r")) == NULL) {
    454  1.1  fvdl 		perror(ifilename);
    455  1.1  fvdl 		stop(NULL, EX_DATAERR);
    456  1.1  fvdl 	}
    457  1.1  fvdl 
    458  1.1  fvdl 	/*
    459  1.1  fvdl 	 * Determine which options to apply to this listing.
    460  1.1  fvdl 	 */
    461  1.1  fvdl 	for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
    462  1.1  fvdl 	    cur_func != NULL;
    463  1.1  fvdl 	    cur_func = SLIST_NEXT(cur_func, links))
    464  1.1  fvdl 		func_count++;
    465  1.1  fvdl 
    466  1.1  fvdl 	if (func_count != 0) {
    467  1.1  fvdl 		func_values = (int *)malloc(func_count * sizeof(int));
    468  1.1  fvdl 
    469  1.1  fvdl 		if (func_values == NULL)
    470  1.1  fvdl 			stop("Could not malloc", EX_OSERR);
    471  1.1  fvdl 
    472  1.1  fvdl 		func_values[0] = 0; /* FALSE func */
    473  1.1  fvdl 		func_count--;
    474  1.1  fvdl 
    475  1.1  fvdl 		/*
    476  1.1  fvdl 		 * Ask the user to fill in the return values for
    477  1.1  fvdl 		 * the rest of the functions.
    478  1.1  fvdl 		 */
    479  1.1  fvdl 
    480  1.1  fvdl 
    481  1.1  fvdl 		for (cur_func = SLIST_FIRST(&patch_functions);
    482  1.1  fvdl 		     cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
    483  1.1  fvdl 		     cur_func = SLIST_NEXT(cur_func, links), func_count--) {
    484  1.1  fvdl 			int input;
    485  1.1  fvdl 
    486  1.1  fvdl 			fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
    487  1.1  fvdl 			fprintf(stdout,
    488  1.1  fvdl 				"Enter the return value for "
    489  1.1  fvdl 				"this expression[T/F]:");
    490  1.1  fvdl 
    491  1.1  fvdl 			while (1) {
    492  1.1  fvdl 
    493  1.1  fvdl 				input = getchar();
    494  1.1  fvdl 				input = toupper(input);
    495  1.1  fvdl 
    496  1.1  fvdl 				if (input == 'T') {
    497  1.1  fvdl 					func_values[func_count] = 1;
    498  1.1  fvdl 					break;
    499  1.1  fvdl 				} else if (input == 'F') {
    500  1.1  fvdl 					func_values[func_count] = 0;
    501  1.1  fvdl 					break;
    502  1.1  fvdl 				}
    503  1.1  fvdl 			}
    504  1.1  fvdl 			if (isatty(fileno(stdin)) == 0)
    505  1.1  fvdl 				putchar(input);
    506  1.1  fvdl 		}
    507  1.1  fvdl 		fprintf(stdout, "\nThanks!\n");
    508  1.1  fvdl 	}
    509  1.1  fvdl 
    510  1.1  fvdl 	/* Now output the listing */
    511  1.1  fvdl 	cur_patch = STAILQ_FIRST(&patches);
    512  1.1  fvdl 	for(cur_instr = STAILQ_FIRST(&seq_program);
    513  1.1  fvdl 	    cur_instr != NULL;
    514  1.1  fvdl 	    cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
    515  1.1  fvdl 
    516  1.1  fvdl 		if (check_patch(&cur_patch, instrcount,
    517  1.1  fvdl 				&skip_addr, func_values) == 0) {
    518  1.1  fvdl 			/* Don't count this instruction as it is in a patch
    519  1.1  fvdl 			 * that was removed.
    520  1.1  fvdl 			 */
    521  1.1  fvdl                         continue;
    522  1.1  fvdl 		}
    523  1.1  fvdl 
    524  1.1  fvdl 		while (line < cur_instr->srcline) {
    525  1.1  fvdl 			fgets(buf, sizeof(buf), ifile);
    526  1.1  fvdl 				fprintf(listfile, "\t\t%s", buf);
    527  1.1  fvdl 				line++;
    528  1.1  fvdl 		}
    529  1.1  fvdl 		fprintf(listfile, "%03x %02x%02x%02x%02x", instrptr,
    530  1.1  fvdl 			cur_instr->format.bytes[0],
    531  1.1  fvdl 			cur_instr->format.bytes[1],
    532  1.1  fvdl 			cur_instr->format.bytes[2],
    533  1.1  fvdl 			cur_instr->format.bytes[3]);
    534  1.1  fvdl 		fgets(buf, sizeof(buf), ifile);
    535  1.1  fvdl 		fprintf(listfile, "\t%s", buf);
    536  1.1  fvdl 		line++;
    537  1.1  fvdl 		instrptr++;
    538  1.1  fvdl 	}
    539  1.1  fvdl 	/* Dump the remainder of the file */
    540  1.1  fvdl 	while(fgets(buf, sizeof(buf), ifile) != NULL)
    541  1.1  fvdl 		fprintf(listfile, "\t\t%s", buf);
    542  1.1  fvdl 
    543  1.1  fvdl 	fclose(ifile);
    544  1.1  fvdl }
    545  1.1  fvdl 
    546  1.1  fvdl static int
    547  1.1  fvdl check_patch(patch_t **start_patch, int start_instr,
    548  1.1  fvdl 	    int *skip_addr, int *func_vals)
    549  1.1  fvdl {
    550  1.1  fvdl 	patch_t *cur_patch;
    551  1.1  fvdl 
    552  1.1  fvdl 	cur_patch = *start_patch;
    553  1.1  fvdl 
    554  1.1  fvdl 	while (cur_patch != NULL && start_instr == cur_patch->begin) {
    555  1.1  fvdl 		if (func_vals[cur_patch->patch_func] == 0) {
    556  1.1  fvdl 			int skip;
    557  1.1  fvdl 
    558  1.1  fvdl 			/* Start rejecting code */
    559  1.1  fvdl 			*skip_addr = start_instr + cur_patch->skip_instr;
    560  1.1  fvdl 			for (skip = cur_patch->skip_patch;
    561  1.1  fvdl 			     skip > 0 && cur_patch != NULL;
    562  1.1  fvdl 			     skip--)
    563  1.1  fvdl 				cur_patch = STAILQ_NEXT(cur_patch, links);
    564  1.1  fvdl 		} else {
    565  1.1  fvdl 			/* Accepted this patch.  Advance to the next
    566  1.1  fvdl 			 * one and wait for our intruction pointer to
    567  1.1  fvdl 			 * hit this point.
    568  1.1  fvdl 			 */
    569  1.1  fvdl 			cur_patch = STAILQ_NEXT(cur_patch, links);
    570  1.1  fvdl 		}
    571  1.1  fvdl 	}
    572  1.1  fvdl 
    573  1.1  fvdl 	*start_patch = cur_patch;
    574  1.1  fvdl 	if (start_instr < *skip_addr)
    575  1.1  fvdl 		/* Still skipping */
    576  1.1  fvdl 		return (0);
    577  1.1  fvdl 
    578  1.1  fvdl 	return (1);
    579  1.1  fvdl }
    580  1.1  fvdl 
    581  1.1  fvdl /*
    582  1.1  fvdl  * Print out error information if appropriate, and clean up before
    583  1.1  fvdl  * terminating the program.
    584  1.1  fvdl  */
    585  1.1  fvdl void
    586  1.1  fvdl stop(string, err_code)
    587  1.1  fvdl 	const char *string;
    588  1.1  fvdl 	int  err_code;
    589  1.1  fvdl {
    590  1.1  fvdl 	if (string != NULL) {
    591  1.1  fvdl 		fprintf(stderr, "%s: ", appname);
    592  1.1  fvdl 		if (yyfilename != NULL) {
    593  1.1  fvdl 			fprintf(stderr, "Stopped at file %s, line %d - ",
    594  1.1  fvdl 				yyfilename, yylineno);
    595  1.1  fvdl 		}
    596  1.1  fvdl 		fprintf(stderr, "%s\n", string);
    597  1.1  fvdl 	}
    598  1.1  fvdl 
    599  1.1  fvdl 	if (ofile != NULL) {
    600  1.1  fvdl 		fclose(ofile);
    601  1.1  fvdl 		if (err_code != 0) {
    602  1.1  fvdl 			fprintf(stderr, "%s: Removing %s due to error\n",
    603  1.1  fvdl 				appname, ofilename);
    604  1.1  fvdl 			unlink(ofilename);
    605  1.1  fvdl 		}
    606  1.1  fvdl 	}
    607  1.1  fvdl 
    608  1.1  fvdl 	if (regfile != NULL) {
    609  1.1  fvdl 		fclose(regfile);
    610  1.1  fvdl 		if (err_code != 0) {
    611  1.1  fvdl 			fprintf(stderr, "%s: Removing %s due to error\n",
    612  1.1  fvdl 				appname, regfilename);
    613  1.1  fvdl 			unlink(regfilename);
    614  1.1  fvdl 		}
    615  1.1  fvdl 	}
    616  1.1  fvdl 
    617  1.1  fvdl 	if (listfile != NULL) {
    618  1.1  fvdl 		fclose(listfile);
    619  1.1  fvdl 		if (err_code != 0) {
    620  1.1  fvdl 			fprintf(stderr, "%s: Removing %s due to error\n",
    621  1.1  fvdl 				appname, listfilename);
    622  1.1  fvdl 			unlink(listfilename);
    623  1.1  fvdl 		}
    624  1.1  fvdl 	}
    625  1.1  fvdl 
    626  1.1  fvdl 	symlist_free(&patch_functions);
    627  1.1  fvdl 	symtable_close();
    628  1.1  fvdl 
    629  1.1  fvdl 	exit(err_code);
    630  1.1  fvdl }
    631  1.1  fvdl 
    632  1.1  fvdl struct instruction *
    633  1.1  fvdl seq_alloc()
    634  1.1  fvdl {
    635  1.1  fvdl 	struct instruction *new_instr;
    636  1.1  fvdl 
    637  1.1  fvdl 	new_instr = (struct instruction *)malloc(sizeof(struct instruction));
    638  1.1  fvdl 	if (new_instr == NULL)
    639  1.1  fvdl 		stop("Unable to malloc instruction object", EX_SOFTWARE);
    640  1.1  fvdl 	memset(new_instr, 0, sizeof(*new_instr));
    641  1.1  fvdl 	STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
    642  1.1  fvdl 	new_instr->srcline = yylineno;
    643  1.1  fvdl 	return new_instr;
    644  1.1  fvdl }
    645  1.1  fvdl 
    646  1.1  fvdl scope_t *
    647  1.1  fvdl scope_alloc()
    648  1.1  fvdl {
    649  1.1  fvdl 	scope_t *new_scope;
    650  1.1  fvdl 
    651  1.1  fvdl 	new_scope = (scope_t *)malloc(sizeof(scope_t));
    652  1.1  fvdl 	if (new_scope == NULL)
    653  1.1  fvdl 		stop("Unable to malloc scope object", EX_SOFTWARE);
    654  1.1  fvdl 	memset(new_scope, 0, sizeof(*new_scope));
    655  1.1  fvdl 	TAILQ_INIT(&new_scope->inner_scope);
    656  1.1  fvdl 
    657  1.1  fvdl 	if (SLIST_FIRST(&scope_stack) != NULL) {
    658  1.1  fvdl 		TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
    659  1.1  fvdl 				  new_scope, scope_links);
    660  1.1  fvdl 	}
    661  1.1  fvdl 	/* This patch is now the current scope */
    662  1.1  fvdl 	SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
    663  1.1  fvdl 	return new_scope;
    664  1.1  fvdl }
    665  1.1  fvdl 
    666  1.1  fvdl void
    667  1.1  fvdl process_scope(scope_t *scope)
    668  1.1  fvdl {
    669  1.1  fvdl 	/*
    670  1.1  fvdl 	 * We are "leaving" this scope.  We should now have
    671  1.1  fvdl 	 * enough information to process the lists of scopes
    672  1.1  fvdl 	 * we encapsulate.
    673  1.1  fvdl 	 */
    674  1.1  fvdl 	scope_t *cur_scope;
    675  1.1  fvdl 	u_int skip_patch_count;
    676  1.1  fvdl 	u_int skip_instr_count;
    677  1.1  fvdl 
    678  1.1  fvdl 	cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
    679  1.1  fvdl 	skip_patch_count = 0;
    680  1.1  fvdl 	skip_instr_count = 0;
    681  1.1  fvdl 	while (cur_scope != NULL) {
    682  1.1  fvdl 		u_int patch0_patch_skip;
    683  1.1  fvdl 
    684  1.1  fvdl 		patch0_patch_skip = 0;
    685  1.1  fvdl 		switch (cur_scope->type) {
    686  1.1  fvdl 		case SCOPE_IF:
    687  1.1  fvdl 		case SCOPE_ELSE_IF:
    688  1.1  fvdl 			if (skip_instr_count != 0) {
    689  1.1  fvdl 				/* Create a tail patch */
    690  1.1  fvdl 				patch0_patch_skip++;
    691  1.1  fvdl 				cur_scope->patches[1].skip_patch =
    692  1.1  fvdl 				    skip_patch_count + 1;
    693  1.1  fvdl 				cur_scope->patches[1].skip_instr =
    694  1.1  fvdl 				    skip_instr_count;
    695  1.1  fvdl 			}
    696  1.1  fvdl 
    697  1.1  fvdl 			/* Count Head patch */
    698  1.1  fvdl 			patch0_patch_skip++;
    699  1.1  fvdl 
    700  1.1  fvdl 			/* Count any patches contained in our inner scope */
    701  1.1  fvdl 			patch0_patch_skip += cur_scope->inner_scope_patches;
    702  1.1  fvdl 
    703  1.1  fvdl 			cur_scope->patches[0].skip_patch = patch0_patch_skip;
    704  1.1  fvdl 			cur_scope->patches[0].skip_instr =
    705  1.1  fvdl 			    cur_scope->end_addr - cur_scope->begin_addr;
    706  1.1  fvdl 
    707  1.1  fvdl 			skip_instr_count += cur_scope->patches[0].skip_instr;
    708  1.1  fvdl 
    709  1.1  fvdl 			skip_patch_count += patch0_patch_skip;
    710  1.1  fvdl 			if (cur_scope->type == SCOPE_IF) {
    711  1.1  fvdl 				scope->inner_scope_patches += skip_patch_count;
    712  1.1  fvdl 				skip_patch_count = 0;
    713  1.1  fvdl 			        skip_instr_count = 0;
    714  1.1  fvdl 			}
    715  1.1  fvdl 			break;
    716  1.1  fvdl 		case SCOPE_ELSE:
    717  1.1  fvdl 			/* Count any patches contained in our innter scope */
    718  1.1  fvdl 			skip_patch_count += cur_scope->inner_scope_patches;
    719  1.1  fvdl 
    720  1.1  fvdl 			skip_instr_count += cur_scope->end_addr
    721  1.1  fvdl 					  - cur_scope->begin_addr;
    722  1.1  fvdl 			break;
    723  1.1  fvdl 		case SCOPE_ROOT:
    724  1.1  fvdl 			stop("Unexpected scope type encountered", EX_SOFTWARE);
    725  1.1  fvdl 			/* NOTREACHED */
    726  1.1  fvdl 		}
    727  1.1  fvdl 
    728  1.1  fvdl 		cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);
    729  1.1  fvdl 	}
    730  1.1  fvdl }
    731