1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2021 WANG Xuerui <git@xen0n.name>
5 *
6 * Based on tcg/riscv/tcg-target.c.inc
7 *
8 * Copyright (c) 2018 SiFive, Inc
9 * Copyright (c) 2008-2009 Arnaud Patard <arnaud.patard@rtp-net.org>
10 * Copyright (c) 2009 Aurelien Jarno <aurelien@aurel32.net>
11 * Copyright (c) 2008 Fabrice Bellard
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29 * THE SOFTWARE.
30 */
31
32#include "../tcg-ldst.c.inc"
33
34#ifdef CONFIG_DEBUG_TCG
35static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
36    "zero",
37    "ra",
38    "tp",
39    "sp",
40    "a0",
41    "a1",
42    "a2",
43    "a3",
44    "a4",
45    "a5",
46    "a6",
47    "a7",
48    "t0",
49    "t1",
50    "t2",
51    "t3",
52    "t4",
53    "t5",
54    "t6",
55    "t7",
56    "t8",
57    "r21", /* reserved in the LP64* ABI, hence no ABI name */
58    "s9",
59    "s0",
60    "s1",
61    "s2",
62    "s3",
63    "s4",
64    "s5",
65    "s6",
66    "s7",
67    "s8"
68};
69#endif
70
71static const int tcg_target_reg_alloc_order[] = {
72    /* Registers preserved across calls */
73    /* TCG_REG_S0 reserved for TCG_AREG0 */
74    TCG_REG_S1,
75    TCG_REG_S2,
76    TCG_REG_S3,
77    TCG_REG_S4,
78    TCG_REG_S5,
79    TCG_REG_S6,
80    TCG_REG_S7,
81    TCG_REG_S8,
82    TCG_REG_S9,
83
84    /* Registers (potentially) clobbered across calls */
85    TCG_REG_T0,
86    TCG_REG_T1,
87    TCG_REG_T2,
88    TCG_REG_T3,
89    TCG_REG_T4,
90    TCG_REG_T5,
91    TCG_REG_T6,
92    TCG_REG_T7,
93    TCG_REG_T8,
94
95    /* Argument registers, opposite order of allocation.  */
96    TCG_REG_A7,
97    TCG_REG_A6,
98    TCG_REG_A5,
99    TCG_REG_A4,
100    TCG_REG_A3,
101    TCG_REG_A2,
102    TCG_REG_A1,
103    TCG_REG_A0,
104};
105
106static const int tcg_target_call_iarg_regs[] = {
107    TCG_REG_A0,
108    TCG_REG_A1,
109    TCG_REG_A2,
110    TCG_REG_A3,
111    TCG_REG_A4,
112    TCG_REG_A5,
113    TCG_REG_A6,
114    TCG_REG_A7,
115};
116
117static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
118{
119    tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
120    tcg_debug_assert(slot >= 0 && slot <= 1);
121    return TCG_REG_A0 + slot;
122}
123
124#ifndef CONFIG_SOFTMMU
125#define USE_GUEST_BASE     (guest_base != 0)
126#define TCG_GUEST_BASE_REG TCG_REG_S1
127#endif
128
129#define TCG_CT_CONST_ZERO  0x100
130#define TCG_CT_CONST_S12   0x200
131#define TCG_CT_CONST_S32   0x400
132#define TCG_CT_CONST_U12   0x800
133#define TCG_CT_CONST_C12   0x1000
134#define TCG_CT_CONST_WSZ   0x2000
135
136#define ALL_GENERAL_REGS      MAKE_64BIT_MASK(0, 32)
137/*
138 * For softmmu, we need to avoid conflicts with the first 5
139 * argument registers to call the helper.  Some of these are
140 * also used for the tlb lookup.
141 */
142#ifdef CONFIG_SOFTMMU
143#define SOFTMMU_RESERVE_REGS  MAKE_64BIT_MASK(TCG_REG_A0, 5)
144#else
145#define SOFTMMU_RESERVE_REGS  0
146#endif
147
148
149static inline tcg_target_long sextreg(tcg_target_long val, int pos, int len)
150{
151    return sextract64(val, pos, len);
152}
153
154/* test if a constant matches the constraint */
155static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
156{
157    if (ct & TCG_CT_CONST) {
158        return true;
159    }
160    if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
161        return true;
162    }
163    if ((ct & TCG_CT_CONST_S12) && val == sextreg(val, 0, 12)) {
164        return true;
165    }
166    if ((ct & TCG_CT_CONST_S32) && val == (int32_t)val) {
167        return true;
168    }
169    if ((ct & TCG_CT_CONST_U12) && val >= 0 && val <= 0xfff) {
170        return true;
171    }
172    if ((ct & TCG_CT_CONST_C12) && ~val >= 0 && ~val <= 0xfff) {
173        return true;
174    }
175    if ((ct & TCG_CT_CONST_WSZ) && val == (type == TCG_TYPE_I32 ? 32 : 64)) {
176        return true;
177    }
178    return false;
179}
180
181/*
182 * Relocations
183 */
184
185/*
186 * Relocation records defined in LoongArch ELF psABI v1.00 is way too
187 * complicated; a whopping stack machine is needed to stuff the fields, at
188 * the very least one SOP_PUSH and one SOP_POP (of the correct format) are
189 * needed.
190 *
191 * Hence, define our own simpler relocation types. Numbers are chosen as to
192 * not collide with potential future additions to the true ELF relocation
193 * type enum.
194 */
195
196/* Field Sk16, shifted right by 2; suitable for conditional jumps */
197#define R_LOONGARCH_BR_SK16     256
198/* Field Sd10k16, shifted right by 2; suitable for B and BL */
199#define R_LOONGARCH_BR_SD10K16  257
200
201static bool reloc_br_sk16(tcg_insn_unit *src_rw, const tcg_insn_unit *target)
202{
203    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
204    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
205
206    tcg_debug_assert((offset & 3) == 0);
207    offset >>= 2;
208    if (offset == sextreg(offset, 0, 16)) {
209        *src_rw = deposit64(*src_rw, 10, 16, offset);
210        return true;
211    }
212
213    return false;
214}
215
216static bool reloc_br_sd10k16(tcg_insn_unit *src_rw,
217                             const tcg_insn_unit *target)
218{
219    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
220    intptr_t offset = (intptr_t)target - (intptr_t)src_rx;
221
222    tcg_debug_assert((offset & 3) == 0);
223    offset >>= 2;
224    if (offset == sextreg(offset, 0, 26)) {
225        *src_rw = deposit64(*src_rw, 0, 10, offset >> 16); /* slot d10 */
226        *src_rw = deposit64(*src_rw, 10, 16, offset); /* slot k16 */
227        return true;
228    }
229
230    return false;
231}
232
233static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
234                        intptr_t value, intptr_t addend)
235{
236    tcg_debug_assert(addend == 0);
237    switch (type) {
238    case R_LOONGARCH_BR_SK16:
239        return reloc_br_sk16(code_ptr, (tcg_insn_unit *)value);
240    case R_LOONGARCH_BR_SD10K16:
241        return reloc_br_sd10k16(code_ptr, (tcg_insn_unit *)value);
242    default:
243        g_assert_not_reached();
244    }
245}
246
247#include "tcg-insn-defs.c.inc"
248
249/*
250 * TCG intrinsics
251 */
252
253static void tcg_out_mb(TCGContext *s, TCGArg a0)
254{
255    /* Baseline LoongArch only has the full barrier, unfortunately.  */
256    tcg_out_opc_dbar(s, 0);
257}
258
259static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
260{
261    if (ret == arg) {
262        return true;
263    }
264    switch (type) {
265    case TCG_TYPE_I32:
266    case TCG_TYPE_I64:
267        /*
268         * Conventional register-register move used in LoongArch is
269         * `or dst, src, zero`.
270         */
271        tcg_out_opc_or(s, ret, arg, TCG_REG_ZERO);
272        break;
273    default:
274        g_assert_not_reached();
275    }
276    return true;
277}
278
279/* Loads a 32-bit immediate into rd, sign-extended.  */
280static void tcg_out_movi_i32(TCGContext *s, TCGReg rd, int32_t val)
281{
282    tcg_target_long lo = sextreg(val, 0, 12);
283    tcg_target_long hi12 = sextreg(val, 12, 20);
284
285    /* Single-instruction cases.  */
286    if (hi12 == 0) {
287        /* val fits in uimm12: ori rd, zero, val */
288        tcg_out_opc_ori(s, rd, TCG_REG_ZERO, val);
289        return;
290    }
291    if (hi12 == sextreg(lo, 12, 20)) {
292        /* val fits in simm12: addi.w rd, zero, val */
293        tcg_out_opc_addi_w(s, rd, TCG_REG_ZERO, val);
294        return;
295    }
296
297    /* High bits must be set; load with lu12i.w + optional ori.  */
298    tcg_out_opc_lu12i_w(s, rd, hi12);
299    if (lo != 0) {
300        tcg_out_opc_ori(s, rd, rd, lo & 0xfff);
301    }
302}
303
304static void tcg_out_movi(TCGContext *s, TCGType type, TCGReg rd,
305                         tcg_target_long val)
306{
307    /*
308     * LoongArch conventionally loads 64-bit immediates in at most 4 steps,
309     * with dedicated instructions for filling the respective bitfields
310     * below:
311     *
312     *        6                   5                   4               3
313     *  3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2
314     * +-----------------------+---------------------------------------+...
315     * |          hi52         |                  hi32                 |
316     * +-----------------------+---------------------------------------+...
317     *       3                   2                   1
318     *     1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
319     * ...+-------------------------------------+-------------------------+
320     *    |                 hi12                |            lo           |
321     * ...+-------------------------------------+-------------------------+
322     *
323     * Check if val belong to one of the several fast cases, before falling
324     * back to the slow path.
325     */
326
327    intptr_t pc_offset;
328    tcg_target_long val_lo, val_hi, pc_hi, offset_hi;
329    tcg_target_long hi12, hi32, hi52;
330
331    /* Value fits in signed i32.  */
332    if (type == TCG_TYPE_I32 || val == (int32_t)val) {
333        tcg_out_movi_i32(s, rd, val);
334        return;
335    }
336
337    /* PC-relative cases.  */
338    pc_offset = tcg_pcrel_diff(s, (void *)val);
339    if (pc_offset == sextreg(pc_offset, 0, 22) && (pc_offset & 3) == 0) {
340        /* Single pcaddu2i.  */
341        tcg_out_opc_pcaddu2i(s, rd, pc_offset >> 2);
342        return;
343    }
344
345    if (pc_offset == (int32_t)pc_offset) {
346        /* Offset within 32 bits; load with pcalau12i + ori.  */
347        val_lo = sextreg(val, 0, 12);
348        val_hi = val >> 12;
349        pc_hi = (val - pc_offset) >> 12;
350        offset_hi = val_hi - pc_hi;
351
352        tcg_debug_assert(offset_hi == sextreg(offset_hi, 0, 20));
353        tcg_out_opc_pcalau12i(s, rd, offset_hi);
354        if (val_lo != 0) {
355            tcg_out_opc_ori(s, rd, rd, val_lo & 0xfff);
356        }
357        return;
358    }
359
360    hi12 = sextreg(val, 12, 20);
361    hi32 = sextreg(val, 32, 20);
362    hi52 = sextreg(val, 52, 12);
363
364    /* Single cu52i.d case.  */
365    if ((hi52 != 0) && (ctz64(val) >= 52)) {
366        tcg_out_opc_cu52i_d(s, rd, TCG_REG_ZERO, hi52);
367        return;
368    }
369
370    /* Slow path.  Initialize the low 32 bits, then concat high bits.  */
371    tcg_out_movi_i32(s, rd, val);
372
373    /* Load hi32 and hi52 explicitly when they are unexpected values. */
374    if (hi32 != sextreg(hi12, 20, 20)) {
375        tcg_out_opc_cu32i_d(s, rd, hi32);
376    }
377
378    if (hi52 != sextreg(hi32, 20, 12)) {
379        tcg_out_opc_cu52i_d(s, rd, rd, hi52);
380    }
381}
382
383static void tcg_out_addi(TCGContext *s, TCGType type, TCGReg rd,
384                         TCGReg rs, tcg_target_long imm)
385{
386    tcg_target_long lo12 = sextreg(imm, 0, 12);
387    tcg_target_long hi16 = sextreg(imm - lo12, 16, 16);
388
389    /*
390     * Note that there's a hole in between hi16 and lo12:
391     *
392     *       3                   2                   1                   0
393     *     1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
394     * ...+-------------------------------+-------+-----------------------+
395     *    |             hi16              |       |          lo12         |
396     * ...+-------------------------------+-------+-----------------------+
397     *
398     * For bits within that hole, it's more efficient to use LU12I and ADD.
399     */
400    if (imm == (hi16 << 16) + lo12) {
401        if (hi16) {
402            tcg_out_opc_addu16i_d(s, rd, rs, hi16);
403            rs = rd;
404        }
405        if (type == TCG_TYPE_I32) {
406            tcg_out_opc_addi_w(s, rd, rs, lo12);
407        } else if (lo12) {
408            tcg_out_opc_addi_d(s, rd, rs, lo12);
409        } else {
410            tcg_out_mov(s, type, rd, rs);
411        }
412    } else {
413        tcg_out_movi(s, type, TCG_REG_TMP0, imm);
414        if (type == TCG_TYPE_I32) {
415            tcg_out_opc_add_w(s, rd, rs, TCG_REG_TMP0);
416        } else {
417            tcg_out_opc_add_d(s, rd, rs, TCG_REG_TMP0);
418        }
419    }
420}
421
422static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
423{
424    return false;
425}
426
427static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
428                             tcg_target_long imm)
429{
430    /* This function is only used for passing structs by reference. */
431    g_assert_not_reached();
432}
433
434static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg)
435{
436    tcg_out_opc_andi(s, ret, arg, 0xff);
437}
438
439static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg)
440{
441    tcg_out_opc_bstrpick_w(s, ret, arg, 0, 15);
442}
443
444static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg)
445{
446    tcg_out_opc_bstrpick_d(s, ret, arg, 0, 31);
447}
448
449static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
450{
451    tcg_out_opc_sext_b(s, ret, arg);
452}
453
454static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg)
455{
456    tcg_out_opc_sext_h(s, ret, arg);
457}
458
459static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg)
460{
461    tcg_out_opc_addi_w(s, ret, arg, 0);
462}
463
464static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg)
465{
466    if (ret != arg) {
467        tcg_out_ext32s(s, ret, arg);
468    }
469}
470
471static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg)
472{
473    tcg_out_ext32u(s, ret, arg);
474}
475
476static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg)
477{
478    tcg_out_ext32s(s, ret, arg);
479}
480
481static void tcg_out_clzctz(TCGContext *s, LoongArchInsn opc,
482                           TCGReg a0, TCGReg a1, TCGReg a2,
483                           bool c2, bool is_32bit)
484{
485    if (c2) {
486        /*
487         * Fast path: semantics already satisfied due to constraint and
488         * insn behavior, single instruction is enough.
489         */
490        tcg_debug_assert(a2 == (is_32bit ? 32 : 64));
491        /* all clz/ctz insns belong to DJ-format */
492        tcg_out32(s, encode_dj_insn(opc, a0, a1));
493        return;
494    }
495
496    tcg_out32(s, encode_dj_insn(opc, TCG_REG_TMP0, a1));
497    /* a0 = a1 ? REG_TMP0 : a2 */
498    tcg_out_opc_maskeqz(s, TCG_REG_TMP0, TCG_REG_TMP0, a1);
499    tcg_out_opc_masknez(s, a0, a2, a1);
500    tcg_out_opc_or(s, a0, TCG_REG_TMP0, a0);
501}
502
503#define SETCOND_INV    TCG_TARGET_NB_REGS
504#define SETCOND_NEZ    (SETCOND_INV << 1)
505#define SETCOND_FLAGS  (SETCOND_INV | SETCOND_NEZ)
506
507static int tcg_out_setcond_int(TCGContext *s, TCGCond cond, TCGReg ret,
508                               TCGReg arg1, tcg_target_long arg2, bool c2)
509{
510    int flags = 0;
511
512    switch (cond) {
513    case TCG_COND_EQ:    /* -> NE  */
514    case TCG_COND_GE:    /* -> LT  */
515    case TCG_COND_GEU:   /* -> LTU */
516    case TCG_COND_GT:    /* -> LE  */
517    case TCG_COND_GTU:   /* -> LEU */
518        cond = tcg_invert_cond(cond);
519        flags ^= SETCOND_INV;
520        break;
521    default:
522        break;
523    }
524
525    switch (cond) {
526    case TCG_COND_LE:
527    case TCG_COND_LEU:
528        /*
529         * If we have a constant input, the most efficient way to implement
530         * LE is by adding 1 and using LT.  Watch out for wrap around for LEU.
531         * We don't need to care for this for LE because the constant input
532         * is still constrained to int32_t, and INT32_MAX+1 is representable
533         * in the 64-bit temporary register.
534         */
535        if (c2) {
536            if (cond == TCG_COND_LEU) {
537                /* unsigned <= -1 is true */
538                if (arg2 == -1) {
539                    tcg_out_movi(s, TCG_TYPE_REG, ret, !(flags & SETCOND_INV));
540                    return ret;
541                }
542                cond = TCG_COND_LTU;
543            } else {
544                cond = TCG_COND_LT;
545            }
546            arg2 += 1;
547        } else {
548            TCGReg tmp = arg2;
549            arg2 = arg1;
550            arg1 = tmp;
551            cond = tcg_swap_cond(cond);    /* LE -> GE */
552            cond = tcg_invert_cond(cond);  /* GE -> LT */
553            flags ^= SETCOND_INV;
554        }
555        break;
556    default:
557        break;
558    }
559
560    switch (cond) {
561    case TCG_COND_NE:
562        flags |= SETCOND_NEZ;
563        if (!c2) {
564            tcg_out_opc_xor(s, ret, arg1, arg2);
565        } else if (arg2 == 0) {
566            ret = arg1;
567        } else if (arg2 >= 0 && arg2 <= 0xfff) {
568            tcg_out_opc_xori(s, ret, arg1, arg2);
569        } else {
570            tcg_out_addi(s, TCG_TYPE_REG, ret, arg1, -arg2);
571        }
572        break;
573
574    case TCG_COND_LT:
575    case TCG_COND_LTU:
576        if (c2) {
577            if (arg2 >= -0x800 && arg2 <= 0x7ff) {
578                if (cond == TCG_COND_LT) {
579                    tcg_out_opc_slti(s, ret, arg1, arg2);
580                } else {
581                    tcg_out_opc_sltui(s, ret, arg1, arg2);
582                }
583                break;
584            }
585            tcg_out_movi(s, TCG_TYPE_REG, TCG_REG_TMP0, arg2);
586            arg2 = TCG_REG_TMP0;
587        }
588        if (cond == TCG_COND_LT) {
589            tcg_out_opc_slt(s, ret, arg1, arg2);
590        } else {
591            tcg_out_opc_sltu(s, ret, arg1, arg2);
592        }
593        break;
594
595    default:
596        g_assert_not_reached();
597        break;
598    }
599
600    return ret | flags;
601}
602
603static void tcg_out_setcond(TCGContext *s, TCGCond cond, TCGReg ret,
604                            TCGReg arg1, tcg_target_long arg2, bool c2)
605{
606    int tmpflags = tcg_out_setcond_int(s, cond, ret, arg1, arg2, c2);
607
608    if (tmpflags != ret) {
609        TCGReg tmp = tmpflags & ~SETCOND_FLAGS;
610
611        switch (tmpflags & SETCOND_FLAGS) {
612        case SETCOND_INV:
613            /* Intermediate result is boolean: simply invert. */
614            tcg_out_opc_xori(s, ret, tmp, 1);
615            break;
616        case SETCOND_NEZ:
617            /* Intermediate result is zero/non-zero: test != 0. */
618            tcg_out_opc_sltu(s, ret, TCG_REG_ZERO, tmp);
619            break;
620        case SETCOND_NEZ | SETCOND_INV:
621            /* Intermediate result is zero/non-zero: test == 0. */
622            tcg_out_opc_sltui(s, ret, tmp, 1);
623            break;
624        default:
625            g_assert_not_reached();
626        }
627    }
628}
629
630static void tcg_out_movcond(TCGContext *s, TCGCond cond, TCGReg ret,
631                            TCGReg c1, tcg_target_long c2, bool const2,
632                            TCGReg v1, TCGReg v2)
633{
634    int tmpflags = tcg_out_setcond_int(s, cond, TCG_REG_TMP0, c1, c2, const2);
635    TCGReg t;
636
637    /* Standardize the test below to t != 0. */
638    if (tmpflags & SETCOND_INV) {
639        t = v1, v1 = v2, v2 = t;
640    }
641
642    t = tmpflags & ~SETCOND_FLAGS;
643    if (v1 == TCG_REG_ZERO) {
644        tcg_out_opc_masknez(s, ret, v2, t);
645    } else if (v2 == TCG_REG_ZERO) {
646        tcg_out_opc_maskeqz(s, ret, v1, t);
647    } else {
648        tcg_out_opc_masknez(s, TCG_REG_TMP2, v2, t); /* t ? 0 : v2 */
649        tcg_out_opc_maskeqz(s, TCG_REG_TMP1, v1, t); /* t ? v1 : 0 */
650        tcg_out_opc_or(s, ret, TCG_REG_TMP1, TCG_REG_TMP2);
651    }
652}
653
654/*
655 * Branch helpers
656 */
657
658static const struct {
659    LoongArchInsn op;
660    bool swap;
661} tcg_brcond_to_loongarch[] = {
662    [TCG_COND_EQ] =  { OPC_BEQ,  false },
663    [TCG_COND_NE] =  { OPC_BNE,  false },
664    [TCG_COND_LT] =  { OPC_BGT,  true  },
665    [TCG_COND_GE] =  { OPC_BLE,  true  },
666    [TCG_COND_LE] =  { OPC_BLE,  false },
667    [TCG_COND_GT] =  { OPC_BGT,  false },
668    [TCG_COND_LTU] = { OPC_BGTU, true  },
669    [TCG_COND_GEU] = { OPC_BLEU, true  },
670    [TCG_COND_LEU] = { OPC_BLEU, false },
671    [TCG_COND_GTU] = { OPC_BGTU, false }
672};
673
674static void tcg_out_brcond(TCGContext *s, TCGCond cond, TCGReg arg1,
675                           TCGReg arg2, TCGLabel *l)
676{
677    LoongArchInsn op = tcg_brcond_to_loongarch[cond].op;
678
679    tcg_debug_assert(op != 0);
680
681    if (tcg_brcond_to_loongarch[cond].swap) {
682        TCGReg t = arg1;
683        arg1 = arg2;
684        arg2 = t;
685    }
686
687    /* all conditional branch insns belong to DJSk16-format */
688    tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SK16, l, 0);
689    tcg_out32(s, encode_djsk16_insn(op, arg1, arg2, 0));
690}
691
692static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *arg, bool tail)
693{
694    TCGReg link = tail ? TCG_REG_ZERO : TCG_REG_RA;
695    ptrdiff_t offset = tcg_pcrel_diff(s, arg);
696
697    tcg_debug_assert((offset & 3) == 0);
698    if (offset == sextreg(offset, 0, 28)) {
699        /* short jump: +/- 256MiB */
700        if (tail) {
701            tcg_out_opc_b(s, offset >> 2);
702        } else {
703            tcg_out_opc_bl(s, offset >> 2);
704        }
705    } else if (offset == sextreg(offset, 0, 38)) {
706        /* long jump: +/- 256GiB */
707        tcg_target_long lo = sextreg(offset, 0, 18);
708        tcg_target_long hi = offset - lo;
709        tcg_out_opc_pcaddu18i(s, TCG_REG_TMP0, hi >> 18);
710        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
711    } else {
712        /* far jump: 64-bit */
713        tcg_target_long lo = sextreg((tcg_target_long)arg, 0, 18);
714        tcg_target_long hi = (tcg_target_long)arg - lo;
715        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP0, hi);
716        tcg_out_opc_jirl(s, link, TCG_REG_TMP0, lo >> 2);
717    }
718}
719
720static void tcg_out_call(TCGContext *s, const tcg_insn_unit *arg,
721                         const TCGHelperInfo *info)
722{
723    tcg_out_call_int(s, arg, false);
724}
725
726/*
727 * Load/store helpers
728 */
729
730static void tcg_out_ldst(TCGContext *s, LoongArchInsn opc, TCGReg data,
731                         TCGReg addr, intptr_t offset)
732{
733    intptr_t imm12 = sextreg(offset, 0, 12);
734
735    if (offset != imm12) {
736        intptr_t diff = tcg_pcrel_diff(s, (void *)offset);
737
738        if (addr == TCG_REG_ZERO && diff == (int32_t)diff) {
739            imm12 = sextreg(diff, 0, 12);
740            tcg_out_opc_pcaddu12i(s, TCG_REG_TMP2, (diff - imm12) >> 12);
741        } else {
742            tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_TMP2, offset - imm12);
743            if (addr != TCG_REG_ZERO) {
744                tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, addr);
745            }
746        }
747        addr = TCG_REG_TMP2;
748    }
749
750    switch (opc) {
751    case OPC_LD_B:
752    case OPC_LD_BU:
753    case OPC_LD_H:
754    case OPC_LD_HU:
755    case OPC_LD_W:
756    case OPC_LD_WU:
757    case OPC_LD_D:
758    case OPC_ST_B:
759    case OPC_ST_H:
760    case OPC_ST_W:
761    case OPC_ST_D:
762        tcg_out32(s, encode_djsk12_insn(opc, data, addr, imm12));
763        break;
764    default:
765        g_assert_not_reached();
766    }
767}
768
769static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg arg,
770                       TCGReg arg1, intptr_t arg2)
771{
772    bool is_32bit = type == TCG_TYPE_I32;
773    tcg_out_ldst(s, is_32bit ? OPC_LD_W : OPC_LD_D, arg, arg1, arg2);
774}
775
776static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
777                       TCGReg arg1, intptr_t arg2)
778{
779    bool is_32bit = type == TCG_TYPE_I32;
780    tcg_out_ldst(s, is_32bit ? OPC_ST_W : OPC_ST_D, arg, arg1, arg2);
781}
782
783static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
784                        TCGReg base, intptr_t ofs)
785{
786    if (val == 0) {
787        tcg_out_st(s, type, TCG_REG_ZERO, base, ofs);
788        return true;
789    }
790    return false;
791}
792
793/*
794 * Load/store helpers for SoftMMU, and qemu_ld/st implementations
795 */
796
797#if defined(CONFIG_SOFTMMU)
798/*
799 * helper signature: helper_ret_ld_mmu(CPUState *env, target_ulong addr,
800 *                                     MemOpIdx oi, uintptr_t ra)
801 */
802static void * const qemu_ld_helpers[4] = {
803    [MO_8]  = helper_ret_ldub_mmu,
804    [MO_16] = helper_le_lduw_mmu,
805    [MO_32] = helper_le_ldul_mmu,
806    [MO_64] = helper_le_ldq_mmu,
807};
808
809/*
810 * helper signature: helper_ret_st_mmu(CPUState *env, target_ulong addr,
811 *                                     uintxx_t val, MemOpIdx oi,
812 *                                     uintptr_t ra)
813 */
814static void * const qemu_st_helpers[4] = {
815    [MO_8]  = helper_ret_stb_mmu,
816    [MO_16] = helper_le_stw_mmu,
817    [MO_32] = helper_le_stl_mmu,
818    [MO_64] = helper_le_stq_mmu,
819};
820
821/* We expect to use a 12-bit negative offset from ENV.  */
822QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) > 0);
823QEMU_BUILD_BUG_ON(TLB_MASK_TABLE_OFS(0) < -(1 << 11));
824
825static bool tcg_out_goto(TCGContext *s, const tcg_insn_unit *target)
826{
827    tcg_out_opc_b(s, 0);
828    return reloc_br_sd10k16(s->code_ptr - 1, target);
829}
830
831/*
832 * Emits common code for TLB addend lookup, that eventually loads the
833 * addend in TCG_REG_TMP2.
834 */
835static void tcg_out_tlb_load(TCGContext *s, TCGReg addrl, MemOpIdx oi,
836                             tcg_insn_unit **label_ptr, bool is_load)
837{
838    MemOp opc = get_memop(oi);
839    unsigned s_bits = opc & MO_SIZE;
840    unsigned a_bits = get_alignment_bits(opc);
841    tcg_target_long compare_mask;
842    int mem_index = get_mmuidx(oi);
843    int fast_ofs = TLB_MASK_TABLE_OFS(mem_index);
844    int mask_ofs = fast_ofs + offsetof(CPUTLBDescFast, mask);
845    int table_ofs = fast_ofs + offsetof(CPUTLBDescFast, table);
846
847    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_AREG0, mask_ofs);
848    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP1, TCG_AREG0, table_ofs);
849
850    tcg_out_opc_srli_d(s, TCG_REG_TMP2, addrl,
851                    TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
852    tcg_out_opc_and(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP0);
853    tcg_out_opc_add_d(s, TCG_REG_TMP2, TCG_REG_TMP2, TCG_REG_TMP1);
854
855    /* Load the tlb comparator and the addend.  */
856    tcg_out_ld(s, TCG_TYPE_TL, TCG_REG_TMP0, TCG_REG_TMP2,
857               is_load ? offsetof(CPUTLBEntry, addr_read)
858               : offsetof(CPUTLBEntry, addr_write));
859    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP2, TCG_REG_TMP2,
860               offsetof(CPUTLBEntry, addend));
861
862    /* We don't support unaligned accesses.  */
863    if (a_bits < s_bits) {
864        a_bits = s_bits;
865    }
866    /* Clear the non-page, non-alignment bits from the address.  */
867    compare_mask = (tcg_target_long)TARGET_PAGE_MASK | ((1 << a_bits) - 1);
868    tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_TMP1, compare_mask);
869    tcg_out_opc_and(s, TCG_REG_TMP1, TCG_REG_TMP1, addrl);
870
871    /* Compare masked address with the TLB entry.  */
872    label_ptr[0] = s->code_ptr;
873    tcg_out_opc_bne(s, TCG_REG_TMP0, TCG_REG_TMP1, 0);
874
875    /* TLB Hit - addend in TCG_REG_TMP2, ready for use.  */
876}
877
878static void add_qemu_ldst_label(TCGContext *s, int is_ld, MemOpIdx oi,
879                                TCGType type,
880                                TCGReg datalo, TCGReg addrlo,
881                                void *raddr, tcg_insn_unit **label_ptr)
882{
883    TCGLabelQemuLdst *label = new_ldst_label(s);
884
885    label->is_ld = is_ld;
886    label->oi = oi;
887    label->type = type;
888    label->datalo_reg = datalo;
889    label->datahi_reg = 0; /* unused */
890    label->addrlo_reg = addrlo;
891    label->addrhi_reg = 0; /* unused */
892    label->raddr = tcg_splitwx_to_rx(raddr);
893    label->label_ptr[0] = label_ptr[0];
894}
895
896static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
897{
898    MemOpIdx oi = l->oi;
899    MemOp opc = get_memop(oi);
900    MemOp size = opc & MO_SIZE;
901
902    /* resolve label address */
903    if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
904        return false;
905    }
906
907    /* call load helper */
908    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
909    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A1, l->addrlo_reg);
910    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A2, oi);
911    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A3, (tcg_target_long)l->raddr);
912
913    tcg_out_call_int(s, qemu_ld_helpers[size], false);
914
915    tcg_out_movext(s, l->type, l->datalo_reg,
916                   TCG_TYPE_REG, opc & MO_SSIZE, TCG_REG_A0);
917    return tcg_out_goto(s, l->raddr);
918}
919
920static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
921{
922    MemOpIdx oi = l->oi;
923    MemOp opc = get_memop(oi);
924    MemOp size = opc & MO_SIZE;
925
926    /* resolve label address */
927    if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
928        return false;
929    }
930
931    /* call store helper */
932    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
933    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A1, l->addrlo_reg);
934    tcg_out_movext(s, size == MO_64 ? TCG_TYPE_I32 : TCG_TYPE_I32, TCG_REG_A2,
935                   l->type, size, l->datalo_reg);
936    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A3, oi);
937    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A4, (tcg_target_long)l->raddr);
938
939    tcg_out_call_int(s, qemu_st_helpers[size], false);
940
941    return tcg_out_goto(s, l->raddr);
942}
943#else
944
945/*
946 * Alignment helpers for user-mode emulation
947 */
948
949static void tcg_out_test_alignment(TCGContext *s, bool is_ld, TCGReg addr_reg,
950                                   unsigned a_bits)
951{
952    TCGLabelQemuLdst *l = new_ldst_label(s);
953
954    l->is_ld = is_ld;
955    l->addrlo_reg = addr_reg;
956
957    /*
958     * Without micro-architecture details, we don't know which of bstrpick or
959     * andi is faster, so use bstrpick as it's not constrained by imm field
960     * width. (Not to say alignments >= 2^12 are going to happen any time
961     * soon, though)
962     */
963    tcg_out_opc_bstrpick_d(s, TCG_REG_TMP1, addr_reg, 0, a_bits - 1);
964
965    l->label_ptr[0] = s->code_ptr;
966    tcg_out_opc_bne(s, TCG_REG_TMP1, TCG_REG_ZERO, 0);
967
968    l->raddr = tcg_splitwx_to_rx(s->code_ptr);
969}
970
971static bool tcg_out_fail_alignment(TCGContext *s, TCGLabelQemuLdst *l)
972{
973    /* resolve label address */
974    if (!reloc_br_sk16(l->label_ptr[0], tcg_splitwx_to_rx(s->code_ptr))) {
975        return false;
976    }
977
978    tcg_out_mov(s, TCG_TYPE_TL, TCG_REG_A1, l->addrlo_reg);
979    tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_A0, TCG_AREG0);
980
981    /* tail call, with the return address back inline. */
982    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_RA, (uintptr_t)l->raddr);
983    tcg_out_call_int(s, (const void *)(l->is_ld ? helper_unaligned_ld
984                                       : helper_unaligned_st), true);
985    return true;
986}
987
988static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
989{
990    return tcg_out_fail_alignment(s, l);
991}
992
993static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *l)
994{
995    return tcg_out_fail_alignment(s, l);
996}
997
998#endif /* CONFIG_SOFTMMU */
999
1000/*
1001 * `ext32u` the address register into the temp register given,
1002 * if target is 32-bit, no-op otherwise.
1003 *
1004 * Returns the address register ready for use with TLB addend.
1005 */
1006static TCGReg tcg_out_zext_addr_if_32_bit(TCGContext *s,
1007                                          TCGReg addr, TCGReg tmp)
1008{
1009    if (TARGET_LONG_BITS == 32) {
1010        tcg_out_ext32u(s, tmp, addr);
1011        return tmp;
1012    }
1013    return addr;
1014}
1015
1016typedef struct {
1017    TCGReg base;
1018    TCGReg index;
1019} HostAddress;
1020
1021static void tcg_out_qemu_ld_indexed(TCGContext *s, MemOp opc, TCGType type,
1022                                    TCGReg rd, HostAddress h)
1023{
1024    /* Byte swapping is left to middle-end expansion.  */
1025    tcg_debug_assert((opc & MO_BSWAP) == 0);
1026
1027    switch (opc & MO_SSIZE) {
1028    case MO_UB:
1029        tcg_out_opc_ldx_bu(s, rd, h.base, h.index);
1030        break;
1031    case MO_SB:
1032        tcg_out_opc_ldx_b(s, rd, h.base, h.index);
1033        break;
1034    case MO_UW:
1035        tcg_out_opc_ldx_hu(s, rd, h.base, h.index);
1036        break;
1037    case MO_SW:
1038        tcg_out_opc_ldx_h(s, rd, h.base, h.index);
1039        break;
1040    case MO_UL:
1041        if (type == TCG_TYPE_I64) {
1042            tcg_out_opc_ldx_wu(s, rd, h.base, h.index);
1043            break;
1044        }
1045        /* fallthrough */
1046    case MO_SL:
1047        tcg_out_opc_ldx_w(s, rd, h.base, h.index);
1048        break;
1049    case MO_UQ:
1050        tcg_out_opc_ldx_d(s, rd, h.base, h.index);
1051        break;
1052    default:
1053        g_assert_not_reached();
1054    }
1055}
1056
1057static void tcg_out_qemu_ld(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1058                            MemOpIdx oi, TCGType data_type)
1059{
1060    MemOp opc = get_memop(oi);
1061    HostAddress h;
1062
1063#ifdef CONFIG_SOFTMMU
1064    tcg_insn_unit *label_ptr[1];
1065
1066    tcg_out_tlb_load(s, addr_reg, oi, label_ptr, 1);
1067    h.index = TCG_REG_TMP2;
1068#else
1069    unsigned a_bits = get_alignment_bits(opc);
1070    if (a_bits) {
1071        tcg_out_test_alignment(s, true, addr_reg, a_bits);
1072    }
1073    h.index = USE_GUEST_BASE ? TCG_GUEST_BASE_REG : TCG_REG_ZERO;
1074#endif
1075
1076    h.base = tcg_out_zext_addr_if_32_bit(s, addr_reg, TCG_REG_TMP0);
1077    tcg_out_qemu_ld_indexed(s, opc, data_type, data_reg, h);
1078
1079#ifdef CONFIG_SOFTMMU
1080    add_qemu_ldst_label(s, true, oi, data_type, data_reg, addr_reg,
1081                        s->code_ptr, label_ptr);
1082#endif
1083}
1084
1085static void tcg_out_qemu_st_indexed(TCGContext *s, MemOp opc,
1086                                    TCGReg rd, HostAddress h)
1087{
1088    /* Byte swapping is left to middle-end expansion.  */
1089    tcg_debug_assert((opc & MO_BSWAP) == 0);
1090
1091    switch (opc & MO_SIZE) {
1092    case MO_8:
1093        tcg_out_opc_stx_b(s, rd, h.base, h.index);
1094        break;
1095    case MO_16:
1096        tcg_out_opc_stx_h(s, rd, h.base, h.index);
1097        break;
1098    case MO_32:
1099        tcg_out_opc_stx_w(s, rd, h.base, h.index);
1100        break;
1101    case MO_64:
1102        tcg_out_opc_stx_d(s, rd, h.base, h.index);
1103        break;
1104    default:
1105        g_assert_not_reached();
1106    }
1107}
1108
1109static void tcg_out_qemu_st(TCGContext *s, TCGReg data_reg, TCGReg addr_reg,
1110                            MemOpIdx oi, TCGType data_type)
1111{
1112    MemOp opc = get_memop(oi);
1113    HostAddress h;
1114
1115#ifdef CONFIG_SOFTMMU
1116    tcg_insn_unit *label_ptr[1];
1117
1118    tcg_out_tlb_load(s, addr_reg, oi, label_ptr, 0);
1119    h.index = TCG_REG_TMP2;
1120#else
1121    unsigned a_bits = get_alignment_bits(opc);
1122    if (a_bits) {
1123        tcg_out_test_alignment(s, false, addr_reg, a_bits);
1124    }
1125    h.index = USE_GUEST_BASE ? TCG_GUEST_BASE_REG : TCG_REG_ZERO;
1126#endif
1127
1128    h.base = tcg_out_zext_addr_if_32_bit(s, addr_reg, TCG_REG_TMP0);
1129    tcg_out_qemu_st_indexed(s, opc, data_reg, h);
1130
1131#ifdef CONFIG_SOFTMMU
1132    add_qemu_ldst_label(s, false, oi, data_type, data_reg, addr_reg,
1133                        s->code_ptr, label_ptr);
1134#endif
1135}
1136
1137/*
1138 * Entry-points
1139 */
1140
1141static const tcg_insn_unit *tb_ret_addr;
1142
1143static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1144{
1145    /* Reuse the zeroing that exists for goto_ptr.  */
1146    if (a0 == 0) {
1147        tcg_out_call_int(s, tcg_code_gen_epilogue, true);
1148    } else {
1149        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_A0, a0);
1150        tcg_out_call_int(s, tb_ret_addr, true);
1151    }
1152}
1153
1154static void tcg_out_goto_tb(TCGContext *s, int which)
1155{
1156    /*
1157     * Direct branch, or load indirect address, to be patched
1158     * by tb_target_set_jmp_target.  Check indirect load offset
1159     * in range early, regardless of direct branch distance,
1160     * via assert within tcg_out_opc_pcaddu2i.
1161     */
1162    uintptr_t i_addr = get_jmp_target_addr(s, which);
1163    intptr_t i_disp = tcg_pcrel_diff(s, (void *)i_addr);
1164
1165    set_jmp_insn_offset(s, which);
1166    tcg_out_opc_pcaddu2i(s, TCG_REG_TMP0, i_disp >> 2);
1167
1168    /* Finish the load and indirect branch. */
1169    tcg_out_ld(s, TCG_TYPE_PTR, TCG_REG_TMP0, TCG_REG_TMP0, 0);
1170    tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_TMP0, 0);
1171    set_jmp_reset_offset(s, which);
1172}
1173
1174void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1175                              uintptr_t jmp_rx, uintptr_t jmp_rw)
1176{
1177    uintptr_t d_addr = tb->jmp_target_addr[n];
1178    ptrdiff_t d_disp = (ptrdiff_t)(d_addr - jmp_rx) >> 2;
1179    tcg_insn_unit insn;
1180
1181    /* Either directly branch, or load slot address for indirect branch. */
1182    if (d_disp == sextreg(d_disp, 0, 26)) {
1183        insn = encode_sd10k16_insn(OPC_B, d_disp);
1184    } else {
1185        uintptr_t i_addr = (uintptr_t)&tb->jmp_target_addr[n];
1186        intptr_t i_disp = i_addr - jmp_rx;
1187        insn = encode_dsj20_insn(OPC_PCADDU2I, TCG_REG_TMP0, i_disp >> 2);
1188    }
1189
1190    qatomic_set((tcg_insn_unit *)jmp_rw, insn);
1191    flush_idcache_range(jmp_rx, jmp_rw, 4);
1192}
1193
1194static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1195                       const TCGArg args[TCG_MAX_OP_ARGS],
1196                       const int const_args[TCG_MAX_OP_ARGS])
1197{
1198    TCGArg a0 = args[0];
1199    TCGArg a1 = args[1];
1200    TCGArg a2 = args[2];
1201    int c2 = const_args[2];
1202
1203    switch (opc) {
1204    case INDEX_op_mb:
1205        tcg_out_mb(s, a0);
1206        break;
1207
1208    case INDEX_op_goto_ptr:
1209        tcg_out_opc_jirl(s, TCG_REG_ZERO, a0, 0);
1210        break;
1211
1212    case INDEX_op_br:
1213        tcg_out_reloc(s, s->code_ptr, R_LOONGARCH_BR_SD10K16, arg_label(a0),
1214                      0);
1215        tcg_out_opc_b(s, 0);
1216        break;
1217
1218    case INDEX_op_brcond_i32:
1219    case INDEX_op_brcond_i64:
1220        tcg_out_brcond(s, a2, a0, a1, arg_label(args[3]));
1221        break;
1222
1223    case INDEX_op_extrh_i64_i32:
1224        tcg_out_opc_srai_d(s, a0, a1, 32);
1225        break;
1226
1227    case INDEX_op_not_i32:
1228    case INDEX_op_not_i64:
1229        tcg_out_opc_nor(s, a0, a1, TCG_REG_ZERO);
1230        break;
1231
1232    case INDEX_op_nor_i32:
1233    case INDEX_op_nor_i64:
1234        if (c2) {
1235            tcg_out_opc_ori(s, a0, a1, a2);
1236            tcg_out_opc_nor(s, a0, a0, TCG_REG_ZERO);
1237        } else {
1238            tcg_out_opc_nor(s, a0, a1, a2);
1239        }
1240        break;
1241
1242    case INDEX_op_andc_i32:
1243    case INDEX_op_andc_i64:
1244        if (c2) {
1245            /* guaranteed to fit due to constraint */
1246            tcg_out_opc_andi(s, a0, a1, ~a2);
1247        } else {
1248            tcg_out_opc_andn(s, a0, a1, a2);
1249        }
1250        break;
1251
1252    case INDEX_op_orc_i32:
1253    case INDEX_op_orc_i64:
1254        if (c2) {
1255            /* guaranteed to fit due to constraint */
1256            tcg_out_opc_ori(s, a0, a1, ~a2);
1257        } else {
1258            tcg_out_opc_orn(s, a0, a1, a2);
1259        }
1260        break;
1261
1262    case INDEX_op_and_i32:
1263    case INDEX_op_and_i64:
1264        if (c2) {
1265            tcg_out_opc_andi(s, a0, a1, a2);
1266        } else {
1267            tcg_out_opc_and(s, a0, a1, a2);
1268        }
1269        break;
1270
1271    case INDEX_op_or_i32:
1272    case INDEX_op_or_i64:
1273        if (c2) {
1274            tcg_out_opc_ori(s, a0, a1, a2);
1275        } else {
1276            tcg_out_opc_or(s, a0, a1, a2);
1277        }
1278        break;
1279
1280    case INDEX_op_xor_i32:
1281    case INDEX_op_xor_i64:
1282        if (c2) {
1283            tcg_out_opc_xori(s, a0, a1, a2);
1284        } else {
1285            tcg_out_opc_xor(s, a0, a1, a2);
1286        }
1287        break;
1288
1289    case INDEX_op_extract_i32:
1290        tcg_out_opc_bstrpick_w(s, a0, a1, a2, a2 + args[3] - 1);
1291        break;
1292    case INDEX_op_extract_i64:
1293        tcg_out_opc_bstrpick_d(s, a0, a1, a2, a2 + args[3] - 1);
1294        break;
1295
1296    case INDEX_op_deposit_i32:
1297        tcg_out_opc_bstrins_w(s, a0, a2, args[3], args[3] + args[4] - 1);
1298        break;
1299    case INDEX_op_deposit_i64:
1300        tcg_out_opc_bstrins_d(s, a0, a2, args[3], args[3] + args[4] - 1);
1301        break;
1302
1303    case INDEX_op_bswap16_i32:
1304    case INDEX_op_bswap16_i64:
1305        tcg_out_opc_revb_2h(s, a0, a1);
1306        if (a2 & TCG_BSWAP_OS) {
1307            tcg_out_ext16s(s, TCG_TYPE_REG, a0, a0);
1308        } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1309            tcg_out_ext16u(s, a0, a0);
1310        }
1311        break;
1312
1313    case INDEX_op_bswap32_i32:
1314        /* All 32-bit values are computed sign-extended in the register.  */
1315        a2 = TCG_BSWAP_OS;
1316        /* fallthrough */
1317    case INDEX_op_bswap32_i64:
1318        tcg_out_opc_revb_2w(s, a0, a1);
1319        if (a2 & TCG_BSWAP_OS) {
1320            tcg_out_ext32s(s, a0, a0);
1321        } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
1322            tcg_out_ext32u(s, a0, a0);
1323        }
1324        break;
1325
1326    case INDEX_op_bswap64_i64:
1327        tcg_out_opc_revb_d(s, a0, a1);
1328        break;
1329
1330    case INDEX_op_clz_i32:
1331        tcg_out_clzctz(s, OPC_CLZ_W, a0, a1, a2, c2, true);
1332        break;
1333    case INDEX_op_clz_i64:
1334        tcg_out_clzctz(s, OPC_CLZ_D, a0, a1, a2, c2, false);
1335        break;
1336
1337    case INDEX_op_ctz_i32:
1338        tcg_out_clzctz(s, OPC_CTZ_W, a0, a1, a2, c2, true);
1339        break;
1340    case INDEX_op_ctz_i64:
1341        tcg_out_clzctz(s, OPC_CTZ_D, a0, a1, a2, c2, false);
1342        break;
1343
1344    case INDEX_op_shl_i32:
1345        if (c2) {
1346            tcg_out_opc_slli_w(s, a0, a1, a2 & 0x1f);
1347        } else {
1348            tcg_out_opc_sll_w(s, a0, a1, a2);
1349        }
1350        break;
1351    case INDEX_op_shl_i64:
1352        if (c2) {
1353            tcg_out_opc_slli_d(s, a0, a1, a2 & 0x3f);
1354        } else {
1355            tcg_out_opc_sll_d(s, a0, a1, a2);
1356        }
1357        break;
1358
1359    case INDEX_op_shr_i32:
1360        if (c2) {
1361            tcg_out_opc_srli_w(s, a0, a1, a2 & 0x1f);
1362        } else {
1363            tcg_out_opc_srl_w(s, a0, a1, a2);
1364        }
1365        break;
1366    case INDEX_op_shr_i64:
1367        if (c2) {
1368            tcg_out_opc_srli_d(s, a0, a1, a2 & 0x3f);
1369        } else {
1370            tcg_out_opc_srl_d(s, a0, a1, a2);
1371        }
1372        break;
1373
1374    case INDEX_op_sar_i32:
1375        if (c2) {
1376            tcg_out_opc_srai_w(s, a0, a1, a2 & 0x1f);
1377        } else {
1378            tcg_out_opc_sra_w(s, a0, a1, a2);
1379        }
1380        break;
1381    case INDEX_op_sar_i64:
1382        if (c2) {
1383            tcg_out_opc_srai_d(s, a0, a1, a2 & 0x3f);
1384        } else {
1385            tcg_out_opc_sra_d(s, a0, a1, a2);
1386        }
1387        break;
1388
1389    case INDEX_op_rotl_i32:
1390        /* transform into equivalent rotr/rotri */
1391        if (c2) {
1392            tcg_out_opc_rotri_w(s, a0, a1, (32 - a2) & 0x1f);
1393        } else {
1394            tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
1395            tcg_out_opc_rotr_w(s, a0, a1, TCG_REG_TMP0);
1396        }
1397        break;
1398    case INDEX_op_rotl_i64:
1399        /* transform into equivalent rotr/rotri */
1400        if (c2) {
1401            tcg_out_opc_rotri_d(s, a0, a1, (64 - a2) & 0x3f);
1402        } else {
1403            tcg_out_opc_sub_w(s, TCG_REG_TMP0, TCG_REG_ZERO, a2);
1404            tcg_out_opc_rotr_d(s, a0, a1, TCG_REG_TMP0);
1405        }
1406        break;
1407
1408    case INDEX_op_rotr_i32:
1409        if (c2) {
1410            tcg_out_opc_rotri_w(s, a0, a1, a2 & 0x1f);
1411        } else {
1412            tcg_out_opc_rotr_w(s, a0, a1, a2);
1413        }
1414        break;
1415    case INDEX_op_rotr_i64:
1416        if (c2) {
1417            tcg_out_opc_rotri_d(s, a0, a1, a2 & 0x3f);
1418        } else {
1419            tcg_out_opc_rotr_d(s, a0, a1, a2);
1420        }
1421        break;
1422
1423    case INDEX_op_add_i32:
1424        if (c2) {
1425            tcg_out_addi(s, TCG_TYPE_I32, a0, a1, a2);
1426        } else {
1427            tcg_out_opc_add_w(s, a0, a1, a2);
1428        }
1429        break;
1430    case INDEX_op_add_i64:
1431        if (c2) {
1432            tcg_out_addi(s, TCG_TYPE_I64, a0, a1, a2);
1433        } else {
1434            tcg_out_opc_add_d(s, a0, a1, a2);
1435        }
1436        break;
1437
1438    case INDEX_op_sub_i32:
1439        if (c2) {
1440            tcg_out_addi(s, TCG_TYPE_I32, a0, a1, -a2);
1441        } else {
1442            tcg_out_opc_sub_w(s, a0, a1, a2);
1443        }
1444        break;
1445    case INDEX_op_sub_i64:
1446        if (c2) {
1447            tcg_out_addi(s, TCG_TYPE_I64, a0, a1, -a2);
1448        } else {
1449            tcg_out_opc_sub_d(s, a0, a1, a2);
1450        }
1451        break;
1452
1453    case INDEX_op_mul_i32:
1454        tcg_out_opc_mul_w(s, a0, a1, a2);
1455        break;
1456    case INDEX_op_mul_i64:
1457        tcg_out_opc_mul_d(s, a0, a1, a2);
1458        break;
1459
1460    case INDEX_op_mulsh_i32:
1461        tcg_out_opc_mulh_w(s, a0, a1, a2);
1462        break;
1463    case INDEX_op_mulsh_i64:
1464        tcg_out_opc_mulh_d(s, a0, a1, a2);
1465        break;
1466
1467    case INDEX_op_muluh_i32:
1468        tcg_out_opc_mulh_wu(s, a0, a1, a2);
1469        break;
1470    case INDEX_op_muluh_i64:
1471        tcg_out_opc_mulh_du(s, a0, a1, a2);
1472        break;
1473
1474    case INDEX_op_div_i32:
1475        tcg_out_opc_div_w(s, a0, a1, a2);
1476        break;
1477    case INDEX_op_div_i64:
1478        tcg_out_opc_div_d(s, a0, a1, a2);
1479        break;
1480
1481    case INDEX_op_divu_i32:
1482        tcg_out_opc_div_wu(s, a0, a1, a2);
1483        break;
1484    case INDEX_op_divu_i64:
1485        tcg_out_opc_div_du(s, a0, a1, a2);
1486        break;
1487
1488    case INDEX_op_rem_i32:
1489        tcg_out_opc_mod_w(s, a0, a1, a2);
1490        break;
1491    case INDEX_op_rem_i64:
1492        tcg_out_opc_mod_d(s, a0, a1, a2);
1493        break;
1494
1495    case INDEX_op_remu_i32:
1496        tcg_out_opc_mod_wu(s, a0, a1, a2);
1497        break;
1498    case INDEX_op_remu_i64:
1499        tcg_out_opc_mod_du(s, a0, a1, a2);
1500        break;
1501
1502    case INDEX_op_setcond_i32:
1503    case INDEX_op_setcond_i64:
1504        tcg_out_setcond(s, args[3], a0, a1, a2, c2);
1505        break;
1506
1507    case INDEX_op_movcond_i32:
1508    case INDEX_op_movcond_i64:
1509        tcg_out_movcond(s, args[5], a0, a1, a2, c2, args[3], args[4]);
1510        break;
1511
1512    case INDEX_op_ld8s_i32:
1513    case INDEX_op_ld8s_i64:
1514        tcg_out_ldst(s, OPC_LD_B, a0, a1, a2);
1515        break;
1516    case INDEX_op_ld8u_i32:
1517    case INDEX_op_ld8u_i64:
1518        tcg_out_ldst(s, OPC_LD_BU, a0, a1, a2);
1519        break;
1520    case INDEX_op_ld16s_i32:
1521    case INDEX_op_ld16s_i64:
1522        tcg_out_ldst(s, OPC_LD_H, a0, a1, a2);
1523        break;
1524    case INDEX_op_ld16u_i32:
1525    case INDEX_op_ld16u_i64:
1526        tcg_out_ldst(s, OPC_LD_HU, a0, a1, a2);
1527        break;
1528    case INDEX_op_ld_i32:
1529    case INDEX_op_ld32s_i64:
1530        tcg_out_ldst(s, OPC_LD_W, a0, a1, a2);
1531        break;
1532    case INDEX_op_ld32u_i64:
1533        tcg_out_ldst(s, OPC_LD_WU, a0, a1, a2);
1534        break;
1535    case INDEX_op_ld_i64:
1536        tcg_out_ldst(s, OPC_LD_D, a0, a1, a2);
1537        break;
1538
1539    case INDEX_op_st8_i32:
1540    case INDEX_op_st8_i64:
1541        tcg_out_ldst(s, OPC_ST_B, a0, a1, a2);
1542        break;
1543    case INDEX_op_st16_i32:
1544    case INDEX_op_st16_i64:
1545        tcg_out_ldst(s, OPC_ST_H, a0, a1, a2);
1546        break;
1547    case INDEX_op_st_i32:
1548    case INDEX_op_st32_i64:
1549        tcg_out_ldst(s, OPC_ST_W, a0, a1, a2);
1550        break;
1551    case INDEX_op_st_i64:
1552        tcg_out_ldst(s, OPC_ST_D, a0, a1, a2);
1553        break;
1554
1555    case INDEX_op_qemu_ld_i32:
1556        tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I32);
1557        break;
1558    case INDEX_op_qemu_ld_i64:
1559        tcg_out_qemu_ld(s, a0, a1, a2, TCG_TYPE_I64);
1560        break;
1561    case INDEX_op_qemu_st_i32:
1562        tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I32);
1563        break;
1564    case INDEX_op_qemu_st_i64:
1565        tcg_out_qemu_st(s, a0, a1, a2, TCG_TYPE_I64);
1566        break;
1567
1568    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
1569    case INDEX_op_mov_i64:
1570    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
1571    case INDEX_op_exit_tb:  /* Always emitted via tcg_out_exit_tb.  */
1572    case INDEX_op_goto_tb:  /* Always emitted via tcg_out_goto_tb.  */
1573    case INDEX_op_ext8s_i32:  /* Always emitted via tcg_reg_alloc_op.  */
1574    case INDEX_op_ext8s_i64:
1575    case INDEX_op_ext8u_i32:
1576    case INDEX_op_ext8u_i64:
1577    case INDEX_op_ext16s_i32:
1578    case INDEX_op_ext16s_i64:
1579    case INDEX_op_ext16u_i32:
1580    case INDEX_op_ext16u_i64:
1581    case INDEX_op_ext32s_i64:
1582    case INDEX_op_ext32u_i64:
1583    case INDEX_op_ext_i32_i64:
1584    case INDEX_op_extu_i32_i64:
1585    case INDEX_op_extrl_i64_i32:
1586    default:
1587        g_assert_not_reached();
1588    }
1589}
1590
1591static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
1592{
1593    switch (op) {
1594    case INDEX_op_goto_ptr:
1595        return C_O0_I1(r);
1596
1597    case INDEX_op_st8_i32:
1598    case INDEX_op_st8_i64:
1599    case INDEX_op_st16_i32:
1600    case INDEX_op_st16_i64:
1601    case INDEX_op_st32_i64:
1602    case INDEX_op_st_i32:
1603    case INDEX_op_st_i64:
1604        return C_O0_I2(rZ, r);
1605
1606    case INDEX_op_brcond_i32:
1607    case INDEX_op_brcond_i64:
1608        return C_O0_I2(rZ, rZ);
1609
1610    case INDEX_op_qemu_st_i32:
1611    case INDEX_op_qemu_st_i64:
1612        return C_O0_I2(LZ, L);
1613
1614    case INDEX_op_ext8s_i32:
1615    case INDEX_op_ext8s_i64:
1616    case INDEX_op_ext8u_i32:
1617    case INDEX_op_ext8u_i64:
1618    case INDEX_op_ext16s_i32:
1619    case INDEX_op_ext16s_i64:
1620    case INDEX_op_ext16u_i32:
1621    case INDEX_op_ext16u_i64:
1622    case INDEX_op_ext32s_i64:
1623    case INDEX_op_ext32u_i64:
1624    case INDEX_op_extu_i32_i64:
1625    case INDEX_op_extrl_i64_i32:
1626    case INDEX_op_extrh_i64_i32:
1627    case INDEX_op_ext_i32_i64:
1628    case INDEX_op_not_i32:
1629    case INDEX_op_not_i64:
1630    case INDEX_op_extract_i32:
1631    case INDEX_op_extract_i64:
1632    case INDEX_op_bswap16_i32:
1633    case INDEX_op_bswap16_i64:
1634    case INDEX_op_bswap32_i32:
1635    case INDEX_op_bswap32_i64:
1636    case INDEX_op_bswap64_i64:
1637    case INDEX_op_ld8s_i32:
1638    case INDEX_op_ld8s_i64:
1639    case INDEX_op_ld8u_i32:
1640    case INDEX_op_ld8u_i64:
1641    case INDEX_op_ld16s_i32:
1642    case INDEX_op_ld16s_i64:
1643    case INDEX_op_ld16u_i32:
1644    case INDEX_op_ld16u_i64:
1645    case INDEX_op_ld32s_i64:
1646    case INDEX_op_ld32u_i64:
1647    case INDEX_op_ld_i32:
1648    case INDEX_op_ld_i64:
1649        return C_O1_I1(r, r);
1650
1651    case INDEX_op_qemu_ld_i32:
1652    case INDEX_op_qemu_ld_i64:
1653        return C_O1_I1(r, L);
1654
1655    case INDEX_op_andc_i32:
1656    case INDEX_op_andc_i64:
1657    case INDEX_op_orc_i32:
1658    case INDEX_op_orc_i64:
1659        /*
1660         * LoongArch insns for these ops don't have reg-imm forms, but we
1661         * can express using andi/ori if ~constant satisfies
1662         * TCG_CT_CONST_U12.
1663         */
1664        return C_O1_I2(r, r, rC);
1665
1666    case INDEX_op_shl_i32:
1667    case INDEX_op_shl_i64:
1668    case INDEX_op_shr_i32:
1669    case INDEX_op_shr_i64:
1670    case INDEX_op_sar_i32:
1671    case INDEX_op_sar_i64:
1672    case INDEX_op_rotl_i32:
1673    case INDEX_op_rotl_i64:
1674    case INDEX_op_rotr_i32:
1675    case INDEX_op_rotr_i64:
1676        return C_O1_I2(r, r, ri);
1677
1678    case INDEX_op_add_i32:
1679        return C_O1_I2(r, r, ri);
1680    case INDEX_op_add_i64:
1681        return C_O1_I2(r, r, rJ);
1682
1683    case INDEX_op_and_i32:
1684    case INDEX_op_and_i64:
1685    case INDEX_op_nor_i32:
1686    case INDEX_op_nor_i64:
1687    case INDEX_op_or_i32:
1688    case INDEX_op_or_i64:
1689    case INDEX_op_xor_i32:
1690    case INDEX_op_xor_i64:
1691        /* LoongArch reg-imm bitops have their imms ZERO-extended */
1692        return C_O1_I2(r, r, rU);
1693
1694    case INDEX_op_clz_i32:
1695    case INDEX_op_clz_i64:
1696    case INDEX_op_ctz_i32:
1697    case INDEX_op_ctz_i64:
1698        return C_O1_I2(r, r, rW);
1699
1700    case INDEX_op_deposit_i32:
1701    case INDEX_op_deposit_i64:
1702        /* Must deposit into the same register as input */
1703        return C_O1_I2(r, 0, rZ);
1704
1705    case INDEX_op_sub_i32:
1706    case INDEX_op_setcond_i32:
1707        return C_O1_I2(r, rZ, ri);
1708    case INDEX_op_sub_i64:
1709    case INDEX_op_setcond_i64:
1710        return C_O1_I2(r, rZ, rJ);
1711
1712    case INDEX_op_mul_i32:
1713    case INDEX_op_mul_i64:
1714    case INDEX_op_mulsh_i32:
1715    case INDEX_op_mulsh_i64:
1716    case INDEX_op_muluh_i32:
1717    case INDEX_op_muluh_i64:
1718    case INDEX_op_div_i32:
1719    case INDEX_op_div_i64:
1720    case INDEX_op_divu_i32:
1721    case INDEX_op_divu_i64:
1722    case INDEX_op_rem_i32:
1723    case INDEX_op_rem_i64:
1724    case INDEX_op_remu_i32:
1725    case INDEX_op_remu_i64:
1726        return C_O1_I2(r, rZ, rZ);
1727
1728    case INDEX_op_movcond_i32:
1729    case INDEX_op_movcond_i64:
1730        return C_O1_I4(r, rZ, rJ, rZ, rZ);
1731
1732    default:
1733        g_assert_not_reached();
1734    }
1735}
1736
1737static const int tcg_target_callee_save_regs[] = {
1738    TCG_REG_S0,     /* used for the global env (TCG_AREG0) */
1739    TCG_REG_S1,
1740    TCG_REG_S2,
1741    TCG_REG_S3,
1742    TCG_REG_S4,
1743    TCG_REG_S5,
1744    TCG_REG_S6,
1745    TCG_REG_S7,
1746    TCG_REG_S8,
1747    TCG_REG_S9,
1748    TCG_REG_RA,     /* should be last for ABI compliance */
1749};
1750
1751/* Stack frame parameters.  */
1752#define REG_SIZE   (TCG_TARGET_REG_BITS / 8)
1753#define SAVE_SIZE  ((int)ARRAY_SIZE(tcg_target_callee_save_regs) * REG_SIZE)
1754#define TEMP_SIZE  (CPU_TEMP_BUF_NLONGS * (int)sizeof(long))
1755#define FRAME_SIZE ((TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE + SAVE_SIZE \
1756                     + TCG_TARGET_STACK_ALIGN - 1) \
1757                    & -TCG_TARGET_STACK_ALIGN)
1758#define SAVE_OFS   (TCG_STATIC_CALL_ARGS_SIZE + TEMP_SIZE)
1759
1760/* We're expecting to be able to use an immediate for frame allocation.  */
1761QEMU_BUILD_BUG_ON(FRAME_SIZE > 0x7ff);
1762
1763/* Generate global QEMU prologue and epilogue code */
1764static void tcg_target_qemu_prologue(TCGContext *s)
1765{
1766    int i;
1767
1768    tcg_set_frame(s, TCG_REG_SP, TCG_STATIC_CALL_ARGS_SIZE, TEMP_SIZE);
1769
1770    /* TB prologue */
1771    tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, -FRAME_SIZE);
1772    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1773        tcg_out_st(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1774                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1775    }
1776
1777#if !defined(CONFIG_SOFTMMU)
1778    if (USE_GUEST_BASE) {
1779        tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
1780        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
1781    }
1782#endif
1783
1784    /* Call generated code */
1785    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
1786    tcg_out_opc_jirl(s, TCG_REG_ZERO, tcg_target_call_iarg_regs[1], 0);
1787
1788    /* Return path for goto_ptr. Set return value to 0 */
1789    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
1790    tcg_out_mov(s, TCG_TYPE_REG, TCG_REG_A0, TCG_REG_ZERO);
1791
1792    /* TB epilogue */
1793    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
1794    for (i = 0; i < ARRAY_SIZE(tcg_target_callee_save_regs); i++) {
1795        tcg_out_ld(s, TCG_TYPE_REG, tcg_target_callee_save_regs[i],
1796                   TCG_REG_SP, SAVE_OFS + i * REG_SIZE);
1797    }
1798
1799    tcg_out_opc_addi_d(s, TCG_REG_SP, TCG_REG_SP, FRAME_SIZE);
1800    tcg_out_opc_jirl(s, TCG_REG_ZERO, TCG_REG_RA, 0);
1801}
1802
1803static void tcg_target_init(TCGContext *s)
1804{
1805    tcg_target_available_regs[TCG_TYPE_I32] = ALL_GENERAL_REGS;
1806    tcg_target_available_regs[TCG_TYPE_I64] = ALL_GENERAL_REGS;
1807
1808    tcg_target_call_clobber_regs = ALL_GENERAL_REGS;
1809    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S0);
1810    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S1);
1811    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S2);
1812    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S3);
1813    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S4);
1814    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S5);
1815    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S6);
1816    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S7);
1817    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S8);
1818    tcg_regset_reset_reg(tcg_target_call_clobber_regs, TCG_REG_S9);
1819
1820    s->reserved_regs = 0;
1821    tcg_regset_set_reg(s->reserved_regs, TCG_REG_ZERO);
1822    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP0);
1823    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP1);
1824    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TMP2);
1825    tcg_regset_set_reg(s->reserved_regs, TCG_REG_SP);
1826    tcg_regset_set_reg(s->reserved_regs, TCG_REG_TP);
1827    tcg_regset_set_reg(s->reserved_regs, TCG_REG_RESERVED);
1828}
1829
1830typedef struct {
1831    DebugFrameHeader h;
1832    uint8_t fde_def_cfa[4];
1833    uint8_t fde_reg_ofs[ARRAY_SIZE(tcg_target_callee_save_regs) * 2];
1834} DebugFrame;
1835
1836#define ELF_HOST_MACHINE EM_LOONGARCH
1837
1838static const DebugFrame debug_frame = {
1839    .h.cie.len = sizeof(DebugFrameCIE) - 4, /* length after .len member */
1840    .h.cie.id = -1,
1841    .h.cie.version = 1,
1842    .h.cie.code_align = 1,
1843    .h.cie.data_align = -(TCG_TARGET_REG_BITS / 8) & 0x7f, /* sleb128 */
1844    .h.cie.return_column = TCG_REG_RA,
1845
1846    /* Total FDE size does not include the "len" member.  */
1847    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
1848
1849    .fde_def_cfa = {
1850        12, TCG_REG_SP,                 /* DW_CFA_def_cfa sp, ...  */
1851        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
1852        (FRAME_SIZE >> 7)
1853    },
1854    .fde_reg_ofs = {
1855        0x80 + 23, 11,                  /* DW_CFA_offset, s0, -88 */
1856        0x80 + 24, 10,                  /* DW_CFA_offset, s1, -80 */
1857        0x80 + 25, 9,                   /* DW_CFA_offset, s2, -72 */
1858        0x80 + 26, 8,                   /* DW_CFA_offset, s3, -64 */
1859        0x80 + 27, 7,                   /* DW_CFA_offset, s4, -56 */
1860        0x80 + 28, 6,                   /* DW_CFA_offset, s5, -48 */
1861        0x80 + 29, 5,                   /* DW_CFA_offset, s6, -40 */
1862        0x80 + 30, 4,                   /* DW_CFA_offset, s7, -32 */
1863        0x80 + 31, 3,                   /* DW_CFA_offset, s8, -24 */
1864        0x80 + 22, 2,                   /* DW_CFA_offset, s9, -16 */
1865        0x80 + 1 , 1,                   /* DW_CFA_offset, ra, -8 */
1866    }
1867};
1868
1869void tcg_register_jit(const void *buf, size_t buf_size)
1870{
1871    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1872}
1873