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#22 $
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.4 2006/04/22 16:15:26 dillon Exp $
45 #include <sys/types.h>
60 #include <machine/endian.h>
64 #include "aicasm_symbol.h"
65 #include "aicasm_insformat.h"
67 typedef struct patch {
68 STAILQ_ENTRY(patch) links;
75 STAILQ_HEAD(patch_list, patch) patches;
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, unsigned int start_instr,
84 unsigned int *skip_addr, int *func_vals);
86 struct path_list search_path;
87 int includes_search_curdir;
89 char *stock_include_file;
96 char *regdiagfilename;
101 static STAILQ_HEAD(,instruction) seq_program;
102 struct cs_tailq cs_tailq;
103 struct scope_list scope_stack;
104 symlist_t patch_functions;
107 extern int yy_flex_debug;
108 extern int mm_flex_debug;
113 extern int yyparse(void);
115 int main(int argc, char *argv[]);
118 main(int argc, char *argv[])
125 STAILQ_INIT(&patches);
126 SLIST_INIT(&search_path);
127 STAILQ_INIT(&seq_program);
128 TAILQ_INIT(&cs_tailq);
129 SLIST_INIT(&scope_stack);
131 /* Set Sentinal scope node */
132 sentinal = scope_alloc();
133 sentinal->type = SCOPE_ROOT;
135 includes_search_curdir = 1;
145 while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:")) != -1) {
149 if (strcmp(optarg, "s") == 0) {
152 } else if (strcmp(optarg, "p") == 0) {
156 fprintf(stderr, "%s: -d Requires either an "
157 "'s' or 'p' argument\n", appname);
161 stop("-d: Assembler not built with debugging "
162 "information", EX_SOFTWARE);
166 stock_include_file = optarg;
169 /* Create a program listing */
170 if ((listfile = fopen(optarg, "w")) == NULL) {
172 stop(NULL, EX_CANTCREAT);
174 listfilename = optarg;
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);
186 if ((ofile = fopen(optarg, "w")) == NULL) {
188 stop(NULL, EX_CANTCREAT);
193 /* Create Register Diagnostic "printing" Functions */
194 if ((regdiagfile = fopen(optarg, "w")) == NULL) {
196 stop(NULL, EX_CANTCREAT);
198 regdiagfilename = optarg;
201 if ((regfile = fopen(optarg, "w")) == NULL) {
203 stop(NULL, EX_CANTCREAT);
205 regfilename = optarg;
209 path_entry_t include_dir;
211 if (strcmp(optarg, "-") == 0) {
212 if (includes_search_curdir == 0) {
213 fprintf(stderr, "%s: Warning - '-I-' "
214 "specified multiple "
217 includes_search_curdir = 0;
218 for (include_dir = SLIST_FIRST(&search_path);
220 include_dir = SLIST_NEXT(include_dir,
223 * All entries before a '-I-' only
224 * apply to includes specified with
225 * quotes instead of "<>".
227 include_dir->quoted_includes_only = 1;
230 (path_entry_t)malloc(sizeof(*include_dir));
231 if (include_dir == NULL) {
233 stop(NULL, EX_OSERR);
235 include_dir->directory = strdup(optarg);
236 if (include_dir->directory == NULL) {
238 stop(NULL, EX_OSERR);
240 include_dir->quoted_includes_only = 0;
241 SLIST_INSERT_HEAD(&search_path, include_dir,
256 fprintf(stderr, "%s: No input file specifiled\n", appname);
261 if (regdiagfile != NULL
262 && (regfile == NULL || stock_include_file == NULL)) {
264 "%s: The -p option requires the -r and -i options.\n",
270 inputfilename = *argv;
271 include_file(*argv, SOURCE_FILE);
274 if (SLIST_FIRST(&scope_stack) == NULL
275 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
276 stop("Unterminated conditional expression", EX_DATAERR);
280 /* Process outmost scope */
281 process_scope(SLIST_FIRST(&scope_stack));
283 * Decend the tree of scopes and insert/emit
284 * patches as appropriate. We perform a depth first
285 * tranversal, recursively handling each scope.
287 /* start at the root scope */
288 dump_scope(SLIST_FIRST(&scope_stack));
290 /* Patch up forward jump addresses */
296 symtable_dump(regfile, regdiagfile);
297 if (listfile != NULL)
298 output_listing(inputfilename);
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);
321 struct instruction *cur_instr;
323 for (cur_instr = STAILQ_FIRST(&seq_program);
325 cur_instr = STAILQ_NEXT(cur_instr, links)) {
326 if (cur_instr->patch_label != NULL) {
327 struct ins_format3 *f3_instr;
330 if (cur_instr->patch_label->type != LABEL) {
333 snprintf(buf, sizeof(buf),
334 "Undefined label %s",
335 cur_instr->patch_label->name);
336 stop(buf, EX_DATAERR);
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;
350 struct instruction *cur_instr;
352 critical_section_t *cs;
353 symbol_node_t *cur_node;
359 " * DO NOT EDIT - This file is automatically generated\n"
360 " * from the following source files:\n"
362 "%s */\n", versions);
364 fprintf(ofile, "static uint8_t seqprog[] = {\n");
365 for (cur_instr = STAILQ_FIRST(&seq_program);
367 cur_instr = STAILQ_NEXT(cur_instr, links)) {
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]);
377 cur_instr->format.bytes[3],
378 cur_instr->format.bytes[2],
379 cur_instr->format.bytes[1],
380 cur_instr->format.bytes[0]);
384 fprintf(ofile, "\n};\n\n");
386 if (patch_arg_list == NULL)
387 stop("Patch argument list not defined",
391 * Output patch information. Patch functions first.
394 "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
396 for (cur_node = SLIST_FIRST(&patch_functions);
398 cur_node = SLIST_NEXT(cur_node,links)) {
400 "static %spatch_func_t %spatch%d_func;\n"
403 "%spatch%d_func(%s)\n"
409 cur_node->symbol->info.condinfo->func_num,
411 cur_node->symbol->info.condinfo->func_num,
413 cur_node->symbol->name);
417 "static struct patch {\n"
418 " %spatch_func_t *patch_func;\n"
419 " uint32_t begin :10,\n"
422 "} patches[] = {\n", prefix);
424 for (cur_patch = STAILQ_FIRST(&patches);
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",
430 cur_patch->patch_func, cur_patch->begin,
431 cur_patch->skip_instr, cur_patch->skip_patch);
434 fprintf(ofile, "\n};\n\n");
437 "static struct cs {\n"
440 "} critical_sections[] = {\n");
442 for (cs = TAILQ_FIRST(&cs_tailq);
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);
450 fprintf(ofile, "\n};\n\n");
453 "static const int num_critical_sections = sizeof(critical_sections)\n"
454 " / sizeof(*critical_sections);\n");
456 fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
460 dump_scope(scope_t *scope)
465 * Emit the first patch for this scope
467 emit_patch(scope, 0);
470 * Dump each scope within this one.
472 cur_scope = TAILQ_FIRST(&scope->inner_scope);
474 while (cur_scope != NULL) {
476 dump_scope(cur_scope);
478 cur_scope = TAILQ_NEXT(cur_scope, scope_links);
482 * Emit the second, closing, patch for this scope
484 emit_patch(scope, 1);
488 emit_patch(scope_t *scope, int patch)
493 pinfo = &scope->patches[patch];
495 if (pinfo->skip_instr == 0)
499 new_patch = (patch_t *)malloc(sizeof(*new_patch));
501 if (new_patch == NULL)
502 stop("Could not malloc patch structure", EX_OSERR);
504 memset(new_patch, 0, sizeof(*new_patch));
507 new_patch->patch_func = scope->func_num;
508 new_patch->begin = scope->begin_addr;
510 new_patch->patch_func = 0;
511 new_patch->begin = scope->end_addr;
513 new_patch->skip_instr = pinfo->skip_instr;
514 new_patch->skip_patch = pinfo->skip_patch;
515 STAILQ_INSERT_TAIL(&patches, new_patch, links);
519 output_listing(char *ifilename)
523 struct instruction *cur_instr;
525 symbol_node_t *cur_func;
537 if ((ifile = fopen(ifilename, "r")) == NULL) {
539 stop(NULL, EX_DATAERR);
543 * Determine which options to apply to this listing.
545 for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
547 cur_func = SLIST_NEXT(cur_func, links))
551 if (func_count != 0) {
552 func_values = (int *)malloc(func_count * sizeof(int));
554 if (func_values == NULL)
555 stop("Could not malloc", EX_OSERR);
557 func_values[0] = 0; /* FALSE func */
561 * Ask the user to fill in the return values for
562 * the rest of the functions.
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--) {
571 fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
573 "Enter the return value for "
574 "this expression[T/F]:");
579 input = toupper(input);
582 func_values[func_count] = 1;
584 } else if (input == 'F') {
585 func_values[func_count] = 0;
589 if (isatty(fileno(stdin)) == 0)
592 fprintf(stdout, "\nThanks!\n");
595 /* Now output the listing */
596 cur_patch = STAILQ_FIRST(&patches);
597 for (cur_instr = STAILQ_FIRST(&seq_program);
599 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
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
609 while (line < cur_instr->srcline) {
610 fgets(buf, sizeof(buf), ifile);
611 fprintf(listfile, "\t\t%s", buf);
614 fprintf(listfile, "%03x %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]);
621 cur_instr->format.bytes[3],
622 cur_instr->format.bytes[2],
623 cur_instr->format.bytes[1],
624 cur_instr->format.bytes[0]);
626 fgets(buf, sizeof(buf), ifile);
627 fprintf(listfile, "\t%s", buf);
631 /* Dump the remainder of the file */
632 while(fgets(buf, sizeof(buf), ifile) != NULL)
633 fprintf(listfile, "\t\t%s", buf);
639 check_patch(patch_t **start_patch, unsigned int start_instr,
640 unsigned int *skip_addr, int *func_vals)
644 cur_patch = *start_patch;
646 while (cur_patch != NULL && start_instr == cur_patch->begin) {
647 if (func_vals[cur_patch->patch_func] == 0) {
650 /* Start rejecting code */
651 *skip_addr = start_instr + cur_patch->skip_instr;
652 for (skip = cur_patch->skip_patch;
653 skip > 0 && cur_patch != NULL;
655 cur_patch = STAILQ_NEXT(cur_patch, links);
657 /* Accepted this patch. Advance to the next
658 * one and wait for our intruction pointer to
661 cur_patch = STAILQ_NEXT(cur_patch, links);
665 *start_patch = cur_patch;
666 if (start_instr < *skip_addr)
674 * Print out error information if appropriate, and clean up before
675 * terminating the program.
678 stop(const char *string, int err_code)
680 if (string != NULL) {
681 fprintf(stderr, "%s: ", appname);
682 if (yyfilename != NULL) {
683 fprintf(stderr, "Stopped at file %s, line %d - ",
684 yyfilename, yylineno);
686 fprintf(stderr, "%s\n", string);
692 fprintf(stderr, "%s: Removing %s due to error\n",
698 if (regfile != NULL) {
701 fprintf(stderr, "%s: Removing %s due to error\n",
702 appname, regfilename);
707 if (listfile != NULL) {
710 fprintf(stderr, "%s: Removing %s due to error\n",
711 appname, listfilename);
712 unlink(listfilename);
716 symlist_free(&patch_functions);
725 struct instruction *new_instr;
727 new_instr = (struct instruction *)malloc(sizeof(struct instruction));
728 if (new_instr == NULL)
729 stop("Unable to malloc instruction object", EX_SOFTWARE);
730 memset(new_instr, 0, sizeof(*new_instr));
731 STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
732 new_instr->srcline = yylineno;
739 critical_section_t *new_cs;
741 new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
743 stop("Unable to malloc critical_section object", EX_SOFTWARE);
744 memset(new_cs, 0, sizeof(*new_cs));
746 TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
755 new_scope = (scope_t *)malloc(sizeof(scope_t));
756 if (new_scope == NULL)
757 stop("Unable to malloc scope object", EX_SOFTWARE);
758 memset(new_scope, 0, sizeof(*new_scope));
759 TAILQ_INIT(&new_scope->inner_scope);
761 if (SLIST_FIRST(&scope_stack) != NULL) {
762 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
763 new_scope, scope_links);
765 /* This patch is now the current scope */
766 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
771 process_scope(scope_t *scope)
774 * We are "leaving" this scope. We should now have
775 * enough information to process the lists of scopes
779 u_int skip_patch_count;
780 u_int skip_instr_count;
782 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
783 skip_patch_count = 0;
784 skip_instr_count = 0;
785 while (cur_scope != NULL) {
786 u_int patch0_patch_skip;
788 patch0_patch_skip = 0;
789 switch (cur_scope->type) {
792 if (skip_instr_count != 0) {
793 /* Create a tail patch */
795 cur_scope->patches[1].skip_patch =
796 skip_patch_count + 1;
797 cur_scope->patches[1].skip_instr =
801 /* Count Head patch */
804 /* Count any patches contained in our inner scope */
805 patch0_patch_skip += cur_scope->inner_scope_patches;
807 cur_scope->patches[0].skip_patch = patch0_patch_skip;
808 cur_scope->patches[0].skip_instr =
809 cur_scope->end_addr - cur_scope->begin_addr;
811 skip_instr_count += cur_scope->patches[0].skip_instr;
813 skip_patch_count += patch0_patch_skip;
814 if (cur_scope->type == SCOPE_IF) {
815 scope->inner_scope_patches += skip_patch_count;
816 skip_patch_count = 0;
817 skip_instr_count = 0;
821 /* Count any patches contained in our innter scope */
822 skip_patch_count += cur_scope->inner_scope_patches;
824 skip_instr_count += cur_scope->end_addr
825 - cur_scope->begin_addr;
828 stop("Unexpected scope type encountered", EX_SOFTWARE);
832 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);