Home | History | Annotate | Line # | Download | only in aic7xxx
aicasm_gram.y revision 1.4.4.1
      1      1.1   fvdl %{
      2  1.4.4.1   yamt /*	$NetBSD: aicasm_gram.y,v 1.4.4.1 2006/12/30 20:48:35 yamt Exp $	*/
      3      1.1   fvdl 
      4      1.1   fvdl /*
      5      1.1   fvdl  * Parser for the Aic7xxx SCSI Host adapter sequencer assembler.
      6      1.1   fvdl  *
      7      1.3   fvdl  * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
      8      1.3   fvdl  * Copyright (c) 2001, 2002 Adaptec Inc.
      9      1.1   fvdl  * All rights reserved.
     10      1.1   fvdl  *
     11      1.1   fvdl  * Redistribution and use in source and binary forms, with or without
     12      1.1   fvdl  * modification, are permitted provided that the following conditions
     13      1.1   fvdl  * are met:
     14      1.1   fvdl  * 1. Redistributions of source code must retain the above copyright
     15      1.1   fvdl  *    notice, this list of conditions, and the following disclaimer,
     16      1.1   fvdl  *    without modification.
     17      1.3   fvdl  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18      1.3   fvdl  *    substantially similar to the "NO WARRANTY" disclaimer below
     19      1.3   fvdl  *    ("Disclaimer") and any redistribution must be conditioned upon
     20      1.3   fvdl  *    including a substantially similar Disclaimer requirement for further
     21      1.3   fvdl  *    binary redistribution.
     22      1.3   fvdl  * 3. Neither the names of the above-listed copyright holders nor the names
     23      1.3   fvdl  *    of any contributors may be used to endorse or promote products derived
     24      1.3   fvdl  *    from this software without specific prior written permission.
     25      1.1   fvdl  *
     26      1.3   fvdl  * Alternatively, this software may be distributed under the terms of the
     27      1.3   fvdl  * GNU General Public License ("GPL") version 2 as published by the Free
     28      1.3   fvdl  * Software Foundation.
     29      1.3   fvdl  *
     30      1.3   fvdl  * NO WARRANTY
     31      1.3   fvdl  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32      1.3   fvdl  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33      1.3   fvdl  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34      1.3   fvdl  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35      1.3   fvdl  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36      1.1   fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37      1.1   fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38      1.3   fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39      1.3   fvdl  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40      1.3   fvdl  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41      1.3   fvdl  * POSSIBILITY OF SUCH DAMAGES.
     42      1.1   fvdl  *
     43      1.3   fvdl  * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_gram.y,v 1.23 2003/01/20 18:02:11 gibbs Exp $
     44      1.1   fvdl  */
     45      1.1   fvdl 
     46      1.3   fvdl #include <sys/types.h>
     47      1.3   fvdl 
     48      1.3   fvdl #include <inttypes.h>
     49      1.3   fvdl #include <regex.h>
     50      1.1   fvdl #include <stdio.h>
     51      1.1   fvdl #include <stdlib.h>
     52      1.1   fvdl #include <string.h>
     53      1.1   fvdl #include <sysexits.h>
     54      1.1   fvdl 
     55      1.3   fvdl #ifdef __linux__
     56      1.3   fvdl #include "../queue.h"
     57      1.3   fvdl #else
     58      1.1   fvdl #include <sys/queue.h>
     59      1.3   fvdl #endif
     60      1.1   fvdl 
     61      1.1   fvdl #include "aicasm.h"
     62      1.1   fvdl #include "aicasm_symbol.h"
     63      1.3   fvdl #include "aicasm_insformat.h"
     64      1.1   fvdl 
     65      1.1   fvdl int yylineno;
     66      1.1   fvdl char *yyfilename;
     67      1.3   fvdl char stock_prefix[] = "aic_";
     68      1.3   fvdl char *prefix = stock_prefix;
     69      1.3   fvdl char *patch_arg_list;
     70      1.3   fvdl char *versions;
     71      1.3   fvdl static char errbuf[255];
     72      1.3   fvdl static char regex_pattern[255];
     73      1.1   fvdl static symbol_t *cur_symbol;
     74      1.3   fvdl static symbol_t *field_symbol;
     75      1.3   fvdl static symbol_t *scb_or_sram_symbol;
     76      1.1   fvdl static symtype cur_symtype;
     77      1.3   fvdl static symbol_ref_t accumulator;
     78      1.3   fvdl static symbol_ref_t mode_ptr;
     79      1.1   fvdl static symbol_ref_t allones;
     80      1.1   fvdl static symbol_ref_t allzeros;
     81      1.1   fvdl static symbol_ref_t none;
     82      1.1   fvdl static symbol_ref_t sindex;
     83      1.1   fvdl static int instruction_ptr;
     84      1.3   fvdl static int num_srams;
     85      1.1   fvdl static int sram_or_scb_offset;
     86      1.1   fvdl static int download_constant_count;
     87      1.3   fvdl static int in_critical_section;
     88      1.3   fvdl static u_int enum_increment;
     89      1.3   fvdl static u_int enum_next_value;
     90      1.3   fvdl 
     91      1.3   fvdl static void process_field(int field_type, symbol_t *sym, int mask);
     92      1.3   fvdl static void initialize_symbol(symbol_t *symbol);
     93      1.3   fvdl static void add_macro_arg(const char *argtext, int position);
     94      1.3   fvdl static void add_macro_body(const char *bodytext);
     95      1.3   fvdl static void process_register(symbol_t **p_symbol);
     96      1.3   fvdl static void format_1_instr(int opcode, symbol_ref_t *dest,
     97      1.3   fvdl 			   expression_t *immed, symbol_ref_t *src, int ret);
     98      1.3   fvdl static void format_2_instr(int opcode, symbol_ref_t *dest,
     99      1.3   fvdl 			   expression_t *places, symbol_ref_t *src, int ret);
    100      1.3   fvdl static void format_3_instr(int opcode, symbol_ref_t *src,
    101      1.3   fvdl 			   expression_t *immed, symbol_ref_t *address);
    102      1.3   fvdl static void test_readable_symbol(symbol_t *symbol);
    103      1.3   fvdl static void test_writable_symbol(symbol_t *symbol);
    104      1.3   fvdl static void type_check(symbol_t *symbol, expression_t *expression, int and_op);
    105      1.3   fvdl static void make_expression(expression_t *immed, int value);
    106      1.3   fvdl static void add_conditional(symbol_t *symbol);
    107      1.3   fvdl static void add_version(const char *verstring);
    108      1.3   fvdl static int  is_download_const(expression_t *immed);
    109      1.1   fvdl 
    110      1.1   fvdl #define SRAM_SYMNAME "SRAM_BASE"
    111      1.1   fvdl #define SCB_SYMNAME "SCB_BASE"
    112      1.1   fvdl %}
    113      1.1   fvdl 
    114      1.1   fvdl %union {
    115      1.3   fvdl 	u_int		value;
    116      1.1   fvdl 	char		*str;
    117      1.1   fvdl 	symbol_t	*sym;
    118      1.1   fvdl 	symbol_ref_t	sym_ref;
    119      1.1   fvdl 	expression_t	expression;
    120      1.1   fvdl }
    121      1.1   fvdl 
    122      1.1   fvdl %token T_REGISTER
    123      1.1   fvdl 
    124      1.1   fvdl %token <value> T_CONST
    125      1.1   fvdl 
    126      1.3   fvdl %token T_EXPORT
    127      1.3   fvdl 
    128      1.1   fvdl %token T_DOWNLOAD
    129      1.1   fvdl 
    130      1.1   fvdl %token T_SCB
    131      1.1   fvdl 
    132      1.1   fvdl %token T_SRAM
    133      1.1   fvdl 
    134      1.1   fvdl %token T_ALIAS
    135      1.1   fvdl 
    136      1.1   fvdl %token T_SIZE
    137      1.1   fvdl 
    138      1.3   fvdl %token T_EXPR_LSHIFT
    139      1.3   fvdl 
    140      1.3   fvdl %token T_EXPR_RSHIFT
    141      1.3   fvdl 
    142      1.1   fvdl %token <value> T_ADDRESS
    143      1.1   fvdl 
    144      1.1   fvdl %token T_ACCESS_MODE
    145      1.1   fvdl 
    146      1.3   fvdl %token T_MODES
    147      1.3   fvdl 
    148      1.3   fvdl %token T_DEFINE
    149      1.3   fvdl 
    150      1.3   fvdl %token T_SET_SRC_MODE
    151      1.3   fvdl 
    152      1.3   fvdl %token T_SET_DST_MODE
    153      1.3   fvdl 
    154      1.1   fvdl %token <value> T_MODE
    155      1.1   fvdl 
    156      1.3   fvdl %token T_BEGIN_CS
    157      1.3   fvdl 
    158      1.3   fvdl %token T_END_CS
    159      1.3   fvdl 
    160      1.3   fvdl %token T_FIELD
    161      1.3   fvdl 
    162      1.3   fvdl %token T_ENUM
    163      1.1   fvdl 
    164      1.1   fvdl %token T_MASK
    165      1.1   fvdl 
    166      1.1   fvdl %token <value> T_NUMBER
    167      1.1   fvdl 
    168      1.3   fvdl %token <str> T_PATH T_STRING T_ARG T_MACROBODY
    169      1.1   fvdl 
    170      1.1   fvdl %token <sym> T_CEXPR
    171      1.1   fvdl 
    172      1.3   fvdl %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
    173      1.1   fvdl 
    174      1.1   fvdl %token <value> T_SHR T_SHL T_ROR T_ROL
    175      1.1   fvdl 
    176      1.1   fvdl %token <value> T_MVI T_MOV T_CLR T_BMOV
    177      1.1   fvdl 
    178      1.1   fvdl %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
    179      1.1   fvdl 
    180      1.1   fvdl %token <value> T_ADD T_ADC
    181      1.1   fvdl 
    182      1.1   fvdl %token <value> T_INC T_DEC
    183      1.1   fvdl 
    184      1.1   fvdl %token <value> T_STC T_CLC
    185      1.1   fvdl 
    186      1.3   fvdl %token <value> T_CMP T_NOT T_XOR
    187      1.1   fvdl 
    188      1.1   fvdl %token <value> T_TEST T_AND
    189      1.1   fvdl 
    190      1.1   fvdl %token <value> T_OR
    191      1.1   fvdl 
    192      1.1   fvdl %token T_RET
    193      1.1   fvdl 
    194      1.1   fvdl %token T_NOP
    195      1.1   fvdl 
    196      1.3   fvdl %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
    197      1.1   fvdl 
    198      1.1   fvdl %token T_A
    199      1.1   fvdl 
    200      1.1   fvdl %token <sym> T_SYMBOL
    201      1.1   fvdl 
    202      1.1   fvdl %token T_NL
    203      1.1   fvdl 
    204      1.1   fvdl %token T_IF T_ELSE T_ELSE_IF T_ENDIF
    205      1.1   fvdl 
    206      1.1   fvdl %type <sym_ref> reg_symbol address destination source opt_source
    207      1.1   fvdl 
    208      1.1   fvdl %type <expression> expression immediate immediate_or_a
    209      1.1   fvdl 
    210      1.3   fvdl %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
    211      1.1   fvdl 
    212      1.3   fvdl %type <value> mode_value mode_list macro_arglist
    213      1.1   fvdl 
    214      1.1   fvdl %left '|'
    215      1.1   fvdl %left '&'
    216      1.3   fvdl %left T_EXPR_LSHIFT T_EXPR_RSHIFT
    217      1.1   fvdl %left '+' '-'
    218      1.3   fvdl %left '*' '/'
    219      1.1   fvdl %right '~'
    220      1.1   fvdl %nonassoc UMINUS
    221      1.1   fvdl %%
    222      1.1   fvdl 
    223      1.1   fvdl program:
    224      1.1   fvdl 	include
    225      1.1   fvdl |	program include
    226      1.3   fvdl |	prefix
    227      1.3   fvdl |	program prefix
    228      1.3   fvdl |	patch_arg_list
    229      1.3   fvdl |	program patch_arg_list
    230      1.3   fvdl |	version
    231      1.3   fvdl |	program version
    232      1.1   fvdl |	register
    233      1.1   fvdl |	program register
    234      1.1   fvdl |	constant
    235      1.1   fvdl |	program constant
    236      1.3   fvdl |	macrodefn
    237      1.3   fvdl |	program macrodefn
    238      1.1   fvdl |	scratch_ram
    239      1.1   fvdl |	program scratch_ram
    240      1.1   fvdl |	scb
    241      1.1   fvdl |	program scb
    242      1.1   fvdl |	label
    243      1.1   fvdl |	program label
    244      1.3   fvdl |	set_src_mode
    245      1.3   fvdl |	program set_src_mode
    246      1.3   fvdl |	set_dst_mode
    247      1.3   fvdl |	program set_dst_mode
    248      1.3   fvdl |	critical_section_start
    249      1.3   fvdl |	program critical_section_start
    250      1.3   fvdl |	critical_section_end
    251      1.3   fvdl |	program critical_section_end
    252      1.1   fvdl |	conditional
    253      1.1   fvdl |	program conditional
    254      1.1   fvdl |	code
    255      1.1   fvdl |	program code
    256      1.1   fvdl ;
    257      1.1   fvdl 
    258      1.1   fvdl include:
    259      1.1   fvdl 	T_INCLUDE '<' T_PATH '>'
    260      1.3   fvdl 	{
    261      1.3   fvdl 		include_file($3, BRACKETED_INCLUDE);
    262      1.3   fvdl 	}
    263      1.1   fvdl |	T_INCLUDE '"' T_PATH '"'
    264      1.3   fvdl 	{
    265      1.3   fvdl 		include_file($3, QUOTED_INCLUDE);
    266      1.3   fvdl 	}
    267      1.3   fvdl ;
    268      1.3   fvdl 
    269      1.3   fvdl prefix:
    270      1.3   fvdl 	T_PREFIX '=' T_STRING
    271      1.3   fvdl 	{
    272      1.3   fvdl 		if (prefix != stock_prefix)
    273      1.3   fvdl 			stop("Prefix multiply defined",
    274      1.3   fvdl 			     EX_DATAERR);
    275      1.3   fvdl 		prefix = strdup($3);
    276      1.3   fvdl 		if (prefix == NULL)
    277      1.3   fvdl 			stop("Unable to record prefix", EX_SOFTWARE);
    278      1.3   fvdl 	}
    279      1.3   fvdl ;
    280      1.3   fvdl 
    281      1.3   fvdl patch_arg_list:
    282      1.3   fvdl 	T_PATCH_ARG_LIST '=' T_STRING
    283      1.3   fvdl 	{
    284      1.3   fvdl 		if (patch_arg_list != NULL)
    285      1.3   fvdl 			stop("Patch argument list multiply defined",
    286      1.3   fvdl 			     EX_DATAERR);
    287      1.3   fvdl 		patch_arg_list = strdup($3);
    288      1.3   fvdl 		if (patch_arg_list == NULL)
    289      1.3   fvdl 			stop("Unable to record patch arg list", EX_SOFTWARE);
    290      1.3   fvdl 	}
    291      1.3   fvdl ;
    292      1.3   fvdl 
    293      1.3   fvdl version:
    294      1.3   fvdl 	T_VERSION '=' T_STRING
    295      1.3   fvdl 	{ add_version($3); }
    296      1.1   fvdl ;
    297      1.1   fvdl 
    298      1.1   fvdl register:
    299      1.1   fvdl 	T_REGISTER { cur_symtype = REGISTER; } reg_definition
    300      1.1   fvdl ;
    301      1.1   fvdl 
    302      1.1   fvdl reg_definition:
    303      1.1   fvdl 	T_SYMBOL '{'
    304      1.1   fvdl 		{
    305      1.1   fvdl 			if ($1->type != UNINITIALIZED) {
    306      1.1   fvdl 				stop("Register multiply defined", EX_DATAERR);
    307      1.1   fvdl 				/* NOTREACHED */
    308      1.1   fvdl 			}
    309      1.4  perry 			cur_symbol = $1;
    310      1.1   fvdl 			cur_symbol->type = cur_symtype;
    311      1.1   fvdl 			initialize_symbol(cur_symbol);
    312      1.1   fvdl 		}
    313      1.1   fvdl 		reg_attribute_list
    314      1.1   fvdl 	'}'
    315      1.4  perry 		{
    316      1.1   fvdl 			/*
    317      1.1   fvdl 			 * Default to allowing everything in for registers
    318      1.1   fvdl 			 * with no bit or mask definitions.
    319      1.1   fvdl 			 */
    320      1.1   fvdl 			if (cur_symbol->info.rinfo->valid_bitmask == 0)
    321      1.1   fvdl 				cur_symbol->info.rinfo->valid_bitmask = 0xFF;
    322      1.1   fvdl 
    323      1.1   fvdl 			if (cur_symbol->info.rinfo->size == 0)
    324      1.1   fvdl 				cur_symbol->info.rinfo->size = 1;
    325      1.1   fvdl 
    326      1.1   fvdl 			/*
    327      1.1   fvdl 			 * This might be useful for registers too.
    328      1.1   fvdl 			 */
    329      1.1   fvdl 			if (cur_symbol->type != REGISTER) {
    330      1.1   fvdl 				if (cur_symbol->info.rinfo->address == 0)
    331      1.1   fvdl 					cur_symbol->info.rinfo->address =
    332      1.1   fvdl 					    sram_or_scb_offset;
    333      1.1   fvdl 				sram_or_scb_offset +=
    334      1.1   fvdl 				    cur_symbol->info.rinfo->size;
    335      1.1   fvdl 			}
    336      1.1   fvdl 			cur_symbol = NULL;
    337      1.1   fvdl 		}
    338      1.1   fvdl ;
    339      1.1   fvdl 
    340      1.1   fvdl reg_attribute_list:
    341      1.1   fvdl 	reg_attribute
    342      1.1   fvdl |	reg_attribute_list reg_attribute
    343      1.1   fvdl ;
    344      1.1   fvdl 
    345      1.4  perry reg_attribute:
    346      1.1   fvdl 	reg_address
    347      1.1   fvdl |	size
    348      1.1   fvdl |	access_mode
    349      1.3   fvdl |	modes
    350      1.3   fvdl |	field_defn
    351      1.3   fvdl |	enum_defn
    352      1.1   fvdl |	mask_defn
    353      1.1   fvdl |	alias
    354      1.1   fvdl |	accumulator
    355      1.3   fvdl |	mode_pointer
    356      1.1   fvdl |	allones
    357      1.1   fvdl |	allzeros
    358      1.1   fvdl |	none
    359      1.1   fvdl |	sindex
    360      1.1   fvdl ;
    361      1.1   fvdl 
    362      1.1   fvdl reg_address:
    363      1.1   fvdl 	T_ADDRESS T_NUMBER
    364      1.1   fvdl 	{
    365      1.1   fvdl 		cur_symbol->info.rinfo->address = $2;
    366      1.1   fvdl 	}
    367      1.1   fvdl ;
    368      1.1   fvdl 
    369      1.1   fvdl size:
    370      1.1   fvdl 	T_SIZE T_NUMBER
    371      1.1   fvdl 	{
    372      1.1   fvdl 		cur_symbol->info.rinfo->size = $2;
    373      1.3   fvdl 		if (scb_or_sram_symbol != NULL) {
    374      1.3   fvdl 			u_int max_addr;
    375      1.3   fvdl 			u_int sym_max_addr;
    376      1.3   fvdl 
    377      1.3   fvdl 			max_addr = scb_or_sram_symbol->info.rinfo->address
    378      1.3   fvdl 				 + scb_or_sram_symbol->info.rinfo->size;
    379      1.3   fvdl 			sym_max_addr = cur_symbol->info.rinfo->address
    380      1.3   fvdl 				     + cur_symbol->info.rinfo->size;
    381      1.3   fvdl 
    382      1.3   fvdl 			if (sym_max_addr > max_addr)
    383      1.3   fvdl 				stop("SCB or SRAM space exhausted", EX_DATAERR);
    384      1.3   fvdl 		}
    385      1.1   fvdl 	}
    386      1.1   fvdl ;
    387      1.1   fvdl 
    388      1.1   fvdl access_mode:
    389      1.1   fvdl 	T_ACCESS_MODE T_MODE
    390      1.1   fvdl 	{
    391      1.1   fvdl 		cur_symbol->info.rinfo->mode = $2;
    392      1.1   fvdl 	}
    393      1.1   fvdl ;
    394      1.1   fvdl 
    395      1.3   fvdl modes:
    396      1.3   fvdl 	T_MODES mode_list
    397      1.3   fvdl 	{
    398      1.3   fvdl 		cur_symbol->info.rinfo->modes = $2;
    399      1.3   fvdl 	}
    400      1.3   fvdl ;
    401      1.3   fvdl 
    402      1.3   fvdl mode_list:
    403      1.3   fvdl 	mode_value
    404      1.3   fvdl 	{
    405      1.3   fvdl 		$$ = $1;
    406      1.3   fvdl 	}
    407      1.3   fvdl |	mode_list ',' mode_value
    408      1.3   fvdl 	{
    409      1.3   fvdl 		$$ = $1 | $3;
    410      1.3   fvdl 	}
    411      1.3   fvdl ;
    412      1.3   fvdl 
    413      1.3   fvdl mode_value:
    414      1.3   fvdl 	T_NUMBER
    415      1.3   fvdl 	{
    416      1.3   fvdl 		if ($1 > 4) {
    417      1.3   fvdl 			stop("Valid register modes range between 0 and 4.",
    418      1.3   fvdl 			     EX_DATAERR);
    419      1.3   fvdl 			/* NOTREACHED */
    420      1.3   fvdl 		}
    421      1.3   fvdl 
    422      1.3   fvdl 		$$ = (0x1 << $1);
    423      1.3   fvdl 	}
    424      1.3   fvdl |	T_SYMBOL
    425      1.1   fvdl 	{
    426      1.3   fvdl 		symbol_t *symbol;
    427      1.3   fvdl 
    428      1.3   fvdl 		symbol = $1;
    429      1.3   fvdl 		if (symbol->type != CONST) {
    430      1.3   fvdl 			stop("Only \"const\" symbols allowed in "
    431      1.3   fvdl 			     "mode definitions.", EX_DATAERR);
    432      1.3   fvdl 			/* NOTREACHED */
    433      1.3   fvdl 		}
    434      1.3   fvdl 		if (symbol->info.cinfo->value > 4) {
    435      1.3   fvdl 			stop("Valid register modes range between 0 and 4.",
    436      1.3   fvdl 			     EX_DATAERR);
    437      1.3   fvdl 			/* NOTREACHED */
    438      1.3   fvdl 		}
    439      1.3   fvdl 		$$ = (0x1 << symbol->info.cinfo->value);
    440      1.3   fvdl 	}
    441      1.3   fvdl ;
    442      1.3   fvdl 
    443      1.3   fvdl field_defn:
    444      1.3   fvdl 	T_FIELD
    445      1.3   fvdl 		{
    446      1.3   fvdl 			field_symbol = NULL;
    447      1.3   fvdl 			enum_next_value = 0;
    448      1.3   fvdl 			enum_increment = 1;
    449      1.3   fvdl 		}
    450      1.3   fvdl 	'{' enum_entry_list '}'
    451      1.3   fvdl |	T_FIELD T_SYMBOL expression
    452      1.3   fvdl 		{
    453      1.3   fvdl 			process_field(FIELD, $2, $3.value);
    454      1.3   fvdl 			field_symbol = $2;
    455      1.3   fvdl 			enum_next_value = 0;
    456      1.3   fvdl 			enum_increment = 0x01 << (ffs($3.value) - 1);
    457      1.3   fvdl 		}
    458      1.3   fvdl 	'{' enum_entry_list '}'
    459      1.3   fvdl |	T_FIELD T_SYMBOL expression
    460      1.3   fvdl 	{
    461      1.3   fvdl 		process_field(FIELD, $2, $3.value);
    462      1.3   fvdl 	}
    463      1.3   fvdl ;
    464      1.3   fvdl 
    465      1.3   fvdl enum_defn:
    466      1.3   fvdl 	T_ENUM
    467      1.3   fvdl 		{
    468      1.3   fvdl 			field_symbol = NULL;
    469      1.3   fvdl 			enum_next_value = 0;
    470      1.3   fvdl 			enum_increment = 1;
    471      1.3   fvdl 		}
    472      1.3   fvdl 	'{' enum_entry_list '}'
    473      1.3   fvdl |	T_ENUM T_SYMBOL expression
    474      1.3   fvdl 		{
    475      1.3   fvdl 			process_field(ENUM, $2, $3.value);
    476      1.3   fvdl 			field_symbol = $2;
    477      1.3   fvdl 			enum_next_value = 0;
    478      1.3   fvdl 			enum_increment = 0x01 << (ffs($3.value) - 1);
    479      1.3   fvdl 		}
    480      1.3   fvdl 	'{' enum_entry_list '}'
    481      1.3   fvdl ;
    482      1.3   fvdl 
    483      1.3   fvdl enum_entry_list:
    484      1.3   fvdl 	enum_entry
    485      1.3   fvdl |	enum_entry_list ',' enum_entry
    486      1.3   fvdl ;
    487      1.3   fvdl 
    488      1.3   fvdl enum_entry:
    489      1.3   fvdl 	T_SYMBOL
    490      1.3   fvdl 	{
    491      1.3   fvdl 		process_field(ENUM_ENTRY, $1, enum_next_value);
    492      1.3   fvdl 		enum_next_value += enum_increment;
    493      1.3   fvdl 	}
    494      1.3   fvdl |	T_SYMBOL expression
    495      1.3   fvdl 	{
    496      1.3   fvdl 		process_field(ENUM_ENTRY, $1, $2.value);
    497      1.3   fvdl 		enum_next_value = $2.value + enum_increment;
    498      1.1   fvdl 	}
    499      1.1   fvdl ;
    500      1.1   fvdl 
    501      1.1   fvdl mask_defn:
    502      1.1   fvdl 	T_MASK T_SYMBOL expression
    503      1.1   fvdl 	{
    504      1.3   fvdl 		process_field(MASK, $2, $3.value);
    505      1.1   fvdl 	}
    506      1.1   fvdl ;
    507      1.1   fvdl 
    508      1.1   fvdl alias:
    509      1.1   fvdl 	T_ALIAS	T_SYMBOL
    510      1.1   fvdl 	{
    511      1.1   fvdl 		if ($2->type != UNINITIALIZED) {
    512      1.1   fvdl 			stop("Re-definition of register alias",
    513      1.1   fvdl 			     EX_DATAERR);
    514      1.1   fvdl 			/* NOTREACHED */
    515      1.1   fvdl 		}
    516      1.1   fvdl 		$2->type = ALIAS;
    517      1.1   fvdl 		initialize_symbol($2);
    518      1.1   fvdl 		$2->info.ainfo->parent = cur_symbol;
    519      1.1   fvdl 	}
    520      1.1   fvdl ;
    521      1.1   fvdl 
    522      1.1   fvdl accumulator:
    523      1.1   fvdl 	T_ACCUM
    524      1.1   fvdl 	{
    525      1.3   fvdl 		if (accumulator.symbol != NULL) {
    526      1.1   fvdl 			stop("Only one accumulator definition allowed",
    527      1.1   fvdl 			     EX_DATAERR);
    528      1.1   fvdl 			/* NOTREACHED */
    529      1.1   fvdl 		}
    530      1.3   fvdl 		accumulator.symbol = cur_symbol;
    531      1.3   fvdl 	}
    532      1.3   fvdl ;
    533      1.3   fvdl 
    534      1.3   fvdl mode_pointer:
    535      1.3   fvdl 	T_MODE_PTR
    536      1.3   fvdl 	{
    537      1.3   fvdl 		if (mode_ptr.symbol != NULL) {
    538      1.3   fvdl 			stop("Only one mode pointer definition allowed",
    539      1.3   fvdl 			     EX_DATAERR);
    540      1.3   fvdl 			/* NOTREACHED */
    541      1.3   fvdl 		}
    542      1.3   fvdl 		mode_ptr.symbol = cur_symbol;
    543      1.1   fvdl 	}
    544      1.1   fvdl ;
    545      1.1   fvdl 
    546      1.1   fvdl allones:
    547      1.1   fvdl 	T_ALLONES
    548      1.1   fvdl 	{
    549      1.1   fvdl 		if (allones.symbol != NULL) {
    550      1.1   fvdl 			stop("Only one definition of allones allowed",
    551      1.1   fvdl 			     EX_DATAERR);
    552      1.1   fvdl 			/* NOTREACHED */
    553      1.1   fvdl 		}
    554      1.1   fvdl 		allones.symbol = cur_symbol;
    555      1.1   fvdl 	}
    556      1.1   fvdl ;
    557      1.1   fvdl 
    558      1.1   fvdl allzeros:
    559      1.1   fvdl 	T_ALLZEROS
    560      1.1   fvdl 	{
    561      1.1   fvdl 		if (allzeros.symbol != NULL) {
    562      1.1   fvdl 			stop("Only one definition of allzeros allowed",
    563      1.1   fvdl 			     EX_DATAERR);
    564      1.1   fvdl 			/* NOTREACHED */
    565      1.1   fvdl 		}
    566      1.1   fvdl 		allzeros.symbol = cur_symbol;
    567      1.1   fvdl 	}
    568      1.1   fvdl ;
    569      1.1   fvdl 
    570      1.1   fvdl none:
    571      1.1   fvdl 	T_NONE
    572      1.1   fvdl 	{
    573      1.1   fvdl 		if (none.symbol != NULL) {
    574      1.1   fvdl 			stop("Only one definition of none allowed",
    575      1.1   fvdl 			     EX_DATAERR);
    576      1.1   fvdl 			/* NOTREACHED */
    577      1.1   fvdl 		}
    578      1.1   fvdl 		none.symbol = cur_symbol;
    579      1.1   fvdl 	}
    580      1.1   fvdl ;
    581      1.1   fvdl 
    582      1.1   fvdl sindex:
    583      1.1   fvdl 	T_SINDEX
    584      1.1   fvdl 	{
    585      1.1   fvdl 		if (sindex.symbol != NULL) {
    586      1.1   fvdl 			stop("Only one definition of sindex allowed",
    587      1.1   fvdl 			     EX_DATAERR);
    588      1.1   fvdl 			/* NOTREACHED */
    589      1.1   fvdl 		}
    590      1.1   fvdl 		sindex.symbol = cur_symbol;
    591      1.1   fvdl 	}
    592      1.1   fvdl ;
    593      1.1   fvdl 
    594      1.1   fvdl expression:
    595      1.1   fvdl 	expression '|' expression
    596      1.1   fvdl 	{
    597      1.1   fvdl 		 $$.value = $1.value | $3.value;
    598      1.1   fvdl 		 symlist_merge(&$$.referenced_syms,
    599      1.1   fvdl 			       &$1.referenced_syms,
    600      1.1   fvdl 			       &$3.referenced_syms);
    601      1.1   fvdl 	}
    602      1.1   fvdl |	expression '&' expression
    603      1.1   fvdl 	{
    604      1.1   fvdl 		$$.value = $1.value & $3.value;
    605      1.1   fvdl 		symlist_merge(&$$.referenced_syms,
    606      1.1   fvdl 			       &$1.referenced_syms,
    607      1.1   fvdl 			       &$3.referenced_syms);
    608      1.1   fvdl 	}
    609      1.1   fvdl |	expression '+' expression
    610      1.1   fvdl 	{
    611      1.1   fvdl 		$$.value = $1.value + $3.value;
    612      1.1   fvdl 		symlist_merge(&$$.referenced_syms,
    613      1.1   fvdl 			       &$1.referenced_syms,
    614      1.1   fvdl 			       &$3.referenced_syms);
    615      1.1   fvdl 	}
    616      1.1   fvdl |	expression '-' expression
    617      1.1   fvdl 	{
    618      1.1   fvdl 		$$.value = $1.value - $3.value;
    619      1.1   fvdl 		symlist_merge(&($$.referenced_syms),
    620      1.1   fvdl 			       &($1.referenced_syms),
    621      1.1   fvdl 			       &($3.referenced_syms));
    622      1.1   fvdl 	}
    623      1.3   fvdl |	expression '*' expression
    624      1.3   fvdl 	{
    625      1.3   fvdl 		$$.value = $1.value * $3.value;
    626      1.3   fvdl 		symlist_merge(&($$.referenced_syms),
    627      1.3   fvdl 			       &($1.referenced_syms),
    628      1.3   fvdl 			       &($3.referenced_syms));
    629      1.3   fvdl 	}
    630      1.3   fvdl |	expression '/' expression
    631      1.3   fvdl 	{
    632      1.3   fvdl 		$$.value = $1.value / $3.value;
    633      1.3   fvdl 		symlist_merge(&($$.referenced_syms),
    634      1.3   fvdl 			       &($1.referenced_syms),
    635      1.3   fvdl 			       &($3.referenced_syms));
    636      1.3   fvdl 	}
    637      1.3   fvdl | 	expression T_EXPR_LSHIFT expression
    638      1.3   fvdl 	{
    639      1.3   fvdl 		$$.value = $1.value << $3.value;
    640      1.3   fvdl 		symlist_merge(&$$.referenced_syms,
    641      1.3   fvdl 			       &$1.referenced_syms,
    642      1.3   fvdl 			       &$3.referenced_syms);
    643      1.3   fvdl 	}
    644      1.3   fvdl | 	expression T_EXPR_RSHIFT expression
    645      1.3   fvdl 	{
    646      1.3   fvdl 		$$.value = $1.value >> $3.value;
    647      1.3   fvdl 		symlist_merge(&$$.referenced_syms,
    648      1.3   fvdl 			       &$1.referenced_syms,
    649      1.3   fvdl 			       &$3.referenced_syms);
    650      1.3   fvdl 	}
    651      1.1   fvdl |	'(' expression ')'
    652      1.1   fvdl 	{
    653      1.1   fvdl 		$$ = $2;
    654      1.1   fvdl 	}
    655      1.1   fvdl |	'~' expression
    656      1.1   fvdl 	{
    657      1.1   fvdl 		$$ = $2;
    658      1.1   fvdl 		$$.value = (~$$.value) & 0xFF;
    659      1.1   fvdl 	}
    660      1.1   fvdl |	'-' expression %prec UMINUS
    661      1.1   fvdl 	{
    662      1.1   fvdl 		$$ = $2;
    663      1.1   fvdl 		$$.value = -$$.value;
    664      1.1   fvdl 	}
    665      1.1   fvdl |	T_NUMBER
    666      1.1   fvdl 	{
    667      1.1   fvdl 		$$.value = $1;
    668      1.1   fvdl 		SLIST_INIT(&$$.referenced_syms);
    669      1.1   fvdl 	}
    670      1.1   fvdl |	T_SYMBOL
    671      1.1   fvdl 	{
    672      1.1   fvdl 		symbol_t *symbol;
    673      1.1   fvdl 
    674      1.1   fvdl 		symbol = $1;
    675      1.1   fvdl 		switch (symbol->type) {
    676      1.1   fvdl 		case ALIAS:
    677      1.1   fvdl 			symbol = $1->info.ainfo->parent;
    678      1.1   fvdl 		case REGISTER:
    679      1.1   fvdl 		case SCBLOC:
    680      1.1   fvdl 		case SRAMLOC:
    681      1.1   fvdl 			$$.value = symbol->info.rinfo->address;
    682      1.1   fvdl 			break;
    683      1.1   fvdl 		case MASK:
    684      1.3   fvdl 		case FIELD:
    685      1.3   fvdl 		case ENUM:
    686      1.3   fvdl 		case ENUM_ENTRY:
    687      1.3   fvdl 			$$.value = symbol->info.finfo->value;
    688      1.1   fvdl 			break;
    689      1.1   fvdl 		case DOWNLOAD_CONST:
    690      1.1   fvdl 		case CONST:
    691      1.1   fvdl 			$$.value = symbol->info.cinfo->value;
    692      1.1   fvdl 			break;
    693      1.1   fvdl 		case UNINITIALIZED:
    694      1.1   fvdl 		default:
    695      1.1   fvdl 		{
    696      1.3   fvdl 			snprintf(errbuf, sizeof(errbuf),
    697      1.1   fvdl 				 "Undefined symbol %s referenced",
    698      1.1   fvdl 				 symbol->name);
    699      1.3   fvdl 			stop(errbuf, EX_DATAERR);
    700      1.1   fvdl 			/* NOTREACHED */
    701      1.1   fvdl 			break;
    702      1.1   fvdl 		}
    703      1.1   fvdl 		}
    704      1.1   fvdl 		SLIST_INIT(&$$.referenced_syms);
    705      1.1   fvdl 		symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
    706      1.1   fvdl 	}
    707      1.1   fvdl ;
    708      1.1   fvdl 
    709      1.1   fvdl constant:
    710      1.4  perry 	T_CONST T_SYMBOL expression
    711      1.1   fvdl 	{
    712      1.1   fvdl 		if ($2->type != UNINITIALIZED) {
    713      1.1   fvdl 			stop("Re-definition of symbol as a constant",
    714      1.1   fvdl 			     EX_DATAERR);
    715      1.1   fvdl 			/* NOTREACHED */
    716      1.1   fvdl 		}
    717      1.1   fvdl 		$2->type = CONST;
    718      1.1   fvdl 		initialize_symbol($2);
    719      1.3   fvdl 		$2->info.cinfo->value = $3.value;
    720      1.1   fvdl 	}
    721      1.1   fvdl |	T_CONST T_SYMBOL T_DOWNLOAD
    722      1.1   fvdl 	{
    723      1.1   fvdl 		if ($1) {
    724      1.1   fvdl 			stop("Invalid downloaded constant declaration",
    725      1.1   fvdl 			     EX_DATAERR);
    726      1.1   fvdl 			/* NOTREACHED */
    727      1.1   fvdl 		}
    728      1.1   fvdl 		if ($2->type != UNINITIALIZED) {
    729      1.1   fvdl 			stop("Re-definition of symbol as a downloaded constant",
    730      1.1   fvdl 			     EX_DATAERR);
    731      1.1   fvdl 			/* NOTREACHED */
    732      1.1   fvdl 		}
    733      1.1   fvdl 		$2->type = DOWNLOAD_CONST;
    734      1.1   fvdl 		initialize_symbol($2);
    735      1.1   fvdl 		$2->info.cinfo->value = download_constant_count++;
    736      1.1   fvdl 	}
    737      1.1   fvdl ;
    738      1.1   fvdl 
    739      1.3   fvdl macrodefn_prologue:
    740      1.3   fvdl 	T_DEFINE T_SYMBOL
    741      1.3   fvdl 	{
    742      1.3   fvdl 		if ($2->type != UNINITIALIZED) {
    743      1.3   fvdl 			stop("Re-definition of symbol as a macro",
    744      1.3   fvdl 			     EX_DATAERR);
    745      1.3   fvdl 			/* NOTREACHED */
    746      1.3   fvdl 		}
    747      1.3   fvdl 		cur_symbol = $2;
    748      1.3   fvdl 		cur_symbol->type = MACRO;
    749      1.3   fvdl 		initialize_symbol(cur_symbol);
    750      1.3   fvdl 	}
    751      1.3   fvdl ;
    752      1.3   fvdl 
    753      1.3   fvdl macrodefn:
    754      1.3   fvdl 	macrodefn_prologue T_MACROBODY
    755      1.3   fvdl 	{
    756      1.3   fvdl 		add_macro_body($2);
    757      1.3   fvdl 	}
    758      1.3   fvdl |	macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
    759      1.3   fvdl 	{
    760      1.3   fvdl 		add_macro_body($5);
    761      1.3   fvdl 		cur_symbol->info.macroinfo->narg = $3;
    762      1.3   fvdl 	}
    763      1.3   fvdl ;
    764      1.3   fvdl 
    765      1.3   fvdl macro_arglist:
    766      1.3   fvdl 	{
    767      1.3   fvdl 		/* Macros can take no arguments */
    768      1.3   fvdl 		$$ = 0;
    769      1.3   fvdl 	}
    770      1.3   fvdl |	T_ARG
    771      1.1   fvdl 	{
    772      1.3   fvdl 		$$ = 1;
    773      1.3   fvdl 		add_macro_arg($1, 0);
    774      1.1   fvdl 	}
    775      1.3   fvdl |	macro_arglist ',' T_ARG
    776      1.1   fvdl 	{
    777      1.3   fvdl 		if ($1 == 0) {
    778  1.4.4.1   yamt 			stop("Comma without preceding argument in arg list",
    779      1.3   fvdl 			     EX_DATAERR);
    780      1.3   fvdl 			/* NOTREACHED */
    781      1.3   fvdl 		}
    782      1.3   fvdl 		$$ = $1 + 1;
    783      1.3   fvdl 		add_macro_arg($3, $1);
    784      1.1   fvdl 	}
    785      1.1   fvdl ;
    786      1.1   fvdl 
    787      1.1   fvdl scratch_ram:
    788      1.1   fvdl 	T_SRAM '{'
    789      1.1   fvdl 		{
    790      1.3   fvdl 			snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
    791      1.3   fvdl 				 num_srams);
    792      1.1   fvdl 			cur_symbol = symtable_get(SRAM_SYMNAME);
    793      1.1   fvdl 			cur_symtype = SRAMLOC;
    794      1.1   fvdl 			cur_symbol->type = SRAMLOC;
    795      1.1   fvdl 			initialize_symbol(cur_symbol);
    796      1.1   fvdl 		}
    797      1.1   fvdl 		reg_address
    798      1.1   fvdl 		{
    799      1.1   fvdl 			sram_or_scb_offset = cur_symbol->info.rinfo->address;
    800      1.1   fvdl 		}
    801      1.3   fvdl 		size
    802      1.3   fvdl 		{
    803      1.3   fvdl 			scb_or_sram_symbol = cur_symbol;
    804      1.3   fvdl 		}
    805      1.3   fvdl 		scb_or_sram_attributes
    806      1.1   fvdl 	'}'
    807      1.1   fvdl 		{
    808      1.1   fvdl 			cur_symbol = NULL;
    809      1.3   fvdl 			scb_or_sram_symbol = NULL;
    810      1.1   fvdl 		}
    811      1.1   fvdl ;
    812      1.1   fvdl 
    813      1.1   fvdl scb:
    814      1.1   fvdl 	T_SCB '{'
    815      1.1   fvdl 		{
    816      1.1   fvdl 			cur_symbol = symtable_get(SCB_SYMNAME);
    817      1.1   fvdl 			cur_symtype = SCBLOC;
    818      1.1   fvdl 			if (cur_symbol->type != UNINITIALIZED) {
    819      1.1   fvdl 				stop("Only one SRAM definition allowed",
    820      1.1   fvdl 				     EX_SOFTWARE);
    821      1.1   fvdl 				/* NOTREACHED */
    822      1.1   fvdl 			}
    823      1.1   fvdl 			cur_symbol->type = SCBLOC;
    824      1.1   fvdl 			initialize_symbol(cur_symbol);
    825      1.3   fvdl 			/* 64 bytes of SCB space */
    826      1.3   fvdl 			cur_symbol->info.rinfo->size = 64;
    827      1.1   fvdl 		}
    828      1.1   fvdl 		reg_address
    829      1.1   fvdl 		{
    830      1.1   fvdl 			sram_or_scb_offset = cur_symbol->info.rinfo->address;
    831      1.1   fvdl 		}
    832      1.3   fvdl 		size
    833      1.3   fvdl 		{
    834      1.3   fvdl 			scb_or_sram_symbol = cur_symbol;
    835      1.3   fvdl 		}
    836      1.3   fvdl 		scb_or_sram_attributes
    837      1.1   fvdl 	'}'
    838      1.1   fvdl 		{
    839      1.1   fvdl 			cur_symbol = NULL;
    840      1.3   fvdl 			scb_or_sram_symbol = NULL;
    841      1.1   fvdl 		}
    842      1.1   fvdl ;
    843      1.1   fvdl 
    844      1.3   fvdl scb_or_sram_attributes:
    845      1.3   fvdl 	/* NULL definition is okay */
    846      1.3   fvdl |	modes
    847      1.3   fvdl |	scb_or_sram_reg_list
    848      1.3   fvdl |	modes scb_or_sram_reg_list
    849      1.3   fvdl ;
    850      1.3   fvdl 
    851      1.1   fvdl scb_or_sram_reg_list:
    852      1.1   fvdl 	reg_definition
    853      1.1   fvdl |	scb_or_sram_reg_list reg_definition
    854      1.1   fvdl ;
    855      1.1   fvdl 
    856      1.1   fvdl reg_symbol:
    857      1.1   fvdl 	T_SYMBOL
    858      1.1   fvdl 	{
    859      1.1   fvdl 		process_register(&$1);
    860      1.1   fvdl 		$$.symbol = $1;
    861      1.1   fvdl 		$$.offset = 0;
    862      1.1   fvdl 	}
    863      1.3   fvdl |	T_SYMBOL '[' T_SYMBOL ']'
    864      1.3   fvdl 	{
    865      1.3   fvdl 		process_register(&$1);
    866      1.3   fvdl 		if ($3->type != CONST) {
    867      1.3   fvdl 			stop("register offset must be a constant", EX_DATAERR);
    868      1.3   fvdl 			/* NOTREACHED */
    869      1.3   fvdl 		}
    870      1.3   fvdl 		if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
    871      1.3   fvdl 			stop("Accessing offset beyond range of register",
    872      1.3   fvdl 			     EX_DATAERR);
    873      1.3   fvdl 			/* NOTREACHED */
    874      1.3   fvdl 		}
    875      1.3   fvdl 		$$.symbol = $1;
    876      1.3   fvdl 		$$.offset = $3->info.cinfo->value;
    877      1.3   fvdl 	}
    878      1.1   fvdl |	T_SYMBOL '[' T_NUMBER ']'
    879      1.1   fvdl 	{
    880      1.1   fvdl 		process_register(&$1);
    881      1.1   fvdl 		if (($3 + 1) > $1->info.rinfo->size) {
    882      1.1   fvdl 			stop("Accessing offset beyond range of register",
    883      1.1   fvdl 			     EX_DATAERR);
    884      1.1   fvdl 			/* NOTREACHED */
    885      1.1   fvdl 		}
    886      1.1   fvdl 		$$.symbol = $1;
    887      1.1   fvdl 		$$.offset = $3;
    888      1.1   fvdl 	}
    889      1.1   fvdl |	T_A
    890      1.1   fvdl 	{
    891      1.3   fvdl 		if (accumulator.symbol == NULL) {
    892      1.1   fvdl 			stop("No accumulator has been defined", EX_DATAERR);
    893      1.1   fvdl 			/* NOTREACHED */
    894      1.1   fvdl 		}
    895      1.3   fvdl 		$$.symbol = accumulator.symbol;
    896      1.1   fvdl 		$$.offset = 0;
    897      1.1   fvdl 	}
    898      1.1   fvdl ;
    899      1.1   fvdl 
    900      1.1   fvdl destination:
    901      1.1   fvdl 	reg_symbol
    902      1.1   fvdl 	{
    903      1.1   fvdl 		test_writable_symbol($1.symbol);
    904      1.1   fvdl 		$$ = $1;
    905      1.1   fvdl 	}
    906      1.1   fvdl ;
    907      1.1   fvdl 
    908      1.1   fvdl immediate:
    909      1.1   fvdl 	expression
    910      1.1   fvdl 	{ $$ = $1; }
    911      1.1   fvdl ;
    912      1.1   fvdl 
    913      1.1   fvdl immediate_or_a:
    914      1.1   fvdl 	expression
    915      1.1   fvdl 	{
    916      1.3   fvdl 		if ($1.value == 0 && is_download_const(&$1) == 0) {
    917      1.3   fvdl 			snprintf(errbuf, sizeof(errbuf),
    918      1.3   fvdl 				 "\nExpression evaluates to 0 and thus "
    919      1.3   fvdl 				 "references the accumulator.\n "
    920      1.3   fvdl 				 "If this is the desired effect, use 'A' "
    921      1.3   fvdl 				 "instead.\n");
    922      1.3   fvdl 			stop(errbuf, EX_DATAERR);
    923      1.3   fvdl 		}
    924      1.1   fvdl 		$$ = $1;
    925      1.1   fvdl 	}
    926      1.1   fvdl |	T_A
    927      1.1   fvdl 	{
    928      1.1   fvdl 		SLIST_INIT(&$$.referenced_syms);
    929      1.3   fvdl 		symlist_add(&$$.referenced_syms, accumulator.symbol,
    930      1.3   fvdl 			    SYMLIST_INSERT_HEAD);
    931      1.1   fvdl 		$$.value = 0;
    932      1.1   fvdl 	}
    933      1.1   fvdl ;
    934      1.1   fvdl 
    935      1.1   fvdl source:
    936      1.1   fvdl 	reg_symbol
    937      1.1   fvdl 	{
    938      1.1   fvdl 		test_readable_symbol($1.symbol);
    939      1.1   fvdl 		$$ = $1;
    940      1.1   fvdl 	}
    941      1.1   fvdl ;
    942      1.1   fvdl 
    943      1.1   fvdl opt_source:
    944      1.1   fvdl 	{
    945      1.1   fvdl 		$$.symbol = NULL;
    946      1.1   fvdl 		$$.offset = 0;
    947      1.1   fvdl 	}
    948      1.1   fvdl |	',' source
    949      1.1   fvdl 	{ $$ = $2; }
    950      1.1   fvdl ;
    951      1.1   fvdl 
    952      1.1   fvdl ret:
    953      1.1   fvdl 	{ $$ = 0; }
    954      1.1   fvdl |	T_RET
    955      1.1   fvdl 	{ $$ = 1; }
    956      1.1   fvdl ;
    957      1.1   fvdl 
    958      1.3   fvdl set_src_mode:
    959      1.3   fvdl 	T_SET_SRC_MODE T_NUMBER ';'
    960      1.3   fvdl 	{
    961      1.3   fvdl 		src_mode = $2;
    962      1.3   fvdl 	}
    963      1.3   fvdl ;
    964      1.3   fvdl 
    965      1.3   fvdl set_dst_mode:
    966      1.3   fvdl 	T_SET_DST_MODE T_NUMBER ';'
    967      1.3   fvdl 	{
    968      1.3   fvdl 		dst_mode = $2;
    969      1.3   fvdl 	}
    970      1.3   fvdl ;
    971      1.3   fvdl 
    972      1.3   fvdl critical_section_start:
    973      1.3   fvdl 	T_BEGIN_CS ';'
    974      1.3   fvdl 	{
    975      1.3   fvdl 		critical_section_t *cs;
    976      1.3   fvdl 
    977      1.3   fvdl 		if (in_critical_section != FALSE) {
    978      1.3   fvdl 			stop("Critical Section within Critical Section",
    979      1.3   fvdl 			     EX_DATAERR);
    980      1.3   fvdl 			/* NOTREACHED */
    981      1.3   fvdl 		}
    982      1.3   fvdl 		cs = cs_alloc();
    983      1.3   fvdl 		cs->begin_addr = instruction_ptr;
    984      1.3   fvdl 		in_critical_section = TRUE;
    985      1.3   fvdl 	}
    986      1.3   fvdl ;
    987      1.3   fvdl 
    988      1.3   fvdl critical_section_end:
    989      1.3   fvdl 	T_END_CS ';'
    990      1.3   fvdl 	{
    991      1.3   fvdl 		critical_section_t *cs;
    992      1.3   fvdl 
    993      1.3   fvdl 		if (in_critical_section == FALSE) {
    994      1.3   fvdl 			stop("Unballanced 'end_cs'", EX_DATAERR);
    995      1.3   fvdl 			/* NOTREACHED */
    996      1.3   fvdl 		}
    997      1.3   fvdl 		cs = TAILQ_LAST(&cs_tailq, cs_tailq);
    998      1.3   fvdl 		cs->end_addr = instruction_ptr;
    999      1.3   fvdl 		in_critical_section = FALSE;
   1000      1.3   fvdl 	}
   1001      1.3   fvdl ;
   1002      1.3   fvdl 
   1003      1.3   fvdl export:
   1004      1.3   fvdl 	{ $$ = 0; }
   1005      1.3   fvdl |	T_EXPORT
   1006      1.3   fvdl 	{ $$ = 1; }
   1007      1.3   fvdl ;
   1008      1.3   fvdl 
   1009      1.1   fvdl label:
   1010      1.3   fvdl 	export T_SYMBOL ':'
   1011      1.1   fvdl 	{
   1012      1.3   fvdl 		if ($2->type != UNINITIALIZED) {
   1013      1.1   fvdl 			stop("Program label multiply defined", EX_DATAERR);
   1014      1.1   fvdl 			/* NOTREACHED */
   1015      1.1   fvdl 		}
   1016      1.3   fvdl 		$2->type = LABEL;
   1017      1.3   fvdl 		initialize_symbol($2);
   1018      1.3   fvdl 		$2->info.linfo->address = instruction_ptr;
   1019      1.3   fvdl 		$2->info.linfo->exported = $1;
   1020      1.1   fvdl 	}
   1021      1.1   fvdl ;
   1022      1.1   fvdl 
   1023      1.1   fvdl address:
   1024      1.1   fvdl 	T_SYMBOL
   1025      1.1   fvdl 	{
   1026      1.1   fvdl 		$$.symbol = $1;
   1027      1.1   fvdl 		$$.offset = 0;
   1028      1.1   fvdl 	}
   1029      1.1   fvdl |	T_SYMBOL '+' T_NUMBER
   1030      1.1   fvdl 	{
   1031      1.1   fvdl 		$$.symbol = $1;
   1032      1.1   fvdl 		$$.offset = $3;
   1033      1.1   fvdl 	}
   1034      1.1   fvdl |	T_SYMBOL '-' T_NUMBER
   1035      1.1   fvdl 	{
   1036      1.1   fvdl 		$$.symbol = $1;
   1037      1.1   fvdl 		$$.offset = -$3;
   1038      1.1   fvdl 	}
   1039      1.1   fvdl |	'.'
   1040      1.1   fvdl 	{
   1041      1.1   fvdl 		$$.symbol = NULL;
   1042      1.1   fvdl 		$$.offset = 0;
   1043      1.1   fvdl 	}
   1044      1.1   fvdl |	'.' '+' T_NUMBER
   1045      1.1   fvdl 	{
   1046      1.1   fvdl 		$$.symbol = NULL;
   1047      1.1   fvdl 		$$.offset = $3;
   1048      1.1   fvdl 	}
   1049      1.1   fvdl |	'.' '-' T_NUMBER
   1050      1.1   fvdl 	{
   1051      1.1   fvdl 		$$.symbol = NULL;
   1052      1.1   fvdl 		$$.offset = -$3;
   1053      1.1   fvdl 	}
   1054      1.1   fvdl ;
   1055      1.1   fvdl 
   1056      1.1   fvdl conditional:
   1057      1.1   fvdl 	T_IF T_CEXPR '{'
   1058      1.1   fvdl 	{
   1059      1.1   fvdl 		scope_t *new_scope;
   1060      1.1   fvdl 
   1061      1.1   fvdl 		add_conditional($2);
   1062      1.1   fvdl 		new_scope = scope_alloc();
   1063      1.1   fvdl 		new_scope->type = SCOPE_IF;
   1064      1.1   fvdl 		new_scope->begin_addr = instruction_ptr;
   1065      1.1   fvdl 		new_scope->func_num = $2->info.condinfo->func_num;
   1066      1.1   fvdl 	}
   1067      1.1   fvdl |	T_ELSE T_IF T_CEXPR '{'
   1068      1.1   fvdl 	{
   1069      1.1   fvdl 		scope_t *new_scope;
   1070      1.1   fvdl 		scope_t *scope_context;
   1071      1.1   fvdl 		scope_t *last_scope;
   1072      1.1   fvdl 
   1073      1.1   fvdl 		/*
   1074      1.1   fvdl 		 * Ensure that the previous scope is either an
   1075      1.1   fvdl 		 * if or and else if.
   1076      1.1   fvdl 		 */
   1077      1.1   fvdl 		scope_context = SLIST_FIRST(&scope_stack);
   1078      1.1   fvdl 		last_scope = TAILQ_LAST(&scope_context->inner_scope,
   1079      1.1   fvdl 					scope_tailq);
   1080      1.1   fvdl 		if (last_scope == NULL
   1081      1.1   fvdl 		 || last_scope->type == T_ELSE) {
   1082      1.1   fvdl 
   1083      1.1   fvdl 			stop("'else if' without leading 'if'", EX_DATAERR);
   1084      1.1   fvdl 			/* NOTREACHED */
   1085      1.1   fvdl 		}
   1086      1.1   fvdl 		add_conditional($3);
   1087      1.1   fvdl 		new_scope = scope_alloc();
   1088      1.1   fvdl 		new_scope->type = SCOPE_ELSE_IF;
   1089      1.1   fvdl 		new_scope->begin_addr = instruction_ptr;
   1090      1.1   fvdl 		new_scope->func_num = $3->info.condinfo->func_num;
   1091      1.1   fvdl 	}
   1092      1.1   fvdl |	T_ELSE '{'
   1093      1.1   fvdl 	{
   1094      1.1   fvdl 		scope_t *new_scope;
   1095      1.1   fvdl 		scope_t *scope_context;
   1096      1.1   fvdl 		scope_t *last_scope;
   1097      1.1   fvdl 
   1098      1.1   fvdl 		/*
   1099      1.1   fvdl 		 * Ensure that the previous scope is either an
   1100      1.1   fvdl 		 * if or and else if.
   1101      1.1   fvdl 		 */
   1102      1.1   fvdl 		scope_context = SLIST_FIRST(&scope_stack);
   1103      1.1   fvdl 		last_scope = TAILQ_LAST(&scope_context->inner_scope,
   1104      1.1   fvdl 					scope_tailq);
   1105      1.1   fvdl 		if (last_scope == NULL
   1106      1.1   fvdl 		 || last_scope->type == SCOPE_ELSE) {
   1107      1.1   fvdl 
   1108      1.1   fvdl 			stop("'else' without leading 'if'", EX_DATAERR);
   1109      1.1   fvdl 			/* NOTREACHED */
   1110      1.1   fvdl 		}
   1111      1.1   fvdl 		new_scope = scope_alloc();
   1112      1.1   fvdl 		new_scope->type = SCOPE_ELSE;
   1113      1.1   fvdl 		new_scope->begin_addr = instruction_ptr;
   1114      1.1   fvdl 	}
   1115      1.1   fvdl ;
   1116      1.1   fvdl 
   1117      1.1   fvdl conditional:
   1118      1.1   fvdl 	'}'
   1119      1.1   fvdl 	{
   1120      1.1   fvdl 		scope_t *scope_context;
   1121      1.1   fvdl 
   1122      1.1   fvdl 		scope_context = SLIST_FIRST(&scope_stack);
   1123      1.1   fvdl 		if (scope_context->type == SCOPE_ROOT) {
   1124      1.1   fvdl 			stop("Unexpected '}' encountered", EX_DATAERR);
   1125      1.1   fvdl 			/* NOTREACHED */
   1126      1.1   fvdl 		}
   1127      1.1   fvdl 
   1128      1.1   fvdl 		scope_context->end_addr = instruction_ptr;
   1129      1.1   fvdl 
   1130      1.1   fvdl 		/* Pop the scope */
   1131      1.1   fvdl 		SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
   1132      1.1   fvdl 
   1133      1.1   fvdl 		process_scope(scope_context);
   1134      1.1   fvdl 
   1135      1.1   fvdl 		if (SLIST_FIRST(&scope_stack) == NULL) {
   1136      1.1   fvdl 			stop("Unexpected '}' encountered", EX_DATAERR);
   1137      1.1   fvdl 			/* NOTREACHED */
   1138      1.1   fvdl 		}
   1139      1.1   fvdl 	}
   1140      1.1   fvdl ;
   1141      1.1   fvdl 
   1142      1.1   fvdl f1_opcode:
   1143      1.1   fvdl 	T_AND { $$ = AIC_OP_AND; }
   1144      1.1   fvdl |	T_XOR { $$ = AIC_OP_XOR; }
   1145      1.1   fvdl |	T_ADD { $$ = AIC_OP_ADD; }
   1146      1.1   fvdl |	T_ADC { $$ = AIC_OP_ADC; }
   1147      1.1   fvdl ;
   1148      1.1   fvdl 
   1149      1.1   fvdl code:
   1150      1.1   fvdl 	f1_opcode destination ',' immediate_or_a opt_source ret ';'
   1151      1.1   fvdl 	{
   1152      1.1   fvdl 		format_1_instr($1, &$2, &$4, &$5, $6);
   1153      1.1   fvdl 	}
   1154      1.1   fvdl ;
   1155      1.1   fvdl 
   1156      1.1   fvdl code:
   1157      1.1   fvdl 	T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
   1158      1.1   fvdl 	{
   1159      1.1   fvdl 		format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
   1160      1.1   fvdl 	}
   1161      1.1   fvdl ;
   1162      1.1   fvdl 
   1163      1.1   fvdl code:
   1164      1.1   fvdl 	T_INC destination opt_source ret ';'
   1165      1.1   fvdl 	{
   1166      1.1   fvdl 		expression_t immed;
   1167      1.1   fvdl 
   1168      1.1   fvdl 		make_expression(&immed, 1);
   1169      1.1   fvdl 		format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
   1170      1.1   fvdl 	}
   1171      1.1   fvdl ;
   1172      1.1   fvdl 
   1173      1.1   fvdl code:
   1174      1.1   fvdl 	T_DEC destination opt_source ret ';'
   1175      1.1   fvdl 	{
   1176      1.1   fvdl 		expression_t immed;
   1177      1.1   fvdl 
   1178      1.1   fvdl 		make_expression(&immed, -1);
   1179      1.1   fvdl 		format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
   1180      1.1   fvdl 	}
   1181      1.1   fvdl ;
   1182      1.1   fvdl 
   1183      1.1   fvdl code:
   1184      1.1   fvdl 	T_CLC ret ';'
   1185      1.1   fvdl 	{
   1186      1.1   fvdl 		expression_t immed;
   1187      1.1   fvdl 
   1188      1.1   fvdl 		make_expression(&immed, -1);
   1189      1.1   fvdl 		format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
   1190      1.1   fvdl 	}
   1191      1.1   fvdl |	T_CLC T_MVI destination ',' immediate_or_a ret ';'
   1192      1.1   fvdl 	{
   1193      1.1   fvdl 		format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
   1194      1.1   fvdl 	}
   1195      1.1   fvdl ;
   1196      1.1   fvdl 
   1197      1.1   fvdl code:
   1198      1.1   fvdl 	T_STC ret ';'
   1199      1.1   fvdl 	{
   1200      1.1   fvdl 		expression_t immed;
   1201      1.1   fvdl 
   1202      1.1   fvdl 		make_expression(&immed, 1);
   1203      1.1   fvdl 		format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
   1204      1.1   fvdl 	}
   1205      1.1   fvdl |	T_STC destination ret ';'
   1206      1.1   fvdl 	{
   1207      1.1   fvdl 		expression_t immed;
   1208      1.1   fvdl 
   1209      1.1   fvdl 		make_expression(&immed, 1);
   1210      1.1   fvdl 		format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
   1211      1.1   fvdl 	}
   1212      1.1   fvdl ;
   1213      1.1   fvdl 
   1214      1.1   fvdl code:
   1215      1.1   fvdl 	T_BMOV destination ',' source ',' immediate ret ';'
   1216      1.1   fvdl 	{
   1217      1.1   fvdl 		format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
   1218      1.1   fvdl 	}
   1219      1.1   fvdl ;
   1220      1.1   fvdl 
   1221      1.1   fvdl code:
   1222      1.1   fvdl 	T_MOV destination ',' source ret ';'
   1223      1.1   fvdl 	{
   1224      1.1   fvdl 		expression_t immed;
   1225      1.1   fvdl 
   1226      1.3   fvdl 		make_expression(&immed, 1);
   1227      1.3   fvdl 		format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
   1228      1.3   fvdl 	}
   1229      1.3   fvdl ;
   1230      1.3   fvdl 
   1231      1.3   fvdl code:
   1232      1.3   fvdl 	T_MVI destination ',' immediate ret ';'
   1233      1.3   fvdl 	{
   1234      1.3   fvdl 		if ($4.value == 0
   1235      1.3   fvdl 		 && is_download_const(&$4) == 0) {
   1236      1.3   fvdl 			expression_t immed;
   1237      1.3   fvdl 
   1238      1.3   fvdl 			/*
   1239      1.3   fvdl 			 * Allow move immediates of 0 so that macros,
   1240      1.3   fvdl 			 * that can't know the immediate's value and
   1241      1.3   fvdl 			 * otherwise compensate, still work.
   1242      1.3   fvdl 			 */
   1243      1.3   fvdl 			make_expression(&immed, 1);
   1244      1.3   fvdl 			format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
   1245      1.3   fvdl 		} else {
   1246      1.3   fvdl 			format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
   1247      1.3   fvdl 		}
   1248      1.1   fvdl 	}
   1249      1.1   fvdl ;
   1250      1.1   fvdl 
   1251      1.1   fvdl code:
   1252      1.3   fvdl 	T_NOT destination opt_source ret ';'
   1253      1.1   fvdl 	{
   1254      1.3   fvdl 		expression_t immed;
   1255      1.3   fvdl 
   1256      1.3   fvdl 		make_expression(&immed, 0xff);
   1257      1.3   fvdl 		format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
   1258      1.1   fvdl 	}
   1259      1.1   fvdl ;
   1260      1.1   fvdl 
   1261      1.1   fvdl code:
   1262      1.1   fvdl 	T_CLR destination ret ';'
   1263      1.1   fvdl 	{
   1264      1.1   fvdl 		expression_t immed;
   1265      1.1   fvdl 
   1266      1.1   fvdl 		make_expression(&immed, 0xff);
   1267      1.1   fvdl 		format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
   1268      1.1   fvdl 	}
   1269      1.1   fvdl ;
   1270      1.1   fvdl 
   1271      1.1   fvdl code:
   1272      1.1   fvdl 	T_NOP ret ';'
   1273      1.1   fvdl 	{
   1274      1.1   fvdl 		expression_t immed;
   1275      1.1   fvdl 
   1276      1.1   fvdl 		make_expression(&immed, 0xff);
   1277      1.1   fvdl 		format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
   1278      1.1   fvdl 	}
   1279      1.1   fvdl ;
   1280      1.1   fvdl 
   1281      1.1   fvdl code:
   1282      1.1   fvdl 	T_RET ';'
   1283      1.1   fvdl 	{
   1284      1.1   fvdl 		expression_t immed;
   1285      1.1   fvdl 
   1286      1.1   fvdl 		make_expression(&immed, 0xff);
   1287      1.1   fvdl 		format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
   1288      1.1   fvdl 	}
   1289      1.1   fvdl ;
   1290      1.1   fvdl 
   1291      1.1   fvdl 	/*
   1292      1.1   fvdl 	 * This grammer differs from the one in the aic7xxx
   1293      1.1   fvdl 	 * reference manual since the grammer listed there is
   1294      1.1   fvdl 	 * ambiguous and causes a shift/reduce conflict.
   1295      1.1   fvdl 	 * It also seems more logical as the "immediate"
   1296      1.1   fvdl 	 * argument is listed as the second arg like the
   1297      1.1   fvdl 	 * other formats.
   1298      1.1   fvdl 	 */
   1299      1.1   fvdl 
   1300      1.1   fvdl f2_opcode:
   1301      1.1   fvdl 	T_SHL { $$ = AIC_OP_SHL; }
   1302      1.1   fvdl |	T_SHR { $$ = AIC_OP_SHR; }
   1303      1.1   fvdl |	T_ROL { $$ = AIC_OP_ROL; }
   1304      1.1   fvdl |	T_ROR { $$ = AIC_OP_ROR; }
   1305      1.1   fvdl ;
   1306      1.1   fvdl 
   1307      1.1   fvdl code:
   1308      1.1   fvdl 	f2_opcode destination ',' expression opt_source ret ';'
   1309      1.1   fvdl 	{
   1310      1.1   fvdl 		format_2_instr($1, &$2, &$4, &$5, $6);
   1311      1.1   fvdl 	}
   1312      1.1   fvdl ;
   1313      1.1   fvdl 
   1314      1.1   fvdl jmp_jc_jnc_call:
   1315      1.1   fvdl 	T_JMP	{ $$ = AIC_OP_JMP; }
   1316      1.1   fvdl |	T_JC	{ $$ = AIC_OP_JC; }
   1317      1.1   fvdl |	T_JNC	{ $$ = AIC_OP_JNC; }
   1318      1.1   fvdl |	T_CALL	{ $$ = AIC_OP_CALL; }
   1319      1.1   fvdl ;
   1320      1.1   fvdl 
   1321      1.1   fvdl jz_jnz:
   1322      1.1   fvdl 	T_JZ	{ $$ = AIC_OP_JZ; }
   1323      1.1   fvdl |	T_JNZ	{ $$ = AIC_OP_JNZ; }
   1324      1.1   fvdl ;
   1325      1.1   fvdl 
   1326      1.1   fvdl je_jne:
   1327      1.1   fvdl 	T_JE	{ $$ = AIC_OP_JE; }
   1328      1.1   fvdl |	T_JNE	{ $$ = AIC_OP_JNE; }
   1329      1.1   fvdl ;
   1330      1.1   fvdl 
   1331      1.1   fvdl code:
   1332      1.1   fvdl 	jmp_jc_jnc_call address ';'
   1333      1.1   fvdl 	{
   1334      1.1   fvdl 		expression_t immed;
   1335      1.1   fvdl 
   1336      1.1   fvdl 		make_expression(&immed, 0);
   1337      1.1   fvdl 		format_3_instr($1, &sindex, &immed, &$2);
   1338      1.1   fvdl 	}
   1339      1.1   fvdl ;
   1340      1.1   fvdl 
   1341      1.1   fvdl code:
   1342      1.1   fvdl 	T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
   1343      1.1   fvdl 	{
   1344      1.1   fvdl 		format_3_instr($5, &$2, &$4, &$6);
   1345      1.1   fvdl 	}
   1346      1.1   fvdl ;
   1347      1.1   fvdl 
   1348      1.1   fvdl code:
   1349      1.1   fvdl 	T_TEST source ',' immediate_or_a jz_jnz address ';'
   1350      1.1   fvdl 	{
   1351      1.1   fvdl 		format_3_instr($5, &$2, &$4, &$6);
   1352      1.1   fvdl 	}
   1353      1.1   fvdl ;
   1354      1.1   fvdl 
   1355      1.1   fvdl code:
   1356      1.1   fvdl 	T_CMP source ',' immediate_or_a je_jne address ';'
   1357      1.1   fvdl 	{
   1358      1.1   fvdl 		format_3_instr($5, &$2, &$4, &$6);
   1359      1.1   fvdl 	}
   1360      1.1   fvdl ;
   1361      1.1   fvdl 
   1362      1.1   fvdl code:
   1363      1.1   fvdl 	T_MOV source jmp_jc_jnc_call address ';'
   1364      1.1   fvdl 	{
   1365      1.1   fvdl 		expression_t immed;
   1366      1.1   fvdl 
   1367      1.1   fvdl 		make_expression(&immed, 0);
   1368      1.1   fvdl 		format_3_instr($3, &$2, &immed, &$4);
   1369      1.1   fvdl 	}
   1370      1.1   fvdl ;
   1371      1.1   fvdl 
   1372      1.1   fvdl code:
   1373      1.1   fvdl 	T_MVI immediate jmp_jc_jnc_call address ';'
   1374      1.1   fvdl 	{
   1375      1.1   fvdl 		format_3_instr($3, &allzeros, &$2, &$4);
   1376      1.1   fvdl 	}
   1377      1.1   fvdl ;
   1378      1.1   fvdl 
   1379      1.1   fvdl %%
   1380      1.1   fvdl 
   1381      1.1   fvdl static void
   1382      1.3   fvdl process_field(int field_type, symbol_t *sym, int value)
   1383      1.1   fvdl {
   1384      1.1   fvdl 	/*
   1385      1.1   fvdl 	 * Add the current register to its
   1386      1.1   fvdl 	 * symbol list, if it already exists,
   1387      1.1   fvdl 	 * warn if we are setting it to a
   1388      1.1   fvdl 	 * different value, or in the bit to
   1389      1.1   fvdl 	 * the "allowed bits" of this register.
   1390      1.1   fvdl 	 */
   1391      1.1   fvdl 	if (sym->type == UNINITIALIZED) {
   1392      1.3   fvdl 		sym->type = field_type;
   1393      1.1   fvdl 		initialize_symbol(sym);
   1394      1.3   fvdl 		sym->info.finfo->value = value;
   1395      1.3   fvdl 		if (field_type != ENUM_ENTRY) {
   1396      1.3   fvdl 			if (field_type != MASK && value == 0) {
   1397      1.3   fvdl 				stop("Empty Field, or Enum", EX_DATAERR);
   1398      1.1   fvdl 				/* NOTREACHED */
   1399      1.1   fvdl 			}
   1400      1.3   fvdl 			sym->info.finfo->value = value;
   1401      1.3   fvdl 			sym->info.finfo->mask = value;
   1402      1.3   fvdl 		} else if (field_symbol != NULL) {
   1403      1.3   fvdl 			sym->info.finfo->mask = field_symbol->info.finfo->value;
   1404      1.3   fvdl 		} else {
   1405      1.3   fvdl 			sym->info.finfo->mask = 0xFF;
   1406      1.1   fvdl 		}
   1407      1.3   fvdl 	} else if (sym->type != field_type) {
   1408      1.3   fvdl 		stop("Field definition mirrors a definition of the same "
   1409      1.1   fvdl 		     " name, but a different type", EX_DATAERR);
   1410      1.1   fvdl 		/* NOTREACHED */
   1411      1.3   fvdl 	} else if (value != sym->info.finfo->value) {
   1412      1.3   fvdl 		stop("Field redefined with a conflicting value", EX_DATAERR);
   1413      1.1   fvdl 		/* NOTREACHED */
   1414      1.1   fvdl 	}
   1415      1.1   fvdl 	/* Fail if this symbol is already listed */
   1416      1.3   fvdl 	if (symlist_search(&(sym->info.finfo->symrefs),
   1417      1.1   fvdl 			   cur_symbol->name) != NULL) {
   1418      1.3   fvdl 		stop("Field defined multiple times for register", EX_DATAERR);
   1419      1.1   fvdl 		/* NOTREACHED */
   1420      1.1   fvdl 	}
   1421      1.3   fvdl 	symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
   1422      1.1   fvdl 		    SYMLIST_INSERT_HEAD);
   1423      1.3   fvdl 	cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
   1424      1.1   fvdl 	cur_symbol->info.rinfo->typecheck_masks = TRUE;
   1425      1.3   fvdl 	symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
   1426      1.1   fvdl }
   1427      1.1   fvdl 
   1428      1.1   fvdl static void
   1429      1.3   fvdl initialize_symbol(symbol_t *symbol)
   1430      1.1   fvdl {
   1431      1.1   fvdl 	switch (symbol->type) {
   1432      1.3   fvdl 	case UNINITIALIZED:
   1433      1.1   fvdl 		stop("Call to initialize_symbol with type field unset",
   1434      1.1   fvdl 		     EX_SOFTWARE);
   1435      1.1   fvdl 		/* NOTREACHED */
   1436      1.1   fvdl 		break;
   1437      1.3   fvdl 	case REGISTER:
   1438      1.3   fvdl 	case SRAMLOC:
   1439      1.3   fvdl 	case SCBLOC:
   1440      1.1   fvdl 		symbol->info.rinfo =
   1441      1.1   fvdl 		    (struct reg_info *)malloc(sizeof(struct reg_info));
   1442      1.1   fvdl 		if (symbol->info.rinfo == NULL) {
   1443      1.1   fvdl 			stop("Can't create register info", EX_SOFTWARE);
   1444      1.1   fvdl 			/* NOTREACHED */
   1445      1.1   fvdl 		}
   1446      1.1   fvdl 		memset(symbol->info.rinfo, 0,
   1447      1.1   fvdl 		       sizeof(struct reg_info));
   1448      1.3   fvdl 		SLIST_INIT(&(symbol->info.rinfo->fields));
   1449      1.3   fvdl 		/*
   1450      1.3   fvdl 		 * Default to allowing access in all register modes
   1451      1.3   fvdl 		 * or to the mode specified by the SCB or SRAM space
   1452      1.3   fvdl 		 * we are in.
   1453      1.3   fvdl 		 */
   1454      1.3   fvdl 		if (scb_or_sram_symbol != NULL)
   1455      1.3   fvdl 			symbol->info.rinfo->modes =
   1456      1.3   fvdl 			    scb_or_sram_symbol->info.rinfo->modes;
   1457      1.3   fvdl 		else
   1458      1.3   fvdl 			symbol->info.rinfo->modes = ~0;
   1459      1.1   fvdl 		break;
   1460      1.3   fvdl 	case ALIAS:
   1461      1.1   fvdl 		symbol->info.ainfo =
   1462      1.1   fvdl 		    (struct alias_info *)malloc(sizeof(struct alias_info));
   1463      1.1   fvdl 		if (symbol->info.ainfo == NULL) {
   1464      1.1   fvdl 			stop("Can't create alias info", EX_SOFTWARE);
   1465      1.1   fvdl 			/* NOTREACHED */
   1466      1.1   fvdl 		}
   1467      1.1   fvdl 		memset(symbol->info.ainfo, 0,
   1468      1.1   fvdl 		       sizeof(struct alias_info));
   1469      1.1   fvdl 		break;
   1470      1.3   fvdl 	case MASK:
   1471      1.3   fvdl 	case FIELD:
   1472      1.3   fvdl 	case ENUM:
   1473      1.3   fvdl 	case ENUM_ENTRY:
   1474      1.3   fvdl 		symbol->info.finfo =
   1475      1.3   fvdl 		    (struct field_info *)malloc(sizeof(struct field_info));
   1476      1.3   fvdl 		if (symbol->info.finfo == NULL) {
   1477      1.3   fvdl 			stop("Can't create field info", EX_SOFTWARE);
   1478      1.1   fvdl 			/* NOTREACHED */
   1479      1.1   fvdl 		}
   1480      1.3   fvdl 		memset(symbol->info.finfo, 0, sizeof(struct field_info));
   1481      1.3   fvdl 		SLIST_INIT(&(symbol->info.finfo->symrefs));
   1482      1.1   fvdl 		break;
   1483      1.3   fvdl 	case CONST:
   1484      1.3   fvdl 	case DOWNLOAD_CONST:
   1485      1.1   fvdl 		symbol->info.cinfo =
   1486      1.1   fvdl 		    (struct const_info *)malloc(sizeof(struct const_info));
   1487      1.1   fvdl 		if (symbol->info.cinfo == NULL) {
   1488      1.1   fvdl 			stop("Can't create alias info", EX_SOFTWARE);
   1489      1.1   fvdl 			/* NOTREACHED */
   1490      1.1   fvdl 		}
   1491      1.1   fvdl 		memset(symbol->info.cinfo, 0,
   1492      1.1   fvdl 		       sizeof(struct const_info));
   1493      1.1   fvdl 		break;
   1494      1.1   fvdl 	case LABEL:
   1495      1.1   fvdl 		symbol->info.linfo =
   1496      1.1   fvdl 		    (struct label_info *)malloc(sizeof(struct label_info));
   1497      1.1   fvdl 		if (symbol->info.linfo == NULL) {
   1498      1.1   fvdl 			stop("Can't create label info", EX_SOFTWARE);
   1499      1.1   fvdl 			/* NOTREACHED */
   1500      1.1   fvdl 		}
   1501      1.1   fvdl 		memset(symbol->info.linfo, 0,
   1502      1.1   fvdl 		       sizeof(struct label_info));
   1503      1.1   fvdl 		break;
   1504      1.1   fvdl 	case CONDITIONAL:
   1505      1.1   fvdl 		symbol->info.condinfo =
   1506      1.1   fvdl 		    (struct cond_info *)malloc(sizeof(struct cond_info));
   1507      1.1   fvdl 		if (symbol->info.condinfo == NULL) {
   1508      1.1   fvdl 			stop("Can't create conditional info", EX_SOFTWARE);
   1509      1.1   fvdl 			/* NOTREACHED */
   1510      1.1   fvdl 		}
   1511      1.1   fvdl 		memset(symbol->info.condinfo, 0,
   1512      1.1   fvdl 		       sizeof(struct cond_info));
   1513      1.1   fvdl 		break;
   1514      1.3   fvdl 	case MACRO:
   1515      1.4  perry 		symbol->info.macroinfo =
   1516      1.3   fvdl 		    (struct macro_info *)malloc(sizeof(struct macro_info));
   1517      1.3   fvdl 		if (symbol->info.macroinfo == NULL) {
   1518      1.3   fvdl 			stop("Can't create macro info", EX_SOFTWARE);
   1519      1.3   fvdl 			/* NOTREACHED */
   1520      1.3   fvdl 		}
   1521      1.3   fvdl 		memset(symbol->info.macroinfo, 0,
   1522      1.3   fvdl 		       sizeof(struct macro_info));
   1523      1.3   fvdl 		STAILQ_INIT(&symbol->info.macroinfo->args);
   1524      1.3   fvdl 		break;
   1525      1.1   fvdl 	default:
   1526      1.1   fvdl 		stop("Call to initialize_symbol with invalid symbol type",
   1527      1.1   fvdl 		     EX_SOFTWARE);
   1528      1.1   fvdl 		/* NOTREACHED */
   1529      1.1   fvdl 		break;
   1530      1.1   fvdl 	}
   1531      1.1   fvdl }
   1532      1.1   fvdl 
   1533      1.1   fvdl static void
   1534      1.3   fvdl add_macro_arg(const char *argtext, int argnum)
   1535      1.3   fvdl {
   1536      1.3   fvdl 	struct macro_arg *marg;
   1537      1.3   fvdl 	int i;
   1538      1.3   fvdl 	int retval;
   1539      1.4  perry 
   1540      1.3   fvdl 
   1541      1.3   fvdl 	if (cur_symbol == NULL || cur_symbol->type != MACRO) {
   1542      1.3   fvdl 		stop("Invalid current symbol for adding macro arg",
   1543      1.3   fvdl 		     EX_SOFTWARE);
   1544      1.3   fvdl 		/* NOTREACHED */
   1545      1.3   fvdl 	}
   1546      1.3   fvdl 
   1547      1.3   fvdl 	marg = (struct macro_arg *)malloc(sizeof(*marg));
   1548      1.3   fvdl 	if (marg == NULL) {
   1549      1.3   fvdl 		stop("Can't create macro_arg structure", EX_SOFTWARE);
   1550      1.3   fvdl 		/* NOTREACHED */
   1551      1.3   fvdl 	}
   1552      1.3   fvdl 	marg->replacement_text = NULL;
   1553      1.3   fvdl 	retval = snprintf(regex_pattern, sizeof(regex_pattern),
   1554      1.3   fvdl 			  "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
   1555      1.3   fvdl 			  argtext);
   1556      1.3   fvdl 	if (retval >= sizeof(regex_pattern)) {
   1557      1.3   fvdl 		stop("Regex text buffer too small for arg",
   1558      1.3   fvdl 		     EX_SOFTWARE);
   1559      1.3   fvdl 		/* NOTREACHED */
   1560      1.3   fvdl 	}
   1561      1.3   fvdl 	retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
   1562      1.3   fvdl 	if (retval != 0) {
   1563      1.3   fvdl 		stop("Regex compilation failed", EX_SOFTWARE);
   1564      1.3   fvdl 		/* NOTREACHED */
   1565      1.3   fvdl 	}
   1566      1.3   fvdl 	STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
   1567      1.3   fvdl }
   1568      1.3   fvdl 
   1569      1.3   fvdl static void
   1570      1.3   fvdl add_macro_body(const char *bodytext)
   1571      1.3   fvdl {
   1572      1.3   fvdl 	if (cur_symbol == NULL || cur_symbol->type != MACRO) {
   1573      1.3   fvdl 		stop("Invalid current symbol for adding macro arg",
   1574      1.3   fvdl 		     EX_SOFTWARE);
   1575      1.3   fvdl 		/* NOTREACHED */
   1576      1.3   fvdl 	}
   1577      1.3   fvdl 	cur_symbol->info.macroinfo->body = strdup(bodytext);
   1578      1.3   fvdl 	if (cur_symbol->info.macroinfo->body == NULL) {
   1579      1.3   fvdl 		stop("Can't duplicate macro body text", EX_SOFTWARE);
   1580      1.3   fvdl 		/* NOTREACHED */
   1581      1.3   fvdl 	}
   1582      1.3   fvdl }
   1583      1.3   fvdl 
   1584      1.3   fvdl static void
   1585      1.3   fvdl process_register(symbol_t **p_symbol)
   1586      1.1   fvdl {
   1587      1.1   fvdl 	symbol_t *symbol = *p_symbol;
   1588      1.1   fvdl 
   1589      1.1   fvdl 	if (symbol->type == UNINITIALIZED) {
   1590      1.3   fvdl 		snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
   1591      1.1   fvdl 			 symbol->name);
   1592      1.3   fvdl 		stop(errbuf, EX_DATAERR);
   1593      1.1   fvdl 		/* NOTREACHED */
   1594      1.1   fvdl 	} else if (symbol->type == ALIAS) {
   1595      1.1   fvdl 		*p_symbol = symbol->info.ainfo->parent;
   1596      1.1   fvdl 	} else if ((symbol->type != REGISTER)
   1597      1.1   fvdl 		&& (symbol->type != SCBLOC)
   1598      1.1   fvdl 		&& (symbol->type != SRAMLOC)) {
   1599      1.3   fvdl 		snprintf(errbuf, sizeof(errbuf),
   1600      1.1   fvdl 			 "Specified symbol %s is not a register",
   1601      1.1   fvdl 			 symbol->name);
   1602      1.3   fvdl 		stop(errbuf, EX_DATAERR);
   1603      1.1   fvdl 	}
   1604      1.1   fvdl }
   1605      1.1   fvdl 
   1606      1.1   fvdl static void
   1607      1.3   fvdl format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
   1608      1.3   fvdl 	       symbol_ref_t *src, int ret)
   1609      1.1   fvdl {
   1610      1.1   fvdl 	struct instruction *instr;
   1611      1.1   fvdl 	struct ins_format1 *f1_instr;
   1612      1.1   fvdl 
   1613      1.1   fvdl 	if (src->symbol == NULL)
   1614      1.1   fvdl 		src = dest;
   1615      1.1   fvdl 
   1616      1.1   fvdl 	/* Test register permissions */
   1617      1.1   fvdl 	test_writable_symbol(dest->symbol);
   1618      1.1   fvdl 	test_readable_symbol(src->symbol);
   1619      1.1   fvdl 
   1620      1.1   fvdl 	/* Ensure that immediate makes sense for this destination */
   1621      1.1   fvdl 	type_check(dest->symbol, immed, opcode);
   1622      1.1   fvdl 
   1623      1.1   fvdl 	/* Allocate sequencer space for the instruction and fill it out */
   1624      1.1   fvdl 	instr = seq_alloc();
   1625      1.1   fvdl 	f1_instr = &instr->format.format1;
   1626      1.1   fvdl 	f1_instr->ret = ret ? 1 : 0;
   1627      1.1   fvdl 	f1_instr->opcode = opcode;
   1628      1.1   fvdl 	f1_instr->destination = dest->symbol->info.rinfo->address
   1629      1.1   fvdl 			      + dest->offset;
   1630      1.1   fvdl 	f1_instr->source = src->symbol->info.rinfo->address
   1631      1.1   fvdl 			 + src->offset;
   1632      1.1   fvdl 	f1_instr->immediate = immed->value;
   1633      1.1   fvdl 
   1634      1.1   fvdl 	if (is_download_const(immed))
   1635      1.1   fvdl 		f1_instr->parity = 1;
   1636      1.3   fvdl 	else if (dest->symbol == mode_ptr.symbol) {
   1637      1.3   fvdl 		u_int src_value;
   1638      1.3   fvdl 		u_int dst_value;
   1639      1.3   fvdl 
   1640      1.3   fvdl 		/*
   1641      1.3   fvdl 		 * Attempt to update mode information if
   1642      1.3   fvdl 		 * we are operating on the mode register.
   1643      1.3   fvdl 		 */
   1644      1.3   fvdl 		if (src->symbol == allones.symbol)
   1645      1.3   fvdl 			src_value = 0xFF;
   1646      1.3   fvdl 		else if (src->symbol == allzeros.symbol)
   1647      1.3   fvdl 			src_value = 0;
   1648      1.3   fvdl 		else if (src->symbol == mode_ptr.symbol)
   1649      1.3   fvdl 			src_value = (dst_mode << 4) | src_mode;
   1650      1.3   fvdl 		else
   1651      1.3   fvdl 			goto cant_update;
   1652      1.1   fvdl 
   1653      1.3   fvdl 		switch (opcode) {
   1654      1.3   fvdl 		case AIC_OP_AND:
   1655      1.3   fvdl 			dst_value = src_value & immed->value;
   1656      1.3   fvdl 			break;
   1657      1.3   fvdl 		case AIC_OP_XOR:
   1658      1.3   fvdl 			dst_value = src_value ^ immed->value;
   1659      1.3   fvdl 			break;
   1660      1.3   fvdl 		case AIC_OP_ADD:
   1661      1.3   fvdl 			dst_value = (src_value + immed->value) & 0xFF;
   1662      1.3   fvdl 			break;
   1663      1.3   fvdl 		case AIC_OP_OR:
   1664      1.3   fvdl 			dst_value = src_value | immed->value;
   1665      1.3   fvdl 			break;
   1666      1.3   fvdl 		case AIC_OP_BMOV:
   1667      1.3   fvdl 			dst_value = src_value;
   1668      1.3   fvdl 			break;
   1669      1.3   fvdl 		default:
   1670      1.3   fvdl 			goto cant_update;
   1671      1.3   fvdl 		}
   1672      1.3   fvdl 		src_mode = dst_value & 0xF;
   1673      1.3   fvdl 		dst_mode = (dst_value >> 4) & 0xF;
   1674      1.3   fvdl 	}
   1675      1.3   fvdl 
   1676      1.3   fvdl cant_update:
   1677      1.1   fvdl 	symlist_free(&immed->referenced_syms);
   1678      1.1   fvdl 	instruction_ptr++;
   1679      1.1   fvdl }
   1680      1.1   fvdl 
   1681      1.1   fvdl static void
   1682      1.3   fvdl format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
   1683      1.3   fvdl 	       symbol_ref_t *src, int ret)
   1684      1.1   fvdl {
   1685      1.1   fvdl 	struct instruction *instr;
   1686      1.1   fvdl 	struct ins_format2 *f2_instr;
   1687      1.3   fvdl 	uint8_t shift_control;
   1688      1.1   fvdl 
   1689      1.1   fvdl 	if (src->symbol == NULL)
   1690      1.1   fvdl 		src = dest;
   1691      1.1   fvdl 
   1692      1.1   fvdl 	/* Test register permissions */
   1693      1.1   fvdl 	test_writable_symbol(dest->symbol);
   1694      1.1   fvdl 	test_readable_symbol(src->symbol);
   1695      1.1   fvdl 
   1696      1.1   fvdl 	/* Allocate sequencer space for the instruction and fill it out */
   1697      1.1   fvdl 	instr = seq_alloc();
   1698      1.1   fvdl 	f2_instr = &instr->format.format2;
   1699      1.1   fvdl 	f2_instr->ret = ret ? 1 : 0;
   1700      1.1   fvdl 	f2_instr->opcode = AIC_OP_ROL;
   1701      1.1   fvdl 	f2_instr->destination = dest->symbol->info.rinfo->address
   1702      1.1   fvdl 			      + dest->offset;
   1703      1.1   fvdl 	f2_instr->source = src->symbol->info.rinfo->address
   1704      1.1   fvdl 			 + src->offset;
   1705      1.1   fvdl 	if (places->value > 8 || places->value <= 0) {
   1706      1.1   fvdl 		stop("illegal shift value", EX_DATAERR);
   1707      1.1   fvdl 		/* NOTREACHED */
   1708      1.1   fvdl 	}
   1709      1.1   fvdl 	switch (opcode) {
   1710      1.1   fvdl 	case AIC_OP_SHL:
   1711      1.1   fvdl 		if (places->value == 8)
   1712      1.1   fvdl 			shift_control = 0xf0;
   1713      1.1   fvdl 		else
   1714      1.1   fvdl 			shift_control = (places->value << 4) | places->value;
   1715      1.1   fvdl 		break;
   1716      1.1   fvdl 	case AIC_OP_SHR:
   1717      1.1   fvdl 		if (places->value == 8) {
   1718      1.1   fvdl 			shift_control = 0xf8;
   1719      1.1   fvdl 		} else {
   1720      1.1   fvdl 			shift_control = (places->value << 4)
   1721      1.1   fvdl 				      | (8 - places->value)
   1722      1.1   fvdl 				      | 0x08;
   1723      1.1   fvdl 		}
   1724      1.1   fvdl 		break;
   1725      1.1   fvdl 	case AIC_OP_ROL:
   1726      1.1   fvdl 		shift_control = places->value & 0x7;
   1727      1.1   fvdl 		break;
   1728      1.1   fvdl 	case AIC_OP_ROR:
   1729      1.1   fvdl 		shift_control = (8 - places->value) | 0x08;
   1730      1.1   fvdl 		break;
   1731      1.1   fvdl 	default:
   1732      1.1   fvdl 		shift_control = 0; /* Quiet Compiler */
   1733      1.1   fvdl 		stop("Invalid shift operation specified", EX_SOFTWARE);
   1734      1.1   fvdl 		/* NOTREACHED */
   1735      1.1   fvdl 		break;
   1736      1.1   fvdl 	};
   1737      1.1   fvdl 	f2_instr->shift_control = shift_control;
   1738      1.1   fvdl 	symlist_free(&places->referenced_syms);
   1739      1.1   fvdl 	instruction_ptr++;
   1740      1.1   fvdl }
   1741      1.1   fvdl 
   1742      1.1   fvdl static void
   1743      1.3   fvdl format_3_instr(int opcode, symbol_ref_t *src,
   1744      1.3   fvdl 	       expression_t *immed, symbol_ref_t *address)
   1745      1.1   fvdl {
   1746      1.1   fvdl 	struct instruction *instr;
   1747      1.1   fvdl 	struct ins_format3 *f3_instr;
   1748      1.1   fvdl 	int addr;
   1749      1.1   fvdl 
   1750      1.1   fvdl 	/* Test register permissions */
   1751      1.1   fvdl 	test_readable_symbol(src->symbol);
   1752      1.1   fvdl 
   1753      1.1   fvdl 	/* Ensure that immediate makes sense for this source */
   1754      1.1   fvdl 	type_check(src->symbol, immed, opcode);
   1755      1.1   fvdl 
   1756      1.1   fvdl 	/* Allocate sequencer space for the instruction and fill it out */
   1757      1.1   fvdl 	instr = seq_alloc();
   1758      1.1   fvdl 	f3_instr = &instr->format.format3;
   1759      1.1   fvdl 	if (address->symbol == NULL) {
   1760      1.1   fvdl 		/* 'dot' referrence.  Use the current instruction pointer */
   1761      1.1   fvdl 		addr = instruction_ptr + address->offset;
   1762      1.1   fvdl 	} else if (address->symbol->type == UNINITIALIZED) {
   1763      1.1   fvdl 		/* forward reference */
   1764      1.1   fvdl 		addr = address->offset;
   1765      1.1   fvdl 		instr->patch_label = address->symbol;
   1766      1.1   fvdl 	} else
   1767      1.1   fvdl 		addr = address->symbol->info.linfo->address + address->offset;
   1768      1.1   fvdl 	f3_instr->opcode = opcode;
   1769      1.1   fvdl 	f3_instr->address = addr;
   1770      1.1   fvdl 	f3_instr->source = src->symbol->info.rinfo->address
   1771      1.1   fvdl 			 + src->offset;
   1772      1.1   fvdl 	f3_instr->immediate = immed->value;
   1773      1.1   fvdl 
   1774      1.1   fvdl 	if (is_download_const(immed))
   1775      1.1   fvdl 		f3_instr->parity = 1;
   1776      1.1   fvdl 
   1777      1.1   fvdl 	symlist_free(&immed->referenced_syms);
   1778      1.1   fvdl 	instruction_ptr++;
   1779      1.1   fvdl }
   1780      1.1   fvdl 
   1781      1.1   fvdl static void
   1782      1.3   fvdl test_readable_symbol(symbol_t *symbol)
   1783      1.1   fvdl {
   1784      1.4  perry 
   1785      1.3   fvdl 	if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
   1786      1.3   fvdl 		snprintf(errbuf, sizeof(errbuf),
   1787      1.3   fvdl 			"Register %s unavailable in source reg mode %d",
   1788      1.3   fvdl 			symbol->name, src_mode);
   1789      1.3   fvdl 		stop(errbuf, EX_DATAERR);
   1790      1.3   fvdl 	}
   1791      1.3   fvdl 
   1792      1.1   fvdl 	if (symbol->info.rinfo->mode == WO) {
   1793      1.1   fvdl 		stop("Write Only register specified as source",
   1794      1.1   fvdl 		     EX_DATAERR);
   1795      1.1   fvdl 		/* NOTREACHED */
   1796      1.1   fvdl 	}
   1797      1.1   fvdl }
   1798      1.1   fvdl 
   1799      1.1   fvdl static void
   1800      1.3   fvdl test_writable_symbol(symbol_t *symbol)
   1801      1.1   fvdl {
   1802      1.4  perry 
   1803      1.3   fvdl 	if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
   1804      1.3   fvdl 		snprintf(errbuf, sizeof(errbuf),
   1805      1.3   fvdl 			"Register %s unavailable in destination reg mode %d",
   1806      1.3   fvdl 			symbol->name, dst_mode);
   1807      1.3   fvdl 		stop(errbuf, EX_DATAERR);
   1808      1.3   fvdl 	}
   1809      1.3   fvdl 
   1810      1.1   fvdl 	if (symbol->info.rinfo->mode == RO) {
   1811      1.1   fvdl 		stop("Read Only register specified as destination",
   1812      1.1   fvdl 		     EX_DATAERR);
   1813      1.1   fvdl 		/* NOTREACHED */
   1814      1.1   fvdl 	}
   1815      1.1   fvdl }
   1816      1.1   fvdl 
   1817      1.1   fvdl static void
   1818      1.3   fvdl type_check(symbol_t *symbol, expression_t *expression, int opcode)
   1819      1.1   fvdl {
   1820      1.1   fvdl 	symbol_node_t *node;
   1821      1.1   fvdl 	int and_op;
   1822      1.1   fvdl 
   1823      1.1   fvdl 	and_op = FALSE;
   1824      1.1   fvdl 	if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || AIC_OP_JZ)
   1825      1.1   fvdl 		and_op = TRUE;
   1826      1.1   fvdl 
   1827      1.1   fvdl 	/*
   1828      1.1   fvdl 	 * Make sure that we aren't attempting to write something
   1829      1.1   fvdl 	 * that hasn't been defined.  If this is an and operation,
   1830      1.1   fvdl 	 * this is a mask, so "undefined" bits are okay.
   1831      1.1   fvdl 	 */
   1832      1.1   fvdl 	if (and_op == FALSE
   1833      1.1   fvdl 	 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
   1834      1.3   fvdl 		snprintf(errbuf, sizeof(errbuf),
   1835      1.1   fvdl 			 "Invalid bit(s) 0x%x in immediate written to %s",
   1836      1.1   fvdl 			 expression->value & ~symbol->info.rinfo->valid_bitmask,
   1837      1.1   fvdl 			 symbol->name);
   1838      1.3   fvdl 		stop(errbuf, EX_DATAERR);
   1839      1.1   fvdl 		/* NOTREACHED */
   1840      1.1   fvdl 	}
   1841      1.1   fvdl 
   1842      1.1   fvdl 	/*
   1843      1.1   fvdl 	 * Now make sure that all of the symbols referenced by the
   1844      1.1   fvdl 	 * expression are defined for this register.
   1845      1.1   fvdl 	 */
   1846      1.3   fvdl 	if (symbol->info.rinfo->typecheck_masks != FALSE) {
   1847      1.3   fvdl 		for(node = expression->referenced_syms.slh_first;
   1848      1.3   fvdl 		    node != NULL;
   1849      1.3   fvdl 		    node = node->links.sle_next) {
   1850      1.1   fvdl 			if ((node->symbol->type == MASK
   1851      1.3   fvdl 			  || node->symbol->type == FIELD
   1852      1.3   fvdl 			  || node->symbol->type == ENUM
   1853      1.3   fvdl 			  || node->symbol->type == ENUM_ENTRY)
   1854      1.3   fvdl 			 && symlist_search(&node->symbol->info.finfo->symrefs,
   1855      1.1   fvdl 					   symbol->name) == NULL) {
   1856      1.3   fvdl 				snprintf(errbuf, sizeof(errbuf),
   1857      1.3   fvdl 					 "Invalid field or mask %s "
   1858      1.1   fvdl 					 "for register %s",
   1859      1.1   fvdl 					 node->symbol->name, symbol->name);
   1860      1.3   fvdl 				stop(errbuf, EX_DATAERR);
   1861      1.1   fvdl 				/* NOTREACHED */
   1862      1.1   fvdl 			}
   1863      1.1   fvdl 		}
   1864      1.1   fvdl 	}
   1865      1.1   fvdl }
   1866      1.1   fvdl 
   1867      1.1   fvdl static void
   1868      1.3   fvdl make_expression(expression_t *immed, int value)
   1869      1.1   fvdl {
   1870      1.1   fvdl 	SLIST_INIT(&immed->referenced_syms);
   1871      1.1   fvdl 	immed->value = value & 0xff;
   1872      1.1   fvdl }
   1873      1.1   fvdl 
   1874      1.1   fvdl static void
   1875      1.3   fvdl add_conditional(symbol_t *symbol)
   1876      1.1   fvdl {
   1877      1.1   fvdl 	static int numfuncs;
   1878      1.1   fvdl 
   1879      1.1   fvdl 	if (numfuncs == 0) {
   1880      1.1   fvdl 		/* add a special conditional, "0" */
   1881      1.1   fvdl 		symbol_t *false_func;
   1882      1.1   fvdl 
   1883      1.1   fvdl 		false_func = symtable_get("0");
   1884      1.1   fvdl 		if (false_func->type != UNINITIALIZED) {
   1885      1.1   fvdl 			stop("Conditional expression '0' "
   1886      1.1   fvdl 			     "conflicts with a symbol", EX_DATAERR);
   1887      1.1   fvdl 			/* NOTREACHED */
   1888      1.1   fvdl 		}
   1889      1.1   fvdl 		false_func->type = CONDITIONAL;
   1890      1.1   fvdl 		initialize_symbol(false_func);
   1891      1.1   fvdl 		false_func->info.condinfo->func_num = numfuncs++;
   1892      1.1   fvdl 		symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
   1893      1.1   fvdl 	}
   1894      1.1   fvdl 
   1895      1.1   fvdl 	/* This condition has occurred before */
   1896      1.1   fvdl 	if (symbol->type == CONDITIONAL)
   1897      1.1   fvdl 		return;
   1898      1.1   fvdl 
   1899      1.1   fvdl 	if (symbol->type != UNINITIALIZED) {
   1900      1.1   fvdl 		stop("Conditional expression conflicts with a symbol",
   1901      1.1   fvdl 		     EX_DATAERR);
   1902      1.1   fvdl 		/* NOTREACHED */
   1903      1.1   fvdl 	}
   1904      1.1   fvdl 
   1905      1.1   fvdl 	symbol->type = CONDITIONAL;
   1906      1.1   fvdl 	initialize_symbol(symbol);
   1907      1.1   fvdl 	symbol->info.condinfo->func_num = numfuncs++;
   1908      1.1   fvdl 	symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
   1909      1.1   fvdl }
   1910      1.1   fvdl 
   1911      1.3   fvdl static void
   1912      1.3   fvdl add_version(const char *verstring)
   1913      1.3   fvdl {
   1914      1.3   fvdl 	const char prefix[] = " * ";
   1915      1.3   fvdl 	int newlen;
   1916      1.3   fvdl 	int oldlen;
   1917      1.3   fvdl 
   1918      1.3   fvdl 	newlen = strlen(verstring) + strlen(prefix);
   1919      1.3   fvdl 	oldlen = 0;
   1920      1.3   fvdl 	if (versions != NULL)
   1921      1.3   fvdl 		oldlen = strlen(versions);
   1922      1.3   fvdl 	versions = realloc(versions, newlen + oldlen + 2);
   1923      1.3   fvdl 	if (versions == NULL)
   1924      1.3   fvdl 		stop("Can't allocate version string", EX_SOFTWARE);
   1925      1.3   fvdl 	strcpy(&versions[oldlen], prefix);
   1926      1.3   fvdl 	strcpy(&versions[oldlen + strlen(prefix)], verstring);
   1927      1.3   fvdl 	versions[newlen + oldlen] = '\n';
   1928      1.3   fvdl 	versions[newlen + oldlen + 1] = '\0';
   1929      1.3   fvdl }
   1930      1.3   fvdl 
   1931      1.1   fvdl void
   1932      1.3   fvdl yyerror(const char *string)
   1933      1.1   fvdl {
   1934      1.1   fvdl 	stop(string, EX_DATAERR);
   1935      1.1   fvdl }
   1936      1.1   fvdl 
   1937      1.1   fvdl static int
   1938      1.3   fvdl is_download_const(expression_t *immed)
   1939      1.1   fvdl {
   1940      1.3   fvdl 	if ((immed->referenced_syms.slh_first != NULL)
   1941      1.3   fvdl 	 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
   1942      1.1   fvdl 		return (TRUE);
   1943      1.1   fvdl 
   1944      1.1   fvdl 	return (FALSE);
   1945      1.1   fvdl }
   1946