assembler: Some r15 fixes
[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 static void assembler_assertion_failed(void);
32
33 /* The current .section */
34 extern int section;
35 /* Pointer to the current initvals section data structure. */
36 extern struct initvals_sect *cur_initvals_sect;
37
38 %}
39
40 %token SECTION_TEXT SECTION_IVALS
41
42 %token ASM_ARCH ASM_START ASM_ASSERT SPR GPR OFFR LR COMMA SEMICOLON BRACK_OPEN BRACK_CLOSE PAREN_OPEN PAREN_CLOSE HEXNUM DECNUM ARCH_NEWWORLD ARCH_OLDWORLD LABEL IDENT LABELREF
43
44 %token EQUAL NOT_EQUAL LOGICAL_OR LOGICAL_AND PLUS MINUS MULTIPLY DIVIDE BITW_OR BITW_AND BITW_XOR BITW_NOT LEFTSHIFT RIGHTSHIFT
45
46 %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
47
48 %token IVAL_MMIO16 IVAL_MMIO32 IVAL_PHY IVAL_RADIO IVAL_SHM16 IVAL_SHM32 IVAL_TRAM
49
50 %start line
51
52 %%
53
54 line    : line_terminator {
55                 /* empty */
56           }
57         | line statement line_terminator {
58                 struct statement *s = $2;
59                 if (s) {
60                         if (section != SECTION_TEXT)
61                                 yyerror("Microcode text instruction in non .text section");
62                         memcpy(&s->info, &cur_lineinfo, sizeof(struct lineinfo));
63                         list_add_tail(&s->list, &infile.sl);
64                 }
65           }
66         | line section_switch line_terminator {
67           }
68         | line ivals_write line_terminator {
69                 struct initval_op *io = $2;
70                 if (section != SECTION_IVALS)
71                         yyerror("InitVals write in non .initvals section");
72                 memcpy(&io->info, &cur_lineinfo, sizeof(struct lineinfo));
73                 INIT_LIST_HEAD(&io->list);
74                 list_add_tail(&io->list, &cur_initvals_sect->ops);
75           }
76         ;
77
78 /* Allow terminating lines with the ";" char */
79 line_terminator : /* Nothing */
80                 | SEMICOLON line_terminator
81                 ;
82
83 section_switch  : SECTION_TEXT {
84                         section = SECTION_TEXT;
85                   }
86                 | SECTION_IVALS PAREN_OPEN identifier PAREN_CLOSE {
87                         const char *sectname = $3;
88                         struct initvals_sect *s;
89                         cur_initvals_sect = NULL;
90                         /* Search if there already is a section by that name. */
91                         list_for_each_entry(s, &infile.ivals, list) {
92                                 if (strcmp(sectname, s->name) == 0)
93                                         cur_initvals_sect = s;
94                         }
95                         if (!cur_initvals_sect) {
96                                 /* Not found, create a new one. */
97                                 s = xmalloc(sizeof(struct initvals_sect));
98                                 s->name = sectname;
99                                 INIT_LIST_HEAD(&s->ops);
100                                 INIT_LIST_HEAD(&s->list);
101                                 list_add_tail(&s->list, &infile.ivals);
102                                 cur_initvals_sect = s;
103                         }
104                         section = SECTION_IVALS;
105                   }
106                 ;
107
108 ivals_write     : IVAL_MMIO16 imm_value COMMA imm_value {
109                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
110                         iop->type = IVAL_W_MMIO16;
111                         iop->args[0] = (unsigned int)(unsigned long)$2;
112                         iop->args[1] = (unsigned int)(unsigned long)$4;
113                         $$ = iop;
114                   }
115                 | IVAL_MMIO32 imm_value COMMA imm_value {
116                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
117                         iop->type = IVAL_W_MMIO32;
118                         iop->args[0] = (unsigned int)(unsigned long)$2;
119                         iop->args[1] = (unsigned int)(unsigned long)$4;
120                         $$ = iop;
121                   }
122                 | IVAL_PHY imm_value COMMA imm_value {
123                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
124                         iop->type = IVAL_W_PHY;
125                         iop->args[0] = (unsigned int)(unsigned long)$2;
126                         iop->args[1] = (unsigned int)(unsigned long)$4;
127                         $$ = iop;
128                   }
129                 | IVAL_RADIO imm_value COMMA imm_value {
130                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
131                         iop->type = IVAL_W_RADIO;
132                         iop->args[0] = (unsigned int)(unsigned long)$2;
133                         iop->args[1] = (unsigned int)(unsigned long)$4;
134                         $$ = iop;
135                   }
136                 | IVAL_SHM16 imm_value COMMA imm_value COMMA imm_value {
137                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
138                         iop->type = IVAL_W_SHM16;
139                         iop->args[0] = (unsigned int)(unsigned long)$2;
140                         iop->args[1] = (unsigned int)(unsigned long)$4;
141                         iop->args[2] = (unsigned int)(unsigned long)$6;
142                         $$ = iop;
143                   }
144                 | IVAL_SHM32 imm_value COMMA imm_value COMMA imm_value {
145                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
146                         iop->type = IVAL_W_SHM32;
147                         iop->args[0] = (unsigned int)(unsigned long)$2;
148                         iop->args[1] = (unsigned int)(unsigned long)$4;
149                         iop->args[2] = (unsigned int)(unsigned long)$6;
150                         $$ = iop;
151                   }
152                 | IVAL_TRAM imm_value COMMA imm_value {
153                         struct initval_op *iop = xmalloc(sizeof(struct initval_op));
154                         iop->type = IVAL_W_TRAM;
155                         iop->args[0] = (unsigned int)(unsigned long)$2;
156                         iop->args[1] = (unsigned int)(unsigned long)$4;
157                         $$ = iop;
158                   }
159                 ;
160
161 statement       : asmdir {
162                         struct asmdir *ad = $1;
163                         if (ad) {
164                                 struct statement *s = xmalloc(sizeof(struct statement));
165                                 INIT_LIST_HEAD(&s->list);
166                                 s->type = STMT_ASMDIR;
167                                 s->u.asmdir = $1;
168                                 $$ = s;
169                         } else
170                                 $$ = NULL;
171                   }
172                 | label {
173                         struct statement *s = xmalloc(sizeof(struct statement));
174                         INIT_LIST_HEAD(&s->list);
175                         s->type = STMT_LABEL;
176                         s->u.label = $1;
177                         $$ = s;
178                   }
179                 | insn_add {
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_addsc {
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_addc {
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_addscc {
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_sub {
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_subsc {
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_subc {
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_subscc {
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_sra {
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_or {
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_and {
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_xor {
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_sr {
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_srx {
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_sl {
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_rl {
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_rr {
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_nand {
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_orx {
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_mov {
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_jmp {
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_jand {
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_jnand {
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_js {
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_jns {
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_je {
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_jne {
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_jls {
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_jges {
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_jgs {
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_jles {
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_jl {
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_jge {
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_jg {
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_jle {
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_jzx {
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_jnzx {
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_jext {
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_jnext {
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_call {
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_ret {
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_tkiph {
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_tkiphs {
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_tkipl {
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                 | insn_tkipls {
488                         struct statement *s = xmalloc(sizeof(struct statement));
489                         INIT_LIST_HEAD(&s->list);
490                         s->type = STMT_INSN;
491                         s->u.insn = $1;
492                         $$ = s;
493                   }
494                 | insn_nap {
495                         struct statement *s = xmalloc(sizeof(struct statement));
496                         INIT_LIST_HEAD(&s->list);
497                         s->type = STMT_INSN;
498                         s->u.insn = $1;
499                         $$ = s;
500                   }
501                 | insn_raw {
502                         struct statement *s = xmalloc(sizeof(struct statement));
503                         INIT_LIST_HEAD(&s->list);
504                         s->type = STMT_INSN;
505                         s->u.insn = $1;
506                         $$ = s;
507                   }
508                 ;
509
510 /* ASM directives */
511 asmdir          : ASM_ARCH hexnum_decnum {
512                         struct asmdir *ad = xmalloc(sizeof(struct asmdir));
513                         ad->type = ADIR_ARCH;
514                         ad->u.arch = (unsigned int)(unsigned long)$2;
515                         $$ = ad;
516                   }
517                 | ASM_START identifier {
518                         struct asmdir *ad = xmalloc(sizeof(struct asmdir));
519                         struct label *label = xmalloc(sizeof(struct label));
520                         label->name = $2;
521                         label->direction = LABELREF_ABSOLUTE;
522                         ad->type = ADIR_START;
523                         ad->u.start = label;
524                         $$ = ad;
525                   }
526                 | asm_assert {
527                         $$ = NULL;
528                   }
529                 ;
530
531 asm_assert      : ASM_ASSERT assertion {
532                         unsigned int ok = (unsigned int)(unsigned long)$2;
533                         if (!ok)
534                                 assembler_assertion_failed();
535                         $$ = NULL;
536                   }
537                 ;
538
539 assertion       : PAREN_OPEN assert_expr PAREN_CLOSE {
540                         $$ = $2;
541                   }
542                 | PAREN_OPEN assertion LOGICAL_OR assertion PAREN_CLOSE {
543                         unsigned int a = (unsigned int)(unsigned long)$2;
544                         unsigned int b = (unsigned int)(unsigned long)$4;
545                         unsigned int result = (a || b);
546                         $$ = (void *)(unsigned long)result;
547                   }
548                 | PAREN_OPEN assertion LOGICAL_AND assertion PAREN_CLOSE {
549                         unsigned int a = (unsigned int)(unsigned long)$2;
550                         unsigned int b = (unsigned int)(unsigned long)$4;
551                         unsigned int result = (a && b);
552                         $$ = (void *)(unsigned long)result;
553                   }
554                 ;
555
556 assert_expr     : imm_value EQUAL imm_value {
557                         unsigned int a = (unsigned int)(unsigned long)$1;
558                         unsigned int b = (unsigned int)(unsigned long)$3;
559                         unsigned int result = (a == b);
560                         $$ = (void *)(unsigned long)result;
561                   }
562                 | imm_value NOT_EQUAL imm_value {
563                         unsigned int a = (unsigned int)(unsigned long)$1;
564                         unsigned int b = (unsigned int)(unsigned long)$3;
565                         unsigned int result = (a != b);
566                         $$ = (void *)(unsigned long)result;
567                   }
568                 ;
569
570 label           : LABEL {
571                         struct label *label = xmalloc(sizeof(struct label));
572                         char *l;
573                         l = xstrdup(yytext);
574                         l[strlen(l) - 1] = '\0';
575                         label->name = l;
576                         $$ = label;
577                   }
578                 ;
579
580 /* add */
581 insn_add        : OP_ADD operlist_3 {
582                         struct instruction *insn = xmalloc(sizeof(struct instruction));
583                         insn->op = OP_ADD;
584                         insn->operands = $2;
585                         $$ = insn;
586                   }
587                 ;
588
589 /* add. */
590 insn_addsc      : OP_ADDSC operlist_3 {
591                         struct instruction *insn = xmalloc(sizeof(struct instruction));
592                         insn->op = OP_ADDSC;
593                         insn->operands = $2;
594                         $$ = insn;
595                   }
596                 ;
597
598 /* addc */
599 insn_addc       : OP_ADDC operlist_3 {
600                         struct instruction *insn = xmalloc(sizeof(struct instruction));
601                         insn->op = OP_ADDC;
602                         insn->operands = $2;
603                         $$ = insn;
604                   }
605                 ;
606
607 /* addc. */
608 insn_addscc     : OP_ADDSCC operlist_3 {
609                         struct instruction *insn = xmalloc(sizeof(struct instruction));
610                         insn->op = OP_ADDSCC;
611                         insn->operands = $2;
612                         $$ = insn;
613                   }
614                 ;
615
616 /* sub */
617 insn_sub        : OP_SUB operlist_3 {
618                         struct instruction *insn = xmalloc(sizeof(struct instruction));
619                         insn->op = OP_SUB;
620                         insn->operands = $2;
621                         $$ = insn;
622                   }
623                 ;
624
625 /* sub. */
626 insn_subsc      : OP_SUBSC operlist_3 {
627                         struct instruction *insn = xmalloc(sizeof(struct instruction));
628                         insn->op = OP_SUBSC;
629                         insn->operands = $2;
630                         $$ = insn;
631                   }
632                 ;
633
634 /* subc */
635 insn_subc       : OP_SUBC operlist_3 {
636                         struct instruction *insn = xmalloc(sizeof(struct instruction));
637                         insn->op = OP_SUBC;
638                         insn->operands = $2;
639                         $$ = insn;
640                   }
641                 ;
642
643 /* subc. */
644 insn_subscc     : OP_SUBSCC operlist_3 {
645                         struct instruction *insn = xmalloc(sizeof(struct instruction));
646                         insn->op = OP_SUBSCC;
647                         insn->operands = $2;
648                         $$ = insn;
649                   }
650                 ;
651
652 insn_sra        : OP_SRA operlist_3 {
653                         struct instruction *insn = xmalloc(sizeof(struct instruction));
654                         insn->op = OP_SRA;
655                         insn->operands = $2;
656                         $$ = insn;
657                   }
658                 ;
659
660 insn_or         : OP_OR operlist_3 {
661                         struct instruction *insn = xmalloc(sizeof(struct instruction));
662                         insn->op = OP_OR;
663                         insn->operands = $2;
664                         $$ = insn;
665                   }
666                 ;
667
668 insn_and        : OP_AND operlist_3 {
669                         struct instruction *insn = xmalloc(sizeof(struct instruction));
670                         insn->op = OP_AND;
671                         insn->operands = $2;
672                         $$ = insn;
673                   }
674                 ;
675
676 insn_xor        : OP_XOR operlist_3 {
677                         struct instruction *insn = xmalloc(sizeof(struct instruction));
678                         insn->op = OP_XOR;
679                         insn->operands = $2;
680                         $$ = insn;
681                   }
682                 ;
683
684 insn_sr         : OP_SR operlist_3 {
685                         struct instruction *insn = xmalloc(sizeof(struct instruction));
686                         insn->op = OP_SR;
687                         insn->operands = $2;
688                         $$ = insn;
689                   }
690                 ;
691
692 insn_srx        : OP_SRX extended_operlist {
693                         struct instruction *insn = xmalloc(sizeof(struct instruction));
694                         insn->op = OP_SRX;
695                         insn->operands = $2;
696                         $$ = insn;
697                   }
698                 ;
699
700 insn_sl         : OP_SL operlist_3 {
701                         struct instruction *insn = xmalloc(sizeof(struct instruction));
702                         insn->op = OP_SL;
703                         insn->operands = $2;
704                         $$ = insn;
705                   }
706                 ;
707
708 insn_rl         : OP_RL operlist_3 {
709                         struct instruction *insn = xmalloc(sizeof(struct instruction));
710                         insn->op = OP_RL;
711                         insn->operands = $2;
712                         $$ = insn;
713                   }
714                 ;
715
716 insn_rr         : OP_RR operlist_3 {
717                         struct instruction *insn = xmalloc(sizeof(struct instruction));
718                         insn->op = OP_RR;
719                         insn->operands = $2;
720                         $$ = insn;
721                   }
722                 ;
723
724 insn_nand       : OP_NAND operlist_3 {
725                         struct instruction *insn = xmalloc(sizeof(struct instruction));
726                         insn->op = OP_NAND;
727                         insn->operands = $2;
728                         $$ = insn;
729                   }
730                 ;
731
732 insn_orx        : OP_ORX extended_operlist {
733                         struct instruction *insn = xmalloc(sizeof(struct instruction));
734                         insn->op = OP_ORX;
735                         insn->operands = $2;
736                         $$ = insn;
737                   }
738                 ;
739
740 insn_mov        : OP_MOV operlist_2 {
741                         struct instruction *insn = xmalloc(sizeof(struct instruction));
742                         insn->op = OP_MOV;
743                         insn->operands = $2;
744                         $$ = insn;
745                   }
746                 ;
747
748 insn_jmp        : OP_JMP labelref {
749                         struct instruction *insn = xmalloc(sizeof(struct instruction));
750                         struct operlist *ol = xmalloc(sizeof(struct operlist));
751                         ol->oper[0] = $2;
752                         insn->op = OP_JMP;
753                         insn->operands = ol;
754                         $$ = insn;
755                   }
756                 ;
757
758 insn_jand       : OP_JAND operlist_3 {
759                         struct instruction *insn = xmalloc(sizeof(struct instruction));
760                         insn->op = OP_JAND;
761                         insn->operands = $2;
762                         $$ = insn;
763                   }
764                 ;
765
766 insn_jnand      : OP_JNAND operlist_3 {
767                         struct instruction *insn = xmalloc(sizeof(struct instruction));
768                         insn->op = OP_JNAND;
769                         insn->operands = $2;
770                         $$ = insn;
771                   }
772                 ;
773
774 insn_js         : OP_JS operlist_3 {
775                         struct instruction *insn = xmalloc(sizeof(struct instruction));
776                         insn->op = OP_JS;
777                         insn->operands = $2;
778                         $$ = insn;
779                   }
780                 ;
781
782 insn_jns        : OP_JNS operlist_3 {
783                         struct instruction *insn = xmalloc(sizeof(struct instruction));
784                         insn->op = OP_JNS;
785                         insn->operands = $2;
786                         $$ = insn;
787                   }
788                 ;
789
790 insn_je         : OP_JE operlist_3 {
791                         struct instruction *insn = xmalloc(sizeof(struct instruction));
792                         insn->op = OP_JE;
793                         insn->operands = $2;
794                         $$ = insn;
795                   }
796                 ;
797
798 insn_jne        : OP_JNE operlist_3 {
799                         struct instruction *insn = xmalloc(sizeof(struct instruction));
800                         insn->op = OP_JNE;
801                         insn->operands = $2;
802                         $$ = insn;
803                   }
804                 ;
805
806 insn_jls        : OP_JLS operlist_3 {
807                         struct instruction *insn = xmalloc(sizeof(struct instruction));
808                         insn->op = OP_JLS;
809                         insn->operands = $2;
810                         $$ = insn;
811                   }
812                 ;
813
814 insn_jges       : OP_JGES operlist_3 {
815                         struct instruction *insn = xmalloc(sizeof(struct instruction));
816                         insn->op = OP_JGES;
817                         insn->operands = $2;
818                         $$ = insn;
819                   }
820                 ;
821
822 insn_jgs        : OP_JGS operlist_3 {
823                         struct instruction *insn = xmalloc(sizeof(struct instruction));
824                         insn->op = OP_JGS;
825                         insn->operands = $2;
826                         $$ = insn;
827                   }
828                 ;
829
830 insn_jles       : OP_JLES operlist_3 {
831                         struct instruction *insn = xmalloc(sizeof(struct instruction));
832                         insn->op = OP_JLES;
833                         insn->operands = $2;
834                         $$ = insn;
835                   }
836                 ;
837
838 insn_jl         : OP_JL operlist_3 {
839                         struct instruction *insn = xmalloc(sizeof(struct instruction));
840                         insn->op = OP_JL;
841                         insn->operands = $2;
842                         $$ = insn;
843                   }
844                 ;
845
846 insn_jge        : OP_JGE operlist_3 {
847                         struct instruction *insn = xmalloc(sizeof(struct instruction));
848                         insn->op = OP_JGE;
849                         insn->operands = $2;
850                         $$ = insn;
851                   }
852                 ;
853
854 insn_jg         : OP_JG operlist_3 {
855                         struct instruction *insn = xmalloc(sizeof(struct instruction));
856                         insn->op = OP_JG;
857                         insn->operands = $2;
858                         $$ = insn;
859                   }
860                 ;
861
862 insn_jle        : OP_JLE operlist_3 {
863                         struct instruction *insn = xmalloc(sizeof(struct instruction));
864                         insn->op = OP_JLE;
865                         insn->operands = $2;
866                         $$ = insn;
867                   }
868                 ;
869
870 insn_jzx        : OP_JZX extended_operlist {
871                         struct instruction *insn = xmalloc(sizeof(struct instruction));
872                         insn->op = OP_JZX;
873                         insn->operands = $2;
874                         $$ = insn;
875                   }
876                 ;
877
878 insn_jnzx       : OP_JNZX extended_operlist {
879                         struct instruction *insn = xmalloc(sizeof(struct instruction));
880                         insn->op = OP_JNZX;
881                         insn->operands = $2;
882                         $$ = insn;
883                   }
884                 ;
885
886 insn_jext       : OP_JEXT external_jump_operands {
887                         struct instruction *insn = xmalloc(sizeof(struct instruction));
888                         insn->op = OP_JEXT;
889                         insn->operands = $2;
890                         $$ = insn;
891                   }
892                 ;
893
894 insn_jnext      : OP_JNEXT external_jump_operands {
895                         struct instruction *insn = xmalloc(sizeof(struct instruction));
896                         insn->op = OP_JNEXT;
897                         insn->operands = $2;
898                         $$ = insn;
899                   }
900                 ;
901
902 linkreg         : LR regnr {
903                         $$ = $2;
904                   }
905                 ;
906
907 insn_call       : OP_CALL linkreg COMMA labelref {
908                         struct instruction *insn = xmalloc(sizeof(struct instruction));
909                         struct operlist *ol = xmalloc(sizeof(struct operlist));
910                         struct operand *oper_lr = xmalloc(sizeof(struct operand));
911                         struct operand *oper_zero = xmalloc(sizeof(struct operand));
912                         oper_zero->type = OPER_RAW;
913                         oper_zero->u.raw = 0;
914                         oper_lr->type = OPER_RAW;
915                         oper_lr->u.raw = (unsigned long)$2;
916                         ol->oper[0] = oper_lr;
917                         ol->oper[1] = oper_zero;
918                         ol->oper[2] = $4;
919                         insn->op = OP_CALL;
920                         insn->operands = ol;
921                         $$ = insn;
922                   }
923                 ;
924
925 insn_ret        : OP_RET linkreg COMMA linkreg {
926                         struct instruction *insn = xmalloc(sizeof(struct instruction));
927                         struct operlist *ol = xmalloc(sizeof(struct operlist));
928                         struct operand *oper_lr0 = xmalloc(sizeof(struct operand));
929                         struct operand *oper_lr1 = xmalloc(sizeof(struct operand));
930                         struct operand *oper_zero = xmalloc(sizeof(struct operand));
931                         oper_zero->type = OPER_RAW;
932                         oper_zero->u.raw = 0;
933                         oper_lr0->type = OPER_RAW;
934                         oper_lr0->u.raw = (unsigned long)$2;
935                         oper_lr1->type = OPER_RAW;
936                         oper_lr1->u.raw = (unsigned long)$4;
937                         ol->oper[0] = oper_lr0;
938                         ol->oper[1] = oper_zero;
939                         ol->oper[2] = oper_lr1;
940                         insn->op = OP_RET;
941                         insn->operands = ol;
942                         $$ = insn;
943                   }
944                 ;
945
946 insn_tkiph      : OP_TKIPH operlist_2 {
947                         struct instruction *insn = xmalloc(sizeof(struct instruction));
948                         struct operlist *ol = $2;
949                         struct operand *flags = xmalloc(sizeof(struct operand));
950                         struct immediate *imm = xmalloc(sizeof(struct immediate));
951                         imm->imm = 0x1;
952                         flags->type = OPER_IMM;
953                         flags->u.imm = imm;
954                         ol->oper[2] = ol->oper[1];
955                         ol->oper[1] = flags;
956                         insn->op = OP_TKIPH;
957                         insn->operands = ol;
958                         $$ = insn;
959                   }
960                 ;
961
962 insn_tkiphs     : OP_TKIPHS operlist_2 {
963                         struct instruction *insn = xmalloc(sizeof(struct instruction));
964                         struct operlist *ol = $2;
965                         struct operand *flags = xmalloc(sizeof(struct operand));
966                         struct immediate *imm = xmalloc(sizeof(struct immediate));
967                         imm->imm = 0x1 | 0x2;
968                         flags->type = OPER_IMM;
969                         flags->u.imm = imm;
970                         ol->oper[2] = ol->oper[1];
971                         ol->oper[1] = flags;
972                         insn->op = OP_TKIPH;
973                         insn->operands = ol;
974                         $$ = insn;
975                   }
976                 ;
977
978 insn_tkipl      : OP_TKIPL operlist_2 {
979                         struct instruction *insn = xmalloc(sizeof(struct instruction));
980                         struct operlist *ol = $2;
981                         struct operand *flags = xmalloc(sizeof(struct operand));
982                         struct immediate *imm = xmalloc(sizeof(struct immediate));
983                         imm->imm = 0x0;
984                         flags->type = OPER_IMM;
985                         flags->u.imm = imm;
986                         ol->oper[2] = ol->oper[1];
987                         ol->oper[1] = flags;
988                         insn->op = OP_TKIPH;
989                         insn->operands = ol;
990                         $$ = insn;
991                   }
992                 ;
993
994 insn_tkipls     : OP_TKIPLS operlist_2 {
995                         struct instruction *insn = xmalloc(sizeof(struct instruction));
996                         struct operlist *ol = $2;
997                         struct operand *flags = xmalloc(sizeof(struct operand));
998                         struct immediate *imm = xmalloc(sizeof(struct immediate));
999                         imm->imm = 0x0 | 0x2;
1000                         flags->type = OPER_IMM;
1001                         flags->u.imm = imm;
1002                         ol->oper[2] = ol->oper[1];
1003                         ol->oper[1] = flags;
1004                         insn->op = OP_TKIPH;
1005                         insn->operands = ol;
1006                         $$ = insn;
1007                   }
1008                 ;
1009
1010 insn_nap        : OP_NAP {
1011                         struct instruction *insn = xmalloc(sizeof(struct instruction));
1012                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1013                         struct operand *regop = xmalloc(sizeof(struct operand));
1014                         struct operand *zeroop = xmalloc(sizeof(struct operand));
1015                         struct registr *r0 = xmalloc(sizeof(struct registr));
1016                         r0->type = GPR;
1017                         r0->nr = 0;
1018                         regop->type = OPER_REG;
1019                         regop->u.reg = r0;
1020                         zeroop->type = OPER_RAW;
1021                         zeroop->u.raw = 0x000;
1022                         ol->oper[0] = regop;
1023                         ol->oper[1] = regop;
1024                         ol->oper[2] = zeroop;
1025                         insn->op = OP_NAP;
1026                         insn->operands = ol;
1027                         $$ = insn;
1028                   }
1029                 ;
1030
1031 insn_raw        : raw_code operlist_3 {
1032                         struct instruction *insn = xmalloc(sizeof(struct instruction));
1033                         insn->op = RAW_CODE;
1034                         insn->operands = $2;
1035                         insn->opcode = (unsigned long)$1;
1036                         $$ = insn;
1037                   }
1038                 ;
1039
1040 raw_code        : RAW_CODE {
1041                         yytext++; /* skip @ */
1042                         $$ = (void *)(unsigned long)strtoul(yytext, NULL, 16);
1043                   }
1044                 ;
1045
1046 extended_operlist : imm_value COMMA imm_value COMMA operand COMMA operand COMMA operand {
1047                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1048                         struct operand *mask_oper = xmalloc(sizeof(struct operand));
1049                         struct operand *shift_oper = xmalloc(sizeof(struct operand));
1050                         mask_oper->type = OPER_RAW;
1051                         mask_oper->u.raw = (unsigned long)$1;
1052                         shift_oper->type = OPER_RAW;
1053                         shift_oper->u.raw = (unsigned long)$3;
1054                         ol->oper[0] = mask_oper;
1055                         ol->oper[1] = shift_oper;
1056                         ol->oper[2] = $5;
1057                         ol->oper[3] = $7;
1058                         ol->oper[4] = store_oper_sanity($9);
1059                         $$ = ol;
1060                   }
1061                 ;
1062
1063 external_jump_operands : imm COMMA labelref {
1064                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1065                         struct operand *cond = xmalloc(sizeof(struct operand));
1066                         cond->type = OPER_IMM;
1067                         cond->u.imm = $1;
1068                         ol->oper[0] = cond;
1069                         ol->oper[1] = $3;
1070                         $$ = ol;
1071                   }
1072                 ;
1073
1074 operlist_2      : operand COMMA operand {
1075                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1076                         ol->oper[0] = $1;
1077                         ol->oper[1] = store_oper_sanity($3);
1078                         $$ = ol;
1079                   }
1080                 ;
1081
1082 operlist_3      : operand COMMA operand COMMA operand {
1083                         struct operlist *ol = xmalloc(sizeof(struct operlist));
1084                         ol->oper[0] = $1;
1085                         ol->oper[1] = $3;
1086                         ol->oper[2] = store_oper_sanity($5);
1087                         $$ = ol;
1088                   }
1089                 ;
1090
1091 operand         : reg {
1092                         struct operand *oper = xmalloc(sizeof(struct operand));
1093                         oper->type = OPER_REG;
1094                         oper->u.reg = $1;
1095                         $$ = oper;
1096                   }
1097                 | mem {
1098                         struct operand *oper = xmalloc(sizeof(struct operand));
1099                         oper->type = OPER_MEM;
1100                         oper->u.mem = $1;
1101                         $$ = oper;
1102                   }
1103                 | raw_code {
1104                         struct operand *oper = xmalloc(sizeof(struct operand));
1105                         oper->type = OPER_RAW;
1106                         oper->u.raw = (unsigned long)$1;
1107                         $$ = oper;
1108                   }
1109                 | imm {
1110                         struct operand *oper = xmalloc(sizeof(struct operand));
1111                         oper->type = OPER_IMM;
1112                         oper->u.imm = $1;
1113                         $$ = oper;
1114                   }
1115                 | labelref {
1116                         $$ = $1;
1117                   }
1118                 ;
1119
1120 reg             : GPR regnr {
1121                         struct registr *reg = xmalloc(sizeof(struct registr));
1122                         reg->type = GPR;
1123                         reg->nr = (unsigned long)$2;
1124                         $$ = reg;
1125                   }
1126                 | SPR {
1127                         struct registr *reg = xmalloc(sizeof(struct registr));
1128                         reg->type = SPR;
1129                         yytext += 3; /* skip "spr" */
1130                         reg->nr = strtoul(yytext, NULL, 16);
1131                         $$ = reg;
1132                   }
1133                 | OFFR regnr {
1134                         struct registr *reg = xmalloc(sizeof(struct registr));
1135                         reg->type = OFFR;
1136                         reg->nr = (unsigned long)$2;
1137                         $$ = reg;
1138                   }
1139                 ;
1140
1141 mem             : BRACK_OPEN imm BRACK_CLOSE {
1142                         struct memory *mem = xmalloc(sizeof(struct memory));
1143                         struct immediate *offset_imm = $2;
1144                         mem->type = MEM_DIRECT;
1145                         mem->offset = offset_imm->imm;
1146                         free(offset_imm);
1147                         $$ = mem;
1148                   }
1149                 | BRACK_OPEN imm COMMA OFFR regnr BRACK_CLOSE {
1150                         struct memory *mem = xmalloc(sizeof(struct memory));
1151                         struct immediate *offset_imm = $2;
1152                         mem->type = MEM_INDIRECT;
1153                         mem->offset = offset_imm->imm;
1154                         free(offset_imm);
1155                         mem->offr_nr = (unsigned long)$5;
1156                         $$ = mem;
1157                   }
1158                 ;
1159
1160 imm             : imm_value {
1161                         struct immediate *imm = xmalloc(sizeof(struct immediate));
1162                         imm->imm = (unsigned long)$1;
1163                         $$ = imm;
1164                   }
1165                 ;
1166
1167 imm_value       : hexnum_decnum {
1168                         $$ = $1;
1169                   }
1170                 | complex_imm {
1171                         $$ = $1;
1172                   }
1173                 ;
1174
1175 complex_imm     : PAREN_OPEN complex_imm_arg complex_imm_oper complex_imm_arg PAREN_CLOSE {
1176                         unsigned long a = (unsigned long)$2;
1177                         unsigned long b = (unsigned long)$4;
1178                         unsigned long operation = (unsigned long)$3;
1179                         unsigned long res = 31337;
1180                         switch (operation) {
1181                         case PLUS:
1182                                 res = a + b;
1183                                 break;
1184                         case MINUS:
1185                                 res = a - b;
1186                                 break;
1187                         case MULTIPLY:
1188                                 res = a * b;
1189                                 break;
1190                         case DIVIDE:
1191                                 res = a / b;
1192                                 break;
1193                         case BITW_OR:
1194                                 res = a | b;
1195                                 break;
1196                         case BITW_AND:
1197                                 res = a & b;
1198                                 break;
1199                         case BITW_XOR:
1200                                 res = a ^ b;
1201                                 break;
1202                         case LEFTSHIFT:
1203                                 res = a << b;
1204                                 break;
1205                         case RIGHTSHIFT:
1206                                 res = a >> b;
1207                                 break;
1208                         default:
1209                                 yyerror("Internal parser BUG. complex_imm oper unknown");
1210                         }
1211                         $$ = (void *)res;
1212                   }
1213                 | PAREN_OPEN complex_imm PAREN_CLOSE {
1214                         $$ = $2;
1215                   }
1216                 | PAREN_OPEN asm_assert PAREN_CLOSE {
1217                         /* Inline assertion. Always return zero */
1218                         $$ = (void *)(unsigned long)(unsigned int)0;
1219                   }
1220                 | PAREN_OPEN BITW_NOT complex_imm PAREN_CLOSE {
1221                         unsigned long n = (unsigned long)$3;
1222                         n = ~n;
1223                         $$ = (void *)n;
1224                   }
1225                 | PAREN_OPEN complex_imm_const PAREN_CLOSE {
1226                         $$ = $2;
1227                   }
1228                 ;
1229
1230 complex_imm_oper : PLUS {
1231                         $$ = (void *)(unsigned long)PLUS;
1232                   }
1233                 | MINUS {
1234                         $$ = (void *)(unsigned long)MINUS;
1235                   }
1236                 | MULTIPLY {
1237                         $$ = (void *)(unsigned long)MULTIPLY;
1238                   }
1239                 | DIVIDE {
1240                         $$ = (void *)(unsigned long)DIVIDE;
1241                   }
1242                 | BITW_OR {
1243                         $$ = (void *)(unsigned long)BITW_OR;
1244                   }
1245                 | BITW_AND {
1246                         $$ = (void *)(unsigned long)BITW_AND;
1247                   }
1248                 | BITW_XOR {
1249                         $$ = (void *)(unsigned long)BITW_XOR;
1250                   }
1251                 | LEFTSHIFT {
1252                         $$ = (void *)(unsigned long)LEFTSHIFT;
1253                   }
1254                 | RIGHTSHIFT {
1255                         $$ = (void *)(unsigned long)RIGHTSHIFT;
1256                   }
1257                 ;
1258
1259 complex_imm_arg : complex_imm_const {
1260                         $$ = $1;
1261                   }
1262                 | complex_imm {
1263                         $$ = $1;
1264                   }
1265                 ;
1266
1267 complex_imm_const : hexnum_decnum {
1268                         $$ = $1;
1269                   }
1270                 | BITW_NOT hexnum_decnum {
1271                         unsigned long n = (unsigned long)$2;
1272                         n = ~n;
1273                         $$ = (void *)n;
1274                   }
1275                 ;
1276
1277 hexnum          : HEXNUM {
1278                         while (yytext[0] != 'x') {
1279                                 if (yytext[0] == '\0')
1280                                         yyerror("Internal HEXNUM parser error");
1281                                 yytext++;
1282                         }
1283                         yytext++;
1284                         $$ = (void *)(unsigned long)strtoul(yytext, NULL, 16);
1285                   }
1286                 ;
1287
1288 decnum          : DECNUM {
1289                         $$ = (void *)(unsigned long)strtol(yytext, NULL, 10);
1290                   }
1291                 ;
1292
1293 hexnum_decnum   : hexnum {
1294                         $$ = $1;
1295                   }
1296                 | decnum {
1297                         $$ = $1;
1298                   }
1299                 ;
1300
1301 labelref        : identifier {
1302                         struct operand *oper = xmalloc(sizeof(struct operand));
1303                         struct label *label = xmalloc(sizeof(struct label));
1304                         label->name = $1;
1305                         label->direction = LABELREF_ABSOLUTE;
1306                         oper->type = OPER_LABEL;
1307                         oper->u.label = label;
1308                         $$ = oper;
1309                   }
1310                 | identifier MINUS {
1311                         struct operand *oper = xmalloc(sizeof(struct operand));
1312                         struct label *label = xmalloc(sizeof(struct label));
1313                         label->name = $1;
1314                         label->direction = LABELREF_RELATIVE_BACK;
1315                         oper->type = OPER_LABEL;
1316                         oper->u.label = label;
1317                         $$ = oper;
1318                   }
1319                 | identifier PLUS {
1320                         struct operand *oper = xmalloc(sizeof(struct operand));
1321                         struct label *label = xmalloc(sizeof(struct label));
1322                         label->name = $1;
1323                         label->direction = LABELREF_RELATIVE_FORWARD;
1324                         oper->type = OPER_LABEL;
1325                         oper->u.label = label;
1326                         $$ = oper;
1327                   }
1328                 ;
1329
1330 regnr           : DECNUM {
1331                         $$ = (void *)(unsigned long)strtoul(yytext, NULL, 10);
1332                   }
1333                 ;
1334
1335 identifier      : IDENT {
1336                         $$ = xstrdup(yytext);
1337                   }
1338                 ;
1339
1340 %%
1341
1342 int section = SECTION_TEXT; /* default to .text section */
1343 struct initvals_sect *cur_initvals_sect;
1344
1345 void yyerror(char *str)
1346 {
1347         unsigned int i;
1348
1349         fprintf(stderr,
1350                 "Parser ERROR (file \"%s\", line %u, col %u):\n",
1351                 cur_lineinfo.file,
1352                 cur_lineinfo.lineno,
1353                 cur_lineinfo.column);
1354         fprintf(stderr, "%s\n", cur_lineinfo.linecopy);
1355         for (i = 0; i < cur_lineinfo.column - 1; i++)
1356                 fprintf(stderr, " ");
1357         fprintf(stderr, "^\n");
1358         fprintf(stderr, "%s\n", str);
1359         exit(1);
1360 }
1361
1362 static struct operand * store_oper_sanity(struct operand *oper)
1363 {
1364         if (oper->type == OPER_IMM &&
1365             oper->u.imm->imm != 0) {
1366                 yyerror("Only 0x000 Immediate is allowed for "
1367                         "Output operands");
1368         }
1369         return oper;
1370 }
1371
1372 static void assembler_assertion_failed(void)
1373 {
1374         yyerror("Assembler %assert failed");
1375 }