]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/tcg.c
tcg: Widen helper_atomic_* addresses to uint64_t
[mirror_qemu.git] / tcg / tcg.c
CommitLineData
c896fe29
FB
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
c896fe29 25/* define it to use liveness analysis (better code) */
8f2e8c07 26#define USE_TCG_OPTIMIZATIONS
c896fe29 27
757e725b 28#include "qemu/osdep.h"
cca82982 29
813da627
RH
30/* Define to jump the ELF file used to communicate with GDB. */
31#undef DEBUG_JIT
32
72fd2efb 33#include "qemu/error-report.h"
f348b6d1 34#include "qemu/cutils.h"
1de7afc9 35#include "qemu/host-utils.h"
d4c51a0a 36#include "qemu/qemu-print.h"
084cfca1 37#include "qemu/cacheflush.h"
ad768e6f 38#include "qemu/cacheinfo.h"
533206f0 39#include "qemu/timer.h"
c896fe29 40
c5d3c498 41/* Note: the long term plan is to reduce the dependencies on the QEMU
c896fe29
FB
42 CPU definitions. Currently they are used for qemu_ld/st
43 instructions */
44#define NO_CPU_IO_DEFS
c896fe29 45
63c91552 46#include "exec/exec-all.h"
dcb32f1d 47#include "tcg/tcg-op.h"
813da627 48
edee2579 49#if UINTPTR_MAX == UINT32_MAX
813da627 50# define ELF_CLASS ELFCLASS32
edee2579
RH
51#else
52# define ELF_CLASS ELFCLASS64
813da627 53#endif
e03b5686 54#if HOST_BIG_ENDIAN
813da627
RH
55# define ELF_DATA ELFDATA2MSB
56#else
57# define ELF_DATA ELFDATA2LSB
58#endif
59
c896fe29 60#include "elf.h"
508127e2 61#include "exec/log.h"
d2ba8026 62#include "tcg/tcg-ldst.h"
47f7313d 63#include "tcg/tcg-temp-internal.h"
5ff7258c 64#include "tcg-internal.h"
5584e2db 65#include "accel/tcg/perf.h"
c896fe29 66
139c1837 67/* Forward declarations for functions declared in tcg-target.c.inc and
ce151109 68 used here. */
e4d58b41
RH
69static void tcg_target_init(TCGContext *s);
70static void tcg_target_qemu_prologue(TCGContext *s);
6ac17786 71static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
2ba7fae2 72 intptr_t value, intptr_t addend);
c896fe29 73
497a22eb
RH
74/* The CIE and FDE header definitions will be common to all hosts. */
75typedef struct {
76 uint32_t len __attribute__((aligned((sizeof(void *)))));
77 uint32_t id;
78 uint8_t version;
79 char augmentation[1];
80 uint8_t code_align;
81 uint8_t data_align;
82 uint8_t return_column;
83} DebugFrameCIE;
84
85typedef struct QEMU_PACKED {
86 uint32_t len __attribute__((aligned((sizeof(void *)))));
87 uint32_t cie_offset;
edee2579
RH
88 uintptr_t func_start;
89 uintptr_t func_len;
497a22eb
RH
90} DebugFrameFDEHeader;
91
2c90784a
RH
92typedef struct QEMU_PACKED {
93 DebugFrameCIE cie;
94 DebugFrameFDEHeader fde;
95} DebugFrameHeader;
96
2528f771
RH
97typedef struct TCGLabelQemuLdst {
98 bool is_ld; /* qemu_ld: true, qemu_st: false */
99 MemOpIdx oi;
100 TCGType type; /* result type of a load */
101 TCGReg addrlo_reg; /* reg index for low word of guest virtual addr */
102 TCGReg addrhi_reg; /* reg index for high word of guest virtual addr */
103 TCGReg datalo_reg; /* reg index for low word to be loaded or stored */
104 TCGReg datahi_reg; /* reg index for high word to be loaded or stored */
105 const tcg_insn_unit *raddr; /* addr of the next IR of qemu_ld/st IR */
106 tcg_insn_unit *label_ptr[2]; /* label pointers to be updated */
107 QSIMPLEQ_ENTRY(TCGLabelQemuLdst) next;
108} TCGLabelQemuLdst;
109
755bf9e5 110static void tcg_register_jit_int(const void *buf, size_t size,
2c90784a
RH
111 const void *debug_frame,
112 size_t debug_frame_size)
813da627
RH
113 __attribute__((unused));
114
139c1837 115/* Forward declarations for functions declared and used in tcg-target.c.inc. */
2a534aff 116static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
a05b5b9b 117 intptr_t arg2);
78113e83 118static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
c0ad3001 119static void tcg_out_movi(TCGContext *s, TCGType type,
2a534aff 120 TCGReg ret, tcg_target_long arg);
678155b2 121static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
753e42ea 122static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
d0e66c89 123static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg);
379afdff 124static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg);
52bf3398 125static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg);
9ecf5f61 126static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg);
9c6aa274 127static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg);
b9bfe000 128static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg);
b8b94ac6 129static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg);
313bdea8 130static void tcg_out_addi_ptr(TCGContext *s, TCGReg, TCGReg, tcg_target_long);
129f1f9e 131static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2);
b55a8d9d 132static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg);
cf7d6b8e 133static void tcg_out_goto_tb(TCGContext *s, int which);
5e8892db
MR
134static void tcg_out_op(TCGContext *s, TCGOpcode opc,
135 const TCGArg args[TCG_MAX_OP_ARGS],
136 const int const_args[TCG_MAX_OP_ARGS]);
d2fd745f 137#if TCG_TARGET_MAYBE_vec
e7632cfa
RH
138static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
139 TCGReg dst, TCGReg src);
d6ecb4a9
RH
140static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
141 TCGReg dst, TCGReg base, intptr_t offset);
4e186175
RH
142static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
143 TCGReg dst, int64_t arg);
5e8892db
MR
144static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
145 unsigned vecl, unsigned vece,
146 const TCGArg args[TCG_MAX_OP_ARGS],
147 const int const_args[TCG_MAX_OP_ARGS]);
d2fd745f 148#else
e7632cfa
RH
149static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
150 TCGReg dst, TCGReg src)
151{
152 g_assert_not_reached();
153}
d6ecb4a9
RH
154static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
155 TCGReg dst, TCGReg base, intptr_t offset)
156{
157 g_assert_not_reached();
158}
4e186175
RH
159static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
160 TCGReg dst, int64_t arg)
e7632cfa
RH
161{
162 g_assert_not_reached();
163}
5e8892db
MR
164static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
165 unsigned vecl, unsigned vece,
166 const TCGArg args[TCG_MAX_OP_ARGS],
167 const int const_args[TCG_MAX_OP_ARGS])
d2fd745f
RH
168{
169 g_assert_not_reached();
170}
171#endif
2a534aff 172static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
a05b5b9b 173 intptr_t arg2);
59d7c14e
RH
174static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
175 TCGReg base, intptr_t ofs);
7b7d8b2d 176static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target,
cee44b03 177 const TCGHelperInfo *info);
5e3d0c19 178static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot);
a4fbbd77 179static bool tcg_target_const_match(int64_t val, TCGType type, int ct);
659ef5cb 180#ifdef TCG_TARGET_NEED_LDST_LABELS
aeee05f5 181static int tcg_out_ldst_finalize(TCGContext *s);
659ef5cb 182#endif
c896fe29 183
8429a1ca
RH
184typedef struct TCGLdstHelperParam {
185 TCGReg (*ra_gen)(TCGContext *s, const TCGLabelQemuLdst *l, int arg_reg);
186 unsigned ntmp;
187 int tmp[3];
188} TCGLdstHelperParam;
189
190static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *l,
191 const TCGLdstHelperParam *p)
192 __attribute__((unused));
193static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *l,
194 bool load_sign, const TCGLdstHelperParam *p)
195 __attribute__((unused));
196static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *l,
197 const TCGLdstHelperParam *p)
198 __attribute__((unused));
199
de95016d 200static void * const qemu_ld_helpers[MO_SSIZE + 1] __attribute__((unused)) = {
0cadc1ed
RH
201 [MO_UB] = helper_ldub_mmu,
202 [MO_SB] = helper_ldsb_mmu,
203 [MO_UW] = helper_lduw_mmu,
204 [MO_SW] = helper_ldsw_mmu,
205 [MO_UL] = helper_ldul_mmu,
206 [MO_UQ] = helper_ldq_mmu,
207#if TCG_TARGET_REG_BITS == 64
208 [MO_SL] = helper_ldsl_mmu,
ebebea53 209 [MO_128] = helper_ld16_mmu,
0cadc1ed
RH
210#endif
211};
212
de95016d 213static void * const qemu_st_helpers[MO_SIZE + 1] __attribute__((unused)) = {
0cadc1ed
RH
214 [MO_8] = helper_stb_mmu,
215 [MO_16] = helper_stw_mmu,
216 [MO_32] = helper_stl_mmu,
217 [MO_64] = helper_stq_mmu,
ebebea53
RH
218#if TCG_TARGET_REG_BITS == 64
219 [MO_128] = helper_st16_mmu,
220#endif
0cadc1ed 221};
0cadc1ed 222
e63b8a29
RH
223typedef struct {
224 MemOp atom; /* lg2 bits of atomicity required */
225 MemOp align; /* lg2 bits of alignment to use */
226} TCGAtomAlign;
227
228static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc,
229 MemOp host_atom, bool allow_two_ops)
230 __attribute__((unused));
231
42eb6dfc
RH
232TCGContext tcg_init_ctx;
233__thread TCGContext *tcg_ctx;
234
5ff7258c 235TCGContext **tcg_ctxs;
0e2d61cf
RH
236unsigned int tcg_cur_ctxs;
237unsigned int tcg_max_ctxs;
1c2adb95 238TCGv_env cpu_env = 0;
c8bc1168 239const void *tcg_code_gen_epilogue;
db0c51a3 240uintptr_t tcg_splitwx_diff;
df2cce29 241
b91ccb31
RH
242#ifndef CONFIG_TCG_INTERPRETER
243tcg_prologue_fn *tcg_qemu_tb_exec;
244#endif
245
d2fd745f 246static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
b1d8e52e 247static TCGRegSet tcg_target_call_clobber_regs;
c896fe29 248
1813e175 249#if TCG_TARGET_INSN_UNIT_SIZE == 1
4196dca6 250static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
c896fe29
FB
251{
252 *s->code_ptr++ = v;
253}
254
4196dca6
PM
255static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
256 uint8_t v)
5c53bb81 257{
1813e175 258 *p = v;
5c53bb81 259}
1813e175 260#endif
5c53bb81 261
1813e175 262#if TCG_TARGET_INSN_UNIT_SIZE <= 2
4196dca6 263static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
c896fe29 264{
1813e175
RH
265 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
266 *s->code_ptr++ = v;
267 } else {
268 tcg_insn_unit *p = s->code_ptr;
269 memcpy(p, &v, sizeof(v));
270 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
271 }
c896fe29
FB
272}
273
4196dca6
PM
274static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
275 uint16_t v)
5c53bb81 276{
1813e175
RH
277 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
278 *p = v;
279 } else {
280 memcpy(p, &v, sizeof(v));
281 }
5c53bb81 282}
1813e175 283#endif
5c53bb81 284
1813e175 285#if TCG_TARGET_INSN_UNIT_SIZE <= 4
4196dca6 286static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
c896fe29 287{
1813e175
RH
288 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
289 *s->code_ptr++ = v;
290 } else {
291 tcg_insn_unit *p = s->code_ptr;
292 memcpy(p, &v, sizeof(v));
293 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
294 }
c896fe29
FB
295}
296
4196dca6
PM
297static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
298 uint32_t v)
5c53bb81 299{
1813e175
RH
300 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
301 *p = v;
302 } else {
303 memcpy(p, &v, sizeof(v));
304 }
5c53bb81 305}
1813e175 306#endif
5c53bb81 307
1813e175 308#if TCG_TARGET_INSN_UNIT_SIZE <= 8
4196dca6 309static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
ac26eb69 310{
1813e175
RH
311 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
312 *s->code_ptr++ = v;
313 } else {
314 tcg_insn_unit *p = s->code_ptr;
315 memcpy(p, &v, sizeof(v));
316 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
317 }
ac26eb69
RH
318}
319
4196dca6
PM
320static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
321 uint64_t v)
5c53bb81 322{
1813e175
RH
323 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
324 *p = v;
325 } else {
326 memcpy(p, &v, sizeof(v));
327 }
5c53bb81 328}
1813e175 329#endif
5c53bb81 330
c896fe29
FB
331/* label relocation processing */
332
1813e175 333static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
bec16311 334 TCGLabel *l, intptr_t addend)
c896fe29 335{
7ecd02a0 336 TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
c896fe29 337
7ecd02a0
RH
338 r->type = type;
339 r->ptr = code_ptr;
340 r->addend = addend;
341 QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
c896fe29
FB
342}
343
92ab8e7d 344static void tcg_out_label(TCGContext *s, TCGLabel *l)
c896fe29 345{
eabb7b91 346 tcg_debug_assert(!l->has_value);
c896fe29 347 l->has_value = 1;
92ab8e7d 348 l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
c896fe29
FB
349}
350
42a268c2 351TCGLabel *gen_new_label(void)
c896fe29 352{
b1311c4a 353 TCGContext *s = tcg_ctx;
51e3972c 354 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
c896fe29 355
7ecd02a0
RH
356 memset(l, 0, sizeof(TCGLabel));
357 l->id = s->nb_labels++;
f85b1fc4 358 QSIMPLEQ_INIT(&l->branches);
7ecd02a0
RH
359 QSIMPLEQ_INIT(&l->relocs);
360
bef16ab4 361 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
42a268c2
RH
362
363 return l;
c896fe29
FB
364}
365
7ecd02a0
RH
366static bool tcg_resolve_relocs(TCGContext *s)
367{
368 TCGLabel *l;
369
370 QSIMPLEQ_FOREACH(l, &s->labels, next) {
371 TCGRelocation *r;
372 uintptr_t value = l->u.value;
373
374 QSIMPLEQ_FOREACH(r, &l->relocs, next) {
375 if (!patch_reloc(r->ptr, r->type, value, r->addend)) {
376 return false;
377 }
378 }
379 }
380 return true;
381}
382
9f754620
RH
383static void set_jmp_reset_offset(TCGContext *s, int which)
384{
f14bed3f
RH
385 /*
386 * We will check for overflow at the end of the opcode loop in
387 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
388 */
b7e4afbd 389 s->gen_tb->jmp_reset_offset[which] = tcg_current_code_size(s);
9f754620
RH
390}
391
b52a2c03
RH
392static void G_GNUC_UNUSED set_jmp_insn_offset(TCGContext *s, int which)
393{
394 /*
395 * We will check for overflow at the end of the opcode loop in
396 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
397 */
9da6079b 398 s->gen_tb->jmp_insn_offset[which] = tcg_current_code_size(s);
b52a2c03
RH
399}
400
becc452a
RH
401static uintptr_t G_GNUC_UNUSED get_jmp_target_addr(TCGContext *s, int which)
402{
403 /*
404 * Return the read-execute version of the pointer, for the benefit
405 * of any pc-relative addressing mode.
406 */
9da6079b 407 return (uintptr_t)tcg_splitwx_to_rx(&s->gen_tb->jmp_target_addr[which]);
becc452a
RH
408}
409
db6b7d0c 410/* Signal overflow, starting over with fewer guest insns. */
8905770b
MAL
411static G_NORETURN
412void tcg_raise_tb_overflow(TCGContext *s)
db6b7d0c
RH
413{
414 siglongjmp(s->jmp_trans, -2);
415}
416
8429a1ca
RH
417/*
418 * Used by tcg_out_movext{1,2} to hold the arguments for tcg_out_movext.
419 * By the time we arrive at tcg_out_movext1, @dst is always a TCGReg.
420 *
421 * However, tcg_out_helper_load_slots reuses this field to hold an
422 * argument slot number (which may designate a argument register or an
423 * argument stack slot), converting to TCGReg once all arguments that
424 * are destined for the stack are processed.
425 */
129f1f9e 426typedef struct TCGMovExtend {
8429a1ca 427 unsigned dst;
129f1f9e
RH
428 TCGReg src;
429 TCGType dst_type;
430 TCGType src_type;
431 MemOp src_ext;
432} TCGMovExtend;
433
b3dfd5fc
RH
434/**
435 * tcg_out_movext -- move and extend
436 * @s: tcg context
437 * @dst_type: integral type for destination
438 * @dst: destination register
439 * @src_type: integral type for source
440 * @src_ext: extension to apply to source
441 * @src: source register
442 *
443 * Move or extend @src into @dst, depending on @src_ext and the types.
444 */
129f1f9e
RH
445static void tcg_out_movext(TCGContext *s, TCGType dst_type, TCGReg dst,
446 TCGType src_type, MemOp src_ext, TCGReg src)
b3dfd5fc
RH
447{
448 switch (src_ext) {
449 case MO_UB:
450 tcg_out_ext8u(s, dst, src);
451 break;
452 case MO_SB:
453 tcg_out_ext8s(s, dst_type, dst, src);
454 break;
455 case MO_UW:
456 tcg_out_ext16u(s, dst, src);
457 break;
458 case MO_SW:
459 tcg_out_ext16s(s, dst_type, dst, src);
460 break;
461 case MO_UL:
462 case MO_SL:
463 if (dst_type == TCG_TYPE_I32) {
464 if (src_type == TCG_TYPE_I32) {
465 tcg_out_mov(s, TCG_TYPE_I32, dst, src);
466 } else {
467 tcg_out_extrl_i64_i32(s, dst, src);
468 }
469 } else if (src_type == TCG_TYPE_I32) {
470 if (src_ext & MO_SIGN) {
471 tcg_out_exts_i32_i64(s, dst, src);
472 } else {
473 tcg_out_extu_i32_i64(s, dst, src);
474 }
475 } else {
476 if (src_ext & MO_SIGN) {
477 tcg_out_ext32s(s, dst, src);
478 } else {
479 tcg_out_ext32u(s, dst, src);
480 }
481 }
482 break;
483 case MO_UQ:
484 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
485 if (dst_type == TCG_TYPE_I32) {
486 tcg_out_extrl_i64_i32(s, dst, src);
487 } else {
488 tcg_out_mov(s, TCG_TYPE_I64, dst, src);
489 }
490 break;
491 default:
492 g_assert_not_reached();
493 }
494}
495
129f1f9e
RH
496/* Minor variations on a theme, using a structure. */
497static void tcg_out_movext1_new_src(TCGContext *s, const TCGMovExtend *i,
498 TCGReg src)
499{
500 tcg_out_movext(s, i->dst_type, i->dst, i->src_type, i->src_ext, src);
501}
502
503static void tcg_out_movext1(TCGContext *s, const TCGMovExtend *i)
504{
505 tcg_out_movext1_new_src(s, i, i->src);
506}
507
508/**
509 * tcg_out_movext2 -- move and extend two pair
510 * @s: tcg context
511 * @i1: first move description
512 * @i2: second move description
513 * @scratch: temporary register, or -1 for none
514 *
515 * As tcg_out_movext, for both @i1 and @i2, caring for overlap
516 * between the sources and destinations.
517 */
518
8429a1ca
RH
519static void tcg_out_movext2(TCGContext *s, const TCGMovExtend *i1,
520 const TCGMovExtend *i2, int scratch)
129f1f9e
RH
521{
522 TCGReg src1 = i1->src;
523 TCGReg src2 = i2->src;
524
525 if (i1->dst != src2) {
526 tcg_out_movext1(s, i1);
527 tcg_out_movext1(s, i2);
528 return;
529 }
530 if (i2->dst == src1) {
531 TCGType src1_type = i1->src_type;
532 TCGType src2_type = i2->src_type;
533
534 if (tcg_out_xchg(s, MAX(src1_type, src2_type), src1, src2)) {
535 /* The data is now in the correct registers, now extend. */
536 src1 = i2->src;
537 src2 = i1->src;
538 } else {
539 tcg_debug_assert(scratch >= 0);
540 tcg_out_mov(s, src1_type, scratch, src1);
541 src1 = scratch;
542 }
543 }
544 tcg_out_movext1_new_src(s, i2, src2);
545 tcg_out_movext1_new_src(s, i1, src1);
546}
547
2462e30e
RH
548/**
549 * tcg_out_movext3 -- move and extend three pair
550 * @s: tcg context
551 * @i1: first move description
552 * @i2: second move description
553 * @i3: third move description
554 * @scratch: temporary register, or -1 for none
555 *
556 * As tcg_out_movext, for all of @i1, @i2 and @i3, caring for overlap
557 * between the sources and destinations.
558 */
559
560static void tcg_out_movext3(TCGContext *s, const TCGMovExtend *i1,
561 const TCGMovExtend *i2, const TCGMovExtend *i3,
562 int scratch)
563{
564 TCGReg src1 = i1->src;
565 TCGReg src2 = i2->src;
566 TCGReg src3 = i3->src;
567
568 if (i1->dst != src2 && i1->dst != src3) {
569 tcg_out_movext1(s, i1);
570 tcg_out_movext2(s, i2, i3, scratch);
571 return;
572 }
573 if (i2->dst != src1 && i2->dst != src3) {
574 tcg_out_movext1(s, i2);
575 tcg_out_movext2(s, i1, i3, scratch);
576 return;
577 }
578 if (i3->dst != src1 && i3->dst != src2) {
579 tcg_out_movext1(s, i3);
580 tcg_out_movext2(s, i1, i2, scratch);
581 return;
582 }
583
584 /*
585 * There is a cycle. Since there are only 3 nodes, the cycle is
586 * either "clockwise" or "anti-clockwise", and can be solved with
587 * a single scratch or two xchg.
588 */
589 if (i1->dst == src2 && i2->dst == src3 && i3->dst == src1) {
590 /* "Clockwise" */
591 if (tcg_out_xchg(s, MAX(i1->src_type, i2->src_type), src1, src2)) {
592 tcg_out_xchg(s, MAX(i2->src_type, i3->src_type), src2, src3);
593 /* The data is now in the correct registers, now extend. */
594 tcg_out_movext1_new_src(s, i1, i1->dst);
595 tcg_out_movext1_new_src(s, i2, i2->dst);
596 tcg_out_movext1_new_src(s, i3, i3->dst);
597 } else {
598 tcg_debug_assert(scratch >= 0);
599 tcg_out_mov(s, i1->src_type, scratch, src1);
600 tcg_out_movext1(s, i3);
601 tcg_out_movext1(s, i2);
602 tcg_out_movext1_new_src(s, i1, scratch);
603 }
604 } else if (i1->dst == src3 && i2->dst == src1 && i3->dst == src2) {
605 /* "Anti-clockwise" */
606 if (tcg_out_xchg(s, MAX(i2->src_type, i3->src_type), src2, src3)) {
607 tcg_out_xchg(s, MAX(i1->src_type, i2->src_type), src1, src2);
608 /* The data is now in the correct registers, now extend. */
609 tcg_out_movext1_new_src(s, i1, i1->dst);
610 tcg_out_movext1_new_src(s, i2, i2->dst);
611 tcg_out_movext1_new_src(s, i3, i3->dst);
612 } else {
613 tcg_debug_assert(scratch >= 0);
614 tcg_out_mov(s, i1->src_type, scratch, src1);
615 tcg_out_movext1(s, i2);
616 tcg_out_movext1(s, i3);
617 tcg_out_movext1_new_src(s, i1, scratch);
618 }
619 } else {
620 g_assert_not_reached();
621 }
622}
623
4c22e840
RH
624#define C_PFX1(P, A) P##A
625#define C_PFX2(P, A, B) P##A##_##B
626#define C_PFX3(P, A, B, C) P##A##_##B##_##C
627#define C_PFX4(P, A, B, C, D) P##A##_##B##_##C##_##D
628#define C_PFX5(P, A, B, C, D, E) P##A##_##B##_##C##_##D##_##E
629#define C_PFX6(P, A, B, C, D, E, F) P##A##_##B##_##C##_##D##_##E##_##F
630
631/* Define an enumeration for the various combinations. */
632
633#define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1),
634#define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2),
635#define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3),
636#define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4),
637
638#define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1),
639#define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2),
640#define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3),
641#define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4),
642
643#define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2),
644
645#define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1),
646#define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2),
647#define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3),
648#define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4),
649
650typedef enum {
651#include "tcg-target-con-set.h"
652} TCGConstraintSetIndex;
653
654static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode);
655
656#undef C_O0_I1
657#undef C_O0_I2
658#undef C_O0_I3
659#undef C_O0_I4
660#undef C_O1_I1
661#undef C_O1_I2
662#undef C_O1_I3
663#undef C_O1_I4
664#undef C_N1_I2
665#undef C_O2_I1
666#undef C_O2_I2
667#undef C_O2_I3
668#undef C_O2_I4
669
670/* Put all of the constraint sets into an array, indexed by the enum. */
671
672#define C_O0_I1(I1) { .args_ct_str = { #I1 } },
673#define C_O0_I2(I1, I2) { .args_ct_str = { #I1, #I2 } },
674#define C_O0_I3(I1, I2, I3) { .args_ct_str = { #I1, #I2, #I3 } },
675#define C_O0_I4(I1, I2, I3, I4) { .args_ct_str = { #I1, #I2, #I3, #I4 } },
676
677#define C_O1_I1(O1, I1) { .args_ct_str = { #O1, #I1 } },
678#define C_O1_I2(O1, I1, I2) { .args_ct_str = { #O1, #I1, #I2 } },
679#define C_O1_I3(O1, I1, I2, I3) { .args_ct_str = { #O1, #I1, #I2, #I3 } },
680#define C_O1_I4(O1, I1, I2, I3, I4) { .args_ct_str = { #O1, #I1, #I2, #I3, #I4 } },
681
682#define C_N1_I2(O1, I1, I2) { .args_ct_str = { "&" #O1, #I1, #I2 } },
683
684#define C_O2_I1(O1, O2, I1) { .args_ct_str = { #O1, #O2, #I1 } },
685#define C_O2_I2(O1, O2, I1, I2) { .args_ct_str = { #O1, #O2, #I1, #I2 } },
686#define C_O2_I3(O1, O2, I1, I2, I3) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3 } },
687#define C_O2_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3, #I4 } },
688
689static const TCGTargetOpDef constraint_sets[] = {
690#include "tcg-target-con-set.h"
691};
692
693
694#undef C_O0_I1
695#undef C_O0_I2
696#undef C_O0_I3
697#undef C_O0_I4
698#undef C_O1_I1
699#undef C_O1_I2
700#undef C_O1_I3
701#undef C_O1_I4
702#undef C_N1_I2
703#undef C_O2_I1
704#undef C_O2_I2
705#undef C_O2_I3
706#undef C_O2_I4
707
708/* Expand the enumerator to be returned from tcg_target_op_def(). */
709
710#define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1)
711#define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2)
712#define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3)
713#define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4)
714
715#define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1)
716#define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2)
717#define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3)
718#define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4)
719
720#define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2)
721
722#define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1)
723#define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2)
724#define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3)
725#define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4)
726
139c1837 727#include "tcg-target.c.inc"
c896fe29 728
38b47b19
EC
729static void alloc_tcg_plugin_context(TCGContext *s)
730{
731#ifdef CONFIG_PLUGIN
732 s->plugin_tb = g_new0(struct qemu_plugin_tb, 1);
733 s->plugin_tb->insns =
734 g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn);
735#endif
736}
737
3468b59e
EC
738/*
739 * All TCG threads except the parent (i.e. the one that called tcg_context_init
740 * and registered the target's TCG globals) must register with this function
741 * before initiating translation.
742 *
743 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
744 * of tcg_region_init() for the reasoning behind this.
745 *
746 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
747 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
748 * is not used anymore for translation once this function is called.
749 *
750 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
751 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
752 */
753#ifdef CONFIG_USER_ONLY
754void tcg_register_thread(void)
755{
756 tcg_ctx = &tcg_init_ctx;
757}
758#else
759void tcg_register_thread(void)
760{
761 TCGContext *s = g_malloc(sizeof(*s));
762 unsigned int i, n;
3468b59e
EC
763
764 *s = tcg_init_ctx;
765
766 /* Relink mem_base. */
767 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
768 if (tcg_init_ctx.temps[i].mem_base) {
769 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
770 tcg_debug_assert(b >= 0 && b < n);
771 s->temps[i].mem_base = &s->temps[b];
772 }
773 }
774
775 /* Claim an entry in tcg_ctxs */
0e2d61cf
RH
776 n = qatomic_fetch_inc(&tcg_cur_ctxs);
777 g_assert(n < tcg_max_ctxs);
d73415a3 778 qatomic_set(&tcg_ctxs[n], s);
3468b59e 779
38b47b19
EC
780 if (n > 0) {
781 alloc_tcg_plugin_context(s);
bf042e8e 782 tcg_region_initial_alloc(s);
38b47b19
EC
783 }
784
3468b59e 785 tcg_ctx = s;
e8feb96f 786}
3468b59e 787#endif /* !CONFIG_USER_ONLY */
e8feb96f 788
c896fe29
FB
789/* pool based memory allocation */
790void *tcg_malloc_internal(TCGContext *s, int size)
791{
792 TCGPool *p;
793 int pool_size;
a813e36f 794
c896fe29
FB
795 if (size > TCG_POOL_CHUNK_SIZE) {
796 /* big malloc: insert a new pool (XXX: could optimize) */
7267c094 797 p = g_malloc(sizeof(TCGPool) + size);
c896fe29 798 p->size = size;
4055299e
KB
799 p->next = s->pool_first_large;
800 s->pool_first_large = p;
801 return p->data;
c896fe29
FB
802 } else {
803 p = s->pool_current;
804 if (!p) {
805 p = s->pool_first;
806 if (!p)
807 goto new_pool;
808 } else {
809 if (!p->next) {
810 new_pool:
811 pool_size = TCG_POOL_CHUNK_SIZE;
7267c094 812 p = g_malloc(sizeof(TCGPool) + pool_size);
c896fe29
FB
813 p->size = pool_size;
814 p->next = NULL;
a813e36f 815 if (s->pool_current) {
c896fe29 816 s->pool_current->next = p;
a813e36f 817 } else {
c896fe29 818 s->pool_first = p;
a813e36f 819 }
c896fe29
FB
820 } else {
821 p = p->next;
822 }
823 }
824 }
825 s->pool_current = p;
826 s->pool_cur = p->data + size;
827 s->pool_end = p->data + p->size;
828 return p->data;
829}
830
831void tcg_pool_reset(TCGContext *s)
832{
4055299e
KB
833 TCGPool *p, *t;
834 for (p = s->pool_first_large; p; p = t) {
835 t = p->next;
836 g_free(p);
837 }
838 s->pool_first_large = NULL;
c896fe29
FB
839 s->pool_cur = s->pool_end = NULL;
840 s->pool_current = NULL;
841}
842
2ef6175a
RH
843#include "exec/helper-proto.h"
844
39004a71 845static TCGHelperInfo all_helpers[] = {
2ef6175a 846#include "exec/helper-tcg.h"
100b5e01 847};
619205fd 848static GHashTable *helper_table;
100b5e01 849
8429a1ca
RH
850/*
851 * Create TCGHelperInfo structures for "tcg/tcg-ldst.h" functions,
852 * akin to what "exec/helper-tcg.h" does with DEF_HELPER_FLAGS_N.
853 * We only use these for layout in tcg_out_ld_helper_ret and
854 * tcg_out_st_helper_args, and share them between several of
855 * the helpers, with the end result that it's easier to build manually.
856 */
857
858#if TCG_TARGET_REG_BITS == 32
859# define dh_typecode_ttl dh_typecode_i32
860#else
861# define dh_typecode_ttl dh_typecode_i64
862#endif
863
864static TCGHelperInfo info_helper_ld32_mmu = {
865 .flags = TCG_CALL_NO_WG,
866 .typemask = dh_typemask(ttl, 0) /* return tcg_target_ulong */
867 | dh_typemask(env, 1)
24e46e6c 868 | dh_typemask(i64, 2) /* uint64_t addr */
8429a1ca
RH
869 | dh_typemask(i32, 3) /* unsigned oi */
870 | dh_typemask(ptr, 4) /* uintptr_t ra */
871};
872
873static TCGHelperInfo info_helper_ld64_mmu = {
874 .flags = TCG_CALL_NO_WG,
875 .typemask = dh_typemask(i64, 0) /* return uint64_t */
876 | dh_typemask(env, 1)
24e46e6c 877 | dh_typemask(i64, 2) /* uint64_t addr */
8429a1ca
RH
878 | dh_typemask(i32, 3) /* unsigned oi */
879 | dh_typemask(ptr, 4) /* uintptr_t ra */
880};
881
ebebea53
RH
882static TCGHelperInfo info_helper_ld128_mmu = {
883 .flags = TCG_CALL_NO_WG,
884 .typemask = dh_typemask(i128, 0) /* return Int128 */
885 | dh_typemask(env, 1)
24e46e6c 886 | dh_typemask(i64, 2) /* uint64_t addr */
ebebea53
RH
887 | dh_typemask(i32, 3) /* unsigned oi */
888 | dh_typemask(ptr, 4) /* uintptr_t ra */
889};
890
8429a1ca
RH
891static TCGHelperInfo info_helper_st32_mmu = {
892 .flags = TCG_CALL_NO_WG,
893 .typemask = dh_typemask(void, 0)
894 | dh_typemask(env, 1)
24e46e6c 895 | dh_typemask(i64, 2) /* uint64_t addr */
8429a1ca
RH
896 | dh_typemask(i32, 3) /* uint32_t data */
897 | dh_typemask(i32, 4) /* unsigned oi */
898 | dh_typemask(ptr, 5) /* uintptr_t ra */
899};
900
901static TCGHelperInfo info_helper_st64_mmu = {
902 .flags = TCG_CALL_NO_WG,
903 .typemask = dh_typemask(void, 0)
904 | dh_typemask(env, 1)
24e46e6c 905 | dh_typemask(i64, 2) /* uint64_t addr */
8429a1ca
RH
906 | dh_typemask(i64, 3) /* uint64_t data */
907 | dh_typemask(i32, 4) /* unsigned oi */
908 | dh_typemask(ptr, 5) /* uintptr_t ra */
909};
910
ebebea53
RH
911static TCGHelperInfo info_helper_st128_mmu = {
912 .flags = TCG_CALL_NO_WG,
913 .typemask = dh_typemask(void, 0)
914 | dh_typemask(env, 1)
24e46e6c 915 | dh_typemask(i64, 2) /* uint64_t addr */
ebebea53
RH
916 | dh_typemask(i128, 3) /* Int128 data */
917 | dh_typemask(i32, 4) /* unsigned oi */
918 | dh_typemask(ptr, 5) /* uintptr_t ra */
919};
920
22f15579 921#ifdef CONFIG_TCG_INTERPRETER
c6ef8c7b
PMD
922static ffi_type *typecode_to_ffi(int argmask)
923{
e9709e17
RH
924 /*
925 * libffi does not support __int128_t, so we have forced Int128
926 * to use the structure definition instead of the builtin type.
927 */
928 static ffi_type *ffi_type_i128_elements[3] = {
929 &ffi_type_uint64,
930 &ffi_type_uint64,
931 NULL
932 };
933 static ffi_type ffi_type_i128 = {
934 .size = 16,
935 .alignment = __alignof__(Int128),
936 .type = FFI_TYPE_STRUCT,
937 .elements = ffi_type_i128_elements,
938 };
939
c6ef8c7b
PMD
940 switch (argmask) {
941 case dh_typecode_void:
942 return &ffi_type_void;
943 case dh_typecode_i32:
944 return &ffi_type_uint32;
945 case dh_typecode_s32:
946 return &ffi_type_sint32;
947 case dh_typecode_i64:
948 return &ffi_type_uint64;
949 case dh_typecode_s64:
950 return &ffi_type_sint64;
951 case dh_typecode_ptr:
952 return &ffi_type_pointer;
e9709e17
RH
953 case dh_typecode_i128:
954 return &ffi_type_i128;
c6ef8c7b
PMD
955 }
956 g_assert_not_reached();
957}
0c22e176
PMD
958
959static void init_ffi_layouts(void)
960{
961 /* g_direct_hash/equal for direct comparisons on uint32_t. */
f9c4bb80
RH
962 GHashTable *ffi_table = g_hash_table_new(NULL, NULL);
963
0c22e176 964 for (int i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
f9c4bb80
RH
965 TCGHelperInfo *info = &all_helpers[i];
966 unsigned typemask = info->typemask;
0c22e176
PMD
967 gpointer hash = (gpointer)(uintptr_t)typemask;
968 struct {
969 ffi_cif cif;
970 ffi_type *args[];
971 } *ca;
972 ffi_status status;
973 int nargs;
f9c4bb80 974 ffi_cif *cif;
0c22e176 975
f9c4bb80
RH
976 cif = g_hash_table_lookup(ffi_table, hash);
977 if (cif) {
978 info->cif = cif;
0c22e176
PMD
979 continue;
980 }
981
982 /* Ignoring the return type, find the last non-zero field. */
983 nargs = 32 - clz32(typemask >> 3);
984 nargs = DIV_ROUND_UP(nargs, 3);
e9709e17 985 assert(nargs <= MAX_CALL_IARGS);
0c22e176
PMD
986
987 ca = g_malloc0(sizeof(*ca) + nargs * sizeof(ffi_type *));
988 ca->cif.rtype = typecode_to_ffi(typemask & 7);
989 ca->cif.nargs = nargs;
990
991 if (nargs != 0) {
992 ca->cif.arg_types = ca->args;
993 for (int j = 0; j < nargs; ++j) {
994 int typecode = extract32(typemask, (j + 1) * 3, 3);
995 ca->args[j] = typecode_to_ffi(typecode);
996 }
997 }
998
999 status = ffi_prep_cif(&ca->cif, FFI_DEFAULT_ABI, nargs,
1000 ca->cif.rtype, ca->cif.arg_types);
1001 assert(status == FFI_OK);
1002
f9c4bb80
RH
1003 cif = &ca->cif;
1004 info->cif = cif;
1005 g_hash_table_insert(ffi_table, hash, (gpointer)cif);
0c22e176 1006 }
f9c4bb80
RH
1007
1008 g_hash_table_destroy(ffi_table);
0c22e176
PMD
1009}
1010#endif /* CONFIG_TCG_INTERPRETER */
22f15579 1011
338b61e9
RH
1012static inline bool arg_slot_reg_p(unsigned arg_slot)
1013{
1014 /*
1015 * Split the sizeof away from the comparison to avoid Werror from
1016 * "unsigned < 0 is always false", when iarg_regs is empty.
1017 */
1018 unsigned nreg = ARRAY_SIZE(tcg_target_call_iarg_regs);
1019 return arg_slot < nreg;
1020}
1021
d78e4a4f
RH
1022static inline int arg_slot_stk_ofs(unsigned arg_slot)
1023{
1024 unsigned max = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
1025 unsigned stk_slot = arg_slot - ARRAY_SIZE(tcg_target_call_iarg_regs);
1026
1027 tcg_debug_assert(stk_slot < max);
1028 return TCG_TARGET_CALL_STACK_OFFSET + stk_slot * sizeof(tcg_target_long);
1029}
1030
39004a71
RH
1031typedef struct TCGCumulativeArgs {
1032 int arg_idx; /* tcg_gen_callN args[] */
1033 int info_in_idx; /* TCGHelperInfo in[] */
1034 int arg_slot; /* regs+stack slot */
1035 int ref_slot; /* stack slots for references */
1036} TCGCumulativeArgs;
1037
1038static void layout_arg_even(TCGCumulativeArgs *cum)
1039{
1040 cum->arg_slot += cum->arg_slot & 1;
1041}
1042
1043static void layout_arg_1(TCGCumulativeArgs *cum, TCGHelperInfo *info,
1044 TCGCallArgumentKind kind)
1045{
1046 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1047
1048 *loc = (TCGCallArgumentLoc){
1049 .kind = kind,
1050 .arg_idx = cum->arg_idx,
1051 .arg_slot = cum->arg_slot,
1052 };
1053 cum->info_in_idx++;
1054 cum->arg_slot++;
1055}
1056
1057static void layout_arg_normal_n(TCGCumulativeArgs *cum,
1058 TCGHelperInfo *info, int n)
1059{
1060 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1061
1062 for (int i = 0; i < n; ++i) {
1063 /* Layout all using the same arg_idx, adjusting the subindex. */
1064 loc[i] = (TCGCallArgumentLoc){
1065 .kind = TCG_CALL_ARG_NORMAL,
1066 .arg_idx = cum->arg_idx,
1067 .tmp_subindex = i,
1068 .arg_slot = cum->arg_slot + i,
1069 };
1070 }
1071 cum->info_in_idx += n;
1072 cum->arg_slot += n;
1073}
1074
313bdea8
RH
1075static void layout_arg_by_ref(TCGCumulativeArgs *cum, TCGHelperInfo *info)
1076{
1077 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1078 int n = 128 / TCG_TARGET_REG_BITS;
1079
1080 /* The first subindex carries the pointer. */
1081 layout_arg_1(cum, info, TCG_CALL_ARG_BY_REF);
1082
1083 /*
1084 * The callee is allowed to clobber memory associated with
1085 * structure pass by-reference. Therefore we must make copies.
1086 * Allocate space from "ref_slot", which will be adjusted to
1087 * follow the parameters on the stack.
1088 */
1089 loc[0].ref_slot = cum->ref_slot;
1090
1091 /*
1092 * Subsequent words also go into the reference slot, but
1093 * do not accumulate into the regular arguments.
1094 */
1095 for (int i = 1; i < n; ++i) {
1096 loc[i] = (TCGCallArgumentLoc){
1097 .kind = TCG_CALL_ARG_BY_REF_N,
1098 .arg_idx = cum->arg_idx,
1099 .tmp_subindex = i,
1100 .ref_slot = cum->ref_slot + i,
1101 };
1102 }
1103 cum->info_in_idx += n;
1104 cum->ref_slot += n;
1105}
1106
39004a71
RH
1107static void init_call_layout(TCGHelperInfo *info)
1108{
1109 int max_reg_slots = ARRAY_SIZE(tcg_target_call_iarg_regs);
1110 int max_stk_slots = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
1111 unsigned typemask = info->typemask;
1112 unsigned typecode;
1113 TCGCumulativeArgs cum = { };
1114
1115 /*
1116 * Parse and place any function return value.
1117 */
1118 typecode = typemask & 7;
1119 switch (typecode) {
1120 case dh_typecode_void:
1121 info->nr_out = 0;
1122 break;
1123 case dh_typecode_i32:
1124 case dh_typecode_s32:
1125 case dh_typecode_ptr:
1126 info->nr_out = 1;
1127 info->out_kind = TCG_CALL_RET_NORMAL;
1128 break;
1129 case dh_typecode_i64:
1130 case dh_typecode_s64:
1131 info->nr_out = 64 / TCG_TARGET_REG_BITS;
1132 info->out_kind = TCG_CALL_RET_NORMAL;
5e3d0c19
RH
1133 /* Query the last register now to trigger any assert early. */
1134 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
466d3759
RH
1135 break;
1136 case dh_typecode_i128:
1137 info->nr_out = 128 / TCG_TARGET_REG_BITS;
5427a9a7
RH
1138 info->out_kind = TCG_TARGET_CALL_RET_I128;
1139 switch (TCG_TARGET_CALL_RET_I128) {
466d3759 1140 case TCG_CALL_RET_NORMAL:
5e3d0c19
RH
1141 /* Query the last register now to trigger any assert early. */
1142 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
466d3759 1143 break;
c6556aa0
RH
1144 case TCG_CALL_RET_BY_VEC:
1145 /* Query the single register now to trigger any assert early. */
1146 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0);
1147 break;
313bdea8
RH
1148 case TCG_CALL_RET_BY_REF:
1149 /*
1150 * Allocate the first argument to the output.
1151 * We don't need to store this anywhere, just make it
1152 * unavailable for use in the input loop below.
1153 */
1154 cum.arg_slot = 1;
1155 break;
466d3759
RH
1156 default:
1157 qemu_build_not_reached();
1158 }
39004a71
RH
1159 break;
1160 default:
1161 g_assert_not_reached();
1162 }
39004a71
RH
1163
1164 /*
1165 * Parse and place function arguments.
1166 */
1167 for (typemask >>= 3; typemask; typemask >>= 3, cum.arg_idx++) {
1168 TCGCallArgumentKind kind;
1169 TCGType type;
1170
1171 typecode = typemask & 7;
1172 switch (typecode) {
1173 case dh_typecode_i32:
1174 case dh_typecode_s32:
1175 type = TCG_TYPE_I32;
1176 break;
1177 case dh_typecode_i64:
1178 case dh_typecode_s64:
1179 type = TCG_TYPE_I64;
1180 break;
1181 case dh_typecode_ptr:
1182 type = TCG_TYPE_PTR;
1183 break;
466d3759
RH
1184 case dh_typecode_i128:
1185 type = TCG_TYPE_I128;
1186 break;
39004a71
RH
1187 default:
1188 g_assert_not_reached();
1189 }
1190
1191 switch (type) {
1192 case TCG_TYPE_I32:
1193 switch (TCG_TARGET_CALL_ARG_I32) {
1194 case TCG_CALL_ARG_EVEN:
1195 layout_arg_even(&cum);
1196 /* fall through */
1197 case TCG_CALL_ARG_NORMAL:
1198 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
1199 break;
1200 case TCG_CALL_ARG_EXTEND:
1201 kind = TCG_CALL_ARG_EXTEND_U + (typecode & 1);
1202 layout_arg_1(&cum, info, kind);
1203 break;
1204 default:
1205 qemu_build_not_reached();
1206 }
1207 break;
1208
1209 case TCG_TYPE_I64:
1210 switch (TCG_TARGET_CALL_ARG_I64) {
1211 case TCG_CALL_ARG_EVEN:
1212 layout_arg_even(&cum);
1213 /* fall through */
1214 case TCG_CALL_ARG_NORMAL:
1215 if (TCG_TARGET_REG_BITS == 32) {
1216 layout_arg_normal_n(&cum, info, 2);
1217 } else {
1218 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
1219 }
1220 break;
1221 default:
1222 qemu_build_not_reached();
1223 }
1224 break;
1225
466d3759 1226 case TCG_TYPE_I128:
5427a9a7 1227 switch (TCG_TARGET_CALL_ARG_I128) {
466d3759
RH
1228 case TCG_CALL_ARG_EVEN:
1229 layout_arg_even(&cum);
1230 /* fall through */
1231 case TCG_CALL_ARG_NORMAL:
1232 layout_arg_normal_n(&cum, info, 128 / TCG_TARGET_REG_BITS);
1233 break;
313bdea8
RH
1234 case TCG_CALL_ARG_BY_REF:
1235 layout_arg_by_ref(&cum, info);
1236 break;
466d3759
RH
1237 default:
1238 qemu_build_not_reached();
1239 }
1240 break;
1241
39004a71
RH
1242 default:
1243 g_assert_not_reached();
1244 }
1245 }
1246 info->nr_in = cum.info_in_idx;
1247
1248 /* Validate that we didn't overrun the input array. */
1249 assert(cum.info_in_idx <= ARRAY_SIZE(info->in));
1250 /* Validate the backend has enough argument space. */
1251 assert(cum.arg_slot <= max_reg_slots + max_stk_slots);
313bdea8
RH
1252
1253 /*
1254 * Relocate the "ref_slot" area to the end of the parameters.
1255 * Minimizing this stack offset helps code size for x86,
1256 * which has a signed 8-bit offset encoding.
1257 */
1258 if (cum.ref_slot != 0) {
1259 int ref_base = 0;
1260
1261 if (cum.arg_slot > max_reg_slots) {
1262 int align = __alignof(Int128) / sizeof(tcg_target_long);
1263
1264 ref_base = cum.arg_slot - max_reg_slots;
1265 if (align > 1) {
1266 ref_base = ROUND_UP(ref_base, align);
1267 }
1268 }
1269 assert(ref_base + cum.ref_slot <= max_stk_slots);
d78e4a4f 1270 ref_base += max_reg_slots;
313bdea8
RH
1271
1272 if (ref_base != 0) {
1273 for (int i = cum.info_in_idx - 1; i >= 0; --i) {
1274 TCGCallArgumentLoc *loc = &info->in[i];
1275 switch (loc->kind) {
1276 case TCG_CALL_ARG_BY_REF:
1277 case TCG_CALL_ARG_BY_REF_N:
1278 loc->ref_slot += ref_base;
1279 break;
1280 default:
1281 break;
1282 }
1283 }
1284 }
1285 }
39004a71
RH
1286}
1287
91478cef 1288static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
f69d277e 1289static void process_op_defs(TCGContext *s);
1c2adb95
RH
1290static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1291 TCGReg reg, const char *name);
91478cef 1292
43b972b7 1293static void tcg_context_init(unsigned max_cpus)
c896fe29 1294{
a76aabd3 1295 TCGContext *s = &tcg_init_ctx;
100b5e01 1296 int op, total_args, n, i;
c896fe29
FB
1297 TCGOpDef *def;
1298 TCGArgConstraint *args_ct;
1c2adb95 1299 TCGTemp *ts;
c896fe29
FB
1300
1301 memset(s, 0, sizeof(*s));
c896fe29 1302 s->nb_globals = 0;
c70fbf0a 1303
c896fe29
FB
1304 /* Count total number of arguments and allocate the corresponding
1305 space */
1306 total_args = 0;
1307 for(op = 0; op < NB_OPS; op++) {
1308 def = &tcg_op_defs[op];
1309 n = def->nb_iargs + def->nb_oargs;
1310 total_args += n;
1311 }
1312
bc2b17e6 1313 args_ct = g_new0(TCGArgConstraint, total_args);
c896fe29
FB
1314
1315 for(op = 0; op < NB_OPS; op++) {
1316 def = &tcg_op_defs[op];
1317 def->args_ct = args_ct;
c896fe29 1318 n = def->nb_iargs + def->nb_oargs;
c896fe29
FB
1319 args_ct += n;
1320 }
5cd8f621
RH
1321
1322 /* Register helpers. */
84fd9dd3 1323 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
619205fd 1324 helper_table = g_hash_table_new(NULL, NULL);
84fd9dd3 1325
100b5e01 1326 for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
39004a71 1327 init_call_layout(&all_helpers[i]);
84fd9dd3 1328 g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
72866e82 1329 (gpointer)&all_helpers[i]);
100b5e01 1330 }
5cd8f621 1331
8429a1ca
RH
1332 init_call_layout(&info_helper_ld32_mmu);
1333 init_call_layout(&info_helper_ld64_mmu);
ebebea53 1334 init_call_layout(&info_helper_ld128_mmu);
8429a1ca
RH
1335 init_call_layout(&info_helper_st32_mmu);
1336 init_call_layout(&info_helper_st64_mmu);
ebebea53 1337 init_call_layout(&info_helper_st128_mmu);
8429a1ca 1338
22f15579 1339#ifdef CONFIG_TCG_INTERPRETER
0c22e176 1340 init_ffi_layouts();
22f15579
RH
1341#endif
1342
c896fe29 1343 tcg_target_init(s);
f69d277e 1344 process_op_defs(s);
91478cef
RH
1345
1346 /* Reverse the order of the saved registers, assuming they're all at
1347 the start of tcg_target_reg_alloc_order. */
1348 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
1349 int r = tcg_target_reg_alloc_order[n];
1350 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
1351 break;
1352 }
1353 }
1354 for (i = 0; i < n; ++i) {
1355 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
1356 }
1357 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
1358 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
1359 }
b1311c4a 1360
38b47b19
EC
1361 alloc_tcg_plugin_context(s);
1362
b1311c4a 1363 tcg_ctx = s;
3468b59e
EC
1364 /*
1365 * In user-mode we simply share the init context among threads, since we
1366 * use a single region. See the documentation tcg_region_init() for the
1367 * reasoning behind this.
1368 * In softmmu we will have at most max_cpus TCG threads.
1369 */
1370#ifdef CONFIG_USER_ONLY
df2cce29 1371 tcg_ctxs = &tcg_ctx;
0e2d61cf
RH
1372 tcg_cur_ctxs = 1;
1373 tcg_max_ctxs = 1;
3468b59e 1374#else
0e2d61cf
RH
1375 tcg_max_ctxs = max_cpus;
1376 tcg_ctxs = g_new0(TCGContext *, max_cpus);
3468b59e 1377#endif
1c2adb95
RH
1378
1379 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
1380 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
1381 cpu_env = temp_tcgv_ptr(ts);
9002ec79 1382}
b03cce8e 1383
43b972b7 1384void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus)
a76aabd3 1385{
43b972b7
RH
1386 tcg_context_init(max_cpus);
1387 tcg_region_init(tb_size, splitwx, max_cpus);
a76aabd3
RH
1388}
1389
6e3b2bfd
EC
1390/*
1391 * Allocate TBs right before their corresponding translated code, making
1392 * sure that TBs and code are on different cache lines.
1393 */
1394TranslationBlock *tcg_tb_alloc(TCGContext *s)
1395{
1396 uintptr_t align = qemu_icache_linesize;
1397 TranslationBlock *tb;
1398 void *next;
1399
e8feb96f 1400 retry:
6e3b2bfd
EC
1401 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
1402 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
1403
1404 if (unlikely(next > s->code_gen_highwater)) {
e8feb96f
EC
1405 if (tcg_region_alloc(s)) {
1406 return NULL;
1407 }
1408 goto retry;
6e3b2bfd 1409 }
d73415a3 1410 qatomic_set(&s->code_gen_ptr, next);
57a26946 1411 s->data_gen_ptr = NULL;
6e3b2bfd
EC
1412 return tb;
1413}
1414
9002ec79
RH
1415void tcg_prologue_init(TCGContext *s)
1416{
b0a0794a 1417 size_t prologue_size;
8163b749 1418
b0a0794a
RH
1419 s->code_ptr = s->code_gen_ptr;
1420 s->code_buf = s->code_gen_ptr;
5b38ee31 1421 s->data_gen_ptr = NULL;
b91ccb31
RH
1422
1423#ifndef CONFIG_TCG_INTERPRETER
b0a0794a 1424 tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr);
b91ccb31 1425#endif
8163b749 1426
5b38ee31
RH
1427#ifdef TCG_TARGET_NEED_POOL_LABELS
1428 s->pool_labels = NULL;
1429#endif
1430
653b87eb 1431 qemu_thread_jit_write();
8163b749 1432 /* Generate the prologue. */
b03cce8e 1433 tcg_target_qemu_prologue(s);
5b38ee31
RH
1434
1435#ifdef TCG_TARGET_NEED_POOL_LABELS
1436 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1437 {
1768987b
RH
1438 int result = tcg_out_pool_finalize(s);
1439 tcg_debug_assert(result == 0);
5b38ee31
RH
1440 }
1441#endif
1442
b0a0794a 1443 prologue_size = tcg_current_code_size(s);
5584e2db 1444 perf_report_prologue(s->code_gen_ptr, prologue_size);
b0a0794a 1445
df5d2b16 1446#ifndef CONFIG_TCG_INTERPRETER
b0a0794a
RH
1447 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
1448 (uintptr_t)s->code_buf, prologue_size);
df5d2b16 1449#endif
8163b749 1450
d6b64b2b
RH
1451#ifdef DEBUG_DISAS
1452 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
c60f599b 1453 FILE *logfile = qemu_log_trylock();
78b54858
RH
1454 if (logfile) {
1455 fprintf(logfile, "PROLOGUE: [size=%zu]\n", prologue_size);
1456 if (s->data_gen_ptr) {
1457 size_t code_size = s->data_gen_ptr - s->code_gen_ptr;
1458 size_t data_size = prologue_size - code_size;
1459 size_t i;
1460
1461 disas(logfile, s->code_gen_ptr, code_size);
1462
1463 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1464 if (sizeof(tcg_target_ulong) == 8) {
1465 fprintf(logfile,
1466 "0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1467 (uintptr_t)s->data_gen_ptr + i,
1468 *(uint64_t *)(s->data_gen_ptr + i));
1469 } else {
1470 fprintf(logfile,
1471 "0x%08" PRIxPTR ": .long 0x%08x\n",
1472 (uintptr_t)s->data_gen_ptr + i,
1473 *(uint32_t *)(s->data_gen_ptr + i));
1474 }
5b38ee31 1475 }
78b54858
RH
1476 } else {
1477 disas(logfile, s->code_gen_ptr, prologue_size);
5b38ee31 1478 }
78b54858 1479 fprintf(logfile, "\n");
78b54858 1480 qemu_log_unlock(logfile);
5b38ee31 1481 }
d6b64b2b
RH
1482 }
1483#endif
cedbcb01 1484
6eea0434
RH
1485#ifndef CONFIG_TCG_INTERPRETER
1486 /*
1487 * Assert that goto_ptr is implemented completely, setting an epilogue.
1488 * For tci, we use NULL as the signal to return from the interpreter,
1489 * so skip this check.
1490 */
f4e01e30 1491 tcg_debug_assert(tcg_code_gen_epilogue != NULL);
6eea0434 1492#endif
d1c74ab3
RH
1493
1494 tcg_region_prologue_set(s);
c896fe29
FB
1495}
1496
c896fe29
FB
1497void tcg_func_start(TCGContext *s)
1498{
1499 tcg_pool_reset(s);
1500 s->nb_temps = s->nb_globals;
0ec9eabc
RH
1501
1502 /* No temps have been previously allocated for size or locality. */
1503 memset(s->free_temps, 0, sizeof(s->free_temps));
1504
c0522136
RH
1505 /* No constant temps have been previously allocated. */
1506 for (int i = 0; i < TCG_TYPE_COUNT; ++i) {
1507 if (s->const_table[i]) {
1508 g_hash_table_remove_all(s->const_table[i]);
1509 }
1510 }
1511
abebf925 1512 s->nb_ops = 0;
c896fe29
FB
1513 s->nb_labels = 0;
1514 s->current_frame_offset = s->frame_start;
1515
0a209d4b
RH
1516#ifdef CONFIG_DEBUG_TCG
1517 s->goto_tb_issue_mask = 0;
1518#endif
1519
15fa08f8
RH
1520 QTAILQ_INIT(&s->ops);
1521 QTAILQ_INIT(&s->free_ops);
bef16ab4 1522 QSIMPLEQ_INIT(&s->labels);
c896fe29
FB
1523}
1524
ae30e866 1525static TCGTemp *tcg_temp_alloc(TCGContext *s)
7ca4b752
RH
1526{
1527 int n = s->nb_temps++;
ae30e866
RH
1528
1529 if (n >= TCG_MAX_TEMPS) {
db6b7d0c 1530 tcg_raise_tb_overflow(s);
ae30e866 1531 }
7ca4b752
RH
1532 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1533}
1534
ae30e866 1535static TCGTemp *tcg_global_alloc(TCGContext *s)
7ca4b752 1536{
fa477d25
RH
1537 TCGTemp *ts;
1538
7ca4b752 1539 tcg_debug_assert(s->nb_globals == s->nb_temps);
ae30e866 1540 tcg_debug_assert(s->nb_globals < TCG_MAX_TEMPS);
7ca4b752 1541 s->nb_globals++;
fa477d25 1542 ts = tcg_temp_alloc(s);
ee17db83 1543 ts->kind = TEMP_GLOBAL;
fa477d25
RH
1544
1545 return ts;
c896fe29
FB
1546}
1547
085272b3
RH
1548static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1549 TCGReg reg, const char *name)
c896fe29 1550{
c896fe29 1551 TCGTemp *ts;
c896fe29 1552
1a057554 1553 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
7ca4b752
RH
1554
1555 ts = tcg_global_alloc(s);
c896fe29
FB
1556 ts->base_type = type;
1557 ts->type = type;
ee17db83 1558 ts->kind = TEMP_FIXED;
c896fe29 1559 ts->reg = reg;
c896fe29 1560 ts->name = name;
c896fe29 1561 tcg_regset_set_reg(s->reserved_regs, reg);
7ca4b752 1562
085272b3 1563 return ts;
a7812ae4
PB
1564}
1565
b6638662 1566void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
b3a62939 1567{
b3a62939
RH
1568 s->frame_start = start;
1569 s->frame_end = start + size;
085272b3
RH
1570 s->frame_temp
1571 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
b3a62939
RH
1572}
1573
085272b3
RH
1574TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
1575 intptr_t offset, const char *name)
c896fe29 1576{
b1311c4a 1577 TCGContext *s = tcg_ctx;
dc41aa7d 1578 TCGTemp *base_ts = tcgv_ptr_temp(base);
7ca4b752 1579 TCGTemp *ts = tcg_global_alloc(s);
aef85402 1580 int indirect_reg = 0;
c896fe29 1581
c0522136
RH
1582 switch (base_ts->kind) {
1583 case TEMP_FIXED:
1584 break;
1585 case TEMP_GLOBAL:
5a18407f
RH
1586 /* We do not support double-indirect registers. */
1587 tcg_debug_assert(!base_ts->indirect_reg);
b3915dbb 1588 base_ts->indirect_base = 1;
5a18407f
RH
1589 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1590 ? 2 : 1);
1591 indirect_reg = 1;
c0522136
RH
1592 break;
1593 default:
1594 g_assert_not_reached();
b3915dbb
RH
1595 }
1596
7ca4b752
RH
1597 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1598 TCGTemp *ts2 = tcg_global_alloc(s);
c896fe29 1599 char buf[64];
7ca4b752
RH
1600
1601 ts->base_type = TCG_TYPE_I64;
c896fe29 1602 ts->type = TCG_TYPE_I32;
b3915dbb 1603 ts->indirect_reg = indirect_reg;
c896fe29 1604 ts->mem_allocated = 1;
b3a62939 1605 ts->mem_base = base_ts;
aef85402 1606 ts->mem_offset = offset;
c896fe29
FB
1607 pstrcpy(buf, sizeof(buf), name);
1608 pstrcat(buf, sizeof(buf), "_0");
1609 ts->name = strdup(buf);
c896fe29 1610
7ca4b752
RH
1611 tcg_debug_assert(ts2 == ts + 1);
1612 ts2->base_type = TCG_TYPE_I64;
1613 ts2->type = TCG_TYPE_I32;
b3915dbb 1614 ts2->indirect_reg = indirect_reg;
7ca4b752
RH
1615 ts2->mem_allocated = 1;
1616 ts2->mem_base = base_ts;
aef85402 1617 ts2->mem_offset = offset + 4;
fac87bd2 1618 ts2->temp_subindex = 1;
c896fe29
FB
1619 pstrcpy(buf, sizeof(buf), name);
1620 pstrcat(buf, sizeof(buf), "_1");
120c1084 1621 ts2->name = strdup(buf);
7ca4b752 1622 } else {
c896fe29
FB
1623 ts->base_type = type;
1624 ts->type = type;
b3915dbb 1625 ts->indirect_reg = indirect_reg;
c896fe29 1626 ts->mem_allocated = 1;
b3a62939 1627 ts->mem_base = base_ts;
c896fe29 1628 ts->mem_offset = offset;
c896fe29 1629 ts->name = name;
c896fe29 1630 }
085272b3 1631 return ts;
a7812ae4
PB
1632}
1633
bbf989bf 1634TCGTemp *tcg_temp_new_internal(TCGType type, TCGTempKind kind)
c896fe29 1635{
b1311c4a 1636 TCGContext *s = tcg_ctx;
c896fe29 1637 TCGTemp *ts;
e1c08b00 1638 int n;
7ca4b752 1639
e1c08b00
RH
1640 if (kind == TEMP_EBB) {
1641 int idx = find_first_bit(s->free_temps[type].l, TCG_MAX_TEMPS);
1642
1643 if (idx < TCG_MAX_TEMPS) {
1644 /* There is already an available temp with the right type. */
1645 clear_bit(idx, s->free_temps[type].l);
1646
1647 ts = &s->temps[idx];
1648 ts->temp_allocated = 1;
1649 tcg_debug_assert(ts->base_type == type);
1650 tcg_debug_assert(ts->kind == kind);
2f2e911d 1651 return ts;
43eef72f 1652 }
e1c08b00
RH
1653 } else {
1654 tcg_debug_assert(kind == TEMP_TB);
1655 }
7ca4b752 1656
e1c08b00
RH
1657 switch (type) {
1658 case TCG_TYPE_I32:
1659 case TCG_TYPE_V64:
1660 case TCG_TYPE_V128:
1661 case TCG_TYPE_V256:
1662 n = 1;
1663 break;
1664 case TCG_TYPE_I64:
1665 n = 64 / TCG_TARGET_REG_BITS;
1666 break;
1667 case TCG_TYPE_I128:
1668 n = 128 / TCG_TARGET_REG_BITS;
1669 break;
1670 default:
1671 g_assert_not_reached();
1672 }
43eef72f 1673
e1c08b00
RH
1674 ts = tcg_temp_alloc(s);
1675 ts->base_type = type;
1676 ts->temp_allocated = 1;
1677 ts->kind = kind;
1678
1679 if (n == 1) {
1680 ts->type = type;
1681 } else {
1682 ts->type = TCG_TYPE_REG;
43eef72f 1683
e1c08b00
RH
1684 for (int i = 1; i < n; ++i) {
1685 TCGTemp *ts2 = tcg_temp_alloc(s);
43eef72f 1686
e1c08b00
RH
1687 tcg_debug_assert(ts2 == ts + i);
1688 ts2->base_type = type;
1689 ts2->type = TCG_TYPE_REG;
1690 ts2->temp_allocated = 1;
1691 ts2->temp_subindex = i;
1692 ts2->kind = kind;
e8996ee0 1693 }
c896fe29 1694 }
085272b3 1695 return ts;
c896fe29
FB
1696}
1697
d2fd745f
RH
1698TCGv_vec tcg_temp_new_vec(TCGType type)
1699{
1700 TCGTemp *t;
1701
1702#ifdef CONFIG_DEBUG_TCG
1703 switch (type) {
1704 case TCG_TYPE_V64:
1705 assert(TCG_TARGET_HAS_v64);
1706 break;
1707 case TCG_TYPE_V128:
1708 assert(TCG_TARGET_HAS_v128);
1709 break;
1710 case TCG_TYPE_V256:
1711 assert(TCG_TARGET_HAS_v256);
1712 break;
1713 default:
1714 g_assert_not_reached();
1715 }
1716#endif
1717
bbf989bf 1718 t = tcg_temp_new_internal(type, TEMP_EBB);
d2fd745f
RH
1719 return temp_tcgv_vec(t);
1720}
1721
1722/* Create a new temp of the same type as an existing temp. */
1723TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1724{
1725 TCGTemp *t = tcgv_vec_temp(match);
1726
1727 tcg_debug_assert(t->temp_allocated != 0);
1728
bbf989bf 1729 t = tcg_temp_new_internal(t->base_type, TEMP_EBB);
d2fd745f
RH
1730 return temp_tcgv_vec(t);
1731}
1732
5bfa8034 1733void tcg_temp_free_internal(TCGTemp *ts)
c896fe29 1734{
b1311c4a 1735 TCGContext *s = tcg_ctx;
c896fe29 1736
c7482438
RH
1737 switch (ts->kind) {
1738 case TEMP_CONST:
f57c6915 1739 case TEMP_TB:
2f2e911d
RH
1740 /* Silently ignore free. */
1741 break;
1742 case TEMP_EBB:
1743 tcg_debug_assert(ts->temp_allocated != 0);
1744 ts->temp_allocated = 0;
1745 set_bit(temp_idx(ts), s->free_temps[ts->base_type].l);
c7482438
RH
1746 break;
1747 default:
2f2e911d 1748 /* It never made sense to free TEMP_FIXED or TEMP_GLOBAL. */
c7482438 1749 g_assert_not_reached();
c0522136 1750 }
c896fe29
FB
1751}
1752
c0522136
RH
1753TCGTemp *tcg_constant_internal(TCGType type, int64_t val)
1754{
1755 TCGContext *s = tcg_ctx;
1756 GHashTable *h = s->const_table[type];
1757 TCGTemp *ts;
1758
1759 if (h == NULL) {
1760 h = g_hash_table_new(g_int64_hash, g_int64_equal);
1761 s->const_table[type] = h;
1762 }
1763
1764 ts = g_hash_table_lookup(h, &val);
1765 if (ts == NULL) {
aef85402
RH
1766 int64_t *val_ptr;
1767
c0522136
RH
1768 ts = tcg_temp_alloc(s);
1769
1770 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1771 TCGTemp *ts2 = tcg_temp_alloc(s);
1772
aef85402
RH
1773 tcg_debug_assert(ts2 == ts + 1);
1774
c0522136
RH
1775 ts->base_type = TCG_TYPE_I64;
1776 ts->type = TCG_TYPE_I32;
1777 ts->kind = TEMP_CONST;
1778 ts->temp_allocated = 1;
c0522136 1779
c0522136
RH
1780 ts2->base_type = TCG_TYPE_I64;
1781 ts2->type = TCG_TYPE_I32;
1782 ts2->kind = TEMP_CONST;
1783 ts2->temp_allocated = 1;
fac87bd2 1784 ts2->temp_subindex = 1;
aef85402
RH
1785
1786 /*
1787 * Retain the full value of the 64-bit constant in the low
1788 * part, so that the hash table works. Actual uses will
1789 * truncate the value to the low part.
1790 */
1791 ts[HOST_BIG_ENDIAN].val = val;
1792 ts[!HOST_BIG_ENDIAN].val = val >> 32;
1793 val_ptr = &ts[HOST_BIG_ENDIAN].val;
c0522136
RH
1794 } else {
1795 ts->base_type = type;
1796 ts->type = type;
1797 ts->kind = TEMP_CONST;
1798 ts->temp_allocated = 1;
1799 ts->val = val;
aef85402 1800 val_ptr = &ts->val;
c0522136 1801 }
aef85402 1802 g_hash_table_insert(h, val_ptr, ts);
c0522136
RH
1803 }
1804
1805 return ts;
1806}
1807
1808TCGv_vec tcg_constant_vec(TCGType type, unsigned vece, int64_t val)
1809{
1810 val = dup_const(vece, val);
1811 return temp_tcgv_vec(tcg_constant_internal(type, val));
1812}
1813
88d4005b
RH
1814TCGv_vec tcg_constant_vec_matching(TCGv_vec match, unsigned vece, int64_t val)
1815{
1816 TCGTemp *t = tcgv_vec_temp(match);
1817
1818 tcg_debug_assert(t->temp_allocated != 0);
1819 return tcg_constant_vec(t->base_type, vece, val);
1820}
1821
be0f34b5
RH
1822/* Return true if OP may appear in the opcode stream.
1823 Test the runtime variable that controls each opcode. */
1824bool tcg_op_supported(TCGOpcode op)
1825{
d2fd745f
RH
1826 const bool have_vec
1827 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1828
be0f34b5
RH
1829 switch (op) {
1830 case INDEX_op_discard:
1831 case INDEX_op_set_label:
1832 case INDEX_op_call:
1833 case INDEX_op_br:
1834 case INDEX_op_mb:
1835 case INDEX_op_insn_start:
1836 case INDEX_op_exit_tb:
1837 case INDEX_op_goto_tb:
f4e01e30 1838 case INDEX_op_goto_ptr:
be0f34b5
RH
1839 case INDEX_op_qemu_ld_i32:
1840 case INDEX_op_qemu_st_i32:
1841 case INDEX_op_qemu_ld_i64:
1842 case INDEX_op_qemu_st_i64:
1843 return true;
1844
07ce0b05
RH
1845 case INDEX_op_qemu_st8_i32:
1846 return TCG_TARGET_HAS_qemu_st8_i32;
1847
12fde9bc
RH
1848 case INDEX_op_qemu_ld_i128:
1849 case INDEX_op_qemu_st_i128:
1850 return TCG_TARGET_HAS_qemu_ldst_i128;
1851
be0f34b5 1852 case INDEX_op_mov_i32:
be0f34b5
RH
1853 case INDEX_op_setcond_i32:
1854 case INDEX_op_brcond_i32:
1855 case INDEX_op_ld8u_i32:
1856 case INDEX_op_ld8s_i32:
1857 case INDEX_op_ld16u_i32:
1858 case INDEX_op_ld16s_i32:
1859 case INDEX_op_ld_i32:
1860 case INDEX_op_st8_i32:
1861 case INDEX_op_st16_i32:
1862 case INDEX_op_st_i32:
1863 case INDEX_op_add_i32:
1864 case INDEX_op_sub_i32:
1865 case INDEX_op_mul_i32:
1866 case INDEX_op_and_i32:
1867 case INDEX_op_or_i32:
1868 case INDEX_op_xor_i32:
1869 case INDEX_op_shl_i32:
1870 case INDEX_op_shr_i32:
1871 case INDEX_op_sar_i32:
1872 return true;
1873
1874 case INDEX_op_movcond_i32:
1875 return TCG_TARGET_HAS_movcond_i32;
1876 case INDEX_op_div_i32:
1877 case INDEX_op_divu_i32:
1878 return TCG_TARGET_HAS_div_i32;
1879 case INDEX_op_rem_i32:
1880 case INDEX_op_remu_i32:
1881 return TCG_TARGET_HAS_rem_i32;
1882 case INDEX_op_div2_i32:
1883 case INDEX_op_divu2_i32:
1884 return TCG_TARGET_HAS_div2_i32;
1885 case INDEX_op_rotl_i32:
1886 case INDEX_op_rotr_i32:
1887 return TCG_TARGET_HAS_rot_i32;
1888 case INDEX_op_deposit_i32:
1889 return TCG_TARGET_HAS_deposit_i32;
1890 case INDEX_op_extract_i32:
1891 return TCG_TARGET_HAS_extract_i32;
1892 case INDEX_op_sextract_i32:
1893 return TCG_TARGET_HAS_sextract_i32;
fce1296f
RH
1894 case INDEX_op_extract2_i32:
1895 return TCG_TARGET_HAS_extract2_i32;
be0f34b5
RH
1896 case INDEX_op_add2_i32:
1897 return TCG_TARGET_HAS_add2_i32;
1898 case INDEX_op_sub2_i32:
1899 return TCG_TARGET_HAS_sub2_i32;
1900 case INDEX_op_mulu2_i32:
1901 return TCG_TARGET_HAS_mulu2_i32;
1902 case INDEX_op_muls2_i32:
1903 return TCG_TARGET_HAS_muls2_i32;
1904 case INDEX_op_muluh_i32:
1905 return TCG_TARGET_HAS_muluh_i32;
1906 case INDEX_op_mulsh_i32:
1907 return TCG_TARGET_HAS_mulsh_i32;
1908 case INDEX_op_ext8s_i32:
1909 return TCG_TARGET_HAS_ext8s_i32;
1910 case INDEX_op_ext16s_i32:
1911 return TCG_TARGET_HAS_ext16s_i32;
1912 case INDEX_op_ext8u_i32:
1913 return TCG_TARGET_HAS_ext8u_i32;
1914 case INDEX_op_ext16u_i32:
1915 return TCG_TARGET_HAS_ext16u_i32;
1916 case INDEX_op_bswap16_i32:
1917 return TCG_TARGET_HAS_bswap16_i32;
1918 case INDEX_op_bswap32_i32:
1919 return TCG_TARGET_HAS_bswap32_i32;
1920 case INDEX_op_not_i32:
1921 return TCG_TARGET_HAS_not_i32;
1922 case INDEX_op_neg_i32:
1923 return TCG_TARGET_HAS_neg_i32;
1924 case INDEX_op_andc_i32:
1925 return TCG_TARGET_HAS_andc_i32;
1926 case INDEX_op_orc_i32:
1927 return TCG_TARGET_HAS_orc_i32;
1928 case INDEX_op_eqv_i32:
1929 return TCG_TARGET_HAS_eqv_i32;
1930 case INDEX_op_nand_i32:
1931 return TCG_TARGET_HAS_nand_i32;
1932 case INDEX_op_nor_i32:
1933 return TCG_TARGET_HAS_nor_i32;
1934 case INDEX_op_clz_i32:
1935 return TCG_TARGET_HAS_clz_i32;
1936 case INDEX_op_ctz_i32:
1937 return TCG_TARGET_HAS_ctz_i32;
1938 case INDEX_op_ctpop_i32:
1939 return TCG_TARGET_HAS_ctpop_i32;
1940
1941 case INDEX_op_brcond2_i32:
1942 case INDEX_op_setcond2_i32:
1943 return TCG_TARGET_REG_BITS == 32;
1944
1945 case INDEX_op_mov_i64:
be0f34b5
RH
1946 case INDEX_op_setcond_i64:
1947 case INDEX_op_brcond_i64:
1948 case INDEX_op_ld8u_i64:
1949 case INDEX_op_ld8s_i64:
1950 case INDEX_op_ld16u_i64:
1951 case INDEX_op_ld16s_i64:
1952 case INDEX_op_ld32u_i64:
1953 case INDEX_op_ld32s_i64:
1954 case INDEX_op_ld_i64:
1955 case INDEX_op_st8_i64:
1956 case INDEX_op_st16_i64:
1957 case INDEX_op_st32_i64:
1958 case INDEX_op_st_i64:
1959 case INDEX_op_add_i64:
1960 case INDEX_op_sub_i64:
1961 case INDEX_op_mul_i64:
1962 case INDEX_op_and_i64:
1963 case INDEX_op_or_i64:
1964 case INDEX_op_xor_i64:
1965 case INDEX_op_shl_i64:
1966 case INDEX_op_shr_i64:
1967 case INDEX_op_sar_i64:
1968 case INDEX_op_ext_i32_i64:
1969 case INDEX_op_extu_i32_i64:
1970 return TCG_TARGET_REG_BITS == 64;
1971
1972 case INDEX_op_movcond_i64:
1973 return TCG_TARGET_HAS_movcond_i64;
1974 case INDEX_op_div_i64:
1975 case INDEX_op_divu_i64:
1976 return TCG_TARGET_HAS_div_i64;
1977 case INDEX_op_rem_i64:
1978 case INDEX_op_remu_i64:
1979 return TCG_TARGET_HAS_rem_i64;
1980 case INDEX_op_div2_i64:
1981 case INDEX_op_divu2_i64:
1982 return TCG_TARGET_HAS_div2_i64;
1983 case INDEX_op_rotl_i64:
1984 case INDEX_op_rotr_i64:
1985 return TCG_TARGET_HAS_rot_i64;
1986 case INDEX_op_deposit_i64:
1987 return TCG_TARGET_HAS_deposit_i64;
1988 case INDEX_op_extract_i64:
1989 return TCG_TARGET_HAS_extract_i64;
1990 case INDEX_op_sextract_i64:
1991 return TCG_TARGET_HAS_sextract_i64;
fce1296f
RH
1992 case INDEX_op_extract2_i64:
1993 return TCG_TARGET_HAS_extract2_i64;
be0f34b5
RH
1994 case INDEX_op_extrl_i64_i32:
1995 return TCG_TARGET_HAS_extrl_i64_i32;
1996 case INDEX_op_extrh_i64_i32:
1997 return TCG_TARGET_HAS_extrh_i64_i32;
1998 case INDEX_op_ext8s_i64:
1999 return TCG_TARGET_HAS_ext8s_i64;
2000 case INDEX_op_ext16s_i64:
2001 return TCG_TARGET_HAS_ext16s_i64;
2002 case INDEX_op_ext32s_i64:
2003 return TCG_TARGET_HAS_ext32s_i64;
2004 case INDEX_op_ext8u_i64:
2005 return TCG_TARGET_HAS_ext8u_i64;
2006 case INDEX_op_ext16u_i64:
2007 return TCG_TARGET_HAS_ext16u_i64;
2008 case INDEX_op_ext32u_i64:
2009 return TCG_TARGET_HAS_ext32u_i64;
2010 case INDEX_op_bswap16_i64:
2011 return TCG_TARGET_HAS_bswap16_i64;
2012 case INDEX_op_bswap32_i64:
2013 return TCG_TARGET_HAS_bswap32_i64;
2014 case INDEX_op_bswap64_i64:
2015 return TCG_TARGET_HAS_bswap64_i64;
2016 case INDEX_op_not_i64:
2017 return TCG_TARGET_HAS_not_i64;
2018 case INDEX_op_neg_i64:
2019 return TCG_TARGET_HAS_neg_i64;
2020 case INDEX_op_andc_i64:
2021 return TCG_TARGET_HAS_andc_i64;
2022 case INDEX_op_orc_i64:
2023 return TCG_TARGET_HAS_orc_i64;
2024 case INDEX_op_eqv_i64:
2025 return TCG_TARGET_HAS_eqv_i64;
2026 case INDEX_op_nand_i64:
2027 return TCG_TARGET_HAS_nand_i64;
2028 case INDEX_op_nor_i64:
2029 return TCG_TARGET_HAS_nor_i64;
2030 case INDEX_op_clz_i64:
2031 return TCG_TARGET_HAS_clz_i64;
2032 case INDEX_op_ctz_i64:
2033 return TCG_TARGET_HAS_ctz_i64;
2034 case INDEX_op_ctpop_i64:
2035 return TCG_TARGET_HAS_ctpop_i64;
2036 case INDEX_op_add2_i64:
2037 return TCG_TARGET_HAS_add2_i64;
2038 case INDEX_op_sub2_i64:
2039 return TCG_TARGET_HAS_sub2_i64;
2040 case INDEX_op_mulu2_i64:
2041 return TCG_TARGET_HAS_mulu2_i64;
2042 case INDEX_op_muls2_i64:
2043 return TCG_TARGET_HAS_muls2_i64;
2044 case INDEX_op_muluh_i64:
2045 return TCG_TARGET_HAS_muluh_i64;
2046 case INDEX_op_mulsh_i64:
2047 return TCG_TARGET_HAS_mulsh_i64;
2048
d2fd745f
RH
2049 case INDEX_op_mov_vec:
2050 case INDEX_op_dup_vec:
37ee55a0 2051 case INDEX_op_dupm_vec:
d2fd745f
RH
2052 case INDEX_op_ld_vec:
2053 case INDEX_op_st_vec:
2054 case INDEX_op_add_vec:
2055 case INDEX_op_sub_vec:
2056 case INDEX_op_and_vec:
2057 case INDEX_op_or_vec:
2058 case INDEX_op_xor_vec:
212be173 2059 case INDEX_op_cmp_vec:
d2fd745f
RH
2060 return have_vec;
2061 case INDEX_op_dup2_vec:
2062 return have_vec && TCG_TARGET_REG_BITS == 32;
2063 case INDEX_op_not_vec:
2064 return have_vec && TCG_TARGET_HAS_not_vec;
2065 case INDEX_op_neg_vec:
2066 return have_vec && TCG_TARGET_HAS_neg_vec;
bcefc902
RH
2067 case INDEX_op_abs_vec:
2068 return have_vec && TCG_TARGET_HAS_abs_vec;
d2fd745f
RH
2069 case INDEX_op_andc_vec:
2070 return have_vec && TCG_TARGET_HAS_andc_vec;
2071 case INDEX_op_orc_vec:
2072 return have_vec && TCG_TARGET_HAS_orc_vec;
ed523473
RH
2073 case INDEX_op_nand_vec:
2074 return have_vec && TCG_TARGET_HAS_nand_vec;
2075 case INDEX_op_nor_vec:
2076 return have_vec && TCG_TARGET_HAS_nor_vec;
2077 case INDEX_op_eqv_vec:
2078 return have_vec && TCG_TARGET_HAS_eqv_vec;
3774030a
RH
2079 case INDEX_op_mul_vec:
2080 return have_vec && TCG_TARGET_HAS_mul_vec;
d0ec9796
RH
2081 case INDEX_op_shli_vec:
2082 case INDEX_op_shri_vec:
2083 case INDEX_op_sari_vec:
2084 return have_vec && TCG_TARGET_HAS_shi_vec;
2085 case INDEX_op_shls_vec:
2086 case INDEX_op_shrs_vec:
2087 case INDEX_op_sars_vec:
2088 return have_vec && TCG_TARGET_HAS_shs_vec;
2089 case INDEX_op_shlv_vec:
2090 case INDEX_op_shrv_vec:
2091 case INDEX_op_sarv_vec:
2092 return have_vec && TCG_TARGET_HAS_shv_vec;
b0f7e744
RH
2093 case INDEX_op_rotli_vec:
2094 return have_vec && TCG_TARGET_HAS_roti_vec;
23850a74
RH
2095 case INDEX_op_rotls_vec:
2096 return have_vec && TCG_TARGET_HAS_rots_vec;
5d0ceda9
RH
2097 case INDEX_op_rotlv_vec:
2098 case INDEX_op_rotrv_vec:
2099 return have_vec && TCG_TARGET_HAS_rotv_vec;
8afaf050
RH
2100 case INDEX_op_ssadd_vec:
2101 case INDEX_op_usadd_vec:
2102 case INDEX_op_sssub_vec:
2103 case INDEX_op_ussub_vec:
2104 return have_vec && TCG_TARGET_HAS_sat_vec;
dd0a0fcd
RH
2105 case INDEX_op_smin_vec:
2106 case INDEX_op_umin_vec:
2107 case INDEX_op_smax_vec:
2108 case INDEX_op_umax_vec:
2109 return have_vec && TCG_TARGET_HAS_minmax_vec;
38dc1294
RH
2110 case INDEX_op_bitsel_vec:
2111 return have_vec && TCG_TARGET_HAS_bitsel_vec;
f75da298
RH
2112 case INDEX_op_cmpsel_vec:
2113 return have_vec && TCG_TARGET_HAS_cmpsel_vec;
d2fd745f 2114
db432672
RH
2115 default:
2116 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
2117 return true;
be0f34b5 2118 }
be0f34b5
RH
2119}
2120
39004a71
RH
2121static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs);
2122
ae8b75dc 2123void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)
c896fe29 2124{
3e92aa34 2125 const TCGHelperInfo *info;
39004a71
RH
2126 TCGv_i64 extend_free[MAX_CALL_IARGS];
2127 int n_extend = 0;
75e8b9b7 2128 TCGOp *op;
39004a71 2129 int i, n, pi = 0, total_args;
afb49896 2130
619205fd 2131 info = g_hash_table_lookup(helper_table, (gpointer)func);
39004a71
RH
2132 total_args = info->nr_out + info->nr_in + 2;
2133 op = tcg_op_alloc(INDEX_op_call, total_args);
2bece2c8 2134
38b47b19 2135#ifdef CONFIG_PLUGIN
17083f6f
EC
2136 /* Flag helpers that may affect guest state */
2137 if (tcg_ctx->plugin_insn &&
2138 !(info->flags & TCG_CALL_PLUGIN) &&
2139 !(info->flags & TCG_CALL_NO_SIDE_EFFECTS)) {
38b47b19
EC
2140 tcg_ctx->plugin_insn->calls_helpers = true;
2141 }
2142#endif
2143
39004a71
RH
2144 TCGOP_CALLO(op) = n = info->nr_out;
2145 switch (n) {
2146 case 0:
2147 tcg_debug_assert(ret == NULL);
2148 break;
2149 case 1:
2150 tcg_debug_assert(ret != NULL);
2151 op->args[pi++] = temp_arg(ret);
2152 break;
2153 case 2:
466d3759 2154 case 4:
39004a71 2155 tcg_debug_assert(ret != NULL);
466d3759 2156 tcg_debug_assert(ret->base_type == ret->type + ctz32(n));
39004a71 2157 tcg_debug_assert(ret->temp_subindex == 0);
466d3759
RH
2158 for (i = 0; i < n; ++i) {
2159 op->args[pi++] = temp_arg(ret + i);
2160 }
39004a71
RH
2161 break;
2162 default:
2163 g_assert_not_reached();
2164 }
2165
2166 TCGOP_CALLI(op) = n = info->nr_in;
2167 for (i = 0; i < n; i++) {
2168 const TCGCallArgumentLoc *loc = &info->in[i];
2169 TCGTemp *ts = args[loc->arg_idx] + loc->tmp_subindex;
2170
2171 switch (loc->kind) {
2172 case TCG_CALL_ARG_NORMAL:
313bdea8
RH
2173 case TCG_CALL_ARG_BY_REF:
2174 case TCG_CALL_ARG_BY_REF_N:
39004a71
RH
2175 op->args[pi++] = temp_arg(ts);
2176 break;
eb8b0224 2177
39004a71
RH
2178 case TCG_CALL_ARG_EXTEND_U:
2179 case TCG_CALL_ARG_EXTEND_S:
2180 {
5dd48602 2181 TCGv_i64 temp = tcg_temp_ebb_new_i64();
39004a71
RH
2182 TCGv_i32 orig = temp_tcgv_i32(ts);
2183
2184 if (loc->kind == TCG_CALL_ARG_EXTEND_S) {
eb8b0224
RH
2185 tcg_gen_ext_i32_i64(temp, orig);
2186 } else {
2187 tcg_gen_extu_i32_i64(temp, orig);
2188 }
39004a71
RH
2189 op->args[pi++] = tcgv_i64_arg(temp);
2190 extend_free[n_extend++] = temp;
2bece2c8 2191 }
e2a9dd6b 2192 break;
7b7d8b2d 2193
e2a9dd6b
RH
2194 default:
2195 g_assert_not_reached();
c896fe29
FB
2196 }
2197 }
75e8b9b7 2198 op->args[pi++] = (uintptr_t)func;
3e92aa34 2199 op->args[pi++] = (uintptr_t)info;
39004a71 2200 tcg_debug_assert(pi == total_args);
a7812ae4 2201
39004a71 2202 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
7319d83a 2203
39004a71
RH
2204 tcg_debug_assert(n_extend < ARRAY_SIZE(extend_free));
2205 for (i = 0; i < n_extend; ++i) {
2206 tcg_temp_free_i64(extend_free[i]);
2bece2c8 2207 }
c896fe29 2208}
c896fe29 2209
8fcd3692 2210static void tcg_reg_alloc_start(TCGContext *s)
c896fe29 2211{
ac3b8891 2212 int i, n;
ac3b8891 2213
ee17db83
RH
2214 for (i = 0, n = s->nb_temps; i < n; i++) {
2215 TCGTemp *ts = &s->temps[i];
2216 TCGTempVal val = TEMP_VAL_MEM;
2217
2218 switch (ts->kind) {
c0522136
RH
2219 case TEMP_CONST:
2220 val = TEMP_VAL_CONST;
2221 break;
ee17db83
RH
2222 case TEMP_FIXED:
2223 val = TEMP_VAL_REG;
2224 break;
2225 case TEMP_GLOBAL:
2226 break;
c7482438 2227 case TEMP_EBB:
ee17db83
RH
2228 val = TEMP_VAL_DEAD;
2229 /* fall through */
f57c6915 2230 case TEMP_TB:
ee17db83
RH
2231 ts->mem_allocated = 0;
2232 break;
2233 default:
2234 g_assert_not_reached();
2235 }
2236 ts->val_type = val;
e8996ee0 2237 }
f8b2f202
RH
2238
2239 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
c896fe29
FB
2240}
2241
f8b2f202
RH
2242static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
2243 TCGTemp *ts)
c896fe29 2244{
1807f4c4 2245 int idx = temp_idx(ts);
ac56dd48 2246
ee17db83
RH
2247 switch (ts->kind) {
2248 case TEMP_FIXED:
2249 case TEMP_GLOBAL:
ac56dd48 2250 pstrcpy(buf, buf_size, ts->name);
ee17db83 2251 break;
f57c6915 2252 case TEMP_TB:
f8b2f202 2253 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
ee17db83 2254 break;
c7482438 2255 case TEMP_EBB:
f8b2f202 2256 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
ee17db83 2257 break;
c0522136
RH
2258 case TEMP_CONST:
2259 switch (ts->type) {
2260 case TCG_TYPE_I32:
2261 snprintf(buf, buf_size, "$0x%x", (int32_t)ts->val);
2262 break;
2263#if TCG_TARGET_REG_BITS > 32
2264 case TCG_TYPE_I64:
2265 snprintf(buf, buf_size, "$0x%" PRIx64, ts->val);
2266 break;
2267#endif
2268 case TCG_TYPE_V64:
2269 case TCG_TYPE_V128:
2270 case TCG_TYPE_V256:
2271 snprintf(buf, buf_size, "v%d$0x%" PRIx64,
2272 64 << (ts->type - TCG_TYPE_V64), ts->val);
2273 break;
2274 default:
2275 g_assert_not_reached();
2276 }
2277 break;
c896fe29
FB
2278 }
2279 return buf;
2280}
2281
43439139
RH
2282static char *tcg_get_arg_str(TCGContext *s, char *buf,
2283 int buf_size, TCGArg arg)
f8b2f202 2284{
43439139 2285 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
f8b2f202
RH
2286}
2287
f48f3ede
BS
2288static const char * const cond_name[] =
2289{
0aed257f
RH
2290 [TCG_COND_NEVER] = "never",
2291 [TCG_COND_ALWAYS] = "always",
f48f3ede
BS
2292 [TCG_COND_EQ] = "eq",
2293 [TCG_COND_NE] = "ne",
2294 [TCG_COND_LT] = "lt",
2295 [TCG_COND_GE] = "ge",
2296 [TCG_COND_LE] = "le",
2297 [TCG_COND_GT] = "gt",
2298 [TCG_COND_LTU] = "ltu",
2299 [TCG_COND_GEU] = "geu",
2300 [TCG_COND_LEU] = "leu",
2301 [TCG_COND_GTU] = "gtu"
2302};
2303
12fde9bc 2304static const char * const ldst_name[(MO_BSWAP | MO_SSIZE) + 1] =
f713d6ad
RH
2305{
2306 [MO_UB] = "ub",
2307 [MO_SB] = "sb",
2308 [MO_LEUW] = "leuw",
2309 [MO_LESW] = "lesw",
2310 [MO_LEUL] = "leul",
2311 [MO_LESL] = "lesl",
fc313c64 2312 [MO_LEUQ] = "leq",
f713d6ad
RH
2313 [MO_BEUW] = "beuw",
2314 [MO_BESW] = "besw",
2315 [MO_BEUL] = "beul",
2316 [MO_BESL] = "besl",
fc313c64 2317 [MO_BEUQ] = "beq",
12fde9bc
RH
2318 [MO_128 + MO_BE] = "beo",
2319 [MO_128 + MO_LE] = "leo",
f713d6ad
RH
2320};
2321
1f00b27f 2322static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
1f00b27f 2323 [MO_UNALN >> MO_ASHIFT] = "un+",
1f00b27f 2324 [MO_ALIGN >> MO_ASHIFT] = "al+",
1f00b27f
SS
2325 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
2326 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
2327 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
2328 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
2329 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
2330 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
2331};
2332
37031fef
RH
2333static const char * const atom_name[(MO_ATOM_MASK >> MO_ATOM_SHIFT) + 1] = {
2334 [MO_ATOM_IFALIGN >> MO_ATOM_SHIFT] = "",
2335 [MO_ATOM_IFALIGN_PAIR >> MO_ATOM_SHIFT] = "pair+",
2336 [MO_ATOM_WITHIN16 >> MO_ATOM_SHIFT] = "w16+",
2337 [MO_ATOM_WITHIN16_PAIR >> MO_ATOM_SHIFT] = "w16p+",
2338 [MO_ATOM_SUBALIGN >> MO_ATOM_SHIFT] = "sub+",
2339 [MO_ATOM_NONE >> MO_ATOM_SHIFT] = "noat+",
2340};
2341
587195bd
RH
2342static const char bswap_flag_name[][6] = {
2343 [TCG_BSWAP_IZ] = "iz",
2344 [TCG_BSWAP_OZ] = "oz",
2345 [TCG_BSWAP_OS] = "os",
2346 [TCG_BSWAP_IZ | TCG_BSWAP_OZ] = "iz,oz",
2347 [TCG_BSWAP_IZ | TCG_BSWAP_OS] = "iz,os",
2348};
2349
b016486e
RH
2350static inline bool tcg_regset_single(TCGRegSet d)
2351{
2352 return (d & (d - 1)) == 0;
2353}
2354
2355static inline TCGReg tcg_regset_first(TCGRegSet d)
2356{
2357 if (TCG_TARGET_NB_REGS <= 32) {
2358 return ctz32(d);
2359 } else {
2360 return ctz64(d);
2361 }
2362}
2363
b7a83ff8
RH
2364/* Return only the number of characters output -- no error return. */
2365#define ne_fprintf(...) \
2366 ({ int ret_ = fprintf(__VA_ARGS__); ret_ >= 0 ? ret_ : 0; })
2367
2368static void tcg_dump_ops(TCGContext *s, FILE *f, bool have_prefs)
c896fe29 2369{
c896fe29 2370 char buf[128];
c45cb8bb 2371 TCGOp *op;
c45cb8bb 2372
15fa08f8 2373 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb
RH
2374 int i, k, nb_oargs, nb_iargs, nb_cargs;
2375 const TCGOpDef *def;
c45cb8bb 2376 TCGOpcode c;
bdfb460e 2377 int col = 0;
c896fe29 2378
c45cb8bb 2379 c = op->opc;
c896fe29 2380 def = &tcg_op_defs[c];
c45cb8bb 2381
765b842a 2382 if (c == INDEX_op_insn_start) {
b016486e 2383 nb_oargs = 0;
b7a83ff8 2384 col += ne_fprintf(f, "\n ----");
9aef40ed
RH
2385
2386 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
c9ad8d27
RH
2387 col += ne_fprintf(f, " %016" PRIx64,
2388 tcg_get_insn_start_param(op, i));
eeacee4d 2389 }
7e4597d7 2390 } else if (c == INDEX_op_call) {
3e92aa34 2391 const TCGHelperInfo *info = tcg_call_info(op);
fa52e660 2392 void *func = tcg_call_func(op);
3e92aa34 2393
c896fe29 2394 /* variable number of arguments */
cd9090aa
RH
2395 nb_oargs = TCGOP_CALLO(op);
2396 nb_iargs = TCGOP_CALLI(op);
c896fe29 2397 nb_cargs = def->nb_cargs;
c896fe29 2398
b7a83ff8 2399 col += ne_fprintf(f, " %s ", def->name);
3e92aa34
RH
2400
2401 /*
2402 * Print the function name from TCGHelperInfo, if available.
2403 * Note that plugins have a template function for the info,
2404 * but the actual function pointer comes from the plugin.
2405 */
3e92aa34 2406 if (func == info->func) {
b7a83ff8 2407 col += ne_fprintf(f, "%s", info->name);
3e92aa34 2408 } else {
b7a83ff8 2409 col += ne_fprintf(f, "plugin(%p)", func);
3e92aa34
RH
2410 }
2411
b7a83ff8 2412 col += ne_fprintf(f, ",$0x%x,$%d", info->flags, nb_oargs);
cf066674 2413 for (i = 0; i < nb_oargs; i++) {
b7a83ff8
RH
2414 col += ne_fprintf(f, ",%s", tcg_get_arg_str(s, buf, sizeof(buf),
2415 op->args[i]));
b03cce8e 2416 }
cf066674 2417 for (i = 0; i < nb_iargs; i++) {
efee3746 2418 TCGArg arg = op->args[nb_oargs + i];
39004a71 2419 const char *t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
b7a83ff8 2420 col += ne_fprintf(f, ",%s", t);
e8996ee0 2421 }
b03cce8e 2422 } else {
b7a83ff8 2423 col += ne_fprintf(f, " %s ", def->name);
c45cb8bb
RH
2424
2425 nb_oargs = def->nb_oargs;
2426 nb_iargs = def->nb_iargs;
2427 nb_cargs = def->nb_cargs;
2428
d2fd745f 2429 if (def->flags & TCG_OPF_VECTOR) {
b7a83ff8
RH
2430 col += ne_fprintf(f, "v%d,e%d,", 64 << TCGOP_VECL(op),
2431 8 << TCGOP_VECE(op));
d2fd745f
RH
2432 }
2433
b03cce8e 2434 k = 0;
c45cb8bb 2435 for (i = 0; i < nb_oargs; i++) {
b7a83ff8
RH
2436 const char *sep = k ? "," : "";
2437 col += ne_fprintf(f, "%s%s", sep,
2438 tcg_get_arg_str(s, buf, sizeof(buf),
2439 op->args[k++]));
b03cce8e 2440 }
c45cb8bb 2441 for (i = 0; i < nb_iargs; i++) {
b7a83ff8
RH
2442 const char *sep = k ? "," : "";
2443 col += ne_fprintf(f, "%s%s", sep,
2444 tcg_get_arg_str(s, buf, sizeof(buf),
2445 op->args[k++]));
b03cce8e 2446 }
be210acb
RH
2447 switch (c) {
2448 case INDEX_op_brcond_i32:
be210acb 2449 case INDEX_op_setcond_i32:
ffc5ea09 2450 case INDEX_op_movcond_i32:
ffc5ea09 2451 case INDEX_op_brcond2_i32:
be210acb 2452 case INDEX_op_setcond2_i32:
ffc5ea09 2453 case INDEX_op_brcond_i64:
be210acb 2454 case INDEX_op_setcond_i64:
ffc5ea09 2455 case INDEX_op_movcond_i64:
212be173 2456 case INDEX_op_cmp_vec:
f75da298 2457 case INDEX_op_cmpsel_vec:
efee3746
RH
2458 if (op->args[k] < ARRAY_SIZE(cond_name)
2459 && cond_name[op->args[k]]) {
b7a83ff8 2460 col += ne_fprintf(f, ",%s", cond_name[op->args[k++]]);
eeacee4d 2461 } else {
b7a83ff8 2462 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, op->args[k++]);
eeacee4d 2463 }
f48f3ede 2464 i = 1;
be210acb 2465 break;
f713d6ad
RH
2466 case INDEX_op_qemu_ld_i32:
2467 case INDEX_op_qemu_st_i32:
07ce0b05 2468 case INDEX_op_qemu_st8_i32:
f713d6ad
RH
2469 case INDEX_op_qemu_ld_i64:
2470 case INDEX_op_qemu_st_i64:
12fde9bc
RH
2471 case INDEX_op_qemu_ld_i128:
2472 case INDEX_op_qemu_st_i128:
59227d5d 2473 {
37031fef 2474 const char *s_al, *s_op, *s_at;
9002ffcb 2475 MemOpIdx oi = op->args[k++];
14776ab5 2476 MemOp op = get_memop(oi);
59227d5d
RH
2477 unsigned ix = get_mmuidx(oi);
2478
37031fef
RH
2479 s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT];
2480 s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
2481 s_at = atom_name[(op & MO_ATOM_MASK) >> MO_ATOM_SHIFT];
2482 op &= ~(MO_AMASK | MO_BSWAP | MO_SSIZE | MO_ATOM_MASK);
2483
2484 /* If all fields are accounted for, print symbolically. */
2485 if (!op && s_al && s_op && s_at) {
2486 col += ne_fprintf(f, ",%s%s%s,%u",
2487 s_at, s_al, s_op, ix);
59c4b7e8 2488 } else {
37031fef
RH
2489 op = get_memop(oi);
2490 col += ne_fprintf(f, ",$0x%x,%u", op, ix);
59227d5d
RH
2491 }
2492 i = 1;
f713d6ad 2493 }
f713d6ad 2494 break;
587195bd
RH
2495 case INDEX_op_bswap16_i32:
2496 case INDEX_op_bswap16_i64:
2497 case INDEX_op_bswap32_i32:
2498 case INDEX_op_bswap32_i64:
2499 case INDEX_op_bswap64_i64:
2500 {
2501 TCGArg flags = op->args[k];
2502 const char *name = NULL;
2503
2504 if (flags < ARRAY_SIZE(bswap_flag_name)) {
2505 name = bswap_flag_name[flags];
2506 }
2507 if (name) {
b7a83ff8 2508 col += ne_fprintf(f, ",%s", name);
587195bd 2509 } else {
b7a83ff8 2510 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, flags);
587195bd
RH
2511 }
2512 i = k = 1;
2513 }
2514 break;
be210acb 2515 default:
f48f3ede 2516 i = 0;
be210acb
RH
2517 break;
2518 }
51e3972c
RH
2519 switch (c) {
2520 case INDEX_op_set_label:
2521 case INDEX_op_br:
2522 case INDEX_op_brcond_i32:
2523 case INDEX_op_brcond_i64:
2524 case INDEX_op_brcond2_i32:
b7a83ff8
RH
2525 col += ne_fprintf(f, "%s$L%d", k ? "," : "",
2526 arg_label(op->args[k])->id);
51e3972c
RH
2527 i++, k++;
2528 break;
3470867b
RH
2529 case INDEX_op_mb:
2530 {
2531 TCGBar membar = op->args[k];
2532 const char *b_op, *m_op;
2533
2534 switch (membar & TCG_BAR_SC) {
2535 case 0:
2536 b_op = "none";
2537 break;
2538 case TCG_BAR_LDAQ:
2539 b_op = "acq";
2540 break;
2541 case TCG_BAR_STRL:
2542 b_op = "rel";
2543 break;
2544 case TCG_BAR_SC:
2545 b_op = "seq";
2546 break;
2547 default:
2548 g_assert_not_reached();
2549 }
2550
2551 switch (membar & TCG_MO_ALL) {
2552 case 0:
2553 m_op = "none";
2554 break;
2555 case TCG_MO_LD_LD:
2556 m_op = "rr";
2557 break;
2558 case TCG_MO_LD_ST:
2559 m_op = "rw";
2560 break;
2561 case TCG_MO_ST_LD:
2562 m_op = "wr";
2563 break;
2564 case TCG_MO_ST_ST:
2565 m_op = "ww";
2566 break;
2567 case TCG_MO_LD_LD | TCG_MO_LD_ST:
2568 m_op = "rr+rw";
2569 break;
2570 case TCG_MO_LD_LD | TCG_MO_ST_LD:
2571 m_op = "rr+wr";
2572 break;
2573 case TCG_MO_LD_LD | TCG_MO_ST_ST:
2574 m_op = "rr+ww";
2575 break;
2576 case TCG_MO_LD_ST | TCG_MO_ST_LD:
2577 m_op = "rw+wr";
2578 break;
2579 case TCG_MO_LD_ST | TCG_MO_ST_ST:
2580 m_op = "rw+ww";
2581 break;
2582 case TCG_MO_ST_LD | TCG_MO_ST_ST:
2583 m_op = "wr+ww";
2584 break;
2585 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_LD:
2586 m_op = "rr+rw+wr";
2587 break;
2588 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST:
2589 m_op = "rr+rw+ww";
2590 break;
2591 case TCG_MO_LD_LD | TCG_MO_ST_LD | TCG_MO_ST_ST:
2592 m_op = "rr+wr+ww";
2593 break;
2594 case TCG_MO_LD_ST | TCG_MO_ST_LD | TCG_MO_ST_ST:
2595 m_op = "rw+wr+ww";
2596 break;
2597 case TCG_MO_ALL:
2598 m_op = "all";
2599 break;
2600 default:
2601 g_assert_not_reached();
2602 }
2603
2604 col += ne_fprintf(f, "%s%s:%s", (k ? "," : ""), b_op, m_op);
2605 i++, k++;
2606 }
2607 break;
51e3972c
RH
2608 default:
2609 break;
2610 }
2611 for (; i < nb_cargs; i++, k++) {
b7a83ff8
RH
2612 col += ne_fprintf(f, "%s$0x%" TCG_PRIlx, k ? "," : "",
2613 op->args[k]);
bdfb460e
RH
2614 }
2615 }
bdfb460e 2616
1894f69a 2617 if (have_prefs || op->life) {
b7a83ff8
RH
2618 for (; col < 40; ++col) {
2619 putc(' ', f);
bdfb460e 2620 }
1894f69a
RH
2621 }
2622
2623 if (op->life) {
2624 unsigned life = op->life;
bdfb460e
RH
2625
2626 if (life & (SYNC_ARG * 3)) {
b7a83ff8 2627 ne_fprintf(f, " sync:");
bdfb460e
RH
2628 for (i = 0; i < 2; ++i) {
2629 if (life & (SYNC_ARG << i)) {
b7a83ff8 2630 ne_fprintf(f, " %d", i);
bdfb460e
RH
2631 }
2632 }
2633 }
2634 life /= DEAD_ARG;
2635 if (life) {
b7a83ff8 2636 ne_fprintf(f, " dead:");
bdfb460e
RH
2637 for (i = 0; life; ++i, life >>= 1) {
2638 if (life & 1) {
b7a83ff8 2639 ne_fprintf(f, " %d", i);
bdfb460e
RH
2640 }
2641 }
b03cce8e 2642 }
c896fe29 2643 }
1894f69a
RH
2644
2645 if (have_prefs) {
2646 for (i = 0; i < nb_oargs; ++i) {
31fd884b 2647 TCGRegSet set = output_pref(op, i);
1894f69a
RH
2648
2649 if (i == 0) {
b7a83ff8 2650 ne_fprintf(f, " pref=");
1894f69a 2651 } else {
b7a83ff8 2652 ne_fprintf(f, ",");
1894f69a
RH
2653 }
2654 if (set == 0) {
b7a83ff8 2655 ne_fprintf(f, "none");
1894f69a 2656 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
b7a83ff8 2657 ne_fprintf(f, "all");
1894f69a
RH
2658#ifdef CONFIG_DEBUG_TCG
2659 } else if (tcg_regset_single(set)) {
2660 TCGReg reg = tcg_regset_first(set);
b7a83ff8 2661 ne_fprintf(f, "%s", tcg_target_reg_names[reg]);
1894f69a
RH
2662#endif
2663 } else if (TCG_TARGET_NB_REGS <= 32) {
b7a83ff8 2664 ne_fprintf(f, "0x%x", (uint32_t)set);
1894f69a 2665 } else {
b7a83ff8 2666 ne_fprintf(f, "0x%" PRIx64, (uint64_t)set);
1894f69a
RH
2667 }
2668 }
2669 }
2670
b7a83ff8 2671 putc('\n', f);
c896fe29
FB
2672 }
2673}
2674
2675/* we give more priority to constraints with less registers */
2676static int get_constraint_priority(const TCGOpDef *def, int k)
2677{
74a11790 2678 const TCGArgConstraint *arg_ct = &def->args_ct[k];
29f5e925 2679 int n = ctpop64(arg_ct->regs);
c896fe29 2680
29f5e925
RH
2681 /*
2682 * Sort constraints of a single register first, which includes output
2683 * aliases (which must exactly match the input already allocated).
2684 */
2685 if (n == 1 || arg_ct->oalias) {
2686 return INT_MAX;
2687 }
2688
2689 /*
2690 * Sort register pairs next, first then second immediately after.
2691 * Arbitrarily sort multiple pairs by the index of the first reg;
2692 * there shouldn't be many pairs.
2693 */
2694 switch (arg_ct->pair) {
2695 case 1:
2696 case 3:
2697 return (k + 1) * 2;
2698 case 2:
2699 return (arg_ct->pair_index + 1) * 2 - 1;
c896fe29 2700 }
29f5e925
RH
2701
2702 /* Finally, sort by decreasing register count. */
2703 assert(n > 1);
2704 return -n;
c896fe29
FB
2705}
2706
2707/* sort from highest priority to lowest */
2708static void sort_constraints(TCGOpDef *def, int start, int n)
2709{
66792f90
RH
2710 int i, j;
2711 TCGArgConstraint *a = def->args_ct;
c896fe29 2712
66792f90
RH
2713 for (i = 0; i < n; i++) {
2714 a[start + i].sort_index = start + i;
2715 }
2716 if (n <= 1) {
c896fe29 2717 return;
66792f90
RH
2718 }
2719 for (i = 0; i < n - 1; i++) {
2720 for (j = i + 1; j < n; j++) {
2721 int p1 = get_constraint_priority(def, a[start + i].sort_index);
2722 int p2 = get_constraint_priority(def, a[start + j].sort_index);
c896fe29 2723 if (p1 < p2) {
66792f90
RH
2724 int tmp = a[start + i].sort_index;
2725 a[start + i].sort_index = a[start + j].sort_index;
2726 a[start + j].sort_index = tmp;
c896fe29
FB
2727 }
2728 }
2729 }
2730}
2731
f69d277e 2732static void process_op_defs(TCGContext *s)
c896fe29 2733{
a9751609 2734 TCGOpcode op;
c896fe29 2735
f69d277e
RH
2736 for (op = 0; op < NB_OPS; op++) {
2737 TCGOpDef *def = &tcg_op_defs[op];
2738 const TCGTargetOpDef *tdefs;
29f5e925
RH
2739 bool saw_alias_pair = false;
2740 int i, o, i2, o2, nb_args;
f69d277e
RH
2741
2742 if (def->flags & TCG_OPF_NOT_PRESENT) {
2743 continue;
2744 }
2745
c896fe29 2746 nb_args = def->nb_iargs + def->nb_oargs;
f69d277e
RH
2747 if (nb_args == 0) {
2748 continue;
2749 }
2750
4c22e840
RH
2751 /*
2752 * Macro magic should make it impossible, but double-check that
2753 * the array index is in range. Since the signness of an enum
2754 * is implementation defined, force the result to unsigned.
2755 */
2756 unsigned con_set = tcg_target_op_def(op);
2757 tcg_debug_assert(con_set < ARRAY_SIZE(constraint_sets));
2758 tdefs = &constraint_sets[con_set];
f69d277e
RH
2759
2760 for (i = 0; i < nb_args; i++) {
2761 const char *ct_str = tdefs->args_ct_str[i];
8940ea0d
PMD
2762 bool input_p = i >= def->nb_oargs;
2763
f69d277e 2764 /* Incomplete TCGTargetOpDef entry. */
eabb7b91 2765 tcg_debug_assert(ct_str != NULL);
f69d277e 2766
8940ea0d
PMD
2767 switch (*ct_str) {
2768 case '0' ... '9':
2769 o = *ct_str - '0';
2770 tcg_debug_assert(input_p);
2771 tcg_debug_assert(o < def->nb_oargs);
2772 tcg_debug_assert(def->args_ct[o].regs != 0);
2773 tcg_debug_assert(!def->args_ct[o].oalias);
2774 def->args_ct[i] = def->args_ct[o];
2775 /* The output sets oalias. */
2776 def->args_ct[o].oalias = 1;
2777 def->args_ct[o].alias_index = i;
2778 /* The input sets ialias. */
2779 def->args_ct[i].ialias = 1;
2780 def->args_ct[i].alias_index = o;
29f5e925
RH
2781 if (def->args_ct[i].pair) {
2782 saw_alias_pair = true;
2783 }
8940ea0d
PMD
2784 tcg_debug_assert(ct_str[1] == '\0');
2785 continue;
2786
2787 case '&':
2788 tcg_debug_assert(!input_p);
2789 def->args_ct[i].newreg = true;
2790 ct_str++;
2791 break;
29f5e925
RH
2792
2793 case 'p': /* plus */
2794 /* Allocate to the register after the previous. */
2795 tcg_debug_assert(i > (input_p ? def->nb_oargs : 0));
2796 o = i - 1;
2797 tcg_debug_assert(!def->args_ct[o].pair);
2798 tcg_debug_assert(!def->args_ct[o].ct);
2799 def->args_ct[i] = (TCGArgConstraint){
2800 .pair = 2,
2801 .pair_index = o,
2802 .regs = def->args_ct[o].regs << 1,
2803 };
2804 def->args_ct[o].pair = 1;
2805 def->args_ct[o].pair_index = i;
2806 tcg_debug_assert(ct_str[1] == '\0');
2807 continue;
2808
2809 case 'm': /* minus */
2810 /* Allocate to the register before the previous. */
2811 tcg_debug_assert(i > (input_p ? def->nb_oargs : 0));
2812 o = i - 1;
2813 tcg_debug_assert(!def->args_ct[o].pair);
2814 tcg_debug_assert(!def->args_ct[o].ct);
2815 def->args_ct[i] = (TCGArgConstraint){
2816 .pair = 1,
2817 .pair_index = o,
2818 .regs = def->args_ct[o].regs >> 1,
2819 };
2820 def->args_ct[o].pair = 2;
2821 def->args_ct[o].pair_index = i;
2822 tcg_debug_assert(ct_str[1] == '\0');
2823 continue;
8940ea0d
PMD
2824 }
2825
2826 do {
2827 switch (*ct_str) {
17280ff4
RH
2828 case 'i':
2829 def->args_ct[i].ct |= TCG_CT_CONST;
17280ff4 2830 break;
358b4923 2831
358b4923
RH
2832 /* Include all of the target-specific constraints. */
2833
2834#undef CONST
2835#define CONST(CASE, MASK) \
8940ea0d 2836 case CASE: def->args_ct[i].ct |= MASK; break;
358b4923 2837#define REGS(CASE, MASK) \
8940ea0d 2838 case CASE: def->args_ct[i].regs |= MASK; break;
358b4923
RH
2839
2840#include "tcg-target-con-str.h"
2841
2842#undef REGS
2843#undef CONST
17280ff4 2844 default:
8940ea0d
PMD
2845 case '0' ... '9':
2846 case '&':
29f5e925
RH
2847 case 'p':
2848 case 'm':
17280ff4 2849 /* Typo in TCGTargetOpDef constraint. */
358b4923 2850 g_assert_not_reached();
c896fe29 2851 }
8940ea0d 2852 } while (*++ct_str != '\0');
c896fe29
FB
2853 }
2854
c68aaa18 2855 /* TCGTargetOpDef entry with too much information? */
eabb7b91 2856 tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
c68aaa18 2857
29f5e925
RH
2858 /*
2859 * Fix up output pairs that are aliased with inputs.
2860 * When we created the alias, we copied pair from the output.
2861 * There are three cases:
2862 * (1a) Pairs of inputs alias pairs of outputs.
2863 * (1b) One input aliases the first of a pair of outputs.
2864 * (2) One input aliases the second of a pair of outputs.
2865 *
2866 * Case 1a is handled by making sure that the pair_index'es are
2867 * properly updated so that they appear the same as a pair of inputs.
2868 *
2869 * Case 1b is handled by setting the pair_index of the input to
2870 * itself, simply so it doesn't point to an unrelated argument.
2871 * Since we don't encounter the "second" during the input allocation
2872 * phase, nothing happens with the second half of the input pair.
2873 *
2874 * Case 2 is handled by setting the second input to pair=3, the
2875 * first output to pair=3, and the pair_index'es to match.
2876 */
2877 if (saw_alias_pair) {
2878 for (i = def->nb_oargs; i < nb_args; i++) {
2879 /*
2880 * Since [0-9pm] must be alone in the constraint string,
2881 * the only way they can both be set is if the pair comes
2882 * from the output alias.
2883 */
2884 if (!def->args_ct[i].ialias) {
2885 continue;
2886 }
2887 switch (def->args_ct[i].pair) {
2888 case 0:
2889 break;
2890 case 1:
2891 o = def->args_ct[i].alias_index;
2892 o2 = def->args_ct[o].pair_index;
2893 tcg_debug_assert(def->args_ct[o].pair == 1);
2894 tcg_debug_assert(def->args_ct[o2].pair == 2);
2895 if (def->args_ct[o2].oalias) {
2896 /* Case 1a */
2897 i2 = def->args_ct[o2].alias_index;
2898 tcg_debug_assert(def->args_ct[i2].pair == 2);
2899 def->args_ct[i2].pair_index = i;
2900 def->args_ct[i].pair_index = i2;
2901 } else {
2902 /* Case 1b */
2903 def->args_ct[i].pair_index = i;
2904 }
2905 break;
2906 case 2:
2907 o = def->args_ct[i].alias_index;
2908 o2 = def->args_ct[o].pair_index;
2909 tcg_debug_assert(def->args_ct[o].pair == 2);
2910 tcg_debug_assert(def->args_ct[o2].pair == 1);
2911 if (def->args_ct[o2].oalias) {
2912 /* Case 1a */
2913 i2 = def->args_ct[o2].alias_index;
2914 tcg_debug_assert(def->args_ct[i2].pair == 1);
2915 def->args_ct[i2].pair_index = i;
2916 def->args_ct[i].pair_index = i2;
2917 } else {
2918 /* Case 2 */
2919 def->args_ct[i].pair = 3;
2920 def->args_ct[o2].pair = 3;
2921 def->args_ct[i].pair_index = o2;
2922 def->args_ct[o2].pair_index = i;
2923 }
2924 break;
2925 default:
2926 g_assert_not_reached();
2927 }
2928 }
2929 }
2930
c896fe29
FB
2931 /* sort the constraints (XXX: this is just an heuristic) */
2932 sort_constraints(def, 0, def->nb_oargs);
2933 sort_constraints(def, def->nb_oargs, def->nb_iargs);
a9751609 2934 }
c896fe29
FB
2935}
2936
f85b1fc4 2937static void remove_label_use(TCGOp *op, int idx)
0c627cdc 2938{
f85b1fc4
RH
2939 TCGLabel *label = arg_label(op->args[idx]);
2940 TCGLabelUse *use;
d88a117e 2941
f85b1fc4
RH
2942 QSIMPLEQ_FOREACH(use, &label->branches, next) {
2943 if (use->op == op) {
2944 QSIMPLEQ_REMOVE(&label->branches, use, TCGLabelUse, next);
2945 return;
2946 }
2947 }
2948 g_assert_not_reached();
2949}
2950
2951void tcg_op_remove(TCGContext *s, TCGOp *op)
2952{
d88a117e
RH
2953 switch (op->opc) {
2954 case INDEX_op_br:
f85b1fc4 2955 remove_label_use(op, 0);
d88a117e
RH
2956 break;
2957 case INDEX_op_brcond_i32:
2958 case INDEX_op_brcond_i64:
f85b1fc4 2959 remove_label_use(op, 3);
d88a117e
RH
2960 break;
2961 case INDEX_op_brcond2_i32:
f85b1fc4 2962 remove_label_use(op, 5);
d88a117e
RH
2963 break;
2964 default:
2965 break;
2966 }
2967
15fa08f8
RH
2968 QTAILQ_REMOVE(&s->ops, op, link);
2969 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
abebf925 2970 s->nb_ops--;
0c627cdc
RH
2971
2972#ifdef CONFIG_PROFILER
d73415a3 2973 qatomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
0c627cdc
RH
2974#endif
2975}
2976
a80cdd31
RH
2977void tcg_remove_ops_after(TCGOp *op)
2978{
2979 TCGContext *s = tcg_ctx;
2980
2981 while (true) {
2982 TCGOp *last = tcg_last_op();
2983 if (last == op) {
2984 return;
2985 }
2986 tcg_op_remove(s, last);
2987 }
2988}
2989
d4478943 2990static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs)
5a18407f 2991{
15fa08f8 2992 TCGContext *s = tcg_ctx;
cb10bc63
RH
2993 TCGOp *op = NULL;
2994
2995 if (unlikely(!QTAILQ_EMPTY(&s->free_ops))) {
2996 QTAILQ_FOREACH(op, &s->free_ops, link) {
2997 if (nargs <= op->nargs) {
2998 QTAILQ_REMOVE(&s->free_ops, op, link);
2999 nargs = op->nargs;
3000 goto found;
3001 }
3002 }
15fa08f8 3003 }
cb10bc63
RH
3004
3005 /* Most opcodes have 3 or 4 operands: reduce fragmentation. */
3006 nargs = MAX(4, nargs);
3007 op = tcg_malloc(sizeof(TCGOp) + sizeof(TCGArg) * nargs);
3008
3009 found:
15fa08f8
RH
3010 memset(op, 0, offsetof(TCGOp, link));
3011 op->opc = opc;
cb10bc63
RH
3012 op->nargs = nargs;
3013
3014 /* Check for bitfield overflow. */
3015 tcg_debug_assert(op->nargs == nargs);
5a18407f 3016
cb10bc63 3017 s->nb_ops++;
15fa08f8
RH
3018 return op;
3019}
3020
d4478943 3021TCGOp *tcg_emit_op(TCGOpcode opc, unsigned nargs)
15fa08f8 3022{
d4478943 3023 TCGOp *op = tcg_op_alloc(opc, nargs);
15fa08f8
RH
3024 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
3025 return op;
3026}
5a18407f 3027
d4478943
PMD
3028TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op,
3029 TCGOpcode opc, unsigned nargs)
15fa08f8 3030{
d4478943 3031 TCGOp *new_op = tcg_op_alloc(opc, nargs);
15fa08f8 3032 QTAILQ_INSERT_BEFORE(old_op, new_op, link);
5a18407f
RH
3033 return new_op;
3034}
3035
d4478943
PMD
3036TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op,
3037 TCGOpcode opc, unsigned nargs)
5a18407f 3038{
d4478943 3039 TCGOp *new_op = tcg_op_alloc(opc, nargs);
15fa08f8 3040 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
5a18407f
RH
3041 return new_op;
3042}
3043
968f305e
RH
3044static void move_label_uses(TCGLabel *to, TCGLabel *from)
3045{
3046 TCGLabelUse *u;
3047
3048 QSIMPLEQ_FOREACH(u, &from->branches, next) {
3049 TCGOp *op = u->op;
3050 switch (op->opc) {
3051 case INDEX_op_br:
3052 op->args[0] = label_arg(to);
3053 break;
3054 case INDEX_op_brcond_i32:
3055 case INDEX_op_brcond_i64:
3056 op->args[3] = label_arg(to);
3057 break;
3058 case INDEX_op_brcond2_i32:
3059 op->args[5] = label_arg(to);
3060 break;
3061 default:
3062 g_assert_not_reached();
3063 }
3064 }
3065
3066 QSIMPLEQ_CONCAT(&to->branches, &from->branches);
3067}
3068
b4fc67c7 3069/* Reachable analysis : remove unreachable code. */
9bbee4c0
RH
3070static void __attribute__((noinline))
3071reachable_code_pass(TCGContext *s)
b4fc67c7 3072{
4d89d0bb 3073 TCGOp *op, *op_next, *op_prev;
b4fc67c7
RH
3074 bool dead = false;
3075
3076 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
3077 bool remove = dead;
3078 TCGLabel *label;
b4fc67c7
RH
3079
3080 switch (op->opc) {
3081 case INDEX_op_set_label:
3082 label = arg_label(op->args[0]);
4d89d0bb 3083
968f305e
RH
3084 /*
3085 * Note that the first op in the TB is always a load,
3086 * so there is always something before a label.
3087 */
3088 op_prev = QTAILQ_PREV(op, link);
3089
3090 /*
3091 * If we find two sequential labels, move all branches to
3092 * reference the second label and remove the first label.
3093 * Do this before branch to next optimization, so that the
3094 * middle label is out of the way.
3095 */
3096 if (op_prev->opc == INDEX_op_set_label) {
3097 move_label_uses(label, arg_label(op_prev->args[0]));
3098 tcg_op_remove(s, op_prev);
3099 op_prev = QTAILQ_PREV(op, link);
3100 }
3101
4d89d0bb
RH
3102 /*
3103 * Optimization can fold conditional branches to unconditional.
3104 * If we find a label which is preceded by an unconditional
3105 * branch to next, remove the branch. We couldn't do this when
3106 * processing the branch because any dead code between the branch
3107 * and label had not yet been removed.
3108 */
4d89d0bb
RH
3109 if (op_prev->opc == INDEX_op_br &&
3110 label == arg_label(op_prev->args[0])) {
3111 tcg_op_remove(s, op_prev);
3112 /* Fall through means insns become live again. */
3113 dead = false;
3114 }
3115
f85b1fc4 3116 if (QSIMPLEQ_EMPTY(&label->branches)) {
b4fc67c7
RH
3117 /*
3118 * While there is an occasional backward branch, virtually
3119 * all branches generated by the translators are forward.
3120 * Which means that generally we will have already removed
3121 * all references to the label that will be, and there is
3122 * little to be gained by iterating.
3123 */
3124 remove = true;
3125 } else {
3126 /* Once we see a label, insns become live again. */
3127 dead = false;
3128 remove = false;
b4fc67c7
RH
3129 }
3130 break;
3131
3132 case INDEX_op_br:
3133 case INDEX_op_exit_tb:
3134 case INDEX_op_goto_ptr:
3135 /* Unconditional branches; everything following is dead. */
3136 dead = true;
3137 break;
3138
3139 case INDEX_op_call:
3140 /* Notice noreturn helper calls, raising exceptions. */
90163900 3141 if (tcg_call_flags(op) & TCG_CALL_NO_RETURN) {
b4fc67c7
RH
3142 dead = true;
3143 }
3144 break;
3145
3146 case INDEX_op_insn_start:
3147 /* Never remove -- we need to keep these for unwind. */
3148 remove = false;
3149 break;
3150
3151 default:
3152 break;
3153 }
3154
3155 if (remove) {
3156 tcg_op_remove(s, op);
3157 }
3158 }
3159}
3160
c70fbf0a
RH
3161#define TS_DEAD 1
3162#define TS_MEM 2
3163
5a18407f
RH
3164#define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
3165#define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
3166
25f49c5f
RH
3167/* For liveness_pass_1, the register preferences for a given temp. */
3168static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
3169{
3170 return ts->state_ptr;
3171}
3172
3173/* For liveness_pass_1, reset the preferences for a given temp to the
3174 * maximal regset for its type.
3175 */
3176static inline void la_reset_pref(TCGTemp *ts)
3177{
3178 *la_temp_pref(ts)
3179 = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
3180}
3181
9c43b68d
AJ
3182/* liveness analysis: end of function: all temps are dead, and globals
3183 should be in memory. */
2616c808 3184static void la_func_end(TCGContext *s, int ng, int nt)
c896fe29 3185{
b83eabea
RH
3186 int i;
3187
3188 for (i = 0; i < ng; ++i) {
3189 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f 3190 la_reset_pref(&s->temps[i]);
b83eabea
RH
3191 }
3192 for (i = ng; i < nt; ++i) {
3193 s->temps[i].state = TS_DEAD;
25f49c5f 3194 la_reset_pref(&s->temps[i]);
b83eabea 3195 }
c896fe29
FB
3196}
3197
9c43b68d
AJ
3198/* liveness analysis: end of basic block: all temps are dead, globals
3199 and local temps should be in memory. */
2616c808 3200static void la_bb_end(TCGContext *s, int ng, int nt)
641d5fbe 3201{
b83eabea 3202 int i;
641d5fbe 3203
ee17db83
RH
3204 for (i = 0; i < nt; ++i) {
3205 TCGTemp *ts = &s->temps[i];
3206 int state;
3207
3208 switch (ts->kind) {
3209 case TEMP_FIXED:
3210 case TEMP_GLOBAL:
f57c6915 3211 case TEMP_TB:
ee17db83
RH
3212 state = TS_DEAD | TS_MEM;
3213 break;
c7482438 3214 case TEMP_EBB:
c0522136 3215 case TEMP_CONST:
ee17db83
RH
3216 state = TS_DEAD;
3217 break;
3218 default:
3219 g_assert_not_reached();
3220 }
3221 ts->state = state;
3222 la_reset_pref(ts);
641d5fbe
FB
3223 }
3224}
3225
f65a061c
RH
3226/* liveness analysis: sync globals back to memory. */
3227static void la_global_sync(TCGContext *s, int ng)
3228{
3229 int i;
3230
3231 for (i = 0; i < ng; ++i) {
25f49c5f
RH
3232 int state = s->temps[i].state;
3233 s->temps[i].state = state | TS_MEM;
3234 if (state == TS_DEAD) {
3235 /* If the global was previously dead, reset prefs. */
3236 la_reset_pref(&s->temps[i]);
3237 }
f65a061c
RH
3238 }
3239}
3240
b4cb76e6 3241/*
c7482438
RH
3242 * liveness analysis: conditional branch: all temps are dead unless
3243 * explicitly live-across-conditional-branch, globals and local temps
3244 * should be synced.
b4cb76e6
RH
3245 */
3246static void la_bb_sync(TCGContext *s, int ng, int nt)
3247{
3248 la_global_sync(s, ng);
3249
3250 for (int i = ng; i < nt; ++i) {
c0522136
RH
3251 TCGTemp *ts = &s->temps[i];
3252 int state;
3253
3254 switch (ts->kind) {
f57c6915 3255 case TEMP_TB:
c0522136
RH
3256 state = ts->state;
3257 ts->state = state | TS_MEM;
b4cb76e6
RH
3258 if (state != TS_DEAD) {
3259 continue;
3260 }
c0522136 3261 break;
c7482438 3262 case TEMP_EBB:
c0522136
RH
3263 case TEMP_CONST:
3264 continue;
3265 default:
3266 g_assert_not_reached();
b4cb76e6
RH
3267 }
3268 la_reset_pref(&s->temps[i]);
3269 }
3270}
3271
f65a061c
RH
3272/* liveness analysis: sync globals back to memory and kill. */
3273static void la_global_kill(TCGContext *s, int ng)
3274{
3275 int i;
3276
3277 for (i = 0; i < ng; i++) {
3278 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f
RH
3279 la_reset_pref(&s->temps[i]);
3280 }
3281}
3282
3283/* liveness analysis: note live globals crossing calls. */
3284static void la_cross_call(TCGContext *s, int nt)
3285{
3286 TCGRegSet mask = ~tcg_target_call_clobber_regs;
3287 int i;
3288
3289 for (i = 0; i < nt; i++) {
3290 TCGTemp *ts = &s->temps[i];
3291 if (!(ts->state & TS_DEAD)) {
3292 TCGRegSet *pset = la_temp_pref(ts);
3293 TCGRegSet set = *pset;
3294
3295 set &= mask;
3296 /* If the combination is not possible, restart. */
3297 if (set == 0) {
3298 set = tcg_target_available_regs[ts->type] & mask;
3299 }
3300 *pset = set;
3301 }
f65a061c
RH
3302 }
3303}
3304
874b8574
RH
3305/*
3306 * Liveness analysis: Verify the lifetime of TEMP_TB, and reduce
3307 * to TEMP_EBB, if possible.
3308 */
3309static void __attribute__((noinline))
3310liveness_pass_0(TCGContext *s)
3311{
3312 void * const multiple_ebb = (void *)(uintptr_t)-1;
3313 int nb_temps = s->nb_temps;
3314 TCGOp *op, *ebb;
3315
3316 for (int i = s->nb_globals; i < nb_temps; ++i) {
3317 s->temps[i].state_ptr = NULL;
3318 }
3319
3320 /*
3321 * Represent each EBB by the op at which it begins. In the case of
3322 * the first EBB, this is the first op, otherwise it is a label.
3323 * Collect the uses of each TEMP_TB: NULL for unused, EBB for use
3324 * within a single EBB, else MULTIPLE_EBB.
3325 */
3326 ebb = QTAILQ_FIRST(&s->ops);
3327 QTAILQ_FOREACH(op, &s->ops, link) {
3328 const TCGOpDef *def;
3329 int nb_oargs, nb_iargs;
3330
3331 switch (op->opc) {
3332 case INDEX_op_set_label:
3333 ebb = op;
3334 continue;
3335 case INDEX_op_discard:
3336 continue;
3337 case INDEX_op_call:
3338 nb_oargs = TCGOP_CALLO(op);
3339 nb_iargs = TCGOP_CALLI(op);
3340 break;
3341 default:
3342 def = &tcg_op_defs[op->opc];
3343 nb_oargs = def->nb_oargs;
3344 nb_iargs = def->nb_iargs;
3345 break;
3346 }
3347
3348 for (int i = 0; i < nb_oargs + nb_iargs; ++i) {
3349 TCGTemp *ts = arg_temp(op->args[i]);
3350
3351 if (ts->kind != TEMP_TB) {
3352 continue;
3353 }
3354 if (ts->state_ptr == NULL) {
3355 ts->state_ptr = ebb;
3356 } else if (ts->state_ptr != ebb) {
3357 ts->state_ptr = multiple_ebb;
3358 }
3359 }
3360 }
3361
3362 /*
3363 * For TEMP_TB that turned out not to be used beyond one EBB,
3364 * reduce the liveness to TEMP_EBB.
3365 */
3366 for (int i = s->nb_globals; i < nb_temps; ++i) {
3367 TCGTemp *ts = &s->temps[i];
3368 if (ts->kind == TEMP_TB && ts->state_ptr != multiple_ebb) {
3369 ts->kind = TEMP_EBB;
3370 }
3371 }
3372}
3373
a1b3c48d 3374/* Liveness analysis : update the opc_arg_life array to tell if a
c896fe29
FB
3375 given input arguments is dead. Instructions updating dead
3376 temporaries are removed. */
9bbee4c0
RH
3377static void __attribute__((noinline))
3378liveness_pass_1(TCGContext *s)
c896fe29 3379{
c70fbf0a 3380 int nb_globals = s->nb_globals;
2616c808 3381 int nb_temps = s->nb_temps;
15fa08f8 3382 TCGOp *op, *op_prev;
25f49c5f
RH
3383 TCGRegSet *prefs;
3384 int i;
3385
3386 prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
3387 for (i = 0; i < nb_temps; ++i) {
3388 s->temps[i].state_ptr = prefs + i;
3389 }
a1b3c48d 3390
ae36a246 3391 /* ??? Should be redundant with the exit_tb that ends the TB. */
2616c808 3392 la_func_end(s, nb_globals, nb_temps);
c896fe29 3393
eae3eb3e 3394 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
25f49c5f 3395 int nb_iargs, nb_oargs;
c45cb8bb
RH
3396 TCGOpcode opc_new, opc_new2;
3397 bool have_opc_new2;
a1b3c48d 3398 TCGLifeData arg_life = 0;
25f49c5f 3399 TCGTemp *ts;
c45cb8bb
RH
3400 TCGOpcode opc = op->opc;
3401 const TCGOpDef *def = &tcg_op_defs[opc];
3402
c45cb8bb 3403 switch (opc) {
c896fe29 3404 case INDEX_op_call:
c6e113f5 3405 {
39004a71
RH
3406 const TCGHelperInfo *info = tcg_call_info(op);
3407 int call_flags = tcg_call_flags(op);
c896fe29 3408
cd9090aa
RH
3409 nb_oargs = TCGOP_CALLO(op);
3410 nb_iargs = TCGOP_CALLI(op);
c6e113f5 3411
c45cb8bb 3412 /* pure functions can be removed if their result is unused */
78505279 3413 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
cf066674 3414 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
3415 ts = arg_temp(op->args[i]);
3416 if (ts->state != TS_DEAD) {
c6e113f5 3417 goto do_not_remove_call;
9c43b68d 3418 }
c6e113f5 3419 }
c45cb8bb 3420 goto do_remove;
152c35aa
RH
3421 }
3422 do_not_remove_call:
c896fe29 3423
25f49c5f 3424 /* Output args are dead. */
152c35aa 3425 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
3426 ts = arg_temp(op->args[i]);
3427 if (ts->state & TS_DEAD) {
152c35aa
RH
3428 arg_life |= DEAD_ARG << i;
3429 }
25f49c5f 3430 if (ts->state & TS_MEM) {
152c35aa 3431 arg_life |= SYNC_ARG << i;
c6e113f5 3432 }
25f49c5f
RH
3433 ts->state = TS_DEAD;
3434 la_reset_pref(ts);
152c35aa 3435 }
78505279 3436
31fd884b
RH
3437 /* Not used -- it will be tcg_target_call_oarg_reg(). */
3438 memset(op->output_pref, 0, sizeof(op->output_pref));
3439
152c35aa
RH
3440 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
3441 TCG_CALL_NO_READ_GLOBALS))) {
f65a061c 3442 la_global_kill(s, nb_globals);
152c35aa 3443 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
f65a061c 3444 la_global_sync(s, nb_globals);
152c35aa 3445 }
b9c18f56 3446
25f49c5f 3447 /* Record arguments that die in this helper. */
152c35aa 3448 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
25f49c5f 3449 ts = arg_temp(op->args[i]);
39004a71 3450 if (ts->state & TS_DEAD) {
152c35aa 3451 arg_life |= DEAD_ARG << i;
c6e113f5 3452 }
152c35aa 3453 }
25f49c5f
RH
3454
3455 /* For all live registers, remove call-clobbered prefs. */
3456 la_cross_call(s, nb_temps);
3457
39004a71
RH
3458 /*
3459 * Input arguments are live for preceding opcodes.
3460 *
3461 * For those arguments that die, and will be allocated in
3462 * registers, clear the register set for that arg, to be
3463 * filled in below. For args that will be on the stack,
3464 * reset to any available reg. Process arguments in reverse
3465 * order so that if a temp is used more than once, the stack
3466 * reset to max happens before the register reset to 0.
3467 */
3468 for (i = nb_iargs - 1; i >= 0; i--) {
3469 const TCGCallArgumentLoc *loc = &info->in[i];
3470 ts = arg_temp(op->args[nb_oargs + i]);
25f49c5f 3471
39004a71
RH
3472 if (ts->state & TS_DEAD) {
3473 switch (loc->kind) {
3474 case TCG_CALL_ARG_NORMAL:
3475 case TCG_CALL_ARG_EXTEND_U:
3476 case TCG_CALL_ARG_EXTEND_S:
338b61e9 3477 if (arg_slot_reg_p(loc->arg_slot)) {
39004a71
RH
3478 *la_temp_pref(ts) = 0;
3479 break;
3480 }
3481 /* fall through */
3482 default:
3483 *la_temp_pref(ts) =
3484 tcg_target_available_regs[ts->type];
3485 break;
3486 }
25f49c5f
RH
3487 ts->state &= ~TS_DEAD;
3488 }
3489 }
3490
39004a71
RH
3491 /*
3492 * For each input argument, add its input register to prefs.
3493 * If a temp is used once, this produces a single set bit;
3494 * if a temp is used multiple times, this produces a set.
3495 */
3496 for (i = 0; i < nb_iargs; i++) {
3497 const TCGCallArgumentLoc *loc = &info->in[i];
3498 ts = arg_temp(op->args[nb_oargs + i]);
3499
3500 switch (loc->kind) {
3501 case TCG_CALL_ARG_NORMAL:
3502 case TCG_CALL_ARG_EXTEND_U:
3503 case TCG_CALL_ARG_EXTEND_S:
338b61e9 3504 if (arg_slot_reg_p(loc->arg_slot)) {
39004a71
RH
3505 tcg_regset_set_reg(*la_temp_pref(ts),
3506 tcg_target_call_iarg_regs[loc->arg_slot]);
3507 }
3508 break;
3509 default:
3510 break;
c19f47bf 3511 }
c896fe29 3512 }
c896fe29 3513 }
c896fe29 3514 break;
765b842a 3515 case INDEX_op_insn_start:
c896fe29 3516 break;
5ff9d6a4 3517 case INDEX_op_discard:
5ff9d6a4 3518 /* mark the temporary as dead */
25f49c5f
RH
3519 ts = arg_temp(op->args[0]);
3520 ts->state = TS_DEAD;
3521 la_reset_pref(ts);
5ff9d6a4 3522 break;
1305c451
RH
3523
3524 case INDEX_op_add2_i32:
c45cb8bb 3525 opc_new = INDEX_op_add_i32;
f1fae40c 3526 goto do_addsub2;
1305c451 3527 case INDEX_op_sub2_i32:
c45cb8bb 3528 opc_new = INDEX_op_sub_i32;
f1fae40c
RH
3529 goto do_addsub2;
3530 case INDEX_op_add2_i64:
c45cb8bb 3531 opc_new = INDEX_op_add_i64;
f1fae40c
RH
3532 goto do_addsub2;
3533 case INDEX_op_sub2_i64:
c45cb8bb 3534 opc_new = INDEX_op_sub_i64;
f1fae40c 3535 do_addsub2:
1305c451
RH
3536 nb_iargs = 4;
3537 nb_oargs = 2;
3538 /* Test if the high part of the operation is dead, but not
3539 the low part. The result can be optimized to a simple
3540 add or sub. This happens often for x86_64 guest when the
3541 cpu mode is set to 32 bit. */
b83eabea
RH
3542 if (arg_temp(op->args[1])->state == TS_DEAD) {
3543 if (arg_temp(op->args[0])->state == TS_DEAD) {
1305c451
RH
3544 goto do_remove;
3545 }
c45cb8bb
RH
3546 /* Replace the opcode and adjust the args in place,
3547 leaving 3 unused args at the end. */
3548 op->opc = opc = opc_new;
efee3746
RH
3549 op->args[1] = op->args[2];
3550 op->args[2] = op->args[4];
1305c451
RH
3551 /* Fall through and mark the single-word operation live. */
3552 nb_iargs = 2;
3553 nb_oargs = 1;
3554 }
3555 goto do_not_remove;
3556
1414968a 3557 case INDEX_op_mulu2_i32:
c45cb8bb
RH
3558 opc_new = INDEX_op_mul_i32;
3559 opc_new2 = INDEX_op_muluh_i32;
3560 have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
03271524 3561 goto do_mul2;
f1fae40c 3562 case INDEX_op_muls2_i32:
c45cb8bb
RH
3563 opc_new = INDEX_op_mul_i32;
3564 opc_new2 = INDEX_op_mulsh_i32;
3565 have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
f1fae40c
RH
3566 goto do_mul2;
3567 case INDEX_op_mulu2_i64:
c45cb8bb
RH
3568 opc_new = INDEX_op_mul_i64;
3569 opc_new2 = INDEX_op_muluh_i64;
3570 have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
03271524 3571 goto do_mul2;
f1fae40c 3572 case INDEX_op_muls2_i64:
c45cb8bb
RH
3573 opc_new = INDEX_op_mul_i64;
3574 opc_new2 = INDEX_op_mulsh_i64;
3575 have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
03271524 3576 goto do_mul2;
f1fae40c 3577 do_mul2:
1414968a
RH
3578 nb_iargs = 2;
3579 nb_oargs = 2;
b83eabea
RH
3580 if (arg_temp(op->args[1])->state == TS_DEAD) {
3581 if (arg_temp(op->args[0])->state == TS_DEAD) {
03271524 3582 /* Both parts of the operation are dead. */
1414968a
RH
3583 goto do_remove;
3584 }
03271524 3585 /* The high part of the operation is dead; generate the low. */
c45cb8bb 3586 op->opc = opc = opc_new;
efee3746
RH
3587 op->args[1] = op->args[2];
3588 op->args[2] = op->args[3];
b83eabea 3589 } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
c45cb8bb
RH
3590 /* The low part of the operation is dead; generate the high. */
3591 op->opc = opc = opc_new2;
efee3746
RH
3592 op->args[0] = op->args[1];
3593 op->args[1] = op->args[2];
3594 op->args[2] = op->args[3];
03271524
RH
3595 } else {
3596 goto do_not_remove;
1414968a 3597 }
03271524
RH
3598 /* Mark the single-word operation live. */
3599 nb_oargs = 1;
1414968a
RH
3600 goto do_not_remove;
3601
c896fe29 3602 default:
1305c451 3603 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
49516bc0
AJ
3604 nb_iargs = def->nb_iargs;
3605 nb_oargs = def->nb_oargs;
c896fe29 3606
49516bc0
AJ
3607 /* Test if the operation can be removed because all
3608 its outputs are dead. We assume that nb_oargs == 0
3609 implies side effects */
3610 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
c45cb8bb 3611 for (i = 0; i < nb_oargs; i++) {
b83eabea 3612 if (arg_temp(op->args[i])->state != TS_DEAD) {
49516bc0 3613 goto do_not_remove;
9c43b68d 3614 }
49516bc0 3615 }
152c35aa
RH
3616 goto do_remove;
3617 }
3618 goto do_not_remove;
49516bc0 3619
152c35aa
RH
3620 do_remove:
3621 tcg_op_remove(s, op);
3622 break;
3623
3624 do_not_remove:
152c35aa 3625 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
3626 ts = arg_temp(op->args[i]);
3627
3628 /* Remember the preference of the uses that followed. */
31fd884b
RH
3629 if (i < ARRAY_SIZE(op->output_pref)) {
3630 op->output_pref[i] = *la_temp_pref(ts);
3631 }
25f49c5f
RH
3632
3633 /* Output args are dead. */
3634 if (ts->state & TS_DEAD) {
152c35aa 3635 arg_life |= DEAD_ARG << i;
49516bc0 3636 }
25f49c5f 3637 if (ts->state & TS_MEM) {
152c35aa
RH
3638 arg_life |= SYNC_ARG << i;
3639 }
25f49c5f
RH
3640 ts->state = TS_DEAD;
3641 la_reset_pref(ts);
152c35aa 3642 }
49516bc0 3643
25f49c5f 3644 /* If end of basic block, update. */
ae36a246
RH
3645 if (def->flags & TCG_OPF_BB_EXIT) {
3646 la_func_end(s, nb_globals, nb_temps);
b4cb76e6
RH
3647 } else if (def->flags & TCG_OPF_COND_BRANCH) {
3648 la_bb_sync(s, nb_globals, nb_temps);
ae36a246 3649 } else if (def->flags & TCG_OPF_BB_END) {
2616c808 3650 la_bb_end(s, nb_globals, nb_temps);
152c35aa 3651 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
f65a061c 3652 la_global_sync(s, nb_globals);
25f49c5f
RH
3653 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3654 la_cross_call(s, nb_temps);
3655 }
152c35aa
RH
3656 }
3657
25f49c5f 3658 /* Record arguments that die in this opcode. */
152c35aa 3659 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
25f49c5f
RH
3660 ts = arg_temp(op->args[i]);
3661 if (ts->state & TS_DEAD) {
152c35aa 3662 arg_life |= DEAD_ARG << i;
c896fe29 3663 }
c896fe29 3664 }
25f49c5f
RH
3665
3666 /* Input arguments are live for preceding opcodes. */
152c35aa 3667 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
25f49c5f
RH
3668 ts = arg_temp(op->args[i]);
3669 if (ts->state & TS_DEAD) {
3670 /* For operands that were dead, initially allow
3671 all regs for the type. */
3672 *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
3673 ts->state &= ~TS_DEAD;
3674 }
3675 }
3676
3677 /* Incorporate constraints for this operand. */
3678 switch (opc) {
3679 case INDEX_op_mov_i32:
3680 case INDEX_op_mov_i64:
3681 /* Note that these are TCG_OPF_NOT_PRESENT and do not
3682 have proper constraints. That said, special case
3683 moves to propagate preferences backward. */
3684 if (IS_DEAD_ARG(1)) {
3685 *la_temp_pref(arg_temp(op->args[0]))
3686 = *la_temp_pref(arg_temp(op->args[1]));
3687 }
3688 break;
3689
3690 default:
3691 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3692 const TCGArgConstraint *ct = &def->args_ct[i];
3693 TCGRegSet set, *pset;
3694
3695 ts = arg_temp(op->args[i]);
3696 pset = la_temp_pref(ts);
3697 set = *pset;
3698
9be0d080 3699 set &= ct->regs;
bc2b17e6 3700 if (ct->ialias) {
31fd884b 3701 set &= output_pref(op, ct->alias_index);
25f49c5f
RH
3702 }
3703 /* If the combination is not possible, restart. */
3704 if (set == 0) {
9be0d080 3705 set = ct->regs;
25f49c5f
RH
3706 }
3707 *pset = set;
3708 }
3709 break;
152c35aa 3710 }
c896fe29
FB
3711 break;
3712 }
bee158cb 3713 op->life = arg_life;
1ff0a2c5 3714 }
c896fe29 3715}
c896fe29 3716
5a18407f 3717/* Liveness analysis: Convert indirect regs to direct temporaries. */
9bbee4c0
RH
3718static bool __attribute__((noinline))
3719liveness_pass_2(TCGContext *s)
5a18407f
RH
3720{
3721 int nb_globals = s->nb_globals;
15fa08f8 3722 int nb_temps, i;
5a18407f 3723 bool changes = false;
15fa08f8 3724 TCGOp *op, *op_next;
5a18407f 3725
5a18407f
RH
3726 /* Create a temporary for each indirect global. */
3727 for (i = 0; i < nb_globals; ++i) {
3728 TCGTemp *its = &s->temps[i];
3729 if (its->indirect_reg) {
3730 TCGTemp *dts = tcg_temp_alloc(s);
3731 dts->type = its->type;
3732 dts->base_type = its->base_type;
e1e64652 3733 dts->temp_subindex = its->temp_subindex;
c7482438 3734 dts->kind = TEMP_EBB;
b83eabea
RH
3735 its->state_ptr = dts;
3736 } else {
3737 its->state_ptr = NULL;
5a18407f 3738 }
b83eabea
RH
3739 /* All globals begin dead. */
3740 its->state = TS_DEAD;
3741 }
3742 for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
3743 TCGTemp *its = &s->temps[i];
3744 its->state_ptr = NULL;
3745 its->state = TS_DEAD;
5a18407f 3746 }
5a18407f 3747
15fa08f8 3748 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
5a18407f
RH
3749 TCGOpcode opc = op->opc;
3750 const TCGOpDef *def = &tcg_op_defs[opc];
3751 TCGLifeData arg_life = op->life;
3752 int nb_iargs, nb_oargs, call_flags;
b83eabea 3753 TCGTemp *arg_ts, *dir_ts;
5a18407f 3754
5a18407f 3755 if (opc == INDEX_op_call) {
cd9090aa
RH
3756 nb_oargs = TCGOP_CALLO(op);
3757 nb_iargs = TCGOP_CALLI(op);
90163900 3758 call_flags = tcg_call_flags(op);
5a18407f
RH
3759 } else {
3760 nb_iargs = def->nb_iargs;
3761 nb_oargs = def->nb_oargs;
3762
3763 /* Set flags similar to how calls require. */
b4cb76e6
RH
3764 if (def->flags & TCG_OPF_COND_BRANCH) {
3765 /* Like reading globals: sync_globals */
3766 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3767 } else if (def->flags & TCG_OPF_BB_END) {
5a18407f
RH
3768 /* Like writing globals: save_globals */
3769 call_flags = 0;
3770 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3771 /* Like reading globals: sync_globals */
3772 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3773 } else {
3774 /* No effect on globals. */
3775 call_flags = (TCG_CALL_NO_READ_GLOBALS |
3776 TCG_CALL_NO_WRITE_GLOBALS);
3777 }
3778 }
3779
3780 /* Make sure that input arguments are available. */
3781 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
b83eabea 3782 arg_ts = arg_temp(op->args[i]);
39004a71
RH
3783 dir_ts = arg_ts->state_ptr;
3784 if (dir_ts && arg_ts->state == TS_DEAD) {
3785 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
3786 ? INDEX_op_ld_i32
3787 : INDEX_op_ld_i64);
3788 TCGOp *lop = tcg_op_insert_before(s, op, lopc, 3);
3789
3790 lop->args[0] = temp_arg(dir_ts);
3791 lop->args[1] = temp_arg(arg_ts->mem_base);
3792 lop->args[2] = arg_ts->mem_offset;
3793
3794 /* Loaded, but synced with memory. */
3795 arg_ts->state = TS_MEM;
5a18407f
RH
3796 }
3797 }
3798
3799 /* Perform input replacement, and mark inputs that became dead.
3800 No action is required except keeping temp_state up to date
3801 so that we reload when needed. */
3802 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
b83eabea 3803 arg_ts = arg_temp(op->args[i]);
39004a71
RH
3804 dir_ts = arg_ts->state_ptr;
3805 if (dir_ts) {
3806 op->args[i] = temp_arg(dir_ts);
3807 changes = true;
3808 if (IS_DEAD_ARG(i)) {
3809 arg_ts->state = TS_DEAD;
5a18407f
RH
3810 }
3811 }
3812 }
3813
3814 /* Liveness analysis should ensure that the following are
3815 all correct, for call sites and basic block end points. */
3816 if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
3817 /* Nothing to do */
3818 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
3819 for (i = 0; i < nb_globals; ++i) {
3820 /* Liveness should see that globals are synced back,
3821 that is, either TS_DEAD or TS_MEM. */
b83eabea
RH
3822 arg_ts = &s->temps[i];
3823 tcg_debug_assert(arg_ts->state_ptr == 0
3824 || arg_ts->state != 0);
5a18407f
RH
3825 }
3826 } else {
3827 for (i = 0; i < nb_globals; ++i) {
3828 /* Liveness should see that globals are saved back,
3829 that is, TS_DEAD, waiting to be reloaded. */
b83eabea
RH
3830 arg_ts = &s->temps[i];
3831 tcg_debug_assert(arg_ts->state_ptr == 0
3832 || arg_ts->state == TS_DEAD);
5a18407f
RH
3833 }
3834 }
3835
3836 /* Outputs become available. */
61f15c48
RH
3837 if (opc == INDEX_op_mov_i32 || opc == INDEX_op_mov_i64) {
3838 arg_ts = arg_temp(op->args[0]);
b83eabea 3839 dir_ts = arg_ts->state_ptr;
61f15c48
RH
3840 if (dir_ts) {
3841 op->args[0] = temp_arg(dir_ts);
3842 changes = true;
3843
3844 /* The output is now live and modified. */
3845 arg_ts->state = 0;
3846
3847 if (NEED_SYNC_ARG(0)) {
3848 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
3849 ? INDEX_op_st_i32
3850 : INDEX_op_st_i64);
d4478943 3851 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 3);
61f15c48
RH
3852 TCGTemp *out_ts = dir_ts;
3853
3854 if (IS_DEAD_ARG(0)) {
3855 out_ts = arg_temp(op->args[1]);
3856 arg_ts->state = TS_DEAD;
3857 tcg_op_remove(s, op);
3858 } else {
3859 arg_ts->state = TS_MEM;
3860 }
3861
3862 sop->args[0] = temp_arg(out_ts);
3863 sop->args[1] = temp_arg(arg_ts->mem_base);
3864 sop->args[2] = arg_ts->mem_offset;
3865 } else {
3866 tcg_debug_assert(!IS_DEAD_ARG(0));
3867 }
5a18407f 3868 }
61f15c48
RH
3869 } else {
3870 for (i = 0; i < nb_oargs; i++) {
3871 arg_ts = arg_temp(op->args[i]);
3872 dir_ts = arg_ts->state_ptr;
3873 if (!dir_ts) {
3874 continue;
3875 }
3876 op->args[i] = temp_arg(dir_ts);
3877 changes = true;
5a18407f 3878
61f15c48
RH
3879 /* The output is now live and modified. */
3880 arg_ts->state = 0;
5a18407f 3881
61f15c48
RH
3882 /* Sync outputs upon their last write. */
3883 if (NEED_SYNC_ARG(i)) {
3884 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
3885 ? INDEX_op_st_i32
3886 : INDEX_op_st_i64);
d4478943 3887 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 3);
5a18407f 3888
61f15c48
RH
3889 sop->args[0] = temp_arg(dir_ts);
3890 sop->args[1] = temp_arg(arg_ts->mem_base);
3891 sop->args[2] = arg_ts->mem_offset;
5a18407f 3892
61f15c48
RH
3893 arg_ts->state = TS_MEM;
3894 }
3895 /* Drop outputs that are dead. */
3896 if (IS_DEAD_ARG(i)) {
3897 arg_ts->state = TS_DEAD;
3898 }
5a18407f
RH
3899 }
3900 }
3901 }
3902
3903 return changes;
3904}
3905
2272e4a7 3906static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
c896fe29 3907{
31c96417 3908 intptr_t off;
273eb50c 3909 int size, align;
c1c09194 3910
273eb50c
RH
3911 /* When allocating an object, look at the full type. */
3912 size = tcg_type_size(ts->base_type);
3913 switch (ts->base_type) {
c1c09194 3914 case TCG_TYPE_I32:
31c96417 3915 align = 4;
c1c09194
RH
3916 break;
3917 case TCG_TYPE_I64:
3918 case TCG_TYPE_V64:
31c96417 3919 align = 8;
c1c09194 3920 break;
43eef72f 3921 case TCG_TYPE_I128:
c1c09194 3922 case TCG_TYPE_V128:
c1c09194 3923 case TCG_TYPE_V256:
43eef72f
RH
3924 /*
3925 * Note that we do not require aligned storage for V256,
3926 * and that we provide alignment for I128 to match V128,
3927 * even if that's above what the host ABI requires.
3928 */
31c96417 3929 align = 16;
c1c09194
RH
3930 break;
3931 default:
3932 g_assert_not_reached();
b591dc59 3933 }
c1c09194 3934
b9537d59
RH
3935 /*
3936 * Assume the stack is sufficiently aligned.
3937 * This affects e.g. ARM NEON, where we have 8 byte stack alignment
3938 * and do not require 16 byte vector alignment. This seems slightly
3939 * easier than fully parameterizing the above switch statement.
3940 */
3941 align = MIN(TCG_TARGET_STACK_ALIGN, align);
c1c09194 3942 off = ROUND_UP(s->current_frame_offset, align);
732d5897
RH
3943
3944 /* If we've exhausted the stack frame, restart with a smaller TB. */
3945 if (off + size > s->frame_end) {
3946 tcg_raise_tb_overflow(s);
3947 }
c1c09194 3948 s->current_frame_offset = off + size;
9defd1bd 3949#if defined(__sparc__)
273eb50c 3950 off += TCG_TARGET_STACK_BIAS;
9defd1bd 3951#endif
273eb50c
RH
3952
3953 /* If the object was subdivided, assign memory to all the parts. */
3954 if (ts->base_type != ts->type) {
3955 int part_size = tcg_type_size(ts->type);
3956 int part_count = size / part_size;
3957
3958 /*
3959 * Each part is allocated sequentially in tcg_temp_new_internal.
3960 * Jump back to the first part by subtracting the current index.
3961 */
3962 ts -= ts->temp_subindex;
3963 for (int i = 0; i < part_count; ++i) {
3964 ts[i].mem_offset = off + i * part_size;
3965 ts[i].mem_base = s->frame_temp;
3966 ts[i].mem_allocated = 1;
3967 }
3968 } else {
3969 ts->mem_offset = off;
3970 ts->mem_base = s->frame_temp;
3971 ts->mem_allocated = 1;
3972 }
c896fe29
FB
3973}
3974
098859f1
RH
3975/* Assign @reg to @ts, and update reg_to_temp[]. */
3976static void set_temp_val_reg(TCGContext *s, TCGTemp *ts, TCGReg reg)
3977{
3978 if (ts->val_type == TEMP_VAL_REG) {
3979 TCGReg old = ts->reg;
3980 tcg_debug_assert(s->reg_to_temp[old] == ts);
3981 if (old == reg) {
3982 return;
3983 }
3984 s->reg_to_temp[old] = NULL;
3985 }
3986 tcg_debug_assert(s->reg_to_temp[reg] == NULL);
3987 s->reg_to_temp[reg] = ts;
3988 ts->val_type = TEMP_VAL_REG;
3989 ts->reg = reg;
3990}
3991
3992/* Assign a non-register value type to @ts, and update reg_to_temp[]. */
3993static void set_temp_val_nonreg(TCGContext *s, TCGTemp *ts, TCGTempVal type)
3994{
3995 tcg_debug_assert(type != TEMP_VAL_REG);
3996 if (ts->val_type == TEMP_VAL_REG) {
3997 TCGReg reg = ts->reg;
3998 tcg_debug_assert(s->reg_to_temp[reg] == ts);
3999 s->reg_to_temp[reg] = NULL;
4000 }
4001 ts->val_type = type;
4002}
4003
b722452a 4004static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
b3915dbb 4005
59d7c14e
RH
4006/* Mark a temporary as free or dead. If 'free_or_dead' is negative,
4007 mark it free; otherwise mark it dead. */
4008static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
7f6ceedf 4009{
c0522136
RH
4010 TCGTempVal new_type;
4011
4012 switch (ts->kind) {
4013 case TEMP_FIXED:
59d7c14e 4014 return;
c0522136 4015 case TEMP_GLOBAL:
f57c6915 4016 case TEMP_TB:
c0522136
RH
4017 new_type = TEMP_VAL_MEM;
4018 break;
c7482438 4019 case TEMP_EBB:
c0522136
RH
4020 new_type = free_or_dead < 0 ? TEMP_VAL_MEM : TEMP_VAL_DEAD;
4021 break;
4022 case TEMP_CONST:
4023 new_type = TEMP_VAL_CONST;
4024 break;
4025 default:
4026 g_assert_not_reached();
59d7c14e 4027 }
098859f1 4028 set_temp_val_nonreg(s, ts, new_type);
59d7c14e 4029}
7f6ceedf 4030
59d7c14e
RH
4031/* Mark a temporary as dead. */
4032static inline void temp_dead(TCGContext *s, TCGTemp *ts)
4033{
4034 temp_free_or_dead(s, ts, 1);
4035}
4036
4037/* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
4038 registers needs to be allocated to store a constant. If 'free_or_dead'
4039 is non-zero, subsequently release the temporary; if it is positive, the
4040 temp is dead; if it is negative, the temp is free. */
98b4e186
RH
4041static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
4042 TCGRegSet preferred_regs, int free_or_dead)
59d7c14e 4043{
c0522136 4044 if (!temp_readonly(ts) && !ts->mem_coherent) {
7f6ceedf 4045 if (!ts->mem_allocated) {
2272e4a7 4046 temp_allocate_frame(s, ts);
59d7c14e 4047 }
59d7c14e
RH
4048 switch (ts->val_type) {
4049 case TEMP_VAL_CONST:
4050 /* If we're going to free the temp immediately, then we won't
4051 require it later in a register, so attempt to store the
4052 constant to memory directly. */
4053 if (free_or_dead
4054 && tcg_out_sti(s, ts->type, ts->val,
4055 ts->mem_base->reg, ts->mem_offset)) {
4056 break;
4057 }
4058 temp_load(s, ts, tcg_target_available_regs[ts->type],
98b4e186 4059 allocated_regs, preferred_regs);
59d7c14e
RH
4060 /* fallthrough */
4061
4062 case TEMP_VAL_REG:
4063 tcg_out_st(s, ts->type, ts->reg,
4064 ts->mem_base->reg, ts->mem_offset);
4065 break;
4066
4067 case TEMP_VAL_MEM:
4068 break;
4069
4070 case TEMP_VAL_DEAD:
4071 default:
732e89f4 4072 g_assert_not_reached();
59d7c14e
RH
4073 }
4074 ts->mem_coherent = 1;
4075 }
4076 if (free_or_dead) {
4077 temp_free_or_dead(s, ts, free_or_dead);
7f6ceedf 4078 }
7f6ceedf
AJ
4079}
4080
c896fe29 4081/* free register 'reg' by spilling the corresponding temporary if necessary */
b3915dbb 4082static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
c896fe29 4083{
f8b2f202 4084 TCGTemp *ts = s->reg_to_temp[reg];
f8b2f202 4085 if (ts != NULL) {
98b4e186 4086 temp_sync(s, ts, allocated_regs, 0, -1);
c896fe29
FB
4087 }
4088}
4089
b016486e
RH
4090/**
4091 * tcg_reg_alloc:
4092 * @required_regs: Set of registers in which we must allocate.
4093 * @allocated_regs: Set of registers which must be avoided.
4094 * @preferred_regs: Set of registers we should prefer.
4095 * @rev: True if we search the registers in "indirect" order.
4096 *
4097 * The allocated register must be in @required_regs & ~@allocated_regs,
4098 * but if we can put it in @preferred_regs we may save a move later.
4099 */
4100static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
4101 TCGRegSet allocated_regs,
4102 TCGRegSet preferred_regs, bool rev)
c896fe29 4103{
b016486e
RH
4104 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
4105 TCGRegSet reg_ct[2];
91478cef 4106 const int *order;
c896fe29 4107
b016486e
RH
4108 reg_ct[1] = required_regs & ~allocated_regs;
4109 tcg_debug_assert(reg_ct[1] != 0);
4110 reg_ct[0] = reg_ct[1] & preferred_regs;
4111
4112 /* Skip the preferred_regs option if it cannot be satisfied,
4113 or if the preference made no difference. */
4114 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
4115
91478cef 4116 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
c896fe29 4117
b016486e
RH
4118 /* Try free registers, preferences first. */
4119 for (j = f; j < 2; j++) {
4120 TCGRegSet set = reg_ct[j];
4121
4122 if (tcg_regset_single(set)) {
4123 /* One register in the set. */
4124 TCGReg reg = tcg_regset_first(set);
4125 if (s->reg_to_temp[reg] == NULL) {
4126 return reg;
4127 }
4128 } else {
4129 for (i = 0; i < n; i++) {
4130 TCGReg reg = order[i];
4131 if (s->reg_to_temp[reg] == NULL &&
4132 tcg_regset_test_reg(set, reg)) {
4133 return reg;
4134 }
4135 }
4136 }
c896fe29
FB
4137 }
4138
b016486e
RH
4139 /* We must spill something. */
4140 for (j = f; j < 2; j++) {
4141 TCGRegSet set = reg_ct[j];
4142
4143 if (tcg_regset_single(set)) {
4144 /* One register in the set. */
4145 TCGReg reg = tcg_regset_first(set);
b3915dbb 4146 tcg_reg_free(s, reg, allocated_regs);
c896fe29 4147 return reg;
b016486e
RH
4148 } else {
4149 for (i = 0; i < n; i++) {
4150 TCGReg reg = order[i];
4151 if (tcg_regset_test_reg(set, reg)) {
4152 tcg_reg_free(s, reg, allocated_regs);
4153 return reg;
4154 }
4155 }
c896fe29
FB
4156 }
4157 }
4158
732e89f4 4159 g_assert_not_reached();
c896fe29
FB
4160}
4161
29f5e925
RH
4162static TCGReg tcg_reg_alloc_pair(TCGContext *s, TCGRegSet required_regs,
4163 TCGRegSet allocated_regs,
4164 TCGRegSet preferred_regs, bool rev)
4165{
4166 int i, j, k, fmin, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
4167 TCGRegSet reg_ct[2];
4168 const int *order;
4169
4170 /* Ensure that if I is not in allocated_regs, I+1 is not either. */
4171 reg_ct[1] = required_regs & ~(allocated_regs | (allocated_regs >> 1));
4172 tcg_debug_assert(reg_ct[1] != 0);
4173 reg_ct[0] = reg_ct[1] & preferred_regs;
4174
4175 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
4176
4177 /*
4178 * Skip the preferred_regs option if it cannot be satisfied,
4179 * or if the preference made no difference.
4180 */
4181 k = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
4182
4183 /*
4184 * Minimize the number of flushes by looking for 2 free registers first,
4185 * then a single flush, then two flushes.
4186 */
4187 for (fmin = 2; fmin >= 0; fmin--) {
4188 for (j = k; j < 2; j++) {
4189 TCGRegSet set = reg_ct[j];
4190
4191 for (i = 0; i < n; i++) {
4192 TCGReg reg = order[i];
4193
4194 if (tcg_regset_test_reg(set, reg)) {
4195 int f = !s->reg_to_temp[reg] + !s->reg_to_temp[reg + 1];
4196 if (f >= fmin) {
4197 tcg_reg_free(s, reg, allocated_regs);
4198 tcg_reg_free(s, reg + 1, allocated_regs);
4199 return reg;
4200 }
4201 }
4202 }
4203 }
4204 }
732e89f4 4205 g_assert_not_reached();
29f5e925
RH
4206}
4207
40ae5c62
RH
4208/* Make sure the temporary is in a register. If needed, allocate the register
4209 from DESIRED while avoiding ALLOCATED. */
4210static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
b722452a 4211 TCGRegSet allocated_regs, TCGRegSet preferred_regs)
40ae5c62
RH
4212{
4213 TCGReg reg;
4214
4215 switch (ts->val_type) {
4216 case TEMP_VAL_REG:
4217 return;
4218 case TEMP_VAL_CONST:
b016486e 4219 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
b722452a 4220 preferred_regs, ts->indirect_base);
0a6a8bc8
RH
4221 if (ts->type <= TCG_TYPE_I64) {
4222 tcg_out_movi(s, ts->type, reg, ts->val);
4223 } else {
4e186175
RH
4224 uint64_t val = ts->val;
4225 MemOp vece = MO_64;
4226
4227 /*
4228 * Find the minimal vector element that matches the constant.
4229 * The targets will, in general, have to do this search anyway,
4230 * do this generically.
4231 */
4e186175
RH
4232 if (val == dup_const(MO_8, val)) {
4233 vece = MO_8;
4234 } else if (val == dup_const(MO_16, val)) {
4235 vece = MO_16;
0b4286dd 4236 } else if (val == dup_const(MO_32, val)) {
4e186175
RH
4237 vece = MO_32;
4238 }
4239
4240 tcg_out_dupi_vec(s, ts->type, vece, reg, ts->val);
0a6a8bc8 4241 }
40ae5c62
RH
4242 ts->mem_coherent = 0;
4243 break;
4244 case TEMP_VAL_MEM:
b016486e 4245 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
b722452a 4246 preferred_regs, ts->indirect_base);
40ae5c62
RH
4247 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
4248 ts->mem_coherent = 1;
4249 break;
4250 case TEMP_VAL_DEAD:
4251 default:
732e89f4 4252 g_assert_not_reached();
40ae5c62 4253 }
098859f1 4254 set_temp_val_reg(s, ts, reg);
40ae5c62
RH
4255}
4256
59d7c14e
RH
4257/* Save a temporary to memory. 'allocated_regs' is used in case a
4258 temporary registers needs to be allocated to store a constant. */
4259static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
1ad80729 4260{
5a18407f
RH
4261 /* The liveness analysis already ensures that globals are back
4262 in memory. Keep an tcg_debug_assert for safety. */
e01fa97d 4263 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || temp_readonly(ts));
1ad80729
AJ
4264}
4265
9814dd27 4266/* save globals to their canonical location and assume they can be
e8996ee0
FB
4267 modified be the following code. 'allocated_regs' is used in case a
4268 temporary registers needs to be allocated to store a constant. */
4269static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
c896fe29 4270{
ac3b8891 4271 int i, n;
c896fe29 4272
ac3b8891 4273 for (i = 0, n = s->nb_globals; i < n; i++) {
b13eb728 4274 temp_save(s, &s->temps[i], allocated_regs);
c896fe29 4275 }
e5097dc8
FB
4276}
4277
3d5c5f87
AJ
4278/* sync globals to their canonical location and assume they can be
4279 read by the following code. 'allocated_regs' is used in case a
4280 temporary registers needs to be allocated to store a constant. */
4281static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
4282{
ac3b8891 4283 int i, n;
3d5c5f87 4284
ac3b8891 4285 for (i = 0, n = s->nb_globals; i < n; i++) {
12b9b11a 4286 TCGTemp *ts = &s->temps[i];
5a18407f 4287 tcg_debug_assert(ts->val_type != TEMP_VAL_REG
ee17db83 4288 || ts->kind == TEMP_FIXED
5a18407f 4289 || ts->mem_coherent);
3d5c5f87
AJ
4290 }
4291}
4292
e5097dc8 4293/* at the end of a basic block, we assume all temporaries are dead and
e8996ee0
FB
4294 all globals are stored at their canonical location. */
4295static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
e5097dc8 4296{
e5097dc8
FB
4297 int i;
4298
b13eb728
RH
4299 for (i = s->nb_globals; i < s->nb_temps; i++) {
4300 TCGTemp *ts = &s->temps[i];
c0522136
RH
4301
4302 switch (ts->kind) {
f57c6915 4303 case TEMP_TB:
b13eb728 4304 temp_save(s, ts, allocated_regs);
c0522136 4305 break;
c7482438 4306 case TEMP_EBB:
5a18407f
RH
4307 /* The liveness analysis already ensures that temps are dead.
4308 Keep an tcg_debug_assert for safety. */
4309 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
c0522136
RH
4310 break;
4311 case TEMP_CONST:
4312 /* Similarly, we should have freed any allocated register. */
4313 tcg_debug_assert(ts->val_type == TEMP_VAL_CONST);
4314 break;
4315 default:
4316 g_assert_not_reached();
c896fe29
FB
4317 }
4318 }
e8996ee0
FB
4319
4320 save_globals(s, allocated_regs);
c896fe29
FB
4321}
4322
b4cb76e6 4323/*
c7482438
RH
4324 * At a conditional branch, we assume all temporaries are dead unless
4325 * explicitly live-across-conditional-branch; all globals and local
4326 * temps are synced to their location.
b4cb76e6
RH
4327 */
4328static void tcg_reg_alloc_cbranch(TCGContext *s, TCGRegSet allocated_regs)
4329{
4330 sync_globals(s, allocated_regs);
4331
4332 for (int i = s->nb_globals; i < s->nb_temps; i++) {
4333 TCGTemp *ts = &s->temps[i];
4334 /*
4335 * The liveness analysis already ensures that temps are dead.
4336 * Keep tcg_debug_asserts for safety.
4337 */
c0522136 4338 switch (ts->kind) {
f57c6915 4339 case TEMP_TB:
b4cb76e6 4340 tcg_debug_assert(ts->val_type != TEMP_VAL_REG || ts->mem_coherent);
c0522136 4341 break;
c7482438 4342 case TEMP_EBB:
c0522136
RH
4343 case TEMP_CONST:
4344 break;
4345 default:
4346 g_assert_not_reached();
b4cb76e6
RH
4347 }
4348 }
4349}
4350
bab1671f 4351/*
c58f4c97 4352 * Specialized code generation for INDEX_op_mov_* with a constant.
bab1671f 4353 */
0fe4fca4 4354static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
ba87719c
RH
4355 tcg_target_ulong val, TCGLifeData arg_life,
4356 TCGRegSet preferred_regs)
e8996ee0 4357{
d63e3b6e 4358 /* ENV should not be modified. */
e01fa97d 4359 tcg_debug_assert(!temp_readonly(ots));
59d7c14e
RH
4360
4361 /* The movi is not explicitly generated here. */
098859f1 4362 set_temp_val_nonreg(s, ots, TEMP_VAL_CONST);
59d7c14e
RH
4363 ots->val = val;
4364 ots->mem_coherent = 0;
4365 if (NEED_SYNC_ARG(0)) {
ba87719c 4366 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
59d7c14e 4367 } else if (IS_DEAD_ARG(0)) {
f8bf00f1 4368 temp_dead(s, ots);
4c4e1ab2 4369 }
e8996ee0
FB
4370}
4371
bab1671f
RH
4372/*
4373 * Specialized code generation for INDEX_op_mov_*.
4374 */
dd186292 4375static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
c896fe29 4376{
dd186292 4377 const TCGLifeData arg_life = op->life;
69e3706d 4378 TCGRegSet allocated_regs, preferred_regs;
c896fe29 4379 TCGTemp *ts, *ots;
450445d5 4380 TCGType otype, itype;
098859f1 4381 TCGReg oreg, ireg;
c896fe29 4382
d21369f5 4383 allocated_regs = s->reserved_regs;
31fd884b 4384 preferred_regs = output_pref(op, 0);
43439139
RH
4385 ots = arg_temp(op->args[0]);
4386 ts = arg_temp(op->args[1]);
450445d5 4387
d63e3b6e 4388 /* ENV should not be modified. */
e01fa97d 4389 tcg_debug_assert(!temp_readonly(ots));
d63e3b6e 4390
450445d5
RH
4391 /* Note that otype != itype for no-op truncation. */
4392 otype = ots->type;
4393 itype = ts->type;
c29c1d7e 4394
0fe4fca4
PB
4395 if (ts->val_type == TEMP_VAL_CONST) {
4396 /* propagate constant or generate sti */
4397 tcg_target_ulong val = ts->val;
4398 if (IS_DEAD_ARG(1)) {
4399 temp_dead(s, ts);
4400 }
69e3706d 4401 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
0fe4fca4
PB
4402 return;
4403 }
4404
4405 /* If the source value is in memory we're going to be forced
4406 to have it in a register in order to perform the copy. Copy
4407 the SOURCE value into its own register first, that way we
4408 don't have to reload SOURCE the next time it is used. */
4409 if (ts->val_type == TEMP_VAL_MEM) {
69e3706d
RH
4410 temp_load(s, ts, tcg_target_available_regs[itype],
4411 allocated_regs, preferred_regs);
c29c1d7e 4412 }
0fe4fca4 4413 tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
098859f1
RH
4414 ireg = ts->reg;
4415
d63e3b6e 4416 if (IS_DEAD_ARG(0)) {
c29c1d7e
AJ
4417 /* mov to a non-saved dead register makes no sense (even with
4418 liveness analysis disabled). */
eabb7b91 4419 tcg_debug_assert(NEED_SYNC_ARG(0));
c29c1d7e 4420 if (!ots->mem_allocated) {
2272e4a7 4421 temp_allocate_frame(s, ots);
c29c1d7e 4422 }
098859f1 4423 tcg_out_st(s, otype, ireg, ots->mem_base->reg, ots->mem_offset);
c29c1d7e 4424 if (IS_DEAD_ARG(1)) {
f8bf00f1 4425 temp_dead(s, ts);
c29c1d7e 4426 }
f8bf00f1 4427 temp_dead(s, ots);
098859f1
RH
4428 return;
4429 }
4430
4431 if (IS_DEAD_ARG(1) && ts->kind != TEMP_FIXED) {
4432 /*
4433 * The mov can be suppressed. Kill input first, so that it
4434 * is unlinked from reg_to_temp, then set the output to the
4435 * reg that we saved from the input.
4436 */
4437 temp_dead(s, ts);
4438 oreg = ireg;
c29c1d7e 4439 } else {
098859f1
RH
4440 if (ots->val_type == TEMP_VAL_REG) {
4441 oreg = ots->reg;
c896fe29 4442 } else {
098859f1
RH
4443 /* Make sure to not spill the input register during allocation. */
4444 oreg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
4445 allocated_regs | ((TCGRegSet)1 << ireg),
4446 preferred_regs, ots->indirect_base);
c896fe29 4447 }
098859f1
RH
4448 if (!tcg_out_mov(s, otype, oreg, ireg)) {
4449 /*
4450 * Cross register class move not supported.
4451 * Store the source register into the destination slot
4452 * and leave the destination temp as TEMP_VAL_MEM.
4453 */
4454 assert(!temp_readonly(ots));
4455 if (!ts->mem_allocated) {
4456 temp_allocate_frame(s, ots);
4457 }
4458 tcg_out_st(s, ts->type, ireg, ots->mem_base->reg, ots->mem_offset);
4459 set_temp_val_nonreg(s, ts, TEMP_VAL_MEM);
4460 ots->mem_coherent = 1;
4461 return;
c896fe29 4462 }
ec7a869d 4463 }
098859f1
RH
4464 set_temp_val_reg(s, ots, oreg);
4465 ots->mem_coherent = 0;
4466
4467 if (NEED_SYNC_ARG(0)) {
4468 temp_sync(s, ots, allocated_regs, 0, 0);
4469 }
c896fe29
FB
4470}
4471
bab1671f
RH
4472/*
4473 * Specialized code generation for INDEX_op_dup_vec.
4474 */
4475static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)
4476{
4477 const TCGLifeData arg_life = op->life;
4478 TCGRegSet dup_out_regs, dup_in_regs;
4479 TCGTemp *its, *ots;
4480 TCGType itype, vtype;
4481 unsigned vece;
31c96417 4482 int lowpart_ofs;
bab1671f
RH
4483 bool ok;
4484
4485 ots = arg_temp(op->args[0]);
4486 its = arg_temp(op->args[1]);
4487
4488 /* ENV should not be modified. */
e01fa97d 4489 tcg_debug_assert(!temp_readonly(ots));
bab1671f
RH
4490
4491 itype = its->type;
4492 vece = TCGOP_VECE(op);
4493 vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
4494
4495 if (its->val_type == TEMP_VAL_CONST) {
4496 /* Propagate constant via movi -> dupi. */
4497 tcg_target_ulong val = its->val;
4498 if (IS_DEAD_ARG(1)) {
4499 temp_dead(s, its);
4500 }
31fd884b 4501 tcg_reg_alloc_do_movi(s, ots, val, arg_life, output_pref(op, 0));
bab1671f
RH
4502 return;
4503 }
4504
9be0d080
RH
4505 dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
4506 dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].regs;
bab1671f
RH
4507
4508 /* Allocate the output register now. */
4509 if (ots->val_type != TEMP_VAL_REG) {
4510 TCGRegSet allocated_regs = s->reserved_regs;
098859f1 4511 TCGReg oreg;
bab1671f
RH
4512
4513 if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) {
4514 /* Make sure to not spill the input register. */
4515 tcg_regset_set_reg(allocated_regs, its->reg);
4516 }
098859f1 4517 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
31fd884b 4518 output_pref(op, 0), ots->indirect_base);
098859f1 4519 set_temp_val_reg(s, ots, oreg);
bab1671f
RH
4520 }
4521
4522 switch (its->val_type) {
4523 case TEMP_VAL_REG:
4524 /*
4525 * The dup constriaints must be broad, covering all possible VECE.
4526 * However, tcg_op_dup_vec() gets to see the VECE and we allow it
4527 * to fail, indicating that extra moves are required for that case.
4528 */
4529 if (tcg_regset_test_reg(dup_in_regs, its->reg)) {
4530 if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) {
4531 goto done;
4532 }
4533 /* Try again from memory or a vector input register. */
4534 }
4535 if (!its->mem_coherent) {
4536 /*
4537 * The input register is not synced, and so an extra store
4538 * would be required to use memory. Attempt an integer-vector
4539 * register move first. We do not have a TCGRegSet for this.
4540 */
4541 if (tcg_out_mov(s, itype, ots->reg, its->reg)) {
4542 break;
4543 }
4544 /* Sync the temp back to its slot and load from there. */
4545 temp_sync(s, its, s->reserved_regs, 0, 0);
4546 }
4547 /* fall through */
4548
4549 case TEMP_VAL_MEM:
31c96417
RH
4550 lowpart_ofs = 0;
4551 if (HOST_BIG_ENDIAN) {
4552 lowpart_ofs = tcg_type_size(itype) - (1 << vece);
4553 }
d6ecb4a9 4554 if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
31c96417 4555 its->mem_offset + lowpart_ofs)) {
d6ecb4a9
RH
4556 goto done;
4557 }
098859f1 4558 /* Load the input into the destination vector register. */
bab1671f
RH
4559 tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
4560 break;
4561
4562 default:
4563 g_assert_not_reached();
4564 }
4565
4566 /* We now have a vector input register, so dup must succeed. */
4567 ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg);
4568 tcg_debug_assert(ok);
4569
4570 done:
36f5539c 4571 ots->mem_coherent = 0;
bab1671f
RH
4572 if (IS_DEAD_ARG(1)) {
4573 temp_dead(s, its);
4574 }
4575 if (NEED_SYNC_ARG(0)) {
4576 temp_sync(s, ots, s->reserved_regs, 0, 0);
4577 }
4578 if (IS_DEAD_ARG(0)) {
4579 temp_dead(s, ots);
4580 }
4581}
4582
dd186292 4583static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
c896fe29 4584{
dd186292
RH
4585 const TCGLifeData arg_life = op->life;
4586 const TCGOpDef * const def = &tcg_op_defs[op->opc];
82790a87
RH
4587 TCGRegSet i_allocated_regs;
4588 TCGRegSet o_allocated_regs;
b6638662
RH
4589 int i, k, nb_iargs, nb_oargs;
4590 TCGReg reg;
c896fe29
FB
4591 TCGArg arg;
4592 const TCGArgConstraint *arg_ct;
4593 TCGTemp *ts;
4594 TCGArg new_args[TCG_MAX_OP_ARGS];
4595 int const_args[TCG_MAX_OP_ARGS];
4596
4597 nb_oargs = def->nb_oargs;
4598 nb_iargs = def->nb_iargs;
4599
4600 /* copy constants */
a813e36f 4601 memcpy(new_args + nb_oargs + nb_iargs,
dd186292 4602 op->args + nb_oargs + nb_iargs,
c896fe29
FB
4603 sizeof(TCGArg) * def->nb_cargs);
4604
d21369f5
RH
4605 i_allocated_regs = s->reserved_regs;
4606 o_allocated_regs = s->reserved_regs;
82790a87 4607
a813e36f 4608 /* satisfy input constraints */
dd186292 4609 for (k = 0; k < nb_iargs; k++) {
29f5e925
RH
4610 TCGRegSet i_preferred_regs, i_required_regs;
4611 bool allocate_new_reg, copyto_new_reg;
4612 TCGTemp *ts2;
4613 int i1, i2;
d62816f2 4614
66792f90 4615 i = def->args_ct[nb_oargs + k].sort_index;
dd186292 4616 arg = op->args[i];
c896fe29 4617 arg_ct = &def->args_ct[i];
43439139 4618 ts = arg_temp(arg);
40ae5c62
RH
4619
4620 if (ts->val_type == TEMP_VAL_CONST
a4fbbd77 4621 && tcg_target_const_match(ts->val, ts->type, arg_ct->ct)) {
40ae5c62
RH
4622 /* constant is OK for instruction */
4623 const_args[i] = 1;
4624 new_args[i] = ts->val;
d62816f2 4625 continue;
c896fe29 4626 }
40ae5c62 4627
1c1824dc
RH
4628 reg = ts->reg;
4629 i_preferred_regs = 0;
29f5e925 4630 i_required_regs = arg_ct->regs;
1c1824dc 4631 allocate_new_reg = false;
29f5e925
RH
4632 copyto_new_reg = false;
4633
4634 switch (arg_ct->pair) {
4635 case 0: /* not paired */
4636 if (arg_ct->ialias) {
31fd884b 4637 i_preferred_regs = output_pref(op, arg_ct->alias_index);
29f5e925
RH
4638
4639 /*
4640 * If the input is readonly, then it cannot also be an
4641 * output and aliased to itself. If the input is not
4642 * dead after the instruction, we must allocate a new
4643 * register and move it.
4644 */
4645 if (temp_readonly(ts) || !IS_DEAD_ARG(i)) {
4646 allocate_new_reg = true;
4647 } else if (ts->val_type == TEMP_VAL_REG) {
4648 /*
4649 * Check if the current register has already been
4650 * allocated for another input.
4651 */
4652 allocate_new_reg =
4653 tcg_regset_test_reg(i_allocated_regs, reg);
4654 }
4655 }
4656 if (!allocate_new_reg) {
4657 temp_load(s, ts, i_required_regs, i_allocated_regs,
4658 i_preferred_regs);
4659 reg = ts->reg;
4660 allocate_new_reg = !tcg_regset_test_reg(i_required_regs, reg);
4661 }
4662 if (allocate_new_reg) {
4663 /*
4664 * Allocate a new register matching the constraint
4665 * and move the temporary register into it.
4666 */
4667 temp_load(s, ts, tcg_target_available_regs[ts->type],
4668 i_allocated_regs, 0);
4669 reg = tcg_reg_alloc(s, i_required_regs, i_allocated_regs,
4670 i_preferred_regs, ts->indirect_base);
4671 copyto_new_reg = true;
4672 }
4673 break;
4674
4675 case 1:
4676 /* First of an input pair; if i1 == i2, the second is an output. */
4677 i1 = i;
4678 i2 = arg_ct->pair_index;
4679 ts2 = i1 != i2 ? arg_temp(op->args[i2]) : NULL;
4680
4681 /*
4682 * It is easier to default to allocating a new pair
4683 * and to identify a few cases where it's not required.
4684 */
4685 if (arg_ct->ialias) {
31fd884b 4686 i_preferred_regs = output_pref(op, arg_ct->alias_index);
29f5e925
RH
4687 if (IS_DEAD_ARG(i1) &&
4688 IS_DEAD_ARG(i2) &&
4689 !temp_readonly(ts) &&
4690 ts->val_type == TEMP_VAL_REG &&
4691 ts->reg < TCG_TARGET_NB_REGS - 1 &&
4692 tcg_regset_test_reg(i_required_regs, reg) &&
4693 !tcg_regset_test_reg(i_allocated_regs, reg) &&
4694 !tcg_regset_test_reg(i_allocated_regs, reg + 1) &&
4695 (ts2
4696 ? ts2->val_type == TEMP_VAL_REG &&
4697 ts2->reg == reg + 1 &&
4698 !temp_readonly(ts2)
4699 : s->reg_to_temp[reg + 1] == NULL)) {
4700 break;
4701 }
4702 } else {
4703 /* Without aliasing, the pair must also be an input. */
4704 tcg_debug_assert(ts2);
4705 if (ts->val_type == TEMP_VAL_REG &&
4706 ts2->val_type == TEMP_VAL_REG &&
4707 ts2->reg == reg + 1 &&
4708 tcg_regset_test_reg(i_required_regs, reg)) {
4709 break;
4710 }
4711 }
4712 reg = tcg_reg_alloc_pair(s, i_required_regs, i_allocated_regs,
4713 0, ts->indirect_base);
4714 goto do_pair;
4715
4716 case 2: /* pair second */
4717 reg = new_args[arg_ct->pair_index] + 1;
4718 goto do_pair;
1c1824dc 4719
29f5e925
RH
4720 case 3: /* ialias with second output, no first input */
4721 tcg_debug_assert(arg_ct->ialias);
31fd884b 4722 i_preferred_regs = output_pref(op, arg_ct->alias_index);
d62816f2 4723
29f5e925
RH
4724 if (IS_DEAD_ARG(i) &&
4725 !temp_readonly(ts) &&
4726 ts->val_type == TEMP_VAL_REG &&
4727 reg > 0 &&
4728 s->reg_to_temp[reg - 1] == NULL &&
4729 tcg_regset_test_reg(i_required_regs, reg) &&
4730 !tcg_regset_test_reg(i_allocated_regs, reg) &&
4731 !tcg_regset_test_reg(i_allocated_regs, reg - 1)) {
4732 tcg_regset_set_reg(i_allocated_regs, reg - 1);
4733 break;
4734 }
4735 reg = tcg_reg_alloc_pair(s, i_required_regs >> 1,
4736 i_allocated_regs, 0,
4737 ts->indirect_base);
4738 tcg_regset_set_reg(i_allocated_regs, reg);
4739 reg += 1;
4740 goto do_pair;
4741
4742 do_pair:
c0522136 4743 /*
29f5e925
RH
4744 * If an aliased input is not dead after the instruction,
4745 * we must allocate a new register and move it.
c0522136 4746 */
29f5e925
RH
4747 if (arg_ct->ialias && (!IS_DEAD_ARG(i) || temp_readonly(ts))) {
4748 TCGRegSet t_allocated_regs = i_allocated_regs;
4749
1c1824dc 4750 /*
29f5e925
RH
4751 * Because of the alias, and the continued life, make sure
4752 * that the temp is somewhere *other* than the reg pair,
4753 * and we get a copy in reg.
1c1824dc 4754 */
29f5e925
RH
4755 tcg_regset_set_reg(t_allocated_regs, reg);
4756 tcg_regset_set_reg(t_allocated_regs, reg + 1);
4757 if (ts->val_type == TEMP_VAL_REG && ts->reg == reg) {
4758 /* If ts was already in reg, copy it somewhere else. */
4759 TCGReg nr;
4760 bool ok;
4761
4762 tcg_debug_assert(ts->kind != TEMP_FIXED);
4763 nr = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
4764 t_allocated_regs, 0, ts->indirect_base);
4765 ok = tcg_out_mov(s, ts->type, nr, reg);
4766 tcg_debug_assert(ok);
4767
4768 set_temp_val_reg(s, ts, nr);
4769 } else {
4770 temp_load(s, ts, tcg_target_available_regs[ts->type],
4771 t_allocated_regs, 0);
4772 copyto_new_reg = true;
4773 }
4774 } else {
4775 /* Preferably allocate to reg, otherwise copy. */
4776 i_required_regs = (TCGRegSet)1 << reg;
4777 temp_load(s, ts, i_required_regs, i_allocated_regs,
4778 i_preferred_regs);
4779 copyto_new_reg = ts->reg != reg;
5ff9d6a4 4780 }
29f5e925 4781 break;
d62816f2 4782
29f5e925
RH
4783 default:
4784 g_assert_not_reached();
1c1824dc 4785 }
d62816f2 4786
29f5e925 4787 if (copyto_new_reg) {
78113e83 4788 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
240c08d0
RH
4789 /*
4790 * Cross register class move not supported. Sync the
4791 * temp back to its slot and load from there.
4792 */
4793 temp_sync(s, ts, i_allocated_regs, 0, 0);
4794 tcg_out_ld(s, ts->type, reg,
4795 ts->mem_base->reg, ts->mem_offset);
78113e83 4796 }
c896fe29 4797 }
c896fe29
FB
4798 new_args[i] = reg;
4799 const_args[i] = 0;
82790a87 4800 tcg_regset_set_reg(i_allocated_regs, reg);
c896fe29 4801 }
a813e36f 4802
a52ad07e
AJ
4803 /* mark dead temporaries and free the associated registers */
4804 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
4805 if (IS_DEAD_ARG(i)) {
43439139 4806 temp_dead(s, arg_temp(op->args[i]));
a52ad07e
AJ
4807 }
4808 }
4809
b4cb76e6
RH
4810 if (def->flags & TCG_OPF_COND_BRANCH) {
4811 tcg_reg_alloc_cbranch(s, i_allocated_regs);
4812 } else if (def->flags & TCG_OPF_BB_END) {
82790a87 4813 tcg_reg_alloc_bb_end(s, i_allocated_regs);
e8996ee0 4814 } else {
e8996ee0 4815 if (def->flags & TCG_OPF_CALL_CLOBBER) {
a813e36f 4816 /* XXX: permit generic clobber register list ? */
c8074023
RH
4817 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
4818 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
82790a87 4819 tcg_reg_free(s, i, i_allocated_regs);
e8996ee0 4820 }
c896fe29 4821 }
3d5c5f87
AJ
4822 }
4823 if (def->flags & TCG_OPF_SIDE_EFFECTS) {
4824 /* sync globals if the op has side effects and might trigger
4825 an exception. */
82790a87 4826 sync_globals(s, i_allocated_regs);
c896fe29 4827 }
a813e36f 4828
e8996ee0 4829 /* satisfy the output constraints */
e8996ee0 4830 for(k = 0; k < nb_oargs; k++) {
66792f90 4831 i = def->args_ct[k].sort_index;
dd186292 4832 arg = op->args[i];
e8996ee0 4833 arg_ct = &def->args_ct[i];
43439139 4834 ts = arg_temp(arg);
d63e3b6e
RH
4835
4836 /* ENV should not be modified. */
e01fa97d 4837 tcg_debug_assert(!temp_readonly(ts));
d63e3b6e 4838
29f5e925
RH
4839 switch (arg_ct->pair) {
4840 case 0: /* not paired */
4841 if (arg_ct->oalias && !const_args[arg_ct->alias_index]) {
4842 reg = new_args[arg_ct->alias_index];
4843 } else if (arg_ct->newreg) {
4844 reg = tcg_reg_alloc(s, arg_ct->regs,
4845 i_allocated_regs | o_allocated_regs,
31fd884b 4846 output_pref(op, k), ts->indirect_base);
29f5e925
RH
4847 } else {
4848 reg = tcg_reg_alloc(s, arg_ct->regs, o_allocated_regs,
31fd884b 4849 output_pref(op, k), ts->indirect_base);
29f5e925
RH
4850 }
4851 break;
4852
4853 case 1: /* first of pair */
4854 tcg_debug_assert(!arg_ct->newreg);
4855 if (arg_ct->oalias) {
4856 reg = new_args[arg_ct->alias_index];
4857 break;
4858 }
4859 reg = tcg_reg_alloc_pair(s, arg_ct->regs, o_allocated_regs,
31fd884b 4860 output_pref(op, k), ts->indirect_base);
29f5e925
RH
4861 break;
4862
4863 case 2: /* second of pair */
4864 tcg_debug_assert(!arg_ct->newreg);
4865 if (arg_ct->oalias) {
4866 reg = new_args[arg_ct->alias_index];
4867 } else {
4868 reg = new_args[arg_ct->pair_index] + 1;
4869 }
4870 break;
4871
4872 case 3: /* first of pair, aliasing with a second input */
4873 tcg_debug_assert(!arg_ct->newreg);
4874 reg = new_args[arg_ct->pair_index] - 1;
4875 break;
4876
4877 default:
4878 g_assert_not_reached();
c896fe29 4879 }
82790a87 4880 tcg_regset_set_reg(o_allocated_regs, reg);
098859f1 4881 set_temp_val_reg(s, ts, reg);
d63e3b6e 4882 ts->mem_coherent = 0;
e8996ee0 4883 new_args[i] = reg;
c896fe29 4884 }
c896fe29
FB
4885 }
4886
c896fe29 4887 /* emit instruction */
678155b2
RH
4888 switch (op->opc) {
4889 case INDEX_op_ext8s_i32:
4890 tcg_out_ext8s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
4891 break;
4892 case INDEX_op_ext8s_i64:
4893 tcg_out_ext8s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
4894 break;
d0e66c89
RH
4895 case INDEX_op_ext8u_i32:
4896 case INDEX_op_ext8u_i64:
4897 tcg_out_ext8u(s, new_args[0], new_args[1]);
4898 break;
753e42ea
RH
4899 case INDEX_op_ext16s_i32:
4900 tcg_out_ext16s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
4901 break;
4902 case INDEX_op_ext16s_i64:
4903 tcg_out_ext16s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
4904 break;
379afdff
RH
4905 case INDEX_op_ext16u_i32:
4906 case INDEX_op_ext16u_i64:
4907 tcg_out_ext16u(s, new_args[0], new_args[1]);
4908 break;
52bf3398
RH
4909 case INDEX_op_ext32s_i64:
4910 tcg_out_ext32s(s, new_args[0], new_args[1]);
4911 break;
9ecf5f61
RH
4912 case INDEX_op_ext32u_i64:
4913 tcg_out_ext32u(s, new_args[0], new_args[1]);
4914 break;
9c6aa274
RH
4915 case INDEX_op_ext_i32_i64:
4916 tcg_out_exts_i32_i64(s, new_args[0], new_args[1]);
4917 break;
b9bfe000
RH
4918 case INDEX_op_extu_i32_i64:
4919 tcg_out_extu_i32_i64(s, new_args[0], new_args[1]);
4920 break;
b8b94ac6
RH
4921 case INDEX_op_extrl_i64_i32:
4922 tcg_out_extrl_i64_i32(s, new_args[0], new_args[1]);
4923 break;
678155b2
RH
4924 default:
4925 if (def->flags & TCG_OPF_VECTOR) {
4926 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
4927 new_args, const_args);
4928 } else {
4929 tcg_out_op(s, op->opc, new_args, const_args);
4930 }
4931 break;
d2fd745f
RH
4932 }
4933
c896fe29
FB
4934 /* move the outputs in the correct register if needed */
4935 for(i = 0; i < nb_oargs; i++) {
43439139 4936 ts = arg_temp(op->args[i]);
d63e3b6e
RH
4937
4938 /* ENV should not be modified. */
e01fa97d 4939 tcg_debug_assert(!temp_readonly(ts));
d63e3b6e 4940
ec7a869d 4941 if (NEED_SYNC_ARG(i)) {
98b4e186 4942 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
59d7c14e 4943 } else if (IS_DEAD_ARG(i)) {
f8bf00f1 4944 temp_dead(s, ts);
ec7a869d 4945 }
c896fe29
FB
4946 }
4947}
4948
efe86b21
RH
4949static bool tcg_reg_alloc_dup2(TCGContext *s, const TCGOp *op)
4950{
4951 const TCGLifeData arg_life = op->life;
4952 TCGTemp *ots, *itsl, *itsh;
4953 TCGType vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
4954
4955 /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */
4956 tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
4957 tcg_debug_assert(TCGOP_VECE(op) == MO_64);
4958
4959 ots = arg_temp(op->args[0]);
4960 itsl = arg_temp(op->args[1]);
4961 itsh = arg_temp(op->args[2]);
4962
4963 /* ENV should not be modified. */
4964 tcg_debug_assert(!temp_readonly(ots));
4965
4966 /* Allocate the output register now. */
4967 if (ots->val_type != TEMP_VAL_REG) {
4968 TCGRegSet allocated_regs = s->reserved_regs;
4969 TCGRegSet dup_out_regs =
4970 tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
098859f1 4971 TCGReg oreg;
efe86b21
RH
4972
4973 /* Make sure to not spill the input registers. */
4974 if (!IS_DEAD_ARG(1) && itsl->val_type == TEMP_VAL_REG) {
4975 tcg_regset_set_reg(allocated_regs, itsl->reg);
4976 }
4977 if (!IS_DEAD_ARG(2) && itsh->val_type == TEMP_VAL_REG) {
4978 tcg_regset_set_reg(allocated_regs, itsh->reg);
4979 }
4980
098859f1 4981 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
31fd884b 4982 output_pref(op, 0), ots->indirect_base);
098859f1 4983 set_temp_val_reg(s, ots, oreg);
efe86b21
RH
4984 }
4985
4986 /* Promote dup2 of immediates to dupi_vec. */
4987 if (itsl->val_type == TEMP_VAL_CONST && itsh->val_type == TEMP_VAL_CONST) {
4988 uint64_t val = deposit64(itsl->val, 32, 32, itsh->val);
4989 MemOp vece = MO_64;
4990
4991 if (val == dup_const(MO_8, val)) {
4992 vece = MO_8;
4993 } else if (val == dup_const(MO_16, val)) {
4994 vece = MO_16;
4995 } else if (val == dup_const(MO_32, val)) {
4996 vece = MO_32;
4997 }
4998
4999 tcg_out_dupi_vec(s, vtype, vece, ots->reg, val);
5000 goto done;
5001 }
5002
5003 /* If the two inputs form one 64-bit value, try dupm_vec. */
aef85402
RH
5004 if (itsl->temp_subindex == HOST_BIG_ENDIAN &&
5005 itsh->temp_subindex == !HOST_BIG_ENDIAN &&
5006 itsl == itsh + (HOST_BIG_ENDIAN ? 1 : -1)) {
5007 TCGTemp *its = itsl - HOST_BIG_ENDIAN;
5008
5009 temp_sync(s, its + 0, s->reserved_regs, 0, 0);
5010 temp_sync(s, its + 1, s->reserved_regs, 0, 0);
5011
efe86b21
RH
5012 if (tcg_out_dupm_vec(s, vtype, MO_64, ots->reg,
5013 its->mem_base->reg, its->mem_offset)) {
5014 goto done;
5015 }
5016 }
5017
5018 /* Fall back to generic expansion. */
5019 return false;
5020
5021 done:
36f5539c 5022 ots->mem_coherent = 0;
efe86b21
RH
5023 if (IS_DEAD_ARG(1)) {
5024 temp_dead(s, itsl);
5025 }
5026 if (IS_DEAD_ARG(2)) {
5027 temp_dead(s, itsh);
5028 }
5029 if (NEED_SYNC_ARG(0)) {
5030 temp_sync(s, ots, s->reserved_regs, 0, IS_DEAD_ARG(0));
5031 } else if (IS_DEAD_ARG(0)) {
5032 temp_dead(s, ots);
5033 }
5034 return true;
5035}
5036
39004a71
RH
5037static void load_arg_reg(TCGContext *s, TCGReg reg, TCGTemp *ts,
5038 TCGRegSet allocated_regs)
c896fe29 5039{
39004a71
RH
5040 if (ts->val_type == TEMP_VAL_REG) {
5041 if (ts->reg != reg) {
5042 tcg_reg_free(s, reg, allocated_regs);
5043 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
5044 /*
5045 * Cross register class move not supported. Sync the
5046 * temp back to its slot and load from there.
5047 */
5048 temp_sync(s, ts, allocated_regs, 0, 0);
5049 tcg_out_ld(s, ts->type, reg,
5050 ts->mem_base->reg, ts->mem_offset);
5051 }
5052 }
5053 } else {
5054 TCGRegSet arg_set = 0;
c896fe29 5055
39004a71
RH
5056 tcg_reg_free(s, reg, allocated_regs);
5057 tcg_regset_set_reg(arg_set, reg);
5058 temp_load(s, ts, arg_set, allocated_regs, 0);
b03cce8e 5059 }
39004a71 5060}
39cf05d3 5061
d78e4a4f 5062static void load_arg_stk(TCGContext *s, unsigned arg_slot, TCGTemp *ts,
39004a71
RH
5063 TCGRegSet allocated_regs)
5064{
5065 /*
5066 * When the destination is on the stack, load up the temp and store.
5067 * If there are many call-saved registers, the temp might live to
5068 * see another use; otherwise it'll be discarded.
5069 */
5070 temp_load(s, ts, tcg_target_available_regs[ts->type], allocated_regs, 0);
5071 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK,
d78e4a4f 5072 arg_slot_stk_ofs(arg_slot));
39004a71 5073}
a813e36f 5074
39004a71
RH
5075static void load_arg_normal(TCGContext *s, const TCGCallArgumentLoc *l,
5076 TCGTemp *ts, TCGRegSet *allocated_regs)
5077{
338b61e9 5078 if (arg_slot_reg_p(l->arg_slot)) {
39004a71
RH
5079 TCGReg reg = tcg_target_call_iarg_regs[l->arg_slot];
5080 load_arg_reg(s, reg, ts, *allocated_regs);
5081 tcg_regset_set_reg(*allocated_regs, reg);
5082 } else {
d78e4a4f 5083 load_arg_stk(s, l->arg_slot, ts, *allocated_regs);
39004a71
RH
5084 }
5085}
40ae5c62 5086
d78e4a4f 5087static void load_arg_ref(TCGContext *s, unsigned arg_slot, TCGReg ref_base,
313bdea8
RH
5088 intptr_t ref_off, TCGRegSet *allocated_regs)
5089{
5090 TCGReg reg;
313bdea8 5091
d78e4a4f 5092 if (arg_slot_reg_p(arg_slot)) {
313bdea8
RH
5093 reg = tcg_target_call_iarg_regs[arg_slot];
5094 tcg_reg_free(s, reg, *allocated_regs);
5095 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
5096 tcg_regset_set_reg(*allocated_regs, reg);
5097 } else {
5098 reg = tcg_reg_alloc(s, tcg_target_available_regs[TCG_TYPE_PTR],
5099 *allocated_regs, 0, false);
5100 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
5101 tcg_out_st(s, TCG_TYPE_PTR, reg, TCG_REG_CALL_STACK,
d78e4a4f 5102 arg_slot_stk_ofs(arg_slot));
313bdea8
RH
5103 }
5104}
5105
39004a71
RH
5106static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
5107{
5108 const int nb_oargs = TCGOP_CALLO(op);
5109 const int nb_iargs = TCGOP_CALLI(op);
5110 const TCGLifeData arg_life = op->life;
5111 const TCGHelperInfo *info = tcg_call_info(op);
5112 TCGRegSet allocated_regs = s->reserved_regs;
5113 int i;
40ae5c62 5114
39004a71
RH
5115 /*
5116 * Move inputs into place in reverse order,
5117 * so that we place stacked arguments first.
5118 */
5119 for (i = nb_iargs - 1; i >= 0; --i) {
5120 const TCGCallArgumentLoc *loc = &info->in[i];
5121 TCGTemp *ts = arg_temp(op->args[nb_oargs + i]);
40ae5c62 5122
39004a71
RH
5123 switch (loc->kind) {
5124 case TCG_CALL_ARG_NORMAL:
5125 case TCG_CALL_ARG_EXTEND_U:
5126 case TCG_CALL_ARG_EXTEND_S:
5127 load_arg_normal(s, loc, ts, &allocated_regs);
5128 break;
313bdea8
RH
5129 case TCG_CALL_ARG_BY_REF:
5130 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
5131 load_arg_ref(s, loc->arg_slot, TCG_REG_CALL_STACK,
d78e4a4f 5132 arg_slot_stk_ofs(loc->ref_slot),
313bdea8
RH
5133 &allocated_regs);
5134 break;
5135 case TCG_CALL_ARG_BY_REF_N:
5136 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
5137 break;
39004a71
RH
5138 default:
5139 g_assert_not_reached();
c896fe29 5140 }
c896fe29 5141 }
a813e36f 5142
39004a71 5143 /* Mark dead temporaries and free the associated registers. */
dd186292 5144 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
866cb6cb 5145 if (IS_DEAD_ARG(i)) {
43439139 5146 temp_dead(s, arg_temp(op->args[i]));
c896fe29
FB
5147 }
5148 }
a813e36f 5149
39004a71 5150 /* Clobber call registers. */
c8074023
RH
5151 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
5152 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
b3915dbb 5153 tcg_reg_free(s, i, allocated_regs);
c896fe29
FB
5154 }
5155 }
78505279 5156
39004a71
RH
5157 /*
5158 * Save globals if they might be written by the helper,
5159 * sync them if they might be read.
5160 */
5161 if (info->flags & TCG_CALL_NO_READ_GLOBALS) {
78505279 5162 /* Nothing to do */
39004a71 5163 } else if (info->flags & TCG_CALL_NO_WRITE_GLOBALS) {
78505279
AJ
5164 sync_globals(s, allocated_regs);
5165 } else {
b9c18f56
AJ
5166 save_globals(s, allocated_regs);
5167 }
c896fe29 5168
313bdea8
RH
5169 /*
5170 * If the ABI passes a pointer to the returned struct as the first
5171 * argument, load that now. Pass a pointer to the output home slot.
5172 */
5173 if (info->out_kind == TCG_CALL_RET_BY_REF) {
5174 TCGTemp *ts = arg_temp(op->args[0]);
5175
5176 if (!ts->mem_allocated) {
5177 temp_allocate_frame(s, ts);
5178 }
5179 load_arg_ref(s, 0, ts->mem_base->reg, ts->mem_offset, &allocated_regs);
5180 }
5181
cee44b03 5182 tcg_out_call(s, tcg_call_func(op), info);
c896fe29 5183
39004a71
RH
5184 /* Assign output registers and emit moves if needed. */
5185 switch (info->out_kind) {
5186 case TCG_CALL_RET_NORMAL:
5187 for (i = 0; i < nb_oargs; i++) {
5188 TCGTemp *ts = arg_temp(op->args[i]);
5e3d0c19 5189 TCGReg reg = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, i);
d63e3b6e 5190
39004a71
RH
5191 /* ENV should not be modified. */
5192 tcg_debug_assert(!temp_readonly(ts));
d63e3b6e 5193
39004a71
RH
5194 set_temp_val_reg(s, ts, reg);
5195 ts->mem_coherent = 0;
5196 }
5197 break;
313bdea8 5198
c6556aa0
RH
5199 case TCG_CALL_RET_BY_VEC:
5200 {
5201 TCGTemp *ts = arg_temp(op->args[0]);
5202
5203 tcg_debug_assert(ts->base_type == TCG_TYPE_I128);
5204 tcg_debug_assert(ts->temp_subindex == 0);
5205 if (!ts->mem_allocated) {
5206 temp_allocate_frame(s, ts);
5207 }
5208 tcg_out_st(s, TCG_TYPE_V128,
5209 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
5210 ts->mem_base->reg, ts->mem_offset);
5211 }
5212 /* fall through to mark all parts in memory */
5213
313bdea8
RH
5214 case TCG_CALL_RET_BY_REF:
5215 /* The callee has performed a write through the reference. */
5216 for (i = 0; i < nb_oargs; i++) {
5217 TCGTemp *ts = arg_temp(op->args[i]);
5218 ts->val_type = TEMP_VAL_MEM;
5219 }
5220 break;
5221
39004a71
RH
5222 default:
5223 g_assert_not_reached();
5224 }
5225
5226 /* Flush or discard output registers as needed. */
5227 for (i = 0; i < nb_oargs; i++) {
5228 TCGTemp *ts = arg_temp(op->args[i]);
d63e3b6e 5229 if (NEED_SYNC_ARG(i)) {
39004a71 5230 temp_sync(s, ts, s->reserved_regs, 0, IS_DEAD_ARG(i));
d63e3b6e
RH
5231 } else if (IS_DEAD_ARG(i)) {
5232 temp_dead(s, ts);
c896fe29
FB
5233 }
5234 }
c896fe29
FB
5235}
5236
e63b8a29
RH
5237/**
5238 * atom_and_align_for_opc:
5239 * @s: tcg context
5240 * @opc: memory operation code
5241 * @host_atom: MO_ATOM_{IFALIGN,WITHIN16,SUBALIGN} for host operations
5242 * @allow_two_ops: true if we are prepared to issue two operations
5243 *
5244 * Return the alignment and atomicity to use for the inline fast path
5245 * for the given memory operation. The alignment may be larger than
5246 * that specified in @opc, and the correct alignment will be diagnosed
5247 * by the slow path helper.
5248 *
5249 * If @allow_two_ops, the host is prepared to test for 2x alignment,
5250 * and issue two loads or stores for subalignment.
5251 */
5252static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc,
5253 MemOp host_atom, bool allow_two_ops)
5254{
5255 MemOp align = get_alignment_bits(opc);
5256 MemOp size = opc & MO_SIZE;
5257 MemOp half = size ? size - 1 : 0;
5258 MemOp atmax;
5259 MemOp atom;
5260
5261 /* When serialized, no further atomicity required. */
5262 if (s->gen_tb->cflags & CF_PARALLEL) {
5263 atom = opc & MO_ATOM_MASK;
5264 } else {
5265 atom = MO_ATOM_NONE;
5266 }
5267
5268 switch (atom) {
5269 case MO_ATOM_NONE:
5270 /* The operation requires no specific atomicity. */
5271 atmax = MO_8;
5272 break;
5273
5274 case MO_ATOM_IFALIGN:
5275 atmax = size;
5276 break;
5277
5278 case MO_ATOM_IFALIGN_PAIR:
5279 atmax = half;
5280 break;
5281
5282 case MO_ATOM_WITHIN16:
5283 atmax = size;
5284 if (size == MO_128) {
5285 /* Misalignment implies !within16, and therefore no atomicity. */
5286 } else if (host_atom != MO_ATOM_WITHIN16) {
5287 /* The host does not implement within16, so require alignment. */
5288 align = MAX(align, size);
5289 }
5290 break;
5291
5292 case MO_ATOM_WITHIN16_PAIR:
5293 atmax = size;
5294 /*
5295 * Misalignment implies !within16, and therefore half atomicity.
5296 * Any host prepared for two operations can implement this with
5297 * half alignment.
5298 */
5299 if (host_atom != MO_ATOM_WITHIN16 && allow_two_ops) {
5300 align = MAX(align, half);
5301 }
5302 break;
5303
5304 case MO_ATOM_SUBALIGN:
5305 atmax = size;
5306 if (host_atom != MO_ATOM_SUBALIGN) {
5307 /* If unaligned but not odd, there are subobjects up to half. */
5308 if (allow_two_ops) {
5309 align = MAX(align, half);
5310 } else {
5311 align = MAX(align, size);
5312 }
5313 }
5314 break;
5315
5316 default:
5317 g_assert_not_reached();
5318 }
5319
5320 return (TCGAtomAlign){ .atom = atmax, .align = align };
5321}
5322
8429a1ca
RH
5323/*
5324 * Similarly for qemu_ld/st slow path helpers.
5325 * We must re-implement tcg_gen_callN and tcg_reg_alloc_call simultaneously,
5326 * using only the provided backend tcg_out_* functions.
5327 */
5328
5329static int tcg_out_helper_stk_ofs(TCGType type, unsigned slot)
5330{
5331 int ofs = arg_slot_stk_ofs(slot);
5332
5333 /*
5334 * Each stack slot is TCG_TARGET_LONG_BITS. If the host does not
5335 * require extension to uint64_t, adjust the address for uint32_t.
5336 */
5337 if (HOST_BIG_ENDIAN &&
5338 TCG_TARGET_REG_BITS == 64 &&
5339 type == TCG_TYPE_I32) {
5340 ofs += 4;
5341 }
5342 return ofs;
5343}
5344
8d314041
RH
5345static void tcg_out_helper_load_slots(TCGContext *s,
5346 unsigned nmov, TCGMovExtend *mov,
5347 const TCGLdstHelperParam *parm)
8429a1ca 5348{
8d314041 5349 unsigned i;
2462e30e
RH
5350 TCGReg dst3;
5351
8d314041
RH
5352 /*
5353 * Start from the end, storing to the stack first.
5354 * This frees those registers, so we need not consider overlap.
5355 */
5356 for (i = nmov; i-- > 0; ) {
5357 unsigned slot = mov[i].dst;
5358
5359 if (arg_slot_reg_p(slot)) {
5360 goto found_reg;
5361 }
5362
5363 TCGReg src = mov[i].src;
5364 TCGType dst_type = mov[i].dst_type;
5365 MemOp dst_mo = dst_type == TCG_TYPE_I32 ? MO_32 : MO_64;
5366
5367 /* The argument is going onto the stack; extend into scratch. */
5368 if ((mov[i].src_ext & MO_SIZE) != dst_mo) {
5369 tcg_debug_assert(parm->ntmp != 0);
5370 mov[i].dst = src = parm->tmp[0];
5371 tcg_out_movext1(s, &mov[i]);
5372 }
5373
5374 tcg_out_st(s, dst_type, src, TCG_REG_CALL_STACK,
5375 tcg_out_helper_stk_ofs(dst_type, slot));
5376 }
5377 return;
5378
5379 found_reg:
5380 /*
5381 * The remaining arguments are in registers.
5382 * Convert slot numbers to argument registers.
5383 */
5384 nmov = i + 1;
5385 for (i = 0; i < nmov; ++i) {
5386 mov[i].dst = tcg_target_call_iarg_regs[mov[i].dst];
5387 }
5388
8429a1ca 5389 switch (nmov) {
2462e30e 5390 case 4:
8429a1ca 5391 /* The backend must have provided enough temps for the worst case. */
2462e30e 5392 tcg_debug_assert(parm->ntmp >= 2);
8429a1ca 5393
2462e30e
RH
5394 dst3 = mov[3].dst;
5395 for (unsigned j = 0; j < 3; ++j) {
5396 if (dst3 == mov[j].src) {
5397 /*
5398 * Conflict. Copy the source to a temporary, perform the
5399 * remaining moves, then the extension from our scratch
5400 * on the way out.
5401 */
5402 TCGReg scratch = parm->tmp[1];
8429a1ca 5403
2462e30e
RH
5404 tcg_out_mov(s, mov[3].src_type, scratch, mov[3].src);
5405 tcg_out_movext3(s, mov, mov + 1, mov + 2, parm->tmp[0]);
5406 tcg_out_movext1_new_src(s, &mov[3], scratch);
5407 break;
8429a1ca 5408 }
8429a1ca 5409 }
8429a1ca 5410
2462e30e
RH
5411 /* No conflicts: perform this move and continue. */
5412 tcg_out_movext1(s, &mov[3]);
5413 /* fall through */
5414
5415 case 3:
5416 tcg_out_movext3(s, mov, mov + 1, mov + 2,
5417 parm->ntmp ? parm->tmp[0] : -1);
5418 break;
8429a1ca 5419 case 2:
2462e30e
RH
5420 tcg_out_movext2(s, mov, mov + 1,
5421 parm->ntmp ? parm->tmp[0] : -1);
5422 break;
8429a1ca
RH
5423 case 1:
5424 tcg_out_movext1(s, mov);
2462e30e
RH
5425 break;
5426 default:
8429a1ca
RH
5427 g_assert_not_reached();
5428 }
5429}
5430
8429a1ca
RH
5431static void tcg_out_helper_load_imm(TCGContext *s, unsigned slot,
5432 TCGType type, tcg_target_long imm,
5433 const TCGLdstHelperParam *parm)
5434{
5435 if (arg_slot_reg_p(slot)) {
5436 tcg_out_movi(s, type, tcg_target_call_iarg_regs[slot], imm);
5437 } else {
5438 int ofs = tcg_out_helper_stk_ofs(type, slot);
5439 if (!tcg_out_sti(s, type, imm, TCG_REG_CALL_STACK, ofs)) {
5440 tcg_debug_assert(parm->ntmp != 0);
5441 tcg_out_movi(s, type, parm->tmp[0], imm);
5442 tcg_out_st(s, type, parm->tmp[0], TCG_REG_CALL_STACK, ofs);
5443 }
5444 }
5445}
5446
5447static void tcg_out_helper_load_common_args(TCGContext *s,
5448 const TCGLabelQemuLdst *ldst,
5449 const TCGLdstHelperParam *parm,
5450 const TCGHelperInfo *info,
5451 unsigned next_arg)
5452{
5453 TCGMovExtend ptr_mov = {
5454 .dst_type = TCG_TYPE_PTR,
5455 .src_type = TCG_TYPE_PTR,
5456 .src_ext = sizeof(void *) == 4 ? MO_32 : MO_64
5457 };
5458 const TCGCallArgumentLoc *loc = &info->in[0];
5459 TCGType type;
5460 unsigned slot;
5461 tcg_target_ulong imm;
5462
5463 /*
5464 * Handle env, which is always first.
5465 */
5466 ptr_mov.dst = loc->arg_slot;
5467 ptr_mov.src = TCG_AREG0;
5468 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm);
5469
5470 /*
5471 * Handle oi.
5472 */
5473 imm = ldst->oi;
5474 loc = &info->in[next_arg];
5475 type = TCG_TYPE_I32;
5476 switch (loc->kind) {
5477 case TCG_CALL_ARG_NORMAL:
5478 break;
5479 case TCG_CALL_ARG_EXTEND_U:
5480 case TCG_CALL_ARG_EXTEND_S:
5481 /* No extension required for MemOpIdx. */
5482 tcg_debug_assert(imm <= INT32_MAX);
5483 type = TCG_TYPE_REG;
5484 break;
5485 default:
5486 g_assert_not_reached();
5487 }
5488 tcg_out_helper_load_imm(s, loc->arg_slot, type, imm, parm);
5489 next_arg++;
5490
5491 /*
5492 * Handle ra.
5493 */
5494 loc = &info->in[next_arg];
5495 slot = loc->arg_slot;
5496 if (parm->ra_gen) {
5497 int arg_reg = -1;
5498 TCGReg ra_reg;
5499
5500 if (arg_slot_reg_p(slot)) {
5501 arg_reg = tcg_target_call_iarg_regs[slot];
5502 }
5503 ra_reg = parm->ra_gen(s, ldst, arg_reg);
5504
5505 ptr_mov.dst = slot;
5506 ptr_mov.src = ra_reg;
5507 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm);
5508 } else {
5509 imm = (uintptr_t)ldst->raddr;
5510 tcg_out_helper_load_imm(s, slot, TCG_TYPE_PTR, imm, parm);
5511 }
5512}
5513
5514static unsigned tcg_out_helper_add_mov(TCGMovExtend *mov,
5515 const TCGCallArgumentLoc *loc,
5516 TCGType dst_type, TCGType src_type,
5517 TCGReg lo, TCGReg hi)
5518{
ebebea53
RH
5519 MemOp reg_mo;
5520
8429a1ca
RH
5521 if (dst_type <= TCG_TYPE_REG) {
5522 MemOp src_ext;
5523
5524 switch (loc->kind) {
5525 case TCG_CALL_ARG_NORMAL:
5526 src_ext = src_type == TCG_TYPE_I32 ? MO_32 : MO_64;
5527 break;
5528 case TCG_CALL_ARG_EXTEND_U:
5529 dst_type = TCG_TYPE_REG;
5530 src_ext = MO_UL;
5531 break;
5532 case TCG_CALL_ARG_EXTEND_S:
5533 dst_type = TCG_TYPE_REG;
5534 src_ext = MO_SL;
5535 break;
5536 default:
5537 g_assert_not_reached();
5538 }
5539
5540 mov[0].dst = loc->arg_slot;
5541 mov[0].dst_type = dst_type;
5542 mov[0].src = lo;
5543 mov[0].src_type = src_type;
5544 mov[0].src_ext = src_ext;
5545 return 1;
5546 }
5547
ebebea53
RH
5548 if (TCG_TARGET_REG_BITS == 32) {
5549 assert(dst_type == TCG_TYPE_I64);
5550 reg_mo = MO_32;
5551 } else {
5552 assert(dst_type == TCG_TYPE_I128);
5553 reg_mo = MO_64;
5554 }
8429a1ca
RH
5555
5556 mov[0].dst = loc[HOST_BIG_ENDIAN].arg_slot;
5557 mov[0].src = lo;
ebebea53
RH
5558 mov[0].dst_type = TCG_TYPE_REG;
5559 mov[0].src_type = TCG_TYPE_REG;
5560 mov[0].src_ext = reg_mo;
8429a1ca
RH
5561
5562 mov[1].dst = loc[!HOST_BIG_ENDIAN].arg_slot;
5563 mov[1].src = hi;
ebebea53
RH
5564 mov[1].dst_type = TCG_TYPE_REG;
5565 mov[1].src_type = TCG_TYPE_REG;
5566 mov[1].src_ext = reg_mo;
8429a1ca
RH
5567
5568 return 2;
5569}
5570
5571static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst,
5572 const TCGLdstHelperParam *parm)
5573{
5574 const TCGHelperInfo *info;
5575 const TCGCallArgumentLoc *loc;
5576 TCGMovExtend mov[2];
5577 unsigned next_arg, nmov;
5578 MemOp mop = get_memop(ldst->oi);
5579
5580 switch (mop & MO_SIZE) {
5581 case MO_8:
5582 case MO_16:
5583 case MO_32:
5584 info = &info_helper_ld32_mmu;
5585 break;
5586 case MO_64:
5587 info = &info_helper_ld64_mmu;
5588 break;
ebebea53
RH
5589 case MO_128:
5590 info = &info_helper_ld128_mmu;
5591 break;
8429a1ca
RH
5592 default:
5593 g_assert_not_reached();
5594 }
5595
5596 /* Defer env argument. */
5597 next_arg = 1;
5598
5599 loc = &info->in[next_arg];
24e46e6c
RH
5600 if (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 64) {
5601 nmov = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, TCG_TYPE_TL,
5602 ldst->addrlo_reg, ldst->addrhi_reg);
5603 tcg_out_helper_load_slots(s, nmov, mov, parm);
5604 next_arg += nmov;
5605 } else {
5606 /*
5607 * 32-bit host with 32-bit guest: zero-extend the guest address
5608 * to 64-bits for the helper by storing the low part, then
5609 * load a zero for the high part.
5610 */
5611 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN,
5612 TCG_TYPE_I32, TCG_TYPE_I32,
5613 ldst->addrlo_reg, -1);
5614 tcg_out_helper_load_slots(s, 1, mov, parm);
8429a1ca 5615
24e46e6c
RH
5616 tcg_out_helper_load_imm(s, loc[!HOST_BIG_ENDIAN].arg_slot,
5617 TCG_TYPE_I32, 0, parm);
5618 next_arg += 2;
5619 }
8429a1ca 5620
ebebea53
RH
5621 switch (info->out_kind) {
5622 case TCG_CALL_RET_NORMAL:
5623 case TCG_CALL_RET_BY_VEC:
5624 break;
5625 case TCG_CALL_RET_BY_REF:
5626 /*
5627 * The return reference is in the first argument slot.
5628 * We need memory in which to return: re-use the top of stack.
5629 */
5630 {
5631 int ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET;
5632
5633 if (arg_slot_reg_p(0)) {
5634 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[0],
5635 TCG_REG_CALL_STACK, ofs_slot0);
5636 } else {
5637 tcg_debug_assert(parm->ntmp != 0);
5638 tcg_out_addi_ptr(s, parm->tmp[0],
5639 TCG_REG_CALL_STACK, ofs_slot0);
5640 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0],
5641 TCG_REG_CALL_STACK, ofs_slot0);
5642 }
5643 }
5644 break;
5645 default:
5646 g_assert_not_reached();
5647 }
8429a1ca
RH
5648
5649 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg);
5650}
5651
5652static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *ldst,
5653 bool load_sign,
5654 const TCGLdstHelperParam *parm)
5655{
ebebea53 5656 MemOp mop = get_memop(ldst->oi);
8429a1ca 5657 TCGMovExtend mov[2];
ebebea53 5658 int ofs_slot0;
8429a1ca 5659
ebebea53
RH
5660 switch (ldst->type) {
5661 case TCG_TYPE_I64:
5662 if (TCG_TARGET_REG_BITS == 32) {
5663 break;
5664 }
5665 /* fall through */
8429a1ca 5666
ebebea53 5667 case TCG_TYPE_I32:
8429a1ca
RH
5668 mov[0].dst = ldst->datalo_reg;
5669 mov[0].src = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, 0);
5670 mov[0].dst_type = ldst->type;
5671 mov[0].src_type = TCG_TYPE_REG;
5672
5673 /*
5674 * If load_sign, then we allowed the helper to perform the
5675 * appropriate sign extension to tcg_target_ulong, and all
5676 * we need now is a plain move.
5677 *
5678 * If they do not, then we expect the relevant extension
5679 * instruction to be no more expensive than a move, and
5680 * we thus save the icache etc by only using one of two
5681 * helper functions.
5682 */
5683 if (load_sign || !(mop & MO_SIGN)) {
5684 if (TCG_TARGET_REG_BITS == 32 || ldst->type == TCG_TYPE_I32) {
5685 mov[0].src_ext = MO_32;
5686 } else {
5687 mov[0].src_ext = MO_64;
5688 }
5689 } else {
5690 mov[0].src_ext = mop & MO_SSIZE;
5691 }
5692 tcg_out_movext1(s, mov);
ebebea53 5693 return;
8429a1ca 5694
ebebea53
RH
5695 case TCG_TYPE_I128:
5696 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
5697 ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET;
5698 switch (TCG_TARGET_CALL_RET_I128) {
5699 case TCG_CALL_RET_NORMAL:
5700 break;
5701 case TCG_CALL_RET_BY_VEC:
5702 tcg_out_st(s, TCG_TYPE_V128,
5703 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
5704 TCG_REG_CALL_STACK, ofs_slot0);
5705 /* fall through */
5706 case TCG_CALL_RET_BY_REF:
5707 tcg_out_ld(s, TCG_TYPE_I64, ldst->datalo_reg,
5708 TCG_REG_CALL_STACK, ofs_slot0 + 8 * HOST_BIG_ENDIAN);
5709 tcg_out_ld(s, TCG_TYPE_I64, ldst->datahi_reg,
5710 TCG_REG_CALL_STACK, ofs_slot0 + 8 * !HOST_BIG_ENDIAN);
5711 return;
5712 default:
5713 g_assert_not_reached();
5714 }
5715 break;
8429a1ca 5716
ebebea53
RH
5717 default:
5718 g_assert_not_reached();
8429a1ca 5719 }
ebebea53
RH
5720
5721 mov[0].dst = ldst->datalo_reg;
5722 mov[0].src =
5723 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, HOST_BIG_ENDIAN);
5724 mov[0].dst_type = TCG_TYPE_I32;
5725 mov[0].src_type = TCG_TYPE_I32;
5726 mov[0].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64;
5727
5728 mov[1].dst = ldst->datahi_reg;
5729 mov[1].src =
5730 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, !HOST_BIG_ENDIAN);
5731 mov[1].dst_type = TCG_TYPE_REG;
5732 mov[1].src_type = TCG_TYPE_REG;
5733 mov[1].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64;
5734
5735 tcg_out_movext2(s, mov, mov + 1, parm->ntmp ? parm->tmp[0] : -1);
8429a1ca
RH
5736}
5737
5738static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst,
5739 const TCGLdstHelperParam *parm)
5740{
5741 const TCGHelperInfo *info;
5742 const TCGCallArgumentLoc *loc;
5743 TCGMovExtend mov[4];
5744 TCGType data_type;
5745 unsigned next_arg, nmov, n;
5746 MemOp mop = get_memop(ldst->oi);
5747
5748 switch (mop & MO_SIZE) {
5749 case MO_8:
5750 case MO_16:
5751 case MO_32:
5752 info = &info_helper_st32_mmu;
5753 data_type = TCG_TYPE_I32;
5754 break;
5755 case MO_64:
5756 info = &info_helper_st64_mmu;
5757 data_type = TCG_TYPE_I64;
5758 break;
ebebea53
RH
5759 case MO_128:
5760 info = &info_helper_st128_mmu;
5761 data_type = TCG_TYPE_I128;
5762 break;
8429a1ca
RH
5763 default:
5764 g_assert_not_reached();
5765 }
5766
5767 /* Defer env argument. */
5768 next_arg = 1;
5769 nmov = 0;
5770
5771 /* Handle addr argument. */
5772 loc = &info->in[next_arg];
24e46e6c
RH
5773 if (TCG_TARGET_REG_BITS == 64 || TARGET_LONG_BITS == 64) {
5774 n = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, TCG_TYPE_TL,
5775 ldst->addrlo_reg, ldst->addrhi_reg);
5776 next_arg += n;
5777 nmov += n;
5778 } else {
5779 /*
5780 * 32-bit host with 32-bit guest: zero-extend the guest address
5781 * to 64-bits for the helper by storing the low part. Later,
5782 * after we have processed the register inputs, we will load a
5783 * zero for the high part.
5784 */
5785 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN,
5786 TCG_TYPE_I32, TCG_TYPE_I32,
5787 ldst->addrlo_reg, -1);
5788 next_arg += 2;
5789 nmov += 1;
5790 }
8429a1ca
RH
5791
5792 /* Handle data argument. */
5793 loc = &info->in[next_arg];
ebebea53
RH
5794 switch (loc->kind) {
5795 case TCG_CALL_ARG_NORMAL:
5796 case TCG_CALL_ARG_EXTEND_U:
5797 case TCG_CALL_ARG_EXTEND_S:
5798 n = tcg_out_helper_add_mov(mov + nmov, loc, data_type, ldst->type,
5799 ldst->datalo_reg, ldst->datahi_reg);
5800 next_arg += n;
5801 nmov += n;
5802 tcg_out_helper_load_slots(s, nmov, mov, parm);
5803 break;
5804
5805 case TCG_CALL_ARG_BY_REF:
5806 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
5807 tcg_debug_assert(data_type == TCG_TYPE_I128);
5808 tcg_out_st(s, TCG_TYPE_I64,
5809 HOST_BIG_ENDIAN ? ldst->datahi_reg : ldst->datalo_reg,
5810 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[0].ref_slot));
5811 tcg_out_st(s, TCG_TYPE_I64,
5812 HOST_BIG_ENDIAN ? ldst->datalo_reg : ldst->datahi_reg,
5813 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[1].ref_slot));
5814
5815 tcg_out_helper_load_slots(s, nmov, mov, parm);
5816
5817 if (arg_slot_reg_p(loc->arg_slot)) {
5818 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[loc->arg_slot],
5819 TCG_REG_CALL_STACK,
5820 arg_slot_stk_ofs(loc->ref_slot));
5821 } else {
5822 tcg_debug_assert(parm->ntmp != 0);
5823 tcg_out_addi_ptr(s, parm->tmp[0], TCG_REG_CALL_STACK,
5824 arg_slot_stk_ofs(loc->ref_slot));
5825 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0],
5826 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc->arg_slot));
5827 }
5828 next_arg += 2;
5829 break;
5830
5831 default:
5832 g_assert_not_reached();
5833 }
8429a1ca 5834
24e46e6c
RH
5835 if (TCG_TARGET_REG_BITS == 32 && TARGET_LONG_BITS == 32) {
5836 loc = &info->in[1 + !HOST_BIG_ENDIAN];
5837 tcg_out_helper_load_imm(s, loc->arg_slot, TCG_TYPE_I32, 0, parm);
5838 }
5839
8429a1ca
RH
5840 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg);
5841}
5842
c896fe29
FB
5843#ifdef CONFIG_PROFILER
5844
c3fac113
EC
5845/* avoid copy/paste errors */
5846#define PROF_ADD(to, from, field) \
5847 do { \
d73415a3 5848 (to)->field += qatomic_read(&((from)->field)); \
c3fac113
EC
5849 } while (0)
5850
5851#define PROF_MAX(to, from, field) \
5852 do { \
d73415a3 5853 typeof((from)->field) val__ = qatomic_read(&((from)->field)); \
c3fac113
EC
5854 if (val__ > (to)->field) { \
5855 (to)->field = val__; \
5856 } \
5857 } while (0)
5858
5859/* Pass in a zero'ed @prof */
5860static inline
5861void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
5862{
0e2d61cf 5863 unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
c3fac113
EC
5864 unsigned int i;
5865
3468b59e 5866 for (i = 0; i < n_ctxs; i++) {
d73415a3 5867 TCGContext *s = qatomic_read(&tcg_ctxs[i]);
3468b59e 5868 const TCGProfile *orig = &s->prof;
c3fac113
EC
5869
5870 if (counters) {
72fd2efb 5871 PROF_ADD(prof, orig, cpu_exec_time);
c3fac113
EC
5872 PROF_ADD(prof, orig, tb_count1);
5873 PROF_ADD(prof, orig, tb_count);
5874 PROF_ADD(prof, orig, op_count);
5875 PROF_MAX(prof, orig, op_count_max);
5876 PROF_ADD(prof, orig, temp_count);
5877 PROF_MAX(prof, orig, temp_count_max);
5878 PROF_ADD(prof, orig, del_op_count);
5879 PROF_ADD(prof, orig, code_in_len);
5880 PROF_ADD(prof, orig, code_out_len);
5881 PROF_ADD(prof, orig, search_out_len);
5882 PROF_ADD(prof, orig, interm_time);
5883 PROF_ADD(prof, orig, code_time);
5884 PROF_ADD(prof, orig, la_time);
5885 PROF_ADD(prof, orig, opt_time);
5886 PROF_ADD(prof, orig, restore_count);
5887 PROF_ADD(prof, orig, restore_time);
5888 }
5889 if (table) {
5890 int i;
5891
5892 for (i = 0; i < NB_OPS; i++) {
5893 PROF_ADD(prof, orig, table_op_count[i]);
5894 }
5895 }
5896 }
5897}
5898
5899#undef PROF_ADD
5900#undef PROF_MAX
5901
5902static void tcg_profile_snapshot_counters(TCGProfile *prof)
5903{
5904 tcg_profile_snapshot(prof, true, false);
5905}
5906
5907static void tcg_profile_snapshot_table(TCGProfile *prof)
5908{
5909 tcg_profile_snapshot(prof, false, true);
5910}
c896fe29 5911
b6a7f3e0 5912void tcg_dump_op_count(GString *buf)
c896fe29 5913{
c3fac113 5914 TCGProfile prof = {};
c896fe29 5915 int i;
d70724ce 5916
c3fac113 5917 tcg_profile_snapshot_table(&prof);
15fc7daa 5918 for (i = 0; i < NB_OPS; i++) {
b6a7f3e0
DB
5919 g_string_append_printf(buf, "%s %" PRId64 "\n", tcg_op_defs[i].name,
5920 prof.table_op_count[i]);
c896fe29 5921 }
c896fe29 5922}
72fd2efb
EC
5923
5924int64_t tcg_cpu_exec_time(void)
5925{
0e2d61cf 5926 unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
72fd2efb
EC
5927 unsigned int i;
5928 int64_t ret = 0;
5929
5930 for (i = 0; i < n_ctxs; i++) {
d73415a3 5931 const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
72fd2efb
EC
5932 const TCGProfile *prof = &s->prof;
5933
d73415a3 5934 ret += qatomic_read(&prof->cpu_exec_time);
72fd2efb
EC
5935 }
5936 return ret;
5937}
246ae24d 5938#else
b6a7f3e0 5939void tcg_dump_op_count(GString *buf)
246ae24d 5940{
b6a7f3e0 5941 g_string_append_printf(buf, "[TCG profiler not compiled]\n");
246ae24d 5942}
72fd2efb
EC
5943
5944int64_t tcg_cpu_exec_time(void)
5945{
5946 error_report("%s: TCG profiler not compiled", __func__);
5947 exit(EXIT_FAILURE);
5948}
c896fe29
FB
5949#endif
5950
5951
fbf59aad 5952int tcg_gen_code(TCGContext *s, TranslationBlock *tb, target_ulong pc_start)
c896fe29 5953{
c3fac113
EC
5954#ifdef CONFIG_PROFILER
5955 TCGProfile *prof = &s->prof;
5956#endif
15fa08f8
RH
5957 int i, num_insns;
5958 TCGOp *op;
c896fe29 5959
04fe6400
RH
5960#ifdef CONFIG_PROFILER
5961 {
c1f543b7 5962 int n = 0;
04fe6400 5963
15fa08f8
RH
5964 QTAILQ_FOREACH(op, &s->ops, link) {
5965 n++;
5966 }
d73415a3 5967 qatomic_set(&prof->op_count, prof->op_count + n);
c3fac113 5968 if (n > prof->op_count_max) {
d73415a3 5969 qatomic_set(&prof->op_count_max, n);
04fe6400
RH
5970 }
5971
5972 n = s->nb_temps;
d73415a3 5973 qatomic_set(&prof->temp_count, prof->temp_count + n);
c3fac113 5974 if (n > prof->temp_count_max) {
d73415a3 5975 qatomic_set(&prof->temp_count_max, n);
04fe6400
RH
5976 }
5977 }
5978#endif
5979
c896fe29 5980#ifdef DEBUG_DISAS
d977e1c2 5981 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
fbf59aad 5982 && qemu_log_in_addr_range(pc_start))) {
c60f599b 5983 FILE *logfile = qemu_log_trylock();
78b54858
RH
5984 if (logfile) {
5985 fprintf(logfile, "OP:\n");
b7a83ff8 5986 tcg_dump_ops(s, logfile, false);
78b54858
RH
5987 fprintf(logfile, "\n");
5988 qemu_log_unlock(logfile);
5989 }
c896fe29
FB
5990 }
5991#endif
5992
bef16ab4
RH
5993#ifdef CONFIG_DEBUG_TCG
5994 /* Ensure all labels referenced have been emitted. */
5995 {
5996 TCGLabel *l;
5997 bool error = false;
5998
5999 QSIMPLEQ_FOREACH(l, &s->labels, next) {
f85b1fc4 6000 if (unlikely(!l->present) && !QSIMPLEQ_EMPTY(&l->branches)) {
bef16ab4
RH
6001 qemu_log_mask(CPU_LOG_TB_OP,
6002 "$L%d referenced but not present.\n", l->id);
6003 error = true;
6004 }
6005 }
6006 assert(!error);
6007 }
6008#endif
6009
c5cc28ff 6010#ifdef CONFIG_PROFILER
d73415a3 6011 qatomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
c5cc28ff
AJ
6012#endif
6013
8f2e8c07 6014#ifdef USE_TCG_OPTIMIZATIONS
c45cb8bb 6015 tcg_optimize(s);
8f2e8c07
KB
6016#endif
6017
a23a9ec6 6018#ifdef CONFIG_PROFILER
d73415a3
SH
6019 qatomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
6020 qatomic_set(&prof->la_time, prof->la_time - profile_getclock());
a23a9ec6 6021#endif
c5cc28ff 6022
b4fc67c7 6023 reachable_code_pass(s);
874b8574 6024 liveness_pass_0(s);
b83eabea 6025 liveness_pass_1(s);
5a18407f 6026
b83eabea 6027 if (s->nb_indirects > 0) {
5a18407f 6028#ifdef DEBUG_DISAS
b83eabea 6029 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
fbf59aad 6030 && qemu_log_in_addr_range(pc_start))) {
c60f599b 6031 FILE *logfile = qemu_log_trylock();
78b54858
RH
6032 if (logfile) {
6033 fprintf(logfile, "OP before indirect lowering:\n");
b7a83ff8 6034 tcg_dump_ops(s, logfile, false);
78b54858
RH
6035 fprintf(logfile, "\n");
6036 qemu_log_unlock(logfile);
6037 }
b83eabea 6038 }
5a18407f 6039#endif
b83eabea
RH
6040 /* Replace indirect temps with direct temps. */
6041 if (liveness_pass_2(s)) {
6042 /* If changes were made, re-run liveness. */
6043 liveness_pass_1(s);
5a18407f
RH
6044 }
6045 }
c5cc28ff 6046
a23a9ec6 6047#ifdef CONFIG_PROFILER
d73415a3 6048 qatomic_set(&prof->la_time, prof->la_time + profile_getclock());
a23a9ec6 6049#endif
c896fe29
FB
6050
6051#ifdef DEBUG_DISAS
d977e1c2 6052 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
fbf59aad 6053 && qemu_log_in_addr_range(pc_start))) {
c60f599b 6054 FILE *logfile = qemu_log_trylock();
78b54858
RH
6055 if (logfile) {
6056 fprintf(logfile, "OP after optimization and liveness analysis:\n");
b7a83ff8 6057 tcg_dump_ops(s, logfile, true);
78b54858
RH
6058 fprintf(logfile, "\n");
6059 qemu_log_unlock(logfile);
6060 }
c896fe29
FB
6061 }
6062#endif
6063
35abb009 6064 /* Initialize goto_tb jump offsets. */
3a50f424
RH
6065 tb->jmp_reset_offset[0] = TB_JMP_OFFSET_INVALID;
6066 tb->jmp_reset_offset[1] = TB_JMP_OFFSET_INVALID;
9da6079b
RH
6067 tb->jmp_insn_offset[0] = TB_JMP_OFFSET_INVALID;
6068 tb->jmp_insn_offset[1] = TB_JMP_OFFSET_INVALID;
35abb009 6069
c896fe29
FB
6070 tcg_reg_alloc_start(s);
6071
db0c51a3
RH
6072 /*
6073 * Reset the buffer pointers when restarting after overflow.
6074 * TODO: Move this into translate-all.c with the rest of the
6075 * buffer management. Having only this done here is confusing.
6076 */
6077 s->code_buf = tcg_splitwx_to_rw(tb->tc.ptr);
6078 s->code_ptr = s->code_buf;
c896fe29 6079
659ef5cb 6080#ifdef TCG_TARGET_NEED_LDST_LABELS
6001f772 6081 QSIMPLEQ_INIT(&s->ldst_labels);
659ef5cb 6082#endif
57a26946
RH
6083#ifdef TCG_TARGET_NEED_POOL_LABELS
6084 s->pool_labels = NULL;
6085#endif
9ecefc84 6086
fca8a500 6087 num_insns = -1;
15fa08f8 6088 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb 6089 TCGOpcode opc = op->opc;
b3db8758 6090
c896fe29 6091#ifdef CONFIG_PROFILER
d73415a3 6092 qatomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
c896fe29 6093#endif
c45cb8bb
RH
6094
6095 switch (opc) {
c896fe29 6096 case INDEX_op_mov_i32:
c896fe29 6097 case INDEX_op_mov_i64:
d2fd745f 6098 case INDEX_op_mov_vec:
dd186292 6099 tcg_reg_alloc_mov(s, op);
c896fe29 6100 break;
bab1671f
RH
6101 case INDEX_op_dup_vec:
6102 tcg_reg_alloc_dup(s, op);
6103 break;
765b842a 6104 case INDEX_op_insn_start:
fca8a500 6105 if (num_insns >= 0) {
9f754620
RH
6106 size_t off = tcg_current_code_size(s);
6107 s->gen_insn_end_off[num_insns] = off;
6108 /* Assert that we do not overflow our stored offset. */
6109 assert(s->gen_insn_end_off[num_insns] == off);
fca8a500
RH
6110 }
6111 num_insns++;
bad729e2 6112 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
c9ad8d27
RH
6113 s->gen_insn_data[num_insns][i] =
6114 tcg_get_insn_start_param(op, i);
bad729e2 6115 }
c896fe29 6116 break;
5ff9d6a4 6117 case INDEX_op_discard:
43439139 6118 temp_dead(s, arg_temp(op->args[0]));
5ff9d6a4 6119 break;
c896fe29 6120 case INDEX_op_set_label:
e8996ee0 6121 tcg_reg_alloc_bb_end(s, s->reserved_regs);
92ab8e7d 6122 tcg_out_label(s, arg_label(op->args[0]));
c896fe29
FB
6123 break;
6124 case INDEX_op_call:
dd186292 6125 tcg_reg_alloc_call(s, op);
c45cb8bb 6126 break;
b55a8d9d
RH
6127 case INDEX_op_exit_tb:
6128 tcg_out_exit_tb(s, op->args[0]);
6129 break;
cf7d6b8e
RH
6130 case INDEX_op_goto_tb:
6131 tcg_out_goto_tb(s, op->args[0]);
6132 break;
efe86b21
RH
6133 case INDEX_op_dup2_vec:
6134 if (tcg_reg_alloc_dup2(s, op)) {
6135 break;
6136 }
6137 /* fall through */
c896fe29 6138 default:
25c4d9cc 6139 /* Sanity check that we've not introduced any unhandled opcodes. */
be0f34b5 6140 tcg_debug_assert(tcg_op_supported(opc));
c896fe29
FB
6141 /* Note: in order to speed up the code, it would be much
6142 faster to have specialized register allocator functions for
6143 some common argument patterns */
dd186292 6144 tcg_reg_alloc_op(s, op);
c896fe29
FB
6145 break;
6146 }
b125f9dc
RH
6147 /* Test for (pending) buffer overflow. The assumption is that any
6148 one operation beginning below the high water mark cannot overrun
6149 the buffer completely. Thus we can test for overflow after
6150 generating code without having to check during generation. */
644da9b3 6151 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
b125f9dc
RH
6152 return -1;
6153 }
6e6c4efe
RH
6154 /* Test for TB overflow, as seen by gen_insn_end_off. */
6155 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
6156 return -2;
6157 }
c896fe29 6158 }
fca8a500
RH
6159 tcg_debug_assert(num_insns >= 0);
6160 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
c45cb8bb 6161
b76f0d8c 6162 /* Generate TB finalization at the end of block */
659ef5cb 6163#ifdef TCG_TARGET_NEED_LDST_LABELS
aeee05f5
RH
6164 i = tcg_out_ldst_finalize(s);
6165 if (i < 0) {
6166 return i;
23dceda6 6167 }
659ef5cb 6168#endif
57a26946 6169#ifdef TCG_TARGET_NEED_POOL_LABELS
1768987b
RH
6170 i = tcg_out_pool_finalize(s);
6171 if (i < 0) {
6172 return i;
57a26946
RH
6173 }
6174#endif
7ecd02a0
RH
6175 if (!tcg_resolve_relocs(s)) {
6176 return -2;
6177 }
c896fe29 6178
df5d2b16 6179#ifndef CONFIG_TCG_INTERPRETER
c896fe29 6180 /* flush instruction cache */
db0c51a3
RH
6181 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
6182 (uintptr_t)s->code_buf,
1da8de39 6183 tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
df5d2b16 6184#endif
2aeabc08 6185
1813e175 6186 return tcg_current_code_size(s);
c896fe29
FB
6187}
6188
a23a9ec6 6189#ifdef CONFIG_PROFILER
3a841ab5 6190void tcg_dump_info(GString *buf)
a23a9ec6 6191{
c3fac113
EC
6192 TCGProfile prof = {};
6193 const TCGProfile *s;
6194 int64_t tb_count;
6195 int64_t tb_div_count;
6196 int64_t tot;
6197
6198 tcg_profile_snapshot_counters(&prof);
6199 s = &prof;
6200 tb_count = s->tb_count;
6201 tb_div_count = tb_count ? tb_count : 1;
6202 tot = s->interm_time + s->code_time;
a23a9ec6 6203
3a841ab5
DB
6204 g_string_append_printf(buf, "JIT cycles %" PRId64
6205 " (%0.3f s at 2.4 GHz)\n",
6206 tot, tot / 2.4e9);
6207 g_string_append_printf(buf, "translated TBs %" PRId64
6208 " (aborted=%" PRId64 " %0.1f%%)\n",
6209 tb_count, s->tb_count1 - tb_count,
6210 (double)(s->tb_count1 - s->tb_count)
6211 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
6212 g_string_append_printf(buf, "avg ops/TB %0.1f max=%d\n",
6213 (double)s->op_count / tb_div_count, s->op_count_max);
6214 g_string_append_printf(buf, "deleted ops/TB %0.2f\n",
6215 (double)s->del_op_count / tb_div_count);
6216 g_string_append_printf(buf, "avg temps/TB %0.2f max=%d\n",
6217 (double)s->temp_count / tb_div_count,
6218 s->temp_count_max);
6219 g_string_append_printf(buf, "avg host code/TB %0.1f\n",
6220 (double)s->code_out_len / tb_div_count);
6221 g_string_append_printf(buf, "avg search data/TB %0.1f\n",
6222 (double)s->search_out_len / tb_div_count);
a813e36f 6223
3a841ab5
DB
6224 g_string_append_printf(buf, "cycles/op %0.1f\n",
6225 s->op_count ? (double)tot / s->op_count : 0);
6226 g_string_append_printf(buf, "cycles/in byte %0.1f\n",
6227 s->code_in_len ? (double)tot / s->code_in_len : 0);
6228 g_string_append_printf(buf, "cycles/out byte %0.1f\n",
6229 s->code_out_len ? (double)tot / s->code_out_len : 0);
6230 g_string_append_printf(buf, "cycles/search byte %0.1f\n",
6231 s->search_out_len ?
6232 (double)tot / s->search_out_len : 0);
fca8a500 6233 if (tot == 0) {
a23a9ec6 6234 tot = 1;
fca8a500 6235 }
3a841ab5
DB
6236 g_string_append_printf(buf, " gen_interm time %0.1f%%\n",
6237 (double)s->interm_time / tot * 100.0);
6238 g_string_append_printf(buf, " gen_code time %0.1f%%\n",
6239 (double)s->code_time / tot * 100.0);
6240 g_string_append_printf(buf, "optim./code time %0.1f%%\n",
6241 (double)s->opt_time / (s->code_time ?
6242 s->code_time : 1)
6243 * 100.0);
6244 g_string_append_printf(buf, "liveness/code time %0.1f%%\n",
6245 (double)s->la_time / (s->code_time ?
6246 s->code_time : 1) * 100.0);
6247 g_string_append_printf(buf, "cpu_restore count %" PRId64 "\n",
6248 s->restore_count);
6249 g_string_append_printf(buf, " avg cycles %0.1f\n",
6250 s->restore_count ?
6251 (double)s->restore_time / s->restore_count : 0);
a23a9ec6
FB
6252}
6253#else
3a841ab5 6254void tcg_dump_info(GString *buf)
a23a9ec6 6255{
3a841ab5 6256 g_string_append_printf(buf, "[TCG profiler not compiled]\n");
a23a9ec6
FB
6257}
6258#endif
813da627
RH
6259
6260#ifdef ELF_HOST_MACHINE
5872bbf2
RH
6261/* In order to use this feature, the backend needs to do three things:
6262
6263 (1) Define ELF_HOST_MACHINE to indicate both what value to
6264 put into the ELF image and to indicate support for the feature.
6265
6266 (2) Define tcg_register_jit. This should create a buffer containing
6267 the contents of a .debug_frame section that describes the post-
6268 prologue unwind info for the tcg machine.
6269
6270 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
6271*/
813da627
RH
6272
6273/* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
6274typedef enum {
6275 JIT_NOACTION = 0,
6276 JIT_REGISTER_FN,
6277 JIT_UNREGISTER_FN
6278} jit_actions_t;
6279
6280struct jit_code_entry {
6281 struct jit_code_entry *next_entry;
6282 struct jit_code_entry *prev_entry;
6283 const void *symfile_addr;
6284 uint64_t symfile_size;
6285};
6286
6287struct jit_descriptor {
6288 uint32_t version;
6289 uint32_t action_flag;
6290 struct jit_code_entry *relevant_entry;
6291 struct jit_code_entry *first_entry;
6292};
6293
6294void __jit_debug_register_code(void) __attribute__((noinline));
6295void __jit_debug_register_code(void)
6296{
6297 asm("");
6298}
6299
6300/* Must statically initialize the version, because GDB may check
6301 the version before we can set it. */
6302struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
6303
6304/* End GDB interface. */
6305
6306static int find_string(const char *strtab, const char *str)
6307{
6308 const char *p = strtab + 1;
6309
6310 while (1) {
6311 if (strcmp(p, str) == 0) {
6312 return p - strtab;
6313 }
6314 p += strlen(p) + 1;
6315 }
6316}
6317
755bf9e5 6318static void tcg_register_jit_int(const void *buf_ptr, size_t buf_size,
2c90784a
RH
6319 const void *debug_frame,
6320 size_t debug_frame_size)
813da627 6321{
5872bbf2
RH
6322 struct __attribute__((packed)) DebugInfo {
6323 uint32_t len;
6324 uint16_t version;
6325 uint32_t abbrev;
6326 uint8_t ptr_size;
6327 uint8_t cu_die;
6328 uint16_t cu_lang;
6329 uintptr_t cu_low_pc;
6330 uintptr_t cu_high_pc;
6331 uint8_t fn_die;
6332 char fn_name[16];
6333 uintptr_t fn_low_pc;
6334 uintptr_t fn_high_pc;
6335 uint8_t cu_eoc;
6336 };
813da627
RH
6337
6338 struct ElfImage {
6339 ElfW(Ehdr) ehdr;
6340 ElfW(Phdr) phdr;
5872bbf2
RH
6341 ElfW(Shdr) shdr[7];
6342 ElfW(Sym) sym[2];
6343 struct DebugInfo di;
6344 uint8_t da[24];
6345 char str[80];
6346 };
6347
6348 struct ElfImage *img;
6349
6350 static const struct ElfImage img_template = {
6351 .ehdr = {
6352 .e_ident[EI_MAG0] = ELFMAG0,
6353 .e_ident[EI_MAG1] = ELFMAG1,
6354 .e_ident[EI_MAG2] = ELFMAG2,
6355 .e_ident[EI_MAG3] = ELFMAG3,
6356 .e_ident[EI_CLASS] = ELF_CLASS,
6357 .e_ident[EI_DATA] = ELF_DATA,
6358 .e_ident[EI_VERSION] = EV_CURRENT,
6359 .e_type = ET_EXEC,
6360 .e_machine = ELF_HOST_MACHINE,
6361 .e_version = EV_CURRENT,
6362 .e_phoff = offsetof(struct ElfImage, phdr),
6363 .e_shoff = offsetof(struct ElfImage, shdr),
6364 .e_ehsize = sizeof(ElfW(Shdr)),
6365 .e_phentsize = sizeof(ElfW(Phdr)),
6366 .e_phnum = 1,
6367 .e_shentsize = sizeof(ElfW(Shdr)),
6368 .e_shnum = ARRAY_SIZE(img->shdr),
6369 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
abbb3eae
RH
6370#ifdef ELF_HOST_FLAGS
6371 .e_flags = ELF_HOST_FLAGS,
6372#endif
6373#ifdef ELF_OSABI
6374 .e_ident[EI_OSABI] = ELF_OSABI,
6375#endif
5872bbf2
RH
6376 },
6377 .phdr = {
6378 .p_type = PT_LOAD,
6379 .p_flags = PF_X,
6380 },
6381 .shdr = {
6382 [0] = { .sh_type = SHT_NULL },
6383 /* Trick: The contents of code_gen_buffer are not present in
6384 this fake ELF file; that got allocated elsewhere. Therefore
6385 we mark .text as SHT_NOBITS (similar to .bss) so that readers
6386 will not look for contents. We can record any address. */
6387 [1] = { /* .text */
6388 .sh_type = SHT_NOBITS,
6389 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
6390 },
6391 [2] = { /* .debug_info */
6392 .sh_type = SHT_PROGBITS,
6393 .sh_offset = offsetof(struct ElfImage, di),
6394 .sh_size = sizeof(struct DebugInfo),
6395 },
6396 [3] = { /* .debug_abbrev */
6397 .sh_type = SHT_PROGBITS,
6398 .sh_offset = offsetof(struct ElfImage, da),
6399 .sh_size = sizeof(img->da),
6400 },
6401 [4] = { /* .debug_frame */
6402 .sh_type = SHT_PROGBITS,
6403 .sh_offset = sizeof(struct ElfImage),
6404 },
6405 [5] = { /* .symtab */
6406 .sh_type = SHT_SYMTAB,
6407 .sh_offset = offsetof(struct ElfImage, sym),
6408 .sh_size = sizeof(img->sym),
6409 .sh_info = 1,
6410 .sh_link = ARRAY_SIZE(img->shdr) - 1,
6411 .sh_entsize = sizeof(ElfW(Sym)),
6412 },
6413 [6] = { /* .strtab */
6414 .sh_type = SHT_STRTAB,
6415 .sh_offset = offsetof(struct ElfImage, str),
6416 .sh_size = sizeof(img->str),
6417 }
6418 },
6419 .sym = {
6420 [1] = { /* code_gen_buffer */
6421 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
6422 .st_shndx = 1,
6423 }
6424 },
6425 .di = {
6426 .len = sizeof(struct DebugInfo) - 4,
6427 .version = 2,
6428 .ptr_size = sizeof(void *),
6429 .cu_die = 1,
6430 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
6431 .fn_die = 2,
6432 .fn_name = "code_gen_buffer"
6433 },
6434 .da = {
6435 1, /* abbrev number (the cu) */
6436 0x11, 1, /* DW_TAG_compile_unit, has children */
6437 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
6438 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
6439 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
6440 0, 0, /* end of abbrev */
6441 2, /* abbrev number (the fn) */
6442 0x2e, 0, /* DW_TAG_subprogram, no children */
6443 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
6444 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
6445 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
6446 0, 0, /* end of abbrev */
6447 0 /* no more abbrev */
6448 },
6449 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
6450 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
813da627
RH
6451 };
6452
6453 /* We only need a single jit entry; statically allocate it. */
6454 static struct jit_code_entry one_entry;
6455
5872bbf2 6456 uintptr_t buf = (uintptr_t)buf_ptr;
813da627 6457 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
2c90784a 6458 DebugFrameHeader *dfh;
813da627 6459
5872bbf2
RH
6460 img = g_malloc(img_size);
6461 *img = img_template;
813da627 6462
5872bbf2
RH
6463 img->phdr.p_vaddr = buf;
6464 img->phdr.p_paddr = buf;
6465 img->phdr.p_memsz = buf_size;
813da627 6466
813da627 6467 img->shdr[1].sh_name = find_string(img->str, ".text");
5872bbf2 6468 img->shdr[1].sh_addr = buf;
813da627
RH
6469 img->shdr[1].sh_size = buf_size;
6470
5872bbf2
RH
6471 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
6472 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
6473
6474 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
6475 img->shdr[4].sh_size = debug_frame_size;
6476
6477 img->shdr[5].sh_name = find_string(img->str, ".symtab");
6478 img->shdr[6].sh_name = find_string(img->str, ".strtab");
6479
6480 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
6481 img->sym[1].st_value = buf;
6482 img->sym[1].st_size = buf_size;
813da627 6483
5872bbf2 6484 img->di.cu_low_pc = buf;
45aba097 6485 img->di.cu_high_pc = buf + buf_size;
5872bbf2 6486 img->di.fn_low_pc = buf;
45aba097 6487 img->di.fn_high_pc = buf + buf_size;
813da627 6488
2c90784a
RH
6489 dfh = (DebugFrameHeader *)(img + 1);
6490 memcpy(dfh, debug_frame, debug_frame_size);
6491 dfh->fde.func_start = buf;
6492 dfh->fde.func_len = buf_size;
6493
813da627
RH
6494#ifdef DEBUG_JIT
6495 /* Enable this block to be able to debug the ELF image file creation.
6496 One can use readelf, objdump, or other inspection utilities. */
6497 {
eb6b2edf
BM
6498 g_autofree char *jit = g_strdup_printf("%s/qemu.jit", g_get_tmp_dir());
6499 FILE *f = fopen(jit, "w+b");
813da627 6500 if (f) {
5872bbf2 6501 if (fwrite(img, img_size, 1, f) != img_size) {
813da627
RH
6502 /* Avoid stupid unused return value warning for fwrite. */
6503 }
6504 fclose(f);
6505 }
6506 }
6507#endif
6508
6509 one_entry.symfile_addr = img;
6510 one_entry.symfile_size = img_size;
6511
6512 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
6513 __jit_debug_descriptor.relevant_entry = &one_entry;
6514 __jit_debug_descriptor.first_entry = &one_entry;
6515 __jit_debug_register_code();
6516}
6517#else
5872bbf2
RH
6518/* No support for the feature. Provide the entry point expected by exec.c,
6519 and implement the internal function we declared earlier. */
813da627 6520
755bf9e5 6521static void tcg_register_jit_int(const void *buf, size_t size,
2c90784a
RH
6522 const void *debug_frame,
6523 size_t debug_frame_size)
813da627
RH
6524{
6525}
6526
755bf9e5 6527void tcg_register_jit(const void *buf, size_t buf_size)
813da627
RH
6528{
6529}
6530#endif /* ELF_HOST_MACHINE */
db432672
RH
6531
6532#if !TCG_TARGET_MAYBE_vec
6533void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
6534{
6535 g_assert_not_reached();
6536}
6537#endif