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
25 #include "qemu/osdep.h"
27 #include "tcg/tcg-temp-internal.h"
28 #include "tcg/tcg-op-common.h"
29 #include "tcg/tcg-mo.h"
30 #include "exec/translation-block.h"
31 #include "exec/plugin-gen.h"
32 #include "tcg-internal.h"
35 static void check_max_alignment(unsigned a_bits)
37 #if defined(CONFIG_SOFTMMU)
39 * The requested alignment cannot overlap the TLB flags.
40 * FIXME: Must keep the count up-to-date with "exec/cpu-all.h".
42 tcg_debug_assert(a_bits + 5 <= tcg_ctx->page_bits);
46 static MemOp tcg_canonicalize_memop(MemOp op, bool is64, bool st)
48 unsigned a_bits = get_alignment_bits(op);
50 check_max_alignment(a_bits);
52 /* Prefer MO_ALIGN+MO_XX over MO_ALIGN_XX+MO_XX */
53 if (a_bits == (op & MO_SIZE)) {
54 op = (op & ~MO_AMASK) | MO_ALIGN;
57 switch (op & MO_SIZE) {
75 g_assert_not_reached();
83 static void gen_ldst(TCGOpcode opc, TCGTemp *vl, TCGTemp *vh,
84 TCGTemp *addr, MemOpIdx oi)
86 if (TCG_TARGET_REG_BITS == 64 || tcg_ctx->addr_type == TCG_TYPE_I32) {
88 tcg_gen_op4(opc, temp_arg(vl), temp_arg(vh), temp_arg(addr), oi);
90 tcg_gen_op3(opc, temp_arg(vl), temp_arg(addr), oi);
93 /* See TCGV_LOW/HIGH. */
94 TCGTemp *al = addr + HOST_BIG_ENDIAN;
95 TCGTemp *ah = addr + !HOST_BIG_ENDIAN;
98 tcg_gen_op5(opc, temp_arg(vl), temp_arg(vh),
99 temp_arg(al), temp_arg(ah), oi);
101 tcg_gen_op4(opc, temp_arg(vl), temp_arg(al), temp_arg(ah), oi);
106 static void gen_ldst_i64(TCGOpcode opc, TCGv_i64 v, TCGTemp *addr, MemOpIdx oi)
108 if (TCG_TARGET_REG_BITS == 32) {
109 TCGTemp *vl = tcgv_i32_temp(TCGV_LOW(v));
110 TCGTemp *vh = tcgv_i32_temp(TCGV_HIGH(v));
111 gen_ldst(opc, vl, vh, addr, oi);
113 gen_ldst(opc, tcgv_i64_temp(v), NULL, addr, oi);
117 static void tcg_gen_req_mo(TCGBar type)
119 type &= tcg_ctx->guest_mo;
120 type &= ~TCG_TARGET_DEFAULT_MO;
122 tcg_gen_mb(type | TCG_BAR_SC);
126 /* Only required for loads, where value might overlap addr. */
127 static TCGv_i64 plugin_maybe_preserve_addr(TCGTemp *addr)
130 if (tcg_ctx->plugin_insn != NULL) {
131 /* Save a copy of the vaddr for use after a load. */
132 TCGv_i64 temp = tcg_temp_ebb_new_i64();
133 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
134 tcg_gen_extu_i32_i64(temp, temp_tcgv_i32(addr));
136 tcg_gen_mov_i64(temp, temp_tcgv_i64(addr));
145 plugin_gen_mem_callbacks(TCGv_i64 copy_addr, TCGTemp *orig_addr, MemOpIdx oi,
146 enum qemu_plugin_mem_rw rw)
149 if (tcg_ctx->plugin_insn != NULL) {
150 qemu_plugin_meminfo_t info = make_plugin_meminfo(oi, rw);
152 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
154 copy_addr = tcg_temp_ebb_new_i64();
155 tcg_gen_extu_i32_i64(copy_addr, temp_tcgv_i32(orig_addr));
157 plugin_gen_empty_mem_callback(copy_addr, info);
158 tcg_temp_free_i64(copy_addr);
161 plugin_gen_empty_mem_callback(copy_addr, info);
162 tcg_temp_free_i64(copy_addr);
164 plugin_gen_empty_mem_callback(temp_tcgv_i64(orig_addr), info);
171 static void tcg_gen_qemu_ld_i32_int(TCGv_i32 val, TCGTemp *addr,
172 TCGArg idx, MemOp memop)
175 MemOpIdx orig_oi, oi;
179 tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
180 orig_memop = memop = tcg_canonicalize_memop(memop, 0, 0);
181 orig_oi = oi = make_memop_idx(memop, idx);
183 if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
185 /* The bswap primitive benefits from zero-extended input. */
186 if ((memop & MO_SSIZE) == MO_SW) {
189 oi = make_memop_idx(memop, idx);
192 copy_addr = plugin_maybe_preserve_addr(addr);
193 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
194 opc = INDEX_op_qemu_ld_a32_i32;
196 opc = INDEX_op_qemu_ld_a64_i32;
198 gen_ldst(opc, tcgv_i32_temp(val), NULL, addr, oi);
199 plugin_gen_mem_callbacks(copy_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
201 if ((orig_memop ^ memop) & MO_BSWAP) {
202 switch (orig_memop & MO_SIZE) {
204 tcg_gen_bswap16_i32(val, val, (orig_memop & MO_SIGN
205 ? TCG_BSWAP_IZ | TCG_BSWAP_OS
206 : TCG_BSWAP_IZ | TCG_BSWAP_OZ));
209 tcg_gen_bswap32_i32(val, val);
212 g_assert_not_reached();
217 void tcg_gen_qemu_ld_i32_chk(TCGv_i32 val, TCGTemp *addr, TCGArg idx,
218 MemOp memop, TCGType addr_type)
220 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
221 tcg_debug_assert((memop & MO_SIZE) <= MO_32);
222 tcg_gen_qemu_ld_i32_int(val, addr, idx, memop);
225 static void tcg_gen_qemu_st_i32_int(TCGv_i32 val, TCGTemp *addr,
226 TCGArg idx, MemOp memop)
228 TCGv_i32 swap = NULL;
229 MemOpIdx orig_oi, oi;
232 tcg_gen_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
233 memop = tcg_canonicalize_memop(memop, 0, 1);
234 orig_oi = oi = make_memop_idx(memop, idx);
236 if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
237 swap = tcg_temp_ebb_new_i32();
238 switch (memop & MO_SIZE) {
240 tcg_gen_bswap16_i32(swap, val, 0);
243 tcg_gen_bswap32_i32(swap, val);
246 g_assert_not_reached();
250 oi = make_memop_idx(memop, idx);
253 if (TCG_TARGET_HAS_qemu_st8_i32 && (memop & MO_SIZE) == MO_8) {
254 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
255 opc = INDEX_op_qemu_st8_a32_i32;
257 opc = INDEX_op_qemu_st8_a64_i32;
260 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
261 opc = INDEX_op_qemu_st_a32_i32;
263 opc = INDEX_op_qemu_st_a64_i32;
266 gen_ldst(opc, tcgv_i32_temp(val), NULL, addr, oi);
267 plugin_gen_mem_callbacks(NULL, addr, orig_oi, QEMU_PLUGIN_MEM_W);
270 tcg_temp_free_i32(swap);
274 void tcg_gen_qemu_st_i32_chk(TCGv_i32 val, TCGTemp *addr, TCGArg idx,
275 MemOp memop, TCGType addr_type)
277 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
278 tcg_debug_assert((memop & MO_SIZE) <= MO_32);
279 tcg_gen_qemu_st_i32_int(val, addr, idx, memop);
282 static void tcg_gen_qemu_ld_i64_int(TCGv_i64 val, TCGTemp *addr,
283 TCGArg idx, MemOp memop)
286 MemOpIdx orig_oi, oi;
290 if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
291 tcg_gen_qemu_ld_i32_int(TCGV_LOW(val), addr, idx, memop);
292 if (memop & MO_SIGN) {
293 tcg_gen_sari_i32(TCGV_HIGH(val), TCGV_LOW(val), 31);
295 tcg_gen_movi_i32(TCGV_HIGH(val), 0);
300 tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
301 orig_memop = memop = tcg_canonicalize_memop(memop, 1, 0);
302 orig_oi = oi = make_memop_idx(memop, idx);
304 if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
306 /* The bswap primitive benefits from zero-extended input. */
307 if ((memop & MO_SIGN) && (memop & MO_SIZE) < MO_64) {
310 oi = make_memop_idx(memop, idx);
313 copy_addr = plugin_maybe_preserve_addr(addr);
314 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
315 opc = INDEX_op_qemu_ld_a32_i64;
317 opc = INDEX_op_qemu_ld_a64_i64;
319 gen_ldst_i64(opc, val, addr, oi);
320 plugin_gen_mem_callbacks(copy_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
322 if ((orig_memop ^ memop) & MO_BSWAP) {
323 int flags = (orig_memop & MO_SIGN
324 ? TCG_BSWAP_IZ | TCG_BSWAP_OS
325 : TCG_BSWAP_IZ | TCG_BSWAP_OZ);
326 switch (orig_memop & MO_SIZE) {
328 tcg_gen_bswap16_i64(val, val, flags);
331 tcg_gen_bswap32_i64(val, val, flags);
334 tcg_gen_bswap64_i64(val, val);
337 g_assert_not_reached();
342 void tcg_gen_qemu_ld_i64_chk(TCGv_i64 val, TCGTemp *addr, TCGArg idx,
343 MemOp memop, TCGType addr_type)
345 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
346 tcg_debug_assert((memop & MO_SIZE) <= MO_64);
347 tcg_gen_qemu_ld_i64_int(val, addr, idx, memop);
350 static void tcg_gen_qemu_st_i64_int(TCGv_i64 val, TCGTemp *addr,
351 TCGArg idx, MemOp memop)
353 TCGv_i64 swap = NULL;
354 MemOpIdx orig_oi, oi;
357 if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
358 tcg_gen_qemu_st_i32_int(TCGV_LOW(val), addr, idx, memop);
362 tcg_gen_req_mo(TCG_MO_LD_ST | TCG_MO_ST_ST);
363 memop = tcg_canonicalize_memop(memop, 1, 1);
364 orig_oi = oi = make_memop_idx(memop, idx);
366 if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
367 swap = tcg_temp_ebb_new_i64();
368 switch (memop & MO_SIZE) {
370 tcg_gen_bswap16_i64(swap, val, 0);
373 tcg_gen_bswap32_i64(swap, val, 0);
376 tcg_gen_bswap64_i64(swap, val);
379 g_assert_not_reached();
383 oi = make_memop_idx(memop, idx);
386 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
387 opc = INDEX_op_qemu_st_a32_i64;
389 opc = INDEX_op_qemu_st_a64_i64;
391 gen_ldst_i64(opc, val, addr, oi);
392 plugin_gen_mem_callbacks(NULL, addr, orig_oi, QEMU_PLUGIN_MEM_W);
395 tcg_temp_free_i64(swap);
399 void tcg_gen_qemu_st_i64_chk(TCGv_i64 val, TCGTemp *addr, TCGArg idx,
400 MemOp memop, TCGType addr_type)
402 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
403 tcg_debug_assert((memop & MO_SIZE) <= MO_64);
404 tcg_gen_qemu_st_i64_int(val, addr, idx, memop);
408 * Return true if @mop, without knowledge of the pointer alignment,
409 * does not require 16-byte atomicity, and it would be adventagous
410 * to avoid a call to a helper function.
412 static bool use_two_i64_for_i128(MemOp mop)
414 #ifdef CONFIG_SOFTMMU
415 /* Two softmmu tlb lookups is larger than one function call. */
419 * For user-only, two 64-bit operations may well be smaller than a call.
420 * Determine if that would be legal for the requested atomicity.
422 switch (mop & MO_ATOM_MASK) {
424 case MO_ATOM_IFALIGN_PAIR:
426 case MO_ATOM_IFALIGN:
427 case MO_ATOM_SUBALIGN:
428 case MO_ATOM_WITHIN16:
429 case MO_ATOM_WITHIN16_PAIR:
430 /* In a serialized context, no atomicity is required. */
431 return !(tcg_ctx->gen_tb->cflags & CF_PARALLEL);
433 g_assert_not_reached();
438 static void canonicalize_memop_i128_as_i64(MemOp ret[2], MemOp orig)
440 MemOp mop_1 = orig, mop_2;
442 /* Reduce the size to 64-bit. */
443 mop_1 = (mop_1 & ~MO_SIZE) | MO_64;
445 /* Retain the alignment constraints of the original. */
446 switch (orig & MO_AMASK) {
453 /* Prefer MO_ALIGN+MO_64 to MO_ALIGN_8+MO_64. */
454 mop_1 = (mop_1 & ~MO_AMASK) | MO_ALIGN;
458 /* Second has 8-byte alignment; first has 16-byte alignment. */
460 mop_1 = (mop_1 & ~MO_AMASK) | MO_ALIGN_16;
465 /* Second has 8-byte alignment; first retains original. */
466 mop_2 = (mop_1 & ~MO_AMASK) | MO_ALIGN;
469 g_assert_not_reached();
472 /* Use a memory ordering implemented by the host. */
473 if ((orig & MO_BSWAP) && !tcg_target_has_memory_bswap(mop_1)) {
482 static TCGv_i64 maybe_extend_addr64(TCGTemp *addr)
484 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
485 TCGv_i64 a64 = tcg_temp_ebb_new_i64();
486 tcg_gen_extu_i32_i64(a64, temp_tcgv_i32(addr));
489 return temp_tcgv_i64(addr);
492 static void maybe_free_addr64(TCGv_i64 a64)
494 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
495 tcg_temp_free_i64(a64);
499 static void tcg_gen_qemu_ld_i128_int(TCGv_i128 val, TCGTemp *addr,
500 TCGArg idx, MemOp memop)
502 const MemOpIdx orig_oi = make_memop_idx(memop, idx);
503 TCGv_i64 ext_addr = NULL;
506 check_max_alignment(get_alignment_bits(memop));
507 tcg_gen_req_mo(TCG_MO_LD_LD | TCG_MO_ST_LD);
509 /* TODO: For now, force 32-bit hosts to use the helper. */
510 if (TCG_TARGET_HAS_qemu_ldst_i128 && TCG_TARGET_REG_BITS == 64) {
512 bool need_bswap = false;
513 MemOpIdx oi = orig_oi;
515 if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
516 lo = TCGV128_HIGH(val);
517 hi = TCGV128_LOW(val);
518 oi = make_memop_idx(memop & ~MO_BSWAP, idx);
521 lo = TCGV128_LOW(val);
522 hi = TCGV128_HIGH(val);
525 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
526 opc = INDEX_op_qemu_ld_a32_i128;
528 opc = INDEX_op_qemu_ld_a64_i128;
530 gen_ldst(opc, tcgv_i64_temp(lo), tcgv_i64_temp(hi), addr, oi);
533 tcg_gen_bswap64_i64(lo, lo);
534 tcg_gen_bswap64_i64(hi, hi);
536 } else if (use_two_i64_for_i128(memop)) {
542 canonicalize_memop_i128_as_i64(mop, memop);
543 need_bswap = (mop[0] ^ memop) & MO_BSWAP;
545 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
546 opc = INDEX_op_qemu_ld_a32_i64;
548 opc = INDEX_op_qemu_ld_a64_i64;
552 * Since there are no global TCGv_i128, there is no visible state
553 * changed if the second load faults. Load directly into the two
556 if ((memop & MO_BSWAP) == MO_LE) {
557 x = TCGV128_LOW(val);
558 y = TCGV128_HIGH(val);
560 x = TCGV128_HIGH(val);
561 y = TCGV128_LOW(val);
564 gen_ldst_i64(opc, x, addr, make_memop_idx(mop[0], idx));
567 tcg_gen_bswap64_i64(x, x);
570 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
571 TCGv_i32 t = tcg_temp_ebb_new_i32();
572 tcg_gen_addi_i32(t, temp_tcgv_i32(addr), 8);
573 addr_p8 = tcgv_i32_temp(t);
575 TCGv_i64 t = tcg_temp_ebb_new_i64();
576 tcg_gen_addi_i64(t, temp_tcgv_i64(addr), 8);
577 addr_p8 = tcgv_i64_temp(t);
580 gen_ldst_i64(opc, y, addr_p8, make_memop_idx(mop[1], idx));
581 tcg_temp_free_internal(addr_p8);
584 tcg_gen_bswap64_i64(y, y);
587 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
588 ext_addr = tcg_temp_ebb_new_i64();
589 tcg_gen_extu_i32_i64(ext_addr, temp_tcgv_i32(addr));
590 addr = tcgv_i64_temp(ext_addr);
592 gen_helper_ld_i128(val, cpu_env, temp_tcgv_i64(addr),
593 tcg_constant_i32(orig_oi));
596 plugin_gen_mem_callbacks(ext_addr, addr, orig_oi, QEMU_PLUGIN_MEM_R);
599 void tcg_gen_qemu_ld_i128_chk(TCGv_i128 val, TCGTemp *addr, TCGArg idx,
600 MemOp memop, TCGType addr_type)
602 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
603 tcg_debug_assert((memop & MO_SIZE) == MO_128);
604 tcg_debug_assert((memop & MO_SIGN) == 0);
605 tcg_gen_qemu_ld_i128_int(val, addr, idx, memop);
608 static void tcg_gen_qemu_st_i128_int(TCGv_i128 val, TCGTemp *addr,
609 TCGArg idx, MemOp memop)
611 const MemOpIdx orig_oi = make_memop_idx(memop, idx);
612 TCGv_i64 ext_addr = NULL;
615 check_max_alignment(get_alignment_bits(memop));
616 tcg_gen_req_mo(TCG_MO_ST_LD | TCG_MO_ST_ST);
618 /* TODO: For now, force 32-bit hosts to use the helper. */
620 if (TCG_TARGET_HAS_qemu_ldst_i128 && TCG_TARGET_REG_BITS == 64) {
622 MemOpIdx oi = orig_oi;
623 bool need_bswap = false;
625 if ((memop & MO_BSWAP) && !tcg_target_has_memory_bswap(memop)) {
626 lo = tcg_temp_ebb_new_i64();
627 hi = tcg_temp_ebb_new_i64();
628 tcg_gen_bswap64_i64(lo, TCGV128_HIGH(val));
629 tcg_gen_bswap64_i64(hi, TCGV128_LOW(val));
630 oi = make_memop_idx(memop & ~MO_BSWAP, idx);
633 lo = TCGV128_LOW(val);
634 hi = TCGV128_HIGH(val);
637 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
638 opc = INDEX_op_qemu_st_a32_i128;
640 opc = INDEX_op_qemu_st_a64_i128;
642 gen_ldst(opc, tcgv_i64_temp(lo), tcgv_i64_temp(hi), addr, oi);
645 tcg_temp_free_i64(lo);
646 tcg_temp_free_i64(hi);
648 } else if (use_two_i64_for_i128(memop)) {
651 TCGv_i64 x, y, b = NULL;
653 canonicalize_memop_i128_as_i64(mop, memop);
655 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
656 opc = INDEX_op_qemu_st_a32_i64;
658 opc = INDEX_op_qemu_st_a64_i64;
661 if ((memop & MO_BSWAP) == MO_LE) {
662 x = TCGV128_LOW(val);
663 y = TCGV128_HIGH(val);
665 x = TCGV128_HIGH(val);
666 y = TCGV128_LOW(val);
669 if ((mop[0] ^ memop) & MO_BSWAP) {
670 b = tcg_temp_ebb_new_i64();
671 tcg_gen_bswap64_i64(b, x);
675 gen_ldst_i64(opc, x, addr, make_memop_idx(mop[0], idx));
677 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
678 TCGv_i32 t = tcg_temp_ebb_new_i32();
679 tcg_gen_addi_i32(t, temp_tcgv_i32(addr), 8);
680 addr_p8 = tcgv_i32_temp(t);
682 TCGv_i64 t = tcg_temp_ebb_new_i64();
683 tcg_gen_addi_i64(t, temp_tcgv_i64(addr), 8);
684 addr_p8 = tcgv_i64_temp(t);
688 tcg_gen_bswap64_i64(b, y);
689 gen_ldst_i64(opc, b, addr_p8, make_memop_idx(mop[1], idx));
690 tcg_temp_free_i64(b);
692 gen_ldst_i64(opc, y, addr_p8, make_memop_idx(mop[1], idx));
694 tcg_temp_free_internal(addr_p8);
696 if (tcg_ctx->addr_type == TCG_TYPE_I32) {
697 ext_addr = tcg_temp_ebb_new_i64();
698 tcg_gen_extu_i32_i64(ext_addr, temp_tcgv_i32(addr));
699 addr = tcgv_i64_temp(ext_addr);
701 gen_helper_st_i128(cpu_env, temp_tcgv_i64(addr), val,
702 tcg_constant_i32(orig_oi));
705 plugin_gen_mem_callbacks(ext_addr, addr, orig_oi, QEMU_PLUGIN_MEM_W);
708 void tcg_gen_qemu_st_i128_chk(TCGv_i128 val, TCGTemp *addr, TCGArg idx,
709 MemOp memop, TCGType addr_type)
711 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
712 tcg_debug_assert((memop & MO_SIZE) == MO_128);
713 tcg_debug_assert((memop & MO_SIGN) == 0);
714 tcg_gen_qemu_st_i128_int(val, addr, idx, memop);
717 static void tcg_gen_ext_i32(TCGv_i32 ret, TCGv_i32 val, MemOp opc)
719 switch (opc & MO_SSIZE) {
721 tcg_gen_ext8s_i32(ret, val);
724 tcg_gen_ext8u_i32(ret, val);
727 tcg_gen_ext16s_i32(ret, val);
730 tcg_gen_ext16u_i32(ret, val);
733 tcg_gen_mov_i32(ret, val);
738 static void tcg_gen_ext_i64(TCGv_i64 ret, TCGv_i64 val, MemOp opc)
740 switch (opc & MO_SSIZE) {
742 tcg_gen_ext8s_i64(ret, val);
745 tcg_gen_ext8u_i64(ret, val);
748 tcg_gen_ext16s_i64(ret, val);
751 tcg_gen_ext16u_i64(ret, val);
754 tcg_gen_ext32s_i64(ret, val);
757 tcg_gen_ext32u_i64(ret, val);
760 tcg_gen_mov_i64(ret, val);
765 typedef void (*gen_atomic_cx_i32)(TCGv_i32, TCGv_env, TCGv_i64,
766 TCGv_i32, TCGv_i32, TCGv_i32);
767 typedef void (*gen_atomic_cx_i64)(TCGv_i64, TCGv_env, TCGv_i64,
768 TCGv_i64, TCGv_i64, TCGv_i32);
769 typedef void (*gen_atomic_cx_i128)(TCGv_i128, TCGv_env, TCGv_i64,
770 TCGv_i128, TCGv_i128, TCGv_i32);
771 typedef void (*gen_atomic_op_i32)(TCGv_i32, TCGv_env, TCGv_i64,
773 typedef void (*gen_atomic_op_i64)(TCGv_i64, TCGv_env, TCGv_i64,
776 #ifdef CONFIG_ATOMIC64
777 # define WITH_ATOMIC64(X) X,
779 # define WITH_ATOMIC64(X)
782 # define WITH_ATOMIC128(X) X,
784 # define WITH_ATOMIC128(X)
787 static void * const table_cmpxchg[(MO_SIZE | MO_BSWAP) + 1] = {
788 [MO_8] = gen_helper_atomic_cmpxchgb,
789 [MO_16 | MO_LE] = gen_helper_atomic_cmpxchgw_le,
790 [MO_16 | MO_BE] = gen_helper_atomic_cmpxchgw_be,
791 [MO_32 | MO_LE] = gen_helper_atomic_cmpxchgl_le,
792 [MO_32 | MO_BE] = gen_helper_atomic_cmpxchgl_be,
793 WITH_ATOMIC64([MO_64 | MO_LE] = gen_helper_atomic_cmpxchgq_le)
794 WITH_ATOMIC64([MO_64 | MO_BE] = gen_helper_atomic_cmpxchgq_be)
795 WITH_ATOMIC128([MO_128 | MO_LE] = gen_helper_atomic_cmpxchgo_le)
796 WITH_ATOMIC128([MO_128 | MO_BE] = gen_helper_atomic_cmpxchgo_be)
799 static void tcg_gen_nonatomic_cmpxchg_i32_int(TCGv_i32 retv, TCGTemp *addr,
800 TCGv_i32 cmpv, TCGv_i32 newv,
801 TCGArg idx, MemOp memop)
803 TCGv_i32 t1 = tcg_temp_ebb_new_i32();
804 TCGv_i32 t2 = tcg_temp_ebb_new_i32();
806 tcg_gen_ext_i32(t2, cmpv, memop & MO_SIZE);
808 tcg_gen_qemu_ld_i32_int(t1, addr, idx, memop & ~MO_SIGN);
809 tcg_gen_movcond_i32(TCG_COND_EQ, t2, t1, t2, newv, t1);
810 tcg_gen_qemu_st_i32_int(t2, addr, idx, memop);
811 tcg_temp_free_i32(t2);
813 if (memop & MO_SIGN) {
814 tcg_gen_ext_i32(retv, t1, memop);
816 tcg_gen_mov_i32(retv, t1);
818 tcg_temp_free_i32(t1);
821 void tcg_gen_nonatomic_cmpxchg_i32_chk(TCGv_i32 retv, TCGTemp *addr,
822 TCGv_i32 cmpv, TCGv_i32 newv,
823 TCGArg idx, MemOp memop,
826 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
827 tcg_debug_assert((memop & MO_SIZE) <= MO_32);
828 tcg_gen_nonatomic_cmpxchg_i32_int(retv, addr, cmpv, newv, idx, memop);
831 static void tcg_gen_atomic_cmpxchg_i32_int(TCGv_i32 retv, TCGTemp *addr,
832 TCGv_i32 cmpv, TCGv_i32 newv,
833 TCGArg idx, MemOp memop)
835 gen_atomic_cx_i32 gen;
839 if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
840 tcg_gen_nonatomic_cmpxchg_i32_int(retv, addr, cmpv, newv, idx, memop);
844 memop = tcg_canonicalize_memop(memop, 0, 0);
845 gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
846 tcg_debug_assert(gen != NULL);
848 oi = make_memop_idx(memop & ~MO_SIGN, idx);
849 a64 = maybe_extend_addr64(addr);
850 gen(retv, cpu_env, a64, cmpv, newv, tcg_constant_i32(oi));
851 maybe_free_addr64(a64);
853 if (memop & MO_SIGN) {
854 tcg_gen_ext_i32(retv, retv, memop);
858 void tcg_gen_atomic_cmpxchg_i32_chk(TCGv_i32 retv, TCGTemp *addr,
859 TCGv_i32 cmpv, TCGv_i32 newv,
860 TCGArg idx, MemOp memop,
863 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
864 tcg_debug_assert((memop & MO_SIZE) <= MO_32);
865 tcg_gen_atomic_cmpxchg_i32_int(retv, addr, cmpv, newv, idx, memop);
868 static void tcg_gen_nonatomic_cmpxchg_i64_int(TCGv_i64 retv, TCGTemp *addr,
869 TCGv_i64 cmpv, TCGv_i64 newv,
870 TCGArg idx, MemOp memop)
874 if (TCG_TARGET_REG_BITS == 32 && (memop & MO_SIZE) < MO_64) {
875 tcg_gen_nonatomic_cmpxchg_i32_int(TCGV_LOW(retv), addr, TCGV_LOW(cmpv),
876 TCGV_LOW(newv), idx, memop);
877 if (memop & MO_SIGN) {
878 tcg_gen_sari_i32(TCGV_HIGH(retv), TCGV_LOW(retv), 31);
880 tcg_gen_movi_i32(TCGV_HIGH(retv), 0);
885 t1 = tcg_temp_ebb_new_i64();
886 t2 = tcg_temp_ebb_new_i64();
888 tcg_gen_ext_i64(t2, cmpv, memop & MO_SIZE);
890 tcg_gen_qemu_ld_i64_int(t1, addr, idx, memop & ~MO_SIGN);
891 tcg_gen_movcond_i64(TCG_COND_EQ, t2, t1, t2, newv, t1);
892 tcg_gen_qemu_st_i64_int(t2, addr, idx, memop);
893 tcg_temp_free_i64(t2);
895 if (memop & MO_SIGN) {
896 tcg_gen_ext_i64(retv, t1, memop);
898 tcg_gen_mov_i64(retv, t1);
900 tcg_temp_free_i64(t1);
903 void tcg_gen_nonatomic_cmpxchg_i64_chk(TCGv_i64 retv, TCGTemp *addr,
904 TCGv_i64 cmpv, TCGv_i64 newv,
905 TCGArg idx, MemOp memop,
908 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
909 tcg_debug_assert((memop & MO_SIZE) <= MO_64);
910 tcg_gen_nonatomic_cmpxchg_i64_int(retv, addr, cmpv, newv, idx, memop);
913 static void tcg_gen_atomic_cmpxchg_i64_int(TCGv_i64 retv, TCGTemp *addr,
914 TCGv_i64 cmpv, TCGv_i64 newv,
915 TCGArg idx, MemOp memop)
917 if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
918 tcg_gen_nonatomic_cmpxchg_i64_int(retv, addr, cmpv, newv, idx, memop);
922 if ((memop & MO_SIZE) == MO_64) {
923 gen_atomic_cx_i64 gen;
925 memop = tcg_canonicalize_memop(memop, 1, 0);
926 gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
928 MemOpIdx oi = make_memop_idx(memop, idx);
929 TCGv_i64 a64 = maybe_extend_addr64(addr);
930 gen(retv, cpu_env, a64, cmpv, newv, tcg_constant_i32(oi));
931 maybe_free_addr64(a64);
935 gen_helper_exit_atomic(cpu_env);
938 * Produce a result for a well-formed opcode stream. This satisfies
939 * liveness for set before used, which happens before this dead code
942 tcg_gen_movi_i64(retv, 0);
946 if (TCG_TARGET_REG_BITS == 32) {
947 tcg_gen_atomic_cmpxchg_i32_int(TCGV_LOW(retv), addr, TCGV_LOW(cmpv),
948 TCGV_LOW(newv), idx, memop);
949 if (memop & MO_SIGN) {
950 tcg_gen_sari_i32(TCGV_HIGH(retv), TCGV_LOW(retv), 31);
952 tcg_gen_movi_i32(TCGV_HIGH(retv), 0);
955 TCGv_i32 c32 = tcg_temp_ebb_new_i32();
956 TCGv_i32 n32 = tcg_temp_ebb_new_i32();
957 TCGv_i32 r32 = tcg_temp_ebb_new_i32();
959 tcg_gen_extrl_i64_i32(c32, cmpv);
960 tcg_gen_extrl_i64_i32(n32, newv);
961 tcg_gen_atomic_cmpxchg_i32_int(r32, addr, c32, n32,
962 idx, memop & ~MO_SIGN);
963 tcg_temp_free_i32(c32);
964 tcg_temp_free_i32(n32);
966 tcg_gen_extu_i32_i64(retv, r32);
967 tcg_temp_free_i32(r32);
969 if (memop & MO_SIGN) {
970 tcg_gen_ext_i64(retv, retv, memop);
975 void tcg_gen_atomic_cmpxchg_i64_chk(TCGv_i64 retv, TCGTemp *addr,
976 TCGv_i64 cmpv, TCGv_i64 newv,
977 TCGArg idx, MemOp memop, TCGType addr_type)
979 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
980 tcg_debug_assert((memop & MO_SIZE) <= MO_64);
981 tcg_gen_atomic_cmpxchg_i64_int(retv, addr, cmpv, newv, idx, memop);
984 static void tcg_gen_nonatomic_cmpxchg_i128_int(TCGv_i128 retv, TCGTemp *addr,
985 TCGv_i128 cmpv, TCGv_i128 newv,
986 TCGArg idx, MemOp memop)
988 if (TCG_TARGET_REG_BITS == 32) {
989 /* Inline expansion below is simply too large for 32-bit hosts. */
990 MemOpIdx oi = make_memop_idx(memop, idx);
991 TCGv_i64 a64 = maybe_extend_addr64(addr);
993 gen_helper_nonatomic_cmpxchgo(retv, cpu_env, a64, cmpv, newv,
994 tcg_constant_i32(oi));
995 maybe_free_addr64(a64);
997 TCGv_i128 oldv = tcg_temp_ebb_new_i128();
998 TCGv_i128 tmpv = tcg_temp_ebb_new_i128();
999 TCGv_i64 t0 = tcg_temp_ebb_new_i64();
1000 TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1001 TCGv_i64 z = tcg_constant_i64(0);
1003 tcg_gen_qemu_ld_i128_int(oldv, addr, idx, memop);
1006 tcg_gen_xor_i64(t0, TCGV128_LOW(oldv), TCGV128_LOW(cmpv));
1007 tcg_gen_xor_i64(t1, TCGV128_HIGH(oldv), TCGV128_HIGH(cmpv));
1008 tcg_gen_or_i64(t0, t0, t1);
1010 /* tmpv = equal ? newv : oldv */
1011 tcg_gen_movcond_i64(TCG_COND_EQ, TCGV128_LOW(tmpv), t0, z,
1012 TCGV128_LOW(newv), TCGV128_LOW(oldv));
1013 tcg_gen_movcond_i64(TCG_COND_EQ, TCGV128_HIGH(tmpv), t0, z,
1014 TCGV128_HIGH(newv), TCGV128_HIGH(oldv));
1016 /* Unconditional writeback. */
1017 tcg_gen_qemu_st_i128_int(tmpv, addr, idx, memop);
1018 tcg_gen_mov_i128(retv, oldv);
1020 tcg_temp_free_i64(t0);
1021 tcg_temp_free_i64(t1);
1022 tcg_temp_free_i128(tmpv);
1023 tcg_temp_free_i128(oldv);
1027 void tcg_gen_nonatomic_cmpxchg_i128_chk(TCGv_i128 retv, TCGTemp *addr,
1028 TCGv_i128 cmpv, TCGv_i128 newv,
1029 TCGArg idx, MemOp memop,
1032 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
1033 tcg_debug_assert((memop & (MO_SIZE | MO_SIGN)) == MO_128);
1034 tcg_gen_nonatomic_cmpxchg_i128_int(retv, addr, cmpv, newv, idx, memop);
1037 static void tcg_gen_atomic_cmpxchg_i128_int(TCGv_i128 retv, TCGTemp *addr,
1038 TCGv_i128 cmpv, TCGv_i128 newv,
1039 TCGArg idx, MemOp memop)
1041 gen_atomic_cx_i128 gen;
1043 if (!(tcg_ctx->gen_tb->cflags & CF_PARALLEL)) {
1044 tcg_gen_nonatomic_cmpxchg_i128_int(retv, addr, cmpv, newv, idx, memop);
1048 gen = table_cmpxchg[memop & (MO_SIZE | MO_BSWAP)];
1050 MemOpIdx oi = make_memop_idx(memop, idx);
1051 TCGv_i64 a64 = maybe_extend_addr64(addr);
1052 gen(retv, cpu_env, a64, cmpv, newv, tcg_constant_i32(oi));
1053 maybe_free_addr64(a64);
1057 gen_helper_exit_atomic(cpu_env);
1060 * Produce a result for a well-formed opcode stream. This satisfies
1061 * liveness for set before used, which happens before this dead code
1064 tcg_gen_movi_i64(TCGV128_LOW(retv), 0);
1065 tcg_gen_movi_i64(TCGV128_HIGH(retv), 0);
1068 void tcg_gen_atomic_cmpxchg_i128_chk(TCGv_i128 retv, TCGTemp *addr,
1069 TCGv_i128 cmpv, TCGv_i128 newv,
1070 TCGArg idx, MemOp memop,
1073 tcg_debug_assert(addr_type == tcg_ctx->addr_type);
1074 tcg_debug_assert((memop & (MO_SIZE | MO_SIGN)) == MO_128);
1075 tcg_gen_atomic_cmpxchg_i128_int(retv, addr, cmpv, newv, idx, memop);
1078 static void do_nonatomic_op_i32(TCGv_i32 ret, TCGTemp *addr, TCGv_i32 val,
1079 TCGArg idx, MemOp memop, bool new_val,
1080 void (*gen)(TCGv_i32, TCGv_i32, TCGv_i32))
1082 TCGv_i32 t1 = tcg_temp_ebb_new_i32();
1083 TCGv_i32 t2 = tcg_temp_ebb_new_i32();
1085 memop = tcg_canonicalize_memop(memop, 0, 0);
1087 tcg_gen_qemu_ld_i32_int(t1, addr, idx, memop);
1088 tcg_gen_ext_i32(t2, val, memop);
1090 tcg_gen_qemu_st_i32_int(t2, addr, idx, memop);
1092 tcg_gen_ext_i32(ret, (new_val ? t2 : t1), memop);
1093 tcg_temp_free_i32(t1);
1094 tcg_temp_free_i32(t2);
1097 static void do_atomic_op_i32(TCGv_i32 ret, TCGTemp *addr, TCGv_i32 val,
1098 TCGArg idx, MemOp memop, void * const table[])
1100 gen_atomic_op_i32 gen;
1104 memop = tcg_canonicalize_memop(memop, 0, 0);
1106 gen = table[memop & (MO_SIZE | MO_BSWAP)];
1107 tcg_debug_assert(gen != NULL);
1109 oi = make_memop_idx(memop & ~MO_SIGN, idx);
1110 a64 = maybe_extend_addr64(addr);
1111 gen(ret, cpu_env, a64, val, tcg_constant_i32(oi));
1112 maybe_free_addr64(a64);
1114 if (memop & MO_SIGN) {
1115 tcg_gen_ext_i32(ret, ret, memop);
1119 static void do_nonatomic_op_i64(TCGv_i64 ret, TCGTemp *addr, TCGv_i64 val,
1120 TCGArg idx, MemOp memop, bool new_val,
1121 void (*gen)(TCGv_i64, TCGv_i64, TCGv_i64))
1123 TCGv_i64 t1 = tcg_temp_ebb_new_i64();
1124 TCGv_i64 t2 = tcg_temp_ebb_new_i64();
1126 memop = tcg_canonicalize_memop(memop, 1, 0);
1128 tcg_gen_qemu_ld_i64_int(t1, addr, idx, memop);
1129 tcg_gen_ext_i64(t2, val, memop);
1131 tcg_gen_qemu_st_i64_int(t2, addr, idx, memop);
1133 tcg_gen_ext_i64(ret, (new_val ? t2 : t1), memop);
1134 tcg_temp_free_i64(t1);
1135 tcg_temp_free_i64(t2);
1138 static void do_atomic_op_i64(TCGv_i64 ret, TCGTemp *addr, TCGv_i64 val,
1139 TCGArg idx, MemOp memop, void * const table[])
1141 memop = tcg_canonicalize_memop(memop, 1, 0);
1143 if ((memop & MO_SIZE) == MO_64) {
1144 gen_atomic_op_i64 gen = table[memop & (MO_SIZE | MO_BSWAP)];
1147 MemOpIdx oi = make_memop_idx(memop & ~MO_SIGN, idx);
1148 TCGv_i64 a64 = maybe_extend_addr64(addr);
1149 gen(ret, cpu_env, a64, val, tcg_constant_i32(oi));
1150 maybe_free_addr64(a64);
1154 gen_helper_exit_atomic(cpu_env);
1155 /* Produce a result, so that we have a well-formed opcode stream
1156 with respect to uses of the result in the (dead) code following. */
1157 tcg_gen_movi_i64(ret, 0);
1159 TCGv_i32 v32 = tcg_temp_ebb_new_i32();
1160 TCGv_i32 r32 = tcg_temp_ebb_new_i32();
1162 tcg_gen_extrl_i64_i32(v32, val);
1163 do_atomic_op_i32(r32, addr, v32, idx, memop & ~MO_SIGN, table);
1164 tcg_temp_free_i32(v32);
1166 tcg_gen_extu_i32_i64(ret, r32);
1167 tcg_temp_free_i32(r32);
1169 if (memop & MO_SIGN) {
1170 tcg_gen_ext_i64(ret, ret, memop);
1175 #define GEN_ATOMIC_HELPER(NAME, OP, NEW) \
1176 static void * const table_##NAME[(MO_SIZE | MO_BSWAP) + 1] = { \
1177 [MO_8] = gen_helper_atomic_##NAME##b, \
1178 [MO_16 | MO_LE] = gen_helper_atomic_##NAME##w_le, \
1179 [MO_16 | MO_BE] = gen_helper_atomic_##NAME##w_be, \
1180 [MO_32 | MO_LE] = gen_helper_atomic_##NAME##l_le, \
1181 [MO_32 | MO_BE] = gen_helper_atomic_##NAME##l_be, \
1182 WITH_ATOMIC64([MO_64 | MO_LE] = gen_helper_atomic_##NAME##q_le) \
1183 WITH_ATOMIC64([MO_64 | MO_BE] = gen_helper_atomic_##NAME##q_be) \
1185 void tcg_gen_atomic_##NAME##_i32_chk(TCGv_i32 ret, TCGTemp *addr, \
1186 TCGv_i32 val, TCGArg idx, \
1187 MemOp memop, TCGType addr_type) \
1189 tcg_debug_assert(addr_type == tcg_ctx->addr_type); \
1190 tcg_debug_assert((memop & MO_SIZE) <= MO_32); \
1191 if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) { \
1192 do_atomic_op_i32(ret, addr, val, idx, memop, table_##NAME); \
1194 do_nonatomic_op_i32(ret, addr, val, idx, memop, NEW, \
1195 tcg_gen_##OP##_i32); \
1198 void tcg_gen_atomic_##NAME##_i64_chk(TCGv_i64 ret, TCGTemp *addr, \
1199 TCGv_i64 val, TCGArg idx, \
1200 MemOp memop, TCGType addr_type) \
1202 tcg_debug_assert(addr_type == tcg_ctx->addr_type); \
1203 tcg_debug_assert((memop & MO_SIZE) <= MO_64); \
1204 if (tcg_ctx->gen_tb->cflags & CF_PARALLEL) { \
1205 do_atomic_op_i64(ret, addr, val, idx, memop, table_##NAME); \
1207 do_nonatomic_op_i64(ret, addr, val, idx, memop, NEW, \
1208 tcg_gen_##OP##_i64); \
1212 GEN_ATOMIC_HELPER(fetch_add, add, 0)
1213 GEN_ATOMIC_HELPER(fetch_and, and, 0)
1214 GEN_ATOMIC_HELPER(fetch_or, or, 0)
1215 GEN_ATOMIC_HELPER(fetch_xor, xor, 0)
1216 GEN_ATOMIC_HELPER(fetch_smin, smin, 0)
1217 GEN_ATOMIC_HELPER(fetch_umin, umin, 0)
1218 GEN_ATOMIC_HELPER(fetch_smax, smax, 0)
1219 GEN_ATOMIC_HELPER(fetch_umax, umax, 0)
1221 GEN_ATOMIC_HELPER(add_fetch, add, 1)
1222 GEN_ATOMIC_HELPER(and_fetch, and, 1)
1223 GEN_ATOMIC_HELPER(or_fetch, or, 1)
1224 GEN_ATOMIC_HELPER(xor_fetch, xor, 1)
1225 GEN_ATOMIC_HELPER(smin_fetch, smin, 1)
1226 GEN_ATOMIC_HELPER(umin_fetch, umin, 1)
1227 GEN_ATOMIC_HELPER(smax_fetch, smax, 1)
1228 GEN_ATOMIC_HELPER(umax_fetch, umax, 1)
1230 static void tcg_gen_mov2_i32(TCGv_i32 r, TCGv_i32 a, TCGv_i32 b)
1232 tcg_gen_mov_i32(r, b);
1235 static void tcg_gen_mov2_i64(TCGv_i64 r, TCGv_i64 a, TCGv_i64 b)
1237 tcg_gen_mov_i64(r, b);
1240 GEN_ATOMIC_HELPER(xchg, mov2, 0)
1242 #undef GEN_ATOMIC_HELPER