1 %option noyywrap noinput nounput
2 %option 8bit reentrant bison-bridge
3 %option warn nodefault
4 %option bison-locations
5 
6 %{
7 /*
8  *  Copyright(c) 2019-2022 rev.ng Labs Srl. All Rights Reserved.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23 
24 #include <string.h>
25 #include <stdbool.h>
26 
27 #include "hex_regs.h"
28 
29 #include "idef-parser.h"
30 #include "idef-parser.tab.h"
31 
32 /* Keep track of scanner position for error message printout */
33 #define YY_USER_ACTION yylloc->first_column = yylloc->last_column; \
34     for (int i = 0; yytext[i] != '\0'; i++) {   \
35         yylloc->last_column++;                  \
36     }
37 
38 /* Global Error Counter */
39 int error_count;
40 
41 %}
42 
43 /* Definitions */
44 DIGIT                    [0-9]
45 LOWER_ID                 [a-z]
46 UPPER_ID                 [A-Z]
47 ID                       LOWER_ID|UPPER_ID
48 INST_NAME                [A-Z]+[0-9]_([A-Za-z]|[0-9]|_)+
49 HEX_DIGIT                [0-9a-fA-F]
50 REG_ID_32                e|s|d|t|u|v|x|y
51 REG_ID_64                ee|ss|dd|tt|uu|vv|xx|yy
52 SYS_ID_32                s|d
53 SYS_ID_64                ss|dd
54 PRED_ID                  d|s|t|u|v|e|x|x
55 IMM_ID                   r|s|S|u|U
56 VAR_ID                   [a-zA-Z_][a-zA-Z0-9_]*
57 SIGN_ID                  s|u
58 STRING_LIT               \"(\\.|[^"\\])*\"
59 
60 /* Tokens */
61 %%
62 
63 [ \t\f\v]+                { /* Ignore whitespaces. */ }
64 [\n\r]+                   { /* Ignore newlines. */ }
65 ^#.*$                     { /* Ignore linemarkers. */ }
66 
67 {INST_NAME}               { yylval->string = g_string_new(yytext);
68                             return INAME; }
69 "fFLOAT"                 |
70 "fUNFLOAT"               |
71 "fDOUBLE"                |
72 "fUNDOUBLE"              |
73 "0.0"                    |
74 "0x1.0p52"               |
75 "0x1.0p-52"              { return FAIL; }
76 "in"                     { return IN; }
77 "R"{REG_ID_32}"V" {
78                            yylval->rvalue.type = REGISTER_ARG;
79                            yylval->rvalue.reg.type = GENERAL_PURPOSE;
80                            yylval->rvalue.reg.id = yytext[1];
81                            yylval->rvalue.reg.bit_width = 32;
82                            yylval->rvalue.bit_width = 32;
83                            yylval->rvalue.is_dotnew = false;
84                            yylval->rvalue.signedness = SIGNED;
85                            return REG; }
86 "R"{REG_ID_64}"V" {
87                            yylval->rvalue.type = REGISTER_ARG;
88                            yylval->rvalue.reg.type = GENERAL_PURPOSE;
89                            yylval->rvalue.reg.id = yytext[1];
90                            yylval->rvalue.reg.bit_width = 64;
91                            yylval->rvalue.bit_width = 64;
92                            yylval->rvalue.is_dotnew = false;
93                            yylval->rvalue.signedness = SIGNED;
94                            return REG; }
95 "MuV" {
96                            yylval->rvalue.type = REGISTER_ARG;
97                            yylval->rvalue.reg.type = MODIFIER;
98                            yylval->rvalue.reg.id = 'u';
99                            yylval->rvalue.reg.bit_width = 32;
100                            yylval->rvalue.bit_width = 32;
101                            yylval->rvalue.signedness = SIGNED;
102                            return REG; }
103 "C"{REG_ID_32}"V" {
104                            yylval->rvalue.type = REGISTER_ARG;
105                            yylval->rvalue.reg.type = CONTROL;
106                            yylval->rvalue.reg.id = yytext[1];
107                            yylval->rvalue.reg.bit_width = 32;
108                            yylval->rvalue.bit_width = 32;
109                            yylval->rvalue.is_dotnew = false;
110                            yylval->rvalue.signedness = SIGNED;
111                            return REG; }
112 "C"{REG_ID_64}"V" {
113                            yylval->rvalue.type = REGISTER_ARG;
114                            yylval->rvalue.reg.type = CONTROL;
115                            yylval->rvalue.reg.id = yytext[1];
116                            yylval->rvalue.reg.bit_width = 64;
117                            yylval->rvalue.bit_width = 64;
118                            yylval->rvalue.is_dotnew = false;
119                            yylval->rvalue.signedness = SIGNED;
120                            return REG; }
121 {IMM_ID}"iV" {
122                            yylval->rvalue.type = IMMEDIATE;
123                            yylval->rvalue.signedness = SIGNED;
124                            yylval->rvalue.imm.type = VARIABLE;
125                            yylval->rvalue.imm.id = yytext[0];
126                            yylval->rvalue.bit_width = 32;
127                            yylval->rvalue.is_dotnew = false;
128                            return IMM; }
129 "P"{PRED_ID}"V" {
130                            yylval->rvalue.type = PREDICATE;
131                            yylval->rvalue.pred.id = yytext[1];
132                            yylval->rvalue.bit_width = 32;
133                            yylval->rvalue.is_dotnew = false;
134                            yylval->rvalue.signedness = SIGNED;
135                            return PRED; }
136 "P"{PRED_ID}"N" {
137                            yylval->rvalue.type = PREDICATE;
138                            yylval->rvalue.pred.id = yytext[1];
139                            yylval->rvalue.bit_width = 32;
140                            yylval->rvalue.is_dotnew = true;
141                            yylval->rvalue.signedness = SIGNED;
142                            return PRED; }
143 "IV1DEAD()"              |
144 "fPAUSE(uiV);"           { return ';'; }
145 "+="                     { return INC; }
146 "-="                     { return DEC; }
147 "++"                     { return PLUSPLUS; }
148 "&="                     { return ANDA; }
149 "|="                     { return ORA; }
150 "^="                     { return XORA; }
151 "<<"                     { return ASL; }
152 ">>"                     { return ASR; }
153 ">>>"                    { return LSR; }
154 "=="                     { return EQ; }
155 "!="                     { return NEQ; }
156 "<="                     { return LTE; }
157 ">="                     { return GTE; }
158 "&&"                     { return ANDL; }
159 "else"                   { return ELSE; }
160 "for"                    { return FOR; }
161 "fREAD_IREG"             { return ICIRC; }
162 "fPART1"                 { return PART1; }
163 "if"                     { return IF; }
164 "fFRAME_SCRAMBLE"        { return FSCR; }
165 "fFRAME_UNSCRAMBLE"      { return FSCR; }
166 "fFRAMECHECK"            { return FCHK; }
167 "Constant_extended"      { return CONSTEXT; }
168 "fCL1_"{DIGIT}           { return LOCNT; }
169 "fbrev"                  { return BREV; }
170 "fSXTN"                  { return SXT; }
171 "fZXTN"                  { return ZXT; }
172 "fDF_MAX"                |
173 "fSF_MAX"                |
174 "fMAX"                   { return MAX; }
175 "fDF_MIN"                |
176 "fSF_MIN"                |
177 "fMIN"                   { return MIN; }
178 "fABS"                   { return ABS; }
179 "fRNDN"                  { return ROUND; }
180 "fCRND"                  { return CROUND; }
181 "fCRNDN"                 { return CROUND; }
182 "fPM_CIRI"               { return CIRCADD; }
183 "fPM_CIRR"               { return CIRCADD; }
184 "fCOUNTONES_"{DIGIT}     { return COUNTONES; }
185 "fSATN"                  { yylval->sat.signedness = SIGNED;
186                            return SAT; }
187 "fSATUN"                 { yylval->sat.signedness = UNSIGNED;
188                            return SAT; }
189 "fCONSTLL"               { yylval->cast.bit_width = 64;
190                            yylval->cast.signedness = SIGNED;
191                            return CAST; }
192 "fSE32_64"               { yylval->cast.bit_width = 64;
193                            yylval->cast.signedness = SIGNED;
194                            return CAST; }
195 "fCAST4_4u"              { yylval->cast.bit_width = 32;
196                            yylval->cast.signedness = UNSIGNED;
197                            return CAST; }
198 "fCAST4_8s"              { yylval->cast.bit_width = 64;
199                            yylval->cast.signedness = SIGNED;
200                            return CAST; }
201 "fCAST4_8u"              { return CAST4_8U; }
202 "fCAST4u"                { yylval->cast.bit_width = 32;
203                            yylval->cast.signedness = UNSIGNED;
204                            return CAST; }
205 "fNEWREG"                |
206 "fCAST4_4s"              |
207 "fCAST4s"                { yylval->cast.bit_width = 32;
208                            yylval->cast.signedness = SIGNED;
209                            return CAST; }
210 "fCAST8_8u"              { yylval->cast.bit_width = 64;
211                            yylval->cast.signedness = UNSIGNED;
212                            return CAST; }
213 "fCAST8u"                { yylval->cast.bit_width = 64;
214                            yylval->cast.signedness = UNSIGNED;
215                            return CAST; }
216 "fCAST8_8s"              |
217 "fCAST8s"                { yylval->cast.bit_width = 64;
218                            yylval->cast.signedness = SIGNED;
219                            return CAST; }
220 "fGETBIT"                { yylval->extract.bit_width = 1;
221                            yylval->extract.storage_bit_width = 1;
222                            yylval->extract.signedness = UNSIGNED;
223                            return EXTRACT; }
224 "fGETBYTE"               { yylval->extract.bit_width = 8;
225                            yylval->extract.storage_bit_width = 8;
226                            yylval->extract.signedness = SIGNED;
227                            return EXTRACT; }
228 "fGETUBYTE"              { yylval->extract.bit_width = 8;
229                            yylval->extract.storage_bit_width = 8;
230                            yylval->extract.signedness = UNSIGNED;
231                            return EXTRACT; }
232 "fGETHALF"               { yylval->extract.bit_width = 16;
233                            yylval->extract.storage_bit_width = 16;
234                            yylval->extract.signedness = SIGNED;
235                            return EXTRACT; }
236 "fGETUHALF"              { yylval->extract.bit_width = 16;
237                            yylval->extract.storage_bit_width = 16;
238                            yylval->extract.signedness = UNSIGNED;
239                            return EXTRACT; }
240 "fGETWORD"               { yylval->extract.bit_width = 32;
241                            yylval->extract.storage_bit_width = 64;
242                            yylval->extract.signedness = SIGNED;
243                            return EXTRACT; }
244 "fGETUWORD"              { yylval->extract.bit_width = 32;
245                            yylval->extract.storage_bit_width = 64;
246                            yylval->extract.signedness = UNSIGNED;
247                            return EXTRACT; }
248 "fEXTRACTU_RANGE"        { return EXTRANGE; }
249 "fSETBIT"                { yylval->cast.bit_width = 1;
250                            yylval->cast.signedness = SIGNED;
251                            return DEPOSIT; }
252 "fSETBYTE"               { yylval->cast.bit_width = 8;
253                            yylval->cast.signedness = SIGNED;
254                            return DEPOSIT; }
255 "fSETHALF"               { yylval->cast.bit_width = 16;
256                            yylval->cast.signedness = SIGNED;
257                            return SETHALF; }
258 "fSETWORD"               { yylval->cast.bit_width = 32;
259                            yylval->cast.signedness = SIGNED;
260                            return DEPOSIT; }
261 "fINSERT_BITS"           { return INSBITS; }
262 "fSETBITS"               { return SETBITS; }
263 "fMPY16UU"               { yylval->mpy.first_bit_width = 16;
264                            yylval->mpy.second_bit_width = 16;
265                            yylval->mpy.first_signedness = UNSIGNED;
266                            yylval->mpy.second_signedness = UNSIGNED;
267                            return MPY; }
268 "fMPY16SU"               { yylval->mpy.first_bit_width = 16;
269                            yylval->mpy.second_bit_width = 16;
270                            yylval->mpy.first_signedness = SIGNED;
271                            yylval->mpy.second_signedness = UNSIGNED;
272                            return MPY; }
273 "fMPY16SS"               { yylval->mpy.first_bit_width = 16;
274                            yylval->mpy.second_bit_width = 16;
275                            yylval->mpy.first_signedness = SIGNED;
276                            yylval->mpy.second_signedness = SIGNED;
277                            return MPY; }
278 "fMPY32UU"               { yylval->mpy.first_bit_width = 32;
279                            yylval->mpy.second_bit_width = 32;
280                            yylval->mpy.first_signedness = UNSIGNED;
281                            yylval->mpy.second_signedness = UNSIGNED;
282                            return MPY; }
283 "fMPY32SU"               { yylval->mpy.first_bit_width = 32;
284                            yylval->mpy.second_bit_width = 32;
285                            yylval->mpy.first_signedness = SIGNED;
286                            yylval->mpy.second_signedness = UNSIGNED;
287                            return MPY; }
288 "fSFMPY"                 |
289 "fMPY32SS"               { yylval->mpy.first_bit_width = 32;
290                            yylval->mpy.second_bit_width = 32;
291                            yylval->mpy.first_signedness = SIGNED;
292                            yylval->mpy.second_signedness = SIGNED;
293                            return MPY; }
294 "fMPY3216SS"             { yylval->mpy.first_bit_width = 32;
295                            yylval->mpy.second_bit_width = 16;
296                            yylval->mpy.first_signedness = SIGNED;
297                            yylval->mpy.second_signedness = SIGNED;
298                            return MPY; }
299 "fMPY3216SU"             { yylval->mpy.first_bit_width = 32;
300                            yylval->mpy.second_bit_width = 16;
301                            yylval->mpy.first_signedness = SIGNED;
302                            yylval->mpy.second_signedness = UNSIGNED;
303                            return MPY; }
304 "fNEWREG_ST"             |
305 "fIMMEXT"                |
306 "fMUST_IMMEXT"           |
307 "fPASS"                  |
308 "fECHO"                  { return IDENTITY; }
309 "(size8u_t)"             { yylval->cast.bit_width = 64;
310                            yylval->cast.signedness = UNSIGNED;
311                            return CAST; }
312 "(unsigned int)"         { yylval->cast.bit_width = 32;
313                            yylval->cast.signedness = UNSIGNED;
314                            return CAST; }
315 "fREAD_PC()"             |
316 "PC"                     { return PC; }
317 "fREAD_NPC()"            |
318 "NPC"                    { return NPC; }
319 "fGET_LPCFG"             |
320 "USR.LPCFG"              { return LPCFG; }
321 "LOAD_CANCEL(EA)"        { return LOAD_CANCEL; }
322 "STORE_CANCEL(EA)"       |
323 "CANCEL"                 { return CANCEL; }
324 "N"{LOWER_ID}"N"         { yylval->rvalue.type = REGISTER_ARG;
325                            yylval->rvalue.reg.type = DOTNEW;
326                            yylval->rvalue.reg.id = yytext[1];
327                            yylval->rvalue.reg.bit_width = 32;
328                            yylval->rvalue.bit_width = 32;
329                            yylval->rvalue.signedness = UNSIGNED;
330                            return REG; }
331 "fREAD_SP()"             |
332 "SP"                     { yylval->rvalue.type = REGISTER;
333                            yylval->rvalue.reg.type = GENERAL_PURPOSE;
334                            yylval->rvalue.reg.id = HEX_REG_SP;
335                            yylval->rvalue.reg.bit_width = 32;
336                            yylval->rvalue.bit_width = 32;
337                            yylval->rvalue.signedness = UNSIGNED;
338                            return REG; }
339 "fREAD_FP()"             |
340 "FP"                     { yylval->rvalue.type = REGISTER;
341                            yylval->rvalue.reg.type = GENERAL_PURPOSE;
342                            yylval->rvalue.reg.id = HEX_REG_FP;
343                            yylval->rvalue.reg.bit_width = 32;
344                            yylval->rvalue.bit_width = 32;
345                            yylval->rvalue.signedness = UNSIGNED;
346                            return REG; }
347 "fREAD_LR()"             |
348 "LR"                     { yylval->rvalue.type = REGISTER;
349                            yylval->rvalue.reg.type = GENERAL_PURPOSE;
350                            yylval->rvalue.reg.id = HEX_REG_LR;
351                            yylval->rvalue.reg.bit_width = 32;
352                            yylval->rvalue.bit_width = 32;
353                            yylval->rvalue.signedness = UNSIGNED;
354                            return REG; }
355 "fREAD_GP()"             |
356 "GP"                     { yylval->rvalue.type = REGISTER;
357                            yylval->rvalue.reg.type = CONTROL;
358                            yylval->rvalue.reg.id = HEX_REG_GP;
359                            yylval->rvalue.reg.bit_width = 32;
360                            yylval->rvalue.bit_width = 32;
361                            yylval->rvalue.signedness = UNSIGNED;
362                            return REG; }
363 "fREAD_LC"[01]           { yylval->rvalue.type = REGISTER;
364                            yylval->rvalue.reg.type = CONTROL;
365                            yylval->rvalue.reg.id = HEX_REG_LC0
366                                                  + (yytext[8] - '0') * 2;
367                            yylval->rvalue.reg.bit_width = 32;
368                            yylval->rvalue.bit_width = 32;
369                            yylval->rvalue.signedness = UNSIGNED;
370                            return REG; }
371 "LC"[01]                 { yylval->rvalue.type = REGISTER;
372                            yylval->rvalue.reg.type = CONTROL;
373                            yylval->rvalue.reg.id = HEX_REG_LC0
374                                                  + (yytext[2] - '0') * 2;
375                            yylval->rvalue.reg.bit_width = 32;
376                            yylval->rvalue.bit_width = 32;
377                            yylval->rvalue.signedness = UNSIGNED;
378                            return REG; }
379 "fREAD_SA"[01]           { yylval->rvalue.type = REGISTER;
380                            yylval->rvalue.reg.type = CONTROL;
381                            yylval->rvalue.reg.id = HEX_REG_SA0
382                                                  + (yytext[8] - '0') * 2;
383                            yylval->rvalue.reg.bit_width = 32;
384                            yylval->rvalue.bit_width = 32;
385                            yylval->rvalue.signedness = UNSIGNED;
386                            return REG; }
387 "SA"[01]                 { yylval->rvalue.type = REGISTER;
388                            yylval->rvalue.reg.type = CONTROL;
389                            yylval->rvalue.reg.id = HEX_REG_SA0
390                                                  + (yytext[2] - '0') * 2;
391                            yylval->rvalue.reg.bit_width = 32;
392                            yylval->rvalue.bit_width = 32;
393                            yylval->rvalue.signedness = UNSIGNED;
394                            return REG; }
395 "fREAD_P0()"             { yylval->rvalue.type = PREDICATE;
396                            yylval->rvalue.pred.id = '0';
397                            yylval->rvalue.bit_width = 32;
398                            return PRED; }
399 [pP]{DIGIT}              { yylval->rvalue.type = PREDICATE;
400                            yylval->rvalue.pred.id = yytext[1];
401                            yylval->rvalue.bit_width = 32;
402                            yylval->rvalue.is_dotnew = false;
403                            return PRED; }
404 [pP]{DIGIT}[nN]          { yylval->rvalue.type = PREDICATE;
405                            yylval->rvalue.pred.id = yytext[1];
406                            yylval->rvalue.bit_width = 32;
407                            yylval->rvalue.is_dotnew = true;
408                            return PRED; }
409 "fLSBNEW"                { return LSBNEW; }
410 "N"                      { yylval->rvalue.type = IMMEDIATE;
411                            yylval->rvalue.bit_width = 32;
412                            yylval->rvalue.imm.type = VARIABLE;
413                            yylval->rvalue.imm.id = 'N';
414                            return IMM; }
415 "i"                      { yylval->rvalue.type = IMMEDIATE;
416                            yylval->rvalue.bit_width = 32;
417                            yylval->rvalue.signedness = SIGNED;
418                            yylval->rvalue.imm.type = I;
419                            return IMM; }
420 {SIGN_ID}                { if (yytext[0] == 'u') {
421                                yylval->signedness = UNSIGNED;
422                            } else {
423                                yylval->signedness = SIGNED;
424                            }
425                            return SIGN;
426                          }
427 "0x"{HEX_DIGIT}+         |
428 {DIGIT}+                 { yylval->rvalue.type = IMMEDIATE;
429                            yylval->rvalue.bit_width = 32;
430                            yylval->rvalue.signedness = SIGNED;
431                            yylval->rvalue.imm.type = VALUE;
432                            yylval->rvalue.imm.value = strtoull(yytext, NULL, 0);
433                            return IMM; }
434 "0x"{HEX_DIGIT}+"ULL"    |
435 {DIGIT}+"ULL"            { yylval->rvalue.type = IMMEDIATE;
436                            yylval->rvalue.bit_width = 64;
437                            yylval->rvalue.signedness = UNSIGNED;
438                            yylval->rvalue.imm.type = VALUE;
439                            yylval->rvalue.imm.value = strtoull(yytext, NULL, 0);
440                            return IMM; }
441 "fLOAD"                  { return LOAD; }
442 "fSTORE"                 { return STORE; }
443 "fROTL"                  { return ROTL; }
444 "fCARRY_FROM_ADD"        { return CARRY_FROM_ADD; }
445 "fADDSAT64"              { return ADDSAT64; }
446 "size"[1248][us]"_t"     { /* Handles "size_t" variants of int types */
447                            const unsigned int bits_per_byte = 8;
448                            const unsigned int bytes = yytext[4] - '0';
449                            yylval->rvalue.bit_width = bits_per_byte * bytes;
450                            if (yytext[5] == 'u') {
451                                yylval->rvalue.signedness = UNSIGNED;
452                            } else {
453                                yylval->rvalue.signedness = SIGNED;
454                            }
455                            return TYPE_SIZE_T; }
456 "unsigned"               { return TYPE_UNSIGNED; }
457 "long"                   { return TYPE_LONG; }
458 "int"                    { return TYPE_INT; }
459 "const"                  { /* Emit no token */ }
460 {VAR_ID}                 { /* Variable name, we adopt the C names convention */
461                            yylval->rvalue.type = VARID;
462                            yylval->rvalue.var.name = g_string_new(yytext);
463                            /* Default to an unknown signedness and 0 width. */
464                            yylval->rvalue.bit_width = 0;
465                            yylval->rvalue.signedness = UNKNOWN_SIGNEDNESS;
466                            return VAR; }
467 "fatal("{STRING_LIT}")"  { /* Emit no token */ }
468 "fHINTJR(RsV)"           { /* Emit no token */ }
469 .                        { return yytext[0]; }
470 
471 %%
472