aicasm_scan.l revision 1.6 1 %{
2 /* $NetBSD: aicasm_scan.l,v 1.6 2020/06/27 16:19:38 jdolecek Exp $ */
3 /*
4 * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
5 *
6 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
7 * Copyright (c) 2001, 2002 Adaptec Inc.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
24 *
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.
28 *
29 * NO WARRANTY
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.
41 *
42 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_scan.l,v 1.21 2002/09/27 03:23:02 gibbs Exp $
43 */
44
45 #include <sys/types.h>
46
47 #include <inttypes.h>
48 #include <limits.h>
49 #include <regex.h>
50 #include <stdio.h>
51 #include <string.h>
52 #include <sysexits.h>
53 #ifdef __linux__
54 #include "../queue.h"
55 #else
56 #include <sys/queue.h>
57 #endif
58
59 #include "aicasm.h"
60 #include "aicasm_symbol.h"
61 #include "aicasm_gram.h"
62
63 int yyparse(void);
64 void yy_delete_buffer(YY_BUFFER_STATE);
65 int mmlex(void);
66 int mmparse(void);
67 void mm_delete_buffer(YY_BUFFER_STATE);
68 void mm_switch_to_buffer(YY_BUFFER_STATE);
69
70 /* This is used for macro body capture too, so err on the large size. */
71 #define MAX_STR_CONST 4096
72 static char string_buf[MAX_STR_CONST];
73 static char *string_buf_ptr;
74 static int parren_count;
75 static int quote_count;
76 static char buf[255];
77 %}
78
79 PATH ([/]*[-A-Za-z0-9_.])+
80 WORD [A-Za-z_][-A-Za-z_0-9]*
81 SPACE [ \t]+
82 MCARG [^(), \t]+
83 MBODY ((\\[^\n])*[^\n\\]*)+
84
85 %x COMMENT
86 %x CEXPR
87 %x INCLUDE
88 %x STRING
89 %x MACRODEF
90 %x MACROARGLIST
91 %x MACROCALLARGS
92 %x MACROBODY
93
94 %%
95 \n { ++yylineno; }
96 "/*" { BEGIN COMMENT; /* Enter comment eating state */ }
97 <COMMENT>"/*" { fprintf(stderr, "Warning! Comment within comment."); }
98 <COMMENT>\n { ++yylineno; }
99 <COMMENT>[^*/\n]* ;
100 <COMMENT>"*"+[^*/\n]* ;
101 <COMMENT>"/"+[^*/\n]* ;
102 <COMMENT>"*"+"/" { BEGIN INITIAL; }
103 if[ \t]*\( {
104 string_buf_ptr = string_buf;
105 parren_count = 1;
106 BEGIN CEXPR;
107 return T_IF;
108 }
109 <CEXPR>\( { *string_buf_ptr++ = '('; parren_count++; }
110 <CEXPR>\) {
111 parren_count--;
112 if (parren_count == 0) {
113 /* All done */
114 BEGIN INITIAL;
115 *string_buf_ptr = '\0';
116 yylval.sym = symtable_get(string_buf);
117 return T_CEXPR;
118 } else {
119 *string_buf_ptr++ = ')';
120 }
121 }
122 <CEXPR>\n { ++yylineno; }
123 <CEXPR>[^()\n]+ {
124 char *yptr;
125
126 yptr = yytext;
127 while (*yptr != '\0') {
128 /* Remove duplicate spaces */
129 if (*yptr == '\t')
130 *yptr = ' ';
131 if (*yptr == ' '
132 && string_buf_ptr != string_buf
133 && string_buf_ptr[-1] == ' ')
134 yptr++;
135 else
136 *string_buf_ptr++ = *yptr++;
137 }
138 }
139
140 VERSION { return T_VERSION; }
141 PREFIX { return T_PREFIX; }
142 PATCH_ARG_LIST { return T_PATCH_ARG_LIST; }
143 \" {
144 string_buf_ptr = string_buf;
145 BEGIN STRING;
146 }
147 <STRING>[^"]+ {
148 char *yptr;
149
150 yptr = yytext;
151 while (*yptr)
152 *string_buf_ptr++ = *yptr++;
153 }
154 <STRING>\" {
155 /* All done */
156 BEGIN INITIAL;
157 *string_buf_ptr = '\0';
158 yylval.str = string_buf;
159 return T_STRING;
160 }
161 {SPACE} ;
162
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; }
171 RW|RO|WO {
172 if (strcmp(yytext, "RW") == 0)
173 yylval.value = RW;
174 else if (strcmp(yytext, "RO") == 0)
175 yylval.value = RO;
176 else
177 yylval.value = WO;
178 return T_MODE;
179 }
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; }
197 A { return T_A; }
198
199 /* Opcodes */
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; }
208 jc { return T_JC; }
209 jnc { return T_JNC; }
210 je { return T_JE; }
211 jne { return T_JNE; }
212 jz { return T_JZ; }
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; }
227 or { return T_OR; }
228 ret { return T_RET; }
229 nop { return T_NOP; }
230 else { return T_ELSE; }
231
232 /* Allowed Symbols */
233 \<\< { return T_EXPR_LSHIFT; }
234 \>\> { return T_EXPR_RSHIFT; }
235 [-+,:()~|&."{};<>[\]/*!=] { return yytext[0]; }
236
237 /* Number processing */
238 0[0-7]* {
239 yylval.value = strtol(yytext, NULL, 8);
240 return T_NUMBER;
241 }
242
243 0[xX][0-9a-fA-F]+ {
244 yylval.value = strtoul(yytext + 2, NULL, 16);
245 return T_NUMBER;
246 }
247
248 [1-9][0-9]* {
249 yylval.value = strtol(yytext, NULL, 10);
250 return T_NUMBER;
251 }
252 /* Include Files */
253 #include{SPACE} {
254 BEGIN INCLUDE;
255 quote_count = 0;
256 return T_INCLUDE;
257 }
258 <INCLUDE>[<] { return yytext[0]; }
259 <INCLUDE>[>] { BEGIN INITIAL; return yytext[0]; }
260 <INCLUDE>[\"] {
261 if (quote_count != 0)
262 BEGIN INITIAL;
263 quote_count++;
264 return yytext[0];
265 }
266 <INCLUDE>{PATH} {
267 char *yptr;
268
269 yptr = yytext;
270 string_buf_ptr = string_buf;
271 while (*yptr)
272 *string_buf_ptr++ = *yptr++;
273 yylval.str = string_buf;
274 *string_buf_ptr = '\0';
275 return T_PATH;
276 }
277 <INCLUDE>. { stop("Invalid include line", EX_DATAERR); }
278 #define{SPACE} {
279 BEGIN MACRODEF;
280 return T_DEFINE;
281 }
282 <MACRODEF>{WORD}{SPACE} {
283 char *yptr;
284
285 /* Strip space and return as a normal symbol */
286 yptr = yytext;
287 while (*yptr != ' ' && *yptr != '\t')
288 yptr++;
289 *yptr = '\0';
290 yylval.sym = symtable_get(yytext);
291 string_buf_ptr = string_buf;
292 BEGIN MACROBODY;
293 return T_SYMBOL;
294 }
295 <MACRODEF>{WORD}\( {
296 /*
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
301 * is allowed in C.
302 */
303 BEGIN MACROARGLIST;
304 yylval.sym = symtable_get(yytext);
305 unput('(');
306 return T_SYMBOL;
307 }
308 <MACROARGLIST>{WORD} {
309 yylval.str = yytext;
310 return T_ARG;
311 }
312 <MACROARGLIST>{SPACE} ;
313 <MACROARGLIST>[(,] {
314 return yytext[0];
315 }
316 <MACROARGLIST>[)] {
317 string_buf_ptr = string_buf;
318 BEGIN MACROBODY;
319 return ')';
320 }
321 <MACROARGLIST>. {
322 snprintf(buf, sizeof(buf), "Invalid character "
323 "'%c' in macro argument list",
324 yytext[0]);
325 stop(buf, EX_DATAERR);
326 }
327 <MACROCALLARGS>{SPACE} ;
328 <MACROCALLARGS>\( {
329 parren_count++;
330 if (parren_count == 1)
331 return ('(');
332 *string_buf_ptr++ = '(';
333 }
334 <MACROCALLARGS>\) {
335 parren_count--;
336 if (parren_count == 0) {
337 BEGIN INITIAL;
338 return (')');
339 }
340 *string_buf_ptr++ = ')';
341 }
342 <MACROCALLARGS>{MCARG} {
343 char *yptr;
344
345 yptr = yytext;
346 while (*yptr)
347 *string_buf_ptr++ = *yptr++;
348 }
349 <MACROCALLARGS>\, {
350 if (string_buf_ptr != string_buf) {
351 /*
352 * Return an argument and
353 * rescan this comma so we
354 * can return it as well.
355 */
356 *string_buf_ptr = '\0';
357 yylval.str = string_buf;
358 string_buf_ptr = string_buf;
359 unput(',');
360 return T_ARG;
361 }
362 return ',';
363 }
364 <MACROBODY>\\\n {
365 /* Eat escaped newlines. */
366 ++yylineno;
367 }
368 <MACROBODY>\n {
369 /* Macros end on the first unescaped newline. */
370 BEGIN INITIAL;
371 *string_buf_ptr = '\0';
372 yylval.str = string_buf;
373 ++yylineno;
374 return T_MACROBODY;
375 }
376 <MACROBODY>{MBODY} {
377 char *yptr;
378
379 yptr = yytext;
380 while (*yptr)
381 *string_buf_ptr++ = *yptr++;
382 }
383 {WORD}\( {
384 char *yptr;
385 char *ycopy;
386
387 /* May be a symbol or a macro invocation. */
388 yylval.sym = symtable_get(yytext);
389 if (yylval.sym->type == MACRO) {
390 YY_BUFFER_STATE old_state;
391 YY_BUFFER_STATE temp_state;
392
393 ycopy = strdup(yytext);
394 yptr = ycopy + yyleng;
395 while (yptr > ycopy)
396 unput(*--yptr);
397 old_state = YY_CURRENT_BUFFER;
398 temp_state =
399 yy_create_buffer(stdin,
400 YY_BUF_SIZE);
401 yy_switch_to_buffer(temp_state);
402 mm_switch_to_buffer(old_state);
403 mmparse();
404 mm_switch_to_buffer(temp_state);
405 yy_switch_to_buffer(old_state);
406 mm_delete_buffer(temp_state);
407 expand_macro(yylval.sym);
408 } else {
409 if (yylval.sym->type == UNINITIALIZED) {
410 /* Try without the '(' */
411 symbol_delete(yylval.sym);
412 yytext[yyleng-1] = '\0';
413 yylval.sym =
414 symtable_get(yytext);
415 }
416 unput('(');
417 return T_SYMBOL;
418 }
419 }
420 {WORD} {
421 yylval.sym = symtable_get(yytext);
422 if (yylval.sym->type == MACRO) {
423 expand_macro(yylval.sym);
424 } else {
425 return T_SYMBOL;
426 }
427 }
428 . {
429 snprintf(buf, sizeof(buf), "Invalid character "
430 "'%c'", yytext[0]);
431 stop(buf, EX_DATAERR);
432 }
433 %%
434
435 typedef struct include {
436 YY_BUFFER_STATE buffer;
437 int lineno;
438 char *filename;
439 SLIST_ENTRY(include) links;
440 }include_t;
441
442 SLIST_HEAD(, include) include_stack;
443
444 void
445 include_file(char *file_name, include_type type)
446 {
447 FILE *newfile;
448 include_t *include;
449
450 newfile = NULL;
451 /* Try the current directory first */
452 if (includes_search_curdir != 0 || type == SOURCE_FILE)
453 newfile = fopen(file_name, "r");
454
455 if (newfile == NULL && type != SOURCE_FILE) {
456 path_entry_t include_dir;
457 for (include_dir = search_path.slh_first;
458 include_dir != NULL;
459 include_dir = include_dir->links.sle_next) {
460 char fullname[PATH_MAX];
461
462 if ((include_dir->quoted_includes_only == TRUE)
463 && (type != QUOTED_INCLUDE))
464 continue;
465
466 snprintf(fullname, sizeof(fullname),
467 "%s/%s", include_dir->directory, file_name);
468
469 if ((newfile = fopen(fullname, "r")) != NULL)
470 break;
471 }
472 }
473
474 if (newfile == NULL) {
475 perror(file_name);
476 stop("Unable to open input file", EX_SOFTWARE);
477 /* NOTREACHED */
478 }
479
480 if (type != SOURCE_FILE) {
481 include = (include_t *)malloc(sizeof(include_t));
482 if (include == NULL) {
483 stop("Unable to allocate include stack entry",
484 EX_SOFTWARE);
485 /* NOTREACHED */
486 }
487 include->buffer = YY_CURRENT_BUFFER;
488 include->lineno = yylineno;
489 include->filename = yyfilename;
490 SLIST_INSERT_HEAD(&include_stack, include, links);
491 }
492 yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
493 yylineno = 1;
494 yyfilename = strdup(file_name);
495 }
496
497 static void next_substitution(struct symbol *mac_symbol, const char *body_pos,
498 const char **next_match,
499 struct macro_arg **match_marg, regmatch_t *match);
500
501 void
502 expand_macro(struct symbol *macro_symbol)
503 {
504 struct macro_arg *marg;
505 struct macro_arg *match_marg;
506 const char *body_head;
507 const char *body_pos;
508 const char *next_match;
509
510 /*
511 * Due to the nature of unput, we must work
512 * backwards through the macro body performing
513 * any expansions.
514 */
515 body_head = macro_symbol->info.macroinfo->body;
516 body_pos = body_head + strlen(body_head);
517 while (body_pos > body_head) {
518 regmatch_t match;
519
520 next_match = body_head;
521 match_marg = NULL;
522 next_substitution(macro_symbol, body_pos, &next_match,
523 &match_marg, &match);
524
525 /* Put back everything up until the replacement. */
526 while (body_pos > next_match)
527 unput(*--body_pos);
528
529 /* Perform the replacement. */
530 if (match_marg != NULL) {
531 const char *strp;
532
533 next_match = match_marg->replacement_text;
534 strp = next_match + strlen(next_match);
535 while (strp > next_match)
536 unput(*--strp);
537
538 /* Skip past the unexpanded macro arg. */
539 body_pos -= match.rm_eo - match.rm_so;
540 }
541 }
542
543 /* Cleanup replacement text. */
544 STAILQ_FOREACH(marg, ¯o_symbol->info.macroinfo->args, links) {
545 free(marg->replacement_text);
546 }
547 }
548
549 /*
550 * Find the next substitution in the macro working backwards from
551 * body_pos until the beginning of the macro buffer. next_match
552 * should be initialized to the beginning of the macro buffer prior
553 * to calling this routine.
554 */
555 static void
556 next_substitution(struct symbol *mac_symbol, const char *body_pos,
557 const char **next_match, struct macro_arg **match_marg,
558 regmatch_t *match)
559 {
560 regmatch_t matches[2];
561 struct macro_arg *marg;
562 const char *search_pos;
563 int retval;
564
565 do {
566 search_pos = *next_match;
567
568 STAILQ_FOREACH(marg, &mac_symbol->info.macroinfo->args, links) {
569
570 retval = regexec(&marg->arg_regex, search_pos, 2,
571 matches, 0);
572 if (retval == 0
573 && (matches[1].rm_eo + search_pos) <= body_pos
574 && (matches[1].rm_eo + search_pos) > *next_match) {
575 *match = matches[1];
576 *next_match = match->rm_eo + search_pos;
577 *match_marg = marg;
578 }
579 }
580 } while (search_pos != *next_match);
581 }
582
583 int
584 yywrap()
585 {
586 include_t *include;
587
588 yy_delete_buffer(YY_CURRENT_BUFFER);
589 (void)fclose(yyin);
590 if (yyfilename != NULL)
591 free(yyfilename);
592 yyfilename = NULL;
593 include = include_stack.slh_first;
594 if (include != NULL) {
595 yy_switch_to_buffer(include->buffer);
596 yylineno = include->lineno;
597 yyfilename = include->filename;
598 SLIST_REMOVE_HEAD(&include_stack, links);
599 free(include);
600 return (0);
601 }
602 return (1);
603 }
604