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