2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2021 WANG Xuerui <git@xen0n.name>
6 * Based on tcg/riscv/tcg-target.c.inc
8 * Copyright (c) 2018 SiFive, Inc
9 * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org>
10 * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net>
11 * Copyright (c) 2008 Fabrice Bellard
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 #include "../tcg-ldst.c.inc"
33 #include <asm/hwcap.h>
35 #ifdef CONFIG_DEBUG_TCG
36 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
58 "r21", /* reserved in the LP64* ABI, hence no ABI name */
104 static const int tcg_target_reg_alloc_order[] = {
105 /* Registers preserved across calls */
106 /* TCG_REG_S0 reserved for TCG_AREG0 */
117 /* Registers (potentially) clobbered across calls */
128 /* Argument registers, opposite order of allocation. */
138 /* Vector registers */
139 TCG_REG_V0, TCG_REG_V1, TCG_REG_V2, TCG_REG_V3,
140 TCG_REG_V4, TCG_REG_V5, TCG_REG_V6, TCG_REG_V7,
141 TCG_REG_V8, TCG_REG_V9, TCG_REG_V10, TCG_REG_V11,
142 TCG_REG_V12, TCG_REG_V13, TCG_REG_V14, TCG_REG_V15,
143 TCG_REG_V16, TCG_REG_V17, TCG_REG_V18, TCG_REG_V19,
144 TCG_REG_V20, TCG_REG_V21, TCG_REG_V22, TCG_REG_V23,
145 /* V24 - V31 are caller-saved, and skipped. */
148 static const int tcg_target_call_iarg_regs[] = {
159 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
161 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
162 tcg_debug_assert(slot >= 0 && slot <= 1);
163 return TCG_REG_A0 + slot;
166 #define TCG_GUEST_BASE_REG TCG_REG_S1
168 #define TCG_CT_CONST_ZERO 0x100
169 #define TCG_CT_CONST_S12 0x200
170 #define TCG_CT_CONST_S32 0x400
171 #define TCG_CT_CONST_U12 0x800
172 #define TCG_CT_CONST_C12 0x1000
173 #define TCG_CT_CONST_WSZ 0x2000
174 #define TCG_CT_CONST_VCMP 0x4000
175 #define TCG_CT_CONST_VADD 0x8000
177 #define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32)
178 #define ALL_VECTOR_REGS MAKE_64BIT_MASK(32, 32)
180 static inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
182 return sextract64(val, pos, len);
185 /* test if a constant matches the constraint */
186 static bool tcg_target_const_match(int64_t val, int ct,
187 TCGType type, TCGCond cond, int vece)
189 if (ct & TCG_CT_CONST) {
192 if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
195 if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
198 if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
201 if ((ct & TCG_CT_CONST_U12) && val >= 0 && val <= 0xfff) {
204 if ((ct & TCG_CT_CONST_C12) && ~val >= 0 && ~val <= 0xfff) {
207 if ((ct & TCG_CT_CONST_WSZ) && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
210 int64_t vec_val = sextract64(val, 0, 8 << vece);
211 if ((ct & TCG_CT_CONST_VCMP) && -0x10 <= vec_val && vec_val <= 0x1f) {
214 if ((ct & TCG_CT_CONST_VADD) && -0x1f <= vec_val && vec_val <= 0x1f) {
225 * Relocation records defined in LoongArch ELF psABI v1.00 is way too
226 * complicated; a whopping stack machine is needed to stuff the fields, at
227 * the very least one SOP_PUSH and one SOP_POP (of the correct format) are
230 * Hence, define our own simpler relocation types. Numbers are chosen as to
231 * not collide with potential future additions to the true ELF relocation
235 /* Field Sk16, shifted right by 2; suitable for conditional jumps */
236 #define R_LOONGARCH_BR_SK16 256
237 /* Field Sd10k16, shifted right by 2; suitable for B and BL */
238 #define R_LOONGARCH_BR_SD10K16 257
240 static bool reloc_br_sk16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
242 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
243 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
245 tcg_debug_assert((offset & 3) == 0);
247 if (offset == sextreg(offset, 0, 16)) {
248 *src_rw = deposit64(*src_rw, 10, 16, offset);
255 static bool reloc_br_sd10k16(tcg_insn_unit *src_rw,
256 const tcg_insn_unit *target)
258 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
259 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
261 tcg_debug_assert((offset & 3) == 0);
263 if (offset == sextreg(offset, 0, 26)) {
264 *src_rw = deposit64(*src_rw, 0, 10, offset >> 16); /* slot d10 */
265 *src_rw = deposit64(*src_rw, 10, 16, offset); /* slot k16 */
272 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
273 intptr_t value, intptr_t addend)
275 tcg_debug_assert(addend == 0);
277 case R_LOONGARCH_BR_SK16:
278 return reloc_br_sk16(code_ptr, (tcg_insn_unit *)value);
279 case R_LOONGARCH_BR_SD10K16:
280 return reloc_br_sd10k16(code_ptr, (tcg_insn_unit *)value);
282 g_assert_not_reached();
286 #include "tcg-insn-defs.c.inc"
292 static void tcg_out_mb(TCGContext *s, TCGArg a0)
294 /* Baseline LoongArch only has the full barrier, unfortunately. */
295 tcg_out_opc_dbar(s, 0);
298 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
306 if (ret < TCG_REG_V0) {
307 if (arg < TCG_REG_V0) {
309 * Conventional register-register move used in LoongArch is
310 * `or dst, src, zero`.
312 tcg_out_opc_or(s, ret, arg, TCG_REG_ZERO);
314 tcg_out_opc_movfr2gr_d(s, ret, arg);
317 if (arg < TCG_REG_V0) {
318 tcg_out_opc_movgr2fr_d(s, ret, arg);
320 tcg_out_opc_fmov_d(s, ret, arg);
326 tcg_out_opc_vori_b(s, ret, arg, 0);
329 g_assert_not_reached();
334 /* Loads a 32-bit immediate into rd, sign-extended. */
335 static void tcg_out_movi_i32(TCGContext *s, TCGReg rd, int32_t val)
337 tcg_target_long lo = sextreg(val, 0, 12);
338 tcg_target_long hi12 = sextreg(val, 12, 20);
340 /* Single-instruction cases. */
342 /* val fits in uimm12: ori rd, zero, val */
343 tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val);
346 if (hi12 == sextreg(lo, 12, 20)) {
347 /* val fits in simm12: addi.w rd, zero, val */
348 tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val);
352 /* High bits must be set; load with lu12i.w + optional ori. */
353 tcg_out_opc_lu12i_w(s, rd, hi12);
355 tcg_out_opc_ori(s, rd, rd, lo & 0xfff);
359 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
363 * LoongArch conventionally loads 64-bit immediates in at most 4 steps,
364 * with dedicated instructions for filling the respective bitfields
368 * 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
369 * +-----------------------+---------------------------------------+...
371 * +-----------------------+---------------------------------------+...
373 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
374 * ...+-------------------------------------+-------------------------+
376 * ...+-------------------------------------+-------------------------+
378 * Check if val belong to one of the several fast cases, before falling
379 * back to the slow path.
383 tcg_target_long val_lo, val_hi, pc_hi, offset_hi;
384 tcg_target_long hi12, hi32, hi52;
386 /* Value fits in signed i32. */
387 if (type == TCG_TYPE_I32 || val == (int32_t)val) {
388 tcg_out_movi_i32(s, rd, val);
392 /* PC-relative cases. */
393 pc_offset = tcg_pcrel_diff(s, (void *)val);
394 if (pc_offset == sextreg(pc_offset, 0, 22) && (pc_offset & 3) == 0) {
395 /* Single pcaddu2i. */
396 tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2);
400 if (pc_offset == (int32_t)pc_offset) {
401 /* Offset within 32 bits; load with pcalau12i + ori. */
402 val_lo = sextreg(val, 0, 12);
404 pc_hi = (val - pc_offset) >> 12;
405 offset_hi = val_hi - pc_hi;
407 tcg_debug_assert(offset_hi == sextreg(offset_hi, 0, 20));
408 tcg_out_opc_pcalau12i(s, rd, offset_hi);
410 tcg_out_opc_ori(s, rd, rd, val_lo & 0xfff);
415 hi12 = sextreg(val, 12, 20);
416 hi32 = sextreg(val, 32, 20);
417 hi52 = sextreg(val, 52, 12);
419 /* Single cu52i.d case. */
420 if ((hi52 != 0) && (ctz64(val) >= 52)) {
421 tcg_out_opc_cu52i_d(s, rd, TCG_REG_ZERO, hi52);
425 /* Slow path. Initialize the low 32 bits, then concat high bits. */
426 tcg_out_movi_i32(s, rd, val);
428 /* Load hi32 and hi52 explicitly when they are unexpected values. */
429 if (hi32 != sextreg(hi12, 20, 20)) {
430 tcg_out_opc_cu32i_d(s, rd, hi32);
433 if (hi52 != sextreg(hi32, 20, 12)) {
434 tcg_out_opc_cu52i_d(s, rd, rd, hi52);
438 static void tcg_out_addi(TCGContext *s, TCGType type, TCGReg rd,
439 TCGReg rs, tcg_target_long imm)
441 tcg_target_long lo12 = sextreg(imm, 0, 12);
442 tcg_target_long hi16 = sextreg(imm - lo12, 16, 16);
445 * Note that there's a hole in between hi16 and lo12:
448 * 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
449 * ...+-------------------------------+-------+-----------------------+
451 * ...+-------------------------------+-------+-----------------------+
453 * For bits within that hole, it's more efficient to use LU12I and ADD.
455 if (imm == (hi16 << 16) + lo12) {
457 tcg_out_opc_addu16i_d(s, rd, rs, hi16);
460 if (type == TCG_TYPE_I32) {
461 tcg_out_opc_addi_w(s, rd, rs, lo12);
463 tcg_out_opc_addi_d(s, rd, rs, lo12);
465 tcg_out_mov(s, type, rd, rs);
468 tcg_out_movi(s, type, TCG_REG_TMP0, imm);
469 if (type == TCG_TYPE_I32) {
470 tcg_out_opc_add_w(s, rd, rs, TCG_REG_TMP0);
472 tcg_out_opc_add_d(s, rd, rs, TCG_REG_TMP0);
477 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
482 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
485 /* This function is only used for passing structs by reference. */
486 g_assert_not_reached();
489 static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
491 tcg_out_opc_andi(s, ret, arg, 0xff);
494 static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
496 tcg_out_opc_bstrpick_w(s, ret, arg, 0, 15);
499 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
501 tcg_out_opc_bstrpick_d(s, ret, arg, 0, 31);
504 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
506 tcg_out_opc_sext_b(s, ret, arg);
509 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
511 tcg_out_opc_sext_h(s, ret, arg);
514 static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
516 tcg_out_opc_addi_w(s, ret, arg, 0);
519 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg)
522 tcg_out_ext32s(s, ret, arg);
526 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg)
528 tcg_out_ext32u(s, ret, arg);
531 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg)
533 tcg_out_ext32s(s, ret, arg);
536 static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
537 TCGReg a0, TCGReg a1, TCGReg a2,
538 bool c2, bool is_32bit)
542 * Fast path: semantics already satisfied due to constraint and
543 * insn behavior, single instruction is enough.
545 tcg_debug_assert(a2 == (is_32bit ? 32 : 64));
546 /* all clz/ctz insns belong to DJ-format */
547 tcg_out32(s, encode_dj_insn(opc, a0, a1));
551 tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1));
552 /* a0 = a1 ? REG_TMP0 : a2 */
553 tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1);
554 tcg_out_opc_masknez(s, a0, a2, a1);
555 tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
558 #define SETCOND_INV TCG_TARGET_NB_REGS
559 #define SETCOND_NEZ (SETCOND_INV << 1)
560 #define SETCOND_FLAGS (SETCOND_INV | SETCOND_NEZ)
562 static int tcg_out_setcond_int(TCGContext *s, TCGCond cond, TCGReg ret,
563 TCGReg arg1, tcg_target_long arg2, bool c2)
568 case TCG_COND_EQ: /* -> NE */
569 case TCG_COND_GE: /* -> LT */
570 case TCG_COND_GEU: /* -> LTU */
571 case TCG_COND_GT: /* -> LE */
572 case TCG_COND_GTU: /* -> LEU */
573 cond = tcg_invert_cond(cond);
574 flags ^= SETCOND_INV;
584 * If we have a constant input, the most efficient way to implement
585 * LE is by adding 1 and using LT. Watch out for wrap around for LEU.
586 * We don't need to care for this for LE because the constant input
587 * is still constrained to int32_t, and INT32_MAX+1 is representable
588 * in the 64-bit temporary register.
591 if (cond == TCG_COND_LEU) {
592 /* unsigned <= -1 is true */
594 tcg_out_movi(s, TCG_TYPE_REG, ret, !(flags & SETCOND_INV));
606 cond = tcg_swap_cond(cond); /* LE -> GE */
607 cond = tcg_invert_cond(cond); /* GE -> LT */
608 flags ^= SETCOND_INV;
617 flags |= SETCOND_NEZ;
619 tcg_out_opc_xor(s, ret, arg1, arg2);
620 } else if (arg2 == 0) {
622 } else if (arg2 >= 0 && arg2 <= 0xfff) {
623 tcg_out_opc_xori(s, ret, arg1, arg2);
625 tcg_out_addi(s, TCG_TYPE_REG, ret, arg1, -arg2);
632 if (arg2 >= -0x800 && arg2 <= 0x7ff) {
633 if (cond == TCG_COND_LT) {
634 tcg_out_opc_slti(s, ret, arg1, arg2);
636 tcg_out_opc_sltui(s, ret, arg1, arg2);
640 tcg_out_movi(s, TCG_TYPE_REG, TCG_REG_TMP0, arg2);
643 if (cond == TCG_COND_LT) {
644 tcg_out_opc_slt(s, ret, arg1, arg2);
646 tcg_out_opc_sltu(s, ret, arg1, arg2);
651 g_assert_not_reached();
658 static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
659 TCGReg arg1, tcg_target_long arg2, bool c2)
661 int tmpflags = tcg_out_setcond_int(s, cond, ret, arg1, arg2, c2);
663 if (tmpflags != ret) {
664 TCGReg tmp = tmpflags & ~SETCOND_FLAGS;
666 switch (tmpflags & SETCOND_FLAGS) {
668 /* Intermediate result is boolean: simply invert. */
669 tcg_out_opc_xori(s, ret, tmp, 1);
672 /* Intermediate result is zero/non-zero: test != 0. */
673 tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, tmp);
675 case SETCOND_NEZ | SETCOND_INV:
676 /* Intermediate result is zero/non-zero: test == 0. */
677 tcg_out_opc_sltui(s, ret, tmp, 1);
680 g_assert_not_reached();
685 static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
686 TCGReg c1, tcg_target_long c2, bool const2,
687 TCGReg v1, TCGReg v2)
689 int tmpflags = tcg_out_setcond_int(s, cond, TCG_REG_TMP0, c1, c2, const2);
692 /* Standardize the test below to t != 0. */
693 if (tmpflags & SETCOND_INV) {
694 t = v1, v1 = v2, v2 = t;
697 t = tmpflags & ~SETCOND_FLAGS;
698 if (v1 == TCG_REG_ZERO) {
699 tcg_out_opc_masknez(s, ret, v2, t);
700 } else if (v2 == TCG_REG_ZERO) {
701 tcg_out_opc_maskeqz(s, ret, v1, t);
703 tcg_out_opc_masknez(s, TCG_REG_TMP2, v2, t); /* t ? 0 : v2 */
704 tcg_out_opc_maskeqz(s, TCG_REG_TMP1, v1, t); /* t ? v1 : 0 */
705 tcg_out_opc_or(s, ret, TCG_REG_TMP1, TCG_REG_TMP2);
713 static const struct {
716 } tcg_brcond_to_loongarch[] = {
717 [TCG_COND_EQ] = { OPC_BEQ, false },
718 [TCG_COND_NE] = { OPC_BNE, false },
719 [TCG_COND_LT] = { OPC_BGT, true },
720 [TCG_COND_GE] = { OPC_BLE, true },
721 [TCG_COND_LE] = { OPC_BLE, false },
722 [TCG_COND_GT] = { OPC_BGT, false },
723 [TCG_COND_LTU] = { OPC_BGTU, true },
724 [TCG_COND_GEU] = { OPC_BLEU, true },
725 [TCG_COND_LEU] = { OPC_BLEU, false },
726 [TCG_COND_GTU] = { OPC_BGTU, false }
729 static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
730 TCGReg arg2, TCGLabel *l)
732 LoongArchInsn op = tcg_brcond_to_loongarch[cond].op;
734 tcg_debug_assert(op != 0);
736 if (tcg_brcond_to_loongarch[cond].swap) {
742 /* all conditional branch insns belong to DJSk16-format */
743 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SK16, l, 0);
744 tcg_out32(s, encode_djsk16_insn(op, arg1, arg2, 0));
747 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
749 TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
750 ptrdiff_t offset = tcg_pcrel_diff(s, arg);
752 tcg_debug_assert((offset & 3) == 0);
753 if (offset == sextreg(offset, 0, 28)) {
754 /* short jump: +/- 256MiB */
756 tcg_out_opc_b(s, offset >> 2);
758 tcg_out_opc_bl(s, offset >> 2);
760 } else if (offset == sextreg(offset, 0, 38)) {
761 /* long jump: +/- 256GiB */
762 tcg_target_long lo = sextreg(offset, 0, 18);
763 tcg_target_long hi = offset - lo;
764 tcg_out_opc_pcaddu18i(s, TCG_REG_TMP0, hi >> 18);
765 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
767 /* far jump: 64-bit */
768 tcg_target_long lo = sextreg((tcg_target_long)arg, 0, 18);
769 tcg_target_long hi = (tcg_target_long)arg - lo;
770 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, hi);
771 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
775 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
776 const TCGHelperInfo *info)
778 tcg_out_call_int(s, arg, false);
785 static void tcg_out_ldst(TCGContext *s, LoongArchInsn opc, TCGReg data,
786 TCGReg addr, intptr_t offset)
788 intptr_t imm12 = sextreg(offset, 0, 12);
790 if (offset != imm12) {
791 intptr_t diff = tcg_pcrel_diff(s, (void *)offset);
793 if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
794 imm12 = sextreg(diff, 0, 12);
795 tcg_out_opc_pcaddu12i(s, TCG_REG_TMP2, (diff - imm12) >> 12);
797 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
798 if (addr != TCG_REG_ZERO) {
799 tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, addr);
817 tcg_out32(s, encode_djsk12_insn(opc, data, addr, imm12));
823 tcg_out32(s, encode_fdjsk12_insn(opc, data, addr, imm12));
826 g_assert_not_reached();
830 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg dest,
831 TCGReg base, intptr_t offset)
835 if (dest < TCG_REG_V0) {
836 tcg_out_ldst(s, OPC_LD_W, dest, base, offset);
838 tcg_out_ldst(s, OPC_FLD_S, dest, base, offset);
843 if (dest < TCG_REG_V0) {
844 tcg_out_ldst(s, OPC_LD_D, dest, base, offset);
846 tcg_out_ldst(s, OPC_FLD_D, dest, base, offset);
850 if (-0x800 <= offset && offset <= 0x7ff) {
851 tcg_out_opc_vld(s, dest, base, offset);
853 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset);
854 tcg_out_opc_vldx(s, dest, base, TCG_REG_TMP0);
858 g_assert_not_reached();
862 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg src,
863 TCGReg base, intptr_t offset)
867 if (src < TCG_REG_V0) {
868 tcg_out_ldst(s, OPC_ST_W, src, base, offset);
870 tcg_out_ldst(s, OPC_FST_S, src, base, offset);
875 if (src < TCG_REG_V0) {
876 tcg_out_ldst(s, OPC_ST_D, src, base, offset);
878 tcg_out_ldst(s, OPC_FST_D, src, base, offset);
882 if (-0x800 <= offset && offset <= 0x7ff) {
883 tcg_out_opc_vst(s, src, base, offset);
885 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, offset);
886 tcg_out_opc_vstx(s, src, base, TCG_REG_TMP0);
890 g_assert_not_reached();
894 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
895 TCGReg base, intptr_t ofs)
898 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
905 * Load/store helpers for SoftMMU, and qemu_ld/st implementations
908 static bool tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
911 return reloc_br_sd10k16(s->code_ptr - 1, target);
914 static const TCGLdstHelperParam ldst_helper_param = {
915 .ntmp = 1, .tmp = { TCG_REG_TMP0 }
918 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
920 MemOp opc = get_memop(l->oi);
922 /* resolve label address */
923 if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
927 tcg_out_ld_helper_args(s, l, &ldst_helper_param);
928 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE], false);
929 tcg_out_ld_helper_ret(s, l, false, &ldst_helper_param);
930 return tcg_out_goto(s, l->raddr);
933 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
935 MemOp opc = get_memop(l->oi);
937 /* resolve label address */
938 if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
942 tcg_out_st_helper_args(s, l, &ldst_helper_param);
943 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
944 return tcg_out_goto(s, l->raddr);
953 bool tcg_target_has_memory_bswap(MemOp memop)
958 /* We expect to use a 12-bit negative offset from ENV. */
959 #define MIN_TLB_MASK_TABLE_OFS -(1 << 11)
962 * For system-mode, perform the TLB load and compare.
963 * For user-mode, perform any required alignment tests.
964 * In both cases, return a TCGLabelQemuLdst structure if the slow path
965 * is required and fill in @h with the host address for the fast path.
967 static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
968 TCGReg addr_reg, MemOpIdx oi,
971 TCGType addr_type = s->addr_type;
972 TCGLabelQemuLdst *ldst = NULL;
973 MemOp opc = get_memop(oi);
976 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
977 a_bits = h->aa.align;
979 if (tcg_use_softmmu) {
980 unsigned s_bits = opc & MO_SIZE;
981 int mem_index = get_mmuidx(oi);
982 int fast_ofs = tlb_mask_table_ofs(s, mem_index);
983 int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
984 int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
986 ldst = new_ldst_label(s);
989 ldst->addrlo_reg = addr_reg;
991 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs);
992 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs);
994 tcg_out_opc_srli_d(s, TCG_REG_TMP2, addr_reg,
995 s->page_bits - CPU_TLB_ENTRY_BITS);
996 tcg_out_opc_and(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
997 tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
999 /* Load the tlb comparator and the addend. */
1000 QEMU_BUILD_BUG_ON(HOST_BIG_ENDIAN);
1001 tcg_out_ld(s, addr_type, TCG_REG_TMP0, TCG_REG_TMP2,
1002 is_ld ? offsetof(CPUTLBEntry, addr_read)
1003 : offsetof(CPUTLBEntry, addr_write));
1004 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
1005 offsetof(CPUTLBEntry, addend));
1008 * For aligned accesses, we check the first byte and include the
1009 * alignment bits within the address. For unaligned access, we
1010 * check that we don't cross pages using the address of the last
1011 * byte of the access.
1013 if (a_bits < s_bits) {
1014 unsigned a_mask = (1u << a_bits) - 1;
1015 unsigned s_mask = (1u << s_bits) - 1;
1016 tcg_out_addi(s, addr_type, TCG_REG_TMP1, addr_reg, s_mask - a_mask);
1018 tcg_out_mov(s, addr_type, TCG_REG_TMP1, addr_reg);
1020 tcg_out_opc_bstrins_d(s, TCG_REG_TMP1, TCG_REG_ZERO,
1021 a_bits, s->page_bits - 1);
1023 /* Compare masked address with the TLB entry. */
1024 ldst->label_ptr[0] = s->code_ptr;
1025 tcg_out_opc_bne(s, TCG_REG_TMP0, TCG_REG_TMP1, 0);
1027 h->index = TCG_REG_TMP2;
1030 ldst = new_ldst_label(s);
1032 ldst->is_ld = is_ld;
1034 ldst->addrlo_reg = addr_reg;
1037 * Without micro-architecture details, we don't know which of
1038 * bstrpick or andi is faster, so use bstrpick as it's not
1039 * constrained by imm field width. Not to say alignments >= 2^12
1040 * are going to happen any time soon.
1042 tcg_out_opc_bstrpick_d(s, TCG_REG_TMP1, addr_reg, 0, a_bits - 1);
1044 ldst->label_ptr[0] = s->code_ptr;
1045 tcg_out_opc_bne(s, TCG_REG_TMP1, TCG_REG_ZERO, 0);
1048 h->index = guest_base ? TCG_GUEST_BASE_REG : TCG_REG_ZERO;
1051 if (addr_type == TCG_TYPE_I32) {
1052 h->base = TCG_REG_TMP0;
1053 tcg_out_ext32u(s, h->base, addr_reg);
1061 static void tcg_out_qemu_ld_indexed(TCGContext *s, MemOp opc, TCGType type,
1062 TCGReg rd, HostAddress h)
1064 /* Byte swapping is left to middle-end expansion. */
1065 tcg_debug_assert((opc & MO_BSWAP) == 0);
1067 switch (opc & MO_SSIZE) {
1069 tcg_out_opc_ldx_bu(s, rd, h.base, h.index);
1072 tcg_out_opc_ldx_b(s, rd, h.base, h.index);
1075 tcg_out_opc_ldx_hu(s, rd, h.base, h.index);
1078 tcg_out_opc_ldx_h(s, rd, h.base, h.index);
1081 if (type == TCG_TYPE_I64) {
1082 tcg_out_opc_ldx_wu(s, rd, h.base, h.index);
1087 tcg_out_opc_ldx_w(s, rd, h.base, h.index);
1090 tcg_out_opc_ldx_d(s, rd, h.base, h.index);
1093 g_assert_not_reached();
1097 static void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1098 MemOpIdx oi, TCGType data_type)
1100 TCGLabelQemuLdst *ldst;
1103 ldst = prepare_host_addr(s, &h, addr_reg, oi, true);
1104 tcg_out_qemu_ld_indexed(s, get_memop(oi), data_type, data_reg, h);
1107 ldst->type = data_type;
1108 ldst->datalo_reg = data_reg;
1109 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1113 static void tcg_out_qemu_st_indexed(TCGContext *s, MemOp opc,
1114 TCGReg rd, HostAddress h)
1116 /* Byte swapping is left to middle-end expansion. */
1117 tcg_debug_assert((opc & MO_BSWAP) == 0);
1119 switch (opc & MO_SIZE) {
1121 tcg_out_opc_stx_b(s, rd, h.base, h.index);
1124 tcg_out_opc_stx_h(s, rd, h.base, h.index);
1127 tcg_out_opc_stx_w(s, rd, h.base, h.index);
1130 tcg_out_opc_stx_d(s, rd, h.base, h.index);
1133 g_assert_not_reached();
1137 static void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1138 MemOpIdx oi, TCGType data_type)
1140 TCGLabelQemuLdst *ldst;
1143 ldst = prepare_host_addr(s, &h, addr_reg, oi, false);
1144 tcg_out_qemu_st_indexed(s, get_memop(oi), data_reg, h);
1147 ldst->type = data_type;
1148 ldst->datalo_reg = data_reg;
1149 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1153 static void tcg_out_qemu_ldst_i128(TCGContext *s, TCGReg data_lo, TCGReg data_hi,
1154 TCGReg addr_reg, MemOpIdx oi, bool is_ld)
1156 TCGLabelQemuLdst *ldst;
1159 ldst = prepare_host_addr(s, &h, addr_reg, oi, is_ld);
1161 if (h.aa.atom == MO_128) {
1163 * Use VLDX/VSTX when 128-bit atomicity is required.
1164 * If address is aligned to 16-bytes, the 128-bit load/store is atomic.
1167 tcg_out_opc_vldx(s, TCG_VEC_TMP0, h.base, h.index);
1168 tcg_out_opc_vpickve2gr_d(s, data_lo, TCG_VEC_TMP0, 0);
1169 tcg_out_opc_vpickve2gr_d(s, data_hi, TCG_VEC_TMP0, 1);
1171 tcg_out_opc_vinsgr2vr_d(s, TCG_VEC_TMP0, data_lo, 0);
1172 tcg_out_opc_vinsgr2vr_d(s, TCG_VEC_TMP0, data_hi, 1);
1173 tcg_out_opc_vstx(s, TCG_VEC_TMP0, h.base, h.index);
1176 /* Otherwise use a pair of LD/ST. */
1177 TCGReg base = h.base;
1178 if (h.index != TCG_REG_ZERO) {
1179 base = TCG_REG_TMP0;
1180 tcg_out_opc_add_d(s, base, h.base, h.index);
1183 tcg_debug_assert(base != data_lo);
1184 tcg_out_opc_ld_d(s, data_lo, base, 0);
1185 tcg_out_opc_ld_d(s, data_hi, base, 8);
1187 tcg_out_opc_st_d(s, data_lo, base, 0);
1188 tcg_out_opc_st_d(s, data_hi, base, 8);
1193 ldst->type = TCG_TYPE_I128;
1194 ldst->datalo_reg = data_lo;
1195 ldst->datahi_reg = data_hi;
1196 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1204 static const tcg_insn_unit *tb_ret_addr;
1206 static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1208 /* Reuse the zeroing that exists for goto_ptr. */
1210 tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1212 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1213 tcg_out_call_int(s, tb_ret_addr, true);
1217 static void tcg_out_goto_tb(TCGContext *s, int which)
1220 * Direct branch, or load indirect address, to be patched
1221 * by tb_target_set_jmp_target. Check indirect load offset
1222 * in range early, regardless of direct branch distance,
1223 * via assert within tcg_out_opc_pcaddu2i.
1225 uintptr_t i_addr = get_jmp_target_addr(s, which);
1226 intptr_t i_disp = tcg_pcrel_diff(s, (void *)i_addr);
1228 set_jmp_insn_offset(s, which);
1229 tcg_out_opc_pcaddu2i(s, TCG_REG_TMP0, i_disp >> 2);
1231 /* Finish the load and indirect branch. */
1232 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_TMP0, 0);
1233 tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1234 set_jmp_reset_offset(s, which);
1237 void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1238 uintptr_t jmp_rx, uintptr_t jmp_rw)
1240 uintptr_t d_addr = tb->jmp_target_addr[n];
1241 ptrdiff_t d_disp = (ptrdiff_t)(d_addr - jmp_rx) >> 2;
1244 /* Either directly branch, or load slot address for indirect branch. */
1245 if (d_disp == sextreg(d_disp, 0, 26)) {
1246 insn = encode_sd10k16_insn(OPC_B, d_disp);
1248 uintptr_t i_addr = (uintptr_t)&tb->jmp_target_addr[n];
1249 intptr_t i_disp = i_addr - jmp_rx;
1250 insn = encode_dsj20_insn(OPC_PCADDU2I, TCG_REG_TMP0, i_disp >> 2);
1253 qatomic_set((tcg_insn_unit *)jmp_rw, insn);
1254 flush_idcache_range(jmp_rx, jmp_rw, 4);
1257 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1258 const TCGArg args[TCG_MAX_OP_ARGS],
1259 const int const_args[TCG_MAX_OP_ARGS])
1261 TCGArg a0 = args[0];
1262 TCGArg a1 = args[1];
1263 TCGArg a2 = args[2];
1264 TCGArg a3 = args[3];
1265 int c2 = const_args[2];
1272 case INDEX_op_goto_ptr:
1273 tcg_out_opc_jirl(s, TCG_REG_ZERO, a0, 0);
1277 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SD10K16, arg_label(a0),
1279 tcg_out_opc_b(s, 0);
1282 case INDEX_op_brcond_i32:
1283 case INDEX_op_brcond_i64:
1284 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1287 case INDEX_op_extrh_i64_i32:
1288 tcg_out_opc_srai_d(s, a0, a1, 32);
1291 case INDEX_op_not_i32:
1292 case INDEX_op_not_i64:
1293 tcg_out_opc_nor(s, a0, a1, TCG_REG_ZERO);
1296 case INDEX_op_nor_i32:
1297 case INDEX_op_nor_i64:
1299 tcg_out_opc_ori(s, a0, a1, a2);
1300 tcg_out_opc_nor(s, a0, a0, TCG_REG_ZERO);
1302 tcg_out_opc_nor(s, a0, a1, a2);
1306 case INDEX_op_andc_i32:
1307 case INDEX_op_andc_i64:
1309 /* guaranteed to fit due to constraint */
1310 tcg_out_opc_andi(s, a0, a1, ~a2);
1312 tcg_out_opc_andn(s, a0, a1, a2);
1316 case INDEX_op_orc_i32:
1317 case INDEX_op_orc_i64:
1319 /* guaranteed to fit due to constraint */
1320 tcg_out_opc_ori(s, a0, a1, ~a2);
1322 tcg_out_opc_orn(s, a0, a1, a2);
1326 case INDEX_op_and_i32:
1327 case INDEX_op_and_i64:
1329 tcg_out_opc_andi(s, a0, a1, a2);
1331 tcg_out_opc_and(s, a0, a1, a2);
1335 case INDEX_op_or_i32:
1336 case INDEX_op_or_i64:
1338 tcg_out_opc_ori(s, a0, a1, a2);
1340 tcg_out_opc_or(s, a0, a1, a2);
1344 case INDEX_op_xor_i32:
1345 case INDEX_op_xor_i64:
1347 tcg_out_opc_xori(s, a0, a1, a2);
1349 tcg_out_opc_xor(s, a0, a1, a2);
1353 case INDEX_op_extract_i32:
1354 tcg_out_opc_bstrpick_w(s, a0, a1, a2, a2 + args[3] - 1);
1356 case INDEX_op_extract_i64:
1357 tcg_out_opc_bstrpick_d(s, a0, a1, a2, a2 + args[3] - 1);
1360 case INDEX_op_deposit_i32:
1361 tcg_out_opc_bstrins_w(s, a0, a2, args[3], args[3] + args[4] - 1);
1363 case INDEX_op_deposit_i64:
1364 tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1);
1367 case INDEX_op_bswap16_i32:
1368 case INDEX_op_bswap16_i64:
1369 tcg_out_opc_revb_2h(s, a0, a1);
1370 if (a2 & TCG_BSWAP_OS) {
1371 tcg_out_ext16s(s, TCG_TYPE_REG, a0, a0);
1372 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1373 tcg_out_ext16u(s, a0, a0);
1377 case INDEX_op_bswap32_i32:
1378 /* All 32-bit values are computed sign-extended in the register. */
1381 case INDEX_op_bswap32_i64:
1382 tcg_out_opc_revb_2w(s, a0, a1);
1383 if (a2 & TCG_BSWAP_OS) {
1384 tcg_out_ext32s(s, a0, a0);
1385 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1386 tcg_out_ext32u(s, a0, a0);
1390 case INDEX_op_bswap64_i64:
1391 tcg_out_opc_revb_d(s, a0, a1);
1394 case INDEX_op_clz_i32:
1395 tcg_out_clzctz(s, OPC_CLZ_W, a0, a1, a2, c2, true);
1397 case INDEX_op_clz_i64:
1398 tcg_out_clzctz(s, OPC_CLZ_D, a0, a1, a2, c2, false);
1401 case INDEX_op_ctz_i32:
1402 tcg_out_clzctz(s, OPC_CTZ_W, a0, a1, a2, c2, true);
1404 case INDEX_op_ctz_i64:
1405 tcg_out_clzctz(s, OPC_CTZ_D, a0, a1, a2, c2, false);
1408 case INDEX_op_shl_i32:
1410 tcg_out_opc_slli_w(s, a0, a1, a2 & 0x1f);
1412 tcg_out_opc_sll_w(s, a0, a1, a2);
1415 case INDEX_op_shl_i64:
1417 tcg_out_opc_slli_d(s, a0, a1, a2 & 0x3f);
1419 tcg_out_opc_sll_d(s, a0, a1, a2);
1423 case INDEX_op_shr_i32:
1425 tcg_out_opc_srli_w(s, a0, a1, a2 & 0x1f);
1427 tcg_out_opc_srl_w(s, a0, a1, a2);
1430 case INDEX_op_shr_i64:
1432 tcg_out_opc_srli_d(s, a0, a1, a2 & 0x3f);
1434 tcg_out_opc_srl_d(s, a0, a1, a2);
1438 case INDEX_op_sar_i32:
1440 tcg_out_opc_srai_w(s, a0, a1, a2 & 0x1f);
1442 tcg_out_opc_sra_w(s, a0, a1, a2);
1445 case INDEX_op_sar_i64:
1447 tcg_out_opc_srai_d(s, a0, a1, a2 & 0x3f);
1449 tcg_out_opc_sra_d(s, a0, a1, a2);
1453 case INDEX_op_rotl_i32:
1454 /* transform into equivalent rotr/rotri */
1456 tcg_out_opc_rotri_w(s, a0, a1, (32 - a2) & 0x1f);
1458 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
1459 tcg_out_opc_rotr_w(s, a0, a1, TCG_REG_TMP0);
1462 case INDEX_op_rotl_i64:
1463 /* transform into equivalent rotr/rotri */
1465 tcg_out_opc_rotri_d(s, a0, a1, (64 - a2) & 0x3f);
1467 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
1468 tcg_out_opc_rotr_d(s, a0, a1, TCG_REG_TMP0);
1472 case INDEX_op_rotr_i32:
1474 tcg_out_opc_rotri_w(s, a0, a1, a2 & 0x1f);
1476 tcg_out_opc_rotr_w(s, a0, a1, a2);
1479 case INDEX_op_rotr_i64:
1481 tcg_out_opc_rotri_d(s, a0, a1, a2 & 0x3f);
1483 tcg_out_opc_rotr_d(s, a0, a1, a2);
1487 case INDEX_op_add_i32:
1489 tcg_out_addi(s, TCG_TYPE_I32, a0, a1, a2);
1491 tcg_out_opc_add_w(s, a0, a1, a2);
1494 case INDEX_op_add_i64:
1496 tcg_out_addi(s, TCG_TYPE_I64, a0, a1, a2);
1498 tcg_out_opc_add_d(s, a0, a1, a2);
1502 case INDEX_op_sub_i32:
1504 tcg_out_addi(s, TCG_TYPE_I32, a0, a1, -a2);
1506 tcg_out_opc_sub_w(s, a0, a1, a2);
1509 case INDEX_op_sub_i64:
1511 tcg_out_addi(s, TCG_TYPE_I64, a0, a1, -a2);
1513 tcg_out_opc_sub_d(s, a0, a1, a2);
1517 case INDEX_op_neg_i32:
1518 tcg_out_opc_sub_w(s, a0, TCG_REG_ZERO, a1);
1520 case INDEX_op_neg_i64:
1521 tcg_out_opc_sub_d(s, a0, TCG_REG_ZERO, a1);
1524 case INDEX_op_mul_i32:
1525 tcg_out_opc_mul_w(s, a0, a1, a2);
1527 case INDEX_op_mul_i64:
1528 tcg_out_opc_mul_d(s, a0, a1, a2);
1531 case INDEX_op_mulsh_i32:
1532 tcg_out_opc_mulh_w(s, a0, a1, a2);
1534 case INDEX_op_mulsh_i64:
1535 tcg_out_opc_mulh_d(s, a0, a1, a2);
1538 case INDEX_op_muluh_i32:
1539 tcg_out_opc_mulh_wu(s, a0, a1, a2);
1541 case INDEX_op_muluh_i64:
1542 tcg_out_opc_mulh_du(s, a0, a1, a2);
1545 case INDEX_op_div_i32:
1546 tcg_out_opc_div_w(s, a0, a1, a2);
1548 case INDEX_op_div_i64:
1549 tcg_out_opc_div_d(s, a0, a1, a2);
1552 case INDEX_op_divu_i32:
1553 tcg_out_opc_div_wu(s, a0, a1, a2);
1555 case INDEX_op_divu_i64:
1556 tcg_out_opc_div_du(s, a0, a1, a2);
1559 case INDEX_op_rem_i32:
1560 tcg_out_opc_mod_w(s, a0, a1, a2);
1562 case INDEX_op_rem_i64:
1563 tcg_out_opc_mod_d(s, a0, a1, a2);
1566 case INDEX_op_remu_i32:
1567 tcg_out_opc_mod_wu(s, a0, a1, a2);
1569 case INDEX_op_remu_i64:
1570 tcg_out_opc_mod_du(s, a0, a1, a2);
1573 case INDEX_op_setcond_i32:
1574 case INDEX_op_setcond_i64:
1575 tcg_out_setcond(s, args[3], a0, a1, a2, c2);
1578 case INDEX_op_movcond_i32:
1579 case INDEX_op_movcond_i64:
1580 tcg_out_movcond(s, args[5], a0, a1, a2, c2, args[3], args[4]);
1583 case INDEX_op_ld8s_i32:
1584 case INDEX_op_ld8s_i64:
1585 tcg_out_ldst(s, OPC_LD_B, a0, a1, a2);
1587 case INDEX_op_ld8u_i32:
1588 case INDEX_op_ld8u_i64:
1589 tcg_out_ldst(s, OPC_LD_BU, a0, a1, a2);
1591 case INDEX_op_ld16s_i32:
1592 case INDEX_op_ld16s_i64:
1593 tcg_out_ldst(s, OPC_LD_H, a0, a1, a2);
1595 case INDEX_op_ld16u_i32:
1596 case INDEX_op_ld16u_i64:
1597 tcg_out_ldst(s, OPC_LD_HU, a0, a1, a2);
1599 case INDEX_op_ld_i32:
1600 case INDEX_op_ld32s_i64:
1601 tcg_out_ldst(s, OPC_LD_W, a0, a1, a2);
1603 case INDEX_op_ld32u_i64:
1604 tcg_out_ldst(s, OPC_LD_WU, a0, a1, a2);
1606 case INDEX_op_ld_i64:
1607 tcg_out_ldst(s, OPC_LD_D, a0, a1, a2);
1610 case INDEX_op_st8_i32:
1611 case INDEX_op_st8_i64:
1612 tcg_out_ldst(s, OPC_ST_B, a0, a1, a2);
1614 case INDEX_op_st16_i32:
1615 case INDEX_op_st16_i64:
1616 tcg_out_ldst(s, OPC_ST_H, a0, a1, a2);
1618 case INDEX_op_st_i32:
1619 case INDEX_op_st32_i64:
1620 tcg_out_ldst(s, OPC_ST_W, a0, a1, a2);
1622 case INDEX_op_st_i64:
1623 tcg_out_ldst(s, OPC_ST_D, a0, a1, a2);
1626 case INDEX_op_qemu_ld_a32_i32:
1627 case INDEX_op_qemu_ld_a64_i32:
1628 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32);
1630 case INDEX_op_qemu_ld_a32_i64:
1631 case INDEX_op_qemu_ld_a64_i64:
1632 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64);
1634 case INDEX_op_qemu_ld_a32_i128:
1635 case INDEX_op_qemu_ld_a64_i128:
1636 tcg_out_qemu_ldst_i128(s, a0, a1, a2, a3, true);
1638 case INDEX_op_qemu_st_a32_i32:
1639 case INDEX_op_qemu_st_a64_i32:
1640 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32);
1642 case INDEX_op_qemu_st_a32_i64:
1643 case INDEX_op_qemu_st_a64_i64:
1644 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64);
1646 case INDEX_op_qemu_st_a32_i128:
1647 case INDEX_op_qemu_st_a64_i128:
1648 tcg_out_qemu_ldst_i128(s, a0, a1, a2, a3, false);
1651 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
1652 case INDEX_op_mov_i64:
1653 case INDEX_op_call: /* Always emitted via tcg_out_call. */
1654 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
1655 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
1656 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */
1657 case INDEX_op_ext8s_i64:
1658 case INDEX_op_ext8u_i32:
1659 case INDEX_op_ext8u_i64:
1660 case INDEX_op_ext16s_i32:
1661 case INDEX_op_ext16s_i64:
1662 case INDEX_op_ext16u_i32:
1663 case INDEX_op_ext16u_i64:
1664 case INDEX_op_ext32s_i64:
1665 case INDEX_op_ext32u_i64:
1666 case INDEX_op_ext_i32_i64:
1667 case INDEX_op_extu_i32_i64:
1668 case INDEX_op_extrl_i64_i32:
1670 g_assert_not_reached();
1674 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
1675 TCGReg rd, TCGReg rs)
1679 tcg_out_opc_vreplgr2vr_b(s, rd, rs);
1682 tcg_out_opc_vreplgr2vr_h(s, rd, rs);
1685 tcg_out_opc_vreplgr2vr_w(s, rd, rs);
1688 tcg_out_opc_vreplgr2vr_d(s, rd, rs);
1691 g_assert_not_reached();
1696 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
1697 TCGReg r, TCGReg base, intptr_t offset)
1699 /* Handle imm overflow and division (vldrepl.d imm is divided by 8) */
1700 if (offset < -0x800 || offset > 0x7ff || \
1701 (offset & ((1 << vece) - 1)) != 0) {
1702 tcg_out_addi(s, TCG_TYPE_I64, TCG_REG_TMP0, base, offset);
1703 base = TCG_REG_TMP0;
1710 tcg_out_opc_vldrepl_b(s, r, base, offset);
1713 tcg_out_opc_vldrepl_h(s, r, base, offset);
1716 tcg_out_opc_vldrepl_w(s, r, base, offset);
1719 tcg_out_opc_vldrepl_d(s, r, base, offset);
1722 g_assert_not_reached();
1727 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
1728 TCGReg rd, int64_t v64)
1730 /* Try vldi if imm can fit */
1731 int64_t value = sextract64(v64, 0, 8 << vece);
1732 if (-0x200 <= value && value <= 0x1FF) {
1733 uint32_t imm = (vece << 10) | ((uint32_t)v64 & 0x3FF);
1734 tcg_out_opc_vldi(s, rd, imm);
1738 /* TODO: vldi patterns when imm 12 is set */
1740 /* Fallback to vreplgr2vr */
1741 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, value);
1744 tcg_out_opc_vreplgr2vr_b(s, rd, TCG_REG_TMP0);
1747 tcg_out_opc_vreplgr2vr_h(s, rd, TCG_REG_TMP0);
1750 tcg_out_opc_vreplgr2vr_w(s, rd, TCG_REG_TMP0);
1753 tcg_out_opc_vreplgr2vr_d(s, rd, TCG_REG_TMP0);
1756 g_assert_not_reached();
1760 static void tcg_out_addsub_vec(TCGContext *s, unsigned vece, const TCGArg a0,
1761 const TCGArg a1, const TCGArg a2,
1762 bool a2_is_const, bool is_add)
1764 static const LoongArchInsn add_vec_insn[4] = {
1765 OPC_VADD_B, OPC_VADD_H, OPC_VADD_W, OPC_VADD_D
1767 static const LoongArchInsn add_vec_imm_insn[4] = {
1768 OPC_VADDI_BU, OPC_VADDI_HU, OPC_VADDI_WU, OPC_VADDI_DU
1770 static const LoongArchInsn sub_vec_insn[4] = {
1771 OPC_VSUB_B, OPC_VSUB_H, OPC_VSUB_W, OPC_VSUB_D
1773 static const LoongArchInsn sub_vec_imm_insn[4] = {
1774 OPC_VSUBI_BU, OPC_VSUBI_HU, OPC_VSUBI_WU, OPC_VSUBI_DU
1778 int64_t value = sextract64(a2, 0, 8 << vece);
1783 /* Try vaddi/vsubi */
1784 if (0 <= value && value <= 0x1f) {
1785 tcg_out32(s, encode_vdvjuk5_insn(add_vec_imm_insn[vece], a0, \
1788 } else if (-0x1f <= value && value < 0) {
1789 tcg_out32(s, encode_vdvjuk5_insn(sub_vec_imm_insn[vece], a0, \
1794 /* constraint TCG_CT_CONST_VADD ensures unreachable */
1795 g_assert_not_reached();
1799 tcg_out32(s, encode_vdvjvk_insn(add_vec_insn[vece], a0, a1, a2));
1801 tcg_out32(s, encode_vdvjvk_insn(sub_vec_insn[vece], a0, a1, a2));
1805 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
1806 unsigned vecl, unsigned vece,
1807 const TCGArg args[TCG_MAX_OP_ARGS],
1808 const int const_args[TCG_MAX_OP_ARGS])
1810 TCGType type = vecl + TCG_TYPE_V64;
1811 TCGArg a0, a1, a2, a3;
1812 TCGReg temp_vec = TCG_VEC_TMP0;
1814 static const LoongArchInsn cmp_vec_insn[16][4] = {
1815 [TCG_COND_EQ] = {OPC_VSEQ_B, OPC_VSEQ_H, OPC_VSEQ_W, OPC_VSEQ_D},
1816 [TCG_COND_LE] = {OPC_VSLE_B, OPC_VSLE_H, OPC_VSLE_W, OPC_VSLE_D},
1817 [TCG_COND_LEU] = {OPC_VSLE_BU, OPC_VSLE_HU, OPC_VSLE_WU, OPC_VSLE_DU},
1818 [TCG_COND_LT] = {OPC_VSLT_B, OPC_VSLT_H, OPC_VSLT_W, OPC_VSLT_D},
1819 [TCG_COND_LTU] = {OPC_VSLT_BU, OPC_VSLT_HU, OPC_VSLT_WU, OPC_VSLT_DU},
1821 static const LoongArchInsn cmp_vec_imm_insn[16][4] = {
1822 [TCG_COND_EQ] = {OPC_VSEQI_B, OPC_VSEQI_H, OPC_VSEQI_W, OPC_VSEQI_D},
1823 [TCG_COND_LE] = {OPC_VSLEI_B, OPC_VSLEI_H, OPC_VSLEI_W, OPC_VSLEI_D},
1824 [TCG_COND_LEU] = {OPC_VSLEI_BU, OPC_VSLEI_HU, OPC_VSLEI_WU, OPC_VSLEI_DU},
1825 [TCG_COND_LT] = {OPC_VSLTI_B, OPC_VSLTI_H, OPC_VSLTI_W, OPC_VSLTI_D},
1826 [TCG_COND_LTU] = {OPC_VSLTI_BU, OPC_VSLTI_HU, OPC_VSLTI_WU, OPC_VSLTI_DU},
1829 static const LoongArchInsn neg_vec_insn[4] = {
1830 OPC_VNEG_B, OPC_VNEG_H, OPC_VNEG_W, OPC_VNEG_D
1832 static const LoongArchInsn mul_vec_insn[4] = {
1833 OPC_VMUL_B, OPC_VMUL_H, OPC_VMUL_W, OPC_VMUL_D
1835 static const LoongArchInsn smin_vec_insn[4] = {
1836 OPC_VMIN_B, OPC_VMIN_H, OPC_VMIN_W, OPC_VMIN_D
1838 static const LoongArchInsn umin_vec_insn[4] = {
1839 OPC_VMIN_BU, OPC_VMIN_HU, OPC_VMIN_WU, OPC_VMIN_DU
1841 static const LoongArchInsn smax_vec_insn[4] = {
1842 OPC_VMAX_B, OPC_VMAX_H, OPC_VMAX_W, OPC_VMAX_D
1844 static const LoongArchInsn umax_vec_insn[4] = {
1845 OPC_VMAX_BU, OPC_VMAX_HU, OPC_VMAX_WU, OPC_VMAX_DU
1847 static const LoongArchInsn ssadd_vec_insn[4] = {
1848 OPC_VSADD_B, OPC_VSADD_H, OPC_VSADD_W, OPC_VSADD_D
1850 static const LoongArchInsn usadd_vec_insn[4] = {
1851 OPC_VSADD_BU, OPC_VSADD_HU, OPC_VSADD_WU, OPC_VSADD_DU
1853 static const LoongArchInsn sssub_vec_insn[4] = {
1854 OPC_VSSUB_B, OPC_VSSUB_H, OPC_VSSUB_W, OPC_VSSUB_D
1856 static const LoongArchInsn ussub_vec_insn[4] = {
1857 OPC_VSSUB_BU, OPC_VSSUB_HU, OPC_VSSUB_WU, OPC_VSSUB_DU
1859 static const LoongArchInsn shlv_vec_insn[4] = {
1860 OPC_VSLL_B, OPC_VSLL_H, OPC_VSLL_W, OPC_VSLL_D
1862 static const LoongArchInsn shrv_vec_insn[4] = {
1863 OPC_VSRL_B, OPC_VSRL_H, OPC_VSRL_W, OPC_VSRL_D
1865 static const LoongArchInsn sarv_vec_insn[4] = {
1866 OPC_VSRA_B, OPC_VSRA_H, OPC_VSRA_W, OPC_VSRA_D
1868 static const LoongArchInsn shli_vec_insn[4] = {
1869 OPC_VSLLI_B, OPC_VSLLI_H, OPC_VSLLI_W, OPC_VSLLI_D
1871 static const LoongArchInsn shri_vec_insn[4] = {
1872 OPC_VSRLI_B, OPC_VSRLI_H, OPC_VSRLI_W, OPC_VSRLI_D
1874 static const LoongArchInsn sari_vec_insn[4] = {
1875 OPC_VSRAI_B, OPC_VSRAI_H, OPC_VSRAI_W, OPC_VSRAI_D
1877 static const LoongArchInsn rotrv_vec_insn[4] = {
1878 OPC_VROTR_B, OPC_VROTR_H, OPC_VROTR_W, OPC_VROTR_D
1886 /* Currently only supports V64 & V128 */
1887 tcg_debug_assert(type == TCG_TYPE_V64 || type == TCG_TYPE_V128);
1890 case INDEX_op_st_vec:
1891 tcg_out_st(s, type, a0, a1, a2);
1893 case INDEX_op_ld_vec:
1894 tcg_out_ld(s, type, a0, a1, a2);
1896 case INDEX_op_and_vec:
1897 tcg_out_opc_vand_v(s, a0, a1, a2);
1899 case INDEX_op_andc_vec:
1901 * vandn vd, vj, vk: vd = vk & ~vj
1902 * andc_vec vd, vj, vk: vd = vj & ~vk
1903 * vk and vk are swapped
1905 tcg_out_opc_vandn_v(s, a0, a2, a1);
1907 case INDEX_op_or_vec:
1908 tcg_out_opc_vor_v(s, a0, a1, a2);
1910 case INDEX_op_orc_vec:
1911 tcg_out_opc_vorn_v(s, a0, a1, a2);
1913 case INDEX_op_xor_vec:
1914 tcg_out_opc_vxor_v(s, a0, a1, a2);
1916 case INDEX_op_nor_vec:
1917 tcg_out_opc_vnor_v(s, a0, a1, a2);
1919 case INDEX_op_not_vec:
1920 tcg_out_opc_vnor_v(s, a0, a1, a1);
1922 case INDEX_op_cmp_vec:
1924 TCGCond cond = args[3];
1925 if (const_args[2]) {
1927 * cmp_vec dest, src, value
1928 * Try vseqi/vslei/vslti
1930 int64_t value = sextract64(a2, 0, 8 << vece);
1931 if ((cond == TCG_COND_EQ || cond == TCG_COND_LE || \
1932 cond == TCG_COND_LT) && (-0x10 <= value && value <= 0x0f)) {
1933 tcg_out32(s, encode_vdvjsk5_insn(cmp_vec_imm_insn[cond][vece], \
1936 } else if ((cond == TCG_COND_LEU || cond == TCG_COND_LTU) &&
1937 (0x00 <= value && value <= 0x1f)) {
1938 tcg_out32(s, encode_vdvjuk5_insn(cmp_vec_imm_insn[cond][vece], \
1946 * cmp_vec a0, a1, temp, cond
1948 tcg_out_dupi_vec(s, type, vece, temp_vec, a2);
1952 insn = cmp_vec_insn[cond][vece];
1955 t = a1, a1 = a2, a2 = t;
1956 cond = tcg_swap_cond(cond);
1957 insn = cmp_vec_insn[cond][vece];
1958 tcg_debug_assert(insn != 0);
1960 tcg_out32(s, encode_vdvjvk_insn(insn, a0, a1, a2));
1963 case INDEX_op_add_vec:
1964 tcg_out_addsub_vec(s, vece, a0, a1, a2, const_args[2], true);
1966 case INDEX_op_sub_vec:
1967 tcg_out_addsub_vec(s, vece, a0, a1, a2, const_args[2], false);
1969 case INDEX_op_neg_vec:
1970 tcg_out32(s, encode_vdvj_insn(neg_vec_insn[vece], a0, a1));
1972 case INDEX_op_mul_vec:
1973 tcg_out32(s, encode_vdvjvk_insn(mul_vec_insn[vece], a0, a1, a2));
1975 case INDEX_op_smin_vec:
1976 tcg_out32(s, encode_vdvjvk_insn(smin_vec_insn[vece], a0, a1, a2));
1978 case INDEX_op_smax_vec:
1979 tcg_out32(s, encode_vdvjvk_insn(smax_vec_insn[vece], a0, a1, a2));
1981 case INDEX_op_umin_vec:
1982 tcg_out32(s, encode_vdvjvk_insn(umin_vec_insn[vece], a0, a1, a2));
1984 case INDEX_op_umax_vec:
1985 tcg_out32(s, encode_vdvjvk_insn(umax_vec_insn[vece], a0, a1, a2));
1987 case INDEX_op_ssadd_vec:
1988 tcg_out32(s, encode_vdvjvk_insn(ssadd_vec_insn[vece], a0, a1, a2));
1990 case INDEX_op_usadd_vec:
1991 tcg_out32(s, encode_vdvjvk_insn(usadd_vec_insn[vece], a0, a1, a2));
1993 case INDEX_op_sssub_vec:
1994 tcg_out32(s, encode_vdvjvk_insn(sssub_vec_insn[vece], a0, a1, a2));
1996 case INDEX_op_ussub_vec:
1997 tcg_out32(s, encode_vdvjvk_insn(ussub_vec_insn[vece], a0, a1, a2));
1999 case INDEX_op_shlv_vec:
2000 tcg_out32(s, encode_vdvjvk_insn(shlv_vec_insn[vece], a0, a1, a2));
2002 case INDEX_op_shrv_vec:
2003 tcg_out32(s, encode_vdvjvk_insn(shrv_vec_insn[vece], a0, a1, a2));
2005 case INDEX_op_sarv_vec:
2006 tcg_out32(s, encode_vdvjvk_insn(sarv_vec_insn[vece], a0, a1, a2));
2008 case INDEX_op_shli_vec:
2009 tcg_out32(s, encode_vdvjuk3_insn(shli_vec_insn[vece], a0, a1, a2));
2011 case INDEX_op_shri_vec:
2012 tcg_out32(s, encode_vdvjuk3_insn(shri_vec_insn[vece], a0, a1, a2));
2014 case INDEX_op_sari_vec:
2015 tcg_out32(s, encode_vdvjuk3_insn(sari_vec_insn[vece], a0, a1, a2));
2017 case INDEX_op_rotrv_vec:
2018 tcg_out32(s, encode_vdvjvk_insn(rotrv_vec_insn[vece], a0, a1, a2));
2020 case INDEX_op_rotlv_vec:
2021 /* rotlv_vec a1, a2 = rotrv_vec a1, -a2 */
2022 tcg_out32(s, encode_vdvj_insn(neg_vec_insn[vece], temp_vec, a2));
2023 tcg_out32(s, encode_vdvjvk_insn(rotrv_vec_insn[vece], a0, a1,
2026 case INDEX_op_rotli_vec:
2027 /* rotli_vec a1, a2 = rotri_vec a1, -a2 */
2028 a2 = extract32(-a2, 0, 3 + vece);
2031 tcg_out_opc_vrotri_b(s, a0, a1, a2);
2034 tcg_out_opc_vrotri_h(s, a0, a1, a2);
2037 tcg_out_opc_vrotri_w(s, a0, a1, a2);
2040 tcg_out_opc_vrotri_d(s, a0, a1, a2);
2043 g_assert_not_reached();
2046 case INDEX_op_bitsel_vec:
2047 /* vbitsel vd, vj, vk, va = bitsel_vec vd, va, vk, vj */
2048 tcg_out_opc_vbitsel_v(s, a0, a3, a2, a1);
2050 case INDEX_op_dupm_vec:
2051 tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
2054 g_assert_not_reached();
2058 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2061 case INDEX_op_ld_vec:
2062 case INDEX_op_st_vec:
2063 case INDEX_op_dup_vec:
2064 case INDEX_op_dupm_vec:
2065 case INDEX_op_cmp_vec:
2066 case INDEX_op_add_vec:
2067 case INDEX_op_sub_vec:
2068 case INDEX_op_and_vec:
2069 case INDEX_op_andc_vec:
2070 case INDEX_op_or_vec:
2071 case INDEX_op_orc_vec:
2072 case INDEX_op_xor_vec:
2073 case INDEX_op_nor_vec:
2074 case INDEX_op_not_vec:
2075 case INDEX_op_neg_vec:
2076 case INDEX_op_mul_vec:
2077 case INDEX_op_smin_vec:
2078 case INDEX_op_smax_vec:
2079 case INDEX_op_umin_vec:
2080 case INDEX_op_umax_vec:
2081 case INDEX_op_ssadd_vec:
2082 case INDEX_op_usadd_vec:
2083 case INDEX_op_sssub_vec:
2084 case INDEX_op_ussub_vec:
2085 case INDEX_op_shlv_vec:
2086 case INDEX_op_shrv_vec:
2087 case INDEX_op_sarv_vec:
2088 case INDEX_op_bitsel_vec:
2095 void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
2098 g_assert_not_reached();
2101 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2104 case INDEX_op_goto_ptr:
2107 case INDEX_op_st8_i32:
2108 case INDEX_op_st8_i64:
2109 case INDEX_op_st16_i32:
2110 case INDEX_op_st16_i64:
2111 case INDEX_op_st32_i64:
2112 case INDEX_op_st_i32:
2113 case INDEX_op_st_i64:
2114 case INDEX_op_qemu_st_a32_i32:
2115 case INDEX_op_qemu_st_a64_i32:
2116 case INDEX_op_qemu_st_a32_i64:
2117 case INDEX_op_qemu_st_a64_i64:
2118 return C_O0_I2(rZ, r);
2120 case INDEX_op_qemu_ld_a32_i128:
2121 case INDEX_op_qemu_ld_a64_i128:
2122 return C_N2_I1(r, r, r);
2124 case INDEX_op_qemu_st_a32_i128:
2125 case INDEX_op_qemu_st_a64_i128:
2126 return C_O0_I3(r, r, r);
2128 case INDEX_op_brcond_i32:
2129 case INDEX_op_brcond_i64:
2130 return C_O0_I2(rZ, rZ);
2132 case INDEX_op_ext8s_i32:
2133 case INDEX_op_ext8s_i64:
2134 case INDEX_op_ext8u_i32:
2135 case INDEX_op_ext8u_i64:
2136 case INDEX_op_ext16s_i32:
2137 case INDEX_op_ext16s_i64:
2138 case INDEX_op_ext16u_i32:
2139 case INDEX_op_ext16u_i64:
2140 case INDEX_op_ext32s_i64:
2141 case INDEX_op_ext32u_i64:
2142 case INDEX_op_extu_i32_i64:
2143 case INDEX_op_extrl_i64_i32:
2144 case INDEX_op_extrh_i64_i32:
2145 case INDEX_op_ext_i32_i64:
2146 case INDEX_op_neg_i32:
2147 case INDEX_op_neg_i64:
2148 case INDEX_op_not_i32:
2149 case INDEX_op_not_i64:
2150 case INDEX_op_extract_i32:
2151 case INDEX_op_extract_i64:
2152 case INDEX_op_bswap16_i32:
2153 case INDEX_op_bswap16_i64:
2154 case INDEX_op_bswap32_i32:
2155 case INDEX_op_bswap32_i64:
2156 case INDEX_op_bswap64_i64:
2157 case INDEX_op_ld8s_i32:
2158 case INDEX_op_ld8s_i64:
2159 case INDEX_op_ld8u_i32:
2160 case INDEX_op_ld8u_i64:
2161 case INDEX_op_ld16s_i32:
2162 case INDEX_op_ld16s_i64:
2163 case INDEX_op_ld16u_i32:
2164 case INDEX_op_ld16u_i64:
2165 case INDEX_op_ld32s_i64:
2166 case INDEX_op_ld32u_i64:
2167 case INDEX_op_ld_i32:
2168 case INDEX_op_ld_i64:
2169 case INDEX_op_qemu_ld_a32_i32:
2170 case INDEX_op_qemu_ld_a64_i32:
2171 case INDEX_op_qemu_ld_a32_i64:
2172 case INDEX_op_qemu_ld_a64_i64:
2173 return C_O1_I1(r, r);
2175 case INDEX_op_andc_i32:
2176 case INDEX_op_andc_i64:
2177 case INDEX_op_orc_i32:
2178 case INDEX_op_orc_i64:
2180 * LoongArch insns for these ops don't have reg-imm forms, but we
2181 * can express using andi/ori if ~constant satisfies
2184 return C_O1_I2(r, r, rC);
2186 case INDEX_op_shl_i32:
2187 case INDEX_op_shl_i64:
2188 case INDEX_op_shr_i32:
2189 case INDEX_op_shr_i64:
2190 case INDEX_op_sar_i32:
2191 case INDEX_op_sar_i64:
2192 case INDEX_op_rotl_i32:
2193 case INDEX_op_rotl_i64:
2194 case INDEX_op_rotr_i32:
2195 case INDEX_op_rotr_i64:
2196 return C_O1_I2(r, r, ri);
2198 case INDEX_op_add_i32:
2199 return C_O1_I2(r, r, ri);
2200 case INDEX_op_add_i64:
2201 return C_O1_I2(r, r, rJ);
2203 case INDEX_op_and_i32:
2204 case INDEX_op_and_i64:
2205 case INDEX_op_nor_i32:
2206 case INDEX_op_nor_i64:
2207 case INDEX_op_or_i32:
2208 case INDEX_op_or_i64:
2209 case INDEX_op_xor_i32:
2210 case INDEX_op_xor_i64:
2211 /* LoongArch reg-imm bitops have their imms ZERO-extended */
2212 return C_O1_I2(r, r, rU);
2214 case INDEX_op_clz_i32:
2215 case INDEX_op_clz_i64:
2216 case INDEX_op_ctz_i32:
2217 case INDEX_op_ctz_i64:
2218 return C_O1_I2(r, r, rW);
2220 case INDEX_op_deposit_i32:
2221 case INDEX_op_deposit_i64:
2222 /* Must deposit into the same register as input */
2223 return C_O1_I2(r, 0, rZ);
2225 case INDEX_op_sub_i32:
2226 case INDEX_op_setcond_i32:
2227 return C_O1_I2(r, rZ, ri);
2228 case INDEX_op_sub_i64:
2229 case INDEX_op_setcond_i64:
2230 return C_O1_I2(r, rZ, rJ);
2232 case INDEX_op_mul_i32:
2233 case INDEX_op_mul_i64:
2234 case INDEX_op_mulsh_i32:
2235 case INDEX_op_mulsh_i64:
2236 case INDEX_op_muluh_i32:
2237 case INDEX_op_muluh_i64:
2238 case INDEX_op_div_i32:
2239 case INDEX_op_div_i64:
2240 case INDEX_op_divu_i32:
2241 case INDEX_op_divu_i64:
2242 case INDEX_op_rem_i32:
2243 case INDEX_op_rem_i64:
2244 case INDEX_op_remu_i32:
2245 case INDEX_op_remu_i64:
2246 return C_O1_I2(r, rZ, rZ);
2248 case INDEX_op_movcond_i32:
2249 case INDEX_op_movcond_i64:
2250 return C_O1_I4(r, rZ, rJ, rZ, rZ);
2252 case INDEX_op_ld_vec:
2253 case INDEX_op_dupm_vec:
2254 case INDEX_op_dup_vec:
2255 return C_O1_I1(w, r);
2257 case INDEX_op_st_vec:
2258 return C_O0_I2(w, r);
2260 case INDEX_op_cmp_vec:
2261 return C_O1_I2(w, w, wM);
2263 case INDEX_op_add_vec:
2264 case INDEX_op_sub_vec:
2265 return C_O1_I2(w, w, wA);
2267 case INDEX_op_and_vec:
2268 case INDEX_op_andc_vec:
2269 case INDEX_op_or_vec:
2270 case INDEX_op_orc_vec:
2271 case INDEX_op_xor_vec:
2272 case INDEX_op_nor_vec:
2273 case INDEX_op_mul_vec:
2274 case INDEX_op_smin_vec:
2275 case INDEX_op_smax_vec:
2276 case INDEX_op_umin_vec:
2277 case INDEX_op_umax_vec:
2278 case INDEX_op_ssadd_vec:
2279 case INDEX_op_usadd_vec:
2280 case INDEX_op_sssub_vec:
2281 case INDEX_op_ussub_vec:
2282 case INDEX_op_shlv_vec:
2283 case INDEX_op_shrv_vec:
2284 case INDEX_op_sarv_vec:
2285 case INDEX_op_rotrv_vec:
2286 case INDEX_op_rotlv_vec:
2287 return C_O1_I2(w, w, w);
2289 case INDEX_op_not_vec:
2290 case INDEX_op_neg_vec:
2291 case INDEX_op_shli_vec:
2292 case INDEX_op_shri_vec:
2293 case INDEX_op_sari_vec:
2294 case INDEX_op_rotli_vec:
2295 return C_O1_I1(w, w);
2297 case INDEX_op_bitsel_vec:
2298 return C_O1_I3(w, w, w, w);
2301 g_assert_not_reached();
2305 static const int tcg_target_callee_save_regs[] = {
2306 TCG_REG_S0, /* used for the global env (TCG_AREG0) */
2316 TCG_REG_RA, /* should be last for ABI compliance */
2319 /* Stack frame parameters. */
2320 #define REG_SIZE (TCG_TARGET_REG_BITS / 8)
2321 #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
2322 #define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2323 #define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
2324 + TCG_TARGET_STACK_ALIGN - 1) \
2325 & -TCG_TARGET_STACK_ALIGN)
2326 #define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
2328 /* We're expecting to be able to use an immediate for frame allocation. */
2329 QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
2331 /* Generate global QEMU prologue and epilogue code */
2332 static void tcg_target_qemu_prologue(TCGContext *s)
2336 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
2339 tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
2340 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2341 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2342 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2345 if (!tcg_use_softmmu && guest_base) {
2346 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
2347 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2350 /* Call generated code */
2351 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2352 tcg_out_opc_jirl(s, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
2354 /* Return path for goto_ptr. Set return value to 0 */
2355 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2356 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
2359 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
2360 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2361 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2362 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2365 tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
2366 tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_RA, 0);
2369 static void tcg_out_tb_start(TCGContext *s)
2374 static void tcg_target_init(TCGContext *s)
2376 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2378 /* Server and desktop class cpus have UAL; embedded cpus do not. */
2379 if (!(hwcap & HWCAP_LOONGARCH_UAL)) {
2380 error_report("TCG: unaligned access support required; exiting");
2384 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
2385 tcg_target_available_regs[TCG_TYPE_I64] = ALL_GENERAL_REGS;
2387 tcg_target_call_clobber_regs = ALL_GENERAL_REGS | ALL_VECTOR_REGS;
2388 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
2389 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
2390 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
2391 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
2392 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
2393 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
2394 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
2395 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
2396 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
2397 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
2399 if (cpuinfo & CPUINFO_LSX) {
2400 tcg_target_available_regs[TCG_TYPE_V64] = ALL_VECTOR_REGS;
2401 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
2402 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V24);
2403 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V25);
2404 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V26);
2405 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V27);
2406 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V28);
2407 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V29);
2408 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V30);
2409 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V31);
2412 s->reserved_regs = 0;
2413 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
2414 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
2415 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
2416 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
2417 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
2418 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
2419 tcg_regset_set_reg(s->reserved_regs, TCG_REG_RESERVED);
2420 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP0);
2425 uint8_t fde_def_cfa[4];
2426 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
2429 #define ELF_HOST_MACHINE EM_LOONGARCH
2431 static const DebugFrame debug_frame = {
2432 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
2435 .h.cie.code_align = 1,
2436 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
2437 .h.cie.return_column = TCG_REG_RA,
2439 /* Total FDE size does not include the "len" member. */
2440 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2443 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */
2444 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2448 0x80 + 23, 11, /* DW_CFA_offset, s0, -88 */
2449 0x80 + 24, 10, /* DW_CFA_offset, s1, -80 */
2450 0x80 + 25, 9, /* DW_CFA_offset, s2, -72 */
2451 0x80 + 26, 8, /* DW_CFA_offset, s3, -64 */
2452 0x80 + 27, 7, /* DW_CFA_offset, s4, -56 */
2453 0x80 + 28, 6, /* DW_CFA_offset, s5, -48 */
2454 0x80 + 29, 5, /* DW_CFA_offset, s6, -40 */
2455 0x80 + 30, 4, /* DW_CFA_offset, s7, -32 */
2456 0x80 + 31, 3, /* DW_CFA_offset, s8, -24 */
2457 0x80 + 22, 2, /* DW_CFA_offset, s9, -16 */
2458 0x80 + 1 , 1, /* DW_CFA_offset, ra, -8 */
2462 void tcg_register_jit(const void *buf, size_t buf_size)
2464 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));