b43-asm: Add an option to print the code sizes after assembling.
[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
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]{3,3}     { 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 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 //FIXME The linenumber sometimes is wrong.
153
154 static void interpret_cppinfo(const char *str)
155 {
156         const char * const orig = str;
157         char tmp[64];
158         char *found;
159
160         /* This will interpret lines added by CPP.
161          * They look like:
162          *     # 3 "file.asm" 1
163          */
164
165         if (*str == '\0')
166                 goto error;
167         str++; /* skip # character */
168         if (*str == '\0')
169                 goto error;
170         str++; /* skip whitespace */
171
172         /* Line number */
173         found = strchr(str, ' ');
174         if (!found)
175                 goto error;
176         memset(tmp, 0, sizeof(tmp));
177         memcpy(tmp, str, min(sizeof(tmp) - 1,
178                              (int)(found - str)));
179         cur_lineinfo.lineno = strtoul(tmp, NULL, 10) - 1;
180         str = found;
181         str++;
182
183         /* File name */
184         if (*str != '\"')
185                 goto error;
186         str++;
187         if (*str == '\0')
188                 goto error;
189         found = strchr(str, '\"');
190         if (!found)
191                 goto error;
192         memset(cur_lineinfo.file, 0, sizeof(cur_lineinfo.file));
193         memcpy(cur_lineinfo.file, str,
194                min(sizeof(cur_lineinfo.file) - 1,
195                    (int)(found - str)));
196
197         return;
198 error:
199         fprintf(stderr, "Invalid CPP line directive:  %s\n", orig);
200         exit(1);
201 }
202
203 static void update_lineinfo(void)
204 {
205         int i = 0;
206
207         while (yytext[i] != '\0') {
208                 switch (yytext[i]) {
209                 case '\r':
210                 case '\n':
211                         cur_lineinfo.column = 0;
212                         break;
213                 case '\t':
214                         cur_lineinfo.column += 8 - (cur_lineinfo.column % 8);
215                         break;
216                 default:
217                         cur_lineinfo.column++;
218                 }
219                 i++;
220         }
221 }