2 * Aic7xxx SCSI host adapter firmware assembler
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
, int start_instr
,
83 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
[])
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:")) != -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
,
257 fprintf(stderr
, "%s: No input file specified\n", appname
);
262 if (regdiagfile
!= NULL
263 && (regfile
== NULL
|| stock_include_file
== NULL
)) {
265 "%s: The -p option requires the -r and -i options.\n",
271 inputfilename
= *argv
;
272 include_file(*argv
, SOURCE_FILE
);
275 if (SLIST_FIRST(&scope_stack
) == NULL
276 || SLIST_FIRST(&scope_stack
)->type
!= SCOPE_ROOT
) {
277 stop("Unterminated conditional expression", EX_DATAERR
);
281 /* Process outmost scope */
282 process_scope(SLIST_FIRST(&scope_stack
));
284 * Decend the tree of scopes and insert/emit
285 * patches as appropriate. We perform a depth first
286 * traversal, recursively handling each scope.
288 /* start at the root scope */
289 dump_scope(SLIST_FIRST(&scope_stack
));
291 /* Patch up forward jump addresses */
297 symtable_dump(regfile
, regdiagfile
);
298 if (listfile
!= NULL
)
299 output_listing(inputfilename
);
311 (void)fprintf(stderr
,
312 "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]\n"
313 " [-r register_output_file [-p register_diag_file -i includefile]]\n"
314 " [-l program_list_file]\n"
315 " input_file\n", appname
);
322 struct instruction
*cur_instr
;
324 for (cur_instr
= STAILQ_FIRST(&seq_program
);
326 cur_instr
= STAILQ_NEXT(cur_instr
, links
)) {
327 if (cur_instr
->patch_label
!= NULL
) {
328 struct ins_format3
*f3_instr
;
331 if (cur_instr
->patch_label
->type
!= LABEL
) {
334 snprintf(buf
, sizeof(buf
),
335 "Undefined label %s",
336 cur_instr
->patch_label
->name
);
337 stop(buf
, EX_DATAERR
);
340 f3_instr
= &cur_instr
->format
.format3
;
341 address
= f3_instr
->address
;
342 address
+= cur_instr
->patch_label
->info
.linfo
->address
;
343 f3_instr
->address
= address
;
351 struct instruction
*cur_instr
;
353 critical_section_t
*cs
;
354 symbol_node_t
*cur_node
;
360 " * DO NOT EDIT - This file is automatically generated\n"
361 " * from the following source files:\n"
363 "%s */\n", versions
);
365 fprintf(ofile
, "static const uint8_t seqprog[] = {\n");
366 for (cur_instr
= STAILQ_FIRST(&seq_program
);
368 cur_instr
= STAILQ_NEXT(cur_instr
, links
)) {
370 fprintf(ofile
, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
371 cur_instr
== STAILQ_FIRST(&seq_program
) ? "" : ",\n",
372 #ifdef __LITTLE_ENDIAN
373 cur_instr
->format
.bytes
[0],
374 cur_instr
->format
.bytes
[1],
375 cur_instr
->format
.bytes
[2],
376 cur_instr
->format
.bytes
[3]);
378 cur_instr
->format
.bytes
[3],
379 cur_instr
->format
.bytes
[2],
380 cur_instr
->format
.bytes
[1],
381 cur_instr
->format
.bytes
[0]);
385 fprintf(ofile
, "\n};\n\n");
387 if (patch_arg_list
== NULL
)
388 stop("Patch argument list not defined",
392 * Output patch information. Patch functions first.
395 "typedef int %spatch_func_t (%s);\n", prefix
, patch_arg_list
);
397 for (cur_node
= SLIST_FIRST(&patch_functions
);
399 cur_node
= SLIST_NEXT(cur_node
,links
)) {
401 "static %spatch_func_t %spatch%d_func;\n"
404 "%spatch%d_func(%s)\n"
410 cur_node
->symbol
->info
.condinfo
->func_num
,
412 cur_node
->symbol
->info
.condinfo
->func_num
,
414 cur_node
->symbol
->name
);
418 "static const struct patch {\n"
419 " %spatch_func_t *patch_func;\n"
420 " uint32_t begin :10,\n"
423 "} patches[] = {\n", prefix
);
425 for (cur_patch
= STAILQ_FIRST(&patches
);
427 cur_patch
= STAILQ_NEXT(cur_patch
,links
)) {
428 fprintf(ofile
, "%s\t{ %spatch%d_func, %d, %d, %d }",
429 cur_patch
== STAILQ_FIRST(&patches
) ? "" : ",\n",
431 cur_patch
->patch_func
, cur_patch
->begin
,
432 cur_patch
->skip_instr
, cur_patch
->skip_patch
);
435 fprintf(ofile
, "\n};\n\n");
438 "static const struct cs {\n"
441 "} critical_sections[] = {\n");
443 for (cs
= TAILQ_FIRST(&cs_tailq
);
445 cs
= TAILQ_NEXT(cs
, links
)) {
446 fprintf(ofile
, "%s\t{ %d, %d }",
447 cs
== TAILQ_FIRST(&cs_tailq
) ? "" : ",\n",
448 cs
->begin_addr
, cs
->end_addr
);
451 fprintf(ofile
, "\n};\n\n");
454 "#define NUM_CRITICAL_SECTIONS ARRAY_SIZE(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%s", buf
);
614 fprintf(listfile
, "%04x %02x%02x%02x%02x", instrptr
,
615 #ifdef __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]);
627 * Macro expansions can cause several instructions
628 * to be output for a single source line. Only
629 * advance the line once in these cases.
631 if (line
== cur_instr
->srcline
) {
632 fgets(buf
, sizeof(buf
), ifile
);
633 fprintf(listfile
, "\t%s", buf
);
636 fprintf(listfile
, "\n");
640 /* Dump the remainder of the file */
641 while(fgets(buf
, sizeof(buf
), ifile
) != NULL
)
642 fprintf(listfile
, " %s", buf
);
648 check_patch(patch_t
**start_patch
, int start_instr
,
649 int *skip_addr
, int *func_vals
)
653 cur_patch
= *start_patch
;
655 while (cur_patch
!= NULL
&& start_instr
== cur_patch
->begin
) {
656 if (func_vals
[cur_patch
->patch_func
] == 0) {
659 /* Start rejecting code */
660 *skip_addr
= start_instr
+ cur_patch
->skip_instr
;
661 for (skip
= cur_patch
->skip_patch
;
662 skip
> 0 && cur_patch
!= NULL
;
664 cur_patch
= STAILQ_NEXT(cur_patch
, links
);
666 /* Accepted this patch. Advance to the next
667 * one and wait for our intruction pointer to
670 cur_patch
= STAILQ_NEXT(cur_patch
, links
);
674 *start_patch
= cur_patch
;
675 if (start_instr
< *skip_addr
)
683 * Print out error information if appropriate, and clean up before
684 * terminating the program.
687 stop(const char *string
, int err_code
)
689 if (string
!= NULL
) {
690 fprintf(stderr
, "%s: ", appname
);
691 if (yyfilename
!= NULL
) {
692 fprintf(stderr
, "Stopped at file %s, line %d - ",
693 yyfilename
, yylineno
);
695 fprintf(stderr
, "%s\n", string
);
701 fprintf(stderr
, "%s: Removing %s due to error\n",
707 if (regfile
!= NULL
) {
710 fprintf(stderr
, "%s: Removing %s due to error\n",
711 appname
, regfilename
);
716 if (listfile
!= NULL
) {
719 fprintf(stderr
, "%s: Removing %s due to error\n",
720 appname
, listfilename
);
721 unlink(listfilename
);
725 symlist_free(&patch_functions
);
734 struct instruction
*new_instr
;
736 new_instr
= (struct instruction
*)malloc(sizeof(struct instruction
));
737 if (new_instr
== NULL
)
738 stop("Unable to malloc instruction object", EX_SOFTWARE
);
739 memset(new_instr
, 0, sizeof(*new_instr
));
740 STAILQ_INSERT_TAIL(&seq_program
, new_instr
, links
);
741 new_instr
->srcline
= yylineno
;
748 critical_section_t
*new_cs
;
750 new_cs
= (critical_section_t
*)malloc(sizeof(critical_section_t
));
752 stop("Unable to malloc critical_section object", EX_SOFTWARE
);
753 memset(new_cs
, 0, sizeof(*new_cs
));
755 TAILQ_INSERT_TAIL(&cs_tailq
, new_cs
, links
);
764 new_scope
= (scope_t
*)malloc(sizeof(scope_t
));
765 if (new_scope
== NULL
)
766 stop("Unable to malloc scope object", EX_SOFTWARE
);
767 memset(new_scope
, 0, sizeof(*new_scope
));
768 TAILQ_INIT(&new_scope
->inner_scope
);
770 if (SLIST_FIRST(&scope_stack
) != NULL
) {
771 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack
)->inner_scope
,
772 new_scope
, scope_links
);
774 /* This patch is now the current scope */
775 SLIST_INSERT_HEAD(&scope_stack
, new_scope
, scope_stack_links
);
780 process_scope(scope_t
*scope
)
783 * We are "leaving" this scope. We should now have
784 * enough information to process the lists of scopes
788 u_int skip_patch_count
;
789 u_int skip_instr_count
;
791 cur_scope
= TAILQ_LAST(&scope
->inner_scope
, scope_tailq
);
792 skip_patch_count
= 0;
793 skip_instr_count
= 0;
794 while (cur_scope
!= NULL
) {
795 u_int patch0_patch_skip
;
797 patch0_patch_skip
= 0;
798 switch (cur_scope
->type
) {
801 if (skip_instr_count
!= 0) {
802 /* Create a tail patch */
804 cur_scope
->patches
[1].skip_patch
=
805 skip_patch_count
+ 1;
806 cur_scope
->patches
[1].skip_instr
=
810 /* Count Head patch */
813 /* Count any patches contained in our inner scope */
814 patch0_patch_skip
+= cur_scope
->inner_scope_patches
;
816 cur_scope
->patches
[0].skip_patch
= patch0_patch_skip
;
817 cur_scope
->patches
[0].skip_instr
=
818 cur_scope
->end_addr
- cur_scope
->begin_addr
;
820 skip_instr_count
+= cur_scope
->patches
[0].skip_instr
;
822 skip_patch_count
+= patch0_patch_skip
;
823 if (cur_scope
->type
== SCOPE_IF
) {
824 scope
->inner_scope_patches
+= skip_patch_count
;
825 skip_patch_count
= 0;
826 skip_instr_count
= 0;
830 /* Count any patches contained in our innter scope */
831 skip_patch_count
+= cur_scope
->inner_scope_patches
;
833 skip_instr_count
+= cur_scope
->end_addr
834 - cur_scope
->begin_addr
;
837 stop("Unexpected scope type encountered", EX_SOFTWARE
);
841 cur_scope
= TAILQ_PREV(cur_scope
, scope_tailq
, scope_links
);