xref: /openbmc/qemu/tcg/mips/tcg-target.c.inc (revision 0cadc1eda1a3120c37c713ab6d6b7a02da0d2e6f)
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 */
26
27#include "../tcg-ldst.c.inc"
28
29#if HOST_BIG_ENDIAN
30# define MIPS_BE  1
31#else
32# define MIPS_BE  0
33#endif
34
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.  */
41int link_error(void);
42# define LO_OFF  link_error()
43# define HI_OFF  link_error()
44#endif
45
46#ifdef CONFIG_DEBUG_TCG
47static 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",
80};
81#endif
82
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
87
88#ifndef CONFIG_SOFTMMU
89#define TCG_GUEST_BASE_REG TCG_REG_S1
90#endif
91
92/* check if we really need so many registers :P */
93static const int tcg_target_reg_alloc_order[] = {
94    /* Call saved registers.  */
95    TCG_REG_S0,
96    TCG_REG_S1,
97    TCG_REG_S2,
98    TCG_REG_S3,
99    TCG_REG_S4,
100    TCG_REG_S5,
101    TCG_REG_S6,
102    TCG_REG_S7,
103    TCG_REG_S8,
104
105    /* Call clobbered registers.  */
106    TCG_REG_T4,
107    TCG_REG_T5,
108    TCG_REG_T6,
109    TCG_REG_T7,
110    TCG_REG_T8,
111    TCG_REG_T9,
112    TCG_REG_V1,
113    TCG_REG_V0,
114
115    /* Argument registers, opposite order of allocation.  */
116    TCG_REG_T3,
117    TCG_REG_T2,
118    TCG_REG_T1,
119    TCG_REG_T0,
120    TCG_REG_A3,
121    TCG_REG_A2,
122    TCG_REG_A1,
123    TCG_REG_A0,
124};
125
126static const TCGReg tcg_target_call_iarg_regs[] = {
127    TCG_REG_A0,
128    TCG_REG_A1,
129    TCG_REG_A2,
130    TCG_REG_A3,
131#if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
132    TCG_REG_T0,
133    TCG_REG_T1,
134    TCG_REG_T2,
135    TCG_REG_T3,
136#endif
137};
138
139static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
140{
141    tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
142    tcg_debug_assert(slot >= 0 && slot <= 1);
143    return TCG_REG_V0 + slot;
144}
145
146static const tcg_insn_unit *tb_ret_addr;
147static const tcg_insn_unit *bswap32_addr;
148static const tcg_insn_unit *bswap32u_addr;
149static const tcg_insn_unit *bswap64_addr;
150
151static bool reloc_pc16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
152{
153    /* Let the compiler perform the right-shift as part of the arithmetic.  */
154    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
155    ptrdiff_t disp = target - (src_rx + 1);
156    if (disp == (int16_t)disp) {
157        *src_rw = deposit32(*src_rw, 0, 16, disp);
158        return true;
159    }
160    return false;
161}
162
163static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
164                        intptr_t value, intptr_t addend)
165{
166    tcg_debug_assert(type == R_MIPS_PC16);
167    tcg_debug_assert(addend == 0);
168    return reloc_pc16(code_ptr, (const tcg_insn_unit *)value);
169}
170
171#define TCG_CT_CONST_ZERO 0x100
172#define TCG_CT_CONST_U16  0x200    /* Unsigned 16-bit: 0 - 0xffff.  */
173#define TCG_CT_CONST_S16  0x400    /* Signed 16-bit: -32768 - 32767 */
174#define TCG_CT_CONST_P2M1 0x800    /* Power of 2 minus 1.  */
175#define TCG_CT_CONST_N16  0x1000   /* "Negatable" 16-bit: -32767 - 32767 */
176#define TCG_CT_CONST_WSZ  0x2000   /* word size */
177
178#define ALL_GENERAL_REGS  0xffffffffu
179
180static bool is_p2m1(tcg_target_long val)
181{
182    return val && ((val + 1) & val) == 0;
183}
184
185/* test if a constant matches the constraint */
186static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
187{
188    if (ct & TCG_CT_CONST) {
189        return 1;
190    } else if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
191        return 1;
192    } else if ((ct & TCG_CT_CONST_U16) && val == (uint16_t)val) {
193        return 1;
194    } else if ((ct & TCG_CT_CONST_S16) && val == (int16_t)val) {
195        return 1;
196    } else if ((ct & TCG_CT_CONST_N16) && val >= -32767 && val <= 32767) {
197        return 1;
198    } else if ((ct & TCG_CT_CONST_P2M1)
199               && use_mips32r2_instructions && is_p2m1(val)) {
200        return 1;
201    } else if ((ct & TCG_CT_CONST_WSZ)
202               && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
203        return 1;
204    }
205    return 0;
206}
207
208/* instruction opcodes */
209typedef enum {
210    OPC_J        = 002 << 26,
211    OPC_JAL      = 003 << 26,
212    OPC_BEQ      = 004 << 26,
213    OPC_BNE      = 005 << 26,
214    OPC_BLEZ     = 006 << 26,
215    OPC_BGTZ     = 007 << 26,
216    OPC_ADDIU    = 011 << 26,
217    OPC_SLTI     = 012 << 26,
218    OPC_SLTIU    = 013 << 26,
219    OPC_ANDI     = 014 << 26,
220    OPC_ORI      = 015 << 26,
221    OPC_XORI     = 016 << 26,
222    OPC_LUI      = 017 << 26,
223    OPC_BNEL     = 025 << 26,
224    OPC_BNEZALC_R6 = 030 << 26,
225    OPC_DADDIU   = 031 << 26,
226    OPC_LDL      = 032 << 26,
227    OPC_LDR      = 033 << 26,
228    OPC_LB       = 040 << 26,
229    OPC_LH       = 041 << 26,
230    OPC_LWL      = 042 << 26,
231    OPC_LW       = 043 << 26,
232    OPC_LBU      = 044 << 26,
233    OPC_LHU      = 045 << 26,
234    OPC_LWR      = 046 << 26,
235    OPC_LWU      = 047 << 26,
236    OPC_SB       = 050 << 26,
237    OPC_SH       = 051 << 26,
238    OPC_SWL      = 052 << 26,
239    OPC_SW       = 053 << 26,
240    OPC_SDL      = 054 << 26,
241    OPC_SDR      = 055 << 26,
242    OPC_SWR      = 056 << 26,
243    OPC_LD       = 067 << 26,
244    OPC_SD       = 077 << 26,
245
246    OPC_SPECIAL  = 000 << 26,
247    OPC_SLL      = OPC_SPECIAL | 000,
248    OPC_SRL      = OPC_SPECIAL | 002,
249    OPC_ROTR     = OPC_SPECIAL | 002 | (1 << 21),
250    OPC_SRA      = OPC_SPECIAL | 003,
251    OPC_SLLV     = OPC_SPECIAL | 004,
252    OPC_SRLV     = OPC_SPECIAL | 006,
253    OPC_ROTRV    = OPC_SPECIAL | 006 | 0100,
254    OPC_SRAV     = OPC_SPECIAL | 007,
255    OPC_JR_R5    = OPC_SPECIAL | 010,
256    OPC_JALR     = OPC_SPECIAL | 011,
257    OPC_MOVZ     = OPC_SPECIAL | 012,
258    OPC_MOVN     = OPC_SPECIAL | 013,
259    OPC_SYNC     = OPC_SPECIAL | 017,
260    OPC_MFHI     = OPC_SPECIAL | 020,
261    OPC_MFLO     = OPC_SPECIAL | 022,
262    OPC_DSLLV    = OPC_SPECIAL | 024,
263    OPC_DSRLV    = OPC_SPECIAL | 026,
264    OPC_DROTRV   = OPC_SPECIAL | 026 | 0100,
265    OPC_DSRAV    = OPC_SPECIAL | 027,
266    OPC_MULT     = OPC_SPECIAL | 030,
267    OPC_MUL_R6   = OPC_SPECIAL | 030 | 0200,
268    OPC_MUH      = OPC_SPECIAL | 030 | 0300,
269    OPC_MULTU    = OPC_SPECIAL | 031,
270    OPC_MULU     = OPC_SPECIAL | 031 | 0200,
271    OPC_MUHU     = OPC_SPECIAL | 031 | 0300,
272    OPC_DIV      = OPC_SPECIAL | 032,
273    OPC_DIV_R6   = OPC_SPECIAL | 032 | 0200,
274    OPC_MOD      = OPC_SPECIAL | 032 | 0300,
275    OPC_DIVU     = OPC_SPECIAL | 033,
276    OPC_DIVU_R6  = OPC_SPECIAL | 033 | 0200,
277    OPC_MODU     = OPC_SPECIAL | 033 | 0300,
278    OPC_DMULT    = OPC_SPECIAL | 034,
279    OPC_DMUL     = OPC_SPECIAL | 034 | 0200,
280    OPC_DMUH     = OPC_SPECIAL | 034 | 0300,
281    OPC_DMULTU   = OPC_SPECIAL | 035,
282    OPC_DMULU    = OPC_SPECIAL | 035 | 0200,
283    OPC_DMUHU    = OPC_SPECIAL | 035 | 0300,
284    OPC_DDIV     = OPC_SPECIAL | 036,
285    OPC_DDIV_R6  = OPC_SPECIAL | 036 | 0200,
286    OPC_DMOD     = OPC_SPECIAL | 036 | 0300,
287    OPC_DDIVU    = OPC_SPECIAL | 037,
288    OPC_DDIVU_R6 = OPC_SPECIAL | 037 | 0200,
289    OPC_DMODU    = OPC_SPECIAL | 037 | 0300,
290    OPC_ADDU     = OPC_SPECIAL | 041,
291    OPC_SUBU     = OPC_SPECIAL | 043,
292    OPC_AND      = OPC_SPECIAL | 044,
293    OPC_OR       = OPC_SPECIAL | 045,
294    OPC_XOR      = OPC_SPECIAL | 046,
295    OPC_NOR      = OPC_SPECIAL | 047,
296    OPC_SLT      = OPC_SPECIAL | 052,
297    OPC_SLTU     = OPC_SPECIAL | 053,
298    OPC_DADDU    = OPC_SPECIAL | 055,
299    OPC_DSUBU    = OPC_SPECIAL | 057,
300    OPC_SELEQZ   = OPC_SPECIAL | 065,
301    OPC_SELNEZ   = OPC_SPECIAL | 067,
302    OPC_DSLL     = OPC_SPECIAL | 070,
303    OPC_DSRL     = OPC_SPECIAL | 072,
304    OPC_DROTR    = OPC_SPECIAL | 072 | (1 << 21),
305    OPC_DSRA     = OPC_SPECIAL | 073,
306    OPC_DSLL32   = OPC_SPECIAL | 074,
307    OPC_DSRL32   = OPC_SPECIAL | 076,
308    OPC_DROTR32  = OPC_SPECIAL | 076 | (1 << 21),
309    OPC_DSRA32   = OPC_SPECIAL | 077,
310    OPC_CLZ_R6   = OPC_SPECIAL | 0120,
311    OPC_DCLZ_R6  = OPC_SPECIAL | 0122,
312
313    OPC_REGIMM   = 001 << 26,
314    OPC_BLTZ     = OPC_REGIMM | (000 << 16),
315    OPC_BGEZ     = OPC_REGIMM | (001 << 16),
316
317    OPC_SPECIAL2 = 034 << 26,
318    OPC_MUL_R5   = OPC_SPECIAL2 | 002,
319    OPC_CLZ      = OPC_SPECIAL2 | 040,
320    OPC_DCLZ     = OPC_SPECIAL2 | 044,
321
322    OPC_SPECIAL3 = 037 << 26,
323    OPC_EXT      = OPC_SPECIAL3 | 000,
324    OPC_DEXTM    = OPC_SPECIAL3 | 001,
325    OPC_DEXTU    = OPC_SPECIAL3 | 002,
326    OPC_DEXT     = OPC_SPECIAL3 | 003,
327    OPC_INS      = OPC_SPECIAL3 | 004,
328    OPC_DINSM    = OPC_SPECIAL3 | 005,
329    OPC_DINSU    = OPC_SPECIAL3 | 006,
330    OPC_DINS     = OPC_SPECIAL3 | 007,
331    OPC_WSBH     = OPC_SPECIAL3 | 00240,
332    OPC_DSBH     = OPC_SPECIAL3 | 00244,
333    OPC_DSHD     = OPC_SPECIAL3 | 00544,
334    OPC_SEB      = OPC_SPECIAL3 | 02040,
335    OPC_SEH      = OPC_SPECIAL3 | 03040,
336
337    /* MIPS r6 doesn't have JR, JALR should be used instead */
338    OPC_JR       = use_mips32r6_instructions ? OPC_JALR : OPC_JR_R5,
339
340    /*
341     * MIPS r6 replaces MUL with an alternative encoding which is
342     * backwards-compatible at the assembly level.
343     */
344    OPC_MUL      = use_mips32r6_instructions ? OPC_MUL_R6 : OPC_MUL_R5,
345
346    /* MIPS r6 introduced names for weaker variants of SYNC.  These are
347       backward compatible to previous architecture revisions.  */
348    OPC_SYNC_WMB     = OPC_SYNC | 0x04 << 6,
349    OPC_SYNC_MB      = OPC_SYNC | 0x10 << 6,
350    OPC_SYNC_ACQUIRE = OPC_SYNC | 0x11 << 6,
351    OPC_SYNC_RELEASE = OPC_SYNC | 0x12 << 6,
352    OPC_SYNC_RMB     = OPC_SYNC | 0x13 << 6,
353
354    /* Aliases for convenience.  */
355    ALIAS_PADD     = sizeof(void *) == 4 ? OPC_ADDU : OPC_DADDU,
356    ALIAS_PADDI    = sizeof(void *) == 4 ? OPC_ADDIU : OPC_DADDIU,
357    ALIAS_TSRL     = TARGET_LONG_BITS == 32 || TCG_TARGET_REG_BITS == 32
358                     ? OPC_SRL : OPC_DSRL,
359    ALIAS_TADDI    = TARGET_LONG_BITS == 32 || TCG_TARGET_REG_BITS == 32
360                     ? OPC_ADDIU : OPC_DADDIU,
361} MIPSInsn;
362
363/*
364 * Type reg
365 */
366static void tcg_out_opc_reg(TCGContext *s, MIPSInsn opc,
367                            TCGReg rd, TCGReg rs, TCGReg rt)
368{
369    int32_t inst;
370
371    inst = opc;
372    inst |= (rs & 0x1F) << 21;
373    inst |= (rt & 0x1F) << 16;
374    inst |= (rd & 0x1F) << 11;
375    tcg_out32(s, inst);
376}
377
378/*
379 * Type immediate
380 */
381static void tcg_out_opc_imm(TCGContext *s, MIPSInsn opc,
382                            TCGReg rt, TCGReg rs, TCGArg imm)
383{
384    int32_t inst;
385
386    inst = opc;
387    inst |= (rs & 0x1F) << 21;
388    inst |= (rt & 0x1F) << 16;
389    inst |= (imm & 0xffff);
390    tcg_out32(s, inst);
391}
392
393/*
394 * Type bitfield
395 */
396static void tcg_out_opc_bf(TCGContext *s, MIPSInsn opc, TCGReg rt,
397                           TCGReg rs, int msb, int lsb)
398{
399    int32_t inst;
400
401    inst = opc;
402    inst |= (rs & 0x1F) << 21;
403    inst |= (rt & 0x1F) << 16;
404    inst |= (msb & 0x1F) << 11;
405    inst |= (lsb & 0x1F) << 6;
406    tcg_out32(s, inst);
407}
408
409static void tcg_out_opc_bf64(TCGContext *s, MIPSInsn opc, MIPSInsn opm,
410                             MIPSInsn oph, TCGReg rt, TCGReg rs,
411                                    int msb, int lsb)
412{
413    if (lsb >= 32) {
414        opc = oph;
415        msb -= 32;
416        lsb -= 32;
417    } else if (msb >= 32) {
418        opc = opm;
419        msb -= 32;
420    }
421    tcg_out_opc_bf(s, opc, rt, rs, msb, lsb);
422}
423
424/*
425 * Type branch
426 */
427static void tcg_out_opc_br(TCGContext *s, MIPSInsn opc, TCGReg rt, TCGReg rs)
428{
429    tcg_out_opc_imm(s, opc, rt, rs, 0);
430}
431
432/*
433 * Type sa
434 */
435static void tcg_out_opc_sa(TCGContext *s, MIPSInsn opc,
436                           TCGReg rd, TCGReg rt, TCGArg sa)
437{
438    int32_t inst;
439
440    inst = opc;
441    inst |= (rt & 0x1F) << 16;
442    inst |= (rd & 0x1F) << 11;
443    inst |= (sa & 0x1F) <<  6;
444    tcg_out32(s, inst);
445
446}
447
448static void tcg_out_opc_sa64(TCGContext *s, MIPSInsn opc1, MIPSInsn opc2,
449                             TCGReg rd, TCGReg rt, TCGArg sa)
450{
451    int32_t inst;
452
453    inst = (sa & 32 ? opc2 : opc1);
454    inst |= (rt & 0x1F) << 16;
455    inst |= (rd & 0x1F) << 11;
456    inst |= (sa & 0x1F) <<  6;
457    tcg_out32(s, inst);
458}
459
460/*
461 * Type jump.
462 * Returns true if the branch was in range and the insn was emitted.
463 */
464static bool tcg_out_opc_jmp(TCGContext *s, MIPSInsn opc, const void *target)
465{
466    uintptr_t dest = (uintptr_t)target;
467    uintptr_t from = (uintptr_t)tcg_splitwx_to_rx(s->code_ptr) + 4;
468    int32_t inst;
469
470    /* The pc-region branch happens within the 256MB region of
471       the delay slot (thus the +4).  */
472    if ((from ^ dest) & -(1 << 28)) {
473        return false;
474    }
475    tcg_debug_assert((dest & 3) == 0);
476
477    inst = opc;
478    inst |= (dest >> 2) & 0x3ffffff;
479    tcg_out32(s, inst);
480    return true;
481}
482
483static void tcg_out_nop(TCGContext *s)
484{
485    tcg_out32(s, 0);
486}
487
488static void tcg_out_dsll(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
489{
490    tcg_out_opc_sa64(s, OPC_DSLL, OPC_DSLL32, rd, rt, sa);
491}
492
493static void tcg_out_dsrl(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
494{
495    tcg_out_opc_sa64(s, OPC_DSRL, OPC_DSRL32, rd, rt, sa);
496}
497
498static void tcg_out_dsra(TCGContext *s, TCGReg rd, TCGReg rt, TCGArg sa)
499{
500    tcg_out_opc_sa64(s, OPC_DSRA, OPC_DSRA32, rd, rt, sa);
501}
502
503static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
504{
505    /* Simple reg-reg move, optimising out the 'do nothing' case */
506    if (ret != arg) {
507        tcg_out_opc_reg(s, OPC_OR, ret, arg, TCG_REG_ZERO);
508    }
509    return true;
510}
511
512static void tcg_out_movi(TCGContext *s, TCGType type,
513                         TCGReg ret, tcg_target_long arg)
514{
515    if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I32) {
516        arg = (int32_t)arg;
517    }
518    if (arg == (int16_t)arg) {
519        tcg_out_opc_imm(s, OPC_ADDIU, ret, TCG_REG_ZERO, arg);
520        return;
521    }
522    if (arg == (uint16_t)arg) {
523        tcg_out_opc_imm(s, OPC_ORI, ret, TCG_REG_ZERO, arg);
524        return;
525    }
526    if (TCG_TARGET_REG_BITS == 32 || arg == (int32_t)arg) {
527        tcg_out_opc_imm(s, OPC_LUI, ret, TCG_REG_ZERO, arg >> 16);
528    } else {
529        tcg_out_movi(s, TCG_TYPE_I32, ret, arg >> 31 >> 1);
530        if (arg & 0xffff0000ull) {
531            tcg_out_dsll(s, ret, ret, 16);
532            tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg >> 16);
533            tcg_out_dsll(s, ret, ret, 16);
534        } else {
535            tcg_out_dsll(s, ret, ret, 32);
536        }
537    }
538    if (arg & 0xffff) {
539        tcg_out_opc_imm(s, OPC_ORI, ret, ret, arg & 0xffff);
540    }
541}
542
543static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
544{
545    tcg_debug_assert(TCG_TARGET_HAS_ext8s_i32);
546    tcg_out_opc_reg(s, OPC_SEB, rd, TCG_REG_ZERO, rs);
547}
548
549static void tcg_out_ext8u(TCGContext *s, TCGReg rd, TCGReg rs)
550{
551    tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xff);
552}
553
554static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg rd, TCGReg rs)
555{
556    tcg_debug_assert(TCG_TARGET_HAS_ext16s_i32);
557    tcg_out_opc_reg(s, OPC_SEH, rd, TCG_REG_ZERO, rs);
558}
559
560static void tcg_out_ext16u(TCGContext *s, TCGReg rd, TCGReg rs)
561{
562    tcg_out_opc_imm(s, OPC_ANDI, rd, rs, 0xffff);
563}
564
565static void tcg_out_ext32s(TCGContext *s, TCGReg rd, TCGReg rs)
566{
567    tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
568    tcg_out_opc_sa(s, OPC_SLL, rd, rs, 0);
569}
570
571static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
572{
573    if (rd != rs) {
574        tcg_out_ext32s(s, rd, rs);
575    }
576}
577
578static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg rd, TCGReg rs)
579{
580    tcg_out_ext32u(s, rd, rs);
581}
582
583static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg rd, TCGReg rs)
584{
585    tcg_out_ext32s(s, rd, rs);
586}
587
588static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
589{
590    return false;
591}
592
593static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
594                             tcg_target_long imm)
595{
596    /* This function is only used for passing structs by reference. */
597    g_assert_not_reached();
598}
599
600static void tcg_out_bswap16(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
601{
602    /* ret and arg can't be register tmp0 */
603    tcg_debug_assert(ret != TCG_TMP0);
604    tcg_debug_assert(arg != TCG_TMP0);
605
606    /* With arg = abcd: */
607    if (use_mips32r2_instructions) {
608        tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg);                 /* badc */
609        if (flags & TCG_BSWAP_OS) {
610            tcg_out_opc_reg(s, OPC_SEH, ret, 0, ret);              /* ssdc */
611        } else if ((flags & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
612            tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xffff);        /* 00dc */
613        }
614        return;
615    }
616
617    tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, arg, 8);                  /* 0abc */
618    if (!(flags & TCG_BSWAP_IZ)) {
619        tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, TCG_TMP0, 0x00ff);  /* 000c */
620    }
621    if (flags & TCG_BSWAP_OS) {
622        tcg_out_opc_sa(s, OPC_SLL, ret, arg, 24);                  /* d000 */
623        tcg_out_opc_sa(s, OPC_SRA, ret, ret, 16);                  /* ssd0 */
624    } else {
625        tcg_out_opc_sa(s, OPC_SLL, ret, arg, 8);                   /* bcd0 */
626        if (flags & TCG_BSWAP_OZ) {
627            tcg_out_opc_imm(s, OPC_ANDI, ret, ret, 0xff00);        /* 00d0 */
628        }
629    }
630    tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP0);                /* ssdc */
631}
632
633static void tcg_out_bswap_subr(TCGContext *s, const tcg_insn_unit *sub)
634{
635    if (!tcg_out_opc_jmp(s, OPC_JAL, sub)) {
636        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP1, (uintptr_t)sub);
637        tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_TMP1, 0);
638    }
639}
640
641static void tcg_out_bswap32(TCGContext *s, TCGReg ret, TCGReg arg, int flags)
642{
643    if (use_mips32r2_instructions) {
644        tcg_out_opc_reg(s, OPC_WSBH, ret, 0, arg);
645        tcg_out_opc_sa(s, OPC_ROTR, ret, ret, 16);
646        if (flags & TCG_BSWAP_OZ) {
647            tcg_out_opc_bf(s, OPC_DEXT, ret, ret, 31, 0);
648        }
649    } else {
650        if (flags & TCG_BSWAP_OZ) {
651            tcg_out_bswap_subr(s, bswap32u_addr);
652        } else {
653            tcg_out_bswap_subr(s, bswap32_addr);
654        }
655        /* delay slot -- never omit the insn, like tcg_out_mov might.  */
656        tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
657        tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
658    }
659}
660
661static void tcg_out_bswap64(TCGContext *s, TCGReg ret, TCGReg arg)
662{
663    if (use_mips32r2_instructions) {
664        tcg_out_opc_reg(s, OPC_DSBH, ret, 0, arg);
665        tcg_out_opc_reg(s, OPC_DSHD, ret, 0, ret);
666    } else {
667        tcg_out_bswap_subr(s, bswap64_addr);
668        /* delay slot -- never omit the insn, like tcg_out_mov might.  */
669        tcg_out_opc_reg(s, OPC_OR, TCG_TMP0, arg, TCG_REG_ZERO);
670        tcg_out_mov(s, TCG_TYPE_I32, ret, TCG_TMP3);
671    }
672}
673
674static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
675{
676    tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
677    if (use_mips32r2_instructions) {
678        tcg_out_opc_bf(s, OPC_DEXT, ret, arg, 31, 0);
679    } else {
680        tcg_out_dsll(s, ret, arg, 32);
681        tcg_out_dsrl(s, ret, ret, 32);
682    }
683}
684
685static void tcg_out_ldst(TCGContext *s, MIPSInsn opc, TCGReg data,
686                         TCGReg addr, intptr_t ofs)
687{
688    int16_t lo = ofs;
689    if (ofs != lo) {
690        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - lo);
691        if (addr != TCG_REG_ZERO) {
692            tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP0, TCG_TMP0, addr);
693        }
694        addr = TCG_TMP0;
695    }
696    tcg_out_opc_imm(s, opc, data, addr, lo);
697}
698
699static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
700                       TCGReg arg1, intptr_t arg2)
701{
702    MIPSInsn opc = OPC_LD;
703    if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
704        opc = OPC_LW;
705    }
706    tcg_out_ldst(s, opc, arg, arg1, arg2);
707}
708
709static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
710                       TCGReg arg1, intptr_t arg2)
711{
712    MIPSInsn opc = OPC_SD;
713    if (TCG_TARGET_REG_BITS == 32 || type == TCG_TYPE_I32) {
714        opc = OPC_SW;
715    }
716    tcg_out_ldst(s, opc, arg, arg1, arg2);
717}
718
719static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
720                        TCGReg base, intptr_t ofs)
721{
722    if (val == 0) {
723        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
724        return true;
725    }
726    return false;
727}
728
729static void tcg_out_addsub2(TCGContext *s, TCGReg rl, TCGReg rh, TCGReg al,
730                            TCGReg ah, TCGArg bl, TCGArg bh, bool cbl,
731                            bool cbh, bool is_sub)
732{
733    TCGReg th = TCG_TMP1;
734
735    /* If we have a negative constant such that negating it would
736       make the high part zero, we can (usually) eliminate one insn.  */
737    if (cbl && cbh && bh == -1 && bl != 0) {
738        bl = -bl;
739        bh = 0;
740        is_sub = !is_sub;
741    }
742
743    /* By operating on the high part first, we get to use the final
744       carry operation to move back from the temporary.  */
745    if (!cbh) {
746        tcg_out_opc_reg(s, (is_sub ? OPC_SUBU : OPC_ADDU), th, ah, bh);
747    } else if (bh != 0 || ah == rl) {
748        tcg_out_opc_imm(s, OPC_ADDIU, th, ah, (is_sub ? -bh : bh));
749    } else {
750        th = ah;
751    }
752
753    /* Note that tcg optimization should eliminate the bl == 0 case.  */
754    if (is_sub) {
755        if (cbl) {
756            tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, al, bl);
757            tcg_out_opc_imm(s, OPC_ADDIU, rl, al, -bl);
758        } else {
759            tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, al, bl);
760            tcg_out_opc_reg(s, OPC_SUBU, rl, al, bl);
761        }
762        tcg_out_opc_reg(s, OPC_SUBU, rh, th, TCG_TMP0);
763    } else {
764        if (cbl) {
765            tcg_out_opc_imm(s, OPC_ADDIU, rl, al, bl);
766            tcg_out_opc_imm(s, OPC_SLTIU, TCG_TMP0, rl, bl);
767        } else if (rl == al && rl == bl) {
768            tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, al, TCG_TARGET_REG_BITS - 1);
769            tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
770        } else {
771            tcg_out_opc_reg(s, OPC_ADDU, rl, al, bl);
772            tcg_out_opc_reg(s, OPC_SLTU, TCG_TMP0, rl, (rl == bl ? al : bl));
773        }
774        tcg_out_opc_reg(s, OPC_ADDU, rh, th, TCG_TMP0);
775    }
776}
777
778/* Bit 0 set if inversion required; bit 1 set if swapping required.  */
779#define MIPS_CMP_INV  1
780#define MIPS_CMP_SWAP 2
781
782static const uint8_t mips_cmp_map[16] = {
783    [TCG_COND_LT]  = 0,
784    [TCG_COND_LTU] = 0,
785    [TCG_COND_GE]  = MIPS_CMP_INV,
786    [TCG_COND_GEU] = MIPS_CMP_INV,
787    [TCG_COND_LE]  = MIPS_CMP_INV | MIPS_CMP_SWAP,
788    [TCG_COND_LEU] = MIPS_CMP_INV | MIPS_CMP_SWAP,
789    [TCG_COND_GT]  = MIPS_CMP_SWAP,
790    [TCG_COND_GTU] = MIPS_CMP_SWAP,
791};
792
793static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
794                            TCGReg arg1, TCGReg arg2)
795{
796    MIPSInsn s_opc = OPC_SLTU;
797    int cmp_map;
798
799    switch (cond) {
800    case TCG_COND_EQ:
801        if (arg2 != 0) {
802            tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
803            arg1 = ret;
804        }
805        tcg_out_opc_imm(s, OPC_SLTIU, ret, arg1, 1);
806        break;
807
808    case TCG_COND_NE:
809        if (arg2 != 0) {
810            tcg_out_opc_reg(s, OPC_XOR, ret, arg1, arg2);
811            arg1 = ret;
812        }
813        tcg_out_opc_reg(s, OPC_SLTU, ret, TCG_REG_ZERO, arg1);
814        break;
815
816    case TCG_COND_LT:
817    case TCG_COND_GE:
818    case TCG_COND_LE:
819    case TCG_COND_GT:
820        s_opc = OPC_SLT;
821        /* FALLTHRU */
822
823    case TCG_COND_LTU:
824    case TCG_COND_GEU:
825    case TCG_COND_LEU:
826    case TCG_COND_GTU:
827        cmp_map = mips_cmp_map[cond];
828        if (cmp_map & MIPS_CMP_SWAP) {
829            TCGReg t = arg1;
830            arg1 = arg2;
831            arg2 = t;
832        }
833        tcg_out_opc_reg(s, s_opc, ret, arg1, arg2);
834        if (cmp_map & MIPS_CMP_INV) {
835            tcg_out_opc_imm(s, OPC_XORI, ret, ret, 1);
836        }
837        break;
838
839     default:
840         g_assert_not_reached();
841         break;
842     }
843}
844
845static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
846                           TCGReg arg2, TCGLabel *l)
847{
848    static const MIPSInsn b_zero[16] = {
849        [TCG_COND_LT] = OPC_BLTZ,
850        [TCG_COND_GT] = OPC_BGTZ,
851        [TCG_COND_LE] = OPC_BLEZ,
852        [TCG_COND_GE] = OPC_BGEZ,
853    };
854
855    MIPSInsn s_opc = OPC_SLTU;
856    MIPSInsn b_opc;
857    int cmp_map;
858
859    switch (cond) {
860    case TCG_COND_EQ:
861        b_opc = OPC_BEQ;
862        break;
863    case TCG_COND_NE:
864        b_opc = OPC_BNE;
865        break;
866
867    case TCG_COND_LT:
868    case TCG_COND_GT:
869    case TCG_COND_LE:
870    case TCG_COND_GE:
871        if (arg2 == 0) {
872            b_opc = b_zero[cond];
873            arg2 = arg1;
874            arg1 = 0;
875            break;
876        }
877        s_opc = OPC_SLT;
878        /* FALLTHRU */
879
880    case TCG_COND_LTU:
881    case TCG_COND_GTU:
882    case TCG_COND_LEU:
883    case TCG_COND_GEU:
884        cmp_map = mips_cmp_map[cond];
885        if (cmp_map & MIPS_CMP_SWAP) {
886            TCGReg t = arg1;
887            arg1 = arg2;
888            arg2 = t;
889        }
890        tcg_out_opc_reg(s, s_opc, TCG_TMP0, arg1, arg2);
891        b_opc = (cmp_map & MIPS_CMP_INV ? OPC_BEQ : OPC_BNE);
892        arg1 = TCG_TMP0;
893        arg2 = TCG_REG_ZERO;
894        break;
895
896    default:
897        g_assert_not_reached();
898        break;
899    }
900
901    tcg_out_opc_br(s, b_opc, arg1, arg2);
902    tcg_out_reloc(s, s->code_ptr - 1, R_MIPS_PC16, l, 0);
903    tcg_out_nop(s);
904}
905
906static TCGReg tcg_out_reduce_eq2(TCGContext *s, TCGReg tmp0, TCGReg tmp1,
907                                 TCGReg al, TCGReg ah,
908                                 TCGReg bl, TCGReg bh)
909{
910    /* Merge highpart comparison into AH.  */
911    if (bh != 0) {
912        if (ah != 0) {
913            tcg_out_opc_reg(s, OPC_XOR, tmp0, ah, bh);
914            ah = tmp0;
915        } else {
916            ah = bh;
917        }
918    }
919    /* Merge lowpart comparison into AL.  */
920    if (bl != 0) {
921        if (al != 0) {
922            tcg_out_opc_reg(s, OPC_XOR, tmp1, al, bl);
923            al = tmp1;
924        } else {
925            al = bl;
926        }
927    }
928    /* Merge high and low part comparisons into AL.  */
929    if (ah != 0) {
930        if (al != 0) {
931            tcg_out_opc_reg(s, OPC_OR, tmp0, ah, al);
932            al = tmp0;
933        } else {
934            al = ah;
935        }
936    }
937    return al;
938}
939
940static void tcg_out_setcond2(TCGContext *s, TCGCond cond, TCGReg ret,
941                             TCGReg al, TCGReg ah, TCGReg bl, TCGReg bh)
942{
943    TCGReg tmp0 = TCG_TMP0;
944    TCGReg tmp1 = ret;
945
946    tcg_debug_assert(ret != TCG_TMP0);
947    if (ret == ah || ret == bh) {
948        tcg_debug_assert(ret != TCG_TMP1);
949        tmp1 = TCG_TMP1;
950    }
951
952    switch (cond) {
953    case TCG_COND_EQ:
954    case TCG_COND_NE:
955        tmp1 = tcg_out_reduce_eq2(s, tmp0, tmp1, al, ah, bl, bh);
956        tcg_out_setcond(s, cond, ret, tmp1, TCG_REG_ZERO);
957        break;
958
959    default:
960        tcg_out_setcond(s, TCG_COND_EQ, tmp0, ah, bh);
961        tcg_out_setcond(s, tcg_unsigned_cond(cond), tmp1, al, bl);
962        tcg_out_opc_reg(s, OPC_AND, tmp1, tmp1, tmp0);
963        tcg_out_setcond(s, tcg_high_cond(cond), tmp0, ah, bh);
964        tcg_out_opc_reg(s, OPC_OR, ret, tmp1, tmp0);
965        break;
966    }
967}
968
969static void tcg_out_brcond2(TCGContext *s, TCGCond cond, TCGReg al, TCGReg ah,
970                            TCGReg bl, TCGReg bh, TCGLabel *l)
971{
972    TCGCond b_cond = TCG_COND_NE;
973    TCGReg tmp = TCG_TMP1;
974
975    /* With branches, we emit between 4 and 9 insns with 2 or 3 branches.
976       With setcond, we emit between 3 and 10 insns and only 1 branch,
977       which ought to get better branch prediction.  */
978     switch (cond) {
979     case TCG_COND_EQ:
980     case TCG_COND_NE:
981        b_cond = cond;
982        tmp = tcg_out_reduce_eq2(s, TCG_TMP0, TCG_TMP1, al, ah, bl, bh);
983        break;
984
985    default:
986        /* Minimize code size by preferring a compare not requiring INV.  */
987        if (mips_cmp_map[cond] & MIPS_CMP_INV) {
988            cond = tcg_invert_cond(cond);
989            b_cond = TCG_COND_EQ;
990        }
991        tcg_out_setcond2(s, cond, tmp, al, ah, bl, bh);
992        break;
993    }
994
995    tcg_out_brcond(s, b_cond, tmp, TCG_REG_ZERO, l);
996}
997
998static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
999                            TCGReg c1, TCGReg c2, TCGReg v1, TCGReg v2)
1000{
1001    bool eqz = false;
1002
1003    /* If one of the values is zero, put it last to match SEL*Z instructions */
1004    if (use_mips32r6_instructions && v1 == 0) {
1005        v1 = v2;
1006        v2 = 0;
1007        cond = tcg_invert_cond(cond);
1008    }
1009
1010    switch (cond) {
1011    case TCG_COND_EQ:
1012        eqz = true;
1013        /* FALLTHRU */
1014    case TCG_COND_NE:
1015        if (c2 != 0) {
1016            tcg_out_opc_reg(s, OPC_XOR, TCG_TMP0, c1, c2);
1017            c1 = TCG_TMP0;
1018        }
1019        break;
1020
1021    default:
1022        /* Minimize code size by preferring a compare not requiring INV.  */
1023        if (mips_cmp_map[cond] & MIPS_CMP_INV) {
1024            cond = tcg_invert_cond(cond);
1025            eqz = true;
1026        }
1027        tcg_out_setcond(s, cond, TCG_TMP0, c1, c2);
1028        c1 = TCG_TMP0;
1029        break;
1030    }
1031
1032    if (use_mips32r6_instructions) {
1033        MIPSInsn m_opc_t = eqz ? OPC_SELEQZ : OPC_SELNEZ;
1034        MIPSInsn m_opc_f = eqz ? OPC_SELNEZ : OPC_SELEQZ;
1035
1036        if (v2 != 0) {
1037            tcg_out_opc_reg(s, m_opc_f, TCG_TMP1, v2, c1);
1038        }
1039        tcg_out_opc_reg(s, m_opc_t, ret, v1, c1);
1040        if (v2 != 0) {
1041            tcg_out_opc_reg(s, OPC_OR, ret, ret, TCG_TMP1);
1042        }
1043    } else {
1044        MIPSInsn m_opc = eqz ? OPC_MOVZ : OPC_MOVN;
1045
1046        tcg_out_opc_reg(s, m_opc, ret, v1, c1);
1047
1048        /* This should be guaranteed via constraints */
1049        tcg_debug_assert(v2 == ret);
1050    }
1051}
1052
1053static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
1054{
1055    /* Note that the ABI requires the called function's address to be
1056       loaded into T9, even if a direct branch is in range.  */
1057    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T9, (uintptr_t)arg);
1058
1059    /* But do try a direct branch, allowing the cpu better insn prefetch.  */
1060    if (tail) {
1061        if (!tcg_out_opc_jmp(s, OPC_J, arg)) {
1062            tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_T9, 0);
1063        }
1064    } else {
1065        if (!tcg_out_opc_jmp(s, OPC_JAL, arg)) {
1066            tcg_out_opc_reg(s, OPC_JALR, TCG_REG_RA, TCG_REG_T9, 0);
1067        }
1068    }
1069}
1070
1071static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
1072                         const TCGHelperInfo *info)
1073{
1074    tcg_out_call_int(s, arg, false);
1075    tcg_out_nop(s);
1076}
1077
1078#if defined(CONFIG_SOFTMMU)
1079/* We have four temps, we might as well expose three of them. */
1080static const TCGLdstHelperParam ldst_helper_param = {
1081    .ntmp = 3, .tmp = { TCG_TMP0, TCG_TMP1, TCG_TMP2 }
1082};
1083
1084static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1085{
1086    const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
1087    MemOp opc = get_memop(l->oi);
1088
1089    /* resolve label address */
1090    if (!reloc_pc16(l->label_ptr[0], tgt_rx)
1091        || (TCG_TARGET_REG_BITS < TARGET_LONG_BITS
1092            && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
1093        return false;
1094    }
1095
1096    tcg_out_ld_helper_args(s, l, &ldst_helper_param);
1097
1098    tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SSIZE], false);
1099    /* delay slot */
1100    tcg_out_nop(s);
1101
1102    tcg_out_ld_helper_ret(s, l, true, &ldst_helper_param);
1103
1104    tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1105    if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1106        return false;
1107    }
1108
1109    /* delay slot */
1110    tcg_out_nop(s);
1111    return true;
1112}
1113
1114static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1115{
1116    const tcg_insn_unit *tgt_rx = tcg_splitwx_to_rx(s->code_ptr);
1117    MemOp opc = get_memop(l->oi);
1118
1119    /* resolve label address */
1120    if (!reloc_pc16(l->label_ptr[0], tgt_rx)
1121        || (TCG_TARGET_REG_BITS < TARGET_LONG_BITS
1122            && !reloc_pc16(l->label_ptr[1], tgt_rx))) {
1123        return false;
1124    }
1125
1126    tcg_out_st_helper_args(s, l, &ldst_helper_param);
1127
1128    tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE], false);
1129    /* delay slot */
1130    tcg_out_nop(s);
1131
1132    tcg_out_opc_br(s, OPC_BEQ, TCG_REG_ZERO, TCG_REG_ZERO);
1133    if (!reloc_pc16(s->code_ptr - 1, l->raddr)) {
1134        return false;
1135    }
1136
1137    /* delay slot */
1138    tcg_out_nop(s);
1139    return true;
1140}
1141
1142#else
1143static bool tcg_out_fail_alignment(TCGContext *s, TCGLabelQemuLdst *l)
1144{
1145    void *target;
1146
1147    if (!reloc_pc16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
1148        return false;
1149    }
1150
1151    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1152        /* A0 is env, A1 is skipped, A2:A3 is the uint64_t address. */
1153        TCGReg a2 = MIPS_BE ? l->addrhi_reg : l->addrlo_reg;
1154        TCGReg a3 = MIPS_BE ? l->addrlo_reg : l->addrhi_reg;
1155
1156        if (a3 != TCG_REG_A2) {
1157            tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A2, a2);
1158            tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A3, a3);
1159        } else if (a2 != TCG_REG_A3) {
1160            tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A3, a3);
1161            tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A2, a2);
1162        } else {
1163            tcg_out_mov(s, TCG_TYPE_I32, TCG_TMP0, TCG_REG_A2);
1164            tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A2, TCG_REG_A3);
1165            tcg_out_mov(s, TCG_TYPE_I32, TCG_REG_A3, TCG_TMP0);
1166        }
1167    } else {
1168        tcg_out_mov(s, TCG_TYPE_TL, TCG_REG_A1, l->addrlo_reg);
1169    }
1170    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
1171
1172    /*
1173     * Tail call to the helper, with the return address back inline.
1174     * We have arrived here via BNEL, so $31 is already set.
1175     */
1176    target = (l->is_ld ? helper_unaligned_ld : helper_unaligned_st);
1177    tcg_out_call_int(s, target, true);
1178    return true;
1179}
1180
1181static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1182{
1183    return tcg_out_fail_alignment(s, l);
1184}
1185
1186static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
1187{
1188    return tcg_out_fail_alignment(s, l);
1189}
1190#endif /* SOFTMMU */
1191
1192typedef struct {
1193    TCGReg base;
1194    MemOp align;
1195} HostAddress;
1196
1197/*
1198 * For softmmu, perform the TLB load and compare.
1199 * For useronly, perform any required alignment tests.
1200 * In both cases, return a TCGLabelQemuLdst structure if the slow path
1201 * is required and fill in @h with the host address for the fast path.
1202 */
1203static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1204                                           TCGReg addrlo, TCGReg addrhi,
1205                                           MemOpIdx oi, bool is_ld)
1206{
1207    TCGLabelQemuLdst *ldst = NULL;
1208    MemOp opc = get_memop(oi);
1209    unsigned a_bits = get_alignment_bits(opc);
1210    unsigned s_bits = opc & MO_SIZE;
1211    unsigned a_mask = (1 << a_bits) - 1;
1212    TCGReg base;
1213
1214#ifdef CONFIG_SOFTMMU
1215    unsigned s_mask = (1 << s_bits) - 1;
1216    int mem_index = get_mmuidx(oi);
1217    int fast_off = TLB_MASK_TABLE_OFS(mem_index);
1218    int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1219    int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1220    int add_off = offsetof(CPUTLBEntry, addend);
1221    int cmp_off = is_ld ? offsetof(CPUTLBEntry, addr_read)
1222                        : offsetof(CPUTLBEntry, addr_write);
1223
1224    ldst = new_ldst_label(s);
1225    ldst->is_ld = is_ld;
1226    ldst->oi = oi;
1227    ldst->addrlo_reg = addrlo;
1228    ldst->addrhi_reg = addrhi;
1229
1230    /* Load tlb_mask[mmu_idx] and tlb_table[mmu_idx].  */
1231    QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
1232    QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -32768);
1233    tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_AREG0, mask_off);
1234    tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP1, TCG_AREG0, table_off);
1235
1236    /* Extract the TLB index from the address into TMP3.  */
1237    tcg_out_opc_sa(s, ALIAS_TSRL, TCG_TMP3, addrlo,
1238                   TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1239    tcg_out_opc_reg(s, OPC_AND, TCG_TMP3, TCG_TMP3, TCG_TMP0);
1240
1241    /* Add the tlb_table pointer, creating the CPUTLBEntry address in TMP3.  */
1242    tcg_out_opc_reg(s, ALIAS_PADD, TCG_TMP3, TCG_TMP3, TCG_TMP1);
1243
1244    /* Load the (low-half) tlb comparator.  */
1245    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1246        tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + LO_OFF);
1247    } else {
1248        tcg_out_ld(s, TCG_TYPE_TL, TCG_TMP0, TCG_TMP3, cmp_off);
1249    }
1250
1251    if (TCG_TARGET_REG_BITS >= TARGET_LONG_BITS) {
1252        /* Load the tlb addend for the fast path.  */
1253        tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off);
1254    }
1255
1256    /*
1257     * Mask the page bits, keeping the alignment bits to compare against.
1258     * For unaligned accesses, compare against the end of the access to
1259     * verify that it does not cross a page boundary.
1260     */
1261    tcg_out_movi(s, TCG_TYPE_TL, TCG_TMP1, TARGET_PAGE_MASK | a_mask);
1262    if (a_mask < s_mask) {
1263        tcg_out_opc_imm(s, ALIAS_TADDI, TCG_TMP2, addrlo, s_mask - a_mask);
1264        tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, TCG_TMP2);
1265    } else {
1266        tcg_out_opc_reg(s, OPC_AND, TCG_TMP1, TCG_TMP1, addrlo);
1267    }
1268
1269    /* Zero extend a 32-bit guest address for a 64-bit host. */
1270    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1271        tcg_out_ext32u(s, TCG_TMP2, addrlo);
1272        addrlo = TCG_TMP2;
1273    }
1274
1275    ldst->label_ptr[0] = s->code_ptr;
1276    tcg_out_opc_br(s, OPC_BNE, TCG_TMP1, TCG_TMP0);
1277
1278    /* Load and test the high half tlb comparator.  */
1279    if (TCG_TARGET_REG_BITS < TARGET_LONG_BITS) {
1280        /* delay slot */
1281        tcg_out_ldst(s, OPC_LW, TCG_TMP0, TCG_TMP3, cmp_off + HI_OFF);
1282
1283        /* Load the tlb addend for the fast path.  */
1284        tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP3, TCG_TMP3, add_off);
1285
1286        ldst->label_ptr[1] = s->code_ptr;
1287        tcg_out_opc_br(s, OPC_BNE, addrhi, TCG_TMP0);
1288    }
1289
1290    /* delay slot */
1291    base = TCG_TMP3;
1292    tcg_out_opc_reg(s, ALIAS_PADD, base, TCG_TMP3, addrlo);
1293#else
1294    if (a_mask && (use_mips32r6_instructions || a_bits != s_bits)) {
1295        ldst = new_ldst_label(s);
1296
1297        ldst->is_ld = is_ld;
1298        ldst->oi = oi;
1299        ldst->addrlo_reg = addrlo;
1300        ldst->addrhi_reg = addrhi;
1301
1302        /* We are expecting a_bits to max out at 7, much lower than ANDI. */
1303        tcg_debug_assert(a_bits < 16);
1304        tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP0, addrlo, a_mask);
1305
1306        ldst->label_ptr[0] = s->code_ptr;
1307        if (use_mips32r6_instructions) {
1308            tcg_out_opc_br(s, OPC_BNEZALC_R6, TCG_REG_ZERO, TCG_TMP0);
1309        } else {
1310            tcg_out_opc_br(s, OPC_BNEL, TCG_TMP0, TCG_REG_ZERO);
1311            tcg_out_nop(s);
1312        }
1313    }
1314
1315    base = addrlo;
1316    if (TCG_TARGET_REG_BITS > TARGET_LONG_BITS) {
1317        tcg_out_ext32u(s, TCG_REG_A0, base);
1318        base = TCG_REG_A0;
1319    }
1320    if (guest_base) {
1321        if (guest_base == (int16_t)guest_base) {
1322            tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_A0, base, guest_base);
1323        } else {
1324            tcg_out_opc_reg(s, ALIAS_PADD, TCG_REG_A0, base,
1325                            TCG_GUEST_BASE_REG);
1326        }
1327        base = TCG_REG_A0;
1328    }
1329#endif
1330
1331    h->base = base;
1332    h->align = a_bits;
1333    return ldst;
1334}
1335
1336static void tcg_out_qemu_ld_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1337                                   TCGReg base, MemOp opc, TCGType type)
1338{
1339    switch (opc & MO_SSIZE) {
1340    case MO_UB:
1341        tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1342        break;
1343    case MO_SB:
1344        tcg_out_opc_imm(s, OPC_LB, lo, base, 0);
1345        break;
1346    case MO_UW:
1347        tcg_out_opc_imm(s, OPC_LHU, lo, base, 0);
1348        break;
1349    case MO_SW:
1350        tcg_out_opc_imm(s, OPC_LH, lo, base, 0);
1351        break;
1352    case MO_UL:
1353        if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64) {
1354            tcg_out_opc_imm(s, OPC_LWU, lo, base, 0);
1355            break;
1356        }
1357        /* FALLTHRU */
1358    case MO_SL:
1359        tcg_out_opc_imm(s, OPC_LW, lo, base, 0);
1360        break;
1361    case MO_UQ:
1362        /* Prefer to load from offset 0 first, but allow for overlap.  */
1363        if (TCG_TARGET_REG_BITS == 64) {
1364            tcg_out_opc_imm(s, OPC_LD, lo, base, 0);
1365        } else if (MIPS_BE ? hi != base : lo == base) {
1366            tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1367            tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1368        } else {
1369            tcg_out_opc_imm(s, OPC_LW, lo, base, LO_OFF);
1370            tcg_out_opc_imm(s, OPC_LW, hi, base, HI_OFF);
1371        }
1372        break;
1373    default:
1374        g_assert_not_reached();
1375    }
1376}
1377
1378static void tcg_out_qemu_ld_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
1379                                    TCGReg base, MemOp opc, TCGType type)
1380{
1381    const MIPSInsn lw1 = MIPS_BE ? OPC_LWL : OPC_LWR;
1382    const MIPSInsn lw2 = MIPS_BE ? OPC_LWR : OPC_LWL;
1383    const MIPSInsn ld1 = MIPS_BE ? OPC_LDL : OPC_LDR;
1384    const MIPSInsn ld2 = MIPS_BE ? OPC_LDR : OPC_LDL;
1385    bool sgn = opc & MO_SIGN;
1386
1387    switch (opc & MO_SIZE) {
1388    case MO_16:
1389        if (HOST_BIG_ENDIAN) {
1390            tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 0);
1391            tcg_out_opc_imm(s, OPC_LBU, lo, base, 1);
1392            if (use_mips32r2_instructions) {
1393                tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
1394            } else {
1395                tcg_out_opc_sa(s, OPC_SLL, TCG_TMP0, TCG_TMP0, 8);
1396                tcg_out_opc_reg(s, OPC_OR, lo, lo, TCG_TMP0);
1397            }
1398        } else if (use_mips32r2_instructions && lo != base) {
1399            tcg_out_opc_imm(s, OPC_LBU, lo, base, 0);
1400            tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP0, base, 1);
1401            tcg_out_opc_bf(s, OPC_INS, lo, TCG_TMP0, 31, 8);
1402        } else {
1403            tcg_out_opc_imm(s, OPC_LBU, TCG_TMP0, base, 0);
1404            tcg_out_opc_imm(s, sgn ? OPC_LB : OPC_LBU, TCG_TMP1, base, 1);
1405            tcg_out_opc_sa(s, OPC_SLL, TCG_TMP1, TCG_TMP1, 8);
1406            tcg_out_opc_reg(s, OPC_OR, lo, TCG_TMP0, TCG_TMP1);
1407        }
1408        break;
1409
1410    case MO_32:
1411        tcg_out_opc_imm(s, lw1, lo, base, 0);
1412        tcg_out_opc_imm(s, lw2, lo, base, 3);
1413        if (TCG_TARGET_REG_BITS == 64 && type == TCG_TYPE_I64 && !sgn) {
1414            tcg_out_ext32u(s, lo, lo);
1415        }
1416        break;
1417
1418    case MO_64:
1419        if (TCG_TARGET_REG_BITS == 64) {
1420            tcg_out_opc_imm(s, ld1, lo, base, 0);
1421            tcg_out_opc_imm(s, ld2, lo, base, 7);
1422        } else {
1423            tcg_out_opc_imm(s, lw1, MIPS_BE ? hi : lo, base, 0 + 0);
1424            tcg_out_opc_imm(s, lw2, MIPS_BE ? hi : lo, base, 0 + 3);
1425            tcg_out_opc_imm(s, lw1, MIPS_BE ? lo : hi, base, 4 + 0);
1426            tcg_out_opc_imm(s, lw2, MIPS_BE ? lo : hi, base, 4 + 3);
1427        }
1428        break;
1429
1430    default:
1431        g_assert_not_reached();
1432    }
1433}
1434
1435static void tcg_out_qemu_ld(TCGContext *s, TCGReg datalo, TCGReg datahi,
1436                            TCGReg addrlo, TCGReg addrhi,
1437                            MemOpIdx oi, TCGType data_type)
1438{
1439    MemOp opc = get_memop(oi);
1440    TCGLabelQemuLdst *ldst;
1441    HostAddress h;
1442
1443    ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, true);
1444
1445    if (use_mips32r6_instructions || h.align >= (opc & MO_SIZE)) {
1446        tcg_out_qemu_ld_direct(s, datalo, datahi, h.base, opc, data_type);
1447    } else {
1448        tcg_out_qemu_ld_unalign(s, datalo, datahi, h.base, opc, data_type);
1449    }
1450
1451    if (ldst) {
1452        ldst->type = data_type;
1453        ldst->datalo_reg = datalo;
1454        ldst->datahi_reg = datahi;
1455        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1456    }
1457}
1458
1459static void tcg_out_qemu_st_direct(TCGContext *s, TCGReg lo, TCGReg hi,
1460                                   TCGReg base, MemOp opc)
1461{
1462    switch (opc & MO_SIZE) {
1463    case MO_8:
1464        tcg_out_opc_imm(s, OPC_SB, lo, base, 0);
1465        break;
1466    case MO_16:
1467        tcg_out_opc_imm(s, OPC_SH, lo, base, 0);
1468        break;
1469    case MO_32:
1470        tcg_out_opc_imm(s, OPC_SW, lo, base, 0);
1471        break;
1472    case MO_64:
1473        if (TCG_TARGET_REG_BITS == 64) {
1474            tcg_out_opc_imm(s, OPC_SD, lo, base, 0);
1475        } else {
1476            tcg_out_opc_imm(s, OPC_SW, MIPS_BE ? hi : lo, base, 0);
1477            tcg_out_opc_imm(s, OPC_SW, MIPS_BE ? lo : hi, base, 4);
1478        }
1479        break;
1480    default:
1481        g_assert_not_reached();
1482    }
1483}
1484
1485static void tcg_out_qemu_st_unalign(TCGContext *s, TCGReg lo, TCGReg hi,
1486                                    TCGReg base, MemOp opc)
1487{
1488    const MIPSInsn sw1 = MIPS_BE ? OPC_SWL : OPC_SWR;
1489    const MIPSInsn sw2 = MIPS_BE ? OPC_SWR : OPC_SWL;
1490    const MIPSInsn sd1 = MIPS_BE ? OPC_SDL : OPC_SDR;
1491    const MIPSInsn sd2 = MIPS_BE ? OPC_SDR : OPC_SDL;
1492
1493    switch (opc & MO_SIZE) {
1494    case MO_16:
1495        tcg_out_opc_sa(s, OPC_SRL, TCG_TMP0, lo, 8);
1496        tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? TCG_TMP0 : lo, base, 0);
1497        tcg_out_opc_imm(s, OPC_SB, HOST_BIG_ENDIAN ? lo : TCG_TMP0, base, 1);
1498        break;
1499
1500    case MO_32:
1501        tcg_out_opc_imm(s, sw1, lo, base, 0);
1502        tcg_out_opc_imm(s, sw2, lo, base, 3);
1503        break;
1504
1505    case MO_64:
1506        if (TCG_TARGET_REG_BITS == 64) {
1507            tcg_out_opc_imm(s, sd1, lo, base, 0);
1508            tcg_out_opc_imm(s, sd2, lo, base, 7);
1509        } else {
1510            tcg_out_opc_imm(s, sw1, MIPS_BE ? hi : lo, base, 0 + 0);
1511            tcg_out_opc_imm(s, sw2, MIPS_BE ? hi : lo, base, 0 + 3);
1512            tcg_out_opc_imm(s, sw1, MIPS_BE ? lo : hi, base, 4 + 0);
1513            tcg_out_opc_imm(s, sw2, MIPS_BE ? lo : hi, base, 4 + 3);
1514        }
1515        break;
1516
1517    default:
1518        g_assert_not_reached();
1519    }
1520}
1521
1522static void tcg_out_qemu_st(TCGContext *s, TCGReg datalo, TCGReg datahi,
1523                            TCGReg addrlo, TCGReg addrhi,
1524                            MemOpIdx oi, TCGType data_type)
1525{
1526    MemOp opc = get_memop(oi);
1527    TCGLabelQemuLdst *ldst;
1528    HostAddress h;
1529
1530    ldst = prepare_host_addr(s, &h, addrlo, addrhi, oi, false);
1531
1532    if (use_mips32r6_instructions || h.align >= (opc & MO_SIZE)) {
1533        tcg_out_qemu_st_direct(s, datalo, datahi, h.base, opc);
1534    } else {
1535        tcg_out_qemu_st_unalign(s, datalo, datahi, h.base, opc);
1536    }
1537
1538    if (ldst) {
1539        ldst->type = data_type;
1540        ldst->datalo_reg = datalo;
1541        ldst->datahi_reg = datahi;
1542        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1543    }
1544}
1545
1546static void tcg_out_mb(TCGContext *s, TCGArg a0)
1547{
1548    static const MIPSInsn sync[] = {
1549        /* Note that SYNC_MB is a slightly weaker than SYNC 0,
1550           as the former is an ordering barrier and the latter
1551           is a completion barrier.  */
1552        [0 ... TCG_MO_ALL]            = OPC_SYNC_MB,
1553        [TCG_MO_LD_LD]                = OPC_SYNC_RMB,
1554        [TCG_MO_ST_ST]                = OPC_SYNC_WMB,
1555        [TCG_MO_LD_ST]                = OPC_SYNC_RELEASE,
1556        [TCG_MO_LD_ST | TCG_MO_ST_ST] = OPC_SYNC_RELEASE,
1557        [TCG_MO_LD_ST | TCG_MO_LD_LD] = OPC_SYNC_ACQUIRE,
1558    };
1559    tcg_out32(s, sync[a0 & TCG_MO_ALL]);
1560}
1561
1562static void tcg_out_clz(TCGContext *s, MIPSInsn opcv2, MIPSInsn opcv6,
1563                        int width, TCGReg a0, TCGReg a1, TCGArg a2)
1564{
1565    if (use_mips32r6_instructions) {
1566        if (a2 == width) {
1567            tcg_out_opc_reg(s, opcv6, a0, a1, 0);
1568        } else {
1569            tcg_out_opc_reg(s, opcv6, TCG_TMP0, a1, 0);
1570            tcg_out_movcond(s, TCG_COND_EQ, a0, a1, 0, a2, TCG_TMP0);
1571        }
1572    } else {
1573        if (a2 == width) {
1574            tcg_out_opc_reg(s, opcv2, a0, a1, a1);
1575        } else if (a0 == a2) {
1576            tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
1577            tcg_out_opc_reg(s, OPC_MOVN, a0, TCG_TMP0, a1);
1578        } else if (a0 != a1) {
1579            tcg_out_opc_reg(s, opcv2, a0, a1, a1);
1580            tcg_out_opc_reg(s, OPC_MOVZ, a0, a2, a1);
1581        } else {
1582            tcg_out_opc_reg(s, opcv2, TCG_TMP0, a1, a1);
1583            tcg_out_opc_reg(s, OPC_MOVZ, TCG_TMP0, a2, a1);
1584            tcg_out_mov(s, TCG_TYPE_REG, a0, TCG_TMP0);
1585        }
1586    }
1587}
1588
1589static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1590{
1591    TCGReg b0 = TCG_REG_ZERO;
1592
1593    if (a0 & ~0xffff) {
1594        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_V0, a0 & ~0xffff);
1595        b0 = TCG_REG_V0;
1596    }
1597    if (!tcg_out_opc_jmp(s, OPC_J, tb_ret_addr)) {
1598        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)tb_ret_addr);
1599        tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0);
1600    }
1601    tcg_out_opc_imm(s, OPC_ORI, TCG_REG_V0, b0, a0 & 0xffff);
1602}
1603
1604static void tcg_out_goto_tb(TCGContext *s, int which)
1605{
1606    /* indirect jump method */
1607    tcg_out_ld(s, TCG_TYPE_PTR, TCG_TMP0, TCG_REG_ZERO,
1608               get_jmp_target_addr(s, which));
1609    tcg_out_opc_reg(s, OPC_JR, 0, TCG_TMP0, 0);
1610    tcg_out_nop(s);
1611    set_jmp_reset_offset(s, which);
1612}
1613
1614void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1615                              uintptr_t jmp_rx, uintptr_t jmp_rw)
1616{
1617    /* Always indirect, nothing to do */
1618}
1619
1620static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1621                       const TCGArg args[TCG_MAX_OP_ARGS],
1622                       const int const_args[TCG_MAX_OP_ARGS])
1623{
1624    MIPSInsn i1, i2;
1625    TCGArg a0, a1, a2;
1626    int c2;
1627
1628    /*
1629     * Note that many operands use the constraint set "rZ".
1630     * We make use of the fact that 0 is the ZERO register,
1631     * and hence such cases need not check for const_args.
1632     */
1633    a0 = args[0];
1634    a1 = args[1];
1635    a2 = args[2];
1636    c2 = const_args[2];
1637
1638    switch (opc) {
1639    case INDEX_op_goto_ptr:
1640        /* jmp to the given host address (could be epilogue) */
1641        tcg_out_opc_reg(s, OPC_JR, 0, a0, 0);
1642        tcg_out_nop(s);
1643        break;
1644    case INDEX_op_br:
1645        tcg_out_brcond(s, TCG_COND_EQ, TCG_REG_ZERO, TCG_REG_ZERO,
1646                       arg_label(a0));
1647        break;
1648
1649    case INDEX_op_ld8u_i32:
1650    case INDEX_op_ld8u_i64:
1651        i1 = OPC_LBU;
1652        goto do_ldst;
1653    case INDEX_op_ld8s_i32:
1654    case INDEX_op_ld8s_i64:
1655        i1 = OPC_LB;
1656        goto do_ldst;
1657    case INDEX_op_ld16u_i32:
1658    case INDEX_op_ld16u_i64:
1659        i1 = OPC_LHU;
1660        goto do_ldst;
1661    case INDEX_op_ld16s_i32:
1662    case INDEX_op_ld16s_i64:
1663        i1 = OPC_LH;
1664        goto do_ldst;
1665    case INDEX_op_ld_i32:
1666    case INDEX_op_ld32s_i64:
1667        i1 = OPC_LW;
1668        goto do_ldst;
1669    case INDEX_op_ld32u_i64:
1670        i1 = OPC_LWU;
1671        goto do_ldst;
1672    case INDEX_op_ld_i64:
1673        i1 = OPC_LD;
1674        goto do_ldst;
1675    case INDEX_op_st8_i32:
1676    case INDEX_op_st8_i64:
1677        i1 = OPC_SB;
1678        goto do_ldst;
1679    case INDEX_op_st16_i32:
1680    case INDEX_op_st16_i64:
1681        i1 = OPC_SH;
1682        goto do_ldst;
1683    case INDEX_op_st_i32:
1684    case INDEX_op_st32_i64:
1685        i1 = OPC_SW;
1686        goto do_ldst;
1687    case INDEX_op_st_i64:
1688        i1 = OPC_SD;
1689    do_ldst:
1690        tcg_out_ldst(s, i1, a0, a1, a2);
1691        break;
1692
1693    case INDEX_op_add_i32:
1694        i1 = OPC_ADDU, i2 = OPC_ADDIU;
1695        goto do_binary;
1696    case INDEX_op_add_i64:
1697        i1 = OPC_DADDU, i2 = OPC_DADDIU;
1698        goto do_binary;
1699    case INDEX_op_or_i32:
1700    case INDEX_op_or_i64:
1701        i1 = OPC_OR, i2 = OPC_ORI;
1702        goto do_binary;
1703    case INDEX_op_xor_i32:
1704    case INDEX_op_xor_i64:
1705        i1 = OPC_XOR, i2 = OPC_XORI;
1706    do_binary:
1707        if (c2) {
1708            tcg_out_opc_imm(s, i2, a0, a1, a2);
1709            break;
1710        }
1711    do_binaryv:
1712        tcg_out_opc_reg(s, i1, a0, a1, a2);
1713        break;
1714
1715    case INDEX_op_sub_i32:
1716        i1 = OPC_SUBU, i2 = OPC_ADDIU;
1717        goto do_subtract;
1718    case INDEX_op_sub_i64:
1719        i1 = OPC_DSUBU, i2 = OPC_DADDIU;
1720    do_subtract:
1721        if (c2) {
1722            tcg_out_opc_imm(s, i2, a0, a1, -a2);
1723            break;
1724        }
1725        goto do_binaryv;
1726    case INDEX_op_and_i32:
1727        if (c2 && a2 != (uint16_t)a2) {
1728            int msb = ctz32(~a2) - 1;
1729            tcg_debug_assert(use_mips32r2_instructions);
1730            tcg_debug_assert(is_p2m1(a2));
1731            tcg_out_opc_bf(s, OPC_EXT, a0, a1, msb, 0);
1732            break;
1733        }
1734        i1 = OPC_AND, i2 = OPC_ANDI;
1735        goto do_binary;
1736    case INDEX_op_and_i64:
1737        if (c2 && a2 != (uint16_t)a2) {
1738            int msb = ctz64(~a2) - 1;
1739            tcg_debug_assert(use_mips32r2_instructions);
1740            tcg_debug_assert(is_p2m1(a2));
1741            tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1, msb, 0);
1742            break;
1743        }
1744        i1 = OPC_AND, i2 = OPC_ANDI;
1745        goto do_binary;
1746    case INDEX_op_nor_i32:
1747    case INDEX_op_nor_i64:
1748        i1 = OPC_NOR;
1749        goto do_binaryv;
1750
1751    case INDEX_op_mul_i32:
1752        if (use_mips32_instructions) {
1753            tcg_out_opc_reg(s, OPC_MUL, a0, a1, a2);
1754            break;
1755        }
1756        i1 = OPC_MULT, i2 = OPC_MFLO;
1757        goto do_hilo1;
1758    case INDEX_op_mulsh_i32:
1759        if (use_mips32r6_instructions) {
1760            tcg_out_opc_reg(s, OPC_MUH, a0, a1, a2);
1761            break;
1762        }
1763        i1 = OPC_MULT, i2 = OPC_MFHI;
1764        goto do_hilo1;
1765    case INDEX_op_muluh_i32:
1766        if (use_mips32r6_instructions) {
1767            tcg_out_opc_reg(s, OPC_MUHU, a0, a1, a2);
1768            break;
1769        }
1770        i1 = OPC_MULTU, i2 = OPC_MFHI;
1771        goto do_hilo1;
1772    case INDEX_op_div_i32:
1773        if (use_mips32r6_instructions) {
1774            tcg_out_opc_reg(s, OPC_DIV_R6, a0, a1, a2);
1775            break;
1776        }
1777        i1 = OPC_DIV, i2 = OPC_MFLO;
1778        goto do_hilo1;
1779    case INDEX_op_divu_i32:
1780        if (use_mips32r6_instructions) {
1781            tcg_out_opc_reg(s, OPC_DIVU_R6, a0, a1, a2);
1782            break;
1783        }
1784        i1 = OPC_DIVU, i2 = OPC_MFLO;
1785        goto do_hilo1;
1786    case INDEX_op_rem_i32:
1787        if (use_mips32r6_instructions) {
1788            tcg_out_opc_reg(s, OPC_MOD, a0, a1, a2);
1789            break;
1790        }
1791        i1 = OPC_DIV, i2 = OPC_MFHI;
1792        goto do_hilo1;
1793    case INDEX_op_remu_i32:
1794        if (use_mips32r6_instructions) {
1795            tcg_out_opc_reg(s, OPC_MODU, a0, a1, a2);
1796            break;
1797        }
1798        i1 = OPC_DIVU, i2 = OPC_MFHI;
1799        goto do_hilo1;
1800    case INDEX_op_mul_i64:
1801        if (use_mips32r6_instructions) {
1802            tcg_out_opc_reg(s, OPC_DMUL, a0, a1, a2);
1803            break;
1804        }
1805        i1 = OPC_DMULT, i2 = OPC_MFLO;
1806        goto do_hilo1;
1807    case INDEX_op_mulsh_i64:
1808        if (use_mips32r6_instructions) {
1809            tcg_out_opc_reg(s, OPC_DMUH, a0, a1, a2);
1810            break;
1811        }
1812        i1 = OPC_DMULT, i2 = OPC_MFHI;
1813        goto do_hilo1;
1814    case INDEX_op_muluh_i64:
1815        if (use_mips32r6_instructions) {
1816            tcg_out_opc_reg(s, OPC_DMUHU, a0, a1, a2);
1817            break;
1818        }
1819        i1 = OPC_DMULTU, i2 = OPC_MFHI;
1820        goto do_hilo1;
1821    case INDEX_op_div_i64:
1822        if (use_mips32r6_instructions) {
1823            tcg_out_opc_reg(s, OPC_DDIV_R6, a0, a1, a2);
1824            break;
1825        }
1826        i1 = OPC_DDIV, i2 = OPC_MFLO;
1827        goto do_hilo1;
1828    case INDEX_op_divu_i64:
1829        if (use_mips32r6_instructions) {
1830            tcg_out_opc_reg(s, OPC_DDIVU_R6, a0, a1, a2);
1831            break;
1832        }
1833        i1 = OPC_DDIVU, i2 = OPC_MFLO;
1834        goto do_hilo1;
1835    case INDEX_op_rem_i64:
1836        if (use_mips32r6_instructions) {
1837            tcg_out_opc_reg(s, OPC_DMOD, a0, a1, a2);
1838            break;
1839        }
1840        i1 = OPC_DDIV, i2 = OPC_MFHI;
1841        goto do_hilo1;
1842    case INDEX_op_remu_i64:
1843        if (use_mips32r6_instructions) {
1844            tcg_out_opc_reg(s, OPC_DMODU, a0, a1, a2);
1845            break;
1846        }
1847        i1 = OPC_DDIVU, i2 = OPC_MFHI;
1848    do_hilo1:
1849        tcg_out_opc_reg(s, i1, 0, a1, a2);
1850        tcg_out_opc_reg(s, i2, a0, 0, 0);
1851        break;
1852
1853    case INDEX_op_muls2_i32:
1854        i1 = OPC_MULT;
1855        goto do_hilo2;
1856    case INDEX_op_mulu2_i32:
1857        i1 = OPC_MULTU;
1858        goto do_hilo2;
1859    case INDEX_op_muls2_i64:
1860        i1 = OPC_DMULT;
1861        goto do_hilo2;
1862    case INDEX_op_mulu2_i64:
1863        i1 = OPC_DMULTU;
1864    do_hilo2:
1865        tcg_out_opc_reg(s, i1, 0, a2, args[3]);
1866        tcg_out_opc_reg(s, OPC_MFLO, a0, 0, 0);
1867        tcg_out_opc_reg(s, OPC_MFHI, a1, 0, 0);
1868        break;
1869
1870    case INDEX_op_not_i32:
1871    case INDEX_op_not_i64:
1872        i1 = OPC_NOR;
1873        goto do_unary;
1874    do_unary:
1875        tcg_out_opc_reg(s, i1, a0, TCG_REG_ZERO, a1);
1876        break;
1877
1878    case INDEX_op_bswap16_i32:
1879    case INDEX_op_bswap16_i64:
1880        tcg_out_bswap16(s, a0, a1, a2);
1881        break;
1882    case INDEX_op_bswap32_i32:
1883        tcg_out_bswap32(s, a0, a1, 0);
1884        break;
1885    case INDEX_op_bswap32_i64:
1886        tcg_out_bswap32(s, a0, a1, a2);
1887        break;
1888    case INDEX_op_bswap64_i64:
1889        tcg_out_bswap64(s, a0, a1);
1890        break;
1891    case INDEX_op_extrh_i64_i32:
1892        tcg_out_dsra(s, a0, a1, 32);
1893        break;
1894
1895    case INDEX_op_sar_i32:
1896        i1 = OPC_SRAV, i2 = OPC_SRA;
1897        goto do_shift;
1898    case INDEX_op_shl_i32:
1899        i1 = OPC_SLLV, i2 = OPC_SLL;
1900        goto do_shift;
1901    case INDEX_op_shr_i32:
1902        i1 = OPC_SRLV, i2 = OPC_SRL;
1903        goto do_shift;
1904    case INDEX_op_rotr_i32:
1905        i1 = OPC_ROTRV, i2 = OPC_ROTR;
1906    do_shift:
1907        if (c2) {
1908            tcg_out_opc_sa(s, i2, a0, a1, a2);
1909            break;
1910        }
1911    do_shiftv:
1912        tcg_out_opc_reg(s, i1, a0, a2, a1);
1913        break;
1914    case INDEX_op_rotl_i32:
1915        if (c2) {
1916            tcg_out_opc_sa(s, OPC_ROTR, a0, a1, 32 - a2);
1917        } else {
1918            tcg_out_opc_reg(s, OPC_SUBU, TCG_TMP0, TCG_REG_ZERO, a2);
1919            tcg_out_opc_reg(s, OPC_ROTRV, a0, TCG_TMP0, a1);
1920        }
1921        break;
1922    case INDEX_op_sar_i64:
1923        if (c2) {
1924            tcg_out_dsra(s, a0, a1, a2);
1925            break;
1926        }
1927        i1 = OPC_DSRAV;
1928        goto do_shiftv;
1929    case INDEX_op_shl_i64:
1930        if (c2) {
1931            tcg_out_dsll(s, a0, a1, a2);
1932            break;
1933        }
1934        i1 = OPC_DSLLV;
1935        goto do_shiftv;
1936    case INDEX_op_shr_i64:
1937        if (c2) {
1938            tcg_out_dsrl(s, a0, a1, a2);
1939            break;
1940        }
1941        i1 = OPC_DSRLV;
1942        goto do_shiftv;
1943    case INDEX_op_rotr_i64:
1944        if (c2) {
1945            tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, a2);
1946            break;
1947        }
1948        i1 = OPC_DROTRV;
1949        goto do_shiftv;
1950    case INDEX_op_rotl_i64:
1951        if (c2) {
1952            tcg_out_opc_sa64(s, OPC_DROTR, OPC_DROTR32, a0, a1, 64 - a2);
1953        } else {
1954            tcg_out_opc_reg(s, OPC_DSUBU, TCG_TMP0, TCG_REG_ZERO, a2);
1955            tcg_out_opc_reg(s, OPC_DROTRV, a0, TCG_TMP0, a1);
1956        }
1957        break;
1958
1959    case INDEX_op_clz_i32:
1960        tcg_out_clz(s, OPC_CLZ, OPC_CLZ_R6, 32, a0, a1, a2);
1961        break;
1962    case INDEX_op_clz_i64:
1963        tcg_out_clz(s, OPC_DCLZ, OPC_DCLZ_R6, 64, a0, a1, a2);
1964        break;
1965
1966    case INDEX_op_deposit_i32:
1967        tcg_out_opc_bf(s, OPC_INS, a0, a2, args[3] + args[4] - 1, args[3]);
1968        break;
1969    case INDEX_op_deposit_i64:
1970        tcg_out_opc_bf64(s, OPC_DINS, OPC_DINSM, OPC_DINSU, a0, a2,
1971                         args[3] + args[4] - 1, args[3]);
1972        break;
1973    case INDEX_op_extract_i32:
1974        tcg_out_opc_bf(s, OPC_EXT, a0, a1, args[3] - 1, a2);
1975        break;
1976    case INDEX_op_extract_i64:
1977        tcg_out_opc_bf64(s, OPC_DEXT, OPC_DEXTM, OPC_DEXTU, a0, a1,
1978                         args[3] - 1, a2);
1979        break;
1980
1981    case INDEX_op_brcond_i32:
1982    case INDEX_op_brcond_i64:
1983        tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1984        break;
1985    case INDEX_op_brcond2_i32:
1986        tcg_out_brcond2(s, args[4], a0, a1, a2, args[3], arg_label(args[5]));
1987        break;
1988
1989    case INDEX_op_movcond_i32:
1990    case INDEX_op_movcond_i64:
1991        tcg_out_movcond(s, args[5], a0, a1, a2, args[3], args[4]);
1992        break;
1993
1994    case INDEX_op_setcond_i32:
1995    case INDEX_op_setcond_i64:
1996        tcg_out_setcond(s, args[3], a0, a1, a2);
1997        break;
1998    case INDEX_op_setcond2_i32:
1999        tcg_out_setcond2(s, args[5], a0, a1, a2, args[3], args[4]);
2000        break;
2001
2002    case INDEX_op_qemu_ld_i32:
2003        if (TCG_TARGET_REG_BITS >= TARGET_LONG_BITS) {
2004            tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2005        } else {
2006            tcg_out_qemu_ld(s, a0, 0, a1, a2, args[3], TCG_TYPE_I32);
2007        }
2008        break;
2009    case INDEX_op_qemu_ld_i64:
2010        if (TCG_TARGET_REG_BITS == 64) {
2011            tcg_out_qemu_ld(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2012        } else if (TARGET_LONG_BITS == 32) {
2013            tcg_out_qemu_ld(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2014        } else {
2015            tcg_out_qemu_ld(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2016        }
2017        break;
2018    case INDEX_op_qemu_st_i32:
2019        if (TCG_TARGET_REG_BITS >= TARGET_LONG_BITS) {
2020            tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I32);
2021        } else {
2022            tcg_out_qemu_st(s, a0, 0, a1, a2, args[3], TCG_TYPE_I32);
2023        }
2024        break;
2025    case INDEX_op_qemu_st_i64:
2026        if (TCG_TARGET_REG_BITS == 64) {
2027            tcg_out_qemu_st(s, a0, 0, a1, 0, a2, TCG_TYPE_I64);
2028        } else if (TARGET_LONG_BITS == 32) {
2029            tcg_out_qemu_st(s, a0, a1, a2, 0, args[3], TCG_TYPE_I64);
2030        } else {
2031            tcg_out_qemu_st(s, a0, a1, a2, args[3], args[4], TCG_TYPE_I64);
2032        }
2033        break;
2034
2035    case INDEX_op_add2_i32:
2036        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
2037                        const_args[4], const_args[5], false);
2038        break;
2039    case INDEX_op_sub2_i32:
2040        tcg_out_addsub2(s, a0, a1, a2, args[3], args[4], args[5],
2041                        const_args[4], const_args[5], true);
2042        break;
2043
2044    case INDEX_op_mb:
2045        tcg_out_mb(s, a0);
2046        break;
2047    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
2048    case INDEX_op_mov_i64:
2049    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
2050    case INDEX_op_exit_tb:  /* Always emitted via tcg_out_exit_tb.  */
2051    case INDEX_op_goto_tb:  /* Always emitted via tcg_out_goto_tb.  */
2052    case INDEX_op_ext8s_i32:  /* Always emitted via tcg_reg_alloc_op.  */
2053    case INDEX_op_ext8s_i64:
2054    case INDEX_op_ext8u_i32:
2055    case INDEX_op_ext8u_i64:
2056    case INDEX_op_ext16s_i32:
2057    case INDEX_op_ext16s_i64:
2058    case INDEX_op_ext32s_i64:
2059    case INDEX_op_ext32u_i64:
2060    case INDEX_op_ext_i32_i64:
2061    case INDEX_op_extu_i32_i64:
2062    case INDEX_op_extrl_i64_i32:
2063    default:
2064        g_assert_not_reached();
2065    }
2066}
2067
2068static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
2069{
2070    switch (op) {
2071    case INDEX_op_goto_ptr:
2072        return C_O0_I1(r);
2073
2074    case INDEX_op_ld8u_i32:
2075    case INDEX_op_ld8s_i32:
2076    case INDEX_op_ld16u_i32:
2077    case INDEX_op_ld16s_i32:
2078    case INDEX_op_ld_i32:
2079    case INDEX_op_not_i32:
2080    case INDEX_op_bswap16_i32:
2081    case INDEX_op_bswap32_i32:
2082    case INDEX_op_ext8s_i32:
2083    case INDEX_op_ext16s_i32:
2084    case INDEX_op_extract_i32:
2085    case INDEX_op_ld8u_i64:
2086    case INDEX_op_ld8s_i64:
2087    case INDEX_op_ld16u_i64:
2088    case INDEX_op_ld16s_i64:
2089    case INDEX_op_ld32s_i64:
2090    case INDEX_op_ld32u_i64:
2091    case INDEX_op_ld_i64:
2092    case INDEX_op_not_i64:
2093    case INDEX_op_bswap16_i64:
2094    case INDEX_op_bswap32_i64:
2095    case INDEX_op_bswap64_i64:
2096    case INDEX_op_ext8s_i64:
2097    case INDEX_op_ext16s_i64:
2098    case INDEX_op_ext32s_i64:
2099    case INDEX_op_ext32u_i64:
2100    case INDEX_op_ext_i32_i64:
2101    case INDEX_op_extu_i32_i64:
2102    case INDEX_op_extrl_i64_i32:
2103    case INDEX_op_extrh_i64_i32:
2104    case INDEX_op_extract_i64:
2105        return C_O1_I1(r, r);
2106
2107    case INDEX_op_st8_i32:
2108    case INDEX_op_st16_i32:
2109    case INDEX_op_st_i32:
2110    case INDEX_op_st8_i64:
2111    case INDEX_op_st16_i64:
2112    case INDEX_op_st32_i64:
2113    case INDEX_op_st_i64:
2114        return C_O0_I2(rZ, r);
2115
2116    case INDEX_op_add_i32:
2117    case INDEX_op_add_i64:
2118        return C_O1_I2(r, r, rJ);
2119    case INDEX_op_sub_i32:
2120    case INDEX_op_sub_i64:
2121        return C_O1_I2(r, rZ, rN);
2122    case INDEX_op_mul_i32:
2123    case INDEX_op_mulsh_i32:
2124    case INDEX_op_muluh_i32:
2125    case INDEX_op_div_i32:
2126    case INDEX_op_divu_i32:
2127    case INDEX_op_rem_i32:
2128    case INDEX_op_remu_i32:
2129    case INDEX_op_nor_i32:
2130    case INDEX_op_setcond_i32:
2131    case INDEX_op_mul_i64:
2132    case INDEX_op_mulsh_i64:
2133    case INDEX_op_muluh_i64:
2134    case INDEX_op_div_i64:
2135    case INDEX_op_divu_i64:
2136    case INDEX_op_rem_i64:
2137    case INDEX_op_remu_i64:
2138    case INDEX_op_nor_i64:
2139    case INDEX_op_setcond_i64:
2140        return C_O1_I2(r, rZ, rZ);
2141    case INDEX_op_muls2_i32:
2142    case INDEX_op_mulu2_i32:
2143    case INDEX_op_muls2_i64:
2144    case INDEX_op_mulu2_i64:
2145        return C_O2_I2(r, r, r, r);
2146    case INDEX_op_and_i32:
2147    case INDEX_op_and_i64:
2148        return C_O1_I2(r, r, rIK);
2149    case INDEX_op_or_i32:
2150    case INDEX_op_xor_i32:
2151    case INDEX_op_or_i64:
2152    case INDEX_op_xor_i64:
2153        return C_O1_I2(r, r, rI);
2154    case INDEX_op_shl_i32:
2155    case INDEX_op_shr_i32:
2156    case INDEX_op_sar_i32:
2157    case INDEX_op_rotr_i32:
2158    case INDEX_op_rotl_i32:
2159    case INDEX_op_shl_i64:
2160    case INDEX_op_shr_i64:
2161    case INDEX_op_sar_i64:
2162    case INDEX_op_rotr_i64:
2163    case INDEX_op_rotl_i64:
2164        return C_O1_I2(r, r, ri);
2165    case INDEX_op_clz_i32:
2166    case INDEX_op_clz_i64:
2167        return C_O1_I2(r, r, rWZ);
2168
2169    case INDEX_op_deposit_i32:
2170    case INDEX_op_deposit_i64:
2171        return C_O1_I2(r, 0, rZ);
2172    case INDEX_op_brcond_i32:
2173    case INDEX_op_brcond_i64:
2174        return C_O0_I2(rZ, rZ);
2175    case INDEX_op_movcond_i32:
2176    case INDEX_op_movcond_i64:
2177        return (use_mips32r6_instructions
2178                ? C_O1_I4(r, rZ, rZ, rZ, rZ)
2179                : C_O1_I4(r, rZ, rZ, rZ, 0));
2180    case INDEX_op_add2_i32:
2181    case INDEX_op_sub2_i32:
2182        return C_O2_I4(r, r, rZ, rZ, rN, rN);
2183    case INDEX_op_setcond2_i32:
2184        return C_O1_I4(r, rZ, rZ, rZ, rZ);
2185    case INDEX_op_brcond2_i32:
2186        return C_O0_I4(rZ, rZ, rZ, rZ);
2187
2188    case INDEX_op_qemu_ld_i32:
2189        return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
2190                ? C_O1_I1(r, r) : C_O1_I2(r, r, r));
2191    case INDEX_op_qemu_st_i32:
2192        return (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 32
2193                ? C_O0_I2(rZ, r) : C_O0_I3(rZ, r, r));
2194    case INDEX_op_qemu_ld_i64:
2195        return (TCG_TARGET_REG_BITS == 64 ? C_O1_I1(r, r)
2196                : TARGET_LONG_BITS == 32 ? C_O2_I1(r, r, r)
2197                : C_O2_I2(r, r, r, r));
2198    case INDEX_op_qemu_st_i64:
2199        return (TCG_TARGET_REG_BITS == 64 ? C_O0_I2(rZ, r)
2200                : TARGET_LONG_BITS == 32 ? C_O0_I3(rZ, rZ, r)
2201                : C_O0_I4(rZ, rZ, r, r));
2202
2203    default:
2204        g_assert_not_reached();
2205    }
2206}
2207
2208static const int tcg_target_callee_save_regs[] = {
2209    TCG_REG_S0,       /* used for the global env (TCG_AREG0) */
2210    TCG_REG_S1,
2211    TCG_REG_S2,
2212    TCG_REG_S3,
2213    TCG_REG_S4,
2214    TCG_REG_S5,
2215    TCG_REG_S6,
2216    TCG_REG_S7,
2217    TCG_REG_S8,
2218    TCG_REG_RA,       /* should be last for ABI compliance */
2219};
2220
2221/* The Linux kernel doesn't provide any information about the available
2222   instruction set. Probe it using a signal handler. */
2223
2224
2225#ifndef use_movnz_instructions
2226bool use_movnz_instructions = false;
2227#endif
2228
2229#ifndef use_mips32_instructions
2230bool use_mips32_instructions = false;
2231#endif
2232
2233#ifndef use_mips32r2_instructions
2234bool use_mips32r2_instructions = false;
2235#endif
2236
2237static volatile sig_atomic_t got_sigill;
2238
2239static void sigill_handler(int signo, siginfo_t *si, void *data)
2240{
2241    /* Skip the faulty instruction */
2242    ucontext_t *uc = (ucontext_t *)data;
2243    uc->uc_mcontext.pc += 4;
2244
2245    got_sigill = 1;
2246}
2247
2248static void tcg_target_detect_isa(void)
2249{
2250    struct sigaction sa_old, sa_new;
2251
2252    memset(&sa_new, 0, sizeof(sa_new));
2253    sa_new.sa_flags = SA_SIGINFO;
2254    sa_new.sa_sigaction = sigill_handler;
2255    sigaction(SIGILL, &sa_new, &sa_old);
2256
2257    /* Probe for movn/movz, necessary to implement movcond. */
2258#ifndef use_movnz_instructions
2259    got_sigill = 0;
2260    asm volatile(".set push\n"
2261                 ".set mips32\n"
2262                 "movn $zero, $zero, $zero\n"
2263                 "movz $zero, $zero, $zero\n"
2264                 ".set pop\n"
2265                 : : : );
2266    use_movnz_instructions = !got_sigill;
2267#endif
2268
2269    /* Probe for MIPS32 instructions. As no subsetting is allowed
2270       by the specification, it is only necessary to probe for one
2271       of the instructions. */
2272#ifndef use_mips32_instructions
2273    got_sigill = 0;
2274    asm volatile(".set push\n"
2275                 ".set mips32\n"
2276                 "mul $zero, $zero\n"
2277                 ".set pop\n"
2278                 : : : );
2279    use_mips32_instructions = !got_sigill;
2280#endif
2281
2282    /* Probe for MIPS32r2 instructions if MIPS32 instructions are
2283       available. As no subsetting is allowed by the specification,
2284       it is only necessary to probe for one of the instructions. */
2285#ifndef use_mips32r2_instructions
2286    if (use_mips32_instructions) {
2287        got_sigill = 0;
2288        asm volatile(".set push\n"
2289                     ".set mips32r2\n"
2290                     "seb $zero, $zero\n"
2291                     ".set pop\n"
2292                     : : : );
2293        use_mips32r2_instructions = !got_sigill;
2294    }
2295#endif
2296
2297    sigaction(SIGILL, &sa_old, NULL);
2298}
2299
2300static tcg_insn_unit *align_code_ptr(TCGContext *s)
2301{
2302    uintptr_t p = (uintptr_t)s->code_ptr;
2303    if (p & 15) {
2304        p = (p + 15) & -16;
2305        s->code_ptr = (void *)p;
2306    }
2307    return s->code_ptr;
2308}
2309
2310/* Stack frame parameters.  */
2311#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
2312#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
2313#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
2314
2315#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
2316                     + TCG_TARGET_STACK_ALIGN - 1) \
2317                    & -TCG_TARGET_STACK_ALIGN)
2318#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
2319
2320/* We're expecting to be able to use an immediate for frame allocation.  */
2321QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7fff);
2322
2323/* Generate global QEMU prologue and epilogue code */
2324static void tcg_target_qemu_prologue(TCGContext *s)
2325{
2326    int i;
2327
2328    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
2329
2330    /* TB prologue */
2331    tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
2332    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2333        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2334                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2335    }
2336
2337#ifndef CONFIG_SOFTMMU
2338    if (guest_base) {
2339        tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
2340        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2341    }
2342#endif
2343
2344    /* Call generated code */
2345    tcg_out_opc_reg(s, OPC_JR, 0, tcg_target_call_iarg_regs[1], 0);
2346    /* delay slot */
2347    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2348
2349    /*
2350     * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
2351     * and fall through to the rest of the epilogue.
2352     */
2353    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
2354    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_V0, TCG_REG_ZERO);
2355
2356    /* TB epilogue */
2357    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
2358    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
2359        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
2360                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
2361    }
2362
2363    tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2364    /* delay slot */
2365    tcg_out_opc_imm(s, ALIAS_PADDI, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
2366
2367    if (use_mips32r2_instructions) {
2368        return;
2369    }
2370
2371    /* Bswap subroutines: Input in TCG_TMP0, output in TCG_TMP3;
2372       clobbers TCG_TMP1, TCG_TMP2.  */
2373
2374    /*
2375     * bswap32 -- 32-bit swap (signed result for mips64).  a0 = abcd.
2376     */
2377    bswap32_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2378    /* t3 = (ssss)d000 */
2379    tcg_out_opc_sa(s, OPC_SLL, TCG_TMP3, TCG_TMP0, 24);
2380    /* t1 = 000a */
2381    tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 24);
2382    /* t2 = 00c0 */
2383    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2384    /* t3 = d00a */
2385    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2386    /* t1 = 0abc */
2387    tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2388    /* t2 = 0c00 */
2389    tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2390    /* t1 = 00b0 */
2391    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2392    /* t3 = dc0a */
2393    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2394    tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2395    /* t3 = dcba -- delay slot */
2396    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2397
2398    if (TCG_TARGET_REG_BITS == 32) {
2399        return;
2400    }
2401
2402    /*
2403     * bswap32u -- unsigned 32-bit swap.  a0 = ....abcd.
2404     */
2405    bswap32u_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2406    /* t1 = (0000)000d */
2407    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP0, 0xff);
2408    /* t3 = 000a */
2409    tcg_out_opc_sa(s, OPC_SRL, TCG_TMP3, TCG_TMP0, 24);
2410    /* t1 = (0000)d000 */
2411    tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2412    /* t2 = 00c0 */
2413    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2414    /* t3 = d00a */
2415    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2416    /* t1 = 0abc */
2417    tcg_out_opc_sa(s, OPC_SRL, TCG_TMP1, TCG_TMP0, 8);
2418    /* t2 = 0c00 */
2419    tcg_out_opc_sa(s, OPC_SLL, TCG_TMP2, TCG_TMP2, 8);
2420    /* t1 = 00b0 */
2421    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2422    /* t3 = dc0a */
2423    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2424    tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2425    /* t3 = dcba -- delay slot */
2426    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2427
2428    /*
2429     * bswap64 -- 64-bit swap.  a0 = abcdefgh
2430     */
2431    bswap64_addr = tcg_splitwx_to_rx(align_code_ptr(s));
2432    /* t3 = h0000000 */
2433    tcg_out_dsll(s, TCG_TMP3, TCG_TMP0, 56);
2434    /* t1 = 0000000a */
2435    tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 56);
2436
2437    /* t2 = 000000g0 */
2438    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP0, 0xff00);
2439    /* t3 = h000000a */
2440    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2441    /* t1 = 00000abc */
2442    tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 40);
2443    /* t2 = 0g000000 */
2444    tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2445    /* t1 = 000000b0 */
2446    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2447
2448    /* t3 = hg00000a */
2449    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2450    /* t2 = 0000abcd */
2451    tcg_out_dsrl(s, TCG_TMP2, TCG_TMP0, 32);
2452    /* t3 = hg0000ba */
2453    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2454
2455    /* t1 = 000000c0 */
2456    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP2, 0xff00);
2457    /* t2 = 0000000d */
2458    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP2, 0x00ff);
2459    /* t1 = 00000c00 */
2460    tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 8);
2461    /* t2 = 0000d000 */
2462    tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 24);
2463
2464    /* t3 = hg000cba */
2465    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2466    /* t1 = 00abcdef */
2467    tcg_out_dsrl(s, TCG_TMP1, TCG_TMP0, 16);
2468    /* t3 = hg00dcba */
2469    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2470
2471    /* t2 = 0000000f */
2472    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP2, TCG_TMP1, 0x00ff);
2473    /* t1 = 000000e0 */
2474    tcg_out_opc_imm(s, OPC_ANDI, TCG_TMP1, TCG_TMP1, 0xff00);
2475    /* t2 = 00f00000 */
2476    tcg_out_dsll(s, TCG_TMP2, TCG_TMP2, 40);
2477    /* t1 = 000e0000 */
2478    tcg_out_dsll(s, TCG_TMP1, TCG_TMP1, 24);
2479
2480    /* t3 = hgf0dcba */
2481    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP2);
2482    tcg_out_opc_reg(s, OPC_JR, 0, TCG_REG_RA, 0);
2483    /* t3 = hgfedcba -- delay slot */
2484    tcg_out_opc_reg(s, OPC_OR, TCG_TMP3, TCG_TMP3, TCG_TMP1);
2485}
2486
2487static void tcg_target_init(TCGContext *s)
2488{
2489    tcg_target_detect_isa();
2490    tcg_target_available_regs[TCG_TYPE_I32] = 0xffffffff;
2491    if (TCG_TARGET_REG_BITS == 64) {
2492        tcg_target_available_regs[TCG_TYPE_I64] = 0xffffffff;
2493    }
2494
2495    tcg_target_call_clobber_regs = 0;
2496    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
2497    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
2498    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A0);
2499    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A1);
2500    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A2);
2501    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_A3);
2502    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T0);
2503    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T1);
2504    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T2);
2505    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T3);
2506    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T4);
2507    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T5);
2508    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T6);
2509    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T7);
2510    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T8);
2511    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_T9);
2512
2513    s->reserved_regs = 0;
2514    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO); /* zero register */
2515    tcg_regset_set_reg(s->reserved_regs, TCG_REG_K0);   /* kernel use only */
2516    tcg_regset_set_reg(s->reserved_regs, TCG_REG_K1);   /* kernel use only */
2517    tcg_regset_set_reg(s->reserved_regs, TCG_TMP0);     /* internal use */
2518    tcg_regset_set_reg(s->reserved_regs, TCG_TMP1);     /* internal use */
2519    tcg_regset_set_reg(s->reserved_regs, TCG_TMP2);     /* internal use */
2520    tcg_regset_set_reg(s->reserved_regs, TCG_TMP3);     /* internal use */
2521    tcg_regset_set_reg(s->reserved_regs, TCG_REG_RA);   /* return address */
2522    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);   /* stack pointer */
2523    tcg_regset_set_reg(s->reserved_regs, TCG_REG_GP);   /* global pointer */
2524}
2525
2526typedef struct {
2527    DebugFrameHeader h;
2528    uint8_t fde_def_cfa[4];
2529    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
2530} DebugFrame;
2531
2532#define ELF_HOST_MACHINE EM_MIPS
2533/* GDB doesn't appear to require proper setting of ELF_HOST_FLAGS,
2534   which is good because they're really quite complicated for MIPS.  */
2535
2536static const DebugFrame debug_frame = {
2537    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
2538    .h.cie.id = -1,
2539    .h.cie.version = 1,
2540    .h.cie.code_align = 1,
2541    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
2542    .h.cie.return_column = TCG_REG_RA,
2543
2544    /* Total FDE size does not include the "len" member.  */
2545    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
2546
2547    .fde_def_cfa = {
2548        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ... */
2549        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
2550        (FRAME_SIZE >> 7)
2551    },
2552    .fde_reg_ofs = {
2553        0x80 + 16, 9,                   /* DW_CFA_offset, s0, -72 */
2554        0x80 + 17, 8,                   /* DW_CFA_offset, s2, -64 */
2555        0x80 + 18, 7,                   /* DW_CFA_offset, s3, -56 */
2556        0x80 + 19, 6,                   /* DW_CFA_offset, s4, -48 */
2557        0x80 + 20, 5,                   /* DW_CFA_offset, s5, -40 */
2558        0x80 + 21, 4,                   /* DW_CFA_offset, s6, -32 */
2559        0x80 + 22, 3,                   /* DW_CFA_offset, s7, -24 */
2560        0x80 + 30, 2,                   /* DW_CFA_offset, s8, -16 */
2561        0x80 + 31, 1,                   /* DW_CFA_offset, ra,  -8 */
2562    }
2563};
2564
2565void tcg_register_jit(const void *buf, size_t buf_size)
2566{
2567    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
2568}
2569