9f82286692c1596d741137cf36c02891228d8fff
[mesa.git] / src / freedreno / afuc / parser.y
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 #define YYDEBUG 0
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <math.h>
31 #include "asm.h"
32
33
34 int yyget_lineno(void);
35
36 #ifdef YYDEBUG
37 int yydebug;
38 #endif
39
40 extern int yylex(void);
41 typedef void *YY_BUFFER_STATE;
42 extern YY_BUFFER_STATE yy_scan_string(const char *);
43 extern void yy_delete_buffer(YY_BUFFER_STATE);
44
45 int yyparse(void);
46
47 void yyerror(const char *error);
48 void yyerror(const char *error)
49 {
50 fprintf(stderr, "error at line %d: %s\n", yyget_lineno(), error);
51 }
52
53 static struct asm_instruction *instr; /* current instruction */
54
55 static void
56 new_instr(int tok)
57 {
58 instr = next_instr(tok);
59 }
60
61 static void
62 dst(int num)
63 {
64 instr->dst = num;
65 }
66
67 static void
68 src1(int num)
69 {
70 instr->src1 = num;
71 }
72
73 static void
74 src2(int num)
75 {
76 instr->src2 = num;
77 }
78
79 static void
80 immed(int num)
81 {
82 instr->immed = num;
83 instr->has_immed = true;
84 }
85
86 static void
87 shift(int num)
88 {
89 instr->shift = num;
90 instr->has_shift = true;
91 }
92
93 static void
94 bit(int num)
95 {
96 instr->bit = num;
97 instr->has_bit = true;
98 }
99
100 static void
101 literal(uint32_t num)
102 {
103 instr->literal = num;
104 instr->is_literal = true;
105 }
106
107 static void
108 label(const char *str)
109 {
110 instr->label = str;
111 }
112
113 %}
114
115 %union {
116 int tok;
117 uint32_t num;
118 const char *str;
119 }
120
121 %{
122 static void print_token(FILE *file, int type, YYSTYPE value)
123 {
124 fprintf(file, "\ntype: %d\n", type);
125 }
126
127 #define YYPRINT(file, type, value) print_token(file, type, value)
128 %}
129
130 %token <num> T_INT
131 %token <num> T_HEX
132 %token <num> T_CONTROL_REG
133 %token <str> T_LABEL_DECL
134 %token <str> T_LABEL_REF
135 %token <num> T_LITERAL
136 %token <num> T_BIT
137 %token <num> T_REGISTER
138
139 %token <tok> T_OP_NOP
140 %token <tok> T_OP_ADD
141 %token <tok> T_OP_ADDHI
142 %token <tok> T_OP_SUB
143 %token <tok> T_OP_SUBHI
144 %token <tok> T_OP_AND
145 %token <tok> T_OP_OR
146 %token <tok> T_OP_XOR
147 %token <tok> T_OP_NOT
148 %token <tok> T_OP_SHL
149 %token <tok> T_OP_USHR
150 %token <tok> T_OP_ISHR
151 %token <tok> T_OP_ROT
152 %token <tok> T_OP_MUL8
153 %token <tok> T_OP_MIN
154 %token <tok> T_OP_MAX
155 %token <tok> T_OP_CMP
156 %token <tok> T_OP_MSB
157 %token <tok> T_OP_MOV
158 %token <tok> T_OP_CWRITE
159 %token <tok> T_OP_CREAD
160 %token <tok> T_OP_STORE
161 %token <tok> T_OP_LOAD
162 %token <tok> T_OP_BRNE
163 %token <tok> T_OP_BREQ
164 %token <tok> T_OP_RET
165 %token <tok> T_OP_CALL
166 %token <tok> T_OP_JUMP
167 %token <tok> T_OP_WAITIN
168 %token <tok> T_OP_PREEMPTLEAVE
169 %token <tok> T_LSHIFT
170 %token <tok> T_REP
171
172 %type <num> reg
173 %type <num> immediate
174
175 %error-verbose
176
177 %start instrs
178
179 %%
180
181 instrs: instr_or_label instrs
182 | instr_or_label
183
184 instr_or_label: instr_r
185 | T_REP instr_r { instr->rep = true; }
186 | branch_instr
187 | other_instr
188 | T_LABEL_DECL { decl_label($1); }
189
190 /* instructions that can optionally have (rep) flag: */
191 instr_r: alu_instr
192 | config_instr
193
194 /* need to special case:
195 * - not (single src, possibly an immediate)
196 * - msb (single src, must be reg)
197 * - mov (single src, plus possibly a shift)
198 * from the other ALU instructions:
199 */
200
201 alu_msb_instr: T_OP_MSB reg ',' reg { new_instr($1); dst($2); src2($4); }
202
203 alu_not_instr: T_OP_NOT reg ',' reg { new_instr($1); dst($2); src2($4); }
204 | T_OP_NOT reg ',' immediate { new_instr($1); dst($2); immed($4); }
205
206 alu_mov_instr: T_OP_MOV reg ',' reg { new_instr($1); dst($2); src1($4); }
207 | T_OP_MOV reg ',' immediate T_LSHIFT immediate {
208 new_instr($1); dst($2); immed($4); shift($6);
209 }
210 | T_OP_MOV reg ',' immediate { new_instr($1); dst($2); immed($4); }
211 | T_OP_MOV reg ',' T_LABEL_REF T_LSHIFT immediate {
212 new_instr($1); dst($2); label($4); shift($6);
213 }
214 | T_OP_MOV reg ',' T_LABEL_REF { new_instr($1); dst($2); label($4); }
215
216 alu_2src_op: T_OP_ADD { new_instr($1); }
217 | T_OP_ADDHI { new_instr($1); }
218 | T_OP_SUB { new_instr($1); }
219 | T_OP_SUBHI { new_instr($1); }
220 | T_OP_AND { new_instr($1); }
221 | T_OP_OR { new_instr($1); }
222 | T_OP_XOR { new_instr($1); }
223 | T_OP_SHL { new_instr($1); }
224 | T_OP_USHR { new_instr($1); }
225 | T_OP_ISHR { new_instr($1); }
226 | T_OP_ROT { new_instr($1); }
227 | T_OP_MUL8 { new_instr($1); }
228 | T_OP_MIN { new_instr($1); }
229 | T_OP_MAX { new_instr($1); }
230 | T_OP_CMP { new_instr($1); }
231
232 alu_2src_instr: alu_2src_op reg ',' reg ',' reg { dst($2); src1($4); src2($6); }
233 | alu_2src_op reg ',' reg ',' immediate { dst($2); src1($4); immed($6); }
234
235 alu_instr: alu_2src_instr
236 | alu_msb_instr
237 | alu_not_instr
238 | alu_mov_instr
239
240 config_op: T_OP_CWRITE { new_instr($1); }
241 | T_OP_CREAD { new_instr($1); }
242 | T_OP_LOAD { new_instr($1); }
243 | T_OP_STORE { new_instr($1); }
244
245 config_instr: config_op reg ',' '[' reg '+' immediate ']' ',' immediate {
246 src1($2); src2($5); immed($7); bit($10);
247 }
248
249 branch_op: T_OP_BRNE { new_instr($1); }
250 | T_OP_BREQ { new_instr($1); }
251
252 branch_instr: branch_op reg ',' T_BIT ',' T_LABEL_REF { src1($2); bit($4); label($6); }
253 | branch_op reg ',' immediate ',' T_LABEL_REF { src1($2); immed($4); label($6); }
254
255 other_instr: T_OP_CALL T_LABEL_REF { new_instr($1); label($2); }
256 | T_OP_PREEMPTLEAVE T_LABEL_REF { new_instr($1); label($2); }
257 | T_OP_RET { new_instr($1); }
258 | T_OP_JUMP T_LABEL_REF { new_instr($1); label($2); }
259 | T_OP_WAITIN { new_instr($1); }
260 | T_OP_NOP { new_instr($1); }
261 | T_LITERAL { new_instr($1); literal($1); }
262
263 reg: T_REGISTER
264
265 immediate: T_HEX
266 | T_INT
267 | T_CONTROL_REG
268 | T_CONTROL_REG '+' immediate { $$ = $1 + $3; }
269