xref: /openbmc/qemu/tcg/s390x/tcg-target.c.inc (revision 136cb9cc)
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2009 Ulrich Hecht <uli@suse.de>
5 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
6 * Copyright (c) 2010 Richard Henderson <rth@twiddle.net>
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#include "../tcg-pool.c.inc"
29#include "elf.h"
30
31#define TCG_CT_CONST_S16        (1 << 8)
32#define TCG_CT_CONST_S32        (1 << 9)
33#define TCG_CT_CONST_S33        (1 << 10)
34#define TCG_CT_CONST_ZERO       (1 << 11)
35#define TCG_CT_CONST_P32        (1 << 12)
36#define TCG_CT_CONST_INV        (1 << 13)
37#define TCG_CT_CONST_INVRISBG   (1 << 14)
38
39#define ALL_GENERAL_REGS     MAKE_64BIT_MASK(0, 16)
40#define ALL_VECTOR_REGS      MAKE_64BIT_MASK(32, 32)
41
42/* Several places within the instruction set 0 means "no register"
43   rather than TCG_REG_R0.  */
44#define TCG_REG_NONE    0
45
46/* A scratch register that may be be used throughout the backend.  */
47#define TCG_TMP0        TCG_REG_R1
48
49#ifndef CONFIG_SOFTMMU
50#define TCG_GUEST_BASE_REG TCG_REG_R13
51#endif
52
53/* All of the following instructions are prefixed with their instruction
54   format, and are defined as 8- or 16-bit quantities, even when the two
55   halves of the 16-bit quantity may appear 32 bits apart in the insn.
56   This makes it easy to copy the values from the tables in Appendix B.  */
57typedef enum S390Opcode {
58    RIL_AFI     = 0xc209,
59    RIL_AGFI    = 0xc208,
60    RIL_ALFI    = 0xc20b,
61    RIL_ALGFI   = 0xc20a,
62    RIL_BRASL   = 0xc005,
63    RIL_BRCL    = 0xc004,
64    RIL_CFI     = 0xc20d,
65    RIL_CGFI    = 0xc20c,
66    RIL_CLFI    = 0xc20f,
67    RIL_CLGFI   = 0xc20e,
68    RIL_CLRL    = 0xc60f,
69    RIL_CLGRL   = 0xc60a,
70    RIL_CRL     = 0xc60d,
71    RIL_CGRL    = 0xc608,
72    RIL_IIHF    = 0xc008,
73    RIL_IILF    = 0xc009,
74    RIL_LARL    = 0xc000,
75    RIL_LGFI    = 0xc001,
76    RIL_LGRL    = 0xc408,
77    RIL_LLIHF   = 0xc00e,
78    RIL_LLILF   = 0xc00f,
79    RIL_LRL     = 0xc40d,
80    RIL_MSFI    = 0xc201,
81    RIL_MSGFI   = 0xc200,
82    RIL_NIHF    = 0xc00a,
83    RIL_NILF    = 0xc00b,
84    RIL_OIHF    = 0xc00c,
85    RIL_OILF    = 0xc00d,
86    RIL_SLFI    = 0xc205,
87    RIL_SLGFI   = 0xc204,
88    RIL_XIHF    = 0xc006,
89    RIL_XILF    = 0xc007,
90
91    RI_AGHI     = 0xa70b,
92    RI_AHI      = 0xa70a,
93    RI_BRC      = 0xa704,
94    RI_CHI      = 0xa70e,
95    RI_CGHI     = 0xa70f,
96    RI_IIHH     = 0xa500,
97    RI_IIHL     = 0xa501,
98    RI_IILH     = 0xa502,
99    RI_IILL     = 0xa503,
100    RI_LGHI     = 0xa709,
101    RI_LLIHH    = 0xa50c,
102    RI_LLIHL    = 0xa50d,
103    RI_LLILH    = 0xa50e,
104    RI_LLILL    = 0xa50f,
105    RI_MGHI     = 0xa70d,
106    RI_MHI      = 0xa70c,
107    RI_NIHH     = 0xa504,
108    RI_NIHL     = 0xa505,
109    RI_NILH     = 0xa506,
110    RI_NILL     = 0xa507,
111    RI_OIHH     = 0xa508,
112    RI_OIHL     = 0xa509,
113    RI_OILH     = 0xa50a,
114    RI_OILL     = 0xa50b,
115    RI_TMLL     = 0xa701,
116
117    RIEb_CGRJ    = 0xec64,
118    RIEb_CLGRJ   = 0xec65,
119    RIEb_CLRJ    = 0xec77,
120    RIEb_CRJ     = 0xec76,
121
122    RIEc_CGIJ    = 0xec7c,
123    RIEc_CIJ     = 0xec7e,
124    RIEc_CLGIJ   = 0xec7d,
125    RIEc_CLIJ    = 0xec7f,
126
127    RIEf_RISBG   = 0xec55,
128
129    RIEg_LOCGHI  = 0xec46,
130
131    RRE_AGR     = 0xb908,
132    RRE_ALGR    = 0xb90a,
133    RRE_ALCR    = 0xb998,
134    RRE_ALCGR   = 0xb988,
135    RRE_ALGFR   = 0xb91a,
136    RRE_CGR     = 0xb920,
137    RRE_CLGR    = 0xb921,
138    RRE_DLGR    = 0xb987,
139    RRE_DLR     = 0xb997,
140    RRE_DSGFR   = 0xb91d,
141    RRE_DSGR    = 0xb90d,
142    RRE_FLOGR   = 0xb983,
143    RRE_LGBR    = 0xb906,
144    RRE_LCGR    = 0xb903,
145    RRE_LGFR    = 0xb914,
146    RRE_LGHR    = 0xb907,
147    RRE_LGR     = 0xb904,
148    RRE_LLGCR   = 0xb984,
149    RRE_LLGFR   = 0xb916,
150    RRE_LLGHR   = 0xb985,
151    RRE_LRVR    = 0xb91f,
152    RRE_LRVGR   = 0xb90f,
153    RRE_LTGR    = 0xb902,
154    RRE_MLGR    = 0xb986,
155    RRE_MSGR    = 0xb90c,
156    RRE_MSR     = 0xb252,
157    RRE_NGR     = 0xb980,
158    RRE_OGR     = 0xb981,
159    RRE_SGR     = 0xb909,
160    RRE_SLGR    = 0xb90b,
161    RRE_SLBR    = 0xb999,
162    RRE_SLBGR   = 0xb989,
163    RRE_XGR     = 0xb982,
164
165    RRFa_MGRK   = 0xb9ec,
166    RRFa_MSRKC  = 0xb9fd,
167    RRFa_MSGRKC = 0xb9ed,
168    RRFa_NCRK   = 0xb9f5,
169    RRFa_NCGRK  = 0xb9e5,
170    RRFa_NNRK   = 0xb974,
171    RRFa_NNGRK  = 0xb964,
172    RRFa_NORK   = 0xb976,
173    RRFa_NOGRK  = 0xb966,
174    RRFa_NRK    = 0xb9f4,
175    RRFa_NGRK   = 0xb9e4,
176    RRFa_NXRK   = 0xb977,
177    RRFa_NXGRK  = 0xb967,
178    RRFa_OCRK   = 0xb975,
179    RRFa_OCGRK  = 0xb965,
180    RRFa_ORK    = 0xb9f6,
181    RRFa_OGRK   = 0xb9e6,
182    RRFa_SRK    = 0xb9f9,
183    RRFa_SGRK   = 0xb9e9,
184    RRFa_SLRK   = 0xb9fb,
185    RRFa_SLGRK  = 0xb9eb,
186    RRFa_XRK    = 0xb9f7,
187    RRFa_XGRK   = 0xb9e7,
188
189    RRFam_SELGR = 0xb9e3,
190
191    RRFc_LOCR   = 0xb9f2,
192    RRFc_LOCGR  = 0xb9e2,
193    RRFc_POPCNT = 0xb9e1,
194
195    RR_AR       = 0x1a,
196    RR_ALR      = 0x1e,
197    RR_BASR     = 0x0d,
198    RR_BCR      = 0x07,
199    RR_CLR      = 0x15,
200    RR_CR       = 0x19,
201    RR_DR       = 0x1d,
202    RR_LCR      = 0x13,
203    RR_LR       = 0x18,
204    RR_LTR      = 0x12,
205    RR_NR       = 0x14,
206    RR_OR       = 0x16,
207    RR_SR       = 0x1b,
208    RR_SLR      = 0x1f,
209    RR_XR       = 0x17,
210
211    RSY_RLL     = 0xeb1d,
212    RSY_RLLG    = 0xeb1c,
213    RSY_SLLG    = 0xeb0d,
214    RSY_SLLK    = 0xebdf,
215    RSY_SRAG    = 0xeb0a,
216    RSY_SRAK    = 0xebdc,
217    RSY_SRLG    = 0xeb0c,
218    RSY_SRLK    = 0xebde,
219
220    RS_SLL      = 0x89,
221    RS_SRA      = 0x8a,
222    RS_SRL      = 0x88,
223
224    RXY_AG      = 0xe308,
225    RXY_AY      = 0xe35a,
226    RXY_CG      = 0xe320,
227    RXY_CLG     = 0xe321,
228    RXY_CLY     = 0xe355,
229    RXY_CY      = 0xe359,
230    RXY_LAY     = 0xe371,
231    RXY_LB      = 0xe376,
232    RXY_LG      = 0xe304,
233    RXY_LGB     = 0xe377,
234    RXY_LGF     = 0xe314,
235    RXY_LGH     = 0xe315,
236    RXY_LHY     = 0xe378,
237    RXY_LLGC    = 0xe390,
238    RXY_LLGF    = 0xe316,
239    RXY_LLGH    = 0xe391,
240    RXY_LMG     = 0xeb04,
241    RXY_LPQ     = 0xe38f,
242    RXY_LRV     = 0xe31e,
243    RXY_LRVG    = 0xe30f,
244    RXY_LRVH    = 0xe31f,
245    RXY_LY      = 0xe358,
246    RXY_NG      = 0xe380,
247    RXY_OG      = 0xe381,
248    RXY_STCY    = 0xe372,
249    RXY_STG     = 0xe324,
250    RXY_STHY    = 0xe370,
251    RXY_STMG    = 0xeb24,
252    RXY_STPQ    = 0xe38e,
253    RXY_STRV    = 0xe33e,
254    RXY_STRVG   = 0xe32f,
255    RXY_STRVH   = 0xe33f,
256    RXY_STY     = 0xe350,
257    RXY_XG      = 0xe382,
258
259    RX_A        = 0x5a,
260    RX_C        = 0x59,
261    RX_L        = 0x58,
262    RX_LA       = 0x41,
263    RX_LH       = 0x48,
264    RX_ST       = 0x50,
265    RX_STC      = 0x42,
266    RX_STH      = 0x40,
267
268    VRIa_VGBM   = 0xe744,
269    VRIa_VREPI  = 0xe745,
270    VRIb_VGM    = 0xe746,
271    VRIc_VREP   = 0xe74d,
272
273    VRRa_VLC    = 0xe7de,
274    VRRa_VLP    = 0xe7df,
275    VRRa_VLR    = 0xe756,
276    VRRc_VA     = 0xe7f3,
277    VRRc_VCEQ   = 0xe7f8,   /* we leave the m5 cs field 0 */
278    VRRc_VCH    = 0xe7fb,   /* " */
279    VRRc_VCHL   = 0xe7f9,   /* " */
280    VRRc_VERLLV = 0xe773,
281    VRRc_VESLV  = 0xe770,
282    VRRc_VESRAV = 0xe77a,
283    VRRc_VESRLV = 0xe778,
284    VRRc_VML    = 0xe7a2,
285    VRRc_VMN    = 0xe7fe,
286    VRRc_VMNL   = 0xe7fc,
287    VRRc_VMX    = 0xe7ff,
288    VRRc_VMXL   = 0xe7fd,
289    VRRc_VN     = 0xe768,
290    VRRc_VNC    = 0xe769,
291    VRRc_VNN    = 0xe76e,
292    VRRc_VNO    = 0xe76b,
293    VRRc_VNX    = 0xe76c,
294    VRRc_VO     = 0xe76a,
295    VRRc_VOC    = 0xe76f,
296    VRRc_VPKS   = 0xe797,   /* we leave the m5 cs field 0 */
297    VRRc_VS     = 0xe7f7,
298    VRRa_VUPH   = 0xe7d7,
299    VRRa_VUPL   = 0xe7d6,
300    VRRc_VX     = 0xe76d,
301    VRRe_VSEL   = 0xe78d,
302    VRRf_VLVGP  = 0xe762,
303
304    VRSa_VERLL  = 0xe733,
305    VRSa_VESL   = 0xe730,
306    VRSa_VESRA  = 0xe73a,
307    VRSa_VESRL  = 0xe738,
308    VRSb_VLVG   = 0xe722,
309    VRSc_VLGV   = 0xe721,
310
311    VRX_VL      = 0xe706,
312    VRX_VLLEZ   = 0xe704,
313    VRX_VLREP   = 0xe705,
314    VRX_VST     = 0xe70e,
315    VRX_VSTEF   = 0xe70b,
316    VRX_VSTEG   = 0xe70a,
317
318    NOP         = 0x0707,
319} S390Opcode;
320
321#ifdef CONFIG_DEBUG_TCG
322static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
323    "%r0",  "%r1",  "%r2",  "%r3",  "%r4",  "%r5",  "%r6",  "%r7",
324    "%r8",  "%r9",  "%r10", "%r11", "%r12", "%r13", "%r14", "%r15",
325    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
326    "%v0",  "%v1",  "%v2",  "%v3",  "%v4",  "%v5",  "%v6",  "%v7",
327    "%v8",  "%v9",  "%v10", "%v11", "%v12", "%v13", "%v14", "%v15",
328    "%v16", "%v17", "%v18", "%v19", "%v20", "%v21", "%v22", "%v23",
329    "%v24", "%v25", "%v26", "%v27", "%v28", "%v29", "%v30", "%v31",
330};
331#endif
332
333/* Since R6 is a potential argument register, choose it last of the
334   call-saved registers.  Likewise prefer the call-clobbered registers
335   in reverse order to maximize the chance of avoiding the arguments.  */
336static const int tcg_target_reg_alloc_order[] = {
337    /* Call saved registers.  */
338    TCG_REG_R13,
339    TCG_REG_R12,
340    TCG_REG_R11,
341    TCG_REG_R10,
342    TCG_REG_R9,
343    TCG_REG_R8,
344    TCG_REG_R7,
345    TCG_REG_R6,
346    /* Call clobbered registers.  */
347    TCG_REG_R14,
348    TCG_REG_R0,
349    TCG_REG_R1,
350    /* Argument registers, in reverse order of allocation.  */
351    TCG_REG_R5,
352    TCG_REG_R4,
353    TCG_REG_R3,
354    TCG_REG_R2,
355
356    /* V8-V15 are call saved, and omitted. */
357    TCG_REG_V0,
358    TCG_REG_V1,
359    TCG_REG_V2,
360    TCG_REG_V3,
361    TCG_REG_V4,
362    TCG_REG_V5,
363    TCG_REG_V6,
364    TCG_REG_V7,
365    TCG_REG_V16,
366    TCG_REG_V17,
367    TCG_REG_V18,
368    TCG_REG_V19,
369    TCG_REG_V20,
370    TCG_REG_V21,
371    TCG_REG_V22,
372    TCG_REG_V23,
373    TCG_REG_V24,
374    TCG_REG_V25,
375    TCG_REG_V26,
376    TCG_REG_V27,
377    TCG_REG_V28,
378    TCG_REG_V29,
379    TCG_REG_V30,
380    TCG_REG_V31,
381};
382
383static const int tcg_target_call_iarg_regs[] = {
384    TCG_REG_R2,
385    TCG_REG_R3,
386    TCG_REG_R4,
387    TCG_REG_R5,
388    TCG_REG_R6,
389};
390
391static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot)
392{
393    tcg_debug_assert(kind == TCG_CALL_RET_NORMAL);
394    tcg_debug_assert(slot == 0);
395    return TCG_REG_R2;
396}
397
398#define S390_CC_EQ      8
399#define S390_CC_LT      4
400#define S390_CC_GT      2
401#define S390_CC_OV      1
402#define S390_CC_NE      (S390_CC_LT | S390_CC_GT)
403#define S390_CC_LE      (S390_CC_LT | S390_CC_EQ)
404#define S390_CC_GE      (S390_CC_GT | S390_CC_EQ)
405#define S390_CC_NEVER   0
406#define S390_CC_ALWAYS  15
407
408/* Condition codes that result from a COMPARE and COMPARE LOGICAL.  */
409static const uint8_t tcg_cond_to_s390_cond[] = {
410    [TCG_COND_EQ]  = S390_CC_EQ,
411    [TCG_COND_NE]  = S390_CC_NE,
412    [TCG_COND_LT]  = S390_CC_LT,
413    [TCG_COND_LE]  = S390_CC_LE,
414    [TCG_COND_GT]  = S390_CC_GT,
415    [TCG_COND_GE]  = S390_CC_GE,
416    [TCG_COND_LTU] = S390_CC_LT,
417    [TCG_COND_LEU] = S390_CC_LE,
418    [TCG_COND_GTU] = S390_CC_GT,
419    [TCG_COND_GEU] = S390_CC_GE,
420};
421
422/* Condition codes that result from a LOAD AND TEST.  Here, we have no
423   unsigned instruction variation, however since the test is vs zero we
424   can re-map the outcomes appropriately.  */
425static const uint8_t tcg_cond_to_ltr_cond[] = {
426    [TCG_COND_EQ]  = S390_CC_EQ,
427    [TCG_COND_NE]  = S390_CC_NE,
428    [TCG_COND_LT]  = S390_CC_LT,
429    [TCG_COND_LE]  = S390_CC_LE,
430    [TCG_COND_GT]  = S390_CC_GT,
431    [TCG_COND_GE]  = S390_CC_GE,
432    [TCG_COND_LTU] = S390_CC_NEVER,
433    [TCG_COND_LEU] = S390_CC_EQ,
434    [TCG_COND_GTU] = S390_CC_NE,
435    [TCG_COND_GEU] = S390_CC_ALWAYS,
436};
437
438static const tcg_insn_unit *tb_ret_addr;
439uint64_t s390_facilities[3];
440
441static inline bool is_general_reg(TCGReg r)
442{
443    return r <= TCG_REG_R15;
444}
445
446static inline bool is_vector_reg(TCGReg r)
447{
448    return r >= TCG_REG_V0 && r <= TCG_REG_V31;
449}
450
451static bool patch_reloc(tcg_insn_unit *src_rw, int type,
452                        intptr_t value, intptr_t addend)
453{
454    const tcg_insn_unit *src_rx = tcg_splitwx_to_rx(src_rw);
455    intptr_t pcrel2;
456    uint32_t old;
457
458    value += addend;
459    pcrel2 = (tcg_insn_unit *)value - src_rx;
460
461    switch (type) {
462    case R_390_PC16DBL:
463        if (pcrel2 == (int16_t)pcrel2) {
464            tcg_patch16(src_rw, pcrel2);
465            return true;
466        }
467        break;
468    case R_390_PC32DBL:
469        if (pcrel2 == (int32_t)pcrel2) {
470            tcg_patch32(src_rw, pcrel2);
471            return true;
472        }
473        break;
474    case R_390_20:
475        if (value == sextract64(value, 0, 20)) {
476            old = *(uint32_t *)src_rw & 0xf00000ff;
477            old |= ((value & 0xfff) << 16) | ((value & 0xff000) >> 4);
478            tcg_patch32(src_rw, old);
479            return true;
480        }
481        break;
482    default:
483        g_assert_not_reached();
484    }
485    return false;
486}
487
488static int is_const_p16(uint64_t val)
489{
490    for (int i = 0; i < 4; ++i) {
491        uint64_t mask = 0xffffull << (i * 16);
492        if ((val & ~mask) == 0) {
493            return i;
494        }
495    }
496    return -1;
497}
498
499static int is_const_p32(uint64_t val)
500{
501    if ((val & 0xffffffff00000000ull) == 0) {
502        return 0;
503    }
504    if ((val & 0x00000000ffffffffull) == 0) {
505        return 1;
506    }
507    return -1;
508}
509
510/*
511 * Accept bit patterns like these:
512 *  0....01....1
513 *  1....10....0
514 *  1..10..01..1
515 *  0..01..10..0
516 * Copied from gcc sources.
517 */
518static bool risbg_mask(uint64_t c)
519{
520    uint64_t lsb;
521    /* We don't change the number of transitions by inverting,
522       so make sure we start with the LSB zero.  */
523    if (c & 1) {
524        c = ~c;
525    }
526    /* Reject all zeros or all ones.  */
527    if (c == 0) {
528        return false;
529    }
530    /* Find the first transition.  */
531    lsb = c & -c;
532    /* Invert to look for a second transition.  */
533    c = ~c;
534    /* Erase the first transition.  */
535    c &= -lsb;
536    /* Find the second transition, if any.  */
537    lsb = c & -c;
538    /* Match if all the bits are 1's, or if c is zero.  */
539    return c == -lsb;
540}
541
542/* Test if a constant matches the constraint. */
543static bool tcg_target_const_match(int64_t val, TCGType type, int ct)
544{
545    if (ct & TCG_CT_CONST) {
546        return 1;
547    }
548
549    if (type == TCG_TYPE_I32) {
550        val = (int32_t)val;
551    }
552
553    /* The following are mutually exclusive.  */
554    if (ct & TCG_CT_CONST_S16) {
555        return val == (int16_t)val;
556    } else if (ct & TCG_CT_CONST_S32) {
557        return val == (int32_t)val;
558    } else if (ct & TCG_CT_CONST_S33) {
559        return val >= -0xffffffffll && val <= 0xffffffffll;
560    } else if (ct & TCG_CT_CONST_ZERO) {
561        return val == 0;
562    }
563
564    if (ct & TCG_CT_CONST_INV) {
565        val = ~val;
566    }
567    /*
568     * Note that is_const_p16 is a subset of is_const_p32,
569     * so we don't need both constraints.
570     */
571    if ((ct & TCG_CT_CONST_P32) && is_const_p32(val) >= 0) {
572        return true;
573    }
574    if ((ct & TCG_CT_CONST_INVRISBG) && risbg_mask(~val)) {
575        return true;
576    }
577
578    return 0;
579}
580
581/* Emit instructions according to the given instruction format.  */
582
583static void tcg_out_insn_RR(TCGContext *s, S390Opcode op, TCGReg r1, TCGReg r2)
584{
585    tcg_out16(s, (op << 8) | (r1 << 4) | r2);
586}
587
588static void tcg_out_insn_RRE(TCGContext *s, S390Opcode op,
589                             TCGReg r1, TCGReg r2)
590{
591    tcg_out32(s, (op << 16) | (r1 << 4) | r2);
592}
593
594/* RRF-a without the m4 field */
595static void tcg_out_insn_RRFa(TCGContext *s, S390Opcode op,
596                              TCGReg r1, TCGReg r2, TCGReg r3)
597{
598    tcg_out32(s, (op << 16) | (r3 << 12) | (r1 << 4) | r2);
599}
600
601/* RRF-a with the m4 field */
602static void tcg_out_insn_RRFam(TCGContext *s, S390Opcode op,
603                               TCGReg r1, TCGReg r2, TCGReg r3, int m4)
604{
605    tcg_out32(s, (op << 16) | (r3 << 12) | (m4 << 8) | (r1 << 4) | r2);
606}
607
608static void tcg_out_insn_RRFc(TCGContext *s, S390Opcode op,
609                              TCGReg r1, TCGReg r2, int m3)
610{
611    tcg_out32(s, (op << 16) | (m3 << 12) | (r1 << 4) | r2);
612}
613
614static void tcg_out_insn_RI(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
615{
616    tcg_out32(s, (op << 16) | (r1 << 20) | (i2 & 0xffff));
617}
618
619static void tcg_out_insn_RIEg(TCGContext *s, S390Opcode op, TCGReg r1,
620                             int i2, int m3)
621{
622    tcg_out16(s, (op & 0xff00) | (r1 << 4) | m3);
623    tcg_out32(s, (i2 << 16) | (op & 0xff));
624}
625
626static void tcg_out_insn_RIL(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
627{
628    tcg_out16(s, op | (r1 << 4));
629    tcg_out32(s, i2);
630}
631
632static void tcg_out_insn_RS(TCGContext *s, S390Opcode op, TCGReg r1,
633                            TCGReg b2, TCGReg r3, int disp)
634{
635    tcg_out32(s, (op << 24) | (r1 << 20) | (r3 << 16) | (b2 << 12)
636              | (disp & 0xfff));
637}
638
639static void tcg_out_insn_RSY(TCGContext *s, S390Opcode op, TCGReg r1,
640                             TCGReg b2, TCGReg r3, int disp)
641{
642    tcg_out16(s, (op & 0xff00) | (r1 << 4) | r3);
643    tcg_out32(s, (op & 0xff) | (b2 << 28)
644              | ((disp & 0xfff) << 16) | ((disp & 0xff000) >> 4));
645}
646
647#define tcg_out_insn_RX   tcg_out_insn_RS
648#define tcg_out_insn_RXY  tcg_out_insn_RSY
649
650static int RXB(TCGReg v1, TCGReg v2, TCGReg v3, TCGReg v4)
651{
652    /*
653     * Shift bit 4 of each regno to its corresponding bit of RXB.
654     * RXB itself begins at bit 8 of the instruction so 8 - 4 = 4
655     * is the left-shift of the 4th operand.
656     */
657    return ((v1 & 0x10) << (4 + 3))
658         | ((v2 & 0x10) << (4 + 2))
659         | ((v3 & 0x10) << (4 + 1))
660         | ((v4 & 0x10) << (4 + 0));
661}
662
663static void tcg_out_insn_VRIa(TCGContext *s, S390Opcode op,
664                              TCGReg v1, uint16_t i2, int m3)
665{
666    tcg_debug_assert(is_vector_reg(v1));
667    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4));
668    tcg_out16(s, i2);
669    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m3 << 12));
670}
671
672static void tcg_out_insn_VRIb(TCGContext *s, S390Opcode op,
673                              TCGReg v1, uint8_t i2, uint8_t i3, int m4)
674{
675    tcg_debug_assert(is_vector_reg(v1));
676    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4));
677    tcg_out16(s, (i2 << 8) | (i3 & 0xff));
678    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m4 << 12));
679}
680
681static void tcg_out_insn_VRIc(TCGContext *s, S390Opcode op,
682                              TCGReg v1, uint16_t i2, TCGReg v3, int m4)
683{
684    tcg_debug_assert(is_vector_reg(v1));
685    tcg_debug_assert(is_vector_reg(v3));
686    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v3 & 0xf));
687    tcg_out16(s, i2);
688    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, v3, 0) | (m4 << 12));
689}
690
691static void tcg_out_insn_VRRa(TCGContext *s, S390Opcode op,
692                              TCGReg v1, TCGReg v2, int m3)
693{
694    tcg_debug_assert(is_vector_reg(v1));
695    tcg_debug_assert(is_vector_reg(v2));
696    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf));
697    tcg_out32(s, (op & 0x00ff) | RXB(v1, v2, 0, 0) | (m3 << 12));
698}
699
700static void tcg_out_insn_VRRc(TCGContext *s, S390Opcode op,
701                              TCGReg v1, TCGReg v2, TCGReg v3, int m4)
702{
703    tcg_debug_assert(is_vector_reg(v1));
704    tcg_debug_assert(is_vector_reg(v2));
705    tcg_debug_assert(is_vector_reg(v3));
706    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf));
707    tcg_out16(s, v3 << 12);
708    tcg_out16(s, (op & 0x00ff) | RXB(v1, v2, v3, 0) | (m4 << 12));
709}
710
711static void tcg_out_insn_VRRe(TCGContext *s, S390Opcode op,
712                              TCGReg v1, TCGReg v2, TCGReg v3, TCGReg v4)
713{
714    tcg_debug_assert(is_vector_reg(v1));
715    tcg_debug_assert(is_vector_reg(v2));
716    tcg_debug_assert(is_vector_reg(v3));
717    tcg_debug_assert(is_vector_reg(v4));
718    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v2 & 0xf));
719    tcg_out16(s, v3 << 12);
720    tcg_out16(s, (op & 0x00ff) | RXB(v1, v2, v3, v4) | (v4 << 12));
721}
722
723static void tcg_out_insn_VRRf(TCGContext *s, S390Opcode op,
724                              TCGReg v1, TCGReg r2, TCGReg r3)
725{
726    tcg_debug_assert(is_vector_reg(v1));
727    tcg_debug_assert(is_general_reg(r2));
728    tcg_debug_assert(is_general_reg(r3));
729    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | r2);
730    tcg_out16(s, r3 << 12);
731    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0));
732}
733
734static void tcg_out_insn_VRSa(TCGContext *s, S390Opcode op, TCGReg v1,
735                              intptr_t d2, TCGReg b2, TCGReg v3, int m4)
736{
737    tcg_debug_assert(is_vector_reg(v1));
738    tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
739    tcg_debug_assert(is_general_reg(b2));
740    tcg_debug_assert(is_vector_reg(v3));
741    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | (v3 & 0xf));
742    tcg_out16(s, b2 << 12 | d2);
743    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, v3, 0) | (m4 << 12));
744}
745
746static void tcg_out_insn_VRSb(TCGContext *s, S390Opcode op, TCGReg v1,
747                              intptr_t d2, TCGReg b2, TCGReg r3, int m4)
748{
749    tcg_debug_assert(is_vector_reg(v1));
750    tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
751    tcg_debug_assert(is_general_reg(b2));
752    tcg_debug_assert(is_general_reg(r3));
753    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | r3);
754    tcg_out16(s, b2 << 12 | d2);
755    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m4 << 12));
756}
757
758static void tcg_out_insn_VRSc(TCGContext *s, S390Opcode op, TCGReg r1,
759                              intptr_t d2, TCGReg b2, TCGReg v3, int m4)
760{
761    tcg_debug_assert(is_general_reg(r1));
762    tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
763    tcg_debug_assert(is_general_reg(b2));
764    tcg_debug_assert(is_vector_reg(v3));
765    tcg_out16(s, (op & 0xff00) | (r1 << 4) | (v3 & 0xf));
766    tcg_out16(s, b2 << 12 | d2);
767    tcg_out16(s, (op & 0x00ff) | RXB(0, 0, v3, 0) | (m4 << 12));
768}
769
770static void tcg_out_insn_VRX(TCGContext *s, S390Opcode op, TCGReg v1,
771                             TCGReg b2, TCGReg x2, intptr_t d2, int m3)
772{
773    tcg_debug_assert(is_vector_reg(v1));
774    tcg_debug_assert(d2 >= 0 && d2 <= 0xfff);
775    tcg_debug_assert(is_general_reg(x2));
776    tcg_debug_assert(is_general_reg(b2));
777    tcg_out16(s, (op & 0xff00) | ((v1 & 0xf) << 4) | x2);
778    tcg_out16(s, (b2 << 12) | d2);
779    tcg_out16(s, (op & 0x00ff) | RXB(v1, 0, 0, 0) | (m3 << 12));
780}
781
782/* Emit an opcode with "type-checking" of the format.  */
783#define tcg_out_insn(S, FMT, OP, ...) \
784    glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__)
785
786
787/* emit 64-bit shifts */
788static void tcg_out_sh64(TCGContext* s, S390Opcode op, TCGReg dest,
789                         TCGReg src, TCGReg sh_reg, int sh_imm)
790{
791    tcg_out_insn_RSY(s, op, dest, sh_reg, src, sh_imm);
792}
793
794/* emit 32-bit shifts */
795static void tcg_out_sh32(TCGContext* s, S390Opcode op, TCGReg dest,
796                         TCGReg sh_reg, int sh_imm)
797{
798    tcg_out_insn_RS(s, op, dest, sh_reg, 0, sh_imm);
799}
800
801static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg dst, TCGReg src)
802{
803    if (src == dst) {
804        return true;
805    }
806    switch (type) {
807    case TCG_TYPE_I32:
808        if (likely(is_general_reg(dst) && is_general_reg(src))) {
809            tcg_out_insn(s, RR, LR, dst, src);
810            break;
811        }
812        /* fallthru */
813
814    case TCG_TYPE_I64:
815        if (likely(is_general_reg(dst))) {
816            if (likely(is_general_reg(src))) {
817                tcg_out_insn(s, RRE, LGR, dst, src);
818            } else {
819                tcg_out_insn(s, VRSc, VLGV, dst, 0, 0, src, 3);
820            }
821            break;
822        } else if (is_general_reg(src)) {
823            tcg_out_insn(s, VRSb, VLVG, dst, 0, 0, src, 3);
824            break;
825        }
826        /* fallthru */
827
828    case TCG_TYPE_V64:
829    case TCG_TYPE_V128:
830        tcg_out_insn(s, VRRa, VLR, dst, src, 0);
831        break;
832
833    default:
834        g_assert_not_reached();
835    }
836    return true;
837}
838
839static const S390Opcode li_insns[4] = {
840    RI_LLILL, RI_LLILH, RI_LLIHL, RI_LLIHH
841};
842static const S390Opcode oi_insns[4] = {
843    RI_OILL, RI_OILH, RI_OIHL, RI_OIHH
844};
845static const S390Opcode lif_insns[2] = {
846    RIL_LLILF, RIL_LLIHF,
847};
848
849/* load a register with an immediate value */
850static void tcg_out_movi(TCGContext *s, TCGType type,
851                         TCGReg ret, tcg_target_long sval)
852{
853    tcg_target_ulong uval = sval;
854    ptrdiff_t pc_off;
855    int i;
856
857    if (type == TCG_TYPE_I32) {
858        uval = (uint32_t)sval;
859        sval = (int32_t)sval;
860    }
861
862    /* Try all 32-bit insns that can load it in one go.  */
863    if (sval >= -0x8000 && sval < 0x8000) {
864        tcg_out_insn(s, RI, LGHI, ret, sval);
865        return;
866    }
867
868    i = is_const_p16(uval);
869    if (i >= 0) {
870        tcg_out_insn_RI(s, li_insns[i], ret, uval >> (i * 16));
871        return;
872    }
873
874    /* Try all 48-bit insns that can load it in one go.  */
875    if (sval == (int32_t)sval) {
876        tcg_out_insn(s, RIL, LGFI, ret, sval);
877        return;
878    }
879
880    i = is_const_p32(uval);
881    if (i >= 0) {
882        tcg_out_insn_RIL(s, lif_insns[i], ret, uval >> (i * 32));
883        return;
884    }
885
886    /* Try for PC-relative address load.  For odd addresses, add one. */
887    pc_off = tcg_pcrel_diff(s, (void *)sval) >> 1;
888    if (pc_off == (int32_t)pc_off) {
889        tcg_out_insn(s, RIL, LARL, ret, pc_off);
890        if (sval & 1) {
891            tcg_out_insn(s, RI, AGHI, ret, 1);
892        }
893        return;
894    }
895
896    /* Otherwise, load it by parts. */
897    i = is_const_p16((uint32_t)uval);
898    if (i >= 0) {
899        tcg_out_insn_RI(s, li_insns[i], ret, uval >> (i * 16));
900    } else {
901        tcg_out_insn(s, RIL, LLILF, ret, uval);
902    }
903    uval >>= 32;
904    i = is_const_p16(uval);
905    if (i >= 0) {
906        tcg_out_insn_RI(s, oi_insns[i + 2], ret, uval >> (i * 16));
907    } else {
908        tcg_out_insn(s, RIL, OIHF, ret, uval);
909    }
910}
911
912/* Emit a load/store type instruction.  Inputs are:
913   DATA:     The register to be loaded or stored.
914   BASE+OFS: The effective address.
915   OPC_RX:   If the operation has an RX format opcode (e.g. STC), otherwise 0.
916   OPC_RXY:  The RXY format opcode for the operation (e.g. STCY).  */
917
918static void tcg_out_mem(TCGContext *s, S390Opcode opc_rx, S390Opcode opc_rxy,
919                        TCGReg data, TCGReg base, TCGReg index,
920                        tcg_target_long ofs)
921{
922    if (ofs < -0x80000 || ofs >= 0x80000) {
923        /* Combine the low 20 bits of the offset with the actual load insn;
924           the high 44 bits must come from an immediate load.  */
925        tcg_target_long low = ((ofs & 0xfffff) ^ 0x80000) - 0x80000;
926        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - low);
927        ofs = low;
928
929        /* If we were already given an index register, add it in.  */
930        if (index != TCG_REG_NONE) {
931            tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
932        }
933        index = TCG_TMP0;
934    }
935
936    if (opc_rx && ofs >= 0 && ofs < 0x1000) {
937        tcg_out_insn_RX(s, opc_rx, data, base, index, ofs);
938    } else {
939        tcg_out_insn_RXY(s, opc_rxy, data, base, index, ofs);
940    }
941}
942
943static void tcg_out_vrx_mem(TCGContext *s, S390Opcode opc_vrx,
944                            TCGReg data, TCGReg base, TCGReg index,
945                            tcg_target_long ofs, int m3)
946{
947    if (ofs < 0 || ofs >= 0x1000) {
948        if (ofs >= -0x80000 && ofs < 0x80000) {
949            tcg_out_insn(s, RXY, LAY, TCG_TMP0, base, index, ofs);
950            base = TCG_TMP0;
951            index = TCG_REG_NONE;
952            ofs = 0;
953        } else {
954            tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs);
955            if (index != TCG_REG_NONE) {
956                tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
957            }
958            index = TCG_TMP0;
959            ofs = 0;
960        }
961    }
962    tcg_out_insn_VRX(s, opc_vrx, data, base, index, ofs, m3);
963}
964
965/* load data without address translation or endianness conversion */
966static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg data,
967                       TCGReg base, intptr_t ofs)
968{
969    switch (type) {
970    case TCG_TYPE_I32:
971        if (likely(is_general_reg(data))) {
972            tcg_out_mem(s, RX_L, RXY_LY, data, base, TCG_REG_NONE, ofs);
973            break;
974        }
975        tcg_out_vrx_mem(s, VRX_VLLEZ, data, base, TCG_REG_NONE, ofs, MO_32);
976        break;
977
978    case TCG_TYPE_I64:
979        if (likely(is_general_reg(data))) {
980            tcg_out_mem(s, 0, RXY_LG, data, base, TCG_REG_NONE, ofs);
981            break;
982        }
983        /* fallthru */
984
985    case TCG_TYPE_V64:
986        tcg_out_vrx_mem(s, VRX_VLLEZ, data, base, TCG_REG_NONE, ofs, MO_64);
987        break;
988
989    case TCG_TYPE_V128:
990        /* Hint quadword aligned.  */
991        tcg_out_vrx_mem(s, VRX_VL, data, base, TCG_REG_NONE, ofs, 4);
992        break;
993
994    default:
995        g_assert_not_reached();
996    }
997}
998
999static void tcg_out_st(TCGContext *s, TCGType type, TCGReg data,
1000                       TCGReg base, intptr_t ofs)
1001{
1002    switch (type) {
1003    case TCG_TYPE_I32:
1004        if (likely(is_general_reg(data))) {
1005            tcg_out_mem(s, RX_ST, RXY_STY, data, base, TCG_REG_NONE, ofs);
1006        } else {
1007            tcg_out_vrx_mem(s, VRX_VSTEF, data, base, TCG_REG_NONE, ofs, 1);
1008        }
1009        break;
1010
1011    case TCG_TYPE_I64:
1012        if (likely(is_general_reg(data))) {
1013            tcg_out_mem(s, 0, RXY_STG, data, base, TCG_REG_NONE, ofs);
1014            break;
1015        }
1016        /* fallthru */
1017
1018    case TCG_TYPE_V64:
1019        tcg_out_vrx_mem(s, VRX_VSTEG, data, base, TCG_REG_NONE, ofs, 0);
1020        break;
1021
1022    case TCG_TYPE_V128:
1023        /* Hint quadword aligned.  */
1024        tcg_out_vrx_mem(s, VRX_VST, data, base, TCG_REG_NONE, ofs, 4);
1025        break;
1026
1027    default:
1028        g_assert_not_reached();
1029    }
1030}
1031
1032static inline bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
1033                               TCGReg base, intptr_t ofs)
1034{
1035    return false;
1036}
1037
1038static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2)
1039{
1040    return false;
1041}
1042
1043static void tcg_out_addi_ptr(TCGContext *s, TCGReg rd, TCGReg rs,
1044                             tcg_target_long imm)
1045{
1046    /* This function is only used for passing structs by reference. */
1047    tcg_out_mem(s, RX_LA, RXY_LAY, rd, rs, TCG_REG_NONE, imm);
1048}
1049
1050static inline void tcg_out_risbg(TCGContext *s, TCGReg dest, TCGReg src,
1051                                 int msb, int lsb, int ofs, int z)
1052{
1053    /* Format RIE-f */
1054    tcg_out16(s, (RIEf_RISBG & 0xff00) | (dest << 4) | src);
1055    tcg_out16(s, (msb << 8) | (z << 7) | lsb);
1056    tcg_out16(s, (ofs << 8) | (RIEf_RISBG & 0xff));
1057}
1058
1059static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
1060{
1061    tcg_out_insn(s, RRE, LGBR, dest, src);
1062}
1063
1064static void tcg_out_ext8u(TCGContext *s, TCGReg dest, TCGReg src)
1065{
1066    tcg_out_insn(s, RRE, LLGCR, dest, src);
1067}
1068
1069static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
1070{
1071    tcg_out_insn(s, RRE, LGHR, dest, src);
1072}
1073
1074static void tcg_out_ext16u(TCGContext *s, TCGReg dest, TCGReg src)
1075{
1076    tcg_out_insn(s, RRE, LLGHR, dest, src);
1077}
1078
1079static void tcg_out_ext32s(TCGContext *s, TCGReg dest, TCGReg src)
1080{
1081    tcg_out_insn(s, RRE, LGFR, dest, src);
1082}
1083
1084static void tcg_out_ext32u(TCGContext *s, TCGReg dest, TCGReg src)
1085{
1086    tcg_out_insn(s, RRE, LLGFR, dest, src);
1087}
1088
1089static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg dest, TCGReg src)
1090{
1091    tcg_out_ext32s(s, dest, src);
1092}
1093
1094static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg dest, TCGReg src)
1095{
1096    tcg_out_ext32u(s, dest, src);
1097}
1098
1099static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg dest, TCGReg src)
1100{
1101    tcg_out_mov(s, TCG_TYPE_I32, dest, src);
1102}
1103
1104static void tgen_andi_risbg(TCGContext *s, TCGReg out, TCGReg in, uint64_t val)
1105{
1106    int msb, lsb;
1107    if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
1108        /* Achieve wraparound by swapping msb and lsb.  */
1109        msb = 64 - ctz64(~val);
1110        lsb = clz64(~val) - 1;
1111    } else {
1112        msb = clz64(val);
1113        lsb = 63 - ctz64(val);
1114    }
1115    tcg_out_risbg(s, out, in, msb, lsb, 0, 1);
1116}
1117
1118static void tgen_andi(TCGContext *s, TCGType type, TCGReg dest, uint64_t val)
1119{
1120    static const S390Opcode ni_insns[4] = {
1121        RI_NILL, RI_NILH, RI_NIHL, RI_NIHH
1122    };
1123    static const S390Opcode nif_insns[2] = {
1124        RIL_NILF, RIL_NIHF
1125    };
1126    uint64_t valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull);
1127    int i;
1128
1129    /* Look for the zero-extensions.  */
1130    if ((val & valid) == 0xffffffff) {
1131        tcg_out_ext32u(s, dest, dest);
1132        return;
1133    }
1134    if ((val & valid) == 0xff) {
1135        tcg_out_ext8u(s, dest, dest);
1136        return;
1137    }
1138    if ((val & valid) == 0xffff) {
1139        tcg_out_ext16u(s, dest, dest);
1140        return;
1141    }
1142
1143    i = is_const_p16(~val & valid);
1144    if (i >= 0) {
1145        tcg_out_insn_RI(s, ni_insns[i], dest, val >> (i * 16));
1146        return;
1147    }
1148
1149    i = is_const_p32(~val & valid);
1150    tcg_debug_assert(i == 0 || type != TCG_TYPE_I32);
1151    if (i >= 0) {
1152        tcg_out_insn_RIL(s, nif_insns[i], dest, val >> (i * 32));
1153        return;
1154    }
1155
1156    if (risbg_mask(val)) {
1157        tgen_andi_risbg(s, dest, dest, val);
1158        return;
1159    }
1160
1161    g_assert_not_reached();
1162}
1163
1164static void tgen_ori(TCGContext *s, TCGReg dest, uint64_t val)
1165{
1166    static const S390Opcode oif_insns[2] = {
1167        RIL_OILF, RIL_OIHF
1168    };
1169
1170    int i;
1171
1172    i = is_const_p16(val);
1173    if (i >= 0) {
1174        tcg_out_insn_RI(s, oi_insns[i], dest, val >> (i * 16));
1175        return;
1176    }
1177
1178    i = is_const_p32(val);
1179    if (i >= 0) {
1180        tcg_out_insn_RIL(s, oif_insns[i], dest, val >> (i * 32));
1181        return;
1182    }
1183
1184    g_assert_not_reached();
1185}
1186
1187static void tgen_xori(TCGContext *s, TCGReg dest, uint64_t val)
1188{
1189    switch (is_const_p32(val)) {
1190    case 0:
1191        tcg_out_insn(s, RIL, XILF, dest, val);
1192        break;
1193    case 1:
1194        tcg_out_insn(s, RIL, XIHF, dest, val >> 32);
1195        break;
1196    default:
1197        g_assert_not_reached();
1198    }
1199}
1200
1201static int tgen_cmp2(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
1202                     TCGArg c2, bool c2const, bool need_carry, int *inv_cc)
1203{
1204    bool is_unsigned = is_unsigned_cond(c);
1205    TCGCond inv_c = tcg_invert_cond(c);
1206    S390Opcode op;
1207
1208    if (c2const) {
1209        if (c2 == 0) {
1210            if (!(is_unsigned && need_carry)) {
1211                if (type == TCG_TYPE_I32) {
1212                    tcg_out_insn(s, RR, LTR, r1, r1);
1213                } else {
1214                    tcg_out_insn(s, RRE, LTGR, r1, r1);
1215                }
1216                *inv_cc = tcg_cond_to_ltr_cond[inv_c];
1217                return tcg_cond_to_ltr_cond[c];
1218            }
1219        }
1220
1221        if (!is_unsigned && c2 == (int16_t)c2) {
1222            op = (type == TCG_TYPE_I32 ? RI_CHI : RI_CGHI);
1223            tcg_out_insn_RI(s, op, r1, c2);
1224            goto exit;
1225        }
1226
1227        if (type == TCG_TYPE_I32) {
1228            op = (is_unsigned ? RIL_CLFI : RIL_CFI);
1229            tcg_out_insn_RIL(s, op, r1, c2);
1230            goto exit;
1231        }
1232
1233        /*
1234         * Constraints are for a signed 33-bit operand, which is a
1235         * convenient superset of this signed/unsigned test.
1236         */
1237        if (c2 == (is_unsigned ? (TCGArg)(uint32_t)c2 : (TCGArg)(int32_t)c2)) {
1238            op = (is_unsigned ? RIL_CLGFI : RIL_CGFI);
1239            tcg_out_insn_RIL(s, op, r1, c2);
1240            goto exit;
1241        }
1242
1243        /* Load everything else into a register. */
1244        tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, c2);
1245        c2 = TCG_TMP0;
1246    }
1247
1248    if (type == TCG_TYPE_I32) {
1249        op = (is_unsigned ? RR_CLR : RR_CR);
1250        tcg_out_insn_RR(s, op, r1, c2);
1251    } else {
1252        op = (is_unsigned ? RRE_CLGR : RRE_CGR);
1253        tcg_out_insn_RRE(s, op, r1, c2);
1254    }
1255
1256 exit:
1257    *inv_cc = tcg_cond_to_s390_cond[inv_c];
1258    return tcg_cond_to_s390_cond[c];
1259}
1260
1261static int tgen_cmp(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
1262                    TCGArg c2, bool c2const, bool need_carry)
1263{
1264    int inv_cc;
1265    return tgen_cmp2(s, type, c, r1, c2, c2const, need_carry, &inv_cc);
1266}
1267
1268static void tgen_setcond(TCGContext *s, TCGType type, TCGCond cond,
1269                         TCGReg dest, TCGReg c1, TCGArg c2, int c2const)
1270{
1271    int cc;
1272
1273    /* With LOC2, we can always emit the minimum 3 insns.  */
1274    if (HAVE_FACILITY(LOAD_ON_COND2)) {
1275        /* Emit: d = 0, d = (cc ? 1 : d).  */
1276        cc = tgen_cmp(s, type, cond, c1, c2, c2const, false);
1277        tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
1278        tcg_out_insn(s, RIEg, LOCGHI, dest, 1, cc);
1279        return;
1280    }
1281
1282 restart:
1283    switch (cond) {
1284    case TCG_COND_NE:
1285        /* X != 0 is X > 0.  */
1286        if (c2const && c2 == 0) {
1287            cond = TCG_COND_GTU;
1288        } else {
1289            break;
1290        }
1291        /* fallthru */
1292
1293    case TCG_COND_GTU:
1294    case TCG_COND_GT:
1295        /* The result of a compare has CC=2 for GT and CC=3 unused.
1296           ADD LOGICAL WITH CARRY considers (CC & 2) the carry bit.  */
1297        tgen_cmp(s, type, cond, c1, c2, c2const, true);
1298        tcg_out_movi(s, type, dest, 0);
1299        tcg_out_insn(s, RRE, ALCGR, dest, dest);
1300        return;
1301
1302    case TCG_COND_EQ:
1303        /* X == 0 is X <= 0.  */
1304        if (c2const && c2 == 0) {
1305            cond = TCG_COND_LEU;
1306        } else {
1307            break;
1308        }
1309        /* fallthru */
1310
1311    case TCG_COND_LEU:
1312    case TCG_COND_LE:
1313        /* As above, but we're looking for borrow, or !carry.
1314           The second insn computes d - d - borrow, or -1 for true
1315           and 0 for false.  So we must mask to 1 bit afterward.  */
1316        tgen_cmp(s, type, cond, c1, c2, c2const, true);
1317        tcg_out_insn(s, RRE, SLBGR, dest, dest);
1318        tgen_andi(s, type, dest, 1);
1319        return;
1320
1321    case TCG_COND_GEU:
1322    case TCG_COND_LTU:
1323    case TCG_COND_LT:
1324    case TCG_COND_GE:
1325        /* Swap operands so that we can use LEU/GTU/GT/LE.  */
1326        if (!c2const) {
1327            TCGReg t = c1;
1328            c1 = c2;
1329            c2 = t;
1330            cond = tcg_swap_cond(cond);
1331            goto restart;
1332        }
1333        break;
1334
1335    default:
1336        g_assert_not_reached();
1337    }
1338
1339    cc = tgen_cmp(s, type, cond, c1, c2, c2const, false);
1340    /* Emit: d = 0, t = 1, d = (cc ? t : d).  */
1341    tcg_out_movi(s, TCG_TYPE_I64, dest, 0);
1342    tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 1);
1343    tcg_out_insn(s, RRFc, LOCGR, dest, TCG_TMP0, cc);
1344}
1345
1346static void tgen_movcond_int(TCGContext *s, TCGType type, TCGReg dest,
1347                             TCGArg v3, int v3const, TCGReg v4,
1348                             int cc, int inv_cc)
1349{
1350    TCGReg src;
1351
1352    if (v3const) {
1353        if (dest == v4) {
1354            if (HAVE_FACILITY(LOAD_ON_COND2)) {
1355                /* Emit: if (cc) dest = v3. */
1356                tcg_out_insn(s, RIEg, LOCGHI, dest, v3, cc);
1357                return;
1358            }
1359            tcg_out_insn(s, RI, LGHI, TCG_TMP0, v3);
1360            src = TCG_TMP0;
1361        } else {
1362            /* LGR+LOCGHI is larger than LGHI+LOCGR. */
1363            tcg_out_insn(s, RI, LGHI, dest, v3);
1364            cc = inv_cc;
1365            src = v4;
1366        }
1367    } else {
1368        if (HAVE_FACILITY(MISC_INSN_EXT3)) {
1369            /* Emit: dest = cc ? v3 : v4. */
1370            tcg_out_insn(s, RRFam, SELGR, dest, v3, v4, cc);
1371            return;
1372        }
1373        if (dest == v4) {
1374            src = v3;
1375        } else {
1376            tcg_out_mov(s, type, dest, v3);
1377            cc = inv_cc;
1378            src = v4;
1379        }
1380    }
1381
1382    /* Emit: if (cc) dest = src. */
1383    tcg_out_insn(s, RRFc, LOCGR, dest, src, cc);
1384}
1385
1386static void tgen_movcond(TCGContext *s, TCGType type, TCGCond c, TCGReg dest,
1387                         TCGReg c1, TCGArg c2, int c2const,
1388                         TCGArg v3, int v3const, TCGReg v4)
1389{
1390    int cc, inv_cc;
1391
1392    cc = tgen_cmp2(s, type, c, c1, c2, c2const, false, &inv_cc);
1393    tgen_movcond_int(s, type, dest, v3, v3const, v4, cc, inv_cc);
1394}
1395
1396static void tgen_clz(TCGContext *s, TCGReg dest, TCGReg a1,
1397                     TCGArg a2, int a2const)
1398{
1399    /* Since this sets both R and R+1, we have no choice but to store the
1400       result into R0, allowing R1 == TCG_TMP0 to be clobbered as well.  */
1401    QEMU_BUILD_BUG_ON(TCG_TMP0 != TCG_REG_R1);
1402    tcg_out_insn(s, RRE, FLOGR, TCG_REG_R0, a1);
1403
1404    if (a2const && a2 == 64) {
1405        tcg_out_mov(s, TCG_TYPE_I64, dest, TCG_REG_R0);
1406        return;
1407    }
1408
1409    /*
1410     * Conditions from FLOGR are:
1411     *   2 -> one bit found
1412     *   8 -> no one bit found
1413     */
1414    tgen_movcond_int(s, TCG_TYPE_I64, dest, a2, a2const, TCG_REG_R0, 8, 2);
1415}
1416
1417static void tgen_ctpop(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
1418{
1419    /* With MIE3, and bit 0 of m4 set, we get the complete result. */
1420    if (HAVE_FACILITY(MISC_INSN_EXT3)) {
1421        if (type == TCG_TYPE_I32) {
1422            tcg_out_ext32u(s, dest, src);
1423            src = dest;
1424        }
1425        tcg_out_insn(s, RRFc, POPCNT, dest, src, 8);
1426        return;
1427    }
1428
1429    /* Without MIE3, each byte gets the count of bits for the byte. */
1430    tcg_out_insn(s, RRFc, POPCNT, dest, src, 0);
1431
1432    /* Multiply to sum each byte at the top of the word. */
1433    if (type == TCG_TYPE_I32) {
1434        tcg_out_insn(s, RIL, MSFI, dest, 0x01010101);
1435        tcg_out_sh32(s, RS_SRL, dest, TCG_REG_NONE, 24);
1436    } else {
1437        tcg_out_movi(s, TCG_TYPE_I64, TCG_TMP0, 0x0101010101010101ull);
1438        tcg_out_insn(s, RRE, MSGR, dest, TCG_TMP0);
1439        tcg_out_sh64(s, RSY_SRLG, dest, dest, TCG_REG_NONE, 56);
1440    }
1441}
1442
1443static void tgen_deposit(TCGContext *s, TCGReg dest, TCGReg src,
1444                         int ofs, int len, int z)
1445{
1446    int lsb = (63 - ofs);
1447    int msb = lsb - (len - 1);
1448    tcg_out_risbg(s, dest, src, msb, lsb, ofs, z);
1449}
1450
1451static void tgen_extract(TCGContext *s, TCGReg dest, TCGReg src,
1452                         int ofs, int len)
1453{
1454    tcg_out_risbg(s, dest, src, 64 - len, 63, 64 - ofs, 1);
1455}
1456
1457static void tgen_gotoi(TCGContext *s, int cc, const tcg_insn_unit *dest)
1458{
1459    ptrdiff_t off = tcg_pcrel_diff(s, dest) >> 1;
1460    if (off == (int16_t)off) {
1461        tcg_out_insn(s, RI, BRC, cc, off);
1462    } else if (off == (int32_t)off) {
1463        tcg_out_insn(s, RIL, BRCL, cc, off);
1464    } else {
1465        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1466        tcg_out_insn(s, RR, BCR, cc, TCG_TMP0);
1467    }
1468}
1469
1470static void tgen_branch(TCGContext *s, int cc, TCGLabel *l)
1471{
1472    if (l->has_value) {
1473        tgen_gotoi(s, cc, l->u.value_ptr);
1474    } else {
1475        tcg_out16(s, RI_BRC | (cc << 4));
1476        tcg_out_reloc(s, s->code_ptr, R_390_PC16DBL, l, 2);
1477        s->code_ptr += 1;
1478    }
1479}
1480
1481static void tgen_compare_branch(TCGContext *s, S390Opcode opc, int cc,
1482                                TCGReg r1, TCGReg r2, TCGLabel *l)
1483{
1484    tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, 2);
1485    /* Format RIE-b */
1486    tcg_out16(s, (opc & 0xff00) | (r1 << 4) | r2);
1487    tcg_out16(s, 0);
1488    tcg_out16(s, cc << 12 | (opc & 0xff));
1489}
1490
1491static void tgen_compare_imm_branch(TCGContext *s, S390Opcode opc, int cc,
1492                                    TCGReg r1, int i2, TCGLabel *l)
1493{
1494    tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, l, 2);
1495    /* Format RIE-c */
1496    tcg_out16(s, (opc & 0xff00) | (r1 << 4) | cc);
1497    tcg_out16(s, 0);
1498    tcg_out16(s, (i2 << 8) | (opc & 0xff));
1499}
1500
1501static void tgen_brcond(TCGContext *s, TCGType type, TCGCond c,
1502                        TCGReg r1, TCGArg c2, int c2const, TCGLabel *l)
1503{
1504    int cc;
1505    bool is_unsigned = is_unsigned_cond(c);
1506    bool in_range;
1507    S390Opcode opc;
1508
1509    cc = tcg_cond_to_s390_cond[c];
1510
1511    if (!c2const) {
1512        opc = (type == TCG_TYPE_I32
1513               ? (is_unsigned ? RIEb_CLRJ : RIEb_CRJ)
1514               : (is_unsigned ? RIEb_CLGRJ : RIEb_CGRJ));
1515        tgen_compare_branch(s, opc, cc, r1, c2, l);
1516        return;
1517    }
1518
1519    /*
1520     * COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field.
1521     * If the immediate we've been given does not fit that range, we'll
1522     * fall back to separate compare and branch instructions using the
1523     * larger comparison range afforded by COMPARE IMMEDIATE.
1524     */
1525    if (type == TCG_TYPE_I32) {
1526        if (is_unsigned) {
1527            opc = RIEc_CLIJ;
1528            in_range = (uint32_t)c2 == (uint8_t)c2;
1529        } else {
1530            opc = RIEc_CIJ;
1531            in_range = (int32_t)c2 == (int8_t)c2;
1532        }
1533    } else {
1534        if (is_unsigned) {
1535            opc = RIEc_CLGIJ;
1536            in_range = (uint64_t)c2 == (uint8_t)c2;
1537        } else {
1538            opc = RIEc_CGIJ;
1539            in_range = (int64_t)c2 == (int8_t)c2;
1540        }
1541    }
1542    if (in_range) {
1543        tgen_compare_imm_branch(s, opc, cc, r1, c2, l);
1544        return;
1545    }
1546
1547    cc = tgen_cmp(s, type, c, r1, c2, c2const, false);
1548    tgen_branch(s, cc, l);
1549}
1550
1551static void tcg_out_call_int(TCGContext *s, const tcg_insn_unit *dest)
1552{
1553    ptrdiff_t off = tcg_pcrel_diff(s, dest) >> 1;
1554    if (off == (int32_t)off) {
1555        tcg_out_insn(s, RIL, BRASL, TCG_REG_R14, off);
1556    } else {
1557        tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1558        tcg_out_insn(s, RR, BASR, TCG_REG_R14, TCG_TMP0);
1559    }
1560}
1561
1562static void tcg_out_call(TCGContext *s, const tcg_insn_unit *dest,
1563                         const TCGHelperInfo *info)
1564{
1565    tcg_out_call_int(s, dest);
1566}
1567
1568typedef struct {
1569    TCGReg base;
1570    TCGReg index;
1571    int disp;
1572    TCGAtomAlign aa;
1573} HostAddress;
1574
1575bool tcg_target_has_memory_bswap(MemOp memop)
1576{
1577    TCGAtomAlign aa;
1578
1579    if ((memop & MO_SIZE) <= MO_64) {
1580        return true;
1581    }
1582
1583    /*
1584     * Reject 16-byte memop with 16-byte atomicity,
1585     * but do allow a pair of 64-bit operations.
1586     */
1587    aa = atom_and_align_for_opc(tcg_ctx, memop, MO_ATOM_IFALIGN, true);
1588    return aa.atom <= MO_64;
1589}
1590
1591static void tcg_out_qemu_ld_direct(TCGContext *s, MemOp opc, TCGReg data,
1592                                   HostAddress h)
1593{
1594    switch (opc & (MO_SSIZE | MO_BSWAP)) {
1595    case MO_UB:
1596        tcg_out_insn(s, RXY, LLGC, data, h.base, h.index, h.disp);
1597        break;
1598    case MO_SB:
1599        tcg_out_insn(s, RXY, LGB, data, h.base, h.index, h.disp);
1600        break;
1601
1602    case MO_UW | MO_BSWAP:
1603        /* swapped unsigned halfword load with upper bits zeroed */
1604        tcg_out_insn(s, RXY, LRVH, data, h.base, h.index, h.disp);
1605        tcg_out_ext16u(s, data, data);
1606        break;
1607    case MO_UW:
1608        tcg_out_insn(s, RXY, LLGH, data, h.base, h.index, h.disp);
1609        break;
1610
1611    case MO_SW | MO_BSWAP:
1612        /* swapped sign-extended halfword load */
1613        tcg_out_insn(s, RXY, LRVH, data, h.base, h.index, h.disp);
1614        tcg_out_ext16s(s, TCG_TYPE_REG, data, data);
1615        break;
1616    case MO_SW:
1617        tcg_out_insn(s, RXY, LGH, data, h.base, h.index, h.disp);
1618        break;
1619
1620    case MO_UL | MO_BSWAP:
1621        /* swapped unsigned int load with upper bits zeroed */
1622        tcg_out_insn(s, RXY, LRV, data, h.base, h.index, h.disp);
1623        tcg_out_ext32u(s, data, data);
1624        break;
1625    case MO_UL:
1626        tcg_out_insn(s, RXY, LLGF, data, h.base, h.index, h.disp);
1627        break;
1628
1629    case MO_SL | MO_BSWAP:
1630        /* swapped sign-extended int load */
1631        tcg_out_insn(s, RXY, LRV, data, h.base, h.index, h.disp);
1632        tcg_out_ext32s(s, data, data);
1633        break;
1634    case MO_SL:
1635        tcg_out_insn(s, RXY, LGF, data, h.base, h.index, h.disp);
1636        break;
1637
1638    case MO_UQ | MO_BSWAP:
1639        tcg_out_insn(s, RXY, LRVG, data, h.base, h.index, h.disp);
1640        break;
1641    case MO_UQ:
1642        tcg_out_insn(s, RXY, LG, data, h.base, h.index, h.disp);
1643        break;
1644
1645    default:
1646        g_assert_not_reached();
1647    }
1648}
1649
1650static void tcg_out_qemu_st_direct(TCGContext *s, MemOp opc, TCGReg data,
1651                                   HostAddress h)
1652{
1653    switch (opc & (MO_SIZE | MO_BSWAP)) {
1654    case MO_UB:
1655        if (h.disp >= 0 && h.disp < 0x1000) {
1656            tcg_out_insn(s, RX, STC, data, h.base, h.index, h.disp);
1657        } else {
1658            tcg_out_insn(s, RXY, STCY, data, h.base, h.index, h.disp);
1659        }
1660        break;
1661
1662    case MO_UW | MO_BSWAP:
1663        tcg_out_insn(s, RXY, STRVH, data, h.base, h.index, h.disp);
1664        break;
1665    case MO_UW:
1666        if (h.disp >= 0 && h.disp < 0x1000) {
1667            tcg_out_insn(s, RX, STH, data, h.base, h.index, h.disp);
1668        } else {
1669            tcg_out_insn(s, RXY, STHY, data, h.base, h.index, h.disp);
1670        }
1671        break;
1672
1673    case MO_UL | MO_BSWAP:
1674        tcg_out_insn(s, RXY, STRV, data, h.base, h.index, h.disp);
1675        break;
1676    case MO_UL:
1677        if (h.disp >= 0 && h.disp < 0x1000) {
1678            tcg_out_insn(s, RX, ST, data, h.base, h.index, h.disp);
1679        } else {
1680            tcg_out_insn(s, RXY, STY, data, h.base, h.index, h.disp);
1681        }
1682        break;
1683
1684    case MO_UQ | MO_BSWAP:
1685        tcg_out_insn(s, RXY, STRVG, data, h.base, h.index, h.disp);
1686        break;
1687    case MO_UQ:
1688        tcg_out_insn(s, RXY, STG, data, h.base, h.index, h.disp);
1689        break;
1690
1691    default:
1692        g_assert_not_reached();
1693    }
1694}
1695
1696static const TCGLdstHelperParam ldst_helper_param = {
1697    .ntmp = 1, .tmp = { TCG_TMP0 }
1698};
1699
1700static bool tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1701{
1702    MemOp opc = get_memop(lb->oi);
1703
1704    if (!patch_reloc(lb->label_ptr[0], R_390_PC16DBL,
1705                     (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 2)) {
1706        return false;
1707    }
1708
1709    tcg_out_ld_helper_args(s, lb, &ldst_helper_param);
1710    tcg_out_call_int(s, qemu_ld_helpers[opc & MO_SIZE]);
1711    tcg_out_ld_helper_ret(s, lb, false, &ldst_helper_param);
1712
1713    tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1714    return true;
1715}
1716
1717static bool tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1718{
1719    MemOp opc = get_memop(lb->oi);
1720
1721    if (!patch_reloc(lb->label_ptr[0], R_390_PC16DBL,
1722                     (intptr_t)tcg_splitwx_to_rx(s->code_ptr), 2)) {
1723        return false;
1724    }
1725
1726    tcg_out_st_helper_args(s, lb, &ldst_helper_param);
1727    tcg_out_call_int(s, qemu_st_helpers[opc & MO_SIZE]);
1728
1729    tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1730    return true;
1731}
1732
1733/* We're expecting to use a 20-bit negative offset on the tlb memory ops.  */
1734#define MIN_TLB_MASK_TABLE_OFS  -(1 << 19)
1735
1736/*
1737 * For softmmu, perform the TLB load and compare.
1738 * For useronly, perform any required alignment tests.
1739 * In both cases, return a TCGLabelQemuLdst structure if the slow path
1740 * is required and fill in @h with the host address for the fast path.
1741 */
1742static TCGLabelQemuLdst *prepare_host_addr(TCGContext *s, HostAddress *h,
1743                                           TCGReg addr_reg, MemOpIdx oi,
1744                                           bool is_ld)
1745{
1746    TCGType addr_type = s->addr_type;
1747    TCGLabelQemuLdst *ldst = NULL;
1748    MemOp opc = get_memop(oi);
1749    MemOp s_bits = opc & MO_SIZE;
1750    unsigned a_mask;
1751
1752    h->aa = atom_and_align_for_opc(s, opc, MO_ATOM_IFALIGN, s_bits == MO_128);
1753    a_mask = (1 << h->aa.align) - 1;
1754
1755#ifdef CONFIG_SOFTMMU
1756    unsigned s_mask = (1 << s_bits) - 1;
1757    int mem_index = get_mmuidx(oi);
1758    int fast_off = tlb_mask_table_ofs(s, mem_index);
1759    int mask_off = fast_off + offsetof(CPUTLBDescFast, mask);
1760    int table_off = fast_off + offsetof(CPUTLBDescFast, table);
1761    int ofs, a_off;
1762    uint64_t tlb_mask;
1763
1764    ldst = new_ldst_label(s);
1765    ldst->is_ld = is_ld;
1766    ldst->oi = oi;
1767    ldst->addrlo_reg = addr_reg;
1768
1769    tcg_out_sh64(s, RSY_SRLG, TCG_TMP0, addr_reg, TCG_REG_NONE,
1770                 s->page_bits - CPU_TLB_ENTRY_BITS);
1771
1772    tcg_out_insn(s, RXY, NG, TCG_TMP0, TCG_AREG0, TCG_REG_NONE, mask_off);
1773    tcg_out_insn(s, RXY, AG, TCG_TMP0, TCG_AREG0, TCG_REG_NONE, table_off);
1774
1775    /*
1776     * For aligned accesses, we check the first byte and include the alignment
1777     * bits within the address.  For unaligned access, we check that we don't
1778     * cross pages using the address of the last byte of the access.
1779     */
1780    a_off = (a_mask >= s_mask ? 0 : s_mask - a_mask);
1781    tlb_mask = (uint64_t)s->page_mask | a_mask;
1782    if (a_off == 0) {
1783        tgen_andi_risbg(s, TCG_REG_R0, addr_reg, tlb_mask);
1784    } else {
1785        tcg_out_insn(s, RX, LA, TCG_REG_R0, addr_reg, TCG_REG_NONE, a_off);
1786        tgen_andi(s, addr_type, TCG_REG_R0, tlb_mask);
1787    }
1788
1789    if (is_ld) {
1790        ofs = offsetof(CPUTLBEntry, addr_read);
1791    } else {
1792        ofs = offsetof(CPUTLBEntry, addr_write);
1793    }
1794    if (addr_type == TCG_TYPE_I32) {
1795        ofs += HOST_BIG_ENDIAN * 4;
1796        tcg_out_insn(s, RX, C, TCG_REG_R0, TCG_TMP0, TCG_REG_NONE, ofs);
1797    } else {
1798        tcg_out_insn(s, RXY, CG, TCG_REG_R0, TCG_TMP0, TCG_REG_NONE, ofs);
1799    }
1800
1801    tcg_out16(s, RI_BRC | (S390_CC_NE << 4));
1802    ldst->label_ptr[0] = s->code_ptr++;
1803
1804    h->index = TCG_TMP0;
1805    tcg_out_insn(s, RXY, LG, h->index, TCG_TMP0, TCG_REG_NONE,
1806                 offsetof(CPUTLBEntry, addend));
1807
1808    if (addr_type == TCG_TYPE_I32) {
1809        tcg_out_insn(s, RRE, ALGFR, h->index, addr_reg);
1810        h->base = TCG_REG_NONE;
1811    } else {
1812        h->base = addr_reg;
1813    }
1814    h->disp = 0;
1815#else
1816    if (a_mask) {
1817        ldst = new_ldst_label(s);
1818        ldst->is_ld = is_ld;
1819        ldst->oi = oi;
1820        ldst->addrlo_reg = addr_reg;
1821
1822        /* We are expecting a_bits to max out at 7, much lower than TMLL. */
1823        tcg_debug_assert(a_mask <= 0xffff);
1824        tcg_out_insn(s, RI, TMLL, addr_reg, a_mask);
1825
1826        tcg_out16(s, RI_BRC | (7 << 4)); /* CC in {1,2,3} */
1827        ldst->label_ptr[0] = s->code_ptr++;
1828    }
1829
1830    h->base = addr_reg;
1831    if (addr_type == TCG_TYPE_I32) {
1832        tcg_out_ext32u(s, TCG_TMP0, addr_reg);
1833        h->base = TCG_TMP0;
1834    }
1835    if (guest_base < 0x80000) {
1836        h->index = TCG_REG_NONE;
1837        h->disp = guest_base;
1838    } else {
1839        h->index = TCG_GUEST_BASE_REG;
1840        h->disp = 0;
1841    }
1842#endif
1843
1844    return ldst;
1845}
1846
1847static void tcg_out_qemu_ld(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1848                            MemOpIdx oi, TCGType data_type)
1849{
1850    TCGLabelQemuLdst *ldst;
1851    HostAddress h;
1852
1853    ldst = prepare_host_addr(s, &h, addr_reg, oi, true);
1854    tcg_out_qemu_ld_direct(s, get_memop(oi), data_reg, h);
1855
1856    if (ldst) {
1857        ldst->type = data_type;
1858        ldst->datalo_reg = data_reg;
1859        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1860    }
1861}
1862
1863static void tcg_out_qemu_st(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1864                            MemOpIdx oi, TCGType data_type)
1865{
1866    TCGLabelQemuLdst *ldst;
1867    HostAddress h;
1868
1869    ldst = prepare_host_addr(s, &h, addr_reg, oi, false);
1870    tcg_out_qemu_st_direct(s, get_memop(oi), data_reg, h);
1871
1872    if (ldst) {
1873        ldst->type = data_type;
1874        ldst->datalo_reg = data_reg;
1875        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1876    }
1877}
1878
1879static void tcg_out_qemu_ldst_i128(TCGContext *s, TCGReg datalo, TCGReg datahi,
1880                                   TCGReg addr_reg, MemOpIdx oi, bool is_ld)
1881{
1882    TCGLabel *l1 = NULL, *l2 = NULL;
1883    TCGLabelQemuLdst *ldst;
1884    HostAddress h;
1885    bool need_bswap;
1886    bool use_pair;
1887    S390Opcode insn;
1888
1889    ldst = prepare_host_addr(s, &h, addr_reg, oi, is_ld);
1890
1891    use_pair = h.aa.atom < MO_128;
1892    need_bswap = get_memop(oi) & MO_BSWAP;
1893
1894    if (!use_pair) {
1895        /*
1896         * Atomicity requires we use LPQ.  If we've already checked for
1897         * 16-byte alignment, that's all we need.  If we arrive with
1898         * lesser alignment, we have determined that less than 16-byte
1899         * alignment can be satisfied with two 8-byte loads.
1900         */
1901        if (h.aa.align < MO_128) {
1902            use_pair = true;
1903            l1 = gen_new_label();
1904            l2 = gen_new_label();
1905
1906            tcg_out_insn(s, RI, TMLL, addr_reg, 15);
1907            tgen_branch(s, 7, l1); /* CC in {1,2,3} */
1908        }
1909
1910        tcg_debug_assert(!need_bswap);
1911        tcg_debug_assert(datalo & 1);
1912        tcg_debug_assert(datahi == datalo - 1);
1913        insn = is_ld ? RXY_LPQ : RXY_STPQ;
1914        tcg_out_insn_RXY(s, insn, datahi, h.base, h.index, h.disp);
1915
1916        if (use_pair) {
1917            tgen_branch(s, S390_CC_ALWAYS, l2);
1918            tcg_out_label(s, l1);
1919        }
1920    }
1921    if (use_pair) {
1922        TCGReg d1, d2;
1923
1924        if (need_bswap) {
1925            d1 = datalo, d2 = datahi;
1926            insn = is_ld ? RXY_LRVG : RXY_STRVG;
1927        } else {
1928            d1 = datahi, d2 = datalo;
1929            insn = is_ld ? RXY_LG : RXY_STG;
1930        }
1931
1932        if (h.base == d1 || h.index == d1) {
1933            tcg_out_insn(s, RXY, LAY, TCG_TMP0, h.base, h.index, h.disp);
1934            h.base = TCG_TMP0;
1935            h.index = TCG_REG_NONE;
1936            h.disp = 0;
1937        }
1938        tcg_out_insn_RXY(s, insn, d1, h.base, h.index, h.disp);
1939        tcg_out_insn_RXY(s, insn, d2, h.base, h.index, h.disp + 8);
1940    }
1941    if (l2) {
1942        tcg_out_label(s, l2);
1943    }
1944
1945    if (ldst) {
1946        ldst->type = TCG_TYPE_I128;
1947        ldst->datalo_reg = datalo;
1948        ldst->datahi_reg = datahi;
1949        ldst->raddr = tcg_splitwx_to_rx(s->code_ptr);
1950    }
1951}
1952
1953static void tcg_out_exit_tb(TCGContext *s, uintptr_t a0)
1954{
1955    /* Reuse the zeroing that exists for goto_ptr.  */
1956    if (a0 == 0) {
1957        tgen_gotoi(s, S390_CC_ALWAYS, tcg_code_gen_epilogue);
1958    } else {
1959        tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, a0);
1960        tgen_gotoi(s, S390_CC_ALWAYS, tb_ret_addr);
1961    }
1962}
1963
1964static void tcg_out_goto_tb(TCGContext *s, int which)
1965{
1966    /*
1967     * Branch displacement must be aligned for atomic patching;
1968     * see if we need to add extra nop before branch
1969     */
1970    if (!QEMU_PTR_IS_ALIGNED(s->code_ptr + 1, 4)) {
1971        tcg_out16(s, NOP);
1972    }
1973    tcg_out16(s, RIL_BRCL | (S390_CC_ALWAYS << 4));
1974    set_jmp_insn_offset(s, which);
1975    s->code_ptr += 2;
1976    set_jmp_reset_offset(s, which);
1977}
1978
1979void tb_target_set_jmp_target(const TranslationBlock *tb, int n,
1980                              uintptr_t jmp_rx, uintptr_t jmp_rw)
1981{
1982    if (!HAVE_FACILITY(GEN_INST_EXT)) {
1983        return;
1984    }
1985    /* patch the branch destination */
1986    uintptr_t addr = tb->jmp_target_addr[n];
1987    intptr_t disp = addr - (jmp_rx - 2);
1988    qatomic_set((int32_t *)jmp_rw, disp / 2);
1989    /* no need to flush icache explicitly */
1990}
1991
1992# define OP_32_64(x) \
1993        case glue(glue(INDEX_op_,x),_i32): \
1994        case glue(glue(INDEX_op_,x),_i64)
1995
1996static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
1997                              const TCGArg args[TCG_MAX_OP_ARGS],
1998                              const int const_args[TCG_MAX_OP_ARGS])
1999{
2000    S390Opcode op, op2;
2001    TCGArg a0, a1, a2;
2002
2003    switch (opc) {
2004    case INDEX_op_goto_ptr:
2005        a0 = args[0];
2006        tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, a0);
2007        break;
2008
2009    OP_32_64(ld8u):
2010        /* ??? LLC (RXY format) is only present with the extended-immediate
2011           facility, whereas LLGC is always present.  */
2012        tcg_out_mem(s, 0, RXY_LLGC, args[0], args[1], TCG_REG_NONE, args[2]);
2013        break;
2014
2015    OP_32_64(ld8s):
2016        /* ??? LB is no smaller than LGB, so no point to using it.  */
2017        tcg_out_mem(s, 0, RXY_LGB, args[0], args[1], TCG_REG_NONE, args[2]);
2018        break;
2019
2020    OP_32_64(ld16u):
2021        /* ??? LLH (RXY format) is only present with the extended-immediate
2022           facility, whereas LLGH is always present.  */
2023        tcg_out_mem(s, 0, RXY_LLGH, args[0], args[1], TCG_REG_NONE, args[2]);
2024        break;
2025
2026    case INDEX_op_ld16s_i32:
2027        tcg_out_mem(s, RX_LH, RXY_LHY, args[0], args[1], TCG_REG_NONE, args[2]);
2028        break;
2029
2030    case INDEX_op_ld_i32:
2031        tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
2032        break;
2033
2034    OP_32_64(st8):
2035        tcg_out_mem(s, RX_STC, RXY_STCY, args[0], args[1],
2036                    TCG_REG_NONE, args[2]);
2037        break;
2038
2039    OP_32_64(st16):
2040        tcg_out_mem(s, RX_STH, RXY_STHY, args[0], args[1],
2041                    TCG_REG_NONE, args[2]);
2042        break;
2043
2044    case INDEX_op_st_i32:
2045        tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
2046        break;
2047
2048    case INDEX_op_add_i32:
2049        a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2050        if (const_args[2]) {
2051        do_addi_32:
2052            if (a0 == a1) {
2053                if (a2 == (int16_t)a2) {
2054                    tcg_out_insn(s, RI, AHI, a0, a2);
2055                    break;
2056                }
2057                tcg_out_insn(s, RIL, AFI, a0, a2);
2058                break;
2059            }
2060            tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
2061        } else if (a0 == a1) {
2062            tcg_out_insn(s, RR, AR, a0, a2);
2063        } else {
2064            tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
2065        }
2066        break;
2067    case INDEX_op_sub_i32:
2068        a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2069        if (const_args[2]) {
2070            a2 = -a2;
2071            goto do_addi_32;
2072        } else if (a0 == a1) {
2073            tcg_out_insn(s, RR, SR, a0, a2);
2074        } else {
2075            tcg_out_insn(s, RRFa, SRK, a0, a1, a2);
2076        }
2077        break;
2078
2079    case INDEX_op_and_i32:
2080        a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2081        if (const_args[2]) {
2082            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2083            tgen_andi(s, TCG_TYPE_I32, a0, a2);
2084        } else if (a0 == a1) {
2085            tcg_out_insn(s, RR, NR, a0, a2);
2086        } else {
2087            tcg_out_insn(s, RRFa, NRK, a0, a1, a2);
2088        }
2089        break;
2090    case INDEX_op_or_i32:
2091        a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2092        if (const_args[2]) {
2093            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2094            tgen_ori(s, a0, a2);
2095        } else if (a0 == a1) {
2096            tcg_out_insn(s, RR, OR, a0, a2);
2097        } else {
2098            tcg_out_insn(s, RRFa, ORK, a0, a1, a2);
2099        }
2100        break;
2101    case INDEX_op_xor_i32:
2102        a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2103        if (const_args[2]) {
2104            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2105            tcg_out_insn(s, RIL, XILF, a0, a2);
2106        } else if (a0 == a1) {
2107            tcg_out_insn(s, RR, XR, args[0], args[2]);
2108        } else {
2109            tcg_out_insn(s, RRFa, XRK, a0, a1, a2);
2110        }
2111        break;
2112
2113    case INDEX_op_andc_i32:
2114        a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2115        if (const_args[2]) {
2116            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2117            tgen_andi(s, TCG_TYPE_I32, a0, (uint32_t)~a2);
2118	} else {
2119            tcg_out_insn(s, RRFa, NCRK, a0, a1, a2);
2120	}
2121        break;
2122    case INDEX_op_orc_i32:
2123        a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2124        if (const_args[2]) {
2125            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2126            tgen_ori(s, a0, (uint32_t)~a2);
2127        } else {
2128            tcg_out_insn(s, RRFa, OCRK, a0, a1, a2);
2129        }
2130        break;
2131    case INDEX_op_eqv_i32:
2132        a0 = args[0], a1 = args[1], a2 = (uint32_t)args[2];
2133        if (const_args[2]) {
2134            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2135            tcg_out_insn(s, RIL, XILF, a0, ~a2);
2136        } else {
2137            tcg_out_insn(s, RRFa, NXRK, a0, a1, a2);
2138        }
2139        break;
2140    case INDEX_op_nand_i32:
2141        tcg_out_insn(s, RRFa, NNRK, args[0], args[1], args[2]);
2142        break;
2143    case INDEX_op_nor_i32:
2144        tcg_out_insn(s, RRFa, NORK, args[0], args[1], args[2]);
2145        break;
2146
2147    case INDEX_op_neg_i32:
2148        tcg_out_insn(s, RR, LCR, args[0], args[1]);
2149        break;
2150    case INDEX_op_not_i32:
2151        tcg_out_insn(s, RRFa, NORK, args[0], args[1], args[1]);
2152        break;
2153
2154    case INDEX_op_mul_i32:
2155        a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2156        if (const_args[2]) {
2157            tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
2158            if (a2 == (int16_t)a2) {
2159                tcg_out_insn(s, RI, MHI, a0, a2);
2160            } else {
2161                tcg_out_insn(s, RIL, MSFI, a0, a2);
2162            }
2163        } else if (a0 == a1) {
2164            tcg_out_insn(s, RRE, MSR, a0, a2);
2165        } else {
2166            tcg_out_insn(s, RRFa, MSRKC, a0, a1, a2);
2167        }
2168        break;
2169
2170    case INDEX_op_div2_i32:
2171        tcg_debug_assert(args[0] == args[2]);
2172        tcg_debug_assert(args[1] == args[3]);
2173        tcg_debug_assert((args[1] & 1) == 0);
2174        tcg_debug_assert(args[0] == args[1] + 1);
2175        tcg_out_insn(s, RR, DR, args[1], args[4]);
2176        break;
2177    case INDEX_op_divu2_i32:
2178        tcg_debug_assert(args[0] == args[2]);
2179        tcg_debug_assert(args[1] == args[3]);
2180        tcg_debug_assert((args[1] & 1) == 0);
2181        tcg_debug_assert(args[0] == args[1] + 1);
2182        tcg_out_insn(s, RRE, DLR, args[1], args[4]);
2183        break;
2184
2185    case INDEX_op_shl_i32:
2186        op = RS_SLL;
2187        op2 = RSY_SLLK;
2188    do_shift32:
2189        a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
2190        if (a0 == a1) {
2191            if (const_args[2]) {
2192                tcg_out_sh32(s, op, a0, TCG_REG_NONE, a2);
2193            } else {
2194                tcg_out_sh32(s, op, a0, a2, 0);
2195            }
2196        } else {
2197            /* Using tcg_out_sh64 here for the format; it is a 32-bit shift.  */
2198            if (const_args[2]) {
2199                tcg_out_sh64(s, op2, a0, a1, TCG_REG_NONE, a2);
2200            } else {
2201                tcg_out_sh64(s, op2, a0, a1, a2, 0);
2202            }
2203        }
2204        break;
2205    case INDEX_op_shr_i32:
2206        op = RS_SRL;
2207        op2 = RSY_SRLK;
2208        goto do_shift32;
2209    case INDEX_op_sar_i32:
2210        op = RS_SRA;
2211        op2 = RSY_SRAK;
2212        goto do_shift32;
2213
2214    case INDEX_op_rotl_i32:
2215        /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol.  */
2216        if (const_args[2]) {
2217            tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_REG_NONE, args[2]);
2218        } else {
2219            tcg_out_sh64(s, RSY_RLL, args[0], args[1], args[2], 0);
2220        }
2221        break;
2222    case INDEX_op_rotr_i32:
2223        if (const_args[2]) {
2224            tcg_out_sh64(s, RSY_RLL, args[0], args[1],
2225                         TCG_REG_NONE, (32 - args[2]) & 31);
2226        } else {
2227            tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
2228            tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_TMP0, 0);
2229        }
2230        break;
2231
2232    case INDEX_op_bswap16_i32:
2233        a0 = args[0], a1 = args[1], a2 = args[2];
2234        tcg_out_insn(s, RRE, LRVR, a0, a1);
2235        if (a2 & TCG_BSWAP_OS) {
2236            tcg_out_sh32(s, RS_SRA, a0, TCG_REG_NONE, 16);
2237        } else {
2238            tcg_out_sh32(s, RS_SRL, a0, TCG_REG_NONE, 16);
2239        }
2240        break;
2241    case INDEX_op_bswap16_i64:
2242        a0 = args[0], a1 = args[1], a2 = args[2];
2243        tcg_out_insn(s, RRE, LRVGR, a0, a1);
2244        if (a2 & TCG_BSWAP_OS) {
2245            tcg_out_sh64(s, RSY_SRAG, a0, a0, TCG_REG_NONE, 48);
2246        } else {
2247            tcg_out_sh64(s, RSY_SRLG, a0, a0, TCG_REG_NONE, 48);
2248        }
2249        break;
2250
2251    case INDEX_op_bswap32_i32:
2252        tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
2253        break;
2254    case INDEX_op_bswap32_i64:
2255        a0 = args[0], a1 = args[1], a2 = args[2];
2256        tcg_out_insn(s, RRE, LRVR, a0, a1);
2257        if (a2 & TCG_BSWAP_OS) {
2258            tcg_out_ext32s(s, a0, a0);
2259        } else if ((a2 & (TCG_BSWAP_IZ | TCG_BSWAP_OZ)) == TCG_BSWAP_OZ) {
2260            tcg_out_ext32u(s, a0, a0);
2261        }
2262        break;
2263
2264    case INDEX_op_add2_i32:
2265        if (const_args[4]) {
2266            tcg_out_insn(s, RIL, ALFI, args[0], args[4]);
2267        } else {
2268            tcg_out_insn(s, RR, ALR, args[0], args[4]);
2269        }
2270        tcg_out_insn(s, RRE, ALCR, args[1], args[5]);
2271        break;
2272    case INDEX_op_sub2_i32:
2273        if (const_args[4]) {
2274            tcg_out_insn(s, RIL, SLFI, args[0], args[4]);
2275        } else {
2276            tcg_out_insn(s, RR, SLR, args[0], args[4]);
2277        }
2278        tcg_out_insn(s, RRE, SLBR, args[1], args[5]);
2279        break;
2280
2281    case INDEX_op_br:
2282        tgen_branch(s, S390_CC_ALWAYS, arg_label(args[0]));
2283        break;
2284
2285    case INDEX_op_brcond_i32:
2286        tgen_brcond(s, TCG_TYPE_I32, args[2], args[0],
2287                    args[1], const_args[1], arg_label(args[3]));
2288        break;
2289    case INDEX_op_setcond_i32:
2290        tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1],
2291                     args[2], const_args[2]);
2292        break;
2293    case INDEX_op_movcond_i32:
2294        tgen_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1],
2295                     args[2], const_args[2], args[3], const_args[3], args[4]);
2296        break;
2297
2298    case INDEX_op_qemu_ld_a32_i32:
2299    case INDEX_op_qemu_ld_a64_i32:
2300        tcg_out_qemu_ld(s, args[0], args[1], args[2], TCG_TYPE_I32);
2301        break;
2302    case INDEX_op_qemu_ld_a32_i64:
2303    case INDEX_op_qemu_ld_a64_i64:
2304        tcg_out_qemu_ld(s, args[0], args[1], args[2], TCG_TYPE_I64);
2305        break;
2306    case INDEX_op_qemu_st_a32_i32:
2307    case INDEX_op_qemu_st_a64_i32:
2308        tcg_out_qemu_st(s, args[0], args[1], args[2], TCG_TYPE_I32);
2309        break;
2310    case INDEX_op_qemu_st_a32_i64:
2311    case INDEX_op_qemu_st_a64_i64:
2312        tcg_out_qemu_st(s, args[0], args[1], args[2], TCG_TYPE_I64);
2313        break;
2314    case INDEX_op_qemu_ld_a32_i128:
2315    case INDEX_op_qemu_ld_a64_i128:
2316        tcg_out_qemu_ldst_i128(s, args[0], args[1], args[2], args[3], true);
2317        break;
2318    case INDEX_op_qemu_st_a32_i128:
2319    case INDEX_op_qemu_st_a64_i128:
2320        tcg_out_qemu_ldst_i128(s, args[0], args[1], args[2], args[3], false);
2321        break;
2322
2323    case INDEX_op_ld16s_i64:
2324        tcg_out_mem(s, 0, RXY_LGH, args[0], args[1], TCG_REG_NONE, args[2]);
2325        break;
2326    case INDEX_op_ld32u_i64:
2327        tcg_out_mem(s, 0, RXY_LLGF, args[0], args[1], TCG_REG_NONE, args[2]);
2328        break;
2329    case INDEX_op_ld32s_i64:
2330        tcg_out_mem(s, 0, RXY_LGF, args[0], args[1], TCG_REG_NONE, args[2]);
2331        break;
2332    case INDEX_op_ld_i64:
2333        tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]);
2334        break;
2335
2336    case INDEX_op_st32_i64:
2337        tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
2338        break;
2339    case INDEX_op_st_i64:
2340        tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]);
2341        break;
2342
2343    case INDEX_op_add_i64:
2344        a0 = args[0], a1 = args[1], a2 = args[2];
2345        if (const_args[2]) {
2346        do_addi_64:
2347            if (a0 == a1) {
2348                if (a2 == (int16_t)a2) {
2349                    tcg_out_insn(s, RI, AGHI, a0, a2);
2350                    break;
2351                }
2352                if (a2 == (int32_t)a2) {
2353                    tcg_out_insn(s, RIL, AGFI, a0, a2);
2354                    break;
2355                }
2356                if (a2 == (uint32_t)a2) {
2357                    tcg_out_insn(s, RIL, ALGFI, a0, a2);
2358                    break;
2359                }
2360                if (-a2 == (uint32_t)-a2) {
2361                    tcg_out_insn(s, RIL, SLGFI, a0, -a2);
2362                    break;
2363                }
2364            }
2365            tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
2366        } else if (a0 == a1) {
2367            tcg_out_insn(s, RRE, AGR, a0, a2);
2368        } else {
2369            tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
2370        }
2371        break;
2372    case INDEX_op_sub_i64:
2373        a0 = args[0], a1 = args[1], a2 = args[2];
2374        if (const_args[2]) {
2375            a2 = -a2;
2376            goto do_addi_64;
2377        } else {
2378            tcg_out_insn(s, RRFa, SGRK, a0, a1, a2);
2379        }
2380        break;
2381
2382    case INDEX_op_and_i64:
2383        a0 = args[0], a1 = args[1], a2 = args[2];
2384        if (const_args[2]) {
2385            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2386            tgen_andi(s, TCG_TYPE_I64, args[0], args[2]);
2387        } else {
2388            tcg_out_insn(s, RRFa, NGRK, a0, a1, a2);
2389        }
2390        break;
2391    case INDEX_op_or_i64:
2392        a0 = args[0], a1 = args[1], a2 = args[2];
2393        if (const_args[2]) {
2394            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2395            tgen_ori(s, a0, a2);
2396        } else {
2397            tcg_out_insn(s, RRFa, OGRK, a0, a1, a2);
2398        }
2399        break;
2400    case INDEX_op_xor_i64:
2401        a0 = args[0], a1 = args[1], a2 = args[2];
2402        if (const_args[2]) {
2403            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2404            tgen_xori(s, a0, a2);
2405        } else {
2406            tcg_out_insn(s, RRFa, XGRK, a0, a1, a2);
2407        }
2408        break;
2409
2410    case INDEX_op_andc_i64:
2411        a0 = args[0], a1 = args[1], a2 = args[2];
2412        if (const_args[2]) {
2413            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2414            tgen_andi(s, TCG_TYPE_I64, a0, ~a2);
2415        } else {
2416            tcg_out_insn(s, RRFa, NCGRK, a0, a1, a2);
2417        }
2418        break;
2419    case INDEX_op_orc_i64:
2420        a0 = args[0], a1 = args[1], a2 = args[2];
2421        if (const_args[2]) {
2422            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2423            tgen_ori(s, a0, ~a2);
2424        } else {
2425            tcg_out_insn(s, RRFa, OCGRK, a0, a1, a2);
2426        }
2427        break;
2428    case INDEX_op_eqv_i64:
2429        a0 = args[0], a1 = args[1], a2 = args[2];
2430        if (const_args[2]) {
2431            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2432            tgen_xori(s, a0, ~a2);
2433        } else {
2434            tcg_out_insn(s, RRFa, NXGRK, a0, a1, a2);
2435        }
2436        break;
2437    case INDEX_op_nand_i64:
2438        tcg_out_insn(s, RRFa, NNGRK, args[0], args[1], args[2]);
2439        break;
2440    case INDEX_op_nor_i64:
2441        tcg_out_insn(s, RRFa, NOGRK, args[0], args[1], args[2]);
2442        break;
2443
2444    case INDEX_op_neg_i64:
2445        tcg_out_insn(s, RRE, LCGR, args[0], args[1]);
2446        break;
2447    case INDEX_op_not_i64:
2448        tcg_out_insn(s, RRFa, NOGRK, args[0], args[1], args[1]);
2449        break;
2450    case INDEX_op_bswap64_i64:
2451        tcg_out_insn(s, RRE, LRVGR, args[0], args[1]);
2452        break;
2453
2454    case INDEX_op_mul_i64:
2455        a0 = args[0], a1 = args[1], a2 = args[2];
2456        if (const_args[2]) {
2457            tcg_out_mov(s, TCG_TYPE_I64, a0, a1);
2458            if (a2 == (int16_t)a2) {
2459                tcg_out_insn(s, RI, MGHI, a0, a2);
2460            } else {
2461                tcg_out_insn(s, RIL, MSGFI, a0, a2);
2462            }
2463        } else if (a0 == a1) {
2464            tcg_out_insn(s, RRE, MSGR, a0, a2);
2465        } else {
2466            tcg_out_insn(s, RRFa, MSGRKC, a0, a1, a2);
2467        }
2468        break;
2469
2470    case INDEX_op_div2_i64:
2471        /*
2472         * ??? We get an unnecessary sign-extension of the dividend
2473         * into op0 with this definition, but as we do in fact always
2474         * produce both quotient and remainder using INDEX_op_div_i64
2475         * instead requires jumping through even more hoops.
2476         */
2477        tcg_debug_assert(args[0] == args[2]);
2478        tcg_debug_assert(args[1] == args[3]);
2479        tcg_debug_assert((args[1] & 1) == 0);
2480        tcg_debug_assert(args[0] == args[1] + 1);
2481        tcg_out_insn(s, RRE, DSGR, args[1], args[4]);
2482        break;
2483    case INDEX_op_divu2_i64:
2484        tcg_debug_assert(args[0] == args[2]);
2485        tcg_debug_assert(args[1] == args[3]);
2486        tcg_debug_assert((args[1] & 1) == 0);
2487        tcg_debug_assert(args[0] == args[1] + 1);
2488        tcg_out_insn(s, RRE, DLGR, args[1], args[4]);
2489        break;
2490    case INDEX_op_mulu2_i64:
2491        tcg_debug_assert(args[0] == args[2]);
2492        tcg_debug_assert((args[1] & 1) == 0);
2493        tcg_debug_assert(args[0] == args[1] + 1);
2494        tcg_out_insn(s, RRE, MLGR, args[1], args[3]);
2495        break;
2496    case INDEX_op_muls2_i64:
2497        tcg_debug_assert((args[1] & 1) == 0);
2498        tcg_debug_assert(args[0] == args[1] + 1);
2499        tcg_out_insn(s, RRFa, MGRK, args[1], args[2], args[3]);
2500        break;
2501
2502    case INDEX_op_shl_i64:
2503        op = RSY_SLLG;
2504    do_shift64:
2505        if (const_args[2]) {
2506            tcg_out_sh64(s, op, args[0], args[1], TCG_REG_NONE, args[2]);
2507        } else {
2508            tcg_out_sh64(s, op, args[0], args[1], args[2], 0);
2509        }
2510        break;
2511    case INDEX_op_shr_i64:
2512        op = RSY_SRLG;
2513        goto do_shift64;
2514    case INDEX_op_sar_i64:
2515        op = RSY_SRAG;
2516        goto do_shift64;
2517
2518    case INDEX_op_rotl_i64:
2519        if (const_args[2]) {
2520            tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
2521                         TCG_REG_NONE, args[2]);
2522        } else {
2523            tcg_out_sh64(s, RSY_RLLG, args[0], args[1], args[2], 0);
2524        }
2525        break;
2526    case INDEX_op_rotr_i64:
2527        if (const_args[2]) {
2528            tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
2529                         TCG_REG_NONE, (64 - args[2]) & 63);
2530        } else {
2531            /* We can use the smaller 32-bit negate because only the
2532               low 6 bits are examined for the rotate.  */
2533            tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
2534            tcg_out_sh64(s, RSY_RLLG, args[0], args[1], TCG_TMP0, 0);
2535        }
2536        break;
2537
2538    case INDEX_op_add2_i64:
2539        if (const_args[4]) {
2540            if ((int64_t)args[4] >= 0) {
2541                tcg_out_insn(s, RIL, ALGFI, args[0], args[4]);
2542            } else {
2543                tcg_out_insn(s, RIL, SLGFI, args[0], -args[4]);
2544            }
2545        } else {
2546            tcg_out_insn(s, RRE, ALGR, args[0], args[4]);
2547        }
2548        tcg_out_insn(s, RRE, ALCGR, args[1], args[5]);
2549        break;
2550    case INDEX_op_sub2_i64:
2551        if (const_args[4]) {
2552            if ((int64_t)args[4] >= 0) {
2553                tcg_out_insn(s, RIL, SLGFI, args[0], args[4]);
2554            } else {
2555                tcg_out_insn(s, RIL, ALGFI, args[0], -args[4]);
2556            }
2557        } else {
2558            tcg_out_insn(s, RRE, SLGR, args[0], args[4]);
2559        }
2560        tcg_out_insn(s, RRE, SLBGR, args[1], args[5]);
2561        break;
2562
2563    case INDEX_op_brcond_i64:
2564        tgen_brcond(s, TCG_TYPE_I64, args[2], args[0],
2565                    args[1], const_args[1], arg_label(args[3]));
2566        break;
2567    case INDEX_op_setcond_i64:
2568        tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1],
2569                     args[2], const_args[2]);
2570        break;
2571    case INDEX_op_movcond_i64:
2572        tgen_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1],
2573                     args[2], const_args[2], args[3], const_args[3], args[4]);
2574        break;
2575
2576    OP_32_64(deposit):
2577        a0 = args[0], a1 = args[1], a2 = args[2];
2578        if (const_args[1]) {
2579            tgen_deposit(s, a0, a2, args[3], args[4], 1);
2580        } else {
2581            /* Since we can't support "0Z" as a constraint, we allow a1 in
2582               any register.  Fix things up as if a matching constraint.  */
2583            if (a0 != a1) {
2584                TCGType type = (opc == INDEX_op_deposit_i64);
2585                if (a0 == a2) {
2586                    tcg_out_mov(s, type, TCG_TMP0, a2);
2587                    a2 = TCG_TMP0;
2588                }
2589                tcg_out_mov(s, type, a0, a1);
2590            }
2591            tgen_deposit(s, a0, a2, args[3], args[4], 0);
2592        }
2593        break;
2594
2595    OP_32_64(extract):
2596        tgen_extract(s, args[0], args[1], args[2], args[3]);
2597        break;
2598
2599    case INDEX_op_clz_i64:
2600        tgen_clz(s, args[0], args[1], args[2], const_args[2]);
2601        break;
2602
2603    case INDEX_op_ctpop_i32:
2604        tgen_ctpop(s, TCG_TYPE_I32, args[0], args[1]);
2605        break;
2606    case INDEX_op_ctpop_i64:
2607        tgen_ctpop(s, TCG_TYPE_I64, args[0], args[1]);
2608        break;
2609
2610    case INDEX_op_mb:
2611        /* The host memory model is quite strong, we simply need to
2612           serialize the instruction stream.  */
2613        if (args[0] & TCG_MO_ST_LD) {
2614            /* fast-bcr-serialization facility (45) is present */
2615            tcg_out_insn(s, RR, BCR, 14, 0);
2616        }
2617        break;
2618
2619    case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
2620    case INDEX_op_mov_i64:
2621    case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
2622    case INDEX_op_exit_tb:  /* Always emitted via tcg_out_exit_tb.  */
2623    case INDEX_op_goto_tb:  /* Always emitted via tcg_out_goto_tb.  */
2624    case INDEX_op_ext8s_i32:  /* Always emitted via tcg_reg_alloc_op.  */
2625    case INDEX_op_ext8s_i64:
2626    case INDEX_op_ext8u_i32:
2627    case INDEX_op_ext8u_i64:
2628    case INDEX_op_ext16s_i32:
2629    case INDEX_op_ext16s_i64:
2630    case INDEX_op_ext16u_i32:
2631    case INDEX_op_ext16u_i64:
2632    case INDEX_op_ext32s_i64:
2633    case INDEX_op_ext32u_i64:
2634    case INDEX_op_ext_i32_i64:
2635    case INDEX_op_extu_i32_i64:
2636    case INDEX_op_extrl_i64_i32:
2637    default:
2638        g_assert_not_reached();
2639    }
2640}
2641
2642static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
2643                            TCGReg dst, TCGReg src)
2644{
2645    if (is_general_reg(src)) {
2646        /* Replicate general register into two MO_64. */
2647        tcg_out_insn(s, VRRf, VLVGP, dst, src, src);
2648        if (vece == MO_64) {
2649            return true;
2650        }
2651        src = dst;
2652    }
2653
2654    /*
2655     * Recall that the "standard" integer, within a vector, is the
2656     * rightmost element of the leftmost doubleword, a-la VLLEZ.
2657     */
2658    tcg_out_insn(s, VRIc, VREP, dst, (8 >> vece) - 1, src, vece);
2659    return true;
2660}
2661
2662static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
2663                             TCGReg dst, TCGReg base, intptr_t offset)
2664{
2665    tcg_out_vrx_mem(s, VRX_VLREP, dst, base, TCG_REG_NONE, offset, vece);
2666    return true;
2667}
2668
2669static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
2670                             TCGReg dst, int64_t val)
2671{
2672    int i, mask, msb, lsb;
2673
2674    /* Look for int16_t elements.  */
2675    if (vece <= MO_16 ||
2676        (vece == MO_32 ? (int32_t)val : val) == (int16_t)val) {
2677        tcg_out_insn(s, VRIa, VREPI, dst, val, vece);
2678        return;
2679    }
2680
2681    /* Look for bit masks.  */
2682    if (vece == MO_32) {
2683        if (risbg_mask((int32_t)val)) {
2684            /* Handle wraparound by swapping msb and lsb.  */
2685            if ((val & 0x80000001u) == 0x80000001u) {
2686                msb = 32 - ctz32(~val);
2687                lsb = clz32(~val) - 1;
2688            } else {
2689                msb = clz32(val);
2690                lsb = 31 - ctz32(val);
2691            }
2692            tcg_out_insn(s, VRIb, VGM, dst, msb, lsb, MO_32);
2693            return;
2694        }
2695    } else {
2696        if (risbg_mask(val)) {
2697            /* Handle wraparound by swapping msb and lsb.  */
2698            if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
2699                /* Handle wraparound by swapping msb and lsb.  */
2700                msb = 64 - ctz64(~val);
2701                lsb = clz64(~val) - 1;
2702            } else {
2703                msb = clz64(val);
2704                lsb = 63 - ctz64(val);
2705            }
2706            tcg_out_insn(s, VRIb, VGM, dst, msb, lsb, MO_64);
2707            return;
2708        }
2709    }
2710
2711    /* Look for all bytes 0x00 or 0xff.  */
2712    for (i = mask = 0; i < 8; i++) {
2713        uint8_t byte = val >> (i * 8);
2714        if (byte == 0xff) {
2715            mask |= 1 << i;
2716        } else if (byte != 0) {
2717            break;
2718        }
2719    }
2720    if (i == 8) {
2721        tcg_out_insn(s, VRIa, VGBM, dst, mask * 0x0101, 0);
2722        return;
2723    }
2724
2725    /* Otherwise, stuff it in the constant pool.  */
2726    tcg_out_insn(s, RIL, LARL, TCG_TMP0, 0);
2727    new_pool_label(s, val, R_390_PC32DBL, s->code_ptr - 2, 2);
2728    tcg_out_insn(s, VRX, VLREP, dst, TCG_TMP0, TCG_REG_NONE, 0, MO_64);
2729}
2730
2731static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
2732                           unsigned vecl, unsigned vece,
2733                           const TCGArg args[TCG_MAX_OP_ARGS],
2734                           const int const_args[TCG_MAX_OP_ARGS])
2735{
2736    TCGType type = vecl + TCG_TYPE_V64;
2737    TCGArg a0 = args[0], a1 = args[1], a2 = args[2];
2738
2739    switch (opc) {
2740    case INDEX_op_ld_vec:
2741        tcg_out_ld(s, type, a0, a1, a2);
2742        break;
2743    case INDEX_op_st_vec:
2744        tcg_out_st(s, type, a0, a1, a2);
2745        break;
2746    case INDEX_op_dupm_vec:
2747        tcg_out_dupm_vec(s, type, vece, a0, a1, a2);
2748        break;
2749
2750    case INDEX_op_abs_vec:
2751        tcg_out_insn(s, VRRa, VLP, a0, a1, vece);
2752        break;
2753    case INDEX_op_neg_vec:
2754        tcg_out_insn(s, VRRa, VLC, a0, a1, vece);
2755        break;
2756    case INDEX_op_not_vec:
2757        tcg_out_insn(s, VRRc, VNO, a0, a1, a1, 0);
2758        break;
2759
2760    case INDEX_op_add_vec:
2761        tcg_out_insn(s, VRRc, VA, a0, a1, a2, vece);
2762        break;
2763    case INDEX_op_sub_vec:
2764        tcg_out_insn(s, VRRc, VS, a0, a1, a2, vece);
2765        break;
2766    case INDEX_op_and_vec:
2767        tcg_out_insn(s, VRRc, VN, a0, a1, a2, 0);
2768        break;
2769    case INDEX_op_andc_vec:
2770        tcg_out_insn(s, VRRc, VNC, a0, a1, a2, 0);
2771        break;
2772    case INDEX_op_mul_vec:
2773        tcg_out_insn(s, VRRc, VML, a0, a1, a2, vece);
2774        break;
2775    case INDEX_op_or_vec:
2776        tcg_out_insn(s, VRRc, VO, a0, a1, a2, 0);
2777        break;
2778    case INDEX_op_orc_vec:
2779        tcg_out_insn(s, VRRc, VOC, a0, a1, a2, 0);
2780        break;
2781    case INDEX_op_xor_vec:
2782        tcg_out_insn(s, VRRc, VX, a0, a1, a2, 0);
2783        break;
2784    case INDEX_op_nand_vec:
2785        tcg_out_insn(s, VRRc, VNN, a0, a1, a2, 0);
2786        break;
2787    case INDEX_op_nor_vec:
2788        tcg_out_insn(s, VRRc, VNO, a0, a1, a2, 0);
2789        break;
2790    case INDEX_op_eqv_vec:
2791        tcg_out_insn(s, VRRc, VNX, a0, a1, a2, 0);
2792        break;
2793
2794    case INDEX_op_shli_vec:
2795        tcg_out_insn(s, VRSa, VESL, a0, a2, TCG_REG_NONE, a1, vece);
2796        break;
2797    case INDEX_op_shri_vec:
2798        tcg_out_insn(s, VRSa, VESRL, a0, a2, TCG_REG_NONE, a1, vece);
2799        break;
2800    case INDEX_op_sari_vec:
2801        tcg_out_insn(s, VRSa, VESRA, a0, a2, TCG_REG_NONE, a1, vece);
2802        break;
2803    case INDEX_op_rotli_vec:
2804        tcg_out_insn(s, VRSa, VERLL, a0, a2, TCG_REG_NONE, a1, vece);
2805        break;
2806    case INDEX_op_shls_vec:
2807        tcg_out_insn(s, VRSa, VESL, a0, 0, a2, a1, vece);
2808        break;
2809    case INDEX_op_shrs_vec:
2810        tcg_out_insn(s, VRSa, VESRL, a0, 0, a2, a1, vece);
2811        break;
2812    case INDEX_op_sars_vec:
2813        tcg_out_insn(s, VRSa, VESRA, a0, 0, a2, a1, vece);
2814        break;
2815    case INDEX_op_rotls_vec:
2816        tcg_out_insn(s, VRSa, VERLL, a0, 0, a2, a1, vece);
2817        break;
2818    case INDEX_op_shlv_vec:
2819        tcg_out_insn(s, VRRc, VESLV, a0, a1, a2, vece);
2820        break;
2821    case INDEX_op_shrv_vec:
2822        tcg_out_insn(s, VRRc, VESRLV, a0, a1, a2, vece);
2823        break;
2824    case INDEX_op_sarv_vec:
2825        tcg_out_insn(s, VRRc, VESRAV, a0, a1, a2, vece);
2826        break;
2827    case INDEX_op_rotlv_vec:
2828        tcg_out_insn(s, VRRc, VERLLV, a0, a1, a2, vece);
2829        break;
2830
2831    case INDEX_op_smin_vec:
2832        tcg_out_insn(s, VRRc, VMN, a0, a1, a2, vece);
2833        break;
2834    case INDEX_op_smax_vec:
2835        tcg_out_insn(s, VRRc, VMX, a0, a1, a2, vece);
2836        break;
2837    case INDEX_op_umin_vec:
2838        tcg_out_insn(s, VRRc, VMNL, a0, a1, a2, vece);
2839        break;
2840    case INDEX_op_umax_vec:
2841        tcg_out_insn(s, VRRc, VMXL, a0, a1, a2, vece);
2842        break;
2843
2844    case INDEX_op_bitsel_vec:
2845        tcg_out_insn(s, VRRe, VSEL, a0, a2, args[3], a1);
2846        break;
2847
2848    case INDEX_op_cmp_vec:
2849        switch ((TCGCond)args[3]) {
2850        case TCG_COND_EQ:
2851            tcg_out_insn(s, VRRc, VCEQ, a0, a1, a2, vece);
2852            break;
2853        case TCG_COND_GT:
2854            tcg_out_insn(s, VRRc, VCH, a0, a1, a2, vece);
2855            break;
2856        case TCG_COND_GTU:
2857            tcg_out_insn(s, VRRc, VCHL, a0, a1, a2, vece);
2858            break;
2859        default:
2860            g_assert_not_reached();
2861        }
2862        break;
2863
2864    case INDEX_op_s390_vuph_vec:
2865        tcg_out_insn(s, VRRa, VUPH, a0, a1, vece);
2866        break;
2867    case INDEX_op_s390_vupl_vec:
2868        tcg_out_insn(s, VRRa, VUPL, a0, a1, vece);
2869        break;
2870    case INDEX_op_s390_vpks_vec:
2871        tcg_out_insn(s, VRRc, VPKS, a0, a1, a2, vece);
2872        break;
2873
2874    case INDEX_op_mov_vec:   /* Always emitted via tcg_out_mov.  */
2875    case INDEX_op_dup_vec:   /* Always emitted via tcg_out_dup_vec.  */
2876    default:
2877        g_assert_not_reached();
2878    }
2879}
2880
2881int tcg_can_emit_vec_op(TCGOpcode opc, TCGType type, unsigned vece)
2882{
2883    switch (opc) {
2884    case INDEX_op_abs_vec:
2885    case INDEX_op_add_vec:
2886    case INDEX_op_and_vec:
2887    case INDEX_op_andc_vec:
2888    case INDEX_op_bitsel_vec:
2889    case INDEX_op_eqv_vec:
2890    case INDEX_op_nand_vec:
2891    case INDEX_op_neg_vec:
2892    case INDEX_op_nor_vec:
2893    case INDEX_op_not_vec:
2894    case INDEX_op_or_vec:
2895    case INDEX_op_orc_vec:
2896    case INDEX_op_rotli_vec:
2897    case INDEX_op_rotls_vec:
2898    case INDEX_op_rotlv_vec:
2899    case INDEX_op_sari_vec:
2900    case INDEX_op_sars_vec:
2901    case INDEX_op_sarv_vec:
2902    case INDEX_op_shli_vec:
2903    case INDEX_op_shls_vec:
2904    case INDEX_op_shlv_vec:
2905    case INDEX_op_shri_vec:
2906    case INDEX_op_shrs_vec:
2907    case INDEX_op_shrv_vec:
2908    case INDEX_op_smax_vec:
2909    case INDEX_op_smin_vec:
2910    case INDEX_op_sub_vec:
2911    case INDEX_op_umax_vec:
2912    case INDEX_op_umin_vec:
2913    case INDEX_op_xor_vec:
2914        return 1;
2915    case INDEX_op_cmp_vec:
2916    case INDEX_op_cmpsel_vec:
2917    case INDEX_op_rotrv_vec:
2918        return -1;
2919    case INDEX_op_mul_vec:
2920        return vece < MO_64;
2921    case INDEX_op_ssadd_vec:
2922    case INDEX_op_sssub_vec:
2923        return vece < MO_64 ? -1 : 0;
2924    default:
2925        return 0;
2926    }
2927}
2928
2929static bool expand_vec_cmp_noinv(TCGType type, unsigned vece, TCGv_vec v0,
2930                                 TCGv_vec v1, TCGv_vec v2, TCGCond cond)
2931{
2932    bool need_swap = false, need_inv = false;
2933
2934    switch (cond) {
2935    case TCG_COND_EQ:
2936    case TCG_COND_GT:
2937    case TCG_COND_GTU:
2938        break;
2939    case TCG_COND_NE:
2940    case TCG_COND_LE:
2941    case TCG_COND_LEU:
2942        need_inv = true;
2943        break;
2944    case TCG_COND_LT:
2945    case TCG_COND_LTU:
2946        need_swap = true;
2947        break;
2948    case TCG_COND_GE:
2949    case TCG_COND_GEU:
2950        need_swap = need_inv = true;
2951        break;
2952    default:
2953        g_assert_not_reached();
2954    }
2955
2956    if (need_inv) {
2957        cond = tcg_invert_cond(cond);
2958    }
2959    if (need_swap) {
2960        TCGv_vec t1;
2961        t1 = v1, v1 = v2, v2 = t1;
2962        cond = tcg_swap_cond(cond);
2963    }
2964
2965    vec_gen_4(INDEX_op_cmp_vec, type, vece, tcgv_vec_arg(v0),
2966              tcgv_vec_arg(v1), tcgv_vec_arg(v2), cond);
2967
2968    return need_inv;
2969}
2970
2971static void expand_vec_cmp(TCGType type, unsigned vece, TCGv_vec v0,
2972                           TCGv_vec v1, TCGv_vec v2, TCGCond cond)
2973{
2974    if (expand_vec_cmp_noinv(type, vece, v0, v1, v2, cond)) {
2975        tcg_gen_not_vec(vece, v0, v0);
2976    }
2977}
2978
2979static void expand_vec_cmpsel(TCGType type, unsigned vece, TCGv_vec v0,
2980                              TCGv_vec c1, TCGv_vec c2,
2981                              TCGv_vec v3, TCGv_vec v4, TCGCond cond)
2982{
2983    TCGv_vec t = tcg_temp_new_vec(type);
2984
2985    if (expand_vec_cmp_noinv(type, vece, t, c1, c2, cond)) {
2986        /* Invert the sense of the compare by swapping arguments.  */
2987        tcg_gen_bitsel_vec(vece, v0, t, v4, v3);
2988    } else {
2989        tcg_gen_bitsel_vec(vece, v0, t, v3, v4);
2990    }
2991    tcg_temp_free_vec(t);
2992}
2993
2994static void expand_vec_sat(TCGType type, unsigned vece, TCGv_vec v0,
2995                           TCGv_vec v1, TCGv_vec v2, TCGOpcode add_sub_opc)
2996{
2997    TCGv_vec h1 = tcg_temp_new_vec(type);
2998    TCGv_vec h2 = tcg_temp_new_vec(type);
2999    TCGv_vec l1 = tcg_temp_new_vec(type);
3000    TCGv_vec l2 = tcg_temp_new_vec(type);
3001
3002    tcg_debug_assert (vece < MO_64);
3003
3004    /* Unpack with sign-extension. */
3005    vec_gen_2(INDEX_op_s390_vuph_vec, type, vece,
3006              tcgv_vec_arg(h1), tcgv_vec_arg(v1));
3007    vec_gen_2(INDEX_op_s390_vuph_vec, type, vece,
3008              tcgv_vec_arg(h2), tcgv_vec_arg(v2));
3009
3010    vec_gen_2(INDEX_op_s390_vupl_vec, type, vece,
3011              tcgv_vec_arg(l1), tcgv_vec_arg(v1));
3012    vec_gen_2(INDEX_op_s390_vupl_vec, type, vece,
3013              tcgv_vec_arg(l2), tcgv_vec_arg(v2));
3014
3015    /* Arithmetic on a wider element size. */
3016    vec_gen_3(add_sub_opc, type, vece + 1, tcgv_vec_arg(h1),
3017              tcgv_vec_arg(h1), tcgv_vec_arg(h2));
3018    vec_gen_3(add_sub_opc, type, vece + 1, tcgv_vec_arg(l1),
3019              tcgv_vec_arg(l1), tcgv_vec_arg(l2));
3020
3021    /* Pack with saturation. */
3022    vec_gen_3(INDEX_op_s390_vpks_vec, type, vece + 1,
3023              tcgv_vec_arg(v0), tcgv_vec_arg(h1), tcgv_vec_arg(l1));
3024
3025    tcg_temp_free_vec(h1);
3026    tcg_temp_free_vec(h2);
3027    tcg_temp_free_vec(l1);
3028    tcg_temp_free_vec(l2);
3029}
3030
3031void tcg_expand_vec_op(TCGOpcode opc, TCGType type, unsigned vece,
3032                       TCGArg a0, ...)
3033{
3034    va_list va;
3035    TCGv_vec v0, v1, v2, v3, v4, t0;
3036
3037    va_start(va, a0);
3038    v0 = temp_tcgv_vec(arg_temp(a0));
3039    v1 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3040    v2 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3041
3042    switch (opc) {
3043    case INDEX_op_cmp_vec:
3044        expand_vec_cmp(type, vece, v0, v1, v2, va_arg(va, TCGArg));
3045        break;
3046
3047    case INDEX_op_cmpsel_vec:
3048        v3 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3049        v4 = temp_tcgv_vec(arg_temp(va_arg(va, TCGArg)));
3050        expand_vec_cmpsel(type, vece, v0, v1, v2, v3, v4, va_arg(va, TCGArg));
3051        break;
3052
3053    case INDEX_op_rotrv_vec:
3054        t0 = tcg_temp_new_vec(type);
3055        tcg_gen_neg_vec(vece, t0, v2);
3056        tcg_gen_rotlv_vec(vece, v0, v1, t0);
3057        tcg_temp_free_vec(t0);
3058        break;
3059
3060    case INDEX_op_ssadd_vec:
3061        expand_vec_sat(type, vece, v0, v1, v2, INDEX_op_add_vec);
3062        break;
3063    case INDEX_op_sssub_vec:
3064        expand_vec_sat(type, vece, v0, v1, v2, INDEX_op_sub_vec);
3065        break;
3066
3067    default:
3068        g_assert_not_reached();
3069    }
3070    va_end(va);
3071}
3072
3073static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode op)
3074{
3075    switch (op) {
3076    case INDEX_op_goto_ptr:
3077        return C_O0_I1(r);
3078
3079    case INDEX_op_ld8u_i32:
3080    case INDEX_op_ld8u_i64:
3081    case INDEX_op_ld8s_i32:
3082    case INDEX_op_ld8s_i64:
3083    case INDEX_op_ld16u_i32:
3084    case INDEX_op_ld16u_i64:
3085    case INDEX_op_ld16s_i32:
3086    case INDEX_op_ld16s_i64:
3087    case INDEX_op_ld_i32:
3088    case INDEX_op_ld32u_i64:
3089    case INDEX_op_ld32s_i64:
3090    case INDEX_op_ld_i64:
3091        return C_O1_I1(r, r);
3092
3093    case INDEX_op_st8_i32:
3094    case INDEX_op_st8_i64:
3095    case INDEX_op_st16_i32:
3096    case INDEX_op_st16_i64:
3097    case INDEX_op_st_i32:
3098    case INDEX_op_st32_i64:
3099    case INDEX_op_st_i64:
3100        return C_O0_I2(r, r);
3101
3102    case INDEX_op_add_i32:
3103    case INDEX_op_add_i64:
3104    case INDEX_op_shl_i64:
3105    case INDEX_op_shr_i64:
3106    case INDEX_op_sar_i64:
3107    case INDEX_op_rotl_i32:
3108    case INDEX_op_rotl_i64:
3109    case INDEX_op_rotr_i32:
3110    case INDEX_op_rotr_i64:
3111    case INDEX_op_setcond_i32:
3112        return C_O1_I2(r, r, ri);
3113    case INDEX_op_setcond_i64:
3114        return C_O1_I2(r, r, rA);
3115
3116    case INDEX_op_clz_i64:
3117        return C_O1_I2(r, r, rI);
3118
3119    case INDEX_op_sub_i32:
3120    case INDEX_op_sub_i64:
3121    case INDEX_op_and_i32:
3122    case INDEX_op_or_i32:
3123    case INDEX_op_xor_i32:
3124        return C_O1_I2(r, r, ri);
3125    case INDEX_op_and_i64:
3126        return C_O1_I2(r, r, rNKR);
3127    case INDEX_op_or_i64:
3128    case INDEX_op_xor_i64:
3129        return C_O1_I2(r, r, rK);
3130
3131    case INDEX_op_andc_i32:
3132    case INDEX_op_orc_i32:
3133    case INDEX_op_eqv_i32:
3134        return C_O1_I2(r, r, ri);
3135    case INDEX_op_andc_i64:
3136        return C_O1_I2(r, r, rKR);
3137    case INDEX_op_orc_i64:
3138    case INDEX_op_eqv_i64:
3139        return C_O1_I2(r, r, rNK);
3140
3141    case INDEX_op_nand_i32:
3142    case INDEX_op_nand_i64:
3143    case INDEX_op_nor_i32:
3144    case INDEX_op_nor_i64:
3145        return C_O1_I2(r, r, r);
3146
3147    case INDEX_op_mul_i32:
3148        return (HAVE_FACILITY(MISC_INSN_EXT2)
3149                ? C_O1_I2(r, r, ri)
3150                : C_O1_I2(r, 0, ri));
3151    case INDEX_op_mul_i64:
3152        return (HAVE_FACILITY(MISC_INSN_EXT2)
3153                ? C_O1_I2(r, r, rJ)
3154                : C_O1_I2(r, 0, rJ));
3155
3156    case INDEX_op_shl_i32:
3157    case INDEX_op_shr_i32:
3158    case INDEX_op_sar_i32:
3159        return C_O1_I2(r, r, ri);
3160
3161    case INDEX_op_brcond_i32:
3162        return C_O0_I2(r, ri);
3163    case INDEX_op_brcond_i64:
3164        return C_O0_I2(r, rA);
3165
3166    case INDEX_op_bswap16_i32:
3167    case INDEX_op_bswap16_i64:
3168    case INDEX_op_bswap32_i32:
3169    case INDEX_op_bswap32_i64:
3170    case INDEX_op_bswap64_i64:
3171    case INDEX_op_neg_i32:
3172    case INDEX_op_neg_i64:
3173    case INDEX_op_not_i32:
3174    case INDEX_op_not_i64:
3175    case INDEX_op_ext8s_i32:
3176    case INDEX_op_ext8s_i64:
3177    case INDEX_op_ext8u_i32:
3178    case INDEX_op_ext8u_i64:
3179    case INDEX_op_ext16s_i32:
3180    case INDEX_op_ext16s_i64:
3181    case INDEX_op_ext16u_i32:
3182    case INDEX_op_ext16u_i64:
3183    case INDEX_op_ext32s_i64:
3184    case INDEX_op_ext32u_i64:
3185    case INDEX_op_ext_i32_i64:
3186    case INDEX_op_extu_i32_i64:
3187    case INDEX_op_extract_i32:
3188    case INDEX_op_extract_i64:
3189    case INDEX_op_ctpop_i32:
3190    case INDEX_op_ctpop_i64:
3191        return C_O1_I1(r, r);
3192
3193    case INDEX_op_qemu_ld_a32_i32:
3194    case INDEX_op_qemu_ld_a64_i32:
3195    case INDEX_op_qemu_ld_a32_i64:
3196    case INDEX_op_qemu_ld_a64_i64:
3197        return C_O1_I1(r, r);
3198    case INDEX_op_qemu_st_a32_i64:
3199    case INDEX_op_qemu_st_a64_i64:
3200    case INDEX_op_qemu_st_a32_i32:
3201    case INDEX_op_qemu_st_a64_i32:
3202        return C_O0_I2(r, r);
3203    case INDEX_op_qemu_ld_a32_i128:
3204    case INDEX_op_qemu_ld_a64_i128:
3205        return C_O2_I1(o, m, r);
3206    case INDEX_op_qemu_st_a32_i128:
3207    case INDEX_op_qemu_st_a64_i128:
3208        return C_O0_I3(o, m, r);
3209
3210    case INDEX_op_deposit_i32:
3211    case INDEX_op_deposit_i64:
3212        return C_O1_I2(r, rZ, r);
3213
3214    case INDEX_op_movcond_i32:
3215        return C_O1_I4(r, r, ri, rI, r);
3216    case INDEX_op_movcond_i64:
3217        return C_O1_I4(r, r, rA, rI, r);
3218
3219    case INDEX_op_div2_i32:
3220    case INDEX_op_div2_i64:
3221    case INDEX_op_divu2_i32:
3222    case INDEX_op_divu2_i64:
3223        return C_O2_I3(o, m, 0, 1, r);
3224
3225    case INDEX_op_mulu2_i64:
3226        return C_O2_I2(o, m, 0, r);
3227    case INDEX_op_muls2_i64:
3228        return C_O2_I2(o, m, r, r);
3229
3230    case INDEX_op_add2_i32:
3231    case INDEX_op_sub2_i32:
3232        return C_N1_O1_I4(r, r, 0, 1, ri, r);
3233
3234    case INDEX_op_add2_i64:
3235    case INDEX_op_sub2_i64:
3236        return C_N1_O1_I4(r, r, 0, 1, rA, r);
3237
3238    case INDEX_op_st_vec:
3239        return C_O0_I2(v, r);
3240    case INDEX_op_ld_vec:
3241    case INDEX_op_dupm_vec:
3242        return C_O1_I1(v, r);
3243    case INDEX_op_dup_vec:
3244        return C_O1_I1(v, vr);
3245    case INDEX_op_abs_vec:
3246    case INDEX_op_neg_vec:
3247    case INDEX_op_not_vec:
3248    case INDEX_op_rotli_vec:
3249    case INDEX_op_sari_vec:
3250    case INDEX_op_shli_vec:
3251    case INDEX_op_shri_vec:
3252    case INDEX_op_s390_vuph_vec:
3253    case INDEX_op_s390_vupl_vec:
3254        return C_O1_I1(v, v);
3255    case INDEX_op_add_vec:
3256    case INDEX_op_sub_vec:
3257    case INDEX_op_and_vec:
3258    case INDEX_op_andc_vec:
3259    case INDEX_op_or_vec:
3260    case INDEX_op_orc_vec:
3261    case INDEX_op_xor_vec:
3262    case INDEX_op_nand_vec:
3263    case INDEX_op_nor_vec:
3264    case INDEX_op_eqv_vec:
3265    case INDEX_op_cmp_vec:
3266    case INDEX_op_mul_vec:
3267    case INDEX_op_rotlv_vec:
3268    case INDEX_op_rotrv_vec:
3269    case INDEX_op_shlv_vec:
3270    case INDEX_op_shrv_vec:
3271    case INDEX_op_sarv_vec:
3272    case INDEX_op_smax_vec:
3273    case INDEX_op_smin_vec:
3274    case INDEX_op_umax_vec:
3275    case INDEX_op_umin_vec:
3276    case INDEX_op_s390_vpks_vec:
3277        return C_O1_I2(v, v, v);
3278    case INDEX_op_rotls_vec:
3279    case INDEX_op_shls_vec:
3280    case INDEX_op_shrs_vec:
3281    case INDEX_op_sars_vec:
3282        return C_O1_I2(v, v, r);
3283    case INDEX_op_bitsel_vec:
3284        return C_O1_I3(v, v, v, v);
3285
3286    default:
3287        g_assert_not_reached();
3288    }
3289}
3290
3291/*
3292 * Mainline glibc added HWCAP_S390_VX before it was kernel abi.
3293 * Some distros have fixed this up locally, others have not.
3294 */
3295#ifndef HWCAP_S390_VXRS
3296#define HWCAP_S390_VXRS 2048
3297#endif
3298
3299static void query_s390_facilities(void)
3300{
3301    unsigned long hwcap = qemu_getauxval(AT_HWCAP);
3302    const char *which;
3303
3304    /* Is STORE FACILITY LIST EXTENDED available?  Honestly, I believe this
3305       is present on all 64-bit systems, but let's check for it anyway.  */
3306    if (hwcap & HWCAP_S390_STFLE) {
3307        register int r0 __asm__("0") = ARRAY_SIZE(s390_facilities) - 1;
3308        register void *r1 __asm__("1") = s390_facilities;
3309
3310        /* stfle 0(%r1) */
3311        asm volatile(".word 0xb2b0,0x1000"
3312                     : "=r"(r0) : "r"(r0), "r"(r1) : "memory", "cc");
3313    }
3314
3315    /*
3316     * Use of vector registers requires os support beyond the facility bit.
3317     * If the kernel does not advertise support, disable the facility bits.
3318     * There is nothing else we currently care about in the 3rd word, so
3319     * disable VECTOR with one store.
3320     */
3321    if (!(hwcap & HWCAP_S390_VXRS)) {
3322        s390_facilities[2] = 0;
3323    }
3324
3325    /*
3326     * Minimum supported cpu revision is z196.
3327     * Check for all required facilities.
3328     * ZARCH_ACTIVE is done via preprocessor check for 64-bit.
3329     */
3330    if (!HAVE_FACILITY(LONG_DISP)) {
3331        which = "long-displacement";
3332        goto fail;
3333    }
3334    if (!HAVE_FACILITY(EXT_IMM)) {
3335        which = "extended-immediate";
3336        goto fail;
3337    }
3338    if (!HAVE_FACILITY(GEN_INST_EXT)) {
3339        which = "general-instructions-extension";
3340        goto fail;
3341    }
3342    /*
3343     * Facility 45 is a big bin that contains: distinct-operands,
3344     * fast-BCR-serialization, high-word, population-count,
3345     * interlocked-access-1, and load/store-on-condition-1
3346     */
3347    if (!HAVE_FACILITY(45)) {
3348        which = "45";
3349        goto fail;
3350    }
3351    return;
3352
3353 fail:
3354    error_report("%s: missing required facility %s", __func__, which);
3355    exit(EXIT_FAILURE);
3356}
3357
3358static void tcg_target_init(TCGContext *s)
3359{
3360    query_s390_facilities();
3361
3362    tcg_target_available_regs[TCG_TYPE_I32] = 0xffff;
3363    tcg_target_available_regs[TCG_TYPE_I64] = 0xffff;
3364    if (HAVE_FACILITY(VECTOR)) {
3365        tcg_target_available_regs[TCG_TYPE_V64] = 0xffffffff00000000ull;
3366        tcg_target_available_regs[TCG_TYPE_V128] = 0xffffffff00000000ull;
3367    }
3368
3369    tcg_target_call_clobber_regs = 0;
3370    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
3371    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
3372    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
3373    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
3374    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
3375    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
3376    /* The r6 register is technically call-saved, but it's also a parameter
3377       register, so it can get killed by setup for the qemu_st helper.  */
3378    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
3379    /* The return register can be considered call-clobbered.  */
3380    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
3381
3382    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V0);
3383    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V1);
3384    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V2);
3385    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V3);
3386    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V4);
3387    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V5);
3388    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V6);
3389    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V7);
3390    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V16);
3391    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V17);
3392    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V18);
3393    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V19);
3394    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V20);
3395    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V21);
3396    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V22);
3397    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V23);
3398    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V24);
3399    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V25);
3400    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V26);
3401    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V27);
3402    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V28);
3403    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V29);
3404    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V30);
3405    tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_V31);
3406
3407    s->reserved_regs = 0;
3408    tcg_regset_set_reg(s->reserved_regs, TCG_TMP0);
3409    /* XXX many insns can't be used with R0, so we better avoid it for now */
3410    tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
3411    tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
3412}
3413
3414#define FRAME_SIZE  ((int)(TCG_TARGET_CALL_STACK_OFFSET          \
3415                           + TCG_STATIC_CALL_ARGS_SIZE           \
3416                           + CPU_TEMP_BUF_NLONGS * sizeof(long)))
3417
3418static void tcg_target_qemu_prologue(TCGContext *s)
3419{
3420    /* stmg %r6,%r15,48(%r15) (save registers) */
3421    tcg_out_insn(s, RXY, STMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 48);
3422
3423    /* aghi %r15,-frame_size */
3424    tcg_out_insn(s, RI, AGHI, TCG_REG_R15, -FRAME_SIZE);
3425
3426    tcg_set_frame(s, TCG_REG_CALL_STACK,
3427                  TCG_STATIC_CALL_ARGS_SIZE + TCG_TARGET_CALL_STACK_OFFSET,
3428                  CPU_TEMP_BUF_NLONGS * sizeof(long));
3429
3430#ifndef CONFIG_SOFTMMU
3431    if (guest_base >= 0x80000) {
3432        tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, guest_base);
3433        tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
3434    }
3435#endif
3436
3437    tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
3438
3439    /* br %r3 (go to TB) */
3440    tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, tcg_target_call_iarg_regs[1]);
3441
3442    /*
3443     * Return path for goto_ptr. Set return value to 0, a-la exit_tb,
3444     * and fall through to the rest of the epilogue.
3445     */
3446    tcg_code_gen_epilogue = tcg_splitwx_to_rx(s->code_ptr);
3447    tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, 0);
3448
3449    /* TB epilogue */
3450    tb_ret_addr = tcg_splitwx_to_rx(s->code_ptr);
3451
3452    /* lmg %r6,%r15,fs+48(%r15) (restore registers) */
3453    tcg_out_insn(s, RXY, LMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15,
3454                 FRAME_SIZE + 48);
3455
3456    /* br %r14 (return) */
3457    tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_REG_R14);
3458}
3459
3460static void tcg_out_nop_fill(tcg_insn_unit *p, int count)
3461{
3462    memset(p, 0x07, count * sizeof(tcg_insn_unit));
3463}
3464
3465typedef struct {
3466    DebugFrameHeader h;
3467    uint8_t fde_def_cfa[4];
3468    uint8_t fde_reg_ofs[18];
3469} DebugFrame;
3470
3471/* We're expecting a 2 byte uleb128 encoded value.  */
3472QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
3473
3474#define ELF_HOST_MACHINE  EM_S390
3475
3476static const DebugFrame debug_frame = {
3477    .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
3478    .h.cie.id = -1,
3479    .h.cie.version = 1,
3480    .h.cie.code_align = 1,
3481    .h.cie.data_align = 8,                /* sleb128 8 */
3482    .h.cie.return_column = TCG_REG_R14,
3483
3484    /* Total FDE size does not include the "len" member.  */
3485    .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
3486
3487    .fde_def_cfa = {
3488        12, TCG_REG_CALL_STACK,         /* DW_CFA_def_cfa %r15, ... */
3489        (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
3490        (FRAME_SIZE >> 7)
3491    },
3492    .fde_reg_ofs = {
3493        0x86, 6,                        /* DW_CFA_offset, %r6, 48 */
3494        0x87, 7,                        /* DW_CFA_offset, %r7, 56 */
3495        0x88, 8,                        /* DW_CFA_offset, %r8, 64 */
3496        0x89, 9,                        /* DW_CFA_offset, %r92, 72 */
3497        0x8a, 10,                       /* DW_CFA_offset, %r10, 80 */
3498        0x8b, 11,                       /* DW_CFA_offset, %r11, 88 */
3499        0x8c, 12,                       /* DW_CFA_offset, %r12, 96 */
3500        0x8d, 13,                       /* DW_CFA_offset, %r13, 104 */
3501        0x8e, 14,                       /* DW_CFA_offset, %r14, 112 */
3502    }
3503};
3504
3505void tcg_register_jit(const void *buf, size_t buf_size)
3506{
3507    tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
3508}
3509