📄 aicasm.c
字号:
/* * Aic7xxx SCSI host adapter firmware asssembler * * Copyright (c) 1997, 1998 Justin T. Gibbs. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification, immediately at the beginning of the file. * 2. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Id: aicasm.c,v 1.20.2.1 1999/03/23 07:41:27 gibbs Exp $ */#include <sys/types.h>#include <sys/mman.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sysexits.h>#include <unistd.h>#include "aicasm.h"#include "aicasm_symbol.h"#include "sequencer.h"typedef struct patch { STAILQ_ENTRY(patch) links; int patch_func; u_int begin; u_int skip_instr; u_int skip_patch;} patch_t;STAILQ_HEAD(patch_list, patch) patches;static void usage(void);static void back_patch(void);static void output_code(FILE *ofile);static void output_listing(FILE *listfile, char *ifilename);static int dump_scope(scope_t *scope);static void emit_patch(scope_t *scope, int patch);static int check_patch(patch_t **start_patch, int start_instr, int *skip_addr, int *func_vals);struct path_list search_path;int includes_search_curdir;char *appname;FILE *ofile;char *ofilename;char *regfilename;FILE *regfile;char *listfilename;FILE *listfile;static STAILQ_HEAD(,instruction) seq_program;struct scope_list scope_stack;symlist_t patch_functions;#if DEBUGextern int yy_flex_debug;extern int yydebug;#endifextern FILE *yyin;extern int yyparse __P((void));intmain(argc, argv) int argc; char *argv[];{ extern char *optarg; extern int optind; int ch; int retval; char *inputfilename; scope_t *sentinal; STAILQ_INIT(&patches); SLIST_INIT(&search_path); STAILQ_INIT(&seq_program); SLIST_INIT(&scope_stack); /* Set Sentinal scope node */ sentinal = scope_alloc(); sentinal->type = SCOPE_ROOT; includes_search_curdir = 1; appname = *argv; regfile = NULL; listfile = NULL;#if DEBUG yy_flex_debug = 0; yydebug = 0;#endif while ((ch = getopt(argc, argv, "d:l:n:o:r:I:O:")) != -1) { switch(ch) { case 'd':#if DEBUG if (strcmp(optarg, "s") == 0) { yy_flex_debug = 1; } else if (strcmp(optarg, "p") == 0) { yydebug = 1; } else { fprintf(stderr, "%s: -d Requires either an " "'s' or 'p' argument\n", appname); usage(); }#else stop("-d: Assembler not built with debugging " "information", EX_SOFTWARE);#endif break; case 'l': /* Create a program listing */ if ((listfile = fopen(optarg, "w")) == NULL) { perror(optarg); stop(NULL, EX_CANTCREAT); } listfilename = optarg; break; case 'n': /* Don't complain about the -nostdinc directrive */ if (strcmp(optarg, "ostdinc")) { fprintf(stderr, "%s: Unknown option -%c%s\n", appname, ch, optarg); usage(); /* NOTREACHED */ } break; case 'o': if ((ofile = fopen(optarg, "w")) == NULL) { perror(optarg); stop(NULL, EX_CANTCREAT); } ofilename = optarg; break; case 'r': if ((regfile = fopen(optarg, "w")) == NULL) { perror(optarg); stop(NULL, EX_CANTCREAT); } regfilename = optarg; break; case 'I': { path_entry_t include_dir; if (strcmp(optarg, "-") == 0) { if (includes_search_curdir == 0) { fprintf(stderr, "%s: Warning - '-I-' " "specified multiple " "times\n", appname); } includes_search_curdir = 0; for (include_dir = search_path.slh_first; include_dir != NULL; include_dir = include_dir->links.sle_next) /* * All entries before a '-I-' only * apply to includes specified with * quotes instead of "<>". */ include_dir->quoted_includes_only = 1; } else { include_dir = (path_entry_t)malloc(sizeof(*include_dir)); if (include_dir == NULL) { perror(optarg); stop(NULL, EX_OSERR); } include_dir->directory = strdup(optarg); if (include_dir->directory == NULL) { perror(optarg); stop(NULL, EX_OSERR); } include_dir->quoted_includes_only = 0; SLIST_INSERT_HEAD(&search_path, include_dir, links); } break; } case '?': default: usage(); /* NOTREACHED */ } } argc -= optind; argv += optind; if (argc != 1) { fprintf(stderr, "%s: No input file specifiled\n", appname); usage(); /* NOTREACHED */ } symtable_open(); inputfilename = *argv; include_file(*argv, SOURCE_FILE); retval = yyparse(); if (retval == 0) { if (SLIST_FIRST(&scope_stack) == NULL || SLIST_FIRST(&scope_stack)->type != SCOPE_ROOT) { stop("Unterminated conditional expression", EX_DATAERR); /* NOTREACHED */ } /* Process outmost scope */ process_scope(SLIST_FIRST(&scope_stack)); /* * Decend the tree of scopes and insert/emit * patches as appropriate. We perform a depth first * tranversal, recursively handling each scope. */ /* start at the root scope */ dump_scope(SLIST_FIRST(&scope_stack)); /* Patch up forward jump addresses */ back_patch(); if (ofile != NULL) output_code(ofile); if (regfile != NULL) { symtable_dump(regfile); } if (listfile != NULL) output_listing(listfile, inputfilename); } stop(NULL, 0); /* NOTREACHED */ return (0);}static voidusage(){ (void)fprintf(stderr,"usage: %-16s [-nostdinc] [-I-] [-I directory] [-o output_file] [-r register_output_file] [-l program_list_file] input_file\n", appname); exit(EX_USAGE);}static voidback_patch(){ struct instruction *cur_instr; for(cur_instr = seq_program.stqh_first; cur_instr != NULL; cur_instr = cur_instr->links.stqe_next) { if (cur_instr->patch_label != NULL) { struct ins_format3 *f3_instr; u_int address; if (cur_instr->patch_label->type != LABEL) { char buf[255]; snprintf(buf, sizeof(buf), "Undefined label %s", cur_instr->patch_label->name); stop(buf, EX_DATAERR); /* NOTREACHED */ } f3_instr = &cur_instr->format.format3; address = f3_instr->address; address += cur_instr->patch_label->info.linfo->address; f3_instr->address = address; } }}static voidoutput_code(ofile) FILE *ofile;{ struct instruction *cur_instr; patch_t *cur_patch; symbol_node_t *cur_node; int instrcount; instrcount = 0; fprintf(ofile,"/* * DO NOT EDIT - This file is automatically generated. */\n"); fprintf(ofile, "static u_int8_t seqprog[] = {\n"); for(cur_instr = seq_program.stqh_first; cur_instr != NULL; cur_instr = cur_instr->links.stqe_next) { fprintf(ofile, "\t0x%02x, 0x%02x, 0x%02x, 0x%02x,\n", cur_instr->format.bytes[0], cur_instr->format.bytes[1], cur_instr->format.bytes[2], cur_instr->format.bytes[3]); instrcount++; } fprintf(ofile, "};\n\n"); /* * Output patch information. Patch functions first. */ for(cur_node = SLIST_FIRST(&patch_functions); cur_node != NULL; cur_node = SLIST_NEXT(cur_node,links)) { fprintf(ofile,"static int ahc_patch%d_func(struct ahc_softc *ahc);static intahc_patch%d_func(struct ahc_softc *ahc){ return (%s);}\n\n", cur_node->symbol->info.condinfo->func_num, cur_node->symbol->info.condinfo->func_num, cur_node->symbol->name); } fprintf(ofile,"typedef int patch_func_t __P((struct ahc_softc *));struct patch { patch_func_t *patch_func; u_int32_t begin :10, skip_instr :10, skip_patch :12;} patches[] = {\n"); for(cur_patch = STAILQ_FIRST(&patches); cur_patch != NULL; cur_patch = STAILQ_NEXT(cur_patch,links)) { fprintf(ofile, "\t{ ahc_patch%d_func, %d, %d, %d },\n", cur_patch->patch_func, cur_patch->begin, cur_patch->skip_instr, cur_patch->skip_patch); } fprintf(ofile, "\n};\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -