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(unsigned 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 extern int yylex (void);
111 extern int yyparse (void);
113 #define SRAM_SYMNAME "SRAM_BASE"
114 #define SCB_SYMNAME "SCB_BASE"
121 symbol_ref_t sym_ref;
122 expression_t expression;
127 %token <value> T_CONST
145 %token <value> T_ADDRESS
153 %token T_SET_SRC_MODE
155 %token T_SET_DST_MODE
157 %token <value> T_MODE
169 %token <value> T_NUMBER
171 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
175 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
177 %token <value> T_SHR T_SHL T_ROR T_ROL
179 %token <value> T_MVI T_MOV T_CLR T_BMOV
181 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
183 %token <value> T_ADD T_ADC
185 %token <value> T_INC T_DEC
187 %token <value> T_STC T_CLC
189 %token <value> T_CMP T_NOT T_XOR
191 %token <value> T_TEST T_AND
199 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
203 %token <sym> T_SYMBOL
207 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
209 %type <sym_ref> reg_symbol address destination source opt_source
211 %type <expression> expression immediate immediate_or_a
213 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
215 %type <value> mode_value mode_list macro_arglist
219 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
232 | program patch_arg_list
242 | program scratch_ram
248 | program set_src_mode
250 | program set_dst_mode
251 | critical_section_start
252 | program critical_section_start
253 | critical_section_end
254 | program critical_section_end
256 | program conditional
262 T_INCLUDE '<' T_PATH '>'
264 include_file($3, BRACKETED_INCLUDE);
266 | T_INCLUDE '"' T_PATH '"'
268 include_file($3, QUOTED_INCLUDE);
273 T_PREFIX '=' T_STRING
275 if (prefix != stock_prefix)
276 stop("Prefix multiply defined",
280 stop("Unable to record prefix", EX_SOFTWARE);
285 T_PATCH_ARG_LIST '=' T_STRING
287 if (patch_arg_list != NULL)
288 stop("Patch argument list multiply defined",
290 patch_arg_list = strdup($3);
291 if (patch_arg_list == NULL)
292 stop("Unable to record patch arg list", EX_SOFTWARE);
297 T_VERSION '=' T_STRING
302 T_REGISTER { cur_symtype = REGISTER; } reg_definition
308 if ($1->type != UNINITIALIZED) {
309 stop("Register multiply defined", EX_DATAERR);
313 cur_symbol->type = cur_symtype;
314 initialize_symbol(cur_symbol);
320 * Default to allowing everything in for registers
321 * with no bit or mask definitions.
323 if (cur_symbol->info.rinfo->valid_bitmask == 0)
324 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
326 if (cur_symbol->info.rinfo->size == 0)
327 cur_symbol->info.rinfo->size = 1;
330 * This might be useful for registers too.
332 if (cur_symbol->type != REGISTER) {
333 if (cur_symbol->info.rinfo->address == 0)
334 cur_symbol->info.rinfo->address =
336 sram_or_scb_offset +=
337 cur_symbol->info.rinfo->size;
345 | reg_attribute_list reg_attribute
368 cur_symbol->info.rinfo->address = $2;
375 cur_symbol->info.rinfo->size = $2;
376 if (scb_or_sram_symbol != NULL) {
380 max_addr = scb_or_sram_symbol->info.rinfo->address
381 + scb_or_sram_symbol->info.rinfo->size;
382 sym_max_addr = cur_symbol->info.rinfo->address
383 + cur_symbol->info.rinfo->size;
385 if (sym_max_addr > max_addr)
386 stop("SCB or SRAM space exhausted", EX_DATAERR);
394 cur_symbol->info.rinfo->mode = $2;
401 cur_symbol->info.rinfo->modes = $2;
410 | mode_list ',' mode_value
420 stop("Valid register modes range between 0 and 4.",
432 if (symbol->type != CONST) {
433 stop("Only \"const\" symbols allowed in "
434 "mode definitions.", EX_DATAERR);
437 if (symbol->info.cinfo->value > 4) {
438 stop("Valid register modes range between 0 and 4.",
442 $$ = (0x1 << symbol->info.cinfo->value);
453 '{' enum_entry_list '}'
454 | T_FIELD T_SYMBOL expression
456 process_field(FIELD, $2, $3.value);
459 enum_increment = 0x01 << (ffs($3.value) - 1);
461 '{' enum_entry_list '}'
462 | T_FIELD T_SYMBOL expression
464 process_field(FIELD, $2, $3.value);
475 '{' enum_entry_list '}'
476 | T_ENUM T_SYMBOL expression
478 process_field(ENUM, $2, $3.value);
481 enum_increment = 0x01 << (ffs($3.value) - 1);
483 '{' enum_entry_list '}'
488 | enum_entry_list ',' enum_entry
494 process_field(ENUM_ENTRY, $1, enum_next_value);
495 enum_next_value += enum_increment;
497 | T_SYMBOL expression
499 process_field(ENUM_ENTRY, $1, $2.value);
500 enum_next_value = $2.value + enum_increment;
505 T_MASK T_SYMBOL expression
507 process_field(MASK, $2, $3.value);
514 if ($2->type != UNINITIALIZED) {
515 stop("Re-definition of register alias",
520 initialize_symbol($2);
521 $2->info.ainfo->parent = cur_symbol;
528 if (accumulator.symbol != NULL) {
529 stop("Only one accumulator definition allowed",
533 accumulator.symbol = cur_symbol;
540 if (mode_ptr.symbol != NULL) {
541 stop("Only one mode pointer definition allowed",
545 mode_ptr.symbol = cur_symbol;
552 if (allones.symbol != NULL) {
553 stop("Only one definition of allones allowed",
557 allones.symbol = cur_symbol;
564 if (allzeros.symbol != NULL) {
565 stop("Only one definition of allzeros allowed",
569 allzeros.symbol = cur_symbol;
576 if (none.symbol != NULL) {
577 stop("Only one definition of none allowed",
581 none.symbol = cur_symbol;
588 if (sindex.symbol != NULL) {
589 stop("Only one definition of sindex allowed",
593 sindex.symbol = cur_symbol;
598 expression '|' expression
600 $$.value = $1.value | $3.value;
601 symlist_merge(&$$.referenced_syms,
603 &$3.referenced_syms);
605 | expression '&' expression
607 $$.value = $1.value & $3.value;
608 symlist_merge(&$$.referenced_syms,
610 &$3.referenced_syms);
612 | expression '+' expression
614 $$.value = $1.value + $3.value;
615 symlist_merge(&$$.referenced_syms,
617 &$3.referenced_syms);
619 | expression '-' expression
621 $$.value = $1.value - $3.value;
622 symlist_merge(&($$.referenced_syms),
623 &($1.referenced_syms),
624 &($3.referenced_syms));
626 | expression '*' expression
628 $$.value = $1.value * $3.value;
629 symlist_merge(&($$.referenced_syms),
630 &($1.referenced_syms),
631 &($3.referenced_syms));
633 | expression '/' expression
635 $$.value = $1.value / $3.value;
636 symlist_merge(&($$.referenced_syms),
637 &($1.referenced_syms),
638 &($3.referenced_syms));
640 | expression T_EXPR_LSHIFT expression
642 $$.value = $1.value << $3.value;
643 symlist_merge(&$$.referenced_syms,
645 &$3.referenced_syms);
647 | expression T_EXPR_RSHIFT expression
649 $$.value = $1.value >> $3.value;
650 symlist_merge(&$$.referenced_syms,
652 &$3.referenced_syms);
661 $$.value = (~$$.value) & 0xFF;
663 | '-' expression %prec UMINUS
666 $$.value = -$$.value;
671 SLIST_INIT(&$$.referenced_syms);
678 switch (symbol->type) {
680 symbol = $1->info.ainfo->parent;
684 $$.value = symbol->info.rinfo->address;
690 $$.value = symbol->info.finfo->value;
694 $$.value = symbol->info.cinfo->value;
699 snprintf(errbuf, sizeof(errbuf),
700 "Undefined symbol %s referenced",
702 stop(errbuf, EX_DATAERR);
707 SLIST_INIT(&$$.referenced_syms);
708 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
713 T_CONST T_SYMBOL expression
715 if ($2->type != UNINITIALIZED) {
716 stop("Re-definition of symbol as a constant",
721 initialize_symbol($2);
722 $2->info.cinfo->value = $3.value;
724 | T_CONST T_SYMBOL T_DOWNLOAD
727 stop("Invalid downloaded constant declaration",
731 if ($2->type != UNINITIALIZED) {
732 stop("Re-definition of symbol as a downloaded constant",
736 $2->type = DOWNLOAD_CONST;
737 initialize_symbol($2);
738 $2->info.cinfo->value = download_constant_count++;
745 if ($2->type != UNINITIALIZED) {
746 stop("Re-definition of symbol as a macro",
751 cur_symbol->type = MACRO;
752 initialize_symbol(cur_symbol);
757 macrodefn_prologue T_MACROBODY
761 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
764 cur_symbol->info.macroinfo->narg = $3;
770 /* Macros can take no arguments */
776 add_macro_arg($1, 0);
778 | macro_arglist ',' T_ARG
781 stop("Comma without preceding argument in arg list",
786 add_macro_arg($3, $1);
793 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
795 cur_symbol = symtable_get(SRAM_SYMNAME);
796 cur_symtype = SRAMLOC;
797 cur_symbol->type = SRAMLOC;
798 initialize_symbol(cur_symbol);
802 sram_or_scb_offset = cur_symbol->info.rinfo->address;
806 scb_or_sram_symbol = cur_symbol;
808 scb_or_sram_attributes
812 scb_or_sram_symbol = NULL;
819 cur_symbol = symtable_get(SCB_SYMNAME);
820 cur_symtype = SCBLOC;
821 if (cur_symbol->type != UNINITIALIZED) {
822 stop("Only one SRAM definition allowed",
826 cur_symbol->type = SCBLOC;
827 initialize_symbol(cur_symbol);
828 /* 64 bytes of SCB space */
829 cur_symbol->info.rinfo->size = 64;
833 sram_or_scb_offset = cur_symbol->info.rinfo->address;
837 scb_or_sram_symbol = cur_symbol;
839 scb_or_sram_attributes
843 scb_or_sram_symbol = NULL;
847 scb_or_sram_attributes:
848 /* NULL definition is okay */
850 | scb_or_sram_reg_list
851 | modes scb_or_sram_reg_list
854 scb_or_sram_reg_list:
856 | scb_or_sram_reg_list reg_definition
862 process_register(&$1);
866 | T_SYMBOL '[' T_SYMBOL ']'
868 process_register(&$1);
869 if ($3->type != CONST) {
870 stop("register offset must be a constant", EX_DATAERR);
873 if (($3->info.cinfo->value + 1) > (unsigned)$1->info.rinfo->size) {
874 stop("Accessing offset beyond range of register",
879 $$.offset = $3->info.cinfo->value;
881 | T_SYMBOL '[' T_NUMBER ']'
883 process_register(&$1);
884 if (($3 + 1) > (unsigned)$1->info.rinfo->size) {
885 stop("Accessing offset beyond range of register",
894 if (accumulator.symbol == NULL) {
895 stop("No accumulator has been defined", EX_DATAERR);
898 $$.symbol = accumulator.symbol;
906 test_writable_symbol($1.symbol);
919 if ($1.value == 0 && is_download_const(&$1) == 0) {
920 snprintf(errbuf, sizeof(errbuf),
921 "\nExpression evaluates to 0 and thus "
922 "references the accumulator.\n "
923 "If this is the desired effect, use 'A' "
925 stop(errbuf, EX_DATAERR);
931 SLIST_INIT(&$$.referenced_syms);
932 symlist_add(&$$.referenced_syms, accumulator.symbol,
933 SYMLIST_INSERT_HEAD);
941 test_readable_symbol($1.symbol);
962 T_SET_SRC_MODE T_NUMBER ';'
969 T_SET_DST_MODE T_NUMBER ';'
975 critical_section_start:
978 critical_section_t *cs;
980 if (in_critical_section != FALSE) {
981 stop("Critical Section within Critical Section",
986 cs->begin_addr = instruction_ptr;
987 in_critical_section = TRUE;
991 critical_section_end:
994 critical_section_t *cs;
996 if (in_critical_section == FALSE) {
997 stop("Unballanced 'end_cs'", EX_DATAERR);
1000 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1001 cs->end_addr = instruction_ptr;
1002 in_critical_section = FALSE;
1015 if ($2->type != UNINITIALIZED) {
1016 stop("Program label multiply defined", EX_DATAERR);
1020 initialize_symbol($2);
1021 $2->info.linfo->address = instruction_ptr;
1022 $2->info.linfo->exported = $1;
1032 | T_SYMBOL '+' T_NUMBER
1037 | T_SYMBOL '-' T_NUMBER
1064 add_conditional($2);
1065 new_scope = scope_alloc();
1066 new_scope->type = SCOPE_IF;
1067 new_scope->begin_addr = instruction_ptr;
1068 new_scope->func_num = $2->info.condinfo->func_num;
1070 | T_ELSE T_IF T_CEXPR '{'
1073 scope_t *scope_context;
1074 scope_t *last_scope;
1077 * Ensure that the previous scope is either an
1078 * if or and else if.
1080 scope_context = SLIST_FIRST(&scope_stack);
1081 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1083 if (last_scope == NULL
1084 || last_scope->type == T_ELSE) {
1086 stop("'else if' without leading 'if'", EX_DATAERR);
1089 add_conditional($3);
1090 new_scope = scope_alloc();
1091 new_scope->type = SCOPE_ELSE_IF;
1092 new_scope->begin_addr = instruction_ptr;
1093 new_scope->func_num = $3->info.condinfo->func_num;
1098 scope_t *scope_context;
1099 scope_t *last_scope;
1102 * Ensure that the previous scope is either an
1103 * if or and else if.
1105 scope_context = SLIST_FIRST(&scope_stack);
1106 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1108 if (last_scope == NULL
1109 || last_scope->type == SCOPE_ELSE) {
1111 stop("'else' without leading 'if'", EX_DATAERR);
1114 new_scope = scope_alloc();
1115 new_scope->type = SCOPE_ELSE;
1116 new_scope->begin_addr = instruction_ptr;
1123 scope_t *scope_context;
1125 scope_context = SLIST_FIRST(&scope_stack);
1126 if (scope_context->type == SCOPE_ROOT) {
1127 stop("Unexpected '}' encountered", EX_DATAERR);
1131 scope_context->end_addr = instruction_ptr;
1134 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1136 process_scope(scope_context);
1138 if (SLIST_FIRST(&scope_stack) == NULL) {
1139 stop("Unexpected '}' encountered", EX_DATAERR);
1146 T_AND { $$ = AIC_OP_AND; }
1147 | T_XOR { $$ = AIC_OP_XOR; }
1148 | T_ADD { $$ = AIC_OP_ADD; }
1149 | T_ADC { $$ = AIC_OP_ADC; }
1153 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1155 format_1_instr($1, &$2, &$4, &$5, $6);
1160 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1162 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1167 T_INC destination opt_source ret ';'
1171 make_expression(&immed, 1);
1172 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1177 T_DEC destination opt_source ret ';'
1181 make_expression(&immed, -1);
1182 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1191 make_expression(&immed, -1);
1192 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1194 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1196 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1205 make_expression(&immed, 1);
1206 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1208 | T_STC destination ret ';'
1212 make_expression(&immed, 1);
1213 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1218 T_BMOV destination ',' source ',' immediate ret ';'
1220 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1225 T_MOV destination ',' source ret ';'
1229 make_expression(&immed, 1);
1230 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1235 T_MVI destination ',' immediate ret ';'
1238 && is_download_const(&$4) == 0) {
1242 * Allow move immediates of 0 so that macros,
1243 * that can't know the immediate's value and
1244 * otherwise compensate, still work.
1246 make_expression(&immed, 1);
1247 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1249 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1255 T_NOT destination opt_source ret ';'
1259 make_expression(&immed, 0xff);
1260 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1265 T_CLR destination ret ';'
1269 make_expression(&immed, 0xff);
1270 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1279 make_expression(&immed, 0xff);
1280 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1289 make_expression(&immed, 0xff);
1290 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1295 * This grammer differs from the one in the aic7xxx
1296 * reference manual since the grammer listed there is
1297 * ambiguous and causes a shift/reduce conflict.
1298 * It also seems more logical as the "immediate"
1299 * argument is listed as the second arg like the
1304 T_SHL { $$ = AIC_OP_SHL; }
1305 | T_SHR { $$ = AIC_OP_SHR; }
1306 | T_ROL { $$ = AIC_OP_ROL; }
1307 | T_ROR { $$ = AIC_OP_ROR; }
1311 f2_opcode destination ',' expression opt_source ret ';'
1313 format_2_instr($1, &$2, &$4, &$5, $6);
1318 T_JMP { $$ = AIC_OP_JMP; }
1319 | T_JC { $$ = AIC_OP_JC; }
1320 | T_JNC { $$ = AIC_OP_JNC; }
1321 | T_CALL { $$ = AIC_OP_CALL; }
1325 T_JZ { $$ = AIC_OP_JZ; }
1326 | T_JNZ { $$ = AIC_OP_JNZ; }
1330 T_JE { $$ = AIC_OP_JE; }
1331 | T_JNE { $$ = AIC_OP_JNE; }
1335 jmp_jc_jnc_call address ';'
1339 make_expression(&immed, 0);
1340 format_3_instr($1, &sindex, &immed, &$2);
1345 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1347 format_3_instr($5, &$2, &$4, &$6);
1352 T_TEST source ',' immediate_or_a jz_jnz address ';'
1354 format_3_instr($5, &$2, &$4, &$6);
1359 T_CMP source ',' immediate_or_a je_jne address ';'
1361 format_3_instr($5, &$2, &$4, &$6);
1366 T_MOV source jmp_jc_jnc_call address ';'
1370 make_expression(&immed, 0);
1371 format_3_instr($3, &$2, &immed, &$4);
1376 T_MVI immediate jmp_jc_jnc_call address ';'
1378 format_3_instr($3, &allzeros, &$2, &$4);
1385 process_field(unsigned int field_type, symbol_t *sym, int value)
1388 * Add the current register to its
1389 * symbol list, if it already exists,
1390 * warn if we are setting it to a
1391 * different value, or in the bit to
1392 * the "allowed bits" of this register.
1394 if (sym->type == UNINITIALIZED) {
1395 sym->type = field_type;
1396 initialize_symbol(sym);
1397 sym->info.finfo->value = value;
1398 if (field_type != ENUM_ENTRY) {
1399 if (field_type != MASK && value == 0) {
1400 stop("Empty Field, or Enum", EX_DATAERR);
1403 sym->info.finfo->value = value;
1404 sym->info.finfo->mask = value;
1405 } else if (field_symbol != NULL) {
1406 sym->info.finfo->mask = field_symbol->info.finfo->value;
1408 sym->info.finfo->mask = 0xFF;
1410 } else if (sym->type != field_type) {
1411 stop("Field definition mirrors a definition of the same "
1412 " name, but a different type", EX_DATAERR);
1414 } else if (value != sym->info.finfo->value) {
1415 stop("Field redefined with a conflicting value", EX_DATAERR);
1418 /* Fail if this symbol is already listed */
1419 if (symlist_search(&(sym->info.finfo->symrefs),
1420 cur_symbol->name) != NULL) {
1421 stop("Field defined multiple times for register", EX_DATAERR);
1424 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1425 SYMLIST_INSERT_HEAD);
1426 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1427 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1428 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1432 initialize_symbol(symbol_t *symbol)
1434 switch (symbol->type) {
1436 stop("Call to initialize_symbol with type field unset",
1443 symbol->info.rinfo =
1444 (struct reg_info *)malloc(sizeof(struct reg_info));
1445 if (symbol->info.rinfo == NULL) {
1446 stop("Can't create register info", EX_SOFTWARE);
1449 memset(symbol->info.rinfo, 0,
1450 sizeof(struct reg_info));
1451 SLIST_INIT(&(symbol->info.rinfo->fields));
1453 * Default to allowing access in all register modes
1454 * or to the mode specified by the SCB or SRAM space
1457 if (scb_or_sram_symbol != NULL)
1458 symbol->info.rinfo->modes =
1459 scb_or_sram_symbol->info.rinfo->modes;
1461 symbol->info.rinfo->modes = ~0;
1464 symbol->info.ainfo =
1465 (struct alias_info *)malloc(sizeof(struct alias_info));
1466 if (symbol->info.ainfo == NULL) {
1467 stop("Can't create alias info", EX_SOFTWARE);
1470 memset(symbol->info.ainfo, 0,
1471 sizeof(struct alias_info));
1477 symbol->info.finfo =
1478 (struct field_info *)malloc(sizeof(struct field_info));
1479 if (symbol->info.finfo == NULL) {
1480 stop("Can't create field info", EX_SOFTWARE);
1483 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1484 SLIST_INIT(&(symbol->info.finfo->symrefs));
1487 case DOWNLOAD_CONST:
1488 symbol->info.cinfo =
1489 (struct const_info *)malloc(sizeof(struct const_info));
1490 if (symbol->info.cinfo == NULL) {
1491 stop("Can't create alias info", EX_SOFTWARE);
1494 memset(symbol->info.cinfo, 0,
1495 sizeof(struct const_info));
1498 symbol->info.linfo =
1499 (struct label_info *)malloc(sizeof(struct label_info));
1500 if (symbol->info.linfo == NULL) {
1501 stop("Can't create label info", EX_SOFTWARE);
1504 memset(symbol->info.linfo, 0,
1505 sizeof(struct label_info));
1508 symbol->info.condinfo =
1509 (struct cond_info *)malloc(sizeof(struct cond_info));
1510 if (symbol->info.condinfo == NULL) {
1511 stop("Can't create conditional info", EX_SOFTWARE);
1514 memset(symbol->info.condinfo, 0,
1515 sizeof(struct cond_info));
1518 symbol->info.macroinfo =
1519 (struct macro_info *)malloc(sizeof(struct macro_info));
1520 if (symbol->info.macroinfo == NULL) {
1521 stop("Can't create macro info", EX_SOFTWARE);
1524 memset(symbol->info.macroinfo, 0,
1525 sizeof(struct macro_info));
1526 STAILQ_INIT(&symbol->info.macroinfo->args);
1529 stop("Call to initialize_symbol with invalid symbol type",
1537 add_macro_arg(const char *argtext, int argnum __unused)
1539 struct macro_arg *marg;
1543 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1544 stop("Invalid current symbol for adding macro arg",
1549 marg = (struct macro_arg *)malloc(sizeof(*marg));
1551 stop("Can't create macro_arg structure", EX_SOFTWARE);
1554 marg->replacement_text = NULL;
1555 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1556 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1558 if (retval >= (int)sizeof(regex_pattern)) {
1559 stop("Regex text buffer too small for arg",
1563 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1565 stop("Regex compilation failed", EX_SOFTWARE);
1568 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1572 add_macro_body(const char *bodytext)
1574 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1575 stop("Invalid current symbol for adding macro arg",
1579 cur_symbol->info.macroinfo->body = strdup(bodytext);
1580 if (cur_symbol->info.macroinfo->body == NULL) {
1581 stop("Can't duplicate macro body text", EX_SOFTWARE);
1587 process_register(symbol_t **p_symbol)
1589 symbol_t *symbol = *p_symbol;
1591 if (symbol->type == UNINITIALIZED) {
1592 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1594 stop(errbuf, EX_DATAERR);
1596 } else if (symbol->type == ALIAS) {
1597 *p_symbol = symbol->info.ainfo->parent;
1598 } else if ((symbol->type != REGISTER)
1599 && (symbol->type != SCBLOC)
1600 && (symbol->type != SRAMLOC)) {
1601 snprintf(errbuf, sizeof(errbuf),
1602 "Specified symbol %s is not a register",
1604 stop(errbuf, EX_DATAERR);
1609 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1610 symbol_ref_t *src, int ret)
1612 struct instruction *instr;
1613 struct ins_format1 *f1_instr;
1615 if (src->symbol == NULL)
1618 /* Test register permissions */
1619 test_writable_symbol(dest->symbol);
1620 test_readable_symbol(src->symbol);
1622 /* Ensure that immediate makes sense for this destination */
1623 type_check(dest->symbol, immed, opcode);
1625 /* Allocate sequencer space for the instruction and fill it out */
1626 instr = seq_alloc();
1627 f1_instr = &instr->format.format1;
1628 f1_instr->ret = ret ? 1 : 0;
1629 f1_instr->opcode = opcode;
1630 f1_instr->destination = dest->symbol->info.rinfo->address
1632 f1_instr->source = src->symbol->info.rinfo->address
1634 f1_instr->immediate = immed->value;
1636 if (is_download_const(immed))
1637 f1_instr->parity = 1;
1638 else if (dest->symbol == mode_ptr.symbol) {
1643 * Attempt to update mode information if
1644 * we are operating on the mode register.
1646 if (src->symbol == allones.symbol)
1648 else if (src->symbol == allzeros.symbol)
1650 else if (src->symbol == mode_ptr.symbol)
1651 src_value = (dst_mode << 4) | src_mode;
1657 dst_value = src_value & immed->value;
1660 dst_value = src_value ^ immed->value;
1663 dst_value = (src_value + immed->value) & 0xFF;
1666 dst_value = src_value | immed->value;
1669 dst_value = src_value;
1674 src_mode = dst_value & 0xF;
1675 dst_mode = (dst_value >> 4) & 0xF;
1679 symlist_free(&immed->referenced_syms);
1684 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1685 symbol_ref_t *src, int ret)
1687 struct instruction *instr;
1688 struct ins_format2 *f2_instr;
1689 uint8_t shift_control;
1691 if (src->symbol == NULL)
1694 /* Test register permissions */
1695 test_writable_symbol(dest->symbol);
1696 test_readable_symbol(src->symbol);
1698 /* Allocate sequencer space for the instruction and fill it out */
1699 instr = seq_alloc();
1700 f2_instr = &instr->format.format2;
1701 f2_instr->ret = ret ? 1 : 0;
1702 f2_instr->opcode = AIC_OP_ROL;
1703 f2_instr->destination = dest->symbol->info.rinfo->address
1705 f2_instr->source = src->symbol->info.rinfo->address
1707 if (places->value > 8 || places->value <= 0) {
1708 stop("illegal shift value", EX_DATAERR);
1713 if (places->value == 8)
1714 shift_control = 0xf0;
1716 shift_control = (places->value << 4) | places->value;
1719 if (places->value == 8) {
1720 shift_control = 0xf8;
1722 shift_control = (places->value << 4)
1723 | (8 - places->value)
1728 shift_control = places->value & 0x7;
1731 shift_control = (8 - places->value) | 0x08;
1734 shift_control = 0; /* Quiet Compiler */
1735 stop("Invalid shift operation specified", EX_SOFTWARE);
1739 f2_instr->shift_control = shift_control;
1740 symlist_free(&places->referenced_syms);
1745 format_3_instr(int opcode, symbol_ref_t *src,
1746 expression_t *immed, symbol_ref_t *address)
1748 struct instruction *instr;
1749 struct ins_format3 *f3_instr;
1752 /* Test register permissions */
1753 test_readable_symbol(src->symbol);
1755 /* Ensure that immediate makes sense for this source */
1756 type_check(src->symbol, immed, opcode);
1758 /* Allocate sequencer space for the instruction and fill it out */
1759 instr = seq_alloc();
1760 f3_instr = &instr->format.format3;
1761 if (address->symbol == NULL) {
1762 /* 'dot' referrence. Use the current instruction pointer */
1763 addr = instruction_ptr + address->offset;
1764 } else if (address->symbol->type == UNINITIALIZED) {
1765 /* forward reference */
1766 addr = address->offset;
1767 instr->patch_label = address->symbol;
1769 addr = address->symbol->info.linfo->address + address->offset;
1770 f3_instr->opcode = opcode;
1771 f3_instr->address = addr;
1772 f3_instr->source = src->symbol->info.rinfo->address
1774 f3_instr->immediate = immed->value;
1776 if (is_download_const(immed))
1777 f3_instr->parity = 1;
1779 symlist_free(&immed->referenced_syms);
1784 test_readable_symbol(symbol_t *symbol)
1787 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1788 snprintf(errbuf, sizeof(errbuf),
1789 "Register %s unavailable in source reg mode %d",
1790 symbol->name, src_mode);
1791 stop(errbuf, EX_DATAERR);
1794 if (symbol->info.rinfo->mode == WO) {
1795 stop("Write Only register specified as source",
1802 test_writable_symbol(symbol_t *symbol)
1805 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1806 snprintf(errbuf, sizeof(errbuf),
1807 "Register %s unavailable in destination reg mode %d",
1808 symbol->name, dst_mode);
1809 stop(errbuf, EX_DATAERR);
1812 if (symbol->info.rinfo->mode == RO) {
1813 stop("Read Only register specified as destination",
1820 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1822 symbol_node_t *node;
1826 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ || 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.
1835 && (expression->value & ~symbol->info.rinfo->valid_bitmask) != 0) {
1836 snprintf(errbuf, sizeof(errbuf),
1837 "Invalid bit(s) 0x%x in immediate written to %s",
1838 expression->value & ~symbol->info.rinfo->valid_bitmask,
1840 stop(errbuf, EX_DATAERR);
1845 * Now make sure that all of the symbols referenced by the
1846 * expression are defined for this register.
1848 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1849 for(node = expression->referenced_syms.slh_first;
1851 node = node->links.sle_next) {
1852 if ((node->symbol->type == MASK
1853 || node->symbol->type == FIELD
1854 || node->symbol->type == ENUM
1855 || node->symbol->type == ENUM_ENTRY)
1856 && symlist_search(&node->symbol->info.finfo->symrefs,
1857 symbol->name) == NULL) {
1858 snprintf(errbuf, sizeof(errbuf),
1859 "Invalid field or mask %s "
1861 node->symbol->name, symbol->name);
1862 stop(errbuf, EX_DATAERR);
1870 make_expression(expression_t *immed, int value)
1872 SLIST_INIT(&immed->referenced_syms);
1873 immed->value = value & 0xff;
1877 add_conditional(symbol_t *symbol)
1879 static int numfuncs;
1881 if (numfuncs == 0) {
1882 /* add a special conditional, "0" */
1883 symbol_t *false_func;
1885 false_func = symtable_get("0");
1886 if (false_func->type != UNINITIALIZED) {
1887 stop("Conditional expression '0' "
1888 "conflicts with a symbol", EX_DATAERR);
1891 false_func->type = CONDITIONAL;
1892 initialize_symbol(false_func);
1893 false_func->info.condinfo->func_num = numfuncs++;
1894 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1897 /* This condition has occurred before */
1898 if (symbol->type == CONDITIONAL)
1901 if (symbol->type != UNINITIALIZED) {
1902 stop("Conditional expression conflicts with a symbol",
1907 symbol->type = CONDITIONAL;
1908 initialize_symbol(symbol);
1909 symbol->info.condinfo->func_num = numfuncs++;
1910 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1914 add_version(const char *verstring)
1916 const char verprefix[] = " * ";
1920 newlen = strlen(verstring) + strlen(verprefix);
1922 if (versions != NULL)
1923 oldlen = strlen(versions);
1924 versions = realloc(versions, newlen + oldlen + 2);
1925 if (versions == NULL)
1926 stop("Can't allocate version string", EX_SOFTWARE);
1927 strcpy(&versions[oldlen], verprefix);
1928 strcpy(&versions[oldlen + strlen(verprefix)], verstring);
1929 versions[newlen + oldlen] = '\n';
1930 versions[newlen + oldlen + 1] = '\0';
1934 yyerror(const char *string)
1936 stop(string, EX_DATAERR);
1940 is_download_const(expression_t *immed)
1942 if ((immed->referenced_syms.slh_first != NULL)
1943 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))