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.2 2003/06/17 04:28:22 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(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 #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
167 %token <value> T_NUMBER
169 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
173 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
175 %token <value> T_SHR T_SHL T_ROR T_ROL
177 %token <value> T_MVI T_MOV T_CLR T_BMOV
179 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
181 %token <value> T_ADD T_ADC
183 %token <value> T_INC T_DEC
185 %token <value> T_STC T_CLC
187 %token <value> T_CMP T_NOT T_XOR
189 %token <value> T_TEST T_AND
197 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
201 %token <sym> T_SYMBOL
205 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
207 %type <sym_ref> reg_symbol address destination source opt_source
209 %type <expression> expression immediate immediate_or_a
211 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
213 %type <value> mode_value mode_list macro_arglist
217 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
230 | program patch_arg_list
240 | program scratch_ram
246 | program set_src_mode
248 | program set_dst_mode
249 | critical_section_start
250 | program critical_section_start
251 | critical_section_end
252 | program critical_section_end
254 | program conditional
260 T_INCLUDE '<' T_PATH '>'
262 include_file($3, BRACKETED_INCLUDE);
264 | T_INCLUDE '"' T_PATH '"'
266 include_file($3, QUOTED_INCLUDE);
271 T_PREFIX '=' T_STRING
273 if (prefix != stock_prefix)
274 stop("Prefix multiply defined",
278 stop("Unable to record prefix", EX_SOFTWARE);
283 T_PATCH_ARG_LIST '=' T_STRING
285 if (patch_arg_list != NULL)
286 stop("Patch argument list multiply defined",
288 patch_arg_list = strdup($3);
289 if (patch_arg_list == NULL)
290 stop("Unable to record patch arg list", EX_SOFTWARE);
295 T_VERSION '=' T_STRING
300 T_REGISTER { cur_symtype = REGISTER; } reg_definition
306 if ($1->type != UNINITIALIZED) {
307 stop("Register multiply defined", EX_DATAERR);
311 cur_symbol->type = cur_symtype;
312 initialize_symbol(cur_symbol);
318 * Default to allowing everything in for registers
319 * with no bit or mask definitions.
321 if (cur_symbol->info.rinfo->valid_bitmask == 0)
322 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
324 if (cur_symbol->info.rinfo->size == 0)
325 cur_symbol->info.rinfo->size = 1;
328 * This might be useful for registers too.
330 if (cur_symbol->type != REGISTER) {
331 if (cur_symbol->info.rinfo->address == 0)
332 cur_symbol->info.rinfo->address =
334 sram_or_scb_offset +=
335 cur_symbol->info.rinfo->size;
343 | reg_attribute_list reg_attribute
366 cur_symbol->info.rinfo->address = $2;
373 cur_symbol->info.rinfo->size = $2;
374 if (scb_or_sram_symbol != NULL) {
378 max_addr = scb_or_sram_symbol->info.rinfo->address
379 + scb_or_sram_symbol->info.rinfo->size;
380 sym_max_addr = cur_symbol->info.rinfo->address
381 + cur_symbol->info.rinfo->size;
383 if (sym_max_addr > max_addr)
384 stop("SCB or SRAM space exhausted", EX_DATAERR);
392 cur_symbol->info.rinfo->mode = $2;
399 cur_symbol->info.rinfo->modes = $2;
408 | mode_list ',' mode_value
418 stop("Valid register modes range between 0 and 4.",
430 if (symbol->type != CONST) {
431 stop("Only \"const\" symbols allowed in "
432 "mode definitions.", EX_DATAERR);
435 if (symbol->info.cinfo->value > 4) {
436 stop("Valid register modes range between 0 and 4.",
440 $$ = (0x1 << symbol->info.cinfo->value);
451 '{' enum_entry_list '}'
452 | T_FIELD T_SYMBOL expression
454 process_field(FIELD, $2, $3.value);
457 enum_increment = 0x01 << (ffs($3.value) - 1);
459 '{' enum_entry_list '}'
460 | T_FIELD T_SYMBOL expression
462 process_field(FIELD, $2, $3.value);
473 '{' enum_entry_list '}'
474 | T_ENUM T_SYMBOL expression
476 process_field(ENUM, $2, $3.value);
479 enum_increment = 0x01 << (ffs($3.value) - 1);
481 '{' enum_entry_list '}'
486 | enum_entry_list ',' enum_entry
492 process_field(ENUM_ENTRY, $1, enum_next_value);
493 enum_next_value += enum_increment;
495 | T_SYMBOL expression
497 process_field(ENUM_ENTRY, $1, $2.value);
498 enum_next_value = $2.value + enum_increment;
503 T_MASK T_SYMBOL expression
505 process_field(MASK, $2, $3.value);
512 if ($2->type != UNINITIALIZED) {
513 stop("Re-definition of register alias",
518 initialize_symbol($2);
519 $2->info.ainfo->parent = cur_symbol;
526 if (accumulator.symbol != NULL) {
527 stop("Only one accumulator definition allowed",
531 accumulator.symbol = cur_symbol;
538 if (mode_ptr.symbol != NULL) {
539 stop("Only one mode pointer definition allowed",
543 mode_ptr.symbol = cur_symbol;
550 if (allones.symbol != NULL) {
551 stop("Only one definition of allones allowed",
555 allones.symbol = cur_symbol;
562 if (allzeros.symbol != NULL) {
563 stop("Only one definition of allzeros allowed",
567 allzeros.symbol = cur_symbol;
574 if (none.symbol != NULL) {
575 stop("Only one definition of none allowed",
579 none.symbol = cur_symbol;
586 if (sindex.symbol != NULL) {
587 stop("Only one definition of sindex allowed",
591 sindex.symbol = cur_symbol;
596 expression '|' expression
598 $$.value = $1.value | $3.value;
599 symlist_merge(&$$.referenced_syms,
601 &$3.referenced_syms);
603 | expression '&' expression
605 $$.value = $1.value & $3.value;
606 symlist_merge(&$$.referenced_syms,
608 &$3.referenced_syms);
610 | expression '+' expression
612 $$.value = $1.value + $3.value;
613 symlist_merge(&$$.referenced_syms,
615 &$3.referenced_syms);
617 | expression '-' expression
619 $$.value = $1.value - $3.value;
620 symlist_merge(&($$.referenced_syms),
621 &($1.referenced_syms),
622 &($3.referenced_syms));
624 | expression '*' expression
626 $$.value = $1.value * $3.value;
627 symlist_merge(&($$.referenced_syms),
628 &($1.referenced_syms),
629 &($3.referenced_syms));
631 | expression '/' expression
633 $$.value = $1.value / $3.value;
634 symlist_merge(&($$.referenced_syms),
635 &($1.referenced_syms),
636 &($3.referenced_syms));
638 | expression T_EXPR_LSHIFT expression
640 $$.value = $1.value << $3.value;
641 symlist_merge(&$$.referenced_syms,
643 &$3.referenced_syms);
645 | expression T_EXPR_RSHIFT expression
647 $$.value = $1.value >> $3.value;
648 symlist_merge(&$$.referenced_syms,
650 &$3.referenced_syms);
659 $$.value = (~$$.value) & 0xFF;
661 | '-' expression %prec UMINUS
664 $$.value = -$$.value;
669 SLIST_INIT(&$$.referenced_syms);
676 switch (symbol->type) {
678 symbol = $1->info.ainfo->parent;
682 $$.value = symbol->info.rinfo->address;
688 $$.value = symbol->info.finfo->value;
692 $$.value = symbol->info.cinfo->value;
697 snprintf(errbuf, sizeof(errbuf),
698 "Undefined symbol %s referenced",
700 stop(errbuf, EX_DATAERR);
705 SLIST_INIT(&$$.referenced_syms);
706 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
711 T_CONST T_SYMBOL expression
713 if ($2->type != UNINITIALIZED) {
714 stop("Re-definition of symbol as a constant",
719 initialize_symbol($2);
720 $2->info.cinfo->value = $3.value;
722 | T_CONST T_SYMBOL T_DOWNLOAD
725 stop("Invalid downloaded constant declaration",
729 if ($2->type != UNINITIALIZED) {
730 stop("Re-definition of symbol as a downloaded constant",
734 $2->type = DOWNLOAD_CONST;
735 initialize_symbol($2);
736 $2->info.cinfo->value = download_constant_count++;
743 if ($2->type != UNINITIALIZED) {
744 stop("Re-definition of symbol as a macro",
749 cur_symbol->type = MACRO;
750 initialize_symbol(cur_symbol);
755 macrodefn_prologue T_MACROBODY
759 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
762 cur_symbol->info.macroinfo->narg = $3;
768 /* Macros can take no arguments */
774 add_macro_arg($1, 0);
776 | macro_arglist ',' T_ARG
779 stop("Comma without preceeding argument in arg list",
784 add_macro_arg($3, $1);
791 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
793 cur_symbol = symtable_get(SRAM_SYMNAME);
794 cur_symtype = SRAMLOC;
795 cur_symbol->type = SRAMLOC;
796 initialize_symbol(cur_symbol);
800 sram_or_scb_offset = cur_symbol->info.rinfo->address;
804 scb_or_sram_symbol = cur_symbol;
806 scb_or_sram_attributes
810 scb_or_sram_symbol = NULL;
817 cur_symbol = symtable_get(SCB_SYMNAME);
818 cur_symtype = SCBLOC;
819 if (cur_symbol->type != UNINITIALIZED) {
820 stop("Only one SRAM definition allowed",
824 cur_symbol->type = SCBLOC;
825 initialize_symbol(cur_symbol);
826 /* 64 bytes of SCB space */
827 cur_symbol->info.rinfo->size = 64;
831 sram_or_scb_offset = cur_symbol->info.rinfo->address;
835 scb_or_sram_symbol = cur_symbol;
837 scb_or_sram_attributes
841 scb_or_sram_symbol = NULL;
845 scb_or_sram_attributes:
846 /* NULL definition is okay */
848 | scb_or_sram_reg_list
849 | modes scb_or_sram_reg_list
852 scb_or_sram_reg_list:
854 | scb_or_sram_reg_list reg_definition
860 process_register(&$1);
864 | T_SYMBOL '[' T_SYMBOL ']'
866 process_register(&$1);
867 if ($3->type != CONST) {
868 stop("register offset must be a constant", EX_DATAERR);
871 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
872 stop("Accessing offset beyond range of register",
877 $$.offset = $3->info.cinfo->value;
879 | T_SYMBOL '[' T_NUMBER ']'
881 process_register(&$1);
882 if (($3 + 1) > $1->info.rinfo->size) {
883 stop("Accessing offset beyond range of register",
892 if (accumulator.symbol == NULL) {
893 stop("No accumulator has been defined", EX_DATAERR);
896 $$.symbol = accumulator.symbol;
904 test_writable_symbol($1.symbol);
917 if ($1.value == 0 && is_download_const(&$1) == 0) {
918 snprintf(errbuf, sizeof(errbuf),
919 "\nExpression evaluates to 0 and thus "
920 "references the accumulator.\n "
921 "If this is the desired effect, use 'A' "
923 stop(errbuf, EX_DATAERR);
929 SLIST_INIT(&$$.referenced_syms);
930 symlist_add(&$$.referenced_syms, accumulator.symbol,
931 SYMLIST_INSERT_HEAD);
939 test_readable_symbol($1.symbol);
960 T_SET_SRC_MODE T_NUMBER ';'
967 T_SET_DST_MODE T_NUMBER ';'
973 critical_section_start:
976 critical_section_t *cs;
978 if (in_critical_section != FALSE) {
979 stop("Critical Section within Critical Section",
984 cs->begin_addr = instruction_ptr;
985 in_critical_section = TRUE;
989 critical_section_end:
992 critical_section_t *cs;
994 if (in_critical_section == FALSE) {
995 stop("Unballanced 'end_cs'", EX_DATAERR);
998 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
999 cs->end_addr = instruction_ptr;
1000 in_critical_section = FALSE;
1013 if ($2->type != UNINITIALIZED) {
1014 stop("Program label multiply defined", EX_DATAERR);
1018 initialize_symbol($2);
1019 $2->info.linfo->address = instruction_ptr;
1020 $2->info.linfo->exported = $1;
1030 | T_SYMBOL '+' T_NUMBER
1035 | T_SYMBOL '-' T_NUMBER
1062 add_conditional($2);
1063 new_scope = scope_alloc();
1064 new_scope->type = SCOPE_IF;
1065 new_scope->begin_addr = instruction_ptr;
1066 new_scope->func_num = $2->info.condinfo->func_num;
1068 | T_ELSE T_IF T_CEXPR '{'
1071 scope_t *scope_context;
1072 scope_t *last_scope;
1075 * Ensure that the previous scope is either an
1076 * if or and else if.
1078 scope_context = SLIST_FIRST(&scope_stack);
1079 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1081 if (last_scope == NULL
1082 || last_scope->type == T_ELSE) {
1084 stop("'else if' without leading 'if'", EX_DATAERR);
1087 add_conditional($3);
1088 new_scope = scope_alloc();
1089 new_scope->type = SCOPE_ELSE_IF;
1090 new_scope->begin_addr = instruction_ptr;
1091 new_scope->func_num = $3->info.condinfo->func_num;
1096 scope_t *scope_context;
1097 scope_t *last_scope;
1100 * Ensure that the previous scope is either an
1101 * if or and else if.
1103 scope_context = SLIST_FIRST(&scope_stack);
1104 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1106 if (last_scope == NULL
1107 || last_scope->type == SCOPE_ELSE) {
1109 stop("'else' without leading 'if'", EX_DATAERR);
1112 new_scope = scope_alloc();
1113 new_scope->type = SCOPE_ELSE;
1114 new_scope->begin_addr = instruction_ptr;
1121 scope_t *scope_context;
1123 scope_context = SLIST_FIRST(&scope_stack);
1124 if (scope_context->type == SCOPE_ROOT) {
1125 stop("Unexpected '}' encountered", EX_DATAERR);
1129 scope_context->end_addr = instruction_ptr;
1132 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1134 process_scope(scope_context);
1136 if (SLIST_FIRST(&scope_stack) == NULL) {
1137 stop("Unexpected '}' encountered", EX_DATAERR);
1144 T_AND { $$ = AIC_OP_AND; }
1145 | T_XOR { $$ = AIC_OP_XOR; }
1146 | T_ADD { $$ = AIC_OP_ADD; }
1147 | T_ADC { $$ = AIC_OP_ADC; }
1151 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1153 format_1_instr($1, &$2, &$4, &$5, $6);
1158 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1160 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1165 T_INC destination opt_source ret ';'
1169 make_expression(&immed, 1);
1170 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1175 T_DEC destination opt_source ret ';'
1179 make_expression(&immed, -1);
1180 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1189 make_expression(&immed, -1);
1190 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1192 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1194 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1203 make_expression(&immed, 1);
1204 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1206 | T_STC destination ret ';'
1210 make_expression(&immed, 1);
1211 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1216 T_BMOV destination ',' source ',' immediate ret ';'
1218 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1223 T_MOV destination ',' source ret ';'
1227 make_expression(&immed, 1);
1228 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1233 T_MVI destination ',' immediate ret ';'
1236 && is_download_const(&$4) == 0) {
1240 * Allow move immediates of 0 so that macros,
1241 * that can't know the immediate's value and
1242 * otherwise compensate, still work.
1244 make_expression(&immed, 1);
1245 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1247 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1253 T_NOT destination opt_source ret ';'
1257 make_expression(&immed, 0xff);
1258 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1263 T_CLR destination ret ';'
1267 make_expression(&immed, 0xff);
1268 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1277 make_expression(&immed, 0xff);
1278 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1287 make_expression(&immed, 0xff);
1288 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1293 * This grammer differs from the one in the aic7xxx
1294 * reference manual since the grammer listed there is
1295 * ambiguous and causes a shift/reduce conflict.
1296 * It also seems more logical as the "immediate"
1297 * argument is listed as the second arg like the
1302 T_SHL { $$ = AIC_OP_SHL; }
1303 | T_SHR { $$ = AIC_OP_SHR; }
1304 | T_ROL { $$ = AIC_OP_ROL; }
1305 | T_ROR { $$ = AIC_OP_ROR; }
1309 f2_opcode destination ',' expression opt_source ret ';'
1311 format_2_instr($1, &$2, &$4, &$5, $6);
1316 T_JMP { $$ = AIC_OP_JMP; }
1317 | T_JC { $$ = AIC_OP_JC; }
1318 | T_JNC { $$ = AIC_OP_JNC; }
1319 | T_CALL { $$ = AIC_OP_CALL; }
1323 T_JZ { $$ = AIC_OP_JZ; }
1324 | T_JNZ { $$ = AIC_OP_JNZ; }
1328 T_JE { $$ = AIC_OP_JE; }
1329 | T_JNE { $$ = AIC_OP_JNE; }
1333 jmp_jc_jnc_call address ';'
1337 make_expression(&immed, 0);
1338 format_3_instr($1, &sindex, &immed, &$2);
1343 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1345 format_3_instr($5, &$2, &$4, &$6);
1350 T_TEST source ',' immediate_or_a jz_jnz address ';'
1352 format_3_instr($5, &$2, &$4, &$6);
1357 T_CMP source ',' immediate_or_a je_jne address ';'
1359 format_3_instr($5, &$2, &$4, &$6);
1364 T_MOV source jmp_jc_jnc_call address ';'
1368 make_expression(&immed, 0);
1369 format_3_instr($3, &$2, &immed, &$4);
1374 T_MVI immediate jmp_jc_jnc_call address ';'
1376 format_3_instr($3, &allzeros, &$2, &$4);
1383 process_field(int field_type, symbol_t *sym, int value)
1386 * Add the current register to its
1387 * symbol list, if it already exists,
1388 * warn if we are setting it to a
1389 * different value, or in the bit to
1390 * the "allowed bits" of this register.
1392 if (sym->type == UNINITIALIZED) {
1393 sym->type = field_type;
1394 initialize_symbol(sym);
1395 sym->info.finfo->value = value;
1396 if (field_type != ENUM_ENTRY) {
1397 if (field_type != MASK && value == 0) {
1398 stop("Empty Field, or Enum", EX_DATAERR);
1401 sym->info.finfo->value = value;
1402 sym->info.finfo->mask = value;
1403 } else if (field_symbol != NULL) {
1404 sym->info.finfo->mask = field_symbol->info.finfo->value;
1406 sym->info.finfo->mask = 0xFF;
1408 } else if (sym->type != field_type) {
1409 stop("Field definition mirrors a definition of the same "
1410 " name, but a different type", EX_DATAERR);
1412 } else if (value != sym->info.finfo->value) {
1413 stop("Field redefined with a conflicting value", EX_DATAERR);
1416 /* Fail if this symbol is already listed */
1417 if (symlist_search(&(sym->info.finfo->symrefs),
1418 cur_symbol->name) != NULL) {
1419 stop("Field defined multiple times for register", EX_DATAERR);
1422 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1423 SYMLIST_INSERT_HEAD);
1424 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1425 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1426 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1430 initialize_symbol(symbol_t *symbol)
1432 switch (symbol->type) {
1434 stop("Call to initialize_symbol with type field unset",
1441 symbol->info.rinfo =
1442 (struct reg_info *)malloc(sizeof(struct reg_info));
1443 if (symbol->info.rinfo == NULL) {
1444 stop("Can't create register info", EX_SOFTWARE);
1447 memset(symbol->info.rinfo, 0,
1448 sizeof(struct reg_info));
1449 SLIST_INIT(&(symbol->info.rinfo->fields));
1451 * Default to allowing access in all register modes
1452 * or to the mode specified by the SCB or SRAM space
1455 if (scb_or_sram_symbol != NULL)
1456 symbol->info.rinfo->modes =
1457 scb_or_sram_symbol->info.rinfo->modes;
1459 symbol->info.rinfo->modes = ~0;
1462 symbol->info.ainfo =
1463 (struct alias_info *)malloc(sizeof(struct alias_info));
1464 if (symbol->info.ainfo == NULL) {
1465 stop("Can't create alias info", EX_SOFTWARE);
1468 memset(symbol->info.ainfo, 0,
1469 sizeof(struct alias_info));
1475 symbol->info.finfo =
1476 (struct field_info *)malloc(sizeof(struct field_info));
1477 if (symbol->info.finfo == NULL) {
1478 stop("Can't create field info", EX_SOFTWARE);
1481 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1482 SLIST_INIT(&(symbol->info.finfo->symrefs));
1485 case DOWNLOAD_CONST:
1486 symbol->info.cinfo =
1487 (struct const_info *)malloc(sizeof(struct const_info));
1488 if (symbol->info.cinfo == NULL) {
1489 stop("Can't create alias info", EX_SOFTWARE);
1492 memset(symbol->info.cinfo, 0,
1493 sizeof(struct const_info));
1496 symbol->info.linfo =
1497 (struct label_info *)malloc(sizeof(struct label_info));
1498 if (symbol->info.linfo == NULL) {
1499 stop("Can't create label info", EX_SOFTWARE);
1502 memset(symbol->info.linfo, 0,
1503 sizeof(struct label_info));
1506 symbol->info.condinfo =
1507 (struct cond_info *)malloc(sizeof(struct cond_info));
1508 if (symbol->info.condinfo == NULL) {
1509 stop("Can't create conditional info", EX_SOFTWARE);
1512 memset(symbol->info.condinfo, 0,
1513 sizeof(struct cond_info));
1516 symbol->info.macroinfo =
1517 (struct macro_info *)malloc(sizeof(struct macro_info));
1518 if (symbol->info.macroinfo == NULL) {
1519 stop("Can't create macro info", EX_SOFTWARE);
1522 memset(symbol->info.macroinfo, 0,
1523 sizeof(struct macro_info));
1524 STAILQ_INIT(&symbol->info.macroinfo->args);
1527 stop("Call to initialize_symbol with invalid symbol type",
1535 add_macro_arg(const char *argtext, int argnum)
1537 struct macro_arg *marg;
1542 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1543 stop("Invalid current symbol for adding macro arg",
1548 marg = (struct macro_arg *)malloc(sizeof(*marg));
1550 stop("Can't create macro_arg structure", EX_SOFTWARE);
1553 marg->replacement_text = NULL;
1554 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1555 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1557 if (retval >= sizeof(regex_pattern)) {
1558 stop("Regex text buffer too small for arg",
1562 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1564 stop("Regex compilation failed", EX_SOFTWARE);
1567 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1571 add_macro_body(const char *bodytext)
1573 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1574 stop("Invalid current symbol for adding macro arg",
1578 cur_symbol->info.macroinfo->body = strdup(bodytext);
1579 if (cur_symbol->info.macroinfo->body == NULL) {
1580 stop("Can't duplicate macro body text", EX_SOFTWARE);
1586 process_register(symbol_t **p_symbol)
1588 symbol_t *symbol = *p_symbol;
1590 if (symbol->type == UNINITIALIZED) {
1591 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1593 stop(errbuf, EX_DATAERR);
1595 } else if (symbol->type == ALIAS) {
1596 *p_symbol = symbol->info.ainfo->parent;
1597 } else if ((symbol->type != REGISTER)
1598 && (symbol->type != SCBLOC)
1599 && (symbol->type != SRAMLOC)) {
1600 snprintf(errbuf, sizeof(errbuf),
1601 "Specified symbol %s is not a register",
1603 stop(errbuf, EX_DATAERR);
1608 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1609 symbol_ref_t *src, int ret)
1611 struct instruction *instr;
1612 struct ins_format1 *f1_instr;
1614 if (src->symbol == NULL)
1617 /* Test register permissions */
1618 test_writable_symbol(dest->symbol);
1619 test_readable_symbol(src->symbol);
1621 /* Ensure that immediate makes sense for this destination */
1622 type_check(dest->symbol, immed, opcode);
1624 /* Allocate sequencer space for the instruction and fill it out */
1625 instr = seq_alloc();
1626 f1_instr = &instr->format.format1;
1627 f1_instr->ret = ret ? 1 : 0;
1628 f1_instr->opcode = opcode;
1629 f1_instr->destination = dest->symbol->info.rinfo->address
1631 f1_instr->source = src->symbol->info.rinfo->address
1633 f1_instr->immediate = immed->value;
1635 if (is_download_const(immed))
1636 f1_instr->parity = 1;
1637 else if (dest->symbol == mode_ptr.symbol) {
1642 * Attempt to update mode information if
1643 * we are operating on the mode register.
1645 if (src->symbol == allones.symbol)
1647 else if (src->symbol == allzeros.symbol)
1649 else if (src->symbol == mode_ptr.symbol)
1650 src_value = (dst_mode << 4) | src_mode;
1656 dst_value = src_value & immed->value;
1659 dst_value = src_value ^ immed->value;
1662 dst_value = (src_value + immed->value) & 0xFF;
1665 dst_value = src_value | immed->value;
1668 dst_value = src_value;
1673 src_mode = dst_value & 0xF;
1674 dst_mode = (dst_value >> 4) & 0xF;
1678 symlist_free(&immed->referenced_syms);
1683 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1684 symbol_ref_t *src, int ret)
1686 struct instruction *instr;
1687 struct ins_format2 *f2_instr;
1688 uint8_t shift_control;
1690 if (src->symbol == NULL)
1693 /* Test register permissions */
1694 test_writable_symbol(dest->symbol);
1695 test_readable_symbol(src->symbol);
1697 /* Allocate sequencer space for the instruction and fill it out */
1698 instr = seq_alloc();
1699 f2_instr = &instr->format.format2;
1700 f2_instr->ret = ret ? 1 : 0;
1701 f2_instr->opcode = AIC_OP_ROL;
1702 f2_instr->destination = dest->symbol->info.rinfo->address
1704 f2_instr->source = src->symbol->info.rinfo->address
1706 if (places->value > 8 || places->value <= 0) {
1707 stop("illegal shift value", EX_DATAERR);
1712 if (places->value == 8)
1713 shift_control = 0xf0;
1715 shift_control = (places->value << 4) | places->value;
1718 if (places->value == 8) {
1719 shift_control = 0xf8;
1721 shift_control = (places->value << 4)
1722 | (8 - places->value)
1727 shift_control = places->value & 0x7;
1730 shift_control = (8 - places->value) | 0x08;
1733 shift_control = 0; /* Quiet Compiler */
1734 stop("Invalid shift operation specified", EX_SOFTWARE);
1738 f2_instr->shift_control = shift_control;
1739 symlist_free(&places->referenced_syms);
1744 format_3_instr(int opcode, symbol_ref_t *src,
1745 expression_t *immed, symbol_ref_t *address)
1747 struct instruction *instr;
1748 struct ins_format3 *f3_instr;
1751 /* Test register permissions */
1752 test_readable_symbol(src->symbol);
1754 /* Ensure that immediate makes sense for this source */
1755 type_check(src->symbol, immed, opcode);
1757 /* Allocate sequencer space for the instruction and fill it out */
1758 instr = seq_alloc();
1759 f3_instr = &instr->format.format3;
1760 if (address->symbol == NULL) {
1761 /* 'dot' referrence. Use the current instruction pointer */
1762 addr = instruction_ptr + address->offset;
1763 } else if (address->symbol->type == UNINITIALIZED) {
1764 /* forward reference */
1765 addr = address->offset;
1766 instr->patch_label = address->symbol;
1768 addr = address->symbol->info.linfo->address + address->offset;
1769 f3_instr->opcode = opcode;
1770 f3_instr->address = addr;
1771 f3_instr->source = src->symbol->info.rinfo->address
1773 f3_instr->immediate = immed->value;
1775 if (is_download_const(immed))
1776 f3_instr->parity = 1;
1778 symlist_free(&immed->referenced_syms);
1783 test_readable_symbol(symbol_t *symbol)
1786 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1787 snprintf(errbuf, sizeof(errbuf),
1788 "Register %s unavailable in source reg mode %d",
1789 symbol->name, src_mode);
1790 stop(errbuf, EX_DATAERR);
1793 if (symbol->info.rinfo->mode == WO) {
1794 stop("Write Only register specified as source",
1801 test_writable_symbol(symbol_t *symbol)
1804 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1805 snprintf(errbuf, sizeof(errbuf),
1806 "Register %s unavailable in destination reg mode %d",
1807 symbol->name, dst_mode);
1808 stop(errbuf, EX_DATAERR);
1811 if (symbol->info.rinfo->mode == RO) {
1812 stop("Read Only register specified as destination",
1819 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1821 symbol_node_t *node;
1825 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || AIC_OP_JZ)
1829 * Make sure that we aren't attempting to write something
1830 * that hasn't been defined. If this is an and operation,
1831 * this is a mask, so "undefined" bits are okay.
1834 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
1835 snprintf(errbuf, sizeof(errbuf),
1836 "Invalid bit(s) 0x%x in immediate written to %s",
1837 expression->value & ~symbol->info.rinfo->valid_bitmask,
1839 stop(errbuf, EX_DATAERR);
1844 * Now make sure that all of the symbols referenced by the
1845 * expression are defined for this register.
1847 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1848 for(node = expression->referenced_syms.slh_first;
1850 node = node->links.sle_next) {
1851 if ((node->symbol->type == MASK
1852 || node->symbol->type == FIELD
1853 || node->symbol->type == ENUM
1854 || node->symbol->type == ENUM_ENTRY)
1855 && symlist_search(&node->symbol->info.finfo->symrefs,
1856 symbol->name) == NULL) {
1857 snprintf(errbuf, sizeof(errbuf),
1858 "Invalid field or mask %s "
1860 node->symbol->name, symbol->name);
1861 stop(errbuf, EX_DATAERR);
1869 make_expression(expression_t *immed, int value)
1871 SLIST_INIT(&immed->referenced_syms);
1872 immed->value = value & 0xff;
1876 add_conditional(symbol_t *symbol)
1878 static int numfuncs;
1880 if (numfuncs == 0) {
1881 /* add a special conditional, "0" */
1882 symbol_t *false_func;
1884 false_func = symtable_get("0");
1885 if (false_func->type != UNINITIALIZED) {
1886 stop("Conditional expression '0' "
1887 "conflicts with a symbol", EX_DATAERR);
1890 false_func->type = CONDITIONAL;
1891 initialize_symbol(false_func);
1892 false_func->info.condinfo->func_num = numfuncs++;
1893 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1896 /* This condition has occurred before */
1897 if (symbol->type == CONDITIONAL)
1900 if (symbol->type != UNINITIALIZED) {
1901 stop("Conditional expression conflicts with a symbol",
1906 symbol->type = CONDITIONAL;
1907 initialize_symbol(symbol);
1908 symbol->info.condinfo->func_num = numfuncs++;
1909 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1913 add_version(const char *verstring)
1915 const char prefix[] = " * ";
1919 newlen = strlen(verstring) + strlen(prefix);
1921 if (versions != NULL)
1922 oldlen = strlen(versions);
1923 versions = realloc(versions, newlen + oldlen + 2);
1924 if (versions == NULL)
1925 stop("Can't allocate version string", EX_SOFTWARE);
1926 strcpy(&versions[oldlen], prefix);
1927 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1928 versions[newlen + oldlen] = '\n';
1929 versions[newlen + oldlen + 1] = '\0';
1933 yyerror(const char *string)
1935 stop(string, EX_DATAERR);
1939 is_download_const(expression_t *immed)
1941 if ((immed->referenced_syms.slh_first != NULL)
1942 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))