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