3 * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
5 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
6 * Copyright (c) 2001, 2002 Adaptec Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
20 * 3. Neither the names of the above-listed copyright holders nor the names
21 * of any contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * Alternatively, this software may be distributed under the terms of the
25 * GNU General Public License ("GPL") version 2 as published by the Free
26 * Software Foundation.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
38 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGES.
41 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_scan.l#18 $
43 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_scan.l,v 1.13.2.6 2002/09/27 15:46:28 gibbs Exp $
44 * $DragonFly: src/sys/dev/disk/aic7xxx/aicasm/aicasm_scan.l,v 1.2 2003/06/17 04:28:22 dillon Exp $
47 #include <sys/types.h>
58 #include <sys/queue.h>
62 #include "aicasm_symbol.h"
63 #include "aicasm_gram.h"
65 /* This is used for macro body capture too, so err on the large size. */
66 #define MAX_STR_CONST 4096
67 static char string_buf[MAX_STR_CONST];
68 static char *string_buf_ptr;
69 static int parren_count;
70 static int quote_count;
74 PATH ([/]*[-A-Za-z0-9_.])+
75 WORD [A-Za-z_][-A-Za-z_0-9]*
78 MBODY ((\\[^\n])*[^\n\\]*)+
91 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
92 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
93 <COMMENT>\n { ++yylineno; }
95 <COMMENT>"*"+[^*/\n]* ;
96 <COMMENT>"/"+[^*/\n]* ;
97 <COMMENT>"*"+"/" { BEGIN INITIAL; }
99 string_buf_ptr = string_buf;
104 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
107 if (parren_count == 0) {
110 *string_buf_ptr = '\0';
111 yylval.sym = symtable_get(string_buf);
114 *string_buf_ptr++ = ')';
117 <CEXPR>\n { ++yylineno; }
122 while (*yptr != '\0') {
123 /* Remove duplicate spaces */
127 && string_buf_ptr != string_buf
128 && string_buf_ptr[-1] == ' ')
131 *string_buf_ptr++ = *yptr++;
135 VERSION { return T_VERSION; }
136 PREFIX { return T_PREFIX; }
137 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
139 string_buf_ptr = string_buf;
147 *string_buf_ptr++ = *yptr++;
152 *string_buf_ptr = '\0';
153 yylval.str = string_buf;
158 /* Register/SCB/SRAM definition keywords */
159 export { return T_EXPORT; }
160 register { return T_REGISTER; }
161 const { yylval.value = FALSE; return T_CONST; }
162 download { return T_DOWNLOAD; }
163 address { return T_ADDRESS; }
164 access_mode { return T_ACCESS_MODE; }
165 modes { return T_MODES; }
167 if (strcmp(yytext, "RW") == 0)
169 else if (strcmp(yytext, "RO") == 0)
175 BEGIN_CRITICAL { return T_BEGIN_CS; }
176 END_CRITICAL { return T_END_CS; }
177 SET_SRC_MODE { return T_SET_SRC_MODE; }
178 SET_DST_MODE { return T_SET_DST_MODE; }
179 field { return T_FIELD; }
180 enum { return T_ENUM; }
181 mask { return T_MASK; }
182 alias { return T_ALIAS; }
183 size { return T_SIZE; }
184 scb { return T_SCB; }
185 scratch_ram { return T_SRAM; }
186 accumulator { return T_ACCUM; }
187 mode_pointer { return T_MODE_PTR; }
188 allones { return T_ALLONES; }
189 allzeros { return T_ALLZEROS; }
190 none { return T_NONE; }
191 sindex { return T_SINDEX; }
195 shl { return T_SHL; }
196 shr { return T_SHR; }
197 ror { return T_ROR; }
198 rol { return T_ROL; }
199 mvi { return T_MVI; }
200 mov { return T_MOV; }
201 clr { return T_CLR; }
202 jmp { return T_JMP; }
204 jnc { return T_JNC; }
206 jne { return T_JNE; }
208 jnz { return T_JNZ; }
209 call { return T_CALL; }
210 add { return T_ADD; }
211 adc { return T_ADC; }
212 bmov { return T_BMOV; }
213 inc { return T_INC; }
214 dec { return T_DEC; }
215 stc { return T_STC; }
216 clc { return T_CLC; }
217 cmp { return T_CMP; }
218 not { return T_NOT; }
219 xor { return T_XOR; }
220 test { return T_TEST;}
221 and { return T_AND; }
223 ret { return T_RET; }
224 nop { return T_NOP; }
225 else { return T_ELSE; }
227 /* Allowed Symbols */
228 \<\< { return T_EXPR_LSHIFT; }
229 \>\> { return T_EXPR_RSHIFT; }
230 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
232 /* Number processing */
234 yylval.value = strtol(yytext, NULL, 8);
239 yylval.value = strtoul(yytext + 2, NULL, 16);
244 yylval.value = strtol(yytext, NULL, 10);
253 <INCLUDE>[<] { return yytext[0]; }
254 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
256 if (quote_count != 0)
265 string_buf_ptr = string_buf;
267 *string_buf_ptr++ = *yptr++;
268 yylval.str = string_buf;
269 *string_buf_ptr = '\0';
272 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
277 <MACRODEF>{WORD}{SPACE} {
280 /* Strip space and return as a normal symbol */
282 while (*yptr != ' ' && *yptr != '\t')
285 yylval.sym = symtable_get(yytext);
286 string_buf_ptr = string_buf;
292 * We store the symbol with its opening
293 * parren so we can differentiate macros
294 * that take args from macros with the
295 * same name that do not take args as
299 yylval.sym = symtable_get(yytext);
303 <MACROARGLIST>{WORD} {
307 <MACROARGLIST>{SPACE} ;
312 string_buf_ptr = string_buf;
317 snprintf(buf, sizeof(buf), "Invalid character "
318 "'%c' in macro argument list",
320 stop(buf, EX_DATAERR);
322 <MACROCALLARGS>{SPACE} ;
325 if (parren_count == 1)
327 *string_buf_ptr++ = '(';
331 if (parren_count == 0) {
335 *string_buf_ptr++ = ')';
337 <MACROCALLARGS>{MCARG} {
342 *string_buf_ptr++ = *yptr++;
345 if (string_buf_ptr != string_buf) {
347 * Return an argument and
348 * rescan this comma so we
349 * can return it as well.
351 *string_buf_ptr = '\0';
352 yylval.str = string_buf;
353 string_buf_ptr = string_buf;
360 /* Eat escaped newlines. */
364 /* Macros end on the first unescaped newline. */
366 *string_buf_ptr = '\0';
367 yylval.str = string_buf;
376 *string_buf_ptr++ = *yptr++;
382 /* May be a symbol or a macro invocation. */
383 yylval.sym = symtable_get(yytext);
384 if (yylval.sym->type == MACRO) {
385 YY_BUFFER_STATE old_state;
386 YY_BUFFER_STATE temp_state;
388 ycopy = strdup(yytext);
389 yptr = ycopy + yyleng;
392 old_state = YY_CURRENT_BUFFER;
394 yy_create_buffer(stdin,
396 yy_switch_to_buffer(temp_state);
397 mm_switch_to_buffer(old_state);
399 mm_switch_to_buffer(temp_state);
400 yy_switch_to_buffer(old_state);
401 mm_delete_buffer(temp_state);
402 expand_macro(yylval.sym);
404 if (yylval.sym->type == UNINITIALIZED) {
405 /* Try without the '(' */
406 symbol_delete(yylval.sym);
407 yytext[yyleng-1] = '\0';
409 symtable_get(yytext);
416 yylval.sym = symtable_get(yytext);
417 if (yylval.sym->type == MACRO) {
418 expand_macro(yylval.sym);
424 snprintf(buf, sizeof(buf), "Invalid character "
426 stop(buf, EX_DATAERR);
430 typedef struct include {
431 YY_BUFFER_STATE buffer;
434 SLIST_ENTRY(include) links;
437 SLIST_HEAD(, include) include_stack;
440 include_file(char *file_name, include_type type)
446 /* Try the current directory first */
447 if (includes_search_curdir != 0 || type == SOURCE_FILE)
448 newfile = fopen(file_name, "r");
450 if (newfile == NULL && type != SOURCE_FILE) {
451 path_entry_t include_dir;
452 for (include_dir = search_path.slh_first;
454 include_dir = include_dir->links.sle_next) {
455 char fullname[PATH_MAX];
457 if ((include_dir->quoted_includes_only == TRUE)
458 && (type != QUOTED_INCLUDE))
461 snprintf(fullname, sizeof(fullname),
462 "%s/%s", include_dir->directory, file_name);
464 if ((newfile = fopen(fullname, "r")) != NULL)
469 if (newfile == NULL) {
471 stop("Unable to open input file", EX_SOFTWARE);
475 if (type != SOURCE_FILE) {
476 include = (include_t *)malloc(sizeof(include_t));
477 if (include == NULL) {
478 stop("Unable to allocate include stack entry",
482 include->buffer = YY_CURRENT_BUFFER;
483 include->lineno = yylineno;
484 include->filename = yyfilename;
485 SLIST_INSERT_HEAD(&include_stack, include, links);
487 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
489 yyfilename = strdup(file_name);
492 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
493 const char **next_match,
494 struct macro_arg **match_marg, regmatch_t *match);
497 expand_macro(struct symbol *macro_symbol)
499 struct macro_arg *marg;
500 struct macro_arg *match_marg;
501 const char *body_head;
502 const char *body_pos;
503 const char *next_match;
506 * Due to the nature of unput, we must work
507 * backwards through the macro body performing
510 body_head = macro_symbol->info.macroinfo->body;
511 body_pos = body_head + strlen(body_head);
512 while (body_pos > body_head) {
515 next_match = body_head;
517 next_substitution(macro_symbol, body_pos, &next_match,
518 &match_marg, &match);
520 /* Put back everything up until the replacement. */
521 while (body_pos > next_match)
524 /* Perform the replacement. */
525 if (match_marg != NULL) {
528 next_match = match_marg->replacement_text;
529 strp = next_match + strlen(next_match);
530 while (strp > next_match)
533 /* Skip past the unexpanded macro arg. */
534 body_pos -= match.rm_eo - match.rm_so;
538 /* Cleanup replacement text. */
539 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
540 free(marg->replacement_text);
545 * Find the next substitution in the macro working backwards from
546 * body_pos until the beginning of the macro buffer. next_match
547 * should be initialized to the beginning of the macro buffer prior
548 * to calling this routine.
551 next_substitution(struct symbol *mac_symbol, const char *body_pos,
552 const char **next_match, struct macro_arg **match_marg,
555 regmatch_t matches[2];
556 struct macro_arg *marg;
557 const char *search_pos;
561 search_pos = *next_match;
563 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
565 retval = regexec(&marg->arg_regex, search_pos, 2,
568 && (matches[1].rm_eo + search_pos) <= body_pos
569 && (matches[1].rm_eo + search_pos) > *next_match) {
571 *next_match = match->rm_eo + search_pos;
575 } while (search_pos != *next_match);
583 yy_delete_buffer(YY_CURRENT_BUFFER);
585 if (yyfilename != NULL)
588 include = include_stack.slh_first;
589 if (include != NULL) {
590 yy_switch_to_buffer(include->buffer);
591 yylineno = include->lineno;
592 yyfilename = include->filename;
593 SLIST_REMOVE_HEAD(&include_stack, links);