2 * Aic7xxx SCSI host adapter firmware asssembler
4 * Copyright (c) 1997, 1998 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/types.h>
41 #include "aicasm_symbol.h"
42 #include "sequencer.h"
44 typedef struct patch {
45 STAILQ_ENTRY(struct patch) links;
52 STAILQ_HEAD(patch_list, struct patch) patches;
54 static void usage(void);
55 static void back_patch(void);
56 static void output_code(FILE *ofile);
57 static void output_listing(FILE *listfile, char *ifilename);
58 static void dump_scope(scope_t *scope);
59 static void emit_patch(scope_t *scope, int patch);
60 static int check_patch(patch_t **start_patch, int start_instr,
61 int *skip_addr, int *func_vals);
63 struct path_list search_path;
64 int includes_search_curdir;
73 static STAILQ_HEAD(, struct instruction) seq_program;
74 struct scope_list scope_stack;
75 symlist_t patch_functions;
78 extern int yy_flex_debug;
82 extern int yyparse __P((void));
96 STAILQ_INIT(&patches);
97 SLIST_INIT(&search_path);
98 STAILQ_INIT(&seq_program);
99 SLIST_INIT(&scope_stack);
101 /* Set Sentinal scope node */
102 sentinal = scope_alloc();
103 sentinal->type = SCOPE_ROOT;
105 includes_search_curdir = 1;
113 while ((ch = getopt(argc, argv, "d:l:n:o:r:I:O:")) != -1) {
117 if (strcmp(optarg, "s") == 0) {
119 } else if (strcmp(optarg, "p") == 0) {
122 fprintf(stderr, "%s: -d Requires either an "
123 "'s' or 'p' argument\n", appname);
127 stop("-d: Assembler not built with debugging "
128 "information", EX_SOFTWARE);
132 /* Create a program listing */
133 if ((listfile = fopen(optarg, "w")) == NULL) {
135 stop(NULL, EX_CANTCREAT);
137 listfilename = optarg;
140 /* Don't complain about the -nostdinc directrive */
141 if (strcmp(optarg, "ostdinc")) {
142 fprintf(stderr, "%s: Unknown option -%c%s\n",
143 appname, ch, optarg);
149 if ((ofile = fopen(optarg, "w")) == NULL) {
151 stop(NULL, EX_CANTCREAT);
156 if ((regfile = fopen(optarg, "w")) == NULL) {
158 stop(NULL, EX_CANTCREAT);
160 regfilename = optarg;
164 path_entry_t include_dir;
166 if (strcmp(optarg, "-") == 0) {
167 if (includes_search_curdir == 0) {
168 fprintf(stderr, "%s: Warning - '-I-' "
169 "specified multiple "
172 includes_search_curdir = 0;
173 for (include_dir = search_path.slh_first;
175 include_dir = include_dir->links.sle_next)
177 * All entries before a '-I-' only
178 * apply to includes specified with
179 * quotes instead of "<>".
181 include_dir->quoted_includes_only = 1;
184 (path_entry_t)malloc(sizeof(*include_dir));
185 if (include_dir == NULL) {
187 stop(NULL, EX_OSERR);
189 include_dir->directory = strdup(optarg);
190 if (include_dir->directory == NULL) {
192 stop(NULL, EX_OSERR);
194 include_dir->quoted_includes_only = 0;
195 SLIST_INSERT_HEAD(&search_path, include_dir,
210 fprintf(stderr, "%s: No input file specifiled\n", appname);
216 inputfilename = *argv;
217 include_file(*argv, SOURCE_FILE);
220 if (SLIST_FIRST(&scope_stack) == NULL
221 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
222 stop("Unterminated conditional expression",
227 /* Process outmost scope */
228 process_scope(SLIST_FIRST(&scope_stack));
230 * Decend the tree of scopes and insert/emit
231 * patches as appropriate. We perform a depth first
232 * tranversal, recursively handling each scope.
234 /* start at the root scope */
235 dump_scope(SLIST_FIRST(&scope_stack));
237 /* Patch up forward jump addresses */
242 if (regfile != NULL) {
243 symtable_dump(regfile);
245 if (listfile != NULL)
246 output_listing(listfile, inputfilename);
258 (void)fprintf(stderr,
259 "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]
260 [-r register_output_file] [-l program_list_file]
269 struct instruction *cur_instr;
271 for(cur_instr = seq_program.stqh_first;
273 cur_instr = cur_instr->links.stqe_next) {
274 if (cur_instr->patch_label != NULL) {
275 struct ins_format3 *f3_instr;
278 if (cur_instr->patch_label->type != LABEL) {
281 snprintf(buf, sizeof(buf),
282 "Undefined label %s",
283 cur_instr->patch_label->name);
284 stop(buf, EX_DATAERR);
287 f3_instr = &cur_instr->format.format3;
288 address = f3_instr->address;
289 address += cur_instr->patch_label->info.linfo->address;
290 f3_instr->address = address;
299 struct instruction *cur_instr;
301 symbol_node_t *cur_node;
307 * DO NOT EDIT - This file is automatically generated.
310 fprintf(ofile, "static u_int8_t seqprog[] = {\n");
311 for(cur_instr = seq_program.stqh_first;
313 cur_instr = cur_instr->links.stqe_next) {
315 fprintf(ofile, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x,\n",
316 cur_instr->format.bytes[0],
317 cur_instr->format.bytes[1],
318 cur_instr->format.bytes[2],
319 cur_instr->format.bytes[3]);
322 fprintf(ofile, "};\n\n");
325 * Output patch information. Patch functions first.
327 for(cur_node = SLIST_FIRST(&patch_functions);
329 cur_node = SLIST_NEXT(cur_node,links)) {
331 "static int ahc_patch%d_func(struct ahc_softc *ahc);
334 ahc_patch%d_func(struct ahc_softc *ahc)
338 cur_node->symbol->info.condinfo->func_num,
339 cur_node->symbol->info.condinfo->func_num,
340 cur_node->symbol->name);
344 "typedef int patch_func_t __P((struct ahc_softc *));
346 patch_func_t *patch_func;
352 for(cur_patch = STAILQ_FIRST(&patches);
354 cur_patch = STAILQ_NEXT(cur_patch,links)) {
355 fprintf(ofile, "\t{ ahc_patch%d_func, %d, %d, %d },\n",
356 cur_patch->patch_func, cur_patch->begin,
357 cur_patch->skip_instr, cur_patch->skip_patch);
360 fprintf(ofile, "\n};\n");
362 fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
366 dump_scope(scope_t *scope)
371 * Emit the first patch for this scope
373 emit_patch(scope, 0);
376 * Dump each scope within this one.
378 cur_scope = TAILQ_FIRST(&scope->inner_scope);
380 while (cur_scope != NULL) {
382 dump_scope(cur_scope);
384 cur_scope = TAILQ_NEXT(cur_scope, scope_links);
388 * Emit the second, closing, patch for this scope
390 emit_patch(scope, 1);
394 emit_patch(scope_t *scope, int patch)
399 pinfo = &scope->patches[patch];
401 if (pinfo->skip_instr == 0)
405 new_patch = (patch_t *)malloc(sizeof(*new_patch));
407 if (new_patch == NULL)
408 stop("Could not malloc patch structure", EX_OSERR);
410 memset(new_patch, 0, sizeof(*new_patch));
413 new_patch->patch_func = scope->func_num;
414 new_patch->begin = scope->begin_addr;
416 new_patch->patch_func = 0;
417 new_patch->begin = scope->end_addr;
419 new_patch->skip_instr = pinfo->skip_instr;
420 new_patch->skip_patch = pinfo->skip_patch;
421 STAILQ_INSERT_TAIL(&patches, new_patch, links);
425 output_listing(FILE *listfile, char *ifilename)
429 struct instruction *cur_instr;
431 symbol_node_t *cur_func;
443 if ((ifile = fopen(ifilename, "r")) == NULL) {
445 stop(NULL, EX_DATAERR);
449 * Determine which options to apply to this listing.
451 for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
453 cur_func = SLIST_NEXT(cur_func, links))
456 if (func_count != 0) {
457 func_values = (int *)malloc(func_count * sizeof(int));
459 if (func_values == NULL)
460 stop("Could not malloc", EX_OSERR);
462 func_values[0] = 0; /* FALSE func */
466 * Ask the user to fill in the return values for
467 * the rest of the functions.
471 for (cur_func = SLIST_FIRST(&patch_functions);
472 cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
473 cur_func = SLIST_NEXT(cur_func, links), func_count--) {
476 fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
478 "Enter the return value for "
479 "this expression[T/F]:");
484 input = toupper(input);
487 func_values[func_count] = 1;
489 } else if (input == 'F') {
490 func_values[func_count] = 0;
494 if (isatty(fileno(stdin)) == 0)
497 fprintf(stdout, "\nThanks!\n");
500 /* Now output the listing */
501 cur_patch = STAILQ_FIRST(&patches);
502 for(cur_instr = STAILQ_FIRST(&seq_program);
504 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
506 if (check_patch(&cur_patch, instrcount,
507 &skip_addr, func_values) == 0) {
508 /* Don't count this instruction as it is in a patch
514 while (line < cur_instr->srcline) {
515 fgets(buf, sizeof(buf), ifile);
516 fprintf(listfile, "\t\t%s", buf);
519 fprintf(listfile, "%03x %02x%02x%02x%02x", instrptr,
520 cur_instr->format.bytes[0],
521 cur_instr->format.bytes[1],
522 cur_instr->format.bytes[2],
523 cur_instr->format.bytes[3]);
524 fgets(buf, sizeof(buf), ifile);
525 fprintf(listfile, "\t%s", buf);
529 /* Dump the remainder of the file */
530 while(fgets(buf, sizeof(buf), ifile) != NULL)
531 fprintf(listfile, "\t\t%s", buf);
537 check_patch(patch_t **start_patch, int start_instr,
538 int *skip_addr, int *func_vals)
542 cur_patch = *start_patch;
544 while (cur_patch != NULL && start_instr == cur_patch->begin) {
545 if (func_vals[cur_patch->patch_func] == 0) {
548 /* Start rejecting code */
549 *skip_addr = start_instr + cur_patch->skip_instr;
550 for (skip = cur_patch->skip_patch;
551 skip > 0 && cur_patch != NULL;
553 cur_patch = STAILQ_NEXT(cur_patch, links);
555 /* Accepted this patch. Advance to the next
556 * one and wait for our intruction pointer to
559 cur_patch = STAILQ_NEXT(cur_patch, links);
563 *start_patch = cur_patch;
564 if (start_instr < *skip_addr)
572 * Print out error information if appropriate, and clean up before
573 * terminating the program.
576 stop(string, err_code)
580 if (string != NULL) {
581 fprintf(stderr, "%s: ", appname);
582 if (yyfilename != NULL) {
583 fprintf(stderr, "Stopped at file %s, line %d - ",
584 yyfilename, yylineno);
586 fprintf(stderr, "%s\n", string);
592 fprintf(stderr, "%s: Removing %s due to error\n",
598 if (regfile != NULL) {
601 fprintf(stderr, "%s: Removing %s due to error\n",
602 appname, regfilename);
607 if (listfile != NULL) {
610 fprintf(stderr, "%s: Removing %s due to error\n",
611 appname, listfilename);
612 unlink(listfilename);
616 symlist_free(&patch_functions);
625 struct instruction *new_instr;
627 new_instr = (struct instruction *)malloc(sizeof(struct instruction));
628 if (new_instr == NULL)
629 stop("Unable to malloc instruction object", EX_SOFTWARE);
630 memset(new_instr, 0, sizeof(*new_instr));
631 STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
632 new_instr->srcline = yylineno;
641 new_scope = (scope_t *)malloc(sizeof(scope_t));
642 if (new_scope == NULL)
643 stop("Unable to malloc scope object", EX_SOFTWARE);
644 memset(new_scope, 0, sizeof(*new_scope));
645 TAILQ_INIT(&new_scope->inner_scope);
647 if (SLIST_FIRST(&scope_stack) != NULL) {
648 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
649 new_scope, scope_links);
651 /* This patch is now the current scope */
652 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
657 process_scope(scope_t *scope)
660 * We are "leaving" this scope. We should now have
661 * enough information to process the lists of scopes
665 u_int skip_patch_count;
666 u_int skip_instr_count;
668 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
669 skip_patch_count = 0;
670 skip_instr_count = 0;
671 while (cur_scope != NULL) {
672 u_int patch0_patch_skip;
674 patch0_patch_skip = 0;
675 switch (cur_scope->type) {
678 if (skip_instr_count != 0) {
679 /* Create a tail patch */
681 cur_scope->patches[1].skip_patch =
682 skip_patch_count + 1;
683 cur_scope->patches[1].skip_instr =
687 /* Count Head patch */
690 /* Count any patches contained in our inner scope */
691 patch0_patch_skip += cur_scope->inner_scope_patches;
693 cur_scope->patches[0].skip_patch = patch0_patch_skip;
694 cur_scope->patches[0].skip_instr =
695 cur_scope->end_addr - cur_scope->begin_addr;
697 skip_instr_count += cur_scope->patches[0].skip_instr;
699 skip_patch_count += patch0_patch_skip;
700 if (cur_scope->type == SCOPE_IF) {
701 scope->inner_scope_patches += skip_patch_count;
702 skip_patch_count = 0;
703 skip_instr_count = 0;
707 /* Count any patches contained in our innter scope */
708 skip_patch_count += cur_scope->inner_scope_patches;
710 skip_instr_count += cur_scope->end_addr
711 - cur_scope->begin_addr;
714 stop("Unexpected scope type encountered", EX_SOFTWARE);
718 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);