assembler: Remove two fixmes.
[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 ;.*$                    { update_lineinfo(); /* comment */ }
52
53 ^{WS}*"%"{WS}*arch      { update_lineinfo(); return ASM_ARCH; }
54 ^{WS}*"%"{WS}*start     { update_lineinfo(); return ASM_START; }
55
56 ^{WS}*\.text{WS}*$                              { update_lineinfo(); return SECTION_TEXT; }
57 ^{WS}*\.initvals/\({IDENTIFIER}\){WS}*{NEWLINE} { 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 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 PLUS; }
71 \-                      { update_lineinfo(); return MINUS; }
72 \*                      { update_lineinfo(); return MULTIPLY; }
73 \/                      { update_lineinfo(); return DIVIDE; }
74 \|                      { update_lineinfo(); return BITW_OR; }
75 \&                      { update_lineinfo(); return BITW_AND; }
76 \^                      { update_lineinfo(); return BITW_XOR; }
77 \~                      { update_lineinfo(); return BITW_NOT; }
78 \<\<                    { update_lineinfo(); return LEFTSHIFT; }
79 \>\>                    { update_lineinfo(); return RIGHTSHIFT; }
80
81 add                     { update_lineinfo(); return OP_ADD; }
82 add\.                   { update_lineinfo(); return OP_ADDSC; }
83 addc                    { update_lineinfo(); return OP_ADDC; }
84 addc\.                  { update_lineinfo(); return OP_ADDSCC; }
85
86 sub                     { update_lineinfo(); return OP_SUB; }
87 sub\.                   { update_lineinfo(); return OP_SUBSC; }
88 subc                    { update_lineinfo(); return OP_SUBC; }
89 subc\.                  { update_lineinfo(); return OP_SUBSCC; }
90
91 sra                     { update_lineinfo(); return OP_SRA; }
92 or                      { update_lineinfo(); return OP_OR; }
93 and                     { update_lineinfo(); return OP_AND; }
94 xor                     { update_lineinfo(); return OP_XOR; }
95 sr                      { update_lineinfo(); return OP_SR; }
96 srx                     { update_lineinfo(); return OP_SRX; }
97 sl                      { update_lineinfo(); return OP_SL; }
98 rl                      { update_lineinfo(); return OP_RL; }
99 rr                      { update_lineinfo(); return OP_RR; }
100 nand                    { update_lineinfo(); return OP_NAND; }
101 orx                     { update_lineinfo(); return OP_ORX; }
102 mov                     { update_lineinfo(); return OP_MOV; }
103
104 jmp                     { update_lineinfo(); return OP_JMP; }
105 jand                    { update_lineinfo(); return OP_JAND; }
106 jnand                   { update_lineinfo(); return OP_JNAND; }
107 js                      { update_lineinfo(); return OP_JS; }
108 jns                     { update_lineinfo(); return OP_JNS; }
109 je                      { update_lineinfo(); return OP_JE; }
110 jne                     { update_lineinfo(); return OP_JNE; }
111 jls                     { update_lineinfo(); return OP_JLS; }
112 jges                    { update_lineinfo(); return OP_JGES; }
113 jgs                     { update_lineinfo(); return OP_JGS; }
114 jles                    { update_lineinfo(); return OP_JLES; }
115 jl                      { update_lineinfo(); return OP_JL; }
116 jge                     { update_lineinfo(); return OP_JGE; }
117 jg                      { update_lineinfo(); return OP_JG; }
118 jle                     { update_lineinfo(); return OP_JLE; }
119 jzx                     { update_lineinfo(); return OP_JZX; }
120 jnzx                    { update_lineinfo(); return OP_JNZX; }
121 jext                    { update_lineinfo(); return OP_JEXT; }
122 jnext                   { update_lineinfo(); return OP_JNEXT; }
123
124 call                    { update_lineinfo(); return OP_CALL; }
125 ret                     { update_lineinfo(); return OP_RET; }
126
127 tkiph                   { update_lineinfo(); return OP_TKIPH; }
128 tkiphs                  { update_lineinfo(); return OP_TKIPHS; }
129 tkipl                   { update_lineinfo(); return OP_TKIPL; }
130 tkipls                  { update_lineinfo(); return OP_TKIPLS; }
131
132 nap                     { update_lineinfo(); return OP_NAP; }
133
134 mmio16                  { update_lineinfo(); return IVAL_MMIO16; }
135 mmio32                  { update_lineinfo(); return IVAL_MMIO32; }
136 phy                     { update_lineinfo(); return IVAL_PHY; }
137 radio                   { update_lineinfo(); return IVAL_RADIO; }
138 shm16                   { update_lineinfo(); return IVAL_SHM16; }
139 shm32                   { update_lineinfo(); return IVAL_SHM32; }
140
141 @[0-9a-fA-F]{3,3}       { update_lineinfo(); return RAW_CODE; }
142
143 0x[0-9a-fA-F]+          { update_lineinfo(); return HEXNUM; }
144 -?[0-9]+                { update_lineinfo(); return DECNUM; }
145
146 {IDENTIFIER}:           { update_lineinfo(); return LABEL; }
147 {IDENTIFIER}            { update_lineinfo(); return IDENT; }
148
149 %%
150
151 struct lineinfo cur_lineinfo;
152
153 static void interpret_cppinfo(const char *str)
154 {
155         const char * const orig = str;
156         char tmp[64];
157         char *found;
158
159         /* This will interpret lines added by CPP.
160          * They look like:
161          *     # 3 "file.asm" 1
162          */
163
164         if (*str == '\0')
165                 goto error;
166         str++; /* skip # character */
167         if (*str == '\0')
168                 goto error;
169         str++; /* skip whitespace */
170
171         /* Line number */
172         found = strchr(str, ' ');
173         if (!found)
174                 goto error;
175         memset(tmp, 0, sizeof(tmp));
176         memcpy(tmp, str, min(sizeof(tmp) - 1,
177                              (int)(found - str)));
178         cur_lineinfo.lineno = strtoul(tmp, NULL, 10);
179         str = found;
180         str++;
181
182         /* File name */
183         if (*str != '\"')
184                 goto error;
185         str++;
186         if (*str == '\0')
187                 goto error;
188         found = strchr(str, '\"');
189         if (!found)
190                 goto error;
191         memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
192         memcpy(cur_lineinfo.file, str,
193                min(sizeof(cur_lineinfo.file) - 1,
194                    (int)(found - str)));
195
196         if (strcmp(cur_lineinfo.file, "<stdin>") == 0)
197                 strcpy(cur_lineinfo.file, "Input File");
198
199         return;
200 error:
201         fprintf(stderr, "Invalid CPP line directive:  %s\n", orig);
202         exit(1);
203 }
204
205 static void update_lineinfo(void)
206 {
207         int i = 0;
208
209         while (yytext[i] != '\0') {
210                 switch (yytext[i]) {
211                 case '\r':
212                 case '\n':
213                         cur_lineinfo.column = 0;
214                         break;
215                 case '\t':
216                         cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
217                         break;
218                 default:
219                         cur_lineinfo.column++;
220                 }
221                 i++;
222         }
223 }