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#29 $
43 * $FreeBSD: src/sys/dev/aic7xxx/aicasm/aicasm_gram.y,v 1.11.2.8 2003/01/20 23:59:21 gibbs Exp $
44 * $DragonFly: src/sys/dev/disk/aic7xxx/aicasm/aicasm_gram.y,v 1.3 2006/04/22 16:15:26 dillon Exp $
47 #include <sys/types.h>
59 #include <sys/queue.h>
63 #include "aicasm_symbol.h"
64 #include "aicasm_insformat.h"
68 char stock_prefix[] = "aic_";
69 char *prefix = stock_prefix;
72 static char errbuf[255];
73 static char regex_pattern[255];
74 static symbol_t *cur_symbol;
75 static symbol_t *field_symbol;
76 static symbol_t *scb_or_sram_symbol;
77 static symtype cur_symtype;
78 static symbol_ref_t accumulator;
79 static symbol_ref_t mode_ptr;
80 static symbol_ref_t allones;
81 static symbol_ref_t allzeros;
82 static symbol_ref_t none;
83 static symbol_ref_t sindex;
84 static int instruction_ptr;
86 static int sram_or_scb_offset;
87 static int download_constant_count;
88 static int in_critical_section;
89 static u_int enum_increment;
90 static u_int enum_next_value;
92 static void process_field(unsigned int field_type, symbol_t *sym, int mask);
93 static void initialize_symbol(symbol_t *symbol);
94 static void add_macro_arg(const char *argtext, int position);
95 static void add_macro_body(const char *bodytext);
96 static void process_register(symbol_t **p_symbol);
97 static void format_1_instr(int opcode, symbol_ref_t *dest,
98 expression_t *immed, symbol_ref_t *src, int ret);
99 static void format_2_instr(int opcode, symbol_ref_t *dest,
100 expression_t *places, symbol_ref_t *src, int ret);
101 static void format_3_instr(int opcode, symbol_ref_t *src,
102 expression_t *immed, symbol_ref_t *address);
103 static void test_readable_symbol(symbol_t *symbol);
104 static void test_writable_symbol(symbol_t *symbol);
105 static void type_check(symbol_t *symbol, expression_t *expression, int and_op);
106 static void make_expression(expression_t *immed, int value);
107 static void add_conditional(symbol_t *symbol);
108 static void add_version(const char *verstring);
109 static int is_download_const(expression_t *immed);
111 extern int yylex (void);
112 extern int yyparse (void);
114 #define SRAM_SYMNAME "SRAM_BASE"
115 #define SCB_SYMNAME "SCB_BASE"
122 symbol_ref_t sym_ref;
123 expression_t expression;
128 %token <value> T_CONST
146 %token <value> T_ADDRESS
154 %token T_SET_SRC_MODE
156 %token T_SET_DST_MODE
158 %token <value> T_MODE
170 %token <value> T_NUMBER
172 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
176 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
178 %token <value> T_SHR T_SHL T_ROR T_ROL
180 %token <value> T_MVI T_MOV T_CLR T_BMOV
182 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
184 %token <value> T_ADD T_ADC
186 %token <value> T_INC T_DEC
188 %token <value> T_STC T_CLC
190 %token <value> T_CMP T_NOT T_XOR
192 %token <value> T_TEST T_AND
200 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
204 %token <sym> T_SYMBOL
208 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
210 %type <sym_ref> reg_symbol address destination source opt_source
212 %type <expression> expression immediate immediate_or_a
214 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
216 %type <value> mode_value mode_list macro_arglist
220 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
233 | program patch_arg_list
243 | program scratch_ram
249 | program set_src_mode
251 | program set_dst_mode
252 | critical_section_start
253 | program critical_section_start
254 | critical_section_end
255 | program critical_section_end
257 | program conditional
263 T_INCLUDE '<' T_PATH '>'
265 include_file($3, BRACKETED_INCLUDE);
267 | T_INCLUDE '"' T_PATH '"'
269 include_file($3, QUOTED_INCLUDE);
274 T_PREFIX '=' T_STRING
276 if (prefix != stock_prefix)
277 stop("Prefix multiply defined",
281 stop("Unable to record prefix", EX_SOFTWARE);
286 T_PATCH_ARG_LIST '=' T_STRING
288 if (patch_arg_list != NULL)
289 stop("Patch argument list multiply defined",
291 patch_arg_list = strdup($3);
292 if (patch_arg_list == NULL)
293 stop("Unable to record patch arg list", EX_SOFTWARE);
298 T_VERSION '=' T_STRING
303 T_REGISTER { cur_symtype = REGISTER; } reg_definition
309 if ($1->type != UNINITIALIZED) {
310 stop("Register multiply defined", EX_DATAERR);
314 cur_symbol->type = cur_symtype;
315 initialize_symbol(cur_symbol);
321 * Default to allowing everything in for registers
322 * with no bit or mask definitions.
324 if (cur_symbol->info.rinfo->valid_bitmask == 0)
325 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
327 if (cur_symbol->info.rinfo->size == 0)
328 cur_symbol->info.rinfo->size = 1;
331 * This might be useful for registers too.
333 if (cur_symbol->type != REGISTER) {
334 if (cur_symbol->info.rinfo->address == 0)
335 cur_symbol->info.rinfo->address =
337 sram_or_scb_offset +=
338 cur_symbol->info.rinfo->size;
346 | reg_attribute_list reg_attribute
369 cur_symbol->info.rinfo->address = $2;
376 cur_symbol->info.rinfo->size = $2;
377 if (scb_or_sram_symbol != NULL) {
381 max_addr = scb_or_sram_symbol->info.rinfo->address
382 + scb_or_sram_symbol->info.rinfo->size;
383 sym_max_addr = cur_symbol->info.rinfo->address
384 + cur_symbol->info.rinfo->size;
386 if (sym_max_addr > max_addr)
387 stop("SCB or SRAM space exhausted", EX_DATAERR);
395 cur_symbol->info.rinfo->mode = $2;
402 cur_symbol->info.rinfo->modes = $2;
411 | mode_list ',' mode_value
421 stop("Valid register modes range between 0 and 4.",
433 if (symbol->type != CONST) {
434 stop("Only \"const\" symbols allowed in "
435 "mode definitions.", EX_DATAERR);
438 if (symbol->info.cinfo->value > 4) {
439 stop("Valid register modes range between 0 and 4.",
443 $$ = (0x1 << symbol->info.cinfo->value);
454 '{' enum_entry_list '}'
455 | T_FIELD T_SYMBOL expression
457 process_field(FIELD, $2, $3.value);
460 enum_increment = 0x01 << (ffs($3.value) - 1);
462 '{' enum_entry_list '}'
463 | T_FIELD T_SYMBOL expression
465 process_field(FIELD, $2, $3.value);
476 '{' enum_entry_list '}'
477 | T_ENUM T_SYMBOL expression
479 process_field(ENUM, $2, $3.value);
482 enum_increment = 0x01 << (ffs($3.value) - 1);
484 '{' enum_entry_list '}'
489 | enum_entry_list ',' enum_entry
495 process_field(ENUM_ENTRY, $1, enum_next_value);
496 enum_next_value += enum_increment;
498 | T_SYMBOL expression
500 process_field(ENUM_ENTRY, $1, $2.value);
501 enum_next_value = $2.value + enum_increment;
506 T_MASK T_SYMBOL expression
508 process_field(MASK, $2, $3.value);
515 if ($2->type != UNINITIALIZED) {
516 stop("Re-definition of register alias",
521 initialize_symbol($2);
522 $2->info.ainfo->parent = cur_symbol;
529 if (accumulator.symbol != NULL) {
530 stop("Only one accumulator definition allowed",
534 accumulator.symbol = cur_symbol;
541 if (mode_ptr.symbol != NULL) {
542 stop("Only one mode pointer definition allowed",
546 mode_ptr.symbol = cur_symbol;
553 if (allones.symbol != NULL) {
554 stop("Only one definition of allones allowed",
558 allones.symbol = cur_symbol;
565 if (allzeros.symbol != NULL) {
566 stop("Only one definition of allzeros allowed",
570 allzeros.symbol = cur_symbol;
577 if (none.symbol != NULL) {
578 stop("Only one definition of none allowed",
582 none.symbol = cur_symbol;
589 if (sindex.symbol != NULL) {
590 stop("Only one definition of sindex allowed",
594 sindex.symbol = cur_symbol;
599 expression '|' expression
601 $$.value = $1.value | $3.value;
602 symlist_merge(&$$.referenced_syms,
604 &$3.referenced_syms);
606 | expression '&' expression
608 $$.value = $1.value & $3.value;
609 symlist_merge(&$$.referenced_syms,
611 &$3.referenced_syms);
613 | expression '+' expression
615 $$.value = $1.value + $3.value;
616 symlist_merge(&$$.referenced_syms,
618 &$3.referenced_syms);
620 | expression '-' expression
622 $$.value = $1.value - $3.value;
623 symlist_merge(&($$.referenced_syms),
624 &($1.referenced_syms),
625 &($3.referenced_syms));
627 | expression '*' expression
629 $$.value = $1.value * $3.value;
630 symlist_merge(&($$.referenced_syms),
631 &($1.referenced_syms),
632 &($3.referenced_syms));
634 | expression '/' expression
636 $$.value = $1.value / $3.value;
637 symlist_merge(&($$.referenced_syms),
638 &($1.referenced_syms),
639 &($3.referenced_syms));
641 | expression T_EXPR_LSHIFT expression
643 $$.value = $1.value << $3.value;
644 symlist_merge(&$$.referenced_syms,
646 &$3.referenced_syms);
648 | expression T_EXPR_RSHIFT expression
650 $$.value = $1.value >> $3.value;
651 symlist_merge(&$$.referenced_syms,
653 &$3.referenced_syms);
662 $$.value = (~$$.value) & 0xFF;
664 | '-' expression %prec UMINUS
667 $$.value = -$$.value;
672 SLIST_INIT(&$$.referenced_syms);
679 switch (symbol->type) {
681 symbol = $1->info.ainfo->parent;
685 $$.value = symbol->info.rinfo->address;
691 $$.value = symbol->info.finfo->value;
695 $$.value = symbol->info.cinfo->value;
700 snprintf(errbuf, sizeof(errbuf),
701 "Undefined symbol %s referenced",
703 stop(errbuf, EX_DATAERR);
708 SLIST_INIT(&$$.referenced_syms);
709 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
714 T_CONST T_SYMBOL expression
716 if ($2->type != UNINITIALIZED) {
717 stop("Re-definition of symbol as a constant",
722 initialize_symbol($2);
723 $2->info.cinfo->value = $3.value;
725 | T_CONST T_SYMBOL T_DOWNLOAD
728 stop("Invalid downloaded constant declaration",
732 if ($2->type != UNINITIALIZED) {
733 stop("Re-definition of symbol as a downloaded constant",
737 $2->type = DOWNLOAD_CONST;
738 initialize_symbol($2);
739 $2->info.cinfo->value = download_constant_count++;
746 if ($2->type != UNINITIALIZED) {
747 stop("Re-definition of symbol as a macro",
752 cur_symbol->type = MACRO;
753 initialize_symbol(cur_symbol);
758 macrodefn_prologue T_MACROBODY
762 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
765 cur_symbol->info.macroinfo->narg = $3;
771 /* Macros can take no arguments */
777 add_macro_arg($1, 0);
779 | macro_arglist ',' T_ARG
782 stop("Comma without preceeding argument in arg list",
787 add_macro_arg($3, $1);
794 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
796 cur_symbol = symtable_get(SRAM_SYMNAME);
797 cur_symtype = SRAMLOC;
798 cur_symbol->type = SRAMLOC;
799 initialize_symbol(cur_symbol);
803 sram_or_scb_offset = cur_symbol->info.rinfo->address;
807 scb_or_sram_symbol = cur_symbol;
809 scb_or_sram_attributes
813 scb_or_sram_symbol = NULL;
820 cur_symbol = symtable_get(SCB_SYMNAME);
821 cur_symtype = SCBLOC;
822 if (cur_symbol->type != UNINITIALIZED) {
823 stop("Only one SRAM definition allowed",
827 cur_symbol->type = SCBLOC;
828 initialize_symbol(cur_symbol);
829 /* 64 bytes of SCB space */
830 cur_symbol->info.rinfo->size = 64;
834 sram_or_scb_offset = cur_symbol->info.rinfo->address;
838 scb_or_sram_symbol = cur_symbol;
840 scb_or_sram_attributes
844 scb_or_sram_symbol = NULL;
848 scb_or_sram_attributes:
849 /* NULL definition is okay */
851 | scb_or_sram_reg_list
852 | modes scb_or_sram_reg_list
855 scb_or_sram_reg_list:
857 | scb_or_sram_reg_list reg_definition
863 process_register(&$1);
867 | T_SYMBOL '[' T_SYMBOL ']'
869 process_register(&$1);
870 if ($3->type != CONST) {
871 stop("register offset must be a constant", EX_DATAERR);
874 if (($3->info.cinfo->value + 1) > (unsigned)$1->info.rinfo->size) {
875 stop("Accessing offset beyond range of register",
880 $$.offset = $3->info.cinfo->value;
882 | T_SYMBOL '[' T_NUMBER ']'
884 process_register(&$1);
885 if (($3 + 1) > (unsigned)$1->info.rinfo->size) {
886 stop("Accessing offset beyond range of register",
895 if (accumulator.symbol == NULL) {
896 stop("No accumulator has been defined", EX_DATAERR);
899 $$.symbol = accumulator.symbol;
907 test_writable_symbol($1.symbol);
920 if ($1.value == 0 && is_download_const(&$1) == 0) {
921 snprintf(errbuf, sizeof(errbuf),
922 "\nExpression evaluates to 0 and thus "
923 "references the accumulator.\n "
924 "If this is the desired effect, use 'A' "
926 stop(errbuf, EX_DATAERR);
932 SLIST_INIT(&$$.referenced_syms);
933 symlist_add(&$$.referenced_syms, accumulator.symbol,
934 SYMLIST_INSERT_HEAD);
942 test_readable_symbol($1.symbol);
963 T_SET_SRC_MODE T_NUMBER ';'
970 T_SET_DST_MODE T_NUMBER ';'
976 critical_section_start:
979 critical_section_t *cs;
981 if (in_critical_section != FALSE) {
982 stop("Critical Section within Critical Section",
987 cs->begin_addr = instruction_ptr;
988 in_critical_section = TRUE;
992 critical_section_end:
995 critical_section_t *cs;
997 if (in_critical_section == FALSE) {
998 stop("Unballanced 'end_cs'", EX_DATAERR);
1001 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1002 cs->end_addr = instruction_ptr;
1003 in_critical_section = FALSE;
1016 if ($2->type != UNINITIALIZED) {
1017 stop("Program label multiply defined", EX_DATAERR);
1021 initialize_symbol($2);
1022 $2->info.linfo->address = instruction_ptr;
1023 $2->info.linfo->exported = $1;
1033 | T_SYMBOL '+' T_NUMBER
1038 | T_SYMBOL '-' T_NUMBER
1065 add_conditional($2);
1066 new_scope = scope_alloc();
1067 new_scope->type = SCOPE_IF;
1068 new_scope->begin_addr = instruction_ptr;
1069 new_scope->func_num = $2->info.condinfo->func_num;
1071 | T_ELSE T_IF T_CEXPR '{'
1074 scope_t *scope_context;
1075 scope_t *last_scope;
1078 * Ensure that the previous scope is either an
1079 * if or and else if.
1081 scope_context = SLIST_FIRST(&scope_stack);
1082 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1084 if (last_scope == NULL
1085 || last_scope->type == T_ELSE) {
1087 stop("'else if' without leading 'if'", EX_DATAERR);
1090 add_conditional($3);
1091 new_scope = scope_alloc();
1092 new_scope->type = SCOPE_ELSE_IF;
1093 new_scope->begin_addr = instruction_ptr;
1094 new_scope->func_num = $3->info.condinfo->func_num;
1099 scope_t *scope_context;
1100 scope_t *last_scope;
1103 * Ensure that the previous scope is either an
1104 * if or and else if.
1106 scope_context = SLIST_FIRST(&scope_stack);
1107 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1109 if (last_scope == NULL
1110 || last_scope->type == SCOPE_ELSE) {
1112 stop("'else' without leading 'if'", EX_DATAERR);
1115 new_scope = scope_alloc();
1116 new_scope->type = SCOPE_ELSE;
1117 new_scope->begin_addr = instruction_ptr;
1124 scope_t *scope_context;
1126 scope_context = SLIST_FIRST(&scope_stack);
1127 if (scope_context->type == SCOPE_ROOT) {
1128 stop("Unexpected '}' encountered", EX_DATAERR);
1132 scope_context->end_addr = instruction_ptr;
1135 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1137 process_scope(scope_context);
1139 if (SLIST_FIRST(&scope_stack) == NULL) {
1140 stop("Unexpected '}' encountered", EX_DATAERR);
1147 T_AND { $$ = AIC_OP_AND; }
1148 | T_XOR { $$ = AIC_OP_XOR; }
1149 | T_ADD { $$ = AIC_OP_ADD; }
1150 | T_ADC { $$ = AIC_OP_ADC; }
1154 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1156 format_1_instr($1, &$2, &$4, &$5, $6);
1161 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1163 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1168 T_INC destination opt_source ret ';'
1172 make_expression(&immed, 1);
1173 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1178 T_DEC destination opt_source ret ';'
1182 make_expression(&immed, -1);
1183 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1192 make_expression(&immed, -1);
1193 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1195 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1197 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1206 make_expression(&immed, 1);
1207 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1209 | T_STC destination ret ';'
1213 make_expression(&immed, 1);
1214 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1219 T_BMOV destination ',' source ',' immediate ret ';'
1221 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1226 T_MOV destination ',' source ret ';'
1230 make_expression(&immed, 1);
1231 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1236 T_MVI destination ',' immediate ret ';'
1239 && is_download_const(&$4) == 0) {
1243 * Allow move immediates of 0 so that macros,
1244 * that can't know the immediate's value and
1245 * otherwise compensate, still work.
1247 make_expression(&immed, 1);
1248 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1250 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1256 T_NOT destination opt_source ret ';'
1260 make_expression(&immed, 0xff);
1261 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1266 T_CLR destination ret ';'
1270 make_expression(&immed, 0xff);
1271 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1280 make_expression(&immed, 0xff);
1281 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1290 make_expression(&immed, 0xff);
1291 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1296 * This grammer differs from the one in the aic7xxx
1297 * reference manual since the grammer listed there is
1298 * ambiguous and causes a shift/reduce conflict.
1299 * It also seems more logical as the "immediate"
1300 * argument is listed as the second arg like the
1305 T_SHL { $$ = AIC_OP_SHL; }
1306 | T_SHR { $$ = AIC_OP_SHR; }
1307 | T_ROL { $$ = AIC_OP_ROL; }
1308 | T_ROR { $$ = AIC_OP_ROR; }
1312 f2_opcode destination ',' expression opt_source ret ';'
1314 format_2_instr($1, &$2, &$4, &$5, $6);
1319 T_JMP { $$ = AIC_OP_JMP; }
1320 | T_JC { $$ = AIC_OP_JC; }
1321 | T_JNC { $$ = AIC_OP_JNC; }
1322 | T_CALL { $$ = AIC_OP_CALL; }
1326 T_JZ { $$ = AIC_OP_JZ; }
1327 | T_JNZ { $$ = AIC_OP_JNZ; }
1331 T_JE { $$ = AIC_OP_JE; }
1332 | T_JNE { $$ = AIC_OP_JNE; }
1336 jmp_jc_jnc_call address ';'
1340 make_expression(&immed, 0);
1341 format_3_instr($1, &sindex, &immed, &$2);
1346 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1348 format_3_instr($5, &$2, &$4, &$6);
1353 T_TEST source ',' immediate_or_a jz_jnz address ';'
1355 format_3_instr($5, &$2, &$4, &$6);
1360 T_CMP source ',' immediate_or_a je_jne address ';'
1362 format_3_instr($5, &$2, &$4, &$6);
1367 T_MOV source jmp_jc_jnc_call address ';'
1371 make_expression(&immed, 0);
1372 format_3_instr($3, &$2, &immed, &$4);
1377 T_MVI immediate jmp_jc_jnc_call address ';'
1379 format_3_instr($3, &allzeros, &$2, &$4);
1386 process_field(unsigned int field_type, symbol_t *sym, int value)
1389 * Add the current register to its
1390 * symbol list, if it already exists,
1391 * warn if we are setting it to a
1392 * different value, or in the bit to
1393 * the "allowed bits" of this register.
1395 if (sym->type == UNINITIALIZED) {
1396 sym->type = field_type;
1397 initialize_symbol(sym);
1398 sym->info.finfo->value = value;
1399 if (field_type != ENUM_ENTRY) {
1400 if (field_type != MASK && value == 0) {
1401 stop("Empty Field, or Enum", EX_DATAERR);
1404 sym->info.finfo->value = value;
1405 sym->info.finfo->mask = value;
1406 } else if (field_symbol != NULL) {
1407 sym->info.finfo->mask = field_symbol->info.finfo->value;
1409 sym->info.finfo->mask = 0xFF;
1411 } else if (sym->type != field_type) {
1412 stop("Field definition mirrors a definition of the same "
1413 " name, but a different type", EX_DATAERR);
1415 } else if (value != sym->info.finfo->value) {
1416 stop("Field redefined with a conflicting value", EX_DATAERR);
1419 /* Fail if this symbol is already listed */
1420 if (symlist_search(&(sym->info.finfo->symrefs),
1421 cur_symbol->name) != NULL) {
1422 stop("Field defined multiple times for register", EX_DATAERR);
1425 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1426 SYMLIST_INSERT_HEAD);
1427 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1428 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1429 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1433 initialize_symbol(symbol_t *symbol)
1435 switch (symbol->type) {
1437 stop("Call to initialize_symbol with type field unset",
1444 symbol->info.rinfo =
1445 (struct reg_info *)malloc(sizeof(struct reg_info));
1446 if (symbol->info.rinfo == NULL) {
1447 stop("Can't create register info", EX_SOFTWARE);
1450 memset(symbol->info.rinfo, 0,
1451 sizeof(struct reg_info));
1452 SLIST_INIT(&(symbol->info.rinfo->fields));
1454 * Default to allowing access in all register modes
1455 * or to the mode specified by the SCB or SRAM space
1458 if (scb_or_sram_symbol != NULL)
1459 symbol->info.rinfo->modes =
1460 scb_or_sram_symbol->info.rinfo->modes;
1462 symbol->info.rinfo->modes = ~0;
1465 symbol->info.ainfo =
1466 (struct alias_info *)malloc(sizeof(struct alias_info));
1467 if (symbol->info.ainfo == NULL) {
1468 stop("Can't create alias info", EX_SOFTWARE);
1471 memset(symbol->info.ainfo, 0,
1472 sizeof(struct alias_info));
1478 symbol->info.finfo =
1479 (struct field_info *)malloc(sizeof(struct field_info));
1480 if (symbol->info.finfo == NULL) {
1481 stop("Can't create field info", EX_SOFTWARE);
1484 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1485 SLIST_INIT(&(symbol->info.finfo->symrefs));
1488 case DOWNLOAD_CONST:
1489 symbol->info.cinfo =
1490 (struct const_info *)malloc(sizeof(struct const_info));
1491 if (symbol->info.cinfo == NULL) {
1492 stop("Can't create alias info", EX_SOFTWARE);
1495 memset(symbol->info.cinfo, 0,
1496 sizeof(struct const_info));
1499 symbol->info.linfo =
1500 (struct label_info *)malloc(sizeof(struct label_info));
1501 if (symbol->info.linfo == NULL) {
1502 stop("Can't create label info", EX_SOFTWARE);
1505 memset(symbol->info.linfo, 0,
1506 sizeof(struct label_info));
1509 symbol->info.condinfo =
1510 (struct cond_info *)malloc(sizeof(struct cond_info));
1511 if (symbol->info.condinfo == NULL) {
1512 stop("Can't create conditional info", EX_SOFTWARE);
1515 memset(symbol->info.condinfo, 0,
1516 sizeof(struct cond_info));
1519 symbol->info.macroinfo =
1520 (struct macro_info *)malloc(sizeof(struct macro_info));
1521 if (symbol->info.macroinfo == NULL) {
1522 stop("Can't create macro info", EX_SOFTWARE);
1525 memset(symbol->info.macroinfo, 0,
1526 sizeof(struct macro_info));
1527 STAILQ_INIT(&symbol->info.macroinfo->args);
1530 stop("Call to initialize_symbol with invalid symbol type",
1538 add_macro_arg(const char *argtext, int argnum __unused)
1540 struct macro_arg *marg;
1544 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1545 stop("Invalid current symbol for adding macro arg",
1550 marg = (struct macro_arg *)malloc(sizeof(*marg));
1552 stop("Can't create macro_arg structure", EX_SOFTWARE);
1555 marg->replacement_text = NULL;
1556 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1557 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1559 if (retval >= (int)sizeof(regex_pattern)) {
1560 stop("Regex text buffer too small for arg",
1564 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1566 stop("Regex compilation failed", EX_SOFTWARE);
1569 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1573 add_macro_body(const char *bodytext)
1575 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1576 stop("Invalid current symbol for adding macro arg",
1580 cur_symbol->info.macroinfo->body = strdup(bodytext);
1581 if (cur_symbol->info.macroinfo->body == NULL) {
1582 stop("Can't duplicate macro body text", EX_SOFTWARE);
1588 process_register(symbol_t **p_symbol)
1590 symbol_t *symbol = *p_symbol;
1592 if (symbol->type == UNINITIALIZED) {
1593 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1595 stop(errbuf, EX_DATAERR);
1597 } else if (symbol->type == ALIAS) {
1598 *p_symbol = symbol->info.ainfo->parent;
1599 } else if ((symbol->type != REGISTER)
1600 && (symbol->type != SCBLOC)
1601 && (symbol->type != SRAMLOC)) {
1602 snprintf(errbuf, sizeof(errbuf),
1603 "Specified symbol %s is not a register",
1605 stop(errbuf, EX_DATAERR);
1610 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1611 symbol_ref_t *src, int ret)
1613 struct instruction *instr;
1614 struct ins_format1 *f1_instr;
1616 if (src->symbol == NULL)
1619 /* Test register permissions */
1620 test_writable_symbol(dest->symbol);
1621 test_readable_symbol(src->symbol);
1623 /* Ensure that immediate makes sense for this destination */
1624 type_check(dest->symbol, immed, opcode);
1626 /* Allocate sequencer space for the instruction and fill it out */
1627 instr = seq_alloc();
1628 f1_instr = &instr->format.format1;
1629 f1_instr->ret = ret ? 1 : 0;
1630 f1_instr->opcode = opcode;
1631 f1_instr->destination = dest->symbol->info.rinfo->address
1633 f1_instr->source = src->symbol->info.rinfo->address
1635 f1_instr->immediate = immed->value;
1637 if (is_download_const(immed))
1638 f1_instr->parity = 1;
1639 else if (dest->symbol == mode_ptr.symbol) {
1644 * Attempt to update mode information if
1645 * we are operating on the mode register.
1647 if (src->symbol == allones.symbol)
1649 else if (src->symbol == allzeros.symbol)
1651 else if (src->symbol == mode_ptr.symbol)
1652 src_value = (dst_mode << 4) | src_mode;
1658 dst_value = src_value & immed->value;
1661 dst_value = src_value ^ immed->value;
1664 dst_value = (src_value + immed->value) & 0xFF;
1667 dst_value = src_value | immed->value;
1670 dst_value = src_value;
1675 src_mode = dst_value & 0xF;
1676 dst_mode = (dst_value >> 4) & 0xF;
1680 symlist_free(&immed->referenced_syms);
1685 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1686 symbol_ref_t *src, int ret)
1688 struct instruction *instr;
1689 struct ins_format2 *f2_instr;
1690 uint8_t shift_control;
1692 if (src->symbol == NULL)
1695 /* Test register permissions */
1696 test_writable_symbol(dest->symbol);
1697 test_readable_symbol(src->symbol);
1699 /* Allocate sequencer space for the instruction and fill it out */
1700 instr = seq_alloc();
1701 f2_instr = &instr->format.format2;
1702 f2_instr->ret = ret ? 1 : 0;
1703 f2_instr->opcode = AIC_OP_ROL;
1704 f2_instr->destination = dest->symbol->info.rinfo->address
1706 f2_instr->source = src->symbol->info.rinfo->address
1708 if (places->value > 8 || places->value <= 0) {
1709 stop("illegal shift value", EX_DATAERR);
1714 if (places->value == 8)
1715 shift_control = 0xf0;
1717 shift_control = (places->value << 4) | places->value;
1720 if (places->value == 8) {
1721 shift_control = 0xf8;
1723 shift_control = (places->value << 4)
1724 | (8 - places->value)
1729 shift_control = places->value & 0x7;
1732 shift_control = (8 - places->value) | 0x08;
1735 shift_control = 0; /* Quiet Compiler */
1736 stop("Invalid shift operation specified", EX_SOFTWARE);
1740 f2_instr->shift_control = shift_control;
1741 symlist_free(&places->referenced_syms);
1746 format_3_instr(int opcode, symbol_ref_t *src,
1747 expression_t *immed, symbol_ref_t *address)
1749 struct instruction *instr;
1750 struct ins_format3 *f3_instr;
1753 /* Test register permissions */
1754 test_readable_symbol(src->symbol);
1756 /* Ensure that immediate makes sense for this source */
1757 type_check(src->symbol, immed, opcode);
1759 /* Allocate sequencer space for the instruction and fill it out */
1760 instr = seq_alloc();
1761 f3_instr = &instr->format.format3;
1762 if (address->symbol == NULL) {
1763 /* 'dot' referrence. Use the current instruction pointer */
1764 addr = instruction_ptr + address->offset;
1765 } else if (address->symbol->type == UNINITIALIZED) {
1766 /* forward reference */
1767 addr = address->offset;
1768 instr->patch_label = address->symbol;
1770 addr = address->symbol->info.linfo->address + address->offset;
1771 f3_instr->opcode = opcode;
1772 f3_instr->address = addr;
1773 f3_instr->source = src->symbol->info.rinfo->address
1775 f3_instr->immediate = immed->value;
1777 if (is_download_const(immed))
1778 f3_instr->parity = 1;
1780 symlist_free(&immed->referenced_syms);
1785 test_readable_symbol(symbol_t *symbol)
1788 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1789 snprintf(errbuf, sizeof(errbuf),
1790 "Register %s unavailable in source reg mode %d",
1791 symbol->name, src_mode);
1792 stop(errbuf, EX_DATAERR);
1795 if (symbol->info.rinfo->mode == WO) {
1796 stop("Write Only register specified as source",
1803 test_writable_symbol(symbol_t *symbol)
1806 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1807 snprintf(errbuf, sizeof(errbuf),
1808 "Register %s unavailable in destination reg mode %d",
1809 symbol->name, dst_mode);
1810 stop(errbuf, EX_DATAERR);
1813 if (symbol->info.rinfo->mode == RO) {
1814 stop("Read Only register specified as destination",
1821 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1823 symbol_node_t *node;
1827 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || AIC_OP_JZ)
1831 * Make sure that we aren't attempting to write something
1832 * that hasn't been defined. If this is an and operation,
1833 * this is a mask, so "undefined" bits are okay.
1836 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
1837 snprintf(errbuf, sizeof(errbuf),
1838 "Invalid bit(s) 0x%x in immediate written to %s",
1839 expression->value & ~symbol->info.rinfo->valid_bitmask,
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))