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>
58 #include <sys/queue.h>
62 #include "aicasm_symbol.h"
63 #include "aicasm_insformat.h"
67 char stock_prefix[] = "aic_";
68 char *prefix = stock_prefix;
71 static char errbuf[255];
72 static char regex_pattern[255];
73 static symbol_t *cur_symbol;
74 static symbol_t *field_symbol;
75 static symbol_t *scb_or_sram_symbol;
76 static symtype cur_symtype;
77 static symbol_ref_t accumulator;
78 static symbol_ref_t mode_ptr;
79 static symbol_ref_t allones;
80 static symbol_ref_t allzeros;
81 static symbol_ref_t none;
82 static symbol_ref_t sindex;
83 static int instruction_ptr;
85 static int sram_or_scb_offset;
86 static int download_constant_count;
87 static int in_critical_section;
88 static u_int enum_increment;
89 static u_int enum_next_value;
91 static void process_field(int field_type, symbol_t *sym, int mask);
92 static void initialize_symbol(symbol_t *symbol);
93 static void add_macro_arg(const char *argtext, int position);
94 static void add_macro_body(const char *bodytext);
95 static void process_register(symbol_t **p_symbol);
96 static void format_1_instr(int opcode, symbol_ref_t *dest,
97 expression_t *immed, symbol_ref_t *src, int ret);
98 static void format_2_instr(int opcode, symbol_ref_t *dest,
99 expression_t *places, symbol_ref_t *src, int ret);
100 static void format_3_instr(int opcode, symbol_ref_t *src,
101 expression_t *immed, symbol_ref_t *address);
102 static void test_readable_symbol(symbol_t *symbol);
103 static void test_writable_symbol(symbol_t *symbol);
104 static void type_check(symbol_t *symbol, expression_t *expression, int and_op);
105 static void make_expression(expression_t *immed, int value);
106 static void add_conditional(symbol_t *symbol);
107 static void add_version(const char *verstring);
108 static int is_download_const(expression_t *immed);
110 #define SRAM_SYMNAME "SRAM_BASE"
111 #define SCB_SYMNAME "SCB_BASE"
118 symbol_ref_t sym_ref;
119 expression_t expression;
124 %token <value> T_CONST
142 %token <value> T_ADDRESS
150 %token T_SET_SRC_MODE
152 %token T_SET_DST_MODE
154 %token <value> T_MODE
166 %token <value> T_NUMBER
168 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
172 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
174 %token <value> T_SHR T_SHL T_ROR T_ROL
176 %token <value> T_MVI T_MOV T_CLR T_BMOV
178 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
180 %token <value> T_ADD T_ADC
182 %token <value> T_INC T_DEC
184 %token <value> T_STC T_CLC
186 %token <value> T_CMP T_NOT T_XOR
188 %token <value> T_TEST T_AND
196 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
200 %token <sym> T_SYMBOL
204 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
206 %type <sym_ref> reg_symbol address destination source opt_source
208 %type <expression> expression immediate immediate_or_a
210 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
212 %type <value> mode_value mode_list macro_arglist
216 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
229 | program patch_arg_list
239 | program scratch_ram
245 | program set_src_mode
247 | program set_dst_mode
248 | critical_section_start
249 | program critical_section_start
250 | critical_section_end
251 | program critical_section_end
253 | program conditional
259 T_INCLUDE '<' T_PATH '>'
261 include_file($3, BRACKETED_INCLUDE);
263 | T_INCLUDE '"' T_PATH '"'
265 include_file($3, QUOTED_INCLUDE);
270 T_PREFIX '=' T_STRING
272 if (prefix != stock_prefix)
273 stop("Prefix multiply defined",
277 stop("Unable to record prefix", EX_SOFTWARE);
282 T_PATCH_ARG_LIST '=' T_STRING
284 if (patch_arg_list != NULL)
285 stop("Patch argument list multiply defined",
287 patch_arg_list = strdup($3);
288 if (patch_arg_list == NULL)
289 stop("Unable to record patch arg list", EX_SOFTWARE);
294 T_VERSION '=' T_STRING
299 T_REGISTER { cur_symtype = REGISTER; } reg_definition
305 if ($1->type != UNINITIALIZED) {
306 stop("Register multiply defined", EX_DATAERR);
310 cur_symbol->type = cur_symtype;
311 initialize_symbol(cur_symbol);
317 * Default to allowing everything in for registers
318 * with no bit or mask definitions.
320 if (cur_symbol->info.rinfo->valid_bitmask == 0)
321 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
323 if (cur_symbol->info.rinfo->size == 0)
324 cur_symbol->info.rinfo->size = 1;
327 * This might be useful for registers too.
329 if (cur_symbol->type != REGISTER) {
330 if (cur_symbol->info.rinfo->address == 0)
331 cur_symbol->info.rinfo->address =
333 sram_or_scb_offset +=
334 cur_symbol->info.rinfo->size;
342 | reg_attribute_list reg_attribute
365 cur_symbol->info.rinfo->address = $2;
372 cur_symbol->info.rinfo->size = $2;
373 if (scb_or_sram_symbol != NULL) {
377 max_addr = scb_or_sram_symbol->info.rinfo->address
378 + scb_or_sram_symbol->info.rinfo->size;
379 sym_max_addr = cur_symbol->info.rinfo->address
380 + cur_symbol->info.rinfo->size;
382 if (sym_max_addr > max_addr)
383 stop("SCB or SRAM space exhausted", EX_DATAERR);
391 cur_symbol->info.rinfo->mode = $2;
398 cur_symbol->info.rinfo->modes = $2;
407 | mode_list ',' mode_value
417 stop("Valid register modes range between 0 and 4.",
429 if (symbol->type != CONST) {
430 stop("Only \"const\" symbols allowed in "
431 "mode definitions.", EX_DATAERR);
434 if (symbol->info.cinfo->value > 4) {
435 stop("Valid register modes range between 0 and 4.",
439 $$ = (0x1 << symbol->info.cinfo->value);
450 '{' enum_entry_list '}'
451 | T_FIELD T_SYMBOL expression
453 process_field(FIELD, $2, $3.value);
456 enum_increment = 0x01 << (ffs($3.value) - 1);
458 '{' enum_entry_list '}'
459 | T_FIELD T_SYMBOL expression
461 process_field(FIELD, $2, $3.value);
472 '{' enum_entry_list '}'
473 | T_ENUM T_SYMBOL expression
475 process_field(ENUM, $2, $3.value);
478 enum_increment = 0x01 << (ffs($3.value) - 1);
480 '{' enum_entry_list '}'
485 | enum_entry_list ',' enum_entry
491 process_field(ENUM_ENTRY, $1, enum_next_value);
492 enum_next_value += enum_increment;
494 | T_SYMBOL expression
496 process_field(ENUM_ENTRY, $1, $2.value);
497 enum_next_value = $2.value + enum_increment;
502 T_MASK T_SYMBOL expression
504 process_field(MASK, $2, $3.value);
511 if ($2->type != UNINITIALIZED) {
512 stop("Re-definition of register alias",
517 initialize_symbol($2);
518 $2->info.ainfo->parent = cur_symbol;
525 if (accumulator.symbol != NULL) {
526 stop("Only one accumulator definition allowed",
530 accumulator.symbol = cur_symbol;
537 if (mode_ptr.symbol != NULL) {
538 stop("Only one mode pointer definition allowed",
542 mode_ptr.symbol = cur_symbol;
549 if (allones.symbol != NULL) {
550 stop("Only one definition of allones allowed",
554 allones.symbol = cur_symbol;
561 if (allzeros.symbol != NULL) {
562 stop("Only one definition of allzeros allowed",
566 allzeros.symbol = cur_symbol;
573 if (none.symbol != NULL) {
574 stop("Only one definition of none allowed",
578 none.symbol = cur_symbol;
585 if (sindex.symbol != NULL) {
586 stop("Only one definition of sindex allowed",
590 sindex.symbol = cur_symbol;
595 expression '|' expression
597 $$.value = $1.value | $3.value;
598 symlist_merge(&$$.referenced_syms,
600 &$3.referenced_syms);
602 | expression '&' expression
604 $$.value = $1.value & $3.value;
605 symlist_merge(&$$.referenced_syms,
607 &$3.referenced_syms);
609 | expression '+' expression
611 $$.value = $1.value + $3.value;
612 symlist_merge(&$$.referenced_syms,
614 &$3.referenced_syms);
616 | expression '-' expression
618 $$.value = $1.value - $3.value;
619 symlist_merge(&($$.referenced_syms),
620 &($1.referenced_syms),
621 &($3.referenced_syms));
623 | expression '*' expression
625 $$.value = $1.value * $3.value;
626 symlist_merge(&($$.referenced_syms),
627 &($1.referenced_syms),
628 &($3.referenced_syms));
630 | expression '/' expression
632 $$.value = $1.value / $3.value;
633 symlist_merge(&($$.referenced_syms),
634 &($1.referenced_syms),
635 &($3.referenced_syms));
637 | expression T_EXPR_LSHIFT expression
639 $$.value = $1.value << $3.value;
640 symlist_merge(&$$.referenced_syms,
642 &$3.referenced_syms);
644 | expression T_EXPR_RSHIFT expression
646 $$.value = $1.value >> $3.value;
647 symlist_merge(&$$.referenced_syms,
649 &$3.referenced_syms);
658 $$.value = (~$$.value) & 0xFF;
660 | '-' expression %prec UMINUS
663 $$.value = -$$.value;
668 SLIST_INIT(&$$.referenced_syms);
675 switch (symbol->type) {
677 symbol = $1->info.ainfo->parent;
681 $$.value = symbol->info.rinfo->address;
687 $$.value = symbol->info.finfo->value;
691 $$.value = symbol->info.cinfo->value;
696 snprintf(errbuf, sizeof(errbuf),
697 "Undefined symbol %s referenced",
699 stop(errbuf, EX_DATAERR);
704 SLIST_INIT(&$$.referenced_syms);
705 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
710 T_CONST T_SYMBOL expression
712 if ($2->type != UNINITIALIZED) {
713 stop("Re-definition of symbol as a constant",
718 initialize_symbol($2);
719 $2->info.cinfo->value = $3.value;
721 | T_CONST T_SYMBOL T_DOWNLOAD
724 stop("Invalid downloaded constant declaration",
728 if ($2->type != UNINITIALIZED) {
729 stop("Re-definition of symbol as a downloaded constant",
733 $2->type = DOWNLOAD_CONST;
734 initialize_symbol($2);
735 $2->info.cinfo->value = download_constant_count++;
742 if ($2->type != UNINITIALIZED) {
743 stop("Re-definition of symbol as a macro",
748 cur_symbol->type = MACRO;
749 initialize_symbol(cur_symbol);
754 macrodefn_prologue T_MACROBODY
758 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
761 cur_symbol->info.macroinfo->narg = $3;
767 /* Macros can take no arguments */
773 add_macro_arg($1, 0);
775 | macro_arglist ',' T_ARG
778 stop("Comma without preceeding argument in arg list",
783 add_macro_arg($3, $1);
790 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
792 cur_symbol = symtable_get(SRAM_SYMNAME);
793 cur_symtype = SRAMLOC;
794 cur_symbol->type = SRAMLOC;
795 initialize_symbol(cur_symbol);
799 sram_or_scb_offset = cur_symbol->info.rinfo->address;
803 scb_or_sram_symbol = cur_symbol;
805 scb_or_sram_attributes
809 scb_or_sram_symbol = NULL;
816 cur_symbol = symtable_get(SCB_SYMNAME);
817 cur_symtype = SCBLOC;
818 if (cur_symbol->type != UNINITIALIZED) {
819 stop("Only one SRAM definition allowed",
823 cur_symbol->type = SCBLOC;
824 initialize_symbol(cur_symbol);
825 /* 64 bytes of SCB space */
826 cur_symbol->info.rinfo->size = 64;
830 sram_or_scb_offset = cur_symbol->info.rinfo->address;
834 scb_or_sram_symbol = cur_symbol;
836 scb_or_sram_attributes
840 scb_or_sram_symbol = NULL;
844 scb_or_sram_attributes:
845 /* NULL definition is okay */
847 | scb_or_sram_reg_list
848 | modes scb_or_sram_reg_list
851 scb_or_sram_reg_list:
853 | scb_or_sram_reg_list reg_definition
859 process_register(&$1);
863 | T_SYMBOL '[' T_SYMBOL ']'
865 process_register(&$1);
866 if ($3->type != CONST) {
867 stop("register offset must be a constant", EX_DATAERR);
870 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
871 stop("Accessing offset beyond range of register",
876 $$.offset = $3->info.cinfo->value;
878 | T_SYMBOL '[' T_NUMBER ']'
880 process_register(&$1);
881 if (($3 + 1) > $1->info.rinfo->size) {
882 stop("Accessing offset beyond range of register",
891 if (accumulator.symbol == NULL) {
892 stop("No accumulator has been defined", EX_DATAERR);
895 $$.symbol = accumulator.symbol;
903 test_writable_symbol($1.symbol);
916 if ($1.value == 0 && is_download_const(&$1) == 0) {
917 snprintf(errbuf, sizeof(errbuf),
918 "\nExpression evaluates to 0 and thus "
919 "references the accumulator.\n "
920 "If this is the desired effect, use 'A' "
922 stop(errbuf, EX_DATAERR);
928 SLIST_INIT(&$$.referenced_syms);
929 symlist_add(&$$.referenced_syms, accumulator.symbol,
930 SYMLIST_INSERT_HEAD);
938 test_readable_symbol($1.symbol);
959 T_SET_SRC_MODE T_NUMBER ';'
966 T_SET_DST_MODE T_NUMBER ';'
972 critical_section_start:
975 critical_section_t *cs;
977 if (in_critical_section != FALSE) {
978 stop("Critical Section within Critical Section",
983 cs->begin_addr = instruction_ptr;
984 in_critical_section = TRUE;
988 critical_section_end:
991 critical_section_t *cs;
993 if (in_critical_section == FALSE) {
994 stop("Unballanced 'end_cs'", EX_DATAERR);
997 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
998 cs->end_addr = instruction_ptr;
999 in_critical_section = FALSE;
1012 if ($2->type != UNINITIALIZED) {
1013 stop("Program label multiply defined", EX_DATAERR);
1017 initialize_symbol($2);
1018 $2->info.linfo->address = instruction_ptr;
1019 $2->info.linfo->exported = $1;
1029 | T_SYMBOL '+' T_NUMBER
1034 | T_SYMBOL '-' T_NUMBER
1061 add_conditional($2);
1062 new_scope = scope_alloc();
1063 new_scope->type = SCOPE_IF;
1064 new_scope->begin_addr = instruction_ptr;
1065 new_scope->func_num = $2->info.condinfo->func_num;
1067 | T_ELSE T_IF T_CEXPR '{'
1070 scope_t *scope_context;
1071 scope_t *last_scope;
1074 * Ensure that the previous scope is either an
1075 * if or and else if.
1077 scope_context = SLIST_FIRST(&scope_stack);
1078 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1080 if (last_scope == NULL
1081 || last_scope->type == T_ELSE) {
1083 stop("'else if' without leading 'if'", EX_DATAERR);
1086 add_conditional($3);
1087 new_scope = scope_alloc();
1088 new_scope->type = SCOPE_ELSE_IF;
1089 new_scope->begin_addr = instruction_ptr;
1090 new_scope->func_num = $3->info.condinfo->func_num;
1095 scope_t *scope_context;
1096 scope_t *last_scope;
1099 * Ensure that the previous scope is either an
1100 * if or and else if.
1102 scope_context = SLIST_FIRST(&scope_stack);
1103 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1105 if (last_scope == NULL
1106 || last_scope->type == SCOPE_ELSE) {
1108 stop("'else' without leading 'if'", EX_DATAERR);
1111 new_scope = scope_alloc();
1112 new_scope->type = SCOPE_ELSE;
1113 new_scope->begin_addr = instruction_ptr;
1120 scope_t *scope_context;
1122 scope_context = SLIST_FIRST(&scope_stack);
1123 if (scope_context->type == SCOPE_ROOT) {
1124 stop("Unexpected '}' encountered", EX_DATAERR);
1128 scope_context->end_addr = instruction_ptr;
1131 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1133 process_scope(scope_context);
1135 if (SLIST_FIRST(&scope_stack) == NULL) {
1136 stop("Unexpected '}' encountered", EX_DATAERR);
1143 T_AND { $$ = AIC_OP_AND; }
1144 | T_XOR { $$ = AIC_OP_XOR; }
1145 | T_ADD { $$ = AIC_OP_ADD; }
1146 | T_ADC { $$ = AIC_OP_ADC; }
1150 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1152 format_1_instr($1, &$2, &$4, &$5, $6);
1157 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1159 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1164 T_INC destination opt_source ret ';'
1168 make_expression(&immed, 1);
1169 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1174 T_DEC destination opt_source ret ';'
1178 make_expression(&immed, -1);
1179 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1188 make_expression(&immed, -1);
1189 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1191 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1193 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1202 make_expression(&immed, 1);
1203 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1205 | T_STC destination ret ';'
1209 make_expression(&immed, 1);
1210 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1215 T_BMOV destination ',' source ',' immediate ret ';'
1217 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1222 T_MOV destination ',' source ret ';'
1226 make_expression(&immed, 1);
1227 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1232 T_MVI destination ',' immediate ret ';'
1235 && is_download_const(&$4) == 0) {
1239 * Allow move immediates of 0 so that macros,
1240 * that can't know the immediate's value and
1241 * otherwise compensate, still work.
1243 make_expression(&immed, 1);
1244 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1246 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1252 T_NOT destination opt_source ret ';'
1256 make_expression(&immed, 0xff);
1257 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1262 T_CLR destination ret ';'
1266 make_expression(&immed, 0xff);
1267 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1276 make_expression(&immed, 0xff);
1277 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1286 make_expression(&immed, 0xff);
1287 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1292 * This grammer differs from the one in the aic7xxx
1293 * reference manual since the grammer listed there is
1294 * ambiguous and causes a shift/reduce conflict.
1295 * It also seems more logical as the "immediate"
1296 * argument is listed as the second arg like the
1301 T_SHL { $$ = AIC_OP_SHL; }
1302 | T_SHR { $$ = AIC_OP_SHR; }
1303 | T_ROL { $$ = AIC_OP_ROL; }
1304 | T_ROR { $$ = AIC_OP_ROR; }
1308 f2_opcode destination ',' expression opt_source ret ';'
1310 format_2_instr($1, &$2, &$4, &$5, $6);
1315 T_JMP { $$ = AIC_OP_JMP; }
1316 | T_JC { $$ = AIC_OP_JC; }
1317 | T_JNC { $$ = AIC_OP_JNC; }
1318 | T_CALL { $$ = AIC_OP_CALL; }
1322 T_JZ { $$ = AIC_OP_JZ; }
1323 | T_JNZ { $$ = AIC_OP_JNZ; }
1327 T_JE { $$ = AIC_OP_JE; }
1328 | T_JNE { $$ = AIC_OP_JNE; }
1332 jmp_jc_jnc_call address ';'
1336 make_expression(&immed, 0);
1337 format_3_instr($1, &sindex, &immed, &$2);
1342 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1344 format_3_instr($5, &$2, &$4, &$6);
1349 T_TEST source ',' immediate_or_a jz_jnz address ';'
1351 format_3_instr($5, &$2, &$4, &$6);
1356 T_CMP source ',' immediate_or_a je_jne address ';'
1358 format_3_instr($5, &$2, &$4, &$6);
1363 T_MOV source jmp_jc_jnc_call address ';'
1367 make_expression(&immed, 0);
1368 format_3_instr($3, &$2, &immed, &$4);
1373 T_MVI immediate jmp_jc_jnc_call address ';'
1375 format_3_instr($3, &allzeros, &$2, &$4);
1382 process_field(int field_type, symbol_t *sym, int value)
1385 * Add the current register to its
1386 * symbol list, if it already exists,
1387 * warn if we are setting it to a
1388 * different value, or in the bit to
1389 * the "allowed bits" of this register.
1391 if (sym->type == UNINITIALIZED) {
1392 sym->type = field_type;
1393 initialize_symbol(sym);
1394 sym->info.finfo->value = value;
1395 if (field_type != ENUM_ENTRY) {
1396 if (field_type != MASK && value == 0) {
1397 stop("Empty Field, or Enum", EX_DATAERR);
1400 sym->info.finfo->value = value;
1401 sym->info.finfo->mask = value;
1402 } else if (field_symbol != NULL) {
1403 sym->info.finfo->mask = field_symbol->info.finfo->value;
1405 sym->info.finfo->mask = 0xFF;
1407 } else if (sym->type != field_type) {
1408 stop("Field definition mirrors a definition of the same "
1409 " name, but a different type", EX_DATAERR);
1411 } else if (value != sym->info.finfo->value) {
1412 stop("Field redefined with a conflicting value", EX_DATAERR);
1415 /* Fail if this symbol is already listed */
1416 if (symlist_search(&(sym->info.finfo->symrefs),
1417 cur_symbol->name) != NULL) {
1418 stop("Field defined multiple times for register", EX_DATAERR);
1421 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1422 SYMLIST_INSERT_HEAD);
1423 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1424 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1425 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1429 initialize_symbol(symbol_t *symbol)
1431 switch (symbol->type) {
1433 stop("Call to initialize_symbol with type field unset",
1440 symbol->info.rinfo =
1441 (struct reg_info *)malloc(sizeof(struct reg_info));
1442 if (symbol->info.rinfo == NULL) {
1443 stop("Can't create register info", EX_SOFTWARE);
1446 memset(symbol->info.rinfo, 0,
1447 sizeof(struct reg_info));
1448 SLIST_INIT(&(symbol->info.rinfo->fields));
1450 * Default to allowing access in all register modes
1451 * or to the mode specified by the SCB or SRAM space
1454 if (scb_or_sram_symbol != NULL)
1455 symbol->info.rinfo->modes =
1456 scb_or_sram_symbol->info.rinfo->modes;
1458 symbol->info.rinfo->modes = ~0;
1461 symbol->info.ainfo =
1462 (struct alias_info *)malloc(sizeof(struct alias_info));
1463 if (symbol->info.ainfo == NULL) {
1464 stop("Can't create alias info", EX_SOFTWARE);
1467 memset(symbol->info.ainfo, 0,
1468 sizeof(struct alias_info));
1474 symbol->info.finfo =
1475 (struct field_info *)malloc(sizeof(struct field_info));
1476 if (symbol->info.finfo == NULL) {
1477 stop("Can't create field info", EX_SOFTWARE);
1480 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1481 SLIST_INIT(&(symbol->info.finfo->symrefs));
1484 case DOWNLOAD_CONST:
1485 symbol->info.cinfo =
1486 (struct const_info *)malloc(sizeof(struct const_info));
1487 if (symbol->info.cinfo == NULL) {
1488 stop("Can't create alias info", EX_SOFTWARE);
1491 memset(symbol->info.cinfo, 0,
1492 sizeof(struct const_info));
1495 symbol->info.linfo =
1496 (struct label_info *)malloc(sizeof(struct label_info));
1497 if (symbol->info.linfo == NULL) {
1498 stop("Can't create label info", EX_SOFTWARE);
1501 memset(symbol->info.linfo, 0,
1502 sizeof(struct label_info));
1505 symbol->info.condinfo =
1506 (struct cond_info *)malloc(sizeof(struct cond_info));
1507 if (symbol->info.condinfo == NULL) {
1508 stop("Can't create conditional info", EX_SOFTWARE);
1511 memset(symbol->info.condinfo, 0,
1512 sizeof(struct cond_info));
1515 symbol->info.macroinfo =
1516 (struct macro_info *)malloc(sizeof(struct macro_info));
1517 if (symbol->info.macroinfo == NULL) {
1518 stop("Can't create macro info", EX_SOFTWARE);
1521 memset(symbol->info.macroinfo, 0,
1522 sizeof(struct macro_info));
1523 STAILQ_INIT(&symbol->info.macroinfo->args);
1526 stop("Call to initialize_symbol with invalid symbol type",
1534 add_macro_arg(const char *argtext, int argnum)
1536 struct macro_arg *marg;
1541 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1542 stop("Invalid current symbol for adding macro arg",
1547 marg = (struct macro_arg *)malloc(sizeof(*marg));
1549 stop("Can't create macro_arg structure", EX_SOFTWARE);
1552 marg->replacement_text = NULL;
1553 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1554 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1556 if (retval >= sizeof(regex_pattern)) {
1557 stop("Regex text buffer too small for arg",
1561 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1563 stop("Regex compilation failed", EX_SOFTWARE);
1566 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1570 add_macro_body(const char *bodytext)
1572 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1573 stop("Invalid current symbol for adding macro arg",
1577 cur_symbol->info.macroinfo->body = strdup(bodytext);
1578 if (cur_symbol->info.macroinfo->body == NULL) {
1579 stop("Can't duplicate macro body text", EX_SOFTWARE);
1585 process_register(symbol_t **p_symbol)
1587 symbol_t *symbol = *p_symbol;
1589 if (symbol->type == UNINITIALIZED) {
1590 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1592 stop(errbuf, EX_DATAERR);
1594 } else if (symbol->type == ALIAS) {
1595 *p_symbol = symbol->info.ainfo->parent;
1596 } else if ((symbol->type != REGISTER)
1597 && (symbol->type != SCBLOC)
1598 && (symbol->type != SRAMLOC)) {
1599 snprintf(errbuf, sizeof(errbuf),
1600 "Specified symbol %s is not a register",
1602 stop(errbuf, EX_DATAERR);
1607 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1608 symbol_ref_t *src, int ret)
1610 struct instruction *instr;
1611 struct ins_format1 *f1_instr;
1613 if (src->symbol == NULL)
1616 /* Test register permissions */
1617 test_writable_symbol(dest->symbol);
1618 test_readable_symbol(src->symbol);
1620 /* Ensure that immediate makes sense for this destination */
1621 type_check(dest->symbol, immed, opcode);
1623 /* Allocate sequencer space for the instruction and fill it out */
1624 instr = seq_alloc();
1625 f1_instr = &instr->format.format1;
1626 f1_instr->ret = ret ? 1 : 0;
1627 f1_instr->opcode = opcode;
1628 f1_instr->destination = dest->symbol->info.rinfo->address
1630 f1_instr->source = src->symbol->info.rinfo->address
1632 f1_instr->immediate = immed->value;
1634 if (is_download_const(immed))
1635 f1_instr->parity = 1;
1636 else if (dest->symbol == mode_ptr.symbol) {
1641 * Attempt to update mode information if
1642 * we are operating on the mode register.
1644 if (src->symbol == allones.symbol)
1646 else if (src->symbol == allzeros.symbol)
1648 else if (src->symbol == mode_ptr.symbol)
1649 src_value = (dst_mode << 4) | src_mode;
1655 dst_value = src_value & immed->value;
1658 dst_value = src_value ^ immed->value;
1661 dst_value = (src_value + immed->value) & 0xFF;
1664 dst_value = src_value | immed->value;
1667 dst_value = src_value;
1672 src_mode = dst_value & 0xF;
1673 dst_mode = (dst_value >> 4) & 0xF;
1677 symlist_free(&immed->referenced_syms);
1682 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1683 symbol_ref_t *src, int ret)
1685 struct instruction *instr;
1686 struct ins_format2 *f2_instr;
1687 uint8_t shift_control;
1689 if (src->symbol == NULL)
1692 /* Test register permissions */
1693 test_writable_symbol(dest->symbol);
1694 test_readable_symbol(src->symbol);
1696 /* Allocate sequencer space for the instruction and fill it out */
1697 instr = seq_alloc();
1698 f2_instr = &instr->format.format2;
1699 f2_instr->ret = ret ? 1 : 0;
1700 f2_instr->opcode = AIC_OP_ROL;
1701 f2_instr->destination = dest->symbol->info.rinfo->address
1703 f2_instr->source = src->symbol->info.rinfo->address
1705 if (places->value > 8 || places->value <= 0) {
1706 stop("illegal shift value", EX_DATAERR);
1711 if (places->value == 8)
1712 shift_control = 0xf0;
1714 shift_control = (places->value << 4) | places->value;
1717 if (places->value == 8) {
1718 shift_control = 0xf8;
1720 shift_control = (places->value << 4)
1721 | (8 - places->value)
1726 shift_control = places->value & 0x7;
1729 shift_control = (8 - places->value) | 0x08;
1732 shift_control = 0; /* Quiet Compiler */
1733 stop("Invalid shift operation specified", EX_SOFTWARE);
1737 f2_instr->shift_control = shift_control;
1738 symlist_free(&places->referenced_syms);
1743 format_3_instr(int opcode, symbol_ref_t *src,
1744 expression_t *immed, symbol_ref_t *address)
1746 struct instruction *instr;
1747 struct ins_format3 *f3_instr;
1750 /* Test register permissions */
1751 test_readable_symbol(src->symbol);
1753 /* Ensure that immediate makes sense for this source */
1754 type_check(src->symbol, immed, opcode);
1756 /* Allocate sequencer space for the instruction and fill it out */
1757 instr = seq_alloc();
1758 f3_instr = &instr->format.format3;
1759 if (address->symbol == NULL) {
1760 /* 'dot' referrence. Use the current instruction pointer */
1761 addr = instruction_ptr + address->offset;
1762 } else if (address->symbol->type == UNINITIALIZED) {
1763 /* forward reference */
1764 addr = address->offset;
1765 instr->patch_label = address->symbol;
1767 addr = address->symbol->info.linfo->address + address->offset;
1768 f3_instr->opcode = opcode;
1769 f3_instr->address = addr;
1770 f3_instr->source = src->symbol->info.rinfo->address
1772 f3_instr->immediate = immed->value;
1774 if (is_download_const(immed))
1775 f3_instr->parity = 1;
1777 symlist_free(&immed->referenced_syms);
1782 test_readable_symbol(symbol_t *symbol)
1785 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1786 snprintf(errbuf, sizeof(errbuf),
1787 "Register %s unavailable in source reg mode %d",
1788 symbol->name, src_mode);
1789 stop(errbuf, EX_DATAERR);
1792 if (symbol->info.rinfo->mode == WO) {
1793 stop("Write Only register specified as source",
1800 test_writable_symbol(symbol_t *symbol)
1803 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1804 snprintf(errbuf, sizeof(errbuf),
1805 "Register %s unavailable in destination reg mode %d",
1806 symbol->name, dst_mode);
1807 stop(errbuf, EX_DATAERR);
1810 if (symbol->info.rinfo->mode == RO) {
1811 stop("Read Only register specified as destination",
1818 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1820 symbol_node_t *node;
1824 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || AIC_OP_JZ)
1828 * Make sure that we aren't attempting to write something
1829 * that hasn't been defined. If this is an and operation,
1830 * this is a mask, so "undefined" bits are okay.
1833 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
1834 snprintf(errbuf, sizeof(errbuf),
1835 "Invalid bit(s) 0x%x in immediate written to %s",
1836 expression->value & ~symbol->info.rinfo->valid_bitmask,
1838 stop(errbuf, EX_DATAERR);
1843 * Now make sure that all of the symbols referenced by the
1844 * expression are defined for this register.
1846 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1847 for(node = expression->referenced_syms.slh_first;
1849 node = node->links.sle_next) {
1850 if ((node->symbol->type == MASK
1851 || node->symbol->type == FIELD
1852 || node->symbol->type == ENUM
1853 || node->symbol->type == ENUM_ENTRY)
1854 && symlist_search(&node->symbol->info.finfo->symrefs,
1855 symbol->name) == NULL) {
1856 snprintf(errbuf, sizeof(errbuf),
1857 "Invalid field or mask %s "
1859 node->symbol->name, symbol->name);
1860 stop(errbuf, EX_DATAERR);
1868 make_expression(expression_t *immed, int value)
1870 SLIST_INIT(&immed->referenced_syms);
1871 immed->value = value & 0xff;
1875 add_conditional(symbol_t *symbol)
1877 static int numfuncs;
1879 if (numfuncs == 0) {
1880 /* add a special conditional, "0" */
1881 symbol_t *false_func;
1883 false_func = symtable_get("0");
1884 if (false_func->type != UNINITIALIZED) {
1885 stop("Conditional expression '0' "
1886 "conflicts with a symbol", EX_DATAERR);
1889 false_func->type = CONDITIONAL;
1890 initialize_symbol(false_func);
1891 false_func->info.condinfo->func_num = numfuncs++;
1892 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1895 /* This condition has occurred before */
1896 if (symbol->type == CONDITIONAL)
1899 if (symbol->type != UNINITIALIZED) {
1900 stop("Conditional expression conflicts with a symbol",
1905 symbol->type = CONDITIONAL;
1906 initialize_symbol(symbol);
1907 symbol->info.condinfo->func_num = numfuncs++;
1908 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1912 add_version(const char *verstring)
1914 const char prefix[] = " * ";
1918 newlen = strlen(verstring) + strlen(prefix);
1920 if (versions != NULL)
1921 oldlen = strlen(versions);
1922 versions = realloc(versions, newlen + oldlen + 2);
1923 if (versions == NULL)
1924 stop("Can't allocate version string", EX_SOFTWARE);
1925 strcpy(&versions[oldlen], prefix);
1926 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1927 versions[newlen + oldlen] = '\n';
1928 versions[newlen + oldlen + 1] = '\0';
1932 yyerror(const char *string)
1934 stop(string, EX_DATAERR);
1938 is_download_const(expression_t *immed)
1940 if ((immed->referenced_syms.slh_first != NULL)
1941 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))