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"
28 #include "../tcg-pool.c.inc"
30 #if TCG_TARGET_REG_BITS == 32
31 # define LO_OFF (HOST_BIG_ENDIAN * 4)
32 # define HI_OFF (4 - LO_OFF)
34 /* Assert at compile-time that these values are never used for 64-bit. */
35 # define LO_OFF ({ qemu_build_not_reached(); 0; })
36 # define HI_OFF ({ qemu_build_not_reached(); 0; })
39 #ifdef CONFIG_DEBUG_TCG
40 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
76 #define TCG_TMP0 TCG_REG_AT
77 #define TCG_TMP1 TCG_REG_T9
78 #define TCG_TMP2 TCG_REG_T8
79 #define TCG_TMP3 TCG_REG_T7
81 #ifndef CONFIG_SOFTMMU
82 #define TCG_GUEST_BASE_REG TCG_REG_S7
84 #if TCG_TARGET_REG_BITS == 64
85 #define TCG_REG_TB TCG_REG_S6
87 #define TCG_REG_TB (qemu_build_not_reached(), TCG_REG_ZERO)
90 /* check if we really need so many registers :P */
91 static const int tcg_target_reg_alloc_order[] = {
92 /* Call saved registers. */
103 /* Call clobbered registers. */
113 /* Argument registers, opposite order of allocation. */
124 static const TCGReg tcg_target_call_iarg_regs[] = {
129 #if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
137 static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
139 tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
140 tcg_debug_assert(slot >= 0 && slot <= 1);
141 return TCG_REG_V0 + slot;
144 static const tcg_insn_unit *tb_ret_addr;
145 static const tcg_insn_unit *bswap32_addr;
146 static const tcg_insn_unit *bswap32u_addr;
147 static const tcg_insn_unit *bswap64_addr;
149 static bool reloc_pc16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
151 /* Let the compiler perform the right-shift as part of the arithmetic. */
152 const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
153 ptrdiff_t disp = target - (src_rx + 1);
154 if (disp == (int16_t)disp) {
155 *src_rw = deposit32(*src_rw, 0, 16, disp);
161 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
162 intptr_t value, intptr_t addend)
167 return reloc_pc16(code_ptr, (const tcg_insn_unit *)value);
169 if (value != (int16_t)value) {
172 *code_ptr = deposit32(*code_ptr, 0, 16, value);
175 g_assert_not_reached();
178 #define TCG_CT_CONST_ZERO 0x100
179 #define TCG_CT_CONST_U16 0x200 /* Unsigned 16-bit: 0 - 0xffff. */
180 #define TCG_CT_CONST_S16 0x400 /* Signed 16-bit: -32768 - 32767 */
181 #define TCG_CT_CONST_P2M1 0x800 /* Power of 2 minus 1. */
182 #define TCG_CT_CONST_N16 0x1000 /* "Negatable" 16-bit: -32767 - 32767 */
183 #define TCG_CT_CONST_WSZ 0x2000 /* word size */
185 #define ALL_GENERAL_REGS 0xffffffffu
187 static bool is_p2m1(tcg_target_long val)
189 return val && ((val + 1) & val) == 0;
192 /* test if a constant matches the constraint */
193 static bool tcg_target_const_match(int64_t val, TCGType type, int ct, int vece)
195 if (ct & TCG_CT_CONST) {
197 } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
199 } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
201 } else if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
203 } else if ((ct & TCG_CT_CONST_N16) && val >= -32767 && val <= 32767) {
205 } else if ((ct & TCG_CT_CONST_P2M1)
206 && use_mips32r2_instructions && is_p2m1(val)) {
208 } else if ((ct & TCG_CT_CONST_WSZ)
209 && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
215 /* instruction opcodes */
221 OPC_BLEZ = 006 << 26,
222 OPC_BGTZ = 007 << 26,
223 OPC_ADDIU = 011 << 26,
224 OPC_SLTI = 012 << 26,
225 OPC_SLTIU = 013 << 26,
226 OPC_ANDI = 014 << 26,
228 OPC_XORI = 016 << 26,
230 OPC_BNEL = 025 << 26,
231 OPC_BNEZALC_R6 = 030 << 26,
232 OPC_DADDIU = 031 << 26,
253 OPC_SPECIAL = 000 << 26,
254 OPC_SLL = OPC_SPECIAL | 000,
255 OPC_SRL = OPC_SPECIAL | 002,
256 OPC_ROTR = OPC_SPECIAL | 002 | (1 << 21),
257 OPC_SRA = OPC_SPECIAL | 003,
258 OPC_SLLV = OPC_SPECIAL | 004,
259 OPC_SRLV = OPC_SPECIAL | 006,
260 OPC_ROTRV = OPC_SPECIAL | 006 | 0100,
261 OPC_SRAV = OPC_SPECIAL | 007,
262 OPC_JR_R5 = OPC_SPECIAL | 010,
263 OPC_JALR = OPC_SPECIAL | 011,
264 OPC_MOVZ = OPC_SPECIAL | 012,
265 OPC_MOVN = OPC_SPECIAL | 013,
266 OPC_SYNC = OPC_SPECIAL | 017,
267 OPC_MFHI = OPC_SPECIAL | 020,
268 OPC_MFLO = OPC_SPECIAL | 022,
269 OPC_DSLLV = OPC_SPECIAL | 024,
270 OPC_DSRLV = OPC_SPECIAL | 026,
271 OPC_DROTRV = OPC_SPECIAL | 026 | 0100,
272 OPC_DSRAV = OPC_SPECIAL | 027,
273 OPC_MULT = OPC_SPECIAL | 030,
274 OPC_MUL_R6 = OPC_SPECIAL | 030 | 0200,
275 OPC_MUH = OPC_SPECIAL | 030 | 0300,
276 OPC_MULTU = OPC_SPECIAL | 031,
277 OPC_MULU = OPC_SPECIAL | 031 | 0200,
278 OPC_MUHU = OPC_SPECIAL | 031 | 0300,
279 OPC_DIV = OPC_SPECIAL | 032,
280 OPC_DIV_R6 = OPC_SPECIAL | 032 | 0200,
281 OPC_MOD = OPC_SPECIAL | 032 | 0300,
282 OPC_DIVU = OPC_SPECIAL | 033,
283 OPC_DIVU_R6 = OPC_SPECIAL | 033 | 0200,
284 OPC_MODU = OPC_SPECIAL | 033 | 0300,
285 OPC_DMULT = OPC_SPECIAL | 034,
286 OPC_DMUL = OPC_SPECIAL | 034 | 0200,
287 OPC_DMUH = OPC_SPECIAL | 034 | 0300,
288 OPC_DMULTU = OPC_SPECIAL | 035,
289 OPC_DMULU = OPC_SPECIAL | 035 | 0200,
290 OPC_DMUHU = OPC_SPECIAL | 035 | 0300,
291 OPC_DDIV = OPC_SPECIAL | 036,
292 OPC_DDIV_R6 = OPC_SPECIAL | 036 | 0200,
293 OPC_DMOD = OPC_SPECIAL | 036 | 0300,
294 OPC_DDIVU = OPC_SPECIAL | 037,
295 OPC_DDIVU_R6 = OPC_SPECIAL | 037 | 0200,
296 OPC_DMODU = OPC_SPECIAL | 037 | 0300,
297 OPC_ADDU = OPC_SPECIAL | 041,
298 OPC_SUBU = OPC_SPECIAL | 043,
299 OPC_AND = OPC_SPECIAL | 044,
300 OPC_OR = OPC_SPECIAL | 045,
301 OPC_XOR = OPC_SPECIAL | 046,
302 OPC_NOR = OPC_SPECIAL | 047,
303 OPC_SLT = OPC_SPECIAL | 052,
304 OPC_SLTU = OPC_SPECIAL | 053,
305 OPC_DADDU = OPC_SPECIAL | 055,
306 OPC_DSUBU = OPC_SPECIAL | 057,
307 OPC_SELEQZ = OPC_SPECIAL | 065,
308 OPC_SELNEZ = OPC_SPECIAL | 067,
309 OPC_DSLL = OPC_SPECIAL | 070,
310 OPC_DSRL = OPC_SPECIAL | 072,
311 OPC_DROTR = OPC_SPECIAL | 072 | (1 << 21),
312 OPC_DSRA = OPC_SPECIAL | 073,
313 OPC_DSLL32 = OPC_SPECIAL | 074,
314 OPC_DSRL32 = OPC_SPECIAL | 076,
315 OPC_DROTR32 = OPC_SPECIAL | 076 | (1 << 21),
316 OPC_DSRA32 = OPC_SPECIAL | 077,
317 OPC_CLZ_R6 = OPC_SPECIAL | 0120,
318 OPC_DCLZ_R6 = OPC_SPECIAL | 0122,
320 OPC_REGIMM = 001 << 26,
321 OPC_BLTZ = OPC_REGIMM | (000 << 16),
322 OPC_BGEZ = OPC_REGIMM | (001 << 16),
324 OPC_SPECIAL2 = 034 << 26,
325 OPC_MUL_R5 = OPC_SPECIAL2 | 002,
326 OPC_CLZ = OPC_SPECIAL2 | 040,
327 OPC_DCLZ = OPC_SPECIAL2 | 044,
329 OPC_SPECIAL3 = 037 << 26,
330 OPC_EXT = OPC_SPECIAL3 | 000,
331 OPC_DEXTM = OPC_SPECIAL3 | 001,
332 OPC_DEXTU = OPC_SPECIAL3 | 002,
333 OPC_DEXT = OPC_SPECIAL3 | 003,
334 OPC_INS = OPC_SPECIAL3 | 004,
335 OPC_DINSM = OPC_SPECIAL3 | 005,
336 OPC_DINSU = OPC_SPECIAL3 | 006,
337 OPC_DINS = OPC_SPECIAL3 | 007,
338 OPC_WSBH = OPC_SPECIAL3 | 00240,
339 OPC_DSBH = OPC_SPECIAL3 | 00244,
340 OPC_DSHD = OPC_SPECIAL3 | 00544,
341 OPC_SEB = OPC_SPECIAL3 | 02040,
342 OPC_SEH = OPC_SPECIAL3 | 03040,
344 /* MIPS r6 doesn't have JR, JALR should be used instead */
345 OPC_JR = use_mips32r6_instructions ? OPC_JALR : OPC_JR_R5,
348 * MIPS r6 replaces MUL with an alternative encoding which is
349 * backwards-compatible at the assembly level.
351 OPC_MUL = use_mips32r6_instructions ? OPC_MUL_R6 : OPC_MUL_R5,
353 /* MIPS r6 introduced names for weaker variants of SYNC. These are
354 backward compatible to previous architecture revisions. */
355 OPC_SYNC_WMB = OPC_SYNC | 0x04 << 6,
356 OPC_SYNC_MB = OPC_SYNC | 0x10 << 6,
357 OPC_SYNC_ACQUIRE = OPC_SYNC | 0x11 << 6,
358 OPC_SYNC_RELEASE = OPC_SYNC | 0x12 << 6,
359 OPC_SYNC_RMB = OPC_SYNC | 0x13 << 6,
361 /* Aliases for convenience. */
362 ALIAS_PADD = sizeof(void *) == 4 ? OPC_ADDU : OPC_DADDU,
363 ALIAS_PADDI = sizeof(void *) == 4 ? OPC_ADDIU : OPC_DADDIU,
369 static void tcg_out_opc_reg(TCGContext *s, MIPSInsn opc,
370 TCGReg rd, TCGReg rs, TCGReg rt)
375 inst |= (rs & 0x1F) << 21;
376 inst |= (rt & 0x1F) << 16;
377 inst |= (rd & 0x1F) << 11;
384 static void tcg_out_opc_imm(TCGContext *s, MIPSInsn opc,
385 TCGReg rt, TCGReg rs, TCGArg imm)
390 inst |= (rs & 0x1F) << 21;
391 inst |= (rt & 0x1F) << 16;
392 inst |= (imm & 0xffff);
399 static void tcg_out_opc_bf(TCGContext *s, MIPSInsn opc, TCGReg rt,
400 TCGReg rs, int msb, int lsb)
405 inst |= (rs & 0x1F) << 21;
406 inst |= (rt & 0x1F) << 16;
407 inst |= (msb & 0x1F) << 11;
408 inst |= (lsb & 0x1F) << 6;
412 static void tcg_out_opc_bf64(TCGContext *s, MIPSInsn opc, MIPSInsn opm,
413 MIPSInsn oph, TCGReg rt, TCGReg rs,
420 } else if (msb >= 32) {
424 tcg_out_opc_bf(s, opc, rt, rs, msb, lsb);
430 static void tcg_out_opc_br(TCGContext *s, MIPSInsn opc, TCGReg rt, TCGReg rs)
432 tcg_out_opc_imm(s, opc, rt, rs, 0);
438 static void tcg_out_opc_sa(TCGContext *s, MIPSInsn opc,
439 TCGReg rd, TCGReg rt, TCGArg sa)
444 inst |= (rt & 0x1F) << 16;
445 inst |= (rd & 0x1F) << 11;
446 inst |= (sa & 0x1F) << 6;
451 static void tcg_out_opc_sa64(TCGContext *s, MIPSInsn opc1, MIPSInsn opc2,
452 TCGReg rd, TCGReg rt, TCGArg sa)
456 inst = (sa & 32 ? opc2 : opc1);
457 inst |= (rt & 0x1F) << 16;
458 inst |= (rd & 0x1F) << 11;
459 inst |= (sa & 0x1F) << 6;
465 * Returns true if the branch was in range and the insn was emitted.
467 static bool tcg_out_opc_jmp(TCGContext *s, MIPSInsn opc, const void *target)
469 uintptr_t dest = (uintptr_t)target;
470 uintptr_t from = (uintptr_t)tcg_splitwx_to_rx(s->code_ptr) + 4;
473 /* The pc-region branch happens within the 256MB region of
474 the delay slot (thus the +4). */
475 if ((from ^ dest) & -(1 << 28)) {
478 tcg_debug_assert((dest & 3) == 0);
481 inst |= (dest >> 2) & 0x3ffffff;
486 static void tcg_out_nop(TCGContext *s)
491 static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
493 memset(p, 0, count * sizeof(tcg_insn_unit));
496 static void tcg_out_dsll(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
498 tcg_out_opc_sa64(s, OPC_DSLL, OPC_DSLL32, rd, rt, sa);
501 static void tcg_out_dsrl(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
503 tcg_out_opc_sa64(s, OPC_DSRL, OPC_DSRL32, rd, rt, sa);
506 static void tcg_out_dsra(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
508 tcg_out_opc_sa64(s, OPC_DSRA, OPC_DSRA32, rd, rt, sa);
511 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
513 /* Simple reg-reg move, optimising out the 'do nothing' case */
515 tcg_out_opc_reg(s, OPC_OR, ret, arg, TCG_REG_ZERO);
520 static bool tcg_out_movi_one(TCGContext *s, TCGReg ret, tcg_target_long arg)
522 if (arg == (int16_t)arg) {
523 tcg_out_opc_imm(s, OPC_ADDIU, ret, TCG_REG_ZERO, arg);
526 if (arg == (uint16_t)arg) {
527 tcg_out_opc_imm(s, OPC_ORI, ret, TCG_REG_ZERO, arg);
530 if (arg == (int32_t)arg && (arg & 0xffff) == 0) {
531 tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
537 static bool tcg_out_movi_two(TCGContext *s, TCGReg ret, tcg_target_long arg)
540 * All signed 32-bit constants are loadable with two immediates,
541 * and everything else requires more work.
543 if (arg == (int32_t)arg) {
544 if (!tcg_out_movi_one(s, ret, arg)) {
545 tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
546 tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg & 0xffff);
553 static void tcg_out_movi_pool(TCGContext *s, TCGReg ret,
554 tcg_target_long arg, TCGReg tbreg)
556 new_pool_label(s, arg, R_MIPS_16, s->code_ptr, tcg_tbrel_diff(s, NULL));
557 tcg_out_opc_imm(s, OPC_LD, ret, tbreg, 0);
560 static void tcg_out_movi_int(TCGContext *s, TCGType type, TCGReg ret,
561 tcg_target_long arg, TCGReg tbreg)
566 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
570 /* Load all 32-bit constants. */
571 if (tcg_out_movi_two(s, ret, arg)) {
574 assert(TCG_TARGET_REG_BITS == 64);
576 /* Load addresses within 2GB of TB with 1 or 3 insns. */
577 tmp = tcg_tbrel_diff(s, (void *)arg);
578 if (tmp == (int16_t)tmp) {
579 tcg_out_opc_imm(s, OPC_DADDIU, ret, tbreg, tmp);
582 if (tcg_out_movi_two(s, ret, tmp)) {
583 tcg_out_opc_reg(s, OPC_DADDU, ret, ret, tbreg);
588 * Load bitmasks with a right-shift. This is good for things
589 * like 0x0fff_ffff_ffff_fff0: ADDUI r,0,0xff00 + DSRL r,r,4.
590 * or similarly using LUI. For this to work, bit 31 must be set.
592 if (arg > 0 && (int32_t)arg < 0) {
594 if (tcg_out_movi_one(s, ret, arg << sh)) {
595 tcg_out_dsrl(s, ret, ret, sh);
601 * Load slightly larger constants using left-shift.
602 * Limit this sequence to 3 insns to avoid too much expansion.
605 if (sh && tcg_out_movi_two(s, ret, arg >> sh)) {
606 tcg_out_dsll(s, ret, ret, sh);
611 * Load slightly larger constants using left-shift and add/or.
612 * Prefer addi with a negative immediate when that would produce
613 * a larger shift. For this to work, bits 15 and 16 must be set.
617 if ((arg & 0x18000) == 0x18000) {
623 if (tcg_out_movi_one(s, ret, tmp)) {
624 tcg_out_dsll(s, ret, ret, sh);
625 tcg_out_opc_imm(s, lo < 0 ? OPC_DADDIU : OPC_ORI, ret, ret, lo);
630 /* Otherwise, put 64-bit constants into the constant pool. */
631 tcg_out_movi_pool(s, ret, arg, tbreg);
634 static void tcg_out_movi(TCGContext *s, TCGType type,
635 TCGReg ret, tcg_target_long arg)
637 TCGReg tbreg = TCG_TARGET_REG_BITS == 64 ? TCG_REG_TB : 0;
638 tcg_out_movi_int(s, type, ret, arg, tbreg);
641 static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
643 tcg_debug_assert(TCG_TARGET_HAS_ext8s_i32);
644 tcg_out_opc_reg(s, OPC_SEB, rd, TCG_REG_ZERO, rs);
647 static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs)
649 tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xff);
652 static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
654 tcg_debug_assert(TCG_TARGET_HAS_ext16s_i32);
655 tcg_out_opc_reg(s, OPC_SEH, rd, TCG_REG_ZERO, rs);
658 static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs)
660 tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xffff);
663 static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs)
665 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
666 tcg_out_opc_sa(s, OPC_SLL, rd, rs, 0);
669 static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
672 tcg_out_ext32s(s, rd, rs);
676 static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
678 tcg_out_ext32u(s, rd, rs);
681 static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs)
683 tcg_out_ext32s(s, rd, rs);
686 static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
691 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
694 /* This function is only used for passing structs by reference. */
695 g_assert_not_reached();
698 static void tcg_out_bswap16(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
700 /* ret and arg can't be register tmp0 */
701 tcg_debug_assert(ret != TCG_TMP0);
702 tcg_debug_assert(arg != TCG_TMP0);
704 /* With arg = abcd: */
705 if (use_mips32r2_instructions) {
706 tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg); /* badc */
707 if (flags & TCG_BSWAP_OS) {
708 tcg_out_opc_reg(s, OPC_SEH, ret, 0, ret); /* ssdc */
709 } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
710 tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xffff); /* 00dc */
715 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, arg, 8); /* 0abc */
716 if (!(flags & TCG_BSWAP_IZ)) {
717 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, TCG_TMP0, 0x00ff); /* 000c */
719 if (flags & TCG_BSWAP_OS) {
720 tcg_out_opc_sa(s, OPC_SLL, ret, arg, 24); /* d000 */
721 tcg_out_opc_sa(s, OPC_SRA, ret, ret, 16); /* ssd0 */
723 tcg_out_opc_sa(s, OPC_SLL, ret, arg, 8); /* bcd0 */
724 if (flags & TCG_BSWAP_OZ) {
725 tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xff00); /* 00d0 */
728 tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP0); /* ssdc */
731 static void tcg_out_bswap_subr(TCGContext *s, const tcg_insn_unit *sub)
733 if (!tcg_out_opc_jmp(s, OPC_JAL, sub)) {
734 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP1, (uintptr_t)sub);
735 tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_TMP1, 0);
739 static void tcg_out_bswap32(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
741 if (use_mips32r2_instructions) {
742 tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg);
743 tcg_out_opc_sa(s, OPC_ROTR, ret, ret, 16);
744 if (flags & TCG_BSWAP_OZ) {
745 tcg_out_opc_bf(s, OPC_DEXT, ret, ret, 31, 0);
748 if (flags & TCG_BSWAP_OZ) {
749 tcg_out_bswap_subr(s, bswap32u_addr);
751 tcg_out_bswap_subr(s, bswap32_addr);
753 /* delay slot -- never omit the insn, like tcg_out_mov might. */
754 tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
755 tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
759 static void tcg_out_bswap64(TCGContext *s, TCGReg ret, TCGReg arg)
761 if (use_mips32r2_instructions) {
762 tcg_out_opc_reg(s, OPC_DSBH, ret, 0, arg);
763 tcg_out_opc_reg(s, OPC_DSHD, ret, 0, ret);
765 tcg_out_bswap_subr(s, bswap64_addr);
766 /* delay slot -- never omit the insn, like tcg_out_mov might. */
767 tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
768 tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
772 static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
774 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
775 if (use_mips32r2_instructions) {
776 tcg_out_opc_bf(s, OPC_DEXT, ret, arg, 31, 0);
778 tcg_out_dsll(s, ret, arg, 32);
779 tcg_out_dsrl(s, ret, ret, 32);
783 static void tcg_out_ldst(TCGContext *s, MIPSInsn opc, TCGReg data,
784 TCGReg addr, intptr_t ofs)
788 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - lo);
789 if (addr != TCG_REG_ZERO) {
790 tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP0, TCG_TMP0, addr);
794 tcg_out_opc_imm(s, opc, data, addr, lo);
797 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
798 TCGReg arg1, intptr_t arg2)
800 MIPSInsn opc = OPC_LD;
801 if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
804 tcg_out_ldst(s, opc, arg, arg1, arg2);
807 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
808 TCGReg arg1, intptr_t arg2)
810 MIPSInsn opc = OPC_SD;
811 if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
814 tcg_out_ldst(s, opc, arg, arg1, arg2);
817 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
818 TCGReg base, intptr_t ofs)
821 tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
827 static void tcg_out_addsub2(TCGContext *s, TCGReg rl, TCGReg rh, TCGReg al,
828 TCGReg ah, TCGArg bl, TCGArg bh, bool cbl,
829 bool cbh, bool is_sub)
831 TCGReg th = TCG_TMP1;
833 /* If we have a negative constant such that negating it would
834 make the high part zero, we can (usually) eliminate one insn. */
835 if (cbl && cbh && bh == -1 && bl != 0) {
841 /* By operating on the high part first, we get to use the final
842 carry operation to move back from the temporary. */
844 tcg_out_opc_reg(s, (is_sub ? OPC_SUBU : OPC_ADDU), th, ah, bh);
845 } else if (bh != 0 || ah == rl) {
846 tcg_out_opc_imm(s, OPC_ADDIU, th, ah, (is_sub ? -bh : bh));
851 /* Note that tcg optimization should eliminate the bl == 0 case. */
854 tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, al, bl);
855 tcg_out_opc_imm(s, OPC_ADDIU, rl, al, -bl);
857 tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, al, bl);
858 tcg_out_opc_reg(s, OPC_SUBU, rl, al, bl);
860 tcg_out_opc_reg(s, OPC_SUBU, rh, th, TCG_TMP0);
863 tcg_out_opc_imm(s, OPC_ADDIU, rl, al, bl);
864 tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, rl, bl);
865 } else if (rl == al && rl == bl) {
866 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, al, TCG_TARGET_REG_BITS - 1);
867 tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
869 tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
870 tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, rl, (rl == bl ? al : bl));
872 tcg_out_opc_reg(s, OPC_ADDU, rh, th, TCG_TMP0);
876 /* Bit 0 set if inversion required; bit 1 set if swapping required. */
877 #define MIPS_CMP_INV 1
878 #define MIPS_CMP_SWAP 2
880 static const uint8_t mips_cmp_map[16] = {
883 [TCG_COND_GE] = MIPS_CMP_INV,
884 [TCG_COND_GEU] = MIPS_CMP_INV,
885 [TCG_COND_LE] = MIPS_CMP_INV | MIPS_CMP_SWAP,
886 [TCG_COND_LEU] = MIPS_CMP_INV | MIPS_CMP_SWAP,
887 [TCG_COND_GT] = MIPS_CMP_SWAP,
888 [TCG_COND_GTU] = MIPS_CMP_SWAP,
891 static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
892 TCGReg arg1, TCGReg arg2)
894 MIPSInsn s_opc = OPC_SLTU;
900 tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
903 tcg_out_opc_imm(s, OPC_SLTIU, ret, arg1, 1);
908 tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
911 tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, arg1);
925 cmp_map = mips_cmp_map[cond];
926 if (cmp_map & MIPS_CMP_SWAP) {
931 tcg_out_opc_reg(s, s_opc, ret, arg1, arg2);
932 if (cmp_map & MIPS_CMP_INV) {
933 tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
938 g_assert_not_reached();
943 static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
944 TCGReg arg2, TCGLabel *l)
946 static const MIPSInsn b_zero[16] = {
947 [TCG_COND_LT] = OPC_BLTZ,
948 [TCG_COND_GT] = OPC_BGTZ,
949 [TCG_COND_LE] = OPC_BLEZ,
950 [TCG_COND_GE] = OPC_BGEZ,
953 MIPSInsn s_opc = OPC_SLTU;
970 b_opc = b_zero[cond];
982 cmp_map = mips_cmp_map[cond];
983 if (cmp_map & MIPS_CMP_SWAP) {
988 tcg_out_opc_reg(s, s_opc, TCG_TMP0, arg1, arg2);
989 b_opc = (cmp_map & MIPS_CMP_INV ? OPC_BEQ : OPC_BNE);
995 g_assert_not_reached();
999 tcg_out_opc_br(s, b_opc, arg1, arg2);
1000 tcg_out_reloc(s, s->code_ptr - 1, R_MIPS_PC16, l, 0);
1004 static TCGReg tcg_out_reduce_eq2(TCGContext *s, TCGReg tmp0, TCGReg tmp1,
1005 TCGReg al, TCGReg ah,
1006 TCGReg bl, TCGReg bh)
1008 /* Merge highpart comparison into AH. */
1011 tcg_out_opc_reg(s, OPC_XOR, tmp0, ah, bh);
1017 /* Merge lowpart comparison into AL. */
1020 tcg_out_opc_reg(s, OPC_XOR, tmp1, al, bl);
1026 /* Merge high and low part comparisons into AL. */
1029 tcg_out_opc_reg(s, OPC_OR, tmp0, ah, al);
1038 static void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
1039 TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
1041 TCGReg tmp0 = TCG_TMP0;
1044 tcg_debug_assert(ret != TCG_TMP0);
1045 if (ret == ah || ret == bh) {
1046 tcg_debug_assert(ret != TCG_TMP1);
1053 tmp1 = tcg_out_reduce_eq2(s, tmp0, tmp1, al, ah, bl, bh);
1054 tcg_out_setcond(s, cond, ret, tmp1, TCG_REG_ZERO);
1058 tcg_out_setcond(s, TCG_COND_EQ, tmp0, ah, bh);
1059 tcg_out_setcond(s, tcg_unsigned_cond(cond), tmp1, al, bl);
1060 tcg_out_opc_reg(s, OPC_AND, tmp1, tmp1, tmp0);
1061 tcg_out_setcond(s, tcg_high_cond(cond), tmp0, ah, bh);
1062 tcg_out_opc_reg(s, OPC_OR, ret, tmp1, tmp0);
1067 static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
1068 TCGReg bl, TCGReg bh, TCGLabel *l)
1070 TCGCond b_cond = TCG_COND_NE;
1071 TCGReg tmp = TCG_TMP1;
1073 /* With branches, we emit between 4 and 9 insns with 2 or 3 branches.
1074 With setcond, we emit between 3 and 10 insns and only 1 branch,
1075 which ought to get better branch prediction. */
1080 tmp = tcg_out_reduce_eq2(s, TCG_TMP0, TCG_TMP1, al, ah, bl, bh);
1084 /* Minimize code size by preferring a compare not requiring INV. */
1085 if (mips_cmp_map[cond] & MIPS_CMP_INV) {
1086 cond = tcg_invert_cond(cond);
1087 b_cond = TCG_COND_EQ;
1089 tcg_out_setcond2(s, cond, tmp, al, ah, bl, bh);
1093 tcg_out_brcond(s, b_cond, tmp, TCG_REG_ZERO, l);
1096 static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
1097 TCGReg c1, TCGReg c2, TCGReg v1, TCGReg v2)
1101 /* If one of the values is zero, put it last to match SEL*Z instructions */
1102 if (use_mips32r6_instructions && v1 == 0) {
1105 cond = tcg_invert_cond(cond);
1114 tcg_out_opc_reg(s, OPC_XOR, TCG_TMP0, c1, c2);
1120 /* Minimize code size by preferring a compare not requiring INV. */
1121 if (mips_cmp_map[cond] & MIPS_CMP_INV) {
1122 cond = tcg_invert_cond(cond);
1125 tcg_out_setcond(s, cond, TCG_TMP0, c1, c2);
1130 if (use_mips32r6_instructions) {
1131 MIPSInsn m_opc_t = eqz ? OPC_SELEQZ : OPC_SELNEZ;
1132 MIPSInsn m_opc_f = eqz ? OPC_SELNEZ : OPC_SELEQZ;
1135 tcg_out_opc_reg(s, m_opc_f, TCG_TMP1, v2, c1);
1137 tcg_out_opc_reg(s, m_opc_t, ret, v1, c1);
1139 tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP1);
1142 MIPSInsn m_opc = eqz ? OPC_MOVZ : OPC_MOVN;
1144 tcg_out_opc_reg(s, m_opc, ret, v1, c1);
1146 /* This should be guaranteed via constraints */
1147 tcg_debug_assert(v2 == ret);
1151 static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
1154 * Note that __mips_abicalls requires the called function's address
1155 * to be loaded into $25 (t9), even if a direct branch is in range.
1157 * For n64, always drop the pointer into the constant pool.
1158 * We can re-use helper addresses often and do not want any
1159 * of the longer sequences tcg_out_movi may try.
1161 if (sizeof(uintptr_t) == 8) {
1162 tcg_out_movi_pool(s, TCG_REG_T9, (uintptr_t)arg, TCG_REG_TB);
1164 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T9, (uintptr_t)arg);
1167 /* But do try a direct branch, allowing the cpu better insn prefetch. */
1169 if (!tcg_out_opc_jmp(s, OPC_J, arg)) {
1170 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_T9, 0);
1173 if (!tcg_out_opc_jmp(s, OPC_JAL, arg)) {
1174 tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_REG_T9, 0);
1179 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
1180 const TCGHelperInfo *info)
1182 tcg_out_call_int(s, arg, false);
1186 /* We have four temps, we might as well expose three of them. */
1187 static const TCGLdstHelperParam ldst_helper_param = {
1188 .ntmp = 3, .tmp = { TCG_TMP0, TCG_TMP1, TCG_TMP2 }
1191 static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1193 const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
1194 MemOp opc = get_memop(l->oi);
1196 /* resolve label address */
1197 if (!reloc_pc16(l->label_ptr[0], tgt_rx)
1198 || (l->label_ptr[1] && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
1202 tcg_out_ld_helper_args(s, l, &ldst_helper_param);
1204 tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false);
1208 tcg_out_ld_helper_ret(s, l, true, &ldst_helper_param);
1210 tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1211 if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1220 static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1222 const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
1223 MemOp opc = get_memop(l->oi);
1225 /* resolve label address */
1226 if (!reloc_pc16(l->label_ptr[0], tgt_rx)
1227 || (l->label_ptr[1] && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
1231 tcg_out_st_helper_args(s, l, &ldst_helper_param);
1233 tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
1237 tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1238 if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1252 bool tcg_target_has_memory_bswap(MemOp memop)
1257 /* We expect to use a 16-bit negative offset from ENV. */
1258 #define MIN_TLB_MASK_TABLE_OFS -32768
1261 * For softmmu, perform the TLB load and compare.
1262 * For useronly, perform any required alignment tests.
1263 * In both cases, return a TCGLabelQemuLdst structure if the slow path
1264 * is required and fill in @h with the host address for the fast path.
1266 static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1267 TCGReg addrlo, TCGReg addrhi,
1268 MemOpIdx oi, bool is_ld)
1270 TCGType addr_type = s->addr_type;
1271 TCGLabelQemuLdst *ldst = NULL;
1272 MemOp opc = get_memop(oi);
1274 unsigned s_bits = opc & MO_SIZE;
1278 h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, false);
1279 a_bits = h->aa.align;
1280 a_mask = (1 << a_bits) - 1;
1282 #ifdef CONFIG_SOFTMMU
1283 unsigned s_mask = (1 << s_bits) - 1;
1284 int mem_index = get_mmuidx(oi);
1285 int fast_off = tlb_mask_table_ofs(s, mem_index);
1286 int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1287 int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1288 int add_off = offsetof(CPUTLBEntry, addend);
1289 int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read)
1290 : offsetof(CPUTLBEntry, addr_write);
1292 ldst = new_ldst_label(s);
1293 ldst->is_ld = is_ld;
1295 ldst->addrlo_reg = addrlo;
1296 ldst->addrhi_reg = addrhi;
1298 /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx]. */
1299 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_AREG0, mask_off);
1300 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP1, TCG_AREG0, table_off);
1302 /* Extract the TLB index from the address into TMP3. */
1303 if (TCG_TARGET_REG_BITS == 32 || addr_type == TCG_TYPE_I32) {
1304 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, addrlo,
1305 s->page_bits - CPU_TLB_ENTRY_BITS);
1307 tcg_out_dsrl(s, TCG_TMP3, addrlo,
1308 s->page_bits - CPU_TLB_ENTRY_BITS);
1310 tcg_out_opc_reg(s, OPC_AND, TCG_TMP3, TCG_TMP3, TCG_TMP0);
1312 /* Add the tlb_table pointer, creating the CPUTLBEntry address in TMP3. */
1313 tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP3, TCG_TMP3, TCG_TMP1);
1315 if (TCG_TARGET_REG_BITS == 32 || addr_type == TCG_TYPE_I32) {
1316 /* Load the (low half) tlb comparator. */
1317 tcg_out_ld(s, TCG_TYPE_I32, TCG_TMP0, TCG_TMP3,
1318 cmp_off + HOST_BIG_ENDIAN * 4);
1320 tcg_out_ld(s, TCG_TYPE_I64, TCG_TMP0, TCG_TMP3, cmp_off);
1323 if (TCG_TARGET_REG_BITS == 64 || addr_type == TCG_TYPE_I32) {
1324 /* Load the tlb addend for the fast path. */
1325 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off);
1329 * Mask the page bits, keeping the alignment bits to compare against.
1330 * For unaligned accesses, compare against the end of the access to
1331 * verify that it does not cross a page boundary.
1333 tcg_out_movi(s, addr_type, TCG_TMP1, s->page_mask | a_mask);
1334 if (a_mask < s_mask) {
1335 if (TCG_TARGET_REG_BITS == 32 || addr_type == TCG_TYPE_I32) {
1336 tcg_out_opc_imm(s, OPC_ADDIU, TCG_TMP2, addrlo, s_mask - a_mask);
1338 tcg_out_opc_imm(s, OPC_DADDIU, TCG_TMP2, addrlo, s_mask - a_mask);
1340 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP2);
1342 tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, addrlo);
1345 /* Zero extend a 32-bit guest address for a 64-bit host. */
1346 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) {
1347 tcg_out_ext32u(s, TCG_TMP2, addrlo);
1351 ldst->label_ptr[0] = s->code_ptr;
1352 tcg_out_opc_br(s, OPC_BNE, TCG_TMP1, TCG_TMP0);
1354 /* Load and test the high half tlb comparator. */
1355 if (TCG_TARGET_REG_BITS == 32 && addr_type != TCG_TYPE_I32) {
1357 tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + HI_OFF);
1359 /* Load the tlb addend for the fast path. */
1360 tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off);
1362 ldst->label_ptr[1] = s->code_ptr;
1363 tcg_out_opc_br(s, OPC_BNE, addrhi, TCG_TMP0);
1368 tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_TMP3, addrlo);
1370 if (a_mask && (use_mips32r6_instructions || a_bits != s_bits)) {
1371 ldst = new_ldst_label(s);
1373 ldst->is_ld = is_ld;
1375 ldst->addrlo_reg = addrlo;
1376 ldst->addrhi_reg = addrhi;
1378 /* We are expecting a_bits to max out at 7, much lower than ANDI. */
1379 tcg_debug_assert(a_bits < 16);
1380 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, addrlo, a_mask);
1382 ldst->label_ptr[0] = s->code_ptr;
1383 if (use_mips32r6_instructions) {
1384 tcg_out_opc_br(s, OPC_BNEZALC_R6, TCG_REG_ZERO, TCG_TMP0);
1386 tcg_out_opc_br(s, OPC_BNEL, TCG_TMP0, TCG_REG_ZERO);
1392 if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) {
1393 tcg_out_ext32u(s, TCG_REG_A0, base);
1397 if (guest_base == (int16_t)guest_base) {
1398 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_A0, base, guest_base);
1400 tcg_out_opc_reg(s, ALIAS_PADD, TCG_REG_A0, base,
1401 TCG_GUEST_BASE_REG);
1411 static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1412 TCGReg base, MemOp opc, TCGType type)
1414 switch (opc & MO_SSIZE) {
1416 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1419 tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1422 tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1425 tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1428 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) {
1429 tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1434 tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1437 /* Prefer to load from offset 0 first, but allow for overlap. */
1438 if (TCG_TARGET_REG_BITS == 64) {
1439 tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1440 } else if (HOST_BIG_ENDIAN ? hi != base : lo == base) {
1441 tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1442 tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1444 tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1445 tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1449 g_assert_not_reached();
1453 static void tcg_out_qemu_ld_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
1454 TCGReg base, MemOp opc, TCGType type)
1456 const MIPSInsn lw1 = HOST_BIG_ENDIAN ? OPC_LWL : OPC_LWR;
1457 const MIPSInsn lw2 = HOST_BIG_ENDIAN ? OPC_LWR : OPC_LWL;
1458 const MIPSInsn ld1 = HOST_BIG_ENDIAN ? OPC_LDL : OPC_LDR;
1459 const MIPSInsn ld2 = HOST_BIG_ENDIAN ? OPC_LDR : OPC_LDL;
1460 bool sgn = opc & MO_SIGN;
1462 switch (opc & MO_SIZE) {
1464 if (HOST_BIG_ENDIAN) {
1465 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 0);
1466 tcg_out_opc_imm(s, OPC_LBU, lo, base, 1);
1467 if (use_mips32r2_instructions) {
1468 tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
1470 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP0, TCG_TMP0, 8);
1471 tcg_out_opc_reg(s, OPC_OR, lo, lo, TCG_TMP0);
1473 } else if (use_mips32r2_instructions && lo != base) {
1474 tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1475 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 1);
1476 tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
1478 tcg_out_opc_imm(s, OPC_LBU, TCG_TMP0, base, 0);
1479 tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP1, base, 1);
1480 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP1, TCG_TMP1, 8);
1481 tcg_out_opc_reg(s, OPC_OR, lo, TCG_TMP0, TCG_TMP1);
1486 tcg_out_opc_imm(s, lw1, lo, base, 0);
1487 tcg_out_opc_imm(s, lw2, lo, base, 3);
1488 if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64 && !sgn) {
1489 tcg_out_ext32u(s, lo, lo);
1494 if (TCG_TARGET_REG_BITS == 64) {
1495 tcg_out_opc_imm(s, ld1, lo, base, 0);
1496 tcg_out_opc_imm(s, ld2, lo, base, 7);
1498 tcg_out_opc_imm(s, lw1, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 0);
1499 tcg_out_opc_imm(s, lw2, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 3);
1500 tcg_out_opc_imm(s, lw1, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 0);
1501 tcg_out_opc_imm(s, lw2, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 3);
1506 g_assert_not_reached();
1510 static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi,
1511 TCGReg addrlo, TCGReg addrhi,
1512 MemOpIdx oi, TCGType data_type)
1514 MemOp opc = get_memop(oi);
1515 TCGLabelQemuLdst *ldst;
1518 ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, true);
1520 if (use_mips32r6_instructions || h.aa.align >= (opc & MO_SIZE)) {
1521 tcg_out_qemu_ld_direct(s, datalo, datahi, h.base, opc, data_type);
1523 tcg_out_qemu_ld_unalign(s, datalo, datahi, h.base, opc, data_type);
1527 ldst->type = data_type;
1528 ldst->datalo_reg = datalo;
1529 ldst->datahi_reg = datahi;
1530 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1534 static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1535 TCGReg base, MemOp opc)
1537 switch (opc & MO_SIZE) {
1539 tcg_out_opc_imm(s, OPC_SB, lo, base, 0);
1542 tcg_out_opc_imm(s, OPC_SH, lo, base, 0);
1545 tcg_out_opc_imm(s, OPC_SW, lo, base, 0);
1548 if (TCG_TARGET_REG_BITS == 64) {
1549 tcg_out_opc_imm(s, OPC_SD, lo, base, 0);
1551 tcg_out_opc_imm(s, OPC_SW, HOST_BIG_ENDIAN ? hi : lo, base, 0);
1552 tcg_out_opc_imm(s, OPC_SW, HOST_BIG_ENDIAN ? lo : hi, base, 4);
1556 g_assert_not_reached();
1560 static void tcg_out_qemu_st_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
1561 TCGReg base, MemOp opc)
1563 const MIPSInsn sw1 = HOST_BIG_ENDIAN ? OPC_SWL : OPC_SWR;
1564 const MIPSInsn sw2 = HOST_BIG_ENDIAN ? OPC_SWR : OPC_SWL;
1565 const MIPSInsn sd1 = HOST_BIG_ENDIAN ? OPC_SDL : OPC_SDR;
1566 const MIPSInsn sd2 = HOST_BIG_ENDIAN ? OPC_SDR : OPC_SDL;
1568 switch (opc & MO_SIZE) {
1570 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, lo, 8);
1571 tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? TCG_TMP0 : lo, base, 0);
1572 tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? lo : TCG_TMP0, base, 1);
1576 tcg_out_opc_imm(s, sw1, lo, base, 0);
1577 tcg_out_opc_imm(s, sw2, lo, base, 3);
1581 if (TCG_TARGET_REG_BITS == 64) {
1582 tcg_out_opc_imm(s, sd1, lo, base, 0);
1583 tcg_out_opc_imm(s, sd2, lo, base, 7);
1585 tcg_out_opc_imm(s, sw1, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 0);
1586 tcg_out_opc_imm(s, sw2, HOST_BIG_ENDIAN ? hi : lo, base, 0 + 3);
1587 tcg_out_opc_imm(s, sw1, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 0);
1588 tcg_out_opc_imm(s, sw2, HOST_BIG_ENDIAN ? lo : hi, base, 4 + 3);
1593 g_assert_not_reached();
1597 static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi,
1598 TCGReg addrlo, TCGReg addrhi,
1599 MemOpIdx oi, TCGType data_type)
1601 MemOp opc = get_memop(oi);
1602 TCGLabelQemuLdst *ldst;
1605 ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, false);
1607 if (use_mips32r6_instructions || h.aa.align >= (opc & MO_SIZE)) {
1608 tcg_out_qemu_st_direct(s, datalo, datahi, h.base, opc);
1610 tcg_out_qemu_st_unalign(s, datalo, datahi, h.base, opc);
1614 ldst->type = data_type;
1615 ldst->datalo_reg = datalo;
1616 ldst->datahi_reg = datahi;
1617 ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1621 static void tcg_out_mb(TCGContext *s, TCGArg a0)
1623 static const MIPSInsn sync[] = {
1624 /* Note that SYNC_MB is a slightly weaker than SYNC 0,
1625 as the former is an ordering barrier and the latter
1626 is a completion barrier. */
1627 [0 ... TCG_MO_ALL] = OPC_SYNC_MB,
1628 [TCG_MO_LD_LD] = OPC_SYNC_RMB,
1629 [TCG_MO_ST_ST] = OPC_SYNC_WMB,
1630 [TCG_MO_LD_ST] = OPC_SYNC_RELEASE,
1631 [TCG_MO_LD_ST | TCG_MO_ST_ST] = OPC_SYNC_RELEASE,
1632 [TCG_MO_LD_ST | TCG_MO_LD_LD] = OPC_SYNC_ACQUIRE,
1634 tcg_out32(s, sync[a0 & TCG_MO_ALL]);
1637 static void tcg_out_clz(TCGContext *s, MIPSInsn opcv2, MIPSInsn opcv6,
1638 int width, TCGReg a0, TCGReg a1, TCGArg a2)
1640 if (use_mips32r6_instructions) {
1642 tcg_out_opc_reg(s, opcv6, a0, a1, 0);
1644 tcg_out_opc_reg(s, opcv6, TCG_TMP0, a1, 0);
1645 tcg_out_movcond(s, TCG_COND_EQ, a0, a1, 0, a2, TCG_TMP0);
1649 tcg_out_opc_reg(s, opcv2, a0, a1, a1);
1650 } else if (a0 == a2) {
1651 tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
1652 tcg_out_opc_reg(s, OPC_MOVN, a0, TCG_TMP0, a1);
1653 } else if (a0 != a1) {
1654 tcg_out_opc_reg(s, opcv2, a0, a1, a1);
1655 tcg_out_opc_reg(s, OPC_MOVZ, a0, a2, a1);
1657 tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
1658 tcg_out_opc_reg(s, OPC_MOVZ, TCG_TMP0, a2, a1);
1659 tcg_out_mov(s, TCG_TYPE_REG, a0, TCG_TMP0);
1664 static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1666 TCGReg base = TCG_REG_ZERO;
1671 if (TCG_TARGET_REG_BITS == 64) {
1672 ofs = tcg_tbrel_diff(s, (void *)a0);
1678 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo);
1679 tcg_out_opc_reg(s, ALIAS_PADD, base, base, TCG_REG_TB);
1685 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo);
1688 if (!tcg_out_opc_jmp(s, OPC_J, tb_ret_addr)) {
1689 tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)tb_ret_addr);
1690 tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0);
1693 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_V0, base, lo);
1696 static void tcg_out_goto_tb(TCGContext *s, int which)
1698 intptr_t ofs = get_jmp_target_addr(s, which);
1701 /* indirect jump method */
1702 if (TCG_TARGET_REG_BITS == 64) {
1705 ofs = tcg_tbrel_diff(s, (void *)ofs);
1708 base = TCG_REG_ZERO;
1710 tcg_out_ld(s, TCG_TYPE_PTR, dest, base, ofs);
1711 tcg_out_opc_reg(s, OPC_JR, 0, dest, 0);
1715 set_jmp_reset_offset(s, which);
1716 if (TCG_TARGET_REG_BITS == 64) {
1717 /* For the unlinked case, need to reset TCG_REG_TB. */
1718 tcg_out_ldst(s, ALIAS_PADDI, TCG_REG_TB, TCG_REG_TB,
1719 -tcg_current_code_size(s));
1723 void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1724 uintptr_t jmp_rx, uintptr_t jmp_rw)
1726 /* Always indirect, nothing to do */
1729 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1730 const TCGArg args[TCG_MAX_OP_ARGS],
1731 const int const_args[TCG_MAX_OP_ARGS])
1738 * Note that many operands use the constraint set "rZ".
1739 * We make use of the fact that 0 is the ZERO register,
1740 * and hence such cases need not check for const_args.
1748 case INDEX_op_goto_ptr:
1749 /* jmp to the given host address (could be epilogue) */
1750 tcg_out_opc_reg(s, OPC_JR, 0, a0, 0);
1751 if (TCG_TARGET_REG_BITS == 64) {
1752 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, a0);
1758 tcg_out_brcond(s, TCG_COND_EQ, TCG_REG_ZERO, TCG_REG_ZERO,
1762 case INDEX_op_ld8u_i32:
1763 case INDEX_op_ld8u_i64:
1766 case INDEX_op_ld8s_i32:
1767 case INDEX_op_ld8s_i64:
1770 case INDEX_op_ld16u_i32:
1771 case INDEX_op_ld16u_i64:
1774 case INDEX_op_ld16s_i32:
1775 case INDEX_op_ld16s_i64:
1778 case INDEX_op_ld_i32:
1779 case INDEX_op_ld32s_i64:
1782 case INDEX_op_ld32u_i64:
1785 case INDEX_op_ld_i64:
1788 case INDEX_op_st8_i32:
1789 case INDEX_op_st8_i64:
1792 case INDEX_op_st16_i32:
1793 case INDEX_op_st16_i64:
1796 case INDEX_op_st_i32:
1797 case INDEX_op_st32_i64:
1800 case INDEX_op_st_i64:
1803 tcg_out_ldst(s, i1, a0, a1, a2);
1806 case INDEX_op_add_i32:
1807 i1 = OPC_ADDU, i2 = OPC_ADDIU;
1809 case INDEX_op_add_i64:
1810 i1 = OPC_DADDU, i2 = OPC_DADDIU;
1812 case INDEX_op_or_i32:
1813 case INDEX_op_or_i64:
1814 i1 = OPC_OR, i2 = OPC_ORI;
1816 case INDEX_op_xor_i32:
1817 case INDEX_op_xor_i64:
1818 i1 = OPC_XOR, i2 = OPC_XORI;
1821 tcg_out_opc_imm(s, i2, a0, a1, a2);
1825 tcg_out_opc_reg(s, i1, a0, a1, a2);
1828 case INDEX_op_sub_i32:
1829 i1 = OPC_SUBU, i2 = OPC_ADDIU;
1831 case INDEX_op_sub_i64:
1832 i1 = OPC_DSUBU, i2 = OPC_DADDIU;
1835 tcg_out_opc_imm(s, i2, a0, a1, -a2);
1839 case INDEX_op_and_i32:
1840 if (c2 && a2 != (uint16_t)a2) {
1841 int msb = ctz32(~a2) - 1;
1842 tcg_debug_assert(use_mips32r2_instructions);
1843 tcg_debug_assert(is_p2m1(a2));
1844 tcg_out_opc_bf(s, OPC_EXT, a0, a1, msb, 0);
1847 i1 = OPC_AND, i2 = OPC_ANDI;
1849 case INDEX_op_and_i64:
1850 if (c2 && a2 != (uint16_t)a2) {
1851 int msb = ctz64(~a2) - 1;
1852 tcg_debug_assert(use_mips32r2_instructions);
1853 tcg_debug_assert(is_p2m1(a2));
1854 tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1, msb, 0);
1857 i1 = OPC_AND, i2 = OPC_ANDI;
1859 case INDEX_op_nor_i32:
1860 case INDEX_op_nor_i64:
1864 case INDEX_op_mul_i32:
1865 if (use_mips32_instructions) {
1866 tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1869 i1 = OPC_MULT, i2 = OPC_MFLO;
1871 case INDEX_op_mulsh_i32:
1872 if (use_mips32r6_instructions) {
1873 tcg_out_opc_reg(s, OPC_MUH, a0, a1, a2);
1876 i1 = OPC_MULT, i2 = OPC_MFHI;
1878 case INDEX_op_muluh_i32:
1879 if (use_mips32r6_instructions) {
1880 tcg_out_opc_reg(s, OPC_MUHU, a0, a1, a2);
1883 i1 = OPC_MULTU, i2 = OPC_MFHI;
1885 case INDEX_op_div_i32:
1886 if (use_mips32r6_instructions) {
1887 tcg_out_opc_reg(s, OPC_DIV_R6, a0, a1, a2);
1890 i1 = OPC_DIV, i2 = OPC_MFLO;
1892 case INDEX_op_divu_i32:
1893 if (use_mips32r6_instructions) {
1894 tcg_out_opc_reg(s, OPC_DIVU_R6, a0, a1, a2);
1897 i1 = OPC_DIVU, i2 = OPC_MFLO;
1899 case INDEX_op_rem_i32:
1900 if (use_mips32r6_instructions) {
1901 tcg_out_opc_reg(s, OPC_MOD, a0, a1, a2);
1904 i1 = OPC_DIV, i2 = OPC_MFHI;
1906 case INDEX_op_remu_i32:
1907 if (use_mips32r6_instructions) {
1908 tcg_out_opc_reg(s, OPC_MODU, a0, a1, a2);
1911 i1 = OPC_DIVU, i2 = OPC_MFHI;
1913 case INDEX_op_mul_i64:
1914 if (use_mips32r6_instructions) {
1915 tcg_out_opc_reg(s, OPC_DMUL, a0, a1, a2);
1918 i1 = OPC_DMULT, i2 = OPC_MFLO;
1920 case INDEX_op_mulsh_i64:
1921 if (use_mips32r6_instructions) {
1922 tcg_out_opc_reg(s, OPC_DMUH, a0, a1, a2);
1925 i1 = OPC_DMULT, i2 = OPC_MFHI;
1927 case INDEX_op_muluh_i64:
1928 if (use_mips32r6_instructions) {
1929 tcg_out_opc_reg(s, OPC_DMUHU, a0, a1, a2);
1932 i1 = OPC_DMULTU, i2 = OPC_MFHI;
1934 case INDEX_op_div_i64:
1935 if (use_mips32r6_instructions) {
1936 tcg_out_opc_reg(s, OPC_DDIV_R6, a0, a1, a2);
1939 i1 = OPC_DDIV, i2 = OPC_MFLO;
1941 case INDEX_op_divu_i64:
1942 if (use_mips32r6_instructions) {
1943 tcg_out_opc_reg(s, OPC_DDIVU_R6, a0, a1, a2);
1946 i1 = OPC_DDIVU, i2 = OPC_MFLO;
1948 case INDEX_op_rem_i64:
1949 if (use_mips32r6_instructions) {
1950 tcg_out_opc_reg(s, OPC_DMOD, a0, a1, a2);
1953 i1 = OPC_DDIV, i2 = OPC_MFHI;
1955 case INDEX_op_remu_i64:
1956 if (use_mips32r6_instructions) {
1957 tcg_out_opc_reg(s, OPC_DMODU, a0, a1, a2);
1960 i1 = OPC_DDIVU, i2 = OPC_MFHI;
1962 tcg_out_opc_reg(s, i1, 0, a1, a2);
1963 tcg_out_opc_reg(s, i2, a0, 0, 0);
1966 case INDEX_op_muls2_i32:
1969 case INDEX_op_mulu2_i32:
1972 case INDEX_op_muls2_i64:
1975 case INDEX_op_mulu2_i64:
1978 tcg_out_opc_reg(s, i1, 0, a2, args[3]);
1979 tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0);
1980 tcg_out_opc_reg(s, OPC_MFHI, a1, 0, 0);
1983 case INDEX_op_not_i32:
1984 case INDEX_op_not_i64:
1988 tcg_out_opc_reg(s, i1, a0, TCG_REG_ZERO, a1);
1991 case INDEX_op_bswap16_i32:
1992 case INDEX_op_bswap16_i64:
1993 tcg_out_bswap16(s, a0, a1, a2);
1995 case INDEX_op_bswap32_i32:
1996 tcg_out_bswap32(s, a0, a1, 0);
1998 case INDEX_op_bswap32_i64:
1999 tcg_out_bswap32(s, a0, a1, a2);
2001 case INDEX_op_bswap64_i64:
2002 tcg_out_bswap64(s, a0, a1);
2004 case INDEX_op_extrh_i64_i32:
2005 tcg_out_dsra(s, a0, a1, 32);
2008 case INDEX_op_sar_i32:
2009 i1 = OPC_SRAV, i2 = OPC_SRA;
2011 case INDEX_op_shl_i32:
2012 i1 = OPC_SLLV, i2 = OPC_SLL;
2014 case INDEX_op_shr_i32:
2015 i1 = OPC_SRLV, i2 = OPC_SRL;
2017 case INDEX_op_rotr_i32:
2018 i1 = OPC_ROTRV, i2 = OPC_ROTR;
2021 tcg_out_opc_sa(s, i2, a0, a1, a2);
2025 tcg_out_opc_reg(s, i1, a0, a2, a1);
2027 case INDEX_op_rotl_i32:
2029 tcg_out_opc_sa(s, OPC_ROTR, a0, a1, 32 - a2);
2031 tcg_out_opc_reg(s, OPC_SUBU, TCG_TMP0, TCG_REG_ZERO, a2);
2032 tcg_out_opc_reg(s, OPC_ROTRV, a0, TCG_TMP0, a1);
2035 case INDEX_op_sar_i64:
2037 tcg_out_dsra(s, a0, a1, a2);
2042 case INDEX_op_shl_i64:
2044 tcg_out_dsll(s, a0, a1, a2);
2049 case INDEX_op_shr_i64:
2051 tcg_out_dsrl(s, a0, a1, a2);
2056 case INDEX_op_rotr_i64:
2058 tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, a2);
2063 case INDEX_op_rotl_i64:
2065 tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, 64 - a2);
2067 tcg_out_opc_reg(s, OPC_DSUBU, TCG_TMP0, TCG_REG_ZERO, a2);
2068 tcg_out_opc_reg(s, OPC_DROTRV, a0, TCG_TMP0, a1);
2072 case INDEX_op_clz_i32:
2073 tcg_out_clz(s, OPC_CLZ, OPC_CLZ_R6, 32, a0, a1, a2);
2075 case INDEX_op_clz_i64:
2076 tcg_out_clz(s, OPC_DCLZ, OPC_DCLZ_R6, 64, a0, a1, a2);
2079 case INDEX_op_deposit_i32:
2080 tcg_out_opc_bf(s, OPC_INS, a0, a2, args[3] + args[4] - 1, args[3]);
2082 case INDEX_op_deposit_i64:
2083 tcg_out_opc_bf64(s, OPC_DINS, OPC_DINSM, OPC_DINSU, a0, a2,
2084 args[3] + args[4] - 1, args[3]);
2086 case INDEX_op_extract_i32:
2087 tcg_out_opc_bf(s, OPC_EXT, a0, a1, args[3] - 1, a2);
2089 case INDEX_op_extract_i64:
2090 tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1,
2094 case INDEX_op_brcond_i32:
2095 case INDEX_op_brcond_i64:
2096 tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
2098 case INDEX_op_brcond2_i32:
2099 tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
2102 case INDEX_op_movcond_i32:
2103 case INDEX_op_movcond_i64:
2104 tcg_out_movcond(s, args[5], a0, a1, a2, args[3], args[4]);
2107 case INDEX_op_setcond_i32:
2108 case INDEX_op_setcond_i64:
2109 tcg_out_setcond(s, args[3], a0, a1, a2);
2111 case INDEX_op_setcond2_i32:
2112 tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
2115 case INDEX_op_qemu_ld_a64_i32:
2116 if (TCG_TARGET_REG_BITS == 32) {
2117 tcg_out_qemu_ld(s, a0, 0, a1, a2, args[3], TCG_TYPE_I32);
2121 case INDEX_op_qemu_ld_a32_i32:
2122 tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2124 case INDEX_op_qemu_ld_a32_i64:
2125 if (TCG_TARGET_REG_BITS == 64) {
2126 tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2128 tcg_out_qemu_ld(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2131 case INDEX_op_qemu_ld_a64_i64:
2132 if (TCG_TARGET_REG_BITS == 64) {
2133 tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2135 tcg_out_qemu_ld(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2139 case INDEX_op_qemu_st_a64_i32:
2140 if (TCG_TARGET_REG_BITS == 32) {
2141 tcg_out_qemu_st(s, a0, 0, a1, a2, args[3], TCG_TYPE_I32);
2145 case INDEX_op_qemu_st_a32_i32:
2146 tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2148 case INDEX_op_qemu_st_a32_i64:
2149 if (TCG_TARGET_REG_BITS == 64) {
2150 tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2152 tcg_out_qemu_st(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2155 case INDEX_op_qemu_st_a64_i64:
2156 if (TCG_TARGET_REG_BITS == 64) {
2157 tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2159 tcg_out_qemu_st(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2163 case INDEX_op_add2_i32:
2164 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
2165 const_args[4], const_args[5], false);
2167 case INDEX_op_sub2_i32:
2168 tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
2169 const_args[4], const_args[5], true);
2175 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
2176 case INDEX_op_mov_i64:
2177 case INDEX_op_call: /* Always emitted via tcg_out_call. */
2178 case INDEX_op_exit_tb: /* Always emitted via tcg_out_exit_tb. */
2179 case INDEX_op_goto_tb: /* Always emitted via tcg_out_goto_tb. */
2180 case INDEX_op_ext8s_i32: /* Always emitted via tcg_reg_alloc_op. */
2181 case INDEX_op_ext8s_i64:
2182 case INDEX_op_ext8u_i32:
2183 case INDEX_op_ext8u_i64:
2184 case INDEX_op_ext16s_i32:
2185 case INDEX_op_ext16s_i64:
2186 case INDEX_op_ext32s_i64:
2187 case INDEX_op_ext32u_i64:
2188 case INDEX_op_ext_i32_i64:
2189 case INDEX_op_extu_i32_i64:
2190 case INDEX_op_extrl_i64_i32:
2192 g_assert_not_reached();
2196 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2199 case INDEX_op_goto_ptr:
2202 case INDEX_op_ld8u_i32:
2203 case INDEX_op_ld8s_i32:
2204 case INDEX_op_ld16u_i32:
2205 case INDEX_op_ld16s_i32:
2206 case INDEX_op_ld_i32:
2207 case INDEX_op_not_i32:
2208 case INDEX_op_bswap16_i32:
2209 case INDEX_op_bswap32_i32:
2210 case INDEX_op_ext8s_i32:
2211 case INDEX_op_ext16s_i32:
2212 case INDEX_op_extract_i32:
2213 case INDEX_op_ld8u_i64:
2214 case INDEX_op_ld8s_i64:
2215 case INDEX_op_ld16u_i64:
2216 case INDEX_op_ld16s_i64:
2217 case INDEX_op_ld32s_i64:
2218 case INDEX_op_ld32u_i64:
2219 case INDEX_op_ld_i64:
2220 case INDEX_op_not_i64:
2221 case INDEX_op_bswap16_i64:
2222 case INDEX_op_bswap32_i64:
2223 case INDEX_op_bswap64_i64:
2224 case INDEX_op_ext8s_i64:
2225 case INDEX_op_ext16s_i64:
2226 case INDEX_op_ext32s_i64:
2227 case INDEX_op_ext32u_i64:
2228 case INDEX_op_ext_i32_i64:
2229 case INDEX_op_extu_i32_i64:
2230 case INDEX_op_extrl_i64_i32:
2231 case INDEX_op_extrh_i64_i32:
2232 case INDEX_op_extract_i64:
2233 return C_O1_I1(r, r);
2235 case INDEX_op_st8_i32:
2236 case INDEX_op_st16_i32:
2237 case INDEX_op_st_i32:
2238 case INDEX_op_st8_i64:
2239 case INDEX_op_st16_i64:
2240 case INDEX_op_st32_i64:
2241 case INDEX_op_st_i64:
2242 return C_O0_I2(rZ, r);
2244 case INDEX_op_add_i32:
2245 case INDEX_op_add_i64:
2246 return C_O1_I2(r, r, rJ);
2247 case INDEX_op_sub_i32:
2248 case INDEX_op_sub_i64:
2249 return C_O1_I2(r, rZ, rN);
2250 case INDEX_op_mul_i32:
2251 case INDEX_op_mulsh_i32:
2252 case INDEX_op_muluh_i32:
2253 case INDEX_op_div_i32:
2254 case INDEX_op_divu_i32:
2255 case INDEX_op_rem_i32:
2256 case INDEX_op_remu_i32:
2257 case INDEX_op_nor_i32:
2258 case INDEX_op_setcond_i32:
2259 case INDEX_op_mul_i64:
2260 case INDEX_op_mulsh_i64:
2261 case INDEX_op_muluh_i64:
2262 case INDEX_op_div_i64:
2263 case INDEX_op_divu_i64:
2264 case INDEX_op_rem_i64:
2265 case INDEX_op_remu_i64:
2266 case INDEX_op_nor_i64:
2267 case INDEX_op_setcond_i64:
2268 return C_O1_I2(r, rZ, rZ);
2269 case INDEX_op_muls2_i32:
2270 case INDEX_op_mulu2_i32:
2271 case INDEX_op_muls2_i64:
2272 case INDEX_op_mulu2_i64:
2273 return C_O2_I2(r, r, r, r);
2274 case INDEX_op_and_i32:
2275 case INDEX_op_and_i64:
2276 return C_O1_I2(r, r, rIK);
2277 case INDEX_op_or_i32:
2278 case INDEX_op_xor_i32:
2279 case INDEX_op_or_i64:
2280 case INDEX_op_xor_i64:
2281 return C_O1_I2(r, r, rI);
2282 case INDEX_op_shl_i32:
2283 case INDEX_op_shr_i32:
2284 case INDEX_op_sar_i32:
2285 case INDEX_op_rotr_i32:
2286 case INDEX_op_rotl_i32:
2287 case INDEX_op_shl_i64:
2288 case INDEX_op_shr_i64:
2289 case INDEX_op_sar_i64:
2290 case INDEX_op_rotr_i64:
2291 case INDEX_op_rotl_i64:
2292 return C_O1_I2(r, r, ri);
2293 case INDEX_op_clz_i32:
2294 case INDEX_op_clz_i64:
2295 return C_O1_I2(r, r, rWZ);
2297 case INDEX_op_deposit_i32:
2298 case INDEX_op_deposit_i64:
2299 return C_O1_I2(r, 0, rZ);
2300 case INDEX_op_brcond_i32:
2301 case INDEX_op_brcond_i64:
2302 return C_O0_I2(rZ, rZ);
2303 case INDEX_op_movcond_i32:
2304 case INDEX_op_movcond_i64:
2305 return (use_mips32r6_instructions
2306 ? C_O1_I4(r, rZ, rZ, rZ, rZ)
2307 : C_O1_I4(r, rZ, rZ, rZ, 0));
2308 case INDEX_op_add2_i32:
2309 case INDEX_op_sub2_i32:
2310 return C_O2_I4(r, r, rZ, rZ, rN, rN);
2311 case INDEX_op_setcond2_i32:
2312 return C_O1_I4(r, rZ, rZ, rZ, rZ);
2313 case INDEX_op_brcond2_i32:
2314 return C_O0_I4(rZ, rZ, rZ, rZ);
2316 case INDEX_op_qemu_ld_a32_i32:
2317 return C_O1_I1(r, r);
2318 case INDEX_op_qemu_ld_a64_i32:
2319 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O1_I2(r, r, r);
2320 case INDEX_op_qemu_st_a32_i32:
2321 return C_O0_I2(rZ, r);
2322 case INDEX_op_qemu_st_a64_i32:
2323 return TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r) : C_O0_I3(rZ, r, r);
2324 case INDEX_op_qemu_ld_a32_i64:
2325 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O2_I1(r, r, r);
2326 case INDEX_op_qemu_ld_a64_i64:
2327 return TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r) : C_O2_I2(r, r, r, r);
2328 case INDEX_op_qemu_st_a32_i64:
2329 return TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r) : C_O0_I3(rZ, rZ, r);
2330 case INDEX_op_qemu_st_a64_i64:
2331 return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r)
2332 : C_O0_I4(rZ, rZ, r, r));
2335 g_assert_not_reached();
2339 static const int tcg_target_callee_save_regs[] = {
2346 TCG_REG_S6, /* used for the tb base (TCG_REG_TB) */
2347 TCG_REG_S7, /* used for guest_base */
2348 TCG_REG_S8, /* used for the global env (TCG_AREG0) */
2349 TCG_REG_RA, /* should be last for ABI compliance */
2352 /* The Linux kernel doesn't provide any information about the available
2353 instruction set. Probe it using a signal handler. */
2356 #ifndef use_movnz_instructions
2357 bool use_movnz_instructions = false;
2360 #ifndef use_mips32_instructions
2361 bool use_mips32_instructions = false;
2364 #ifndef use_mips32r2_instructions
2365 bool use_mips32r2_instructions = false;
2368 static volatile sig_atomic_t got_sigill;
2370 static void sigill_handler(int signo, siginfo_t *si, void *data)
2372 /* Skip the faulty instruction */
2373 ucontext_t *uc = (ucontext_t *)data;
2374 uc->uc_mcontext.pc += 4;
2379 static void tcg_target_detect_isa(void)
2381 struct sigaction sa_old, sa_new;
2383 memset(&sa_new, 0, sizeof(sa_new));
2384 sa_new.sa_flags = SA_SIGINFO;
2385 sa_new.sa_sigaction = sigill_handler;
2386 sigaction(SIGILL, &sa_new, &sa_old);
2388 /* Probe for movn/movz, necessary to implement movcond. */
2389 #ifndef use_movnz_instructions
2391 asm volatile(".set push\n"
2393 "movn $zero, $zero, $zero\n"
2394 "movz $zero, $zero, $zero\n"
2397 use_movnz_instructions = !got_sigill;
2400 /* Probe for MIPS32 instructions. As no subsetting is allowed
2401 by the specification, it is only necessary to probe for one
2402 of the instructions. */
2403 #ifndef use_mips32_instructions
2405 asm volatile(".set push\n"
2407 "mul $zero, $zero\n"
2410 use_mips32_instructions = !got_sigill;
2413 /* Probe for MIPS32r2 instructions if MIPS32 instructions are
2414 available. As no subsetting is allowed by the specification,
2415 it is only necessary to probe for one of the instructions. */
2416 #ifndef use_mips32r2_instructions
2417 if (use_mips32_instructions) {
2419 asm volatile(".set push\n"
2421 "seb $zero, $zero\n"
2424 use_mips32r2_instructions = !got_sigill;
2428 sigaction(SIGILL, &sa_old, NULL);
2431 static tcg_insn_unit *align_code_ptr(TCGContext *s)
2433 uintptr_t p = (uintptr_t)s->code_ptr;
2436 s->code_ptr = (void *)p;
2441 /* Stack frame parameters. */
2442 #define REG_SIZE (TCG_TARGET_REG_BITS / 8)
2443 #define SAVE_SIZE ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
2444 #define TEMP_SIZE (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2446 #define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
2447 + TCG_TARGET_STACK_ALIGN - 1) \
2448 & -TCG_TARGET_STACK_ALIGN)
2449 #define SAVE_OFS (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
2451 /* We're expecting to be able to use an immediate for frame allocation. */
2452 QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7fff);
2454 /* Generate global QEMU prologue and epilogue code */
2455 static void tcg_target_qemu_prologue(TCGContext *s)
2459 tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
2462 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
2463 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2464 tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2465 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2468 #ifndef CONFIG_SOFTMMU
2469 if (guest_base != (int16_t)guest_base) {
2471 * The function call abi for n32 and n64 will have loaded $25 (t9)
2472 * with the address of the prologue, so we can use that instead
2475 #if TCG_TARGET_REG_BITS == 64 && !defined(__mips_abicalls)
2476 # error "Unknown mips abi"
2478 tcg_out_movi_int(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base,
2479 TCG_TARGET_REG_BITS == 64 ? TCG_REG_T9 : 0);
2480 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2484 if (TCG_TARGET_REG_BITS == 64) {
2485 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, tcg_target_call_iarg_regs[1]);
2488 /* Call generated code */
2489 tcg_out_opc_reg(s, OPC_JR, 0, tcg_target_call_iarg_regs[1], 0);
2491 tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2494 * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
2495 * and fall through to the rest of the epilogue.
2497 tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2498 tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_V0, TCG_REG_ZERO);
2501 tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
2502 for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2503 tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2504 TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2507 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2509 tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
2511 if (use_mips32r2_instructions) {
2515 /* Bswap subroutines: Input in TCG_TMP0, output in TCG_TMP3;
2516 clobbers TCG_TMP1, TCG_TMP2. */
2519 * bswap32 -- 32-bit swap (signed result for mips64). a0 = abcd.
2521 bswap32_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2522 /* t3 = (ssss)d000 */
2523 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP3, TCG_TMP0, 24);
2525 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 24);
2527 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2529 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2531 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2533 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2535 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2537 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2538 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2539 /* t3 = dcba -- delay slot */
2540 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2542 if (TCG_TARGET_REG_BITS == 32) {
2547 * bswap32u -- unsigned 32-bit swap. a0 = ....abcd.
2549 bswap32u_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2550 /* t1 = (0000)000d */
2551 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP0, 0xff);
2553 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, TCG_TMP0, 24);
2554 /* t1 = (0000)d000 */
2555 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2557 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2559 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2561 tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2563 tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2565 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2567 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2568 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2569 /* t3 = dcba -- delay slot */
2570 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2573 * bswap64 -- 64-bit swap. a0 = abcdefgh
2575 bswap64_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2577 tcg_out_dsll(s, TCG_TMP3, TCG_TMP0, 56);
2579 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 56);
2582 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2584 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2586 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 40);
2588 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2590 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2593 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2595 tcg_out_dsrl(s, TCG_TMP2, TCG_TMP0, 32);
2597 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2600 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP2, 0xff00);
2602 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP2, 0x00ff);
2604 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 8);
2606 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 24);
2609 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2611 tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 16);
2613 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2616 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP1, 0x00ff);
2618 tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2620 tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2622 tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2625 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2626 tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2627 /* t3 = hgfedcba -- delay slot */
2628 tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2631 static void tcg_out_tb_start(TCGContext *s)
2636 static void tcg_target_init(TCGContext *s)
2638 tcg_target_detect_isa();
2639 tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
2640 if (TCG_TARGET_REG_BITS == 64) {
2641 tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
2644 tcg_target_call_clobber_regs = 0;
2645 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
2646 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
2647 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A0);
2648 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A1);
2649 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A2);
2650 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A3);
2651 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T0);
2652 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T1);
2653 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T2);
2654 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T3);
2655 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T4);
2656 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T5);
2657 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T6);
2658 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T7);
2659 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T8);
2660 tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T9);
2662 s->reserved_regs = 0;
2663 tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); /* zero register */
2664 tcg_regset_set_reg(s->reserved_regs, TCG_REG_K0); /* kernel use only */
2665 tcg_regset_set_reg(s->reserved_regs, TCG_REG_K1); /* kernel use only */
2666 tcg_regset_set_reg(s->reserved_regs, TCG_TMP0); /* internal use */
2667 tcg_regset_set_reg(s->reserved_regs, TCG_TMP1); /* internal use */
2668 tcg_regset_set_reg(s->reserved_regs, TCG_TMP2); /* internal use */
2669 tcg_regset_set_reg(s->reserved_regs, TCG_TMP3); /* internal use */
2670 tcg_regset_set_reg(s->reserved_regs, TCG_REG_RA); /* return address */
2671 tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP); /* stack pointer */
2672 tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP); /* global pointer */
2673 if (TCG_TARGET_REG_BITS == 64) {
2674 tcg_regset_set_reg(s->reserved_regs, TCG_REG_TB); /* tc->tc_ptr */
2680 uint8_t fde_def_cfa[4];
2681 uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
2684 #define ELF_HOST_MACHINE EM_MIPS
2685 /* GDB doesn't appear to require proper setting of ELF_HOST_FLAGS,
2686 which is good because they're really quite complicated for MIPS. */
2688 static const DebugFrame debug_frame = {
2689 .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
2692 .h.cie.code_align = 1,
2693 .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
2694 .h.cie.return_column = TCG_REG_RA,
2696 /* Total FDE size does not include the "len" member. */
2697 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2700 12, TCG_REG_SP, /* DW_CFA_def_cfa sp, ... */
2701 (FRAME_SIZE & 0x7f) | 0x80, /* ... uleb128 FRAME_SIZE */
2705 0x80 + 16, 9, /* DW_CFA_offset, s0, -72 */
2706 0x80 + 17, 8, /* DW_CFA_offset, s2, -64 */
2707 0x80 + 18, 7, /* DW_CFA_offset, s3, -56 */
2708 0x80 + 19, 6, /* DW_CFA_offset, s4, -48 */
2709 0x80 + 20, 5, /* DW_CFA_offset, s5, -40 */
2710 0x80 + 21, 4, /* DW_CFA_offset, s6, -32 */
2711 0x80 + 22, 3, /* DW_CFA_offset, s7, -24 */
2712 0x80 + 30, 2, /* DW_CFA_offset, s8, -16 */
2713 0x80 + 31, 1, /* DW_CFA_offset, ra, -8 */
2717 void tcg_register_jit(const void *buf, size_t buf_size)
2719 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));