3 * Parser for the Aic7xxx SCSI Host adapter sequencer assembler.
5 * SPDX-License-Identifier: BSD-3-Clause
7 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
8 * Copyright (c) 2001, 2002 Adaptec Inc.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
43 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_gram.y#29 $
48 #include <sys/types.h>
56 #include <sys/queue.h>
59 #include "aicasm_symbol.h"
60 #include "aicasm_insformat.h"
64 char stock_prefix[] = "aic_";
65 char *prefix = stock_prefix;
68 static char errbuf[255];
69 static char regex_pattern[255];
70 static symbol_t *cur_symbol;
71 static symbol_t *field_symbol;
72 static symbol_t *scb_or_sram_symbol;
73 static symtype cur_symtype;
74 static symbol_ref_t accumulator;
75 static symbol_ref_t mode_ptr;
76 static symbol_ref_t allones;
77 static symbol_ref_t allzeros;
78 static symbol_ref_t none;
79 static symbol_ref_t sindex;
80 static int instruction_ptr;
82 static int sram_or_scb_offset;
83 static int download_constant_count;
84 static int in_critical_section;
85 static u_int enum_increment;
86 static u_int enum_next_value;
88 static void process_field(unsigned int field_type, symbol_t *sym, int mask);
89 static void initialize_symbol(symbol_t *symbol);
90 static void add_macro_arg(const char *argtext, int position);
91 static void add_macro_body(const char *bodytext);
92 static void process_register(symbol_t **p_symbol);
93 static void format_1_instr(int opcode, symbol_ref_t *dest,
94 expression_t *immed, symbol_ref_t *src, int ret);
95 static void format_2_instr(int opcode, symbol_ref_t *dest,
96 expression_t *places, symbol_ref_t *src, int ret);
97 static void format_3_instr(int opcode, symbol_ref_t *src,
98 expression_t *immed, symbol_ref_t *address);
99 static void test_readable_symbol(symbol_t *symbol);
100 static void test_writable_symbol(symbol_t *symbol);
101 static void type_check(symbol_t *symbol, expression_t *expression, int and_op);
102 static void make_expression(expression_t *immed, int value);
103 static void add_conditional(symbol_t *symbol);
104 static void add_version(const char *verstring);
105 static int is_download_const(expression_t *immed);
107 extern int yylex (void);
109 #define SRAM_SYMNAME "SRAM_BASE"
110 #define SCB_SYMNAME "SCB_BASE"
117 symbol_ref_t sym_ref;
118 expression_t expression;
123 %token <value> T_CONST
141 %token <value> T_ADDRESS
149 %token T_SET_SRC_MODE
151 %token T_SET_DST_MODE
153 %token <value> T_MODE
165 %token <value> T_NUMBER
167 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
171 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
173 %token <value> T_SHR T_SHL T_ROR T_ROL
175 %token <value> T_MVI T_MOV T_CLR T_BMOV
177 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
179 %token <value> T_ADD T_ADC
181 %token <value> T_INC T_DEC
183 %token <value> T_STC T_CLC
185 %token <value> T_CMP T_NOT T_XOR
187 %token <value> T_TEST T_AND
195 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
199 %token <sym> T_SYMBOL
203 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
205 %type <sym_ref> reg_symbol address destination source opt_source
207 %type <expression> expression immediate immediate_or_a
209 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
211 %type <value> mode_value mode_list macro_arglist
215 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
228 | program patch_arg_list
238 | program scratch_ram
244 | program set_src_mode
246 | program set_dst_mode
247 | critical_section_start
248 | program critical_section_start
249 | critical_section_end
250 | program critical_section_end
252 | program conditional
258 T_INCLUDE '<' T_PATH '>'
260 include_file($3, BRACKETED_INCLUDE);
262 | T_INCLUDE '"' T_PATH '"'
264 include_file($3, QUOTED_INCLUDE);
269 T_PREFIX '=' T_STRING
271 if (prefix != stock_prefix)
272 stop("Prefix multiply defined",
276 stop("Unable to record prefix", EX_SOFTWARE);
281 T_PATCH_ARG_LIST '=' T_STRING
283 if (patch_arg_list != NULL)
284 stop("Patch argument list multiply defined",
286 patch_arg_list = strdup($3);
287 if (patch_arg_list == NULL)
288 stop("Unable to record patch arg list", EX_SOFTWARE);
293 T_VERSION '=' T_STRING
298 T_REGISTER { cur_symtype = REGISTER; } reg_definition
304 if ($1->type != UNINITIALIZED) {
305 stop("Register multiply defined", EX_DATAERR);
309 cur_symbol->type = cur_symtype;
310 initialize_symbol(cur_symbol);
316 * Default to allowing everything in for registers
317 * with no bit or mask definitions.
319 if (cur_symbol->info.rinfo->valid_bitmask == 0)
320 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
322 if (cur_symbol->info.rinfo->size == 0)
323 cur_symbol->info.rinfo->size = 1;
326 * This might be useful for registers too.
328 if (cur_symbol->type != REGISTER) {
329 if (cur_symbol->info.rinfo->address == 0)
330 cur_symbol->info.rinfo->address =
332 sram_or_scb_offset +=
333 cur_symbol->info.rinfo->size;
341 | reg_attribute_list reg_attribute
364 cur_symbol->info.rinfo->address = $2;
371 cur_symbol->info.rinfo->size = $2;
372 if (scb_or_sram_symbol != NULL) {
376 max_addr = scb_or_sram_symbol->info.rinfo->address
377 + scb_or_sram_symbol->info.rinfo->size;
378 sym_max_addr = cur_symbol->info.rinfo->address
379 + cur_symbol->info.rinfo->size;
381 if (sym_max_addr > max_addr)
382 stop("SCB or SRAM space exhausted", EX_DATAERR);
390 cur_symbol->info.rinfo->mode = $2;
397 cur_symbol->info.rinfo->modes = $2;
406 | mode_list ',' mode_value
416 stop("Valid register modes range between 0 and 4.",
428 if (symbol->type != CONST) {
429 stop("Only \"const\" symbols allowed in "
430 "mode definitions.", EX_DATAERR);
433 if (symbol->info.cinfo->value > 4) {
434 stop("Valid register modes range between 0 and 4.",
438 $$ = (0x1 << symbol->info.cinfo->value);
449 '{' enum_entry_list '}'
450 | T_FIELD T_SYMBOL expression
452 process_field(FIELD, $2, $3.value);
455 enum_increment = 0x01 << (ffs($3.value) - 1);
457 '{' enum_entry_list '}'
458 | T_FIELD T_SYMBOL expression
460 process_field(FIELD, $2, $3.value);
471 '{' enum_entry_list '}'
472 | T_ENUM T_SYMBOL expression
474 process_field(ENUM, $2, $3.value);
477 enum_increment = 0x01 << (ffs($3.value) - 1);
479 '{' enum_entry_list '}'
484 | enum_entry_list ',' enum_entry
490 process_field(ENUM_ENTRY, $1, enum_next_value);
491 enum_next_value += enum_increment;
493 | T_SYMBOL expression
495 process_field(ENUM_ENTRY, $1, $2.value);
496 enum_next_value = $2.value + enum_increment;
501 T_MASK T_SYMBOL expression
503 process_field(MASK, $2, $3.value);
510 if ($2->type != UNINITIALIZED) {
511 stop("Re-definition of register alias",
516 initialize_symbol($2);
517 $2->info.ainfo->parent = cur_symbol;
524 if (accumulator.symbol != NULL) {
525 stop("Only one accumulator definition allowed",
529 accumulator.symbol = cur_symbol;
536 if (mode_ptr.symbol != NULL) {
537 stop("Only one mode pointer definition allowed",
541 mode_ptr.symbol = cur_symbol;
548 if (allones.symbol != NULL) {
549 stop("Only one definition of allones allowed",
553 allones.symbol = cur_symbol;
560 if (allzeros.symbol != NULL) {
561 stop("Only one definition of allzeros allowed",
565 allzeros.symbol = cur_symbol;
572 if (none.symbol != NULL) {
573 stop("Only one definition of none allowed",
577 none.symbol = cur_symbol;
584 if (sindex.symbol != NULL) {
585 stop("Only one definition of sindex allowed",
589 sindex.symbol = cur_symbol;
594 expression '|' expression
596 $$.value = $1.value | $3.value;
597 symlist_merge(&$$.referenced_syms,
599 &$3.referenced_syms);
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),
619 &($1.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 T_EXPR_LSHIFT expression
638 $$.value = $1.value << $3.value;
639 symlist_merge(&$$.referenced_syms,
641 &$3.referenced_syms);
643 | expression T_EXPR_RSHIFT expression
645 $$.value = $1.value >> $3.value;
646 symlist_merge(&$$.referenced_syms,
648 &$3.referenced_syms);
657 $$.value = (~$$.value) & 0xFF;
659 | '-' expression %prec UMINUS
662 $$.value = -$$.value;
667 SLIST_INIT(&$$.referenced_syms);
674 switch (symbol->type) {
676 symbol = $1->info.ainfo->parent;
680 $$.value = symbol->info.rinfo->address;
686 $$.value = symbol->info.finfo->value;
690 $$.value = symbol->info.cinfo->value;
695 snprintf(errbuf, sizeof(errbuf),
696 "Undefined symbol %s referenced",
698 stop(errbuf, EX_DATAERR);
703 SLIST_INIT(&$$.referenced_syms);
704 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
709 T_CONST T_SYMBOL expression
711 if ($2->type != UNINITIALIZED) {
712 stop("Re-definition of symbol as a constant",
717 initialize_symbol($2);
718 $2->info.cinfo->value = $3.value;
720 | T_CONST T_SYMBOL T_DOWNLOAD
723 stop("Invalid downloaded constant declaration",
727 if ($2->type != UNINITIALIZED) {
728 stop("Re-definition of symbol as a downloaded constant",
732 $2->type = DOWNLOAD_CONST;
733 initialize_symbol($2);
734 $2->info.cinfo->value = download_constant_count++;
741 if ($2->type != UNINITIALIZED) {
742 stop("Re-definition of symbol as a macro",
747 cur_symbol->type = MACRO;
748 initialize_symbol(cur_symbol);
753 macrodefn_prologue T_MACROBODY
757 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
760 cur_symbol->info.macroinfo->narg = $3;
766 /* Macros can take no arguments */
772 add_macro_arg($1, 0);
774 | macro_arglist ',' T_ARG
777 stop("Comma without preceding argument in arg list",
782 add_macro_arg($3, $1);
789 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
791 cur_symbol = symtable_get(SRAM_SYMNAME);
792 cur_symtype = SRAMLOC;
793 cur_symbol->type = SRAMLOC;
794 initialize_symbol(cur_symbol);
798 sram_or_scb_offset = cur_symbol->info.rinfo->address;
802 scb_or_sram_symbol = cur_symbol;
804 scb_or_sram_attributes
808 scb_or_sram_symbol = NULL;
815 cur_symbol = symtable_get(SCB_SYMNAME);
816 cur_symtype = SCBLOC;
817 if (cur_symbol->type != UNINITIALIZED) {
818 stop("Only one SRAM definition allowed",
822 cur_symbol->type = SCBLOC;
823 initialize_symbol(cur_symbol);
824 /* 64 bytes of SCB space */
825 cur_symbol->info.rinfo->size = 64;
829 sram_or_scb_offset = cur_symbol->info.rinfo->address;
833 scb_or_sram_symbol = cur_symbol;
835 scb_or_sram_attributes
839 scb_or_sram_symbol = NULL;
843 scb_or_sram_attributes:
844 /* NULL definition is okay */
846 | scb_or_sram_reg_list
847 | modes scb_or_sram_reg_list
850 scb_or_sram_reg_list:
852 | scb_or_sram_reg_list reg_definition
858 process_register(&$1);
862 | T_SYMBOL '[' T_SYMBOL ']'
864 process_register(&$1);
865 if ($3->type != CONST) {
866 stop("register offset must be a constant", EX_DATAERR);
869 if (($3->info.cinfo->value + 1) > (unsigned)$1->info.rinfo->size) {
870 stop("Accessing offset beyond range of register",
875 $$.offset = $3->info.cinfo->value;
877 | T_SYMBOL '[' T_NUMBER ']'
879 process_register(&$1);
880 if (($3 + 1) > (unsigned)$1->info.rinfo->size) {
881 stop("Accessing offset beyond range of register",
890 if (accumulator.symbol == NULL) {
891 stop("No accumulator has been defined", EX_DATAERR);
894 $$.symbol = accumulator.symbol;
902 test_writable_symbol($1.symbol);
915 if ($1.value == 0 && is_download_const(&$1) == 0) {
916 snprintf(errbuf, sizeof(errbuf),
917 "\nExpression evaluates to 0 and thus "
918 "references the accumulator.\n "
919 "If this is the desired effect, use 'A' "
921 stop(errbuf, EX_DATAERR);
927 SLIST_INIT(&$$.referenced_syms);
928 symlist_add(&$$.referenced_syms, accumulator.symbol,
929 SYMLIST_INSERT_HEAD);
937 test_readable_symbol($1.symbol);
958 T_SET_SRC_MODE T_NUMBER ';'
965 T_SET_DST_MODE T_NUMBER ';'
971 critical_section_start:
974 critical_section_t *cs;
976 if (in_critical_section != FALSE) {
977 stop("Critical Section within Critical Section",
982 cs->begin_addr = instruction_ptr;
983 in_critical_section = TRUE;
987 critical_section_end:
990 critical_section_t *cs;
992 if (in_critical_section == FALSE) {
993 stop("Unballanced 'end_cs'", EX_DATAERR);
996 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
997 cs->end_addr = instruction_ptr;
998 in_critical_section = FALSE;
1011 if ($2->type != UNINITIALIZED) {
1012 stop("Program label multiply defined", EX_DATAERR);
1016 initialize_symbol($2);
1017 $2->info.linfo->address = instruction_ptr;
1018 $2->info.linfo->exported = $1;
1028 | T_SYMBOL '+' T_NUMBER
1033 | T_SYMBOL '-' T_NUMBER
1060 add_conditional($2);
1061 new_scope = scope_alloc();
1062 new_scope->type = SCOPE_IF;
1063 new_scope->begin_addr = instruction_ptr;
1064 new_scope->func_num = $2->info.condinfo->func_num;
1066 | T_ELSE T_IF T_CEXPR '{'
1069 scope_t *scope_context;
1070 scope_t *last_scope;
1073 * Ensure that the previous scope is either an
1074 * if or and else if.
1076 scope_context = SLIST_FIRST(&scope_stack);
1077 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1079 if (last_scope == NULL
1080 || last_scope->type == SCOPE_ELSE) {
1082 stop("'else if' without leading 'if'", EX_DATAERR);
1085 add_conditional($3);
1086 new_scope = scope_alloc();
1087 new_scope->type = SCOPE_ELSE_IF;
1088 new_scope->begin_addr = instruction_ptr;
1089 new_scope->func_num = $3->info.condinfo->func_num;
1094 scope_t *scope_context;
1095 scope_t *last_scope;
1098 * Ensure that the previous scope is either an
1099 * if or and else if.
1101 scope_context = SLIST_FIRST(&scope_stack);
1102 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1104 if (last_scope == NULL
1105 || last_scope->type == SCOPE_ELSE) {
1107 stop("'else' without leading 'if'", EX_DATAERR);
1110 new_scope = scope_alloc();
1111 new_scope->type = SCOPE_ELSE;
1112 new_scope->begin_addr = instruction_ptr;
1119 scope_t *scope_context;
1121 scope_context = SLIST_FIRST(&scope_stack);
1122 if (scope_context->type == SCOPE_ROOT) {
1123 stop("Unexpected '}' encountered", EX_DATAERR);
1127 scope_context->end_addr = instruction_ptr;
1130 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1132 process_scope(scope_context);
1134 if (SLIST_FIRST(&scope_stack) == NULL) {
1135 stop("Unexpected '}' encountered", EX_DATAERR);
1142 T_AND { $$ = AIC_OP_AND; }
1143 | T_XOR { $$ = AIC_OP_XOR; }
1144 | T_ADD { $$ = AIC_OP_ADD; }
1145 | T_ADC { $$ = AIC_OP_ADC; }
1149 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1151 format_1_instr($1, &$2, &$4, &$5, $6);
1156 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1158 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1163 T_INC destination opt_source ret ';'
1167 make_expression(&immed, 1);
1168 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1173 T_DEC destination opt_source ret ';'
1177 make_expression(&immed, -1);
1178 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1187 make_expression(&immed, -1);
1188 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1190 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1192 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1201 make_expression(&immed, 1);
1202 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1204 | T_STC destination ret ';'
1208 make_expression(&immed, 1);
1209 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1214 T_BMOV destination ',' source ',' immediate ret ';'
1216 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1221 T_MOV destination ',' source ret ';'
1225 make_expression(&immed, 1);
1226 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1231 T_MVI destination ',' immediate ret ';'
1234 && is_download_const(&$4) == 0) {
1238 * Allow move immediates of 0 so that macros,
1239 * that can't know the immediate's value and
1240 * otherwise compensate, still work.
1242 make_expression(&immed, 1);
1243 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1245 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1251 T_NOT destination opt_source ret ';'
1255 make_expression(&immed, 0xff);
1256 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1261 T_CLR destination ret ';'
1265 make_expression(&immed, 0xff);
1266 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1275 make_expression(&immed, 0xff);
1276 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1285 make_expression(&immed, 0xff);
1286 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1291 * This grammar differs from the one in the aic7xxx
1292 * reference manual since the grammar listed there is
1293 * ambiguous and causes a shift/reduce conflict.
1294 * It also seems more logical as the "immediate"
1295 * argument is listed as the second arg like the
1300 T_SHL { $$ = AIC_OP_SHL; }
1301 | T_SHR { $$ = AIC_OP_SHR; }
1302 | T_ROL { $$ = AIC_OP_ROL; }
1303 | T_ROR { $$ = AIC_OP_ROR; }
1307 f2_opcode destination ',' expression opt_source ret ';'
1309 format_2_instr($1, &$2, &$4, &$5, $6);
1314 T_JMP { $$ = AIC_OP_JMP; }
1315 | T_JC { $$ = AIC_OP_JC; }
1316 | T_JNC { $$ = AIC_OP_JNC; }
1317 | T_CALL { $$ = AIC_OP_CALL; }
1321 T_JZ { $$ = AIC_OP_JZ; }
1322 | T_JNZ { $$ = AIC_OP_JNZ; }
1326 T_JE { $$ = AIC_OP_JE; }
1327 | T_JNE { $$ = AIC_OP_JNE; }
1331 jmp_jc_jnc_call address ';'
1335 make_expression(&immed, 0);
1336 format_3_instr($1, &sindex, &immed, &$2);
1341 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1343 format_3_instr($5, &$2, &$4, &$6);
1348 T_TEST source ',' immediate_or_a jz_jnz address ';'
1350 format_3_instr($5, &$2, &$4, &$6);
1355 T_CMP source ',' immediate_or_a je_jne address ';'
1357 format_3_instr($5, &$2, &$4, &$6);
1362 T_MOV source jmp_jc_jnc_call address ';'
1366 make_expression(&immed, 0);
1367 format_3_instr($3, &$2, &immed, &$4);
1372 T_MVI immediate jmp_jc_jnc_call address ';'
1374 format_3_instr($3, &allzeros, &$2, &$4);
1381 process_field(unsigned int field_type, symbol_t *sym, int value)
1384 * Add the current register to its
1385 * symbol list, if it already exists,
1386 * warn if we are setting it to a
1387 * different value, or in the bit to
1388 * the "allowed bits" of this register.
1390 if (sym->type == UNINITIALIZED) {
1391 sym->type = field_type;
1392 initialize_symbol(sym);
1393 sym->info.finfo->value = value;
1394 if (field_type != ENUM_ENTRY) {
1395 if (field_type != MASK && value == 0) {
1396 stop("Empty Field, or Enum", EX_DATAERR);
1399 sym->info.finfo->value = value;
1400 sym->info.finfo->mask = value;
1401 } else if (field_symbol != NULL) {
1402 sym->info.finfo->mask = field_symbol->info.finfo->value;
1404 sym->info.finfo->mask = 0xFF;
1406 } else if (sym->type != field_type) {
1407 stop("Field definition mirrors a definition of the same "
1408 " name, but a different type", EX_DATAERR);
1410 } else if (value != sym->info.finfo->value) {
1411 stop("Field redefined with a conflicting value", EX_DATAERR);
1414 /* Fail if this symbol is already listed */
1415 if (symlist_search(&(sym->info.finfo->symrefs),
1416 cur_symbol->name) != NULL) {
1417 stop("Field defined multiple times for register", EX_DATAERR);
1420 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1421 SYMLIST_INSERT_HEAD);
1422 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1423 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1424 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1428 initialize_symbol(symbol_t *symbol)
1430 switch (symbol->type) {
1432 stop("Call to initialize_symbol with type field unset",
1439 symbol->info.rinfo =
1440 (struct reg_info *)malloc(sizeof(struct reg_info));
1441 if (symbol->info.rinfo == NULL) {
1442 stop("Can't create register info", EX_SOFTWARE);
1445 memset(symbol->info.rinfo, 0,
1446 sizeof(struct reg_info));
1447 SLIST_INIT(&(symbol->info.rinfo->fields));
1449 * Default to allowing access in all register modes
1450 * or to the mode specified by the SCB or SRAM space
1453 if (scb_or_sram_symbol != NULL)
1454 symbol->info.rinfo->modes =
1455 scb_or_sram_symbol->info.rinfo->modes;
1457 symbol->info.rinfo->modes = ~0;
1460 symbol->info.ainfo =
1461 (struct alias_info *)malloc(sizeof(struct alias_info));
1462 if (symbol->info.ainfo == NULL) {
1463 stop("Can't create alias info", EX_SOFTWARE);
1466 memset(symbol->info.ainfo, 0,
1467 sizeof(struct alias_info));
1473 symbol->info.finfo =
1474 (struct field_info *)malloc(sizeof(struct field_info));
1475 if (symbol->info.finfo == NULL) {
1476 stop("Can't create field info", EX_SOFTWARE);
1479 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1480 SLIST_INIT(&(symbol->info.finfo->symrefs));
1483 case DOWNLOAD_CONST:
1484 symbol->info.cinfo =
1485 (struct const_info *)malloc(sizeof(struct const_info));
1486 if (symbol->info.cinfo == NULL) {
1487 stop("Can't create alias info", EX_SOFTWARE);
1490 memset(symbol->info.cinfo, 0,
1491 sizeof(struct const_info));
1494 symbol->info.linfo =
1495 (struct label_info *)malloc(sizeof(struct label_info));
1496 if (symbol->info.linfo == NULL) {
1497 stop("Can't create label info", EX_SOFTWARE);
1500 memset(symbol->info.linfo, 0,
1501 sizeof(struct label_info));
1504 symbol->info.condinfo =
1505 (struct cond_info *)malloc(sizeof(struct cond_info));
1506 if (symbol->info.condinfo == NULL) {
1507 stop("Can't create conditional info", EX_SOFTWARE);
1510 memset(symbol->info.condinfo, 0,
1511 sizeof(struct cond_info));
1514 symbol->info.macroinfo =
1515 (struct macro_info *)malloc(sizeof(struct macro_info));
1516 if (symbol->info.macroinfo == NULL) {
1517 stop("Can't create macro info", EX_SOFTWARE);
1520 memset(symbol->info.macroinfo, 0,
1521 sizeof(struct macro_info));
1522 STAILQ_INIT(&symbol->info.macroinfo->args);
1525 stop("Call to initialize_symbol with invalid symbol type",
1533 add_macro_arg(const char *argtext, int argnum __unused)
1535 struct macro_arg *marg;
1539 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1540 stop("Invalid current symbol for adding macro arg",
1545 marg = (struct macro_arg *)malloc(sizeof(*marg));
1547 stop("Can't create macro_arg structure", EX_SOFTWARE);
1550 marg->replacement_text = NULL;
1551 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1552 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1554 if (retval >= (int)sizeof(regex_pattern)) {
1555 stop("Regex text buffer too small for arg",
1559 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1561 stop("Regex compilation failed", EX_SOFTWARE);
1564 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1568 add_macro_body(const char *bodytext)
1570 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1571 stop("Invalid current symbol for adding macro arg",
1575 cur_symbol->info.macroinfo->body = strdup(bodytext);
1576 if (cur_symbol->info.macroinfo->body == NULL) {
1577 stop("Can't duplicate macro body text", EX_SOFTWARE);
1583 process_register(symbol_t **p_symbol)
1585 symbol_t *symbol = *p_symbol;
1587 if (symbol->type == UNINITIALIZED) {
1588 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1590 stop(errbuf, EX_DATAERR);
1592 } else if (symbol->type == ALIAS) {
1593 *p_symbol = symbol->info.ainfo->parent;
1594 } else if ((symbol->type != REGISTER)
1595 && (symbol->type != SCBLOC)
1596 && (symbol->type != SRAMLOC)) {
1597 snprintf(errbuf, sizeof(errbuf),
1598 "Specified symbol %s is not a register",
1600 stop(errbuf, EX_DATAERR);
1605 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1606 symbol_ref_t *src, int ret)
1608 struct instruction *instr;
1609 struct ins_format1 *f1_instr;
1611 if (src->symbol == NULL)
1614 /* Test register permissions */
1615 test_writable_symbol(dest->symbol);
1616 test_readable_symbol(src->symbol);
1618 /* Ensure that immediate makes sense for this destination */
1619 type_check(dest->symbol, immed, opcode);
1621 /* Allocate sequencer space for the instruction and fill it out */
1622 instr = seq_alloc();
1623 f1_instr = &instr->format.format1;
1624 f1_instr->ret = ret ? 1 : 0;
1625 f1_instr->opcode = opcode;
1626 f1_instr->destination = dest->symbol->info.rinfo->address
1628 f1_instr->source = src->symbol->info.rinfo->address
1630 f1_instr->immediate = immed->value;
1632 if (is_download_const(immed))
1633 f1_instr->parity = 1;
1634 else if (dest->symbol == mode_ptr.symbol) {
1639 * Attempt to update mode information if
1640 * we are operating on the mode register.
1642 if (src->symbol == allones.symbol)
1644 else if (src->symbol == allzeros.symbol)
1646 else if (src->symbol == mode_ptr.symbol)
1647 src_value = (dst_mode << 4) | src_mode;
1653 dst_value = src_value & immed->value;
1656 dst_value = src_value ^ immed->value;
1659 dst_value = (src_value + immed->value) & 0xFF;
1662 dst_value = src_value | immed->value;
1665 dst_value = src_value;
1670 src_mode = dst_value & 0xF;
1671 dst_mode = (dst_value >> 4) & 0xF;
1675 symlist_free(&immed->referenced_syms);
1680 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1681 symbol_ref_t *src, int ret)
1683 struct instruction *instr;
1684 struct ins_format2 *f2_instr;
1685 uint8_t shift_control;
1687 if (src->symbol == NULL)
1690 /* Test register permissions */
1691 test_writable_symbol(dest->symbol);
1692 test_readable_symbol(src->symbol);
1694 /* Allocate sequencer space for the instruction and fill it out */
1695 instr = seq_alloc();
1696 f2_instr = &instr->format.format2;
1697 f2_instr->ret = ret ? 1 : 0;
1698 f2_instr->opcode = AIC_OP_ROL;
1699 f2_instr->destination = dest->symbol->info.rinfo->address
1701 f2_instr->source = src->symbol->info.rinfo->address
1703 if (places->value > 8 || places->value <= 0) {
1704 stop("illegal shift value", EX_DATAERR);
1709 if (places->value == 8)
1710 shift_control = 0xf0;
1712 shift_control = (places->value << 4) | places->value;
1715 if (places->value == 8) {
1716 shift_control = 0xf8;
1718 shift_control = (places->value << 4)
1719 | (8 - places->value)
1724 shift_control = places->value & 0x7;
1727 shift_control = (8 - places->value) | 0x08;
1730 shift_control = 0; /* Quiet Compiler */
1731 stop("Invalid shift operation specified", EX_SOFTWARE);
1735 f2_instr->shift_control = shift_control;
1736 symlist_free(&places->referenced_syms);
1741 format_3_instr(int opcode, symbol_ref_t *src,
1742 expression_t *immed, symbol_ref_t *address)
1744 struct instruction *instr;
1745 struct ins_format3 *f3_instr;
1748 /* Test register permissions */
1749 test_readable_symbol(src->symbol);
1751 /* Ensure that immediate makes sense for this source */
1752 type_check(src->symbol, immed, opcode);
1754 /* Allocate sequencer space for the instruction and fill it out */
1755 instr = seq_alloc();
1756 f3_instr = &instr->format.format3;
1757 if (address->symbol == NULL) {
1758 /* 'dot' reference. Use the current instruction pointer */
1759 addr = instruction_ptr + address->offset;
1760 } else if (address->symbol->type == UNINITIALIZED) {
1761 /* forward reference */
1762 addr = address->offset;
1763 instr->patch_label = address->symbol;
1765 addr = address->symbol->info.linfo->address + address->offset;
1766 f3_instr->opcode = opcode;
1767 f3_instr->address = addr;
1768 f3_instr->source = src->symbol->info.rinfo->address
1770 f3_instr->immediate = immed->value;
1772 if (is_download_const(immed))
1773 f3_instr->parity = 1;
1775 symlist_free(&immed->referenced_syms);
1780 test_readable_symbol(symbol_t *symbol)
1783 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1784 snprintf(errbuf, sizeof(errbuf),
1785 "Register %s unavailable in source reg mode %d",
1786 symbol->name, src_mode);
1787 stop(errbuf, EX_DATAERR);
1790 if (symbol->info.rinfo->mode == WO) {
1791 stop("Write Only register specified as source",
1798 test_writable_symbol(symbol_t *symbol)
1801 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1802 snprintf(errbuf, sizeof(errbuf),
1803 "Register %s unavailable in destination reg mode %d",
1804 symbol->name, dst_mode);
1805 stop(errbuf, EX_DATAERR);
1808 if (symbol->info.rinfo->mode == RO) {
1809 stop("Read Only register specified as destination",
1816 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1818 symbol_node_t *node;
1820 uint8_t invalid_bits;
1823 if (opcode == AIC_OP_AND
1824 || opcode == AIC_OP_BMOV
1825 || opcode == AIC_OP_JE
1826 || opcode == AIC_OP_JNE
1827 || opcode == AIC_OP_JNZ
1828 || opcode == AIC_OP_JZ)
1832 * Make sure that we aren't attempting to write something
1833 * that hasn't been defined. If this is an and operation,
1834 * this is a mask, so "undefined" bits are okay.
1836 invalid_bits = expression->value & ~symbol->info.rinfo->valid_bitmask;
1837 if (and_op == FALSE && invalid_bits != 0) {
1838 snprintf(errbuf, sizeof(errbuf),
1839 "Invalid bit(s) 0x%x in immediate written to %s",
1840 invalid_bits, symbol->name);
1841 stop(errbuf, EX_DATAERR);
1846 * Now make sure that all of the symbols referenced by the
1847 * expression are defined for this register.
1849 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1850 for(node = expression->referenced_syms.slh_first;
1852 node = node->links.sle_next) {
1853 if ((node->symbol->type == MASK
1854 || node->symbol->type == FIELD
1855 || node->symbol->type == ENUM
1856 || node->symbol->type == ENUM_ENTRY)
1857 && symlist_search(&node->symbol->info.finfo->symrefs,
1858 symbol->name) == NULL) {
1859 snprintf(errbuf, sizeof(errbuf),
1860 "Invalid field or mask %s "
1862 node->symbol->name, symbol->name);
1863 stop(errbuf, EX_DATAERR);
1871 make_expression(expression_t *immed, int value)
1873 SLIST_INIT(&immed->referenced_syms);
1874 immed->value = value & 0xff;
1878 add_conditional(symbol_t *symbol)
1880 static int numfuncs;
1882 if (numfuncs == 0) {
1883 /* add a special conditional, "0" */
1884 symbol_t *false_func;
1886 false_func = symtable_get("0");
1887 if (false_func->type != UNINITIALIZED) {
1888 stop("Conditional expression '0' "
1889 "conflicts with a symbol", EX_DATAERR);
1892 false_func->type = CONDITIONAL;
1893 initialize_symbol(false_func);
1894 false_func->info.condinfo->func_num = numfuncs++;
1895 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1898 /* This condition has occurred before */
1899 if (symbol->type == CONDITIONAL)
1902 if (symbol->type != UNINITIALIZED) {
1903 stop("Conditional expression conflicts with a symbol",
1908 symbol->type = CONDITIONAL;
1909 initialize_symbol(symbol);
1910 symbol->info.condinfo->func_num = numfuncs++;
1911 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1915 add_version(const char *verstring)
1917 const char verprefix[] = " * ";
1921 newlen = strlen(verstring) + strlen(verprefix);
1923 if (versions != NULL)
1924 oldlen = strlen(versions);
1925 versions = realloc(versions, newlen + oldlen + 2);
1926 if (versions == NULL)
1927 stop("Can't allocate version string", EX_SOFTWARE);
1928 strcpy(&versions[oldlen], verprefix);
1929 strcpy(&versions[oldlen + strlen(verprefix)], verstring);
1930 versions[newlen + oldlen] = '\n';
1931 versions[newlen + oldlen + 1] = '\0';
1935 yyerror(const char *string)
1937 stop(string, EX_DATAERR);
1941 is_download_const(expression_t *immed)
1943 if ((immed->referenced_syms.slh_first != NULL)
1944 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))