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