Allow inline %assert inside of complex immediates.
[b43-tools.git] / assembler / scanner.l
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 "parser.h"
17 #include "main.h"
18
19 #include <stdio.h>
20
21 #undef min
22 #define min(x,y)        ((x) < (y) ? (x) : (y))
23
24
25 static void interpret_cppinfo(const char *);
26 static void update_lineinfo(void);
27
28 static inline void log_current_line(void)
29 {
30         size_t len;
31
32         len = min(sizeof(cur_lineinfo.linecopy) - 1, strlen(yytext));
33         strncpy(cur_lineinfo.linecopy, yytext, len);
34         cur_lineinfo.linecopy[len] = '\0';
35 }
36
37 %}
38
39 IDENTIFIER      ([a-zA-Z_][0-9a-zA-Z_]*)
40 WS              ([ \t])
41 NEWLINE         ((\r)|(\n)|(\r\n))
42
43 %%
44
45 ^.*$                                    { log_current_line(); REJECT; }
46 #\ [0-9]+\ \".*\"[ ]*[0-9]*{NEWLINE}    { interpret_cppinfo(yytext); }
47
48
49 {WS}+                   { update_lineinfo(); /* whitespace */ }
50 {NEWLINE}               { cur_lineinfo.lineno++; update_lineinfo(); }
51
52 ^{WS}*"%"{WS}*arch      { update_lineinfo(); return ASM_ARCH; }
53 ^{WS}*"%"{WS}*start     { update_lineinfo(); return ASM_START; }
54 "%"{WS}*assert          { update_lineinfo(); return ASM_ASSERT; }
55
56 ^{WS}*\.text{WS}*$                      { update_lineinfo(); return SECTION_TEXT; }
57 ^{WS}*\.initvals/\({IDENTIFIER}\)       { update_lineinfo(); return SECTION_IVALS; }
58
59 spr[0-9a-fA-F]{3,3}     { update_lineinfo(); return SPR; }
60 r/([0-9]|([1-5][0-9])|(6[0-3])) { update_lineinfo(); return GPR; }
61 off/[0-6]               { update_lineinfo(); return OFFR; }
62 lr/[0-3]                { update_lineinfo(); return LR; }
63
64 ,                       { update_lineinfo(); return COMMA; }
65 ;                       { update_lineinfo(); return SEMICOLON; }
66 \[                      { update_lineinfo(); return BRACK_OPEN; }
67 \]                      { update_lineinfo(); return BRACK_CLOSE; }
68 \(                      { update_lineinfo(); return PAREN_OPEN; }
69 \)                      { update_lineinfo(); return PAREN_CLOSE; }
70
71 ==                      { update_lineinfo(); return EQUAL; }
72 !=                      { update_lineinfo(); return NOT_EQUAL; }
73 \|\|                    { update_lineinfo(); return LOGICAL_OR; }
74 \&\&                    { update_lineinfo(); return LOGICAL_AND; }
75 \+                      { update_lineinfo(); return PLUS; }
76 \-                      { update_lineinfo(); return MINUS; }
77 \*                      { update_lineinfo(); return MULTIPLY; }
78 \/                      { update_lineinfo(); return DIVIDE; }
79 \|                      { update_lineinfo(); return BITW_OR; }
80 \&                      { update_lineinfo(); return BITW_AND; }
81 \^                      { update_lineinfo(); return BITW_XOR; }
82 \~                      { update_lineinfo(); return BITW_NOT; }
83 \<\<                    { update_lineinfo(); return LEFTSHIFT; }
84 \>\>                    { update_lineinfo(); return RIGHTSHIFT; }
85
86 add                     { update_lineinfo(); return OP_ADD; }
87 add\.                   { update_lineinfo(); return OP_ADDSC; }
88 addc                    { update_lineinfo(); return OP_ADDC; }
89 addc\.                  { update_lineinfo(); return OP_ADDSCC; }
90
91 sub                     { update_lineinfo(); return OP_SUB; }
92 sub\.                   { update_lineinfo(); return OP_SUBSC; }
93 subc                    { update_lineinfo(); return OP_SUBC; }
94 subc\.                  { update_lineinfo(); return OP_SUBSCC; }
95
96 sra                     { update_lineinfo(); return OP_SRA; }
97 or                      { update_lineinfo(); return OP_OR; }
98 and                     { update_lineinfo(); return OP_AND; }
99 xor                     { update_lineinfo(); return OP_XOR; }
100 sr                      { update_lineinfo(); return OP_SR; }
101 srx                     { update_lineinfo(); return OP_SRX; }
102 sl                      { update_lineinfo(); return OP_SL; }
103 rl                      { update_lineinfo(); return OP_RL; }
104 rr                      { update_lineinfo(); return OP_RR; }
105 nand                    { update_lineinfo(); return OP_NAND; }
106 orx                     { update_lineinfo(); return OP_ORX; }
107 mov                     { update_lineinfo(); return OP_MOV; }
108
109 jmp                     { update_lineinfo(); return OP_JMP; }
110 jand                    { update_lineinfo(); return OP_JAND; }
111 jnand                   { update_lineinfo(); return OP_JNAND; }
112 js                      { update_lineinfo(); return OP_JS; }
113 jns                     { update_lineinfo(); return OP_JNS; }
114 je                      { update_lineinfo(); return OP_JE; }
115 jne                     { update_lineinfo(); return OP_JNE; }
116 jls                     { update_lineinfo(); return OP_JLS; }
117 jges                    { update_lineinfo(); return OP_JGES; }
118 jgs                     { update_lineinfo(); return OP_JGS; }
119 jles                    { update_lineinfo(); return OP_JLES; }
120 jl                      { update_lineinfo(); return OP_JL; }
121 jge                     { update_lineinfo(); return OP_JGE; }
122 jg                      { update_lineinfo(); return OP_JG; }
123 jle                     { update_lineinfo(); return OP_JLE; }
124 jzx                     { update_lineinfo(); return OP_JZX; }
125 jnzx                    { update_lineinfo(); return OP_JNZX; }
126 jext                    { update_lineinfo(); return OP_JEXT; }
127 jnext                   { update_lineinfo(); return OP_JNEXT; }
128
129 call                    { update_lineinfo(); return OP_CALL; }
130 ret                     { update_lineinfo(); return OP_RET; }
131
132 tkiph                   { update_lineinfo(); return OP_TKIPH; }
133 tkiphs                  { update_lineinfo(); return OP_TKIPHS; }
134 tkipl                   { update_lineinfo(); return OP_TKIPL; }
135 tkipls                  { update_lineinfo(); return OP_TKIPLS; }
136
137 nap                     { update_lineinfo(); return OP_NAP; }
138
139 mmio16                  { update_lineinfo(); return IVAL_MMIO16; }
140 mmio32                  { update_lineinfo(); return IVAL_MMIO32; }
141 phy                     { update_lineinfo(); return IVAL_PHY; }
142 radio                   { update_lineinfo(); return IVAL_RADIO; }
143 shm16                   { update_lineinfo(); return IVAL_SHM16; }
144 shm32                   { update_lineinfo(); return IVAL_SHM32; }
145
146 @[0-9a-fA-F]{3,3}       { update_lineinfo(); return RAW_CODE; }
147
148 0x[0-9a-fA-F]+          { update_lineinfo(); return HEXNUM; }
149 -?[0-9]+                { update_lineinfo(); return DECNUM; }
150
151 {IDENTIFIER}:           { update_lineinfo(); return LABEL; }
152 {IDENTIFIER}            { update_lineinfo(); return IDENT; }
153
154 %%
155
156 struct lineinfo cur_lineinfo;
157 //FIXME The linenumber sometimes is wrong.
158
159 static void interpret_cppinfo(const char *str)
160 {
161         const char * const orig = str;
162         char tmp[64];
163         char *found;
164
165         /* This will interpret lines added by CPP.
166          * They look like:
167          *     # 3 "file.asm" 1
168          */
169
170         if (*str == '\0')
171                 goto error;
172         str++; /* skip # character */
173         if (*str == '\0')
174                 goto error;
175         str++; /* skip whitespace */
176
177         /* Line number */
178         found = strchr(str, ' ');
179         if (!found)
180                 goto error;
181         memset(tmp, 0, sizeof(tmp));
182         memcpy(tmp, str, min(sizeof(tmp) - 1,
183                              (int)(found - str)));
184         cur_lineinfo.lineno = strtoul(tmp, NULL, 10) - 1;
185         str = found;
186         str++;
187
188         /* File name */
189         if (*str != '\"')
190                 goto error;
191         str++;
192         if (*str == '\0')
193                 goto error;
194         found = strchr(str, '\"');
195         if (!found)
196                 goto error;
197         memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
198         memcpy(cur_lineinfo.file, str,
199                min(sizeof(cur_lineinfo.file) - 1,
200                    (int)(found - str)));
201
202         return;
203 error:
204         fprintf(stderr, "Invalid CPP line directive:  %s\n", orig);
205         exit(1);
206 }
207
208 static void update_lineinfo(void)
209 {
210         int i = 0;
211
212         while (yytext[i] != '\0') {
213                 switch (yytext[i]) {
214                 case '\r':
215                 case '\n':
216                         cur_lineinfo.column = 0;
217                         break;
218                 case '\t':
219                         cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
220                         break;
221                 default:
222                         cur_lineinfo.column++;
223                 }
224                 i++;
225         }
226 }