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
150 %token T_DONT_GENERATE_DEBUG_CODE
156 %token T_SET_SRC_MODE
158 %token T_SET_DST_MODE
160 %token <value> T_MODE
174 %token <value> T_NUMBER
176 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
180 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
182 %token <value> T_SHR T_SHL T_ROR T_ROL
184 %token <value> T_MVI T_MOV T_CLR T_BMOV
186 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
188 %token <value> T_ADD T_ADC
190 %token <value> T_INC T_DEC
192 %token <value> T_STC T_CLC
194 %token <value> T_CMP T_NOT T_XOR
196 %token <value> T_TEST T_AND
200 /* 16 bit extensions, not implemented
201 * %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
202 * %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
208 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
212 %token <sym> T_SYMBOL
216 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
218 %type <sym_ref> reg_symbol address destination source opt_source
220 %type <expression> expression immediate immediate_or_a
222 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
224 %type <value> mode_value mode_list macro_arglist
228 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
241 | program patch_arg_list
251 | program scratch_ram
257 | program set_src_mode
259 | program set_dst_mode
260 | critical_section_start
261 | program critical_section_start
262 | critical_section_end
263 | program critical_section_end
265 | program conditional
271 T_INCLUDE '<' T_PATH '>'
273 include_file($3, BRACKETED_INCLUDE);
275 | T_INCLUDE '"' T_PATH '"'
277 include_file($3, QUOTED_INCLUDE);
282 T_PREFIX '=' T_STRING
284 if (prefix != stock_prefix)
285 stop("Prefix multiply defined",
289 stop("Unable to record prefix", EX_SOFTWARE);
294 T_PATCH_ARG_LIST '=' T_STRING
296 if (patch_arg_list != NULL)
297 stop("Patch argument list multiply defined",
299 patch_arg_list = strdup($3);
300 if (patch_arg_list == NULL)
301 stop("Unable to record patch arg list", EX_SOFTWARE);
306 T_VERSION '=' T_STRING
311 T_REGISTER { cur_symtype = REGISTER; } reg_definition
317 if ($1->type != UNINITIALIZED) {
318 stop("Register multiply defined", EX_DATAERR);
322 cur_symbol->type = cur_symtype;
323 initialize_symbol(cur_symbol);
329 * Default to allowing everything in for registers
330 * with no bit or mask definitions.
332 if (cur_symbol->info.rinfo->valid_bitmask == 0)
333 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
335 if (cur_symbol->info.rinfo->size == 0)
336 cur_symbol->info.rinfo->size = 1;
339 * This might be useful for registers too.
341 if (cur_symbol->type != REGISTER) {
342 if (cur_symbol->info.rinfo->address == 0)
343 cur_symbol->info.rinfo->address =
345 sram_or_scb_offset +=
346 cur_symbol->info.rinfo->size;
354 | reg_attribute_list reg_attribute
362 | dont_generate_debug_code
379 cur_symbol->info.rinfo->address = $2;
386 cur_symbol->info.rinfo->size = $2;
387 if (scb_or_sram_symbol != NULL) {
391 max_addr = scb_or_sram_symbol->info.rinfo->address
392 + scb_or_sram_symbol->info.rinfo->size;
393 sym_max_addr = cur_symbol->info.rinfo->address
394 + cur_symbol->info.rinfo->size;
396 if (sym_max_addr > max_addr)
397 stop("SCB or SRAM space exhausted", EX_DATAERR);
405 cur_symbol->count += $2;
412 cur_symbol->info.rinfo->mode = $2;
416 dont_generate_debug_code:
417 T_DONT_GENERATE_DEBUG_CODE
419 cur_symbol->dont_generate_debug_code = 1;
426 cur_symbol->info.rinfo->modes = $2;
435 | mode_list ',' mode_value
445 stop("Valid register modes range between 0 and 4.",
457 if (symbol->type != CONST) {
458 stop("Only \"const\" symbols allowed in "
459 "mode definitions.", EX_DATAERR);
462 if (symbol->info.cinfo->value > 4) {
463 stop("Valid register modes range between 0 and 4.",
467 $$ = (0x1 << symbol->info.cinfo->value);
478 '{' enum_entry_list '}'
479 | T_FIELD T_SYMBOL expression
481 process_field(FIELD, $2, $3.value);
484 enum_increment = 0x01 << (ffs($3.value) - 1);
486 '{' enum_entry_list '}'
487 | T_FIELD T_SYMBOL expression
489 process_field(FIELD, $2, $3.value);
500 '{' enum_entry_list '}'
501 | T_ENUM T_SYMBOL expression
503 process_field(ENUM, $2, $3.value);
506 enum_increment = 0x01 << (ffs($3.value) - 1);
508 '{' enum_entry_list '}'
513 | enum_entry_list ',' enum_entry
519 process_field(ENUM_ENTRY, $1, enum_next_value);
520 enum_next_value += enum_increment;
522 | T_SYMBOL expression
524 process_field(ENUM_ENTRY, $1, $2.value);
525 enum_next_value = $2.value + enum_increment;
530 T_MASK T_SYMBOL expression
532 process_field(MASK, $2, $3.value);
539 if ($2->type != UNINITIALIZED) {
540 stop("Re-definition of register alias",
545 initialize_symbol($2);
546 $2->info.ainfo->parent = cur_symbol;
553 if (accumulator.symbol != NULL) {
554 stop("Only one accumulator definition allowed",
558 accumulator.symbol = cur_symbol;
565 if (mode_ptr.symbol != NULL) {
566 stop("Only one mode pointer definition allowed",
570 mode_ptr.symbol = cur_symbol;
577 if (allones.symbol != NULL) {
578 stop("Only one definition of allones allowed",
582 allones.symbol = cur_symbol;
589 if (allzeros.symbol != NULL) {
590 stop("Only one definition of allzeros allowed",
594 allzeros.symbol = cur_symbol;
601 if (none.symbol != NULL) {
602 stop("Only one definition of none allowed",
606 none.symbol = cur_symbol;
613 if (sindex.symbol != NULL) {
614 stop("Only one definition of sindex allowed",
618 sindex.symbol = cur_symbol;
623 expression '|' expression
625 $$.value = $1.value | $3.value;
626 symlist_merge(&$$.referenced_syms,
628 &$3.referenced_syms);
630 | expression '&' expression
632 $$.value = $1.value & $3.value;
633 symlist_merge(&$$.referenced_syms,
635 &$3.referenced_syms);
637 | expression '+' expression
639 $$.value = $1.value + $3.value;
640 symlist_merge(&$$.referenced_syms,
642 &$3.referenced_syms);
644 | expression '-' expression
646 $$.value = $1.value - $3.value;
647 symlist_merge(&($$.referenced_syms),
648 &($1.referenced_syms),
649 &($3.referenced_syms));
651 | expression '*' expression
653 $$.value = $1.value * $3.value;
654 symlist_merge(&($$.referenced_syms),
655 &($1.referenced_syms),
656 &($3.referenced_syms));
658 | expression '/' expression
660 $$.value = $1.value / $3.value;
661 symlist_merge(&($$.referenced_syms),
662 &($1.referenced_syms),
663 &($3.referenced_syms));
665 | expression T_EXPR_LSHIFT expression
667 $$.value = $1.value << $3.value;
668 symlist_merge(&$$.referenced_syms,
670 &$3.referenced_syms);
672 | expression T_EXPR_RSHIFT expression
674 $$.value = $1.value >> $3.value;
675 symlist_merge(&$$.referenced_syms,
677 &$3.referenced_syms);
686 $$.value = (~$$.value) & 0xFF;
688 | '-' expression %prec UMINUS
691 $$.value = -$$.value;
696 SLIST_INIT(&$$.referenced_syms);
703 switch (symbol->type) {
705 symbol = $1->info.ainfo->parent;
709 $$.value = symbol->info.rinfo->address;
715 $$.value = symbol->info.finfo->value;
719 $$.value = symbol->info.cinfo->value;
724 snprintf(errbuf, sizeof(errbuf),
725 "Undefined symbol %s referenced",
727 stop(errbuf, EX_DATAERR);
732 SLIST_INIT(&$$.referenced_syms);
733 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
738 T_CONST T_SYMBOL expression
740 if ($2->type != UNINITIALIZED) {
741 stop("Re-definition of symbol as a constant",
746 initialize_symbol($2);
747 $2->info.cinfo->value = $3.value;
749 | T_CONST T_SYMBOL T_DOWNLOAD
752 stop("Invalid downloaded constant declaration",
756 if ($2->type != UNINITIALIZED) {
757 stop("Re-definition of symbol as a downloaded constant",
761 $2->type = DOWNLOAD_CONST;
762 initialize_symbol($2);
763 $2->info.cinfo->value = download_constant_count++;
770 if ($2->type != UNINITIALIZED) {
771 stop("Re-definition of symbol as a macro",
776 cur_symbol->type = MACRO;
777 initialize_symbol(cur_symbol);
782 macrodefn_prologue T_MACROBODY
786 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
789 cur_symbol->info.macroinfo->narg = $3;
795 /* Macros can take no arguments */
801 add_macro_arg($1, 0);
803 | macro_arglist ',' T_ARG
806 stop("Comma without preceeding argument in arg list",
811 add_macro_arg($3, $1);
818 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
820 cur_symbol = symtable_get(SRAM_SYMNAME);
821 cur_symtype = SRAMLOC;
822 cur_symbol->type = SRAMLOC;
823 initialize_symbol(cur_symbol);
824 cur_symbol->count += 1;
828 sram_or_scb_offset = cur_symbol->info.rinfo->address;
832 scb_or_sram_symbol = cur_symbol;
834 scb_or_sram_attributes
838 scb_or_sram_symbol = NULL;
845 cur_symbol = symtable_get(SCB_SYMNAME);
846 cur_symtype = SCBLOC;
847 if (cur_symbol->type != UNINITIALIZED) {
848 stop("Only one SRAM definition allowed",
852 cur_symbol->type = SCBLOC;
853 initialize_symbol(cur_symbol);
854 /* 64 bytes of SCB space */
855 cur_symbol->info.rinfo->size = 64;
856 cur_symbol->count += 1;
860 sram_or_scb_offset = cur_symbol->info.rinfo->address;
864 scb_or_sram_symbol = cur_symbol;
866 scb_or_sram_attributes
870 scb_or_sram_symbol = NULL;
874 scb_or_sram_attributes:
875 /* NULL definition is okay */
877 | scb_or_sram_reg_list
878 | modes scb_or_sram_reg_list
881 scb_or_sram_reg_list:
883 | scb_or_sram_reg_list reg_definition
889 process_register(&$1);
893 | T_SYMBOL '[' T_SYMBOL ']'
895 process_register(&$1);
896 if ($3->type != CONST) {
897 stop("register offset must be a constant", EX_DATAERR);
900 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
901 stop("Accessing offset beyond range of register",
906 $$.offset = $3->info.cinfo->value;
908 | T_SYMBOL '[' T_NUMBER ']'
910 process_register(&$1);
911 if (($3 + 1) > $1->info.rinfo->size) {
912 stop("Accessing offset beyond range of register",
921 if (accumulator.symbol == NULL) {
922 stop("No accumulator has been defined", EX_DATAERR);
925 $$.symbol = accumulator.symbol;
933 test_writable_symbol($1.symbol);
946 if ($1.value == 0 && is_download_const(&$1) == 0) {
947 snprintf(errbuf, sizeof(errbuf),
948 "\nExpression evaluates to 0 and thus "
949 "references the accumulator.\n "
950 "If this is the desired effect, use 'A' "
952 stop(errbuf, EX_DATAERR);
958 SLIST_INIT(&$$.referenced_syms);
959 symlist_add(&$$.referenced_syms, accumulator.symbol,
960 SYMLIST_INSERT_HEAD);
968 test_readable_symbol($1.symbol);
989 T_SET_SRC_MODE T_NUMBER ';'
996 T_SET_DST_MODE T_NUMBER ';'
1002 critical_section_start:
1005 critical_section_t *cs;
1007 if (in_critical_section != FALSE) {
1008 stop("Critical Section within Critical Section",
1013 cs->begin_addr = instruction_ptr;
1014 in_critical_section = TRUE;
1018 critical_section_end:
1021 critical_section_t *cs;
1023 if (in_critical_section == FALSE) {
1024 stop("Unballanced 'end_cs'", EX_DATAERR);
1027 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1028 cs->end_addr = instruction_ptr;
1029 in_critical_section = FALSE;
1042 if ($2->type != UNINITIALIZED) {
1043 stop("Program label multiply defined", EX_DATAERR);
1047 initialize_symbol($2);
1048 $2->info.linfo->address = instruction_ptr;
1049 $2->info.linfo->exported = $1;
1059 | T_SYMBOL '+' T_NUMBER
1064 | T_SYMBOL '-' T_NUMBER
1091 add_conditional($2);
1092 new_scope = scope_alloc();
1093 new_scope->type = SCOPE_IF;
1094 new_scope->begin_addr = instruction_ptr;
1095 new_scope->func_num = $2->info.condinfo->func_num;
1097 | T_ELSE T_IF T_CEXPR '{'
1100 scope_t *scope_context;
1101 scope_t *last_scope;
1104 * Ensure that the previous scope is either an
1105 * if or and else if.
1107 scope_context = SLIST_FIRST(&scope_stack);
1108 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1110 if (last_scope == NULL
1111 || last_scope->type == T_ELSE) {
1113 stop("'else if' without leading 'if'", EX_DATAERR);
1116 add_conditional($3);
1117 new_scope = scope_alloc();
1118 new_scope->type = SCOPE_ELSE_IF;
1119 new_scope->begin_addr = instruction_ptr;
1120 new_scope->func_num = $3->info.condinfo->func_num;
1125 scope_t *scope_context;
1126 scope_t *last_scope;
1129 * Ensure that the previous scope is either an
1130 * if or and else if.
1132 scope_context = SLIST_FIRST(&scope_stack);
1133 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1135 if (last_scope == NULL
1136 || last_scope->type == SCOPE_ELSE) {
1138 stop("'else' without leading 'if'", EX_DATAERR);
1141 new_scope = scope_alloc();
1142 new_scope->type = SCOPE_ELSE;
1143 new_scope->begin_addr = instruction_ptr;
1150 scope_t *scope_context;
1152 scope_context = SLIST_FIRST(&scope_stack);
1153 if (scope_context->type == SCOPE_ROOT) {
1154 stop("Unexpected '}' encountered", EX_DATAERR);
1158 scope_context->end_addr = instruction_ptr;
1161 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1163 process_scope(scope_context);
1165 if (SLIST_FIRST(&scope_stack) == NULL) {
1166 stop("Unexpected '}' encountered", EX_DATAERR);
1173 T_AND { $$ = AIC_OP_AND; }
1174 | T_XOR { $$ = AIC_OP_XOR; }
1175 | T_ADD { $$ = AIC_OP_ADD; }
1176 | T_ADC { $$ = AIC_OP_ADC; }
1180 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1182 format_1_instr($1, &$2, &$4, &$5, $6);
1187 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1189 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1194 T_INC destination opt_source ret ';'
1198 make_expression(&immed, 1);
1199 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1204 T_DEC destination opt_source ret ';'
1208 make_expression(&immed, -1);
1209 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1218 make_expression(&immed, -1);
1219 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1221 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1223 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1232 make_expression(&immed, 1);
1233 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1235 | T_STC destination ret ';'
1239 make_expression(&immed, 1);
1240 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1245 T_BMOV destination ',' source ',' immediate ret ';'
1247 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1252 T_MOV destination ',' source ret ';'
1256 make_expression(&immed, 1);
1257 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1262 T_MVI destination ',' immediate ret ';'
1265 && is_download_const(&$4) == 0) {
1269 * Allow move immediates of 0 so that macros,
1270 * that can't know the immediate's value and
1271 * otherwise compensate, still work.
1273 make_expression(&immed, 1);
1274 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1276 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1282 T_NOT destination opt_source ret ';'
1286 make_expression(&immed, 0xff);
1287 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1292 T_CLR destination ret ';'
1296 make_expression(&immed, 0xff);
1297 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1306 make_expression(&immed, 0xff);
1307 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1316 make_expression(&immed, 0xff);
1317 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1322 * This grammer differs from the one in the aic7xxx
1323 * reference manual since the grammer listed there is
1324 * ambiguous and causes a shift/reduce conflict.
1325 * It also seems more logical as the "immediate"
1326 * argument is listed as the second arg like the
1331 T_SHL { $$ = AIC_OP_SHL; }
1332 | T_SHR { $$ = AIC_OP_SHR; }
1333 | T_ROL { $$ = AIC_OP_ROL; }
1334 | T_ROR { $$ = AIC_OP_ROR; }
1338 * 16bit opcodes, not used
1341 * T_OR16 { $$ = AIC_OP_OR16; }
1342 *| T_AND16 { $$ = AIC_OP_AND16; }
1343 *| T_XOR16 { $$ = AIC_OP_XOR16; }
1344 *| T_ADD16 { $$ = AIC_OP_ADD16; }
1345 *| T_ADC16 { $$ = AIC_OP_ADC16; }
1346 *| T_MVI16 { $$ = AIC_OP_MVI16; }
1351 f2_opcode destination ',' expression opt_source ret ';'
1353 format_2_instr($1, &$2, &$4, &$5, $6);
1358 T_JMP { $$ = AIC_OP_JMP; }
1359 | T_JC { $$ = AIC_OP_JC; }
1360 | T_JNC { $$ = AIC_OP_JNC; }
1361 | T_CALL { $$ = AIC_OP_CALL; }
1365 T_JZ { $$ = AIC_OP_JZ; }
1366 | T_JNZ { $$ = AIC_OP_JNZ; }
1370 T_JE { $$ = AIC_OP_JE; }
1371 | T_JNE { $$ = AIC_OP_JNE; }
1375 jmp_jc_jnc_call address ';'
1379 make_expression(&immed, 0);
1380 format_3_instr($1, &sindex, &immed, &$2);
1385 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1387 type_check(&$2, &$4, AIC_OP_OR);
1388 format_3_instr($5, &$2, &$4, &$6);
1393 T_TEST source ',' immediate_or_a jz_jnz address ';'
1395 format_3_instr($5, &$2, &$4, &$6);
1400 T_CMP source ',' immediate_or_a je_jne address ';'
1402 format_3_instr($5, &$2, &$4, &$6);
1407 T_MOV source jmp_jc_jnc_call address ';'
1411 make_expression(&immed, 0);
1412 format_3_instr($3, &$2, &immed, &$4);
1417 T_MVI immediate jmp_jc_jnc_call address ';'
1419 format_3_instr($3, &allzeros, &$2, &$4);
1426 process_field(int field_type, symbol_t *sym, int value)
1429 * Add the current register to its
1430 * symbol list, if it already exists,
1431 * warn if we are setting it to a
1432 * different value, or in the bit to
1433 * the "allowed bits" of this register.
1435 if (sym->type == UNINITIALIZED) {
1436 sym->type = field_type;
1437 initialize_symbol(sym);
1438 sym->info.finfo->value = value;
1439 if (field_type != ENUM_ENTRY) {
1440 if (field_type != MASK && value == 0) {
1441 stop("Empty Field, or Enum", EX_DATAERR);
1444 sym->info.finfo->value = value;
1445 sym->info.finfo->mask = value;
1446 } else if (field_symbol != NULL) {
1447 sym->info.finfo->mask = field_symbol->info.finfo->value;
1449 sym->info.finfo->mask = 0xFF;
1451 } else if (sym->type != field_type) {
1452 stop("Field definition mirrors a definition of the same "
1453 " name, but a different type", EX_DATAERR);
1455 } else if (value != sym->info.finfo->value) {
1456 stop("Field redefined with a conflicting value", EX_DATAERR);
1459 /* Fail if this symbol is already listed */
1460 if (symlist_search(&(sym->info.finfo->symrefs),
1461 cur_symbol->name) != NULL) {
1462 stop("Field defined multiple times for register", EX_DATAERR);
1465 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1466 SYMLIST_INSERT_HEAD);
1467 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1468 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1469 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1473 initialize_symbol(symbol_t *symbol)
1475 switch (symbol->type) {
1477 stop("Call to initialize_symbol with type field unset",
1484 symbol->info.rinfo =
1485 (struct reg_info *)malloc(sizeof(struct reg_info));
1486 if (symbol->info.rinfo == NULL) {
1487 stop("Can't create register info", EX_SOFTWARE);
1490 memset(symbol->info.rinfo, 0,
1491 sizeof(struct reg_info));
1492 SLIST_INIT(&(symbol->info.rinfo->fields));
1494 * Default to allowing access in all register modes
1495 * or to the mode specified by the SCB or SRAM space
1498 if (scb_or_sram_symbol != NULL)
1499 symbol->info.rinfo->modes =
1500 scb_or_sram_symbol->info.rinfo->modes;
1502 symbol->info.rinfo->modes = ~0;
1505 symbol->info.ainfo =
1506 (struct alias_info *)malloc(sizeof(struct alias_info));
1507 if (symbol->info.ainfo == NULL) {
1508 stop("Can't create alias info", EX_SOFTWARE);
1511 memset(symbol->info.ainfo, 0,
1512 sizeof(struct alias_info));
1518 symbol->info.finfo =
1519 (struct field_info *)malloc(sizeof(struct field_info));
1520 if (symbol->info.finfo == NULL) {
1521 stop("Can't create field info", EX_SOFTWARE);
1524 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1525 SLIST_INIT(&(symbol->info.finfo->symrefs));
1528 case DOWNLOAD_CONST:
1529 symbol->info.cinfo =
1530 (struct const_info *)malloc(sizeof(struct const_info));
1531 if (symbol->info.cinfo == NULL) {
1532 stop("Can't create alias info", EX_SOFTWARE);
1535 memset(symbol->info.cinfo, 0,
1536 sizeof(struct const_info));
1539 symbol->info.linfo =
1540 (struct label_info *)malloc(sizeof(struct label_info));
1541 if (symbol->info.linfo == NULL) {
1542 stop("Can't create label info", EX_SOFTWARE);
1545 memset(symbol->info.linfo, 0,
1546 sizeof(struct label_info));
1549 symbol->info.condinfo =
1550 (struct cond_info *)malloc(sizeof(struct cond_info));
1551 if (symbol->info.condinfo == NULL) {
1552 stop("Can't create conditional info", EX_SOFTWARE);
1555 memset(symbol->info.condinfo, 0,
1556 sizeof(struct cond_info));
1559 symbol->info.macroinfo =
1560 (struct macro_info *)malloc(sizeof(struct macro_info));
1561 if (symbol->info.macroinfo == NULL) {
1562 stop("Can't create macro info", EX_SOFTWARE);
1565 memset(symbol->info.macroinfo, 0,
1566 sizeof(struct macro_info));
1567 STAILQ_INIT(&symbol->info.macroinfo->args);
1570 stop("Call to initialize_symbol with invalid symbol type",
1578 add_macro_arg(const char *argtext, int argnum)
1580 struct macro_arg *marg;
1584 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1585 stop("Invalid current symbol for adding macro arg",
1590 marg = (struct macro_arg *)malloc(sizeof(*marg));
1592 stop("Can't create macro_arg structure", EX_SOFTWARE);
1595 marg->replacement_text = NULL;
1596 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1597 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1599 if (retval >= sizeof(regex_pattern)) {
1600 stop("Regex text buffer too small for arg",
1604 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1606 stop("Regex compilation failed", EX_SOFTWARE);
1609 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1613 add_macro_body(const char *bodytext)
1615 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1616 stop("Invalid current symbol for adding macro arg",
1620 cur_symbol->info.macroinfo->body = strdup(bodytext);
1621 if (cur_symbol->info.macroinfo->body == NULL) {
1622 stop("Can't duplicate macro body text", EX_SOFTWARE);
1628 process_register(symbol_t **p_symbol)
1630 symbol_t *symbol = *p_symbol;
1632 if (symbol->type == UNINITIALIZED) {
1633 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1635 stop(errbuf, EX_DATAERR);
1637 } else if (symbol->type == ALIAS) {
1638 *p_symbol = symbol->info.ainfo->parent;
1639 } else if ((symbol->type != REGISTER)
1640 && (symbol->type != SCBLOC)
1641 && (symbol->type != SRAMLOC)) {
1642 snprintf(errbuf, sizeof(errbuf),
1643 "Specified symbol %s is not a register",
1645 stop(errbuf, EX_DATAERR);
1650 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1651 symbol_ref_t *src, int ret)
1653 struct instruction *instr;
1654 struct ins_format1 *f1_instr;
1656 if (src->symbol == NULL)
1659 /* Test register permissions */
1660 test_writable_symbol(dest->symbol);
1661 test_readable_symbol(src->symbol);
1663 if (!is_location_address(dest->symbol)) {
1664 /* Ensure that immediate makes sense for this destination */
1665 type_check(dest, immed, opcode);
1668 /* Allocate sequencer space for the instruction and fill it out */
1669 instr = seq_alloc();
1670 f1_instr = &instr->format.format1;
1671 f1_instr->ret = ret ? 1 : 0;
1672 f1_instr->opcode = opcode;
1673 f1_instr->destination = dest->symbol->info.rinfo->address
1675 f1_instr->source = src->symbol->info.rinfo->address
1677 f1_instr->immediate = immed->value;
1679 if (is_download_const(immed))
1680 f1_instr->parity = 1;
1681 else if (dest->symbol == mode_ptr.symbol) {
1686 * Attempt to update mode information if
1687 * we are operating on the mode register.
1689 if (src->symbol == allones.symbol)
1691 else if (src->symbol == allzeros.symbol)
1693 else if (src->symbol == mode_ptr.symbol)
1694 src_value = (dst_mode << 4) | src_mode;
1700 dst_value = src_value & immed->value;
1703 dst_value = src_value ^ immed->value;
1706 dst_value = (src_value + immed->value) & 0xFF;
1709 dst_value = src_value | immed->value;
1712 dst_value = src_value;
1717 src_mode = dst_value & 0xF;
1718 dst_mode = (dst_value >> 4) & 0xF;
1722 symlist_free(&immed->referenced_syms);
1727 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1728 symbol_ref_t *src, int ret)
1730 struct instruction *instr;
1731 struct ins_format2 *f2_instr;
1732 uint8_t shift_control;
1734 if (src->symbol == NULL)
1737 /* Test register permissions */
1738 test_writable_symbol(dest->symbol);
1739 test_readable_symbol(src->symbol);
1741 /* Allocate sequencer space for the instruction and fill it out */
1742 instr = seq_alloc();
1743 f2_instr = &instr->format.format2;
1744 f2_instr->ret = ret ? 1 : 0;
1745 f2_instr->opcode = AIC_OP_ROL;
1746 f2_instr->destination = dest->symbol->info.rinfo->address
1748 f2_instr->source = src->symbol->info.rinfo->address
1750 if (places->value > 8 || places->value <= 0) {
1751 stop("illegal shift value", EX_DATAERR);
1756 if (places->value == 8)
1757 shift_control = 0xf0;
1759 shift_control = (places->value << 4) | places->value;
1762 if (places->value == 8) {
1763 shift_control = 0xf8;
1765 shift_control = (places->value << 4)
1766 | (8 - places->value)
1771 shift_control = places->value & 0x7;
1774 shift_control = (8 - places->value) | 0x08;
1777 shift_control = 0; /* Quiet Compiler */
1778 stop("Invalid shift operation specified", EX_SOFTWARE);
1782 f2_instr->shift_control = shift_control;
1783 symlist_free(&places->referenced_syms);
1788 format_3_instr(int opcode, symbol_ref_t *src,
1789 expression_t *immed, symbol_ref_t *address)
1791 struct instruction *instr;
1792 struct ins_format3 *f3_instr;
1795 /* Test register permissions */
1796 test_readable_symbol(src->symbol);
1798 /* Allocate sequencer space for the instruction and fill it out */
1799 instr = seq_alloc();
1800 f3_instr = &instr->format.format3;
1801 if (address->symbol == NULL) {
1802 /* 'dot' referrence. Use the current instruction pointer */
1803 addr = instruction_ptr + address->offset;
1804 } else if (address->symbol->type == UNINITIALIZED) {
1805 /* forward reference */
1806 addr = address->offset;
1807 instr->patch_label = address->symbol;
1809 addr = address->symbol->info.linfo->address + address->offset;
1810 f3_instr->opcode = opcode;
1811 f3_instr->address = addr;
1812 f3_instr->source = src->symbol->info.rinfo->address
1814 f3_instr->immediate = immed->value;
1816 if (is_download_const(immed))
1817 f3_instr->parity = 1;
1819 symlist_free(&immed->referenced_syms);
1824 test_readable_symbol(symbol_t *symbol)
1826 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1827 snprintf(errbuf, sizeof(errbuf),
1828 "Register %s unavailable in source reg mode %d",
1829 symbol->name, src_mode);
1830 stop(errbuf, EX_DATAERR);
1833 if (symbol->info.rinfo->mode == WO) {
1834 stop("Write Only register specified as source",
1841 test_writable_symbol(symbol_t *symbol)
1843 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1844 snprintf(errbuf, sizeof(errbuf),
1845 "Register %s unavailable in destination reg mode %d",
1846 symbol->name, dst_mode);
1847 stop(errbuf, EX_DATAERR);
1850 if (symbol->info.rinfo->mode == RO) {
1851 stop("Read Only register specified as destination",
1858 type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
1860 symbol_t *symbol = sym->symbol;
1861 symbol_node_t *node;
1867 * Make sure that we aren't attempting to write something
1868 * that hasn't been defined. If this is an and operation,
1869 * this is a mask, so "undefined" bits are okay.
1871 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ ||
1872 opcode == AIC_OP_JZ || opcode == AIC_OP_JNE ||
1873 opcode == AIC_OP_BMOV)
1877 * Defaulting to 8 bit logic
1879 mask = (int8_t)~symbol->info.rinfo->valid_bitmask;
1880 value = (int8_t)expression->value;
1882 if (and_op == FALSE && (mask & value) != 0 ) {
1883 snprintf(errbuf, sizeof(errbuf),
1884 "Invalid bit(s) 0x%x in immediate written to %s",
1887 stop(errbuf, EX_DATAERR);
1892 * Now make sure that all of the symbols referenced by the
1893 * expression are defined for this register.
1895 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1896 for(node = expression->referenced_syms.slh_first;
1898 node = node->links.sle_next) {
1899 if ((node->symbol->type == MASK
1900 || node->symbol->type == FIELD
1901 || node->symbol->type == ENUM
1902 || node->symbol->type == ENUM_ENTRY)
1903 && symlist_search(&node->symbol->info.finfo->symrefs,
1904 symbol->name) == NULL) {
1905 snprintf(errbuf, sizeof(errbuf),
1906 "Invalid field or mask %s "
1908 node->symbol->name, symbol->name);
1909 stop(errbuf, EX_DATAERR);
1917 make_expression(expression_t *immed, int value)
1919 SLIST_INIT(&immed->referenced_syms);
1920 immed->value = value & 0xff;
1924 add_conditional(symbol_t *symbol)
1926 static int numfuncs;
1928 if (numfuncs == 0) {
1929 /* add a special conditional, "0" */
1930 symbol_t *false_func;
1932 false_func = symtable_get("0");
1933 if (false_func->type != UNINITIALIZED) {
1934 stop("Conditional expression '0' "
1935 "conflicts with a symbol", EX_DATAERR);
1938 false_func->type = CONDITIONAL;
1939 initialize_symbol(false_func);
1940 false_func->info.condinfo->func_num = numfuncs++;
1941 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1944 /* This condition has occurred before */
1945 if (symbol->type == CONDITIONAL)
1948 if (symbol->type != UNINITIALIZED) {
1949 stop("Conditional expression conflicts with a symbol",
1954 symbol->type = CONDITIONAL;
1955 initialize_symbol(symbol);
1956 symbol->info.condinfo->func_num = numfuncs++;
1957 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1961 add_version(const char *verstring)
1963 const char prefix[] = " * ";
1967 newlen = strlen(verstring) + strlen(prefix);
1969 if (versions != NULL)
1970 oldlen = strlen(versions);
1971 versions = realloc(versions, newlen + oldlen + 2);
1972 if (versions == NULL)
1973 stop("Can't allocate version string", EX_SOFTWARE);
1974 strcpy(&versions[oldlen], prefix);
1975 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1976 versions[newlen + oldlen] = '\n';
1977 versions[newlen + oldlen + 1] = '\0';
1981 yyerror(const char *string)
1983 stop(string, EX_DATAERR);
1987 is_download_const(expression_t *immed)
1989 if ((immed->referenced_syms.slh_first != NULL)
1990 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
1997 is_location_address(symbol_t *sym)
1999 if (sym->type == SCBLOC ||
2000 sym->type == SRAMLOC)