Rename bcm43xx -> b43
[b43-tools.git] / assembler / parser.y
1 %{
2
3 /*
4  *   Copyright (C) 2006-2007  Michael Buesch <mb@bu3sch.de>
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License version 2
8  *   as published by the Free Software Foundation.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  */
15
16 #include "main.h"
17 #include "initvals.h"
18 #include "util.h"
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdint.h>
24
25 extern char *yytext;
26 extern void yyerror(char *);
27 extern int yyparse(void);
28 extern int yylex(void);
29
30 static struct operand * store_oper_sanity(struct operand *oper);
31
32 /* The current .section */
33 extern int section;
34 /* Pointer to the current initvals section data structure. */
35 extern struct initvals_sect *cur_initvals_sect;
36
37 %}
38
39 %token SECTION_TEXT SECTION_IVALS
40
41 %token ASM_ARCH ASM_START SPR GPR OFFR LR COMMA BRACK_OPEN BRACK_CLOSE PAREN_OPEN PAREN_CLOSE HEXNUM DECNUM ARCH_NEWWORLD ARCH_OLDWORLD LABEL IDENT LABELREF
42
43 %token PLUS MINUS MULTIPLY DIVIDE BITW_OR BITW_AND BITW_XOR BITW_NOT LEFTSHIFT RIGHTSHIFT
44
45 %token OP_ADD OP_ADDSC OP_ADDC OP_ADDSCC OP_SUB OP_SUBSC OP_SUBC OP_SUBSCC OP_SRA OP_OR OP_AND OP_XOR OP_SR OP_SRX OP_SL OP_RL OP_RR OP_NAND OP_ORX OP_MOV OP_JMP OP_JAND OP_JNAND OP_JS OP_JNS OP_JE OP_JNE OP_JLS OP_JGES OP_JGS OP_JLES OP_JL OP_JGE OP_JG OP_JLE OP_JZX OP_JNZX OP_JEXT OP_JNEXT OP_CALL OP_RET OP_TKIPH OP_TKIPHS OP_TKIPL OP_TKIPLS OP_NAP RAW_CODE
46
47 %token IVAL_MMIO16 IVAL_MMIO32 IVAL_PHY IVAL_RADIO IVAL_SHM16 IVAL_SHM32
48
49 %start line
50
51 %%
52
53 line    : /* empty */
54         | line statement {
55                 struct statement *s = $2;
56                 if (section != SECTION_TEXT)
57                         yyerror("Microcode text instruction in non .text section");
58                 memcpy(&s->info, &cur_lineinfo, sizeof(struct lineinfo));
59                 list_add_tail(&s->list, &infile.sl);
60           }
61         | line section_switch {
62           }
63         | line ivals_write {
64                 struct initval_op *io = $2;
65                 if (section != SECTION_IVALS)
66                         yyerror("InitVals write in non .initvals section");
67                 memcpy(&io->info, &cur_lineinfo, sizeof(struct lineinfo));
68                 INIT_LIST_HEAD(&io->list);
69                 list_add_tail(&io->list, &cur_initvals_sect->ops);
70           }
71         ;
72
73 section_switch  : SECTION_TEXT {
74                         section = SECTION_TEXT;
75                   }
76                 | SECTION_IVALS PAREN_OPEN identifier PAREN_CLOSE {
77                         const char *sectname = $3;
78                         struct initvals_sect *s;
79                         cur_initvals_sect = NULL;
80                         /* Search if there already is a section by that name. */
81                         list_for_each_entry(s, &infile.ivals, list) {
82                                 if (strcmp(sectname, s->name) == 0)
83                                         cur_initvals_sect = s;
84                         }
85                         if (!cur_initvals_sect) {
86                                 /* Not found, create a new one. */
87                                 s = xmalloc(sizeof(struct initvals_sect));
88                                 s->name = sectname;
89                                 INIT_LIST_HEAD(&s->ops);
90                                 INIT_LIST_HEAD(&s->list);
91                                 list_add_tail(&s->list, &infile.ivals);
92                                 cur_initvals_sect = s;
93                         }
94                         section = SECTION_IVALS;
95                   }
96                 ;
97
98 ivals_write     : IVAL_MMIO16 hexnum_decnum COMMA hexnum_decnum {
99                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
100                         iop->type = IVAL_W_MMIO16;
101                         iop->args[0] = (unsigned int)(unsigned long)$2;
102                         iop->args[1] = (unsigned int)(unsigned long)$4;
103                         $$ = iop;
104                   }
105                 | IVAL_MMIO32 hexnum_decnum COMMA hexnum_decnum {
106                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
107                         iop->type = IVAL_W_MMIO32;
108                         iop->args[0] = (unsigned int)(unsigned long)$2;
109                         iop->args[1] = (unsigned int)(unsigned long)$4;
110                         $$ = iop;
111                   }
112                 | IVAL_PHY hexnum_decnum COMMA hexnum_decnum {
113                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
114                         iop->type = IVAL_W_PHY;
115                         iop->args[0] = (unsigned int)(unsigned long)$2;
116                         iop->args[1] = (unsigned int)(unsigned long)$4;
117                         $$ = iop;
118                   }
119                 | IVAL_RADIO hexnum_decnum COMMA hexnum_decnum {
120                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
121                         iop->type = IVAL_W_RADIO;
122                         iop->args[0] = (unsigned int)(unsigned long)$2;
123                         iop->args[1] = (unsigned int)(unsigned long)$4;
124                         $$ = iop;
125                   }
126                 | IVAL_SHM16 hexnum_decnum COMMA hexnum_decnum COMMA hexnum_decnum {
127                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
128                         iop->type = IVAL_W_SHM16;
129                         iop->args[0] = (unsigned int)(unsigned long)$2;
130                         iop->args[1] = (unsigned int)(unsigned long)$4;
131                         iop->args[2] = (unsigned int)(unsigned long)$6;
132                         $$ = iop;
133                   }
134                 | IVAL_SHM32 hexnum_decnum COMMA hexnum_decnum COMMA hexnum_decnum {
135                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
136                         iop->type = IVAL_W_SHM32;
137                         iop->args[0] = (unsigned int)(unsigned long)$2;
138                         iop->args[1] = (unsigned int)(unsigned long)$4;
139                         iop->args[2] = (unsigned int)(unsigned long)$6;
140                         $$ = iop;
141                   }
142                 ;
143
144 statement       : asmdir {
145                         struct statement *s = xmalloc(sizeof(struct statement));
146                         INIT_LIST_HEAD(&s->list);
147                         s->type = STMT_ASMDIR;
148                         s->u.asmdir = $1;
149                         $$ = s;
150                   }
151                 | label {
152                         struct statement *s = xmalloc(sizeof(struct statement));
153                         INIT_LIST_HEAD(&s->list);
154                         s->type = STMT_LABEL;
155                         s->u.label = $1;
156                         $$ = s;
157                   }
158                 | insn_add {
159                         struct statement *s = xmalloc(sizeof(struct statement));
160                         INIT_LIST_HEAD(&s->list);
161                         s->type = STMT_INSN;
162                         s->u.insn = $1;
163                         $$ = s;
164                   }
165                 | insn_addsc {
166                         struct statement *s = xmalloc(sizeof(struct statement));
167                         INIT_LIST_HEAD(&s->list);
168                         s->type = STMT_INSN;
169                         s->u.insn = $1;
170                         $$ = s;
171                   }
172                 | insn_addc {
173                         struct statement *s = xmalloc(sizeof(struct statement));
174                         INIT_LIST_HEAD(&s->list);
175                         s->type = STMT_INSN;
176                         s->u.insn = $1;
177                         $$ = s;
178                   }
179                 | insn_addscc {
180                         struct statement *s = xmalloc(sizeof(struct statement));
181                         INIT_LIST_HEAD(&s->list);
182                         s->type = STMT_INSN;
183                         s->u.insn = $1;
184                         $$ = s;
185                   }
186                 | insn_sub {
187                         struct statement *s = xmalloc(sizeof(struct statement));
188                         INIT_LIST_HEAD(&s->list);
189                         s->type = STMT_INSN;
190                         s->u.insn = $1;
191                         $$ = s;
192                   }
193                 | insn_subsc {
194                         struct statement *s = xmalloc(sizeof(struct statement));
195                         INIT_LIST_HEAD(&s->list);
196                         s->type = STMT_INSN;
197                         s->u.insn = $1;
198                         $$ = s;
199                   }
200                 | insn_subc {
201                         struct statement *s = xmalloc(sizeof(struct statement));
202                         INIT_LIST_HEAD(&s->list);
203                         s->type = STMT_INSN;
204                         s->u.insn = $1;
205                         $$ = s;
206                   }
207                 | insn_subscc {
208                         struct statement *s = xmalloc(sizeof(struct statement));
209                         INIT_LIST_HEAD(&s->list);
210                         s->type = STMT_INSN;
211                         s->u.insn = $1;
212                         $$ = s;
213                   }
214                 | insn_sra {
215                         struct statement *s = xmalloc(sizeof(struct statement));
216                         INIT_LIST_HEAD(&s->list);
217                         s->type = STMT_INSN;
218                         s->u.insn = $1;
219                         $$ = s;
220                   }
221                 | insn_or {
222                         struct statement *s = xmalloc(sizeof(struct statement));
223                         INIT_LIST_HEAD(&s->list);
224                         s->type = STMT_INSN;
225                         s->u.insn = $1;
226                         $$ = s;
227                   }
228                 | insn_and {
229                         struct statement *s = xmalloc(sizeof(struct statement));
230                         INIT_LIST_HEAD(&s->list);
231                         s->type = STMT_INSN;
232                         s->u.insn = $1;
233                         $$ = s;
234                   }
235                 | insn_xor {
236                         struct statement *s = xmalloc(sizeof(struct statement));
237                         INIT_LIST_HEAD(&s->list);
238                         s->type = STMT_INSN;
239                         s->u.insn = $1;
240                         $$ = s;
241                   }
242                 | insn_sr {
243                         struct statement *s = xmalloc(sizeof(struct statement));
244                         INIT_LIST_HEAD(&s->list);
245                         s->type = STMT_INSN;
246                         s->u.insn = $1;
247                         $$ = s;
248                   }
249                 | insn_srx {
250                         struct statement *s = xmalloc(sizeof(struct statement));
251                         INIT_LIST_HEAD(&s->list);
252                         s->type = STMT_INSN;
253                         s->u.insn = $1;
254                         $$ = s;
255                   }
256                 | insn_sl {
257                         struct statement *s = xmalloc(sizeof(struct statement));
258                         INIT_LIST_HEAD(&s->list);
259                         s->type = STMT_INSN;
260                         s->u.insn = $1;
261                         $$ = s;
262                   }
263                 | insn_rl {
264                         struct statement *s = xmalloc(sizeof(struct statement));
265                         INIT_LIST_HEAD(&s->list);
266                         s->type = STMT_INSN;
267                         s->u.insn = $1;
268                         $$ = s;
269                   }
270                 | insn_rr {
271                         struct statement *s = xmalloc(sizeof(struct statement));
272                         INIT_LIST_HEAD(&s->list);
273                         s->type = STMT_INSN;
274                         s->u.insn = $1;
275                         $$ = s;
276                   }
277                 | insn_nand {
278                         struct statement *s = xmalloc(sizeof(struct statement));
279                         INIT_LIST_HEAD(&s->list);
280                         s->type = STMT_INSN;
281                         s->u.insn = $1;
282                         $$ = s;
283                   }
284                 | insn_orx {
285                         struct statement *s = xmalloc(sizeof(struct statement));
286                         INIT_LIST_HEAD(&s->list);
287                         s->type = STMT_INSN;
288                         s->u.insn = $1;
289                         $$ = s;
290                   }
291                 | insn_mov {
292                         struct statement *s = xmalloc(sizeof(struct statement));
293                         INIT_LIST_HEAD(&s->list);
294                         s->type = STMT_INSN;
295                         s->u.insn = $1;
296                         $$ = s;
297                   }
298                 | insn_jmp {
299                         struct statement *s = xmalloc(sizeof(struct statement));
300                         INIT_LIST_HEAD(&s->list);
301                         s->type = STMT_INSN;
302                         s->u.insn = $1;
303                         $$ = s;
304                   }
305                 | insn_jand {
306                         struct statement *s = xmalloc(sizeof(struct statement));
307                         INIT_LIST_HEAD(&s->list);
308                         s->type = STMT_INSN;
309                         s->u.insn = $1;
310                         $$ = s;
311                   }
312                 | insn_jnand {
313                         struct statement *s = xmalloc(sizeof(struct statement));
314                         INIT_LIST_HEAD(&s->list);
315                         s->type = STMT_INSN;
316                         s->u.insn = $1;
317                         $$ = s;
318                   }
319                 | insn_js {
320                         struct statement *s = xmalloc(sizeof(struct statement));
321                         INIT_LIST_HEAD(&s->list);
322                         s->type = STMT_INSN;
323                         s->u.insn = $1;
324                         $$ = s;
325                   }
326                 | insn_jns {
327                         struct statement *s = xmalloc(sizeof(struct statement));
328                         INIT_LIST_HEAD(&s->list);
329                         s->type = STMT_INSN;
330                         s->u.insn = $1;
331                         $$ = s;
332                   }
333                 | insn_je {
334                         struct statement *s = xmalloc(sizeof(struct statement));
335                         INIT_LIST_HEAD(&s->list);
336                         s->type = STMT_INSN;
337                         s->u.insn = $1;
338                         $$ = s;
339                   }
340                 | insn_jne {
341                         struct statement *s = xmalloc(sizeof(struct statement));
342                         INIT_LIST_HEAD(&s->list);
343                         s->type = STMT_INSN;
344                         s->u.insn = $1;
345                         $$ = s;
346                   }
347                 | insn_jls {
348                         struct statement *s = xmalloc(sizeof(struct statement));
349                         INIT_LIST_HEAD(&s->list);
350                         s->type = STMT_INSN;
351                         s->u.insn = $1;
352                         $$ = s;
353                   }
354                 | insn_jges {
355                         struct statement *s = xmalloc(sizeof(struct statement));
356                         INIT_LIST_HEAD(&s->list);
357                         s->type = STMT_INSN;
358                         s->u.insn = $1;
359                         $$ = s;
360                   }
361                 | insn_jgs {
362                         struct statement *s = xmalloc(sizeof(struct statement));
363                         INIT_LIST_HEAD(&s->list);
364                         s->type = STMT_INSN;
365                         s->u.insn = $1;
366                         $$ = s;
367                   }
368                 | insn_jles {
369                         struct statement *s = xmalloc(sizeof(struct statement));
370                         INIT_LIST_HEAD(&s->list);
371                         s->type = STMT_INSN;
372                         s->u.insn = $1;
373                         $$ = s;
374                   }
375                 | insn_jl {
376                         struct statement *s = xmalloc(sizeof(struct statement));
377                         INIT_LIST_HEAD(&s->list);
378                         s->type = STMT_INSN;
379                         s->u.insn = $1;
380                         $$ = s;
381                   }
382                 | insn_jge {
383                         struct statement *s = xmalloc(sizeof(struct statement));
384                         INIT_LIST_HEAD(&s->list);
385                         s->type = STMT_INSN;
386                         s->u.insn = $1;
387                         $$ = s;
388                   }
389                 | insn_jg {
390                         struct statement *s = xmalloc(sizeof(struct statement));
391                         INIT_LIST_HEAD(&s->list);
392                         s->type = STMT_INSN;
393                         s->u.insn = $1;
394                         $$ = s;
395                   }
396                 | insn_jle {
397                         struct statement *s = xmalloc(sizeof(struct statement));
398                         INIT_LIST_HEAD(&s->list);
399                         s->type = STMT_INSN;
400                         s->u.insn = $1;
401                         $$ = s;
402                   }
403                 | insn_jzx {
404                         struct statement *s = xmalloc(sizeof(struct statement));
405                         INIT_LIST_HEAD(&s->list);
406                         s->type = STMT_INSN;
407                         s->u.insn = $1;
408                         $$ = s;
409                   }
410                 | insn_jnzx {
411                         struct statement *s = xmalloc(sizeof(struct statement));
412                         INIT_LIST_HEAD(&s->list);
413                         s->type = STMT_INSN;
414                         s->u.insn = $1;
415                         $$ = s;
416                   }
417                 | insn_jext {
418                         struct statement *s = xmalloc(sizeof(struct statement));
419                         INIT_LIST_HEAD(&s->list);
420                         s->type = STMT_INSN;
421                         s->u.insn = $1;
422                         $$ = s;
423                   }
424                 | insn_jnext {
425                         struct statement *s = xmalloc(sizeof(struct statement));
426                         INIT_LIST_HEAD(&s->list);
427                         s->type = STMT_INSN;
428                         s->u.insn = $1;
429                         $$ = s;
430                   }
431                 | insn_call {
432                         struct statement *s = xmalloc(sizeof(struct statement));
433                         INIT_LIST_HEAD(&s->list);
434                         s->type = STMT_INSN;
435                         s->u.insn = $1;
436                         $$ = s;
437                   }
438                 | insn_ret {
439                         struct statement *s = xmalloc(sizeof(struct statement));
440                         INIT_LIST_HEAD(&s->list);
441                         s->type = STMT_INSN;
442                         s->u.insn = $1;
443                         $$ = s;
444                   }
445                 | insn_tkiph {
446                         struct statement *s = xmalloc(sizeof(struct statement));
447                         INIT_LIST_HEAD(&s->list);
448                         s->type = STMT_INSN;
449                         s->u.insn = $1;
450                         $$ = s;
451                   }
452                 | insn_tkiphs {
453                         struct statement *s = xmalloc(sizeof(struct statement));
454                         INIT_LIST_HEAD(&s->list);
455                         s->type = STMT_INSN;
456                         s->u.insn = $1;
457                         $$ = s;
458                   }
459                 | insn_tkipl {
460                         struct statement *s = xmalloc(sizeof(struct statement));
461                         INIT_LIST_HEAD(&s->list);
462                         s->type = STMT_INSN;
463                         s->u.insn = $1;
464                         $$ = s;
465                   }
466                 | insn_tkipls {
467                         struct statement *s = xmalloc(sizeof(struct statement));
468                         INIT_LIST_HEAD(&s->list);
469                         s->type = STMT_INSN;
470                         s->u.insn = $1;
471                         $$ = s;
472                   }
473                 | insn_nap {
474                         struct statement *s = xmalloc(sizeof(struct statement));
475                         INIT_LIST_HEAD(&s->list);
476                         s->type = STMT_INSN;
477                         s->u.insn = $1;
478                         $$ = s;
479                   }
480                 | insn_raw {
481                         struct statement *s = xmalloc(sizeof(struct statement));
482                         INIT_LIST_HEAD(&s->list);
483                         s->type = STMT_INSN;
484                         s->u.insn = $1;
485                         $$ = s;
486                   }
487                 ;
488
489 /* ASM directives */
490 asmdir          : asmarch {
491                         $$ = $1;
492                   }
493                 | ASM_START identifier {
494                         struct asmdir *ad = xmalloc(sizeof(struct asmdir));
495                         struct label *label = xmalloc(sizeof(struct label));
496                         label->name = $2;
497                         label->direction = LABELREF_ABSOLUTE;
498                         ad->type = ADIR_START;
499                         ad->u.start = label;
500                         $$ = ad;
501                   }
502                 ;
503
504 asmarch         : ASM_ARCH ARCH_NEWWORLD {
505                         struct asmdir *ad = xmalloc(sizeof(struct asmdir));
506                         ad->type = ADIR_ARCH;
507                         ad->u.arch = NEWWORLD;
508                         $$ = ad;
509                   }
510                 | ASM_ARCH ARCH_OLDWORLD {
511                         struct asmdir *ad = xmalloc(sizeof(struct asmdir));
512                         ad->type = ADIR_ARCH;
513                         ad->u.arch = OLDWORLD;
514                         $$ = ad;
515                   }
516                 ;
517
518 label           : LABEL {
519                         struct label *label = xmalloc(sizeof(struct label));
520                         char *l;
521                         l = xstrdup(yytext);
522                         l[strlen(l) - 1] = '\0';
523                         label->name = l;
524                         $$ = label;
525                   }
526                 ;
527
528 /* add */
529 insn_add        : OP_ADD operlist_3 {
530                         struct instruction *insn = xmalloc(sizeof(struct instruction));
531                         insn->op = OP_ADD;
532                         insn->operands = $2;
533                         $$ = insn;
534                   }
535                 ;
536
537 /* add. */
538 insn_addsc      : OP_ADDSC operlist_3 {
539                         struct instruction *insn = xmalloc(sizeof(struct instruction));
540                         insn->op = OP_ADDSC;
541                         insn->operands = $2;
542                         $$ = insn;
543                   }
544                 ;
545
546 /* addc */
547 insn_addc       : OP_ADDC operlist_3 {
548                         struct instruction *insn = xmalloc(sizeof(struct instruction));
549                         insn->op = OP_ADDC;
550                         insn->operands = $2;
551                         $$ = insn;
552                   }
553                 ;
554
555 /* addc. */
556 insn_addscc     : OP_ADDSCC operlist_3 {
557                         struct instruction *insn = xmalloc(sizeof(struct instruction));
558                         insn->op = OP_ADDSCC;
559                         insn->operands = $2;
560                         $$ = insn;
561                   }
562                 ;
563
564 /* sub */
565 insn_sub        : OP_SUB operlist_3 {
566                         struct instruction *insn = xmalloc(sizeof(struct instruction));
567                         insn->op = OP_SUB;
568                         insn->operands = $2;
569                         $$ = insn;
570                   }
571                 ;
572
573 /* sub. */
574 insn_subsc      : OP_SUBSC operlist_3 {
575                         struct instruction *insn = xmalloc(sizeof(struct instruction));
576                         insn->op = OP_SUBSC;
577                         insn->operands = $2;
578                         $$ = insn;
579                   }
580                 ;
581
582 /* subc */
583 insn_subc       : OP_SUBC operlist_3 {
584                         struct instruction *insn = xmalloc(sizeof(struct instruction));
585                         insn->op = OP_SUBC;
586                         insn->operands = $2;
587                         $$ = insn;
588                   }
589                 ;
590
591 /* subc. */
592 insn_subscc     : OP_SUBSCC operlist_3 {
593                         struct instruction *insn = xmalloc(sizeof(struct instruction));
594                         insn->op = OP_SUBSCC;
595                         insn->operands = $2;
596                         $$ = insn;
597                   }
598                 ;
599
600 insn_sra        : OP_SRA operlist_3 {
601                         struct instruction *insn = xmalloc(sizeof(struct instruction));
602                         insn->op = OP_SRA;
603                         insn->operands = $2;
604                         $$ = insn;
605                   }
606                 ;
607
608 insn_or         : OP_OR operlist_3 {
609                         struct instruction *insn = xmalloc(sizeof(struct instruction));
610                         insn->op = OP_OR;
611                         insn->operands = $2;
612                         $$ = insn;
613                   }
614                 ;
615
616 insn_and        : OP_AND operlist_3 {
617                         struct instruction *insn = xmalloc(sizeof(struct instruction));
618                         insn->op = OP_AND;
619                         insn->operands = $2;
620                         $$ = insn;
621                   }
622                 ;
623
624 insn_xor        : OP_XOR operlist_3 {
625                         struct instruction *insn = xmalloc(sizeof(struct instruction));
626                         insn->op = OP_XOR;
627                         insn->operands = $2;
628                         $$ = insn;
629                   }
630                 ;
631
632 insn_sr         : OP_SR operlist_3 {
633                         struct instruction *insn = xmalloc(sizeof(struct instruction));
634                         insn->op = OP_SR;
635                         insn->operands = $2;
636                         $$ = insn;
637                   }
638                 ;
639
640 insn_srx        : OP_SRX extended_operlist {
641                         struct instruction *insn = xmalloc(sizeof(struct instruction));
642                         insn->op = OP_SRX;
643                         insn->operands = $2;
644                         $$ = insn;
645                   }
646                 ;
647
648 insn_sl         : OP_SL operlist_3 {
649                         struct instruction *insn = xmalloc(sizeof(struct instruction));
650                         insn->op = OP_SL;
651                         insn->operands = $2;
652                         $$ = insn;
653                   }
654                 ;
655
656 insn_rl         : OP_RL operlist_3 {
657                         struct instruction *insn = xmalloc(sizeof(struct instruction));
658                         insn->op = OP_RL;
659                         insn->operands = $2;
660                         $$ = insn;
661                   }
662                 ;
663
664 insn_rr         : OP_RR operlist_3 {
665                         struct instruction *insn = xmalloc(sizeof(struct instruction));
666                         insn->op = OP_RR;
667                         insn->operands = $2;
668                         $$ = insn;
669                   }
670                 ;
671
672 insn_nand       : OP_NAND operlist_3 {
673                         struct instruction *insn = xmalloc(sizeof(struct instruction));
674                         insn->op = OP_NAND;
675                         insn->operands = $2;
676                         $$ = insn;
677                   }
678                 ;
679
680 insn_orx        : OP_ORX extended_operlist {
681                         struct instruction *insn = xmalloc(sizeof(struct instruction));
682                         insn->op = OP_ORX;
683                         insn->operands = $2;
684                         $$ = insn;
685                   }
686                 ;
687
688 insn_mov        : OP_MOV operlist_2 {
689                         struct instruction *insn = xmalloc(sizeof(struct instruction));
690                         insn->op = OP_MOV;
691                         insn->operands = $2;
692                         $$ = insn;
693                   }
694                 ;
695
696 insn_jmp        : OP_JMP labelref {
697                         struct instruction *insn = xmalloc(sizeof(struct instruction));
698                         struct operlist *ol = xmalloc(sizeof(struct operlist));
699                         ol->oper[0] = $2;
700                         insn->op = OP_JMP;
701                         insn->operands = ol;
702                         $$ = insn;
703                   }
704                 ;
705
706 insn_jand       : OP_JAND operlist_3 {
707                         struct instruction *insn = xmalloc(sizeof(struct instruction));
708                         insn->op = OP_JAND;
709                         insn->operands = $2;
710                         $$ = insn;
711                   }
712                 ;
713
714 insn_jnand      : OP_JNAND operlist_3 {
715                         struct instruction *insn = xmalloc(sizeof(struct instruction));
716                         insn->op = OP_JNAND;
717                         insn->operands = $2;
718                         $$ = insn;
719                   }
720                 ;
721
722 insn_js         : OP_JS operlist_3 {
723                         struct instruction *insn = xmalloc(sizeof(struct instruction));
724                         insn->op = OP_JS;
725                         insn->operands = $2;
726                         $$ = insn;
727                   }
728                 ;
729
730 insn_jns        : OP_JNS operlist_3 {
731                         struct instruction *insn = xmalloc(sizeof(struct instruction));
732                         insn->op = OP_JNS;
733                         insn->operands = $2;
734                         $$ = insn;
735                   }
736                 ;
737
738 insn_je         : OP_JE operlist_3 {
739                         struct instruction *insn = xmalloc(sizeof(struct instruction));
740                         insn->op = OP_JE;
741                         insn->operands = $2;
742                         $$ = insn;
743                   }
744                 ;
745
746 insn_jne        : OP_JNE operlist_3 {
747                         struct instruction *insn = xmalloc(sizeof(struct instruction));
748                         insn->op = OP_JNE;
749                         insn->operands = $2;
750                         $$ = insn;
751                   }
752                 ;
753
754 insn_jls        : OP_JLS operlist_3 {
755                         struct instruction *insn = xmalloc(sizeof(struct instruction));
756                         insn->op = OP_JLS;
757                         insn->operands = $2;
758                         $$ = insn;
759                   }
760                 ;
761
762 insn_jges       : OP_JGES operlist_3 {
763                         struct instruction *insn = xmalloc(sizeof(struct instruction));
764                         insn->op = OP_JGES;
765                         insn->operands = $2;
766                         $$ = insn;
767                   }
768                 ;
769
770 insn_jgs        : OP_JGS operlist_3 {
771                         struct instruction *insn = xmalloc(sizeof(struct instruction));
772                         insn->op = OP_JGS;
773                         insn->operands = $2;
774                         $$ = insn;
775                   }
776                 ;
777
778 insn_jles       : OP_JLES operlist_3 {
779                         struct instruction *insn = xmalloc(sizeof(struct instruction));
780                         insn->op = OP_JLES;
781                         insn->operands = $2;
782                         $$ = insn;
783                   }
784                 ;
785
786 insn_jl         : OP_JL operlist_3 {
787                         struct instruction *insn = xmalloc(sizeof(struct instruction));
788                         insn->op = OP_JL;
789                         insn->operands = $2;
790                         $$ = insn;
791                   }
792                 ;
793
794 insn_jge        : OP_JGE operlist_3 {
795                         struct instruction *insn = xmalloc(sizeof(struct instruction));
796                         insn->op = OP_JGE;
797                         insn->operands = $2;
798                         $$ = insn;
799                   }
800                 ;
801
802 insn_jg         : OP_JG operlist_3 {
803                         struct instruction *insn = xmalloc(sizeof(struct instruction));
804                         insn->op = OP_JG;
805                         insn->operands = $2;
806                         $$ = insn;
807                   }
808                 ;
809
810 insn_jle        : OP_JLE operlist_3 {
811                         struct instruction *insn = xmalloc(sizeof(struct instruction));
812                         insn->op = OP_JLE;
813                         insn->operands = $2;
814                         $$ = insn;
815                   }
816                 ;
817
818 insn_jzx        : OP_JZX extended_operlist {
819                         struct instruction *insn = xmalloc(sizeof(struct instruction));
820                         insn->op = OP_JZX;
821                         insn->operands = $2;
822                         $$ = insn;
823                   }
824                 ;
825
826 insn_jnzx       : OP_JNZX extended_operlist {
827                         struct instruction *insn = xmalloc(sizeof(struct instruction));
828                         insn->op = OP_JNZX;
829                         insn->operands = $2;
830                         $$ = insn;
831                   }
832                 ;
833
834 insn_jext       : OP_JEXT external_jump_operands {
835                         struct instruction *insn = xmalloc(sizeof(struct instruction));
836                         insn->op = OP_JEXT;
837                         insn->operands = $2;
838                         $$ = insn;
839                   }
840                 ;
841
842 insn_jnext      : OP_JNEXT external_jump_operands {
843                         struct instruction *insn = xmalloc(sizeof(struct instruction));
844                         insn->op = OP_JNEXT;
845                         insn->operands = $2;
846                         $$ = insn;
847                   }
848                 ;
849
850 linkreg         : LR regnr {
851                         $$ = $2;
852                   }
853                 ;
854
855 insn_call       : OP_CALL linkreg COMMA labelref {
856                         struct instruction *insn = xmalloc(sizeof(struct instruction));
857                         struct operlist *ol = xmalloc(sizeof(struct operlist));
858                         struct operand *oper_lr = xmalloc(sizeof(struct operand));
859                         struct operand *oper_zero = xmalloc(sizeof(struct operand));
860                         oper_zero->type = OPER_RAW;
861                         oper_zero->u.raw = 0;
862                         oper_lr->type = OPER_RAW;
863                         oper_lr->u.raw = (unsigned long)$2;
864                         ol->oper[0] = oper_lr;
865                         ol->oper[1] = oper_zero;
866                         ol->oper[2] = $4;
867                         insn->op = OP_CALL;
868                         insn->operands = ol;
869                         $$ = insn;
870                   }
871                 ;
872
873 insn_ret        : OP_RET linkreg COMMA linkreg {
874                         struct instruction *insn = xmalloc(sizeof(struct instruction));
875                         struct operlist *ol = xmalloc(sizeof(struct operlist));
876                         struct operand *oper_lr0 = xmalloc(sizeof(struct operand));
877                         struct operand *oper_lr1 = xmalloc(sizeof(struct operand));
878                         struct operand *oper_zero = xmalloc(sizeof(struct operand));
879                         oper_zero->type = OPER_RAW;
880                         oper_zero->u.raw = 0;
881                         oper_lr0->type = OPER_RAW;
882                         oper_lr0->u.raw = (unsigned long)$2;
883                         oper_lr1->type = OPER_RAW;
884                         oper_lr1->u.raw = (unsigned long)$4;
885                         ol->oper[0] = oper_lr0;
886                         ol->oper[1] = oper_zero;
887                         ol->oper[2] = oper_lr1;
888                         insn->op = OP_RET;
889                         insn->operands = ol;
890                         $$ = insn;
891                   }
892                 ;
893
894 insn_tkiph      : OP_TKIPH operlist_2 {
895                         struct instruction *insn = xmalloc(sizeof(struct instruction));
896                         struct operlist *ol = $2;
897                         struct operand *flags = xmalloc(sizeof(struct operand));
898                         struct immediate *imm = xmalloc(sizeof(struct immediate));
899                         imm->imm = 0x1;
900                         flags->type = OPER_IMM;
901                         flags->u.imm = imm;
902                         ol->oper[2] = ol->oper[1];
903                         ol->oper[1] = flags;
904                         insn->op = OP_TKIPH;
905                         insn->operands = ol;
906                         $$ = insn;
907                   }
908                 ;
909
910 insn_tkiphs     : OP_TKIPHS operlist_2 {
911                         struct instruction *insn = xmalloc(sizeof(struct instruction));
912                         struct operlist *ol = $2;
913                         struct operand *flags = xmalloc(sizeof(struct operand));
914                         struct immediate *imm = xmalloc(sizeof(struct immediate));
915                         imm->imm = 0x1 | 0x2;
916                         flags->type = OPER_IMM;
917                         flags->u.imm = imm;
918                         ol->oper[2] = ol->oper[1];
919                         ol->oper[1] = flags;
920                         insn->op = OP_TKIPH;
921                         insn->operands = ol;
922                         $$ = insn;
923                   }
924                 ;
925
926 insn_tkipl      : OP_TKIPL operlist_2 {
927                         struct instruction *insn = xmalloc(sizeof(struct instruction));
928                         struct operlist *ol = $2;
929                         struct operand *flags = xmalloc(sizeof(struct operand));
930                         struct immediate *imm = xmalloc(sizeof(struct immediate));
931                         imm->imm = 0x0;
932                         flags->type = OPER_IMM;
933                         flags->u.imm = imm;
934                         ol->oper[2] = ol->oper[1];
935                         ol->oper[1] = flags;
936                         insn->op = OP_TKIPH;
937                         insn->operands = ol;
938                         $$ = insn;
939                   }
940                 ;
941
942 insn_tkipls     : OP_TKIPLS operlist_2 {
943                         struct instruction *insn = xmalloc(sizeof(struct instruction));
944                         struct operlist *ol = $2;
945                         struct operand *flags = xmalloc(sizeof(struct operand));
946                         struct immediate *imm = xmalloc(sizeof(struct immediate));
947                         imm->imm = 0x0 | 0x2;
948                         flags->type = OPER_IMM;
949                         flags->u.imm = imm;
950                         ol->oper[2] = ol->oper[1];
951                         ol->oper[1] = flags;
952                         insn->op = OP_TKIPH;
953                         insn->operands = ol;
954                         $$ = insn;
955                   }
956                 ;
957
958 insn_nap        : OP_NAP {
959                         struct instruction *insn = xmalloc(sizeof(struct instruction));
960                         struct operlist *ol = xmalloc(sizeof(struct operlist));
961                         struct operand *regop = xmalloc(sizeof(struct operand));
962                         struct operand *zeroop = xmalloc(sizeof(struct operand));
963                         struct registr *r0 = xmalloc(sizeof(struct registr));
964                         r0->type = GPR;
965                         r0->nr = 0;
966                         regop->type = OPER_REG;
967                         regop->u.reg = r0;
968                         zeroop->type = OPER_RAW;
969                         zeroop->u.raw = 0x000;
970                         ol->oper[0] = regop;
971                         ol->oper[1] = regop;
972                         ol->oper[2] = zeroop;
973                         insn->op = OP_NAP;
974                         insn->operands = ol;
975                         $$ = insn;
976                   }
977                 ;
978
979 insn_raw        : raw_code operlist_3 {
980                         struct instruction *insn = xmalloc(sizeof(struct instruction));
981                         insn->op = RAW_CODE;
982                         insn->operands = $2;
983                         insn->opcode = (unsigned long)$1;
984                         $$ = insn;
985                   }
986                 ;
987
988 raw_code        : RAW_CODE {
989                         yytext++; /* skip @ */
990                         $$ = (void *)(unsigned long)strtoul(yytext, NULL, 16);
991                   }
992                 ;
993
994 extended_operlist : decnum COMMA decnum COMMA operand COMMA operand COMMA operand {
995                         struct operlist *ol = xmalloc(sizeof(struct operlist));
996                         struct operand *mask_oper = xmalloc(sizeof(struct operand));
997                         struct operand *shift_oper = xmalloc(sizeof(struct operand));
998                         mask_oper->type = OPER_RAW;
999                         mask_oper->u.raw = (unsigned long)$1;
1000                         shift_oper->type = OPER_RAW;
1001                         shift_oper->u.raw = (unsigned long)$3;
1002                         ol->oper[0] = mask_oper;
1003                         ol->oper[1] = shift_oper;
1004                         ol->oper[2] = $5;
1005                         ol->oper[3] = $7;
1006                         ol->oper[4] = store_oper_sanity($9);
1007                         $$ = ol;
1008                   }
1009                 ;
1010
1011 external_jump_operands : hexnum COMMA operand COMMA operand COMMA labelref {
1012                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1013                         struct operand *cond = xmalloc(sizeof(struct operand));
1014                         cond->type = OPER_RAW;
1015                         cond->u.raw = (unsigned long)$1;
1016                         ol->oper[0] = cond;
1017                         ol->oper[1] = $3;
1018                         ol->oper[2] = $5;
1019                         ol->oper[3] = $7;
1020                         $$ = ol;
1021                   }
1022                 ;
1023
1024 operlist_2      : operand COMMA operand {
1025                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1026                         ol->oper[0] = $1;
1027                         ol->oper[1] = store_oper_sanity($3);
1028                         $$ = ol;
1029                   }
1030                 ;
1031
1032 operlist_3      : operand COMMA operand COMMA operand {
1033                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1034                         ol->oper[0] = $1;
1035                         ol->oper[1] = $3;
1036                         ol->oper[2] = store_oper_sanity($5);
1037                         $$ = ol;
1038                   }
1039                 ;
1040
1041 operand         : reg {
1042                         struct operand *oper = xmalloc(sizeof(struct operand));
1043                         oper->type = OPER_REG;
1044                         oper->u.reg = $1;
1045                         $$ = oper;
1046                   }
1047                 | mem {
1048                         struct operand *oper = xmalloc(sizeof(struct operand));
1049                         oper->type = OPER_MEM;
1050                         oper->u.mem = $1;
1051                         $$ = oper;
1052                   }
1053                 | raw_code {
1054                         struct operand *oper = xmalloc(sizeof(struct operand));
1055                         oper->type = OPER_RAW;
1056                         oper->u.raw = (unsigned long)$1;
1057                         $$ = oper;
1058                   }
1059                 | imm {
1060                         struct operand *oper = xmalloc(sizeof(struct operand));
1061                         oper->type = OPER_IMM;
1062                         oper->u.imm = $1;
1063                         $$ = oper;
1064                   }
1065                 | labelref {
1066                         $$ = $1;
1067                   }
1068                 ;
1069
1070 reg             : GPR regnr {
1071                         struct registr *reg = xmalloc(sizeof(struct registr));
1072                         reg->type = GPR;
1073                         reg->nr = (unsigned long)$2;
1074                         $$ = reg;
1075                   }
1076                 | SPR {
1077                         struct registr *reg = xmalloc(sizeof(struct registr));
1078                         reg->type = SPR;
1079                         yytext += 3; /* skip "spr" */
1080                         reg->nr = strtoul(yytext, NULL, 16);
1081                         $$ = reg;
1082                   }
1083                 | OFFR regnr {
1084                         struct registr *reg = xmalloc(sizeof(struct registr));
1085                         reg->type = OFFR;
1086                         reg->nr = (unsigned long)$2;
1087                         $$ = reg;
1088                   }
1089                 ;
1090
1091 mem             : BRACK_OPEN hexnum_decnum BRACK_CLOSE {
1092                         struct memory *mem = xmalloc(sizeof(struct memory));
1093                         mem->type = MEM_DIRECT;
1094                         mem->offset = (unsigned long)$2;
1095                         $$ = mem;
1096                   }
1097                 | BRACK_OPEN hexnum_decnum COMMA OFFR regnr BRACK_CLOSE {
1098                         struct memory *mem = xmalloc(sizeof(struct memory));
1099                         mem->type = MEM_INDIRECT;
1100                         mem->offset = (unsigned long)$2;
1101                         mem->offr_nr = (unsigned long)$5;
1102                         $$ = mem;
1103                   }
1104                 ;
1105
1106 imm             : hexnum {
1107                         struct immediate *imm = xmalloc(sizeof(struct immediate));
1108                         imm->imm = (unsigned long)$1;
1109                         $$ = imm;
1110                   }
1111                 | decnum {
1112                         struct immediate *imm = xmalloc(sizeof(struct immediate));
1113                         imm->imm = (unsigned long)$1;
1114                         $$ = imm;
1115                   }
1116                 | complex_imm {
1117                         struct immediate *imm = xmalloc(sizeof(struct immediate));
1118                         imm->imm = (unsigned long)$1;
1119                         $$ = imm;
1120                   }
1121                 ;
1122
1123 complex_imm     : PAREN_OPEN complex_imm_arg complex_imm_oper complex_imm_arg PAREN_CLOSE {
1124                         unsigned long a = (unsigned long)$2;
1125                         unsigned long b = (unsigned long)$4;
1126                         unsigned long operation = (unsigned long)$3;
1127                         unsigned long res = 31337;
1128                         switch (operation) {
1129                         case PLUS:
1130                                 res = a + b;
1131                                 break;
1132                         case MINUS:
1133                                 res = a - b;
1134                                 break;
1135                         case MULTIPLY:
1136                                 res = a * b;
1137                                 break;
1138                         case DIVIDE:
1139                                 res = a / b;
1140                                 break;
1141                         case BITW_OR:
1142                                 res = a | b;
1143                                 break;
1144                         case BITW_AND:
1145                                 res = a & b;
1146                                 break;
1147                         case BITW_XOR:
1148                                 res = a ^ b;
1149                                 break;
1150                         case LEFTSHIFT:
1151                                 res = a << b;
1152                                 break;
1153                         case RIGHTSHIFT:
1154                                 res = a >> b;
1155                                 break;
1156                         default:
1157                                 yyerror("Internal parser BUG. complex_imm oper unknown");
1158                         }
1159                         $$ = (void *)res;
1160                   }
1161                 | PAREN_OPEN complex_imm PAREN_CLOSE {
1162                         $$ = $2;
1163                   }
1164                 | PAREN_OPEN BITW_NOT complex_imm PAREN_CLOSE {
1165                         unsigned long n = (unsigned long)$3;
1166                         n = ~n;
1167                         $$ = (void *)n;
1168                   }
1169                 | PAREN_OPEN complex_imm_const PAREN_CLOSE {
1170                         $$ = $2;
1171                   }
1172                 ;
1173
1174 complex_imm_oper : PLUS {
1175                         $$ = (void *)(unsigned long)PLUS;
1176                   }
1177                 | MINUS {
1178                         $$ = (void *)(unsigned long)MINUS;
1179                   }
1180                 | MULTIPLY {
1181                         $$ = (void *)(unsigned long)MULTIPLY;
1182                   }
1183                 | DIVIDE {
1184                         $$ = (void *)(unsigned long)DIVIDE;
1185                   }
1186                 | BITW_OR {
1187                         $$ = (void *)(unsigned long)BITW_OR;
1188                   }
1189                 | BITW_AND {
1190                         $$ = (void *)(unsigned long)BITW_AND;
1191                   }
1192                 | BITW_XOR {
1193                         $$ = (void *)(unsigned long)BITW_XOR;
1194                   }
1195                 | LEFTSHIFT {
1196                         $$ = (void *)(unsigned long)LEFTSHIFT;
1197                   }
1198                 | RIGHTSHIFT {
1199                         $$ = (void *)(unsigned long)RIGHTSHIFT;
1200                   }
1201                 ;
1202
1203 complex_imm_arg : complex_imm_const {
1204                         $$ = $1;
1205                   }
1206                 | complex_imm {
1207                         $$ = $1;
1208                   }
1209                 ;
1210
1211 complex_imm_const : hexnum_decnum {
1212                         $$ = $1;
1213                   }
1214                 | BITW_NOT hexnum_decnum {
1215                         unsigned long n = (unsigned long)$2;
1216                         n = ~n;
1217                         $$ = (void *)n;
1218                   }
1219                 ;
1220
1221 hexnum          : HEXNUM {
1222                         while (yytext[0] != 'x') {
1223                                 if (yytext[0] == '\0')
1224                                         yyerror("Internal HEXNUM parser error");
1225                                 yytext++;
1226                         }
1227                         yytext++;
1228                         $$ = (void *)(unsigned long)strtoul(yytext, NULL, 16);
1229                   }
1230                 ;
1231
1232 decnum          : DECNUM {
1233                         $$ = (void *)(unsigned long)strtol(yytext, NULL, 10);
1234                   }
1235                 ;
1236
1237 hexnum_decnum   : hexnum {
1238                         $$ = $1;
1239                   }
1240                 | decnum {
1241                         $$ = $1;
1242                   }
1243                 ;
1244
1245 labelref        : identifier {
1246                         struct operand *oper = xmalloc(sizeof(struct operand));
1247                         struct label *label = xmalloc(sizeof(struct label));
1248                         label->name = $1;
1249                         label->direction = LABELREF_ABSOLUTE;
1250                         oper->type = OPER_LABEL;
1251                         oper->u.label = label;
1252                         $$ = oper;
1253                   }
1254                 | identifier MINUS {
1255                         struct operand *oper = xmalloc(sizeof(struct operand));
1256                         struct label *label = xmalloc(sizeof(struct label));
1257                         label->name = $1;
1258                         label->direction = LABELREF_RELATIVE_BACK;
1259                         oper->type = OPER_LABEL;
1260                         oper->u.label = label;
1261                         $$ = oper;
1262                   }
1263                 | identifier PLUS {
1264                         struct operand *oper = xmalloc(sizeof(struct operand));
1265                         struct label *label = xmalloc(sizeof(struct label));
1266                         label->name = $1;
1267                         label->direction = LABELREF_RELATIVE_FORWARD;
1268                         oper->type = OPER_LABEL;
1269                         oper->u.label = label;
1270                         $$ = oper;
1271                   }
1272                 ;
1273
1274 regnr           : DECNUM {
1275                         $$ = (void *)(unsigned long)strtoul(yytext, NULL, 10);
1276                   }
1277                 ;
1278
1279 identifier      : IDENT {
1280                         $$ = xstrdup(yytext);
1281                   }
1282                 ;
1283
1284 %%
1285
1286 int section = SECTION_TEXT; /* default to .text section */
1287 struct initvals_sect *cur_initvals_sect;
1288
1289 void yyerror(char *str)
1290 {
1291         unsigned int i;
1292
1293         fprintf(stderr,
1294                 "Parser ERROR (file \"%s\", line %u, col %u):\n",
1295                 cur_lineinfo.file,
1296                 cur_lineinfo.lineno,
1297                 cur_lineinfo.column);
1298         fprintf(stderr, "%s\n", cur_lineinfo.linecopy);
1299         for (i = 0; i < cur_lineinfo.column - 1; i++)
1300                 fprintf(stderr, " ");
1301         fprintf(stderr, "^\n");
1302         fprintf(stderr, "%s\n", str);
1303         exit(1);
1304 }
1305
1306 static struct operand * store_oper_sanity(struct operand *oper)
1307 {
1308         if (oper->type == OPER_IMM &&
1309             oper->u.imm->imm != 0) {
1310                 yyerror("Only 0x000 Immediate is allowed for "
1311                         "Output operands");
1312         }
1313         return oper;
1314 }