1 /*
2  * Copyright (c) 2013 Rob Clark <robclark@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 
24 %{
25 #include <stdlib.h>
26 #include "util/ralloc.h"
27 #include "ir3/ir3.h"
28 #include "ir3_parser.h"
29 
30 #define YY_NO_INPUT
31 #define YY_NO_UNPUT
32 #define TOKEN(t) (ir3_yylval.tok = t)
33 extern YYSTYPE ir3_yylval;
34 extern void *ir3_parser_dead_ctx;
35 
36 void ir3_yyset_input(FILE *f);
37 
ir3_yyset_input(FILE * f)38 void ir3_yyset_input(FILE *f)
39 {
40 	YY_FLUSH_BUFFER;
41 	ir3_yyin = f;
42 }
43 
parse_wrmask(const char * src)44 static int parse_wrmask(const char *src)
45 {
46 	int i, num = 0;
47 	for (i = 0; i < 4; i++) {
48 		if ("xyzw"[i] == src[1]) {
49 			num |= (1 << i);
50 			src++;
51 		}
52 	}
53 	return num;
54 }
55 
parse_reg(const char * str)56 static int parse_reg(const char *str)
57 {
58 	int num = 0;
59 	if (str[0] == 'h') {
60 		str++;
61 		num++;
62 	}
63 	str++;
64 	num += strtol(str, (char **)&str, 10) << 3;
65 	switch (str[1]) {
66 	case 'x': num += 0; break;
67 	case 'y': num += 2; break;
68 	case 'z': num += 4; break;
69 	case 'w': num += 6; break;
70 	default: assert(0); break;
71 	}
72 	return num;
73 }
74 
parse_w(const char * str)75 static int parse_w(const char *str)
76 {
77 	str++;
78 	unsigned num = strtol(str, NULL, 10);
79 	if ((num % 32) != 0)
80 		yy_fatal_error("w# must be multiple of 32");
81 	if (num < 32)
82 		yy_fatal_error("w# must be at least 32");
83 	return num / 32;
84 }
85 %}
86 
87 %option noyywrap
88 %option prefix="ir3_yy"
89 
90 %%
91 "\n"                              yylineno++;
92 [ \t]                             ; /* ignore whitespace */
93 ";"[^\n]*"\n"                     yylineno++; /* ignore comments */
94 "(0.0)"                           ir3_yylval.num = 0;  return T_FLUT_0_0;
95 "(0.5)"                           ir3_yylval.num = 1;  return T_FLUT_0_5;
96 "(1.0)"                           ir3_yylval.num = 2;  return T_FLUT_1_0;
97 "(2.0)"                           ir3_yylval.num = 3;  return T_FLUT_2_0;
98 "(e)"                             ir3_yylval.num = 4;  return T_FLUT_E;
99 "(pi)"                            ir3_yylval.num = 5;  return T_FLUT_PI;
100 "(1/pi)"                          ir3_yylval.num = 6;  return T_FLUT_INV_PI;
101 "(1/log2(e))"                     ir3_yylval.num = 7;  return T_FLUT_INV_LOG2_E;
102 "(log2(e))"                       ir3_yylval.num = 8;  return T_FLUT_LOG2_E;
103 "(1/log2(10))"                    ir3_yylval.num = 9;  return T_FLUT_INV_LOG2_10;
104 "(log2(10))"                      ir3_yylval.num = 10; return T_FLUT_LOG2_10;
105 "(4.0)"                           ir3_yylval.num = 11; return T_FLUT_4_0;
106 [0-9]+"."[0-9]+                   ir3_yylval.flt = strtod(yytext, NULL);       return T_FLOAT;
107 [0-9]*                            ir3_yylval.num = strtoul(yytext, NULL, 0);    return T_INT;
108 "0x"[0-9a-fA-F]*                  ir3_yylval.num = strtoul(yytext, NULL, 0);    return T_HEX;
109 "@localsize"                      return TOKEN(T_A_LOCALSIZE);
110 "@const"                          return TOKEN(T_A_CONST);
111 "@buf"                            return TOKEN(T_A_BUF);
112 "@invocationid"                   return TOKEN(T_A_INVOCATIONID);
113 "@wgid"                           return TOKEN(T_A_WGID);
114 "@numwg"                          return TOKEN(T_A_NUMWG);
115 "@branchstack"                    return TOKEN(T_A_BRANCHSTACK);
116 "@in"                             return TOKEN(T_A_IN);
117 "@out"                            return TOKEN(T_A_OUT);
118 "@tex"                            return TOKEN(T_A_TEX);
119 "@pvtmem"                         return TOKEN(T_A_PVTMEM);
120 "(sy)"                            return TOKEN(T_SY);
121 "(ss)"                            return TOKEN(T_SS);
122 "(absneg)"                        return TOKEN(T_ABSNEG);
123 "(neg)"                           return TOKEN(T_NEG);
124 "(abs)"                           return TOKEN(T_ABS);
125 "(r)"                             return TOKEN(T_R);
126 "(ul)"                            return TOKEN(T_UL);
127 "(even)"                          return TOKEN(T_EVEN);
128 "(pos_infinity)"                  return TOKEN(T_POS_INFINITY);
129 "(neg_infinity)"                  return TOKEN(T_NEG_INFINITY);
130 "(ei)"                            return TOKEN(T_EI);
131 "(jp)"                            return TOKEN(T_JP);
132 "(sat)"                           return TOKEN(T_SAT);
133 "(rpt"[0-7]")"                    ir3_yylval.num = strtol(yytext+4, NULL, 10); return T_RPT;
134 "(nop"[0-7]")"                    ir3_yylval.num = strtol(yytext+4, NULL, 10); return T_NOP;
135 "("[x]?[y]?[z]?[w]?")"            ir3_yylval.num = parse_wrmask(yytext); return T_WRMASK;
136 
137 [h]?"r"[0-9]+"."[xyzw]            ir3_yylval.num = parse_reg(yytext); return T_REGISTER;
138 [h]?"c"[0-9]+"."[xyzw]            ir3_yylval.num = parse_reg(yytext); return T_CONSTANT;
139 "a0.x"                            return T_A0;
140 "a1.x"                            return T_A1;
141 "p0."[xyzw]                       ir3_yylval.num = parse_reg(yytext); return T_P0;
142 "w"[0-9]+                         ir3_yylval.num = parse_w(yytext);   return T_W;
143 "s#"[0-9]+                        ir3_yylval.num = strtol(yytext+2, NULL, 10); return T_SAMP;
144 "t#"[0-9]+                        ir3_yylval.num = strtol(yytext+2, NULL, 10); return T_TEX;
145 
146                                   /* category 0: */
147 "nop"                             return TOKEN(T_OP_NOP);
148 "br"                              return TOKEN(T_OP_BR);
149 "brao"                            return TOKEN(T_OP_BRAO);
150 "braa"                            return TOKEN(T_OP_BRAA);
151 "brac"                            return TOKEN(T_OP_BRAC);
152 "bany"                            return TOKEN(T_OP_BANY);
153 "ball"                            return TOKEN(T_OP_BALL);
154 "brax"                            return TOKEN(T_OP_BRAX);
155 "jump"                            return TOKEN(T_OP_JUMP);
156 "call"                            return TOKEN(T_OP_CALL);
157 "ret"                             return TOKEN(T_OP_RET);
158 "kill"                            return TOKEN(T_OP_KILL);
159 "end"                             return TOKEN(T_OP_END);
160 "emit"                            return TOKEN(T_OP_EMIT);
161 "cut"                             return TOKEN(T_OP_CUT);
162 "chmask"                          return TOKEN(T_OP_CHMASK);
163 "chsh"                            return TOKEN(T_OP_CHSH);
164 "flow_rev"                        return TOKEN(T_OP_FLOW_REV);
165 "bkt"                             return TOKEN(T_OP_BKT);
166 "stks"                            return TOKEN(T_OP_STKS);
167 "stkr"                            return TOKEN(T_OP_STKR);
168 "xset"                            return TOKEN(T_OP_XSET);
169 "xclr"                            return TOKEN(T_OP_XCLR);
170 "getone"                          return TOKEN(T_OP_GETONE);
171 "dbg"                             return TOKEN(T_OP_DBG);
172 "shps"                            return TOKEN(T_OP_SHPS);
173 "shpe"                            return TOKEN(T_OP_SHPE);
174 "predt"                           return TOKEN(T_OP_PREDT);
175 "predf"                           return TOKEN(T_OP_PREDF);
176 "prede"                           return TOKEN(T_OP_PREDE);
177 
178                                   /* category 1: */
179 "movmsk"                          return TOKEN(T_OP_MOVMSK);
180 "mova1"                           return TOKEN(T_OP_MOVA1);
181 "mova"                            return TOKEN(T_OP_MOVA);
182 "mov"                             return TOKEN(T_OP_MOV);
183 "cov"                             return TOKEN(T_OP_COV);
184 "swz"                             return TOKEN(T_OP_SWZ);
185 "gat"                             return TOKEN(T_OP_GAT);
186 "sct"                             return TOKEN(T_OP_SCT);
187 
188 ("f16"|"f32"|"u16"|"u32"|"s16"|"s32"|"u8"|"s8"){2} ir3_yylval.str = yytext; return T_CAT1_TYPE_TYPE;
189 
190                                   /* category 2: */
191 "add.f"                           return TOKEN(T_OP_ADD_F);
192 "min.f"                           return TOKEN(T_OP_MIN_F);
193 "max.f"                           return TOKEN(T_OP_MAX_F);
194 "mul.f"                           return TOKEN(T_OP_MUL_F);
195 "sign.f"                          return TOKEN(T_OP_SIGN_F);
196 "cmps.f"                          return TOKEN(T_OP_CMPS_F);
197 "absneg.f"                        return TOKEN(T_OP_ABSNEG_F);
198 "cmpv.f"                          return TOKEN(T_OP_CMPV_F);
199 "floor.f"                         return TOKEN(T_OP_FLOOR_F);
200 "ceil.f"                          return TOKEN(T_OP_CEIL_F);
201 "rndne.f"                         return TOKEN(T_OP_RNDNE_F);
202 "rndaz.f"                         return TOKEN(T_OP_RNDAZ_F);
203 "trunc.f"                         return TOKEN(T_OP_TRUNC_F);
204 "add.u"                           return TOKEN(T_OP_ADD_U);
205 "add.s"                           return TOKEN(T_OP_ADD_S);
206 "sub.u"                           return TOKEN(T_OP_SUB_U);
207 "sub.s"                           return TOKEN(T_OP_SUB_S);
208 "cmps.u"                          return TOKEN(T_OP_CMPS_U);
209 "cmps.s"                          return TOKEN(T_OP_CMPS_S);
210 "min.u"                           return TOKEN(T_OP_MIN_U);
211 "min.s"                           return TOKEN(T_OP_MIN_S);
212 "max.u"                           return TOKEN(T_OP_MAX_U);
213 "max.s"                           return TOKEN(T_OP_MAX_S);
214 "absneg.s"                        return TOKEN(T_OP_ABSNEG_S);
215 "and.b"                           return TOKEN(T_OP_AND_B);
216 "or.b"                            return TOKEN(T_OP_OR_B);
217 "not.b"                           return TOKEN(T_OP_NOT_B);
218 "xor.b"                           return TOKEN(T_OP_XOR_B);
219 "cmpv.u"                          return TOKEN(T_OP_CMPV_U);
220 "cmpv.s"                          return TOKEN(T_OP_CMPV_S);
221 "mul.u24"                         return TOKEN(T_OP_MUL_U24);
222 "mul.s24"                         return TOKEN(T_OP_MUL_S24);
223 "mull.u"                          return TOKEN(T_OP_MULL_U);
224 "bfrev.b"                         return TOKEN(T_OP_BFREV_B);
225 "clz.s"                           return TOKEN(T_OP_CLZ_S);
226 "clz.b"                           return TOKEN(T_OP_CLZ_B);
227 "shl.b"                           return TOKEN(T_OP_SHL_B);
228 "shr.b"                           return TOKEN(T_OP_SHR_B);
229 "ashr.b"                          return TOKEN(T_OP_ASHR_B);
230 "bary.f"                          return TOKEN(T_OP_BARY_F);
231 "mgen.b"                          return TOKEN(T_OP_MGEN_B);
232 "getbit.b"                        return TOKEN(T_OP_GETBIT_B);
233 "setrm"                           return TOKEN(T_OP_SETRM);
234 "cbits.b"                         return TOKEN(T_OP_CBITS_B);
235 "shb"                             return TOKEN(T_OP_SHB);
236 "msad"                            return TOKEN(T_OP_MSAD);
237 
238                                   /* category 3: */
239 "mad.u16"                         return TOKEN(T_OP_MAD_U16);
240 "madsh.u16"                       return TOKEN(T_OP_MADSH_U16);
241 "mad.s16"                         return TOKEN(T_OP_MAD_S16);
242 "madsh.m16"                       return TOKEN(T_OP_MADSH_M16);
243 "mad.u24"                         return TOKEN(T_OP_MAD_U24);
244 "mad.s24"                         return TOKEN(T_OP_MAD_S24);
245 "mad.f16"                         return TOKEN(T_OP_MAD_F16);
246 "mad.f32"                         return TOKEN(T_OP_MAD_F32);
247 "sel.b16"                         return TOKEN(T_OP_SEL_B16);
248 "sel.b32"                         return TOKEN(T_OP_SEL_B32);
249 "sel.s16"                         return TOKEN(T_OP_SEL_S16);
250 "sel.s32"                         return TOKEN(T_OP_SEL_S32);
251 "sel.f16"                         return TOKEN(T_OP_SEL_F16);
252 "sel.f32"                         return TOKEN(T_OP_SEL_F32);
253 "sad.s16"                         return TOKEN(T_OP_SAD_S16);
254 "sad.s32"                         return TOKEN(T_OP_SAD_S32);
255 "shlg.b16"                        return TOKEN(T_OP_SHLG_B16);
256 
257                                   /* category 4: */
258 "rcp"                             return TOKEN(T_OP_RCP);
259 "rsq"                             return TOKEN(T_OP_RSQ);
260 "log2"                            return TOKEN(T_OP_LOG2);
261 "exp2"                            return TOKEN(T_OP_EXP2);
262 "sin"                             return TOKEN(T_OP_SIN);
263 "cos"                             return TOKEN(T_OP_COS);
264 "sqrt"                            return TOKEN(T_OP_SQRT);
265 "hrsq"                            return TOKEN(T_OP_HRSQ);
266 "hlog2"                           return TOKEN(T_OP_HLOG2);
267 "hexp2"                           return TOKEN(T_OP_HEXP2);
268 
269                                   /* category 5: */
270 "isam"                            return TOKEN(T_OP_ISAM);
271 "isaml"                           return TOKEN(T_OP_ISAML);
272 "isamm"                           return TOKEN(T_OP_ISAMM);
273 "sam"                             return TOKEN(T_OP_SAM);
274 "samb"                            return TOKEN(T_OP_SAMB);
275 "saml"                            return TOKEN(T_OP_SAML);
276 "samgq"                           return TOKEN(T_OP_SAMGQ);
277 "getlod"                          return TOKEN(T_OP_GETLOD);
278 "conv"                            return TOKEN(T_OP_CONV);
279 "convm"                           return TOKEN(T_OP_CONVM);
280 "getsize"                         return TOKEN(T_OP_GETSIZE);
281 "getbuf"                          return TOKEN(T_OP_GETBUF);
282 "getpos"                          return TOKEN(T_OP_GETPOS);
283 "getinfo"                         return TOKEN(T_OP_GETINFO);
284 "dsx"                             return TOKEN(T_OP_DSX);
285 "dsy"                             return TOKEN(T_OP_DSY);
286 "gather4r"                        return TOKEN(T_OP_GATHER4R);
287 "gather4g"                        return TOKEN(T_OP_GATHER4G);
288 "gather4b"                        return TOKEN(T_OP_GATHER4B);
289 "gather4a"                        return TOKEN(T_OP_GATHER4A);
290 "samgp0"                          return TOKEN(T_OP_SAMGP0);
291 "samgp1"                          return TOKEN(T_OP_SAMGP1);
292 "samgp2"                          return TOKEN(T_OP_SAMGP2);
293 "samgp3"                          return TOKEN(T_OP_SAMGP3);
294 "dsxpp.1"                         return TOKEN(T_OP_DSXPP_1);
295 "dsypp.1"                         return TOKEN(T_OP_DSYPP_1);
296 "rgetpos"                         return TOKEN(T_OP_RGETPOS);
297 "rgetinfo"                        return TOKEN(T_OP_RGETINFO);
298 
299                                   /* category 6: */
300 "ldg"                             return TOKEN(T_OP_LDG);
301 "ldg.a"                           return TOKEN(T_OP_LDG_A);
302 "ldl"                             return TOKEN(T_OP_LDL);
303 "ldp"                             return TOKEN(T_OP_LDP);
304 "stg"                             return TOKEN(T_OP_STG);
305 "stg.a"                           return TOKEN(T_OP_STG_A);
306 "stl"                             return TOKEN(T_OP_STL);
307 "stp"                             return TOKEN(T_OP_STP);
308 "ldib"                            return TOKEN(T_OP_LDIB);
309 "g2l"                             return TOKEN(T_OP_G2L);
310 "l2g"                             return TOKEN(T_OP_L2G);
311 "prefetch"                        return TOKEN(T_OP_PREFETCH);
312 "ldlw"                            return TOKEN(T_OP_LDLW);
313 "stlw"                            return TOKEN(T_OP_STLW);
314 "resfmt"                          return TOKEN(T_OP_RESFMT);
315 "resinfo"                         return TOKEN(T_OP_RESINFO);
316 "atomic.add"                      return TOKEN(T_OP_ATOMIC_ADD);
317 "atomic.sub"                      return TOKEN(T_OP_ATOMIC_SUB);
318 "atomic.xchg"                     return TOKEN(T_OP_ATOMIC_XCHG);
319 "atomic.inc"                      return TOKEN(T_OP_ATOMIC_INC);
320 "atomic.dec"                      return TOKEN(T_OP_ATOMIC_DEC);
321 "atomic.cmpxchg"                  return TOKEN(T_OP_ATOMIC_CMPXCHG);
322 "atomic.min"                      return TOKEN(T_OP_ATOMIC_MIN);
323 "atomic.max"                      return TOKEN(T_OP_ATOMIC_MAX);
324 "atomic.and"                      return TOKEN(T_OP_ATOMIC_AND);
325 "atomic.or"                       return TOKEN(T_OP_ATOMIC_OR);
326 "atomic.xor"                      return TOKEN(T_OP_ATOMIC_XOR);
327 "resinfo.b"                       return TOKEN(T_OP_RESINFO_B);
328 "ldib.b"                          return TOKEN(T_OP_LDIB_B);
329 "stib.b"                          return TOKEN(T_OP_STIB_B);
330 "atomic.b.add"                    return TOKEN(T_OP_ATOMIC_B_ADD);
331 "atomic.b.sub"                    return TOKEN(T_OP_ATOMIC_B_SUB);
332 "atomic.b.xchg"                   return TOKEN(T_OP_ATOMIC_B_XCHG);
333 "atomic.b.inc"                    return TOKEN(T_OP_ATOMIC_B_INC);
334 "atomic.b.dec"                    return TOKEN(T_OP_ATOMIC_B_DEC);
335 "atomic.b.cmpxchg"                return TOKEN(T_OP_ATOMIC_B_CMPXCHG);
336 "atomic.b.min"                    return TOKEN(T_OP_ATOMIC_B_MIN);
337 "atomic.b.max"                    return TOKEN(T_OP_ATOMIC_B_MAX);
338 "atomic.b.and"                    return TOKEN(T_OP_ATOMIC_B_AND);
339 "atomic.b.or"                     return TOKEN(T_OP_ATOMIC_B_OR);
340 "atomic.b.xor"                    return TOKEN(T_OP_ATOMIC_B_XOR);
341 "ldgb"                            return TOKEN(T_OP_LDGB);
342 "stgb"                            return TOKEN(T_OP_STGB);
343 "stib"                            return TOKEN(T_OP_STIB);
344 "ldc"                             return TOKEN(T_OP_LDC);
345 "ldlv"                            return TOKEN(T_OP_LDLV);
346 "getspid"                         return TOKEN(T_OP_GETSPID);
347 "getwid"                          return TOKEN(T_OP_GETWID);
348 
349                                   /* category 7: */
350 "bar"                             return TOKEN(T_OP_BAR);
351 "fence"                           return TOKEN(T_OP_FENCE);
352 
353 "f16"                             return TOKEN(T_TYPE_F16);
354 "f32"                             return TOKEN(T_TYPE_F32);
355 "u16"                             return TOKEN(T_TYPE_U16);
356 "u32"                             return TOKEN(T_TYPE_U32);
357 "s16"                             return TOKEN(T_TYPE_S16);
358 "s32"                             return TOKEN(T_TYPE_S32);
359 "u8"                              return TOKEN(T_TYPE_U8);
360 "s8"                              return TOKEN(T_TYPE_S8);
361 
362 "untyped"                         return TOKEN(T_UNTYPED);
363 "typed"                           return TOKEN(T_TYPED);
364 
365 "1d"                              return TOKEN(T_1D);
366 "2d"                              return TOKEN(T_2D);
367 "3d"                              return TOKEN(T_3D);
368 "4d"                              return TOKEN(T_4D);
369 
370 "lt"                              return TOKEN(T_LT);
371 "le"                              return TOKEN(T_LE);
372 "gt"                              return TOKEN(T_GT);
373 "ge"                              return TOKEN(T_GE);
374 "eq"                              return TOKEN(T_EQ);
375 "ne"                              return TOKEN(T_NE);
376 
377 "a"                               return 'a';
378 "o"                               return 'o';
379 "p"                               return 'p';
380 "s2en"                            return TOKEN(T_S2EN);
381 "s"                               return 's';
382 "base"[0-9]+                      ir3_yylval.num = strtol(yytext+4, NULL, 10); return T_BASE;
383 "offset"[0-9]+                    ir3_yylval.num = strtol(yytext+6, NULL, 10); return T_OFFSET;
384 "uniform"                         return T_UNIFORM;
385 "nonuniform"                      return T_NONUNIFORM;
386 "imm"                             return T_IMM;
387 
388 "h"                               return 'h';
389 "="                               return '=';
390 "("                               return '(';
391 ")"                               return ')';
392 "["                               return '[';
393 "]"                               return ']';
394 ","                               return ',';
395 "."                               return '.';
396 "-"                               return '-';
397 "+"                               return '+';
398 "|"                               return '|';
399 "c"                               return 'c';
400 "r"                               return 'r';
401 "hc"                              return TOKEN(T_HC);
402 "hr"                              return TOKEN(T_HR);
403 "g"                               return 'g';
404 "w"                               return 'w';
405 "l"                               return 'l';
406 "<"                               return '<';
407 ">"                               return '>';
408 "!"                               return '!';
409 "#"                               return '#';
410 ":"                               return ':';
411 
412 "nan"                             return TOKEN(T_NAN);
413 "inf"                             return TOKEN(T_INF);
414 
415 [a-zA-Z_][a-zA-Z_0-9]*            ir3_yylval.str = ralloc_strdup(ir3_parser_dead_ctx, yytext); return T_IDENTIFIER;
416 .                                 fprintf(stderr, "error at line %d: Unknown token: %s\n", ir3_yyget_lineno(), yytext); yyterminate();
417 %%
418