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