]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/tcg.c
tcg: Split out tcg_out_ext8u
[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);
678155b2 108static void tcg_out_ext8s(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
d0e66c89 109static void tcg_out_ext8u(TCGContext *s, TCGReg ret, TCGReg arg);
313bdea8 110static void tcg_out_addi_ptr(TCGContext *s, TCGReg, TCGReg, tcg_target_long);
b55a8d9d 111static void tcg_out_exit_tb(TCGContext *s, uintptr_t arg);
cf7d6b8e 112static void tcg_out_goto_tb(TCGContext *s, int which);
5e8892db
MR
113static void tcg_out_op(TCGContext *s, TCGOpcode opc,
114 const TCGArg args[TCG_MAX_OP_ARGS],
115 const int const_args[TCG_MAX_OP_ARGS]);
d2fd745f 116#if TCG_TARGET_MAYBE_vec
e7632cfa
RH
117static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
118 TCGReg dst, TCGReg src);
d6ecb4a9
RH
119static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
120 TCGReg dst, TCGReg base, intptr_t offset);
4e186175
RH
121static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
122 TCGReg dst, int64_t arg);
5e8892db
MR
123static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
124 unsigned vecl, unsigned vece,
125 const TCGArg args[TCG_MAX_OP_ARGS],
126 const int const_args[TCG_MAX_OP_ARGS]);
d2fd745f 127#else
e7632cfa
RH
128static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
129 TCGReg dst, TCGReg src)
130{
131 g_assert_not_reached();
132}
d6ecb4a9
RH
133static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
134 TCGReg dst, TCGReg base, intptr_t offset)
135{
136 g_assert_not_reached();
137}
4e186175
RH
138static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
139 TCGReg dst, int64_t arg)
e7632cfa
RH
140{
141 g_assert_not_reached();
142}
5e8892db
MR
143static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
144 unsigned vecl, unsigned vece,
145 const TCGArg args[TCG_MAX_OP_ARGS],
146 const int const_args[TCG_MAX_OP_ARGS])
d2fd745f
RH
147{
148 g_assert_not_reached();
149}
150#endif
2a534aff 151static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
a05b5b9b 152 intptr_t arg2);
59d7c14e
RH
153static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
154 TCGReg base, intptr_t ofs);
7b7d8b2d 155static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target,
cee44b03 156 const TCGHelperInfo *info);
5e3d0c19 157static TCGReg tcg_target_call_oarg_reg(TCGCallReturnKind kind, int slot);
a4fbbd77 158static bool tcg_target_const_match(int64_t val, TCGType type, int ct);
659ef5cb 159#ifdef TCG_TARGET_NEED_LDST_LABELS
aeee05f5 160static int tcg_out_ldst_finalize(TCGContext *s);
659ef5cb 161#endif
c896fe29 162
42eb6dfc
RH
163TCGContext tcg_init_ctx;
164__thread TCGContext *tcg_ctx;
165
5ff7258c 166TCGContext **tcg_ctxs;
0e2d61cf
RH
167unsigned int tcg_cur_ctxs;
168unsigned int tcg_max_ctxs;
1c2adb95 169TCGv_env cpu_env = 0;
c8bc1168 170const void *tcg_code_gen_epilogue;
db0c51a3 171uintptr_t tcg_splitwx_diff;
df2cce29 172
b91ccb31
RH
173#ifndef CONFIG_TCG_INTERPRETER
174tcg_prologue_fn *tcg_qemu_tb_exec;
175#endif
176
d2fd745f 177static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
b1d8e52e 178static TCGRegSet tcg_target_call_clobber_regs;
c896fe29 179
1813e175 180#if TCG_TARGET_INSN_UNIT_SIZE == 1
4196dca6 181static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
c896fe29
FB
182{
183 *s->code_ptr++ = v;
184}
185
4196dca6
PM
186static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
187 uint8_t v)
5c53bb81 188{
1813e175 189 *p = v;
5c53bb81 190}
1813e175 191#endif
5c53bb81 192
1813e175 193#if TCG_TARGET_INSN_UNIT_SIZE <= 2
4196dca6 194static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
c896fe29 195{
1813e175
RH
196 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
197 *s->code_ptr++ = v;
198 } else {
199 tcg_insn_unit *p = s->code_ptr;
200 memcpy(p, &v, sizeof(v));
201 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
202 }
c896fe29
FB
203}
204
4196dca6
PM
205static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
206 uint16_t v)
5c53bb81 207{
1813e175
RH
208 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
209 *p = v;
210 } else {
211 memcpy(p, &v, sizeof(v));
212 }
5c53bb81 213}
1813e175 214#endif
5c53bb81 215
1813e175 216#if TCG_TARGET_INSN_UNIT_SIZE <= 4
4196dca6 217static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
c896fe29 218{
1813e175
RH
219 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
220 *s->code_ptr++ = v;
221 } else {
222 tcg_insn_unit *p = s->code_ptr;
223 memcpy(p, &v, sizeof(v));
224 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
225 }
c896fe29
FB
226}
227
4196dca6
PM
228static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
229 uint32_t v)
5c53bb81 230{
1813e175
RH
231 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
232 *p = v;
233 } else {
234 memcpy(p, &v, sizeof(v));
235 }
5c53bb81 236}
1813e175 237#endif
5c53bb81 238
1813e175 239#if TCG_TARGET_INSN_UNIT_SIZE <= 8
4196dca6 240static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
ac26eb69 241{
1813e175
RH
242 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
243 *s->code_ptr++ = v;
244 } else {
245 tcg_insn_unit *p = s->code_ptr;
246 memcpy(p, &v, sizeof(v));
247 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
248 }
ac26eb69
RH
249}
250
4196dca6
PM
251static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
252 uint64_t v)
5c53bb81 253{
1813e175
RH
254 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
255 *p = v;
256 } else {
257 memcpy(p, &v, sizeof(v));
258 }
5c53bb81 259}
1813e175 260#endif
5c53bb81 261
c896fe29
FB
262/* label relocation processing */
263
1813e175 264static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
bec16311 265 TCGLabel *l, intptr_t addend)
c896fe29 266{
7ecd02a0 267 TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
c896fe29 268
7ecd02a0
RH
269 r->type = type;
270 r->ptr = code_ptr;
271 r->addend = addend;
272 QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
c896fe29
FB
273}
274
92ab8e7d 275static void tcg_out_label(TCGContext *s, TCGLabel *l)
c896fe29 276{
eabb7b91 277 tcg_debug_assert(!l->has_value);
c896fe29 278 l->has_value = 1;
92ab8e7d 279 l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
c896fe29
FB
280}
281
42a268c2 282TCGLabel *gen_new_label(void)
c896fe29 283{
b1311c4a 284 TCGContext *s = tcg_ctx;
51e3972c 285 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
c896fe29 286
7ecd02a0
RH
287 memset(l, 0, sizeof(TCGLabel));
288 l->id = s->nb_labels++;
f85b1fc4 289 QSIMPLEQ_INIT(&l->branches);
7ecd02a0
RH
290 QSIMPLEQ_INIT(&l->relocs);
291
bef16ab4 292 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
42a268c2
RH
293
294 return l;
c896fe29
FB
295}
296
7ecd02a0
RH
297static bool tcg_resolve_relocs(TCGContext *s)
298{
299 TCGLabel *l;
300
301 QSIMPLEQ_FOREACH(l, &s->labels, next) {
302 TCGRelocation *r;
303 uintptr_t value = l->u.value;
304
305 QSIMPLEQ_FOREACH(r, &l->relocs, next) {
306 if (!patch_reloc(r->ptr, r->type, value, r->addend)) {
307 return false;
308 }
309 }
310 }
311 return true;
312}
313
9f754620
RH
314static void set_jmp_reset_offset(TCGContext *s, int which)
315{
f14bed3f
RH
316 /*
317 * We will check for overflow at the end of the opcode loop in
318 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
319 */
b7e4afbd 320 s->gen_tb->jmp_reset_offset[which] = tcg_current_code_size(s);
9f754620
RH
321}
322
b52a2c03
RH
323static void G_GNUC_UNUSED set_jmp_insn_offset(TCGContext *s, int which)
324{
325 /*
326 * We will check for overflow at the end of the opcode loop in
327 * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
328 */
9da6079b 329 s->gen_tb->jmp_insn_offset[which] = tcg_current_code_size(s);
b52a2c03
RH
330}
331
becc452a
RH
332static uintptr_t G_GNUC_UNUSED get_jmp_target_addr(TCGContext *s, int which)
333{
334 /*
335 * Return the read-execute version of the pointer, for the benefit
336 * of any pc-relative addressing mode.
337 */
9da6079b 338 return (uintptr_t)tcg_splitwx_to_rx(&s->gen_tb->jmp_target_addr[which]);
becc452a
RH
339}
340
db6b7d0c 341/* Signal overflow, starting over with fewer guest insns. */
8905770b
MAL
342static G_NORETURN
343void tcg_raise_tb_overflow(TCGContext *s)
db6b7d0c
RH
344{
345 siglongjmp(s->jmp_trans, -2);
346}
347
4c22e840
RH
348#define C_PFX1(P, A) P##A
349#define C_PFX2(P, A, B) P##A##_##B
350#define C_PFX3(P, A, B, C) P##A##_##B##_##C
351#define C_PFX4(P, A, B, C, D) P##A##_##B##_##C##_##D
352#define C_PFX5(P, A, B, C, D, E) P##A##_##B##_##C##_##D##_##E
353#define C_PFX6(P, A, B, C, D, E, F) P##A##_##B##_##C##_##D##_##E##_##F
354
355/* Define an enumeration for the various combinations. */
356
357#define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1),
358#define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2),
359#define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3),
360#define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4),
361
362#define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1),
363#define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2),
364#define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3),
365#define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4),
366
367#define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2),
368
369#define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1),
370#define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2),
371#define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3),
372#define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4),
373
374typedef enum {
375#include "tcg-target-con-set.h"
376} TCGConstraintSetIndex;
377
378static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode);
379
380#undef C_O0_I1
381#undef C_O0_I2
382#undef C_O0_I3
383#undef C_O0_I4
384#undef C_O1_I1
385#undef C_O1_I2
386#undef C_O1_I3
387#undef C_O1_I4
388#undef C_N1_I2
389#undef C_O2_I1
390#undef C_O2_I2
391#undef C_O2_I3
392#undef C_O2_I4
393
394/* Put all of the constraint sets into an array, indexed by the enum. */
395
396#define C_O0_I1(I1) { .args_ct_str = { #I1 } },
397#define C_O0_I2(I1, I2) { .args_ct_str = { #I1, #I2 } },
398#define C_O0_I3(I1, I2, I3) { .args_ct_str = { #I1, #I2, #I3 } },
399#define C_O0_I4(I1, I2, I3, I4) { .args_ct_str = { #I1, #I2, #I3, #I4 } },
400
401#define C_O1_I1(O1, I1) { .args_ct_str = { #O1, #I1 } },
402#define C_O1_I2(O1, I1, I2) { .args_ct_str = { #O1, #I1, #I2 } },
403#define C_O1_I3(O1, I1, I2, I3) { .args_ct_str = { #O1, #I1, #I2, #I3 } },
404#define C_O1_I4(O1, I1, I2, I3, I4) { .args_ct_str = { #O1, #I1, #I2, #I3, #I4 } },
405
406#define C_N1_I2(O1, I1, I2) { .args_ct_str = { "&" #O1, #I1, #I2 } },
407
408#define C_O2_I1(O1, O2, I1) { .args_ct_str = { #O1, #O2, #I1 } },
409#define C_O2_I2(O1, O2, I1, I2) { .args_ct_str = { #O1, #O2, #I1, #I2 } },
410#define C_O2_I3(O1, O2, I1, I2, I3) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3 } },
411#define C_O2_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3, #I4 } },
412
413static const TCGTargetOpDef constraint_sets[] = {
414#include "tcg-target-con-set.h"
415};
416
417
418#undef C_O0_I1
419#undef C_O0_I2
420#undef C_O0_I3
421#undef C_O0_I4
422#undef C_O1_I1
423#undef C_O1_I2
424#undef C_O1_I3
425#undef C_O1_I4
426#undef C_N1_I2
427#undef C_O2_I1
428#undef C_O2_I2
429#undef C_O2_I3
430#undef C_O2_I4
431
432/* Expand the enumerator to be returned from tcg_target_op_def(). */
433
434#define C_O0_I1(I1) C_PFX1(c_o0_i1_, I1)
435#define C_O0_I2(I1, I2) C_PFX2(c_o0_i2_, I1, I2)
436#define C_O0_I3(I1, I2, I3) C_PFX3(c_o0_i3_, I1, I2, I3)
437#define C_O0_I4(I1, I2, I3, I4) C_PFX4(c_o0_i4_, I1, I2, I3, I4)
438
439#define C_O1_I1(O1, I1) C_PFX2(c_o1_i1_, O1, I1)
440#define C_O1_I2(O1, I1, I2) C_PFX3(c_o1_i2_, O1, I1, I2)
441#define C_O1_I3(O1, I1, I2, I3) C_PFX4(c_o1_i3_, O1, I1, I2, I3)
442#define C_O1_I4(O1, I1, I2, I3, I4) C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4)
443
444#define C_N1_I2(O1, I1, I2) C_PFX3(c_n1_i2_, O1, I1, I2)
445
446#define C_O2_I1(O1, O2, I1) C_PFX3(c_o2_i1_, O1, O2, I1)
447#define C_O2_I2(O1, O2, I1, I2) C_PFX4(c_o2_i2_, O1, O2, I1, I2)
448#define C_O2_I3(O1, O2, I1, I2, I3) C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3)
449#define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4)
450
139c1837 451#include "tcg-target.c.inc"
c896fe29 452
38b47b19
EC
453static void alloc_tcg_plugin_context(TCGContext *s)
454{
455#ifdef CONFIG_PLUGIN
456 s->plugin_tb = g_new0(struct qemu_plugin_tb, 1);
457 s->plugin_tb->insns =
458 g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn);
459#endif
460}
461
3468b59e
EC
462/*
463 * All TCG threads except the parent (i.e. the one that called tcg_context_init
464 * and registered the target's TCG globals) must register with this function
465 * before initiating translation.
466 *
467 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
468 * of tcg_region_init() for the reasoning behind this.
469 *
470 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
471 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
472 * is not used anymore for translation once this function is called.
473 *
474 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
475 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
476 */
477#ifdef CONFIG_USER_ONLY
478void tcg_register_thread(void)
479{
480 tcg_ctx = &tcg_init_ctx;
481}
482#else
483void tcg_register_thread(void)
484{
485 TCGContext *s = g_malloc(sizeof(*s));
486 unsigned int i, n;
3468b59e
EC
487
488 *s = tcg_init_ctx;
489
490 /* Relink mem_base. */
491 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
492 if (tcg_init_ctx.temps[i].mem_base) {
493 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
494 tcg_debug_assert(b >= 0 && b < n);
495 s->temps[i].mem_base = &s->temps[b];
496 }
497 }
498
499 /* Claim an entry in tcg_ctxs */
0e2d61cf
RH
500 n = qatomic_fetch_inc(&tcg_cur_ctxs);
501 g_assert(n < tcg_max_ctxs);
d73415a3 502 qatomic_set(&tcg_ctxs[n], s);
3468b59e 503
38b47b19
EC
504 if (n > 0) {
505 alloc_tcg_plugin_context(s);
bf042e8e 506 tcg_region_initial_alloc(s);
38b47b19
EC
507 }
508
3468b59e 509 tcg_ctx = s;
e8feb96f 510}
3468b59e 511#endif /* !CONFIG_USER_ONLY */
e8feb96f 512
c896fe29
FB
513/* pool based memory allocation */
514void *tcg_malloc_internal(TCGContext *s, int size)
515{
516 TCGPool *p;
517 int pool_size;
a813e36f 518
c896fe29
FB
519 if (size > TCG_POOL_CHUNK_SIZE) {
520 /* big malloc: insert a new pool (XXX: could optimize) */
7267c094 521 p = g_malloc(sizeof(TCGPool) + size);
c896fe29 522 p->size = size;
4055299e
KB
523 p->next = s->pool_first_large;
524 s->pool_first_large = p;
525 return p->data;
c896fe29
FB
526 } else {
527 p = s->pool_current;
528 if (!p) {
529 p = s->pool_first;
530 if (!p)
531 goto new_pool;
532 } else {
533 if (!p->next) {
534 new_pool:
535 pool_size = TCG_POOL_CHUNK_SIZE;
7267c094 536 p = g_malloc(sizeof(TCGPool) + pool_size);
c896fe29
FB
537 p->size = pool_size;
538 p->next = NULL;
a813e36f 539 if (s->pool_current) {
c896fe29 540 s->pool_current->next = p;
a813e36f 541 } else {
c896fe29 542 s->pool_first = p;
a813e36f 543 }
c896fe29
FB
544 } else {
545 p = p->next;
546 }
547 }
548 }
549 s->pool_current = p;
550 s->pool_cur = p->data + size;
551 s->pool_end = p->data + p->size;
552 return p->data;
553}
554
555void tcg_pool_reset(TCGContext *s)
556{
4055299e
KB
557 TCGPool *p, *t;
558 for (p = s->pool_first_large; p; p = t) {
559 t = p->next;
560 g_free(p);
561 }
562 s->pool_first_large = NULL;
c896fe29
FB
563 s->pool_cur = s->pool_end = NULL;
564 s->pool_current = NULL;
565}
566
2ef6175a
RH
567#include "exec/helper-proto.h"
568
39004a71 569static TCGHelperInfo all_helpers[] = {
2ef6175a 570#include "exec/helper-tcg.h"
100b5e01 571};
619205fd 572static GHashTable *helper_table;
100b5e01 573
22f15579 574#ifdef CONFIG_TCG_INTERPRETER
c6ef8c7b
PMD
575static ffi_type *typecode_to_ffi(int argmask)
576{
e9709e17
RH
577 /*
578 * libffi does not support __int128_t, so we have forced Int128
579 * to use the structure definition instead of the builtin type.
580 */
581 static ffi_type *ffi_type_i128_elements[3] = {
582 &ffi_type_uint64,
583 &ffi_type_uint64,
584 NULL
585 };
586 static ffi_type ffi_type_i128 = {
587 .size = 16,
588 .alignment = __alignof__(Int128),
589 .type = FFI_TYPE_STRUCT,
590 .elements = ffi_type_i128_elements,
591 };
592
c6ef8c7b
PMD
593 switch (argmask) {
594 case dh_typecode_void:
595 return &ffi_type_void;
596 case dh_typecode_i32:
597 return &ffi_type_uint32;
598 case dh_typecode_s32:
599 return &ffi_type_sint32;
600 case dh_typecode_i64:
601 return &ffi_type_uint64;
602 case dh_typecode_s64:
603 return &ffi_type_sint64;
604 case dh_typecode_ptr:
605 return &ffi_type_pointer;
e9709e17
RH
606 case dh_typecode_i128:
607 return &ffi_type_i128;
c6ef8c7b
PMD
608 }
609 g_assert_not_reached();
610}
0c22e176
PMD
611
612static void init_ffi_layouts(void)
613{
614 /* g_direct_hash/equal for direct comparisons on uint32_t. */
f9c4bb80
RH
615 GHashTable *ffi_table = g_hash_table_new(NULL, NULL);
616
0c22e176 617 for (int i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
f9c4bb80
RH
618 TCGHelperInfo *info = &all_helpers[i];
619 unsigned typemask = info->typemask;
0c22e176
PMD
620 gpointer hash = (gpointer)(uintptr_t)typemask;
621 struct {
622 ffi_cif cif;
623 ffi_type *args[];
624 } *ca;
625 ffi_status status;
626 int nargs;
f9c4bb80 627 ffi_cif *cif;
0c22e176 628
f9c4bb80
RH
629 cif = g_hash_table_lookup(ffi_table, hash);
630 if (cif) {
631 info->cif = cif;
0c22e176
PMD
632 continue;
633 }
634
635 /* Ignoring the return type, find the last non-zero field. */
636 nargs = 32 - clz32(typemask >> 3);
637 nargs = DIV_ROUND_UP(nargs, 3);
e9709e17 638 assert(nargs <= MAX_CALL_IARGS);
0c22e176
PMD
639
640 ca = g_malloc0(sizeof(*ca) + nargs * sizeof(ffi_type *));
641 ca->cif.rtype = typecode_to_ffi(typemask & 7);
642 ca->cif.nargs = nargs;
643
644 if (nargs != 0) {
645 ca->cif.arg_types = ca->args;
646 for (int j = 0; j < nargs; ++j) {
647 int typecode = extract32(typemask, (j + 1) * 3, 3);
648 ca->args[j] = typecode_to_ffi(typecode);
649 }
650 }
651
652 status = ffi_prep_cif(&ca->cif, FFI_DEFAULT_ABI, nargs,
653 ca->cif.rtype, ca->cif.arg_types);
654 assert(status == FFI_OK);
655
f9c4bb80
RH
656 cif = &ca->cif;
657 info->cif = cif;
658 g_hash_table_insert(ffi_table, hash, (gpointer)cif);
0c22e176 659 }
f9c4bb80
RH
660
661 g_hash_table_destroy(ffi_table);
0c22e176
PMD
662}
663#endif /* CONFIG_TCG_INTERPRETER */
22f15579 664
39004a71
RH
665typedef struct TCGCumulativeArgs {
666 int arg_idx; /* tcg_gen_callN args[] */
667 int info_in_idx; /* TCGHelperInfo in[] */
668 int arg_slot; /* regs+stack slot */
669 int ref_slot; /* stack slots for references */
670} TCGCumulativeArgs;
671
672static void layout_arg_even(TCGCumulativeArgs *cum)
673{
674 cum->arg_slot += cum->arg_slot & 1;
675}
676
677static void layout_arg_1(TCGCumulativeArgs *cum, TCGHelperInfo *info,
678 TCGCallArgumentKind kind)
679{
680 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
681
682 *loc = (TCGCallArgumentLoc){
683 .kind = kind,
684 .arg_idx = cum->arg_idx,
685 .arg_slot = cum->arg_slot,
686 };
687 cum->info_in_idx++;
688 cum->arg_slot++;
689}
690
691static void layout_arg_normal_n(TCGCumulativeArgs *cum,
692 TCGHelperInfo *info, int n)
693{
694 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
695
696 for (int i = 0; i < n; ++i) {
697 /* Layout all using the same arg_idx, adjusting the subindex. */
698 loc[i] = (TCGCallArgumentLoc){
699 .kind = TCG_CALL_ARG_NORMAL,
700 .arg_idx = cum->arg_idx,
701 .tmp_subindex = i,
702 .arg_slot = cum->arg_slot + i,
703 };
704 }
705 cum->info_in_idx += n;
706 cum->arg_slot += n;
707}
708
313bdea8
RH
709static void layout_arg_by_ref(TCGCumulativeArgs *cum, TCGHelperInfo *info)
710{
711 TCGCallArgumentLoc *loc = &info->in[cum->info_in_idx];
712 int n = 128 / TCG_TARGET_REG_BITS;
713
714 /* The first subindex carries the pointer. */
715 layout_arg_1(cum, info, TCG_CALL_ARG_BY_REF);
716
717 /*
718 * The callee is allowed to clobber memory associated with
719 * structure pass by-reference. Therefore we must make copies.
720 * Allocate space from "ref_slot", which will be adjusted to
721 * follow the parameters on the stack.
722 */
723 loc[0].ref_slot = cum->ref_slot;
724
725 /*
726 * Subsequent words also go into the reference slot, but
727 * do not accumulate into the regular arguments.
728 */
729 for (int i = 1; i < n; ++i) {
730 loc[i] = (TCGCallArgumentLoc){
731 .kind = TCG_CALL_ARG_BY_REF_N,
732 .arg_idx = cum->arg_idx,
733 .tmp_subindex = i,
734 .ref_slot = cum->ref_slot + i,
735 };
736 }
737 cum->info_in_idx += n;
738 cum->ref_slot += n;
739}
740
39004a71
RH
741static void init_call_layout(TCGHelperInfo *info)
742{
743 int max_reg_slots = ARRAY_SIZE(tcg_target_call_iarg_regs);
744 int max_stk_slots = TCG_STATIC_CALL_ARGS_SIZE / sizeof(tcg_target_long);
745 unsigned typemask = info->typemask;
746 unsigned typecode;
747 TCGCumulativeArgs cum = { };
748
749 /*
750 * Parse and place any function return value.
751 */
752 typecode = typemask & 7;
753 switch (typecode) {
754 case dh_typecode_void:
755 info->nr_out = 0;
756 break;
757 case dh_typecode_i32:
758 case dh_typecode_s32:
759 case dh_typecode_ptr:
760 info->nr_out = 1;
761 info->out_kind = TCG_CALL_RET_NORMAL;
762 break;
763 case dh_typecode_i64:
764 case dh_typecode_s64:
765 info->nr_out = 64 / TCG_TARGET_REG_BITS;
766 info->out_kind = TCG_CALL_RET_NORMAL;
5e3d0c19
RH
767 /* Query the last register now to trigger any assert early. */
768 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
466d3759
RH
769 break;
770 case dh_typecode_i128:
771 info->nr_out = 128 / TCG_TARGET_REG_BITS;
5427a9a7
RH
772 info->out_kind = TCG_TARGET_CALL_RET_I128;
773 switch (TCG_TARGET_CALL_RET_I128) {
466d3759 774 case TCG_CALL_RET_NORMAL:
5e3d0c19
RH
775 /* Query the last register now to trigger any assert early. */
776 tcg_target_call_oarg_reg(info->out_kind, info->nr_out - 1);
466d3759 777 break;
c6556aa0
RH
778 case TCG_CALL_RET_BY_VEC:
779 /* Query the single register now to trigger any assert early. */
780 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0);
781 break;
313bdea8
RH
782 case TCG_CALL_RET_BY_REF:
783 /*
784 * Allocate the first argument to the output.
785 * We don't need to store this anywhere, just make it
786 * unavailable for use in the input loop below.
787 */
788 cum.arg_slot = 1;
789 break;
466d3759
RH
790 default:
791 qemu_build_not_reached();
792 }
39004a71
RH
793 break;
794 default:
795 g_assert_not_reached();
796 }
39004a71
RH
797
798 /*
799 * Parse and place function arguments.
800 */
801 for (typemask >>= 3; typemask; typemask >>= 3, cum.arg_idx++) {
802 TCGCallArgumentKind kind;
803 TCGType type;
804
805 typecode = typemask & 7;
806 switch (typecode) {
807 case dh_typecode_i32:
808 case dh_typecode_s32:
809 type = TCG_TYPE_I32;
810 break;
811 case dh_typecode_i64:
812 case dh_typecode_s64:
813 type = TCG_TYPE_I64;
814 break;
815 case dh_typecode_ptr:
816 type = TCG_TYPE_PTR;
817 break;
466d3759
RH
818 case dh_typecode_i128:
819 type = TCG_TYPE_I128;
820 break;
39004a71
RH
821 default:
822 g_assert_not_reached();
823 }
824
825 switch (type) {
826 case TCG_TYPE_I32:
827 switch (TCG_TARGET_CALL_ARG_I32) {
828 case TCG_CALL_ARG_EVEN:
829 layout_arg_even(&cum);
830 /* fall through */
831 case TCG_CALL_ARG_NORMAL:
832 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
833 break;
834 case TCG_CALL_ARG_EXTEND:
835 kind = TCG_CALL_ARG_EXTEND_U + (typecode & 1);
836 layout_arg_1(&cum, info, kind);
837 break;
838 default:
839 qemu_build_not_reached();
840 }
841 break;
842
843 case TCG_TYPE_I64:
844 switch (TCG_TARGET_CALL_ARG_I64) {
845 case TCG_CALL_ARG_EVEN:
846 layout_arg_even(&cum);
847 /* fall through */
848 case TCG_CALL_ARG_NORMAL:
849 if (TCG_TARGET_REG_BITS == 32) {
850 layout_arg_normal_n(&cum, info, 2);
851 } else {
852 layout_arg_1(&cum, info, TCG_CALL_ARG_NORMAL);
853 }
854 break;
855 default:
856 qemu_build_not_reached();
857 }
858 break;
859
466d3759 860 case TCG_TYPE_I128:
5427a9a7 861 switch (TCG_TARGET_CALL_ARG_I128) {
466d3759
RH
862 case TCG_CALL_ARG_EVEN:
863 layout_arg_even(&cum);
864 /* fall through */
865 case TCG_CALL_ARG_NORMAL:
866 layout_arg_normal_n(&cum, info, 128 / TCG_TARGET_REG_BITS);
867 break;
313bdea8
RH
868 case TCG_CALL_ARG_BY_REF:
869 layout_arg_by_ref(&cum, info);
870 break;
466d3759
RH
871 default:
872 qemu_build_not_reached();
873 }
874 break;
875
39004a71
RH
876 default:
877 g_assert_not_reached();
878 }
879 }
880 info->nr_in = cum.info_in_idx;
881
882 /* Validate that we didn't overrun the input array. */
883 assert(cum.info_in_idx <= ARRAY_SIZE(info->in));
884 /* Validate the backend has enough argument space. */
885 assert(cum.arg_slot <= max_reg_slots + max_stk_slots);
313bdea8
RH
886
887 /*
888 * Relocate the "ref_slot" area to the end of the parameters.
889 * Minimizing this stack offset helps code size for x86,
890 * which has a signed 8-bit offset encoding.
891 */
892 if (cum.ref_slot != 0) {
893 int ref_base = 0;
894
895 if (cum.arg_slot > max_reg_slots) {
896 int align = __alignof(Int128) / sizeof(tcg_target_long);
897
898 ref_base = cum.arg_slot - max_reg_slots;
899 if (align > 1) {
900 ref_base = ROUND_UP(ref_base, align);
901 }
902 }
903 assert(ref_base + cum.ref_slot <= max_stk_slots);
904
905 if (ref_base != 0) {
906 for (int i = cum.info_in_idx - 1; i >= 0; --i) {
907 TCGCallArgumentLoc *loc = &info->in[i];
908 switch (loc->kind) {
909 case TCG_CALL_ARG_BY_REF:
910 case TCG_CALL_ARG_BY_REF_N:
911 loc->ref_slot += ref_base;
912 break;
913 default:
914 break;
915 }
916 }
917 }
918 }
39004a71
RH
919}
920
91478cef 921static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
f69d277e 922static void process_op_defs(TCGContext *s);
1c2adb95
RH
923static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
924 TCGReg reg, const char *name);
91478cef 925
43b972b7 926static void tcg_context_init(unsigned max_cpus)
c896fe29 927{
a76aabd3 928 TCGContext *s = &tcg_init_ctx;
100b5e01 929 int op, total_args, n, i;
c896fe29
FB
930 TCGOpDef *def;
931 TCGArgConstraint *args_ct;
1c2adb95 932 TCGTemp *ts;
c896fe29
FB
933
934 memset(s, 0, sizeof(*s));
c896fe29 935 s->nb_globals = 0;
c70fbf0a 936
c896fe29
FB
937 /* Count total number of arguments and allocate the corresponding
938 space */
939 total_args = 0;
940 for(op = 0; op < NB_OPS; op++) {
941 def = &tcg_op_defs[op];
942 n = def->nb_iargs + def->nb_oargs;
943 total_args += n;
944 }
945
bc2b17e6 946 args_ct = g_new0(TCGArgConstraint, total_args);
c896fe29
FB
947
948 for(op = 0; op < NB_OPS; op++) {
949 def = &tcg_op_defs[op];
950 def->args_ct = args_ct;
c896fe29 951 n = def->nb_iargs + def->nb_oargs;
c896fe29
FB
952 args_ct += n;
953 }
5cd8f621
RH
954
955 /* Register helpers. */
84fd9dd3 956 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
619205fd 957 helper_table = g_hash_table_new(NULL, NULL);
84fd9dd3 958
100b5e01 959 for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
39004a71 960 init_call_layout(&all_helpers[i]);
84fd9dd3 961 g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
72866e82 962 (gpointer)&all_helpers[i]);
100b5e01 963 }
5cd8f621 964
22f15579 965#ifdef CONFIG_TCG_INTERPRETER
0c22e176 966 init_ffi_layouts();
22f15579
RH
967#endif
968
c896fe29 969 tcg_target_init(s);
f69d277e 970 process_op_defs(s);
91478cef
RH
971
972 /* Reverse the order of the saved registers, assuming they're all at
973 the start of tcg_target_reg_alloc_order. */
974 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
975 int r = tcg_target_reg_alloc_order[n];
976 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
977 break;
978 }
979 }
980 for (i = 0; i < n; ++i) {
981 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
982 }
983 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
984 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
985 }
b1311c4a 986
38b47b19
EC
987 alloc_tcg_plugin_context(s);
988
b1311c4a 989 tcg_ctx = s;
3468b59e
EC
990 /*
991 * In user-mode we simply share the init context among threads, since we
992 * use a single region. See the documentation tcg_region_init() for the
993 * reasoning behind this.
994 * In softmmu we will have at most max_cpus TCG threads.
995 */
996#ifdef CONFIG_USER_ONLY
df2cce29 997 tcg_ctxs = &tcg_ctx;
0e2d61cf
RH
998 tcg_cur_ctxs = 1;
999 tcg_max_ctxs = 1;
3468b59e 1000#else
0e2d61cf
RH
1001 tcg_max_ctxs = max_cpus;
1002 tcg_ctxs = g_new0(TCGContext *, max_cpus);
3468b59e 1003#endif
1c2adb95
RH
1004
1005 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
1006 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
1007 cpu_env = temp_tcgv_ptr(ts);
9002ec79 1008}
b03cce8e 1009
43b972b7 1010void tcg_init(size_t tb_size, int splitwx, unsigned max_cpus)
a76aabd3 1011{
43b972b7
RH
1012 tcg_context_init(max_cpus);
1013 tcg_region_init(tb_size, splitwx, max_cpus);
a76aabd3
RH
1014}
1015
6e3b2bfd
EC
1016/*
1017 * Allocate TBs right before their corresponding translated code, making
1018 * sure that TBs and code are on different cache lines.
1019 */
1020TranslationBlock *tcg_tb_alloc(TCGContext *s)
1021{
1022 uintptr_t align = qemu_icache_linesize;
1023 TranslationBlock *tb;
1024 void *next;
1025
e8feb96f 1026 retry:
6e3b2bfd
EC
1027 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
1028 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
1029
1030 if (unlikely(next > s->code_gen_highwater)) {
e8feb96f
EC
1031 if (tcg_region_alloc(s)) {
1032 return NULL;
1033 }
1034 goto retry;
6e3b2bfd 1035 }
d73415a3 1036 qatomic_set(&s->code_gen_ptr, next);
57a26946 1037 s->data_gen_ptr = NULL;
6e3b2bfd
EC
1038 return tb;
1039}
1040
9002ec79
RH
1041void tcg_prologue_init(TCGContext *s)
1042{
b0a0794a 1043 size_t prologue_size;
8163b749 1044
b0a0794a
RH
1045 s->code_ptr = s->code_gen_ptr;
1046 s->code_buf = s->code_gen_ptr;
5b38ee31 1047 s->data_gen_ptr = NULL;
b91ccb31
RH
1048
1049#ifndef CONFIG_TCG_INTERPRETER
b0a0794a 1050 tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(s->code_ptr);
b91ccb31 1051#endif
8163b749 1052
5b38ee31
RH
1053#ifdef TCG_TARGET_NEED_POOL_LABELS
1054 s->pool_labels = NULL;
1055#endif
1056
653b87eb 1057 qemu_thread_jit_write();
8163b749 1058 /* Generate the prologue. */
b03cce8e 1059 tcg_target_qemu_prologue(s);
5b38ee31
RH
1060
1061#ifdef TCG_TARGET_NEED_POOL_LABELS
1062 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1063 {
1768987b
RH
1064 int result = tcg_out_pool_finalize(s);
1065 tcg_debug_assert(result == 0);
5b38ee31
RH
1066 }
1067#endif
1068
b0a0794a 1069 prologue_size = tcg_current_code_size(s);
5584e2db 1070 perf_report_prologue(s->code_gen_ptr, prologue_size);
b0a0794a 1071
df5d2b16 1072#ifndef CONFIG_TCG_INTERPRETER
b0a0794a
RH
1073 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
1074 (uintptr_t)s->code_buf, prologue_size);
df5d2b16 1075#endif
8163b749 1076
d6b64b2b
RH
1077#ifdef DEBUG_DISAS
1078 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
c60f599b 1079 FILE *logfile = qemu_log_trylock();
78b54858
RH
1080 if (logfile) {
1081 fprintf(logfile, "PROLOGUE: [size=%zu]\n", prologue_size);
1082 if (s->data_gen_ptr) {
1083 size_t code_size = s->data_gen_ptr - s->code_gen_ptr;
1084 size_t data_size = prologue_size - code_size;
1085 size_t i;
1086
1087 disas(logfile, s->code_gen_ptr, code_size);
1088
1089 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1090 if (sizeof(tcg_target_ulong) == 8) {
1091 fprintf(logfile,
1092 "0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1093 (uintptr_t)s->data_gen_ptr + i,
1094 *(uint64_t *)(s->data_gen_ptr + i));
1095 } else {
1096 fprintf(logfile,
1097 "0x%08" PRIxPTR ": .long 0x%08x\n",
1098 (uintptr_t)s->data_gen_ptr + i,
1099 *(uint32_t *)(s->data_gen_ptr + i));
1100 }
5b38ee31 1101 }
78b54858
RH
1102 } else {
1103 disas(logfile, s->code_gen_ptr, prologue_size);
5b38ee31 1104 }
78b54858 1105 fprintf(logfile, "\n");
78b54858 1106 qemu_log_unlock(logfile);
5b38ee31 1107 }
d6b64b2b
RH
1108 }
1109#endif
cedbcb01 1110
6eea0434
RH
1111#ifndef CONFIG_TCG_INTERPRETER
1112 /*
1113 * Assert that goto_ptr is implemented completely, setting an epilogue.
1114 * For tci, we use NULL as the signal to return from the interpreter,
1115 * so skip this check.
1116 */
f4e01e30 1117 tcg_debug_assert(tcg_code_gen_epilogue != NULL);
6eea0434 1118#endif
d1c74ab3
RH
1119
1120 tcg_region_prologue_set(s);
c896fe29
FB
1121}
1122
c896fe29
FB
1123void tcg_func_start(TCGContext *s)
1124{
1125 tcg_pool_reset(s);
1126 s->nb_temps = s->nb_globals;
0ec9eabc
RH
1127
1128 /* No temps have been previously allocated for size or locality. */
1129 memset(s->free_temps, 0, sizeof(s->free_temps));
1130
c0522136
RH
1131 /* No constant temps have been previously allocated. */
1132 for (int i = 0; i < TCG_TYPE_COUNT; ++i) {
1133 if (s->const_table[i]) {
1134 g_hash_table_remove_all(s->const_table[i]);
1135 }
1136 }
1137
abebf925 1138 s->nb_ops = 0;
c896fe29
FB
1139 s->nb_labels = 0;
1140 s->current_frame_offset = s->frame_start;
1141
0a209d4b
RH
1142#ifdef CONFIG_DEBUG_TCG
1143 s->goto_tb_issue_mask = 0;
1144#endif
1145
15fa08f8
RH
1146 QTAILQ_INIT(&s->ops);
1147 QTAILQ_INIT(&s->free_ops);
bef16ab4 1148 QSIMPLEQ_INIT(&s->labels);
c896fe29
FB
1149}
1150
ae30e866 1151static TCGTemp *tcg_temp_alloc(TCGContext *s)
7ca4b752
RH
1152{
1153 int n = s->nb_temps++;
ae30e866
RH
1154
1155 if (n >= TCG_MAX_TEMPS) {
db6b7d0c 1156 tcg_raise_tb_overflow(s);
ae30e866 1157 }
7ca4b752
RH
1158 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1159}
1160
ae30e866 1161static TCGTemp *tcg_global_alloc(TCGContext *s)
7ca4b752 1162{
fa477d25
RH
1163 TCGTemp *ts;
1164
7ca4b752 1165 tcg_debug_assert(s->nb_globals == s->nb_temps);
ae30e866 1166 tcg_debug_assert(s->nb_globals < TCG_MAX_TEMPS);
7ca4b752 1167 s->nb_globals++;
fa477d25 1168 ts = tcg_temp_alloc(s);
ee17db83 1169 ts->kind = TEMP_GLOBAL;
fa477d25
RH
1170
1171 return ts;
c896fe29
FB
1172}
1173
085272b3
RH
1174static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1175 TCGReg reg, const char *name)
c896fe29 1176{
c896fe29 1177 TCGTemp *ts;
c896fe29 1178
1a057554 1179 tcg_debug_assert(TCG_TARGET_REG_BITS == 64 || type == TCG_TYPE_I32);
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:
732e89f4 3685 g_assert_not_reached();
59d7c14e
RH
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
732e89f4 3772 g_assert_not_reached();
c896fe29
FB
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 }
732e89f4 3818 g_assert_not_reached();
29f5e925
RH
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:
732e89f4 3865 g_assert_not_reached();
40ae5c62 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 */
678155b2
RH
4501 switch (op->opc) {
4502 case INDEX_op_ext8s_i32:
4503 tcg_out_ext8s(s, TCG_TYPE_I32, new_args[0], new_args[1]);
4504 break;
4505 case INDEX_op_ext8s_i64:
4506 tcg_out_ext8s(s, TCG_TYPE_I64, new_args[0], new_args[1]);
4507 break;
d0e66c89
RH
4508 case INDEX_op_ext8u_i32:
4509 case INDEX_op_ext8u_i64:
4510 tcg_out_ext8u(s, new_args[0], new_args[1]);
4511 break;
678155b2
RH
4512 default:
4513 if (def->flags & TCG_OPF_VECTOR) {
4514 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
4515 new_args, const_args);
4516 } else {
4517 tcg_out_op(s, op->opc, new_args, const_args);
4518 }
4519 break;
d2fd745f
RH
4520 }
4521
c896fe29
FB
4522 /* move the outputs in the correct register if needed */
4523 for(i = 0; i < nb_oargs; i++) {
43439139 4524 ts = arg_temp(op->args[i]);
d63e3b6e
RH
4525
4526 /* ENV should not be modified. */
e01fa97d 4527 tcg_debug_assert(!temp_readonly(ts));
d63e3b6e 4528
ec7a869d 4529 if (NEED_SYNC_ARG(i)) {
98b4e186 4530 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
59d7c14e 4531 } else if (IS_DEAD_ARG(i)) {
f8bf00f1 4532 temp_dead(s, ts);
ec7a869d 4533 }
c896fe29
FB
4534 }
4535}
4536
efe86b21
RH
4537static bool tcg_reg_alloc_dup2(TCGContext *s, const TCGOp *op)
4538{
4539 const TCGLifeData arg_life = op->life;
4540 TCGTemp *ots, *itsl, *itsh;
4541 TCGType vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
4542
4543 /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */
4544 tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
4545 tcg_debug_assert(TCGOP_VECE(op) == MO_64);
4546
4547 ots = arg_temp(op->args[0]);
4548 itsl = arg_temp(op->args[1]);
4549 itsh = arg_temp(op->args[2]);
4550
4551 /* ENV should not be modified. */
4552 tcg_debug_assert(!temp_readonly(ots));
4553
4554 /* Allocate the output register now. */
4555 if (ots->val_type != TEMP_VAL_REG) {
4556 TCGRegSet allocated_regs = s->reserved_regs;
4557 TCGRegSet dup_out_regs =
4558 tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
098859f1 4559 TCGReg oreg;
efe86b21
RH
4560
4561 /* Make sure to not spill the input registers. */
4562 if (!IS_DEAD_ARG(1) && itsl->val_type == TEMP_VAL_REG) {
4563 tcg_regset_set_reg(allocated_regs, itsl->reg);
4564 }
4565 if (!IS_DEAD_ARG(2) && itsh->val_type == TEMP_VAL_REG) {
4566 tcg_regset_set_reg(allocated_regs, itsh->reg);
4567 }
4568
098859f1 4569 oreg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
31fd884b 4570 output_pref(op, 0), ots->indirect_base);
098859f1 4571 set_temp_val_reg(s, ots, oreg);
efe86b21
RH
4572 }
4573
4574 /* Promote dup2 of immediates to dupi_vec. */
4575 if (itsl->val_type == TEMP_VAL_CONST && itsh->val_type == TEMP_VAL_CONST) {
4576 uint64_t val = deposit64(itsl->val, 32, 32, itsh->val);
4577 MemOp vece = MO_64;
4578
4579 if (val == dup_const(MO_8, val)) {
4580 vece = MO_8;
4581 } else if (val == dup_const(MO_16, val)) {
4582 vece = MO_16;
4583 } else if (val == dup_const(MO_32, val)) {
4584 vece = MO_32;
4585 }
4586
4587 tcg_out_dupi_vec(s, vtype, vece, ots->reg, val);
4588 goto done;
4589 }
4590
4591 /* If the two inputs form one 64-bit value, try dupm_vec. */
aef85402
RH
4592 if (itsl->temp_subindex == HOST_BIG_ENDIAN &&
4593 itsh->temp_subindex == !HOST_BIG_ENDIAN &&
4594 itsl == itsh + (HOST_BIG_ENDIAN ? 1 : -1)) {
4595 TCGTemp *its = itsl - HOST_BIG_ENDIAN;
4596
4597 temp_sync(s, its + 0, s->reserved_regs, 0, 0);
4598 temp_sync(s, its + 1, s->reserved_regs, 0, 0);
4599
efe86b21
RH
4600 if (tcg_out_dupm_vec(s, vtype, MO_64, ots->reg,
4601 its->mem_base->reg, its->mem_offset)) {
4602 goto done;
4603 }
4604 }
4605
4606 /* Fall back to generic expansion. */
4607 return false;
4608
4609 done:
36f5539c 4610 ots->mem_coherent = 0;
efe86b21
RH
4611 if (IS_DEAD_ARG(1)) {
4612 temp_dead(s, itsl);
4613 }
4614 if (IS_DEAD_ARG(2)) {
4615 temp_dead(s, itsh);
4616 }
4617 if (NEED_SYNC_ARG(0)) {
4618 temp_sync(s, ots, s->reserved_regs, 0, IS_DEAD_ARG(0));
4619 } else if (IS_DEAD_ARG(0)) {
4620 temp_dead(s, ots);
4621 }
4622 return true;
4623}
4624
39004a71
RH
4625static void load_arg_reg(TCGContext *s, TCGReg reg, TCGTemp *ts,
4626 TCGRegSet allocated_regs)
c896fe29 4627{
39004a71
RH
4628 if (ts->val_type == TEMP_VAL_REG) {
4629 if (ts->reg != reg) {
4630 tcg_reg_free(s, reg, allocated_regs);
4631 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
4632 /*
4633 * Cross register class move not supported. Sync the
4634 * temp back to its slot and load from there.
4635 */
4636 temp_sync(s, ts, allocated_regs, 0, 0);
4637 tcg_out_ld(s, ts->type, reg,
4638 ts->mem_base->reg, ts->mem_offset);
4639 }
4640 }
4641 } else {
4642 TCGRegSet arg_set = 0;
c896fe29 4643
39004a71
RH
4644 tcg_reg_free(s, reg, allocated_regs);
4645 tcg_regset_set_reg(arg_set, reg);
4646 temp_load(s, ts, arg_set, allocated_regs, 0);
b03cce8e 4647 }
39004a71 4648}
39cf05d3 4649
39004a71
RH
4650static void load_arg_stk(TCGContext *s, int stk_slot, TCGTemp *ts,
4651 TCGRegSet allocated_regs)
4652{
4653 /*
4654 * When the destination is on the stack, load up the temp and store.
4655 * If there are many call-saved registers, the temp might live to
4656 * see another use; otherwise it'll be discarded.
4657 */
4658 temp_load(s, ts, tcg_target_available_regs[ts->type], allocated_regs, 0);
4659 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK,
4660 TCG_TARGET_CALL_STACK_OFFSET +
4661 stk_slot * sizeof(tcg_target_long));
4662}
a813e36f 4663
39004a71
RH
4664static void load_arg_normal(TCGContext *s, const TCGCallArgumentLoc *l,
4665 TCGTemp *ts, TCGRegSet *allocated_regs)
4666{
4667 if (REG_P(l)) {
4668 TCGReg reg = tcg_target_call_iarg_regs[l->arg_slot];
4669 load_arg_reg(s, reg, ts, *allocated_regs);
4670 tcg_regset_set_reg(*allocated_regs, reg);
4671 } else {
4672 load_arg_stk(s, l->arg_slot - ARRAY_SIZE(tcg_target_call_iarg_regs),
4673 ts, *allocated_regs);
4674 }
4675}
40ae5c62 4676
313bdea8
RH
4677static void load_arg_ref(TCGContext *s, int arg_slot, TCGReg ref_base,
4678 intptr_t ref_off, TCGRegSet *allocated_regs)
4679{
4680 TCGReg reg;
4681 int stk_slot = arg_slot - ARRAY_SIZE(tcg_target_call_iarg_regs);
4682
4683 if (stk_slot < 0) {
4684 reg = tcg_target_call_iarg_regs[arg_slot];
4685 tcg_reg_free(s, reg, *allocated_regs);
4686 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
4687 tcg_regset_set_reg(*allocated_regs, reg);
4688 } else {
4689 reg = tcg_reg_alloc(s, tcg_target_available_regs[TCG_TYPE_PTR],
4690 *allocated_regs, 0, false);
4691 tcg_out_addi_ptr(s, reg, ref_base, ref_off);
4692 tcg_out_st(s, TCG_TYPE_PTR, reg, TCG_REG_CALL_STACK,
4693 TCG_TARGET_CALL_STACK_OFFSET
4694 + stk_slot * sizeof(tcg_target_long));
4695 }
4696}
4697
39004a71
RH
4698static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
4699{
4700 const int nb_oargs = TCGOP_CALLO(op);
4701 const int nb_iargs = TCGOP_CALLI(op);
4702 const TCGLifeData arg_life = op->life;
4703 const TCGHelperInfo *info = tcg_call_info(op);
4704 TCGRegSet allocated_regs = s->reserved_regs;
4705 int i;
40ae5c62 4706
39004a71
RH
4707 /*
4708 * Move inputs into place in reverse order,
4709 * so that we place stacked arguments first.
4710 */
4711 for (i = nb_iargs - 1; i >= 0; --i) {
4712 const TCGCallArgumentLoc *loc = &info->in[i];
4713 TCGTemp *ts = arg_temp(op->args[nb_oargs + i]);
40ae5c62 4714
39004a71
RH
4715 switch (loc->kind) {
4716 case TCG_CALL_ARG_NORMAL:
4717 case TCG_CALL_ARG_EXTEND_U:
4718 case TCG_CALL_ARG_EXTEND_S:
4719 load_arg_normal(s, loc, ts, &allocated_regs);
4720 break;
313bdea8
RH
4721 case TCG_CALL_ARG_BY_REF:
4722 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
4723 load_arg_ref(s, loc->arg_slot, TCG_REG_CALL_STACK,
4724 TCG_TARGET_CALL_STACK_OFFSET
4725 + loc->ref_slot * sizeof(tcg_target_long),
4726 &allocated_regs);
4727 break;
4728 case TCG_CALL_ARG_BY_REF_N:
4729 load_arg_stk(s, loc->ref_slot, ts, allocated_regs);
4730 break;
39004a71
RH
4731 default:
4732 g_assert_not_reached();
c896fe29 4733 }
c896fe29 4734 }
a813e36f 4735
39004a71 4736 /* Mark dead temporaries and free the associated registers. */
dd186292 4737 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
866cb6cb 4738 if (IS_DEAD_ARG(i)) {
43439139 4739 temp_dead(s, arg_temp(op->args[i]));
c896fe29
FB
4740 }
4741 }
a813e36f 4742
39004a71 4743 /* Clobber call registers. */
c8074023
RH
4744 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
4745 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
b3915dbb 4746 tcg_reg_free(s, i, allocated_regs);
c896fe29
FB
4747 }
4748 }
78505279 4749
39004a71
RH
4750 /*
4751 * Save globals if they might be written by the helper,
4752 * sync them if they might be read.
4753 */
4754 if (info->flags & TCG_CALL_NO_READ_GLOBALS) {
78505279 4755 /* Nothing to do */
39004a71 4756 } else if (info->flags & TCG_CALL_NO_WRITE_GLOBALS) {
78505279
AJ
4757 sync_globals(s, allocated_regs);
4758 } else {
b9c18f56
AJ
4759 save_globals(s, allocated_regs);
4760 }
c896fe29 4761
313bdea8
RH
4762 /*
4763 * If the ABI passes a pointer to the returned struct as the first
4764 * argument, load that now. Pass a pointer to the output home slot.
4765 */
4766 if (info->out_kind == TCG_CALL_RET_BY_REF) {
4767 TCGTemp *ts = arg_temp(op->args[0]);
4768
4769 if (!ts->mem_allocated) {
4770 temp_allocate_frame(s, ts);
4771 }
4772 load_arg_ref(s, 0, ts->mem_base->reg, ts->mem_offset, &allocated_regs);
4773 }
4774
cee44b03 4775 tcg_out_call(s, tcg_call_func(op), info);
c896fe29 4776
39004a71
RH
4777 /* Assign output registers and emit moves if needed. */
4778 switch (info->out_kind) {
4779 case TCG_CALL_RET_NORMAL:
4780 for (i = 0; i < nb_oargs; i++) {
4781 TCGTemp *ts = arg_temp(op->args[i]);
5e3d0c19 4782 TCGReg reg = tcg_target_call_oarg_reg(TCG_CALL_RET_NORMAL, i);
d63e3b6e 4783
39004a71
RH
4784 /* ENV should not be modified. */
4785 tcg_debug_assert(!temp_readonly(ts));
d63e3b6e 4786
39004a71
RH
4787 set_temp_val_reg(s, ts, reg);
4788 ts->mem_coherent = 0;
4789 }
4790 break;
313bdea8 4791
c6556aa0
RH
4792 case TCG_CALL_RET_BY_VEC:
4793 {
4794 TCGTemp *ts = arg_temp(op->args[0]);
4795
4796 tcg_debug_assert(ts->base_type == TCG_TYPE_I128);
4797 tcg_debug_assert(ts->temp_subindex == 0);
4798 if (!ts->mem_allocated) {
4799 temp_allocate_frame(s, ts);
4800 }
4801 tcg_out_st(s, TCG_TYPE_V128,
4802 tcg_target_call_oarg_reg(TCG_CALL_RET_BY_VEC, 0),
4803 ts->mem_base->reg, ts->mem_offset);
4804 }
4805 /* fall through to mark all parts in memory */
4806
313bdea8
RH
4807 case TCG_CALL_RET_BY_REF:
4808 /* The callee has performed a write through the reference. */
4809 for (i = 0; i < nb_oargs; i++) {
4810 TCGTemp *ts = arg_temp(op->args[i]);
4811 ts->val_type = TEMP_VAL_MEM;
4812 }
4813 break;
4814
39004a71
RH
4815 default:
4816 g_assert_not_reached();
4817 }
4818
4819 /* Flush or discard output registers as needed. */
4820 for (i = 0; i < nb_oargs; i++) {
4821 TCGTemp *ts = arg_temp(op->args[i]);
d63e3b6e 4822 if (NEED_SYNC_ARG(i)) {
39004a71 4823 temp_sync(s, ts, s->reserved_regs, 0, IS_DEAD_ARG(i));
d63e3b6e
RH
4824 } else if (IS_DEAD_ARG(i)) {
4825 temp_dead(s, ts);
c896fe29
FB
4826 }
4827 }
c896fe29
FB
4828}
4829
4830#ifdef CONFIG_PROFILER
4831
c3fac113
EC
4832/* avoid copy/paste errors */
4833#define PROF_ADD(to, from, field) \
4834 do { \
d73415a3 4835 (to)->field += qatomic_read(&((from)->field)); \
c3fac113
EC
4836 } while (0)
4837
4838#define PROF_MAX(to, from, field) \
4839 do { \
d73415a3 4840 typeof((from)->field) val__ = qatomic_read(&((from)->field)); \
c3fac113
EC
4841 if (val__ > (to)->field) { \
4842 (to)->field = val__; \
4843 } \
4844 } while (0)
4845
4846/* Pass in a zero'ed @prof */
4847static inline
4848void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
4849{
0e2d61cf 4850 unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
c3fac113
EC
4851 unsigned int i;
4852
3468b59e 4853 for (i = 0; i < n_ctxs; i++) {
d73415a3 4854 TCGContext *s = qatomic_read(&tcg_ctxs[i]);
3468b59e 4855 const TCGProfile *orig = &s->prof;
c3fac113
EC
4856
4857 if (counters) {
72fd2efb 4858 PROF_ADD(prof, orig, cpu_exec_time);
c3fac113
EC
4859 PROF_ADD(prof, orig, tb_count1);
4860 PROF_ADD(prof, orig, tb_count);
4861 PROF_ADD(prof, orig, op_count);
4862 PROF_MAX(prof, orig, op_count_max);
4863 PROF_ADD(prof, orig, temp_count);
4864 PROF_MAX(prof, orig, temp_count_max);
4865 PROF_ADD(prof, orig, del_op_count);
4866 PROF_ADD(prof, orig, code_in_len);
4867 PROF_ADD(prof, orig, code_out_len);
4868 PROF_ADD(prof, orig, search_out_len);
4869 PROF_ADD(prof, orig, interm_time);
4870 PROF_ADD(prof, orig, code_time);
4871 PROF_ADD(prof, orig, la_time);
4872 PROF_ADD(prof, orig, opt_time);
4873 PROF_ADD(prof, orig, restore_count);
4874 PROF_ADD(prof, orig, restore_time);
4875 }
4876 if (table) {
4877 int i;
4878
4879 for (i = 0; i < NB_OPS; i++) {
4880 PROF_ADD(prof, orig, table_op_count[i]);
4881 }
4882 }
4883 }
4884}
4885
4886#undef PROF_ADD
4887#undef PROF_MAX
4888
4889static void tcg_profile_snapshot_counters(TCGProfile *prof)
4890{
4891 tcg_profile_snapshot(prof, true, false);
4892}
4893
4894static void tcg_profile_snapshot_table(TCGProfile *prof)
4895{
4896 tcg_profile_snapshot(prof, false, true);
4897}
c896fe29 4898
b6a7f3e0 4899void tcg_dump_op_count(GString *buf)
c896fe29 4900{
c3fac113 4901 TCGProfile prof = {};
c896fe29 4902 int i;
d70724ce 4903
c3fac113 4904 tcg_profile_snapshot_table(&prof);
15fc7daa 4905 for (i = 0; i < NB_OPS; i++) {
b6a7f3e0
DB
4906 g_string_append_printf(buf, "%s %" PRId64 "\n", tcg_op_defs[i].name,
4907 prof.table_op_count[i]);
c896fe29 4908 }
c896fe29 4909}
72fd2efb
EC
4910
4911int64_t tcg_cpu_exec_time(void)
4912{
0e2d61cf 4913 unsigned int n_ctxs = qatomic_read(&tcg_cur_ctxs);
72fd2efb
EC
4914 unsigned int i;
4915 int64_t ret = 0;
4916
4917 for (i = 0; i < n_ctxs; i++) {
d73415a3 4918 const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
72fd2efb
EC
4919 const TCGProfile *prof = &s->prof;
4920
d73415a3 4921 ret += qatomic_read(&prof->cpu_exec_time);
72fd2efb
EC
4922 }
4923 return ret;
4924}
246ae24d 4925#else
b6a7f3e0 4926void tcg_dump_op_count(GString *buf)
246ae24d 4927{
b6a7f3e0 4928 g_string_append_printf(buf, "[TCG profiler not compiled]\n");
246ae24d 4929}
72fd2efb
EC
4930
4931int64_t tcg_cpu_exec_time(void)
4932{
4933 error_report("%s: TCG profiler not compiled", __func__);
4934 exit(EXIT_FAILURE);
4935}
c896fe29
FB
4936#endif
4937
4938
fbf59aad 4939int tcg_gen_code(TCGContext *s, TranslationBlock *tb, target_ulong pc_start)
c896fe29 4940{
c3fac113
EC
4941#ifdef CONFIG_PROFILER
4942 TCGProfile *prof = &s->prof;
4943#endif
15fa08f8
RH
4944 int i, num_insns;
4945 TCGOp *op;
c896fe29 4946
04fe6400
RH
4947#ifdef CONFIG_PROFILER
4948 {
c1f543b7 4949 int n = 0;
04fe6400 4950
15fa08f8
RH
4951 QTAILQ_FOREACH(op, &s->ops, link) {
4952 n++;
4953 }
d73415a3 4954 qatomic_set(&prof->op_count, prof->op_count + n);
c3fac113 4955 if (n > prof->op_count_max) {
d73415a3 4956 qatomic_set(&prof->op_count_max, n);
04fe6400
RH
4957 }
4958
4959 n = s->nb_temps;
d73415a3 4960 qatomic_set(&prof->temp_count, prof->temp_count + n);
c3fac113 4961 if (n > prof->temp_count_max) {
d73415a3 4962 qatomic_set(&prof->temp_count_max, n);
04fe6400
RH
4963 }
4964 }
4965#endif
4966
c896fe29 4967#ifdef DEBUG_DISAS
d977e1c2 4968 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
fbf59aad 4969 && qemu_log_in_addr_range(pc_start))) {
c60f599b 4970 FILE *logfile = qemu_log_trylock();
78b54858
RH
4971 if (logfile) {
4972 fprintf(logfile, "OP:\n");
b7a83ff8 4973 tcg_dump_ops(s, logfile, false);
78b54858
RH
4974 fprintf(logfile, "\n");
4975 qemu_log_unlock(logfile);
4976 }
c896fe29
FB
4977 }
4978#endif
4979
bef16ab4
RH
4980#ifdef CONFIG_DEBUG_TCG
4981 /* Ensure all labels referenced have been emitted. */
4982 {
4983 TCGLabel *l;
4984 bool error = false;
4985
4986 QSIMPLEQ_FOREACH(l, &s->labels, next) {
f85b1fc4 4987 if (unlikely(!l->present) && !QSIMPLEQ_EMPTY(&l->branches)) {
bef16ab4
RH
4988 qemu_log_mask(CPU_LOG_TB_OP,
4989 "$L%d referenced but not present.\n", l->id);
4990 error = true;
4991 }
4992 }
4993 assert(!error);
4994 }
4995#endif
4996
c5cc28ff 4997#ifdef CONFIG_PROFILER
d73415a3 4998 qatomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
c5cc28ff
AJ
4999#endif
5000
8f2e8c07 5001#ifdef USE_TCG_OPTIMIZATIONS
c45cb8bb 5002 tcg_optimize(s);
8f2e8c07
KB
5003#endif
5004
a23a9ec6 5005#ifdef CONFIG_PROFILER
d73415a3
SH
5006 qatomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
5007 qatomic_set(&prof->la_time, prof->la_time - profile_getclock());
a23a9ec6 5008#endif
c5cc28ff 5009
b4fc67c7 5010 reachable_code_pass(s);
874b8574 5011 liveness_pass_0(s);
b83eabea 5012 liveness_pass_1(s);
5a18407f 5013
b83eabea 5014 if (s->nb_indirects > 0) {
5a18407f 5015#ifdef DEBUG_DISAS
b83eabea 5016 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
fbf59aad 5017 && qemu_log_in_addr_range(pc_start))) {
c60f599b 5018 FILE *logfile = qemu_log_trylock();
78b54858
RH
5019 if (logfile) {
5020 fprintf(logfile, "OP before indirect lowering:\n");
b7a83ff8 5021 tcg_dump_ops(s, logfile, false);
78b54858
RH
5022 fprintf(logfile, "\n");
5023 qemu_log_unlock(logfile);
5024 }
b83eabea 5025 }
5a18407f 5026#endif
b83eabea
RH
5027 /* Replace indirect temps with direct temps. */
5028 if (liveness_pass_2(s)) {
5029 /* If changes were made, re-run liveness. */
5030 liveness_pass_1(s);
5a18407f
RH
5031 }
5032 }
c5cc28ff 5033
a23a9ec6 5034#ifdef CONFIG_PROFILER
d73415a3 5035 qatomic_set(&prof->la_time, prof->la_time + profile_getclock());
a23a9ec6 5036#endif
c896fe29
FB
5037
5038#ifdef DEBUG_DISAS
d977e1c2 5039 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
fbf59aad 5040 && qemu_log_in_addr_range(pc_start))) {
c60f599b 5041 FILE *logfile = qemu_log_trylock();
78b54858
RH
5042 if (logfile) {
5043 fprintf(logfile, "OP after optimization and liveness analysis:\n");
b7a83ff8 5044 tcg_dump_ops(s, logfile, true);
78b54858
RH
5045 fprintf(logfile, "\n");
5046 qemu_log_unlock(logfile);
5047 }
c896fe29
FB
5048 }
5049#endif
5050
35abb009 5051 /* Initialize goto_tb jump offsets. */
3a50f424
RH
5052 tb->jmp_reset_offset[0] = TB_JMP_OFFSET_INVALID;
5053 tb->jmp_reset_offset[1] = TB_JMP_OFFSET_INVALID;
9da6079b
RH
5054 tb->jmp_insn_offset[0] = TB_JMP_OFFSET_INVALID;
5055 tb->jmp_insn_offset[1] = TB_JMP_OFFSET_INVALID;
35abb009 5056
c896fe29
FB
5057 tcg_reg_alloc_start(s);
5058
db0c51a3
RH
5059 /*
5060 * Reset the buffer pointers when restarting after overflow.
5061 * TODO: Move this into translate-all.c with the rest of the
5062 * buffer management. Having only this done here is confusing.
5063 */
5064 s->code_buf = tcg_splitwx_to_rw(tb->tc.ptr);
5065 s->code_ptr = s->code_buf;
c896fe29 5066
659ef5cb 5067#ifdef TCG_TARGET_NEED_LDST_LABELS
6001f772 5068 QSIMPLEQ_INIT(&s->ldst_labels);
659ef5cb 5069#endif
57a26946
RH
5070#ifdef TCG_TARGET_NEED_POOL_LABELS
5071 s->pool_labels = NULL;
5072#endif
9ecefc84 5073
fca8a500 5074 num_insns = -1;
15fa08f8 5075 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb 5076 TCGOpcode opc = op->opc;
b3db8758 5077
c896fe29 5078#ifdef CONFIG_PROFILER
d73415a3 5079 qatomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
c896fe29 5080#endif
c45cb8bb
RH
5081
5082 switch (opc) {
c896fe29 5083 case INDEX_op_mov_i32:
c896fe29 5084 case INDEX_op_mov_i64:
d2fd745f 5085 case INDEX_op_mov_vec:
dd186292 5086 tcg_reg_alloc_mov(s, op);
c896fe29 5087 break;
bab1671f
RH
5088 case INDEX_op_dup_vec:
5089 tcg_reg_alloc_dup(s, op);
5090 break;
765b842a 5091 case INDEX_op_insn_start:
fca8a500 5092 if (num_insns >= 0) {
9f754620
RH
5093 size_t off = tcg_current_code_size(s);
5094 s->gen_insn_end_off[num_insns] = off;
5095 /* Assert that we do not overflow our stored offset. */
5096 assert(s->gen_insn_end_off[num_insns] == off);
fca8a500
RH
5097 }
5098 num_insns++;
bad729e2
RH
5099 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
5100 target_ulong a;
5101#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
efee3746 5102 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
bad729e2 5103#else
efee3746 5104 a = op->args[i];
bad729e2 5105#endif
fca8a500 5106 s->gen_insn_data[num_insns][i] = a;
bad729e2 5107 }
c896fe29 5108 break;
5ff9d6a4 5109 case INDEX_op_discard:
43439139 5110 temp_dead(s, arg_temp(op->args[0]));
5ff9d6a4 5111 break;
c896fe29 5112 case INDEX_op_set_label:
e8996ee0 5113 tcg_reg_alloc_bb_end(s, s->reserved_regs);
92ab8e7d 5114 tcg_out_label(s, arg_label(op->args[0]));
c896fe29
FB
5115 break;
5116 case INDEX_op_call:
dd186292 5117 tcg_reg_alloc_call(s, op);
c45cb8bb 5118 break;
b55a8d9d
RH
5119 case INDEX_op_exit_tb:
5120 tcg_out_exit_tb(s, op->args[0]);
5121 break;
cf7d6b8e
RH
5122 case INDEX_op_goto_tb:
5123 tcg_out_goto_tb(s, op->args[0]);
5124 break;
efe86b21
RH
5125 case INDEX_op_dup2_vec:
5126 if (tcg_reg_alloc_dup2(s, op)) {
5127 break;
5128 }
5129 /* fall through */
c896fe29 5130 default:
25c4d9cc 5131 /* Sanity check that we've not introduced any unhandled opcodes. */
be0f34b5 5132 tcg_debug_assert(tcg_op_supported(opc));
c896fe29
FB
5133 /* Note: in order to speed up the code, it would be much
5134 faster to have specialized register allocator functions for
5135 some common argument patterns */
dd186292 5136 tcg_reg_alloc_op(s, op);
c896fe29
FB
5137 break;
5138 }
b125f9dc
RH
5139 /* Test for (pending) buffer overflow. The assumption is that any
5140 one operation beginning below the high water mark cannot overrun
5141 the buffer completely. Thus we can test for overflow after
5142 generating code without having to check during generation. */
644da9b3 5143 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
b125f9dc
RH
5144 return -1;
5145 }
6e6c4efe
RH
5146 /* Test for TB overflow, as seen by gen_insn_end_off. */
5147 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
5148 return -2;
5149 }
c896fe29 5150 }
fca8a500
RH
5151 tcg_debug_assert(num_insns >= 0);
5152 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
c45cb8bb 5153
b76f0d8c 5154 /* Generate TB finalization at the end of block */
659ef5cb 5155#ifdef TCG_TARGET_NEED_LDST_LABELS
aeee05f5
RH
5156 i = tcg_out_ldst_finalize(s);
5157 if (i < 0) {
5158 return i;
23dceda6 5159 }
659ef5cb 5160#endif
57a26946 5161#ifdef TCG_TARGET_NEED_POOL_LABELS
1768987b
RH
5162 i = tcg_out_pool_finalize(s);
5163 if (i < 0) {
5164 return i;
57a26946
RH
5165 }
5166#endif
7ecd02a0
RH
5167 if (!tcg_resolve_relocs(s)) {
5168 return -2;
5169 }
c896fe29 5170
df5d2b16 5171#ifndef CONFIG_TCG_INTERPRETER
c896fe29 5172 /* flush instruction cache */
db0c51a3
RH
5173 flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
5174 (uintptr_t)s->code_buf,
1da8de39 5175 tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
df5d2b16 5176#endif
2aeabc08 5177
1813e175 5178 return tcg_current_code_size(s);
c896fe29
FB
5179}
5180
a23a9ec6 5181#ifdef CONFIG_PROFILER
3a841ab5 5182void tcg_dump_info(GString *buf)
a23a9ec6 5183{
c3fac113
EC
5184 TCGProfile prof = {};
5185 const TCGProfile *s;
5186 int64_t tb_count;
5187 int64_t tb_div_count;
5188 int64_t tot;
5189
5190 tcg_profile_snapshot_counters(&prof);
5191 s = &prof;
5192 tb_count = s->tb_count;
5193 tb_div_count = tb_count ? tb_count : 1;
5194 tot = s->interm_time + s->code_time;
a23a9ec6 5195
3a841ab5
DB
5196 g_string_append_printf(buf, "JIT cycles %" PRId64
5197 " (%0.3f s at 2.4 GHz)\n",
5198 tot, tot / 2.4e9);
5199 g_string_append_printf(buf, "translated TBs %" PRId64
5200 " (aborted=%" PRId64 " %0.1f%%)\n",
5201 tb_count, s->tb_count1 - tb_count,
5202 (double)(s->tb_count1 - s->tb_count)
5203 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
5204 g_string_append_printf(buf, "avg ops/TB %0.1f max=%d\n",
5205 (double)s->op_count / tb_div_count, s->op_count_max);
5206 g_string_append_printf(buf, "deleted ops/TB %0.2f\n",
5207 (double)s->del_op_count / tb_div_count);
5208 g_string_append_printf(buf, "avg temps/TB %0.2f max=%d\n",
5209 (double)s->temp_count / tb_div_count,
5210 s->temp_count_max);
5211 g_string_append_printf(buf, "avg host code/TB %0.1f\n",
5212 (double)s->code_out_len / tb_div_count);
5213 g_string_append_printf(buf, "avg search data/TB %0.1f\n",
5214 (double)s->search_out_len / tb_div_count);
a813e36f 5215
3a841ab5
DB
5216 g_string_append_printf(buf, "cycles/op %0.1f\n",
5217 s->op_count ? (double)tot / s->op_count : 0);
5218 g_string_append_printf(buf, "cycles/in byte %0.1f\n",
5219 s->code_in_len ? (double)tot / s->code_in_len : 0);
5220 g_string_append_printf(buf, "cycles/out byte %0.1f\n",
5221 s->code_out_len ? (double)tot / s->code_out_len : 0);
5222 g_string_append_printf(buf, "cycles/search byte %0.1f\n",
5223 s->search_out_len ?
5224 (double)tot / s->search_out_len : 0);
fca8a500 5225 if (tot == 0) {
a23a9ec6 5226 tot = 1;
fca8a500 5227 }
3a841ab5
DB
5228 g_string_append_printf(buf, " gen_interm time %0.1f%%\n",
5229 (double)s->interm_time / tot * 100.0);
5230 g_string_append_printf(buf, " gen_code time %0.1f%%\n",
5231 (double)s->code_time / tot * 100.0);
5232 g_string_append_printf(buf, "optim./code time %0.1f%%\n",
5233 (double)s->opt_time / (s->code_time ?
5234 s->code_time : 1)
5235 * 100.0);
5236 g_string_append_printf(buf, "liveness/code time %0.1f%%\n",
5237 (double)s->la_time / (s->code_time ?
5238 s->code_time : 1) * 100.0);
5239 g_string_append_printf(buf, "cpu_restore count %" PRId64 "\n",
5240 s->restore_count);
5241 g_string_append_printf(buf, " avg cycles %0.1f\n",
5242 s->restore_count ?
5243 (double)s->restore_time / s->restore_count : 0);
a23a9ec6
FB
5244}
5245#else
3a841ab5 5246void tcg_dump_info(GString *buf)
a23a9ec6 5247{
3a841ab5 5248 g_string_append_printf(buf, "[TCG profiler not compiled]\n");
a23a9ec6
FB
5249}
5250#endif
813da627
RH
5251
5252#ifdef ELF_HOST_MACHINE
5872bbf2
RH
5253/* In order to use this feature, the backend needs to do three things:
5254
5255 (1) Define ELF_HOST_MACHINE to indicate both what value to
5256 put into the ELF image and to indicate support for the feature.
5257
5258 (2) Define tcg_register_jit. This should create a buffer containing
5259 the contents of a .debug_frame section that describes the post-
5260 prologue unwind info for the tcg machine.
5261
5262 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
5263*/
813da627
RH
5264
5265/* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
5266typedef enum {
5267 JIT_NOACTION = 0,
5268 JIT_REGISTER_FN,
5269 JIT_UNREGISTER_FN
5270} jit_actions_t;
5271
5272struct jit_code_entry {
5273 struct jit_code_entry *next_entry;
5274 struct jit_code_entry *prev_entry;
5275 const void *symfile_addr;
5276 uint64_t symfile_size;
5277};
5278
5279struct jit_descriptor {
5280 uint32_t version;
5281 uint32_t action_flag;
5282 struct jit_code_entry *relevant_entry;
5283 struct jit_code_entry *first_entry;
5284};
5285
5286void __jit_debug_register_code(void) __attribute__((noinline));
5287void __jit_debug_register_code(void)
5288{
5289 asm("");
5290}
5291
5292/* Must statically initialize the version, because GDB may check
5293 the version before we can set it. */
5294struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
5295
5296/* End GDB interface. */
5297
5298static int find_string(const char *strtab, const char *str)
5299{
5300 const char *p = strtab + 1;
5301
5302 while (1) {
5303 if (strcmp(p, str) == 0) {
5304 return p - strtab;
5305 }
5306 p += strlen(p) + 1;
5307 }
5308}
5309
755bf9e5 5310static void tcg_register_jit_int(const void *buf_ptr, size_t buf_size,
2c90784a
RH
5311 const void *debug_frame,
5312 size_t debug_frame_size)
813da627 5313{
5872bbf2
RH
5314 struct __attribute__((packed)) DebugInfo {
5315 uint32_t len;
5316 uint16_t version;
5317 uint32_t abbrev;
5318 uint8_t ptr_size;
5319 uint8_t cu_die;
5320 uint16_t cu_lang;
5321 uintptr_t cu_low_pc;
5322 uintptr_t cu_high_pc;
5323 uint8_t fn_die;
5324 char fn_name[16];
5325 uintptr_t fn_low_pc;
5326 uintptr_t fn_high_pc;
5327 uint8_t cu_eoc;
5328 };
813da627
RH
5329
5330 struct ElfImage {
5331 ElfW(Ehdr) ehdr;
5332 ElfW(Phdr) phdr;
5872bbf2
RH
5333 ElfW(Shdr) shdr[7];
5334 ElfW(Sym) sym[2];
5335 struct DebugInfo di;
5336 uint8_t da[24];
5337 char str[80];
5338 };
5339
5340 struct ElfImage *img;
5341
5342 static const struct ElfImage img_template = {
5343 .ehdr = {
5344 .e_ident[EI_MAG0] = ELFMAG0,
5345 .e_ident[EI_MAG1] = ELFMAG1,
5346 .e_ident[EI_MAG2] = ELFMAG2,
5347 .e_ident[EI_MAG3] = ELFMAG3,
5348 .e_ident[EI_CLASS] = ELF_CLASS,
5349 .e_ident[EI_DATA] = ELF_DATA,
5350 .e_ident[EI_VERSION] = EV_CURRENT,
5351 .e_type = ET_EXEC,
5352 .e_machine = ELF_HOST_MACHINE,
5353 .e_version = EV_CURRENT,
5354 .e_phoff = offsetof(struct ElfImage, phdr),
5355 .e_shoff = offsetof(struct ElfImage, shdr),
5356 .e_ehsize = sizeof(ElfW(Shdr)),
5357 .e_phentsize = sizeof(ElfW(Phdr)),
5358 .e_phnum = 1,
5359 .e_shentsize = sizeof(ElfW(Shdr)),
5360 .e_shnum = ARRAY_SIZE(img->shdr),
5361 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
abbb3eae
RH
5362#ifdef ELF_HOST_FLAGS
5363 .e_flags = ELF_HOST_FLAGS,
5364#endif
5365#ifdef ELF_OSABI
5366 .e_ident[EI_OSABI] = ELF_OSABI,
5367#endif
5872bbf2
RH
5368 },
5369 .phdr = {
5370 .p_type = PT_LOAD,
5371 .p_flags = PF_X,
5372 },
5373 .shdr = {
5374 [0] = { .sh_type = SHT_NULL },
5375 /* Trick: The contents of code_gen_buffer are not present in
5376 this fake ELF file; that got allocated elsewhere. Therefore
5377 we mark .text as SHT_NOBITS (similar to .bss) so that readers
5378 will not look for contents. We can record any address. */
5379 [1] = { /* .text */
5380 .sh_type = SHT_NOBITS,
5381 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
5382 },
5383 [2] = { /* .debug_info */
5384 .sh_type = SHT_PROGBITS,
5385 .sh_offset = offsetof(struct ElfImage, di),
5386 .sh_size = sizeof(struct DebugInfo),
5387 },
5388 [3] = { /* .debug_abbrev */
5389 .sh_type = SHT_PROGBITS,
5390 .sh_offset = offsetof(struct ElfImage, da),
5391 .sh_size = sizeof(img->da),
5392 },
5393 [4] = { /* .debug_frame */
5394 .sh_type = SHT_PROGBITS,
5395 .sh_offset = sizeof(struct ElfImage),
5396 },
5397 [5] = { /* .symtab */
5398 .sh_type = SHT_SYMTAB,
5399 .sh_offset = offsetof(struct ElfImage, sym),
5400 .sh_size = sizeof(img->sym),
5401 .sh_info = 1,
5402 .sh_link = ARRAY_SIZE(img->shdr) - 1,
5403 .sh_entsize = sizeof(ElfW(Sym)),
5404 },
5405 [6] = { /* .strtab */
5406 .sh_type = SHT_STRTAB,
5407 .sh_offset = offsetof(struct ElfImage, str),
5408 .sh_size = sizeof(img->str),
5409 }
5410 },
5411 .sym = {
5412 [1] = { /* code_gen_buffer */
5413 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
5414 .st_shndx = 1,
5415 }
5416 },
5417 .di = {
5418 .len = sizeof(struct DebugInfo) - 4,
5419 .version = 2,
5420 .ptr_size = sizeof(void *),
5421 .cu_die = 1,
5422 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
5423 .fn_die = 2,
5424 .fn_name = "code_gen_buffer"
5425 },
5426 .da = {
5427 1, /* abbrev number (the cu) */
5428 0x11, 1, /* DW_TAG_compile_unit, has children */
5429 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
5430 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
5431 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
5432 0, 0, /* end of abbrev */
5433 2, /* abbrev number (the fn) */
5434 0x2e, 0, /* DW_TAG_subprogram, no children */
5435 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
5436 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
5437 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
5438 0, 0, /* end of abbrev */
5439 0 /* no more abbrev */
5440 },
5441 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
5442 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
813da627
RH
5443 };
5444
5445 /* We only need a single jit entry; statically allocate it. */
5446 static struct jit_code_entry one_entry;
5447
5872bbf2 5448 uintptr_t buf = (uintptr_t)buf_ptr;
813da627 5449 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
2c90784a 5450 DebugFrameHeader *dfh;
813da627 5451
5872bbf2
RH
5452 img = g_malloc(img_size);
5453 *img = img_template;
813da627 5454
5872bbf2
RH
5455 img->phdr.p_vaddr = buf;
5456 img->phdr.p_paddr = buf;
5457 img->phdr.p_memsz = buf_size;
813da627 5458
813da627 5459 img->shdr[1].sh_name = find_string(img->str, ".text");
5872bbf2 5460 img->shdr[1].sh_addr = buf;
813da627
RH
5461 img->shdr[1].sh_size = buf_size;
5462
5872bbf2
RH
5463 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
5464 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
5465
5466 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
5467 img->shdr[4].sh_size = debug_frame_size;
5468
5469 img->shdr[5].sh_name = find_string(img->str, ".symtab");
5470 img->shdr[6].sh_name = find_string(img->str, ".strtab");
5471
5472 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
5473 img->sym[1].st_value = buf;
5474 img->sym[1].st_size = buf_size;
813da627 5475
5872bbf2 5476 img->di.cu_low_pc = buf;
45aba097 5477 img->di.cu_high_pc = buf + buf_size;
5872bbf2 5478 img->di.fn_low_pc = buf;
45aba097 5479 img->di.fn_high_pc = buf + buf_size;
813da627 5480
2c90784a
RH
5481 dfh = (DebugFrameHeader *)(img + 1);
5482 memcpy(dfh, debug_frame, debug_frame_size);
5483 dfh->fde.func_start = buf;
5484 dfh->fde.func_len = buf_size;
5485
813da627
RH
5486#ifdef DEBUG_JIT
5487 /* Enable this block to be able to debug the ELF image file creation.
5488 One can use readelf, objdump, or other inspection utilities. */
5489 {
eb6b2edf
BM
5490 g_autofree char *jit = g_strdup_printf("%s/qemu.jit", g_get_tmp_dir());
5491 FILE *f = fopen(jit, "w+b");
813da627 5492 if (f) {
5872bbf2 5493 if (fwrite(img, img_size, 1, f) != img_size) {
813da627
RH
5494 /* Avoid stupid unused return value warning for fwrite. */
5495 }
5496 fclose(f);
5497 }
5498 }
5499#endif
5500
5501 one_entry.symfile_addr = img;
5502 one_entry.symfile_size = img_size;
5503
5504 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
5505 __jit_debug_descriptor.relevant_entry = &one_entry;
5506 __jit_debug_descriptor.first_entry = &one_entry;
5507 __jit_debug_register_code();
5508}
5509#else
5872bbf2
RH
5510/* No support for the feature. Provide the entry point expected by exec.c,
5511 and implement the internal function we declared earlier. */
813da627 5512
755bf9e5 5513static void tcg_register_jit_int(const void *buf, size_t size,
2c90784a
RH
5514 const void *debug_frame,
5515 size_t debug_frame_size)
813da627
RH
5516{
5517}
5518
755bf9e5 5519void tcg_register_jit(const void *buf, size_t buf_size)
813da627
RH
5520{
5521}
5522#endif /* ELF_HOST_MACHINE */
db432672
RH
5523
5524#if !TCG_TARGET_MAYBE_vec
5525void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
5526{
5527 g_assert_not_reached();
5528}
5529#endif