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 $
46 #include <sys/types.h>
57 #include <sys/queue.h>
61 #include "aicasm_symbol.h"
62 #include "aicasm_gram.h"
64 /* This is used for macro body capture too, so err on the large size. */
65 #define MAX_STR_CONST 4096
66 static char string_buf[MAX_STR_CONST];
67 static char *string_buf_ptr;
68 static int parren_count;
69 static int quote_count;
73 PATH ([/]*[-A-Za-z0-9_.])+
74 WORD [A-Za-z_][-A-Za-z_0-9]*
77 MBODY ((\\[^\n])*[^\n\\]*)+
90 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
91 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
92 <COMMENT>\n { ++yylineno; }
94 <COMMENT>"*"+[^*/\n]* ;
95 <COMMENT>"/"+[^*/\n]* ;
96 <COMMENT>"*"+"/" { BEGIN INITIAL; }
98 string_buf_ptr = string_buf;
103 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
106 if (parren_count == 0) {
109 *string_buf_ptr = '\0';
110 yylval.sym = symtable_get(string_buf);
113 *string_buf_ptr++ = ')';
116 <CEXPR>\n { ++yylineno; }
121 while (*yptr != '\0') {
122 /* Remove duplicate spaces */
126 && string_buf_ptr != string_buf
127 && string_buf_ptr[-1] == ' ')
130 *string_buf_ptr++ = *yptr++;
134 VERSION { return T_VERSION; }
135 PREFIX { return T_PREFIX; }
136 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
138 string_buf_ptr = string_buf;
146 *string_buf_ptr++ = *yptr++;
151 *string_buf_ptr = '\0';
152 yylval.str = string_buf;
157 /* Register/SCB/SRAM definition keywords */
158 export { return T_EXPORT; }
159 register { return T_REGISTER; }
160 const { yylval.value = FALSE; return T_CONST; }
161 download { return T_DOWNLOAD; }
162 address { return T_ADDRESS; }
163 access_mode { return T_ACCESS_MODE; }
164 modes { return T_MODES; }
166 if (strcmp(yytext, "RW") == 0)
168 else if (strcmp(yytext, "RO") == 0)
174 BEGIN_CRITICAL { return T_BEGIN_CS; }
175 END_CRITICAL { return T_END_CS; }
176 SET_SRC_MODE { return T_SET_SRC_MODE; }
177 SET_DST_MODE { return T_SET_DST_MODE; }
178 field { return T_FIELD; }
179 enum { return T_ENUM; }
180 mask { return T_MASK; }
181 alias { return T_ALIAS; }
182 size { return T_SIZE; }
183 scb { return T_SCB; }
184 scratch_ram { return T_SRAM; }
185 accumulator { return T_ACCUM; }
186 mode_pointer { return T_MODE_PTR; }
187 allones { return T_ALLONES; }
188 allzeros { return T_ALLZEROS; }
189 none { return T_NONE; }
190 sindex { return T_SINDEX; }
194 shl { return T_SHL; }
195 shr { return T_SHR; }
196 ror { return T_ROR; }
197 rol { return T_ROL; }
198 mvi { return T_MVI; }
199 mov { return T_MOV; }
200 clr { return T_CLR; }
201 jmp { return T_JMP; }
203 jnc { return T_JNC; }
205 jne { return T_JNE; }
207 jnz { return T_JNZ; }
208 call { return T_CALL; }
209 add { return T_ADD; }
210 adc { return T_ADC; }
211 bmov { return T_BMOV; }
212 inc { return T_INC; }
213 dec { return T_DEC; }
214 stc { return T_STC; }
215 clc { return T_CLC; }
216 cmp { return T_CMP; }
217 not { return T_NOT; }
218 xor { return T_XOR; }
219 test { return T_TEST;}
220 and { return T_AND; }
222 ret { return T_RET; }
223 nop { return T_NOP; }
224 else { return T_ELSE; }
226 /* Allowed Symbols */
227 \<\< { return T_EXPR_LSHIFT; }
228 \>\> { return T_EXPR_RSHIFT; }
229 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
231 /* Number processing */
233 yylval.value = strtol(yytext, NULL, 8);
238 yylval.value = strtoul(yytext + 2, NULL, 16);
243 yylval.value = strtol(yytext, NULL, 10);
252 <INCLUDE>[<] { return yytext[0]; }
253 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
255 if (quote_count != 0)
264 string_buf_ptr = string_buf;
266 *string_buf_ptr++ = *yptr++;
267 yylval.str = string_buf;
268 *string_buf_ptr = '\0';
271 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
276 <MACRODEF>{WORD}{SPACE} {
279 /* Strip space and return as a normal symbol */
281 while (*yptr != ' ' && *yptr != '\t')
284 yylval.sym = symtable_get(yytext);
285 string_buf_ptr = string_buf;
291 * We store the symbol with its opening
292 * parren so we can differentiate macros
293 * that take args from macros with the
294 * same name that do not take args as
298 yylval.sym = symtable_get(yytext);
302 <MACROARGLIST>{WORD} {
306 <MACROARGLIST>{SPACE} ;
311 string_buf_ptr = string_buf;
316 snprintf(buf, sizeof(buf), "Invalid character "
317 "'%c' in macro argument list",
319 stop(buf, EX_DATAERR);
321 <MACROCALLARGS>{SPACE} ;
324 if (parren_count == 1)
326 *string_buf_ptr++ = '(';
330 if (parren_count == 0) {
334 *string_buf_ptr++ = ')';
336 <MACROCALLARGS>{MCARG} {
341 *string_buf_ptr++ = *yptr++;
344 if (string_buf_ptr != string_buf) {
346 * Return an argument and
347 * rescan this comma so we
348 * can return it as well.
350 *string_buf_ptr = '\0';
351 yylval.str = string_buf;
352 string_buf_ptr = string_buf;
359 /* Eat escaped newlines. */
363 /* Macros end on the first unescaped newline. */
365 *string_buf_ptr = '\0';
366 yylval.str = string_buf;
375 *string_buf_ptr++ = *yptr++;
381 /* May be a symbol or a macro invocation. */
382 yylval.sym = symtable_get(yytext);
383 if (yylval.sym->type == MACRO) {
384 YY_BUFFER_STATE old_state;
385 YY_BUFFER_STATE temp_state;
387 ycopy = strdup(yytext);
388 yptr = ycopy + yyleng;
391 old_state = YY_CURRENT_BUFFER;
393 yy_create_buffer(stdin,
395 yy_switch_to_buffer(temp_state);
396 mm_switch_to_buffer(old_state);
398 mm_switch_to_buffer(temp_state);
399 yy_switch_to_buffer(old_state);
400 mm_delete_buffer(temp_state);
401 expand_macro(yylval.sym);
403 if (yylval.sym->type == UNINITIALIZED) {
404 /* Try without the '(' */
405 symbol_delete(yylval.sym);
406 yytext[yyleng-1] = '\0';
408 symtable_get(yytext);
415 yylval.sym = symtable_get(yytext);
416 if (yylval.sym->type == MACRO) {
417 expand_macro(yylval.sym);
423 snprintf(buf, sizeof(buf), "Invalid character "
425 stop(buf, EX_DATAERR);
429 typedef struct include {
430 YY_BUFFER_STATE buffer;
433 SLIST_ENTRY(include) links;
436 SLIST_HEAD(, include) include_stack;
439 include_file(char *file_name, include_type type)
445 /* Try the current directory first */
446 if (includes_search_curdir != 0 || type == SOURCE_FILE)
447 newfile = fopen(file_name, "r");
449 if (newfile == NULL && type != SOURCE_FILE) {
450 path_entry_t include_dir;
451 for (include_dir = search_path.slh_first;
453 include_dir = include_dir->links.sle_next) {
454 char fullname[PATH_MAX];
456 if ((include_dir->quoted_includes_only == TRUE)
457 && (type != QUOTED_INCLUDE))
460 snprintf(fullname, sizeof(fullname),
461 "%s/%s", include_dir->directory, file_name);
463 if ((newfile = fopen(fullname, "r")) != NULL)
468 if (newfile == NULL) {
470 stop("Unable to open input file", EX_SOFTWARE);
474 if (type != SOURCE_FILE) {
475 include = (include_t *)malloc(sizeof(include_t));
476 if (include == NULL) {
477 stop("Unable to allocate include stack entry",
481 include->buffer = YY_CURRENT_BUFFER;
482 include->lineno = yylineno;
483 include->filename = yyfilename;
484 SLIST_INSERT_HEAD(&include_stack, include, links);
486 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
488 yyfilename = strdup(file_name);
491 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
492 const char **next_match,
493 struct macro_arg **match_marg, regmatch_t *match);
496 expand_macro(struct symbol *macro_symbol)
498 struct macro_arg *marg;
499 struct macro_arg *match_marg;
500 const char *body_head;
501 const char *body_pos;
502 const char *next_match;
505 * Due to the nature of unput, we must work
506 * backwards through the macro body performing
509 body_head = macro_symbol->info.macroinfo->body;
510 body_pos = body_head + strlen(body_head);
511 while (body_pos > body_head) {
514 next_match = body_head;
516 next_substitution(macro_symbol, body_pos, &next_match,
517 &match_marg, &match);
519 /* Put back everything up until the replacement. */
520 while (body_pos > next_match)
523 /* Perform the replacement. */
524 if (match_marg != NULL) {
527 next_match = match_marg->replacement_text;
528 strp = next_match + strlen(next_match);
529 while (strp > next_match)
532 /* Skip past the unexpanded macro arg. */
533 body_pos -= match.rm_eo - match.rm_so;
537 /* Cleanup replacement text. */
538 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
539 free(marg->replacement_text);
544 * Find the next substitution in the macro working backwards from
545 * body_pos until the beginning of the macro buffer. next_match
546 * should be initialized to the beginning of the macro buffer prior
547 * to calling this routine.
550 next_substitution(struct symbol *mac_symbol, const char *body_pos,
551 const char **next_match, struct macro_arg **match_marg,
554 regmatch_t matches[2];
555 struct macro_arg *marg;
556 const char *search_pos;
560 search_pos = *next_match;
562 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
564 retval = regexec(&marg->arg_regex, search_pos, 2,
567 && (matches[1].rm_eo + search_pos) <= body_pos
568 && (matches[1].rm_eo + search_pos) > *next_match) {
570 *next_match = match->rm_eo + search_pos;
574 } while (search_pos != *next_match);
582 yy_delete_buffer(YY_CURRENT_BUFFER);
584 if (yyfilename != NULL)
587 include = include_stack.slh_first;
588 if (include != NULL) {
589 yy_switch_to_buffer(include->buffer);
590 yylineno = include->lineno;
591 yyfilename = include->filename;
592 SLIST_REMOVE_HEAD(&include_stack, links);