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