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 "aicasm_symbol.h"
59 #include "aicasm_insformat.h"
62 char stock_prefix[] = "aic_";
63 char *prefix = stock_prefix;
66 static char errbuf[255];
67 static char regex_pattern[255];
68 static symbol_t *cur_symbol;
69 static symbol_t *field_symbol;
70 static symbol_t *scb_or_sram_symbol;
71 static symtype cur_symtype;
72 static symbol_ref_t accumulator;
73 static symbol_ref_t mode_ptr;
74 static symbol_ref_t allones;
75 static symbol_ref_t allzeros;
76 static symbol_ref_t none;
77 static symbol_ref_t sindex;
78 static int instruction_ptr;
80 static int sram_or_scb_offset;
81 static int download_constant_count;
82 static int in_critical_section;
83 static u_int enum_increment;
84 static u_int enum_next_value;
86 static void process_field(int field_type, symbol_t *sym, int mask);
87 static void initialize_symbol(symbol_t *symbol);
88 static void add_macro_arg(const char *argtext, int position);
89 static void add_macro_body(const char *bodytext);
90 static void process_register(symbol_t **p_symbol);
91 static void format_1_instr(int opcode, symbol_ref_t *dest,
92 expression_t *immed, symbol_ref_t *src, int ret);
93 static void format_2_instr(int opcode, symbol_ref_t *dest,
94 expression_t *places, symbol_ref_t *src, int ret);
95 static void format_3_instr(int opcode, symbol_ref_t *src,
96 expression_t *immed, symbol_ref_t *address);
97 static void test_readable_symbol(symbol_t *symbol);
98 static void test_writable_symbol(symbol_t *symbol);
99 static void type_check(symbol_ref_t *sym, expression_t *expression, int and_op);
100 static void make_expression(expression_t *immed, int value);
101 static void add_conditional(symbol_t *symbol);
102 static void add_version(const char *verstring);
103 static int is_download_const(expression_t *immed);
104 static int is_location_address(symbol_t *symbol);
105 void yyerror(const char *string);
107 #define SRAM_SYMNAME "SRAM_BASE"
108 #define SCB_SYMNAME "SCB_BASE"
115 symbol_ref_t sym_ref;
116 expression_t expression;
121 %token <value> T_CONST
139 %token <value> T_ADDRESS
145 %token T_DONT_GENERATE_DEBUG_CODE
151 %token T_SET_SRC_MODE
153 %token T_SET_DST_MODE
155 %token <value> T_MODE
169 %token <value> T_NUMBER
171 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
175 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
177 %token <value> T_SHR T_SHL T_ROR T_ROL
179 %token <value> T_MVI T_MOV T_CLR T_BMOV
181 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
183 %token <value> T_ADD T_ADC
185 %token <value> T_INC T_DEC
187 %token <value> T_STC T_CLC
189 %token <value> T_CMP T_NOT T_XOR
191 %token <value> T_TEST T_AND
195 /* 16 bit extensions, not implemented
196 * %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
197 * %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
203 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
207 %token <sym> T_SYMBOL
211 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
213 %type <sym_ref> reg_symbol address destination source opt_source
215 %type <expression> expression immediate immediate_or_a
217 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
219 %type <value> mode_value mode_list macro_arglist
223 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
236 | program patch_arg_list
246 | program scratch_ram
252 | program set_src_mode
254 | program set_dst_mode
255 | critical_section_start
256 | program critical_section_start
257 | critical_section_end
258 | program critical_section_end
260 | program conditional
266 T_INCLUDE '<' T_PATH '>'
268 include_file($3, BRACKETED_INCLUDE);
270 | T_INCLUDE '"' T_PATH '"'
272 include_file($3, QUOTED_INCLUDE);
277 T_PREFIX '=' T_STRING
279 if (prefix != stock_prefix)
280 stop("Prefix multiply defined",
284 stop("Unable to record prefix", EX_SOFTWARE);
289 T_PATCH_ARG_LIST '=' T_STRING
291 if (patch_arg_list != NULL)
292 stop("Patch argument list multiply defined",
294 patch_arg_list = strdup($3);
295 if (patch_arg_list == NULL)
296 stop("Unable to record patch arg list", EX_SOFTWARE);
301 T_VERSION '=' T_STRING
306 T_REGISTER { cur_symtype = REGISTER; } reg_definition
312 if ($1->type != UNINITIALIZED) {
313 stop("Register multiply defined", EX_DATAERR);
317 cur_symbol->type = cur_symtype;
318 initialize_symbol(cur_symbol);
324 * Default to allowing everything in for registers
325 * with no bit or mask definitions.
327 if (cur_symbol->info.rinfo->valid_bitmask == 0)
328 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
330 if (cur_symbol->info.rinfo->size == 0)
331 cur_symbol->info.rinfo->size = 1;
334 * This might be useful for registers too.
336 if (cur_symbol->type != REGISTER) {
337 if (cur_symbol->info.rinfo->address == 0)
338 cur_symbol->info.rinfo->address =
340 sram_or_scb_offset +=
341 cur_symbol->info.rinfo->size;
349 | reg_attribute_list reg_attribute
357 | dont_generate_debug_code
374 cur_symbol->info.rinfo->address = $2;
381 cur_symbol->info.rinfo->size = $2;
382 if (scb_or_sram_symbol != NULL) {
386 max_addr = scb_or_sram_symbol->info.rinfo->address
387 + scb_or_sram_symbol->info.rinfo->size;
388 sym_max_addr = cur_symbol->info.rinfo->address
389 + cur_symbol->info.rinfo->size;
391 if (sym_max_addr > max_addr)
392 stop("SCB or SRAM space exhausted", EX_DATAERR);
400 cur_symbol->count += $2;
407 cur_symbol->info.rinfo->mode = $2;
411 dont_generate_debug_code:
412 T_DONT_GENERATE_DEBUG_CODE
414 cur_symbol->dont_generate_debug_code = 1;
421 cur_symbol->info.rinfo->modes = $2;
430 | mode_list ',' mode_value
440 stop("Valid register modes range between 0 and 4.",
452 if (symbol->type != CONST) {
453 stop("Only \"const\" symbols allowed in "
454 "mode definitions.", EX_DATAERR);
457 if (symbol->info.cinfo->value > 4) {
458 stop("Valid register modes range between 0 and 4.",
462 $$ = (0x1 << symbol->info.cinfo->value);
473 '{' enum_entry_list '}'
474 | T_FIELD T_SYMBOL expression
476 process_field(FIELD, $2, $3.value);
479 enum_increment = 0x01 << (ffs($3.value) - 1);
481 '{' enum_entry_list '}'
482 | T_FIELD T_SYMBOL expression
484 process_field(FIELD, $2, $3.value);
495 '{' enum_entry_list '}'
496 | T_ENUM T_SYMBOL expression
498 process_field(ENUM, $2, $3.value);
501 enum_increment = 0x01 << (ffs($3.value) - 1);
503 '{' enum_entry_list '}'
508 | enum_entry_list ',' enum_entry
514 process_field(ENUM_ENTRY, $1, enum_next_value);
515 enum_next_value += enum_increment;
517 | T_SYMBOL expression
519 process_field(ENUM_ENTRY, $1, $2.value);
520 enum_next_value = $2.value + enum_increment;
525 T_MASK T_SYMBOL expression
527 process_field(MASK, $2, $3.value);
534 if ($2->type != UNINITIALIZED) {
535 stop("Re-definition of register alias",
540 initialize_symbol($2);
541 $2->info.ainfo->parent = cur_symbol;
548 if (accumulator.symbol != NULL) {
549 stop("Only one accumulator definition allowed",
553 accumulator.symbol = cur_symbol;
560 if (mode_ptr.symbol != NULL) {
561 stop("Only one mode pointer definition allowed",
565 mode_ptr.symbol = cur_symbol;
572 if (allones.symbol != NULL) {
573 stop("Only one definition of allones allowed",
577 allones.symbol = cur_symbol;
584 if (allzeros.symbol != NULL) {
585 stop("Only one definition of allzeros allowed",
589 allzeros.symbol = cur_symbol;
596 if (none.symbol != NULL) {
597 stop("Only one definition of none allowed",
601 none.symbol = cur_symbol;
608 if (sindex.symbol != NULL) {
609 stop("Only one definition of sindex allowed",
613 sindex.symbol = cur_symbol;
618 expression '|' expression
620 $$.value = $1.value | $3.value;
621 symlist_merge(&$$.referenced_syms,
623 &$3.referenced_syms);
625 | expression '&' expression
627 $$.value = $1.value & $3.value;
628 symlist_merge(&$$.referenced_syms,
630 &$3.referenced_syms);
632 | expression '+' expression
634 $$.value = $1.value + $3.value;
635 symlist_merge(&$$.referenced_syms,
637 &$3.referenced_syms);
639 | expression '-' expression
641 $$.value = $1.value - $3.value;
642 symlist_merge(&($$.referenced_syms),
643 &($1.referenced_syms),
644 &($3.referenced_syms));
646 | expression '*' expression
648 $$.value = $1.value * $3.value;
649 symlist_merge(&($$.referenced_syms),
650 &($1.referenced_syms),
651 &($3.referenced_syms));
653 | expression '/' expression
655 $$.value = $1.value / $3.value;
656 symlist_merge(&($$.referenced_syms),
657 &($1.referenced_syms),
658 &($3.referenced_syms));
660 | expression T_EXPR_LSHIFT expression
662 $$.value = $1.value << $3.value;
663 symlist_merge(&$$.referenced_syms,
665 &$3.referenced_syms);
667 | expression T_EXPR_RSHIFT expression
669 $$.value = $1.value >> $3.value;
670 symlist_merge(&$$.referenced_syms,
672 &$3.referenced_syms);
681 $$.value = (~$$.value) & 0xFF;
683 | '-' expression %prec UMINUS
686 $$.value = -$$.value;
691 SLIST_INIT(&$$.referenced_syms);
698 switch (symbol->type) {
700 symbol = $1->info.ainfo->parent;
704 $$.value = symbol->info.rinfo->address;
710 $$.value = symbol->info.finfo->value;
714 $$.value = symbol->info.cinfo->value;
719 snprintf(errbuf, sizeof(errbuf),
720 "Undefined symbol %s referenced",
722 stop(errbuf, EX_DATAERR);
727 SLIST_INIT(&$$.referenced_syms);
728 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
733 T_CONST T_SYMBOL expression
735 if ($2->type != UNINITIALIZED) {
736 stop("Re-definition of symbol as a constant",
741 initialize_symbol($2);
742 $2->info.cinfo->value = $3.value;
744 | T_CONST T_SYMBOL T_DOWNLOAD
747 stop("Invalid downloaded constant declaration",
751 if ($2->type != UNINITIALIZED) {
752 stop("Re-definition of symbol as a downloaded constant",
756 $2->type = DOWNLOAD_CONST;
757 initialize_symbol($2);
758 $2->info.cinfo->value = download_constant_count++;
765 if ($2->type != UNINITIALIZED) {
766 stop("Re-definition of symbol as a macro",
771 cur_symbol->type = MACRO;
772 initialize_symbol(cur_symbol);
777 macrodefn_prologue T_MACROBODY
781 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
784 cur_symbol->info.macroinfo->narg = $3;
790 /* Macros can take no arguments */
796 add_macro_arg($1, 0);
798 | macro_arglist ',' T_ARG
801 stop("Comma without preceding argument in arg list",
806 add_macro_arg($3, $1);
813 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
815 cur_symbol = symtable_get(SRAM_SYMNAME);
816 cur_symtype = SRAMLOC;
817 cur_symbol->type = SRAMLOC;
818 initialize_symbol(cur_symbol);
819 cur_symbol->count += 1;
823 sram_or_scb_offset = cur_symbol->info.rinfo->address;
827 scb_or_sram_symbol = cur_symbol;
829 scb_or_sram_attributes
833 scb_or_sram_symbol = NULL;
840 cur_symbol = symtable_get(SCB_SYMNAME);
841 cur_symtype = SCBLOC;
842 if (cur_symbol->type != UNINITIALIZED) {
843 stop("Only one SRAM definition allowed",
847 cur_symbol->type = SCBLOC;
848 initialize_symbol(cur_symbol);
849 /* 64 bytes of SCB space */
850 cur_symbol->info.rinfo->size = 64;
851 cur_symbol->count += 1;
855 sram_or_scb_offset = cur_symbol->info.rinfo->address;
859 scb_or_sram_symbol = cur_symbol;
861 scb_or_sram_attributes
865 scb_or_sram_symbol = NULL;
869 scb_or_sram_attributes:
870 /* NULL definition is okay */
872 | scb_or_sram_reg_list
873 | modes scb_or_sram_reg_list
876 scb_or_sram_reg_list:
878 | scb_or_sram_reg_list reg_definition
884 process_register(&$1);
888 | T_SYMBOL '[' T_SYMBOL ']'
890 process_register(&$1);
891 if ($3->type != CONST) {
892 stop("register offset must be a constant", EX_DATAERR);
895 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
896 stop("Accessing offset beyond range of register",
901 $$.offset = $3->info.cinfo->value;
903 | T_SYMBOL '[' T_NUMBER ']'
905 process_register(&$1);
906 if (($3 + 1) > $1->info.rinfo->size) {
907 stop("Accessing offset beyond range of register",
916 if (accumulator.symbol == NULL) {
917 stop("No accumulator has been defined", EX_DATAERR);
920 $$.symbol = accumulator.symbol;
928 test_writable_symbol($1.symbol);
941 if ($1.value == 0 && is_download_const(&$1) == 0) {
942 snprintf(errbuf, sizeof(errbuf),
943 "\nExpression evaluates to 0 and thus "
944 "references the accumulator.\n "
945 "If this is the desired effect, use 'A' "
947 stop(errbuf, EX_DATAERR);
953 SLIST_INIT(&$$.referenced_syms);
954 symlist_add(&$$.referenced_syms, accumulator.symbol,
955 SYMLIST_INSERT_HEAD);
963 test_readable_symbol($1.symbol);
984 T_SET_SRC_MODE T_NUMBER ';'
991 T_SET_DST_MODE T_NUMBER ';'
997 critical_section_start:
1000 critical_section_t *cs;
1002 if (in_critical_section != FALSE) {
1003 stop("Critical Section within Critical Section",
1008 cs->begin_addr = instruction_ptr;
1009 in_critical_section = TRUE;
1013 critical_section_end:
1016 critical_section_t *cs;
1018 if (in_critical_section == FALSE) {
1019 stop("Unballanced 'end_cs'", EX_DATAERR);
1022 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1023 cs->end_addr = instruction_ptr;
1024 in_critical_section = FALSE;
1037 if ($2->type != UNINITIALIZED) {
1038 stop("Program label multiply defined", EX_DATAERR);
1042 initialize_symbol($2);
1043 $2->info.linfo->address = instruction_ptr;
1044 $2->info.linfo->exported = $1;
1054 | T_SYMBOL '+' T_NUMBER
1059 | T_SYMBOL '-' T_NUMBER
1086 add_conditional($2);
1087 new_scope = scope_alloc();
1088 new_scope->type = SCOPE_IF;
1089 new_scope->begin_addr = instruction_ptr;
1090 new_scope->func_num = $2->info.condinfo->func_num;
1092 | T_ELSE T_IF T_CEXPR '{'
1095 scope_t *scope_context;
1096 scope_t *last_scope;
1099 * Ensure that the previous scope is either an
1100 * if or and else if.
1102 scope_context = SLIST_FIRST(&scope_stack);
1103 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1105 if (last_scope == NULL
1106 || last_scope->type == T_ELSE) {
1108 stop("'else if' without leading 'if'", EX_DATAERR);
1111 add_conditional($3);
1112 new_scope = scope_alloc();
1113 new_scope->type = SCOPE_ELSE_IF;
1114 new_scope->begin_addr = instruction_ptr;
1115 new_scope->func_num = $3->info.condinfo->func_num;
1120 scope_t *scope_context;
1121 scope_t *last_scope;
1124 * Ensure that the previous scope is either an
1125 * if or and else if.
1127 scope_context = SLIST_FIRST(&scope_stack);
1128 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1130 if (last_scope == NULL
1131 || last_scope->type == SCOPE_ELSE) {
1133 stop("'else' without leading 'if'", EX_DATAERR);
1136 new_scope = scope_alloc();
1137 new_scope->type = SCOPE_ELSE;
1138 new_scope->begin_addr = instruction_ptr;
1145 scope_t *scope_context;
1147 scope_context = SLIST_FIRST(&scope_stack);
1148 if (scope_context->type == SCOPE_ROOT) {
1149 stop("Unexpected '}' encountered", EX_DATAERR);
1153 scope_context->end_addr = instruction_ptr;
1156 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1158 process_scope(scope_context);
1160 if (SLIST_FIRST(&scope_stack) == NULL) {
1161 stop("Unexpected '}' encountered", EX_DATAERR);
1168 T_AND { $$ = AIC_OP_AND; }
1169 | T_XOR { $$ = AIC_OP_XOR; }
1170 | T_ADD { $$ = AIC_OP_ADD; }
1171 | T_ADC { $$ = AIC_OP_ADC; }
1175 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1177 format_1_instr($1, &$2, &$4, &$5, $6);
1182 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1184 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1189 T_INC destination opt_source ret ';'
1193 make_expression(&immed, 1);
1194 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1199 T_DEC destination opt_source ret ';'
1203 make_expression(&immed, -1);
1204 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1213 make_expression(&immed, -1);
1214 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1216 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1218 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1227 make_expression(&immed, 1);
1228 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1230 | T_STC destination ret ';'
1234 make_expression(&immed, 1);
1235 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1240 T_BMOV destination ',' source ',' immediate ret ';'
1242 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1247 T_MOV destination ',' source ret ';'
1251 make_expression(&immed, 1);
1252 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1257 T_MVI destination ',' immediate ret ';'
1260 && is_download_const(&$4) == 0) {
1264 * Allow move immediates of 0 so that macros,
1265 * that can't know the immediate's value and
1266 * otherwise compensate, still work.
1268 make_expression(&immed, 1);
1269 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1271 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1277 T_NOT destination opt_source ret ';'
1281 make_expression(&immed, 0xff);
1282 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1287 T_CLR destination ret ';'
1291 make_expression(&immed, 0xff);
1292 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1301 make_expression(&immed, 0xff);
1302 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1311 make_expression(&immed, 0xff);
1312 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1317 * This grammar differs from the one in the aic7xxx
1318 * reference manual since the grammar listed there is
1319 * ambiguous and causes a shift/reduce conflict.
1320 * It also seems more logical as the "immediate"
1321 * argument is listed as the second arg like the
1326 T_SHL { $$ = AIC_OP_SHL; }
1327 | T_SHR { $$ = AIC_OP_SHR; }
1328 | T_ROL { $$ = AIC_OP_ROL; }
1329 | T_ROR { $$ = AIC_OP_ROR; }
1333 * 16bit opcodes, not used
1336 * T_OR16 { $$ = AIC_OP_OR16; }
1337 *| T_AND16 { $$ = AIC_OP_AND16; }
1338 *| T_XOR16 { $$ = AIC_OP_XOR16; }
1339 *| T_ADD16 { $$ = AIC_OP_ADD16; }
1340 *| T_ADC16 { $$ = AIC_OP_ADC16; }
1341 *| T_MVI16 { $$ = AIC_OP_MVI16; }
1346 f2_opcode destination ',' expression opt_source ret ';'
1348 format_2_instr($1, &$2, &$4, &$5, $6);
1353 T_JMP { $$ = AIC_OP_JMP; }
1354 | T_JC { $$ = AIC_OP_JC; }
1355 | T_JNC { $$ = AIC_OP_JNC; }
1356 | T_CALL { $$ = AIC_OP_CALL; }
1360 T_JZ { $$ = AIC_OP_JZ; }
1361 | T_JNZ { $$ = AIC_OP_JNZ; }
1365 T_JE { $$ = AIC_OP_JE; }
1366 | T_JNE { $$ = AIC_OP_JNE; }
1370 jmp_jc_jnc_call address ';'
1374 make_expression(&immed, 0);
1375 format_3_instr($1, &sindex, &immed, &$2);
1380 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1382 type_check(&$2, &$4, AIC_OP_OR);
1383 format_3_instr($5, &$2, &$4, &$6);
1388 T_TEST source ',' immediate_or_a jz_jnz address ';'
1390 format_3_instr($5, &$2, &$4, &$6);
1395 T_CMP source ',' immediate_or_a je_jne address ';'
1397 format_3_instr($5, &$2, &$4, &$6);
1402 T_MOV source jmp_jc_jnc_call address ';'
1406 make_expression(&immed, 0);
1407 format_3_instr($3, &$2, &immed, &$4);
1412 T_MVI immediate jmp_jc_jnc_call address ';'
1414 format_3_instr($3, &allzeros, &$2, &$4);
1421 process_field(int field_type, symbol_t *sym, int value)
1424 * Add the current register to its
1425 * symbol list, if it already exists,
1426 * warn if we are setting it to a
1427 * different value, or in the bit to
1428 * the "allowed bits" of this register.
1430 if (sym->type == UNINITIALIZED) {
1431 sym->type = field_type;
1432 initialize_symbol(sym);
1433 sym->info.finfo->value = value;
1434 if (field_type != ENUM_ENTRY) {
1435 if (field_type != MASK && value == 0) {
1436 stop("Empty Field, or Enum", EX_DATAERR);
1439 sym->info.finfo->value = value;
1440 sym->info.finfo->mask = value;
1441 } else if (field_symbol != NULL) {
1442 sym->info.finfo->mask = field_symbol->info.finfo->value;
1444 sym->info.finfo->mask = 0xFF;
1446 } else if (sym->type != field_type) {
1447 stop("Field definition mirrors a definition of the same "
1448 " name, but a different type", EX_DATAERR);
1450 } else if (value != sym->info.finfo->value) {
1451 stop("Field redefined with a conflicting value", EX_DATAERR);
1454 /* Fail if this symbol is already listed */
1455 if (symlist_search(&(sym->info.finfo->symrefs),
1456 cur_symbol->name) != NULL) {
1457 stop("Field defined multiple times for register", EX_DATAERR);
1460 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1461 SYMLIST_INSERT_HEAD);
1462 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1463 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1464 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1468 initialize_symbol(symbol_t *symbol)
1470 switch (symbol->type) {
1472 stop("Call to initialize_symbol with type field unset",
1479 symbol->info.rinfo =
1480 (struct reg_info *)malloc(sizeof(struct reg_info));
1481 if (symbol->info.rinfo == NULL) {
1482 stop("Can't create register info", EX_SOFTWARE);
1485 memset(symbol->info.rinfo, 0,
1486 sizeof(struct reg_info));
1487 SLIST_INIT(&(symbol->info.rinfo->fields));
1489 * Default to allowing access in all register modes
1490 * or to the mode specified by the SCB or SRAM space
1493 if (scb_or_sram_symbol != NULL)
1494 symbol->info.rinfo->modes =
1495 scb_or_sram_symbol->info.rinfo->modes;
1497 symbol->info.rinfo->modes = ~0;
1500 symbol->info.ainfo =
1501 (struct alias_info *)malloc(sizeof(struct alias_info));
1502 if (symbol->info.ainfo == NULL) {
1503 stop("Can't create alias info", EX_SOFTWARE);
1506 memset(symbol->info.ainfo, 0,
1507 sizeof(struct alias_info));
1513 symbol->info.finfo =
1514 (struct field_info *)malloc(sizeof(struct field_info));
1515 if (symbol->info.finfo == NULL) {
1516 stop("Can't create field info", EX_SOFTWARE);
1519 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1520 SLIST_INIT(&(symbol->info.finfo->symrefs));
1523 case DOWNLOAD_CONST:
1524 symbol->info.cinfo =
1525 (struct const_info *)malloc(sizeof(struct const_info));
1526 if (symbol->info.cinfo == NULL) {
1527 stop("Can't create alias info", EX_SOFTWARE);
1530 memset(symbol->info.cinfo, 0,
1531 sizeof(struct const_info));
1534 symbol->info.linfo =
1535 (struct label_info *)malloc(sizeof(struct label_info));
1536 if (symbol->info.linfo == NULL) {
1537 stop("Can't create label info", EX_SOFTWARE);
1540 memset(symbol->info.linfo, 0,
1541 sizeof(struct label_info));
1544 symbol->info.condinfo =
1545 (struct cond_info *)malloc(sizeof(struct cond_info));
1546 if (symbol->info.condinfo == NULL) {
1547 stop("Can't create conditional info", EX_SOFTWARE);
1550 memset(symbol->info.condinfo, 0,
1551 sizeof(struct cond_info));
1554 symbol->info.macroinfo =
1555 (struct macro_info *)malloc(sizeof(struct macro_info));
1556 if (symbol->info.macroinfo == NULL) {
1557 stop("Can't create macro info", EX_SOFTWARE);
1560 memset(symbol->info.macroinfo, 0,
1561 sizeof(struct macro_info));
1562 STAILQ_INIT(&symbol->info.macroinfo->args);
1565 stop("Call to initialize_symbol with invalid symbol type",
1573 add_macro_arg(const char *argtext, int argnum)
1575 struct macro_arg *marg;
1579 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1580 stop("Invalid current symbol for adding macro arg",
1585 marg = (struct macro_arg *)malloc(sizeof(*marg));
1587 stop("Can't create macro_arg structure", EX_SOFTWARE);
1590 marg->replacement_text = NULL;
1591 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1592 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1594 if (retval >= sizeof(regex_pattern)) {
1595 stop("Regex text buffer too small for arg",
1599 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1601 stop("Regex compilation failed", EX_SOFTWARE);
1604 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1608 add_macro_body(const char *bodytext)
1610 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1611 stop("Invalid current symbol for adding macro arg",
1615 cur_symbol->info.macroinfo->body = strdup(bodytext);
1616 if (cur_symbol->info.macroinfo->body == NULL) {
1617 stop("Can't duplicate macro body text", EX_SOFTWARE);
1623 process_register(symbol_t **p_symbol)
1625 symbol_t *symbol = *p_symbol;
1627 if (symbol->type == UNINITIALIZED) {
1628 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1630 stop(errbuf, EX_DATAERR);
1632 } else if (symbol->type == ALIAS) {
1633 *p_symbol = symbol->info.ainfo->parent;
1634 } else if ((symbol->type != REGISTER)
1635 && (symbol->type != SCBLOC)
1636 && (symbol->type != SRAMLOC)) {
1637 snprintf(errbuf, sizeof(errbuf),
1638 "Specified symbol %s is not a register",
1640 stop(errbuf, EX_DATAERR);
1645 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1646 symbol_ref_t *src, int ret)
1648 struct instruction *instr;
1649 struct ins_format1 *f1_instr;
1651 if (src->symbol == NULL)
1654 /* Test register permissions */
1655 test_writable_symbol(dest->symbol);
1656 test_readable_symbol(src->symbol);
1658 if (!is_location_address(dest->symbol)) {
1659 /* Ensure that immediate makes sense for this destination */
1660 type_check(dest, immed, opcode);
1663 /* Allocate sequencer space for the instruction and fill it out */
1664 instr = seq_alloc();
1665 f1_instr = &instr->format.format1;
1666 f1_instr->ret = ret ? 1 : 0;
1667 f1_instr->opcode = opcode;
1668 f1_instr->destination = dest->symbol->info.rinfo->address
1670 f1_instr->source = src->symbol->info.rinfo->address
1672 f1_instr->immediate = immed->value;
1674 if (is_download_const(immed))
1675 f1_instr->parity = 1;
1676 else if (dest->symbol == mode_ptr.symbol) {
1681 * Attempt to update mode information if
1682 * we are operating on the mode register.
1684 if (src->symbol == allones.symbol)
1686 else if (src->symbol == allzeros.symbol)
1688 else if (src->symbol == mode_ptr.symbol)
1689 src_value = (dst_mode << 4) | src_mode;
1695 dst_value = src_value & immed->value;
1698 dst_value = src_value ^ immed->value;
1701 dst_value = (src_value + immed->value) & 0xFF;
1704 dst_value = src_value | immed->value;
1707 dst_value = src_value;
1712 src_mode = dst_value & 0xF;
1713 dst_mode = (dst_value >> 4) & 0xF;
1717 symlist_free(&immed->referenced_syms);
1722 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1723 symbol_ref_t *src, int ret)
1725 struct instruction *instr;
1726 struct ins_format2 *f2_instr;
1727 uint8_t shift_control;
1729 if (src->symbol == NULL)
1732 /* Test register permissions */
1733 test_writable_symbol(dest->symbol);
1734 test_readable_symbol(src->symbol);
1736 /* Allocate sequencer space for the instruction and fill it out */
1737 instr = seq_alloc();
1738 f2_instr = &instr->format.format2;
1739 f2_instr->ret = ret ? 1 : 0;
1740 f2_instr->opcode = AIC_OP_ROL;
1741 f2_instr->destination = dest->symbol->info.rinfo->address
1743 f2_instr->source = src->symbol->info.rinfo->address
1745 if (places->value > 8 || places->value <= 0) {
1746 stop("illegal shift value", EX_DATAERR);
1751 if (places->value == 8)
1752 shift_control = 0xf0;
1754 shift_control = (places->value << 4) | places->value;
1757 if (places->value == 8) {
1758 shift_control = 0xf8;
1760 shift_control = (places->value << 4)
1761 | (8 - places->value)
1766 shift_control = places->value & 0x7;
1769 shift_control = (8 - places->value) | 0x08;
1772 shift_control = 0; /* Quiet Compiler */
1773 stop("Invalid shift operation specified", EX_SOFTWARE);
1777 f2_instr->shift_control = shift_control;
1778 symlist_free(&places->referenced_syms);
1783 format_3_instr(int opcode, symbol_ref_t *src,
1784 expression_t *immed, symbol_ref_t *address)
1786 struct instruction *instr;
1787 struct ins_format3 *f3_instr;
1790 /* Test register permissions */
1791 test_readable_symbol(src->symbol);
1793 /* Allocate sequencer space for the instruction and fill it out */
1794 instr = seq_alloc();
1795 f3_instr = &instr->format.format3;
1796 if (address->symbol == NULL) {
1797 /* 'dot' reference. Use the current instruction pointer */
1798 addr = instruction_ptr + address->offset;
1799 } else if (address->symbol->type == UNINITIALIZED) {
1800 /* forward reference */
1801 addr = address->offset;
1802 instr->patch_label = address->symbol;
1804 addr = address->symbol->info.linfo->address + address->offset;
1805 f3_instr->opcode = opcode;
1806 f3_instr->address = addr;
1807 f3_instr->source = src->symbol->info.rinfo->address
1809 f3_instr->immediate = immed->value;
1811 if (is_download_const(immed))
1812 f3_instr->parity = 1;
1814 symlist_free(&immed->referenced_syms);
1819 test_readable_symbol(symbol_t *symbol)
1821 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1822 snprintf(errbuf, sizeof(errbuf),
1823 "Register %s unavailable in source reg mode %d",
1824 symbol->name, src_mode);
1825 stop(errbuf, EX_DATAERR);
1828 if (symbol->info.rinfo->mode == WO) {
1829 stop("Write Only register specified as source",
1836 test_writable_symbol(symbol_t *symbol)
1838 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1839 snprintf(errbuf, sizeof(errbuf),
1840 "Register %s unavailable in destination reg mode %d",
1841 symbol->name, dst_mode);
1842 stop(errbuf, EX_DATAERR);
1845 if (symbol->info.rinfo->mode == RO) {
1846 stop("Read Only register specified as destination",
1853 type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
1855 symbol_t *symbol = sym->symbol;
1856 symbol_node_t *node;
1862 * Make sure that we aren't attempting to write something
1863 * that hasn't been defined. If this is an and operation,
1864 * this is a mask, so "undefined" bits are okay.
1866 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ ||
1867 opcode == AIC_OP_JZ || opcode == AIC_OP_JNE ||
1868 opcode == AIC_OP_BMOV)
1872 * Defaulting to 8 bit logic
1874 mask = (int8_t)~symbol->info.rinfo->valid_bitmask;
1875 value = (int8_t)expression->value;
1877 if (and_op == FALSE && (mask & value) != 0 ) {
1878 snprintf(errbuf, sizeof(errbuf),
1879 "Invalid bit(s) 0x%x in immediate written to %s",
1882 stop(errbuf, EX_DATAERR);
1887 * Now make sure that all of the symbols referenced by the
1888 * expression are defined for this register.
1890 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1891 for(node = expression->referenced_syms.slh_first;
1893 node = node->links.sle_next) {
1894 if ((node->symbol->type == MASK
1895 || node->symbol->type == FIELD
1896 || node->symbol->type == ENUM
1897 || node->symbol->type == ENUM_ENTRY)
1898 && symlist_search(&node->symbol->info.finfo->symrefs,
1899 symbol->name) == NULL) {
1900 snprintf(errbuf, sizeof(errbuf),
1901 "Invalid field or mask %s "
1903 node->symbol->name, symbol->name);
1904 stop(errbuf, EX_DATAERR);
1912 make_expression(expression_t *immed, int value)
1914 SLIST_INIT(&immed->referenced_syms);
1915 immed->value = value & 0xff;
1919 add_conditional(symbol_t *symbol)
1921 static int numfuncs;
1923 if (numfuncs == 0) {
1924 /* add a special conditional, "0" */
1925 symbol_t *false_func;
1927 false_func = symtable_get("0");
1928 if (false_func->type != UNINITIALIZED) {
1929 stop("Conditional expression '0' "
1930 "conflicts with a symbol", EX_DATAERR);
1933 false_func->type = CONDITIONAL;
1934 initialize_symbol(false_func);
1935 false_func->info.condinfo->func_num = numfuncs++;
1936 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1939 /* This condition has occurred before */
1940 if (symbol->type == CONDITIONAL)
1943 if (symbol->type != UNINITIALIZED) {
1944 stop("Conditional expression conflicts with a symbol",
1949 symbol->type = CONDITIONAL;
1950 initialize_symbol(symbol);
1951 symbol->info.condinfo->func_num = numfuncs++;
1952 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1956 add_version(const char *verstring)
1958 const char prefix[] = " * ";
1962 newlen = strlen(verstring) + strlen(prefix);
1964 if (versions != NULL)
1965 oldlen = strlen(versions);
1966 versions = realloc(versions, newlen + oldlen + 2);
1967 if (versions == NULL)
1968 stop("Can't allocate version string", EX_SOFTWARE);
1969 strcpy(&versions[oldlen], prefix);
1970 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1971 versions[newlen + oldlen] = '\n';
1972 versions[newlen + oldlen + 1] = '\0';
1976 yyerror(const char *string)
1978 stop(string, EX_DATAERR);
1982 is_download_const(expression_t *immed)
1984 if ((immed->referenced_syms.slh_first != NULL)
1985 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
1992 is_location_address(symbol_t *sym)
1994 if (sym->type == SCBLOC ||
1995 sym->type == SRAMLOC)