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#20 $
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\\]*)+
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; }
123 while (*yptr != '\0') {
124 /* Remove duplicate spaces */
128 && string_buf_ptr != string_buf
129 && string_buf_ptr[-1] == ' ')
132 *string_buf_ptr++ = *yptr++;
135 else { return T_ELSE; }
136 VERSION { return T_VERSION; }
137 PREFIX { return T_PREFIX; }
138 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
140 string_buf_ptr = string_buf;
148 *string_buf_ptr++ = *yptr++;
153 *string_buf_ptr = '\0';
154 yylval.str = string_buf;
159 /* Register/SCB/SRAM definition keywords */
160 export { return T_EXPORT; }
161 register { return T_REGISTER; }
162 const { yylval.value = FALSE; return T_CONST; }
163 download { return T_DOWNLOAD; }
164 address { return T_ADDRESS; }
165 access_mode { return T_ACCESS_MODE; }
166 modes { return T_MODES; }
168 if (strcmp(yytext, "RW") == 0)
170 else if (strcmp(yytext, "RO") == 0)
176 field { return T_FIELD; }
177 enum { return T_ENUM; }
178 mask { return T_MASK; }
179 alias { return T_ALIAS; }
180 size { return T_SIZE; }
181 scb { return T_SCB; }
182 scratch_ram { return T_SRAM; }
183 accumulator { return T_ACCUM; }
184 mode_pointer { return T_MODE_PTR; }
185 allones { return T_ALLONES; }
186 allzeros { return T_ALLZEROS; }
187 none { return T_NONE; }
188 sindex { return T_SINDEX; }
191 /* Instruction Formatting */
192 PAD_PAGE { return T_PAD_PAGE; }
193 BEGIN_CRITICAL { return T_BEGIN_CS; }
194 END_CRITICAL { return T_END_CS; }
195 SET_SRC_MODE { return T_SET_SRC_MODE; }
196 SET_DST_MODE { return T_SET_DST_MODE; }
199 shl { return T_SHL; }
200 shr { return T_SHR; }
201 ror { return T_ROR; }
202 rol { return T_ROL; }
203 mvi { return T_MVI; }
204 mov { return T_MOV; }
205 clr { return T_CLR; }
206 jmp { return T_JMP; }
208 jnc { return T_JNC; }
210 jne { return T_JNE; }
212 jnz { return T_JNZ; }
213 call { return T_CALL; }
214 add { return T_ADD; }
215 adc { return T_ADC; }
216 bmov { return T_BMOV; }
217 inc { return T_INC; }
218 dec { return T_DEC; }
219 stc { return T_STC; }
220 clc { return T_CLC; }
221 cmp { return T_CMP; }
222 not { return T_NOT; }
223 xor { return T_XOR; }
224 test { return T_TEST;}
225 and { return T_AND; }
227 ret { return T_RET; }
228 nop { return T_NOP; }
230 /* ARP2 16bit extensions */
231 or16 { return T_OR16; }
232 and16 { return T_AND16; }
233 xor16 { return T_XOR16; }
234 add16 { return T_ADD16; }
235 adc16 { return T_ADC16; }
236 mvi16 { return T_MVI16; }
237 test16 { return T_TEST16; }
238 cmp16 { return T_CMP16; }
239 cmpxchg { return T_CMPXCHG; }
241 /* Allowed Symbols */
242 \<\< { return T_EXPR_LSHIFT; }
243 \>\> { return T_EXPR_RSHIFT; }
244 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
246 /* Number processing */
248 yylval.value = strtol(yytext, NULL, 8);
253 yylval.value = strtoul(yytext + 2, NULL, 16);
258 yylval.value = strtol(yytext, NULL, 10);
267 <INCLUDE>[<] { return yytext[0]; }
268 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
270 if (quote_count != 0)
279 string_buf_ptr = string_buf;
281 *string_buf_ptr++ = *yptr++;
282 yylval.str = string_buf;
283 *string_buf_ptr = '\0';
286 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
291 <MACRODEF>{WORD}{SPACE} {
294 /* Strip space and return as a normal symbol */
296 while (*yptr != ' ' && *yptr != '\t')
299 yylval.sym = symtable_get(yytext);
300 string_buf_ptr = string_buf;
306 * We store the symbol with its opening
307 * parren so we can differentiate macros
308 * that take args from macros with the
309 * same name that do not take args as
313 yylval.sym = symtable_get(yytext);
317 <MACROARGLIST>{WORD} {
321 <MACROARGLIST>{SPACE} ;
326 string_buf_ptr = string_buf;
331 snprintf(buf, sizeof(buf), "Invalid character "
332 "'%c' in macro argument list",
334 stop(buf, EX_DATAERR);
336 <MACROCALLARGS>{SPACE} ;
339 if (parren_count == 1)
341 *string_buf_ptr++ = '(';
345 if (parren_count == 0) {
349 *string_buf_ptr++ = ')';
351 <MACROCALLARGS>{MCARG} {
356 *string_buf_ptr++ = *yptr++;
359 if (string_buf_ptr != string_buf) {
361 * Return an argument and
362 * rescan this comma so we
363 * can return it as well.
365 *string_buf_ptr = '\0';
366 yylval.str = string_buf;
367 string_buf_ptr = string_buf;
374 /* Eat escaped newlines. */
379 /* Macros end on the first unescaped newline. */
381 *string_buf_ptr = '\0';
382 yylval.str = string_buf;
391 while (c = *yptr++) {
393 * Strip carriage returns.
397 *string_buf_ptr++ = c;
404 /* May be a symbol or a macro invocation. */
405 yylval.sym = symtable_get(yytext);
406 if (yylval.sym->type == MACRO) {
407 YY_BUFFER_STATE old_state;
408 YY_BUFFER_STATE temp_state;
410 ycopy = strdup(yytext);
411 yptr = ycopy + yyleng;
414 old_state = YY_CURRENT_BUFFER;
416 yy_create_buffer(stdin,
418 yy_switch_to_buffer(temp_state);
419 mm_switch_to_buffer(old_state);
421 mm_switch_to_buffer(temp_state);
422 yy_switch_to_buffer(old_state);
423 mm_delete_buffer(temp_state);
424 expand_macro(yylval.sym);
426 if (yylval.sym->type == UNINITIALIZED) {
427 /* Try without the '(' */
428 symbol_delete(yylval.sym);
429 yytext[yyleng-1] = '\0';
431 symtable_get(yytext);
438 yylval.sym = symtable_get(yytext);
439 if (yylval.sym->type == MACRO) {
440 expand_macro(yylval.sym);
446 snprintf(buf, sizeof(buf), "Invalid character "
448 stop(buf, EX_DATAERR);
452 typedef struct include {
453 YY_BUFFER_STATE buffer;
456 SLIST_ENTRY(include) links;
459 SLIST_HEAD(, include) include_stack;
462 include_file(char *file_name, include_type type)
468 /* Try the current directory first */
469 if (includes_search_curdir != 0 || type == SOURCE_FILE)
470 newfile = fopen(file_name, "r");
472 if (newfile == NULL && type != SOURCE_FILE) {
473 path_entry_t include_dir;
474 for (include_dir = search_path.slh_first;
476 include_dir = include_dir->links.sle_next) {
477 char fullname[PATH_MAX];
479 if ((include_dir->quoted_includes_only == TRUE)
480 && (type != QUOTED_INCLUDE))
483 snprintf(fullname, sizeof(fullname),
484 "%s/%s", include_dir->directory, file_name);
486 if ((newfile = fopen(fullname, "r")) != NULL)
491 if (newfile == NULL) {
493 stop("Unable to open input file", EX_SOFTWARE);
497 if (type != SOURCE_FILE) {
498 include = (include_t *)malloc(sizeof(include_t));
499 if (include == NULL) {
500 stop("Unable to allocate include stack entry",
504 include->buffer = YY_CURRENT_BUFFER;
505 include->lineno = yylineno;
506 include->filename = yyfilename;
507 SLIST_INSERT_HEAD(&include_stack, include, links);
509 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
511 yyfilename = strdup(file_name);
514 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
515 const char **next_match,
516 struct macro_arg **match_marg, regmatch_t *match);
519 expand_macro(struct symbol *macro_symbol)
521 struct macro_arg *marg;
522 struct macro_arg *match_marg;
523 const char *body_head;
524 const char *body_pos;
525 const char *next_match;
528 * Due to the nature of unput, we must work
529 * backwards through the macro body performing
532 body_head = macro_symbol->info.macroinfo->body;
533 body_pos = body_head + strlen(body_head);
534 while (body_pos > body_head) {
537 next_match = body_head;
539 next_substitution(macro_symbol, body_pos, &next_match,
540 &match_marg, &match);
542 /* Put back everything up until the replacement. */
543 while (body_pos > next_match)
546 /* Perform the replacement. */
547 if (match_marg != NULL) {
550 next_match = match_marg->replacement_text;
551 strp = next_match + strlen(next_match);
552 while (strp > next_match)
555 /* Skip past the unexpanded macro arg. */
556 body_pos -= match.rm_eo - match.rm_so;
560 /* Cleanup replacement text. */
561 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
562 free(marg->replacement_text);
567 * Find the next substitution in the macro working backwards from
568 * body_pos until the beginning of the macro buffer. next_match
569 * should be initialized to the beginning of the macro buffer prior
570 * to calling this routine.
573 next_substitution(struct symbol *mac_symbol, const char *body_pos,
574 const char **next_match, struct macro_arg **match_marg,
577 regmatch_t matches[2];
578 struct macro_arg *marg;
579 const char *search_pos;
583 search_pos = *next_match;
585 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
587 retval = regexec(&marg->arg_regex, search_pos, 2,
590 && (matches[1].rm_eo + search_pos) <= body_pos
591 && (matches[1].rm_eo + search_pos) > *next_match) {
593 *next_match = match->rm_eo + search_pos;
597 } while (search_pos != *next_match);
605 yy_delete_buffer(YY_CURRENT_BUFFER);
607 if (yyfilename != NULL)
610 include = include_stack.slh_first;
611 if (include != NULL) {
612 yy_switch_to_buffer(include->buffer);
613 yylineno = include->lineno;
614 yyfilename = include->filename;
615 SLIST_REMOVE_HEAD(&include_stack, links);