2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
26 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
64 static const int tcg_target_reg_alloc_order
[] = {
80 static const int tcg_target_call_iarg_regs
[6] = {
89 static const int tcg_target_call_oarg_regs
[] = {
96 static inline int check_fit_tl(tcg_target_long val
, unsigned int bits
)
98 return (val
<< ((sizeof(tcg_target_long
) * 8 - bits
))
99 >> (sizeof(tcg_target_long
) * 8 - bits
)) == val
;
102 static inline int check_fit_i32(uint32_t val
, unsigned int bits
)
104 return ((val
<< (32 - bits
)) >> (32 - bits
)) == val
;
107 static void patch_reloc(uint8_t *code_ptr
, int type
,
108 tcg_target_long value
, tcg_target_long addend
)
113 if (value
!= (uint32_t)value
)
115 *(uint32_t *)code_ptr
= value
;
117 case R_SPARC_WDISP22
:
118 value
-= (long)code_ptr
;
120 if (!check_fit_tl(value
, 22))
122 *(uint32_t *)code_ptr
= ((*(uint32_t *)code_ptr
) & ~0x3fffff) | value
;
124 case R_SPARC_WDISP19
:
125 value
-= (long)code_ptr
;
127 if (!check_fit_tl(value
, 19))
129 *(uint32_t *)code_ptr
= ((*(uint32_t *)code_ptr
) & ~0x7ffff) | value
;
136 /* parse target specific constraints */
137 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
144 ct
->ct
|= TCG_CT_REG
;
145 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
147 case 'L': /* qemu_ld/st constraint */
148 ct
->ct
|= TCG_CT_REG
;
149 tcg_regset_set32(ct
->u
.regs
, 0, 0xffffffff);
151 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_O0
);
152 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_O1
);
153 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_O2
);
154 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_O3
);
157 ct
->ct
|= TCG_CT_CONST_S11
;
160 ct
->ct
|= TCG_CT_CONST_S13
;
170 /* test if a constant matches the constraint */
171 static inline int tcg_target_const_match(tcg_target_long val
,
172 const TCGArgConstraint
*arg_ct
)
177 if (ct
& TCG_CT_CONST
)
179 else if ((ct
& TCG_CT_CONST_S11
) && check_fit_tl(val
, 11))
181 else if ((ct
& TCG_CT_CONST_S13
) && check_fit_tl(val
, 13))
187 #define INSN_OP(x) ((x) << 30)
188 #define INSN_OP2(x) ((x) << 22)
189 #define INSN_OP3(x) ((x) << 19)
190 #define INSN_OPF(x) ((x) << 5)
191 #define INSN_RD(x) ((x) << 25)
192 #define INSN_RS1(x) ((x) << 14)
193 #define INSN_RS2(x) (x)
194 #define INSN_ASI(x) ((x) << 5)
196 #define INSN_IMM11(x) ((1 << 13) | ((x) & 0x7ff))
197 #define INSN_IMM13(x) ((1 << 13) | ((x) & 0x1fff))
198 #define INSN_OFF19(x) (((x) >> 2) & 0x07ffff)
199 #define INSN_OFF22(x) (((x) >> 2) & 0x3fffff)
201 #define INSN_COND(x, a) (((x) << 25) | ((a) << 29))
218 #define BA (INSN_OP(0) | INSN_COND(COND_A, 0) | INSN_OP2(0x2))
220 #define MOVCC_ICC (1 << 18)
221 #define MOVCC_XCC (1 << 18 | 1 << 12)
223 #define ARITH_ADD (INSN_OP(2) | INSN_OP3(0x00))
224 #define ARITH_ADDCC (INSN_OP(2) | INSN_OP3(0x10))
225 #define ARITH_AND (INSN_OP(2) | INSN_OP3(0x01))
226 #define ARITH_ANDN (INSN_OP(2) | INSN_OP3(0x05))
227 #define ARITH_OR (INSN_OP(2) | INSN_OP3(0x02))
228 #define ARITH_ORCC (INSN_OP(2) | INSN_OP3(0x12))
229 #define ARITH_ORN (INSN_OP(2) | INSN_OP3(0x06))
230 #define ARITH_XOR (INSN_OP(2) | INSN_OP3(0x03))
231 #define ARITH_SUB (INSN_OP(2) | INSN_OP3(0x04))
232 #define ARITH_SUBCC (INSN_OP(2) | INSN_OP3(0x14))
233 #define ARITH_ADDX (INSN_OP(2) | INSN_OP3(0x10))
234 #define ARITH_SUBX (INSN_OP(2) | INSN_OP3(0x0c))
235 #define ARITH_UMUL (INSN_OP(2) | INSN_OP3(0x0a))
236 #define ARITH_UDIV (INSN_OP(2) | INSN_OP3(0x0e))
237 #define ARITH_SDIV (INSN_OP(2) | INSN_OP3(0x0f))
238 #define ARITH_MULX (INSN_OP(2) | INSN_OP3(0x09))
239 #define ARITH_UDIVX (INSN_OP(2) | INSN_OP3(0x0d))
240 #define ARITH_SDIVX (INSN_OP(2) | INSN_OP3(0x2d))
241 #define ARITH_MOVCC (INSN_OP(2) | INSN_OP3(0x2c))
243 #define SHIFT_SLL (INSN_OP(2) | INSN_OP3(0x25))
244 #define SHIFT_SRL (INSN_OP(2) | INSN_OP3(0x26))
245 #define SHIFT_SRA (INSN_OP(2) | INSN_OP3(0x27))
247 #define SHIFT_SLLX (INSN_OP(2) | INSN_OP3(0x25) | (1 << 12))
248 #define SHIFT_SRLX (INSN_OP(2) | INSN_OP3(0x26) | (1 << 12))
249 #define SHIFT_SRAX (INSN_OP(2) | INSN_OP3(0x27) | (1 << 12))
251 #define RDY (INSN_OP(2) | INSN_OP3(0x28) | INSN_RS1(0))
252 #define WRY (INSN_OP(2) | INSN_OP3(0x30) | INSN_RD(0))
253 #define JMPL (INSN_OP(2) | INSN_OP3(0x38))
254 #define SAVE (INSN_OP(2) | INSN_OP3(0x3c))
255 #define RESTORE (INSN_OP(2) | INSN_OP3(0x3d))
256 #define SETHI (INSN_OP(0) | INSN_OP2(0x4))
257 #define CALL INSN_OP(1)
258 #define LDUB (INSN_OP(3) | INSN_OP3(0x01))
259 #define LDSB (INSN_OP(3) | INSN_OP3(0x09))
260 #define LDUH (INSN_OP(3) | INSN_OP3(0x02))
261 #define LDSH (INSN_OP(3) | INSN_OP3(0x0a))
262 #define LDUW (INSN_OP(3) | INSN_OP3(0x00))
263 #define LDSW (INSN_OP(3) | INSN_OP3(0x08))
264 #define LDX (INSN_OP(3) | INSN_OP3(0x0b))
265 #define STB (INSN_OP(3) | INSN_OP3(0x05))
266 #define STH (INSN_OP(3) | INSN_OP3(0x06))
267 #define STW (INSN_OP(3) | INSN_OP3(0x04))
268 #define STX (INSN_OP(3) | INSN_OP3(0x0e))
269 #define LDUBA (INSN_OP(3) | INSN_OP3(0x11))
270 #define LDSBA (INSN_OP(3) | INSN_OP3(0x19))
271 #define LDUHA (INSN_OP(3) | INSN_OP3(0x12))
272 #define LDSHA (INSN_OP(3) | INSN_OP3(0x1a))
273 #define LDUWA (INSN_OP(3) | INSN_OP3(0x10))
274 #define LDSWA (INSN_OP(3) | INSN_OP3(0x18))
275 #define LDXA (INSN_OP(3) | INSN_OP3(0x1b))
276 #define STBA (INSN_OP(3) | INSN_OP3(0x15))
277 #define STHA (INSN_OP(3) | INSN_OP3(0x16))
278 #define STWA (INSN_OP(3) | INSN_OP3(0x14))
279 #define STXA (INSN_OP(3) | INSN_OP3(0x1e))
281 #ifndef ASI_PRIMARY_LITTLE
282 #define ASI_PRIMARY_LITTLE 0x88
285 static inline void tcg_out_arith(TCGContext
*s
, int rd
, int rs1
, int rs2
,
288 tcg_out32(s
, op
| INSN_RD(rd
) | INSN_RS1(rs1
) |
292 static inline void tcg_out_arithi(TCGContext
*s
, int rd
, int rs1
,
293 uint32_t offset
, int op
)
295 tcg_out32(s
, op
| INSN_RD(rd
) | INSN_RS1(rs1
) |
299 static void tcg_out_arithc(TCGContext
*s
, int rd
, int rs1
,
300 int val2
, int val2const
, int op
)
302 tcg_out32(s
, op
| INSN_RD(rd
) | INSN_RS1(rs1
)
303 | (val2const
? INSN_IMM13(val2
) : INSN_RS2(val2
)));
306 static inline void tcg_out_mov(TCGContext
*s
, TCGType type
,
307 TCGReg ret
, TCGReg arg
)
309 tcg_out_arith(s
, ret
, arg
, TCG_REG_G0
, ARITH_OR
);
312 static inline void tcg_out_sethi(TCGContext
*s
, int ret
, uint32_t arg
)
314 tcg_out32(s
, SETHI
| INSN_RD(ret
) | ((arg
& 0xfffffc00) >> 10));
317 static inline void tcg_out_movi_imm13(TCGContext
*s
, int ret
, uint32_t arg
)
319 tcg_out_arithi(s
, ret
, TCG_REG_G0
, arg
, ARITH_OR
);
322 static inline void tcg_out_movi_imm32(TCGContext
*s
, int ret
, uint32_t arg
)
324 if (check_fit_tl(arg
, 13))
325 tcg_out_movi_imm13(s
, ret
, arg
);
327 tcg_out_sethi(s
, ret
, arg
);
329 tcg_out_arithi(s
, ret
, ret
, arg
& 0x3ff, ARITH_OR
);
333 static inline void tcg_out_movi(TCGContext
*s
, TCGType type
,
334 TCGReg ret
, tcg_target_long arg
)
336 /* All 32-bit constants, as well as 64-bit constants with
337 no high bits set go through movi_imm32. */
338 if (TCG_TARGET_REG_BITS
== 32
339 || type
== TCG_TYPE_I32
340 || (arg
& ~(tcg_target_long
)0xffffffff) == 0) {
341 tcg_out_movi_imm32(s
, ret
, arg
);
342 } else if (check_fit_tl(arg
, 13)) {
343 /* A 13-bit constant sign-extended to 64-bits. */
344 tcg_out_movi_imm13(s
, ret
, arg
);
345 } else if (check_fit_tl(arg
, 32)) {
346 /* A 32-bit constant sign-extended to 64-bits. */
347 tcg_out_sethi(s
, ret
, ~arg
);
348 tcg_out_arithi(s
, ret
, ret
, (arg
& 0x3ff) | -0x400, ARITH_XOR
);
350 tcg_out_movi_imm32(s
, TCG_REG_I4
, arg
>> (TCG_TARGET_REG_BITS
/ 2));
351 tcg_out_arithi(s
, TCG_REG_I4
, TCG_REG_I4
, 32, SHIFT_SLLX
);
352 tcg_out_movi_imm32(s
, ret
, arg
);
353 tcg_out_arith(s
, ret
, ret
, TCG_REG_I4
, ARITH_OR
);
357 static inline void tcg_out_ld_raw(TCGContext
*s
, int ret
,
360 tcg_out_sethi(s
, ret
, arg
);
361 tcg_out32(s
, LDUW
| INSN_RD(ret
) | INSN_RS1(ret
) |
362 INSN_IMM13(arg
& 0x3ff));
365 static inline void tcg_out_ld_ptr(TCGContext
*s
, int ret
,
368 if (!check_fit_tl(arg
, 10))
369 tcg_out_movi(s
, TCG_TYPE_PTR
, ret
, arg
& ~0x3ffULL
);
370 if (TCG_TARGET_REG_BITS
== 64) {
371 tcg_out32(s
, LDX
| INSN_RD(ret
) | INSN_RS1(ret
) |
372 INSN_IMM13(arg
& 0x3ff));
374 tcg_out32(s
, LDUW
| INSN_RD(ret
) | INSN_RS1(ret
) |
375 INSN_IMM13(arg
& 0x3ff));
379 static inline void tcg_out_ldst(TCGContext
*s
, int ret
, int addr
, int offset
, int op
)
381 if (check_fit_tl(offset
, 13))
382 tcg_out32(s
, op
| INSN_RD(ret
) | INSN_RS1(addr
) |
385 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_I5
, offset
);
386 tcg_out32(s
, op
| INSN_RD(ret
) | INSN_RS1(TCG_REG_I5
) |
391 static inline void tcg_out_ldst_asi(TCGContext
*s
, int ret
, int addr
,
392 int offset
, int op
, int asi
)
394 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_I5
, offset
);
395 tcg_out32(s
, op
| INSN_RD(ret
) | INSN_RS1(TCG_REG_I5
) |
396 INSN_ASI(asi
) | INSN_RS2(addr
));
399 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, TCGReg ret
,
400 TCGReg arg1
, tcg_target_long arg2
)
402 if (type
== TCG_TYPE_I32
)
403 tcg_out_ldst(s
, ret
, arg1
, arg2
, LDUW
);
405 tcg_out_ldst(s
, ret
, arg1
, arg2
, LDX
);
408 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, TCGReg arg
,
409 TCGReg arg1
, tcg_target_long arg2
)
411 if (type
== TCG_TYPE_I32
)
412 tcg_out_ldst(s
, arg
, arg1
, arg2
, STW
);
414 tcg_out_ldst(s
, arg
, arg1
, arg2
, STX
);
417 static inline void tcg_out_sety(TCGContext
*s
, int rs
)
419 tcg_out32(s
, WRY
| INSN_RS1(TCG_REG_G0
) | INSN_RS2(rs
));
422 static inline void tcg_out_rdy(TCGContext
*s
, int rd
)
424 tcg_out32(s
, RDY
| INSN_RD(rd
));
427 static inline void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
430 if (check_fit_tl(val
, 13))
431 tcg_out_arithi(s
, reg
, reg
, val
, ARITH_ADD
);
433 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_I5
, val
);
434 tcg_out_arith(s
, reg
, reg
, TCG_REG_I5
, ARITH_ADD
);
439 static inline void tcg_out_andi(TCGContext
*s
, int reg
, tcg_target_long val
)
442 if (check_fit_tl(val
, 13))
443 tcg_out_arithi(s
, reg
, reg
, val
, ARITH_AND
);
445 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_I5
, val
);
446 tcg_out_arith(s
, reg
, reg
, TCG_REG_I5
, ARITH_AND
);
451 static void tcg_out_div32(TCGContext
*s
, int rd
, int rs1
,
452 int val2
, int val2const
, int uns
)
454 /* Load Y with the sign/zero extension of RS1 to 64-bits. */
456 tcg_out_sety(s
, TCG_REG_G0
);
458 tcg_out_arithi(s
, TCG_REG_I5
, rs1
, 31, SHIFT_SRA
);
459 tcg_out_sety(s
, TCG_REG_I5
);
462 tcg_out_arithc(s
, rd
, rs1
, val2
, val2const
,
463 uns
? ARITH_UDIV
: ARITH_SDIV
);
466 static inline void tcg_out_nop(TCGContext
*s
)
468 tcg_out_sethi(s
, TCG_REG_G0
, 0);
471 static void tcg_out_branch_i32(TCGContext
*s
, int opc
, int label_index
)
473 TCGLabel
*l
= &s
->labels
[label_index
];
476 tcg_out32(s
, (INSN_OP(0) | INSN_COND(opc
, 0) | INSN_OP2(0x2)
477 | INSN_OFF22(l
->u
.value
- (unsigned long)s
->code_ptr
)));
479 tcg_out_reloc(s
, s
->code_ptr
, R_SPARC_WDISP22
, label_index
, 0);
480 tcg_out32(s
, (INSN_OP(0) | INSN_COND(opc
, 0) | INSN_OP2(0x2) | 0));
484 #if TCG_TARGET_REG_BITS == 64
485 static void tcg_out_branch_i64(TCGContext
*s
, int opc
, int label_index
)
487 TCGLabel
*l
= &s
->labels
[label_index
];
490 tcg_out32(s
, (INSN_OP(0) | INSN_COND(opc
, 0) | INSN_OP2(0x1) |
492 INSN_OFF19(l
->u
.value
- (unsigned long)s
->code_ptr
)));
494 tcg_out_reloc(s
, s
->code_ptr
, R_SPARC_WDISP19
, label_index
, 0);
495 tcg_out32(s
, (INSN_OP(0) | INSN_COND(opc
, 0) | INSN_OP2(0x1) |
501 static const uint8_t tcg_cond_to_bcond
[10] = {
502 [TCG_COND_EQ
] = COND_E
,
503 [TCG_COND_NE
] = COND_NE
,
504 [TCG_COND_LT
] = COND_L
,
505 [TCG_COND_GE
] = COND_GE
,
506 [TCG_COND_LE
] = COND_LE
,
507 [TCG_COND_GT
] = COND_G
,
508 [TCG_COND_LTU
] = COND_CS
,
509 [TCG_COND_GEU
] = COND_CC
,
510 [TCG_COND_LEU
] = COND_LEU
,
511 [TCG_COND_GTU
] = COND_GU
,
514 static void tcg_out_cmp(TCGContext
*s
, TCGArg c1
, TCGArg c2
, int c2const
)
516 tcg_out_arithc(s
, TCG_REG_G0
, c1
, c2
, c2const
, ARITH_SUBCC
);
519 static void tcg_out_brcond_i32(TCGContext
*s
, TCGCond cond
,
520 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
523 tcg_out_cmp(s
, arg1
, arg2
, const_arg2
);
524 tcg_out_branch_i32(s
, tcg_cond_to_bcond
[cond
], label_index
);
528 #if TCG_TARGET_REG_BITS == 64
529 static void tcg_out_brcond_i64(TCGContext
*s
, TCGCond cond
,
530 TCGArg arg1
, TCGArg arg2
, int const_arg2
,
533 tcg_out_cmp(s
, arg1
, arg2
, const_arg2
);
534 tcg_out_branch_i64(s
, tcg_cond_to_bcond
[cond
], label_index
);
538 static void tcg_out_brcond2_i32(TCGContext
*s
, TCGCond cond
,
539 TCGArg al
, TCGArg ah
,
540 TCGArg bl
, int blconst
,
541 TCGArg bh
, int bhconst
, int label_dest
)
543 int cc
, label_next
= gen_new_label();
545 tcg_out_cmp(s
, ah
, bh
, bhconst
);
547 /* Note that we fill one of the delay slots with the second compare. */
550 cc
= INSN_COND(tcg_cond_to_bcond
[TCG_COND_NE
], 0);
551 tcg_out_branch_i32(s
, cc
, label_next
);
552 tcg_out_cmp(s
, al
, bl
, blconst
);
553 cc
= INSN_COND(tcg_cond_to_bcond
[TCG_COND_EQ
], 0);
554 tcg_out_branch_i32(s
, cc
, label_dest
);
558 cc
= INSN_COND(tcg_cond_to_bcond
[TCG_COND_NE
], 0);
559 tcg_out_branch_i32(s
, cc
, label_dest
);
560 tcg_out_cmp(s
, al
, bl
, blconst
);
561 tcg_out_branch_i32(s
, cc
, label_dest
);
565 /* ??? One could fairly easily special-case 64-bit unsigned
566 compares against 32-bit zero-extended constants. For instance,
567 we know that (unsigned)AH < 0 is false and need not emit it.
568 Similarly, (unsigned)AH > 0 being true implies AH != 0, so the
569 second branch will never be taken. */
570 cc
= INSN_COND(tcg_cond_to_bcond
[cond
], 0);
571 tcg_out_branch_i32(s
, cc
, label_dest
);
573 cc
= INSN_COND(tcg_cond_to_bcond
[TCG_COND_NE
], 0);
574 tcg_out_branch_i32(s
, cc
, label_next
);
575 tcg_out_cmp(s
, al
, bl
, blconst
);
576 cc
= INSN_COND(tcg_cond_to_bcond
[tcg_unsigned_cond(cond
)], 0);
577 tcg_out_branch_i32(s
, cc
, label_dest
);
582 tcg_out_label(s
, label_next
, s
->code_ptr
);
586 static void tcg_out_setcond_i32(TCGContext
*s
, TCGCond cond
, TCGArg ret
,
587 TCGArg c1
, TCGArg c2
, int c2const
)
591 /* For 32-bit comparisons, we can play games with ADDX/SUBX. */
596 tcg_out_arithc(s
, ret
, c1
, c2
, c2const
, ARITH_XOR
);
598 c1
= TCG_REG_G0
, c2
= ret
, c2const
= 0;
599 cond
= (cond
== TCG_COND_EQ
? TCG_COND_LEU
: TCG_COND_LTU
);
604 if (c2const
&& c2
!= 0) {
605 tcg_out_movi_imm13(s
, TCG_REG_I5
, c2
);
608 t
= c1
, c1
= c2
, c2
= t
, c2const
= 0;
609 cond
= tcg_swap_cond(cond
);
617 tcg_out_cmp(s
, c1
, c2
, c2const
);
618 #if defined(__sparc_v9__) || defined(__sparc_v8plus__)
619 tcg_out_movi_imm13(s
, ret
, 0);
620 tcg_out32 (s
, ARITH_MOVCC
| INSN_RD(ret
)
621 | INSN_RS1(tcg_cond_to_bcond
[cond
])
622 | MOVCC_ICC
| INSN_IMM11(1));
625 tcg_out_branch_i32(s
, INSN_COND(tcg_cond_to_bcond
[cond
], 1), t
);
626 tcg_out_movi_imm13(s
, ret
, 1);
627 tcg_out_movi_imm13(s
, ret
, 0);
628 tcg_out_label(s
, t
, s
->code_ptr
);
633 tcg_out_cmp(s
, c1
, c2
, c2const
);
634 if (cond
== TCG_COND_LTU
) {
635 tcg_out_arithi(s
, ret
, TCG_REG_G0
, 0, ARITH_ADDX
);
637 tcg_out_arithi(s
, ret
, TCG_REG_G0
, -1, ARITH_SUBX
);
641 #if TCG_TARGET_REG_BITS == 64
642 static void tcg_out_setcond_i64(TCGContext
*s
, TCGCond cond
, TCGArg ret
,
643 TCGArg c1
, TCGArg c2
, int c2const
)
645 tcg_out_cmp(s
, c1
, c2
, c2const
);
646 tcg_out_movi_imm13(s
, ret
, 0);
647 tcg_out32 (s
, ARITH_MOVCC
| INSN_RD(ret
)
648 | INSN_RS1(tcg_cond_to_bcond
[cond
])
649 | MOVCC_XCC
| INSN_IMM11(1));
652 static void tcg_out_setcond2_i32(TCGContext
*s
, TCGCond cond
, TCGArg ret
,
653 TCGArg al
, TCGArg ah
,
654 TCGArg bl
, int blconst
,
655 TCGArg bh
, int bhconst
)
661 tcg_out_setcond_i32(s
, TCG_COND_EQ
, TCG_REG_I5
, al
, bl
, blconst
);
662 tcg_out_setcond_i32(s
, TCG_COND_EQ
, ret
, ah
, bh
, bhconst
);
663 tcg_out_arith(s
, ret
, ret
, TCG_REG_I5
, ARITH_AND
);
667 tcg_out_setcond_i32(s
, TCG_COND_NE
, TCG_REG_I5
, al
, al
, blconst
);
668 tcg_out_setcond_i32(s
, TCG_COND_NE
, ret
, ah
, bh
, bhconst
);
669 tcg_out_arith(s
, ret
, ret
, TCG_REG_I5
, ARITH_OR
);
673 lab
= gen_new_label();
675 tcg_out_cmp(s
, ah
, bh
, bhconst
);
676 tcg_out_branch_i32(s
, INSN_COND(tcg_cond_to_bcond
[cond
], 1), lab
);
677 tcg_out_movi_imm13(s
, ret
, 1);
678 tcg_out_branch_i32(s
, INSN_COND(COND_NE
, 1), lab
);
679 tcg_out_movi_imm13(s
, ret
, 0);
681 tcg_out_setcond_i32(s
, tcg_unsigned_cond(cond
), ret
, al
, bl
, blconst
);
683 tcg_out_label(s
, lab
, s
->code_ptr
);
689 /* Generate global QEMU prologue and epilogue code */
690 static void tcg_target_qemu_prologue(TCGContext
*s
)
692 tcg_set_frame(s
, TCG_REG_I6
, TCG_TARGET_CALL_STACK_OFFSET
,
693 CPU_TEMP_BUF_NLONGS
* (int)sizeof(long));
694 tcg_out32(s
, SAVE
| INSN_RD(TCG_REG_O6
) | INSN_RS1(TCG_REG_O6
) |
695 INSN_IMM13(-(TCG_TARGET_STACK_MINFRAME
+
696 CPU_TEMP_BUF_NLONGS
* (int)sizeof(long))));
697 tcg_out32(s
, JMPL
| INSN_RD(TCG_REG_G0
) | INSN_RS1(TCG_REG_I1
) |
698 INSN_RS2(TCG_REG_G0
));
699 tcg_out_mov(s
, TCG_TYPE_PTR
, TCG_AREG0
, TCG_REG_I0
);
702 #if defined(CONFIG_SOFTMMU)
704 #include "../../softmmu_defs.h"
706 /* helper signature: helper_ld_mmu(CPUState *env, target_ulong addr,
708 static const void * const qemu_ld_helpers
[4] = {
715 /* helper signature: helper_st_mmu(CPUState *env, target_ulong addr,
716 uintxx_t val, int mmu_idx) */
717 static const void * const qemu_st_helpers
[4] = {
725 #if TARGET_LONG_BITS == 32
726 #define TARGET_LD_OP LDUW
728 #define TARGET_LD_OP LDX
731 #if defined(CONFIG_SOFTMMU)
732 #if HOST_LONG_BITS == 32
733 #define TARGET_ADDEND_LD_OP LDUW
735 #define TARGET_ADDEND_LD_OP LDX
740 #define HOST_LD_OP LDX
741 #define HOST_ST_OP STX
742 #define HOST_SLL_OP SHIFT_SLLX
743 #define HOST_SRA_OP SHIFT_SRAX
745 #define HOST_LD_OP LDUW
746 #define HOST_ST_OP STW
747 #define HOST_SLL_OP SHIFT_SLL
748 #define HOST_SRA_OP SHIFT_SRA
751 static void tcg_out_qemu_ld(TCGContext
*s
, const TCGArg
*args
,
754 int addr_reg
, data_reg
, arg0
, arg1
, arg2
, mem_index
, s_bits
;
755 #if defined(CONFIG_SOFTMMU)
756 uint32_t *label1_ptr
, *label2_ptr
;
768 #if defined(CONFIG_SOFTMMU)
769 /* srl addr_reg, x, arg1 */
770 tcg_out_arithi(s
, arg1
, addr_reg
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
,
772 /* and addr_reg, x, arg0 */
773 tcg_out_arithi(s
, arg0
, addr_reg
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1),
776 /* and arg1, x, arg1 */
777 tcg_out_andi(s
, arg1
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
779 /* add arg1, x, arg1 */
780 tcg_out_addi(s
, arg1
, offsetof(CPUArchState
,
781 tlb_table
[mem_index
][0].addr_read
));
783 /* add env, arg1, arg1 */
784 tcg_out_arith(s
, arg1
, TCG_AREG0
, arg1
, ARITH_ADD
);
786 /* ld [arg1], arg2 */
787 tcg_out32(s
, TARGET_LD_OP
| INSN_RD(arg2
) | INSN_RS1(arg1
) |
788 INSN_RS2(TCG_REG_G0
));
790 /* subcc arg0, arg2, %g0 */
791 tcg_out_arith(s
, TCG_REG_G0
, arg0
, arg2
, ARITH_SUBCC
);
797 label1_ptr
= (uint32_t *)s
->code_ptr
;
800 /* mov (delay slot) */
801 tcg_out_mov(s
, TCG_TYPE_PTR
, arg0
, addr_reg
);
804 tcg_out_movi(s
, TCG_TYPE_I32
, arg1
, mem_index
);
805 /* XXX/FIXME: suboptimal */
806 tcg_out_mov(s
, TCG_TYPE_I32
, tcg_target_call_iarg_regs
[3],
807 tcg_target_call_iarg_regs
[2]);
808 tcg_out_mov(s
, TCG_TYPE_I64
, tcg_target_call_iarg_regs
[2],
809 tcg_target_call_iarg_regs
[1]);
810 tcg_out_mov(s
, TCG_TYPE_TL
, tcg_target_call_iarg_regs
[1],
811 tcg_target_call_iarg_regs
[0]);
812 tcg_out_mov(s
, TCG_TYPE_PTR
, tcg_target_call_iarg_regs
[0],
815 /* XXX: move that code at the end of the TB */
816 /* qemu_ld_helper[s_bits](arg0, arg1) */
817 tcg_out32(s
, CALL
| ((((tcg_target_ulong
)qemu_ld_helpers
[s_bits
]
818 - (tcg_target_ulong
)s
->code_ptr
) >> 2)
820 /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
823 tcg_out_ldst(s
, TCG_AREG0
, TCG_REG_CALL_STACK
,
824 TCG_TARGET_CALL_STACK_OFFSET
- TCG_STATIC_CALL_ARGS_SIZE
-
825 sizeof(long), HOST_ST_OP
);
826 tcg_out_ldst(s
, TCG_AREG0
, TCG_REG_CALL_STACK
,
827 TCG_TARGET_CALL_STACK_OFFSET
- TCG_STATIC_CALL_ARGS_SIZE
-
828 sizeof(long), HOST_LD_OP
);
830 /* data_reg = sign_extend(arg0) */
833 /* sll arg0, 24/56, data_reg */
834 tcg_out_arithi(s
, data_reg
, arg0
, (int)sizeof(tcg_target_long
) * 8 - 8,
836 /* sra data_reg, 24/56, data_reg */
837 tcg_out_arithi(s
, data_reg
, data_reg
,
838 (int)sizeof(tcg_target_long
) * 8 - 8, HOST_SRA_OP
);
841 /* sll arg0, 16/48, data_reg */
842 tcg_out_arithi(s
, data_reg
, arg0
,
843 (int)sizeof(tcg_target_long
) * 8 - 16, HOST_SLL_OP
);
844 /* sra data_reg, 16/48, data_reg */
845 tcg_out_arithi(s
, data_reg
, data_reg
,
846 (int)sizeof(tcg_target_long
) * 8 - 16, HOST_SRA_OP
);
849 /* sll arg0, 32, data_reg */
850 tcg_out_arithi(s
, data_reg
, arg0
, 32, HOST_SLL_OP
);
851 /* sra data_reg, 32, data_reg */
852 tcg_out_arithi(s
, data_reg
, data_reg
, 32, HOST_SRA_OP
);
860 tcg_out_mov(s
, TCG_TYPE_REG
, data_reg
, arg0
);
866 label2_ptr
= (uint32_t *)s
->code_ptr
;
869 /* nop (delay slot */
873 #if TARGET_LONG_BITS == 32
875 *label1_ptr
= (INSN_OP(0) | INSN_COND(COND_E
, 0) | INSN_OP2(0x2) |
876 INSN_OFF22((unsigned long)s
->code_ptr
-
877 (unsigned long)label1_ptr
));
879 /* be,pt %xcc label1 */
880 *label1_ptr
= (INSN_OP(0) | INSN_COND(COND_E
, 0) | INSN_OP2(0x1) |
881 (0x5 << 19) | INSN_OFF19((unsigned long)s
->code_ptr
-
882 (unsigned long)label1_ptr
));
885 /* ld [arg1 + x], arg1 */
886 tcg_out_ldst(s
, arg1
, arg1
, offsetof(CPUTLBEntry
, addend
) -
887 offsetof(CPUTLBEntry
, addr_read
), TARGET_ADDEND_LD_OP
);
889 #if TARGET_LONG_BITS == 32
890 /* and addr_reg, x, arg0 */
891 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_I5
, 0xffffffff);
892 tcg_out_arith(s
, arg0
, addr_reg
, TCG_REG_I5
, ARITH_AND
);
893 /* add arg0, arg1, arg0 */
894 tcg_out_arith(s
, arg0
, arg0
, arg1
, ARITH_ADD
);
896 /* add addr_reg, arg1, arg0 */
897 tcg_out_arith(s
, arg0
, addr_reg
, arg1
, ARITH_ADD
);
906 /* ldub [arg0], data_reg */
907 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDUB
);
910 /* ldsb [arg0], data_reg */
911 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDSB
);
914 #ifdef TARGET_WORDS_BIGENDIAN
915 /* lduh [arg0], data_reg */
916 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDUH
);
918 /* lduha [arg0] ASI_PRIMARY_LITTLE, data_reg */
919 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, LDUHA
, ASI_PRIMARY_LITTLE
);
923 #ifdef TARGET_WORDS_BIGENDIAN
924 /* ldsh [arg0], data_reg */
925 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDSH
);
927 /* ldsha [arg0] ASI_PRIMARY_LITTLE, data_reg */
928 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, LDSHA
, ASI_PRIMARY_LITTLE
);
932 #ifdef TARGET_WORDS_BIGENDIAN
933 /* lduw [arg0], data_reg */
934 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDUW
);
936 /* lduwa [arg0] ASI_PRIMARY_LITTLE, data_reg */
937 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, LDUWA
, ASI_PRIMARY_LITTLE
);
941 #ifdef TARGET_WORDS_BIGENDIAN
942 /* ldsw [arg0], data_reg */
943 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDSW
);
945 /* ldswa [arg0] ASI_PRIMARY_LITTLE, data_reg */
946 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, LDSWA
, ASI_PRIMARY_LITTLE
);
950 #ifdef TARGET_WORDS_BIGENDIAN
951 /* ldx [arg0], data_reg */
952 tcg_out_ldst(s
, data_reg
, arg0
, 0, LDX
);
954 /* ldxa [arg0] ASI_PRIMARY_LITTLE, data_reg */
955 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, LDXA
, ASI_PRIMARY_LITTLE
);
962 #if defined(CONFIG_SOFTMMU)
964 *label2_ptr
= (INSN_OP(0) | INSN_COND(COND_A
, 0) | INSN_OP2(0x2) |
965 INSN_OFF22((unsigned long)s
->code_ptr
-
966 (unsigned long)label2_ptr
));
970 static void tcg_out_qemu_st(TCGContext
*s
, const TCGArg
*args
,
973 int addr_reg
, data_reg
, arg0
, arg1
, arg2
, mem_index
, s_bits
;
974 #if defined(CONFIG_SOFTMMU)
975 uint32_t *label1_ptr
, *label2_ptr
;
988 #if defined(CONFIG_SOFTMMU)
989 /* srl addr_reg, x, arg1 */
990 tcg_out_arithi(s
, arg1
, addr_reg
, TARGET_PAGE_BITS
- CPU_TLB_ENTRY_BITS
,
993 /* and addr_reg, x, arg0 */
994 tcg_out_arithi(s
, arg0
, addr_reg
, TARGET_PAGE_MASK
| ((1 << s_bits
) - 1),
997 /* and arg1, x, arg1 */
998 tcg_out_andi(s
, arg1
, (CPU_TLB_SIZE
- 1) << CPU_TLB_ENTRY_BITS
);
1000 /* add arg1, x, arg1 */
1001 tcg_out_addi(s
, arg1
, offsetof(CPUArchState
,
1002 tlb_table
[mem_index
][0].addr_write
));
1004 /* add env, arg1, arg1 */
1005 tcg_out_arith(s
, arg1
, TCG_AREG0
, arg1
, ARITH_ADD
);
1007 /* ld [arg1], arg2 */
1008 tcg_out32(s
, TARGET_LD_OP
| INSN_RD(arg2
) | INSN_RS1(arg1
) |
1009 INSN_RS2(TCG_REG_G0
));
1011 /* subcc arg0, arg2, %g0 */
1012 tcg_out_arith(s
, TCG_REG_G0
, arg0
, arg2
, ARITH_SUBCC
);
1017 be,pt %xcc label1 */
1018 label1_ptr
= (uint32_t *)s
->code_ptr
;
1021 /* mov (delay slot) */
1022 tcg_out_mov(s
, TCG_TYPE_PTR
, arg0
, addr_reg
);
1025 tcg_out_mov(s
, TCG_TYPE_REG
, arg1
, data_reg
);
1028 tcg_out_movi(s
, TCG_TYPE_I32
, arg2
, mem_index
);
1030 /* XXX/FIXME: suboptimal */
1031 tcg_out_mov(s
, TCG_TYPE_I32
, tcg_target_call_iarg_regs
[3],
1032 tcg_target_call_iarg_regs
[2]);
1033 tcg_out_mov(s
, TCG_TYPE_I64
, tcg_target_call_iarg_regs
[2],
1034 tcg_target_call_iarg_regs
[1]);
1035 tcg_out_mov(s
, TCG_TYPE_TL
, tcg_target_call_iarg_regs
[1],
1036 tcg_target_call_iarg_regs
[0]);
1037 tcg_out_mov(s
, TCG_TYPE_PTR
, tcg_target_call_iarg_regs
[0],
1039 /* XXX: move that code at the end of the TB */
1040 /* qemu_st_helper[s_bits](arg0, arg1, arg2) */
1041 tcg_out32(s
, CALL
| ((((tcg_target_ulong
)qemu_st_helpers
[s_bits
]
1042 - (tcg_target_ulong
)s
->code_ptr
) >> 2)
1044 /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
1047 tcg_out_ldst(s
, TCG_AREG0
, TCG_REG_CALL_STACK
,
1048 TCG_TARGET_CALL_STACK_OFFSET
- TCG_STATIC_CALL_ARGS_SIZE
-
1049 sizeof(long), HOST_ST_OP
);
1050 tcg_out_ldst(s
, TCG_AREG0
, TCG_REG_CALL_STACK
,
1051 TCG_TARGET_CALL_STACK_OFFSET
- TCG_STATIC_CALL_ARGS_SIZE
-
1052 sizeof(long), HOST_LD_OP
);
1056 label2_ptr
= (uint32_t *)s
->code_ptr
;
1059 /* nop (delay slot) */
1062 #if TARGET_LONG_BITS == 32
1064 *label1_ptr
= (INSN_OP(0) | INSN_COND(COND_E
, 0) | INSN_OP2(0x2) |
1065 INSN_OFF22((unsigned long)s
->code_ptr
-
1066 (unsigned long)label1_ptr
));
1068 /* be,pt %xcc label1 */
1069 *label1_ptr
= (INSN_OP(0) | INSN_COND(COND_E
, 0) | INSN_OP2(0x1) |
1070 (0x5 << 19) | INSN_OFF19((unsigned long)s
->code_ptr
-
1071 (unsigned long)label1_ptr
));
1074 /* ld [arg1 + x], arg1 */
1075 tcg_out_ldst(s
, arg1
, arg1
, offsetof(CPUTLBEntry
, addend
) -
1076 offsetof(CPUTLBEntry
, addr_write
), TARGET_ADDEND_LD_OP
);
1078 #if TARGET_LONG_BITS == 32
1079 /* and addr_reg, x, arg0 */
1080 tcg_out_movi(s
, TCG_TYPE_I32
, TCG_REG_I5
, 0xffffffff);
1081 tcg_out_arith(s
, arg0
, addr_reg
, TCG_REG_I5
, ARITH_AND
);
1082 /* add arg0, arg1, arg0 */
1083 tcg_out_arith(s
, arg0
, arg0
, arg1
, ARITH_ADD
);
1085 /* add addr_reg, arg1, arg0 */
1086 tcg_out_arith(s
, arg0
, addr_reg
, arg1
, ARITH_ADD
);
1095 /* stb data_reg, [arg0] */
1096 tcg_out_ldst(s
, data_reg
, arg0
, 0, STB
);
1099 #ifdef TARGET_WORDS_BIGENDIAN
1100 /* sth data_reg, [arg0] */
1101 tcg_out_ldst(s
, data_reg
, arg0
, 0, STH
);
1103 /* stha data_reg, [arg0] ASI_PRIMARY_LITTLE */
1104 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, STHA
, ASI_PRIMARY_LITTLE
);
1108 #ifdef TARGET_WORDS_BIGENDIAN
1109 /* stw data_reg, [arg0] */
1110 tcg_out_ldst(s
, data_reg
, arg0
, 0, STW
);
1112 /* stwa data_reg, [arg0] ASI_PRIMARY_LITTLE */
1113 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, STWA
, ASI_PRIMARY_LITTLE
);
1117 #ifdef TARGET_WORDS_BIGENDIAN
1118 /* stx data_reg, [arg0] */
1119 tcg_out_ldst(s
, data_reg
, arg0
, 0, STX
);
1121 /* stxa data_reg, [arg0] ASI_PRIMARY_LITTLE */
1122 tcg_out_ldst_asi(s
, data_reg
, arg0
, 0, STXA
, ASI_PRIMARY_LITTLE
);
1129 #if defined(CONFIG_SOFTMMU)
1131 *label2_ptr
= (INSN_OP(0) | INSN_COND(COND_A
, 0) | INSN_OP2(0x2) |
1132 INSN_OFF22((unsigned long)s
->code_ptr
-
1133 (unsigned long)label2_ptr
));
1137 static inline void tcg_out_op(TCGContext
*s
, TCGOpcode opc
, const TCGArg
*args
,
1138 const int *const_args
)
1143 case INDEX_op_exit_tb
:
1144 tcg_out_movi(s
, TCG_TYPE_PTR
, TCG_REG_I0
, args
[0]);
1145 tcg_out32(s
, JMPL
| INSN_RD(TCG_REG_G0
) | INSN_RS1(TCG_REG_I7
) |
1147 tcg_out32(s
, RESTORE
| INSN_RD(TCG_REG_G0
) | INSN_RS1(TCG_REG_G0
) |
1148 INSN_RS2(TCG_REG_G0
));
1150 case INDEX_op_goto_tb
:
1151 if (s
->tb_jmp_offset
) {
1152 /* direct jump method */
1153 tcg_out_sethi(s
, TCG_REG_I5
, args
[0] & 0xffffe000);
1154 tcg_out32(s
, JMPL
| INSN_RD(TCG_REG_G0
) | INSN_RS1(TCG_REG_I5
) |
1155 INSN_IMM13((args
[0] & 0x1fff)));
1156 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1158 /* indirect jump method */
1159 tcg_out_ld_ptr(s
, TCG_REG_I5
, (tcg_target_long
)(s
->tb_next
+ args
[0]));
1160 tcg_out32(s
, JMPL
| INSN_RD(TCG_REG_G0
) | INSN_RS1(TCG_REG_I5
) |
1161 INSN_RS2(TCG_REG_G0
));
1164 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1168 tcg_out32(s
, CALL
| ((((tcg_target_ulong
)args
[0]
1169 - (tcg_target_ulong
)s
->code_ptr
) >> 2)
1172 tcg_out_ld_ptr(s
, TCG_REG_I5
,
1173 (tcg_target_long
)(s
->tb_next
+ args
[0]));
1174 tcg_out32(s
, JMPL
| INSN_RD(TCG_REG_O7
) | INSN_RS1(TCG_REG_I5
) |
1175 INSN_RS2(TCG_REG_G0
));
1177 /* Store AREG0 in stack to avoid ugly glibc bugs that mangle
1180 tcg_out_ldst(s
, TCG_AREG0
, TCG_REG_CALL_STACK
,
1181 TCG_TARGET_CALL_STACK_OFFSET
- TCG_STATIC_CALL_ARGS_SIZE
-
1182 sizeof(long), HOST_ST_OP
);
1183 tcg_out_ldst(s
, TCG_AREG0
, TCG_REG_CALL_STACK
,
1184 TCG_TARGET_CALL_STACK_OFFSET
- TCG_STATIC_CALL_ARGS_SIZE
-
1185 sizeof(long), HOST_LD_OP
);
1189 tcg_out_branch_i32(s
, COND_A
, args
[0]);
1192 case INDEX_op_movi_i32
:
1193 tcg_out_movi(s
, TCG_TYPE_I32
, args
[0], (uint32_t)args
[1]);
1196 #if TCG_TARGET_REG_BITS == 64
1197 #define OP_32_64(x) \
1198 glue(glue(case INDEX_op_, x), _i32): \
1199 glue(glue(case INDEX_op_, x), _i64)
1201 #define OP_32_64(x) \
1202 glue(glue(case INDEX_op_, x), _i32)
1205 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDUB
);
1208 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDSB
);
1211 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDUH
);
1214 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDSH
);
1216 case INDEX_op_ld_i32
:
1217 #if TCG_TARGET_REG_BITS == 64
1218 case INDEX_op_ld32u_i64
:
1220 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDUW
);
1223 tcg_out_ldst(s
, args
[0], args
[1], args
[2], STB
);
1226 tcg_out_ldst(s
, args
[0], args
[1], args
[2], STH
);
1228 case INDEX_op_st_i32
:
1229 #if TCG_TARGET_REG_BITS == 64
1230 case INDEX_op_st32_i64
:
1232 tcg_out_ldst(s
, args
[0], args
[1], args
[2], STW
);
1255 case INDEX_op_shl_i32
:
1258 case INDEX_op_shr_i32
:
1261 case INDEX_op_sar_i32
:
1264 case INDEX_op_mul_i32
:
1275 case INDEX_op_div_i32
:
1276 tcg_out_div32(s
, args
[0], args
[1], args
[2], const_args
[2], 0);
1278 case INDEX_op_divu_i32
:
1279 tcg_out_div32(s
, args
[0], args
[1], args
[2], const_args
[2], 1);
1282 case INDEX_op_rem_i32
:
1283 case INDEX_op_remu_i32
:
1284 tcg_out_div32(s
, TCG_REG_I5
, args
[1], args
[2], const_args
[2],
1285 opc
== INDEX_op_remu_i32
);
1286 tcg_out_arithc(s
, TCG_REG_I5
, TCG_REG_I5
, args
[2], const_args
[2],
1288 tcg_out_arith(s
, args
[0], args
[1], TCG_REG_I5
, ARITH_SUB
);
1291 case INDEX_op_brcond_i32
:
1292 tcg_out_brcond_i32(s
, args
[2], args
[0], args
[1], const_args
[1],
1295 case INDEX_op_setcond_i32
:
1296 tcg_out_setcond_i32(s
, args
[3], args
[0], args
[1],
1297 args
[2], const_args
[2]);
1300 #if TCG_TARGET_REG_BITS == 32
1301 case INDEX_op_brcond2_i32
:
1302 tcg_out_brcond2_i32(s
, args
[4], args
[0], args
[1],
1303 args
[2], const_args
[2],
1304 args
[3], const_args
[3], args
[5]);
1306 case INDEX_op_setcond2_i32
:
1307 tcg_out_setcond2_i32(s
, args
[5], args
[0], args
[1], args
[2],
1308 args
[3], const_args
[3],
1309 args
[4], const_args
[4]);
1311 case INDEX_op_add2_i32
:
1312 tcg_out_arithc(s
, args
[0], args
[2], args
[4], const_args
[4],
1314 tcg_out_arithc(s
, args
[1], args
[3], args
[5], const_args
[5],
1317 case INDEX_op_sub2_i32
:
1318 tcg_out_arithc(s
, args
[0], args
[2], args
[4], const_args
[4],
1320 tcg_out_arithc(s
, args
[1], args
[3], args
[5], const_args
[5],
1323 case INDEX_op_mulu2_i32
:
1324 tcg_out_arithc(s
, args
[0], args
[2], args
[3], const_args
[3],
1326 tcg_out_rdy(s
, args
[1]);
1330 case INDEX_op_qemu_ld8u
:
1331 tcg_out_qemu_ld(s
, args
, 0);
1333 case INDEX_op_qemu_ld8s
:
1334 tcg_out_qemu_ld(s
, args
, 0 | 4);
1336 case INDEX_op_qemu_ld16u
:
1337 tcg_out_qemu_ld(s
, args
, 1);
1339 case INDEX_op_qemu_ld16s
:
1340 tcg_out_qemu_ld(s
, args
, 1 | 4);
1342 case INDEX_op_qemu_ld32
:
1343 #if TCG_TARGET_REG_BITS == 64
1344 case INDEX_op_qemu_ld32u
:
1346 tcg_out_qemu_ld(s
, args
, 2);
1348 #if TCG_TARGET_REG_BITS == 64
1349 case INDEX_op_qemu_ld32s
:
1350 tcg_out_qemu_ld(s
, args
, 2 | 4);
1353 case INDEX_op_qemu_st8
:
1354 tcg_out_qemu_st(s
, args
, 0);
1356 case INDEX_op_qemu_st16
:
1357 tcg_out_qemu_st(s
, args
, 1);
1359 case INDEX_op_qemu_st32
:
1360 tcg_out_qemu_st(s
, args
, 2);
1363 #if TCG_TARGET_REG_BITS == 64
1364 case INDEX_op_movi_i64
:
1365 tcg_out_movi(s
, TCG_TYPE_I64
, args
[0], args
[1]);
1367 case INDEX_op_ld32s_i64
:
1368 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDSW
);
1370 case INDEX_op_ld_i64
:
1371 tcg_out_ldst(s
, args
[0], args
[1], args
[2], LDX
);
1373 case INDEX_op_st_i64
:
1374 tcg_out_ldst(s
, args
[0], args
[1], args
[2], STX
);
1376 case INDEX_op_shl_i64
:
1379 case INDEX_op_shr_i64
:
1382 case INDEX_op_sar_i64
:
1385 case INDEX_op_mul_i64
:
1388 case INDEX_op_div_i64
:
1391 case INDEX_op_divu_i64
:
1394 case INDEX_op_rem_i64
:
1395 case INDEX_op_remu_i64
:
1396 tcg_out_arithc(s
, TCG_REG_I5
, args
[1], args
[2], const_args
[2],
1397 opc
== INDEX_op_rem_i64
? ARITH_SDIVX
: ARITH_UDIVX
);
1398 tcg_out_arithc(s
, TCG_REG_I5
, TCG_REG_I5
, args
[2], const_args
[2],
1400 tcg_out_arith(s
, args
[0], args
[1], TCG_REG_I5
, ARITH_SUB
);
1402 case INDEX_op_ext32s_i64
:
1403 if (const_args
[1]) {
1404 tcg_out_movi(s
, TCG_TYPE_I64
, args
[0], (int32_t)args
[1]);
1406 tcg_out_arithi(s
, args
[0], args
[1], 0, SHIFT_SRA
);
1409 case INDEX_op_ext32u_i64
:
1410 if (const_args
[1]) {
1411 tcg_out_movi_imm32(s
, args
[0], args
[1]);
1413 tcg_out_arithi(s
, args
[0], args
[1], 0, SHIFT_SRL
);
1417 case INDEX_op_brcond_i64
:
1418 tcg_out_brcond_i64(s
, args
[2], args
[0], args
[1], const_args
[1],
1421 case INDEX_op_setcond_i64
:
1422 tcg_out_setcond_i64(s
, args
[3], args
[0], args
[1],
1423 args
[2], const_args
[2]);
1426 case INDEX_op_qemu_ld64
:
1427 tcg_out_qemu_ld(s
, args
, 3);
1429 case INDEX_op_qemu_st64
:
1430 tcg_out_qemu_st(s
, args
, 3);
1435 tcg_out_arithc(s
, args
[0], args
[1], args
[2], const_args
[2], c
);
1439 tcg_out_arithc(s
, args
[0], TCG_REG_G0
, args
[1], const_args
[1], c
);
1443 fprintf(stderr
, "unknown opcode 0x%x\n", opc
);
1448 static const TCGTargetOpDef sparc_op_defs
[] = {
1449 { INDEX_op_exit_tb
, { } },
1450 { INDEX_op_goto_tb
, { } },
1451 { INDEX_op_call
, { "ri" } },
1452 { INDEX_op_jmp
, { "ri" } },
1453 { INDEX_op_br
, { } },
1455 { INDEX_op_mov_i32
, { "r", "r" } },
1456 { INDEX_op_movi_i32
, { "r" } },
1457 { INDEX_op_ld8u_i32
, { "r", "r" } },
1458 { INDEX_op_ld8s_i32
, { "r", "r" } },
1459 { INDEX_op_ld16u_i32
, { "r", "r" } },
1460 { INDEX_op_ld16s_i32
, { "r", "r" } },
1461 { INDEX_op_ld_i32
, { "r", "r" } },
1462 { INDEX_op_st8_i32
, { "r", "r" } },
1463 { INDEX_op_st16_i32
, { "r", "r" } },
1464 { INDEX_op_st_i32
, { "r", "r" } },
1466 { INDEX_op_add_i32
, { "r", "r", "rJ" } },
1467 { INDEX_op_mul_i32
, { "r", "r", "rJ" } },
1468 { INDEX_op_div_i32
, { "r", "r", "rJ" } },
1469 { INDEX_op_divu_i32
, { "r", "r", "rJ" } },
1470 { INDEX_op_rem_i32
, { "r", "r", "rJ" } },
1471 { INDEX_op_remu_i32
, { "r", "r", "rJ" } },
1472 { INDEX_op_sub_i32
, { "r", "r", "rJ" } },
1473 { INDEX_op_and_i32
, { "r", "r", "rJ" } },
1474 { INDEX_op_andc_i32
, { "r", "r", "rJ" } },
1475 { INDEX_op_or_i32
, { "r", "r", "rJ" } },
1476 { INDEX_op_orc_i32
, { "r", "r", "rJ" } },
1477 { INDEX_op_xor_i32
, { "r", "r", "rJ" } },
1479 { INDEX_op_shl_i32
, { "r", "r", "rJ" } },
1480 { INDEX_op_shr_i32
, { "r", "r", "rJ" } },
1481 { INDEX_op_sar_i32
, { "r", "r", "rJ" } },
1483 { INDEX_op_neg_i32
, { "r", "rJ" } },
1484 { INDEX_op_not_i32
, { "r", "rJ" } },
1486 { INDEX_op_brcond_i32
, { "r", "rJ" } },
1487 { INDEX_op_setcond_i32
, { "r", "r", "rJ" } },
1489 #if TCG_TARGET_REG_BITS == 32
1490 { INDEX_op_brcond2_i32
, { "r", "r", "rJ", "rJ" } },
1491 { INDEX_op_setcond2_i32
, { "r", "r", "r", "rJ", "rJ" } },
1492 { INDEX_op_add2_i32
, { "r", "r", "r", "r", "rJ", "rJ" } },
1493 { INDEX_op_sub2_i32
, { "r", "r", "r", "r", "rJ", "rJ" } },
1494 { INDEX_op_mulu2_i32
, { "r", "r", "r", "rJ" } },
1497 { INDEX_op_qemu_ld8u
, { "r", "L" } },
1498 { INDEX_op_qemu_ld8s
, { "r", "L" } },
1499 { INDEX_op_qemu_ld16u
, { "r", "L" } },
1500 { INDEX_op_qemu_ld16s
, { "r", "L" } },
1501 { INDEX_op_qemu_ld32
, { "r", "L" } },
1502 #if TCG_TARGET_REG_BITS == 64
1503 { INDEX_op_qemu_ld32u
, { "r", "L" } },
1504 { INDEX_op_qemu_ld32s
, { "r", "L" } },
1507 { INDEX_op_qemu_st8
, { "L", "L" } },
1508 { INDEX_op_qemu_st16
, { "L", "L" } },
1509 { INDEX_op_qemu_st32
, { "L", "L" } },
1511 #if TCG_TARGET_REG_BITS == 64
1512 { INDEX_op_mov_i64
, { "r", "r" } },
1513 { INDEX_op_movi_i64
, { "r" } },
1514 { INDEX_op_ld8u_i64
, { "r", "r" } },
1515 { INDEX_op_ld8s_i64
, { "r", "r" } },
1516 { INDEX_op_ld16u_i64
, { "r", "r" } },
1517 { INDEX_op_ld16s_i64
, { "r", "r" } },
1518 { INDEX_op_ld32u_i64
, { "r", "r" } },
1519 { INDEX_op_ld32s_i64
, { "r", "r" } },
1520 { INDEX_op_ld_i64
, { "r", "r" } },
1521 { INDEX_op_st8_i64
, { "r", "r" } },
1522 { INDEX_op_st16_i64
, { "r", "r" } },
1523 { INDEX_op_st32_i64
, { "r", "r" } },
1524 { INDEX_op_st_i64
, { "r", "r" } },
1525 { INDEX_op_qemu_ld64
, { "L", "L" } },
1526 { INDEX_op_qemu_st64
, { "L", "L" } },
1528 { INDEX_op_add_i64
, { "r", "r", "rJ" } },
1529 { INDEX_op_mul_i64
, { "r", "r", "rJ" } },
1530 { INDEX_op_div_i64
, { "r", "r", "rJ" } },
1531 { INDEX_op_divu_i64
, { "r", "r", "rJ" } },
1532 { INDEX_op_rem_i64
, { "r", "r", "rJ" } },
1533 { INDEX_op_remu_i64
, { "r", "r", "rJ" } },
1534 { INDEX_op_sub_i64
, { "r", "r", "rJ" } },
1535 { INDEX_op_and_i64
, { "r", "r", "rJ" } },
1536 { INDEX_op_andc_i64
, { "r", "r", "rJ" } },
1537 { INDEX_op_or_i64
, { "r", "r", "rJ" } },
1538 { INDEX_op_orc_i64
, { "r", "r", "rJ" } },
1539 { INDEX_op_xor_i64
, { "r", "r", "rJ" } },
1541 { INDEX_op_shl_i64
, { "r", "r", "rJ" } },
1542 { INDEX_op_shr_i64
, { "r", "r", "rJ" } },
1543 { INDEX_op_sar_i64
, { "r", "r", "rJ" } },
1545 { INDEX_op_neg_i64
, { "r", "rJ" } },
1546 { INDEX_op_not_i64
, { "r", "rJ" } },
1548 { INDEX_op_ext32s_i64
, { "r", "ri" } },
1549 { INDEX_op_ext32u_i64
, { "r", "ri" } },
1551 { INDEX_op_brcond_i64
, { "r", "rJ" } },
1552 { INDEX_op_setcond_i64
, { "r", "r", "rJ" } },
1557 static void tcg_target_init(TCGContext
*s
)
1559 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffffffff);
1560 #if TCG_TARGET_REG_BITS == 64
1561 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I64
], 0, 0xffffffff);
1563 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1579 tcg_regset_clear(s
->reserved_regs
);
1580 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_G0
);
1581 #if TCG_TARGET_REG_BITS == 64
1582 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_I4
); // for internal use
1584 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_I5
); // for internal use
1585 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_I6
);
1586 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_I7
);
1587 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_O6
);
1588 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_O7
);
1589 tcg_add_target_add_op_defs(sparc_op_defs
);
1592 #if TCG_TARGET_REG_BITS == 64
1593 # define ELF_HOST_MACHINE EM_SPARCV9
1594 #elif defined(__sparc_v8plus__)
1595 # define ELF_HOST_MACHINE EM_SPARC32PLUS
1596 # define ELF_HOST_FLAGS EF_SPARC_32PLUS
1598 # define ELF_HOST_MACHINE EM_SPARC
1602 uint32_t len
__attribute__((aligned((sizeof(void *)))));
1605 char augmentation
[1];
1608 uint8_t return_column
;
1612 uint32_t len
__attribute__((aligned((sizeof(void *)))));
1613 uint32_t cie_offset
;
1614 tcg_target_long func_start
__attribute__((packed
));
1615 tcg_target_long func_len
__attribute__((packed
));
1616 uint8_t def_cfa
[TCG_TARGET_REG_BITS
== 64 ? 4 : 2];
1618 uint8_t ret_save
[3];
1626 static DebugFrame debug_frame
= {
1627 .cie
.len
= sizeof(DebugFrameCIE
)-4, /* length after .len member */
1630 .cie
.code_align
= 1,
1631 .cie
.data_align
= -sizeof(void *) & 0x7f,
1632 .cie
.return_column
= 15, /* o7 */
1634 .fde
.len
= sizeof(DebugFrameFDE
)-4, /* length after .len member */
1636 #if TCG_TARGET_REG_BITS == 64
1637 12, 30, /* DW_CFA_def_cfa i6, 2047 */
1638 (2047 & 0x7f) | 0x80, (2047 >> 7)
1640 13, 30 /* DW_CFA_def_cfa_register i6 */
1643 .fde
.win_save
= 0x2d, /* DW_CFA_GNU_window_save */
1644 .fde
.ret_save
= { 9, 15, 31 }, /* DW_CFA_register o7, i7 */
1647 void tcg_register_jit(void *buf
, size_t buf_size
)
1649 debug_frame
.fde
.func_start
= (tcg_target_long
) buf
;
1650 debug_frame
.fde
.func_len
= buf_size
;
1652 tcg_register_jit_int(buf
, buf_size
, &debug_frame
, sizeof(debug_frame
));