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