]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/sparc/tcg-target.c
Merge remote-tracking branch 'remotes/agraf/tags/signed-s390-for-upstream' into staging
[mirror_qemu.git] / tcg / sparc / tcg-target.c
CommitLineData
8289b279
BS
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
3cf246f0
RH
25#include "tcg-be-null.h"
26
d4a9eb1f 27#ifndef NDEBUG
8289b279
BS
28static const char * const tcg_target_reg_names[TCG_TARGET_NB_REGS] = {
29 "%g0",
30 "%g1",
31 "%g2",
32 "%g3",
33 "%g4",
34 "%g5",
35 "%g6",
36 "%g7",
37 "%o0",
38 "%o1",
39 "%o2",
40 "%o3",
41 "%o4",
42 "%o5",
43 "%o6",
44 "%o7",
45 "%l0",
46 "%l1",
47 "%l2",
48 "%l3",
49 "%l4",
50 "%l5",
51 "%l6",
52 "%l7",
53 "%i0",
54 "%i1",
55 "%i2",
56 "%i3",
57 "%i4",
58 "%i5",
59 "%i6",
60 "%i7",
61};
d4a9eb1f 62#endif
8289b279 63
9f44adc5
RH
64#ifdef __arch64__
65# define SPARC64 1
66#else
67# define SPARC64 0
68#endif
69
34b1a49c
RH
70/* Note that sparcv8plus can only hold 64 bit quantities in %g and %o
71 registers. These are saved manually by the kernel in full 64-bit
72 slots. The %i and %l registers are saved by the register window
73 mechanism, which only allocates space for 32 bits. Given that this
74 window spill/fill can happen on any signal, we must consider the
75 high bits of the %i and %l registers garbage at all times. */
76#if SPARC64
77# define ALL_64 0xffffffffu
78#else
79# define ALL_64 0xffffu
80#endif
81
375816f8
RH
82/* Define some temporary registers. T2 is used for constant generation. */
83#define TCG_REG_T1 TCG_REG_G1
84#define TCG_REG_T2 TCG_REG_O7
85
c6f7e4fb 86#ifdef CONFIG_USE_GUEST_BASE
375816f8 87# define TCG_GUEST_BASE_REG TCG_REG_I5
c6f7e4fb
RH
88#else
89# define TCG_GUEST_BASE_REG TCG_REG_G0
90#endif
e141ab52 91
0954d0d9 92static const int tcg_target_reg_alloc_order[] = {
8289b279
BS
93 TCG_REG_L0,
94 TCG_REG_L1,
95 TCG_REG_L2,
96 TCG_REG_L3,
97 TCG_REG_L4,
98 TCG_REG_L5,
99 TCG_REG_L6,
100 TCG_REG_L7,
26adfb75 101
8289b279
BS
102 TCG_REG_I0,
103 TCG_REG_I1,
104 TCG_REG_I2,
105 TCG_REG_I3,
106 TCG_REG_I4,
375816f8 107 TCG_REG_I5,
26adfb75
RH
108
109 TCG_REG_G2,
110 TCG_REG_G3,
111 TCG_REG_G4,
112 TCG_REG_G5,
113
114 TCG_REG_O0,
115 TCG_REG_O1,
116 TCG_REG_O2,
117 TCG_REG_O3,
118 TCG_REG_O4,
119 TCG_REG_O5,
8289b279
BS
120};
121
122static const int tcg_target_call_iarg_regs[6] = {
123 TCG_REG_O0,
124 TCG_REG_O1,
125 TCG_REG_O2,
126 TCG_REG_O3,
127 TCG_REG_O4,
128 TCG_REG_O5,
129};
130
26a74ae3 131static const int tcg_target_call_oarg_regs[] = {
8289b279 132 TCG_REG_O0,
e141ab52
BS
133 TCG_REG_O1,
134 TCG_REG_O2,
135 TCG_REG_O3,
8289b279
BS
136};
137
8289b279
BS
138#define INSN_OP(x) ((x) << 30)
139#define INSN_OP2(x) ((x) << 22)
140#define INSN_OP3(x) ((x) << 19)
141#define INSN_OPF(x) ((x) << 5)
142#define INSN_RD(x) ((x) << 25)
143#define INSN_RS1(x) ((x) << 14)
144#define INSN_RS2(x) (x)
8384dd67 145#define INSN_ASI(x) ((x) << 5)
8289b279 146
203342d8 147#define INSN_IMM10(x) ((1 << 13) | ((x) & 0x3ff))
dbfe80e1 148#define INSN_IMM11(x) ((1 << 13) | ((x) & 0x7ff))
8289b279 149#define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
ab1339b9 150#define INSN_OFF16(x) ((((x) >> 2) & 0x3fff) | ((((x) >> 16) & 3) << 20))
1da92db2 151#define INSN_OFF19(x) (((x) >> 2) & 0x07ffff)
a115f3ea 152#define INSN_COND(x) ((x) << 25)
8289b279 153
cf7c2ca5
BS
154#define COND_N 0x0
155#define COND_E 0x1
156#define COND_LE 0x2
157#define COND_L 0x3
158#define COND_LEU 0x4
159#define COND_CS 0x5
160#define COND_NEG 0x6
161#define COND_VS 0x7
b3db8758 162#define COND_A 0x8
cf7c2ca5
BS
163#define COND_NE 0x9
164#define COND_G 0xa
165#define COND_GE 0xb
166#define COND_GU 0xc
167#define COND_CC 0xd
168#define COND_POS 0xe
169#define COND_VC 0xf
a115f3ea 170#define BA (INSN_OP(0) | INSN_COND(COND_A) | INSN_OP2(0x2))
8289b279 171
ab1339b9
RH
172#define RCOND_Z 1
173#define RCOND_LEZ 2
174#define RCOND_LZ 3
175#define RCOND_NZ 5
176#define RCOND_GZ 6
177#define RCOND_GEZ 7
178
dbfe80e1
RH
179#define MOVCC_ICC (1 << 18)
180#define MOVCC_XCC (1 << 18 | 1 << 12)
181
a115f3ea
RH
182#define BPCC_ICC 0
183#define BPCC_XCC (2 << 20)
184#define BPCC_PT (1 << 19)
185#define BPCC_PN 0
186#define BPCC_A (1 << 29)
187
ab1339b9
RH
188#define BPR_PT BPCC_PT
189
8289b279 190#define ARITH_ADD (INSN_OP(2) | INSN_OP3(0x00))
7a3766f3 191#define ARITH_ADDCC (INSN_OP(2) | INSN_OP3(0x10))
8289b279 192#define ARITH_AND (INSN_OP(2) | INSN_OP3(0x01))
dc69960d 193#define ARITH_ANDN (INSN_OP(2) | INSN_OP3(0x05))
8289b279 194#define ARITH_OR (INSN_OP(2) | INSN_OP3(0x02))
9a7f3228 195#define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
be6551b1 196#define ARITH_ORN (INSN_OP(2) | INSN_OP3(0x06))
8289b279 197#define ARITH_XOR (INSN_OP(2) | INSN_OP3(0x03))
f5ef6aac
BS
198#define ARITH_SUB (INSN_OP(2) | INSN_OP3(0x04))
199#define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
c470b663
RH
200#define ARITH_ADDC (INSN_OP(2) | INSN_OP3(0x08))
201#define ARITH_SUBC (INSN_OP(2) | INSN_OP3(0x0c))
8289b279 202#define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
f4c16661 203#define ARITH_SMUL (INSN_OP(2) | INSN_OP3(0x0b))
8289b279
BS
204#define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
205#define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
206#define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
207#define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
208#define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
dbfe80e1 209#define ARITH_MOVCC (INSN_OP(2) | INSN_OP3(0x2c))
203342d8 210#define ARITH_MOVR (INSN_OP(2) | INSN_OP3(0x2f))
8289b279 211
90379ca8 212#define ARITH_ADDXC (INSN_OP(2) | INSN_OP3(0x36) | INSN_OPF(0x11))
de8301e5 213#define ARITH_UMULXHI (INSN_OP(2) | INSN_OP3(0x36) | INSN_OPF(0x16))
90379ca8 214
8289b279
BS
215#define SHIFT_SLL (INSN_OP(2) | INSN_OP3(0x25))
216#define SHIFT_SRL (INSN_OP(2) | INSN_OP3(0x26))
217#define SHIFT_SRA (INSN_OP(2) | INSN_OP3(0x27))
218
219#define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
220#define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
221#define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
222
7a3766f3 223#define RDY (INSN_OP(2) | INSN_OP3(0x28) | INSN_RS1(0))
583d1215 224#define WRY (INSN_OP(2) | INSN_OP3(0x30) | INSN_RD(0))
8289b279 225#define JMPL (INSN_OP(2) | INSN_OP3(0x38))
8b66eefe 226#define RETURN (INSN_OP(2) | INSN_OP3(0x39))
8289b279
BS
227#define SAVE (INSN_OP(2) | INSN_OP3(0x3c))
228#define RESTORE (INSN_OP(2) | INSN_OP3(0x3d))
229#define SETHI (INSN_OP(0) | INSN_OP2(0x4))
230#define CALL INSN_OP(1)
231#define LDUB (INSN_OP(3) | INSN_OP3(0x01))
232#define LDSB (INSN_OP(3) | INSN_OP3(0x09))
233#define LDUH (INSN_OP(3) | INSN_OP3(0x02))
234#define LDSH (INSN_OP(3) | INSN_OP3(0x0a))
235#define LDUW (INSN_OP(3) | INSN_OP3(0x00))
236#define LDSW (INSN_OP(3) | INSN_OP3(0x08))
237#define LDX (INSN_OP(3) | INSN_OP3(0x0b))
238#define STB (INSN_OP(3) | INSN_OP3(0x05))
239#define STH (INSN_OP(3) | INSN_OP3(0x06))
240#define STW (INSN_OP(3) | INSN_OP3(0x04))
241#define STX (INSN_OP(3) | INSN_OP3(0x0e))
8384dd67
BS
242#define LDUBA (INSN_OP(3) | INSN_OP3(0x11))
243#define LDSBA (INSN_OP(3) | INSN_OP3(0x19))
244#define LDUHA (INSN_OP(3) | INSN_OP3(0x12))
245#define LDSHA (INSN_OP(3) | INSN_OP3(0x1a))
246#define LDUWA (INSN_OP(3) | INSN_OP3(0x10))
247#define LDSWA (INSN_OP(3) | INSN_OP3(0x18))
248#define LDXA (INSN_OP(3) | INSN_OP3(0x1b))
249#define STBA (INSN_OP(3) | INSN_OP3(0x15))
250#define STHA (INSN_OP(3) | INSN_OP3(0x16))
251#define STWA (INSN_OP(3) | INSN_OP3(0x14))
252#define STXA (INSN_OP(3) | INSN_OP3(0x1e))
253
254#ifndef ASI_PRIMARY_LITTLE
255#define ASI_PRIMARY_LITTLE 0x88
256#endif
8289b279 257
a0ce341a
RH
258#define LDUH_LE (LDUHA | INSN_ASI(ASI_PRIMARY_LITTLE))
259#define LDSH_LE (LDSHA | INSN_ASI(ASI_PRIMARY_LITTLE))
260#define LDUW_LE (LDUWA | INSN_ASI(ASI_PRIMARY_LITTLE))
261#define LDSW_LE (LDSWA | INSN_ASI(ASI_PRIMARY_LITTLE))
262#define LDX_LE (LDXA | INSN_ASI(ASI_PRIMARY_LITTLE))
263
264#define STH_LE (STHA | INSN_ASI(ASI_PRIMARY_LITTLE))
265#define STW_LE (STWA | INSN_ASI(ASI_PRIMARY_LITTLE))
266#define STX_LE (STXA | INSN_ASI(ASI_PRIMARY_LITTLE))
267
90379ca8
RH
268#ifndef use_vis3_instructions
269bool use_vis3_instructions;
270#endif
271
425532d7 272static inline int check_fit_i64(int64_t val, unsigned int bits)
a115f3ea 273{
425532d7 274 return val == sextract64(val, 0, bits);
a115f3ea
RH
275}
276
425532d7 277static inline int check_fit_i32(int32_t val, unsigned int bits)
a115f3ea 278{
425532d7 279 return val == sextract32(val, 0, bits);
a115f3ea
RH
280}
281
425532d7
RH
282#define check_fit_tl check_fit_i64
283#if SPARC64
284# define check_fit_ptr check_fit_i64
285#else
286# define check_fit_ptr check_fit_i32
287#endif
288
abce5964 289static void patch_reloc(tcg_insn_unit *code_ptr, int type,
2ba7fae2 290 intptr_t value, intptr_t addend)
a115f3ea
RH
291{
292 uint32_t insn;
abce5964
RH
293
294 assert(addend == 0);
295 value = tcg_ptr_byte_diff((tcg_insn_unit *)value, code_ptr);
296
a115f3ea 297 switch (type) {
ab1339b9 298 case R_SPARC_WDISP16:
425532d7 299 if (!check_fit_ptr(value >> 2, 16)) {
ab1339b9
RH
300 tcg_abort();
301 }
abce5964 302 insn = *code_ptr;
ab1339b9
RH
303 insn &= ~INSN_OFF16(-1);
304 insn |= INSN_OFF16(value);
abce5964 305 *code_ptr = insn;
ab1339b9 306 break;
a115f3ea 307 case R_SPARC_WDISP19:
425532d7 308 if (!check_fit_ptr(value >> 2, 19)) {
a115f3ea
RH
309 tcg_abort();
310 }
abce5964 311 insn = *code_ptr;
a115f3ea
RH
312 insn &= ~INSN_OFF19(-1);
313 insn |= INSN_OFF19(value);
abce5964 314 *code_ptr = insn;
a115f3ea
RH
315 break;
316 default:
317 tcg_abort();
318 }
319}
320
321/* parse target specific constraints */
322static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)
323{
324 const char *ct_str;
325
326 ct_str = *pct_str;
327 switch (ct_str[0]) {
328 case 'r':
329 ct->ct |= TCG_CT_REG;
330 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
331 break;
34b1a49c 332 case 'R':
a115f3ea 333 ct->ct |= TCG_CT_REG;
34b1a49c
RH
334 tcg_regset_set32(ct->u.regs, 0, ALL_64);
335 break;
336 case 'A': /* qemu_ld/st address constraint */
337 ct->ct |= TCG_CT_REG;
338 tcg_regset_set32(ct->u.regs, 0,
339 TARGET_LONG_BITS == 64 ? ALL_64 : 0xffffffff);
340 reserve_helpers:
a115f3ea
RH
341 tcg_regset_reset_reg(ct->u.regs, TCG_REG_O0);
342 tcg_regset_reset_reg(ct->u.regs, TCG_REG_O1);
343 tcg_regset_reset_reg(ct->u.regs, TCG_REG_O2);
344 break;
34b1a49c
RH
345 case 's': /* qemu_st data 32-bit constraint */
346 ct->ct |= TCG_CT_REG;
347 tcg_regset_set32(ct->u.regs, 0, 0xffffffff);
348 goto reserve_helpers;
349 case 'S': /* qemu_st data 64-bit constraint */
350 ct->ct |= TCG_CT_REG;
351 tcg_regset_set32(ct->u.regs, 0, ALL_64);
352 goto reserve_helpers;
a115f3ea
RH
353 case 'I':
354 ct->ct |= TCG_CT_CONST_S11;
355 break;
356 case 'J':
357 ct->ct |= TCG_CT_CONST_S13;
358 break;
359 case 'Z':
360 ct->ct |= TCG_CT_CONST_ZERO;
361 break;
362 default:
363 return -1;
364 }
365 ct_str++;
366 *pct_str = ct_str;
367 return 0;
368}
369
370/* test if a constant matches the constraint */
f6c6afc1 371static inline int tcg_target_const_match(tcg_target_long val, TCGType type,
a115f3ea
RH
372 const TCGArgConstraint *arg_ct)
373{
374 int ct = arg_ct->ct;
375
376 if (ct & TCG_CT_CONST) {
377 return 1;
4b304cfa
RH
378 }
379
380 if (type == TCG_TYPE_I32) {
381 val = (int32_t)val;
382 }
383
384 if ((ct & TCG_CT_CONST_ZERO) && val == 0) {
a115f3ea
RH
385 return 1;
386 } else if ((ct & TCG_CT_CONST_S11) && check_fit_tl(val, 11)) {
387 return 1;
388 } else if ((ct & TCG_CT_CONST_S13) && check_fit_tl(val, 13)) {
389 return 1;
390 } else {
391 return 0;
392 }
393}
394
35e2da15
RH
395static inline void tcg_out_arith(TCGContext *s, TCGReg rd, TCGReg rs1,
396 TCGReg rs2, int op)
26cc915c 397{
35e2da15 398 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) | INSN_RS2(rs2));
26cc915c
BS
399}
400
35e2da15
RH
401static inline void tcg_out_arithi(TCGContext *s, TCGReg rd, TCGReg rs1,
402 int32_t offset, int op)
26cc915c 403{
35e2da15 404 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1) | INSN_IMM13(offset));
26cc915c
BS
405}
406
35e2da15
RH
407static void tcg_out_arithc(TCGContext *s, TCGReg rd, TCGReg rs1,
408 int32_t val2, int val2const, int op)
ba225198
RH
409{
410 tcg_out32(s, op | INSN_RD(rd) | INSN_RS1(rs1)
411 | (val2const ? INSN_IMM13(val2) : INSN_RS2(val2)));
412}
413
2a534aff
RH
414static inline void tcg_out_mov(TCGContext *s, TCGType type,
415 TCGReg ret, TCGReg arg)
8289b279 416{
dda73c78
RH
417 if (ret != arg) {
418 tcg_out_arith(s, ret, arg, TCG_REG_G0, ARITH_OR);
419 }
26cc915c
BS
420}
421
35e2da15 422static inline void tcg_out_sethi(TCGContext *s, TCGReg ret, uint32_t arg)
26cc915c
BS
423{
424 tcg_out32(s, SETHI | INSN_RD(ret) | ((arg & 0xfffffc00) >> 10));
8289b279
BS
425}
426
35e2da15 427static inline void tcg_out_movi_imm13(TCGContext *s, TCGReg ret, int32_t arg)
b101234a
BS
428{
429 tcg_out_arithi(s, ret, TCG_REG_G0, arg, ARITH_OR);
430}
431
a9c7d27b
RH
432static void tcg_out_movi(TCGContext *s, TCGType type,
433 TCGReg ret, tcg_target_long arg)
8289b279 434{
425532d7 435 tcg_target_long hi, lo = (int32_t)arg;
a9c7d27b 436
035b2398
RH
437 /* Make sure we test 32-bit constants for imm13 properly. */
438 if (type == TCG_TYPE_I32) {
439 arg = lo;
440 }
441
a9c7d27b
RH
442 /* A 13-bit constant sign-extended to 64-bits. */
443 if (check_fit_tl(arg, 13)) {
b101234a 444 tcg_out_movi_imm13(s, ret, arg);
a9c7d27b 445 return;
8289b279 446 }
8289b279 447
a9c7d27b 448 /* A 32-bit constant, or 32-bit zero-extended to 64-bits. */
34b1a49c 449 if (type == TCG_TYPE_I32 || arg == (uint32_t)arg) {
a9c7d27b
RH
450 tcg_out_sethi(s, ret, arg);
451 if (arg & 0x3ff) {
452 tcg_out_arithi(s, ret, ret, arg & 0x3ff, ARITH_OR);
453 }
454 return;
455 }
456
457 /* A 32-bit constant sign-extended to 64-bits. */
425532d7 458 if (arg == lo) {
43172207
RH
459 tcg_out_sethi(s, ret, ~arg);
460 tcg_out_arithi(s, ret, ret, (arg & 0x3ff) | -0x400, ARITH_XOR);
a9c7d27b
RH
461 return;
462 }
463
464 /* A 64-bit constant decomposed into 2 32-bit pieces. */
425532d7 465 if (check_fit_i32(lo, 13)) {
34b1a49c 466 hi = (arg - lo) >> 32;
a9c7d27b
RH
467 tcg_out_movi(s, TCG_TYPE_I32, ret, hi);
468 tcg_out_arithi(s, ret, ret, 32, SHIFT_SLLX);
469 tcg_out_arithi(s, ret, ret, lo, ARITH_ADD);
43172207 470 } else {
34b1a49c 471 hi = arg >> 32;
a9c7d27b
RH
472 tcg_out_movi(s, TCG_TYPE_I32, ret, hi);
473 tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_T2, lo);
375816f8 474 tcg_out_arithi(s, ret, ret, 32, SHIFT_SLLX);
375816f8 475 tcg_out_arith(s, ret, ret, TCG_REG_T2, ARITH_OR);
6f41b777 476 }
b101234a
BS
477}
478
35e2da15
RH
479static inline void tcg_out_ldst_rr(TCGContext *s, TCGReg data, TCGReg a1,
480 TCGReg a2, int op)
8289b279 481{
a0ce341a 482 tcg_out32(s, op | INSN_RD(data) | INSN_RS1(a1) | INSN_RS2(a2));
8289b279
BS
483}
484
35e2da15
RH
485static void tcg_out_ldst(TCGContext *s, TCGReg ret, TCGReg addr,
486 intptr_t offset, int op)
8289b279 487{
425532d7 488 if (check_fit_ptr(offset, 13)) {
8289b279
BS
489 tcg_out32(s, op | INSN_RD(ret) | INSN_RS1(addr) |
490 INSN_IMM13(offset));
a0ce341a 491 } else {
375816f8
RH
492 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T1, offset);
493 tcg_out_ldst_rr(s, ret, addr, TCG_REG_T1, op);
cf7c2ca5 494 }
8289b279
BS
495}
496
2a534aff 497static inline void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret,
a05b5b9b 498 TCGReg arg1, intptr_t arg2)
8289b279 499{
a0ce341a 500 tcg_out_ldst(s, ret, arg1, arg2, (type == TCG_TYPE_I32 ? LDUW : LDX));
8289b279
BS
501}
502
2a534aff 503static inline void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg,
a05b5b9b 504 TCGReg arg1, intptr_t arg2)
8289b279 505{
a0ce341a
RH
506 tcg_out_ldst(s, arg, arg1, arg2, (type == TCG_TYPE_I32 ? STW : STX));
507}
508
35e2da15 509static void tcg_out_ld_ptr(TCGContext *s, TCGReg ret, uintptr_t arg)
a0ce341a 510{
35e2da15
RH
511 tcg_out_movi(s, TCG_TYPE_PTR, ret, arg & ~0x3ff);
512 tcg_out_ld(s, TCG_TYPE_PTR, ret, ret, arg & 0x3ff);
8289b279
BS
513}
514
35e2da15 515static inline void tcg_out_sety(TCGContext *s, TCGReg rs)
8289b279 516{
583d1215 517 tcg_out32(s, WRY | INSN_RS1(TCG_REG_G0) | INSN_RS2(rs));
8289b279
BS
518}
519
35e2da15 520static inline void tcg_out_rdy(TCGContext *s, TCGReg rd)
7a3766f3
RH
521{
522 tcg_out32(s, RDY | INSN_RD(rd));
523}
524
35e2da15
RH
525static void tcg_out_div32(TCGContext *s, TCGReg rd, TCGReg rs1,
526 int32_t val2, int val2const, int uns)
583d1215
RH
527{
528 /* Load Y with the sign/zero extension of RS1 to 64-bits. */
529 if (uns) {
530 tcg_out_sety(s, TCG_REG_G0);
531 } else {
375816f8
RH
532 tcg_out_arithi(s, TCG_REG_T1, rs1, 31, SHIFT_SRA);
533 tcg_out_sety(s, TCG_REG_T1);
583d1215
RH
534 }
535
536 tcg_out_arithc(s, rd, rs1, val2, val2const,
537 uns ? ARITH_UDIV : ARITH_SDIV);
538}
539
8289b279
BS
540static inline void tcg_out_nop(TCGContext *s)
541{
26cc915c 542 tcg_out_sethi(s, TCG_REG_G0, 0);
8289b279
BS
543}
544
0aed257f 545static const uint8_t tcg_cond_to_bcond[] = {
cf7c2ca5
BS
546 [TCG_COND_EQ] = COND_E,
547 [TCG_COND_NE] = COND_NE,
548 [TCG_COND_LT] = COND_L,
549 [TCG_COND_GE] = COND_GE,
550 [TCG_COND_LE] = COND_LE,
551 [TCG_COND_GT] = COND_G,
552 [TCG_COND_LTU] = COND_CS,
553 [TCG_COND_GEU] = COND_CC,
554 [TCG_COND_LEU] = COND_LEU,
555 [TCG_COND_GTU] = COND_GU,
556};
557
ab1339b9
RH
558static const uint8_t tcg_cond_to_rcond[] = {
559 [TCG_COND_EQ] = RCOND_Z,
560 [TCG_COND_NE] = RCOND_NZ,
561 [TCG_COND_LT] = RCOND_LZ,
562 [TCG_COND_GT] = RCOND_GZ,
563 [TCG_COND_LE] = RCOND_LEZ,
564 [TCG_COND_GE] = RCOND_GEZ
565};
566
a115f3ea
RH
567static void tcg_out_bpcc0(TCGContext *s, int scond, int flags, int off19)
568{
569 tcg_out32(s, INSN_OP(0) | INSN_OP2(1) | INSN_COND(scond) | flags | off19);
570}
571
bec16311 572static void tcg_out_bpcc(TCGContext *s, int scond, int flags, TCGLabel *l)
a115f3ea 573{
a115f3ea
RH
574 int off19;
575
576 if (l->has_value) {
abce5964 577 off19 = INSN_OFF19(tcg_pcrel_diff(s, l->u.value_ptr));
a115f3ea
RH
578 } else {
579 /* Make sure to preserve destinations during retranslation. */
abce5964 580 off19 = *s->code_ptr & INSN_OFF19(-1);
bec16311 581 tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP19, l, 0);
a115f3ea
RH
582 }
583 tcg_out_bpcc0(s, scond, flags, off19);
584}
585
35e2da15 586static void tcg_out_cmp(TCGContext *s, TCGReg c1, int32_t c2, int c2const)
56f4927e 587{
ba225198 588 tcg_out_arithc(s, TCG_REG_G0, c1, c2, c2const, ARITH_SUBCC);
56f4927e
RH
589}
590
35e2da15 591static void tcg_out_brcond_i32(TCGContext *s, TCGCond cond, TCGReg arg1,
bec16311 592 int32_t arg2, int const_arg2, TCGLabel *l)
cf7c2ca5 593{
56f4927e 594 tcg_out_cmp(s, arg1, arg2, const_arg2);
bec16311 595 tcg_out_bpcc(s, tcg_cond_to_bcond[cond], BPCC_ICC | BPCC_PT, l);
cf7c2ca5
BS
596 tcg_out_nop(s);
597}
598
35e2da15
RH
599static void tcg_out_movcc(TCGContext *s, TCGCond cond, int cc, TCGReg ret,
600 int32_t v1, int v1const)
ded37f0d
RH
601{
602 tcg_out32(s, ARITH_MOVCC | cc | INSN_RD(ret)
603 | INSN_RS1(tcg_cond_to_bcond[cond])
604 | (v1const ? INSN_IMM11(v1) : INSN_RS2(v1)));
605}
606
35e2da15
RH
607static void tcg_out_movcond_i32(TCGContext *s, TCGCond cond, TCGReg ret,
608 TCGReg c1, int32_t c2, int c2const,
609 int32_t v1, int v1const)
ded37f0d
RH
610{
611 tcg_out_cmp(s, c1, c2, c2const);
612 tcg_out_movcc(s, cond, MOVCC_ICC, ret, v1, v1const);
613}
614
35e2da15 615static void tcg_out_brcond_i64(TCGContext *s, TCGCond cond, TCGReg arg1,
bec16311 616 int32_t arg2, int const_arg2, TCGLabel *l)
1da92db2 617{
ab1339b9
RH
618 /* For 64-bit signed comparisons vs zero, we can avoid the compare. */
619 if (arg2 == 0 && !is_unsigned_cond(cond)) {
ab1339b9
RH
620 int off16;
621
622 if (l->has_value) {
abce5964 623 off16 = INSN_OFF16(tcg_pcrel_diff(s, l->u.value_ptr));
ab1339b9
RH
624 } else {
625 /* Make sure to preserve destinations during retranslation. */
abce5964 626 off16 = *s->code_ptr & INSN_OFF16(-1);
bec16311 627 tcg_out_reloc(s, s->code_ptr, R_SPARC_WDISP16, l, 0);
ab1339b9
RH
628 }
629 tcg_out32(s, INSN_OP(0) | INSN_OP2(3) | BPR_PT | INSN_RS1(arg1)
630 | INSN_COND(tcg_cond_to_rcond[cond]) | off16);
631 } else {
632 tcg_out_cmp(s, arg1, arg2, const_arg2);
bec16311 633 tcg_out_bpcc(s, tcg_cond_to_bcond[cond], BPCC_XCC | BPCC_PT, l);
ab1339b9 634 }
1da92db2
BS
635 tcg_out_nop(s);
636}
ded37f0d 637
35e2da15
RH
638static void tcg_out_movr(TCGContext *s, TCGCond cond, TCGReg ret, TCGReg c1,
639 int32_t v1, int v1const)
203342d8
RH
640{
641 tcg_out32(s, ARITH_MOVR | INSN_RD(ret) | INSN_RS1(c1)
642 | (tcg_cond_to_rcond[cond] << 10)
643 | (v1const ? INSN_IMM10(v1) : INSN_RS2(v1)));
644}
645
35e2da15
RH
646static void tcg_out_movcond_i64(TCGContext *s, TCGCond cond, TCGReg ret,
647 TCGReg c1, int32_t c2, int c2const,
648 int32_t v1, int v1const)
ded37f0d 649{
203342d8
RH
650 /* For 64-bit signed comparisons vs zero, we can avoid the compare.
651 Note that the immediate range is one bit smaller, so we must check
652 for that as well. */
653 if (c2 == 0 && !is_unsigned_cond(cond)
35e2da15 654 && (!v1const || check_fit_i32(v1, 10))) {
203342d8
RH
655 tcg_out_movr(s, cond, ret, c1, v1, v1const);
656 } else {
657 tcg_out_cmp(s, c1, c2, c2const);
658 tcg_out_movcc(s, cond, MOVCC_XCC, ret, v1, v1const);
659 }
ded37f0d 660}
1da92db2 661
35e2da15
RH
662static void tcg_out_setcond_i32(TCGContext *s, TCGCond cond, TCGReg ret,
663 TCGReg c1, int32_t c2, int c2const)
dbfe80e1 664{
c470b663 665 /* For 32-bit comparisons, we can play games with ADDC/SUBC. */
dbfe80e1 666 switch (cond) {
7d458a75
RH
667 case TCG_COND_LTU:
668 case TCG_COND_GEU:
669 /* The result of the comparison is in the carry bit. */
670 break;
671
dbfe80e1
RH
672 case TCG_COND_EQ:
673 case TCG_COND_NE:
7d458a75 674 /* For equality, we can transform to inequality vs zero. */
dbfe80e1 675 if (c2 != 0) {
321b6c05
RH
676 tcg_out_arithc(s, TCG_REG_T1, c1, c2, c2const, ARITH_XOR);
677 c2 = TCG_REG_T1;
678 } else {
679 c2 = c1;
dbfe80e1 680 }
321b6c05 681 c1 = TCG_REG_G0, c2const = 0;
7d458a75 682 cond = (cond == TCG_COND_EQ ? TCG_COND_GEU : TCG_COND_LTU);
dbfe80e1
RH
683 break;
684
685 case TCG_COND_GTU:
dbfe80e1 686 case TCG_COND_LEU:
7d458a75
RH
687 /* If we don't need to load a constant into a register, we can
688 swap the operands on GTU/LEU. There's no benefit to loading
689 the constant into a temporary register. */
690 if (!c2const || c2 == 0) {
35e2da15 691 TCGReg t = c1;
7d458a75
RH
692 c1 = c2;
693 c2 = t;
694 c2const = 0;
695 cond = tcg_swap_cond(cond);
696 break;
697 }
698 /* FALLTHRU */
dbfe80e1
RH
699
700 default:
701 tcg_out_cmp(s, c1, c2, c2const);
dbfe80e1 702 tcg_out_movi_imm13(s, ret, 0);
ded37f0d 703 tcg_out_movcc(s, cond, MOVCC_ICC, ret, 1, 1);
dbfe80e1
RH
704 return;
705 }
706
707 tcg_out_cmp(s, c1, c2, c2const);
708 if (cond == TCG_COND_LTU) {
c470b663 709 tcg_out_arithi(s, ret, TCG_REG_G0, 0, ARITH_ADDC);
dbfe80e1 710 } else {
c470b663 711 tcg_out_arithi(s, ret, TCG_REG_G0, -1, ARITH_SUBC);
dbfe80e1
RH
712 }
713}
714
35e2da15
RH
715static void tcg_out_setcond_i64(TCGContext *s, TCGCond cond, TCGReg ret,
716 TCGReg c1, int32_t c2, int c2const)
dbfe80e1 717{
9d6a7a85
RH
718 if (use_vis3_instructions) {
719 switch (cond) {
720 case TCG_COND_NE:
721 if (c2 != 0) {
722 break;
723 }
724 c2 = c1, c2const = 0, c1 = TCG_REG_G0;
725 /* FALLTHRU */
726 case TCG_COND_LTU:
727 tcg_out_cmp(s, c1, c2, c2const);
728 tcg_out_arith(s, ret, TCG_REG_G0, TCG_REG_G0, ARITH_ADDXC);
729 return;
730 default:
731 break;
732 }
733 }
734
203342d8
RH
735 /* For 64-bit signed comparisons vs zero, we can avoid the compare
736 if the input does not overlap the output. */
737 if (c2 == 0 && !is_unsigned_cond(cond) && c1 != ret) {
738 tcg_out_movi_imm13(s, ret, 0);
739 tcg_out_movr(s, cond, ret, c1, 1, 1);
740 } else {
741 tcg_out_cmp(s, c1, c2, c2const);
742 tcg_out_movi_imm13(s, ret, 0);
743 tcg_out_movcc(s, cond, MOVCC_XCC, ret, 1, 1);
744 }
dbfe80e1 745}
4ec28e25 746
609ac1e1
RH
747static void tcg_out_addsub2_i32(TCGContext *s, TCGReg rl, TCGReg rh,
748 TCGReg al, TCGReg ah, int32_t bl, int blconst,
749 int32_t bh, int bhconst, int opl, int oph)
4ec28e25 750{
35e2da15 751 TCGReg tmp = TCG_REG_T1;
4ec28e25
RH
752
753 /* Note that the low parts are fully consumed before tmp is set. */
754 if (rl != ah && (bhconst || rl != bh)) {
755 tmp = rl;
756 }
757
758 tcg_out_arithc(s, tmp, al, bl, blconst, opl);
759 tcg_out_arithc(s, rh, ah, bh, bhconst, oph);
760 tcg_out_mov(s, TCG_TYPE_I32, rl, tmp);
761}
dbfe80e1 762
609ac1e1
RH
763static void tcg_out_addsub2_i64(TCGContext *s, TCGReg rl, TCGReg rh,
764 TCGReg al, TCGReg ah, int32_t bl, int blconst,
765 int32_t bh, int bhconst, bool is_sub)
766{
767 TCGReg tmp = TCG_REG_T1;
768
769 /* Note that the low parts are fully consumed before tmp is set. */
770 if (rl != ah && (bhconst || rl != bh)) {
771 tmp = rl;
772 }
773
774 tcg_out_arithc(s, tmp, al, bl, blconst, is_sub ? ARITH_SUBCC : ARITH_ADDCC);
775
90379ca8
RH
776 if (use_vis3_instructions && !is_sub) {
777 /* Note that ADDXC doesn't accept immediates. */
778 if (bhconst && bh != 0) {
779 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_T2, bh);
780 bh = TCG_REG_T2;
781 }
782 tcg_out_arith(s, rh, ah, bh, ARITH_ADDXC);
783 } else if (bh == TCG_REG_G0) {
609ac1e1
RH
784 /* If we have a zero, we can perform the operation in two insns,
785 with the arithmetic first, and a conditional move into place. */
786 if (rh == ah) {
787 tcg_out_arithi(s, TCG_REG_T2, ah, 1,
788 is_sub ? ARITH_SUB : ARITH_ADD);
789 tcg_out_movcc(s, TCG_COND_LTU, MOVCC_XCC, rh, TCG_REG_T2, 0);
790 } else {
791 tcg_out_arithi(s, rh, ah, 1, is_sub ? ARITH_SUB : ARITH_ADD);
792 tcg_out_movcc(s, TCG_COND_GEU, MOVCC_XCC, rh, ah, 0);
793 }
794 } else {
795 /* Otherwise adjust BH as if there is carry into T2 ... */
796 if (bhconst) {
797 tcg_out_movi(s, TCG_TYPE_I64, TCG_REG_T2, bh + (is_sub ? -1 : 1));
798 } else {
799 tcg_out_arithi(s, TCG_REG_T2, bh, 1,
800 is_sub ? ARITH_SUB : ARITH_ADD);
801 }
802 /* ... smoosh T2 back to original BH if carry is clear ... */
803 tcg_out_movcc(s, TCG_COND_GEU, MOVCC_XCC, TCG_REG_T2, bh, bhconst);
804 /* ... and finally perform the arithmetic with the new operand. */
805 tcg_out_arith(s, rh, ah, TCG_REG_T2, is_sub ? ARITH_SUB : ARITH_ADD);
806 }
807
808 tcg_out_mov(s, TCG_TYPE_I64, rl, tmp);
809}
810
4e9cf840 811static void tcg_out_call_nodelay(TCGContext *s, tcg_insn_unit *dest)
aad2f06a 812{
abce5964 813 ptrdiff_t disp = tcg_pcrel_diff(s, dest);
aad2f06a
RH
814
815 if (disp == (int32_t)disp) {
816 tcg_out32(s, CALL | (uint32_t)disp >> 2);
817 } else {
abce5964
RH
818 uintptr_t desti = (uintptr_t)dest;
819 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T1, desti & ~0xfff);
820 tcg_out_arithi(s, TCG_REG_O7, TCG_REG_T1, desti & 0xfff, JMPL);
aad2f06a
RH
821 }
822}
823
4e9cf840
RH
824static void tcg_out_call(TCGContext *s, tcg_insn_unit *dest)
825{
826 tcg_out_call_nodelay(s, dest);
827 tcg_out_nop(s);
828}
829
7ea5d725 830#ifdef CONFIG_SOFTMMU
abce5964
RH
831static tcg_insn_unit *qemu_ld_trampoline[16];
832static tcg_insn_unit *qemu_st_trampoline[16];
7ea5d725
RH
833
834static void build_trampolines(TCGContext *s)
835{
abce5964
RH
836 static void * const qemu_ld_helpers[16] = {
837 [MO_UB] = helper_ret_ldub_mmu,
838 [MO_SB] = helper_ret_ldsb_mmu,
839 [MO_LEUW] = helper_le_lduw_mmu,
840 [MO_LESW] = helper_le_ldsw_mmu,
841 [MO_LEUL] = helper_le_ldul_mmu,
842 [MO_LEQ] = helper_le_ldq_mmu,
843 [MO_BEUW] = helper_be_lduw_mmu,
844 [MO_BESW] = helper_be_ldsw_mmu,
845 [MO_BEUL] = helper_be_ldul_mmu,
846 [MO_BEQ] = helper_be_ldq_mmu,
7ea5d725 847 };
abce5964
RH
848 static void * const qemu_st_helpers[16] = {
849 [MO_UB] = helper_ret_stb_mmu,
850 [MO_LEUW] = helper_le_stw_mmu,
851 [MO_LEUL] = helper_le_stl_mmu,
852 [MO_LEQ] = helper_le_stq_mmu,
853 [MO_BEUW] = helper_be_stw_mmu,
854 [MO_BEUL] = helper_be_stl_mmu,
855 [MO_BEQ] = helper_be_stq_mmu,
7ea5d725
RH
856 };
857
858 int i;
859 TCGReg ra;
7ea5d725
RH
860
861 for (i = 0; i < 16; ++i) {
abce5964 862 if (qemu_ld_helpers[i] == NULL) {
7ea5d725
RH
863 continue;
864 }
865
866 /* May as well align the trampoline. */
abce5964 867 while ((uintptr_t)s->code_ptr & 15) {
7ea5d725 868 tcg_out_nop(s);
7ea5d725 869 }
abce5964 870 qemu_ld_trampoline[i] = s->code_ptr;
7ea5d725 871
34b1a49c
RH
872 if (SPARC64 || TARGET_LONG_BITS == 32) {
873 ra = TCG_REG_O3;
874 } else {
875 /* Install the high part of the address. */
876 tcg_out_arithi(s, TCG_REG_O1, TCG_REG_O2, 32, SHIFT_SRLX);
877 ra = TCG_REG_O4;
878 }
7ea5d725
RH
879
880 /* Set the retaddr operand. */
881 tcg_out_mov(s, TCG_TYPE_PTR, ra, TCG_REG_O7);
882 /* Set the env operand. */
883 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_O0, TCG_AREG0);
884 /* Tail call. */
4e9cf840 885 tcg_out_call_nodelay(s, qemu_ld_helpers[i]);
7ea5d725
RH
886 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_O7, ra);
887 }
888
889 for (i = 0; i < 16; ++i) {
abce5964 890 if (qemu_st_helpers[i] == NULL) {
7ea5d725
RH
891 continue;
892 }
893
894 /* May as well align the trampoline. */
abce5964 895 while ((uintptr_t)s->code_ptr & 15) {
7ea5d725 896 tcg_out_nop(s);
7ea5d725 897 }
abce5964 898 qemu_st_trampoline[i] = s->code_ptr;
7ea5d725 899
34b1a49c
RH
900 if (SPARC64) {
901 ra = TCG_REG_O4;
902 } else {
903 ra = TCG_REG_O1;
904 if (TARGET_LONG_BITS == 64) {
905 /* Install the high part of the address. */
906 tcg_out_arithi(s, ra, ra + 1, 32, SHIFT_SRLX);
907 ra += 2;
908 } else {
909 ra += 1;
910 }
911 if ((i & MO_SIZE) == MO_64) {
912 /* Install the high part of the data. */
913 tcg_out_arithi(s, ra, ra + 1, 32, SHIFT_SRLX);
914 ra += 2;
915 } else {
916 ra += 1;
917 }
918 /* Skip the mem_index argument. */
919 ra += 1;
920 }
921
7ea5d725
RH
922 /* Set the retaddr operand. */
923 if (ra >= TCG_REG_O6) {
924 tcg_out_st(s, TCG_TYPE_PTR, TCG_REG_O7, TCG_REG_CALL_STACK,
925 TCG_TARGET_CALL_STACK_OFFSET);
926 ra = TCG_REG_G1;
927 }
928 tcg_out_mov(s, TCG_TYPE_PTR, ra, TCG_REG_O7);
929 /* Set the env operand. */
930 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_O0, TCG_AREG0);
931 /* Tail call. */
4e9cf840 932 tcg_out_call_nodelay(s, qemu_st_helpers[i]);
7ea5d725
RH
933 tcg_out_mov(s, TCG_TYPE_PTR, TCG_REG_O7, ra);
934 }
935}
936#endif
937
7d551702 938/* Generate global QEMU prologue and epilogue code */
e4d58b41 939static void tcg_target_qemu_prologue(TCGContext *s)
b3db8758 940{
4c3204cb
RH
941 int tmp_buf_size, frame_size;
942
943 /* The TCG temp buffer is at the top of the frame, immediately
944 below the frame pointer. */
945 tmp_buf_size = CPU_TEMP_BUF_NLONGS * (int)sizeof(long);
946 tcg_set_frame(s, TCG_REG_I6, TCG_TARGET_STACK_BIAS - tmp_buf_size,
947 tmp_buf_size);
948
949 /* TCG_TARGET_CALL_STACK_OFFSET includes the stack bias, but is
950 otherwise the minimal frame usable by callees. */
951 frame_size = TCG_TARGET_CALL_STACK_OFFSET - TCG_TARGET_STACK_BIAS;
952 frame_size += TCG_STATIC_CALL_ARGS_SIZE + tmp_buf_size;
953 frame_size += TCG_TARGET_STACK_ALIGN - 1;
954 frame_size &= -TCG_TARGET_STACK_ALIGN;
b3db8758 955 tcg_out32(s, SAVE | INSN_RD(TCG_REG_O6) | INSN_RS1(TCG_REG_O6) |
4c3204cb 956 INSN_IMM13(-frame_size));
c6f7e4fb
RH
957
958#ifdef CONFIG_USE_GUEST_BASE
959 if (GUEST_BASE != 0) {
960 tcg_out_movi(s, TCG_TYPE_PTR, TCG_GUEST_BASE_REG, GUEST_BASE);
961 tcg_regset_set_reg(s->reserved_regs, TCG_GUEST_BASE_REG);
962 }
963#endif
964
aad2f06a 965 tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I1, 0, JMPL);
0c554161
RH
966 /* delay slot */
967 tcg_out_nop(s);
4c3204cb
RH
968
969 /* No epilogue required. We issue ret + restore directly in the TB. */
7ea5d725
RH
970
971#ifdef CONFIG_SOFTMMU
972 build_trampolines(s);
973#endif
b3db8758
BS
974}
975
f5ef6aac 976#if defined(CONFIG_SOFTMMU)
a0ce341a 977/* Perform the TLB load and compare.
bffe1431 978
a0ce341a 979 Inputs:
a8b12c10 980 ADDRLO and ADDRHI contain the possible two parts of the address.
a0ce341a
RH
981
982 MEM_INDEX and S_BITS are the memory context and log2 size of the load.
983
984 WHICH is the offset into the CPUTLBEntry structure of the slot to read.
985 This should be offsetof addr_read or addr_write.
986
987 The result of the TLB comparison is in %[ix]cc. The sanitized address
988 is in the returned register, maybe %o0. The TLB addend is in %o1. */
989
34b1a49c
RH
990static TCGReg tcg_out_tlb_load(TCGContext *s, TCGReg addr, int mem_index,
991 TCGMemOp s_bits, int which)
a0ce341a 992{
a8b12c10
RH
993 const TCGReg r0 = TCG_REG_O0;
994 const TCGReg r1 = TCG_REG_O1;
995 const TCGReg r2 = TCG_REG_O2;
a0ce341a
RH
996 int tlb_ofs;
997
d801a8f2 998 /* Shift the page number down. */
34b1a49c 999 tcg_out_arithi(s, r1, addr, TARGET_PAGE_BITS, SHIFT_SRL);
a0ce341a
RH
1000
1001 /* Mask out the page offset, except for the required alignment. */
d801a8f2
RH
1002 tcg_out_movi(s, TCG_TYPE_TL, TCG_REG_T1,
1003 TARGET_PAGE_MASK | ((1 << s_bits) - 1));
1004
1005 /* Mask the tlb index. */
1006 tcg_out_arithi(s, r1, r1, CPU_TLB_SIZE - 1, ARITH_AND);
1007
1008 /* Mask page, part 2. */
1009 tcg_out_arith(s, r0, addr, TCG_REG_T1, ARITH_AND);
a0ce341a 1010
d801a8f2
RH
1011 /* Shift the tlb index into place. */
1012 tcg_out_arithi(s, r1, r1, CPU_TLB_ENTRY_BITS, SHIFT_SLL);
a0ce341a
RH
1013
1014 /* Relative to the current ENV. */
1015 tcg_out_arith(s, r1, TCG_AREG0, r1, ARITH_ADD);
1016
1017 /* Find a base address that can load both tlb comparator and addend. */
1018 tlb_ofs = offsetof(CPUArchState, tlb_table[mem_index][0]);
425532d7 1019 if (!check_fit_ptr(tlb_ofs + sizeof(CPUTLBEntry), 13)) {
35e2da15
RH
1020 if (tlb_ofs & ~0x3ff) {
1021 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_T1, tlb_ofs & ~0x3ff);
1022 tcg_out_arith(s, r1, r1, TCG_REG_T1, ARITH_ADD);
1023 }
d801a8f2 1024 tlb_ofs &= 0x3ff;
a0ce341a
RH
1025 }
1026
1027 /* Load the tlb comparator and the addend. */
1028 tcg_out_ld(s, TCG_TYPE_TL, r2, r1, tlb_ofs + which);
1029 tcg_out_ld(s, TCG_TYPE_PTR, r1, r1, tlb_ofs+offsetof(CPUTLBEntry, addend));
1030
1031 /* subcc arg0, arg2, %g0 */
1032 tcg_out_cmp(s, r0, r2, 0);
1033
1034 /* If the guest address must be zero-extended, do so now. */
9f44adc5 1035 if (SPARC64 && TARGET_LONG_BITS == 32) {
34b1a49c 1036 tcg_out_arithi(s, r0, addr, 0, SHIFT_SRL);
a0ce341a
RH
1037 return r0;
1038 }
34b1a49c 1039 return addr;
a0ce341a
RH
1040}
1041#endif /* CONFIG_SOFTMMU */
1042
eef0d9e7
RH
1043static const int qemu_ld_opc[16] = {
1044 [MO_UB] = LDUB,
1045 [MO_SB] = LDSB,
1046
1047 [MO_BEUW] = LDUH,
1048 [MO_BESW] = LDSH,
1049 [MO_BEUL] = LDUW,
1050 [MO_BESL] = LDSW,
1051 [MO_BEQ] = LDX,
1052
1053 [MO_LEUW] = LDUH_LE,
1054 [MO_LESW] = LDSH_LE,
1055 [MO_LEUL] = LDUW_LE,
1056 [MO_LESL] = LDSW_LE,
1057 [MO_LEQ] = LDX_LE,
a0ce341a 1058};
9d0efc88 1059
eef0d9e7
RH
1060static const int qemu_st_opc[16] = {
1061 [MO_UB] = STB,
1062
1063 [MO_BEUW] = STH,
1064 [MO_BEUL] = STW,
1065 [MO_BEQ] = STX,
1066
1067 [MO_LEUW] = STH_LE,
1068 [MO_LEUL] = STW_LE,
1069 [MO_LEQ] = STX_LE,
a0ce341a 1070};
bffe1431 1071
34b1a49c
RH
1072static void tcg_out_qemu_ld(TCGContext *s, TCGReg data, TCGReg addr,
1073 TCGMemOp memop, int memi, bool is_64)
f5ef6aac 1074{
34b1a49c
RH
1075#ifdef CONFIG_SOFTMMU
1076 TCGMemOp s_bits = memop & MO_SIZE;
cab0a7ea 1077 TCGReg addrz, param;
abce5964
RH
1078 tcg_insn_unit *func;
1079 tcg_insn_unit *label_ptr;
f5ef6aac 1080
34b1a49c 1081 addrz = tcg_out_tlb_load(s, addr, memi, s_bits,
cab0a7ea 1082 offsetof(CPUTLBEntry, addr_read));
a0ce341a 1083
34b1a49c
RH
1084 /* The fast path is exactly one insn. Thus we can perform the
1085 entire TLB Hit in the (annulled) delay slot of the branch
1086 over the TLB Miss case. */
a0ce341a 1087
34b1a49c 1088 /* beq,a,pt %[xi]cc, label0 */
abce5964 1089 label_ptr = s->code_ptr;
34b1a49c
RH
1090 tcg_out_bpcc0(s, COND_E, BPCC_A | BPCC_PT
1091 | (TARGET_LONG_BITS == 64 ? BPCC_XCC : BPCC_ICC), 0);
1092 /* delay slot */
1093 tcg_out_ldst_rr(s, data, addrz, TCG_REG_O1, qemu_ld_opc[memop]);
53c37487 1094
a0ce341a 1095 /* TLB Miss. */
f5ef6aac 1096
7ea5d725 1097 param = TCG_REG_O1;
34b1a49c
RH
1098 if (!SPARC64 && TARGET_LONG_BITS == 64) {
1099 /* Skip the high-part; we'll perform the extract in the trampoline. */
1100 param++;
a0ce341a 1101 }
34b1a49c 1102 tcg_out_mov(s, TCG_TYPE_REG, param++, addr);
f5ef6aac 1103
7ea5d725
RH
1104 /* We use the helpers to extend SB and SW data, leaving the case
1105 of SL needing explicit extending below. */
1106 if ((memop & ~MO_BSWAP) == MO_SL) {
1107 func = qemu_ld_trampoline[memop & ~MO_SIGN];
1108 } else {
1109 func = qemu_ld_trampoline[memop];
1110 }
abce5964 1111 assert(func != NULL);
4e9cf840 1112 tcg_out_call_nodelay(s, func);
a0ce341a 1113 /* delay slot */
7ea5d725
RH
1114 tcg_out_movi(s, TCG_TYPE_I32, param, memi);
1115
34b1a49c
RH
1116 /* Recall that all of the helpers return 64-bit results.
1117 Which complicates things for sparcv8plus. */
1118 if (SPARC64) {
1119 /* We let the helper sign-extend SB and SW, but leave SL for here. */
1120 if (is_64 && (memop & ~MO_BSWAP) == MO_SL) {
1121 tcg_out_arithi(s, data, TCG_REG_O0, 0, SHIFT_SRA);
1122 } else {
1123 tcg_out_mov(s, TCG_TYPE_REG, data, TCG_REG_O0);
1124 }
1125 } else {
1126 if (s_bits == MO_64) {
1127 tcg_out_arithi(s, TCG_REG_O0, TCG_REG_O0, 32, SHIFT_SLLX);
1128 tcg_out_arithi(s, TCG_REG_O1, TCG_REG_O1, 0, SHIFT_SRL);
1129 tcg_out_arith(s, data, TCG_REG_O0, TCG_REG_O1, ARITH_OR);
1130 } else if (is_64) {
1131 /* Re-extend from 32-bit rather than reassembling when we
1132 know the high register must be an extension. */
1133 tcg_out_arithi(s, data, TCG_REG_O1, 0,
1134 memop & MO_SIGN ? SHIFT_SRA : SHIFT_SRL);
1135 } else {
1136 tcg_out_mov(s, TCG_TYPE_I32, data, TCG_REG_O1);
a0ce341a 1137 }
f5ef6aac
BS
1138 }
1139
abce5964 1140 *label_ptr |= INSN_OFF19(tcg_ptr_byte_diff(s->code_ptr, label_ptr));
90cbed46 1141#else
9f44adc5 1142 if (SPARC64 && TARGET_LONG_BITS == 32) {
34b1a49c
RH
1143 tcg_out_arithi(s, TCG_REG_T1, addr, 0, SHIFT_SRL);
1144 addr = TCG_REG_T1;
f5ef6aac 1145 }
34b1a49c
RH
1146 tcg_out_ldst_rr(s, data, addr,
1147 (GUEST_BASE ? TCG_GUEST_BASE_REG : TCG_REG_G0),
1148 qemu_ld_opc[memop]);
a0ce341a 1149#endif /* CONFIG_SOFTMMU */
f5ef6aac
BS
1150}
1151
34b1a49c
RH
1152static void tcg_out_qemu_st(TCGContext *s, TCGReg data, TCGReg addr,
1153 TCGMemOp memop, int memi)
f5ef6aac 1154{
34b1a49c
RH
1155#ifdef CONFIG_SOFTMMU
1156 TCGMemOp s_bits = memop & MO_SIZE;
1157 TCGReg addrz, param;
abce5964
RH
1158 tcg_insn_unit *func;
1159 tcg_insn_unit *label_ptr;
f5ef6aac 1160
34b1a49c 1161 addrz = tcg_out_tlb_load(s, addr, memi, s_bits,
cab0a7ea 1162 offsetof(CPUTLBEntry, addr_write));
a0ce341a 1163
a0ce341a
RH
1164 /* The fast path is exactly one insn. Thus we can perform the entire
1165 TLB Hit in the (annulled) delay slot of the branch over TLB Miss. */
1166 /* beq,a,pt %[xi]cc, label0 */
abce5964 1167 label_ptr = s->code_ptr;
a115f3ea
RH
1168 tcg_out_bpcc0(s, COND_E, BPCC_A | BPCC_PT
1169 | (TARGET_LONG_BITS == 64 ? BPCC_XCC : BPCC_ICC), 0);
a0ce341a 1170 /* delay slot */
34b1a49c 1171 tcg_out_ldst_rr(s, data, addrz, TCG_REG_O1, qemu_st_opc[memop]);
a0ce341a
RH
1172
1173 /* TLB Miss. */
1174
7ea5d725 1175 param = TCG_REG_O1;
34b1a49c
RH
1176 if (!SPARC64 && TARGET_LONG_BITS == 64) {
1177 /* Skip the high-part; we'll perform the extract in the trampoline. */
1178 param++;
a0ce341a 1179 }
34b1a49c
RH
1180 tcg_out_mov(s, TCG_TYPE_REG, param++, addr);
1181 if (!SPARC64 && s_bits == MO_64) {
1182 /* Skip the high-part; we'll perform the extract in the trampoline. */
1183 param++;
a0ce341a 1184 }
34b1a49c 1185 tcg_out_mov(s, TCG_TYPE_REG, param++, data);
53c37487 1186
7ea5d725 1187 func = qemu_st_trampoline[memop];
abce5964 1188 assert(func != NULL);
4e9cf840 1189 tcg_out_call_nodelay(s, func);
a0ce341a 1190 /* delay slot */
7ea5d725 1191 tcg_out_movi(s, TCG_TYPE_REG, param, memi);
f5ef6aac 1192
abce5964 1193 *label_ptr |= INSN_OFF19(tcg_ptr_byte_diff(s->code_ptr, label_ptr));
8384dd67 1194#else
9f44adc5 1195 if (SPARC64 && TARGET_LONG_BITS == 32) {
34b1a49c
RH
1196 tcg_out_arithi(s, TCG_REG_T1, addr, 0, SHIFT_SRL);
1197 addr = TCG_REG_T1;
a0ce341a 1198 }
34b1a49c 1199 tcg_out_ldst_rr(s, data, addr,
c6f7e4fb 1200 (GUEST_BASE ? TCG_GUEST_BASE_REG : TCG_REG_G0),
eef0d9e7 1201 qemu_st_opc[memop]);
a0ce341a 1202#endif /* CONFIG_SOFTMMU */
f5ef6aac
BS
1203}
1204
b357f902
RH
1205static void tcg_out_op(TCGContext *s, TCGOpcode opc,
1206 const TCGArg args[TCG_MAX_OP_ARGS],
1207 const int const_args[TCG_MAX_OP_ARGS])
8289b279 1208{
b357f902
RH
1209 TCGArg a0, a1, a2;
1210 int c, c2;
1211
1212 /* Hoist the loads of the most common arguments. */
1213 a0 = args[0];
1214 a1 = args[1];
1215 a2 = args[2];
1216 c2 = const_args[2];
8289b279
BS
1217
1218 switch (opc) {
1219 case INDEX_op_exit_tb:
b357f902 1220 if (check_fit_ptr(a0, 13)) {
8b66eefe 1221 tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I7, 8, RETURN);
b357f902 1222 tcg_out_movi_imm13(s, TCG_REG_O0, a0);
8b66eefe 1223 } else {
b357f902 1224 tcg_out_movi(s, TCG_TYPE_PTR, TCG_REG_I0, a0 & ~0x3ff);
8b66eefe 1225 tcg_out_arithi(s, TCG_REG_G0, TCG_REG_I7, 8, RETURN);
b357f902 1226 tcg_out_arithi(s, TCG_REG_O0, TCG_REG_O0, a0 & 0x3ff, ARITH_OR);
8b66eefe 1227 }
8289b279
BS
1228 break;
1229 case INDEX_op_goto_tb:
1230 if (s->tb_jmp_offset) {
1231 /* direct jump method */
abce5964 1232 s->tb_jmp_offset[a0] = tcg_current_code_size(s);
5bbd2cae 1233 /* Make sure to preserve links during retranslation. */
abce5964 1234 tcg_out32(s, CALL | (*s->code_ptr & ~INSN_OP(-1)));
8289b279
BS
1235 } else {
1236 /* indirect jump method */
b357f902 1237 tcg_out_ld_ptr(s, TCG_REG_T1, (uintptr_t)(s->tb_next + a0));
aad2f06a 1238 tcg_out_arithi(s, TCG_REG_G0, TCG_REG_T1, 0, JMPL);
8289b279 1239 }
53cd9273 1240 tcg_out_nop(s);
abce5964 1241 s->tb_next_offset[a0] = tcg_current_code_size(s);
8289b279 1242 break;
8289b279 1243 case INDEX_op_br:
bec16311 1244 tcg_out_bpcc(s, COND_A, BPCC_PT, arg_label(a0));
f5ef6aac 1245 tcg_out_nop(s);
8289b279 1246 break;
8289b279 1247
8289b279 1248#define OP_32_64(x) \
ba225198
RH
1249 glue(glue(case INDEX_op_, x), _i32): \
1250 glue(glue(case INDEX_op_, x), _i64)
34b1a49c 1251
ba225198 1252 OP_32_64(ld8u):
b357f902 1253 tcg_out_ldst(s, a0, a1, a2, LDUB);
8289b279 1254 break;
ba225198 1255 OP_32_64(ld8s):
b357f902 1256 tcg_out_ldst(s, a0, a1, a2, LDSB);
8289b279 1257 break;
ba225198 1258 OP_32_64(ld16u):
b357f902 1259 tcg_out_ldst(s, a0, a1, a2, LDUH);
8289b279 1260 break;
ba225198 1261 OP_32_64(ld16s):
b357f902 1262 tcg_out_ldst(s, a0, a1, a2, LDSH);
8289b279
BS
1263 break;
1264 case INDEX_op_ld_i32:
53cd9273 1265 case INDEX_op_ld32u_i64:
b357f902 1266 tcg_out_ldst(s, a0, a1, a2, LDUW);
8289b279 1267 break;
ba225198 1268 OP_32_64(st8):
b357f902 1269 tcg_out_ldst(s, a0, a1, a2, STB);
8289b279 1270 break;
ba225198 1271 OP_32_64(st16):
b357f902 1272 tcg_out_ldst(s, a0, a1, a2, STH);
8289b279
BS
1273 break;
1274 case INDEX_op_st_i32:
53cd9273 1275 case INDEX_op_st32_i64:
b357f902 1276 tcg_out_ldst(s, a0, a1, a2, STW);
8289b279 1277 break;
ba225198 1278 OP_32_64(add):
53cd9273 1279 c = ARITH_ADD;
ba225198
RH
1280 goto gen_arith;
1281 OP_32_64(sub):
8289b279 1282 c = ARITH_SUB;
ba225198
RH
1283 goto gen_arith;
1284 OP_32_64(and):
8289b279 1285 c = ARITH_AND;
ba225198 1286 goto gen_arith;
dc69960d
RH
1287 OP_32_64(andc):
1288 c = ARITH_ANDN;
1289 goto gen_arith;
ba225198 1290 OP_32_64(or):
8289b279 1291 c = ARITH_OR;
ba225198 1292 goto gen_arith;
18c8f7a3
RH
1293 OP_32_64(orc):
1294 c = ARITH_ORN;
1295 goto gen_arith;
ba225198 1296 OP_32_64(xor):
8289b279 1297 c = ARITH_XOR;
ba225198 1298 goto gen_arith;
8289b279
BS
1299 case INDEX_op_shl_i32:
1300 c = SHIFT_SLL;
1fd95946
RH
1301 do_shift32:
1302 /* Limit immediate shift count lest we create an illegal insn. */
b357f902 1303 tcg_out_arithc(s, a0, a1, a2 & 31, c2, c);
1fd95946 1304 break;
8289b279
BS
1305 case INDEX_op_shr_i32:
1306 c = SHIFT_SRL;
1fd95946 1307 goto do_shift32;
8289b279
BS
1308 case INDEX_op_sar_i32:
1309 c = SHIFT_SRA;
1fd95946 1310 goto do_shift32;
8289b279
BS
1311 case INDEX_op_mul_i32:
1312 c = ARITH_UMUL;
ba225198 1313 goto gen_arith;
583d1215 1314
4b5a85c1
RH
1315 OP_32_64(neg):
1316 c = ARITH_SUB;
1317 goto gen_arith1;
be6551b1
RH
1318 OP_32_64(not):
1319 c = ARITH_ORN;
1320 goto gen_arith1;
4b5a85c1 1321
583d1215 1322 case INDEX_op_div_i32:
b357f902 1323 tcg_out_div32(s, a0, a1, a2, c2, 0);
583d1215
RH
1324 break;
1325 case INDEX_op_divu_i32:
b357f902 1326 tcg_out_div32(s, a0, a1, a2, c2, 1);
583d1215
RH
1327 break;
1328
8289b279 1329 case INDEX_op_brcond_i32:
bec16311 1330 tcg_out_brcond_i32(s, a2, a0, a1, const_args[1], arg_label(args[3]));
8289b279 1331 break;
dbfe80e1 1332 case INDEX_op_setcond_i32:
b357f902 1333 tcg_out_setcond_i32(s, args[3], a0, a1, a2, c2);
dbfe80e1 1334 break;
ded37f0d 1335 case INDEX_op_movcond_i32:
b357f902 1336 tcg_out_movcond_i32(s, args[5], a0, a1, a2, c2, args[3], const_args[3]);
ded37f0d 1337 break;
dbfe80e1 1338
7a3766f3 1339 case INDEX_op_add2_i32:
609ac1e1
RH
1340 tcg_out_addsub2_i32(s, args[0], args[1], args[2], args[3],
1341 args[4], const_args[4], args[5], const_args[5],
c470b663 1342 ARITH_ADDCC, ARITH_ADDC);
7a3766f3
RH
1343 break;
1344 case INDEX_op_sub2_i32:
609ac1e1
RH
1345 tcg_out_addsub2_i32(s, args[0], args[1], args[2], args[3],
1346 args[4], const_args[4], args[5], const_args[5],
c470b663 1347 ARITH_SUBCC, ARITH_SUBC);
7a3766f3
RH
1348 break;
1349 case INDEX_op_mulu2_i32:
f4c16661
RH
1350 c = ARITH_UMUL;
1351 goto do_mul2;
1352 case INDEX_op_muls2_i32:
1353 c = ARITH_SMUL;
1354 do_mul2:
1355 /* The 32-bit multiply insns produce a full 64-bit result. If the
1356 destination register can hold it, we can avoid the slower RDY. */
b357f902
RH
1357 tcg_out_arithc(s, a0, a2, args[3], const_args[3], c);
1358 if (SPARC64 || a0 <= TCG_REG_O7) {
1359 tcg_out_arithi(s, a1, a0, 32, SHIFT_SRLX);
f4c16661 1360 } else {
b357f902 1361 tcg_out_rdy(s, a1);
f4c16661 1362 }
7a3766f3 1363 break;
8289b279 1364
cab0a7ea 1365 case INDEX_op_qemu_ld_i32:
b357f902 1366 tcg_out_qemu_ld(s, a0, a1, a2, args[3], false);
8289b279 1367 break;
cab0a7ea 1368 case INDEX_op_qemu_ld_i64:
b357f902 1369 tcg_out_qemu_ld(s, a0, a1, a2, args[3], true);
8289b279 1370 break;
cab0a7ea 1371 case INDEX_op_qemu_st_i32:
cab0a7ea 1372 case INDEX_op_qemu_st_i64:
b357f902 1373 tcg_out_qemu_st(s, a0, a1, a2, args[3]);
a0ce341a 1374 break;
8289b279 1375
53cd9273 1376 case INDEX_op_ld32s_i64:
b357f902 1377 tcg_out_ldst(s, a0, a1, a2, LDSW);
53cd9273 1378 break;
8289b279 1379 case INDEX_op_ld_i64:
b357f902 1380 tcg_out_ldst(s, a0, a1, a2, LDX);
8289b279
BS
1381 break;
1382 case INDEX_op_st_i64:
b357f902 1383 tcg_out_ldst(s, a0, a1, a2, STX);
8289b279
BS
1384 break;
1385 case INDEX_op_shl_i64:
1386 c = SHIFT_SLLX;
1fd95946
RH
1387 do_shift64:
1388 /* Limit immediate shift count lest we create an illegal insn. */
b357f902 1389 tcg_out_arithc(s, a0, a1, a2 & 63, c2, c);
1fd95946 1390 break;
8289b279
BS
1391 case INDEX_op_shr_i64:
1392 c = SHIFT_SRLX;
1fd95946 1393 goto do_shift64;
8289b279
BS
1394 case INDEX_op_sar_i64:
1395 c = SHIFT_SRAX;
1fd95946 1396 goto do_shift64;
8289b279
BS
1397 case INDEX_op_mul_i64:
1398 c = ARITH_MULX;
ba225198 1399 goto gen_arith;
583d1215 1400 case INDEX_op_div_i64:
53cd9273 1401 c = ARITH_SDIVX;
ba225198 1402 goto gen_arith;
583d1215 1403 case INDEX_op_divu_i64:
8289b279 1404 c = ARITH_UDIVX;
ba225198 1405 goto gen_arith;
cc6dfecf 1406 case INDEX_op_ext32s_i64:
b357f902 1407 tcg_out_arithi(s, a0, a1, 0, SHIFT_SRA);
cc6dfecf
RH
1408 break;
1409 case INDEX_op_ext32u_i64:
b357f902 1410 tcg_out_arithi(s, a0, a1, 0, SHIFT_SRL);
cc6dfecf 1411 break;
a24fba93 1412 case INDEX_op_trunc_shr_i32:
b357f902
RH
1413 if (a2 == 0) {
1414 tcg_out_mov(s, TCG_TYPE_I32, a0, a1);
a24fba93 1415 } else {
b357f902 1416 tcg_out_arithi(s, a0, a1, a2, SHIFT_SRLX);
a24fba93
RH
1417 }
1418 break;
8289b279
BS
1419
1420 case INDEX_op_brcond_i64:
bec16311 1421 tcg_out_brcond_i64(s, a2, a0, a1, const_args[1], arg_label(args[3]));
8289b279 1422 break;
dbfe80e1 1423 case INDEX_op_setcond_i64:
b357f902 1424 tcg_out_setcond_i64(s, args[3], a0, a1, a2, c2);
dbfe80e1 1425 break;
ded37f0d 1426 case INDEX_op_movcond_i64:
b357f902 1427 tcg_out_movcond_i64(s, args[5], a0, a1, a2, c2, args[3], const_args[3]);
ded37f0d 1428 break;
609ac1e1
RH
1429 case INDEX_op_add2_i64:
1430 tcg_out_addsub2_i64(s, args[0], args[1], args[2], args[3], args[4],
1431 const_args[4], args[5], const_args[5], false);
1432 break;
1433 case INDEX_op_sub2_i64:
1434 tcg_out_addsub2_i64(s, args[0], args[1], args[2], args[3], args[4],
1435 const_args[4], args[5], const_args[5], true);
1436 break;
de8301e5
RH
1437 case INDEX_op_muluh_i64:
1438 tcg_out_arith(s, args[0], args[1], args[2], ARITH_UMULXHI);
1439 break;
34b1a49c 1440
ba225198 1441 gen_arith:
b357f902 1442 tcg_out_arithc(s, a0, a1, a2, c2, c);
53cd9273
BS
1443 break;
1444
4b5a85c1 1445 gen_arith1:
b357f902 1446 tcg_out_arithc(s, a0, TCG_REG_G0, a1, const_args[1], c);
4b5a85c1
RH
1447 break;
1448
96d0ee7f 1449 case INDEX_op_mov_i32: /* Always emitted via tcg_out_mov. */
98b90bab 1450 case INDEX_op_mov_i64:
96d0ee7f 1451 case INDEX_op_movi_i32: /* Always emitted via tcg_out_movi. */
98b90bab 1452 case INDEX_op_movi_i64:
96d0ee7f 1453 case INDEX_op_call: /* Always emitted via tcg_out_call. */
8289b279 1454 default:
8289b279
BS
1455 tcg_abort();
1456 }
1457}
1458
1459static const TCGTargetOpDef sparc_op_defs[] = {
1460 { INDEX_op_exit_tb, { } },
b3db8758 1461 { INDEX_op_goto_tb, { } },
8289b279
BS
1462 { INDEX_op_br, { } },
1463
8289b279
BS
1464 { INDEX_op_ld8u_i32, { "r", "r" } },
1465 { INDEX_op_ld8s_i32, { "r", "r" } },
1466 { INDEX_op_ld16u_i32, { "r", "r" } },
1467 { INDEX_op_ld16s_i32, { "r", "r" } },
1468 { INDEX_op_ld_i32, { "r", "r" } },
89269f6c
RH
1469 { INDEX_op_st8_i32, { "rZ", "r" } },
1470 { INDEX_op_st16_i32, { "rZ", "r" } },
1471 { INDEX_op_st_i32, { "rZ", "r" } },
1472
1473 { INDEX_op_add_i32, { "r", "rZ", "rJ" } },
1474 { INDEX_op_mul_i32, { "r", "rZ", "rJ" } },
1475 { INDEX_op_div_i32, { "r", "rZ", "rJ" } },
1476 { INDEX_op_divu_i32, { "r", "rZ", "rJ" } },
89269f6c
RH
1477 { INDEX_op_sub_i32, { "r", "rZ", "rJ" } },
1478 { INDEX_op_and_i32, { "r", "rZ", "rJ" } },
1479 { INDEX_op_andc_i32, { "r", "rZ", "rJ" } },
1480 { INDEX_op_or_i32, { "r", "rZ", "rJ" } },
1481 { INDEX_op_orc_i32, { "r", "rZ", "rJ" } },
1482 { INDEX_op_xor_i32, { "r", "rZ", "rJ" } },
1483
1484 { INDEX_op_shl_i32, { "r", "rZ", "rJ" } },
1485 { INDEX_op_shr_i32, { "r", "rZ", "rJ" } },
1486 { INDEX_op_sar_i32, { "r", "rZ", "rJ" } },
8289b279 1487
4b5a85c1 1488 { INDEX_op_neg_i32, { "r", "rJ" } },
be6551b1 1489 { INDEX_op_not_i32, { "r", "rJ" } },
4b5a85c1 1490
89269f6c
RH
1491 { INDEX_op_brcond_i32, { "rZ", "rJ" } },
1492 { INDEX_op_setcond_i32, { "r", "rZ", "rJ" } },
1493 { INDEX_op_movcond_i32, { "r", "rZ", "rJ", "rI", "0" } },
dbfe80e1 1494
89269f6c
RH
1495 { INDEX_op_add2_i32, { "r", "r", "rZ", "rZ", "rJ", "rJ" } },
1496 { INDEX_op_sub2_i32, { "r", "r", "rZ", "rZ", "rJ", "rJ" } },
1497 { INDEX_op_mulu2_i32, { "r", "r", "rZ", "rJ" } },
f4c16661 1498 { INDEX_op_muls2_i32, { "r", "r", "rZ", "rJ" } },
8289b279 1499
34b1a49c
RH
1500 { INDEX_op_ld8u_i64, { "R", "r" } },
1501 { INDEX_op_ld8s_i64, { "R", "r" } },
1502 { INDEX_op_ld16u_i64, { "R", "r" } },
1503 { INDEX_op_ld16s_i64, { "R", "r" } },
1504 { INDEX_op_ld32u_i64, { "R", "r" } },
1505 { INDEX_op_ld32s_i64, { "R", "r" } },
1506 { INDEX_op_ld_i64, { "R", "r" } },
1507 { INDEX_op_st8_i64, { "RZ", "r" } },
1508 { INDEX_op_st16_i64, { "RZ", "r" } },
1509 { INDEX_op_st32_i64, { "RZ", "r" } },
1510 { INDEX_op_st_i64, { "RZ", "r" } },
1511
1512 { INDEX_op_add_i64, { "R", "RZ", "RJ" } },
1513 { INDEX_op_mul_i64, { "R", "RZ", "RJ" } },
1514 { INDEX_op_div_i64, { "R", "RZ", "RJ" } },
1515 { INDEX_op_divu_i64, { "R", "RZ", "RJ" } },
1516 { INDEX_op_sub_i64, { "R", "RZ", "RJ" } },
1517 { INDEX_op_and_i64, { "R", "RZ", "RJ" } },
1518 { INDEX_op_andc_i64, { "R", "RZ", "RJ" } },
1519 { INDEX_op_or_i64, { "R", "RZ", "RJ" } },
1520 { INDEX_op_orc_i64, { "R", "RZ", "RJ" } },
1521 { INDEX_op_xor_i64, { "R", "RZ", "RJ" } },
1522
1523 { INDEX_op_shl_i64, { "R", "RZ", "RJ" } },
1524 { INDEX_op_shr_i64, { "R", "RZ", "RJ" } },
1525 { INDEX_op_sar_i64, { "R", "RZ", "RJ" } },
1526
1527 { INDEX_op_neg_i64, { "R", "RJ" } },
1528 { INDEX_op_not_i64, { "R", "RJ" } },
1529
1530 { INDEX_op_ext32s_i64, { "R", "r" } },
1531 { INDEX_op_ext32u_i64, { "R", "r" } },
1532 { INDEX_op_trunc_shr_i32, { "r", "R" } },
1533
1534 { INDEX_op_brcond_i64, { "RZ", "RJ" } },
1535 { INDEX_op_setcond_i64, { "R", "RZ", "RJ" } },
1536 { INDEX_op_movcond_i64, { "R", "RZ", "RJ", "RI", "0" } },
1537
609ac1e1
RH
1538 { INDEX_op_add2_i64, { "R", "R", "RZ", "RZ", "RJ", "RI" } },
1539 { INDEX_op_sub2_i64, { "R", "R", "RZ", "RZ", "RJ", "RI" } },
de8301e5 1540 { INDEX_op_muluh_i64, { "R", "RZ", "RZ" } },
609ac1e1 1541
34b1a49c
RH
1542 { INDEX_op_qemu_ld_i32, { "r", "A" } },
1543 { INDEX_op_qemu_ld_i64, { "R", "A" } },
ebd0c614
RH
1544 { INDEX_op_qemu_st_i32, { "sZ", "A" } },
1545 { INDEX_op_qemu_st_i64, { "SZ", "A" } },
a0ce341a 1546
8289b279
BS
1547 { -1 },
1548};
1549
e4d58b41 1550static void tcg_target_init(TCGContext *s)
8289b279 1551{
90379ca8
RH
1552 /* Only probe for the platform and capabilities if we havn't already
1553 determined maximum values at compile time. */
1554#ifndef use_vis3_instructions
1555 {
1556 unsigned long hwcap = qemu_getauxval(AT_HWCAP);
1557 use_vis3_instructions = (hwcap & HWCAP_SPARC_VIS3) != 0;
1558 }
1559#endif
1560
8289b279 1561 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I32], 0, 0xffffffff);
34b1a49c
RH
1562 tcg_regset_set32(tcg_target_available_regs[TCG_TYPE_I64], 0, ALL_64);
1563
8289b279 1564 tcg_regset_set32(tcg_target_call_clobber_regs, 0,
b3db8758
BS
1565 (1 << TCG_REG_G1) |
1566 (1 << TCG_REG_G2) |
1567 (1 << TCG_REG_G3) |
1568 (1 << TCG_REG_G4) |
1569 (1 << TCG_REG_G5) |
1570 (1 << TCG_REG_G6) |
1571 (1 << TCG_REG_G7) |
8289b279
BS
1572 (1 << TCG_REG_O0) |
1573 (1 << TCG_REG_O1) |
1574 (1 << TCG_REG_O2) |
1575 (1 << TCG_REG_O3) |
1576 (1 << TCG_REG_O4) |
1577 (1 << TCG_REG_O5) |
8289b279
BS
1578 (1 << TCG_REG_O7));
1579
1580 tcg_regset_clear(s->reserved_regs);
375816f8
RH
1581 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G0); /* zero */
1582 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G6); /* reserved for os */
1583 tcg_regset_set_reg(s->reserved_regs, TCG_REG_G7); /* thread pointer */
1584 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I6); /* frame pointer */
1585 tcg_regset_set_reg(s->reserved_regs, TCG_REG_I7); /* return address */
1586 tcg_regset_set_reg(s->reserved_regs, TCG_REG_O6); /* stack pointer */
1587 tcg_regset_set_reg(s->reserved_regs, TCG_REG_T1); /* for internal use */
1588 tcg_regset_set_reg(s->reserved_regs, TCG_REG_T2); /* for internal use */
1589
8289b279
BS
1590 tcg_add_target_add_op_defs(sparc_op_defs);
1591}
cb1977d3 1592
9f44adc5 1593#if SPARC64
cb1977d3 1594# define ELF_HOST_MACHINE EM_SPARCV9
9b9c37c3 1595#else
cb1977d3
RH
1596# define ELF_HOST_MACHINE EM_SPARC32PLUS
1597# define ELF_HOST_FLAGS EF_SPARC_32PLUS
cb1977d3
RH
1598#endif
1599
cb1977d3 1600typedef struct {
ae18b28d 1601 DebugFrameHeader h;
9f44adc5 1602 uint8_t fde_def_cfa[SPARC64 ? 4 : 2];
497a22eb
RH
1603 uint8_t fde_win_save;
1604 uint8_t fde_ret_save[3];
cb1977d3
RH
1605} DebugFrame;
1606
ae18b28d
RH
1607static const DebugFrame debug_frame = {
1608 .h.cie.len = sizeof(DebugFrameCIE)-4, /* length after .len member */
1609 .h.cie.id = -1,
1610 .h.cie.version = 1,
1611 .h.cie.code_align = 1,
1612 .h.cie.data_align = -sizeof(void *) & 0x7f,
1613 .h.cie.return_column = 15, /* o7 */
cb1977d3 1614
497a22eb 1615 /* Total FDE size does not include the "len" member. */
ae18b28d 1616 .h.fde.len = sizeof(DebugFrame) - offsetof(DebugFrame, h.fde.cie_offset),
497a22eb
RH
1617
1618 .fde_def_cfa = {
9f44adc5 1619#if SPARC64
cb1977d3
RH
1620 12, 30, /* DW_CFA_def_cfa i6, 2047 */
1621 (2047 & 0x7f) | 0x80, (2047 >> 7)
1622#else
1623 13, 30 /* DW_CFA_def_cfa_register i6 */
1624#endif
1625 },
497a22eb
RH
1626 .fde_win_save = 0x2d, /* DW_CFA_GNU_window_save */
1627 .fde_ret_save = { 9, 15, 31 }, /* DW_CFA_register o7, i7 */
cb1977d3
RH
1628};
1629
1630void tcg_register_jit(void *buf, size_t buf_size)
1631{
cb1977d3
RH
1632 tcg_register_jit_int(buf, buf_size, &debug_frame, sizeof(debug_frame));
1633}
5bbd2cae
RH
1634
1635void tb_set_jmp_target1(uintptr_t jmp_addr, uintptr_t addr)
1636{
1637 uint32_t *ptr = (uint32_t *)jmp_addr;
c8fc56ce 1638 uintptr_t disp = addr - jmp_addr;
5bbd2cae
RH
1639
1640 /* We can reach the entire address space for 32-bit. For 64-bit
1641 the code_gen_buffer can't be larger than 2GB. */
c8fc56ce 1642 assert(disp == (int32_t)disp);
5bbd2cae 1643
c8fc56ce 1644 *ptr = CALL | (uint32_t)disp >> 2;
5bbd2cae
RH
1645 flush_icache_range(jmp_addr, jmp_addr + 4);
1646}