1 /* 2 * Tiny Code Generator for QEMU 3 * 4 * Copyright (c) 2008 Fabrice Bellard 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 #include "qemu/osdep.h" 26 27 /* Define to jump the ELF file used to communicate with GDB. */ 28 #undef DEBUG_JIT 29 30 #include "qemu/error-report.h" 31 #include "qemu/cutils.h" 32 #include "qemu/host-utils.h" 33 #include "qemu/qemu-print.h" 34 #include "qemu/cacheflush.h" 35 #include "qemu/cacheinfo.h" 36 #include "qemu/timer.h" 37 #include "exec/translation-block.h" 38 #include "exec/tlb-common.h" 39 #include "tcg/startup.h" 40 #include "tcg/tcg-op-common.h" 41 42 #if UINTPTR_MAX == UINT32_MAX 43 # define ELF_CLASS ELFCLASS32 44 #else 45 # define ELF_CLASS ELFCLASS64 46 #endif 47 #if HOST_BIG_ENDIAN 48 # define ELF_DATA ELFDATA2MSB 49 #else 50 # define ELF_DATA ELFDATA2LSB 51 #endif 52 53 #include "elf.h" 54 #include "exec/log.h" 55 #include "tcg/tcg-ldst.h" 56 #include "tcg/tcg-temp-internal.h" 57 #include "tcg-internal.h" 58 #include "tcg/perf.h" 59 #include "tcg-has.h" 60 #ifdef CONFIG_USER_ONLY 61 #include "user/guest-base.h" 62 #endif 63 64 /* Forward declarations for functions declared in tcg-target.c.inc and 65 used here. */ 66 static void tcg_target_init(TCGContext *s); 67 static void tcg_target_qemu_prologue(TCGContext *s); 68 static bool patch_reloc(tcg_insn_unit *code_ptr, int type, 69 intptr_t value, intptr_t addend); 70 static void tcg_out_nop_fill(tcg_insn_unit *p, int count); 71 72 typedef struct TCGLabelQemuLdst TCGLabelQemuLdst; 73 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l); 74 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l); 75 76 /* The CIE and FDE header definitions will be common to all hosts. */ 77 typedef struct { 78 uint32_t len __attribute__((aligned((sizeof(void *))))); 79 uint32_t id; 80 uint8_t version; 81 char augmentation[1]; 82 uint8_t code_align; 83 uint8_t data_align; 84 uint8_t return_column; 85 } DebugFrameCIE; 86 87 typedef struct QEMU_PACKED { 88 uint32_t len __attribute__((aligned((sizeof(void *))))); 89 uint32_t cie_offset; 90 uintptr_t func_start; 91 uintptr_t func_len; 92 } DebugFrameFDEHeader; 93 94 typedef struct QEMU_PACKED { 95 DebugFrameCIE cie; 96 DebugFrameFDEHeader fde; 97 } DebugFrameHeader; 98 99 struct TCGLabelQemuLdst { 100 bool is_ld; /* qemu_ld: true, qemu_st: false */ 101 MemOpIdx oi; 102 TCGType type; /* result type of a load */ 103 TCGReg addr_reg; /* reg index for guest virtual addr */ 104 TCGReg datalo_reg; /* reg index for low word to be loaded or stored */ 105 TCGReg datahi_reg; /* reg index for high word to be loaded or stored */ 106 const tcg_insn_unit *raddr; /* addr of the next IR of qemu_ld/st IR */ 107 tcg_insn_unit *label_ptr[2]; /* label pointers to be updated */ 108 QSIMPLEQ_ENTRY(TCGLabelQemuLdst) next; 109 }; 110 111 static void tcg_register_jit_int(const void *buf, size_t size, 112 const void *debug_frame, 113 size_t debug_frame_size) 114 __attribute__((unused)); 115 116 /* Forward declarations for functions declared and used in tcg-target.c.inc. */ 117 static void tcg_out_tb_start(TCGContext *s); 118 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1, 119 intptr_t arg2); 120 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg); 121 static void tcg_out_movi(TCGContext *s, TCGType type, 122 TCGReg ret, tcg_target_long arg); 123 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg); 124 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg); 125 static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg); 126 static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg); 127 static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg); 128 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg); 129 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg); 130 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg); 131 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg); 132 static void tcg_out_addi_ptr(TCGContext *s, TCGReg, TCGReg, tcg_target_long); 133 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2); 134 static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg); 135 static void tcg_out_goto_tb(TCGContext *s, int which); 136 static void tcg_out_op(TCGContext *s, TCGOpcode opc, TCGType type, 137 const TCGArg args[TCG_MAX_OP_ARGS], 138 const int const_args[TCG_MAX_OP_ARGS]); 139 #if TCG_TARGET_MAYBE_vec 140 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, 141 TCGReg dst, TCGReg src); 142 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, 143 TCGReg dst, TCGReg base, intptr_t offset); 144 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, 145 TCGReg dst, int64_t arg); 146 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, 147 unsigned vecl, unsigned vece, 148 const TCGArg args[TCG_MAX_OP_ARGS], 149 const int const_args[TCG_MAX_OP_ARGS]); 150 #else 151 static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece, 152 TCGReg dst, TCGReg src) 153 { 154 g_assert_not_reached(); 155 } 156 static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece, 157 TCGReg dst, TCGReg base, intptr_t offset) 158 { 159 g_assert_not_reached(); 160 } 161 static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece, 162 TCGReg dst, int64_t arg) 163 { 164 g_assert_not_reached(); 165 } 166 static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, 167 unsigned vecl, unsigned vece, 168 const TCGArg args[TCG_MAX_OP_ARGS], 169 const int const_args[TCG_MAX_OP_ARGS]) 170 { 171 g_assert_not_reached(); 172 } 173 int tcg_can_emit_vec_op(TCGOpcode o, TCGType t, unsigned ve) 174 { 175 return 0; 176 } 177 #endif 178 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1, 179 intptr_t arg2); 180 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val, 181 TCGReg base, intptr_t ofs); 182 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target, 183 const TCGHelperInfo *info); 184 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot); 185 static bool tcg_target_const_match(int64_t val, int ct, 186 TCGType type, TCGCond cond, int vece); 187 188 #ifndef CONFIG_USER_ONLY 189 #define guest_base ({ qemu_build_not_reached(); (uintptr_t)0; }) 190 #endif 191 192 typedef struct TCGLdstHelperParam { 193 TCGReg (*ra_gen)(TCGContext *s, const TCGLabelQemuLdst *l, int arg_reg); 194 unsigned ntmp; 195 int tmp[3]; 196 } TCGLdstHelperParam; 197 198 static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *l, 199 const TCGLdstHelperParam *p) 200 __attribute__((unused)); 201 static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *l, 202 bool load_sign, const TCGLdstHelperParam *p) 203 __attribute__((unused)); 204 static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *l, 205 const TCGLdstHelperParam *p) 206 __attribute__((unused)); 207 208 static void * const qemu_ld_helpers[MO_SSIZE + 1] __attribute__((unused)) = { 209 [MO_UB] = helper_ldub_mmu, 210 [MO_SB] = helper_ldsb_mmu, 211 [MO_UW] = helper_lduw_mmu, 212 [MO_SW] = helper_ldsw_mmu, 213 [MO_UL] = helper_ldul_mmu, 214 [MO_UQ] = helper_ldq_mmu, 215 #if TCG_TARGET_REG_BITS == 64 216 [MO_SL] = helper_ldsl_mmu, 217 [MO_128] = helper_ld16_mmu, 218 #endif 219 }; 220 221 static void * const qemu_st_helpers[MO_SIZE + 1] __attribute__((unused)) = { 222 [MO_8] = helper_stb_mmu, 223 [MO_16] = helper_stw_mmu, 224 [MO_32] = helper_stl_mmu, 225 [MO_64] = helper_stq_mmu, 226 #if TCG_TARGET_REG_BITS == 64 227 [MO_128] = helper_st16_mmu, 228 #endif 229 }; 230 231 typedef struct { 232 MemOp atom; /* lg2 bits of atomicity required */ 233 MemOp align; /* lg2 bits of alignment to use */ 234 } TCGAtomAlign; 235 236 static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc, 237 MemOp host_atom, bool allow_two_ops) 238 __attribute__((unused)); 239 240 #ifdef CONFIG_USER_ONLY 241 bool tcg_use_softmmu; 242 #endif 243 244 TCGContext tcg_init_ctx; 245 __thread TCGContext *tcg_ctx; 246 247 TCGContext **tcg_ctxs; 248 unsigned int tcg_cur_ctxs; 249 unsigned int tcg_max_ctxs; 250 TCGv_env tcg_env; 251 const void *tcg_code_gen_epilogue; 252 uintptr_t tcg_splitwx_diff; 253 254 #ifndef CONFIG_TCG_INTERPRETER 255 tcg_prologue_fn *tcg_qemu_tb_exec; 256 #endif 257 258 static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT]; 259 static TCGRegSet tcg_target_call_clobber_regs; 260 261 #if TCG_TARGET_INSN_UNIT_SIZE == 1 262 static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v) 263 { 264 *s->code_ptr++ = v; 265 } 266 267 static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p, 268 uint8_t v) 269 { 270 *p = v; 271 } 272 #endif 273 274 #if TCG_TARGET_INSN_UNIT_SIZE <= 2 275 static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v) 276 { 277 if (TCG_TARGET_INSN_UNIT_SIZE == 2) { 278 *s->code_ptr++ = v; 279 } else { 280 tcg_insn_unit *p = s->code_ptr; 281 memcpy(p, &v, sizeof(v)); 282 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE); 283 } 284 } 285 286 static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p, 287 uint16_t v) 288 { 289 if (TCG_TARGET_INSN_UNIT_SIZE == 2) { 290 *p = v; 291 } else { 292 memcpy(p, &v, sizeof(v)); 293 } 294 } 295 #endif 296 297 #if TCG_TARGET_INSN_UNIT_SIZE <= 4 298 static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v) 299 { 300 if (TCG_TARGET_INSN_UNIT_SIZE == 4) { 301 *s->code_ptr++ = v; 302 } else { 303 tcg_insn_unit *p = s->code_ptr; 304 memcpy(p, &v, sizeof(v)); 305 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE); 306 } 307 } 308 309 static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p, 310 uint32_t v) 311 { 312 if (TCG_TARGET_INSN_UNIT_SIZE == 4) { 313 *p = v; 314 } else { 315 memcpy(p, &v, sizeof(v)); 316 } 317 } 318 #endif 319 320 #if TCG_TARGET_INSN_UNIT_SIZE <= 8 321 static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v) 322 { 323 if (TCG_TARGET_INSN_UNIT_SIZE == 8) { 324 *s->code_ptr++ = v; 325 } else { 326 tcg_insn_unit *p = s->code_ptr; 327 memcpy(p, &v, sizeof(v)); 328 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE); 329 } 330 } 331 332 static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p, 333 uint64_t v) 334 { 335 if (TCG_TARGET_INSN_UNIT_SIZE == 8) { 336 *p = v; 337 } else { 338 memcpy(p, &v, sizeof(v)); 339 } 340 } 341 #endif 342 343 /* label relocation processing */ 344 345 static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type, 346 TCGLabel *l, intptr_t addend) 347 { 348 TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation)); 349 350 r->type = type; 351 r->ptr = code_ptr; 352 r->addend = addend; 353 QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next); 354 } 355 356 static void tcg_out_label(TCGContext *s, TCGLabel *l) 357 { 358 tcg_debug_assert(!l->has_value); 359 l->has_value = 1; 360 l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr); 361 } 362 363 TCGLabel *gen_new_label(void) 364 { 365 TCGContext *s = tcg_ctx; 366 TCGLabel *l = tcg_malloc(sizeof(TCGLabel)); 367 368 memset(l, 0, sizeof(TCGLabel)); 369 l->id = s->nb_labels++; 370 QSIMPLEQ_INIT(&l->branches); 371 QSIMPLEQ_INIT(&l->relocs); 372 373 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next); 374 375 return l; 376 } 377 378 static bool tcg_resolve_relocs(TCGContext *s) 379 { 380 TCGLabel *l; 381 382 QSIMPLEQ_FOREACH(l, &s->labels, next) { 383 TCGRelocation *r; 384 uintptr_t value = l->u.value; 385 386 QSIMPLEQ_FOREACH(r, &l->relocs, next) { 387 if (!patch_reloc(r->ptr, r->type, value, r->addend)) { 388 return false; 389 } 390 } 391 } 392 return true; 393 } 394 395 static void set_jmp_reset_offset(TCGContext *s, int which) 396 { 397 /* 398 * We will check for overflow at the end of the opcode loop in 399 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX. 400 */ 401 s->gen_tb->jmp_reset_offset[which] = tcg_current_code_size(s); 402 } 403 404 static void G_GNUC_UNUSED set_jmp_insn_offset(TCGContext *s, int which) 405 { 406 /* 407 * We will check for overflow at the end of the opcode loop in 408 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX. 409 */ 410 s->gen_tb->jmp_insn_offset[which] = tcg_current_code_size(s); 411 } 412 413 static uintptr_t G_GNUC_UNUSED get_jmp_target_addr(TCGContext *s, int which) 414 { 415 /* 416 * Return the read-execute version of the pointer, for the benefit 417 * of any pc-relative addressing mode. 418 */ 419 return (uintptr_t)tcg_splitwx_to_rx(&s->gen_tb->jmp_target_addr[which]); 420 } 421 422 static int __attribute__((unused)) 423 tlb_mask_table_ofs(TCGContext *s, int which) 424 { 425 return (offsetof(CPUNegativeOffsetState, tlb.f[which]) - 426 sizeof(CPUNegativeOffsetState)); 427 } 428 429 /* Signal overflow, starting over with fewer guest insns. */ 430 static G_NORETURN 431 void tcg_raise_tb_overflow(TCGContext *s) 432 { 433 siglongjmp(s->jmp_trans, -2); 434 } 435 436 /* 437 * Used by tcg_out_movext{1,2} to hold the arguments for tcg_out_movext. 438 * By the time we arrive at tcg_out_movext1, @dst is always a TCGReg. 439 * 440 * However, tcg_out_helper_load_slots reuses this field to hold an 441 * argument slot number (which may designate a argument register or an 442 * argument stack slot), converting to TCGReg once all arguments that 443 * are destined for the stack are processed. 444 */ 445 typedef struct TCGMovExtend { 446 unsigned dst; 447 TCGReg src; 448 TCGType dst_type; 449 TCGType src_type; 450 MemOp src_ext; 451 } TCGMovExtend; 452 453 /** 454 * tcg_out_movext -- move and extend 455 * @s: tcg context 456 * @dst_type: integral type for destination 457 * @dst: destination register 458 * @src_type: integral type for source 459 * @src_ext: extension to apply to source 460 * @src: source register 461 * 462 * Move or extend @src into @dst, depending on @src_ext and the types. 463 */ 464 static void tcg_out_movext(TCGContext *s, TCGType dst_type, TCGReg dst, 465 TCGType src_type, MemOp src_ext, TCGReg src) 466 { 467 switch (src_ext) { 468 case MO_UB: 469 tcg_out_ext8u(s, dst, src); 470 break; 471 case MO_SB: 472 tcg_out_ext8s(s, dst_type, dst, src); 473 break; 474 case MO_UW: 475 tcg_out_ext16u(s, dst, src); 476 break; 477 case MO_SW: 478 tcg_out_ext16s(s, dst_type, dst, src); 479 break; 480 case MO_UL: 481 case MO_SL: 482 if (dst_type == TCG_TYPE_I32) { 483 if (src_type == TCG_TYPE_I32) { 484 tcg_out_mov(s, TCG_TYPE_I32, dst, src); 485 } else { 486 tcg_out_extrl_i64_i32(s, dst, src); 487 } 488 } else if (src_type == TCG_TYPE_I32) { 489 if (src_ext & MO_SIGN) { 490 tcg_out_exts_i32_i64(s, dst, src); 491 } else { 492 tcg_out_extu_i32_i64(s, dst, src); 493 } 494 } else { 495 if (src_ext & MO_SIGN) { 496 tcg_out_ext32s(s, dst, src); 497 } else { 498 tcg_out_ext32u(s, dst, src); 499 } 500 } 501 break; 502 case MO_UQ: 503 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 504 if (dst_type == TCG_TYPE_I32) { 505 tcg_out_extrl_i64_i32(s, dst, src); 506 } else { 507 tcg_out_mov(s, TCG_TYPE_I64, dst, src); 508 } 509 break; 510 default: 511 g_assert_not_reached(); 512 } 513 } 514 515 /* Minor variations on a theme, using a structure. */ 516 static void tcg_out_movext1_new_src(TCGContext *s, const TCGMovExtend *i, 517 TCGReg src) 518 { 519 tcg_out_movext(s, i->dst_type, i->dst, i->src_type, i->src_ext, src); 520 } 521 522 static void tcg_out_movext1(TCGContext *s, const TCGMovExtend *i) 523 { 524 tcg_out_movext1_new_src(s, i, i->src); 525 } 526 527 /** 528 * tcg_out_movext2 -- move and extend two pair 529 * @s: tcg context 530 * @i1: first move description 531 * @i2: second move description 532 * @scratch: temporary register, or -1 for none 533 * 534 * As tcg_out_movext, for both @i1 and @i2, caring for overlap 535 * between the sources and destinations. 536 */ 537 538 static void tcg_out_movext2(TCGContext *s, const TCGMovExtend *i1, 539 const TCGMovExtend *i2, int scratch) 540 { 541 TCGReg src1 = i1->src; 542 TCGReg src2 = i2->src; 543 544 if (i1->dst != src2) { 545 tcg_out_movext1(s, i1); 546 tcg_out_movext1(s, i2); 547 return; 548 } 549 if (i2->dst == src1) { 550 TCGType src1_type = i1->src_type; 551 TCGType src2_type = i2->src_type; 552 553 if (tcg_out_xchg(s, MAX(src1_type, src2_type), src1, src2)) { 554 /* The data is now in the correct registers, now extend. */ 555 src1 = i2->src; 556 src2 = i1->src; 557 } else { 558 tcg_debug_assert(scratch >= 0); 559 tcg_out_mov(s, src1_type, scratch, src1); 560 src1 = scratch; 561 } 562 } 563 tcg_out_movext1_new_src(s, i2, src2); 564 tcg_out_movext1_new_src(s, i1, src1); 565 } 566 567 /** 568 * tcg_out_movext3 -- move and extend three pair 569 * @s: tcg context 570 * @i1: first move description 571 * @i2: second move description 572 * @i3: third move description 573 * @scratch: temporary register, or -1 for none 574 * 575 * As tcg_out_movext, for all of @i1, @i2 and @i3, caring for overlap 576 * between the sources and destinations. 577 */ 578 579 static void tcg_out_movext3(TCGContext *s, const TCGMovExtend *i1, 580 const TCGMovExtend *i2, const TCGMovExtend *i3, 581 int scratch) 582 { 583 TCGReg src1 = i1->src; 584 TCGReg src2 = i2->src; 585 TCGReg src3 = i3->src; 586 587 if (i1->dst != src2 && i1->dst != src3) { 588 tcg_out_movext1(s, i1); 589 tcg_out_movext2(s, i2, i3, scratch); 590 return; 591 } 592 if (i2->dst != src1 && i2->dst != src3) { 593 tcg_out_movext1(s, i2); 594 tcg_out_movext2(s, i1, i3, scratch); 595 return; 596 } 597 if (i3->dst != src1 && i3->dst != src2) { 598 tcg_out_movext1(s, i3); 599 tcg_out_movext2(s, i1, i2, scratch); 600 return; 601 } 602 603 /* 604 * There is a cycle. Since there are only 3 nodes, the cycle is 605 * either "clockwise" or "anti-clockwise", and can be solved with 606 * a single scratch or two xchg. 607 */ 608 if (i1->dst == src2 && i2->dst == src3 && i3->dst == src1) { 609 /* "Clockwise" */ 610 if (tcg_out_xchg(s, MAX(i1->src_type, i2->src_type), src1, src2)) { 611 tcg_out_xchg(s, MAX(i2->src_type, i3->src_type), src2, src3); 612 /* The data is now in the correct registers, now extend. */ 613 tcg_out_movext1_new_src(s, i1, i1->dst); 614 tcg_out_movext1_new_src(s, i2, i2->dst); 615 tcg_out_movext1_new_src(s, i3, i3->dst); 616 } else { 617 tcg_debug_assert(scratch >= 0); 618 tcg_out_mov(s, i1->src_type, scratch, src1); 619 tcg_out_movext1(s, i3); 620 tcg_out_movext1(s, i2); 621 tcg_out_movext1_new_src(s, i1, scratch); 622 } 623 } else if (i1->dst == src3 && i2->dst == src1 && i3->dst == src2) { 624 /* "Anti-clockwise" */ 625 if (tcg_out_xchg(s, MAX(i2->src_type, i3->src_type), src2, src3)) { 626 tcg_out_xchg(s, MAX(i1->src_type, i2->src_type), src1, src2); 627 /* The data is now in the correct registers, now extend. */ 628 tcg_out_movext1_new_src(s, i1, i1->dst); 629 tcg_out_movext1_new_src(s, i2, i2->dst); 630 tcg_out_movext1_new_src(s, i3, i3->dst); 631 } else { 632 tcg_debug_assert(scratch >= 0); 633 tcg_out_mov(s, i1->src_type, scratch, src1); 634 tcg_out_movext1(s, i2); 635 tcg_out_movext1(s, i3); 636 tcg_out_movext1_new_src(s, i1, scratch); 637 } 638 } else { 639 g_assert_not_reached(); 640 } 641 } 642 643 /* 644 * Allocate a new TCGLabelQemuLdst entry. 645 */ 646 647 __attribute__((unused)) 648 static TCGLabelQemuLdst *new_ldst_label(TCGContext *s) 649 { 650 TCGLabelQemuLdst *l = tcg_malloc(sizeof(*l)); 651 652 memset(l, 0, sizeof(*l)); 653 QSIMPLEQ_INSERT_TAIL(&s->ldst_labels, l, next); 654 655 return l; 656 } 657 658 /* 659 * Allocate new constant pool entries. 660 */ 661 662 typedef struct TCGLabelPoolData { 663 struct TCGLabelPoolData *next; 664 tcg_insn_unit *label; 665 intptr_t addend; 666 int rtype; 667 unsigned nlong; 668 tcg_target_ulong data[]; 669 } TCGLabelPoolData; 670 671 static TCGLabelPoolData *new_pool_alloc(TCGContext *s, int nlong, int rtype, 672 tcg_insn_unit *label, intptr_t addend) 673 { 674 TCGLabelPoolData *n = tcg_malloc(sizeof(TCGLabelPoolData) 675 + sizeof(tcg_target_ulong) * nlong); 676 677 n->label = label; 678 n->addend = addend; 679 n->rtype = rtype; 680 n->nlong = nlong; 681 return n; 682 } 683 684 static void new_pool_insert(TCGContext *s, TCGLabelPoolData *n) 685 { 686 TCGLabelPoolData *i, **pp; 687 int nlong = n->nlong; 688 689 /* Insertion sort on the pool. */ 690 for (pp = &s->pool_labels; (i = *pp) != NULL; pp = &i->next) { 691 if (nlong > i->nlong) { 692 break; 693 } 694 if (nlong < i->nlong) { 695 continue; 696 } 697 if (memcmp(n->data, i->data, sizeof(tcg_target_ulong) * nlong) >= 0) { 698 break; 699 } 700 } 701 n->next = *pp; 702 *pp = n; 703 } 704 705 /* The "usual" for generic integer code. */ 706 __attribute__((unused)) 707 static void new_pool_label(TCGContext *s, tcg_target_ulong d, int rtype, 708 tcg_insn_unit *label, intptr_t addend) 709 { 710 TCGLabelPoolData *n = new_pool_alloc(s, 1, rtype, label, addend); 711 n->data[0] = d; 712 new_pool_insert(s, n); 713 } 714 715 /* For v64 or v128, depending on the host. */ 716 __attribute__((unused)) 717 static void new_pool_l2(TCGContext *s, int rtype, tcg_insn_unit *label, 718 intptr_t addend, tcg_target_ulong d0, 719 tcg_target_ulong d1) 720 { 721 TCGLabelPoolData *n = new_pool_alloc(s, 2, rtype, label, addend); 722 n->data[0] = d0; 723 n->data[1] = d1; 724 new_pool_insert(s, n); 725 } 726 727 /* For v128 or v256, depending on the host. */ 728 __attribute__((unused)) 729 static void new_pool_l4(TCGContext *s, int rtype, tcg_insn_unit *label, 730 intptr_t addend, tcg_target_ulong d0, 731 tcg_target_ulong d1, tcg_target_ulong d2, 732 tcg_target_ulong d3) 733 { 734 TCGLabelPoolData *n = new_pool_alloc(s, 4, rtype, label, addend); 735 n->data[0] = d0; 736 n->data[1] = d1; 737 n->data[2] = d2; 738 n->data[3] = d3; 739 new_pool_insert(s, n); 740 } 741 742 /* For v256, for 32-bit host. */ 743 __attribute__((unused)) 744 static void new_pool_l8(TCGContext *s, int rtype, tcg_insn_unit *label, 745 intptr_t addend, tcg_target_ulong d0, 746 tcg_target_ulong d1, tcg_target_ulong d2, 747 tcg_target_ulong d3, tcg_target_ulong d4, 748 tcg_target_ulong d5, tcg_target_ulong d6, 749 tcg_target_ulong d7) 750 { 751 TCGLabelPoolData *n = new_pool_alloc(s, 8, rtype, label, addend); 752 n->data[0] = d0; 753 n->data[1] = d1; 754 n->data[2] = d2; 755 n->data[3] = d3; 756 n->data[4] = d4; 757 n->data[5] = d5; 758 n->data[6] = d6; 759 n->data[7] = d7; 760 new_pool_insert(s, n); 761 } 762 763 /* 764 * Generate TB finalization at the end of block 765 */ 766 767 static int tcg_out_ldst_finalize(TCGContext *s) 768 { 769 TCGLabelQemuLdst *lb; 770 771 /* qemu_ld/st slow paths */ 772 QSIMPLEQ_FOREACH(lb, &s->ldst_labels, next) { 773 if (lb->is_ld 774 ? !tcg_out_qemu_ld_slow_path(s, lb) 775 : !tcg_out_qemu_st_slow_path(s, lb)) { 776 return -2; 777 } 778 779 /* 780 * Test for (pending) buffer overflow. The assumption is that any 781 * one operation beginning below the high water mark cannot overrun 782 * the buffer completely. Thus we can test for overflow after 783 * generating code without having to check during generation. 784 */ 785 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) { 786 return -1; 787 } 788 } 789 return 0; 790 } 791 792 static int tcg_out_pool_finalize(TCGContext *s) 793 { 794 TCGLabelPoolData *p = s->pool_labels; 795 TCGLabelPoolData *l = NULL; 796 void *a; 797 798 if (p == NULL) { 799 return 0; 800 } 801 802 /* 803 * ??? Round up to qemu_icache_linesize, but then do not round 804 * again when allocating the next TranslationBlock structure. 805 */ 806 a = (void *)ROUND_UP((uintptr_t)s->code_ptr, 807 sizeof(tcg_target_ulong) * p->nlong); 808 tcg_out_nop_fill(s->code_ptr, (tcg_insn_unit *)a - s->code_ptr); 809 s->data_gen_ptr = a; 810 811 for (; p != NULL; p = p->next) { 812 size_t size = sizeof(tcg_target_ulong) * p->nlong; 813 uintptr_t value; 814 815 if (!l || l->nlong != p->nlong || memcmp(l->data, p->data, size)) { 816 if (unlikely(a > s->code_gen_highwater)) { 817 return -1; 818 } 819 memcpy(a, p->data, size); 820 a += size; 821 l = p; 822 } 823 824 value = (uintptr_t)tcg_splitwx_to_rx(a) - size; 825 if (!patch_reloc(p->label, p->rtype, value, p->addend)) { 826 return -2; 827 } 828 } 829 830 s->code_ptr = a; 831 return 0; 832 } 833 834 #define C_PFX1(P, A) P##A 835 #define C_PFX2(P, A, B) P##A##_##B 836 #define C_PFX3(P, A, B, C) P##A##_##B##_##C 837 #define C_PFX4(P, A, B, C, D) P##A##_##B##_##C##_##D 838 #define C_PFX5(P, A, B, C, D, E) P##A##_##B##_##C##_##D##_##E 839 #define C_PFX6(P, A, B, C, D, E, F) P##A##_##B##_##C##_##D##_##E##_##F 840 841 /* Define an enumeration for the various combinations. */ 842 843 #define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1), 844 #define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2), 845 #define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3), 846 #define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4), 847 848 #define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1), 849 #define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2), 850 #define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3), 851 #define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4), 852 853 #define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2), 854 #define C_N1O1_I1(O1, O2, I1) C_PFX3(c_n1o1_i1_, O1, O2, I1), 855 #define C_N2_I1(O1, O2, I1) C_PFX3(c_n2_i1_, O1, O2, I1), 856 857 #define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1), 858 #define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2), 859 #define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3), 860 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4), 861 #define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_n1_o1_i4_, O1, O2, I1, I2, I3, I4), 862 863 typedef enum { 864 C_Dynamic = -2, 865 C_NotImplemented = -1, 866 #include "tcg-target-con-set.h" 867 } TCGConstraintSetIndex; 868 869 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode, TCGType, unsigned); 870 871 #undef C_O0_I1 872 #undef C_O0_I2 873 #undef C_O0_I3 874 #undef C_O0_I4 875 #undef C_O1_I1 876 #undef C_O1_I2 877 #undef C_O1_I3 878 #undef C_O1_I4 879 #undef C_N1_I2 880 #undef C_N1O1_I1 881 #undef C_N2_I1 882 #undef C_O2_I1 883 #undef C_O2_I2 884 #undef C_O2_I3 885 #undef C_O2_I4 886 #undef C_N1_O1_I4 887 888 /* Put all of the constraint sets into an array, indexed by the enum. */ 889 890 typedef struct TCGConstraintSet { 891 uint8_t nb_oargs, nb_iargs; 892 const char *args_ct_str[TCG_MAX_OP_ARGS]; 893 } TCGConstraintSet; 894 895 #define C_O0_I1(I1) { 0, 1, { #I1 } }, 896 #define C_O0_I2(I1, I2) { 0, 2, { #I1, #I2 } }, 897 #define C_O0_I3(I1, I2, I3) { 0, 3, { #I1, #I2, #I3 } }, 898 #define C_O0_I4(I1, I2, I3, I4) { 0, 4, { #I1, #I2, #I3, #I4 } }, 899 900 #define C_O1_I1(O1, I1) { 1, 1, { #O1, #I1 } }, 901 #define C_O1_I2(O1, I1, I2) { 1, 2, { #O1, #I1, #I2 } }, 902 #define C_O1_I3(O1, I1, I2, I3) { 1, 3, { #O1, #I1, #I2, #I3 } }, 903 #define C_O1_I4(O1, I1, I2, I3, I4) { 1, 4, { #O1, #I1, #I2, #I3, #I4 } }, 904 905 #define C_N1_I2(O1, I1, I2) { 1, 2, { "&" #O1, #I1, #I2 } }, 906 #define C_N1O1_I1(O1, O2, I1) { 2, 1, { "&" #O1, #O2, #I1 } }, 907 #define C_N2_I1(O1, O2, I1) { 2, 1, { "&" #O1, "&" #O2, #I1 } }, 908 909 #define C_O2_I1(O1, O2, I1) { 2, 1, { #O1, #O2, #I1 } }, 910 #define C_O2_I2(O1, O2, I1, I2) { 2, 2, { #O1, #O2, #I1, #I2 } }, 911 #define C_O2_I3(O1, O2, I1, I2, I3) { 2, 3, { #O1, #O2, #I1, #I2, #I3 } }, 912 #define C_O2_I4(O1, O2, I1, I2, I3, I4) { 2, 4, { #O1, #O2, #I1, #I2, #I3, #I4 } }, 913 #define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) { 2, 4, { "&" #O1, #O2, #I1, #I2, #I3, #I4 } }, 914 915 static const TCGConstraintSet constraint_sets[] = { 916 #include "tcg-target-con-set.h" 917 }; 918 919 #undef C_O0_I1 920 #undef C_O0_I2 921 #undef C_O0_I3 922 #undef C_O0_I4 923 #undef C_O1_I1 924 #undef C_O1_I2 925 #undef C_O1_I3 926 #undef C_O1_I4 927 #undef C_N1_I2 928 #undef C_N1O1_I1 929 #undef C_N2_I1 930 #undef C_O2_I1 931 #undef C_O2_I2 932 #undef C_O2_I3 933 #undef C_O2_I4 934 #undef C_N1_O1_I4 935 936 /* Expand the enumerator to be returned from tcg_target_op_def(). */ 937 938 #define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1) 939 #define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2) 940 #define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3) 941 #define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4) 942 943 #define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1) 944 #define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2) 945 #define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3) 946 #define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4) 947 948 #define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2) 949 #define C_N1O1_I1(O1, O2, I1) C_PFX3(c_n1o1_i1_, O1, O2, I1) 950 #define C_N2_I1(O1, O2, I1) C_PFX3(c_n2_i1_, O1, O2, I1) 951 952 #define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1) 953 #define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2) 954 #define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3) 955 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4) 956 #define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_n1_o1_i4_, O1, O2, I1, I2, I3, I4) 957 958 /* 959 * TCGOutOp is the base class for a set of structures that describe how 960 * to generate code for a given TCGOpcode. 961 * 962 * @static_constraint: 963 * C_NotImplemented: The TCGOpcode is not supported by the backend. 964 * C_Dynamic: Use @dynamic_constraint to select a constraint set 965 * based on any of @type, @flags, or host isa. 966 * Otherwise: The register allocation constrains for the TCGOpcode. 967 * 968 * Subclasses of TCGOutOp will define a set of output routines that may 969 * be used. Such routines will often be selected by the set of registers 970 * and constants that come out of register allocation. The set of 971 * routines that are provided will guide the set of constraints that are 972 * legal. In particular, assume that tcg_optimize() has done its job in 973 * swapping commutative operands and folding operations for which all 974 * operands are constant. 975 */ 976 typedef struct TCGOutOp { 977 TCGConstraintSetIndex static_constraint; 978 TCGConstraintSetIndex (*dynamic_constraint)(TCGType type, unsigned flags); 979 } TCGOutOp; 980 981 typedef struct TCGOutOpBinary { 982 TCGOutOp base; 983 void (*out_rrr)(TCGContext *s, TCGType type, 984 TCGReg a0, TCGReg a1, TCGReg a2); 985 void (*out_rri)(TCGContext *s, TCGType type, 986 TCGReg a0, TCGReg a1, tcg_target_long a2); 987 } TCGOutOpBinary; 988 989 typedef struct TCGOutOpBrcond { 990 TCGOutOp base; 991 void (*out_rr)(TCGContext *s, TCGType type, TCGCond cond, 992 TCGReg a1, TCGReg a2, TCGLabel *label); 993 void (*out_ri)(TCGContext *s, TCGType type, TCGCond cond, 994 TCGReg a1, tcg_target_long a2, TCGLabel *label); 995 } TCGOutOpBrcond; 996 997 typedef struct TCGOutOpBrcond2 { 998 TCGOutOp base; 999 void (*out)(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah, 1000 TCGArg bl, bool const_bl, 1001 TCGArg bh, bool const_bh, TCGLabel *l); 1002 } TCGOutOpBrcond2; 1003 1004 typedef struct TCGOutOpBswap { 1005 TCGOutOp base; 1006 void (*out_rr)(TCGContext *s, TCGType type, 1007 TCGReg a0, TCGReg a1, unsigned flags); 1008 } TCGOutOpBswap; 1009 1010 typedef struct TCGOutOpDivRem { 1011 TCGOutOp base; 1012 void (*out_rr01r)(TCGContext *s, TCGType type, 1013 TCGReg a0, TCGReg a1, TCGReg a4); 1014 } TCGOutOpDivRem; 1015 1016 typedef struct TCGOutOpExtract { 1017 TCGOutOp base; 1018 void (*out_rr)(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1, 1019 unsigned ofs, unsigned len); 1020 } TCGOutOpExtract; 1021 1022 typedef struct TCGOutOpMovcond { 1023 TCGOutOp base; 1024 void (*out)(TCGContext *s, TCGType type, TCGCond cond, 1025 TCGReg ret, TCGReg c1, TCGArg c2, bool const_c2, 1026 TCGArg vt, bool const_vt, TCGArg vf, bool consf_vf); 1027 } TCGOutOpMovcond; 1028 1029 typedef struct TCGOutOpMul2 { 1030 TCGOutOp base; 1031 void (*out_rrrr)(TCGContext *s, TCGType type, 1032 TCGReg a0, TCGReg a1, TCGReg a2, TCGReg a3); 1033 } TCGOutOpMul2; 1034 1035 typedef struct TCGOutOpUnary { 1036 TCGOutOp base; 1037 void (*out_rr)(TCGContext *s, TCGType type, TCGReg a0, TCGReg a1); 1038 } TCGOutOpUnary; 1039 1040 typedef struct TCGOutOpSetcond { 1041 TCGOutOp base; 1042 void (*out_rrr)(TCGContext *s, TCGType type, TCGCond cond, 1043 TCGReg ret, TCGReg a1, TCGReg a2); 1044 void (*out_rri)(TCGContext *s, TCGType type, TCGCond cond, 1045 TCGReg ret, TCGReg a1, tcg_target_long a2); 1046 } TCGOutOpSetcond; 1047 1048 typedef struct TCGOutOpSetcond2 { 1049 TCGOutOp base; 1050 void (*out)(TCGContext *s, TCGCond cond, TCGReg ret, TCGReg al, TCGReg ah, 1051 TCGArg bl, bool const_bl, TCGArg bh, bool const_bh); 1052 } TCGOutOpSetcond2; 1053 1054 typedef struct TCGOutOpSubtract { 1055 TCGOutOp base; 1056 void (*out_rrr)(TCGContext *s, TCGType type, 1057 TCGReg a0, TCGReg a1, TCGReg a2); 1058 void (*out_rir)(TCGContext *s, TCGType type, 1059 TCGReg a0, tcg_target_long a1, TCGReg a2); 1060 } TCGOutOpSubtract; 1061 1062 #include "tcg-target.c.inc" 1063 1064 #ifndef CONFIG_TCG_INTERPRETER 1065 /* Validate CPUTLBDescFast placement. */ 1066 QEMU_BUILD_BUG_ON((int)(offsetof(CPUNegativeOffsetState, tlb.f[0]) - 1067 sizeof(CPUNegativeOffsetState)) 1068 < MIN_TLB_MASK_TABLE_OFS); 1069 #endif 1070 1071 /* 1072 * Register V as the TCGOutOp for O. 1073 * This verifies that V is of type T, otherwise give a nice compiler error. 1074 * This prevents trivial mistakes within each arch/tcg-target.c.inc. 1075 */ 1076 #define OUTOP(O, T, V) [O] = _Generic(V, T: &V.base) 1077 1078 /* Register allocation descriptions for every TCGOpcode. */ 1079 static const TCGOutOp * const all_outop[NB_OPS] = { 1080 OUTOP(INDEX_op_add, TCGOutOpBinary, outop_add), 1081 OUTOP(INDEX_op_and, TCGOutOpBinary, outop_and), 1082 OUTOP(INDEX_op_andc, TCGOutOpBinary, outop_andc), 1083 OUTOP(INDEX_op_brcond, TCGOutOpBrcond, outop_brcond), 1084 OUTOP(INDEX_op_bswap16, TCGOutOpBswap, outop_bswap16), 1085 OUTOP(INDEX_op_bswap32, TCGOutOpBswap, outop_bswap32), 1086 OUTOP(INDEX_op_clz, TCGOutOpBinary, outop_clz), 1087 OUTOP(INDEX_op_ctpop, TCGOutOpUnary, outop_ctpop), 1088 OUTOP(INDEX_op_ctz, TCGOutOpBinary, outop_ctz), 1089 OUTOP(INDEX_op_divs, TCGOutOpBinary, outop_divs), 1090 OUTOP(INDEX_op_divu, TCGOutOpBinary, outop_divu), 1091 OUTOP(INDEX_op_divs2, TCGOutOpDivRem, outop_divs2), 1092 OUTOP(INDEX_op_divu2, TCGOutOpDivRem, outop_divu2), 1093 OUTOP(INDEX_op_eqv, TCGOutOpBinary, outop_eqv), 1094 OUTOP(INDEX_op_extract, TCGOutOpExtract, outop_extract), 1095 OUTOP(INDEX_op_movcond, TCGOutOpMovcond, outop_movcond), 1096 OUTOP(INDEX_op_mul, TCGOutOpBinary, outop_mul), 1097 OUTOP(INDEX_op_muls2, TCGOutOpMul2, outop_muls2), 1098 OUTOP(INDEX_op_mulsh, TCGOutOpBinary, outop_mulsh), 1099 OUTOP(INDEX_op_mulu2, TCGOutOpMul2, outop_mulu2), 1100 OUTOP(INDEX_op_muluh, TCGOutOpBinary, outop_muluh), 1101 OUTOP(INDEX_op_nand, TCGOutOpBinary, outop_nand), 1102 OUTOP(INDEX_op_neg, TCGOutOpUnary, outop_neg), 1103 OUTOP(INDEX_op_negsetcond, TCGOutOpSetcond, outop_negsetcond), 1104 OUTOP(INDEX_op_nor, TCGOutOpBinary, outop_nor), 1105 OUTOP(INDEX_op_not, TCGOutOpUnary, outop_not), 1106 OUTOP(INDEX_op_or, TCGOutOpBinary, outop_or), 1107 OUTOP(INDEX_op_orc, TCGOutOpBinary, outop_orc), 1108 OUTOP(INDEX_op_rems, TCGOutOpBinary, outop_rems), 1109 OUTOP(INDEX_op_remu, TCGOutOpBinary, outop_remu), 1110 OUTOP(INDEX_op_rotl, TCGOutOpBinary, outop_rotl), 1111 OUTOP(INDEX_op_rotr, TCGOutOpBinary, outop_rotr), 1112 OUTOP(INDEX_op_sar, TCGOutOpBinary, outop_sar), 1113 OUTOP(INDEX_op_setcond, TCGOutOpSetcond, outop_setcond), 1114 OUTOP(INDEX_op_shl, TCGOutOpBinary, outop_shl), 1115 OUTOP(INDEX_op_shr, TCGOutOpBinary, outop_shr), 1116 OUTOP(INDEX_op_sub, TCGOutOpSubtract, outop_sub), 1117 OUTOP(INDEX_op_xor, TCGOutOpBinary, outop_xor), 1118 1119 #if TCG_TARGET_REG_BITS == 32 1120 OUTOP(INDEX_op_brcond2_i32, TCGOutOpBrcond2, outop_brcond2), 1121 OUTOP(INDEX_op_setcond2_i32, TCGOutOpSetcond2, outop_setcond2), 1122 #else 1123 OUTOP(INDEX_op_bswap64, TCGOutOpUnary, outop_bswap64), 1124 #endif 1125 }; 1126 1127 #undef OUTOP 1128 1129 /* 1130 * All TCG threads except the parent (i.e. the one that called tcg_context_init 1131 * and registered the target's TCG globals) must register with this function 1132 * before initiating translation. 1133 * 1134 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation 1135 * of tcg_region_init() for the reasoning behind this. 1136 * 1137 * In system-mode each caller registers its context in tcg_ctxs[]. Note that in 1138 * system-mode tcg_ctxs[] does not track tcg_ctx_init, since the initial context 1139 * is not used anymore for translation once this function is called. 1140 * 1141 * Not tracking tcg_init_ctx in tcg_ctxs[] in system-mode keeps code that 1142 * iterates over the array (e.g. tcg_code_size() the same for both system/user 1143 * modes. 1144 */ 1145 #ifdef CONFIG_USER_ONLY 1146 void tcg_register_thread(void) 1147 { 1148 tcg_ctx = &tcg_init_ctx; 1149 } 1150 #else 1151 void tcg_register_thread(void) 1152 { 1153 TCGContext *s = g_malloc(sizeof(*s)); 1154 unsigned int i, n; 1155 1156 *s = tcg_init_ctx; 1157 1158 /* Relink mem_base. */ 1159 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) { 1160 if (tcg_init_ctx.temps[i].mem_base) { 1161 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps; 1162 tcg_debug_assert(b >= 0 && b < n); 1163 s->temps[i].mem_base = &s->temps[b]; 1164 } 1165 } 1166 1167 /* Claim an entry in tcg_ctxs */ 1168 n = qatomic_fetch_inc(&tcg_cur_ctxs); 1169 g_assert(n < tcg_max_ctxs); 1170 qatomic_set(&tcg_ctxs[n], s); 1171 1172 if (n > 0) { 1173 tcg_region_initial_alloc(s); 1174 } 1175 1176 tcg_ctx = s; 1177 } 1178 #endif /* !CONFIG_USER_ONLY */ 1179 1180 /* pool based memory allocation */ 1181 void *tcg_malloc_internal(TCGContext *s, int size) 1182 { 1183 TCGPool *p; 1184 int pool_size; 1185 1186 if (size > TCG_POOL_CHUNK_SIZE) { 1187 /* big malloc: insert a new pool (XXX: could optimize) */ 1188 p = g_malloc(sizeof(TCGPool) + size); 1189 p->size = size; 1190 p->next = s->pool_first_large; 1191 s->pool_first_large = p; 1192 return p->data; 1193 } else { 1194 p = s->pool_current; 1195 if (!p) { 1196 p = s->pool_first; 1197 if (!p) 1198 goto new_pool; 1199 } else { 1200 if (!p->next) { 1201 new_pool: 1202 pool_size = TCG_POOL_CHUNK_SIZE; 1203 p = g_malloc(sizeof(TCGPool) + pool_size); 1204 p->size = pool_size; 1205 p->next = NULL; 1206 if (s->pool_current) { 1207 s->pool_current->next = p; 1208 } else { 1209 s->pool_first = p; 1210 } 1211 } else { 1212 p = p->next; 1213 } 1214 } 1215 } 1216 s->pool_current = p; 1217 s->pool_cur = p->data + size; 1218 s->pool_end = p->data + p->size; 1219 return p->data; 1220 } 1221 1222 void tcg_pool_reset(TCGContext *s) 1223 { 1224 TCGPool *p, *t; 1225 for (p = s->pool_first_large; p; p = t) { 1226 t = p->next; 1227 g_free(p); 1228 } 1229 s->pool_first_large = NULL; 1230 s->pool_cur = s->pool_end = NULL; 1231 s->pool_current = NULL; 1232 } 1233 1234 /* 1235 * Create TCGHelperInfo structures for "tcg/tcg-ldst.h" functions, 1236 * akin to what "exec/helper-tcg.h" does with DEF_HELPER_FLAGS_N. 1237 * We only use these for layout in tcg_out_ld_helper_ret and 1238 * tcg_out_st_helper_args, and share them between several of 1239 * the helpers, with the end result that it's easier to build manually. 1240 */ 1241 1242 #if TCG_TARGET_REG_BITS == 32 1243 # define dh_typecode_ttl dh_typecode_i32 1244 #else 1245 # define dh_typecode_ttl dh_typecode_i64 1246 #endif 1247 1248 static TCGHelperInfo info_helper_ld32_mmu = { 1249 .flags = TCG_CALL_NO_WG, 1250 .typemask = dh_typemask(ttl, 0) /* return tcg_target_ulong */ 1251 | dh_typemask(env, 1) 1252 | dh_typemask(i64, 2) /* uint64_t addr */ 1253 | dh_typemask(i32, 3) /* unsigned oi */ 1254 | dh_typemask(ptr, 4) /* uintptr_t ra */ 1255 }; 1256 1257 static TCGHelperInfo info_helper_ld64_mmu = { 1258 .flags = TCG_CALL_NO_WG, 1259 .typemask = dh_typemask(i64, 0) /* return uint64_t */ 1260 | dh_typemask(env, 1) 1261 | dh_typemask(i64, 2) /* uint64_t addr */ 1262 | dh_typemask(i32, 3) /* unsigned oi */ 1263 | dh_typemask(ptr, 4) /* uintptr_t ra */ 1264 }; 1265 1266 static TCGHelperInfo info_helper_ld128_mmu = { 1267 .flags = TCG_CALL_NO_WG, 1268 .typemask = dh_typemask(i128, 0) /* return Int128 */ 1269 | dh_typemask(env, 1) 1270 | dh_typemask(i64, 2) /* uint64_t addr */ 1271 | dh_typemask(i32, 3) /* unsigned oi */ 1272 | dh_typemask(ptr, 4) /* uintptr_t ra */ 1273 }; 1274 1275 static TCGHelperInfo info_helper_st32_mmu = { 1276 .flags = TCG_CALL_NO_WG, 1277 .typemask = dh_typemask(void, 0) 1278 | dh_typemask(env, 1) 1279 | dh_typemask(i64, 2) /* uint64_t addr */ 1280 | dh_typemask(i32, 3) /* uint32_t data */ 1281 | dh_typemask(i32, 4) /* unsigned oi */ 1282 | dh_typemask(ptr, 5) /* uintptr_t ra */ 1283 }; 1284 1285 static TCGHelperInfo info_helper_st64_mmu = { 1286 .flags = TCG_CALL_NO_WG, 1287 .typemask = dh_typemask(void, 0) 1288 | dh_typemask(env, 1) 1289 | dh_typemask(i64, 2) /* uint64_t addr */ 1290 | dh_typemask(i64, 3) /* uint64_t data */ 1291 | dh_typemask(i32, 4) /* unsigned oi */ 1292 | dh_typemask(ptr, 5) /* uintptr_t ra */ 1293 }; 1294 1295 static TCGHelperInfo info_helper_st128_mmu = { 1296 .flags = TCG_CALL_NO_WG, 1297 .typemask = dh_typemask(void, 0) 1298 | dh_typemask(env, 1) 1299 | dh_typemask(i64, 2) /* uint64_t addr */ 1300 | dh_typemask(i128, 3) /* Int128 data */ 1301 | dh_typemask(i32, 4) /* unsigned oi */ 1302 | dh_typemask(ptr, 5) /* uintptr_t ra */ 1303 }; 1304 1305 #ifdef CONFIG_TCG_INTERPRETER 1306 static ffi_type *typecode_to_ffi(int argmask) 1307 { 1308 /* 1309 * libffi does not support __int128_t, so we have forced Int128 1310 * to use the structure definition instead of the builtin type. 1311 */ 1312 static ffi_type *ffi_type_i128_elements[3] = { 1313 &ffi_type_uint64, 1314 &ffi_type_uint64, 1315 NULL 1316 }; 1317 static ffi_type ffi_type_i128 = { 1318 .size = 16, 1319 .alignment = __alignof__(Int128), 1320 .type = FFI_TYPE_STRUCT, 1321 .elements = ffi_type_i128_elements, 1322 }; 1323 1324 switch (argmask) { 1325 case dh_typecode_void: 1326 return &ffi_type_void; 1327 case dh_typecode_i32: 1328 return &ffi_type_uint32; 1329 case dh_typecode_s32: 1330 return &ffi_type_sint32; 1331 case dh_typecode_i64: 1332 return &ffi_type_uint64; 1333 case dh_typecode_s64: 1334 return &ffi_type_sint64; 1335 case dh_typecode_ptr: 1336 return &ffi_type_pointer; 1337 case dh_typecode_i128: 1338 return &ffi_type_i128; 1339 } 1340 g_assert_not_reached(); 1341 } 1342 1343 static ffi_cif *init_ffi_layout(TCGHelperInfo *info) 1344 { 1345 unsigned typemask = info->typemask; 1346 struct { 1347 ffi_cif cif; 1348 ffi_type *args[]; 1349 } *ca; 1350 ffi_status status; 1351 int nargs; 1352 1353 /* Ignoring the return type, find the last non-zero field. */ 1354 nargs = 32 - clz32(typemask >> 3); 1355 nargs = DIV_ROUND_UP(nargs, 3); 1356 assert(nargs <= MAX_CALL_IARGS); 1357 1358 ca = g_malloc0(sizeof(*ca) + nargs * sizeof(ffi_type *)); 1359 ca->cif.rtype = typecode_to_ffi(typemask & 7); 1360 ca->cif.nargs = nargs; 1361 1362 if (nargs != 0) { 1363 ca->cif.arg_types = ca->args; 1364 for (int j = 0; j < nargs; ++j) { 1365 int typecode = extract32(typemask, (j + 1) * 3, 3); 1366 ca->args[j] = typecode_to_ffi(typecode); 1367 } 1368 } 1369 1370 status = ffi_prep_cif(&ca->cif, FFI_DEFAULT_ABI, nargs, 1371 ca->cif.rtype, ca->cif.arg_types); 1372 assert(status == FFI_OK); 1373 1374 return &ca->cif; 1375 } 1376 1377 #define HELPER_INFO_INIT(I) (&(I)->cif) 1378 #define HELPER_INFO_INIT_VAL(I) init_ffi_layout(I) 1379 #else 1380 #define HELPER_INFO_INIT(I) (&(I)->init) 1381 #define HELPER_INFO_INIT_VAL(I) 1 1382 #endif /* CONFIG_TCG_INTERPRETER */ 1383 1384 static inline bool arg_slot_reg_p(unsigned arg_slot) 1385 { 1386 /* 1387 * Split the sizeof away from the comparison to avoid Werror from 1388 * "unsigned < 0 is always false", when iarg_regs is empty. 1389 */ 1390 unsigned nreg = ARRAY_SIZE(tcg_target_call_iarg_regs); 1391 return arg_slot < nreg; 1392 } 1393 1394 static inline int arg_slot_stk_ofs(unsigned arg_slot) 1395 { 1396 unsigned max = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long); 1397 unsigned stk_slot = arg_slot - ARRAY_SIZE(tcg_target_call_iarg_regs); 1398 1399 tcg_debug_assert(stk_slot < max); 1400 return TCG_TARGET_CALL_STACK_OFFSET + stk_slot * sizeof(tcg_target_long); 1401 } 1402 1403 typedef struct TCGCumulativeArgs { 1404 int arg_idx; /* tcg_gen_callN args[] */ 1405 int info_in_idx; /* TCGHelperInfo in[] */ 1406 int arg_slot; /* regs+stack slot */ 1407 int ref_slot; /* stack slots for references */ 1408 } TCGCumulativeArgs; 1409 1410 static void layout_arg_even(TCGCumulativeArgs *cum) 1411 { 1412 cum->arg_slot += cum->arg_slot & 1; 1413 } 1414 1415 static void layout_arg_1(TCGCumulativeArgs *cum, TCGHelperInfo *info, 1416 TCGCallArgumentKind kind) 1417 { 1418 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx]; 1419 1420 *loc = (TCGCallArgumentLoc){ 1421 .kind = kind, 1422 .arg_idx = cum->arg_idx, 1423 .arg_slot = cum->arg_slot, 1424 }; 1425 cum->info_in_idx++; 1426 cum->arg_slot++; 1427 } 1428 1429 static void layout_arg_normal_n(TCGCumulativeArgs *cum, 1430 TCGHelperInfo *info, int n) 1431 { 1432 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx]; 1433 1434 for (int i = 0; i < n; ++i) { 1435 /* Layout all using the same arg_idx, adjusting the subindex. */ 1436 loc[i] = (TCGCallArgumentLoc){ 1437 .kind = TCG_CALL_ARG_NORMAL, 1438 .arg_idx = cum->arg_idx, 1439 .tmp_subindex = i, 1440 .arg_slot = cum->arg_slot + i, 1441 }; 1442 } 1443 cum->info_in_idx += n; 1444 cum->arg_slot += n; 1445 } 1446 1447 static void layout_arg_by_ref(TCGCumulativeArgs *cum, TCGHelperInfo *info) 1448 { 1449 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx]; 1450 int n = 128 / TCG_TARGET_REG_BITS; 1451 1452 /* The first subindex carries the pointer. */ 1453 layout_arg_1(cum, info, TCG_CALL_ARG_BY_REF); 1454 1455 /* 1456 * The callee is allowed to clobber memory associated with 1457 * structure pass by-reference. Therefore we must make copies. 1458 * Allocate space from "ref_slot", which will be adjusted to 1459 * follow the parameters on the stack. 1460 */ 1461 loc[0].ref_slot = cum->ref_slot; 1462 1463 /* 1464 * Subsequent words also go into the reference slot, but 1465 * do not accumulate into the regular arguments. 1466 */ 1467 for (int i = 1; i < n; ++i) { 1468 loc[i] = (TCGCallArgumentLoc){ 1469 .kind = TCG_CALL_ARG_BY_REF_N, 1470 .arg_idx = cum->arg_idx, 1471 .tmp_subindex = i, 1472 .ref_slot = cum->ref_slot + i, 1473 }; 1474 } 1475 cum->info_in_idx += n - 1; /* i=0 accounted for in layout_arg_1 */ 1476 cum->ref_slot += n; 1477 } 1478 1479 static void init_call_layout(TCGHelperInfo *info) 1480 { 1481 int max_reg_slots = ARRAY_SIZE(tcg_target_call_iarg_regs); 1482 int max_stk_slots = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long); 1483 unsigned typemask = info->typemask; 1484 unsigned typecode; 1485 TCGCumulativeArgs cum = { }; 1486 1487 /* 1488 * Parse and place any function return value. 1489 */ 1490 typecode = typemask & 7; 1491 switch (typecode) { 1492 case dh_typecode_void: 1493 info->nr_out = 0; 1494 break; 1495 case dh_typecode_i32: 1496 case dh_typecode_s32: 1497 case dh_typecode_ptr: 1498 info->nr_out = 1; 1499 info->out_kind = TCG_CALL_RET_NORMAL; 1500 break; 1501 case dh_typecode_i64: 1502 case dh_typecode_s64: 1503 info->nr_out = 64 / TCG_TARGET_REG_BITS; 1504 info->out_kind = TCG_CALL_RET_NORMAL; 1505 /* Query the last register now to trigger any assert early. */ 1506 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1); 1507 break; 1508 case dh_typecode_i128: 1509 info->nr_out = 128 / TCG_TARGET_REG_BITS; 1510 info->out_kind = TCG_TARGET_CALL_RET_I128; 1511 switch (TCG_TARGET_CALL_RET_I128) { 1512 case TCG_CALL_RET_NORMAL: 1513 /* Query the last register now to trigger any assert early. */ 1514 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1); 1515 break; 1516 case TCG_CALL_RET_BY_VEC: 1517 /* Query the single register now to trigger any assert early. */ 1518 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0); 1519 break; 1520 case TCG_CALL_RET_BY_REF: 1521 /* 1522 * Allocate the first argument to the output. 1523 * We don't need to store this anywhere, just make it 1524 * unavailable for use in the input loop below. 1525 */ 1526 cum.arg_slot = 1; 1527 break; 1528 default: 1529 qemu_build_not_reached(); 1530 } 1531 break; 1532 default: 1533 g_assert_not_reached(); 1534 } 1535 1536 /* 1537 * Parse and place function arguments. 1538 */ 1539 for (typemask >>= 3; typemask; typemask >>= 3, cum.arg_idx++) { 1540 TCGCallArgumentKind kind; 1541 TCGType type; 1542 1543 typecode = typemask & 7; 1544 switch (typecode) { 1545 case dh_typecode_i32: 1546 case dh_typecode_s32: 1547 type = TCG_TYPE_I32; 1548 break; 1549 case dh_typecode_i64: 1550 case dh_typecode_s64: 1551 type = TCG_TYPE_I64; 1552 break; 1553 case dh_typecode_ptr: 1554 type = TCG_TYPE_PTR; 1555 break; 1556 case dh_typecode_i128: 1557 type = TCG_TYPE_I128; 1558 break; 1559 default: 1560 g_assert_not_reached(); 1561 } 1562 1563 switch (type) { 1564 case TCG_TYPE_I32: 1565 switch (TCG_TARGET_CALL_ARG_I32) { 1566 case TCG_CALL_ARG_EVEN: 1567 layout_arg_even(&cum); 1568 /* fall through */ 1569 case TCG_CALL_ARG_NORMAL: 1570 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL); 1571 break; 1572 case TCG_CALL_ARG_EXTEND: 1573 kind = TCG_CALL_ARG_EXTEND_U + (typecode & 1); 1574 layout_arg_1(&cum, info, kind); 1575 break; 1576 default: 1577 qemu_build_not_reached(); 1578 } 1579 break; 1580 1581 case TCG_TYPE_I64: 1582 switch (TCG_TARGET_CALL_ARG_I64) { 1583 case TCG_CALL_ARG_EVEN: 1584 layout_arg_even(&cum); 1585 /* fall through */ 1586 case TCG_CALL_ARG_NORMAL: 1587 if (TCG_TARGET_REG_BITS == 32) { 1588 layout_arg_normal_n(&cum, info, 2); 1589 } else { 1590 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL); 1591 } 1592 break; 1593 default: 1594 qemu_build_not_reached(); 1595 } 1596 break; 1597 1598 case TCG_TYPE_I128: 1599 switch (TCG_TARGET_CALL_ARG_I128) { 1600 case TCG_CALL_ARG_EVEN: 1601 layout_arg_even(&cum); 1602 /* fall through */ 1603 case TCG_CALL_ARG_NORMAL: 1604 layout_arg_normal_n(&cum, info, 128 / TCG_TARGET_REG_BITS); 1605 break; 1606 case TCG_CALL_ARG_BY_REF: 1607 layout_arg_by_ref(&cum, info); 1608 break; 1609 default: 1610 qemu_build_not_reached(); 1611 } 1612 break; 1613 1614 default: 1615 g_assert_not_reached(); 1616 } 1617 } 1618 info->nr_in = cum.info_in_idx; 1619 1620 /* Validate that we didn't overrun the input array. */ 1621 assert(cum.info_in_idx <= ARRAY_SIZE(info->in)); 1622 /* Validate the backend has enough argument space. */ 1623 assert(cum.arg_slot <= max_reg_slots + max_stk_slots); 1624 1625 /* 1626 * Relocate the "ref_slot" area to the end of the parameters. 1627 * Minimizing this stack offset helps code size for x86, 1628 * which has a signed 8-bit offset encoding. 1629 */ 1630 if (cum.ref_slot != 0) { 1631 int ref_base = 0; 1632 1633 if (cum.arg_slot > max_reg_slots) { 1634 int align = __alignof(Int128) / sizeof(tcg_target_long); 1635 1636 ref_base = cum.arg_slot - max_reg_slots; 1637 if (align > 1) { 1638 ref_base = ROUND_UP(ref_base, align); 1639 } 1640 } 1641 assert(ref_base + cum.ref_slot <= max_stk_slots); 1642 ref_base += max_reg_slots; 1643 1644 if (ref_base != 0) { 1645 for (int i = cum.info_in_idx - 1; i >= 0; --i) { 1646 TCGCallArgumentLoc *loc = &info->in[i]; 1647 switch (loc->kind) { 1648 case TCG_CALL_ARG_BY_REF: 1649 case TCG_CALL_ARG_BY_REF_N: 1650 loc->ref_slot += ref_base; 1651 break; 1652 default: 1653 break; 1654 } 1655 } 1656 } 1657 } 1658 } 1659 1660 static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)]; 1661 static void process_constraint_sets(void); 1662 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, 1663 TCGReg reg, const char *name); 1664 1665 static void tcg_context_init(unsigned max_threads) 1666 { 1667 TCGContext *s = &tcg_init_ctx; 1668 int n, i; 1669 TCGTemp *ts; 1670 1671 memset(s, 0, sizeof(*s)); 1672 s->nb_globals = 0; 1673 1674 init_call_layout(&info_helper_ld32_mmu); 1675 init_call_layout(&info_helper_ld64_mmu); 1676 init_call_layout(&info_helper_ld128_mmu); 1677 init_call_layout(&info_helper_st32_mmu); 1678 init_call_layout(&info_helper_st64_mmu); 1679 init_call_layout(&info_helper_st128_mmu); 1680 1681 tcg_target_init(s); 1682 process_constraint_sets(); 1683 1684 /* Reverse the order of the saved registers, assuming they're all at 1685 the start of tcg_target_reg_alloc_order. */ 1686 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) { 1687 int r = tcg_target_reg_alloc_order[n]; 1688 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) { 1689 break; 1690 } 1691 } 1692 for (i = 0; i < n; ++i) { 1693 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i]; 1694 } 1695 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) { 1696 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i]; 1697 } 1698 1699 tcg_ctx = s; 1700 /* 1701 * In user-mode we simply share the init context among threads, since we 1702 * use a single region. See the documentation tcg_region_init() for the 1703 * reasoning behind this. 1704 * In system-mode we will have at most max_threads TCG threads. 1705 */ 1706 #ifdef CONFIG_USER_ONLY 1707 tcg_ctxs = &tcg_ctx; 1708 tcg_cur_ctxs = 1; 1709 tcg_max_ctxs = 1; 1710 #else 1711 tcg_max_ctxs = max_threads; 1712 tcg_ctxs = g_new0(TCGContext *, max_threads); 1713 #endif 1714 1715 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0)); 1716 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env"); 1717 tcg_env = temp_tcgv_ptr(ts); 1718 } 1719 1720 void tcg_init(size_t tb_size, int splitwx, unsigned max_threads) 1721 { 1722 tcg_context_init(max_threads); 1723 tcg_region_init(tb_size, splitwx, max_threads); 1724 } 1725 1726 /* 1727 * Allocate TBs right before their corresponding translated code, making 1728 * sure that TBs and code are on different cache lines. 1729 */ 1730 TranslationBlock *tcg_tb_alloc(TCGContext *s) 1731 { 1732 uintptr_t align = qemu_icache_linesize; 1733 TranslationBlock *tb; 1734 void *next; 1735 1736 retry: 1737 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align); 1738 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align); 1739 1740 if (unlikely(next > s->code_gen_highwater)) { 1741 if (tcg_region_alloc(s)) { 1742 return NULL; 1743 } 1744 goto retry; 1745 } 1746 qatomic_set(&s->code_gen_ptr, next); 1747 return tb; 1748 } 1749 1750 void tcg_prologue_init(void) 1751 { 1752 TCGContext *s = tcg_ctx; 1753 size_t prologue_size; 1754 1755 s->code_ptr = s->code_gen_ptr; 1756 s->code_buf = s->code_gen_ptr; 1757 s->data_gen_ptr = NULL; 1758 1759 #ifndef CONFIG_TCG_INTERPRETER 1760 tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr); 1761 #endif 1762 1763 s->pool_labels = NULL; 1764 1765 qemu_thread_jit_write(); 1766 /* Generate the prologue. */ 1767 tcg_target_qemu_prologue(s); 1768 1769 /* Allow the prologue to put e.g. guest_base into a pool entry. */ 1770 { 1771 int result = tcg_out_pool_finalize(s); 1772 tcg_debug_assert(result == 0); 1773 } 1774 1775 prologue_size = tcg_current_code_size(s); 1776 perf_report_prologue(s->code_gen_ptr, prologue_size); 1777 1778 #ifndef CONFIG_TCG_INTERPRETER 1779 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf), 1780 (uintptr_t)s->code_buf, prologue_size); 1781 #endif 1782 1783 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { 1784 FILE *logfile = qemu_log_trylock(); 1785 if (logfile) { 1786 fprintf(logfile, "PROLOGUE: [size=%zu]\n", prologue_size); 1787 if (s->data_gen_ptr) { 1788 size_t code_size = s->data_gen_ptr - s->code_gen_ptr; 1789 size_t data_size = prologue_size - code_size; 1790 size_t i; 1791 1792 disas(logfile, s->code_gen_ptr, code_size); 1793 1794 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) { 1795 if (sizeof(tcg_target_ulong) == 8) { 1796 fprintf(logfile, 1797 "0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n", 1798 (uintptr_t)s->data_gen_ptr + i, 1799 *(uint64_t *)(s->data_gen_ptr + i)); 1800 } else { 1801 fprintf(logfile, 1802 "0x%08" PRIxPTR ": .long 0x%08x\n", 1803 (uintptr_t)s->data_gen_ptr + i, 1804 *(uint32_t *)(s->data_gen_ptr + i)); 1805 } 1806 } 1807 } else { 1808 disas(logfile, s->code_gen_ptr, prologue_size); 1809 } 1810 fprintf(logfile, "\n"); 1811 qemu_log_unlock(logfile); 1812 } 1813 } 1814 1815 #ifndef CONFIG_TCG_INTERPRETER 1816 /* 1817 * Assert that goto_ptr is implemented completely, setting an epilogue. 1818 * For tci, we use NULL as the signal to return from the interpreter, 1819 * so skip this check. 1820 */ 1821 tcg_debug_assert(tcg_code_gen_epilogue != NULL); 1822 #endif 1823 1824 tcg_region_prologue_set(s); 1825 } 1826 1827 void tcg_func_start(TCGContext *s) 1828 { 1829 tcg_pool_reset(s); 1830 s->nb_temps = s->nb_globals; 1831 1832 /* No temps have been previously allocated for size or locality. */ 1833 tcg_temp_ebb_reset_freed(s); 1834 1835 /* No constant temps have been previously allocated. */ 1836 for (int i = 0; i < TCG_TYPE_COUNT; ++i) { 1837 if (s->const_table[i]) { 1838 g_hash_table_remove_all(s->const_table[i]); 1839 } 1840 } 1841 1842 s->nb_ops = 0; 1843 s->nb_labels = 0; 1844 s->current_frame_offset = s->frame_start; 1845 1846 #ifdef CONFIG_DEBUG_TCG 1847 s->goto_tb_issue_mask = 0; 1848 #endif 1849 1850 QTAILQ_INIT(&s->ops); 1851 QTAILQ_INIT(&s->free_ops); 1852 s->emit_before_op = NULL; 1853 QSIMPLEQ_INIT(&s->labels); 1854 1855 tcg_debug_assert(s->addr_type <= TCG_TYPE_REG); 1856 tcg_debug_assert(s->insn_start_words > 0); 1857 } 1858 1859 static TCGTemp *tcg_temp_alloc(TCGContext *s) 1860 { 1861 int n = s->nb_temps++; 1862 1863 if (n >= TCG_MAX_TEMPS) { 1864 tcg_raise_tb_overflow(s); 1865 } 1866 return memset(&s->temps[n], 0, sizeof(TCGTemp)); 1867 } 1868 1869 static TCGTemp *tcg_global_alloc(TCGContext *s) 1870 { 1871 TCGTemp *ts; 1872 1873 tcg_debug_assert(s->nb_globals == s->nb_temps); 1874 tcg_debug_assert(s->nb_globals < TCG_MAX_TEMPS); 1875 s->nb_globals++; 1876 ts = tcg_temp_alloc(s); 1877 ts->kind = TEMP_GLOBAL; 1878 1879 return ts; 1880 } 1881 1882 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type, 1883 TCGReg reg, const char *name) 1884 { 1885 TCGTemp *ts; 1886 1887 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32); 1888 1889 ts = tcg_global_alloc(s); 1890 ts->base_type = type; 1891 ts->type = type; 1892 ts->kind = TEMP_FIXED; 1893 ts->reg = reg; 1894 ts->name = name; 1895 tcg_regset_set_reg(s->reserved_regs, reg); 1896 1897 return ts; 1898 } 1899 1900 void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size) 1901 { 1902 s->frame_start = start; 1903 s->frame_end = start + size; 1904 s->frame_temp 1905 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame"); 1906 } 1907 1908 static TCGTemp *tcg_global_mem_new_internal(TCGv_ptr base, intptr_t offset, 1909 const char *name, TCGType type) 1910 { 1911 TCGContext *s = tcg_ctx; 1912 TCGTemp *base_ts = tcgv_ptr_temp(base); 1913 TCGTemp *ts = tcg_global_alloc(s); 1914 int indirect_reg = 0; 1915 1916 switch (base_ts->kind) { 1917 case TEMP_FIXED: 1918 break; 1919 case TEMP_GLOBAL: 1920 /* We do not support double-indirect registers. */ 1921 tcg_debug_assert(!base_ts->indirect_reg); 1922 base_ts->indirect_base = 1; 1923 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64 1924 ? 2 : 1); 1925 indirect_reg = 1; 1926 break; 1927 default: 1928 g_assert_not_reached(); 1929 } 1930 1931 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) { 1932 TCGTemp *ts2 = tcg_global_alloc(s); 1933 char buf[64]; 1934 1935 ts->base_type = TCG_TYPE_I64; 1936 ts->type = TCG_TYPE_I32; 1937 ts->indirect_reg = indirect_reg; 1938 ts->mem_allocated = 1; 1939 ts->mem_base = base_ts; 1940 ts->mem_offset = offset; 1941 pstrcpy(buf, sizeof(buf), name); 1942 pstrcat(buf, sizeof(buf), "_0"); 1943 ts->name = strdup(buf); 1944 1945 tcg_debug_assert(ts2 == ts + 1); 1946 ts2->base_type = TCG_TYPE_I64; 1947 ts2->type = TCG_TYPE_I32; 1948 ts2->indirect_reg = indirect_reg; 1949 ts2->mem_allocated = 1; 1950 ts2->mem_base = base_ts; 1951 ts2->mem_offset = offset + 4; 1952 ts2->temp_subindex = 1; 1953 pstrcpy(buf, sizeof(buf), name); 1954 pstrcat(buf, sizeof(buf), "_1"); 1955 ts2->name = strdup(buf); 1956 } else { 1957 ts->base_type = type; 1958 ts->type = type; 1959 ts->indirect_reg = indirect_reg; 1960 ts->mem_allocated = 1; 1961 ts->mem_base = base_ts; 1962 ts->mem_offset = offset; 1963 ts->name = name; 1964 } 1965 return ts; 1966 } 1967 1968 TCGv_i32 tcg_global_mem_new_i32(TCGv_ptr reg, intptr_t off, const char *name) 1969 { 1970 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I32); 1971 return temp_tcgv_i32(ts); 1972 } 1973 1974 TCGv_i64 tcg_global_mem_new_i64(TCGv_ptr reg, intptr_t off, const char *name) 1975 { 1976 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I64); 1977 return temp_tcgv_i64(ts); 1978 } 1979 1980 TCGv_ptr tcg_global_mem_new_ptr(TCGv_ptr reg, intptr_t off, const char *name) 1981 { 1982 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_PTR); 1983 return temp_tcgv_ptr(ts); 1984 } 1985 1986 TCGTemp *tcg_temp_new_internal(TCGType type, TCGTempKind kind) 1987 { 1988 TCGContext *s = tcg_ctx; 1989 TCGTemp *ts; 1990 int n; 1991 1992 if (kind == TEMP_EBB) { 1993 int idx = find_first_bit(s->free_temps[type].l, TCG_MAX_TEMPS); 1994 1995 if (idx < TCG_MAX_TEMPS) { 1996 /* There is already an available temp with the right type. */ 1997 clear_bit(idx, s->free_temps[type].l); 1998 1999 ts = &s->temps[idx]; 2000 ts->temp_allocated = 1; 2001 tcg_debug_assert(ts->base_type == type); 2002 tcg_debug_assert(ts->kind == kind); 2003 return ts; 2004 } 2005 } else { 2006 tcg_debug_assert(kind == TEMP_TB); 2007 } 2008 2009 switch (type) { 2010 case TCG_TYPE_I32: 2011 case TCG_TYPE_V64: 2012 case TCG_TYPE_V128: 2013 case TCG_TYPE_V256: 2014 n = 1; 2015 break; 2016 case TCG_TYPE_I64: 2017 n = 64 / TCG_TARGET_REG_BITS; 2018 break; 2019 case TCG_TYPE_I128: 2020 n = 128 / TCG_TARGET_REG_BITS; 2021 break; 2022 default: 2023 g_assert_not_reached(); 2024 } 2025 2026 ts = tcg_temp_alloc(s); 2027 ts->base_type = type; 2028 ts->temp_allocated = 1; 2029 ts->kind = kind; 2030 2031 if (n == 1) { 2032 ts->type = type; 2033 } else { 2034 ts->type = TCG_TYPE_REG; 2035 2036 for (int i = 1; i < n; ++i) { 2037 TCGTemp *ts2 = tcg_temp_alloc(s); 2038 2039 tcg_debug_assert(ts2 == ts + i); 2040 ts2->base_type = type; 2041 ts2->type = TCG_TYPE_REG; 2042 ts2->temp_allocated = 1; 2043 ts2->temp_subindex = i; 2044 ts2->kind = kind; 2045 } 2046 } 2047 return ts; 2048 } 2049 2050 TCGv_i32 tcg_temp_new_i32(void) 2051 { 2052 return temp_tcgv_i32(tcg_temp_new_internal(TCG_TYPE_I32, TEMP_TB)); 2053 } 2054 2055 TCGv_i32 tcg_temp_ebb_new_i32(void) 2056 { 2057 return temp_tcgv_i32(tcg_temp_new_internal(TCG_TYPE_I32, TEMP_EBB)); 2058 } 2059 2060 TCGv_i64 tcg_temp_new_i64(void) 2061 { 2062 return temp_tcgv_i64(tcg_temp_new_internal(TCG_TYPE_I64, TEMP_TB)); 2063 } 2064 2065 TCGv_i64 tcg_temp_ebb_new_i64(void) 2066 { 2067 return temp_tcgv_i64(tcg_temp_new_internal(TCG_TYPE_I64, TEMP_EBB)); 2068 } 2069 2070 TCGv_ptr tcg_temp_new_ptr(void) 2071 { 2072 return temp_tcgv_ptr(tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_TB)); 2073 } 2074 2075 TCGv_ptr tcg_temp_ebb_new_ptr(void) 2076 { 2077 return temp_tcgv_ptr(tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_EBB)); 2078 } 2079 2080 TCGv_i128 tcg_temp_new_i128(void) 2081 { 2082 return temp_tcgv_i128(tcg_temp_new_internal(TCG_TYPE_I128, TEMP_TB)); 2083 } 2084 2085 TCGv_i128 tcg_temp_ebb_new_i128(void) 2086 { 2087 return temp_tcgv_i128(tcg_temp_new_internal(TCG_TYPE_I128, TEMP_EBB)); 2088 } 2089 2090 TCGv_vec tcg_temp_new_vec(TCGType type) 2091 { 2092 TCGTemp *t; 2093 2094 #ifdef CONFIG_DEBUG_TCG 2095 switch (type) { 2096 case TCG_TYPE_V64: 2097 assert(TCG_TARGET_HAS_v64); 2098 break; 2099 case TCG_TYPE_V128: 2100 assert(TCG_TARGET_HAS_v128); 2101 break; 2102 case TCG_TYPE_V256: 2103 assert(TCG_TARGET_HAS_v256); 2104 break; 2105 default: 2106 g_assert_not_reached(); 2107 } 2108 #endif 2109 2110 t = tcg_temp_new_internal(type, TEMP_EBB); 2111 return temp_tcgv_vec(t); 2112 } 2113 2114 /* Create a new temp of the same type as an existing temp. */ 2115 TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match) 2116 { 2117 TCGTemp *t = tcgv_vec_temp(match); 2118 2119 tcg_debug_assert(t->temp_allocated != 0); 2120 2121 t = tcg_temp_new_internal(t->base_type, TEMP_EBB); 2122 return temp_tcgv_vec(t); 2123 } 2124 2125 void tcg_temp_free_internal(TCGTemp *ts) 2126 { 2127 TCGContext *s = tcg_ctx; 2128 2129 switch (ts->kind) { 2130 case TEMP_CONST: 2131 case TEMP_TB: 2132 /* Silently ignore free. */ 2133 break; 2134 case TEMP_EBB: 2135 tcg_debug_assert(ts->temp_allocated != 0); 2136 ts->temp_allocated = 0; 2137 set_bit(temp_idx(ts), s->free_temps[ts->base_type].l); 2138 break; 2139 default: 2140 /* It never made sense to free TEMP_FIXED or TEMP_GLOBAL. */ 2141 g_assert_not_reached(); 2142 } 2143 } 2144 2145 void tcg_temp_free_i32(TCGv_i32 arg) 2146 { 2147 tcg_temp_free_internal(tcgv_i32_temp(arg)); 2148 } 2149 2150 void tcg_temp_free_i64(TCGv_i64 arg) 2151 { 2152 tcg_temp_free_internal(tcgv_i64_temp(arg)); 2153 } 2154 2155 void tcg_temp_free_i128(TCGv_i128 arg) 2156 { 2157 tcg_temp_free_internal(tcgv_i128_temp(arg)); 2158 } 2159 2160 void tcg_temp_free_ptr(TCGv_ptr arg) 2161 { 2162 tcg_temp_free_internal(tcgv_ptr_temp(arg)); 2163 } 2164 2165 void tcg_temp_free_vec(TCGv_vec arg) 2166 { 2167 tcg_temp_free_internal(tcgv_vec_temp(arg)); 2168 } 2169 2170 TCGTemp *tcg_constant_internal(TCGType type, int64_t val) 2171 { 2172 TCGContext *s = tcg_ctx; 2173 GHashTable *h = s->const_table[type]; 2174 TCGTemp *ts; 2175 2176 if (h == NULL) { 2177 h = g_hash_table_new(g_int64_hash, g_int64_equal); 2178 s->const_table[type] = h; 2179 } 2180 2181 ts = g_hash_table_lookup(h, &val); 2182 if (ts == NULL) { 2183 int64_t *val_ptr; 2184 2185 ts = tcg_temp_alloc(s); 2186 2187 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) { 2188 TCGTemp *ts2 = tcg_temp_alloc(s); 2189 2190 tcg_debug_assert(ts2 == ts + 1); 2191 2192 ts->base_type = TCG_TYPE_I64; 2193 ts->type = TCG_TYPE_I32; 2194 ts->kind = TEMP_CONST; 2195 ts->temp_allocated = 1; 2196 2197 ts2->base_type = TCG_TYPE_I64; 2198 ts2->type = TCG_TYPE_I32; 2199 ts2->kind = TEMP_CONST; 2200 ts2->temp_allocated = 1; 2201 ts2->temp_subindex = 1; 2202 2203 /* 2204 * Retain the full value of the 64-bit constant in the low 2205 * part, so that the hash table works. Actual uses will 2206 * truncate the value to the low part. 2207 */ 2208 ts[HOST_BIG_ENDIAN].val = val; 2209 ts[!HOST_BIG_ENDIAN].val = val >> 32; 2210 val_ptr = &ts[HOST_BIG_ENDIAN].val; 2211 } else { 2212 ts->base_type = type; 2213 ts->type = type; 2214 ts->kind = TEMP_CONST; 2215 ts->temp_allocated = 1; 2216 ts->val = val; 2217 val_ptr = &ts->val; 2218 } 2219 g_hash_table_insert(h, val_ptr, ts); 2220 } 2221 2222 return ts; 2223 } 2224 2225 TCGv_i32 tcg_constant_i32(int32_t val) 2226 { 2227 return temp_tcgv_i32(tcg_constant_internal(TCG_TYPE_I32, val)); 2228 } 2229 2230 TCGv_i64 tcg_constant_i64(int64_t val) 2231 { 2232 return temp_tcgv_i64(tcg_constant_internal(TCG_TYPE_I64, val)); 2233 } 2234 2235 TCGv_ptr tcg_constant_ptr_int(intptr_t val) 2236 { 2237 return temp_tcgv_ptr(tcg_constant_internal(TCG_TYPE_PTR, val)); 2238 } 2239 2240 TCGv_vec tcg_constant_vec(TCGType type, unsigned vece, int64_t val) 2241 { 2242 val = dup_const(vece, val); 2243 return temp_tcgv_vec(tcg_constant_internal(type, val)); 2244 } 2245 2246 TCGv_vec tcg_constant_vec_matching(TCGv_vec match, unsigned vece, int64_t val) 2247 { 2248 TCGTemp *t = tcgv_vec_temp(match); 2249 2250 tcg_debug_assert(t->temp_allocated != 0); 2251 return tcg_constant_vec(t->base_type, vece, val); 2252 } 2253 2254 #ifdef CONFIG_DEBUG_TCG 2255 size_t temp_idx(TCGTemp *ts) 2256 { 2257 ptrdiff_t n = ts - tcg_ctx->temps; 2258 assert(n >= 0 && n < tcg_ctx->nb_temps); 2259 return n; 2260 } 2261 2262 TCGTemp *tcgv_i32_temp(TCGv_i32 v) 2263 { 2264 uintptr_t o = (uintptr_t)v - offsetof(TCGContext, temps); 2265 2266 assert(o < sizeof(TCGTemp) * tcg_ctx->nb_temps); 2267 assert(o % sizeof(TCGTemp) == 0); 2268 2269 return (void *)tcg_ctx + (uintptr_t)v; 2270 } 2271 #endif /* CONFIG_DEBUG_TCG */ 2272 2273 /* 2274 * Return true if OP may appear in the opcode stream with TYPE. 2275 * Test the runtime variable that controls each opcode. 2276 */ 2277 bool tcg_op_supported(TCGOpcode op, TCGType type, unsigned flags) 2278 { 2279 bool has_type; 2280 2281 switch (type) { 2282 case TCG_TYPE_I32: 2283 has_type = true; 2284 break; 2285 case TCG_TYPE_I64: 2286 has_type = TCG_TARGET_REG_BITS == 64; 2287 break; 2288 case TCG_TYPE_V64: 2289 has_type = TCG_TARGET_HAS_v64; 2290 break; 2291 case TCG_TYPE_V128: 2292 has_type = TCG_TARGET_HAS_v128; 2293 break; 2294 case TCG_TYPE_V256: 2295 has_type = TCG_TARGET_HAS_v256; 2296 break; 2297 default: 2298 has_type = false; 2299 break; 2300 } 2301 2302 switch (op) { 2303 case INDEX_op_discard: 2304 case INDEX_op_set_label: 2305 case INDEX_op_call: 2306 case INDEX_op_br: 2307 case INDEX_op_mb: 2308 case INDEX_op_insn_start: 2309 case INDEX_op_exit_tb: 2310 case INDEX_op_goto_tb: 2311 case INDEX_op_goto_ptr: 2312 case INDEX_op_qemu_ld_i32: 2313 case INDEX_op_qemu_st_i32: 2314 case INDEX_op_qemu_ld_i64: 2315 case INDEX_op_qemu_st_i64: 2316 return true; 2317 2318 case INDEX_op_qemu_st8_i32: 2319 return TCG_TARGET_HAS_qemu_st8_i32; 2320 2321 case INDEX_op_qemu_ld_i128: 2322 case INDEX_op_qemu_st_i128: 2323 return TCG_TARGET_HAS_qemu_ldst_i128; 2324 2325 case INDEX_op_add: 2326 case INDEX_op_and: 2327 case INDEX_op_brcond: 2328 case INDEX_op_extract: 2329 case INDEX_op_mov: 2330 case INDEX_op_movcond: 2331 case INDEX_op_negsetcond: 2332 case INDEX_op_or: 2333 case INDEX_op_setcond: 2334 case INDEX_op_xor: 2335 return has_type; 2336 2337 case INDEX_op_ld8u_i32: 2338 case INDEX_op_ld8s_i32: 2339 case INDEX_op_ld16u_i32: 2340 case INDEX_op_ld16s_i32: 2341 case INDEX_op_ld_i32: 2342 case INDEX_op_st8_i32: 2343 case INDEX_op_st16_i32: 2344 case INDEX_op_st_i32: 2345 case INDEX_op_sextract_i32: 2346 case INDEX_op_deposit_i32: 2347 return true; 2348 2349 case INDEX_op_extract2_i32: 2350 return TCG_TARGET_HAS_extract2_i32; 2351 case INDEX_op_add2_i32: 2352 return TCG_TARGET_HAS_add2_i32; 2353 case INDEX_op_sub2_i32: 2354 return TCG_TARGET_HAS_sub2_i32; 2355 2356 case INDEX_op_brcond2_i32: 2357 case INDEX_op_setcond2_i32: 2358 return TCG_TARGET_REG_BITS == 32; 2359 2360 case INDEX_op_ld8u_i64: 2361 case INDEX_op_ld8s_i64: 2362 case INDEX_op_ld16u_i64: 2363 case INDEX_op_ld16s_i64: 2364 case INDEX_op_ld32u_i64: 2365 case INDEX_op_ld32s_i64: 2366 case INDEX_op_ld_i64: 2367 case INDEX_op_st8_i64: 2368 case INDEX_op_st16_i64: 2369 case INDEX_op_st32_i64: 2370 case INDEX_op_st_i64: 2371 case INDEX_op_ext_i32_i64: 2372 case INDEX_op_extu_i32_i64: 2373 case INDEX_op_sextract_i64: 2374 case INDEX_op_deposit_i64: 2375 return TCG_TARGET_REG_BITS == 64; 2376 2377 case INDEX_op_extract2_i64: 2378 return TCG_TARGET_HAS_extract2_i64; 2379 case INDEX_op_extrl_i64_i32: 2380 case INDEX_op_extrh_i64_i32: 2381 return TCG_TARGET_HAS_extr_i64_i32; 2382 case INDEX_op_add2_i64: 2383 return TCG_TARGET_HAS_add2_i64; 2384 case INDEX_op_sub2_i64: 2385 return TCG_TARGET_HAS_sub2_i64; 2386 2387 case INDEX_op_mov_vec: 2388 case INDEX_op_dup_vec: 2389 case INDEX_op_dupm_vec: 2390 case INDEX_op_ld_vec: 2391 case INDEX_op_st_vec: 2392 case INDEX_op_add_vec: 2393 case INDEX_op_sub_vec: 2394 case INDEX_op_and_vec: 2395 case INDEX_op_or_vec: 2396 case INDEX_op_xor_vec: 2397 case INDEX_op_cmp_vec: 2398 return has_type; 2399 case INDEX_op_dup2_vec: 2400 return has_type && TCG_TARGET_REG_BITS == 32; 2401 case INDEX_op_not_vec: 2402 return has_type && TCG_TARGET_HAS_not_vec; 2403 case INDEX_op_neg_vec: 2404 return has_type && TCG_TARGET_HAS_neg_vec; 2405 case INDEX_op_abs_vec: 2406 return has_type && TCG_TARGET_HAS_abs_vec; 2407 case INDEX_op_andc_vec: 2408 return has_type && TCG_TARGET_HAS_andc_vec; 2409 case INDEX_op_orc_vec: 2410 return has_type && TCG_TARGET_HAS_orc_vec; 2411 case INDEX_op_nand_vec: 2412 return has_type && TCG_TARGET_HAS_nand_vec; 2413 case INDEX_op_nor_vec: 2414 return has_type && TCG_TARGET_HAS_nor_vec; 2415 case INDEX_op_eqv_vec: 2416 return has_type && TCG_TARGET_HAS_eqv_vec; 2417 case INDEX_op_mul_vec: 2418 return has_type && TCG_TARGET_HAS_mul_vec; 2419 case INDEX_op_shli_vec: 2420 case INDEX_op_shri_vec: 2421 case INDEX_op_sari_vec: 2422 return has_type && TCG_TARGET_HAS_shi_vec; 2423 case INDEX_op_shls_vec: 2424 case INDEX_op_shrs_vec: 2425 case INDEX_op_sars_vec: 2426 return has_type && TCG_TARGET_HAS_shs_vec; 2427 case INDEX_op_shlv_vec: 2428 case INDEX_op_shrv_vec: 2429 case INDEX_op_sarv_vec: 2430 return has_type && TCG_TARGET_HAS_shv_vec; 2431 case INDEX_op_rotli_vec: 2432 return has_type && TCG_TARGET_HAS_roti_vec; 2433 case INDEX_op_rotls_vec: 2434 return has_type && TCG_TARGET_HAS_rots_vec; 2435 case INDEX_op_rotlv_vec: 2436 case INDEX_op_rotrv_vec: 2437 return has_type && TCG_TARGET_HAS_rotv_vec; 2438 case INDEX_op_ssadd_vec: 2439 case INDEX_op_usadd_vec: 2440 case INDEX_op_sssub_vec: 2441 case INDEX_op_ussub_vec: 2442 return has_type && TCG_TARGET_HAS_sat_vec; 2443 case INDEX_op_smin_vec: 2444 case INDEX_op_umin_vec: 2445 case INDEX_op_smax_vec: 2446 case INDEX_op_umax_vec: 2447 return has_type && TCG_TARGET_HAS_minmax_vec; 2448 case INDEX_op_bitsel_vec: 2449 return has_type && TCG_TARGET_HAS_bitsel_vec; 2450 case INDEX_op_cmpsel_vec: 2451 return has_type && TCG_TARGET_HAS_cmpsel_vec; 2452 2453 default: 2454 if (op < INDEX_op_last_generic) { 2455 const TCGOutOp *outop; 2456 TCGConstraintSetIndex con_set; 2457 2458 if (!has_type) { 2459 return false; 2460 } 2461 2462 outop = all_outop[op]; 2463 tcg_debug_assert(outop != NULL); 2464 2465 con_set = outop->static_constraint; 2466 if (con_set == C_Dynamic) { 2467 con_set = outop->dynamic_constraint(type, flags); 2468 } 2469 if (con_set >= 0) { 2470 return true; 2471 } 2472 tcg_debug_assert(con_set == C_NotImplemented); 2473 return false; 2474 } 2475 tcg_debug_assert(op < NB_OPS); 2476 return true; 2477 2478 case INDEX_op_last_generic: 2479 g_assert_not_reached(); 2480 } 2481 } 2482 2483 bool tcg_op_deposit_valid(TCGType type, unsigned ofs, unsigned len) 2484 { 2485 unsigned width; 2486 2487 tcg_debug_assert(type == TCG_TYPE_I32 || type == TCG_TYPE_I64); 2488 width = (type == TCG_TYPE_I32 ? 32 : 64); 2489 2490 tcg_debug_assert(ofs < width); 2491 tcg_debug_assert(len > 0); 2492 tcg_debug_assert(len <= width - ofs); 2493 2494 return TCG_TARGET_deposit_valid(type, ofs, len); 2495 } 2496 2497 static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs); 2498 2499 static void tcg_gen_callN(void *func, TCGHelperInfo *info, 2500 TCGTemp *ret, TCGTemp **args) 2501 { 2502 TCGv_i64 extend_free[MAX_CALL_IARGS]; 2503 int n_extend = 0; 2504 TCGOp *op; 2505 int i, n, pi = 0, total_args; 2506 2507 if (unlikely(g_once_init_enter(HELPER_INFO_INIT(info)))) { 2508 init_call_layout(info); 2509 g_once_init_leave(HELPER_INFO_INIT(info), HELPER_INFO_INIT_VAL(info)); 2510 } 2511 2512 total_args = info->nr_out + info->nr_in + 2; 2513 op = tcg_op_alloc(INDEX_op_call, total_args); 2514 2515 #ifdef CONFIG_PLUGIN 2516 /* Flag helpers that may affect guest state */ 2517 if (tcg_ctx->plugin_insn && !(info->flags & TCG_CALL_NO_SIDE_EFFECTS)) { 2518 tcg_ctx->plugin_insn->calls_helpers = true; 2519 } 2520 #endif 2521 2522 TCGOP_CALLO(op) = n = info->nr_out; 2523 switch (n) { 2524 case 0: 2525 tcg_debug_assert(ret == NULL); 2526 break; 2527 case 1: 2528 tcg_debug_assert(ret != NULL); 2529 op->args[pi++] = temp_arg(ret); 2530 break; 2531 case 2: 2532 case 4: 2533 tcg_debug_assert(ret != NULL); 2534 tcg_debug_assert(ret->base_type == ret->type + ctz32(n)); 2535 tcg_debug_assert(ret->temp_subindex == 0); 2536 for (i = 0; i < n; ++i) { 2537 op->args[pi++] = temp_arg(ret + i); 2538 } 2539 break; 2540 default: 2541 g_assert_not_reached(); 2542 } 2543 2544 TCGOP_CALLI(op) = n = info->nr_in; 2545 for (i = 0; i < n; i++) { 2546 const TCGCallArgumentLoc *loc = &info->in[i]; 2547 TCGTemp *ts = args[loc->arg_idx] + loc->tmp_subindex; 2548 2549 switch (loc->kind) { 2550 case TCG_CALL_ARG_NORMAL: 2551 case TCG_CALL_ARG_BY_REF: 2552 case TCG_CALL_ARG_BY_REF_N: 2553 op->args[pi++] = temp_arg(ts); 2554 break; 2555 2556 case TCG_CALL_ARG_EXTEND_U: 2557 case TCG_CALL_ARG_EXTEND_S: 2558 { 2559 TCGv_i64 temp = tcg_temp_ebb_new_i64(); 2560 TCGv_i32 orig = temp_tcgv_i32(ts); 2561 2562 if (loc->kind == TCG_CALL_ARG_EXTEND_S) { 2563 tcg_gen_ext_i32_i64(temp, orig); 2564 } else { 2565 tcg_gen_extu_i32_i64(temp, orig); 2566 } 2567 op->args[pi++] = tcgv_i64_arg(temp); 2568 extend_free[n_extend++] = temp; 2569 } 2570 break; 2571 2572 default: 2573 g_assert_not_reached(); 2574 } 2575 } 2576 op->args[pi++] = (uintptr_t)func; 2577 op->args[pi++] = (uintptr_t)info; 2578 tcg_debug_assert(pi == total_args); 2579 2580 if (tcg_ctx->emit_before_op) { 2581 QTAILQ_INSERT_BEFORE(tcg_ctx->emit_before_op, op, link); 2582 } else { 2583 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link); 2584 } 2585 2586 tcg_debug_assert(n_extend < ARRAY_SIZE(extend_free)); 2587 for (i = 0; i < n_extend; ++i) { 2588 tcg_temp_free_i64(extend_free[i]); 2589 } 2590 } 2591 2592 void tcg_gen_call0(void *func, TCGHelperInfo *info, TCGTemp *ret) 2593 { 2594 tcg_gen_callN(func, info, ret, NULL); 2595 } 2596 2597 void tcg_gen_call1(void *func, TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1) 2598 { 2599 tcg_gen_callN(func, info, ret, &t1); 2600 } 2601 2602 void tcg_gen_call2(void *func, TCGHelperInfo *info, TCGTemp *ret, 2603 TCGTemp *t1, TCGTemp *t2) 2604 { 2605 TCGTemp *args[2] = { t1, t2 }; 2606 tcg_gen_callN(func, info, ret, args); 2607 } 2608 2609 void tcg_gen_call3(void *func, TCGHelperInfo *info, TCGTemp *ret, 2610 TCGTemp *t1, TCGTemp *t2, TCGTemp *t3) 2611 { 2612 TCGTemp *args[3] = { t1, t2, t3 }; 2613 tcg_gen_callN(func, info, ret, args); 2614 } 2615 2616 void tcg_gen_call4(void *func, TCGHelperInfo *info, TCGTemp *ret, 2617 TCGTemp *t1, TCGTemp *t2, TCGTemp *t3, TCGTemp *t4) 2618 { 2619 TCGTemp *args[4] = { t1, t2, t3, t4 }; 2620 tcg_gen_callN(func, info, ret, args); 2621 } 2622 2623 void tcg_gen_call5(void *func, TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1, 2624 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4, TCGTemp *t5) 2625 { 2626 TCGTemp *args[5] = { t1, t2, t3, t4, t5 }; 2627 tcg_gen_callN(func, info, ret, args); 2628 } 2629 2630 void tcg_gen_call6(void *func, TCGHelperInfo *info, TCGTemp *ret, 2631 TCGTemp *t1, TCGTemp *t2, TCGTemp *t3, 2632 TCGTemp *t4, TCGTemp *t5, TCGTemp *t6) 2633 { 2634 TCGTemp *args[6] = { t1, t2, t3, t4, t5, t6 }; 2635 tcg_gen_callN(func, info, ret, args); 2636 } 2637 2638 void tcg_gen_call7(void *func, TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1, 2639 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4, 2640 TCGTemp *t5, TCGTemp *t6, TCGTemp *t7) 2641 { 2642 TCGTemp *args[7] = { t1, t2, t3, t4, t5, t6, t7 }; 2643 tcg_gen_callN(func, info, ret, args); 2644 } 2645 2646 static void tcg_reg_alloc_start(TCGContext *s) 2647 { 2648 int i, n; 2649 2650 for (i = 0, n = s->nb_temps; i < n; i++) { 2651 TCGTemp *ts = &s->temps[i]; 2652 TCGTempVal val = TEMP_VAL_MEM; 2653 2654 switch (ts->kind) { 2655 case TEMP_CONST: 2656 val = TEMP_VAL_CONST; 2657 break; 2658 case TEMP_FIXED: 2659 val = TEMP_VAL_REG; 2660 break; 2661 case TEMP_GLOBAL: 2662 break; 2663 case TEMP_EBB: 2664 val = TEMP_VAL_DEAD; 2665 /* fall through */ 2666 case TEMP_TB: 2667 ts->mem_allocated = 0; 2668 break; 2669 default: 2670 g_assert_not_reached(); 2671 } 2672 ts->val_type = val; 2673 } 2674 2675 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp)); 2676 } 2677 2678 static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size, 2679 TCGTemp *ts) 2680 { 2681 int idx = temp_idx(ts); 2682 2683 switch (ts->kind) { 2684 case TEMP_FIXED: 2685 case TEMP_GLOBAL: 2686 pstrcpy(buf, buf_size, ts->name); 2687 break; 2688 case TEMP_TB: 2689 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals); 2690 break; 2691 case TEMP_EBB: 2692 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals); 2693 break; 2694 case TEMP_CONST: 2695 switch (ts->type) { 2696 case TCG_TYPE_I32: 2697 snprintf(buf, buf_size, "$0x%x", (int32_t)ts->val); 2698 break; 2699 #if TCG_TARGET_REG_BITS > 32 2700 case TCG_TYPE_I64: 2701 snprintf(buf, buf_size, "$0x%" PRIx64, ts->val); 2702 break; 2703 #endif 2704 case TCG_TYPE_V64: 2705 case TCG_TYPE_V128: 2706 case TCG_TYPE_V256: 2707 snprintf(buf, buf_size, "v%d$0x%" PRIx64, 2708 64 << (ts->type - TCG_TYPE_V64), ts->val); 2709 break; 2710 default: 2711 g_assert_not_reached(); 2712 } 2713 break; 2714 } 2715 return buf; 2716 } 2717 2718 static char *tcg_get_arg_str(TCGContext *s, char *buf, 2719 int buf_size, TCGArg arg) 2720 { 2721 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg)); 2722 } 2723 2724 static const char * const cond_name[] = 2725 { 2726 [TCG_COND_NEVER] = "never", 2727 [TCG_COND_ALWAYS] = "always", 2728 [TCG_COND_EQ] = "eq", 2729 [TCG_COND_NE] = "ne", 2730 [TCG_COND_LT] = "lt", 2731 [TCG_COND_GE] = "ge", 2732 [TCG_COND_LE] = "le", 2733 [TCG_COND_GT] = "gt", 2734 [TCG_COND_LTU] = "ltu", 2735 [TCG_COND_GEU] = "geu", 2736 [TCG_COND_LEU] = "leu", 2737 [TCG_COND_GTU] = "gtu", 2738 [TCG_COND_TSTEQ] = "tsteq", 2739 [TCG_COND_TSTNE] = "tstne", 2740 }; 2741 2742 static const char * const ldst_name[(MO_BSWAP | MO_SSIZE) + 1] = 2743 { 2744 [MO_UB] = "ub", 2745 [MO_SB] = "sb", 2746 [MO_LEUW] = "leuw", 2747 [MO_LESW] = "lesw", 2748 [MO_LEUL] = "leul", 2749 [MO_LESL] = "lesl", 2750 [MO_LEUQ] = "leq", 2751 [MO_BEUW] = "beuw", 2752 [MO_BESW] = "besw", 2753 [MO_BEUL] = "beul", 2754 [MO_BESL] = "besl", 2755 [MO_BEUQ] = "beq", 2756 [MO_128 + MO_BE] = "beo", 2757 [MO_128 + MO_LE] = "leo", 2758 }; 2759 2760 static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = { 2761 [MO_UNALN >> MO_ASHIFT] = "un+", 2762 [MO_ALIGN >> MO_ASHIFT] = "al+", 2763 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+", 2764 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+", 2765 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+", 2766 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+", 2767 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+", 2768 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+", 2769 }; 2770 2771 static const char * const atom_name[(MO_ATOM_MASK >> MO_ATOM_SHIFT) + 1] = { 2772 [MO_ATOM_IFALIGN >> MO_ATOM_SHIFT] = "", 2773 [MO_ATOM_IFALIGN_PAIR >> MO_ATOM_SHIFT] = "pair+", 2774 [MO_ATOM_WITHIN16 >> MO_ATOM_SHIFT] = "w16+", 2775 [MO_ATOM_WITHIN16_PAIR >> MO_ATOM_SHIFT] = "w16p+", 2776 [MO_ATOM_SUBALIGN >> MO_ATOM_SHIFT] = "sub+", 2777 [MO_ATOM_NONE >> MO_ATOM_SHIFT] = "noat+", 2778 }; 2779 2780 static const char bswap_flag_name[][6] = { 2781 [TCG_BSWAP_IZ] = "iz", 2782 [TCG_BSWAP_OZ] = "oz", 2783 [TCG_BSWAP_OS] = "os", 2784 [TCG_BSWAP_IZ | TCG_BSWAP_OZ] = "iz,oz", 2785 [TCG_BSWAP_IZ | TCG_BSWAP_OS] = "iz,os", 2786 }; 2787 2788 #ifdef CONFIG_PLUGIN 2789 static const char * const plugin_from_name[] = { 2790 "from-tb", 2791 "from-insn", 2792 "after-insn", 2793 "after-tb", 2794 }; 2795 #endif 2796 2797 static inline bool tcg_regset_single(TCGRegSet d) 2798 { 2799 return (d & (d - 1)) == 0; 2800 } 2801 2802 static inline TCGReg tcg_regset_first(TCGRegSet d) 2803 { 2804 if (TCG_TARGET_NB_REGS <= 32) { 2805 return ctz32(d); 2806 } else { 2807 return ctz64(d); 2808 } 2809 } 2810 2811 /* Return only the number of characters output -- no error return. */ 2812 #define ne_fprintf(...) \ 2813 ({ int ret_ = fprintf(__VA_ARGS__); ret_ >= 0 ? ret_ : 0; }) 2814 2815 void tcg_dump_ops(TCGContext *s, FILE *f, bool have_prefs) 2816 { 2817 char buf[128]; 2818 TCGOp *op; 2819 2820 QTAILQ_FOREACH(op, &s->ops, link) { 2821 int i, k, nb_oargs, nb_iargs, nb_cargs; 2822 const TCGOpDef *def; 2823 TCGOpcode c; 2824 int col = 0; 2825 2826 c = op->opc; 2827 def = &tcg_op_defs[c]; 2828 2829 if (c == INDEX_op_insn_start) { 2830 nb_oargs = 0; 2831 col += ne_fprintf(f, "\n ----"); 2832 2833 for (i = 0, k = s->insn_start_words; i < k; ++i) { 2834 col += ne_fprintf(f, " %016" PRIx64, 2835 tcg_get_insn_start_param(op, i)); 2836 } 2837 } else if (c == INDEX_op_call) { 2838 const TCGHelperInfo *info = tcg_call_info(op); 2839 void *func = tcg_call_func(op); 2840 2841 /* variable number of arguments */ 2842 nb_oargs = TCGOP_CALLO(op); 2843 nb_iargs = TCGOP_CALLI(op); 2844 nb_cargs = def->nb_cargs; 2845 2846 col += ne_fprintf(f, " %s ", def->name); 2847 2848 /* 2849 * Print the function name from TCGHelperInfo, if available. 2850 * Note that plugins have a template function for the info, 2851 * but the actual function pointer comes from the plugin. 2852 */ 2853 if (func == info->func) { 2854 col += ne_fprintf(f, "%s", info->name); 2855 } else { 2856 col += ne_fprintf(f, "plugin(%p)", func); 2857 } 2858 2859 col += ne_fprintf(f, ",$0x%x,$%d", info->flags, nb_oargs); 2860 for (i = 0; i < nb_oargs; i++) { 2861 col += ne_fprintf(f, ",%s", tcg_get_arg_str(s, buf, sizeof(buf), 2862 op->args[i])); 2863 } 2864 for (i = 0; i < nb_iargs; i++) { 2865 TCGArg arg = op->args[nb_oargs + i]; 2866 const char *t = tcg_get_arg_str(s, buf, sizeof(buf), arg); 2867 col += ne_fprintf(f, ",%s", t); 2868 } 2869 } else { 2870 if (def->flags & TCG_OPF_INT) { 2871 col += ne_fprintf(f, " %s_i%d ", 2872 def->name, 2873 8 * tcg_type_size(TCGOP_TYPE(op))); 2874 } else if (def->flags & TCG_OPF_VECTOR) { 2875 col += ne_fprintf(f, "%s v%d,e%d,", 2876 def->name, 2877 8 * tcg_type_size(TCGOP_TYPE(op)), 2878 8 << TCGOP_VECE(op)); 2879 } else { 2880 col += ne_fprintf(f, " %s ", def->name); 2881 } 2882 2883 nb_oargs = def->nb_oargs; 2884 nb_iargs = def->nb_iargs; 2885 nb_cargs = def->nb_cargs; 2886 2887 k = 0; 2888 for (i = 0; i < nb_oargs; i++) { 2889 const char *sep = k ? "," : ""; 2890 col += ne_fprintf(f, "%s%s", sep, 2891 tcg_get_arg_str(s, buf, sizeof(buf), 2892 op->args[k++])); 2893 } 2894 for (i = 0; i < nb_iargs; i++) { 2895 const char *sep = k ? "," : ""; 2896 col += ne_fprintf(f, "%s%s", sep, 2897 tcg_get_arg_str(s, buf, sizeof(buf), 2898 op->args[k++])); 2899 } 2900 switch (c) { 2901 case INDEX_op_brcond: 2902 case INDEX_op_setcond: 2903 case INDEX_op_negsetcond: 2904 case INDEX_op_movcond: 2905 case INDEX_op_brcond2_i32: 2906 case INDEX_op_setcond2_i32: 2907 case INDEX_op_cmp_vec: 2908 case INDEX_op_cmpsel_vec: 2909 if (op->args[k] < ARRAY_SIZE(cond_name) 2910 && cond_name[op->args[k]]) { 2911 col += ne_fprintf(f, ",%s", cond_name[op->args[k++]]); 2912 } else { 2913 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, op->args[k++]); 2914 } 2915 i = 1; 2916 break; 2917 case INDEX_op_qemu_ld_i32: 2918 case INDEX_op_qemu_st_i32: 2919 case INDEX_op_qemu_st8_i32: 2920 case INDEX_op_qemu_ld_i64: 2921 case INDEX_op_qemu_st_i64: 2922 case INDEX_op_qemu_ld_i128: 2923 case INDEX_op_qemu_st_i128: 2924 { 2925 const char *s_al, *s_op, *s_at; 2926 MemOpIdx oi = op->args[k++]; 2927 MemOp mop = get_memop(oi); 2928 unsigned ix = get_mmuidx(oi); 2929 2930 s_al = alignment_name[(mop & MO_AMASK) >> MO_ASHIFT]; 2931 s_op = ldst_name[mop & (MO_BSWAP | MO_SSIZE)]; 2932 s_at = atom_name[(mop & MO_ATOM_MASK) >> MO_ATOM_SHIFT]; 2933 mop &= ~(MO_AMASK | MO_BSWAP | MO_SSIZE | MO_ATOM_MASK); 2934 2935 /* If all fields are accounted for, print symbolically. */ 2936 if (!mop && s_al && s_op && s_at) { 2937 col += ne_fprintf(f, ",%s%s%s,%u", 2938 s_at, s_al, s_op, ix); 2939 } else { 2940 mop = get_memop(oi); 2941 col += ne_fprintf(f, ",$0x%x,%u", mop, ix); 2942 } 2943 i = 1; 2944 } 2945 break; 2946 case INDEX_op_bswap16: 2947 case INDEX_op_bswap32: 2948 case INDEX_op_bswap64: 2949 { 2950 TCGArg flags = op->args[k]; 2951 const char *name = NULL; 2952 2953 if (flags < ARRAY_SIZE(bswap_flag_name)) { 2954 name = bswap_flag_name[flags]; 2955 } 2956 if (name) { 2957 col += ne_fprintf(f, ",%s", name); 2958 } else { 2959 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, flags); 2960 } 2961 i = k = 1; 2962 } 2963 break; 2964 #ifdef CONFIG_PLUGIN 2965 case INDEX_op_plugin_cb: 2966 { 2967 TCGArg from = op->args[k++]; 2968 const char *name = NULL; 2969 2970 if (from < ARRAY_SIZE(plugin_from_name)) { 2971 name = plugin_from_name[from]; 2972 } 2973 if (name) { 2974 col += ne_fprintf(f, "%s", name); 2975 } else { 2976 col += ne_fprintf(f, "$0x%" TCG_PRIlx, from); 2977 } 2978 i = 1; 2979 } 2980 break; 2981 #endif 2982 default: 2983 i = 0; 2984 break; 2985 } 2986 switch (c) { 2987 case INDEX_op_set_label: 2988 case INDEX_op_br: 2989 case INDEX_op_brcond: 2990 case INDEX_op_brcond2_i32: 2991 col += ne_fprintf(f, "%s$L%d", k ? "," : "", 2992 arg_label(op->args[k])->id); 2993 i++, k++; 2994 break; 2995 case INDEX_op_mb: 2996 { 2997 TCGBar membar = op->args[k]; 2998 const char *b_op, *m_op; 2999 3000 switch (membar & TCG_BAR_SC) { 3001 case 0: 3002 b_op = "none"; 3003 break; 3004 case TCG_BAR_LDAQ: 3005 b_op = "acq"; 3006 break; 3007 case TCG_BAR_STRL: 3008 b_op = "rel"; 3009 break; 3010 case TCG_BAR_SC: 3011 b_op = "seq"; 3012 break; 3013 default: 3014 g_assert_not_reached(); 3015 } 3016 3017 switch (membar & TCG_MO_ALL) { 3018 case 0: 3019 m_op = "none"; 3020 break; 3021 case TCG_MO_LD_LD: 3022 m_op = "rr"; 3023 break; 3024 case TCG_MO_LD_ST: 3025 m_op = "rw"; 3026 break; 3027 case TCG_MO_ST_LD: 3028 m_op = "wr"; 3029 break; 3030 case TCG_MO_ST_ST: 3031 m_op = "ww"; 3032 break; 3033 case TCG_MO_LD_LD | TCG_MO_LD_ST: 3034 m_op = "rr+rw"; 3035 break; 3036 case TCG_MO_LD_LD | TCG_MO_ST_LD: 3037 m_op = "rr+wr"; 3038 break; 3039 case TCG_MO_LD_LD | TCG_MO_ST_ST: 3040 m_op = "rr+ww"; 3041 break; 3042 case TCG_MO_LD_ST | TCG_MO_ST_LD: 3043 m_op = "rw+wr"; 3044 break; 3045 case TCG_MO_LD_ST | TCG_MO_ST_ST: 3046 m_op = "rw+ww"; 3047 break; 3048 case TCG_MO_ST_LD | TCG_MO_ST_ST: 3049 m_op = "wr+ww"; 3050 break; 3051 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_LD: 3052 m_op = "rr+rw+wr"; 3053 break; 3054 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST: 3055 m_op = "rr+rw+ww"; 3056 break; 3057 case TCG_MO_LD_LD | TCG_MO_ST_LD | TCG_MO_ST_ST: 3058 m_op = "rr+wr+ww"; 3059 break; 3060 case TCG_MO_LD_ST | TCG_MO_ST_LD | TCG_MO_ST_ST: 3061 m_op = "rw+wr+ww"; 3062 break; 3063 case TCG_MO_ALL: 3064 m_op = "all"; 3065 break; 3066 default: 3067 g_assert_not_reached(); 3068 } 3069 3070 col += ne_fprintf(f, "%s%s:%s", (k ? "," : ""), b_op, m_op); 3071 i++, k++; 3072 } 3073 break; 3074 default: 3075 break; 3076 } 3077 for (; i < nb_cargs; i++, k++) { 3078 col += ne_fprintf(f, "%s$0x%" TCG_PRIlx, k ? "," : "", 3079 op->args[k]); 3080 } 3081 } 3082 3083 if (have_prefs || op->life) { 3084 for (; col < 40; ++col) { 3085 putc(' ', f); 3086 } 3087 } 3088 3089 if (op->life) { 3090 unsigned life = op->life; 3091 3092 if (life & (SYNC_ARG * 3)) { 3093 ne_fprintf(f, " sync:"); 3094 for (i = 0; i < 2; ++i) { 3095 if (life & (SYNC_ARG << i)) { 3096 ne_fprintf(f, " %d", i); 3097 } 3098 } 3099 } 3100 life /= DEAD_ARG; 3101 if (life) { 3102 ne_fprintf(f, " dead:"); 3103 for (i = 0; life; ++i, life >>= 1) { 3104 if (life & 1) { 3105 ne_fprintf(f, " %d", i); 3106 } 3107 } 3108 } 3109 } 3110 3111 if (have_prefs) { 3112 for (i = 0; i < nb_oargs; ++i) { 3113 TCGRegSet set = output_pref(op, i); 3114 3115 if (i == 0) { 3116 ne_fprintf(f, " pref="); 3117 } else { 3118 ne_fprintf(f, ","); 3119 } 3120 if (set == 0) { 3121 ne_fprintf(f, "none"); 3122 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) { 3123 ne_fprintf(f, "all"); 3124 #ifdef CONFIG_DEBUG_TCG 3125 } else if (tcg_regset_single(set)) { 3126 TCGReg reg = tcg_regset_first(set); 3127 ne_fprintf(f, "%s", tcg_target_reg_names[reg]); 3128 #endif 3129 } else if (TCG_TARGET_NB_REGS <= 32) { 3130 ne_fprintf(f, "0x%x", (uint32_t)set); 3131 } else { 3132 ne_fprintf(f, "0x%" PRIx64, (uint64_t)set); 3133 } 3134 } 3135 } 3136 3137 putc('\n', f); 3138 } 3139 } 3140 3141 /* we give more priority to constraints with less registers */ 3142 static int get_constraint_priority(const TCGArgConstraint *arg_ct, int k) 3143 { 3144 int n; 3145 3146 arg_ct += k; 3147 n = ctpop64(arg_ct->regs); 3148 3149 /* 3150 * Sort constraints of a single register first, which includes output 3151 * aliases (which must exactly match the input already allocated). 3152 */ 3153 if (n == 1 || arg_ct->oalias) { 3154 return INT_MAX; 3155 } 3156 3157 /* 3158 * Sort register pairs next, first then second immediately after. 3159 * Arbitrarily sort multiple pairs by the index of the first reg; 3160 * there shouldn't be many pairs. 3161 */ 3162 switch (arg_ct->pair) { 3163 case 1: 3164 case 3: 3165 return (k + 1) * 2; 3166 case 2: 3167 return (arg_ct->pair_index + 1) * 2 - 1; 3168 } 3169 3170 /* Finally, sort by decreasing register count. */ 3171 assert(n > 1); 3172 return -n; 3173 } 3174 3175 /* sort from highest priority to lowest */ 3176 static void sort_constraints(TCGArgConstraint *a, int start, int n) 3177 { 3178 int i, j; 3179 3180 for (i = 0; i < n; i++) { 3181 a[start + i].sort_index = start + i; 3182 } 3183 if (n <= 1) { 3184 return; 3185 } 3186 for (i = 0; i < n - 1; i++) { 3187 for (j = i + 1; j < n; j++) { 3188 int p1 = get_constraint_priority(a, a[start + i].sort_index); 3189 int p2 = get_constraint_priority(a, a[start + j].sort_index); 3190 if (p1 < p2) { 3191 int tmp = a[start + i].sort_index; 3192 a[start + i].sort_index = a[start + j].sort_index; 3193 a[start + j].sort_index = tmp; 3194 } 3195 } 3196 } 3197 } 3198 3199 static const TCGArgConstraint empty_cts[TCG_MAX_OP_ARGS]; 3200 static TCGArgConstraint all_cts[ARRAY_SIZE(constraint_sets)][TCG_MAX_OP_ARGS]; 3201 3202 static void process_constraint_sets(void) 3203 { 3204 for (size_t c = 0; c < ARRAY_SIZE(constraint_sets); ++c) { 3205 const TCGConstraintSet *tdefs = &constraint_sets[c]; 3206 TCGArgConstraint *args_ct = all_cts[c]; 3207 int nb_oargs = tdefs->nb_oargs; 3208 int nb_iargs = tdefs->nb_iargs; 3209 int nb_args = nb_oargs + nb_iargs; 3210 bool saw_alias_pair = false; 3211 3212 for (int i = 0; i < nb_args; i++) { 3213 const char *ct_str = tdefs->args_ct_str[i]; 3214 bool input_p = i >= nb_oargs; 3215 int o; 3216 3217 switch (*ct_str) { 3218 case '0' ... '9': 3219 o = *ct_str - '0'; 3220 tcg_debug_assert(input_p); 3221 tcg_debug_assert(o < nb_oargs); 3222 tcg_debug_assert(args_ct[o].regs != 0); 3223 tcg_debug_assert(!args_ct[o].oalias); 3224 args_ct[i] = args_ct[o]; 3225 /* The output sets oalias. */ 3226 args_ct[o].oalias = 1; 3227 args_ct[o].alias_index = i; 3228 /* The input sets ialias. */ 3229 args_ct[i].ialias = 1; 3230 args_ct[i].alias_index = o; 3231 if (args_ct[i].pair) { 3232 saw_alias_pair = true; 3233 } 3234 tcg_debug_assert(ct_str[1] == '\0'); 3235 continue; 3236 3237 case '&': 3238 tcg_debug_assert(!input_p); 3239 args_ct[i].newreg = true; 3240 ct_str++; 3241 break; 3242 3243 case 'p': /* plus */ 3244 /* Allocate to the register after the previous. */ 3245 tcg_debug_assert(i > (input_p ? nb_oargs : 0)); 3246 o = i - 1; 3247 tcg_debug_assert(!args_ct[o].pair); 3248 tcg_debug_assert(!args_ct[o].ct); 3249 args_ct[i] = (TCGArgConstraint){ 3250 .pair = 2, 3251 .pair_index = o, 3252 .regs = args_ct[o].regs << 1, 3253 .newreg = args_ct[o].newreg, 3254 }; 3255 args_ct[o].pair = 1; 3256 args_ct[o].pair_index = i; 3257 tcg_debug_assert(ct_str[1] == '\0'); 3258 continue; 3259 3260 case 'm': /* minus */ 3261 /* Allocate to the register before the previous. */ 3262 tcg_debug_assert(i > (input_p ? nb_oargs : 0)); 3263 o = i - 1; 3264 tcg_debug_assert(!args_ct[o].pair); 3265 tcg_debug_assert(!args_ct[o].ct); 3266 args_ct[i] = (TCGArgConstraint){ 3267 .pair = 1, 3268 .pair_index = o, 3269 .regs = args_ct[o].regs >> 1, 3270 .newreg = args_ct[o].newreg, 3271 }; 3272 args_ct[o].pair = 2; 3273 args_ct[o].pair_index = i; 3274 tcg_debug_assert(ct_str[1] == '\0'); 3275 continue; 3276 } 3277 3278 do { 3279 switch (*ct_str) { 3280 case 'i': 3281 args_ct[i].ct |= TCG_CT_CONST; 3282 break; 3283 #ifdef TCG_REG_ZERO 3284 case 'z': 3285 args_ct[i].ct |= TCG_CT_REG_ZERO; 3286 break; 3287 #endif 3288 3289 /* Include all of the target-specific constraints. */ 3290 3291 #undef CONST 3292 #define CONST(CASE, MASK) \ 3293 case CASE: args_ct[i].ct |= MASK; break; 3294 #define REGS(CASE, MASK) \ 3295 case CASE: args_ct[i].regs |= MASK; break; 3296 3297 #include "tcg-target-con-str.h" 3298 3299 #undef REGS 3300 #undef CONST 3301 default: 3302 case '0' ... '9': 3303 case '&': 3304 case 'p': 3305 case 'm': 3306 /* Typo in TCGConstraintSet constraint. */ 3307 g_assert_not_reached(); 3308 } 3309 } while (*++ct_str != '\0'); 3310 } 3311 3312 /* 3313 * Fix up output pairs that are aliased with inputs. 3314 * When we created the alias, we copied pair from the output. 3315 * There are three cases: 3316 * (1a) Pairs of inputs alias pairs of outputs. 3317 * (1b) One input aliases the first of a pair of outputs. 3318 * (2) One input aliases the second of a pair of outputs. 3319 * 3320 * Case 1a is handled by making sure that the pair_index'es are 3321 * properly updated so that they appear the same as a pair of inputs. 3322 * 3323 * Case 1b is handled by setting the pair_index of the input to 3324 * itself, simply so it doesn't point to an unrelated argument. 3325 * Since we don't encounter the "second" during the input allocation 3326 * phase, nothing happens with the second half of the input pair. 3327 * 3328 * Case 2 is handled by setting the second input to pair=3, the 3329 * first output to pair=3, and the pair_index'es to match. 3330 */ 3331 if (saw_alias_pair) { 3332 for (int i = nb_oargs; i < nb_args; i++) { 3333 int o, o2, i2; 3334 3335 /* 3336 * Since [0-9pm] must be alone in the constraint string, 3337 * the only way they can both be set is if the pair comes 3338 * from the output alias. 3339 */ 3340 if (!args_ct[i].ialias) { 3341 continue; 3342 } 3343 switch (args_ct[i].pair) { 3344 case 0: 3345 break; 3346 case 1: 3347 o = args_ct[i].alias_index; 3348 o2 = args_ct[o].pair_index; 3349 tcg_debug_assert(args_ct[o].pair == 1); 3350 tcg_debug_assert(args_ct[o2].pair == 2); 3351 if (args_ct[o2].oalias) { 3352 /* Case 1a */ 3353 i2 = args_ct[o2].alias_index; 3354 tcg_debug_assert(args_ct[i2].pair == 2); 3355 args_ct[i2].pair_index = i; 3356 args_ct[i].pair_index = i2; 3357 } else { 3358 /* Case 1b */ 3359 args_ct[i].pair_index = i; 3360 } 3361 break; 3362 case 2: 3363 o = args_ct[i].alias_index; 3364 o2 = args_ct[o].pair_index; 3365 tcg_debug_assert(args_ct[o].pair == 2); 3366 tcg_debug_assert(args_ct[o2].pair == 1); 3367 if (args_ct[o2].oalias) { 3368 /* Case 1a */ 3369 i2 = args_ct[o2].alias_index; 3370 tcg_debug_assert(args_ct[i2].pair == 1); 3371 args_ct[i2].pair_index = i; 3372 args_ct[i].pair_index = i2; 3373 } else { 3374 /* Case 2 */ 3375 args_ct[i].pair = 3; 3376 args_ct[o2].pair = 3; 3377 args_ct[i].pair_index = o2; 3378 args_ct[o2].pair_index = i; 3379 } 3380 break; 3381 default: 3382 g_assert_not_reached(); 3383 } 3384 } 3385 } 3386 3387 /* sort the constraints (XXX: this is just an heuristic) */ 3388 sort_constraints(args_ct, 0, nb_oargs); 3389 sort_constraints(args_ct, nb_oargs, nb_iargs); 3390 } 3391 } 3392 3393 static const TCGArgConstraint *opcode_args_ct(const TCGOp *op) 3394 { 3395 TCGOpcode opc = op->opc; 3396 TCGType type = TCGOP_TYPE(op); 3397 unsigned flags = TCGOP_FLAGS(op); 3398 const TCGOpDef *def = &tcg_op_defs[opc]; 3399 const TCGOutOp *outop = all_outop[opc]; 3400 TCGConstraintSetIndex con_set; 3401 3402 if (def->flags & TCG_OPF_NOT_PRESENT) { 3403 return empty_cts; 3404 } 3405 3406 if (outop) { 3407 con_set = outop->static_constraint; 3408 if (con_set == C_Dynamic) { 3409 con_set = outop->dynamic_constraint(type, flags); 3410 } 3411 } else { 3412 con_set = tcg_target_op_def(opc, type, flags); 3413 } 3414 tcg_debug_assert(con_set >= 0); 3415 tcg_debug_assert(con_set < ARRAY_SIZE(constraint_sets)); 3416 3417 /* The constraint arguments must match TCGOpcode arguments. */ 3418 tcg_debug_assert(constraint_sets[con_set].nb_oargs == def->nb_oargs); 3419 tcg_debug_assert(constraint_sets[con_set].nb_iargs == def->nb_iargs); 3420 3421 return all_cts[con_set]; 3422 } 3423 3424 static void remove_label_use(TCGOp *op, int idx) 3425 { 3426 TCGLabel *label = arg_label(op->args[idx]); 3427 TCGLabelUse *use; 3428 3429 QSIMPLEQ_FOREACH(use, &label->branches, next) { 3430 if (use->op == op) { 3431 QSIMPLEQ_REMOVE(&label->branches, use, TCGLabelUse, next); 3432 return; 3433 } 3434 } 3435 g_assert_not_reached(); 3436 } 3437 3438 void tcg_op_remove(TCGContext *s, TCGOp *op) 3439 { 3440 switch (op->opc) { 3441 case INDEX_op_br: 3442 remove_label_use(op, 0); 3443 break; 3444 case INDEX_op_brcond: 3445 remove_label_use(op, 3); 3446 break; 3447 case INDEX_op_brcond2_i32: 3448 remove_label_use(op, 5); 3449 break; 3450 default: 3451 break; 3452 } 3453 3454 QTAILQ_REMOVE(&s->ops, op, link); 3455 QTAILQ_INSERT_TAIL(&s->free_ops, op, link); 3456 s->nb_ops--; 3457 } 3458 3459 void tcg_remove_ops_after(TCGOp *op) 3460 { 3461 TCGContext *s = tcg_ctx; 3462 3463 while (true) { 3464 TCGOp *last = tcg_last_op(); 3465 if (last == op) { 3466 return; 3467 } 3468 tcg_op_remove(s, last); 3469 } 3470 } 3471 3472 static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs) 3473 { 3474 TCGContext *s = tcg_ctx; 3475 TCGOp *op = NULL; 3476 3477 if (unlikely(!QTAILQ_EMPTY(&s->free_ops))) { 3478 QTAILQ_FOREACH(op, &s->free_ops, link) { 3479 if (nargs <= op->nargs) { 3480 QTAILQ_REMOVE(&s->free_ops, op, link); 3481 nargs = op->nargs; 3482 goto found; 3483 } 3484 } 3485 } 3486 3487 /* Most opcodes have 3 or 4 operands: reduce fragmentation. */ 3488 nargs = MAX(4, nargs); 3489 op = tcg_malloc(sizeof(TCGOp) + sizeof(TCGArg) * nargs); 3490 3491 found: 3492 memset(op, 0, offsetof(TCGOp, link)); 3493 op->opc = opc; 3494 op->nargs = nargs; 3495 3496 /* Check for bitfield overflow. */ 3497 tcg_debug_assert(op->nargs == nargs); 3498 3499 s->nb_ops++; 3500 return op; 3501 } 3502 3503 TCGOp *tcg_emit_op(TCGOpcode opc, unsigned nargs) 3504 { 3505 TCGOp *op = tcg_op_alloc(opc, nargs); 3506 3507 if (tcg_ctx->emit_before_op) { 3508 QTAILQ_INSERT_BEFORE(tcg_ctx->emit_before_op, op, link); 3509 } else { 3510 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link); 3511 } 3512 return op; 3513 } 3514 3515 TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, 3516 TCGOpcode opc, TCGType type, unsigned nargs) 3517 { 3518 TCGOp *new_op = tcg_op_alloc(opc, nargs); 3519 3520 TCGOP_TYPE(new_op) = type; 3521 QTAILQ_INSERT_BEFORE(old_op, new_op, link); 3522 return new_op; 3523 } 3524 3525 TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, 3526 TCGOpcode opc, TCGType type, unsigned nargs) 3527 { 3528 TCGOp *new_op = tcg_op_alloc(opc, nargs); 3529 3530 TCGOP_TYPE(new_op) = type; 3531 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link); 3532 return new_op; 3533 } 3534 3535 static void move_label_uses(TCGLabel *to, TCGLabel *from) 3536 { 3537 TCGLabelUse *u; 3538 3539 QSIMPLEQ_FOREACH(u, &from->branches, next) { 3540 TCGOp *op = u->op; 3541 switch (op->opc) { 3542 case INDEX_op_br: 3543 op->args[0] = label_arg(to); 3544 break; 3545 case INDEX_op_brcond: 3546 op->args[3] = label_arg(to); 3547 break; 3548 case INDEX_op_brcond2_i32: 3549 op->args[5] = label_arg(to); 3550 break; 3551 default: 3552 g_assert_not_reached(); 3553 } 3554 } 3555 3556 QSIMPLEQ_CONCAT(&to->branches, &from->branches); 3557 } 3558 3559 /* Reachable analysis : remove unreachable code. */ 3560 static void __attribute__((noinline)) 3561 reachable_code_pass(TCGContext *s) 3562 { 3563 TCGOp *op, *op_next, *op_prev; 3564 bool dead = false; 3565 3566 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { 3567 bool remove = dead; 3568 TCGLabel *label; 3569 3570 switch (op->opc) { 3571 case INDEX_op_set_label: 3572 label = arg_label(op->args[0]); 3573 3574 /* 3575 * Note that the first op in the TB is always a load, 3576 * so there is always something before a label. 3577 */ 3578 op_prev = QTAILQ_PREV(op, link); 3579 3580 /* 3581 * If we find two sequential labels, move all branches to 3582 * reference the second label and remove the first label. 3583 * Do this before branch to next optimization, so that the 3584 * middle label is out of the way. 3585 */ 3586 if (op_prev->opc == INDEX_op_set_label) { 3587 move_label_uses(label, arg_label(op_prev->args[0])); 3588 tcg_op_remove(s, op_prev); 3589 op_prev = QTAILQ_PREV(op, link); 3590 } 3591 3592 /* 3593 * Optimization can fold conditional branches to unconditional. 3594 * If we find a label which is preceded by an unconditional 3595 * branch to next, remove the branch. We couldn't do this when 3596 * processing the branch because any dead code between the branch 3597 * and label had not yet been removed. 3598 */ 3599 if (op_prev->opc == INDEX_op_br && 3600 label == arg_label(op_prev->args[0])) { 3601 tcg_op_remove(s, op_prev); 3602 /* Fall through means insns become live again. */ 3603 dead = false; 3604 } 3605 3606 if (QSIMPLEQ_EMPTY(&label->branches)) { 3607 /* 3608 * While there is an occasional backward branch, virtually 3609 * all branches generated by the translators are forward. 3610 * Which means that generally we will have already removed 3611 * all references to the label that will be, and there is 3612 * little to be gained by iterating. 3613 */ 3614 remove = true; 3615 } else { 3616 /* Once we see a label, insns become live again. */ 3617 dead = false; 3618 remove = false; 3619 } 3620 break; 3621 3622 case INDEX_op_br: 3623 case INDEX_op_exit_tb: 3624 case INDEX_op_goto_ptr: 3625 /* Unconditional branches; everything following is dead. */ 3626 dead = true; 3627 break; 3628 3629 case INDEX_op_call: 3630 /* Notice noreturn helper calls, raising exceptions. */ 3631 if (tcg_call_flags(op) & TCG_CALL_NO_RETURN) { 3632 dead = true; 3633 } 3634 break; 3635 3636 case INDEX_op_insn_start: 3637 /* Never remove -- we need to keep these for unwind. */ 3638 remove = false; 3639 break; 3640 3641 default: 3642 break; 3643 } 3644 3645 if (remove) { 3646 tcg_op_remove(s, op); 3647 } 3648 } 3649 } 3650 3651 #define TS_DEAD 1 3652 #define TS_MEM 2 3653 3654 #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n))) 3655 #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n))) 3656 3657 /* For liveness_pass_1, the register preferences for a given temp. */ 3658 static inline TCGRegSet *la_temp_pref(TCGTemp *ts) 3659 { 3660 return ts->state_ptr; 3661 } 3662 3663 /* For liveness_pass_1, reset the preferences for a given temp to the 3664 * maximal regset for its type. 3665 */ 3666 static inline void la_reset_pref(TCGTemp *ts) 3667 { 3668 *la_temp_pref(ts) 3669 = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]); 3670 } 3671 3672 /* liveness analysis: end of function: all temps are dead, and globals 3673 should be in memory. */ 3674 static void la_func_end(TCGContext *s, int ng, int nt) 3675 { 3676 int i; 3677 3678 for (i = 0; i < ng; ++i) { 3679 s->temps[i].state = TS_DEAD | TS_MEM; 3680 la_reset_pref(&s->temps[i]); 3681 } 3682 for (i = ng; i < nt; ++i) { 3683 s->temps[i].state = TS_DEAD; 3684 la_reset_pref(&s->temps[i]); 3685 } 3686 } 3687 3688 /* liveness analysis: end of basic block: all temps are dead, globals 3689 and local temps should be in memory. */ 3690 static void la_bb_end(TCGContext *s, int ng, int nt) 3691 { 3692 int i; 3693 3694 for (i = 0; i < nt; ++i) { 3695 TCGTemp *ts = &s->temps[i]; 3696 int state; 3697 3698 switch (ts->kind) { 3699 case TEMP_FIXED: 3700 case TEMP_GLOBAL: 3701 case TEMP_TB: 3702 state = TS_DEAD | TS_MEM; 3703 break; 3704 case TEMP_EBB: 3705 case TEMP_CONST: 3706 state = TS_DEAD; 3707 break; 3708 default: 3709 g_assert_not_reached(); 3710 } 3711 ts->state = state; 3712 la_reset_pref(ts); 3713 } 3714 } 3715 3716 /* liveness analysis: sync globals back to memory. */ 3717 static void la_global_sync(TCGContext *s, int ng) 3718 { 3719 int i; 3720 3721 for (i = 0; i < ng; ++i) { 3722 int state = s->temps[i].state; 3723 s->temps[i].state = state | TS_MEM; 3724 if (state == TS_DEAD) { 3725 /* If the global was previously dead, reset prefs. */ 3726 la_reset_pref(&s->temps[i]); 3727 } 3728 } 3729 } 3730 3731 /* 3732 * liveness analysis: conditional branch: all temps are dead unless 3733 * explicitly live-across-conditional-branch, globals and local temps 3734 * should be synced. 3735 */ 3736 static void la_bb_sync(TCGContext *s, int ng, int nt) 3737 { 3738 la_global_sync(s, ng); 3739 3740 for (int i = ng; i < nt; ++i) { 3741 TCGTemp *ts = &s->temps[i]; 3742 int state; 3743 3744 switch (ts->kind) { 3745 case TEMP_TB: 3746 state = ts->state; 3747 ts->state = state | TS_MEM; 3748 if (state != TS_DEAD) { 3749 continue; 3750 } 3751 break; 3752 case TEMP_EBB: 3753 case TEMP_CONST: 3754 continue; 3755 default: 3756 g_assert_not_reached(); 3757 } 3758 la_reset_pref(&s->temps[i]); 3759 } 3760 } 3761 3762 /* liveness analysis: sync globals back to memory and kill. */ 3763 static void la_global_kill(TCGContext *s, int ng) 3764 { 3765 int i; 3766 3767 for (i = 0; i < ng; i++) { 3768 s->temps[i].state = TS_DEAD | TS_MEM; 3769 la_reset_pref(&s->temps[i]); 3770 } 3771 } 3772 3773 /* liveness analysis: note live globals crossing calls. */ 3774 static void la_cross_call(TCGContext *s, int nt) 3775 { 3776 TCGRegSet mask = ~tcg_target_call_clobber_regs; 3777 int i; 3778 3779 for (i = 0; i < nt; i++) { 3780 TCGTemp *ts = &s->temps[i]; 3781 if (!(ts->state & TS_DEAD)) { 3782 TCGRegSet *pset = la_temp_pref(ts); 3783 TCGRegSet set = *pset; 3784 3785 set &= mask; 3786 /* If the combination is not possible, restart. */ 3787 if (set == 0) { 3788 set = tcg_target_available_regs[ts->type] & mask; 3789 } 3790 *pset = set; 3791 } 3792 } 3793 } 3794 3795 /* 3796 * Liveness analysis: Verify the lifetime of TEMP_TB, and reduce 3797 * to TEMP_EBB, if possible. 3798 */ 3799 static void __attribute__((noinline)) 3800 liveness_pass_0(TCGContext *s) 3801 { 3802 void * const multiple_ebb = (void *)(uintptr_t)-1; 3803 int nb_temps = s->nb_temps; 3804 TCGOp *op, *ebb; 3805 3806 for (int i = s->nb_globals; i < nb_temps; ++i) { 3807 s->temps[i].state_ptr = NULL; 3808 } 3809 3810 /* 3811 * Represent each EBB by the op at which it begins. In the case of 3812 * the first EBB, this is the first op, otherwise it is a label. 3813 * Collect the uses of each TEMP_TB: NULL for unused, EBB for use 3814 * within a single EBB, else MULTIPLE_EBB. 3815 */ 3816 ebb = QTAILQ_FIRST(&s->ops); 3817 QTAILQ_FOREACH(op, &s->ops, link) { 3818 const TCGOpDef *def; 3819 int nb_oargs, nb_iargs; 3820 3821 switch (op->opc) { 3822 case INDEX_op_set_label: 3823 ebb = op; 3824 continue; 3825 case INDEX_op_discard: 3826 continue; 3827 case INDEX_op_call: 3828 nb_oargs = TCGOP_CALLO(op); 3829 nb_iargs = TCGOP_CALLI(op); 3830 break; 3831 default: 3832 def = &tcg_op_defs[op->opc]; 3833 nb_oargs = def->nb_oargs; 3834 nb_iargs = def->nb_iargs; 3835 break; 3836 } 3837 3838 for (int i = 0; i < nb_oargs + nb_iargs; ++i) { 3839 TCGTemp *ts = arg_temp(op->args[i]); 3840 3841 if (ts->kind != TEMP_TB) { 3842 continue; 3843 } 3844 if (ts->state_ptr == NULL) { 3845 ts->state_ptr = ebb; 3846 } else if (ts->state_ptr != ebb) { 3847 ts->state_ptr = multiple_ebb; 3848 } 3849 } 3850 } 3851 3852 /* 3853 * For TEMP_TB that turned out not to be used beyond one EBB, 3854 * reduce the liveness to TEMP_EBB. 3855 */ 3856 for (int i = s->nb_globals; i < nb_temps; ++i) { 3857 TCGTemp *ts = &s->temps[i]; 3858 if (ts->kind == TEMP_TB && ts->state_ptr != multiple_ebb) { 3859 ts->kind = TEMP_EBB; 3860 } 3861 } 3862 } 3863 3864 /* Liveness analysis : update the opc_arg_life array to tell if a 3865 given input arguments is dead. Instructions updating dead 3866 temporaries are removed. */ 3867 static void __attribute__((noinline)) 3868 liveness_pass_1(TCGContext *s) 3869 { 3870 int nb_globals = s->nb_globals; 3871 int nb_temps = s->nb_temps; 3872 TCGOp *op, *op_prev; 3873 TCGRegSet *prefs; 3874 int i; 3875 3876 prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps); 3877 for (i = 0; i < nb_temps; ++i) { 3878 s->temps[i].state_ptr = prefs + i; 3879 } 3880 3881 /* ??? Should be redundant with the exit_tb that ends the TB. */ 3882 la_func_end(s, nb_globals, nb_temps); 3883 3884 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) { 3885 int nb_iargs, nb_oargs; 3886 TCGOpcode opc_new, opc_new2; 3887 TCGLifeData arg_life = 0; 3888 TCGTemp *ts; 3889 TCGOpcode opc = op->opc; 3890 const TCGOpDef *def = &tcg_op_defs[opc]; 3891 const TCGArgConstraint *args_ct; 3892 3893 switch (opc) { 3894 case INDEX_op_call: 3895 { 3896 const TCGHelperInfo *info = tcg_call_info(op); 3897 int call_flags = tcg_call_flags(op); 3898 3899 nb_oargs = TCGOP_CALLO(op); 3900 nb_iargs = TCGOP_CALLI(op); 3901 3902 /* pure functions can be removed if their result is unused */ 3903 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) { 3904 for (i = 0; i < nb_oargs; i++) { 3905 ts = arg_temp(op->args[i]); 3906 if (ts->state != TS_DEAD) { 3907 goto do_not_remove_call; 3908 } 3909 } 3910 goto do_remove; 3911 } 3912 do_not_remove_call: 3913 3914 /* Output args are dead. */ 3915 for (i = 0; i < nb_oargs; i++) { 3916 ts = arg_temp(op->args[i]); 3917 if (ts->state & TS_DEAD) { 3918 arg_life |= DEAD_ARG << i; 3919 } 3920 if (ts->state & TS_MEM) { 3921 arg_life |= SYNC_ARG << i; 3922 } 3923 ts->state = TS_DEAD; 3924 la_reset_pref(ts); 3925 } 3926 3927 /* Not used -- it will be tcg_target_call_oarg_reg(). */ 3928 memset(op->output_pref, 0, sizeof(op->output_pref)); 3929 3930 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS | 3931 TCG_CALL_NO_READ_GLOBALS))) { 3932 la_global_kill(s, nb_globals); 3933 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) { 3934 la_global_sync(s, nb_globals); 3935 } 3936 3937 /* Record arguments that die in this helper. */ 3938 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { 3939 ts = arg_temp(op->args[i]); 3940 if (ts->state & TS_DEAD) { 3941 arg_life |= DEAD_ARG << i; 3942 } 3943 } 3944 3945 /* For all live registers, remove call-clobbered prefs. */ 3946 la_cross_call(s, nb_temps); 3947 3948 /* 3949 * Input arguments are live for preceding opcodes. 3950 * 3951 * For those arguments that die, and will be allocated in 3952 * registers, clear the register set for that arg, to be 3953 * filled in below. For args that will be on the stack, 3954 * reset to any available reg. Process arguments in reverse 3955 * order so that if a temp is used more than once, the stack 3956 * reset to max happens before the register reset to 0. 3957 */ 3958 for (i = nb_iargs - 1; i >= 0; i--) { 3959 const TCGCallArgumentLoc *loc = &info->in[i]; 3960 ts = arg_temp(op->args[nb_oargs + i]); 3961 3962 if (ts->state & TS_DEAD) { 3963 switch (loc->kind) { 3964 case TCG_CALL_ARG_NORMAL: 3965 case TCG_CALL_ARG_EXTEND_U: 3966 case TCG_CALL_ARG_EXTEND_S: 3967 if (arg_slot_reg_p(loc->arg_slot)) { 3968 *la_temp_pref(ts) = 0; 3969 break; 3970 } 3971 /* fall through */ 3972 default: 3973 *la_temp_pref(ts) = 3974 tcg_target_available_regs[ts->type]; 3975 break; 3976 } 3977 ts->state &= ~TS_DEAD; 3978 } 3979 } 3980 3981 /* 3982 * For each input argument, add its input register to prefs. 3983 * If a temp is used once, this produces a single set bit; 3984 * if a temp is used multiple times, this produces a set. 3985 */ 3986 for (i = 0; i < nb_iargs; i++) { 3987 const TCGCallArgumentLoc *loc = &info->in[i]; 3988 ts = arg_temp(op->args[nb_oargs + i]); 3989 3990 switch (loc->kind) { 3991 case TCG_CALL_ARG_NORMAL: 3992 case TCG_CALL_ARG_EXTEND_U: 3993 case TCG_CALL_ARG_EXTEND_S: 3994 if (arg_slot_reg_p(loc->arg_slot)) { 3995 tcg_regset_set_reg(*la_temp_pref(ts), 3996 tcg_target_call_iarg_regs[loc->arg_slot]); 3997 } 3998 break; 3999 default: 4000 break; 4001 } 4002 } 4003 } 4004 break; 4005 case INDEX_op_insn_start: 4006 break; 4007 case INDEX_op_discard: 4008 /* mark the temporary as dead */ 4009 ts = arg_temp(op->args[0]); 4010 ts->state = TS_DEAD; 4011 la_reset_pref(ts); 4012 break; 4013 4014 case INDEX_op_add2_i32: 4015 case INDEX_op_add2_i64: 4016 opc_new = INDEX_op_add; 4017 goto do_addsub2; 4018 case INDEX_op_sub2_i32: 4019 case INDEX_op_sub2_i64: 4020 opc_new = INDEX_op_sub; 4021 do_addsub2: 4022 nb_iargs = 4; 4023 nb_oargs = 2; 4024 /* Test if the high part of the operation is dead, but not 4025 the low part. The result can be optimized to a simple 4026 add or sub. This happens often for x86_64 guest when the 4027 cpu mode is set to 32 bit. */ 4028 if (arg_temp(op->args[1])->state == TS_DEAD) { 4029 if (arg_temp(op->args[0])->state == TS_DEAD) { 4030 goto do_remove; 4031 } 4032 /* Replace the opcode and adjust the args in place, 4033 leaving 3 unused args at the end. */ 4034 op->opc = opc = opc_new; 4035 op->args[1] = op->args[2]; 4036 op->args[2] = op->args[4]; 4037 /* Fall through and mark the single-word operation live. */ 4038 nb_iargs = 2; 4039 nb_oargs = 1; 4040 } 4041 goto do_not_remove; 4042 4043 case INDEX_op_muls2: 4044 opc_new = INDEX_op_mul; 4045 opc_new2 = INDEX_op_mulsh; 4046 goto do_mul2; 4047 case INDEX_op_mulu2: 4048 opc_new = INDEX_op_mul; 4049 opc_new2 = INDEX_op_muluh; 4050 do_mul2: 4051 nb_iargs = 2; 4052 nb_oargs = 2; 4053 if (arg_temp(op->args[1])->state == TS_DEAD) { 4054 if (arg_temp(op->args[0])->state == TS_DEAD) { 4055 /* Both parts of the operation are dead. */ 4056 goto do_remove; 4057 } 4058 /* The high part of the operation is dead; generate the low. */ 4059 op->opc = opc = opc_new; 4060 op->args[1] = op->args[2]; 4061 op->args[2] = op->args[3]; 4062 } else if (arg_temp(op->args[0])->state == TS_DEAD && 4063 tcg_op_supported(opc_new2, TCGOP_TYPE(op), 0)) { 4064 /* The low part of the operation is dead; generate the high. */ 4065 op->opc = opc = opc_new2; 4066 op->args[0] = op->args[1]; 4067 op->args[1] = op->args[2]; 4068 op->args[2] = op->args[3]; 4069 } else { 4070 goto do_not_remove; 4071 } 4072 /* Mark the single-word operation live. */ 4073 nb_oargs = 1; 4074 goto do_not_remove; 4075 4076 default: 4077 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */ 4078 nb_iargs = def->nb_iargs; 4079 nb_oargs = def->nb_oargs; 4080 4081 /* Test if the operation can be removed because all 4082 its outputs are dead. We assume that nb_oargs == 0 4083 implies side effects */ 4084 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) { 4085 for (i = 0; i < nb_oargs; i++) { 4086 if (arg_temp(op->args[i])->state != TS_DEAD) { 4087 goto do_not_remove; 4088 } 4089 } 4090 goto do_remove; 4091 } 4092 goto do_not_remove; 4093 4094 do_remove: 4095 tcg_op_remove(s, op); 4096 break; 4097 4098 do_not_remove: 4099 for (i = 0; i < nb_oargs; i++) { 4100 ts = arg_temp(op->args[i]); 4101 4102 /* Remember the preference of the uses that followed. */ 4103 if (i < ARRAY_SIZE(op->output_pref)) { 4104 op->output_pref[i] = *la_temp_pref(ts); 4105 } 4106 4107 /* Output args are dead. */ 4108 if (ts->state & TS_DEAD) { 4109 arg_life |= DEAD_ARG << i; 4110 } 4111 if (ts->state & TS_MEM) { 4112 arg_life |= SYNC_ARG << i; 4113 } 4114 ts->state = TS_DEAD; 4115 la_reset_pref(ts); 4116 } 4117 4118 /* If end of basic block, update. */ 4119 if (def->flags & TCG_OPF_BB_EXIT) { 4120 la_func_end(s, nb_globals, nb_temps); 4121 } else if (def->flags & TCG_OPF_COND_BRANCH) { 4122 la_bb_sync(s, nb_globals, nb_temps); 4123 } else if (def->flags & TCG_OPF_BB_END) { 4124 la_bb_end(s, nb_globals, nb_temps); 4125 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) { 4126 la_global_sync(s, nb_globals); 4127 if (def->flags & TCG_OPF_CALL_CLOBBER) { 4128 la_cross_call(s, nb_temps); 4129 } 4130 } 4131 4132 /* Record arguments that die in this opcode. */ 4133 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 4134 ts = arg_temp(op->args[i]); 4135 if (ts->state & TS_DEAD) { 4136 arg_life |= DEAD_ARG << i; 4137 } 4138 } 4139 4140 /* Input arguments are live for preceding opcodes. */ 4141 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 4142 ts = arg_temp(op->args[i]); 4143 if (ts->state & TS_DEAD) { 4144 /* For operands that were dead, initially allow 4145 all regs for the type. */ 4146 *la_temp_pref(ts) = tcg_target_available_regs[ts->type]; 4147 ts->state &= ~TS_DEAD; 4148 } 4149 } 4150 4151 /* Incorporate constraints for this operand. */ 4152 switch (opc) { 4153 case INDEX_op_mov: 4154 /* Note that these are TCG_OPF_NOT_PRESENT and do not 4155 have proper constraints. That said, special case 4156 moves to propagate preferences backward. */ 4157 if (IS_DEAD_ARG(1)) { 4158 *la_temp_pref(arg_temp(op->args[0])) 4159 = *la_temp_pref(arg_temp(op->args[1])); 4160 } 4161 break; 4162 4163 default: 4164 args_ct = opcode_args_ct(op); 4165 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 4166 const TCGArgConstraint *ct = &args_ct[i]; 4167 TCGRegSet set, *pset; 4168 4169 ts = arg_temp(op->args[i]); 4170 pset = la_temp_pref(ts); 4171 set = *pset; 4172 4173 set &= ct->regs; 4174 if (ct->ialias) { 4175 set &= output_pref(op, ct->alias_index); 4176 } 4177 /* If the combination is not possible, restart. */ 4178 if (set == 0) { 4179 set = ct->regs; 4180 } 4181 *pset = set; 4182 } 4183 break; 4184 } 4185 break; 4186 } 4187 op->life = arg_life; 4188 } 4189 } 4190 4191 /* Liveness analysis: Convert indirect regs to direct temporaries. */ 4192 static bool __attribute__((noinline)) 4193 liveness_pass_2(TCGContext *s) 4194 { 4195 int nb_globals = s->nb_globals; 4196 int nb_temps, i; 4197 bool changes = false; 4198 TCGOp *op, *op_next; 4199 4200 /* Create a temporary for each indirect global. */ 4201 for (i = 0; i < nb_globals; ++i) { 4202 TCGTemp *its = &s->temps[i]; 4203 if (its->indirect_reg) { 4204 TCGTemp *dts = tcg_temp_alloc(s); 4205 dts->type = its->type; 4206 dts->base_type = its->base_type; 4207 dts->temp_subindex = its->temp_subindex; 4208 dts->kind = TEMP_EBB; 4209 its->state_ptr = dts; 4210 } else { 4211 its->state_ptr = NULL; 4212 } 4213 /* All globals begin dead. */ 4214 its->state = TS_DEAD; 4215 } 4216 for (nb_temps = s->nb_temps; i < nb_temps; ++i) { 4217 TCGTemp *its = &s->temps[i]; 4218 its->state_ptr = NULL; 4219 its->state = TS_DEAD; 4220 } 4221 4222 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) { 4223 TCGOpcode opc = op->opc; 4224 const TCGOpDef *def = &tcg_op_defs[opc]; 4225 TCGLifeData arg_life = op->life; 4226 int nb_iargs, nb_oargs, call_flags; 4227 TCGTemp *arg_ts, *dir_ts; 4228 4229 if (opc == INDEX_op_call) { 4230 nb_oargs = TCGOP_CALLO(op); 4231 nb_iargs = TCGOP_CALLI(op); 4232 call_flags = tcg_call_flags(op); 4233 } else { 4234 nb_iargs = def->nb_iargs; 4235 nb_oargs = def->nb_oargs; 4236 4237 /* Set flags similar to how calls require. */ 4238 if (def->flags & TCG_OPF_COND_BRANCH) { 4239 /* Like reading globals: sync_globals */ 4240 call_flags = TCG_CALL_NO_WRITE_GLOBALS; 4241 } else if (def->flags & TCG_OPF_BB_END) { 4242 /* Like writing globals: save_globals */ 4243 call_flags = 0; 4244 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) { 4245 /* Like reading globals: sync_globals */ 4246 call_flags = TCG_CALL_NO_WRITE_GLOBALS; 4247 } else { 4248 /* No effect on globals. */ 4249 call_flags = (TCG_CALL_NO_READ_GLOBALS | 4250 TCG_CALL_NO_WRITE_GLOBALS); 4251 } 4252 } 4253 4254 /* Make sure that input arguments are available. */ 4255 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { 4256 arg_ts = arg_temp(op->args[i]); 4257 dir_ts = arg_ts->state_ptr; 4258 if (dir_ts && arg_ts->state == TS_DEAD) { 4259 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32 4260 ? INDEX_op_ld_i32 4261 : INDEX_op_ld_i64); 4262 TCGOp *lop = tcg_op_insert_before(s, op, lopc, 4263 arg_ts->type, 3); 4264 4265 lop->args[0] = temp_arg(dir_ts); 4266 lop->args[1] = temp_arg(arg_ts->mem_base); 4267 lop->args[2] = arg_ts->mem_offset; 4268 4269 /* Loaded, but synced with memory. */ 4270 arg_ts->state = TS_MEM; 4271 } 4272 } 4273 4274 /* Perform input replacement, and mark inputs that became dead. 4275 No action is required except keeping temp_state up to date 4276 so that we reload when needed. */ 4277 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { 4278 arg_ts = arg_temp(op->args[i]); 4279 dir_ts = arg_ts->state_ptr; 4280 if (dir_ts) { 4281 op->args[i] = temp_arg(dir_ts); 4282 changes = true; 4283 if (IS_DEAD_ARG(i)) { 4284 arg_ts->state = TS_DEAD; 4285 } 4286 } 4287 } 4288 4289 /* Liveness analysis should ensure that the following are 4290 all correct, for call sites and basic block end points. */ 4291 if (call_flags & TCG_CALL_NO_READ_GLOBALS) { 4292 /* Nothing to do */ 4293 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) { 4294 for (i = 0; i < nb_globals; ++i) { 4295 /* Liveness should see that globals are synced back, 4296 that is, either TS_DEAD or TS_MEM. */ 4297 arg_ts = &s->temps[i]; 4298 tcg_debug_assert(arg_ts->state_ptr == 0 4299 || arg_ts->state != 0); 4300 } 4301 } else { 4302 for (i = 0; i < nb_globals; ++i) { 4303 /* Liveness should see that globals are saved back, 4304 that is, TS_DEAD, waiting to be reloaded. */ 4305 arg_ts = &s->temps[i]; 4306 tcg_debug_assert(arg_ts->state_ptr == 0 4307 || arg_ts->state == TS_DEAD); 4308 } 4309 } 4310 4311 /* Outputs become available. */ 4312 if (opc == INDEX_op_mov) { 4313 arg_ts = arg_temp(op->args[0]); 4314 dir_ts = arg_ts->state_ptr; 4315 if (dir_ts) { 4316 op->args[0] = temp_arg(dir_ts); 4317 changes = true; 4318 4319 /* The output is now live and modified. */ 4320 arg_ts->state = 0; 4321 4322 if (NEED_SYNC_ARG(0)) { 4323 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32 4324 ? INDEX_op_st_i32 4325 : INDEX_op_st_i64); 4326 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 4327 arg_ts->type, 3); 4328 TCGTemp *out_ts = dir_ts; 4329 4330 if (IS_DEAD_ARG(0)) { 4331 out_ts = arg_temp(op->args[1]); 4332 arg_ts->state = TS_DEAD; 4333 tcg_op_remove(s, op); 4334 } else { 4335 arg_ts->state = TS_MEM; 4336 } 4337 4338 sop->args[0] = temp_arg(out_ts); 4339 sop->args[1] = temp_arg(arg_ts->mem_base); 4340 sop->args[2] = arg_ts->mem_offset; 4341 } else { 4342 tcg_debug_assert(!IS_DEAD_ARG(0)); 4343 } 4344 } 4345 } else { 4346 for (i = 0; i < nb_oargs; i++) { 4347 arg_ts = arg_temp(op->args[i]); 4348 dir_ts = arg_ts->state_ptr; 4349 if (!dir_ts) { 4350 continue; 4351 } 4352 op->args[i] = temp_arg(dir_ts); 4353 changes = true; 4354 4355 /* The output is now live and modified. */ 4356 arg_ts->state = 0; 4357 4358 /* Sync outputs upon their last write. */ 4359 if (NEED_SYNC_ARG(i)) { 4360 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32 4361 ? INDEX_op_st_i32 4362 : INDEX_op_st_i64); 4363 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 4364 arg_ts->type, 3); 4365 4366 sop->args[0] = temp_arg(dir_ts); 4367 sop->args[1] = temp_arg(arg_ts->mem_base); 4368 sop->args[2] = arg_ts->mem_offset; 4369 4370 arg_ts->state = TS_MEM; 4371 } 4372 /* Drop outputs that are dead. */ 4373 if (IS_DEAD_ARG(i)) { 4374 arg_ts->state = TS_DEAD; 4375 } 4376 } 4377 } 4378 } 4379 4380 return changes; 4381 } 4382 4383 static void temp_allocate_frame(TCGContext *s, TCGTemp *ts) 4384 { 4385 intptr_t off; 4386 int size, align; 4387 4388 /* When allocating an object, look at the full type. */ 4389 size = tcg_type_size(ts->base_type); 4390 switch (ts->base_type) { 4391 case TCG_TYPE_I32: 4392 align = 4; 4393 break; 4394 case TCG_TYPE_I64: 4395 case TCG_TYPE_V64: 4396 align = 8; 4397 break; 4398 case TCG_TYPE_I128: 4399 case TCG_TYPE_V128: 4400 case TCG_TYPE_V256: 4401 /* 4402 * Note that we do not require aligned storage for V256, 4403 * and that we provide alignment for I128 to match V128, 4404 * even if that's above what the host ABI requires. 4405 */ 4406 align = 16; 4407 break; 4408 default: 4409 g_assert_not_reached(); 4410 } 4411 4412 /* 4413 * Assume the stack is sufficiently aligned. 4414 * This affects e.g. ARM NEON, where we have 8 byte stack alignment 4415 * and do not require 16 byte vector alignment. This seems slightly 4416 * easier than fully parameterizing the above switch statement. 4417 */ 4418 align = MIN(TCG_TARGET_STACK_ALIGN, align); 4419 off = ROUND_UP(s->current_frame_offset, align); 4420 4421 /* If we've exhausted the stack frame, restart with a smaller TB. */ 4422 if (off + size > s->frame_end) { 4423 tcg_raise_tb_overflow(s); 4424 } 4425 s->current_frame_offset = off + size; 4426 #if defined(__sparc__) 4427 off += TCG_TARGET_STACK_BIAS; 4428 #endif 4429 4430 /* If the object was subdivided, assign memory to all the parts. */ 4431 if (ts->base_type != ts->type) { 4432 int part_size = tcg_type_size(ts->type); 4433 int part_count = size / part_size; 4434 4435 /* 4436 * Each part is allocated sequentially in tcg_temp_new_internal. 4437 * Jump back to the first part by subtracting the current index. 4438 */ 4439 ts -= ts->temp_subindex; 4440 for (int i = 0; i < part_count; ++i) { 4441 ts[i].mem_offset = off + i * part_size; 4442 ts[i].mem_base = s->frame_temp; 4443 ts[i].mem_allocated = 1; 4444 } 4445 } else { 4446 ts->mem_offset = off; 4447 ts->mem_base = s->frame_temp; 4448 ts->mem_allocated = 1; 4449 } 4450 } 4451 4452 /* Assign @reg to @ts, and update reg_to_temp[]. */ 4453 static void set_temp_val_reg(TCGContext *s, TCGTemp *ts, TCGReg reg) 4454 { 4455 if (ts->val_type == TEMP_VAL_REG) { 4456 TCGReg old = ts->reg; 4457 tcg_debug_assert(s->reg_to_temp[old] == ts); 4458 if (old == reg) { 4459 return; 4460 } 4461 s->reg_to_temp[old] = NULL; 4462 } 4463 tcg_debug_assert(s->reg_to_temp[reg] == NULL); 4464 s->reg_to_temp[reg] = ts; 4465 ts->val_type = TEMP_VAL_REG; 4466 ts->reg = reg; 4467 } 4468 4469 /* Assign a non-register value type to @ts, and update reg_to_temp[]. */ 4470 static void set_temp_val_nonreg(TCGContext *s, TCGTemp *ts, TCGTempVal type) 4471 { 4472 tcg_debug_assert(type != TEMP_VAL_REG); 4473 if (ts->val_type == TEMP_VAL_REG) { 4474 TCGReg reg = ts->reg; 4475 tcg_debug_assert(s->reg_to_temp[reg] == ts); 4476 s->reg_to_temp[reg] = NULL; 4477 } 4478 ts->val_type = type; 4479 } 4480 4481 static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet); 4482 4483 /* Mark a temporary as free or dead. If 'free_or_dead' is negative, 4484 mark it free; otherwise mark it dead. */ 4485 static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead) 4486 { 4487 TCGTempVal new_type; 4488 4489 switch (ts->kind) { 4490 case TEMP_FIXED: 4491 return; 4492 case TEMP_GLOBAL: 4493 case TEMP_TB: 4494 new_type = TEMP_VAL_MEM; 4495 break; 4496 case TEMP_EBB: 4497 new_type = free_or_dead < 0 ? TEMP_VAL_MEM : TEMP_VAL_DEAD; 4498 break; 4499 case TEMP_CONST: 4500 new_type = TEMP_VAL_CONST; 4501 break; 4502 default: 4503 g_assert_not_reached(); 4504 } 4505 set_temp_val_nonreg(s, ts, new_type); 4506 } 4507 4508 /* Mark a temporary as dead. */ 4509 static inline void temp_dead(TCGContext *s, TCGTemp *ts) 4510 { 4511 temp_free_or_dead(s, ts, 1); 4512 } 4513 4514 /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary 4515 registers needs to be allocated to store a constant. If 'free_or_dead' 4516 is non-zero, subsequently release the temporary; if it is positive, the 4517 temp is dead; if it is negative, the temp is free. */ 4518 static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs, 4519 TCGRegSet preferred_regs, int free_or_dead) 4520 { 4521 if (!temp_readonly(ts) && !ts->mem_coherent) { 4522 if (!ts->mem_allocated) { 4523 temp_allocate_frame(s, ts); 4524 } 4525 switch (ts->val_type) { 4526 case TEMP_VAL_CONST: 4527 /* If we're going to free the temp immediately, then we won't 4528 require it later in a register, so attempt to store the 4529 constant to memory directly. */ 4530 if (free_or_dead 4531 && tcg_out_sti(s, ts->type, ts->val, 4532 ts->mem_base->reg, ts->mem_offset)) { 4533 break; 4534 } 4535 temp_load(s, ts, tcg_target_available_regs[ts->type], 4536 allocated_regs, preferred_regs); 4537 /* fallthrough */ 4538 4539 case TEMP_VAL_REG: 4540 tcg_out_st(s, ts->type, ts->reg, 4541 ts->mem_base->reg, ts->mem_offset); 4542 break; 4543 4544 case TEMP_VAL_MEM: 4545 break; 4546 4547 case TEMP_VAL_DEAD: 4548 default: 4549 g_assert_not_reached(); 4550 } 4551 ts->mem_coherent = 1; 4552 } 4553 if (free_or_dead) { 4554 temp_free_or_dead(s, ts, free_or_dead); 4555 } 4556 } 4557 4558 /* free register 'reg' by spilling the corresponding temporary if necessary */ 4559 static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs) 4560 { 4561 TCGTemp *ts = s->reg_to_temp[reg]; 4562 if (ts != NULL) { 4563 temp_sync(s, ts, allocated_regs, 0, -1); 4564 } 4565 } 4566 4567 /** 4568 * tcg_reg_alloc: 4569 * @required_regs: Set of registers in which we must allocate. 4570 * @allocated_regs: Set of registers which must be avoided. 4571 * @preferred_regs: Set of registers we should prefer. 4572 * @rev: True if we search the registers in "indirect" order. 4573 * 4574 * The allocated register must be in @required_regs & ~@allocated_regs, 4575 * but if we can put it in @preferred_regs we may save a move later. 4576 */ 4577 static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs, 4578 TCGRegSet allocated_regs, 4579 TCGRegSet preferred_regs, bool rev) 4580 { 4581 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order); 4582 TCGRegSet reg_ct[2]; 4583 const int *order; 4584 4585 reg_ct[1] = required_regs & ~allocated_regs; 4586 tcg_debug_assert(reg_ct[1] != 0); 4587 reg_ct[0] = reg_ct[1] & preferred_regs; 4588 4589 /* Skip the preferred_regs option if it cannot be satisfied, 4590 or if the preference made no difference. */ 4591 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1]; 4592 4593 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order; 4594 4595 /* Try free registers, preferences first. */ 4596 for (j = f; j < 2; j++) { 4597 TCGRegSet set = reg_ct[j]; 4598 4599 if (tcg_regset_single(set)) { 4600 /* One register in the set. */ 4601 TCGReg reg = tcg_regset_first(set); 4602 if (s->reg_to_temp[reg] == NULL) { 4603 return reg; 4604 } 4605 } else { 4606 for (i = 0; i < n; i++) { 4607 TCGReg reg = order[i]; 4608 if (s->reg_to_temp[reg] == NULL && 4609 tcg_regset_test_reg(set, reg)) { 4610 return reg; 4611 } 4612 } 4613 } 4614 } 4615 4616 /* We must spill something. */ 4617 for (j = f; j < 2; j++) { 4618 TCGRegSet set = reg_ct[j]; 4619 4620 if (tcg_regset_single(set)) { 4621 /* One register in the set. */ 4622 TCGReg reg = tcg_regset_first(set); 4623 tcg_reg_free(s, reg, allocated_regs); 4624 return reg; 4625 } else { 4626 for (i = 0; i < n; i++) { 4627 TCGReg reg = order[i]; 4628 if (tcg_regset_test_reg(set, reg)) { 4629 tcg_reg_free(s, reg, allocated_regs); 4630 return reg; 4631 } 4632 } 4633 } 4634 } 4635 4636 g_assert_not_reached(); 4637 } 4638 4639 static TCGReg tcg_reg_alloc_pair(TCGContext *s, TCGRegSet required_regs, 4640 TCGRegSet allocated_regs, 4641 TCGRegSet preferred_regs, bool rev) 4642 { 4643 int i, j, k, fmin, n = ARRAY_SIZE(tcg_target_reg_alloc_order); 4644 TCGRegSet reg_ct[2]; 4645 const int *order; 4646 4647 /* Ensure that if I is not in allocated_regs, I+1 is not either. */ 4648 reg_ct[1] = required_regs & ~(allocated_regs | (allocated_regs >> 1)); 4649 tcg_debug_assert(reg_ct[1] != 0); 4650 reg_ct[0] = reg_ct[1] & preferred_regs; 4651 4652 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order; 4653 4654 /* 4655 * Skip the preferred_regs option if it cannot be satisfied, 4656 * or if the preference made no difference. 4657 */ 4658 k = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1]; 4659 4660 /* 4661 * Minimize the number of flushes by looking for 2 free registers first, 4662 * then a single flush, then two flushes. 4663 */ 4664 for (fmin = 2; fmin >= 0; fmin--) { 4665 for (j = k; j < 2; j++) { 4666 TCGRegSet set = reg_ct[j]; 4667 4668 for (i = 0; i < n; i++) { 4669 TCGReg reg = order[i]; 4670 4671 if (tcg_regset_test_reg(set, reg)) { 4672 int f = !s->reg_to_temp[reg] + !s->reg_to_temp[reg + 1]; 4673 if (f >= fmin) { 4674 tcg_reg_free(s, reg, allocated_regs); 4675 tcg_reg_free(s, reg + 1, allocated_regs); 4676 return reg; 4677 } 4678 } 4679 } 4680 } 4681 } 4682 g_assert_not_reached(); 4683 } 4684 4685 /* Make sure the temporary is in a register. If needed, allocate the register 4686 from DESIRED while avoiding ALLOCATED. */ 4687 static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs, 4688 TCGRegSet allocated_regs, TCGRegSet preferred_regs) 4689 { 4690 TCGReg reg; 4691 4692 switch (ts->val_type) { 4693 case TEMP_VAL_REG: 4694 return; 4695 case TEMP_VAL_CONST: 4696 reg = tcg_reg_alloc(s, desired_regs, allocated_regs, 4697 preferred_regs, ts->indirect_base); 4698 if (ts->type <= TCG_TYPE_I64) { 4699 tcg_out_movi(s, ts->type, reg, ts->val); 4700 } else { 4701 uint64_t val = ts->val; 4702 MemOp vece = MO_64; 4703 4704 /* 4705 * Find the minimal vector element that matches the constant. 4706 * The targets will, in general, have to do this search anyway, 4707 * do this generically. 4708 */ 4709 if (val == dup_const(MO_8, val)) { 4710 vece = MO_8; 4711 } else if (val == dup_const(MO_16, val)) { 4712 vece = MO_16; 4713 } else if (val == dup_const(MO_32, val)) { 4714 vece = MO_32; 4715 } 4716 4717 tcg_out_dupi_vec(s, ts->type, vece, reg, ts->val); 4718 } 4719 ts->mem_coherent = 0; 4720 break; 4721 case TEMP_VAL_MEM: 4722 if (!ts->mem_allocated) { 4723 temp_allocate_frame(s, ts); 4724 } 4725 reg = tcg_reg_alloc(s, desired_regs, allocated_regs, 4726 preferred_regs, ts->indirect_base); 4727 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset); 4728 ts->mem_coherent = 1; 4729 break; 4730 case TEMP_VAL_DEAD: 4731 default: 4732 g_assert_not_reached(); 4733 } 4734 set_temp_val_reg(s, ts, reg); 4735 } 4736 4737 /* Save a temporary to memory. 'allocated_regs' is used in case a 4738 temporary registers needs to be allocated to store a constant. */ 4739 static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs) 4740 { 4741 /* The liveness analysis already ensures that globals are back 4742 in memory. Keep an tcg_debug_assert for safety. */ 4743 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || temp_readonly(ts)); 4744 } 4745 4746 /* save globals to their canonical location and assume they can be 4747 modified be the following code. 'allocated_regs' is used in case a 4748 temporary registers needs to be allocated to store a constant. */ 4749 static void save_globals(TCGContext *s, TCGRegSet allocated_regs) 4750 { 4751 int i, n; 4752 4753 for (i = 0, n = s->nb_globals; i < n; i++) { 4754 temp_save(s, &s->temps[i], allocated_regs); 4755 } 4756 } 4757 4758 /* sync globals to their canonical location and assume they can be 4759 read by the following code. 'allocated_regs' is used in case a 4760 temporary registers needs to be allocated to store a constant. */ 4761 static void sync_globals(TCGContext *s, TCGRegSet allocated_regs) 4762 { 4763 int i, n; 4764 4765 for (i = 0, n = s->nb_globals; i < n; i++) { 4766 TCGTemp *ts = &s->temps[i]; 4767 tcg_debug_assert(ts->val_type != TEMP_VAL_REG 4768 || ts->kind == TEMP_FIXED 4769 || ts->mem_coherent); 4770 } 4771 } 4772 4773 /* at the end of a basic block, we assume all temporaries are dead and 4774 all globals are stored at their canonical location. */ 4775 static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs) 4776 { 4777 int i; 4778 4779 for (i = s->nb_globals; i < s->nb_temps; i++) { 4780 TCGTemp *ts = &s->temps[i]; 4781 4782 switch (ts->kind) { 4783 case TEMP_TB: 4784 temp_save(s, ts, allocated_regs); 4785 break; 4786 case TEMP_EBB: 4787 /* The liveness analysis already ensures that temps are dead. 4788 Keep an tcg_debug_assert for safety. */ 4789 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD); 4790 break; 4791 case TEMP_CONST: 4792 /* Similarly, we should have freed any allocated register. */ 4793 tcg_debug_assert(ts->val_type == TEMP_VAL_CONST); 4794 break; 4795 default: 4796 g_assert_not_reached(); 4797 } 4798 } 4799 4800 save_globals(s, allocated_regs); 4801 } 4802 4803 /* 4804 * At a conditional branch, we assume all temporaries are dead unless 4805 * explicitly live-across-conditional-branch; all globals and local 4806 * temps are synced to their location. 4807 */ 4808 static void tcg_reg_alloc_cbranch(TCGContext *s, TCGRegSet allocated_regs) 4809 { 4810 sync_globals(s, allocated_regs); 4811 4812 for (int i = s->nb_globals; i < s->nb_temps; i++) { 4813 TCGTemp *ts = &s->temps[i]; 4814 /* 4815 * The liveness analysis already ensures that temps are dead. 4816 * Keep tcg_debug_asserts for safety. 4817 */ 4818 switch (ts->kind) { 4819 case TEMP_TB: 4820 tcg_debug_assert(ts->val_type != TEMP_VAL_REG || ts->mem_coherent); 4821 break; 4822 case TEMP_EBB: 4823 case TEMP_CONST: 4824 break; 4825 default: 4826 g_assert_not_reached(); 4827 } 4828 } 4829 } 4830 4831 /* 4832 * Specialized code generation for INDEX_op_mov_* with a constant. 4833 */ 4834 static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots, 4835 tcg_target_ulong val, TCGLifeData arg_life, 4836 TCGRegSet preferred_regs) 4837 { 4838 /* ENV should not be modified. */ 4839 tcg_debug_assert(!temp_readonly(ots)); 4840 4841 /* The movi is not explicitly generated here. */ 4842 set_temp_val_nonreg(s, ots, TEMP_VAL_CONST); 4843 ots->val = val; 4844 ots->mem_coherent = 0; 4845 if (NEED_SYNC_ARG(0)) { 4846 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0)); 4847 } else if (IS_DEAD_ARG(0)) { 4848 temp_dead(s, ots); 4849 } 4850 } 4851 4852 /* 4853 * Specialized code generation for INDEX_op_mov_*. 4854 */ 4855 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op) 4856 { 4857 const TCGLifeData arg_life = op->life; 4858 TCGRegSet allocated_regs, preferred_regs; 4859 TCGTemp *ts, *ots; 4860 TCGType otype, itype; 4861 TCGReg oreg, ireg; 4862 4863 allocated_regs = s->reserved_regs; 4864 preferred_regs = output_pref(op, 0); 4865 ots = arg_temp(op->args[0]); 4866 ts = arg_temp(op->args[1]); 4867 4868 /* ENV should not be modified. */ 4869 tcg_debug_assert(!temp_readonly(ots)); 4870 4871 /* Note that otype != itype for no-op truncation. */ 4872 otype = ots->type; 4873 itype = ts->type; 4874 4875 if (ts->val_type == TEMP_VAL_CONST) { 4876 /* propagate constant or generate sti */ 4877 tcg_target_ulong val = ts->val; 4878 if (IS_DEAD_ARG(1)) { 4879 temp_dead(s, ts); 4880 } 4881 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs); 4882 return; 4883 } 4884 4885 /* If the source value is in memory we're going to be forced 4886 to have it in a register in order to perform the copy. Copy 4887 the SOURCE value into its own register first, that way we 4888 don't have to reload SOURCE the next time it is used. */ 4889 if (ts->val_type == TEMP_VAL_MEM) { 4890 temp_load(s, ts, tcg_target_available_regs[itype], 4891 allocated_regs, preferred_regs); 4892 } 4893 tcg_debug_assert(ts->val_type == TEMP_VAL_REG); 4894 ireg = ts->reg; 4895 4896 if (IS_DEAD_ARG(0)) { 4897 /* mov to a non-saved dead register makes no sense (even with 4898 liveness analysis disabled). */ 4899 tcg_debug_assert(NEED_SYNC_ARG(0)); 4900 if (!ots->mem_allocated) { 4901 temp_allocate_frame(s, ots); 4902 } 4903 tcg_out_st(s, otype, ireg, ots->mem_base->reg, ots->mem_offset); 4904 if (IS_DEAD_ARG(1)) { 4905 temp_dead(s, ts); 4906 } 4907 temp_dead(s, ots); 4908 return; 4909 } 4910 4911 if (IS_DEAD_ARG(1) && ts->kind != TEMP_FIXED) { 4912 /* 4913 * The mov can be suppressed. Kill input first, so that it 4914 * is unlinked from reg_to_temp, then set the output to the 4915 * reg that we saved from the input. 4916 */ 4917 temp_dead(s, ts); 4918 oreg = ireg; 4919 } else { 4920 if (ots->val_type == TEMP_VAL_REG) { 4921 oreg = ots->reg; 4922 } else { 4923 /* Make sure to not spill the input register during allocation. */ 4924 oreg = tcg_reg_alloc(s, tcg_target_available_regs[otype], 4925 allocated_regs | ((TCGRegSet)1 << ireg), 4926 preferred_regs, ots->indirect_base); 4927 } 4928 if (!tcg_out_mov(s, otype, oreg, ireg)) { 4929 /* 4930 * Cross register class move not supported. 4931 * Store the source register into the destination slot 4932 * and leave the destination temp as TEMP_VAL_MEM. 4933 */ 4934 assert(!temp_readonly(ots)); 4935 if (!ts->mem_allocated) { 4936 temp_allocate_frame(s, ots); 4937 } 4938 tcg_out_st(s, ts->type, ireg, ots->mem_base->reg, ots->mem_offset); 4939 set_temp_val_nonreg(s, ts, TEMP_VAL_MEM); 4940 ots->mem_coherent = 1; 4941 return; 4942 } 4943 } 4944 set_temp_val_reg(s, ots, oreg); 4945 ots->mem_coherent = 0; 4946 4947 if (NEED_SYNC_ARG(0)) { 4948 temp_sync(s, ots, allocated_regs, 0, 0); 4949 } 4950 } 4951 4952 /* 4953 * Specialized code generation for INDEX_op_dup_vec. 4954 */ 4955 static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op) 4956 { 4957 const TCGLifeData arg_life = op->life; 4958 TCGRegSet dup_out_regs, dup_in_regs; 4959 const TCGArgConstraint *dup_args_ct; 4960 TCGTemp *its, *ots; 4961 TCGType itype, vtype; 4962 unsigned vece; 4963 int lowpart_ofs; 4964 bool ok; 4965 4966 ots = arg_temp(op->args[0]); 4967 its = arg_temp(op->args[1]); 4968 4969 /* ENV should not be modified. */ 4970 tcg_debug_assert(!temp_readonly(ots)); 4971 4972 itype = its->type; 4973 vece = TCGOP_VECE(op); 4974 vtype = TCGOP_TYPE(op); 4975 4976 if (its->val_type == TEMP_VAL_CONST) { 4977 /* Propagate constant via movi -> dupi. */ 4978 tcg_target_ulong val = its->val; 4979 if (IS_DEAD_ARG(1)) { 4980 temp_dead(s, its); 4981 } 4982 tcg_reg_alloc_do_movi(s, ots, val, arg_life, output_pref(op, 0)); 4983 return; 4984 } 4985 4986 dup_args_ct = opcode_args_ct(op); 4987 dup_out_regs = dup_args_ct[0].regs; 4988 dup_in_regs = dup_args_ct[1].regs; 4989 4990 /* Allocate the output register now. */ 4991 if (ots->val_type != TEMP_VAL_REG) { 4992 TCGRegSet allocated_regs = s->reserved_regs; 4993 TCGReg oreg; 4994 4995 if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) { 4996 /* Make sure to not spill the input register. */ 4997 tcg_regset_set_reg(allocated_regs, its->reg); 4998 } 4999 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs, 5000 output_pref(op, 0), ots->indirect_base); 5001 set_temp_val_reg(s, ots, oreg); 5002 } 5003 5004 switch (its->val_type) { 5005 case TEMP_VAL_REG: 5006 /* 5007 * The dup constriaints must be broad, covering all possible VECE. 5008 * However, tcg_op_dup_vec() gets to see the VECE and we allow it 5009 * to fail, indicating that extra moves are required for that case. 5010 */ 5011 if (tcg_regset_test_reg(dup_in_regs, its->reg)) { 5012 if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) { 5013 goto done; 5014 } 5015 /* Try again from memory or a vector input register. */ 5016 } 5017 if (!its->mem_coherent) { 5018 /* 5019 * The input register is not synced, and so an extra store 5020 * would be required to use memory. Attempt an integer-vector 5021 * register move first. We do not have a TCGRegSet for this. 5022 */ 5023 if (tcg_out_mov(s, itype, ots->reg, its->reg)) { 5024 break; 5025 } 5026 /* Sync the temp back to its slot and load from there. */ 5027 temp_sync(s, its, s->reserved_regs, 0, 0); 5028 } 5029 /* fall through */ 5030 5031 case TEMP_VAL_MEM: 5032 lowpart_ofs = 0; 5033 if (HOST_BIG_ENDIAN) { 5034 lowpart_ofs = tcg_type_size(itype) - (1 << vece); 5035 } 5036 if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg, 5037 its->mem_offset + lowpart_ofs)) { 5038 goto done; 5039 } 5040 /* Load the input into the destination vector register. */ 5041 tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset); 5042 break; 5043 5044 default: 5045 g_assert_not_reached(); 5046 } 5047 5048 /* We now have a vector input register, so dup must succeed. */ 5049 ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg); 5050 tcg_debug_assert(ok); 5051 5052 done: 5053 ots->mem_coherent = 0; 5054 if (IS_DEAD_ARG(1)) { 5055 temp_dead(s, its); 5056 } 5057 if (NEED_SYNC_ARG(0)) { 5058 temp_sync(s, ots, s->reserved_regs, 0, 0); 5059 } 5060 if (IS_DEAD_ARG(0)) { 5061 temp_dead(s, ots); 5062 } 5063 } 5064 5065 static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op) 5066 { 5067 const TCGLifeData arg_life = op->life; 5068 const TCGOpDef * const def = &tcg_op_defs[op->opc]; 5069 TCGRegSet i_allocated_regs; 5070 TCGRegSet o_allocated_regs; 5071 int i, k, nb_iargs, nb_oargs; 5072 TCGReg reg; 5073 TCGArg arg; 5074 const TCGArgConstraint *args_ct; 5075 const TCGArgConstraint *arg_ct; 5076 TCGTemp *ts; 5077 TCGArg new_args[TCG_MAX_OP_ARGS]; 5078 int const_args[TCG_MAX_OP_ARGS]; 5079 TCGCond op_cond; 5080 5081 nb_oargs = def->nb_oargs; 5082 nb_iargs = def->nb_iargs; 5083 5084 /* copy constants */ 5085 memcpy(new_args + nb_oargs + nb_iargs, 5086 op->args + nb_oargs + nb_iargs, 5087 sizeof(TCGArg) * def->nb_cargs); 5088 5089 i_allocated_regs = s->reserved_regs; 5090 o_allocated_regs = s->reserved_regs; 5091 5092 switch (op->opc) { 5093 case INDEX_op_brcond: 5094 op_cond = op->args[2]; 5095 break; 5096 case INDEX_op_setcond: 5097 case INDEX_op_negsetcond: 5098 case INDEX_op_cmp_vec: 5099 op_cond = op->args[3]; 5100 break; 5101 case INDEX_op_brcond2_i32: 5102 op_cond = op->args[4]; 5103 break; 5104 case INDEX_op_movcond: 5105 case INDEX_op_setcond2_i32: 5106 case INDEX_op_cmpsel_vec: 5107 op_cond = op->args[5]; 5108 break; 5109 default: 5110 /* No condition within opcode. */ 5111 op_cond = TCG_COND_ALWAYS; 5112 break; 5113 } 5114 5115 args_ct = opcode_args_ct(op); 5116 5117 /* satisfy input constraints */ 5118 for (k = 0; k < nb_iargs; k++) { 5119 TCGRegSet i_preferred_regs, i_required_regs; 5120 bool allocate_new_reg, copyto_new_reg; 5121 TCGTemp *ts2; 5122 int i1, i2; 5123 5124 i = args_ct[nb_oargs + k].sort_index; 5125 arg = op->args[i]; 5126 arg_ct = &args_ct[i]; 5127 ts = arg_temp(arg); 5128 5129 if (ts->val_type == TEMP_VAL_CONST) { 5130 #ifdef TCG_REG_ZERO 5131 if (ts->val == 0 && (arg_ct->ct & TCG_CT_REG_ZERO)) { 5132 /* Hardware zero register: indicate register via non-const. */ 5133 const_args[i] = 0; 5134 new_args[i] = TCG_REG_ZERO; 5135 continue; 5136 } 5137 #endif 5138 5139 if (tcg_target_const_match(ts->val, arg_ct->ct, ts->type, 5140 op_cond, TCGOP_VECE(op))) { 5141 /* constant is OK for instruction */ 5142 const_args[i] = 1; 5143 new_args[i] = ts->val; 5144 continue; 5145 } 5146 } 5147 5148 reg = ts->reg; 5149 i_preferred_regs = 0; 5150 i_required_regs = arg_ct->regs; 5151 allocate_new_reg = false; 5152 copyto_new_reg = false; 5153 5154 switch (arg_ct->pair) { 5155 case 0: /* not paired */ 5156 if (arg_ct->ialias) { 5157 i_preferred_regs = output_pref(op, arg_ct->alias_index); 5158 5159 /* 5160 * If the input is readonly, then it cannot also be an 5161 * output and aliased to itself. If the input is not 5162 * dead after the instruction, we must allocate a new 5163 * register and move it. 5164 */ 5165 if (temp_readonly(ts) || !IS_DEAD_ARG(i) 5166 || args_ct[arg_ct->alias_index].newreg) { 5167 allocate_new_reg = true; 5168 } else if (ts->val_type == TEMP_VAL_REG) { 5169 /* 5170 * Check if the current register has already been 5171 * allocated for another input. 5172 */ 5173 allocate_new_reg = 5174 tcg_regset_test_reg(i_allocated_regs, reg); 5175 } 5176 } 5177 if (!allocate_new_reg) { 5178 temp_load(s, ts, i_required_regs, i_allocated_regs, 5179 i_preferred_regs); 5180 reg = ts->reg; 5181 allocate_new_reg = !tcg_regset_test_reg(i_required_regs, reg); 5182 } 5183 if (allocate_new_reg) { 5184 /* 5185 * Allocate a new register matching the constraint 5186 * and move the temporary register into it. 5187 */ 5188 temp_load(s, ts, tcg_target_available_regs[ts->type], 5189 i_allocated_regs, 0); 5190 reg = tcg_reg_alloc(s, i_required_regs, i_allocated_regs, 5191 i_preferred_regs, ts->indirect_base); 5192 copyto_new_reg = true; 5193 } 5194 break; 5195 5196 case 1: 5197 /* First of an input pair; if i1 == i2, the second is an output. */ 5198 i1 = i; 5199 i2 = arg_ct->pair_index; 5200 ts2 = i1 != i2 ? arg_temp(op->args[i2]) : NULL; 5201 5202 /* 5203 * It is easier to default to allocating a new pair 5204 * and to identify a few cases where it's not required. 5205 */ 5206 if (arg_ct->ialias) { 5207 i_preferred_regs = output_pref(op, arg_ct->alias_index); 5208 if (IS_DEAD_ARG(i1) && 5209 IS_DEAD_ARG(i2) && 5210 !temp_readonly(ts) && 5211 ts->val_type == TEMP_VAL_REG && 5212 ts->reg < TCG_TARGET_NB_REGS - 1 && 5213 tcg_regset_test_reg(i_required_regs, reg) && 5214 !tcg_regset_test_reg(i_allocated_regs, reg) && 5215 !tcg_regset_test_reg(i_allocated_regs, reg + 1) && 5216 (ts2 5217 ? ts2->val_type == TEMP_VAL_REG && 5218 ts2->reg == reg + 1 && 5219 !temp_readonly(ts2) 5220 : s->reg_to_temp[reg + 1] == NULL)) { 5221 break; 5222 } 5223 } else { 5224 /* Without aliasing, the pair must also be an input. */ 5225 tcg_debug_assert(ts2); 5226 if (ts->val_type == TEMP_VAL_REG && 5227 ts2->val_type == TEMP_VAL_REG && 5228 ts2->reg == reg + 1 && 5229 tcg_regset_test_reg(i_required_regs, reg)) { 5230 break; 5231 } 5232 } 5233 reg = tcg_reg_alloc_pair(s, i_required_regs, i_allocated_regs, 5234 0, ts->indirect_base); 5235 goto do_pair; 5236 5237 case 2: /* pair second */ 5238 reg = new_args[arg_ct->pair_index] + 1; 5239 goto do_pair; 5240 5241 case 3: /* ialias with second output, no first input */ 5242 tcg_debug_assert(arg_ct->ialias); 5243 i_preferred_regs = output_pref(op, arg_ct->alias_index); 5244 5245 if (IS_DEAD_ARG(i) && 5246 !temp_readonly(ts) && 5247 ts->val_type == TEMP_VAL_REG && 5248 reg > 0 && 5249 s->reg_to_temp[reg - 1] == NULL && 5250 tcg_regset_test_reg(i_required_regs, reg) && 5251 !tcg_regset_test_reg(i_allocated_regs, reg) && 5252 !tcg_regset_test_reg(i_allocated_regs, reg - 1)) { 5253 tcg_regset_set_reg(i_allocated_regs, reg - 1); 5254 break; 5255 } 5256 reg = tcg_reg_alloc_pair(s, i_required_regs >> 1, 5257 i_allocated_regs, 0, 5258 ts->indirect_base); 5259 tcg_regset_set_reg(i_allocated_regs, reg); 5260 reg += 1; 5261 goto do_pair; 5262 5263 do_pair: 5264 /* 5265 * If an aliased input is not dead after the instruction, 5266 * we must allocate a new register and move it. 5267 */ 5268 if (arg_ct->ialias && (!IS_DEAD_ARG(i) || temp_readonly(ts))) { 5269 TCGRegSet t_allocated_regs = i_allocated_regs; 5270 5271 /* 5272 * Because of the alias, and the continued life, make sure 5273 * that the temp is somewhere *other* than the reg pair, 5274 * and we get a copy in reg. 5275 */ 5276 tcg_regset_set_reg(t_allocated_regs, reg); 5277 tcg_regset_set_reg(t_allocated_regs, reg + 1); 5278 if (ts->val_type == TEMP_VAL_REG && ts->reg == reg) { 5279 /* If ts was already in reg, copy it somewhere else. */ 5280 TCGReg nr; 5281 bool ok; 5282 5283 tcg_debug_assert(ts->kind != TEMP_FIXED); 5284 nr = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 5285 t_allocated_regs, 0, ts->indirect_base); 5286 ok = tcg_out_mov(s, ts->type, nr, reg); 5287 tcg_debug_assert(ok); 5288 5289 set_temp_val_reg(s, ts, nr); 5290 } else { 5291 temp_load(s, ts, tcg_target_available_regs[ts->type], 5292 t_allocated_regs, 0); 5293 copyto_new_reg = true; 5294 } 5295 } else { 5296 /* Preferably allocate to reg, otherwise copy. */ 5297 i_required_regs = (TCGRegSet)1 << reg; 5298 temp_load(s, ts, i_required_regs, i_allocated_regs, 5299 i_preferred_regs); 5300 copyto_new_reg = ts->reg != reg; 5301 } 5302 break; 5303 5304 default: 5305 g_assert_not_reached(); 5306 } 5307 5308 if (copyto_new_reg) { 5309 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) { 5310 /* 5311 * Cross register class move not supported. Sync the 5312 * temp back to its slot and load from there. 5313 */ 5314 temp_sync(s, ts, i_allocated_regs, 0, 0); 5315 tcg_out_ld(s, ts->type, reg, 5316 ts->mem_base->reg, ts->mem_offset); 5317 } 5318 } 5319 new_args[i] = reg; 5320 const_args[i] = 0; 5321 tcg_regset_set_reg(i_allocated_regs, reg); 5322 } 5323 5324 /* mark dead temporaries and free the associated registers */ 5325 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) { 5326 if (IS_DEAD_ARG(i)) { 5327 temp_dead(s, arg_temp(op->args[i])); 5328 } 5329 } 5330 5331 if (def->flags & TCG_OPF_COND_BRANCH) { 5332 tcg_reg_alloc_cbranch(s, i_allocated_regs); 5333 } else if (def->flags & TCG_OPF_BB_END) { 5334 tcg_reg_alloc_bb_end(s, i_allocated_regs); 5335 } else { 5336 if (def->flags & TCG_OPF_CALL_CLOBBER) { 5337 /* XXX: permit generic clobber register list ? */ 5338 for (i = 0; i < TCG_TARGET_NB_REGS; i++) { 5339 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) { 5340 tcg_reg_free(s, i, i_allocated_regs); 5341 } 5342 } 5343 } 5344 if (def->flags & TCG_OPF_SIDE_EFFECTS) { 5345 /* sync globals if the op has side effects and might trigger 5346 an exception. */ 5347 sync_globals(s, i_allocated_regs); 5348 } 5349 5350 /* satisfy the output constraints */ 5351 for (k = 0; k < nb_oargs; k++) { 5352 i = args_ct[k].sort_index; 5353 arg = op->args[i]; 5354 arg_ct = &args_ct[i]; 5355 ts = arg_temp(arg); 5356 5357 /* ENV should not be modified. */ 5358 tcg_debug_assert(!temp_readonly(ts)); 5359 5360 switch (arg_ct->pair) { 5361 case 0: /* not paired */ 5362 if (arg_ct->oalias && !const_args[arg_ct->alias_index]) { 5363 reg = new_args[arg_ct->alias_index]; 5364 } else if (arg_ct->newreg) { 5365 reg = tcg_reg_alloc(s, arg_ct->regs, 5366 i_allocated_regs | o_allocated_regs, 5367 output_pref(op, k), ts->indirect_base); 5368 } else { 5369 reg = tcg_reg_alloc(s, arg_ct->regs, o_allocated_regs, 5370 output_pref(op, k), ts->indirect_base); 5371 } 5372 break; 5373 5374 case 1: /* first of pair */ 5375 if (arg_ct->oalias) { 5376 reg = new_args[arg_ct->alias_index]; 5377 } else if (arg_ct->newreg) { 5378 reg = tcg_reg_alloc_pair(s, arg_ct->regs, 5379 i_allocated_regs | o_allocated_regs, 5380 output_pref(op, k), 5381 ts->indirect_base); 5382 } else { 5383 reg = tcg_reg_alloc_pair(s, arg_ct->regs, o_allocated_regs, 5384 output_pref(op, k), 5385 ts->indirect_base); 5386 } 5387 break; 5388 5389 case 2: /* second of pair */ 5390 if (arg_ct->oalias) { 5391 reg = new_args[arg_ct->alias_index]; 5392 } else { 5393 reg = new_args[arg_ct->pair_index] + 1; 5394 } 5395 break; 5396 5397 case 3: /* first of pair, aliasing with a second input */ 5398 tcg_debug_assert(!arg_ct->newreg); 5399 reg = new_args[arg_ct->pair_index] - 1; 5400 break; 5401 5402 default: 5403 g_assert_not_reached(); 5404 } 5405 tcg_regset_set_reg(o_allocated_regs, reg); 5406 set_temp_val_reg(s, ts, reg); 5407 ts->mem_coherent = 0; 5408 new_args[i] = reg; 5409 } 5410 } 5411 5412 /* emit instruction */ 5413 TCGType type = TCGOP_TYPE(op); 5414 switch (op->opc) { 5415 case INDEX_op_ext_i32_i64: 5416 tcg_out_exts_i32_i64(s, new_args[0], new_args[1]); 5417 break; 5418 case INDEX_op_extu_i32_i64: 5419 tcg_out_extu_i32_i64(s, new_args[0], new_args[1]); 5420 break; 5421 case INDEX_op_extrl_i64_i32: 5422 tcg_out_extrl_i64_i32(s, new_args[0], new_args[1]); 5423 break; 5424 5425 case INDEX_op_add: 5426 case INDEX_op_and: 5427 case INDEX_op_andc: 5428 case INDEX_op_clz: 5429 case INDEX_op_ctz: 5430 case INDEX_op_divs: 5431 case INDEX_op_divu: 5432 case INDEX_op_eqv: 5433 case INDEX_op_mul: 5434 case INDEX_op_mulsh: 5435 case INDEX_op_muluh: 5436 case INDEX_op_nand: 5437 case INDEX_op_nor: 5438 case INDEX_op_or: 5439 case INDEX_op_orc: 5440 case INDEX_op_rems: 5441 case INDEX_op_remu: 5442 case INDEX_op_rotl: 5443 case INDEX_op_rotr: 5444 case INDEX_op_sar: 5445 case INDEX_op_shl: 5446 case INDEX_op_shr: 5447 case INDEX_op_xor: 5448 { 5449 const TCGOutOpBinary *out = 5450 container_of(all_outop[op->opc], TCGOutOpBinary, base); 5451 5452 /* Constants should never appear in the first source operand. */ 5453 tcg_debug_assert(!const_args[1]); 5454 if (const_args[2]) { 5455 out->out_rri(s, type, new_args[0], new_args[1], new_args[2]); 5456 } else { 5457 out->out_rrr(s, type, new_args[0], new_args[1], new_args[2]); 5458 } 5459 } 5460 break; 5461 5462 case INDEX_op_sub: 5463 { 5464 const TCGOutOpSubtract *out = &outop_sub; 5465 5466 /* 5467 * Constants should never appear in the second source operand. 5468 * These are folded to add with negative constant. 5469 */ 5470 tcg_debug_assert(!const_args[2]); 5471 if (const_args[1]) { 5472 out->out_rir(s, type, new_args[0], new_args[1], new_args[2]); 5473 } else { 5474 out->out_rrr(s, type, new_args[0], new_args[1], new_args[2]); 5475 } 5476 } 5477 break; 5478 5479 case INDEX_op_bswap64: 5480 assert(TCG_TARGET_REG_BITS == 64); 5481 /* fall through */ 5482 case INDEX_op_ctpop: 5483 case INDEX_op_neg: 5484 case INDEX_op_not: 5485 { 5486 const TCGOutOpUnary *out = 5487 container_of(all_outop[op->opc], TCGOutOpUnary, base); 5488 5489 /* Constants should have been folded. */ 5490 tcg_debug_assert(!const_args[1]); 5491 out->out_rr(s, type, new_args[0], new_args[1]); 5492 } 5493 break; 5494 5495 case INDEX_op_bswap16: 5496 case INDEX_op_bswap32: 5497 { 5498 const TCGOutOpBswap *out = 5499 container_of(all_outop[op->opc], TCGOutOpBswap, base); 5500 5501 tcg_debug_assert(!const_args[1]); 5502 out->out_rr(s, type, new_args[0], new_args[1], new_args[2]); 5503 } 5504 break; 5505 5506 case INDEX_op_divs2: 5507 case INDEX_op_divu2: 5508 { 5509 const TCGOutOpDivRem *out = 5510 container_of(all_outop[op->opc], TCGOutOpDivRem, base); 5511 5512 /* Only used by x86 and s390x, which use matching constraints. */ 5513 tcg_debug_assert(new_args[0] == new_args[2]); 5514 tcg_debug_assert(new_args[1] == new_args[3]); 5515 tcg_debug_assert(!const_args[4]); 5516 out->out_rr01r(s, type, new_args[0], new_args[1], new_args[4]); 5517 } 5518 break; 5519 5520 case INDEX_op_extract: 5521 { 5522 const TCGOutOpExtract *out = 5523 container_of(all_outop[op->opc], TCGOutOpExtract, base); 5524 5525 tcg_debug_assert(!const_args[1]); 5526 out->out_rr(s, type, new_args[0], new_args[1], 5527 new_args[2], new_args[3]); 5528 } 5529 break; 5530 5531 case INDEX_op_muls2: 5532 case INDEX_op_mulu2: 5533 { 5534 const TCGOutOpMul2 *out = 5535 container_of(all_outop[op->opc], TCGOutOpMul2, base); 5536 5537 tcg_debug_assert(!const_args[2]); 5538 tcg_debug_assert(!const_args[3]); 5539 out->out_rrrr(s, type, new_args[0], new_args[1], 5540 new_args[2], new_args[3]); 5541 } 5542 break; 5543 5544 case INDEX_op_brcond: 5545 { 5546 const TCGOutOpBrcond *out = &outop_brcond; 5547 TCGCond cond = new_args[2]; 5548 TCGLabel *label = arg_label(new_args[3]); 5549 5550 tcg_debug_assert(!const_args[0]); 5551 if (const_args[1]) { 5552 out->out_ri(s, type, cond, new_args[0], new_args[1], label); 5553 } else { 5554 out->out_rr(s, type, cond, new_args[0], new_args[1], label); 5555 } 5556 } 5557 break; 5558 5559 case INDEX_op_movcond: 5560 { 5561 const TCGOutOpMovcond *out = &outop_movcond; 5562 TCGCond cond = new_args[5]; 5563 5564 tcg_debug_assert(!const_args[1]); 5565 out->out(s, type, cond, new_args[0], 5566 new_args[1], new_args[2], const_args[2], 5567 new_args[3], const_args[3], 5568 new_args[4], const_args[4]); 5569 } 5570 break; 5571 5572 case INDEX_op_setcond: 5573 case INDEX_op_negsetcond: 5574 { 5575 const TCGOutOpSetcond *out = 5576 container_of(all_outop[op->opc], TCGOutOpSetcond, base); 5577 TCGCond cond = new_args[3]; 5578 5579 tcg_debug_assert(!const_args[1]); 5580 if (const_args[2]) { 5581 out->out_rri(s, type, cond, 5582 new_args[0], new_args[1], new_args[2]); 5583 } else { 5584 out->out_rrr(s, type, cond, 5585 new_args[0], new_args[1], new_args[2]); 5586 } 5587 } 5588 break; 5589 5590 #if TCG_TARGET_REG_BITS == 32 5591 case INDEX_op_brcond2_i32: 5592 { 5593 const TCGOutOpBrcond2 *out = &outop_brcond2; 5594 TCGCond cond = new_args[4]; 5595 TCGLabel *label = arg_label(new_args[5]); 5596 5597 tcg_debug_assert(!const_args[0]); 5598 tcg_debug_assert(!const_args[1]); 5599 out->out(s, cond, new_args[0], new_args[1], 5600 new_args[2], const_args[2], 5601 new_args[3], const_args[3], label); 5602 } 5603 break; 5604 case INDEX_op_setcond2_i32: 5605 { 5606 const TCGOutOpSetcond2 *out = &outop_setcond2; 5607 TCGCond cond = new_args[5]; 5608 5609 tcg_debug_assert(!const_args[1]); 5610 tcg_debug_assert(!const_args[2]); 5611 out->out(s, cond, new_args[0], new_args[1], new_args[2], 5612 new_args[3], const_args[3], new_args[4], const_args[4]); 5613 } 5614 break; 5615 #else 5616 case INDEX_op_brcond2_i32: 5617 case INDEX_op_setcond2_i32: 5618 g_assert_not_reached(); 5619 #endif 5620 5621 default: 5622 if (def->flags & TCG_OPF_VECTOR) { 5623 tcg_out_vec_op(s, op->opc, type - TCG_TYPE_V64, 5624 TCGOP_VECE(op), new_args, const_args); 5625 } else { 5626 tcg_out_op(s, op->opc, type, new_args, const_args); 5627 } 5628 break; 5629 } 5630 5631 /* move the outputs in the correct register if needed */ 5632 for(i = 0; i < nb_oargs; i++) { 5633 ts = arg_temp(op->args[i]); 5634 5635 /* ENV should not be modified. */ 5636 tcg_debug_assert(!temp_readonly(ts)); 5637 5638 if (NEED_SYNC_ARG(i)) { 5639 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i)); 5640 } else if (IS_DEAD_ARG(i)) { 5641 temp_dead(s, ts); 5642 } 5643 } 5644 } 5645 5646 static bool tcg_reg_alloc_dup2(TCGContext *s, const TCGOp *op) 5647 { 5648 const TCGLifeData arg_life = op->life; 5649 TCGTemp *ots, *itsl, *itsh; 5650 TCGType vtype = TCGOP_TYPE(op); 5651 5652 /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */ 5653 tcg_debug_assert(TCG_TARGET_REG_BITS == 32); 5654 tcg_debug_assert(TCGOP_VECE(op) == MO_64); 5655 5656 ots = arg_temp(op->args[0]); 5657 itsl = arg_temp(op->args[1]); 5658 itsh = arg_temp(op->args[2]); 5659 5660 /* ENV should not be modified. */ 5661 tcg_debug_assert(!temp_readonly(ots)); 5662 5663 /* Allocate the output register now. */ 5664 if (ots->val_type != TEMP_VAL_REG) { 5665 TCGRegSet allocated_regs = s->reserved_regs; 5666 TCGRegSet dup_out_regs = opcode_args_ct(op)[0].regs; 5667 TCGReg oreg; 5668 5669 /* Make sure to not spill the input registers. */ 5670 if (!IS_DEAD_ARG(1) && itsl->val_type == TEMP_VAL_REG) { 5671 tcg_regset_set_reg(allocated_regs, itsl->reg); 5672 } 5673 if (!IS_DEAD_ARG(2) && itsh->val_type == TEMP_VAL_REG) { 5674 tcg_regset_set_reg(allocated_regs, itsh->reg); 5675 } 5676 5677 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs, 5678 output_pref(op, 0), ots->indirect_base); 5679 set_temp_val_reg(s, ots, oreg); 5680 } 5681 5682 /* Promote dup2 of immediates to dupi_vec. */ 5683 if (itsl->val_type == TEMP_VAL_CONST && itsh->val_type == TEMP_VAL_CONST) { 5684 uint64_t val = deposit64(itsl->val, 32, 32, itsh->val); 5685 MemOp vece = MO_64; 5686 5687 if (val == dup_const(MO_8, val)) { 5688 vece = MO_8; 5689 } else if (val == dup_const(MO_16, val)) { 5690 vece = MO_16; 5691 } else if (val == dup_const(MO_32, val)) { 5692 vece = MO_32; 5693 } 5694 5695 tcg_out_dupi_vec(s, vtype, vece, ots->reg, val); 5696 goto done; 5697 } 5698 5699 /* If the two inputs form one 64-bit value, try dupm_vec. */ 5700 if (itsl->temp_subindex == HOST_BIG_ENDIAN && 5701 itsh->temp_subindex == !HOST_BIG_ENDIAN && 5702 itsl == itsh + (HOST_BIG_ENDIAN ? 1 : -1)) { 5703 TCGTemp *its = itsl - HOST_BIG_ENDIAN; 5704 5705 temp_sync(s, its + 0, s->reserved_regs, 0, 0); 5706 temp_sync(s, its + 1, s->reserved_regs, 0, 0); 5707 5708 if (tcg_out_dupm_vec(s, vtype, MO_64, ots->reg, 5709 its->mem_base->reg, its->mem_offset)) { 5710 goto done; 5711 } 5712 } 5713 5714 /* Fall back to generic expansion. */ 5715 return false; 5716 5717 done: 5718 ots->mem_coherent = 0; 5719 if (IS_DEAD_ARG(1)) { 5720 temp_dead(s, itsl); 5721 } 5722 if (IS_DEAD_ARG(2)) { 5723 temp_dead(s, itsh); 5724 } 5725 if (NEED_SYNC_ARG(0)) { 5726 temp_sync(s, ots, s->reserved_regs, 0, IS_DEAD_ARG(0)); 5727 } else if (IS_DEAD_ARG(0)) { 5728 temp_dead(s, ots); 5729 } 5730 return true; 5731 } 5732 5733 static void load_arg_reg(TCGContext *s, TCGReg reg, TCGTemp *ts, 5734 TCGRegSet allocated_regs) 5735 { 5736 if (ts->val_type == TEMP_VAL_REG) { 5737 if (ts->reg != reg) { 5738 tcg_reg_free(s, reg, allocated_regs); 5739 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) { 5740 /* 5741 * Cross register class move not supported. Sync the 5742 * temp back to its slot and load from there. 5743 */ 5744 temp_sync(s, ts, allocated_regs, 0, 0); 5745 tcg_out_ld(s, ts->type, reg, 5746 ts->mem_base->reg, ts->mem_offset); 5747 } 5748 } 5749 } else { 5750 TCGRegSet arg_set = 0; 5751 5752 tcg_reg_free(s, reg, allocated_regs); 5753 tcg_regset_set_reg(arg_set, reg); 5754 temp_load(s, ts, arg_set, allocated_regs, 0); 5755 } 5756 } 5757 5758 static void load_arg_stk(TCGContext *s, unsigned arg_slot, TCGTemp *ts, 5759 TCGRegSet allocated_regs) 5760 { 5761 /* 5762 * When the destination is on the stack, load up the temp and store. 5763 * If there are many call-saved registers, the temp might live to 5764 * see another use; otherwise it'll be discarded. 5765 */ 5766 temp_load(s, ts, tcg_target_available_regs[ts->type], allocated_regs, 0); 5767 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, 5768 arg_slot_stk_ofs(arg_slot)); 5769 } 5770 5771 static void load_arg_normal(TCGContext *s, const TCGCallArgumentLoc *l, 5772 TCGTemp *ts, TCGRegSet *allocated_regs) 5773 { 5774 if (arg_slot_reg_p(l->arg_slot)) { 5775 TCGReg reg = tcg_target_call_iarg_regs[l->arg_slot]; 5776 load_arg_reg(s, reg, ts, *allocated_regs); 5777 tcg_regset_set_reg(*allocated_regs, reg); 5778 } else { 5779 load_arg_stk(s, l->arg_slot, ts, *allocated_regs); 5780 } 5781 } 5782 5783 static void load_arg_ref(TCGContext *s, unsigned arg_slot, TCGReg ref_base, 5784 intptr_t ref_off, TCGRegSet *allocated_regs) 5785 { 5786 TCGReg reg; 5787 5788 if (arg_slot_reg_p(arg_slot)) { 5789 reg = tcg_target_call_iarg_regs[arg_slot]; 5790 tcg_reg_free(s, reg, *allocated_regs); 5791 tcg_out_addi_ptr(s, reg, ref_base, ref_off); 5792 tcg_regset_set_reg(*allocated_regs, reg); 5793 } else { 5794 reg = tcg_reg_alloc(s, tcg_target_available_regs[TCG_TYPE_PTR], 5795 *allocated_regs, 0, false); 5796 tcg_out_addi_ptr(s, reg, ref_base, ref_off); 5797 tcg_out_st(s, TCG_TYPE_PTR, reg, TCG_REG_CALL_STACK, 5798 arg_slot_stk_ofs(arg_slot)); 5799 } 5800 } 5801 5802 static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op) 5803 { 5804 const int nb_oargs = TCGOP_CALLO(op); 5805 const int nb_iargs = TCGOP_CALLI(op); 5806 const TCGLifeData arg_life = op->life; 5807 const TCGHelperInfo *info = tcg_call_info(op); 5808 TCGRegSet allocated_regs = s->reserved_regs; 5809 int i; 5810 5811 /* 5812 * Move inputs into place in reverse order, 5813 * so that we place stacked arguments first. 5814 */ 5815 for (i = nb_iargs - 1; i >= 0; --i) { 5816 const TCGCallArgumentLoc *loc = &info->in[i]; 5817 TCGTemp *ts = arg_temp(op->args[nb_oargs + i]); 5818 5819 switch (loc->kind) { 5820 case TCG_CALL_ARG_NORMAL: 5821 case TCG_CALL_ARG_EXTEND_U: 5822 case TCG_CALL_ARG_EXTEND_S: 5823 load_arg_normal(s, loc, ts, &allocated_regs); 5824 break; 5825 case TCG_CALL_ARG_BY_REF: 5826 load_arg_stk(s, loc->ref_slot, ts, allocated_regs); 5827 load_arg_ref(s, loc->arg_slot, TCG_REG_CALL_STACK, 5828 arg_slot_stk_ofs(loc->ref_slot), 5829 &allocated_regs); 5830 break; 5831 case TCG_CALL_ARG_BY_REF_N: 5832 load_arg_stk(s, loc->ref_slot, ts, allocated_regs); 5833 break; 5834 default: 5835 g_assert_not_reached(); 5836 } 5837 } 5838 5839 /* Mark dead temporaries and free the associated registers. */ 5840 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) { 5841 if (IS_DEAD_ARG(i)) { 5842 temp_dead(s, arg_temp(op->args[i])); 5843 } 5844 } 5845 5846 /* Clobber call registers. */ 5847 for (i = 0; i < TCG_TARGET_NB_REGS; i++) { 5848 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) { 5849 tcg_reg_free(s, i, allocated_regs); 5850 } 5851 } 5852 5853 /* 5854 * Save globals if they might be written by the helper, 5855 * sync them if they might be read. 5856 */ 5857 if (info->flags & TCG_CALL_NO_READ_GLOBALS) { 5858 /* Nothing to do */ 5859 } else if (info->flags & TCG_CALL_NO_WRITE_GLOBALS) { 5860 sync_globals(s, allocated_regs); 5861 } else { 5862 save_globals(s, allocated_regs); 5863 } 5864 5865 /* 5866 * If the ABI passes a pointer to the returned struct as the first 5867 * argument, load that now. Pass a pointer to the output home slot. 5868 */ 5869 if (info->out_kind == TCG_CALL_RET_BY_REF) { 5870 TCGTemp *ts = arg_temp(op->args[0]); 5871 5872 if (!ts->mem_allocated) { 5873 temp_allocate_frame(s, ts); 5874 } 5875 load_arg_ref(s, 0, ts->mem_base->reg, ts->mem_offset, &allocated_regs); 5876 } 5877 5878 tcg_out_call(s, tcg_call_func(op), info); 5879 5880 /* Assign output registers and emit moves if needed. */ 5881 switch (info->out_kind) { 5882 case TCG_CALL_RET_NORMAL: 5883 for (i = 0; i < nb_oargs; i++) { 5884 TCGTemp *ts = arg_temp(op->args[i]); 5885 TCGReg reg = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, i); 5886 5887 /* ENV should not be modified. */ 5888 tcg_debug_assert(!temp_readonly(ts)); 5889 5890 set_temp_val_reg(s, ts, reg); 5891 ts->mem_coherent = 0; 5892 } 5893 break; 5894 5895 case TCG_CALL_RET_BY_VEC: 5896 { 5897 TCGTemp *ts = arg_temp(op->args[0]); 5898 5899 tcg_debug_assert(ts->base_type == TCG_TYPE_I128); 5900 tcg_debug_assert(ts->temp_subindex == 0); 5901 if (!ts->mem_allocated) { 5902 temp_allocate_frame(s, ts); 5903 } 5904 tcg_out_st(s, TCG_TYPE_V128, 5905 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0), 5906 ts->mem_base->reg, ts->mem_offset); 5907 } 5908 /* fall through to mark all parts in memory */ 5909 5910 case TCG_CALL_RET_BY_REF: 5911 /* The callee has performed a write through the reference. */ 5912 for (i = 0; i < nb_oargs; i++) { 5913 TCGTemp *ts = arg_temp(op->args[i]); 5914 ts->val_type = TEMP_VAL_MEM; 5915 } 5916 break; 5917 5918 default: 5919 g_assert_not_reached(); 5920 } 5921 5922 /* Flush or discard output registers as needed. */ 5923 for (i = 0; i < nb_oargs; i++) { 5924 TCGTemp *ts = arg_temp(op->args[i]); 5925 if (NEED_SYNC_ARG(i)) { 5926 temp_sync(s, ts, s->reserved_regs, 0, IS_DEAD_ARG(i)); 5927 } else if (IS_DEAD_ARG(i)) { 5928 temp_dead(s, ts); 5929 } 5930 } 5931 } 5932 5933 /** 5934 * atom_and_align_for_opc: 5935 * @s: tcg context 5936 * @opc: memory operation code 5937 * @host_atom: MO_ATOM_{IFALIGN,WITHIN16,SUBALIGN} for host operations 5938 * @allow_two_ops: true if we are prepared to issue two operations 5939 * 5940 * Return the alignment and atomicity to use for the inline fast path 5941 * for the given memory operation. The alignment may be larger than 5942 * that specified in @opc, and the correct alignment will be diagnosed 5943 * by the slow path helper. 5944 * 5945 * If @allow_two_ops, the host is prepared to test for 2x alignment, 5946 * and issue two loads or stores for subalignment. 5947 */ 5948 static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc, 5949 MemOp host_atom, bool allow_two_ops) 5950 { 5951 MemOp align = memop_alignment_bits(opc); 5952 MemOp size = opc & MO_SIZE; 5953 MemOp half = size ? size - 1 : 0; 5954 MemOp atom = opc & MO_ATOM_MASK; 5955 MemOp atmax; 5956 5957 switch (atom) { 5958 case MO_ATOM_NONE: 5959 /* The operation requires no specific atomicity. */ 5960 atmax = MO_8; 5961 break; 5962 5963 case MO_ATOM_IFALIGN: 5964 atmax = size; 5965 break; 5966 5967 case MO_ATOM_IFALIGN_PAIR: 5968 atmax = half; 5969 break; 5970 5971 case MO_ATOM_WITHIN16: 5972 atmax = size; 5973 if (size == MO_128) { 5974 /* Misalignment implies !within16, and therefore no atomicity. */ 5975 } else if (host_atom != MO_ATOM_WITHIN16) { 5976 /* The host does not implement within16, so require alignment. */ 5977 align = MAX(align, size); 5978 } 5979 break; 5980 5981 case MO_ATOM_WITHIN16_PAIR: 5982 atmax = size; 5983 /* 5984 * Misalignment implies !within16, and therefore half atomicity. 5985 * Any host prepared for two operations can implement this with 5986 * half alignment. 5987 */ 5988 if (host_atom != MO_ATOM_WITHIN16 && allow_two_ops) { 5989 align = MAX(align, half); 5990 } 5991 break; 5992 5993 case MO_ATOM_SUBALIGN: 5994 atmax = size; 5995 if (host_atom != MO_ATOM_SUBALIGN) { 5996 /* If unaligned but not odd, there are subobjects up to half. */ 5997 if (allow_two_ops) { 5998 align = MAX(align, half); 5999 } else { 6000 align = MAX(align, size); 6001 } 6002 } 6003 break; 6004 6005 default: 6006 g_assert_not_reached(); 6007 } 6008 6009 return (TCGAtomAlign){ .atom = atmax, .align = align }; 6010 } 6011 6012 /* 6013 * Similarly for qemu_ld/st slow path helpers. 6014 * We must re-implement tcg_gen_callN and tcg_reg_alloc_call simultaneously, 6015 * using only the provided backend tcg_out_* functions. 6016 */ 6017 6018 static int tcg_out_helper_stk_ofs(TCGType type, unsigned slot) 6019 { 6020 int ofs = arg_slot_stk_ofs(slot); 6021 6022 /* 6023 * Each stack slot is TCG_TARGET_LONG_BITS. If the host does not 6024 * require extension to uint64_t, adjust the address for uint32_t. 6025 */ 6026 if (HOST_BIG_ENDIAN && 6027 TCG_TARGET_REG_BITS == 64 && 6028 type == TCG_TYPE_I32) { 6029 ofs += 4; 6030 } 6031 return ofs; 6032 } 6033 6034 static void tcg_out_helper_load_slots(TCGContext *s, 6035 unsigned nmov, TCGMovExtend *mov, 6036 const TCGLdstHelperParam *parm) 6037 { 6038 unsigned i; 6039 TCGReg dst3; 6040 6041 /* 6042 * Start from the end, storing to the stack first. 6043 * This frees those registers, so we need not consider overlap. 6044 */ 6045 for (i = nmov; i-- > 0; ) { 6046 unsigned slot = mov[i].dst; 6047 6048 if (arg_slot_reg_p(slot)) { 6049 goto found_reg; 6050 } 6051 6052 TCGReg src = mov[i].src; 6053 TCGType dst_type = mov[i].dst_type; 6054 MemOp dst_mo = dst_type == TCG_TYPE_I32 ? MO_32 : MO_64; 6055 6056 /* The argument is going onto the stack; extend into scratch. */ 6057 if ((mov[i].src_ext & MO_SIZE) != dst_mo) { 6058 tcg_debug_assert(parm->ntmp != 0); 6059 mov[i].dst = src = parm->tmp[0]; 6060 tcg_out_movext1(s, &mov[i]); 6061 } 6062 6063 tcg_out_st(s, dst_type, src, TCG_REG_CALL_STACK, 6064 tcg_out_helper_stk_ofs(dst_type, slot)); 6065 } 6066 return; 6067 6068 found_reg: 6069 /* 6070 * The remaining arguments are in registers. 6071 * Convert slot numbers to argument registers. 6072 */ 6073 nmov = i + 1; 6074 for (i = 0; i < nmov; ++i) { 6075 mov[i].dst = tcg_target_call_iarg_regs[mov[i].dst]; 6076 } 6077 6078 switch (nmov) { 6079 case 4: 6080 /* The backend must have provided enough temps for the worst case. */ 6081 tcg_debug_assert(parm->ntmp >= 2); 6082 6083 dst3 = mov[3].dst; 6084 for (unsigned j = 0; j < 3; ++j) { 6085 if (dst3 == mov[j].src) { 6086 /* 6087 * Conflict. Copy the source to a temporary, perform the 6088 * remaining moves, then the extension from our scratch 6089 * on the way out. 6090 */ 6091 TCGReg scratch = parm->tmp[1]; 6092 6093 tcg_out_mov(s, mov[3].src_type, scratch, mov[3].src); 6094 tcg_out_movext3(s, mov, mov + 1, mov + 2, parm->tmp[0]); 6095 tcg_out_movext1_new_src(s, &mov[3], scratch); 6096 break; 6097 } 6098 } 6099 6100 /* No conflicts: perform this move and continue. */ 6101 tcg_out_movext1(s, &mov[3]); 6102 /* fall through */ 6103 6104 case 3: 6105 tcg_out_movext3(s, mov, mov + 1, mov + 2, 6106 parm->ntmp ? parm->tmp[0] : -1); 6107 break; 6108 case 2: 6109 tcg_out_movext2(s, mov, mov + 1, 6110 parm->ntmp ? parm->tmp[0] : -1); 6111 break; 6112 case 1: 6113 tcg_out_movext1(s, mov); 6114 break; 6115 default: 6116 g_assert_not_reached(); 6117 } 6118 } 6119 6120 static void tcg_out_helper_load_imm(TCGContext *s, unsigned slot, 6121 TCGType type, tcg_target_long imm, 6122 const TCGLdstHelperParam *parm) 6123 { 6124 if (arg_slot_reg_p(slot)) { 6125 tcg_out_movi(s, type, tcg_target_call_iarg_regs[slot], imm); 6126 } else { 6127 int ofs = tcg_out_helper_stk_ofs(type, slot); 6128 if (!tcg_out_sti(s, type, imm, TCG_REG_CALL_STACK, ofs)) { 6129 tcg_debug_assert(parm->ntmp != 0); 6130 tcg_out_movi(s, type, parm->tmp[0], imm); 6131 tcg_out_st(s, type, parm->tmp[0], TCG_REG_CALL_STACK, ofs); 6132 } 6133 } 6134 } 6135 6136 static void tcg_out_helper_load_common_args(TCGContext *s, 6137 const TCGLabelQemuLdst *ldst, 6138 const TCGLdstHelperParam *parm, 6139 const TCGHelperInfo *info, 6140 unsigned next_arg) 6141 { 6142 TCGMovExtend ptr_mov = { 6143 .dst_type = TCG_TYPE_PTR, 6144 .src_type = TCG_TYPE_PTR, 6145 .src_ext = sizeof(void *) == 4 ? MO_32 : MO_64 6146 }; 6147 const TCGCallArgumentLoc *loc = &info->in[0]; 6148 TCGType type; 6149 unsigned slot; 6150 tcg_target_ulong imm; 6151 6152 /* 6153 * Handle env, which is always first. 6154 */ 6155 ptr_mov.dst = loc->arg_slot; 6156 ptr_mov.src = TCG_AREG0; 6157 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm); 6158 6159 /* 6160 * Handle oi. 6161 */ 6162 imm = ldst->oi; 6163 loc = &info->in[next_arg]; 6164 type = TCG_TYPE_I32; 6165 switch (loc->kind) { 6166 case TCG_CALL_ARG_NORMAL: 6167 break; 6168 case TCG_CALL_ARG_EXTEND_U: 6169 case TCG_CALL_ARG_EXTEND_S: 6170 /* No extension required for MemOpIdx. */ 6171 tcg_debug_assert(imm <= INT32_MAX); 6172 type = TCG_TYPE_REG; 6173 break; 6174 default: 6175 g_assert_not_reached(); 6176 } 6177 tcg_out_helper_load_imm(s, loc->arg_slot, type, imm, parm); 6178 next_arg++; 6179 6180 /* 6181 * Handle ra. 6182 */ 6183 loc = &info->in[next_arg]; 6184 slot = loc->arg_slot; 6185 if (parm->ra_gen) { 6186 int arg_reg = -1; 6187 TCGReg ra_reg; 6188 6189 if (arg_slot_reg_p(slot)) { 6190 arg_reg = tcg_target_call_iarg_regs[slot]; 6191 } 6192 ra_reg = parm->ra_gen(s, ldst, arg_reg); 6193 6194 ptr_mov.dst = slot; 6195 ptr_mov.src = ra_reg; 6196 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm); 6197 } else { 6198 imm = (uintptr_t)ldst->raddr; 6199 tcg_out_helper_load_imm(s, slot, TCG_TYPE_PTR, imm, parm); 6200 } 6201 } 6202 6203 static unsigned tcg_out_helper_add_mov(TCGMovExtend *mov, 6204 const TCGCallArgumentLoc *loc, 6205 TCGType dst_type, TCGType src_type, 6206 TCGReg lo, TCGReg hi) 6207 { 6208 MemOp reg_mo; 6209 6210 if (dst_type <= TCG_TYPE_REG) { 6211 MemOp src_ext; 6212 6213 switch (loc->kind) { 6214 case TCG_CALL_ARG_NORMAL: 6215 src_ext = src_type == TCG_TYPE_I32 ? MO_32 : MO_64; 6216 break; 6217 case TCG_CALL_ARG_EXTEND_U: 6218 dst_type = TCG_TYPE_REG; 6219 src_ext = MO_UL; 6220 break; 6221 case TCG_CALL_ARG_EXTEND_S: 6222 dst_type = TCG_TYPE_REG; 6223 src_ext = MO_SL; 6224 break; 6225 default: 6226 g_assert_not_reached(); 6227 } 6228 6229 mov[0].dst = loc->arg_slot; 6230 mov[0].dst_type = dst_type; 6231 mov[0].src = lo; 6232 mov[0].src_type = src_type; 6233 mov[0].src_ext = src_ext; 6234 return 1; 6235 } 6236 6237 if (TCG_TARGET_REG_BITS == 32) { 6238 assert(dst_type == TCG_TYPE_I64); 6239 reg_mo = MO_32; 6240 } else { 6241 assert(dst_type == TCG_TYPE_I128); 6242 reg_mo = MO_64; 6243 } 6244 6245 mov[0].dst = loc[HOST_BIG_ENDIAN].arg_slot; 6246 mov[0].src = lo; 6247 mov[0].dst_type = TCG_TYPE_REG; 6248 mov[0].src_type = TCG_TYPE_REG; 6249 mov[0].src_ext = reg_mo; 6250 6251 mov[1].dst = loc[!HOST_BIG_ENDIAN].arg_slot; 6252 mov[1].src = hi; 6253 mov[1].dst_type = TCG_TYPE_REG; 6254 mov[1].src_type = TCG_TYPE_REG; 6255 mov[1].src_ext = reg_mo; 6256 6257 return 2; 6258 } 6259 6260 static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst, 6261 const TCGLdstHelperParam *parm) 6262 { 6263 const TCGHelperInfo *info; 6264 const TCGCallArgumentLoc *loc; 6265 TCGMovExtend mov[2]; 6266 unsigned next_arg, nmov; 6267 MemOp mop = get_memop(ldst->oi); 6268 6269 switch (mop & MO_SIZE) { 6270 case MO_8: 6271 case MO_16: 6272 case MO_32: 6273 info = &info_helper_ld32_mmu; 6274 break; 6275 case MO_64: 6276 info = &info_helper_ld64_mmu; 6277 break; 6278 case MO_128: 6279 info = &info_helper_ld128_mmu; 6280 break; 6281 default: 6282 g_assert_not_reached(); 6283 } 6284 6285 /* Defer env argument. */ 6286 next_arg = 1; 6287 6288 loc = &info->in[next_arg]; 6289 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) { 6290 /* 6291 * 32-bit host with 32-bit guest: zero-extend the guest address 6292 * to 64-bits for the helper by storing the low part, then 6293 * load a zero for the high part. 6294 */ 6295 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN, 6296 TCG_TYPE_I32, TCG_TYPE_I32, 6297 ldst->addr_reg, -1); 6298 tcg_out_helper_load_slots(s, 1, mov, parm); 6299 6300 tcg_out_helper_load_imm(s, loc[!HOST_BIG_ENDIAN].arg_slot, 6301 TCG_TYPE_I32, 0, parm); 6302 next_arg += 2; 6303 } else { 6304 nmov = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, s->addr_type, 6305 ldst->addr_reg, -1); 6306 tcg_out_helper_load_slots(s, nmov, mov, parm); 6307 next_arg += nmov; 6308 } 6309 6310 switch (info->out_kind) { 6311 case TCG_CALL_RET_NORMAL: 6312 case TCG_CALL_RET_BY_VEC: 6313 break; 6314 case TCG_CALL_RET_BY_REF: 6315 /* 6316 * The return reference is in the first argument slot. 6317 * We need memory in which to return: re-use the top of stack. 6318 */ 6319 { 6320 int ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET; 6321 6322 if (arg_slot_reg_p(0)) { 6323 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[0], 6324 TCG_REG_CALL_STACK, ofs_slot0); 6325 } else { 6326 tcg_debug_assert(parm->ntmp != 0); 6327 tcg_out_addi_ptr(s, parm->tmp[0], 6328 TCG_REG_CALL_STACK, ofs_slot0); 6329 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0], 6330 TCG_REG_CALL_STACK, ofs_slot0); 6331 } 6332 } 6333 break; 6334 default: 6335 g_assert_not_reached(); 6336 } 6337 6338 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg); 6339 } 6340 6341 static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *ldst, 6342 bool load_sign, 6343 const TCGLdstHelperParam *parm) 6344 { 6345 MemOp mop = get_memop(ldst->oi); 6346 TCGMovExtend mov[2]; 6347 int ofs_slot0; 6348 6349 switch (ldst->type) { 6350 case TCG_TYPE_I64: 6351 if (TCG_TARGET_REG_BITS == 32) { 6352 break; 6353 } 6354 /* fall through */ 6355 6356 case TCG_TYPE_I32: 6357 mov[0].dst = ldst->datalo_reg; 6358 mov[0].src = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, 0); 6359 mov[0].dst_type = ldst->type; 6360 mov[0].src_type = TCG_TYPE_REG; 6361 6362 /* 6363 * If load_sign, then we allowed the helper to perform the 6364 * appropriate sign extension to tcg_target_ulong, and all 6365 * we need now is a plain move. 6366 * 6367 * If they do not, then we expect the relevant extension 6368 * instruction to be no more expensive than a move, and 6369 * we thus save the icache etc by only using one of two 6370 * helper functions. 6371 */ 6372 if (load_sign || !(mop & MO_SIGN)) { 6373 if (TCG_TARGET_REG_BITS == 32 || ldst->type == TCG_TYPE_I32) { 6374 mov[0].src_ext = MO_32; 6375 } else { 6376 mov[0].src_ext = MO_64; 6377 } 6378 } else { 6379 mov[0].src_ext = mop & MO_SSIZE; 6380 } 6381 tcg_out_movext1(s, mov); 6382 return; 6383 6384 case TCG_TYPE_I128: 6385 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 6386 ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET; 6387 switch (TCG_TARGET_CALL_RET_I128) { 6388 case TCG_CALL_RET_NORMAL: 6389 break; 6390 case TCG_CALL_RET_BY_VEC: 6391 tcg_out_st(s, TCG_TYPE_V128, 6392 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0), 6393 TCG_REG_CALL_STACK, ofs_slot0); 6394 /* fall through */ 6395 case TCG_CALL_RET_BY_REF: 6396 tcg_out_ld(s, TCG_TYPE_I64, ldst->datalo_reg, 6397 TCG_REG_CALL_STACK, ofs_slot0 + 8 * HOST_BIG_ENDIAN); 6398 tcg_out_ld(s, TCG_TYPE_I64, ldst->datahi_reg, 6399 TCG_REG_CALL_STACK, ofs_slot0 + 8 * !HOST_BIG_ENDIAN); 6400 return; 6401 default: 6402 g_assert_not_reached(); 6403 } 6404 break; 6405 6406 default: 6407 g_assert_not_reached(); 6408 } 6409 6410 mov[0].dst = ldst->datalo_reg; 6411 mov[0].src = 6412 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, HOST_BIG_ENDIAN); 6413 mov[0].dst_type = TCG_TYPE_REG; 6414 mov[0].src_type = TCG_TYPE_REG; 6415 mov[0].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64; 6416 6417 mov[1].dst = ldst->datahi_reg; 6418 mov[1].src = 6419 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, !HOST_BIG_ENDIAN); 6420 mov[1].dst_type = TCG_TYPE_REG; 6421 mov[1].src_type = TCG_TYPE_REG; 6422 mov[1].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64; 6423 6424 tcg_out_movext2(s, mov, mov + 1, parm->ntmp ? parm->tmp[0] : -1); 6425 } 6426 6427 static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst, 6428 const TCGLdstHelperParam *parm) 6429 { 6430 const TCGHelperInfo *info; 6431 const TCGCallArgumentLoc *loc; 6432 TCGMovExtend mov[4]; 6433 TCGType data_type; 6434 unsigned next_arg, nmov, n; 6435 MemOp mop = get_memop(ldst->oi); 6436 6437 switch (mop & MO_SIZE) { 6438 case MO_8: 6439 case MO_16: 6440 case MO_32: 6441 info = &info_helper_st32_mmu; 6442 data_type = TCG_TYPE_I32; 6443 break; 6444 case MO_64: 6445 info = &info_helper_st64_mmu; 6446 data_type = TCG_TYPE_I64; 6447 break; 6448 case MO_128: 6449 info = &info_helper_st128_mmu; 6450 data_type = TCG_TYPE_I128; 6451 break; 6452 default: 6453 g_assert_not_reached(); 6454 } 6455 6456 /* Defer env argument. */ 6457 next_arg = 1; 6458 nmov = 0; 6459 6460 /* Handle addr argument. */ 6461 loc = &info->in[next_arg]; 6462 tcg_debug_assert(s->addr_type <= TCG_TYPE_REG); 6463 if (TCG_TARGET_REG_BITS == 32) { 6464 /* 6465 * 32-bit host (and thus 32-bit guest): zero-extend the guest address 6466 * to 64-bits for the helper by storing the low part. Later, 6467 * after we have processed the register inputs, we will load a 6468 * zero for the high part. 6469 */ 6470 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN, 6471 TCG_TYPE_I32, TCG_TYPE_I32, 6472 ldst->addr_reg, -1); 6473 next_arg += 2; 6474 nmov += 1; 6475 } else { 6476 n = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, s->addr_type, 6477 ldst->addr_reg, -1); 6478 next_arg += n; 6479 nmov += n; 6480 } 6481 6482 /* Handle data argument. */ 6483 loc = &info->in[next_arg]; 6484 switch (loc->kind) { 6485 case TCG_CALL_ARG_NORMAL: 6486 case TCG_CALL_ARG_EXTEND_U: 6487 case TCG_CALL_ARG_EXTEND_S: 6488 n = tcg_out_helper_add_mov(mov + nmov, loc, data_type, ldst->type, 6489 ldst->datalo_reg, ldst->datahi_reg); 6490 next_arg += n; 6491 nmov += n; 6492 tcg_out_helper_load_slots(s, nmov, mov, parm); 6493 break; 6494 6495 case TCG_CALL_ARG_BY_REF: 6496 tcg_debug_assert(TCG_TARGET_REG_BITS == 64); 6497 tcg_debug_assert(data_type == TCG_TYPE_I128); 6498 tcg_out_st(s, TCG_TYPE_I64, 6499 HOST_BIG_ENDIAN ? ldst->datahi_reg : ldst->datalo_reg, 6500 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[0].ref_slot)); 6501 tcg_out_st(s, TCG_TYPE_I64, 6502 HOST_BIG_ENDIAN ? ldst->datalo_reg : ldst->datahi_reg, 6503 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[1].ref_slot)); 6504 6505 tcg_out_helper_load_slots(s, nmov, mov, parm); 6506 6507 if (arg_slot_reg_p(loc->arg_slot)) { 6508 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[loc->arg_slot], 6509 TCG_REG_CALL_STACK, 6510 arg_slot_stk_ofs(loc->ref_slot)); 6511 } else { 6512 tcg_debug_assert(parm->ntmp != 0); 6513 tcg_out_addi_ptr(s, parm->tmp[0], TCG_REG_CALL_STACK, 6514 arg_slot_stk_ofs(loc->ref_slot)); 6515 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0], 6516 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc->arg_slot)); 6517 } 6518 next_arg += 2; 6519 break; 6520 6521 default: 6522 g_assert_not_reached(); 6523 } 6524 6525 if (TCG_TARGET_REG_BITS == 32) { 6526 /* Zero extend the address by loading a zero for the high part. */ 6527 loc = &info->in[1 + !HOST_BIG_ENDIAN]; 6528 tcg_out_helper_load_imm(s, loc->arg_slot, TCG_TYPE_I32, 0, parm); 6529 } 6530 6531 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg); 6532 } 6533 6534 int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start) 6535 { 6536 int i, start_words, num_insns; 6537 TCGOp *op; 6538 6539 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP) 6540 && qemu_log_in_addr_range(pc_start))) { 6541 FILE *logfile = qemu_log_trylock(); 6542 if (logfile) { 6543 fprintf(logfile, "OP:\n"); 6544 tcg_dump_ops(s, logfile, false); 6545 fprintf(logfile, "\n"); 6546 qemu_log_unlock(logfile); 6547 } 6548 } 6549 6550 #ifdef CONFIG_DEBUG_TCG 6551 /* Ensure all labels referenced have been emitted. */ 6552 { 6553 TCGLabel *l; 6554 bool error = false; 6555 6556 QSIMPLEQ_FOREACH(l, &s->labels, next) { 6557 if (unlikely(!l->present) && !QSIMPLEQ_EMPTY(&l->branches)) { 6558 qemu_log_mask(CPU_LOG_TB_OP, 6559 "$L%d referenced but not present.\n", l->id); 6560 error = true; 6561 } 6562 } 6563 assert(!error); 6564 } 6565 #endif 6566 6567 /* Do not reuse any EBB that may be allocated within the TB. */ 6568 tcg_temp_ebb_reset_freed(s); 6569 6570 tcg_optimize(s); 6571 6572 reachable_code_pass(s); 6573 liveness_pass_0(s); 6574 liveness_pass_1(s); 6575 6576 if (s->nb_indirects > 0) { 6577 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND) 6578 && qemu_log_in_addr_range(pc_start))) { 6579 FILE *logfile = qemu_log_trylock(); 6580 if (logfile) { 6581 fprintf(logfile, "OP before indirect lowering:\n"); 6582 tcg_dump_ops(s, logfile, false); 6583 fprintf(logfile, "\n"); 6584 qemu_log_unlock(logfile); 6585 } 6586 } 6587 6588 /* Replace indirect temps with direct temps. */ 6589 if (liveness_pass_2(s)) { 6590 /* If changes were made, re-run liveness. */ 6591 liveness_pass_1(s); 6592 } 6593 } 6594 6595 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT) 6596 && qemu_log_in_addr_range(pc_start))) { 6597 FILE *logfile = qemu_log_trylock(); 6598 if (logfile) { 6599 fprintf(logfile, "OP after optimization and liveness analysis:\n"); 6600 tcg_dump_ops(s, logfile, true); 6601 fprintf(logfile, "\n"); 6602 qemu_log_unlock(logfile); 6603 } 6604 } 6605 6606 /* Initialize goto_tb jump offsets. */ 6607 tb->jmp_reset_offset[0] = TB_JMP_OFFSET_INVALID; 6608 tb->jmp_reset_offset[1] = TB_JMP_OFFSET_INVALID; 6609 tb->jmp_insn_offset[0] = TB_JMP_OFFSET_INVALID; 6610 tb->jmp_insn_offset[1] = TB_JMP_OFFSET_INVALID; 6611 6612 tcg_reg_alloc_start(s); 6613 6614 /* 6615 * Reset the buffer pointers when restarting after overflow. 6616 * TODO: Move this into translate-all.c with the rest of the 6617 * buffer management. Having only this done here is confusing. 6618 */ 6619 s->code_buf = tcg_splitwx_to_rw(tb->tc.ptr); 6620 s->code_ptr = s->code_buf; 6621 s->data_gen_ptr = NULL; 6622 6623 QSIMPLEQ_INIT(&s->ldst_labels); 6624 s->pool_labels = NULL; 6625 6626 start_words = s->insn_start_words; 6627 s->gen_insn_data = 6628 tcg_malloc(sizeof(uint64_t) * s->gen_tb->icount * start_words); 6629 6630 tcg_out_tb_start(s); 6631 6632 num_insns = -1; 6633 QTAILQ_FOREACH(op, &s->ops, link) { 6634 TCGOpcode opc = op->opc; 6635 6636 switch (opc) { 6637 case INDEX_op_mov: 6638 case INDEX_op_mov_vec: 6639 tcg_reg_alloc_mov(s, op); 6640 break; 6641 case INDEX_op_dup_vec: 6642 tcg_reg_alloc_dup(s, op); 6643 break; 6644 case INDEX_op_insn_start: 6645 if (num_insns >= 0) { 6646 size_t off = tcg_current_code_size(s); 6647 s->gen_insn_end_off[num_insns] = off; 6648 /* Assert that we do not overflow our stored offset. */ 6649 assert(s->gen_insn_end_off[num_insns] == off); 6650 } 6651 num_insns++; 6652 for (i = 0; i < start_words; ++i) { 6653 s->gen_insn_data[num_insns * start_words + i] = 6654 tcg_get_insn_start_param(op, i); 6655 } 6656 break; 6657 case INDEX_op_discard: 6658 temp_dead(s, arg_temp(op->args[0])); 6659 break; 6660 case INDEX_op_set_label: 6661 tcg_reg_alloc_bb_end(s, s->reserved_regs); 6662 tcg_out_label(s, arg_label(op->args[0])); 6663 break; 6664 case INDEX_op_call: 6665 tcg_reg_alloc_call(s, op); 6666 break; 6667 case INDEX_op_exit_tb: 6668 tcg_out_exit_tb(s, op->args[0]); 6669 break; 6670 case INDEX_op_goto_tb: 6671 tcg_out_goto_tb(s, op->args[0]); 6672 break; 6673 case INDEX_op_dup2_vec: 6674 if (tcg_reg_alloc_dup2(s, op)) { 6675 break; 6676 } 6677 /* fall through */ 6678 default: 6679 /* Sanity check that we've not introduced any unhandled opcodes. */ 6680 tcg_debug_assert(tcg_op_supported(opc, TCGOP_TYPE(op), 6681 TCGOP_FLAGS(op))); 6682 /* Note: in order to speed up the code, it would be much 6683 faster to have specialized register allocator functions for 6684 some common argument patterns */ 6685 tcg_reg_alloc_op(s, op); 6686 break; 6687 } 6688 /* Test for (pending) buffer overflow. The assumption is that any 6689 one operation beginning below the high water mark cannot overrun 6690 the buffer completely. Thus we can test for overflow after 6691 generating code without having to check during generation. */ 6692 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) { 6693 return -1; 6694 } 6695 /* Test for TB overflow, as seen by gen_insn_end_off. */ 6696 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) { 6697 return -2; 6698 } 6699 } 6700 tcg_debug_assert(num_insns + 1 == s->gen_tb->icount); 6701 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s); 6702 6703 /* Generate TB finalization at the end of block */ 6704 i = tcg_out_ldst_finalize(s); 6705 if (i < 0) { 6706 return i; 6707 } 6708 i = tcg_out_pool_finalize(s); 6709 if (i < 0) { 6710 return i; 6711 } 6712 if (!tcg_resolve_relocs(s)) { 6713 return -2; 6714 } 6715 6716 #ifndef CONFIG_TCG_INTERPRETER 6717 /* flush instruction cache */ 6718 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf), 6719 (uintptr_t)s->code_buf, 6720 tcg_ptr_byte_diff(s->code_ptr, s->code_buf)); 6721 #endif 6722 6723 return tcg_current_code_size(s); 6724 } 6725 6726 #ifdef ELF_HOST_MACHINE 6727 /* In order to use this feature, the backend needs to do three things: 6728 6729 (1) Define ELF_HOST_MACHINE to indicate both what value to 6730 put into the ELF image and to indicate support for the feature. 6731 6732 (2) Define tcg_register_jit. This should create a buffer containing 6733 the contents of a .debug_frame section that describes the post- 6734 prologue unwind info for the tcg machine. 6735 6736 (3) Call tcg_register_jit_int, with the constructed .debug_frame. 6737 */ 6738 6739 /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */ 6740 typedef enum { 6741 JIT_NOACTION = 0, 6742 JIT_REGISTER_FN, 6743 JIT_UNREGISTER_FN 6744 } jit_actions_t; 6745 6746 struct jit_code_entry { 6747 struct jit_code_entry *next_entry; 6748 struct jit_code_entry *prev_entry; 6749 const void *symfile_addr; 6750 uint64_t symfile_size; 6751 }; 6752 6753 struct jit_descriptor { 6754 uint32_t version; 6755 uint32_t action_flag; 6756 struct jit_code_entry *relevant_entry; 6757 struct jit_code_entry *first_entry; 6758 }; 6759 6760 void __jit_debug_register_code(void) __attribute__((noinline)); 6761 void __jit_debug_register_code(void) 6762 { 6763 asm(""); 6764 } 6765 6766 /* Must statically initialize the version, because GDB may check 6767 the version before we can set it. */ 6768 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 }; 6769 6770 /* End GDB interface. */ 6771 6772 static int find_string(const char *strtab, const char *str) 6773 { 6774 const char *p = strtab + 1; 6775 6776 while (1) { 6777 if (strcmp(p, str) == 0) { 6778 return p - strtab; 6779 } 6780 p += strlen(p) + 1; 6781 } 6782 } 6783 6784 static void tcg_register_jit_int(const void *buf_ptr, size_t buf_size, 6785 const void *debug_frame, 6786 size_t debug_frame_size) 6787 { 6788 struct __attribute__((packed)) DebugInfo { 6789 uint32_t len; 6790 uint16_t version; 6791 uint32_t abbrev; 6792 uint8_t ptr_size; 6793 uint8_t cu_die; 6794 uint16_t cu_lang; 6795 uintptr_t cu_low_pc; 6796 uintptr_t cu_high_pc; 6797 uint8_t fn_die; 6798 char fn_name[16]; 6799 uintptr_t fn_low_pc; 6800 uintptr_t fn_high_pc; 6801 uint8_t cu_eoc; 6802 }; 6803 6804 struct ElfImage { 6805 ElfW(Ehdr) ehdr; 6806 ElfW(Phdr) phdr; 6807 ElfW(Shdr) shdr[7]; 6808 ElfW(Sym) sym[2]; 6809 struct DebugInfo di; 6810 uint8_t da[24]; 6811 char str[80]; 6812 }; 6813 6814 struct ElfImage *img; 6815 6816 static const struct ElfImage img_template = { 6817 .ehdr = { 6818 .e_ident[EI_MAG0] = ELFMAG0, 6819 .e_ident[EI_MAG1] = ELFMAG1, 6820 .e_ident[EI_MAG2] = ELFMAG2, 6821 .e_ident[EI_MAG3] = ELFMAG3, 6822 .e_ident[EI_CLASS] = ELF_CLASS, 6823 .e_ident[EI_DATA] = ELF_DATA, 6824 .e_ident[EI_VERSION] = EV_CURRENT, 6825 .e_type = ET_EXEC, 6826 .e_machine = ELF_HOST_MACHINE, 6827 .e_version = EV_CURRENT, 6828 .e_phoff = offsetof(struct ElfImage, phdr), 6829 .e_shoff = offsetof(struct ElfImage, shdr), 6830 .e_ehsize = sizeof(ElfW(Shdr)), 6831 .e_phentsize = sizeof(ElfW(Phdr)), 6832 .e_phnum = 1, 6833 .e_shentsize = sizeof(ElfW(Shdr)), 6834 .e_shnum = ARRAY_SIZE(img->shdr), 6835 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1, 6836 #ifdef ELF_HOST_FLAGS 6837 .e_flags = ELF_HOST_FLAGS, 6838 #endif 6839 #ifdef ELF_OSABI 6840 .e_ident[EI_OSABI] = ELF_OSABI, 6841 #endif 6842 }, 6843 .phdr = { 6844 .p_type = PT_LOAD, 6845 .p_flags = PF_X, 6846 }, 6847 .shdr = { 6848 [0] = { .sh_type = SHT_NULL }, 6849 /* Trick: The contents of code_gen_buffer are not present in 6850 this fake ELF file; that got allocated elsewhere. Therefore 6851 we mark .text as SHT_NOBITS (similar to .bss) so that readers 6852 will not look for contents. We can record any address. */ 6853 [1] = { /* .text */ 6854 .sh_type = SHT_NOBITS, 6855 .sh_flags = SHF_EXECINSTR | SHF_ALLOC, 6856 }, 6857 [2] = { /* .debug_info */ 6858 .sh_type = SHT_PROGBITS, 6859 .sh_offset = offsetof(struct ElfImage, di), 6860 .sh_size = sizeof(struct DebugInfo), 6861 }, 6862 [3] = { /* .debug_abbrev */ 6863 .sh_type = SHT_PROGBITS, 6864 .sh_offset = offsetof(struct ElfImage, da), 6865 .sh_size = sizeof(img->da), 6866 }, 6867 [4] = { /* .debug_frame */ 6868 .sh_type = SHT_PROGBITS, 6869 .sh_offset = sizeof(struct ElfImage), 6870 }, 6871 [5] = { /* .symtab */ 6872 .sh_type = SHT_SYMTAB, 6873 .sh_offset = offsetof(struct ElfImage, sym), 6874 .sh_size = sizeof(img->sym), 6875 .sh_info = 1, 6876 .sh_link = ARRAY_SIZE(img->shdr) - 1, 6877 .sh_entsize = sizeof(ElfW(Sym)), 6878 }, 6879 [6] = { /* .strtab */ 6880 .sh_type = SHT_STRTAB, 6881 .sh_offset = offsetof(struct ElfImage, str), 6882 .sh_size = sizeof(img->str), 6883 } 6884 }, 6885 .sym = { 6886 [1] = { /* code_gen_buffer */ 6887 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC), 6888 .st_shndx = 1, 6889 } 6890 }, 6891 .di = { 6892 .len = sizeof(struct DebugInfo) - 4, 6893 .version = 2, 6894 .ptr_size = sizeof(void *), 6895 .cu_die = 1, 6896 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */ 6897 .fn_die = 2, 6898 .fn_name = "code_gen_buffer" 6899 }, 6900 .da = { 6901 1, /* abbrev number (the cu) */ 6902 0x11, 1, /* DW_TAG_compile_unit, has children */ 6903 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */ 6904 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */ 6905 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */ 6906 0, 0, /* end of abbrev */ 6907 2, /* abbrev number (the fn) */ 6908 0x2e, 0, /* DW_TAG_subprogram, no children */ 6909 0x3, 0x8, /* DW_AT_name, DW_FORM_string */ 6910 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */ 6911 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */ 6912 0, 0, /* end of abbrev */ 6913 0 /* no more abbrev */ 6914 }, 6915 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0" 6916 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer", 6917 }; 6918 6919 /* We only need a single jit entry; statically allocate it. */ 6920 static struct jit_code_entry one_entry; 6921 6922 uintptr_t buf = (uintptr_t)buf_ptr; 6923 size_t img_size = sizeof(struct ElfImage) + debug_frame_size; 6924 DebugFrameHeader *dfh; 6925 6926 img = g_malloc(img_size); 6927 *img = img_template; 6928 6929 img->phdr.p_vaddr = buf; 6930 img->phdr.p_paddr = buf; 6931 img->phdr.p_memsz = buf_size; 6932 6933 img->shdr[1].sh_name = find_string(img->str, ".text"); 6934 img->shdr[1].sh_addr = buf; 6935 img->shdr[1].sh_size = buf_size; 6936 6937 img->shdr[2].sh_name = find_string(img->str, ".debug_info"); 6938 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev"); 6939 6940 img->shdr[4].sh_name = find_string(img->str, ".debug_frame"); 6941 img->shdr[4].sh_size = debug_frame_size; 6942 6943 img->shdr[5].sh_name = find_string(img->str, ".symtab"); 6944 img->shdr[6].sh_name = find_string(img->str, ".strtab"); 6945 6946 img->sym[1].st_name = find_string(img->str, "code_gen_buffer"); 6947 img->sym[1].st_value = buf; 6948 img->sym[1].st_size = buf_size; 6949 6950 img->di.cu_low_pc = buf; 6951 img->di.cu_high_pc = buf + buf_size; 6952 img->di.fn_low_pc = buf; 6953 img->di.fn_high_pc = buf + buf_size; 6954 6955 dfh = (DebugFrameHeader *)(img + 1); 6956 memcpy(dfh, debug_frame, debug_frame_size); 6957 dfh->fde.func_start = buf; 6958 dfh->fde.func_len = buf_size; 6959 6960 #ifdef DEBUG_JIT 6961 /* Enable this block to be able to debug the ELF image file creation. 6962 One can use readelf, objdump, or other inspection utilities. */ 6963 { 6964 g_autofree char *jit = g_strdup_printf("%s/qemu.jit", g_get_tmp_dir()); 6965 FILE *f = fopen(jit, "w+b"); 6966 if (f) { 6967 if (fwrite(img, img_size, 1, f) != img_size) { 6968 /* Avoid stupid unused return value warning for fwrite. */ 6969 } 6970 fclose(f); 6971 } 6972 } 6973 #endif 6974 6975 one_entry.symfile_addr = img; 6976 one_entry.symfile_size = img_size; 6977 6978 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN; 6979 __jit_debug_descriptor.relevant_entry = &one_entry; 6980 __jit_debug_descriptor.first_entry = &one_entry; 6981 __jit_debug_register_code(); 6982 } 6983 #else 6984 /* No support for the feature. Provide the entry point expected by exec.c, 6985 and implement the internal function we declared earlier. */ 6986 6987 static void tcg_register_jit_int(const void *buf, size_t size, 6988 const void *debug_frame, 6989 size_t debug_frame_size) 6990 { 6991 } 6992 6993 void tcg_register_jit(const void *buf, size_t buf_size) 6994 { 6995 } 6996 #endif /* ELF_HOST_MACHINE */ 6997 6998 #if !TCG_TARGET_MAYBE_vec 6999 void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...) 7000 { 7001 g_assert_not_reached(); 7002 } 7003 #endif 7004