2 * Tiny Code Interpreter for QEMU
4 * Copyright (c) 2009, 2011, 2016 Stefan Weil
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 /* Enable TCI assertions only when debugging TCG (and without NDEBUG defined).
23 * Without assertions, the interpreter runs much faster. */
24 #if defined(CONFIG_DEBUG_TCG)
25 # define tci_assert(cond) assert(cond)
27 # define tci_assert(cond) ((void)0)
30 #include "qemu-common.h"
31 #include "tcg/tcg.h" /* MAX_OPC_PARAM_IARGS */
32 #include "exec/cpu_ldst.h"
33 #include "tcg/tcg-op.h"
34 #include "qemu/compiler.h"
36 /* Marker for missing code. */
39 fprintf(stderr, "TODO %s:%u: %s()\n", \
40 __FILE__, __LINE__, __func__); \
44 #if MAX_OPC_PARAM_IARGS != 6
45 # error Fix needed, number of supported input arguments changed!
47 #if TCG_TARGET_REG_BITS == 32
48 typedef uint64_t (*helper_function
)(tcg_target_ulong
, tcg_target_ulong
,
49 tcg_target_ulong
, tcg_target_ulong
,
50 tcg_target_ulong
, tcg_target_ulong
,
51 tcg_target_ulong
, tcg_target_ulong
,
52 tcg_target_ulong
, tcg_target_ulong
,
53 tcg_target_ulong
, tcg_target_ulong
);
55 typedef uint64_t (*helper_function
)(tcg_target_ulong
, tcg_target_ulong
,
56 tcg_target_ulong
, tcg_target_ulong
,
57 tcg_target_ulong
, tcg_target_ulong
);
60 static tcg_target_ulong
tci_read_reg(const tcg_target_ulong
*regs
, TCGReg index
)
62 tci_assert(index
< TCG_TARGET_NB_REGS
);
66 #if TCG_TARGET_HAS_ext8s_i32 || TCG_TARGET_HAS_ext8s_i64
67 static int8_t tci_read_reg8s(const tcg_target_ulong
*regs
, TCGReg index
)
69 return (int8_t)tci_read_reg(regs
, index
);
73 #if TCG_TARGET_HAS_ext16s_i32 || TCG_TARGET_HAS_ext16s_i64
74 static int16_t tci_read_reg16s(const tcg_target_ulong
*regs
, TCGReg index
)
76 return (int16_t)tci_read_reg(regs
, index
);
80 #if TCG_TARGET_REG_BITS == 64
81 static int32_t tci_read_reg32s(const tcg_target_ulong
*regs
, TCGReg index
)
83 return (int32_t)tci_read_reg(regs
, index
);
87 static uint8_t tci_read_reg8(const tcg_target_ulong
*regs
, TCGReg index
)
89 return (uint8_t)tci_read_reg(regs
, index
);
92 static uint16_t tci_read_reg16(const tcg_target_ulong
*regs
, TCGReg index
)
94 return (uint16_t)tci_read_reg(regs
, index
);
97 static uint32_t tci_read_reg32(const tcg_target_ulong
*regs
, TCGReg index
)
99 return (uint32_t)tci_read_reg(regs
, index
);
102 #if TCG_TARGET_REG_BITS == 64
103 static uint64_t tci_read_reg64(const tcg_target_ulong
*regs
, TCGReg index
)
105 return tci_read_reg(regs
, index
);
110 tci_write_reg(tcg_target_ulong
*regs
, TCGReg index
, tcg_target_ulong value
)
112 tci_assert(index
< TCG_TARGET_NB_REGS
);
113 tci_assert(index
!= TCG_AREG0
);
114 tci_assert(index
!= TCG_REG_CALL_STACK
);
118 #if TCG_TARGET_REG_BITS == 64
120 tci_write_reg32s(tcg_target_ulong
*regs
, TCGReg index
, int32_t value
)
122 tci_write_reg(regs
, index
, value
);
126 static void tci_write_reg8(tcg_target_ulong
*regs
, TCGReg index
, uint8_t value
)
128 tci_write_reg(regs
, index
, value
);
132 tci_write_reg16(tcg_target_ulong
*regs
, TCGReg index
, uint16_t value
)
134 tci_write_reg(regs
, index
, value
);
138 tci_write_reg32(tcg_target_ulong
*regs
, TCGReg index
, uint32_t value
)
140 tci_write_reg(regs
, index
, value
);
143 #if TCG_TARGET_REG_BITS == 32
144 static void tci_write_reg64(tcg_target_ulong
*regs
, uint32_t high_index
,
145 uint32_t low_index
, uint64_t value
)
147 tci_write_reg(regs
, low_index
, value
);
148 tci_write_reg(regs
, high_index
, value
>> 32);
150 #elif TCG_TARGET_REG_BITS == 64
152 tci_write_reg64(tcg_target_ulong
*regs
, TCGReg index
, uint64_t value
)
154 tci_write_reg(regs
, index
, value
);
158 #if TCG_TARGET_REG_BITS == 32
159 /* Create a 64 bit value from two 32 bit values. */
160 static uint64_t tci_uint64(uint32_t high
, uint32_t low
)
162 return ((uint64_t)high
<< 32) + low
;
166 /* Read constant (native size) from bytecode. */
167 static tcg_target_ulong
tci_read_i(const uint8_t **tb_ptr
)
169 tcg_target_ulong value
= *(const tcg_target_ulong
*)(*tb_ptr
);
170 *tb_ptr
+= sizeof(value
);
174 /* Read unsigned constant (32 bit) from bytecode. */
175 static uint32_t tci_read_i32(const uint8_t **tb_ptr
)
177 uint32_t value
= *(const uint32_t *)(*tb_ptr
);
178 *tb_ptr
+= sizeof(value
);
182 /* Read signed constant (32 bit) from bytecode. */
183 static int32_t tci_read_s32(const uint8_t **tb_ptr
)
185 int32_t value
= *(const int32_t *)(*tb_ptr
);
186 *tb_ptr
+= sizeof(value
);
190 #if TCG_TARGET_REG_BITS == 64
191 /* Read constant (64 bit) from bytecode. */
192 static uint64_t tci_read_i64(const uint8_t **tb_ptr
)
194 uint64_t value
= *(const uint64_t *)(*tb_ptr
);
195 *tb_ptr
+= sizeof(value
);
200 /* Read indexed register (native size) from bytecode. */
201 static tcg_target_ulong
202 tci_read_r(const tcg_target_ulong
*regs
, const uint8_t **tb_ptr
)
204 tcg_target_ulong value
= tci_read_reg(regs
, **tb_ptr
);
209 /* Read indexed register (8 bit) from bytecode. */
210 static uint8_t tci_read_r8(const tcg_target_ulong
*regs
, const uint8_t **tb_ptr
)
212 uint8_t value
= tci_read_reg8(regs
, **tb_ptr
);
217 #if TCG_TARGET_HAS_ext8s_i32 || TCG_TARGET_HAS_ext8s_i64
218 /* Read indexed register (8 bit signed) from bytecode. */
219 static int8_t tci_read_r8s(const tcg_target_ulong
*regs
, const uint8_t **tb_ptr
)
221 int8_t value
= tci_read_reg8s(regs
, **tb_ptr
);
227 /* Read indexed register (16 bit) from bytecode. */
228 static uint16_t tci_read_r16(const tcg_target_ulong
*regs
,
229 const uint8_t **tb_ptr
)
231 uint16_t value
= tci_read_reg16(regs
, **tb_ptr
);
236 #if TCG_TARGET_HAS_ext16s_i32 || TCG_TARGET_HAS_ext16s_i64
237 /* Read indexed register (16 bit signed) from bytecode. */
238 static int16_t tci_read_r16s(const tcg_target_ulong
*regs
,
239 const uint8_t **tb_ptr
)
241 int16_t value
= tci_read_reg16s(regs
, **tb_ptr
);
247 /* Read indexed register (32 bit) from bytecode. */
248 static uint32_t tci_read_r32(const tcg_target_ulong
*regs
,
249 const uint8_t **tb_ptr
)
251 uint32_t value
= tci_read_reg32(regs
, **tb_ptr
);
256 #if TCG_TARGET_REG_BITS == 32
257 /* Read two indexed registers (2 * 32 bit) from bytecode. */
258 static uint64_t tci_read_r64(const tcg_target_ulong
*regs
,
259 const uint8_t **tb_ptr
)
261 uint32_t low
= tci_read_r32(regs
, tb_ptr
);
262 return tci_uint64(tci_read_r32(regs
, tb_ptr
), low
);
264 #elif TCG_TARGET_REG_BITS == 64
265 /* Read indexed register (32 bit signed) from bytecode. */
266 static int32_t tci_read_r32s(const tcg_target_ulong
*regs
,
267 const uint8_t **tb_ptr
)
269 int32_t value
= tci_read_reg32s(regs
, **tb_ptr
);
274 /* Read indexed register (64 bit) from bytecode. */
275 static uint64_t tci_read_r64(const tcg_target_ulong
*regs
,
276 const uint8_t **tb_ptr
)
278 uint64_t value
= tci_read_reg64(regs
, **tb_ptr
);
284 /* Read indexed register(s) with target address from bytecode. */
286 tci_read_ulong(const tcg_target_ulong
*regs
, const uint8_t **tb_ptr
)
288 target_ulong taddr
= tci_read_r(regs
, tb_ptr
);
289 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
290 taddr
+= (uint64_t)tci_read_r(regs
, tb_ptr
) << 32;
295 /* Read indexed register or constant (native size) from bytecode. */
296 static tcg_target_ulong
297 tci_read_ri(const tcg_target_ulong
*regs
, const uint8_t **tb_ptr
)
299 tcg_target_ulong value
;
302 if (r
== TCG_CONST
) {
303 value
= tci_read_i(tb_ptr
);
305 value
= tci_read_reg(regs
, r
);
310 /* Read indexed register or constant (32 bit) from bytecode. */
311 static uint32_t tci_read_ri32(const tcg_target_ulong
*regs
,
312 const uint8_t **tb_ptr
)
317 if (r
== TCG_CONST
) {
318 value
= tci_read_i32(tb_ptr
);
320 value
= tci_read_reg32(regs
, r
);
325 #if TCG_TARGET_REG_BITS == 32
326 /* Read two indexed registers or constants (2 * 32 bit) from bytecode. */
327 static uint64_t tci_read_ri64(const tcg_target_ulong
*regs
,
328 const uint8_t **tb_ptr
)
330 uint32_t low
= tci_read_ri32(regs
, tb_ptr
);
331 return tci_uint64(tci_read_ri32(regs
, tb_ptr
), low
);
333 #elif TCG_TARGET_REG_BITS == 64
334 /* Read indexed register or constant (64 bit) from bytecode. */
335 static uint64_t tci_read_ri64(const tcg_target_ulong
*regs
,
336 const uint8_t **tb_ptr
)
341 if (r
== TCG_CONST
) {
342 value
= tci_read_i64(tb_ptr
);
344 value
= tci_read_reg64(regs
, r
);
350 static tcg_target_ulong
tci_read_label(const uint8_t **tb_ptr
)
352 tcg_target_ulong label
= tci_read_i(tb_ptr
);
353 tci_assert(label
!= 0);
357 static bool tci_compare32(uint32_t u0
, uint32_t u1
, TCGCond condition
)
399 static bool tci_compare64(uint64_t u0
, uint64_t u1
, TCGCond condition
)
441 #ifdef CONFIG_SOFTMMU
442 # define qemu_ld_ub \
443 helper_ret_ldub_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
444 # define qemu_ld_leuw \
445 helper_le_lduw_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
446 # define qemu_ld_leul \
447 helper_le_ldul_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
448 # define qemu_ld_leq \
449 helper_le_ldq_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
450 # define qemu_ld_beuw \
451 helper_be_lduw_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
452 # define qemu_ld_beul \
453 helper_be_ldul_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
454 # define qemu_ld_beq \
455 helper_be_ldq_mmu(env, taddr, oi, (uintptr_t)tb_ptr)
456 # define qemu_st_b(X) \
457 helper_ret_stb_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
458 # define qemu_st_lew(X) \
459 helper_le_stw_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
460 # define qemu_st_lel(X) \
461 helper_le_stl_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
462 # define qemu_st_leq(X) \
463 helper_le_stq_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
464 # define qemu_st_bew(X) \
465 helper_be_stw_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
466 # define qemu_st_bel(X) \
467 helper_be_stl_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
468 # define qemu_st_beq(X) \
469 helper_be_stq_mmu(env, taddr, X, oi, (uintptr_t)tb_ptr)
471 # define qemu_ld_ub ldub_p(g2h(taddr))
472 # define qemu_ld_leuw lduw_le_p(g2h(taddr))
473 # define qemu_ld_leul (uint32_t)ldl_le_p(g2h(taddr))
474 # define qemu_ld_leq ldq_le_p(g2h(taddr))
475 # define qemu_ld_beuw lduw_be_p(g2h(taddr))
476 # define qemu_ld_beul (uint32_t)ldl_be_p(g2h(taddr))
477 # define qemu_ld_beq ldq_be_p(g2h(taddr))
478 # define qemu_st_b(X) stb_p(g2h(taddr), X)
479 # define qemu_st_lew(X) stw_le_p(g2h(taddr), X)
480 # define qemu_st_lel(X) stl_le_p(g2h(taddr), X)
481 # define qemu_st_leq(X) stq_le_p(g2h(taddr), X)
482 # define qemu_st_bew(X) stw_be_p(g2h(taddr), X)
483 # define qemu_st_bel(X) stl_be_p(g2h(taddr), X)
484 # define qemu_st_beq(X) stq_be_p(g2h(taddr), X)
487 /* Interpret pseudo code in tb. */
489 * Disable CFI checks.
490 * One possible operation in the pseudo code is a call to binary code.
491 * Therefore, disable CFI checks in the interpreter function
493 uintptr_t QEMU_DISABLE_CFI
tcg_qemu_tb_exec(CPUArchState
*env
,
494 const void *v_tb_ptr
)
496 const uint8_t *tb_ptr
= v_tb_ptr
;
497 tcg_target_ulong regs
[TCG_TARGET_NB_REGS
];
498 long tcg_temps
[CPU_TEMP_BUF_NLONGS
];
499 uintptr_t sp_value
= (uintptr_t)(tcg_temps
+ CPU_TEMP_BUF_NLONGS
);
502 regs
[TCG_AREG0
] = (tcg_target_ulong
)env
;
503 regs
[TCG_REG_CALL_STACK
] = sp_value
;
507 TCGOpcode opc
= tb_ptr
[0];
508 #if defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG)
509 uint8_t op_size
= tb_ptr
[1];
510 const uint8_t *old_code_ptr
= tb_ptr
;
515 tcg_target_ulong label
;
522 #if TCG_TARGET_REG_BITS == 32
528 tci_tb_ptr
= (uintptr_t)tb_ptr
;
531 /* Skip opcode and size entry. */
536 t0
= tci_read_ri(regs
, &tb_ptr
);
537 #if TCG_TARGET_REG_BITS == 32
538 tmp64
= ((helper_function
)t0
)(tci_read_reg(regs
, TCG_REG_R0
),
539 tci_read_reg(regs
, TCG_REG_R1
),
540 tci_read_reg(regs
, TCG_REG_R2
),
541 tci_read_reg(regs
, TCG_REG_R3
),
542 tci_read_reg(regs
, TCG_REG_R5
),
543 tci_read_reg(regs
, TCG_REG_R6
),
544 tci_read_reg(regs
, TCG_REG_R7
),
545 tci_read_reg(regs
, TCG_REG_R8
),
546 tci_read_reg(regs
, TCG_REG_R9
),
547 tci_read_reg(regs
, TCG_REG_R10
),
548 tci_read_reg(regs
, TCG_REG_R11
),
549 tci_read_reg(regs
, TCG_REG_R12
));
550 tci_write_reg(regs
, TCG_REG_R0
, tmp64
);
551 tci_write_reg(regs
, TCG_REG_R1
, tmp64
>> 32);
553 tmp64
= ((helper_function
)t0
)(tci_read_reg(regs
, TCG_REG_R0
),
554 tci_read_reg(regs
, TCG_REG_R1
),
555 tci_read_reg(regs
, TCG_REG_R2
),
556 tci_read_reg(regs
, TCG_REG_R3
),
557 tci_read_reg(regs
, TCG_REG_R5
),
558 tci_read_reg(regs
, TCG_REG_R6
));
559 tci_write_reg(regs
, TCG_REG_R0
, tmp64
);
563 label
= tci_read_label(&tb_ptr
);
564 tci_assert(tb_ptr
== old_code_ptr
+ op_size
);
565 tb_ptr
= (uint8_t *)label
;
567 case INDEX_op_setcond_i32
:
569 t1
= tci_read_r32(regs
, &tb_ptr
);
570 t2
= tci_read_ri32(regs
, &tb_ptr
);
571 condition
= *tb_ptr
++;
572 tci_write_reg32(regs
, t0
, tci_compare32(t1
, t2
, condition
));
574 #if TCG_TARGET_REG_BITS == 32
575 case INDEX_op_setcond2_i32
:
577 tmp64
= tci_read_r64(regs
, &tb_ptr
);
578 v64
= tci_read_ri64(regs
, &tb_ptr
);
579 condition
= *tb_ptr
++;
580 tci_write_reg32(regs
, t0
, tci_compare64(tmp64
, v64
, condition
));
582 #elif TCG_TARGET_REG_BITS == 64
583 case INDEX_op_setcond_i64
:
585 t1
= tci_read_r64(regs
, &tb_ptr
);
586 t2
= tci_read_ri64(regs
, &tb_ptr
);
587 condition
= *tb_ptr
++;
588 tci_write_reg64(regs
, t0
, tci_compare64(t1
, t2
, condition
));
591 case INDEX_op_mov_i32
:
593 t1
= tci_read_r32(regs
, &tb_ptr
);
594 tci_write_reg32(regs
, t0
, t1
);
596 case INDEX_op_tci_movi_i32
:
598 t1
= tci_read_i32(&tb_ptr
);
599 tci_write_reg32(regs
, t0
, t1
);
602 /* Load/store operations (32 bit). */
604 case INDEX_op_ld8u_i32
:
606 t1
= tci_read_r(regs
, &tb_ptr
);
607 t2
= tci_read_s32(&tb_ptr
);
608 tci_write_reg8(regs
, t0
, *(uint8_t *)(t1
+ t2
));
610 case INDEX_op_ld8s_i32
:
613 case INDEX_op_ld16u_i32
:
616 case INDEX_op_ld16s_i32
:
619 case INDEX_op_ld_i32
:
621 t1
= tci_read_r(regs
, &tb_ptr
);
622 t2
= tci_read_s32(&tb_ptr
);
623 tci_write_reg32(regs
, t0
, *(uint32_t *)(t1
+ t2
));
625 case INDEX_op_st8_i32
:
626 t0
= tci_read_r8(regs
, &tb_ptr
);
627 t1
= tci_read_r(regs
, &tb_ptr
);
628 t2
= tci_read_s32(&tb_ptr
);
629 *(uint8_t *)(t1
+ t2
) = t0
;
631 case INDEX_op_st16_i32
:
632 t0
= tci_read_r16(regs
, &tb_ptr
);
633 t1
= tci_read_r(regs
, &tb_ptr
);
634 t2
= tci_read_s32(&tb_ptr
);
635 *(uint16_t *)(t1
+ t2
) = t0
;
637 case INDEX_op_st_i32
:
638 t0
= tci_read_r32(regs
, &tb_ptr
);
639 t1
= tci_read_r(regs
, &tb_ptr
);
640 t2
= tci_read_s32(&tb_ptr
);
641 tci_assert(t1
!= sp_value
|| (int32_t)t2
< 0);
642 *(uint32_t *)(t1
+ t2
) = t0
;
645 /* Arithmetic operations (32 bit). */
647 case INDEX_op_add_i32
:
649 t1
= tci_read_ri32(regs
, &tb_ptr
);
650 t2
= tci_read_ri32(regs
, &tb_ptr
);
651 tci_write_reg32(regs
, t0
, t1
+ t2
);
653 case INDEX_op_sub_i32
:
655 t1
= tci_read_ri32(regs
, &tb_ptr
);
656 t2
= tci_read_ri32(regs
, &tb_ptr
);
657 tci_write_reg32(regs
, t0
, t1
- t2
);
659 case INDEX_op_mul_i32
:
661 t1
= tci_read_ri32(regs
, &tb_ptr
);
662 t2
= tci_read_ri32(regs
, &tb_ptr
);
663 tci_write_reg32(regs
, t0
, t1
* t2
);
665 #if TCG_TARGET_HAS_div_i32
666 case INDEX_op_div_i32
:
668 t1
= tci_read_ri32(regs
, &tb_ptr
);
669 t2
= tci_read_ri32(regs
, &tb_ptr
);
670 tci_write_reg32(regs
, t0
, (int32_t)t1
/ (int32_t)t2
);
672 case INDEX_op_divu_i32
:
674 t1
= tci_read_ri32(regs
, &tb_ptr
);
675 t2
= tci_read_ri32(regs
, &tb_ptr
);
676 tci_write_reg32(regs
, t0
, t1
/ t2
);
678 case INDEX_op_rem_i32
:
680 t1
= tci_read_ri32(regs
, &tb_ptr
);
681 t2
= tci_read_ri32(regs
, &tb_ptr
);
682 tci_write_reg32(regs
, t0
, (int32_t)t1
% (int32_t)t2
);
684 case INDEX_op_remu_i32
:
686 t1
= tci_read_ri32(regs
, &tb_ptr
);
687 t2
= tci_read_ri32(regs
, &tb_ptr
);
688 tci_write_reg32(regs
, t0
, t1
% t2
);
690 #elif TCG_TARGET_HAS_div2_i32
691 case INDEX_op_div2_i32
:
692 case INDEX_op_divu2_i32
:
696 case INDEX_op_and_i32
:
698 t1
= tci_read_ri32(regs
, &tb_ptr
);
699 t2
= tci_read_ri32(regs
, &tb_ptr
);
700 tci_write_reg32(regs
, t0
, t1
& t2
);
702 case INDEX_op_or_i32
:
704 t1
= tci_read_ri32(regs
, &tb_ptr
);
705 t2
= tci_read_ri32(regs
, &tb_ptr
);
706 tci_write_reg32(regs
, t0
, t1
| t2
);
708 case INDEX_op_xor_i32
:
710 t1
= tci_read_ri32(regs
, &tb_ptr
);
711 t2
= tci_read_ri32(regs
, &tb_ptr
);
712 tci_write_reg32(regs
, t0
, t1
^ t2
);
715 /* Shift/rotate operations (32 bit). */
717 case INDEX_op_shl_i32
:
719 t1
= tci_read_ri32(regs
, &tb_ptr
);
720 t2
= tci_read_ri32(regs
, &tb_ptr
);
721 tci_write_reg32(regs
, t0
, t1
<< (t2
& 31));
723 case INDEX_op_shr_i32
:
725 t1
= tci_read_ri32(regs
, &tb_ptr
);
726 t2
= tci_read_ri32(regs
, &tb_ptr
);
727 tci_write_reg32(regs
, t0
, t1
>> (t2
& 31));
729 case INDEX_op_sar_i32
:
731 t1
= tci_read_ri32(regs
, &tb_ptr
);
732 t2
= tci_read_ri32(regs
, &tb_ptr
);
733 tci_write_reg32(regs
, t0
, ((int32_t)t1
>> (t2
& 31)));
735 #if TCG_TARGET_HAS_rot_i32
736 case INDEX_op_rotl_i32
:
738 t1
= tci_read_ri32(regs
, &tb_ptr
);
739 t2
= tci_read_ri32(regs
, &tb_ptr
);
740 tci_write_reg32(regs
, t0
, rol32(t1
, t2
& 31));
742 case INDEX_op_rotr_i32
:
744 t1
= tci_read_ri32(regs
, &tb_ptr
);
745 t2
= tci_read_ri32(regs
, &tb_ptr
);
746 tci_write_reg32(regs
, t0
, ror32(t1
, t2
& 31));
749 #if TCG_TARGET_HAS_deposit_i32
750 case INDEX_op_deposit_i32
:
752 t1
= tci_read_r32(regs
, &tb_ptr
);
753 t2
= tci_read_r32(regs
, &tb_ptr
);
756 tmp32
= (((1 << tmp8
) - 1) << tmp16
);
757 tci_write_reg32(regs
, t0
, (t1
& ~tmp32
) | ((t2
<< tmp16
) & tmp32
));
760 case INDEX_op_brcond_i32
:
761 t0
= tci_read_r32(regs
, &tb_ptr
);
762 t1
= tci_read_ri32(regs
, &tb_ptr
);
763 condition
= *tb_ptr
++;
764 label
= tci_read_label(&tb_ptr
);
765 if (tci_compare32(t0
, t1
, condition
)) {
766 tci_assert(tb_ptr
== old_code_ptr
+ op_size
);
767 tb_ptr
= (uint8_t *)label
;
771 #if TCG_TARGET_REG_BITS == 32
772 case INDEX_op_add2_i32
:
775 tmp64
= tci_read_r64(regs
, &tb_ptr
);
776 tmp64
+= tci_read_r64(regs
, &tb_ptr
);
777 tci_write_reg64(regs
, t1
, t0
, tmp64
);
779 case INDEX_op_sub2_i32
:
782 tmp64
= tci_read_r64(regs
, &tb_ptr
);
783 tmp64
-= tci_read_r64(regs
, &tb_ptr
);
784 tci_write_reg64(regs
, t1
, t0
, tmp64
);
786 case INDEX_op_brcond2_i32
:
787 tmp64
= tci_read_r64(regs
, &tb_ptr
);
788 v64
= tci_read_ri64(regs
, &tb_ptr
);
789 condition
= *tb_ptr
++;
790 label
= tci_read_label(&tb_ptr
);
791 if (tci_compare64(tmp64
, v64
, condition
)) {
792 tci_assert(tb_ptr
== old_code_ptr
+ op_size
);
793 tb_ptr
= (uint8_t *)label
;
797 case INDEX_op_mulu2_i32
:
800 t2
= tci_read_r32(regs
, &tb_ptr
);
801 tmp64
= tci_read_r32(regs
, &tb_ptr
);
802 tci_write_reg64(regs
, t1
, t0
, t2
* tmp64
);
804 #endif /* TCG_TARGET_REG_BITS == 32 */
805 #if TCG_TARGET_HAS_ext8s_i32
806 case INDEX_op_ext8s_i32
:
808 t1
= tci_read_r8s(regs
, &tb_ptr
);
809 tci_write_reg32(regs
, t0
, t1
);
812 #if TCG_TARGET_HAS_ext16s_i32
813 case INDEX_op_ext16s_i32
:
815 t1
= tci_read_r16s(regs
, &tb_ptr
);
816 tci_write_reg32(regs
, t0
, t1
);
819 #if TCG_TARGET_HAS_ext8u_i32
820 case INDEX_op_ext8u_i32
:
822 t1
= tci_read_r8(regs
, &tb_ptr
);
823 tci_write_reg32(regs
, t0
, t1
);
826 #if TCG_TARGET_HAS_ext16u_i32
827 case INDEX_op_ext16u_i32
:
829 t1
= tci_read_r16(regs
, &tb_ptr
);
830 tci_write_reg32(regs
, t0
, t1
);
833 #if TCG_TARGET_HAS_bswap16_i32
834 case INDEX_op_bswap16_i32
:
836 t1
= tci_read_r16(regs
, &tb_ptr
);
837 tci_write_reg32(regs
, t0
, bswap16(t1
));
840 #if TCG_TARGET_HAS_bswap32_i32
841 case INDEX_op_bswap32_i32
:
843 t1
= tci_read_r32(regs
, &tb_ptr
);
844 tci_write_reg32(regs
, t0
, bswap32(t1
));
847 #if TCG_TARGET_HAS_not_i32
848 case INDEX_op_not_i32
:
850 t1
= tci_read_r32(regs
, &tb_ptr
);
851 tci_write_reg32(regs
, t0
, ~t1
);
854 #if TCG_TARGET_HAS_neg_i32
855 case INDEX_op_neg_i32
:
857 t1
= tci_read_r32(regs
, &tb_ptr
);
858 tci_write_reg32(regs
, t0
, -t1
);
861 #if TCG_TARGET_REG_BITS == 64
862 case INDEX_op_mov_i64
:
864 t1
= tci_read_r64(regs
, &tb_ptr
);
865 tci_write_reg64(regs
, t0
, t1
);
867 case INDEX_op_tci_movi_i64
:
869 t1
= tci_read_i64(&tb_ptr
);
870 tci_write_reg64(regs
, t0
, t1
);
873 /* Load/store operations (64 bit). */
875 case INDEX_op_ld8u_i64
:
877 t1
= tci_read_r(regs
, &tb_ptr
);
878 t2
= tci_read_s32(&tb_ptr
);
879 tci_write_reg8(regs
, t0
, *(uint8_t *)(t1
+ t2
));
881 case INDEX_op_ld8s_i64
:
884 case INDEX_op_ld16u_i64
:
886 t1
= tci_read_r(regs
, &tb_ptr
);
887 t2
= tci_read_s32(&tb_ptr
);
888 tci_write_reg16(regs
, t0
, *(uint16_t *)(t1
+ t2
));
890 case INDEX_op_ld16s_i64
:
893 case INDEX_op_ld32u_i64
:
895 t1
= tci_read_r(regs
, &tb_ptr
);
896 t2
= tci_read_s32(&tb_ptr
);
897 tci_write_reg32(regs
, t0
, *(uint32_t *)(t1
+ t2
));
899 case INDEX_op_ld32s_i64
:
901 t1
= tci_read_r(regs
, &tb_ptr
);
902 t2
= tci_read_s32(&tb_ptr
);
903 tci_write_reg32s(regs
, t0
, *(int32_t *)(t1
+ t2
));
905 case INDEX_op_ld_i64
:
907 t1
= tci_read_r(regs
, &tb_ptr
);
908 t2
= tci_read_s32(&tb_ptr
);
909 tci_write_reg64(regs
, t0
, *(uint64_t *)(t1
+ t2
));
911 case INDEX_op_st8_i64
:
912 t0
= tci_read_r8(regs
, &tb_ptr
);
913 t1
= tci_read_r(regs
, &tb_ptr
);
914 t2
= tci_read_s32(&tb_ptr
);
915 *(uint8_t *)(t1
+ t2
) = t0
;
917 case INDEX_op_st16_i64
:
918 t0
= tci_read_r16(regs
, &tb_ptr
);
919 t1
= tci_read_r(regs
, &tb_ptr
);
920 t2
= tci_read_s32(&tb_ptr
);
921 *(uint16_t *)(t1
+ t2
) = t0
;
923 case INDEX_op_st32_i64
:
924 t0
= tci_read_r32(regs
, &tb_ptr
);
925 t1
= tci_read_r(regs
, &tb_ptr
);
926 t2
= tci_read_s32(&tb_ptr
);
927 *(uint32_t *)(t1
+ t2
) = t0
;
929 case INDEX_op_st_i64
:
930 t0
= tci_read_r64(regs
, &tb_ptr
);
931 t1
= tci_read_r(regs
, &tb_ptr
);
932 t2
= tci_read_s32(&tb_ptr
);
933 tci_assert(t1
!= sp_value
|| (int32_t)t2
< 0);
934 *(uint64_t *)(t1
+ t2
) = t0
;
937 /* Arithmetic operations (64 bit). */
939 case INDEX_op_add_i64
:
941 t1
= tci_read_ri64(regs
, &tb_ptr
);
942 t2
= tci_read_ri64(regs
, &tb_ptr
);
943 tci_write_reg64(regs
, t0
, t1
+ t2
);
945 case INDEX_op_sub_i64
:
947 t1
= tci_read_ri64(regs
, &tb_ptr
);
948 t2
= tci_read_ri64(regs
, &tb_ptr
);
949 tci_write_reg64(regs
, t0
, t1
- t2
);
951 case INDEX_op_mul_i64
:
953 t1
= tci_read_ri64(regs
, &tb_ptr
);
954 t2
= tci_read_ri64(regs
, &tb_ptr
);
955 tci_write_reg64(regs
, t0
, t1
* t2
);
957 #if TCG_TARGET_HAS_div_i64
958 case INDEX_op_div_i64
:
959 case INDEX_op_divu_i64
:
960 case INDEX_op_rem_i64
:
961 case INDEX_op_remu_i64
:
964 #elif TCG_TARGET_HAS_div2_i64
965 case INDEX_op_div2_i64
:
966 case INDEX_op_divu2_i64
:
970 case INDEX_op_and_i64
:
972 t1
= tci_read_ri64(regs
, &tb_ptr
);
973 t2
= tci_read_ri64(regs
, &tb_ptr
);
974 tci_write_reg64(regs
, t0
, t1
& t2
);
976 case INDEX_op_or_i64
:
978 t1
= tci_read_ri64(regs
, &tb_ptr
);
979 t2
= tci_read_ri64(regs
, &tb_ptr
);
980 tci_write_reg64(regs
, t0
, t1
| t2
);
982 case INDEX_op_xor_i64
:
984 t1
= tci_read_ri64(regs
, &tb_ptr
);
985 t2
= tci_read_ri64(regs
, &tb_ptr
);
986 tci_write_reg64(regs
, t0
, t1
^ t2
);
989 /* Shift/rotate operations (64 bit). */
991 case INDEX_op_shl_i64
:
993 t1
= tci_read_ri64(regs
, &tb_ptr
);
994 t2
= tci_read_ri64(regs
, &tb_ptr
);
995 tci_write_reg64(regs
, t0
, t1
<< (t2
& 63));
997 case INDEX_op_shr_i64
:
999 t1
= tci_read_ri64(regs
, &tb_ptr
);
1000 t2
= tci_read_ri64(regs
, &tb_ptr
);
1001 tci_write_reg64(regs
, t0
, t1
>> (t2
& 63));
1003 case INDEX_op_sar_i64
:
1005 t1
= tci_read_ri64(regs
, &tb_ptr
);
1006 t2
= tci_read_ri64(regs
, &tb_ptr
);
1007 tci_write_reg64(regs
, t0
, ((int64_t)t1
>> (t2
& 63)));
1009 #if TCG_TARGET_HAS_rot_i64
1010 case INDEX_op_rotl_i64
:
1012 t1
= tci_read_ri64(regs
, &tb_ptr
);
1013 t2
= tci_read_ri64(regs
, &tb_ptr
);
1014 tci_write_reg64(regs
, t0
, rol64(t1
, t2
& 63));
1016 case INDEX_op_rotr_i64
:
1018 t1
= tci_read_ri64(regs
, &tb_ptr
);
1019 t2
= tci_read_ri64(regs
, &tb_ptr
);
1020 tci_write_reg64(regs
, t0
, ror64(t1
, t2
& 63));
1023 #if TCG_TARGET_HAS_deposit_i64
1024 case INDEX_op_deposit_i64
:
1026 t1
= tci_read_r64(regs
, &tb_ptr
);
1027 t2
= tci_read_r64(regs
, &tb_ptr
);
1030 tmp64
= (((1ULL << tmp8
) - 1) << tmp16
);
1031 tci_write_reg64(regs
, t0
, (t1
& ~tmp64
) | ((t2
<< tmp16
) & tmp64
));
1034 case INDEX_op_brcond_i64
:
1035 t0
= tci_read_r64(regs
, &tb_ptr
);
1036 t1
= tci_read_ri64(regs
, &tb_ptr
);
1037 condition
= *tb_ptr
++;
1038 label
= tci_read_label(&tb_ptr
);
1039 if (tci_compare64(t0
, t1
, condition
)) {
1040 tci_assert(tb_ptr
== old_code_ptr
+ op_size
);
1041 tb_ptr
= (uint8_t *)label
;
1045 #if TCG_TARGET_HAS_ext8u_i64
1046 case INDEX_op_ext8u_i64
:
1048 t1
= tci_read_r8(regs
, &tb_ptr
);
1049 tci_write_reg64(regs
, t0
, t1
);
1052 #if TCG_TARGET_HAS_ext8s_i64
1053 case INDEX_op_ext8s_i64
:
1055 t1
= tci_read_r8s(regs
, &tb_ptr
);
1056 tci_write_reg64(regs
, t0
, t1
);
1059 #if TCG_TARGET_HAS_ext16s_i64
1060 case INDEX_op_ext16s_i64
:
1062 t1
= tci_read_r16s(regs
, &tb_ptr
);
1063 tci_write_reg64(regs
, t0
, t1
);
1066 #if TCG_TARGET_HAS_ext16u_i64
1067 case INDEX_op_ext16u_i64
:
1069 t1
= tci_read_r16(regs
, &tb_ptr
);
1070 tci_write_reg64(regs
, t0
, t1
);
1073 #if TCG_TARGET_HAS_ext32s_i64
1074 case INDEX_op_ext32s_i64
:
1076 case INDEX_op_ext_i32_i64
:
1078 t1
= tci_read_r32s(regs
, &tb_ptr
);
1079 tci_write_reg64(regs
, t0
, t1
);
1081 #if TCG_TARGET_HAS_ext32u_i64
1082 case INDEX_op_ext32u_i64
:
1084 case INDEX_op_extu_i32_i64
:
1086 t1
= tci_read_r32(regs
, &tb_ptr
);
1087 tci_write_reg64(regs
, t0
, t1
);
1089 #if TCG_TARGET_HAS_bswap16_i64
1090 case INDEX_op_bswap16_i64
:
1092 t1
= tci_read_r16(regs
, &tb_ptr
);
1093 tci_write_reg64(regs
, t0
, bswap16(t1
));
1096 #if TCG_TARGET_HAS_bswap32_i64
1097 case INDEX_op_bswap32_i64
:
1099 t1
= tci_read_r32(regs
, &tb_ptr
);
1100 tci_write_reg64(regs
, t0
, bswap32(t1
));
1103 #if TCG_TARGET_HAS_bswap64_i64
1104 case INDEX_op_bswap64_i64
:
1106 t1
= tci_read_r64(regs
, &tb_ptr
);
1107 tci_write_reg64(regs
, t0
, bswap64(t1
));
1110 #if TCG_TARGET_HAS_not_i64
1111 case INDEX_op_not_i64
:
1113 t1
= tci_read_r64(regs
, &tb_ptr
);
1114 tci_write_reg64(regs
, t0
, ~t1
);
1117 #if TCG_TARGET_HAS_neg_i64
1118 case INDEX_op_neg_i64
:
1120 t1
= tci_read_r64(regs
, &tb_ptr
);
1121 tci_write_reg64(regs
, t0
, -t1
);
1124 #endif /* TCG_TARGET_REG_BITS == 64 */
1126 /* QEMU specific operations. */
1128 case INDEX_op_exit_tb
:
1129 ret
= *(uint64_t *)tb_ptr
;
1132 case INDEX_op_goto_tb
:
1133 /* Jump address is aligned */
1134 tb_ptr
= QEMU_ALIGN_PTR_UP(tb_ptr
, 4);
1135 t0
= qatomic_read((int32_t *)tb_ptr
);
1136 tb_ptr
+= sizeof(int32_t);
1137 tci_assert(tb_ptr
== old_code_ptr
+ op_size
);
1138 tb_ptr
+= (int32_t)t0
;
1140 case INDEX_op_qemu_ld_i32
:
1142 taddr
= tci_read_ulong(regs
, &tb_ptr
);
1143 oi
= tci_read_i(&tb_ptr
);
1144 switch (get_memop(oi
) & (MO_BSWAP
| MO_SSIZE
)) {
1149 tmp32
= (int8_t)qemu_ld_ub
;
1152 tmp32
= qemu_ld_leuw
;
1155 tmp32
= (int16_t)qemu_ld_leuw
;
1158 tmp32
= qemu_ld_leul
;
1161 tmp32
= qemu_ld_beuw
;
1164 tmp32
= (int16_t)qemu_ld_beuw
;
1167 tmp32
= qemu_ld_beul
;
1172 tci_write_reg(regs
, t0
, tmp32
);
1174 case INDEX_op_qemu_ld_i64
:
1176 if (TCG_TARGET_REG_BITS
== 32) {
1179 taddr
= tci_read_ulong(regs
, &tb_ptr
);
1180 oi
= tci_read_i(&tb_ptr
);
1181 switch (get_memop(oi
) & (MO_BSWAP
| MO_SSIZE
)) {
1186 tmp64
= (int8_t)qemu_ld_ub
;
1189 tmp64
= qemu_ld_leuw
;
1192 tmp64
= (int16_t)qemu_ld_leuw
;
1195 tmp64
= qemu_ld_leul
;
1198 tmp64
= (int32_t)qemu_ld_leul
;
1201 tmp64
= qemu_ld_leq
;
1204 tmp64
= qemu_ld_beuw
;
1207 tmp64
= (int16_t)qemu_ld_beuw
;
1210 tmp64
= qemu_ld_beul
;
1213 tmp64
= (int32_t)qemu_ld_beul
;
1216 tmp64
= qemu_ld_beq
;
1221 tci_write_reg(regs
, t0
, tmp64
);
1222 if (TCG_TARGET_REG_BITS
== 32) {
1223 tci_write_reg(regs
, t1
, tmp64
>> 32);
1226 case INDEX_op_qemu_st_i32
:
1227 t0
= tci_read_r(regs
, &tb_ptr
);
1228 taddr
= tci_read_ulong(regs
, &tb_ptr
);
1229 oi
= tci_read_i(&tb_ptr
);
1230 switch (get_memop(oi
) & (MO_BSWAP
| MO_SIZE
)) {
1250 case INDEX_op_qemu_st_i64
:
1251 tmp64
= tci_read_r64(regs
, &tb_ptr
);
1252 taddr
= tci_read_ulong(regs
, &tb_ptr
);
1253 oi
= tci_read_i(&tb_ptr
);
1254 switch (get_memop(oi
) & (MO_BSWAP
| MO_SIZE
)) {
1281 /* Ensure ordering for all kinds */
1288 tci_assert(tb_ptr
== old_code_ptr
+ op_size
);