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