2
* Aic7xxx SCSI host adapter firmware asssembler
4
* Copyright (c) 1997, 1998, 2000, 2001 Justin T. Gibbs.
5
* Copyright (c) 2001 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#16 $
42
* $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm.c,v 1.28.2.5 2002/04/29 19:36:36 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, int start_instr,
83
int *skip_addr, int *func_vals);
85
struct path_list search_path;
86
int includes_search_curdir;
97
static STAILQ_HEAD(,instruction) seq_program;
98
struct cs_tailq cs_tailq;
99
struct scope_list scope_stack;
100
symlist_t patch_functions;
103
extern int yy_flex_debug;
104
extern int mm_flex_debug;
109
extern int yyparse(void);
111
int main(int argc, char *argv[]);
114
main(int argc, char *argv[])
123
STAILQ_INIT(&patches);
124
SLIST_INIT(&search_path);
125
STAILQ_INIT(&seq_program);
126
TAILQ_INIT(&cs_tailq);
127
SLIST_INIT(&scope_stack);
129
/* Set Sentinal scope node */
130
sentinal = scope_alloc();
131
sentinal->type = SCOPE_ROOT;
133
includes_search_curdir = 1;
143
while ((ch = getopt(argc, argv, "d:l:n:o:r:I:O:")) != -1) {
147
if (strcmp(optarg, "s") == 0) {
150
} else if (strcmp(optarg, "p") == 0) {
154
fprintf(stderr, "%s: -d Requires either an "
155
"'s' or 'p' argument\n", appname);
159
stop("-d: Assembler not built with debugging "
160
"information", EX_SOFTWARE);
164
/* Create a program listing */
165
if ((listfile = fopen(optarg, "w")) == NULL) {
167
stop(NULL, EX_CANTCREAT);
169
listfilename = optarg;
172
/* Don't complain about the -nostdinc directrive */
173
if (strcmp(optarg, "ostdinc")) {
174
fprintf(stderr, "%s: Unknown option -%c%s\n",
175
appname, ch, optarg);
181
if ((ofile = fopen(optarg, "w")) == NULL) {
183
stop(NULL, EX_CANTCREAT);
188
if ((regfile = fopen(optarg, "w")) == NULL) {
190
stop(NULL, EX_CANTCREAT);
192
regfilename = optarg;
196
path_entry_t include_dir;
198
if (strcmp(optarg, "-") == 0) {
199
if (includes_search_curdir == 0) {
200
fprintf(stderr, "%s: Warning - '-I-' "
201
"specified multiple "
204
includes_search_curdir = 0;
205
for (include_dir = SLIST_FIRST(&search_path);
207
include_dir = SLIST_NEXT(include_dir,
210
* All entries before a '-I-' only
211
* apply to includes specified with
212
* quotes instead of "<>".
214
include_dir->quoted_includes_only = 1;
217
(path_entry_t)malloc(sizeof(*include_dir));
218
if (include_dir == NULL) {
220
stop(NULL, EX_OSERR);
222
include_dir->directory = strdup(optarg);
223
if (include_dir->directory == NULL) {
225
stop(NULL, EX_OSERR);
227
include_dir->quoted_includes_only = 0;
228
SLIST_INSERT_HEAD(&search_path, include_dir,
243
fprintf(stderr, "%s: No input file specifiled\n", appname);
249
inputfilename = *argv;
250
include_file(*argv, SOURCE_FILE);
253
if (SLIST_FIRST(&scope_stack) == NULL
254
|| SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) {
255
stop("Unterminated conditional expression", EX_DATAERR);
259
/* Process outmost scope */
260
process_scope(SLIST_FIRST(&scope_stack));
262
* Decend the tree of scopes and insert/emit
263
* patches as appropriate. We perform a depth first
264
* tranversal, recursively handling each scope.
266
/* start at the root scope */
267
dump_scope(SLIST_FIRST(&scope_stack));
269
/* Patch up forward jump addresses */
274
if (regfile != NULL) {
275
symtable_dump(regfile);
277
if (listfile != NULL)
278
output_listing(inputfilename);
290
(void)fprintf(stderr,
291
"usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file]
292
[-r register_output_file] [-l program_list_file]
301
struct instruction *cur_instr;
303
for (cur_instr = STAILQ_FIRST(&seq_program);
305
cur_instr = STAILQ_NEXT(cur_instr, links)) {
306
if (cur_instr->patch_label != NULL) {
307
struct ins_format3 *f3_instr;
310
if (cur_instr->patch_label->type != LABEL) {
313
snprintf(buf, sizeof(buf),
314
"Undefined label %s",
315
cur_instr->patch_label->name);
316
stop(buf, EX_DATAERR);
319
f3_instr = &cur_instr->format.format3;
320
address = f3_instr->address;
321
address += cur_instr->patch_label->info.linfo->address;
322
f3_instr->address = address;
330
struct instruction *cur_instr;
332
critical_section_t *cs;
333
symbol_node_t *cur_node;
339
* DO NOT EDIT - This file is automatically generated
340
* from the following source files:
344
fprintf(ofile, "static uint8_t seqprog[] = {\n");
345
for (cur_instr = STAILQ_FIRST(&seq_program);
347
cur_instr = STAILQ_NEXT(cur_instr, links)) {
349
fprintf(ofile, "%s\t0x%02x, 0x%02x, 0x%02x, 0x%02x",
350
cur_instr == STAILQ_FIRST(&seq_program) ? "" : ",\n",
351
#if BYTE_ORDER == LITTLE_ENDIAN
352
cur_instr->format.bytes[0],
353
cur_instr->format.bytes[1],
354
cur_instr->format.bytes[2],
355
cur_instr->format.bytes[3]);
357
cur_instr->format.bytes[3],
358
cur_instr->format.bytes[2],
359
cur_instr->format.bytes[1],
360
cur_instr->format.bytes[0]);
364
fprintf(ofile, "\n};\n\n");
366
if (patch_arg_list == NULL)
367
stop("Patch argument list not defined",
371
* Output patch information. Patch functions first.
373
for (cur_node = SLIST_FIRST(&patch_functions);
375
cur_node = SLIST_NEXT(cur_node,links)) {
377
"static int aic_patch%d_func(%s);
384
cur_node->symbol->info.condinfo->func_num,
386
cur_node->symbol->info.condinfo->func_num,
388
cur_node->symbol->name);
392
"typedef int patch_func_t (%s);
393
static struct patch {
394
patch_func_t *patch_func;
398
} patches[] = {\n", patch_arg_list);
400
for (cur_patch = STAILQ_FIRST(&patches);
402
cur_patch = STAILQ_NEXT(cur_patch,links)) {
403
fprintf(ofile, "%s\t{ aic_patch%d_func, %d, %d, %d }",
404
cur_patch == STAILQ_FIRST(&patches) ? "" : ",\n",
405
cur_patch->patch_func, cur_patch->begin,
406
cur_patch->skip_instr, cur_patch->skip_patch);
409
fprintf(ofile, "\n};\n");
415
} critical_sections[] = {\n");
417
for (cs = TAILQ_FIRST(&cs_tailq);
419
cs = TAILQ_NEXT(cs, links)) {
420
fprintf(ofile, "%s\t{ %d, %d }",
421
cs == TAILQ_FIRST(&cs_tailq) ? "" : ",\n",
422
cs->begin_addr, cs->end_addr);
425
fprintf(ofile, "\n};\n");
428
"static const int num_critical_sections = sizeof(critical_sections)
429
/ sizeof(*critical_sections);\n");
431
fprintf(stderr, "%s: %d instructions used\n", appname, instrcount);
435
dump_scope(scope_t *scope)
440
* Emit the first patch for this scope
442
emit_patch(scope, 0);
445
* Dump each scope within this one.
447
cur_scope = TAILQ_FIRST(&scope->inner_scope);
449
while (cur_scope != NULL) {
451
dump_scope(cur_scope);
453
cur_scope = TAILQ_NEXT(cur_scope, scope_links);
457
* Emit the second, closing, patch for this scope
459
emit_patch(scope, 1);
463
emit_patch(scope_t *scope, int patch)
468
pinfo = &scope->patches[patch];
470
if (pinfo->skip_instr == 0)
474
new_patch = (patch_t *)malloc(sizeof(*new_patch));
476
if (new_patch == NULL)
477
stop("Could not malloc patch structure", EX_OSERR);
479
memset(new_patch, 0, sizeof(*new_patch));
482
new_patch->patch_func = scope->func_num;
483
new_patch->begin = scope->begin_addr;
485
new_patch->patch_func = 0;
486
new_patch->begin = scope->end_addr;
488
new_patch->skip_instr = pinfo->skip_instr;
489
new_patch->skip_patch = pinfo->skip_patch;
490
STAILQ_INSERT_TAIL(&patches, new_patch, links);
494
output_listing(char *ifilename)
498
struct instruction *cur_instr;
500
symbol_node_t *cur_func;
512
if ((ifile = fopen(ifilename, "r")) == NULL) {
514
stop(NULL, EX_DATAERR);
518
* Determine which options to apply to this listing.
520
for (func_count = 0, cur_func = SLIST_FIRST(&patch_functions);
522
cur_func = SLIST_NEXT(cur_func, links))
526
if (func_count != 0) {
527
func_values = (int *)malloc(func_count * sizeof(int));
529
if (func_values == NULL)
530
stop("Could not malloc", EX_OSERR);
532
func_values[0] = 0; /* FALSE func */
536
* Ask the user to fill in the return values for
537
* the rest of the functions.
541
for (cur_func = SLIST_FIRST(&patch_functions);
542
cur_func != NULL && SLIST_NEXT(cur_func, links) != NULL;
543
cur_func = SLIST_NEXT(cur_func, links), func_count--) {
546
fprintf(stdout, "\n(%s)\n", cur_func->symbol->name);
548
"Enter the return value for "
549
"this expression[T/F]:");
554
input = toupper(input);
557
func_values[func_count] = 1;
559
} else if (input == 'F') {
560
func_values[func_count] = 0;
564
if (isatty(fileno(stdin)) == 0)
567
fprintf(stdout, "\nThanks!\n");
570
/* Now output the listing */
571
cur_patch = STAILQ_FIRST(&patches);
572
for (cur_instr = STAILQ_FIRST(&seq_program);
574
cur_instr = STAILQ_NEXT(cur_instr, links), instrcount++) {
576
if (check_patch(&cur_patch, instrcount,
577
&skip_addr, func_values) == 0) {
578
/* Don't count this instruction as it is in a patch
584
while (line < cur_instr->srcline) {
585
fgets(buf, sizeof(buf), ifile);
586
fprintf(listfile, "\t\t%s", buf);
589
fprintf(listfile, "%03x %02x%02x%02x%02x", instrptr,
590
#if BYTE_ORDER == LITTLE_ENDIAN
591
cur_instr->format.bytes[0],
592
cur_instr->format.bytes[1],
593
cur_instr->format.bytes[2],
594
cur_instr->format.bytes[3]);
596
cur_instr->format.bytes[3],
597
cur_instr->format.bytes[2],
598
cur_instr->format.bytes[1],
599
cur_instr->format.bytes[0]);
601
fgets(buf, sizeof(buf), ifile);
602
fprintf(listfile, "\t%s", buf);
606
/* Dump the remainder of the file */
607
while(fgets(buf, sizeof(buf), ifile) != NULL)
608
fprintf(listfile, "\t\t%s", buf);
614
check_patch(patch_t **start_patch, int start_instr,
615
int *skip_addr, int *func_vals)
619
cur_patch = *start_patch;
621
while (cur_patch != NULL && start_instr == cur_patch->begin) {
622
if (func_vals[cur_patch->patch_func] == 0) {
625
/* Start rejecting code */
626
*skip_addr = start_instr + cur_patch->skip_instr;
627
for (skip = cur_patch->skip_patch;
628
skip > 0 && cur_patch != NULL;
630
cur_patch = STAILQ_NEXT(cur_patch, links);
632
/* Accepted this patch. Advance to the next
633
* one and wait for our intruction pointer to
636
cur_patch = STAILQ_NEXT(cur_patch, links);
640
*start_patch = cur_patch;
641
if (start_instr < *skip_addr)
649
* Print out error information if appropriate, and clean up before
650
* terminating the program.
653
stop(const char *string, int err_code)
655
if (string != NULL) {
656
fprintf(stderr, "%s: ", appname);
657
if (yyfilename != NULL) {
658
fprintf(stderr, "Stopped at file %s, line %d - ",
659
yyfilename, yylineno);
661
fprintf(stderr, "%s\n", string);
667
fprintf(stderr, "%s: Removing %s due to error\n",
673
if (regfile != NULL) {
676
fprintf(stderr, "%s: Removing %s due to error\n",
677
appname, regfilename);
682
if (listfile != NULL) {
685
fprintf(stderr, "%s: Removing %s due to error\n",
686
appname, listfilename);
687
unlink(listfilename);
691
symlist_free(&patch_functions);
700
struct instruction *new_instr;
702
new_instr = (struct instruction *)malloc(sizeof(struct instruction));
703
if (new_instr == NULL)
704
stop("Unable to malloc instruction object", EX_SOFTWARE);
705
memset(new_instr, 0, sizeof(*new_instr));
706
STAILQ_INSERT_TAIL(&seq_program, new_instr, links);
707
new_instr->srcline = yylineno;
714
critical_section_t *new_cs;
716
new_cs= (critical_section_t *)malloc(sizeof(critical_section_t));
718
stop("Unable to malloc critical_section object", EX_SOFTWARE);
719
memset(new_cs, 0, sizeof(*new_cs));
721
TAILQ_INSERT_TAIL(&cs_tailq, new_cs, links);
730
new_scope = (scope_t *)malloc(sizeof(scope_t));
731
if (new_scope == NULL)
732
stop("Unable to malloc scope object", EX_SOFTWARE);
733
memset(new_scope, 0, sizeof(*new_scope));
734
TAILQ_INIT(&new_scope->inner_scope);
736
if (SLIST_FIRST(&scope_stack) != NULL) {
737
TAILQ_INSERT_TAIL(&SLIST_FIRST(&scope_stack)->inner_scope,
738
new_scope, scope_links);
740
/* This patch is now the current scope */
741
SLIST_INSERT_HEAD(&scope_stack, new_scope, scope_stack_links);
746
process_scope(scope_t *scope)
749
* We are "leaving" this scope. We should now have
750
* enough information to process the lists of scopes
754
u_int skip_patch_count;
755
u_int skip_instr_count;
757
cur_scope = TAILQ_LAST(&scope->inner_scope, scope_tailq);
758
skip_patch_count = 0;
759
skip_instr_count = 0;
760
while (cur_scope != NULL) {
761
u_int patch0_patch_skip;
763
patch0_patch_skip = 0;
764
switch (cur_scope->type) {
767
if (skip_instr_count != 0) {
768
/* Create a tail patch */
770
cur_scope->patches[1].skip_patch =
771
skip_patch_count + 1;
772
cur_scope->patches[1].skip_instr =
776
/* Count Head patch */
779
/* Count any patches contained in our inner scope */
780
patch0_patch_skip += cur_scope->inner_scope_patches;
782
cur_scope->patches[0].skip_patch = patch0_patch_skip;
783
cur_scope->patches[0].skip_instr =
784
cur_scope->end_addr - cur_scope->begin_addr;
786
skip_instr_count += cur_scope->patches[0].skip_instr;
788
skip_patch_count += patch0_patch_skip;
789
if (cur_scope->type == SCOPE_IF) {
790
scope->inner_scope_patches += skip_patch_count;
791
skip_patch_count = 0;
792
skip_instr_count = 0;
796
/* Count any patches contained in our innter scope */
797
skip_patch_count += cur_scope->inner_scope_patches;
799
skip_instr_count += cur_scope->end_addr
800
- cur_scope->begin_addr;
803
stop("Unexpected scope type encountered", EX_SOFTWARE);
807
cur_scope = TAILQ_PREV(cur_scope, scope_tailq, scope_links);