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#30 $
46 #include <sys/types.h>
58 #include "aicasm_symbol.h"
59 #include "aicasm_insformat.h"
63 char stock_prefix
[] = "aic_";
64 char *prefix
= stock_prefix
;
67 static char errbuf
[255];
68 static char regex_pattern
[255];
69 static symbol_t
*cur_symbol
;
70 static symbol_t
*field_symbol
;
71 static symbol_t
*scb_or_sram_symbol
;
72 static symtype cur_symtype
;
73 static symbol_ref_t accumulator
;
74 static symbol_ref_t mode_ptr
;
75 static symbol_ref_t allones
;
76 static symbol_ref_t allzeros
;
77 static symbol_ref_t none
;
78 static symbol_ref_t sindex
;
79 static int instruction_ptr
;
81 static int sram_or_scb_offset
;
82 static int download_constant_count
;
83 static int in_critical_section
;
84 static u_int enum_increment
;
85 static u_int enum_next_value
;
87 static void process_field
(int field_type
, symbol_t
*sym
, int mask
);
88 static void initialize_symbol
(symbol_t
*symbol
);
89 static void add_macro_arg
(const char *argtext
, int position
);
90 static void add_macro_body
(const char *bodytext
);
91 static void process_register
(symbol_t
**p_symbol
);
92 static void format_1_instr
(int opcode
, symbol_ref_t
*dest
,
93 expression_t
*immed
, symbol_ref_t
*src
, int ret
);
94 static void format_2_instr
(int opcode
, symbol_ref_t
*dest
,
95 expression_t
*places
, symbol_ref_t
*src
, int ret
);
96 static void format_3_instr
(int opcode
, symbol_ref_t
*src
,
97 expression_t
*immed
, symbol_ref_t
*address
);
98 static void test_readable_symbol
(symbol_t
*symbol
);
99 static void test_writable_symbol
(symbol_t
*symbol
);
100 static void type_check
(symbol_ref_t
*sym
, expression_t
*expression
, int and_op
);
101 static void make_expression
(expression_t
*immed
, int value
);
102 static void add_conditional
(symbol_t
*symbol
);
103 static void add_version
(const char *verstring
);
104 static int is_download_const
(expression_t
*immed
);
105 static int is_location_address
(symbol_t
*symbol
);
106 void yyerror(const char *string);
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
146 %token T_DONT_GENERATE_DEBUG_CODE
152 %token T_SET_SRC_MODE
154 %token T_SET_DST_MODE
156 %token
<value
> T_MODE
170 %token
<value
> T_NUMBER
172 %token
<str
> T_PATH T_STRING T_ARG T_MACROBODY
176 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
178 %token
<value
> T_SHR T_SHL T_ROR T_ROL
180 %token
<value
> T_MVI T_MOV T_CLR T_BMOV
182 %token
<value
> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
184 %token
<value
> T_ADD T_ADC
186 %token
<value
> T_INC T_DEC
188 %token
<value
> T_STC T_CLC
190 %token
<value
> T_CMP T_NOT T_XOR
192 %token
<value
> T_TEST T_AND
196 /* 16 bit extensions, not implemented
197 * %token <value> T_OR16 T_AND16 T_XOR16 T_ADD16
198 * %token <value> T_ADC16 T_MVI16 T_TEST16 T_CMP16 T_CMPXCHG
204 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
208 %token
<sym
> T_SYMBOL
212 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
214 %type
<sym_ref
> reg_symbol address destination source opt_source
216 %type
<expression
> expression immediate immediate_or_a
218 %type
<value
> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
220 %type
<value
> mode_value mode_list macro_arglist
224 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
237 | program patch_arg_list
247 | program scratch_ram
253 | program set_src_mode
255 | program set_dst_mode
256 | critical_section_start
257 | program critical_section_start
258 | critical_section_end
259 | program critical_section_end
261 | program conditional
267 T_INCLUDE
'<' T_PATH
'>'
269 include_file
($3, BRACKETED_INCLUDE
);
271 | T_INCLUDE
'"' T_PATH
'"'
273 include_file
($3, QUOTED_INCLUDE
);
278 T_PREFIX
'=' T_STRING
280 if
(prefix
!= stock_prefix
)
281 stop
("Prefix multiply defined",
285 stop
("Unable to record prefix", EX_SOFTWARE
);
290 T_PATCH_ARG_LIST
'=' T_STRING
292 if
(patch_arg_list
!= NULL
)
293 stop
("Patch argument list multiply defined",
295 patch_arg_list
= strdup
($3);
296 if
(patch_arg_list
== NULL
)
297 stop
("Unable to record patch arg list", EX_SOFTWARE
);
302 T_VERSION
'=' T_STRING
307 T_REGISTER
{ cur_symtype
= REGISTER
; } reg_definition
313 if
($1->type
!= UNINITIALIZED
) {
314 stop
("Register multiply defined", EX_DATAERR
);
318 cur_symbol
->type
= cur_symtype
;
319 initialize_symbol
(cur_symbol
);
325 * Default to allowing everything in for registers
326 * with no bit or mask definitions.
328 if
(cur_symbol
->info.rinfo
->valid_bitmask
== 0)
329 cur_symbol
->info.rinfo
->valid_bitmask
= 0xFF;
331 if
(cur_symbol
->info.rinfo
->size
== 0)
332 cur_symbol
->info.rinfo
->size
= 1;
335 * This might be useful for registers too.
337 if
(cur_symbol
->type
!= REGISTER
) {
338 if
(cur_symbol
->info.rinfo
->address
== 0)
339 cur_symbol
->info.rinfo
->address
=
341 sram_or_scb_offset
+=
342 cur_symbol
->info.rinfo
->size
;
350 | reg_attribute_list reg_attribute
358 | dont_generate_debug_code
375 cur_symbol
->info.rinfo
->address
= $2;
382 cur_symbol
->info.rinfo
->size
= $2;
383 if
(scb_or_sram_symbol
!= NULL
) {
387 max_addr
= scb_or_sram_symbol
->info.rinfo
->address
388 + scb_or_sram_symbol
->info.rinfo
->size
;
389 sym_max_addr
= cur_symbol
->info.rinfo
->address
390 + cur_symbol
->info.rinfo
->size
;
392 if
(sym_max_addr
> max_addr
)
393 stop
("SCB or SRAM space exhausted", EX_DATAERR
);
401 cur_symbol
->count
+= $2;
408 cur_symbol
->info.rinfo
->mode
= $2;
412 dont_generate_debug_code:
413 T_DONT_GENERATE_DEBUG_CODE
415 cur_symbol
->dont_generate_debug_code
= 1;
422 cur_symbol
->info.rinfo
->modes
= $2;
431 | mode_list
',' mode_value
441 stop
("Valid register modes range between 0 and 4.",
453 if
(symbol
->type
!= CONST
) {
454 stop
("Only \"const\" symbols allowed in "
455 "mode definitions.", EX_DATAERR
);
458 if
(symbol
->info.cinfo
->value
> 4) {
459 stop
("Valid register modes range between 0 and 4.",
463 $$
= (0x1 << symbol
->info.cinfo
->value
);
474 '{' enum_entry_list
'}'
475 | T_FIELD T_SYMBOL expression
477 process_field
(FIELD
, $2, $3.value
);
480 enum_increment
= 0x01 << (ffs
($3.value
) - 1);
482 '{' enum_entry_list
'}'
483 | T_FIELD T_SYMBOL expression
485 process_field
(FIELD
, $2, $3.value
);
496 '{' enum_entry_list
'}'
497 | T_ENUM T_SYMBOL expression
499 process_field
(ENUM
, $2, $3.value
);
502 enum_increment
= 0x01 << (ffs
($3.value
) - 1);
504 '{' enum_entry_list
'}'
509 | enum_entry_list
',' enum_entry
515 process_field
(ENUM_ENTRY
, $1, enum_next_value
);
516 enum_next_value
+= enum_increment
;
518 | T_SYMBOL expression
520 process_field
(ENUM_ENTRY
, $1, $2.value
);
521 enum_next_value
= $2.value
+ enum_increment
;
526 T_MASK T_SYMBOL expression
528 process_field
(MASK
, $2, $3.value
);
535 if
($2->type
!= UNINITIALIZED
) {
536 stop
("Re-definition of register alias",
541 initialize_symbol
($2);
542 $2->info.ainfo
->parent
= cur_symbol
;
549 if
(accumulator.symbol
!= NULL
) {
550 stop
("Only one accumulator definition allowed",
554 accumulator.symbol
= cur_symbol
;
561 if
(mode_ptr.symbol
!= NULL
) {
562 stop
("Only one mode pointer definition allowed",
566 mode_ptr.symbol
= cur_symbol
;
573 if
(allones.symbol
!= NULL
) {
574 stop
("Only one definition of allones allowed",
578 allones.symbol
= cur_symbol
;
585 if
(allzeros.symbol
!= NULL
) {
586 stop
("Only one definition of allzeros allowed",
590 allzeros.symbol
= cur_symbol
;
597 if
(none.symbol
!= NULL
) {
598 stop
("Only one definition of none allowed",
602 none.symbol
= cur_symbol
;
609 if
(sindex.symbol
!= NULL
) {
610 stop
("Only one definition of sindex allowed",
614 sindex.symbol
= cur_symbol
;
619 expression
'|' expression
621 $$.value
= $1.value |
$3.value
;
622 symlist_merge
(&$$.referenced_syms
,
624 &$3.referenced_syms
);
626 | expression
'&' expression
628 $$.value
= $1.value
& $3.value
;
629 symlist_merge
(&$$.referenced_syms
,
631 &$3.referenced_syms
);
633 | expression
'+' expression
635 $$.value
= $1.value
+ $3.value
;
636 symlist_merge
(&$$.referenced_syms
,
638 &$3.referenced_syms
);
640 | expression
'-' expression
642 $$.value
= $1.value
- $3.value
;
643 symlist_merge
(&($$.referenced_syms
),
644 &($1.referenced_syms
),
645 &($3.referenced_syms
));
647 | expression
'*' expression
649 $$.value
= $1.value
* $3.value
;
650 symlist_merge
(&($$.referenced_syms
),
651 &($1.referenced_syms
),
652 &($3.referenced_syms
));
654 | expression
'/' expression
656 $$.value
= $1.value
/ $3.value
;
657 symlist_merge
(&($$.referenced_syms
),
658 &($1.referenced_syms
),
659 &($3.referenced_syms
));
661 | expression T_EXPR_LSHIFT expression
663 $$.value
= $1.value
<< $3.value
;
664 symlist_merge
(&$$.referenced_syms
,
666 &$3.referenced_syms
);
668 | expression T_EXPR_RSHIFT expression
670 $$.value
= $1.value
>> $3.value
;
671 symlist_merge
(&$$.referenced_syms
,
673 &$3.referenced_syms
);
682 $$.value
= (~$$.value
) & 0xFF;
684 |
'-' expression %prec UMINUS
687 $$.value
= -$$.value
;
692 SLIST_INIT
(&$$.referenced_syms
);
699 switch
(symbol
->type
) {
701 symbol
= $1->info.ainfo
->parent
;
705 $$.value
= symbol
->info.rinfo
->address
;
711 $$.value
= symbol
->info.finfo
->value
;
715 $$.value
= symbol
->info.cinfo
->value
;
720 snprintf
(errbuf
, sizeof
(errbuf
),
721 "Undefined symbol %s referenced",
723 stop
(errbuf
, EX_DATAERR
);
728 SLIST_INIT
(&$$.referenced_syms
);
729 symlist_add
(&$$.referenced_syms
, symbol
, SYMLIST_INSERT_HEAD
);
734 T_CONST T_SYMBOL expression
736 if
($2->type
!= UNINITIALIZED
) {
737 stop
("Re-definition of symbol as a constant",
742 initialize_symbol
($2);
743 $2->info.cinfo
->value
= $3.value
;
745 | T_CONST T_SYMBOL T_DOWNLOAD
748 stop
("Invalid downloaded constant declaration",
752 if
($2->type
!= UNINITIALIZED
) {
753 stop
("Re-definition of symbol as a downloaded constant",
757 $2->type
= DOWNLOAD_CONST
;
758 initialize_symbol
($2);
759 $2->info.cinfo
->value
= download_constant_count
++;
766 if
($2->type
!= UNINITIALIZED
) {
767 stop
("Re-definition of symbol as a macro",
772 cur_symbol
->type
= MACRO
;
773 initialize_symbol
(cur_symbol
);
778 macrodefn_prologue T_MACROBODY
782 | macrodefn_prologue
'(' macro_arglist
')' T_MACROBODY
785 cur_symbol
->info.macroinfo
->narg
= $3;
791 /* Macros can take no arguments */
797 add_macro_arg
($1, 0);
799 | macro_arglist
',' T_ARG
802 stop
("Comma without preceding argument in arg list",
807 add_macro_arg
($3, $1);
814 snprintf
(errbuf
, sizeof
(errbuf
), "%s%d", SRAM_SYMNAME
,
816 cur_symbol
= symtable_get
(SRAM_SYMNAME
);
817 cur_symtype
= SRAMLOC
;
818 cur_symbol
->type
= SRAMLOC
;
819 initialize_symbol
(cur_symbol
);
820 cur_symbol
->count
+= 1;
824 sram_or_scb_offset
= cur_symbol
->info.rinfo
->address
;
828 scb_or_sram_symbol
= cur_symbol
;
830 scb_or_sram_attributes
834 scb_or_sram_symbol
= NULL
;
841 cur_symbol
= symtable_get
(SCB_SYMNAME
);
842 cur_symtype
= SCBLOC
;
843 if
(cur_symbol
->type
!= UNINITIALIZED
) {
844 stop
("Only one SRAM definition allowed",
848 cur_symbol
->type
= SCBLOC
;
849 initialize_symbol
(cur_symbol
);
850 /* 64 bytes of SCB space */
851 cur_symbol
->info.rinfo
->size
= 64;
852 cur_symbol
->count
+= 1;
856 sram_or_scb_offset
= cur_symbol
->info.rinfo
->address
;
860 scb_or_sram_symbol
= cur_symbol
;
862 scb_or_sram_attributes
866 scb_or_sram_symbol
= NULL
;
870 scb_or_sram_attributes:
871 /* NULL definition is okay */
873 | scb_or_sram_reg_list
874 | modes scb_or_sram_reg_list
877 scb_or_sram_reg_list:
879 | scb_or_sram_reg_list reg_definition
885 process_register
(&$1);
889 | T_SYMBOL
'[' T_SYMBOL
']'
891 process_register
(&$1);
892 if
($3->type
!= CONST
) {
893 stop
("register offset must be a constant", EX_DATAERR
);
896 if
(($3->info.cinfo
->value
+ 1) > $1->info.rinfo
->size
) {
897 stop
("Accessing offset beyond range of register",
902 $$.offset
= $3->info.cinfo
->value
;
904 | T_SYMBOL
'[' T_NUMBER
']'
906 process_register
(&$1);
907 if
(($3 + 1) > $1->info.rinfo
->size
) {
908 stop
("Accessing offset beyond range of register",
917 if
(accumulator.symbol
== NULL
) {
918 stop
("No accumulator has been defined", EX_DATAERR
);
921 $$.symbol
= accumulator.symbol
;
929 test_writable_symbol
($1.symbol
);
942 if
($1.value
== 0 && is_download_const
(&$1) == 0) {
943 snprintf
(errbuf
, sizeof
(errbuf
),
944 "\nExpression evaluates to 0 and thus "
945 "references the accumulator.\n "
946 "If this is the desired effect, use 'A' "
948 stop
(errbuf
, EX_DATAERR
);
954 SLIST_INIT
(&$$.referenced_syms
);
955 symlist_add
(&$$.referenced_syms
, accumulator.symbol
,
956 SYMLIST_INSERT_HEAD
);
964 test_readable_symbol
($1.symbol
);
985 T_SET_SRC_MODE T_NUMBER
';'
992 T_SET_DST_MODE T_NUMBER
';'
998 critical_section_start:
1001 critical_section_t
*cs
;
1003 if
(in_critical_section
!= FALSE
) {
1004 stop
("Critical Section within Critical Section",
1009 cs
->begin_addr
= instruction_ptr
;
1010 in_critical_section
= TRUE
;
1014 critical_section_end:
1017 critical_section_t
*cs
;
1019 if
(in_critical_section
== FALSE
) {
1020 stop
("Unballanced 'end_cs'", EX_DATAERR
);
1023 cs
= TAILQ_LAST
(&cs_tailq
, cs_tailq
);
1024 cs
->end_addr
= instruction_ptr
;
1025 in_critical_section
= FALSE
;
1038 if
($2->type
!= UNINITIALIZED
) {
1039 stop
("Program label multiply defined", EX_DATAERR
);
1043 initialize_symbol
($2);
1044 $2->info.linfo
->address
= instruction_ptr
;
1045 $2->info.linfo
->exported
= $1;
1055 | T_SYMBOL
'+' T_NUMBER
1060 | T_SYMBOL
'-' T_NUMBER
1087 add_conditional
($2);
1088 new_scope
= scope_alloc
();
1089 new_scope
->type
= SCOPE_IF
;
1090 new_scope
->begin_addr
= instruction_ptr
;
1091 new_scope
->func_num
= $2->info.condinfo
->func_num
;
1093 | T_ELSE T_IF T_CEXPR
'{'
1096 scope_t
*scope_context
;
1097 scope_t
*last_scope
;
1100 * Ensure that the previous scope is either an
1101 * if or and else if.
1103 scope_context
= SLIST_FIRST
(&scope_stack
);
1104 last_scope
= TAILQ_LAST
(&scope_context
->inner_scope
,
1106 if
(last_scope
== NULL
1107 || last_scope
->type
== T_ELSE
) {
1109 stop
("'else if' without leading 'if'", EX_DATAERR
);
1112 add_conditional
($3);
1113 new_scope
= scope_alloc
();
1114 new_scope
->type
= SCOPE_ELSE_IF
;
1115 new_scope
->begin_addr
= instruction_ptr
;
1116 new_scope
->func_num
= $3->info.condinfo
->func_num
;
1121 scope_t
*scope_context
;
1122 scope_t
*last_scope
;
1125 * Ensure that the previous scope is either an
1126 * if or and else if.
1128 scope_context
= SLIST_FIRST
(&scope_stack
);
1129 last_scope
= TAILQ_LAST
(&scope_context
->inner_scope
,
1131 if
(last_scope
== NULL
1132 || last_scope
->type
== SCOPE_ELSE
) {
1134 stop
("'else' without leading 'if'", EX_DATAERR
);
1137 new_scope
= scope_alloc
();
1138 new_scope
->type
= SCOPE_ELSE
;
1139 new_scope
->begin_addr
= instruction_ptr
;
1146 scope_t
*scope_context
;
1148 scope_context
= SLIST_FIRST
(&scope_stack
);
1149 if
(scope_context
->type
== SCOPE_ROOT
) {
1150 stop
("Unexpected '}' encountered", EX_DATAERR
);
1154 scope_context
->end_addr
= instruction_ptr
;
1157 SLIST_REMOVE_HEAD
(&scope_stack
, scope_stack_links
);
1159 process_scope
(scope_context
);
1161 if
(SLIST_FIRST
(&scope_stack
) == NULL
) {
1162 stop
("Unexpected '}' encountered", EX_DATAERR
);
1169 T_AND
{ $$
= AIC_OP_AND
; }
1170 | T_XOR
{ $$
= AIC_OP_XOR
; }
1171 | T_ADD
{ $$
= AIC_OP_ADD
; }
1172 | T_ADC
{ $$
= AIC_OP_ADC
; }
1176 f1_opcode destination
',' immediate_or_a opt_source ret
';'
1178 format_1_instr
($1, &$2, &$4, &$5, $6);
1183 T_OR reg_symbol
',' immediate_or_a opt_source ret
';'
1185 format_1_instr
(AIC_OP_OR
, &$2, &$4, &$5, $6);
1190 T_INC destination opt_source ret
';'
1194 make_expression
(&immed
, 1);
1195 format_1_instr
(AIC_OP_ADD
, &$2, &immed
, &$3, $4);
1200 T_DEC destination opt_source ret
';'
1204 make_expression
(&immed
, -1);
1205 format_1_instr
(AIC_OP_ADD
, &$2, &immed
, &$3, $4);
1214 make_expression
(&immed
, -1);
1215 format_1_instr
(AIC_OP_ADD
, &none
, &immed
, &allzeros
, $2);
1217 | T_CLC T_MVI destination
',' immediate_or_a ret
';'
1219 format_1_instr
(AIC_OP_ADD
, &$3, &$5, &allzeros
, $6);
1228 make_expression
(&immed
, 1);
1229 format_1_instr
(AIC_OP_ADD
, &none
, &immed
, &allones
, $2);
1231 | T_STC destination ret
';'
1235 make_expression
(&immed
, 1);
1236 format_1_instr
(AIC_OP_ADD
, &$2, &immed
, &allones
, $3);
1241 T_BMOV destination
',' source
',' immediate ret
';'
1243 format_1_instr
(AIC_OP_BMOV
, &$2, &$6, &$4, $7);
1248 T_MOV destination
',' source ret
';'
1252 make_expression
(&immed
, 1);
1253 format_1_instr
(AIC_OP_BMOV
, &$2, &immed
, &$4, $5);
1258 T_MVI destination
',' immediate ret
';'
1261 && is_download_const
(&$4) == 0) {
1265 * Allow move immediates of 0 so that macros,
1266 * that can't know the immediate's value and
1267 * otherwise compensate, still work.
1269 make_expression
(&immed
, 1);
1270 format_1_instr
(AIC_OP_BMOV
, &$2, &immed
, &allzeros
, $5);
1272 format_1_instr
(AIC_OP_OR
, &$2, &$4, &allzeros
, $5);
1278 T_NOT destination opt_source ret
';'
1282 make_expression
(&immed
, 0xff);
1283 format_1_instr
(AIC_OP_XOR
, &$2, &immed
, &$3, $4);
1288 T_CLR destination ret
';'
1292 make_expression
(&immed
, 0xff);
1293 format_1_instr
(AIC_OP_AND
, &$2, &immed
, &allzeros
, $3);
1302 make_expression
(&immed
, 0xff);
1303 format_1_instr
(AIC_OP_AND
, &none
, &immed
, &allzeros
, $2);
1312 make_expression
(&immed
, 0xff);
1313 format_1_instr
(AIC_OP_AND
, &none
, &immed
, &allzeros
, TRUE
);
1318 * This grammar differs from the one in the aic7xxx
1319 * reference manual since the grammar listed there is
1320 * ambiguous and causes a shift/reduce conflict.
1321 * It also seems more logical as the "immediate"
1322 * argument is listed as the second arg like the
1327 T_SHL
{ $$
= AIC_OP_SHL
; }
1328 | T_SHR
{ $$
= AIC_OP_SHR
; }
1329 | T_ROL
{ $$
= AIC_OP_ROL
; }
1330 | T_ROR
{ $$
= AIC_OP_ROR
; }
1334 * 16bit opcodes, not used
1337 * T_OR16 { $$ = AIC_OP_OR16; }
1338 *| T_AND16 { $$ = AIC_OP_AND16; }
1339 *| T_XOR16 { $$ = AIC_OP_XOR16; }
1340 *| T_ADD16 { $$ = AIC_OP_ADD16; }
1341 *| T_ADC16 { $$ = AIC_OP_ADC16; }
1342 *| T_MVI16 { $$ = AIC_OP_MVI16; }
1347 f2_opcode destination
',' expression opt_source ret
';'
1349 format_2_instr
($1, &$2, &$4, &$5, $6);
1354 T_JMP
{ $$
= AIC_OP_JMP
; }
1355 | T_JC
{ $$
= AIC_OP_JC
; }
1356 | T_JNC
{ $$
= AIC_OP_JNC
; }
1357 | T_CALL
{ $$
= AIC_OP_CALL
; }
1361 T_JZ
{ $$
= AIC_OP_JZ
; }
1362 | T_JNZ
{ $$
= AIC_OP_JNZ
; }
1366 T_JE
{ $$
= AIC_OP_JE
; }
1367 | T_JNE
{ $$
= AIC_OP_JNE
; }
1371 jmp_jc_jnc_call address
';'
1375 make_expression
(&immed
, 0);
1376 format_3_instr
($1, &sindex
, &immed
, &$2);
1381 T_OR reg_symbol
',' immediate jmp_jc_jnc_call address
';'
1383 type_check
(&$2, &$4, AIC_OP_OR
);
1384 format_3_instr
($5, &$2, &$4, &$6);
1389 T_TEST source
',' immediate_or_a jz_jnz address
';'
1391 format_3_instr
($5, &$2, &$4, &$6);
1396 T_CMP source
',' immediate_or_a je_jne address
';'
1398 format_3_instr
($5, &$2, &$4, &$6);
1403 T_MOV source jmp_jc_jnc_call address
';'
1407 make_expression
(&immed
, 0);
1408 format_3_instr
($3, &$2, &immed
, &$4);
1413 T_MVI immediate jmp_jc_jnc_call address
';'
1415 format_3_instr
($3, &allzeros
, &$2, &$4);
1422 process_field
(int field_type
, symbol_t
*sym
, int value
)
1425 * Add the current register to its
1426 * symbol list, if it already exists,
1427 * warn if we are setting it to a
1428 * different value, or in the bit to
1429 * the "allowed bits" of this register.
1431 if
(sym
->type
== UNINITIALIZED
) {
1432 sym
->type
= field_type
;
1433 initialize_symbol
(sym
);
1434 sym
->info.finfo
->value
= value
;
1435 if
(field_type
!= ENUM_ENTRY
) {
1436 if
(field_type
!= MASK
&& value
== 0) {
1437 stop
("Empty Field, or Enum", EX_DATAERR
);
1440 sym
->info.finfo
->value
= value
;
1441 sym
->info.finfo
->mask
= value
;
1442 } else if
(field_symbol
!= NULL
) {
1443 sym
->info.finfo
->mask
= field_symbol
->info.finfo
->value
;
1445 sym
->info.finfo
->mask
= 0xFF;
1447 } else if
(sym
->type
!= field_type
) {
1448 stop
("Field definition mirrors a definition of the same "
1449 " name, but a different type", EX_DATAERR
);
1451 } else if
(value
!= sym
->info.finfo
->value
) {
1452 stop
("Field redefined with a conflicting value", EX_DATAERR
);
1455 /* Fail if this symbol is already listed */
1456 if
(symlist_search
(&(sym
->info.finfo
->symrefs
),
1457 cur_symbol
->name
) != NULL
) {
1458 stop
("Field defined multiple times for register", EX_DATAERR
);
1461 symlist_add
(&(sym
->info.finfo
->symrefs
), cur_symbol
,
1462 SYMLIST_INSERT_HEAD
);
1463 cur_symbol
->info.rinfo
->valid_bitmask |
= sym
->info.finfo
->mask
;
1464 cur_symbol
->info.rinfo
->typecheck_masks
= TRUE
;
1465 symlist_add
(&(cur_symbol
->info.rinfo
->fields
), sym
, SYMLIST_SORT
);
1469 initialize_symbol
(symbol_t
*symbol
)
1471 switch
(symbol
->type
) {
1473 stop
("Call to initialize_symbol with type field unset",
1480 symbol
->info.rinfo
=
1481 (struct reg_info
*)malloc
(sizeof
(struct reg_info
));
1482 if
(symbol
->info.rinfo
== NULL
) {
1483 stop
("Can't create register info", EX_SOFTWARE
);
1486 memset
(symbol
->info.rinfo
, 0,
1487 sizeof
(struct reg_info
));
1488 SLIST_INIT
(&(symbol
->info.rinfo
->fields
));
1490 * Default to allowing access in all register modes
1491 * or to the mode specified by the SCB or SRAM space
1494 if
(scb_or_sram_symbol
!= NULL
)
1495 symbol
->info.rinfo
->modes
=
1496 scb_or_sram_symbol
->info.rinfo
->modes
;
1498 symbol
->info.rinfo
->modes
= ~
0;
1501 symbol
->info.ainfo
=
1502 (struct alias_info
*)malloc
(sizeof
(struct alias_info
));
1503 if
(symbol
->info.ainfo
== NULL
) {
1504 stop
("Can't create alias info", EX_SOFTWARE
);
1507 memset
(symbol
->info.ainfo
, 0,
1508 sizeof
(struct alias_info
));
1514 symbol
->info.finfo
=
1515 (struct field_info
*)malloc
(sizeof
(struct field_info
));
1516 if
(symbol
->info.finfo
== NULL
) {
1517 stop
("Can't create field info", EX_SOFTWARE
);
1520 memset
(symbol
->info.finfo
, 0, sizeof
(struct field_info
));
1521 SLIST_INIT
(&(symbol
->info.finfo
->symrefs
));
1524 case DOWNLOAD_CONST
:
1525 symbol
->info.cinfo
=
1526 (struct const_info
*)malloc
(sizeof
(struct const_info
));
1527 if
(symbol
->info.cinfo
== NULL
) {
1528 stop
("Can't create alias info", EX_SOFTWARE
);
1531 memset
(symbol
->info.cinfo
, 0,
1532 sizeof
(struct const_info
));
1535 symbol
->info.linfo
=
1536 (struct label_info
*)malloc
(sizeof
(struct label_info
));
1537 if
(symbol
->info.linfo
== NULL
) {
1538 stop
("Can't create label info", EX_SOFTWARE
);
1541 memset
(symbol
->info.linfo
, 0,
1542 sizeof
(struct label_info
));
1545 symbol
->info.condinfo
=
1546 (struct cond_info
*)malloc
(sizeof
(struct cond_info
));
1547 if
(symbol
->info.condinfo
== NULL
) {
1548 stop
("Can't create conditional info", EX_SOFTWARE
);
1551 memset
(symbol
->info.condinfo
, 0,
1552 sizeof
(struct cond_info
));
1555 symbol
->info.macroinfo
=
1556 (struct macro_info
*)malloc
(sizeof
(struct macro_info
));
1557 if
(symbol
->info.macroinfo
== NULL
) {
1558 stop
("Can't create macro info", EX_SOFTWARE
);
1561 memset
(symbol
->info.macroinfo
, 0,
1562 sizeof
(struct macro_info
));
1563 STAILQ_INIT
(&symbol
->info.macroinfo
->args
);
1566 stop
("Call to initialize_symbol with invalid symbol type",
1574 add_macro_arg
(const char *argtext
, int argnum
)
1576 struct macro_arg
*marg
;
1580 if
(cur_symbol
== NULL || cur_symbol
->type
!= MACRO
) {
1581 stop
("Invalid current symbol for adding macro arg",
1586 marg
= (struct macro_arg
*)malloc
(sizeof
(*marg
));
1588 stop
("Can't create macro_arg structure", EX_SOFTWARE
);
1591 marg
->replacement_text
= NULL
;
1592 retval
= snprintf
(regex_pattern
, sizeof
(regex_pattern
),
1593 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1595 if
(retval
>= sizeof
(regex_pattern
)) {
1596 stop
("Regex text buffer too small for arg",
1600 retval
= regcomp
(&marg
->arg_regex
, regex_pattern
, REG_EXTENDED
);
1602 stop
("Regex compilation failed", EX_SOFTWARE
);
1605 STAILQ_INSERT_TAIL
(&cur_symbol
->info.macroinfo
->args
, marg
, links
);
1609 add_macro_body
(const char *bodytext
)
1611 if
(cur_symbol
== NULL || cur_symbol
->type
!= MACRO
) {
1612 stop
("Invalid current symbol for adding macro arg",
1616 cur_symbol
->info.macroinfo
->body
= strdup
(bodytext
);
1617 if
(cur_symbol
->info.macroinfo
->body
== NULL
) {
1618 stop
("Can't duplicate macro body text", EX_SOFTWARE
);
1624 process_register
(symbol_t
**p_symbol
)
1626 symbol_t
*symbol
= *p_symbol
;
1628 if
(symbol
->type
== UNINITIALIZED
) {
1629 snprintf
(errbuf
, sizeof
(errbuf
), "Undefined register %s",
1631 stop
(errbuf
, EX_DATAERR
);
1633 } else if
(symbol
->type
== ALIAS
) {
1634 *p_symbol
= symbol
->info.ainfo
->parent
;
1635 } else if
((symbol
->type
!= REGISTER
)
1636 && (symbol
->type
!= SCBLOC
)
1637 && (symbol
->type
!= SRAMLOC
)) {
1638 snprintf
(errbuf
, sizeof
(errbuf
),
1639 "Specified symbol %s is not a register",
1641 stop
(errbuf
, EX_DATAERR
);
1646 format_1_instr
(int opcode
, symbol_ref_t
*dest
, expression_t
*immed
,
1647 symbol_ref_t
*src
, int ret
)
1649 struct instruction
*instr
;
1650 struct ins_format1
*f1_instr
;
1652 if
(src
->symbol
== NULL
)
1655 /* Test register permissions */
1656 test_writable_symbol
(dest
->symbol
);
1657 test_readable_symbol
(src
->symbol
);
1659 if
(!is_location_address
(dest
->symbol
)) {
1660 /* Ensure that immediate makes sense for this destination */
1661 type_check
(dest
, immed
, opcode
);
1664 /* Allocate sequencer space for the instruction and fill it out */
1665 instr
= seq_alloc
();
1666 f1_instr
= &instr
->format.format1
;
1667 f1_instr
->ret
= ret ?
1 : 0;
1668 f1_instr
->opcode
= opcode
;
1669 f1_instr
->destination
= dest
->symbol
->info.rinfo
->address
1671 f1_instr
->source
= src
->symbol
->info.rinfo
->address
1673 f1_instr
->immediate
= immed
->value
;
1675 if
(is_download_const
(immed
))
1676 f1_instr
->parity
= 1;
1677 else if
(dest
->symbol
== mode_ptr.symbol
) {
1682 * Attempt to update mode information if
1683 * we are operating on the mode register.
1685 if
(src
->symbol
== allones.symbol
)
1687 else if
(src
->symbol
== allzeros.symbol
)
1689 else if
(src
->symbol
== mode_ptr.symbol
)
1690 src_value
= (dst_mode
<< 4) | src_mode
;
1696 dst_value
= src_value
& immed
->value
;
1699 dst_value
= src_value ^ immed
->value
;
1702 dst_value
= (src_value
+ immed
->value
) & 0xFF;
1705 dst_value
= src_value | immed
->value
;
1708 dst_value
= src_value
;
1713 src_mode
= dst_value
& 0xF;
1714 dst_mode
= (dst_value
>> 4) & 0xF;
1718 symlist_free
(&immed
->referenced_syms
);
1723 format_2_instr
(int opcode
, symbol_ref_t
*dest
, expression_t
*places
,
1724 symbol_ref_t
*src
, int ret
)
1726 struct instruction
*instr
;
1727 struct ins_format2
*f2_instr
;
1728 uint8_t shift_control
;
1730 if
(src
->symbol
== NULL
)
1733 /* Test register permissions */
1734 test_writable_symbol
(dest
->symbol
);
1735 test_readable_symbol
(src
->symbol
);
1737 /* Allocate sequencer space for the instruction and fill it out */
1738 instr
= seq_alloc
();
1739 f2_instr
= &instr
->format.format2
;
1740 f2_instr
->ret
= ret ?
1 : 0;
1741 f2_instr
->opcode
= AIC_OP_ROL
;
1742 f2_instr
->destination
= dest
->symbol
->info.rinfo
->address
1744 f2_instr
->source
= src
->symbol
->info.rinfo
->address
1746 if
(places
->value
> 8 || places
->value
<= 0) {
1747 stop
("illegal shift value", EX_DATAERR
);
1752 if
(places
->value
== 8)
1753 shift_control
= 0xf0;
1755 shift_control
= (places
->value
<< 4) | places
->value
;
1758 if
(places
->value
== 8) {
1759 shift_control
= 0xf8;
1761 shift_control
= (places
->value
<< 4)
1762 |
(8 - places
->value
)
1767 shift_control
= places
->value
& 0x7;
1770 shift_control
= (8 - places
->value
) |
0x08;
1773 shift_control
= 0; /* Quiet Compiler */
1774 stop
("Invalid shift operation specified", EX_SOFTWARE
);
1778 f2_instr
->shift_control
= shift_control
;
1779 symlist_free
(&places
->referenced_syms
);
1784 format_3_instr
(int opcode
, symbol_ref_t
*src
,
1785 expression_t
*immed
, symbol_ref_t
*address
)
1787 struct instruction
*instr
;
1788 struct ins_format3
*f3_instr
;
1791 /* Test register permissions */
1792 test_readable_symbol
(src
->symbol
);
1794 /* Allocate sequencer space for the instruction and fill it out */
1795 instr
= seq_alloc
();
1796 f3_instr
= &instr
->format.format3
;
1797 if
(address
->symbol
== NULL
) {
1798 /* 'dot' reference. Use the current instruction pointer */
1799 addr
= instruction_ptr
+ address
->offset
;
1800 } else if
(address
->symbol
->type
== UNINITIALIZED
) {
1801 /* forward reference */
1802 addr
= address
->offset
;
1803 instr
->patch_label
= address
->symbol
;
1805 addr
= address
->symbol
->info.linfo
->address
+ address
->offset
;
1806 f3_instr
->opcode
= opcode
;
1807 f3_instr
->address
= addr
;
1808 f3_instr
->source
= src
->symbol
->info.rinfo
->address
1810 f3_instr
->immediate
= immed
->value
;
1812 if
(is_download_const
(immed
))
1813 f3_instr
->parity
= 1;
1815 symlist_free
(&immed
->referenced_syms
);
1820 test_readable_symbol
(symbol_t
*symbol
)
1822 if
((symbol
->info.rinfo
->modes
& (0x1 << src_mode
)) == 0) {
1823 snprintf
(errbuf
, sizeof
(errbuf
),
1824 "Register %s unavailable in source reg mode %d",
1825 symbol
->name
, src_mode
);
1826 stop
(errbuf
, EX_DATAERR
);
1829 if
(symbol
->info.rinfo
->mode
== WO
) {
1830 stop
("Write Only register specified as source",
1837 test_writable_symbol
(symbol_t
*symbol
)
1839 if
((symbol
->info.rinfo
->modes
& (0x1 << dst_mode
)) == 0) {
1840 snprintf
(errbuf
, sizeof
(errbuf
),
1841 "Register %s unavailable in destination reg mode %d",
1842 symbol
->name
, dst_mode
);
1843 stop
(errbuf
, EX_DATAERR
);
1846 if
(symbol
->info.rinfo
->mode
== RO
) {
1847 stop
("Read Only register specified as destination",
1854 type_check
(symbol_ref_t
*sym
, expression_t
*expression
, int opcode
)
1856 symbol_t
*symbol
= sym
->symbol
;
1857 symbol_node_t
*node
;
1863 * Make sure that we aren't attempting to write something
1864 * that hasn't been defined. If this is an and operation,
1865 * this is a mask, so "undefined" bits are okay.
1867 if
(opcode
== AIC_OP_AND || opcode
== AIC_OP_JNZ ||
1868 opcode
== AIC_OP_JZ || opcode
== AIC_OP_JNE ||
1869 opcode
== AIC_OP_BMOV
)
1873 * Defaulting to 8 bit logic
1875 mask
= (int8_t)~symbol
->info.rinfo
->valid_bitmask
;
1876 value
= (int8_t)expression
->value
;
1878 if
(and_op
== FALSE
&& (mask
& value
) != 0 ) {
1879 snprintf
(errbuf
, sizeof
(errbuf
),
1880 "Invalid bit(s) 0x%x in immediate written to %s",
1883 stop
(errbuf
, EX_DATAERR
);
1888 * Now make sure that all of the symbols referenced by the
1889 * expression are defined for this register.
1891 if
(symbol
->info.rinfo
->typecheck_masks
!= FALSE
) {
1892 for
(node
= expression
->referenced_syms.slh_first
;
1894 node
= node
->links.sle_next
) {
1895 if
((node
->symbol
->type
== MASK
1896 || node
->symbol
->type
== FIELD
1897 || node
->symbol
->type
== ENUM
1898 || node
->symbol
->type
== ENUM_ENTRY
)
1899 && symlist_search
(&node
->symbol
->info.finfo
->symrefs
,
1900 symbol
->name
) == NULL
) {
1901 snprintf
(errbuf
, sizeof
(errbuf
),
1902 "Invalid field or mask %s "
1904 node
->symbol
->name
, symbol
->name
);
1905 stop
(errbuf
, EX_DATAERR
);
1913 make_expression
(expression_t
*immed
, int value
)
1915 SLIST_INIT
(&immed
->referenced_syms
);
1916 immed
->value
= value
& 0xff;
1920 add_conditional
(symbol_t
*symbol
)
1922 static int numfuncs
;
1924 if
(numfuncs
== 0) {
1925 /* add a special conditional, "0" */
1926 symbol_t
*false_func
;
1928 false_func
= symtable_get
("0");
1929 if
(false_func
->type
!= UNINITIALIZED
) {
1930 stop
("Conditional expression '0' "
1931 "conflicts with a symbol", EX_DATAERR
);
1934 false_func
->type
= CONDITIONAL
;
1935 initialize_symbol
(false_func
);
1936 false_func
->info.condinfo
->func_num
= numfuncs
++;
1937 symlist_add
(&patch_functions
, false_func
, SYMLIST_INSERT_HEAD
);
1940 /* This condition has occurred before */
1941 if
(symbol
->type
== CONDITIONAL
)
1944 if
(symbol
->type
!= UNINITIALIZED
) {
1945 stop
("Conditional expression conflicts with a symbol",
1950 symbol
->type
= CONDITIONAL
;
1951 initialize_symbol
(symbol
);
1952 symbol
->info.condinfo
->func_num
= numfuncs
++;
1953 symlist_add
(&patch_functions
, symbol
, SYMLIST_INSERT_HEAD
);
1957 add_version
(const char *verstring
)
1959 const char prefix
[] = " * ";
1963 newlen
= strlen
(verstring
) + strlen
(prefix
);
1965 if
(versions
!= NULL
)
1966 oldlen
= strlen
(versions
);
1967 versions
= realloc
(versions
, newlen
+ oldlen
+ 2);
1968 if
(versions
== NULL
)
1969 stop
("Can't allocate version string", EX_SOFTWARE
);
1970 strcpy
(&versions
[oldlen
], prefix
);
1971 strcpy
(&versions
[oldlen
+ strlen
(prefix
)], verstring
);
1972 versions
[newlen
+ oldlen
] = '\n';
1973 versions
[newlen
+ oldlen
+ 1] = '\0';
1977 yyerror(const char *string)
1979 stop
(string, EX_DATAERR
);
1983 is_download_const
(expression_t
*immed
)
1985 if
((immed
->referenced_syms.slh_first
!= NULL
)
1986 && (immed
->referenced_syms.slh_first
->symbol
->type
== DOWNLOAD_CONST
))
1993 is_location_address
(symbol_t
*sym
)
1995 if
(sym
->type
== SCBLOC ||
1996 sym
->type
== SRAMLOC
)