1 /* $NetBSD: aicasm.c,v 1.7 2009/03/18 10:22:40 cegger Exp $ */
4 * Aic7xxx SCSI host adapter firmware asssembler
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 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm.c,v 1.35 2002/08/31 06:39:40 gibbs Exp $
45 #include <sys/cdefs.h>
46 __RCSID("$NetBSD: aicasm.c,v 1.7 2009/03/18 10:22:40 cegger Exp $");
48 #include <sys/types.h>
63 #include <machine/endian.h>
67 #include "aicasm_symbol.h"
68 #include "aicasm_insformat.h"
70 typedef struct patch
{
71 STAILQ_ENTRY(patch
) links
;
78 STAILQ_HEAD(patch_list
, patch
) patches
;
80 static void usage(void);
81 static void back_patch(void);
82 static void output_code(void);
83 static void output_listing(char *ifilename
);
84 static void dump_scope(scope_t
*scope
);
85 static void emit_patch(scope_t
*scope
, int patch
);
86 static int check_patch(patch_t
**start_patch
, int start_instr
,
87 int *skip_addr
, int *func_vals
);
89 struct path_list search_path
;
90 int includes_search_curdir
;
92 char *stock_include_file
;
99 char *regdiagfilename
;
104 static STAILQ_HEAD(,instruction
) seq_program
;
105 struct cs_tailq cs_tailq
;
106 struct scope_list scope_stack
;
107 symlist_t patch_functions
;
110 extern int yy_flex_debug
;
111 extern int mm_flex_debug
;
116 extern int yyparse(void);
118 int main(int argc
, char *argv
[]);
121 main(int argc
, char *argv
[])
130 STAILQ_INIT(&patches
);
131 SLIST_INIT(&search_path
);
132 STAILQ_INIT(&seq_program
);
133 TAILQ_INIT(&cs_tailq
);
134 SLIST_INIT(&scope_stack
);
136 /* Set Sentinal scope node */
137 sentinal
= scope_alloc();
138 sentinal
->type
= SCOPE_ROOT
;
140 includes_search_curdir
= 1;
150 while ((ch
= getopt(argc
, argv
, "d:i:l:n:o:p:r:I:")) != -1) {
154 if (strcmp(optarg
, "s") == 0) {
157 } else if (strcmp(optarg
, "p") == 0) {
161 fprintf(stderr
, "%s: -d Requires either an "
162 "'s' or 'p' argument\n", appname
);
166 stop("-d: Assembler not built with debugging "
167 "information", EX_SOFTWARE
);
171 stock_include_file
= optarg
;
174 /* Create a program listing */
175 if ((listfile
= fopen(optarg
, "w")) == NULL
) {
177 stop(NULL
, EX_CANTCREAT
);
179 listfilename
= optarg
;
182 /* Don't complain about the -nostdinc directrive */
183 if (strcmp(optarg
, "ostdinc")) {
184 fprintf(stderr
, "%s: Unknown option -%c%s\n",
185 appname
, ch
, optarg
);
191 if ((ofile
= fopen(optarg
, "w")) == NULL
) {
193 stop(NULL
, EX_CANTCREAT
);
198 /* Create Register Diagnostic "printing" Functions */
199 if ((regdiagfile
= fopen(optarg
, "w")) == NULL
) {
201 stop(NULL
, EX_CANTCREAT
);
203 regdiagfilename
= optarg
;
206 if ((regfile
= fopen(optarg
, "w")) == NULL
) {
208 stop(NULL
, EX_CANTCREAT
);
210 regfilename
= optarg
;
214 path_entry_t include_dir
;
216 if (strcmp(optarg
, "-") == 0) {
217 if (includes_search_curdir
== 0) {
218 fprintf(stderr
, "%s: Warning - '-I-' "
219 "specified multiple "
222 includes_search_curdir
= 0;
223 for (include_dir
= SLIST_FIRST(&search_path
);
225 include_dir
= SLIST_NEXT(include_dir
,
228 * All entries before a '-I-' only
229 * apply to includes specified with
230 * quotes instead of "<>".
232 include_dir
->quoted_includes_only
= 1;
235 (path_entry_t
)malloc(sizeof(*include_dir
));
236 if (include_dir
== NULL
) {
238 stop(NULL
, EX_OSERR
);
240 include_dir
->directory
= strdup(optarg
);
241 if (include_dir
->directory
== NULL
) {
243 stop(NULL
, EX_OSERR
);
245 include_dir
->quoted_includes_only
= 0;
246 SLIST_INSERT_HEAD(&search_path
, include_dir
,
261 fprintf(stderr
, "%s: No input file specifiled\n", appname
);
266 if (regdiagfile
!= NULL
267 && (regfile
== NULL
|| stock_include_file
== NULL
)) {
269 "%s: The -p option requires the -r and -i options.\n",
275 inputfilename
= *argv
;
276 include_file(*argv
, SOURCE_FILE
);
279 if (SLIST_FIRST(&scope_stack
) == NULL
280 || SLIST_FIRST(&scope_stack
)->type
!= SCOPE_ROOT
) {
281 stop("Unterminated conditional expression", EX_DATAERR
);
285 /* Process outmost scope */
286 process_scope(SLIST_FIRST(&scope_stack
));
288 * Decend the tree of scopes and insert/emit
289 * patches as appropriate. We perform a depth first
290 * tranversal, recursively handling each scope.
292 /* start at the root scope */
293 dump_scope(SLIST_FIRST(&scope_stack
));
295 /* Patch up forward jump addresses */
301 symtable_dump(regfile
, regdiagfile
);
302 if (listfile
!= NULL
)
303 output_listing(inputfilename
);
315 (void)fprintf(stderr
,
316 "usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]\n"
317 " [-r register_output_file [-p register_diag_file -i includefile]]\n"
318 " [-l program_list_file]\n"
319 " input_file\n", appname
);
326 struct instruction
*cur_instr
;
328 for (cur_instr
= STAILQ_FIRST(&seq_program
);
330 cur_instr
= STAILQ_NEXT(cur_instr
, links
)) {
331 if (cur_instr
->patch_label
!= NULL
) {
332 struct ins_format3
*f3_instr
;
335 if (cur_instr
->patch_label
->type
!= LABEL
) {
338 snprintf(buf
, sizeof(buf
),
339 "Undefined label %s",
340 cur_instr
->patch_label
->name
);
341 stop(buf
, EX_DATAERR
);
344 f3_instr
= &cur_instr
->format
.format3
;
345 address
= f3_instr
->address
;
346 address
+= cur_instr
->patch_label
->info
.linfo
->address
;
347 f3_instr
->address
= address
;
355 struct instruction
*cur_instr
;
357 critical_section_t
*cs
;
358 symbol_node_t
*cur_node
;
364 " * DO NOT EDIT - This file is automatically generated\n"
365 " * from the following source files:\n"
367 "%s */\n", versions
);
369 fprintf(ofile
, "static uint8_t seqprog[] = {\n");
370 for (cur_instr
= STAILQ_FIRST(&seq_program
);
372 cur_instr
= STAILQ_NEXT(cur_instr
, links
)) {
374 fprintf(ofile
, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
375 cur_instr
== STAILQ_FIRST(&seq_program
) ? "" : ",\n",
376 #if BYTE_ORDER == LITTLE_ENDIAN
377 cur_instr
->format
.bytes
[0],
378 cur_instr
->format
.bytes
[1],
379 cur_instr
->format
.bytes
[2],
380 cur_instr
->format
.bytes
[3]);
382 cur_instr
->format
.bytes
[3],
383 cur_instr
->format
.bytes
[2],
384 cur_instr
->format
.bytes
[1],
385 cur_instr
->format
.bytes
[0]);
389 fprintf(ofile
, "\n};\n\n");
391 if (patch_arg_list
== NULL
)
392 stop("Patch argument list not defined",
396 * Output patch information. Patch functions first.
399 "typedef int %spatch_func_t (%s);\n", prefix
, patch_arg_list
);
401 for (cur_node
= SLIST_FIRST(&patch_functions
);
403 cur_node
= SLIST_NEXT(cur_node
,links
)) {
405 "static %spatch_func_t %spatch%d_func;\n"
408 "%spatch%d_func(%s)\n"
414 cur_node
->symbol
->info
.condinfo
->func_num
,
416 cur_node
->symbol
->info
.condinfo
->func_num
,
418 cur_node
->symbol
->name
);
422 "static struct patch {\n"
423 " %spatch_func_t *patch_func;\n"
424 " uint32_t begin :10,\n"
427 "} patches[] = {\n", prefix
);
429 for (cur_patch
= STAILQ_FIRST(&patches
);
431 cur_patch
= STAILQ_NEXT(cur_patch
,links
)) {
432 fprintf(ofile
, "%s\t{ %spatch%d_func, %d, %d, %d }",
433 cur_patch
== STAILQ_FIRST(&patches
) ? "" : ",\n",
435 cur_patch
->patch_func
, cur_patch
->begin
,
436 cur_patch
->skip_instr
, cur_patch
->skip_patch
);
439 fprintf(ofile
, "\n};\n\n");
442 "static struct cs {\n"
445 "} critical_sections[] = {\n");
447 for (cs
= TAILQ_FIRST(&cs_tailq
);
449 cs
= TAILQ_NEXT(cs
, links
)) {
450 fprintf(ofile
, "%s\t{ %d, %d }",
451 cs
== TAILQ_FIRST(&cs_tailq
) ? "" : ",\n",
452 cs
->begin_addr
, cs
->end_addr
);
455 fprintf(ofile
, "\n};\n\n");
458 "static const int num_critical_sections = sizeof(critical_sections)\n"
459 " / sizeof(*critical_sections);\n");
461 fprintf(stderr
, "%s: %d instructions used\n", appname
, instrcount
);
465 dump_scope(scope_t
*scope
)
470 * Emit the first patch for this scope
472 emit_patch(scope
, 0);
475 * Dump each scope within this one.
477 cur_scope
= TAILQ_FIRST(&scope
->inner_scope
);
479 while (cur_scope
!= NULL
) {
481 dump_scope(cur_scope
);
483 cur_scope
= TAILQ_NEXT(cur_scope
, scope_links
);
487 * Emit the second, closing, patch for this scope
489 emit_patch(scope
, 1);
493 emit_patch(scope_t
*scope
, int patch
)
498 pinfo
= &scope
->patches
[patch
];
500 if (pinfo
->skip_instr
== 0)
504 new_patch
= (patch_t
*)malloc(sizeof(*new_patch
));
506 if (new_patch
== NULL
)
507 stop("Could not malloc patch structure", EX_OSERR
);
509 memset(new_patch
, 0, sizeof(*new_patch
));
512 new_patch
->patch_func
= scope
->func_num
;
513 new_patch
->begin
= scope
->begin_addr
;
515 new_patch
->patch_func
= 0;
516 new_patch
->begin
= scope
->end_addr
;
518 new_patch
->skip_instr
= pinfo
->skip_instr
;
519 new_patch
->skip_patch
= pinfo
->skip_patch
;
520 STAILQ_INSERT_TAIL(&patches
, new_patch
, links
);
524 output_listing(char *ifilename
)
528 struct instruction
*cur_instr
;
530 symbol_node_t
*cur_func
;
542 if ((ifile
= fopen(ifilename
, "r")) == NULL
) {
544 stop(NULL
, EX_DATAERR
);
548 * Determine which options to apply to this listing.
550 for (func_count
= 0, cur_func
= SLIST_FIRST(&patch_functions
);
552 cur_func
= SLIST_NEXT(cur_func
, links
))
556 if (func_count
!= 0) {
557 func_values
= (int *)malloc(func_count
* sizeof(int));
559 if (func_values
== NULL
)
560 stop("Could not malloc", EX_OSERR
);
562 func_values
[0] = 0; /* FALSE func */
566 * Ask the user to fill in the return values for
567 * the rest of the functions.
571 for (cur_func
= SLIST_FIRST(&patch_functions
);
572 cur_func
!= NULL
&& SLIST_NEXT(cur_func
, links
) != NULL
;
573 cur_func
= SLIST_NEXT(cur_func
, links
), func_count
--) {
576 fprintf(stdout
, "\n(%s)\n", cur_func
->symbol
->name
);
578 "Enter the return value for "
579 "this expression[T/F]:");
584 input
= toupper(input
);
587 func_values
[func_count
] = 1;
589 } else if (input
== 'F') {
590 func_values
[func_count
] = 0;
594 if (isatty(fileno(stdin
)) == 0)
598 fprintf(stdout
, "\nThanks!\n");
601 /* Now output the listing */
602 cur_patch
= STAILQ_FIRST(&patches
);
603 for (cur_instr
= STAILQ_FIRST(&seq_program
);
605 cur_instr
= STAILQ_NEXT(cur_instr
, links
), instrcount
++) {
607 if (check_patch(&cur_patch
, instrcount
,
608 &skip_addr
, func_values
) == 0) {
609 /* Don't count this instruction as it is in a patch
615 while (line
< cur_instr
->srcline
) {
616 fgets(buf
, sizeof(buf
), ifile
);
617 fprintf(listfile
, "\t\t%s", buf
);
620 fprintf(listfile
, "%03x %02x%02x%02x%02x", instrptr
,
621 #if BYTE_ORDER == LITTLE_ENDIAN
622 cur_instr
->format
.bytes
[0],
623 cur_instr
->format
.bytes
[1],
624 cur_instr
->format
.bytes
[2],
625 cur_instr
->format
.bytes
[3]);
627 cur_instr
->format
.bytes
[3],
628 cur_instr
->format
.bytes
[2],
629 cur_instr
->format
.bytes
[1],
630 cur_instr
->format
.bytes
[0]);
632 fgets(buf
, sizeof(buf
), ifile
);
633 fprintf(listfile
, "\t%s", buf
);
637 /* Dump the remainder of the file */
638 while(fgets(buf
, sizeof(buf
), ifile
) != NULL
)
639 fprintf(listfile
, "\t\t%s", buf
);
645 check_patch(patch_t
**start_patch
, int start_instr
,
646 int *skip_addr
, int *func_vals
)
650 cur_patch
= *start_patch
;
652 while (cur_patch
!= NULL
&& start_instr
== cur_patch
->begin
) {
653 if (func_vals
[cur_patch
->patch_func
] == 0) {
656 /* Start rejecting code */
657 *skip_addr
= start_instr
+ cur_patch
->skip_instr
;
658 for (skip
= cur_patch
->skip_patch
;
659 skip
> 0 && cur_patch
!= NULL
;
661 cur_patch
= STAILQ_NEXT(cur_patch
, links
);
663 /* Accepted this patch. Advance to the next
664 * one and wait for our intruction pointer to
667 cur_patch
= STAILQ_NEXT(cur_patch
, links
);
671 *start_patch
= cur_patch
;
672 if (start_instr
< *skip_addr
)
680 * Print out error information if appropriate, and clean up before
681 * terminating the program.
684 stop(const char *string
, int err_code
)
686 if (string
!= NULL
) {
687 fprintf(stderr
, "%s: ", appname
);
688 if (yyfilename
!= NULL
) {
689 fprintf(stderr
, "Stopped at file %s, line %d - ",
690 yyfilename
, yylineno
);
692 fprintf(stderr
, "%s\n", string
);
698 fprintf(stderr
, "%s: Removing %s due to error\n",
704 if (regfile
!= NULL
) {
707 fprintf(stderr
, "%s: Removing %s due to error\n",
708 appname
, regfilename
);
713 if (listfile
!= NULL
) {
716 fprintf(stderr
, "%s: Removing %s due to error\n",
717 appname
, listfilename
);
718 unlink(listfilename
);
722 symlist_free(&patch_functions
);
731 struct instruction
*new_instr
;
733 new_instr
= (struct instruction
*)malloc(sizeof(struct instruction
));
734 if (new_instr
== NULL
)
735 stop("Unable to malloc instruction object", EX_SOFTWARE
);
736 memset(new_instr
, 0, sizeof(*new_instr
));
737 STAILQ_INSERT_TAIL(&seq_program
, new_instr
, links
);
738 new_instr
->srcline
= yylineno
;
745 critical_section_t
*new_cs
;
747 new_cs
= (critical_section_t
*)malloc(sizeof(critical_section_t
));
749 stop("Unable to malloc critical_section object", EX_SOFTWARE
);
750 memset(new_cs
, 0, sizeof(*new_cs
));
752 TAILQ_INSERT_TAIL(&cs_tailq
, new_cs
, links
);
761 new_scope
= (scope_t
*)malloc(sizeof(scope_t
));
762 if (new_scope
== NULL
)
763 stop("Unable to malloc scope object", EX_SOFTWARE
);
764 memset(new_scope
, 0, sizeof(*new_scope
));
765 TAILQ_INIT(&new_scope
->inner_scope
);
767 if (SLIST_FIRST(&scope_stack
) != NULL
) {
768 TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack
)->inner_scope
,
769 new_scope
, scope_links
);
771 /* This patch is now the current scope */
772 SLIST_INSERT_HEAD(&scope_stack
, new_scope
, scope_stack_links
);
777 process_scope(scope_t
*scope
)
780 * We are "leaving" this scope. We should now have
781 * enough information to process the lists of scopes
785 u_int skip_patch_count
;
786 u_int skip_instr_count
;
788 cur_scope
= TAILQ_LAST(&scope
->inner_scope
, scope_tailq
);
789 skip_patch_count
= 0;
790 skip_instr_count
= 0;
791 while (cur_scope
!= NULL
) {
792 u_int patch0_patch_skip
;
794 patch0_patch_skip
= 0;
795 switch (cur_scope
->type
) {
798 if (skip_instr_count
!= 0) {
799 /* Create a tail patch */
801 cur_scope
->patches
[1].skip_patch
=
802 skip_patch_count
+ 1;
803 cur_scope
->patches
[1].skip_instr
=
807 /* Count Head patch */
810 /* Count any patches contained in our inner scope */
811 patch0_patch_skip
+= cur_scope
->inner_scope_patches
;
813 cur_scope
->patches
[0].skip_patch
= patch0_patch_skip
;
814 cur_scope
->patches
[0].skip_instr
=
815 cur_scope
->end_addr
- cur_scope
->begin_addr
;
817 skip_instr_count
+= cur_scope
->patches
[0].skip_instr
;
819 skip_patch_count
+= patch0_patch_skip
;
820 if (cur_scope
->type
== SCOPE_IF
) {
821 scope
->inner_scope_patches
+= skip_patch_count
;
822 skip_patch_count
= 0;
823 skip_instr_count
= 0;
827 /* Count any patches contained in our innter scope */
828 skip_patch_count
+= cur_scope
->inner_scope_patches
;
830 skip_instr_count
+= cur_scope
->end_addr
831 - cur_scope
->begin_addr
;
834 stop("Unexpected scope type encountered", EX_SOFTWARE
);
838 cur_scope
= TAILQ_PREV(cur_scope
, scope_tailq
, scope_links
);