2 * Aic7xxx SCSI host adapter firmware asssembler
4 * Copyright (c) 1997, 1998, 2000, 2001 Justin T. Gibbs.
5 * Copyright (c) 2001, 2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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.
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.
40 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm.c#23 $
44 #include <sys/types.h>
59 #include <machine/endian.h>
63 #include "aicasm_symbol.h"
64 #include "aicasm_insformat.h"
66 typedef struct patch {
67 STAILQ_ENTRY(patch) links;
74 STAILQ_HEAD(patch_list, patch) patches;
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);
85 struct path_list search_path;
86 int includes_search_curdir;
88 char *stock_include_file;
95 char *regdiagfilename;
100 static STAILQ_HEAD(,instruction) seq_program;
101 struct cs_tailq cs_tailq;
102 struct scope_list scope_stack;
103 symlist_t patch_functions;
106 extern int yy_flex_debug;
107 extern int mm_flex_debug;
112 extern int yyparse(void);
114 int main(int argc, char *argv[]);
117 main(int argc, char *argv[])
124 STAILQ_INIT(&patches);
125 SLIST_INIT(&search_path);
126 STAILQ_INIT(&seq_program);
127 TAILQ_INIT(&cs_tailq);
128 SLIST_INIT(&scope_stack);
130 /* Set Sentinal scope node */
131 sentinal = scope_alloc();
132 sentinal->type = SCOPE_ROOT;
134 includes_search_curdir = 1;
144 while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:X")) != -1) {
148 if (strcmp(optarg, "s") == 0) {
151 } else if (strcmp(optarg, "p") == 0) {
155 fprintf(stderr, "%s: -d Requires either an "
156 "'s' or 'p' argument\n", appname);
160 stop("-d: Assembler not built with debugging "
161 "information", EX_SOFTWARE);
165 stock_include_file = optarg;
168 /* Create a program listing */
169 if ((listfile = fopen(optarg, "w")) == NULL) {
171 stop(NULL, EX_CANTCREAT);
173 listfilename = optarg;
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);
185 if ((ofile = fopen(optarg, "w")) == NULL) {
187 stop(NULL, EX_CANTCREAT);
192 /* Create Register Diagnostic "printing" Functions */
193 if ((regdiagfile = fopen(optarg, "w")) == NULL) {
195 stop(NULL, EX_CANTCREAT);
197 regdiagfilename = optarg;
200 if ((regfile = fopen(optarg, "w")) == NULL) {
202 stop(NULL, EX_CANTCREAT);
204 regfilename = optarg;
208 path_entry_t include_dir;
210 if (strcmp(optarg, "-") == 0) {
211 if (includes_search_curdir == 0) {
212 fprintf(stderr, "%s: Warning - '-I-' "
213 "specified multiple "
216 includes_search_curdir = 0;
217 for (include_dir = SLIST_FIRST(&search_path);
219 include_dir = SLIST_NEXT(include_dir,
222 * All entries before a '-I-' only
223 * apply to includes specified with
224 * quotes instead of "<>".
226 include_dir->quoted_includes_only = 1;
229 (path_entry_t)malloc(sizeof(*include_dir));
230 if (include_dir == NULL) {
232 stop(NULL, EX_OSERR);
234 include_dir->directory = strdup(optarg);
235 if (include_dir->directory == NULL) {
237 stop(NULL, EX_OSERR);
239 include_dir->quoted_includes_only = 0;
240 SLIST_INSERT_HEAD(&search_path, include_dir,
246 /* icc version of -nostdinc */
258 fprintf(stderr, "%s: No input file specifiled\n", appname);
263 if (regdiagfile != NULL
264 && (regfile == NULL || stock_include_file == NULL)) {
266 "%s: The -p option requires the -r and -i options.\n",
272 inputfilename = *argv;
273 include_file(*argv, SOURCE_FILE);
276 if (SLIST_FIRST(&scope_stack) == NULL
277 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
278 stop("Unterminated conditional expression", EX_DATAERR);
282 /* Process outmost scope */
283 process_scope(SLIST_FIRST(&scope_stack));
285 * Decend the tree of scopes and insert/emit
286 * patches as appropriate. We perform a depth first
287 * tranversal, recursively handling each scope.
289 /* start at the root scope */
290 dump_scope(SLIST_FIRST(&scope_stack));
292 /* Patch up forward jump addresses */
298 symtable_dump(regfile, regdiagfile);
299 if (listfile != NULL)
300 output_listing(inputfilename);
312 (void)fprintf(stderr,
313 "usage: %-16s [-nostdinc|-X] [-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);
323 struct instruction *cur_instr;
325 for (cur_instr = STAILQ_FIRST(&seq_program);
327 cur_instr = STAILQ_NEXT(cur_instr, links)) {
328 if (cur_instr->patch_label != NULL) {
329 struct ins_format3 *f3_instr;
332 if (cur_instr->patch_label->type != LABEL) {
335 snprintf(buf, sizeof(buf),
336 "Undefined label %s",
337 cur_instr->patch_label->name);
338 stop(buf, EX_DATAERR);
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;
352 struct instruction *cur_instr;
354 critical_section_t *cs;
355 symbol_node_t *cur_node;
361 " * DO NOT EDIT - This file is automatically generated\n"
362 " * from the following source files:\n"
364 "%s */\n", versions);
366 fprintf(ofile, "static uint8_t seqprog[] = {\n");
367 for (cur_instr = STAILQ_FIRST(&seq_program);
369 cur_instr = STAILQ_NEXT(cur_instr, links)) {
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]);
379 cur_instr->format.bytes[3],
380 cur_instr->format.bytes[2],
381 cur_instr->format.bytes[1],
382 cur_instr->format.bytes[0]);
386 fprintf(ofile, "\n};\n\n");
388 if (patch_arg_list == NULL)
389 stop("Patch argument list not defined",
393 * Output patch information. Patch functions first.
396 "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
398 for (cur_node = SLIST_FIRST(&patch_functions);
400 cur_node = SLIST_NEXT(cur_node,links)) {
402 "static %spatch_func_t %spatch%d_func;\n"
405 "%spatch%d_func(%s)\n"
411 cur_node->symbol->info.condinfo->func_num,
413 cur_node->symbol->info.condinfo->func_num,
415 cur_node->symbol->name);
419 "static struct patch {\n"
420 " %spatch_func_t *patch_func;\n"
421 " uint32_t begin :10,\n"
424 "} patches[] = {\n", prefix);
426 for (cur_patch = STAILQ_FIRST(&patches);
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",
432 cur_patch->patch_func, cur_patch->begin,
433 cur_patch->skip_instr, cur_patch->skip_patch);
436 fprintf(ofile, "\n};\n\n");
439 "static struct cs {\n"
442 "} critical_sections[] = {\n");
444 for (cs = TAILQ_FIRST(&cs_tailq);
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);
452 fprintf(ofile, "\n};\n\n");
455 "static const int num_critical_sections = sizeof(critical_sections)\n"
456 " / sizeof(*critical_sections);\n");
458 fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
462 dump_scope(scope_t *scope)
467 * Emit the first patch for this scope
469 emit_patch(scope, 0);
472 * Dump each scope within this one.
474 cur_scope = TAILQ_FIRST(&scope->inner_scope);
476 while (cur_scope != NULL) {
478 dump_scope(cur_scope);
480 cur_scope = TAILQ_NEXT(cur_scope, scope_links);
484 * Emit the second, closing, patch for this scope
486 emit_patch(scope, 1);
490 emit_patch(scope_t *scope, int patch)
495 pinfo = &scope->patches[patch];
497 if (pinfo->skip_instr == 0)
501 new_patch = (patch_t *)malloc(sizeof(*new_patch));
503 if (new_patch == NULL)
504 stop("Could not malloc patch structure", EX_OSERR);
506 memset(new_patch, 0, sizeof(*new_patch));
509 new_patch->patch_func = scope->func_num;
510 new_patch->begin = scope->begin_addr;
512 new_patch->patch_func = 0;
513 new_patch->begin = scope->end_addr;
515 new_patch->skip_instr = pinfo->skip_instr;
516 new_patch->skip_patch = pinfo->skip_patch;
517 STAILQ_INSERT_TAIL(&patches, new_patch, links);
521 output_listing(char *ifilename)
525 struct instruction *cur_instr;
527 symbol_node_t *cur_func;
539 if ((ifile = fopen(ifilename, "r")) == NULL) {
541 stop(NULL, EX_DATAERR);
545 * Determine which options to apply to this listing.
547 for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
549 cur_func = SLIST_NEXT(cur_func, links))
553 if (func_count != 0) {
554 func_values = (int *)malloc(func_count * sizeof(int));
556 if (func_values == NULL)
557 stop("Could not malloc", EX_OSERR);
559 func_values[0] = 0; /* FALSE func */
563 * Ask the user to fill in the return values for
564 * the rest of the functions.
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--) {
573 fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
575 "Enter the return value for "
576 "this expression[T/F]:");
581 input = toupper(input);
584 func_values[func_count] = 1;
586 } else if (input == 'F') {
587 func_values[func_count] = 0;
591 if (isatty(fileno(stdin)) == 0)
594 fprintf(stdout, "\nThanks!\n");
597 /* Now output the listing */
598 cur_patch = STAILQ_FIRST(&patches);
599 for (cur_instr = STAILQ_FIRST(&seq_program);
601 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
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
611 while (line < cur_instr->srcline) {
612 fgets(buf, sizeof(buf), ifile);
613 fprintf(listfile, " \t%s", buf);
616 fprintf(listfile, "%04x %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]);
623 cur_instr->format.bytes[3],
624 cur_instr->format.bytes[2],
625 cur_instr->format.bytes[1],
626 cur_instr->format.bytes[0]);
629 * Macro expansions can cause several instructions
630 * to be output for a single source line. Only
631 * advance the line once in these cases.
633 if (line == cur_instr->srcline) {
634 fgets(buf, sizeof(buf), ifile);
635 fprintf(listfile, "\t%s", buf);
638 fprintf(listfile, "\n");
644 /* Dump the remainder of the file */
645 while(fgets(buf, sizeof(buf), ifile) != NULL)
646 fprintf(listfile, " %s", buf);
652 check_patch(patch_t **start_patch, unsigned int start_instr,
653 unsigned int *skip_addr, int *func_vals)
657 cur_patch = *start_patch;
659 while (cur_patch != NULL && start_instr == cur_patch->begin) {
660 if (func_vals[cur_patch->patch_func] == 0) {
663 /* Start rejecting code */
664 *skip_addr = start_instr + cur_patch->skip_instr;
665 for (skip = cur_patch->skip_patch;
666 skip > 0 && cur_patch != NULL;
668 cur_patch = STAILQ_NEXT(cur_patch, links);
670 /* Accepted this patch. Advance to the next
671 * one and wait for our intruction pointer to
674 cur_patch = STAILQ_NEXT(cur_patch, links);
678 *start_patch = cur_patch;
679 if (start_instr < *skip_addr)
687 * Print out error information if appropriate, and clean up before
688 * terminating the program.
691 stop(const char *string, int err_code)
693 if (string != NULL) {
694 fprintf(stderr, "%s: ", appname);
695 if (yyfilename != NULL) {
696 fprintf(stderr, "Stopped at file %s, line %d - ",
697 yyfilename, yylineno);
699 fprintf(stderr, "%s\n", string);
705 fprintf(stderr, "%s: Removing %s due to error\n",
711 if (regfile != NULL) {
714 fprintf(stderr, "%s: Removing %s due to error\n",
715 appname, regfilename);
720 if (listfile != NULL) {
723 fprintf(stderr, "%s: Removing %s due to error\n",
724 appname, listfilename);
725 unlink(listfilename);
729 symlist_free(&patch_functions);
738 struct instruction *new_instr;
740 new_instr = (struct instruction *)malloc(sizeof(struct instruction));
741 if (new_instr == NULL)
742 stop("Unable to malloc instruction object", EX_SOFTWARE);
743 memset(new_instr, 0, sizeof(*new_instr));
744 STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
745 new_instr->srcline = yylineno;
752 critical_section_t *new_cs;
754 new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
756 stop("Unable to malloc critical_section object", EX_SOFTWARE);
757 memset(new_cs, 0, sizeof(*new_cs));
759 TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
768 new_scope = (scope_t *)malloc(sizeof(scope_t));
769 if (new_scope == NULL)
770 stop("Unable to malloc scope object", EX_SOFTWARE);
771 memset(new_scope, 0, sizeof(*new_scope));
772 TAILQ_INIT(&new_scope->inner_scope);
774 if (SLIST_FIRST(&scope_stack) != NULL) {
775 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
776 new_scope, scope_links);
778 /* This patch is now the current scope */
779 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
784 process_scope(scope_t *scope)
787 * We are "leaving" this scope. We should now have
788 * enough information to process the lists of scopes
792 u_int skip_patch_count;
793 u_int skip_instr_count;
795 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
796 skip_patch_count = 0;
797 skip_instr_count = 0;
798 while (cur_scope != NULL) {
799 u_int patch0_patch_skip;
801 patch0_patch_skip = 0;
802 switch (cur_scope->type) {
805 if (skip_instr_count != 0) {
806 /* Create a tail patch */
808 cur_scope->patches[1].skip_patch =
809 skip_patch_count + 1;
810 cur_scope->patches[1].skip_instr =
814 /* Count Head patch */
817 /* Count any patches contained in our inner scope */
818 patch0_patch_skip += cur_scope->inner_scope_patches;
820 cur_scope->patches[0].skip_patch = patch0_patch_skip;
821 cur_scope->patches[0].skip_instr =
822 cur_scope->end_addr - cur_scope->begin_addr;
824 skip_instr_count += cur_scope->patches[0].skip_instr;
826 skip_patch_count += patch0_patch_skip;
827 if (cur_scope->type == SCOPE_IF) {
828 scope->inner_scope_patches += skip_patch_count;
829 skip_patch_count = 0;
830 skip_instr_count = 0;
834 /* Count any patches contained in our innter scope */
835 skip_patch_count += cur_scope->inner_scope_patches;
837 skip_instr_count += cur_scope->end_addr
838 - cur_scope->begin_addr;
841 stop("Unexpected scope type encountered", EX_SOFTWARE);
845 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);