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