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