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 #ifdef CONFIG_DEBUG_TCG
33 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
55 "r21", /* reserved in the LP64* ABI, hence no ABI name */
69 static const int tcg_target_reg_alloc_order[] = {
70 /* Registers preserved across calls */
71 /* TCG_REG_S0 reserved for TCG_AREG0 */
82 /* Registers (potentially) clobbered across calls */
93 /* Argument registers, opposite order of allocation. */
104 static const int tcg_target_call_iarg_regs[] = {
115 static const int tcg_target_call_oarg_regs[] = {
120 #define TCG_CT_CONST_ZERO 0x100
121 #define TCG_CT_CONST_S12 0x200
122 #define TCG_CT_CONST_N12 0x400
123 #define TCG_CT_CONST_U12 0x800
124 #define TCG_CT_CONST_C12 0x1000
125 #define TCG_CT_CONST_WSZ 0x2000
127 #define ALL_GENERAL_REGS MAKE_64BIT_MASK(0, 32)
129 * For softmmu, we need to avoid conflicts with the first 5
130 * argument registers to call the helper. Some of these are
131 * also used for the tlb lookup.
133 #ifdef CONFIG_SOFTMMU
134 #define SOFTMMU_RESERVE_REGS MAKE_64BIT_MASK(TCG_REG_A0, 5)
136 #define SOFTMMU_RESERVE_REGS 0
140 static inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
142 return sextract64(val, pos, len);
145 /* test if a constant matches the constraint */
146 static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
148 if (ct & TCG_CT_CONST) {
151 if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
154 if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
157 if ((ct & TCG_CT_CONST_N12) && -val == sextreg(-val, 0, 12)) {
160 if ((ct & TCG_CT_CONST_U12) && val >= 0 && val <= 0xfff) {
163 if ((ct & TCG_CT_CONST_C12) && ~val >= 0 && ~val <= 0xfff) {
166 if ((ct & TCG_CT_CONST_WSZ) && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
177 * Relocation records defined in LoongArch ELF psABI v1.00 is way too
178 * complicated; a whopping stack machine is needed to stuff the fields, at
179 * the very least one SOP_PUSH and one SOP_POP (of the correct format) are
182 * Hence, define our own simpler relocation types. Numbers are chosen as to
183 * not collide with potential future additions to the true ELF relocation
187 /* Field Sk16, shifted right by 2; suitable for conditional jumps */
188 #define R_LOONGARCH_BR_SK16 256
189 /* Field Sd10k16, shifted right by 2; suitable for B and BL */
190 #define R_LOONGARCH_BR_SD10K16 257
192 static bool reloc_br_sk16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
194 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
195 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
197 tcg_debug_assert((offset & 3) == 0);
199 if (offset == sextreg(offset, 0, 16)) {
200 *src_rw = deposit64(*src_rw, 10, 16, offset);
207 static bool reloc_br_sd10k16(tcg_insn_unit *src_rw,
208 const tcg_insn_unit *target)
210 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
211 intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
213 tcg_debug_assert((offset & 3) == 0);
215 if (offset == sextreg(offset, 0, 26)) {
216 *src_rw = deposit64(*src_rw, 0, 10, offset >> 16); /* slot d10 */
217 *src_rw = deposit64(*src_rw, 10, 16, offset); /* slot k16 */
224 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
225 intptr_t value, intptr_t addend)
227 tcg_debug_assert(addend == 0);
229 case R_LOONGARCH_BR_SK16:
230 return reloc_br_sk16(code_ptr, (tcg_insn_unit *)value);
231 case R_LOONGARCH_BR_SD10K16:
232 return reloc_br_sd10k16(code_ptr, (tcg_insn_unit *)value);
234 g_assert_not_reached();
238 #include "tcg-insn-defs.c.inc"
244 static void tcg_out_mb(TCGContext *s, TCGArg a0)
246 /* Baseline LoongArch only has the full barrier, unfortunately. */
247 tcg_out_opc_dbar(s, 0);
250 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
259 * Conventional register-register move used in LoongArch is
260 * `or dst, src, zero`.
262 tcg_out_opc_or(s, ret, arg, TCG_REG_ZERO);
265 g_assert_not_reached();
270 static bool imm_part_needs_loading(bool high_bits_are_ones,
271 tcg_target_long part)
273 if (high_bits_are_ones) {
280 /* Loads a 32-bit immediate into rd, sign-extended. */
281 static void tcg_out_movi_i32(TCGContext *s, TCGReg rd, int32_t val)
283 tcg_target_long lo = sextreg(val, 0, 12);
284 tcg_target_long hi12 = sextreg(val, 12, 20);
286 /* Single-instruction cases. */
288 /* val fits in simm12: addi.w rd, zero, val */
289 tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val);
292 if (0x800 <= val && val <= 0xfff) {
293 /* val fits in uimm12: ori rd, zero, val */
294 tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val);
298 /* High bits must be set; load with lu12i.w + optional ori. */
299 tcg_out_opc_lu12i_w(s, rd, hi12);
301 tcg_out_opc_ori(s, rd, rd, lo & 0xfff);
305 static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
309 * LoongArch conventionally loads 64-bit immediates in at most 4 steps,
310 * with dedicated instructions for filling the respective bitfields
314 * 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
315 * +-----------------------+---------------------------------------+...
317 * +-----------------------+---------------------------------------+...
319 * 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
320 * ...+-------------------------------------+-------------------------+
322 * ...+-------------------------------------+-------------------------+
324 * Check if val belong to one of the several fast cases, before falling
325 * back to the slow path.
329 tcg_target_long val_lo, val_hi, pc_hi, offset_hi;
330 tcg_target_long hi32, hi52;
331 bool rd_high_bits_are_ones;
333 /* Value fits in signed i32. */
334 if (type == TCG_TYPE_I32 || val == (int32_t)val) {
335 tcg_out_movi_i32(s, rd, val);
339 /* PC-relative cases. */
340 pc_offset = tcg_pcrel_diff(s, (void *)val);
341 if (pc_offset == sextreg(pc_offset, 0, 22) && (pc_offset & 3) == 0) {
342 /* Single pcaddu2i. */
343 tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2);
347 if (pc_offset == (int32_t)pc_offset) {
348 /* Offset within 32 bits; load with pcalau12i + ori. */
349 val_lo = sextreg(val, 0, 12);
351 pc_hi = (val - pc_offset) >> 12;
352 offset_hi = val_hi - pc_hi;
354 tcg_debug_assert(offset_hi == sextreg(offset_hi, 0, 20));
355 tcg_out_opc_pcalau12i(s, rd, offset_hi);
357 tcg_out_opc_ori(s, rd, rd, val_lo & 0xfff);
362 hi32 = sextreg(val, 32, 20);
363 hi52 = sextreg(val, 52, 12);
365 /* Single cu52i.d case. */
366 if (ctz64(val) >= 52) {
367 tcg_out_opc_cu52i_d(s, rd, TCG_REG_ZERO, hi52);
371 /* Slow path. Initialize the low 32 bits, then concat high bits. */
372 tcg_out_movi_i32(s, rd, val);
373 rd_high_bits_are_ones = (int32_t)val < 0;
375 if (imm_part_needs_loading(rd_high_bits_are_ones, hi32)) {
376 tcg_out_opc_cu32i_d(s, rd, hi32);
377 rd_high_bits_are_ones = hi32 < 0;
380 if (imm_part_needs_loading(rd_high_bits_are_ones, hi52)) {
381 tcg_out_opc_cu52i_d(s, rd, rd, hi52);
385 static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
387 tcg_out_opc_andi(s, ret, arg, 0xff);
390 static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
392 tcg_out_opc_bstrpick_w(s, ret, arg, 0, 15);
395 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
397 tcg_out_opc_bstrpick_d(s, ret, arg, 0, 31);
400 static void tcg_out_ext8s(TCGContext *s, TCGReg ret, TCGReg arg)
402 tcg_out_opc_sext_b(s, ret, arg);
405 static void tcg_out_ext16s(TCGContext *s, TCGReg ret, TCGReg arg)
407 tcg_out_opc_sext_h(s, ret, arg);
410 static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
412 tcg_out_opc_addi_w(s, ret, arg, 0);
415 static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
416 TCGReg a0, TCGReg a1, TCGReg a2,
417 bool c2, bool is_32bit)
421 * Fast path: semantics already satisfied due to constraint and
422 * insn behavior, single instruction is enough.
424 tcg_debug_assert(a2 == (is_32bit ? 32 : 64));
425 /* all clz/ctz insns belong to DJ-format */
426 tcg_out32(s, encode_dj_insn(opc, a0, a1));
430 tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1));
431 /* a0 = a1 ? REG_TMP0 : a2 */
432 tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1);
433 tcg_out_opc_masknez(s, a0, a2, a1);
434 tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
437 static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
438 TCGReg arg1, TCGReg arg2, bool c2)
443 tcg_debug_assert(arg2 == 0);
451 tcg_out_opc_sub_d(s, ret, arg1, arg2);
454 tcg_out_opc_sltui(s, ret, tmp, 1);
460 tcg_out_opc_sub_d(s, ret, arg1, arg2);
463 tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, tmp);
466 tcg_out_opc_slt(s, ret, arg1, arg2);
469 tcg_out_opc_slt(s, ret, arg1, arg2);
470 tcg_out_opc_xori(s, ret, ret, 1);
473 tcg_out_setcond(s, TCG_COND_GE, ret, arg2, arg1, false);
476 tcg_out_setcond(s, TCG_COND_LT, ret, arg2, arg1, false);
479 tcg_out_opc_sltu(s, ret, arg1, arg2);
482 tcg_out_opc_sltu(s, ret, arg1, arg2);
483 tcg_out_opc_xori(s, ret, ret, 1);
486 tcg_out_setcond(s, TCG_COND_GEU, ret, arg2, arg1, false);
489 tcg_out_setcond(s, TCG_COND_LTU, ret, arg2, arg1, false);
492 g_assert_not_reached();
501 static const struct {
504 } tcg_brcond_to_loongarch[] = {
505 [TCG_COND_EQ] = { OPC_BEQ, false },
506 [TCG_COND_NE] = { OPC_BNE, false },
507 [TCG_COND_LT] = { OPC_BGT, true },
508 [TCG_COND_GE] = { OPC_BLE, true },
509 [TCG_COND_LE] = { OPC_BLE, false },
510 [TCG_COND_GT] = { OPC_BGT, false },
511 [TCG_COND_LTU] = { OPC_BGTU, true },
512 [TCG_COND_GEU] = { OPC_BLEU, true },
513 [TCG_COND_LEU] = { OPC_BLEU, false },
514 [TCG_COND_GTU] = { OPC_BGTU, false }
517 static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
518 TCGReg arg2, TCGLabel *l)
520 LoongArchInsn op = tcg_brcond_to_loongarch[cond].op;
522 tcg_debug_assert(op != 0);
524 if (tcg_brcond_to_loongarch[cond].swap) {
530 /* all conditional branch insns belong to DJSk16-format */
531 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SK16, l, 0);
532 tcg_out32(s, encode_djsk16_insn(op, arg1, arg2, 0));
535 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
537 TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
538 ptrdiff_t offset = tcg_pcrel_diff(s, arg);
540 tcg_debug_assert((offset & 3) == 0);
541 if (offset == sextreg(offset, 0, 28)) {
542 /* short jump: +/- 256MiB */
544 tcg_out_opc_b(s, offset >> 2);
546 tcg_out_opc_bl(s, offset >> 2);
548 } else if (offset == sextreg(offset, 0, 38)) {
549 /* long jump: +/- 256GiB */
550 tcg_target_long lo = sextreg(offset, 0, 18);
551 tcg_target_long hi = offset - lo;
552 tcg_out_opc_pcaddu18i(s, TCG_REG_TMP0, hi >> 18);
553 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
555 /* far jump: 64-bit */
556 tcg_target_long lo = sextreg((tcg_target_long)arg, 0, 18);
557 tcg_target_long hi = (tcg_target_long)arg - lo;
558 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, hi);
559 tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
563 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg)
565 tcg_out_call_int(s, arg, false);
572 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
573 const TCGArg args[TCG_MAX_OP_ARGS],
574 const int const_args[TCG_MAX_OP_ARGS])
579 int c2 = const_args[2];
586 case INDEX_op_goto_ptr:
587 tcg_out_opc_jirl(s, TCG_REG_ZERO, a0, 0);
591 tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SD10K16, arg_label(a0),
596 case INDEX_op_brcond_i32:
597 case INDEX_op_brcond_i64:
598 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
601 case INDEX_op_ext8s_i32:
602 case INDEX_op_ext8s_i64:
603 tcg_out_ext8s(s, a0, a1);
606 case INDEX_op_ext8u_i32:
607 case INDEX_op_ext8u_i64:
608 tcg_out_ext8u(s, a0, a1);
611 case INDEX_op_ext16s_i32:
612 case INDEX_op_ext16s_i64:
613 tcg_out_ext16s(s, a0, a1);
616 case INDEX_op_ext16u_i32:
617 case INDEX_op_ext16u_i64:
618 tcg_out_ext16u(s, a0, a1);
621 case INDEX_op_ext32u_i64:
622 case INDEX_op_extu_i32_i64:
623 tcg_out_ext32u(s, a0, a1);
626 case INDEX_op_ext32s_i64:
627 case INDEX_op_extrl_i64_i32:
628 case INDEX_op_ext_i32_i64:
629 tcg_out_ext32s(s, a0, a1);
632 case INDEX_op_extrh_i64_i32:
633 tcg_out_opc_srai_d(s, a0, a1, 32);
636 case INDEX_op_not_i32:
637 case INDEX_op_not_i64:
638 tcg_out_opc_nor(s, a0, a1, TCG_REG_ZERO);
641 case INDEX_op_nor_i32:
642 case INDEX_op_nor_i64:
644 tcg_out_opc_ori(s, a0, a1, a2);
645 tcg_out_opc_nor(s, a0, a0, TCG_REG_ZERO);
647 tcg_out_opc_nor(s, a0, a1, a2);
651 case INDEX_op_andc_i32:
652 case INDEX_op_andc_i64:
654 /* guaranteed to fit due to constraint */
655 tcg_out_opc_andi(s, a0, a1, ~a2);
657 tcg_out_opc_andn(s, a0, a1, a2);
661 case INDEX_op_orc_i32:
662 case INDEX_op_orc_i64:
664 /* guaranteed to fit due to constraint */
665 tcg_out_opc_ori(s, a0, a1, ~a2);
667 tcg_out_opc_orn(s, a0, a1, a2);
671 case INDEX_op_and_i32:
672 case INDEX_op_and_i64:
674 tcg_out_opc_andi(s, a0, a1, a2);
676 tcg_out_opc_and(s, a0, a1, a2);
680 case INDEX_op_or_i32:
681 case INDEX_op_or_i64:
683 tcg_out_opc_ori(s, a0, a1, a2);
685 tcg_out_opc_or(s, a0, a1, a2);
689 case INDEX_op_xor_i32:
690 case INDEX_op_xor_i64:
692 tcg_out_opc_xori(s, a0, a1, a2);
694 tcg_out_opc_xor(s, a0, a1, a2);
698 case INDEX_op_extract_i32:
699 tcg_out_opc_bstrpick_w(s, a0, a1, a2, a2 + args[3] - 1);
701 case INDEX_op_extract_i64:
702 tcg_out_opc_bstrpick_d(s, a0, a1, a2, a2 + args[3] - 1);
705 case INDEX_op_deposit_i32:
706 tcg_out_opc_bstrins_w(s, a0, a2, args[3], args[3] + args[4] - 1);
708 case INDEX_op_deposit_i64:
709 tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1);
712 case INDEX_op_bswap16_i32:
713 case INDEX_op_bswap16_i64:
714 tcg_out_opc_revb_2h(s, a0, a1);
715 if (a2 & TCG_BSWAP_OS) {
716 tcg_out_ext16s(s, a0, a0);
717 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
718 tcg_out_ext16u(s, a0, a0);
722 case INDEX_op_bswap32_i32:
723 /* All 32-bit values are computed sign-extended in the register. */
726 case INDEX_op_bswap32_i64:
727 tcg_out_opc_revb_2w(s, a0, a1);
728 if (a2 & TCG_BSWAP_OS) {
729 tcg_out_ext32s(s, a0, a0);
730 } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
731 tcg_out_ext32u(s, a0, a0);
735 case INDEX_op_bswap64_i64:
736 tcg_out_opc_revb_d(s, a0, a1);
739 case INDEX_op_clz_i32:
740 tcg_out_clzctz(s, OPC_CLZ_W, a0, a1, a2, c2, true);
742 case INDEX_op_clz_i64:
743 tcg_out_clzctz(s, OPC_CLZ_D, a0, a1, a2, c2, false);
746 case INDEX_op_ctz_i32:
747 tcg_out_clzctz(s, OPC_CTZ_W, a0, a1, a2, c2, true);
749 case INDEX_op_ctz_i64:
750 tcg_out_clzctz(s, OPC_CTZ_D, a0, a1, a2, c2, false);
753 case INDEX_op_shl_i32:
755 tcg_out_opc_slli_w(s, a0, a1, a2 & 0x1f);
757 tcg_out_opc_sll_w(s, a0, a1, a2);
760 case INDEX_op_shl_i64:
762 tcg_out_opc_slli_d(s, a0, a1, a2 & 0x3f);
764 tcg_out_opc_sll_d(s, a0, a1, a2);
768 case INDEX_op_shr_i32:
770 tcg_out_opc_srli_w(s, a0, a1, a2 & 0x1f);
772 tcg_out_opc_srl_w(s, a0, a1, a2);
775 case INDEX_op_shr_i64:
777 tcg_out_opc_srli_d(s, a0, a1, a2 & 0x3f);
779 tcg_out_opc_srl_d(s, a0, a1, a2);
783 case INDEX_op_sar_i32:
785 tcg_out_opc_srai_w(s, a0, a1, a2 & 0x1f);
787 tcg_out_opc_sra_w(s, a0, a1, a2);
790 case INDEX_op_sar_i64:
792 tcg_out_opc_srai_d(s, a0, a1, a2 & 0x3f);
794 tcg_out_opc_sra_d(s, a0, a1, a2);
798 case INDEX_op_rotl_i32:
799 /* transform into equivalent rotr/rotri */
801 tcg_out_opc_rotri_w(s, a0, a1, (32 - a2) & 0x1f);
803 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
804 tcg_out_opc_rotr_w(s, a0, a1, TCG_REG_TMP0);
807 case INDEX_op_rotl_i64:
808 /* transform into equivalent rotr/rotri */
810 tcg_out_opc_rotri_d(s, a0, a1, (64 - a2) & 0x3f);
812 tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
813 tcg_out_opc_rotr_d(s, a0, a1, TCG_REG_TMP0);
817 case INDEX_op_rotr_i32:
819 tcg_out_opc_rotri_w(s, a0, a1, a2 & 0x1f);
821 tcg_out_opc_rotr_w(s, a0, a1, a2);
824 case INDEX_op_rotr_i64:
826 tcg_out_opc_rotri_d(s, a0, a1, a2 & 0x3f);
828 tcg_out_opc_rotr_d(s, a0, a1, a2);
832 case INDEX_op_add_i32:
834 tcg_out_opc_addi_w(s, a0, a1, a2);
836 tcg_out_opc_add_w(s, a0, a1, a2);
839 case INDEX_op_add_i64:
841 tcg_out_opc_addi_d(s, a0, a1, a2);
843 tcg_out_opc_add_d(s, a0, a1, a2);
847 case INDEX_op_sub_i32:
849 tcg_out_opc_addi_w(s, a0, a1, -a2);
851 tcg_out_opc_sub_w(s, a0, a1, a2);
854 case INDEX_op_sub_i64:
856 tcg_out_opc_addi_d(s, a0, a1, -a2);
858 tcg_out_opc_sub_d(s, a0, a1, a2);
862 case INDEX_op_mul_i32:
863 tcg_out_opc_mul_w(s, a0, a1, a2);
865 case INDEX_op_mul_i64:
866 tcg_out_opc_mul_d(s, a0, a1, a2);
869 case INDEX_op_mulsh_i32:
870 tcg_out_opc_mulh_w(s, a0, a1, a2);
872 case INDEX_op_mulsh_i64:
873 tcg_out_opc_mulh_d(s, a0, a1, a2);
876 case INDEX_op_muluh_i32:
877 tcg_out_opc_mulh_wu(s, a0, a1, a2);
879 case INDEX_op_muluh_i64:
880 tcg_out_opc_mulh_du(s, a0, a1, a2);
883 case INDEX_op_div_i32:
884 tcg_out_opc_div_w(s, a0, a1, a2);
886 case INDEX_op_div_i64:
887 tcg_out_opc_div_d(s, a0, a1, a2);
890 case INDEX_op_divu_i32:
891 tcg_out_opc_div_wu(s, a0, a1, a2);
893 case INDEX_op_divu_i64:
894 tcg_out_opc_div_du(s, a0, a1, a2);
897 case INDEX_op_rem_i32:
898 tcg_out_opc_mod_w(s, a0, a1, a2);
900 case INDEX_op_rem_i64:
901 tcg_out_opc_mod_d(s, a0, a1, a2);
904 case INDEX_op_remu_i32:
905 tcg_out_opc_mod_wu(s, a0, a1, a2);
907 case INDEX_op_remu_i64:
908 tcg_out_opc_mod_du(s, a0, a1, a2);
911 case INDEX_op_setcond_i32:
912 case INDEX_op_setcond_i64:
913 tcg_out_setcond(s, args[3], a0, a1, a2, c2);
916 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
917 case INDEX_op_mov_i64:
918 case INDEX_op_call: /* Always emitted via tcg_out_call. */
920 g_assert_not_reached();
924 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
927 case INDEX_op_goto_ptr:
930 case INDEX_op_brcond_i32:
931 case INDEX_op_brcond_i64:
932 return C_O0_I2(rZ, rZ);
934 case INDEX_op_ext8s_i32:
935 case INDEX_op_ext8s_i64:
936 case INDEX_op_ext8u_i32:
937 case INDEX_op_ext8u_i64:
938 case INDEX_op_ext16s_i32:
939 case INDEX_op_ext16s_i64:
940 case INDEX_op_ext16u_i32:
941 case INDEX_op_ext16u_i64:
942 case INDEX_op_ext32s_i64:
943 case INDEX_op_ext32u_i64:
944 case INDEX_op_extu_i32_i64:
945 case INDEX_op_extrl_i64_i32:
946 case INDEX_op_extrh_i64_i32:
947 case INDEX_op_ext_i32_i64:
948 case INDEX_op_not_i32:
949 case INDEX_op_not_i64:
950 case INDEX_op_extract_i32:
951 case INDEX_op_extract_i64:
952 case INDEX_op_bswap16_i32:
953 case INDEX_op_bswap16_i64:
954 case INDEX_op_bswap32_i32:
955 case INDEX_op_bswap32_i64:
956 case INDEX_op_bswap64_i64:
957 return C_O1_I1(r, r);
959 case INDEX_op_andc_i32:
960 case INDEX_op_andc_i64:
961 case INDEX_op_orc_i32:
962 case INDEX_op_orc_i64:
964 * LoongArch insns for these ops don't have reg-imm forms, but we
965 * can express using andi/ori if ~constant satisfies
968 return C_O1_I2(r, r, rC);
970 case INDEX_op_shl_i32:
971 case INDEX_op_shl_i64:
972 case INDEX_op_shr_i32:
973 case INDEX_op_shr_i64:
974 case INDEX_op_sar_i32:
975 case INDEX_op_sar_i64:
976 case INDEX_op_rotl_i32:
977 case INDEX_op_rotl_i64:
978 case INDEX_op_rotr_i32:
979 case INDEX_op_rotr_i64:
980 return C_O1_I2(r, r, ri);
982 case INDEX_op_add_i32:
983 case INDEX_op_add_i64:
984 return C_O1_I2(r, r, rI);
986 case INDEX_op_and_i32:
987 case INDEX_op_and_i64:
988 case INDEX_op_nor_i32:
989 case INDEX_op_nor_i64:
990 case INDEX_op_or_i32:
991 case INDEX_op_or_i64:
992 case INDEX_op_xor_i32:
993 case INDEX_op_xor_i64:
994 /* LoongArch reg-imm bitops have their imms ZERO-extended */
995 return C_O1_I2(r, r, rU);
997 case INDEX_op_clz_i32:
998 case INDEX_op_clz_i64:
999 case INDEX_op_ctz_i32:
1000 case INDEX_op_ctz_i64:
1001 return C_O1_I2(r, r, rW);
1003 case INDEX_op_setcond_i32:
1004 case INDEX_op_setcond_i64:
1005 return C_O1_I2(r, r, rZ);
1007 case INDEX_op_deposit_i32:
1008 case INDEX_op_deposit_i64:
1009 /* Must deposit into the same register as input */
1010 return C_O1_I2(r, 0, rZ);
1012 case INDEX_op_sub_i32:
1013 case INDEX_op_sub_i64:
1014 return C_O1_I2(r, rZ, rN);
1016 case INDEX_op_mul_i32:
1017 case INDEX_op_mul_i64:
1018 case INDEX_op_mulsh_i32:
1019 case INDEX_op_mulsh_i64:
1020 case INDEX_op_muluh_i32:
1021 case INDEX_op_muluh_i64:
1022 case INDEX_op_div_i32:
1023 case INDEX_op_div_i64:
1024 case INDEX_op_divu_i32:
1025 case INDEX_op_divu_i64:
1026 case INDEX_op_rem_i32:
1027 case INDEX_op_rem_i64:
1028 case INDEX_op_remu_i32:
1029 case INDEX_op_remu_i64:
1030 return C_O1_I2(r, rZ, rZ);
1033 g_assert_not_reached();