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);
106 extern int yyparse (void);
108 #define SRAM_SYMNAME "SRAM_BASE"
109 #define SCB_SYMNAME "SCB_BASE"
116 symbol_ref_t sym_ref;
117 expression_t expression;
122 %token <value> T_CONST
140 %token <value> T_ADDRESS
148 %token T_SET_SRC_MODE
150 %token T_SET_DST_MODE
152 %token <value> T_MODE
164 %token <value> T_NUMBER
166 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
170 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
172 %token <value> T_SHR T_SHL T_ROR T_ROL
174 %token <value> T_MVI T_MOV T_CLR T_BMOV
176 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
178 %token <value> T_ADD T_ADC
180 %token <value> T_INC T_DEC
182 %token <value> T_STC T_CLC
184 %token <value> T_CMP T_NOT T_XOR
186 %token <value> T_TEST T_AND
194 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
198 %token <sym> T_SYMBOL
202 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
204 %type <sym_ref> reg_symbol address destination source opt_source
206 %type <expression> expression immediate immediate_or_a
208 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
210 %type <value> mode_value mode_list macro_arglist
214 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
227 | program patch_arg_list
237 | program scratch_ram
243 | program set_src_mode
245 | program set_dst_mode
246 | critical_section_start
247 | program critical_section_start
248 | critical_section_end
249 | program critical_section_end
251 | program conditional
257 T_INCLUDE '<' T_PATH '>'
259 include_file($3, BRACKETED_INCLUDE);
261 | T_INCLUDE '"' T_PATH '"'
263 include_file($3, QUOTED_INCLUDE);
268 T_PREFIX '=' T_STRING
270 if (prefix != stock_prefix)
271 stop("Prefix multiply defined",
275 stop("Unable to record prefix", EX_SOFTWARE);
280 T_PATCH_ARG_LIST '=' T_STRING
282 if (patch_arg_list != NULL)
283 stop("Patch argument list multiply defined",
285 patch_arg_list = strdup($3);
286 if (patch_arg_list == NULL)
287 stop("Unable to record patch arg list", EX_SOFTWARE);
292 T_VERSION '=' T_STRING
297 T_REGISTER { cur_symtype = REGISTER; } reg_definition
303 if ($1->type != UNINITIALIZED) {
304 stop("Register multiply defined", EX_DATAERR);
308 cur_symbol->type = cur_symtype;
309 initialize_symbol(cur_symbol);
315 * Default to allowing everything in for registers
316 * with no bit or mask definitions.
318 if (cur_symbol->info.rinfo->valid_bitmask == 0)
319 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
321 if (cur_symbol->info.rinfo->size == 0)
322 cur_symbol->info.rinfo->size = 1;
325 * This might be useful for registers too.
327 if (cur_symbol->type != REGISTER) {
328 if (cur_symbol->info.rinfo->address == 0)
329 cur_symbol->info.rinfo->address =
331 sram_or_scb_offset +=
332 cur_symbol->info.rinfo->size;
340 | reg_attribute_list reg_attribute
363 cur_symbol->info.rinfo->address = $2;
370 cur_symbol->info.rinfo->size = $2;
371 if (scb_or_sram_symbol != NULL) {
375 max_addr = scb_or_sram_symbol->info.rinfo->address
376 + scb_or_sram_symbol->info.rinfo->size;
377 sym_max_addr = cur_symbol->info.rinfo->address
378 + cur_symbol->info.rinfo->size;
380 if (sym_max_addr > max_addr)
381 stop("SCB or SRAM space exhausted", EX_DATAERR);
389 cur_symbol->info.rinfo->mode = $2;
396 cur_symbol->info.rinfo->modes = $2;
405 | mode_list ',' mode_value
415 stop("Valid register modes range between 0 and 4.",
427 if (symbol->type != CONST) {
428 stop("Only \"const\" symbols allowed in "
429 "mode definitions.", EX_DATAERR);
432 if (symbol->info.cinfo->value > 4) {
433 stop("Valid register modes range between 0 and 4.",
437 $$ = (0x1 << symbol->info.cinfo->value);
448 '{' enum_entry_list '}'
449 | T_FIELD T_SYMBOL expression
451 process_field(FIELD, $2, $3.value);
454 enum_increment = 0x01 << (ffs($3.value) - 1);
456 '{' enum_entry_list '}'
457 | T_FIELD T_SYMBOL expression
459 process_field(FIELD, $2, $3.value);
470 '{' enum_entry_list '}'
471 | T_ENUM T_SYMBOL expression
473 process_field(ENUM, $2, $3.value);
476 enum_increment = 0x01 << (ffs($3.value) - 1);
478 '{' enum_entry_list '}'
483 | enum_entry_list ',' enum_entry
489 process_field(ENUM_ENTRY, $1, enum_next_value);
490 enum_next_value += enum_increment;
492 | T_SYMBOL expression
494 process_field(ENUM_ENTRY, $1, $2.value);
495 enum_next_value = $2.value + enum_increment;
500 T_MASK T_SYMBOL expression
502 process_field(MASK, $2, $3.value);
509 if ($2->type != UNINITIALIZED) {
510 stop("Re-definition of register alias",
515 initialize_symbol($2);
516 $2->info.ainfo->parent = cur_symbol;
523 if (accumulator.symbol != NULL) {
524 stop("Only one accumulator definition allowed",
528 accumulator.symbol = cur_symbol;
535 if (mode_ptr.symbol != NULL) {
536 stop("Only one mode pointer definition allowed",
540 mode_ptr.symbol = cur_symbol;
547 if (allones.symbol != NULL) {
548 stop("Only one definition of allones allowed",
552 allones.symbol = cur_symbol;
559 if (allzeros.symbol != NULL) {
560 stop("Only one definition of allzeros allowed",
564 allzeros.symbol = cur_symbol;
571 if (none.symbol != NULL) {
572 stop("Only one definition of none allowed",
576 none.symbol = cur_symbol;
583 if (sindex.symbol != NULL) {
584 stop("Only one definition of sindex allowed",
588 sindex.symbol = cur_symbol;
593 expression '|' expression
595 $$.value = $1.value | $3.value;
596 symlist_merge(&$$.referenced_syms,
598 &$3.referenced_syms);
600 | expression '&' expression
602 $$.value = $1.value & $3.value;
603 symlist_merge(&$$.referenced_syms,
605 &$3.referenced_syms);
607 | expression '+' expression
609 $$.value = $1.value + $3.value;
610 symlist_merge(&$$.referenced_syms,
612 &$3.referenced_syms);
614 | expression '-' expression
616 $$.value = $1.value - $3.value;
617 symlist_merge(&($$.referenced_syms),
618 &($1.referenced_syms),
619 &($3.referenced_syms));
621 | expression '*' expression
623 $$.value = $1.value * $3.value;
624 symlist_merge(&($$.referenced_syms),
625 &($1.referenced_syms),
626 &($3.referenced_syms));
628 | expression '/' expression
630 $$.value = $1.value / $3.value;
631 symlist_merge(&($$.referenced_syms),
632 &($1.referenced_syms),
633 &($3.referenced_syms));
635 | expression T_EXPR_LSHIFT expression
637 $$.value = $1.value << $3.value;
638 symlist_merge(&$$.referenced_syms,
640 &$3.referenced_syms);
642 | expression T_EXPR_RSHIFT expression
644 $$.value = $1.value >> $3.value;
645 symlist_merge(&$$.referenced_syms,
647 &$3.referenced_syms);
656 $$.value = (~$$.value) & 0xFF;
658 | '-' expression %prec UMINUS
661 $$.value = -$$.value;
666 SLIST_INIT(&$$.referenced_syms);
673 switch (symbol->type) {
675 symbol = $1->info.ainfo->parent;
679 $$.value = symbol->info.rinfo->address;
685 $$.value = symbol->info.finfo->value;
689 $$.value = symbol->info.cinfo->value;
694 snprintf(errbuf, sizeof(errbuf),
695 "Undefined symbol %s referenced",
697 stop(errbuf, EX_DATAERR);
702 SLIST_INIT(&$$.referenced_syms);
703 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
708 T_CONST T_SYMBOL expression
710 if ($2->type != UNINITIALIZED) {
711 stop("Re-definition of symbol as a constant",
716 initialize_symbol($2);
717 $2->info.cinfo->value = $3.value;
719 | T_CONST T_SYMBOL T_DOWNLOAD
722 stop("Invalid downloaded constant declaration",
726 if ($2->type != UNINITIALIZED) {
727 stop("Re-definition of symbol as a downloaded constant",
731 $2->type = DOWNLOAD_CONST;
732 initialize_symbol($2);
733 $2->info.cinfo->value = download_constant_count++;
740 if ($2->type != UNINITIALIZED) {
741 stop("Re-definition of symbol as a macro",
746 cur_symbol->type = MACRO;
747 initialize_symbol(cur_symbol);
752 macrodefn_prologue T_MACROBODY
756 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
759 cur_symbol->info.macroinfo->narg = $3;
765 /* Macros can take no arguments */
771 add_macro_arg($1, 0);
773 | macro_arglist ',' T_ARG
776 stop("Comma without preceding argument in arg list",
781 add_macro_arg($3, $1);
788 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
790 cur_symbol = symtable_get(SRAM_SYMNAME);
791 cur_symtype = SRAMLOC;
792 cur_symbol->type = SRAMLOC;
793 initialize_symbol(cur_symbol);
797 sram_or_scb_offset = cur_symbol->info.rinfo->address;
801 scb_or_sram_symbol = cur_symbol;
803 scb_or_sram_attributes
807 scb_or_sram_symbol = NULL;
814 cur_symbol = symtable_get(SCB_SYMNAME);
815 cur_symtype = SCBLOC;
816 if (cur_symbol->type != UNINITIALIZED) {
817 stop("Only one SRAM definition allowed",
821 cur_symbol->type = SCBLOC;
822 initialize_symbol(cur_symbol);
823 /* 64 bytes of SCB space */
824 cur_symbol->info.rinfo->size = 64;
828 sram_or_scb_offset = cur_symbol->info.rinfo->address;
832 scb_or_sram_symbol = cur_symbol;
834 scb_or_sram_attributes
838 scb_or_sram_symbol = NULL;
842 scb_or_sram_attributes:
843 /* NULL definition is okay */
845 | scb_or_sram_reg_list
846 | modes scb_or_sram_reg_list
849 scb_or_sram_reg_list:
851 | scb_or_sram_reg_list reg_definition
857 process_register(&$1);
861 | T_SYMBOL '[' T_SYMBOL ']'
863 process_register(&$1);
864 if ($3->type != CONST) {
865 stop("register offset must be a constant", EX_DATAERR);
868 if (($3->info.cinfo->value + 1) > (unsigned)$1->info.rinfo->size) {
869 stop("Accessing offset beyond range of register",
874 $$.offset = $3->info.cinfo->value;
876 | T_SYMBOL '[' T_NUMBER ']'
878 process_register(&$1);
879 if (($3 + 1) > (unsigned)$1->info.rinfo->size) {
880 stop("Accessing offset beyond range of register",
889 if (accumulator.symbol == NULL) {
890 stop("No accumulator has been defined", EX_DATAERR);
893 $$.symbol = accumulator.symbol;
901 test_writable_symbol($1.symbol);
914 if ($1.value == 0 && is_download_const(&$1) == 0) {
915 snprintf(errbuf, sizeof(errbuf),
916 "\nExpression evaluates to 0 and thus "
917 "references the accumulator.\n "
918 "If this is the desired effect, use 'A' "
920 stop(errbuf, EX_DATAERR);
926 SLIST_INIT(&$$.referenced_syms);
927 symlist_add(&$$.referenced_syms, accumulator.symbol,
928 SYMLIST_INSERT_HEAD);
936 test_readable_symbol($1.symbol);
957 T_SET_SRC_MODE T_NUMBER ';'
964 T_SET_DST_MODE T_NUMBER ';'
970 critical_section_start:
973 critical_section_t *cs;
975 if (in_critical_section != FALSE) {
976 stop("Critical Section within Critical Section",
981 cs->begin_addr = instruction_ptr;
982 in_critical_section = TRUE;
986 critical_section_end:
989 critical_section_t *cs;
991 if (in_critical_section == FALSE) {
992 stop("Unballanced 'end_cs'", EX_DATAERR);
995 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
996 cs->end_addr = instruction_ptr;
997 in_critical_section = FALSE;
1010 if ($2->type != UNINITIALIZED) {
1011 stop("Program label multiply defined", EX_DATAERR);
1015 initialize_symbol($2);
1016 $2->info.linfo->address = instruction_ptr;
1017 $2->info.linfo->exported = $1;
1027 | T_SYMBOL '+' T_NUMBER
1032 | T_SYMBOL '-' T_NUMBER
1059 add_conditional($2);
1060 new_scope = scope_alloc();
1061 new_scope->type = SCOPE_IF;
1062 new_scope->begin_addr = instruction_ptr;
1063 new_scope->func_num = $2->info.condinfo->func_num;
1065 | T_ELSE T_IF T_CEXPR '{'
1068 scope_t *scope_context;
1069 scope_t *last_scope;
1072 * Ensure that the previous scope is either an
1073 * if or and else if.
1075 scope_context = SLIST_FIRST(&scope_stack);
1076 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1078 if (last_scope == NULL
1079 || last_scope->type == T_ELSE) {
1081 stop("'else if' without leading 'if'", EX_DATAERR);
1084 add_conditional($3);
1085 new_scope = scope_alloc();
1086 new_scope->type = SCOPE_ELSE_IF;
1087 new_scope->begin_addr = instruction_ptr;
1088 new_scope->func_num = $3->info.condinfo->func_num;
1093 scope_t *scope_context;
1094 scope_t *last_scope;
1097 * Ensure that the previous scope is either an
1098 * if or and else if.
1100 scope_context = SLIST_FIRST(&scope_stack);
1101 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1103 if (last_scope == NULL
1104 || last_scope->type == SCOPE_ELSE) {
1106 stop("'else' without leading 'if'", EX_DATAERR);
1109 new_scope = scope_alloc();
1110 new_scope->type = SCOPE_ELSE;
1111 new_scope->begin_addr = instruction_ptr;
1118 scope_t *scope_context;
1120 scope_context = SLIST_FIRST(&scope_stack);
1121 if (scope_context->type == SCOPE_ROOT) {
1122 stop("Unexpected '}' encountered", EX_DATAERR);
1126 scope_context->end_addr = instruction_ptr;
1129 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1131 process_scope(scope_context);
1133 if (SLIST_FIRST(&scope_stack) == NULL) {
1134 stop("Unexpected '}' encountered", EX_DATAERR);
1141 T_AND { $$ = AIC_OP_AND; }
1142 | T_XOR { $$ = AIC_OP_XOR; }
1143 | T_ADD { $$ = AIC_OP_ADD; }
1144 | T_ADC { $$ = AIC_OP_ADC; }
1148 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1150 format_1_instr($1, &$2, &$4, &$5, $6);
1155 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1157 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1162 T_INC destination opt_source ret ';'
1166 make_expression(&immed, 1);
1167 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1172 T_DEC destination opt_source ret ';'
1176 make_expression(&immed, -1);
1177 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1186 make_expression(&immed, -1);
1187 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1189 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1191 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1200 make_expression(&immed, 1);
1201 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1203 | T_STC destination ret ';'
1207 make_expression(&immed, 1);
1208 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1213 T_BMOV destination ',' source ',' immediate ret ';'
1215 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1220 T_MOV destination ',' source ret ';'
1224 make_expression(&immed, 1);
1225 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1230 T_MVI destination ',' immediate ret ';'
1233 && is_download_const(&$4) == 0) {
1237 * Allow move immediates of 0 so that macros,
1238 * that can't know the immediate's value and
1239 * otherwise compensate, still work.
1241 make_expression(&immed, 1);
1242 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1244 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1250 T_NOT destination opt_source ret ';'
1254 make_expression(&immed, 0xff);
1255 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1260 T_CLR destination ret ';'
1264 make_expression(&immed, 0xff);
1265 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1274 make_expression(&immed, 0xff);
1275 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1284 make_expression(&immed, 0xff);
1285 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1290 * This grammer differs from the one in the aic7xxx
1291 * reference manual since the grammer listed there is
1292 * ambiguous and causes a shift/reduce conflict.
1293 * It also seems more logical as the "immediate"
1294 * argument is listed as the second arg like the
1299 T_SHL { $$ = AIC_OP_SHL; }
1300 | T_SHR { $$ = AIC_OP_SHR; }
1301 | T_ROL { $$ = AIC_OP_ROL; }
1302 | T_ROR { $$ = AIC_OP_ROR; }
1306 f2_opcode destination ',' expression opt_source ret ';'
1308 format_2_instr($1, &$2, &$4, &$5, $6);
1313 T_JMP { $$ = AIC_OP_JMP; }
1314 | T_JC { $$ = AIC_OP_JC; }
1315 | T_JNC { $$ = AIC_OP_JNC; }
1316 | T_CALL { $$ = AIC_OP_CALL; }
1320 T_JZ { $$ = AIC_OP_JZ; }
1321 | T_JNZ { $$ = AIC_OP_JNZ; }
1325 T_JE { $$ = AIC_OP_JE; }
1326 | T_JNE { $$ = AIC_OP_JNE; }
1330 jmp_jc_jnc_call address ';'
1334 make_expression(&immed, 0);
1335 format_3_instr($1, &sindex, &immed, &$2);
1340 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1342 format_3_instr($5, &$2, &$4, &$6);
1347 T_TEST source ',' immediate_or_a jz_jnz address ';'
1349 format_3_instr($5, &$2, &$4, &$6);
1354 T_CMP source ',' immediate_or_a je_jne address ';'
1356 format_3_instr($5, &$2, &$4, &$6);
1361 T_MOV source jmp_jc_jnc_call address ';'
1365 make_expression(&immed, 0);
1366 format_3_instr($3, &$2, &immed, &$4);
1371 T_MVI immediate jmp_jc_jnc_call address ';'
1373 format_3_instr($3, &allzeros, &$2, &$4);
1380 process_field(unsigned int field_type, symbol_t *sym, int value)
1383 * Add the current register to its
1384 * symbol list, if it already exists,
1385 * warn if we are setting it to a
1386 * different value, or in the bit to
1387 * the "allowed bits" of this register.
1389 if (sym->type == UNINITIALIZED) {
1390 sym->type = field_type;
1391 initialize_symbol(sym);
1392 sym->info.finfo->value = value;
1393 if (field_type != ENUM_ENTRY) {
1394 if (field_type != MASK && value == 0) {
1395 stop("Empty Field, or Enum", EX_DATAERR);
1398 sym->info.finfo->value = value;
1399 sym->info.finfo->mask = value;
1400 } else if (field_symbol != NULL) {
1401 sym->info.finfo->mask = field_symbol->info.finfo->value;
1403 sym->info.finfo->mask = 0xFF;
1405 } else if (sym->type != field_type) {
1406 stop("Field definition mirrors a definition of the same "
1407 " name, but a different type", EX_DATAERR);
1409 } else if (value != sym->info.finfo->value) {
1410 stop("Field redefined with a conflicting value", EX_DATAERR);
1413 /* Fail if this symbol is already listed */
1414 if (symlist_search(&(sym->info.finfo->symrefs),
1415 cur_symbol->name) != NULL) {
1416 stop("Field defined multiple times for register", EX_DATAERR);
1419 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1420 SYMLIST_INSERT_HEAD);
1421 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1422 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1423 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1427 initialize_symbol(symbol_t *symbol)
1429 switch (symbol->type) {
1431 stop("Call to initialize_symbol with type field unset",
1438 symbol->info.rinfo =
1439 (struct reg_info *)malloc(sizeof(struct reg_info));
1440 if (symbol->info.rinfo == NULL) {
1441 stop("Can't create register info", EX_SOFTWARE);
1444 memset(symbol->info.rinfo, 0,
1445 sizeof(struct reg_info));
1446 SLIST_INIT(&(symbol->info.rinfo->fields));
1448 * Default to allowing access in all register modes
1449 * or to the mode specified by the SCB or SRAM space
1452 if (scb_or_sram_symbol != NULL)
1453 symbol->info.rinfo->modes =
1454 scb_or_sram_symbol->info.rinfo->modes;
1456 symbol->info.rinfo->modes = ~0;
1459 symbol->info.ainfo =
1460 (struct alias_info *)malloc(sizeof(struct alias_info));
1461 if (symbol->info.ainfo == NULL) {
1462 stop("Can't create alias info", EX_SOFTWARE);
1465 memset(symbol->info.ainfo, 0,
1466 sizeof(struct alias_info));
1472 symbol->info.finfo =
1473 (struct field_info *)malloc(sizeof(struct field_info));
1474 if (symbol->info.finfo == NULL) {
1475 stop("Can't create field info", EX_SOFTWARE);
1478 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1479 SLIST_INIT(&(symbol->info.finfo->symrefs));
1482 case DOWNLOAD_CONST:
1483 symbol->info.cinfo =
1484 (struct const_info *)malloc(sizeof(struct const_info));
1485 if (symbol->info.cinfo == NULL) {
1486 stop("Can't create alias info", EX_SOFTWARE);
1489 memset(symbol->info.cinfo, 0,
1490 sizeof(struct const_info));
1493 symbol->info.linfo =
1494 (struct label_info *)malloc(sizeof(struct label_info));
1495 if (symbol->info.linfo == NULL) {
1496 stop("Can't create label info", EX_SOFTWARE);
1499 memset(symbol->info.linfo, 0,
1500 sizeof(struct label_info));
1503 symbol->info.condinfo =
1504 (struct cond_info *)malloc(sizeof(struct cond_info));
1505 if (symbol->info.condinfo == NULL) {
1506 stop("Can't create conditional info", EX_SOFTWARE);
1509 memset(symbol->info.condinfo, 0,
1510 sizeof(struct cond_info));
1513 symbol->info.macroinfo =
1514 (struct macro_info *)malloc(sizeof(struct macro_info));
1515 if (symbol->info.macroinfo == NULL) {
1516 stop("Can't create macro info", EX_SOFTWARE);
1519 memset(symbol->info.macroinfo, 0,
1520 sizeof(struct macro_info));
1521 STAILQ_INIT(&symbol->info.macroinfo->args);
1524 stop("Call to initialize_symbol with invalid symbol type",
1532 add_macro_arg(const char *argtext, int argnum __unused)
1534 struct macro_arg *marg;
1538 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1539 stop("Invalid current symbol for adding macro arg",
1544 marg = (struct macro_arg *)malloc(sizeof(*marg));
1546 stop("Can't create macro_arg structure", EX_SOFTWARE);
1549 marg->replacement_text = NULL;
1550 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1551 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1553 if (retval >= (int)sizeof(regex_pattern)) {
1554 stop("Regex text buffer too small for arg",
1558 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1560 stop("Regex compilation failed", EX_SOFTWARE);
1563 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1567 add_macro_body(const char *bodytext)
1569 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1570 stop("Invalid current symbol for adding macro arg",
1574 cur_symbol->info.macroinfo->body = strdup(bodytext);
1575 if (cur_symbol->info.macroinfo->body == NULL) {
1576 stop("Can't duplicate macro body text", EX_SOFTWARE);
1582 process_register(symbol_t **p_symbol)
1584 symbol_t *symbol = *p_symbol;
1586 if (symbol->type == UNINITIALIZED) {
1587 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1589 stop(errbuf, EX_DATAERR);
1591 } else if (symbol->type == ALIAS) {
1592 *p_symbol = symbol->info.ainfo->parent;
1593 } else if ((symbol->type != REGISTER)
1594 && (symbol->type != SCBLOC)
1595 && (symbol->type != SRAMLOC)) {
1596 snprintf(errbuf, sizeof(errbuf),
1597 "Specified symbol %s is not a register",
1599 stop(errbuf, EX_DATAERR);
1604 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1605 symbol_ref_t *src, int ret)
1607 struct instruction *instr;
1608 struct ins_format1 *f1_instr;
1610 if (src->symbol == NULL)
1613 /* Test register permissions */
1614 test_writable_symbol(dest->symbol);
1615 test_readable_symbol(src->symbol);
1617 /* Ensure that immediate makes sense for this destination */
1618 type_check(dest->symbol, immed, opcode);
1620 /* Allocate sequencer space for the instruction and fill it out */
1621 instr = seq_alloc();
1622 f1_instr = &instr->format.format1;
1623 f1_instr->ret = ret ? 1 : 0;
1624 f1_instr->opcode = opcode;
1625 f1_instr->destination = dest->symbol->info.rinfo->address
1627 f1_instr->source = src->symbol->info.rinfo->address
1629 f1_instr->immediate = immed->value;
1631 if (is_download_const(immed))
1632 f1_instr->parity = 1;
1633 else if (dest->symbol == mode_ptr.symbol) {
1638 * Attempt to update mode information if
1639 * we are operating on the mode register.
1641 if (src->symbol == allones.symbol)
1643 else if (src->symbol == allzeros.symbol)
1645 else if (src->symbol == mode_ptr.symbol)
1646 src_value = (dst_mode << 4) | src_mode;
1652 dst_value = src_value & immed->value;
1655 dst_value = src_value ^ immed->value;
1658 dst_value = (src_value + immed->value) & 0xFF;
1661 dst_value = src_value | immed->value;
1664 dst_value = src_value;
1669 src_mode = dst_value & 0xF;
1670 dst_mode = (dst_value >> 4) & 0xF;
1674 symlist_free(&immed->referenced_syms);
1679 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1680 symbol_ref_t *src, int ret)
1682 struct instruction *instr;
1683 struct ins_format2 *f2_instr;
1684 uint8_t shift_control;
1686 if (src->symbol == NULL)
1689 /* Test register permissions */
1690 test_writable_symbol(dest->symbol);
1691 test_readable_symbol(src->symbol);
1693 /* Allocate sequencer space for the instruction and fill it out */
1694 instr = seq_alloc();
1695 f2_instr = &instr->format.format2;
1696 f2_instr->ret = ret ? 1 : 0;
1697 f2_instr->opcode = AIC_OP_ROL;
1698 f2_instr->destination = dest->symbol->info.rinfo->address
1700 f2_instr->source = src->symbol->info.rinfo->address
1702 if (places->value > 8 || places->value <= 0) {
1703 stop("illegal shift value", EX_DATAERR);
1708 if (places->value == 8)
1709 shift_control = 0xf0;
1711 shift_control = (places->value << 4) | places->value;
1714 if (places->value == 8) {
1715 shift_control = 0xf8;
1717 shift_control = (places->value << 4)
1718 | (8 - places->value)
1723 shift_control = places->value & 0x7;
1726 shift_control = (8 - places->value) | 0x08;
1729 shift_control = 0; /* Quiet Compiler */
1730 stop("Invalid shift operation specified", EX_SOFTWARE);
1734 f2_instr->shift_control = shift_control;
1735 symlist_free(&places->referenced_syms);
1740 format_3_instr(int opcode, symbol_ref_t *src,
1741 expression_t *immed, symbol_ref_t *address)
1743 struct instruction *instr;
1744 struct ins_format3 *f3_instr;
1747 /* Test register permissions */
1748 test_readable_symbol(src->symbol);
1750 /* Ensure that immediate makes sense for this source */
1751 type_check(src->symbol, immed, opcode);
1753 /* Allocate sequencer space for the instruction and fill it out */
1754 instr = seq_alloc();
1755 f3_instr = &instr->format.format3;
1756 if (address->symbol == NULL) {
1757 /* 'dot' referrence. Use the current instruction pointer */
1758 addr = instruction_ptr + address->offset;
1759 } else if (address->symbol->type == UNINITIALIZED) {
1760 /* forward reference */
1761 addr = address->offset;
1762 instr->patch_label = address->symbol;
1764 addr = address->symbol->info.linfo->address + address->offset;
1765 f3_instr->opcode = opcode;
1766 f3_instr->address = addr;
1767 f3_instr->source = src->symbol->info.rinfo->address
1769 f3_instr->immediate = immed->value;
1771 if (is_download_const(immed))
1772 f3_instr->parity = 1;
1774 symlist_free(&immed->referenced_syms);
1779 test_readable_symbol(symbol_t *symbol)
1782 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1783 snprintf(errbuf, sizeof(errbuf),
1784 "Register %s unavailable in source reg mode %d",
1785 symbol->name, src_mode);
1786 stop(errbuf, EX_DATAERR);
1789 if (symbol->info.rinfo->mode == WO) {
1790 stop("Write Only register specified as source",
1797 test_writable_symbol(symbol_t *symbol)
1800 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1801 snprintf(errbuf, sizeof(errbuf),
1802 "Register %s unavailable in destination reg mode %d",
1803 symbol->name, dst_mode);
1804 stop(errbuf, EX_DATAERR);
1807 if (symbol->info.rinfo->mode == RO) {
1808 stop("Read Only register specified as destination",
1815 type_check(symbol_t *symbol, expression_t *expression, int opcode)
1817 symbol_node_t *node;
1819 uint8_t invalid_bits;
1822 if (opcode == AIC_OP_AND
1823 || opcode == AIC_OP_BMOV
1824 || opcode == AIC_OP_JE
1825 || opcode == AIC_OP_JNE
1826 || opcode == AIC_OP_JNZ
1827 || opcode == AIC_OP_JZ)
1831 * Make sure that we aren't attempting to write something
1832 * that hasn't been defined. If this is an and operation,
1833 * this is a mask, so "undefined" bits are okay.
1835 invalid_bits = expression->value & ~symbol->info.rinfo->valid_bitmask;
1836 if (and_op == FALSE && invalid_bits != 0) {
1837 snprintf(errbuf, sizeof(errbuf),
1838 "Invalid bit(s) 0x%x in immediate written to %s",
1839 invalid_bits, symbol->name);
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))