]> Git Repo - qemu.git/blob - tcg/s390/tcg-target.c
tcg-s390: Implement tcg_register_jit
[qemu.git] / tcg / s390 / tcg-target.c
1 /*
2  * Tiny Code Generator for QEMU
3  *
4  * Copyright (c) 2009 Ulrich Hecht <[email protected]>
5  * Copyright (c) 2009 Alexander Graf <[email protected]>
6  * Copyright (c) 2010 Richard Henderson <[email protected]>
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-be-ldst.h"
28
29 /* We only support generating code for 64-bit mode.  */
30 #if TCG_TARGET_REG_BITS != 64
31 #error "unsupported code generation mode"
32 #endif
33
34 #include "elf.h"
35
36 /* ??? The translation blocks produced by TCG are generally small enough to
37    be entirely reachable with a 16-bit displacement.  Leaving the option for
38    a 32-bit displacement here Just In Case.  */
39 #define USE_LONG_BRANCHES 0
40
41 #define TCG_CT_CONST_MULI  0x100
42 #define TCG_CT_CONST_ORI   0x200
43 #define TCG_CT_CONST_XORI  0x400
44 #define TCG_CT_CONST_CMPI  0x800
45
46 /* Several places within the instruction set 0 means "no register"
47    rather than TCG_REG_R0.  */
48 #define TCG_REG_NONE    0
49
50 /* A scratch register that may be be used throughout the backend.  */
51 #define TCG_TMP0        TCG_REG_R14
52
53 #ifdef CONFIG_USE_GUEST_BASE
54 #define TCG_GUEST_BASE_REG TCG_REG_R13
55 #else
56 #define TCG_GUEST_BASE_REG TCG_REG_R0
57 #endif
58
59 #ifndef GUEST_BASE
60 #define GUEST_BASE 0
61 #endif
62
63
64 /* All of the following instructions are prefixed with their instruction
65    format, and are defined as 8- or 16-bit quantities, even when the two
66    halves of the 16-bit quantity may appear 32 bits apart in the insn.
67    This makes it easy to copy the values from the tables in Appendix B.  */
68 typedef enum S390Opcode {
69     RIL_AFI     = 0xc209,
70     RIL_AGFI    = 0xc208,
71     RIL_ALFI    = 0xc20b,
72     RIL_ALGFI   = 0xc20a,
73     RIL_BRASL   = 0xc005,
74     RIL_BRCL    = 0xc004,
75     RIL_CFI     = 0xc20d,
76     RIL_CGFI    = 0xc20c,
77     RIL_CLFI    = 0xc20f,
78     RIL_CLGFI   = 0xc20e,
79     RIL_IIHF    = 0xc008,
80     RIL_IILF    = 0xc009,
81     RIL_LARL    = 0xc000,
82     RIL_LGFI    = 0xc001,
83     RIL_LGRL    = 0xc408,
84     RIL_LLIHF   = 0xc00e,
85     RIL_LLILF   = 0xc00f,
86     RIL_LRL     = 0xc40d,
87     RIL_MSFI    = 0xc201,
88     RIL_MSGFI   = 0xc200,
89     RIL_NIHF    = 0xc00a,
90     RIL_NILF    = 0xc00b,
91     RIL_OIHF    = 0xc00c,
92     RIL_OILF    = 0xc00d,
93     RIL_SLFI    = 0xc205,
94     RIL_SLGFI   = 0xc204,
95     RIL_XIHF    = 0xc006,
96     RIL_XILF    = 0xc007,
97
98     RI_AGHI     = 0xa70b,
99     RI_AHI      = 0xa70a,
100     RI_BRC      = 0xa704,
101     RI_IIHH     = 0xa500,
102     RI_IIHL     = 0xa501,
103     RI_IILH     = 0xa502,
104     RI_IILL     = 0xa503,
105     RI_LGHI     = 0xa709,
106     RI_LLIHH    = 0xa50c,
107     RI_LLIHL    = 0xa50d,
108     RI_LLILH    = 0xa50e,
109     RI_LLILL    = 0xa50f,
110     RI_MGHI     = 0xa70d,
111     RI_MHI      = 0xa70c,
112     RI_NIHH     = 0xa504,
113     RI_NIHL     = 0xa505,
114     RI_NILH     = 0xa506,
115     RI_NILL     = 0xa507,
116     RI_OIHH     = 0xa508,
117     RI_OIHL     = 0xa509,
118     RI_OILH     = 0xa50a,
119     RI_OILL     = 0xa50b,
120
121     RIE_CGIJ    = 0xec7c,
122     RIE_CGRJ    = 0xec64,
123     RIE_CIJ     = 0xec7e,
124     RIE_CLGRJ   = 0xec65,
125     RIE_CLIJ    = 0xec7f,
126     RIE_CLGIJ   = 0xec7d,
127     RIE_CLRJ    = 0xec77,
128     RIE_CRJ     = 0xec76,
129     RIE_RISBG   = 0xec55,
130
131     RRE_AGR     = 0xb908,
132     RRE_ALGR    = 0xb90a,
133     RRE_ALCR    = 0xb998,
134     RRE_ALCGR   = 0xb988,
135     RRE_CGR     = 0xb920,
136     RRE_CLGR    = 0xb921,
137     RRE_DLGR    = 0xb987,
138     RRE_DLR     = 0xb997,
139     RRE_DSGFR   = 0xb91d,
140     RRE_DSGR    = 0xb90d,
141     RRE_LGBR    = 0xb906,
142     RRE_LCGR    = 0xb903,
143     RRE_LGFR    = 0xb914,
144     RRE_LGHR    = 0xb907,
145     RRE_LGR     = 0xb904,
146     RRE_LLGCR   = 0xb984,
147     RRE_LLGFR   = 0xb916,
148     RRE_LLGHR   = 0xb985,
149     RRE_LRVR    = 0xb91f,
150     RRE_LRVGR   = 0xb90f,
151     RRE_LTGR    = 0xb902,
152     RRE_MLGR    = 0xb986,
153     RRE_MSGR    = 0xb90c,
154     RRE_MSR     = 0xb252,
155     RRE_NGR     = 0xb980,
156     RRE_OGR     = 0xb981,
157     RRE_SGR     = 0xb909,
158     RRE_SLGR    = 0xb90b,
159     RRE_SLBR    = 0xb999,
160     RRE_SLBGR   = 0xb989,
161     RRE_XGR     = 0xb982,
162
163     RRF_LOCR    = 0xb9f2,
164     RRF_LOCGR   = 0xb9e2,
165
166     RR_AR       = 0x1a,
167     RR_ALR      = 0x1e,
168     RR_BASR     = 0x0d,
169     RR_BCR      = 0x07,
170     RR_CLR      = 0x15,
171     RR_CR       = 0x19,
172     RR_DR       = 0x1d,
173     RR_LCR      = 0x13,
174     RR_LR       = 0x18,
175     RR_LTR      = 0x12,
176     RR_NR       = 0x14,
177     RR_OR       = 0x16,
178     RR_SR       = 0x1b,
179     RR_SLR      = 0x1f,
180     RR_XR       = 0x17,
181
182     RSY_RLL     = 0xeb1d,
183     RSY_RLLG    = 0xeb1c,
184     RSY_SLLG    = 0xeb0d,
185     RSY_SRAG    = 0xeb0a,
186     RSY_SRLG    = 0xeb0c,
187
188     RS_SLL      = 0x89,
189     RS_SRA      = 0x8a,
190     RS_SRL      = 0x88,
191
192     RXY_AG      = 0xe308,
193     RXY_AY      = 0xe35a,
194     RXY_CG      = 0xe320,
195     RXY_CY      = 0xe359,
196     RXY_LAY     = 0xe371,
197     RXY_LB      = 0xe376,
198     RXY_LG      = 0xe304,
199     RXY_LGB     = 0xe377,
200     RXY_LGF     = 0xe314,
201     RXY_LGH     = 0xe315,
202     RXY_LHY     = 0xe378,
203     RXY_LLGC    = 0xe390,
204     RXY_LLGF    = 0xe316,
205     RXY_LLGH    = 0xe391,
206     RXY_LMG     = 0xeb04,
207     RXY_LRV     = 0xe31e,
208     RXY_LRVG    = 0xe30f,
209     RXY_LRVH    = 0xe31f,
210     RXY_LY      = 0xe358,
211     RXY_STCY    = 0xe372,
212     RXY_STG     = 0xe324,
213     RXY_STHY    = 0xe370,
214     RXY_STMG    = 0xeb24,
215     RXY_STRV    = 0xe33e,
216     RXY_STRVG   = 0xe32f,
217     RXY_STRVH   = 0xe33f,
218     RXY_STY     = 0xe350,
219
220     RX_A        = 0x5a,
221     RX_C        = 0x59,
222     RX_L        = 0x58,
223     RX_LA       = 0x41,
224     RX_LH       = 0x48,
225     RX_ST       = 0x50,
226     RX_STC      = 0x42,
227     RX_STH      = 0x40,
228 } S390Opcode;
229
230 #ifndef NDEBUG
231 static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
232     "%r0", "%r1", "%r2", "%r3", "%r4", "%r5", "%r6", "%r7",
233     "%r8", "%r9", "%r10" "%r11" "%r12" "%r13" "%r14" "%r15"
234 };
235 #endif
236
237 /* Since R6 is a potential argument register, choose it last of the
238    call-saved registers.  Likewise prefer the call-clobbered registers
239    in reverse order to maximize the chance of avoiding the arguments.  */
240 static const int tcg_target_reg_alloc_order[] = {
241     /* Call saved registers.  */
242     TCG_REG_R13,
243     TCG_REG_R12,
244     TCG_REG_R11,
245     TCG_REG_R10,
246     TCG_REG_R9,
247     TCG_REG_R8,
248     TCG_REG_R7,
249     TCG_REG_R6,
250     /* Call clobbered registers.  */
251     TCG_REG_R14,
252     TCG_REG_R0,
253     TCG_REG_R1,
254     /* Argument registers, in reverse order of allocation.  */
255     TCG_REG_R5,
256     TCG_REG_R4,
257     TCG_REG_R3,
258     TCG_REG_R2,
259 };
260
261 static const int tcg_target_call_iarg_regs[] = {
262     TCG_REG_R2,
263     TCG_REG_R3,
264     TCG_REG_R4,
265     TCG_REG_R5,
266     TCG_REG_R6,
267 };
268
269 static const int tcg_target_call_oarg_regs[] = {
270     TCG_REG_R2,
271 };
272
273 #define S390_CC_EQ      8
274 #define S390_CC_LT      4
275 #define S390_CC_GT      2
276 #define S390_CC_OV      1
277 #define S390_CC_NE      (S390_CC_LT | S390_CC_GT)
278 #define S390_CC_LE      (S390_CC_LT | S390_CC_EQ)
279 #define S390_CC_GE      (S390_CC_GT | S390_CC_EQ)
280 #define S390_CC_NEVER   0
281 #define S390_CC_ALWAYS  15
282
283 /* Condition codes that result from a COMPARE and COMPARE LOGICAL.  */
284 static const uint8_t tcg_cond_to_s390_cond[] = {
285     [TCG_COND_EQ]  = S390_CC_EQ,
286     [TCG_COND_NE]  = S390_CC_NE,
287     [TCG_COND_LT]  = S390_CC_LT,
288     [TCG_COND_LE]  = S390_CC_LE,
289     [TCG_COND_GT]  = S390_CC_GT,
290     [TCG_COND_GE]  = S390_CC_GE,
291     [TCG_COND_LTU] = S390_CC_LT,
292     [TCG_COND_LEU] = S390_CC_LE,
293     [TCG_COND_GTU] = S390_CC_GT,
294     [TCG_COND_GEU] = S390_CC_GE,
295 };
296
297 /* Condition codes that result from a LOAD AND TEST.  Here, we have no
298    unsigned instruction variation, however since the test is vs zero we
299    can re-map the outcomes appropriately.  */
300 static const uint8_t tcg_cond_to_ltr_cond[] = {
301     [TCG_COND_EQ]  = S390_CC_EQ,
302     [TCG_COND_NE]  = S390_CC_NE,
303     [TCG_COND_LT]  = S390_CC_LT,
304     [TCG_COND_LE]  = S390_CC_LE,
305     [TCG_COND_GT]  = S390_CC_GT,
306     [TCG_COND_GE]  = S390_CC_GE,
307     [TCG_COND_LTU] = S390_CC_NEVER,
308     [TCG_COND_LEU] = S390_CC_EQ,
309     [TCG_COND_GTU] = S390_CC_NE,
310     [TCG_COND_GEU] = S390_CC_ALWAYS,
311 };
312
313 #ifdef CONFIG_SOFTMMU
314 static void * const qemu_ld_helpers[16] = {
315     [MO_UB]   = helper_ret_ldub_mmu,
316     [MO_SB]   = helper_ret_ldsb_mmu,
317     [MO_LEUW] = helper_le_lduw_mmu,
318     [MO_LESW] = helper_le_ldsw_mmu,
319     [MO_LEUL] = helper_le_ldul_mmu,
320     [MO_LESL] = helper_le_ldsl_mmu,
321     [MO_LEQ]  = helper_le_ldq_mmu,
322     [MO_BEUW] = helper_be_lduw_mmu,
323     [MO_BESW] = helper_be_ldsw_mmu,
324     [MO_BEUL] = helper_be_ldul_mmu,
325     [MO_BESL] = helper_be_ldsl_mmu,
326     [MO_BEQ]  = helper_be_ldq_mmu,
327 };
328
329 static void * const qemu_st_helpers[16] = {
330     [MO_UB]   = helper_ret_stb_mmu,
331     [MO_LEUW] = helper_le_stw_mmu,
332     [MO_LEUL] = helper_le_stl_mmu,
333     [MO_LEQ]  = helper_le_stq_mmu,
334     [MO_BEUW] = helper_be_stw_mmu,
335     [MO_BEUL] = helper_be_stl_mmu,
336     [MO_BEQ]  = helper_be_stq_mmu,
337 };
338 #endif
339
340 static tcg_insn_unit *tb_ret_addr;
341
342 /* A list of relevant facilities used by this translator.  Some of these
343    are required for proper operation, and these are checked at startup.  */
344
345 #define FACILITY_ZARCH_ACTIVE   (1ULL << (63 - 2))
346 #define FACILITY_LONG_DISP      (1ULL << (63 - 18))
347 #define FACILITY_EXT_IMM        (1ULL << (63 - 21))
348 #define FACILITY_GEN_INST_EXT   (1ULL << (63 - 34))
349 #define FACILITY_LOAD_ON_COND   (1ULL << (63 - 45))
350
351 static uint64_t facilities;
352
353 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
354                         intptr_t value, intptr_t addend)
355 {
356     intptr_t pcrel2 = (tcg_insn_unit *)value - (code_ptr - 1);
357     assert(addend == -2);
358
359     switch (type) {
360     case R_390_PC16DBL:
361         assert(pcrel2 == (int16_t)pcrel2);
362         tcg_patch16(code_ptr, pcrel2);
363         break;
364     case R_390_PC32DBL:
365         assert(pcrel2 == (int32_t)pcrel2);
366         tcg_patch32(code_ptr, pcrel2);
367         break;
368     default:
369         tcg_abort();
370         break;
371     }
372 }
373
374 /* parse target specific constraints */
375 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
376 {
377     const char *ct_str = *pct_str;
378
379     switch (ct_str[0]) {
380     case 'r':                  /* all registers */
381         ct->ct |= TCG_CT_REG;
382         tcg_regset_set32(ct->u.regs, 0, 0xffff);
383         break;
384     case 'R':                  /* not R0 */
385         ct->ct |= TCG_CT_REG;
386         tcg_regset_set32(ct->u.regs, 0, 0xffff);
387         tcg_regset_reset_reg(ct->u.regs, TCG_REG_R0);
388         break;
389     case 'L':                  /* qemu_ld/st constraint */
390         ct->ct |= TCG_CT_REG;
391         tcg_regset_set32(ct->u.regs, 0, 0xffff);
392         tcg_regset_reset_reg (ct->u.regs, TCG_REG_R2);
393         tcg_regset_reset_reg (ct->u.regs, TCG_REG_R3);
394         tcg_regset_reset_reg (ct->u.regs, TCG_REG_R4);
395         break;
396     case 'a':                  /* force R2 for division */
397         ct->ct |= TCG_CT_REG;
398         tcg_regset_clear(ct->u.regs);
399         tcg_regset_set_reg(ct->u.regs, TCG_REG_R2);
400         break;
401     case 'b':                  /* force R3 for division */
402         ct->ct |= TCG_CT_REG;
403         tcg_regset_clear(ct->u.regs);
404         tcg_regset_set_reg(ct->u.regs, TCG_REG_R3);
405         break;
406     case 'K':
407         ct->ct |= TCG_CT_CONST_MULI;
408         break;
409     case 'O':
410         ct->ct |= TCG_CT_CONST_ORI;
411         break;
412     case 'X':
413         ct->ct |= TCG_CT_CONST_XORI;
414         break;
415     case 'C':
416         ct->ct |= TCG_CT_CONST_CMPI;
417         break;
418     default:
419         return -1;
420     }
421     ct_str++;
422     *pct_str = ct_str;
423
424     return 0;
425 }
426
427 /* Immediates to be used with logical OR.  This is an optimization only,
428    since a full 64-bit immediate OR can always be performed with 4 sequential
429    OI[LH][LH] instructions.  What we're looking for is immediates that we
430    can load efficiently, and the immediate load plus the reg-reg OR is
431    smaller than the sequential OI's.  */
432
433 static int tcg_match_ori(TCGType type, tcg_target_long val)
434 {
435     if (facilities & FACILITY_EXT_IMM) {
436         if (type == TCG_TYPE_I32) {
437             /* All 32-bit ORs can be performed with 1 48-bit insn.  */
438             return 1;
439         }
440     }
441
442     /* Look for negative values.  These are best to load with LGHI.  */
443     if (val < 0) {
444         if (val == (int16_t)val) {
445             return 0;
446         }
447         if (facilities & FACILITY_EXT_IMM) {
448             if (val == (int32_t)val) {
449                 return 0;
450             }
451         }
452     }
453
454     return 1;
455 }
456
457 /* Immediates to be used with logical XOR.  This is almost, but not quite,
458    only an optimization.  XOR with immediate is only supported with the
459    extended-immediate facility.  That said, there are a few patterns for
460    which it is better to load the value into a register first.  */
461
462 static int tcg_match_xori(TCGType type, tcg_target_long val)
463 {
464     if ((facilities & FACILITY_EXT_IMM) == 0) {
465         return 0;
466     }
467
468     if (type == TCG_TYPE_I32) {
469         /* All 32-bit XORs can be performed with 1 48-bit insn.  */
470         return 1;
471     }
472
473     /* Look for negative values.  These are best to load with LGHI.  */
474     if (val < 0 && val == (int32_t)val) {
475         return 0;
476     }
477
478     return 1;
479 }
480
481 /* Imediates to be used with comparisons.  */
482
483 static int tcg_match_cmpi(TCGType type, tcg_target_long val)
484 {
485     if (facilities & FACILITY_EXT_IMM) {
486         /* The COMPARE IMMEDIATE instruction is available.  */
487         if (type == TCG_TYPE_I32) {
488             /* We have a 32-bit immediate and can compare against anything.  */
489             return 1;
490         } else {
491             /* ??? We have no insight here into whether the comparison is
492                signed or unsigned.  The COMPARE IMMEDIATE insn uses a 32-bit
493                signed immediate, and the COMPARE LOGICAL IMMEDIATE insn uses
494                a 32-bit unsigned immediate.  If we were to use the (semi)
495                obvious "val == (int32_t)val" we would be enabling unsigned
496                comparisons vs very large numbers.  The only solution is to
497                take the intersection of the ranges.  */
498             /* ??? Another possible solution is to simply lie and allow all
499                constants here and force the out-of-range values into a temp
500                register in tgen_cmp when we have knowledge of the actual
501                comparison code in use.  */
502             return val >= 0 && val <= 0x7fffffff;
503         }
504     } else {
505         /* Only the LOAD AND TEST instruction is available.  */
506         return val == 0;
507     }
508 }
509
510 /* Test if a constant matches the constraint. */
511 static int tcg_target_const_match(tcg_target_long val, TCGType type,
512                                   const TCGArgConstraint *arg_ct)
513 {
514     int ct = arg_ct->ct;
515
516     if (ct & TCG_CT_CONST) {
517         return 1;
518     }
519
520     if (type == TCG_TYPE_I32) {
521         val = (int32_t)val;
522     }
523
524     /* The following are mutually exclusive.  */
525     if (ct & TCG_CT_CONST_MULI) {
526         /* Immediates that may be used with multiply.  If we have the
527            general-instruction-extensions, then we have MULTIPLY SINGLE
528            IMMEDIATE with a signed 32-bit, otherwise we have only
529            MULTIPLY HALFWORD IMMEDIATE, with a signed 16-bit.  */
530         if (facilities & FACILITY_GEN_INST_EXT) {
531             return val == (int32_t)val;
532         } else {
533             return val == (int16_t)val;
534         }
535     } else if (ct & TCG_CT_CONST_ORI) {
536         return tcg_match_ori(type, val);
537     } else if (ct & TCG_CT_CONST_XORI) {
538         return tcg_match_xori(type, val);
539     } else if (ct & TCG_CT_CONST_CMPI) {
540         return tcg_match_cmpi(type, val);
541     }
542
543     return 0;
544 }
545
546 /* Emit instructions according to the given instruction format.  */
547
548 static void tcg_out_insn_RR(TCGContext *s, S390Opcode op, TCGReg r1, TCGReg r2)
549 {
550     tcg_out16(s, (op << 8) | (r1 << 4) | r2);
551 }
552
553 static void tcg_out_insn_RRE(TCGContext *s, S390Opcode op,
554                              TCGReg r1, TCGReg r2)
555 {
556     tcg_out32(s, (op << 16) | (r1 << 4) | r2);
557 }
558
559 static void tcg_out_insn_RRF(TCGContext *s, S390Opcode op,
560                              TCGReg r1, TCGReg r2, int m3)
561 {
562     tcg_out32(s, (op << 16) | (m3 << 12) | (r1 << 4) | r2);
563 }
564
565 static void tcg_out_insn_RI(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
566 {
567     tcg_out32(s, (op << 16) | (r1 << 20) | (i2 & 0xffff));
568 }
569
570 static void tcg_out_insn_RIL(TCGContext *s, S390Opcode op, TCGReg r1, int i2)
571 {
572     tcg_out16(s, op | (r1 << 4));
573     tcg_out32(s, i2);
574 }
575
576 static void tcg_out_insn_RS(TCGContext *s, S390Opcode op, TCGReg r1,
577                             TCGReg b2, TCGReg r3, int disp)
578 {
579     tcg_out32(s, (op << 24) | (r1 << 20) | (r3 << 16) | (b2 << 12)
580               | (disp & 0xfff));
581 }
582
583 static void tcg_out_insn_RSY(TCGContext *s, S390Opcode op, TCGReg r1,
584                              TCGReg b2, TCGReg r3, int disp)
585 {
586     tcg_out16(s, (op & 0xff00) | (r1 << 4) | r3);
587     tcg_out32(s, (op & 0xff) | (b2 << 28)
588               | ((disp & 0xfff) << 16) | ((disp & 0xff000) >> 4));
589 }
590
591 #define tcg_out_insn_RX   tcg_out_insn_RS
592 #define tcg_out_insn_RXY  tcg_out_insn_RSY
593
594 /* Emit an opcode with "type-checking" of the format.  */
595 #define tcg_out_insn(S, FMT, OP, ...) \
596     glue(tcg_out_insn_,FMT)(S, glue(glue(FMT,_),OP), ## __VA_ARGS__)
597
598
599 /* emit 64-bit shifts */
600 static void tcg_out_sh64(TCGContext* s, S390Opcode op, TCGReg dest,
601                          TCGReg src, TCGReg sh_reg, int sh_imm)
602 {
603     tcg_out_insn_RSY(s, op, dest, sh_reg, src, sh_imm);
604 }
605
606 /* emit 32-bit shifts */
607 static void tcg_out_sh32(TCGContext* s, S390Opcode op, TCGReg dest,
608                          TCGReg sh_reg, int sh_imm)
609 {
610     tcg_out_insn_RS(s, op, dest, sh_reg, 0, sh_imm);
611 }
612
613 static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg dst, TCGReg src)
614 {
615     if (src != dst) {
616         if (type == TCG_TYPE_I32) {
617             tcg_out_insn(s, RR, LR, dst, src);
618         } else {
619             tcg_out_insn(s, RRE, LGR, dst, src);
620         }
621     }
622 }
623
624 /* load a register with an immediate value */
625 static void tcg_out_movi(TCGContext *s, TCGType type,
626                          TCGReg ret, tcg_target_long sval)
627 {
628     static const S390Opcode lli_insns[4] = {
629         RI_LLILL, RI_LLILH, RI_LLIHL, RI_LLIHH
630     };
631
632     tcg_target_ulong uval = sval;
633     int i;
634
635     if (type == TCG_TYPE_I32) {
636         uval = (uint32_t)sval;
637         sval = (int32_t)sval;
638     }
639
640     /* Try all 32-bit insns that can load it in one go.  */
641     if (sval >= -0x8000 && sval < 0x8000) {
642         tcg_out_insn(s, RI, LGHI, ret, sval);
643         return;
644     }
645
646     for (i = 0; i < 4; i++) {
647         tcg_target_long mask = 0xffffull << i*16;
648         if ((uval & mask) == uval) {
649             tcg_out_insn_RI(s, lli_insns[i], ret, uval >> i*16);
650             return;
651         }
652     }
653
654     /* Try all 48-bit insns that can load it in one go.  */
655     if (facilities & FACILITY_EXT_IMM) {
656         if (sval == (int32_t)sval) {
657             tcg_out_insn(s, RIL, LGFI, ret, sval);
658             return;
659         }
660         if (uval <= 0xffffffff) {
661             tcg_out_insn(s, RIL, LLILF, ret, uval);
662             return;
663         }
664         if ((uval & 0xffffffff) == 0) {
665             tcg_out_insn(s, RIL, LLIHF, ret, uval >> 31 >> 1);
666             return;
667         }
668     }
669
670     /* Try for PC-relative address load.  */
671     if ((sval & 1) == 0) {
672         ptrdiff_t off = tcg_pcrel_diff(s, (void *)sval) >> 1;
673         if (off == (int32_t)off) {
674             tcg_out_insn(s, RIL, LARL, ret, off);
675             return;
676         }
677     }
678
679     /* If extended immediates are not present, then we may have to issue
680        several instructions to load the low 32 bits.  */
681     if (!(facilities & FACILITY_EXT_IMM)) {
682         /* A 32-bit unsigned value can be loaded in 2 insns.  And given
683            that the lli_insns loop above did not succeed, we know that
684            both insns are required.  */
685         if (uval <= 0xffffffff) {
686             tcg_out_insn(s, RI, LLILL, ret, uval);
687             tcg_out_insn(s, RI, IILH, ret, uval >> 16);
688             return;
689         }
690
691         /* If all high bits are set, the value can be loaded in 2 or 3 insns.
692            We first want to make sure that all the high bits get set.  With
693            luck the low 16-bits can be considered negative to perform that for
694            free, otherwise we load an explicit -1.  */
695         if (sval >> 31 >> 1 == -1) {
696             if (uval & 0x8000) {
697                 tcg_out_insn(s, RI, LGHI, ret, uval);
698             } else {
699                 tcg_out_insn(s, RI, LGHI, ret, -1);
700                 tcg_out_insn(s, RI, IILL, ret, uval);
701             }
702             tcg_out_insn(s, RI, IILH, ret, uval >> 16);
703             return;
704         }
705     }
706
707     /* If we get here, both the high and low parts have non-zero bits.  */
708
709     /* Recurse to load the lower 32-bits.  */
710     tcg_out_movi(s, TCG_TYPE_I64, ret, uval & 0xffffffff);
711
712     /* Insert data into the high 32-bits.  */
713     uval = uval >> 31 >> 1;
714     if (facilities & FACILITY_EXT_IMM) {
715         if (uval < 0x10000) {
716             tcg_out_insn(s, RI, IIHL, ret, uval);
717         } else if ((uval & 0xffff) == 0) {
718             tcg_out_insn(s, RI, IIHH, ret, uval >> 16);
719         } else {
720             tcg_out_insn(s, RIL, IIHF, ret, uval);
721         }
722     } else {
723         if (uval & 0xffff) {
724             tcg_out_insn(s, RI, IIHL, ret, uval);
725         }
726         if (uval & 0xffff0000) {
727             tcg_out_insn(s, RI, IIHH, ret, uval >> 16);
728         }
729     }
730 }
731
732
733 /* Emit a load/store type instruction.  Inputs are:
734    DATA:     The register to be loaded or stored.
735    BASE+OFS: The effective address.
736    OPC_RX:   If the operation has an RX format opcode (e.g. STC), otherwise 0.
737    OPC_RXY:  The RXY format opcode for the operation (e.g. STCY).  */
738
739 static void tcg_out_mem(TCGContext *s, S390Opcode opc_rx, S390Opcode opc_rxy,
740                         TCGReg data, TCGReg base, TCGReg index,
741                         tcg_target_long ofs)
742 {
743     if (ofs < -0x80000 || ofs >= 0x80000) {
744         /* Combine the low 20 bits of the offset with the actual load insn;
745            the high 44 bits must come from an immediate load.  */
746         tcg_target_long low = ((ofs & 0xfffff) ^ 0x80000) - 0x80000;
747         tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, ofs - low);
748         ofs = low;
749
750         /* If we were already given an index register, add it in.  */
751         if (index != TCG_REG_NONE) {
752             tcg_out_insn(s, RRE, AGR, TCG_TMP0, index);
753         }
754         index = TCG_TMP0;
755     }
756
757     if (opc_rx && ofs >= 0 && ofs < 0x1000) {
758         tcg_out_insn_RX(s, opc_rx, data, base, index, ofs);
759     } else {
760         tcg_out_insn_RXY(s, opc_rxy, data, base, index, ofs);
761     }
762 }
763
764
765 /* load data without address translation or endianness conversion */
766 static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg data,
767                               TCGReg base, intptr_t ofs)
768 {
769     if (type == TCG_TYPE_I32) {
770         tcg_out_mem(s, RX_L, RXY_LY, data, base, TCG_REG_NONE, ofs);
771     } else {
772         tcg_out_mem(s, 0, RXY_LG, data, base, TCG_REG_NONE, ofs);
773     }
774 }
775
776 static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg data,
777                               TCGReg base, intptr_t ofs)
778 {
779     if (type == TCG_TYPE_I32) {
780         tcg_out_mem(s, RX_ST, RXY_STY, data, base, TCG_REG_NONE, ofs);
781     } else {
782         tcg_out_mem(s, 0, RXY_STG, data, base, TCG_REG_NONE, ofs);
783     }
784 }
785
786 /* load data from an absolute host address */
787 static void tcg_out_ld_abs(TCGContext *s, TCGType type, TCGReg dest, void *abs)
788 {
789     intptr_t addr = (intptr_t)abs;
790
791     if ((facilities & FACILITY_GEN_INST_EXT) && !(addr & 1)) {
792         ptrdiff_t disp = tcg_pcrel_diff(s, abs) >> 1;
793         if (disp == (int32_t)disp) {
794             if (type == TCG_TYPE_I32) {
795                 tcg_out_insn(s, RIL, LRL, dest, disp);
796             } else {
797                 tcg_out_insn(s, RIL, LGRL, dest, disp);
798             }
799             return;
800         }
801     }
802
803     tcg_out_movi(s, TCG_TYPE_PTR, dest, addr & ~0xffff);
804     tcg_out_ld(s, type, dest, dest, addr & 0xffff);
805 }
806
807 static inline void tcg_out_risbg(TCGContext *s, TCGReg dest, TCGReg src,
808                                  int msb, int lsb, int ofs, int z)
809 {
810     /* Format RIE-f */
811     tcg_out16(s, (RIE_RISBG & 0xff00) | (dest << 4) | src);
812     tcg_out16(s, (msb << 8) | (z << 7) | lsb);
813     tcg_out16(s, (ofs << 8) | (RIE_RISBG & 0xff));
814 }
815
816 static void tgen_ext8s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
817 {
818     if (facilities & FACILITY_EXT_IMM) {
819         tcg_out_insn(s, RRE, LGBR, dest, src);
820         return;
821     }
822
823     if (type == TCG_TYPE_I32) {
824         if (dest == src) {
825             tcg_out_sh32(s, RS_SLL, dest, TCG_REG_NONE, 24);
826         } else {
827             tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 24);
828         }
829         tcg_out_sh32(s, RS_SRA, dest, TCG_REG_NONE, 24);
830     } else {
831         tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 56);
832         tcg_out_sh64(s, RSY_SRAG, dest, dest, TCG_REG_NONE, 56);
833     }
834 }
835
836 static void tgen_ext8u(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
837 {
838     if (facilities & FACILITY_EXT_IMM) {
839         tcg_out_insn(s, RRE, LLGCR, dest, src);
840         return;
841     }
842
843     if (dest == src) {
844         tcg_out_movi(s, type, TCG_TMP0, 0xff);
845         src = TCG_TMP0;
846     } else {
847         tcg_out_movi(s, type, dest, 0xff);
848     }
849     if (type == TCG_TYPE_I32) {
850         tcg_out_insn(s, RR, NR, dest, src);
851     } else {
852         tcg_out_insn(s, RRE, NGR, dest, src);
853     }
854 }
855
856 static void tgen_ext16s(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
857 {
858     if (facilities & FACILITY_EXT_IMM) {
859         tcg_out_insn(s, RRE, LGHR, dest, src);
860         return;
861     }
862
863     if (type == TCG_TYPE_I32) {
864         if (dest == src) {
865             tcg_out_sh32(s, RS_SLL, dest, TCG_REG_NONE, 16);
866         } else {
867             tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 16);
868         }
869         tcg_out_sh32(s, RS_SRA, dest, TCG_REG_NONE, 16);
870     } else {
871         tcg_out_sh64(s, RSY_SLLG, dest, src, TCG_REG_NONE, 48);
872         tcg_out_sh64(s, RSY_SRAG, dest, dest, TCG_REG_NONE, 48);
873     }
874 }
875
876 static void tgen_ext16u(TCGContext *s, TCGType type, TCGReg dest, TCGReg src)
877 {
878     if (facilities & FACILITY_EXT_IMM) {
879         tcg_out_insn(s, RRE, LLGHR, dest, src);
880         return;
881     }
882
883     if (dest == src) {
884         tcg_out_movi(s, type, TCG_TMP0, 0xffff);
885         src = TCG_TMP0;
886     } else {
887         tcg_out_movi(s, type, dest, 0xffff);
888     }
889     if (type == TCG_TYPE_I32) {
890         tcg_out_insn(s, RR, NR, dest, src);
891     } else {
892         tcg_out_insn(s, RRE, NGR, dest, src);
893     }
894 }
895
896 static inline void tgen_ext32s(TCGContext *s, TCGReg dest, TCGReg src)
897 {
898     tcg_out_insn(s, RRE, LGFR, dest, src);
899 }
900
901 static inline void tgen_ext32u(TCGContext *s, TCGReg dest, TCGReg src)
902 {
903     tcg_out_insn(s, RRE, LLGFR, dest, src);
904 }
905
906 /* Accept bit patterns like these:
907     0....01....1
908     1....10....0
909     1..10..01..1
910     0..01..10..0
911    Copied from gcc sources.  */
912 static inline bool risbg_mask(uint64_t c)
913 {
914     uint64_t lsb;
915     /* We don't change the number of transitions by inverting,
916        so make sure we start with the LSB zero.  */
917     if (c & 1) {
918         c = ~c;
919     }
920     /* Reject all zeros or all ones.  */
921     if (c == 0) {
922         return false;
923     }
924     /* Find the first transition.  */
925     lsb = c & -c;
926     /* Invert to look for a second transition.  */
927     c = ~c;
928     /* Erase the first transition.  */
929     c &= -lsb;
930     /* Find the second transition, if any.  */
931     lsb = c & -c;
932     /* Match if all the bits are 1's, or if c is zero.  */
933     return c == -lsb;
934 }
935
936 static void tgen_andi_risbg(TCGContext *s, TCGReg out, TCGReg in, uint64_t val)
937 {
938     int msb, lsb;
939     if ((val & 0x8000000000000001ull) == 0x8000000000000001ull) {
940         /* Achieve wraparound by swapping msb and lsb.  */
941         msb = 64 - ctz64(~val);
942         lsb = clz64(~val) - 1;
943     } else {
944         msb = clz64(val);
945         lsb = 63 - ctz64(val);
946     }
947     tcg_out_risbg(s, out, in, msb, lsb, 0, 1);
948 }
949
950 static void tgen_andi(TCGContext *s, TCGType type, TCGReg dest, uint64_t val)
951 {
952     static const S390Opcode ni_insns[4] = {
953         RI_NILL, RI_NILH, RI_NIHL, RI_NIHH
954     };
955     static const S390Opcode nif_insns[2] = {
956         RIL_NILF, RIL_NIHF
957     };
958     uint64_t valid = (type == TCG_TYPE_I32 ? 0xffffffffull : -1ull);
959     int i;
960
961     /* Look for the zero-extensions.  */
962     if ((val & valid) == 0xffffffff) {
963         tgen_ext32u(s, dest, dest);
964         return;
965     }
966     if (facilities & FACILITY_EXT_IMM) {
967         if ((val & valid) == 0xff) {
968             tgen_ext8u(s, TCG_TYPE_I64, dest, dest);
969             return;
970         }
971         if ((val & valid) == 0xffff) {
972             tgen_ext16u(s, TCG_TYPE_I64, dest, dest);
973             return;
974         }
975     }
976
977     /* Try all 32-bit insns that can perform it in one go.  */
978     for (i = 0; i < 4; i++) {
979         tcg_target_ulong mask = ~(0xffffull << i*16);
980         if (((val | ~valid) & mask) == mask) {
981             tcg_out_insn_RI(s, ni_insns[i], dest, val >> i*16);
982             return;
983         }
984     }
985
986     /* Try all 48-bit insns that can perform it in one go.  */
987     if (facilities & FACILITY_EXT_IMM) {
988         for (i = 0; i < 2; i++) {
989             tcg_target_ulong mask = ~(0xffffffffull << i*32);
990             if (((val | ~valid) & mask) == mask) {
991                 tcg_out_insn_RIL(s, nif_insns[i], dest, val >> i*32);
992                 return;
993             }
994         }
995     }
996     if ((facilities & FACILITY_GEN_INST_EXT) && risbg_mask(val)) {
997         tgen_andi_risbg(s, dest, dest, val);
998         return;
999     }
1000
1001     /* Fall back to loading the constant.  */
1002     tcg_out_movi(s, type, TCG_TMP0, val);
1003     if (type == TCG_TYPE_I32) {
1004         tcg_out_insn(s, RR, NR, dest, TCG_TMP0);
1005     } else {
1006         tcg_out_insn(s, RRE, NGR, dest, TCG_TMP0);
1007     }
1008 }
1009
1010 static void tgen64_ori(TCGContext *s, TCGReg dest, tcg_target_ulong val)
1011 {
1012     static const S390Opcode oi_insns[4] = {
1013         RI_OILL, RI_OILH, RI_OIHL, RI_OIHH
1014     };
1015     static const S390Opcode nif_insns[2] = {
1016         RIL_OILF, RIL_OIHF
1017     };
1018
1019     int i;
1020
1021     /* Look for no-op.  */
1022     if (val == 0) {
1023         return;
1024     }
1025
1026     if (facilities & FACILITY_EXT_IMM) {
1027         /* Try all 32-bit insns that can perform it in one go.  */
1028         for (i = 0; i < 4; i++) {
1029             tcg_target_ulong mask = (0xffffull << i*16);
1030             if ((val & mask) != 0 && (val & ~mask) == 0) {
1031                 tcg_out_insn_RI(s, oi_insns[i], dest, val >> i*16);
1032                 return;
1033             }
1034         }
1035
1036         /* Try all 48-bit insns that can perform it in one go.  */
1037         for (i = 0; i < 2; i++) {
1038             tcg_target_ulong mask = (0xffffffffull << i*32);
1039             if ((val & mask) != 0 && (val & ~mask) == 0) {
1040                 tcg_out_insn_RIL(s, nif_insns[i], dest, val >> i*32);
1041                 return;
1042             }
1043         }
1044
1045         /* Perform the OR via sequential modifications to the high and
1046            low parts.  Do this via recursion to handle 16-bit vs 32-bit
1047            masks in each half.  */
1048         tgen64_ori(s, dest, val & 0x00000000ffffffffull);
1049         tgen64_ori(s, dest, val & 0xffffffff00000000ull);
1050     } else {
1051         /* With no extended-immediate facility, we don't need to be so
1052            clever.  Just iterate over the insns and mask in the constant.  */
1053         for (i = 0; i < 4; i++) {
1054             tcg_target_ulong mask = (0xffffull << i*16);
1055             if ((val & mask) != 0) {
1056                 tcg_out_insn_RI(s, oi_insns[i], dest, val >> i*16);
1057             }
1058         }
1059     }
1060 }
1061
1062 static void tgen64_xori(TCGContext *s, TCGReg dest, tcg_target_ulong val)
1063 {
1064     /* Perform the xor by parts.  */
1065     if (val & 0xffffffff) {
1066         tcg_out_insn(s, RIL, XILF, dest, val);
1067     }
1068     if (val > 0xffffffff) {
1069         tcg_out_insn(s, RIL, XIHF, dest, val >> 31 >> 1);
1070     }
1071 }
1072
1073 static int tgen_cmp(TCGContext *s, TCGType type, TCGCond c, TCGReg r1,
1074                     TCGArg c2, int c2const)
1075 {
1076     bool is_unsigned = is_unsigned_cond(c);
1077     if (c2const) {
1078         if (c2 == 0) {
1079             if (type == TCG_TYPE_I32) {
1080                 tcg_out_insn(s, RR, LTR, r1, r1);
1081             } else {
1082                 tcg_out_insn(s, RRE, LTGR, r1, r1);
1083             }
1084             return tcg_cond_to_ltr_cond[c];
1085         } else {
1086             if (is_unsigned) {
1087                 if (type == TCG_TYPE_I32) {
1088                     tcg_out_insn(s, RIL, CLFI, r1, c2);
1089                 } else {
1090                     tcg_out_insn(s, RIL, CLGFI, r1, c2);
1091                 }
1092             } else {
1093                 if (type == TCG_TYPE_I32) {
1094                     tcg_out_insn(s, RIL, CFI, r1, c2);
1095                 } else {
1096                     tcg_out_insn(s, RIL, CGFI, r1, c2);
1097                 }
1098             }
1099         }
1100     } else {
1101         if (is_unsigned) {
1102             if (type == TCG_TYPE_I32) {
1103                 tcg_out_insn(s, RR, CLR, r1, c2);
1104             } else {
1105                 tcg_out_insn(s, RRE, CLGR, r1, c2);
1106             }
1107         } else {
1108             if (type == TCG_TYPE_I32) {
1109                 tcg_out_insn(s, RR, CR, r1, c2);
1110             } else {
1111                 tcg_out_insn(s, RRE, CGR, r1, c2);
1112             }
1113         }
1114     }
1115     return tcg_cond_to_s390_cond[c];
1116 }
1117
1118 static void tgen_setcond(TCGContext *s, TCGType type, TCGCond c,
1119                          TCGReg dest, TCGReg c1, TCGArg c2, int c2const)
1120 {
1121     int cc = tgen_cmp(s, type, c, c1, c2, c2const);
1122
1123     /* Emit: r1 = 1; if (cc) goto over; r1 = 0; over:  */
1124     tcg_out_movi(s, type, dest, 1);
1125     tcg_out_insn(s, RI, BRC, cc, (4 + 4) >> 1);
1126     tcg_out_movi(s, type, dest, 0);
1127 }
1128
1129 static void tgen_movcond(TCGContext *s, TCGType type, TCGCond c, TCGReg dest,
1130                          TCGReg c1, TCGArg c2, int c2const, TCGReg r3)
1131 {
1132     int cc;
1133     if (facilities & FACILITY_LOAD_ON_COND) {
1134         cc = tgen_cmp(s, type, c, c1, c2, c2const);
1135         tcg_out_insn(s, RRF, LOCGR, dest, r3, cc);
1136     } else {
1137         c = tcg_invert_cond(c);
1138         cc = tgen_cmp(s, type, c, c1, c2, c2const);
1139
1140         /* Emit: if (cc) goto over; dest = r3; over:  */
1141         tcg_out_insn(s, RI, BRC, cc, (4 + 4) >> 1);
1142         tcg_out_insn(s, RRE, LGR, dest, r3);
1143     }
1144 }
1145
1146 bool tcg_target_deposit_valid(int ofs, int len)
1147 {
1148     return (facilities & FACILITY_GEN_INST_EXT) != 0;
1149 }
1150
1151 static void tgen_deposit(TCGContext *s, TCGReg dest, TCGReg src,
1152                          int ofs, int len)
1153 {
1154     int lsb = (63 - ofs);
1155     int msb = lsb - (len - 1);
1156     tcg_out_risbg(s, dest, src, msb, lsb, ofs, 0);
1157 }
1158
1159 static void tgen_gotoi(TCGContext *s, int cc, tcg_insn_unit *dest)
1160 {
1161     ptrdiff_t off = dest - s->code_ptr;
1162     if (off == (int16_t)off) {
1163         tcg_out_insn(s, RI, BRC, cc, off);
1164     } else if (off == (int32_t)off) {
1165         tcg_out_insn(s, RIL, BRCL, cc, off);
1166     } else {
1167         tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1168         tcg_out_insn(s, RR, BCR, cc, TCG_TMP0);
1169     }
1170 }
1171
1172 static void tgen_branch(TCGContext *s, int cc, int labelno)
1173 {
1174     TCGLabel* l = &s->labels[labelno];
1175     if (l->has_value) {
1176         tgen_gotoi(s, cc, l->u.value_ptr);
1177     } else if (USE_LONG_BRANCHES) {
1178         tcg_out16(s, RIL_BRCL | (cc << 4));
1179         tcg_out_reloc(s, s->code_ptr, R_390_PC32DBL, labelno, -2);
1180         s->code_ptr += 2;
1181     } else {
1182         tcg_out16(s, RI_BRC | (cc << 4));
1183         tcg_out_reloc(s, s->code_ptr, R_390_PC16DBL, labelno, -2);
1184         s->code_ptr += 1;
1185     }
1186 }
1187
1188 static void tgen_compare_branch(TCGContext *s, S390Opcode opc, int cc,
1189                                 TCGReg r1, TCGReg r2, int labelno)
1190 {
1191     TCGLabel* l = &s->labels[labelno];
1192     intptr_t off;
1193
1194     if (l->has_value) {
1195         off = l->u.value_ptr - s->code_ptr;
1196     } else {
1197         /* We need to keep the offset unchanged for retranslation.  */
1198         off = s->code_ptr[1];
1199         tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, labelno, -2);
1200     }
1201
1202     tcg_out16(s, (opc & 0xff00) | (r1 << 4) | r2);
1203     tcg_out16(s, off);
1204     tcg_out16(s, cc << 12 | (opc & 0xff));
1205 }
1206
1207 static void tgen_compare_imm_branch(TCGContext *s, S390Opcode opc, int cc,
1208                                     TCGReg r1, int i2, int labelno)
1209 {
1210     TCGLabel* l = &s->labels[labelno];
1211     tcg_target_long off;
1212
1213     if (l->has_value) {
1214         off = l->u.value_ptr - s->code_ptr;
1215     } else {
1216         /* We need to keep the offset unchanged for retranslation.  */
1217         off = s->code_ptr[1];
1218         tcg_out_reloc(s, s->code_ptr + 1, R_390_PC16DBL, labelno, -2);
1219     }
1220
1221     tcg_out16(s, (opc & 0xff00) | (r1 << 4) | cc);
1222     tcg_out16(s, off);
1223     tcg_out16(s, (i2 << 8) | (opc & 0xff));
1224 }
1225
1226 static void tgen_brcond(TCGContext *s, TCGType type, TCGCond c,
1227                         TCGReg r1, TCGArg c2, int c2const, int labelno)
1228 {
1229     int cc;
1230
1231     if (facilities & FACILITY_GEN_INST_EXT) {
1232         bool is_unsigned = is_unsigned_cond(c);
1233         bool in_range;
1234         S390Opcode opc;
1235
1236         cc = tcg_cond_to_s390_cond[c];
1237
1238         if (!c2const) {
1239             opc = (type == TCG_TYPE_I32
1240                    ? (is_unsigned ? RIE_CLRJ : RIE_CRJ)
1241                    : (is_unsigned ? RIE_CLGRJ : RIE_CGRJ));
1242             tgen_compare_branch(s, opc, cc, r1, c2, labelno);
1243             return;
1244         }
1245
1246         /* COMPARE IMMEDIATE AND BRANCH RELATIVE has an 8-bit immediate field.
1247            If the immediate we've been given does not fit that range, we'll
1248            fall back to separate compare and branch instructions using the
1249            larger comparison range afforded by COMPARE IMMEDIATE.  */
1250         if (type == TCG_TYPE_I32) {
1251             if (is_unsigned) {
1252                 opc = RIE_CLIJ;
1253                 in_range = (uint32_t)c2 == (uint8_t)c2;
1254             } else {
1255                 opc = RIE_CIJ;
1256                 in_range = (int32_t)c2 == (int8_t)c2;
1257             }
1258         } else {
1259             if (is_unsigned) {
1260                 opc = RIE_CLGIJ;
1261                 in_range = (uint64_t)c2 == (uint8_t)c2;
1262             } else {
1263                 opc = RIE_CGIJ;
1264                 in_range = (int64_t)c2 == (int8_t)c2;
1265             }
1266         }
1267         if (in_range) {
1268             tgen_compare_imm_branch(s, opc, cc, r1, c2, labelno);
1269             return;
1270         }
1271     }
1272
1273     cc = tgen_cmp(s, type, c, r1, c2, c2const);
1274     tgen_branch(s, cc, labelno);
1275 }
1276
1277 static void tcg_out_call(TCGContext *s, tcg_insn_unit *dest)
1278 {
1279     ptrdiff_t off = dest - s->code_ptr;
1280     if (off == (int32_t)off) {
1281         tcg_out_insn(s, RIL, BRASL, TCG_REG_R14, off);
1282     } else {
1283         tcg_out_movi(s, TCG_TYPE_PTR, TCG_TMP0, (uintptr_t)dest);
1284         tcg_out_insn(s, RR, BASR, TCG_REG_R14, TCG_TMP0);
1285     }
1286 }
1287
1288 static void tcg_out_qemu_ld_direct(TCGContext *s, TCGMemOp opc, TCGReg data,
1289                                    TCGReg base, TCGReg index, int disp)
1290 {
1291     switch (opc) {
1292     case MO_UB:
1293         tcg_out_insn(s, RXY, LLGC, data, base, index, disp);
1294         break;
1295     case MO_SB:
1296         tcg_out_insn(s, RXY, LGB, data, base, index, disp);
1297         break;
1298
1299     case MO_UW | MO_BSWAP:
1300         /* swapped unsigned halfword load with upper bits zeroed */
1301         tcg_out_insn(s, RXY, LRVH, data, base, index, disp);
1302         tgen_ext16u(s, TCG_TYPE_I64, data, data);
1303         break;
1304     case MO_UW:
1305         tcg_out_insn(s, RXY, LLGH, data, base, index, disp);
1306         break;
1307
1308     case MO_SW | MO_BSWAP:
1309         /* swapped sign-extended halfword load */
1310         tcg_out_insn(s, RXY, LRVH, data, base, index, disp);
1311         tgen_ext16s(s, TCG_TYPE_I64, data, data);
1312         break;
1313     case MO_SW:
1314         tcg_out_insn(s, RXY, LGH, data, base, index, disp);
1315         break;
1316
1317     case MO_UL | MO_BSWAP:
1318         /* swapped unsigned int load with upper bits zeroed */
1319         tcg_out_insn(s, RXY, LRV, data, base, index, disp);
1320         tgen_ext32u(s, data, data);
1321         break;
1322     case MO_UL:
1323         tcg_out_insn(s, RXY, LLGF, data, base, index, disp);
1324         break;
1325
1326     case MO_SL | MO_BSWAP:
1327         /* swapped sign-extended int load */
1328         tcg_out_insn(s, RXY, LRV, data, base, index, disp);
1329         tgen_ext32s(s, data, data);
1330         break;
1331     case MO_SL:
1332         tcg_out_insn(s, RXY, LGF, data, base, index, disp);
1333         break;
1334
1335     case MO_Q | MO_BSWAP:
1336         tcg_out_insn(s, RXY, LRVG, data, base, index, disp);
1337         break;
1338     case MO_Q:
1339         tcg_out_insn(s, RXY, LG, data, base, index, disp);
1340         break;
1341
1342     default:
1343         tcg_abort();
1344     }
1345 }
1346
1347 static void tcg_out_qemu_st_direct(TCGContext *s, TCGMemOp opc, TCGReg data,
1348                                    TCGReg base, TCGReg index, int disp)
1349 {
1350     switch (opc) {
1351     case MO_UB:
1352         if (disp >= 0 && disp < 0x1000) {
1353             tcg_out_insn(s, RX, STC, data, base, index, disp);
1354         } else {
1355             tcg_out_insn(s, RXY, STCY, data, base, index, disp);
1356         }
1357         break;
1358
1359     case MO_UW | MO_BSWAP:
1360         tcg_out_insn(s, RXY, STRVH, data, base, index, disp);
1361         break;
1362     case MO_UW:
1363         if (disp >= 0 && disp < 0x1000) {
1364             tcg_out_insn(s, RX, STH, data, base, index, disp);
1365         } else {
1366             tcg_out_insn(s, RXY, STHY, data, base, index, disp);
1367         }
1368         break;
1369
1370     case MO_UL | MO_BSWAP:
1371         tcg_out_insn(s, RXY, STRV, data, base, index, disp);
1372         break;
1373     case MO_UL:
1374         if (disp >= 0 && disp < 0x1000) {
1375             tcg_out_insn(s, RX, ST, data, base, index, disp);
1376         } else {
1377             tcg_out_insn(s, RXY, STY, data, base, index, disp);
1378         }
1379         break;
1380
1381     case MO_Q | MO_BSWAP:
1382         tcg_out_insn(s, RXY, STRVG, data, base, index, disp);
1383         break;
1384     case MO_Q:
1385         tcg_out_insn(s, RXY, STG, data, base, index, disp);
1386         break;
1387
1388     default:
1389         tcg_abort();
1390     }
1391 }
1392
1393 #if defined(CONFIG_SOFTMMU)
1394 /* We're expecting to use a 20-bit signed offset on the tlb memory ops.
1395    Using the offset of the second entry in the last tlb table ensures
1396    that we can index all of the elements of the first entry.  */
1397 QEMU_BUILD_BUG_ON(offsetof(CPUArchState, tlb_table[NB_MMU_MODES - 1][1])
1398                   > 0x7ffff);
1399
1400 /* Load and compare a TLB entry, leaving the flags set.  Loads the TLB
1401    addend into R2.  Returns a register with the santitized guest address.  */
1402 static TCGReg tcg_out_tlb_read(TCGContext* s, TCGReg addr_reg, TCGMemOp opc,
1403                                int mem_index, bool is_ld)
1404 {
1405     TCGMemOp s_bits = opc & MO_SIZE;
1406     uint64_t tlb_mask = TARGET_PAGE_MASK | ((1 << s_bits) - 1);
1407     int ofs;
1408
1409     if (facilities & FACILITY_GEN_INST_EXT) {
1410         tcg_out_risbg(s, TCG_REG_R2, addr_reg,
1411                       64 - CPU_TLB_BITS - CPU_TLB_ENTRY_BITS,
1412                       63 - CPU_TLB_ENTRY_BITS,
1413                       64 + CPU_TLB_ENTRY_BITS - TARGET_PAGE_BITS, 1);
1414         tgen_andi_risbg(s, TCG_REG_R3, addr_reg, tlb_mask);
1415     } else {
1416         tcg_out_sh64(s, RSY_SRLG, TCG_REG_R2, addr_reg, TCG_REG_NONE,
1417                      TARGET_PAGE_BITS - CPU_TLB_ENTRY_BITS);
1418         tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_R3, addr_reg);
1419         tgen_andi(s, TCG_TYPE_I64, TCG_REG_R2,
1420                   (CPU_TLB_SIZE - 1) << CPU_TLB_ENTRY_BITS);
1421         tgen_andi(s, TCG_TYPE_TL, TCG_REG_R3, tlb_mask);
1422     }
1423
1424     if (is_ld) {
1425         ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addr_read);
1426     } else {
1427         ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addr_write);
1428     }
1429     if (TARGET_LONG_BITS == 32) {
1430         tcg_out_mem(s, RX_C, RXY_CY, TCG_REG_R3, TCG_REG_R2, TCG_AREG0, ofs);
1431     } else {
1432         tcg_out_mem(s, 0, RXY_CG, TCG_REG_R3, TCG_REG_R2, TCG_AREG0, ofs);
1433     }
1434
1435     ofs = offsetof(CPUArchState, tlb_table[mem_index][0].addend);
1436     tcg_out_mem(s, 0, RXY_LG, TCG_REG_R2, TCG_REG_R2, TCG_AREG0, ofs);
1437
1438     if (TARGET_LONG_BITS == 32) {
1439         tgen_ext32u(s, TCG_REG_R3, addr_reg);
1440         return TCG_REG_R3;
1441     }
1442     return addr_reg;
1443 }
1444
1445 static void add_qemu_ldst_label(TCGContext *s, bool is_ld, TCGMemOp opc,
1446                                 TCGReg data, TCGReg addr, int mem_index,
1447                                 tcg_insn_unit *raddr, tcg_insn_unit *label_ptr)
1448 {
1449     TCGLabelQemuLdst *label = new_ldst_label(s);
1450
1451     label->is_ld = is_ld;
1452     label->opc = opc;
1453     label->datalo_reg = data;
1454     label->addrlo_reg = addr;
1455     label->mem_index = mem_index;
1456     label->raddr = raddr;
1457     label->label_ptr[0] = label_ptr;
1458 }
1459
1460 static void tcg_out_qemu_ld_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1461 {
1462     TCGReg addr_reg = lb->addrlo_reg;
1463     TCGReg data_reg = lb->datalo_reg;
1464     TCGMemOp opc = lb->opc;
1465
1466     patch_reloc(lb->label_ptr[0], R_390_PC16DBL, (intptr_t)s->code_ptr, -2);
1467
1468     tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_AREG0);
1469     if (TARGET_LONG_BITS == 64) {
1470         tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, addr_reg);
1471     }
1472     tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R4, lb->mem_index);
1473     tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R5, (uintptr_t)lb->raddr);
1474     tcg_out_call(s, qemu_ld_helpers[opc]);
1475     tcg_out_mov(s, TCG_TYPE_I64, data_reg, TCG_REG_R2);
1476
1477     tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1478 }
1479
1480 static void tcg_out_qemu_st_slow_path(TCGContext *s, TCGLabelQemuLdst *lb)
1481 {
1482     TCGReg addr_reg = lb->addrlo_reg;
1483     TCGReg data_reg = lb->datalo_reg;
1484     TCGMemOp opc = lb->opc;
1485
1486     patch_reloc(lb->label_ptr[0], R_390_PC16DBL, (intptr_t)s->code_ptr, -2);
1487
1488     tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_R2, TCG_AREG0);
1489     if (TARGET_LONG_BITS == 64) {
1490         tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R3, addr_reg);
1491     }
1492     switch (opc & MO_SIZE) {
1493     case MO_UB:
1494         tgen_ext8u(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
1495         break;
1496     case MO_UW:
1497         tgen_ext16u(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
1498         break;
1499     case MO_UL:
1500         tgen_ext32u(s, TCG_REG_R4, data_reg);
1501         break;
1502     case MO_Q:
1503         tcg_out_mov(s, TCG_TYPE_I64, TCG_REG_R4, data_reg);
1504         break;
1505     default:
1506         tcg_abort();
1507     }
1508     tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_R5, lb->mem_index);
1509     tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R6, (uintptr_t)lb->raddr);
1510     tcg_out_call(s, qemu_st_helpers[opc]);
1511
1512     tgen_gotoi(s, S390_CC_ALWAYS, lb->raddr);
1513 }
1514 #else
1515 static void tcg_prepare_user_ldst(TCGContext *s, TCGReg *addr_reg,
1516                                   TCGReg *index_reg, tcg_target_long *disp)
1517 {
1518     if (TARGET_LONG_BITS == 32) {
1519         tgen_ext32u(s, TCG_TMP0, *addr_reg);
1520         *addr_reg = TCG_TMP0;
1521     }
1522     if (GUEST_BASE < 0x80000) {
1523         *index_reg = TCG_REG_NONE;
1524         *disp = GUEST_BASE;
1525     } else {
1526         *index_reg = TCG_GUEST_BASE_REG;
1527         *disp = 0;
1528     }
1529 }
1530 #endif /* CONFIG_SOFTMMU */
1531
1532 static void tcg_out_qemu_ld(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1533                             TCGMemOp opc, int mem_index)
1534 {
1535 #ifdef CONFIG_SOFTMMU
1536     tcg_insn_unit *label_ptr;
1537     TCGReg base_reg;
1538
1539     base_reg = tcg_out_tlb_read(s, addr_reg, opc, mem_index, 1);
1540
1541     label_ptr = s->code_ptr + 1;
1542     tcg_out_insn(s, RI, BRC, S390_CC_NE, 0);
1543
1544     tcg_out_qemu_ld_direct(s, opc, data_reg, base_reg, TCG_REG_R2, 0);
1545
1546     add_qemu_ldst_label(s, 1, opc, data_reg, addr_reg, mem_index,
1547                         s->code_ptr, label_ptr);
1548 #else
1549     TCGReg index_reg;
1550     tcg_target_long disp;
1551
1552     tcg_prepare_user_ldst(s, &addr_reg, &index_reg, &disp);
1553     tcg_out_qemu_ld_direct(s, opc, data_reg, addr_reg, index_reg, disp);
1554 #endif
1555 }
1556
1557 static void tcg_out_qemu_st(TCGContext* s, TCGReg data_reg, TCGReg addr_reg,
1558                             TCGMemOp opc, int mem_index)
1559 {
1560 #ifdef CONFIG_SOFTMMU
1561     tcg_insn_unit *label_ptr;
1562     TCGReg base_reg;
1563
1564     base_reg = tcg_out_tlb_read(s, addr_reg, opc, mem_index, 0);
1565
1566     label_ptr = s->code_ptr + 1;
1567     tcg_out_insn(s, RI, BRC, S390_CC_NE, 0);
1568
1569     tcg_out_qemu_st_direct(s, opc, data_reg, base_reg, TCG_REG_R2, 0);
1570
1571     add_qemu_ldst_label(s, 0, opc, data_reg, addr_reg, mem_index,
1572                         s->code_ptr, label_ptr);
1573 #else
1574     TCGReg index_reg;
1575     tcg_target_long disp;
1576
1577     tcg_prepare_user_ldst(s, &addr_reg, &index_reg, &disp);
1578     tcg_out_qemu_st_direct(s, opc, data_reg, addr_reg, index_reg, disp);
1579 #endif
1580 }
1581
1582 # define OP_32_64(x) \
1583         case glue(glue(INDEX_op_,x),_i32): \
1584         case glue(glue(INDEX_op_,x),_i64)
1585
1586 static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,
1587                 const TCGArg *args, const int *const_args)
1588 {
1589     S390Opcode op;
1590     TCGArg a0, a1, a2;
1591
1592     switch (opc) {
1593     case INDEX_op_exit_tb:
1594         /* return value */
1595         tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_R2, args[0]);
1596         tgen_gotoi(s, S390_CC_ALWAYS, tb_ret_addr);
1597         break;
1598
1599     case INDEX_op_goto_tb:
1600         if (s->tb_jmp_offset) {
1601             tcg_abort();
1602         } else {
1603             /* load address stored at s->tb_next + args[0] */
1604             tcg_out_ld_abs(s, TCG_TYPE_PTR, TCG_TMP0, s->tb_next + args[0]);
1605             /* and go there */
1606             tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_TMP0);
1607         }
1608         s->tb_next_offset[args[0]] = tcg_current_code_size(s);
1609         break;
1610
1611     OP_32_64(ld8u):
1612         /* ??? LLC (RXY format) is only present with the extended-immediate
1613            facility, whereas LLGC is always present.  */
1614         tcg_out_mem(s, 0, RXY_LLGC, args[0], args[1], TCG_REG_NONE, args[2]);
1615         break;
1616
1617     OP_32_64(ld8s):
1618         /* ??? LB is no smaller than LGB, so no point to using it.  */
1619         tcg_out_mem(s, 0, RXY_LGB, args[0], args[1], TCG_REG_NONE, args[2]);
1620         break;
1621
1622     OP_32_64(ld16u):
1623         /* ??? LLH (RXY format) is only present with the extended-immediate
1624            facility, whereas LLGH is always present.  */
1625         tcg_out_mem(s, 0, RXY_LLGH, args[0], args[1], TCG_REG_NONE, args[2]);
1626         break;
1627
1628     case INDEX_op_ld16s_i32:
1629         tcg_out_mem(s, RX_LH, RXY_LHY, args[0], args[1], TCG_REG_NONE, args[2]);
1630         break;
1631
1632     case INDEX_op_ld_i32:
1633         tcg_out_ld(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1634         break;
1635
1636     OP_32_64(st8):
1637         tcg_out_mem(s, RX_STC, RXY_STCY, args[0], args[1],
1638                     TCG_REG_NONE, args[2]);
1639         break;
1640
1641     OP_32_64(st16):
1642         tcg_out_mem(s, RX_STH, RXY_STHY, args[0], args[1],
1643                     TCG_REG_NONE, args[2]);
1644         break;
1645
1646     case INDEX_op_st_i32:
1647         tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1648         break;
1649
1650     case INDEX_op_add_i32:
1651         a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
1652         if (const_args[2]) {
1653         do_addi_32:
1654             if (a0 == a1) {
1655                 if (a2 == (int16_t)a2) {
1656                     tcg_out_insn(s, RI, AHI, a0, a2);
1657                     break;
1658                 }
1659                 if (facilities & FACILITY_EXT_IMM) {
1660                     tcg_out_insn(s, RIL, AFI, a0, a2);
1661                     break;
1662                 }
1663             }
1664             tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
1665         } else if (a0 == a1) {
1666             tcg_out_insn(s, RR, AR, a0, a2);
1667         } else {
1668             tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
1669         }
1670         break;
1671     case INDEX_op_sub_i32:
1672         a0 = args[0], a1 = args[1], a2 = (int32_t)args[2];
1673         if (const_args[2]) {
1674             a2 = -a2;
1675             goto do_addi_32;
1676         }
1677         tcg_out_insn(s, RR, SR, args[0], args[2]);
1678         break;
1679
1680     case INDEX_op_and_i32:
1681         if (const_args[2]) {
1682             tgen_andi(s, TCG_TYPE_I32, args[0], args[2]);
1683         } else {
1684             tcg_out_insn(s, RR, NR, args[0], args[2]);
1685         }
1686         break;
1687     case INDEX_op_or_i32:
1688         if (const_args[2]) {
1689             tgen64_ori(s, args[0], args[2] & 0xffffffff);
1690         } else {
1691             tcg_out_insn(s, RR, OR, args[0], args[2]);
1692         }
1693         break;
1694     case INDEX_op_xor_i32:
1695         if (const_args[2]) {
1696             tgen64_xori(s, args[0], args[2] & 0xffffffff);
1697         } else {
1698             tcg_out_insn(s, RR, XR, args[0], args[2]);
1699         }
1700         break;
1701
1702     case INDEX_op_neg_i32:
1703         tcg_out_insn(s, RR, LCR, args[0], args[1]);
1704         break;
1705
1706     case INDEX_op_mul_i32:
1707         if (const_args[2]) {
1708             if ((int32_t)args[2] == (int16_t)args[2]) {
1709                 tcg_out_insn(s, RI, MHI, args[0], args[2]);
1710             } else {
1711                 tcg_out_insn(s, RIL, MSFI, args[0], args[2]);
1712             }
1713         } else {
1714             tcg_out_insn(s, RRE, MSR, args[0], args[2]);
1715         }
1716         break;
1717
1718     case INDEX_op_div2_i32:
1719         tcg_out_insn(s, RR, DR, TCG_REG_R2, args[4]);
1720         break;
1721     case INDEX_op_divu2_i32:
1722         tcg_out_insn(s, RRE, DLR, TCG_REG_R2, args[4]);
1723         break;
1724
1725     case INDEX_op_shl_i32:
1726         op = RS_SLL;
1727     do_shift32:
1728         if (const_args[2]) {
1729             tcg_out_sh32(s, op, args[0], TCG_REG_NONE, args[2]);
1730         } else {
1731             tcg_out_sh32(s, op, args[0], args[2], 0);
1732         }
1733         break;
1734     case INDEX_op_shr_i32:
1735         op = RS_SRL;
1736         goto do_shift32;
1737     case INDEX_op_sar_i32:
1738         op = RS_SRA;
1739         goto do_shift32;
1740
1741     case INDEX_op_rotl_i32:
1742         /* ??? Using tcg_out_sh64 here for the format; it is a 32-bit rol.  */
1743         if (const_args[2]) {
1744             tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_REG_NONE, args[2]);
1745         } else {
1746             tcg_out_sh64(s, RSY_RLL, args[0], args[1], args[2], 0);
1747         }
1748         break;
1749     case INDEX_op_rotr_i32:
1750         if (const_args[2]) {
1751             tcg_out_sh64(s, RSY_RLL, args[0], args[1],
1752                          TCG_REG_NONE, (32 - args[2]) & 31);
1753         } else {
1754             tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
1755             tcg_out_sh64(s, RSY_RLL, args[0], args[1], TCG_TMP0, 0);
1756         }
1757         break;
1758
1759     case INDEX_op_ext8s_i32:
1760         tgen_ext8s(s, TCG_TYPE_I32, args[0], args[1]);
1761         break;
1762     case INDEX_op_ext16s_i32:
1763         tgen_ext16s(s, TCG_TYPE_I32, args[0], args[1]);
1764         break;
1765     case INDEX_op_ext8u_i32:
1766         tgen_ext8u(s, TCG_TYPE_I32, args[0], args[1]);
1767         break;
1768     case INDEX_op_ext16u_i32:
1769         tgen_ext16u(s, TCG_TYPE_I32, args[0], args[1]);
1770         break;
1771
1772     OP_32_64(bswap16):
1773         /* The TCG bswap definition requires bits 0-47 already be zero.
1774            Thus we don't need the G-type insns to implement bswap16_i64.  */
1775         tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
1776         tcg_out_sh32(s, RS_SRL, args[0], TCG_REG_NONE, 16);
1777         break;
1778     OP_32_64(bswap32):
1779         tcg_out_insn(s, RRE, LRVR, args[0], args[1]);
1780         break;
1781
1782     case INDEX_op_add2_i32:
1783         /* ??? Make use of ALFI.  */
1784         tcg_out_insn(s, RR, ALR, args[0], args[4]);
1785         tcg_out_insn(s, RRE, ALCR, args[1], args[5]);
1786         break;
1787     case INDEX_op_sub2_i32:
1788         /* ??? Make use of SLFI.  */
1789         tcg_out_insn(s, RR, SLR, args[0], args[4]);
1790         tcg_out_insn(s, RRE, SLBR, args[1], args[5]);
1791         break;
1792
1793     case INDEX_op_br:
1794         tgen_branch(s, S390_CC_ALWAYS, args[0]);
1795         break;
1796
1797     case INDEX_op_brcond_i32:
1798         tgen_brcond(s, TCG_TYPE_I32, args[2], args[0],
1799                     args[1], const_args[1], args[3]);
1800         break;
1801     case INDEX_op_setcond_i32:
1802         tgen_setcond(s, TCG_TYPE_I32, args[3], args[0], args[1],
1803                      args[2], const_args[2]);
1804         break;
1805     case INDEX_op_movcond_i32:
1806         tgen_movcond(s, TCG_TYPE_I32, args[5], args[0], args[1],
1807                      args[2], const_args[2], args[3]);
1808         break;
1809
1810     case INDEX_op_qemu_ld_i32:
1811         /* ??? Technically we can use a non-extending instruction.  */
1812     case INDEX_op_qemu_ld_i64:
1813         tcg_out_qemu_ld(s, args[0], args[1], args[2], args[3]);
1814         break;
1815     case INDEX_op_qemu_st_i32:
1816     case INDEX_op_qemu_st_i64:
1817         tcg_out_qemu_st(s, args[0], args[1], args[2], args[3]);
1818         break;
1819
1820     case INDEX_op_ld16s_i64:
1821         tcg_out_mem(s, 0, RXY_LGH, args[0], args[1], TCG_REG_NONE, args[2]);
1822         break;
1823     case INDEX_op_ld32u_i64:
1824         tcg_out_mem(s, 0, RXY_LLGF, args[0], args[1], TCG_REG_NONE, args[2]);
1825         break;
1826     case INDEX_op_ld32s_i64:
1827         tcg_out_mem(s, 0, RXY_LGF, args[0], args[1], TCG_REG_NONE, args[2]);
1828         break;
1829     case INDEX_op_ld_i64:
1830         tcg_out_ld(s, TCG_TYPE_I64, args[0], args[1], args[2]);
1831         break;
1832
1833     case INDEX_op_st32_i64:
1834         tcg_out_st(s, TCG_TYPE_I32, args[0], args[1], args[2]);
1835         break;
1836     case INDEX_op_st_i64:
1837         tcg_out_st(s, TCG_TYPE_I64, args[0], args[1], args[2]);
1838         break;
1839
1840     case INDEX_op_add_i64:
1841         a0 = args[0], a1 = args[1], a2 = args[2];
1842         if (const_args[2]) {
1843         do_addi_64:
1844             if (a0 == a1) {
1845                 if (a2 == (int16_t)a2) {
1846                     tcg_out_insn(s, RI, AGHI, a0, a2);
1847                     break;
1848                 }
1849                 if (facilities & FACILITY_EXT_IMM) {
1850                     if (a2 == (int32_t)a2) {
1851                         tcg_out_insn(s, RIL, AGFI, a0, a2);
1852                         break;
1853                     } else if (a2 == (uint32_t)a2) {
1854                         tcg_out_insn(s, RIL, ALGFI, a0, a2);
1855                         break;
1856                     } else if (-a2 == (uint32_t)-a2) {
1857                         tcg_out_insn(s, RIL, SLGFI, a0, -a2);
1858                         break;
1859                     }
1860                 }
1861             }
1862             tcg_out_mem(s, RX_LA, RXY_LAY, a0, a1, TCG_REG_NONE, a2);
1863         } else if (a0 == a1) {
1864             tcg_out_insn(s, RRE, AGR, a0, a2);
1865         } else {
1866             tcg_out_insn(s, RX, LA, a0, a1, a2, 0);
1867         }
1868         break;
1869     case INDEX_op_sub_i64:
1870         a0 = args[0], a1 = args[1], a2 = args[2];
1871         if (const_args[2]) {
1872             a2 = -a2;
1873             goto do_addi_64;
1874         } else {
1875             tcg_out_insn(s, RRE, SGR, args[0], args[2]);
1876         }
1877         break;
1878
1879     case INDEX_op_and_i64:
1880         if (const_args[2]) {
1881             tgen_andi(s, TCG_TYPE_I64, args[0], args[2]);
1882         } else {
1883             tcg_out_insn(s, RRE, NGR, args[0], args[2]);
1884         }
1885         break;
1886     case INDEX_op_or_i64:
1887         if (const_args[2]) {
1888             tgen64_ori(s, args[0], args[2]);
1889         } else {
1890             tcg_out_insn(s, RRE, OGR, args[0], args[2]);
1891         }
1892         break;
1893     case INDEX_op_xor_i64:
1894         if (const_args[2]) {
1895             tgen64_xori(s, args[0], args[2]);
1896         } else {
1897             tcg_out_insn(s, RRE, XGR, args[0], args[2]);
1898         }
1899         break;
1900
1901     case INDEX_op_neg_i64:
1902         tcg_out_insn(s, RRE, LCGR, args[0], args[1]);
1903         break;
1904     case INDEX_op_bswap64_i64:
1905         tcg_out_insn(s, RRE, LRVGR, args[0], args[1]);
1906         break;
1907
1908     case INDEX_op_mul_i64:
1909         if (const_args[2]) {
1910             if (args[2] == (int16_t)args[2]) {
1911                 tcg_out_insn(s, RI, MGHI, args[0], args[2]);
1912             } else {
1913                 tcg_out_insn(s, RIL, MSGFI, args[0], args[2]);
1914             }
1915         } else {
1916             tcg_out_insn(s, RRE, MSGR, args[0], args[2]);
1917         }
1918         break;
1919
1920     case INDEX_op_div2_i64:
1921         /* ??? We get an unnecessary sign-extension of the dividend
1922            into R3 with this definition, but as we do in fact always
1923            produce both quotient and remainder using INDEX_op_div_i64
1924            instead requires jumping through even more hoops.  */
1925         tcg_out_insn(s, RRE, DSGR, TCG_REG_R2, args[4]);
1926         break;
1927     case INDEX_op_divu2_i64:
1928         tcg_out_insn(s, RRE, DLGR, TCG_REG_R2, args[4]);
1929         break;
1930     case INDEX_op_mulu2_i64:
1931         tcg_out_insn(s, RRE, MLGR, TCG_REG_R2, args[3]);
1932         break;
1933
1934     case INDEX_op_shl_i64:
1935         op = RSY_SLLG;
1936     do_shift64:
1937         if (const_args[2]) {
1938             tcg_out_sh64(s, op, args[0], args[1], TCG_REG_NONE, args[2]);
1939         } else {
1940             tcg_out_sh64(s, op, args[0], args[1], args[2], 0);
1941         }
1942         break;
1943     case INDEX_op_shr_i64:
1944         op = RSY_SRLG;
1945         goto do_shift64;
1946     case INDEX_op_sar_i64:
1947         op = RSY_SRAG;
1948         goto do_shift64;
1949
1950     case INDEX_op_rotl_i64:
1951         if (const_args[2]) {
1952             tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
1953                          TCG_REG_NONE, args[2]);
1954         } else {
1955             tcg_out_sh64(s, RSY_RLLG, args[0], args[1], args[2], 0);
1956         }
1957         break;
1958     case INDEX_op_rotr_i64:
1959         if (const_args[2]) {
1960             tcg_out_sh64(s, RSY_RLLG, args[0], args[1],
1961                          TCG_REG_NONE, (64 - args[2]) & 63);
1962         } else {
1963             /* We can use the smaller 32-bit negate because only the
1964                low 6 bits are examined for the rotate.  */
1965             tcg_out_insn(s, RR, LCR, TCG_TMP0, args[2]);
1966             tcg_out_sh64(s, RSY_RLLG, args[0], args[1], TCG_TMP0, 0);
1967         }
1968         break;
1969
1970     case INDEX_op_ext8s_i64:
1971         tgen_ext8s(s, TCG_TYPE_I64, args[0], args[1]);
1972         break;
1973     case INDEX_op_ext16s_i64:
1974         tgen_ext16s(s, TCG_TYPE_I64, args[0], args[1]);
1975         break;
1976     case INDEX_op_ext32s_i64:
1977         tgen_ext32s(s, args[0], args[1]);
1978         break;
1979     case INDEX_op_ext8u_i64:
1980         tgen_ext8u(s, TCG_TYPE_I64, args[0], args[1]);
1981         break;
1982     case INDEX_op_ext16u_i64:
1983         tgen_ext16u(s, TCG_TYPE_I64, args[0], args[1]);
1984         break;
1985     case INDEX_op_ext32u_i64:
1986         tgen_ext32u(s, args[0], args[1]);
1987         break;
1988
1989     case INDEX_op_add2_i64:
1990         /* ??? Make use of ALGFI and SLGFI.  */
1991         tcg_out_insn(s, RRE, ALGR, args[0], args[4]);
1992         tcg_out_insn(s, RRE, ALCGR, args[1], args[5]);
1993         break;
1994     case INDEX_op_sub2_i64:
1995         /* ??? Make use of ALGFI and SLGFI.  */
1996         tcg_out_insn(s, RRE, SLGR, args[0], args[4]);
1997         tcg_out_insn(s, RRE, SLBGR, args[1], args[5]);
1998         break;
1999
2000     case INDEX_op_brcond_i64:
2001         tgen_brcond(s, TCG_TYPE_I64, args[2], args[0],
2002                     args[1], const_args[1], args[3]);
2003         break;
2004     case INDEX_op_setcond_i64:
2005         tgen_setcond(s, TCG_TYPE_I64, args[3], args[0], args[1],
2006                      args[2], const_args[2]);
2007         break;
2008     case INDEX_op_movcond_i64:
2009         tgen_movcond(s, TCG_TYPE_I64, args[5], args[0], args[1],
2010                      args[2], const_args[2], args[3]);
2011         break;
2012
2013     OP_32_64(deposit):
2014         tgen_deposit(s, args[0], args[2], args[3], args[4]);
2015         break;
2016
2017     case INDEX_op_mov_i32:  /* Always emitted via tcg_out_mov.  */
2018     case INDEX_op_mov_i64:
2019     case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi.  */
2020     case INDEX_op_movi_i64:
2021     case INDEX_op_call:     /* Always emitted via tcg_out_call.  */
2022     default:
2023         tcg_abort();
2024     }
2025 }
2026
2027 static const TCGTargetOpDef s390_op_defs[] = {
2028     { INDEX_op_exit_tb, { } },
2029     { INDEX_op_goto_tb, { } },
2030     { INDEX_op_br, { } },
2031
2032     { INDEX_op_ld8u_i32, { "r", "r" } },
2033     { INDEX_op_ld8s_i32, { "r", "r" } },
2034     { INDEX_op_ld16u_i32, { "r", "r" } },
2035     { INDEX_op_ld16s_i32, { "r", "r" } },
2036     { INDEX_op_ld_i32, { "r", "r" } },
2037     { INDEX_op_st8_i32, { "r", "r" } },
2038     { INDEX_op_st16_i32, { "r", "r" } },
2039     { INDEX_op_st_i32, { "r", "r" } },
2040
2041     { INDEX_op_add_i32, { "r", "r", "ri" } },
2042     { INDEX_op_sub_i32, { "r", "0", "ri" } },
2043     { INDEX_op_mul_i32, { "r", "0", "rK" } },
2044
2045     { INDEX_op_div2_i32, { "b", "a", "0", "1", "r" } },
2046     { INDEX_op_divu2_i32, { "b", "a", "0", "1", "r" } },
2047
2048     { INDEX_op_and_i32, { "r", "0", "ri" } },
2049     { INDEX_op_or_i32, { "r", "0", "rO" } },
2050     { INDEX_op_xor_i32, { "r", "0", "rX" } },
2051
2052     { INDEX_op_neg_i32, { "r", "r" } },
2053
2054     { INDEX_op_shl_i32, { "r", "0", "Ri" } },
2055     { INDEX_op_shr_i32, { "r", "0", "Ri" } },
2056     { INDEX_op_sar_i32, { "r", "0", "Ri" } },
2057
2058     { INDEX_op_rotl_i32, { "r", "r", "Ri" } },
2059     { INDEX_op_rotr_i32, { "r", "r", "Ri" } },
2060
2061     { INDEX_op_ext8s_i32, { "r", "r" } },
2062     { INDEX_op_ext8u_i32, { "r", "r" } },
2063     { INDEX_op_ext16s_i32, { "r", "r" } },
2064     { INDEX_op_ext16u_i32, { "r", "r" } },
2065
2066     { INDEX_op_bswap16_i32, { "r", "r" } },
2067     { INDEX_op_bswap32_i32, { "r", "r" } },
2068
2069     { INDEX_op_add2_i32, { "r", "r", "0", "1", "r", "r" } },
2070     { INDEX_op_sub2_i32, { "r", "r", "0", "1", "r", "r" } },
2071
2072     { INDEX_op_brcond_i32, { "r", "rC" } },
2073     { INDEX_op_setcond_i32, { "r", "r", "rC" } },
2074     { INDEX_op_movcond_i32, { "r", "r", "rC", "r", "0" } },
2075     { INDEX_op_deposit_i32, { "r", "0", "r" } },
2076
2077     { INDEX_op_qemu_ld_i32, { "r", "L" } },
2078     { INDEX_op_qemu_ld_i64, { "r", "L" } },
2079     { INDEX_op_qemu_st_i32, { "L", "L" } },
2080     { INDEX_op_qemu_st_i64, { "L", "L" } },
2081
2082     { INDEX_op_ld8u_i64, { "r", "r" } },
2083     { INDEX_op_ld8s_i64, { "r", "r" } },
2084     { INDEX_op_ld16u_i64, { "r", "r" } },
2085     { INDEX_op_ld16s_i64, { "r", "r" } },
2086     { INDEX_op_ld32u_i64, { "r", "r" } },
2087     { INDEX_op_ld32s_i64, { "r", "r" } },
2088     { INDEX_op_ld_i64, { "r", "r" } },
2089
2090     { INDEX_op_st8_i64, { "r", "r" } },
2091     { INDEX_op_st16_i64, { "r", "r" } },
2092     { INDEX_op_st32_i64, { "r", "r" } },
2093     { INDEX_op_st_i64, { "r", "r" } },
2094
2095     { INDEX_op_add_i64, { "r", "r", "ri" } },
2096     { INDEX_op_sub_i64, { "r", "0", "ri" } },
2097     { INDEX_op_mul_i64, { "r", "0", "rK" } },
2098
2099     { INDEX_op_div2_i64, { "b", "a", "0", "1", "r" } },
2100     { INDEX_op_divu2_i64, { "b", "a", "0", "1", "r" } },
2101     { INDEX_op_mulu2_i64, { "b", "a", "0", "r" } },
2102
2103     { INDEX_op_and_i64, { "r", "0", "ri" } },
2104     { INDEX_op_or_i64, { "r", "0", "rO" } },
2105     { INDEX_op_xor_i64, { "r", "0", "rX" } },
2106
2107     { INDEX_op_neg_i64, { "r", "r" } },
2108
2109     { INDEX_op_shl_i64, { "r", "r", "Ri" } },
2110     { INDEX_op_shr_i64, { "r", "r", "Ri" } },
2111     { INDEX_op_sar_i64, { "r", "r", "Ri" } },
2112
2113     { INDEX_op_rotl_i64, { "r", "r", "Ri" } },
2114     { INDEX_op_rotr_i64, { "r", "r", "Ri" } },
2115
2116     { INDEX_op_ext8s_i64, { "r", "r" } },
2117     { INDEX_op_ext8u_i64, { "r", "r" } },
2118     { INDEX_op_ext16s_i64, { "r", "r" } },
2119     { INDEX_op_ext16u_i64, { "r", "r" } },
2120     { INDEX_op_ext32s_i64, { "r", "r" } },
2121     { INDEX_op_ext32u_i64, { "r", "r" } },
2122
2123     { INDEX_op_bswap16_i64, { "r", "r" } },
2124     { INDEX_op_bswap32_i64, { "r", "r" } },
2125     { INDEX_op_bswap64_i64, { "r", "r" } },
2126
2127     { INDEX_op_add2_i64, { "r", "r", "0", "1", "r", "r" } },
2128     { INDEX_op_sub2_i64, { "r", "r", "0", "1", "r", "r" } },
2129
2130     { INDEX_op_brcond_i64, { "r", "rC" } },
2131     { INDEX_op_setcond_i64, { "r", "r", "rC" } },
2132     { INDEX_op_movcond_i64, { "r", "r", "rC", "r", "0" } },
2133     { INDEX_op_deposit_i64, { "r", "0", "r" } },
2134
2135     { -1 },
2136 };
2137
2138 static void query_facilities(void)
2139 {
2140     unsigned long hwcap = qemu_getauxval(AT_HWCAP);
2141
2142     /* Is STORE FACILITY LIST EXTENDED available?  Honestly, I believe this
2143        is present on all 64-bit systems, but let's check for it anyway.  */
2144     if (hwcap & HWCAP_S390_STFLE) {
2145         register int r0 __asm__("0");
2146         register void *r1 __asm__("1");
2147
2148         /* stfle 0(%r1) */
2149         r1 = &facilities;
2150         asm volatile(".word 0xb2b0,0x1000"
2151                      : "=r"(r0) : "0"(0), "r"(r1) : "memory", "cc");
2152     }
2153 }
2154
2155 static void tcg_target_init(TCGContext *s)
2156 {
2157     query_facilities();
2158
2159     tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffff);
2160     tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, 0xffff);
2161
2162     tcg_regset_clear(tcg_target_call_clobber_regs);
2163     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R0);
2164     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R1);
2165     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R2);
2166     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R3);
2167     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R4);
2168     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R5);
2169     /* The r6 register is technically call-saved, but it's also a parameter
2170        register, so it can get killed by setup for the qemu_st helper.  */
2171     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R6);
2172     /* The return register can be considered call-clobbered.  */
2173     tcg_regset_set_reg(tcg_target_call_clobber_regs, TCG_REG_R14);
2174
2175     tcg_regset_clear(s->reserved_regs);
2176     tcg_regset_set_reg(s->reserved_regs, TCG_TMP0);
2177     /* XXX many insns can't be used with R0, so we better avoid it for now */
2178     tcg_regset_set_reg(s->reserved_regs, TCG_REG_R0);
2179     tcg_regset_set_reg(s->reserved_regs, TCG_REG_CALL_STACK);
2180
2181     tcg_add_target_add_op_defs(s390_op_defs);
2182 }
2183
2184 #define FRAME_SIZE  ((int)(TCG_TARGET_CALL_STACK_OFFSET          \
2185                            + TCG_STATIC_CALL_ARGS_SIZE           \
2186                            + CPU_TEMP_BUF_NLONGS * sizeof(long)))
2187
2188 static void tcg_target_qemu_prologue(TCGContext *s)
2189 {
2190     /* stmg %r6,%r15,48(%r15) (save registers) */
2191     tcg_out_insn(s, RXY, STMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15, 48);
2192
2193     /* aghi %r15,-frame_size */
2194     tcg_out_insn(s, RI, AGHI, TCG_REG_R15, -FRAME_SIZE);
2195
2196     tcg_set_frame(s, TCG_REG_CALL_STACK,
2197                   TCG_STATIC_CALL_ARGS_SIZE + TCG_TARGET_CALL_STACK_OFFSET,
2198                   CPU_TEMP_BUF_NLONGS * sizeof(long));
2199
2200     if (GUEST_BASE >= 0x80000) {
2201         tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, GUEST_BASE);
2202         tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
2203     }
2204
2205     tcg_out_mov(s, TCG_TYPE_PTR, TCG_AREG0, tcg_target_call_iarg_regs[0]);
2206     /* br %r3 (go to TB) */
2207     tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, tcg_target_call_iarg_regs[1]);
2208
2209     tb_ret_addr = s->code_ptr;
2210
2211     /* lmg %r6,%r15,fs+48(%r15) (restore registers) */
2212     tcg_out_insn(s, RXY, LMG, TCG_REG_R6, TCG_REG_R15, TCG_REG_R15,
2213                  FRAME_SIZE + 48);
2214
2215     /* br %r14 (return) */
2216     tcg_out_insn(s, RR, BCR, S390_CC_ALWAYS, TCG_REG_R14);
2217 }
2218
2219 typedef struct {
2220     DebugFrameCIE cie;
2221     DebugFrameFDEHeader fde;
2222     uint8_t fde_def_cfa[4];
2223     uint8_t fde_reg_ofs[18];
2224 } DebugFrame;
2225
2226 /* We're expecting a 2 byte uleb128 encoded value.  */
2227 QEMU_BUILD_BUG_ON(FRAME_SIZE >= (1 << 14));
2228
2229 #define ELF_HOST_MACHINE  EM_S390
2230
2231 static DebugFrame debug_frame = {
2232     .cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
2233     .cie.id = -1,
2234     .cie.version = 1,
2235     .cie.code_align = 1,
2236     .cie.data_align = 8,                /* sleb128 8 */
2237     .cie.return_column = TCG_REG_R14,
2238
2239     /* Total FDE size does not include the "len" member.  */
2240     .fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, fde.cie_offset),
2241
2242     .fde_def_cfa = {
2243         12, TCG_REG_CALL_STACK,         /* DW_CFA_def_cfa %r15, ... */
2244         (FRAME_SIZE & 0x7f) | 0x80,     /* ... uleb128 FRAME_SIZE */
2245         (FRAME_SIZE >> 7)
2246     },
2247     .fde_reg_ofs = {
2248         0x86, 6,                        /* DW_CFA_offset, %r6, 48 */
2249         0x87, 7,                        /* DW_CFA_offset, %r7, 56 */
2250         0x88, 8,                        /* DW_CFA_offset, %r8, 64 */
2251         0x89, 9,                        /* DW_CFA_offset, %r92, 72 */
2252         0x8a, 10,                       /* DW_CFA_offset, %r10, 80 */
2253         0x8b, 11,                       /* DW_CFA_offset, %r11, 88 */
2254         0x8c, 12,                       /* DW_CFA_offset, %r12, 96 */
2255         0x8d, 13,                       /* DW_CFA_offset, %r13, 104 */
2256         0x8e, 14,                       /* DW_CFA_offset, %r14, 112 */
2257     }
2258 };
2259
2260 void tcg_register_jit(void *buf, size_t buf_size)
2261 {
2262     debug_frame.fde.func_start = (uintptr_t)buf;
2263     debug_frame.fde.func_len = buf_size;
2264
2265     tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
2266 }
This page took 0.18026 seconds and 4 git commands to generate.