blob: 34e3056380d81a164786225759a4f264e2a9a081 [file] [log] [blame]
bellardc896fe22008-02-01 10:05:41 +00001/*
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
Peter Maydell757e7252016-01-26 18:17:08 +000025#include "qemu/osdep.h"
aurel32cca82982009-04-16 09:58:30 +000026
Richard Henderson813da622012-03-19 12:25:11 -070027/* Define to jump the ELF file used to communicate with GDB. */
28#undef DEBUG_JIT
29
Emilio G. Cota72fd2ef2018-10-10 10:48:53 -040030#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/host-utils.h"
Markus Armbrusterd4c51a02019-04-17 21:17:51 +020033#include "qemu/qemu-print.h"
Richard Henderson084cfca2020-12-14 08:02:33 -060034#include "qemu/cacheflush.h"
Peter Maydellad768e62022-02-08 20:08:55 +000035#include "qemu/cacheinfo.h"
Richard W.M. Jones533206f2023-03-03 08:49:48 +000036#include "qemu/timer.h"
Richard Hendersoncac9b0f2023-04-01 21:22:06 -070037#include "exec/translation-block.h"
Richard Hendersond0a9bb52023-03-27 16:07:15 -070038#include "exec/tlb-common.h"
Richard Hendersond7ec12f2023-09-29 19:54:54 -070039#include "tcg/startup.h"
Richard Hendersonad3d0e42023-03-28 18:17:24 -070040#include "tcg/tcg-op-common.h"
Richard Henderson813da622012-03-19 12:25:11 -070041
Richard Hendersonedee2572013-08-20 17:20:30 -070042#if UINTPTR_MAX == UINT32_MAX
Richard Henderson813da622012-03-19 12:25:11 -070043# define ELF_CLASS ELFCLASS32
Richard Hendersonedee2572013-08-20 17:20:30 -070044#else
45# define ELF_CLASS ELFCLASS64
Richard Henderson813da622012-03-19 12:25:11 -070046#endif
Marc-André Lureaue03b5682022-03-23 19:57:17 +040047#if HOST_BIG_ENDIAN
Richard Henderson813da622012-03-19 12:25:11 -070048# define ELF_DATA ELFDATA2MSB
49#else
50# define ELF_DATA ELFDATA2LSB
51#endif
52
bellardc896fe22008-02-01 10:05:41 +000053#include "elf.h"
Paolo Bonzini508127e2016-01-07 16:55:28 +030054#include "exec/log.h"
Richard Hendersond2ba8022021-07-27 11:10:22 -100055#include "tcg/tcg-ldst.h"
Richard Henderson47f73132023-02-24 22:45:43 -100056#include "tcg/tcg-temp-internal.h"
Richard Henderson5ff72582021-03-09 16:24:14 -060057#include "tcg-internal.h"
Ilya Leoshkevich327b75a2024-01-25 06:46:30 +010058#include "tcg/perf.h"
Richard Henderson7d478302023-04-30 08:24:36 +010059#ifdef CONFIG_USER_ONLY
Philippe Mathieu-Daudéd3cbde72024-03-26 12:27:29 +010060#include "user/guest-base.h"
Richard Henderson7d478302023-04-30 08:24:36 +010061#endif
bellardc896fe22008-02-01 10:05:41 +000062
Paolo Bonzini139c1832020-02-04 12:41:01 +010063/* Forward declarations for functions declared in tcg-target.c.inc and
Peter Maydellce151102016-02-23 14:49:41 +000064 used here. */
Richard Hendersone4d58b42010-06-02 17:26:56 -070065static void tcg_target_init(TCGContext *s);
66static void tcg_target_qemu_prologue(TCGContext *s);
Richard Henderson6ac17782018-11-30 11:52:48 -080067static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
Richard Henderson2ba7fae22013-08-20 15:30:10 -070068 intptr_t value, intptr_t addend);
bellardc896fe22008-02-01 10:05:41 +000069
Richard Henderson497a22e2013-06-05 07:39:57 -070070/* 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;
Richard Hendersonedee2572013-08-20 17:20:30 -070084 uintptr_t func_start;
85 uintptr_t func_len;
Richard Henderson497a22e2013-06-05 07:39:57 -070086} DebugFrameFDEHeader;
87
Richard Henderson2c907842014-05-15 12:48:01 -070088typedef struct QEMU_PACKED {
89 DebugFrameCIE cie;
90 DebugFrameFDEHeader fde;
91} DebugFrameHeader;
92
Richard Henderson2528f772023-04-07 18:18:03 -050093typedef 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
Richard Henderson755bf9e2020-10-29 09:17:30 -0700106static void tcg_register_jit_int(const void *buf, size_t size,
Richard Henderson2c907842014-05-15 12:48:01 -0700107 const void *debug_frame,
108 size_t debug_frame_size)
Richard Henderson813da622012-03-19 12:25:11 -0700109 __attribute__((unused));
110
Paolo Bonzini139c1832020-02-04 12:41:01 +0100111/* Forward declarations for functions declared and used in tcg-target.c.inc. */
Richard Henderson9358fbb2023-08-15 16:34:59 +0000112static void tcg_out_tb_start(TCGContext *s);
Richard Henderson2a534af2011-11-09 08:03:34 +0000113static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
Richard Hendersona05b5b92013-08-20 17:07:26 -0700114 intptr_t arg2);
Richard Henderson78113e82019-03-16 17:48:18 +0000115static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
Stefan Weilc0ad3002011-09-17 22:00:29 +0200116static void tcg_out_movi(TCGContext *s, TCGType type,
Richard Henderson2a534af2011-11-09 08:03:34 +0000117 TCGReg ret, tcg_target_long arg);
Richard Henderson678155b2023-04-05 11:17:01 -0700118static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
Richard Henderson753e42e2023-04-05 14:49:59 -0700119static void tcg_out_ext16s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
Richard Hendersond0e66c82023-04-05 13:26:51 -0700120static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Henderson379afdf2023-04-05 16:25:22 -0700121static void tcg_out_ext16u(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Henderson52bf3392023-04-05 17:50:09 -0700122static void tcg_out_ext32s(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Henderson9ecf5f62023-04-05 18:07:05 -0700123static void tcg_out_ext32u(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Henderson9c6aa272023-04-05 18:30:56 -0700124static void tcg_out_exts_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Hendersonb9bfe002023-04-05 18:56:28 -0700125static void tcg_out_extu_i32_i64(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Hendersonb8b94ac2023-04-05 19:58:35 -0700126static void tcg_out_extrl_i64_i32(TCGContext *s, TCGReg ret, TCGReg arg);
Richard Henderson313bdea2022-10-31 09:22:59 +1100127static void tcg_out_addi_ptr(TCGContext *s, TCGReg, TCGReg, tcg_target_long);
Richard Henderson129f1f92023-04-05 22:27:03 -0700128static bool tcg_out_xchg(TCGContext *s, TCGType type, TCGReg r1, TCGReg r2);
Richard Hendersonb55a8d92022-11-26 12:42:06 -0800129static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg);
Richard Hendersoncf7d6b82022-11-26 17:14:05 -0800130static void tcg_out_goto_tb(TCGContext *s, int which);
Miroslav Rezanina5e8892d2021-03-12 13:14:18 +0100131static 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]);
Richard Hendersond2fd7452017-09-14 13:53:46 -0700134#if TCG_TARGET_MAYBE_vec
Richard Hendersone7632cf2019-03-18 15:32:44 +0000135static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
136 TCGReg dst, TCGReg src);
Richard Hendersond6ecb4a2019-03-18 12:00:39 -0700137static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
138 TCGReg dst, TCGReg base, intptr_t offset);
Richard Henderson4e186172020-03-31 01:02:08 -0700139static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
140 TCGReg dst, int64_t arg);
Miroslav Rezanina5e8892d2021-03-12 13:14:18 +0100141static 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]);
Richard Hendersond2fd7452017-09-14 13:53:46 -0700145#else
Richard Hendersone7632cf2019-03-18 15:32:44 +0000146static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
147 TCGReg dst, TCGReg src)
148{
149 g_assert_not_reached();
150}
Richard Hendersond6ecb4a2019-03-18 12:00:39 -0700151static 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}
Richard Henderson4e186172020-03-31 01:02:08 -0700156static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
157 TCGReg dst, int64_t arg)
Richard Hendersone7632cf2019-03-18 15:32:44 +0000158{
159 g_assert_not_reached();
160}
Miroslav Rezanina5e8892d2021-03-12 13:14:18 +0100161static 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])
Richard Hendersond2fd7452017-09-14 13:53:46 -0700165{
166 g_assert_not_reached();
167}
168#endif
Richard Henderson2a534af2011-11-09 08:03:34 +0000169static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
Richard Hendersona05b5b92013-08-20 17:07:26 -0700170 intptr_t arg2);
Richard Henderson59d7c142016-06-19 22:59:13 -0700171static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
172 TCGReg base, intptr_t ofs);
Richard Henderson7b7d8b22021-01-30 14:24:25 -0800173static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target,
Richard Hendersoncee44b02022-10-18 17:51:41 +1000174 const TCGHelperInfo *info);
Richard Henderson5e3d0c12022-10-20 00:55:36 +1000175static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot);
Richard Henderson21e9a8a2023-10-27 15:44:45 -0700176static bool tcg_target_const_match(int64_t val, int ct,
177 TCGType type, TCGCond cond, int vece);
Richard Henderson659ef5c2017-07-30 12:30:41 -0700178#ifdef TCG_TARGET_NEED_LDST_LABELS
Richard Hendersonaeee05f2019-04-21 14:51:00 -0700179static int tcg_out_ldst_finalize(TCGContext *s);
Richard Henderson659ef5c2017-07-30 12:30:41 -0700180#endif
bellardc896fe22008-02-01 10:05:41 +0000181
Richard Henderson23088ca2023-10-01 17:12:32 +0000182#ifndef CONFIG_USER_ONLY
183#define guest_base ({ qemu_build_not_reached(); (uintptr_t)0; })
184#endif
185
Richard Henderson8429a1c2023-04-09 22:59:09 -0700186typedef 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
Richard Hendersonde950162022-11-07 19:08:33 +1100202static void * const qemu_ld_helpers[MO_SSIZE + 1] __attribute__((unused)) = {
Richard Henderson0cadc1e2022-11-01 12:51:04 +1100203 [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,
Richard Hendersonebebea52023-04-17 10:20:51 +0200211 [MO_128] = helper_ld16_mmu,
Richard Henderson0cadc1e2022-11-01 12:51:04 +1100212#endif
213};
214
Richard Hendersonde950162022-11-07 19:08:33 +1100215static void * const qemu_st_helpers[MO_SIZE + 1] __attribute__((unused)) = {
Richard Henderson0cadc1e2022-11-01 12:51:04 +1100216 [MO_8] = helper_stb_mmu,
217 [MO_16] = helper_stw_mmu,
218 [MO_32] = helper_stl_mmu,
219 [MO_64] = helper_stq_mmu,
Richard Hendersonebebea52023-04-17 10:20:51 +0200220#if TCG_TARGET_REG_BITS == 64
221 [MO_128] = helper_st16_mmu,
222#endif
Richard Henderson0cadc1e2022-11-01 12:51:04 +1100223};
Richard Henderson0cadc1e2022-11-01 12:51:04 +1100224
Richard Hendersone63b8a22022-11-08 09:23:54 +1100225typedef 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
Richard Henderson397caba2023-10-01 07:53:03 -0700234#ifdef CONFIG_USER_ONLY
235bool tcg_use_softmmu;
236#endif
237
Richard Henderson42eb6df2021-03-13 13:36:51 -0600238TCGContext tcg_init_ctx;
239__thread TCGContext *tcg_ctx;
240
Richard Henderson5ff72582021-03-09 16:24:14 -0600241TCGContext **tcg_ctxs;
Richard Henderson0e2d61c2021-03-09 23:06:32 -0600242unsigned int tcg_cur_ctxs;
243unsigned int tcg_max_ctxs;
Richard Hendersonad75a512023-09-13 16:37:36 -0700244TCGv_env tcg_env;
Richard Hendersonc8bc1162020-11-05 15:41:38 -0800245const void *tcg_code_gen_epilogue;
Richard Hendersondb0c51a2020-10-28 12:05:44 -0700246uintptr_t tcg_splitwx_diff;
Emilio G. Cotadf2cce22017-07-12 18:26:40 -0400247
Richard Hendersonb91ccb32020-10-28 14:11:54 -0700248#ifndef CONFIG_TCG_INTERPRETER
249tcg_prologue_fn *tcg_qemu_tb_exec;
250#endif
251
Richard Hendersond2fd7452017-09-14 13:53:46 -0700252static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
blueswir1b1d8e522008-10-26 13:43:07 +0000253static TCGRegSet tcg_target_call_clobber_regs;
bellardc896fe22008-02-01 10:05:41 +0000254
Richard Henderson1813e172014-03-28 12:56:22 -0700255#if TCG_TARGET_INSN_UNIT_SIZE == 1
Peter Maydell4196dca2014-06-07 18:08:44 +0100256static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
bellardc896fe22008-02-01 10:05:41 +0000257{
258 *s->code_ptr++ = v;
259}
260
Peter Maydell4196dca2014-06-07 18:08:44 +0100261static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
262 uint8_t v)
Peter Maydell5c53bb82014-03-28 15:29:48 +0000263{
Richard Henderson1813e172014-03-28 12:56:22 -0700264 *p = v;
Peter Maydell5c53bb82014-03-28 15:29:48 +0000265}
Richard Henderson1813e172014-03-28 12:56:22 -0700266#endif
Peter Maydell5c53bb82014-03-28 15:29:48 +0000267
Richard Henderson1813e172014-03-28 12:56:22 -0700268#if TCG_TARGET_INSN_UNIT_SIZE <= 2
Peter Maydell4196dca2014-06-07 18:08:44 +0100269static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
bellardc896fe22008-02-01 10:05:41 +0000270{
Richard Henderson1813e172014-03-28 12:56:22 -0700271 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 }
bellardc896fe22008-02-01 10:05:41 +0000278}
279
Peter Maydell4196dca2014-06-07 18:08:44 +0100280static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
281 uint16_t v)
Peter Maydell5c53bb82014-03-28 15:29:48 +0000282{
Richard Henderson1813e172014-03-28 12:56:22 -0700283 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
284 *p = v;
285 } else {
286 memcpy(p, &v, sizeof(v));
287 }
Peter Maydell5c53bb82014-03-28 15:29:48 +0000288}
Richard Henderson1813e172014-03-28 12:56:22 -0700289#endif
Peter Maydell5c53bb82014-03-28 15:29:48 +0000290
Richard Henderson1813e172014-03-28 12:56:22 -0700291#if TCG_TARGET_INSN_UNIT_SIZE <= 4
Peter Maydell4196dca2014-06-07 18:08:44 +0100292static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
bellardc896fe22008-02-01 10:05:41 +0000293{
Richard Henderson1813e172014-03-28 12:56:22 -0700294 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 }
bellardc896fe22008-02-01 10:05:41 +0000301}
302
Peter Maydell4196dca2014-06-07 18:08:44 +0100303static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
304 uint32_t v)
Peter Maydell5c53bb82014-03-28 15:29:48 +0000305{
Richard Henderson1813e172014-03-28 12:56:22 -0700306 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
307 *p = v;
308 } else {
309 memcpy(p, &v, sizeof(v));
310 }
Peter Maydell5c53bb82014-03-28 15:29:48 +0000311}
Richard Henderson1813e172014-03-28 12:56:22 -0700312#endif
Peter Maydell5c53bb82014-03-28 15:29:48 +0000313
Richard Henderson1813e172014-03-28 12:56:22 -0700314#if TCG_TARGET_INSN_UNIT_SIZE <= 8
Peter Maydell4196dca2014-06-07 18:08:44 +0100315static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
Richard Hendersonac26eb62013-07-25 09:42:17 -1000316{
Richard Henderson1813e172014-03-28 12:56:22 -0700317 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 }
Richard Hendersonac26eb62013-07-25 09:42:17 -1000324}
325
Peter Maydell4196dca2014-06-07 18:08:44 +0100326static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
327 uint64_t v)
Peter Maydell5c53bb82014-03-28 15:29:48 +0000328{
Richard Henderson1813e172014-03-28 12:56:22 -0700329 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
330 *p = v;
331 } else {
332 memcpy(p, &v, sizeof(v));
333 }
Peter Maydell5c53bb82014-03-28 15:29:48 +0000334}
Richard Henderson1813e172014-03-28 12:56:22 -0700335#endif
Peter Maydell5c53bb82014-03-28 15:29:48 +0000336
bellardc896fe22008-02-01 10:05:41 +0000337/* label relocation processing */
338
Richard Henderson1813e172014-03-28 12:56:22 -0700339static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
Richard Hendersonbec16312015-02-13 13:39:54 -0800340 TCGLabel *l, intptr_t addend)
bellardc896fe22008-02-01 10:05:41 +0000341{
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700342 TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
bellardc896fe22008-02-01 10:05:41 +0000343
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700344 r->type = type;
345 r->ptr = code_ptr;
346 r->addend = addend;
347 QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
bellardc896fe22008-02-01 10:05:41 +0000348}
349
Richard Henderson92ab8e72020-10-28 18:55:50 -0700350static void tcg_out_label(TCGContext *s, TCGLabel *l)
bellardc896fe22008-02-01 10:05:41 +0000351{
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +0200352 tcg_debug_assert(!l->has_value);
bellardc896fe22008-02-01 10:05:41 +0000353 l->has_value = 1;
Richard Henderson92ab8e72020-10-28 18:55:50 -0700354 l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
bellardc896fe22008-02-01 10:05:41 +0000355}
356
Richard Henderson42a268c2015-02-13 12:51:55 -0800357TCGLabel *gen_new_label(void)
bellardc896fe22008-02-01 10:05:41 +0000358{
Emilio G. Cotab1311c42017-07-12 17:15:52 -0400359 TCGContext *s = tcg_ctx;
Richard Henderson51e39722015-02-13 18:51:05 -0800360 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
bellardc896fe22008-02-01 10:05:41 +0000361
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700362 memset(l, 0, sizeof(TCGLabel));
363 l->id = s->nb_labels++;
Richard Hendersonf85b1fc2023-03-03 13:47:27 -0800364 QSIMPLEQ_INIT(&l->branches);
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700365 QSIMPLEQ_INIT(&l->relocs);
366
Richard Hendersonbef16ab2019-02-07 13:26:40 +0000367 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
Richard Henderson42a268c2015-02-13 12:51:55 -0800368
369 return l;
bellardc896fe22008-02-01 10:05:41 +0000370}
371
Richard Henderson7ecd02a2019-04-21 13:34:35 -0700372static 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
Richard Henderson9f754622018-06-14 19:57:03 -1000389static void set_jmp_reset_offset(TCGContext *s, int which)
390{
Richard Hendersonf14bed32020-11-02 19:36:20 -0800391 /*
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 */
Richard Hendersonb7e4afb2022-11-26 18:39:55 -0800395 s->gen_tb->jmp_reset_offset[which] = tcg_current_code_size(s);
Richard Henderson9f754622018-06-14 19:57:03 -1000396}
397
Richard Hendersonb52a2c02022-11-26 15:18:44 -0800398static 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 */
Richard Henderson9da60792022-11-26 18:54:23 -0800404 s->gen_tb->jmp_insn_offset[which] = tcg_current_code_size(s);
Richard Hendersonb52a2c02022-11-26 15:18:44 -0800405}
406
Richard Hendersonbecc4522022-11-26 17:42:11 -0800407static 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 */
Richard Henderson9da60792022-11-26 18:54:23 -0800413 return (uintptr_t)tcg_splitwx_to_rx(&s->gen_tb->jmp_target_addr[which]);
Richard Hendersonbecc4522022-11-26 17:42:11 -0800414}
415
Richard Henderson397caba2023-10-01 07:53:03 -0700416static int __attribute__((unused))
417tlb_mask_table_ofs(TCGContext *s, int which)
Richard Hendersond0a9bb52023-03-27 16:07:15 -0700418{
Richard Henderson7857ee12023-09-13 17:56:21 -0700419 return (offsetof(CPUNegativeOffsetState, tlb.f[which]) -
420 sizeof(CPUNegativeOffsetState));
Richard Hendersond0a9bb52023-03-27 16:07:15 -0700421}
Richard Hendersond0a9bb52023-03-27 16:07:15 -0700422
Richard Hendersondb6b7d02021-01-31 23:29:26 -1000423/* Signal overflow, starting over with fewer guest insns. */
Marc-André Lureau89057702022-04-20 17:26:02 +0400424static G_NORETURN
425void tcg_raise_tb_overflow(TCGContext *s)
Richard Hendersondb6b7d02021-01-31 23:29:26 -1000426{
427 siglongjmp(s->jmp_trans, -2);
428}
429
Richard Henderson8429a1c2023-04-09 22:59:09 -0700430/*
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 */
Richard Henderson129f1f92023-04-05 22:27:03 -0700439typedef struct TCGMovExtend {
Richard Henderson8429a1c2023-04-09 22:59:09 -0700440 unsigned dst;
Richard Henderson129f1f92023-04-05 22:27:03 -0700441 TCGReg src;
442 TCGType dst_type;
443 TCGType src_type;
444 MemOp src_ext;
445} TCGMovExtend;
446
Richard Hendersonb3dfd5f2023-04-05 21:16:28 -0700447/**
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 */
Richard Henderson129f1f92023-04-05 22:27:03 -0700458static void tcg_out_movext(TCGContext *s, TCGType dst_type, TCGReg dst,
459 TCGType src_type, MemOp src_ext, TCGReg src)
Richard Hendersonb3dfd5f2023-04-05 21:16:28 -0700460{
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
Richard Henderson129f1f92023-04-05 22:27:03 -0700509/* 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
Richard Henderson8429a1c2023-04-09 22:59:09 -0700532static void tcg_out_movext2(TCGContext *s, const TCGMovExtend *i1,
533 const TCGMovExtend *i2, int scratch)
Richard Henderson129f1f92023-04-05 22:27:03 -0700534{
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
Richard Henderson2462e302023-05-14 09:58:39 -0700561/**
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
Richard Henderson4c22e842020-10-16 22:20:55 -0700637#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),
Richard Hendersonca5bed02024-01-02 01:27:18 +0000657#define C_N1O1_I1(O1, O2, I1) C_PFX3(c_n1o1_i1_, O1, O2, I1),
Richard Hendersonfa645b42023-09-16 15:01:45 -0700658#define C_N2_I1(O1, O2, I1) C_PFX3(c_n2_i1_, O1, O2, I1),
Richard Henderson4c22e842020-10-16 22:20:55 -0700659
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),
Ilya Leoshkevich22d2e532023-07-20 00:11:18 +0200664#define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_n1_o1_i4_, O1, O2, I1, I2, I3, I4),
Richard Henderson4c22e842020-10-16 22:20:55 -0700665
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
Richard Hendersonca5bed02024-01-02 01:27:18 +0000681#undef C_N1O1_I1
Richard Hendersonfa645b42023-09-16 15:01:45 -0700682#undef C_N2_I1
Richard Henderson4c22e842020-10-16 22:20:55 -0700683#undef C_O2_I1
684#undef C_O2_I2
685#undef C_O2_I3
686#undef C_O2_I4
Ilya Leoshkevich22d2e532023-07-20 00:11:18 +0200687#undef C_N1_O1_I4
Richard Henderson4c22e842020-10-16 22:20:55 -0700688
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 } },
Richard Hendersonca5bed02024-01-02 01:27:18 +0000702#define C_N1O1_I1(O1, O2, I1) { .args_ct_str = { "&" #O1, #O2, #I1 } },
Richard Hendersonfa645b42023-09-16 15:01:45 -0700703#define C_N2_I1(O1, O2, I1) { .args_ct_str = { "&" #O1, "&" #O2, #I1 } },
Richard Henderson4c22e842020-10-16 22:20:55 -0700704
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 } },
Ilya Leoshkevich22d2e532023-07-20 00:11:18 +0200709#define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { "&" #O1, #O2, #I1, #I2, #I3, #I4 } },
Richard Henderson4c22e842020-10-16 22:20:55 -0700710
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
Richard Hendersonca5bed02024-01-02 01:27:18 +0000725#undef C_N1O1_I1
Richard Hendersonfa645b42023-09-16 15:01:45 -0700726#undef C_N2_I1
Richard Henderson4c22e842020-10-16 22:20:55 -0700727#undef C_O2_I1
728#undef C_O2_I2
729#undef C_O2_I3
730#undef C_O2_I4
Ilya Leoshkevich22d2e532023-07-20 00:11:18 +0200731#undef C_N1_O1_I4
Richard Henderson4c22e842020-10-16 22:20:55 -0700732
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)
Richard Hendersonca5bed02024-01-02 01:27:18 +0000746#define C_N1O1_I1(O1, O2, I1) C_PFX3(c_n1o1_i1_, O1, O2, I1)
Richard Hendersonfa645b42023-09-16 15:01:45 -0700747#define C_N2_I1(O1, O2, I1) C_PFX3(c_n2_i1_, O1, O2, I1)
Richard Henderson4c22e842020-10-16 22:20:55 -0700748
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)
Ilya Leoshkevich22d2e532023-07-20 00:11:18 +0200753#define C_N1_O1_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_n1_o1_i4_, O1, O2, I1, I2, I3, I4)
Richard Henderson4c22e842020-10-16 22:20:55 -0700754
Paolo Bonzini139c1832020-02-04 12:41:01 +0100755#include "tcg-target.c.inc"
bellardc896fe22008-02-01 10:05:41 +0000756
Richard Henderson7857ee12023-09-13 17:56:21 -0700757#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
Emilio G. Cotae8feb962017-07-07 19:24:20 -0400764/*
Emilio G. Cota3468b592017-07-19 18:57:58 -0400765 * All TCG threads except the parent (i.e. the one that called tcg_context_init
766 * and registered the target's TCG globals) must register with this function
767 * before initiating translation.
768 *
769 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
770 * of tcg_region_init() for the reasoning behind this.
771 *
Philippe Mathieu-Daudé7893e422023-10-04 11:06:20 +0200772 * In system-mode each caller registers its context in tcg_ctxs[]. Note that in
773 * system-mode tcg_ctxs[] does not track tcg_ctx_init, since the initial context
Emilio G. Cota3468b592017-07-19 18:57:58 -0400774 * is not used anymore for translation once this function is called.
775 *
Philippe Mathieu-Daudé7893e422023-10-04 11:06:20 +0200776 * Not tracking tcg_init_ctx in tcg_ctxs[] in system-mode keeps code that
777 * iterates over the array (e.g. tcg_code_size() the same for both system/user
778 * modes.
Emilio G. Cota3468b592017-07-19 18:57:58 -0400779 */
780#ifdef CONFIG_USER_ONLY
781void tcg_register_thread(void)
782{
783 tcg_ctx = &tcg_init_ctx;
784}
785#else
786void tcg_register_thread(void)
787{
788 TCGContext *s = g_malloc(sizeof(*s));
789 unsigned int i, n;
Emilio G. Cota3468b592017-07-19 18:57:58 -0400790
791 *s = tcg_init_ctx;
792
793 /* Relink mem_base. */
794 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
795 if (tcg_init_ctx.temps[i].mem_base) {
796 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
797 tcg_debug_assert(b >= 0 && b < n);
798 s->temps[i].mem_base = &s->temps[b];
799 }
800 }
801
802 /* Claim an entry in tcg_ctxs */
Richard Henderson0e2d61c2021-03-09 23:06:32 -0600803 n = qatomic_fetch_inc(&tcg_cur_ctxs);
804 g_assert(n < tcg_max_ctxs);
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100805 qatomic_set(&tcg_ctxs[n], s);
Emilio G. Cota3468b592017-07-19 18:57:58 -0400806
Emilio G. Cota38b47b12018-12-07 15:33:56 -0500807 if (n > 0) {
Richard Hendersonbf042e82021-03-09 16:33:15 -0600808 tcg_region_initial_alloc(s);
Emilio G. Cota38b47b12018-12-07 15:33:56 -0500809 }
810
Emilio G. Cota3468b592017-07-19 18:57:58 -0400811 tcg_ctx = s;
Emilio G. Cota3468b592017-07-19 18:57:58 -0400812}
813#endif /* !CONFIG_USER_ONLY */
814
bellardc896fe22008-02-01 10:05:41 +0000815/* pool based memory allocation */
816void *tcg_malloc_internal(TCGContext *s, int size)
817{
818 TCGPool *p;
819 int pool_size;
Richard Hendersona813e362022-11-30 22:38:25 -0800820
bellardc896fe22008-02-01 10:05:41 +0000821 if (size > TCG_POOL_CHUNK_SIZE) {
822 /* big malloc: insert a new pool (XXX: could optimize) */
Anthony Liguori7267c092011-08-20 22:09:37 -0500823 p = g_malloc(sizeof(TCGPool) + size);
bellardc896fe22008-02-01 10:05:41 +0000824 p->size = size;
Kirill Batuzov40552992012-03-02 13:22:17 +0400825 p->next = s->pool_first_large;
826 s->pool_first_large = p;
827 return p->data;
bellardc896fe22008-02-01 10:05:41 +0000828 } else {
829 p = s->pool_current;
830 if (!p) {
831 p = s->pool_first;
832 if (!p)
833 goto new_pool;
834 } else {
835 if (!p->next) {
836 new_pool:
837 pool_size = TCG_POOL_CHUNK_SIZE;
Anthony Liguori7267c092011-08-20 22:09:37 -0500838 p = g_malloc(sizeof(TCGPool) + pool_size);
bellardc896fe22008-02-01 10:05:41 +0000839 p->size = pool_size;
840 p->next = NULL;
Richard Hendersona813e362022-11-30 22:38:25 -0800841 if (s->pool_current) {
bellardc896fe22008-02-01 10:05:41 +0000842 s->pool_current->next = p;
Richard Hendersona813e362022-11-30 22:38:25 -0800843 } else {
bellardc896fe22008-02-01 10:05:41 +0000844 s->pool_first = p;
Richard Hendersona813e362022-11-30 22:38:25 -0800845 }
bellardc896fe22008-02-01 10:05:41 +0000846 } else {
847 p = p->next;
848 }
849 }
850 }
851 s->pool_current = p;
852 s->pool_cur = p->data + size;
853 s->pool_end = p->data + p->size;
854 return p->data;
855}
856
857void tcg_pool_reset(TCGContext *s)
858{
Kirill Batuzov40552992012-03-02 13:22:17 +0400859 TCGPool *p, *t;
860 for (p = s->pool_first_large; p; p = t) {
861 t = p->next;
862 g_free(p);
863 }
864 s->pool_first_large = NULL;
bellardc896fe22008-02-01 10:05:41 +0000865 s->pool_cur = s->pool_end = NULL;
866 s->pool_current = NULL;
867}
868
Richard Henderson8429a1c2023-04-09 22:59:09 -0700869/*
870 * Create TCGHelperInfo structures for "tcg/tcg-ldst.h" functions,
871 * akin to what "exec/helper-tcg.h" does with DEF_HELPER_FLAGS_N.
872 * We only use these for layout in tcg_out_ld_helper_ret and
873 * tcg_out_st_helper_args, and share them between several of
874 * the helpers, with the end result that it's easier to build manually.
875 */
876
877#if TCG_TARGET_REG_BITS == 32
878# define dh_typecode_ttl dh_typecode_i32
879#else
880# define dh_typecode_ttl dh_typecode_i64
881#endif
882
883static TCGHelperInfo info_helper_ld32_mmu = {
884 .flags = TCG_CALL_NO_WG,
885 .typemask = dh_typemask(ttl, 0) /* return tcg_target_ulong */
886 | dh_typemask(env, 1)
Richard Henderson24e46e62023-04-26 22:09:47 +0100887 | dh_typemask(i64, 2) /* uint64_t addr */
Richard Henderson8429a1c2023-04-09 22:59:09 -0700888 | dh_typemask(i32, 3) /* unsigned oi */
889 | dh_typemask(ptr, 4) /* uintptr_t ra */
890};
891
892static TCGHelperInfo info_helper_ld64_mmu = {
893 .flags = TCG_CALL_NO_WG,
894 .typemask = dh_typemask(i64, 0) /* return uint64_t */
895 | dh_typemask(env, 1)
Richard Henderson24e46e62023-04-26 22:09:47 +0100896 | dh_typemask(i64, 2) /* uint64_t addr */
Richard Henderson8429a1c2023-04-09 22:59:09 -0700897 | dh_typemask(i32, 3) /* unsigned oi */
898 | dh_typemask(ptr, 4) /* uintptr_t ra */
899};
900
Richard Hendersonebebea52023-04-17 10:20:51 +0200901static TCGHelperInfo info_helper_ld128_mmu = {
902 .flags = TCG_CALL_NO_WG,
903 .typemask = dh_typemask(i128, 0) /* return Int128 */
904 | dh_typemask(env, 1)
Richard Henderson24e46e62023-04-26 22:09:47 +0100905 | dh_typemask(i64, 2) /* uint64_t addr */
Richard Hendersonebebea52023-04-17 10:20:51 +0200906 | dh_typemask(i32, 3) /* unsigned oi */
907 | dh_typemask(ptr, 4) /* uintptr_t ra */
908};
909
Richard Henderson8429a1c2023-04-09 22:59:09 -0700910static TCGHelperInfo info_helper_st32_mmu = {
911 .flags = TCG_CALL_NO_WG,
912 .typemask = dh_typemask(void, 0)
913 | dh_typemask(env, 1)
Richard Henderson24e46e62023-04-26 22:09:47 +0100914 | dh_typemask(i64, 2) /* uint64_t addr */
Richard Henderson8429a1c2023-04-09 22:59:09 -0700915 | dh_typemask(i32, 3) /* uint32_t data */
916 | dh_typemask(i32, 4) /* unsigned oi */
917 | dh_typemask(ptr, 5) /* uintptr_t ra */
918};
919
920static TCGHelperInfo info_helper_st64_mmu = {
921 .flags = TCG_CALL_NO_WG,
922 .typemask = dh_typemask(void, 0)
923 | dh_typemask(env, 1)
Richard Henderson24e46e62023-04-26 22:09:47 +0100924 | dh_typemask(i64, 2) /* uint64_t addr */
Richard Henderson8429a1c2023-04-09 22:59:09 -0700925 | dh_typemask(i64, 3) /* uint64_t data */
926 | dh_typemask(i32, 4) /* unsigned oi */
927 | dh_typemask(ptr, 5) /* uintptr_t ra */
928};
929
Richard Hendersonebebea52023-04-17 10:20:51 +0200930static TCGHelperInfo info_helper_st128_mmu = {
931 .flags = TCG_CALL_NO_WG,
932 .typemask = dh_typemask(void, 0)
933 | dh_typemask(env, 1)
Richard Henderson24e46e62023-04-26 22:09:47 +0100934 | dh_typemask(i64, 2) /* uint64_t addr */
Richard Hendersonebebea52023-04-17 10:20:51 +0200935 | dh_typemask(i128, 3) /* Int128 data */
936 | dh_typemask(i32, 4) /* unsigned oi */
937 | dh_typemask(ptr, 5) /* uintptr_t ra */
938};
939
Richard Henderson22f15572021-03-18 12:46:44 -0600940#ifdef CONFIG_TCG_INTERPRETER
Philippe Mathieu-Daudéc6ef8c72022-11-22 19:08:02 +0100941static ffi_type *typecode_to_ffi(int argmask)
942{
Richard Hendersone9709e12022-10-21 10:47:54 +1000943 /*
944 * libffi does not support __int128_t, so we have forced Int128
945 * to use the structure definition instead of the builtin type.
946 */
947 static ffi_type *ffi_type_i128_elements[3] = {
948 &ffi_type_uint64,
949 &ffi_type_uint64,
950 NULL
951 };
952 static ffi_type ffi_type_i128 = {
953 .size = 16,
954 .alignment = __alignof__(Int128),
955 .type = FFI_TYPE_STRUCT,
956 .elements = ffi_type_i128_elements,
957 };
958
Philippe Mathieu-Daudéc6ef8c72022-11-22 19:08:02 +0100959 switch (argmask) {
960 case dh_typecode_void:
961 return &ffi_type_void;
962 case dh_typecode_i32:
963 return &ffi_type_uint32;
964 case dh_typecode_s32:
965 return &ffi_type_sint32;
966 case dh_typecode_i64:
967 return &ffi_type_uint64;
968 case dh_typecode_s64:
969 return &ffi_type_sint64;
970 case dh_typecode_ptr:
971 return &ffi_type_pointer;
Richard Hendersone9709e12022-10-21 10:47:54 +1000972 case dh_typecode_i128:
973 return &ffi_type_i128;
Philippe Mathieu-Daudéc6ef8c72022-11-22 19:08:02 +0100974 }
975 g_assert_not_reached();
976}
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +0100977
Richard Hendersond53106c2023-03-31 10:37:04 -0700978static ffi_cif *init_ffi_layout(TCGHelperInfo *info)
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +0100979{
Richard Hendersond53106c2023-03-31 10:37:04 -0700980 unsigned typemask = info->typemask;
981 struct {
982 ffi_cif cif;
983 ffi_type *args[];
984 } *ca;
985 ffi_status status;
986 int nargs;
Richard Hendersonf9c4bb82022-11-22 19:08:04 +0100987
Richard Hendersond53106c2023-03-31 10:37:04 -0700988 /* Ignoring the return type, find the last non-zero field. */
989 nargs = 32 - clz32(typemask >> 3);
990 nargs = DIV_ROUND_UP(nargs, 3);
991 assert(nargs <= MAX_CALL_IARGS);
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +0100992
Richard Hendersond53106c2023-03-31 10:37:04 -0700993 ca = g_malloc0(sizeof(*ca) + nargs * sizeof(ffi_type *));
994 ca->cif.rtype = typecode_to_ffi(typemask & 7);
995 ca->cif.nargs = nargs;
996
997 if (nargs != 0) {
998 ca->cif.arg_types = ca->args;
999 for (int j = 0; j < nargs; ++j) {
1000 int typecode = extract32(typemask, (j + 1) * 3, 3);
1001 ca->args[j] = typecode_to_ffi(typecode);
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +01001002 }
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +01001003 }
Richard Hendersonf9c4bb82022-11-22 19:08:04 +01001004
Richard Hendersond53106c2023-03-31 10:37:04 -07001005 status = ffi_prep_cif(&ca->cif, FFI_DEFAULT_ABI, nargs,
1006 ca->cif.rtype, ca->cif.arg_types);
1007 assert(status == FFI_OK);
1008
1009 return &ca->cif;
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +01001010}
Richard Hendersond53106c2023-03-31 10:37:04 -07001011
1012#define HELPER_INFO_INIT(I) (&(I)->cif)
1013#define HELPER_INFO_INIT_VAL(I) init_ffi_layout(I)
1014#else
1015#define HELPER_INFO_INIT(I) (&(I)->init)
1016#define HELPER_INFO_INIT_VAL(I) 1
Philippe Mathieu-Daudé0c22e172022-11-22 19:08:03 +01001017#endif /* CONFIG_TCG_INTERPRETER */
Richard Henderson22f15572021-03-18 12:46:44 -06001018
Richard Henderson338b61e2023-04-08 17:28:07 -07001019static inline bool arg_slot_reg_p(unsigned arg_slot)
1020{
1021 /*
1022 * Split the sizeof away from the comparison to avoid Werror from
1023 * "unsigned < 0 is always false", when iarg_regs is empty.
1024 */
1025 unsigned nreg = ARRAY_SIZE(tcg_target_call_iarg_regs);
1026 return arg_slot < nreg;
1027}
1028
Richard Hendersond78e4a42023-04-08 19:05:10 -07001029static inline int arg_slot_stk_ofs(unsigned arg_slot)
1030{
1031 unsigned max = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
1032 unsigned stk_slot = arg_slot - ARRAY_SIZE(tcg_target_call_iarg_regs);
1033
1034 tcg_debug_assert(stk_slot < max);
1035 return TCG_TARGET_CALL_STACK_OFFSET + stk_slot * sizeof(tcg_target_long);
1036}
1037
Richard Henderson39004a72022-11-11 10:09:37 +10001038typedef struct TCGCumulativeArgs {
1039 int arg_idx; /* tcg_gen_callN args[] */
1040 int info_in_idx; /* TCGHelperInfo in[] */
1041 int arg_slot; /* regs+stack slot */
1042 int ref_slot; /* stack slots for references */
1043} TCGCumulativeArgs;
1044
1045static void layout_arg_even(TCGCumulativeArgs *cum)
1046{
1047 cum->arg_slot += cum->arg_slot & 1;
1048}
1049
1050static void layout_arg_1(TCGCumulativeArgs *cum, TCGHelperInfo *info,
1051 TCGCallArgumentKind kind)
1052{
1053 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1054
1055 *loc = (TCGCallArgumentLoc){
1056 .kind = kind,
1057 .arg_idx = cum->arg_idx,
1058 .arg_slot = cum->arg_slot,
1059 };
1060 cum->info_in_idx++;
1061 cum->arg_slot++;
1062}
1063
1064static void layout_arg_normal_n(TCGCumulativeArgs *cum,
1065 TCGHelperInfo *info, int n)
1066{
1067 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1068
1069 for (int i = 0; i < n; ++i) {
1070 /* Layout all using the same arg_idx, adjusting the subindex. */
1071 loc[i] = (TCGCallArgumentLoc){
1072 .kind = TCG_CALL_ARG_NORMAL,
1073 .arg_idx = cum->arg_idx,
1074 .tmp_subindex = i,
1075 .arg_slot = cum->arg_slot + i,
1076 };
1077 }
1078 cum->info_in_idx += n;
1079 cum->arg_slot += n;
1080}
1081
Richard Henderson313bdea2022-10-31 09:22:59 +11001082static void layout_arg_by_ref(TCGCumulativeArgs *cum, TCGHelperInfo *info)
1083{
1084 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
1085 int n = 128 / TCG_TARGET_REG_BITS;
1086
1087 /* The first subindex carries the pointer. */
1088 layout_arg_1(cum, info, TCG_CALL_ARG_BY_REF);
1089
1090 /*
1091 * The callee is allowed to clobber memory associated with
1092 * structure pass by-reference. Therefore we must make copies.
1093 * Allocate space from "ref_slot", which will be adjusted to
1094 * follow the parameters on the stack.
1095 */
1096 loc[0].ref_slot = cum->ref_slot;
1097
1098 /*
1099 * Subsequent words also go into the reference slot, but
1100 * do not accumulate into the regular arguments.
1101 */
1102 for (int i = 1; i < n; ++i) {
1103 loc[i] = (TCGCallArgumentLoc){
1104 .kind = TCG_CALL_ARG_BY_REF_N,
1105 .arg_idx = cum->arg_idx,
1106 .tmp_subindex = i,
1107 .ref_slot = cum->ref_slot + i,
1108 };
1109 }
Richard Hendersone18ed262023-07-07 10:17:44 +00001110 cum->info_in_idx += n - 1; /* i=0 accounted for in layout_arg_1 */
Richard Henderson313bdea2022-10-31 09:22:59 +11001111 cum->ref_slot += n;
1112}
1113
Richard Henderson39004a72022-11-11 10:09:37 +10001114static void init_call_layout(TCGHelperInfo *info)
1115{
1116 int max_reg_slots = ARRAY_SIZE(tcg_target_call_iarg_regs);
1117 int max_stk_slots = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
1118 unsigned typemask = info->typemask;
1119 unsigned typecode;
1120 TCGCumulativeArgs cum = { };
1121
1122 /*
1123 * Parse and place any function return value.
1124 */
1125 typecode = typemask & 7;
1126 switch (typecode) {
1127 case dh_typecode_void:
1128 info->nr_out = 0;
1129 break;
1130 case dh_typecode_i32:
1131 case dh_typecode_s32:
1132 case dh_typecode_ptr:
1133 info->nr_out = 1;
1134 info->out_kind = TCG_CALL_RET_NORMAL;
1135 break;
1136 case dh_typecode_i64:
1137 case dh_typecode_s64:
1138 info->nr_out = 64 / TCG_TARGET_REG_BITS;
1139 info->out_kind = TCG_CALL_RET_NORMAL;
Richard Henderson5e3d0c12022-10-20 00:55:36 +10001140 /* Query the last register now to trigger any assert early. */
1141 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
Richard Henderson466d3752022-11-11 11:01:13 +10001142 break;
1143 case dh_typecode_i128:
1144 info->nr_out = 128 / TCG_TARGET_REG_BITS;
Richard Henderson5427a9a2022-10-20 07:54:48 +10001145 info->out_kind = TCG_TARGET_CALL_RET_I128;
1146 switch (TCG_TARGET_CALL_RET_I128) {
Richard Henderson466d3752022-11-11 11:01:13 +10001147 case TCG_CALL_RET_NORMAL:
Richard Henderson5e3d0c12022-10-20 00:55:36 +10001148 /* Query the last register now to trigger any assert early. */
1149 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
Richard Henderson466d3752022-11-11 11:01:13 +10001150 break;
Richard Hendersonc6556aa2022-10-20 01:13:52 +10001151 case TCG_CALL_RET_BY_VEC:
1152 /* Query the single register now to trigger any assert early. */
1153 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0);
1154 break;
Richard Henderson313bdea2022-10-31 09:22:59 +11001155 case TCG_CALL_RET_BY_REF:
1156 /*
1157 * Allocate the first argument to the output.
1158 * We don't need to store this anywhere, just make it
1159 * unavailable for use in the input loop below.
1160 */
1161 cum.arg_slot = 1;
1162 break;
Richard Henderson466d3752022-11-11 11:01:13 +10001163 default:
1164 qemu_build_not_reached();
1165 }
Richard Henderson39004a72022-11-11 10:09:37 +10001166 break;
1167 default:
1168 g_assert_not_reached();
1169 }
Richard Henderson39004a72022-11-11 10:09:37 +10001170
1171 /*
1172 * Parse and place function arguments.
1173 */
1174 for (typemask >>= 3; typemask; typemask >>= 3, cum.arg_idx++) {
1175 TCGCallArgumentKind kind;
1176 TCGType type;
1177
1178 typecode = typemask & 7;
1179 switch (typecode) {
1180 case dh_typecode_i32:
1181 case dh_typecode_s32:
1182 type = TCG_TYPE_I32;
1183 break;
1184 case dh_typecode_i64:
1185 case dh_typecode_s64:
1186 type = TCG_TYPE_I64;
1187 break;
1188 case dh_typecode_ptr:
1189 type = TCG_TYPE_PTR;
1190 break;
Richard Henderson466d3752022-11-11 11:01:13 +10001191 case dh_typecode_i128:
1192 type = TCG_TYPE_I128;
1193 break;
Richard Henderson39004a72022-11-11 10:09:37 +10001194 default:
1195 g_assert_not_reached();
1196 }
1197
1198 switch (type) {
1199 case TCG_TYPE_I32:
1200 switch (TCG_TARGET_CALL_ARG_I32) {
1201 case TCG_CALL_ARG_EVEN:
1202 layout_arg_even(&cum);
1203 /* fall through */
1204 case TCG_CALL_ARG_NORMAL:
1205 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
1206 break;
1207 case TCG_CALL_ARG_EXTEND:
1208 kind = TCG_CALL_ARG_EXTEND_U + (typecode & 1);
1209 layout_arg_1(&cum, info, kind);
1210 break;
1211 default:
1212 qemu_build_not_reached();
1213 }
1214 break;
1215
1216 case TCG_TYPE_I64:
1217 switch (TCG_TARGET_CALL_ARG_I64) {
1218 case TCG_CALL_ARG_EVEN:
1219 layout_arg_even(&cum);
1220 /* fall through */
1221 case TCG_CALL_ARG_NORMAL:
1222 if (TCG_TARGET_REG_BITS == 32) {
1223 layout_arg_normal_n(&cum, info, 2);
1224 } else {
1225 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
1226 }
1227 break;
1228 default:
1229 qemu_build_not_reached();
1230 }
1231 break;
1232
Richard Henderson466d3752022-11-11 11:01:13 +10001233 case TCG_TYPE_I128:
Richard Henderson5427a9a2022-10-20 07:54:48 +10001234 switch (TCG_TARGET_CALL_ARG_I128) {
Richard Henderson466d3752022-11-11 11:01:13 +10001235 case TCG_CALL_ARG_EVEN:
1236 layout_arg_even(&cum);
1237 /* fall through */
1238 case TCG_CALL_ARG_NORMAL:
1239 layout_arg_normal_n(&cum, info, 128 / TCG_TARGET_REG_BITS);
1240 break;
Richard Henderson313bdea2022-10-31 09:22:59 +11001241 case TCG_CALL_ARG_BY_REF:
1242 layout_arg_by_ref(&cum, info);
1243 break;
Richard Henderson466d3752022-11-11 11:01:13 +10001244 default:
1245 qemu_build_not_reached();
1246 }
1247 break;
1248
Richard Henderson39004a72022-11-11 10:09:37 +10001249 default:
1250 g_assert_not_reached();
1251 }
1252 }
1253 info->nr_in = cum.info_in_idx;
1254
1255 /* Validate that we didn't overrun the input array. */
1256 assert(cum.info_in_idx <= ARRAY_SIZE(info->in));
1257 /* Validate the backend has enough argument space. */
1258 assert(cum.arg_slot <= max_reg_slots + max_stk_slots);
Richard Henderson313bdea2022-10-31 09:22:59 +11001259
1260 /*
1261 * Relocate the "ref_slot" area to the end of the parameters.
1262 * Minimizing this stack offset helps code size for x86,
1263 * which has a signed 8-bit offset encoding.
1264 */
1265 if (cum.ref_slot != 0) {
1266 int ref_base = 0;
1267
1268 if (cum.arg_slot > max_reg_slots) {
1269 int align = __alignof(Int128) / sizeof(tcg_target_long);
1270
1271 ref_base = cum.arg_slot - max_reg_slots;
1272 if (align > 1) {
1273 ref_base = ROUND_UP(ref_base, align);
1274 }
1275 }
1276 assert(ref_base + cum.ref_slot <= max_stk_slots);
Richard Hendersond78e4a42023-04-08 19:05:10 -07001277 ref_base += max_reg_slots;
Richard Henderson313bdea2022-10-31 09:22:59 +11001278
1279 if (ref_base != 0) {
1280 for (int i = cum.info_in_idx - 1; i >= 0; --i) {
1281 TCGCallArgumentLoc *loc = &info->in[i];
1282 switch (loc->kind) {
1283 case TCG_CALL_ARG_BY_REF:
1284 case TCG_CALL_ARG_BY_REF_N:
1285 loc->ref_slot += ref_base;
1286 break;
1287 default:
1288 break;
1289 }
1290 }
1291 }
1292 }
Richard Henderson39004a72022-11-11 10:09:37 +10001293}
1294
Richard Henderson91478ce2015-08-18 23:23:08 -07001295static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
Richard Hendersonf69d2772016-11-18 09:31:40 +01001296static void process_op_defs(TCGContext *s);
Richard Henderson1c2adb92017-10-10 14:34:37 -07001297static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1298 TCGReg reg, const char *name);
Richard Henderson91478ce2015-08-18 23:23:08 -07001299
Richard Henderson43b972b2021-03-09 22:52:45 -06001300static void tcg_context_init(unsigned max_cpus)
bellardc896fe22008-02-01 10:05:41 +00001301{
Richard Hendersona76aabd2021-03-09 17:24:33 -06001302 TCGContext *s = &tcg_init_ctx;
Richard Henderson100b5e02013-09-14 15:57:22 -07001303 int op, total_args, n, i;
bellardc896fe22008-02-01 10:05:41 +00001304 TCGOpDef *def;
1305 TCGArgConstraint *args_ct;
Richard Henderson1c2adb92017-10-10 14:34:37 -07001306 TCGTemp *ts;
bellardc896fe22008-02-01 10:05:41 +00001307
1308 memset(s, 0, sizeof(*s));
bellardc896fe22008-02-01 10:05:41 +00001309 s->nb_globals = 0;
Richard Hendersonc70fbf02016-06-23 20:34:22 -07001310
bellardc896fe22008-02-01 10:05:41 +00001311 /* Count total number of arguments and allocate the corresponding
1312 space */
1313 total_args = 0;
1314 for(op = 0; op < NB_OPS; op++) {
1315 def = &tcg_op_defs[op];
1316 n = def->nb_iargs + def->nb_oargs;
1317 total_args += n;
1318 }
1319
Richard Hendersonbc2b17e2019-04-04 19:34:19 -07001320 args_ct = g_new0(TCGArgConstraint, total_args);
bellardc896fe22008-02-01 10:05:41 +00001321
1322 for(op = 0; op < NB_OPS; op++) {
1323 def = &tcg_op_defs[op];
1324 def->args_ct = args_ct;
bellardc896fe22008-02-01 10:05:41 +00001325 n = def->nb_iargs + def->nb_oargs;
bellardc896fe22008-02-01 10:05:41 +00001326 args_ct += n;
1327 }
Richard Henderson5cd8f622013-09-14 15:09:39 -07001328
Richard Henderson8429a1c2023-04-09 22:59:09 -07001329 init_call_layout(&info_helper_ld32_mmu);
1330 init_call_layout(&info_helper_ld64_mmu);
Richard Hendersonebebea52023-04-17 10:20:51 +02001331 init_call_layout(&info_helper_ld128_mmu);
Richard Henderson8429a1c2023-04-09 22:59:09 -07001332 init_call_layout(&info_helper_st32_mmu);
1333 init_call_layout(&info_helper_st64_mmu);
Richard Hendersonebebea52023-04-17 10:20:51 +02001334 init_call_layout(&info_helper_st128_mmu);
Richard Henderson8429a1c2023-04-09 22:59:09 -07001335
bellardc896fe22008-02-01 10:05:41 +00001336 tcg_target_init(s);
Richard Hendersonf69d2772016-11-18 09:31:40 +01001337 process_op_defs(s);
Richard Henderson91478ce2015-08-18 23:23:08 -07001338
1339 /* Reverse the order of the saved registers, assuming they're all at
1340 the start of tcg_target_reg_alloc_order. */
1341 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
1342 int r = tcg_target_reg_alloc_order[n];
1343 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
1344 break;
1345 }
1346 }
1347 for (i = 0; i < n; ++i) {
1348 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
1349 }
1350 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
1351 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
1352 }
Emilio G. Cotab1311c42017-07-12 17:15:52 -04001353
1354 tcg_ctx = s;
Emilio G. Cota3468b592017-07-19 18:57:58 -04001355 /*
1356 * In user-mode we simply share the init context among threads, since we
1357 * use a single region. See the documentation tcg_region_init() for the
1358 * reasoning behind this.
Philippe Mathieu-Daudé7893e422023-10-04 11:06:20 +02001359 * In system-mode we will have at most max_cpus TCG threads.
Emilio G. Cota3468b592017-07-19 18:57:58 -04001360 */
1361#ifdef CONFIG_USER_ONLY
Emilio G. Cotadf2cce22017-07-12 18:26:40 -04001362 tcg_ctxs = &tcg_ctx;
Richard Henderson0e2d61c2021-03-09 23:06:32 -06001363 tcg_cur_ctxs = 1;
1364 tcg_max_ctxs = 1;
Emilio G. Cota3468b592017-07-19 18:57:58 -04001365#else
Richard Henderson0e2d61c2021-03-09 23:06:32 -06001366 tcg_max_ctxs = max_cpus;
1367 tcg_ctxs = g_new0(TCGContext *, max_cpus);
Emilio G. Cota3468b592017-07-19 18:57:58 -04001368#endif
Richard Henderson1c2adb92017-10-10 14:34:37 -07001369
1370 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
1371 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
Richard Hendersonad75a512023-09-13 16:37:36 -07001372 tcg_env = temp_tcgv_ptr(ts);
Richard Henderson9002ec72010-05-06 08:50:41 -07001373}
bellardb03cce82008-05-10 10:52:05 +00001374
Richard Henderson43b972b2021-03-09 22:52:45 -06001375void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus)
Richard Hendersona76aabd2021-03-09 17:24:33 -06001376{
Richard Henderson43b972b2021-03-09 22:52:45 -06001377 tcg_context_init(max_cpus);
1378 tcg_region_init(tb_size, splitwx, max_cpus);
Richard Hendersona76aabd2021-03-09 17:24:33 -06001379}
1380
Emilio G. Cota6e3b2bf2017-06-06 19:12:25 -04001381/*
1382 * Allocate TBs right before their corresponding translated code, making
1383 * sure that TBs and code are on different cache lines.
1384 */
1385TranslationBlock *tcg_tb_alloc(TCGContext *s)
1386{
1387 uintptr_t align = qemu_icache_linesize;
1388 TranslationBlock *tb;
1389 void *next;
1390
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001391 retry:
Emilio G. Cota6e3b2bf2017-06-06 19:12:25 -04001392 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
1393 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
1394
1395 if (unlikely(next > s->code_gen_highwater)) {
Emilio G. Cotae8feb962017-07-07 19:24:20 -04001396 if (tcg_region_alloc(s)) {
1397 return NULL;
1398 }
1399 goto retry;
Emilio G. Cota6e3b2bf2017-06-06 19:12:25 -04001400 }
Stefan Hajnoczid73415a2020-09-23 11:56:46 +01001401 qatomic_set(&s->code_gen_ptr, next);
Richard Henderson57a26942017-07-30 13:13:21 -07001402 s->data_gen_ptr = NULL;
Emilio G. Cota6e3b2bf2017-06-06 19:12:25 -04001403 return tb;
1404}
1405
Richard Henderson935f75a2023-09-29 19:35:26 -07001406void tcg_prologue_init(void)
Richard Henderson9002ec72010-05-06 08:50:41 -07001407{
Richard Henderson935f75a2023-09-29 19:35:26 -07001408 TCGContext *s = tcg_ctx;
Richard Hendersonb0a07942021-03-09 08:45:58 -08001409 size_t prologue_size;
Richard Henderson8163b742015-09-18 23:43:05 -07001410
Richard Hendersonb0a07942021-03-09 08:45:58 -08001411 s->code_ptr = s->code_gen_ptr;
1412 s->code_buf = s->code_gen_ptr;
Richard Henderson5b38ee32017-10-25 07:14:20 -07001413 s->data_gen_ptr = NULL;
Richard Hendersonb91ccb32020-10-28 14:11:54 -07001414
1415#ifndef CONFIG_TCG_INTERPRETER
Richard Hendersonb0a07942021-03-09 08:45:58 -08001416 tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr);
Richard Hendersonb91ccb32020-10-28 14:11:54 -07001417#endif
Richard Henderson8163b742015-09-18 23:43:05 -07001418
Richard Henderson5b38ee32017-10-25 07:14:20 -07001419#ifdef TCG_TARGET_NEED_POOL_LABELS
1420 s->pool_labels = NULL;
1421#endif
1422
Roman Bolshakov653b87e2021-01-13 06:28:07 +03001423 qemu_thread_jit_write();
Richard Henderson8163b742015-09-18 23:43:05 -07001424 /* Generate the prologue. */
bellardb03cce82008-05-10 10:52:05 +00001425 tcg_target_qemu_prologue(s);
Richard Henderson5b38ee32017-10-25 07:14:20 -07001426
1427#ifdef TCG_TARGET_NEED_POOL_LABELS
1428 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1429 {
Richard Henderson17689872019-04-21 13:51:56 -07001430 int result = tcg_out_pool_finalize(s);
1431 tcg_debug_assert(result == 0);
Richard Henderson5b38ee32017-10-25 07:14:20 -07001432 }
1433#endif
1434
Richard Hendersonb0a07942021-03-09 08:45:58 -08001435 prologue_size = tcg_current_code_size(s);
Ilya Leoshkevich5584e2d2023-01-12 16:20:13 +01001436 perf_report_prologue(s->code_gen_ptr, prologue_size);
Richard Hendersonb0a07942021-03-09 08:45:58 -08001437
Richard Hendersondf5d2b12020-12-12 09:08:02 -06001438#ifndef CONFIG_TCG_INTERPRETER
Richard Hendersonb0a07942021-03-09 08:45:58 -08001439 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
1440 (uintptr_t)s->code_buf, prologue_size);
Richard Hendersondf5d2b12020-12-12 09:08:02 -06001441#endif
Richard Henderson8163b742015-09-18 23:43:05 -07001442
Richard Hendersond6b64b22013-03-31 13:15:19 -07001443 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
Richard Hendersonc60f5992022-04-17 11:29:47 -07001444 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -07001445 if (logfile) {
1446 fprintf(logfile, "PROLOGUE: [size=%zu]\n", prologue_size);
1447 if (s->data_gen_ptr) {
1448 size_t code_size = s->data_gen_ptr - s->code_gen_ptr;
1449 size_t data_size = prologue_size - code_size;
1450 size_t i;
Richard Henderson5b38ee32017-10-25 07:14:20 -07001451
Richard Henderson78b54852022-04-17 11:29:49 -07001452 disas(logfile, s->code_gen_ptr, code_size);
Richard Henderson5b38ee32017-10-25 07:14:20 -07001453
Richard Henderson78b54852022-04-17 11:29:49 -07001454 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1455 if (sizeof(tcg_target_ulong) == 8) {
1456 fprintf(logfile,
1457 "0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1458 (uintptr_t)s->data_gen_ptr + i,
1459 *(uint64_t *)(s->data_gen_ptr + i));
1460 } else {
1461 fprintf(logfile,
1462 "0x%08" PRIxPTR ": .long 0x%08x\n",
1463 (uintptr_t)s->data_gen_ptr + i,
1464 *(uint32_t *)(s->data_gen_ptr + i));
1465 }
Richard Henderson5b38ee32017-10-25 07:14:20 -07001466 }
Richard Henderson78b54852022-04-17 11:29:49 -07001467 } else {
1468 disas(logfile, s->code_gen_ptr, prologue_size);
Richard Henderson5b38ee32017-10-25 07:14:20 -07001469 }
Richard Henderson78b54852022-04-17 11:29:49 -07001470 fprintf(logfile, "\n");
Richard Henderson78b54852022-04-17 11:29:49 -07001471 qemu_log_unlock(logfile);
Richard Henderson5b38ee32017-10-25 07:14:20 -07001472 }
Richard Hendersond6b64b22013-03-31 13:15:19 -07001473 }
Emilio G. Cotacedbcb02017-04-26 23:29:14 -04001474
Richard Henderson6eea0432021-02-02 09:40:22 -10001475#ifndef CONFIG_TCG_INTERPRETER
1476 /*
1477 * Assert that goto_ptr is implemented completely, setting an epilogue.
1478 * For tci, we use NULL as the signal to return from the interpreter,
1479 * so skip this check.
1480 */
Richard Hendersonf4e01e32021-06-29 14:47:39 -07001481 tcg_debug_assert(tcg_code_gen_epilogue != NULL);
Richard Henderson6eea0432021-02-02 09:40:22 -10001482#endif
Richard Hendersond1c74ab2021-07-09 19:45:42 -07001483
1484 tcg_region_prologue_set(s);
bellardc896fe22008-02-01 10:05:41 +00001485}
1486
bellardc896fe22008-02-01 10:05:41 +00001487void tcg_func_start(TCGContext *s)
1488{
1489 tcg_pool_reset(s);
1490 s->nb_temps = s->nb_globals;
Richard Henderson0ec9eab2013-09-19 12:16:45 -07001491
1492 /* No temps have been previously allocated for size or locality. */
1493 memset(s->free_temps, 0, sizeof(s->free_temps));
1494
Richard Hendersonc0522132020-03-29 18:55:52 -07001495 /* No constant temps have been previously allocated. */
1496 for (int i = 0; i < TCG_TYPE_COUNT; ++i) {
1497 if (s->const_table[i]) {
1498 g_hash_table_remove_all(s->const_table[i]);
1499 }
1500 }
1501
Richard Hendersonabebf922018-05-08 19:18:59 +00001502 s->nb_ops = 0;
bellardc896fe22008-02-01 10:05:41 +00001503 s->nb_labels = 0;
1504 s->current_frame_offset = s->frame_start;
1505
Richard Henderson0a209d42012-09-21 17:18:16 -07001506#ifdef CONFIG_DEBUG_TCG
1507 s->goto_tb_issue_mask = 0;
1508#endif
1509
Richard Henderson15fa08f2017-11-02 15:19:14 +01001510 QTAILQ_INIT(&s->ops);
1511 QTAILQ_INIT(&s->free_ops);
Richard Henderson07843f72024-03-13 13:32:29 -10001512 s->emit_before_op = NULL;
Richard Hendersonbef16ab2019-02-07 13:26:40 +00001513 QSIMPLEQ_INIT(&s->labels);
Richard Henderson4baf3972023-03-09 17:46:16 -08001514
1515 tcg_debug_assert(s->addr_type == TCG_TYPE_I32 ||
1516 s->addr_type == TCG_TYPE_I64);
Richard Hendersond0a9bb52023-03-27 16:07:15 -07001517
Richard Henderson747bd692023-03-31 21:30:31 -07001518 tcg_debug_assert(s->insn_start_words > 0);
bellardc896fe22008-02-01 10:05:41 +00001519}
1520
Richard Hendersonae30e862021-01-23 12:11:17 -10001521static TCGTemp *tcg_temp_alloc(TCGContext *s)
Richard Henderson7ca4b752013-09-19 08:46:21 -07001522{
1523 int n = s->nb_temps++;
Richard Hendersonae30e862021-01-23 12:11:17 -10001524
1525 if (n >= TCG_MAX_TEMPS) {
Richard Hendersondb6b7d02021-01-31 23:29:26 -10001526 tcg_raise_tb_overflow(s);
Richard Hendersonae30e862021-01-23 12:11:17 -10001527 }
Richard Henderson7ca4b752013-09-19 08:46:21 -07001528 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1529}
1530
Richard Hendersonae30e862021-01-23 12:11:17 -10001531static TCGTemp *tcg_global_alloc(TCGContext *s)
Richard Henderson7ca4b752013-09-19 08:46:21 -07001532{
Richard Hendersonfa477d22016-11-02 11:20:15 -06001533 TCGTemp *ts;
1534
Richard Henderson7ca4b752013-09-19 08:46:21 -07001535 tcg_debug_assert(s->nb_globals == s->nb_temps);
Richard Hendersonae30e862021-01-23 12:11:17 -10001536 tcg_debug_assert(s->nb_globals < TCG_MAX_TEMPS);
Richard Henderson7ca4b752013-09-19 08:46:21 -07001537 s->nb_globals++;
Richard Hendersonfa477d22016-11-02 11:20:15 -06001538 ts = tcg_temp_alloc(s);
Richard Hendersonee17db82020-03-29 10:11:56 -07001539 ts->kind = TEMP_GLOBAL;
Richard Hendersonfa477d22016-11-02 11:20:15 -06001540
1541 return ts;
bellardc896fe22008-02-01 10:05:41 +00001542}
1543
Richard Henderson085272b2017-10-20 00:05:45 -07001544static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1545 TCGReg reg, const char *name)
bellardc896fe22008-02-01 10:05:41 +00001546{
bellardc896fe22008-02-01 10:05:41 +00001547 TCGTemp *ts;
bellardc896fe22008-02-01 10:05:41 +00001548
Richard Henderson1a057552023-04-05 12:08:46 -07001549 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
Richard Henderson7ca4b752013-09-19 08:46:21 -07001550
1551 ts = tcg_global_alloc(s);
bellardc896fe22008-02-01 10:05:41 +00001552 ts->base_type = type;
1553 ts->type = type;
Richard Hendersonee17db82020-03-29 10:11:56 -07001554 ts->kind = TEMP_FIXED;
bellardc896fe22008-02-01 10:05:41 +00001555 ts->reg = reg;
bellardc896fe22008-02-01 10:05:41 +00001556 ts->name = name;
bellardc896fe22008-02-01 10:05:41 +00001557 tcg_regset_set_reg(s->reserved_regs, reg);
Richard Henderson7ca4b752013-09-19 08:46:21 -07001558
Richard Henderson085272b2017-10-20 00:05:45 -07001559 return ts;
pbrooka7812ae2008-11-17 14:43:54 +00001560}
1561
Richard Hendersonb6638662013-09-18 14:54:45 -07001562void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
pbrooka7812ae2008-11-17 14:43:54 +00001563{
Richard Hendersonb3a62932013-09-18 14:12:53 -07001564 s->frame_start = start;
1565 s->frame_end = start + size;
Richard Henderson085272b2017-10-20 00:05:45 -07001566 s->frame_temp
1567 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
Richard Hendersonb3a62932013-09-18 14:12:53 -07001568}
pbrooka7812ae2008-11-17 14:43:54 +00001569
Richard Henderson4643f3e2023-10-29 14:08:47 -07001570static TCGTemp *tcg_global_mem_new_internal(TCGv_ptr base, intptr_t offset,
1571 const char *name, TCGType type)
bellardc896fe22008-02-01 10:05:41 +00001572{
Emilio G. Cotab1311c42017-07-12 17:15:52 -04001573 TCGContext *s = tcg_ctx;
Richard Hendersondc41aa72017-10-20 00:30:24 -07001574 TCGTemp *base_ts = tcgv_ptr_temp(base);
Richard Henderson7ca4b752013-09-19 08:46:21 -07001575 TCGTemp *ts = tcg_global_alloc(s);
Richard Hendersonaef85402022-10-19 11:53:27 +10001576 int indirect_reg = 0;
bellardc896fe22008-02-01 10:05:41 +00001577
Richard Hendersonc0522132020-03-29 18:55:52 -07001578 switch (base_ts->kind) {
1579 case TEMP_FIXED:
1580 break;
1581 case TEMP_GLOBAL:
Richard Henderson5a184072016-06-23 20:34:33 -07001582 /* We do not support double-indirect registers. */
1583 tcg_debug_assert(!base_ts->indirect_reg);
Richard Hendersonb3915db2013-09-19 10:36:18 -07001584 base_ts->indirect_base = 1;
Richard Henderson5a184072016-06-23 20:34:33 -07001585 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1586 ? 2 : 1);
1587 indirect_reg = 1;
Richard Hendersonc0522132020-03-29 18:55:52 -07001588 break;
1589 default:
1590 g_assert_not_reached();
Richard Hendersonb3915db2013-09-19 10:36:18 -07001591 }
1592
Richard Henderson7ca4b752013-09-19 08:46:21 -07001593 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1594 TCGTemp *ts2 = tcg_global_alloc(s);
bellardc896fe22008-02-01 10:05:41 +00001595 char buf[64];
Richard Henderson7ca4b752013-09-19 08:46:21 -07001596
1597 ts->base_type = TCG_TYPE_I64;
bellardc896fe22008-02-01 10:05:41 +00001598 ts->type = TCG_TYPE_I32;
Richard Hendersonb3915db2013-09-19 10:36:18 -07001599 ts->indirect_reg = indirect_reg;
bellardc896fe22008-02-01 10:05:41 +00001600 ts->mem_allocated = 1;
Richard Hendersonb3a62932013-09-18 14:12:53 -07001601 ts->mem_base = base_ts;
Richard Hendersonaef85402022-10-19 11:53:27 +10001602 ts->mem_offset = offset;
bellardc896fe22008-02-01 10:05:41 +00001603 pstrcpy(buf, sizeof(buf), name);
1604 pstrcat(buf, sizeof(buf), "_0");
1605 ts->name = strdup(buf);
bellardc896fe22008-02-01 10:05:41 +00001606
Richard Henderson7ca4b752013-09-19 08:46:21 -07001607 tcg_debug_assert(ts2 == ts + 1);
1608 ts2->base_type = TCG_TYPE_I64;
1609 ts2->type = TCG_TYPE_I32;
Richard Hendersonb3915db2013-09-19 10:36:18 -07001610 ts2->indirect_reg = indirect_reg;
Richard Henderson7ca4b752013-09-19 08:46:21 -07001611 ts2->mem_allocated = 1;
1612 ts2->mem_base = base_ts;
Richard Hendersonaef85402022-10-19 11:53:27 +10001613 ts2->mem_offset = offset + 4;
Richard Hendersonfac87bd2022-10-19 11:26:37 +10001614 ts2->temp_subindex = 1;
bellardc896fe22008-02-01 10:05:41 +00001615 pstrcpy(buf, sizeof(buf), name);
1616 pstrcat(buf, sizeof(buf), "_1");
Richard Henderson120c1082016-06-17 17:02:20 -07001617 ts2->name = strdup(buf);
Richard Henderson7ca4b752013-09-19 08:46:21 -07001618 } else {
bellardc896fe22008-02-01 10:05:41 +00001619 ts->base_type = type;
1620 ts->type = type;
Richard Hendersonb3915db2013-09-19 10:36:18 -07001621 ts->indirect_reg = indirect_reg;
bellardc896fe22008-02-01 10:05:41 +00001622 ts->mem_allocated = 1;
Richard Hendersonb3a62932013-09-18 14:12:53 -07001623 ts->mem_base = base_ts;
bellardc896fe22008-02-01 10:05:41 +00001624 ts->mem_offset = offset;
bellardc896fe22008-02-01 10:05:41 +00001625 ts->name = name;
bellardc896fe22008-02-01 10:05:41 +00001626 }
Richard Henderson085272b2017-10-20 00:05:45 -07001627 return ts;
bellardc896fe22008-02-01 10:05:41 +00001628}
1629
Richard Henderson4643f3e2023-10-29 14:08:47 -07001630TCGv_i32 tcg_global_mem_new_i32(TCGv_ptr reg, intptr_t off, const char *name)
1631{
1632 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I32);
1633 return temp_tcgv_i32(ts);
1634}
1635
1636TCGv_i64 tcg_global_mem_new_i64(TCGv_ptr reg, intptr_t off, const char *name)
1637{
1638 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_I64);
1639 return temp_tcgv_i64(ts);
1640}
1641
1642TCGv_ptr tcg_global_mem_new_ptr(TCGv_ptr reg, intptr_t off, const char *name)
1643{
1644 TCGTemp *ts = tcg_global_mem_new_internal(reg, off, name, TCG_TYPE_PTR);
1645 return temp_tcgv_ptr(ts);
1646}
1647
Richard Hendersonfb04ab72024-01-10 18:21:58 +11001648TCGTemp *tcg_temp_new_internal(TCGType type, TCGTempKind kind)
bellardc896fe22008-02-01 10:05:41 +00001649{
Emilio G. Cotab1311c42017-07-12 17:15:52 -04001650 TCGContext *s = tcg_ctx;
bellardc896fe22008-02-01 10:05:41 +00001651 TCGTemp *ts;
Richard Hendersone1c08b02023-01-29 14:02:59 -10001652 int n;
bellardc896fe22008-02-01 10:05:41 +00001653
Richard Hendersone1c08b02023-01-29 14:02:59 -10001654 if (kind == TEMP_EBB) {
1655 int idx = find_first_bit(s->free_temps[type].l, TCG_MAX_TEMPS);
Richard Henderson0ec9eab2013-09-19 12:16:45 -07001656
Richard Hendersone1c08b02023-01-29 14:02:59 -10001657 if (idx < TCG_MAX_TEMPS) {
1658 /* There is already an available temp with the right type. */
1659 clear_bit(idx, s->free_temps[type].l);
Richard Henderson43eef722022-10-20 08:03:41 +10001660
Richard Hendersone1c08b02023-01-29 14:02:59 -10001661 ts = &s->temps[idx];
1662 ts->temp_allocated = 1;
1663 tcg_debug_assert(ts->base_type == type);
1664 tcg_debug_assert(ts->kind == kind);
Richard Henderson2f2e9112023-02-24 16:15:18 -10001665 return ts;
Richard Henderson43eef722022-10-20 08:03:41 +10001666 }
Richard Hendersone1c08b02023-01-29 14:02:59 -10001667 } else {
1668 tcg_debug_assert(kind == TEMP_TB);
1669 }
Richard Henderson43eef722022-10-20 08:03:41 +10001670
Richard Hendersone1c08b02023-01-29 14:02:59 -10001671 switch (type) {
1672 case TCG_TYPE_I32:
1673 case TCG_TYPE_V64:
1674 case TCG_TYPE_V128:
1675 case TCG_TYPE_V256:
1676 n = 1;
1677 break;
1678 case TCG_TYPE_I64:
1679 n = 64 / TCG_TARGET_REG_BITS;
1680 break;
1681 case TCG_TYPE_I128:
1682 n = 128 / TCG_TARGET_REG_BITS;
1683 break;
1684 default:
1685 g_assert_not_reached();
1686 }
Richard Henderson7ca4b752013-09-19 08:46:21 -07001687
Richard Hendersone1c08b02023-01-29 14:02:59 -10001688 ts = tcg_temp_alloc(s);
1689 ts->base_type = type;
1690 ts->temp_allocated = 1;
1691 ts->kind = kind;
Richard Henderson43eef722022-10-20 08:03:41 +10001692
Richard Hendersone1c08b02023-01-29 14:02:59 -10001693 if (n == 1) {
1694 ts->type = type;
1695 } else {
1696 ts->type = TCG_TYPE_REG;
Richard Henderson43eef722022-10-20 08:03:41 +10001697
Richard Hendersone1c08b02023-01-29 14:02:59 -10001698 for (int i = 1; i < n; ++i) {
1699 TCGTemp *ts2 = tcg_temp_alloc(s);
1700
1701 tcg_debug_assert(ts2 == ts + i);
1702 ts2->base_type = type;
1703 ts2->type = TCG_TYPE_REG;
1704 ts2->temp_allocated = 1;
1705 ts2->temp_subindex = i;
1706 ts2->kind = kind;
bellarde8996ee2008-05-23 17:33:39 +00001707 }
bellardc896fe22008-02-01 10:05:41 +00001708 }
Richard Henderson085272b2017-10-20 00:05:45 -07001709 return ts;
bellardc896fe22008-02-01 10:05:41 +00001710}
1711
Richard Henderson4643f3e2023-10-29 14:08:47 -07001712TCGv_i32 tcg_temp_new_i32(void)
1713{
1714 return temp_tcgv_i32(tcg_temp_new_internal(TCG_TYPE_I32, TEMP_TB));
1715}
1716
1717TCGv_i32 tcg_temp_ebb_new_i32(void)
1718{
1719 return temp_tcgv_i32(tcg_temp_new_internal(TCG_TYPE_I32, TEMP_EBB));
1720}
1721
1722TCGv_i64 tcg_temp_new_i64(void)
1723{
1724 return temp_tcgv_i64(tcg_temp_new_internal(TCG_TYPE_I64, TEMP_TB));
1725}
1726
1727TCGv_i64 tcg_temp_ebb_new_i64(void)
1728{
1729 return temp_tcgv_i64(tcg_temp_new_internal(TCG_TYPE_I64, TEMP_EBB));
1730}
1731
1732TCGv_ptr tcg_temp_new_ptr(void)
1733{
1734 return temp_tcgv_ptr(tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_TB));
1735}
1736
1737TCGv_ptr tcg_temp_ebb_new_ptr(void)
1738{
1739 return temp_tcgv_ptr(tcg_temp_new_internal(TCG_TYPE_PTR, TEMP_EBB));
1740}
1741
1742TCGv_i128 tcg_temp_new_i128(void)
1743{
1744 return temp_tcgv_i128(tcg_temp_new_internal(TCG_TYPE_I128, TEMP_TB));
1745}
1746
1747TCGv_i128 tcg_temp_ebb_new_i128(void)
1748{
1749 return temp_tcgv_i128(tcg_temp_new_internal(TCG_TYPE_I128, TEMP_EBB));
1750}
1751
Richard Hendersond2fd7452017-09-14 13:53:46 -07001752TCGv_vec tcg_temp_new_vec(TCGType type)
1753{
1754 TCGTemp *t;
1755
1756#ifdef CONFIG_DEBUG_TCG
1757 switch (type) {
1758 case TCG_TYPE_V64:
1759 assert(TCG_TARGET_HAS_v64);
1760 break;
1761 case TCG_TYPE_V128:
1762 assert(TCG_TARGET_HAS_v128);
1763 break;
1764 case TCG_TYPE_V256:
1765 assert(TCG_TARGET_HAS_v256);
1766 break;
1767 default:
1768 g_assert_not_reached();
1769 }
1770#endif
1771
Richard Hendersonbbf989b2023-01-29 13:46:06 -10001772 t = tcg_temp_new_internal(type, TEMP_EBB);
Richard Hendersond2fd7452017-09-14 13:53:46 -07001773 return temp_tcgv_vec(t);
1774}
1775
1776/* Create a new temp of the same type as an existing temp. */
1777TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1778{
1779 TCGTemp *t = tcgv_vec_temp(match);
1780
1781 tcg_debug_assert(t->temp_allocated != 0);
1782
Richard Hendersonbbf989b2023-01-29 13:46:06 -10001783 t = tcg_temp_new_internal(t->base_type, TEMP_EBB);
Richard Hendersond2fd7452017-09-14 13:53:46 -07001784 return temp_tcgv_vec(t);
1785}
1786
Richard Henderson5bfa8032018-02-22 18:17:57 -08001787void tcg_temp_free_internal(TCGTemp *ts)
bellardc896fe22008-02-01 10:05:41 +00001788{
Emilio G. Cotab1311c42017-07-12 17:15:52 -04001789 TCGContext *s = tcg_ctx;
bellardc896fe22008-02-01 10:05:41 +00001790
Richard Hendersonc7482432022-03-16 09:34:18 -07001791 switch (ts->kind) {
1792 case TEMP_CONST:
Richard Hendersonf57c6912023-01-29 10:55:52 -10001793 case TEMP_TB:
Richard Henderson2f2e9112023-02-24 16:15:18 -10001794 /* Silently ignore free. */
1795 break;
1796 case TEMP_EBB:
1797 tcg_debug_assert(ts->temp_allocated != 0);
1798 ts->temp_allocated = 0;
1799 set_bit(temp_idx(ts), s->free_temps[ts->base_type].l);
Richard Hendersonc7482432022-03-16 09:34:18 -07001800 break;
1801 default:
Richard Henderson2f2e9112023-02-24 16:15:18 -10001802 /* It never made sense to free TEMP_FIXED or TEMP_GLOBAL. */
Richard Hendersonc7482432022-03-16 09:34:18 -07001803 g_assert_not_reached();
Richard Hendersonc0522132020-03-29 18:55:52 -07001804 }
bellarde8996ee2008-05-23 17:33:39 +00001805}
1806
Richard Henderson58b79712023-10-29 14:08:48 -07001807void tcg_temp_free_i32(TCGv_i32 arg)
1808{
1809 tcg_temp_free_internal(tcgv_i32_temp(arg));
1810}
1811
1812void tcg_temp_free_i64(TCGv_i64 arg)
1813{
1814 tcg_temp_free_internal(tcgv_i64_temp(arg));
1815}
1816
1817void tcg_temp_free_i128(TCGv_i128 arg)
1818{
1819 tcg_temp_free_internal(tcgv_i128_temp(arg));
1820}
1821
1822void tcg_temp_free_ptr(TCGv_ptr arg)
1823{
1824 tcg_temp_free_internal(tcgv_ptr_temp(arg));
1825}
1826
1827void tcg_temp_free_vec(TCGv_vec arg)
1828{
1829 tcg_temp_free_internal(tcgv_vec_temp(arg));
1830}
1831
Richard Hendersonc0522132020-03-29 18:55:52 -07001832TCGTemp *tcg_constant_internal(TCGType type, int64_t val)
1833{
1834 TCGContext *s = tcg_ctx;
1835 GHashTable *h = s->const_table[type];
1836 TCGTemp *ts;
1837
1838 if (h == NULL) {
1839 h = g_hash_table_new(g_int64_hash, g_int64_equal);
1840 s->const_table[type] = h;
1841 }
1842
1843 ts = g_hash_table_lookup(h, &val);
1844 if (ts == NULL) {
Richard Hendersonaef85402022-10-19 11:53:27 +10001845 int64_t *val_ptr;
1846
Richard Hendersonc0522132020-03-29 18:55:52 -07001847 ts = tcg_temp_alloc(s);
1848
1849 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1850 TCGTemp *ts2 = tcg_temp_alloc(s);
1851
Richard Hendersonaef85402022-10-19 11:53:27 +10001852 tcg_debug_assert(ts2 == ts + 1);
1853
Richard Hendersonc0522132020-03-29 18:55:52 -07001854 ts->base_type = TCG_TYPE_I64;
1855 ts->type = TCG_TYPE_I32;
1856 ts->kind = TEMP_CONST;
1857 ts->temp_allocated = 1;
Richard Hendersonc0522132020-03-29 18:55:52 -07001858
Richard Hendersonc0522132020-03-29 18:55:52 -07001859 ts2->base_type = TCG_TYPE_I64;
1860 ts2->type = TCG_TYPE_I32;
1861 ts2->kind = TEMP_CONST;
1862 ts2->temp_allocated = 1;
Richard Hendersonfac87bd2022-10-19 11:26:37 +10001863 ts2->temp_subindex = 1;
Richard Hendersonaef85402022-10-19 11:53:27 +10001864
1865 /*
1866 * Retain the full value of the 64-bit constant in the low
1867 * part, so that the hash table works. Actual uses will
1868 * truncate the value to the low part.
1869 */
1870 ts[HOST_BIG_ENDIAN].val = val;
1871 ts[!HOST_BIG_ENDIAN].val = val >> 32;
1872 val_ptr = &ts[HOST_BIG_ENDIAN].val;
Richard Hendersonc0522132020-03-29 18:55:52 -07001873 } else {
1874 ts->base_type = type;
1875 ts->type = type;
1876 ts->kind = TEMP_CONST;
1877 ts->temp_allocated = 1;
1878 ts->val = val;
Richard Hendersonaef85402022-10-19 11:53:27 +10001879 val_ptr = &ts->val;
Richard Hendersonc0522132020-03-29 18:55:52 -07001880 }
Richard Hendersonaef85402022-10-19 11:53:27 +10001881 g_hash_table_insert(h, val_ptr, ts);
Richard Hendersonc0522132020-03-29 18:55:52 -07001882 }
1883
1884 return ts;
1885}
1886
Richard Henderson16edaee2023-10-29 14:08:46 -07001887TCGv_i32 tcg_constant_i32(int32_t val)
1888{
1889 return temp_tcgv_i32(tcg_constant_internal(TCG_TYPE_I32, val));
1890}
1891
1892TCGv_i64 tcg_constant_i64(int64_t val)
1893{
1894 return temp_tcgv_i64(tcg_constant_internal(TCG_TYPE_I64, val));
1895}
1896
1897TCGv_ptr tcg_constant_ptr_int(intptr_t val)
1898{
1899 return temp_tcgv_ptr(tcg_constant_internal(TCG_TYPE_PTR, val));
1900}
1901
Richard Hendersonc0522132020-03-29 18:55:52 -07001902TCGv_vec tcg_constant_vec(TCGType type, unsigned vece, int64_t val)
1903{
1904 val = dup_const(vece, val);
1905 return temp_tcgv_vec(tcg_constant_internal(type, val));
1906}
1907
Richard Henderson88d40052020-09-03 18:18:08 -07001908TCGv_vec tcg_constant_vec_matching(TCGv_vec match, unsigned vece, int64_t val)
1909{
1910 TCGTemp *t = tcgv_vec_temp(match);
1911
1912 tcg_debug_assert(t->temp_allocated != 0);
1913 return tcg_constant_vec(t->base_type, vece, val);
1914}
1915
Richard Henderson177f6482023-03-30 08:09:03 -07001916#ifdef CONFIG_DEBUG_TCG
1917size_t temp_idx(TCGTemp *ts)
1918{
1919 ptrdiff_t n = ts - tcg_ctx->temps;
1920 assert(n >= 0 && n < tcg_ctx->nb_temps);
1921 return n;
1922}
1923
1924TCGTemp *tcgv_i32_temp(TCGv_i32 v)
1925{
1926 uintptr_t o = (uintptr_t)v - offsetof(TCGContext, temps);
1927
1928 assert(o < sizeof(TCGTemp) * tcg_ctx->nb_temps);
1929 assert(o % sizeof(TCGTemp) == 0);
1930
1931 return (void *)tcg_ctx + (uintptr_t)v;
1932}
1933#endif /* CONFIG_DEBUG_TCG */
1934
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001935/* Return true if OP may appear in the opcode stream.
1936 Test the runtime variable that controls each opcode. */
1937bool tcg_op_supported(TCGOpcode op)
1938{
Richard Hendersond2fd7452017-09-14 13:53:46 -07001939 const bool have_vec
1940 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1941
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001942 switch (op) {
1943 case INDEX_op_discard:
1944 case INDEX_op_set_label:
1945 case INDEX_op_call:
1946 case INDEX_op_br:
1947 case INDEX_op_mb:
1948 case INDEX_op_insn_start:
1949 case INDEX_op_exit_tb:
1950 case INDEX_op_goto_tb:
Richard Hendersonf4e01e32021-06-29 14:47:39 -07001951 case INDEX_op_goto_ptr:
Richard Hendersonfecccfc2023-05-16 20:07:20 -07001952 case INDEX_op_qemu_ld_a32_i32:
1953 case INDEX_op_qemu_ld_a64_i32:
1954 case INDEX_op_qemu_st_a32_i32:
1955 case INDEX_op_qemu_st_a64_i32:
1956 case INDEX_op_qemu_ld_a32_i64:
1957 case INDEX_op_qemu_ld_a64_i64:
1958 case INDEX_op_qemu_st_a32_i64:
1959 case INDEX_op_qemu_st_a64_i64:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001960 return true;
1961
Richard Hendersonfecccfc2023-05-16 20:07:20 -07001962 case INDEX_op_qemu_st8_a32_i32:
1963 case INDEX_op_qemu_st8_a64_i32:
Richard Henderson07ce0b02020-12-09 13:58:39 -06001964 return TCG_TARGET_HAS_qemu_st8_i32;
1965
Richard Hendersonfecccfc2023-05-16 20:07:20 -07001966 case INDEX_op_qemu_ld_a32_i128:
1967 case INDEX_op_qemu_ld_a64_i128:
1968 case INDEX_op_qemu_st_a32_i128:
1969 case INDEX_op_qemu_st_a64_i128:
Richard Henderson12fde9b2022-11-07 10:42:56 +11001970 return TCG_TARGET_HAS_qemu_ldst_i128;
1971
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001972 case INDEX_op_mov_i32:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001973 case INDEX_op_setcond_i32:
1974 case INDEX_op_brcond_i32:
Richard Henderson3871be72023-10-25 21:14:01 -07001975 case INDEX_op_movcond_i32:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001976 case INDEX_op_ld8u_i32:
1977 case INDEX_op_ld8s_i32:
1978 case INDEX_op_ld16u_i32:
1979 case INDEX_op_ld16s_i32:
1980 case INDEX_op_ld_i32:
1981 case INDEX_op_st8_i32:
1982 case INDEX_op_st16_i32:
1983 case INDEX_op_st_i32:
1984 case INDEX_op_add_i32:
1985 case INDEX_op_sub_i32:
Richard Hendersonb701f192023-10-25 21:14:04 -07001986 case INDEX_op_neg_i32:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001987 case INDEX_op_mul_i32:
1988 case INDEX_op_and_i32:
1989 case INDEX_op_or_i32:
1990 case INDEX_op_xor_i32:
1991 case INDEX_op_shl_i32:
1992 case INDEX_op_shr_i32:
1993 case INDEX_op_sar_i32:
1994 return true;
1995
Richard Henderson36355022023-08-04 23:24:04 +00001996 case INDEX_op_negsetcond_i32:
1997 return TCG_TARGET_HAS_negsetcond_i32;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07001998 case INDEX_op_div_i32:
1999 case INDEX_op_divu_i32:
2000 return TCG_TARGET_HAS_div_i32;
2001 case INDEX_op_rem_i32:
2002 case INDEX_op_remu_i32:
2003 return TCG_TARGET_HAS_rem_i32;
2004 case INDEX_op_div2_i32:
2005 case INDEX_op_divu2_i32:
2006 return TCG_TARGET_HAS_div2_i32;
2007 case INDEX_op_rotl_i32:
2008 case INDEX_op_rotr_i32:
2009 return TCG_TARGET_HAS_rot_i32;
2010 case INDEX_op_deposit_i32:
2011 return TCG_TARGET_HAS_deposit_i32;
2012 case INDEX_op_extract_i32:
2013 return TCG_TARGET_HAS_extract_i32;
2014 case INDEX_op_sextract_i32:
2015 return TCG_TARGET_HAS_sextract_i32;
Richard Hendersonfce12962019-02-25 10:29:25 -08002016 case INDEX_op_extract2_i32:
2017 return TCG_TARGET_HAS_extract2_i32;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002018 case INDEX_op_add2_i32:
2019 return TCG_TARGET_HAS_add2_i32;
2020 case INDEX_op_sub2_i32:
2021 return TCG_TARGET_HAS_sub2_i32;
2022 case INDEX_op_mulu2_i32:
2023 return TCG_TARGET_HAS_mulu2_i32;
2024 case INDEX_op_muls2_i32:
2025 return TCG_TARGET_HAS_muls2_i32;
2026 case INDEX_op_muluh_i32:
2027 return TCG_TARGET_HAS_muluh_i32;
2028 case INDEX_op_mulsh_i32:
2029 return TCG_TARGET_HAS_mulsh_i32;
2030 case INDEX_op_ext8s_i32:
2031 return TCG_TARGET_HAS_ext8s_i32;
2032 case INDEX_op_ext16s_i32:
2033 return TCG_TARGET_HAS_ext16s_i32;
2034 case INDEX_op_ext8u_i32:
2035 return TCG_TARGET_HAS_ext8u_i32;
2036 case INDEX_op_ext16u_i32:
2037 return TCG_TARGET_HAS_ext16u_i32;
2038 case INDEX_op_bswap16_i32:
2039 return TCG_TARGET_HAS_bswap16_i32;
2040 case INDEX_op_bswap32_i32:
2041 return TCG_TARGET_HAS_bswap32_i32;
2042 case INDEX_op_not_i32:
2043 return TCG_TARGET_HAS_not_i32;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002044 case INDEX_op_andc_i32:
2045 return TCG_TARGET_HAS_andc_i32;
2046 case INDEX_op_orc_i32:
2047 return TCG_TARGET_HAS_orc_i32;
2048 case INDEX_op_eqv_i32:
2049 return TCG_TARGET_HAS_eqv_i32;
2050 case INDEX_op_nand_i32:
2051 return TCG_TARGET_HAS_nand_i32;
2052 case INDEX_op_nor_i32:
2053 return TCG_TARGET_HAS_nor_i32;
2054 case INDEX_op_clz_i32:
2055 return TCG_TARGET_HAS_clz_i32;
2056 case INDEX_op_ctz_i32:
2057 return TCG_TARGET_HAS_ctz_i32;
2058 case INDEX_op_ctpop_i32:
2059 return TCG_TARGET_HAS_ctpop_i32;
2060
2061 case INDEX_op_brcond2_i32:
2062 case INDEX_op_setcond2_i32:
2063 return TCG_TARGET_REG_BITS == 32;
2064
2065 case INDEX_op_mov_i64:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002066 case INDEX_op_setcond_i64:
2067 case INDEX_op_brcond_i64:
Richard Henderson3871be72023-10-25 21:14:01 -07002068 case INDEX_op_movcond_i64:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002069 case INDEX_op_ld8u_i64:
2070 case INDEX_op_ld8s_i64:
2071 case INDEX_op_ld16u_i64:
2072 case INDEX_op_ld16s_i64:
2073 case INDEX_op_ld32u_i64:
2074 case INDEX_op_ld32s_i64:
2075 case INDEX_op_ld_i64:
2076 case INDEX_op_st8_i64:
2077 case INDEX_op_st16_i64:
2078 case INDEX_op_st32_i64:
2079 case INDEX_op_st_i64:
2080 case INDEX_op_add_i64:
2081 case INDEX_op_sub_i64:
Richard Hendersonb701f192023-10-25 21:14:04 -07002082 case INDEX_op_neg_i64:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002083 case INDEX_op_mul_i64:
2084 case INDEX_op_and_i64:
2085 case INDEX_op_or_i64:
2086 case INDEX_op_xor_i64:
2087 case INDEX_op_shl_i64:
2088 case INDEX_op_shr_i64:
2089 case INDEX_op_sar_i64:
2090 case INDEX_op_ext_i32_i64:
2091 case INDEX_op_extu_i32_i64:
2092 return TCG_TARGET_REG_BITS == 64;
2093
Richard Henderson36355022023-08-04 23:24:04 +00002094 case INDEX_op_negsetcond_i64:
2095 return TCG_TARGET_HAS_negsetcond_i64;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002096 case INDEX_op_div_i64:
2097 case INDEX_op_divu_i64:
2098 return TCG_TARGET_HAS_div_i64;
2099 case INDEX_op_rem_i64:
2100 case INDEX_op_remu_i64:
2101 return TCG_TARGET_HAS_rem_i64;
2102 case INDEX_op_div2_i64:
2103 case INDEX_op_divu2_i64:
2104 return TCG_TARGET_HAS_div2_i64;
2105 case INDEX_op_rotl_i64:
2106 case INDEX_op_rotr_i64:
2107 return TCG_TARGET_HAS_rot_i64;
2108 case INDEX_op_deposit_i64:
2109 return TCG_TARGET_HAS_deposit_i64;
2110 case INDEX_op_extract_i64:
2111 return TCG_TARGET_HAS_extract_i64;
2112 case INDEX_op_sextract_i64:
2113 return TCG_TARGET_HAS_sextract_i64;
Richard Hendersonfce12962019-02-25 10:29:25 -08002114 case INDEX_op_extract2_i64:
2115 return TCG_TARGET_HAS_extract2_i64;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002116 case INDEX_op_extrl_i64_i32:
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002117 case INDEX_op_extrh_i64_i32:
Richard Henderson13d885b2023-08-22 10:51:10 -07002118 return TCG_TARGET_HAS_extr_i64_i32;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002119 case INDEX_op_ext8s_i64:
2120 return TCG_TARGET_HAS_ext8s_i64;
2121 case INDEX_op_ext16s_i64:
2122 return TCG_TARGET_HAS_ext16s_i64;
2123 case INDEX_op_ext32s_i64:
2124 return TCG_TARGET_HAS_ext32s_i64;
2125 case INDEX_op_ext8u_i64:
2126 return TCG_TARGET_HAS_ext8u_i64;
2127 case INDEX_op_ext16u_i64:
2128 return TCG_TARGET_HAS_ext16u_i64;
2129 case INDEX_op_ext32u_i64:
2130 return TCG_TARGET_HAS_ext32u_i64;
2131 case INDEX_op_bswap16_i64:
2132 return TCG_TARGET_HAS_bswap16_i64;
2133 case INDEX_op_bswap32_i64:
2134 return TCG_TARGET_HAS_bswap32_i64;
2135 case INDEX_op_bswap64_i64:
2136 return TCG_TARGET_HAS_bswap64_i64;
2137 case INDEX_op_not_i64:
2138 return TCG_TARGET_HAS_not_i64;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002139 case INDEX_op_andc_i64:
2140 return TCG_TARGET_HAS_andc_i64;
2141 case INDEX_op_orc_i64:
2142 return TCG_TARGET_HAS_orc_i64;
2143 case INDEX_op_eqv_i64:
2144 return TCG_TARGET_HAS_eqv_i64;
2145 case INDEX_op_nand_i64:
2146 return TCG_TARGET_HAS_nand_i64;
2147 case INDEX_op_nor_i64:
2148 return TCG_TARGET_HAS_nor_i64;
2149 case INDEX_op_clz_i64:
2150 return TCG_TARGET_HAS_clz_i64;
2151 case INDEX_op_ctz_i64:
2152 return TCG_TARGET_HAS_ctz_i64;
2153 case INDEX_op_ctpop_i64:
2154 return TCG_TARGET_HAS_ctpop_i64;
2155 case INDEX_op_add2_i64:
2156 return TCG_TARGET_HAS_add2_i64;
2157 case INDEX_op_sub2_i64:
2158 return TCG_TARGET_HAS_sub2_i64;
2159 case INDEX_op_mulu2_i64:
2160 return TCG_TARGET_HAS_mulu2_i64;
2161 case INDEX_op_muls2_i64:
2162 return TCG_TARGET_HAS_muls2_i64;
2163 case INDEX_op_muluh_i64:
2164 return TCG_TARGET_HAS_muluh_i64;
2165 case INDEX_op_mulsh_i64:
2166 return TCG_TARGET_HAS_mulsh_i64;
2167
Richard Hendersond2fd7452017-09-14 13:53:46 -07002168 case INDEX_op_mov_vec:
2169 case INDEX_op_dup_vec:
Richard Henderson37ee55a2019-03-17 01:55:22 +00002170 case INDEX_op_dupm_vec:
Richard Hendersond2fd7452017-09-14 13:53:46 -07002171 case INDEX_op_ld_vec:
2172 case INDEX_op_st_vec:
2173 case INDEX_op_add_vec:
2174 case INDEX_op_sub_vec:
2175 case INDEX_op_and_vec:
2176 case INDEX_op_or_vec:
2177 case INDEX_op_xor_vec:
Richard Henderson212be172017-11-17 20:47:42 +01002178 case INDEX_op_cmp_vec:
Richard Hendersond2fd7452017-09-14 13:53:46 -07002179 return have_vec;
2180 case INDEX_op_dup2_vec:
2181 return have_vec && TCG_TARGET_REG_BITS == 32;
2182 case INDEX_op_not_vec:
2183 return have_vec && TCG_TARGET_HAS_not_vec;
2184 case INDEX_op_neg_vec:
2185 return have_vec && TCG_TARGET_HAS_neg_vec;
Richard Hendersonbcefc902019-04-17 13:53:02 -10002186 case INDEX_op_abs_vec:
2187 return have_vec && TCG_TARGET_HAS_abs_vec;
Richard Hendersond2fd7452017-09-14 13:53:46 -07002188 case INDEX_op_andc_vec:
2189 return have_vec && TCG_TARGET_HAS_andc_vec;
2190 case INDEX_op_orc_vec:
2191 return have_vec && TCG_TARGET_HAS_orc_vec;
Richard Hendersoned523472021-12-16 11:17:46 -08002192 case INDEX_op_nand_vec:
2193 return have_vec && TCG_TARGET_HAS_nand_vec;
2194 case INDEX_op_nor_vec:
2195 return have_vec && TCG_TARGET_HAS_nor_vec;
2196 case INDEX_op_eqv_vec:
2197 return have_vec && TCG_TARGET_HAS_eqv_vec;
Richard Henderson37740302017-11-21 10:11:14 +01002198 case INDEX_op_mul_vec:
2199 return have_vec && TCG_TARGET_HAS_mul_vec;
Richard Hendersond0ec9792017-11-17 14:35:11 +01002200 case INDEX_op_shli_vec:
2201 case INDEX_op_shri_vec:
2202 case INDEX_op_sari_vec:
2203 return have_vec && TCG_TARGET_HAS_shi_vec;
2204 case INDEX_op_shls_vec:
2205 case INDEX_op_shrs_vec:
2206 case INDEX_op_sars_vec:
2207 return have_vec && TCG_TARGET_HAS_shs_vec;
2208 case INDEX_op_shlv_vec:
2209 case INDEX_op_shrv_vec:
2210 case INDEX_op_sarv_vec:
2211 return have_vec && TCG_TARGET_HAS_shv_vec;
Richard Hendersonb0f7e742020-04-19 18:01:52 -07002212 case INDEX_op_rotli_vec:
2213 return have_vec && TCG_TARGET_HAS_roti_vec;
Richard Henderson23850a72020-04-20 08:22:44 -07002214 case INDEX_op_rotls_vec:
2215 return have_vec && TCG_TARGET_HAS_rots_vec;
Richard Henderson5d0ceda2020-04-19 19:47:59 -07002216 case INDEX_op_rotlv_vec:
2217 case INDEX_op_rotrv_vec:
2218 return have_vec && TCG_TARGET_HAS_rotv_vec;
Richard Henderson8afaf052018-12-17 18:01:47 -08002219 case INDEX_op_ssadd_vec:
2220 case INDEX_op_usadd_vec:
2221 case INDEX_op_sssub_vec:
2222 case INDEX_op_ussub_vec:
2223 return have_vec && TCG_TARGET_HAS_sat_vec;
Richard Hendersondd0a0fc2018-12-17 19:35:46 -08002224 case INDEX_op_smin_vec:
2225 case INDEX_op_umin_vec:
2226 case INDEX_op_smax_vec:
2227 case INDEX_op_umax_vec:
2228 return have_vec && TCG_TARGET_HAS_minmax_vec;
Richard Henderson38dc1292019-04-30 11:02:23 -07002229 case INDEX_op_bitsel_vec:
2230 return have_vec && TCG_TARGET_HAS_bitsel_vec;
Richard Hendersonf75da292019-04-30 13:01:12 -07002231 case INDEX_op_cmpsel_vec:
2232 return have_vec && TCG_TARGET_HAS_cmpsel_vec;
Richard Hendersond2fd7452017-09-14 13:53:46 -07002233
Richard Hendersondb432672017-09-15 14:11:45 -07002234 default:
2235 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
2236 return true;
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002237 }
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07002238}
2239
Richard Henderson39004a72022-11-11 10:09:37 +10002240static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs);
2241
Richard Henderson83a0ad22024-03-14 13:48:41 -10002242static void tcg_gen_callN(void *func, TCGHelperInfo *info,
2243 TCGTemp *ret, TCGTemp **args)
bellardc896fe22008-02-01 10:05:41 +00002244{
Richard Henderson39004a72022-11-11 10:09:37 +10002245 TCGv_i64 extend_free[MAX_CALL_IARGS];
2246 int n_extend = 0;
Richard Henderson75e8b9b2016-12-08 10:52:57 -08002247 TCGOp *op;
Richard Henderson39004a72022-11-11 10:09:37 +10002248 int i, n, pi = 0, total_args;
Richard Hendersonafb49892014-04-07 15:10:05 -07002249
Richard Hendersond53106c2023-03-31 10:37:04 -07002250 if (unlikely(g_once_init_enter(HELPER_INFO_INIT(info)))) {
2251 init_call_layout(info);
2252 g_once_init_leave(HELPER_INFO_INIT(info), HELPER_INFO_INIT_VAL(info));
2253 }
2254
Richard Henderson39004a72022-11-11 10:09:37 +10002255 total_args = info->nr_out + info->nr_in + 2;
2256 op = tcg_op_alloc(INDEX_op_call, total_args);
Richard Henderson2bece2c2010-06-14 17:35:27 -07002257
Emilio G. Cota38b47b12018-12-07 15:33:56 -05002258#ifdef CONFIG_PLUGIN
Emilio Cota17083f62023-01-24 18:01:25 +00002259 /* Flag helpers that may affect guest state */
Richard Hendersonb0748972024-03-14 22:07:07 -10002260 if (tcg_ctx->plugin_insn && !(info->flags & TCG_CALL_NO_SIDE_EFFECTS)) {
Emilio G. Cota38b47b12018-12-07 15:33:56 -05002261 tcg_ctx->plugin_insn->calls_helpers = true;
2262 }
2263#endif
2264
Richard Henderson39004a72022-11-11 10:09:37 +10002265 TCGOP_CALLO(op) = n = info->nr_out;
2266 switch (n) {
2267 case 0:
2268 tcg_debug_assert(ret == NULL);
2269 break;
2270 case 1:
2271 tcg_debug_assert(ret != NULL);
2272 op->args[pi++] = temp_arg(ret);
2273 break;
2274 case 2:
Richard Henderson466d3752022-11-11 11:01:13 +10002275 case 4:
Richard Henderson39004a72022-11-11 10:09:37 +10002276 tcg_debug_assert(ret != NULL);
Richard Henderson466d3752022-11-11 11:01:13 +10002277 tcg_debug_assert(ret->base_type == ret->type + ctz32(n));
Richard Henderson39004a72022-11-11 10:09:37 +10002278 tcg_debug_assert(ret->temp_subindex == 0);
Richard Henderson466d3752022-11-11 11:01:13 +10002279 for (i = 0; i < n; ++i) {
2280 op->args[pi++] = temp_arg(ret + i);
2281 }
Richard Henderson39004a72022-11-11 10:09:37 +10002282 break;
2283 default:
2284 g_assert_not_reached();
2285 }
Richard Henderson7319d832021-03-18 10:01:01 -06002286
Richard Henderson39004a72022-11-11 10:09:37 +10002287 TCGOP_CALLI(op) = n = info->nr_in;
2288 for (i = 0; i < n; i++) {
2289 const TCGCallArgumentLoc *loc = &info->in[i];
2290 TCGTemp *ts = args[loc->arg_idx] + loc->tmp_subindex;
2291
2292 switch (loc->kind) {
2293 case TCG_CALL_ARG_NORMAL:
Richard Henderson313bdea2022-10-31 09:22:59 +11002294 case TCG_CALL_ARG_BY_REF:
2295 case TCG_CALL_ARG_BY_REF_N:
Richard Henderson39004a72022-11-11 10:09:37 +10002296 op->args[pi++] = temp_arg(ts);
2297 break;
2298
2299 case TCG_CALL_ARG_EXTEND_U:
2300 case TCG_CALL_ARG_EXTEND_S:
2301 {
Richard Henderson5dd48602023-01-29 13:26:49 -10002302 TCGv_i64 temp = tcg_temp_ebb_new_i64();
Richard Henderson39004a72022-11-11 10:09:37 +10002303 TCGv_i32 orig = temp_tcgv_i32(ts);
2304
2305 if (loc->kind == TCG_CALL_ARG_EXTEND_S) {
Richard Hendersoneb8b0222022-10-16 20:07:48 +10002306 tcg_gen_ext_i32_i64(temp, orig);
2307 } else {
2308 tcg_gen_extu_i32_i64(temp, orig);
2309 }
Richard Henderson39004a72022-11-11 10:09:37 +10002310 op->args[pi++] = tcgv_i64_arg(temp);
2311 extend_free[n_extend++] = temp;
Richard Henderson2bece2c2010-06-14 17:35:27 -07002312 }
Richard Hendersone2a9dd62022-10-17 15:55:56 +10002313 break;
Richard Henderson7319d832021-03-18 10:01:01 -06002314
Richard Hendersone2a9dd62022-10-17 15:55:56 +10002315 default:
2316 g_assert_not_reached();
bellardc896fe22008-02-01 10:05:41 +00002317 }
2318 }
Richard Henderson83a0ad22024-03-14 13:48:41 -10002319 op->args[pi++] = (uintptr_t)func;
Richard Henderson3e92aa32021-03-18 11:29:50 -06002320 op->args[pi++] = (uintptr_t)info;
Richard Henderson39004a72022-11-11 10:09:37 +10002321 tcg_debug_assert(pi == total_args);
pbrooka7812ae2008-11-17 14:43:54 +00002322
Richard Henderson07843f72024-03-13 13:32:29 -10002323 if (tcg_ctx->emit_before_op) {
2324 QTAILQ_INSERT_BEFORE(tcg_ctx->emit_before_op, op, link);
2325 } else {
2326 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2327 }
Richard Henderson2bece2c2010-06-14 17:35:27 -07002328
Richard Henderson39004a72022-11-11 10:09:37 +10002329 tcg_debug_assert(n_extend < ARRAY_SIZE(extend_free));
2330 for (i = 0; i < n_extend; ++i) {
2331 tcg_temp_free_i64(extend_free[i]);
Richard Henderson2bece2c2010-06-14 17:35:27 -07002332 }
bellardc896fe22008-02-01 10:05:41 +00002333}
bellardc896fe22008-02-01 10:05:41 +00002334
Richard Henderson83a0ad22024-03-14 13:48:41 -10002335void tcg_gen_call0(void *func, TCGHelperInfo *info, TCGTemp *ret)
Richard Hendersona3a692b2023-03-29 22:14:36 -07002336{
Richard Henderson83a0ad22024-03-14 13:48:41 -10002337 tcg_gen_callN(func, info, ret, NULL);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002338}
2339
Richard Henderson83a0ad22024-03-14 13:48:41 -10002340void tcg_gen_call1(void *func, TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1)
Richard Hendersona3a692b2023-03-29 22:14:36 -07002341{
Richard Henderson83a0ad22024-03-14 13:48:41 -10002342 tcg_gen_callN(func, info, ret, &t1);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002343}
2344
Richard Henderson83a0ad22024-03-14 13:48:41 -10002345void tcg_gen_call2(void *func, TCGHelperInfo *info, TCGTemp *ret,
2346 TCGTemp *t1, TCGTemp *t2)
Richard Hendersona3a692b2023-03-29 22:14:36 -07002347{
2348 TCGTemp *args[2] = { t1, t2 };
Richard Henderson83a0ad22024-03-14 13:48:41 -10002349 tcg_gen_callN(func, info, ret, args);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002350}
2351
Richard Henderson83a0ad22024-03-14 13:48:41 -10002352void tcg_gen_call3(void *func, TCGHelperInfo *info, TCGTemp *ret,
2353 TCGTemp *t1, TCGTemp *t2, TCGTemp *t3)
Richard Hendersona3a692b2023-03-29 22:14:36 -07002354{
2355 TCGTemp *args[3] = { t1, t2, t3 };
Richard Henderson83a0ad22024-03-14 13:48:41 -10002356 tcg_gen_callN(func, info, ret, args);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002357}
2358
Richard Henderson83a0ad22024-03-14 13:48:41 -10002359void tcg_gen_call4(void *func, TCGHelperInfo *info, TCGTemp *ret,
2360 TCGTemp *t1, TCGTemp *t2, TCGTemp *t3, TCGTemp *t4)
Richard Hendersona3a692b2023-03-29 22:14:36 -07002361{
2362 TCGTemp *args[4] = { t1, t2, t3, t4 };
Richard Henderson83a0ad22024-03-14 13:48:41 -10002363 tcg_gen_callN(func, info, ret, args);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002364}
2365
Richard Henderson83a0ad22024-03-14 13:48:41 -10002366void tcg_gen_call5(void *func, TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1,
Richard Hendersona3a692b2023-03-29 22:14:36 -07002367 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4, TCGTemp *t5)
2368{
2369 TCGTemp *args[5] = { t1, t2, t3, t4, t5 };
Richard Henderson83a0ad22024-03-14 13:48:41 -10002370 tcg_gen_callN(func, info, ret, args);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002371}
2372
Richard Henderson83a0ad22024-03-14 13:48:41 -10002373void tcg_gen_call6(void *func, TCGHelperInfo *info, TCGTemp *ret,
2374 TCGTemp *t1, TCGTemp *t2, TCGTemp *t3,
2375 TCGTemp *t4, TCGTemp *t5, TCGTemp *t6)
Richard Hendersona3a692b2023-03-29 22:14:36 -07002376{
2377 TCGTemp *args[6] = { t1, t2, t3, t4, t5, t6 };
Richard Henderson83a0ad22024-03-14 13:48:41 -10002378 tcg_gen_callN(func, info, ret, args);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002379}
2380
Richard Henderson83a0ad22024-03-14 13:48:41 -10002381void tcg_gen_call7(void *func, TCGHelperInfo *info, TCGTemp *ret, TCGTemp *t1,
Richard Hendersona3a692b2023-03-29 22:14:36 -07002382 TCGTemp *t2, TCGTemp *t3, TCGTemp *t4,
2383 TCGTemp *t5, TCGTemp *t6, TCGTemp *t7)
2384{
2385 TCGTemp *args[7] = { t1, t2, t3, t4, t5, t6, t7 };
Richard Henderson83a0ad22024-03-14 13:48:41 -10002386 tcg_gen_callN(func, info, ret, args);
Richard Hendersona3a692b2023-03-29 22:14:36 -07002387}
2388
blueswir18fcd3692008-08-17 20:26:25 +00002389static void tcg_reg_alloc_start(TCGContext *s)
bellardc896fe22008-02-01 10:05:41 +00002390{
Richard Hendersonac3b8892016-11-02 11:21:44 -06002391 int i, n;
Richard Hendersonac3b8892016-11-02 11:21:44 -06002392
Richard Hendersonee17db82020-03-29 10:11:56 -07002393 for (i = 0, n = s->nb_temps; i < n; i++) {
2394 TCGTemp *ts = &s->temps[i];
2395 TCGTempVal val = TEMP_VAL_MEM;
2396
2397 switch (ts->kind) {
Richard Hendersonc0522132020-03-29 18:55:52 -07002398 case TEMP_CONST:
2399 val = TEMP_VAL_CONST;
2400 break;
Richard Hendersonee17db82020-03-29 10:11:56 -07002401 case TEMP_FIXED:
2402 val = TEMP_VAL_REG;
2403 break;
2404 case TEMP_GLOBAL:
2405 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07002406 case TEMP_EBB:
Richard Hendersonee17db82020-03-29 10:11:56 -07002407 val = TEMP_VAL_DEAD;
2408 /* fall through */
Richard Hendersonf57c6912023-01-29 10:55:52 -10002409 case TEMP_TB:
Richard Hendersonee17db82020-03-29 10:11:56 -07002410 ts->mem_allocated = 0;
2411 break;
2412 default:
2413 g_assert_not_reached();
2414 }
2415 ts->val_type = val;
bellarde8996ee2008-05-23 17:33:39 +00002416 }
Richard Hendersonf8b2f202013-09-18 15:21:56 -07002417
2418 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
bellardc896fe22008-02-01 10:05:41 +00002419}
2420
Richard Hendersonf8b2f202013-09-18 15:21:56 -07002421static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
2422 TCGTemp *ts)
bellardc896fe22008-02-01 10:05:41 +00002423{
Richard Henderson1807f4c2017-06-20 12:24:57 -07002424 int idx = temp_idx(ts);
pbrookac56dd42008-02-03 19:56:33 +00002425
Richard Hendersonee17db82020-03-29 10:11:56 -07002426 switch (ts->kind) {
2427 case TEMP_FIXED:
2428 case TEMP_GLOBAL:
pbrookac56dd42008-02-03 19:56:33 +00002429 pstrcpy(buf, buf_size, ts->name);
Richard Hendersonee17db82020-03-29 10:11:56 -07002430 break;
Richard Hendersonf57c6912023-01-29 10:55:52 -10002431 case TEMP_TB:
Richard Hendersonf8b2f202013-09-18 15:21:56 -07002432 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
Richard Hendersonee17db82020-03-29 10:11:56 -07002433 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07002434 case TEMP_EBB:
Richard Hendersonf8b2f202013-09-18 15:21:56 -07002435 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
Richard Hendersonee17db82020-03-29 10:11:56 -07002436 break;
Richard Hendersonc0522132020-03-29 18:55:52 -07002437 case TEMP_CONST:
2438 switch (ts->type) {
2439 case TCG_TYPE_I32:
2440 snprintf(buf, buf_size, "$0x%x", (int32_t)ts->val);
2441 break;
2442#if TCG_TARGET_REG_BITS > 32
2443 case TCG_TYPE_I64:
2444 snprintf(buf, buf_size, "$0x%" PRIx64, ts->val);
2445 break;
2446#endif
2447 case TCG_TYPE_V64:
2448 case TCG_TYPE_V128:
2449 case TCG_TYPE_V256:
2450 snprintf(buf, buf_size, "v%d$0x%" PRIx64,
2451 64 << (ts->type - TCG_TYPE_V64), ts->val);
2452 break;
2453 default:
2454 g_assert_not_reached();
2455 }
2456 break;
bellardc896fe22008-02-01 10:05:41 +00002457 }
2458 return buf;
2459}
2460
Richard Henderson43439132017-06-19 23:18:10 -07002461static char *tcg_get_arg_str(TCGContext *s, char *buf,
2462 int buf_size, TCGArg arg)
Richard Hendersonf8b2f202013-09-18 15:21:56 -07002463{
Richard Henderson43439132017-06-19 23:18:10 -07002464 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
Richard Hendersonf8b2f202013-09-18 15:21:56 -07002465}
2466
blueswir1f48f3ed2008-09-14 07:45:17 +00002467static const char * const cond_name[] =
2468{
Richard Henderson0aed2572012-09-24 14:21:40 -07002469 [TCG_COND_NEVER] = "never",
2470 [TCG_COND_ALWAYS] = "always",
blueswir1f48f3ed2008-09-14 07:45:17 +00002471 [TCG_COND_EQ] = "eq",
2472 [TCG_COND_NE] = "ne",
2473 [TCG_COND_LT] = "lt",
2474 [TCG_COND_GE] = "ge",
2475 [TCG_COND_LE] = "le",
2476 [TCG_COND_GT] = "gt",
2477 [TCG_COND_LTU] = "ltu",
2478 [TCG_COND_GEU] = "geu",
2479 [TCG_COND_LEU] = "leu",
Richard Hendersond48097d2023-10-23 18:53:27 -07002480 [TCG_COND_GTU] = "gtu",
2481 [TCG_COND_TSTEQ] = "tsteq",
2482 [TCG_COND_TSTNE] = "tstne",
blueswir1f48f3ed2008-09-14 07:45:17 +00002483};
2484
Richard Henderson12fde9b2022-11-07 10:42:56 +11002485static const char * const ldst_name[(MO_BSWAP | MO_SSIZE) + 1] =
Richard Hendersonf713d6a2013-09-04 08:11:05 -07002486{
2487 [MO_UB] = "ub",
2488 [MO_SB] = "sb",
2489 [MO_LEUW] = "leuw",
2490 [MO_LESW] = "lesw",
2491 [MO_LEUL] = "leul",
2492 [MO_LESL] = "lesl",
Frédéric Pétrotfc313c62022-01-06 22:00:51 +01002493 [MO_LEUQ] = "leq",
Richard Hendersonf713d6a2013-09-04 08:11:05 -07002494 [MO_BEUW] = "beuw",
2495 [MO_BESW] = "besw",
2496 [MO_BEUL] = "beul",
2497 [MO_BESL] = "besl",
Frédéric Pétrotfc313c62022-01-06 22:00:51 +01002498 [MO_BEUQ] = "beq",
Richard Henderson12fde9b2022-11-07 10:42:56 +11002499 [MO_128 + MO_BE] = "beo",
2500 [MO_128 + MO_LE] = "leo",
Richard Hendersonf713d6a2013-09-04 08:11:05 -07002501};
2502
Sergey Sorokin1f00b272016-06-23 21:16:46 +03002503static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
Sergey Sorokin1f00b272016-06-23 21:16:46 +03002504 [MO_UNALN >> MO_ASHIFT] = "un+",
Sergey Sorokin1f00b272016-06-23 21:16:46 +03002505 [MO_ALIGN >> MO_ASHIFT] = "al+",
Sergey Sorokin1f00b272016-06-23 21:16:46 +03002506 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
2507 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
2508 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
2509 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
2510 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
2511 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
2512};
2513
Richard Henderson37031fe2022-10-21 21:24:40 +10002514static const char * const atom_name[(MO_ATOM_MASK >> MO_ATOM_SHIFT) + 1] = {
2515 [MO_ATOM_IFALIGN >> MO_ATOM_SHIFT] = "",
2516 [MO_ATOM_IFALIGN_PAIR >> MO_ATOM_SHIFT] = "pair+",
2517 [MO_ATOM_WITHIN16 >> MO_ATOM_SHIFT] = "w16+",
2518 [MO_ATOM_WITHIN16_PAIR >> MO_ATOM_SHIFT] = "w16p+",
2519 [MO_ATOM_SUBALIGN >> MO_ATOM_SHIFT] = "sub+",
2520 [MO_ATOM_NONE >> MO_ATOM_SHIFT] = "noat+",
2521};
2522
Richard Henderson587195b2021-06-12 21:32:27 -07002523static const char bswap_flag_name[][6] = {
2524 [TCG_BSWAP_IZ] = "iz",
2525 [TCG_BSWAP_OZ] = "oz",
2526 [TCG_BSWAP_OS] = "os",
2527 [TCG_BSWAP_IZ | TCG_BSWAP_OZ] = "iz,oz",
2528 [TCG_BSWAP_IZ | TCG_BSWAP_OS] = "iz,os",
2529};
2530
Richard Hendersonb384c732024-03-15 10:33:49 -10002531#ifdef CONFIG_PLUGIN
2532static const char * const plugin_from_name[] = {
2533 "from-tb",
2534 "from-insn",
2535 "after-insn",
2536 "after-tb",
2537};
2538#endif
2539
Richard Hendersonb0164862018-11-27 07:16:21 -08002540static inline bool tcg_regset_single(TCGRegSet d)
2541{
2542 return (d & (d - 1)) == 0;
2543}
2544
2545static inline TCGReg tcg_regset_first(TCGRegSet d)
2546{
2547 if (TCG_TARGET_NB_REGS <= 32) {
2548 return ctz32(d);
2549 } else {
2550 return ctz64(d);
2551 }
2552}
2553
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002554/* Return only the number of characters output -- no error return. */
2555#define ne_fprintf(...) \
2556 ({ int ret_ = fprintf(__VA_ARGS__); ret_ >= 0 ? ret_ : 0; })
2557
Richard Hendersonb384c732024-03-15 10:33:49 -10002558void tcg_dump_ops(TCGContext *s, FILE *f, bool have_prefs)
bellardc896fe22008-02-01 10:05:41 +00002559{
bellardc896fe22008-02-01 10:05:41 +00002560 char buf[128];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002561 TCGOp *op;
bellardc896fe22008-02-01 10:05:41 +00002562
Richard Henderson15fa08f2017-11-02 15:19:14 +01002563 QTAILQ_FOREACH(op, &s->ops, link) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002564 int i, k, nb_oargs, nb_iargs, nb_cargs;
2565 const TCGOpDef *def;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002566 TCGOpcode c;
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002567 int col = 0;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002568
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002569 c = op->opc;
bellardc896fe22008-02-01 10:05:41 +00002570 def = &tcg_op_defs[c];
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002571
Richard Henderson765b8422015-08-29 12:37:33 -07002572 if (c == INDEX_op_insn_start) {
Richard Hendersonb0164862018-11-27 07:16:21 -08002573 nb_oargs = 0;
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002574 col += ne_fprintf(f, "\n ----");
Richard Henderson9aef40e2015-08-30 09:21:33 -07002575
Richard Henderson747bd692023-03-31 21:30:31 -07002576 for (i = 0, k = s->insn_start_words; i < k; ++i) {
Richard Hendersonc9ad8d22023-03-08 12:24:41 -08002577 col += ne_fprintf(f, " %016" PRIx64,
2578 tcg_get_insn_start_param(op, i));
Blue Swirleeacee42012-06-03 16:35:32 +00002579 }
bellard7e4597d2008-05-22 16:56:05 +00002580 } else if (c == INDEX_op_call) {
Richard Henderson3e92aa32021-03-18 11:29:50 -06002581 const TCGHelperInfo *info = tcg_call_info(op);
Richard Hendersonfa52e662021-03-18 16:40:07 -06002582 void *func = tcg_call_func(op);
Richard Henderson3e92aa32021-03-18 11:29:50 -06002583
bellardc896fe22008-02-01 10:05:41 +00002584 /* variable number of arguments */
Richard Hendersoncd9090a2017-11-14 13:02:51 +01002585 nb_oargs = TCGOP_CALLO(op);
2586 nb_iargs = TCGOP_CALLI(op);
bellardc896fe22008-02-01 10:05:41 +00002587 nb_cargs = def->nb_cargs;
bellardc896fe22008-02-01 10:05:41 +00002588
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002589 col += ne_fprintf(f, " %s ", def->name);
Richard Henderson3e92aa32021-03-18 11:29:50 -06002590
2591 /*
2592 * Print the function name from TCGHelperInfo, if available.
2593 * Note that plugins have a template function for the info,
2594 * but the actual function pointer comes from the plugin.
2595 */
Richard Henderson3e92aa32021-03-18 11:29:50 -06002596 if (func == info->func) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002597 col += ne_fprintf(f, "%s", info->name);
Richard Henderson3e92aa32021-03-18 11:29:50 -06002598 } else {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002599 col += ne_fprintf(f, "plugin(%p)", func);
Richard Henderson3e92aa32021-03-18 11:29:50 -06002600 }
2601
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002602 col += ne_fprintf(f, ",$0x%x,$%d", info->flags, nb_oargs);
Richard Hendersoncf066672014-03-22 20:06:52 -07002603 for (i = 0; i < nb_oargs; i++) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002604 col += ne_fprintf(f, ",%s", tcg_get_arg_str(s, buf, sizeof(buf),
2605 op->args[i]));
bellardb03cce82008-05-10 10:52:05 +00002606 }
Richard Hendersoncf066672014-03-22 20:06:52 -07002607 for (i = 0; i < nb_iargs; i++) {
Richard Hendersonefee3742016-12-08 13:12:08 -08002608 TCGArg arg = op->args[nb_oargs + i];
Richard Henderson39004a72022-11-11 10:09:37 +10002609 const char *t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002610 col += ne_fprintf(f, ",%s", t);
bellarde8996ee2008-05-23 17:33:39 +00002611 }
bellardb03cce82008-05-10 10:52:05 +00002612 } else {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002613 col += ne_fprintf(f, " %s ", def->name);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002614
2615 nb_oargs = def->nb_oargs;
2616 nb_iargs = def->nb_iargs;
2617 nb_cargs = def->nb_cargs;
2618
Richard Hendersond2fd7452017-09-14 13:53:46 -07002619 if (def->flags & TCG_OPF_VECTOR) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002620 col += ne_fprintf(f, "v%d,e%d,", 64 << TCGOP_VECL(op),
2621 8 << TCGOP_VECE(op));
Richard Hendersond2fd7452017-09-14 13:53:46 -07002622 }
2623
bellardb03cce82008-05-10 10:52:05 +00002624 k = 0;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002625 for (i = 0; i < nb_oargs; i++) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002626 const char *sep = k ? "," : "";
2627 col += ne_fprintf(f, "%s%s", sep,
2628 tcg_get_arg_str(s, buf, sizeof(buf),
2629 op->args[k++]));
bellardb03cce82008-05-10 10:52:05 +00002630 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07002631 for (i = 0; i < nb_iargs; i++) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002632 const char *sep = k ? "," : "";
2633 col += ne_fprintf(f, "%s%s", sep,
2634 tcg_get_arg_str(s, buf, sizeof(buf),
2635 op->args[k++]));
bellardb03cce82008-05-10 10:52:05 +00002636 }
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002637 switch (c) {
2638 case INDEX_op_brcond_i32:
Richard Hendersonffc5ea02012-09-21 10:13:34 -07002639 case INDEX_op_setcond_i32:
Richard Henderson36355022023-08-04 23:24:04 +00002640 case INDEX_op_negsetcond_i32:
Richard Hendersonffc5ea02012-09-21 10:13:34 -07002641 case INDEX_op_movcond_i32:
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002642 case INDEX_op_brcond2_i32:
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002643 case INDEX_op_setcond2_i32:
Richard Hendersonffc5ea02012-09-21 10:13:34 -07002644 case INDEX_op_brcond_i64:
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002645 case INDEX_op_setcond_i64:
Richard Henderson36355022023-08-04 23:24:04 +00002646 case INDEX_op_negsetcond_i64:
Richard Hendersonffc5ea02012-09-21 10:13:34 -07002647 case INDEX_op_movcond_i64:
Richard Henderson212be172017-11-17 20:47:42 +01002648 case INDEX_op_cmp_vec:
Richard Hendersonf75da292019-04-30 13:01:12 -07002649 case INDEX_op_cmpsel_vec:
Richard Hendersonefee3742016-12-08 13:12:08 -08002650 if (op->args[k] < ARRAY_SIZE(cond_name)
2651 && cond_name[op->args[k]]) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002652 col += ne_fprintf(f, ",%s", cond_name[op->args[k++]]);
Blue Swirleeacee42012-06-03 16:35:32 +00002653 } else {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002654 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, op->args[k++]);
Blue Swirleeacee42012-06-03 16:35:32 +00002655 }
blueswir1f48f3ed2008-09-14 07:45:17 +00002656 i = 1;
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002657 break;
Richard Hendersonfecccfc2023-05-16 20:07:20 -07002658 case INDEX_op_qemu_ld_a32_i32:
2659 case INDEX_op_qemu_ld_a64_i32:
2660 case INDEX_op_qemu_st_a32_i32:
2661 case INDEX_op_qemu_st_a64_i32:
2662 case INDEX_op_qemu_st8_a32_i32:
2663 case INDEX_op_qemu_st8_a64_i32:
2664 case INDEX_op_qemu_ld_a32_i64:
2665 case INDEX_op_qemu_ld_a64_i64:
2666 case INDEX_op_qemu_st_a32_i64:
2667 case INDEX_op_qemu_st_a64_i64:
2668 case INDEX_op_qemu_ld_a32_i128:
2669 case INDEX_op_qemu_ld_a64_i128:
2670 case INDEX_op_qemu_st_a32_i128:
2671 case INDEX_op_qemu_st_a64_i128:
Richard Henderson59227d52015-05-12 11:51:44 -07002672 {
Richard Henderson37031fe2022-10-21 21:24:40 +10002673 const char *s_al, *s_op, *s_at;
Richard Henderson9002ffc2021-07-25 12:06:49 -10002674 MemOpIdx oi = op->args[k++];
Philippe Mathieu-Daudé9a239c62023-09-04 18:12:13 +02002675 MemOp mop = get_memop(oi);
Richard Henderson59227d52015-05-12 11:51:44 -07002676 unsigned ix = get_mmuidx(oi);
2677
Philippe Mathieu-Daudé9a239c62023-09-04 18:12:13 +02002678 s_al = alignment_name[(mop & MO_AMASK) >> MO_ASHIFT];
2679 s_op = ldst_name[mop & (MO_BSWAP | MO_SSIZE)];
2680 s_at = atom_name[(mop & MO_ATOM_MASK) >> MO_ATOM_SHIFT];
2681 mop &= ~(MO_AMASK | MO_BSWAP | MO_SSIZE | MO_ATOM_MASK);
Richard Henderson37031fe2022-10-21 21:24:40 +10002682
2683 /* If all fields are accounted for, print symbolically. */
Philippe Mathieu-Daudé9a239c62023-09-04 18:12:13 +02002684 if (!mop && s_al && s_op && s_at) {
Richard Henderson37031fe2022-10-21 21:24:40 +10002685 col += ne_fprintf(f, ",%s%s%s,%u",
2686 s_at, s_al, s_op, ix);
Richard Henderson59c4b7e2015-06-01 14:38:56 -07002687 } else {
Philippe Mathieu-Daudé9a239c62023-09-04 18:12:13 +02002688 mop = get_memop(oi);
2689 col += ne_fprintf(f, ",$0x%x,%u", mop, ix);
Richard Henderson59227d52015-05-12 11:51:44 -07002690 }
2691 i = 1;
Richard Hendersonf713d6a2013-09-04 08:11:05 -07002692 }
Richard Hendersonf713d6a2013-09-04 08:11:05 -07002693 break;
Richard Henderson587195b2021-06-12 21:32:27 -07002694 case INDEX_op_bswap16_i32:
2695 case INDEX_op_bswap16_i64:
2696 case INDEX_op_bswap32_i32:
2697 case INDEX_op_bswap32_i64:
2698 case INDEX_op_bswap64_i64:
2699 {
2700 TCGArg flags = op->args[k];
2701 const char *name = NULL;
2702
2703 if (flags < ARRAY_SIZE(bswap_flag_name)) {
2704 name = bswap_flag_name[flags];
2705 }
2706 if (name) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002707 col += ne_fprintf(f, ",%s", name);
Richard Henderson587195b2021-06-12 21:32:27 -07002708 } else {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002709 col += ne_fprintf(f, ",$0x%" TCG_PRIlx, flags);
Richard Henderson587195b2021-06-12 21:32:27 -07002710 }
2711 i = k = 1;
2712 }
2713 break;
Richard Hendersonb384c732024-03-15 10:33:49 -10002714#ifdef CONFIG_PLUGIN
2715 case INDEX_op_plugin_cb:
2716 {
2717 TCGArg from = op->args[k++];
2718 const char *name = NULL;
2719
2720 if (from < ARRAY_SIZE(plugin_from_name)) {
2721 name = plugin_from_name[from];
2722 }
2723 if (name) {
2724 col += ne_fprintf(f, "%s", name);
2725 } else {
2726 col += ne_fprintf(f, "$0x%" TCG_PRIlx, from);
2727 }
2728 i = 1;
2729 }
2730 break;
2731#endif
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002732 default:
blueswir1f48f3ed2008-09-14 07:45:17 +00002733 i = 0;
Richard Hendersonbe210ac2010-01-07 10:13:31 -08002734 break;
2735 }
Richard Henderson51e39722015-02-13 18:51:05 -08002736 switch (c) {
2737 case INDEX_op_set_label:
2738 case INDEX_op_br:
2739 case INDEX_op_brcond_i32:
2740 case INDEX_op_brcond_i64:
2741 case INDEX_op_brcond2_i32:
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002742 col += ne_fprintf(f, "%s$L%d", k ? "," : "",
2743 arg_label(op->args[k])->id);
Richard Henderson51e39722015-02-13 18:51:05 -08002744 i++, k++;
2745 break;
Richard Henderson34708672021-02-18 17:05:55 -08002746 case INDEX_op_mb:
2747 {
2748 TCGBar membar = op->args[k];
2749 const char *b_op, *m_op;
2750
2751 switch (membar & TCG_BAR_SC) {
2752 case 0:
2753 b_op = "none";
2754 break;
2755 case TCG_BAR_LDAQ:
2756 b_op = "acq";
2757 break;
2758 case TCG_BAR_STRL:
2759 b_op = "rel";
2760 break;
2761 case TCG_BAR_SC:
2762 b_op = "seq";
2763 break;
2764 default:
2765 g_assert_not_reached();
2766 }
2767
2768 switch (membar & TCG_MO_ALL) {
2769 case 0:
2770 m_op = "none";
2771 break;
2772 case TCG_MO_LD_LD:
2773 m_op = "rr";
2774 break;
2775 case TCG_MO_LD_ST:
2776 m_op = "rw";
2777 break;
2778 case TCG_MO_ST_LD:
2779 m_op = "wr";
2780 break;
2781 case TCG_MO_ST_ST:
2782 m_op = "ww";
2783 break;
2784 case TCG_MO_LD_LD | TCG_MO_LD_ST:
2785 m_op = "rr+rw";
2786 break;
2787 case TCG_MO_LD_LD | TCG_MO_ST_LD:
2788 m_op = "rr+wr";
2789 break;
2790 case TCG_MO_LD_LD | TCG_MO_ST_ST:
2791 m_op = "rr+ww";
2792 break;
2793 case TCG_MO_LD_ST | TCG_MO_ST_LD:
2794 m_op = "rw+wr";
2795 break;
2796 case TCG_MO_LD_ST | TCG_MO_ST_ST:
2797 m_op = "rw+ww";
2798 break;
2799 case TCG_MO_ST_LD | TCG_MO_ST_ST:
2800 m_op = "wr+ww";
2801 break;
2802 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_LD:
2803 m_op = "rr+rw+wr";
2804 break;
2805 case TCG_MO_LD_LD | TCG_MO_LD_ST | TCG_MO_ST_ST:
2806 m_op = "rr+rw+ww";
2807 break;
2808 case TCG_MO_LD_LD | TCG_MO_ST_LD | TCG_MO_ST_ST:
2809 m_op = "rr+wr+ww";
2810 break;
2811 case TCG_MO_LD_ST | TCG_MO_ST_LD | TCG_MO_ST_ST:
2812 m_op = "rw+wr+ww";
2813 break;
2814 case TCG_MO_ALL:
2815 m_op = "all";
2816 break;
2817 default:
2818 g_assert_not_reached();
2819 }
2820
2821 col += ne_fprintf(f, "%s%s:%s", (k ? "," : ""), b_op, m_op);
2822 i++, k++;
2823 }
2824 break;
Richard Henderson51e39722015-02-13 18:51:05 -08002825 default:
2826 break;
2827 }
2828 for (; i < nb_cargs; i++, k++) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002829 col += ne_fprintf(f, "%s$0x%" TCG_PRIlx, k ? "," : "",
2830 op->args[k]);
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002831 }
2832 }
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002833
Richard Henderson1894f692018-11-27 12:46:00 -08002834 if (have_prefs || op->life) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002835 for (; col < 40; ++col) {
2836 putc(' ', f);
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002837 }
Richard Henderson1894f692018-11-27 12:46:00 -08002838 }
2839
2840 if (op->life) {
2841 unsigned life = op->life;
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002842
2843 if (life & (SYNC_ARG * 3)) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002844 ne_fprintf(f, " sync:");
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002845 for (i = 0; i < 2; ++i) {
2846 if (life & (SYNC_ARG << i)) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002847 ne_fprintf(f, " %d", i);
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002848 }
2849 }
2850 }
2851 life /= DEAD_ARG;
2852 if (life) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002853 ne_fprintf(f, " dead:");
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002854 for (i = 0; life; ++i, life >>= 1) {
2855 if (life & 1) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002856 ne_fprintf(f, " %d", i);
Richard Hendersonbdfb4602016-06-23 19:15:55 -07002857 }
2858 }
bellardb03cce82008-05-10 10:52:05 +00002859 }
bellardc896fe22008-02-01 10:05:41 +00002860 }
Richard Henderson1894f692018-11-27 12:46:00 -08002861
2862 if (have_prefs) {
2863 for (i = 0; i < nb_oargs; ++i) {
Richard Henderson31fd8842022-11-11 15:10:51 +10002864 TCGRegSet set = output_pref(op, i);
Richard Henderson1894f692018-11-27 12:46:00 -08002865
2866 if (i == 0) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002867 ne_fprintf(f, " pref=");
Richard Henderson1894f692018-11-27 12:46:00 -08002868 } else {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002869 ne_fprintf(f, ",");
Richard Henderson1894f692018-11-27 12:46:00 -08002870 }
2871 if (set == 0) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002872 ne_fprintf(f, "none");
Richard Henderson1894f692018-11-27 12:46:00 -08002873 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002874 ne_fprintf(f, "all");
Richard Henderson1894f692018-11-27 12:46:00 -08002875#ifdef CONFIG_DEBUG_TCG
2876 } else if (tcg_regset_single(set)) {
2877 TCGReg reg = tcg_regset_first(set);
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002878 ne_fprintf(f, "%s", tcg_target_reg_names[reg]);
Richard Henderson1894f692018-11-27 12:46:00 -08002879#endif
2880 } else if (TCG_TARGET_NB_REGS <= 32) {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002881 ne_fprintf(f, "0x%x", (uint32_t)set);
Richard Henderson1894f692018-11-27 12:46:00 -08002882 } else {
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002883 ne_fprintf(f, "0x%" PRIx64, (uint64_t)set);
Richard Henderson1894f692018-11-27 12:46:00 -08002884 }
2885 }
2886 }
2887
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07002888 putc('\n', f);
bellardc896fe22008-02-01 10:05:41 +00002889 }
2890}
2891
2892/* we give more priority to constraints with less registers */
2893static int get_constraint_priority(const TCGOpDef *def, int k)
2894{
Richard Henderson74a11792020-09-03 15:56:24 -07002895 const TCGArgConstraint *arg_ct = &def->args_ct[k];
Richard Henderson29f5e922022-10-14 07:37:38 +11002896 int n = ctpop64(arg_ct->regs);
bellardc896fe22008-02-01 10:05:41 +00002897
Richard Henderson29f5e922022-10-14 07:37:38 +11002898 /*
2899 * Sort constraints of a single register first, which includes output
2900 * aliases (which must exactly match the input already allocated).
2901 */
2902 if (n == 1 || arg_ct->oalias) {
2903 return INT_MAX;
bellardc896fe22008-02-01 10:05:41 +00002904 }
Richard Henderson29f5e922022-10-14 07:37:38 +11002905
2906 /*
2907 * Sort register pairs next, first then second immediately after.
2908 * Arbitrarily sort multiple pairs by the index of the first reg;
2909 * there shouldn't be many pairs.
2910 */
2911 switch (arg_ct->pair) {
2912 case 1:
2913 case 3:
2914 return (k + 1) * 2;
2915 case 2:
2916 return (arg_ct->pair_index + 1) * 2 - 1;
2917 }
2918
2919 /* Finally, sort by decreasing register count. */
2920 assert(n > 1);
2921 return -n;
bellardc896fe22008-02-01 10:05:41 +00002922}
2923
2924/* sort from highest priority to lowest */
2925static void sort_constraints(TCGOpDef *def, int start, int n)
2926{
Richard Henderson66792f92019-04-04 09:37:38 +07002927 int i, j;
2928 TCGArgConstraint *a = def->args_ct;
bellardc896fe22008-02-01 10:05:41 +00002929
Richard Henderson66792f92019-04-04 09:37:38 +07002930 for (i = 0; i < n; i++) {
2931 a[start + i].sort_index = start + i;
2932 }
2933 if (n <= 1) {
bellardc896fe22008-02-01 10:05:41 +00002934 return;
Richard Henderson66792f92019-04-04 09:37:38 +07002935 }
2936 for (i = 0; i < n - 1; i++) {
2937 for (j = i + 1; j < n; j++) {
2938 int p1 = get_constraint_priority(def, a[start + i].sort_index);
2939 int p2 = get_constraint_priority(def, a[start + j].sort_index);
bellardc896fe22008-02-01 10:05:41 +00002940 if (p1 < p2) {
Richard Henderson66792f92019-04-04 09:37:38 +07002941 int tmp = a[start + i].sort_index;
2942 a[start + i].sort_index = a[start + j].sort_index;
2943 a[start + j].sort_index = tmp;
bellardc896fe22008-02-01 10:05:41 +00002944 }
2945 }
2946 }
2947}
2948
Richard Hendersonf69d2772016-11-18 09:31:40 +01002949static void process_op_defs(TCGContext *s)
bellardc896fe22008-02-01 10:05:41 +00002950{
Richard Hendersona9751602010-03-19 11:12:29 -07002951 TCGOpcode op;
bellardc896fe22008-02-01 10:05:41 +00002952
Richard Hendersonf69d2772016-11-18 09:31:40 +01002953 for (op = 0; op < NB_OPS; op++) {
2954 TCGOpDef *def = &tcg_op_defs[op];
2955 const TCGTargetOpDef *tdefs;
Richard Henderson29f5e922022-10-14 07:37:38 +11002956 bool saw_alias_pair = false;
2957 int i, o, i2, o2, nb_args;
Richard Hendersonf69d2772016-11-18 09:31:40 +01002958
2959 if (def->flags & TCG_OPF_NOT_PRESENT) {
2960 continue;
2961 }
2962
bellardc896fe22008-02-01 10:05:41 +00002963 nb_args = def->nb_iargs + def->nb_oargs;
Richard Hendersonf69d2772016-11-18 09:31:40 +01002964 if (nb_args == 0) {
2965 continue;
2966 }
2967
Richard Henderson4c22e842020-10-16 22:20:55 -07002968 /*
2969 * Macro magic should make it impossible, but double-check that
2970 * the array index is in range. Since the signness of an enum
2971 * is implementation defined, force the result to unsigned.
2972 */
2973 unsigned con_set = tcg_target_op_def(op);
2974 tcg_debug_assert(con_set < ARRAY_SIZE(constraint_sets));
2975 tdefs = &constraint_sets[con_set];
Richard Hendersonf69d2772016-11-18 09:31:40 +01002976
2977 for (i = 0; i < nb_args; i++) {
2978 const char *ct_str = tdefs->args_ct_str[i];
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01002979 bool input_p = i >= def->nb_oargs;
2980
Richard Hendersonf69d2772016-11-18 09:31:40 +01002981 /* Incomplete TCGTargetOpDef entry. */
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +02002982 tcg_debug_assert(ct_str != NULL);
Richard Hendersonf69d2772016-11-18 09:31:40 +01002983
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01002984 switch (*ct_str) {
2985 case '0' ... '9':
2986 o = *ct_str - '0';
2987 tcg_debug_assert(input_p);
2988 tcg_debug_assert(o < def->nb_oargs);
2989 tcg_debug_assert(def->args_ct[o].regs != 0);
2990 tcg_debug_assert(!def->args_ct[o].oalias);
2991 def->args_ct[i] = def->args_ct[o];
2992 /* The output sets oalias. */
2993 def->args_ct[o].oalias = 1;
2994 def->args_ct[o].alias_index = i;
2995 /* The input sets ialias. */
2996 def->args_ct[i].ialias = 1;
2997 def->args_ct[i].alias_index = o;
Richard Henderson29f5e922022-10-14 07:37:38 +11002998 if (def->args_ct[i].pair) {
2999 saw_alias_pair = true;
3000 }
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01003001 tcg_debug_assert(ct_str[1] == '\0');
3002 continue;
3003
3004 case '&':
3005 tcg_debug_assert(!input_p);
3006 def->args_ct[i].newreg = true;
3007 ct_str++;
3008 break;
Richard Henderson29f5e922022-10-14 07:37:38 +11003009
3010 case 'p': /* plus */
3011 /* Allocate to the register after the previous. */
3012 tcg_debug_assert(i > (input_p ? def->nb_oargs : 0));
3013 o = i - 1;
3014 tcg_debug_assert(!def->args_ct[o].pair);
3015 tcg_debug_assert(!def->args_ct[o].ct);
3016 def->args_ct[i] = (TCGArgConstraint){
3017 .pair = 2,
3018 .pair_index = o,
3019 .regs = def->args_ct[o].regs << 1,
Richard Hendersonca5bed02024-01-02 01:27:18 +00003020 .newreg = def->args_ct[o].newreg,
Richard Henderson29f5e922022-10-14 07:37:38 +11003021 };
3022 def->args_ct[o].pair = 1;
3023 def->args_ct[o].pair_index = i;
3024 tcg_debug_assert(ct_str[1] == '\0');
3025 continue;
3026
3027 case 'm': /* minus */
3028 /* Allocate to the register before the previous. */
3029 tcg_debug_assert(i > (input_p ? def->nb_oargs : 0));
3030 o = i - 1;
3031 tcg_debug_assert(!def->args_ct[o].pair);
3032 tcg_debug_assert(!def->args_ct[o].ct);
3033 def->args_ct[i] = (TCGArgConstraint){
3034 .pair = 1,
3035 .pair_index = o,
3036 .regs = def->args_ct[o].regs >> 1,
Richard Hendersonca5bed02024-01-02 01:27:18 +00003037 .newreg = def->args_ct[o].newreg,
Richard Henderson29f5e922022-10-14 07:37:38 +11003038 };
3039 def->args_ct[o].pair = 2;
3040 def->args_ct[o].pair_index = i;
3041 tcg_debug_assert(ct_str[1] == '\0');
3042 continue;
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01003043 }
3044
3045 do {
3046 switch (*ct_str) {
Richard Henderson17280ff2016-11-18 17:41:24 +01003047 case 'i':
3048 def->args_ct[i].ct |= TCG_CT_CONST;
Richard Henderson17280ff2016-11-18 17:41:24 +01003049 break;
Richard Henderson358b4922020-10-16 15:27:46 -07003050
Richard Henderson358b4922020-10-16 15:27:46 -07003051 /* Include all of the target-specific constraints. */
3052
3053#undef CONST
3054#define CONST(CASE, MASK) \
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01003055 case CASE: def->args_ct[i].ct |= MASK; break;
Richard Henderson358b4922020-10-16 15:27:46 -07003056#define REGS(CASE, MASK) \
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01003057 case CASE: def->args_ct[i].regs |= MASK; break;
Richard Henderson358b4922020-10-16 15:27:46 -07003058
3059#include "tcg-target-con-str.h"
3060
3061#undef REGS
3062#undef CONST
Richard Henderson17280ff2016-11-18 17:41:24 +01003063 default:
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01003064 case '0' ... '9':
3065 case '&':
Richard Henderson29f5e922022-10-14 07:37:38 +11003066 case 'p':
3067 case 'm':
Richard Henderson17280ff2016-11-18 17:41:24 +01003068 /* Typo in TCGTargetOpDef constraint. */
Richard Henderson358b4922020-10-16 15:27:46 -07003069 g_assert_not_reached();
bellardc896fe22008-02-01 10:05:41 +00003070 }
Philippe Mathieu-Daudé8940ea02022-12-19 23:09:23 +01003071 } while (*++ct_str != '\0');
bellardc896fe22008-02-01 10:05:41 +00003072 }
3073
Stefan Weilc68aaa12010-02-15 17:17:21 +01003074 /* TCGTargetOpDef entry with too much information? */
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +02003075 tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
Stefan Weilc68aaa12010-02-15 17:17:21 +01003076
Richard Henderson29f5e922022-10-14 07:37:38 +11003077 /*
3078 * Fix up output pairs that are aliased with inputs.
3079 * When we created the alias, we copied pair from the output.
3080 * There are three cases:
3081 * (1a) Pairs of inputs alias pairs of outputs.
3082 * (1b) One input aliases the first of a pair of outputs.
3083 * (2) One input aliases the second of a pair of outputs.
3084 *
3085 * Case 1a is handled by making sure that the pair_index'es are
3086 * properly updated so that they appear the same as a pair of inputs.
3087 *
3088 * Case 1b is handled by setting the pair_index of the input to
3089 * itself, simply so it doesn't point to an unrelated argument.
3090 * Since we don't encounter the "second" during the input allocation
3091 * phase, nothing happens with the second half of the input pair.
3092 *
3093 * Case 2 is handled by setting the second input to pair=3, the
3094 * first output to pair=3, and the pair_index'es to match.
3095 */
3096 if (saw_alias_pair) {
3097 for (i = def->nb_oargs; i < nb_args; i++) {
3098 /*
3099 * Since [0-9pm] must be alone in the constraint string,
3100 * the only way they can both be set is if the pair comes
3101 * from the output alias.
3102 */
3103 if (!def->args_ct[i].ialias) {
3104 continue;
3105 }
3106 switch (def->args_ct[i].pair) {
3107 case 0:
3108 break;
3109 case 1:
3110 o = def->args_ct[i].alias_index;
3111 o2 = def->args_ct[o].pair_index;
3112 tcg_debug_assert(def->args_ct[o].pair == 1);
3113 tcg_debug_assert(def->args_ct[o2].pair == 2);
3114 if (def->args_ct[o2].oalias) {
3115 /* Case 1a */
3116 i2 = def->args_ct[o2].alias_index;
3117 tcg_debug_assert(def->args_ct[i2].pair == 2);
3118 def->args_ct[i2].pair_index = i;
3119 def->args_ct[i].pair_index = i2;
3120 } else {
3121 /* Case 1b */
3122 def->args_ct[i].pair_index = i;
3123 }
3124 break;
3125 case 2:
3126 o = def->args_ct[i].alias_index;
3127 o2 = def->args_ct[o].pair_index;
3128 tcg_debug_assert(def->args_ct[o].pair == 2);
3129 tcg_debug_assert(def->args_ct[o2].pair == 1);
3130 if (def->args_ct[o2].oalias) {
3131 /* Case 1a */
3132 i2 = def->args_ct[o2].alias_index;
3133 tcg_debug_assert(def->args_ct[i2].pair == 1);
3134 def->args_ct[i2].pair_index = i;
3135 def->args_ct[i].pair_index = i2;
3136 } else {
3137 /* Case 2 */
3138 def->args_ct[i].pair = 3;
3139 def->args_ct[o2].pair = 3;
3140 def->args_ct[i].pair_index = o2;
3141 def->args_ct[o2].pair_index = i;
3142 }
3143 break;
3144 default:
3145 g_assert_not_reached();
3146 }
3147 }
3148 }
3149
bellardc896fe22008-02-01 10:05:41 +00003150 /* sort the constraints (XXX: this is just an heuristic) */
3151 sort_constraints(def, 0, def->nb_oargs);
3152 sort_constraints(def, def->nb_oargs, def->nb_iargs);
bellardc896fe22008-02-01 10:05:41 +00003153 }
bellardc896fe22008-02-01 10:05:41 +00003154}
3155
Richard Hendersonf85b1fc2023-03-03 13:47:27 -08003156static void remove_label_use(TCGOp *op, int idx)
3157{
3158 TCGLabel *label = arg_label(op->args[idx]);
3159 TCGLabelUse *use;
3160
3161 QSIMPLEQ_FOREACH(use, &label->branches, next) {
3162 if (use->op == op) {
3163 QSIMPLEQ_REMOVE(&label->branches, use, TCGLabelUse, next);
3164 return;
3165 }
3166 }
3167 g_assert_not_reached();
3168}
3169
Richard Henderson0c627cd2014-03-30 16:51:54 -07003170void tcg_op_remove(TCGContext *s, TCGOp *op)
3171{
Richard Hendersond88a1172018-11-26 12:47:28 -08003172 switch (op->opc) {
3173 case INDEX_op_br:
Richard Hendersonf85b1fc2023-03-03 13:47:27 -08003174 remove_label_use(op, 0);
Richard Hendersond88a1172018-11-26 12:47:28 -08003175 break;
3176 case INDEX_op_brcond_i32:
3177 case INDEX_op_brcond_i64:
Richard Hendersonf85b1fc2023-03-03 13:47:27 -08003178 remove_label_use(op, 3);
Richard Hendersond88a1172018-11-26 12:47:28 -08003179 break;
3180 case INDEX_op_brcond2_i32:
Richard Hendersonf85b1fc2023-03-03 13:47:27 -08003181 remove_label_use(op, 5);
Richard Hendersond88a1172018-11-26 12:47:28 -08003182 break;
3183 default:
3184 break;
3185 }
3186
Richard Henderson15fa08f2017-11-02 15:19:14 +01003187 QTAILQ_REMOVE(&s->ops, op, link);
3188 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
Richard Hendersonabebf922018-05-08 19:18:59 +00003189 s->nb_ops--;
Richard Henderson0c627cd2014-03-30 16:51:54 -07003190}
3191
Richard Hendersona80cdd32021-06-04 14:26:45 -07003192void tcg_remove_ops_after(TCGOp *op)
3193{
3194 TCGContext *s = tcg_ctx;
3195
3196 while (true) {
3197 TCGOp *last = tcg_last_op();
3198 if (last == op) {
3199 return;
3200 }
3201 tcg_op_remove(s, last);
3202 }
3203}
3204
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003205static TCGOp *tcg_op_alloc(TCGOpcode opc, unsigned nargs)
Richard Henderson15fa08f2017-11-02 15:19:14 +01003206{
3207 TCGContext *s = tcg_ctx;
Richard Hendersoncb10bc62022-12-18 22:18:32 +01003208 TCGOp *op = NULL;
Richard Henderson15fa08f2017-11-02 15:19:14 +01003209
Richard Hendersoncb10bc62022-12-18 22:18:32 +01003210 if (unlikely(!QTAILQ_EMPTY(&s->free_ops))) {
3211 QTAILQ_FOREACH(op, &s->free_ops, link) {
3212 if (nargs <= op->nargs) {
3213 QTAILQ_REMOVE(&s->free_ops, op, link);
3214 nargs = op->nargs;
3215 goto found;
3216 }
3217 }
Richard Henderson15fa08f2017-11-02 15:19:14 +01003218 }
Richard Hendersoncb10bc62022-12-18 22:18:32 +01003219
3220 /* Most opcodes have 3 or 4 operands: reduce fragmentation. */
3221 nargs = MAX(4, nargs);
3222 op = tcg_malloc(sizeof(TCGOp) + sizeof(TCGArg) * nargs);
3223
3224 found:
Richard Henderson15fa08f2017-11-02 15:19:14 +01003225 memset(op, 0, offsetof(TCGOp, link));
3226 op->opc = opc;
Richard Hendersoncb10bc62022-12-18 22:18:32 +01003227 op->nargs = nargs;
Richard Henderson15fa08f2017-11-02 15:19:14 +01003228
Richard Hendersoncb10bc62022-12-18 22:18:32 +01003229 /* Check for bitfield overflow. */
3230 tcg_debug_assert(op->nargs == nargs);
3231
3232 s->nb_ops++;
Richard Henderson15fa08f2017-11-02 15:19:14 +01003233 return op;
3234}
3235
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003236TCGOp *tcg_emit_op(TCGOpcode opc, unsigned nargs)
Richard Henderson15fa08f2017-11-02 15:19:14 +01003237{
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003238 TCGOp *op = tcg_op_alloc(opc, nargs);
Richard Henderson07843f72024-03-13 13:32:29 -10003239
3240 if (tcg_ctx->emit_before_op) {
3241 QTAILQ_INSERT_BEFORE(tcg_ctx->emit_before_op, op, link);
3242 } else {
3243 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
3244 }
Richard Henderson15fa08f2017-11-02 15:19:14 +01003245 return op;
3246}
3247
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003248TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op,
3249 TCGOpcode opc, unsigned nargs)
Richard Henderson5a184072016-06-23 20:34:33 -07003250{
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003251 TCGOp *new_op = tcg_op_alloc(opc, nargs);
Richard Henderson15fa08f2017-11-02 15:19:14 +01003252 QTAILQ_INSERT_BEFORE(old_op, new_op, link);
Richard Henderson5a184072016-06-23 20:34:33 -07003253 return new_op;
3254}
3255
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003256TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op,
3257 TCGOpcode opc, unsigned nargs)
Richard Henderson5a184072016-06-23 20:34:33 -07003258{
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01003259 TCGOp *new_op = tcg_op_alloc(opc, nargs);
Richard Henderson15fa08f2017-11-02 15:19:14 +01003260 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
Richard Henderson5a184072016-06-23 20:34:33 -07003261 return new_op;
3262}
3263
Richard Henderson968f3052023-03-03 14:22:02 -08003264static void move_label_uses(TCGLabel *to, TCGLabel *from)
3265{
3266 TCGLabelUse *u;
3267
3268 QSIMPLEQ_FOREACH(u, &from->branches, next) {
3269 TCGOp *op = u->op;
3270 switch (op->opc) {
3271 case INDEX_op_br:
3272 op->args[0] = label_arg(to);
3273 break;
3274 case INDEX_op_brcond_i32:
3275 case INDEX_op_brcond_i64:
3276 op->args[3] = label_arg(to);
3277 break;
3278 case INDEX_op_brcond2_i32:
3279 op->args[5] = label_arg(to);
3280 break;
3281 default:
3282 g_assert_not_reached();
3283 }
3284 }
3285
3286 QSIMPLEQ_CONCAT(&to->branches, &from->branches);
3287}
3288
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003289/* Reachable analysis : remove unreachable code. */
Richard Henderson9bbee4c2023-02-24 12:07:33 -10003290static void __attribute__((noinline))
3291reachable_code_pass(TCGContext *s)
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003292{
Richard Henderson4d89d0b2023-01-29 10:37:19 -10003293 TCGOp *op, *op_next, *op_prev;
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003294 bool dead = false;
3295
3296 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
3297 bool remove = dead;
3298 TCGLabel *label;
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003299
3300 switch (op->opc) {
3301 case INDEX_op_set_label:
3302 label = arg_label(op->args[0]);
Richard Henderson4d89d0b2023-01-29 10:37:19 -10003303
3304 /*
Richard Henderson968f3052023-03-03 14:22:02 -08003305 * Note that the first op in the TB is always a load,
3306 * so there is always something before a label.
3307 */
3308 op_prev = QTAILQ_PREV(op, link);
3309
3310 /*
3311 * If we find two sequential labels, move all branches to
3312 * reference the second label and remove the first label.
3313 * Do this before branch to next optimization, so that the
3314 * middle label is out of the way.
3315 */
3316 if (op_prev->opc == INDEX_op_set_label) {
3317 move_label_uses(label, arg_label(op_prev->args[0]));
3318 tcg_op_remove(s, op_prev);
3319 op_prev = QTAILQ_PREV(op, link);
3320 }
3321
3322 /*
Richard Henderson4d89d0b2023-01-29 10:37:19 -10003323 * Optimization can fold conditional branches to unconditional.
3324 * If we find a label which is preceded by an unconditional
3325 * branch to next, remove the branch. We couldn't do this when
3326 * processing the branch because any dead code between the branch
3327 * and label had not yet been removed.
3328 */
Richard Henderson4d89d0b2023-01-29 10:37:19 -10003329 if (op_prev->opc == INDEX_op_br &&
3330 label == arg_label(op_prev->args[0])) {
3331 tcg_op_remove(s, op_prev);
3332 /* Fall through means insns become live again. */
3333 dead = false;
3334 }
3335
Richard Hendersonf85b1fc2023-03-03 13:47:27 -08003336 if (QSIMPLEQ_EMPTY(&label->branches)) {
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003337 /*
3338 * While there is an occasional backward branch, virtually
3339 * all branches generated by the translators are forward.
3340 * Which means that generally we will have already removed
3341 * all references to the label that will be, and there is
3342 * little to be gained by iterating.
3343 */
3344 remove = true;
3345 } else {
3346 /* Once we see a label, insns become live again. */
3347 dead = false;
3348 remove = false;
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003349 }
3350 break;
3351
3352 case INDEX_op_br:
3353 case INDEX_op_exit_tb:
3354 case INDEX_op_goto_ptr:
3355 /* Unconditional branches; everything following is dead. */
3356 dead = true;
3357 break;
3358
3359 case INDEX_op_call:
3360 /* Notice noreturn helper calls, raising exceptions. */
Richard Henderson90163902021-03-18 10:21:45 -06003361 if (tcg_call_flags(op) & TCG_CALL_NO_RETURN) {
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08003362 dead = true;
3363 }
3364 break;
3365
3366 case INDEX_op_insn_start:
3367 /* Never remove -- we need to keep these for unwind. */
3368 remove = false;
3369 break;
3370
3371 default:
3372 break;
3373 }
3374
3375 if (remove) {
3376 tcg_op_remove(s, op);
3377 }
3378 }
3379}
3380
Richard Hendersonc70fbf02016-06-23 20:34:22 -07003381#define TS_DEAD 1
3382#define TS_MEM 2
3383
Richard Henderson5a184072016-06-23 20:34:33 -07003384#define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
3385#define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
3386
Richard Henderson25f49c52018-11-27 12:45:26 -08003387/* For liveness_pass_1, the register preferences for a given temp. */
3388static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
3389{
3390 return ts->state_ptr;
3391}
3392
3393/* For liveness_pass_1, reset the preferences for a given temp to the
3394 * maximal regset for its type.
3395 */
3396static inline void la_reset_pref(TCGTemp *ts)
3397{
3398 *la_temp_pref(ts)
3399 = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
3400}
3401
Aurelien Jarno9c43b682012-10-09 21:53:07 +02003402/* liveness analysis: end of function: all temps are dead, and globals
3403 should be in memory. */
Richard Henderson2616c802018-11-27 13:37:24 -08003404static void la_func_end(TCGContext *s, int ng, int nt)
bellardc896fe22008-02-01 10:05:41 +00003405{
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003406 int i;
3407
3408 for (i = 0; i < ng; ++i) {
3409 s->temps[i].state = TS_DEAD | TS_MEM;
Richard Henderson25f49c52018-11-27 12:45:26 -08003410 la_reset_pref(&s->temps[i]);
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003411 }
3412 for (i = ng; i < nt; ++i) {
3413 s->temps[i].state = TS_DEAD;
Richard Henderson25f49c52018-11-27 12:45:26 -08003414 la_reset_pref(&s->temps[i]);
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003415 }
bellardc896fe22008-02-01 10:05:41 +00003416}
3417
Aurelien Jarno9c43b682012-10-09 21:53:07 +02003418/* liveness analysis: end of basic block: all temps are dead, globals
3419 and local temps should be in memory. */
Richard Henderson2616c802018-11-27 13:37:24 -08003420static void la_bb_end(TCGContext *s, int ng, int nt)
bellard641d5fb2008-05-25 17:24:00 +00003421{
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003422 int i;
bellard641d5fb2008-05-25 17:24:00 +00003423
Richard Hendersonee17db82020-03-29 10:11:56 -07003424 for (i = 0; i < nt; ++i) {
3425 TCGTemp *ts = &s->temps[i];
3426 int state;
3427
3428 switch (ts->kind) {
3429 case TEMP_FIXED:
3430 case TEMP_GLOBAL:
Richard Hendersonf57c6912023-01-29 10:55:52 -10003431 case TEMP_TB:
Richard Hendersonee17db82020-03-29 10:11:56 -07003432 state = TS_DEAD | TS_MEM;
3433 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07003434 case TEMP_EBB:
Richard Hendersonc0522132020-03-29 18:55:52 -07003435 case TEMP_CONST:
Richard Hendersonee17db82020-03-29 10:11:56 -07003436 state = TS_DEAD;
3437 break;
3438 default:
3439 g_assert_not_reached();
3440 }
3441 ts->state = state;
3442 la_reset_pref(ts);
bellard641d5fb2008-05-25 17:24:00 +00003443 }
3444}
3445
Richard Hendersonf65a0612018-11-27 14:00:35 -08003446/* liveness analysis: sync globals back to memory. */
3447static void la_global_sync(TCGContext *s, int ng)
3448{
3449 int i;
3450
3451 for (i = 0; i < ng; ++i) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003452 int state = s->temps[i].state;
3453 s->temps[i].state = state | TS_MEM;
3454 if (state == TS_DEAD) {
3455 /* If the global was previously dead, reset prefs. */
3456 la_reset_pref(&s->temps[i]);
3457 }
Richard Hendersonf65a0612018-11-27 14:00:35 -08003458 }
3459}
3460
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05003461/*
Richard Hendersonc7482432022-03-16 09:34:18 -07003462 * liveness analysis: conditional branch: all temps are dead unless
3463 * explicitly live-across-conditional-branch, globals and local temps
3464 * should be synced.
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05003465 */
3466static void la_bb_sync(TCGContext *s, int ng, int nt)
3467{
3468 la_global_sync(s, ng);
3469
3470 for (int i = ng; i < nt; ++i) {
Richard Hendersonc0522132020-03-29 18:55:52 -07003471 TCGTemp *ts = &s->temps[i];
3472 int state;
3473
3474 switch (ts->kind) {
Richard Hendersonf57c6912023-01-29 10:55:52 -10003475 case TEMP_TB:
Richard Hendersonc0522132020-03-29 18:55:52 -07003476 state = ts->state;
3477 ts->state = state | TS_MEM;
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05003478 if (state != TS_DEAD) {
3479 continue;
3480 }
Richard Hendersonc0522132020-03-29 18:55:52 -07003481 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07003482 case TEMP_EBB:
Richard Hendersonc0522132020-03-29 18:55:52 -07003483 case TEMP_CONST:
3484 continue;
3485 default:
3486 g_assert_not_reached();
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05003487 }
3488 la_reset_pref(&s->temps[i]);
3489 }
3490}
3491
Richard Hendersonf65a0612018-11-27 14:00:35 -08003492/* liveness analysis: sync globals back to memory and kill. */
3493static void la_global_kill(TCGContext *s, int ng)
3494{
3495 int i;
3496
3497 for (i = 0; i < ng; i++) {
3498 s->temps[i].state = TS_DEAD | TS_MEM;
Richard Henderson25f49c52018-11-27 12:45:26 -08003499 la_reset_pref(&s->temps[i]);
3500 }
3501}
3502
3503/* liveness analysis: note live globals crossing calls. */
3504static void la_cross_call(TCGContext *s, int nt)
3505{
3506 TCGRegSet mask = ~tcg_target_call_clobber_regs;
3507 int i;
3508
3509 for (i = 0; i < nt; i++) {
3510 TCGTemp *ts = &s->temps[i];
3511 if (!(ts->state & TS_DEAD)) {
3512 TCGRegSet *pset = la_temp_pref(ts);
3513 TCGRegSet set = *pset;
3514
3515 set &= mask;
3516 /* If the combination is not possible, restart. */
3517 if (set == 0) {
3518 set = tcg_target_available_regs[ts->type] & mask;
3519 }
3520 *pset = set;
3521 }
Richard Hendersonf65a0612018-11-27 14:00:35 -08003522 }
3523}
3524
Richard Henderson874b8572023-01-29 11:50:20 -10003525/*
3526 * Liveness analysis: Verify the lifetime of TEMP_TB, and reduce
3527 * to TEMP_EBB, if possible.
3528 */
3529static void __attribute__((noinline))
3530liveness_pass_0(TCGContext *s)
3531{
3532 void * const multiple_ebb = (void *)(uintptr_t)-1;
3533 int nb_temps = s->nb_temps;
3534 TCGOp *op, *ebb;
3535
3536 for (int i = s->nb_globals; i < nb_temps; ++i) {
3537 s->temps[i].state_ptr = NULL;
3538 }
3539
3540 /*
3541 * Represent each EBB by the op at which it begins. In the case of
3542 * the first EBB, this is the first op, otherwise it is a label.
3543 * Collect the uses of each TEMP_TB: NULL for unused, EBB for use
3544 * within a single EBB, else MULTIPLE_EBB.
3545 */
3546 ebb = QTAILQ_FIRST(&s->ops);
3547 QTAILQ_FOREACH(op, &s->ops, link) {
3548 const TCGOpDef *def;
3549 int nb_oargs, nb_iargs;
3550
3551 switch (op->opc) {
3552 case INDEX_op_set_label:
3553 ebb = op;
3554 continue;
3555 case INDEX_op_discard:
3556 continue;
3557 case INDEX_op_call:
3558 nb_oargs = TCGOP_CALLO(op);
3559 nb_iargs = TCGOP_CALLI(op);
3560 break;
3561 default:
3562 def = &tcg_op_defs[op->opc];
3563 nb_oargs = def->nb_oargs;
3564 nb_iargs = def->nb_iargs;
3565 break;
3566 }
3567
3568 for (int i = 0; i < nb_oargs + nb_iargs; ++i) {
3569 TCGTemp *ts = arg_temp(op->args[i]);
3570
3571 if (ts->kind != TEMP_TB) {
3572 continue;
3573 }
3574 if (ts->state_ptr == NULL) {
3575 ts->state_ptr = ebb;
3576 } else if (ts->state_ptr != ebb) {
3577 ts->state_ptr = multiple_ebb;
3578 }
3579 }
3580 }
3581
3582 /*
3583 * For TEMP_TB that turned out not to be used beyond one EBB,
3584 * reduce the liveness to TEMP_EBB.
3585 */
3586 for (int i = s->nb_globals; i < nb_temps; ++i) {
3587 TCGTemp *ts = &s->temps[i];
3588 if (ts->kind == TEMP_TB && ts->state_ptr != multiple_ebb) {
3589 ts->kind = TEMP_EBB;
3590 }
3591 }
3592}
3593
Richard Hendersona1b3c482016-06-22 15:46:09 -07003594/* Liveness analysis : update the opc_arg_life array to tell if a
bellardc896fe22008-02-01 10:05:41 +00003595 given input arguments is dead. Instructions updating dead
3596 temporaries are removed. */
Richard Henderson9bbee4c2023-02-24 12:07:33 -10003597static void __attribute__((noinline))
3598liveness_pass_1(TCGContext *s)
bellardc896fe22008-02-01 10:05:41 +00003599{
Richard Hendersonc70fbf02016-06-23 20:34:22 -07003600 int nb_globals = s->nb_globals;
Richard Henderson2616c802018-11-27 13:37:24 -08003601 int nb_temps = s->nb_temps;
Richard Henderson15fa08f2017-11-02 15:19:14 +01003602 TCGOp *op, *op_prev;
Richard Henderson25f49c52018-11-27 12:45:26 -08003603 TCGRegSet *prefs;
3604 int i;
3605
3606 prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
3607 for (i = 0; i < nb_temps; ++i) {
3608 s->temps[i].state_ptr = prefs + i;
3609 }
Richard Hendersona1b3c482016-06-22 15:46:09 -07003610
Richard Hendersonae36a242018-11-27 13:45:08 -08003611 /* ??? Should be redundant with the exit_tb that ends the TB. */
Richard Henderson2616c802018-11-27 13:37:24 -08003612 la_func_end(s, nb_globals, nb_temps);
bellardc896fe22008-02-01 10:05:41 +00003613
Paolo Bonzinieae3eb32018-12-06 13:10:34 +01003614 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003615 int nb_iargs, nb_oargs;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003616 TCGOpcode opc_new, opc_new2;
3617 bool have_opc_new2;
Richard Hendersona1b3c482016-06-22 15:46:09 -07003618 TCGLifeData arg_life = 0;
Richard Henderson25f49c52018-11-27 12:45:26 -08003619 TCGTemp *ts;
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003620 TCGOpcode opc = op->opc;
3621 const TCGOpDef *def = &tcg_op_defs[opc];
3622
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003623 switch (opc) {
bellardc896fe22008-02-01 10:05:41 +00003624 case INDEX_op_call:
bellardc6e113f2008-05-17 12:42:15 +00003625 {
Richard Henderson39004a72022-11-11 10:09:37 +10003626 const TCGHelperInfo *info = tcg_call_info(op);
3627 int call_flags = tcg_call_flags(op);
bellardc896fe22008-02-01 10:05:41 +00003628
Richard Hendersoncd9090a2017-11-14 13:02:51 +01003629 nb_oargs = TCGOP_CALLO(op);
3630 nb_iargs = TCGOP_CALLI(op);
bellardc896fe22008-02-01 10:05:41 +00003631
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003632 /* pure functions can be removed if their result is unused */
Aurelien Jarno78505272012-10-09 21:53:08 +02003633 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
Richard Hendersoncf066672014-03-22 20:06:52 -07003634 for (i = 0; i < nb_oargs; i++) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003635 ts = arg_temp(op->args[i]);
3636 if (ts->state != TS_DEAD) {
bellardc6e113f2008-05-17 12:42:15 +00003637 goto do_not_remove_call;
Aurelien Jarno9c43b682012-10-09 21:53:07 +02003638 }
bellardc6e113f2008-05-17 12:42:15 +00003639 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003640 goto do_remove;
Richard Henderson152c35a2018-11-27 13:32:33 -08003641 }
3642 do_not_remove_call:
bellardc6e113f2008-05-17 12:42:15 +00003643
Richard Henderson25f49c52018-11-27 12:45:26 -08003644 /* Output args are dead. */
Richard Henderson152c35a2018-11-27 13:32:33 -08003645 for (i = 0; i < nb_oargs; i++) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003646 ts = arg_temp(op->args[i]);
3647 if (ts->state & TS_DEAD) {
Richard Henderson152c35a2018-11-27 13:32:33 -08003648 arg_life |= DEAD_ARG << i;
bellardc6e113f2008-05-17 12:42:15 +00003649 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003650 if (ts->state & TS_MEM) {
Richard Henderson152c35a2018-11-27 13:32:33 -08003651 arg_life |= SYNC_ARG << i;
3652 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003653 ts->state = TS_DEAD;
3654 la_reset_pref(ts);
Richard Henderson152c35a2018-11-27 13:32:33 -08003655 }
Aurelien Jarno78505272012-10-09 21:53:08 +02003656
Richard Henderson31fd8842022-11-11 15:10:51 +10003657 /* Not used -- it will be tcg_target_call_oarg_reg(). */
3658 memset(op->output_pref, 0, sizeof(op->output_pref));
3659
Richard Henderson152c35a2018-11-27 13:32:33 -08003660 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
3661 TCG_CALL_NO_READ_GLOBALS))) {
Richard Hendersonf65a0612018-11-27 14:00:35 -08003662 la_global_kill(s, nb_globals);
Richard Henderson152c35a2018-11-27 13:32:33 -08003663 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
Richard Hendersonf65a0612018-11-27 14:00:35 -08003664 la_global_sync(s, nb_globals);
Richard Henderson152c35a2018-11-27 13:32:33 -08003665 }
aurel32b9c18f52009-04-06 12:33:59 +00003666
Richard Henderson25f49c52018-11-27 12:45:26 -08003667 /* Record arguments that die in this helper. */
Richard Henderson152c35a2018-11-27 13:32:33 -08003668 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003669 ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +10003670 if (ts->state & TS_DEAD) {
Richard Henderson152c35a2018-11-27 13:32:33 -08003671 arg_life |= DEAD_ARG << i;
bellardc6e113f2008-05-17 12:42:15 +00003672 }
Richard Henderson152c35a2018-11-27 13:32:33 -08003673 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003674
3675 /* For all live registers, remove call-clobbered prefs. */
3676 la_cross_call(s, nb_temps);
3677
Richard Henderson39004a72022-11-11 10:09:37 +10003678 /*
3679 * Input arguments are live for preceding opcodes.
3680 *
3681 * For those arguments that die, and will be allocated in
3682 * registers, clear the register set for that arg, to be
3683 * filled in below. For args that will be on the stack,
3684 * reset to any available reg. Process arguments in reverse
3685 * order so that if a temp is used more than once, the stack
3686 * reset to max happens before the register reset to 0.
3687 */
3688 for (i = nb_iargs - 1; i >= 0; i--) {
3689 const TCGCallArgumentLoc *loc = &info->in[i];
3690 ts = arg_temp(op->args[nb_oargs + i]);
Richard Henderson25f49c52018-11-27 12:45:26 -08003691
Richard Henderson39004a72022-11-11 10:09:37 +10003692 if (ts->state & TS_DEAD) {
3693 switch (loc->kind) {
3694 case TCG_CALL_ARG_NORMAL:
3695 case TCG_CALL_ARG_EXTEND_U:
3696 case TCG_CALL_ARG_EXTEND_S:
Richard Henderson338b61e2023-04-08 17:28:07 -07003697 if (arg_slot_reg_p(loc->arg_slot)) {
Richard Henderson39004a72022-11-11 10:09:37 +10003698 *la_temp_pref(ts) = 0;
3699 break;
3700 }
3701 /* fall through */
3702 default:
3703 *la_temp_pref(ts) =
3704 tcg_target_available_regs[ts->type];
3705 break;
3706 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003707 ts->state &= ~TS_DEAD;
3708 }
3709 }
3710
Richard Henderson39004a72022-11-11 10:09:37 +10003711 /*
3712 * For each input argument, add its input register to prefs.
3713 * If a temp is used once, this produces a single set bit;
3714 * if a temp is used multiple times, this produces a set.
3715 */
3716 for (i = 0; i < nb_iargs; i++) {
3717 const TCGCallArgumentLoc *loc = &info->in[i];
3718 ts = arg_temp(op->args[nb_oargs + i]);
3719
3720 switch (loc->kind) {
3721 case TCG_CALL_ARG_NORMAL:
3722 case TCG_CALL_ARG_EXTEND_U:
3723 case TCG_CALL_ARG_EXTEND_S:
Richard Henderson338b61e2023-04-08 17:28:07 -07003724 if (arg_slot_reg_p(loc->arg_slot)) {
Richard Henderson39004a72022-11-11 10:09:37 +10003725 tcg_regset_set_reg(*la_temp_pref(ts),
3726 tcg_target_call_iarg_regs[loc->arg_slot]);
3727 }
3728 break;
3729 default:
3730 break;
Aurelien Jarnoc19f47b2015-06-04 21:47:08 +02003731 }
bellardc896fe22008-02-01 10:05:41 +00003732 }
bellardc896fe22008-02-01 10:05:41 +00003733 }
bellardc896fe22008-02-01 10:05:41 +00003734 break;
Richard Henderson765b8422015-08-29 12:37:33 -07003735 case INDEX_op_insn_start:
bellardc896fe22008-02-01 10:05:41 +00003736 break;
bellard5ff9d6a2008-02-04 00:37:54 +00003737 case INDEX_op_discard:
bellard5ff9d6a2008-02-04 00:37:54 +00003738 /* mark the temporary as dead */
Richard Henderson25f49c52018-11-27 12:45:26 -08003739 ts = arg_temp(op->args[0]);
3740 ts->state = TS_DEAD;
3741 la_reset_pref(ts);
bellard5ff9d6a2008-02-04 00:37:54 +00003742 break;
Richard Henderson1305c452012-10-02 11:32:29 -07003743
3744 case INDEX_op_add2_i32:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003745 opc_new = INDEX_op_add_i32;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003746 goto do_addsub2;
Richard Henderson1305c452012-10-02 11:32:29 -07003747 case INDEX_op_sub2_i32:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003748 opc_new = INDEX_op_sub_i32;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003749 goto do_addsub2;
3750 case INDEX_op_add2_i64:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003751 opc_new = INDEX_op_add_i64;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003752 goto do_addsub2;
3753 case INDEX_op_sub2_i64:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003754 opc_new = INDEX_op_sub_i64;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003755 do_addsub2:
Richard Henderson1305c452012-10-02 11:32:29 -07003756 nb_iargs = 4;
3757 nb_oargs = 2;
3758 /* Test if the high part of the operation is dead, but not
3759 the low part. The result can be optimized to a simple
3760 add or sub. This happens often for x86_64 guest when the
3761 cpu mode is set to 32 bit. */
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003762 if (arg_temp(op->args[1])->state == TS_DEAD) {
3763 if (arg_temp(op->args[0])->state == TS_DEAD) {
Richard Henderson1305c452012-10-02 11:32:29 -07003764 goto do_remove;
3765 }
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003766 /* Replace the opcode and adjust the args in place,
3767 leaving 3 unused args at the end. */
3768 op->opc = opc = opc_new;
Richard Hendersonefee3742016-12-08 13:12:08 -08003769 op->args[1] = op->args[2];
3770 op->args[2] = op->args[4];
Richard Henderson1305c452012-10-02 11:32:29 -07003771 /* Fall through and mark the single-word operation live. */
3772 nb_iargs = 2;
3773 nb_oargs = 1;
3774 }
3775 goto do_not_remove;
3776
Richard Henderson14149682012-10-02 11:32:30 -07003777 case INDEX_op_mulu2_i32:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003778 opc_new = INDEX_op_mul_i32;
3779 opc_new2 = INDEX_op_muluh_i32;
3780 have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
Richard Henderson03271522013-08-14 14:35:56 -07003781 goto do_mul2;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003782 case INDEX_op_muls2_i32:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003783 opc_new = INDEX_op_mul_i32;
3784 opc_new2 = INDEX_op_mulsh_i32;
3785 have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003786 goto do_mul2;
3787 case INDEX_op_mulu2_i64:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003788 opc_new = INDEX_op_mul_i64;
3789 opc_new2 = INDEX_op_muluh_i64;
3790 have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
Richard Henderson03271522013-08-14 14:35:56 -07003791 goto do_mul2;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003792 case INDEX_op_muls2_i64:
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003793 opc_new = INDEX_op_mul_i64;
3794 opc_new2 = INDEX_op_mulsh_i64;
3795 have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
Richard Henderson03271522013-08-14 14:35:56 -07003796 goto do_mul2;
Richard Hendersonf1fae402013-02-19 23:52:02 -08003797 do_mul2:
Richard Henderson14149682012-10-02 11:32:30 -07003798 nb_iargs = 2;
3799 nb_oargs = 2;
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003800 if (arg_temp(op->args[1])->state == TS_DEAD) {
3801 if (arg_temp(op->args[0])->state == TS_DEAD) {
Richard Henderson03271522013-08-14 14:35:56 -07003802 /* Both parts of the operation are dead. */
Richard Henderson14149682012-10-02 11:32:30 -07003803 goto do_remove;
3804 }
Richard Henderson03271522013-08-14 14:35:56 -07003805 /* The high part of the operation is dead; generate the low. */
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003806 op->opc = opc = opc_new;
Richard Hendersonefee3742016-12-08 13:12:08 -08003807 op->args[1] = op->args[2];
3808 op->args[2] = op->args[3];
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003809 } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003810 /* The low part of the operation is dead; generate the high. */
3811 op->opc = opc = opc_new2;
Richard Hendersonefee3742016-12-08 13:12:08 -08003812 op->args[0] = op->args[1];
3813 op->args[1] = op->args[2];
3814 op->args[2] = op->args[3];
Richard Henderson03271522013-08-14 14:35:56 -07003815 } else {
3816 goto do_not_remove;
Richard Henderson14149682012-10-02 11:32:30 -07003817 }
Richard Henderson03271522013-08-14 14:35:56 -07003818 /* Mark the single-word operation live. */
3819 nb_oargs = 1;
Richard Henderson14149682012-10-02 11:32:30 -07003820 goto do_not_remove;
3821
bellardc896fe22008-02-01 10:05:41 +00003822 default:
Richard Henderson1305c452012-10-02 11:32:29 -07003823 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
aurel3249516bc2008-12-07 18:15:45 +00003824 nb_iargs = def->nb_iargs;
3825 nb_oargs = def->nb_oargs;
bellardc896fe22008-02-01 10:05:41 +00003826
aurel3249516bc2008-12-07 18:15:45 +00003827 /* Test if the operation can be removed because all
3828 its outputs are dead. We assume that nb_oargs == 0
3829 implies side effects */
3830 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07003831 for (i = 0; i < nb_oargs; i++) {
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003832 if (arg_temp(op->args[i])->state != TS_DEAD) {
aurel3249516bc2008-12-07 18:15:45 +00003833 goto do_not_remove;
Aurelien Jarno9c43b682012-10-09 21:53:07 +02003834 }
bellardc896fe22008-02-01 10:05:41 +00003835 }
Richard Henderson152c35a2018-11-27 13:32:33 -08003836 goto do_remove;
3837 }
3838 goto do_not_remove;
aurel3249516bc2008-12-07 18:15:45 +00003839
Richard Henderson152c35a2018-11-27 13:32:33 -08003840 do_remove:
3841 tcg_op_remove(s, op);
3842 break;
aurel3249516bc2008-12-07 18:15:45 +00003843
Richard Henderson152c35a2018-11-27 13:32:33 -08003844 do_not_remove:
Richard Henderson152c35a2018-11-27 13:32:33 -08003845 for (i = 0; i < nb_oargs; i++) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003846 ts = arg_temp(op->args[i]);
3847
3848 /* Remember the preference of the uses that followed. */
Richard Henderson31fd8842022-11-11 15:10:51 +10003849 if (i < ARRAY_SIZE(op->output_pref)) {
3850 op->output_pref[i] = *la_temp_pref(ts);
3851 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003852
3853 /* Output args are dead. */
3854 if (ts->state & TS_DEAD) {
Richard Henderson152c35a2018-11-27 13:32:33 -08003855 arg_life |= DEAD_ARG << i;
Aurelien Jarnoc19f47b2015-06-04 21:47:08 +02003856 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003857 if (ts->state & TS_MEM) {
Richard Henderson152c35a2018-11-27 13:32:33 -08003858 arg_life |= SYNC_ARG << i;
aurel3249516bc2008-12-07 18:15:45 +00003859 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003860 ts->state = TS_DEAD;
3861 la_reset_pref(ts);
Richard Henderson152c35a2018-11-27 13:32:33 -08003862 }
3863
Richard Henderson25f49c52018-11-27 12:45:26 -08003864 /* If end of basic block, update. */
Richard Hendersonae36a242018-11-27 13:45:08 -08003865 if (def->flags & TCG_OPF_BB_EXIT) {
3866 la_func_end(s, nb_globals, nb_temps);
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05003867 } else if (def->flags & TCG_OPF_COND_BRANCH) {
3868 la_bb_sync(s, nb_globals, nb_temps);
Richard Hendersonae36a242018-11-27 13:45:08 -08003869 } else if (def->flags & TCG_OPF_BB_END) {
Richard Henderson2616c802018-11-27 13:37:24 -08003870 la_bb_end(s, nb_globals, nb_temps);
Richard Henderson152c35a2018-11-27 13:32:33 -08003871 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
Richard Hendersonf65a0612018-11-27 14:00:35 -08003872 la_global_sync(s, nb_globals);
Richard Henderson25f49c52018-11-27 12:45:26 -08003873 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3874 la_cross_call(s, nb_temps);
3875 }
Richard Henderson152c35a2018-11-27 13:32:33 -08003876 }
3877
Richard Henderson25f49c52018-11-27 12:45:26 -08003878 /* Record arguments that die in this opcode. */
Richard Henderson152c35a2018-11-27 13:32:33 -08003879 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003880 ts = arg_temp(op->args[i]);
3881 if (ts->state & TS_DEAD) {
Richard Henderson152c35a2018-11-27 13:32:33 -08003882 arg_life |= DEAD_ARG << i;
3883 }
3884 }
Richard Henderson25f49c52018-11-27 12:45:26 -08003885
3886 /* Input arguments are live for preceding opcodes. */
Richard Henderson152c35a2018-11-27 13:32:33 -08003887 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
Richard Henderson25f49c52018-11-27 12:45:26 -08003888 ts = arg_temp(op->args[i]);
3889 if (ts->state & TS_DEAD) {
3890 /* For operands that were dead, initially allow
3891 all regs for the type. */
3892 *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
3893 ts->state &= ~TS_DEAD;
3894 }
3895 }
3896
3897 /* Incorporate constraints for this operand. */
3898 switch (opc) {
3899 case INDEX_op_mov_i32:
3900 case INDEX_op_mov_i64:
3901 /* Note that these are TCG_OPF_NOT_PRESENT and do not
3902 have proper constraints. That said, special case
3903 moves to propagate preferences backward. */
3904 if (IS_DEAD_ARG(1)) {
3905 *la_temp_pref(arg_temp(op->args[0]))
3906 = *la_temp_pref(arg_temp(op->args[1]));
3907 }
3908 break;
3909
3910 default:
3911 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3912 const TCGArgConstraint *ct = &def->args_ct[i];
3913 TCGRegSet set, *pset;
3914
3915 ts = arg_temp(op->args[i]);
3916 pset = la_temp_pref(ts);
3917 set = *pset;
3918
Richard Henderson9be0d082020-09-03 15:19:03 -07003919 set &= ct->regs;
Richard Hendersonbc2b17e2019-04-04 19:34:19 -07003920 if (ct->ialias) {
Richard Henderson31fd8842022-11-11 15:10:51 +10003921 set &= output_pref(op, ct->alias_index);
Richard Henderson25f49c52018-11-27 12:45:26 -08003922 }
3923 /* If the combination is not possible, restart. */
3924 if (set == 0) {
Richard Henderson9be0d082020-09-03 15:19:03 -07003925 set = ct->regs;
Richard Henderson25f49c52018-11-27 12:45:26 -08003926 }
3927 *pset = set;
3928 }
3929 break;
bellardc896fe22008-02-01 10:05:41 +00003930 }
3931 break;
3932 }
Richard Hendersonbee158c2016-06-22 20:43:29 -07003933 op->life = arg_life;
Evgeny Voevodin1ff0a2c2012-11-12 13:27:48 +04003934 }
bellardc896fe22008-02-01 10:05:41 +00003935}
bellardc896fe22008-02-01 10:05:41 +00003936
Richard Henderson5a184072016-06-23 20:34:33 -07003937/* Liveness analysis: Convert indirect regs to direct temporaries. */
Richard Henderson9bbee4c2023-02-24 12:07:33 -10003938static bool __attribute__((noinline))
3939liveness_pass_2(TCGContext *s)
Richard Henderson5a184072016-06-23 20:34:33 -07003940{
3941 int nb_globals = s->nb_globals;
Richard Henderson15fa08f2017-11-02 15:19:14 +01003942 int nb_temps, i;
Richard Henderson5a184072016-06-23 20:34:33 -07003943 bool changes = false;
Richard Henderson15fa08f2017-11-02 15:19:14 +01003944 TCGOp *op, *op_next;
Richard Henderson5a184072016-06-23 20:34:33 -07003945
Richard Henderson5a184072016-06-23 20:34:33 -07003946 /* Create a temporary for each indirect global. */
3947 for (i = 0; i < nb_globals; ++i) {
3948 TCGTemp *its = &s->temps[i];
3949 if (its->indirect_reg) {
3950 TCGTemp *dts = tcg_temp_alloc(s);
3951 dts->type = its->type;
3952 dts->base_type = its->base_type;
Richard Hendersone1e64652023-02-03 12:58:12 -10003953 dts->temp_subindex = its->temp_subindex;
Richard Hendersonc7482432022-03-16 09:34:18 -07003954 dts->kind = TEMP_EBB;
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003955 its->state_ptr = dts;
3956 } else {
3957 its->state_ptr = NULL;
Richard Henderson5a184072016-06-23 20:34:33 -07003958 }
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003959 /* All globals begin dead. */
3960 its->state = TS_DEAD;
Richard Henderson5a184072016-06-23 20:34:33 -07003961 }
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003962 for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
3963 TCGTemp *its = &s->temps[i];
3964 its->state_ptr = NULL;
3965 its->state = TS_DEAD;
3966 }
Richard Henderson5a184072016-06-23 20:34:33 -07003967
Richard Henderson15fa08f2017-11-02 15:19:14 +01003968 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
Richard Henderson5a184072016-06-23 20:34:33 -07003969 TCGOpcode opc = op->opc;
3970 const TCGOpDef *def = &tcg_op_defs[opc];
3971 TCGLifeData arg_life = op->life;
3972 int nb_iargs, nb_oargs, call_flags;
Richard Hendersonb83eabe2016-11-01 15:56:04 -06003973 TCGTemp *arg_ts, *dir_ts;
Richard Henderson5a184072016-06-23 20:34:33 -07003974
Richard Henderson5a184072016-06-23 20:34:33 -07003975 if (opc == INDEX_op_call) {
Richard Hendersoncd9090a2017-11-14 13:02:51 +01003976 nb_oargs = TCGOP_CALLO(op);
3977 nb_iargs = TCGOP_CALLI(op);
Richard Henderson90163902021-03-18 10:21:45 -06003978 call_flags = tcg_call_flags(op);
Richard Henderson5a184072016-06-23 20:34:33 -07003979 } else {
3980 nb_iargs = def->nb_iargs;
3981 nb_oargs = def->nb_oargs;
3982
3983 /* Set flags similar to how calls require. */
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05003984 if (def->flags & TCG_OPF_COND_BRANCH) {
3985 /* Like reading globals: sync_globals */
3986 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3987 } else if (def->flags & TCG_OPF_BB_END) {
Richard Henderson5a184072016-06-23 20:34:33 -07003988 /* Like writing globals: save_globals */
3989 call_flags = 0;
3990 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3991 /* Like reading globals: sync_globals */
3992 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3993 } else {
3994 /* No effect on globals. */
3995 call_flags = (TCG_CALL_NO_READ_GLOBALS |
3996 TCG_CALL_NO_WRITE_GLOBALS);
3997 }
3998 }
3999
4000 /* Make sure that input arguments are available. */
4001 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
Richard Hendersonb83eabe2016-11-01 15:56:04 -06004002 arg_ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +10004003 dir_ts = arg_ts->state_ptr;
4004 if (dir_ts && arg_ts->state == TS_DEAD) {
4005 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
4006 ? INDEX_op_ld_i32
4007 : INDEX_op_ld_i64);
4008 TCGOp *lop = tcg_op_insert_before(s, op, lopc, 3);
Richard Henderson5a184072016-06-23 20:34:33 -07004009
Richard Henderson39004a72022-11-11 10:09:37 +10004010 lop->args[0] = temp_arg(dir_ts);
4011 lop->args[1] = temp_arg(arg_ts->mem_base);
4012 lop->args[2] = arg_ts->mem_offset;
Richard Henderson5a184072016-06-23 20:34:33 -07004013
Richard Henderson39004a72022-11-11 10:09:37 +10004014 /* Loaded, but synced with memory. */
4015 arg_ts->state = TS_MEM;
Richard Henderson5a184072016-06-23 20:34:33 -07004016 }
4017 }
4018
4019 /* Perform input replacement, and mark inputs that became dead.
4020 No action is required except keeping temp_state up to date
4021 so that we reload when needed. */
4022 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
Richard Hendersonb83eabe2016-11-01 15:56:04 -06004023 arg_ts = arg_temp(op->args[i]);
Richard Henderson39004a72022-11-11 10:09:37 +10004024 dir_ts = arg_ts->state_ptr;
4025 if (dir_ts) {
4026 op->args[i] = temp_arg(dir_ts);
4027 changes = true;
4028 if (IS_DEAD_ARG(i)) {
4029 arg_ts->state = TS_DEAD;
Richard Henderson5a184072016-06-23 20:34:33 -07004030 }
4031 }
4032 }
4033
4034 /* Liveness analysis should ensure that the following are
4035 all correct, for call sites and basic block end points. */
4036 if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
4037 /* Nothing to do */
4038 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
4039 for (i = 0; i < nb_globals; ++i) {
4040 /* Liveness should see that globals are synced back,
4041 that is, either TS_DEAD or TS_MEM. */
Richard Hendersonb83eabe2016-11-01 15:56:04 -06004042 arg_ts = &s->temps[i];
4043 tcg_debug_assert(arg_ts->state_ptr == 0
4044 || arg_ts->state != 0);
Richard Henderson5a184072016-06-23 20:34:33 -07004045 }
4046 } else {
4047 for (i = 0; i < nb_globals; ++i) {
4048 /* Liveness should see that globals are saved back,
4049 that is, TS_DEAD, waiting to be reloaded. */
Richard Hendersonb83eabe2016-11-01 15:56:04 -06004050 arg_ts = &s->temps[i];
4051 tcg_debug_assert(arg_ts->state_ptr == 0
4052 || arg_ts->state == TS_DEAD);
Richard Henderson5a184072016-06-23 20:34:33 -07004053 }
4054 }
4055
4056 /* Outputs become available. */
Richard Henderson61f15c42020-04-23 12:27:53 -07004057 if (opc == INDEX_op_mov_i32 || opc == INDEX_op_mov_i64) {
4058 arg_ts = arg_temp(op->args[0]);
Richard Hendersonb83eabe2016-11-01 15:56:04 -06004059 dir_ts = arg_ts->state_ptr;
Richard Henderson61f15c42020-04-23 12:27:53 -07004060 if (dir_ts) {
4061 op->args[0] = temp_arg(dir_ts);
4062 changes = true;
4063
4064 /* The output is now live and modified. */
4065 arg_ts->state = 0;
4066
4067 if (NEED_SYNC_ARG(0)) {
4068 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
4069 ? INDEX_op_st_i32
4070 : INDEX_op_st_i64);
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01004071 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 3);
Richard Henderson61f15c42020-04-23 12:27:53 -07004072 TCGTemp *out_ts = dir_ts;
4073
4074 if (IS_DEAD_ARG(0)) {
4075 out_ts = arg_temp(op->args[1]);
4076 arg_ts->state = TS_DEAD;
4077 tcg_op_remove(s, op);
4078 } else {
4079 arg_ts->state = TS_MEM;
4080 }
4081
4082 sop->args[0] = temp_arg(out_ts);
4083 sop->args[1] = temp_arg(arg_ts->mem_base);
4084 sop->args[2] = arg_ts->mem_offset;
4085 } else {
4086 tcg_debug_assert(!IS_DEAD_ARG(0));
4087 }
Richard Henderson5a184072016-06-23 20:34:33 -07004088 }
Richard Henderson61f15c42020-04-23 12:27:53 -07004089 } else {
4090 for (i = 0; i < nb_oargs; i++) {
4091 arg_ts = arg_temp(op->args[i]);
4092 dir_ts = arg_ts->state_ptr;
4093 if (!dir_ts) {
4094 continue;
4095 }
4096 op->args[i] = temp_arg(dir_ts);
4097 changes = true;
Richard Henderson5a184072016-06-23 20:34:33 -07004098
Richard Henderson61f15c42020-04-23 12:27:53 -07004099 /* The output is now live and modified. */
4100 arg_ts->state = 0;
Richard Henderson5a184072016-06-23 20:34:33 -07004101
Richard Henderson61f15c42020-04-23 12:27:53 -07004102 /* Sync outputs upon their last write. */
4103 if (NEED_SYNC_ARG(i)) {
4104 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
4105 ? INDEX_op_st_i32
4106 : INDEX_op_st_i64);
Philippe Mathieu-Daudéd4478942022-12-18 22:18:31 +01004107 TCGOp *sop = tcg_op_insert_after(s, op, sopc, 3);
Richard Henderson5a184072016-06-23 20:34:33 -07004108
Richard Henderson61f15c42020-04-23 12:27:53 -07004109 sop->args[0] = temp_arg(dir_ts);
4110 sop->args[1] = temp_arg(arg_ts->mem_base);
4111 sop->args[2] = arg_ts->mem_offset;
Richard Henderson5a184072016-06-23 20:34:33 -07004112
Richard Henderson61f15c42020-04-23 12:27:53 -07004113 arg_ts->state = TS_MEM;
4114 }
4115 /* Drop outputs that are dead. */
4116 if (IS_DEAD_ARG(i)) {
4117 arg_ts->state = TS_DEAD;
4118 }
Richard Henderson5a184072016-06-23 20:34:33 -07004119 }
4120 }
4121 }
4122
4123 return changes;
4124}
4125
Richard Henderson2272e4a2016-11-09 15:25:09 +01004126static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
bellardc896fe22008-02-01 10:05:41 +00004127{
Richard Henderson31c96412022-10-19 12:41:15 +10004128 intptr_t off;
Richard Henderson273eb502022-10-19 12:03:40 +10004129 int size, align;
Richard Hendersonc1c09192021-06-18 21:53:27 -07004130
Richard Henderson273eb502022-10-19 12:03:40 +10004131 /* When allocating an object, look at the full type. */
4132 size = tcg_type_size(ts->base_type);
4133 switch (ts->base_type) {
Richard Hendersonc1c09192021-06-18 21:53:27 -07004134 case TCG_TYPE_I32:
Richard Henderson31c96412022-10-19 12:41:15 +10004135 align = 4;
Richard Hendersonc1c09192021-06-18 21:53:27 -07004136 break;
4137 case TCG_TYPE_I64:
4138 case TCG_TYPE_V64:
Richard Henderson31c96412022-10-19 12:41:15 +10004139 align = 8;
Richard Hendersonc1c09192021-06-18 21:53:27 -07004140 break;
Richard Henderson43eef722022-10-20 08:03:41 +10004141 case TCG_TYPE_I128:
Richard Hendersonc1c09192021-06-18 21:53:27 -07004142 case TCG_TYPE_V128:
Richard Hendersonc1c09192021-06-18 21:53:27 -07004143 case TCG_TYPE_V256:
Richard Henderson43eef722022-10-20 08:03:41 +10004144 /*
4145 * Note that we do not require aligned storage for V256,
4146 * and that we provide alignment for I128 to match V128,
4147 * even if that's above what the host ABI requires.
4148 */
Richard Henderson31c96412022-10-19 12:41:15 +10004149 align = 16;
Richard Hendersonc1c09192021-06-18 21:53:27 -07004150 break;
4151 default:
4152 g_assert_not_reached();
Blue Swirlb591dc52011-05-14 14:03:22 +00004153 }
Richard Hendersonc1c09192021-06-18 21:53:27 -07004154
Richard Hendersonb9537d52021-09-12 10:49:25 -07004155 /*
4156 * Assume the stack is sufficiently aligned.
4157 * This affects e.g. ARM NEON, where we have 8 byte stack alignment
4158 * and do not require 16 byte vector alignment. This seems slightly
4159 * easier than fully parameterizing the above switch statement.
4160 */
4161 align = MIN(TCG_TARGET_STACK_ALIGN, align);
Richard Hendersonc1c09192021-06-18 21:53:27 -07004162 off = ROUND_UP(s->current_frame_offset, align);
Richard Henderson732d5892021-06-19 06:32:03 -07004163
4164 /* If we've exhausted the stack frame, restart with a smaller TB. */
4165 if (off + size > s->frame_end) {
4166 tcg_raise_tb_overflow(s);
4167 }
Richard Hendersonc1c09192021-06-18 21:53:27 -07004168 s->current_frame_offset = off + size;
Richard Henderson9defd1b2021-06-18 16:49:26 -07004169#if defined(__sparc__)
Richard Henderson273eb502022-10-19 12:03:40 +10004170 off += TCG_TARGET_STACK_BIAS;
Richard Henderson9defd1b2021-06-18 16:49:26 -07004171#endif
Richard Henderson273eb502022-10-19 12:03:40 +10004172
4173 /* If the object was subdivided, assign memory to all the parts. */
4174 if (ts->base_type != ts->type) {
4175 int part_size = tcg_type_size(ts->type);
4176 int part_count = size / part_size;
4177
4178 /*
4179 * Each part is allocated sequentially in tcg_temp_new_internal.
4180 * Jump back to the first part by subtracting the current index.
4181 */
4182 ts -= ts->temp_subindex;
4183 for (int i = 0; i < part_count; ++i) {
4184 ts[i].mem_offset = off + i * part_size;
4185 ts[i].mem_base = s->frame_temp;
4186 ts[i].mem_allocated = 1;
4187 }
4188 } else {
4189 ts->mem_offset = off;
4190 ts->mem_base = s->frame_temp;
4191 ts->mem_allocated = 1;
4192 }
bellardc896fe22008-02-01 10:05:41 +00004193}
4194
Richard Henderson098859f2022-12-01 01:05:05 -08004195/* Assign @reg to @ts, and update reg_to_temp[]. */
4196static void set_temp_val_reg(TCGContext *s, TCGTemp *ts, TCGReg reg)
4197{
4198 if (ts->val_type == TEMP_VAL_REG) {
4199 TCGReg old = ts->reg;
4200 tcg_debug_assert(s->reg_to_temp[old] == ts);
4201 if (old == reg) {
4202 return;
4203 }
4204 s->reg_to_temp[old] = NULL;
4205 }
4206 tcg_debug_assert(s->reg_to_temp[reg] == NULL);
4207 s->reg_to_temp[reg] = ts;
4208 ts->val_type = TEMP_VAL_REG;
4209 ts->reg = reg;
4210}
4211
4212/* Assign a non-register value type to @ts, and update reg_to_temp[]. */
4213static void set_temp_val_nonreg(TCGContext *s, TCGTemp *ts, TCGTempVal type)
4214{
4215 tcg_debug_assert(type != TEMP_VAL_REG);
4216 if (ts->val_type == TEMP_VAL_REG) {
4217 TCGReg reg = ts->reg;
4218 tcg_debug_assert(s->reg_to_temp[reg] == ts);
4219 s->reg_to_temp[reg] = NULL;
4220 }
4221 ts->val_type = type;
4222}
4223
Richard Hendersonb7224522018-11-27 07:48:06 -08004224static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
Richard Hendersonb3915db2013-09-19 10:36:18 -07004225
Richard Henderson59d7c142016-06-19 22:59:13 -07004226/* Mark a temporary as free or dead. If 'free_or_dead' is negative,
4227 mark it free; otherwise mark it dead. */
4228static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
bellardc896fe22008-02-01 10:05:41 +00004229{
Richard Hendersonc0522132020-03-29 18:55:52 -07004230 TCGTempVal new_type;
4231
4232 switch (ts->kind) {
4233 case TEMP_FIXED:
Richard Henderson59d7c142016-06-19 22:59:13 -07004234 return;
Richard Hendersonc0522132020-03-29 18:55:52 -07004235 case TEMP_GLOBAL:
Richard Hendersonf57c6912023-01-29 10:55:52 -10004236 case TEMP_TB:
Richard Hendersonc0522132020-03-29 18:55:52 -07004237 new_type = TEMP_VAL_MEM;
4238 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07004239 case TEMP_EBB:
Richard Hendersonc0522132020-03-29 18:55:52 -07004240 new_type = free_or_dead < 0 ? TEMP_VAL_MEM : TEMP_VAL_DEAD;
4241 break;
4242 case TEMP_CONST:
4243 new_type = TEMP_VAL_CONST;
4244 break;
4245 default:
4246 g_assert_not_reached();
Richard Henderson59d7c142016-06-19 22:59:13 -07004247 }
Richard Henderson098859f2022-12-01 01:05:05 -08004248 set_temp_val_nonreg(s, ts, new_type);
Richard Henderson59d7c142016-06-19 22:59:13 -07004249}
bellardc896fe22008-02-01 10:05:41 +00004250
Richard Henderson59d7c142016-06-19 22:59:13 -07004251/* Mark a temporary as dead. */
4252static inline void temp_dead(TCGContext *s, TCGTemp *ts)
4253{
4254 temp_free_or_dead(s, ts, 1);
4255}
4256
4257/* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
4258 registers needs to be allocated to store a constant. If 'free_or_dead'
4259 is non-zero, subsequently release the temporary; if it is positive, the
4260 temp is dead; if it is negative, the temp is free. */
Richard Henderson98b4e182018-11-27 15:35:04 -08004261static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
4262 TCGRegSet preferred_regs, int free_or_dead)
Richard Henderson59d7c142016-06-19 22:59:13 -07004263{
Richard Hendersonc0522132020-03-29 18:55:52 -07004264 if (!temp_readonly(ts) && !ts->mem_coherent) {
Aurelien Jarno7f6ceed2012-10-09 21:53:06 +02004265 if (!ts->mem_allocated) {
Richard Henderson2272e4a2016-11-09 15:25:09 +01004266 temp_allocate_frame(s, ts);
Richard Henderson59d7c142016-06-19 22:59:13 -07004267 }
Richard Henderson59d7c142016-06-19 22:59:13 -07004268 switch (ts->val_type) {
4269 case TEMP_VAL_CONST:
4270 /* If we're going to free the temp immediately, then we won't
4271 require it later in a register, so attempt to store the
4272 constant to memory directly. */
4273 if (free_or_dead
4274 && tcg_out_sti(s, ts->type, ts->val,
4275 ts->mem_base->reg, ts->mem_offset)) {
4276 break;
4277 }
4278 temp_load(s, ts, tcg_target_available_regs[ts->type],
Richard Henderson98b4e182018-11-27 15:35:04 -08004279 allocated_regs, preferred_regs);
Richard Henderson59d7c142016-06-19 22:59:13 -07004280 /* fallthrough */
4281
4282 case TEMP_VAL_REG:
4283 tcg_out_st(s, ts->type, ts->reg,
4284 ts->mem_base->reg, ts->mem_offset);
4285 break;
4286
4287 case TEMP_VAL_MEM:
4288 break;
4289
4290 case TEMP_VAL_DEAD:
4291 default:
Richard Henderson732e89f2023-04-05 12:09:14 -07004292 g_assert_not_reached();
Richard Henderson59d7c142016-06-19 22:59:13 -07004293 }
4294 ts->mem_coherent = 1;
Aurelien Jarno7f6ceed2012-10-09 21:53:06 +02004295 }
Richard Henderson59d7c142016-06-19 22:59:13 -07004296 if (free_or_dead) {
4297 temp_free_or_dead(s, ts, free_or_dead);
4298 }
Aurelien Jarno7f6ceed2012-10-09 21:53:06 +02004299}
4300
4301/* free register 'reg' by spilling the corresponding temporary if necessary */
Richard Hendersonb3915db2013-09-19 10:36:18 -07004302static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
Aurelien Jarno7f6ceed2012-10-09 21:53:06 +02004303{
Richard Hendersonf8b2f202013-09-18 15:21:56 -07004304 TCGTemp *ts = s->reg_to_temp[reg];
Richard Hendersonf8b2f202013-09-18 15:21:56 -07004305 if (ts != NULL) {
Richard Henderson98b4e182018-11-27 15:35:04 -08004306 temp_sync(s, ts, allocated_regs, 0, -1);
bellardc896fe22008-02-01 10:05:41 +00004307 }
4308}
4309
Richard Hendersonb0164862018-11-27 07:16:21 -08004310/**
4311 * tcg_reg_alloc:
4312 * @required_regs: Set of registers in which we must allocate.
4313 * @allocated_regs: Set of registers which must be avoided.
4314 * @preferred_regs: Set of registers we should prefer.
4315 * @rev: True if we search the registers in "indirect" order.
4316 *
4317 * The allocated register must be in @required_regs & ~@allocated_regs,
4318 * but if we can put it in @preferred_regs we may save a move later.
4319 */
4320static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
4321 TCGRegSet allocated_regs,
4322 TCGRegSet preferred_regs, bool rev)
bellardc896fe22008-02-01 10:05:41 +00004323{
Richard Hendersonb0164862018-11-27 07:16:21 -08004324 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
4325 TCGRegSet reg_ct[2];
Richard Henderson91478ce2015-08-18 23:23:08 -07004326 const int *order;
bellardc896fe22008-02-01 10:05:41 +00004327
Richard Hendersonb0164862018-11-27 07:16:21 -08004328 reg_ct[1] = required_regs & ~allocated_regs;
4329 tcg_debug_assert(reg_ct[1] != 0);
4330 reg_ct[0] = reg_ct[1] & preferred_regs;
4331
4332 /* Skip the preferred_regs option if it cannot be satisfied,
4333 or if the preference made no difference. */
4334 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
4335
Richard Henderson91478ce2015-08-18 23:23:08 -07004336 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
bellardc896fe22008-02-01 10:05:41 +00004337
Richard Hendersonb0164862018-11-27 07:16:21 -08004338 /* Try free registers, preferences first. */
4339 for (j = f; j < 2; j++) {
4340 TCGRegSet set = reg_ct[j];
4341
4342 if (tcg_regset_single(set)) {
4343 /* One register in the set. */
4344 TCGReg reg = tcg_regset_first(set);
4345 if (s->reg_to_temp[reg] == NULL) {
4346 return reg;
4347 }
4348 } else {
4349 for (i = 0; i < n; i++) {
4350 TCGReg reg = order[i];
4351 if (s->reg_to_temp[reg] == NULL &&
4352 tcg_regset_test_reg(set, reg)) {
4353 return reg;
4354 }
4355 }
4356 }
bellardc896fe22008-02-01 10:05:41 +00004357 }
4358
Richard Hendersonb0164862018-11-27 07:16:21 -08004359 /* We must spill something. */
4360 for (j = f; j < 2; j++) {
4361 TCGRegSet set = reg_ct[j];
4362
4363 if (tcg_regset_single(set)) {
4364 /* One register in the set. */
4365 TCGReg reg = tcg_regset_first(set);
Richard Hendersonb3915db2013-09-19 10:36:18 -07004366 tcg_reg_free(s, reg, allocated_regs);
bellardc896fe22008-02-01 10:05:41 +00004367 return reg;
Richard Hendersonb0164862018-11-27 07:16:21 -08004368 } else {
4369 for (i = 0; i < n; i++) {
4370 TCGReg reg = order[i];
4371 if (tcg_regset_test_reg(set, reg)) {
4372 tcg_reg_free(s, reg, allocated_regs);
4373 return reg;
4374 }
4375 }
bellardc896fe22008-02-01 10:05:41 +00004376 }
4377 }
4378
Richard Henderson732e89f2023-04-05 12:09:14 -07004379 g_assert_not_reached();
bellardc896fe22008-02-01 10:05:41 +00004380}
4381
Richard Henderson29f5e922022-10-14 07:37:38 +11004382static TCGReg tcg_reg_alloc_pair(TCGContext *s, TCGRegSet required_regs,
4383 TCGRegSet allocated_regs,
4384 TCGRegSet preferred_regs, bool rev)
4385{
4386 int i, j, k, fmin, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
4387 TCGRegSet reg_ct[2];
4388 const int *order;
4389
4390 /* Ensure that if I is not in allocated_regs, I+1 is not either. */
4391 reg_ct[1] = required_regs & ~(allocated_regs | (allocated_regs >> 1));
4392 tcg_debug_assert(reg_ct[1] != 0);
4393 reg_ct[0] = reg_ct[1] & preferred_regs;
4394
4395 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
4396
4397 /*
4398 * Skip the preferred_regs option if it cannot be satisfied,
4399 * or if the preference made no difference.
4400 */
4401 k = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
4402
4403 /*
4404 * Minimize the number of flushes by looking for 2 free registers first,
4405 * then a single flush, then two flushes.
4406 */
4407 for (fmin = 2; fmin >= 0; fmin--) {
4408 for (j = k; j < 2; j++) {
4409 TCGRegSet set = reg_ct[j];
4410
4411 for (i = 0; i < n; i++) {
4412 TCGReg reg = order[i];
4413
4414 if (tcg_regset_test_reg(set, reg)) {
4415 int f = !s->reg_to_temp[reg] + !s->reg_to_temp[reg + 1];
4416 if (f >= fmin) {
4417 tcg_reg_free(s, reg, allocated_regs);
4418 tcg_reg_free(s, reg + 1, allocated_regs);
4419 return reg;
4420 }
4421 }
4422 }
4423 }
4424 }
Richard Henderson732e89f2023-04-05 12:09:14 -07004425 g_assert_not_reached();
Richard Henderson29f5e922022-10-14 07:37:38 +11004426}
4427
Richard Henderson40ae5c62013-09-19 08:02:05 -07004428/* Make sure the temporary is in a register. If needed, allocate the register
4429 from DESIRED while avoiding ALLOCATED. */
4430static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
Richard Hendersonb7224522018-11-27 07:48:06 -08004431 TCGRegSet allocated_regs, TCGRegSet preferred_regs)
Richard Henderson40ae5c62013-09-19 08:02:05 -07004432{
4433 TCGReg reg;
4434
4435 switch (ts->val_type) {
4436 case TEMP_VAL_REG:
4437 return;
4438 case TEMP_VAL_CONST:
Richard Hendersonb0164862018-11-27 07:16:21 -08004439 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
Richard Hendersonb7224522018-11-27 07:48:06 -08004440 preferred_regs, ts->indirect_base);
Richard Henderson0a6a8bc2020-03-31 05:43:23 -07004441 if (ts->type <= TCG_TYPE_I64) {
4442 tcg_out_movi(s, ts->type, reg, ts->val);
4443 } else {
Richard Henderson4e186172020-03-31 01:02:08 -07004444 uint64_t val = ts->val;
4445 MemOp vece = MO_64;
4446
4447 /*
4448 * Find the minimal vector element that matches the constant.
4449 * The targets will, in general, have to do this search anyway,
4450 * do this generically.
4451 */
Richard Henderson4e186172020-03-31 01:02:08 -07004452 if (val == dup_const(MO_8, val)) {
4453 vece = MO_8;
4454 } else if (val == dup_const(MO_16, val)) {
4455 vece = MO_16;
Richard Henderson0b4286d2020-09-06 17:33:18 -07004456 } else if (val == dup_const(MO_32, val)) {
Richard Henderson4e186172020-03-31 01:02:08 -07004457 vece = MO_32;
4458 }
4459
4460 tcg_out_dupi_vec(s, ts->type, vece, reg, ts->val);
Richard Henderson0a6a8bc2020-03-31 05:43:23 -07004461 }
Richard Henderson40ae5c62013-09-19 08:02:05 -07004462 ts->mem_coherent = 0;
4463 break;
4464 case TEMP_VAL_MEM:
Richard Hendersonb0164862018-11-27 07:16:21 -08004465 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
Richard Hendersonb7224522018-11-27 07:48:06 -08004466 preferred_regs, ts->indirect_base);
Richard Henderson40ae5c62013-09-19 08:02:05 -07004467 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
4468 ts->mem_coherent = 1;
4469 break;
4470 case TEMP_VAL_DEAD:
4471 default:
Richard Henderson732e89f2023-04-05 12:09:14 -07004472 g_assert_not_reached();
Richard Henderson40ae5c62013-09-19 08:02:05 -07004473 }
Richard Henderson098859f2022-12-01 01:05:05 -08004474 set_temp_val_reg(s, ts, reg);
Richard Henderson40ae5c62013-09-19 08:02:05 -07004475}
4476
Richard Henderson59d7c142016-06-19 22:59:13 -07004477/* Save a temporary to memory. 'allocated_regs' is used in case a
4478 temporary registers needs to be allocated to store a constant. */
4479static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
Aurelien Jarno1ad80722012-10-09 21:53:06 +02004480{
Richard Henderson5a184072016-06-23 20:34:33 -07004481 /* The liveness analysis already ensures that globals are back
4482 in memory. Keep an tcg_debug_assert for safety. */
Richard Hendersone01fa972020-03-29 10:40:49 -07004483 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || temp_readonly(ts));
Aurelien Jarno1ad80722012-10-09 21:53:06 +02004484}
4485
Dong Xu Wang9814dd22011-11-22 18:06:22 +08004486/* save globals to their canonical location and assume they can be
bellarde8996ee2008-05-23 17:33:39 +00004487 modified be the following code. 'allocated_regs' is used in case a
4488 temporary registers needs to be allocated to store a constant. */
4489static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
bellardc896fe22008-02-01 10:05:41 +00004490{
Richard Hendersonac3b8892016-11-02 11:21:44 -06004491 int i, n;
bellardc896fe22008-02-01 10:05:41 +00004492
Richard Hendersonac3b8892016-11-02 11:21:44 -06004493 for (i = 0, n = s->nb_globals; i < n; i++) {
Richard Hendersonb13eb722013-09-18 15:35:32 -07004494 temp_save(s, &s->temps[i], allocated_regs);
bellardc896fe22008-02-01 10:05:41 +00004495 }
bellarde5097dc2008-05-21 16:24:20 +00004496}
4497
Aurelien Jarno3d5c5f82012-10-09 21:53:08 +02004498/* sync globals to their canonical location and assume they can be
4499 read by the following code. 'allocated_regs' is used in case a
4500 temporary registers needs to be allocated to store a constant. */
4501static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
4502{
Richard Hendersonac3b8892016-11-02 11:21:44 -06004503 int i, n;
Aurelien Jarno3d5c5f82012-10-09 21:53:08 +02004504
Richard Hendersonac3b8892016-11-02 11:21:44 -06004505 for (i = 0, n = s->nb_globals; i < n; i++) {
Richard Henderson12b9b112013-09-18 15:33:00 -07004506 TCGTemp *ts = &s->temps[i];
Richard Henderson5a184072016-06-23 20:34:33 -07004507 tcg_debug_assert(ts->val_type != TEMP_VAL_REG
Richard Hendersonee17db82020-03-29 10:11:56 -07004508 || ts->kind == TEMP_FIXED
Richard Henderson5a184072016-06-23 20:34:33 -07004509 || ts->mem_coherent);
Aurelien Jarno3d5c5f82012-10-09 21:53:08 +02004510 }
4511}
4512
bellarde5097dc2008-05-21 16:24:20 +00004513/* at the end of a basic block, we assume all temporaries are dead and
bellarde8996ee2008-05-23 17:33:39 +00004514 all globals are stored at their canonical location. */
4515static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
bellarde5097dc2008-05-21 16:24:20 +00004516{
bellarde5097dc2008-05-21 16:24:20 +00004517 int i;
4518
Richard Hendersonb13eb722013-09-18 15:35:32 -07004519 for (i = s->nb_globals; i < s->nb_temps; i++) {
4520 TCGTemp *ts = &s->temps[i];
Richard Hendersonc0522132020-03-29 18:55:52 -07004521
4522 switch (ts->kind) {
Richard Hendersonf57c6912023-01-29 10:55:52 -10004523 case TEMP_TB:
Richard Hendersonb13eb722013-09-18 15:35:32 -07004524 temp_save(s, ts, allocated_regs);
Richard Hendersonc0522132020-03-29 18:55:52 -07004525 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07004526 case TEMP_EBB:
Richard Henderson5a184072016-06-23 20:34:33 -07004527 /* The liveness analysis already ensures that temps are dead.
4528 Keep an tcg_debug_assert for safety. */
4529 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
Richard Hendersonc0522132020-03-29 18:55:52 -07004530 break;
4531 case TEMP_CONST:
4532 /* Similarly, we should have freed any allocated register. */
4533 tcg_debug_assert(ts->val_type == TEMP_VAL_CONST);
4534 break;
4535 default:
4536 g_assert_not_reached();
bellardc896fe22008-02-01 10:05:41 +00004537 }
4538 }
bellarde8996ee2008-05-23 17:33:39 +00004539
4540 save_globals(s, allocated_regs);
bellardc896fe22008-02-01 10:05:41 +00004541}
4542
Richard Hendersonbab16712019-03-18 11:20:27 -07004543/*
Richard Hendersonc7482432022-03-16 09:34:18 -07004544 * At a conditional branch, we assume all temporaries are dead unless
4545 * explicitly live-across-conditional-branch; all globals and local
4546 * temps are synced to their location.
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05004547 */
4548static void tcg_reg_alloc_cbranch(TCGContext *s, TCGRegSet allocated_regs)
4549{
4550 sync_globals(s, allocated_regs);
4551
4552 for (int i = s->nb_globals; i < s->nb_temps; i++) {
4553 TCGTemp *ts = &s->temps[i];
4554 /*
4555 * The liveness analysis already ensures that temps are dead.
4556 * Keep tcg_debug_asserts for safety.
4557 */
Richard Hendersonc0522132020-03-29 18:55:52 -07004558 switch (ts->kind) {
Richard Hendersonf57c6912023-01-29 10:55:52 -10004559 case TEMP_TB:
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05004560 tcg_debug_assert(ts->val_type != TEMP_VAL_REG || ts->mem_coherent);
Richard Hendersonc0522132020-03-29 18:55:52 -07004561 break;
Richard Hendersonc7482432022-03-16 09:34:18 -07004562 case TEMP_EBB:
Richard Hendersonc0522132020-03-29 18:55:52 -07004563 case TEMP_CONST:
4564 break;
4565 default:
4566 g_assert_not_reached();
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05004567 }
4568 }
4569}
4570
4571/*
Richard Hendersonc58f4c92020-04-17 13:22:43 -07004572 * Specialized code generation for INDEX_op_mov_* with a constant.
Richard Hendersonbab16712019-03-18 11:20:27 -07004573 */
Paolo Bonzini0fe4fca2016-09-15 15:16:00 +02004574static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
Richard Hendersonba877192018-11-27 15:39:21 -08004575 tcg_target_ulong val, TCGLifeData arg_life,
4576 TCGRegSet preferred_regs)
bellarde8996ee2008-05-23 17:33:39 +00004577{
Richard Hendersond63e3b62019-03-16 17:48:02 +00004578 /* ENV should not be modified. */
Richard Hendersone01fa972020-03-29 10:40:49 -07004579 tcg_debug_assert(!temp_readonly(ots));
Richard Henderson59d7c142016-06-19 22:59:13 -07004580
4581 /* The movi is not explicitly generated here. */
Richard Henderson098859f2022-12-01 01:05:05 -08004582 set_temp_val_nonreg(s, ots, TEMP_VAL_CONST);
Richard Henderson59d7c142016-06-19 22:59:13 -07004583 ots->val = val;
4584 ots->mem_coherent = 0;
Aurelien Jarnoec7a8692012-10-09 21:53:07 +02004585 if (NEED_SYNC_ARG(0)) {
Richard Hendersonba877192018-11-27 15:39:21 -08004586 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
Richard Henderson59d7c142016-06-19 22:59:13 -07004587 } else if (IS_DEAD_ARG(0)) {
Richard Hendersonf8bf00f2013-09-18 15:29:18 -07004588 temp_dead(s, ots);
Aurelien Jarno4c4e1ab2012-10-09 21:53:07 +02004589 }
bellarde8996ee2008-05-23 17:33:39 +00004590}
4591
Richard Hendersonbab16712019-03-18 11:20:27 -07004592/*
4593 * Specialized code generation for INDEX_op_mov_*.
4594 */
Richard Hendersondd186292016-12-08 13:42:08 -08004595static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
bellardc896fe22008-02-01 10:05:41 +00004596{
Richard Hendersondd186292016-12-08 13:42:08 -08004597 const TCGLifeData arg_life = op->life;
Richard Henderson69e37062018-11-27 07:44:51 -08004598 TCGRegSet allocated_regs, preferred_regs;
bellardc896fe22008-02-01 10:05:41 +00004599 TCGTemp *ts, *ots;
Richard Henderson450445d2014-05-13 14:50:18 -07004600 TCGType otype, itype;
Richard Henderson098859f2022-12-01 01:05:05 -08004601 TCGReg oreg, ireg;
bellardc896fe22008-02-01 10:05:41 +00004602
Richard Hendersond21369f2017-09-11 11:58:44 -07004603 allocated_regs = s->reserved_regs;
Richard Henderson31fd8842022-11-11 15:10:51 +10004604 preferred_regs = output_pref(op, 0);
Richard Henderson43439132017-06-19 23:18:10 -07004605 ots = arg_temp(op->args[0]);
4606 ts = arg_temp(op->args[1]);
Richard Henderson450445d2014-05-13 14:50:18 -07004607
Richard Hendersond63e3b62019-03-16 17:48:02 +00004608 /* ENV should not be modified. */
Richard Hendersone01fa972020-03-29 10:40:49 -07004609 tcg_debug_assert(!temp_readonly(ots));
Richard Hendersond63e3b62019-03-16 17:48:02 +00004610
Richard Henderson450445d2014-05-13 14:50:18 -07004611 /* Note that otype != itype for no-op truncation. */
4612 otype = ots->type;
4613 itype = ts->type;
bellardc896fe22008-02-01 10:05:41 +00004614
Paolo Bonzini0fe4fca2016-09-15 15:16:00 +02004615 if (ts->val_type == TEMP_VAL_CONST) {
4616 /* propagate constant or generate sti */
4617 tcg_target_ulong val = ts->val;
4618 if (IS_DEAD_ARG(1)) {
4619 temp_dead(s, ts);
4620 }
Richard Henderson69e37062018-11-27 07:44:51 -08004621 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
Paolo Bonzini0fe4fca2016-09-15 15:16:00 +02004622 return;
4623 }
4624
4625 /* If the source value is in memory we're going to be forced
4626 to have it in a register in order to perform the copy. Copy
4627 the SOURCE value into its own register first, that way we
4628 don't have to reload SOURCE the next time it is used. */
4629 if (ts->val_type == TEMP_VAL_MEM) {
Richard Henderson69e37062018-11-27 07:44:51 -08004630 temp_load(s, ts, tcg_target_available_regs[itype],
4631 allocated_regs, preferred_regs);
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004632 }
Paolo Bonzini0fe4fca2016-09-15 15:16:00 +02004633 tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
Richard Henderson098859f2022-12-01 01:05:05 -08004634 ireg = ts->reg;
4635
Richard Hendersond63e3b62019-03-16 17:48:02 +00004636 if (IS_DEAD_ARG(0)) {
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004637 /* mov to a non-saved dead register makes no sense (even with
4638 liveness analysis disabled). */
Aurelien Jarnoeabb7b92016-04-21 10:48:49 +02004639 tcg_debug_assert(NEED_SYNC_ARG(0));
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004640 if (!ots->mem_allocated) {
Richard Henderson2272e4a2016-11-09 15:25:09 +01004641 temp_allocate_frame(s, ots);
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004642 }
Richard Henderson098859f2022-12-01 01:05:05 -08004643 tcg_out_st(s, otype, ireg, ots->mem_base->reg, ots->mem_offset);
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004644 if (IS_DEAD_ARG(1)) {
Richard Hendersonf8bf00f2013-09-18 15:29:18 -07004645 temp_dead(s, ts);
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004646 }
Richard Hendersonf8bf00f2013-09-18 15:29:18 -07004647 temp_dead(s, ots);
Richard Henderson098859f2022-12-01 01:05:05 -08004648 return;
4649 }
4650
4651 if (IS_DEAD_ARG(1) && ts->kind != TEMP_FIXED) {
4652 /*
4653 * The mov can be suppressed. Kill input first, so that it
4654 * is unlinked from reg_to_temp, then set the output to the
4655 * reg that we saved from the input.
4656 */
4657 temp_dead(s, ts);
4658 oreg = ireg;
Aurelien Jarnoc29c1d72012-10-09 21:53:07 +02004659 } else {
Richard Henderson098859f2022-12-01 01:05:05 -08004660 if (ots->val_type == TEMP_VAL_REG) {
4661 oreg = ots->reg;
bellardc896fe22008-02-01 10:05:41 +00004662 } else {
Richard Henderson098859f2022-12-01 01:05:05 -08004663 /* Make sure to not spill the input register during allocation. */
4664 oreg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
4665 allocated_regs | ((TCGRegSet)1 << ireg),
4666 preferred_regs, ots->indirect_base);
bellardc896fe22008-02-01 10:05:41 +00004667 }
Richard Henderson098859f2022-12-01 01:05:05 -08004668 if (!tcg_out_mov(s, otype, oreg, ireg)) {
4669 /*
4670 * Cross register class move not supported.
4671 * Store the source register into the destination slot
4672 * and leave the destination temp as TEMP_VAL_MEM.
4673 */
4674 assert(!temp_readonly(ots));
4675 if (!ts->mem_allocated) {
4676 temp_allocate_frame(s, ots);
4677 }
4678 tcg_out_st(s, ts->type, ireg, ots->mem_base->reg, ots->mem_offset);
4679 set_temp_val_nonreg(s, ts, TEMP_VAL_MEM);
4680 ots->mem_coherent = 1;
4681 return;
bellardc896fe22008-02-01 10:05:41 +00004682 }
Aurelien Jarnoec7a8692012-10-09 21:53:07 +02004683 }
Richard Henderson098859f2022-12-01 01:05:05 -08004684 set_temp_val_reg(s, ots, oreg);
4685 ots->mem_coherent = 0;
4686
4687 if (NEED_SYNC_ARG(0)) {
4688 temp_sync(s, ots, allocated_regs, 0, 0);
4689 }
bellardc896fe22008-02-01 10:05:41 +00004690}
4691
Richard Hendersonbab16712019-03-18 11:20:27 -07004692/*
4693 * Specialized code generation for INDEX_op_dup_vec.
4694 */
4695static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)
4696{
4697 const TCGLifeData arg_life = op->life;
4698 TCGRegSet dup_out_regs, dup_in_regs;
4699 TCGTemp *its, *ots;
4700 TCGType itype, vtype;
4701 unsigned vece;
Richard Henderson31c96412022-10-19 12:41:15 +10004702 int lowpart_ofs;
Richard Hendersonbab16712019-03-18 11:20:27 -07004703 bool ok;
4704
4705 ots = arg_temp(op->args[0]);
4706 its = arg_temp(op->args[1]);
4707
4708 /* ENV should not be modified. */
Richard Hendersone01fa972020-03-29 10:40:49 -07004709 tcg_debug_assert(!temp_readonly(ots));
Richard Hendersonbab16712019-03-18 11:20:27 -07004710
4711 itype = its->type;
4712 vece = TCGOP_VECE(op);
4713 vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
4714
4715 if (its->val_type == TEMP_VAL_CONST) {
4716 /* Propagate constant via movi -> dupi. */
4717 tcg_target_ulong val = its->val;
4718 if (IS_DEAD_ARG(1)) {
4719 temp_dead(s, its);
4720 }
Richard Henderson31fd8842022-11-11 15:10:51 +10004721 tcg_reg_alloc_do_movi(s, ots, val, arg_life, output_pref(op, 0));
Richard Hendersonbab16712019-03-18 11:20:27 -07004722 return;
4723 }
4724
Richard Henderson9be0d082020-09-03 15:19:03 -07004725 dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
4726 dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].regs;
Richard Hendersonbab16712019-03-18 11:20:27 -07004727
4728 /* Allocate the output register now. */
4729 if (ots->val_type != TEMP_VAL_REG) {
4730 TCGRegSet allocated_regs = s->reserved_regs;
Richard Henderson098859f2022-12-01 01:05:05 -08004731 TCGReg oreg;
Richard Hendersonbab16712019-03-18 11:20:27 -07004732
4733 if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) {
4734 /* Make sure to not spill the input register. */
4735 tcg_regset_set_reg(allocated_regs, its->reg);
4736 }
Richard Henderson098859f2022-12-01 01:05:05 -08004737 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
Richard Henderson31fd8842022-11-11 15:10:51 +10004738 output_pref(op, 0), ots->indirect_base);
Richard Henderson098859f2022-12-01 01:05:05 -08004739 set_temp_val_reg(s, ots, oreg);
Richard Hendersonbab16712019-03-18 11:20:27 -07004740 }
4741
4742 switch (its->val_type) {
4743 case TEMP_VAL_REG:
4744 /*
4745 * The dup constriaints must be broad, covering all possible VECE.
4746 * However, tcg_op_dup_vec() gets to see the VECE and we allow it
4747 * to fail, indicating that extra moves are required for that case.
4748 */
4749 if (tcg_regset_test_reg(dup_in_regs, its->reg)) {
4750 if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) {
4751 goto done;
4752 }
4753 /* Try again from memory or a vector input register. */
4754 }
4755 if (!its->mem_coherent) {
4756 /*
4757 * The input register is not synced, and so an extra store
4758 * would be required to use memory. Attempt an integer-vector
4759 * register move first. We do not have a TCGRegSet for this.
4760 */
4761 if (tcg_out_mov(s, itype, ots->reg, its->reg)) {
4762 break;
4763 }
4764 /* Sync the temp back to its slot and load from there. */
4765 temp_sync(s, its, s->reserved_regs, 0, 0);
4766 }
4767 /* fall through */
4768
4769 case TEMP_VAL_MEM:
Richard Henderson31c96412022-10-19 12:41:15 +10004770 lowpart_ofs = 0;
4771 if (HOST_BIG_ENDIAN) {
4772 lowpart_ofs = tcg_type_size(itype) - (1 << vece);
4773 }
Richard Hendersond6ecb4a2019-03-18 12:00:39 -07004774 if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
Richard Henderson31c96412022-10-19 12:41:15 +10004775 its->mem_offset + lowpart_ofs)) {
Richard Hendersond6ecb4a2019-03-18 12:00:39 -07004776 goto done;
4777 }
Richard Henderson098859f2022-12-01 01:05:05 -08004778 /* Load the input into the destination vector register. */
Richard Hendersonbab16712019-03-18 11:20:27 -07004779 tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
4780 break;
4781
4782 default:
4783 g_assert_not_reached();
4784 }
4785
4786 /* We now have a vector input register, so dup must succeed. */
4787 ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg);
4788 tcg_debug_assert(ok);
4789
4790 done:
Richard Henderson36f55392022-12-01 00:44:13 -08004791 ots->mem_coherent = 0;
Richard Hendersonbab16712019-03-18 11:20:27 -07004792 if (IS_DEAD_ARG(1)) {
4793 temp_dead(s, its);
4794 }
4795 if (NEED_SYNC_ARG(0)) {
4796 temp_sync(s, ots, s->reserved_regs, 0, 0);
4797 }
4798 if (IS_DEAD_ARG(0)) {
4799 temp_dead(s, ots);
4800 }
4801}
4802
Richard Hendersondd186292016-12-08 13:42:08 -08004803static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
bellardc896fe22008-02-01 10:05:41 +00004804{
Richard Hendersondd186292016-12-08 13:42:08 -08004805 const TCGLifeData arg_life = op->life;
4806 const TCGOpDef * const def = &tcg_op_defs[op->opc];
Richard Henderson82790a82016-11-18 08:35:03 +01004807 TCGRegSet i_allocated_regs;
4808 TCGRegSet o_allocated_regs;
Richard Hendersonb6638662013-09-18 14:54:45 -07004809 int i, k, nb_iargs, nb_oargs;
4810 TCGReg reg;
bellardc896fe22008-02-01 10:05:41 +00004811 TCGArg arg;
4812 const TCGArgConstraint *arg_ct;
4813 TCGTemp *ts;
4814 TCGArg new_args[TCG_MAX_OP_ARGS];
4815 int const_args[TCG_MAX_OP_ARGS];
Richard Henderson21e9a8a2023-10-27 15:44:45 -07004816 TCGCond op_cond;
bellardc896fe22008-02-01 10:05:41 +00004817
4818 nb_oargs = def->nb_oargs;
4819 nb_iargs = def->nb_iargs;
4820
4821 /* copy constants */
Richard Hendersona813e362022-11-30 22:38:25 -08004822 memcpy(new_args + nb_oargs + nb_iargs,
Richard Hendersondd186292016-12-08 13:42:08 -08004823 op->args + nb_oargs + nb_iargs,
bellardc896fe22008-02-01 10:05:41 +00004824 sizeof(TCGArg) * def->nb_cargs);
4825
Richard Hendersond21369f2017-09-11 11:58:44 -07004826 i_allocated_regs = s->reserved_regs;
4827 o_allocated_regs = s->reserved_regs;
Richard Henderson82790a82016-11-18 08:35:03 +01004828
Richard Henderson21e9a8a2023-10-27 15:44:45 -07004829 switch (op->opc) {
4830 case INDEX_op_brcond_i32:
4831 case INDEX_op_brcond_i64:
4832 op_cond = op->args[2];
4833 break;
4834 case INDEX_op_setcond_i32:
4835 case INDEX_op_setcond_i64:
4836 case INDEX_op_negsetcond_i32:
4837 case INDEX_op_negsetcond_i64:
4838 case INDEX_op_cmp_vec:
4839 op_cond = op->args[3];
4840 break;
4841 case INDEX_op_brcond2_i32:
4842 op_cond = op->args[4];
4843 break;
4844 case INDEX_op_movcond_i32:
4845 case INDEX_op_movcond_i64:
4846 case INDEX_op_setcond2_i32:
4847 case INDEX_op_cmpsel_vec:
4848 op_cond = op->args[5];
4849 break;
4850 default:
4851 /* No condition within opcode. */
4852 op_cond = TCG_COND_ALWAYS;
4853 break;
4854 }
4855
Richard Hendersona813e362022-11-30 22:38:25 -08004856 /* satisfy input constraints */
Richard Hendersondd186292016-12-08 13:42:08 -08004857 for (k = 0; k < nb_iargs; k++) {
Richard Henderson29f5e922022-10-14 07:37:38 +11004858 TCGRegSet i_preferred_regs, i_required_regs;
4859 bool allocate_new_reg, copyto_new_reg;
4860 TCGTemp *ts2;
4861 int i1, i2;
Richard Hendersond62816f2018-11-27 20:21:31 -08004862
Richard Henderson66792f92019-04-04 09:37:38 +07004863 i = def->args_ct[nb_oargs + k].sort_index;
Richard Hendersondd186292016-12-08 13:42:08 -08004864 arg = op->args[i];
bellardc896fe22008-02-01 10:05:41 +00004865 arg_ct = &def->args_ct[i];
Richard Henderson43439132017-06-19 23:18:10 -07004866 ts = arg_temp(arg);
Richard Henderson40ae5c62013-09-19 08:02:05 -07004867
4868 if (ts->val_type == TEMP_VAL_CONST
Richard Henderson21e9a8a2023-10-27 15:44:45 -07004869 && tcg_target_const_match(ts->val, arg_ct->ct, ts->type,
4870 op_cond, TCGOP_VECE(op))) {
Richard Henderson40ae5c62013-09-19 08:02:05 -07004871 /* constant is OK for instruction */
4872 const_args[i] = 1;
4873 new_args[i] = ts->val;
Richard Hendersond62816f2018-11-27 20:21:31 -08004874 continue;
bellardc896fe22008-02-01 10:05:41 +00004875 }
Richard Henderson40ae5c62013-09-19 08:02:05 -07004876
Richard Henderson1c1824d2022-10-09 21:06:31 -07004877 reg = ts->reg;
4878 i_preferred_regs = 0;
Richard Henderson29f5e922022-10-14 07:37:38 +11004879 i_required_regs = arg_ct->regs;
Richard Henderson1c1824d2022-10-09 21:06:31 -07004880 allocate_new_reg = false;
Richard Henderson29f5e922022-10-14 07:37:38 +11004881 copyto_new_reg = false;
Richard Henderson1c1824d2022-10-09 21:06:31 -07004882
Richard Henderson29f5e922022-10-14 07:37:38 +11004883 switch (arg_ct->pair) {
4884 case 0: /* not paired */
4885 if (arg_ct->ialias) {
Richard Henderson31fd8842022-11-11 15:10:51 +10004886 i_preferred_regs = output_pref(op, arg_ct->alias_index);
Richard Henderson29f5e922022-10-14 07:37:38 +11004887
4888 /*
4889 * If the input is readonly, then it cannot also be an
4890 * output and aliased to itself. If the input is not
4891 * dead after the instruction, we must allocate a new
4892 * register and move it.
4893 */
Ilya Leoshkevich22d2e532023-07-20 00:11:18 +02004894 if (temp_readonly(ts) || !IS_DEAD_ARG(i)
4895 || def->args_ct[arg_ct->alias_index].newreg) {
Richard Henderson29f5e922022-10-14 07:37:38 +11004896 allocate_new_reg = true;
4897 } else if (ts->val_type == TEMP_VAL_REG) {
4898 /*
4899 * Check if the current register has already been
4900 * allocated for another input.
4901 */
4902 allocate_new_reg =
4903 tcg_regset_test_reg(i_allocated_regs, reg);
4904 }
4905 }
4906 if (!allocate_new_reg) {
4907 temp_load(s, ts, i_required_regs, i_allocated_regs,
4908 i_preferred_regs);
4909 reg = ts->reg;
4910 allocate_new_reg = !tcg_regset_test_reg(i_required_regs, reg);
4911 }
4912 if (allocate_new_reg) {
4913 /*
4914 * Allocate a new register matching the constraint
4915 * and move the temporary register into it.
4916 */
4917 temp_load(s, ts, tcg_target_available_regs[ts->type],
4918 i_allocated_regs, 0);
4919 reg = tcg_reg_alloc(s, i_required_regs, i_allocated_regs,
4920 i_preferred_regs, ts->indirect_base);
4921 copyto_new_reg = true;
4922 }
4923 break;
4924
4925 case 1:
4926 /* First of an input pair; if i1 == i2, the second is an output. */
4927 i1 = i;
4928 i2 = arg_ct->pair_index;
4929 ts2 = i1 != i2 ? arg_temp(op->args[i2]) : NULL;
4930
4931 /*
4932 * It is easier to default to allocating a new pair
4933 * and to identify a few cases where it's not required.
4934 */
4935 if (arg_ct->ialias) {
Richard Henderson31fd8842022-11-11 15:10:51 +10004936 i_preferred_regs = output_pref(op, arg_ct->alias_index);
Richard Henderson29f5e922022-10-14 07:37:38 +11004937 if (IS_DEAD_ARG(i1) &&
4938 IS_DEAD_ARG(i2) &&
4939 !temp_readonly(ts) &&
4940 ts->val_type == TEMP_VAL_REG &&
4941 ts->reg < TCG_TARGET_NB_REGS - 1 &&
4942 tcg_regset_test_reg(i_required_regs, reg) &&
4943 !tcg_regset_test_reg(i_allocated_regs, reg) &&
4944 !tcg_regset_test_reg(i_allocated_regs, reg + 1) &&
4945 (ts2
4946 ? ts2->val_type == TEMP_VAL_REG &&
4947 ts2->reg == reg + 1 &&
4948 !temp_readonly(ts2)
4949 : s->reg_to_temp[reg + 1] == NULL)) {
4950 break;
4951 }
4952 } else {
4953 /* Without aliasing, the pair must also be an input. */
4954 tcg_debug_assert(ts2);
4955 if (ts->val_type == TEMP_VAL_REG &&
4956 ts2->val_type == TEMP_VAL_REG &&
4957 ts2->reg == reg + 1 &&
4958 tcg_regset_test_reg(i_required_regs, reg)) {
4959 break;
4960 }
4961 }
4962 reg = tcg_reg_alloc_pair(s, i_required_regs, i_allocated_regs,
4963 0, ts->indirect_base);
4964 goto do_pair;
4965
4966 case 2: /* pair second */
4967 reg = new_args[arg_ct->pair_index] + 1;
4968 goto do_pair;
4969
4970 case 3: /* ialias with second output, no first input */
4971 tcg_debug_assert(arg_ct->ialias);
Richard Henderson31fd8842022-11-11 15:10:51 +10004972 i_preferred_regs = output_pref(op, arg_ct->alias_index);
Richard Hendersond62816f2018-11-27 20:21:31 -08004973
Richard Henderson29f5e922022-10-14 07:37:38 +11004974 if (IS_DEAD_ARG(i) &&
4975 !temp_readonly(ts) &&
4976 ts->val_type == TEMP_VAL_REG &&
4977 reg > 0 &&
4978 s->reg_to_temp[reg - 1] == NULL &&
4979 tcg_regset_test_reg(i_required_regs, reg) &&
4980 !tcg_regset_test_reg(i_allocated_regs, reg) &&
4981 !tcg_regset_test_reg(i_allocated_regs, reg - 1)) {
4982 tcg_regset_set_reg(i_allocated_regs, reg - 1);
4983 break;
Richard Hendersonc0522132020-03-29 18:55:52 -07004984 }
Richard Henderson29f5e922022-10-14 07:37:38 +11004985 reg = tcg_reg_alloc_pair(s, i_required_regs >> 1,
4986 i_allocated_regs, 0,
4987 ts->indirect_base);
4988 tcg_regset_set_reg(i_allocated_regs, reg);
4989 reg += 1;
4990 goto do_pair;
Richard Hendersond62816f2018-11-27 20:21:31 -08004991
Richard Henderson29f5e922022-10-14 07:37:38 +11004992 do_pair:
Richard Hendersonc0522132020-03-29 18:55:52 -07004993 /*
Richard Henderson29f5e922022-10-14 07:37:38 +11004994 * If an aliased input is not dead after the instruction,
4995 * we must allocate a new register and move it.
Richard Hendersonc0522132020-03-29 18:55:52 -07004996 */
Richard Henderson29f5e922022-10-14 07:37:38 +11004997 if (arg_ct->ialias && (!IS_DEAD_ARG(i) || temp_readonly(ts))) {
4998 TCGRegSet t_allocated_regs = i_allocated_regs;
4999
5000 /*
5001 * Because of the alias, and the continued life, make sure
5002 * that the temp is somewhere *other* than the reg pair,
5003 * and we get a copy in reg.
5004 */
5005 tcg_regset_set_reg(t_allocated_regs, reg);
5006 tcg_regset_set_reg(t_allocated_regs, reg + 1);
5007 if (ts->val_type == TEMP_VAL_REG && ts->reg == reg) {
5008 /* If ts was already in reg, copy it somewhere else. */
5009 TCGReg nr;
5010 bool ok;
5011
5012 tcg_debug_assert(ts->kind != TEMP_FIXED);
5013 nr = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
5014 t_allocated_regs, 0, ts->indirect_base);
5015 ok = tcg_out_mov(s, ts->type, nr, reg);
5016 tcg_debug_assert(ok);
5017
5018 set_temp_val_reg(s, ts, nr);
5019 } else {
5020 temp_load(s, ts, tcg_target_available_regs[ts->type],
5021 t_allocated_regs, 0);
5022 copyto_new_reg = true;
5023 }
5024 } else {
5025 /* Preferably allocate to reg, otherwise copy. */
5026 i_required_regs = (TCGRegSet)1 << reg;
5027 temp_load(s, ts, i_required_regs, i_allocated_regs,
5028 i_preferred_regs);
5029 copyto_new_reg = ts->reg != reg;
5030 }
5031 break;
5032
5033 default:
5034 g_assert_not_reached();
5035 }
5036
5037 if (copyto_new_reg) {
Richard Henderson78113e82019-03-16 17:48:18 +00005038 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
Richard Henderson240c08d2019-03-16 17:48:32 +00005039 /*
5040 * Cross register class move not supported. Sync the
5041 * temp back to its slot and load from there.
5042 */
5043 temp_sync(s, ts, i_allocated_regs, 0, 0);
5044 tcg_out_ld(s, ts->type, reg,
5045 ts->mem_base->reg, ts->mem_offset);
Richard Henderson78113e82019-03-16 17:48:18 +00005046 }
bellardc896fe22008-02-01 10:05:41 +00005047 }
bellardc896fe22008-02-01 10:05:41 +00005048 new_args[i] = reg;
5049 const_args[i] = 0;
Richard Henderson82790a82016-11-18 08:35:03 +01005050 tcg_regset_set_reg(i_allocated_regs, reg);
bellardc896fe22008-02-01 10:05:41 +00005051 }
Richard Hendersona813e362022-11-30 22:38:25 -08005052
Aurelien Jarnoa52ad072012-10-09 21:53:07 +02005053 /* mark dead temporaries and free the associated registers */
5054 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
5055 if (IS_DEAD_ARG(i)) {
Richard Henderson43439132017-06-19 23:18:10 -07005056 temp_dead(s, arg_temp(op->args[i]));
Aurelien Jarnoa52ad072012-10-09 21:53:07 +02005057 }
5058 }
5059
Richard Hendersonb4cb76e2020-10-08 15:21:43 -05005060 if (def->flags & TCG_OPF_COND_BRANCH) {
5061 tcg_reg_alloc_cbranch(s, i_allocated_regs);
5062 } else if (def->flags & TCG_OPF_BB_END) {
Richard Henderson82790a82016-11-18 08:35:03 +01005063 tcg_reg_alloc_bb_end(s, i_allocated_regs);
bellarde8996ee2008-05-23 17:33:39 +00005064 } else {
bellarde8996ee2008-05-23 17:33:39 +00005065 if (def->flags & TCG_OPF_CALL_CLOBBER) {
Richard Hendersona813e362022-11-30 22:38:25 -08005066 /* XXX: permit generic clobber register list ? */
Richard Hendersonc8074022016-02-09 10:43:42 +11005067 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
5068 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
Richard Henderson82790a82016-11-18 08:35:03 +01005069 tcg_reg_free(s, i, i_allocated_regs);
bellarde8996ee2008-05-23 17:33:39 +00005070 }
5071 }
Aurelien Jarno3d5c5f82012-10-09 21:53:08 +02005072 }
5073 if (def->flags & TCG_OPF_SIDE_EFFECTS) {
5074 /* sync globals if the op has side effects and might trigger
5075 an exception. */
Richard Henderson82790a82016-11-18 08:35:03 +01005076 sync_globals(s, i_allocated_regs);
bellarde8996ee2008-05-23 17:33:39 +00005077 }
Richard Hendersona813e362022-11-30 22:38:25 -08005078
bellarde8996ee2008-05-23 17:33:39 +00005079 /* satisfy the output constraints */
bellarde8996ee2008-05-23 17:33:39 +00005080 for(k = 0; k < nb_oargs; k++) {
Richard Henderson66792f92019-04-04 09:37:38 +07005081 i = def->args_ct[k].sort_index;
Richard Hendersondd186292016-12-08 13:42:08 -08005082 arg = op->args[i];
bellarde8996ee2008-05-23 17:33:39 +00005083 arg_ct = &def->args_ct[i];
Richard Henderson43439132017-06-19 23:18:10 -07005084 ts = arg_temp(arg);
Richard Hendersond63e3b62019-03-16 17:48:02 +00005085
5086 /* ENV should not be modified. */
Richard Hendersone01fa972020-03-29 10:40:49 -07005087 tcg_debug_assert(!temp_readonly(ts));
Richard Hendersond63e3b62019-03-16 17:48:02 +00005088
Richard Henderson29f5e922022-10-14 07:37:38 +11005089 switch (arg_ct->pair) {
5090 case 0: /* not paired */
5091 if (arg_ct->oalias && !const_args[arg_ct->alias_index]) {
5092 reg = new_args[arg_ct->alias_index];
5093 } else if (arg_ct->newreg) {
5094 reg = tcg_reg_alloc(s, arg_ct->regs,
5095 i_allocated_regs | o_allocated_regs,
Richard Henderson31fd8842022-11-11 15:10:51 +10005096 output_pref(op, k), ts->indirect_base);
Richard Henderson29f5e922022-10-14 07:37:38 +11005097 } else {
5098 reg = tcg_reg_alloc(s, arg_ct->regs, o_allocated_regs,
Richard Henderson31fd8842022-11-11 15:10:51 +10005099 output_pref(op, k), ts->indirect_base);
Richard Henderson29f5e922022-10-14 07:37:38 +11005100 }
5101 break;
5102
5103 case 1: /* first of pair */
Richard Henderson29f5e922022-10-14 07:37:38 +11005104 if (arg_ct->oalias) {
5105 reg = new_args[arg_ct->alias_index];
Richard Hendersonca5bed02024-01-02 01:27:18 +00005106 } else if (arg_ct->newreg) {
5107 reg = tcg_reg_alloc_pair(s, arg_ct->regs,
5108 i_allocated_regs | o_allocated_regs,
5109 output_pref(op, k),
5110 ts->indirect_base);
5111 } else {
5112 reg = tcg_reg_alloc_pair(s, arg_ct->regs, o_allocated_regs,
5113 output_pref(op, k),
5114 ts->indirect_base);
Richard Henderson29f5e922022-10-14 07:37:38 +11005115 }
Richard Henderson29f5e922022-10-14 07:37:38 +11005116 break;
5117
5118 case 2: /* second of pair */
Richard Henderson29f5e922022-10-14 07:37:38 +11005119 if (arg_ct->oalias) {
5120 reg = new_args[arg_ct->alias_index];
5121 } else {
5122 reg = new_args[arg_ct->pair_index] + 1;
5123 }
5124 break;
5125
5126 case 3: /* first of pair, aliasing with a second input */
5127 tcg_debug_assert(!arg_ct->newreg);
5128 reg = new_args[arg_ct->pair_index] - 1;
5129 break;
5130
5131 default:
5132 g_assert_not_reached();
bellarde8996ee2008-05-23 17:33:39 +00005133 }
Richard Henderson82790a82016-11-18 08:35:03 +01005134 tcg_regset_set_reg(o_allocated_regs, reg);
Richard Henderson098859f2022-12-01 01:05:05 -08005135 set_temp_val_reg(s, ts, reg);
Richard Hendersond63e3b62019-03-16 17:48:02 +00005136 ts->mem_coherent = 0;
bellarde8996ee2008-05-23 17:33:39 +00005137 new_args[i] = reg;
bellardc896fe22008-02-01 10:05:41 +00005138 }
5139 }
5140
bellardc896fe22008-02-01 10:05:41 +00005141 /* emit instruction */
Richard Henderson678155b2023-04-05 11:17:01 -07005142 switch (op->opc) {
5143 case INDEX_op_ext8s_i32:
5144 tcg_out_ext8s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
5145 break;
5146 case INDEX_op_ext8s_i64:
5147 tcg_out_ext8s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
5148 break;
Richard Hendersond0e66c82023-04-05 13:26:51 -07005149 case INDEX_op_ext8u_i32:
5150 case INDEX_op_ext8u_i64:
5151 tcg_out_ext8u(s, new_args[0], new_args[1]);
5152 break;
Richard Henderson753e42e2023-04-05 14:49:59 -07005153 case INDEX_op_ext16s_i32:
5154 tcg_out_ext16s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
5155 break;
5156 case INDEX_op_ext16s_i64:
5157 tcg_out_ext16s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
5158 break;
Richard Henderson379afdf2023-04-05 16:25:22 -07005159 case INDEX_op_ext16u_i32:
5160 case INDEX_op_ext16u_i64:
5161 tcg_out_ext16u(s, new_args[0], new_args[1]);
5162 break;
Richard Henderson52bf3392023-04-05 17:50:09 -07005163 case INDEX_op_ext32s_i64:
5164 tcg_out_ext32s(s, new_args[0], new_args[1]);
5165 break;
Richard Henderson9ecf5f62023-04-05 18:07:05 -07005166 case INDEX_op_ext32u_i64:
5167 tcg_out_ext32u(s, new_args[0], new_args[1]);
5168 break;
Richard Henderson9c6aa272023-04-05 18:30:56 -07005169 case INDEX_op_ext_i32_i64:
5170 tcg_out_exts_i32_i64(s, new_args[0], new_args[1]);
5171 break;
Richard Hendersonb9bfe002023-04-05 18:56:28 -07005172 case INDEX_op_extu_i32_i64:
5173 tcg_out_extu_i32_i64(s, new_args[0], new_args[1]);
5174 break;
Richard Hendersonb8b94ac2023-04-05 19:58:35 -07005175 case INDEX_op_extrl_i64_i32:
5176 tcg_out_extrl_i64_i32(s, new_args[0], new_args[1]);
5177 break;
Richard Henderson678155b2023-04-05 11:17:01 -07005178 default:
5179 if (def->flags & TCG_OPF_VECTOR) {
5180 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
5181 new_args, const_args);
5182 } else {
5183 tcg_out_op(s, op->opc, new_args, const_args);
5184 }
5185 break;
Richard Hendersond2fd7452017-09-14 13:53:46 -07005186 }
5187
bellardc896fe22008-02-01 10:05:41 +00005188 /* move the outputs in the correct register if needed */
5189 for(i = 0; i < nb_oargs; i++) {
Richard Henderson43439132017-06-19 23:18:10 -07005190 ts = arg_temp(op->args[i]);
Richard Hendersond63e3b62019-03-16 17:48:02 +00005191
5192 /* ENV should not be modified. */
Richard Hendersone01fa972020-03-29 10:40:49 -07005193 tcg_debug_assert(!temp_readonly(ts));
Richard Hendersond63e3b62019-03-16 17:48:02 +00005194
Aurelien Jarnoec7a8692012-10-09 21:53:07 +02005195 if (NEED_SYNC_ARG(i)) {
Richard Henderson98b4e182018-11-27 15:35:04 -08005196 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
Richard Henderson59d7c142016-06-19 22:59:13 -07005197 } else if (IS_DEAD_ARG(i)) {
Richard Hendersonf8bf00f2013-09-18 15:29:18 -07005198 temp_dead(s, ts);
Aurelien Jarnoec7a8692012-10-09 21:53:07 +02005199 }
bellardc896fe22008-02-01 10:05:41 +00005200 }
5201}
5202
Richard Hendersonefe86b22020-03-31 02:33:21 -07005203static bool tcg_reg_alloc_dup2(TCGContext *s, const TCGOp *op)
5204{
5205 const TCGLifeData arg_life = op->life;
5206 TCGTemp *ots, *itsl, *itsh;
5207 TCGType vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
5208
5209 /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */
5210 tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
5211 tcg_debug_assert(TCGOP_VECE(op) == MO_64);
5212
5213 ots = arg_temp(op->args[0]);
5214 itsl = arg_temp(op->args[1]);
5215 itsh = arg_temp(op->args[2]);
5216
5217 /* ENV should not be modified. */
5218 tcg_debug_assert(!temp_readonly(ots));
5219
5220 /* Allocate the output register now. */
5221 if (ots->val_type != TEMP_VAL_REG) {
5222 TCGRegSet allocated_regs = s->reserved_regs;
5223 TCGRegSet dup_out_regs =
5224 tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
Richard Henderson098859f2022-12-01 01:05:05 -08005225 TCGReg oreg;
Richard Hendersonefe86b22020-03-31 02:33:21 -07005226
5227 /* Make sure to not spill the input registers. */
5228 if (!IS_DEAD_ARG(1) && itsl->val_type == TEMP_VAL_REG) {
5229 tcg_regset_set_reg(allocated_regs, itsl->reg);
5230 }
5231 if (!IS_DEAD_ARG(2) && itsh->val_type == TEMP_VAL_REG) {
5232 tcg_regset_set_reg(allocated_regs, itsh->reg);
5233 }
5234
Richard Henderson098859f2022-12-01 01:05:05 -08005235 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
Richard Henderson31fd8842022-11-11 15:10:51 +10005236 output_pref(op, 0), ots->indirect_base);
Richard Henderson098859f2022-12-01 01:05:05 -08005237 set_temp_val_reg(s, ots, oreg);
Richard Hendersonefe86b22020-03-31 02:33:21 -07005238 }
5239
5240 /* Promote dup2 of immediates to dupi_vec. */
5241 if (itsl->val_type == TEMP_VAL_CONST && itsh->val_type == TEMP_VAL_CONST) {
5242 uint64_t val = deposit64(itsl->val, 32, 32, itsh->val);
5243 MemOp vece = MO_64;
5244
5245 if (val == dup_const(MO_8, val)) {
5246 vece = MO_8;
5247 } else if (val == dup_const(MO_16, val)) {
5248 vece = MO_16;
5249 } else if (val == dup_const(MO_32, val)) {
5250 vece = MO_32;
5251 }
5252
5253 tcg_out_dupi_vec(s, vtype, vece, ots->reg, val);
5254 goto done;
5255 }
5256
5257 /* If the two inputs form one 64-bit value, try dupm_vec. */
Richard Hendersonaef85402022-10-19 11:53:27 +10005258 if (itsl->temp_subindex == HOST_BIG_ENDIAN &&
5259 itsh->temp_subindex == !HOST_BIG_ENDIAN &&
5260 itsl == itsh + (HOST_BIG_ENDIAN ? 1 : -1)) {
5261 TCGTemp *its = itsl - HOST_BIG_ENDIAN;
5262
5263 temp_sync(s, its + 0, s->reserved_regs, 0, 0);
5264 temp_sync(s, its + 1, s->reserved_regs, 0, 0);
5265
Richard Hendersonefe86b22020-03-31 02:33:21 -07005266 if (tcg_out_dupm_vec(s, vtype, MO_64, ots->reg,
5267 its->mem_base->reg, its->mem_offset)) {
5268 goto done;
5269 }
5270 }
5271
5272 /* Fall back to generic expansion. */
5273 return false;
5274
5275 done:
Richard Henderson36f55392022-12-01 00:44:13 -08005276 ots->mem_coherent = 0;
Richard Hendersonefe86b22020-03-31 02:33:21 -07005277 if (IS_DEAD_ARG(1)) {
5278 temp_dead(s, itsl);
5279 }
5280 if (IS_DEAD_ARG(2)) {
5281 temp_dead(s, itsh);
5282 }
5283 if (NEED_SYNC_ARG(0)) {
5284 temp_sync(s, ots, s->reserved_regs, 0, IS_DEAD_ARG(0));
5285 } else if (IS_DEAD_ARG(0)) {
5286 temp_dead(s, ots);
5287 }
5288 return true;
5289}
5290
Richard Henderson39004a72022-11-11 10:09:37 +10005291static void load_arg_reg(TCGContext *s, TCGReg reg, TCGTemp *ts,
5292 TCGRegSet allocated_regs)
5293{
5294 if (ts->val_type == TEMP_VAL_REG) {
5295 if (ts->reg != reg) {
5296 tcg_reg_free(s, reg, allocated_regs);
5297 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
5298 /*
5299 * Cross register class move not supported. Sync the
5300 * temp back to its slot and load from there.
5301 */
5302 temp_sync(s, ts, allocated_regs, 0, 0);
5303 tcg_out_ld(s, ts->type, reg,
5304 ts->mem_base->reg, ts->mem_offset);
5305 }
5306 }
5307 } else {
5308 TCGRegSet arg_set = 0;
5309
5310 tcg_reg_free(s, reg, allocated_regs);
5311 tcg_regset_set_reg(arg_set, reg);
5312 temp_load(s, ts, arg_set, allocated_regs, 0);
5313 }
5314}
5315
Richard Hendersond78e4a42023-04-08 19:05:10 -07005316static void load_arg_stk(TCGContext *s, unsigned arg_slot, TCGTemp *ts,
Richard Henderson39004a72022-11-11 10:09:37 +10005317 TCGRegSet allocated_regs)
5318{
5319 /*
5320 * When the destination is on the stack, load up the temp and store.
5321 * If there are many call-saved registers, the temp might live to
5322 * see another use; otherwise it'll be discarded.
5323 */
5324 temp_load(s, ts, tcg_target_available_regs[ts->type], allocated_regs, 0);
5325 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK,
Richard Hendersond78e4a42023-04-08 19:05:10 -07005326 arg_slot_stk_ofs(arg_slot));
Richard Henderson39004a72022-11-11 10:09:37 +10005327}
5328
5329static void load_arg_normal(TCGContext *s, const TCGCallArgumentLoc *l,
5330 TCGTemp *ts, TCGRegSet *allocated_regs)
5331{
Richard Henderson338b61e2023-04-08 17:28:07 -07005332 if (arg_slot_reg_p(l->arg_slot)) {
Richard Henderson39004a72022-11-11 10:09:37 +10005333 TCGReg reg = tcg_target_call_iarg_regs[l->arg_slot];
5334 load_arg_reg(s, reg, ts, *allocated_regs);
5335 tcg_regset_set_reg(*allocated_regs, reg);
5336 } else {
Richard Hendersond78e4a42023-04-08 19:05:10 -07005337 load_arg_stk(s, l->arg_slot, ts, *allocated_regs);
Richard Henderson39004a72022-11-11 10:09:37 +10005338 }
5339}
5340
Richard Hendersond78e4a42023-04-08 19:05:10 -07005341static void load_arg_ref(TCGContext *s, unsigned arg_slot, TCGReg ref_base,
Richard Henderson313bdea2022-10-31 09:22:59 +11005342 intptr_t ref_off, TCGRegSet *allocated_regs)
5343{
5344 TCGReg reg;
Richard Henderson313bdea2022-10-31 09:22:59 +11005345
Richard Hendersond78e4a42023-04-08 19:05:10 -07005346 if (arg_slot_reg_p(arg_slot)) {
Richard Henderson313bdea2022-10-31 09:22:59 +11005347 reg = tcg_target_call_iarg_regs[arg_slot];
5348 tcg_reg_free(s, reg, *allocated_regs);
5349 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
5350 tcg_regset_set_reg(*allocated_regs, reg);
5351 } else {
5352 reg = tcg_reg_alloc(s, tcg_target_available_regs[TCG_TYPE_PTR],
5353 *allocated_regs, 0, false);
5354 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
5355 tcg_out_st(s, TCG_TYPE_PTR, reg, TCG_REG_CALL_STACK,
Richard Hendersond78e4a42023-04-08 19:05:10 -07005356 arg_slot_stk_ofs(arg_slot));
Richard Henderson313bdea2022-10-31 09:22:59 +11005357 }
5358}
5359
Richard Hendersondd186292016-12-08 13:42:08 -08005360static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
bellardc896fe22008-02-01 10:05:41 +00005361{
Richard Hendersoncd9090a2017-11-14 13:02:51 +01005362 const int nb_oargs = TCGOP_CALLO(op);
5363 const int nb_iargs = TCGOP_CALLI(op);
Richard Hendersondd186292016-12-08 13:42:08 -08005364 const TCGLifeData arg_life = op->life;
Richard Henderson39004a72022-11-11 10:09:37 +10005365 const TCGHelperInfo *info = tcg_call_info(op);
5366 TCGRegSet allocated_regs = s->reserved_regs;
5367 int i;
bellardc896fe22008-02-01 10:05:41 +00005368
Richard Henderson39004a72022-11-11 10:09:37 +10005369 /*
5370 * Move inputs into place in reverse order,
5371 * so that we place stacked arguments first.
5372 */
5373 for (i = nb_iargs - 1; i >= 0; --i) {
5374 const TCGCallArgumentLoc *loc = &info->in[i];
5375 TCGTemp *ts = arg_temp(op->args[nb_oargs + i]);
bellardc896fe22008-02-01 10:05:41 +00005376
Richard Henderson39004a72022-11-11 10:09:37 +10005377 switch (loc->kind) {
5378 case TCG_CALL_ARG_NORMAL:
5379 case TCG_CALL_ARG_EXTEND_U:
5380 case TCG_CALL_ARG_EXTEND_S:
5381 load_arg_normal(s, loc, ts, &allocated_regs);
5382 break;
Richard Henderson313bdea2022-10-31 09:22:59 +11005383 case TCG_CALL_ARG_BY_REF:
5384 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
5385 load_arg_ref(s, loc->arg_slot, TCG_REG_CALL_STACK,
Richard Hendersond78e4a42023-04-08 19:05:10 -07005386 arg_slot_stk_ofs(loc->ref_slot),
Richard Henderson313bdea2022-10-31 09:22:59 +11005387 &allocated_regs);
5388 break;
5389 case TCG_CALL_ARG_BY_REF_N:
5390 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
5391 break;
Richard Henderson39004a72022-11-11 10:09:37 +10005392 default:
5393 g_assert_not_reached();
bellardc896fe22008-02-01 10:05:41 +00005394 }
bellardc896fe22008-02-01 10:05:41 +00005395 }
Richard Hendersona813e362022-11-30 22:38:25 -08005396
Richard Henderson39004a72022-11-11 10:09:37 +10005397 /* Mark dead temporaries and free the associated registers. */
Richard Hendersondd186292016-12-08 13:42:08 -08005398 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
Aurelien Jarno866cb6c2011-05-17 18:25:45 +02005399 if (IS_DEAD_ARG(i)) {
Richard Henderson43439132017-06-19 23:18:10 -07005400 temp_dead(s, arg_temp(op->args[i]));
bellardc896fe22008-02-01 10:05:41 +00005401 }
5402 }
Richard Hendersona813e362022-11-30 22:38:25 -08005403
Richard Henderson39004a72022-11-11 10:09:37 +10005404 /* Clobber call registers. */
Richard Hendersonc8074022016-02-09 10:43:42 +11005405 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
5406 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
Richard Hendersonb3915db2013-09-19 10:36:18 -07005407 tcg_reg_free(s, i, allocated_regs);
bellardc896fe22008-02-01 10:05:41 +00005408 }
5409 }
Aurelien Jarno78505272012-10-09 21:53:08 +02005410
Richard Henderson39004a72022-11-11 10:09:37 +10005411 /*
5412 * Save globals if they might be written by the helper,
5413 * sync them if they might be read.
5414 */
5415 if (info->flags & TCG_CALL_NO_READ_GLOBALS) {
Aurelien Jarno78505272012-10-09 21:53:08 +02005416 /* Nothing to do */
Richard Henderson39004a72022-11-11 10:09:37 +10005417 } else if (info->flags & TCG_CALL_NO_WRITE_GLOBALS) {
Aurelien Jarno78505272012-10-09 21:53:08 +02005418 sync_globals(s, allocated_regs);
5419 } else {
aurel32b9c18f52009-04-06 12:33:59 +00005420 save_globals(s, allocated_regs);
5421 }
bellardc896fe22008-02-01 10:05:41 +00005422
Richard Henderson313bdea2022-10-31 09:22:59 +11005423 /*
5424 * If the ABI passes a pointer to the returned struct as the first
5425 * argument, load that now. Pass a pointer to the output home slot.
5426 */
5427 if (info->out_kind == TCG_CALL_RET_BY_REF) {
5428 TCGTemp *ts = arg_temp(op->args[0]);
5429
5430 if (!ts->mem_allocated) {
5431 temp_allocate_frame(s, ts);
5432 }
5433 load_arg_ref(s, 0, ts->mem_base->reg, ts->mem_offset, &allocated_regs);
5434 }
5435
Richard Hendersoncee44b02022-10-18 17:51:41 +10005436 tcg_out_call(s, tcg_call_func(op), info);
bellardc896fe22008-02-01 10:05:41 +00005437
Richard Henderson39004a72022-11-11 10:09:37 +10005438 /* Assign output registers and emit moves if needed. */
5439 switch (info->out_kind) {
5440 case TCG_CALL_RET_NORMAL:
5441 for (i = 0; i < nb_oargs; i++) {
5442 TCGTemp *ts = arg_temp(op->args[i]);
Richard Henderson5e3d0c12022-10-20 00:55:36 +10005443 TCGReg reg = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, i);
Richard Hendersond63e3b62019-03-16 17:48:02 +00005444
Richard Henderson39004a72022-11-11 10:09:37 +10005445 /* ENV should not be modified. */
5446 tcg_debug_assert(!temp_readonly(ts));
Richard Hendersond63e3b62019-03-16 17:48:02 +00005447
Richard Henderson39004a72022-11-11 10:09:37 +10005448 set_temp_val_reg(s, ts, reg);
5449 ts->mem_coherent = 0;
5450 }
5451 break;
Richard Henderson313bdea2022-10-31 09:22:59 +11005452
Richard Hendersonc6556aa2022-10-20 01:13:52 +10005453 case TCG_CALL_RET_BY_VEC:
5454 {
5455 TCGTemp *ts = arg_temp(op->args[0]);
5456
5457 tcg_debug_assert(ts->base_type == TCG_TYPE_I128);
5458 tcg_debug_assert(ts->temp_subindex == 0);
5459 if (!ts->mem_allocated) {
5460 temp_allocate_frame(s, ts);
5461 }
5462 tcg_out_st(s, TCG_TYPE_V128,
5463 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
5464 ts->mem_base->reg, ts->mem_offset);
5465 }
5466 /* fall through to mark all parts in memory */
5467
Richard Henderson313bdea2022-10-31 09:22:59 +11005468 case TCG_CALL_RET_BY_REF:
5469 /* The callee has performed a write through the reference. */
5470 for (i = 0; i < nb_oargs; i++) {
5471 TCGTemp *ts = arg_temp(op->args[i]);
5472 ts->val_type = TEMP_VAL_MEM;
5473 }
5474 break;
5475
Richard Henderson39004a72022-11-11 10:09:37 +10005476 default:
5477 g_assert_not_reached();
5478 }
5479
5480 /* Flush or discard output registers as needed. */
5481 for (i = 0; i < nb_oargs; i++) {
5482 TCGTemp *ts = arg_temp(op->args[i]);
Richard Hendersond63e3b62019-03-16 17:48:02 +00005483 if (NEED_SYNC_ARG(i)) {
Richard Henderson39004a72022-11-11 10:09:37 +10005484 temp_sync(s, ts, s->reserved_regs, 0, IS_DEAD_ARG(i));
Richard Hendersond63e3b62019-03-16 17:48:02 +00005485 } else if (IS_DEAD_ARG(i)) {
5486 temp_dead(s, ts);
bellardc896fe22008-02-01 10:05:41 +00005487 }
5488 }
bellardc896fe22008-02-01 10:05:41 +00005489}
5490
Richard Hendersone63b8a22022-11-08 09:23:54 +11005491/**
5492 * atom_and_align_for_opc:
5493 * @s: tcg context
5494 * @opc: memory operation code
5495 * @host_atom: MO_ATOM_{IFALIGN,WITHIN16,SUBALIGN} for host operations
5496 * @allow_two_ops: true if we are prepared to issue two operations
5497 *
5498 * Return the alignment and atomicity to use for the inline fast path
5499 * for the given memory operation. The alignment may be larger than
5500 * that specified in @opc, and the correct alignment will be diagnosed
5501 * by the slow path helper.
5502 *
5503 * If @allow_two_ops, the host is prepared to test for 2x alignment,
5504 * and issue two loads or stores for subalignment.
5505 */
5506static TCGAtomAlign atom_and_align_for_opc(TCGContext *s, MemOp opc,
5507 MemOp host_atom, bool allow_two_ops)
5508{
5509 MemOp align = get_alignment_bits(opc);
5510 MemOp size = opc & MO_SIZE;
5511 MemOp half = size ? size - 1 : 0;
Richard Hendersoncbb14552023-12-12 11:35:42 -08005512 MemOp atom = opc & MO_ATOM_MASK;
Richard Hendersone63b8a22022-11-08 09:23:54 +11005513 MemOp atmax;
Richard Hendersone63b8a22022-11-08 09:23:54 +11005514
5515 switch (atom) {
5516 case MO_ATOM_NONE:
5517 /* The operation requires no specific atomicity. */
5518 atmax = MO_8;
5519 break;
5520
5521 case MO_ATOM_IFALIGN:
5522 atmax = size;
5523 break;
5524
5525 case MO_ATOM_IFALIGN_PAIR:
5526 atmax = half;
5527 break;
5528
5529 case MO_ATOM_WITHIN16:
5530 atmax = size;
5531 if (size == MO_128) {
5532 /* Misalignment implies !within16, and therefore no atomicity. */
5533 } else if (host_atom != MO_ATOM_WITHIN16) {
5534 /* The host does not implement within16, so require alignment. */
5535 align = MAX(align, size);
5536 }
5537 break;
5538
5539 case MO_ATOM_WITHIN16_PAIR:
5540 atmax = size;
5541 /*
5542 * Misalignment implies !within16, and therefore half atomicity.
5543 * Any host prepared for two operations can implement this with
5544 * half alignment.
5545 */
5546 if (host_atom != MO_ATOM_WITHIN16 && allow_two_ops) {
5547 align = MAX(align, half);
5548 }
5549 break;
5550
5551 case MO_ATOM_SUBALIGN:
5552 atmax = size;
5553 if (host_atom != MO_ATOM_SUBALIGN) {
5554 /* If unaligned but not odd, there are subobjects up to half. */
5555 if (allow_two_ops) {
5556 align = MAX(align, half);
5557 } else {
5558 align = MAX(align, size);
5559 }
5560 }
5561 break;
5562
5563 default:
5564 g_assert_not_reached();
5565 }
5566
5567 return (TCGAtomAlign){ .atom = atmax, .align = align };
5568}
5569
Richard Henderson8429a1c2023-04-09 22:59:09 -07005570/*
5571 * Similarly for qemu_ld/st slow path helpers.
5572 * We must re-implement tcg_gen_callN and tcg_reg_alloc_call simultaneously,
5573 * using only the provided backend tcg_out_* functions.
5574 */
5575
5576static int tcg_out_helper_stk_ofs(TCGType type, unsigned slot)
5577{
5578 int ofs = arg_slot_stk_ofs(slot);
5579
5580 /*
5581 * Each stack slot is TCG_TARGET_LONG_BITS. If the host does not
5582 * require extension to uint64_t, adjust the address for uint32_t.
5583 */
5584 if (HOST_BIG_ENDIAN &&
5585 TCG_TARGET_REG_BITS == 64 &&
5586 type == TCG_TYPE_I32) {
5587 ofs += 4;
5588 }
5589 return ofs;
5590}
5591
Richard Henderson8d314042023-05-14 10:07:22 -07005592static void tcg_out_helper_load_slots(TCGContext *s,
5593 unsigned nmov, TCGMovExtend *mov,
5594 const TCGLdstHelperParam *parm)
Richard Henderson8429a1c2023-04-09 22:59:09 -07005595{
Richard Henderson8d314042023-05-14 10:07:22 -07005596 unsigned i;
Richard Henderson2462e302023-05-14 09:58:39 -07005597 TCGReg dst3;
5598
Richard Henderson8d314042023-05-14 10:07:22 -07005599 /*
5600 * Start from the end, storing to the stack first.
5601 * This frees those registers, so we need not consider overlap.
5602 */
5603 for (i = nmov; i-- > 0; ) {
5604 unsigned slot = mov[i].dst;
5605
5606 if (arg_slot_reg_p(slot)) {
5607 goto found_reg;
5608 }
5609
5610 TCGReg src = mov[i].src;
5611 TCGType dst_type = mov[i].dst_type;
5612 MemOp dst_mo = dst_type == TCG_TYPE_I32 ? MO_32 : MO_64;
5613
5614 /* The argument is going onto the stack; extend into scratch. */
5615 if ((mov[i].src_ext & MO_SIZE) != dst_mo) {
5616 tcg_debug_assert(parm->ntmp != 0);
5617 mov[i].dst = src = parm->tmp[0];
5618 tcg_out_movext1(s, &mov[i]);
5619 }
5620
5621 tcg_out_st(s, dst_type, src, TCG_REG_CALL_STACK,
5622 tcg_out_helper_stk_ofs(dst_type, slot));
5623 }
5624 return;
5625
5626 found_reg:
5627 /*
5628 * The remaining arguments are in registers.
5629 * Convert slot numbers to argument registers.
5630 */
5631 nmov = i + 1;
5632 for (i = 0; i < nmov; ++i) {
5633 mov[i].dst = tcg_target_call_iarg_regs[mov[i].dst];
5634 }
5635
Richard Henderson8429a1c2023-04-09 22:59:09 -07005636 switch (nmov) {
Richard Henderson2462e302023-05-14 09:58:39 -07005637 case 4:
Richard Henderson8429a1c2023-04-09 22:59:09 -07005638 /* The backend must have provided enough temps for the worst case. */
Richard Henderson2462e302023-05-14 09:58:39 -07005639 tcg_debug_assert(parm->ntmp >= 2);
Richard Henderson8429a1c2023-04-09 22:59:09 -07005640
Richard Henderson2462e302023-05-14 09:58:39 -07005641 dst3 = mov[3].dst;
5642 for (unsigned j = 0; j < 3; ++j) {
5643 if (dst3 == mov[j].src) {
5644 /*
5645 * Conflict. Copy the source to a temporary, perform the
5646 * remaining moves, then the extension from our scratch
5647 * on the way out.
5648 */
5649 TCGReg scratch = parm->tmp[1];
Richard Henderson8429a1c2023-04-09 22:59:09 -07005650
Richard Henderson2462e302023-05-14 09:58:39 -07005651 tcg_out_mov(s, mov[3].src_type, scratch, mov[3].src);
5652 tcg_out_movext3(s, mov, mov + 1, mov + 2, parm->tmp[0]);
5653 tcg_out_movext1_new_src(s, &mov[3], scratch);
5654 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005655 }
Richard Henderson8429a1c2023-04-09 22:59:09 -07005656 }
Richard Henderson8429a1c2023-04-09 22:59:09 -07005657
Richard Henderson2462e302023-05-14 09:58:39 -07005658 /* No conflicts: perform this move and continue. */
5659 tcg_out_movext1(s, &mov[3]);
5660 /* fall through */
5661
5662 case 3:
5663 tcg_out_movext3(s, mov, mov + 1, mov + 2,
5664 parm->ntmp ? parm->tmp[0] : -1);
5665 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005666 case 2:
Richard Henderson2462e302023-05-14 09:58:39 -07005667 tcg_out_movext2(s, mov, mov + 1,
5668 parm->ntmp ? parm->tmp[0] : -1);
5669 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005670 case 1:
5671 tcg_out_movext1(s, mov);
Richard Henderson2462e302023-05-14 09:58:39 -07005672 break;
5673 default:
Richard Henderson8429a1c2023-04-09 22:59:09 -07005674 g_assert_not_reached();
5675 }
5676}
5677
Richard Henderson8429a1c2023-04-09 22:59:09 -07005678static void tcg_out_helper_load_imm(TCGContext *s, unsigned slot,
5679 TCGType type, tcg_target_long imm,
5680 const TCGLdstHelperParam *parm)
5681{
5682 if (arg_slot_reg_p(slot)) {
5683 tcg_out_movi(s, type, tcg_target_call_iarg_regs[slot], imm);
5684 } else {
5685 int ofs = tcg_out_helper_stk_ofs(type, slot);
5686 if (!tcg_out_sti(s, type, imm, TCG_REG_CALL_STACK, ofs)) {
5687 tcg_debug_assert(parm->ntmp != 0);
5688 tcg_out_movi(s, type, parm->tmp[0], imm);
5689 tcg_out_st(s, type, parm->tmp[0], TCG_REG_CALL_STACK, ofs);
5690 }
5691 }
5692}
5693
5694static void tcg_out_helper_load_common_args(TCGContext *s,
5695 const TCGLabelQemuLdst *ldst,
5696 const TCGLdstHelperParam *parm,
5697 const TCGHelperInfo *info,
5698 unsigned next_arg)
5699{
5700 TCGMovExtend ptr_mov = {
5701 .dst_type = TCG_TYPE_PTR,
5702 .src_type = TCG_TYPE_PTR,
5703 .src_ext = sizeof(void *) == 4 ? MO_32 : MO_64
5704 };
5705 const TCGCallArgumentLoc *loc = &info->in[0];
5706 TCGType type;
5707 unsigned slot;
5708 tcg_target_ulong imm;
5709
5710 /*
5711 * Handle env, which is always first.
5712 */
5713 ptr_mov.dst = loc->arg_slot;
5714 ptr_mov.src = TCG_AREG0;
5715 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm);
5716
5717 /*
5718 * Handle oi.
5719 */
5720 imm = ldst->oi;
5721 loc = &info->in[next_arg];
5722 type = TCG_TYPE_I32;
5723 switch (loc->kind) {
5724 case TCG_CALL_ARG_NORMAL:
5725 break;
5726 case TCG_CALL_ARG_EXTEND_U:
5727 case TCG_CALL_ARG_EXTEND_S:
5728 /* No extension required for MemOpIdx. */
5729 tcg_debug_assert(imm <= INT32_MAX);
5730 type = TCG_TYPE_REG;
5731 break;
5732 default:
5733 g_assert_not_reached();
5734 }
5735 tcg_out_helper_load_imm(s, loc->arg_slot, type, imm, parm);
5736 next_arg++;
5737
5738 /*
5739 * Handle ra.
5740 */
5741 loc = &info->in[next_arg];
5742 slot = loc->arg_slot;
5743 if (parm->ra_gen) {
5744 int arg_reg = -1;
5745 TCGReg ra_reg;
5746
5747 if (arg_slot_reg_p(slot)) {
5748 arg_reg = tcg_target_call_iarg_regs[slot];
5749 }
5750 ra_reg = parm->ra_gen(s, ldst, arg_reg);
5751
5752 ptr_mov.dst = slot;
5753 ptr_mov.src = ra_reg;
5754 tcg_out_helper_load_slots(s, 1, &ptr_mov, parm);
5755 } else {
5756 imm = (uintptr_t)ldst->raddr;
5757 tcg_out_helper_load_imm(s, slot, TCG_TYPE_PTR, imm, parm);
5758 }
5759}
5760
5761static unsigned tcg_out_helper_add_mov(TCGMovExtend *mov,
5762 const TCGCallArgumentLoc *loc,
5763 TCGType dst_type, TCGType src_type,
5764 TCGReg lo, TCGReg hi)
5765{
Richard Hendersonebebea52023-04-17 10:20:51 +02005766 MemOp reg_mo;
5767
Richard Henderson8429a1c2023-04-09 22:59:09 -07005768 if (dst_type <= TCG_TYPE_REG) {
5769 MemOp src_ext;
5770
5771 switch (loc->kind) {
5772 case TCG_CALL_ARG_NORMAL:
5773 src_ext = src_type == TCG_TYPE_I32 ? MO_32 : MO_64;
5774 break;
5775 case TCG_CALL_ARG_EXTEND_U:
5776 dst_type = TCG_TYPE_REG;
5777 src_ext = MO_UL;
5778 break;
5779 case TCG_CALL_ARG_EXTEND_S:
5780 dst_type = TCG_TYPE_REG;
5781 src_ext = MO_SL;
5782 break;
5783 default:
5784 g_assert_not_reached();
5785 }
5786
5787 mov[0].dst = loc->arg_slot;
5788 mov[0].dst_type = dst_type;
5789 mov[0].src = lo;
5790 mov[0].src_type = src_type;
5791 mov[0].src_ext = src_ext;
5792 return 1;
5793 }
5794
Richard Hendersonebebea52023-04-17 10:20:51 +02005795 if (TCG_TARGET_REG_BITS == 32) {
5796 assert(dst_type == TCG_TYPE_I64);
5797 reg_mo = MO_32;
5798 } else {
5799 assert(dst_type == TCG_TYPE_I128);
5800 reg_mo = MO_64;
5801 }
Richard Henderson8429a1c2023-04-09 22:59:09 -07005802
5803 mov[0].dst = loc[HOST_BIG_ENDIAN].arg_slot;
5804 mov[0].src = lo;
Richard Hendersonebebea52023-04-17 10:20:51 +02005805 mov[0].dst_type = TCG_TYPE_REG;
5806 mov[0].src_type = TCG_TYPE_REG;
5807 mov[0].src_ext = reg_mo;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005808
5809 mov[1].dst = loc[!HOST_BIG_ENDIAN].arg_slot;
5810 mov[1].src = hi;
Richard Hendersonebebea52023-04-17 10:20:51 +02005811 mov[1].dst_type = TCG_TYPE_REG;
5812 mov[1].src_type = TCG_TYPE_REG;
5813 mov[1].src_ext = reg_mo;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005814
5815 return 2;
5816}
5817
5818static void tcg_out_ld_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst,
5819 const TCGLdstHelperParam *parm)
5820{
5821 const TCGHelperInfo *info;
5822 const TCGCallArgumentLoc *loc;
5823 TCGMovExtend mov[2];
5824 unsigned next_arg, nmov;
5825 MemOp mop = get_memop(ldst->oi);
5826
5827 switch (mop & MO_SIZE) {
5828 case MO_8:
5829 case MO_16:
5830 case MO_32:
5831 info = &info_helper_ld32_mmu;
5832 break;
5833 case MO_64:
5834 info = &info_helper_ld64_mmu;
5835 break;
Richard Hendersonebebea52023-04-17 10:20:51 +02005836 case MO_128:
5837 info = &info_helper_ld128_mmu;
5838 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005839 default:
5840 g_assert_not_reached();
5841 }
5842
5843 /* Defer env argument. */
5844 next_arg = 1;
5845
5846 loc = &info->in[next_arg];
Richard Hendersonc31e5fa2023-04-28 09:14:17 +01005847 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) {
Richard Henderson24e46e62023-04-26 22:09:47 +01005848 /*
5849 * 32-bit host with 32-bit guest: zero-extend the guest address
5850 * to 64-bits for the helper by storing the low part, then
5851 * load a zero for the high part.
5852 */
5853 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN,
5854 TCG_TYPE_I32, TCG_TYPE_I32,
5855 ldst->addrlo_reg, -1);
5856 tcg_out_helper_load_slots(s, 1, mov, parm);
Richard Henderson8429a1c2023-04-09 22:59:09 -07005857
Richard Henderson24e46e62023-04-26 22:09:47 +01005858 tcg_out_helper_load_imm(s, loc[!HOST_BIG_ENDIAN].arg_slot,
5859 TCG_TYPE_I32, 0, parm);
5860 next_arg += 2;
Richard Hendersonc31e5fa2023-04-28 09:14:17 +01005861 } else {
5862 nmov = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, s->addr_type,
5863 ldst->addrlo_reg, ldst->addrhi_reg);
5864 tcg_out_helper_load_slots(s, nmov, mov, parm);
5865 next_arg += nmov;
Richard Henderson24e46e62023-04-26 22:09:47 +01005866 }
Richard Henderson8429a1c2023-04-09 22:59:09 -07005867
Richard Hendersonebebea52023-04-17 10:20:51 +02005868 switch (info->out_kind) {
5869 case TCG_CALL_RET_NORMAL:
5870 case TCG_CALL_RET_BY_VEC:
5871 break;
5872 case TCG_CALL_RET_BY_REF:
5873 /*
5874 * The return reference is in the first argument slot.
5875 * We need memory in which to return: re-use the top of stack.
5876 */
5877 {
5878 int ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET;
5879
5880 if (arg_slot_reg_p(0)) {
5881 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[0],
5882 TCG_REG_CALL_STACK, ofs_slot0);
5883 } else {
5884 tcg_debug_assert(parm->ntmp != 0);
5885 tcg_out_addi_ptr(s, parm->tmp[0],
5886 TCG_REG_CALL_STACK, ofs_slot0);
5887 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0],
5888 TCG_REG_CALL_STACK, ofs_slot0);
5889 }
5890 }
5891 break;
5892 default:
5893 g_assert_not_reached();
5894 }
Richard Henderson8429a1c2023-04-09 22:59:09 -07005895
5896 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg);
5897}
5898
5899static void tcg_out_ld_helper_ret(TCGContext *s, const TCGLabelQemuLdst *ldst,
5900 bool load_sign,
5901 const TCGLdstHelperParam *parm)
5902{
Richard Hendersonebebea52023-04-17 10:20:51 +02005903 MemOp mop = get_memop(ldst->oi);
Richard Henderson8429a1c2023-04-09 22:59:09 -07005904 TCGMovExtend mov[2];
Richard Hendersonebebea52023-04-17 10:20:51 +02005905 int ofs_slot0;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005906
Richard Hendersonebebea52023-04-17 10:20:51 +02005907 switch (ldst->type) {
5908 case TCG_TYPE_I64:
5909 if (TCG_TARGET_REG_BITS == 32) {
5910 break;
5911 }
5912 /* fall through */
Richard Henderson8429a1c2023-04-09 22:59:09 -07005913
Richard Hendersonebebea52023-04-17 10:20:51 +02005914 case TCG_TYPE_I32:
Richard Henderson8429a1c2023-04-09 22:59:09 -07005915 mov[0].dst = ldst->datalo_reg;
5916 mov[0].src = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, 0);
5917 mov[0].dst_type = ldst->type;
5918 mov[0].src_type = TCG_TYPE_REG;
5919
5920 /*
5921 * If load_sign, then we allowed the helper to perform the
5922 * appropriate sign extension to tcg_target_ulong, and all
5923 * we need now is a plain move.
5924 *
5925 * If they do not, then we expect the relevant extension
5926 * instruction to be no more expensive than a move, and
5927 * we thus save the icache etc by only using one of two
5928 * helper functions.
5929 */
5930 if (load_sign || !(mop & MO_SIGN)) {
5931 if (TCG_TARGET_REG_BITS == 32 || ldst->type == TCG_TYPE_I32) {
5932 mov[0].src_ext = MO_32;
5933 } else {
5934 mov[0].src_ext = MO_64;
5935 }
5936 } else {
5937 mov[0].src_ext = mop & MO_SSIZE;
5938 }
5939 tcg_out_movext1(s, mov);
Richard Hendersonebebea52023-04-17 10:20:51 +02005940 return;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005941
Richard Hendersonebebea52023-04-17 10:20:51 +02005942 case TCG_TYPE_I128:
5943 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
5944 ofs_slot0 = TCG_TARGET_CALL_STACK_OFFSET;
5945 switch (TCG_TARGET_CALL_RET_I128) {
5946 case TCG_CALL_RET_NORMAL:
5947 break;
5948 case TCG_CALL_RET_BY_VEC:
5949 tcg_out_st(s, TCG_TYPE_V128,
5950 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
5951 TCG_REG_CALL_STACK, ofs_slot0);
5952 /* fall through */
5953 case TCG_CALL_RET_BY_REF:
5954 tcg_out_ld(s, TCG_TYPE_I64, ldst->datalo_reg,
5955 TCG_REG_CALL_STACK, ofs_slot0 + 8 * HOST_BIG_ENDIAN);
5956 tcg_out_ld(s, TCG_TYPE_I64, ldst->datahi_reg,
5957 TCG_REG_CALL_STACK, ofs_slot0 + 8 * !HOST_BIG_ENDIAN);
5958 return;
5959 default:
5960 g_assert_not_reached();
5961 }
5962 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07005963
Richard Hendersonebebea52023-04-17 10:20:51 +02005964 default:
5965 g_assert_not_reached();
Richard Henderson8429a1c2023-04-09 22:59:09 -07005966 }
Richard Hendersonebebea52023-04-17 10:20:51 +02005967
5968 mov[0].dst = ldst->datalo_reg;
5969 mov[0].src =
5970 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, HOST_BIG_ENDIAN);
Richard Henderson723d3a22023-05-24 12:59:12 -07005971 mov[0].dst_type = TCG_TYPE_REG;
5972 mov[0].src_type = TCG_TYPE_REG;
Richard Hendersonebebea52023-04-17 10:20:51 +02005973 mov[0].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64;
5974
5975 mov[1].dst = ldst->datahi_reg;
5976 mov[1].src =
5977 tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, !HOST_BIG_ENDIAN);
5978 mov[1].dst_type = TCG_TYPE_REG;
5979 mov[1].src_type = TCG_TYPE_REG;
5980 mov[1].src_ext = TCG_TARGET_REG_BITS == 32 ? MO_32 : MO_64;
5981
5982 tcg_out_movext2(s, mov, mov + 1, parm->ntmp ? parm->tmp[0] : -1);
Richard Henderson8429a1c2023-04-09 22:59:09 -07005983}
5984
5985static void tcg_out_st_helper_args(TCGContext *s, const TCGLabelQemuLdst *ldst,
5986 const TCGLdstHelperParam *parm)
5987{
5988 const TCGHelperInfo *info;
5989 const TCGCallArgumentLoc *loc;
5990 TCGMovExtend mov[4];
5991 TCGType data_type;
5992 unsigned next_arg, nmov, n;
5993 MemOp mop = get_memop(ldst->oi);
5994
5995 switch (mop & MO_SIZE) {
5996 case MO_8:
5997 case MO_16:
5998 case MO_32:
5999 info = &info_helper_st32_mmu;
6000 data_type = TCG_TYPE_I32;
6001 break;
6002 case MO_64:
6003 info = &info_helper_st64_mmu;
6004 data_type = TCG_TYPE_I64;
6005 break;
Richard Hendersonebebea52023-04-17 10:20:51 +02006006 case MO_128:
6007 info = &info_helper_st128_mmu;
6008 data_type = TCG_TYPE_I128;
6009 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07006010 default:
6011 g_assert_not_reached();
6012 }
6013
6014 /* Defer env argument. */
6015 next_arg = 1;
6016 nmov = 0;
6017
6018 /* Handle addr argument. */
6019 loc = &info->in[next_arg];
Richard Hendersonc31e5fa2023-04-28 09:14:17 +01006020 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) {
Richard Henderson24e46e62023-04-26 22:09:47 +01006021 /*
6022 * 32-bit host with 32-bit guest: zero-extend the guest address
6023 * to 64-bits for the helper by storing the low part. Later,
6024 * after we have processed the register inputs, we will load a
6025 * zero for the high part.
6026 */
6027 tcg_out_helper_add_mov(mov, loc + HOST_BIG_ENDIAN,
6028 TCG_TYPE_I32, TCG_TYPE_I32,
6029 ldst->addrlo_reg, -1);
6030 next_arg += 2;
6031 nmov += 1;
Richard Hendersonc31e5fa2023-04-28 09:14:17 +01006032 } else {
6033 n = tcg_out_helper_add_mov(mov, loc, TCG_TYPE_I64, s->addr_type,
6034 ldst->addrlo_reg, ldst->addrhi_reg);
6035 next_arg += n;
6036 nmov += n;
Richard Henderson24e46e62023-04-26 22:09:47 +01006037 }
Richard Henderson8429a1c2023-04-09 22:59:09 -07006038
6039 /* Handle data argument. */
6040 loc = &info->in[next_arg];
Richard Hendersonebebea52023-04-17 10:20:51 +02006041 switch (loc->kind) {
6042 case TCG_CALL_ARG_NORMAL:
6043 case TCG_CALL_ARG_EXTEND_U:
6044 case TCG_CALL_ARG_EXTEND_S:
6045 n = tcg_out_helper_add_mov(mov + nmov, loc, data_type, ldst->type,
6046 ldst->datalo_reg, ldst->datahi_reg);
6047 next_arg += n;
6048 nmov += n;
6049 tcg_out_helper_load_slots(s, nmov, mov, parm);
6050 break;
Richard Henderson8429a1c2023-04-09 22:59:09 -07006051
Richard Hendersonebebea52023-04-17 10:20:51 +02006052 case TCG_CALL_ARG_BY_REF:
6053 tcg_debug_assert(TCG_TARGET_REG_BITS == 64);
6054 tcg_debug_assert(data_type == TCG_TYPE_I128);
6055 tcg_out_st(s, TCG_TYPE_I64,
6056 HOST_BIG_ENDIAN ? ldst->datahi_reg : ldst->datalo_reg,
6057 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[0].ref_slot));
6058 tcg_out_st(s, TCG_TYPE_I64,
6059 HOST_BIG_ENDIAN ? ldst->datalo_reg : ldst->datahi_reg,
6060 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc[1].ref_slot));
6061
6062 tcg_out_helper_load_slots(s, nmov, mov, parm);
6063
6064 if (arg_slot_reg_p(loc->arg_slot)) {
6065 tcg_out_addi_ptr(s, tcg_target_call_iarg_regs[loc->arg_slot],
6066 TCG_REG_CALL_STACK,
6067 arg_slot_stk_ofs(loc->ref_slot));
6068 } else {
6069 tcg_debug_assert(parm->ntmp != 0);
6070 tcg_out_addi_ptr(s, parm->tmp[0], TCG_REG_CALL_STACK,
6071 arg_slot_stk_ofs(loc->ref_slot));
6072 tcg_out_st(s, TCG_TYPE_PTR, parm->tmp[0],
6073 TCG_REG_CALL_STACK, arg_slot_stk_ofs(loc->arg_slot));
6074 }
6075 next_arg += 2;
6076 break;
6077
6078 default:
6079 g_assert_not_reached();
6080 }
6081
Richard Hendersonc31e5fa2023-04-28 09:14:17 +01006082 if (TCG_TARGET_REG_BITS == 32 && s->addr_type == TCG_TYPE_I32) {
6083 /* Zero extend the address by loading a zero for the high part. */
Richard Henderson24e46e62023-04-26 22:09:47 +01006084 loc = &info->in[1 + !HOST_BIG_ENDIAN];
6085 tcg_out_helper_load_imm(s, loc->arg_slot, TCG_TYPE_I32, 0, parm);
6086 }
6087
Richard Henderson8429a1c2023-04-09 22:59:09 -07006088 tcg_out_helper_load_common_args(s, ldst, parm, info, next_arg);
6089}
6090
Richard Henderson76cef4b2023-03-08 16:48:02 -08006091int tcg_gen_code(TCGContext *s, TranslationBlock *tb, uint64_t pc_start)
bellardc896fe22008-02-01 10:05:41 +00006092{
Richard Henderson747bd692023-03-31 21:30:31 -07006093 int i, start_words, num_insns;
Richard Henderson15fa08f2017-11-02 15:19:14 +01006094 TCGOp *op;
bellardc896fe22008-02-01 10:05:41 +00006095
Alex Bennéed977e1c2016-03-15 14:30:21 +00006096 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
Richard Hendersonfbf59aa2022-08-15 15:16:06 -05006097 && qemu_log_in_addr_range(pc_start))) {
Richard Hendersonc60f5992022-04-17 11:29:47 -07006098 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -07006099 if (logfile) {
6100 fprintf(logfile, "OP:\n");
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07006101 tcg_dump_ops(s, logfile, false);
Richard Henderson78b54852022-04-17 11:29:49 -07006102 fprintf(logfile, "\n");
6103 qemu_log_unlock(logfile);
6104 }
bellardc896fe22008-02-01 10:05:41 +00006105 }
bellardc896fe22008-02-01 10:05:41 +00006106
Richard Hendersonbef16ab2019-02-07 13:26:40 +00006107#ifdef CONFIG_DEBUG_TCG
6108 /* Ensure all labels referenced have been emitted. */
6109 {
6110 TCGLabel *l;
6111 bool error = false;
6112
6113 QSIMPLEQ_FOREACH(l, &s->labels, next) {
Richard Hendersonf85b1fc2023-03-03 13:47:27 -08006114 if (unlikely(!l->present) && !QSIMPLEQ_EMPTY(&l->branches)) {
Richard Hendersonbef16ab2019-02-07 13:26:40 +00006115 qemu_log_mask(CPU_LOG_TB_OP,
6116 "$L%d referenced but not present.\n", l->id);
6117 error = true;
6118 }
6119 }
6120 assert(!error);
6121 }
6122#endif
6123
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07006124 tcg_optimize(s);
Kirill Batuzov8f2e8c02011-07-07 16:37:12 +04006125
Richard Hendersonb4fc67c2018-11-26 14:28:28 -08006126 reachable_code_pass(s);
Richard Henderson874b8572023-01-29 11:50:20 -10006127 liveness_pass_0(s);
Richard Hendersonb83eabe2016-11-01 15:56:04 -06006128 liveness_pass_1(s);
Richard Henderson5a184072016-06-23 20:34:33 -07006129
Richard Hendersonb83eabe2016-11-01 15:56:04 -06006130 if (s->nb_indirects > 0) {
Richard Hendersonb83eabe2016-11-01 15:56:04 -06006131 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
Richard Hendersonfbf59aa2022-08-15 15:16:06 -05006132 && qemu_log_in_addr_range(pc_start))) {
Richard Hendersonc60f5992022-04-17 11:29:47 -07006133 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -07006134 if (logfile) {
6135 fprintf(logfile, "OP before indirect lowering:\n");
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07006136 tcg_dump_ops(s, logfile, false);
Richard Henderson78b54852022-04-17 11:29:49 -07006137 fprintf(logfile, "\n");
6138 qemu_log_unlock(logfile);
6139 }
Richard Hendersonb83eabe2016-11-01 15:56:04 -06006140 }
Richard Henderson645e3a82023-04-01 16:06:47 -07006141
Richard Hendersonb83eabe2016-11-01 15:56:04 -06006142 /* Replace indirect temps with direct temps. */
6143 if (liveness_pass_2(s)) {
6144 /* If changes were made, re-run liveness. */
6145 liveness_pass_1(s);
Richard Henderson5a184072016-06-23 20:34:33 -07006146 }
6147 }
Aurelien Jarnoc5cc28f2012-09-06 16:47:13 +02006148
Alex Bennéed977e1c2016-03-15 14:30:21 +00006149 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
Richard Hendersonfbf59aa2022-08-15 15:16:06 -05006150 && qemu_log_in_addr_range(pc_start))) {
Richard Hendersonc60f5992022-04-17 11:29:47 -07006151 FILE *logfile = qemu_log_trylock();
Richard Henderson78b54852022-04-17 11:29:49 -07006152 if (logfile) {
6153 fprintf(logfile, "OP after optimization and liveness analysis:\n");
Richard Hendersonb7a83ff2022-04-17 11:29:51 -07006154 tcg_dump_ops(s, logfile, true);
Richard Henderson78b54852022-04-17 11:29:49 -07006155 fprintf(logfile, "\n");
6156 qemu_log_unlock(logfile);
6157 }
bellardc896fe22008-02-01 10:05:41 +00006158 }
bellardc896fe22008-02-01 10:05:41 +00006159
Richard Henderson35abb002022-11-06 10:55:37 +11006160 /* Initialize goto_tb jump offsets. */
Richard Henderson3a50f422022-11-26 18:20:57 -08006161 tb->jmp_reset_offset[0] = TB_JMP_OFFSET_INVALID;
6162 tb->jmp_reset_offset[1] = TB_JMP_OFFSET_INVALID;
Richard Henderson9da60792022-11-26 18:54:23 -08006163 tb->jmp_insn_offset[0] = TB_JMP_OFFSET_INVALID;
6164 tb->jmp_insn_offset[1] = TB_JMP_OFFSET_INVALID;
Richard Henderson35abb002022-11-06 10:55:37 +11006165
bellardc896fe22008-02-01 10:05:41 +00006166 tcg_reg_alloc_start(s);
6167
Richard Hendersondb0c51a2020-10-28 12:05:44 -07006168 /*
6169 * Reset the buffer pointers when restarting after overflow.
6170 * TODO: Move this into translate-all.c with the rest of the
6171 * buffer management. Having only this done here is confusing.
6172 */
6173 s->code_buf = tcg_splitwx_to_rw(tb->tc.ptr);
6174 s->code_ptr = s->code_buf;
bellardc896fe22008-02-01 10:05:41 +00006175
Richard Henderson659ef5c2017-07-30 12:30:41 -07006176#ifdef TCG_TARGET_NEED_LDST_LABELS
Laurent Vivier6001f772018-04-30 01:58:40 +02006177 QSIMPLEQ_INIT(&s->ldst_labels);
Richard Henderson659ef5c2017-07-30 12:30:41 -07006178#endif
Richard Henderson57a26942017-07-30 13:13:21 -07006179#ifdef TCG_TARGET_NEED_POOL_LABELS
6180 s->pool_labels = NULL;
6181#endif
Richard Henderson9ecefc82013-10-03 14:51:24 -05006182
Richard Henderson747bd692023-03-31 21:30:31 -07006183 start_words = s->insn_start_words;
6184 s->gen_insn_data =
6185 tcg_malloc(sizeof(uint64_t) * s->gen_tb->icount * start_words);
6186
Richard Henderson9358fbb2023-08-15 16:34:59 +00006187 tcg_out_tb_start(s);
6188
Richard Hendersonfca8a502015-09-01 19:11:45 -07006189 num_insns = -1;
Richard Henderson15fa08f2017-11-02 15:19:14 +01006190 QTAILQ_FOREACH(op, &s->ops, link) {
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07006191 TCGOpcode opc = op->opc;
blueswir1b3db8752008-03-08 13:33:42 +00006192
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07006193 switch (opc) {
bellardc896fe22008-02-01 10:05:41 +00006194 case INDEX_op_mov_i32:
bellardc896fe22008-02-01 10:05:41 +00006195 case INDEX_op_mov_i64:
Richard Hendersond2fd7452017-09-14 13:53:46 -07006196 case INDEX_op_mov_vec:
Richard Hendersondd186292016-12-08 13:42:08 -08006197 tcg_reg_alloc_mov(s, op);
bellardc896fe22008-02-01 10:05:41 +00006198 break;
Richard Hendersonbab16712019-03-18 11:20:27 -07006199 case INDEX_op_dup_vec:
6200 tcg_reg_alloc_dup(s, op);
6201 break;
Richard Henderson765b8422015-08-29 12:37:33 -07006202 case INDEX_op_insn_start:
Richard Hendersonfca8a502015-09-01 19:11:45 -07006203 if (num_insns >= 0) {
Richard Henderson9f754622018-06-14 19:57:03 -10006204 size_t off = tcg_current_code_size(s);
6205 s->gen_insn_end_off[num_insns] = off;
6206 /* Assert that we do not overflow our stored offset. */
6207 assert(s->gen_insn_end_off[num_insns] == off);
Richard Hendersonfca8a502015-09-01 19:11:45 -07006208 }
6209 num_insns++;
Richard Henderson747bd692023-03-31 21:30:31 -07006210 for (i = 0; i < start_words; ++i) {
6211 s->gen_insn_data[num_insns * start_words + i] =
Richard Hendersonc9ad8d22023-03-08 12:24:41 -08006212 tcg_get_insn_start_param(op, i);
Richard Hendersonbad729e2015-09-01 15:51:12 -07006213 }
bellardc896fe22008-02-01 10:05:41 +00006214 break;
bellard5ff9d6a2008-02-04 00:37:54 +00006215 case INDEX_op_discard:
Richard Henderson43439132017-06-19 23:18:10 -07006216 temp_dead(s, arg_temp(op->args[0]));
bellard5ff9d6a2008-02-04 00:37:54 +00006217 break;
bellardc896fe22008-02-01 10:05:41 +00006218 case INDEX_op_set_label:
bellarde8996ee2008-05-23 17:33:39 +00006219 tcg_reg_alloc_bb_end(s, s->reserved_regs);
Richard Henderson92ab8e72020-10-28 18:55:50 -07006220 tcg_out_label(s, arg_label(op->args[0]));
bellardc896fe22008-02-01 10:05:41 +00006221 break;
6222 case INDEX_op_call:
Richard Hendersondd186292016-12-08 13:42:08 -08006223 tcg_reg_alloc_call(s, op);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07006224 break;
Richard Hendersonb55a8d92022-11-26 12:42:06 -08006225 case INDEX_op_exit_tb:
6226 tcg_out_exit_tb(s, op->args[0]);
6227 break;
Richard Hendersoncf7d6b82022-11-26 17:14:05 -08006228 case INDEX_op_goto_tb:
6229 tcg_out_goto_tb(s, op->args[0]);
6230 break;
Richard Hendersonefe86b22020-03-31 02:33:21 -07006231 case INDEX_op_dup2_vec:
6232 if (tcg_reg_alloc_dup2(s, op)) {
6233 break;
6234 }
6235 /* fall through */
bellardc896fe22008-02-01 10:05:41 +00006236 default:
Richard Henderson25c4d9c2011-08-17 14:11:46 -07006237 /* Sanity check that we've not introduced any unhandled opcodes. */
Richard Hendersonbe0f34b2017-08-17 07:43:20 -07006238 tcg_debug_assert(tcg_op_supported(opc));
bellardc896fe22008-02-01 10:05:41 +00006239 /* Note: in order to speed up the code, it would be much
6240 faster to have specialized register allocator functions for
6241 some common argument patterns */
Richard Hendersondd186292016-12-08 13:42:08 -08006242 tcg_reg_alloc_op(s, op);
bellardc896fe22008-02-01 10:05:41 +00006243 break;
6244 }
Richard Hendersonb125f9d2015-09-22 13:01:15 -07006245 /* Test for (pending) buffer overflow. The assumption is that any
6246 one operation beginning below the high water mark cannot overrun
6247 the buffer completely. Thus we can test for overflow after
6248 generating code without having to check during generation. */
John Clarke644da9b2015-11-19 10:30:50 +01006249 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
Richard Hendersonb125f9d2015-09-22 13:01:15 -07006250 return -1;
6251 }
Richard Henderson6e6c4ef2019-04-15 22:06:39 -10006252 /* Test for TB overflow, as seen by gen_insn_end_off. */
6253 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
6254 return -2;
6255 }
bellardc896fe22008-02-01 10:05:41 +00006256 }
Richard Henderson747bd692023-03-31 21:30:31 -07006257 tcg_debug_assert(num_insns + 1 == s->gen_tb->icount);
Richard Hendersonfca8a502015-09-01 19:11:45 -07006258 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
Richard Hendersonc45cb8b2014-09-19 13:49:15 -07006259
Yeongkyoon Leeb76f0d82012-10-31 16:04:25 +09006260 /* Generate TB finalization at the end of block */
Richard Henderson659ef5c2017-07-30 12:30:41 -07006261#ifdef TCG_TARGET_NEED_LDST_LABELS
Richard Hendersonaeee05f2019-04-21 14:51:00 -07006262 i = tcg_out_ldst_finalize(s);
6263 if (i < 0) {
6264 return i;
Richard Henderson23dceda2015-12-02 13:59:59 -08006265 }
Richard Henderson659ef5c2017-07-30 12:30:41 -07006266#endif
Richard Henderson57a26942017-07-30 13:13:21 -07006267#ifdef TCG_TARGET_NEED_POOL_LABELS
Richard Henderson17689872019-04-21 13:51:56 -07006268 i = tcg_out_pool_finalize(s);
6269 if (i < 0) {
6270 return i;
Richard Henderson57a26942017-07-30 13:13:21 -07006271 }
6272#endif
Richard Henderson7ecd02a2019-04-21 13:34:35 -07006273 if (!tcg_resolve_relocs(s)) {
6274 return -2;
6275 }
bellardc896fe22008-02-01 10:05:41 +00006276
Richard Hendersondf5d2b12020-12-12 09:08:02 -06006277#ifndef CONFIG_TCG_INTERPRETER
bellardc896fe22008-02-01 10:05:41 +00006278 /* flush instruction cache */
Richard Hendersondb0c51a2020-10-28 12:05:44 -07006279 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
6280 (uintptr_t)s->code_buf,
Richard Henderson1da8de32020-12-12 10:38:21 -06006281 tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
Richard Hendersondf5d2b12020-12-12 09:08:02 -06006282#endif
Stefan Weil2aeabc02012-03-02 23:30:07 +01006283
Richard Henderson1813e172014-03-28 12:56:22 -07006284 return tcg_current_code_size(s);
bellardc896fe22008-02-01 10:05:41 +00006285}
6286
Richard Henderson813da622012-03-19 12:25:11 -07006287#ifdef ELF_HOST_MACHINE
Richard Henderson5872bbf2012-03-24 10:47:36 -07006288/* In order to use this feature, the backend needs to do three things:
6289
6290 (1) Define ELF_HOST_MACHINE to indicate both what value to
6291 put into the ELF image and to indicate support for the feature.
6292
6293 (2) Define tcg_register_jit. This should create a buffer containing
6294 the contents of a .debug_frame section that describes the post-
6295 prologue unwind info for the tcg machine.
6296
6297 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
6298*/
Richard Henderson813da622012-03-19 12:25:11 -07006299
6300/* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
6301typedef enum {
6302 JIT_NOACTION = 0,
6303 JIT_REGISTER_FN,
6304 JIT_UNREGISTER_FN
6305} jit_actions_t;
6306
6307struct jit_code_entry {
6308 struct jit_code_entry *next_entry;
6309 struct jit_code_entry *prev_entry;
6310 const void *symfile_addr;
6311 uint64_t symfile_size;
6312};
6313
6314struct jit_descriptor {
6315 uint32_t version;
6316 uint32_t action_flag;
6317 struct jit_code_entry *relevant_entry;
6318 struct jit_code_entry *first_entry;
6319};
6320
6321void __jit_debug_register_code(void) __attribute__((noinline));
6322void __jit_debug_register_code(void)
6323{
6324 asm("");
6325}
6326
6327/* Must statically initialize the version, because GDB may check
6328 the version before we can set it. */
6329struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
6330
6331/* End GDB interface. */
6332
6333static int find_string(const char *strtab, const char *str)
6334{
6335 const char *p = strtab + 1;
6336
6337 while (1) {
6338 if (strcmp(p, str) == 0) {
6339 return p - strtab;
6340 }
6341 p += strlen(p) + 1;
6342 }
6343}
6344
Richard Henderson755bf9e2020-10-29 09:17:30 -07006345static void tcg_register_jit_int(const void *buf_ptr, size_t buf_size,
Richard Henderson2c907842014-05-15 12:48:01 -07006346 const void *debug_frame,
6347 size_t debug_frame_size)
Richard Henderson813da622012-03-19 12:25:11 -07006348{
Richard Henderson5872bbf2012-03-24 10:47:36 -07006349 struct __attribute__((packed)) DebugInfo {
6350 uint32_t len;
6351 uint16_t version;
6352 uint32_t abbrev;
6353 uint8_t ptr_size;
6354 uint8_t cu_die;
6355 uint16_t cu_lang;
6356 uintptr_t cu_low_pc;
6357 uintptr_t cu_high_pc;
6358 uint8_t fn_die;
6359 char fn_name[16];
6360 uintptr_t fn_low_pc;
6361 uintptr_t fn_high_pc;
6362 uint8_t cu_eoc;
6363 };
Richard Henderson813da622012-03-19 12:25:11 -07006364
6365 struct ElfImage {
6366 ElfW(Ehdr) ehdr;
6367 ElfW(Phdr) phdr;
Richard Henderson5872bbf2012-03-24 10:47:36 -07006368 ElfW(Shdr) shdr[7];
6369 ElfW(Sym) sym[2];
6370 struct DebugInfo di;
6371 uint8_t da[24];
6372 char str[80];
6373 };
6374
6375 struct ElfImage *img;
6376
6377 static const struct ElfImage img_template = {
6378 .ehdr = {
6379 .e_ident[EI_MAG0] = ELFMAG0,
6380 .e_ident[EI_MAG1] = ELFMAG1,
6381 .e_ident[EI_MAG2] = ELFMAG2,
6382 .e_ident[EI_MAG3] = ELFMAG3,
6383 .e_ident[EI_CLASS] = ELF_CLASS,
6384 .e_ident[EI_DATA] = ELF_DATA,
6385 .e_ident[EI_VERSION] = EV_CURRENT,
6386 .e_type = ET_EXEC,
6387 .e_machine = ELF_HOST_MACHINE,
6388 .e_version = EV_CURRENT,
6389 .e_phoff = offsetof(struct ElfImage, phdr),
6390 .e_shoff = offsetof(struct ElfImage, shdr),
6391 .e_ehsize = sizeof(ElfW(Shdr)),
6392 .e_phentsize = sizeof(ElfW(Phdr)),
6393 .e_phnum = 1,
6394 .e_shentsize = sizeof(ElfW(Shdr)),
6395 .e_shnum = ARRAY_SIZE(img->shdr),
6396 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
Richard Hendersonabbb3ea2012-03-24 10:47:37 -07006397#ifdef ELF_HOST_FLAGS
6398 .e_flags = ELF_HOST_FLAGS,
6399#endif
6400#ifdef ELF_OSABI
6401 .e_ident[EI_OSABI] = ELF_OSABI,
6402#endif
Richard Henderson5872bbf2012-03-24 10:47:36 -07006403 },
6404 .phdr = {
6405 .p_type = PT_LOAD,
6406 .p_flags = PF_X,
6407 },
6408 .shdr = {
6409 [0] = { .sh_type = SHT_NULL },
6410 /* Trick: The contents of code_gen_buffer are not present in
6411 this fake ELF file; that got allocated elsewhere. Therefore
6412 we mark .text as SHT_NOBITS (similar to .bss) so that readers
6413 will not look for contents. We can record any address. */
6414 [1] = { /* .text */
6415 .sh_type = SHT_NOBITS,
6416 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
6417 },
6418 [2] = { /* .debug_info */
6419 .sh_type = SHT_PROGBITS,
6420 .sh_offset = offsetof(struct ElfImage, di),
6421 .sh_size = sizeof(struct DebugInfo),
6422 },
6423 [3] = { /* .debug_abbrev */
6424 .sh_type = SHT_PROGBITS,
6425 .sh_offset = offsetof(struct ElfImage, da),
6426 .sh_size = sizeof(img->da),
6427 },
6428 [4] = { /* .debug_frame */
6429 .sh_type = SHT_PROGBITS,
6430 .sh_offset = sizeof(struct ElfImage),
6431 },
6432 [5] = { /* .symtab */
6433 .sh_type = SHT_SYMTAB,
6434 .sh_offset = offsetof(struct ElfImage, sym),
6435 .sh_size = sizeof(img->sym),
6436 .sh_info = 1,
6437 .sh_link = ARRAY_SIZE(img->shdr) - 1,
6438 .sh_entsize = sizeof(ElfW(Sym)),
6439 },
6440 [6] = { /* .strtab */
6441 .sh_type = SHT_STRTAB,
6442 .sh_offset = offsetof(struct ElfImage, str),
6443 .sh_size = sizeof(img->str),
6444 }
6445 },
6446 .sym = {
6447 [1] = { /* code_gen_buffer */
6448 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
6449 .st_shndx = 1,
6450 }
6451 },
6452 .di = {
6453 .len = sizeof(struct DebugInfo) - 4,
6454 .version = 2,
6455 .ptr_size = sizeof(void *),
6456 .cu_die = 1,
6457 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
6458 .fn_die = 2,
6459 .fn_name = "code_gen_buffer"
6460 },
6461 .da = {
6462 1, /* abbrev number (the cu) */
6463 0x11, 1, /* DW_TAG_compile_unit, has children */
6464 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
6465 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
6466 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
6467 0, 0, /* end of abbrev */
6468 2, /* abbrev number (the fn) */
6469 0x2e, 0, /* DW_TAG_subprogram, no children */
6470 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
6471 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
6472 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
6473 0, 0, /* end of abbrev */
6474 0 /* no more abbrev */
6475 },
6476 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
6477 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
Richard Henderson813da622012-03-19 12:25:11 -07006478 };
6479
6480 /* We only need a single jit entry; statically allocate it. */
6481 static struct jit_code_entry one_entry;
6482
Richard Henderson5872bbf2012-03-24 10:47:36 -07006483 uintptr_t buf = (uintptr_t)buf_ptr;
Richard Henderson813da622012-03-19 12:25:11 -07006484 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
Richard Henderson2c907842014-05-15 12:48:01 -07006485 DebugFrameHeader *dfh;
Richard Henderson813da622012-03-19 12:25:11 -07006486
Richard Henderson5872bbf2012-03-24 10:47:36 -07006487 img = g_malloc(img_size);
6488 *img = img_template;
Richard Henderson813da622012-03-19 12:25:11 -07006489
Richard Henderson5872bbf2012-03-24 10:47:36 -07006490 img->phdr.p_vaddr = buf;
6491 img->phdr.p_paddr = buf;
6492 img->phdr.p_memsz = buf_size;
Richard Henderson813da622012-03-19 12:25:11 -07006493
Richard Henderson5872bbf2012-03-24 10:47:36 -07006494 img->shdr[1].sh_name = find_string(img->str, ".text");
6495 img->shdr[1].sh_addr = buf;
6496 img->shdr[1].sh_size = buf_size;
Richard Henderson813da622012-03-19 12:25:11 -07006497
Richard Henderson5872bbf2012-03-24 10:47:36 -07006498 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
6499 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
6500
6501 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
6502 img->shdr[4].sh_size = debug_frame_size;
6503
6504 img->shdr[5].sh_name = find_string(img->str, ".symtab");
6505 img->shdr[6].sh_name = find_string(img->str, ".strtab");
6506
6507 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
6508 img->sym[1].st_value = buf;
6509 img->sym[1].st_size = buf_size;
6510
6511 img->di.cu_low_pc = buf;
Richard Henderson45aba092013-05-24 14:16:14 -07006512 img->di.cu_high_pc = buf + buf_size;
Richard Henderson5872bbf2012-03-24 10:47:36 -07006513 img->di.fn_low_pc = buf;
Richard Henderson45aba092013-05-24 14:16:14 -07006514 img->di.fn_high_pc = buf + buf_size;
Richard Henderson813da622012-03-19 12:25:11 -07006515
Richard Henderson2c907842014-05-15 12:48:01 -07006516 dfh = (DebugFrameHeader *)(img + 1);
6517 memcpy(dfh, debug_frame, debug_frame_size);
6518 dfh->fde.func_start = buf;
6519 dfh->fde.func_len = buf_size;
6520
Richard Henderson813da622012-03-19 12:25:11 -07006521#ifdef DEBUG_JIT
6522 /* Enable this block to be able to debug the ELF image file creation.
6523 One can use readelf, objdump, or other inspection utilities. */
6524 {
Bin Mengeb6b2ed2022-10-27 19:36:17 +01006525 g_autofree char *jit = g_strdup_printf("%s/qemu.jit", g_get_tmp_dir());
6526 FILE *f = fopen(jit, "w+b");
Richard Henderson813da622012-03-19 12:25:11 -07006527 if (f) {
Richard Henderson5872bbf2012-03-24 10:47:36 -07006528 if (fwrite(img, img_size, 1, f) != img_size) {
Richard Henderson813da622012-03-19 12:25:11 -07006529 /* Avoid stupid unused return value warning for fwrite. */
6530 }
6531 fclose(f);
6532 }
6533 }
6534#endif
6535
6536 one_entry.symfile_addr = img;
6537 one_entry.symfile_size = img_size;
6538
6539 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
6540 __jit_debug_descriptor.relevant_entry = &one_entry;
6541 __jit_debug_descriptor.first_entry = &one_entry;
6542 __jit_debug_register_code();
6543}
6544#else
Richard Henderson5872bbf2012-03-24 10:47:36 -07006545/* No support for the feature. Provide the entry point expected by exec.c,
6546 and implement the internal function we declared earlier. */
Richard Henderson813da622012-03-19 12:25:11 -07006547
Richard Henderson755bf9e2020-10-29 09:17:30 -07006548static void tcg_register_jit_int(const void *buf, size_t size,
Richard Henderson2c907842014-05-15 12:48:01 -07006549 const void *debug_frame,
6550 size_t debug_frame_size)
Richard Henderson813da622012-03-19 12:25:11 -07006551{
6552}
6553
Richard Henderson755bf9e2020-10-29 09:17:30 -07006554void tcg_register_jit(const void *buf, size_t buf_size)
Richard Henderson813da622012-03-19 12:25:11 -07006555{
6556}
6557#endif /* ELF_HOST_MACHINE */
Richard Hendersondb432672017-09-15 14:11:45 -07006558
6559#if !TCG_TARGET_MAYBE_vec
6560void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
6561{
6562 g_assert_not_reached();
6563}
6564#endif