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);
77 PATH ([/]*[-A-Za-z0-9_.])+
78 WORD [A-Za-z_][-A-Za-z_0-9]*
81 MBODY ((\\[^\n])*[^\n\\]*)+
95 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
96 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
97 <COMMENT>\n { ++yylineno; }
99 <COMMENT>"*"+[^*/\n]* ;
100 <COMMENT>"/"+[^*/\n]* ;
101 <COMMENT>"*"+"/" { BEGIN INITIAL; }
103 string_buf_ptr = string_buf;
108 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
111 if (parren_count == 0) {
114 *string_buf_ptr = '\0';
115 yylval.sym = symtable_get(string_buf);
118 *string_buf_ptr++ = ')';
121 <CEXPR>\n { ++yylineno; }
127 while (*yptr != '\0') {
128 /* Remove duplicate spaces */
132 && string_buf_ptr != string_buf
133 && string_buf_ptr[-1] == ' ')
136 *string_buf_ptr++ = *yptr++;
140 VERSION { return T_VERSION; }
141 PREFIX { return T_PREFIX; }
142 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
144 string_buf_ptr = string_buf;
152 *string_buf_ptr++ = *yptr++;
157 *string_buf_ptr = '\0';
158 yylval.str = string_buf;
163 /* Register/SCB/SRAM definition keywords */
164 export { return T_EXPORT; }
165 register { return T_REGISTER; }
166 const { yylval.value = FALSE; return T_CONST; }
167 download { return T_DOWNLOAD; }
168 address { return T_ADDRESS; }
169 access_mode { return T_ACCESS_MODE; }
170 modes { return T_MODES; }
172 if (strcmp(yytext, "RW") == 0)
174 else if (strcmp(yytext, "RO") == 0)
180 BEGIN_CRITICAL { return T_BEGIN_CS; }
181 END_CRITICAL { return T_END_CS; }
182 SET_SRC_MODE { return T_SET_SRC_MODE; }
183 SET_DST_MODE { return T_SET_DST_MODE; }
184 field { return T_FIELD; }
185 enum { return T_ENUM; }
186 mask { return T_MASK; }
187 alias { return T_ALIAS; }
188 size { return T_SIZE; }
189 scb { return T_SCB; }
190 scratch_ram { return T_SRAM; }
191 accumulator { return T_ACCUM; }
192 mode_pointer { return T_MODE_PTR; }
193 allones { return T_ALLONES; }
194 allzeros { return T_ALLZEROS; }
195 none { return T_NONE; }
196 sindex { return T_SINDEX; }
200 shl { return T_SHL; }
201 shr { return T_SHR; }
202 ror { return T_ROR; }
203 rol { return T_ROL; }
204 mvi { return T_MVI; }
205 mov { return T_MOV; }
206 clr { return T_CLR; }
207 jmp { return T_JMP; }
209 jnc { return T_JNC; }
211 jne { return T_JNE; }
213 jnz { return T_JNZ; }
214 call { return T_CALL; }
215 add { return T_ADD; }
216 adc { return T_ADC; }
217 bmov { return T_BMOV; }
218 inc { return T_INC; }
219 dec { return T_DEC; }
220 stc { return T_STC; }
221 clc { return T_CLC; }
222 cmp { return T_CMP; }
223 not { return T_NOT; }
224 xor { return T_XOR; }
225 test { return T_TEST;}
226 and { return T_AND; }
228 ret { return T_RET; }
229 nop { return T_NOP; }
230 else { return T_ELSE; }
232 /* Allowed Symbols */
233 \<\< { return T_EXPR_LSHIFT; }
234 \>\> { return T_EXPR_RSHIFT; }
235 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
237 /* Number processing */
239 yylval.value = strtol(yytext, NULL, 8);
244 yylval.value = strtoul(yytext + 2, NULL, 16);
249 yylval.value = strtol(yytext, NULL, 10);
258 <INCLUDE>[<] { return yytext[0]; }
259 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
261 if (quote_count != 0)
270 string_buf_ptr = string_buf;
272 *string_buf_ptr++ = *yptr++;
273 yylval.str = string_buf;
274 *string_buf_ptr = '\0';
277 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
282 <MACRODEF>{WORD}{SPACE} {
285 /* Strip space and return as a normal symbol */
287 while (*yptr != ' ' && *yptr != '\t')
290 yylval.sym = symtable_get(yytext);
291 string_buf_ptr = string_buf;
297 * We store the symbol with its opening
298 * parren so we can differentiate macros
299 * that take args from macros with the
300 * same name that do not take args as
304 yylval.sym = symtable_get(yytext);
308 <MACROARGLIST>{WORD} {
312 <MACROARGLIST>{SPACE} ;
317 string_buf_ptr = string_buf;
322 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
323 "'%c' in macro argument list",
325 stop(msgbuf, EX_DATAERR);
327 <MACROCALLARGS>{SPACE} ;
330 if (parren_count == 1)
332 *string_buf_ptr++ = '(';
336 if (parren_count == 0) {
340 *string_buf_ptr++ = ')';
342 <MACROCALLARGS>{MCARG} {
347 *string_buf_ptr++ = *yptr++;
350 if (string_buf_ptr != string_buf) {
352 * Return an argument and
353 * rescan this comma so we
354 * can return it as well.
356 *string_buf_ptr = '\0';
357 yylval.str = string_buf;
358 string_buf_ptr = string_buf;
365 /* Eat escaped newlines. */
370 /* Macros end on the first unescaped newline. */
372 *string_buf_ptr = '\0';
373 yylval.str = string_buf;
382 while ((c = *yptr++)) {
384 * Strip carriage returns.
388 *string_buf_ptr++ = c;
395 /* May be a symbol or a macro invocation. */
396 yylval.sym = symtable_get(yytext);
397 if (yylval.sym->type == MACRO) {
398 YY_BUFFER_STATE old_state;
399 YY_BUFFER_STATE temp_state;
401 ycopy = strdup(yytext);
402 yptr = ycopy + yyleng;
405 old_state = YY_CURRENT_BUFFER;
407 yy_create_buffer(stdin,
409 yy_switch_to_buffer(temp_state);
410 mm_switch_to_buffer(old_state);
412 mm_switch_to_buffer(temp_state);
413 yy_switch_to_buffer(old_state);
414 mm_delete_buffer(temp_state);
415 expand_macro(yylval.sym);
417 if (yylval.sym->type == UNINITIALIZED) {
418 /* Try without the '(' */
419 symbol_delete(yylval.sym);
420 yytext[yyleng-1] = '\0';
422 symtable_get(yytext);
429 yylval.sym = symtable_get(yytext);
430 if (yylval.sym->type == MACRO) {
431 expand_macro(yylval.sym);
437 snprintf(msgbuf, sizeof(msgbuf), "Invalid character "
439 stop(msgbuf, EX_DATAERR);
443 typedef struct include {
444 YY_BUFFER_STATE buffer;
447 SLIST_ENTRY(include) links;
450 SLIST_HEAD(, include) include_stack;
453 include_file(char *file_name, include_type type)
459 /* Try the current directory first */
460 if (includes_search_curdir != 0 || type == SOURCE_FILE)
461 newfile = fopen(file_name, "r");
463 if (newfile == NULL && type != SOURCE_FILE) {
464 path_entry_t include_dir;
465 for (include_dir = search_path.slh_first;
467 include_dir = include_dir->links.sle_next) {
468 char fullname[PATH_MAX];
470 if ((include_dir->quoted_includes_only == TRUE)
471 && (type != QUOTED_INCLUDE))
474 snprintf(fullname, sizeof(fullname),
475 "%s/%s", include_dir->directory, file_name);
477 if ((newfile = fopen(fullname, "r")) != NULL)
482 if (newfile == NULL) {
484 stop("Unable to open input file", EX_SOFTWARE);
488 if (type != SOURCE_FILE) {
489 include = (include_t *)malloc(sizeof(include_t));
490 if (include == NULL) {
491 stop("Unable to allocate include stack entry",
495 include->buffer = YY_CURRENT_BUFFER;
496 include->lineno = yylineno;
497 include->filename = yyfilename;
498 SLIST_INSERT_HEAD(&include_stack, include, links);
500 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
502 yyfilename = strdup(file_name);
505 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
506 const char **next_match,
507 struct macro_arg **match_marg, regmatch_t *match);
510 expand_macro(struct symbol *macro_symbol)
512 struct macro_arg *marg;
513 struct macro_arg *match_marg;
514 const char *body_head;
515 const char *body_pos;
516 const char *next_match;
517 regmatch_t match = { .rm_so = 0, .rm_eo = 0 };
520 * Due to the nature of unput, we must work
521 * backwards through the macro body performing
524 body_head = macro_symbol->info.macroinfo->body;
525 body_pos = body_head + strlen(body_head);
526 while (body_pos > body_head) {
527 next_match = body_head;
529 next_substitution(macro_symbol, body_pos, &next_match,
530 &match_marg, &match);
532 /* Put back everything up until the replacement. */
533 while (body_pos > next_match)
536 /* Perform the replacement. */
537 if (match_marg != NULL) {
540 next_match = match_marg->replacement_text;
541 strp = next_match + strlen(next_match);
542 while (strp > next_match)
545 /* Skip past the unexpanded macro arg. */
546 body_pos -= match.rm_eo - match.rm_so;
550 /* Cleanup replacement text. */
551 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
552 free(marg->replacement_text);
557 * Find the next substitution in the macro working backwards from
558 * body_pos until the beginning of the macro buffer. next_match
559 * should be initialized to the beginning of the macro buffer prior
560 * to calling this routine.
563 next_substitution(struct symbol *mac_symbol, const char *body_pos,
564 const char **next_match, struct macro_arg **match_marg,
567 regmatch_t matches[2];
568 struct macro_arg *marg;
569 const char *search_pos;
573 search_pos = *next_match;
575 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
577 retval = regexec(&marg->arg_regex, search_pos, 2,
580 && (matches[1].rm_eo + search_pos) <= body_pos
581 && (matches[1].rm_eo + search_pos) > *next_match) {
583 *next_match = match->rm_eo + search_pos;
587 } while (search_pos != *next_match);
595 yy_delete_buffer(YY_CURRENT_BUFFER);
597 if (yyfilename != NULL)
600 include = include_stack.slh_first;
601 if (include != NULL) {
602 yy_switch_to_buffer(include->buffer);
603 yylineno = include->lineno;
604 yyfilename = include->filename;
605 SLIST_REMOVE_HEAD(&include_stack, links);