0a364ed0ebe5813bad26b99fca0b36d4aa68526d
[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 extern int yylineno;
114
115 int main(int argc, char *argv[]);
116
117 int
118 main(int argc, char *argv[])
119 {
120         int  ch;
121         int  retval;
122         char *inputfilename;
123         scope_t *sentinal;
124
125         STAILQ_INIT(&patches);
126         SLIST_INIT(&search_path);
127         STAILQ_INIT(&seq_program);
128         TAILQ_INIT(&cs_tailq);
129         SLIST_INIT(&scope_stack);
130
131         /* Set Sentinal scope node */
132         sentinal = scope_alloc();
133         sentinal->type = SCOPE_ROOT;
134         
135         includes_search_curdir = 1;
136         appname = *argv;
137         regfile = NULL;
138         listfile = NULL;
139 #if DEBUG
140         yy_flex_debug = 0;
141         mm_flex_debug = 0;
142         yydebug = 0;
143         mmdebug = 0;
144 #endif
145         while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:")) != -1) {
146                 switch(ch) {
147                 case 'd':
148 #if DEBUG
149                         if (strcmp(optarg, "s") == 0) {
150                                 yy_flex_debug = 1;
151                                 mm_flex_debug = 1;
152                         } else if (strcmp(optarg, "p") == 0) {
153                                 yydebug = 1;
154                                 mmdebug = 1;
155                         } else {
156                                 fprintf(stderr, "%s: -d Requires either an "
157                                         "'s' or 'p' argument\n", appname);
158                                 usage();
159                         }
160 #else
161                         stop("-d: Assembler not built with debugging "
162                              "information", EX_SOFTWARE);
163 #endif
164                         break;
165                 case 'i':
166                         stock_include_file = optarg;
167                         break;
168                 case 'l':
169                         /* Create a program listing */
170                         if ((listfile = fopen(optarg, "w")) == NULL) {
171                                 perror(optarg);
172                                 stop(NULL, EX_CANTCREAT);
173                         }
174                         listfilename = optarg;
175                         break;
176                 case 'n':
177                         /* Don't complain about the -nostdinc directrive */
178                         if (strcmp(optarg, "ostdinc")) {
179                                 fprintf(stderr, "%s: Unknown option -%c%s\n",
180                                         appname, ch, optarg);
181                                 usage();
182                                 /* NOTREACHED */
183                         }
184                         break;
185                 case 'o':
186                         if ((ofile = fopen(optarg, "w")) == NULL) {
187                                 perror(optarg);
188                                 stop(NULL, EX_CANTCREAT);
189                         }
190                         ofilename = optarg;
191                         break;
192                 case 'p':
193                         /* Create Register Diagnostic "printing" Functions */
194                         if ((regdiagfile = fopen(optarg, "w")) == NULL) {
195                                 perror(optarg);
196                                 stop(NULL, EX_CANTCREAT);
197                         }
198                         regdiagfilename = optarg;
199                         break;
200                 case 'r':
201                         if ((regfile = fopen(optarg, "w")) == NULL) {
202                                 perror(optarg);
203                                 stop(NULL, EX_CANTCREAT);
204                         }
205                         regfilename = optarg;
206                         break;
207                 case 'I':
208                 {
209                         path_entry_t include_dir;
210
211                         if (strcmp(optarg, "-") == 0) {
212                                 if (includes_search_curdir == 0) {
213                                         fprintf(stderr, "%s: Warning - '-I-' "
214                                                         "specified multiple "
215                                                         "times\n", appname);
216                                 }
217                                 includes_search_curdir = 0;
218                                 for (include_dir = SLIST_FIRST(&search_path);
219                                      include_dir != NULL;
220                                      include_dir = SLIST_NEXT(include_dir,
221                                                               links))
222                                         /*
223                                          * All entries before a '-I-' only
224                                          * apply to includes specified with
225                                          * quotes instead of "<>".
226                                          */
227                                         include_dir->quoted_includes_only = 1;
228                         } else {
229                                 include_dir =
230                                     (path_entry_t)malloc(sizeof(*include_dir));
231                                 if (include_dir == NULL) {
232                                         perror(optarg);
233                                         stop(NULL, EX_OSERR);
234                                 }
235                                 include_dir->directory = strdup(optarg);
236                                 if (include_dir->directory == NULL) {
237                                         perror(optarg);
238                                         stop(NULL, EX_OSERR);
239                                 }
240                                 include_dir->quoted_includes_only = 0;
241                                 SLIST_INSERT_HEAD(&search_path, include_dir,
242                                                   links);
243                         }
244                         break;
245                 }
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 specifiled\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
307 usage(void)
308 {
309
310         (void)fprintf(stderr,
311 "usage: %-16s [-nostdinc] [-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
319 back_patch(void)
320 {
321         struct instruction *cur_instr;
322
323         for (cur_instr = STAILQ_FIRST(&seq_program);
324              cur_instr != NULL;
325              cur_instr = STAILQ_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
348 output_code(void)
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 uint8_t seqprog[] = {\n");
365         for (cur_instr = STAILQ_FIRST(&seq_program);
366              cur_instr != NULL;
367              cur_instr = STAILQ_NEXT(cur_instr, links)) {
368
369                 fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
370                         cur_instr == STAILQ_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 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 = STAILQ_FIRST(&patches);
425              cur_patch != NULL;
426              cur_patch = STAILQ_NEXT(cur_patch,links)) {
427                 fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }",
428                         cur_patch == STAILQ_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 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 "static const int num_critical_sections = sizeof(critical_sections)\n"
454 "                                      / sizeof(*critical_sections);\n");
455
456         fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
457 }
458
459 static void
460 dump_scope(scope_t *scope)
461 {
462         scope_t *cur_scope;
463
464         /*
465          * Emit the first patch for this scope
466          */
467         emit_patch(scope, 0);
468
469         /*
470          * Dump each scope within this one.
471          */
472         cur_scope = TAILQ_FIRST(&scope->inner_scope);
473
474         while (cur_scope != NULL) {
475
476                 dump_scope(cur_scope);
477
478                 cur_scope = TAILQ_NEXT(cur_scope, scope_links);
479         }
480
481         /*
482          * Emit the second, closing, patch for this scope
483          */
484         emit_patch(scope, 1);
485 }
486
487 void
488 emit_patch(scope_t *scope, int patch)
489 {
490         patch_info_t *pinfo;
491         patch_t *new_patch;
492
493         pinfo = &scope->patches[patch];
494
495         if (pinfo->skip_instr == 0)
496                 /* No-Op patch */
497                 return;
498
499         new_patch = (patch_t *)malloc(sizeof(*new_patch));
500
501         if (new_patch == NULL)
502                 stop("Could not malloc patch structure", EX_OSERR);
503
504         memset(new_patch, 0, sizeof(*new_patch));
505
506         if (patch == 0) {
507                 new_patch->patch_func = scope->func_num;
508                 new_patch->begin = scope->begin_addr;
509         } else {
510                 new_patch->patch_func = 0;
511                 new_patch->begin = scope->end_addr;
512         }
513         new_patch->skip_instr = pinfo->skip_instr;
514         new_patch->skip_patch = pinfo->skip_patch;
515         STAILQ_INSERT_TAIL(&patches, new_patch, links);
516 }
517
518 void
519 output_listing(char *ifilename)
520 {
521         char buf[1024];
522         FILE *ifile;
523         struct instruction *cur_instr;
524         patch_t *cur_patch;
525         symbol_node_t *cur_func;
526         int *func_values;
527         int instrcount;
528         int instrptr;
529         unsigned int line;
530         int func_count;
531         unsigned int skip_addr;
532
533         instrcount = 0;
534         instrptr = 0;
535         line = 1;
536         skip_addr = 0;
537         if ((ifile = fopen(ifilename, "r")) == NULL) {
538                 perror(ifilename);
539                 stop(NULL, EX_DATAERR);
540         }
541
542         /*
543          * Determine which options to apply to this listing.
544          */
545         for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
546             cur_func != NULL;
547             cur_func = SLIST_NEXT(cur_func, links))
548                 func_count++;
549
550         func_values = NULL;
551         if (func_count != 0) {
552                 func_values = (int *)malloc(func_count * sizeof(int));
553
554                 if (func_values == NULL)
555                         stop("Could not malloc", EX_OSERR);
556                 
557                 func_values[0] = 0; /* FALSE func */
558                 func_count--;
559
560                 /*
561                  * Ask the user to fill in the return values for
562                  * the rest of the functions.
563                  */
564                 
565                 
566                 for (cur_func = SLIST_FIRST(&patch_functions);
567                      cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
568                      cur_func = SLIST_NEXT(cur_func, links), func_count--) {
569                         int input;
570                         
571                         fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
572                         fprintf(stdout,
573                                 "Enter the return value for "
574                                 "this expression[T/F]:");
575
576                         while (1) {
577
578                                 input = getchar();
579                                 input = toupper(input);
580
581                                 if (input == 'T') {
582                                         func_values[func_count] = 1;
583                                         break;
584                                 } else if (input == 'F') {
585                                         func_values[func_count] = 0;
586                                         break;
587                                 }
588                         }
589                         if (isatty(fileno(stdin)) == 0)
590                                 putchar(input);
591                 }
592                 fprintf(stdout, "\nThanks!\n");
593         }
594
595         /* Now output the listing */
596         cur_patch = STAILQ_FIRST(&patches);
597         for (cur_instr = STAILQ_FIRST(&seq_program);
598              cur_instr != NULL;
599              cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
600
601                 if (check_patch(&cur_patch, instrcount,
602                                 &skip_addr, func_values) == 0) {
603                         /* Don't count this instruction as it is in a patch
604                          * that was removed.
605                          */
606                         continue;
607                 }
608
609                 while (line < cur_instr->srcline) {
610                         fgets(buf, sizeof(buf), ifile);
611                                 fprintf(listfile, "             \t%s", buf);
612                                 line++;
613                 }
614                 fprintf(listfile, "%04x %02x%02x%02x%02x", instrptr,
615 #if BYTE_ORDER == LITTLE_ENDIAN
616                         cur_instr->format.bytes[0],
617                         cur_instr->format.bytes[1],
618                         cur_instr->format.bytes[2],
619                         cur_instr->format.bytes[3]);
620 #else
621                         cur_instr->format.bytes[3],
622                         cur_instr->format.bytes[2],
623                         cur_instr->format.bytes[1],
624                         cur_instr->format.bytes[0]);
625 #endif
626                 /*
627                  * Macro expansions can cause several instructions
628                  * to be output for a single source line.  Only
629                  * advance the line once in these cases.
630                  */
631                 if (line == cur_instr->srcline) {
632                         fgets(buf, sizeof(buf), ifile);
633                         fprintf(listfile, "\t%s", buf);
634                         line++;
635                 } else {
636                         fprintf(listfile, "\n");
637                 }
638                 instrptr++;
639         }
640         free(func_values);
641
642         /* Dump the remainder of the file */
643         while(fgets(buf, sizeof(buf), ifile) != NULL)
644                 fprintf(listfile, "             %s", buf);
645
646         fclose(ifile);
647 }
648
649 static int
650 check_patch(patch_t **start_patch, unsigned int start_instr,
651             unsigned int *skip_addr, int *func_vals)
652 {
653         patch_t *cur_patch;
654
655         cur_patch = *start_patch;
656
657         while (cur_patch != NULL && start_instr == cur_patch->begin) {
658                 if (func_vals[cur_patch->patch_func] == 0) {
659                         int skip;
660
661                         /* Start rejecting code */
662                         *skip_addr = start_instr + cur_patch->skip_instr;
663                         for (skip = cur_patch->skip_patch;
664                              skip > 0 && cur_patch != NULL;
665                              skip--)
666                                 cur_patch = STAILQ_NEXT(cur_patch, links);
667                 } else {
668                         /* Accepted this patch.  Advance to the next
669                          * one and wait for our intruction pointer to
670                          * hit this point.
671                          */
672                         cur_patch = STAILQ_NEXT(cur_patch, links);
673                 }
674         }
675
676         *start_patch = cur_patch;
677         if (start_instr < *skip_addr)
678                 /* Still skipping */
679                 return (0);
680
681         return (1);
682 }
683
684 /*
685  * Print out error information if appropriate, and clean up before
686  * terminating the program.
687  */
688 void
689 stop(const char *string, int err_code)
690 {
691         if (string != NULL) {
692                 fprintf(stderr, "%s: ", appname);
693                 if (yyfilename != NULL) {
694                         fprintf(stderr, "Stopped at file %s, line %d - ",
695                                 yyfilename, yylineno);
696                 }
697                 fprintf(stderr, "%s\n", string);
698         }
699
700         if (ofile != NULL) {
701                 fclose(ofile);
702                 if (err_code != 0) {
703                         fprintf(stderr, "%s: Removing %s due to error\n",
704                                 appname, ofilename);
705                         unlink(ofilename);
706                 }
707         }
708
709         if (regfile != NULL) {
710                 fclose(regfile);
711                 if (err_code != 0) {
712                         fprintf(stderr, "%s: Removing %s due to error\n",
713                                 appname, regfilename);
714                         unlink(regfilename);
715                 }
716         }
717
718         if (listfile != NULL) {
719                 fclose(listfile);
720                 if (err_code != 0) {
721                         fprintf(stderr, "%s: Removing %s due to error\n",
722                                 appname, listfilename);
723                         unlink(listfilename);
724                 }
725         }
726
727         symlist_free(&patch_functions);
728         symtable_close();
729
730         exit(err_code);
731 }
732
733 struct instruction *
734 seq_alloc(void)
735 {
736         struct instruction *new_instr;
737
738         new_instr = (struct instruction *)malloc(sizeof(struct instruction));
739         if (new_instr == NULL)
740                 stop("Unable to malloc instruction object", EX_SOFTWARE);
741         memset(new_instr, 0, sizeof(*new_instr));
742         STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
743         new_instr->srcline = yylineno;
744         return new_instr;
745 }
746
747 critical_section_t *
748 cs_alloc(void)
749 {
750         critical_section_t *new_cs;
751
752         new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
753         if (new_cs == NULL)
754                 stop("Unable to malloc critical_section object", EX_SOFTWARE);
755         memset(new_cs, 0, sizeof(*new_cs));
756         
757         TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
758         return new_cs;
759 }
760
761 scope_t *
762 scope_alloc(void)
763 {
764         scope_t *new_scope;
765
766         new_scope = (scope_t *)malloc(sizeof(scope_t));
767         if (new_scope == NULL)
768                 stop("Unable to malloc scope object", EX_SOFTWARE);
769         memset(new_scope, 0, sizeof(*new_scope));
770         TAILQ_INIT(&new_scope->inner_scope);
771         
772         if (SLIST_FIRST(&scope_stack) != NULL) {
773                 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
774                                   new_scope, scope_links);
775         }
776         /* This patch is now the current scope */
777         SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
778         return new_scope;
779 }
780
781 void
782 process_scope(scope_t *scope)
783 {
784         /*
785          * We are "leaving" this scope.  We should now have
786          * enough information to process the lists of scopes
787          * we encapsulate.
788          */
789         scope_t *cur_scope;
790         u_int skip_patch_count;
791         u_int skip_instr_count;
792
793         cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
794         skip_patch_count = 0;
795         skip_instr_count = 0;
796         while (cur_scope != NULL) {
797                 u_int patch0_patch_skip;
798
799                 patch0_patch_skip = 0;
800                 switch (cur_scope->type) {
801                 case SCOPE_IF:
802                 case SCOPE_ELSE_IF:
803                         if (skip_instr_count != 0) {
804                                 /* Create a tail patch */
805                                 patch0_patch_skip++;
806                                 cur_scope->patches[1].skip_patch =
807                                     skip_patch_count + 1;
808                                 cur_scope->patches[1].skip_instr =
809                                     skip_instr_count;
810                         }
811
812                         /* Count Head patch */
813                         patch0_patch_skip++;
814
815                         /* Count any patches contained in our inner scope */
816                         patch0_patch_skip += cur_scope->inner_scope_patches;
817
818                         cur_scope->patches[0].skip_patch = patch0_patch_skip;
819                         cur_scope->patches[0].skip_instr =
820                             cur_scope->end_addr - cur_scope->begin_addr;
821
822                         skip_instr_count += cur_scope->patches[0].skip_instr;
823
824                         skip_patch_count += patch0_patch_skip;
825                         if (cur_scope->type == SCOPE_IF) {
826                                 scope->inner_scope_patches += skip_patch_count;
827                                 skip_patch_count = 0;
828                                 skip_instr_count = 0;
829                         }
830                         break;
831                 case SCOPE_ELSE:
832                         /* Count any patches contained in our innter scope */
833                         skip_patch_count += cur_scope->inner_scope_patches;
834
835                         skip_instr_count += cur_scope->end_addr
836                                           - cur_scope->begin_addr;
837                         break;
838                 case SCOPE_ROOT:
839                         stop("Unexpected scope type encountered", EX_SOFTWARE);
840                         /* NOTREACHED */
841                 }
842
843                 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);
844         }
845 }