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