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