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