3 * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
8 * Copyright (c) 2001, 2002 Adaptec Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
43 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_scan.l#19 $
48 #include <sys/types.h>
56 #include <sys/queue.h>
59 #include "aicasm_symbol.h"
60 #include "aicasm_gram.h"
62 /* This is used for macro body capture too, so err on the large size. */
63 #define MAX_STR_CONST 4096
64 static char string_buf[MAX_STR_CONST];
65 static char *string_buf_ptr;
66 static int parren_count;
67 static int quote_count;
68 static char msgbuf[255];
70 extern int yylex(void);
71 extern int mmlex(void);
72 extern int mmparse(void);
73 extern void mm_switch_to_buffer(YY_BUFFER_STATE);
74 extern void mm_delete_buffer(YY_BUFFER_STATE);
79 PATH ([/]*[-A-Za-z0-9_.])+
80 WORD [A-Za-z_][-A-Za-z_0-9]*
83 MBODY ((\\[^\n])*[^\n\\]*)+
97 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
98 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
99 <COMMENT>\n { ++yylineno; }
101 <COMMENT>"*"+[^*/\n]* ;
102 <COMMENT>"/"+[^*/\n]* ;
103 <COMMENT>"*"+"/" { BEGIN INITIAL; }
105 string_buf_ptr = string_buf;
110 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
113 if (parren_count == 0) {
116 *string_buf_ptr = '\0';
117 yylval.sym = symtable_get(string_buf);
120 *string_buf_ptr++ = ')';
123 <CEXPR>\n { ++yylineno; }
129 while (*yptr != '\0') {
130 /* Remove duplicate spaces */
134 && string_buf_ptr != string_buf
135 && string_buf_ptr[-1] == ' ')
138 *string_buf_ptr++ = *yptr++;
142 VERSION { return T_VERSION; }
143 PREFIX { return T_PREFIX; }
144 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
146 string_buf_ptr = string_buf;
154 *string_buf_ptr++ = *yptr++;
159 *string_buf_ptr = '\0';
160 yylval.str = string_buf;
165 /* Register/SCB/SRAM definition keywords */
166 export { return T_EXPORT; }
167 register { return T_REGISTER; }
168 const { yylval.value = FALSE; return T_CONST; }
169 download { return T_DOWNLOAD; }
170 address { return T_ADDRESS; }
171 access_mode { return T_ACCESS_MODE; }
172 modes { return T_MODES; }
174 if (strcmp(yytext, "RW") == 0)
176 else if (strcmp(yytext, "RO") == 0)
182 BEGIN_CRITICAL { return T_BEGIN_CS; }
183 END_CRITICAL { return T_END_CS; }
184 SET_SRC_MODE { return T_SET_SRC_MODE; }
185 SET_DST_MODE { return T_SET_DST_MODE; }
186 field { return T_FIELD; }
187 enum { return T_ENUM; }
188 mask { return T_MASK; }
189 alias { return T_ALIAS; }
190 size { return T_SIZE; }
191 scb { return T_SCB; }
192 scratch_ram { return T_SRAM; }
193 accumulator { return T_ACCUM; }
194 mode_pointer { return T_MODE_PTR; }
195 allones { return T_ALLONES; }
196 allzeros { return T_ALLZEROS; }
197 none { return T_NONE; }
198 sindex { return T_SINDEX; }
202 shl { return T_SHL; }
203 shr { return T_SHR; }
204 ror { return T_ROR; }
205 rol { return T_ROL; }
206 mvi { return T_MVI; }
207 mov { return T_MOV; }
208 clr { return T_CLR; }
209 jmp { return T_JMP; }
211 jnc { return T_JNC; }
213 jne { return T_JNE; }
215 jnz { return T_JNZ; }
216 call { return T_CALL; }
217 add { return T_ADD; }
218 adc { return T_ADC; }
219 bmov { return T_BMOV; }
220 inc { return T_INC; }
221 dec { return T_DEC; }
222 stc { return T_STC; }
223 clc { return T_CLC; }
224 cmp { return T_CMP; }
225 not { return T_NOT; }
226 xor { return T_XOR; }
227 test { return T_TEST;}
228 and { return T_AND; }
230 ret { return T_RET; }
231 nop { return T_NOP; }
232 else { return T_ELSE; }
234 /* Allowed Symbols */
235 \<\< { return T_EXPR_LSHIFT; }
236 \>\> { return T_EXPR_RSHIFT; }
237 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
239 /* Number processing */
241 yylval.value = strtol(yytext, NULL, 8);
246 yylval.value = strtoul(yytext + 2, NULL, 16);
251 yylval.value = strtol(yytext, NULL, 10);
260 <INCLUDE>[<] { return yytext[0]; }
261 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
263 if (quote_count != 0)
272 string_buf_ptr = string_buf;
274 *string_buf_ptr++ = *yptr++;
275 yylval.str = string_buf;
276 *string_buf_ptr = '\0';
279 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
284 <MACRODEF>{WORD}{SPACE} {
287 /* Strip space and return as a normal symbol */
289 while (*yptr != ' ' && *yptr != '\t')
292 yylval.sym = symtable_get(yytext);
293 string_buf_ptr = string_buf;
299 * We store the symbol with its opening
300 * parren so we can differentiate macros
301 * that take args from macros with the
302 * same name that do not take args as
306 yylval.sym = symtable_get(yytext);
310 <MACROARGLIST>{WORD} {
314 <MACROARGLIST>{SPACE} ;
319 string_buf_ptr = string_buf;
324 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
325 "'%c' in macro argument list",
327 stop(msgbuf, EX_DATAERR);
329 <MACROCALLARGS>{SPACE} ;
332 if (parren_count == 1)
334 *string_buf_ptr++ = '(';
338 if (parren_count == 0) {
342 *string_buf_ptr++ = ')';
344 <MACROCALLARGS>{MCARG} {
349 *string_buf_ptr++ = *yptr++;
352 if (string_buf_ptr != string_buf) {
354 * Return an argument and
355 * rescan this comma so we
356 * can return it as well.
358 *string_buf_ptr = '\0';
359 yylval.str = string_buf;
360 string_buf_ptr = string_buf;
367 /* Eat escaped newlines. */
372 /* Macros end on the first unescaped newline. */
374 *string_buf_ptr = '\0';
375 yylval.str = string_buf;
384 while ((c = *yptr++)) {
386 * Strip carriage returns.
390 *string_buf_ptr++ = c;
397 /* May be a symbol or a macro invocation. */
398 yylval.sym = symtable_get(yytext);
399 if (yylval.sym->type == MACRO) {
400 YY_BUFFER_STATE old_state;
401 YY_BUFFER_STATE temp_state;
403 ycopy = strdup(yytext);
404 yptr = ycopy + yyleng;
407 old_state = YY_CURRENT_BUFFER;
409 yy_create_buffer(stdin,
411 yy_switch_to_buffer(temp_state);
412 mm_switch_to_buffer(old_state);
414 mm_switch_to_buffer(temp_state);
415 yy_switch_to_buffer(old_state);
416 mm_delete_buffer(temp_state);
417 expand_macro(yylval.sym);
419 if (yylval.sym->type == UNINITIALIZED) {
420 /* Try without the '(' */
421 symbol_delete(yylval.sym);
422 yytext[yyleng-1] = '\0';
424 symtable_get(yytext);
431 yylval.sym = symtable_get(yytext);
432 if (yylval.sym->type == MACRO) {
433 expand_macro(yylval.sym);
439 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
441 stop(msgbuf, EX_DATAERR);
445 typedef struct include {
446 YY_BUFFER_STATE buffer;
449 SLIST_ENTRY(include) links;
452 SLIST_HEAD(, include) include_stack;
455 include_file(char *file_name, include_type type)
461 /* Try the current directory first */
462 if (includes_search_curdir != 0 || type == SOURCE_FILE)
463 newfile = fopen(file_name, "r");
465 if (newfile == NULL && type != SOURCE_FILE) {
466 path_entry_t include_dir;
467 for (include_dir = search_path.slh_first;
469 include_dir = include_dir->links.sle_next) {
470 char fullname[PATH_MAX];
472 if ((include_dir->quoted_includes_only == TRUE)
473 && (type != QUOTED_INCLUDE))
476 snprintf(fullname, sizeof(fullname),
477 "%s/%s", include_dir->directory, file_name);
479 if ((newfile = fopen(fullname, "r")) != NULL)
484 if (newfile == NULL) {
486 stop("Unable to open input file", EX_SOFTWARE);
490 if (type != SOURCE_FILE) {
491 include = (include_t *)malloc(sizeof(include_t));
492 if (include == NULL) {
493 stop("Unable to allocate include stack entry",
497 include->buffer = YY_CURRENT_BUFFER;
498 include->lineno = yylineno;
499 include->filename = yyfilename;
500 SLIST_INSERT_HEAD(&include_stack, include, links);
502 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
504 yyfilename = strdup(file_name);
507 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
508 const char **next_match,
509 struct macro_arg **match_marg, regmatch_t *match);
512 expand_macro(struct symbol *macro_symbol)
514 struct macro_arg *marg;
515 struct macro_arg *match_marg;
516 const char *body_head;
517 const char *body_pos;
518 const char *next_match;
519 regmatch_t match = { .rm_so = 0, .rm_eo = 0 };
522 * Due to the nature of unput, we must work
523 * backwards through the macro body performing
526 body_head = macro_symbol->info.macroinfo->body;
527 body_pos = body_head + strlen(body_head);
528 while (body_pos > body_head) {
529 next_match = body_head;
531 next_substitution(macro_symbol, body_pos, &next_match,
532 &match_marg, &match);
534 /* Put back everything up until the replacement. */
535 while (body_pos > next_match)
538 /* Perform the replacement. */
539 if (match_marg != NULL) {
542 next_match = match_marg->replacement_text;
543 strp = next_match + strlen(next_match);
544 while (strp > next_match)
547 /* Skip past the unexpanded macro arg. */
548 body_pos -= match.rm_eo - match.rm_so;
552 /* Cleanup replacement text. */
553 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
554 free(marg->replacement_text);
559 * Find the next substitution in the macro working backwards from
560 * body_pos until the beginning of the macro buffer. next_match
561 * should be initialized to the beginning of the macro buffer prior
562 * to calling this routine.
565 next_substitution(struct symbol *mac_symbol, const char *body_pos,
566 const char **next_match, struct macro_arg **match_marg,
569 regmatch_t matches[2];
570 struct macro_arg *marg;
571 const char *search_pos;
575 search_pos = *next_match;
577 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
579 retval = regexec(&marg->arg_regex, search_pos, 2,
582 && (matches[1].rm_eo + search_pos) <= body_pos
583 && (matches[1].rm_eo + search_pos) > *next_match) {
585 *next_match = match->rm_eo + search_pos;
589 } while (search_pos != *next_match);
597 yy_delete_buffer(YY_CURRENT_BUFFER);
599 if (yyfilename != NULL)
602 include = include_stack.slh_first;
603 if (include != NULL) {
604 yy_switch_to_buffer(include->buffer);
605 yylineno = include->lineno;
606 yyfilename = include->filename;
607 SLIST_REMOVE_HEAD(&include_stack, links);