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);
109 void yyerror(const char *string);
111 #define SRAM_SYMNAME "SRAM_BASE"
112 #define SCB_SYMNAME "SCB_BASE"
119 symbol_ref_t sym_ref;
120 expression_t expression;
125 %token <value> T_CONST
143 %token <value> T_ADDRESS
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 */
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 f4_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
372 cur_symbol->info.rinfo->address = $2;
379 cur_symbol->info.rinfo->size = $2;
380 if (scb_or_sram_symbol != NULL) {
384 max_addr = scb_or_sram_symbol->info.rinfo->address
385 + scb_or_sram_symbol->info.rinfo->size;
386 sym_max_addr = cur_symbol->info.rinfo->address
387 + cur_symbol->info.rinfo->size;
389 if (sym_max_addr > max_addr)
390 stop("SCB or SRAM space exhausted", EX_DATAERR);
398 cur_symbol->info.rinfo->mode = $2;
405 cur_symbol->info.rinfo->modes = $2;
414 | mode_list ',' mode_value
424 stop("Valid register modes range between 0 and 4.",
436 if (symbol->type != CONST) {
437 stop("Only \"const\" symbols allowed in "
438 "mode definitions.", EX_DATAERR);
441 if (symbol->info.cinfo->value > 4) {
442 stop("Valid register modes range between 0 and 4.",
446 $$ = (0x1 << symbol->info.cinfo->value);
457 '{' enum_entry_list '}'
458 | T_FIELD T_SYMBOL expression
460 process_field(FIELD, $2, $3.value);
463 enum_increment = 0x01 << (ffs($3.value) - 1);
465 '{' enum_entry_list '}'
466 | T_FIELD T_SYMBOL expression
468 process_field(FIELD, $2, $3.value);
479 '{' enum_entry_list '}'
480 | T_ENUM T_SYMBOL expression
482 process_field(ENUM, $2, $3.value);
485 enum_increment = 0x01 << (ffs($3.value) - 1);
487 '{' enum_entry_list '}'
492 | enum_entry_list ',' enum_entry
498 process_field(ENUM_ENTRY, $1, enum_next_value);
499 enum_next_value += enum_increment;
501 | T_SYMBOL expression
503 process_field(ENUM_ENTRY, $1, $2.value);
504 enum_next_value = $2.value + enum_increment;
509 T_MASK T_SYMBOL expression
511 process_field(MASK, $2, $3.value);
518 if ($2->type != UNINITIALIZED) {
519 stop("Re-definition of register alias",
524 initialize_symbol($2);
525 $2->info.ainfo->parent = cur_symbol;
532 if (accumulator.symbol != NULL) {
533 stop("Only one accumulator definition allowed",
537 accumulator.symbol = cur_symbol;
544 if (mode_ptr.symbol != NULL) {
545 stop("Only one mode pointer definition allowed",
549 mode_ptr.symbol = cur_symbol;
556 if (allones.symbol != NULL) {
557 stop("Only one definition of allones allowed",
561 allones.symbol = cur_symbol;
568 if (allzeros.symbol != NULL) {
569 stop("Only one definition of allzeros allowed",
573 allzeros.symbol = cur_symbol;
580 if (none.symbol != NULL) {
581 stop("Only one definition of none allowed",
585 none.symbol = cur_symbol;
592 if (sindex.symbol != NULL) {
593 stop("Only one definition of sindex allowed",
597 sindex.symbol = cur_symbol;
602 expression '|' expression
604 $$.value = $1.value | $3.value;
605 symlist_merge(&$$.referenced_syms,
607 &$3.referenced_syms);
609 | expression '&' expression
611 $$.value = $1.value & $3.value;
612 symlist_merge(&$$.referenced_syms,
614 &$3.referenced_syms);
616 | expression '+' expression
618 $$.value = $1.value + $3.value;
619 symlist_merge(&$$.referenced_syms,
621 &$3.referenced_syms);
623 | expression '-' expression
625 $$.value = $1.value - $3.value;
626 symlist_merge(&($$.referenced_syms),
627 &($1.referenced_syms),
628 &($3.referenced_syms));
630 | expression '*' expression
632 $$.value = $1.value * $3.value;
633 symlist_merge(&($$.referenced_syms),
634 &($1.referenced_syms),
635 &($3.referenced_syms));
637 | expression '/' expression
639 $$.value = $1.value / $3.value;
640 symlist_merge(&($$.referenced_syms),
641 &($1.referenced_syms),
642 &($3.referenced_syms));
644 | expression T_EXPR_LSHIFT expression
646 $$.value = $1.value << $3.value;
647 symlist_merge(&$$.referenced_syms,
649 &$3.referenced_syms);
651 | expression T_EXPR_RSHIFT expression
653 $$.value = $1.value >> $3.value;
654 symlist_merge(&$$.referenced_syms,
656 &$3.referenced_syms);
665 $$.value = (~$$.value) & 0xFF;
667 | '-' expression %prec UMINUS
670 $$.value = -$$.value;
675 SLIST_INIT(&$$.referenced_syms);
682 switch (symbol->type) {
684 symbol = $1->info.ainfo->parent;
688 $$.value = symbol->info.rinfo->address;
694 $$.value = symbol->info.finfo->value;
698 $$.value = symbol->info.cinfo->value;
703 snprintf(errbuf, sizeof(errbuf),
704 "Undefined symbol %s referenced",
706 stop(errbuf, EX_DATAERR);
711 SLIST_INIT(&$$.referenced_syms);
712 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
717 T_CONST T_SYMBOL expression
719 if ($2->type != UNINITIALIZED) {
720 stop("Re-definition of symbol as a constant",
725 initialize_symbol($2);
726 $2->info.cinfo->value = $3.value;
728 | T_CONST T_SYMBOL T_DOWNLOAD
731 stop("Invalid downloaded constant declaration",
735 if ($2->type != UNINITIALIZED) {
736 stop("Re-definition of symbol as a downloaded constant",
740 $2->type = DOWNLOAD_CONST;
741 initialize_symbol($2);
742 $2->info.cinfo->value = download_constant_count++;
749 if ($2->type != UNINITIALIZED) {
750 stop("Re-definition of symbol as a macro",
755 cur_symbol->type = MACRO;
756 initialize_symbol(cur_symbol);
761 macrodefn_prologue T_MACROBODY
765 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
768 cur_symbol->info.macroinfo->narg = $3;
774 /* Macros can take no arguments */
780 add_macro_arg($1, 0);
782 | macro_arglist ',' T_ARG
785 stop("Comma without preceeding argument in arg list",
790 add_macro_arg($3, $1);
797 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
799 cur_symbol = symtable_get(SRAM_SYMNAME);
800 cur_symtype = SRAMLOC;
801 cur_symbol->type = SRAMLOC;
802 initialize_symbol(cur_symbol);
806 sram_or_scb_offset = cur_symbol->info.rinfo->address;
810 scb_or_sram_symbol = cur_symbol;
812 scb_or_sram_attributes
816 scb_or_sram_symbol = NULL;
823 cur_symbol = symtable_get(SCB_SYMNAME);
824 cur_symtype = SCBLOC;
825 if (cur_symbol->type != UNINITIALIZED) {
826 stop("Only one SRAM definition allowed",
830 cur_symbol->type = SCBLOC;
831 initialize_symbol(cur_symbol);
832 /* 64 bytes of SCB space */
833 cur_symbol->info.rinfo->size = 64;
837 sram_or_scb_offset = cur_symbol->info.rinfo->address;
841 scb_or_sram_symbol = cur_symbol;
843 scb_or_sram_attributes
847 scb_or_sram_symbol = NULL;
851 scb_or_sram_attributes:
852 /* NULL definition is okay */
854 | scb_or_sram_reg_list
855 | modes scb_or_sram_reg_list
858 scb_or_sram_reg_list:
860 | scb_or_sram_reg_list reg_definition
866 process_register(&$1);
870 | T_SYMBOL '[' T_SYMBOL ']'
872 process_register(&$1);
873 if ($3->type != CONST) {
874 stop("register offset must be a constant", EX_DATAERR);
877 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
878 stop("Accessing offset beyond range of register",
883 $$.offset = $3->info.cinfo->value;
885 | T_SYMBOL '[' T_NUMBER ']'
887 process_register(&$1);
888 if (($3 + 1) > $1->info.rinfo->size) {
889 stop("Accessing offset beyond range of register",
898 if (accumulator.symbol == NULL) {
899 stop("No accumulator has been defined", EX_DATAERR);
902 $$.symbol = accumulator.symbol;
910 test_writable_symbol($1.symbol);
923 if ($1.value == 0 && is_download_const(&$1) == 0) {
924 snprintf(errbuf, sizeof(errbuf),
925 "\nExpression evaluates to 0 and thus "
926 "references the accumulator.\n "
927 "If this is the desired effect, use 'A' "
929 stop(errbuf, EX_DATAERR);
935 SLIST_INIT(&$$.referenced_syms);
936 symlist_add(&$$.referenced_syms, accumulator.symbol,
937 SYMLIST_INSERT_HEAD);
945 test_readable_symbol($1.symbol);
966 T_SET_SRC_MODE T_NUMBER ';'
973 T_SET_DST_MODE T_NUMBER ';'
979 critical_section_start:
982 critical_section_t *cs;
984 if (in_critical_section != FALSE) {
985 stop("Critical Section within Critical Section",
990 cs->begin_addr = instruction_ptr;
991 in_critical_section = TRUE;
995 critical_section_end:
998 critical_section_t *cs;
1000 if (in_critical_section == FALSE) {
1001 stop("Unballanced 'end_cs'", EX_DATAERR);
1004 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1005 cs->end_addr = instruction_ptr;
1006 in_critical_section = FALSE;
1019 if ($2->type != UNINITIALIZED) {
1020 stop("Program label multiply defined", EX_DATAERR);
1024 initialize_symbol($2);
1025 $2->info.linfo->address = instruction_ptr;
1026 $2->info.linfo->exported = $1;
1036 | T_SYMBOL '+' T_NUMBER
1041 | T_SYMBOL '-' T_NUMBER
1068 add_conditional($2);
1069 new_scope = scope_alloc();
1070 new_scope->type = SCOPE_IF;
1071 new_scope->begin_addr = instruction_ptr;
1072 new_scope->func_num = $2->info.condinfo->func_num;
1074 | T_ELSE T_IF T_CEXPR '{'
1077 scope_t *scope_context;
1078 scope_t *last_scope;
1081 * Ensure that the previous scope is either an
1082 * if or and else if.
1084 scope_context = SLIST_FIRST(&scope_stack);
1085 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1087 if (last_scope == NULL
1088 || last_scope->type == T_ELSE) {
1090 stop("'else if' without leading 'if'", EX_DATAERR);
1093 add_conditional($3);
1094 new_scope = scope_alloc();
1095 new_scope->type = SCOPE_ELSE_IF;
1096 new_scope->begin_addr = instruction_ptr;
1097 new_scope->func_num = $3->info.condinfo->func_num;
1102 scope_t *scope_context;
1103 scope_t *last_scope;
1106 * Ensure that the previous scope is either an
1107 * if or and else if.
1109 scope_context = SLIST_FIRST(&scope_stack);
1110 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1112 if (last_scope == NULL
1113 || last_scope->type == SCOPE_ELSE) {
1115 stop("'else' without leading 'if'", EX_DATAERR);
1118 new_scope = scope_alloc();
1119 new_scope->type = SCOPE_ELSE;
1120 new_scope->begin_addr = instruction_ptr;
1127 scope_t *scope_context;
1129 scope_context = SLIST_FIRST(&scope_stack);
1130 if (scope_context->type == SCOPE_ROOT) {
1131 stop("Unexpected '}' encountered", EX_DATAERR);
1135 scope_context->end_addr = instruction_ptr;
1138 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1140 process_scope(scope_context);
1142 if (SLIST_FIRST(&scope_stack) == NULL) {
1143 stop("Unexpected '}' encountered", EX_DATAERR);
1150 T_AND { $$ = AIC_OP_AND; }
1151 | T_XOR { $$ = AIC_OP_XOR; }
1152 | T_ADD { $$ = AIC_OP_ADD; }
1153 | T_ADC { $$ = AIC_OP_ADC; }
1157 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1159 format_1_instr($1, &$2, &$4, &$5, $6);
1164 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1166 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1171 T_INC destination opt_source ret ';'
1175 make_expression(&immed, 1);
1176 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1181 T_DEC destination opt_source ret ';'
1185 make_expression(&immed, -1);
1186 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1195 make_expression(&immed, -1);
1196 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1198 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1200 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1209 make_expression(&immed, 1);
1210 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1212 | T_STC destination ret ';'
1216 make_expression(&immed, 1);
1217 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1222 T_BMOV destination ',' source ',' immediate ret ';'
1224 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1229 T_MOV destination ',' source ret ';'
1233 make_expression(&immed, 1);
1234 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1239 T_MVI destination ',' immediate ret ';'
1242 && is_download_const(&$4) == 0) {
1246 * Allow move immediates of 0 so that macros,
1247 * that can't know the immediate's value and
1248 * otherwise compensate, still work.
1250 make_expression(&immed, 1);
1251 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1253 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1259 T_NOT destination opt_source ret ';'
1263 make_expression(&immed, 0xff);
1264 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1269 T_CLR destination ret ';'
1273 make_expression(&immed, 0xff);
1274 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1283 make_expression(&immed, 0xff);
1284 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1293 make_expression(&immed, 0xff);
1294 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1299 * This grammer differs from the one in the aic7xxx
1300 * reference manual since the grammer listed there is
1301 * ambiguous and causes a shift/reduce conflict.
1302 * It also seems more logical as the "immediate"
1303 * argument is listed as the second arg like the
1308 T_SHL { $$ = AIC_OP_SHL; }
1309 | T_SHR { $$ = AIC_OP_SHR; }
1310 | T_ROL { $$ = AIC_OP_ROL; }
1311 | T_ROR { $$ = AIC_OP_ROR; }
1315 T_OR16 { $$ = AIC_OP_OR16; }
1316 | T_AND16 { $$ = AIC_OP_AND16; }
1317 | T_XOR16 { $$ = AIC_OP_XOR16; }
1318 | T_ADD16 { $$ = AIC_OP_ADD16; }
1319 | T_ADC16 { $$ = AIC_OP_ADC16; }
1320 | T_MVI16 { $$ = AIC_OP_MVI16; }
1324 f2_opcode destination ',' expression opt_source ret ';'
1326 format_2_instr($1, &$2, &$4, &$5, $6);
1331 T_JMP { $$ = AIC_OP_JMP; }
1332 | T_JC { $$ = AIC_OP_JC; }
1333 | T_JNC { $$ = AIC_OP_JNC; }
1334 | T_CALL { $$ = AIC_OP_CALL; }
1338 T_JZ { $$ = AIC_OP_JZ; }
1339 | T_JNZ { $$ = AIC_OP_JNZ; }
1343 T_JE { $$ = AIC_OP_JE; }
1344 | T_JNE { $$ = AIC_OP_JNE; }
1348 jmp_jc_jnc_call address ';'
1352 make_expression(&immed, 0);
1353 format_3_instr($1, &sindex, &immed, &$2);
1358 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1360 format_3_instr($5, &$2, &$4, &$6);
1365 T_TEST source ',' immediate_or_a jz_jnz address ';'
1367 format_3_instr($5, &$2, &$4, &$6);
1372 T_CMP source ',' immediate_or_a je_jne address ';'
1374 format_3_instr($5, &$2, &$4, &$6);
1379 T_MOV source jmp_jc_jnc_call address ';'
1383 make_expression(&immed, 0);
1384 format_3_instr($3, &$2, &immed, &$4);
1389 T_MVI immediate jmp_jc_jnc_call address ';'
1391 format_3_instr($3, &allzeros, &$2, &$4);
1398 process_field(int field_type, symbol_t *sym, int value)
1401 * Add the current register to its
1402 * symbol list, if it already exists,
1403 * warn if we are setting it to a
1404 * different value, or in the bit to
1405 * the "allowed bits" of this register.
1407 if (sym->type == UNINITIALIZED) {
1408 sym->type = field_type;
1409 initialize_symbol(sym);
1410 sym->info.finfo->value = value;
1411 if (field_type != ENUM_ENTRY) {
1412 if (field_type != MASK && value == 0) {
1413 stop("Empty Field, or Enum", EX_DATAERR);
1416 sym->info.finfo->value = value;
1417 sym->info.finfo->mask = value;
1418 } else if (field_symbol != NULL) {
1419 sym->info.finfo->mask = field_symbol->info.finfo->value;
1421 sym->info.finfo->mask = 0xFF;
1423 } else if (sym->type != field_type) {
1424 stop("Field definition mirrors a definition of the same "
1425 " name, but a different type", EX_DATAERR);
1427 } else if (value != sym->info.finfo->value) {
1428 stop("Field redefined with a conflicting value", EX_DATAERR);
1431 /* Fail if this symbol is already listed */
1432 if (symlist_search(&(sym->info.finfo->symrefs),
1433 cur_symbol->name) != NULL) {
1434 stop("Field defined multiple times for register", EX_DATAERR);
1437 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1438 SYMLIST_INSERT_HEAD);
1439 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1440 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1441 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1445 initialize_symbol(symbol_t *symbol)
1447 switch (symbol->type) {
1449 stop("Call to initialize_symbol with type field unset",
1456 symbol->info.rinfo =
1457 (struct reg_info *)malloc(sizeof(struct reg_info));
1458 if (symbol->info.rinfo == NULL) {
1459 stop("Can't create register info", EX_SOFTWARE);
1462 memset(symbol->info.rinfo, 0,
1463 sizeof(struct reg_info));
1464 SLIST_INIT(&(symbol->info.rinfo->fields));
1466 * Default to allowing access in all register modes
1467 * or to the mode specified by the SCB or SRAM space
1470 if (scb_or_sram_symbol != NULL)
1471 symbol->info.rinfo->modes =
1472 scb_or_sram_symbol->info.rinfo->modes;
1474 symbol->info.rinfo->modes = ~0;
1477 symbol->info.ainfo =
1478 (struct alias_info *)malloc(sizeof(struct alias_info));
1479 if (symbol->info.ainfo == NULL) {
1480 stop("Can't create alias info", EX_SOFTWARE);
1483 memset(symbol->info.ainfo, 0,
1484 sizeof(struct alias_info));
1490 symbol->info.finfo =
1491 (struct field_info *)malloc(sizeof(struct field_info));
1492 if (symbol->info.finfo == NULL) {
1493 stop("Can't create field info", EX_SOFTWARE);
1496 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1497 SLIST_INIT(&(symbol->info.finfo->symrefs));
1500 case DOWNLOAD_CONST:
1501 symbol->info.cinfo =
1502 (struct const_info *)malloc(sizeof(struct const_info));
1503 if (symbol->info.cinfo == NULL) {
1504 stop("Can't create alias info", EX_SOFTWARE);
1507 memset(symbol->info.cinfo, 0,
1508 sizeof(struct const_info));
1511 symbol->info.linfo =
1512 (struct label_info *)malloc(sizeof(struct label_info));
1513 if (symbol->info.linfo == NULL) {
1514 stop("Can't create label info", EX_SOFTWARE);
1517 memset(symbol->info.linfo, 0,
1518 sizeof(struct label_info));
1521 symbol->info.condinfo =
1522 (struct cond_info *)malloc(sizeof(struct cond_info));
1523 if (symbol->info.condinfo == NULL) {
1524 stop("Can't create conditional info", EX_SOFTWARE);
1527 memset(symbol->info.condinfo, 0,
1528 sizeof(struct cond_info));
1531 symbol->info.macroinfo =
1532 (struct macro_info *)malloc(sizeof(struct macro_info));
1533 if (symbol->info.macroinfo == NULL) {
1534 stop("Can't create macro info", EX_SOFTWARE);
1537 memset(symbol->info.macroinfo, 0,
1538 sizeof(struct macro_info));
1539 STAILQ_INIT(&symbol->info.macroinfo->args);
1542 stop("Call to initialize_symbol with invalid symbol type",
1550 add_macro_arg(const char *argtext, int argnum)
1552 struct macro_arg *marg;
1557 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1558 stop("Invalid current symbol for adding macro arg",
1563 marg = (struct macro_arg *)malloc(sizeof(*marg));
1565 stop("Can't create macro_arg structure", EX_SOFTWARE);
1568 marg->replacement_text = NULL;
1569 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1570 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1572 if (retval >= sizeof(regex_pattern)) {
1573 stop("Regex text buffer too small for arg",
1577 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1579 stop("Regex compilation failed", EX_SOFTWARE);
1582 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1586 add_macro_body(const char *bodytext)
1588 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1589 stop("Invalid current symbol for adding macro arg",
1593 cur_symbol->info.macroinfo->body = strdup(bodytext);
1594 if (cur_symbol->info.macroinfo->body == NULL) {
1595 stop("Can't duplicate macro body text", EX_SOFTWARE);
1601 process_register(symbol_t **p_symbol)
1603 symbol_t *symbol = *p_symbol;
1605 if (symbol->type == UNINITIALIZED) {
1606 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1608 stop(errbuf, EX_DATAERR);
1610 } else if (symbol->type == ALIAS) {
1611 *p_symbol = symbol->info.ainfo->parent;
1612 } else if ((symbol->type != REGISTER)
1613 && (symbol->type != SCBLOC)
1614 && (symbol->type != SRAMLOC)) {
1615 snprintf(errbuf, sizeof(errbuf),
1616 "Specified symbol %s is not a register",
1618 stop(errbuf, EX_DATAERR);
1623 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1624 symbol_ref_t *src, int ret)
1626 struct instruction *instr;
1627 struct ins_format1 *f1_instr;
1629 if (src->symbol == NULL)
1632 /* Test register permissions */
1633 test_writable_symbol(dest->symbol);
1634 test_readable_symbol(src->symbol);
1636 /* Ensure that immediate makes sense for this destination */
1637 type_check(dest->symbol, immed, opcode);
1639 /* Allocate sequencer space for the instruction and fill it out */
1640 instr = seq_alloc();
1641 f1_instr = &instr->format.format1;
1642 f1_instr->ret = ret ? 1 : 0;
1643 f1_instr->opcode = opcode;
1644 f1_instr->destination = dest->symbol->info.rinfo->address
1646 f1_instr->source = src->symbol->info.rinfo->address
1648 f1_instr->immediate = immed->value;
1650 if (is_download_const(immed))
1651 f1_instr->parity = 1;
1652 else if (dest->symbol == mode_ptr.symbol) {
1657 * Attempt to update mode information if
1658 * we are operating on the mode register.
1660 if (src->symbol == allones.symbol)
1662 else if (src->symbol == allzeros.symbol)
1664 else if (src->symbol == mode_ptr.symbol)
1665 src_value = (dst_mode << 4) | src_mode;
1671 dst_value = src_value & immed->value;
1674 dst_value = src_value ^ immed->value;
1677 dst_value = (src_value + immed->value) & 0xFF;
1680 dst_value = src_value | immed->value;
1683 dst_value = src_value;
1688 src_mode = dst_value & 0xF;
1689 dst_mode = (dst_value >> 4) & 0xF;
1693 symlist_free(&immed->referenced_syms);
1698 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1699 symbol_ref_t *src, int ret)
1701 struct instruction *instr;
1702 struct ins_format2 *f2_instr;
1703 uint8_t shift_control;
1705 if (src->symbol == NULL)
1708 /* Test register permissions */
1709 test_writable_symbol(dest->symbol);
1710 test_readable_symbol(src->symbol);
1712 /* Allocate sequencer space for the instruction and fill it out */
1713 instr = seq_alloc();
1714 f2_instr = &instr->format.format2;
1715 f2_instr->ret = ret ? 1 : 0;
1716 f2_instr->opcode = AIC_OP_ROL;
1717 f2_instr->destination = dest->symbol->info.rinfo->address
1719 f2_instr->source = src->symbol->info.rinfo->address
1721 if (places->value > 8 || places->value <= 0) {
1722 stop("illegal shift value", EX_DATAERR);
1727 if (places->value == 8)
1728 shift_control = 0xf0;
1730 shift_control = (places->value << 4) | places->value;
1733 if (places->value == 8) {
1734 shift_control = 0xf8;
1736 shift_control = (places->value << 4)
1737 | (8 - places->value)
1742 shift_control = places->value & 0x7;
1745 shift_control = (8 - places->value) | 0x08;
1748 shift_control = 0; /* Quiet Compiler */
1749 stop("Invalid shift operation specified", EX_SOFTWARE);
1753 f2_instr->shift_control = shift_control;
1754 symlist_free(&places->referenced_syms);
1759 format_3_instr(int opcode, symbol_ref_t *src,
1760 expression_t *immed, symbol_ref_t *address)
1762 struct instruction *instr;
1763 struct ins_format3 *f3_instr;
1766 /* Test register permissions */
1767 test_readable_symbol(src->symbol);
1769 /* Ensure that immediate makes sense for this source */
1770 type_check(src->symbol, immed, opcode);
1772 /* Allocate sequencer space for the instruction and fill it out */
1773 instr = seq_alloc();
1774 f3_instr = &instr->format.format3;
1775 if (address->symbol == NULL) {
1776 /* 'dot' referrence. Use the current instruction pointer */
1777 addr = instruction_ptr + address->offset;
1778 } else if (address->symbol->type == UNINITIALIZED) {
1779 /* forward reference */
1780 addr = address->offset;
1781 instr->patch_label = address->symbol;
1783 addr = address->symbol->info.linfo->address + address->offset;
1784 f3_instr->opcode = opcode;
1785 f3_instr->address = addr;
1786 f3_instr->source = src->symbol->info.rinfo->address
1788 f3_instr->immediate = immed->value;
1790 if (is_download_const(immed))
1791 f3_instr->parity = 1;
1793 symlist_free(&immed->referenced_syms);
1798 test_readable_symbol(symbol_t *symbol)
1801 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1802 snprintf(errbuf, sizeof(errbuf),
1803 "Register %s unavailable in source reg mode %d",
1804 symbol->name, src_mode);
1805 stop(errbuf, EX_DATAERR);
1808 if (symbol->info.rinfo->mode == WO) {
1809 stop("Write Only register specified as source",
1816 test_writable_symbol(symbol_t *symbol)
1819 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1820 snprintf(errbuf, sizeof(errbuf),
1821 "Register %s unavailable in destination reg mode %d",
1822 symbol->name, dst_mode);
1823 stop(errbuf, EX_DATAERR);
1826 if (symbol->info.rinfo->mode == RO) {
1827 stop("Read Only register specified as destination",
1834 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1836 symbol_node_t *node;
1840 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || AIC_OP_JZ)
1844 * Make sure that we aren't attempting to write something
1845 * that hasn't been defined. If this is an and operation,
1846 * this is a mask, so "undefined" bits are okay.
1849 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
1850 snprintf(errbuf, sizeof(errbuf),
1851 "Invalid bit(s) 0x%x in immediate written to %s",
1852 expression->value & ~symbol->info.rinfo->valid_bitmask,
1854 stop(errbuf, EX_DATAERR);
1859 * Now make sure that all of the symbols referenced by the
1860 * expression are defined for this register.
1862 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1863 for(node = expression->referenced_syms.slh_first;
1865 node = node->links.sle_next) {
1866 if ((node->symbol->type == MASK
1867 || node->symbol->type == FIELD
1868 || node->symbol->type == ENUM
1869 || node->symbol->type == ENUM_ENTRY)
1870 && symlist_search(&node->symbol->info.finfo->symrefs,
1871 symbol->name) == NULL) {
1872 snprintf(errbuf, sizeof(errbuf),
1873 "Invalid field or mask %s "
1875 node->symbol->name, symbol->name);
1876 stop(errbuf, EX_DATAERR);
1884 make_expression(expression_t *immed, int value)
1886 SLIST_INIT(&immed->referenced_syms);
1887 immed->value = value & 0xff;
1891 add_conditional(symbol_t *symbol)
1893 static int numfuncs;
1895 if (numfuncs == 0) {
1896 /* add a special conditional, "0" */
1897 symbol_t *false_func;
1899 false_func = symtable_get("0");
1900 if (false_func->type != UNINITIALIZED) {
1901 stop("Conditional expression '0' "
1902 "conflicts with a symbol", EX_DATAERR);
1905 false_func->type = CONDITIONAL;
1906 initialize_symbol(false_func);
1907 false_func->info.condinfo->func_num = numfuncs++;
1908 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1911 /* This condition has occurred before */
1912 if (symbol->type == CONDITIONAL)
1915 if (symbol->type != UNINITIALIZED) {
1916 stop("Conditional expression conflicts with a symbol",
1921 symbol->type = CONDITIONAL;
1922 initialize_symbol(symbol);
1923 symbol->info.condinfo->func_num = numfuncs++;
1924 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1928 add_version(const char *verstring)
1930 const char prefix[] = " * ";
1934 newlen = strlen(verstring) + strlen(prefix);
1936 if (versions != NULL)
1937 oldlen = strlen(versions);
1938 versions = realloc(versions, newlen + oldlen + 2);
1939 if (versions == NULL)
1940 stop("Can't allocate version string", EX_SOFTWARE);
1941 strcpy(&versions[oldlen], prefix);
1942 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1943 versions[newlen + oldlen] = '\n';
1944 versions[newlen + oldlen + 1] = '\0';
1948 yyerror(const char *string)
1950 stop(string, EX_DATAERR);
1954 is_download_const(expression_t *immed)
1956 if ((immed->referenced_syms.slh_first != NULL)
1957 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))