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 $
42 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm.c,v 1.36 2003/12/17 00:01:11 gibbs Exp $
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:")) != -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,
255 fprintf(stderr, "%s: No input file specifiled\n", appname);
260 if (regdiagfile != NULL
261 && (regfile == NULL || stock_include_file == NULL)) {
263 "%s: The -p option requires the -r and -i options.\n",
269 inputfilename = *argv;
270 include_file(*argv, SOURCE_FILE);
273 if (SLIST_FIRST(&scope_stack) == NULL
274 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
275 stop("Unterminated conditional expression", EX_DATAERR);
279 /* Process outmost scope */
280 process_scope(SLIST_FIRST(&scope_stack));
282 * Decend the tree of scopes and insert/emit
283 * patches as appropriate. We perform a depth first
284 * tranversal, recursively handling each scope.
286 /* start at the root scope */
287 dump_scope(SLIST_FIRST(&scope_stack));
289 /* Patch up forward jump addresses */
295 symtable_dump(regfile, regdiagfile);
296 if (listfile != NULL)
297 output_listing(inputfilename);
309 (void)fprintf(stderr,
310 "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]\n"
311 " [-r register_output_file [-p register_diag_file -i includefile]]\n"
312 " [-l program_list_file]\n"
313 " input_file\n", appname);
320 struct instruction *cur_instr;
322 for (cur_instr = STAILQ_FIRST(&seq_program);
324 cur_instr = STAILQ_NEXT(cur_instr, links)) {
325 if (cur_instr->patch_label != NULL) {
326 struct ins_format3 *f3_instr;
329 if (cur_instr->patch_label->type != LABEL) {
332 snprintf(buf, sizeof(buf),
333 "Undefined label %s",
334 cur_instr->patch_label->name);
335 stop(buf, EX_DATAERR);
338 f3_instr = &cur_instr->format.format3;
339 address = f3_instr->address;
340 address += cur_instr->patch_label->info.linfo->address;
341 f3_instr->address = address;
349 struct instruction *cur_instr;
351 critical_section_t *cs;
352 symbol_node_t *cur_node;
358 " * DO NOT EDIT - This file is automatically generated\n"
359 " * from the following source files:\n"
361 "%s */\n", versions);
363 fprintf(ofile, "static uint8_t seqprog[] = {\n");
364 for (cur_instr = STAILQ_FIRST(&seq_program);
366 cur_instr = STAILQ_NEXT(cur_instr, links)) {
368 fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
369 cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n",
370 #if BYTE_ORDER == LITTLE_ENDIAN
371 cur_instr->format.bytes[0],
372 cur_instr->format.bytes[1],
373 cur_instr->format.bytes[2],
374 cur_instr->format.bytes[3]);
376 cur_instr->format.bytes[3],
377 cur_instr->format.bytes[2],
378 cur_instr->format.bytes[1],
379 cur_instr->format.bytes[0]);
383 fprintf(ofile, "\n};\n\n");
385 if (patch_arg_list == NULL)
386 stop("Patch argument list not defined",
390 * Output patch information. Patch functions first.
393 "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
395 for (cur_node = SLIST_FIRST(&patch_functions);
397 cur_node = SLIST_NEXT(cur_node,links)) {
399 "static %spatch_func_t %spatch%d_func;\n"
402 "%spatch%d_func(%s)\n"
408 cur_node->symbol->info.condinfo->func_num,
410 cur_node->symbol->info.condinfo->func_num,
412 cur_node->symbol->name);
416 "static struct patch {\n"
417 " %spatch_func_t *patch_func;\n"
418 " uint32_t begin :10,\n"
421 "} patches[] = {\n", prefix);
423 for (cur_patch = STAILQ_FIRST(&patches);
425 cur_patch = STAILQ_NEXT(cur_patch,links)) {
426 fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }",
427 cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n",
429 cur_patch->patch_func, cur_patch->begin,
430 cur_patch->skip_instr, cur_patch->skip_patch);
433 fprintf(ofile, "\n};\n\n");
436 "static struct cs {\n"
439 "} critical_sections[] = {\n");
441 for (cs = TAILQ_FIRST(&cs_tailq);
443 cs = TAILQ_NEXT(cs, links)) {
444 fprintf(ofile, "%s\t{ %d, %d }",
445 cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n",
446 cs->begin_addr, cs->end_addr);
449 fprintf(ofile, "\n};\n\n");
452 "static const int num_critical_sections = sizeof(critical_sections)\n"
453 " / sizeof(*critical_sections);\n");
455 fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
459 dump_scope(scope_t *scope)
464 * Emit the first patch for this scope
466 emit_patch(scope, 0);
469 * Dump each scope within this one.
471 cur_scope = TAILQ_FIRST(&scope->inner_scope);
473 while (cur_scope != NULL) {
475 dump_scope(cur_scope);
477 cur_scope = TAILQ_NEXT(cur_scope, scope_links);
481 * Emit the second, closing, patch for this scope
483 emit_patch(scope, 1);
487 emit_patch(scope_t *scope, int patch)
492 pinfo = &scope->patches[patch];
494 if (pinfo->skip_instr == 0)
498 new_patch = (patch_t *)malloc(sizeof(*new_patch));
500 if (new_patch == NULL)
501 stop("Could not malloc patch structure", EX_OSERR);
503 memset(new_patch, 0, sizeof(*new_patch));
506 new_patch->patch_func = scope->func_num;
507 new_patch->begin = scope->begin_addr;
509 new_patch->patch_func = 0;
510 new_patch->begin = scope->end_addr;
512 new_patch->skip_instr = pinfo->skip_instr;
513 new_patch->skip_patch = pinfo->skip_patch;
514 STAILQ_INSERT_TAIL(&patches, new_patch, links);
518 output_listing(char *ifilename)
522 struct instruction *cur_instr;
524 symbol_node_t *cur_func;
536 if ((ifile = fopen(ifilename, "r")) == NULL) {
538 stop(NULL, EX_DATAERR);
542 * Determine which options to apply to this listing.
544 for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
546 cur_func = SLIST_NEXT(cur_func, links))
550 if (func_count != 0) {
551 func_values = (int *)malloc(func_count * sizeof(int));
553 if (func_values == NULL)
554 stop("Could not malloc", EX_OSERR);
556 func_values[0] = 0; /* FALSE func */
560 * Ask the user to fill in the return values for
561 * the rest of the functions.
565 for (cur_func = SLIST_FIRST(&patch_functions);
566 cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
567 cur_func = SLIST_NEXT(cur_func, links), func_count--) {
570 fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
572 "Enter the return value for "
573 "this expression[T/F]:");
578 input = toupper(input);
581 func_values[func_count] = 1;
583 } else if (input == 'F') {
584 func_values[func_count] = 0;
588 if (isatty(fileno(stdin)) == 0)
591 fprintf(stdout, "\nThanks!\n");
594 /* Now output the listing */
595 cur_patch = STAILQ_FIRST(&patches);
596 for (cur_instr = STAILQ_FIRST(&seq_program);
598 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
600 if (check_patch(&cur_patch, instrcount,
601 &skip_addr, func_values) == 0) {
602 /* Don't count this instruction as it is in a patch
608 while (line < cur_instr->srcline) {
609 fgets(buf, sizeof(buf), ifile);
610 fprintf(listfile, " \t%s", buf);
613 fprintf(listfile, "%04x %02x%02x%02x%02x", instrptr,
614 #if BYTE_ORDER == LITTLE_ENDIAN
615 cur_instr->format.bytes[0],
616 cur_instr->format.bytes[1],
617 cur_instr->format.bytes[2],
618 cur_instr->format.bytes[3]);
620 cur_instr->format.bytes[3],
621 cur_instr->format.bytes[2],
622 cur_instr->format.bytes[1],
623 cur_instr->format.bytes[0]);
626 * Macro expansions can cause several instructions
627 * to be output for a single source line. Only
628 * advance the line once in these cases.
630 if (line == cur_instr->srcline) {
631 fgets(buf, sizeof(buf), ifile);
632 fprintf(listfile, "\t%s", buf);
635 fprintf(listfile, "\n");
641 /* Dump the remainder of the file */
642 while(fgets(buf, sizeof(buf), ifile) != NULL)
643 fprintf(listfile, " %s", buf);
649 check_patch(patch_t **start_patch, unsigned int start_instr,
650 unsigned int *skip_addr, int *func_vals)
654 cur_patch = *start_patch;
656 while (cur_patch != NULL && start_instr == cur_patch->begin) {
657 if (func_vals[cur_patch->patch_func] == 0) {
660 /* Start rejecting code */
661 *skip_addr = start_instr + cur_patch->skip_instr;
662 for (skip = cur_patch->skip_patch;
663 skip > 0 && cur_patch != NULL;
665 cur_patch = STAILQ_NEXT(cur_patch, links);
667 /* Accepted this patch. Advance to the next
668 * one and wait for our intruction pointer to
671 cur_patch = STAILQ_NEXT(cur_patch, links);
675 *start_patch = cur_patch;
676 if (start_instr < *skip_addr)
684 * Print out error information if appropriate, and clean up before
685 * terminating the program.
688 stop(const char *string, int err_code)
690 if (string != NULL) {
691 fprintf(stderr, "%s: ", appname);
692 if (yyfilename != NULL) {
693 fprintf(stderr, "Stopped at file %s, line %d - ",
694 yyfilename, yylineno);
696 fprintf(stderr, "%s\n", string);
702 fprintf(stderr, "%s: Removing %s due to error\n",
708 if (regfile != NULL) {
711 fprintf(stderr, "%s: Removing %s due to error\n",
712 appname, regfilename);
717 if (listfile != NULL) {
720 fprintf(stderr, "%s: Removing %s due to error\n",
721 appname, listfilename);
722 unlink(listfilename);
726 symlist_free(&patch_functions);
735 struct instruction *new_instr;
737 new_instr = (struct instruction *)malloc(sizeof(struct instruction));
738 if (new_instr == NULL)
739 stop("Unable to malloc instruction object", EX_SOFTWARE);
740 memset(new_instr, 0, sizeof(*new_instr));
741 STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
742 new_instr->srcline = yylineno;
749 critical_section_t *new_cs;
751 new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
753 stop("Unable to malloc critical_section object", EX_SOFTWARE);
754 memset(new_cs, 0, sizeof(*new_cs));
756 TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
765 new_scope = (scope_t *)malloc(sizeof(scope_t));
766 if (new_scope == NULL)
767 stop("Unable to malloc scope object", EX_SOFTWARE);
768 memset(new_scope, 0, sizeof(*new_scope));
769 TAILQ_INIT(&new_scope->inner_scope);
771 if (SLIST_FIRST(&scope_stack) != NULL) {
772 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
773 new_scope, scope_links);
775 /* This patch is now the current scope */
776 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
781 process_scope(scope_t *scope)
784 * We are "leaving" this scope. We should now have
785 * enough information to process the lists of scopes
789 u_int skip_patch_count;
790 u_int skip_instr_count;
792 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
793 skip_patch_count = 0;
794 skip_instr_count = 0;
795 while (cur_scope != NULL) {
796 u_int patch0_patch_skip;
798 patch0_patch_skip = 0;
799 switch (cur_scope->type) {
802 if (skip_instr_count != 0) {
803 /* Create a tail patch */
805 cur_scope->patches[1].skip_patch =
806 skip_patch_count + 1;
807 cur_scope->patches[1].skip_instr =
811 /* Count Head patch */
814 /* Count any patches contained in our inner scope */
815 patch0_patch_skip += cur_scope->inner_scope_patches;
817 cur_scope->patches[0].skip_patch = patch0_patch_skip;
818 cur_scope->patches[0].skip_instr =
819 cur_scope->end_addr - cur_scope->begin_addr;
821 skip_instr_count += cur_scope->patches[0].skip_instr;
823 skip_patch_count += patch0_patch_skip;
824 if (cur_scope->type == SCOPE_IF) {
825 scope->inner_scope_patches += skip_patch_count;
826 skip_patch_count = 0;
827 skip_instr_count = 0;
831 /* Count any patches contained in our innter scope */
832 skip_patch_count += cur_scope->inner_scope_patches;
834 skip_instr_count += cur_scope->end_addr
835 - cur_scope->begin_addr;
838 stop("Unexpected scope type encountered", EX_SOFTWARE);
842 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);