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