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 "ir3/ir3.h"
27 #include "ir3_parser.h"
28 
29 #define YY_NO_INPUT
30 #define YY_NO_UNPUT
31 #define TOKEN(t) (ir3_yylval.tok = t)
32 extern YYSTYPE ir3_yylval;
33 
parse_wrmask(const char * src)34 static int parse_wrmask(const char *src)
35 {
36 	int i, num = 0;
37 	for (i = 0; i < 4; i++) {
38 		if ("xyzw"[i] == src[1]) {
39 			num |= (1 << i);
40 			src++;
41 		}
42 	}
43 	return num;
44 }
45 
parse_reg(const char * str)46 static int parse_reg(const char *str)
47 {
48 	int num = 0;
49 	if (str[0] == 'h') {
50 		str++;
51 		num++;
52 	}
53 	str++;
54 	num += strtol(str, (char **)&str, 10) << 3;
55 	switch (str[1]) {
56 	case 'x': num += 0; break;
57 	case 'y': num += 2; break;
58 	case 'z': num += 4; break;
59 	case 'w': num += 6; break;
60 	default: assert(0); break;
61 	}
62 	return num;
63 }
64 %}
65 
66 %option noyywrap
67 %option prefix="ir3_yy"
68 
69 %%
70 "\n"                              yylineno++;
71 [ \t]                             ; /* ignore whitespace */
72 ";"[^\n]*"\n"                     yylineno++; /* ignore comments */
73 [0-9]+"."[0-9]+                   ir3_yylval.flt = strtod(yytext, NULL);       return T_FLOAT;
74 [0-9]*                            ir3_yylval.num = strtoul(yytext, NULL, 0);    return T_INT;
75 "0x"[0-9a-fA-F]*                  ir3_yylval.num = strtoul(yytext, NULL, 0);    return T_HEX;
76 "@localsize"                      return TOKEN(T_A_LOCALSIZE);
77 "@const"                          return TOKEN(T_A_CONST);
78 "@buf"                            return TOKEN(T_A_BUF);
79 "@invocationid"                   return TOKEN(T_A_INVOCATIONID);
80 "@wgid"                           return TOKEN(T_A_WGID);
81 "@numwg"                          return TOKEN(T_A_NUMWG);
82 "@in"                             return TOKEN(T_A_IN);
83 "@out"                            return TOKEN(T_A_OUT);
84 "@tex"                            return TOKEN(T_A_TEX);
85 "(sy)"                            return TOKEN(T_SY);
86 "(ss)"                            return TOKEN(T_SS);
87 "(absneg)"                        return TOKEN(T_ABSNEG);
88 "(neg)"                           return TOKEN(T_NEG);
89 "(abs)"                           return TOKEN(T_ABS);
90 "(r)"                             return TOKEN(T_R);
91 "(ul)"                            return TOKEN(T_UL);
92 "(even)"                          return TOKEN(T_EVEN);
93 "(pos_infinity)"                  return TOKEN(T_POS_INFINITY);
94 "(ei)"                            return TOKEN(T_EI);
95 "(jp)"                            return TOKEN(T_JP);
96 "(rpt"[0-7]")"                    ir3_yylval.num = strtol(yytext+4, NULL, 10); return T_RPT;
97 "(nop"[0-7]")"                    ir3_yylval.num = strtol(yytext+4, NULL, 10); return T_NOP;
98 "("[x]?[y]?[z]?[w]?")"            ir3_yylval.num = parse_wrmask(yytext); return T_WRMASK;
99 
100 [h]?"r"[0-9]+"."[xyzw]            ir3_yylval.num = parse_reg(yytext); return T_REGISTER;
101 [h]?"c"[0-9]+"."[xyzw]            ir3_yylval.num = parse_reg(yytext); return T_CONSTANT;
102 "a0."[xyzw]                       ir3_yylval.num = parse_reg(yytext); return T_A0;
103 "p0."[xyzw]                       ir3_yylval.num = parse_reg(yytext); return T_P0;
104 "s#"[0-9]+                        ir3_yylval.num = strtol(yytext+2, NULL, 10); return T_SAMP;
105 "t#"[0-9]+                        ir3_yylval.num = strtol(yytext+2, NULL, 10); return T_TEX;
106 
107                                   /* category 0: */
108 "nop"                             return TOKEN(T_OP_NOP);
109 "br"                              return TOKEN(T_OP_BR);
110 "jump"                            return TOKEN(T_OP_JUMP);
111 "call"                            return TOKEN(T_OP_CALL);
112 "ret"                             return TOKEN(T_OP_RET);
113 "kill"                            return TOKEN(T_OP_KILL);
114 "end"                             return TOKEN(T_OP_END);
115 "emit"                            return TOKEN(T_OP_EMIT);
116 "cut"                             return TOKEN(T_OP_CUT);
117 "chmask"                          return TOKEN(T_OP_CHMASK);
118 "chsh"                            return TOKEN(T_OP_CHSH);
119 "flow_rev"                        return TOKEN(T_OP_FLOW_REV);
120 
121                                   /* category 1: */
122 "mova"                            return TOKEN(T_OP_MOVA);
123 "mov"                             return TOKEN(T_OP_MOV);
124 "cov"                             return TOKEN(T_OP_COV);
125 
126 ("f16"|"f32"|"u16"|"u32"|"s16"|"s32"|"u8"|"s8"){2} ir3_yylval.str = yytext; return T_CAT1_TYPE_TYPE;
127 
128                                   /* category 2: */
129 "add.f"                           return TOKEN(T_OP_ADD_F);
130 "min.f"                           return TOKEN(T_OP_MIN_F);
131 "max.f"                           return TOKEN(T_OP_MAX_F);
132 "mul.f"                           return TOKEN(T_OP_MUL_F);
133 "sign.f"                          return TOKEN(T_OP_SIGN_F);
134 "cmps.f"                          return TOKEN(T_OP_CMPS_F);
135 "absneg.f"                        return TOKEN(T_OP_ABSNEG_F);
136 "cmpv.f"                          return TOKEN(T_OP_CMPV_F);
137 "floor.f"                         return TOKEN(T_OP_FLOOR_F);
138 "ceil.f"                          return TOKEN(T_OP_CEIL_F);
139 "rndne.f"                         return TOKEN(T_OP_RNDNE_F);
140 "rndaz.f"                         return TOKEN(T_OP_RNDAZ_F);
141 "trunc.f"                         return TOKEN(T_OP_TRUNC_F);
142 "add.u"                           return TOKEN(T_OP_ADD_U);
143 "add.s"                           return TOKEN(T_OP_ADD_S);
144 "sub.u"                           return TOKEN(T_OP_SUB_U);
145 "sub.s"                           return TOKEN(T_OP_SUB_S);
146 "cmps.u"                          return TOKEN(T_OP_CMPS_U);
147 "cmps.s"                          return TOKEN(T_OP_CMPS_S);
148 "min.u"                           return TOKEN(T_OP_MIN_U);
149 "min.s"                           return TOKEN(T_OP_MIN_S);
150 "max.u"                           return TOKEN(T_OP_MAX_U);
151 "max.s"                           return TOKEN(T_OP_MAX_S);
152 "absneg.s"                        return TOKEN(T_OP_ABSNEG_S);
153 "and.b"                           return TOKEN(T_OP_AND_B);
154 "or.b"                            return TOKEN(T_OP_OR_B);
155 "not.b"                           return TOKEN(T_OP_NOT_B);
156 "xor.b"                           return TOKEN(T_OP_XOR_B);
157 "cmpv.u"                          return TOKEN(T_OP_CMPV_U);
158 "cmpv.s"                          return TOKEN(T_OP_CMPV_S);
159 "mul.u24"                         return TOKEN(T_OP_MUL_U24);
160 "mul.s24"                         return TOKEN(T_OP_MUL_S24);
161 "mull.u"                          return TOKEN(T_OP_MULL_U);
162 "bfrev.b"                         return TOKEN(T_OP_BFREV_B);
163 "clz.s"                           return TOKEN(T_OP_CLZ_S);
164 "clz.b"                           return TOKEN(T_OP_CLZ_B);
165 "shl.b"                           return TOKEN(T_OP_SHL_B);
166 "shr.b"                           return TOKEN(T_OP_SHR_B);
167 "ashr.b"                          return TOKEN(T_OP_ASHR_B);
168 "bary.f"                          return TOKEN(T_OP_BARY_F);
169 "mgen.b"                          return TOKEN(T_OP_MGEN_B);
170 "getbit.b"                        return TOKEN(T_OP_GETBIT_B);
171 "setrm"                           return TOKEN(T_OP_SETRM);
172 "cbits.b"                         return TOKEN(T_OP_CBITS_B);
173 "shb"                             return TOKEN(T_OP_SHB);
174 "msad"                            return TOKEN(T_OP_MSAD);
175 
176                                   /* category 3: */
177 "mad.u16"                         return TOKEN(T_OP_MAD_U16);
178 "madsh.u16"                       return TOKEN(T_OP_MADSH_U16);
179 "mad.s16"                         return TOKEN(T_OP_MAD_S16);
180 "madsh.m16"                       return TOKEN(T_OP_MADSH_M16);
181 "mad.u24"                         return TOKEN(T_OP_MAD_U24);
182 "mad.s24"                         return TOKEN(T_OP_MAD_S24);
183 "mad.f16"                         return TOKEN(T_OP_MAD_F16);
184 "mad.f32"                         return TOKEN(T_OP_MAD_F32);
185 "sel.b16"                         return TOKEN(T_OP_SEL_B16);
186 "sel.b32"                         return TOKEN(T_OP_SEL_B32);
187 "sel.s16"                         return TOKEN(T_OP_SEL_S16);
188 "sel.s32"                         return TOKEN(T_OP_SEL_S32);
189 "sel.f16"                         return TOKEN(T_OP_SEL_F16);
190 "sel.f32"                         return TOKEN(T_OP_SEL_F32);
191 "sad.s16"                         return TOKEN(T_OP_SAD_S16);
192 "sad.s32"                         return TOKEN(T_OP_SAD_S32);
193 
194                                   /* category 4: */
195 "rcp"                             return TOKEN(T_OP_RCP);
196 "rsq"                             return TOKEN(T_OP_RSQ);
197 "log2"                            return TOKEN(T_OP_LOG2);
198 "exp2"                            return TOKEN(T_OP_EXP2);
199 "sin"                             return TOKEN(T_OP_SIN);
200 "cos"                             return TOKEN(T_OP_COS);
201 "sqrt"                            return TOKEN(T_OP_SQRT);
202 "hrsq"                            return TOKEN(T_OP_HRSQ);
203 "hlog2"                           return TOKEN(T_OP_HLOG2);
204 "hexp2"                           return TOKEN(T_OP_HEXP2);
205 
206                                   /* category 5: */
207 "isam"                            return TOKEN(T_OP_ISAM);
208 "isaml"                           return TOKEN(T_OP_ISAML);
209 "isamm"                           return TOKEN(T_OP_ISAMM);
210 "sam"                             return TOKEN(T_OP_SAM);
211 "samb"                            return TOKEN(T_OP_SAMB);
212 "saml"                            return TOKEN(T_OP_SAML);
213 "samgq"                           return TOKEN(T_OP_SAMGQ);
214 "getlod"                          return TOKEN(T_OP_GETLOD);
215 "conv"                            return TOKEN(T_OP_CONV);
216 "convm"                           return TOKEN(T_OP_CONVM);
217 "getsize"                         return TOKEN(T_OP_GETSIZE);
218 "getbuf"                          return TOKEN(T_OP_GETBUF);
219 "getpos"                          return TOKEN(T_OP_GETPOS);
220 "getinfo"                         return TOKEN(T_OP_GETINFO);
221 "dsx"                             return TOKEN(T_OP_DSX);
222 "dsy"                             return TOKEN(T_OP_DSY);
223 "gather4r"                        return TOKEN(T_OP_GATHER4R);
224 "gather4g"                        return TOKEN(T_OP_GATHER4G);
225 "gather4b"                        return TOKEN(T_OP_GATHER4B);
226 "gather4a"                        return TOKEN(T_OP_GATHER4A);
227 "samgp0"                          return TOKEN(T_OP_SAMGP0);
228 "samgp1"                          return TOKEN(T_OP_SAMGP1);
229 "samgp2"                          return TOKEN(T_OP_SAMGP2);
230 "samgp3"                          return TOKEN(T_OP_SAMGP3);
231 "dsxpp.1"                         return TOKEN(T_OP_DSXPP_1);
232 "dsypp.1"                         return TOKEN(T_OP_DSYPP_1);
233 "rgetpos"                         return TOKEN(T_OP_RGETPOS);
234 "rgetinfo"                        return TOKEN(T_OP_RGETINFO);
235 
236                                   /* category 6: */
237 "ldg"                             return TOKEN(T_OP_LDG);
238 "ldl"                             return TOKEN(T_OP_LDL);
239 "ldp"                             return TOKEN(T_OP_LDP);
240 "stg"                             return TOKEN(T_OP_STG);
241 "stl"                             return TOKEN(T_OP_STL);
242 "stp"                             return TOKEN(T_OP_STP);
243 "ldib"                            return TOKEN(T_OP_LDIB);
244 "g2l"                             return TOKEN(T_OP_G2L);
245 "l2g"                             return TOKEN(T_OP_L2G);
246 "prefetch"                        return TOKEN(T_OP_PREFETCH);
247 "ldlw"                            return TOKEN(T_OP_LDLW);
248 "stlw"                            return TOKEN(T_OP_STLW);
249 "resfmt"                          return TOKEN(T_OP_RESFMT);
250 "resinf"                          return TOKEN(T_OP_RESINF);
251 "atomic.add"                      return TOKEN(T_OP_ATOMIC_ADD);
252 "atomic.sub"                      return TOKEN(T_OP_ATOMIC_SUB);
253 "atomic.xchg"                     return TOKEN(T_OP_ATOMIC_XCHG);
254 "atomic.inc"                      return TOKEN(T_OP_ATOMIC_INC);
255 "atomic.dec"                      return TOKEN(T_OP_ATOMIC_DEC);
256 "atomic.cmpxchg"                  return TOKEN(T_OP_ATOMIC_CMPXCHG);
257 "atomic.min"                      return TOKEN(T_OP_ATOMIC_MIN);
258 "atomic.max"                      return TOKEN(T_OP_ATOMIC_MAX);
259 "atomic.and"                      return TOKEN(T_OP_ATOMIC_AND);
260 "atomic.or"                       return TOKEN(T_OP_ATOMIC_OR);
261 "atomic.xor"                      return TOKEN(T_OP_ATOMIC_XOR);
262 "ldgb"                            return TOKEN(T_OP_LDGB);
263 "stgb"                            return TOKEN(T_OP_STGB);
264 "stib"                            return TOKEN(T_OP_STIB);
265 "ldc"                             return TOKEN(T_OP_LDC);
266 "ldlv"                            return TOKEN(T_OP_LDLV);
267 
268 "f16"                             return TOKEN(T_TYPE_F16);
269 "f32"                             return TOKEN(T_TYPE_F32);
270 "u16"                             return TOKEN(T_TYPE_U16);
271 "u32"                             return TOKEN(T_TYPE_U32);
272 "s16"                             return TOKEN(T_TYPE_S16);
273 "s32"                             return TOKEN(T_TYPE_S32);
274 "u8"                              return TOKEN(T_TYPE_U8);
275 "s8"                              return TOKEN(T_TYPE_S8);
276 
277 "untyped"                         return TOKEN(T_UNTYPED);
278 "typed"                           return TOKEN(T_TYPED);
279 
280 "1d"                              return TOKEN(T_1D);
281 "2d"                              return TOKEN(T_2D);
282 "3d"                              return TOKEN(T_3D);
283 "4d"                              return TOKEN(T_4D);
284 
285 "lt"                              return TOKEN(T_LT);
286 "le"                              return TOKEN(T_LE);
287 "gt"                              return TOKEN(T_GT);
288 "ge"                              return TOKEN(T_GE);
289 "eq"                              return TOKEN(T_EQ);
290 "ne"                              return TOKEN(T_NE);
291 
292 "a"                               return 'a';
293 "o"                               return 'o';
294 "p"                               return 'p';
295 "s2en"                            return TOKEN(T_S2EN);
296 "s"                               return 's';
297 "base"[0-9]+                      ir3_yylval.num = strtol(yytext+4, NULL, 10); return T_BASE;
298 
299 "="                               return '=';
300 "("                               return '(';
301 ")"                               return ')';
302 "["                               return '[';
303 "]"                               return ']';
304 ","                               return ',';
305 "."                               return '.';
306 "-"                               return '-';
307 "+"                               return '+';
308 "|"                               return '|';
309 "c"                               return 'c';
310 "r"                               return 'r';
311 "hc"                              return TOKEN(T_HC);
312 "hr"                              return TOKEN(T_HR);
313 "g"                               return 'g';
314 "l"                               return 'l';
315 "<"                               return '<';
316 ">"                               return '>';
317 "!"                               return '!';
318 "#"                               return '#';
319 
320 "nan"                             return TOKEN(T_NAN);
321 "inf"                             return TOKEN(T_INF);
322 
323 [a-zA-Z_][a-zA-Z_0-9]*            ir3_yylval.str = yytext;     return T_IDENTIFIER;
324 .                                 fprintf(stderr, "error at line %d: Unknown token: %s\n", ir3_yyget_lineno(), yytext); yyterminate();
325 %%
326