2 * Aic7xxx SCSI host adapter firmware asssembler
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 1997, 1998, 2000, 2001 Justin T. Gibbs.
7 * Copyright (c) 2001, 2002 Adaptec Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
21 * 3. Neither the names of the above-listed copyright holders nor the names
22 * of any contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * Alternatively, this software may be distributed under the terms of the
26 * GNU General Public License ("GPL") version 2 as published by the Free
27 * Software Foundation.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGES.
42 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm.c#23 $
46 #include <sys/types.h>
58 #if defined(__linux__) || defined(__GLIBC__)
61 #include <machine/endian.h>
65 #include "aicasm_symbol.h"
66 #include "aicasm_insformat.h"
68 typedef struct patch {
69 STAILQ_ENTRY(patch) links;
76 STAILQ_HEAD(patch_list, patch) patches;
78 static void usage(void);
79 static void back_patch(void);
80 static void output_code(void);
81 static void output_listing(char *ifilename);
82 static void dump_scope(scope_t *scope);
83 static void emit_patch(scope_t *scope, int patch);
84 static int check_patch(patch_t **start_patch, unsigned int start_instr,
85 unsigned int *skip_addr, int *func_vals);
87 struct path_list search_path;
88 int includes_search_curdir;
90 char *stock_include_file;
97 char *regdiagfilename;
102 static STAILQ_HEAD(,instruction) seq_program;
103 struct cs_tailq cs_tailq;
104 struct scope_list scope_stack;
105 symlist_t patch_functions;
108 extern int yy_flex_debug;
109 extern int mm_flex_debug;
114 extern int yyparse(void);
116 int main(int argc, char *argv[]);
119 main(int argc, char *argv[])
126 STAILQ_INIT(&patches);
127 SLIST_INIT(&search_path);
128 STAILQ_INIT(&seq_program);
129 TAILQ_INIT(&cs_tailq);
130 SLIST_INIT(&scope_stack);
132 /* Set Sentinal scope node */
133 sentinal = scope_alloc();
134 sentinal->type = SCOPE_ROOT;
136 includes_search_curdir = 1;
146 while ((ch = getopt(argc, argv, "d:i:l:n:o:p:r:I:X")) != -1) {
150 if (strcmp(optarg, "s") == 0) {
153 } else if (strcmp(optarg, "p") == 0) {
157 fprintf(stderr, "%s: -d Requires either an "
158 "'s' or 'p' argument\n", appname);
162 stop("-d: Assembler not built with debugging "
163 "information", EX_SOFTWARE);
167 stock_include_file = optarg;
170 /* Create a program listing */
171 if ((listfile = fopen(optarg, "w")) == NULL) {
173 stop(NULL, EX_CANTCREAT);
175 listfilename = optarg;
178 /* Don't complain about the -nostdinc directrive */
179 if (strcmp(optarg, "ostdinc")) {
180 fprintf(stderr, "%s: Unknown option -%c%s\n",
181 appname, ch, optarg);
187 if ((ofile = fopen(optarg, "w")) == NULL) {
189 stop(NULL, EX_CANTCREAT);
194 /* Create Register Diagnostic "printing" Functions */
195 if ((regdiagfile = fopen(optarg, "w")) == NULL) {
197 stop(NULL, EX_CANTCREAT);
199 regdiagfilename = optarg;
202 if ((regfile = fopen(optarg, "w")) == NULL) {
204 stop(NULL, EX_CANTCREAT);
206 regfilename = optarg;
210 path_entry_t include_dir;
212 if (strcmp(optarg, "-") == 0) {
213 if (includes_search_curdir == 0) {
214 fprintf(stderr, "%s: Warning - '-I-' "
215 "specified multiple "
218 includes_search_curdir = 0;
219 for (include_dir = SLIST_FIRST(&search_path);
221 include_dir = SLIST_NEXT(include_dir,
224 * All entries before a '-I-' only
225 * apply to includes specified with
226 * quotes instead of "<>".
228 include_dir->quoted_includes_only = 1;
231 (path_entry_t)malloc(sizeof(*include_dir));
232 if (include_dir == NULL) {
234 stop(NULL, EX_OSERR);
236 include_dir->directory = strdup(optarg);
237 if (include_dir->directory == NULL) {
239 stop(NULL, EX_OSERR);
241 include_dir->quoted_includes_only = 0;
242 SLIST_INSERT_HEAD(&search_path, include_dir,
248 /* icc version of -nostdinc */
260 fprintf(stderr, "%s: No input file specifiled\n", appname);
265 if (regdiagfile != NULL
266 && (regfile == NULL || stock_include_file == NULL)) {
268 "%s: The -p option requires the -r and -i options.\n",
274 inputfilename = *argv;
275 include_file(*argv, SOURCE_FILE);
278 if (SLIST_FIRST(&scope_stack) == NULL
279 || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
280 stop("Unterminated conditional expression", EX_DATAERR);
284 /* Process outmost scope */
285 process_scope(SLIST_FIRST(&scope_stack));
287 * Decend the tree of scopes and insert/emit
288 * patches as appropriate. We perform a depth first
289 * tranversal, recursively handling each scope.
291 /* start at the root scope */
292 dump_scope(SLIST_FIRST(&scope_stack));
294 /* Patch up forward jump addresses */
300 symtable_dump(regfile, regdiagfile);
301 if (listfile != NULL)
302 output_listing(inputfilename);
314 (void)fprintf(stderr,
315 "usage: %-16s [-nostdinc|-X] [-I-] [-I directory] [-o output_file]\n"
316 " [-r register_output_file [-p register_diag_file -i includefile]]\n"
317 " [-l program_list_file]\n"
318 " input_file\n", appname);
325 struct instruction *cur_instr;
327 for (cur_instr = STAILQ_FIRST(&seq_program);
329 cur_instr = STAILQ_NEXT(cur_instr, links)) {
330 if (cur_instr->patch_label != NULL) {
331 struct ins_format3 *f3_instr;
334 if (cur_instr->patch_label->type != LABEL) {
337 snprintf(buf, sizeof(buf),
338 "Undefined label %s",
339 cur_instr->patch_label->name);
340 stop(buf, EX_DATAERR);
343 f3_instr = &cur_instr->format.format3;
344 address = f3_instr->address;
345 address += cur_instr->patch_label->info.linfo->address;
346 f3_instr->address = address;
354 struct instruction *cur_instr;
356 critical_section_t *cs;
357 symbol_node_t *cur_node;
363 " * DO NOT EDIT - This file is automatically generated\n"
364 " * from the following source files:\n"
366 "%s */\n", versions);
368 fprintf(ofile, "static uint8_t seqprog[] = {\n");
369 for (cur_instr = STAILQ_FIRST(&seq_program);
371 cur_instr = STAILQ_NEXT(cur_instr, links)) {
373 fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
374 cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n",
375 #if BYTE_ORDER == LITTLE_ENDIAN
376 cur_instr->format.bytes[0],
377 cur_instr->format.bytes[1],
378 cur_instr->format.bytes[2],
379 cur_instr->format.bytes[3]);
381 cur_instr->format.bytes[3],
382 cur_instr->format.bytes[2],
383 cur_instr->format.bytes[1],
384 cur_instr->format.bytes[0]);
388 fprintf(ofile, "\n};\n\n");
390 if (patch_arg_list == NULL)
391 stop("Patch argument list not defined",
395 * Output patch information. Patch functions first.
398 "typedef int %spatch_func_t (%s);\n", prefix, patch_arg_list);
400 for (cur_node = SLIST_FIRST(&patch_functions);
402 cur_node = SLIST_NEXT(cur_node,links)) {
404 "static %spatch_func_t %spatch%d_func;\n"
407 "%spatch%d_func(%s)\n"
413 cur_node->symbol->info.condinfo->func_num,
415 cur_node->symbol->info.condinfo->func_num,
417 cur_node->symbol->name);
421 "static struct patch {\n"
422 " %spatch_func_t *patch_func;\n"
423 " uint32_t begin :10,\n"
426 "} patches[] = {\n", prefix);
428 for (cur_patch = STAILQ_FIRST(&patches);
430 cur_patch = STAILQ_NEXT(cur_patch,links)) {
431 fprintf(ofile, "%s\t{ %spatch%d_func, %d, %d, %d }",
432 cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n",
434 cur_patch->patch_func, cur_patch->begin,
435 cur_patch->skip_instr, cur_patch->skip_patch);
438 fprintf(ofile, "\n};\n\n");
441 "static struct cs {\n"
444 "} critical_sections[] = {\n");
446 for (cs = TAILQ_FIRST(&cs_tailq);
448 cs = TAILQ_NEXT(cs, links)) {
449 fprintf(ofile, "%s\t{ %d, %d }",
450 cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n",
451 cs->begin_addr, cs->end_addr);
454 fprintf(ofile, "\n};\n\n");
457 "static const int num_critical_sections = sizeof(critical_sections)\n"
458 " / sizeof(*critical_sections);\n");
460 fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
464 dump_scope(scope_t *scope)
469 * Emit the first patch for this scope
471 emit_patch(scope, 0);
474 * Dump each scope within this one.
476 cur_scope = TAILQ_FIRST(&scope->inner_scope);
478 while (cur_scope != NULL) {
480 dump_scope(cur_scope);
482 cur_scope = TAILQ_NEXT(cur_scope, scope_links);
486 * Emit the second, closing, patch for this scope
488 emit_patch(scope, 1);
492 emit_patch(scope_t *scope, int patch)
497 pinfo = &scope->patches[patch];
499 if (pinfo->skip_instr == 0)
503 new_patch = (patch_t *)malloc(sizeof(*new_patch));
505 if (new_patch == NULL)
506 stop("Could not malloc patch structure", EX_OSERR);
508 memset(new_patch, 0, sizeof(*new_patch));
511 new_patch->patch_func = scope->func_num;
512 new_patch->begin = scope->begin_addr;
514 new_patch->patch_func = 0;
515 new_patch->begin = scope->end_addr;
517 new_patch->skip_instr = pinfo->skip_instr;
518 new_patch->skip_patch = pinfo->skip_patch;
519 STAILQ_INSERT_TAIL(&patches, new_patch, links);
523 output_listing(char *ifilename)
527 struct instruction *cur_instr;
529 symbol_node_t *cur_func;
535 unsigned int skip_addr;
541 if ((ifile = fopen(ifilename, "r")) == NULL) {
543 stop(NULL, EX_DATAERR);
547 * Determine which options to apply to this listing.
549 for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
551 cur_func = SLIST_NEXT(cur_func, links))
555 if (func_count != 0) {
556 func_values = (int *)malloc(func_count * sizeof(int));
558 if (func_values == NULL)
559 stop("Could not malloc", EX_OSERR);
561 func_values[0] = 0; /* FALSE func */
565 * Ask the user to fill in the return values for
566 * the rest of the functions.
570 for (cur_func = SLIST_FIRST(&patch_functions);
571 cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
572 cur_func = SLIST_NEXT(cur_func, links), func_count--) {
575 fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
577 "Enter the return value for "
578 "this expression[T/F]:");
583 input = toupper(input);
586 func_values[func_count] = 1;
588 } else if (input == 'F') {
589 func_values[func_count] = 0;
593 if (isatty(fileno(stdin)) == 0)
596 fprintf(stdout, "\nThanks!\n");
599 /* Now output the listing */
600 cur_patch = STAILQ_FIRST(&patches);
601 for (cur_instr = STAILQ_FIRST(&seq_program);
603 cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
605 if (check_patch(&cur_patch, instrcount,
606 &skip_addr, func_values) == 0) {
607 /* Don't count this instruction as it is in a patch
613 while (line < cur_instr->srcline) {
614 fgets(buf, sizeof(buf), ifile);
615 fprintf(listfile, " \t%s", buf);
618 fprintf(listfile, "%04x %02x%02x%02x%02x", instrptr,
619 #if BYTE_ORDER == LITTLE_ENDIAN
620 cur_instr->format.bytes[0],
621 cur_instr->format.bytes[1],
622 cur_instr->format.bytes[2],
623 cur_instr->format.bytes[3]);
625 cur_instr->format.bytes[3],
626 cur_instr->format.bytes[2],
627 cur_instr->format.bytes[1],
628 cur_instr->format.bytes[0]);
631 * Macro expansions can cause several instructions
632 * to be output for a single source line. Only
633 * advance the line once in these cases.
635 if (line == cur_instr->srcline) {
636 fgets(buf, sizeof(buf), ifile);
637 fprintf(listfile, "\t%s", buf);
640 fprintf(listfile, "\n");
646 /* Dump the remainder of the file */
647 while(fgets(buf, sizeof(buf), ifile) != NULL)
648 fprintf(listfile, " %s", buf);
654 check_patch(patch_t **start_patch, unsigned int start_instr,
655 unsigned int *skip_addr, int *func_vals)
659 cur_patch = *start_patch;
661 while (cur_patch != NULL && start_instr == cur_patch->begin) {
662 if (func_vals[cur_patch->patch_func] == 0) {
665 /* Start rejecting code */
666 *skip_addr = start_instr + cur_patch->skip_instr;
667 for (skip = cur_patch->skip_patch;
668 skip > 0 && cur_patch != NULL;
670 cur_patch = STAILQ_NEXT(cur_patch, links);
672 /* Accepted this patch. Advance to the next
673 * one and wait for our instruction pointer to
676 cur_patch = STAILQ_NEXT(cur_patch, links);
680 *start_patch = cur_patch;
681 if (start_instr < *skip_addr)
689 * Print out error information if appropriate, and clean up before
690 * terminating the program.
693 stop(const char *string, int err_code)
695 if (string != NULL) {
696 fprintf(stderr, "%s: ", appname);
697 if (yyfilename != NULL) {
698 fprintf(stderr, "Stopped at file %s, line %d - ",
699 yyfilename, yylineno);
701 fprintf(stderr, "%s\n", string);
707 fprintf(stderr, "%s: Removing %s due to error\n",
713 if (regfile != NULL) {
716 fprintf(stderr, "%s: Removing %s due to error\n",
717 appname, regfilename);
722 if (listfile != NULL) {
725 fprintf(stderr, "%s: Removing %s due to error\n",
726 appname, listfilename);
727 unlink(listfilename);
731 symlist_free(&patch_functions);
740 struct instruction *new_instr;
742 new_instr = (struct instruction *)malloc(sizeof(struct instruction));
743 if (new_instr == NULL)
744 stop("Unable to malloc instruction object", EX_SOFTWARE);
745 memset(new_instr, 0, sizeof(*new_instr));
746 STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
747 new_instr->srcline = yylineno;
754 critical_section_t *new_cs;
756 new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
758 stop("Unable to malloc critical_section object", EX_SOFTWARE);
759 memset(new_cs, 0, sizeof(*new_cs));
761 TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
770 new_scope = (scope_t *)malloc(sizeof(scope_t));
771 if (new_scope == NULL)
772 stop("Unable to malloc scope object", EX_SOFTWARE);
773 memset(new_scope, 0, sizeof(*new_scope));
774 TAILQ_INIT(&new_scope->inner_scope);
776 if (SLIST_FIRST(&scope_stack) != NULL) {
777 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
778 new_scope, scope_links);
780 /* This patch is now the current scope */
781 SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
786 process_scope(scope_t *scope)
789 * We are "leaving" this scope. We should now have
790 * enough information to process the lists of scopes
794 u_int skip_patch_count;
795 u_int skip_instr_count;
797 cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
798 skip_patch_count = 0;
799 skip_instr_count = 0;
800 while (cur_scope != NULL) {
801 u_int patch0_patch_skip;
803 patch0_patch_skip = 0;
804 switch (cur_scope->type) {
807 if (skip_instr_count != 0) {
808 /* Create a tail patch */
810 cur_scope->patches[1].skip_patch =
811 skip_patch_count + 1;
812 cur_scope->patches[1].skip_instr =
816 /* Count Head patch */
819 /* Count any patches contained in our inner scope */
820 patch0_patch_skip += cur_scope->inner_scope_patches;
822 cur_scope->patches[0].skip_patch = patch0_patch_skip;
823 cur_scope->patches[0].skip_instr =
824 cur_scope->end_addr - cur_scope->begin_addr;
826 skip_instr_count += cur_scope->patches[0].skip_instr;
828 skip_patch_count += patch0_patch_skip;
829 if (cur_scope->type == SCOPE_IF) {
830 scope->inner_scope_patches += skip_patch_count;
831 skip_patch_count = 0;
832 skip_instr_count = 0;
836 /* Count any patches contained in our innter scope */
837 skip_patch_count += cur_scope->inner_scope_patches;
839 skip_instr_count += cur_scope->end_addr
840 - cur_scope->begin_addr;
843 stop("Unexpected scope type encountered", EX_SOFTWARE);
847 cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);