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#19 $
46 #include <sys/types.h>
54 #include <sys/queue.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;
66 static char msgbuf[255];
68 extern int yylex(void);
69 extern int mmlex(void);
70 extern int mmparse(void);
71 extern void mm_switch_to_buffer(YY_BUFFER_STATE);
72 extern void mm_delete_buffer(YY_BUFFER_STATE);
75 PATH ([/]*[-A-Za-z0-9_.])+
76 WORD [A-Za-z_][-A-Za-z_0-9]*
79 MBODY ((\\[^\n])*[^\n\\]*)+
93 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
94 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
95 <COMMENT>\n { ++yylineno; }
97 <COMMENT>"*"+[^*/\n]* ;
98 <COMMENT>"/"+[^*/\n]* ;
99 <COMMENT>"*"+"/" { BEGIN INITIAL; }
101 string_buf_ptr = string_buf;
106 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
109 if (parren_count == 0) {
112 *string_buf_ptr = '\0';
113 yylval.sym = symtable_get(string_buf);
116 *string_buf_ptr++ = ')';
119 <CEXPR>\n { ++yylineno; }
125 while (*yptr != '\0') {
126 /* Remove duplicate spaces */
130 && string_buf_ptr != string_buf
131 && string_buf_ptr[-1] == ' ')
134 *string_buf_ptr++ = *yptr++;
138 VERSION { return T_VERSION; }
139 PREFIX { return T_PREFIX; }
140 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
142 string_buf_ptr = string_buf;
150 *string_buf_ptr++ = *yptr++;
155 *string_buf_ptr = '\0';
156 yylval.str = string_buf;
161 /* Register/SCB/SRAM definition keywords */
162 export { return T_EXPORT; }
163 register { return T_REGISTER; }
164 const { yylval.value = FALSE; return T_CONST; }
165 download { return T_DOWNLOAD; }
166 address { return T_ADDRESS; }
167 access_mode { return T_ACCESS_MODE; }
168 modes { return T_MODES; }
170 if (strcmp(yytext, "RW") == 0)
172 else if (strcmp(yytext, "RO") == 0)
178 BEGIN_CRITICAL { return T_BEGIN_CS; }
179 END_CRITICAL { return T_END_CS; }
180 SET_SRC_MODE { return T_SET_SRC_MODE; }
181 SET_DST_MODE { return T_SET_DST_MODE; }
182 field { return T_FIELD; }
183 enum { return T_ENUM; }
184 mask { return T_MASK; }
185 alias { return T_ALIAS; }
186 size { return T_SIZE; }
187 scb { return T_SCB; }
188 scratch_ram { return T_SRAM; }
189 accumulator { return T_ACCUM; }
190 mode_pointer { return T_MODE_PTR; }
191 allones { return T_ALLONES; }
192 allzeros { return T_ALLZEROS; }
193 none { return T_NONE; }
194 sindex { return T_SINDEX; }
198 shl { return T_SHL; }
199 shr { return T_SHR; }
200 ror { return T_ROR; }
201 rol { return T_ROL; }
202 mvi { return T_MVI; }
203 mov { return T_MOV; }
204 clr { return T_CLR; }
205 jmp { return T_JMP; }
207 jnc { return T_JNC; }
209 jne { return T_JNE; }
211 jnz { return T_JNZ; }
212 call { return T_CALL; }
213 add { return T_ADD; }
214 adc { return T_ADC; }
215 bmov { return T_BMOV; }
216 inc { return T_INC; }
217 dec { return T_DEC; }
218 stc { return T_STC; }
219 clc { return T_CLC; }
220 cmp { return T_CMP; }
221 not { return T_NOT; }
222 xor { return T_XOR; }
223 test { return T_TEST;}
224 and { return T_AND; }
226 ret { return T_RET; }
227 nop { return T_NOP; }
228 else { return T_ELSE; }
230 /* Allowed Symbols */
231 \<\< { return T_EXPR_LSHIFT; }
232 \>\> { return T_EXPR_RSHIFT; }
233 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
235 /* Number processing */
237 yylval.value = strtol(yytext, NULL, 8);
242 yylval.value = strtoul(yytext + 2, NULL, 16);
247 yylval.value = strtol(yytext, NULL, 10);
256 <INCLUDE>[<] { return yytext[0]; }
257 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
259 if (quote_count != 0)
268 string_buf_ptr = string_buf;
270 *string_buf_ptr++ = *yptr++;
271 yylval.str = string_buf;
272 *string_buf_ptr = '\0';
275 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
280 <MACRODEF>{WORD}{SPACE} {
283 /* Strip space and return as a normal symbol */
285 while (*yptr != ' ' && *yptr != '\t')
288 yylval.sym = symtable_get(yytext);
289 string_buf_ptr = string_buf;
295 * We store the symbol with its opening
296 * parren so we can differentiate macros
297 * that take args from macros with the
298 * same name that do not take args as
302 yylval.sym = symtable_get(yytext);
306 <MACROARGLIST>{WORD} {
310 <MACROARGLIST>{SPACE} ;
315 string_buf_ptr = string_buf;
320 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
321 "'%c' in macro argument list",
323 stop(msgbuf, EX_DATAERR);
325 <MACROCALLARGS>{SPACE} ;
328 if (parren_count == 1)
330 *string_buf_ptr++ = '(';
334 if (parren_count == 0) {
338 *string_buf_ptr++ = ')';
340 <MACROCALLARGS>{MCARG} {
345 *string_buf_ptr++ = *yptr++;
348 if (string_buf_ptr != string_buf) {
350 * Return an argument and
351 * rescan this comma so we
352 * can return it as well.
354 *string_buf_ptr = '\0';
355 yylval.str = string_buf;
356 string_buf_ptr = string_buf;
363 /* Eat escaped newlines. */
368 /* Macros end on the first unescaped newline. */
370 *string_buf_ptr = '\0';
371 yylval.str = string_buf;
380 while ((c = *yptr++)) {
382 * Strip carriage returns.
386 *string_buf_ptr++ = c;
393 /* May be a symbol or a macro invocation. */
394 yylval.sym = symtable_get(yytext);
395 if (yylval.sym->type == MACRO) {
396 YY_BUFFER_STATE old_state;
397 YY_BUFFER_STATE temp_state;
399 ycopy = strdup(yytext);
400 yptr = ycopy + yyleng;
403 old_state = YY_CURRENT_BUFFER;
405 yy_create_buffer(stdin,
407 yy_switch_to_buffer(temp_state);
408 mm_switch_to_buffer(old_state);
410 mm_switch_to_buffer(temp_state);
411 yy_switch_to_buffer(old_state);
412 mm_delete_buffer(temp_state);
413 expand_macro(yylval.sym);
415 if (yylval.sym->type == UNINITIALIZED) {
416 /* Try without the '(' */
417 symbol_delete(yylval.sym);
418 yytext[yyleng-1] = '\0';
420 symtable_get(yytext);
427 yylval.sym = symtable_get(yytext);
428 if (yylval.sym->type == MACRO) {
429 expand_macro(yylval.sym);
435 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
437 stop(msgbuf, EX_DATAERR);
441 typedef struct include {
442 YY_BUFFER_STATE buffer;
445 SLIST_ENTRY(include) links;
448 SLIST_HEAD(, include) include_stack;
451 include_file(char *file_name, include_type type)
457 /* Try the current directory first */
458 if (includes_search_curdir != 0 || type == SOURCE_FILE)
459 newfile = fopen(file_name, "r");
461 if (newfile == NULL && type != SOURCE_FILE) {
462 path_entry_t include_dir;
463 for (include_dir = search_path.slh_first;
465 include_dir = include_dir->links.sle_next) {
466 char fullname[PATH_MAX];
468 if ((include_dir->quoted_includes_only == TRUE)
469 && (type != QUOTED_INCLUDE))
472 snprintf(fullname, sizeof(fullname),
473 "%s/%s", include_dir->directory, file_name);
475 if ((newfile = fopen(fullname, "r")) != NULL)
480 if (newfile == NULL) {
482 stop("Unable to open input file", EX_SOFTWARE);
486 if (type != SOURCE_FILE) {
487 include = (include_t *)malloc(sizeof(include_t));
488 if (include == NULL) {
489 stop("Unable to allocate include stack entry",
493 include->buffer = YY_CURRENT_BUFFER;
494 include->lineno = yylineno;
495 include->filename = yyfilename;
496 SLIST_INSERT_HEAD(&include_stack, include, links);
498 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
500 yyfilename = strdup(file_name);
503 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
504 const char **next_match,
505 struct macro_arg **match_marg, regmatch_t *match);
508 expand_macro(struct symbol *macro_symbol)
510 struct macro_arg *marg;
511 struct macro_arg *match_marg;
512 const char *body_head;
513 const char *body_pos;
514 const char *next_match;
515 regmatch_t match = { .rm_so = 0, .rm_eo = 0 };
518 * Due to the nature of unput, we must work
519 * backwards through the macro body performing
522 body_head = macro_symbol->info.macroinfo->body;
523 body_pos = body_head + strlen(body_head);
524 while (body_pos > body_head) {
525 next_match = body_head;
527 next_substitution(macro_symbol, body_pos, &next_match,
528 &match_marg, &match);
530 /* Put back everything up until the replacement. */
531 while (body_pos > next_match)
534 /* Perform the replacement. */
535 if (match_marg != NULL) {
538 next_match = match_marg->replacement_text;
539 strp = next_match + strlen(next_match);
540 while (strp > next_match)
543 /* Skip past the unexpanded macro arg. */
544 body_pos -= match.rm_eo - match.rm_so;
548 /* Cleanup replacement text. */
549 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
550 free(marg->replacement_text);
555 * Find the next substitution in the macro working backwards from
556 * body_pos until the beginning of the macro buffer. next_match
557 * should be initialized to the beginning of the macro buffer prior
558 * to calling this routine.
561 next_substitution(struct symbol *mac_symbol, const char *body_pos,
562 const char **next_match, struct macro_arg **match_marg,
565 regmatch_t matches[2];
566 struct macro_arg *marg;
567 const char *search_pos;
571 search_pos = *next_match;
573 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
575 retval = regexec(&marg->arg_regex, search_pos, 2,
578 && (matches[1].rm_eo + search_pos) <= body_pos
579 && (matches[1].rm_eo + search_pos) > *next_match) {
581 *next_match = match->rm_eo + search_pos;
585 } while (search_pos != *next_match);
593 yy_delete_buffer(YY_CURRENT_BUFFER);
595 if (yyfilename != NULL)
598 include = include_stack.slh_first;
599 if (include != NULL) {
600 yy_switch_to_buffer(include->buffer);
601 yylineno = include->lineno;
602 yyfilename = include->filename;
603 SLIST_REMOVE_HEAD(&include_stack, links);