2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Andrzej Zaborowski
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
25 #if defined(__ARM_ARCH_7__) || \
26 defined(__ARM_ARCH_7A__) || \
27 defined(__ARM_ARCH_7EM__) || \
28 defined(__ARM_ARCH_7M__) || \
29 defined(__ARM_ARCH_7R__)
30 #define USE_ARMV7_INSTRUCTIONS
33 #if defined(USE_ARMV7_INSTRUCTIONS) || \
34 defined(__ARM_ARCH_6J__) || \
35 defined(__ARM_ARCH_6K__) || \
36 defined(__ARM_ARCH_6T2__) || \
37 defined(__ARM_ARCH_6Z__) || \
38 defined(__ARM_ARCH_6ZK__)
39 #define USE_ARMV6_INSTRUCTIONS
42 #if defined(USE_ARMV6_INSTRUCTIONS) || \
43 defined(__ARM_ARCH_5T__) || \
44 defined(__ARM_ARCH_5TE__) || \
45 defined(__ARM_ARCH_5TEJ__)
46 #define USE_ARMV5_INSTRUCTIONS
49 #ifdef USE_ARMV5_INSTRUCTIONS
50 static const int use_armv5_instructions
= 1;
52 static const int use_armv5_instructions
= 0;
54 #undef USE_ARMV5_INSTRUCTIONS
56 #ifdef USE_ARMV6_INSTRUCTIONS
57 static const int use_armv6_instructions
= 1;
59 static const int use_armv6_instructions
= 0;
61 #undef USE_ARMV6_INSTRUCTIONS
63 #ifdef USE_ARMV7_INSTRUCTIONS
64 static const int use_armv7_instructions
= 1;
66 static const int use_armv7_instructions
= 0;
68 #undef USE_ARMV7_INSTRUCTIONS
71 static const char * const tcg_target_reg_names
[TCG_TARGET_NB_REGS
] = {
91 static const int tcg_target_reg_alloc_order
[] = {
109 static const int tcg_target_call_iarg_regs
[4] = {
110 TCG_REG_R0
, TCG_REG_R1
, TCG_REG_R2
, TCG_REG_R3
112 static const int tcg_target_call_oarg_regs
[2] = {
113 TCG_REG_R0
, TCG_REG_R1
116 static void patch_reloc(uint8_t *code_ptr
, int type
,
117 tcg_target_long value
, tcg_target_long addend
)
121 *(uint32_t *) code_ptr
= value
;
130 *(uint32_t *) code_ptr
= ((*(uint32_t *) code_ptr
) & 0xff000000) |
131 (((value
- ((tcg_target_long
) code_ptr
+ 8)) >> 2) & 0xffffff);
136 /* maximum number of register used for input function arguments */
137 static inline int tcg_target_get_call_iarg_regs_count(int flags
)
142 /* parse target specific constraints */
143 static int target_parse_constraint(TCGArgConstraint
*ct
, const char **pct_str
)
150 ct
->ct
|= TCG_CT_CONST_ARM
;
154 #ifndef CONFIG_SOFTMMU
160 ct
->ct
|= TCG_CT_REG
;
161 tcg_regset_set32(ct
->u
.regs
, 0, (1 << TCG_TARGET_NB_REGS
) - 1);
164 #ifdef CONFIG_SOFTMMU
165 /* qemu_ld/st inputs (unless 'X', 'd' or 'D') */
167 ct
->ct
|= TCG_CT_REG
;
168 tcg_regset_set32(ct
->u
.regs
, 0, (1 << TCG_TARGET_NB_REGS
) - 1);
169 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R0
);
170 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R1
);
173 /* qemu_ld64 data_reg */
175 ct
->ct
|= TCG_CT_REG
;
176 tcg_regset_set32(ct
->u
.regs
, 0, (1 << TCG_TARGET_NB_REGS
) - 1);
177 /* r1 is still needed to load data_reg2, so don't use it. */
178 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R1
);
181 /* qemu_ld/st64 data_reg2 */
183 ct
->ct
|= TCG_CT_REG
;
184 tcg_regset_set32(ct
->u
.regs
, 0, (1 << TCG_TARGET_NB_REGS
) - 1);
185 /* r0, r1 and optionally r2 will be overwritten by the address
186 * and the low word of data, so don't use these. */
187 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R0
);
188 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R1
);
189 # if TARGET_LONG_BITS == 64
190 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R2
);
194 # if TARGET_LONG_BITS == 64
195 /* qemu_ld/st addr_reg2 */
197 ct
->ct
|= TCG_CT_REG
;
198 tcg_regset_set32(ct
->u
.regs
, 0, (1 << TCG_TARGET_NB_REGS
) - 1);
199 /* r0 will be overwritten by the low word of base, so don't use it. */
200 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R0
);
201 tcg_regset_reset_reg(ct
->u
.regs
, TCG_REG_R1
);
215 static inline uint32_t rotl(uint32_t val
, int n
)
217 return (val
<< n
) | (val
>> (32 - n
));
220 /* ARM immediates for ALU instructions are made of an unsigned 8-bit
221 right-rotated by an even amount between 0 and 30. */
222 static inline int encode_imm(uint32_t imm
)
226 /* simple case, only lower bits */
227 if ((imm
& ~0xff) == 0)
229 /* then try a simple even shift */
230 shift
= ctz32(imm
) & ~1;
231 if (((imm
>> shift
) & ~0xff) == 0)
233 /* now try harder with rotations */
234 if ((rotl(imm
, 2) & ~0xff) == 0)
236 if ((rotl(imm
, 4) & ~0xff) == 0)
238 if ((rotl(imm
, 6) & ~0xff) == 0)
240 /* imm can't be encoded */
244 static inline int check_fit_imm(uint32_t imm
)
246 return encode_imm(imm
) >= 0;
249 /* Test if a constant matches the constraint.
250 * TODO: define constraints for:
252 * ldr/str offset: between -0xfff and 0xfff
253 * ldrh/strh offset: between -0xff and 0xff
254 * mov operand2: values represented with x << (2 * y), x < 0x100
255 * add, sub, eor...: ditto
257 static inline int tcg_target_const_match(tcg_target_long val
,
258 const TCGArgConstraint
*arg_ct
)
262 if (ct
& TCG_CT_CONST
)
264 else if ((ct
& TCG_CT_CONST_ARM
) && check_fit_imm(val
))
270 enum arm_data_opc_e
{
288 #define TO_CPSR(opc) \
289 ((opc == ARITH_CMP || opc == ARITH_CMN || opc == ARITH_TST) << 20)
291 #define SHIFT_IMM_LSL(im) (((im) << 7) | 0x00)
292 #define SHIFT_IMM_LSR(im) (((im) << 7) | 0x20)
293 #define SHIFT_IMM_ASR(im) (((im) << 7) | 0x40)
294 #define SHIFT_IMM_ROR(im) (((im) << 7) | 0x60)
295 #define SHIFT_REG_LSL(rs) (((rs) << 8) | 0x10)
296 #define SHIFT_REG_LSR(rs) (((rs) << 8) | 0x30)
297 #define SHIFT_REG_ASR(rs) (((rs) << 8) | 0x50)
298 #define SHIFT_REG_ROR(rs) (((rs) << 8) | 0x70)
300 enum arm_cond_code_e
{
303 COND_CS
= 0x2, /* Unsigned greater or equal */
304 COND_CC
= 0x3, /* Unsigned less than */
305 COND_MI
= 0x4, /* Negative */
306 COND_PL
= 0x5, /* Zero or greater */
307 COND_VS
= 0x6, /* Overflow */
308 COND_VC
= 0x7, /* No overflow */
309 COND_HI
= 0x8, /* Unsigned greater than */
310 COND_LS
= 0x9, /* Unsigned less or equal */
318 static const uint8_t tcg_cond_to_arm_cond
[10] = {
319 [TCG_COND_EQ
] = COND_EQ
,
320 [TCG_COND_NE
] = COND_NE
,
321 [TCG_COND_LT
] = COND_LT
,
322 [TCG_COND_GE
] = COND_GE
,
323 [TCG_COND_LE
] = COND_LE
,
324 [TCG_COND_GT
] = COND_GT
,
326 [TCG_COND_LTU
] = COND_CC
,
327 [TCG_COND_GEU
] = COND_CS
,
328 [TCG_COND_LEU
] = COND_LS
,
329 [TCG_COND_GTU
] = COND_HI
,
332 static inline void tcg_out_bx(TCGContext
*s
, int cond
, int rn
)
334 tcg_out32(s
, (cond
<< 28) | 0x012fff10 | rn
);
337 static inline void tcg_out_b(TCGContext
*s
, int cond
, int32_t offset
)
339 tcg_out32(s
, (cond
<< 28) | 0x0a000000 |
340 (((offset
- 8) >> 2) & 0x00ffffff));
343 static inline void tcg_out_b_noaddr(TCGContext
*s
, int cond
)
345 #ifdef HOST_WORDS_BIGENDIAN
346 tcg_out8(s
, (cond
<< 4) | 0x0a);
350 tcg_out8(s
, (cond
<< 4) | 0x0a);
354 static inline void tcg_out_bl(TCGContext
*s
, int cond
, int32_t offset
)
356 tcg_out32(s
, (cond
<< 28) | 0x0b000000 |
357 (((offset
- 8) >> 2) & 0x00ffffff));
360 static inline void tcg_out_blx(TCGContext
*s
, int cond
, int rn
)
362 tcg_out32(s
, (cond
<< 28) | 0x012fff30 | rn
);
365 static inline void tcg_out_dat_reg(TCGContext
*s
,
366 int cond
, int opc
, int rd
, int rn
, int rm
, int shift
)
368 tcg_out32(s
, (cond
<< 28) | (0 << 25) | (opc
<< 21) | TO_CPSR(opc
) |
369 (rn
<< 16) | (rd
<< 12) | shift
| rm
);
372 static inline void tcg_out_dat_reg2(TCGContext
*s
,
373 int cond
, int opc0
, int opc1
, int rd0
, int rd1
,
374 int rn0
, int rn1
, int rm0
, int rm1
, int shift
)
376 if (rd0
== rn1
|| rd0
== rm1
) {
377 tcg_out32(s
, (cond
<< 28) | (0 << 25) | (opc0
<< 21) | (1 << 20) |
378 (rn0
<< 16) | (8 << 12) | shift
| rm0
);
379 tcg_out32(s
, (cond
<< 28) | (0 << 25) | (opc1
<< 21) |
380 (rn1
<< 16) | (rd1
<< 12) | shift
| rm1
);
381 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
382 rd0
, 0, TCG_REG_R8
, SHIFT_IMM_LSL(0));
384 tcg_out32(s
, (cond
<< 28) | (0 << 25) | (opc0
<< 21) | (1 << 20) |
385 (rn0
<< 16) | (rd0
<< 12) | shift
| rm0
);
386 tcg_out32(s
, (cond
<< 28) | (0 << 25) | (opc1
<< 21) |
387 (rn1
<< 16) | (rd1
<< 12) | shift
| rm1
);
391 static inline void tcg_out_dat_imm(TCGContext
*s
,
392 int cond
, int opc
, int rd
, int rn
, int im
)
394 tcg_out32(s
, (cond
<< 28) | (1 << 25) | (opc
<< 21) | TO_CPSR(opc
) |
395 (rn
<< 16) | (rd
<< 12) | im
);
398 static inline void tcg_out_movi32(TCGContext
*s
,
399 int cond
, int rd
, int32_t arg
)
401 int offset
= (uint32_t) arg
- ((uint32_t) s
->code_ptr
+ 8);
403 /* TODO: This is very suboptimal, we can easily have a constant
404 * pool somewhere after all the instructions. */
406 if (arg
< 0 && arg
> -0x100)
407 return tcg_out_dat_imm(s
, cond
, ARITH_MVN
, rd
, 0, (~arg
) & 0xff);
409 if (offset
< 0x100 && offset
> -0x100)
411 tcg_out_dat_imm(s
, cond
, ARITH_ADD
, rd
, 15, offset
) :
412 tcg_out_dat_imm(s
, cond
, ARITH_SUB
, rd
, 15, -offset
);
414 if (use_armv7_instructions
) {
417 tcg_out32(s
, (cond
<< 28) | 0x03000000 | (rd
<< 12)
418 | ((arg
<< 4) & 0x000f0000) | (arg
& 0xfff));
419 if (arg
& 0xffff0000)
421 tcg_out32(s
, (cond
<< 28) | 0x03400000 | (rd
<< 12)
422 | ((arg
>> 12) & 0x000f0000) | ((arg
>> 16) & 0xfff));
424 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, rd
, 0, arg
& 0xff);
425 if (arg
& 0x0000ff00)
426 tcg_out_dat_imm(s
, cond
, ARITH_ORR
, rd
, rd
,
427 ((arg
>> 8) & 0xff) | 0xc00);
428 if (arg
& 0x00ff0000)
429 tcg_out_dat_imm(s
, cond
, ARITH_ORR
, rd
, rd
,
430 ((arg
>> 16) & 0xff) | 0x800);
431 if (arg
& 0xff000000)
432 tcg_out_dat_imm(s
, cond
, ARITH_ORR
, rd
, rd
,
433 ((arg
>> 24) & 0xff) | 0x400);
437 static inline void tcg_out_mul32(TCGContext
*s
,
438 int cond
, int rd
, int rs
, int rm
)
441 tcg_out32(s
, (cond
<< 28) | (rd
<< 16) | (0 << 12) |
442 (rs
<< 8) | 0x90 | rm
);
444 tcg_out32(s
, (cond
<< 28) | (rd
<< 16) | (0 << 12) |
445 (rm
<< 8) | 0x90 | rs
);
447 tcg_out32(s
, (cond
<< 28) | ( 8 << 16) | (0 << 12) |
448 (rs
<< 8) | 0x90 | rm
);
449 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
450 rd
, 0, TCG_REG_R8
, SHIFT_IMM_LSL(0));
454 static inline void tcg_out_umull32(TCGContext
*s
,
455 int cond
, int rd0
, int rd1
, int rs
, int rm
)
457 if (rd0
!= rm
&& rd1
!= rm
)
458 tcg_out32(s
, (cond
<< 28) | 0x800090 |
459 (rd1
<< 16) | (rd0
<< 12) | (rs
<< 8) | rm
);
460 else if (rd0
!= rs
&& rd1
!= rs
)
461 tcg_out32(s
, (cond
<< 28) | 0x800090 |
462 (rd1
<< 16) | (rd0
<< 12) | (rm
<< 8) | rs
);
464 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
465 TCG_REG_R8
, 0, rm
, SHIFT_IMM_LSL(0));
466 tcg_out32(s
, (cond
<< 28) | 0x800098 |
467 (rd1
<< 16) | (rd0
<< 12) | (rs
<< 8));
471 static inline void tcg_out_smull32(TCGContext
*s
,
472 int cond
, int rd0
, int rd1
, int rs
, int rm
)
474 if (rd0
!= rm
&& rd1
!= rm
)
475 tcg_out32(s
, (cond
<< 28) | 0xc00090 |
476 (rd1
<< 16) | (rd0
<< 12) | (rs
<< 8) | rm
);
477 else if (rd0
!= rs
&& rd1
!= rs
)
478 tcg_out32(s
, (cond
<< 28) | 0xc00090 |
479 (rd1
<< 16) | (rd0
<< 12) | (rm
<< 8) | rs
);
481 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
482 TCG_REG_R8
, 0, rm
, SHIFT_IMM_LSL(0));
483 tcg_out32(s
, (cond
<< 28) | 0xc00098 |
484 (rd1
<< 16) | (rd0
<< 12) | (rs
<< 8));
488 static inline void tcg_out_ext8s(TCGContext
*s
, int cond
,
491 if (use_armv6_instructions
) {
493 tcg_out32(s
, 0x06af0070 | (cond
<< 28) | (rd
<< 12) | rn
);
495 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
496 rd
, 0, rn
, SHIFT_IMM_LSL(24));
497 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
498 rd
, 0, rd
, SHIFT_IMM_ASR(24));
502 static inline void tcg_out_ext16s(TCGContext
*s
, int cond
,
505 if (use_armv6_instructions
) {
507 tcg_out32(s
, 0x06bf0070 | (cond
<< 28) | (rd
<< 12) | rn
);
509 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
510 rd
, 0, rn
, SHIFT_IMM_LSL(16));
511 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
512 rd
, 0, rd
, SHIFT_IMM_ASR(16));
516 static inline void tcg_out_ext16u(TCGContext
*s
, int cond
,
519 if (use_armv6_instructions
) {
521 tcg_out32(s
, 0x06ff0070 | (cond
<< 28) | (rd
<< 12) | rn
);
523 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
524 rd
, 0, rn
, SHIFT_IMM_LSL(16));
525 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
526 rd
, 0, rd
, SHIFT_IMM_LSR(16));
530 static inline void tcg_out_bswap16(TCGContext
*s
, int cond
, int rd
, int rn
)
532 if (use_armv6_instructions
) {
534 tcg_out32(s
, 0x06bf0fb0 | (cond
<< 28) | (rd
<< 12) | rn
);
536 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
537 TCG_REG_R8
, 0, rn
, SHIFT_IMM_LSL(24));
538 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
539 TCG_REG_R8
, 0, TCG_REG_R8
, SHIFT_IMM_LSR(16));
540 tcg_out_dat_reg(s
, cond
, ARITH_ORR
,
541 rd
, TCG_REG_R8
, rn
, SHIFT_IMM_LSR(8));
545 static inline void tcg_out_bswap32(TCGContext
*s
, int cond
, int rd
, int rn
)
547 if (use_armv6_instructions
) {
549 tcg_out32(s
, 0x06bf0f30 | (cond
<< 28) | (rd
<< 12) | rn
);
551 tcg_out_dat_reg(s
, cond
, ARITH_EOR
,
552 TCG_REG_R8
, rn
, rn
, SHIFT_IMM_ROR(16));
553 tcg_out_dat_imm(s
, cond
, ARITH_BIC
,
554 TCG_REG_R8
, TCG_REG_R8
, 0xff | 0x800);
555 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
556 rd
, 0, rn
, SHIFT_IMM_ROR(8));
557 tcg_out_dat_reg(s
, cond
, ARITH_EOR
,
558 rd
, rd
, TCG_REG_R8
, SHIFT_IMM_LSR(8));
562 static inline void tcg_out_ld32_12(TCGContext
*s
, int cond
,
563 int rd
, int rn
, tcg_target_long im
)
566 tcg_out32(s
, (cond
<< 28) | 0x05900000 |
567 (rn
<< 16) | (rd
<< 12) | (im
& 0xfff));
569 tcg_out32(s
, (cond
<< 28) | 0x05100000 |
570 (rn
<< 16) | (rd
<< 12) | ((-im
) & 0xfff));
573 static inline void tcg_out_st32_12(TCGContext
*s
, int cond
,
574 int rd
, int rn
, tcg_target_long im
)
577 tcg_out32(s
, (cond
<< 28) | 0x05800000 |
578 (rn
<< 16) | (rd
<< 12) | (im
& 0xfff));
580 tcg_out32(s
, (cond
<< 28) | 0x05000000 |
581 (rn
<< 16) | (rd
<< 12) | ((-im
) & 0xfff));
584 static inline void tcg_out_ld32_r(TCGContext
*s
, int cond
,
585 int rd
, int rn
, int rm
)
587 tcg_out32(s
, (cond
<< 28) | 0x07900000 |
588 (rn
<< 16) | (rd
<< 12) | rm
);
591 static inline void tcg_out_st32_r(TCGContext
*s
, int cond
,
592 int rd
, int rn
, int rm
)
594 tcg_out32(s
, (cond
<< 28) | 0x07800000 |
595 (rn
<< 16) | (rd
<< 12) | rm
);
598 /* Register pre-increment with base writeback. */
599 static inline void tcg_out_ld32_rwb(TCGContext
*s
, int cond
,
600 int rd
, int rn
, int rm
)
602 tcg_out32(s
, (cond
<< 28) | 0x07b00000 |
603 (rn
<< 16) | (rd
<< 12) | rm
);
606 static inline void tcg_out_st32_rwb(TCGContext
*s
, int cond
,
607 int rd
, int rn
, int rm
)
609 tcg_out32(s
, (cond
<< 28) | 0x07a00000 |
610 (rn
<< 16) | (rd
<< 12) | rm
);
613 static inline void tcg_out_ld16u_8(TCGContext
*s
, int cond
,
614 int rd
, int rn
, tcg_target_long im
)
617 tcg_out32(s
, (cond
<< 28) | 0x01d000b0 |
618 (rn
<< 16) | (rd
<< 12) |
619 ((im
& 0xf0) << 4) | (im
& 0xf));
621 tcg_out32(s
, (cond
<< 28) | 0x015000b0 |
622 (rn
<< 16) | (rd
<< 12) |
623 (((-im
) & 0xf0) << 4) | ((-im
) & 0xf));
626 static inline void tcg_out_st16_8(TCGContext
*s
, int cond
,
627 int rd
, int rn
, tcg_target_long im
)
630 tcg_out32(s
, (cond
<< 28) | 0x01c000b0 |
631 (rn
<< 16) | (rd
<< 12) |
632 ((im
& 0xf0) << 4) | (im
& 0xf));
634 tcg_out32(s
, (cond
<< 28) | 0x014000b0 |
635 (rn
<< 16) | (rd
<< 12) |
636 (((-im
) & 0xf0) << 4) | ((-im
) & 0xf));
639 static inline void tcg_out_ld16u_r(TCGContext
*s
, int cond
,
640 int rd
, int rn
, int rm
)
642 tcg_out32(s
, (cond
<< 28) | 0x019000b0 |
643 (rn
<< 16) | (rd
<< 12) | rm
);
646 static inline void tcg_out_st16_r(TCGContext
*s
, int cond
,
647 int rd
, int rn
, int rm
)
649 tcg_out32(s
, (cond
<< 28) | 0x018000b0 |
650 (rn
<< 16) | (rd
<< 12) | rm
);
653 static inline void tcg_out_ld16s_8(TCGContext
*s
, int cond
,
654 int rd
, int rn
, tcg_target_long im
)
657 tcg_out32(s
, (cond
<< 28) | 0x01d000f0 |
658 (rn
<< 16) | (rd
<< 12) |
659 ((im
& 0xf0) << 4) | (im
& 0xf));
661 tcg_out32(s
, (cond
<< 28) | 0x015000f0 |
662 (rn
<< 16) | (rd
<< 12) |
663 (((-im
) & 0xf0) << 4) | ((-im
) & 0xf));
666 static inline void tcg_out_ld16s_r(TCGContext
*s
, int cond
,
667 int rd
, int rn
, int rm
)
669 tcg_out32(s
, (cond
<< 28) | 0x019000f0 |
670 (rn
<< 16) | (rd
<< 12) | rm
);
673 static inline void tcg_out_ld8_12(TCGContext
*s
, int cond
,
674 int rd
, int rn
, tcg_target_long im
)
677 tcg_out32(s
, (cond
<< 28) | 0x05d00000 |
678 (rn
<< 16) | (rd
<< 12) | (im
& 0xfff));
680 tcg_out32(s
, (cond
<< 28) | 0x05500000 |
681 (rn
<< 16) | (rd
<< 12) | ((-im
) & 0xfff));
684 static inline void tcg_out_st8_12(TCGContext
*s
, int cond
,
685 int rd
, int rn
, tcg_target_long im
)
688 tcg_out32(s
, (cond
<< 28) | 0x05c00000 |
689 (rn
<< 16) | (rd
<< 12) | (im
& 0xfff));
691 tcg_out32(s
, (cond
<< 28) | 0x05400000 |
692 (rn
<< 16) | (rd
<< 12) | ((-im
) & 0xfff));
695 static inline void tcg_out_ld8_r(TCGContext
*s
, int cond
,
696 int rd
, int rn
, int rm
)
698 tcg_out32(s
, (cond
<< 28) | 0x07d00000 |
699 (rn
<< 16) | (rd
<< 12) | rm
);
702 static inline void tcg_out_st8_r(TCGContext
*s
, int cond
,
703 int rd
, int rn
, int rm
)
705 tcg_out32(s
, (cond
<< 28) | 0x07c00000 |
706 (rn
<< 16) | (rd
<< 12) | rm
);
709 static inline void tcg_out_ld8s_8(TCGContext
*s
, int cond
,
710 int rd
, int rn
, tcg_target_long im
)
713 tcg_out32(s
, (cond
<< 28) | 0x01d000d0 |
714 (rn
<< 16) | (rd
<< 12) |
715 ((im
& 0xf0) << 4) | (im
& 0xf));
717 tcg_out32(s
, (cond
<< 28) | 0x015000d0 |
718 (rn
<< 16) | (rd
<< 12) |
719 (((-im
) & 0xf0) << 4) | ((-im
) & 0xf));
722 static inline void tcg_out_ld8s_r(TCGContext
*s
, int cond
,
723 int rd
, int rn
, int rm
)
725 tcg_out32(s
, (cond
<< 28) | 0x019000d0 |
726 (rn
<< 16) | (rd
<< 12) | rm
);
729 static inline void tcg_out_ld32u(TCGContext
*s
, int cond
,
730 int rd
, int rn
, int32_t offset
)
732 if (offset
> 0xfff || offset
< -0xfff) {
733 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
734 tcg_out_ld32_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
736 tcg_out_ld32_12(s
, cond
, rd
, rn
, offset
);
739 static inline void tcg_out_st32(TCGContext
*s
, int cond
,
740 int rd
, int rn
, int32_t offset
)
742 if (offset
> 0xfff || offset
< -0xfff) {
743 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
744 tcg_out_st32_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
746 tcg_out_st32_12(s
, cond
, rd
, rn
, offset
);
749 static inline void tcg_out_ld16u(TCGContext
*s
, int cond
,
750 int rd
, int rn
, int32_t offset
)
752 if (offset
> 0xff || offset
< -0xff) {
753 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
754 tcg_out_ld16u_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
756 tcg_out_ld16u_8(s
, cond
, rd
, rn
, offset
);
759 static inline void tcg_out_ld16s(TCGContext
*s
, int cond
,
760 int rd
, int rn
, int32_t offset
)
762 if (offset
> 0xff || offset
< -0xff) {
763 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
764 tcg_out_ld16s_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
766 tcg_out_ld16s_8(s
, cond
, rd
, rn
, offset
);
769 static inline void tcg_out_st16(TCGContext
*s
, int cond
,
770 int rd
, int rn
, int32_t offset
)
772 if (offset
> 0xff || offset
< -0xff) {
773 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
774 tcg_out_st16_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
776 tcg_out_st16_8(s
, cond
, rd
, rn
, offset
);
779 static inline void tcg_out_ld8u(TCGContext
*s
, int cond
,
780 int rd
, int rn
, int32_t offset
)
782 if (offset
> 0xfff || offset
< -0xfff) {
783 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
784 tcg_out_ld8_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
786 tcg_out_ld8_12(s
, cond
, rd
, rn
, offset
);
789 static inline void tcg_out_ld8s(TCGContext
*s
, int cond
,
790 int rd
, int rn
, int32_t offset
)
792 if (offset
> 0xff || offset
< -0xff) {
793 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
794 tcg_out_ld8s_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
796 tcg_out_ld8s_8(s
, cond
, rd
, rn
, offset
);
799 static inline void tcg_out_st8(TCGContext
*s
, int cond
,
800 int rd
, int rn
, int32_t offset
)
802 if (offset
> 0xfff || offset
< -0xfff) {
803 tcg_out_movi32(s
, cond
, TCG_REG_R8
, offset
);
804 tcg_out_st8_r(s
, cond
, rd
, rn
, TCG_REG_R8
);
806 tcg_out_st8_12(s
, cond
, rd
, rn
, offset
);
809 static inline void tcg_out_goto(TCGContext
*s
, int cond
, uint32_t addr
)
813 val
= addr
- (tcg_target_long
) s
->code_ptr
;
814 if (val
- 8 < 0x01fffffd && val
- 8 > -0x01fffffd)
815 tcg_out_b(s
, cond
, val
);
820 if (cond
== COND_AL
) {
821 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_PC
, -4);
822 tcg_out32(s
, addr
); /* XXX: This is l->u.value, can we use it? */
824 tcg_out_movi32(s
, cond
, TCG_REG_R8
, val
- 8);
825 tcg_out_dat_reg(s
, cond
, ARITH_ADD
,
826 TCG_REG_PC
, TCG_REG_PC
,
827 TCG_REG_R8
, SHIFT_IMM_LSL(0));
833 static inline void tcg_out_call(TCGContext
*s
, int cond
, uint32_t addr
)
837 val
= addr
- (tcg_target_long
) s
->code_ptr
;
838 if (val
< 0x01fffffd && val
> -0x01fffffd)
839 tcg_out_bl(s
, cond
, val
);
844 if (cond
== COND_AL
) {
845 tcg_out_dat_imm(s
, cond
, ARITH_ADD
, TCG_REG_R14
, TCG_REG_PC
, 4);
846 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_PC
, -4);
847 tcg_out32(s
, addr
); /* XXX: This is l->u.value, can we use it? */
849 tcg_out_movi32(s
, cond
, TCG_REG_R9
, addr
);
850 tcg_out_dat_reg(s
, cond
, ARITH_MOV
, TCG_REG_R14
, 0,
851 TCG_REG_PC
, SHIFT_IMM_LSL(0));
852 tcg_out_bx(s
, cond
, TCG_REG_R9
);
858 static inline void tcg_out_callr(TCGContext
*s
, int cond
, int arg
)
860 if (use_armv5_instructions
) {
861 tcg_out_blx(s
, cond
, arg
);
863 tcg_out_dat_reg(s
, cond
, ARITH_MOV
, TCG_REG_R14
, 0,
864 TCG_REG_PC
, SHIFT_IMM_LSL(0));
865 tcg_out_bx(s
, cond
, arg
);
869 static inline void tcg_out_goto_label(TCGContext
*s
, int cond
, int label_index
)
871 TCGLabel
*l
= &s
->labels
[label_index
];
874 tcg_out_goto(s
, cond
, l
->u
.value
);
875 else if (cond
== COND_AL
) {
876 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_PC
, -4);
877 tcg_out_reloc(s
, s
->code_ptr
, R_ARM_ABS32
, label_index
, 31337);
880 /* Probably this should be preferred even for COND_AL... */
881 tcg_out_reloc(s
, s
->code_ptr
, R_ARM_PC24
, label_index
, 31337);
882 tcg_out_b_noaddr(s
, cond
);
886 #ifdef CONFIG_SOFTMMU
888 #include "../../softmmu_defs.h"
890 static void *qemu_ld_helpers
[4] = {
897 static void *qemu_st_helpers
[4] = {
905 #define TLB_SHIFT (CPU_TLB_ENTRY_BITS + CPU_TLB_BITS)
907 static inline void tcg_out_qemu_ld(TCGContext
*s
, int cond
,
908 const TCGArg
*args
, int opc
)
910 int addr_reg
, data_reg
, data_reg2
;
911 #ifdef CONFIG_SOFTMMU
912 int mem_index
, s_bits
;
913 # if TARGET_LONG_BITS == 64
923 data_reg2
= 0; /* suppress warning */
925 #ifdef CONFIG_SOFTMMU
926 # if TARGET_LONG_BITS == 64
932 /* Should generate something like the following:
933 * shr r8, addr_reg, #TARGET_PAGE_BITS
934 * and r0, r8, #(CPU_TLB_SIZE - 1) @ Assumption: CPU_TLB_BITS <= 8
935 * add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS
937 # if CPU_TLB_BITS > 8
940 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
, TCG_REG_R8
,
941 0, addr_reg
, SHIFT_IMM_LSR(TARGET_PAGE_BITS
));
942 tcg_out_dat_imm(s
, COND_AL
, ARITH_AND
,
943 TCG_REG_R0
, TCG_REG_R8
, CPU_TLB_SIZE
- 1);
944 tcg_out_dat_reg(s
, COND_AL
, ARITH_ADD
, TCG_REG_R0
, TCG_AREG0
,
945 TCG_REG_R0
, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS
));
947 * ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_read))]
948 * below, the offset is likely to exceed 12 bits if mem_index != 0 and
949 * not exceed otherwise, so use an
950 * add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)
954 tcg_out_dat_imm(s
, COND_AL
, ARITH_ADD
, TCG_REG_R0
, TCG_REG_R0
,
955 (mem_index
<< (TLB_SHIFT
& 1)) |
956 ((16 - (TLB_SHIFT
>> 1)) << 8));
957 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_R1
, TCG_REG_R0
,
958 offsetof(CPUState
, tlb_table
[0][0].addr_read
));
959 tcg_out_dat_reg(s
, COND_AL
, ARITH_CMP
, 0, TCG_REG_R1
,
960 TCG_REG_R8
, SHIFT_IMM_LSL(TARGET_PAGE_BITS
));
961 /* Check alignment. */
963 tcg_out_dat_imm(s
, COND_EQ
, ARITH_TST
,
964 0, addr_reg
, (1 << s_bits
) - 1);
965 # if TARGET_LONG_BITS == 64
966 /* XXX: possibly we could use a block data load or writeback in
967 * the first access. */
968 tcg_out_ld32_12(s
, COND_EQ
, TCG_REG_R1
, TCG_REG_R0
,
969 offsetof(CPUState
, tlb_table
[0][0].addr_read
) + 4);
970 tcg_out_dat_reg(s
, COND_EQ
, ARITH_CMP
, 0,
971 TCG_REG_R1
, addr_reg2
, SHIFT_IMM_LSL(0));
973 tcg_out_ld32_12(s
, COND_EQ
, TCG_REG_R1
, TCG_REG_R0
,
974 offsetof(CPUState
, tlb_table
[0][0].addend
));
978 tcg_out_ld8_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
981 tcg_out_ld8s_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
984 tcg_out_ld16u_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
987 tcg_out_ld16s_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
991 tcg_out_ld32_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
994 tcg_out_ld32_rwb(s
, COND_EQ
, data_reg
, TCG_REG_R1
, addr_reg
);
995 tcg_out_ld32_12(s
, COND_EQ
, data_reg2
, TCG_REG_R1
, 4);
999 label_ptr
= (void *) s
->code_ptr
;
1000 tcg_out_b(s
, COND_EQ
, 8);
1002 /* TODO: move this code to where the constants pool will be */
1003 if (addr_reg
!= TCG_REG_R0
) {
1004 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1005 TCG_REG_R0
, 0, addr_reg
, SHIFT_IMM_LSL(0));
1007 # if TARGET_LONG_BITS == 32
1008 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R1
, 0, mem_index
);
1010 if (addr_reg2
!= TCG_REG_R1
) {
1011 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1012 TCG_REG_R1
, 0, addr_reg2
, SHIFT_IMM_LSL(0));
1014 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R2
, 0, mem_index
);
1016 tcg_out_bl(s
, cond
, (tcg_target_long
) qemu_ld_helpers
[s_bits
] -
1017 (tcg_target_long
) s
->code_ptr
);
1021 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1022 TCG_REG_R0
, 0, TCG_REG_R0
, SHIFT_IMM_LSL(24));
1023 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1024 data_reg
, 0, TCG_REG_R0
, SHIFT_IMM_ASR(24));
1027 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1028 TCG_REG_R0
, 0, TCG_REG_R0
, SHIFT_IMM_LSL(16));
1029 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1030 data_reg
, 0, TCG_REG_R0
, SHIFT_IMM_ASR(16));
1036 if (data_reg
!= TCG_REG_R0
) {
1037 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1038 data_reg
, 0, TCG_REG_R0
, SHIFT_IMM_LSL(0));
1042 if (data_reg
!= TCG_REG_R0
) {
1043 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1044 data_reg
, 0, TCG_REG_R0
, SHIFT_IMM_LSL(0));
1046 if (data_reg2
!= TCG_REG_R1
) {
1047 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1048 data_reg2
, 0, TCG_REG_R1
, SHIFT_IMM_LSL(0));
1053 *label_ptr
+= ((void *) s
->code_ptr
- (void *) label_ptr
- 8) >> 2;
1054 #else /* !CONFIG_SOFTMMU */
1056 uint32_t offset
= GUEST_BASE
;
1061 i
= ctz32(offset
) & ~1;
1062 rot
= ((32 - i
) << 7) & 0xf00;
1064 tcg_out_dat_imm(s
, COND_AL
, ARITH_ADD
, TCG_REG_R8
, addr_reg
,
1065 ((offset
>> i
) & 0xff) | rot
);
1066 addr_reg
= TCG_REG_R8
;
1067 offset
&= ~(0xff << i
);
1072 tcg_out_ld8_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1075 tcg_out_ld8s_8(s
, COND_AL
, data_reg
, addr_reg
, 0);
1078 tcg_out_ld16u_8(s
, COND_AL
, data_reg
, addr_reg
, 0);
1081 tcg_out_ld16s_8(s
, COND_AL
, data_reg
, addr_reg
, 0);
1085 tcg_out_ld32_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1088 /* TODO: use block load -
1089 * check that data_reg2 > data_reg or the other way */
1090 if (data_reg
== addr_reg
) {
1091 tcg_out_ld32_12(s
, COND_AL
, data_reg2
, addr_reg
, 4);
1092 tcg_out_ld32_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1094 tcg_out_ld32_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1095 tcg_out_ld32_12(s
, COND_AL
, data_reg2
, addr_reg
, 4);
1102 static inline void tcg_out_qemu_st(TCGContext
*s
, int cond
,
1103 const TCGArg
*args
, int opc
)
1105 int addr_reg
, data_reg
, data_reg2
;
1106 #ifdef CONFIG_SOFTMMU
1107 int mem_index
, s_bits
;
1108 # if TARGET_LONG_BITS == 64
1111 uint32_t *label_ptr
;
1116 data_reg2
= *args
++;
1118 data_reg2
= 0; /* suppress warning */
1120 #ifdef CONFIG_SOFTMMU
1121 # if TARGET_LONG_BITS == 64
1122 addr_reg2
= *args
++;
1127 /* Should generate something like the following:
1128 * shr r8, addr_reg, #TARGET_PAGE_BITS
1129 * and r0, r8, #(CPU_TLB_SIZE - 1) @ Assumption: CPU_TLB_BITS <= 8
1130 * add r0, env, r0 lsl #CPU_TLB_ENTRY_BITS
1132 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
1133 TCG_REG_R8
, 0, addr_reg
, SHIFT_IMM_LSR(TARGET_PAGE_BITS
));
1134 tcg_out_dat_imm(s
, COND_AL
, ARITH_AND
,
1135 TCG_REG_R0
, TCG_REG_R8
, CPU_TLB_SIZE
- 1);
1136 tcg_out_dat_reg(s
, COND_AL
, ARITH_ADD
, TCG_REG_R0
,
1137 TCG_AREG0
, TCG_REG_R0
, SHIFT_IMM_LSL(CPU_TLB_ENTRY_BITS
));
1139 * ldr r1 [r0, #(offsetof(CPUState, tlb_table[mem_index][0].addr_write))]
1140 * below, the offset is likely to exceed 12 bits if mem_index != 0 and
1141 * not exceed otherwise, so use an
1142 * add r0, r0, #(mem_index * sizeof *CPUState.tlb_table)
1146 tcg_out_dat_imm(s
, COND_AL
, ARITH_ADD
, TCG_REG_R0
, TCG_REG_R0
,
1147 (mem_index
<< (TLB_SHIFT
& 1)) |
1148 ((16 - (TLB_SHIFT
>> 1)) << 8));
1149 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_R1
, TCG_REG_R0
,
1150 offsetof(CPUState
, tlb_table
[0][0].addr_write
));
1151 tcg_out_dat_reg(s
, COND_AL
, ARITH_CMP
, 0, TCG_REG_R1
,
1152 TCG_REG_R8
, SHIFT_IMM_LSL(TARGET_PAGE_BITS
));
1153 /* Check alignment. */
1155 tcg_out_dat_imm(s
, COND_EQ
, ARITH_TST
,
1156 0, addr_reg
, (1 << s_bits
) - 1);
1157 # if TARGET_LONG_BITS == 64
1158 /* XXX: possibly we could use a block data load or writeback in
1159 * the first access. */
1160 tcg_out_ld32_12(s
, COND_EQ
, TCG_REG_R1
, TCG_REG_R0
,
1161 offsetof(CPUState
, tlb_table
[0][0].addr_write
) + 4);
1162 tcg_out_dat_reg(s
, COND_EQ
, ARITH_CMP
, 0,
1163 TCG_REG_R1
, addr_reg2
, SHIFT_IMM_LSL(0));
1165 tcg_out_ld32_12(s
, COND_EQ
, TCG_REG_R1
, TCG_REG_R0
,
1166 offsetof(CPUState
, tlb_table
[0][0].addend
));
1170 tcg_out_st8_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
1173 tcg_out_st16_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
1177 tcg_out_st32_r(s
, COND_EQ
, data_reg
, addr_reg
, TCG_REG_R1
);
1180 tcg_out_st32_rwb(s
, COND_EQ
, data_reg
, TCG_REG_R1
, addr_reg
);
1181 tcg_out_st32_12(s
, COND_EQ
, data_reg2
, TCG_REG_R1
, 4);
1185 label_ptr
= (void *) s
->code_ptr
;
1186 tcg_out_b(s
, COND_EQ
, 8);
1188 /* TODO: move this code to where the constants pool will be */
1189 if (addr_reg
!= TCG_REG_R0
) {
1190 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1191 TCG_REG_R0
, 0, addr_reg
, SHIFT_IMM_LSL(0));
1193 # if TARGET_LONG_BITS == 32
1196 tcg_out_dat_imm(s
, cond
, ARITH_AND
, TCG_REG_R1
, data_reg
, 0xff);
1197 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R2
, 0, mem_index
);
1200 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1201 TCG_REG_R1
, 0, data_reg
, SHIFT_IMM_LSL(16));
1202 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1203 TCG_REG_R1
, 0, TCG_REG_R1
, SHIFT_IMM_LSR(16));
1204 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R2
, 0, mem_index
);
1207 if (data_reg
!= TCG_REG_R1
) {
1208 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1209 TCG_REG_R1
, 0, data_reg
, SHIFT_IMM_LSL(0));
1211 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R2
, 0, mem_index
);
1214 if (data_reg
!= TCG_REG_R1
) {
1215 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1216 TCG_REG_R1
, 0, data_reg
, SHIFT_IMM_LSL(0));
1218 if (data_reg2
!= TCG_REG_R2
) {
1219 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1220 TCG_REG_R2
, 0, data_reg2
, SHIFT_IMM_LSL(0));
1222 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R3
, 0, mem_index
);
1226 if (addr_reg2
!= TCG_REG_R1
) {
1227 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1228 TCG_REG_R1
, 0, addr_reg2
, SHIFT_IMM_LSL(0));
1232 tcg_out_dat_imm(s
, cond
, ARITH_AND
, TCG_REG_R2
, data_reg
, 0xff);
1233 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R3
, 0, mem_index
);
1236 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1237 TCG_REG_R2
, 0, data_reg
, SHIFT_IMM_LSL(16));
1238 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1239 TCG_REG_R2
, 0, TCG_REG_R2
, SHIFT_IMM_LSR(16));
1240 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R3
, 0, mem_index
);
1243 if (data_reg
!= TCG_REG_R2
) {
1244 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1245 TCG_REG_R2
, 0, data_reg
, SHIFT_IMM_LSL(0));
1247 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R3
, 0, mem_index
);
1250 tcg_out_dat_imm(s
, cond
, ARITH_MOV
, TCG_REG_R8
, 0, mem_index
);
1251 tcg_out32(s
, (cond
<< 28) | 0x052d8010); /* str r8, [sp, #-0x10]! */
1252 if (data_reg
!= TCG_REG_R2
) {
1253 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1254 TCG_REG_R2
, 0, data_reg
, SHIFT_IMM_LSL(0));
1256 if (data_reg2
!= TCG_REG_R3
) {
1257 tcg_out_dat_reg(s
, cond
, ARITH_MOV
,
1258 TCG_REG_R3
, 0, data_reg2
, SHIFT_IMM_LSL(0));
1264 tcg_out_bl(s
, cond
, (tcg_target_long
) qemu_st_helpers
[s_bits
] -
1265 (tcg_target_long
) s
->code_ptr
);
1266 # if TARGET_LONG_BITS == 64
1268 tcg_out_dat_imm(s
, cond
, ARITH_ADD
, TCG_REG_R13
, TCG_REG_R13
, 0x10);
1271 *label_ptr
+= ((void *) s
->code_ptr
- (void *) label_ptr
- 8) >> 2;
1272 #else /* !CONFIG_SOFTMMU */
1274 uint32_t offset
= GUEST_BASE
;
1279 i
= ctz32(offset
) & ~1;
1280 rot
= ((32 - i
) << 7) & 0xf00;
1282 tcg_out_dat_imm(s
, COND_AL
, ARITH_ADD
, TCG_REG_R8
, addr_reg
,
1283 ((offset
>> i
) & 0xff) | rot
);
1284 addr_reg
= TCG_REG_R8
;
1285 offset
&= ~(0xff << i
);
1290 tcg_out_st8_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1293 tcg_out_st16_8(s
, COND_AL
, data_reg
, addr_reg
, 0);
1297 tcg_out_st32_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1300 /* TODO: use block store -
1301 * check that data_reg2 > data_reg or the other way */
1302 tcg_out_st32_12(s
, COND_AL
, data_reg
, addr_reg
, 0);
1303 tcg_out_st32_12(s
, COND_AL
, data_reg2
, addr_reg
, 4);
1309 static uint8_t *tb_ret_addr
;
1311 static inline void tcg_out_op(TCGContext
*s
, TCGOpcode opc
,
1312 const TCGArg
*args
, const int *const_args
)
1317 case INDEX_op_exit_tb
:
1319 uint8_t *ld_ptr
= s
->code_ptr
;
1321 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_R0
, TCG_REG_PC
, 0);
1323 tcg_out_dat_imm(s
, COND_AL
, ARITH_MOV
, TCG_REG_R0
, 0, args
[0]);
1324 tcg_out_goto(s
, COND_AL
, (tcg_target_ulong
) tb_ret_addr
);
1326 *ld_ptr
= (uint8_t) (s
->code_ptr
- ld_ptr
) - 8;
1327 tcg_out32(s
, args
[0]);
1331 case INDEX_op_goto_tb
:
1332 if (s
->tb_jmp_offset
) {
1333 /* Direct jump method */
1334 #if defined(USE_DIRECT_JUMP)
1335 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1336 tcg_out_b(s
, COND_AL
, 8);
1338 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_PC
, -4);
1339 s
->tb_jmp_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1343 /* Indirect jump method */
1345 c
= (int) (s
->tb_next
+ args
[0]) - ((int) s
->code_ptr
+ 8);
1346 if (c
> 0xfff || c
< -0xfff) {
1347 tcg_out_movi32(s
, COND_AL
, TCG_REG_R0
,
1348 (tcg_target_long
) (s
->tb_next
+ args
[0]));
1349 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_R0
, 0);
1351 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_PC
, c
);
1353 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_R0
, TCG_REG_PC
, 0);
1354 tcg_out_ld32_12(s
, COND_AL
, TCG_REG_PC
, TCG_REG_R0
, 0);
1355 tcg_out32(s
, (tcg_target_long
) (s
->tb_next
+ args
[0]));
1358 s
->tb_next_offset
[args
[0]] = s
->code_ptr
- s
->code_buf
;
1362 tcg_out_call(s
, COND_AL
, args
[0]);
1364 tcg_out_callr(s
, COND_AL
, args
[0]);
1368 tcg_out_goto(s
, COND_AL
, args
[0]);
1370 tcg_out_bx(s
, COND_AL
, args
[0]);
1373 tcg_out_goto_label(s
, COND_AL
, args
[0]);
1376 case INDEX_op_ld8u_i32
:
1377 tcg_out_ld8u(s
, COND_AL
, args
[0], args
[1], args
[2]);
1379 case INDEX_op_ld8s_i32
:
1380 tcg_out_ld8s(s
, COND_AL
, args
[0], args
[1], args
[2]);
1382 case INDEX_op_ld16u_i32
:
1383 tcg_out_ld16u(s
, COND_AL
, args
[0], args
[1], args
[2]);
1385 case INDEX_op_ld16s_i32
:
1386 tcg_out_ld16s(s
, COND_AL
, args
[0], args
[1], args
[2]);
1388 case INDEX_op_ld_i32
:
1389 tcg_out_ld32u(s
, COND_AL
, args
[0], args
[1], args
[2]);
1391 case INDEX_op_st8_i32
:
1392 tcg_out_st8(s
, COND_AL
, args
[0], args
[1], args
[2]);
1394 case INDEX_op_st16_i32
:
1395 tcg_out_st16(s
, COND_AL
, args
[0], args
[1], args
[2]);
1397 case INDEX_op_st_i32
:
1398 tcg_out_st32(s
, COND_AL
, args
[0], args
[1], args
[2]);
1401 case INDEX_op_mov_i32
:
1402 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
,
1403 args
[0], 0, args
[1], SHIFT_IMM_LSL(0));
1405 case INDEX_op_movi_i32
:
1406 tcg_out_movi32(s
, COND_AL
, args
[0], args
[1]);
1408 case INDEX_op_add_i32
:
1411 case INDEX_op_sub_i32
:
1414 case INDEX_op_and_i32
:
1417 case INDEX_op_andc_i32
:
1420 case INDEX_op_or_i32
:
1423 case INDEX_op_xor_i32
:
1427 if (const_args
[2]) {
1429 rot
= encode_imm(args
[2]);
1430 tcg_out_dat_imm(s
, COND_AL
, c
,
1431 args
[0], args
[1], rotl(args
[2], rot
) | (rot
<< 7));
1433 tcg_out_dat_reg(s
, COND_AL
, c
,
1434 args
[0], args
[1], args
[2], SHIFT_IMM_LSL(0));
1436 case INDEX_op_add2_i32
:
1437 tcg_out_dat_reg2(s
, COND_AL
, ARITH_ADD
, ARITH_ADC
,
1438 args
[0], args
[1], args
[2], args
[3],
1439 args
[4], args
[5], SHIFT_IMM_LSL(0));
1441 case INDEX_op_sub2_i32
:
1442 tcg_out_dat_reg2(s
, COND_AL
, ARITH_SUB
, ARITH_SBC
,
1443 args
[0], args
[1], args
[2], args
[3],
1444 args
[4], args
[5], SHIFT_IMM_LSL(0));
1446 case INDEX_op_neg_i32
:
1447 tcg_out_dat_imm(s
, COND_AL
, ARITH_RSB
, args
[0], args
[1], 0);
1449 case INDEX_op_not_i32
:
1450 tcg_out_dat_reg(s
, COND_AL
,
1451 ARITH_MVN
, args
[0], 0, args
[1], SHIFT_IMM_LSL(0));
1453 case INDEX_op_mul_i32
:
1454 tcg_out_mul32(s
, COND_AL
, args
[0], args
[1], args
[2]);
1456 case INDEX_op_mulu2_i32
:
1457 tcg_out_umull32(s
, COND_AL
, args
[0], args
[1], args
[2], args
[3]);
1459 /* XXX: Perhaps args[2] & 0x1f is wrong */
1460 case INDEX_op_shl_i32
:
1462 SHIFT_IMM_LSL(args
[2] & 0x1f) : SHIFT_REG_LSL(args
[2]);
1464 case INDEX_op_shr_i32
:
1465 c
= const_args
[2] ? (args
[2] & 0x1f) ? SHIFT_IMM_LSR(args
[2] & 0x1f) :
1466 SHIFT_IMM_LSL(0) : SHIFT_REG_LSR(args
[2]);
1468 case INDEX_op_sar_i32
:
1469 c
= const_args
[2] ? (args
[2] & 0x1f) ? SHIFT_IMM_ASR(args
[2] & 0x1f) :
1470 SHIFT_IMM_LSL(0) : SHIFT_REG_ASR(args
[2]);
1472 case INDEX_op_rotr_i32
:
1473 c
= const_args
[2] ? (args
[2] & 0x1f) ? SHIFT_IMM_ROR(args
[2] & 0x1f) :
1474 SHIFT_IMM_LSL(0) : SHIFT_REG_ROR(args
[2]);
1477 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
, args
[0], 0, args
[1], c
);
1480 case INDEX_op_rotl_i32
:
1481 if (const_args
[2]) {
1482 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
, args
[0], 0, args
[1],
1483 ((0x20 - args
[2]) & 0x1f) ?
1484 SHIFT_IMM_ROR((0x20 - args
[2]) & 0x1f) :
1487 tcg_out_dat_imm(s
, COND_AL
, ARITH_RSB
, TCG_REG_R8
, args
[1], 0x20);
1488 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
, args
[0], 0, args
[1],
1489 SHIFT_REG_ROR(TCG_REG_R8
));
1493 case INDEX_op_brcond_i32
:
1494 if (const_args
[1]) {
1496 rot
= encode_imm(args
[1]);
1497 tcg_out_dat_imm(s
, COND_AL
, ARITH_CMP
, 0,
1498 args
[0], rotl(args
[1], rot
) | (rot
<< 7));
1500 tcg_out_dat_reg(s
, COND_AL
, ARITH_CMP
, 0,
1501 args
[0], args
[1], SHIFT_IMM_LSL(0));
1503 tcg_out_goto_label(s
, tcg_cond_to_arm_cond
[args
[2]], args
[3]);
1505 case INDEX_op_brcond2_i32
:
1506 /* The resulting conditions are:
1507 * TCG_COND_EQ --> a0 == a2 && a1 == a3,
1508 * TCG_COND_NE --> (a0 != a2 && a1 == a3) || a1 != a3,
1509 * TCG_COND_LT(U) --> (a0 < a2 && a1 == a3) || a1 < a3,
1510 * TCG_COND_GE(U) --> (a0 >= a2 && a1 == a3) || (a1 >= a3 && a1 != a3),
1511 * TCG_COND_LE(U) --> (a0 <= a2 && a1 == a3) || (a1 <= a3 && a1 != a3),
1512 * TCG_COND_GT(U) --> (a0 > a2 && a1 == a3) || a1 > a3,
1514 tcg_out_dat_reg(s
, COND_AL
, ARITH_CMP
, 0,
1515 args
[1], args
[3], SHIFT_IMM_LSL(0));
1516 tcg_out_dat_reg(s
, COND_EQ
, ARITH_CMP
, 0,
1517 args
[0], args
[2], SHIFT_IMM_LSL(0));
1518 tcg_out_goto_label(s
, tcg_cond_to_arm_cond
[args
[4]], args
[5]);
1520 case INDEX_op_setcond_i32
:
1521 if (const_args
[2]) {
1523 rot
= encode_imm(args
[2]);
1524 tcg_out_dat_imm(s
, COND_AL
, ARITH_CMP
, 0,
1525 args
[1], rotl(args
[2], rot
) | (rot
<< 7));
1527 tcg_out_dat_reg(s
, COND_AL
, ARITH_CMP
, 0,
1528 args
[1], args
[2], SHIFT_IMM_LSL(0));
1530 tcg_out_dat_imm(s
, tcg_cond_to_arm_cond
[args
[3]],
1531 ARITH_MOV
, args
[0], 0, 1);
1532 tcg_out_dat_imm(s
, tcg_cond_to_arm_cond
[tcg_invert_cond(args
[3])],
1533 ARITH_MOV
, args
[0], 0, 0);
1535 case INDEX_op_setcond2_i32
:
1536 /* See brcond2_i32 comment */
1537 tcg_out_dat_reg(s
, COND_AL
, ARITH_CMP
, 0,
1538 args
[2], args
[4], SHIFT_IMM_LSL(0));
1539 tcg_out_dat_reg(s
, COND_EQ
, ARITH_CMP
, 0,
1540 args
[1], args
[3], SHIFT_IMM_LSL(0));
1541 tcg_out_dat_imm(s
, tcg_cond_to_arm_cond
[args
[5]],
1542 ARITH_MOV
, args
[0], 0, 1);
1543 tcg_out_dat_imm(s
, tcg_cond_to_arm_cond
[tcg_invert_cond(args
[5])],
1544 ARITH_MOV
, args
[0], 0, 0);
1547 case INDEX_op_qemu_ld8u
:
1548 tcg_out_qemu_ld(s
, COND_AL
, args
, 0);
1550 case INDEX_op_qemu_ld8s
:
1551 tcg_out_qemu_ld(s
, COND_AL
, args
, 0 | 4);
1553 case INDEX_op_qemu_ld16u
:
1554 tcg_out_qemu_ld(s
, COND_AL
, args
, 1);
1556 case INDEX_op_qemu_ld16s
:
1557 tcg_out_qemu_ld(s
, COND_AL
, args
, 1 | 4);
1559 case INDEX_op_qemu_ld32
:
1560 tcg_out_qemu_ld(s
, COND_AL
, args
, 2);
1562 case INDEX_op_qemu_ld64
:
1563 tcg_out_qemu_ld(s
, COND_AL
, args
, 3);
1566 case INDEX_op_qemu_st8
:
1567 tcg_out_qemu_st(s
, COND_AL
, args
, 0);
1569 case INDEX_op_qemu_st16
:
1570 tcg_out_qemu_st(s
, COND_AL
, args
, 1);
1572 case INDEX_op_qemu_st32
:
1573 tcg_out_qemu_st(s
, COND_AL
, args
, 2);
1575 case INDEX_op_qemu_st64
:
1576 tcg_out_qemu_st(s
, COND_AL
, args
, 3);
1579 case INDEX_op_bswap16_i32
:
1580 tcg_out_bswap16(s
, COND_AL
, args
[0], args
[1]);
1582 case INDEX_op_bswap32_i32
:
1583 tcg_out_bswap32(s
, COND_AL
, args
[0], args
[1]);
1586 case INDEX_op_ext8s_i32
:
1587 tcg_out_ext8s(s
, COND_AL
, args
[0], args
[1]);
1589 case INDEX_op_ext16s_i32
:
1590 tcg_out_ext16s(s
, COND_AL
, args
[0], args
[1]);
1592 case INDEX_op_ext16u_i32
:
1593 tcg_out_ext16u(s
, COND_AL
, args
[0], args
[1]);
1601 static const TCGTargetOpDef arm_op_defs
[] = {
1602 { INDEX_op_exit_tb
, { } },
1603 { INDEX_op_goto_tb
, { } },
1604 { INDEX_op_call
, { "ri" } },
1605 { INDEX_op_jmp
, { "ri" } },
1606 { INDEX_op_br
, { } },
1608 { INDEX_op_mov_i32
, { "r", "r" } },
1609 { INDEX_op_movi_i32
, { "r" } },
1611 { INDEX_op_ld8u_i32
, { "r", "r" } },
1612 { INDEX_op_ld8s_i32
, { "r", "r" } },
1613 { INDEX_op_ld16u_i32
, { "r", "r" } },
1614 { INDEX_op_ld16s_i32
, { "r", "r" } },
1615 { INDEX_op_ld_i32
, { "r", "r" } },
1616 { INDEX_op_st8_i32
, { "r", "r" } },
1617 { INDEX_op_st16_i32
, { "r", "r" } },
1618 { INDEX_op_st_i32
, { "r", "r" } },
1620 /* TODO: "r", "r", "ri" */
1621 { INDEX_op_add_i32
, { "r", "r", "rI" } },
1622 { INDEX_op_sub_i32
, { "r", "r", "rI" } },
1623 { INDEX_op_mul_i32
, { "r", "r", "r" } },
1624 { INDEX_op_mulu2_i32
, { "r", "r", "r", "r" } },
1625 { INDEX_op_and_i32
, { "r", "r", "rI" } },
1626 { INDEX_op_andc_i32
, { "r", "r", "rI" } },
1627 { INDEX_op_or_i32
, { "r", "r", "rI" } },
1628 { INDEX_op_xor_i32
, { "r", "r", "rI" } },
1629 { INDEX_op_neg_i32
, { "r", "r" } },
1630 { INDEX_op_not_i32
, { "r", "r" } },
1632 { INDEX_op_shl_i32
, { "r", "r", "ri" } },
1633 { INDEX_op_shr_i32
, { "r", "r", "ri" } },
1634 { INDEX_op_sar_i32
, { "r", "r", "ri" } },
1635 { INDEX_op_rotl_i32
, { "r", "r", "ri" } },
1636 { INDEX_op_rotr_i32
, { "r", "r", "ri" } },
1638 { INDEX_op_brcond_i32
, { "r", "rI" } },
1639 { INDEX_op_setcond_i32
, { "r", "r", "rI" } },
1641 /* TODO: "r", "r", "r", "r", "ri", "ri" */
1642 { INDEX_op_add2_i32
, { "r", "r", "r", "r", "r", "r" } },
1643 { INDEX_op_sub2_i32
, { "r", "r", "r", "r", "r", "r" } },
1644 { INDEX_op_brcond2_i32
, { "r", "r", "r", "r" } },
1645 { INDEX_op_setcond2_i32
, { "r", "r", "r", "r", "r" } },
1647 #if TARGET_LONG_BITS == 32
1648 { INDEX_op_qemu_ld8u
, { "r", "x" } },
1649 { INDEX_op_qemu_ld8s
, { "r", "x" } },
1650 { INDEX_op_qemu_ld16u
, { "r", "x" } },
1651 { INDEX_op_qemu_ld16s
, { "r", "x" } },
1652 { INDEX_op_qemu_ld32
, { "r", "x" } },
1653 { INDEX_op_qemu_ld64
, { "d", "r", "x" } },
1655 { INDEX_op_qemu_st8
, { "x", "x" } },
1656 { INDEX_op_qemu_st16
, { "x", "x" } },
1657 { INDEX_op_qemu_st32
, { "x", "x" } },
1658 { INDEX_op_qemu_st64
, { "x", "D", "x" } },
1660 { INDEX_op_qemu_ld8u
, { "r", "x", "X" } },
1661 { INDEX_op_qemu_ld8s
, { "r", "x", "X" } },
1662 { INDEX_op_qemu_ld16u
, { "r", "x", "X" } },
1663 { INDEX_op_qemu_ld16s
, { "r", "x", "X" } },
1664 { INDEX_op_qemu_ld32
, { "r", "x", "X" } },
1665 { INDEX_op_qemu_ld64
, { "d", "r", "x", "X" } },
1667 { INDEX_op_qemu_st8
, { "x", "x", "X" } },
1668 { INDEX_op_qemu_st16
, { "x", "x", "X" } },
1669 { INDEX_op_qemu_st32
, { "x", "x", "X" } },
1670 { INDEX_op_qemu_st64
, { "x", "D", "x", "X" } },
1673 { INDEX_op_bswap16_i32
, { "r", "r" } },
1674 { INDEX_op_bswap32_i32
, { "r", "r" } },
1676 { INDEX_op_ext8s_i32
, { "r", "r" } },
1677 { INDEX_op_ext16s_i32
, { "r", "r" } },
1678 { INDEX_op_ext16u_i32
, { "r", "r" } },
1683 void tcg_target_init(TCGContext
*s
)
1685 #if !defined(CONFIG_USER_ONLY)
1687 if ((1 << CPU_TLB_ENTRY_BITS
) != sizeof(CPUTLBEntry
))
1691 tcg_regset_set32(tcg_target_available_regs
[TCG_TYPE_I32
], 0, 0xffff);
1692 tcg_regset_set32(tcg_target_call_clobber_regs
, 0,
1697 (1 << TCG_REG_R12
) |
1698 (1 << TCG_REG_R14
));
1700 tcg_regset_clear(s
->reserved_regs
);
1701 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_CALL_STACK
);
1702 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_R8
);
1703 tcg_regset_set_reg(s
->reserved_regs
, TCG_REG_PC
);
1705 tcg_add_target_add_op_defs(arm_op_defs
);
1708 static inline void tcg_out_ld(TCGContext
*s
, TCGType type
, int arg
,
1709 int arg1
, tcg_target_long arg2
)
1711 tcg_out_ld32u(s
, COND_AL
, arg
, arg1
, arg2
);
1714 static inline void tcg_out_st(TCGContext
*s
, TCGType type
, int arg
,
1715 int arg1
, tcg_target_long arg2
)
1717 tcg_out_st32(s
, COND_AL
, arg
, arg1
, arg2
);
1720 static void tcg_out_addi(TCGContext
*s
, int reg
, tcg_target_long val
)
1724 tcg_out_dat_imm(s
, COND_AL
, ARITH_ADD
, reg
, reg
, val
);
1729 tcg_out_dat_imm(s
, COND_AL
, ARITH_SUB
, reg
, reg
, -val
);
1735 static inline void tcg_out_mov(TCGContext
*s
, int ret
, int arg
)
1737 tcg_out_dat_reg(s
, COND_AL
, ARITH_MOV
, ret
, 0, arg
, SHIFT_IMM_LSL(0));
1740 static inline void tcg_out_movi(TCGContext
*s
, TCGType type
,
1741 int ret
, tcg_target_long arg
)
1743 tcg_out_movi32(s
, COND_AL
, ret
, arg
);
1746 void tcg_target_qemu_prologue(TCGContext
*s
)
1748 /* There is no need to save r7, it is used to store the address
1749 of the env structure and is not modified by GCC. */
1751 /* stmdb sp!, { r4 - r6, r8 - r11, lr } */
1752 tcg_out32(s
, (COND_AL
<< 28) | 0x092d4f70);
1754 tcg_out_bx(s
, COND_AL
, TCG_REG_R0
);
1755 tb_ret_addr
= s
->code_ptr
;
1757 /* ldmia sp!, { r4 - r6, r8 - r11, pc } */
1758 tcg_out32(s
, (COND_AL
<< 28) | 0x08bd8f70);