]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/scsi/aic7xxx/aicasm/aicasm_gram.y
[SCSI] aic7xxx: Update type check in aicasm grammar
[mirror_ubuntu-zesty-kernel.git] / drivers / scsi / aic7xxx / aicasm / aicasm_gram.y
1 %{
2 /*
3 * Parser for the Aic7xxx SCSI Host adapter sequencer assembler.
4 *
5 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
6 * Copyright (c) 2001, 2002 Adaptec Inc.
7 * All rights reserved.
8 *
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.
23 *
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.
27 *
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.
40 *
41 * $Id: //depot/aic7xxx/aic7xxx/aicasm/aicasm_gram.y#30 $
42 *
43 * $FreeBSD$
44 */
45
46 #include <sys/types.h>
47
48 #include <inttypes.h>
49 #include <regex.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <sysexits.h>
54
55 #ifdef __linux__
56 #include "../queue.h"
57 #else
58 #include <sys/queue.h>
59 #endif
60
61 #include "aicasm.h"
62 #include "aicasm_symbol.h"
63 #include "aicasm_insformat.h"
64
65 int yylineno;
66 char *yyfilename;
67 char stock_prefix[] = "aic_";
68 char *prefix = stock_prefix;
69 char *patch_arg_list;
70 char *versions;
71 static char errbuf[255];
72 static char regex_pattern[255];
73 static symbol_t *cur_symbol;
74 static symbol_t *field_symbol;
75 static symbol_t *scb_or_sram_symbol;
76 static symtype cur_symtype;
77 static symbol_ref_t accumulator;
78 static symbol_ref_t mode_ptr;
79 static symbol_ref_t allones;
80 static symbol_ref_t allzeros;
81 static symbol_ref_t none;
82 static symbol_ref_t sindex;
83 static int instruction_ptr;
84 static int num_srams;
85 static int sram_or_scb_offset;
86 static int download_constant_count;
87 static int in_critical_section;
88 static u_int enum_increment;
89 static u_int enum_next_value;
90
91 static void process_field(int field_type, symbol_t *sym, int mask);
92 static void initialize_symbol(symbol_t *symbol);
93 static void add_macro_arg(const char *argtext, int position);
94 static void add_macro_body(const char *bodytext);
95 static void process_register(symbol_t **p_symbol);
96 static void format_1_instr(int opcode, symbol_ref_t *dest,
97 expression_t *immed, symbol_ref_t *src, int ret);
98 static void format_2_instr(int opcode, symbol_ref_t *dest,
99 expression_t *places, symbol_ref_t *src, int ret);
100 static void format_3_instr(int opcode, symbol_ref_t *src,
101 expression_t *immed, symbol_ref_t *address);
102 static void test_readable_symbol(symbol_t *symbol);
103 static void test_writable_symbol(symbol_t *symbol);
104 static void type_check(symbol_ref_t *sym, expression_t *expression, int and_op);
105 static void make_expression(expression_t *immed, int value);
106 static void add_conditional(symbol_t *symbol);
107 static void add_version(const char *verstring);
108 static int is_download_const(expression_t *immed);
109 static int is_location_address(symbol_t *symbol);
110 void yyerror(const char *string);
111
112 #define SRAM_SYMNAME "SRAM_BASE"
113 #define SCB_SYMNAME "SCB_BASE"
114 %}
115
116 %union {
117 u_int value;
118 char *str;
119 symbol_t *sym;
120 symbol_ref_t sym_ref;
121 expression_t expression;
122 }
123
124 %token T_REGISTER
125
126 %token <value> T_CONST
127
128 %token T_EXPORT
129
130 %token T_DOWNLOAD
131
132 %token T_SCB
133
134 %token T_SRAM
135
136 %token T_ALIAS
137
138 %token T_SIZE
139
140 %token T_EXPR_LSHIFT
141
142 %token T_EXPR_RSHIFT
143
144 %token <value> T_ADDRESS
145
146 %token T_ACCESS_MODE
147
148 %token T_MODES
149
150 %token T_DEFINE
151
152 %token T_SET_SRC_MODE
153
154 %token T_SET_DST_MODE
155
156 %token <value> T_MODE
157
158 %token T_BEGIN_CS
159
160 %token T_END_CS
161
162 %token T_PAD_PAGE
163
164 %token T_FIELD
165
166 %token T_ENUM
167
168 %token T_MASK
169
170 %token <value> T_NUMBER
171
172 %token <str> T_PATH T_STRING T_ARG T_MACROBODY
173
174 %token <sym> T_CEXPR
175
176 %token T_EOF T_INCLUDE T_VERSION T_PREFIX T_PATCH_ARG_LIST
177
178 %token <value> T_SHR T_SHL T_ROR T_ROL
179
180 %token <value> T_MVI T_MOV T_CLR T_BMOV
181
182 %token <value> T_JMP T_JC T_JNC T_JE T_JNE T_JNZ T_JZ T_CALL
183
184 %token <value> T_ADD T_ADC
185
186 %token <value> T_INC T_DEC
187
188 %token <value> T_STC T_CLC
189
190 %token <value> T_CMP T_NOT T_XOR
191
192 %token <value> T_TEST T_AND
193
194 %token <value> T_OR
195
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
199 */
200 %token T_RET
201
202 %token T_NOP
203
204 %token T_ACCUM T_ALLONES T_ALLZEROS T_NONE T_SINDEX T_MODE_PTR
205
206 %token T_A
207
208 %token <sym> T_SYMBOL
209
210 %token T_NL
211
212 %token T_IF T_ELSE T_ELSE_IF T_ENDIF
213
214 %type <sym_ref> reg_symbol address destination source opt_source
215
216 %type <expression> expression immediate immediate_or_a
217
218 %type <value> export ret f1_opcode f2_opcode jmp_jc_jnc_call jz_jnz je_jne
219
220 %type <value> mode_value mode_list macro_arglist
221
222 %left '|'
223 %left '&'
224 %left T_EXPR_LSHIFT T_EXPR_RSHIFT
225 %left '+' '-'
226 %left '*' '/'
227 %right '~'
228 %nonassoc UMINUS
229 %%
230
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
264 ;
265
266 include:
267 T_INCLUDE '<' T_PATH '>'
268 {
269 include_file($3, BRACKETED_INCLUDE);
270 }
271 | T_INCLUDE '"' T_PATH '"'
272 {
273 include_file($3, QUOTED_INCLUDE);
274 }
275 ;
276
277 prefix:
278 T_PREFIX '=' T_STRING
279 {
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);
286 }
287 ;
288
289 patch_arg_list:
290 T_PATCH_ARG_LIST '=' T_STRING
291 {
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);
298 }
299 ;
300
301 version:
302 T_VERSION '=' T_STRING
303 { add_version($3); }
304 ;
305
306 register:
307 T_REGISTER { cur_symtype = REGISTER; } reg_definition
308 ;
309
310 reg_definition:
311 T_SYMBOL '{'
312 {
313 if ($1->type != UNINITIALIZED) {
314 stop("Register multiply defined", EX_DATAERR);
315 /* NOTREACHED */
316 }
317 cur_symbol = $1;
318 cur_symbol->type = cur_symtype;
319 initialize_symbol(cur_symbol);
320 }
321 reg_attribute_list
322 '}'
323 {
324 /*
325 * Default to allowing everything in for registers
326 * with no bit or mask definitions.
327 */
328 if (cur_symbol->info.rinfo->valid_bitmask == 0)
329 cur_symbol->info.rinfo->valid_bitmask = 0xFF;
330
331 if (cur_symbol->info.rinfo->size == 0)
332 cur_symbol->info.rinfo->size = 1;
333
334 /*
335 * This might be useful for registers too.
336 */
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;
343 }
344 cur_symbol = NULL;
345 }
346 ;
347
348 reg_attribute_list:
349 reg_attribute
350 | reg_attribute_list reg_attribute
351 ;
352
353 reg_attribute:
354 reg_address
355 | size
356 | access_mode
357 | modes
358 | field_defn
359 | enum_defn
360 | mask_defn
361 | alias
362 | accumulator
363 | mode_pointer
364 | allones
365 | allzeros
366 | none
367 | sindex
368 ;
369
370 reg_address:
371 T_ADDRESS T_NUMBER
372 {
373 cur_symbol->info.rinfo->address = $2;
374 }
375 ;
376
377 size:
378 T_SIZE T_NUMBER
379 {
380 cur_symbol->info.rinfo->size = $2;
381 if (scb_or_sram_symbol != NULL) {
382 u_int max_addr;
383 u_int sym_max_addr;
384
385 max_addr = scb_or_sram_symbol->info.rinfo->address
386 + scb_or_sram_symbol->info.rinfo->size;
387 sym_max_addr = cur_symbol->info.rinfo->address
388 + cur_symbol->info.rinfo->size;
389
390 if (sym_max_addr > max_addr)
391 stop("SCB or SRAM space exhausted", EX_DATAERR);
392 }
393 }
394 ;
395
396 access_mode:
397 T_ACCESS_MODE T_MODE
398 {
399 cur_symbol->info.rinfo->mode = $2;
400 }
401 ;
402
403 modes:
404 T_MODES mode_list
405 {
406 cur_symbol->info.rinfo->modes = $2;
407 }
408 ;
409
410 mode_list:
411 mode_value
412 {
413 $$ = $1;
414 }
415 | mode_list ',' mode_value
416 {
417 $$ = $1 | $3;
418 }
419 ;
420
421 mode_value:
422 T_NUMBER
423 {
424 if ($1 > 4) {
425 stop("Valid register modes range between 0 and 4.",
426 EX_DATAERR);
427 /* NOTREACHED */
428 }
429
430 $$ = (0x1 << $1);
431 }
432 | T_SYMBOL
433 {
434 symbol_t *symbol;
435
436 symbol = $1;
437 if (symbol->type != CONST) {
438 stop("Only \"const\" symbols allowed in "
439 "mode definitions.", EX_DATAERR);
440 /* NOTREACHED */
441 }
442 if (symbol->info.cinfo->value > 4) {
443 stop("Valid register modes range between 0 and 4.",
444 EX_DATAERR);
445 /* NOTREACHED */
446 }
447 $$ = (0x1 << symbol->info.cinfo->value);
448 }
449 ;
450
451 field_defn:
452 T_FIELD
453 {
454 field_symbol = NULL;
455 enum_next_value = 0;
456 enum_increment = 1;
457 }
458 '{' enum_entry_list '}'
459 | T_FIELD T_SYMBOL expression
460 {
461 process_field(FIELD, $2, $3.value);
462 field_symbol = $2;
463 enum_next_value = 0;
464 enum_increment = 0x01 << (ffs($3.value) - 1);
465 }
466 '{' enum_entry_list '}'
467 | T_FIELD T_SYMBOL expression
468 {
469 process_field(FIELD, $2, $3.value);
470 }
471 ;
472
473 enum_defn:
474 T_ENUM
475 {
476 field_symbol = NULL;
477 enum_next_value = 0;
478 enum_increment = 1;
479 }
480 '{' enum_entry_list '}'
481 | T_ENUM T_SYMBOL expression
482 {
483 process_field(ENUM, $2, $3.value);
484 field_symbol = $2;
485 enum_next_value = 0;
486 enum_increment = 0x01 << (ffs($3.value) - 1);
487 }
488 '{' enum_entry_list '}'
489 ;
490
491 enum_entry_list:
492 enum_entry
493 | enum_entry_list ',' enum_entry
494 ;
495
496 enum_entry:
497 T_SYMBOL
498 {
499 process_field(ENUM_ENTRY, $1, enum_next_value);
500 enum_next_value += enum_increment;
501 }
502 | T_SYMBOL expression
503 {
504 process_field(ENUM_ENTRY, $1, $2.value);
505 enum_next_value = $2.value + enum_increment;
506 }
507 ;
508
509 mask_defn:
510 T_MASK T_SYMBOL expression
511 {
512 process_field(MASK, $2, $3.value);
513 }
514 ;
515
516 alias:
517 T_ALIAS T_SYMBOL
518 {
519 if ($2->type != UNINITIALIZED) {
520 stop("Re-definition of register alias",
521 EX_DATAERR);
522 /* NOTREACHED */
523 }
524 $2->type = ALIAS;
525 initialize_symbol($2);
526 $2->info.ainfo->parent = cur_symbol;
527 }
528 ;
529
530 accumulator:
531 T_ACCUM
532 {
533 if (accumulator.symbol != NULL) {
534 stop("Only one accumulator definition allowed",
535 EX_DATAERR);
536 /* NOTREACHED */
537 }
538 accumulator.symbol = cur_symbol;
539 }
540 ;
541
542 mode_pointer:
543 T_MODE_PTR
544 {
545 if (mode_ptr.symbol != NULL) {
546 stop("Only one mode pointer definition allowed",
547 EX_DATAERR);
548 /* NOTREACHED */
549 }
550 mode_ptr.symbol = cur_symbol;
551 }
552 ;
553
554 allones:
555 T_ALLONES
556 {
557 if (allones.symbol != NULL) {
558 stop("Only one definition of allones allowed",
559 EX_DATAERR);
560 /* NOTREACHED */
561 }
562 allones.symbol = cur_symbol;
563 }
564 ;
565
566 allzeros:
567 T_ALLZEROS
568 {
569 if (allzeros.symbol != NULL) {
570 stop("Only one definition of allzeros allowed",
571 EX_DATAERR);
572 /* NOTREACHED */
573 }
574 allzeros.symbol = cur_symbol;
575 }
576 ;
577
578 none:
579 T_NONE
580 {
581 if (none.symbol != NULL) {
582 stop("Only one definition of none allowed",
583 EX_DATAERR);
584 /* NOTREACHED */
585 }
586 none.symbol = cur_symbol;
587 }
588 ;
589
590 sindex:
591 T_SINDEX
592 {
593 if (sindex.symbol != NULL) {
594 stop("Only one definition of sindex allowed",
595 EX_DATAERR);
596 /* NOTREACHED */
597 }
598 sindex.symbol = cur_symbol;
599 }
600 ;
601
602 expression:
603 expression '|' expression
604 {
605 $$.value = $1.value | $3.value;
606 symlist_merge(&$$.referenced_syms,
607 &$1.referenced_syms,
608 &$3.referenced_syms);
609 }
610 | expression '&' expression
611 {
612 $$.value = $1.value & $3.value;
613 symlist_merge(&$$.referenced_syms,
614 &$1.referenced_syms,
615 &$3.referenced_syms);
616 }
617 | expression '+' expression
618 {
619 $$.value = $1.value + $3.value;
620 symlist_merge(&$$.referenced_syms,
621 &$1.referenced_syms,
622 &$3.referenced_syms);
623 }
624 | expression '-' expression
625 {
626 $$.value = $1.value - $3.value;
627 symlist_merge(&($$.referenced_syms),
628 &($1.referenced_syms),
629 &($3.referenced_syms));
630 }
631 | expression '*' expression
632 {
633 $$.value = $1.value * $3.value;
634 symlist_merge(&($$.referenced_syms),
635 &($1.referenced_syms),
636 &($3.referenced_syms));
637 }
638 | expression '/' expression
639 {
640 $$.value = $1.value / $3.value;
641 symlist_merge(&($$.referenced_syms),
642 &($1.referenced_syms),
643 &($3.referenced_syms));
644 }
645 | expression T_EXPR_LSHIFT expression
646 {
647 $$.value = $1.value << $3.value;
648 symlist_merge(&$$.referenced_syms,
649 &$1.referenced_syms,
650 &$3.referenced_syms);
651 }
652 | expression T_EXPR_RSHIFT expression
653 {
654 $$.value = $1.value >> $3.value;
655 symlist_merge(&$$.referenced_syms,
656 &$1.referenced_syms,
657 &$3.referenced_syms);
658 }
659 | '(' expression ')'
660 {
661 $$ = $2;
662 }
663 | '~' expression
664 {
665 $$ = $2;
666 $$.value = (~$$.value) & 0xFF;
667 }
668 | '-' expression %prec UMINUS
669 {
670 $$ = $2;
671 $$.value = -$$.value;
672 }
673 | T_NUMBER
674 {
675 $$.value = $1;
676 SLIST_INIT(&$$.referenced_syms);
677 }
678 | T_SYMBOL
679 {
680 symbol_t *symbol;
681
682 symbol = $1;
683 switch (symbol->type) {
684 case ALIAS:
685 symbol = $1->info.ainfo->parent;
686 case REGISTER:
687 case SCBLOC:
688 case SRAMLOC:
689 $$.value = symbol->info.rinfo->address;
690 break;
691 case MASK:
692 case FIELD:
693 case ENUM:
694 case ENUM_ENTRY:
695 $$.value = symbol->info.finfo->value;
696 break;
697 case DOWNLOAD_CONST:
698 case CONST:
699 $$.value = symbol->info.cinfo->value;
700 break;
701 case UNINITIALIZED:
702 default:
703 {
704 snprintf(errbuf, sizeof(errbuf),
705 "Undefined symbol %s referenced",
706 symbol->name);
707 stop(errbuf, EX_DATAERR);
708 /* NOTREACHED */
709 break;
710 }
711 }
712 SLIST_INIT(&$$.referenced_syms);
713 symlist_add(&$$.referenced_syms, symbol, SYMLIST_INSERT_HEAD);
714 }
715 ;
716
717 constant:
718 T_CONST T_SYMBOL expression
719 {
720 if ($2->type != UNINITIALIZED) {
721 stop("Re-definition of symbol as a constant",
722 EX_DATAERR);
723 /* NOTREACHED */
724 }
725 $2->type = CONST;
726 initialize_symbol($2);
727 $2->info.cinfo->value = $3.value;
728 }
729 | T_CONST T_SYMBOL T_DOWNLOAD
730 {
731 if ($1) {
732 stop("Invalid downloaded constant declaration",
733 EX_DATAERR);
734 /* NOTREACHED */
735 }
736 if ($2->type != UNINITIALIZED) {
737 stop("Re-definition of symbol as a downloaded constant",
738 EX_DATAERR);
739 /* NOTREACHED */
740 }
741 $2->type = DOWNLOAD_CONST;
742 initialize_symbol($2);
743 $2->info.cinfo->value = download_constant_count++;
744 }
745 ;
746
747 macrodefn_prologue:
748 T_DEFINE T_SYMBOL
749 {
750 if ($2->type != UNINITIALIZED) {
751 stop("Re-definition of symbol as a macro",
752 EX_DATAERR);
753 /* NOTREACHED */
754 }
755 cur_symbol = $2;
756 cur_symbol->type = MACRO;
757 initialize_symbol(cur_symbol);
758 }
759 ;
760
761 macrodefn:
762 macrodefn_prologue T_MACROBODY
763 {
764 add_macro_body($2);
765 }
766 | macrodefn_prologue '(' macro_arglist ')' T_MACROBODY
767 {
768 add_macro_body($5);
769 cur_symbol->info.macroinfo->narg = $3;
770 }
771 ;
772
773 macro_arglist:
774 {
775 /* Macros can take no arguments */
776 $$ = 0;
777 }
778 | T_ARG
779 {
780 $$ = 1;
781 add_macro_arg($1, 0);
782 }
783 | macro_arglist ',' T_ARG
784 {
785 if ($1 == 0) {
786 stop("Comma without preceeding argument in arg list",
787 EX_DATAERR);
788 /* NOTREACHED */
789 }
790 $$ = $1 + 1;
791 add_macro_arg($3, $1);
792 }
793 ;
794
795 scratch_ram:
796 T_SRAM '{'
797 {
798 snprintf(errbuf, sizeof(errbuf), "%s%d", SRAM_SYMNAME,
799 num_srams);
800 cur_symbol = symtable_get(SRAM_SYMNAME);
801 cur_symtype = SRAMLOC;
802 cur_symbol->type = SRAMLOC;
803 initialize_symbol(cur_symbol);
804 }
805 reg_address
806 {
807 sram_or_scb_offset = cur_symbol->info.rinfo->address;
808 }
809 size
810 {
811 scb_or_sram_symbol = cur_symbol;
812 }
813 scb_or_sram_attributes
814 '}'
815 {
816 cur_symbol = NULL;
817 scb_or_sram_symbol = NULL;
818 }
819 ;
820
821 scb:
822 T_SCB '{'
823 {
824 cur_symbol = symtable_get(SCB_SYMNAME);
825 cur_symtype = SCBLOC;
826 if (cur_symbol->type != UNINITIALIZED) {
827 stop("Only one SRAM definition allowed",
828 EX_SOFTWARE);
829 /* NOTREACHED */
830 }
831 cur_symbol->type = SCBLOC;
832 initialize_symbol(cur_symbol);
833 /* 64 bytes of SCB space */
834 cur_symbol->info.rinfo->size = 64;
835 }
836 reg_address
837 {
838 sram_or_scb_offset = cur_symbol->info.rinfo->address;
839 }
840 size
841 {
842 scb_or_sram_symbol = cur_symbol;
843 }
844 scb_or_sram_attributes
845 '}'
846 {
847 cur_symbol = NULL;
848 scb_or_sram_symbol = NULL;
849 }
850 ;
851
852 scb_or_sram_attributes:
853 /* NULL definition is okay */
854 | modes
855 | scb_or_sram_reg_list
856 | modes scb_or_sram_reg_list
857 ;
858
859 scb_or_sram_reg_list:
860 reg_definition
861 | scb_or_sram_reg_list reg_definition
862 ;
863
864 reg_symbol:
865 T_SYMBOL
866 {
867 process_register(&$1);
868 $$.symbol = $1;
869 $$.offset = 0;
870 }
871 | T_SYMBOL '[' T_SYMBOL ']'
872 {
873 process_register(&$1);
874 if ($3->type != CONST) {
875 stop("register offset must be a constant", EX_DATAERR);
876 /* NOTREACHED */
877 }
878 if (($3->info.cinfo->value + 1) > $1->info.rinfo->size) {
879 stop("Accessing offset beyond range of register",
880 EX_DATAERR);
881 /* NOTREACHED */
882 }
883 $$.symbol = $1;
884 $$.offset = $3->info.cinfo->value;
885 }
886 | T_SYMBOL '[' T_NUMBER ']'
887 {
888 process_register(&$1);
889 if (($3 + 1) > $1->info.rinfo->size) {
890 stop("Accessing offset beyond range of register",
891 EX_DATAERR);
892 /* NOTREACHED */
893 }
894 $$.symbol = $1;
895 $$.offset = $3;
896 }
897 | T_A
898 {
899 if (accumulator.symbol == NULL) {
900 stop("No accumulator has been defined", EX_DATAERR);
901 /* NOTREACHED */
902 }
903 $$.symbol = accumulator.symbol;
904 $$.offset = 0;
905 }
906 ;
907
908 destination:
909 reg_symbol
910 {
911 test_writable_symbol($1.symbol);
912 $$ = $1;
913 }
914 ;
915
916 immediate:
917 expression
918 { $$ = $1; }
919 ;
920
921 immediate_or_a:
922 expression
923 {
924 if ($1.value == 0 && is_download_const(&$1) == 0) {
925 snprintf(errbuf, sizeof(errbuf),
926 "\nExpression evaluates to 0 and thus "
927 "references the accumulator.\n "
928 "If this is the desired effect, use 'A' "
929 "instead.\n");
930 stop(errbuf, EX_DATAERR);
931 }
932 $$ = $1;
933 }
934 | T_A
935 {
936 SLIST_INIT(&$$.referenced_syms);
937 symlist_add(&$$.referenced_syms, accumulator.symbol,
938 SYMLIST_INSERT_HEAD);
939 $$.value = 0;
940 }
941 ;
942
943 source:
944 reg_symbol
945 {
946 test_readable_symbol($1.symbol);
947 $$ = $1;
948 }
949 ;
950
951 opt_source:
952 {
953 $$.symbol = NULL;
954 $$.offset = 0;
955 }
956 | ',' source
957 { $$ = $2; }
958 ;
959
960 ret:
961 { $$ = 0; }
962 | T_RET
963 { $$ = 1; }
964 ;
965
966 set_src_mode:
967 T_SET_SRC_MODE T_NUMBER ';'
968 {
969 src_mode = $2;
970 }
971 ;
972
973 set_dst_mode:
974 T_SET_DST_MODE T_NUMBER ';'
975 {
976 dst_mode = $2;
977 }
978 ;
979
980 critical_section_start:
981 T_BEGIN_CS ';'
982 {
983 critical_section_t *cs;
984
985 if (in_critical_section != FALSE) {
986 stop("Critical Section within Critical Section",
987 EX_DATAERR);
988 /* NOTREACHED */
989 }
990 cs = cs_alloc();
991 cs->begin_addr = instruction_ptr;
992 in_critical_section = TRUE;
993 }
994 ;
995
996 critical_section_end:
997 T_END_CS ';'
998 {
999 critical_section_t *cs;
1000
1001 if (in_critical_section == FALSE) {
1002 stop("Unballanced 'end_cs'", EX_DATAERR);
1003 /* NOTREACHED */
1004 }
1005 cs = TAILQ_LAST(&cs_tailq, cs_tailq);
1006 cs->end_addr = instruction_ptr;
1007 in_critical_section = FALSE;
1008 }
1009 ;
1010
1011 export:
1012 { $$ = 0; }
1013 | T_EXPORT
1014 { $$ = 1; }
1015 ;
1016
1017 label:
1018 export T_SYMBOL ':'
1019 {
1020 if ($2->type != UNINITIALIZED) {
1021 stop("Program label multiply defined", EX_DATAERR);
1022 /* NOTREACHED */
1023 }
1024 $2->type = LABEL;
1025 initialize_symbol($2);
1026 $2->info.linfo->address = instruction_ptr;
1027 $2->info.linfo->exported = $1;
1028 }
1029 ;
1030
1031 address:
1032 T_SYMBOL
1033 {
1034 $$.symbol = $1;
1035 $$.offset = 0;
1036 }
1037 | T_SYMBOL '+' T_NUMBER
1038 {
1039 $$.symbol = $1;
1040 $$.offset = $3;
1041 }
1042 | T_SYMBOL '-' T_NUMBER
1043 {
1044 $$.symbol = $1;
1045 $$.offset = -$3;
1046 }
1047 | '.'
1048 {
1049 $$.symbol = NULL;
1050 $$.offset = 0;
1051 }
1052 | '.' '+' T_NUMBER
1053 {
1054 $$.symbol = NULL;
1055 $$.offset = $3;
1056 }
1057 | '.' '-' T_NUMBER
1058 {
1059 $$.symbol = NULL;
1060 $$.offset = -$3;
1061 }
1062 ;
1063
1064 conditional:
1065 T_IF T_CEXPR '{'
1066 {
1067 scope_t *new_scope;
1068
1069 add_conditional($2);
1070 new_scope = scope_alloc();
1071 new_scope->type = SCOPE_IF;
1072 new_scope->begin_addr = instruction_ptr;
1073 new_scope->func_num = $2->info.condinfo->func_num;
1074 }
1075 | T_ELSE T_IF T_CEXPR '{'
1076 {
1077 scope_t *new_scope;
1078 scope_t *scope_context;
1079 scope_t *last_scope;
1080
1081 /*
1082 * Ensure that the previous scope is either an
1083 * if or and else if.
1084 */
1085 scope_context = SLIST_FIRST(&scope_stack);
1086 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1087 scope_tailq);
1088 if (last_scope == NULL
1089 || last_scope->type == T_ELSE) {
1090
1091 stop("'else if' without leading 'if'", EX_DATAERR);
1092 /* NOTREACHED */
1093 }
1094 add_conditional($3);
1095 new_scope = scope_alloc();
1096 new_scope->type = SCOPE_ELSE_IF;
1097 new_scope->begin_addr = instruction_ptr;
1098 new_scope->func_num = $3->info.condinfo->func_num;
1099 }
1100 | T_ELSE '{'
1101 {
1102 scope_t *new_scope;
1103 scope_t *scope_context;
1104 scope_t *last_scope;
1105
1106 /*
1107 * Ensure that the previous scope is either an
1108 * if or and else if.
1109 */
1110 scope_context = SLIST_FIRST(&scope_stack);
1111 last_scope = TAILQ_LAST(&scope_context->inner_scope,
1112 scope_tailq);
1113 if (last_scope == NULL
1114 || last_scope->type == SCOPE_ELSE) {
1115
1116 stop("'else' without leading 'if'", EX_DATAERR);
1117 /* NOTREACHED */
1118 }
1119 new_scope = scope_alloc();
1120 new_scope->type = SCOPE_ELSE;
1121 new_scope->begin_addr = instruction_ptr;
1122 }
1123 ;
1124
1125 conditional:
1126 '}'
1127 {
1128 scope_t *scope_context;
1129
1130 scope_context = SLIST_FIRST(&scope_stack);
1131 if (scope_context->type == SCOPE_ROOT) {
1132 stop("Unexpected '}' encountered", EX_DATAERR);
1133 /* NOTREACHED */
1134 }
1135
1136 scope_context->end_addr = instruction_ptr;
1137
1138 /* Pop the scope */
1139 SLIST_REMOVE_HEAD(&scope_stack, scope_stack_links);
1140
1141 process_scope(scope_context);
1142
1143 if (SLIST_FIRST(&scope_stack) == NULL) {
1144 stop("Unexpected '}' encountered", EX_DATAERR);
1145 /* NOTREACHED */
1146 }
1147 }
1148 ;
1149
1150 f1_opcode:
1151 T_AND { $$ = AIC_OP_AND; }
1152 | T_XOR { $$ = AIC_OP_XOR; }
1153 | T_ADD { $$ = AIC_OP_ADD; }
1154 | T_ADC { $$ = AIC_OP_ADC; }
1155 ;
1156
1157 code:
1158 f1_opcode destination ',' immediate_or_a opt_source ret ';'
1159 {
1160 format_1_instr($1, &$2, &$4, &$5, $6);
1161 }
1162 ;
1163
1164 code:
1165 T_OR reg_symbol ',' immediate_or_a opt_source ret ';'
1166 {
1167 format_1_instr(AIC_OP_OR, &$2, &$4, &$5, $6);
1168 }
1169 ;
1170
1171 code:
1172 T_INC destination opt_source ret ';'
1173 {
1174 expression_t immed;
1175
1176 make_expression(&immed, 1);
1177 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1178 }
1179 ;
1180
1181 code:
1182 T_DEC destination opt_source ret ';'
1183 {
1184 expression_t immed;
1185
1186 make_expression(&immed, -1);
1187 format_1_instr(AIC_OP_ADD, &$2, &immed, &$3, $4);
1188 }
1189 ;
1190
1191 code:
1192 T_CLC ret ';'
1193 {
1194 expression_t immed;
1195
1196 make_expression(&immed, -1);
1197 format_1_instr(AIC_OP_ADD, &none, &immed, &allzeros, $2);
1198 }
1199 | T_CLC T_MVI destination ',' immediate_or_a ret ';'
1200 {
1201 format_1_instr(AIC_OP_ADD, &$3, &$5, &allzeros, $6);
1202 }
1203 ;
1204
1205 code:
1206 T_STC ret ';'
1207 {
1208 expression_t immed;
1209
1210 make_expression(&immed, 1);
1211 format_1_instr(AIC_OP_ADD, &none, &immed, &allones, $2);
1212 }
1213 | T_STC destination ret ';'
1214 {
1215 expression_t immed;
1216
1217 make_expression(&immed, 1);
1218 format_1_instr(AIC_OP_ADD, &$2, &immed, &allones, $3);
1219 }
1220 ;
1221
1222 code:
1223 T_BMOV destination ',' source ',' immediate ret ';'
1224 {
1225 format_1_instr(AIC_OP_BMOV, &$2, &$6, &$4, $7);
1226 }
1227 ;
1228
1229 code:
1230 T_MOV destination ',' source ret ';'
1231 {
1232 expression_t immed;
1233
1234 make_expression(&immed, 1);
1235 format_1_instr(AIC_OP_BMOV, &$2, &immed, &$4, $5);
1236 }
1237 ;
1238
1239 code:
1240 T_MVI destination ',' immediate ret ';'
1241 {
1242 if ($4.value == 0
1243 && is_download_const(&$4) == 0) {
1244 expression_t immed;
1245
1246 /*
1247 * Allow move immediates of 0 so that macros,
1248 * that can't know the immediate's value and
1249 * otherwise compensate, still work.
1250 */
1251 make_expression(&immed, 1);
1252 format_1_instr(AIC_OP_BMOV, &$2, &immed, &allzeros, $5);
1253 } else {
1254 format_1_instr(AIC_OP_OR, &$2, &$4, &allzeros, $5);
1255 }
1256 }
1257 ;
1258
1259 code:
1260 T_NOT destination opt_source ret ';'
1261 {
1262 expression_t immed;
1263
1264 make_expression(&immed, 0xff);
1265 format_1_instr(AIC_OP_XOR, &$2, &immed, &$3, $4);
1266 }
1267 ;
1268
1269 code:
1270 T_CLR destination ret ';'
1271 {
1272 expression_t immed;
1273
1274 make_expression(&immed, 0xff);
1275 format_1_instr(AIC_OP_AND, &$2, &immed, &allzeros, $3);
1276 }
1277 ;
1278
1279 code:
1280 T_NOP ret ';'
1281 {
1282 expression_t immed;
1283
1284 make_expression(&immed, 0xff);
1285 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, $2);
1286 }
1287 ;
1288
1289 code:
1290 T_RET ';'
1291 {
1292 expression_t immed;
1293
1294 make_expression(&immed, 0xff);
1295 format_1_instr(AIC_OP_AND, &none, &immed, &allzeros, TRUE);
1296 }
1297 ;
1298
1299 /*
1300 * This grammer differs from the one in the aic7xxx
1301 * reference manual since the grammer listed there is
1302 * ambiguous and causes a shift/reduce conflict.
1303 * It also seems more logical as the "immediate"
1304 * argument is listed as the second arg like the
1305 * other formats.
1306 */
1307
1308 f2_opcode:
1309 T_SHL { $$ = AIC_OP_SHL; }
1310 | T_SHR { $$ = AIC_OP_SHR; }
1311 | T_ROL { $$ = AIC_OP_ROL; }
1312 | T_ROR { $$ = AIC_OP_ROR; }
1313 ;
1314
1315 /*
1316 * 16bit opcodes, not used
1317 *
1318 *f4_opcode:
1319 * T_OR16 { $$ = AIC_OP_OR16; }
1320 *| T_AND16 { $$ = AIC_OP_AND16; }
1321 *| T_XOR16 { $$ = AIC_OP_XOR16; }
1322 *| T_ADD16 { $$ = AIC_OP_ADD16; }
1323 *| T_ADC16 { $$ = AIC_OP_ADC16; }
1324 *| T_MVI16 { $$ = AIC_OP_MVI16; }
1325 *;
1326 */
1327
1328 code:
1329 f2_opcode destination ',' expression opt_source ret ';'
1330 {
1331 format_2_instr($1, &$2, &$4, &$5, $6);
1332 }
1333 ;
1334
1335 jmp_jc_jnc_call:
1336 T_JMP { $$ = AIC_OP_JMP; }
1337 | T_JC { $$ = AIC_OP_JC; }
1338 | T_JNC { $$ = AIC_OP_JNC; }
1339 | T_CALL { $$ = AIC_OP_CALL; }
1340 ;
1341
1342 jz_jnz:
1343 T_JZ { $$ = AIC_OP_JZ; }
1344 | T_JNZ { $$ = AIC_OP_JNZ; }
1345 ;
1346
1347 je_jne:
1348 T_JE { $$ = AIC_OP_JE; }
1349 | T_JNE { $$ = AIC_OP_JNE; }
1350 ;
1351
1352 code:
1353 jmp_jc_jnc_call address ';'
1354 {
1355 expression_t immed;
1356
1357 make_expression(&immed, 0);
1358 format_3_instr($1, &sindex, &immed, &$2);
1359 }
1360 ;
1361
1362 code:
1363 T_OR reg_symbol ',' immediate jmp_jc_jnc_call address ';'
1364 {
1365 type_check(&$2, &$4, AIC_OP_OR);
1366 format_3_instr($5, &$2, &$4, &$6);
1367 }
1368 ;
1369
1370 code:
1371 T_TEST source ',' immediate_or_a jz_jnz address ';'
1372 {
1373 format_3_instr($5, &$2, &$4, &$6);
1374 }
1375 ;
1376
1377 code:
1378 T_CMP source ',' immediate_or_a je_jne address ';'
1379 {
1380 format_3_instr($5, &$2, &$4, &$6);
1381 }
1382 ;
1383
1384 code:
1385 T_MOV source jmp_jc_jnc_call address ';'
1386 {
1387 expression_t immed;
1388
1389 make_expression(&immed, 0);
1390 format_3_instr($3, &$2, &immed, &$4);
1391 }
1392 ;
1393
1394 code:
1395 T_MVI immediate jmp_jc_jnc_call address ';'
1396 {
1397 format_3_instr($3, &allzeros, &$2, &$4);
1398 }
1399 ;
1400
1401 %%
1402
1403 static void
1404 process_field(int field_type, symbol_t *sym, int value)
1405 {
1406 /*
1407 * Add the current register to its
1408 * symbol list, if it already exists,
1409 * warn if we are setting it to a
1410 * different value, or in the bit to
1411 * the "allowed bits" of this register.
1412 */
1413 if (sym->type == UNINITIALIZED) {
1414 sym->type = field_type;
1415 initialize_symbol(sym);
1416 sym->info.finfo->value = value;
1417 if (field_type != ENUM_ENTRY) {
1418 if (field_type != MASK && value == 0) {
1419 stop("Empty Field, or Enum", EX_DATAERR);
1420 /* NOTREACHED */
1421 }
1422 sym->info.finfo->value = value;
1423 sym->info.finfo->mask = value;
1424 } else if (field_symbol != NULL) {
1425 sym->info.finfo->mask = field_symbol->info.finfo->value;
1426 } else {
1427 sym->info.finfo->mask = 0xFF;
1428 }
1429 } else if (sym->type != field_type) {
1430 stop("Field definition mirrors a definition of the same "
1431 " name, but a different type", EX_DATAERR);
1432 /* NOTREACHED */
1433 } else if (value != sym->info.finfo->value) {
1434 stop("Field redefined with a conflicting value", EX_DATAERR);
1435 /* NOTREACHED */
1436 }
1437 /* Fail if this symbol is already listed */
1438 if (symlist_search(&(sym->info.finfo->symrefs),
1439 cur_symbol->name) != NULL) {
1440 stop("Field defined multiple times for register", EX_DATAERR);
1441 /* NOTREACHED */
1442 }
1443 symlist_add(&(sym->info.finfo->symrefs), cur_symbol,
1444 SYMLIST_INSERT_HEAD);
1445 cur_symbol->info.rinfo->valid_bitmask |= sym->info.finfo->mask;
1446 cur_symbol->info.rinfo->typecheck_masks = TRUE;
1447 symlist_add(&(cur_symbol->info.rinfo->fields), sym, SYMLIST_SORT);
1448 }
1449
1450 static void
1451 initialize_symbol(symbol_t *symbol)
1452 {
1453 switch (symbol->type) {
1454 case UNINITIALIZED:
1455 stop("Call to initialize_symbol with type field unset",
1456 EX_SOFTWARE);
1457 /* NOTREACHED */
1458 break;
1459 case REGISTER:
1460 case SRAMLOC:
1461 case SCBLOC:
1462 symbol->info.rinfo =
1463 (struct reg_info *)malloc(sizeof(struct reg_info));
1464 if (symbol->info.rinfo == NULL) {
1465 stop("Can't create register info", EX_SOFTWARE);
1466 /* NOTREACHED */
1467 }
1468 memset(symbol->info.rinfo, 0,
1469 sizeof(struct reg_info));
1470 SLIST_INIT(&(symbol->info.rinfo->fields));
1471 /*
1472 * Default to allowing access in all register modes
1473 * or to the mode specified by the SCB or SRAM space
1474 * we are in.
1475 */
1476 if (scb_or_sram_symbol != NULL)
1477 symbol->info.rinfo->modes =
1478 scb_or_sram_symbol->info.rinfo->modes;
1479 else
1480 symbol->info.rinfo->modes = ~0;
1481 break;
1482 case ALIAS:
1483 symbol->info.ainfo =
1484 (struct alias_info *)malloc(sizeof(struct alias_info));
1485 if (symbol->info.ainfo == NULL) {
1486 stop("Can't create alias info", EX_SOFTWARE);
1487 /* NOTREACHED */
1488 }
1489 memset(symbol->info.ainfo, 0,
1490 sizeof(struct alias_info));
1491 break;
1492 case MASK:
1493 case FIELD:
1494 case ENUM:
1495 case ENUM_ENTRY:
1496 symbol->info.finfo =
1497 (struct field_info *)malloc(sizeof(struct field_info));
1498 if (symbol->info.finfo == NULL) {
1499 stop("Can't create field info", EX_SOFTWARE);
1500 /* NOTREACHED */
1501 }
1502 memset(symbol->info.finfo, 0, sizeof(struct field_info));
1503 SLIST_INIT(&(symbol->info.finfo->symrefs));
1504 break;
1505 case CONST:
1506 case DOWNLOAD_CONST:
1507 symbol->info.cinfo =
1508 (struct const_info *)malloc(sizeof(struct const_info));
1509 if (symbol->info.cinfo == NULL) {
1510 stop("Can't create alias info", EX_SOFTWARE);
1511 /* NOTREACHED */
1512 }
1513 memset(symbol->info.cinfo, 0,
1514 sizeof(struct const_info));
1515 break;
1516 case LABEL:
1517 symbol->info.linfo =
1518 (struct label_info *)malloc(sizeof(struct label_info));
1519 if (symbol->info.linfo == NULL) {
1520 stop("Can't create label info", EX_SOFTWARE);
1521 /* NOTREACHED */
1522 }
1523 memset(symbol->info.linfo, 0,
1524 sizeof(struct label_info));
1525 break;
1526 case CONDITIONAL:
1527 symbol->info.condinfo =
1528 (struct cond_info *)malloc(sizeof(struct cond_info));
1529 if (symbol->info.condinfo == NULL) {
1530 stop("Can't create conditional info", EX_SOFTWARE);
1531 /* NOTREACHED */
1532 }
1533 memset(symbol->info.condinfo, 0,
1534 sizeof(struct cond_info));
1535 break;
1536 case MACRO:
1537 symbol->info.macroinfo =
1538 (struct macro_info *)malloc(sizeof(struct macro_info));
1539 if (symbol->info.macroinfo == NULL) {
1540 stop("Can't create macro info", EX_SOFTWARE);
1541 /* NOTREACHED */
1542 }
1543 memset(symbol->info.macroinfo, 0,
1544 sizeof(struct macro_info));
1545 STAILQ_INIT(&symbol->info.macroinfo->args);
1546 break;
1547 default:
1548 stop("Call to initialize_symbol with invalid symbol type",
1549 EX_SOFTWARE);
1550 /* NOTREACHED */
1551 break;
1552 }
1553 }
1554
1555 static void
1556 add_macro_arg(const char *argtext, int argnum)
1557 {
1558 struct macro_arg *marg;
1559 int i;
1560 int retval;
1561
1562 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1563 stop("Invalid current symbol for adding macro arg",
1564 EX_SOFTWARE);
1565 /* NOTREACHED */
1566 }
1567
1568 marg = (struct macro_arg *)malloc(sizeof(*marg));
1569 if (marg == NULL) {
1570 stop("Can't create macro_arg structure", EX_SOFTWARE);
1571 /* NOTREACHED */
1572 }
1573 marg->replacement_text = NULL;
1574 retval = snprintf(regex_pattern, sizeof(regex_pattern),
1575 "[^-/A-Za-z0-9_](%s)([^-/A-Za-z0-9_]|$)",
1576 argtext);
1577 if (retval >= sizeof(regex_pattern)) {
1578 stop("Regex text buffer too small for arg",
1579 EX_SOFTWARE);
1580 /* NOTREACHED */
1581 }
1582 retval = regcomp(&marg->arg_regex, regex_pattern, REG_EXTENDED);
1583 if (retval != 0) {
1584 stop("Regex compilation failed", EX_SOFTWARE);
1585 /* NOTREACHED */
1586 }
1587 STAILQ_INSERT_TAIL(&cur_symbol->info.macroinfo->args, marg, links);
1588 }
1589
1590 static void
1591 add_macro_body(const char *bodytext)
1592 {
1593 if (cur_symbol == NULL || cur_symbol->type != MACRO) {
1594 stop("Invalid current symbol for adding macro arg",
1595 EX_SOFTWARE);
1596 /* NOTREACHED */
1597 }
1598 cur_symbol->info.macroinfo->body = strdup(bodytext);
1599 if (cur_symbol->info.macroinfo->body == NULL) {
1600 stop("Can't duplicate macro body text", EX_SOFTWARE);
1601 /* NOTREACHED */
1602 }
1603 }
1604
1605 static void
1606 process_register(symbol_t **p_symbol)
1607 {
1608 symbol_t *symbol = *p_symbol;
1609
1610 if (symbol->type == UNINITIALIZED) {
1611 snprintf(errbuf, sizeof(errbuf), "Undefined register %s",
1612 symbol->name);
1613 stop(errbuf, EX_DATAERR);
1614 /* NOTREACHED */
1615 } else if (symbol->type == ALIAS) {
1616 *p_symbol = symbol->info.ainfo->parent;
1617 } else if ((symbol->type != REGISTER)
1618 && (symbol->type != SCBLOC)
1619 && (symbol->type != SRAMLOC)) {
1620 snprintf(errbuf, sizeof(errbuf),
1621 "Specified symbol %s is not a register",
1622 symbol->name);
1623 stop(errbuf, EX_DATAERR);
1624 }
1625 }
1626
1627 static void
1628 format_1_instr(int opcode, symbol_ref_t *dest, expression_t *immed,
1629 symbol_ref_t *src, int ret)
1630 {
1631 struct instruction *instr;
1632 struct ins_format1 *f1_instr;
1633
1634 if (src->symbol == NULL)
1635 src = dest;
1636
1637 /* Test register permissions */
1638 test_writable_symbol(dest->symbol);
1639 test_readable_symbol(src->symbol);
1640
1641 if (!is_location_address(dest->symbol)) {
1642 /* Ensure that immediate makes sense for this destination */
1643 type_check(dest, immed, opcode);
1644 }
1645
1646 /* Allocate sequencer space for the instruction and fill it out */
1647 instr = seq_alloc();
1648 f1_instr = &instr->format.format1;
1649 f1_instr->ret = ret ? 1 : 0;
1650 f1_instr->opcode = opcode;
1651 f1_instr->destination = dest->symbol->info.rinfo->address
1652 + dest->offset;
1653 f1_instr->source = src->symbol->info.rinfo->address
1654 + src->offset;
1655 f1_instr->immediate = immed->value;
1656
1657 if (is_download_const(immed))
1658 f1_instr->parity = 1;
1659 else if (dest->symbol == mode_ptr.symbol) {
1660 u_int src_value;
1661 u_int dst_value;
1662
1663 /*
1664 * Attempt to update mode information if
1665 * we are operating on the mode register.
1666 */
1667 if (src->symbol == allones.symbol)
1668 src_value = 0xFF;
1669 else if (src->symbol == allzeros.symbol)
1670 src_value = 0;
1671 else if (src->symbol == mode_ptr.symbol)
1672 src_value = (dst_mode << 4) | src_mode;
1673 else
1674 goto cant_update;
1675
1676 switch (opcode) {
1677 case AIC_OP_AND:
1678 dst_value = src_value & immed->value;
1679 break;
1680 case AIC_OP_XOR:
1681 dst_value = src_value ^ immed->value;
1682 break;
1683 case AIC_OP_ADD:
1684 dst_value = (src_value + immed->value) & 0xFF;
1685 break;
1686 case AIC_OP_OR:
1687 dst_value = src_value | immed->value;
1688 break;
1689 case AIC_OP_BMOV:
1690 dst_value = src_value;
1691 break;
1692 default:
1693 goto cant_update;
1694 }
1695 src_mode = dst_value & 0xF;
1696 dst_mode = (dst_value >> 4) & 0xF;
1697 }
1698
1699 cant_update:
1700 symlist_free(&immed->referenced_syms);
1701 instruction_ptr++;
1702 }
1703
1704 static void
1705 format_2_instr(int opcode, symbol_ref_t *dest, expression_t *places,
1706 symbol_ref_t *src, int ret)
1707 {
1708 struct instruction *instr;
1709 struct ins_format2 *f2_instr;
1710 uint8_t shift_control;
1711
1712 if (src->symbol == NULL)
1713 src = dest;
1714
1715 /* Test register permissions */
1716 test_writable_symbol(dest->symbol);
1717 test_readable_symbol(src->symbol);
1718
1719 /* Allocate sequencer space for the instruction and fill it out */
1720 instr = seq_alloc();
1721 f2_instr = &instr->format.format2;
1722 f2_instr->ret = ret ? 1 : 0;
1723 f2_instr->opcode = AIC_OP_ROL;
1724 f2_instr->destination = dest->symbol->info.rinfo->address
1725 + dest->offset;
1726 f2_instr->source = src->symbol->info.rinfo->address
1727 + src->offset;
1728 if (places->value > 8 || places->value <= 0) {
1729 stop("illegal shift value", EX_DATAERR);
1730 /* NOTREACHED */
1731 }
1732 switch (opcode) {
1733 case AIC_OP_SHL:
1734 if (places->value == 8)
1735 shift_control = 0xf0;
1736 else
1737 shift_control = (places->value << 4) | places->value;
1738 break;
1739 case AIC_OP_SHR:
1740 if (places->value == 8) {
1741 shift_control = 0xf8;
1742 } else {
1743 shift_control = (places->value << 4)
1744 | (8 - places->value)
1745 | 0x08;
1746 }
1747 break;
1748 case AIC_OP_ROL:
1749 shift_control = places->value & 0x7;
1750 break;
1751 case AIC_OP_ROR:
1752 shift_control = (8 - places->value) | 0x08;
1753 break;
1754 default:
1755 shift_control = 0; /* Quiet Compiler */
1756 stop("Invalid shift operation specified", EX_SOFTWARE);
1757 /* NOTREACHED */
1758 break;
1759 };
1760 f2_instr->shift_control = shift_control;
1761 symlist_free(&places->referenced_syms);
1762 instruction_ptr++;
1763 }
1764
1765 static void
1766 format_3_instr(int opcode, symbol_ref_t *src,
1767 expression_t *immed, symbol_ref_t *address)
1768 {
1769 struct instruction *instr;
1770 struct ins_format3 *f3_instr;
1771 int addr;
1772
1773 /* Test register permissions */
1774 test_readable_symbol(src->symbol);
1775
1776 /* Allocate sequencer space for the instruction and fill it out */
1777 instr = seq_alloc();
1778 f3_instr = &instr->format.format3;
1779 if (address->symbol == NULL) {
1780 /* 'dot' referrence. Use the current instruction pointer */
1781 addr = instruction_ptr + address->offset;
1782 } else if (address->symbol->type == UNINITIALIZED) {
1783 /* forward reference */
1784 addr = address->offset;
1785 instr->patch_label = address->symbol;
1786 } else
1787 addr = address->symbol->info.linfo->address + address->offset;
1788 f3_instr->opcode = opcode;
1789 f3_instr->address = addr;
1790 f3_instr->source = src->symbol->info.rinfo->address
1791 + src->offset;
1792 f3_instr->immediate = immed->value;
1793
1794 if (is_download_const(immed))
1795 f3_instr->parity = 1;
1796
1797 symlist_free(&immed->referenced_syms);
1798 instruction_ptr++;
1799 }
1800
1801 static void
1802 test_readable_symbol(symbol_t *symbol)
1803 {
1804 if ((symbol->info.rinfo->modes & (0x1 << src_mode)) == 0) {
1805 snprintf(errbuf, sizeof(errbuf),
1806 "Register %s unavailable in source reg mode %d",
1807 symbol->name, src_mode);
1808 stop(errbuf, EX_DATAERR);
1809 }
1810
1811 if (symbol->info.rinfo->mode == WO) {
1812 stop("Write Only register specified as source",
1813 EX_DATAERR);
1814 /* NOTREACHED */
1815 }
1816 }
1817
1818 static void
1819 test_writable_symbol(symbol_t *symbol)
1820 {
1821 if ((symbol->info.rinfo->modes & (0x1 << dst_mode)) == 0) {
1822 snprintf(errbuf, sizeof(errbuf),
1823 "Register %s unavailable in destination reg mode %d",
1824 symbol->name, dst_mode);
1825 stop(errbuf, EX_DATAERR);
1826 }
1827
1828 if (symbol->info.rinfo->mode == RO) {
1829 stop("Read Only register specified as destination",
1830 EX_DATAERR);
1831 /* NOTREACHED */
1832 }
1833 }
1834
1835 static void
1836 type_check(symbol_ref_t *sym, expression_t *expression, int opcode)
1837 {
1838 symbol_t *symbol = sym->symbol;
1839 symbol_node_t *node;
1840 int and_op;
1841 int8_t value, mask;
1842
1843 and_op = FALSE;
1844 /*
1845 * Make sure that we aren't attempting to write something
1846 * that hasn't been defined. If this is an and operation,
1847 * this is a mask, so "undefined" bits are okay.
1848 */
1849 if (opcode == AIC_OP_AND || opcode == AIC_OP_JNZ ||
1850 opcode == AIC_OP_JZ || opcode == AIC_OP_JNE ||
1851 opcode == AIC_OP_BMOV)
1852 and_op = TRUE;
1853
1854 /*
1855 * Defaulting to 8 bit logic
1856 */
1857 mask = (int8_t)~symbol->info.rinfo->valid_bitmask;
1858 value = (int8_t)expression->value;
1859
1860 if (and_op == FALSE && (mask & value) != 0 ) {
1861 snprintf(errbuf, sizeof(errbuf),
1862 "Invalid bit(s) 0x%x in immediate written to %s",
1863 (mask & value),
1864 symbol->name);
1865 stop(errbuf, EX_DATAERR);
1866 /* NOTREACHED */
1867 }
1868
1869 /*
1870 * Now make sure that all of the symbols referenced by the
1871 * expression are defined for this register.
1872 */
1873 if (symbol->info.rinfo->typecheck_masks != FALSE) {
1874 for(node = expression->referenced_syms.slh_first;
1875 node != NULL;
1876 node = node->links.sle_next) {
1877 if ((node->symbol->type == MASK
1878 || node->symbol->type == FIELD
1879 || node->symbol->type == ENUM
1880 || node->symbol->type == ENUM_ENTRY)
1881 && symlist_search(&node->symbol->info.finfo->symrefs,
1882 symbol->name) == NULL) {
1883 snprintf(errbuf, sizeof(errbuf),
1884 "Invalid field or mask %s "
1885 "for register %s",
1886 node->symbol->name, symbol->name);
1887 stop(errbuf, EX_DATAERR);
1888 /* NOTREACHED */
1889 }
1890 }
1891 }
1892 }
1893
1894 static void
1895 make_expression(expression_t *immed, int value)
1896 {
1897 SLIST_INIT(&immed->referenced_syms);
1898 immed->value = value & 0xff;
1899 }
1900
1901 static void
1902 add_conditional(symbol_t *symbol)
1903 {
1904 static int numfuncs;
1905
1906 if (numfuncs == 0) {
1907 /* add a special conditional, "0" */
1908 symbol_t *false_func;
1909
1910 false_func = symtable_get("0");
1911 if (false_func->type != UNINITIALIZED) {
1912 stop("Conditional expression '0' "
1913 "conflicts with a symbol", EX_DATAERR);
1914 /* NOTREACHED */
1915 }
1916 false_func->type = CONDITIONAL;
1917 initialize_symbol(false_func);
1918 false_func->info.condinfo->func_num = numfuncs++;
1919 symlist_add(&patch_functions, false_func, SYMLIST_INSERT_HEAD);
1920 }
1921
1922 /* This condition has occurred before */
1923 if (symbol->type == CONDITIONAL)
1924 return;
1925
1926 if (symbol->type != UNINITIALIZED) {
1927 stop("Conditional expression conflicts with a symbol",
1928 EX_DATAERR);
1929 /* NOTREACHED */
1930 }
1931
1932 symbol->type = CONDITIONAL;
1933 initialize_symbol(symbol);
1934 symbol->info.condinfo->func_num = numfuncs++;
1935 symlist_add(&patch_functions, symbol, SYMLIST_INSERT_HEAD);
1936 }
1937
1938 static void
1939 add_version(const char *verstring)
1940 {
1941 const char prefix[] = " * ";
1942 int newlen;
1943 int oldlen;
1944
1945 newlen = strlen(verstring) + strlen(prefix);
1946 oldlen = 0;
1947 if (versions != NULL)
1948 oldlen = strlen(versions);
1949 versions = realloc(versions, newlen + oldlen + 2);
1950 if (versions == NULL)
1951 stop("Can't allocate version string", EX_SOFTWARE);
1952 strcpy(&versions[oldlen], prefix);
1953 strcpy(&versions[oldlen + strlen(prefix)], verstring);
1954 versions[newlen + oldlen] = '\n';
1955 versions[newlen + oldlen + 1] = '\0';
1956 }
1957
1958 void
1959 yyerror(const char *string)
1960 {
1961 stop(string, EX_DATAERR);
1962 }
1963
1964 static int
1965 is_download_const(expression_t *immed)
1966 {
1967 if ((immed->referenced_syms.slh_first != NULL)
1968 && (immed->referenced_syms.slh_first->symbol->type == DOWNLOAD_CONST))
1969 return (TRUE);
1970
1971 return (FALSE);
1972 }
1973
1974 static int
1975 is_location_address(symbol_t *sym)
1976 {
1977 if (sym->type == SCBLOC ||
1978 sym->type == SRAMLOC)
1979 return (TRUE);
1980 return (FALSE);
1981 }
1982