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