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