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 "aicasm_symbol.h"
58 #include "aicasm_gram.h"
60 /* This is used for macro body capture too, so err on the large size. */
61 #define MAX_STR_CONST 4096
62 static char string_buf[MAX_STR_CONST];
63 static char *string_buf_ptr;
64 static int parren_count;
65 static int quote_count;
69 PATH ([/]*[-A-Za-z0-9_.])+
70 WORD [A-Za-z_][-A-Za-z_0-9]*
73 MBODY ((\\[^\n])*[^\n\\]*)+
87 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
88 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
89 <COMMENT>\n { ++yylineno; }
91 <COMMENT>"*"+[^*/\n]* ;
92 <COMMENT>"/"+[^*/\n]* ;
93 <COMMENT>"*"+"/" { BEGIN INITIAL; }
95 string_buf_ptr = string_buf;
100 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
103 if (parren_count == 0) {
106 *string_buf_ptr = '\0';
107 yylval.sym = symtable_get(string_buf);
110 *string_buf_ptr++ = ')';
113 <CEXPR>\n { ++yylineno; }
119 while (*yptr != '\0') {
120 /* Remove duplicate spaces */
124 && string_buf_ptr != string_buf
125 && string_buf_ptr[-1] == ' ')
128 *string_buf_ptr++ = *yptr++;
131 else { return T_ELSE; }
132 VERSION { return T_VERSION; }
133 PREFIX { return T_PREFIX; }
134 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
136 string_buf_ptr = string_buf;
144 *string_buf_ptr++ = *yptr++;
149 *string_buf_ptr = '\0';
150 yylval.str = string_buf;
155 /* Register/SCB/SRAM definition keywords */
156 export { return T_EXPORT; }
157 register { return T_REGISTER; }
158 const { yylval.value = FALSE; return T_CONST; }
159 download { return T_DOWNLOAD; }
160 address { return T_ADDRESS; }
161 count { return T_COUNT; }
162 access_mode { return T_ACCESS_MODE; }
163 dont_generate_debug_code { return T_DONT_GENERATE_DEBUG_CODE; }
164 modes { return T_MODES; }
166 if (strcmp(yytext, "RW") == 0)
168 else if (strcmp(yytext, "RO") == 0)
174 field { return T_FIELD; }
175 enum { return T_ENUM; }
176 mask { return T_MASK; }
177 alias { return T_ALIAS; }
178 size { return T_SIZE; }
179 scb { return T_SCB; }
180 scratch_ram { return T_SRAM; }
181 accumulator { return T_ACCUM; }
182 mode_pointer { return T_MODE_PTR; }
183 allones { return T_ALLONES; }
184 allzeros { return T_ALLZEROS; }
185 none { return T_NONE; }
186 sindex { return T_SINDEX; }
189 /* Instruction Formatting */
190 PAD_PAGE { return T_PAD_PAGE; }
191 BEGIN_CRITICAL { return T_BEGIN_CS; }
192 END_CRITICAL { return T_END_CS; }
193 SET_SRC_MODE { return T_SET_SRC_MODE; }
194 SET_DST_MODE { return T_SET_DST_MODE; }
197 shl { return T_SHL; }
198 shr { return T_SHR; }
199 ror { return T_ROR; }
200 rol { return T_ROL; }
201 mvi { return T_MVI; }
202 mov { return T_MOV; }
203 clr { return T_CLR; }
204 jmp { return T_JMP; }
206 jnc { return T_JNC; }
208 jne { return T_JNE; }
210 jnz { return T_JNZ; }
211 call { return T_CALL; }
212 add { return T_ADD; }
213 adc { return T_ADC; }
214 bmov { return T_BMOV; }
215 inc { return T_INC; }
216 dec { return T_DEC; }
217 stc { return T_STC; }
218 clc { return T_CLC; }
219 cmp { return T_CMP; }
220 not { return T_NOT; }
221 xor { return T_XOR; }
222 test { return T_TEST;}
223 and { return T_AND; }
225 ret { return T_RET; }
226 nop { return T_NOP; }
228 /* ARP2 16bit extensions */
229 /* or16 { return T_OR16; } */
230 /* and16 { return T_AND16; }*/
231 /* xor16 { return T_XOR16; }*/
232 /* add16 { return T_ADD16; }*/
233 /* adc16 { return T_ADC16; }*/
234 /* mvi16 { return T_MVI16; }*/
235 /* test16 { return T_TEST16; }*/
236 /* cmp16 { return T_CMP16; }*/
237 /* cmpxchg { return T_CMPXCHG; }*/
239 /* Allowed Symbols */
240 \<\< { return T_EXPR_LSHIFT; }
241 \>\> { return T_EXPR_RSHIFT; }
242 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
244 /* Number processing */
246 yylval.value = strtol(yytext, NULL, 8);
251 yylval.value = strtoul(yytext + 2, NULL, 16);
256 yylval.value = strtol(yytext, NULL, 10);
265 <INCLUDE>[<] { return yytext[0]; }
266 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
268 if (quote_count != 0)
277 string_buf_ptr = string_buf;
279 *string_buf_ptr++ = *yptr++;
280 yylval.str = string_buf;
281 *string_buf_ptr = '\0';
284 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
289 <MACRODEF>{WORD}{SPACE} {
292 /* Strip space and return as a normal symbol */
294 while (*yptr != ' ' && *yptr != '\t')
297 yylval.sym = symtable_get(yytext);
298 string_buf_ptr = string_buf;
304 * We store the symbol with its opening
305 * parren so we can differentiate macros
306 * that take args from macros with the
307 * same name that do not take args as
311 yylval.sym = symtable_get(yytext);
315 <MACROARGLIST>{WORD} {
319 <MACROARGLIST>{SPACE} ;
324 string_buf_ptr = string_buf;
329 snprintf(buf, sizeof(buf), "Invalid character "
330 "'%c' in macro argument list",
332 stop(buf, EX_DATAERR);
334 <MACROCALLARGS>{SPACE} ;
337 if (parren_count == 1)
339 *string_buf_ptr++ = '(';
343 if (parren_count == 0) {
347 *string_buf_ptr++ = ')';
349 <MACROCALLARGS>{MCARG} {
354 *string_buf_ptr++ = *yptr++;
357 if (string_buf_ptr != string_buf) {
359 * Return an argument and
360 * rescan this comma so we
361 * can return it as well.
363 *string_buf_ptr = '\0';
364 yylval.str = string_buf;
365 string_buf_ptr = string_buf;
372 /* Eat escaped newlines. */
377 /* Macros end on the first unescaped newline. */
379 *string_buf_ptr = '\0';
380 yylval.str = string_buf;
389 while (c = *yptr++) {
391 * Strip carriage returns.
395 *string_buf_ptr++ = c;
402 /* May be a symbol or a macro invocation. */
403 yylval.sym = symtable_get(yytext);
404 if (yylval.sym->type == MACRO) {
405 YY_BUFFER_STATE old_state;
406 YY_BUFFER_STATE temp_state;
408 ycopy = strdup(yytext);
409 yptr = ycopy + yyleng;
412 old_state = YY_CURRENT_BUFFER;
414 yy_create_buffer(stdin,
416 yy_switch_to_buffer(temp_state);
417 mm_switch_to_buffer(old_state);
419 mm_switch_to_buffer(temp_state);
420 yy_switch_to_buffer(old_state);
421 mm_delete_buffer(temp_state);
422 expand_macro(yylval.sym);
424 if (yylval.sym->type == UNINITIALIZED) {
425 /* Try without the '(' */
426 symbol_delete(yylval.sym);
427 yytext[yyleng-1] = '\0';
429 symtable_get(yytext);
436 yylval.sym = symtable_get(yytext);
437 if (yylval.sym->type == MACRO) {
438 expand_macro(yylval.sym);
444 snprintf(buf, sizeof(buf), "Invalid character "
446 stop(buf, EX_DATAERR);
450 typedef struct include {
451 YY_BUFFER_STATE buffer;
454 SLIST_ENTRY(include) links;
457 SLIST_HEAD(, include) include_stack;
460 include_file(char *file_name, include_type type)
466 /* Try the current directory first */
467 if (includes_search_curdir != 0 || type == SOURCE_FILE)
468 newfile = fopen(file_name, "r");
470 if (newfile == NULL && type != SOURCE_FILE) {
471 path_entry_t include_dir;
472 for (include_dir = search_path.slh_first;
474 include_dir = include_dir->links.sle_next) {
475 char fullname[PATH_MAX];
477 if ((include_dir->quoted_includes_only == TRUE)
478 && (type != QUOTED_INCLUDE))
481 snprintf(fullname, sizeof(fullname),
482 "%s/%s", include_dir->directory, file_name);
484 if ((newfile = fopen(fullname, "r")) != NULL)
489 if (newfile == NULL) {
491 stop("Unable to open input file", EX_SOFTWARE);
495 if (type != SOURCE_FILE) {
496 include = (include_t *)malloc(sizeof(include_t));
497 if (include == NULL) {
498 stop("Unable to allocate include stack entry",
502 include->buffer = YY_CURRENT_BUFFER;
503 include->lineno = yylineno;
504 include->filename = yyfilename;
505 SLIST_INSERT_HEAD(&include_stack, include, links);
507 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
509 yyfilename = strdup(file_name);
512 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
513 const char **next_match,
514 struct macro_arg **match_marg, regmatch_t *match);
517 expand_macro(struct symbol *macro_symbol)
519 struct macro_arg *marg;
520 struct macro_arg *match_marg;
521 const char *body_head;
522 const char *body_pos;
523 const char *next_match;
526 * Due to the nature of unput, we must work
527 * backwards through the macro body performing
530 body_head = macro_symbol->info.macroinfo->body;
531 body_pos = body_head + strlen(body_head);
532 while (body_pos > body_head) {
535 next_match = body_head;
537 next_substitution(macro_symbol, body_pos, &next_match,
538 &match_marg, &match);
540 /* Put back everything up until the replacement. */
541 while (body_pos > next_match)
544 /* Perform the replacement. */
545 if (match_marg != NULL) {
548 next_match = match_marg->replacement_text;
549 strp = next_match + strlen(next_match);
550 while (strp > next_match)
553 /* Skip past the unexpanded macro arg. */
554 body_pos -= match.rm_eo - match.rm_so;
558 /* Cleanup replacement text. */
559 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
560 free(marg->replacement_text);
565 * Find the next substitution in the macro working backwards from
566 * body_pos until the beginning of the macro buffer. next_match
567 * should be initialized to the beginning of the macro buffer prior
568 * to calling this routine.
571 next_substitution(struct symbol *mac_symbol, const char *body_pos,
572 const char **next_match, struct macro_arg **match_marg,
575 regmatch_t matches[2];
576 struct macro_arg *marg;
577 const char *search_pos;
581 search_pos = *next_match;
583 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
585 retval = regexec(&marg->arg_regex, search_pos, 2,
588 && (matches[1].rm_eo + search_pos) <= body_pos
589 && (matches[1].rm_eo + search_pos) > *next_match) {
591 *next_match = match->rm_eo + search_pos;
595 } while (search_pos != *next_match);
603 yy_delete_buffer(YY_CURRENT_BUFFER);
605 if (yyfilename != NULL)
608 include = include_stack.slh_first;
609 if (include != NULL) {
610 yy_switch_to_buffer(include->buffer);
611 yylineno = include->lineno;
612 yyfilename = include->filename;
613 SLIST_REMOVE_HEAD(&include_stack, links);