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 $
46 #include <sys/types.h>
54 #include <sys/queue.h>
57 #include "aicasm_symbol.h"
58 #include "aicasm_insformat.h"
62 char stock_prefix[] = "aic_";
63 char *prefix = stock_prefix;
66 static char errbuf[255];
67 static char regex_pattern[255];
68 static symbol_t *cur_symbol;
69 static symbol_t *field_symbol;
70 static symbol_t *scb_or_sram_symbol;
71 static symtype cur_symtype;
72 static symbol_ref_t accumulator;
73 static symbol_ref_t mode_ptr;
74 static symbol_ref_t allones;
75 static symbol_ref_t allzeros;
76 static symbol_ref_t none;
77 static symbol_ref_t sindex;
78 static int instruction_ptr;
80 static int sram_or_scb_offset;
81 static int download_constant_count;
82 static int in_critical_section;
83 static u_int enum_increment;
84 static u_int enum_next_value;
86 static void process_field(unsigned int field_type, symbol_t *sym, int mask);
87 static void initialize_symbol(symbol_t *symbol);
88 static void add_macro_arg(const char *argtext, int position);
89 static void add_macro_body(const char *bodytext);
90 static void process_register(symbol_t **p_symbol);
91 static void format_1_instr(int opcode, symbol_ref_t *dest,
92 expression_t *immed, symbol_ref_t *src, int ret);
93 static void format_2_instr(int opcode, symbol_ref_t *dest,
94 expression_t *places, symbol_ref_t *src, int ret);
95 static void format_3_instr(int opcode, symbol_ref_t *src,
96 expression_t *immed, symbol_ref_t *address);
97 static void test_readable_symbol(symbol_t *symbol);
98 static void test_writable_symbol(symbol_t *symbol);
99 static void type_check(symbol_t *symbol, expression_t *expression, int and_op);
100 static void make_expression(expression_t *immed, int value);
101 static void add_conditional(symbol_t *symbol);
102 static void add_version(const char *verstring);
103 static int is_download_const(expression_t *immed);
105 extern int yylex (void);
107 #define SRAM_SYMNAME "SRAM_BASE"
108 #define SCB_SYMNAME "SCB_BASE"
115 symbol_ref_t sym_ref;
116 expression_t expression;
121 %token <value> T_CONST
139 %token <value> T_ADDRESS
147 %token T_SET_SRC_MODE
149 %token T_SET_DST_MODE
151 %token <value> T_MODE
163 %token <value> T_NUMBER
165 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
169 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
171 %token <value> T_SHR T_SHL T_ROR T_ROL
173 %token <value> T_MVI T_MOV T_CLR T_BMOV
175 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
177 %token <value> T_ADD T_ADC
179 %token <value> T_INC T_DEC
181 %token <value> T_STC T_CLC
183 %token <value> T_CMP T_NOT T_XOR
185 %token <value> T_TEST T_AND
193 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
197 %token <sym> T_SYMBOL
201 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
203 %type <sym_ref> reg_symbol address destination source opt_source
205 %type <expression> expression immediate immediate_or_a
207 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
209 %type <value> mode_value mode_list macro_arglist
213 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
226 | program patch_arg_list
236 | program scratch_ram
242 | program set_src_mode
244 | program set_dst_mode
245 | critical_section_start
246 | program critical_section_start
247 | critical_section_end
248 | program critical_section_end
250 | program conditional
256 T_INCLUDE '<' T_PATH '>'
258 include_file($3, BRACKETED_INCLUDE);
260 | T_INCLUDE '"' T_PATH '"'
262 include_file($3, QUOTED_INCLUDE);
267 T_PREFIX '=' T_STRING
269 if (prefix != stock_prefix)
270 stop("Prefix multiply defined",
274 stop("Unable to record prefix", EX_SOFTWARE);
279 T_PATCH_ARG_LIST '=' T_STRING
281 if (patch_arg_list != NULL)
282 stop("Patch argument list multiply defined",
284 patch_arg_list = strdup($3);
285 if (patch_arg_list == NULL)
286 stop("Unable to record patch arg list", EX_SOFTWARE);
291 T_VERSION '=' T_STRING
296 T_REGISTER { cur_symtype = REGISTER; } reg_definition
302 if ($1->type != UNINITIALIZED) {
303 stop("Register multiply defined", EX_DATAERR);
307 cur_symbol->type = cur_symtype;
308 initialize_symbol(cur_symbol);
314 * Default to allowing everything in for registers
315 * with no bit or mask definitions.
317 if (cur_symbol->info.rinfo->valid_bitmask == 0)
318 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
320 if (cur_symbol->info.rinfo->size == 0)
321 cur_symbol->info.rinfo->size = 1;
324 * This might be useful for registers too.
326 if (cur_symbol->type != REGISTER) {
327 if (cur_symbol->info.rinfo->address == 0)
328 cur_symbol->info.rinfo->address =
330 sram_or_scb_offset +=
331 cur_symbol->info.rinfo->size;
339 | reg_attribute_list reg_attribute
362 cur_symbol->info.rinfo->address = $2;
369 cur_symbol->info.rinfo->size = $2;
370 if (scb_or_sram_symbol != NULL) {
374 max_addr = scb_or_sram_symbol->info.rinfo->address
375 + scb_or_sram_symbol->info.rinfo->size;
376 sym_max_addr = cur_symbol->info.rinfo->address
377 + cur_symbol->info.rinfo->size;
379 if (sym_max_addr > max_addr)
380 stop("SCB or SRAM space exhausted", EX_DATAERR);
388 cur_symbol->info.rinfo->mode = $2;
395 cur_symbol->info.rinfo->modes = $2;
404 | mode_list ',' mode_value
414 stop("Valid register modes range between 0 and 4.",
426 if (symbol->type != CONST) {
427 stop("Only \"const\" symbols allowed in "
428 "mode definitions.", EX_DATAERR);
431 if (symbol->info.cinfo->value > 4) {
432 stop("Valid register modes range between 0 and 4.",
436 $$ = (0x1 << symbol->info.cinfo->value);
447 '{' enum_entry_list '}'
448 | T_FIELD T_SYMBOL expression
450 process_field(FIELD, $2, $3.value);
453 enum_increment = 0x01 << (ffs($3.value) - 1);
455 '{' enum_entry_list '}'
456 | T_FIELD T_SYMBOL expression
458 process_field(FIELD, $2, $3.value);
469 '{' enum_entry_list '}'
470 | T_ENUM T_SYMBOL expression
472 process_field(ENUM, $2, $3.value);
475 enum_increment = 0x01 << (ffs($3.value) - 1);
477 '{' enum_entry_list '}'
482 | enum_entry_list ',' enum_entry
488 process_field(ENUM_ENTRY, $1, enum_next_value);
489 enum_next_value += enum_increment;
491 | T_SYMBOL expression
493 process_field(ENUM_ENTRY, $1, $2.value);
494 enum_next_value = $2.value + enum_increment;
499 T_MASK T_SYMBOL expression
501 process_field(MASK, $2, $3.value);
508 if ($2->type != UNINITIALIZED) {
509 stop("Re-definition of register alias",
514 initialize_symbol($2);
515 $2->info.ainfo->parent = cur_symbol;
522 if (accumulator.symbol != NULL) {
523 stop("Only one accumulator definition allowed",
527 accumulator.symbol = cur_symbol;
534 if (mode_ptr.symbol != NULL) {
535 stop("Only one mode pointer definition allowed",
539 mode_ptr.symbol = cur_symbol;
546 if (allones.symbol != NULL) {
547 stop("Only one definition of allones allowed",
551 allones.symbol = cur_symbol;
558 if (allzeros.symbol != NULL) {
559 stop("Only one definition of allzeros allowed",
563 allzeros.symbol = cur_symbol;
570 if (none.symbol != NULL) {
571 stop("Only one definition of none allowed",
575 none.symbol = cur_symbol;
582 if (sindex.symbol != NULL) {
583 stop("Only one definition of sindex allowed",
587 sindex.symbol = cur_symbol;
592 expression '|' expression
594 $$.value = $1.value | $3.value;
595 symlist_merge(&$$.referenced_syms,
597 &$3.referenced_syms);
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),
617 &($1.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 T_EXPR_LSHIFT expression
636 $$.value = $1.value << $3.value;
637 symlist_merge(&$$.referenced_syms,
639 &$3.referenced_syms);
641 | expression T_EXPR_RSHIFT expression
643 $$.value = $1.value >> $3.value;
644 symlist_merge(&$$.referenced_syms,
646 &$3.referenced_syms);
655 $$.value = (~$$.value) & 0xFF;
657 | '-' expression %prec UMINUS
660 $$.value = -$$.value;
665 SLIST_INIT(&$$.referenced_syms);
672 switch (symbol->type) {
674 symbol = $1->info.ainfo->parent;
678 $$.value = symbol->info.rinfo->address;
684 $$.value = symbol->info.finfo->value;
688 $$.value = symbol->info.cinfo->value;
693 snprintf(errbuf, sizeof(errbuf),
694 "Undefined symbol %s referenced",
696 stop(errbuf, EX_DATAERR);
701 SLIST_INIT(&$$.referenced_syms);
702 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
707 T_CONST T_SYMBOL expression
709 if ($2->type != UNINITIALIZED) {
710 stop("Re-definition of symbol as a constant",
715 initialize_symbol($2);
716 $2->info.cinfo->value = $3.value;
718 | T_CONST T_SYMBOL T_DOWNLOAD
721 stop("Invalid downloaded constant declaration",
725 if ($2->type != UNINITIALIZED) {
726 stop("Re-definition of symbol as a downloaded constant",
730 $2->type = DOWNLOAD_CONST;
731 initialize_symbol($2);
732 $2->info.cinfo->value = download_constant_count++;
739 if ($2->type != UNINITIALIZED) {
740 stop("Re-definition of symbol as a macro",
745 cur_symbol->type = MACRO;
746 initialize_symbol(cur_symbol);
751 macrodefn_prologue T_MACROBODY
755 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
758 cur_symbol->info.macroinfo->narg = $3;
764 /* Macros can take no arguments */
770 add_macro_arg($1, 0);
772 | macro_arglist ',' T_ARG
775 stop("Comma without preceding argument in arg list",
780 add_macro_arg($3, $1);
787 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
789 cur_symbol = symtable_get(SRAM_SYMNAME);
790 cur_symtype = SRAMLOC;
791 cur_symbol->type = SRAMLOC;
792 initialize_symbol(cur_symbol);
796 sram_or_scb_offset = cur_symbol->info.rinfo->address;
800 scb_or_sram_symbol = cur_symbol;
802 scb_or_sram_attributes
806 scb_or_sram_symbol = NULL;
813 cur_symbol = symtable_get(SCB_SYMNAME);
814 cur_symtype = SCBLOC;
815 if (cur_symbol->type != UNINITIALIZED) {
816 stop("Only one SRAM definition allowed",
820 cur_symbol->type = SCBLOC;
821 initialize_symbol(cur_symbol);
822 /* 64 bytes of SCB space */
823 cur_symbol->info.rinfo->size = 64;
827 sram_or_scb_offset = cur_symbol->info.rinfo->address;
831 scb_or_sram_symbol = cur_symbol;
833 scb_or_sram_attributes
837 scb_or_sram_symbol = NULL;
841 scb_or_sram_attributes:
842 /* NULL definition is okay */
844 | scb_or_sram_reg_list
845 | modes scb_or_sram_reg_list
848 scb_or_sram_reg_list:
850 | scb_or_sram_reg_list reg_definition
856 process_register(&$1);
860 | T_SYMBOL '[' T_SYMBOL ']'
862 process_register(&$1);
863 if ($3->type != CONST) {
864 stop("register offset must be a constant", EX_DATAERR);
867 if (($3->info.cinfo->value + 1) > (unsigned)$1->info.rinfo->size) {
868 stop("Accessing offset beyond range of register",
873 $$.offset = $3->info.cinfo->value;
875 | T_SYMBOL '[' T_NUMBER ']'
877 process_register(&$1);
878 if (($3 + 1) > (unsigned)$1->info.rinfo->size) {
879 stop("Accessing offset beyond range of register",
888 if (accumulator.symbol == NULL) {
889 stop("No accumulator has been defined", EX_DATAERR);
892 $$.symbol = accumulator.symbol;
900 test_writable_symbol($1.symbol);
913 if ($1.value == 0 && is_download_const(&$1) == 0) {
914 snprintf(errbuf, sizeof(errbuf),
915 "\nExpression evaluates to 0 and thus "
916 "references the accumulator.\n "
917 "If this is the desired effect, use 'A' "
919 stop(errbuf, EX_DATAERR);
925 SLIST_INIT(&$$.referenced_syms);
926 symlist_add(&$$.referenced_syms, accumulator.symbol,
927 SYMLIST_INSERT_HEAD);
935 test_readable_symbol($1.symbol);
956 T_SET_SRC_MODE T_NUMBER ';'
963 T_SET_DST_MODE T_NUMBER ';'
969 critical_section_start:
972 critical_section_t *cs;
974 if (in_critical_section != FALSE) {
975 stop("Critical Section within Critical Section",
980 cs->begin_addr = instruction_ptr;
981 in_critical_section = TRUE;
985 critical_section_end:
988 critical_section_t *cs;
990 if (in_critical_section == FALSE) {
991 stop("Unballanced 'end_cs'", EX_DATAERR);
994 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
995 cs->end_addr = instruction_ptr;
996 in_critical_section = FALSE;
1009 if ($2->type != UNINITIALIZED) {
1010 stop("Program label multiply defined", EX_DATAERR);
1014 initialize_symbol($2);
1015 $2->info.linfo->address = instruction_ptr;
1016 $2->info.linfo->exported = $1;
1026 | T_SYMBOL '+' T_NUMBER
1031 | T_SYMBOL '-' T_NUMBER
1058 add_conditional($2);
1059 new_scope = scope_alloc();
1060 new_scope->type = SCOPE_IF;
1061 new_scope->begin_addr = instruction_ptr;
1062 new_scope->func_num = $2->info.condinfo->func_num;
1064 | T_ELSE T_IF T_CEXPR '{'
1067 scope_t *scope_context;
1068 scope_t *last_scope;
1071 * Ensure that the previous scope is either an
1072 * if or and else if.
1074 scope_context = SLIST_FIRST(&scope_stack);
1075 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1077 if (last_scope == NULL
1078 || last_scope->type == SCOPE_ELSE) {
1080 stop("'else if' without leading 'if'", EX_DATAERR);
1083 add_conditional($3);
1084 new_scope = scope_alloc();
1085 new_scope->type = SCOPE_ELSE_IF;
1086 new_scope->begin_addr = instruction_ptr;
1087 new_scope->func_num = $3->info.condinfo->func_num;
1092 scope_t *scope_context;
1093 scope_t *last_scope;
1096 * Ensure that the previous scope is either an
1097 * if or and else if.
1099 scope_context = SLIST_FIRST(&scope_stack);
1100 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1102 if (last_scope == NULL
1103 || last_scope->type == SCOPE_ELSE) {
1105 stop("'else' without leading 'if'", EX_DATAERR);
1108 new_scope = scope_alloc();
1109 new_scope->type = SCOPE_ELSE;
1110 new_scope->begin_addr = instruction_ptr;
1117 scope_t *scope_context;
1119 scope_context = SLIST_FIRST(&scope_stack);
1120 if (scope_context->type == SCOPE_ROOT) {
1121 stop("Unexpected '}' encountered", EX_DATAERR);
1125 scope_context->end_addr = instruction_ptr;
1128 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1130 process_scope(scope_context);
1132 if (SLIST_FIRST(&scope_stack) == NULL) {
1133 stop("Unexpected '}' encountered", EX_DATAERR);
1140 T_AND { $$ = AIC_OP_AND; }
1141 | T_XOR { $$ = AIC_OP_XOR; }
1142 | T_ADD { $$ = AIC_OP_ADD; }
1143 | T_ADC { $$ = AIC_OP_ADC; }
1147 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1149 format_1_instr($1, &$2, &$4, &$5, $6);
1154 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1156 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1161 T_INC destination opt_source ret ';'
1165 make_expression(&immed, 1);
1166 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1171 T_DEC destination opt_source ret ';'
1175 make_expression(&immed, -1);
1176 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1185 make_expression(&immed, -1);
1186 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1188 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1190 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1199 make_expression(&immed, 1);
1200 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1202 | T_STC destination ret ';'
1206 make_expression(&immed, 1);
1207 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1212 T_BMOV destination ',' source ',' immediate ret ';'
1214 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1219 T_MOV destination ',' source ret ';'
1223 make_expression(&immed, 1);
1224 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1229 T_MVI destination ',' immediate ret ';'
1232 && is_download_const(&$4) == 0) {
1236 * Allow move immediates of 0 so that macros,
1237 * that can't know the immediate's value and
1238 * otherwise compensate, still work.
1240 make_expression(&immed, 1);
1241 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1243 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1249 T_NOT destination opt_source ret ';'
1253 make_expression(&immed, 0xff);
1254 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1259 T_CLR destination ret ';'
1263 make_expression(&immed, 0xff);
1264 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1273 make_expression(&immed, 0xff);
1274 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1283 make_expression(&immed, 0xff);
1284 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1289 * This grammer differs from the one in the aic7xxx
1290 * reference manual since the grammer listed there is
1291 * ambiguous and causes a shift/reduce conflict.
1292 * It also seems more logical as the "immediate"
1293 * argument is listed as the second arg like the
1298 T_SHL { $$ = AIC_OP_SHL; }
1299 | T_SHR { $$ = AIC_OP_SHR; }
1300 | T_ROL { $$ = AIC_OP_ROL; }
1301 | T_ROR { $$ = AIC_OP_ROR; }
1305 f2_opcode destination ',' expression opt_source ret ';'
1307 format_2_instr($1, &$2, &$4, &$5, $6);
1312 T_JMP { $$ = AIC_OP_JMP; }
1313 | T_JC { $$ = AIC_OP_JC; }
1314 | T_JNC { $$ = AIC_OP_JNC; }
1315 | T_CALL { $$ = AIC_OP_CALL; }
1319 T_JZ { $$ = AIC_OP_JZ; }
1320 | T_JNZ { $$ = AIC_OP_JNZ; }
1324 T_JE { $$ = AIC_OP_JE; }
1325 | T_JNE { $$ = AIC_OP_JNE; }
1329 jmp_jc_jnc_call address ';'
1333 make_expression(&immed, 0);
1334 format_3_instr($1, &sindex, &immed, &$2);
1339 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1341 format_3_instr($5, &$2, &$4, &$6);
1346 T_TEST source ',' immediate_or_a jz_jnz address ';'
1348 format_3_instr($5, &$2, &$4, &$6);
1353 T_CMP source ',' immediate_or_a je_jne address ';'
1355 format_3_instr($5, &$2, &$4, &$6);
1360 T_MOV source jmp_jc_jnc_call address ';'
1364 make_expression(&immed, 0);
1365 format_3_instr($3, &$2, &immed, &$4);
1370 T_MVI immediate jmp_jc_jnc_call address ';'
1372 format_3_instr($3, &allzeros, &$2, &$4);
1379 process_field(unsigned int field_type, symbol_t *sym, int value)
1382 * Add the current register to its
1383 * symbol list, if it already exists,
1384 * warn if we are setting it to a
1385 * different value, or in the bit to
1386 * the "allowed bits" of this register.
1388 if (sym->type == UNINITIALIZED) {
1389 sym->type = field_type;
1390 initialize_symbol(sym);
1391 sym->info.finfo->value = value;
1392 if (field_type != ENUM_ENTRY) {
1393 if (field_type != MASK && value == 0) {
1394 stop("Empty Field, or Enum", EX_DATAERR);
1397 sym->info.finfo->value = value;
1398 sym->info.finfo->mask = value;
1399 } else if (field_symbol != NULL) {
1400 sym->info.finfo->mask = field_symbol->info.finfo->value;
1402 sym->info.finfo->mask = 0xFF;
1404 } else if (sym->type != field_type) {
1405 stop("Field definition mirrors a definition of the same "
1406 " name, but a different type", EX_DATAERR);
1408 } else if (value != sym->info.finfo->value) {
1409 stop("Field redefined with a conflicting value", EX_DATAERR);
1412 /* Fail if this symbol is already listed */
1413 if (symlist_search(&(sym->info.finfo->symrefs),
1414 cur_symbol->name) != NULL) {
1415 stop("Field defined multiple times for register", EX_DATAERR);
1418 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1419 SYMLIST_INSERT_HEAD);
1420 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1421 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1422 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1426 initialize_symbol(symbol_t *symbol)
1428 switch (symbol->type) {
1430 stop("Call to initialize_symbol with type field unset",
1437 symbol->info.rinfo =
1438 (struct reg_info *)malloc(sizeof(struct reg_info));
1439 if (symbol->info.rinfo == NULL) {
1440 stop("Can't create register info", EX_SOFTWARE);
1443 memset(symbol->info.rinfo, 0,
1444 sizeof(struct reg_info));
1445 SLIST_INIT(&(symbol->info.rinfo->fields));
1447 * Default to allowing access in all register modes
1448 * or to the mode specified by the SCB or SRAM space
1451 if (scb_or_sram_symbol != NULL)
1452 symbol->info.rinfo->modes =
1453 scb_or_sram_symbol->info.rinfo->modes;
1455 symbol->info.rinfo->modes = ~0;
1458 symbol->info.ainfo =
1459 (struct alias_info *)malloc(sizeof(struct alias_info));
1460 if (symbol->info.ainfo == NULL) {
1461 stop("Can't create alias info", EX_SOFTWARE);
1464 memset(symbol->info.ainfo, 0,
1465 sizeof(struct alias_info));
1471 symbol->info.finfo =
1472 (struct field_info *)malloc(sizeof(struct field_info));
1473 if (symbol->info.finfo == NULL) {
1474 stop("Can't create field info", EX_SOFTWARE);
1477 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1478 SLIST_INIT(&(symbol->info.finfo->symrefs));
1481 case DOWNLOAD_CONST:
1482 symbol->info.cinfo =
1483 (struct const_info *)malloc(sizeof(struct const_info));
1484 if (symbol->info.cinfo == NULL) {
1485 stop("Can't create alias info", EX_SOFTWARE);
1488 memset(symbol->info.cinfo, 0,
1489 sizeof(struct const_info));
1492 symbol->info.linfo =
1493 (struct label_info *)malloc(sizeof(struct label_info));
1494 if (symbol->info.linfo == NULL) {
1495 stop("Can't create label info", EX_SOFTWARE);
1498 memset(symbol->info.linfo, 0,
1499 sizeof(struct label_info));
1502 symbol->info.condinfo =
1503 (struct cond_info *)malloc(sizeof(struct cond_info));
1504 if (symbol->info.condinfo == NULL) {
1505 stop("Can't create conditional info", EX_SOFTWARE);
1508 memset(symbol->info.condinfo, 0,
1509 sizeof(struct cond_info));
1512 symbol->info.macroinfo =
1513 (struct macro_info *)malloc(sizeof(struct macro_info));
1514 if (symbol->info.macroinfo == NULL) {
1515 stop("Can't create macro info", EX_SOFTWARE);
1518 memset(symbol->info.macroinfo, 0,
1519 sizeof(struct macro_info));
1520 STAILQ_INIT(&symbol->info.macroinfo->args);
1523 stop("Call to initialize_symbol with invalid symbol type",
1531 add_macro_arg(const char *argtext, int argnum __unused)
1533 struct macro_arg *marg;
1537 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1538 stop("Invalid current symbol for adding macro arg",
1543 marg = (struct macro_arg *)malloc(sizeof(*marg));
1545 stop("Can't create macro_arg structure", EX_SOFTWARE);
1548 marg->replacement_text = NULL;
1549 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1550 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1552 if (retval >= (int)sizeof(regex_pattern)) {
1553 stop("Regex text buffer too small for arg",
1557 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1559 stop("Regex compilation failed", EX_SOFTWARE);
1562 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1566 add_macro_body(const char *bodytext)
1568 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1569 stop("Invalid current symbol for adding macro arg",
1573 cur_symbol->info.macroinfo->body = strdup(bodytext);
1574 if (cur_symbol->info.macroinfo->body == NULL) {
1575 stop("Can't duplicate macro body text", EX_SOFTWARE);
1581 process_register(symbol_t **p_symbol)
1583 symbol_t *symbol = *p_symbol;
1585 if (symbol->type == UNINITIALIZED) {
1586 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1588 stop(errbuf, EX_DATAERR);
1590 } else if (symbol->type == ALIAS) {
1591 *p_symbol = symbol->info.ainfo->parent;
1592 } else if ((symbol->type != REGISTER)
1593 && (symbol->type != SCBLOC)
1594 && (symbol->type != SRAMLOC)) {
1595 snprintf(errbuf, sizeof(errbuf),
1596 "Specified symbol %s is not a register",
1598 stop(errbuf, EX_DATAERR);
1603 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1604 symbol_ref_t *src, int ret)
1606 struct instruction *instr;
1607 struct ins_format1 *f1_instr;
1609 if (src->symbol == NULL)
1612 /* Test register permissions */
1613 test_writable_symbol(dest->symbol);
1614 test_readable_symbol(src->symbol);
1616 /* Ensure that immediate makes sense for this destination */
1617 type_check(dest->symbol, immed, opcode);
1619 /* Allocate sequencer space for the instruction and fill it out */
1620 instr = seq_alloc();
1621 f1_instr = &instr->format.format1;
1622 f1_instr->ret = ret ? 1 : 0;
1623 f1_instr->opcode = opcode;
1624 f1_instr->destination = dest->symbol->info.rinfo->address
1626 f1_instr->source = src->symbol->info.rinfo->address
1628 f1_instr->immediate = immed->value;
1630 if (is_download_const(immed))
1631 f1_instr->parity = 1;
1632 else if (dest->symbol == mode_ptr.symbol) {
1637 * Attempt to update mode information if
1638 * we are operating on the mode register.
1640 if (src->symbol == allones.symbol)
1642 else if (src->symbol == allzeros.symbol)
1644 else if (src->symbol == mode_ptr.symbol)
1645 src_value = (dst_mode << 4) | src_mode;
1651 dst_value = src_value & immed->value;
1654 dst_value = src_value ^ immed->value;
1657 dst_value = (src_value + immed->value) & 0xFF;
1660 dst_value = src_value | immed->value;
1663 dst_value = src_value;
1668 src_mode = dst_value & 0xF;
1669 dst_mode = (dst_value >> 4) & 0xF;
1673 symlist_free(&immed->referenced_syms);
1678 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1679 symbol_ref_t *src, int ret)
1681 struct instruction *instr;
1682 struct ins_format2 *f2_instr;
1683 uint8_t shift_control;
1685 if (src->symbol == NULL)
1688 /* Test register permissions */
1689 test_writable_symbol(dest->symbol);
1690 test_readable_symbol(src->symbol);
1692 /* Allocate sequencer space for the instruction and fill it out */
1693 instr = seq_alloc();
1694 f2_instr = &instr->format.format2;
1695 f2_instr->ret = ret ? 1 : 0;
1696 f2_instr->opcode = AIC_OP_ROL;
1697 f2_instr->destination = dest->symbol->info.rinfo->address
1699 f2_instr->source = src->symbol->info.rinfo->address
1701 if (places->value > 8 || places->value <= 0) {
1702 stop("illegal shift value", EX_DATAERR);
1707 if (places->value == 8)
1708 shift_control = 0xf0;
1710 shift_control = (places->value << 4) | places->value;
1713 if (places->value == 8) {
1714 shift_control = 0xf8;
1716 shift_control = (places->value << 4)
1717 | (8 - places->value)
1722 shift_control = places->value & 0x7;
1725 shift_control = (8 - places->value) | 0x08;
1728 shift_control = 0; /* Quiet Compiler */
1729 stop("Invalid shift operation specified", EX_SOFTWARE);
1733 f2_instr->shift_control = shift_control;
1734 symlist_free(&places->referenced_syms);
1739 format_3_instr(int opcode, symbol_ref_t *src,
1740 expression_t *immed, symbol_ref_t *address)
1742 struct instruction *instr;
1743 struct ins_format3 *f3_instr;
1746 /* Test register permissions */
1747 test_readable_symbol(src->symbol);
1749 /* Ensure that immediate makes sense for this source */
1750 type_check(src->symbol, immed, opcode);
1752 /* Allocate sequencer space for the instruction and fill it out */
1753 instr = seq_alloc();
1754 f3_instr = &instr->format.format3;
1755 if (address->symbol == NULL) {
1756 /* 'dot' referrence. Use the current instruction pointer */
1757 addr = instruction_ptr + address->offset;
1758 } else if (address->symbol->type == UNINITIALIZED) {
1759 /* forward reference */
1760 addr = address->offset;
1761 instr->patch_label = address->symbol;
1763 addr = address->symbol->info.linfo->address + address->offset;
1764 f3_instr->opcode = opcode;
1765 f3_instr->address = addr;
1766 f3_instr->source = src->symbol->info.rinfo->address
1768 f3_instr->immediate = immed->value;
1770 if (is_download_const(immed))
1771 f3_instr->parity = 1;
1773 symlist_free(&immed->referenced_syms);
1778 test_readable_symbol(symbol_t *symbol)
1781 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1782 snprintf(errbuf, sizeof(errbuf),
1783 "Register %s unavailable in source reg mode %d",
1784 symbol->name, src_mode);
1785 stop(errbuf, EX_DATAERR);
1788 if (symbol->info.rinfo->mode == WO) {
1789 stop("Write Only register specified as source",
1796 test_writable_symbol(symbol_t *symbol)
1799 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1800 snprintf(errbuf, sizeof(errbuf),
1801 "Register %s unavailable in destination reg mode %d",
1802 symbol->name, dst_mode);
1803 stop(errbuf, EX_DATAERR);
1806 if (symbol->info.rinfo->mode == RO) {
1807 stop("Read Only register specified as destination",
1814 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1816 symbol_node_t *node;
1818 uint8_t invalid_bits;
1821 if (opcode == AIC_OP_AND
1822 || opcode == AIC_OP_BMOV
1823 || opcode == AIC_OP_JE
1824 || opcode == AIC_OP_JNE
1825 || opcode == AIC_OP_JNZ
1826 || opcode == AIC_OP_JZ)
1830 * Make sure that we aren't attempting to write something
1831 * that hasn't been defined. If this is an and operation,
1832 * this is a mask, so "undefined" bits are okay.
1834 invalid_bits = expression->value & ~symbol->info.rinfo->valid_bitmask;
1835 if (and_op == FALSE && invalid_bits != 0) {
1836 snprintf(errbuf, sizeof(errbuf),
1837 "Invalid bit(s) 0x%x in immediate written to %s",
1838 invalid_bits, symbol->name);
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 verprefix[] = " * ";
1919 newlen = strlen(verstring) + strlen(verprefix);
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], verprefix);
1927 strcpy(&versions[oldlen + strlen(verprefix)], 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))