1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
|
/*
* Copyright(c) 2019-2022 rev.ng Labs Srl. All Rights Reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef PARSER_HELPERS_H
#define PARSER_HELPERS_H
#include <assert.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "tcg/tcg-cond.h"
#include "idef-parser.tab.h"
#include "idef-parser.yy.h"
#include "idef-parser.h"
/* Decomment this to disable yyasserts */
/* #define NDEBUG */
#define ERR_LINE_CONTEXT 40
#define START_COMMENT "/" "*"
#define END_COMMENT "*" "/"
void yyerror(YYLTYPE *locp,
yyscan_t scanner __attribute__((unused)),
Context *c,
const char *s);
#ifndef NDEBUG
#define yyassert(context, locp, condition, msg) \
if (!(condition)) { \
yyerror(locp, (context)->scanner, (context), (msg)); \
}
#endif
bool is_direct_predicate(HexValue *value);
bool is_inside_ternary(Context *c);
/**
* Print functions
*/
void str_print(Context *c, YYLTYPE *locp, const char *string);
void uint8_print(Context *c, YYLTYPE *locp, uint8_t *num);
void uint64_print(Context *c, YYLTYPE *locp, uint64_t *num);
void int_print(Context *c, YYLTYPE *locp, int *num);
void uint_print(Context *c, YYLTYPE *locp, unsigned *num);
void tmp_print(Context *c, YYLTYPE *locp, HexTmp *tmp);
void pred_print(Context *c, YYLTYPE *locp, HexPred *pred, bool is_dotnew);
void reg_compose(Context *c, YYLTYPE *locp, HexReg *reg, char reg_id[5]);
void reg_print(Context *c, YYLTYPE *locp, HexReg *reg);
void imm_print(Context *c, YYLTYPE *locp, HexValue *rvalue);
void var_print(Context *c, YYLTYPE *locp, HexVar *var);
void rvalue_print(Context *c, YYLTYPE *locp, void *pointer);
void out_assert(Context *c, YYLTYPE *locp, void *dummy);
/**
* Copies output code buffer into stdout
*/
void commit(Context *c);
#define OUT_IMPL(c, locp, x) \
_Generic(*(x), \
char: str_print, \
uint8_t: uint8_print, \
uint64_t: uint64_print, \
int: int_print, \
unsigned: uint_print, \
HexValue: rvalue_print, \
default: out_assert \
)(c, locp, x);
/* FOREACH macro */
#define FE_1(c, locp, WHAT, X) WHAT(c, locp, X)
#define FE_2(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_1(c, locp, WHAT, __VA_ARGS__)
#define FE_3(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_2(c, locp, WHAT, __VA_ARGS__)
#define FE_4(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_3(c, locp, WHAT, __VA_ARGS__)
#define FE_5(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_4(c, locp, WHAT, __VA_ARGS__)
#define FE_6(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_5(c, locp, WHAT, __VA_ARGS__)
#define FE_7(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_6(c, locp, WHAT, __VA_ARGS__)
#define FE_8(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_7(c, locp, WHAT, __VA_ARGS__)
#define FE_9(c, locp, WHAT, X, ...) \
WHAT(c, locp, X)FE_8(c, locp, WHAT, __VA_ARGS__)
/* repeat as needed */
#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME
#define FOR_EACH(c, locp, action, ...) \
do { \
GET_MACRO(__VA_ARGS__, \
FE_9, \
FE_8, \
FE_7, \
FE_6, \
FE_5, \
FE_4, \
FE_3, \
FE_2, \
FE_1)(c, locp, action, \
__VA_ARGS__) \
} while (0)
#define OUT(c, locp, ...) FOR_EACH((c), (locp), OUT_IMPL, __VA_ARGS__)
const char *cmp_swap(Context *c, YYLTYPE *locp, const char *type);
/**
* Temporary values creation
*/
HexValue gen_tmp(Context *c,
YYLTYPE *locp,
unsigned bit_width,
HexSignedness signedness);
HexValue gen_imm_value(Context *c __attribute__((unused)),
YYLTYPE *locp,
int value,
unsigned bit_width,
HexSignedness signedness);
HexValue gen_imm_qemu_tmp(Context *c, YYLTYPE *locp, unsigned bit_width,
HexSignedness signedness);
HexValue rvalue_materialize(Context *c, YYLTYPE *locp, HexValue *rvalue);
HexValue gen_rvalue_extend(Context *c, YYLTYPE *locp, HexValue *rvalue);
HexValue gen_rvalue_truncate(Context *c, YYLTYPE *locp, HexValue *rvalue);
void gen_varid_allocate(Context *c,
YYLTYPE *locp,
HexValue *varid,
unsigned bit_width,
HexSignedness signedness);
/**
* Code generation functions
*/
HexValue gen_bin_cmp(Context *c,
YYLTYPE *locp,
TCGCond type,
HexValue *op1,
HexValue *op2);
HexValue gen_bin_op(Context *c,
YYLTYPE *locp,
OpType type,
HexValue *op1,
HexValue *op2);
HexValue gen_cast_op(Context *c,
YYLTYPE *locp,
HexValue *src,
unsigned target_width,
HexSignedness signedness);
/**
* gen_extend_op extends a region of src_width_ptr bits stored in a
* value_ptr to the size of dst_width. Note: src_width_ptr is a
* HexValue * to handle the special case where it is unknown at
* translation time.
*/
HexValue gen_extend_op(Context *c,
YYLTYPE *locp,
HexValue *src_width,
unsigned dst_width,
HexValue *value,
HexSignedness signedness);
void gen_rdeposit_op(Context *c,
YYLTYPE *locp,
HexValue *dst,
HexValue *value,
HexValue *begin,
HexValue *width);
void gen_deposit_op(Context *c,
YYLTYPE *locp,
HexValue *dst,
HexValue *value,
HexValue *index,
HexCast *cast);
HexValue gen_rextract_op(Context *c,
YYLTYPE *locp,
HexValue *src,
unsigned begin,
unsigned width);
HexValue gen_extract_op(Context *c,
YYLTYPE *locp,
HexValue *src,
HexValue *index,
HexExtract *extract);
HexValue gen_read_reg(Context *c, YYLTYPE *locp, HexValue *reg);
void gen_write_reg(Context *c, YYLTYPE *locp, HexValue *reg, HexValue *value);
void gen_assign(Context *c,
YYLTYPE *locp,
HexValue *dst,
HexValue *value);
HexValue gen_convround(Context *c,
YYLTYPE *locp,
HexValue *src);
HexValue gen_round(Context *c,
YYLTYPE *locp,
HexValue *src,
HexValue *position);
HexValue gen_convround_n(Context *c,
YYLTYPE *locp,
HexValue *src,
HexValue *pos);
/**
* Circular addressing mode with auto-increment
*/
void gen_circ_op(Context *c,
YYLTYPE *locp,
HexValue *addr,
HexValue *increment,
HexValue *modifier);
HexValue gen_locnt_op(Context *c, YYLTYPE *locp, HexValue *src);
HexValue gen_ctpop_op(Context *c, YYLTYPE *locp, HexValue *src);
HexValue gen_rotl(Context *c, YYLTYPE *locp, HexValue *src, HexValue *n);
HexValue gen_deinterleave(Context *c, YYLTYPE *locp, HexValue *mixed);
HexValue gen_interleave(Context *c,
YYLTYPE *locp,
HexValue *odd,
HexValue *even);
HexValue gen_carry_from_add(Context *c,
YYLTYPE *locp,
HexValue *op1,
HexValue *op2,
HexValue *op3);
void gen_addsat64(Context *c,
YYLTYPE *locp,
HexValue *dst,
HexValue *op1,
HexValue *op2);
void gen_inst(Context *c, GString *iname);
void gen_inst_init_args(Context *c, YYLTYPE *locp);
void gen_inst_code(Context *c, YYLTYPE *locp);
void gen_pred_assign(Context *c, YYLTYPE *locp, HexValue *left_pred,
HexValue *right_pred);
void gen_cancel(Context *c, YYLTYPE *locp);
void gen_load_cancel(Context *c, YYLTYPE *locp);
void gen_load(Context *c, YYLTYPE *locp, HexValue *size,
HexSignedness signedness, HexValue *ea, HexValue *dst);
void gen_store(Context *c, YYLTYPE *locp, HexValue *size, HexValue *ea,
HexValue *src);
void gen_sethalf(Context *c, YYLTYPE *locp, HexCast *sh, HexValue *n,
HexValue *dst, HexValue *value);
void gen_setbits(Context *c, YYLTYPE *locp, HexValue *hi, HexValue *lo,
HexValue *dst, HexValue *value);
unsigned gen_if_cond(Context *c, YYLTYPE *locp, HexValue *cond);
unsigned gen_if_else(Context *c, YYLTYPE *locp, unsigned index);
HexValue gen_rvalue_pred(Context *c, YYLTYPE *locp, HexValue *pred);
HexValue gen_rvalue_var(Context *c, YYLTYPE *locp, HexValue *var);
HexValue gen_rvalue_mpy(Context *c, YYLTYPE *locp, HexMpy *mpy, HexValue *op1,
HexValue *op2);
HexValue gen_rvalue_not(Context *c, YYLTYPE *locp, HexValue *value);
HexValue gen_rvalue_notl(Context *c, YYLTYPE *locp, HexValue *value);
HexValue gen_rvalue_sat(Context *c, YYLTYPE *locp, HexSat *sat, HexValue *n,
HexValue *value);
HexValue gen_rvalue_fscr(Context *c, YYLTYPE *locp, HexValue *value);
HexValue gen_rvalue_abs(Context *c, YYLTYPE *locp, HexValue *value);
HexValue gen_rvalue_neg(Context *c, YYLTYPE *locp, HexValue *value);
HexValue gen_rvalue_brev(Context *c, YYLTYPE *locp, HexValue *value);
HexValue gen_rvalue_ternary(Context *c, YYLTYPE *locp, HexValue *cond,
HexValue *true_branch, HexValue *false_branch);
const char *cond_to_str(TCGCond cond);
void emit_header(Context *c);
void emit_arg(Context *c, YYLTYPE *locp, HexValue *arg);
void emit_footer(Context *c);
void track_string(Context *c, GString *s);
void free_instruction(Context *c);
void assert_signedness(Context *c,
YYLTYPE *locp,
HexSignedness signedness);
#endif /* PARSER_HELPERS_h */
|