WIP FPC-III support
[linux/fpc-iii.git] / drivers / scsi / aic7xxx / aicasm / aicasm_gram.y
blob924d55a8acbfced66e23f8851f182b8e2c3c7439
1 %{
2 /*
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.
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
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.
28 * NO WARRANTY
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 $
43 * $FreeBSD$
46 #include <sys/types.h>
48 #include <inttypes.h>
49 #include <regex.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sysexits.h>
55 #include "../queue.h"
57 #include "aicasm.h"
58 #include "aicasm_symbol.h"
59 #include "aicasm_insformat.h"
61 int yylineno;
62 char *yyfilename;
63 char stock_prefix[] = "aic_";
64 char *prefix = stock_prefix;
65 char *patch_arg_list;
66 char *versions;
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;
80 static int num_srams;
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"
112 %union {
113 u_int value;
114 char *str;
115 symbol_t *sym;
116 symbol_ref_t sym_ref;
117 expression_t expression;
120 %token T_REGISTER
122 %token <value> T_CONST
124 %token T_EXPORT
126 %token T_DOWNLOAD
128 %token T_SCB
130 %token T_SRAM
132 %token T_ALIAS
134 %token T_SIZE
136 %token T_EXPR_LSHIFT
138 %token T_EXPR_RSHIFT
140 %token <value> T_ADDRESS
142 %token T_COUNT
144 %token T_ACCESS_MODE
146 %token T_DONT_GENERATE_DEBUG_CODE
148 %token T_MODES
150 %token T_DEFINE
152 %token T_SET_SRC_MODE
154 %token T_SET_DST_MODE
156 %token <value> T_MODE
158 %token T_BEGIN_CS
160 %token T_END_CS
162 %token T_PAD_PAGE
164 %token T_FIELD
166 %token T_ENUM
168 %token T_MASK
170 %token <value> T_NUMBER
172 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
174 %token <sym> T_CEXPR
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
194 %token <value> T_OR
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
200 %token T_RET
202 %token T_NOP
204 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
206 %token T_A
208 %token <sym> T_SYMBOL
210 %token T_NL
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
222 %left '|'
223 %left '&'
224 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
225 %left '+' '-'
226 %left '*' '/'
227 %right '~'
228 %nonassoc UMINUS
231 program:
232 include
233 | program include
234 | prefix
235 | program prefix
236 | patch_arg_list
237 | program patch_arg_list
238 | version
239 | program version
240 | register
241 | program register
242 | constant
243 | program constant
244 | macrodefn
245 | program macrodefn
246 | scratch_ram
247 | program scratch_ram
248 | scb
249 | program scb
250 | label
251 | program label
252 | set_src_mode
253 | program set_src_mode
254 | set_dst_mode
255 | program set_dst_mode
256 | critical_section_start
257 | program critical_section_start
258 | critical_section_end
259 | program critical_section_end
260 | conditional
261 | program conditional
262 | code
263 | program code
266 include:
267 T_INCLUDE '<' T_PATH '>'
269 include_file($3, BRACKETED_INCLUDE);
271 | T_INCLUDE '"' T_PATH '"'
273 include_file($3, QUOTED_INCLUDE);
277 prefix:
278 T_PREFIX '=' T_STRING
280 if (prefix != stock_prefix)
281 stop("Prefix multiply defined",
282 EX_DATAERR);
283 prefix = strdup($3);
284 if (prefix == NULL)
285 stop("Unable to record prefix", EX_SOFTWARE);
289 patch_arg_list:
290 T_PATCH_ARG_LIST '=' T_STRING
292 if (patch_arg_list != NULL)
293 stop("Patch argument list multiply defined",
294 EX_DATAERR);
295 patch_arg_list = strdup($3);
296 if (patch_arg_list == NULL)
297 stop("Unable to record patch arg list", EX_SOFTWARE);
301 version:
302 T_VERSION '=' T_STRING
303 { add_version($3); }
306 register:
307 T_REGISTER { cur_symtype = REGISTER; } reg_definition
310 reg_definition:
311 T_SYMBOL '{'
313 if ($1->type != UNINITIALIZED) {
314 stop("Register multiply defined", EX_DATAERR);
315 /* NOTREACHED */
317 cur_symbol = $1;
318 cur_symbol->type = cur_symtype;
319 initialize_symbol(cur_symbol);
321 reg_attribute_list
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 =
340 sram_or_scb_offset;
341 sram_or_scb_offset +=
342 cur_symbol->info.rinfo->size;
344 cur_symbol = NULL;
348 reg_attribute_list:
349 reg_attribute
350 | reg_attribute_list reg_attribute
353 reg_attribute:
354 reg_address
355 | size
356 | count
357 | access_mode
358 | dont_generate_debug_code
359 | modes
360 | field_defn
361 | enum_defn
362 | mask_defn
363 | alias
364 | accumulator
365 | mode_pointer
366 | allones
367 | allzeros
368 | none
369 | sindex
372 reg_address:
373 T_ADDRESS T_NUMBER
375 cur_symbol->info.rinfo->address = $2;
379 size:
380 T_SIZE T_NUMBER
382 cur_symbol->info.rinfo->size = $2;
383 if (scb_or_sram_symbol != NULL) {
384 u_int max_addr;
385 u_int sym_max_addr;
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);
398 count:
399 T_COUNT T_NUMBER
401 cur_symbol->count += $2;
405 access_mode:
406 T_ACCESS_MODE T_MODE
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;
419 modes:
420 T_MODES mode_list
422 cur_symbol->info.rinfo->modes = $2;
426 mode_list:
427 mode_value
429 $$ = $1;
431 | mode_list ',' mode_value
433 $$ = $1 | $3;
437 mode_value:
438 T_NUMBER
440 if ($1 > 4) {
441 stop("Valid register modes range between 0 and 4.",
442 EX_DATAERR);
443 /* NOTREACHED */
446 $$ = (0x1 << $1);
448 | T_SYMBOL
450 symbol_t *symbol;
452 symbol = $1;
453 if (symbol->type != CONST) {
454 stop("Only \"const\" symbols allowed in "
455 "mode definitions.", EX_DATAERR);
456 /* NOTREACHED */
458 if (symbol->info.cinfo->value > 4) {
459 stop("Valid register modes range between 0 and 4.",
460 EX_DATAERR);
461 /* NOTREACHED */
463 $$ = (0x1 << symbol->info.cinfo->value);
467 field_defn:
468 T_FIELD
470 field_symbol = NULL;
471 enum_next_value = 0;
472 enum_increment = 1;
474 '{' enum_entry_list '}'
475 | T_FIELD T_SYMBOL expression
477 process_field(FIELD, $2, $3.value);
478 field_symbol = $2;
479 enum_next_value = 0;
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);
489 enum_defn:
490 T_ENUM
492 field_symbol = NULL;
493 enum_next_value = 0;
494 enum_increment = 1;
496 '{' enum_entry_list '}'
497 | T_ENUM T_SYMBOL expression
499 process_field(ENUM, $2, $3.value);
500 field_symbol = $2;
501 enum_next_value = 0;
502 enum_increment = 0x01 << (ffs($3.value) - 1);
504 '{' enum_entry_list '}'
507 enum_entry_list:
508 enum_entry
509 | enum_entry_list ',' enum_entry
512 enum_entry:
513 T_SYMBOL
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;
525 mask_defn:
526 T_MASK T_SYMBOL expression
528 process_field(MASK, $2, $3.value);
532 alias:
533 T_ALIAS T_SYMBOL
535 if ($2->type != UNINITIALIZED) {
536 stop("Re-definition of register alias",
537 EX_DATAERR);
538 /* NOTREACHED */
540 $2->type = ALIAS;
541 initialize_symbol($2);
542 $2->info.ainfo->parent = cur_symbol;
546 accumulator:
547 T_ACCUM
549 if (accumulator.symbol != NULL) {
550 stop("Only one accumulator definition allowed",
551 EX_DATAERR);
552 /* NOTREACHED */
554 accumulator.symbol = cur_symbol;
558 mode_pointer:
559 T_MODE_PTR
561 if (mode_ptr.symbol != NULL) {
562 stop("Only one mode pointer definition allowed",
563 EX_DATAERR);
564 /* NOTREACHED */
566 mode_ptr.symbol = cur_symbol;
570 allones:
571 T_ALLONES
573 if (allones.symbol != NULL) {
574 stop("Only one definition of allones allowed",
575 EX_DATAERR);
576 /* NOTREACHED */
578 allones.symbol = cur_symbol;
582 allzeros:
583 T_ALLZEROS
585 if (allzeros.symbol != NULL) {
586 stop("Only one definition of allzeros allowed",
587 EX_DATAERR);
588 /* NOTREACHED */
590 allzeros.symbol = cur_symbol;
594 none:
595 T_NONE
597 if (none.symbol != NULL) {
598 stop("Only one definition of none allowed",
599 EX_DATAERR);
600 /* NOTREACHED */
602 none.symbol = cur_symbol;
606 sindex:
607 T_SINDEX
609 if (sindex.symbol != NULL) {
610 stop("Only one definition of sindex allowed",
611 EX_DATAERR);
612 /* NOTREACHED */
614 sindex.symbol = cur_symbol;
618 expression:
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 '-' 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,
665 &$1.referenced_syms,
666 &$3.referenced_syms);
668 | expression T_EXPR_RSHIFT expression
670 $$.value = $1.value >> $3.value;
671 symlist_merge(&$$.referenced_syms,
672 &$1.referenced_syms,
673 &$3.referenced_syms);
675 | '(' expression ')'
677 $$ = $2;
679 | '~' expression
681 $$ = $2;
682 $$.value = (~$$.value) & 0xFF;
684 | '-' expression %prec UMINUS
686 $$ = $2;
687 $$.value = -$$.value;
689 | T_NUMBER
691 $$.value = $1;
692 SLIST_INIT(&$$.referenced_syms);
694 | T_SYMBOL
696 symbol_t *symbol;
698 symbol = $1;
699 switch (symbol->type) {
700 case ALIAS:
701 symbol = $1->info.ainfo->parent;
702 case REGISTER:
703 case SCBLOC:
704 case SRAMLOC:
705 $$.value = symbol->info.rinfo->address;
706 break;
707 case MASK:
708 case FIELD:
709 case ENUM:
710 case ENUM_ENTRY:
711 $$.value = symbol->info.finfo->value;
712 break;
713 case DOWNLOAD_CONST:
714 case CONST:
715 $$.value = symbol->info.cinfo->value;
716 break;
717 case UNINITIALIZED:
718 default:
720 snprintf(errbuf, sizeof(errbuf),
721 "Undefined symbol %s referenced",
722 symbol->name);
723 stop(errbuf, EX_DATAERR);
724 /* NOTREACHED */
725 break;
728 SLIST_INIT(&$$.referenced_syms);
729 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
733 constant:
734 T_CONST T_SYMBOL expression
736 if ($2->type != UNINITIALIZED) {
737 stop("Re-definition of symbol as a constant",
738 EX_DATAERR);
739 /* NOTREACHED */
741 $2->type = CONST;
742 initialize_symbol($2);
743 $2->info.cinfo->value = $3.value;
745 | T_CONST T_SYMBOL T_DOWNLOAD
747 if ($1) {
748 stop("Invalid downloaded constant declaration",
749 EX_DATAERR);
750 /* NOTREACHED */
752 if ($2->type != UNINITIALIZED) {
753 stop("Re-definition of symbol as a downloaded constant",
754 EX_DATAERR);
755 /* NOTREACHED */
757 $2->type = DOWNLOAD_CONST;
758 initialize_symbol($2);
759 $2->info.cinfo->value = download_constant_count++;
763 macrodefn_prologue:
764 T_DEFINE T_SYMBOL
766 if ($2->type != UNINITIALIZED) {
767 stop("Re-definition of symbol as a macro",
768 EX_DATAERR);
769 /* NOTREACHED */
771 cur_symbol = $2;
772 cur_symbol->type = MACRO;
773 initialize_symbol(cur_symbol);
777 macrodefn:
778 macrodefn_prologue T_MACROBODY
780 add_macro_body($2);
782 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
784 add_macro_body($5);
785 cur_symbol->info.macroinfo->narg = $3;
789 macro_arglist:
791 /* Macros can take no arguments */
792 $$ = 0;
794 | T_ARG
796 $$ = 1;
797 add_macro_arg($1, 0);
799 | macro_arglist ',' T_ARG
801 if ($1 == 0) {
802 stop("Comma without preceding argument in arg list",
803 EX_DATAERR);
804 /* NOTREACHED */
806 $$ = $1 + 1;
807 add_macro_arg($3, $1);
811 scratch_ram:
812 T_SRAM '{'
814 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
815 num_srams);
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;
822 reg_address
824 sram_or_scb_offset = cur_symbol->info.rinfo->address;
826 size
828 scb_or_sram_symbol = cur_symbol;
830 scb_or_sram_attributes
833 cur_symbol = NULL;
834 scb_or_sram_symbol = NULL;
838 scb:
839 T_SCB '{'
841 cur_symbol = symtable_get(SCB_SYMNAME);
842 cur_symtype = SCBLOC;
843 if (cur_symbol->type != UNINITIALIZED) {
844 stop("Only one SRAM definition allowed",
845 EX_SOFTWARE);
846 /* NOTREACHED */
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;
854 reg_address
856 sram_or_scb_offset = cur_symbol->info.rinfo->address;
858 size
860 scb_or_sram_symbol = cur_symbol;
862 scb_or_sram_attributes
865 cur_symbol = NULL;
866 scb_or_sram_symbol = NULL;
870 scb_or_sram_attributes:
871 /* NULL definition is okay */
872 | modes
873 | scb_or_sram_reg_list
874 | modes scb_or_sram_reg_list
877 scb_or_sram_reg_list:
878 reg_definition
879 | scb_or_sram_reg_list reg_definition
882 reg_symbol:
883 T_SYMBOL
885 process_register(&$1);
886 $$.symbol = $1;
887 $$.offset = 0;
889 | T_SYMBOL '[' T_SYMBOL ']'
891 process_register(&$1);
892 if ($3->type != CONST) {
893 stop("register offset must be a constant", EX_DATAERR);
894 /* NOTREACHED */
896 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
897 stop("Accessing offset beyond range of register",
898 EX_DATAERR);
899 /* NOTREACHED */
901 $$.symbol = $1;
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",
909 EX_DATAERR);
910 /* NOTREACHED */
912 $$.symbol = $1;
913 $$.offset = $3;
915 | T_A
917 if (accumulator.symbol == NULL) {
918 stop("No accumulator has been defined", EX_DATAERR);
919 /* NOTREACHED */
921 $$.symbol = accumulator.symbol;
922 $$.offset = 0;
926 destination:
927 reg_symbol
929 test_writable_symbol($1.symbol);
930 $$ = $1;
934 immediate:
935 expression
936 { $$ = $1; }
939 immediate_or_a:
940 expression
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' "
947 "instead.\n");
948 stop(errbuf, EX_DATAERR);
950 $$ = $1;
952 | T_A
954 SLIST_INIT(&$$.referenced_syms);
955 symlist_add(&$$.referenced_syms, accumulator.symbol,
956 SYMLIST_INSERT_HEAD);
957 $$.value = 0;
961 source:
962 reg_symbol
964 test_readable_symbol($1.symbol);
965 $$ = $1;
969 opt_source:
971 $$.symbol = NULL;
972 $$.offset = 0;
974 | ',' source
975 { $$ = $2; }
978 ret:
979 { $$ = 0; }
980 | T_RET
981 { $$ = 1; }
984 set_src_mode:
985 T_SET_SRC_MODE T_NUMBER ';'
987 src_mode = $2;
991 set_dst_mode:
992 T_SET_DST_MODE T_NUMBER ';'
994 dst_mode = $2;
998 critical_section_start:
999 T_BEGIN_CS ';'
1001 critical_section_t *cs;
1003 if (in_critical_section != FALSE) {
1004 stop("Critical Section within Critical Section",
1005 EX_DATAERR);
1006 /* NOTREACHED */
1008 cs = cs_alloc();
1009 cs->begin_addr = instruction_ptr;
1010 in_critical_section = TRUE;
1014 critical_section_end:
1015 T_END_CS ';'
1017 critical_section_t *cs;
1019 if (in_critical_section == FALSE) {
1020 stop("Unballanced 'end_cs'", EX_DATAERR);
1021 /* NOTREACHED */
1023 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1024 cs->end_addr = instruction_ptr;
1025 in_critical_section = FALSE;
1029 export:
1030 { $$ = 0; }
1031 | T_EXPORT
1032 { $$ = 1; }
1035 label:
1036 export T_SYMBOL ':'
1038 if ($2->type != UNINITIALIZED) {
1039 stop("Program label multiply defined", EX_DATAERR);
1040 /* NOTREACHED */
1042 $2->type = LABEL;
1043 initialize_symbol($2);
1044 $2->info.linfo->address = instruction_ptr;
1045 $2->info.linfo->exported = $1;
1049 address:
1050 T_SYMBOL
1052 $$.symbol = $1;
1053 $$.offset = 0;
1055 | T_SYMBOL '+' T_NUMBER
1057 $$.symbol = $1;
1058 $$.offset = $3;
1060 | T_SYMBOL '-' T_NUMBER
1062 $$.symbol = $1;
1063 $$.offset = -$3;
1065 | '.'
1067 $$.symbol = NULL;
1068 $$.offset = 0;
1070 | '.' '+' T_NUMBER
1072 $$.symbol = NULL;
1073 $$.offset = $3;
1075 | '.' '-' T_NUMBER
1077 $$.symbol = NULL;
1078 $$.offset = -$3;
1082 conditional:
1083 T_IF T_CEXPR '{'
1085 scope_t *new_scope;
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 '{'
1095 scope_t *new_scope;
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,
1105 scope_tailq);
1106 if (last_scope == NULL
1107 || last_scope->type == T_ELSE) {
1109 stop("'else if' without leading 'if'", EX_DATAERR);
1110 /* NOTREACHED */
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;
1118 | T_ELSE '{'
1120 scope_t *new_scope;
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,
1130 scope_tailq);
1131 if (last_scope == NULL
1132 || last_scope->type == SCOPE_ELSE) {
1134 stop("'else' without leading 'if'", EX_DATAERR);
1135 /* NOTREACHED */
1137 new_scope = scope_alloc();
1138 new_scope->type = SCOPE_ELSE;
1139 new_scope->begin_addr = instruction_ptr;
1143 conditional:
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);
1151 /* NOTREACHED */
1154 scope_context->end_addr = instruction_ptr;
1156 /* Pop the scope */
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);
1163 /* NOTREACHED */
1168 f1_opcode:
1169 T_AND { $$ = AIC_OP_AND; }
1170 | T_XOR { $$ = AIC_OP_XOR; }
1171 | T_ADD { $$ = AIC_OP_ADD; }
1172 | T_ADC { $$ = AIC_OP_ADC; }
1175 code:
1176 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1178 format_1_instr($1, &$2, &$4, &$5, $6);
1182 code:
1183 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1185 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1189 code:
1190 T_INC destination opt_source ret ';'
1192 expression_t immed;
1194 make_expression(&immed, 1);
1195 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1199 code:
1200 T_DEC destination opt_source ret ';'
1202 expression_t immed;
1204 make_expression(&immed, -1);
1205 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1209 code:
1210 T_CLC ret ';'
1212 expression_t immed;
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);
1223 code:
1224 T_STC ret ';'
1226 expression_t immed;
1228 make_expression(&immed, 1);
1229 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1231 | T_STC destination ret ';'
1233 expression_t immed;
1235 make_expression(&immed, 1);
1236 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1240 code:
1241 T_BMOV destination ',' source ',' immediate ret ';'
1243 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1247 code:
1248 T_MOV destination ',' source ret ';'
1250 expression_t immed;
1252 make_expression(&immed, 1);
1253 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1257 code:
1258 T_MVI destination ',' immediate ret ';'
1260 if ($4.value == 0
1261 && is_download_const(&$4) == 0) {
1262 expression_t immed;
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);
1271 } else {
1272 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1277 code:
1278 T_NOT destination opt_source ret ';'
1280 expression_t immed;
1282 make_expression(&immed, 0xff);
1283 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1287 code:
1288 T_CLR destination ret ';'
1290 expression_t immed;
1292 make_expression(&immed, 0xff);
1293 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1297 code:
1298 T_NOP ret ';'
1300 expression_t immed;
1302 make_expression(&immed, 0xff);
1303 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1307 code:
1308 T_RET ';'
1310 expression_t immed;
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
1323 * other formats.
1326 f2_opcode:
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
1336 *f4_opcode:
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; }
1346 code:
1347 f2_opcode destination ',' expression opt_source ret ';'
1349 format_2_instr($1, &$2, &$4, &$5, $6);
1353 jmp_jc_jnc_call:
1354 T_JMP { $$ = AIC_OP_JMP; }
1355 | T_JC { $$ = AIC_OP_JC; }
1356 | T_JNC { $$ = AIC_OP_JNC; }
1357 | T_CALL { $$ = AIC_OP_CALL; }
1360 jz_jnz:
1361 T_JZ { $$ = AIC_OP_JZ; }
1362 | T_JNZ { $$ = AIC_OP_JNZ; }
1365 je_jne:
1366 T_JE { $$ = AIC_OP_JE; }
1367 | T_JNE { $$ = AIC_OP_JNE; }
1370 code:
1371 jmp_jc_jnc_call address ';'
1373 expression_t immed;
1375 make_expression(&immed, 0);
1376 format_3_instr($1, &sindex, &immed, &$2);
1380 code:
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);
1388 code:
1389 T_TEST source ',' immediate_or_a jz_jnz address ';'
1391 format_3_instr($5, &$2, &$4, &$6);
1395 code:
1396 T_CMP source ',' immediate_or_a je_jne address ';'
1398 format_3_instr($5, &$2, &$4, &$6);
1402 code:
1403 T_MOV source jmp_jc_jnc_call address ';'
1405 expression_t immed;
1407 make_expression(&immed, 0);
1408 format_3_instr($3, &$2, &immed, &$4);
1412 code:
1413 T_MVI immediate jmp_jc_jnc_call address ';'
1415 format_3_instr($3, &allzeros, &$2, &$4);
1421 static void
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);
1438 /* NOTREACHED */
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;
1444 } else {
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);
1450 /* NOTREACHED */
1451 } else if (value != sym->info.finfo->value) {
1452 stop("Field redefined with a conflicting value", EX_DATAERR);
1453 /* NOTREACHED */
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);
1459 /* NOTREACHED */
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);
1468 static void
1469 initialize_symbol(symbol_t *symbol)
1471 switch (symbol->type) {
1472 case UNINITIALIZED:
1473 stop("Call to initialize_symbol with type field unset",
1474 EX_SOFTWARE);
1475 /* NOTREACHED */
1476 break;
1477 case REGISTER:
1478 case SRAMLOC:
1479 case SCBLOC:
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);
1484 /* NOTREACHED */
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
1492 * we are in.
1494 if (scb_or_sram_symbol != NULL)
1495 symbol->info.rinfo->modes =
1496 scb_or_sram_symbol->info.rinfo->modes;
1497 else
1498 symbol->info.rinfo->modes = ~0;
1499 break;
1500 case ALIAS:
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);
1505 /* NOTREACHED */
1507 memset(symbol->info.ainfo, 0,
1508 sizeof(struct alias_info));
1509 break;
1510 case MASK:
1511 case FIELD:
1512 case ENUM:
1513 case ENUM_ENTRY:
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);
1518 /* NOTREACHED */
1520 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1521 SLIST_INIT(&(symbol->info.finfo->symrefs));
1522 break;
1523 case CONST:
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);
1529 /* NOTREACHED */
1531 memset(symbol->info.cinfo, 0,
1532 sizeof(struct const_info));
1533 break;
1534 case LABEL:
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);
1539 /* NOTREACHED */
1541 memset(symbol->info.linfo, 0,
1542 sizeof(struct label_info));
1543 break;
1544 case CONDITIONAL:
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);
1549 /* NOTREACHED */
1551 memset(symbol->info.condinfo, 0,
1552 sizeof(struct cond_info));
1553 break;
1554 case MACRO:
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);
1559 /* NOTREACHED */
1561 memset(symbol->info.macroinfo, 0,
1562 sizeof(struct macro_info));
1563 STAILQ_INIT(&symbol->info.macroinfo->args);
1564 break;
1565 default:
1566 stop("Call to initialize_symbol with invalid symbol type",
1567 EX_SOFTWARE);
1568 /* NOTREACHED */
1569 break;
1573 static void
1574 add_macro_arg(const char *argtext, int argnum)
1576 struct macro_arg *marg;
1577 int i;
1578 int retval;
1580 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1581 stop("Invalid current symbol for adding macro arg",
1582 EX_SOFTWARE);
1583 /* NOTREACHED */
1586 marg = (struct macro_arg *)malloc(sizeof(*marg));
1587 if (marg == NULL) {
1588 stop("Can't create macro_arg structure", EX_SOFTWARE);
1589 /* NOTREACHED */
1591 marg->replacement_text = NULL;
1592 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1593 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1594 argtext);
1595 if (retval >= sizeof(regex_pattern)) {
1596 stop("Regex text buffer too small for arg",
1597 EX_SOFTWARE);
1598 /* NOTREACHED */
1600 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1601 if (retval != 0) {
1602 stop("Regex compilation failed", EX_SOFTWARE);
1603 /* NOTREACHED */
1605 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1608 static void
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",
1613 EX_SOFTWARE);
1614 /* NOTREACHED */
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);
1619 /* NOTREACHED */
1623 static void
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",
1630 symbol->name);
1631 stop(errbuf, EX_DATAERR);
1632 /* NOTREACHED */
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",
1640 symbol->name);
1641 stop(errbuf, EX_DATAERR);
1645 static void
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)
1653 src = dest;
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
1670 + dest->offset;
1671 f1_instr->source = src->symbol->info.rinfo->address
1672 + src->offset;
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) {
1678 u_int src_value;
1679 u_int dst_value;
1682 * Attempt to update mode information if
1683 * we are operating on the mode register.
1685 if (src->symbol == allones.symbol)
1686 src_value = 0xFF;
1687 else if (src->symbol == allzeros.symbol)
1688 src_value = 0;
1689 else if (src->symbol == mode_ptr.symbol)
1690 src_value = (dst_mode << 4) | src_mode;
1691 else
1692 goto cant_update;
1694 switch (opcode) {
1695 case AIC_OP_AND:
1696 dst_value = src_value & immed->value;
1697 break;
1698 case AIC_OP_XOR:
1699 dst_value = src_value ^ immed->value;
1700 break;
1701 case AIC_OP_ADD:
1702 dst_value = (src_value + immed->value) & 0xFF;
1703 break;
1704 case AIC_OP_OR:
1705 dst_value = src_value | immed->value;
1706 break;
1707 case AIC_OP_BMOV:
1708 dst_value = src_value;
1709 break;
1710 default:
1711 goto cant_update;
1713 src_mode = dst_value & 0xF;
1714 dst_mode = (dst_value >> 4) & 0xF;
1717 cant_update:
1718 symlist_free(&immed->referenced_syms);
1719 instruction_ptr++;
1722 static void
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)
1731 src = dest;
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
1743 + dest->offset;
1744 f2_instr->source = src->symbol->info.rinfo->address
1745 + src->offset;
1746 if (places->value > 8 || places->value <= 0) {
1747 stop("illegal shift value", EX_DATAERR);
1748 /* NOTREACHED */
1750 switch (opcode) {
1751 case AIC_OP_SHL:
1752 if (places->value == 8)
1753 shift_control = 0xf0;
1754 else
1755 shift_control = (places->value << 4) | places->value;
1756 break;
1757 case AIC_OP_SHR:
1758 if (places->value == 8) {
1759 shift_control = 0xf8;
1760 } else {
1761 shift_control = (places->value << 4)
1762 | (8 - places->value)
1763 | 0x08;
1765 break;
1766 case AIC_OP_ROL:
1767 shift_control = places->value & 0x7;
1768 break;
1769 case AIC_OP_ROR:
1770 shift_control = (8 - places->value) | 0x08;
1771 break;
1772 default:
1773 shift_control = 0; /* Quiet Compiler */
1774 stop("Invalid shift operation specified", EX_SOFTWARE);
1775 /* NOTREACHED */
1776 break;
1778 f2_instr->shift_control = shift_control;
1779 symlist_free(&places->referenced_syms);
1780 instruction_ptr++;
1783 static void
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;
1789 int addr;
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;
1804 } else
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
1809 + src->offset;
1810 f3_instr->immediate = immed->value;
1812 if (is_download_const(immed))
1813 f3_instr->parity = 1;
1815 symlist_free(&immed->referenced_syms);
1816 instruction_ptr++;
1819 static void
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",
1831 EX_DATAERR);
1832 /* NOTREACHED */
1836 static void
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",
1848 EX_DATAERR);
1849 /* NOTREACHED */
1853 static void
1854 type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
1856 symbol_t *symbol = sym->symbol;
1857 symbol_node_t *node;
1858 int and_op;
1859 int8_t value, mask;
1861 and_op = FALSE;
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)
1870 and_op = TRUE;
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",
1881 (mask & value),
1882 symbol->name);
1883 stop(errbuf, EX_DATAERR);
1884 /* NOTREACHED */
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;
1893 node != NULL;
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 "
1903 "for register %s",
1904 node->symbol->name, symbol->name);
1905 stop(errbuf, EX_DATAERR);
1906 /* NOTREACHED */
1912 static void
1913 make_expression(expression_t *immed, int value)
1915 SLIST_INIT(&immed->referenced_syms);
1916 immed->value = value & 0xff;
1919 static void
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);
1932 /* NOTREACHED */
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)
1942 return;
1944 if (symbol->type != UNINITIALIZED) {
1945 stop("Conditional expression conflicts with a symbol",
1946 EX_DATAERR);
1947 /* NOTREACHED */
1950 symbol->type = CONDITIONAL;
1951 initialize_symbol(symbol);
1952 symbol->info.condinfo->func_num = numfuncs++;
1953 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1956 static void
1957 add_version(const char *verstring)
1959 const char prefix[] = " * ";
1960 int newlen;
1961 int oldlen;
1963 newlen = strlen(verstring) + strlen(prefix);
1964 oldlen = 0;
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';
1976 void
1977 yyerror(const char *string)
1979 stop(string, EX_DATAERR);
1982 static int
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))
1987 return (TRUE);
1989 return (FALSE);
1992 static int
1993 is_location_address(symbol_t *sym)
1995 if (sym->type == SCBLOC ||
1996 sym->type == SRAMLOC)
1997 return (TRUE);
1998 return (FALSE);