aic7xxx: fix a pointer sign warning
[dragonfly.git] / sys / dev / disk / aic7xxx / aicasm / aicasm.c
1 /*
2  * Aic7xxx SCSI host adapter firmware asssembler
3  *
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  *
40  * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm.c#23 $
41  *
42  * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm.c,v 1.36 2003/12/17 00:01:11 gibbs Exp $
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
66 typedef 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
74 STAILQ_HEAD(patch_list, patch) patches;
75
76 static void usage(void);
77 static void back_patch(void);
78 static void output_code(void);
79 static void output_listing(char *ifilename);
80 static void dump_scope(scope_t *scope);
81 static void emit_patch(scope_t *scope, int patch);
82 static int check_patch(patch_t **start_patch, unsigned int start_instr,
83                        unsigned int *skip_addr, int *func_vals);
84
85 struct path_list search_path;
86 int includes_search_curdir;
87 char *appname;
88 char *stock_include_file;
89 FILE *ofile;
90 char *ofilename;
91 char *regfilename;
92 FILE *regfile;
93 char *listfilename;
94 FILE *listfile;
95 char *regdiagfilename;
96 FILE *regdiagfile;
97 int   src_mode;
98 int   dst_mode;
99
100 static STAILQ_HEAD(,instruction) seq_program;
101 struct cs_tailq cs_tailq;
102 struct scope_list scope_stack;
103 symlist_t patch_functions;
104
105 #if DEBUG
106 extern int yy_flex_debug;
107 extern int mm_flex_debug;
108 extern int yydebug;
109 extern int mmdebug;
110 #endif
111 extern FILE *yyin;
112 extern int yyparse(void);
113
114 int main(int argc, char *argv[]);
115
116 int
117 main(int argc, char *argv[])
118 {
119         int  ch;
120         int  retval;
121         char *inputfilename;
122         scope_t *sentinal;
123
124         STAILQ_INIT(&patches);
125         SLIST_INIT(&search_path);
126         STAILQ_INIT(&seq_program);
127         TAILQ_INIT(&cs_tailq);
128         SLIST_INIT(&scope_stack);
129
130         /* Set Sentinal scope node */
131         sentinal = scope_alloc();
132         sentinal->type = SCOPE_ROOT;
133         
134         includes_search_curdir = 1;
135         appname = *argv;
136         regfile = NULL;
137         listfile = NULL;
138 #if DEBUG
139         yy_flex_debug = 0;
140         mm_flex_debug = 0;
141         yydebug = 0;
142         mmdebug = 0;
143 #endif
144         while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:")) != -1) {
145                 switch(ch) {
146                 case 'd':
147 #if DEBUG
148                         if (strcmp(optarg, "s") == 0) {
149                                 yy_flex_debug = 1;
150                                 mm_flex_debug = 1;
151                         } else if (strcmp(optarg, "p") == 0) {
152                                 yydebug = 1;
153                                 mmdebug = 1;
154                         } else {
155                                 fprintf(stderr, "%s: -d Requires either an "
156                                         "'s' or 'p' argument\n", appname);
157                                 usage();
158                         }
159 #else
160                         stop("-d: Assembler not built with debugging "
161                              "information", EX_SOFTWARE);
162 #endif
163                         break;
164                 case 'i':
165                         stock_include_file = optarg;
166                         break;
167                 case 'l':
168                         /* Create a program listing */
169                         if ((listfile = fopen(optarg, "w")) == NULL) {
170                                 perror(optarg);
171                                 stop(NULL, EX_CANTCREAT);
172                         }
173                         listfilename = optarg;
174                         break;
175                 case 'n':
176                         /* Don't complain about the -nostdinc directrive */
177                         if (strcmp(optarg, "ostdinc")) {
178                                 fprintf(stderr, "%s: Unknown option -%c%s\n",
179                                         appname, ch, optarg);
180                                 usage();
181                                 /* NOTREACHED */
182                         }
183                         break;
184                 case 'o':
185                         if ((ofile = fopen(optarg, "w")) == NULL) {
186                                 perror(optarg);
187                                 stop(NULL, EX_CANTCREAT);
188                         }
189                         ofilename = optarg;
190                         break;
191                 case 'p':
192                         /* Create Register Diagnostic "printing" Functions */
193                         if ((regdiagfile = fopen(optarg, "w")) == NULL) {
194                                 perror(optarg);
195                                 stop(NULL, EX_CANTCREAT);
196                         }
197                         regdiagfilename = optarg;
198                         break;
199                 case 'r':
200                         if ((regfile = fopen(optarg, "w")) == NULL) {
201                                 perror(optarg);
202                                 stop(NULL, EX_CANTCREAT);
203                         }
204                         regfilename = optarg;
205                         break;
206                 case 'I':
207                 {
208                         path_entry_t include_dir;
209
210                         if (strcmp(optarg, "-") == 0) {
211                                 if (includes_search_curdir == 0) {
212                                         fprintf(stderr, "%s: Warning - '-I-' "
213                                                         "specified multiple "
214                                                         "times\n", appname);
215                                 }
216                                 includes_search_curdir = 0;
217                                 for (include_dir = SLIST_FIRST(&search_path);
218                                      include_dir != NULL;
219                                      include_dir = SLIST_NEXT(include_dir,
220                                                               links))
221                                         /*
222                                          * All entries before a '-I-' only
223                                          * apply to includes specified with
224                                          * quotes instead of "<>".
225                                          */
226                                         include_dir->quoted_includes_only = 1;
227                         } else {
228                                 include_dir =
229                                     (path_entry_t)malloc(sizeof(*include_dir));
230                                 if (include_dir == NULL) {
231                                         perror(optarg);
232                                         stop(NULL, EX_OSERR);
233                                 }
234                                 include_dir->directory = strdup(optarg);
235                                 if (include_dir->directory == NULL) {
236                                         perror(optarg);
237                                         stop(NULL, EX_OSERR);
238                                 }
239                                 include_dir->quoted_includes_only = 0;
240                                 SLIST_INSERT_HEAD(&search_path, include_dir,
241                                                   links);
242                         }
243                         break;
244                 }
245                 case '?':
246                 default:
247                         usage();
248                         /* NOTREACHED */
249                 }
250         }
251         argc -= optind;
252         argv += optind;
253
254         if (argc != 1) {
255                 fprintf(stderr, "%s: No input file specifiled\n", appname);
256                 usage();
257                 /* NOTREACHED */
258         }
259
260         if (regdiagfile != NULL
261          && (regfile == NULL || stock_include_file == NULL)) {
262                 fprintf(stderr,
263                         "%s: The -p option requires the -r and -i options.\n",
264                         appname);
265                 usage();
266                 /* NOTREACHED */
267         }
268         symtable_open();
269         inputfilename = *argv;
270         include_file(*argv, SOURCE_FILE);
271         retval = yyparse();
272         if (retval == 0) {
273                 if (SLIST_FIRST(&scope_stack) == NULL
274                  || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
275                         stop("Unterminated conditional expression", EX_DATAERR);
276                         /* NOTREACHED */
277                 }
278
279                 /* Process outmost scope */
280                 process_scope(SLIST_FIRST(&scope_stack));
281                 /*
282                  * Decend the tree of scopes and insert/emit
283                  * patches as appropriate.  We perform a depth first
284                  * tranversal, recursively handling each scope.
285                  */
286                 /* start at the root scope */
287                 dump_scope(SLIST_FIRST(&scope_stack));
288
289                 /* Patch up forward jump addresses */
290                 back_patch();
291
292                 if (ofile != NULL)
293                         output_code();
294                 if (regfile != NULL)
295                         symtable_dump(regfile, regdiagfile);
296                 if (listfile != NULL)
297                         output_listing(inputfilename);
298         }
299
300         stop(NULL, 0);
301         /* NOTREACHED */
302         return (0);
303 }
304
305 static void
306 usage(void)
307 {
308
309         (void)fprintf(stderr,
310 "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]\n"
311 "       [-r register_output_file [-p register_diag_file -i includefile]]\n"
312 "       [-l program_list_file]\n"
313 "       input_file\n", appname);
314         exit(EX_USAGE);
315 }
316
317 static void
318 back_patch(void)
319 {
320         struct instruction *cur_instr;
321
322         for (cur_instr = STAILQ_FIRST(&seq_program);
323              cur_instr != NULL;
324              cur_instr = STAILQ_NEXT(cur_instr, links)) {
325                 if (cur_instr->patch_label != NULL) {
326                         struct ins_format3 *f3_instr;
327                         u_int address;
328
329                         if (cur_instr->patch_label->type != LABEL) {
330                                 char buf[255];
331
332                                 snprintf(buf, sizeof(buf),
333                                          "Undefined label %s",
334                                          cur_instr->patch_label->name);
335                                 stop(buf, EX_DATAERR);
336                                 /* NOTREACHED */
337                         }
338                         f3_instr = &cur_instr->format.format3;
339                         address = f3_instr->address;
340                         address += cur_instr->patch_label->info.linfo->address;
341                         f3_instr->address = address;
342                 }
343         }
344 }
345
346 static void
347 output_code(void)
348 {
349         struct instruction *cur_instr;
350         patch_t *cur_patch;
351         critical_section_t *cs;
352         symbol_node_t *cur_node;
353         int instrcount;
354
355         instrcount = 0;
356         fprintf(ofile,
357 "/*\n"
358 " * DO NOT EDIT - This file is automatically generated\n"
359 " *              from the following source files:\n"
360 " *\n"
361 "%s */\n", versions);
362
363         fprintf(ofile, "static uint8_t seqprog[] = {\n");
364         for (cur_instr = STAILQ_FIRST(&seq_program);
365              cur_instr != NULL;
366              cur_instr = STAILQ_NEXT(cur_instr, links)) {
367
368                 fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
369                         cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n",
370 #if BYTE_ORDER == LITTLE_ENDIAN
371                         cur_instr->format.bytes[0],
372                         cur_instr->format.bytes[1],
373                         cur_instr->format.bytes[2],
374                         cur_instr->format.bytes[3]);
375 #else
376                         cur_instr->format.bytes[3],
377                         cur_instr->format.bytes[2],
378                         cur_instr->format.bytes[1],
379                         cur_instr->format.bytes[0]);
380 #endif
381                 instrcount++;
382         }
383         fprintf(ofile, "\n};\n\n");
384
385         if (patch_arg_list == NULL)
386                 stop("Patch argument list not defined",
387                      EX_DATAERR);
388
389         /*
390          *  Output patch information.  Patch functions first.
391          */
392         fprintf(ofile,
393 "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
394
395         for (cur_node = SLIST_FIRST(&patch_functions);
396              cur_node != NULL;
397              cur_node = SLIST_NEXT(cur_node,links)) {
398                 fprintf(ofile,
399 "static %spatch_func_t %spatch%d_func;\n"
400 "\n"
401 "static int\n"
402 "%spatch%d_func(%s)\n"
403 "{\n"
404 "       return (%s);\n"
405 "}\n\n",
406                         prefix,
407                         prefix,
408                         cur_node->symbol->info.condinfo->func_num,
409                         prefix,
410                         cur_node->symbol->info.condinfo->func_num,
411                         patch_arg_list,
412                         cur_node->symbol->name);
413         }
414
415         fprintf(ofile,
416 "static struct patch {\n"
417 "       %spatch_func_t          *patch_func;\n"
418 "       uint32_t                 begin          :10,\n"
419 "                                skip_instr     :10,\n"
420 "                                skip_patch     :12;\n"
421 "} patches[] = {\n", prefix);
422
423         for (cur_patch = STAILQ_FIRST(&patches);
424              cur_patch != NULL;
425              cur_patch = STAILQ_NEXT(cur_patch,links)) {
426                 fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }",
427                         cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n",
428                         prefix,
429                         cur_patch->patch_func, cur_patch->begin,
430                         cur_patch->skip_instr, cur_patch->skip_patch);
431         }
432
433         fprintf(ofile, "\n};\n\n");
434
435         fprintf(ofile,
436 "static struct cs {\n"
437 "       uint16_t        begin;\n"
438 "       uint16_t        end;\n"
439 "} critical_sections[] = {\n");
440
441         for (cs = TAILQ_FIRST(&cs_tailq);
442              cs != NULL;
443              cs = TAILQ_NEXT(cs, links)) {
444                 fprintf(ofile, "%s\t{ %d, %d }",
445                         cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n",
446                         cs->begin_addr, cs->end_addr);
447         }
448
449         fprintf(ofile, "\n};\n\n");
450
451         fprintf(ofile,
452 "static const int num_critical_sections = sizeof(critical_sections)\n"
453 "                                      / sizeof(*critical_sections);\n");
454
455         fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
456 }
457
458 static void
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
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         STAILQ_INSERT_TAIL(&patches, new_patch, links);
515 }
516
517 void
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         unsigned int line;
529         int func_count;
530         unsigned 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 = STAILQ_FIRST(&patches);
596         for (cur_instr = STAILQ_FIRST(&seq_program);
597              cur_instr != NULL;
598              cur_instr = STAILQ_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         free(func_values);
640
641         /* Dump the remainder of the file */
642         while(fgets(buf, sizeof(buf), ifile) != NULL)
643                 fprintf(listfile, "             %s", buf);
644
645         fclose(ifile);
646 }
647
648 static int
649 check_patch(patch_t **start_patch, unsigned int start_instr,
650             unsigned 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  */
687 void
688 stop(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
732 struct instruction *
733 seq_alloc(void)
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
746 critical_section_t *
747 cs_alloc(void)
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
760 scope_t *
761 scope_alloc(void)
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
780 void
781 process_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 }