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