3 * Parser for the Aic7xxx SCSI Host adapter sequencer assembler.
5 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
6 * Copyright (c) 2001, 2002 Adaptec Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
20 * 3. Neither the names of the above-listed copyright holders nor the names
21 * of any contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * Alternatively, this software may be distributed under the terms of the
25 * GNU General Public License ("GPL") version 2 as published by the Free
26 * Software Foundation.
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
38 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
39 * POSSIBILITY OF SUCH DAMAGES.
41 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_gram.y#30 $
46 #include <sys/types.h>
58 #include <sys/queue.h>
62 #include "aicasm_symbol.h"
63 #include "aicasm_insformat.h"
67 char stock_prefix[] = "aic_";
68 char *prefix = stock_prefix;
71 static char errbuf[255];
72 static char regex_pattern[255];
73 static symbol_t *cur_symbol;
74 static symbol_t *field_symbol;
75 static symbol_t *scb_or_sram_symbol;
76 static symtype cur_symtype;
77 static symbol_ref_t accumulator;
78 static symbol_ref_t mode_ptr;
79 static symbol_ref_t allones;
80 static symbol_ref_t allzeros;
81 static symbol_ref_t none;
82 static symbol_ref_t sindex;
83 static int instruction_ptr;
85 static int sram_or_scb_offset;
86 static int download_constant_count;
87 static int in_critical_section;
88 static u_int enum_increment;
89 static u_int enum_next_value;
91 static void process_field(int field_type, symbol_t *sym, int mask);
92 static void initialize_symbol(symbol_t *symbol);
93 static void add_macro_arg(const char *argtext, int position);
94 static void add_macro_body(const char *bodytext);
95 static void process_register(symbol_t **p_symbol);
96 static void format_1_instr(int opcode, symbol_ref_t *dest,
97 expression_t *immed, symbol_ref_t *src, int ret);
98 static void format_2_instr(int opcode, symbol_ref_t *dest,
99 expression_t *places, symbol_ref_t *src, int ret);
100 static void format_3_instr(int opcode, symbol_ref_t *src,
101 expression_t *immed, symbol_ref_t *address);
102 static void test_readable_symbol(symbol_t *symbol);
103 static void test_writable_symbol(symbol_t *symbol);
104 static void type_check(symbol_ref_t *sym, expression_t *expression, int and_op);
105 static void make_expression(expression_t *immed, int value);
106 static void add_conditional(symbol_t *symbol);
107 static void add_version(const char *verstring);
108 static int is_download_const(expression_t *immed);
109 static int is_location_address(symbol_t *symbol);
110 void yyerror(const char *string);
112 #define SRAM_SYMNAME "SRAM_BASE"
113 #define SCB_SYMNAME "SCB_BASE"
120 symbol_ref_t sym_ref;
121 expression_t expression;
126 %token <value> T_CONST
144 %token <value> T_ADDRESS
152 %token T_SET_SRC_MODE
154 %token T_SET_DST_MODE
156 %token <value> T_MODE
170 %token <value> T_NUMBER
172 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
176 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
178 %token <value> T_SHR T_SHL T_ROR T_ROL
180 %token <value> T_MVI T_MOV T_CLR T_BMOV
182 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
184 %token <value> T_ADD T_ADC
186 %token <value> T_INC T_DEC
188 %token <value> T_STC T_CLC
190 %token <value> T_CMP T_NOT T_XOR
192 %token <value> T_TEST T_AND
196 /* 16 bit extensions, not implemented
197 * %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
198 * %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
204 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
208 %token <sym> T_SYMBOL
212 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
214 %type <sym_ref> reg_symbol address destination source opt_source
216 %type <expression> expression immediate immediate_or_a
218 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
220 %type <value> mode_value mode_list macro_arglist
224 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
237 | program patch_arg_list
247 | program scratch_ram
253 | program set_src_mode
255 | program set_dst_mode
256 | critical_section_start
257 | program critical_section_start
258 | critical_section_end
259 | program critical_section_end
261 | program conditional
267 T_INCLUDE '<' T_PATH '>'
269 include_file($3, BRACKETED_INCLUDE);
271 | T_INCLUDE '"' T_PATH '"'
273 include_file($3, QUOTED_INCLUDE);
278 T_PREFIX '=' T_STRING
280 if (prefix != stock_prefix)
281 stop("Prefix multiply defined",
285 stop("Unable to record prefix", EX_SOFTWARE);
290 T_PATCH_ARG_LIST '=' T_STRING
292 if (patch_arg_list != NULL)
293 stop("Patch argument list multiply defined",
295 patch_arg_list = strdup($3);
296 if (patch_arg_list == NULL)
297 stop("Unable to record patch arg list", EX_SOFTWARE);
302 T_VERSION '=' T_STRING
307 T_REGISTER { cur_symtype = REGISTER; } reg_definition
313 if ($1->type != UNINITIALIZED) {
314 stop("Register multiply defined", EX_DATAERR);
318 cur_symbol->type = cur_symtype;
319 initialize_symbol(cur_symbol);
325 * Default to allowing everything in for registers
326 * with no bit or mask definitions.
328 if (cur_symbol->info.rinfo->valid_bitmask == 0)
329 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
331 if (cur_symbol->info.rinfo->size == 0)
332 cur_symbol->info.rinfo->size = 1;
335 * This might be useful for registers too.
337 if (cur_symbol->type != REGISTER) {
338 if (cur_symbol->info.rinfo->address == 0)
339 cur_symbol->info.rinfo->address =
341 sram_or_scb_offset +=
342 cur_symbol->info.rinfo->size;
350 | reg_attribute_list reg_attribute
373 cur_symbol->info.rinfo->address = $2;
380 cur_symbol->info.rinfo->size = $2;
381 if (scb_or_sram_symbol != NULL) {
385 max_addr = scb_or_sram_symbol->info.rinfo->address
386 + scb_or_sram_symbol->info.rinfo->size;
387 sym_max_addr = cur_symbol->info.rinfo->address
388 + cur_symbol->info.rinfo->size;
390 if (sym_max_addr > max_addr)
391 stop("SCB or SRAM space exhausted", EX_DATAERR);
399 cur_symbol->info.rinfo->mode = $2;
406 cur_symbol->info.rinfo->modes = $2;
415 | mode_list ',' mode_value
425 stop("Valid register modes range between 0 and 4.",
437 if (symbol->type != CONST) {
438 stop("Only \"const\" symbols allowed in "
439 "mode definitions.", EX_DATAERR);
442 if (symbol->info.cinfo->value > 4) {
443 stop("Valid register modes range between 0 and 4.",
447 $$ = (0x1 << symbol->info.cinfo->value);
458 '{' enum_entry_list '}'
459 | T_FIELD T_SYMBOL expression
461 process_field(FIELD, $2, $3.value);
464 enum_increment = 0x01 << (ffs($3.value) - 1);
466 '{' enum_entry_list '}'
467 | T_FIELD T_SYMBOL expression
469 process_field(FIELD, $2, $3.value);
480 '{' enum_entry_list '}'
481 | T_ENUM T_SYMBOL expression
483 process_field(ENUM, $2, $3.value);
486 enum_increment = 0x01 << (ffs($3.value) - 1);
488 '{' enum_entry_list '}'
493 | enum_entry_list ',' enum_entry
499 process_field(ENUM_ENTRY, $1, enum_next_value);
500 enum_next_value += enum_increment;
502 | T_SYMBOL expression
504 process_field(ENUM_ENTRY, $1, $2.value);
505 enum_next_value = $2.value + enum_increment;
510 T_MASK T_SYMBOL expression
512 process_field(MASK, $2, $3.value);
519 if ($2->type != UNINITIALIZED) {
520 stop("Re-definition of register alias",
525 initialize_symbol($2);
526 $2->info.ainfo->parent = cur_symbol;
533 if (accumulator.symbol != NULL) {
534 stop("Only one accumulator definition allowed",
538 accumulator.symbol = cur_symbol;
545 if (mode_ptr.symbol != NULL) {
546 stop("Only one mode pointer definition allowed",
550 mode_ptr.symbol = cur_symbol;
557 if (allones.symbol != NULL) {
558 stop("Only one definition of allones allowed",
562 allones.symbol = cur_symbol;
569 if (allzeros.symbol != NULL) {
570 stop("Only one definition of allzeros allowed",
574 allzeros.symbol = cur_symbol;
581 if (none.symbol != NULL) {
582 stop("Only one definition of none allowed",
586 none.symbol = cur_symbol;
593 if (sindex.symbol != NULL) {
594 stop("Only one definition of sindex allowed",
598 sindex.symbol = cur_symbol;
603 expression '|' expression
605 $$.value = $1.value | $3.value;
606 symlist_merge(&$$.referenced_syms,
608 &$3.referenced_syms);
610 | expression '&' expression
612 $$.value = $1.value & $3.value;
613 symlist_merge(&$$.referenced_syms,
615 &$3.referenced_syms);
617 | expression '+' expression
619 $$.value = $1.value + $3.value;
620 symlist_merge(&$$.referenced_syms,
622 &$3.referenced_syms);
624 | expression '-' expression
626 $$.value = $1.value - $3.value;
627 symlist_merge(&($$.referenced_syms),
628 &($1.referenced_syms),
629 &($3.referenced_syms));
631 | expression '*' expression
633 $$.value = $1.value * $3.value;
634 symlist_merge(&($$.referenced_syms),
635 &($1.referenced_syms),
636 &($3.referenced_syms));
638 | expression '/' expression
640 $$.value = $1.value / $3.value;
641 symlist_merge(&($$.referenced_syms),
642 &($1.referenced_syms),
643 &($3.referenced_syms));
645 | expression T_EXPR_LSHIFT expression
647 $$.value = $1.value << $3.value;
648 symlist_merge(&$$.referenced_syms,
650 &$3.referenced_syms);
652 | expression T_EXPR_RSHIFT expression
654 $$.value = $1.value >> $3.value;
655 symlist_merge(&$$.referenced_syms,
657 &$3.referenced_syms);
666 $$.value = (~$$.value) & 0xFF;
668 | '-' expression %prec UMINUS
671 $$.value = -$$.value;
676 SLIST_INIT(&$$.referenced_syms);
683 switch (symbol->type) {
685 symbol = $1->info.ainfo->parent;
689 $$.value = symbol->info.rinfo->address;
695 $$.value = symbol->info.finfo->value;
699 $$.value = symbol->info.cinfo->value;
704 snprintf(errbuf, sizeof(errbuf),
705 "Undefined symbol %s referenced",
707 stop(errbuf, EX_DATAERR);
712 SLIST_INIT(&$$.referenced_syms);
713 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
718 T_CONST T_SYMBOL expression
720 if ($2->type != UNINITIALIZED) {
721 stop("Re-definition of symbol as a constant",
726 initialize_symbol($2);
727 $2->info.cinfo->value = $3.value;
729 | T_CONST T_SYMBOL T_DOWNLOAD
732 stop("Invalid downloaded constant declaration",
736 if ($2->type != UNINITIALIZED) {
737 stop("Re-definition of symbol as a downloaded constant",
741 $2->type = DOWNLOAD_CONST;
742 initialize_symbol($2);
743 $2->info.cinfo->value = download_constant_count++;
750 if ($2->type != UNINITIALIZED) {
751 stop("Re-definition of symbol as a macro",
756 cur_symbol->type = MACRO;
757 initialize_symbol(cur_symbol);
762 macrodefn_prologue T_MACROBODY
766 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
769 cur_symbol->info.macroinfo->narg = $3;
775 /* Macros can take no arguments */
781 add_macro_arg($1, 0);
783 | macro_arglist ',' T_ARG
786 stop("Comma without preceeding argument in arg list",
791 add_macro_arg($3, $1);
798 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
800 cur_symbol = symtable_get(SRAM_SYMNAME);
801 cur_symtype = SRAMLOC;
802 cur_symbol->type = SRAMLOC;
803 initialize_symbol(cur_symbol);
807 sram_or_scb_offset = cur_symbol->info.rinfo->address;
811 scb_or_sram_symbol = cur_symbol;
813 scb_or_sram_attributes
817 scb_or_sram_symbol = NULL;
824 cur_symbol = symtable_get(SCB_SYMNAME);
825 cur_symtype = SCBLOC;
826 if (cur_symbol->type != UNINITIALIZED) {
827 stop("Only one SRAM definition allowed",
831 cur_symbol->type = SCBLOC;
832 initialize_symbol(cur_symbol);
833 /* 64 bytes of SCB space */
834 cur_symbol->info.rinfo->size = 64;
838 sram_or_scb_offset = cur_symbol->info.rinfo->address;
842 scb_or_sram_symbol = cur_symbol;
844 scb_or_sram_attributes
848 scb_or_sram_symbol = NULL;
852 scb_or_sram_attributes:
853 /* NULL definition is okay */
855 | scb_or_sram_reg_list
856 | modes scb_or_sram_reg_list
859 scb_or_sram_reg_list:
861 | scb_or_sram_reg_list reg_definition
867 process_register(&$1);
871 | T_SYMBOL '[' T_SYMBOL ']'
873 process_register(&$1);
874 if ($3->type != CONST) {
875 stop("register offset must be a constant", EX_DATAERR);
878 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
879 stop("Accessing offset beyond range of register",
884 $$.offset = $3->info.cinfo->value;
886 | T_SYMBOL '[' T_NUMBER ']'
888 process_register(&$1);
889 if (($3 + 1) > $1->info.rinfo->size) {
890 stop("Accessing offset beyond range of register",
899 if (accumulator.symbol == NULL) {
900 stop("No accumulator has been defined", EX_DATAERR);
903 $$.symbol = accumulator.symbol;
911 test_writable_symbol($1.symbol);
924 if ($1.value == 0 && is_download_const(&$1) == 0) {
925 snprintf(errbuf, sizeof(errbuf),
926 "\nExpression evaluates to 0 and thus "
927 "references the accumulator.\n "
928 "If this is the desired effect, use 'A' "
930 stop(errbuf, EX_DATAERR);
936 SLIST_INIT(&$$.referenced_syms);
937 symlist_add(&$$.referenced_syms, accumulator.symbol,
938 SYMLIST_INSERT_HEAD);
946 test_readable_symbol($1.symbol);
967 T_SET_SRC_MODE T_NUMBER ';'
974 T_SET_DST_MODE T_NUMBER ';'
980 critical_section_start:
983 critical_section_t *cs;
985 if (in_critical_section != FALSE) {
986 stop("Critical Section within Critical Section",
991 cs->begin_addr = instruction_ptr;
992 in_critical_section = TRUE;
996 critical_section_end:
999 critical_section_t *cs;
1001 if (in_critical_section == FALSE) {
1002 stop("Unballanced 'end_cs'", EX_DATAERR);
1005 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1006 cs->end_addr = instruction_ptr;
1007 in_critical_section = FALSE;
1020 if ($2->type != UNINITIALIZED) {
1021 stop("Program label multiply defined", EX_DATAERR);
1025 initialize_symbol($2);
1026 $2->info.linfo->address = instruction_ptr;
1027 $2->info.linfo->exported = $1;
1037 | T_SYMBOL '+' T_NUMBER
1042 | T_SYMBOL '-' T_NUMBER
1069 add_conditional($2);
1070 new_scope = scope_alloc();
1071 new_scope->type = SCOPE_IF;
1072 new_scope->begin_addr = instruction_ptr;
1073 new_scope->func_num = $2->info.condinfo->func_num;
1075 | T_ELSE T_IF T_CEXPR '{'
1078 scope_t *scope_context;
1079 scope_t *last_scope;
1082 * Ensure that the previous scope is either an
1083 * if or and else if.
1085 scope_context = SLIST_FIRST(&scope_stack);
1086 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1088 if (last_scope == NULL
1089 || last_scope->type == T_ELSE) {
1091 stop("'else if' without leading 'if'", EX_DATAERR);
1094 add_conditional($3);
1095 new_scope = scope_alloc();
1096 new_scope->type = SCOPE_ELSE_IF;
1097 new_scope->begin_addr = instruction_ptr;
1098 new_scope->func_num = $3->info.condinfo->func_num;
1103 scope_t *scope_context;
1104 scope_t *last_scope;
1107 * Ensure that the previous scope is either an
1108 * if or and else if.
1110 scope_context = SLIST_FIRST(&scope_stack);
1111 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1113 if (last_scope == NULL
1114 || last_scope->type == SCOPE_ELSE) {
1116 stop("'else' without leading 'if'", EX_DATAERR);
1119 new_scope = scope_alloc();
1120 new_scope->type = SCOPE_ELSE;
1121 new_scope->begin_addr = instruction_ptr;
1128 scope_t *scope_context;
1130 scope_context = SLIST_FIRST(&scope_stack);
1131 if (scope_context->type == SCOPE_ROOT) {
1132 stop("Unexpected '}' encountered", EX_DATAERR);
1136 scope_context->end_addr = instruction_ptr;
1139 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1141 process_scope(scope_context);
1143 if (SLIST_FIRST(&scope_stack) == NULL) {
1144 stop("Unexpected '}' encountered", EX_DATAERR);
1151 T_AND { $$ = AIC_OP_AND; }
1152 | T_XOR { $$ = AIC_OP_XOR; }
1153 | T_ADD { $$ = AIC_OP_ADD; }
1154 | T_ADC { $$ = AIC_OP_ADC; }
1158 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1160 format_1_instr($1, &$2, &$4, &$5, $6);
1165 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1167 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1172 T_INC destination opt_source ret ';'
1176 make_expression(&immed, 1);
1177 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1182 T_DEC destination opt_source ret ';'
1186 make_expression(&immed, -1);
1187 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1196 make_expression(&immed, -1);
1197 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1199 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1201 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1210 make_expression(&immed, 1);
1211 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1213 | T_STC destination ret ';'
1217 make_expression(&immed, 1);
1218 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1223 T_BMOV destination ',' source ',' immediate ret ';'
1225 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1230 T_MOV destination ',' source ret ';'
1234 make_expression(&immed, 1);
1235 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1240 T_MVI destination ',' immediate ret ';'
1243 && is_download_const(&$4) == 0) {
1247 * Allow move immediates of 0 so that macros,
1248 * that can't know the immediate's value and
1249 * otherwise compensate, still work.
1251 make_expression(&immed, 1);
1252 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1254 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1260 T_NOT destination opt_source ret ';'
1264 make_expression(&immed, 0xff);
1265 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1270 T_CLR destination ret ';'
1274 make_expression(&immed, 0xff);
1275 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1284 make_expression(&immed, 0xff);
1285 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1294 make_expression(&immed, 0xff);
1295 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1300 * This grammer differs from the one in the aic7xxx
1301 * reference manual since the grammer listed there is
1302 * ambiguous and causes a shift/reduce conflict.
1303 * It also seems more logical as the "immediate"
1304 * argument is listed as the second arg like the
1309 T_SHL { $$ = AIC_OP_SHL; }
1310 | T_SHR { $$ = AIC_OP_SHR; }
1311 | T_ROL { $$ = AIC_OP_ROL; }
1312 | T_ROR { $$ = AIC_OP_ROR; }
1316 * 16bit opcodes, not used
1319 * T_OR16 { $$ = AIC_OP_OR16; }
1320 *| T_AND16 { $$ = AIC_OP_AND16; }
1321 *| T_XOR16 { $$ = AIC_OP_XOR16; }
1322 *| T_ADD16 { $$ = AIC_OP_ADD16; }
1323 *| T_ADC16 { $$ = AIC_OP_ADC16; }
1324 *| T_MVI16 { $$ = AIC_OP_MVI16; }
1329 f2_opcode destination ',' expression opt_source ret ';'
1331 format_2_instr($1, &$2, &$4, &$5, $6);
1336 T_JMP { $$ = AIC_OP_JMP; }
1337 | T_JC { $$ = AIC_OP_JC; }
1338 | T_JNC { $$ = AIC_OP_JNC; }
1339 | T_CALL { $$ = AIC_OP_CALL; }
1343 T_JZ { $$ = AIC_OP_JZ; }
1344 | T_JNZ { $$ = AIC_OP_JNZ; }
1348 T_JE { $$ = AIC_OP_JE; }
1349 | T_JNE { $$ = AIC_OP_JNE; }
1353 jmp_jc_jnc_call address ';'
1357 make_expression(&immed, 0);
1358 format_3_instr($1, &sindex, &immed, &$2);
1363 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1365 type_check(&$2, &$4, AIC_OP_OR);
1366 format_3_instr($5, &$2, &$4, &$6);
1371 T_TEST source ',' immediate_or_a jz_jnz address ';'
1373 format_3_instr($5, &$2, &$4, &$6);
1378 T_CMP source ',' immediate_or_a je_jne address ';'
1380 format_3_instr($5, &$2, &$4, &$6);
1385 T_MOV source jmp_jc_jnc_call address ';'
1389 make_expression(&immed, 0);
1390 format_3_instr($3, &$2, &immed, &$4);
1395 T_MVI immediate jmp_jc_jnc_call address ';'
1397 format_3_instr($3, &allzeros, &$2, &$4);
1404 process_field(int field_type, symbol_t *sym, int value)
1407 * Add the current register to its
1408 * symbol list, if it already exists,
1409 * warn if we are setting it to a
1410 * different value, or in the bit to
1411 * the "allowed bits" of this register.
1413 if (sym->type == UNINITIALIZED) {
1414 sym->type = field_type;
1415 initialize_symbol(sym);
1416 sym->info.finfo->value = value;
1417 if (field_type != ENUM_ENTRY) {
1418 if (field_type != MASK && value == 0) {
1419 stop("Empty Field, or Enum", EX_DATAERR);
1422 sym->info.finfo->value = value;
1423 sym->info.finfo->mask = value;
1424 } else if (field_symbol != NULL) {
1425 sym->info.finfo->mask = field_symbol->info.finfo->value;
1427 sym->info.finfo->mask = 0xFF;
1429 } else if (sym->type != field_type) {
1430 stop("Field definition mirrors a definition of the same "
1431 " name, but a different type", EX_DATAERR);
1433 } else if (value != sym->info.finfo->value) {
1434 stop("Field redefined with a conflicting value", EX_DATAERR);
1437 /* Fail if this symbol is already listed */
1438 if (symlist_search(&(sym->info.finfo->symrefs),
1439 cur_symbol->name) != NULL) {
1440 stop("Field defined multiple times for register", EX_DATAERR);
1443 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1444 SYMLIST_INSERT_HEAD);
1445 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1446 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1447 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1451 initialize_symbol(symbol_t *symbol)
1453 switch (symbol->type) {
1455 stop("Call to initialize_symbol with type field unset",
1462 symbol->info.rinfo =
1463 (struct reg_info *)malloc(sizeof(struct reg_info));
1464 if (symbol->info.rinfo == NULL) {
1465 stop("Can't create register info", EX_SOFTWARE);
1468 memset(symbol->info.rinfo, 0,
1469 sizeof(struct reg_info));
1470 SLIST_INIT(&(symbol->info.rinfo->fields));
1472 * Default to allowing access in all register modes
1473 * or to the mode specified by the SCB or SRAM space
1476 if (scb_or_sram_symbol != NULL)
1477 symbol->info.rinfo->modes =
1478 scb_or_sram_symbol->info.rinfo->modes;
1480 symbol->info.rinfo->modes = ~0;
1483 symbol->info.ainfo =
1484 (struct alias_info *)malloc(sizeof(struct alias_info));
1485 if (symbol->info.ainfo == NULL) {
1486 stop("Can't create alias info", EX_SOFTWARE);
1489 memset(symbol->info.ainfo, 0,
1490 sizeof(struct alias_info));
1496 symbol->info.finfo =
1497 (struct field_info *)malloc(sizeof(struct field_info));
1498 if (symbol->info.finfo == NULL) {
1499 stop("Can't create field info", EX_SOFTWARE);
1502 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1503 SLIST_INIT(&(symbol->info.finfo->symrefs));
1506 case DOWNLOAD_CONST:
1507 symbol->info.cinfo =
1508 (struct const_info *)malloc(sizeof(struct const_info));
1509 if (symbol->info.cinfo == NULL) {
1510 stop("Can't create alias info", EX_SOFTWARE);
1513 memset(symbol->info.cinfo, 0,
1514 sizeof(struct const_info));
1517 symbol->info.linfo =
1518 (struct label_info *)malloc(sizeof(struct label_info));
1519 if (symbol->info.linfo == NULL) {
1520 stop("Can't create label info", EX_SOFTWARE);
1523 memset(symbol->info.linfo, 0,
1524 sizeof(struct label_info));
1527 symbol->info.condinfo =
1528 (struct cond_info *)malloc(sizeof(struct cond_info));
1529 if (symbol->info.condinfo == NULL) {
1530 stop("Can't create conditional info", EX_SOFTWARE);
1533 memset(symbol->info.condinfo, 0,
1534 sizeof(struct cond_info));
1537 symbol->info.macroinfo =
1538 (struct macro_info *)malloc(sizeof(struct macro_info));
1539 if (symbol->info.macroinfo == NULL) {
1540 stop("Can't create macro info", EX_SOFTWARE);
1543 memset(symbol->info.macroinfo, 0,
1544 sizeof(struct macro_info));
1545 STAILQ_INIT(&symbol->info.macroinfo->args);
1548 stop("Call to initialize_symbol with invalid symbol type",
1556 add_macro_arg(const char *argtext, int argnum)
1558 struct macro_arg *marg;
1562 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1563 stop("Invalid current symbol for adding macro arg",
1568 marg = (struct macro_arg *)malloc(sizeof(*marg));
1570 stop("Can't create macro_arg structure", EX_SOFTWARE);
1573 marg->replacement_text = NULL;
1574 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1575 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1577 if (retval >= sizeof(regex_pattern)) {
1578 stop("Regex text buffer too small for arg",
1582 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1584 stop("Regex compilation failed", EX_SOFTWARE);
1587 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1591 add_macro_body(const char *bodytext)
1593 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1594 stop("Invalid current symbol for adding macro arg",
1598 cur_symbol->info.macroinfo->body = strdup(bodytext);
1599 if (cur_symbol->info.macroinfo->body == NULL) {
1600 stop("Can't duplicate macro body text", EX_SOFTWARE);
1606 process_register(symbol_t **p_symbol)
1608 symbol_t *symbol = *p_symbol;
1610 if (symbol->type == UNINITIALIZED) {
1611 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1613 stop(errbuf, EX_DATAERR);
1615 } else if (symbol->type == ALIAS) {
1616 *p_symbol = symbol->info.ainfo->parent;
1617 } else if ((symbol->type != REGISTER)
1618 && (symbol->type != SCBLOC)
1619 && (symbol->type != SRAMLOC)) {
1620 snprintf(errbuf, sizeof(errbuf),
1621 "Specified symbol %s is not a register",
1623 stop(errbuf, EX_DATAERR);
1628 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1629 symbol_ref_t *src, int ret)
1631 struct instruction *instr;
1632 struct ins_format1 *f1_instr;
1634 if (src->symbol == NULL)
1637 /* Test register permissions */
1638 test_writable_symbol(dest->symbol);
1639 test_readable_symbol(src->symbol);
1641 if (!is_location_address(dest->symbol)) {
1642 /* Ensure that immediate makes sense for this destination */
1643 type_check(dest, immed, opcode);
1646 /* Allocate sequencer space for the instruction and fill it out */
1647 instr = seq_alloc();
1648 f1_instr = &instr->format.format1;
1649 f1_instr->ret = ret ? 1 : 0;
1650 f1_instr->opcode = opcode;
1651 f1_instr->destination = dest->symbol->info.rinfo->address
1653 f1_instr->source = src->symbol->info.rinfo->address
1655 f1_instr->immediate = immed->value;
1657 if (is_download_const(immed))
1658 f1_instr->parity = 1;
1659 else if (dest->symbol == mode_ptr.symbol) {
1664 * Attempt to update mode information if
1665 * we are operating on the mode register.
1667 if (src->symbol == allones.symbol)
1669 else if (src->symbol == allzeros.symbol)
1671 else if (src->symbol == mode_ptr.symbol)
1672 src_value = (dst_mode << 4) | src_mode;
1678 dst_value = src_value & immed->value;
1681 dst_value = src_value ^ immed->value;
1684 dst_value = (src_value + immed->value) & 0xFF;
1687 dst_value = src_value | immed->value;
1690 dst_value = src_value;
1695 src_mode = dst_value & 0xF;
1696 dst_mode = (dst_value >> 4) & 0xF;
1700 symlist_free(&immed->referenced_syms);
1705 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1706 symbol_ref_t *src, int ret)
1708 struct instruction *instr;
1709 struct ins_format2 *f2_instr;
1710 uint8_t shift_control;
1712 if (src->symbol == NULL)
1715 /* Test register permissions */
1716 test_writable_symbol(dest->symbol);
1717 test_readable_symbol(src->symbol);
1719 /* Allocate sequencer space for the instruction and fill it out */
1720 instr = seq_alloc();
1721 f2_instr = &instr->format.format2;
1722 f2_instr->ret = ret ? 1 : 0;
1723 f2_instr->opcode = AIC_OP_ROL;
1724 f2_instr->destination = dest->symbol->info.rinfo->address
1726 f2_instr->source = src->symbol->info.rinfo->address
1728 if (places->value > 8 || places->value <= 0) {
1729 stop("illegal shift value", EX_DATAERR);
1734 if (places->value == 8)
1735 shift_control = 0xf0;
1737 shift_control = (places->value << 4) | places->value;
1740 if (places->value == 8) {
1741 shift_control = 0xf8;
1743 shift_control = (places->value << 4)
1744 | (8 - places->value)
1749 shift_control = places->value & 0x7;
1752 shift_control = (8 - places->value) | 0x08;
1755 shift_control = 0; /* Quiet Compiler */
1756 stop("Invalid shift operation specified", EX_SOFTWARE);
1760 f2_instr->shift_control = shift_control;
1761 symlist_free(&places->referenced_syms);
1766 format_3_instr(int opcode, symbol_ref_t *src,
1767 expression_t *immed, symbol_ref_t *address)
1769 struct instruction *instr;
1770 struct ins_format3 *f3_instr;
1773 /* Test register permissions */
1774 test_readable_symbol(src->symbol);
1776 /* Allocate sequencer space for the instruction and fill it out */
1777 instr = seq_alloc();
1778 f3_instr = &instr->format.format3;
1779 if (address->symbol == NULL) {
1780 /* 'dot' referrence. Use the current instruction pointer */
1781 addr = instruction_ptr + address->offset;
1782 } else if (address->symbol->type == UNINITIALIZED) {
1783 /* forward reference */
1784 addr = address->offset;
1785 instr->patch_label = address->symbol;
1787 addr = address->symbol->info.linfo->address + address->offset;
1788 f3_instr->opcode = opcode;
1789 f3_instr->address = addr;
1790 f3_instr->source = src->symbol->info.rinfo->address
1792 f3_instr->immediate = immed->value;
1794 if (is_download_const(immed))
1795 f3_instr->parity = 1;
1797 symlist_free(&immed->referenced_syms);
1802 test_readable_symbol(symbol_t *symbol)
1804 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1805 snprintf(errbuf, sizeof(errbuf),
1806 "Register %s unavailable in source reg mode %d",
1807 symbol->name, src_mode);
1808 stop(errbuf, EX_DATAERR);
1811 if (symbol->info.rinfo->mode == WO) {
1812 stop("Write Only register specified as source",
1819 test_writable_symbol(symbol_t *symbol)
1821 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1822 snprintf(errbuf, sizeof(errbuf),
1823 "Register %s unavailable in destination reg mode %d",
1824 symbol->name, dst_mode);
1825 stop(errbuf, EX_DATAERR);
1828 if (symbol->info.rinfo->mode == RO) {
1829 stop("Read Only register specified as destination",
1836 type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
1838 symbol_t *symbol = sym->symbol;
1839 symbol_node_t *node;
1845 * Make sure that we aren't attempting to write something
1846 * that hasn't been defined. If this is an and operation,
1847 * this is a mask, so "undefined" bits are okay.
1849 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ ||
1850 opcode == AIC_OP_JZ || opcode == AIC_OP_JNE ||
1851 opcode == AIC_OP_BMOV)
1855 * Defaulting to 8 bit logic
1857 mask = (int8_t)~symbol->info.rinfo->valid_bitmask;
1858 value = (int8_t)expression->value;
1860 if (and_op == FALSE && (mask & value) != 0 ) {
1861 snprintf(errbuf, sizeof(errbuf),
1862 "Invalid bit(s) 0x%x in immediate written to %s",
1865 stop(errbuf, EX_DATAERR);
1870 * Now make sure that all of the symbols referenced by the
1871 * expression are defined for this register.
1873 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1874 for(node = expression->referenced_syms.slh_first;
1876 node = node->links.sle_next) {
1877 if ((node->symbol->type == MASK
1878 || node->symbol->type == FIELD
1879 || node->symbol->type == ENUM
1880 || node->symbol->type == ENUM_ENTRY)
1881 && symlist_search(&node->symbol->info.finfo->symrefs,
1882 symbol->name) == NULL) {
1883 snprintf(errbuf, sizeof(errbuf),
1884 "Invalid field or mask %s "
1886 node->symbol->name, symbol->name);
1887 stop(errbuf, EX_DATAERR);
1895 make_expression(expression_t *immed, int value)
1897 SLIST_INIT(&immed->referenced_syms);
1898 immed->value = value & 0xff;
1902 add_conditional(symbol_t *symbol)
1904 static int numfuncs;
1906 if (numfuncs == 0) {
1907 /* add a special conditional, "0" */
1908 symbol_t *false_func;
1910 false_func = symtable_get("0");
1911 if (false_func->type != UNINITIALIZED) {
1912 stop("Conditional expression '0' "
1913 "conflicts with a symbol", EX_DATAERR);
1916 false_func->type = CONDITIONAL;
1917 initialize_symbol(false_func);
1918 false_func->info.condinfo->func_num = numfuncs++;
1919 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1922 /* This condition has occurred before */
1923 if (symbol->type == CONDITIONAL)
1926 if (symbol->type != UNINITIALIZED) {
1927 stop("Conditional expression conflicts with a symbol",
1932 symbol->type = CONDITIONAL;
1933 initialize_symbol(symbol);
1934 symbol->info.condinfo->func_num = numfuncs++;
1935 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1939 add_version(const char *verstring)
1941 const char prefix[] = " * ";
1945 newlen = strlen(verstring) + strlen(prefix);
1947 if (versions != NULL)
1948 oldlen = strlen(versions);
1949 versions = realloc(versions, newlen + oldlen + 2);
1950 if (versions == NULL)
1951 stop("Can't allocate version string", EX_SOFTWARE);
1952 strcpy(&versions[oldlen], prefix);
1953 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1954 versions[newlen + oldlen] = '\n';
1955 versions[newlen + oldlen + 1] = '\0';
1959 yyerror(const char *string)
1961 stop(string, EX_DATAERR);
1965 is_download_const(expression_t *immed)
1967 if ((immed->referenced_syms.slh_first != NULL)
1968 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
1975 is_location_address(symbol_t *sym)
1977 if (sym->type == SCBLOC ||
1978 sym->type == SRAMLOC)