v2.4.8 -> v2.4.8.1
[opensuse:kernel.git] / drivers / scsi / aic7xxx / aicasm / aicasm_scan.l
1 %{
2 /*
3  * Lexical Analyzer for the Aic7xxx SCSI Host adapter sequencer assembler.
4  *
5  * Copyright (c) 1997, 1998 Justin T. Gibbs.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: //depot/src/aic7xxx/aicasm/aicasm_scan.l#5 $
33  *
34  * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_scan.l,v 1.13 2000/09/22 22:19:54 gibbs Exp $
35  */
36
37 #include <sys/types.h>
38
39 #include <limits.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <sysexits.h>
43 #ifdef __linux__
44 #include "../queue.h"
45 #else
46 #include <sys/queue.h>
47 #endif
48
49 #include "aicasm.h"
50 #include "aicasm_symbol.h"
51 #include "y.tab.h"
52
53 #define MAX_STR_CONST 256
54 char string_buf[MAX_STR_CONST];
55 char *string_buf_ptr;
56 int  parren_count;
57 int  quote_count;
58 %}
59
60 PATH            [-/A-Za-z0-9_.]*[./][-/A-Za-z0-9_.]*
61 WORD            [A-Za-z_][-A-Za-z_0-9]*
62 SPACE           [ \t]+
63
64 %x COMMENT
65 %x CEXPR
66 %x INCLUDE
67 %x STRING
68
69 %%
70 \n                      { ++yylineno; }
71 "/*"                    { BEGIN COMMENT;  /* Enter comment eating state */ }
72 <COMMENT>"/*"           { fprintf(stderr, "Warning! Comment within comment."); }
73 <COMMENT>\n             { ++yylineno; }
74 <COMMENT>[^*/\n]*       ;
75 <COMMENT>"*"+[^*/\n]*   ;
76 <COMMENT>"/"+[^*/\n]*   ;
77 <COMMENT>"*"+"/"        { BEGIN INITIAL; }
78 if[ \t]*\(              {
79                                 string_buf_ptr = string_buf;
80                                 parren_count = 1;
81                                 BEGIN CEXPR;
82                                 return T_IF;
83                         }
84 <CEXPR>\(               {       *string_buf_ptr++ = '('; parren_count++; }
85 <CEXPR>\)               {
86                                 parren_count--;
87                                 if (parren_count == 0) {
88                                         /* All done */
89                                         BEGIN INITIAL;
90                                         *string_buf_ptr = '\0';
91                                         yylval.sym = symtable_get(string_buf);
92                                         return T_CEXPR;
93                                 } else {
94                                         *string_buf_ptr++ = ')';
95                                 }
96                         }
97 <CEXPR>\n               { ++yylineno; }
98 <CEXPR>[^()\n]+ {
99                                 char *yptr;
100
101                                 yptr = yytext;
102                                 while (*yptr != '\0') {
103                                         /* Remove duplicate spaces */
104                                         if (*yptr == '\t')
105                                                 *yptr = ' ';
106                                         if (*yptr == ' '
107                                          && string_buf_ptr != string_buf
108                                          && string_buf_ptr[-1] == ' ')
109                                                 yptr++;
110                                         else 
111                                                 *string_buf_ptr++ = *yptr++;
112                                 }
113                         }
114
115 VERSION                 { return T_VERSION; }
116 \"                      {
117                                 string_buf_ptr = string_buf;
118                                 BEGIN STRING;
119                         }
120 <STRING>[^"]+           {
121                                 char *yptr;
122
123                                 yptr = yytext;
124                                 while (*yptr)
125                                         *string_buf_ptr++ = *yptr++;
126                         }
127 <STRING>\"              {
128                                 /* All done */
129                                 BEGIN INITIAL;
130                                 *string_buf_ptr = '\0';
131                                 yylval.str = string_buf;
132                                 return T_STRING;
133                         }
134 {SPACE}                 ;
135
136         /* Register/SCB/SRAM definition keywords */
137 register                { return T_REGISTER; }
138 const                   { yylval.value = FALSE; return T_CONST; }
139 download                { return T_DOWNLOAD; }
140 address                 { return T_ADDRESS; }
141 access_mode             { return T_ACCESS_MODE; }
142 RW|RO|WO                {
143                                  if (strcmp(yytext, "RW") == 0)
144                                         yylval.value = RW;
145                                  else if (strcmp(yytext, "RO") == 0)
146                                         yylval.value = RO;
147                                  else
148                                         yylval.value = WO;
149                                  return T_MODE;
150                         }
151 BEGIN_CRITICAL          { return T_BEGIN_CS; }
152 END_CRITICAL            { return T_END_CS; }
153 bit                     { return T_BIT; }
154 mask                    { return T_MASK; }
155 alias                   { return T_ALIAS; }
156 size                    { return T_SIZE; }
157 scb                     { return T_SCB; }
158 scratch_ram             { return T_SRAM; }
159 accumulator             { return T_ACCUM; }
160 allones                 { return T_ALLONES; }
161 allzeros                { return T_ALLZEROS; }
162 none                    { return T_NONE; }
163 sindex                  { return T_SINDEX; }
164 A                       { return T_A; }
165
166         /* Opcodes */
167 shl                     { return T_SHL; }
168 shr                     { return T_SHR; }
169 ror                     { return T_ROR; }
170 rol                     { return T_ROL; }
171 mvi                     { return T_MVI; }
172 mov                     { return T_MOV; }
173 clr                     { return T_CLR; }
174 jmp                     { return T_JMP; }
175 jc                      { return T_JC;  }
176 jnc                     { return T_JNC; }
177 je                      { return T_JE;  }
178 jne                     { return T_JNE; }
179 jz                      { return T_JZ;  }
180 jnz                     { return T_JNZ; }
181 call                    { return T_CALL; }
182 add                     { return T_ADD; }
183 adc                     { return T_ADC; }
184 bmov                    { return T_BMOV; }
185 inc                     { return T_INC; }
186 dec                     { return T_DEC; }
187 stc                     { return T_STC; }
188 clc                     { return T_CLC; }
189 cmp                     { return T_CMP; }
190 not                     { return T_NOT; }
191 xor                     { return T_XOR; }
192 test                    { return T_TEST;}
193 and                     { return T_AND; }
194 or                      { return T_OR;  }
195 ret                     { return T_RET; }
196 nop                     { return T_NOP; }
197 else                    { return T_ELSE; }
198
199         /* Allowed Symbols */
200 [-+,:()~|&."{};<>[\]!=] { return yytext[0]; }
201
202         /* Number processing */
203 0[0-7]*                 {
204                                 yylval.value = strtol(yytext, NULL, 8);
205                                 return T_NUMBER;
206                         }
207
208 0[xX][0-9a-fA-F]+       {
209                                 yylval.value = strtoul(yytext + 2, NULL, 16);
210                                 return T_NUMBER;
211                         }
212
213 [1-9][0-9]*             {
214                                 yylval.value = strtol(yytext, NULL, 10);
215                                 return T_NUMBER;
216                         }
217
218         /* Include Files */
219 #include{SPACE}         {
220                                 BEGIN INCLUDE;
221                                 quote_count = 0;
222                                 return T_INCLUDE;
223                         }
224 <INCLUDE>[<]            { return yytext[0]; }
225 <INCLUDE>[>]            { BEGIN INITIAL; return yytext[0]; }
226 <INCLUDE>[\"]           {
227                                 if (quote_count != 0)
228                                         BEGIN INITIAL;
229                                 quote_count++;
230                                 return yytext[0];
231                         }
232 <INCLUDE>.              { stop("Invalid include line", EX_DATAERR); }
233
234         /* For parsing C include files with #define foo */
235 #define                 { yylval.value = TRUE; return T_CONST; }
236         /* Throw away macros */
237 #define[^\n]*[()]+[^\n]* ;
238 <INITIAL,INCLUDE>{PATH} {
239                                 char *yptr;
240
241                                 yptr = yytext;
242                                 string_buf_ptr = string_buf;
243                                 while (*yptr)
244                                         *string_buf_ptr++ = *yptr++;
245                                 yylval.str = string_buf;
246                                 *string_buf_ptr = '\0';
247                                 return T_PATH;
248                         }
249
250 {WORD}                  { yylval.sym = symtable_get(yytext);  return T_SYMBOL; }
251
252 .                       { 
253                                 char buf[255];
254
255                                 snprintf(buf, sizeof(buf), "Invalid character "
256                                          "'%c'", yytext[0]);
257                                 stop(buf, EX_DATAERR);
258                         }
259 %%
260
261 typedef struct include {
262         YY_BUFFER_STATE  buffer;
263         int              lineno;
264         char            *filename;
265         SLIST_ENTRY(include) links;
266 }include_t;
267
268 SLIST_HEAD(, include) include_stack;
269
270 void
271 include_file(char *file_name, include_type type)
272 {
273         FILE *newfile;
274         include_t *include;
275
276         newfile = NULL;
277         /* Try the current directory first */
278         if (includes_search_curdir != 0 || type == SOURCE_FILE)
279                 newfile = fopen(file_name, "r");
280
281         if (newfile == NULL && type != SOURCE_FILE) {
282                 path_entry_t include_dir;
283                 for (include_dir = search_path.slh_first;
284                      include_dir != NULL;                
285                      include_dir = include_dir->links.sle_next) {
286                         char fullname[PATH_MAX];
287
288                         if ((include_dir->quoted_includes_only == TRUE)
289                          && (type != QUOTED_INCLUDE))
290                                 continue;
291
292                         snprintf(fullname, sizeof(fullname),
293                                  "%s/%s", include_dir->directory, file_name);
294
295                         if ((newfile = fopen(fullname, "r")) != NULL)
296                                 break;
297                 }
298         }
299
300         if (newfile == NULL) {
301                 perror(file_name);
302                 stop("Unable to open input file", EX_SOFTWARE);
303                 /* NOTREACHED */
304         }
305
306         if (type != SOURCE_FILE) {
307                 include = (include_t *)malloc(sizeof(include_t));
308                 if (include == NULL) {
309                         stop("Unable to allocate include stack entry",
310                              EX_SOFTWARE);
311                         /* NOTREACHED */
312                 }
313                 include->buffer = YY_CURRENT_BUFFER;
314                 include->lineno = yylineno;
315                 include->filename = yyfilename;
316                 SLIST_INSERT_HEAD(&include_stack, include, links);
317         }
318         yy_switch_to_buffer(yy_create_buffer(newfile, YY_BUF_SIZE));
319         yylineno = 1;
320         yyfilename = strdup(file_name);
321 }
322
323 int
324 yywrap()
325 {
326         include_t *include;
327
328         yy_delete_buffer(YY_CURRENT_BUFFER);
329         (void)fclose(yyin);
330         if (yyfilename != NULL)
331                 free(yyfilename);
332         yyfilename = NULL;
333         include = include_stack.slh_first;
334         if (include != NULL) {
335                 yy_switch_to_buffer(include->buffer);
336                 yylineno = include->lineno;
337                 yyfilename = include->filename;
338                 SLIST_REMOVE_HEAD(&include_stack, links);
339                 free(include);
340                 return (0);
341         }
342         return (1);
343 }