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, TCGType type, int ct, int vece)
188 if (ct & TCG_CT_CONST) {
191 if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
194 if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
197 if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
200 if ((ct & TCG_CT_CONST_U12) && val >= 0 && val <= 0xfff) {
203 if ((ct & TCG_CT_CONST_C12) && ~val >= 0 && ~val <= 0xfff) {
206 if ((ct & TCG_CT_CONST_WSZ) && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
209 int64_t vec_val = sextract64(val, 0, 8 << vece);
210 if ((ct & TCG_CT_CONST_VCMP) && -0x10 <= vec_val && vec_val <= 0x1f) {
213 if ((ct & TCG_CT_CONST_VADD) && -0x1f <= vec_val && vec_val <= 0x1f) {
224 * Relocation records defined in LoongArch ELF psABI v1.00 is way too
225 * complicated; a whopping stack machine is needed to stuff the fields, at
226 * the very least one SOP_PUSH and one SOP_POP (of the correct format) are
229 * Hence, define our own simpler relocation types. Numbers are chosen as to
230 * not collide with potential future additions to the true ELF relocation
234 /* Field Sk16, shifted right by 2; suitable for conditional jumps */
235 #define R_LOONGARCH_BR_SK16 256
236 /* Field Sd10k16, shifted right by 2; suitable for B and BL */
237 #define R_LOONGARCH_BR_SD10K16 257
239 static bool reloc_br_sk16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
241 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
242 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
244 tcg_debug_assert((offset & 3) == 0);
246 if (offset == sextreg(offset, 0, 16)) {
247 *src_rw = deposit64(*src_rw, 10, 16, offset);
254 static bool reloc_br_sd10k16(tcg_insn_unit *src_rw,
255 const tcg_insn_unit *target)
257 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
258 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
260 tcg_debug_assert((offset & 3) == 0);
262 if (offset == sextreg(offset, 0, 26)) {
263 *src_rw = deposit64(*src_rw, 0, 10, offset >> 16); /* slot d10 */
264 *src_rw = deposit64(*src_rw, 10, 16, offset); /* slot k16 */
271 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
272 intptr_t value, intptr_t addend)
274 tcg_debug_assert(addend == 0);
276 case R_LOONGARCH_BR_SK16:
277 return reloc_br_sk16(code_ptr, (tcg_insn_unit *)value);
278 case R_LOONGARCH_BR_SD10K16:
279 return reloc_br_sd10k16(code_ptr, (tcg_insn_unit *)value);
281 g_assert_not_reached();
285 #include "tcg-insn-defs.c.inc"
291 static void tcg_out_mb(TCGContext *s, TCGArg a0)
293 /* Baseline LoongArch only has the full barrier, unfortunately. */
294 tcg_out_opc_dbar(s, 0);
297 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
306 * Conventional register-register move used in LoongArch is
307 * `or dst, src, zero`.
309 tcg_out_opc_or(s, ret, arg, TCG_REG_ZERO);
312 tcg_out_opc_vori_b(s, ret, arg, 0);
315 g_assert_not_reached();
320 /* Loads a 32-bit immediate into rd, sign-extended. */
321 static void tcg_out_movi_i32(TCGContext *s, TCGReg rd, int32_t val)
323 tcg_target_long lo = sextreg(val, 0, 12);
324 tcg_target_long hi12 = sextreg(val, 12, 20);
326 /* Single-instruction cases. */
328 /* val fits in uimm12: ori rd, zero, val */
329 tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val);
332 if (hi12 == sextreg(lo, 12, 20)) {
333 /* val fits in simm12: addi.w rd, zero, val */
334 tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val);
338 /* High bits must be set; load with lu12i.w + optional ori. */
339 tcg_out_opc_lu12i_w(s, rd, hi12);
341 tcg_out_opc_ori(s, rd, rd, lo & 0xfff);
345 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
349 * LoongArch conventionally loads 64-bit immediates in at most 4 steps,
350 * with dedicated instructions for filling the respective bitfields
354 * 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
355 * +-----------------------+---------------------------------------+...
357 * +-----------------------+---------------------------------------+...
359 * 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
360 * ...+-------------------------------------+-------------------------+
362 * ...+-------------------------------------+-------------------------+
364 * Check if val belong to one of the several fast cases, before falling
365 * back to the slow path.
369 tcg_target_long val_lo, val_hi, pc_hi, offset_hi;
370 tcg_target_long hi12, hi32, hi52;
372 /* Value fits in signed i32. */
373 if (type == TCG_TYPE_I32 || val == (int32_t)val) {
374 tcg_out_movi_i32(s, rd, val);
378 /* PC-relative cases. */
379 pc_offset = tcg_pcrel_diff(s, (void *)val);
380 if (pc_offset == sextreg(pc_offset, 0, 22) && (pc_offset & 3) == 0) {
381 /* Single pcaddu2i. */
382 tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2);
386 if (pc_offset == (int32_t)pc_offset) {
387 /* Offset within 32 bits; load with pcalau12i + ori. */
388 val_lo = sextreg(val, 0, 12);
390 pc_hi = (val - pc_offset) >> 12;
391 offset_hi = val_hi - pc_hi;
393 tcg_debug_assert(offset_hi == sextreg(offset_hi, 0, 20));
394 tcg_out_opc_pcalau12i(s, rd, offset_hi);
396 tcg_out_opc_ori(s, rd, rd, val_lo & 0xfff);
401 hi12 = sextreg(val, 12, 20);
402 hi32 = sextreg(val, 32, 20);
403 hi52 = sextreg(val, 52, 12);
405 /* Single cu52i.d case. */
406 if ((hi52 != 0) && (ctz64(val) >= 52)) {
407 tcg_out_opc_cu52i_d(s, rd, TCG_REG_ZERO, hi52);
411 /* Slow path. Initialize the low 32 bits, then concat high bits. */
412 tcg_out_movi_i32(s, rd, val);
414 /* Load hi32 and hi52 explicitly when they are unexpected values. */
415 if (hi32 != sextreg(hi12, 20, 20)) {
416 tcg_out_opc_cu32i_d(s, rd, hi32);
419 if (hi52 != sextreg(hi32, 20, 12)) {
420 tcg_out_opc_cu52i_d(s, rd, rd, hi52);
424 static void tcg_out_addi(TCGContext *s, TCGType type, TCGReg rd,
425 TCGReg rs, tcg_target_long imm)
427 tcg_target_long lo12 = sextreg(imm, 0, 12);
428 tcg_target_long hi16 = sextreg(imm - lo12, 16, 16);
431 * Note that there's a hole in between hi16 and lo12:
434 * 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
435 * ...+-------------------------------+-------+-----------------------+
437 * ...+-------------------------------+-------+-----------------------+
439 * For bits within that hole, it's more efficient to use LU12I and ADD.
441 if (imm == (hi16 << 16) + lo12) {
443 tcg_out_opc_addu16i_d(s, rd, rs, hi16);
446 if (type == TCG_TYPE_I32) {
447 tcg_out_opc_addi_w(s, rd, rs, lo12);
449 tcg_out_opc_addi_d(s, rd, rs, lo12);
451 tcg_out_mov(s, type, rd, rs);
454 tcg_out_movi(s, type, TCG_REG_TMP0, imm);
455 if (type == TCG_TYPE_I32) {
456 tcg_out_opc_add_w(s, rd, rs, TCG_REG_TMP0);
458 tcg_out_opc_add_d(s, rd, rs, TCG_REG_TMP0);
463 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
468 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
471 /* This function is only used for passing structs by reference. */
472 g_assert_not_reached();
475 static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
477 tcg_out_opc_andi(s, ret, arg, 0xff);
480 static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
482 tcg_out_opc_bstrpick_w(s, ret, arg, 0, 15);
485 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
487 tcg_out_opc_bstrpick_d(s, ret, arg, 0, 31);
490 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
492 tcg_out_opc_sext_b(s, ret, arg);
495 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
497 tcg_out_opc_sext_h(s, ret, arg);
500 static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
502 tcg_out_opc_addi_w(s, ret, arg, 0);
505 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg)
508 tcg_out_ext32s(s, ret, arg);
512 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg)
514 tcg_out_ext32u(s, ret, arg);
517 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg)
519 tcg_out_ext32s(s, ret, arg);
522 static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
523 TCGReg a0, TCGReg a1, TCGReg a2,
524 bool c2, bool is_32bit)
528 * Fast path: semantics already satisfied due to constraint and
529 * insn behavior, single instruction is enough.
531 tcg_debug_assert(a2 == (is_32bit ? 32 : 64));
532 /* all clz/ctz insns belong to DJ-format */
533 tcg_out32(s, encode_dj_insn(opc, a0, a1));
537 tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1));
538 /* a0 = a1 ? REG_TMP0 : a2 */
539 tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1);
540 tcg_out_opc_masknez(s, a0, a2, a1);
541 tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
544 #define SETCOND_INV TCG_TARGET_NB_REGS
545 #define SETCOND_NEZ (SETCOND_INV << 1)
546 #define SETCOND_FLAGS (SETCOND_INV | SETCOND_NEZ)
548 static int tcg_out_setcond_int(TCGContext *s, TCGCond cond, TCGReg ret,
549 TCGReg arg1, tcg_target_long arg2, bool c2)
554 case TCG_COND_EQ: /* -> NE */
555 case TCG_COND_GE: /* -> LT */
556 case TCG_COND_GEU: /* -> LTU */
557 case TCG_COND_GT: /* -> LE */
558 case TCG_COND_GTU: /* -> LEU */
559 cond = tcg_invert_cond(cond);
560 flags ^= SETCOND_INV;
570 * If we have a constant input, the most efficient way to implement
571 * LE is by adding 1 and using LT. Watch out for wrap around for LEU.
572 * We don't need to care for this for LE because the constant input
573 * is still constrained to int32_t, and INT32_MAX+1 is representable
574 * in the 64-bit temporary register.
577 if (cond == TCG_COND_LEU) {
578 /* unsigned <= -1 is true */
580 tcg_out_movi(s, TCG_TYPE_REG, ret, !(flags & SETCOND_INV));
592 cond = tcg_swap_cond(cond); /* LE -> GE */
593 cond = tcg_invert_cond(cond); /* GE -> LT */
594 flags ^= SETCOND_INV;
603 flags |= SETCOND_NEZ;
605 tcg_out_opc_xor(s, ret, arg1, arg2);
606 } else if (arg2 == 0) {
608 } else if (arg2 >= 0 && arg2 <= 0xfff) {
609 tcg_out_opc_xori(s, ret, arg1, arg2);
611 tcg_out_addi(s, TCG_TYPE_REG, ret, arg1, -arg2);
618 if (arg2 >= -0x800 && arg2 <= 0x7ff) {
619 if (cond == TCG_COND_LT) {
620 tcg_out_opc_slti(s, ret, arg1, arg2);
622 tcg_out_opc_sltui(s, ret, arg1, arg2);
626 tcg_out_movi(s, TCG_TYPE_REG, TCG_REG_TMP0, arg2);
629 if (cond == TCG_COND_LT) {
630 tcg_out_opc_slt(s, ret, arg1, arg2);
632 tcg_out_opc_sltu(s, ret, arg1, arg2);
637 g_assert_not_reached();
644 static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
645 TCGReg arg1, tcg_target_long arg2, bool c2)
647 int tmpflags = tcg_out_setcond_int(s, cond, ret, arg1, arg2, c2);
649 if (tmpflags != ret) {
650 TCGReg tmp = tmpflags & ~SETCOND_FLAGS;
652 switch (tmpflags & SETCOND_FLAGS) {
654 /* Intermediate result is boolean: simply invert. */
655 tcg_out_opc_xori(s, ret, tmp, 1);
658 /* Intermediate result is zero/non-zero: test != 0. */
659 tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, tmp);
661 case SETCOND_NEZ | SETCOND_INV:
662 /* Intermediate result is zero/non-zero: test == 0. */
663 tcg_out_opc_sltui(s, ret, tmp, 1);
666 g_assert_not_reached();
671 static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
672 TCGReg c1, tcg_target_long c2, bool const2,
673 TCGReg v1, TCGReg v2)
675 int tmpflags = tcg_out_setcond_int(s, cond, TCG_REG_TMP0, c1, c2, const2);
678 /* Standardize the test below to t != 0. */
679 if (tmpflags & SETCOND_INV) {
680 t = v1, v1 = v2, v2 = t;
683 t = tmpflags & ~SETCOND_FLAGS;
684 if (v1 == TCG_REG_ZERO) {
685 tcg_out_opc_masknez(s, ret, v2, t);
686 } else if (v2 == TCG_REG_ZERO) {
687 tcg_out_opc_maskeqz(s, ret, v1, t);
689 tcg_out_opc_masknez(s, TCG_REG_TMP2, v2, t); /* t ? 0 : v2 */
690 tcg_out_opc_maskeqz(s, TCG_REG_TMP1, v1, t); /* t ? v1 : 0 */
691 tcg_out_opc_or(s, ret, TCG_REG_TMP1, TCG_REG_TMP2);
699 static const struct {
702 } tcg_brcond_to_loongarch[] = {
703 [TCG_COND_EQ] = { OPC_BEQ, false },
704 [TCG_COND_NE] = { OPC_BNE, false },
705 [TCG_COND_LT] = { OPC_BGT, true },
706 [TCG_COND_GE] = { OPC_BLE, true },
707 [TCG_COND_LE] = { OPC_BLE, false },
708 [TCG_COND_GT] = { OPC_BGT, false },
709 [TCG_COND_LTU] = { OPC_BGTU, true },
710 [TCG_COND_GEU] = { OPC_BLEU, true },
711 [TCG_COND_LEU] = { OPC_BLEU, false },
712 [TCG_COND_GTU] = { OPC_BGTU, false }
715 static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
716 TCGReg arg2, TCGLabel *l)
718 LoongArchInsn op = tcg_brcond_to_loongarch[cond].op;
720 tcg_debug_assert(op != 0);
722 if (tcg_brcond_to_loongarch[cond].swap) {
728 /* all conditional branch insns belong to DJSk16-format */
729 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SK16, l, 0);
730 tcg_out32(s, encode_djsk16_insn(op, arg1, arg2, 0));
733 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
735 TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
736 ptrdiff_t offset = tcg_pcrel_diff(s, arg);
738 tcg_debug_assert((offset & 3) == 0);
739 if (offset == sextreg(offset, 0, 28)) {
740 /* short jump: +/- 256MiB */
742 tcg_out_opc_b(s, offset >> 2);
744 tcg_out_opc_bl(s, offset >> 2);
746 } else if (offset == sextreg(offset, 0, 38)) {
747 /* long jump: +/- 256GiB */
748 tcg_target_long lo = sextreg(offset, 0, 18);
749 tcg_target_long hi = offset - lo;
750 tcg_out_opc_pcaddu18i(s, TCG_REG_TMP0, hi >> 18);
751 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
753 /* far jump: 64-bit */
754 tcg_target_long lo = sextreg((tcg_target_long)arg, 0, 18);
755 tcg_target_long hi = (tcg_target_long)arg - lo;
756 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, hi);
757 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
761 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
762 const TCGHelperInfo *info)
764 tcg_out_call_int(s, arg, false);
771 static void tcg_out_ldst(TCGContext *s, LoongArchInsn opc, TCGReg data,
772 TCGReg addr, intptr_t offset)
774 intptr_t imm12 = sextreg(offset, 0, 12);
776 if (offset != imm12) {
777 intptr_t diff = tcg_pcrel_diff(s, (void *)offset);
779 if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
780 imm12 = sextreg(diff, 0, 12);
781 tcg_out_opc_pcaddu12i(s, TCG_REG_TMP2, (diff - imm12) >> 12);
783 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
784 if (addr != TCG_REG_ZERO) {
785 tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, addr);
803 tcg_out32(s, encode_djsk12_insn(opc, data, addr, imm12));
806 g_assert_not_reached();
810 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
811 TCGReg arg1, intptr_t arg2)
813 bool is_32bit = type == TCG_TYPE_I32;
814 tcg_out_ldst(s, is_32bit ? OPC_LD_W : OPC_LD_D, arg, arg1, arg2);
817 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
818 TCGReg arg1, intptr_t arg2)
820 bool is_32bit = type == TCG_TYPE_I32;
821 tcg_out_ldst(s, is_32bit ? OPC_ST_W : OPC_ST_D, arg, arg1, arg2);
824 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
825 TCGReg base, intptr_t ofs)
828 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
835 * Load/store helpers for SoftMMU, and qemu_ld/st implementations
838 static bool tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
841 return reloc_br_sd10k16(s->code_ptr - 1, target);
844 static const TCGLdstHelperParam ldst_helper_param = {
845 .ntmp = 1, .tmp = { TCG_REG_TMP0 }
848 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
850 MemOp opc = get_memop(l->oi);
852 /* resolve label address */
853 if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
857 tcg_out_ld_helper_args(s, l, &ldst_helper_param);
858 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE], false);
859 tcg_out_ld_helper_ret(s, l, false, &ldst_helper_param);
860 return tcg_out_goto(s, l->raddr);
863 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
865 MemOp opc = get_memop(l->oi);
867 /* resolve label address */
868 if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
872 tcg_out_st_helper_args(s, l, &ldst_helper_param);
873 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
874 return tcg_out_goto(s, l->raddr);
883 bool tcg_target_has_memory_bswap(MemOp memop)
888 /* We expect to use a 12-bit negative offset from ENV. */
889 #define MIN_TLB_MASK_TABLE_OFS -(1 << 11)
892 * For system-mode, perform the TLB load and compare.
893 * For user-mode, perform any required alignment tests.
894 * In both cases, return a TCGLabelQemuLdst structure if the slow path
895 * is required and fill in @h with the host address for the fast path.
897 static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
898 TCGReg addr_reg, MemOpIdx oi,
901 TCGType addr_type = s->addr_type;
902 TCGLabelQemuLdst *ldst = NULL;
903 MemOp opc = get_memop(oi);
906 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
907 a_bits = h->aa.align;
909 if (tcg_use_softmmu) {
910 unsigned s_bits = opc & MO_SIZE;
911 int mem_index = get_mmuidx(oi);
912 int fast_ofs = tlb_mask_table_ofs(s, mem_index);
913 int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
914 int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
916 ldst = new_ldst_label(s);
919 ldst->addrlo_reg = addr_reg;
921 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs);
922 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs);
924 tcg_out_opc_srli_d(s, TCG_REG_TMP2, addr_reg,
925 s->page_bits - CPU_TLB_ENTRY_BITS);
926 tcg_out_opc_and(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
927 tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
929 /* Load the tlb comparator and the addend. */
930 QEMU_BUILD_BUG_ON(HOST_BIG_ENDIAN);
931 tcg_out_ld(s, addr_type, TCG_REG_TMP0, TCG_REG_TMP2,
932 is_ld ? offsetof(CPUTLBEntry, addr_read)
933 : offsetof(CPUTLBEntry, addr_write));
934 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
935 offsetof(CPUTLBEntry, addend));
938 * For aligned accesses, we check the first byte and include the
939 * alignment bits within the address. For unaligned access, we
940 * check that we don't cross pages using the address of the last
941 * byte of the access.
943 if (a_bits < s_bits) {
944 unsigned a_mask = (1u << a_bits) - 1;
945 unsigned s_mask = (1u << s_bits) - 1;
946 tcg_out_addi(s, addr_type, TCG_REG_TMP1, addr_reg, s_mask - a_mask);
948 tcg_out_mov(s, addr_type, TCG_REG_TMP1, addr_reg);
950 tcg_out_opc_bstrins_d(s, TCG_REG_TMP1, TCG_REG_ZERO,
951 a_bits, s->page_bits - 1);
953 /* Compare masked address with the TLB entry. */
954 ldst->label_ptr[0] = s->code_ptr;
955 tcg_out_opc_bne(s, TCG_REG_TMP0, TCG_REG_TMP1, 0);
957 h->index = TCG_REG_TMP2;
960 ldst = new_ldst_label(s);
964 ldst->addrlo_reg = addr_reg;
967 * Without micro-architecture details, we don't know which of
968 * bstrpick or andi is faster, so use bstrpick as it's not
969 * constrained by imm field width. Not to say alignments >= 2^12
970 * are going to happen any time soon.
972 tcg_out_opc_bstrpick_d(s, TCG_REG_TMP1, addr_reg, 0, a_bits - 1);
974 ldst->label_ptr[0] = s->code_ptr;
975 tcg_out_opc_bne(s, TCG_REG_TMP1, TCG_REG_ZERO, 0);
978 h->index = guest_base ? TCG_GUEST_BASE_REG : TCG_REG_ZERO;
981 if (addr_type == TCG_TYPE_I32) {
982 h->base = TCG_REG_TMP0;
983 tcg_out_ext32u(s, h->base, addr_reg);
991 static void tcg_out_qemu_ld_indexed(TCGContext *s, MemOp opc, TCGType type,
992 TCGReg rd, HostAddress h)
994 /* Byte swapping is left to middle-end expansion. */
995 tcg_debug_assert((opc & MO_BSWAP) == 0);
997 switch (opc & MO_SSIZE) {
999 tcg_out_opc_ldx_bu(s, rd, h.base, h.index);
1002 tcg_out_opc_ldx_b(s, rd, h.base, h.index);
1005 tcg_out_opc_ldx_hu(s, rd, h.base, h.index);
1008 tcg_out_opc_ldx_h(s, rd, h.base, h.index);
1011 if (type == TCG_TYPE_I64) {
1012 tcg_out_opc_ldx_wu(s, rd, h.base, h.index);
1017 tcg_out_opc_ldx_w(s, rd, h.base, h.index);
1020 tcg_out_opc_ldx_d(s, rd, h.base, h.index);
1023 g_assert_not_reached();
1027 static void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1028 MemOpIdx oi, TCGType data_type)
1030 TCGLabelQemuLdst *ldst;
1033 ldst = prepare_host_addr(s, &h, addr_reg, oi, true);
1034 tcg_out_qemu_ld_indexed(s, get_memop(oi), data_type, data_reg, h);
1037 ldst->type = data_type;
1038 ldst->datalo_reg = data_reg;
1039 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1043 static void tcg_out_qemu_st_indexed(TCGContext *s, MemOp opc,
1044 TCGReg rd, HostAddress h)
1046 /* Byte swapping is left to middle-end expansion. */
1047 tcg_debug_assert((opc & MO_BSWAP) == 0);
1049 switch (opc & MO_SIZE) {
1051 tcg_out_opc_stx_b(s, rd, h.base, h.index);
1054 tcg_out_opc_stx_h(s, rd, h.base, h.index);
1057 tcg_out_opc_stx_w(s, rd, h.base, h.index);
1060 tcg_out_opc_stx_d(s, rd, h.base, h.index);
1063 g_assert_not_reached();
1067 static void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1068 MemOpIdx oi, TCGType data_type)
1070 TCGLabelQemuLdst *ldst;
1073 ldst = prepare_host_addr(s, &h, addr_reg, oi, false);
1074 tcg_out_qemu_st_indexed(s, get_memop(oi), data_reg, h);
1077 ldst->type = data_type;
1078 ldst->datalo_reg = data_reg;
1079 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1083 static void tcg_out_qemu_ldst_i128(TCGContext *s, TCGReg data_lo, TCGReg data_hi,
1084 TCGReg addr_reg, MemOpIdx oi, bool is_ld)
1086 TCGLabelQemuLdst *ldst;
1089 ldst = prepare_host_addr(s, &h, addr_reg, oi, is_ld);
1091 if (h.aa.atom == MO_128) {
1093 * Use VLDX/VSTX when 128-bit atomicity is required.
1094 * If address is aligned to 16-bytes, the 128-bit load/store is atomic.
1097 tcg_out_opc_vldx(s, TCG_VEC_TMP0, h.base, h.index);
1098 tcg_out_opc_vpickve2gr_d(s, data_lo, TCG_VEC_TMP0, 0);
1099 tcg_out_opc_vpickve2gr_d(s, data_hi, TCG_VEC_TMP0, 1);
1101 tcg_out_opc_vinsgr2vr_d(s, TCG_VEC_TMP0, data_lo, 0);
1102 tcg_out_opc_vinsgr2vr_d(s, TCG_VEC_TMP0, data_hi, 1);
1103 tcg_out_opc_vstx(s, TCG_VEC_TMP0, h.base, h.index);
1106 /* Otherwise use a pair of LD/ST. */
1107 TCGReg base = h.base;
1108 if (h.index != TCG_REG_ZERO) {
1109 base = TCG_REG_TMP0;
1110 tcg_out_opc_add_d(s, base, h.base, h.index);
1113 tcg_debug_assert(base != data_lo);
1114 tcg_out_opc_ld_d(s, data_lo, base, 0);
1115 tcg_out_opc_ld_d(s, data_hi, base, 8);
1117 tcg_out_opc_st_d(s, data_lo, base, 0);
1118 tcg_out_opc_st_d(s, data_hi, base, 8);
1123 ldst->type = TCG_TYPE_I128;
1124 ldst->datalo_reg = data_lo;
1125 ldst->datahi_reg = data_hi;
1126 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1134 static const tcg_insn_unit *tb_ret_addr;
1136 static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1138 /* Reuse the zeroing that exists for goto_ptr. */
1140 tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1142 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1143 tcg_out_call_int(s, tb_ret_addr, true);
1147 static void tcg_out_goto_tb(TCGContext *s, int which)
1150 * Direct branch, or load indirect address, to be patched
1151 * by tb_target_set_jmp_target. Check indirect load offset
1152 * in range early, regardless of direct branch distance,
1153 * via assert within tcg_out_opc_pcaddu2i.
1155 uintptr_t i_addr = get_jmp_target_addr(s, which);
1156 intptr_t i_disp = tcg_pcrel_diff(s, (void *)i_addr);
1158 set_jmp_insn_offset(s, which);
1159 tcg_out_opc_pcaddu2i(s, TCG_REG_TMP0, i_disp >> 2);
1161 /* Finish the load and indirect branch. */
1162 tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_TMP0, 0);
1163 tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1164 set_jmp_reset_offset(s, which);
1167 void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1168 uintptr_t jmp_rx, uintptr_t jmp_rw)
1170 uintptr_t d_addr = tb->jmp_target_addr[n];
1171 ptrdiff_t d_disp = (ptrdiff_t)(d_addr - jmp_rx) >> 2;
1174 /* Either directly branch, or load slot address for indirect branch. */
1175 if (d_disp == sextreg(d_disp, 0, 26)) {
1176 insn = encode_sd10k16_insn(OPC_B, d_disp);
1178 uintptr_t i_addr = (uintptr_t)&tb->jmp_target_addr[n];
1179 intptr_t i_disp = i_addr - jmp_rx;
1180 insn = encode_dsj20_insn(OPC_PCADDU2I, TCG_REG_TMP0, i_disp >> 2);
1183 qatomic_set((tcg_insn_unit *)jmp_rw, insn);
1184 flush_idcache_range(jmp_rx, jmp_rw, 4);
1187 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1188 const TCGArg args[TCG_MAX_OP_ARGS],
1189 const int const_args[TCG_MAX_OP_ARGS])
1191 TCGArg a0 = args[0];
1192 TCGArg a1 = args[1];
1193 TCGArg a2 = args[2];
1194 TCGArg a3 = args[3];
1195 int c2 = const_args[2];
1202 case INDEX_op_goto_ptr:
1203 tcg_out_opc_jirl(s, TCG_REG_ZERO, a0, 0);
1207 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SD10K16, arg_label(a0),
1209 tcg_out_opc_b(s, 0);
1212 case INDEX_op_brcond_i32:
1213 case INDEX_op_brcond_i64:
1214 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1217 case INDEX_op_extrh_i64_i32:
1218 tcg_out_opc_srai_d(s, a0, a1, 32);
1221 case INDEX_op_not_i32:
1222 case INDEX_op_not_i64:
1223 tcg_out_opc_nor(s, a0, a1, TCG_REG_ZERO);
1226 case INDEX_op_nor_i32:
1227 case INDEX_op_nor_i64:
1229 tcg_out_opc_ori(s, a0, a1, a2);
1230 tcg_out_opc_nor(s, a0, a0, TCG_REG_ZERO);
1232 tcg_out_opc_nor(s, a0, a1, a2);
1236 case INDEX_op_andc_i32:
1237 case INDEX_op_andc_i64:
1239 /* guaranteed to fit due to constraint */
1240 tcg_out_opc_andi(s, a0, a1, ~a2);
1242 tcg_out_opc_andn(s, a0, a1, a2);
1246 case INDEX_op_orc_i32:
1247 case INDEX_op_orc_i64:
1249 /* guaranteed to fit due to constraint */
1250 tcg_out_opc_ori(s, a0, a1, ~a2);
1252 tcg_out_opc_orn(s, a0, a1, a2);
1256 case INDEX_op_and_i32:
1257 case INDEX_op_and_i64:
1259 tcg_out_opc_andi(s, a0, a1, a2);
1261 tcg_out_opc_and(s, a0, a1, a2);
1265 case INDEX_op_or_i32:
1266 case INDEX_op_or_i64:
1268 tcg_out_opc_ori(s, a0, a1, a2);
1270 tcg_out_opc_or(s, a0, a1, a2);
1274 case INDEX_op_xor_i32:
1275 case INDEX_op_xor_i64:
1277 tcg_out_opc_xori(s, a0, a1, a2);
1279 tcg_out_opc_xor(s, a0, a1, a2);
1283 case INDEX_op_extract_i32:
1284 tcg_out_opc_bstrpick_w(s, a0, a1, a2, a2 + args[3] - 1);
1286 case INDEX_op_extract_i64:
1287 tcg_out_opc_bstrpick_d(s, a0, a1, a2, a2 + args[3] - 1);
1290 case INDEX_op_deposit_i32:
1291 tcg_out_opc_bstrins_w(s, a0, a2, args[3], args[3] + args[4] - 1);
1293 case INDEX_op_deposit_i64:
1294 tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1);
1297 case INDEX_op_bswap16_i32:
1298 case INDEX_op_bswap16_i64:
1299 tcg_out_opc_revb_2h(s, a0, a1);
1300 if (a2 & TCG_BSWAP_OS) {
1301 tcg_out_ext16s(s, TCG_TYPE_REG, a0, a0);
1302 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1303 tcg_out_ext16u(s, a0, a0);
1307 case INDEX_op_bswap32_i32:
1308 /* All 32-bit values are computed sign-extended in the register. */
1311 case INDEX_op_bswap32_i64:
1312 tcg_out_opc_revb_2w(s, a0, a1);
1313 if (a2 & TCG_BSWAP_OS) {
1314 tcg_out_ext32s(s, a0, a0);
1315 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1316 tcg_out_ext32u(s, a0, a0);
1320 case INDEX_op_bswap64_i64:
1321 tcg_out_opc_revb_d(s, a0, a1);
1324 case INDEX_op_clz_i32:
1325 tcg_out_clzctz(s, OPC_CLZ_W, a0, a1, a2, c2, true);
1327 case INDEX_op_clz_i64:
1328 tcg_out_clzctz(s, OPC_CLZ_D, a0, a1, a2, c2, false);
1331 case INDEX_op_ctz_i32:
1332 tcg_out_clzctz(s, OPC_CTZ_W, a0, a1, a2, c2, true);
1334 case INDEX_op_ctz_i64:
1335 tcg_out_clzctz(s, OPC_CTZ_D, a0, a1, a2, c2, false);
1338 case INDEX_op_shl_i32:
1340 tcg_out_opc_slli_w(s, a0, a1, a2 & 0x1f);
1342 tcg_out_opc_sll_w(s, a0, a1, a2);
1345 case INDEX_op_shl_i64:
1347 tcg_out_opc_slli_d(s, a0, a1, a2 & 0x3f);
1349 tcg_out_opc_sll_d(s, a0, a1, a2);
1353 case INDEX_op_shr_i32:
1355 tcg_out_opc_srli_w(s, a0, a1, a2 & 0x1f);
1357 tcg_out_opc_srl_w(s, a0, a1, a2);
1360 case INDEX_op_shr_i64:
1362 tcg_out_opc_srli_d(s, a0, a1, a2 & 0x3f);
1364 tcg_out_opc_srl_d(s, a0, a1, a2);
1368 case INDEX_op_sar_i32:
1370 tcg_out_opc_srai_w(s, a0, a1, a2 & 0x1f);
1372 tcg_out_opc_sra_w(s, a0, a1, a2);
1375 case INDEX_op_sar_i64:
1377 tcg_out_opc_srai_d(s, a0, a1, a2 & 0x3f);
1379 tcg_out_opc_sra_d(s, a0, a1, a2);
1383 case INDEX_op_rotl_i32:
1384 /* transform into equivalent rotr/rotri */
1386 tcg_out_opc_rotri_w(s, a0, a1, (32 - a2) & 0x1f);
1388 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
1389 tcg_out_opc_rotr_w(s, a0, a1, TCG_REG_TMP0);
1392 case INDEX_op_rotl_i64:
1393 /* transform into equivalent rotr/rotri */
1395 tcg_out_opc_rotri_d(s, a0, a1, (64 - a2) & 0x3f);
1397 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
1398 tcg_out_opc_rotr_d(s, a0, a1, TCG_REG_TMP0);
1402 case INDEX_op_rotr_i32:
1404 tcg_out_opc_rotri_w(s, a0, a1, a2 & 0x1f);
1406 tcg_out_opc_rotr_w(s, a0, a1, a2);
1409 case INDEX_op_rotr_i64:
1411 tcg_out_opc_rotri_d(s, a0, a1, a2 & 0x3f);
1413 tcg_out_opc_rotr_d(s, a0, a1, a2);
1417 case INDEX_op_add_i32:
1419 tcg_out_addi(s, TCG_TYPE_I32, a0, a1, a2);
1421 tcg_out_opc_add_w(s, a0, a1, a2);
1424 case INDEX_op_add_i64:
1426 tcg_out_addi(s, TCG_TYPE_I64, a0, a1, a2);
1428 tcg_out_opc_add_d(s, a0, a1, a2);
1432 case INDEX_op_sub_i32:
1434 tcg_out_addi(s, TCG_TYPE_I32, a0, a1, -a2);
1436 tcg_out_opc_sub_w(s, a0, a1, a2);
1439 case INDEX_op_sub_i64:
1441 tcg_out_addi(s, TCG_TYPE_I64, a0, a1, -a2);
1443 tcg_out_opc_sub_d(s, a0, a1, a2);
1447 case INDEX_op_neg_i32:
1448 tcg_out_opc_sub_w(s, a0, TCG_REG_ZERO, a1);
1450 case INDEX_op_neg_i64:
1451 tcg_out_opc_sub_d(s, a0, TCG_REG_ZERO, a1);
1454 case INDEX_op_mul_i32:
1455 tcg_out_opc_mul_w(s, a0, a1, a2);
1457 case INDEX_op_mul_i64:
1458 tcg_out_opc_mul_d(s, a0, a1, a2);
1461 case INDEX_op_mulsh_i32:
1462 tcg_out_opc_mulh_w(s, a0, a1, a2);
1464 case INDEX_op_mulsh_i64:
1465 tcg_out_opc_mulh_d(s, a0, a1, a2);
1468 case INDEX_op_muluh_i32:
1469 tcg_out_opc_mulh_wu(s, a0, a1, a2);
1471 case INDEX_op_muluh_i64:
1472 tcg_out_opc_mulh_du(s, a0, a1, a2);
1475 case INDEX_op_div_i32:
1476 tcg_out_opc_div_w(s, a0, a1, a2);
1478 case INDEX_op_div_i64:
1479 tcg_out_opc_div_d(s, a0, a1, a2);
1482 case INDEX_op_divu_i32:
1483 tcg_out_opc_div_wu(s, a0, a1, a2);
1485 case INDEX_op_divu_i64:
1486 tcg_out_opc_div_du(s, a0, a1, a2);
1489 case INDEX_op_rem_i32:
1490 tcg_out_opc_mod_w(s, a0, a1, a2);
1492 case INDEX_op_rem_i64:
1493 tcg_out_opc_mod_d(s, a0, a1, a2);
1496 case INDEX_op_remu_i32:
1497 tcg_out_opc_mod_wu(s, a0, a1, a2);
1499 case INDEX_op_remu_i64:
1500 tcg_out_opc_mod_du(s, a0, a1, a2);
1503 case INDEX_op_setcond_i32:
1504 case INDEX_op_setcond_i64:
1505 tcg_out_setcond(s, args[3], a0, a1, a2, c2);
1508 case INDEX_op_movcond_i32:
1509 case INDEX_op_movcond_i64:
1510 tcg_out_movcond(s, args[5], a0, a1, a2, c2, args[3], args[4]);
1513 case INDEX_op_ld8s_i32:
1514 case INDEX_op_ld8s_i64:
1515 tcg_out_ldst(s, OPC_LD_B, a0, a1, a2);
1517 case INDEX_op_ld8u_i32:
1518 case INDEX_op_ld8u_i64:
1519 tcg_out_ldst(s, OPC_LD_BU, a0, a1, a2);
1521 case INDEX_op_ld16s_i32:
1522 case INDEX_op_ld16s_i64:
1523 tcg_out_ldst(s, OPC_LD_H, a0, a1, a2);
1525 case INDEX_op_ld16u_i32:
1526 case INDEX_op_ld16u_i64:
1527 tcg_out_ldst(s, OPC_LD_HU, a0, a1, a2);
1529 case INDEX_op_ld_i32:
1530 case INDEX_op_ld32s_i64:
1531 tcg_out_ldst(s, OPC_LD_W, a0, a1, a2);
1533 case INDEX_op_ld32u_i64:
1534 tcg_out_ldst(s, OPC_LD_WU, a0, a1, a2);
1536 case INDEX_op_ld_i64:
1537 tcg_out_ldst(s, OPC_LD_D, a0, a1, a2);
1540 case INDEX_op_st8_i32:
1541 case INDEX_op_st8_i64:
1542 tcg_out_ldst(s, OPC_ST_B, a0, a1, a2);
1544 case INDEX_op_st16_i32:
1545 case INDEX_op_st16_i64:
1546 tcg_out_ldst(s, OPC_ST_H, a0, a1, a2);
1548 case INDEX_op_st_i32:
1549 case INDEX_op_st32_i64:
1550 tcg_out_ldst(s, OPC_ST_W, a0, a1, a2);
1552 case INDEX_op_st_i64:
1553 tcg_out_ldst(s, OPC_ST_D, a0, a1, a2);
1556 case INDEX_op_qemu_ld_a32_i32:
1557 case INDEX_op_qemu_ld_a64_i32:
1558 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32);
1560 case INDEX_op_qemu_ld_a32_i64:
1561 case INDEX_op_qemu_ld_a64_i64:
1562 tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64);
1564 case INDEX_op_qemu_ld_a32_i128:
1565 case INDEX_op_qemu_ld_a64_i128:
1566 tcg_out_qemu_ldst_i128(s, a0, a1, a2, a3, true);
1568 case INDEX_op_qemu_st_a32_i32:
1569 case INDEX_op_qemu_st_a64_i32:
1570 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32);
1572 case INDEX_op_qemu_st_a32_i64:
1573 case INDEX_op_qemu_st_a64_i64:
1574 tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64);
1576 case INDEX_op_qemu_st_a32_i128:
1577 case INDEX_op_qemu_st_a64_i128:
1578 tcg_out_qemu_ldst_i128(s, a0, a1, a2, a3, false);
1581 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
1582 case INDEX_op_mov_i64:
1583 case INDEX_op_call: /* Always emitted via tcg_out_call. */
1584 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
1585 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
1586 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */
1587 case INDEX_op_ext8s_i64:
1588 case INDEX_op_ext8u_i32:
1589 case INDEX_op_ext8u_i64:
1590 case INDEX_op_ext16s_i32:
1591 case INDEX_op_ext16s_i64:
1592 case INDEX_op_ext16u_i32:
1593 case INDEX_op_ext16u_i64:
1594 case INDEX_op_ext32s_i64:
1595 case INDEX_op_ext32u_i64:
1596 case INDEX_op_ext_i32_i64:
1597 case INDEX_op_extu_i32_i64:
1598 case INDEX_op_extrl_i64_i32:
1600 g_assert_not_reached();
1604 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
1605 TCGReg rd, TCGReg rs)
1609 tcg_out_opc_vreplgr2vr_b(s, rd, rs);
1612 tcg_out_opc_vreplgr2vr_h(s, rd, rs);
1615 tcg_out_opc_vreplgr2vr_w(s, rd, rs);
1618 tcg_out_opc_vreplgr2vr_d(s, rd, rs);
1621 g_assert_not_reached();
1626 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
1627 TCGReg r, TCGReg base, intptr_t offset)
1629 /* Handle imm overflow and division (vldrepl.d imm is divided by 8) */
1630 if (offset < -0x800 || offset > 0x7ff || \
1631 (offset & ((1 << vece) - 1)) != 0) {
1632 tcg_out_addi(s, TCG_TYPE_I64, TCG_REG_TMP0, base, offset);
1633 base = TCG_REG_TMP0;
1640 tcg_out_opc_vldrepl_b(s, r, base, offset);
1643 tcg_out_opc_vldrepl_h(s, r, base, offset);
1646 tcg_out_opc_vldrepl_w(s, r, base, offset);
1649 tcg_out_opc_vldrepl_d(s, r, base, offset);
1652 g_assert_not_reached();
1657 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
1658 TCGReg rd, int64_t v64)
1660 /* Try vldi if imm can fit */
1661 int64_t value = sextract64(v64, 0, 8 << vece);
1662 if (-0x200 <= value && value <= 0x1FF) {
1663 uint32_t imm = (vece << 10) | ((uint32_t)v64 & 0x3FF);
1664 tcg_out_opc_vldi(s, rd, imm);
1668 /* TODO: vldi patterns when imm 12 is set */
1670 /* Fallback to vreplgr2vr */
1671 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_TMP0, value);
1674 tcg_out_opc_vreplgr2vr_b(s, rd, TCG_REG_TMP0);
1677 tcg_out_opc_vreplgr2vr_h(s, rd, TCG_REG_TMP0);
1680 tcg_out_opc_vreplgr2vr_w(s, rd, TCG_REG_TMP0);
1683 tcg_out_opc_vreplgr2vr_d(s, rd, TCG_REG_TMP0);
1686 g_assert_not_reached();
1690 static void tcg_out_addsub_vec(TCGContext *s, unsigned vece, const TCGArg a0,
1691 const TCGArg a1, const TCGArg a2,
1692 bool a2_is_const, bool is_add)
1694 static const LoongArchInsn add_vec_insn[4] = {
1695 OPC_VADD_B, OPC_VADD_H, OPC_VADD_W, OPC_VADD_D
1697 static const LoongArchInsn add_vec_imm_insn[4] = {
1698 OPC_VADDI_BU, OPC_VADDI_HU, OPC_VADDI_WU, OPC_VADDI_DU
1700 static const LoongArchInsn sub_vec_insn[4] = {
1701 OPC_VSUB_B, OPC_VSUB_H, OPC_VSUB_W, OPC_VSUB_D
1703 static const LoongArchInsn sub_vec_imm_insn[4] = {
1704 OPC_VSUBI_BU, OPC_VSUBI_HU, OPC_VSUBI_WU, OPC_VSUBI_DU
1708 int64_t value = sextract64(a2, 0, 8 << vece);
1713 /* Try vaddi/vsubi */
1714 if (0 <= value && value <= 0x1f) {
1715 tcg_out32(s, encode_vdvjuk5_insn(add_vec_imm_insn[vece], a0, \
1718 } else if (-0x1f <= value && value < 0) {
1719 tcg_out32(s, encode_vdvjuk5_insn(sub_vec_imm_insn[vece], a0, \
1724 /* constraint TCG_CT_CONST_VADD ensures unreachable */
1725 g_assert_not_reached();
1729 tcg_out32(s, encode_vdvjvk_insn(add_vec_insn[vece], a0, a1, a2));
1731 tcg_out32(s, encode_vdvjvk_insn(sub_vec_insn[vece], a0, a1, a2));
1735 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
1736 unsigned vecl, unsigned vece,
1737 const TCGArg args[TCG_MAX_OP_ARGS],
1738 const int const_args[TCG_MAX_OP_ARGS])
1740 TCGType type = vecl + TCG_TYPE_V64;
1741 TCGArg a0, a1, a2, a3;
1742 TCGReg temp = TCG_REG_TMP0;
1743 TCGReg temp_vec = TCG_VEC_TMP0;
1745 static const LoongArchInsn cmp_vec_insn[16][4] = {
1746 [TCG_COND_EQ] = {OPC_VSEQ_B, OPC_VSEQ_H, OPC_VSEQ_W, OPC_VSEQ_D},
1747 [TCG_COND_LE] = {OPC_VSLE_B, OPC_VSLE_H, OPC_VSLE_W, OPC_VSLE_D},
1748 [TCG_COND_LEU] = {OPC_VSLE_BU, OPC_VSLE_HU, OPC_VSLE_WU, OPC_VSLE_DU},
1749 [TCG_COND_LT] = {OPC_VSLT_B, OPC_VSLT_H, OPC_VSLT_W, OPC_VSLT_D},
1750 [TCG_COND_LTU] = {OPC_VSLT_BU, OPC_VSLT_HU, OPC_VSLT_WU, OPC_VSLT_DU},
1752 static const LoongArchInsn cmp_vec_imm_insn[16][4] = {
1753 [TCG_COND_EQ] = {OPC_VSEQI_B, OPC_VSEQI_H, OPC_VSEQI_W, OPC_VSEQI_D},
1754 [TCG_COND_LE] = {OPC_VSLEI_B, OPC_VSLEI_H, OPC_VSLEI_W, OPC_VSLEI_D},
1755 [TCG_COND_LEU] = {OPC_VSLEI_BU, OPC_VSLEI_HU, OPC_VSLEI_WU, OPC_VSLEI_DU},
1756 [TCG_COND_LT] = {OPC_VSLTI_B, OPC_VSLTI_H, OPC_VSLTI_W, OPC_VSLTI_D},
1757 [TCG_COND_LTU] = {OPC_VSLTI_BU, OPC_VSLTI_HU, OPC_VSLTI_WU, OPC_VSLTI_DU},
1760 static const LoongArchInsn neg_vec_insn[4] = {
1761 OPC_VNEG_B, OPC_VNEG_H, OPC_VNEG_W, OPC_VNEG_D
1763 static const LoongArchInsn mul_vec_insn[4] = {
1764 OPC_VMUL_B, OPC_VMUL_H, OPC_VMUL_W, OPC_VMUL_D
1766 static const LoongArchInsn smin_vec_insn[4] = {
1767 OPC_VMIN_B, OPC_VMIN_H, OPC_VMIN_W, OPC_VMIN_D
1769 static const LoongArchInsn umin_vec_insn[4] = {
1770 OPC_VMIN_BU, OPC_VMIN_HU, OPC_VMIN_WU, OPC_VMIN_DU
1772 static const LoongArchInsn smax_vec_insn[4] = {
1773 OPC_VMAX_B, OPC_VMAX_H, OPC_VMAX_W, OPC_VMAX_D
1775 static const LoongArchInsn umax_vec_insn[4] = {
1776 OPC_VMAX_BU, OPC_VMAX_HU, OPC_VMAX_WU, OPC_VMAX_DU
1778 static const LoongArchInsn ssadd_vec_insn[4] = {
1779 OPC_VSADD_B, OPC_VSADD_H, OPC_VSADD_W, OPC_VSADD_D
1781 static const LoongArchInsn usadd_vec_insn[4] = {
1782 OPC_VSADD_BU, OPC_VSADD_HU, OPC_VSADD_WU, OPC_VSADD_DU
1784 static const LoongArchInsn sssub_vec_insn[4] = {
1785 OPC_VSSUB_B, OPC_VSSUB_H, OPC_VSSUB_W, OPC_VSSUB_D
1787 static const LoongArchInsn ussub_vec_insn[4] = {
1788 OPC_VSSUB_BU, OPC_VSSUB_HU, OPC_VSSUB_WU, OPC_VSSUB_DU
1790 static const LoongArchInsn shlv_vec_insn[4] = {
1791 OPC_VSLL_B, OPC_VSLL_H, OPC_VSLL_W, OPC_VSLL_D
1793 static const LoongArchInsn shrv_vec_insn[4] = {
1794 OPC_VSRL_B, OPC_VSRL_H, OPC_VSRL_W, OPC_VSRL_D
1796 static const LoongArchInsn sarv_vec_insn[4] = {
1797 OPC_VSRA_B, OPC_VSRA_H, OPC_VSRA_W, OPC_VSRA_D
1799 static const LoongArchInsn shli_vec_insn[4] = {
1800 OPC_VSLLI_B, OPC_VSLLI_H, OPC_VSLLI_W, OPC_VSLLI_D
1802 static const LoongArchInsn shri_vec_insn[4] = {
1803 OPC_VSRLI_B, OPC_VSRLI_H, OPC_VSRLI_W, OPC_VSRLI_D
1805 static const LoongArchInsn sari_vec_insn[4] = {
1806 OPC_VSRAI_B, OPC_VSRAI_H, OPC_VSRAI_W, OPC_VSRAI_D
1808 static const LoongArchInsn rotrv_vec_insn[4] = {
1809 OPC_VROTR_B, OPC_VROTR_H, OPC_VROTR_W, OPC_VROTR_D
1817 /* Currently only supports V128 */
1818 tcg_debug_assert(type == TCG_TYPE_V128);
1821 case INDEX_op_st_vec:
1822 /* Try to fit vst imm */
1823 if (-0x800 <= a2 && a2 <= 0x7ff) {
1824 tcg_out_opc_vst(s, a0, a1, a2);
1826 tcg_out_movi(s, TCG_TYPE_I64, temp, a2);
1827 tcg_out_opc_vstx(s, a0, a1, temp);
1830 case INDEX_op_ld_vec:
1831 /* Try to fit vld imm */
1832 if (-0x800 <= a2 && a2 <= 0x7ff) {
1833 tcg_out_opc_vld(s, a0, a1, a2);
1835 tcg_out_movi(s, TCG_TYPE_I64, temp, a2);
1836 tcg_out_opc_vldx(s, a0, a1, temp);
1839 case INDEX_op_and_vec:
1840 tcg_out_opc_vand_v(s, a0, a1, a2);
1842 case INDEX_op_andc_vec:
1844 * vandn vd, vj, vk: vd = vk & ~vj
1845 * andc_vec vd, vj, vk: vd = vj & ~vk
1846 * vk and vk are swapped
1848 tcg_out_opc_vandn_v(s, a0, a2, a1);
1850 case INDEX_op_or_vec:
1851 tcg_out_opc_vor_v(s, a0, a1, a2);
1853 case INDEX_op_orc_vec:
1854 tcg_out_opc_vorn_v(s, a0, a1, a2);
1856 case INDEX_op_xor_vec:
1857 tcg_out_opc_vxor_v(s, a0, a1, a2);
1859 case INDEX_op_nor_vec:
1860 tcg_out_opc_vnor_v(s, a0, a1, a2);
1862 case INDEX_op_not_vec:
1863 tcg_out_opc_vnor_v(s, a0, a1, a1);
1865 case INDEX_op_cmp_vec:
1867 TCGCond cond = args[3];
1868 if (const_args[2]) {
1870 * cmp_vec dest, src, value
1871 * Try vseqi/vslei/vslti
1873 int64_t value = sextract64(a2, 0, 8 << vece);
1874 if ((cond == TCG_COND_EQ || cond == TCG_COND_LE || \
1875 cond == TCG_COND_LT) && (-0x10 <= value && value <= 0x0f)) {
1876 tcg_out32(s, encode_vdvjsk5_insn(cmp_vec_imm_insn[cond][vece], \
1879 } else if ((cond == TCG_COND_LEU || cond == TCG_COND_LTU) &&
1880 (0x00 <= value && value <= 0x1f)) {
1881 tcg_out32(s, encode_vdvjuk5_insn(cmp_vec_imm_insn[cond][vece], \
1889 * cmp_vec a0, a1, temp, cond
1891 tcg_out_dupi_vec(s, type, vece, temp_vec, a2);
1895 insn = cmp_vec_insn[cond][vece];
1898 t = a1, a1 = a2, a2 = t;
1899 cond = tcg_swap_cond(cond);
1900 insn = cmp_vec_insn[cond][vece];
1901 tcg_debug_assert(insn != 0);
1903 tcg_out32(s, encode_vdvjvk_insn(insn, a0, a1, a2));
1906 case INDEX_op_add_vec:
1907 tcg_out_addsub_vec(s, vece, a0, a1, a2, const_args[2], true);
1909 case INDEX_op_sub_vec:
1910 tcg_out_addsub_vec(s, vece, a0, a1, a2, const_args[2], false);
1912 case INDEX_op_neg_vec:
1913 tcg_out32(s, encode_vdvj_insn(neg_vec_insn[vece], a0, a1));
1915 case INDEX_op_mul_vec:
1916 tcg_out32(s, encode_vdvjvk_insn(mul_vec_insn[vece], a0, a1, a2));
1918 case INDEX_op_smin_vec:
1919 tcg_out32(s, encode_vdvjvk_insn(smin_vec_insn[vece], a0, a1, a2));
1921 case INDEX_op_smax_vec:
1922 tcg_out32(s, encode_vdvjvk_insn(smax_vec_insn[vece], a0, a1, a2));
1924 case INDEX_op_umin_vec:
1925 tcg_out32(s, encode_vdvjvk_insn(umin_vec_insn[vece], a0, a1, a2));
1927 case INDEX_op_umax_vec:
1928 tcg_out32(s, encode_vdvjvk_insn(umax_vec_insn[vece], a0, a1, a2));
1930 case INDEX_op_ssadd_vec:
1931 tcg_out32(s, encode_vdvjvk_insn(ssadd_vec_insn[vece], a0, a1, a2));
1933 case INDEX_op_usadd_vec:
1934 tcg_out32(s, encode_vdvjvk_insn(usadd_vec_insn[vece], a0, a1, a2));
1936 case INDEX_op_sssub_vec:
1937 tcg_out32(s, encode_vdvjvk_insn(sssub_vec_insn[vece], a0, a1, a2));
1939 case INDEX_op_ussub_vec:
1940 tcg_out32(s, encode_vdvjvk_insn(ussub_vec_insn[vece], a0, a1, a2));
1942 case INDEX_op_shlv_vec:
1943 tcg_out32(s, encode_vdvjvk_insn(shlv_vec_insn[vece], a0, a1, a2));
1945 case INDEX_op_shrv_vec:
1946 tcg_out32(s, encode_vdvjvk_insn(shrv_vec_insn[vece], a0, a1, a2));
1948 case INDEX_op_sarv_vec:
1949 tcg_out32(s, encode_vdvjvk_insn(sarv_vec_insn[vece], a0, a1, a2));
1951 case INDEX_op_shli_vec:
1952 tcg_out32(s, encode_vdvjuk3_insn(shli_vec_insn[vece], a0, a1, a2));
1954 case INDEX_op_shri_vec:
1955 tcg_out32(s, encode_vdvjuk3_insn(shri_vec_insn[vece], a0, a1, a2));
1957 case INDEX_op_sari_vec:
1958 tcg_out32(s, encode_vdvjuk3_insn(sari_vec_insn[vece], a0, a1, a2));
1960 case INDEX_op_rotrv_vec:
1961 tcg_out32(s, encode_vdvjvk_insn(rotrv_vec_insn[vece], a0, a1, a2));
1963 case INDEX_op_rotlv_vec:
1964 /* rotlv_vec a1, a2 = rotrv_vec a1, -a2 */
1965 tcg_out32(s, encode_vdvj_insn(neg_vec_insn[vece], temp_vec, a2));
1966 tcg_out32(s, encode_vdvjvk_insn(rotrv_vec_insn[vece], a0, a1,
1969 case INDEX_op_rotli_vec:
1970 /* rotli_vec a1, a2 = rotri_vec a1, -a2 */
1971 a2 = extract32(-a2, 0, 3 + vece);
1974 tcg_out_opc_vrotri_b(s, a0, a1, a2);
1977 tcg_out_opc_vrotri_h(s, a0, a1, a2);
1980 tcg_out_opc_vrotri_w(s, a0, a1, a2);
1983 tcg_out_opc_vrotri_d(s, a0, a1, a2);
1986 g_assert_not_reached();
1989 case INDEX_op_bitsel_vec:
1990 /* vbitsel vd, vj, vk, va = bitsel_vec vd, va, vk, vj */
1991 tcg_out_opc_vbitsel_v(s, a0, a3, a2, a1);
1993 case INDEX_op_dupm_vec:
1994 tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
1997 g_assert_not_reached();
2001 int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2004 case INDEX_op_ld_vec:
2005 case INDEX_op_st_vec:
2006 case INDEX_op_dup_vec:
2007 case INDEX_op_dupm_vec:
2008 case INDEX_op_cmp_vec:
2009 case INDEX_op_add_vec:
2010 case INDEX_op_sub_vec:
2011 case INDEX_op_and_vec:
2012 case INDEX_op_andc_vec:
2013 case INDEX_op_or_vec:
2014 case INDEX_op_orc_vec:
2015 case INDEX_op_xor_vec:
2016 case INDEX_op_nor_vec:
2017 case INDEX_op_not_vec:
2018 case INDEX_op_neg_vec:
2019 case INDEX_op_mul_vec:
2020 case INDEX_op_smin_vec:
2021 case INDEX_op_smax_vec:
2022 case INDEX_op_umin_vec:
2023 case INDEX_op_umax_vec:
2024 case INDEX_op_ssadd_vec:
2025 case INDEX_op_usadd_vec:
2026 case INDEX_op_sssub_vec:
2027 case INDEX_op_ussub_vec:
2028 case INDEX_op_shlv_vec:
2029 case INDEX_op_shrv_vec:
2030 case INDEX_op_sarv_vec:
2031 case INDEX_op_bitsel_vec:
2038 void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
2041 g_assert_not_reached();
2044 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2047 case INDEX_op_goto_ptr:
2050 case INDEX_op_st8_i32:
2051 case INDEX_op_st8_i64:
2052 case INDEX_op_st16_i32:
2053 case INDEX_op_st16_i64:
2054 case INDEX_op_st32_i64:
2055 case INDEX_op_st_i32:
2056 case INDEX_op_st_i64:
2057 case INDEX_op_qemu_st_a32_i32:
2058 case INDEX_op_qemu_st_a64_i32:
2059 case INDEX_op_qemu_st_a32_i64:
2060 case INDEX_op_qemu_st_a64_i64:
2061 return C_O0_I2(rZ, r);
2063 case INDEX_op_qemu_ld_a32_i128:
2064 case INDEX_op_qemu_ld_a64_i128:
2065 return C_N2_I1(r, r, r);
2067 case INDEX_op_qemu_st_a32_i128:
2068 case INDEX_op_qemu_st_a64_i128:
2069 return C_O0_I3(r, r, r);
2071 case INDEX_op_brcond_i32:
2072 case INDEX_op_brcond_i64:
2073 return C_O0_I2(rZ, rZ);
2075 case INDEX_op_ext8s_i32:
2076 case INDEX_op_ext8s_i64:
2077 case INDEX_op_ext8u_i32:
2078 case INDEX_op_ext8u_i64:
2079 case INDEX_op_ext16s_i32:
2080 case INDEX_op_ext16s_i64:
2081 case INDEX_op_ext16u_i32:
2082 case INDEX_op_ext16u_i64:
2083 case INDEX_op_ext32s_i64:
2084 case INDEX_op_ext32u_i64:
2085 case INDEX_op_extu_i32_i64:
2086 case INDEX_op_extrl_i64_i32:
2087 case INDEX_op_extrh_i64_i32:
2088 case INDEX_op_ext_i32_i64:
2089 case INDEX_op_neg_i32:
2090 case INDEX_op_neg_i64:
2091 case INDEX_op_not_i32:
2092 case INDEX_op_not_i64:
2093 case INDEX_op_extract_i32:
2094 case INDEX_op_extract_i64:
2095 case INDEX_op_bswap16_i32:
2096 case INDEX_op_bswap16_i64:
2097 case INDEX_op_bswap32_i32:
2098 case INDEX_op_bswap32_i64:
2099 case INDEX_op_bswap64_i64:
2100 case INDEX_op_ld8s_i32:
2101 case INDEX_op_ld8s_i64:
2102 case INDEX_op_ld8u_i32:
2103 case INDEX_op_ld8u_i64:
2104 case INDEX_op_ld16s_i32:
2105 case INDEX_op_ld16s_i64:
2106 case INDEX_op_ld16u_i32:
2107 case INDEX_op_ld16u_i64:
2108 case INDEX_op_ld32s_i64:
2109 case INDEX_op_ld32u_i64:
2110 case INDEX_op_ld_i32:
2111 case INDEX_op_ld_i64:
2112 case INDEX_op_qemu_ld_a32_i32:
2113 case INDEX_op_qemu_ld_a64_i32:
2114 case INDEX_op_qemu_ld_a32_i64:
2115 case INDEX_op_qemu_ld_a64_i64:
2116 return C_O1_I1(r, r);
2118 case INDEX_op_andc_i32:
2119 case INDEX_op_andc_i64:
2120 case INDEX_op_orc_i32:
2121 case INDEX_op_orc_i64:
2123 * LoongArch insns for these ops don't have reg-imm forms, but we
2124 * can express using andi/ori if ~constant satisfies
2127 return C_O1_I2(r, r, rC);
2129 case INDEX_op_shl_i32:
2130 case INDEX_op_shl_i64:
2131 case INDEX_op_shr_i32:
2132 case INDEX_op_shr_i64:
2133 case INDEX_op_sar_i32:
2134 case INDEX_op_sar_i64:
2135 case INDEX_op_rotl_i32:
2136 case INDEX_op_rotl_i64:
2137 case INDEX_op_rotr_i32:
2138 case INDEX_op_rotr_i64:
2139 return C_O1_I2(r, r, ri);
2141 case INDEX_op_add_i32:
2142 return C_O1_I2(r, r, ri);
2143 case INDEX_op_add_i64:
2144 return C_O1_I2(r, r, rJ);
2146 case INDEX_op_and_i32:
2147 case INDEX_op_and_i64:
2148 case INDEX_op_nor_i32:
2149 case INDEX_op_nor_i64:
2150 case INDEX_op_or_i32:
2151 case INDEX_op_or_i64:
2152 case INDEX_op_xor_i32:
2153 case INDEX_op_xor_i64:
2154 /* LoongArch reg-imm bitops have their imms ZERO-extended */
2155 return C_O1_I2(r, r, rU);
2157 case INDEX_op_clz_i32:
2158 case INDEX_op_clz_i64:
2159 case INDEX_op_ctz_i32:
2160 case INDEX_op_ctz_i64:
2161 return C_O1_I2(r, r, rW);
2163 case INDEX_op_deposit_i32:
2164 case INDEX_op_deposit_i64:
2165 /* Must deposit into the same register as input */
2166 return C_O1_I2(r, 0, rZ);
2168 case INDEX_op_sub_i32:
2169 case INDEX_op_setcond_i32:
2170 return C_O1_I2(r, rZ, ri);
2171 case INDEX_op_sub_i64:
2172 case INDEX_op_setcond_i64:
2173 return C_O1_I2(r, rZ, rJ);
2175 case INDEX_op_mul_i32:
2176 case INDEX_op_mul_i64:
2177 case INDEX_op_mulsh_i32:
2178 case INDEX_op_mulsh_i64:
2179 case INDEX_op_muluh_i32:
2180 case INDEX_op_muluh_i64:
2181 case INDEX_op_div_i32:
2182 case INDEX_op_div_i64:
2183 case INDEX_op_divu_i32:
2184 case INDEX_op_divu_i64:
2185 case INDEX_op_rem_i32:
2186 case INDEX_op_rem_i64:
2187 case INDEX_op_remu_i32:
2188 case INDEX_op_remu_i64:
2189 return C_O1_I2(r, rZ, rZ);
2191 case INDEX_op_movcond_i32:
2192 case INDEX_op_movcond_i64:
2193 return C_O1_I4(r, rZ, rJ, rZ, rZ);
2195 case INDEX_op_ld_vec:
2196 case INDEX_op_dupm_vec:
2197 case INDEX_op_dup_vec:
2198 return C_O1_I1(w, r);
2200 case INDEX_op_st_vec:
2201 return C_O0_I2(w, r);
2203 case INDEX_op_cmp_vec:
2204 return C_O1_I2(w, w, wM);
2206 case INDEX_op_add_vec:
2207 case INDEX_op_sub_vec:
2208 return C_O1_I2(w, w, wA);
2210 case INDEX_op_and_vec:
2211 case INDEX_op_andc_vec:
2212 case INDEX_op_or_vec:
2213 case INDEX_op_orc_vec:
2214 case INDEX_op_xor_vec:
2215 case INDEX_op_nor_vec:
2216 case INDEX_op_mul_vec:
2217 case INDEX_op_smin_vec:
2218 case INDEX_op_smax_vec:
2219 case INDEX_op_umin_vec:
2220 case INDEX_op_umax_vec:
2221 case INDEX_op_ssadd_vec:
2222 case INDEX_op_usadd_vec:
2223 case INDEX_op_sssub_vec:
2224 case INDEX_op_ussub_vec:
2225 case INDEX_op_shlv_vec:
2226 case INDEX_op_shrv_vec:
2227 case INDEX_op_sarv_vec:
2228 case INDEX_op_rotrv_vec:
2229 case INDEX_op_rotlv_vec:
2230 return C_O1_I2(w, w, w);
2232 case INDEX_op_not_vec:
2233 case INDEX_op_neg_vec:
2234 case INDEX_op_shli_vec:
2235 case INDEX_op_shri_vec:
2236 case INDEX_op_sari_vec:
2237 case INDEX_op_rotli_vec:
2238 return C_O1_I1(w, w);
2240 case INDEX_op_bitsel_vec:
2241 return C_O1_I3(w, w, w, w);
2244 g_assert_not_reached();
2248 static const int tcg_target_callee_save_regs[] = {
2249 TCG_REG_S0, /* used for the global env (TCG_AREG0) */
2259 TCG_REG_RA, /* should be last for ABI compliance */
2262 /* Stack frame parameters. */
2263 #define REG_SIZE (TCG_TARGET_REG_BITS / 8)
2264 #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
2265 #define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2266 #define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
2267 + TCG_TARGET_STACK_ALIGN - 1) \
2268 & -TCG_TARGET_STACK_ALIGN)
2269 #define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
2271 /* We're expecting to be able to use an immediate for frame allocation. */
2272 QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
2274 /* Generate global QEMU prologue and epilogue code */
2275 static void tcg_target_qemu_prologue(TCGContext *s)
2279 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
2282 tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
2283 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2284 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2285 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2288 if (!tcg_use_softmmu && guest_base) {
2289 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
2290 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2293 /* Call generated code */
2294 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2295 tcg_out_opc_jirl(s, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
2297 /* Return path for goto_ptr. Set return value to 0 */
2298 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2299 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
2302 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
2303 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2304 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2305 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2308 tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
2309 tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_RA, 0);
2312 static void tcg_out_tb_start(TCGContext *s)
2317 static void tcg_target_init(TCGContext *s)
2319 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2321 /* Server and desktop class cpus have UAL; embedded cpus do not. */
2322 if (!(hwcap & HWCAP_LOONGARCH_UAL)) {
2323 error_report("TCG: unaligned access support required; exiting");
2327 tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
2328 tcg_target_available_regs[TCG_TYPE_I64] = ALL_GENERAL_REGS;
2330 tcg_target_call_clobber_regs = ALL_GENERAL_REGS;
2331 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
2332 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
2333 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
2334 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
2335 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
2336 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
2337 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
2338 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
2339 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
2340 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
2342 if (cpuinfo & CPUINFO_LSX) {
2343 tcg_target_available_regs[TCG_TYPE_V128] = ALL_VECTOR_REGS;
2344 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V24);
2345 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V25);
2346 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V26);
2347 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V27);
2348 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V28);
2349 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V29);
2350 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V30);
2351 tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_V31);
2354 s->reserved_regs = 0;
2355 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
2356 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
2357 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
2358 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
2359 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
2360 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
2361 tcg_regset_set_reg(s->reserved_regs, TCG_REG_RESERVED);
2362 tcg_regset_set_reg(s->reserved_regs, TCG_VEC_TMP0);
2367 uint8_t fde_def_cfa[4];
2368 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
2371 #define ELF_HOST_MACHINE EM_LOONGARCH
2373 static const DebugFrame debug_frame = {
2374 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
2377 .h.cie.code_align = 1,
2378 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
2379 .h.cie.return_column = TCG_REG_RA,
2381 /* Total FDE size does not include the "len" member. */
2382 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2385 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */
2386 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2390 0x80 + 23, 11, /* DW_CFA_offset, s0, -88 */
2391 0x80 + 24, 10, /* DW_CFA_offset, s1, -80 */
2392 0x80 + 25, 9, /* DW_CFA_offset, s2, -72 */
2393 0x80 + 26, 8, /* DW_CFA_offset, s3, -64 */
2394 0x80 + 27, 7, /* DW_CFA_offset, s4, -56 */
2395 0x80 + 28, 6, /* DW_CFA_offset, s5, -48 */
2396 0x80 + 29, 5, /* DW_CFA_offset, s6, -40 */
2397 0x80 + 30, 4, /* DW_CFA_offset, s7, -32 */
2398 0x80 + 31, 3, /* DW_CFA_offset, s8, -24 */
2399 0x80 + 22, 2, /* DW_CFA_offset, s9, -16 */
2400 0x80 + 1 , 1, /* DW_CFA_offset, ra, -8 */
2404 void tcg_register_jit(const void *buf, size_t buf_size)
2406 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));