aicasm.c revision 1.7 1 /* $NetBSD: aicasm.c,v 1.7 2009/03/18 10:22:40 cegger 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.7 2009/03/18 10:22:40 cegger 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 fprintf(stdout, "\nThanks!\n");
598 }
599
600 /* Now output the listing */
601 cur_patch = STAILQ_FIRST(&patches);
602 for (cur_instr = STAILQ_FIRST(&seq_program);
603 cur_instr != NULL;
604 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
605
606 if (check_patch(&cur_patch, instrcount,
607 &skip_addr, func_values) == 0) {
608 /* Don't count this instruction as it is in a patch
609 * that was removed.
610 */
611 continue;
612 }
613
614 while (line < cur_instr->srcline) {
615 fgets(buf, sizeof(buf), ifile);
616 fprintf(listfile, "\t\t%s", buf);
617 line++;
618 }
619 fprintf(listfile, "%03x %02x%02x%02x%02x", instrptr,
620 #if BYTE_ORDER == LITTLE_ENDIAN
621 cur_instr->format.bytes[0],
622 cur_instr->format.bytes[1],
623 cur_instr->format.bytes[2],
624 cur_instr->format.bytes[3]);
625 #else
626 cur_instr->format.bytes[3],
627 cur_instr->format.bytes[2],
628 cur_instr->format.bytes[1],
629 cur_instr->format.bytes[0]);
630 #endif
631 fgets(buf, sizeof(buf), ifile);
632 fprintf(listfile, "\t%s", buf);
633 line++;
634 instrptr++;
635 }
636 /* Dump the remainder of the file */
637 while(fgets(buf, sizeof(buf), ifile) != NULL)
638 fprintf(listfile, "\t\t%s", buf);
639
640 fclose(ifile);
641 }
642
643 static int
644 check_patch(patch_t **start_patch, int start_instr,
645 int *skip_addr, int *func_vals)
646 {
647 patch_t *cur_patch;
648
649 cur_patch = *start_patch;
650
651 while (cur_patch != NULL && start_instr == cur_patch->begin) {
652 if (func_vals[cur_patch->patch_func] == 0) {
653 int skip;
654
655 /* Start rejecting code */
656 *skip_addr = start_instr + cur_patch->skip_instr;
657 for (skip = cur_patch->skip_patch;
658 skip > 0 && cur_patch != NULL;
659 skip--)
660 cur_patch = STAILQ_NEXT(cur_patch, links);
661 } else {
662 /* Accepted this patch. Advance to the next
663 * one and wait for our intruction pointer to
664 * hit this point.
665 */
666 cur_patch = STAILQ_NEXT(cur_patch, links);
667 }
668 }
669
670 *start_patch = cur_patch;
671 if (start_instr < *skip_addr)
672 /* Still skipping */
673 return (0);
674
675 return (1);
676 }
677
678 /*
679 * Print out error information if appropriate, and clean up before
680 * terminating the program.
681 */
682 void
683 stop(const char *string, int err_code)
684 {
685 if (string != NULL) {
686 fprintf(stderr, "%s: ", appname);
687 if (yyfilename != NULL) {
688 fprintf(stderr, "Stopped at file %s, line %d - ",
689 yyfilename, yylineno);
690 }
691 fprintf(stderr, "%s\n", string);
692 }
693
694 if (ofile != NULL) {
695 fclose(ofile);
696 if (err_code != 0) {
697 fprintf(stderr, "%s: Removing %s due to error\n",
698 appname, ofilename);
699 unlink(ofilename);
700 }
701 }
702
703 if (regfile != NULL) {
704 fclose(regfile);
705 if (err_code != 0) {
706 fprintf(stderr, "%s: Removing %s due to error\n",
707 appname, regfilename);
708 unlink(regfilename);
709 }
710 }
711
712 if (listfile != NULL) {
713 fclose(listfile);
714 if (err_code != 0) {
715 fprintf(stderr, "%s: Removing %s due to error\n",
716 appname, listfilename);
717 unlink(listfilename);
718 }
719 }
720
721 symlist_free(&patch_functions);
722 symtable_close();
723
724 exit(err_code);
725 }
726
727 struct instruction *
728 seq_alloc(void)
729 {
730 struct instruction *new_instr;
731
732 new_instr = (struct instruction *)malloc(sizeof(struct instruction));
733 if (new_instr == NULL)
734 stop("Unable to malloc instruction object", EX_SOFTWARE);
735 memset(new_instr, 0, sizeof(*new_instr));
736 STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
737 new_instr->srcline = yylineno;
738 return new_instr;
739 }
740
741 critical_section_t *
742 cs_alloc(void)
743 {
744 critical_section_t *new_cs;
745
746 new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
747 if (new_cs == NULL)
748 stop("Unable to malloc critical_section object", EX_SOFTWARE);
749 memset(new_cs, 0, sizeof(*new_cs));
750
751 TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
752 return new_cs;
753 }
754
755 scope_t *
756 scope_alloc(void)
757 {
758 scope_t *new_scope;
759
760 new_scope = (scope_t *)malloc(sizeof(scope_t));
761 if (new_scope == NULL)
762 stop("Unable to malloc scope object", EX_SOFTWARE);
763 memset(new_scope, 0, sizeof(*new_scope));
764 TAILQ_INIT(&new_scope->inner_scope);
765
766 if (SLIST_FIRST(&scope_stack) != NULL) {
767 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
768 new_scope, scope_links);
769 }
770 /* This patch is now the current scope */
771 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
772 return new_scope;
773 }
774
775 void
776 process_scope(scope_t *scope)
777 {
778 /*
779 * We are "leaving" this scope. We should now have
780 * enough information to process the lists of scopes
781 * we encapsulate.
782 */
783 scope_t *cur_scope;
784 u_int skip_patch_count;
785 u_int skip_instr_count;
786
787 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
788 skip_patch_count = 0;
789 skip_instr_count = 0;
790 while (cur_scope != NULL) {
791 u_int patch0_patch_skip;
792
793 patch0_patch_skip = 0;
794 switch (cur_scope->type) {
795 case SCOPE_IF:
796 case SCOPE_ELSE_IF:
797 if (skip_instr_count != 0) {
798 /* Create a tail patch */
799 patch0_patch_skip++;
800 cur_scope->patches[1].skip_patch =
801 skip_patch_count + 1;
802 cur_scope->patches[1].skip_instr =
803 skip_instr_count;
804 }
805
806 /* Count Head patch */
807 patch0_patch_skip++;
808
809 /* Count any patches contained in our inner scope */
810 patch0_patch_skip += cur_scope->inner_scope_patches;
811
812 cur_scope->patches[0].skip_patch = patch0_patch_skip;
813 cur_scope->patches[0].skip_instr =
814 cur_scope->end_addr - cur_scope->begin_addr;
815
816 skip_instr_count += cur_scope->patches[0].skip_instr;
817
818 skip_patch_count += patch0_patch_skip;
819 if (cur_scope->type == SCOPE_IF) {
820 scope->inner_scope_patches += skip_patch_count;
821 skip_patch_count = 0;
822 skip_instr_count = 0;
823 }
824 break;
825 case SCOPE_ELSE:
826 /* Count any patches contained in our innter scope */
827 skip_patch_count += cur_scope->inner_scope_patches;
828
829 skip_instr_count += cur_scope->end_addr
830 - cur_scope->begin_addr;
831 break;
832 case SCOPE_ROOT:
833 stop("Unexpected scope type encountered", EX_SOFTWARE);
834 /* NOTREACHED */
835 }
836
837 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);
838 }
839 }
840