tcg/mips: Split out tcg_out_movi_one
[qemu/ar7.git] / tcg / mips / tcg-target.c.inc
blobbbff510c464828b932982ee754be21a58a09386a
1 /*
2  * Tiny Code Generator for QEMU
3  *
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
7  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
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
24  * THE SOFTWARE.
25  */
27 #include "../tcg-ldst.c.inc"
29 #if HOST_BIG_ENDIAN
30 # define MIPS_BE  1
31 #else
32 # define MIPS_BE  0
33 #endif
35 #if TCG_TARGET_REG_BITS == 32
36 # define LO_OFF  (MIPS_BE * 4)
37 # define HI_OFF  (4 - LO_OFF)
38 #else
39 /* To assert at compile-time that these values are never used
40    for TCG_TARGET_REG_BITS == 64.  */
41 int link_error(void);
42 # define LO_OFF  link_error()
43 # define HI_OFF  link_error()
44 #endif
46 #ifdef CONFIG_DEBUG_TCG
47 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
48     "zero",
49     "at",
50     "v0",
51     "v1",
52     "a0",
53     "a1",
54     "a2",
55     "a3",
56     "t0",
57     "t1",
58     "t2",
59     "t3",
60     "t4",
61     "t5",
62     "t6",
63     "t7",
64     "s0",
65     "s1",
66     "s2",
67     "s3",
68     "s4",
69     "s5",
70     "s6",
71     "s7",
72     "t8",
73     "t9",
74     "k0",
75     "k1",
76     "gp",
77     "sp",
78     "s8",
79     "ra",
81 #endif
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
90 #endif
91 #if TCG_TARGET_REG_BITS == 64
92 #define TCG_REG_TB         TCG_REG_S6
93 #else
94 #define TCG_REG_TB         (qemu_build_not_reached(), TCG_REG_ZERO)
95 #endif
97 /* check if we really need so many registers :P */
98 static const int tcg_target_reg_alloc_order[] = {
99     /* Call saved registers.  */
100     TCG_REG_S0,
101     TCG_REG_S1,
102     TCG_REG_S2,
103     TCG_REG_S3,
104     TCG_REG_S4,
105     TCG_REG_S5,
106     TCG_REG_S6,
107     TCG_REG_S7,
108     TCG_REG_S8,
110     /* Call clobbered registers.  */
111     TCG_REG_T4,
112     TCG_REG_T5,
113     TCG_REG_T6,
114     TCG_REG_T7,
115     TCG_REG_T8,
116     TCG_REG_T9,
117     TCG_REG_V1,
118     TCG_REG_V0,
120     /* Argument registers, opposite order of allocation.  */
121     TCG_REG_T3,
122     TCG_REG_T2,
123     TCG_REG_T1,
124     TCG_REG_T0,
125     TCG_REG_A3,
126     TCG_REG_A2,
127     TCG_REG_A1,
128     TCG_REG_A0,
131 static const TCGReg tcg_target_call_iarg_regs[] = {
132     TCG_REG_A0,
133     TCG_REG_A1,
134     TCG_REG_A2,
135     TCG_REG_A3,
136 #if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
137     TCG_REG_T0,
138     TCG_REG_T1,
139     TCG_REG_T2,
140     TCG_REG_T3,
141 #endif
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);
163         return true;
164     }
165     return false;
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) {
194         return 1;
195     } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
196         return 1;
197     } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
198         return 1;
199     } else if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
200         return 1;
201     } else if ((ct & TCG_CT_CONST_N16) && val >= -32767 && val <= 32767) {
202         return 1;
203     } else if ((ct & TCG_CT_CONST_P2M1)
204                && use_mips32r2_instructions && is_p2m1(val)) {
205         return 1;
206     } else if ((ct & TCG_CT_CONST_WSZ)
207                && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
208         return 1;
209     }
210     return 0;
213 /* instruction opcodes */
214 typedef enum {
215     OPC_J        = 002 << 26,
216     OPC_JAL      = 003 << 26,
217     OPC_BEQ      = 004 << 26,
218     OPC_BNE      = 005 << 26,
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,
225     OPC_ORI      = 015 << 26,
226     OPC_XORI     = 016 << 26,
227     OPC_LUI      = 017 << 26,
228     OPC_BNEL     = 025 << 26,
229     OPC_BNEZALC_R6 = 030 << 26,
230     OPC_DADDIU   = 031 << 26,
231     OPC_LDL      = 032 << 26,
232     OPC_LDR      = 033 << 26,
233     OPC_LB       = 040 << 26,
234     OPC_LH       = 041 << 26,
235     OPC_LWL      = 042 << 26,
236     OPC_LW       = 043 << 26,
237     OPC_LBU      = 044 << 26,
238     OPC_LHU      = 045 << 26,
239     OPC_LWR      = 046 << 26,
240     OPC_LWU      = 047 << 26,
241     OPC_SB       = 050 << 26,
242     OPC_SH       = 051 << 26,
243     OPC_SWL      = 052 << 26,
244     OPC_SW       = 053 << 26,
245     OPC_SDL      = 054 << 26,
246     OPC_SDR      = 055 << 26,
247     OPC_SWR      = 056 << 26,
248     OPC_LD       = 067 << 26,
249     OPC_SD       = 077 << 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,
345     /*
346      * MIPS r6 replaces MUL with an alternative encoding which is
347      * backwards-compatible at the assembly level.
348      */
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,
362 } MIPSInsn;
365  * Type reg
366  */
367 static void tcg_out_opc_reg(TCGContext *s, MIPSInsn opc,
368                             TCGReg rd, TCGReg rs, TCGReg rt)
370     int32_t inst;
372     inst = opc;
373     inst |= (rs & 0x1F) << 21;
374     inst |= (rt & 0x1F) << 16;
375     inst |= (rd & 0x1F) << 11;
376     tcg_out32(s, inst);
380  * Type immediate
381  */
382 static void tcg_out_opc_imm(TCGContext *s, MIPSInsn opc,
383                             TCGReg rt, TCGReg rs, TCGArg imm)
385     int32_t inst;
387     inst = opc;
388     inst |= (rs & 0x1F) << 21;
389     inst |= (rt & 0x1F) << 16;
390     inst |= (imm & 0xffff);
391     tcg_out32(s, inst);
395  * Type bitfield
396  */
397 static void tcg_out_opc_bf(TCGContext *s, MIPSInsn opc, TCGReg rt,
398                            TCGReg rs, int msb, int lsb)
400     int32_t inst;
402     inst = opc;
403     inst |= (rs & 0x1F) << 21;
404     inst |= (rt & 0x1F) << 16;
405     inst |= (msb & 0x1F) << 11;
406     inst |= (lsb & 0x1F) << 6;
407     tcg_out32(s, inst);
410 static void tcg_out_opc_bf64(TCGContext *s, MIPSInsn opc, MIPSInsn opm,
411                              MIPSInsn oph, TCGReg rt, TCGReg rs,
412                                     int msb, int lsb)
414     if (lsb >= 32) {
415         opc = oph;
416         msb -= 32;
417         lsb -= 32;
418     } else if (msb >= 32) {
419         opc = opm;
420         msb -= 32;
421     }
422     tcg_out_opc_bf(s, opc, rt, rs, msb, lsb);
426  * Type branch
427  */
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);
434  * Type sa
435  */
436 static void tcg_out_opc_sa(TCGContext *s, MIPSInsn opc,
437                            TCGReg rd, TCGReg rt, TCGArg sa)
439     int32_t inst;
441     inst = opc;
442     inst |= (rt & 0x1F) << 16;
443     inst |= (rd & 0x1F) << 11;
444     inst |= (sa & 0x1F) <<  6;
445     tcg_out32(s, inst);
449 static void tcg_out_opc_sa64(TCGContext *s, MIPSInsn opc1, MIPSInsn opc2,
450                              TCGReg rd, TCGReg rt, TCGArg sa)
452     int32_t inst;
454     inst = (sa & 32 ? opc2 : opc1);
455     inst |= (rt & 0x1F) << 16;
456     inst |= (rd & 0x1F) << 11;
457     inst |= (sa & 0x1F) <<  6;
458     tcg_out32(s, inst);
462  * Type jump.
463  * Returns true if the branch was in range and the insn was emitted.
464  */
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;
469     int32_t inst;
471     /* The pc-region branch happens within the 256MB region of
472        the delay slot (thus the +4).  */
473     if ((from ^ dest) & -(1 << 28)) {
474         return false;
475     }
476     tcg_debug_assert((dest & 3) == 0);
478     inst = opc;
479     inst |= (dest >> 2) & 0x3ffffff;
480     tcg_out32(s, inst);
481     return true;
484 static void tcg_out_nop(TCGContext *s)
486     tcg_out32(s, 0);
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 */
507     if (ret != arg) {
508         tcg_out_opc_reg(s, OPC_OR, ret, arg, TCG_REG_ZERO);
509     }
510     return true;
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);
517         return true;
518     }
519     if (arg == (uint16_t)arg) {
520         tcg_out_opc_imm(s, OPC_ORI, ret, TCG_REG_ZERO, arg);
521         return true;
522     }
523     if (arg == (int32_t)arg && (arg & 0xffff) == 0) {
524         tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
525         return true;
526     }
527     return false;
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) {
534         arg = (int32_t)arg;
535     }
537     if (tcg_out_movi_one(s, ret, arg)) {
538         return;
539     }
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);
543     } else {
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);
549         } else {
550             tcg_out_dsll(s, ret, ret, 32);
551         }
552     }
553     if (arg & 0xffff) {
554         tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg & 0xffff);
555     }
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)
588     if (rd != rs) {
589         tcg_out_ext32s(s, rd, rs);
590     }
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)
605     return false;
608 static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
609                              tcg_target_long imm)
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 */
628         }
629         return;
630     }
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 */
635     }
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 */
639     } else {
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 */
643         }
644     }
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);
653     }
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);
663         }
664     } else {
665         if (flags & TCG_BSWAP_OZ) {
666             tcg_out_bswap_subr(s, bswap32u_addr);
667         } else {
668             tcg_out_bswap_subr(s, bswap32_addr);
669         }
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);
673     }
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);
681     } else {
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);
686     }
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);
694     } else {
695         tcg_out_dsll(s, ret, arg, 32);
696         tcg_out_dsrl(s, ret, ret, 32);
697     }
700 static void tcg_out_ldst(TCGContext *s, MIPSInsn opc, TCGReg data,
701                          TCGReg addr, intptr_t ofs)
703     int16_t lo = ofs;
704     if (ofs != lo) {
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);
708         }
709         addr = TCG_TMP0;
710     }
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) {
719         opc = OPC_LW;
720     }
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) {
729         opc = OPC_SW;
730     }
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)
737     if (val == 0) {
738         tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
739         return true;
740     }
741     return false;
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) {
753         bl = -bl;
754         bh = 0;
755         is_sub = !is_sub;
756     }
758     /* By operating on the high part first, we get to use the final
759        carry operation to move back from the temporary.  */
760     if (!cbh) {
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));
764     } else {
765         th = ah;
766     }
768     /* Note that tcg optimization should eliminate the bl == 0 case.  */
769     if (is_sub) {
770         if (cbl) {
771             tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, al, bl);
772             tcg_out_opc_imm(s, OPC_ADDIU, rl, al, -bl);
773         } else {
774             tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, al, bl);
775             tcg_out_opc_reg(s, OPC_SUBU, rl, al, bl);
776         }
777         tcg_out_opc_reg(s, OPC_SUBU, rh, th, TCG_TMP0);
778     } else {
779         if (cbl) {
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);
785         } else {
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));
788         }
789         tcg_out_opc_reg(s, OPC_ADDU, rh, th, TCG_TMP0);
790     }
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] = {
798     [TCG_COND_LT]  = 0,
799     [TCG_COND_LTU] = 0,
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;
812     int cmp_map;
814     switch (cond) {
815     case TCG_COND_EQ:
816         if (arg2 != 0) {
817             tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
818             arg1 = ret;
819         }
820         tcg_out_opc_imm(s, OPC_SLTIU, ret, arg1, 1);
821         break;
823     case TCG_COND_NE:
824         if (arg2 != 0) {
825             tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
826             arg1 = ret;
827         }
828         tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, arg1);
829         break;
831     case TCG_COND_LT:
832     case TCG_COND_GE:
833     case TCG_COND_LE:
834     case TCG_COND_GT:
835         s_opc = OPC_SLT;
836         /* FALLTHRU */
838     case TCG_COND_LTU:
839     case TCG_COND_GEU:
840     case TCG_COND_LEU:
841     case TCG_COND_GTU:
842         cmp_map = mips_cmp_map[cond];
843         if (cmp_map & MIPS_CMP_SWAP) {
844             TCGReg t = arg1;
845             arg1 = arg2;
846             arg2 = t;
847         }
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);
851         }
852         break;
854      default:
855          g_assert_not_reached();
856          break;
857      }
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,
868     };
870     MIPSInsn s_opc = OPC_SLTU;
871     MIPSInsn b_opc;
872     int cmp_map;
874     switch (cond) {
875     case TCG_COND_EQ:
876         b_opc = OPC_BEQ;
877         break;
878     case TCG_COND_NE:
879         b_opc = OPC_BNE;
880         break;
882     case TCG_COND_LT:
883     case TCG_COND_GT:
884     case TCG_COND_LE:
885     case TCG_COND_GE:
886         if (arg2 == 0) {
887             b_opc = b_zero[cond];
888             arg2 = arg1;
889             arg1 = 0;
890             break;
891         }
892         s_opc = OPC_SLT;
893         /* FALLTHRU */
895     case TCG_COND_LTU:
896     case TCG_COND_GTU:
897     case TCG_COND_LEU:
898     case TCG_COND_GEU:
899         cmp_map = mips_cmp_map[cond];
900         if (cmp_map & MIPS_CMP_SWAP) {
901             TCGReg t = arg1;
902             arg1 = arg2;
903             arg2 = t;
904         }
905         tcg_out_opc_reg(s, s_opc, TCG_TMP0, arg1, arg2);
906         b_opc = (cmp_map & MIPS_CMP_INV ? OPC_BEQ : OPC_BNE);
907         arg1 = TCG_TMP0;
908         arg2 = TCG_REG_ZERO;
909         break;
911     default:
912         g_assert_not_reached();
913         break;
914     }
916     tcg_out_opc_br(s, b_opc, arg1, arg2);
917     tcg_out_reloc(s, s->code_ptr - 1, R_MIPS_PC16, l, 0);
918     tcg_out_nop(s);
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.  */
926     if (bh != 0) {
927         if (ah != 0) {
928             tcg_out_opc_reg(s, OPC_XOR, tmp0, ah, bh);
929             ah = tmp0;
930         } else {
931             ah = bh;
932         }
933     }
934     /* Merge lowpart comparison into AL.  */
935     if (bl != 0) {
936         if (al != 0) {
937             tcg_out_opc_reg(s, OPC_XOR, tmp1, al, bl);
938             al = tmp1;
939         } else {
940             al = bl;
941         }
942     }
943     /* Merge high and low part comparisons into AL.  */
944     if (ah != 0) {
945         if (al != 0) {
946             tcg_out_opc_reg(s, OPC_OR, tmp0, ah, al);
947             al = tmp0;
948         } else {
949             al = ah;
950         }
951     }
952     return 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;
959     TCGReg tmp1 = ret;
961     tcg_debug_assert(ret != TCG_TMP0);
962     if (ret == ah || ret == bh) {
963         tcg_debug_assert(ret != TCG_TMP1);
964         tmp1 = TCG_TMP1;
965     }
967     switch (cond) {
968     case TCG_COND_EQ:
969     case TCG_COND_NE:
970         tmp1 = tcg_out_reduce_eq2(s, tmp0, tmp1, al, ah, bl, bh);
971         tcg_out_setcond(s, cond, ret, tmp1, TCG_REG_ZERO);
972         break;
974     default:
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);
980         break;
981     }
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.  */
993      switch (cond) {
994      case TCG_COND_EQ:
995      case TCG_COND_NE:
996         b_cond = cond;
997         tmp = tcg_out_reduce_eq2(s, TCG_TMP0, TCG_TMP1, al, ah, bl, bh);
998         break;
1000     default:
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;
1005         }
1006         tcg_out_setcond2(s, cond, tmp, al, ah, bl, bh);
1007         break;
1008     }
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)
1016     bool eqz = false;
1018     /* If one of the values is zero, put it last to match SEL*Z instructions */
1019     if (use_mips32r6_instructions && v1 == 0) {
1020         v1 = v2;
1021         v2 = 0;
1022         cond = tcg_invert_cond(cond);
1023     }
1025     switch (cond) {
1026     case TCG_COND_EQ:
1027         eqz = true;
1028         /* FALLTHRU */
1029     case TCG_COND_NE:
1030         if (c2 != 0) {
1031             tcg_out_opc_reg(s, OPC_XOR, TCG_TMP0, c1, c2);
1032             c1 = TCG_TMP0;
1033         }
1034         break;
1036     default:
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);
1040             eqz = true;
1041         }
1042         tcg_out_setcond(s, cond, TCG_TMP0, c1, c2);
1043         c1 = TCG_TMP0;
1044         break;
1045     }
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;
1051         if (v2 != 0) {
1052             tcg_out_opc_reg(s, m_opc_f, TCG_TMP1, v2, c1);
1053         }
1054         tcg_out_opc_reg(s, m_opc_t, ret, v1, c1);
1055         if (v2 != 0) {
1056             tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP1);
1057         }
1058     } else {
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);
1065     }
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.  */
1075     if (tail) {
1076         if (!tcg_out_opc_jmp(s, OPC_J, arg)) {
1077             tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_T9, 0);
1078         }
1079     } else {
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);
1082         }
1083     }
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);
1090     tcg_out_nop(s);
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))) {
1106         return false;
1107     }
1109     tcg_out_ld_helper_args(s, l, &ldst_helper_param);
1111     tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false);
1112     /* delay slot */
1113     tcg_out_nop(s);
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)) {
1119         return false;
1120     }
1122     /* delay slot */
1123     tcg_out_nop(s);
1124     return true;
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))) {
1135         return false;
1136     }
1138     tcg_out_st_helper_args(s, l, &ldst_helper_param);
1140     tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
1141     /* delay slot */
1142     tcg_out_nop(s);
1144     tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1145     if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1146         return false;
1147     }
1149     /* delay slot */
1150     tcg_out_nop(s);
1151     return true;
1154 typedef struct {
1155     TCGReg base;
1156     TCGAtomAlign aa;
1157 } HostAddress;
1159 bool tcg_target_has_memory_bswap(MemOp memop)
1161     return false;
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.
1169  */
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);
1177     MemOp a_bits;
1178     unsigned s_bits = opc & MO_SIZE;
1179     unsigned a_mask;
1180     TCGReg base;
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;
1198     ldst->oi = oi;
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);
1212     } else {
1213         tcg_out_dsrl(s, TCG_TMP3, addrlo,
1214                      s->page_bits - CPU_TLB_ENTRY_BITS);
1215     }
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);
1226     } else {
1227         /* Load the low half of the tlb comparator.  */
1228         tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + LO_OFF);
1229     }
1231     /*
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.
1235      */
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);
1240         } else {
1241             tcg_out_opc_imm(s, OPC_DADDIU, TCG_TMP2, addrlo, s_mask - a_mask);
1242         }
1243         tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP2);
1244     } else {
1245         tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, addrlo);
1246     }
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);
1251         addrlo = TCG_TMP2;
1252     }
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) {
1259         /* delay slot */
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);
1267     }
1269     /* delay slot */
1270     base = TCG_TMP3;
1271     tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_TMP3, addrlo);
1272 #else
1273     if (a_mask && (use_mips32r6_instructions || a_bits != s_bits)) {
1274         ldst = new_ldst_label(s);
1276         ldst->is_ld = is_ld;
1277         ldst->oi = oi;
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);
1288         } else {
1289             tcg_out_opc_br(s, OPC_BNEL, TCG_TMP0, TCG_REG_ZERO);
1290             tcg_out_nop(s);
1291         }
1292     }
1294     base = addrlo;
1295     if (TCG_TARGET_REG_BITS == 64 && addr_type == TCG_TYPE_I32) {
1296         tcg_out_ext32u(s, TCG_REG_A0, base);
1297         base = TCG_REG_A0;
1298     }
1299     if (guest_base) {
1300         if (guest_base == (int16_t)guest_base) {
1301             tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_A0, base, guest_base);
1302         } else {
1303             tcg_out_opc_reg(s, ALIAS_PADD, TCG_REG_A0, base,
1304                             TCG_GUEST_BASE_REG);
1305         }
1306         base = TCG_REG_A0;
1307     }
1308 #endif
1310     h->base = base;
1311     return ldst;
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) {
1318     case MO_UB:
1319         tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1320         break;
1321     case MO_SB:
1322         tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1323         break;
1324     case MO_UW:
1325         tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1326         break;
1327     case MO_SW:
1328         tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1329         break;
1330     case MO_UL:
1331         if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) {
1332             tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1333             break;
1334         }
1335         /* FALLTHRU */
1336     case MO_SL:
1337         tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1338         break;
1339     case MO_UQ:
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);
1346         } else {
1347             tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1348             tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1349         }
1350         break;
1351     default:
1352         g_assert_not_reached();
1353     }
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) {
1366     case MO_16:
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);
1372             } else {
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);
1375             }
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);
1380         } else {
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);
1385         }
1386         break;
1388     case MO_32:
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);
1393         }
1394         break;
1396     case MO_64:
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);
1400         } else {
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);
1405         }
1406         break;
1408     default:
1409         g_assert_not_reached();
1410     }
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;
1419     HostAddress h;
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);
1425     } else {
1426         tcg_out_qemu_ld_unalign(s, datalo, datahi, h.base, opc, data_type);
1427     }
1429     if (ldst) {
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);
1434     }
1437 static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1438                                    TCGReg base, MemOp opc)
1440     switch (opc & MO_SIZE) {
1441     case MO_8:
1442         tcg_out_opc_imm(s, OPC_SB, lo, base, 0);
1443         break;
1444     case MO_16:
1445         tcg_out_opc_imm(s, OPC_SH, lo, base, 0);
1446         break;
1447     case MO_32:
1448         tcg_out_opc_imm(s, OPC_SW, lo, base, 0);
1449         break;
1450     case MO_64:
1451         if (TCG_TARGET_REG_BITS == 64) {
1452             tcg_out_opc_imm(s, OPC_SD, lo, base, 0);
1453         } else {
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);
1456         }
1457         break;
1458     default:
1459         g_assert_not_reached();
1460     }
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) {
1472     case MO_16:
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);
1476         break;
1478     case MO_32:
1479         tcg_out_opc_imm(s, sw1, lo, base, 0);
1480         tcg_out_opc_imm(s, sw2, lo, base, 3);
1481         break;
1483     case MO_64:
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);
1487         } else {
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);
1492         }
1493         break;
1495     default:
1496         g_assert_not_reached();
1497     }
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;
1506     HostAddress h;
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);
1512     } else {
1513         tcg_out_qemu_st_unalign(s, datalo, datahi, h.base, opc);
1514     }
1516     if (ldst) {
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);
1521     }
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,
1536     };
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) {
1544         if (a2 == width) {
1545             tcg_out_opc_reg(s, opcv6, a0, a1, 0);
1546         } else {
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);
1549         }
1550     } else {
1551         if (a2 == width) {
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);
1559         } else {
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);
1563         }
1564     }
1567 static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1569     TCGReg base = TCG_REG_ZERO;
1570     int16_t lo = 0;
1572     if (a0) {
1573         intptr_t ofs;
1574         if (TCG_TARGET_REG_BITS == 64) {
1575             ofs = tcg_tbrel_diff(s, (void *)a0);
1576             lo = ofs;
1577             if (ofs == lo) {
1578                 base = TCG_REG_TB;
1579             } else {
1580                 base = TCG_REG_V0;
1581                 tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo);
1582                 tcg_out_opc_reg(s, ALIAS_PADD, base, base, TCG_REG_TB);
1583             }
1584         } else {
1585             ofs = a0;
1586             lo = ofs;
1587             base = TCG_REG_V0;
1588             tcg_out_movi(s, TCG_TYPE_PTR, base, ofs - lo);
1589         }
1590     }
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);
1594     }
1595     /* delay slot */
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);
1602     TCGReg base, dest;
1604     /* indirect jump method */
1605     if (TCG_TARGET_REG_BITS == 64) {
1606         dest = TCG_REG_TB;
1607         base = TCG_REG_TB;
1608         ofs = tcg_tbrel_diff(s, (void *)ofs);
1609     } else {
1610         dest = TCG_TMP0;
1611         base = TCG_REG_ZERO;
1612     }
1613     tcg_out_ld(s, TCG_TYPE_PTR, dest, base, ofs);
1614     tcg_out_opc_reg(s, OPC_JR, 0, dest, 0);
1615     /* delay slot */
1616     tcg_out_nop(s);
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));
1623     }
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])
1636     MIPSInsn i1, i2;
1637     TCGArg a0, a1, a2;
1638     int c2;
1640     /*
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.
1644      */
1645     a0 = args[0];
1646     a1 = args[1];
1647     a2 = args[2];
1648     c2 = const_args[2];
1650     switch (opc) {
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);
1656         } else {
1657             tcg_out_nop(s);
1658         }
1659         break;
1660     case INDEX_op_br:
1661         tcg_out_brcond(s, TCG_COND_EQ, TCG_REG_ZERO, TCG_REG_ZERO,
1662                        arg_label(a0));
1663         break;
1665     case INDEX_op_ld8u_i32:
1666     case INDEX_op_ld8u_i64:
1667         i1 = OPC_LBU;
1668         goto do_ldst;
1669     case INDEX_op_ld8s_i32:
1670     case INDEX_op_ld8s_i64:
1671         i1 = OPC_LB;
1672         goto do_ldst;
1673     case INDEX_op_ld16u_i32:
1674     case INDEX_op_ld16u_i64:
1675         i1 = OPC_LHU;
1676         goto do_ldst;
1677     case INDEX_op_ld16s_i32:
1678     case INDEX_op_ld16s_i64:
1679         i1 = OPC_LH;
1680         goto do_ldst;
1681     case INDEX_op_ld_i32:
1682     case INDEX_op_ld32s_i64:
1683         i1 = OPC_LW;
1684         goto do_ldst;
1685     case INDEX_op_ld32u_i64:
1686         i1 = OPC_LWU;
1687         goto do_ldst;
1688     case INDEX_op_ld_i64:
1689         i1 = OPC_LD;
1690         goto do_ldst;
1691     case INDEX_op_st8_i32:
1692     case INDEX_op_st8_i64:
1693         i1 = OPC_SB;
1694         goto do_ldst;
1695     case INDEX_op_st16_i32:
1696     case INDEX_op_st16_i64:
1697         i1 = OPC_SH;
1698         goto do_ldst;
1699     case INDEX_op_st_i32:
1700     case INDEX_op_st32_i64:
1701         i1 = OPC_SW;
1702         goto do_ldst;
1703     case INDEX_op_st_i64:
1704         i1 = OPC_SD;
1705     do_ldst:
1706         tcg_out_ldst(s, i1, a0, a1, a2);
1707         break;
1709     case INDEX_op_add_i32:
1710         i1 = OPC_ADDU, i2 = OPC_ADDIU;
1711         goto do_binary;
1712     case INDEX_op_add_i64:
1713         i1 = OPC_DADDU, i2 = OPC_DADDIU;
1714         goto do_binary;
1715     case INDEX_op_or_i32:
1716     case INDEX_op_or_i64:
1717         i1 = OPC_OR, i2 = OPC_ORI;
1718         goto do_binary;
1719     case INDEX_op_xor_i32:
1720     case INDEX_op_xor_i64:
1721         i1 = OPC_XOR, i2 = OPC_XORI;
1722     do_binary:
1723         if (c2) {
1724             tcg_out_opc_imm(s, i2, a0, a1, a2);
1725             break;
1726         }
1727     do_binaryv:
1728         tcg_out_opc_reg(s, i1, a0, a1, a2);
1729         break;
1731     case INDEX_op_sub_i32:
1732         i1 = OPC_SUBU, i2 = OPC_ADDIU;
1733         goto do_subtract;
1734     case INDEX_op_sub_i64:
1735         i1 = OPC_DSUBU, i2 = OPC_DADDIU;
1736     do_subtract:
1737         if (c2) {
1738             tcg_out_opc_imm(s, i2, a0, a1, -a2);
1739             break;
1740         }
1741         goto do_binaryv;
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);
1748             break;
1749         }
1750         i1 = OPC_AND, i2 = OPC_ANDI;
1751         goto do_binary;
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);
1758             break;
1759         }
1760         i1 = OPC_AND, i2 = OPC_ANDI;
1761         goto do_binary;
1762     case INDEX_op_nor_i32:
1763     case INDEX_op_nor_i64:
1764         i1 = OPC_NOR;
1765         goto do_binaryv;
1767     case INDEX_op_mul_i32:
1768         if (use_mips32_instructions) {
1769             tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1770             break;
1771         }
1772         i1 = OPC_MULT, i2 = OPC_MFLO;
1773         goto do_hilo1;
1774     case INDEX_op_mulsh_i32:
1775         if (use_mips32r6_instructions) {
1776             tcg_out_opc_reg(s, OPC_MUH, a0, a1, a2);
1777             break;
1778         }
1779         i1 = OPC_MULT, i2 = OPC_MFHI;
1780         goto do_hilo1;
1781     case INDEX_op_muluh_i32:
1782         if (use_mips32r6_instructions) {
1783             tcg_out_opc_reg(s, OPC_MUHU, a0, a1, a2);
1784             break;
1785         }
1786         i1 = OPC_MULTU, i2 = OPC_MFHI;
1787         goto do_hilo1;
1788     case INDEX_op_div_i32:
1789         if (use_mips32r6_instructions) {
1790             tcg_out_opc_reg(s, OPC_DIV_R6, a0, a1, a2);
1791             break;
1792         }
1793         i1 = OPC_DIV, i2 = OPC_MFLO;
1794         goto do_hilo1;
1795     case INDEX_op_divu_i32:
1796         if (use_mips32r6_instructions) {
1797             tcg_out_opc_reg(s, OPC_DIVU_R6, a0, a1, a2);
1798             break;
1799         }
1800         i1 = OPC_DIVU, i2 = OPC_MFLO;
1801         goto do_hilo1;
1802     case INDEX_op_rem_i32:
1803         if (use_mips32r6_instructions) {
1804             tcg_out_opc_reg(s, OPC_MOD, a0, a1, a2);
1805             break;
1806         }
1807         i1 = OPC_DIV, i2 = OPC_MFHI;
1808         goto do_hilo1;
1809     case INDEX_op_remu_i32:
1810         if (use_mips32r6_instructions) {
1811             tcg_out_opc_reg(s, OPC_MODU, a0, a1, a2);
1812             break;
1813         }
1814         i1 = OPC_DIVU, i2 = OPC_MFHI;
1815         goto do_hilo1;
1816     case INDEX_op_mul_i64:
1817         if (use_mips32r6_instructions) {
1818             tcg_out_opc_reg(s, OPC_DMUL, a0, a1, a2);
1819             break;
1820         }
1821         i1 = OPC_DMULT, i2 = OPC_MFLO;
1822         goto do_hilo1;
1823     case INDEX_op_mulsh_i64:
1824         if (use_mips32r6_instructions) {
1825             tcg_out_opc_reg(s, OPC_DMUH, a0, a1, a2);
1826             break;
1827         }
1828         i1 = OPC_DMULT, i2 = OPC_MFHI;
1829         goto do_hilo1;
1830     case INDEX_op_muluh_i64:
1831         if (use_mips32r6_instructions) {
1832             tcg_out_opc_reg(s, OPC_DMUHU, a0, a1, a2);
1833             break;
1834         }
1835         i1 = OPC_DMULTU, i2 = OPC_MFHI;
1836         goto do_hilo1;
1837     case INDEX_op_div_i64:
1838         if (use_mips32r6_instructions) {
1839             tcg_out_opc_reg(s, OPC_DDIV_R6, a0, a1, a2);
1840             break;
1841         }
1842         i1 = OPC_DDIV, i2 = OPC_MFLO;
1843         goto do_hilo1;
1844     case INDEX_op_divu_i64:
1845         if (use_mips32r6_instructions) {
1846             tcg_out_opc_reg(s, OPC_DDIVU_R6, a0, a1, a2);
1847             break;
1848         }
1849         i1 = OPC_DDIVU, i2 = OPC_MFLO;
1850         goto do_hilo1;
1851     case INDEX_op_rem_i64:
1852         if (use_mips32r6_instructions) {
1853             tcg_out_opc_reg(s, OPC_DMOD, a0, a1, a2);
1854             break;
1855         }
1856         i1 = OPC_DDIV, i2 = OPC_MFHI;
1857         goto do_hilo1;
1858     case INDEX_op_remu_i64:
1859         if (use_mips32r6_instructions) {
1860             tcg_out_opc_reg(s, OPC_DMODU, a0, a1, a2);
1861             break;
1862         }
1863         i1 = OPC_DDIVU, i2 = OPC_MFHI;
1864     do_hilo1:
1865         tcg_out_opc_reg(s, i1, 0, a1, a2);
1866         tcg_out_opc_reg(s, i2, a0, 0, 0);
1867         break;
1869     case INDEX_op_muls2_i32:
1870         i1 = OPC_MULT;
1871         goto do_hilo2;
1872     case INDEX_op_mulu2_i32:
1873         i1 = OPC_MULTU;
1874         goto do_hilo2;
1875     case INDEX_op_muls2_i64:
1876         i1 = OPC_DMULT;
1877         goto do_hilo2;
1878     case INDEX_op_mulu2_i64:
1879         i1 = OPC_DMULTU;
1880     do_hilo2:
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);
1884         break;
1886     case INDEX_op_not_i32:
1887     case INDEX_op_not_i64:
1888         i1 = OPC_NOR;
1889         goto do_unary;
1890     do_unary:
1891         tcg_out_opc_reg(s, i1, a0, TCG_REG_ZERO, a1);
1892         break;
1894     case INDEX_op_bswap16_i32:
1895     case INDEX_op_bswap16_i64:
1896         tcg_out_bswap16(s, a0, a1, a2);
1897         break;
1898     case INDEX_op_bswap32_i32:
1899         tcg_out_bswap32(s, a0, a1, 0);
1900         break;
1901     case INDEX_op_bswap32_i64:
1902         tcg_out_bswap32(s, a0, a1, a2);
1903         break;
1904     case INDEX_op_bswap64_i64:
1905         tcg_out_bswap64(s, a0, a1);
1906         break;
1907     case INDEX_op_extrh_i64_i32:
1908         tcg_out_dsra(s, a0, a1, 32);
1909         break;
1911     case INDEX_op_sar_i32:
1912         i1 = OPC_SRAV, i2 = OPC_SRA;
1913         goto do_shift;
1914     case INDEX_op_shl_i32:
1915         i1 = OPC_SLLV, i2 = OPC_SLL;
1916         goto do_shift;
1917     case INDEX_op_shr_i32:
1918         i1 = OPC_SRLV, i2 = OPC_SRL;
1919         goto do_shift;
1920     case INDEX_op_rotr_i32:
1921         i1 = OPC_ROTRV, i2 = OPC_ROTR;
1922     do_shift:
1923         if (c2) {
1924             tcg_out_opc_sa(s, i2, a0, a1, a2);
1925             break;
1926         }
1927     do_shiftv:
1928         tcg_out_opc_reg(s, i1, a0, a2, a1);
1929         break;
1930     case INDEX_op_rotl_i32:
1931         if (c2) {
1932             tcg_out_opc_sa(s, OPC_ROTR, a0, a1, 32 - a2);
1933         } else {
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);
1936         }
1937         break;
1938     case INDEX_op_sar_i64:
1939         if (c2) {
1940             tcg_out_dsra(s, a0, a1, a2);
1941             break;
1942         }
1943         i1 = OPC_DSRAV;
1944         goto do_shiftv;
1945     case INDEX_op_shl_i64:
1946         if (c2) {
1947             tcg_out_dsll(s, a0, a1, a2);
1948             break;
1949         }
1950         i1 = OPC_DSLLV;
1951         goto do_shiftv;
1952     case INDEX_op_shr_i64:
1953         if (c2) {
1954             tcg_out_dsrl(s, a0, a1, a2);
1955             break;
1956         }
1957         i1 = OPC_DSRLV;
1958         goto do_shiftv;
1959     case INDEX_op_rotr_i64:
1960         if (c2) {
1961             tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, a2);
1962             break;
1963         }
1964         i1 = OPC_DROTRV;
1965         goto do_shiftv;
1966     case INDEX_op_rotl_i64:
1967         if (c2) {
1968             tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, 64 - a2);
1969         } else {
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);
1972         }
1973         break;
1975     case INDEX_op_clz_i32:
1976         tcg_out_clz(s, OPC_CLZ, OPC_CLZ_R6, 32, a0, a1, a2);
1977         break;
1978     case INDEX_op_clz_i64:
1979         tcg_out_clz(s, OPC_DCLZ, OPC_DCLZ_R6, 64, a0, a1, a2);
1980         break;
1982     case INDEX_op_deposit_i32:
1983         tcg_out_opc_bf(s, OPC_INS, a0, a2, args[3] + args[4] - 1, args[3]);
1984         break;
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]);
1988         break;
1989     case INDEX_op_extract_i32:
1990         tcg_out_opc_bf(s, OPC_EXT, a0, a1, args[3] - 1, a2);
1991         break;
1992     case INDEX_op_extract_i64:
1993         tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1,
1994                          args[3] - 1, a2);
1995         break;
1997     case INDEX_op_brcond_i32:
1998     case INDEX_op_brcond_i64:
1999         tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
2000         break;
2001     case INDEX_op_brcond2_i32:
2002         tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
2003         break;
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]);
2008         break;
2010     case INDEX_op_setcond_i32:
2011     case INDEX_op_setcond_i64:
2012         tcg_out_setcond(s, args[3], a0, a1, a2);
2013         break;
2014     case INDEX_op_setcond2_i32:
2015         tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
2016         break;
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);
2021             break;
2022         }
2023         /* fall through */
2024     case INDEX_op_qemu_ld_a32_i32:
2025         tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2026         break;
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);
2030         } else {
2031             tcg_out_qemu_ld(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2032         }
2033         break;
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);
2037         } else {
2038             tcg_out_qemu_ld(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2039         }
2040         break;
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);
2045             break;
2046         }
2047         /* fall through */
2048     case INDEX_op_qemu_st_a32_i32:
2049         tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2050         break;
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);
2054         } else {
2055             tcg_out_qemu_st(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2056         }
2057         break;
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);
2061         } else {
2062             tcg_out_qemu_st(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2063         }
2064         break;
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);
2069         break;
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);
2073         break;
2075     case INDEX_op_mb:
2076         tcg_out_mb(s, a0);
2077         break;
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:
2094     default:
2095         g_assert_not_reached();
2096     }
2099 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2101     switch (op) {
2102     case INDEX_op_goto_ptr:
2103         return C_O0_I1(r);
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));
2237     default:
2238         g_assert_not_reached();
2239     }
2242 static const int tcg_target_callee_save_regs[] = {
2243     TCG_REG_S0,
2244     TCG_REG_S1,
2245     TCG_REG_S2,
2246     TCG_REG_S3,
2247     TCG_REG_S4,
2248     TCG_REG_S5,
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;
2261 #endif
2263 #ifndef use_mips32_instructions
2264 bool use_mips32_instructions = false;
2265 #endif
2267 #ifndef use_mips32r2_instructions
2268 bool use_mips32r2_instructions = false;
2269 #endif
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;
2279     got_sigill = 1;
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
2293     got_sigill = 0;
2294     asm volatile(".set push\n"
2295                  ".set mips32\n"
2296                  "movn $zero, $zero, $zero\n"
2297                  "movz $zero, $zero, $zero\n"
2298                  ".set pop\n"
2299                  : : : );
2300     use_movnz_instructions = !got_sigill;
2301 #endif
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
2307     got_sigill = 0;
2308     asm volatile(".set push\n"
2309                  ".set mips32\n"
2310                  "mul $zero, $zero\n"
2311                  ".set pop\n"
2312                  : : : );
2313     use_mips32_instructions = !got_sigill;
2314 #endif
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) {
2321         got_sigill = 0;
2322         asm volatile(".set push\n"
2323                      ".set mips32r2\n"
2324                      "seb $zero, $zero\n"
2325                      ".set pop\n"
2326                      : : : );
2327         use_mips32r2_instructions = !got_sigill;
2328     }
2329 #endif
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;
2337     if (p & 15) {
2338         p = (p + 15) & -16;
2339         s->code_ptr = (void *)p;
2340     }
2341     return s->code_ptr;
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)
2360     int i;
2362     tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
2364     /* TB prologue */
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);
2369     }
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);
2375     }
2376 #endif
2377     if (TCG_TARGET_REG_BITS == 64) {
2378         tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_TB, tcg_target_call_iarg_regs[1]);
2379     }
2381     /* Call generated code */
2382     tcg_out_opc_reg(s, OPC_JR, 0, tcg_target_call_iarg_regs[1], 0);
2383     /* delay slot */
2384     tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2386     /*
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.
2389      */
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);
2393     /* TB epilogue */
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);
2398     }
2400     tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2401     /* delay slot */
2402     tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
2404     if (use_mips32r2_instructions) {
2405         return;
2406     }
2408     /* Bswap subroutines: Input in TCG_TMP0, output in TCG_TMP3;
2409        clobbers TCG_TMP1, TCG_TMP2.  */
2411     /*
2412      * bswap32 -- 32-bit swap (signed result for mips64).  a0 = abcd.
2413      */
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);
2417     /* t1 = 000a */
2418     tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 24);
2419     /* t2 = 00c0 */
2420     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2421     /* t3 = d00a */
2422     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2423     /* t1 = 0abc */
2424     tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2425     /* t2 = 0c00 */
2426     tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2427     /* t1 = 00b0 */
2428     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2429     /* t3 = dc0a */
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) {
2436         return;
2437     }
2439     /*
2440      * bswap32u -- unsigned 32-bit swap.  a0 = ....abcd.
2441      */
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);
2445     /* t3 = 000a */
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);
2449     /* t2 = 00c0 */
2450     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2451     /* t3 = d00a */
2452     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2453     /* t1 = 0abc */
2454     tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2455     /* t2 = 0c00 */
2456     tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2457     /* t1 = 00b0 */
2458     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2459     /* t3 = dc0a */
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);
2465     /*
2466      * bswap64 -- 64-bit swap.  a0 = abcdefgh
2467      */
2468     bswap64_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2469     /* t3 = h0000000 */
2470     tcg_out_dsll(s, TCG_TMP3, TCG_TMP0, 56);
2471     /* t1 = 0000000a */
2472     tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 56);
2474     /* t2 = 000000g0 */
2475     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2476     /* t3 = h000000a */
2477     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2478     /* t1 = 00000abc */
2479     tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 40);
2480     /* t2 = 0g000000 */
2481     tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2482     /* t1 = 000000b0 */
2483     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2485     /* t3 = hg00000a */
2486     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2487     /* t2 = 0000abcd */
2488     tcg_out_dsrl(s, TCG_TMP2, TCG_TMP0, 32);
2489     /* t3 = hg0000ba */
2490     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2492     /* t1 = 000000c0 */
2493     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP2, 0xff00);
2494     /* t2 = 0000000d */
2495     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP2, 0x00ff);
2496     /* t1 = 00000c00 */
2497     tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 8);
2498     /* t2 = 0000d000 */
2499     tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 24);
2501     /* t3 = hg000cba */
2502     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2503     /* t1 = 00abcdef */
2504     tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 16);
2505     /* t3 = hg00dcba */
2506     tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2508     /* t2 = 0000000f */
2509     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP1, 0x00ff);
2510     /* t1 = 000000e0 */
2511     tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2512     /* t2 = 00f00000 */
2513     tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2514     /* t1 = 000e0000 */
2515     tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2517     /* t3 = hgf0dcba */
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;
2530     }
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 */
2563     }
2566 typedef struct {
2567     DebugFrameHeader h;
2568     uint8_t fde_def_cfa[4];
2569     uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
2570 } DebugFrame;
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 */
2578     .h.cie.id = -1,
2579     .h.cie.version = 1,
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),
2587     .fde_def_cfa = {
2588         12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ... */
2589         (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
2590         (FRAME_SIZE >> 7)
2591     },
2592     .fde_reg_ofs = {
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 */
2602     }
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));