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_t *symbol, 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);
110 #define SRAM_SYMNAME "SRAM_BASE"
111 #define SCB_SYMNAME "SCB_BASE"
118 symbol_ref_t sym_ref;
119 expression_t expression;
124 %token <value> T_CONST
142 %token <value> T_ADDRESS
150 %token T_SET_SRC_MODE
152 %token T_SET_DST_MODE
154 %token <value> T_MODE
168 %token <value> T_NUMBER
170 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
174 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
176 %token <value> T_SHR T_SHL T_ROR T_ROL
178 %token <value> T_MVI T_MOV T_CLR T_BMOV
180 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
182 %token <value> T_ADD T_ADC
184 %token <value> T_INC T_DEC
186 %token <value> T_STC T_CLC
188 %token <value> T_CMP T_NOT T_XOR
190 %token <value> T_TEST T_AND
194 /* 16 bit extensions */
195 %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
196 %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
202 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
206 %token <sym> T_SYMBOL
210 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
212 %type <sym_ref> reg_symbol address destination source opt_source
214 %type <expression> expression immediate immediate_or_a
216 %type <value> export ret f1_opcode f2_opcode f4_opcode jmp_jc_jnc_call jz_jnz je_jne
218 %type <value> mode_value mode_list macro_arglist
222 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
235 | program patch_arg_list
245 | program scratch_ram
251 | program set_src_mode
253 | program set_dst_mode
254 | critical_section_start
255 | program critical_section_start
256 | critical_section_end
257 | program critical_section_end
259 | program conditional
265 T_INCLUDE '<' T_PATH '>'
267 include_file($3, BRACKETED_INCLUDE);
269 | T_INCLUDE '"' T_PATH '"'
271 include_file($3, QUOTED_INCLUDE);
276 T_PREFIX '=' T_STRING
278 if (prefix != stock_prefix)
279 stop("Prefix multiply defined",
283 stop("Unable to record prefix", EX_SOFTWARE);
288 T_PATCH_ARG_LIST '=' T_STRING
290 if (patch_arg_list != NULL)
291 stop("Patch argument list multiply defined",
293 patch_arg_list = strdup($3);
294 if (patch_arg_list == NULL)
295 stop("Unable to record patch arg list", EX_SOFTWARE);
300 T_VERSION '=' T_STRING
305 T_REGISTER { cur_symtype = REGISTER; } reg_definition
311 if ($1->type != UNINITIALIZED) {
312 stop("Register multiply defined", EX_DATAERR);
316 cur_symbol->type = cur_symtype;
317 initialize_symbol(cur_symbol);
323 * Default to allowing everything in for registers
324 * with no bit or mask definitions.
326 if (cur_symbol->info.rinfo->valid_bitmask == 0)
327 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
329 if (cur_symbol->info.rinfo->size == 0)
330 cur_symbol->info.rinfo->size = 1;
333 * This might be useful for registers too.
335 if (cur_symbol->type != REGISTER) {
336 if (cur_symbol->info.rinfo->address == 0)
337 cur_symbol->info.rinfo->address =
339 sram_or_scb_offset +=
340 cur_symbol->info.rinfo->size;
348 | reg_attribute_list reg_attribute
371 cur_symbol->info.rinfo->address = $2;
378 cur_symbol->info.rinfo->size = $2;
379 if (scb_or_sram_symbol != NULL) {
383 max_addr = scb_or_sram_symbol->info.rinfo->address
384 + scb_or_sram_symbol->info.rinfo->size;
385 sym_max_addr = cur_symbol->info.rinfo->address
386 + cur_symbol->info.rinfo->size;
388 if (sym_max_addr > max_addr)
389 stop("SCB or SRAM space exhausted", EX_DATAERR);
397 cur_symbol->info.rinfo->mode = $2;
404 cur_symbol->info.rinfo->modes = $2;
413 | mode_list ',' mode_value
423 stop("Valid register modes range between 0 and 4.",
435 if (symbol->type != CONST) {
436 stop("Only \"const\" symbols allowed in "
437 "mode definitions.", EX_DATAERR);
440 if (symbol->info.cinfo->value > 4) {
441 stop("Valid register modes range between 0 and 4.",
445 $$ = (0x1 << symbol->info.cinfo->value);
456 '{' enum_entry_list '}'
457 | T_FIELD T_SYMBOL expression
459 process_field(FIELD, $2, $3.value);
462 enum_increment = 0x01 << (ffs($3.value) - 1);
464 '{' enum_entry_list '}'
465 | T_FIELD T_SYMBOL expression
467 process_field(FIELD, $2, $3.value);
478 '{' enum_entry_list '}'
479 | T_ENUM T_SYMBOL expression
481 process_field(ENUM, $2, $3.value);
484 enum_increment = 0x01 << (ffs($3.value) - 1);
486 '{' enum_entry_list '}'
491 | enum_entry_list ',' enum_entry
497 process_field(ENUM_ENTRY, $1, enum_next_value);
498 enum_next_value += enum_increment;
500 | T_SYMBOL expression
502 process_field(ENUM_ENTRY, $1, $2.value);
503 enum_next_value = $2.value + enum_increment;
508 T_MASK T_SYMBOL expression
510 process_field(MASK, $2, $3.value);
517 if ($2->type != UNINITIALIZED) {
518 stop("Re-definition of register alias",
523 initialize_symbol($2);
524 $2->info.ainfo->parent = cur_symbol;
531 if (accumulator.symbol != NULL) {
532 stop("Only one accumulator definition allowed",
536 accumulator.symbol = cur_symbol;
543 if (mode_ptr.symbol != NULL) {
544 stop("Only one mode pointer definition allowed",
548 mode_ptr.symbol = cur_symbol;
555 if (allones.symbol != NULL) {
556 stop("Only one definition of allones allowed",
560 allones.symbol = cur_symbol;
567 if (allzeros.symbol != NULL) {
568 stop("Only one definition of allzeros allowed",
572 allzeros.symbol = cur_symbol;
579 if (none.symbol != NULL) {
580 stop("Only one definition of none allowed",
584 none.symbol = cur_symbol;
591 if (sindex.symbol != NULL) {
592 stop("Only one definition of sindex allowed",
596 sindex.symbol = cur_symbol;
601 expression '|' expression
603 $$.value = $1.value | $3.value;
604 symlist_merge(&$$.referenced_syms,
606 &$3.referenced_syms);
608 | expression '&' expression
610 $$.value = $1.value & $3.value;
611 symlist_merge(&$$.referenced_syms,
613 &$3.referenced_syms);
615 | expression '+' expression
617 $$.value = $1.value + $3.value;
618 symlist_merge(&$$.referenced_syms,
620 &$3.referenced_syms);
622 | expression '-' expression
624 $$.value = $1.value - $3.value;
625 symlist_merge(&($$.referenced_syms),
626 &($1.referenced_syms),
627 &($3.referenced_syms));
629 | expression '*' expression
631 $$.value = $1.value * $3.value;
632 symlist_merge(&($$.referenced_syms),
633 &($1.referenced_syms),
634 &($3.referenced_syms));
636 | expression '/' expression
638 $$.value = $1.value / $3.value;
639 symlist_merge(&($$.referenced_syms),
640 &($1.referenced_syms),
641 &($3.referenced_syms));
643 | expression T_EXPR_LSHIFT expression
645 $$.value = $1.value << $3.value;
646 symlist_merge(&$$.referenced_syms,
648 &$3.referenced_syms);
650 | expression T_EXPR_RSHIFT expression
652 $$.value = $1.value >> $3.value;
653 symlist_merge(&$$.referenced_syms,
655 &$3.referenced_syms);
664 $$.value = (~$$.value) & 0xFF;
666 | '-' expression %prec UMINUS
669 $$.value = -$$.value;
674 SLIST_INIT(&$$.referenced_syms);
681 switch (symbol->type) {
683 symbol = $1->info.ainfo->parent;
687 $$.value = symbol->info.rinfo->address;
693 $$.value = symbol->info.finfo->value;
697 $$.value = symbol->info.cinfo->value;
702 snprintf(errbuf, sizeof(errbuf),
703 "Undefined symbol %s referenced",
705 stop(errbuf, EX_DATAERR);
710 SLIST_INIT(&$$.referenced_syms);
711 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
716 T_CONST T_SYMBOL expression
718 if ($2->type != UNINITIALIZED) {
719 stop("Re-definition of symbol as a constant",
724 initialize_symbol($2);
725 $2->info.cinfo->value = $3.value;
727 | T_CONST T_SYMBOL T_DOWNLOAD
730 stop("Invalid downloaded constant declaration",
734 if ($2->type != UNINITIALIZED) {
735 stop("Re-definition of symbol as a downloaded constant",
739 $2->type = DOWNLOAD_CONST;
740 initialize_symbol($2);
741 $2->info.cinfo->value = download_constant_count++;
748 if ($2->type != UNINITIALIZED) {
749 stop("Re-definition of symbol as a macro",
754 cur_symbol->type = MACRO;
755 initialize_symbol(cur_symbol);
760 macrodefn_prologue T_MACROBODY
764 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
767 cur_symbol->info.macroinfo->narg = $3;
773 /* Macros can take no arguments */
779 add_macro_arg($1, 0);
781 | macro_arglist ',' T_ARG
784 stop("Comma without preceeding argument in arg list",
789 add_macro_arg($3, $1);
796 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
798 cur_symbol = symtable_get(SRAM_SYMNAME);
799 cur_symtype = SRAMLOC;
800 cur_symbol->type = SRAMLOC;
801 initialize_symbol(cur_symbol);
805 sram_or_scb_offset = cur_symbol->info.rinfo->address;
809 scb_or_sram_symbol = cur_symbol;
811 scb_or_sram_attributes
815 scb_or_sram_symbol = NULL;
822 cur_symbol = symtable_get(SCB_SYMNAME);
823 cur_symtype = SCBLOC;
824 if (cur_symbol->type != UNINITIALIZED) {
825 stop("Only one SRAM definition allowed",
829 cur_symbol->type = SCBLOC;
830 initialize_symbol(cur_symbol);
831 /* 64 bytes of SCB space */
832 cur_symbol->info.rinfo->size = 64;
836 sram_or_scb_offset = cur_symbol->info.rinfo->address;
840 scb_or_sram_symbol = cur_symbol;
842 scb_or_sram_attributes
846 scb_or_sram_symbol = NULL;
850 scb_or_sram_attributes:
851 /* NULL definition is okay */
853 | scb_or_sram_reg_list
854 | modes scb_or_sram_reg_list
857 scb_or_sram_reg_list:
859 | scb_or_sram_reg_list reg_definition
865 process_register(&$1);
869 | T_SYMBOL '[' T_SYMBOL ']'
871 process_register(&$1);
872 if ($3->type != CONST) {
873 stop("register offset must be a constant", EX_DATAERR);
876 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
877 stop("Accessing offset beyond range of register",
882 $$.offset = $3->info.cinfo->value;
884 | T_SYMBOL '[' T_NUMBER ']'
886 process_register(&$1);
887 if (($3 + 1) > $1->info.rinfo->size) {
888 stop("Accessing offset beyond range of register",
897 if (accumulator.symbol == NULL) {
898 stop("No accumulator has been defined", EX_DATAERR);
901 $$.symbol = accumulator.symbol;
909 test_writable_symbol($1.symbol);
922 if ($1.value == 0 && is_download_const(&$1) == 0) {
923 snprintf(errbuf, sizeof(errbuf),
924 "\nExpression evaluates to 0 and thus "
925 "references the accumulator.\n "
926 "If this is the desired effect, use 'A' "
928 stop(errbuf, EX_DATAERR);
934 SLIST_INIT(&$$.referenced_syms);
935 symlist_add(&$$.referenced_syms, accumulator.symbol,
936 SYMLIST_INSERT_HEAD);
944 test_readable_symbol($1.symbol);
965 T_SET_SRC_MODE T_NUMBER ';'
972 T_SET_DST_MODE T_NUMBER ';'
978 critical_section_start:
981 critical_section_t *cs;
983 if (in_critical_section != FALSE) {
984 stop("Critical Section within Critical Section",
989 cs->begin_addr = instruction_ptr;
990 in_critical_section = TRUE;
994 critical_section_end:
997 critical_section_t *cs;
999 if (in_critical_section == FALSE) {
1000 stop("Unballanced 'end_cs'", EX_DATAERR);
1003 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1004 cs->end_addr = instruction_ptr;
1005 in_critical_section = FALSE;
1018 if ($2->type != UNINITIALIZED) {
1019 stop("Program label multiply defined", EX_DATAERR);
1023 initialize_symbol($2);
1024 $2->info.linfo->address = instruction_ptr;
1025 $2->info.linfo->exported = $1;
1035 | T_SYMBOL '+' T_NUMBER
1040 | T_SYMBOL '-' T_NUMBER
1067 add_conditional($2);
1068 new_scope = scope_alloc();
1069 new_scope->type = SCOPE_IF;
1070 new_scope->begin_addr = instruction_ptr;
1071 new_scope->func_num = $2->info.condinfo->func_num;
1073 | T_ELSE T_IF T_CEXPR '{'
1076 scope_t *scope_context;
1077 scope_t *last_scope;
1080 * Ensure that the previous scope is either an
1081 * if or and else if.
1083 scope_context = SLIST_FIRST(&scope_stack);
1084 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1086 if (last_scope == NULL
1087 || last_scope->type == T_ELSE) {
1089 stop("'else if' without leading 'if'", EX_DATAERR);
1092 add_conditional($3);
1093 new_scope = scope_alloc();
1094 new_scope->type = SCOPE_ELSE_IF;
1095 new_scope->begin_addr = instruction_ptr;
1096 new_scope->func_num = $3->info.condinfo->func_num;
1101 scope_t *scope_context;
1102 scope_t *last_scope;
1105 * Ensure that the previous scope is either an
1106 * if or and else if.
1108 scope_context = SLIST_FIRST(&scope_stack);
1109 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1111 if (last_scope == NULL
1112 || last_scope->type == SCOPE_ELSE) {
1114 stop("'else' without leading 'if'", EX_DATAERR);
1117 new_scope = scope_alloc();
1118 new_scope->type = SCOPE_ELSE;
1119 new_scope->begin_addr = instruction_ptr;
1126 scope_t *scope_context;
1128 scope_context = SLIST_FIRST(&scope_stack);
1129 if (scope_context->type == SCOPE_ROOT) {
1130 stop("Unexpected '}' encountered", EX_DATAERR);
1134 scope_context->end_addr = instruction_ptr;
1137 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1139 process_scope(scope_context);
1141 if (SLIST_FIRST(&scope_stack) == NULL) {
1142 stop("Unexpected '}' encountered", EX_DATAERR);
1149 T_AND { $$ = AIC_OP_AND; }
1150 | T_XOR { $$ = AIC_OP_XOR; }
1151 | T_ADD { $$ = AIC_OP_ADD; }
1152 | T_ADC { $$ = AIC_OP_ADC; }
1156 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1158 format_1_instr($1, &$2, &$4, &$5, $6);
1163 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1165 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1170 T_INC destination opt_source ret ';'
1174 make_expression(&immed, 1);
1175 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1180 T_DEC destination opt_source ret ';'
1184 make_expression(&immed, -1);
1185 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1194 make_expression(&immed, -1);
1195 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1197 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1199 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1208 make_expression(&immed, 1);
1209 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1211 | T_STC destination ret ';'
1215 make_expression(&immed, 1);
1216 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1221 T_BMOV destination ',' source ',' immediate ret ';'
1223 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1228 T_MOV destination ',' source ret ';'
1232 make_expression(&immed, 1);
1233 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1238 T_MVI destination ',' immediate ret ';'
1241 && is_download_const(&$4) == 0) {
1245 * Allow move immediates of 0 so that macros,
1246 * that can't know the immediate's value and
1247 * otherwise compensate, still work.
1249 make_expression(&immed, 1);
1250 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1252 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1258 T_NOT destination opt_source ret ';'
1262 make_expression(&immed, 0xff);
1263 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1268 T_CLR destination ret ';'
1272 make_expression(&immed, 0xff);
1273 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1282 make_expression(&immed, 0xff);
1283 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1292 make_expression(&immed, 0xff);
1293 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1298 * This grammer differs from the one in the aic7xxx
1299 * reference manual since the grammer listed there is
1300 * ambiguous and causes a shift/reduce conflict.
1301 * It also seems more logical as the "immediate"
1302 * argument is listed as the second arg like the
1307 T_SHL { $$ = AIC_OP_SHL; }
1308 | T_SHR { $$ = AIC_OP_SHR; }
1309 | T_ROL { $$ = AIC_OP_ROL; }
1310 | T_ROR { $$ = AIC_OP_ROR; }
1314 T_OR16 { $$ = AIC_OP_OR16; }
1315 | T_AND16 { $$ = AIC_OP_AND16; }
1316 | T_XOR16 { $$ = AIC_OP_XOR16; }
1317 | T_ADD16 { $$ = AIC_OP_ADD16; }
1318 | T_ADC16 { $$ = AIC_OP_ADC16; }
1319 | T_MVI16 { $$ = AIC_OP_MVI16; }
1323 f2_opcode destination ',' expression opt_source ret ';'
1325 format_2_instr($1, &$2, &$4, &$5, $6);
1330 T_JMP { $$ = AIC_OP_JMP; }
1331 | T_JC { $$ = AIC_OP_JC; }
1332 | T_JNC { $$ = AIC_OP_JNC; }
1333 | T_CALL { $$ = AIC_OP_CALL; }
1337 T_JZ { $$ = AIC_OP_JZ; }
1338 | T_JNZ { $$ = AIC_OP_JNZ; }
1342 T_JE { $$ = AIC_OP_JE; }
1343 | T_JNE { $$ = AIC_OP_JNE; }
1347 jmp_jc_jnc_call address ';'
1351 make_expression(&immed, 0);
1352 format_3_instr($1, &sindex, &immed, &$2);
1357 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1359 format_3_instr($5, &$2, &$4, &$6);
1364 T_TEST source ',' immediate_or_a jz_jnz address ';'
1366 format_3_instr($5, &$2, &$4, &$6);
1371 T_CMP source ',' immediate_or_a je_jne address ';'
1373 format_3_instr($5, &$2, &$4, &$6);
1378 T_MOV source jmp_jc_jnc_call address ';'
1382 make_expression(&immed, 0);
1383 format_3_instr($3, &$2, &immed, &$4);
1388 T_MVI immediate jmp_jc_jnc_call address ';'
1390 format_3_instr($3, &allzeros, &$2, &$4);
1397 process_field(int field_type, symbol_t *sym, int value)
1400 * Add the current register to its
1401 * symbol list, if it already exists,
1402 * warn if we are setting it to a
1403 * different value, or in the bit to
1404 * the "allowed bits" of this register.
1406 if (sym->type == UNINITIALIZED) {
1407 sym->type = field_type;
1408 initialize_symbol(sym);
1409 sym->info.finfo->value = value;
1410 if (field_type != ENUM_ENTRY) {
1411 if (field_type != MASK && value == 0) {
1412 stop("Empty Field, or Enum", EX_DATAERR);
1415 sym->info.finfo->value = value;
1416 sym->info.finfo->mask = value;
1417 } else if (field_symbol != NULL) {
1418 sym->info.finfo->mask = field_symbol->info.finfo->value;
1420 sym->info.finfo->mask = 0xFF;
1422 } else if (sym->type != field_type) {
1423 stop("Field definition mirrors a definition of the same "
1424 " name, but a different type", EX_DATAERR);
1426 } else if (value != sym->info.finfo->value) {
1427 stop("Field redefined with a conflicting value", EX_DATAERR);
1430 /* Fail if this symbol is already listed */
1431 if (symlist_search(&(sym->info.finfo->symrefs),
1432 cur_symbol->name) != NULL) {
1433 stop("Field defined multiple times for register", EX_DATAERR);
1436 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1437 SYMLIST_INSERT_HEAD);
1438 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1439 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1440 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1444 initialize_symbol(symbol_t *symbol)
1446 switch (symbol->type) {
1448 stop("Call to initialize_symbol with type field unset",
1455 symbol->info.rinfo =
1456 (struct reg_info *)malloc(sizeof(struct reg_info));
1457 if (symbol->info.rinfo == NULL) {
1458 stop("Can't create register info", EX_SOFTWARE);
1461 memset(symbol->info.rinfo, 0,
1462 sizeof(struct reg_info));
1463 SLIST_INIT(&(symbol->info.rinfo->fields));
1465 * Default to allowing access in all register modes
1466 * or to the mode specified by the SCB or SRAM space
1469 if (scb_or_sram_symbol != NULL)
1470 symbol->info.rinfo->modes =
1471 scb_or_sram_symbol->info.rinfo->modes;
1473 symbol->info.rinfo->modes = ~0;
1476 symbol->info.ainfo =
1477 (struct alias_info *)malloc(sizeof(struct alias_info));
1478 if (symbol->info.ainfo == NULL) {
1479 stop("Can't create alias info", EX_SOFTWARE);
1482 memset(symbol->info.ainfo, 0,
1483 sizeof(struct alias_info));
1489 symbol->info.finfo =
1490 (struct field_info *)malloc(sizeof(struct field_info));
1491 if (symbol->info.finfo == NULL) {
1492 stop("Can't create field info", EX_SOFTWARE);
1495 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1496 SLIST_INIT(&(symbol->info.finfo->symrefs));
1499 case DOWNLOAD_CONST:
1500 symbol->info.cinfo =
1501 (struct const_info *)malloc(sizeof(struct const_info));
1502 if (symbol->info.cinfo == NULL) {
1503 stop("Can't create alias info", EX_SOFTWARE);
1506 memset(symbol->info.cinfo, 0,
1507 sizeof(struct const_info));
1510 symbol->info.linfo =
1511 (struct label_info *)malloc(sizeof(struct label_info));
1512 if (symbol->info.linfo == NULL) {
1513 stop("Can't create label info", EX_SOFTWARE);
1516 memset(symbol->info.linfo, 0,
1517 sizeof(struct label_info));
1520 symbol->info.condinfo =
1521 (struct cond_info *)malloc(sizeof(struct cond_info));
1522 if (symbol->info.condinfo == NULL) {
1523 stop("Can't create conditional info", EX_SOFTWARE);
1526 memset(symbol->info.condinfo, 0,
1527 sizeof(struct cond_info));
1530 symbol->info.macroinfo =
1531 (struct macro_info *)malloc(sizeof(struct macro_info));
1532 if (symbol->info.macroinfo == NULL) {
1533 stop("Can't create macro info", EX_SOFTWARE);
1536 memset(symbol->info.macroinfo, 0,
1537 sizeof(struct macro_info));
1538 STAILQ_INIT(&symbol->info.macroinfo->args);
1541 stop("Call to initialize_symbol with invalid symbol type",
1549 add_macro_arg(const char *argtext, int argnum)
1551 struct macro_arg *marg;
1556 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1557 stop("Invalid current symbol for adding macro arg",
1562 marg = (struct macro_arg *)malloc(sizeof(*marg));
1564 stop("Can't create macro_arg structure", EX_SOFTWARE);
1567 marg->replacement_text = NULL;
1568 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1569 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1571 if (retval >= sizeof(regex_pattern)) {
1572 stop("Regex text buffer too small for arg",
1576 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1578 stop("Regex compilation failed", EX_SOFTWARE);
1581 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1585 add_macro_body(const char *bodytext)
1587 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1588 stop("Invalid current symbol for adding macro arg",
1592 cur_symbol->info.macroinfo->body = strdup(bodytext);
1593 if (cur_symbol->info.macroinfo->body == NULL) {
1594 stop("Can't duplicate macro body text", EX_SOFTWARE);
1600 process_register(symbol_t **p_symbol)
1602 symbol_t *symbol = *p_symbol;
1604 if (symbol->type == UNINITIALIZED) {
1605 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1607 stop(errbuf, EX_DATAERR);
1609 } else if (symbol->type == ALIAS) {
1610 *p_symbol = symbol->info.ainfo->parent;
1611 } else if ((symbol->type != REGISTER)
1612 && (symbol->type != SCBLOC)
1613 && (symbol->type != SRAMLOC)) {
1614 snprintf(errbuf, sizeof(errbuf),
1615 "Specified symbol %s is not a register",
1617 stop(errbuf, EX_DATAERR);
1622 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1623 symbol_ref_t *src, int ret)
1625 struct instruction *instr;
1626 struct ins_format1 *f1_instr;
1628 if (src->symbol == NULL)
1631 /* Test register permissions */
1632 test_writable_symbol(dest->symbol);
1633 test_readable_symbol(src->symbol);
1635 /* Ensure that immediate makes sense for this destination */
1636 type_check(dest->symbol, immed, opcode);
1638 /* Allocate sequencer space for the instruction and fill it out */
1639 instr = seq_alloc();
1640 f1_instr = &instr->format.format1;
1641 f1_instr->ret = ret ? 1 : 0;
1642 f1_instr->opcode = opcode;
1643 f1_instr->destination = dest->symbol->info.rinfo->address
1645 f1_instr->source = src->symbol->info.rinfo->address
1647 f1_instr->immediate = immed->value;
1649 if (is_download_const(immed))
1650 f1_instr->parity = 1;
1651 else if (dest->symbol == mode_ptr.symbol) {
1656 * Attempt to update mode information if
1657 * we are operating on the mode register.
1659 if (src->symbol == allones.symbol)
1661 else if (src->symbol == allzeros.symbol)
1663 else if (src->symbol == mode_ptr.symbol)
1664 src_value = (dst_mode << 4) | src_mode;
1670 dst_value = src_value & immed->value;
1673 dst_value = src_value ^ immed->value;
1676 dst_value = (src_value + immed->value) & 0xFF;
1679 dst_value = src_value | immed->value;
1682 dst_value = src_value;
1687 src_mode = dst_value & 0xF;
1688 dst_mode = (dst_value >> 4) & 0xF;
1692 symlist_free(&immed->referenced_syms);
1697 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1698 symbol_ref_t *src, int ret)
1700 struct instruction *instr;
1701 struct ins_format2 *f2_instr;
1702 uint8_t shift_control;
1704 if (src->symbol == NULL)
1707 /* Test register permissions */
1708 test_writable_symbol(dest->symbol);
1709 test_readable_symbol(src->symbol);
1711 /* Allocate sequencer space for the instruction and fill it out */
1712 instr = seq_alloc();
1713 f2_instr = &instr->format.format2;
1714 f2_instr->ret = ret ? 1 : 0;
1715 f2_instr->opcode = AIC_OP_ROL;
1716 f2_instr->destination = dest->symbol->info.rinfo->address
1718 f2_instr->source = src->symbol->info.rinfo->address
1720 if (places->value > 8 || places->value <= 0) {
1721 stop("illegal shift value", EX_DATAERR);
1726 if (places->value == 8)
1727 shift_control = 0xf0;
1729 shift_control = (places->value << 4) | places->value;
1732 if (places->value == 8) {
1733 shift_control = 0xf8;
1735 shift_control = (places->value << 4)
1736 | (8 - places->value)
1741 shift_control = places->value & 0x7;
1744 shift_control = (8 - places->value) | 0x08;
1747 shift_control = 0; /* Quiet Compiler */
1748 stop("Invalid shift operation specified", EX_SOFTWARE);
1752 f2_instr->shift_control = shift_control;
1753 symlist_free(&places->referenced_syms);
1758 format_3_instr(int opcode, symbol_ref_t *src,
1759 expression_t *immed, symbol_ref_t *address)
1761 struct instruction *instr;
1762 struct ins_format3 *f3_instr;
1765 /* Test register permissions */
1766 test_readable_symbol(src->symbol);
1768 /* Ensure that immediate makes sense for this source */
1769 type_check(src->symbol, immed, opcode);
1771 /* Allocate sequencer space for the instruction and fill it out */
1772 instr = seq_alloc();
1773 f3_instr = &instr->format.format3;
1774 if (address->symbol == NULL) {
1775 /* 'dot' referrence. Use the current instruction pointer */
1776 addr = instruction_ptr + address->offset;
1777 } else if (address->symbol->type == UNINITIALIZED) {
1778 /* forward reference */
1779 addr = address->offset;
1780 instr->patch_label = address->symbol;
1782 addr = address->symbol->info.linfo->address + address->offset;
1783 f3_instr->opcode = opcode;
1784 f3_instr->address = addr;
1785 f3_instr->source = src->symbol->info.rinfo->address
1787 f3_instr->immediate = immed->value;
1789 if (is_download_const(immed))
1790 f3_instr->parity = 1;
1792 symlist_free(&immed->referenced_syms);
1797 test_readable_symbol(symbol_t *symbol)
1800 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1801 snprintf(errbuf, sizeof(errbuf),
1802 "Register %s unavailable in source reg mode %d",
1803 symbol->name, src_mode);
1804 stop(errbuf, EX_DATAERR);
1807 if (symbol->info.rinfo->mode == WO) {
1808 stop("Write Only register specified as source",
1815 test_writable_symbol(symbol_t *symbol)
1818 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1819 snprintf(errbuf, sizeof(errbuf),
1820 "Register %s unavailable in destination reg mode %d",
1821 symbol->name, dst_mode);
1822 stop(errbuf, EX_DATAERR);
1825 if (symbol->info.rinfo->mode == RO) {
1826 stop("Read Only register specified as destination",
1833 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1835 symbol_node_t *node;
1839 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || AIC_OP_JZ)
1843 * Make sure that we aren't attempting to write something
1844 * that hasn't been defined. If this is an and operation,
1845 * this is a mask, so "undefined" bits are okay.
1848 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
1849 snprintf(errbuf, sizeof(errbuf),
1850 "Invalid bit(s) 0x%x in immediate written to %s",
1851 expression->value & ~symbol->info.rinfo->valid_bitmask,
1853 stop(errbuf, EX_DATAERR);
1858 * Now make sure that all of the symbols referenced by the
1859 * expression are defined for this register.
1861 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1862 for(node = expression->referenced_syms.slh_first;
1864 node = node->links.sle_next) {
1865 if ((node->symbol->type == MASK
1866 || node->symbol->type == FIELD
1867 || node->symbol->type == ENUM
1868 || node->symbol->type == ENUM_ENTRY)
1869 && symlist_search(&node->symbol->info.finfo->symrefs,
1870 symbol->name) == NULL) {
1871 snprintf(errbuf, sizeof(errbuf),
1872 "Invalid field or mask %s "
1874 node->symbol->name, symbol->name);
1875 stop(errbuf, EX_DATAERR);
1883 make_expression(expression_t *immed, int value)
1885 SLIST_INIT(&immed->referenced_syms);
1886 immed->value = value & 0xff;
1890 add_conditional(symbol_t *symbol)
1892 static int numfuncs;
1894 if (numfuncs == 0) {
1895 /* add a special conditional, "0" */
1896 symbol_t *false_func;
1898 false_func = symtable_get("0");
1899 if (false_func->type != UNINITIALIZED) {
1900 stop("Conditional expression '0' "
1901 "conflicts with a symbol", EX_DATAERR);
1904 false_func->type = CONDITIONAL;
1905 initialize_symbol(false_func);
1906 false_func->info.condinfo->func_num = numfuncs++;
1907 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1910 /* This condition has occurred before */
1911 if (symbol->type == CONDITIONAL)
1914 if (symbol->type != UNINITIALIZED) {
1915 stop("Conditional expression conflicts with a symbol",
1920 symbol->type = CONDITIONAL;
1921 initialize_symbol(symbol);
1922 symbol->info.condinfo->func_num = numfuncs++;
1923 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1927 add_version(const char *verstring)
1929 const char prefix[] = " * ";
1933 newlen = strlen(verstring) + strlen(prefix);
1935 if (versions != NULL)
1936 oldlen = strlen(versions);
1937 versions = realloc(versions, newlen + oldlen + 2);
1938 if (versions == NULL)
1939 stop("Can't allocate version string", EX_SOFTWARE);
1940 strcpy(&versions[oldlen], prefix);
1941 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1942 versions[newlen + oldlen] = '\n';
1943 versions[newlen + oldlen + 1] = '\0';
1947 yyerror(const char *string)
1949 stop(string, EX_DATAERR);
1953 is_download_const(expression_t *immed)
1955 if ((immed->referenced_syms.slh_first != NULL)
1956 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))