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