aicasm_scan.l revision 23925
1%{ 2/* 3 * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler. 4 * 5 * Copyright (c) 1997 Justin T. Gibbs. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice immediately at the beginning of the file, without modification, 13 * this list of conditions, and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $Id$ 33 */ 34 35#include <limits.h> 36#include <stdio.h> 37#include <string.h> 38#include <sysexits.h> 39#include <sys/queue.h> 40 41#include "aic7xxx_asm.h" 42#include "symbol.h" 43#include "y.tab.h" 44%} 45 46PATH [-/A-Za-z0-9_.]*[./][-/A-Za-z0-9_.]* 47WORD [A-Za-z_][-A-Za-z_0-9]* 48SPACE [ \t]+ 49 50%x COMMENT 51 52%% 53\n { ++yylineno; } 54"/*" { BEGIN COMMENT; /* Enter comment eating state */ } 55<COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); } 56<COMMENT>\n { ++yylineno; } 57<COMMENT>[^*/\n]* ; 58<COMMENT>"*"+[^*/\n]* ; 59<COMMENT>"/"+[^*/\n]* ; 60<COMMENT>"*"+"/" { BEGIN INITIAL; } 61 62{SPACE} ; 63 64 /* Register/SCB/SRAM definition keywords */ 65register { return T_REGISTER; } 66const { yylval.value = FALSE; return T_CONST; } 67address { return T_ADDRESS; } 68access_mode { return T_ACCESS_MODE; } 69RW|RO|WO { 70 if (strcmp(yytext, "RW") == 0) 71 yylval.value = RW; 72 else if (strcmp(yytext, "RO") == 0) 73 yylval.value = RO; 74 else 75 yylval.value = WO; 76 return T_MODE; 77 } 78bit { return T_BIT; } 79mask { return T_MASK; } 80alias { return T_ALIAS; } 81size { return T_SIZE; } 82scb { return T_SCB; } 83scratch_ram { return T_SRAM; } 84accumulator { return T_ACCUM; } 85allones { return T_ALLONES; } 86allzeros { return T_ALLZEROS; } 87none { return T_NONE; } 88sindex { return T_SINDEX; } 89A { return T_A; } 90 91 /* Opcodes */ 92shl { return T_SHL; } 93shr { return T_SHR; } 94ror { return T_ROR; } 95rol { return T_ROL; } 96mvi { return T_MVI; } 97mov { return T_MOV; } 98clr { return T_CLR; } 99jmp { return T_JMP; } 100jc { return T_JC; } 101jnc { return T_JNC; } 102je { return T_JE; } 103jne { return T_JNE; } 104jz { return T_JZ; } 105jnz { return T_JNZ; } 106call { return T_CALL; } 107add { return T_ADD; } 108adc { return T_ADC; } 109inc { return T_INC; } 110dec { return T_DEC; } 111stc { return T_STC; } 112clc { return T_CLC; } 113cmp { return T_CMP; } 114xor { return T_XOR; } 115test { return T_TEST;} 116and { return T_AND; } 117or { return T_OR; } 118ret { return T_RET; } 119nop { return T_NOP; } 120.if { return T_IF; } 121.else { return T_ELSE; } 122.endif { return T_ENDIF; } 123 124 /* Allowed Symbols */ 125[-+,:()~|&."{};<>[\]!] { return yytext[0]; } 126 127 /* Number processing */ 1280[0-7]* { 129 yylval.value = strtol(yytext, NULL, 8); 130 return T_NUMBER; 131 } 132 1330[xX][0-9a-fA-F]+ { 134 yylval.value = strtoul(yytext + 2, NULL, 16); 135 return T_NUMBER; 136 } 137 138[1-9][0-9]* { 139 yylval.value = strtol(yytext, NULL, 10); 140 return T_NUMBER; 141 } 142 143 /* Include Files */ 144#include { return T_INCLUDE; } 145 146 /* For parsing C include files with #define foo */ 147#define { yylval.value = TRUE; return T_CONST; } 148 /* Throw away macros */ 149#define[^\n]*[()]+[^\n]* ; 150{PATH} { yylval.str = strdup(yytext); return T_PATH; } 151 152{WORD} { yylval.sym = symtable_get(yytext); return T_SYMBOL; } 153 154. { 155 char buf[255]; 156 157 snprintf(buf, sizeof(buf), "Invalid character " 158 "'%c'", yytext[0]); 159 stop(buf, EX_DATAERR); 160 } 161%% 162 163typedef struct include { 164 YY_BUFFER_STATE buffer; 165 int lineno; 166 char *filename; 167 SLIST_ENTRY(include) links; 168}include_t; 169 170SLIST_HEAD(, include) include_stack; 171 172void 173include_file(file_name, type) 174 char *file_name; 175 include_type type; 176{ 177 FILE *newfile; 178 include_t *include; 179 180 newfile = NULL; 181 /* Try the current directory first */ 182 if (includes_search_curdir != 0 || type == SOURCE_FILE) 183 newfile = fopen(file_name, "r"); 184 185 if (newfile == NULL && type != SOURCE_FILE) { 186 path_entry_t include_dir; 187 for (include_dir = search_path.slh_first; 188 include_dir != NULL; 189 include_dir = include_dir->links.sle_next) { 190 char fullname[PATH_MAX]; 191 192 if ((include_dir->quoted_includes_only == TRUE) 193 && (type != QUOTED_INCLUDE)) 194 continue; 195 196 snprintf(fullname, sizeof(fullname), 197 "%s/%s", include_dir->directory, file_name); 198 199 if ((newfile = fopen(fullname, "r")) != NULL) 200 break; 201 } 202 } 203 204 if (newfile == NULL) { 205 perror(file_name); 206 stop("Unable to open input file", EX_SOFTWARE); 207 /* NOTREACHED */ 208 } 209 include = (include_t *)malloc(sizeof(include_t)); 210 if (include == NULL) { 211 stop("Unable to allocate include stack entry", EX_SOFTWARE); 212 /* NOTREACHED */ 213 } 214 include->buffer = YY_CURRENT_BUFFER; 215 include->lineno = yylineno; 216 include->filename = yyfilename; 217 SLIST_INSERT_HEAD(&include_stack, include, links); 218 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE)); 219 yylineno = 1; 220 yyfilename = strdup(file_name); 221} 222 223int 224yywrap() 225{ 226 include_t *include; 227 228 yy_delete_buffer(YY_CURRENT_BUFFER); 229 (void)fclose(yyin); 230 if (yyfilename != NULL) 231 free(yyfilename); 232 include = include_stack.slh_first; 233 if (include != NULL) { 234 yy_switch_to_buffer(include->buffer); 235 yylineno = include->lineno; 236 yyfilename = include->filename; 237 SLIST_REMOVE_HEAD(&include_stack, links); 238 free(include); 239 return (0); 240 } 241 return (1); 242} 243