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
154 %token T_SET_SRC_MODE
156 %token T_SET_DST_MODE
158 %token <value> T_MODE
172 %token <value> T_NUMBER
174 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
178 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
180 %token <value> T_SHR T_SHL T_ROR T_ROL
182 %token <value> T_MVI T_MOV T_CLR T_BMOV
184 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
186 %token <value> T_ADD T_ADC
188 %token <value> T_INC T_DEC
190 %token <value> T_STC T_CLC
192 %token <value> T_CMP T_NOT T_XOR
194 %token <value> T_TEST T_AND
198 /* 16 bit extensions, not implemented
199 * %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
200 * %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
206 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
210 %token <sym> T_SYMBOL
214 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
216 %type <sym_ref> reg_symbol address destination source opt_source
218 %type <expression> expression immediate immediate_or_a
220 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
222 %type <value> mode_value mode_list macro_arglist
226 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
239 | program patch_arg_list
249 | program scratch_ram
255 | program set_src_mode
257 | program set_dst_mode
258 | critical_section_start
259 | program critical_section_start
260 | critical_section_end
261 | program critical_section_end
263 | program conditional
269 T_INCLUDE '<' T_PATH '>'
271 include_file($3, BRACKETED_INCLUDE);
273 | T_INCLUDE '"' T_PATH '"'
275 include_file($3, QUOTED_INCLUDE);
280 T_PREFIX '=' T_STRING
282 if (prefix != stock_prefix)
283 stop("Prefix multiply defined",
287 stop("Unable to record prefix", EX_SOFTWARE);
292 T_PATCH_ARG_LIST '=' T_STRING
294 if (patch_arg_list != NULL)
295 stop("Patch argument list multiply defined",
297 patch_arg_list = strdup($3);
298 if (patch_arg_list == NULL)
299 stop("Unable to record patch arg list", EX_SOFTWARE);
304 T_VERSION '=' T_STRING
309 T_REGISTER { cur_symtype = REGISTER; } reg_definition
315 if ($1->type != UNINITIALIZED) {
316 stop("Register multiply defined", EX_DATAERR);
320 cur_symbol->type = cur_symtype;
321 initialize_symbol(cur_symbol);
327 * Default to allowing everything in for registers
328 * with no bit or mask definitions.
330 if (cur_symbol->info.rinfo->valid_bitmask == 0)
331 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
333 if (cur_symbol->info.rinfo->size == 0)
334 cur_symbol->info.rinfo->size = 1;
337 * This might be useful for registers too.
339 if (cur_symbol->type != REGISTER) {
340 if (cur_symbol->info.rinfo->address == 0)
341 cur_symbol->info.rinfo->address =
343 sram_or_scb_offset +=
344 cur_symbol->info.rinfo->size;
352 | reg_attribute_list reg_attribute
376 cur_symbol->info.rinfo->address = $2;
383 cur_symbol->info.rinfo->size = $2;
384 if (scb_or_sram_symbol != NULL) {
388 max_addr = scb_or_sram_symbol->info.rinfo->address
389 + scb_or_sram_symbol->info.rinfo->size;
390 sym_max_addr = cur_symbol->info.rinfo->address
391 + cur_symbol->info.rinfo->size;
393 if (sym_max_addr > max_addr)
394 stop("SCB or SRAM space exhausted", EX_DATAERR);
402 cur_symbol->count += $2;
409 cur_symbol->info.rinfo->mode = $2;
416 cur_symbol->info.rinfo->modes = $2;
425 | mode_list ',' mode_value
435 stop("Valid register modes range between 0 and 4.",
447 if (symbol->type != CONST) {
448 stop("Only \"const\" symbols allowed in "
449 "mode definitions.", EX_DATAERR);
452 if (symbol->info.cinfo->value > 4) {
453 stop("Valid register modes range between 0 and 4.",
457 $$ = (0x1 << symbol->info.cinfo->value);
468 '{' enum_entry_list '}'
469 | T_FIELD T_SYMBOL expression
471 process_field(FIELD, $2, $3.value);
474 enum_increment = 0x01 << (ffs($3.value) - 1);
476 '{' enum_entry_list '}'
477 | T_FIELD T_SYMBOL expression
479 process_field(FIELD, $2, $3.value);
490 '{' enum_entry_list '}'
491 | T_ENUM T_SYMBOL expression
493 process_field(ENUM, $2, $3.value);
496 enum_increment = 0x01 << (ffs($3.value) - 1);
498 '{' enum_entry_list '}'
503 | enum_entry_list ',' enum_entry
509 process_field(ENUM_ENTRY, $1, enum_next_value);
510 enum_next_value += enum_increment;
512 | T_SYMBOL expression
514 process_field(ENUM_ENTRY, $1, $2.value);
515 enum_next_value = $2.value + enum_increment;
520 T_MASK T_SYMBOL expression
522 process_field(MASK, $2, $3.value);
529 if ($2->type != UNINITIALIZED) {
530 stop("Re-definition of register alias",
535 initialize_symbol($2);
536 $2->info.ainfo->parent = cur_symbol;
543 if (accumulator.symbol != NULL) {
544 stop("Only one accumulator definition allowed",
548 accumulator.symbol = cur_symbol;
555 if (mode_ptr.symbol != NULL) {
556 stop("Only one mode pointer definition allowed",
560 mode_ptr.symbol = cur_symbol;
567 if (allones.symbol != NULL) {
568 stop("Only one definition of allones allowed",
572 allones.symbol = cur_symbol;
579 if (allzeros.symbol != NULL) {
580 stop("Only one definition of allzeros allowed",
584 allzeros.symbol = cur_symbol;
591 if (none.symbol != NULL) {
592 stop("Only one definition of none allowed",
596 none.symbol = cur_symbol;
603 if (sindex.symbol != NULL) {
604 stop("Only one definition of sindex allowed",
608 sindex.symbol = cur_symbol;
613 expression '|' expression
615 $$.value = $1.value | $3.value;
616 symlist_merge(&$$.referenced_syms,
618 &$3.referenced_syms);
620 | expression '&' expression
622 $$.value = $1.value & $3.value;
623 symlist_merge(&$$.referenced_syms,
625 &$3.referenced_syms);
627 | expression '+' expression
629 $$.value = $1.value + $3.value;
630 symlist_merge(&$$.referenced_syms,
632 &$3.referenced_syms);
634 | expression '-' expression
636 $$.value = $1.value - $3.value;
637 symlist_merge(&($$.referenced_syms),
638 &($1.referenced_syms),
639 &($3.referenced_syms));
641 | expression '*' expression
643 $$.value = $1.value * $3.value;
644 symlist_merge(&($$.referenced_syms),
645 &($1.referenced_syms),
646 &($3.referenced_syms));
648 | expression '/' expression
650 $$.value = $1.value / $3.value;
651 symlist_merge(&($$.referenced_syms),
652 &($1.referenced_syms),
653 &($3.referenced_syms));
655 | expression T_EXPR_LSHIFT expression
657 $$.value = $1.value << $3.value;
658 symlist_merge(&$$.referenced_syms,
660 &$3.referenced_syms);
662 | expression T_EXPR_RSHIFT expression
664 $$.value = $1.value >> $3.value;
665 symlist_merge(&$$.referenced_syms,
667 &$3.referenced_syms);
676 $$.value = (~$$.value) & 0xFF;
678 | '-' expression %prec UMINUS
681 $$.value = -$$.value;
686 SLIST_INIT(&$$.referenced_syms);
693 switch (symbol->type) {
695 symbol = $1->info.ainfo->parent;
699 $$.value = symbol->info.rinfo->address;
705 $$.value = symbol->info.finfo->value;
709 $$.value = symbol->info.cinfo->value;
714 snprintf(errbuf, sizeof(errbuf),
715 "Undefined symbol %s referenced",
717 stop(errbuf, EX_DATAERR);
722 SLIST_INIT(&$$.referenced_syms);
723 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
728 T_CONST T_SYMBOL expression
730 if ($2->type != UNINITIALIZED) {
731 stop("Re-definition of symbol as a constant",
736 initialize_symbol($2);
737 $2->info.cinfo->value = $3.value;
739 | T_CONST T_SYMBOL T_DOWNLOAD
742 stop("Invalid downloaded constant declaration",
746 if ($2->type != UNINITIALIZED) {
747 stop("Re-definition of symbol as a downloaded constant",
751 $2->type = DOWNLOAD_CONST;
752 initialize_symbol($2);
753 $2->info.cinfo->value = download_constant_count++;
760 if ($2->type != UNINITIALIZED) {
761 stop("Re-definition of symbol as a macro",
766 cur_symbol->type = MACRO;
767 initialize_symbol(cur_symbol);
772 macrodefn_prologue T_MACROBODY
776 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
779 cur_symbol->info.macroinfo->narg = $3;
785 /* Macros can take no arguments */
791 add_macro_arg($1, 0);
793 | macro_arglist ',' T_ARG
796 stop("Comma without preceeding argument in arg list",
801 add_macro_arg($3, $1);
808 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
810 cur_symbol = symtable_get(SRAM_SYMNAME);
811 cur_symtype = SRAMLOC;
812 cur_symbol->type = SRAMLOC;
813 initialize_symbol(cur_symbol);
814 cur_symbol->count += 1;
818 sram_or_scb_offset = cur_symbol->info.rinfo->address;
822 scb_or_sram_symbol = cur_symbol;
824 scb_or_sram_attributes
828 scb_or_sram_symbol = NULL;
835 cur_symbol = symtable_get(SCB_SYMNAME);
836 cur_symtype = SCBLOC;
837 if (cur_symbol->type != UNINITIALIZED) {
838 stop("Only one SRAM definition allowed",
842 cur_symbol->type = SCBLOC;
843 initialize_symbol(cur_symbol);
844 /* 64 bytes of SCB space */
845 cur_symbol->info.rinfo->size = 64;
846 cur_symbol->count += 1;
850 sram_or_scb_offset = cur_symbol->info.rinfo->address;
854 scb_or_sram_symbol = cur_symbol;
856 scb_or_sram_attributes
860 scb_or_sram_symbol = NULL;
864 scb_or_sram_attributes:
865 /* NULL definition is okay */
867 | scb_or_sram_reg_list
868 | modes scb_or_sram_reg_list
871 scb_or_sram_reg_list:
873 | scb_or_sram_reg_list reg_definition
879 process_register(&$1);
883 | T_SYMBOL '[' T_SYMBOL ']'
885 process_register(&$1);
886 if ($3->type != CONST) {
887 stop("register offset must be a constant", EX_DATAERR);
890 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
891 stop("Accessing offset beyond range of register",
896 $$.offset = $3->info.cinfo->value;
898 | T_SYMBOL '[' T_NUMBER ']'
900 process_register(&$1);
901 if (($3 + 1) > $1->info.rinfo->size) {
902 stop("Accessing offset beyond range of register",
911 if (accumulator.symbol == NULL) {
912 stop("No accumulator has been defined", EX_DATAERR);
915 $$.symbol = accumulator.symbol;
923 test_writable_symbol($1.symbol);
936 if ($1.value == 0 && is_download_const(&$1) == 0) {
937 snprintf(errbuf, sizeof(errbuf),
938 "\nExpression evaluates to 0 and thus "
939 "references the accumulator.\n "
940 "If this is the desired effect, use 'A' "
942 stop(errbuf, EX_DATAERR);
948 SLIST_INIT(&$$.referenced_syms);
949 symlist_add(&$$.referenced_syms, accumulator.symbol,
950 SYMLIST_INSERT_HEAD);
958 test_readable_symbol($1.symbol);
979 T_SET_SRC_MODE T_NUMBER ';'
986 T_SET_DST_MODE T_NUMBER ';'
992 critical_section_start:
995 critical_section_t *cs;
997 if (in_critical_section != FALSE) {
998 stop("Critical Section within Critical Section",
1003 cs->begin_addr = instruction_ptr;
1004 in_critical_section = TRUE;
1008 critical_section_end:
1011 critical_section_t *cs;
1013 if (in_critical_section == FALSE) {
1014 stop("Unballanced 'end_cs'", EX_DATAERR);
1017 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1018 cs->end_addr = instruction_ptr;
1019 in_critical_section = FALSE;
1032 if ($2->type != UNINITIALIZED) {
1033 stop("Program label multiply defined", EX_DATAERR);
1037 initialize_symbol($2);
1038 $2->info.linfo->address = instruction_ptr;
1039 $2->info.linfo->exported = $1;
1049 | T_SYMBOL '+' T_NUMBER
1054 | T_SYMBOL '-' T_NUMBER
1081 add_conditional($2);
1082 new_scope = scope_alloc();
1083 new_scope->type = SCOPE_IF;
1084 new_scope->begin_addr = instruction_ptr;
1085 new_scope->func_num = $2->info.condinfo->func_num;
1087 | T_ELSE T_IF T_CEXPR '{'
1090 scope_t *scope_context;
1091 scope_t *last_scope;
1094 * Ensure that the previous scope is either an
1095 * if or and else if.
1097 scope_context = SLIST_FIRST(&scope_stack);
1098 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1100 if (last_scope == NULL
1101 || last_scope->type == T_ELSE) {
1103 stop("'else if' without leading 'if'", EX_DATAERR);
1106 add_conditional($3);
1107 new_scope = scope_alloc();
1108 new_scope->type = SCOPE_ELSE_IF;
1109 new_scope->begin_addr = instruction_ptr;
1110 new_scope->func_num = $3->info.condinfo->func_num;
1115 scope_t *scope_context;
1116 scope_t *last_scope;
1119 * Ensure that the previous scope is either an
1120 * if or and else if.
1122 scope_context = SLIST_FIRST(&scope_stack);
1123 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1125 if (last_scope == NULL
1126 || last_scope->type == SCOPE_ELSE) {
1128 stop("'else' without leading 'if'", EX_DATAERR);
1131 new_scope = scope_alloc();
1132 new_scope->type = SCOPE_ELSE;
1133 new_scope->begin_addr = instruction_ptr;
1140 scope_t *scope_context;
1142 scope_context = SLIST_FIRST(&scope_stack);
1143 if (scope_context->type == SCOPE_ROOT) {
1144 stop("Unexpected '}' encountered", EX_DATAERR);
1148 scope_context->end_addr = instruction_ptr;
1151 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1153 process_scope(scope_context);
1155 if (SLIST_FIRST(&scope_stack) == NULL) {
1156 stop("Unexpected '}' encountered", EX_DATAERR);
1163 T_AND { $$ = AIC_OP_AND; }
1164 | T_XOR { $$ = AIC_OP_XOR; }
1165 | T_ADD { $$ = AIC_OP_ADD; }
1166 | T_ADC { $$ = AIC_OP_ADC; }
1170 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1172 format_1_instr($1, &$2, &$4, &$5, $6);
1177 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1179 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1184 T_INC destination opt_source ret ';'
1188 make_expression(&immed, 1);
1189 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1194 T_DEC destination opt_source ret ';'
1198 make_expression(&immed, -1);
1199 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1208 make_expression(&immed, -1);
1209 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1211 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1213 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1222 make_expression(&immed, 1);
1223 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1225 | T_STC destination ret ';'
1229 make_expression(&immed, 1);
1230 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1235 T_BMOV destination ',' source ',' immediate ret ';'
1237 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1242 T_MOV destination ',' source ret ';'
1246 make_expression(&immed, 1);
1247 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1252 T_MVI destination ',' immediate ret ';'
1255 && is_download_const(&$4) == 0) {
1259 * Allow move immediates of 0 so that macros,
1260 * that can't know the immediate's value and
1261 * otherwise compensate, still work.
1263 make_expression(&immed, 1);
1264 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1266 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1272 T_NOT destination opt_source ret ';'
1276 make_expression(&immed, 0xff);
1277 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1282 T_CLR destination ret ';'
1286 make_expression(&immed, 0xff);
1287 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1296 make_expression(&immed, 0xff);
1297 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1306 make_expression(&immed, 0xff);
1307 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1312 * This grammer differs from the one in the aic7xxx
1313 * reference manual since the grammer listed there is
1314 * ambiguous and causes a shift/reduce conflict.
1315 * It also seems more logical as the "immediate"
1316 * argument is listed as the second arg like the
1321 T_SHL { $$ = AIC_OP_SHL; }
1322 | T_SHR { $$ = AIC_OP_SHR; }
1323 | T_ROL { $$ = AIC_OP_ROL; }
1324 | T_ROR { $$ = AIC_OP_ROR; }
1328 * 16bit opcodes, not used
1331 * T_OR16 { $$ = AIC_OP_OR16; }
1332 *| T_AND16 { $$ = AIC_OP_AND16; }
1333 *| T_XOR16 { $$ = AIC_OP_XOR16; }
1334 *| T_ADD16 { $$ = AIC_OP_ADD16; }
1335 *| T_ADC16 { $$ = AIC_OP_ADC16; }
1336 *| T_MVI16 { $$ = AIC_OP_MVI16; }
1341 f2_opcode destination ',' expression opt_source ret ';'
1343 format_2_instr($1, &$2, &$4, &$5, $6);
1348 T_JMP { $$ = AIC_OP_JMP; }
1349 | T_JC { $$ = AIC_OP_JC; }
1350 | T_JNC { $$ = AIC_OP_JNC; }
1351 | T_CALL { $$ = AIC_OP_CALL; }
1355 T_JZ { $$ = AIC_OP_JZ; }
1356 | T_JNZ { $$ = AIC_OP_JNZ; }
1360 T_JE { $$ = AIC_OP_JE; }
1361 | T_JNE { $$ = AIC_OP_JNE; }
1365 jmp_jc_jnc_call address ';'
1369 make_expression(&immed, 0);
1370 format_3_instr($1, &sindex, &immed, &$2);
1375 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1377 type_check(&$2, &$4, AIC_OP_OR);
1378 format_3_instr($5, &$2, &$4, &$6);
1383 T_TEST source ',' immediate_or_a jz_jnz address ';'
1385 format_3_instr($5, &$2, &$4, &$6);
1390 T_CMP source ',' immediate_or_a je_jne address ';'
1392 format_3_instr($5, &$2, &$4, &$6);
1397 T_MOV source jmp_jc_jnc_call address ';'
1401 make_expression(&immed, 0);
1402 format_3_instr($3, &$2, &immed, &$4);
1407 T_MVI immediate jmp_jc_jnc_call address ';'
1409 format_3_instr($3, &allzeros, &$2, &$4);
1416 process_field(int field_type, symbol_t *sym, int value)
1419 * Add the current register to its
1420 * symbol list, if it already exists,
1421 * warn if we are setting it to a
1422 * different value, or in the bit to
1423 * the "allowed bits" of this register.
1425 if (sym->type == UNINITIALIZED) {
1426 sym->type = field_type;
1427 initialize_symbol(sym);
1428 sym->info.finfo->value = value;
1429 if (field_type != ENUM_ENTRY) {
1430 if (field_type != MASK && value == 0) {
1431 stop("Empty Field, or Enum", EX_DATAERR);
1434 sym->info.finfo->value = value;
1435 sym->info.finfo->mask = value;
1436 } else if (field_symbol != NULL) {
1437 sym->info.finfo->mask = field_symbol->info.finfo->value;
1439 sym->info.finfo->mask = 0xFF;
1441 } else if (sym->type != field_type) {
1442 stop("Field definition mirrors a definition of the same "
1443 " name, but a different type", EX_DATAERR);
1445 } else if (value != sym->info.finfo->value) {
1446 stop("Field redefined with a conflicting value", EX_DATAERR);
1449 /* Fail if this symbol is already listed */
1450 if (symlist_search(&(sym->info.finfo->symrefs),
1451 cur_symbol->name) != NULL) {
1452 stop("Field defined multiple times for register", EX_DATAERR);
1455 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1456 SYMLIST_INSERT_HEAD);
1457 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1458 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1459 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1463 initialize_symbol(symbol_t *symbol)
1465 switch (symbol->type) {
1467 stop("Call to initialize_symbol with type field unset",
1474 symbol->info.rinfo =
1475 (struct reg_info *)malloc(sizeof(struct reg_info));
1476 if (symbol->info.rinfo == NULL) {
1477 stop("Can't create register info", EX_SOFTWARE);
1480 memset(symbol->info.rinfo, 0,
1481 sizeof(struct reg_info));
1482 SLIST_INIT(&(symbol->info.rinfo->fields));
1484 * Default to allowing access in all register modes
1485 * or to the mode specified by the SCB or SRAM space
1488 if (scb_or_sram_symbol != NULL)
1489 symbol->info.rinfo->modes =
1490 scb_or_sram_symbol->info.rinfo->modes;
1492 symbol->info.rinfo->modes = ~0;
1495 symbol->info.ainfo =
1496 (struct alias_info *)malloc(sizeof(struct alias_info));
1497 if (symbol->info.ainfo == NULL) {
1498 stop("Can't create alias info", EX_SOFTWARE);
1501 memset(symbol->info.ainfo, 0,
1502 sizeof(struct alias_info));
1508 symbol->info.finfo =
1509 (struct field_info *)malloc(sizeof(struct field_info));
1510 if (symbol->info.finfo == NULL) {
1511 stop("Can't create field info", EX_SOFTWARE);
1514 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1515 SLIST_INIT(&(symbol->info.finfo->symrefs));
1518 case DOWNLOAD_CONST:
1519 symbol->info.cinfo =
1520 (struct const_info *)malloc(sizeof(struct const_info));
1521 if (symbol->info.cinfo == NULL) {
1522 stop("Can't create alias info", EX_SOFTWARE);
1525 memset(symbol->info.cinfo, 0,
1526 sizeof(struct const_info));
1529 symbol->info.linfo =
1530 (struct label_info *)malloc(sizeof(struct label_info));
1531 if (symbol->info.linfo == NULL) {
1532 stop("Can't create label info", EX_SOFTWARE);
1535 memset(symbol->info.linfo, 0,
1536 sizeof(struct label_info));
1539 symbol->info.condinfo =
1540 (struct cond_info *)malloc(sizeof(struct cond_info));
1541 if (symbol->info.condinfo == NULL) {
1542 stop("Can't create conditional info", EX_SOFTWARE);
1545 memset(symbol->info.condinfo, 0,
1546 sizeof(struct cond_info));
1549 symbol->info.macroinfo =
1550 (struct macro_info *)malloc(sizeof(struct macro_info));
1551 if (symbol->info.macroinfo == NULL) {
1552 stop("Can't create macro info", EX_SOFTWARE);
1555 memset(symbol->info.macroinfo, 0,
1556 sizeof(struct macro_info));
1557 STAILQ_INIT(&symbol->info.macroinfo->args);
1560 stop("Call to initialize_symbol with invalid symbol type",
1568 add_macro_arg(const char *argtext, int argnum)
1570 struct macro_arg *marg;
1574 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1575 stop("Invalid current symbol for adding macro arg",
1580 marg = (struct macro_arg *)malloc(sizeof(*marg));
1582 stop("Can't create macro_arg structure", EX_SOFTWARE);
1585 marg->replacement_text = NULL;
1586 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1587 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1589 if (retval >= sizeof(regex_pattern)) {
1590 stop("Regex text buffer too small for arg",
1594 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1596 stop("Regex compilation failed", EX_SOFTWARE);
1599 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1603 add_macro_body(const char *bodytext)
1605 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1606 stop("Invalid current symbol for adding macro arg",
1610 cur_symbol->info.macroinfo->body = strdup(bodytext);
1611 if (cur_symbol->info.macroinfo->body == NULL) {
1612 stop("Can't duplicate macro body text", EX_SOFTWARE);
1618 process_register(symbol_t **p_symbol)
1620 symbol_t *symbol = *p_symbol;
1622 if (symbol->type == UNINITIALIZED) {
1623 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1625 stop(errbuf, EX_DATAERR);
1627 } else if (symbol->type == ALIAS) {
1628 *p_symbol = symbol->info.ainfo->parent;
1629 } else if ((symbol->type != REGISTER)
1630 && (symbol->type != SCBLOC)
1631 && (symbol->type != SRAMLOC)) {
1632 snprintf(errbuf, sizeof(errbuf),
1633 "Specified symbol %s is not a register",
1635 stop(errbuf, EX_DATAERR);
1640 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1641 symbol_ref_t *src, int ret)
1643 struct instruction *instr;
1644 struct ins_format1 *f1_instr;
1646 if (src->symbol == NULL)
1649 /* Test register permissions */
1650 test_writable_symbol(dest->symbol);
1651 test_readable_symbol(src->symbol);
1653 if (!is_location_address(dest->symbol)) {
1654 /* Ensure that immediate makes sense for this destination */
1655 type_check(dest, immed, opcode);
1658 /* Allocate sequencer space for the instruction and fill it out */
1659 instr = seq_alloc();
1660 f1_instr = &instr->format.format1;
1661 f1_instr->ret = ret ? 1 : 0;
1662 f1_instr->opcode = opcode;
1663 f1_instr->destination = dest->symbol->info.rinfo->address
1665 f1_instr->source = src->symbol->info.rinfo->address
1667 f1_instr->immediate = immed->value;
1669 if (is_download_const(immed))
1670 f1_instr->parity = 1;
1671 else if (dest->symbol == mode_ptr.symbol) {
1676 * Attempt to update mode information if
1677 * we are operating on the mode register.
1679 if (src->symbol == allones.symbol)
1681 else if (src->symbol == allzeros.symbol)
1683 else if (src->symbol == mode_ptr.symbol)
1684 src_value = (dst_mode << 4) | src_mode;
1690 dst_value = src_value & immed->value;
1693 dst_value = src_value ^ immed->value;
1696 dst_value = (src_value + immed->value) & 0xFF;
1699 dst_value = src_value | immed->value;
1702 dst_value = src_value;
1707 src_mode = dst_value & 0xF;
1708 dst_mode = (dst_value >> 4) & 0xF;
1712 symlist_free(&immed->referenced_syms);
1717 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1718 symbol_ref_t *src, int ret)
1720 struct instruction *instr;
1721 struct ins_format2 *f2_instr;
1722 uint8_t shift_control;
1724 if (src->symbol == NULL)
1727 /* Test register permissions */
1728 test_writable_symbol(dest->symbol);
1729 test_readable_symbol(src->symbol);
1731 /* Allocate sequencer space for the instruction and fill it out */
1732 instr = seq_alloc();
1733 f2_instr = &instr->format.format2;
1734 f2_instr->ret = ret ? 1 : 0;
1735 f2_instr->opcode = AIC_OP_ROL;
1736 f2_instr->destination = dest->symbol->info.rinfo->address
1738 f2_instr->source = src->symbol->info.rinfo->address
1740 if (places->value > 8 || places->value <= 0) {
1741 stop("illegal shift value", EX_DATAERR);
1746 if (places->value == 8)
1747 shift_control = 0xf0;
1749 shift_control = (places->value << 4) | places->value;
1752 if (places->value == 8) {
1753 shift_control = 0xf8;
1755 shift_control = (places->value << 4)
1756 | (8 - places->value)
1761 shift_control = places->value & 0x7;
1764 shift_control = (8 - places->value) | 0x08;
1767 shift_control = 0; /* Quiet Compiler */
1768 stop("Invalid shift operation specified", EX_SOFTWARE);
1772 f2_instr->shift_control = shift_control;
1773 symlist_free(&places->referenced_syms);
1778 format_3_instr(int opcode, symbol_ref_t *src,
1779 expression_t *immed, symbol_ref_t *address)
1781 struct instruction *instr;
1782 struct ins_format3 *f3_instr;
1785 /* Test register permissions */
1786 test_readable_symbol(src->symbol);
1788 /* Allocate sequencer space for the instruction and fill it out */
1789 instr = seq_alloc();
1790 f3_instr = &instr->format.format3;
1791 if (address->symbol == NULL) {
1792 /* 'dot' referrence. Use the current instruction pointer */
1793 addr = instruction_ptr + address->offset;
1794 } else if (address->symbol->type == UNINITIALIZED) {
1795 /* forward reference */
1796 addr = address->offset;
1797 instr->patch_label = address->symbol;
1799 addr = address->symbol->info.linfo->address + address->offset;
1800 f3_instr->opcode = opcode;
1801 f3_instr->address = addr;
1802 f3_instr->source = src->symbol->info.rinfo->address
1804 f3_instr->immediate = immed->value;
1806 if (is_download_const(immed))
1807 f3_instr->parity = 1;
1809 symlist_free(&immed->referenced_syms);
1814 test_readable_symbol(symbol_t *symbol)
1816 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1817 snprintf(errbuf, sizeof(errbuf),
1818 "Register %s unavailable in source reg mode %d",
1819 symbol->name, src_mode);
1820 stop(errbuf, EX_DATAERR);
1823 if (symbol->info.rinfo->mode == WO) {
1824 stop("Write Only register specified as source",
1831 test_writable_symbol(symbol_t *symbol)
1833 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1834 snprintf(errbuf, sizeof(errbuf),
1835 "Register %s unavailable in destination reg mode %d",
1836 symbol->name, dst_mode);
1837 stop(errbuf, EX_DATAERR);
1840 if (symbol->info.rinfo->mode == RO) {
1841 stop("Read Only register specified as destination",
1848 type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
1850 symbol_t *symbol = sym->symbol;
1851 symbol_node_t *node;
1857 * Make sure that we aren't attempting to write something
1858 * that hasn't been defined. If this is an and operation,
1859 * this is a mask, so "undefined" bits are okay.
1861 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ ||
1862 opcode == AIC_OP_JZ || opcode == AIC_OP_JNE ||
1863 opcode == AIC_OP_BMOV)
1867 * Defaulting to 8 bit logic
1869 mask = (int8_t)~symbol->info.rinfo->valid_bitmask;
1870 value = (int8_t)expression->value;
1872 if (and_op == FALSE && (mask & value) != 0 ) {
1873 snprintf(errbuf, sizeof(errbuf),
1874 "Invalid bit(s) 0x%x in immediate written to %s",
1877 stop(errbuf, EX_DATAERR);
1882 * Now make sure that all of the symbols referenced by the
1883 * expression are defined for this register.
1885 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1886 for(node = expression->referenced_syms.slh_first;
1888 node = node->links.sle_next) {
1889 if ((node->symbol->type == MASK
1890 || node->symbol->type == FIELD
1891 || node->symbol->type == ENUM
1892 || node->symbol->type == ENUM_ENTRY)
1893 && symlist_search(&node->symbol->info.finfo->symrefs,
1894 symbol->name) == NULL) {
1895 snprintf(errbuf, sizeof(errbuf),
1896 "Invalid field or mask %s "
1898 node->symbol->name, symbol->name);
1899 stop(errbuf, EX_DATAERR);
1907 make_expression(expression_t *immed, int value)
1909 SLIST_INIT(&immed->referenced_syms);
1910 immed->value = value & 0xff;
1914 add_conditional(symbol_t *symbol)
1916 static int numfuncs;
1918 if (numfuncs == 0) {
1919 /* add a special conditional, "0" */
1920 symbol_t *false_func;
1922 false_func = symtable_get("0");
1923 if (false_func->type != UNINITIALIZED) {
1924 stop("Conditional expression '0' "
1925 "conflicts with a symbol", EX_DATAERR);
1928 false_func->type = CONDITIONAL;
1929 initialize_symbol(false_func);
1930 false_func->info.condinfo->func_num = numfuncs++;
1931 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1934 /* This condition has occurred before */
1935 if (symbol->type == CONDITIONAL)
1938 if (symbol->type != UNINITIALIZED) {
1939 stop("Conditional expression conflicts with a symbol",
1944 symbol->type = CONDITIONAL;
1945 initialize_symbol(symbol);
1946 symbol->info.condinfo->func_num = numfuncs++;
1947 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1951 add_version(const char *verstring)
1953 const char prefix[] = " * ";
1957 newlen = strlen(verstring) + strlen(prefix);
1959 if (versions != NULL)
1960 oldlen = strlen(versions);
1961 versions = realloc(versions, newlen + oldlen + 2);
1962 if (versions == NULL)
1963 stop("Can't allocate version string", EX_SOFTWARE);
1964 strcpy(&versions[oldlen], prefix);
1965 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1966 versions[newlen + oldlen] = '\n';
1967 versions[newlen + oldlen + 1] = '\0';
1971 yyerror(const char *string)
1973 stop(string, EX_DATAERR);
1977 is_download_const(expression_t *immed)
1979 if ((immed->referenced_syms.slh_first != NULL)
1980 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
1987 is_location_address(symbol_t *sym)
1989 if (sym->type == SCBLOC ||
1990 sym->type == SRAMLOC)