2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org>
5 * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net>
6 * Based on i386/tcg-target.c - Copyright (c) 2008 Fabrice Bellard
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include "../tcg-ldst.c.inc"
35 #if TCG_TARGET_REG_BITS == 32
36 # define LO_OFF (MIPS_BE * 4)
37 # define HI_OFF (4 - LO_OFF)
39 /* To assert at compile-time that these values are never used
40 for TCG_TARGET_REG_BITS == 64. */
42 # define LO_OFF link_error()
43 # define HI_OFF link_error()
46 #ifdef CONFIG_DEBUG_TCG
47 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
83 #define TCG_TMP0 TCG_REG_AT
84 #define TCG_TMP1 TCG_REG_T9
85 #define TCG_TMP2 TCG_REG_T8
86 #define TCG_TMP3 TCG_REG_T7
88 #ifndef CONFIG_SOFTMMU
89 #define TCG_GUEST_BASE_REG TCG_REG_S7
91 #if TCG_TARGET_REG_BITS == 64
92 #define TCG_REG_TB TCG_REG_S6
94 #define TCG_REG_TB (qemu_build_not_reached(), TCG_REG_ZERO)
97 /* check if we really need so many registers :P */
98 static const int tcg_target_reg_alloc_order[] = {
99 /* Call saved registers. */
110 /* Call clobbered registers. */
120 /* Argument registers, opposite order of allocation. */
131 static const TCGReg tcg_target_call_iarg_regs[] = {
136 #if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
144 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
146 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
147 tcg_debug_assert(slot >= 0 && slot <= 1);
148 return TCG_REG_V0 + slot;
151 static const tcg_insn_unit *tb_ret_addr;
152 static const tcg_insn_unit *bswap32_addr;
153 static const tcg_insn_unit *bswap32u_addr;
154 static const tcg_insn_unit *bswap64_addr;
156 static bool reloc_pc16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
158 /* Let the compiler perform the right-shift as part of the arithmetic. */
159 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
160 ptrdiff_t disp = target - (src_rx + 1);
161 if (disp == (int16_t)disp) {
162 *src_rw = deposit32(*src_rw, 0, 16, disp);
168 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
169 intptr_t value, intptr_t addend)
171 tcg_debug_assert(type == R_MIPS_PC16);
172 tcg_debug_assert(addend == 0);
173 return reloc_pc16(code_ptr, (const tcg_insn_unit *)value);
176 #define TCG_CT_CONST_ZERO 0x100
177 #define TCG_CT_CONST_U16 0x200 /* Unsigned 16-bit: 0 - 0xffff. */
178 #define TCG_CT_CONST_S16 0x400 /* Signed 16-bit: -32768 - 32767 */
179 #define TCG_CT_CONST_P2M1 0x800 /* Power of 2 minus 1. */
180 #define TCG_CT_CONST_N16 0x1000 /* "Negatable" 16-bit: -32767 - 32767 */
181 #define TCG_CT_CONST_WSZ 0x2000 /* word size */
183 #define ALL_GENERAL_REGS 0xffffffffu
185 static bool is_p2m1(tcg_target_long val)
187 return val && ((val + 1) & val) == 0;
190 /* test if a constant matches the constraint */
191 static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
193 if (ct & TCG_CT_CONST) {
195 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
197 } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
199 } else if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
201 } else if ((ct & TCG_CT_CONST_N16) && val >= -32767 && val <= 32767) {
203 } else if ((ct & TCG_CT_CONST_P2M1)
204 && use_mips32r2_instructions && is_p2m1(val)) {
206 } else if ((ct & TCG_CT_CONST_WSZ)
207 && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
213 /* instruction opcodes */
219 OPC_BLEZ = 006 << 26,
220 OPC_BGTZ = 007 << 26,
221 OPC_ADDIU = 011 << 26,
222 OPC_SLTI = 012 << 26,
223 OPC_SLTIU = 013 << 26,
224 OPC_ANDI = 014 << 26,
226 OPC_XORI = 016 << 26,
228 OPC_BNEL = 025 << 26,
229 OPC_BNEZALC_R6 = 030 << 26,
230 OPC_DADDIU = 031 << 26,
251 OPC_SPECIAL = 000 << 26,
252 OPC_SLL = OPC_SPECIAL | 000,
253 OPC_SRL = OPC_SPECIAL | 002,
254 OPC_ROTR = OPC_SPECIAL | 002 | (1 << 21),
255 OPC_SRA = OPC_SPECIAL | 003,
256 OPC_SLLV = OPC_SPECIAL | 004,
257 OPC_SRLV = OPC_SPECIAL | 006,
258 OPC_ROTRV = OPC_SPECIAL | 006 | 0100,
259 OPC_SRAV = OPC_SPECIAL | 007,
260 OPC_JR_R5 = OPC_SPECIAL | 010,
261 OPC_JALR = OPC_SPECIAL | 011,
262 OPC_MOVZ = OPC_SPECIAL | 012,
263 OPC_MOVN = OPC_SPECIAL | 013,
264 OPC_SYNC = OPC_SPECIAL | 017,
265 OPC_MFHI = OPC_SPECIAL | 020,
266 OPC_MFLO = OPC_SPECIAL | 022,
267 OPC_DSLLV = OPC_SPECIAL | 024,
268 OPC_DSRLV = OPC_SPECIAL | 026,
269 OPC_DROTRV = OPC_SPECIAL | 026 | 0100,
270 OPC_DSRAV = OPC_SPECIAL | 027,
271 OPC_MULT = OPC_SPECIAL | 030,
272 OPC_MUL_R6 = OPC_SPECIAL | 030 | 0200,
273 OPC_MUH = OPC_SPECIAL | 030 | 0300,
274 OPC_MULTU = OPC_SPECIAL | 031,
275 OPC_MULU = OPC_SPECIAL | 031 | 0200,
276 OPC_MUHU = OPC_SPECIAL | 031 | 0300,
277 OPC_DIV = OPC_SPECIAL | 032,
278 OPC_DIV_R6 = OPC_SPECIAL | 032 | 0200,
279 OPC_MOD = OPC_SPECIAL | 032 | 0300,
280 OPC_DIVU = OPC_SPECIAL | 033,
281 OPC_DIVU_R6 = OPC_SPECIAL | 033 | 0200,
282 OPC_MODU = OPC_SPECIAL | 033 | 0300,
283 OPC_DMULT = OPC_SPECIAL | 034,
284 OPC_DMUL = OPC_SPECIAL | 034 | 0200,
285 OPC_DMUH = OPC_SPECIAL | 034 | 0300,
286 OPC_DMULTU = OPC_SPECIAL | 035,
287 OPC_DMULU = OPC_SPECIAL | 035 | 0200,
288 OPC_DMUHU = OPC_SPECIAL | 035 | 0300,
289 OPC_DDIV = OPC_SPECIAL | 036,
290 OPC_DDIV_R6 = OPC_SPECIAL | 036 | 0200,
291 OPC_DMOD = OPC_SPECIAL | 036 | 0300,
292 OPC_DDIVU = OPC_SPECIAL | 037,
293 OPC_DDIVU_R6 = OPC_SPECIAL | 037 | 0200,
294 OPC_DMODU = OPC_SPECIAL | 037 | 0300,
295 OPC_ADDU = OPC_SPECIAL | 041,
296 OPC_SUBU = OPC_SPECIAL | 043,
297 OPC_AND = OPC_SPECIAL | 044,
298 OPC_OR = OPC_SPECIAL | 045,
299 OPC_XOR = OPC_SPECIAL | 046,
300 OPC_NOR = OPC_SPECIAL | 047,
301 OPC_SLT = OPC_SPECIAL | 052,
302 OPC_SLTU = OPC_SPECIAL | 053,
303 OPC_DADDU = OPC_SPECIAL | 055,
304 OPC_DSUBU = OPC_SPECIAL | 057,
305 OPC_SELEQZ = OPC_SPECIAL | 065,
306 OPC_SELNEZ = OPC_SPECIAL | 067,
307 OPC_DSLL = OPC_SPECIAL | 070,
308 OPC_DSRL = OPC_SPECIAL | 072,
309 OPC_DROTR = OPC_SPECIAL | 072 | (1 << 21),
310 OPC_DSRA = OPC_SPECIAL | 073,
311 OPC_DSLL32 = OPC_SPECIAL | 074,
312 OPC_DSRL32 = OPC_SPECIAL | 076,
313 OPC_DROTR32 = OPC_SPECIAL | 076 | (1 << 21),
314 OPC_DSRA32 = OPC_SPECIAL | 077,
315 OPC_CLZ_R6 = OPC_SPECIAL | 0120,
316 OPC_DCLZ_R6 = OPC_SPECIAL | 0122,
318 OPC_REGIMM = 001 << 26,
319 OPC_BLTZ = OPC_REGIMM | (000 << 16),
320 OPC_BGEZ = OPC_REGIMM | (001 << 16),
322 OPC_SPECIAL2 = 034 << 26,
323 OPC_MUL_R5 = OPC_SPECIAL2 | 002,
324 OPC_CLZ = OPC_SPECIAL2 | 040,
325 OPC_DCLZ = OPC_SPECIAL2 | 044,
327 OPC_SPECIAL3 = 037 << 26,
328 OPC_EXT = OPC_SPECIAL3 | 000,
329 OPC_DEXTM = OPC_SPECIAL3 | 001,
330 OPC_DEXTU = OPC_SPECIAL3 | 002,
331 OPC_DEXT = OPC_SPECIAL3 | 003,
332 OPC_INS = OPC_SPECIAL3 | 004,
333 OPC_DINSM = OPC_SPECIAL3 | 005,
334 OPC_DINSU = OPC_SPECIAL3 | 006,
335 OPC_DINS = OPC_SPECIAL3 | 007,
336 OPC_WSBH = OPC_SPECIAL3 | 00240,
337 OPC_DSBH = OPC_SPECIAL3 | 00244,
338 OPC_DSHD = OPC_SPECIAL3 | 00544,
339 OPC_SEB = OPC_SPECIAL3 | 02040,
340 OPC_SEH = OPC_SPECIAL3 | 03040,
342 /* MIPS r6 doesn't have JR, JALR should be used instead */
343 OPC_JR = use_mips32r6_instructions ? OPC_JALR : OPC_JR_R5,
346 * MIPS r6 replaces MUL with an alternative encoding which is
347 * backwards-compatible at the assembly level.
349 OPC_MUL = use_mips32r6_instructions ? OPC_MUL_R6 : OPC_MUL_R5,
351 /* MIPS r6 introduced names for weaker variants of SYNC. These are
352 backward compatible to previous architecture revisions. */
353 OPC_SYNC_WMB = OPC_SYNC | 0x04 << 6,
354 OPC_SYNC_MB = OPC_SYNC | 0x10 << 6,
355 OPC_SYNC_ACQUIRE = OPC_SYNC | 0x11 << 6,
356 OPC_SYNC_RELEASE = OPC_SYNC | 0x12 << 6,
357 OPC_SYNC_RMB = OPC_SYNC | 0x13 << 6,
359 /* Aliases for convenience. */
360 ALIAS_PADD = sizeof(void *) == 4 ? OPC_ADDU : OPC_DADDU,
361 ALIAS_PADDI = sizeof(void *) == 4 ? OPC_ADDIU : OPC_DADDIU,
367 static void tcg_out_opc_reg(TCGContext *s, MIPSInsn opc,
368 TCGReg rd, TCGReg rs, TCGReg rt)
373 inst |= (rs & 0x1F) << 21;
374 inst |= (rt & 0x1F) << 16;
375 inst |= (rd & 0x1F) << 11;
382 static void tcg_out_opc_imm(TCGContext *s, MIPSInsn opc,
383 TCGReg rt, TCGReg rs, TCGArg imm)
388 inst |= (rs & 0x1F) << 21;
389 inst |= (rt & 0x1F) << 16;
390 inst |= (imm & 0xffff);
397 static void tcg_out_opc_bf(TCGContext *s, MIPSInsn opc, TCGReg rt,
398 TCGReg rs, int msb, int lsb)
403 inst |= (rs & 0x1F) << 21;
404 inst |= (rt & 0x1F) << 16;
405 inst |= (msb & 0x1F) << 11;
406 inst |= (lsb & 0x1F) << 6;
410 static void tcg_out_opc_bf64(TCGContext *s, MIPSInsn opc, MIPSInsn opm,
411 MIPSInsn oph, TCGReg rt, TCGReg rs,
418 } else if (msb >= 32) {
422 tcg_out_opc_bf(s, opc, rt, rs, msb, lsb);
428 static void tcg_out_opc_br(TCGContext *s, MIPSInsn opc, TCGReg rt, TCGReg rs)
430 tcg_out_opc_imm(s, opc, rt, rs, 0);
436 static void tcg_out_opc_sa(TCGContext *s, MIPSInsn opc,
437 TCGReg rd, TCGReg rt, TCGArg sa)
442 inst |= (rt & 0x1F) << 16;
443 inst |= (rd & 0x1F) << 11;
444 inst |= (sa & 0x1F) << 6;
449 static void tcg_out_opc_sa64(TCGContext *s, MIPSInsn opc1, MIPSInsn opc2,
450 TCGReg rd, TCGReg rt, TCGArg sa)
454 inst = (sa & 32 ? opc2 : opc1);
455 inst |= (rt & 0x1F) << 16;
456 inst |= (rd & 0x1F) << 11;
457 inst |= (sa & 0x1F) << 6;
463 * Returns true if the branch was in range and the insn was emitted.
465 static bool tcg_out_opc_jmp(TCGContext *s, MIPSInsn opc, const void *target)
467 uintptr_t dest = (uintptr_t)target;
468 uintptr_t from = (uintptr_t)tcg_splitwx_to_rx(s->code_ptr) + 4;
471 /* The pc-region branch happens within the 256MB region of
472 the delay slot (thus the +4). */
473 if ((from ^ dest) & -(1 << 28)) {
476 tcg_debug_assert((dest & 3) == 0);
479 inst |= (dest >> 2) & 0x3ffffff;
484 static void tcg_out_nop(TCGContext *s)
489 static void tcg_out_dsll(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
491 tcg_out_opc_sa64(s, OPC_DSLL, OPC_DSLL32, rd, rt, sa);
494 static void tcg_out_dsrl(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
496 tcg_out_opc_sa64(s, OPC_DSRL, OPC_DSRL32, rd, rt, sa);
499 static void tcg_out_dsra(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
501 tcg_out_opc_sa64(s, OPC_DSRA, OPC_DSRA32, rd, rt, sa);
504 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
506 /* Simple reg-reg move, optimising out the 'do nothing' case */
508 tcg_out_opc_reg(s, OPC_OR, ret, arg, TCG_REG_ZERO);
513 static bool tcg_out_movi_one(TCGContext *s, TCGReg ret, tcg_target_long arg)
515 if (arg == (int16_t)arg) {
516 tcg_out_opc_imm(s, OPC_ADDIU, ret, TCG_REG_ZERO, arg);
519 if (arg == (uint16_t)arg) {
520 tcg_out_opc_imm(s, OPC_ORI, ret, TCG_REG_ZERO, arg);
523 if (arg == (int32_t)arg && (arg & 0xffff) == 0) {
524 tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
530 static void tcg_out_movi(TCGContext *s, TCGType type,
531 TCGReg ret, tcg_target_long arg)
533 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
537 if (tcg_out_movi_one(s, ret, arg)) {
541 if (TCG_TARGET_REG_BITS == 32 || arg == (int32_t)arg) {
542 tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
544 tcg_out_movi(s, TCG_TYPE_I32, ret, arg >> 31 >> 1);
545 if (arg & 0xffff0000ull) {
546 tcg_out_dsll(s, ret, ret, 16);
547 tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg >> 16);
548 tcg_out_dsll(s, ret, ret, 16);
550 tcg_out_dsll(s, ret, ret, 32);
554 tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg & 0xffff);
558 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
560 tcg_debug_assert(TCG_TARGET_HAS_ext8s_i32);
561 tcg_out_opc_reg(s, OPC_SEB, rd, TCG_REG_ZERO, rs);
564 static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs)
566 tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xff);
569 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
571 tcg_debug_assert(TCG_TARGET_HAS_ext16s_i32);
572 tcg_out_opc_reg(s, OPC_SEH, rd, TCG_REG_ZERO, rs);
575 static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs)
577 tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xffff);
580 static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs)
582 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
583 tcg_out_opc_sa(s, OPC_SLL, rd, rs, 0);
586 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
589 tcg_out_ext32s(s, rd, rs);
593 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
595 tcg_out_ext32u(s, rd, rs);
598 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs)
600 tcg_out_ext32s(s, rd, rs);
603 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
608 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
611 /* This function is only used for passing structs by reference. */
612 g_assert_not_reached();
615 static void tcg_out_bswap16(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
617 /* ret and arg can't be register tmp0 */
618 tcg_debug_assert(ret != TCG_TMP0);
619 tcg_debug_assert(arg != TCG_TMP0);
621 /* With arg = abcd: */
622 if (use_mips32r2_instructions) {
623 tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg); /* badc */
624 if (flags & TCG_BSWAP_OS) {
625 tcg_out_opc_reg(s, OPC_SEH, ret, 0, ret); /* ssdc */
626 } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
627 tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xffff); /* 00dc */
632 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, arg, 8); /* 0abc */
633 if (!(flags & TCG_BSWAP_IZ)) {
634 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, TCG_TMP0, 0x00ff); /* 000c */
636 if (flags & TCG_BSWAP_OS) {
637 tcg_out_opc_sa(s, OPC_SLL, ret, arg, 24); /* d000 */
638 tcg_out_opc_sa(s, OPC_SRA, ret, ret, 16); /* ssd0 */
640 tcg_out_opc_sa(s, OPC_SLL, ret, arg, 8); /* bcd0 */
641 if (flags & TCG_BSWAP_OZ) {
642 tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xff00); /* 00d0 */
645 tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP0); /* ssdc */
648 static void tcg_out_bswap_subr(TCGContext *s, const tcg_insn_unit *sub)
650 if (!tcg_out_opc_jmp(s, OPC_JAL, sub)) {
651 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP1, (uintptr_t)sub);
652 tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_TMP1, 0);
656 static void tcg_out_bswap32(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
658 if (use_mips32r2_instructions) {
659 tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg);
660 tcg_out_opc_sa(s, OPC_ROTR, ret, ret, 16);
661 if (flags & TCG_BSWAP_OZ) {
662 tcg_out_opc_bf(s, OPC_DEXT, ret, ret, 31, 0);
665 if (flags & TCG_BSWAP_OZ) {
666 tcg_out_bswap_subr(s, bswap32u_addr);
668 tcg_out_bswap_subr(s, bswap32_addr);
670 /* delay slot -- never omit the insn, like tcg_out_mov might. */
671 tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
672 tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
676 static void tcg_out_bswap64(TCGContext *s, TCGReg ret, TCGReg arg)
678 if (use_mips32r2_instructions) {
679 tcg_out_opc_reg(s, OPC_DSBH, ret, 0, arg);
680 tcg_out_opc_reg(s, OPC_DSHD, ret, 0, ret);
682 tcg_out_bswap_subr(s, bswap64_addr);
683 /* delay slot -- never omit the insn, like tcg_out_mov might. */
684 tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
685 tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
689 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
691 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
692 if (use_mips32r2_instructions) {
693 tcg_out_opc_bf(s, OPC_DEXT, ret, arg, 31, 0);
695 tcg_out_dsll(s, ret, arg, 32);
696 tcg_out_dsrl(s, ret, ret, 32);
700 static void tcg_out_ldst(TCGContext *s, MIPSInsn opc, TCGReg data,
701 TCGReg addr, intptr_t ofs)
705 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - lo);
706 if (addr != TCG_REG_ZERO) {
707 tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP0, TCG_TMP0, addr);
711 tcg_out_opc_imm(s, opc, data, addr, lo);
714 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
715 TCGReg arg1, intptr_t arg2)
717 MIPSInsn opc = OPC_LD;
718 if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
721 tcg_out_ldst(s, opc, arg, arg1, arg2);
724 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
725 TCGReg arg1, intptr_t arg2)
727 MIPSInsn opc = OPC_SD;
728 if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
731 tcg_out_ldst(s, opc, arg, arg1, arg2);
734 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
735 TCGReg base, intptr_t ofs)
738 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
744 static void tcg_out_addsub2(TCGContext *s, TCGReg rl, TCGReg rh, TCGReg al,
745 TCGReg ah, TCGArg bl, TCGArg bh, bool cbl,
746 bool cbh, bool is_sub)
748 TCGReg th = TCG_TMP1;
750 /* If we have a negative constant such that negating it would
751 make the high part zero, we can (usually) eliminate one insn. */
752 if (cbl && cbh && bh == -1 && bl != 0) {
758 /* By operating on the high part first, we get to use the final
759 carry operation to move back from the temporary. */
761 tcg_out_opc_reg(s, (is_sub ? OPC_SUBU : OPC_ADDU), th, ah, bh);
762 } else if (bh != 0 || ah == rl) {
763 tcg_out_opc_imm(s, OPC_ADDIU, th, ah, (is_sub ? -bh : bh));
768 /* Note that tcg optimization should eliminate the bl == 0 case. */
771 tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, al, bl);
772 tcg_out_opc_imm(s, OPC_ADDIU, rl, al, -bl);
774 tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, al, bl);
775 tcg_out_opc_reg(s, OPC_SUBU, rl, al, bl);
777 tcg_out_opc_reg(s, OPC_SUBU, rh, th, TCG_TMP0);
780 tcg_out_opc_imm(s, OPC_ADDIU, rl, al, bl);
781 tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, rl, bl);
782 } else if (rl == al && rl == bl) {
783 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, al, TCG_TARGET_REG_BITS - 1);
784 tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
786 tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
787 tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, rl, (rl == bl ? al : bl));
789 tcg_out_opc_reg(s, OPC_ADDU, rh, th, TCG_TMP0);
793 /* Bit 0 set if inversion required; bit 1 set if swapping required. */
794 #define MIPS_CMP_INV 1
795 #define MIPS_CMP_SWAP 2
797 static const uint8_t mips_cmp_map[16] = {
800 [TCG_COND_GE] = MIPS_CMP_INV,
801 [TCG_COND_GEU] = MIPS_CMP_INV,
802 [TCG_COND_LE] = MIPS_CMP_INV | MIPS_CMP_SWAP,
803 [TCG_COND_LEU] = MIPS_CMP_INV | MIPS_CMP_SWAP,
804 [TCG_COND_GT] = MIPS_CMP_SWAP,
805 [TCG_COND_GTU] = MIPS_CMP_SWAP,
808 static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
809 TCGReg arg1, TCGReg arg2)
811 MIPSInsn s_opc = OPC_SLTU;
817 tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
820 tcg_out_opc_imm(s, OPC_SLTIU, ret, arg1, 1);
825 tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
828 tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, arg1);
842 cmp_map = mips_cmp_map[cond];
843 if (cmp_map & MIPS_CMP_SWAP) {
848 tcg_out_opc_reg(s, s_opc, ret, arg1, arg2);
849 if (cmp_map & MIPS_CMP_INV) {
850 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
855 g_assert_not_reached();
860 static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
861 TCGReg arg2, TCGLabel *l)
863 static const MIPSInsn b_zero[16] = {
864 [TCG_COND_LT] = OPC_BLTZ,
865 [TCG_COND_GT] = OPC_BGTZ,
866 [TCG_COND_LE] = OPC_BLEZ,
867 [TCG_COND_GE] = OPC_BGEZ,
870 MIPSInsn s_opc = OPC_SLTU;
887 b_opc = b_zero[cond];
899 cmp_map = mips_cmp_map[cond];
900 if (cmp_map & MIPS_CMP_SWAP) {
905 tcg_out_opc_reg(s, s_opc, TCG_TMP0, arg1, arg2);
906 b_opc = (cmp_map & MIPS_CMP_INV ? OPC_BEQ : OPC_BNE);
912 g_assert_not_reached();
916 tcg_out_opc_br(s, b_opc, arg1, arg2);
917 tcg_out_reloc(s, s->code_ptr - 1, R_MIPS_PC16, l, 0);
921 static TCGReg tcg_out_reduce_eq2(TCGContext *s, TCGReg tmp0, TCGReg tmp1,
922 TCGReg al, TCGReg ah,
923 TCGReg bl, TCGReg bh)
925 /* Merge highpart comparison into AH. */
928 tcg_out_opc_reg(s, OPC_XOR, tmp0, ah, bh);
934 /* Merge lowpart comparison into AL. */
937 tcg_out_opc_reg(s, OPC_XOR, tmp1, al, bl);
943 /* Merge high and low part comparisons into AL. */
946 tcg_out_opc_reg(s, OPC_OR, tmp0, ah, al);
955 static void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
956 TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
958 TCGReg tmp0 = TCG_TMP0;
961 tcg_debug_assert(ret != TCG_TMP0);
962 if (ret == ah || ret == bh) {
963 tcg_debug_assert(ret != TCG_TMP1);
970 tmp1 = tcg_out_reduce_eq2(s, tmp0, tmp1, al, ah, bl, bh);
971 tcg_out_setcond(s, cond, ret, tmp1, TCG_REG_ZERO);
975 tcg_out_setcond(s, TCG_COND_EQ, tmp0, ah, bh);
976 tcg_out_setcond(s, tcg_unsigned_cond(cond), tmp1, al, bl);
977 tcg_out_opc_reg(s, OPC_AND, tmp1, tmp1, tmp0);
978 tcg_out_setcond(s, tcg_high_cond(cond), tmp0, ah, bh);
979 tcg_out_opc_reg(s, OPC_OR, ret, tmp1, tmp0);
984 static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
985 TCGReg bl, TCGReg bh, TCGLabel *l)
987 TCGCond b_cond = TCG_COND_NE;
988 TCGReg tmp = TCG_TMP1;
990 /* With branches, we emit between 4 and 9 insns with 2 or 3 branches.
991 With setcond, we emit between 3 and 10 insns and only 1 branch,
992 which ought to get better branch prediction. */
997 tmp = tcg_out_reduce_eq2(s, TCG_TMP0, TCG_TMP1, al, ah, bl, bh);
1001 /* Minimize code size by preferring a compare not requiring INV. */
1002 if (mips_cmp_map[cond] & MIPS_CMP_INV) {
1003 cond = tcg_invert_cond(cond);
1004 b_cond = TCG_COND_EQ;
1006 tcg_out_setcond2(s, cond, tmp, al, ah, bl, bh);
1010 tcg_out_brcond(s, b_cond, tmp, TCG_REG_ZERO, l);
1013 static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
1014 TCGReg c1, TCGReg c2, TCGReg v1, TCGReg v2)
1018 /* If one of the values is zero, put it last to match SEL*Z instructions */
1019 if (use_mips32r6_instructions && v1 == 0) {
1022 cond = tcg_invert_cond(cond);
1031 tcg_out_opc_reg(s, OPC_XOR, TCG_TMP0, c1, c2);
1037 /* Minimize code size by preferring a compare not requiring INV. */
1038 if (mips_cmp_map[cond] & MIPS_CMP_INV) {
1039 cond = tcg_invert_cond(cond);
1042 tcg_out_setcond(s, cond, TCG_TMP0, c1, c2);
1047 if (use_mips32r6_instructions) {
1048 MIPSInsn m_opc_t = eqz ? OPC_SELEQZ : OPC_SELNEZ;
1049 MIPSInsn m_opc_f = eqz ? OPC_SELNEZ : OPC_SELEQZ;
1052 tcg_out_opc_reg(s, m_opc_f, TCG_TMP1, v2, c1);
1054 tcg_out_opc_reg(s, m_opc_t, ret, v1, c1);
1056 tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP1);
1059 MIPSInsn m_opc = eqz ? OPC_MOVZ : OPC_MOVN;
1061 tcg_out_opc_reg(s, m_opc, ret, v1, c1);
1063 /* This should be guaranteed via constraints */
1064 tcg_debug_assert(v2 == ret);
1068 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
1070 /* Note that the ABI requires the called function's address to be
1071 loaded into T9, even if a direct branch is in range. */
1072 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T9, (uintptr_t)arg);
1074 /* But do try a direct branch, allowing the cpu better insn prefetch. */
1076 if (!tcg_out_opc_jmp(s, OPC_J, arg)) {
1077 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_T9, 0);
1080 if (!tcg_out_opc_jmp(s, OPC_JAL, arg)) {
1081 tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_REG_T9, 0);
1086 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
1087 const TCGHelperInfo *info)
1089 tcg_out_call_int(s, arg, false);
1093 /* We have four temps, we might as well expose three of them. */
1094 static const TCGLdstHelperParam ldst_helper_param = {
1095 .ntmp = 3, .tmp = { TCG_TMP0, TCG_TMP1, TCG_TMP2 }
1098 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1100 const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
1101 MemOp opc = get_memop(l->oi);
1103 /* resolve label address */
1104 if (!reloc_pc16(l->label_ptr[0], tgt_rx)
1105 || (l->label_ptr[1] && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
1109 tcg_out_ld_helper_args(s, l, &ldst_helper_param);
1111 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false);
1115 tcg_out_ld_helper_ret(s, l, true, &ldst_helper_param);
1117 tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1118 if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1127 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1129 const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
1130 MemOp opc = get_memop(l->oi);
1132 /* resolve label address */
1133 if (!reloc_pc16(l->label_ptr[0], tgt_rx)
1134 || (l->label_ptr[1] && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
1138 tcg_out_st_helper_args(s, l, &ldst_helper_param);
1140 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
1144 tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1145 if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1159 bool tcg_target_has_memory_bswap(MemOp memop)
1165 * For softmmu, perform the TLB load and compare.
1166 * For useronly, perform any required alignment tests.
1167 * In both cases, return a TCGLabelQemuLdst structure if the slow path
1168 * is required and fill in @h with the host address for the fast path.
1170 static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1171 TCGReg addrlo, TCGReg addrhi,
1172 MemOpIdx oi, bool is_ld)
1174 TCGType addr_type = s->addr_type;
1175 TCGLabelQemuLdst *ldst = NULL;
1176 MemOp opc = get_memop(oi);
1178 unsigned s_bits = opc & MO_SIZE;
1182 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
1183 a_bits = h->aa.align;
1184 a_mask = (1 << a_bits) - 1;
1186 #ifdef CONFIG_SOFTMMU
1187 unsigned s_mask = (1 << s_bits) - 1;
1188 int mem_index = get_mmuidx(oi);
1189 int fast_off = TLB_MASK_TABLE_OFS(mem_index);
1190 int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1191 int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1192 int add_off = offsetof(CPUTLBEntry, addend);
1193 int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read)
1194 : offsetof(CPUTLBEntry, addr_write);
1196 ldst = new_ldst_label(s);
1197 ldst->is_ld = is_ld;
1199 ldst->addrlo_reg = addrlo;
1200 ldst->addrhi_reg = addrhi;
1202 /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx]. */
1203 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
1204 QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -32768);
1205 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_AREG0, mask_off);
1206 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP1, TCG_AREG0, table_off);
1208 /* Extract the TLB index from the address into TMP3. */
1209 if (TCG_TARGET_REG_BITS == 32 || addr_type == TCG_TYPE_I32) {
1210 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, addrlo,
1211 s->page_bits - CPU_TLB_ENTRY_BITS);
1213 tcg_out_dsrl(s, TCG_TMP3, addrlo,
1214 s->page_bits - CPU_TLB_ENTRY_BITS);
1216 tcg_out_opc_reg(s, OPC_AND, TCG_TMP3, TCG_TMP3, TCG_TMP0);
1218 /* Add the tlb_table pointer, creating the CPUTLBEntry address in TMP3. */
1219 tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP3, TCG_TMP3, TCG_TMP1);
1221 if (TCG_TARGET_REG_BITS == 64 || addr_type == TCG_TYPE_I32) {
1222 /* Load the tlb comparator. */
1223 tcg_out_ld(s, addr_type, TCG_TMP0, TCG_TMP3, cmp_off);
1224 /* Load the tlb addend for the fast path. */
1225 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off);
1227 /* Load the low half of the tlb comparator. */
1228 tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + LO_OFF);
1232 * Mask the page bits, keeping the alignment bits to compare against.
1233 * For unaligned accesses, compare against the end of the access to
1234 * verify that it does not cross a page boundary.
1236 tcg_out_movi(s, addr_type, TCG_TMP1, s->page_mask | a_mask);
1237 if (a_mask < s_mask) {
1238 if (TCG_TARGET_REG_BITS == 32 || addr_type == TCG_TYPE_I32) {
1239 tcg_out_opc_imm(s, OPC_ADDIU, TCG_TMP2, addrlo, s_mask - a_mask);
1241 tcg_out_opc_imm(s, OPC_DADDIU, TCG_TMP2, addrlo, s_mask - a_mask);
1243 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP2);
1245 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, addrlo);
1248 /* Zero extend a 32-bit guest address for a 64-bit host. */
1249 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) {
1250 tcg_out_ext32u(s, TCG_TMP2, addrlo);
1254 ldst->label_ptr[0] = s->code_ptr;
1255 tcg_out_opc_br(s, OPC_BNE, TCG_TMP1, TCG_TMP0);
1257 /* Load and test the high half tlb comparator. */
1258 if (TCG_TARGET_REG_BITS == 32 && addr_type != TCG_TYPE_I32) {
1260 tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + HI_OFF);
1262 /* Load the tlb addend for the fast path. */
1263 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off);
1265 ldst->label_ptr[1] = s->code_ptr;
1266 tcg_out_opc_br(s, OPC_BNE, addrhi, TCG_TMP0);
1271 tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_TMP3, addrlo);
1273 if (a_mask && (use_mips32r6_instructions || a_bits != s_bits)) {
1274 ldst = new_ldst_label(s);
1276 ldst->is_ld = is_ld;
1278 ldst->addrlo_reg = addrlo;
1279 ldst->addrhi_reg = addrhi;
1281 /* We are expecting a_bits to max out at 7, much lower than ANDI. */
1282 tcg_debug_assert(a_bits < 16);
1283 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, addrlo, a_mask);
1285 ldst->label_ptr[0] = s->code_ptr;
1286 if (use_mips32r6_instructions) {
1287 tcg_out_opc_br(s, OPC_BNEZALC_R6, TCG_REG_ZERO, TCG_TMP0);
1289 tcg_out_opc_br(s, OPC_BNEL, TCG_TMP0, TCG_REG_ZERO);
1295 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) {
1296 tcg_out_ext32u(s, TCG_REG_A0, base);
1300 if (guest_base == (int16_t)guest_base) {
1301 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_A0, base, guest_base);
1303 tcg_out_opc_reg(s, ALIAS_PADD, TCG_REG_A0, base,
1304 TCG_GUEST_BASE_REG);
1314 static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1315 TCGReg base, MemOp opc, TCGType type)
1317 switch (opc & MO_SSIZE) {
1319 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1322 tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1325 tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1328 tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1331 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) {
1332 tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1337 tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1340 /* Prefer to load from offset 0 first, but allow for overlap. */
1341 if (TCG_TARGET_REG_BITS == 64) {
1342 tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1343 } else if (MIPS_BE ? hi != base : lo == base) {
1344 tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1345 tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1347 tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1348 tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1352 g_assert_not_reached();
1356 static void tcg_out_qemu_ld_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
1357 TCGReg base, MemOp opc, TCGType type)
1359 const MIPSInsn lw1 = MIPS_BE ? OPC_LWL : OPC_LWR;
1360 const MIPSInsn lw2 = MIPS_BE ? OPC_LWR : OPC_LWL;
1361 const MIPSInsn ld1 = MIPS_BE ? OPC_LDL : OPC_LDR;
1362 const MIPSInsn ld2 = MIPS_BE ? OPC_LDR : OPC_LDL;
1363 bool sgn = opc & MO_SIGN;
1365 switch (opc & MO_SIZE) {
1367 if (HOST_BIG_ENDIAN) {
1368 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 0);
1369 tcg_out_opc_imm(s, OPC_LBU, lo, base, 1);
1370 if (use_mips32r2_instructions) {
1371 tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
1373 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP0, TCG_TMP0, 8);
1374 tcg_out_opc_reg(s, OPC_OR, lo, lo, TCG_TMP0);
1376 } else if (use_mips32r2_instructions && lo != base) {
1377 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1378 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 1);
1379 tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
1381 tcg_out_opc_imm(s, OPC_LBU, TCG_TMP0, base, 0);
1382 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP1, base, 1);
1383 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP1, TCG_TMP1, 8);
1384 tcg_out_opc_reg(s, OPC_OR, lo, TCG_TMP0, TCG_TMP1);
1389 tcg_out_opc_imm(s, lw1, lo, base, 0);
1390 tcg_out_opc_imm(s, lw2, lo, base, 3);
1391 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64 && !sgn) {
1392 tcg_out_ext32u(s, lo, lo);
1397 if (TCG_TARGET_REG_BITS == 64) {
1398 tcg_out_opc_imm(s, ld1, lo, base, 0);
1399 tcg_out_opc_imm(s, ld2, lo, base, 7);
1401 tcg_out_opc_imm(s, lw1, MIPS_BE ? hi : lo, base, 0 + 0);
1402 tcg_out_opc_imm(s, lw2, MIPS_BE ? hi : lo, base, 0 + 3);
1403 tcg_out_opc_imm(s, lw1, MIPS_BE ? lo : hi, base, 4 + 0);
1404 tcg_out_opc_imm(s, lw2, MIPS_BE ? lo : hi, base, 4 + 3);
1409 g_assert_not_reached();
1413 static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi,
1414 TCGReg addrlo, TCGReg addrhi,
1415 MemOpIdx oi, TCGType data_type)
1417 MemOp opc = get_memop(oi);
1418 TCGLabelQemuLdst *ldst;
1421 ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, true);
1423 if (use_mips32r6_instructions || h.aa.align >= (opc & MO_SIZE)) {
1424 tcg_out_qemu_ld_direct(s, datalo, datahi, h.base, opc, data_type);
1426 tcg_out_qemu_ld_unalign(s, datalo, datahi, h.base, opc, data_type);
1430 ldst->type = data_type;
1431 ldst->datalo_reg = datalo;
1432 ldst->datahi_reg = datahi;
1433 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1437 static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1438 TCGReg base, MemOp opc)
1440 switch (opc & MO_SIZE) {
1442 tcg_out_opc_imm(s, OPC_SB, lo, base, 0);
1445 tcg_out_opc_imm(s, OPC_SH, lo, base, 0);
1448 tcg_out_opc_imm(s, OPC_SW, lo, base, 0);
1451 if (TCG_TARGET_REG_BITS == 64) {
1452 tcg_out_opc_imm(s, OPC_SD, lo, base, 0);
1454 tcg_out_opc_imm(s, OPC_SW, MIPS_BE ? hi : lo, base, 0);
1455 tcg_out_opc_imm(s, OPC_SW, MIPS_BE ? lo : hi, base, 4);
1459 g_assert_not_reached();
1463 static void tcg_out_qemu_st_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
1464 TCGReg base, MemOp opc)
1466 const MIPSInsn sw1 = MIPS_BE ? OPC_SWL : OPC_SWR;
1467 const MIPSInsn sw2 = MIPS_BE ? OPC_SWR : OPC_SWL;
1468 const MIPSInsn sd1 = MIPS_BE ? OPC_SDL : OPC_SDR;
1469 const MIPSInsn sd2 = MIPS_BE ? OPC_SDR : OPC_SDL;
1471 switch (opc & MO_SIZE) {
1473 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, lo, 8);
1474 tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? TCG_TMP0 : lo, base, 0);
1475 tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? lo : TCG_TMP0, base, 1);
1479 tcg_out_opc_imm(s, sw1, lo, base, 0);
1480 tcg_out_opc_imm(s, sw2, lo, base, 3);
1484 if (TCG_TARGET_REG_BITS == 64) {
1485 tcg_out_opc_imm(s, sd1, lo, base, 0);
1486 tcg_out_opc_imm(s, sd2, lo, base, 7);
1488 tcg_out_opc_imm(s, sw1, MIPS_BE ? hi : lo, base, 0 + 0);
1489 tcg_out_opc_imm(s, sw2, MIPS_BE ? hi : lo, base, 0 + 3);
1490 tcg_out_opc_imm(s, sw1, MIPS_BE ? lo : hi, base, 4 + 0);
1491 tcg_out_opc_imm(s, sw2, MIPS_BE ? lo : hi, base, 4 + 3);
1496 g_assert_not_reached();
1500 static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi,
1501 TCGReg addrlo, TCGReg addrhi,
1502 MemOpIdx oi, TCGType data_type)
1504 MemOp opc = get_memop(oi);
1505 TCGLabelQemuLdst *ldst;
1508 ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, false);
1510 if (use_mips32r6_instructions || h.aa.align >= (opc & MO_SIZE)) {
1511 tcg_out_qemu_st_direct(s, datalo, datahi, h.base, opc);
1513 tcg_out_qemu_st_unalign(s, datalo, datahi, h.base, opc);
1517 ldst->type = data_type;
1518 ldst->datalo_reg = datalo;
1519 ldst->datahi_reg = datahi;
1520 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1524 static void tcg_out_mb(TCGContext *s, TCGArg a0)
1526 static const MIPSInsn sync[] = {
1527 /* Note that SYNC_MB is a slightly weaker than SYNC 0,
1528 as the former is an ordering barrier and the latter
1529 is a completion barrier. */
1530 [0 ... TCG_MO_ALL] = OPC_SYNC_MB,
1531 [TCG_MO_LD_LD] = OPC_SYNC_RMB,
1532 [TCG_MO_ST_ST] = OPC_SYNC_WMB,
1533 [TCG_MO_LD_ST] = OPC_SYNC_RELEASE,
1534 [TCG_MO_LD_ST | TCG_MO_ST_ST] = OPC_SYNC_RELEASE,
1535 [TCG_MO_LD_ST | TCG_MO_LD_LD] = OPC_SYNC_ACQUIRE,
1537 tcg_out32(s, sync[a0 & TCG_MO_ALL]);
1540 static void tcg_out_clz(TCGContext *s, MIPSInsn opcv2, MIPSInsn opcv6,
1541 int width, TCGReg a0, TCGReg a1, TCGArg a2)
1543 if (use_mips32r6_instructions) {
1545 tcg_out_opc_reg(s, opcv6, a0, a1, 0);
1547 tcg_out_opc_reg(s, opcv6, TCG_TMP0, a1, 0);
1548 tcg_out_movcond(s, TCG_COND_EQ, a0, a1, 0, a2, TCG_TMP0);
1552 tcg_out_opc_reg(s, opcv2, a0, a1, a1);
1553 } else if (a0 == a2) {
1554 tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
1555 tcg_out_opc_reg(s, OPC_MOVN, a0, TCG_TMP0, a1);
1556 } else if (a0 != a1) {
1557 tcg_out_opc_reg(s, opcv2, a0, a1, a1);
1558 tcg_out_opc_reg(s, OPC_MOVZ, a0, a2, a1);
1560 tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
1561 tcg_out_opc_reg(s, OPC_MOVZ, TCG_TMP0, a2, a1);
1562 tcg_out_mov(s, TCG_TYPE_REG, a0, TCG_TMP0);
1567 static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1569 TCGReg base = TCG_REG_ZERO;
1574 if (TCG_TARGET_REG_BITS == 64) {
1575 ofs = tcg_tbrel_diff(s, (void *)a0);
1581 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo);
1582 tcg_out_opc_reg(s, ALIAS_PADD, base, base, TCG_REG_TB);
1588 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo);
1591 if (!tcg_out_opc_jmp(s, OPC_J, tb_ret_addr)) {
1592 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)tb_ret_addr);
1593 tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0);
1596 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_V0, base, lo);
1599 static void tcg_out_goto_tb(TCGContext *s, int which)
1601 intptr_t ofs = get_jmp_target_addr(s, which);
1604 /* indirect jump method */
1605 if (TCG_TARGET_REG_BITS == 64) {
1608 ofs = tcg_tbrel_diff(s, (void *)ofs);
1611 base = TCG_REG_ZERO;
1613 tcg_out_ld(s, TCG_TYPE_PTR, dest, base, ofs);
1614 tcg_out_opc_reg(s, OPC_JR, 0, dest, 0);
1618 set_jmp_reset_offset(s, which);
1619 if (TCG_TARGET_REG_BITS == 64) {
1620 /* For the unlinked case, need to reset TCG_REG_TB. */
1621 tcg_out_ldst(s, ALIAS_PADDI, TCG_REG_TB, TCG_REG_TB,
1622 -tcg_current_code_size(s));
1626 void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1627 uintptr_t jmp_rx, uintptr_t jmp_rw)
1629 /* Always indirect, nothing to do */
1632 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1633 const TCGArg args[TCG_MAX_OP_ARGS],
1634 const int const_args[TCG_MAX_OP_ARGS])
1641 * Note that many operands use the constraint set "rZ".
1642 * We make use of the fact that 0 is the ZERO register,
1643 * and hence such cases need not check for const_args.
1651 case INDEX_op_goto_ptr:
1652 /* jmp to the given host address (could be epilogue) */
1653 tcg_out_opc_reg(s, OPC_JR, 0, a0, 0);
1654 if (TCG_TARGET_REG_BITS == 64) {
1655 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, a0);
1661 tcg_out_brcond(s, TCG_COND_EQ, TCG_REG_ZERO, TCG_REG_ZERO,
1665 case INDEX_op_ld8u_i32:
1666 case INDEX_op_ld8u_i64:
1669 case INDEX_op_ld8s_i32:
1670 case INDEX_op_ld8s_i64:
1673 case INDEX_op_ld16u_i32:
1674 case INDEX_op_ld16u_i64:
1677 case INDEX_op_ld16s_i32:
1678 case INDEX_op_ld16s_i64:
1681 case INDEX_op_ld_i32:
1682 case INDEX_op_ld32s_i64:
1685 case INDEX_op_ld32u_i64:
1688 case INDEX_op_ld_i64:
1691 case INDEX_op_st8_i32:
1692 case INDEX_op_st8_i64:
1695 case INDEX_op_st16_i32:
1696 case INDEX_op_st16_i64:
1699 case INDEX_op_st_i32:
1700 case INDEX_op_st32_i64:
1703 case INDEX_op_st_i64:
1706 tcg_out_ldst(s, i1, a0, a1, a2);
1709 case INDEX_op_add_i32:
1710 i1 = OPC_ADDU, i2 = OPC_ADDIU;
1712 case INDEX_op_add_i64:
1713 i1 = OPC_DADDU, i2 = OPC_DADDIU;
1715 case INDEX_op_or_i32:
1716 case INDEX_op_or_i64:
1717 i1 = OPC_OR, i2 = OPC_ORI;
1719 case INDEX_op_xor_i32:
1720 case INDEX_op_xor_i64:
1721 i1 = OPC_XOR, i2 = OPC_XORI;
1724 tcg_out_opc_imm(s, i2, a0, a1, a2);
1728 tcg_out_opc_reg(s, i1, a0, a1, a2);
1731 case INDEX_op_sub_i32:
1732 i1 = OPC_SUBU, i2 = OPC_ADDIU;
1734 case INDEX_op_sub_i64:
1735 i1 = OPC_DSUBU, i2 = OPC_DADDIU;
1738 tcg_out_opc_imm(s, i2, a0, a1, -a2);
1742 case INDEX_op_and_i32:
1743 if (c2 && a2 != (uint16_t)a2) {
1744 int msb = ctz32(~a2) - 1;
1745 tcg_debug_assert(use_mips32r2_instructions);
1746 tcg_debug_assert(is_p2m1(a2));
1747 tcg_out_opc_bf(s, OPC_EXT, a0, a1, msb, 0);
1750 i1 = OPC_AND, i2 = OPC_ANDI;
1752 case INDEX_op_and_i64:
1753 if (c2 && a2 != (uint16_t)a2) {
1754 int msb = ctz64(~a2) - 1;
1755 tcg_debug_assert(use_mips32r2_instructions);
1756 tcg_debug_assert(is_p2m1(a2));
1757 tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1, msb, 0);
1760 i1 = OPC_AND, i2 = OPC_ANDI;
1762 case INDEX_op_nor_i32:
1763 case INDEX_op_nor_i64:
1767 case INDEX_op_mul_i32:
1768 if (use_mips32_instructions) {
1769 tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1772 i1 = OPC_MULT, i2 = OPC_MFLO;
1774 case INDEX_op_mulsh_i32:
1775 if (use_mips32r6_instructions) {
1776 tcg_out_opc_reg(s, OPC_MUH, a0, a1, a2);
1779 i1 = OPC_MULT, i2 = OPC_MFHI;
1781 case INDEX_op_muluh_i32:
1782 if (use_mips32r6_instructions) {
1783 tcg_out_opc_reg(s, OPC_MUHU, a0, a1, a2);
1786 i1 = OPC_MULTU, i2 = OPC_MFHI;
1788 case INDEX_op_div_i32:
1789 if (use_mips32r6_instructions) {
1790 tcg_out_opc_reg(s, OPC_DIV_R6, a0, a1, a2);
1793 i1 = OPC_DIV, i2 = OPC_MFLO;
1795 case INDEX_op_divu_i32:
1796 if (use_mips32r6_instructions) {
1797 tcg_out_opc_reg(s, OPC_DIVU_R6, a0, a1, a2);
1800 i1 = OPC_DIVU, i2 = OPC_MFLO;
1802 case INDEX_op_rem_i32:
1803 if (use_mips32r6_instructions) {
1804 tcg_out_opc_reg(s, OPC_MOD, a0, a1, a2);
1807 i1 = OPC_DIV, i2 = OPC_MFHI;
1809 case INDEX_op_remu_i32:
1810 if (use_mips32r6_instructions) {
1811 tcg_out_opc_reg(s, OPC_MODU, a0, a1, a2);
1814 i1 = OPC_DIVU, i2 = OPC_MFHI;
1816 case INDEX_op_mul_i64:
1817 if (use_mips32r6_instructions) {
1818 tcg_out_opc_reg(s, OPC_DMUL, a0, a1, a2);
1821 i1 = OPC_DMULT, i2 = OPC_MFLO;
1823 case INDEX_op_mulsh_i64:
1824 if (use_mips32r6_instructions) {
1825 tcg_out_opc_reg(s, OPC_DMUH, a0, a1, a2);
1828 i1 = OPC_DMULT, i2 = OPC_MFHI;
1830 case INDEX_op_muluh_i64:
1831 if (use_mips32r6_instructions) {
1832 tcg_out_opc_reg(s, OPC_DMUHU, a0, a1, a2);
1835 i1 = OPC_DMULTU, i2 = OPC_MFHI;
1837 case INDEX_op_div_i64:
1838 if (use_mips32r6_instructions) {
1839 tcg_out_opc_reg(s, OPC_DDIV_R6, a0, a1, a2);
1842 i1 = OPC_DDIV, i2 = OPC_MFLO;
1844 case INDEX_op_divu_i64:
1845 if (use_mips32r6_instructions) {
1846 tcg_out_opc_reg(s, OPC_DDIVU_R6, a0, a1, a2);
1849 i1 = OPC_DDIVU, i2 = OPC_MFLO;
1851 case INDEX_op_rem_i64:
1852 if (use_mips32r6_instructions) {
1853 tcg_out_opc_reg(s, OPC_DMOD, a0, a1, a2);
1856 i1 = OPC_DDIV, i2 = OPC_MFHI;
1858 case INDEX_op_remu_i64:
1859 if (use_mips32r6_instructions) {
1860 tcg_out_opc_reg(s, OPC_DMODU, a0, a1, a2);
1863 i1 = OPC_DDIVU, i2 = OPC_MFHI;
1865 tcg_out_opc_reg(s, i1, 0, a1, a2);
1866 tcg_out_opc_reg(s, i2, a0, 0, 0);
1869 case INDEX_op_muls2_i32:
1872 case INDEX_op_mulu2_i32:
1875 case INDEX_op_muls2_i64:
1878 case INDEX_op_mulu2_i64:
1881 tcg_out_opc_reg(s, i1, 0, a2, args[3]);
1882 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0);
1883 tcg_out_opc_reg(s, OPC_MFHI, a1, 0, 0);
1886 case INDEX_op_not_i32:
1887 case INDEX_op_not_i64:
1891 tcg_out_opc_reg(s, i1, a0, TCG_REG_ZERO, a1);
1894 case INDEX_op_bswap16_i32:
1895 case INDEX_op_bswap16_i64:
1896 tcg_out_bswap16(s, a0, a1, a2);
1898 case INDEX_op_bswap32_i32:
1899 tcg_out_bswap32(s, a0, a1, 0);
1901 case INDEX_op_bswap32_i64:
1902 tcg_out_bswap32(s, a0, a1, a2);
1904 case INDEX_op_bswap64_i64:
1905 tcg_out_bswap64(s, a0, a1);
1907 case INDEX_op_extrh_i64_i32:
1908 tcg_out_dsra(s, a0, a1, 32);
1911 case INDEX_op_sar_i32:
1912 i1 = OPC_SRAV, i2 = OPC_SRA;
1914 case INDEX_op_shl_i32:
1915 i1 = OPC_SLLV, i2 = OPC_SLL;
1917 case INDEX_op_shr_i32:
1918 i1 = OPC_SRLV, i2 = OPC_SRL;
1920 case INDEX_op_rotr_i32:
1921 i1 = OPC_ROTRV, i2 = OPC_ROTR;
1924 tcg_out_opc_sa(s, i2, a0, a1, a2);
1928 tcg_out_opc_reg(s, i1, a0, a2, a1);
1930 case INDEX_op_rotl_i32:
1932 tcg_out_opc_sa(s, OPC_ROTR, a0, a1, 32 - a2);
1934 tcg_out_opc_reg(s, OPC_SUBU, TCG_TMP0, TCG_REG_ZERO, a2);
1935 tcg_out_opc_reg(s, OPC_ROTRV, a0, TCG_TMP0, a1);
1938 case INDEX_op_sar_i64:
1940 tcg_out_dsra(s, a0, a1, a2);
1945 case INDEX_op_shl_i64:
1947 tcg_out_dsll(s, a0, a1, a2);
1952 case INDEX_op_shr_i64:
1954 tcg_out_dsrl(s, a0, a1, a2);
1959 case INDEX_op_rotr_i64:
1961 tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, a2);
1966 case INDEX_op_rotl_i64:
1968 tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, 64 - a2);
1970 tcg_out_opc_reg(s, OPC_DSUBU, TCG_TMP0, TCG_REG_ZERO, a2);
1971 tcg_out_opc_reg(s, OPC_DROTRV, a0, TCG_TMP0, a1);
1975 case INDEX_op_clz_i32:
1976 tcg_out_clz(s, OPC_CLZ, OPC_CLZ_R6, 32, a0, a1, a2);
1978 case INDEX_op_clz_i64:
1979 tcg_out_clz(s, OPC_DCLZ, OPC_DCLZ_R6, 64, a0, a1, a2);
1982 case INDEX_op_deposit_i32:
1983 tcg_out_opc_bf(s, OPC_INS, a0, a2, args[3] + args[4] - 1, args[3]);
1985 case INDEX_op_deposit_i64:
1986 tcg_out_opc_bf64(s, OPC_DINS, OPC_DINSM, OPC_DINSU, a0, a2,
1987 args[3] + args[4] - 1, args[3]);
1989 case INDEX_op_extract_i32:
1990 tcg_out_opc_bf(s, OPC_EXT, a0, a1, args[3] - 1, a2);
1992 case INDEX_op_extract_i64:
1993 tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1,
1997 case INDEX_op_brcond_i32:
1998 case INDEX_op_brcond_i64:
1999 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
2001 case INDEX_op_brcond2_i32:
2002 tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
2005 case INDEX_op_movcond_i32:
2006 case INDEX_op_movcond_i64:
2007 tcg_out_movcond(s, args[5], a0, a1, a2, args[3], args[4]);
2010 case INDEX_op_setcond_i32:
2011 case INDEX_op_setcond_i64:
2012 tcg_out_setcond(s, args[3], a0, a1, a2);
2014 case INDEX_op_setcond2_i32:
2015 tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
2018 case INDEX_op_qemu_ld_a64_i32:
2019 if (TCG_TARGET_REG_BITS == 32) {
2020 tcg_out_qemu_ld(s, a0, 0, a1, a2, args[3], TCG_TYPE_I32);
2024 case INDEX_op_qemu_ld_a32_i32:
2025 tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2027 case INDEX_op_qemu_ld_a32_i64:
2028 if (TCG_TARGET_REG_BITS == 64) {
2029 tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2031 tcg_out_qemu_ld(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2034 case INDEX_op_qemu_ld_a64_i64:
2035 if (TCG_TARGET_REG_BITS == 64) {
2036 tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2038 tcg_out_qemu_ld(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2042 case INDEX_op_qemu_st_a64_i32:
2043 if (TCG_TARGET_REG_BITS == 32) {
2044 tcg_out_qemu_st(s, a0, 0, a1, a2, args[3], TCG_TYPE_I32);
2048 case INDEX_op_qemu_st_a32_i32:
2049 tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2051 case INDEX_op_qemu_st_a32_i64:
2052 if (TCG_TARGET_REG_BITS == 64) {
2053 tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2055 tcg_out_qemu_st(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2058 case INDEX_op_qemu_st_a64_i64:
2059 if (TCG_TARGET_REG_BITS == 64) {
2060 tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2062 tcg_out_qemu_st(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2066 case INDEX_op_add2_i32:
2067 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
2068 const_args[4], const_args[5], false);
2070 case INDEX_op_sub2_i32:
2071 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
2072 const_args[4], const_args[5], true);
2078 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
2079 case INDEX_op_mov_i64:
2080 case INDEX_op_call: /* Always emitted via tcg_out_call. */
2081 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
2082 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
2083 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */
2084 case INDEX_op_ext8s_i64:
2085 case INDEX_op_ext8u_i32:
2086 case INDEX_op_ext8u_i64:
2087 case INDEX_op_ext16s_i32:
2088 case INDEX_op_ext16s_i64:
2089 case INDEX_op_ext32s_i64:
2090 case INDEX_op_ext32u_i64:
2091 case INDEX_op_ext_i32_i64:
2092 case INDEX_op_extu_i32_i64:
2093 case INDEX_op_extrl_i64_i32:
2095 g_assert_not_reached();
2099 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2102 case INDEX_op_goto_ptr:
2105 case INDEX_op_ld8u_i32:
2106 case INDEX_op_ld8s_i32:
2107 case INDEX_op_ld16u_i32:
2108 case INDEX_op_ld16s_i32:
2109 case INDEX_op_ld_i32:
2110 case INDEX_op_not_i32:
2111 case INDEX_op_bswap16_i32:
2112 case INDEX_op_bswap32_i32:
2113 case INDEX_op_ext8s_i32:
2114 case INDEX_op_ext16s_i32:
2115 case INDEX_op_extract_i32:
2116 case INDEX_op_ld8u_i64:
2117 case INDEX_op_ld8s_i64:
2118 case INDEX_op_ld16u_i64:
2119 case INDEX_op_ld16s_i64:
2120 case INDEX_op_ld32s_i64:
2121 case INDEX_op_ld32u_i64:
2122 case INDEX_op_ld_i64:
2123 case INDEX_op_not_i64:
2124 case INDEX_op_bswap16_i64:
2125 case INDEX_op_bswap32_i64:
2126 case INDEX_op_bswap64_i64:
2127 case INDEX_op_ext8s_i64:
2128 case INDEX_op_ext16s_i64:
2129 case INDEX_op_ext32s_i64:
2130 case INDEX_op_ext32u_i64:
2131 case INDEX_op_ext_i32_i64:
2132 case INDEX_op_extu_i32_i64:
2133 case INDEX_op_extrl_i64_i32:
2134 case INDEX_op_extrh_i64_i32:
2135 case INDEX_op_extract_i64:
2136 return C_O1_I1(r, r);
2138 case INDEX_op_st8_i32:
2139 case INDEX_op_st16_i32:
2140 case INDEX_op_st_i32:
2141 case INDEX_op_st8_i64:
2142 case INDEX_op_st16_i64:
2143 case INDEX_op_st32_i64:
2144 case INDEX_op_st_i64:
2145 return C_O0_I2(rZ, r);
2147 case INDEX_op_add_i32:
2148 case INDEX_op_add_i64:
2149 return C_O1_I2(r, r, rJ);
2150 case INDEX_op_sub_i32:
2151 case INDEX_op_sub_i64:
2152 return C_O1_I2(r, rZ, rN);
2153 case INDEX_op_mul_i32:
2154 case INDEX_op_mulsh_i32:
2155 case INDEX_op_muluh_i32:
2156 case INDEX_op_div_i32:
2157 case INDEX_op_divu_i32:
2158 case INDEX_op_rem_i32:
2159 case INDEX_op_remu_i32:
2160 case INDEX_op_nor_i32:
2161 case INDEX_op_setcond_i32:
2162 case INDEX_op_mul_i64:
2163 case INDEX_op_mulsh_i64:
2164 case INDEX_op_muluh_i64:
2165 case INDEX_op_div_i64:
2166 case INDEX_op_divu_i64:
2167 case INDEX_op_rem_i64:
2168 case INDEX_op_remu_i64:
2169 case INDEX_op_nor_i64:
2170 case INDEX_op_setcond_i64:
2171 return C_O1_I2(r, rZ, rZ);
2172 case INDEX_op_muls2_i32:
2173 case INDEX_op_mulu2_i32:
2174 case INDEX_op_muls2_i64:
2175 case INDEX_op_mulu2_i64:
2176 return C_O2_I2(r, r, r, r);
2177 case INDEX_op_and_i32:
2178 case INDEX_op_and_i64:
2179 return C_O1_I2(r, r, rIK);
2180 case INDEX_op_or_i32:
2181 case INDEX_op_xor_i32:
2182 case INDEX_op_or_i64:
2183 case INDEX_op_xor_i64:
2184 return C_O1_I2(r, r, rI);
2185 case INDEX_op_shl_i32:
2186 case INDEX_op_shr_i32:
2187 case INDEX_op_sar_i32:
2188 case INDEX_op_rotr_i32:
2189 case INDEX_op_rotl_i32:
2190 case INDEX_op_shl_i64:
2191 case INDEX_op_shr_i64:
2192 case INDEX_op_sar_i64:
2193 case INDEX_op_rotr_i64:
2194 case INDEX_op_rotl_i64:
2195 return C_O1_I2(r, r, ri);
2196 case INDEX_op_clz_i32:
2197 case INDEX_op_clz_i64:
2198 return C_O1_I2(r, r, rWZ);
2200 case INDEX_op_deposit_i32:
2201 case INDEX_op_deposit_i64:
2202 return C_O1_I2(r, 0, rZ);
2203 case INDEX_op_brcond_i32:
2204 case INDEX_op_brcond_i64:
2205 return C_O0_I2(rZ, rZ);
2206 case INDEX_op_movcond_i32:
2207 case INDEX_op_movcond_i64:
2208 return (use_mips32r6_instructions
2209 ? C_O1_I4(r, rZ, rZ, rZ, rZ)
2210 : C_O1_I4(r, rZ, rZ, rZ, 0));
2211 case INDEX_op_add2_i32:
2212 case INDEX_op_sub2_i32:
2213 return C_O2_I4(r, r, rZ, rZ, rN, rN);
2214 case INDEX_op_setcond2_i32:
2215 return C_O1_I4(r, rZ, rZ, rZ, rZ);
2216 case INDEX_op_brcond2_i32:
2217 return C_O0_I4(rZ, rZ, rZ, rZ);
2219 case INDEX_op_qemu_ld_a32_i32:
2220 return C_O1_I1(r, r);
2221 case INDEX_op_qemu_ld_a64_i32:
2222 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O1_I2(r, r, r);
2223 case INDEX_op_qemu_st_a32_i32:
2224 return C_O0_I2(rZ, r);
2225 case INDEX_op_qemu_st_a64_i32:
2226 return TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r) : C_O0_I3(rZ, r, r);
2227 case INDEX_op_qemu_ld_a32_i64:
2228 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O2_I1(r, r, r);
2229 case INDEX_op_qemu_ld_a64_i64:
2230 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O2_I2(r, r, r, r);
2231 case INDEX_op_qemu_st_a32_i64:
2232 return TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r) : C_O0_I3(rZ, rZ, r);
2233 case INDEX_op_qemu_st_a64_i64:
2234 return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r)
2235 : C_O0_I4(rZ, rZ, r, r));
2238 g_assert_not_reached();
2242 static const int tcg_target_callee_save_regs[] = {
2249 TCG_REG_S6, /* used for the tb base (TCG_REG_TB) */
2250 TCG_REG_S7, /* used for guest_base */
2251 TCG_REG_S8, /* used for the global env (TCG_AREG0) */
2252 TCG_REG_RA, /* should be last for ABI compliance */
2255 /* The Linux kernel doesn't provide any information about the available
2256 instruction set. Probe it using a signal handler. */
2259 #ifndef use_movnz_instructions
2260 bool use_movnz_instructions = false;
2263 #ifndef use_mips32_instructions
2264 bool use_mips32_instructions = false;
2267 #ifndef use_mips32r2_instructions
2268 bool use_mips32r2_instructions = false;
2271 static volatile sig_atomic_t got_sigill;
2273 static void sigill_handler(int signo, siginfo_t *si, void *data)
2275 /* Skip the faulty instruction */
2276 ucontext_t *uc = (ucontext_t *)data;
2277 uc->uc_mcontext.pc += 4;
2282 static void tcg_target_detect_isa(void)
2284 struct sigaction sa_old, sa_new;
2286 memset(&sa_new, 0, sizeof(sa_new));
2287 sa_new.sa_flags = SA_SIGINFO;
2288 sa_new.sa_sigaction = sigill_handler;
2289 sigaction(SIGILL, &sa_new, &sa_old);
2291 /* Probe for movn/movz, necessary to implement movcond. */
2292 #ifndef use_movnz_instructions
2294 asm volatile(".set push\n"
2296 "movn $zero, $zero, $zero\n"
2297 "movz $zero, $zero, $zero\n"
2300 use_movnz_instructions = !got_sigill;
2303 /* Probe for MIPS32 instructions. As no subsetting is allowed
2304 by the specification, it is only necessary to probe for one
2305 of the instructions. */
2306 #ifndef use_mips32_instructions
2308 asm volatile(".set push\n"
2310 "mul $zero, $zero\n"
2313 use_mips32_instructions = !got_sigill;
2316 /* Probe for MIPS32r2 instructions if MIPS32 instructions are
2317 available. As no subsetting is allowed by the specification,
2318 it is only necessary to probe for one of the instructions. */
2319 #ifndef use_mips32r2_instructions
2320 if (use_mips32_instructions) {
2322 asm volatile(".set push\n"
2324 "seb $zero, $zero\n"
2327 use_mips32r2_instructions = !got_sigill;
2331 sigaction(SIGILL, &sa_old, NULL);
2334 static tcg_insn_unit *align_code_ptr(TCGContext *s)
2336 uintptr_t p = (uintptr_t)s->code_ptr;
2339 s->code_ptr = (void *)p;
2344 /* Stack frame parameters. */
2345 #define REG_SIZE (TCG_TARGET_REG_BITS / 8)
2346 #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
2347 #define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2349 #define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
2350 + TCG_TARGET_STACK_ALIGN - 1) \
2351 & -TCG_TARGET_STACK_ALIGN)
2352 #define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
2354 /* We're expecting to be able to use an immediate for frame allocation. */
2355 QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7fff);
2357 /* Generate global QEMU prologue and epilogue code */
2358 static void tcg_target_qemu_prologue(TCGContext *s)
2362 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
2365 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
2366 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2367 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2368 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2371 #ifndef CONFIG_SOFTMMU
2372 if (guest_base != (int16_t)guest_base) {
2373 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
2374 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2377 if (TCG_TARGET_REG_BITS == 64) {
2378 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, tcg_target_call_iarg_regs[1]);
2381 /* Call generated code */
2382 tcg_out_opc_reg(s, OPC_JR, 0, tcg_target_call_iarg_regs[1], 0);
2384 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2387 * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
2388 * and fall through to the rest of the epilogue.
2390 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2391 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_V0, TCG_REG_ZERO);
2394 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
2395 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2396 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2397 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2400 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2402 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
2404 if (use_mips32r2_instructions) {
2408 /* Bswap subroutines: Input in TCG_TMP0, output in TCG_TMP3;
2409 clobbers TCG_TMP1, TCG_TMP2. */
2412 * bswap32 -- 32-bit swap (signed result for mips64). a0 = abcd.
2414 bswap32_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2415 /* t3 = (ssss)d000 */
2416 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP3, TCG_TMP0, 24);
2418 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 24);
2420 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2422 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2424 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2426 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2428 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2430 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2431 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2432 /* t3 = dcba -- delay slot */
2433 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2435 if (TCG_TARGET_REG_BITS == 32) {
2440 * bswap32u -- unsigned 32-bit swap. a0 = ....abcd.
2442 bswap32u_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2443 /* t1 = (0000)000d */
2444 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP0, 0xff);
2446 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, TCG_TMP0, 24);
2447 /* t1 = (0000)d000 */
2448 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2450 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2452 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2454 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2456 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2458 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2460 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2461 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2462 /* t3 = dcba -- delay slot */
2463 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2466 * bswap64 -- 64-bit swap. a0 = abcdefgh
2468 bswap64_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2470 tcg_out_dsll(s, TCG_TMP3, TCG_TMP0, 56);
2472 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 56);
2475 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2477 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2479 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 40);
2481 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2483 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2486 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2488 tcg_out_dsrl(s, TCG_TMP2, TCG_TMP0, 32);
2490 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2493 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP2, 0xff00);
2495 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP2, 0x00ff);
2497 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 8);
2499 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 24);
2502 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2504 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 16);
2506 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2509 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP1, 0x00ff);
2511 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2513 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2515 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2518 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2519 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2520 /* t3 = hgfedcba -- delay slot */
2521 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2524 static void tcg_target_init(TCGContext *s)
2526 tcg_target_detect_isa();
2527 tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
2528 if (TCG_TARGET_REG_BITS == 64) {
2529 tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
2532 tcg_target_call_clobber_regs = 0;
2533 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
2534 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
2535 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A0);
2536 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A1);
2537 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A2);
2538 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A3);
2539 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T0);
2540 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T1);
2541 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T2);
2542 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T3);
2543 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T4);
2544 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T5);
2545 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T6);
2546 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T7);
2547 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T8);
2548 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T9);
2550 s->reserved_regs = 0;
2551 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); /* zero register */
2552 tcg_regset_set_reg(s->reserved_regs, TCG_REG_K0); /* kernel use only */
2553 tcg_regset_set_reg(s->reserved_regs, TCG_REG_K1); /* kernel use only */
2554 tcg_regset_set_reg(s->reserved_regs, TCG_TMP0); /* internal use */
2555 tcg_regset_set_reg(s->reserved_regs, TCG_TMP1); /* internal use */
2556 tcg_regset_set_reg(s->reserved_regs, TCG_TMP2); /* internal use */
2557 tcg_regset_set_reg(s->reserved_regs, TCG_TMP3); /* internal use */
2558 tcg_regset_set_reg(s->reserved_regs, TCG_REG_RA); /* return address */
2559 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); /* stack pointer */
2560 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP); /* global pointer */
2561 if (TCG_TARGET_REG_BITS == 64) {
2562 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TB); /* tc->tc_ptr */
2568 uint8_t fde_def_cfa[4];
2569 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
2572 #define ELF_HOST_MACHINE EM_MIPS
2573 /* GDB doesn't appear to require proper setting of ELF_HOST_FLAGS,
2574 which is good because they're really quite complicated for MIPS. */
2576 static const DebugFrame debug_frame = {
2577 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
2580 .h.cie.code_align = 1,
2581 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
2582 .h.cie.return_column = TCG_REG_RA,
2584 /* Total FDE size does not include the "len" member. */
2585 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2588 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */
2589 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2593 0x80 + 16, 9, /* DW_CFA_offset, s0, -72 */
2594 0x80 + 17, 8, /* DW_CFA_offset, s2, -64 */
2595 0x80 + 18, 7, /* DW_CFA_offset, s3, -56 */
2596 0x80 + 19, 6, /* DW_CFA_offset, s4, -48 */
2597 0x80 + 20, 5, /* DW_CFA_offset, s5, -40 */
2598 0x80 + 21, 4, /* DW_CFA_offset, s6, -32 */
2599 0x80 + 22, 3, /* DW_CFA_offset, s7, -24 */
2600 0x80 + 30, 2, /* DW_CFA_offset, s8, -16 */
2601 0x80 + 31, 1, /* DW_CFA_offset, ra, -8 */
2605 void tcg_register_jit(const void *buf, size_t buf_size)
2607 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));