2 /* $NetBSD: aicasm_scan.l,v 1.3.2.1 2005/03/04 16:43:50 skrll Exp $ */
4 * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
6 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
7 * Copyright (c) 2001, 2002 Adaptec Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
21 * 3. Neither the names of the above-listed copyright holders nor the names
22 * of any contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
25 * Alternatively, this software may be distributed under the terms of the
26 * GNU General Public License ("GPL") version 2 as published by the Free
27 * Software Foundation.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
39 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGES.
42 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_scan.l,v 1.21 2002/09/27 03:23:02 gibbs Exp $
45 #include <sys/types.h>
56 #include <sys/queue.h>
60 #include "aicasm_symbol.h"
61 #include "aicasm_gram.h"
63 /* This is used for macro body capture too, so err on the large size. */
64 #define MAX_STR_CONST 4096
65 static char string_buf[MAX_STR_CONST];
66 static char *string_buf_ptr;
67 static int parren_count;
68 static int quote_count;
72 PATH ([/]*[-A-Za-z0-9_.])+
73 WORD [A-Za-z_][-A-Za-z_0-9]*
76 MBODY ((\\[^\n])*[^\n\\]*)+
89 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
90 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
91 <COMMENT>\n { ++yylineno; }
93 <COMMENT>"*"+[^*/\n]* ;
94 <COMMENT>"/"+[^*/\n]* ;
95 <COMMENT>"*"+"/" { BEGIN INITIAL; }
97 string_buf_ptr = string_buf;
102 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
105 if (parren_count == 0) {
108 *string_buf_ptr = '\0';
109 yylval.sym = symtable_get(string_buf);
112 *string_buf_ptr++ = ')';
115 <CEXPR>\n { ++yylineno; }
120 while (*yptr != '\0') {
121 /* Remove duplicate spaces */
125 && string_buf_ptr != string_buf
126 && string_buf_ptr[-1] == ' ')
129 *string_buf_ptr++ = *yptr++;
133 VERSION { return T_VERSION; }
134 PREFIX { return T_PREFIX; }
135 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
137 string_buf_ptr = string_buf;
145 *string_buf_ptr++ = *yptr++;
150 *string_buf_ptr = '\0';
151 yylval.str = string_buf;
156 /* Register/SCB/SRAM definition keywords */
157 export { return T_EXPORT; }
158 register { return T_REGISTER; }
159 const { yylval.value = FALSE; return T_CONST; }
160 download { return T_DOWNLOAD; }
161 address { return T_ADDRESS; }
162 access_mode { return T_ACCESS_MODE; }
163 modes { return T_MODES; }
165 if (strcmp(yytext, "RW") == 0)
167 else if (strcmp(yytext, "RO") == 0)
173 BEGIN_CRITICAL { return T_BEGIN_CS; }
174 END_CRITICAL { return T_END_CS; }
175 SET_SRC_MODE { return T_SET_SRC_MODE; }
176 SET_DST_MODE { return T_SET_DST_MODE; }
177 field { return T_FIELD; }
178 enum { return T_ENUM; }
179 mask { return T_MASK; }
180 alias { return T_ALIAS; }
181 size { return T_SIZE; }
182 scb { return T_SCB; }
183 scratch_ram { return T_SRAM; }
184 accumulator { return T_ACCUM; }
185 mode_pointer { return T_MODE_PTR; }
186 allones { return T_ALLONES; }
187 allzeros { return T_ALLZEROS; }
188 none { return T_NONE; }
189 sindex { return T_SINDEX; }
193 shl { return T_SHL; }
194 shr { return T_SHR; }
195 ror { return T_ROR; }
196 rol { return T_ROL; }
197 mvi { return T_MVI; }
198 mov { return T_MOV; }
199 clr { return T_CLR; }
200 jmp { return T_JMP; }
202 jnc { return T_JNC; }
204 jne { return T_JNE; }
206 jnz { return T_JNZ; }
207 call { return T_CALL; }
208 add { return T_ADD; }
209 adc { return T_ADC; }
210 bmov { return T_BMOV; }
211 inc { return T_INC; }
212 dec { return T_DEC; }
213 stc { return T_STC; }
214 clc { return T_CLC; }
215 cmp { return T_CMP; }
216 not { return T_NOT; }
217 xor { return T_XOR; }
218 test { return T_TEST;}
219 and { return T_AND; }
221 ret { return T_RET; }
222 nop { return T_NOP; }
223 else { return T_ELSE; }
225 /* Allowed Symbols */
226 \<\< { return T_EXPR_LSHIFT; }
227 \>\> { return T_EXPR_RSHIFT; }
228 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
230 /* Number processing */
232 yylval.value = strtol(yytext, NULL, 8);
237 yylval.value = strtoul(yytext + 2, NULL, 16);
242 yylval.value = strtol(yytext, NULL, 10);
251 <INCLUDE>[<] { return yytext[0]; }
252 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
254 if (quote_count != 0)
263 string_buf_ptr = string_buf;
265 *string_buf_ptr++ = *yptr++;
266 yylval.str = string_buf;
267 *string_buf_ptr = '\0';
270 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
275 <MACRODEF>{WORD}{SPACE} {
278 /* Strip space and return as a normal symbol */
280 while (*yptr != ' ' && *yptr != '\t')
283 yylval.sym = symtable_get(yytext);
284 string_buf_ptr = string_buf;
290 * We store the symbol with its opening
291 * parren so we can differentiate macros
292 * that take args from macros with the
293 * same name that do not take args as
297 yylval.sym = symtable_get(yytext);
301 <MACROARGLIST>{WORD} {
305 <MACROARGLIST>{SPACE} ;
310 string_buf_ptr = string_buf;
315 snprintf(buf, sizeof(buf), "Invalid character "
316 "'%c' in macro argument list",
318 stop(buf, EX_DATAERR);
320 <MACROCALLARGS>{SPACE} ;
323 if (parren_count == 1)
325 *string_buf_ptr++ = '(';
329 if (parren_count == 0) {
333 *string_buf_ptr++ = ')';
335 <MACROCALLARGS>{MCARG} {
340 *string_buf_ptr++ = *yptr++;
343 if (string_buf_ptr != string_buf) {
345 * Return an argument and
346 * rescan this comma so we
347 * can return it as well.
349 *string_buf_ptr = '\0';
350 yylval.str = string_buf;
351 string_buf_ptr = string_buf;
358 /* Eat escaped newlines. */
362 /* Macros end on the first unescaped newline. */
364 *string_buf_ptr = '\0';
365 yylval.str = string_buf;
374 *string_buf_ptr++ = *yptr++;
380 /* May be a symbol or a macro invocation. */
381 yylval.sym = symtable_get(yytext);
382 if (yylval.sym->type == MACRO) {
383 YY_BUFFER_STATE old_state;
384 YY_BUFFER_STATE temp_state;
386 ycopy = strdup(yytext);
387 yptr = ycopy + yyleng;
390 old_state = YY_CURRENT_BUFFER;
392 yy_create_buffer(stdin,
394 yy_switch_to_buffer(temp_state);
395 mm_switch_to_buffer(old_state);
397 mm_switch_to_buffer(temp_state);
398 yy_switch_to_buffer(old_state);
399 mm_delete_buffer(temp_state);
400 expand_macro(yylval.sym);
402 if (yylval.sym->type == UNINITIALIZED) {
403 /* Try without the '(' */
404 symbol_delete(yylval.sym);
405 yytext[yyleng-1] = '\0';
407 symtable_get(yytext);
414 yylval.sym = symtable_get(yytext);
415 if (yylval.sym->type == MACRO) {
416 expand_macro(yylval.sym);
422 snprintf(buf, sizeof(buf), "Invalid character "
424 stop(buf, EX_DATAERR);
428 typedef struct include {
429 YY_BUFFER_STATE buffer;
432 SLIST_ENTRY(include) links;
435 SLIST_HEAD(, include) include_stack;
438 include_file(char *file_name, include_type type)
444 /* Try the current directory first */
445 if (includes_search_curdir != 0 || type == SOURCE_FILE)
446 newfile = fopen(file_name, "r");
448 if (newfile == NULL && type != SOURCE_FILE) {
449 path_entry_t include_dir;
450 for (include_dir = search_path.slh_first;
452 include_dir = include_dir->links.sle_next) {
453 char fullname[PATH_MAX];
455 if ((include_dir->quoted_includes_only == TRUE)
456 && (type != QUOTED_INCLUDE))
459 snprintf(fullname, sizeof(fullname),
460 "%s/%s", include_dir->directory, file_name);
462 if ((newfile = fopen(fullname, "r")) != NULL)
467 if (newfile == NULL) {
469 stop("Unable to open input file", EX_SOFTWARE);
473 if (type != SOURCE_FILE) {
474 include = (include_t *)malloc(sizeof(include_t));
475 if (include == NULL) {
476 stop("Unable to allocate include stack entry",
480 include->buffer = YY_CURRENT_BUFFER;
481 include->lineno = yylineno;
482 include->filename = yyfilename;
483 SLIST_INSERT_HEAD(&include_stack, include, links);
485 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
487 yyfilename = strdup(file_name);
490 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
491 const char **next_match,
492 struct macro_arg **match_marg, regmatch_t *match);
495 expand_macro(struct symbol *macro_symbol)
497 struct macro_arg *marg;
498 struct macro_arg *match_marg;
499 const char *body_head;
500 const char *body_pos;
501 const char *next_match;
504 * Due to the nature of unput, we must work
505 * backwards through the macro body performing
508 body_head = macro_symbol->info.macroinfo->body;
509 body_pos = body_head + strlen(body_head);
510 while (body_pos > body_head) {
513 next_match = body_head;
515 next_substitution(macro_symbol, body_pos, &next_match,
516 &match_marg, &match);
518 /* Put back everything up until the replacement. */
519 while (body_pos > next_match)
522 /* Perform the replacement. */
523 if (match_marg != NULL) {
526 next_match = match_marg->replacement_text;
527 strp = next_match + strlen(next_match);
528 while (strp > next_match)
531 /* Skip past the unexpanded macro arg. */
532 body_pos -= match.rm_eo - match.rm_so;
536 /* Cleanup replacement text. */
537 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
538 free(marg->replacement_text);
543 * Find the next substitution in the macro working backwards from
544 * body_pos until the beginning of the macro buffer. next_match
545 * should be initialized to the beginning of the macro buffer prior
546 * to calling this routine.
549 next_substitution(struct symbol *mac_symbol, const char *body_pos,
550 const char **next_match, struct macro_arg **match_marg,
553 regmatch_t matches[2];
554 struct macro_arg *marg;
555 const char *search_pos;
559 search_pos = *next_match;
561 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
563 retval = regexec(&marg->arg_regex, search_pos, 2,
566 && (matches[1].rm_eo + search_pos) <= body_pos
567 && (matches[1].rm_eo + search_pos) > *next_match) {
569 *next_match = match->rm_eo + search_pos;
573 } while (search_pos != *next_match);
581 yy_delete_buffer(YY_CURRENT_BUFFER);
583 if (yyfilename != NULL)
586 include = include_stack.slh_first;
587 if (include != NULL) {
588 yy_switch_to_buffer(include->buffer);
589 yylineno = include->lineno;
590 yyfilename = include->filename;
591 SLIST_REMOVE_HEAD(&include_stack, links);