1 /*
2 * Copyright (c) 2013 Rob Clark <[email protected]>
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);
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 afuc_instr *instr; /* current instruction */
54
55 static void
new_instr(afuc_opc opc)56 new_instr(afuc_opc opc)
57 {
58 instr = next_instr(opc);
59 }
60
61 static void
dst(int num)62 dst(int num)
63 {
64 instr->dst = num;
65 }
66
67 static void
src1(int num)68 src1(int num)
69 {
70 instr->src1 = num;
71 }
72
73 static void
src2(int num)74 src2(int num)
75 {
76 instr->src2 = num;
77 }
78
79 static void
immed(int num)80 immed(int num)
81 {
82 instr->immed = num;
83 instr->has_immed = true;
84 }
85
86 static void
shift(int num)87 shift(int num)
88 {
89 instr->shift = num;
90 instr->has_shift = true;
91 }
92
93 static void
bit(int num)94 bit(int num)
95 {
96 instr->bit = num;
97 instr->has_bit = true;
98 }
99
100 static void
literal(uint32_t num)101 literal(uint32_t num)
102 {
103 instr->literal = num;
104 instr->is_literal = true;
105 parse_version(instr);
106 }
107
108 static void
label(const char * str)109 label(const char *str)
110 {
111 instr->label = str;
112 }
113
114 %}
115
116 %union {
117 int tok;
118 uint32_t num;
119 const char *str;
120 }
121
122 %token <num> T_INT
123 %token <num> T_HEX
124 %token <num> T_CONTROL_REG
125 %token <num> T_SQE_REG
126 %token <str> T_LABEL_REF
127 %token <num> T_LITERAL
128 %token <num> T_BIT
129 %token <num> T_REGISTER
130 %token <str> T_IDENTIFIER
131
132 %token <tok> T_OP_NOP
133 %token <tok> T_OP_ADD
134 %token <tok> T_OP_ADDHI
135 %token <tok> T_OP_SUB
136 %token <tok> T_OP_SUBHI
137 %token <tok> T_OP_AND
138 %token <tok> T_OP_OR
139 %token <tok> T_OP_XOR
140 %token <tok> T_OP_NOT
141 %token <tok> T_OP_SHL
142 %token <tok> T_OP_USHR
143 %token <tok> T_OP_ISHR
144 %token <tok> T_OP_ROT
145 %token <tok> T_OP_MUL8
146 %token <tok> T_OP_MIN
147 %token <tok> T_OP_MAX
148 %token <tok> T_OP_CMP
149 %token <tok> T_OP_BIC
150 %token <tok> T_OP_MSB
151 %token <tok> T_OP_SETBIT
152 %token <tok> T_OP_CLRBIT
153 %token <tok> T_OP_BFI
154 %token <tok> T_OP_UBFX
155 %token <tok> T_OP_MOV
156 %token <tok> T_OP_CWRITE
157 %token <tok> T_OP_CREAD
158 %token <tok> T_OP_SWRITE
159 %token <tok> T_OP_SREAD
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_IRET
166 %token <tok> T_OP_CALL
167 %token <tok> T_OP_JUMP
168 %token <tok> T_OP_JUMPA
169 %token <tok> T_OP_SRET
170 %token <tok> T_OP_WAITIN
171 %token <tok> T_OP_BL
172 %token <tok> T_OP_SETSECURE
173 %token <tok> T_LSHIFT
174 %token <tok> T_REP
175 %token <tok> T_PEEK
176 %token <num> T_XMOV
177 %token <num> T_SDS
178
179 %token <tok> T_ALIGN
180 %token <tok> T_JUMPTBL
181 %token <tok> T_SECTION
182
183 %type <num> reg
184 %type <num> immediate
185 %type <num> xmov
186 %type <num> peek
187
188 %error-verbose
189
190 %start instrs
191
192 %%
193
194 instrs: instrs instr_or_label
195 | instr_or_label
196
197 instr_or_label: instr_r
198 | T_REP instr_r { instr->rep = true; }
199 | branch_instr
200 | other_instr
201 | T_IDENTIFIER ':' { decl_label($1); }
202 | T_ALIGN immediate { align_instr($2); }
203 | T_JUMPTBL { decl_jumptbl(); }
204 | T_SECTION T_IDENTIFIER { next_section(); }
205
206 xmov: T_XMOV { $$ = $1; }
207 | { $$ = 0; }
208
209 peek: T_PEEK { $$ = 1; }
210 | { $$ = 0; }
211
212 /* instructions that can optionally have (rep) flag: */
213 instr_r: xmov peek alu_instr { instr->xmov = $1; instr->peek = $2; }
214 | load_instr
215 | store_instr
216
217 /* need to special case:
218 * - not (single src, possibly an immediate)
219 * - msb (single src, must be reg)
220 * - mov (single src, plus possibly a shift)
221 * from the other ALU instructions:
222 */
223
224 alu_msb_instr: T_OP_MSB reg ',' reg { new_instr(OPC_MSB); dst($2); src1($4); }
225
226 alu_not_instr: T_OP_NOT reg ',' reg { new_instr(OPC_NOT); dst($2); src1($4); }
227 | T_OP_NOT reg ',' immediate { new_instr(OPC_NOT); dst($2); immed($4); }
228
229 alu_mov_instr: T_OP_MOV reg ',' reg { new_instr(OPC_OR); dst($2); src1(0); src2($4); }
230 | T_OP_MOV reg ',' immediate T_LSHIFT immediate {
231 new_instr(OPC_MOVI); dst($2); immed($4); shift($6);
232 }
233 | T_OP_MOV reg ',' immediate { new_instr(OPC_MOVI); dst($2); immed($4); shift(0); }
234 | T_OP_MOV reg ',' T_LABEL_REF T_LSHIFT immediate {
235 new_instr(OPC_MOVI); dst($2); label($4); shift($6);
236 }
237 | T_OP_MOV reg ',' T_LABEL_REF { new_instr(OPC_MOVI); dst($2); label($4); shift(0); }
238
239 alu_2src_op: T_OP_ADD { new_instr(OPC_ADD); }
240 | T_OP_ADDHI { new_instr(OPC_ADDHI); }
241 | T_OP_SUB { new_instr(OPC_SUB); }
242 | T_OP_SUBHI { new_instr(OPC_SUBHI); }
243 | T_OP_AND { new_instr(OPC_AND); }
244 | T_OP_OR { new_instr(OPC_OR); }
245 | T_OP_XOR { new_instr(OPC_XOR); }
246 | T_OP_SHL { new_instr(OPC_SHL); }
247 | T_OP_USHR { new_instr(OPC_USHR); }
248 | T_OP_ISHR { new_instr(OPC_ISHR); }
249 | T_OP_ROT { new_instr(OPC_ROT); }
250 | T_OP_MUL8 { new_instr(OPC_MUL8); }
251 | T_OP_MIN { new_instr(OPC_MIN); }
252 | T_OP_MAX { new_instr(OPC_MAX); }
253 | T_OP_CMP { new_instr(OPC_CMP); }
254 | T_OP_BIC { new_instr(OPC_BIC); }
255
256 alu_2src_instr: alu_2src_op reg ',' reg ',' reg { dst($2); src1($4); src2($6); }
257 | alu_2src_op reg ',' reg ',' immediate { dst($2); src1($4); immed($6); }
258
259 alu_clrsetbit_instr: T_OP_SETBIT reg ',' reg ',' T_BIT { new_instr(OPC_SETBITI); dst($2); src1($4); bit($6); }
260 | T_OP_SETBIT reg ',' reg ',' reg { new_instr(OPC_SETBIT); dst($2); src1($4); src2($6); }
261 | T_OP_CLRBIT reg ',' reg ',' T_BIT { new_instr(OPC_CLRBIT); dst($2); src1($4); bit($6); }
262
263 alu_bitfield_op: T_OP_UBFX { new_instr(OPC_UBFX); }
264 | T_OP_BFI { new_instr(OPC_BFI); }
265
266 alu_bitfield_instr: alu_bitfield_op reg ',' reg ',' T_BIT ',' T_BIT { dst($2); src1($4); bit($6); immed($8); }
267
268 alu_instr: alu_2src_instr
269 | alu_msb_instr
270 | alu_not_instr
271 | alu_mov_instr
272 | alu_clrsetbit_instr
273 | alu_bitfield_instr
274
275 load_op: T_OP_LOAD { new_instr(OPC_LOAD); }
276 | T_OP_CREAD { new_instr(OPC_CREAD); }
277 | T_OP_SREAD { new_instr(OPC_SREAD); }
278 store_op: T_OP_STORE { new_instr(OPC_STORE); }
279 | T_OP_CWRITE { new_instr(OPC_CWRITE); instr->sds = 0; }
280 | T_SDS T_OP_CWRITE { new_instr(OPC_CWRITE); instr->sds = $1; }
281 | T_OP_SWRITE { new_instr(OPC_SWRITE); }
282
283 preincrement:
284 | '!' { instr->preincrement = true; }
285
286 load_instr: load_op reg ',' '[' reg '+' immediate ']' preincrement {
287 dst($2); src1($5); immed($7);
288 }
289 store_instr: store_op reg ',' '[' reg '+' immediate ']' preincrement {
290 src1($2); src2($5); immed($7);
291 }
292
293 branch_op: T_OP_BRNE { new_instr(OPC_BRNE); }
294 | T_OP_BREQ { new_instr(OPC_BREQ); }
295
296 branch_instr: branch_op reg ',' T_BIT ',' T_LABEL_REF { src1($2); bit($4); label($6); }
297 | branch_op reg ',' immediate ',' T_LABEL_REF { src1($2); immed($4); label($6); }
298
299 other_instr: T_OP_CALL T_LABEL_REF { new_instr(OPC_CALL); label($2); }
300 | T_OP_BL T_LABEL_REF { new_instr(OPC_BL); label($2); }
301 | T_OP_SETSECURE reg ',' T_LABEL_REF { new_instr(OPC_SETSECURE); src1($2); label($4); }
302 | T_OP_RET { new_instr(OPC_RET); }
303 | T_OP_IRET { new_instr(OPC_IRET); }
304 | T_OP_JUMP T_LABEL_REF { new_instr(OPC_JUMP); label($2); }
305 | T_OP_JUMPA T_LABEL_REF { new_instr(OPC_JUMPA); label($2); }
306 | T_OP_JUMP reg { new_instr(OPC_JUMPR); src1($2); }
307 | T_OP_SRET { new_instr(OPC_SRET); }
308 | T_OP_WAITIN { new_instr(OPC_WAITIN); }
309 | T_OP_NOP { new_instr(OPC_NOP); }
310 | T_LITERAL { new_instr(OPC_RAW_LITERAL); literal($1); }
311 | '[' T_LABEL_REF ']' { new_instr(OPC_RAW_LITERAL); label($2); }
312
313 reg: T_REGISTER
314
315 immediate: T_HEX
316 | T_INT
317 | T_CONTROL_REG
318 | T_CONTROL_REG '+' immediate { $$ = $1 + $3; }
319 | T_SQE_REG
320 | T_SQE_REG '+' immediate { $$ = $1 + $3; }
321
322