]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/tcg.c
tcg/aarch64: Implement tcg_out_dupm_vec
[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"
1de7afc9 37#include "qemu/timer.h"
c896fe29 38
c5d3c498 39/* Note: the long term plan is to reduce the dependencies on the QEMU
c896fe29
FB
40 CPU definitions. Currently they are used for qemu_ld/st
41 instructions */
42#define NO_CPU_IO_DEFS
43#include "cpu.h"
c896fe29 44
63c91552
PB
45#include "exec/cpu-common.h"
46#include "exec/exec-all.h"
47
c896fe29 48#include "tcg-op.h"
813da627 49
edee2579 50#if UINTPTR_MAX == UINT32_MAX
813da627 51# define ELF_CLASS ELFCLASS32
edee2579
RH
52#else
53# define ELF_CLASS ELFCLASS64
813da627
RH
54#endif
55#ifdef HOST_WORDS_BIGENDIAN
56# define ELF_DATA ELFDATA2MSB
57#else
58# define ELF_DATA ELFDATA2LSB
59#endif
60
c896fe29 61#include "elf.h"
508127e2 62#include "exec/log.h"
3468b59e 63#include "sysemu/sysemu.h"
c896fe29 64
ce151109
PM
65/* Forward declarations for functions declared in tcg-target.inc.c and
66 used here. */
e4d58b41 67static void tcg_target_init(TCGContext *s);
f69d277e 68static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode);
e4d58b41 69static void tcg_target_qemu_prologue(TCGContext *s);
6ac17786 70static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
2ba7fae2 71 intptr_t value, intptr_t addend);
c896fe29 72
497a22eb
RH
73/* The CIE and FDE header definitions will be common to all hosts. */
74typedef struct {
75 uint32_t len __attribute__((aligned((sizeof(void *)))));
76 uint32_t id;
77 uint8_t version;
78 char augmentation[1];
79 uint8_t code_align;
80 uint8_t data_align;
81 uint8_t return_column;
82} DebugFrameCIE;
83
84typedef struct QEMU_PACKED {
85 uint32_t len __attribute__((aligned((sizeof(void *)))));
86 uint32_t cie_offset;
edee2579
RH
87 uintptr_t func_start;
88 uintptr_t func_len;
497a22eb
RH
89} DebugFrameFDEHeader;
90
2c90784a
RH
91typedef struct QEMU_PACKED {
92 DebugFrameCIE cie;
93 DebugFrameFDEHeader fde;
94} DebugFrameHeader;
95
813da627 96static void tcg_register_jit_int(void *buf, size_t size,
2c90784a
RH
97 const void *debug_frame,
98 size_t debug_frame_size)
813da627
RH
99 __attribute__((unused));
100
ce151109 101/* Forward declarations for functions declared and used in tcg-target.inc.c. */
069ea736
RH
102static const char *target_parse_constraint(TCGArgConstraint *ct,
103 const char *ct_str, TCGType type);
2a534aff 104static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
a05b5b9b 105 intptr_t arg2);
78113e83 106static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
c0ad3001 107static void tcg_out_movi(TCGContext *s, TCGType type,
2a534aff 108 TCGReg ret, tcg_target_long arg);
c0ad3001
SW
109static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
110 const int *const_args);
d2fd745f 111#if TCG_TARGET_MAYBE_vec
e7632cfa
RH
112static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
113 TCGReg dst, TCGReg src);
d6ecb4a9
RH
114static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
115 TCGReg dst, TCGReg base, intptr_t offset);
e7632cfa
RH
116static void tcg_out_dupi_vec(TCGContext *s, TCGType type,
117 TCGReg dst, tcg_target_long arg);
d2fd745f
RH
118static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
119 unsigned vece, const TCGArg *args,
120 const int *const_args);
121#else
e7632cfa
RH
122static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
123 TCGReg dst, TCGReg src)
124{
125 g_assert_not_reached();
126}
d6ecb4a9
RH
127static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
128 TCGReg dst, TCGReg base, intptr_t offset)
129{
130 g_assert_not_reached();
131}
e7632cfa
RH
132static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type,
133 TCGReg dst, tcg_target_long arg)
134{
135 g_assert_not_reached();
136}
d2fd745f
RH
137static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
138 unsigned vece, const TCGArg *args,
139 const int *const_args)
140{
141 g_assert_not_reached();
142}
143#endif
2a534aff 144static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
a05b5b9b 145 intptr_t arg2);
59d7c14e
RH
146static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
147 TCGReg base, intptr_t ofs);
cf066674 148static void tcg_out_call(TCGContext *s, tcg_insn_unit *target);
f6c6afc1 149static int tcg_target_const_match(tcg_target_long val, TCGType type,
c0ad3001 150 const TCGArgConstraint *arg_ct);
659ef5cb 151#ifdef TCG_TARGET_NEED_LDST_LABELS
aeee05f5 152static int tcg_out_ldst_finalize(TCGContext *s);
659ef5cb 153#endif
c896fe29 154
a505785c
EC
155#define TCG_HIGHWATER 1024
156
df2cce29
EC
157static TCGContext **tcg_ctxs;
158static unsigned int n_tcg_ctxs;
1c2adb95 159TCGv_env cpu_env = 0;
df2cce29 160
be2cdc5e
EC
161struct tcg_region_tree {
162 QemuMutex lock;
163 GTree *tree;
164 /* padding to avoid false sharing is computed at run-time */
165};
166
e8feb96f
EC
167/*
168 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
169 * dynamically allocate from as demand dictates. Given appropriate region
170 * sizing, this minimizes flushes even when some TCG threads generate a lot
171 * more code than others.
172 */
173struct tcg_region_state {
174 QemuMutex lock;
175
176 /* fields set at init time */
177 void *start;
178 void *start_aligned;
179 void *end;
180 size_t n;
181 size_t size; /* size of one region */
182 size_t stride; /* .size + guard size */
183
184 /* fields protected by the lock */
185 size_t current; /* current region index */
186 size_t agg_size_full; /* aggregate size of full regions */
187};
188
189static struct tcg_region_state region;
be2cdc5e
EC
190/*
191 * This is an array of struct tcg_region_tree's, with padding.
192 * We use void * to simplify the computation of region_trees[i]; each
193 * struct is found every tree_size bytes.
194 */
195static void *region_trees;
196static size_t tree_size;
d2fd745f 197static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
b1d8e52e 198static TCGRegSet tcg_target_call_clobber_regs;
c896fe29 199
1813e175 200#if TCG_TARGET_INSN_UNIT_SIZE == 1
4196dca6 201static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
c896fe29
FB
202{
203 *s->code_ptr++ = v;
204}
205
4196dca6
PM
206static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
207 uint8_t v)
5c53bb81 208{
1813e175 209 *p = v;
5c53bb81 210}
1813e175 211#endif
5c53bb81 212
1813e175 213#if TCG_TARGET_INSN_UNIT_SIZE <= 2
4196dca6 214static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
c896fe29 215{
1813e175
RH
216 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
217 *s->code_ptr++ = v;
218 } else {
219 tcg_insn_unit *p = s->code_ptr;
220 memcpy(p, &v, sizeof(v));
221 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
222 }
c896fe29
FB
223}
224
4196dca6
PM
225static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
226 uint16_t v)
5c53bb81 227{
1813e175
RH
228 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
229 *p = v;
230 } else {
231 memcpy(p, &v, sizeof(v));
232 }
5c53bb81 233}
1813e175 234#endif
5c53bb81 235
1813e175 236#if TCG_TARGET_INSN_UNIT_SIZE <= 4
4196dca6 237static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
c896fe29 238{
1813e175
RH
239 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
240 *s->code_ptr++ = v;
241 } else {
242 tcg_insn_unit *p = s->code_ptr;
243 memcpy(p, &v, sizeof(v));
244 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
245 }
c896fe29
FB
246}
247
4196dca6
PM
248static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
249 uint32_t v)
5c53bb81 250{
1813e175
RH
251 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
252 *p = v;
253 } else {
254 memcpy(p, &v, sizeof(v));
255 }
5c53bb81 256}
1813e175 257#endif
5c53bb81 258
1813e175 259#if TCG_TARGET_INSN_UNIT_SIZE <= 8
4196dca6 260static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
ac26eb69 261{
1813e175
RH
262 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
263 *s->code_ptr++ = v;
264 } else {
265 tcg_insn_unit *p = s->code_ptr;
266 memcpy(p, &v, sizeof(v));
267 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
268 }
ac26eb69
RH
269}
270
4196dca6
PM
271static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
272 uint64_t v)
5c53bb81 273{
1813e175
RH
274 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
275 *p = v;
276 } else {
277 memcpy(p, &v, sizeof(v));
278 }
5c53bb81 279}
1813e175 280#endif
5c53bb81 281
c896fe29
FB
282/* label relocation processing */
283
1813e175 284static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
bec16311 285 TCGLabel *l, intptr_t addend)
c896fe29 286{
7ecd02a0 287 TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
c896fe29 288
7ecd02a0
RH
289 r->type = type;
290 r->ptr = code_ptr;
291 r->addend = addend;
292 QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
c896fe29
FB
293}
294
bec16311 295static void tcg_out_label(TCGContext *s, TCGLabel *l, tcg_insn_unit *ptr)
c896fe29 296{
eabb7b91 297 tcg_debug_assert(!l->has_value);
c896fe29 298 l->has_value = 1;
1813e175 299 l->u.value_ptr = ptr;
c896fe29
FB
300}
301
42a268c2 302TCGLabel *gen_new_label(void)
c896fe29 303{
b1311c4a 304 TCGContext *s = tcg_ctx;
51e3972c 305 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
c896fe29 306
7ecd02a0
RH
307 memset(l, 0, sizeof(TCGLabel));
308 l->id = s->nb_labels++;
309 QSIMPLEQ_INIT(&l->relocs);
310
bef16ab4 311 QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
42a268c2
RH
312
313 return l;
c896fe29
FB
314}
315
7ecd02a0
RH
316static bool tcg_resolve_relocs(TCGContext *s)
317{
318 TCGLabel *l;
319
320 QSIMPLEQ_FOREACH(l, &s->labels, next) {
321 TCGRelocation *r;
322 uintptr_t value = l->u.value;
323
324 QSIMPLEQ_FOREACH(r, &l->relocs, next) {
325 if (!patch_reloc(r->ptr, r->type, value, r->addend)) {
326 return false;
327 }
328 }
329 }
330 return true;
331}
332
9f754620
RH
333static void set_jmp_reset_offset(TCGContext *s, int which)
334{
335 size_t off = tcg_current_code_size(s);
336 s->tb_jmp_reset_offset[which] = off;
337 /* Make sure that we didn't overflow the stored offset. */
338 assert(s->tb_jmp_reset_offset[which] == off);
339}
340
ce151109 341#include "tcg-target.inc.c"
c896fe29 342
be2cdc5e
EC
343/* compare a pointer @ptr and a tb_tc @s */
344static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s)
345{
346 if (ptr >= s->ptr + s->size) {
347 return 1;
348 } else if (ptr < s->ptr) {
349 return -1;
350 }
351 return 0;
352}
353
354static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp)
355{
356 const struct tb_tc *a = ap;
357 const struct tb_tc *b = bp;
358
359 /*
360 * When both sizes are set, we know this isn't a lookup.
361 * This is the most likely case: every TB must be inserted; lookups
362 * are a lot less frequent.
363 */
364 if (likely(a->size && b->size)) {
365 if (a->ptr > b->ptr) {
366 return 1;
367 } else if (a->ptr < b->ptr) {
368 return -1;
369 }
370 /* a->ptr == b->ptr should happen only on deletions */
371 g_assert(a->size == b->size);
372 return 0;
373 }
374 /*
375 * All lookups have either .size field set to 0.
376 * From the glib sources we see that @ap is always the lookup key. However
377 * the docs provide no guarantee, so we just mark this case as likely.
378 */
379 if (likely(a->size == 0)) {
380 return ptr_cmp_tb_tc(a->ptr, b);
381 }
382 return ptr_cmp_tb_tc(b->ptr, a);
383}
384
385static void tcg_region_trees_init(void)
386{
387 size_t i;
388
389 tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize);
390 region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size);
391 for (i = 0; i < region.n; i++) {
392 struct tcg_region_tree *rt = region_trees + i * tree_size;
393
394 qemu_mutex_init(&rt->lock);
395 rt->tree = g_tree_new(tb_tc_cmp);
396 }
397}
398
399static struct tcg_region_tree *tc_ptr_to_region_tree(void *p)
400{
401 size_t region_idx;
402
403 if (p < region.start_aligned) {
404 region_idx = 0;
405 } else {
406 ptrdiff_t offset = p - region.start_aligned;
407
408 if (offset > region.stride * (region.n - 1)) {
409 region_idx = region.n - 1;
410 } else {
411 region_idx = offset / region.stride;
412 }
413 }
414 return region_trees + region_idx * tree_size;
415}
416
417void tcg_tb_insert(TranslationBlock *tb)
418{
419 struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
420
421 qemu_mutex_lock(&rt->lock);
422 g_tree_insert(rt->tree, &tb->tc, tb);
423 qemu_mutex_unlock(&rt->lock);
424}
425
426void tcg_tb_remove(TranslationBlock *tb)
427{
428 struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
429
430 qemu_mutex_lock(&rt->lock);
431 g_tree_remove(rt->tree, &tb->tc);
432 qemu_mutex_unlock(&rt->lock);
433}
434
435/*
436 * Find the TB 'tb' such that
437 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
438 * Return NULL if not found.
439 */
440TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr)
441{
442 struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr);
443 TranslationBlock *tb;
444 struct tb_tc s = { .ptr = (void *)tc_ptr };
445
446 qemu_mutex_lock(&rt->lock);
447 tb = g_tree_lookup(rt->tree, &s);
448 qemu_mutex_unlock(&rt->lock);
449 return tb;
450}
451
452static void tcg_region_tree_lock_all(void)
453{
454 size_t i;
455
456 for (i = 0; i < region.n; i++) {
457 struct tcg_region_tree *rt = region_trees + i * tree_size;
458
459 qemu_mutex_lock(&rt->lock);
460 }
461}
462
463static void tcg_region_tree_unlock_all(void)
464{
465 size_t i;
466
467 for (i = 0; i < region.n; i++) {
468 struct tcg_region_tree *rt = region_trees + i * tree_size;
469
470 qemu_mutex_unlock(&rt->lock);
471 }
472}
473
474void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)
475{
476 size_t i;
477
478 tcg_region_tree_lock_all();
479 for (i = 0; i < region.n; i++) {
480 struct tcg_region_tree *rt = region_trees + i * tree_size;
481
482 g_tree_foreach(rt->tree, func, user_data);
483 }
484 tcg_region_tree_unlock_all();
485}
486
487size_t tcg_nb_tbs(void)
488{
489 size_t nb_tbs = 0;
490 size_t i;
491
492 tcg_region_tree_lock_all();
493 for (i = 0; i < region.n; i++) {
494 struct tcg_region_tree *rt = region_trees + i * tree_size;
495
496 nb_tbs += g_tree_nnodes(rt->tree);
497 }
498 tcg_region_tree_unlock_all();
499 return nb_tbs;
500}
501
502static void tcg_region_tree_reset_all(void)
503{
504 size_t i;
505
506 tcg_region_tree_lock_all();
507 for (i = 0; i < region.n; i++) {
508 struct tcg_region_tree *rt = region_trees + i * tree_size;
509
510 /* Increment the refcount first so that destroy acts as a reset */
511 g_tree_ref(rt->tree);
512 g_tree_destroy(rt->tree);
513 }
514 tcg_region_tree_unlock_all();
515}
516
e8feb96f
EC
517static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
518{
519 void *start, *end;
520
521 start = region.start_aligned + curr_region * region.stride;
522 end = start + region.size;
523
524 if (curr_region == 0) {
525 start = region.start;
526 }
527 if (curr_region == region.n - 1) {
528 end = region.end;
529 }
530
531 *pstart = start;
532 *pend = end;
533}
534
535static void tcg_region_assign(TCGContext *s, size_t curr_region)
536{
537 void *start, *end;
538
539 tcg_region_bounds(curr_region, &start, &end);
540
541 s->code_gen_buffer = start;
542 s->code_gen_ptr = start;
543 s->code_gen_buffer_size = end - start;
544 s->code_gen_highwater = end - TCG_HIGHWATER;
545}
546
547static bool tcg_region_alloc__locked(TCGContext *s)
548{
549 if (region.current == region.n) {
550 return true;
551 }
552 tcg_region_assign(s, region.current);
553 region.current++;
554 return false;
555}
556
557/*
558 * Request a new region once the one in use has filled up.
559 * Returns true on error.
560 */
561static bool tcg_region_alloc(TCGContext *s)
562{
563 bool err;
564 /* read the region size now; alloc__locked will overwrite it on success */
565 size_t size_full = s->code_gen_buffer_size;
566
567 qemu_mutex_lock(&region.lock);
568 err = tcg_region_alloc__locked(s);
569 if (!err) {
570 region.agg_size_full += size_full - TCG_HIGHWATER;
571 }
572 qemu_mutex_unlock(&region.lock);
573 return err;
574}
575
576/*
577 * Perform a context's first region allocation.
578 * This function does _not_ increment region.agg_size_full.
579 */
580static inline bool tcg_region_initial_alloc__locked(TCGContext *s)
581{
582 return tcg_region_alloc__locked(s);
583}
584
585/* Call from a safe-work context */
586void tcg_region_reset_all(void)
587{
3468b59e 588 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
e8feb96f
EC
589 unsigned int i;
590
591 qemu_mutex_lock(&region.lock);
592 region.current = 0;
593 region.agg_size_full = 0;
594
3468b59e
EC
595 for (i = 0; i < n_ctxs; i++) {
596 TCGContext *s = atomic_read(&tcg_ctxs[i]);
597 bool err = tcg_region_initial_alloc__locked(s);
e8feb96f
EC
598
599 g_assert(!err);
600 }
601 qemu_mutex_unlock(&region.lock);
be2cdc5e
EC
602
603 tcg_region_tree_reset_all();
e8feb96f
EC
604}
605
3468b59e
EC
606#ifdef CONFIG_USER_ONLY
607static size_t tcg_n_regions(void)
608{
609 return 1;
610}
611#else
612/*
613 * It is likely that some vCPUs will translate more code than others, so we
614 * first try to set more regions than max_cpus, with those regions being of
615 * reasonable size. If that's not possible we make do by evenly dividing
616 * the code_gen_buffer among the vCPUs.
617 */
618static size_t tcg_n_regions(void)
619{
620 size_t i;
621
622 /* Use a single region if all we have is one vCPU thread */
623 if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
624 return 1;
625 }
626
627 /* Try to have more regions than max_cpus, with each region being >= 2 MB */
628 for (i = 8; i > 0; i--) {
629 size_t regions_per_thread = i;
630 size_t region_size;
631
632 region_size = tcg_init_ctx.code_gen_buffer_size;
633 region_size /= max_cpus * regions_per_thread;
634
635 if (region_size >= 2 * 1024u * 1024) {
636 return max_cpus * regions_per_thread;
637 }
638 }
639 /* If we can't, then just allocate one region per vCPU thread */
640 return max_cpus;
641}
642#endif
643
e8feb96f
EC
644/*
645 * Initializes region partitioning.
646 *
647 * Called at init time from the parent thread (i.e. the one calling
648 * tcg_context_init), after the target's TCG globals have been set.
3468b59e
EC
649 *
650 * Region partitioning works by splitting code_gen_buffer into separate regions,
651 * and then assigning regions to TCG threads so that the threads can translate
652 * code in parallel without synchronization.
653 *
654 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
655 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
656 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
657 * must have been parsed before calling this function, since it calls
658 * qemu_tcg_mttcg_enabled().
659 *
660 * In user-mode we use a single region. Having multiple regions in user-mode
661 * is not supported, because the number of vCPU threads (recall that each thread
662 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
663 * OS, and usually this number is huge (tens of thousands is not uncommon).
664 * Thus, given this large bound on the number of vCPU threads and the fact
665 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
666 * that the availability of at least one region per vCPU thread.
667 *
668 * However, this user-mode limitation is unlikely to be a significant problem
669 * in practice. Multi-threaded guests share most if not all of their translated
670 * code, which makes parallel code generation less appealing than in softmmu.
e8feb96f
EC
671 */
672void tcg_region_init(void)
673{
674 void *buf = tcg_init_ctx.code_gen_buffer;
675 void *aligned;
676 size_t size = tcg_init_ctx.code_gen_buffer_size;
677 size_t page_size = qemu_real_host_page_size;
678 size_t region_size;
679 size_t n_regions;
680 size_t i;
681
3468b59e 682 n_regions = tcg_n_regions();
e8feb96f
EC
683
684 /* The first region will be 'aligned - buf' bytes larger than the others */
685 aligned = QEMU_ALIGN_PTR_UP(buf, page_size);
686 g_assert(aligned < tcg_init_ctx.code_gen_buffer + size);
687 /*
688 * Make region_size a multiple of page_size, using aligned as the start.
689 * As a result of this we might end up with a few extra pages at the end of
690 * the buffer; we will assign those to the last region.
691 */
692 region_size = (size - (aligned - buf)) / n_regions;
693 region_size = QEMU_ALIGN_DOWN(region_size, page_size);
694
695 /* A region must have at least 2 pages; one code, one guard */
696 g_assert(region_size >= 2 * page_size);
697
698 /* init the region struct */
699 qemu_mutex_init(&region.lock);
700 region.n = n_regions;
701 region.size = region_size - page_size;
702 region.stride = region_size;
703 region.start = buf;
704 region.start_aligned = aligned;
705 /* page-align the end, since its last page will be a guard page */
706 region.end = QEMU_ALIGN_PTR_DOWN(buf + size, page_size);
707 /* account for that last guard page */
708 region.end -= page_size;
709
710 /* set guard pages */
711 for (i = 0; i < region.n; i++) {
712 void *start, *end;
713 int rc;
714
715 tcg_region_bounds(i, &start, &end);
716 rc = qemu_mprotect_none(end, page_size);
717 g_assert(!rc);
718 }
719
be2cdc5e
EC
720 tcg_region_trees_init();
721
3468b59e
EC
722 /* In user-mode we support only one ctx, so do the initial allocation now */
723#ifdef CONFIG_USER_ONLY
e8feb96f
EC
724 {
725 bool err = tcg_region_initial_alloc__locked(tcg_ctx);
726
727 g_assert(!err);
728 }
3468b59e
EC
729#endif
730}
731
732/*
733 * All TCG threads except the parent (i.e. the one that called tcg_context_init
734 * and registered the target's TCG globals) must register with this function
735 * before initiating translation.
736 *
737 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
738 * of tcg_region_init() for the reasoning behind this.
739 *
740 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
741 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
742 * is not used anymore for translation once this function is called.
743 *
744 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
745 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
746 */
747#ifdef CONFIG_USER_ONLY
748void tcg_register_thread(void)
749{
750 tcg_ctx = &tcg_init_ctx;
751}
752#else
753void tcg_register_thread(void)
754{
755 TCGContext *s = g_malloc(sizeof(*s));
756 unsigned int i, n;
757 bool err;
758
759 *s = tcg_init_ctx;
760
761 /* Relink mem_base. */
762 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
763 if (tcg_init_ctx.temps[i].mem_base) {
764 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
765 tcg_debug_assert(b >= 0 && b < n);
766 s->temps[i].mem_base = &s->temps[b];
767 }
768 }
769
770 /* Claim an entry in tcg_ctxs */
771 n = atomic_fetch_inc(&n_tcg_ctxs);
772 g_assert(n < max_cpus);
773 atomic_set(&tcg_ctxs[n], s);
774
775 tcg_ctx = s;
776 qemu_mutex_lock(&region.lock);
777 err = tcg_region_initial_alloc__locked(tcg_ctx);
778 g_assert(!err);
779 qemu_mutex_unlock(&region.lock);
e8feb96f 780}
3468b59e 781#endif /* !CONFIG_USER_ONLY */
e8feb96f
EC
782
783/*
784 * Returns the size (in bytes) of all translated code (i.e. from all regions)
785 * currently in the cache.
786 * See also: tcg_code_capacity()
787 * Do not confuse with tcg_current_code_size(); that one applies to a single
788 * TCG context.
789 */
790size_t tcg_code_size(void)
791{
3468b59e 792 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
e8feb96f
EC
793 unsigned int i;
794 size_t total;
795
796 qemu_mutex_lock(&region.lock);
797 total = region.agg_size_full;
3468b59e
EC
798 for (i = 0; i < n_ctxs; i++) {
799 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
e8feb96f
EC
800 size_t size;
801
802 size = atomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
803 g_assert(size <= s->code_gen_buffer_size);
804 total += size;
805 }
806 qemu_mutex_unlock(&region.lock);
807 return total;
808}
809
810/*
811 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
812 * regions.
813 * See also: tcg_code_size()
814 */
815size_t tcg_code_capacity(void)
816{
817 size_t guard_size, capacity;
818
819 /* no need for synchronization; these variables are set at init time */
820 guard_size = region.stride - region.size;
821 capacity = region.end + guard_size - region.start;
822 capacity -= region.n * (guard_size + TCG_HIGHWATER);
823 return capacity;
824}
825
128ed227
EC
826size_t tcg_tb_phys_invalidate_count(void)
827{
828 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
829 unsigned int i;
830 size_t total = 0;
831
832 for (i = 0; i < n_ctxs; i++) {
833 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
834
835 total += atomic_read(&s->tb_phys_invalidate_count);
836 }
837 return total;
838}
839
c896fe29
FB
840/* pool based memory allocation */
841void *tcg_malloc_internal(TCGContext *s, int size)
842{
843 TCGPool *p;
844 int pool_size;
845
846 if (size > TCG_POOL_CHUNK_SIZE) {
847 /* big malloc: insert a new pool (XXX: could optimize) */
7267c094 848 p = g_malloc(sizeof(TCGPool) + size);
c896fe29 849 p->size = size;
4055299e
KB
850 p->next = s->pool_first_large;
851 s->pool_first_large = p;
852 return p->data;
c896fe29
FB
853 } else {
854 p = s->pool_current;
855 if (!p) {
856 p = s->pool_first;
857 if (!p)
858 goto new_pool;
859 } else {
860 if (!p->next) {
861 new_pool:
862 pool_size = TCG_POOL_CHUNK_SIZE;
7267c094 863 p = g_malloc(sizeof(TCGPool) + pool_size);
c896fe29
FB
864 p->size = pool_size;
865 p->next = NULL;
866 if (s->pool_current)
867 s->pool_current->next = p;
868 else
869 s->pool_first = p;
870 } else {
871 p = p->next;
872 }
873 }
874 }
875 s->pool_current = p;
876 s->pool_cur = p->data + size;
877 s->pool_end = p->data + p->size;
878 return p->data;
879}
880
881void tcg_pool_reset(TCGContext *s)
882{
4055299e
KB
883 TCGPool *p, *t;
884 for (p = s->pool_first_large; p; p = t) {
885 t = p->next;
886 g_free(p);
887 }
888 s->pool_first_large = NULL;
c896fe29
FB
889 s->pool_cur = s->pool_end = NULL;
890 s->pool_current = NULL;
891}
892
100b5e01
RH
893typedef struct TCGHelperInfo {
894 void *func;
895 const char *name;
afb49896
RH
896 unsigned flags;
897 unsigned sizemask;
100b5e01
RH
898} TCGHelperInfo;
899
2ef6175a
RH
900#include "exec/helper-proto.h"
901
100b5e01 902static const TCGHelperInfo all_helpers[] = {
2ef6175a 903#include "exec/helper-tcg.h"
100b5e01 904};
619205fd 905static GHashTable *helper_table;
100b5e01 906
91478cef 907static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
f69d277e 908static void process_op_defs(TCGContext *s);
1c2adb95
RH
909static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
910 TCGReg reg, const char *name);
91478cef 911
c896fe29
FB
912void tcg_context_init(TCGContext *s)
913{
100b5e01 914 int op, total_args, n, i;
c896fe29
FB
915 TCGOpDef *def;
916 TCGArgConstraint *args_ct;
917 int *sorted_args;
1c2adb95 918 TCGTemp *ts;
c896fe29
FB
919
920 memset(s, 0, sizeof(*s));
c896fe29 921 s->nb_globals = 0;
c70fbf0a 922
c896fe29
FB
923 /* Count total number of arguments and allocate the corresponding
924 space */
925 total_args = 0;
926 for(op = 0; op < NB_OPS; op++) {
927 def = &tcg_op_defs[op];
928 n = def->nb_iargs + def->nb_oargs;
929 total_args += n;
930 }
931
7267c094
AL
932 args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
933 sorted_args = g_malloc(sizeof(int) * total_args);
c896fe29
FB
934
935 for(op = 0; op < NB_OPS; op++) {
936 def = &tcg_op_defs[op];
937 def->args_ct = args_ct;
938 def->sorted_args = sorted_args;
939 n = def->nb_iargs + def->nb_oargs;
940 sorted_args += n;
941 args_ct += n;
942 }
5cd8f621
RH
943
944 /* Register helpers. */
84fd9dd3 945 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
619205fd 946 helper_table = g_hash_table_new(NULL, NULL);
84fd9dd3 947
100b5e01 948 for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
84fd9dd3 949 g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
72866e82 950 (gpointer)&all_helpers[i]);
100b5e01 951 }
5cd8f621 952
c896fe29 953 tcg_target_init(s);
f69d277e 954 process_op_defs(s);
91478cef
RH
955
956 /* Reverse the order of the saved registers, assuming they're all at
957 the start of tcg_target_reg_alloc_order. */
958 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
959 int r = tcg_target_reg_alloc_order[n];
960 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
961 break;
962 }
963 }
964 for (i = 0; i < n; ++i) {
965 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
966 }
967 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
968 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
969 }
b1311c4a
EC
970
971 tcg_ctx = s;
3468b59e
EC
972 /*
973 * In user-mode we simply share the init context among threads, since we
974 * use a single region. See the documentation tcg_region_init() for the
975 * reasoning behind this.
976 * In softmmu we will have at most max_cpus TCG threads.
977 */
978#ifdef CONFIG_USER_ONLY
df2cce29
EC
979 tcg_ctxs = &tcg_ctx;
980 n_tcg_ctxs = 1;
3468b59e
EC
981#else
982 tcg_ctxs = g_new(TCGContext *, max_cpus);
983#endif
1c2adb95
RH
984
985 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
986 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
987 cpu_env = temp_tcgv_ptr(ts);
9002ec79 988}
b03cce8e 989
6e3b2bfd
EC
990/*
991 * Allocate TBs right before their corresponding translated code, making
992 * sure that TBs and code are on different cache lines.
993 */
994TranslationBlock *tcg_tb_alloc(TCGContext *s)
995{
996 uintptr_t align = qemu_icache_linesize;
997 TranslationBlock *tb;
998 void *next;
999
e8feb96f 1000 retry:
6e3b2bfd
EC
1001 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
1002 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
1003
1004 if (unlikely(next > s->code_gen_highwater)) {
e8feb96f
EC
1005 if (tcg_region_alloc(s)) {
1006 return NULL;
1007 }
1008 goto retry;
6e3b2bfd 1009 }
e8feb96f 1010 atomic_set(&s->code_gen_ptr, next);
57a26946 1011 s->data_gen_ptr = NULL;
6e3b2bfd
EC
1012 return tb;
1013}
1014
9002ec79
RH
1015void tcg_prologue_init(TCGContext *s)
1016{
8163b749
RH
1017 size_t prologue_size, total_size;
1018 void *buf0, *buf1;
1019
1020 /* Put the prologue at the beginning of code_gen_buffer. */
1021 buf0 = s->code_gen_buffer;
5b38ee31 1022 total_size = s->code_gen_buffer_size;
8163b749
RH
1023 s->code_ptr = buf0;
1024 s->code_buf = buf0;
5b38ee31 1025 s->data_gen_ptr = NULL;
8163b749
RH
1026 s->code_gen_prologue = buf0;
1027
5b38ee31
RH
1028 /* Compute a high-water mark, at which we voluntarily flush the buffer
1029 and start over. The size here is arbitrary, significantly larger
1030 than we expect the code generation for any one opcode to require. */
1031 s->code_gen_highwater = s->code_gen_buffer + (total_size - TCG_HIGHWATER);
1032
1033#ifdef TCG_TARGET_NEED_POOL_LABELS
1034 s->pool_labels = NULL;
1035#endif
1036
8163b749 1037 /* Generate the prologue. */
b03cce8e 1038 tcg_target_qemu_prologue(s);
5b38ee31
RH
1039
1040#ifdef TCG_TARGET_NEED_POOL_LABELS
1041 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1042 {
1768987b
RH
1043 int result = tcg_out_pool_finalize(s);
1044 tcg_debug_assert(result == 0);
5b38ee31
RH
1045 }
1046#endif
1047
8163b749
RH
1048 buf1 = s->code_ptr;
1049 flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
1050
1051 /* Deduct the prologue from the buffer. */
1052 prologue_size = tcg_current_code_size(s);
1053 s->code_gen_ptr = buf1;
1054 s->code_gen_buffer = buf1;
1055 s->code_buf = buf1;
5b38ee31 1056 total_size -= prologue_size;
8163b749
RH
1057 s->code_gen_buffer_size = total_size;
1058
8163b749 1059 tcg_register_jit(s->code_gen_buffer, total_size);
d6b64b2b
RH
1060
1061#ifdef DEBUG_DISAS
1062 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
1ee73216 1063 qemu_log_lock();
8163b749 1064 qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
5b38ee31
RH
1065 if (s->data_gen_ptr) {
1066 size_t code_size = s->data_gen_ptr - buf0;
1067 size_t data_size = prologue_size - code_size;
1068 size_t i;
1069
1070 log_disas(buf0, code_size);
1071
1072 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1073 if (sizeof(tcg_target_ulong) == 8) {
1074 qemu_log("0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1075 (uintptr_t)s->data_gen_ptr + i,
1076 *(uint64_t *)(s->data_gen_ptr + i));
1077 } else {
1078 qemu_log("0x%08" PRIxPTR ": .long 0x%08x\n",
1079 (uintptr_t)s->data_gen_ptr + i,
1080 *(uint32_t *)(s->data_gen_ptr + i));
1081 }
1082 }
1083 } else {
1084 log_disas(buf0, prologue_size);
1085 }
d6b64b2b
RH
1086 qemu_log("\n");
1087 qemu_log_flush();
1ee73216 1088 qemu_log_unlock();
d6b64b2b
RH
1089 }
1090#endif
cedbcb01
EC
1091
1092 /* Assert that goto_ptr is implemented completely. */
1093 if (TCG_TARGET_HAS_goto_ptr) {
1094 tcg_debug_assert(s->code_gen_epilogue != NULL);
1095 }
c896fe29
FB
1096}
1097
c896fe29
FB
1098void tcg_func_start(TCGContext *s)
1099{
1100 tcg_pool_reset(s);
1101 s->nb_temps = s->nb_globals;
0ec9eabc
RH
1102
1103 /* No temps have been previously allocated for size or locality. */
1104 memset(s->free_temps, 0, sizeof(s->free_temps));
1105
abebf925 1106 s->nb_ops = 0;
c896fe29
FB
1107 s->nb_labels = 0;
1108 s->current_frame_offset = s->frame_start;
1109
0a209d4b
RH
1110#ifdef CONFIG_DEBUG_TCG
1111 s->goto_tb_issue_mask = 0;
1112#endif
1113
15fa08f8
RH
1114 QTAILQ_INIT(&s->ops);
1115 QTAILQ_INIT(&s->free_ops);
bef16ab4 1116 QSIMPLEQ_INIT(&s->labels);
c896fe29
FB
1117}
1118
7ca4b752
RH
1119static inline TCGTemp *tcg_temp_alloc(TCGContext *s)
1120{
1121 int n = s->nb_temps++;
1122 tcg_debug_assert(n < TCG_MAX_TEMPS);
1123 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1124}
1125
1126static inline TCGTemp *tcg_global_alloc(TCGContext *s)
1127{
fa477d25
RH
1128 TCGTemp *ts;
1129
7ca4b752
RH
1130 tcg_debug_assert(s->nb_globals == s->nb_temps);
1131 s->nb_globals++;
fa477d25
RH
1132 ts = tcg_temp_alloc(s);
1133 ts->temp_global = 1;
1134
1135 return ts;
c896fe29
FB
1136}
1137
085272b3
RH
1138static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1139 TCGReg reg, const char *name)
c896fe29 1140{
c896fe29 1141 TCGTemp *ts;
c896fe29 1142
b3a62939 1143 if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
c896fe29 1144 tcg_abort();
b3a62939 1145 }
7ca4b752
RH
1146
1147 ts = tcg_global_alloc(s);
c896fe29
FB
1148 ts->base_type = type;
1149 ts->type = type;
1150 ts->fixed_reg = 1;
1151 ts->reg = reg;
c896fe29 1152 ts->name = name;
c896fe29 1153 tcg_regset_set_reg(s->reserved_regs, reg);
7ca4b752 1154
085272b3 1155 return ts;
a7812ae4
PB
1156}
1157
b6638662 1158void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
b3a62939 1159{
b3a62939
RH
1160 s->frame_start = start;
1161 s->frame_end = start + size;
085272b3
RH
1162 s->frame_temp
1163 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
b3a62939
RH
1164}
1165
085272b3
RH
1166TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
1167 intptr_t offset, const char *name)
c896fe29 1168{
b1311c4a 1169 TCGContext *s = tcg_ctx;
dc41aa7d 1170 TCGTemp *base_ts = tcgv_ptr_temp(base);
7ca4b752 1171 TCGTemp *ts = tcg_global_alloc(s);
b3915dbb 1172 int indirect_reg = 0, bigendian = 0;
7ca4b752
RH
1173#ifdef HOST_WORDS_BIGENDIAN
1174 bigendian = 1;
1175#endif
c896fe29 1176
b3915dbb 1177 if (!base_ts->fixed_reg) {
5a18407f
RH
1178 /* We do not support double-indirect registers. */
1179 tcg_debug_assert(!base_ts->indirect_reg);
b3915dbb 1180 base_ts->indirect_base = 1;
5a18407f
RH
1181 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1182 ? 2 : 1);
1183 indirect_reg = 1;
b3915dbb
RH
1184 }
1185
7ca4b752
RH
1186 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1187 TCGTemp *ts2 = tcg_global_alloc(s);
c896fe29 1188 char buf[64];
7ca4b752
RH
1189
1190 ts->base_type = TCG_TYPE_I64;
c896fe29 1191 ts->type = TCG_TYPE_I32;
b3915dbb 1192 ts->indirect_reg = indirect_reg;
c896fe29 1193 ts->mem_allocated = 1;
b3a62939 1194 ts->mem_base = base_ts;
7ca4b752 1195 ts->mem_offset = offset + bigendian * 4;
c896fe29
FB
1196 pstrcpy(buf, sizeof(buf), name);
1197 pstrcat(buf, sizeof(buf), "_0");
1198 ts->name = strdup(buf);
c896fe29 1199
7ca4b752
RH
1200 tcg_debug_assert(ts2 == ts + 1);
1201 ts2->base_type = TCG_TYPE_I64;
1202 ts2->type = TCG_TYPE_I32;
b3915dbb 1203 ts2->indirect_reg = indirect_reg;
7ca4b752
RH
1204 ts2->mem_allocated = 1;
1205 ts2->mem_base = base_ts;
1206 ts2->mem_offset = offset + (1 - bigendian) * 4;
c896fe29
FB
1207 pstrcpy(buf, sizeof(buf), name);
1208 pstrcat(buf, sizeof(buf), "_1");
120c1084 1209 ts2->name = strdup(buf);
7ca4b752 1210 } else {
c896fe29
FB
1211 ts->base_type = type;
1212 ts->type = type;
b3915dbb 1213 ts->indirect_reg = indirect_reg;
c896fe29 1214 ts->mem_allocated = 1;
b3a62939 1215 ts->mem_base = base_ts;
c896fe29 1216 ts->mem_offset = offset;
c896fe29 1217 ts->name = name;
c896fe29 1218 }
085272b3 1219 return ts;
a7812ae4
PB
1220}
1221
5bfa8034 1222TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local)
c896fe29 1223{
b1311c4a 1224 TCGContext *s = tcg_ctx;
c896fe29 1225 TCGTemp *ts;
641d5fbe 1226 int idx, k;
c896fe29 1227
0ec9eabc
RH
1228 k = type + (temp_local ? TCG_TYPE_COUNT : 0);
1229 idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
1230 if (idx < TCG_MAX_TEMPS) {
1231 /* There is already an available temp with the right type. */
1232 clear_bit(idx, s->free_temps[k].l);
1233
e8996ee0 1234 ts = &s->temps[idx];
e8996ee0 1235 ts->temp_allocated = 1;
7ca4b752
RH
1236 tcg_debug_assert(ts->base_type == type);
1237 tcg_debug_assert(ts->temp_local == temp_local);
e8996ee0 1238 } else {
7ca4b752
RH
1239 ts = tcg_temp_alloc(s);
1240 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1241 TCGTemp *ts2 = tcg_temp_alloc(s);
1242
f6aa2f7d 1243 ts->base_type = type;
e8996ee0
FB
1244 ts->type = TCG_TYPE_I32;
1245 ts->temp_allocated = 1;
641d5fbe 1246 ts->temp_local = temp_local;
7ca4b752
RH
1247
1248 tcg_debug_assert(ts2 == ts + 1);
1249 ts2->base_type = TCG_TYPE_I64;
1250 ts2->type = TCG_TYPE_I32;
1251 ts2->temp_allocated = 1;
1252 ts2->temp_local = temp_local;
1253 } else {
e8996ee0
FB
1254 ts->base_type = type;
1255 ts->type = type;
1256 ts->temp_allocated = 1;
641d5fbe 1257 ts->temp_local = temp_local;
e8996ee0 1258 }
c896fe29 1259 }
27bfd83c
PM
1260
1261#if defined(CONFIG_DEBUG_TCG)
1262 s->temps_in_use++;
1263#endif
085272b3 1264 return ts;
c896fe29
FB
1265}
1266
d2fd745f
RH
1267TCGv_vec tcg_temp_new_vec(TCGType type)
1268{
1269 TCGTemp *t;
1270
1271#ifdef CONFIG_DEBUG_TCG
1272 switch (type) {
1273 case TCG_TYPE_V64:
1274 assert(TCG_TARGET_HAS_v64);
1275 break;
1276 case TCG_TYPE_V128:
1277 assert(TCG_TARGET_HAS_v128);
1278 break;
1279 case TCG_TYPE_V256:
1280 assert(TCG_TARGET_HAS_v256);
1281 break;
1282 default:
1283 g_assert_not_reached();
1284 }
1285#endif
1286
1287 t = tcg_temp_new_internal(type, 0);
1288 return temp_tcgv_vec(t);
1289}
1290
1291/* Create a new temp of the same type as an existing temp. */
1292TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1293{
1294 TCGTemp *t = tcgv_vec_temp(match);
1295
1296 tcg_debug_assert(t->temp_allocated != 0);
1297
1298 t = tcg_temp_new_internal(t->base_type, 0);
1299 return temp_tcgv_vec(t);
1300}
1301
5bfa8034 1302void tcg_temp_free_internal(TCGTemp *ts)
c896fe29 1303{
b1311c4a 1304 TCGContext *s = tcg_ctx;
085272b3 1305 int k, idx;
c896fe29 1306
27bfd83c
PM
1307#if defined(CONFIG_DEBUG_TCG)
1308 s->temps_in_use--;
1309 if (s->temps_in_use < 0) {
1310 fprintf(stderr, "More temporaries freed than allocated!\n");
1311 }
1312#endif
1313
085272b3 1314 tcg_debug_assert(ts->temp_global == 0);
eabb7b91 1315 tcg_debug_assert(ts->temp_allocated != 0);
e8996ee0 1316 ts->temp_allocated = 0;
0ec9eabc 1317
085272b3 1318 idx = temp_idx(ts);
18d13fa2 1319 k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
0ec9eabc 1320 set_bit(idx, s->free_temps[k].l);
c896fe29
FB
1321}
1322
a7812ae4 1323TCGv_i32 tcg_const_i32(int32_t val)
c896fe29 1324{
a7812ae4
PB
1325 TCGv_i32 t0;
1326 t0 = tcg_temp_new_i32();
e8996ee0
FB
1327 tcg_gen_movi_i32(t0, val);
1328 return t0;
1329}
c896fe29 1330
a7812ae4 1331TCGv_i64 tcg_const_i64(int64_t val)
e8996ee0 1332{
a7812ae4
PB
1333 TCGv_i64 t0;
1334 t0 = tcg_temp_new_i64();
e8996ee0
FB
1335 tcg_gen_movi_i64(t0, val);
1336 return t0;
c896fe29
FB
1337}
1338
a7812ae4 1339TCGv_i32 tcg_const_local_i32(int32_t val)
bdffd4a9 1340{
a7812ae4
PB
1341 TCGv_i32 t0;
1342 t0 = tcg_temp_local_new_i32();
bdffd4a9
AJ
1343 tcg_gen_movi_i32(t0, val);
1344 return t0;
1345}
1346
a7812ae4 1347TCGv_i64 tcg_const_local_i64(int64_t val)
bdffd4a9 1348{
a7812ae4
PB
1349 TCGv_i64 t0;
1350 t0 = tcg_temp_local_new_i64();
bdffd4a9
AJ
1351 tcg_gen_movi_i64(t0, val);
1352 return t0;
1353}
1354
27bfd83c
PM
1355#if defined(CONFIG_DEBUG_TCG)
1356void tcg_clear_temp_count(void)
1357{
b1311c4a 1358 TCGContext *s = tcg_ctx;
27bfd83c
PM
1359 s->temps_in_use = 0;
1360}
1361
1362int tcg_check_temp_count(void)
1363{
b1311c4a 1364 TCGContext *s = tcg_ctx;
27bfd83c
PM
1365 if (s->temps_in_use) {
1366 /* Clear the count so that we don't give another
1367 * warning immediately next time around.
1368 */
1369 s->temps_in_use = 0;
1370 return 1;
1371 }
1372 return 0;
1373}
1374#endif
1375
be0f34b5
RH
1376/* Return true if OP may appear in the opcode stream.
1377 Test the runtime variable that controls each opcode. */
1378bool tcg_op_supported(TCGOpcode op)
1379{
d2fd745f
RH
1380 const bool have_vec
1381 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1382
be0f34b5
RH
1383 switch (op) {
1384 case INDEX_op_discard:
1385 case INDEX_op_set_label:
1386 case INDEX_op_call:
1387 case INDEX_op_br:
1388 case INDEX_op_mb:
1389 case INDEX_op_insn_start:
1390 case INDEX_op_exit_tb:
1391 case INDEX_op_goto_tb:
1392 case INDEX_op_qemu_ld_i32:
1393 case INDEX_op_qemu_st_i32:
1394 case INDEX_op_qemu_ld_i64:
1395 case INDEX_op_qemu_st_i64:
1396 return true;
1397
1398 case INDEX_op_goto_ptr:
1399 return TCG_TARGET_HAS_goto_ptr;
1400
1401 case INDEX_op_mov_i32:
1402 case INDEX_op_movi_i32:
1403 case INDEX_op_setcond_i32:
1404 case INDEX_op_brcond_i32:
1405 case INDEX_op_ld8u_i32:
1406 case INDEX_op_ld8s_i32:
1407 case INDEX_op_ld16u_i32:
1408 case INDEX_op_ld16s_i32:
1409 case INDEX_op_ld_i32:
1410 case INDEX_op_st8_i32:
1411 case INDEX_op_st16_i32:
1412 case INDEX_op_st_i32:
1413 case INDEX_op_add_i32:
1414 case INDEX_op_sub_i32:
1415 case INDEX_op_mul_i32:
1416 case INDEX_op_and_i32:
1417 case INDEX_op_or_i32:
1418 case INDEX_op_xor_i32:
1419 case INDEX_op_shl_i32:
1420 case INDEX_op_shr_i32:
1421 case INDEX_op_sar_i32:
1422 return true;
1423
1424 case INDEX_op_movcond_i32:
1425 return TCG_TARGET_HAS_movcond_i32;
1426 case INDEX_op_div_i32:
1427 case INDEX_op_divu_i32:
1428 return TCG_TARGET_HAS_div_i32;
1429 case INDEX_op_rem_i32:
1430 case INDEX_op_remu_i32:
1431 return TCG_TARGET_HAS_rem_i32;
1432 case INDEX_op_div2_i32:
1433 case INDEX_op_divu2_i32:
1434 return TCG_TARGET_HAS_div2_i32;
1435 case INDEX_op_rotl_i32:
1436 case INDEX_op_rotr_i32:
1437 return TCG_TARGET_HAS_rot_i32;
1438 case INDEX_op_deposit_i32:
1439 return TCG_TARGET_HAS_deposit_i32;
1440 case INDEX_op_extract_i32:
1441 return TCG_TARGET_HAS_extract_i32;
1442 case INDEX_op_sextract_i32:
1443 return TCG_TARGET_HAS_sextract_i32;
fce1296f
RH
1444 case INDEX_op_extract2_i32:
1445 return TCG_TARGET_HAS_extract2_i32;
be0f34b5
RH
1446 case INDEX_op_add2_i32:
1447 return TCG_TARGET_HAS_add2_i32;
1448 case INDEX_op_sub2_i32:
1449 return TCG_TARGET_HAS_sub2_i32;
1450 case INDEX_op_mulu2_i32:
1451 return TCG_TARGET_HAS_mulu2_i32;
1452 case INDEX_op_muls2_i32:
1453 return TCG_TARGET_HAS_muls2_i32;
1454 case INDEX_op_muluh_i32:
1455 return TCG_TARGET_HAS_muluh_i32;
1456 case INDEX_op_mulsh_i32:
1457 return TCG_TARGET_HAS_mulsh_i32;
1458 case INDEX_op_ext8s_i32:
1459 return TCG_TARGET_HAS_ext8s_i32;
1460 case INDEX_op_ext16s_i32:
1461 return TCG_TARGET_HAS_ext16s_i32;
1462 case INDEX_op_ext8u_i32:
1463 return TCG_TARGET_HAS_ext8u_i32;
1464 case INDEX_op_ext16u_i32:
1465 return TCG_TARGET_HAS_ext16u_i32;
1466 case INDEX_op_bswap16_i32:
1467 return TCG_TARGET_HAS_bswap16_i32;
1468 case INDEX_op_bswap32_i32:
1469 return TCG_TARGET_HAS_bswap32_i32;
1470 case INDEX_op_not_i32:
1471 return TCG_TARGET_HAS_not_i32;
1472 case INDEX_op_neg_i32:
1473 return TCG_TARGET_HAS_neg_i32;
1474 case INDEX_op_andc_i32:
1475 return TCG_TARGET_HAS_andc_i32;
1476 case INDEX_op_orc_i32:
1477 return TCG_TARGET_HAS_orc_i32;
1478 case INDEX_op_eqv_i32:
1479 return TCG_TARGET_HAS_eqv_i32;
1480 case INDEX_op_nand_i32:
1481 return TCG_TARGET_HAS_nand_i32;
1482 case INDEX_op_nor_i32:
1483 return TCG_TARGET_HAS_nor_i32;
1484 case INDEX_op_clz_i32:
1485 return TCG_TARGET_HAS_clz_i32;
1486 case INDEX_op_ctz_i32:
1487 return TCG_TARGET_HAS_ctz_i32;
1488 case INDEX_op_ctpop_i32:
1489 return TCG_TARGET_HAS_ctpop_i32;
1490
1491 case INDEX_op_brcond2_i32:
1492 case INDEX_op_setcond2_i32:
1493 return TCG_TARGET_REG_BITS == 32;
1494
1495 case INDEX_op_mov_i64:
1496 case INDEX_op_movi_i64:
1497 case INDEX_op_setcond_i64:
1498 case INDEX_op_brcond_i64:
1499 case INDEX_op_ld8u_i64:
1500 case INDEX_op_ld8s_i64:
1501 case INDEX_op_ld16u_i64:
1502 case INDEX_op_ld16s_i64:
1503 case INDEX_op_ld32u_i64:
1504 case INDEX_op_ld32s_i64:
1505 case INDEX_op_ld_i64:
1506 case INDEX_op_st8_i64:
1507 case INDEX_op_st16_i64:
1508 case INDEX_op_st32_i64:
1509 case INDEX_op_st_i64:
1510 case INDEX_op_add_i64:
1511 case INDEX_op_sub_i64:
1512 case INDEX_op_mul_i64:
1513 case INDEX_op_and_i64:
1514 case INDEX_op_or_i64:
1515 case INDEX_op_xor_i64:
1516 case INDEX_op_shl_i64:
1517 case INDEX_op_shr_i64:
1518 case INDEX_op_sar_i64:
1519 case INDEX_op_ext_i32_i64:
1520 case INDEX_op_extu_i32_i64:
1521 return TCG_TARGET_REG_BITS == 64;
1522
1523 case INDEX_op_movcond_i64:
1524 return TCG_TARGET_HAS_movcond_i64;
1525 case INDEX_op_div_i64:
1526 case INDEX_op_divu_i64:
1527 return TCG_TARGET_HAS_div_i64;
1528 case INDEX_op_rem_i64:
1529 case INDEX_op_remu_i64:
1530 return TCG_TARGET_HAS_rem_i64;
1531 case INDEX_op_div2_i64:
1532 case INDEX_op_divu2_i64:
1533 return TCG_TARGET_HAS_div2_i64;
1534 case INDEX_op_rotl_i64:
1535 case INDEX_op_rotr_i64:
1536 return TCG_TARGET_HAS_rot_i64;
1537 case INDEX_op_deposit_i64:
1538 return TCG_TARGET_HAS_deposit_i64;
1539 case INDEX_op_extract_i64:
1540 return TCG_TARGET_HAS_extract_i64;
1541 case INDEX_op_sextract_i64:
1542 return TCG_TARGET_HAS_sextract_i64;
fce1296f
RH
1543 case INDEX_op_extract2_i64:
1544 return TCG_TARGET_HAS_extract2_i64;
be0f34b5
RH
1545 case INDEX_op_extrl_i64_i32:
1546 return TCG_TARGET_HAS_extrl_i64_i32;
1547 case INDEX_op_extrh_i64_i32:
1548 return TCG_TARGET_HAS_extrh_i64_i32;
1549 case INDEX_op_ext8s_i64:
1550 return TCG_TARGET_HAS_ext8s_i64;
1551 case INDEX_op_ext16s_i64:
1552 return TCG_TARGET_HAS_ext16s_i64;
1553 case INDEX_op_ext32s_i64:
1554 return TCG_TARGET_HAS_ext32s_i64;
1555 case INDEX_op_ext8u_i64:
1556 return TCG_TARGET_HAS_ext8u_i64;
1557 case INDEX_op_ext16u_i64:
1558 return TCG_TARGET_HAS_ext16u_i64;
1559 case INDEX_op_ext32u_i64:
1560 return TCG_TARGET_HAS_ext32u_i64;
1561 case INDEX_op_bswap16_i64:
1562 return TCG_TARGET_HAS_bswap16_i64;
1563 case INDEX_op_bswap32_i64:
1564 return TCG_TARGET_HAS_bswap32_i64;
1565 case INDEX_op_bswap64_i64:
1566 return TCG_TARGET_HAS_bswap64_i64;
1567 case INDEX_op_not_i64:
1568 return TCG_TARGET_HAS_not_i64;
1569 case INDEX_op_neg_i64:
1570 return TCG_TARGET_HAS_neg_i64;
1571 case INDEX_op_andc_i64:
1572 return TCG_TARGET_HAS_andc_i64;
1573 case INDEX_op_orc_i64:
1574 return TCG_TARGET_HAS_orc_i64;
1575 case INDEX_op_eqv_i64:
1576 return TCG_TARGET_HAS_eqv_i64;
1577 case INDEX_op_nand_i64:
1578 return TCG_TARGET_HAS_nand_i64;
1579 case INDEX_op_nor_i64:
1580 return TCG_TARGET_HAS_nor_i64;
1581 case INDEX_op_clz_i64:
1582 return TCG_TARGET_HAS_clz_i64;
1583 case INDEX_op_ctz_i64:
1584 return TCG_TARGET_HAS_ctz_i64;
1585 case INDEX_op_ctpop_i64:
1586 return TCG_TARGET_HAS_ctpop_i64;
1587 case INDEX_op_add2_i64:
1588 return TCG_TARGET_HAS_add2_i64;
1589 case INDEX_op_sub2_i64:
1590 return TCG_TARGET_HAS_sub2_i64;
1591 case INDEX_op_mulu2_i64:
1592 return TCG_TARGET_HAS_mulu2_i64;
1593 case INDEX_op_muls2_i64:
1594 return TCG_TARGET_HAS_muls2_i64;
1595 case INDEX_op_muluh_i64:
1596 return TCG_TARGET_HAS_muluh_i64;
1597 case INDEX_op_mulsh_i64:
1598 return TCG_TARGET_HAS_mulsh_i64;
1599
d2fd745f
RH
1600 case INDEX_op_mov_vec:
1601 case INDEX_op_dup_vec:
1602 case INDEX_op_dupi_vec:
1603 case INDEX_op_ld_vec:
1604 case INDEX_op_st_vec:
1605 case INDEX_op_add_vec:
1606 case INDEX_op_sub_vec:
1607 case INDEX_op_and_vec:
1608 case INDEX_op_or_vec:
1609 case INDEX_op_xor_vec:
212be173 1610 case INDEX_op_cmp_vec:
d2fd745f
RH
1611 return have_vec;
1612 case INDEX_op_dup2_vec:
1613 return have_vec && TCG_TARGET_REG_BITS == 32;
1614 case INDEX_op_not_vec:
1615 return have_vec && TCG_TARGET_HAS_not_vec;
1616 case INDEX_op_neg_vec:
1617 return have_vec && TCG_TARGET_HAS_neg_vec;
1618 case INDEX_op_andc_vec:
1619 return have_vec && TCG_TARGET_HAS_andc_vec;
1620 case INDEX_op_orc_vec:
1621 return have_vec && TCG_TARGET_HAS_orc_vec;
3774030a
RH
1622 case INDEX_op_mul_vec:
1623 return have_vec && TCG_TARGET_HAS_mul_vec;
d0ec9796
RH
1624 case INDEX_op_shli_vec:
1625 case INDEX_op_shri_vec:
1626 case INDEX_op_sari_vec:
1627 return have_vec && TCG_TARGET_HAS_shi_vec;
1628 case INDEX_op_shls_vec:
1629 case INDEX_op_shrs_vec:
1630 case INDEX_op_sars_vec:
1631 return have_vec && TCG_TARGET_HAS_shs_vec;
1632 case INDEX_op_shlv_vec:
1633 case INDEX_op_shrv_vec:
1634 case INDEX_op_sarv_vec:
1635 return have_vec && TCG_TARGET_HAS_shv_vec;
8afaf050
RH
1636 case INDEX_op_ssadd_vec:
1637 case INDEX_op_usadd_vec:
1638 case INDEX_op_sssub_vec:
1639 case INDEX_op_ussub_vec:
1640 return have_vec && TCG_TARGET_HAS_sat_vec;
dd0a0fcd
RH
1641 case INDEX_op_smin_vec:
1642 case INDEX_op_umin_vec:
1643 case INDEX_op_smax_vec:
1644 case INDEX_op_umax_vec:
1645 return have_vec && TCG_TARGET_HAS_minmax_vec;
d2fd745f 1646
db432672
RH
1647 default:
1648 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
1649 return true;
be0f34b5 1650 }
be0f34b5
RH
1651}
1652
39cf05d3
FB
1653/* Note: we convert the 64 bit args to 32 bit and do some alignment
1654 and endian swap. Maybe it would be better to do the alignment
1655 and endian swap in tcg_reg_alloc_call(). */
ae8b75dc 1656void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)
c896fe29 1657{
75e8b9b7 1658 int i, real_args, nb_rets, pi;
bbb8a1b4 1659 unsigned sizemask, flags;
afb49896 1660 TCGHelperInfo *info;
75e8b9b7 1661 TCGOp *op;
afb49896 1662
619205fd 1663 info = g_hash_table_lookup(helper_table, (gpointer)func);
bbb8a1b4
RH
1664 flags = info->flags;
1665 sizemask = info->sizemask;
2bece2c8 1666
34b1a49c
RH
1667#if defined(__sparc__) && !defined(__arch64__) \
1668 && !defined(CONFIG_TCG_INTERPRETER)
1669 /* We have 64-bit values in one register, but need to pass as two
1670 separate parameters. Split them. */
1671 int orig_sizemask = sizemask;
1672 int orig_nargs = nargs;
1673 TCGv_i64 retl, reth;
ae8b75dc 1674 TCGTemp *split_args[MAX_OPC_PARAM];
34b1a49c 1675
f764718d
RH
1676 retl = NULL;
1677 reth = NULL;
34b1a49c 1678 if (sizemask != 0) {
34b1a49c
RH
1679 for (i = real_args = 0; i < nargs; ++i) {
1680 int is_64bit = sizemask & (1 << (i+1)*2);
1681 if (is_64bit) {
085272b3 1682 TCGv_i64 orig = temp_tcgv_i64(args[i]);
34b1a49c
RH
1683 TCGv_i32 h = tcg_temp_new_i32();
1684 TCGv_i32 l = tcg_temp_new_i32();
1685 tcg_gen_extr_i64_i32(l, h, orig);
ae8b75dc
RH
1686 split_args[real_args++] = tcgv_i32_temp(h);
1687 split_args[real_args++] = tcgv_i32_temp(l);
34b1a49c
RH
1688 } else {
1689 split_args[real_args++] = args[i];
1690 }
1691 }
1692 nargs = real_args;
1693 args = split_args;
1694 sizemask = 0;
1695 }
1696#elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2bece2c8
RH
1697 for (i = 0; i < nargs; ++i) {
1698 int is_64bit = sizemask & (1 << (i+1)*2);
1699 int is_signed = sizemask & (2 << (i+1)*2);
1700 if (!is_64bit) {
1701 TCGv_i64 temp = tcg_temp_new_i64();
085272b3 1702 TCGv_i64 orig = temp_tcgv_i64(args[i]);
2bece2c8
RH
1703 if (is_signed) {
1704 tcg_gen_ext32s_i64(temp, orig);
1705 } else {
1706 tcg_gen_ext32u_i64(temp, orig);
1707 }
ae8b75dc 1708 args[i] = tcgv_i64_temp(temp);
2bece2c8
RH
1709 }
1710 }
1711#endif /* TCG_TARGET_EXTEND_ARGS */
1712
15fa08f8 1713 op = tcg_emit_op(INDEX_op_call);
75e8b9b7
RH
1714
1715 pi = 0;
ae8b75dc 1716 if (ret != NULL) {
34b1a49c
RH
1717#if defined(__sparc__) && !defined(__arch64__) \
1718 && !defined(CONFIG_TCG_INTERPRETER)
1719 if (orig_sizemask & 1) {
1720 /* The 32-bit ABI is going to return the 64-bit value in
1721 the %o0/%o1 register pair. Prepare for this by using
1722 two return temporaries, and reassemble below. */
1723 retl = tcg_temp_new_i64();
1724 reth = tcg_temp_new_i64();
ae8b75dc
RH
1725 op->args[pi++] = tcgv_i64_arg(reth);
1726 op->args[pi++] = tcgv_i64_arg(retl);
34b1a49c
RH
1727 nb_rets = 2;
1728 } else {
ae8b75dc 1729 op->args[pi++] = temp_arg(ret);
34b1a49c
RH
1730 nb_rets = 1;
1731 }
1732#else
1733 if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
02eb19d0 1734#ifdef HOST_WORDS_BIGENDIAN
ae8b75dc
RH
1735 op->args[pi++] = temp_arg(ret + 1);
1736 op->args[pi++] = temp_arg(ret);
39cf05d3 1737#else
ae8b75dc
RH
1738 op->args[pi++] = temp_arg(ret);
1739 op->args[pi++] = temp_arg(ret + 1);
39cf05d3 1740#endif
a7812ae4 1741 nb_rets = 2;
34b1a49c 1742 } else {
ae8b75dc 1743 op->args[pi++] = temp_arg(ret);
a7812ae4 1744 nb_rets = 1;
c896fe29 1745 }
34b1a49c 1746#endif
a7812ae4
PB
1747 } else {
1748 nb_rets = 0;
c896fe29 1749 }
cd9090aa 1750 TCGOP_CALLO(op) = nb_rets;
75e8b9b7 1751
a7812ae4
PB
1752 real_args = 0;
1753 for (i = 0; i < nargs; i++) {
2bece2c8 1754 int is_64bit = sizemask & (1 << (i+1)*2);
bbb8a1b4 1755 if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
39cf05d3
FB
1756#ifdef TCG_TARGET_CALL_ALIGN_ARGS
1757 /* some targets want aligned 64 bit args */
ebd486d5 1758 if (real_args & 1) {
75e8b9b7 1759 op->args[pi++] = TCG_CALL_DUMMY_ARG;
ebd486d5 1760 real_args++;
39cf05d3
FB
1761 }
1762#endif
c70fbf0a
RH
1763 /* If stack grows up, then we will be placing successive
1764 arguments at lower addresses, which means we need to
1765 reverse the order compared to how we would normally
1766 treat either big or little-endian. For those arguments
1767 that will wind up in registers, this still works for
1768 HPPA (the only current STACK_GROWSUP target) since the
1769 argument registers are *also* allocated in decreasing
1770 order. If another such target is added, this logic may
1771 have to get more complicated to differentiate between
1772 stack arguments and register arguments. */
02eb19d0 1773#if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
ae8b75dc
RH
1774 op->args[pi++] = temp_arg(args[i] + 1);
1775 op->args[pi++] = temp_arg(args[i]);
c896fe29 1776#else
ae8b75dc
RH
1777 op->args[pi++] = temp_arg(args[i]);
1778 op->args[pi++] = temp_arg(args[i] + 1);
c896fe29 1779#endif
a7812ae4 1780 real_args += 2;
2bece2c8 1781 continue;
c896fe29 1782 }
2bece2c8 1783
ae8b75dc 1784 op->args[pi++] = temp_arg(args[i]);
2bece2c8 1785 real_args++;
c896fe29 1786 }
75e8b9b7
RH
1787 op->args[pi++] = (uintptr_t)func;
1788 op->args[pi++] = flags;
cd9090aa 1789 TCGOP_CALLI(op) = real_args;
a7812ae4 1790
75e8b9b7 1791 /* Make sure the fields didn't overflow. */
cd9090aa 1792 tcg_debug_assert(TCGOP_CALLI(op) == real_args);
75e8b9b7 1793 tcg_debug_assert(pi <= ARRAY_SIZE(op->args));
2bece2c8 1794
34b1a49c
RH
1795#if defined(__sparc__) && !defined(__arch64__) \
1796 && !defined(CONFIG_TCG_INTERPRETER)
1797 /* Free all of the parts we allocated above. */
1798 for (i = real_args = 0; i < orig_nargs; ++i) {
1799 int is_64bit = orig_sizemask & (1 << (i+1)*2);
1800 if (is_64bit) {
085272b3
RH
1801 tcg_temp_free_internal(args[real_args++]);
1802 tcg_temp_free_internal(args[real_args++]);
34b1a49c
RH
1803 } else {
1804 real_args++;
1805 }
1806 }
1807 if (orig_sizemask & 1) {
1808 /* The 32-bit ABI returned two 32-bit pieces. Re-assemble them.
1809 Note that describing these as TCGv_i64 eliminates an unnecessary
1810 zero-extension that tcg_gen_concat_i32_i64 would create. */
085272b3 1811 tcg_gen_concat32_i64(temp_tcgv_i64(ret), retl, reth);
34b1a49c
RH
1812 tcg_temp_free_i64(retl);
1813 tcg_temp_free_i64(reth);
1814 }
1815#elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2bece2c8
RH
1816 for (i = 0; i < nargs; ++i) {
1817 int is_64bit = sizemask & (1 << (i+1)*2);
1818 if (!is_64bit) {
085272b3 1819 tcg_temp_free_internal(args[i]);
2bece2c8
RH
1820 }
1821 }
1822#endif /* TCG_TARGET_EXTEND_ARGS */
c896fe29 1823}
c896fe29 1824
8fcd3692 1825static void tcg_reg_alloc_start(TCGContext *s)
c896fe29 1826{
ac3b8891 1827 int i, n;
c896fe29 1828 TCGTemp *ts;
ac3b8891
RH
1829
1830 for (i = 0, n = s->nb_globals; i < n; i++) {
c896fe29 1831 ts = &s->temps[i];
ac3b8891 1832 ts->val_type = (ts->fixed_reg ? TEMP_VAL_REG : TEMP_VAL_MEM);
c896fe29 1833 }
ac3b8891 1834 for (n = s->nb_temps; i < n; i++) {
e8996ee0 1835 ts = &s->temps[i];
ac3b8891 1836 ts->val_type = (ts->temp_local ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
e8996ee0
FB
1837 ts->mem_allocated = 0;
1838 ts->fixed_reg = 0;
1839 }
f8b2f202
RH
1840
1841 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
c896fe29
FB
1842}
1843
f8b2f202
RH
1844static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
1845 TCGTemp *ts)
c896fe29 1846{
1807f4c4 1847 int idx = temp_idx(ts);
ac56dd48 1848
fa477d25 1849 if (ts->temp_global) {
ac56dd48 1850 pstrcpy(buf, buf_size, ts->name);
f8b2f202
RH
1851 } else if (ts->temp_local) {
1852 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
c896fe29 1853 } else {
f8b2f202 1854 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
c896fe29
FB
1855 }
1856 return buf;
1857}
1858
43439139
RH
1859static char *tcg_get_arg_str(TCGContext *s, char *buf,
1860 int buf_size, TCGArg arg)
f8b2f202 1861{
43439139 1862 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
f8b2f202
RH
1863}
1864
6e085f72
RH
1865/* Find helper name. */
1866static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
4dc81f28 1867{
6e085f72 1868 const char *ret = NULL;
619205fd
EC
1869 if (helper_table) {
1870 TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val);
72866e82
RH
1871 if (info) {
1872 ret = info->name;
1873 }
4dc81f28 1874 }
6e085f72 1875 return ret;
4dc81f28
FB
1876}
1877
f48f3ede
BS
1878static const char * const cond_name[] =
1879{
0aed257f
RH
1880 [TCG_COND_NEVER] = "never",
1881 [TCG_COND_ALWAYS] = "always",
f48f3ede
BS
1882 [TCG_COND_EQ] = "eq",
1883 [TCG_COND_NE] = "ne",
1884 [TCG_COND_LT] = "lt",
1885 [TCG_COND_GE] = "ge",
1886 [TCG_COND_LE] = "le",
1887 [TCG_COND_GT] = "gt",
1888 [TCG_COND_LTU] = "ltu",
1889 [TCG_COND_GEU] = "geu",
1890 [TCG_COND_LEU] = "leu",
1891 [TCG_COND_GTU] = "gtu"
1892};
1893
f713d6ad
RH
1894static const char * const ldst_name[] =
1895{
1896 [MO_UB] = "ub",
1897 [MO_SB] = "sb",
1898 [MO_LEUW] = "leuw",
1899 [MO_LESW] = "lesw",
1900 [MO_LEUL] = "leul",
1901 [MO_LESL] = "lesl",
1902 [MO_LEQ] = "leq",
1903 [MO_BEUW] = "beuw",
1904 [MO_BESW] = "besw",
1905 [MO_BEUL] = "beul",
1906 [MO_BESL] = "besl",
1907 [MO_BEQ] = "beq",
1908};
1909
1f00b27f
SS
1910static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
1911#ifdef ALIGNED_ONLY
1912 [MO_UNALN >> MO_ASHIFT] = "un+",
1913 [MO_ALIGN >> MO_ASHIFT] = "",
1914#else
1915 [MO_UNALN >> MO_ASHIFT] = "",
1916 [MO_ALIGN >> MO_ASHIFT] = "al+",
1917#endif
1918 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
1919 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
1920 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
1921 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
1922 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
1923 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
1924};
1925
b016486e
RH
1926static inline bool tcg_regset_single(TCGRegSet d)
1927{
1928 return (d & (d - 1)) == 0;
1929}
1930
1931static inline TCGReg tcg_regset_first(TCGRegSet d)
1932{
1933 if (TCG_TARGET_NB_REGS <= 32) {
1934 return ctz32(d);
1935 } else {
1936 return ctz64(d);
1937 }
1938}
1939
1894f69a 1940static void tcg_dump_ops(TCGContext *s, bool have_prefs)
c896fe29 1941{
c896fe29 1942 char buf[128];
c45cb8bb 1943 TCGOp *op;
c45cb8bb 1944
15fa08f8 1945 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb
RH
1946 int i, k, nb_oargs, nb_iargs, nb_cargs;
1947 const TCGOpDef *def;
c45cb8bb 1948 TCGOpcode c;
bdfb460e 1949 int col = 0;
c896fe29 1950
c45cb8bb 1951 c = op->opc;
c896fe29 1952 def = &tcg_op_defs[c];
c45cb8bb 1953
765b842a 1954 if (c == INDEX_op_insn_start) {
b016486e 1955 nb_oargs = 0;
15fa08f8 1956 col += qemu_log("\n ----");
9aef40ed
RH
1957
1958 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
1959 target_ulong a;
7e4597d7 1960#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
efee3746 1961 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
7e4597d7 1962#else
efee3746 1963 a = op->args[i];
7e4597d7 1964#endif
bdfb460e 1965 col += qemu_log(" " TARGET_FMT_lx, a);
eeacee4d 1966 }
7e4597d7 1967 } else if (c == INDEX_op_call) {
c896fe29 1968 /* variable number of arguments */
cd9090aa
RH
1969 nb_oargs = TCGOP_CALLO(op);
1970 nb_iargs = TCGOP_CALLI(op);
c896fe29 1971 nb_cargs = def->nb_cargs;
c896fe29 1972
cf066674 1973 /* function name, flags, out args */
bdfb460e 1974 col += qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
efee3746
RH
1975 tcg_find_helper(s, op->args[nb_oargs + nb_iargs]),
1976 op->args[nb_oargs + nb_iargs + 1], nb_oargs);
cf066674 1977 for (i = 0; i < nb_oargs; i++) {
43439139
RH
1978 col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
1979 op->args[i]));
b03cce8e 1980 }
cf066674 1981 for (i = 0; i < nb_iargs; i++) {
efee3746 1982 TCGArg arg = op->args[nb_oargs + i];
cf066674
RH
1983 const char *t = "<dummy>";
1984 if (arg != TCG_CALL_DUMMY_ARG) {
43439139 1985 t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
eeacee4d 1986 }
bdfb460e 1987 col += qemu_log(",%s", t);
e8996ee0 1988 }
b03cce8e 1989 } else {
bdfb460e 1990 col += qemu_log(" %s ", def->name);
c45cb8bb
RH
1991
1992 nb_oargs = def->nb_oargs;
1993 nb_iargs = def->nb_iargs;
1994 nb_cargs = def->nb_cargs;
1995
d2fd745f
RH
1996 if (def->flags & TCG_OPF_VECTOR) {
1997 col += qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op),
1998 8 << TCGOP_VECE(op));
1999 }
2000
b03cce8e 2001 k = 0;
c45cb8bb 2002 for (i = 0; i < nb_oargs; i++) {
eeacee4d 2003 if (k != 0) {
bdfb460e 2004 col += qemu_log(",");
eeacee4d 2005 }
43439139
RH
2006 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
2007 op->args[k++]));
b03cce8e 2008 }
c45cb8bb 2009 for (i = 0; i < nb_iargs; i++) {
eeacee4d 2010 if (k != 0) {
bdfb460e 2011 col += qemu_log(",");
eeacee4d 2012 }
43439139
RH
2013 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
2014 op->args[k++]));
b03cce8e 2015 }
be210acb
RH
2016 switch (c) {
2017 case INDEX_op_brcond_i32:
be210acb 2018 case INDEX_op_setcond_i32:
ffc5ea09 2019 case INDEX_op_movcond_i32:
ffc5ea09 2020 case INDEX_op_brcond2_i32:
be210acb 2021 case INDEX_op_setcond2_i32:
ffc5ea09 2022 case INDEX_op_brcond_i64:
be210acb 2023 case INDEX_op_setcond_i64:
ffc5ea09 2024 case INDEX_op_movcond_i64:
212be173 2025 case INDEX_op_cmp_vec:
efee3746
RH
2026 if (op->args[k] < ARRAY_SIZE(cond_name)
2027 && cond_name[op->args[k]]) {
2028 col += qemu_log(",%s", cond_name[op->args[k++]]);
eeacee4d 2029 } else {
efee3746 2030 col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]);
eeacee4d 2031 }
f48f3ede 2032 i = 1;
be210acb 2033 break;
f713d6ad
RH
2034 case INDEX_op_qemu_ld_i32:
2035 case INDEX_op_qemu_st_i32:
2036 case INDEX_op_qemu_ld_i64:
2037 case INDEX_op_qemu_st_i64:
59227d5d 2038 {
efee3746 2039 TCGMemOpIdx oi = op->args[k++];
59227d5d
RH
2040 TCGMemOp op = get_memop(oi);
2041 unsigned ix = get_mmuidx(oi);
2042
59c4b7e8 2043 if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
bdfb460e 2044 col += qemu_log(",$0x%x,%u", op, ix);
59c4b7e8 2045 } else {
1f00b27f
SS
2046 const char *s_al, *s_op;
2047 s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT];
59c4b7e8 2048 s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
bdfb460e 2049 col += qemu_log(",%s%s,%u", s_al, s_op, ix);
59227d5d
RH
2050 }
2051 i = 1;
f713d6ad 2052 }
f713d6ad 2053 break;
be210acb 2054 default:
f48f3ede 2055 i = 0;
be210acb
RH
2056 break;
2057 }
51e3972c
RH
2058 switch (c) {
2059 case INDEX_op_set_label:
2060 case INDEX_op_br:
2061 case INDEX_op_brcond_i32:
2062 case INDEX_op_brcond_i64:
2063 case INDEX_op_brcond2_i32:
efee3746
RH
2064 col += qemu_log("%s$L%d", k ? "," : "",
2065 arg_label(op->args[k])->id);
51e3972c
RH
2066 i++, k++;
2067 break;
2068 default:
2069 break;
2070 }
2071 for (; i < nb_cargs; i++, k++) {
efee3746 2072 col += qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", op->args[k]);
bdfb460e
RH
2073 }
2074 }
bdfb460e 2075
1894f69a
RH
2076 if (have_prefs || op->life) {
2077 for (; col < 40; ++col) {
bdfb460e
RH
2078 putc(' ', qemu_logfile);
2079 }
1894f69a
RH
2080 }
2081
2082 if (op->life) {
2083 unsigned life = op->life;
bdfb460e
RH
2084
2085 if (life & (SYNC_ARG * 3)) {
2086 qemu_log(" sync:");
2087 for (i = 0; i < 2; ++i) {
2088 if (life & (SYNC_ARG << i)) {
2089 qemu_log(" %d", i);
2090 }
2091 }
2092 }
2093 life /= DEAD_ARG;
2094 if (life) {
2095 qemu_log(" dead:");
2096 for (i = 0; life; ++i, life >>= 1) {
2097 if (life & 1) {
2098 qemu_log(" %d", i);
2099 }
2100 }
b03cce8e 2101 }
c896fe29 2102 }
1894f69a
RH
2103
2104 if (have_prefs) {
2105 for (i = 0; i < nb_oargs; ++i) {
2106 TCGRegSet set = op->output_pref[i];
2107
2108 if (i == 0) {
2109 qemu_log(" pref=");
2110 } else {
2111 qemu_log(",");
2112 }
2113 if (set == 0) {
2114 qemu_log("none");
2115 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
2116 qemu_log("all");
2117#ifdef CONFIG_DEBUG_TCG
2118 } else if (tcg_regset_single(set)) {
2119 TCGReg reg = tcg_regset_first(set);
2120 qemu_log("%s", tcg_target_reg_names[reg]);
2121#endif
2122 } else if (TCG_TARGET_NB_REGS <= 32) {
2123 qemu_log("%#x", (uint32_t)set);
2124 } else {
2125 qemu_log("%#" PRIx64, (uint64_t)set);
2126 }
2127 }
2128 }
2129
eeacee4d 2130 qemu_log("\n");
c896fe29
FB
2131 }
2132}
2133
2134/* we give more priority to constraints with less registers */
2135static int get_constraint_priority(const TCGOpDef *def, int k)
2136{
2137 const TCGArgConstraint *arg_ct;
2138
2139 int i, n;
2140 arg_ct = &def->args_ct[k];
2141 if (arg_ct->ct & TCG_CT_ALIAS) {
2142 /* an alias is equivalent to a single register */
2143 n = 1;
2144 } else {
2145 if (!(arg_ct->ct & TCG_CT_REG))
2146 return 0;
2147 n = 0;
2148 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2149 if (tcg_regset_test_reg(arg_ct->u.regs, i))
2150 n++;
2151 }
2152 }
2153 return TCG_TARGET_NB_REGS - n + 1;
2154}
2155
2156/* sort from highest priority to lowest */
2157static void sort_constraints(TCGOpDef *def, int start, int n)
2158{
2159 int i, j, p1, p2, tmp;
2160
2161 for(i = 0; i < n; i++)
2162 def->sorted_args[start + i] = start + i;
2163 if (n <= 1)
2164 return;
2165 for(i = 0; i < n - 1; i++) {
2166 for(j = i + 1; j < n; j++) {
2167 p1 = get_constraint_priority(def, def->sorted_args[start + i]);
2168 p2 = get_constraint_priority(def, def->sorted_args[start + j]);
2169 if (p1 < p2) {
2170 tmp = def->sorted_args[start + i];
2171 def->sorted_args[start + i] = def->sorted_args[start + j];
2172 def->sorted_args[start + j] = tmp;
2173 }
2174 }
2175 }
2176}
2177
f69d277e 2178static void process_op_defs(TCGContext *s)
c896fe29 2179{
a9751609 2180 TCGOpcode op;
c896fe29 2181
f69d277e
RH
2182 for (op = 0; op < NB_OPS; op++) {
2183 TCGOpDef *def = &tcg_op_defs[op];
2184 const TCGTargetOpDef *tdefs;
069ea736
RH
2185 TCGType type;
2186 int i, nb_args;
f69d277e
RH
2187
2188 if (def->flags & TCG_OPF_NOT_PRESENT) {
2189 continue;
2190 }
2191
c896fe29 2192 nb_args = def->nb_iargs + def->nb_oargs;
f69d277e
RH
2193 if (nb_args == 0) {
2194 continue;
2195 }
2196
2197 tdefs = tcg_target_op_def(op);
2198 /* Missing TCGTargetOpDef entry. */
2199 tcg_debug_assert(tdefs != NULL);
2200
069ea736 2201 type = (def->flags & TCG_OPF_64BIT ? TCG_TYPE_I64 : TCG_TYPE_I32);
f69d277e
RH
2202 for (i = 0; i < nb_args; i++) {
2203 const char *ct_str = tdefs->args_ct_str[i];
2204 /* Incomplete TCGTargetOpDef entry. */
eabb7b91 2205 tcg_debug_assert(ct_str != NULL);
f69d277e 2206
ccb1bb66 2207 def->args_ct[i].u.regs = 0;
c896fe29 2208 def->args_ct[i].ct = 0;
17280ff4
RH
2209 while (*ct_str != '\0') {
2210 switch(*ct_str) {
2211 case '0' ... '9':
2212 {
2213 int oarg = *ct_str - '0';
2214 tcg_debug_assert(ct_str == tdefs->args_ct_str[i]);
2215 tcg_debug_assert(oarg < def->nb_oargs);
2216 tcg_debug_assert(def->args_ct[oarg].ct & TCG_CT_REG);
2217 /* TCG_CT_ALIAS is for the output arguments.
2218 The input is tagged with TCG_CT_IALIAS. */
2219 def->args_ct[i] = def->args_ct[oarg];
2220 def->args_ct[oarg].ct |= TCG_CT_ALIAS;
2221 def->args_ct[oarg].alias_index = i;
2222 def->args_ct[i].ct |= TCG_CT_IALIAS;
2223 def->args_ct[i].alias_index = oarg;
c896fe29 2224 }
17280ff4
RH
2225 ct_str++;
2226 break;
2227 case '&':
2228 def->args_ct[i].ct |= TCG_CT_NEWREG;
2229 ct_str++;
2230 break;
2231 case 'i':
2232 def->args_ct[i].ct |= TCG_CT_CONST;
2233 ct_str++;
2234 break;
2235 default:
2236 ct_str = target_parse_constraint(&def->args_ct[i],
2237 ct_str, type);
2238 /* Typo in TCGTargetOpDef constraint. */
2239 tcg_debug_assert(ct_str != NULL);
c896fe29
FB
2240 }
2241 }
2242 }
2243
c68aaa18 2244 /* TCGTargetOpDef entry with too much information? */
eabb7b91 2245 tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
c68aaa18 2246
c896fe29
FB
2247 /* sort the constraints (XXX: this is just an heuristic) */
2248 sort_constraints(def, 0, def->nb_oargs);
2249 sort_constraints(def, def->nb_oargs, def->nb_iargs);
a9751609 2250 }
c896fe29
FB
2251}
2252
0c627cdc
RH
2253void tcg_op_remove(TCGContext *s, TCGOp *op)
2254{
d88a117e
RH
2255 TCGLabel *label;
2256
2257 switch (op->opc) {
2258 case INDEX_op_br:
2259 label = arg_label(op->args[0]);
2260 label->refs--;
2261 break;
2262 case INDEX_op_brcond_i32:
2263 case INDEX_op_brcond_i64:
2264 label = arg_label(op->args[3]);
2265 label->refs--;
2266 break;
2267 case INDEX_op_brcond2_i32:
2268 label = arg_label(op->args[5]);
2269 label->refs--;
2270 break;
2271 default:
2272 break;
2273 }
2274
15fa08f8
RH
2275 QTAILQ_REMOVE(&s->ops, op, link);
2276 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
abebf925 2277 s->nb_ops--;
0c627cdc
RH
2278
2279#ifdef CONFIG_PROFILER
c3fac113 2280 atomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
0c627cdc
RH
2281#endif
2282}
2283
15fa08f8 2284static TCGOp *tcg_op_alloc(TCGOpcode opc)
5a18407f 2285{
15fa08f8
RH
2286 TCGContext *s = tcg_ctx;
2287 TCGOp *op;
5a18407f 2288
15fa08f8
RH
2289 if (likely(QTAILQ_EMPTY(&s->free_ops))) {
2290 op = tcg_malloc(sizeof(TCGOp));
2291 } else {
2292 op = QTAILQ_FIRST(&s->free_ops);
2293 QTAILQ_REMOVE(&s->free_ops, op, link);
2294 }
2295 memset(op, 0, offsetof(TCGOp, link));
2296 op->opc = opc;
abebf925 2297 s->nb_ops++;
5a18407f 2298
15fa08f8
RH
2299 return op;
2300}
2301
2302TCGOp *tcg_emit_op(TCGOpcode opc)
2303{
2304 TCGOp *op = tcg_op_alloc(opc);
2305 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2306 return op;
2307}
5a18407f 2308
ac1043f6 2309TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
15fa08f8
RH
2310{
2311 TCGOp *new_op = tcg_op_alloc(opc);
2312 QTAILQ_INSERT_BEFORE(old_op, new_op, link);
5a18407f
RH
2313 return new_op;
2314}
2315
ac1043f6 2316TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
5a18407f 2317{
15fa08f8
RH
2318 TCGOp *new_op = tcg_op_alloc(opc);
2319 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
5a18407f
RH
2320 return new_op;
2321}
2322
b4fc67c7
RH
2323/* Reachable analysis : remove unreachable code. */
2324static void reachable_code_pass(TCGContext *s)
2325{
2326 TCGOp *op, *op_next;
2327 bool dead = false;
2328
2329 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2330 bool remove = dead;
2331 TCGLabel *label;
2332 int call_flags;
2333
2334 switch (op->opc) {
2335 case INDEX_op_set_label:
2336 label = arg_label(op->args[0]);
2337 if (label->refs == 0) {
2338 /*
2339 * While there is an occasional backward branch, virtually
2340 * all branches generated by the translators are forward.
2341 * Which means that generally we will have already removed
2342 * all references to the label that will be, and there is
2343 * little to be gained by iterating.
2344 */
2345 remove = true;
2346 } else {
2347 /* Once we see a label, insns become live again. */
2348 dead = false;
2349 remove = false;
2350
2351 /*
2352 * Optimization can fold conditional branches to unconditional.
2353 * If we find a label with one reference which is preceded by
2354 * an unconditional branch to it, remove both. This needed to
2355 * wait until the dead code in between them was removed.
2356 */
2357 if (label->refs == 1) {
eae3eb3e 2358 TCGOp *op_prev = QTAILQ_PREV(op, link);
b4fc67c7
RH
2359 if (op_prev->opc == INDEX_op_br &&
2360 label == arg_label(op_prev->args[0])) {
2361 tcg_op_remove(s, op_prev);
2362 remove = true;
2363 }
2364 }
2365 }
2366 break;
2367
2368 case INDEX_op_br:
2369 case INDEX_op_exit_tb:
2370 case INDEX_op_goto_ptr:
2371 /* Unconditional branches; everything following is dead. */
2372 dead = true;
2373 break;
2374
2375 case INDEX_op_call:
2376 /* Notice noreturn helper calls, raising exceptions. */
2377 call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1];
2378 if (call_flags & TCG_CALL_NO_RETURN) {
2379 dead = true;
2380 }
2381 break;
2382
2383 case INDEX_op_insn_start:
2384 /* Never remove -- we need to keep these for unwind. */
2385 remove = false;
2386 break;
2387
2388 default:
2389 break;
2390 }
2391
2392 if (remove) {
2393 tcg_op_remove(s, op);
2394 }
2395 }
2396}
2397
c70fbf0a
RH
2398#define TS_DEAD 1
2399#define TS_MEM 2
2400
5a18407f
RH
2401#define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
2402#define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
2403
25f49c5f
RH
2404/* For liveness_pass_1, the register preferences for a given temp. */
2405static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
2406{
2407 return ts->state_ptr;
2408}
2409
2410/* For liveness_pass_1, reset the preferences for a given temp to the
2411 * maximal regset for its type.
2412 */
2413static inline void la_reset_pref(TCGTemp *ts)
2414{
2415 *la_temp_pref(ts)
2416 = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
2417}
2418
9c43b68d
AJ
2419/* liveness analysis: end of function: all temps are dead, and globals
2420 should be in memory. */
2616c808 2421static void la_func_end(TCGContext *s, int ng, int nt)
c896fe29 2422{
b83eabea
RH
2423 int i;
2424
2425 for (i = 0; i < ng; ++i) {
2426 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f 2427 la_reset_pref(&s->temps[i]);
b83eabea
RH
2428 }
2429 for (i = ng; i < nt; ++i) {
2430 s->temps[i].state = TS_DEAD;
25f49c5f 2431 la_reset_pref(&s->temps[i]);
b83eabea 2432 }
c896fe29
FB
2433}
2434
9c43b68d
AJ
2435/* liveness analysis: end of basic block: all temps are dead, globals
2436 and local temps should be in memory. */
2616c808 2437static void la_bb_end(TCGContext *s, int ng, int nt)
641d5fbe 2438{
b83eabea 2439 int i;
641d5fbe 2440
b83eabea
RH
2441 for (i = 0; i < ng; ++i) {
2442 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f 2443 la_reset_pref(&s->temps[i]);
b83eabea
RH
2444 }
2445 for (i = ng; i < nt; ++i) {
2446 s->temps[i].state = (s->temps[i].temp_local
2447 ? TS_DEAD | TS_MEM
2448 : TS_DEAD);
25f49c5f 2449 la_reset_pref(&s->temps[i]);
641d5fbe
FB
2450 }
2451}
2452
f65a061c
RH
2453/* liveness analysis: sync globals back to memory. */
2454static void la_global_sync(TCGContext *s, int ng)
2455{
2456 int i;
2457
2458 for (i = 0; i < ng; ++i) {
25f49c5f
RH
2459 int state = s->temps[i].state;
2460 s->temps[i].state = state | TS_MEM;
2461 if (state == TS_DEAD) {
2462 /* If the global was previously dead, reset prefs. */
2463 la_reset_pref(&s->temps[i]);
2464 }
f65a061c
RH
2465 }
2466}
2467
2468/* liveness analysis: sync globals back to memory and kill. */
2469static void la_global_kill(TCGContext *s, int ng)
2470{
2471 int i;
2472
2473 for (i = 0; i < ng; i++) {
2474 s->temps[i].state = TS_DEAD | TS_MEM;
25f49c5f
RH
2475 la_reset_pref(&s->temps[i]);
2476 }
2477}
2478
2479/* liveness analysis: note live globals crossing calls. */
2480static void la_cross_call(TCGContext *s, int nt)
2481{
2482 TCGRegSet mask = ~tcg_target_call_clobber_regs;
2483 int i;
2484
2485 for (i = 0; i < nt; i++) {
2486 TCGTemp *ts = &s->temps[i];
2487 if (!(ts->state & TS_DEAD)) {
2488 TCGRegSet *pset = la_temp_pref(ts);
2489 TCGRegSet set = *pset;
2490
2491 set &= mask;
2492 /* If the combination is not possible, restart. */
2493 if (set == 0) {
2494 set = tcg_target_available_regs[ts->type] & mask;
2495 }
2496 *pset = set;
2497 }
f65a061c
RH
2498 }
2499}
2500
a1b3c48d 2501/* Liveness analysis : update the opc_arg_life array to tell if a
c896fe29
FB
2502 given input arguments is dead. Instructions updating dead
2503 temporaries are removed. */
b83eabea 2504static void liveness_pass_1(TCGContext *s)
c896fe29 2505{
c70fbf0a 2506 int nb_globals = s->nb_globals;
2616c808 2507 int nb_temps = s->nb_temps;
15fa08f8 2508 TCGOp *op, *op_prev;
25f49c5f
RH
2509 TCGRegSet *prefs;
2510 int i;
2511
2512 prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
2513 for (i = 0; i < nb_temps; ++i) {
2514 s->temps[i].state_ptr = prefs + i;
2515 }
a1b3c48d 2516
ae36a246 2517 /* ??? Should be redundant with the exit_tb that ends the TB. */
2616c808 2518 la_func_end(s, nb_globals, nb_temps);
c896fe29 2519
eae3eb3e 2520 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
25f49c5f 2521 int nb_iargs, nb_oargs;
c45cb8bb
RH
2522 TCGOpcode opc_new, opc_new2;
2523 bool have_opc_new2;
a1b3c48d 2524 TCGLifeData arg_life = 0;
25f49c5f 2525 TCGTemp *ts;
c45cb8bb
RH
2526 TCGOpcode opc = op->opc;
2527 const TCGOpDef *def = &tcg_op_defs[opc];
2528
c45cb8bb 2529 switch (opc) {
c896fe29 2530 case INDEX_op_call:
c6e113f5
FB
2531 {
2532 int call_flags;
25f49c5f 2533 int nb_call_regs;
c896fe29 2534
cd9090aa
RH
2535 nb_oargs = TCGOP_CALLO(op);
2536 nb_iargs = TCGOP_CALLI(op);
efee3746 2537 call_flags = op->args[nb_oargs + nb_iargs + 1];
c6e113f5 2538
c45cb8bb 2539 /* pure functions can be removed if their result is unused */
78505279 2540 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
cf066674 2541 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
2542 ts = arg_temp(op->args[i]);
2543 if (ts->state != TS_DEAD) {
c6e113f5 2544 goto do_not_remove_call;
9c43b68d 2545 }
c6e113f5 2546 }
c45cb8bb 2547 goto do_remove;
152c35aa
RH
2548 }
2549 do_not_remove_call:
c896fe29 2550
25f49c5f 2551 /* Output args are dead. */
152c35aa 2552 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
2553 ts = arg_temp(op->args[i]);
2554 if (ts->state & TS_DEAD) {
152c35aa
RH
2555 arg_life |= DEAD_ARG << i;
2556 }
25f49c5f 2557 if (ts->state & TS_MEM) {
152c35aa 2558 arg_life |= SYNC_ARG << i;
c6e113f5 2559 }
25f49c5f
RH
2560 ts->state = TS_DEAD;
2561 la_reset_pref(ts);
2562
2563 /* Not used -- it will be tcg_target_call_oarg_regs[i]. */
2564 op->output_pref[i] = 0;
152c35aa 2565 }
78505279 2566
152c35aa
RH
2567 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
2568 TCG_CALL_NO_READ_GLOBALS))) {
f65a061c 2569 la_global_kill(s, nb_globals);
152c35aa 2570 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
f65a061c 2571 la_global_sync(s, nb_globals);
152c35aa 2572 }
b9c18f56 2573
25f49c5f 2574 /* Record arguments that die in this helper. */
152c35aa 2575 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
25f49c5f
RH
2576 ts = arg_temp(op->args[i]);
2577 if (ts && ts->state & TS_DEAD) {
152c35aa 2578 arg_life |= DEAD_ARG << i;
c6e113f5 2579 }
152c35aa 2580 }
25f49c5f
RH
2581
2582 /* For all live registers, remove call-clobbered prefs. */
2583 la_cross_call(s, nb_temps);
2584
2585 nb_call_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2586
2587 /* Input arguments are live for preceding opcodes. */
2588 for (i = 0; i < nb_iargs; i++) {
2589 ts = arg_temp(op->args[i + nb_oargs]);
2590 if (ts && ts->state & TS_DEAD) {
2591 /* For those arguments that die, and will be allocated
2592 * in registers, clear the register set for that arg,
2593 * to be filled in below. For args that will be on
2594 * the stack, reset to any available reg.
2595 */
2596 *la_temp_pref(ts)
2597 = (i < nb_call_regs ? 0 :
2598 tcg_target_available_regs[ts->type]);
2599 ts->state &= ~TS_DEAD;
2600 }
2601 }
2602
2603 /* For each input argument, add its input register to prefs.
2604 If a temp is used once, this produces a single set bit. */
2605 for (i = 0; i < MIN(nb_call_regs, nb_iargs); i++) {
2606 ts = arg_temp(op->args[i + nb_oargs]);
2607 if (ts) {
2608 tcg_regset_set_reg(*la_temp_pref(ts),
2609 tcg_target_call_iarg_regs[i]);
c19f47bf 2610 }
c896fe29 2611 }
c896fe29 2612 }
c896fe29 2613 break;
765b842a 2614 case INDEX_op_insn_start:
c896fe29 2615 break;
5ff9d6a4 2616 case INDEX_op_discard:
5ff9d6a4 2617 /* mark the temporary as dead */
25f49c5f
RH
2618 ts = arg_temp(op->args[0]);
2619 ts->state = TS_DEAD;
2620 la_reset_pref(ts);
5ff9d6a4 2621 break;
1305c451
RH
2622
2623 case INDEX_op_add2_i32:
c45cb8bb 2624 opc_new = INDEX_op_add_i32;
f1fae40c 2625 goto do_addsub2;
1305c451 2626 case INDEX_op_sub2_i32:
c45cb8bb 2627 opc_new = INDEX_op_sub_i32;
f1fae40c
RH
2628 goto do_addsub2;
2629 case INDEX_op_add2_i64:
c45cb8bb 2630 opc_new = INDEX_op_add_i64;
f1fae40c
RH
2631 goto do_addsub2;
2632 case INDEX_op_sub2_i64:
c45cb8bb 2633 opc_new = INDEX_op_sub_i64;
f1fae40c 2634 do_addsub2:
1305c451
RH
2635 nb_iargs = 4;
2636 nb_oargs = 2;
2637 /* Test if the high part of the operation is dead, but not
2638 the low part. The result can be optimized to a simple
2639 add or sub. This happens often for x86_64 guest when the
2640 cpu mode is set to 32 bit. */
b83eabea
RH
2641 if (arg_temp(op->args[1])->state == TS_DEAD) {
2642 if (arg_temp(op->args[0])->state == TS_DEAD) {
1305c451
RH
2643 goto do_remove;
2644 }
c45cb8bb
RH
2645 /* Replace the opcode and adjust the args in place,
2646 leaving 3 unused args at the end. */
2647 op->opc = opc = opc_new;
efee3746
RH
2648 op->args[1] = op->args[2];
2649 op->args[2] = op->args[4];
1305c451
RH
2650 /* Fall through and mark the single-word operation live. */
2651 nb_iargs = 2;
2652 nb_oargs = 1;
2653 }
2654 goto do_not_remove;
2655
1414968a 2656 case INDEX_op_mulu2_i32:
c45cb8bb
RH
2657 opc_new = INDEX_op_mul_i32;
2658 opc_new2 = INDEX_op_muluh_i32;
2659 have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
03271524 2660 goto do_mul2;
f1fae40c 2661 case INDEX_op_muls2_i32:
c45cb8bb
RH
2662 opc_new = INDEX_op_mul_i32;
2663 opc_new2 = INDEX_op_mulsh_i32;
2664 have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
f1fae40c
RH
2665 goto do_mul2;
2666 case INDEX_op_mulu2_i64:
c45cb8bb
RH
2667 opc_new = INDEX_op_mul_i64;
2668 opc_new2 = INDEX_op_muluh_i64;
2669 have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
03271524 2670 goto do_mul2;
f1fae40c 2671 case INDEX_op_muls2_i64:
c45cb8bb
RH
2672 opc_new = INDEX_op_mul_i64;
2673 opc_new2 = INDEX_op_mulsh_i64;
2674 have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
03271524 2675 goto do_mul2;
f1fae40c 2676 do_mul2:
1414968a
RH
2677 nb_iargs = 2;
2678 nb_oargs = 2;
b83eabea
RH
2679 if (arg_temp(op->args[1])->state == TS_DEAD) {
2680 if (arg_temp(op->args[0])->state == TS_DEAD) {
03271524 2681 /* Both parts of the operation are dead. */
1414968a
RH
2682 goto do_remove;
2683 }
03271524 2684 /* The high part of the operation is dead; generate the low. */
c45cb8bb 2685 op->opc = opc = opc_new;
efee3746
RH
2686 op->args[1] = op->args[2];
2687 op->args[2] = op->args[3];
b83eabea 2688 } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
c45cb8bb
RH
2689 /* The low part of the operation is dead; generate the high. */
2690 op->opc = opc = opc_new2;
efee3746
RH
2691 op->args[0] = op->args[1];
2692 op->args[1] = op->args[2];
2693 op->args[2] = op->args[3];
03271524
RH
2694 } else {
2695 goto do_not_remove;
1414968a 2696 }
03271524
RH
2697 /* Mark the single-word operation live. */
2698 nb_oargs = 1;
1414968a
RH
2699 goto do_not_remove;
2700
c896fe29 2701 default:
1305c451 2702 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
49516bc0
AJ
2703 nb_iargs = def->nb_iargs;
2704 nb_oargs = def->nb_oargs;
c896fe29 2705
49516bc0
AJ
2706 /* Test if the operation can be removed because all
2707 its outputs are dead. We assume that nb_oargs == 0
2708 implies side effects */
2709 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
c45cb8bb 2710 for (i = 0; i < nb_oargs; i++) {
b83eabea 2711 if (arg_temp(op->args[i])->state != TS_DEAD) {
49516bc0 2712 goto do_not_remove;
9c43b68d 2713 }
49516bc0 2714 }
152c35aa
RH
2715 goto do_remove;
2716 }
2717 goto do_not_remove;
49516bc0 2718
152c35aa
RH
2719 do_remove:
2720 tcg_op_remove(s, op);
2721 break;
2722
2723 do_not_remove:
152c35aa 2724 for (i = 0; i < nb_oargs; i++) {
25f49c5f
RH
2725 ts = arg_temp(op->args[i]);
2726
2727 /* Remember the preference of the uses that followed. */
2728 op->output_pref[i] = *la_temp_pref(ts);
2729
2730 /* Output args are dead. */
2731 if (ts->state & TS_DEAD) {
152c35aa 2732 arg_life |= DEAD_ARG << i;
49516bc0 2733 }
25f49c5f 2734 if (ts->state & TS_MEM) {
152c35aa
RH
2735 arg_life |= SYNC_ARG << i;
2736 }
25f49c5f
RH
2737 ts->state = TS_DEAD;
2738 la_reset_pref(ts);
152c35aa 2739 }
49516bc0 2740
25f49c5f 2741 /* If end of basic block, update. */
ae36a246
RH
2742 if (def->flags & TCG_OPF_BB_EXIT) {
2743 la_func_end(s, nb_globals, nb_temps);
2744 } else if (def->flags & TCG_OPF_BB_END) {
2616c808 2745 la_bb_end(s, nb_globals, nb_temps);
152c35aa 2746 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
f65a061c 2747 la_global_sync(s, nb_globals);
25f49c5f
RH
2748 if (def->flags & TCG_OPF_CALL_CLOBBER) {
2749 la_cross_call(s, nb_temps);
2750 }
152c35aa
RH
2751 }
2752
25f49c5f 2753 /* Record arguments that die in this opcode. */
152c35aa 2754 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
25f49c5f
RH
2755 ts = arg_temp(op->args[i]);
2756 if (ts->state & TS_DEAD) {
152c35aa 2757 arg_life |= DEAD_ARG << i;
c896fe29 2758 }
c896fe29 2759 }
25f49c5f
RH
2760
2761 /* Input arguments are live for preceding opcodes. */
152c35aa 2762 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
25f49c5f
RH
2763 ts = arg_temp(op->args[i]);
2764 if (ts->state & TS_DEAD) {
2765 /* For operands that were dead, initially allow
2766 all regs for the type. */
2767 *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
2768 ts->state &= ~TS_DEAD;
2769 }
2770 }
2771
2772 /* Incorporate constraints for this operand. */
2773 switch (opc) {
2774 case INDEX_op_mov_i32:
2775 case INDEX_op_mov_i64:
2776 /* Note that these are TCG_OPF_NOT_PRESENT and do not
2777 have proper constraints. That said, special case
2778 moves to propagate preferences backward. */
2779 if (IS_DEAD_ARG(1)) {
2780 *la_temp_pref(arg_temp(op->args[0]))
2781 = *la_temp_pref(arg_temp(op->args[1]));
2782 }
2783 break;
2784
2785 default:
2786 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2787 const TCGArgConstraint *ct = &def->args_ct[i];
2788 TCGRegSet set, *pset;
2789
2790 ts = arg_temp(op->args[i]);
2791 pset = la_temp_pref(ts);
2792 set = *pset;
2793
2794 set &= ct->u.regs;
2795 if (ct->ct & TCG_CT_IALIAS) {
2796 set &= op->output_pref[ct->alias_index];
2797 }
2798 /* If the combination is not possible, restart. */
2799 if (set == 0) {
2800 set = ct->u.regs;
2801 }
2802 *pset = set;
2803 }
2804 break;
152c35aa 2805 }
c896fe29
FB
2806 break;
2807 }
bee158cb 2808 op->life = arg_life;
1ff0a2c5 2809 }
c896fe29 2810}
c896fe29 2811
5a18407f 2812/* Liveness analysis: Convert indirect regs to direct temporaries. */
b83eabea 2813static bool liveness_pass_2(TCGContext *s)
5a18407f
RH
2814{
2815 int nb_globals = s->nb_globals;
15fa08f8 2816 int nb_temps, i;
5a18407f 2817 bool changes = false;
15fa08f8 2818 TCGOp *op, *op_next;
5a18407f 2819
5a18407f
RH
2820 /* Create a temporary for each indirect global. */
2821 for (i = 0; i < nb_globals; ++i) {
2822 TCGTemp *its = &s->temps[i];
2823 if (its->indirect_reg) {
2824 TCGTemp *dts = tcg_temp_alloc(s);
2825 dts->type = its->type;
2826 dts->base_type = its->base_type;
b83eabea
RH
2827 its->state_ptr = dts;
2828 } else {
2829 its->state_ptr = NULL;
5a18407f 2830 }
b83eabea
RH
2831 /* All globals begin dead. */
2832 its->state = TS_DEAD;
2833 }
2834 for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
2835 TCGTemp *its = &s->temps[i];
2836 its->state_ptr = NULL;
2837 its->state = TS_DEAD;
5a18407f 2838 }
5a18407f 2839
15fa08f8 2840 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
5a18407f
RH
2841 TCGOpcode opc = op->opc;
2842 const TCGOpDef *def = &tcg_op_defs[opc];
2843 TCGLifeData arg_life = op->life;
2844 int nb_iargs, nb_oargs, call_flags;
b83eabea 2845 TCGTemp *arg_ts, *dir_ts;
5a18407f 2846
5a18407f 2847 if (opc == INDEX_op_call) {
cd9090aa
RH
2848 nb_oargs = TCGOP_CALLO(op);
2849 nb_iargs = TCGOP_CALLI(op);
efee3746 2850 call_flags = op->args[nb_oargs + nb_iargs + 1];
5a18407f
RH
2851 } else {
2852 nb_iargs = def->nb_iargs;
2853 nb_oargs = def->nb_oargs;
2854
2855 /* Set flags similar to how calls require. */
2856 if (def->flags & TCG_OPF_BB_END) {
2857 /* Like writing globals: save_globals */
2858 call_flags = 0;
2859 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
2860 /* Like reading globals: sync_globals */
2861 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
2862 } else {
2863 /* No effect on globals. */
2864 call_flags = (TCG_CALL_NO_READ_GLOBALS |
2865 TCG_CALL_NO_WRITE_GLOBALS);
2866 }
2867 }
2868
2869 /* Make sure that input arguments are available. */
2870 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
b83eabea
RH
2871 arg_ts = arg_temp(op->args[i]);
2872 if (arg_ts) {
2873 dir_ts = arg_ts->state_ptr;
2874 if (dir_ts && arg_ts->state == TS_DEAD) {
2875 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
5a18407f
RH
2876 ? INDEX_op_ld_i32
2877 : INDEX_op_ld_i64);
ac1043f6 2878 TCGOp *lop = tcg_op_insert_before(s, op, lopc);
5a18407f 2879
b83eabea
RH
2880 lop->args[0] = temp_arg(dir_ts);
2881 lop->args[1] = temp_arg(arg_ts->mem_base);
2882 lop->args[2] = arg_ts->mem_offset;
5a18407f
RH
2883
2884 /* Loaded, but synced with memory. */
b83eabea 2885 arg_ts->state = TS_MEM;
5a18407f
RH
2886 }
2887 }
2888 }
2889
2890 /* Perform input replacement, and mark inputs that became dead.
2891 No action is required except keeping temp_state up to date
2892 so that we reload when needed. */
2893 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
b83eabea
RH
2894 arg_ts = arg_temp(op->args[i]);
2895 if (arg_ts) {
2896 dir_ts = arg_ts->state_ptr;
2897 if (dir_ts) {
2898 op->args[i] = temp_arg(dir_ts);
5a18407f
RH
2899 changes = true;
2900 if (IS_DEAD_ARG(i)) {
b83eabea 2901 arg_ts->state = TS_DEAD;
5a18407f
RH
2902 }
2903 }
2904 }
2905 }
2906
2907 /* Liveness analysis should ensure that the following are
2908 all correct, for call sites and basic block end points. */
2909 if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
2910 /* Nothing to do */
2911 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
2912 for (i = 0; i < nb_globals; ++i) {
2913 /* Liveness should see that globals are synced back,
2914 that is, either TS_DEAD or TS_MEM. */
b83eabea
RH
2915 arg_ts = &s->temps[i];
2916 tcg_debug_assert(arg_ts->state_ptr == 0
2917 || arg_ts->state != 0);
5a18407f
RH
2918 }
2919 } else {
2920 for (i = 0; i < nb_globals; ++i) {
2921 /* Liveness should see that globals are saved back,
2922 that is, TS_DEAD, waiting to be reloaded. */
b83eabea
RH
2923 arg_ts = &s->temps[i];
2924 tcg_debug_assert(arg_ts->state_ptr == 0
2925 || arg_ts->state == TS_DEAD);
5a18407f
RH
2926 }
2927 }
2928
2929 /* Outputs become available. */
2930 for (i = 0; i < nb_oargs; i++) {
b83eabea
RH
2931 arg_ts = arg_temp(op->args[i]);
2932 dir_ts = arg_ts->state_ptr;
2933 if (!dir_ts) {
5a18407f
RH
2934 continue;
2935 }
b83eabea 2936 op->args[i] = temp_arg(dir_ts);
5a18407f
RH
2937 changes = true;
2938
2939 /* The output is now live and modified. */
b83eabea 2940 arg_ts->state = 0;
5a18407f
RH
2941
2942 /* Sync outputs upon their last write. */
2943 if (NEED_SYNC_ARG(i)) {
b83eabea 2944 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
5a18407f
RH
2945 ? INDEX_op_st_i32
2946 : INDEX_op_st_i64);
ac1043f6 2947 TCGOp *sop = tcg_op_insert_after(s, op, sopc);
5a18407f 2948
b83eabea
RH
2949 sop->args[0] = temp_arg(dir_ts);
2950 sop->args[1] = temp_arg(arg_ts->mem_base);
2951 sop->args[2] = arg_ts->mem_offset;
5a18407f 2952
b83eabea 2953 arg_ts->state = TS_MEM;
5a18407f
RH
2954 }
2955 /* Drop outputs that are dead. */
2956 if (IS_DEAD_ARG(i)) {
b83eabea 2957 arg_ts->state = TS_DEAD;
5a18407f
RH
2958 }
2959 }
2960 }
2961
2962 return changes;
2963}
2964
8d8fdbae 2965#ifdef CONFIG_DEBUG_TCG
c896fe29
FB
2966static void dump_regs(TCGContext *s)
2967{
2968 TCGTemp *ts;
2969 int i;
2970 char buf[64];
2971
2972 for(i = 0; i < s->nb_temps; i++) {
2973 ts = &s->temps[i];
43439139 2974 printf(" %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
c896fe29
FB
2975 switch(ts->val_type) {
2976 case TEMP_VAL_REG:
2977 printf("%s", tcg_target_reg_names[ts->reg]);
2978 break;
2979 case TEMP_VAL_MEM:
b3a62939
RH
2980 printf("%d(%s)", (int)ts->mem_offset,
2981 tcg_target_reg_names[ts->mem_base->reg]);
c896fe29
FB
2982 break;
2983 case TEMP_VAL_CONST:
2984 printf("$0x%" TCG_PRIlx, ts->val);
2985 break;
2986 case TEMP_VAL_DEAD:
2987 printf("D");
2988 break;
2989 default:
2990 printf("???");
2991 break;
2992 }
2993 printf("\n");
2994 }
2995
2996 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
f8b2f202 2997 if (s->reg_to_temp[i] != NULL) {
c896fe29
FB
2998 printf("%s: %s\n",
2999 tcg_target_reg_names[i],
f8b2f202 3000 tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
c896fe29
FB
3001 }
3002 }
3003}
3004
3005static void check_regs(TCGContext *s)
3006{
869938ae 3007 int reg;
b6638662 3008 int k;
c896fe29
FB
3009 TCGTemp *ts;
3010 char buf[64];
3011
f8b2f202
RH
3012 for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
3013 ts = s->reg_to_temp[reg];
3014 if (ts != NULL) {
3015 if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
c896fe29
FB
3016 printf("Inconsistency for register %s:\n",
3017 tcg_target_reg_names[reg]);
b03cce8e 3018 goto fail;
c896fe29
FB
3019 }
3020 }
3021 }
f8b2f202 3022 for (k = 0; k < s->nb_temps; k++) {
c896fe29 3023 ts = &s->temps[k];
f8b2f202
RH
3024 if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg
3025 && s->reg_to_temp[ts->reg] != ts) {
3026 printf("Inconsistency for temp %s:\n",
3027 tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
b03cce8e 3028 fail:
f8b2f202
RH
3029 printf("reg state:\n");
3030 dump_regs(s);
3031 tcg_abort();
c896fe29
FB
3032 }
3033 }
3034}
3035#endif
3036
2272e4a7 3037static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
c896fe29 3038{
9b9c37c3
RH
3039#if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
3040 /* Sparc64 stack is accessed with offset of 2047 */
b591dc59
BS
3041 s->current_frame_offset = (s->current_frame_offset +
3042 (tcg_target_long)sizeof(tcg_target_long) - 1) &
3043 ~(sizeof(tcg_target_long) - 1);
f44c9960 3044#endif
b591dc59
BS
3045 if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
3046 s->frame_end) {
5ff9d6a4 3047 tcg_abort();
b591dc59 3048 }
c896fe29 3049 ts->mem_offset = s->current_frame_offset;
b3a62939 3050 ts->mem_base = s->frame_temp;
c896fe29 3051 ts->mem_allocated = 1;
e2c6d1b4 3052 s->current_frame_offset += sizeof(tcg_target_long);
c896fe29
FB
3053}
3054
b722452a 3055static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
b3915dbb 3056
59d7c14e
RH
3057/* Mark a temporary as free or dead. If 'free_or_dead' is negative,
3058 mark it free; otherwise mark it dead. */
3059static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
7f6ceedf 3060{
59d7c14e
RH
3061 if (ts->fixed_reg) {
3062 return;
3063 }
3064 if (ts->val_type == TEMP_VAL_REG) {
3065 s->reg_to_temp[ts->reg] = NULL;
3066 }
3067 ts->val_type = (free_or_dead < 0
3068 || ts->temp_local
fa477d25 3069 || ts->temp_global
59d7c14e
RH
3070 ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
3071}
7f6ceedf 3072
59d7c14e
RH
3073/* Mark a temporary as dead. */
3074static inline void temp_dead(TCGContext *s, TCGTemp *ts)
3075{
3076 temp_free_or_dead(s, ts, 1);
3077}
3078
3079/* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
3080 registers needs to be allocated to store a constant. If 'free_or_dead'
3081 is non-zero, subsequently release the temporary; if it is positive, the
3082 temp is dead; if it is negative, the temp is free. */
98b4e186
RH
3083static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
3084 TCGRegSet preferred_regs, int free_or_dead)
59d7c14e
RH
3085{
3086 if (ts->fixed_reg) {
3087 return;
3088 }
3089 if (!ts->mem_coherent) {
7f6ceedf 3090 if (!ts->mem_allocated) {
2272e4a7 3091 temp_allocate_frame(s, ts);
59d7c14e 3092 }
59d7c14e
RH
3093 switch (ts->val_type) {
3094 case TEMP_VAL_CONST:
3095 /* If we're going to free the temp immediately, then we won't
3096 require it later in a register, so attempt to store the
3097 constant to memory directly. */
3098 if (free_or_dead
3099 && tcg_out_sti(s, ts->type, ts->val,
3100 ts->mem_base->reg, ts->mem_offset)) {
3101 break;
3102 }
3103 temp_load(s, ts, tcg_target_available_regs[ts->type],
98b4e186 3104 allocated_regs, preferred_regs);
59d7c14e
RH
3105 /* fallthrough */
3106
3107 case TEMP_VAL_REG:
3108 tcg_out_st(s, ts->type, ts->reg,
3109 ts->mem_base->reg, ts->mem_offset);
3110 break;
3111
3112 case TEMP_VAL_MEM:
3113 break;
3114
3115 case TEMP_VAL_DEAD:
3116 default:
3117 tcg_abort();
3118 }
3119 ts->mem_coherent = 1;
3120 }
3121 if (free_or_dead) {
3122 temp_free_or_dead(s, ts, free_or_dead);
7f6ceedf 3123 }
7f6ceedf
AJ
3124}
3125
c896fe29 3126/* free register 'reg' by spilling the corresponding temporary if necessary */
b3915dbb 3127static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
c896fe29 3128{
f8b2f202 3129 TCGTemp *ts = s->reg_to_temp[reg];
f8b2f202 3130 if (ts != NULL) {
98b4e186 3131 temp_sync(s, ts, allocated_regs, 0, -1);
c896fe29
FB
3132 }
3133}
3134
b016486e
RH
3135/**
3136 * tcg_reg_alloc:
3137 * @required_regs: Set of registers in which we must allocate.
3138 * @allocated_regs: Set of registers which must be avoided.
3139 * @preferred_regs: Set of registers we should prefer.
3140 * @rev: True if we search the registers in "indirect" order.
3141 *
3142 * The allocated register must be in @required_regs & ~@allocated_regs,
3143 * but if we can put it in @preferred_regs we may save a move later.
3144 */
3145static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
3146 TCGRegSet allocated_regs,
3147 TCGRegSet preferred_regs, bool rev)
c896fe29 3148{
b016486e
RH
3149 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
3150 TCGRegSet reg_ct[2];
91478cef 3151 const int *order;
c896fe29 3152
b016486e
RH
3153 reg_ct[1] = required_regs & ~allocated_regs;
3154 tcg_debug_assert(reg_ct[1] != 0);
3155 reg_ct[0] = reg_ct[1] & preferred_regs;
3156
3157 /* Skip the preferred_regs option if it cannot be satisfied,
3158 or if the preference made no difference. */
3159 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
3160
91478cef 3161 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
c896fe29 3162
b016486e
RH
3163 /* Try free registers, preferences first. */
3164 for (j = f; j < 2; j++) {
3165 TCGRegSet set = reg_ct[j];
3166
3167 if (tcg_regset_single(set)) {
3168 /* One register in the set. */
3169 TCGReg reg = tcg_regset_first(set);
3170 if (s->reg_to_temp[reg] == NULL) {
3171 return reg;
3172 }
3173 } else {
3174 for (i = 0; i < n; i++) {
3175 TCGReg reg = order[i];
3176 if (s->reg_to_temp[reg] == NULL &&
3177 tcg_regset_test_reg(set, reg)) {
3178 return reg;
3179 }
3180 }
3181 }
c896fe29
FB
3182 }
3183
b016486e
RH
3184 /* We must spill something. */
3185 for (j = f; j < 2; j++) {
3186 TCGRegSet set = reg_ct[j];
3187
3188 if (tcg_regset_single(set)) {
3189 /* One register in the set. */
3190 TCGReg reg = tcg_regset_first(set);
b3915dbb 3191 tcg_reg_free(s, reg, allocated_regs);
c896fe29 3192 return reg;
b016486e
RH
3193 } else {
3194 for (i = 0; i < n; i++) {
3195 TCGReg reg = order[i];
3196 if (tcg_regset_test_reg(set, reg)) {
3197 tcg_reg_free(s, reg, allocated_regs);
3198 return reg;
3199 }
3200 }
c896fe29
FB
3201 }
3202 }
3203
3204 tcg_abort();
3205}
3206
40ae5c62
RH
3207/* Make sure the temporary is in a register. If needed, allocate the register
3208 from DESIRED while avoiding ALLOCATED. */
3209static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
b722452a 3210 TCGRegSet allocated_regs, TCGRegSet preferred_regs)
40ae5c62
RH
3211{
3212 TCGReg reg;
3213
3214 switch (ts->val_type) {
3215 case TEMP_VAL_REG:
3216 return;
3217 case TEMP_VAL_CONST:
b016486e 3218 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
b722452a 3219 preferred_regs, ts->indirect_base);
40ae5c62
RH
3220 tcg_out_movi(s, ts->type, reg, ts->val);
3221 ts->mem_coherent = 0;
3222 break;
3223 case TEMP_VAL_MEM:
b016486e 3224 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
b722452a 3225 preferred_regs, ts->indirect_base);
40ae5c62
RH
3226 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
3227 ts->mem_coherent = 1;
3228 break;
3229 case TEMP_VAL_DEAD:
3230 default:
3231 tcg_abort();
3232 }
3233 ts->reg = reg;
3234 ts->val_type = TEMP_VAL_REG;
3235 s->reg_to_temp[reg] = ts;
3236}
3237
59d7c14e
RH
3238/* Save a temporary to memory. 'allocated_regs' is used in case a
3239 temporary registers needs to be allocated to store a constant. */
3240static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
1ad80729 3241{
5a18407f
RH
3242 /* The liveness analysis already ensures that globals are back
3243 in memory. Keep an tcg_debug_assert for safety. */
3244 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg);
1ad80729
AJ
3245}
3246
9814dd27 3247/* save globals to their canonical location and assume they can be
e8996ee0
FB
3248 modified be the following code. 'allocated_regs' is used in case a
3249 temporary registers needs to be allocated to store a constant. */
3250static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
c896fe29 3251{
ac3b8891 3252 int i, n;
c896fe29 3253
ac3b8891 3254 for (i = 0, n = s->nb_globals; i < n; i++) {
b13eb728 3255 temp_save(s, &s->temps[i], allocated_regs);
c896fe29 3256 }
e5097dc8
FB
3257}
3258
3d5c5f87
AJ
3259/* sync globals to their canonical location and assume they can be
3260 read by the following code. 'allocated_regs' is used in case a
3261 temporary registers needs to be allocated to store a constant. */
3262static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
3263{
ac3b8891 3264 int i, n;
3d5c5f87 3265
ac3b8891 3266 for (i = 0, n = s->nb_globals; i < n; i++) {
12b9b11a 3267 TCGTemp *ts = &s->temps[i];
5a18407f
RH
3268 tcg_debug_assert(ts->val_type != TEMP_VAL_REG
3269 || ts->fixed_reg
3270 || ts->mem_coherent);
3d5c5f87
AJ
3271 }
3272}
3273
e5097dc8 3274/* at the end of a basic block, we assume all temporaries are dead and
e8996ee0
FB
3275 all globals are stored at their canonical location. */
3276static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
e5097dc8 3277{
e5097dc8
FB
3278 int i;
3279
b13eb728
RH
3280 for (i = s->nb_globals; i < s->nb_temps; i++) {
3281 TCGTemp *ts = &s->temps[i];
641d5fbe 3282 if (ts->temp_local) {
b13eb728 3283 temp_save(s, ts, allocated_regs);
641d5fbe 3284 } else {
5a18407f
RH
3285 /* The liveness analysis already ensures that temps are dead.
3286 Keep an tcg_debug_assert for safety. */
3287 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
c896fe29
FB
3288 }
3289 }
e8996ee0
FB
3290
3291 save_globals(s, allocated_regs);
c896fe29
FB
3292}
3293
bab1671f
RH
3294/*
3295 * Specialized code generation for INDEX_op_movi_*.
3296 */
0fe4fca4 3297static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
ba87719c
RH
3298 tcg_target_ulong val, TCGLifeData arg_life,
3299 TCGRegSet preferred_regs)
e8996ee0 3300{
d63e3b6e
RH
3301 /* ENV should not be modified. */
3302 tcg_debug_assert(!ots->fixed_reg);
59d7c14e
RH
3303
3304 /* The movi is not explicitly generated here. */
3305 if (ots->val_type == TEMP_VAL_REG) {
3306 s->reg_to_temp[ots->reg] = NULL;
ec7a869d 3307 }
59d7c14e
RH
3308 ots->val_type = TEMP_VAL_CONST;
3309 ots->val = val;
3310 ots->mem_coherent = 0;
3311 if (NEED_SYNC_ARG(0)) {
ba87719c 3312 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
59d7c14e 3313 } else if (IS_DEAD_ARG(0)) {
f8bf00f1 3314 temp_dead(s, ots);
4c4e1ab2 3315 }
e8996ee0
FB
3316}
3317
dd186292 3318static void tcg_reg_alloc_movi(TCGContext *s, const TCGOp *op)
0fe4fca4 3319{
43439139 3320 TCGTemp *ots = arg_temp(op->args[0]);
dd186292 3321 tcg_target_ulong val = op->args[1];
0fe4fca4 3322
69e3706d 3323 tcg_reg_alloc_do_movi(s, ots, val, op->life, op->output_pref[0]);
0fe4fca4
PB
3324}
3325
bab1671f
RH
3326/*
3327 * Specialized code generation for INDEX_op_mov_*.
3328 */
dd186292 3329static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
c896fe29 3330{
dd186292 3331 const TCGLifeData arg_life = op->life;
69e3706d 3332 TCGRegSet allocated_regs, preferred_regs;
c896fe29 3333 TCGTemp *ts, *ots;
450445d5 3334 TCGType otype, itype;
c896fe29 3335
d21369f5 3336 allocated_regs = s->reserved_regs;
69e3706d 3337 preferred_regs = op->output_pref[0];
43439139
RH
3338 ots = arg_temp(op->args[0]);
3339 ts = arg_temp(op->args[1]);
450445d5 3340
d63e3b6e
RH
3341 /* ENV should not be modified. */
3342 tcg_debug_assert(!ots->fixed_reg);
3343
450445d5
RH
3344 /* Note that otype != itype for no-op truncation. */
3345 otype = ots->type;
3346 itype = ts->type;
c29c1d7e 3347
0fe4fca4
PB
3348 if (ts->val_type == TEMP_VAL_CONST) {
3349 /* propagate constant or generate sti */
3350 tcg_target_ulong val = ts->val;
3351 if (IS_DEAD_ARG(1)) {
3352 temp_dead(s, ts);
3353 }
69e3706d 3354 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
0fe4fca4
PB
3355 return;
3356 }
3357
3358 /* If the source value is in memory we're going to be forced
3359 to have it in a register in order to perform the copy. Copy
3360 the SOURCE value into its own register first, that way we
3361 don't have to reload SOURCE the next time it is used. */
3362 if (ts->val_type == TEMP_VAL_MEM) {
69e3706d
RH
3363 temp_load(s, ts, tcg_target_available_regs[itype],
3364 allocated_regs, preferred_regs);
c29c1d7e 3365 }
c896fe29 3366
0fe4fca4 3367 tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
d63e3b6e 3368 if (IS_DEAD_ARG(0)) {
c29c1d7e
AJ
3369 /* mov to a non-saved dead register makes no sense (even with
3370 liveness analysis disabled). */
eabb7b91 3371 tcg_debug_assert(NEED_SYNC_ARG(0));
c29c1d7e 3372 if (!ots->mem_allocated) {
2272e4a7 3373 temp_allocate_frame(s, ots);
c29c1d7e 3374 }
b3a62939 3375 tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset);
c29c1d7e 3376 if (IS_DEAD_ARG(1)) {
f8bf00f1 3377 temp_dead(s, ts);
c29c1d7e 3378 }
f8bf00f1 3379 temp_dead(s, ots);
c29c1d7e 3380 } else {
d63e3b6e 3381 if (IS_DEAD_ARG(1) && !ts->fixed_reg) {
c896fe29 3382 /* the mov can be suppressed */
c29c1d7e 3383 if (ots->val_type == TEMP_VAL_REG) {
f8b2f202 3384 s->reg_to_temp[ots->reg] = NULL;
c29c1d7e
AJ
3385 }
3386 ots->reg = ts->reg;
f8bf00f1 3387 temp_dead(s, ts);
c896fe29 3388 } else {
c29c1d7e
AJ
3389 if (ots->val_type != TEMP_VAL_REG) {
3390 /* When allocating a new register, make sure to not spill the
3391 input one. */
3392 tcg_regset_set_reg(allocated_regs, ts->reg);
450445d5 3393 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
69e3706d 3394 allocated_regs, preferred_regs,
b016486e 3395 ots->indirect_base);
c896fe29 3396 }
78113e83 3397 if (!tcg_out_mov(s, otype, ots->reg, ts->reg)) {
240c08d0
RH
3398 /*
3399 * Cross register class move not supported.
3400 * Store the source register into the destination slot
3401 * and leave the destination temp as TEMP_VAL_MEM.
3402 */
3403 assert(!ots->fixed_reg);
3404 if (!ts->mem_allocated) {
3405 temp_allocate_frame(s, ots);
3406 }
3407 tcg_out_st(s, ts->type, ts->reg,
3408 ots->mem_base->reg, ots->mem_offset);
3409 ots->mem_coherent = 1;
3410 temp_free_or_dead(s, ots, -1);
3411 return;
78113e83 3412 }
c896fe29 3413 }
c29c1d7e
AJ
3414 ots->val_type = TEMP_VAL_REG;
3415 ots->mem_coherent = 0;
f8b2f202 3416 s->reg_to_temp[ots->reg] = ots;
c29c1d7e 3417 if (NEED_SYNC_ARG(0)) {
98b4e186 3418 temp_sync(s, ots, allocated_regs, 0, 0);
c896fe29 3419 }
ec7a869d 3420 }
c896fe29
FB
3421}
3422
bab1671f
RH
3423/*
3424 * Specialized code generation for INDEX_op_dup_vec.
3425 */
3426static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)
3427{
3428 const TCGLifeData arg_life = op->life;
3429 TCGRegSet dup_out_regs, dup_in_regs;
3430 TCGTemp *its, *ots;
3431 TCGType itype, vtype;
d6ecb4a9 3432 intptr_t endian_fixup;
bab1671f
RH
3433 unsigned vece;
3434 bool ok;
3435
3436 ots = arg_temp(op->args[0]);
3437 its = arg_temp(op->args[1]);
3438
3439 /* ENV should not be modified. */
3440 tcg_debug_assert(!ots->fixed_reg);
3441
3442 itype = its->type;
3443 vece = TCGOP_VECE(op);
3444 vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
3445
3446 if (its->val_type == TEMP_VAL_CONST) {
3447 /* Propagate constant via movi -> dupi. */
3448 tcg_target_ulong val = its->val;
3449 if (IS_DEAD_ARG(1)) {
3450 temp_dead(s, its);
3451 }
3452 tcg_reg_alloc_do_movi(s, ots, val, arg_life, op->output_pref[0]);
3453 return;
3454 }
3455
3456 dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].u.regs;
3457 dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].u.regs;
3458
3459 /* Allocate the output register now. */
3460 if (ots->val_type != TEMP_VAL_REG) {
3461 TCGRegSet allocated_regs = s->reserved_regs;
3462
3463 if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) {
3464 /* Make sure to not spill the input register. */
3465 tcg_regset_set_reg(allocated_regs, its->reg);
3466 }
3467 ots->reg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
3468 op->output_pref[0], ots->indirect_base);
3469 ots->val_type = TEMP_VAL_REG;
3470 ots->mem_coherent = 0;
3471 s->reg_to_temp[ots->reg] = ots;
3472 }
3473
3474 switch (its->val_type) {
3475 case TEMP_VAL_REG:
3476 /*
3477 * The dup constriaints must be broad, covering all possible VECE.
3478 * However, tcg_op_dup_vec() gets to see the VECE and we allow it
3479 * to fail, indicating that extra moves are required for that case.
3480 */
3481 if (tcg_regset_test_reg(dup_in_regs, its->reg)) {
3482 if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) {
3483 goto done;
3484 }
3485 /* Try again from memory or a vector input register. */
3486 }
3487 if (!its->mem_coherent) {
3488 /*
3489 * The input register is not synced, and so an extra store
3490 * would be required to use memory. Attempt an integer-vector
3491 * register move first. We do not have a TCGRegSet for this.
3492 */
3493 if (tcg_out_mov(s, itype, ots->reg, its->reg)) {
3494 break;
3495 }
3496 /* Sync the temp back to its slot and load from there. */
3497 temp_sync(s, its, s->reserved_regs, 0, 0);
3498 }
3499 /* fall through */
3500
3501 case TEMP_VAL_MEM:
d6ecb4a9
RH
3502#ifdef HOST_WORDS_BIGENDIAN
3503 endian_fixup = itype == TCG_TYPE_I32 ? 4 : 8;
3504 endian_fixup -= 1 << vece;
3505#else
3506 endian_fixup = 0;
3507#endif
3508 if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
3509 its->mem_offset + endian_fixup)) {
3510 goto done;
3511 }
bab1671f
RH
3512 tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
3513 break;
3514
3515 default:
3516 g_assert_not_reached();
3517 }
3518
3519 /* We now have a vector input register, so dup must succeed. */
3520 ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg);
3521 tcg_debug_assert(ok);
3522
3523 done:
3524 if (IS_DEAD_ARG(1)) {
3525 temp_dead(s, its);
3526 }
3527 if (NEED_SYNC_ARG(0)) {
3528 temp_sync(s, ots, s->reserved_regs, 0, 0);
3529 }
3530 if (IS_DEAD_ARG(0)) {
3531 temp_dead(s, ots);
3532 }
3533}
3534
dd186292 3535static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
c896fe29 3536{
dd186292
RH
3537 const TCGLifeData arg_life = op->life;
3538 const TCGOpDef * const def = &tcg_op_defs[op->opc];
82790a87
RH
3539 TCGRegSet i_allocated_regs;
3540 TCGRegSet o_allocated_regs;
b6638662
RH
3541 int i, k, nb_iargs, nb_oargs;
3542 TCGReg reg;
c896fe29
FB
3543 TCGArg arg;
3544 const TCGArgConstraint *arg_ct;
3545 TCGTemp *ts;
3546 TCGArg new_args[TCG_MAX_OP_ARGS];
3547 int const_args[TCG_MAX_OP_ARGS];
3548
3549 nb_oargs = def->nb_oargs;
3550 nb_iargs = def->nb_iargs;
3551
3552 /* copy constants */
3553 memcpy(new_args + nb_oargs + nb_iargs,
dd186292 3554 op->args + nb_oargs + nb_iargs,
c896fe29
FB
3555 sizeof(TCGArg) * def->nb_cargs);
3556
d21369f5
RH
3557 i_allocated_regs = s->reserved_regs;
3558 o_allocated_regs = s->reserved_regs;
82790a87 3559
c896fe29 3560 /* satisfy input constraints */
dd186292 3561 for (k = 0; k < nb_iargs; k++) {
d62816f2
RH
3562 TCGRegSet i_preferred_regs, o_preferred_regs;
3563
c896fe29 3564 i = def->sorted_args[nb_oargs + k];
dd186292 3565 arg = op->args[i];
c896fe29 3566 arg_ct = &def->args_ct[i];
43439139 3567 ts = arg_temp(arg);
40ae5c62
RH
3568
3569 if (ts->val_type == TEMP_VAL_CONST
3570 && tcg_target_const_match(ts->val, ts->type, arg_ct)) {
3571 /* constant is OK for instruction */
3572 const_args[i] = 1;
3573 new_args[i] = ts->val;
d62816f2 3574 continue;
c896fe29 3575 }
40ae5c62 3576
d62816f2 3577 i_preferred_regs = o_preferred_regs = 0;
5ff9d6a4 3578 if (arg_ct->ct & TCG_CT_IALIAS) {
d62816f2 3579 o_preferred_regs = op->output_pref[arg_ct->alias_index];
5ff9d6a4
FB
3580 if (ts->fixed_reg) {
3581 /* if fixed register, we must allocate a new register
3582 if the alias is not the same register */
d62816f2 3583 if (arg != op->args[arg_ct->alias_index]) {
5ff9d6a4 3584 goto allocate_in_reg;
d62816f2 3585 }
5ff9d6a4
FB
3586 } else {
3587 /* if the input is aliased to an output and if it is
3588 not dead after the instruction, we must allocate
3589 a new register and move it */
866cb6cb 3590 if (!IS_DEAD_ARG(i)) {
5ff9d6a4 3591 goto allocate_in_reg;
866cb6cb 3592 }
d62816f2 3593
7e1df267
AJ
3594 /* check if the current register has already been allocated
3595 for another input aliased to an output */
d62816f2
RH
3596 if (ts->val_type == TEMP_VAL_REG) {
3597 int k2, i2;
3598 reg = ts->reg;
3599 for (k2 = 0 ; k2 < k ; k2++) {
3600 i2 = def->sorted_args[nb_oargs + k2];
3601 if ((def->args_ct[i2].ct & TCG_CT_IALIAS) &&
3602 reg == new_args[i2]) {
3603 goto allocate_in_reg;
3604 }
7e1df267
AJ
3605 }
3606 }
d62816f2 3607 i_preferred_regs = o_preferred_regs;
5ff9d6a4 3608 }
c896fe29 3609 }
d62816f2
RH
3610
3611 temp_load(s, ts, arg_ct->u.regs, i_allocated_regs, i_preferred_regs);
c896fe29 3612 reg = ts->reg;
d62816f2 3613
c896fe29
FB
3614 if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
3615 /* nothing to do : the constraint is satisfied */
3616 } else {
3617 allocate_in_reg:
3618 /* allocate a new register matching the constraint
3619 and move the temporary register into it */
d62816f2
RH
3620 temp_load(s, ts, tcg_target_available_regs[ts->type],
3621 i_allocated_regs, 0);
82790a87 3622 reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs,
d62816f2 3623 o_preferred_regs, ts->indirect_base);
78113e83 3624 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
240c08d0
RH
3625 /*
3626 * Cross register class move not supported. Sync the
3627 * temp back to its slot and load from there.
3628 */
3629 temp_sync(s, ts, i_allocated_regs, 0, 0);
3630 tcg_out_ld(s, ts->type, reg,
3631 ts->mem_base->reg, ts->mem_offset);
78113e83 3632 }
c896fe29 3633 }
c896fe29
FB
3634 new_args[i] = reg;
3635 const_args[i] = 0;
82790a87 3636 tcg_regset_set_reg(i_allocated_regs, reg);
c896fe29
FB
3637 }
3638
a52ad07e
AJ
3639 /* mark dead temporaries and free the associated registers */
3640 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3641 if (IS_DEAD_ARG(i)) {
43439139 3642 temp_dead(s, arg_temp(op->args[i]));
a52ad07e
AJ
3643 }
3644 }
3645
e8996ee0 3646 if (def->flags & TCG_OPF_BB_END) {
82790a87 3647 tcg_reg_alloc_bb_end(s, i_allocated_regs);
e8996ee0 3648 } else {
e8996ee0
FB
3649 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3650 /* XXX: permit generic clobber register list ? */
c8074023
RH
3651 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
3652 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
82790a87 3653 tcg_reg_free(s, i, i_allocated_regs);
e8996ee0 3654 }
c896fe29 3655 }
3d5c5f87
AJ
3656 }
3657 if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3658 /* sync globals if the op has side effects and might trigger
3659 an exception. */
82790a87 3660 sync_globals(s, i_allocated_regs);
c896fe29 3661 }
e8996ee0
FB
3662
3663 /* satisfy the output constraints */
e8996ee0
FB
3664 for(k = 0; k < nb_oargs; k++) {
3665 i = def->sorted_args[k];
dd186292 3666 arg = op->args[i];
e8996ee0 3667 arg_ct = &def->args_ct[i];
43439139 3668 ts = arg_temp(arg);
d63e3b6e
RH
3669
3670 /* ENV should not be modified. */
3671 tcg_debug_assert(!ts->fixed_reg);
3672
17280ff4
RH
3673 if ((arg_ct->ct & TCG_CT_ALIAS)
3674 && !const_args[arg_ct->alias_index]) {
e8996ee0 3675 reg = new_args[arg_ct->alias_index];
82790a87
RH
3676 } else if (arg_ct->ct & TCG_CT_NEWREG) {
3677 reg = tcg_reg_alloc(s, arg_ct->u.regs,
3678 i_allocated_regs | o_allocated_regs,
69e3706d 3679 op->output_pref[k], ts->indirect_base);
e8996ee0 3680 } else {
82790a87 3681 reg = tcg_reg_alloc(s, arg_ct->u.regs, o_allocated_regs,
69e3706d 3682 op->output_pref[k], ts->indirect_base);
c896fe29 3683 }
82790a87 3684 tcg_regset_set_reg(o_allocated_regs, reg);
d63e3b6e
RH
3685 if (ts->val_type == TEMP_VAL_REG) {
3686 s->reg_to_temp[ts->reg] = NULL;
e8996ee0 3687 }
d63e3b6e
RH
3688 ts->val_type = TEMP_VAL_REG;
3689 ts->reg = reg;
3690 /*
3691 * Temp value is modified, so the value kept in memory is
3692 * potentially not the same.
3693 */
3694 ts->mem_coherent = 0;
3695 s->reg_to_temp[reg] = ts;
e8996ee0 3696 new_args[i] = reg;
c896fe29 3697 }
c896fe29
FB
3698 }
3699
c896fe29 3700 /* emit instruction */
d2fd745f
RH
3701 if (def->flags & TCG_OPF_VECTOR) {
3702 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
3703 new_args, const_args);
3704 } else {
3705 tcg_out_op(s, op->opc, new_args, const_args);
3706 }
3707
c896fe29
FB
3708 /* move the outputs in the correct register if needed */
3709 for(i = 0; i < nb_oargs; i++) {
43439139 3710 ts = arg_temp(op->args[i]);
d63e3b6e
RH
3711
3712 /* ENV should not be modified. */
3713 tcg_debug_assert(!ts->fixed_reg);
3714
ec7a869d 3715 if (NEED_SYNC_ARG(i)) {
98b4e186 3716 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
59d7c14e 3717 } else if (IS_DEAD_ARG(i)) {
f8bf00f1 3718 temp_dead(s, ts);
ec7a869d 3719 }
c896fe29
FB
3720 }
3721}
3722
b03cce8e
FB
3723#ifdef TCG_TARGET_STACK_GROWSUP
3724#define STACK_DIR(x) (-(x))
3725#else
3726#define STACK_DIR(x) (x)
3727#endif
3728
dd186292 3729static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
c896fe29 3730{
cd9090aa
RH
3731 const int nb_oargs = TCGOP_CALLO(op);
3732 const int nb_iargs = TCGOP_CALLI(op);
dd186292 3733 const TCGLifeData arg_life = op->life;
b6638662
RH
3734 int flags, nb_regs, i;
3735 TCGReg reg;
cf066674 3736 TCGArg arg;
c896fe29 3737 TCGTemp *ts;
d3452f1f
RH
3738 intptr_t stack_offset;
3739 size_t call_stack_size;
cf066674
RH
3740 tcg_insn_unit *func_addr;
3741 int allocate_args;
c896fe29 3742 TCGRegSet allocated_regs;
c896fe29 3743
dd186292
RH
3744 func_addr = (tcg_insn_unit *)(intptr_t)op->args[nb_oargs + nb_iargs];
3745 flags = op->args[nb_oargs + nb_iargs + 1];
c896fe29 3746
6e17d0c5 3747 nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
c45cb8bb
RH
3748 if (nb_regs > nb_iargs) {
3749 nb_regs = nb_iargs;
cf066674 3750 }
c896fe29
FB
3751
3752 /* assign stack slots first */
c45cb8bb 3753 call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
c896fe29
FB
3754 call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
3755 ~(TCG_TARGET_STACK_ALIGN - 1);
b03cce8e
FB
3756 allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
3757 if (allocate_args) {
345649c0
BS
3758 /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
3759 preallocate call stack */
3760 tcg_abort();
b03cce8e 3761 }
39cf05d3
FB
3762
3763 stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
dd186292
RH
3764 for (i = nb_regs; i < nb_iargs; i++) {
3765 arg = op->args[nb_oargs + i];
39cf05d3
FB
3766#ifdef TCG_TARGET_STACK_GROWSUP
3767 stack_offset -= sizeof(tcg_target_long);
3768#endif
3769 if (arg != TCG_CALL_DUMMY_ARG) {
43439139 3770 ts = arg_temp(arg);
40ae5c62 3771 temp_load(s, ts, tcg_target_available_regs[ts->type],
b722452a 3772 s->reserved_regs, 0);
40ae5c62 3773 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
c896fe29 3774 }
39cf05d3
FB
3775#ifndef TCG_TARGET_STACK_GROWSUP
3776 stack_offset += sizeof(tcg_target_long);
3777#endif
c896fe29
FB
3778 }
3779
3780 /* assign input registers */
d21369f5 3781 allocated_regs = s->reserved_regs;
dd186292
RH
3782 for (i = 0; i < nb_regs; i++) {
3783 arg = op->args[nb_oargs + i];
39cf05d3 3784 if (arg != TCG_CALL_DUMMY_ARG) {
43439139 3785 ts = arg_temp(arg);
39cf05d3 3786 reg = tcg_target_call_iarg_regs[i];
40ae5c62 3787
39cf05d3
FB
3788 if (ts->val_type == TEMP_VAL_REG) {
3789 if (ts->reg != reg) {
4250da10 3790 tcg_reg_free(s, reg, allocated_regs);
78113e83 3791 if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
240c08d0
RH
3792 /*
3793 * Cross register class move not supported. Sync the
3794 * temp back to its slot and load from there.
3795 */
3796 temp_sync(s, ts, allocated_regs, 0, 0);
3797 tcg_out_ld(s, ts->type, reg,
3798 ts->mem_base->reg, ts->mem_offset);
78113e83 3799 }
39cf05d3 3800 }
39cf05d3 3801 } else {
ccb1bb66 3802 TCGRegSet arg_set = 0;
40ae5c62 3803
4250da10 3804 tcg_reg_free(s, reg, allocated_regs);
40ae5c62 3805 tcg_regset_set_reg(arg_set, reg);
b722452a 3806 temp_load(s, ts, arg_set, allocated_regs, 0);
c896fe29 3807 }
40ae5c62 3808
39cf05d3 3809 tcg_regset_set_reg(allocated_regs, reg);
c896fe29 3810 }
c896fe29
FB
3811 }
3812
c896fe29 3813 /* mark dead temporaries and free the associated registers */
dd186292 3814 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
866cb6cb 3815 if (IS_DEAD_ARG(i)) {
43439139 3816 temp_dead(s, arg_temp(op->args[i]));
c896fe29
FB
3817 }
3818 }
3819
3820 /* clobber call registers */
c8074023
RH
3821 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
3822 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
b3915dbb 3823 tcg_reg_free(s, i, allocated_regs);
c896fe29
FB
3824 }
3825 }
78505279
AJ
3826
3827 /* Save globals if they might be written by the helper, sync them if
3828 they might be read. */
3829 if (flags & TCG_CALL_NO_READ_GLOBALS) {
3830 /* Nothing to do */
3831 } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
3832 sync_globals(s, allocated_regs);
3833 } else {
b9c18f56
AJ
3834 save_globals(s, allocated_regs);
3835 }
c896fe29 3836
cf066674 3837 tcg_out_call(s, func_addr);
c896fe29
FB
3838
3839 /* assign output registers and emit moves if needed */
3840 for(i = 0; i < nb_oargs; i++) {
dd186292 3841 arg = op->args[i];
43439139 3842 ts = arg_temp(arg);
d63e3b6e
RH
3843
3844 /* ENV should not be modified. */
3845 tcg_debug_assert(!ts->fixed_reg);
3846
c896fe29 3847 reg = tcg_target_call_oarg_regs[i];
eabb7b91 3848 tcg_debug_assert(s->reg_to_temp[reg] == NULL);
d63e3b6e
RH
3849 if (ts->val_type == TEMP_VAL_REG) {
3850 s->reg_to_temp[ts->reg] = NULL;
3851 }
3852 ts->val_type = TEMP_VAL_REG;
3853 ts->reg = reg;
3854 ts->mem_coherent = 0;
3855 s->reg_to_temp[reg] = ts;
3856 if (NEED_SYNC_ARG(i)) {
3857 temp_sync(s, ts, allocated_regs, 0, IS_DEAD_ARG(i));
3858 } else if (IS_DEAD_ARG(i)) {
3859 temp_dead(s, ts);
c896fe29
FB
3860 }
3861 }
c896fe29
FB
3862}
3863
3864#ifdef CONFIG_PROFILER
3865
c3fac113
EC
3866/* avoid copy/paste errors */
3867#define PROF_ADD(to, from, field) \
3868 do { \
3869 (to)->field += atomic_read(&((from)->field)); \
3870 } while (0)
3871
3872#define PROF_MAX(to, from, field) \
3873 do { \
3874 typeof((from)->field) val__ = atomic_read(&((from)->field)); \
3875 if (val__ > (to)->field) { \
3876 (to)->field = val__; \
3877 } \
3878 } while (0)
3879
3880/* Pass in a zero'ed @prof */
3881static inline
3882void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
3883{
3468b59e 3884 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
c3fac113
EC
3885 unsigned int i;
3886
3468b59e
EC
3887 for (i = 0; i < n_ctxs; i++) {
3888 TCGContext *s = atomic_read(&tcg_ctxs[i]);
3889 const TCGProfile *orig = &s->prof;
c3fac113
EC
3890
3891 if (counters) {
72fd2efb 3892 PROF_ADD(prof, orig, cpu_exec_time);
c3fac113
EC
3893 PROF_ADD(prof, orig, tb_count1);
3894 PROF_ADD(prof, orig, tb_count);
3895 PROF_ADD(prof, orig, op_count);
3896 PROF_MAX(prof, orig, op_count_max);
3897 PROF_ADD(prof, orig, temp_count);
3898 PROF_MAX(prof, orig, temp_count_max);
3899 PROF_ADD(prof, orig, del_op_count);
3900 PROF_ADD(prof, orig, code_in_len);
3901 PROF_ADD(prof, orig, code_out_len);
3902 PROF_ADD(prof, orig, search_out_len);
3903 PROF_ADD(prof, orig, interm_time);
3904 PROF_ADD(prof, orig, code_time);
3905 PROF_ADD(prof, orig, la_time);
3906 PROF_ADD(prof, orig, opt_time);
3907 PROF_ADD(prof, orig, restore_count);
3908 PROF_ADD(prof, orig, restore_time);
3909 }
3910 if (table) {
3911 int i;
3912
3913 for (i = 0; i < NB_OPS; i++) {
3914 PROF_ADD(prof, orig, table_op_count[i]);
3915 }
3916 }
3917 }
3918}
3919
3920#undef PROF_ADD
3921#undef PROF_MAX
3922
3923static void tcg_profile_snapshot_counters(TCGProfile *prof)
3924{
3925 tcg_profile_snapshot(prof, true, false);
3926}
3927
3928static void tcg_profile_snapshot_table(TCGProfile *prof)
3929{
3930 tcg_profile_snapshot(prof, false, true);
3931}
c896fe29 3932
d4c51a0a 3933void tcg_dump_op_count(void)
c896fe29 3934{
c3fac113 3935 TCGProfile prof = {};
c896fe29 3936 int i;
d70724ce 3937
c3fac113 3938 tcg_profile_snapshot_table(&prof);
15fc7daa 3939 for (i = 0; i < NB_OPS; i++) {
d4c51a0a 3940 qemu_printf("%s %" PRId64 "\n", tcg_op_defs[i].name,
c3fac113 3941 prof.table_op_count[i]);
c896fe29 3942 }
c896fe29 3943}
72fd2efb
EC
3944
3945int64_t tcg_cpu_exec_time(void)
3946{
3947 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
3948 unsigned int i;
3949 int64_t ret = 0;
3950
3951 for (i = 0; i < n_ctxs; i++) {
3952 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
3953 const TCGProfile *prof = &s->prof;
3954
3955 ret += atomic_read(&prof->cpu_exec_time);
3956 }
3957 return ret;
3958}
246ae24d 3959#else
d4c51a0a 3960void tcg_dump_op_count(void)
246ae24d 3961{
d4c51a0a 3962 qemu_printf("[TCG profiler not compiled]\n");
246ae24d 3963}
72fd2efb
EC
3964
3965int64_t tcg_cpu_exec_time(void)
3966{
3967 error_report("%s: TCG profiler not compiled", __func__);
3968 exit(EXIT_FAILURE);
3969}
c896fe29
FB
3970#endif
3971
3972
5bd2ec3d 3973int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
c896fe29 3974{
c3fac113
EC
3975#ifdef CONFIG_PROFILER
3976 TCGProfile *prof = &s->prof;
3977#endif
15fa08f8
RH
3978 int i, num_insns;
3979 TCGOp *op;
c896fe29 3980
04fe6400
RH
3981#ifdef CONFIG_PROFILER
3982 {
c1f543b7 3983 int n = 0;
04fe6400 3984
15fa08f8
RH
3985 QTAILQ_FOREACH(op, &s->ops, link) {
3986 n++;
3987 }
c3fac113
EC
3988 atomic_set(&prof->op_count, prof->op_count + n);
3989 if (n > prof->op_count_max) {
3990 atomic_set(&prof->op_count_max, n);
04fe6400
RH
3991 }
3992
3993 n = s->nb_temps;
c3fac113
EC
3994 atomic_set(&prof->temp_count, prof->temp_count + n);
3995 if (n > prof->temp_count_max) {
3996 atomic_set(&prof->temp_count_max, n);
04fe6400
RH
3997 }
3998 }
3999#endif
4000
c896fe29 4001#ifdef DEBUG_DISAS
d977e1c2
AB
4002 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
4003 && qemu_log_in_addr_range(tb->pc))) {
1ee73216 4004 qemu_log_lock();
93fcfe39 4005 qemu_log("OP:\n");
1894f69a 4006 tcg_dump_ops(s, false);
93fcfe39 4007 qemu_log("\n");
1ee73216 4008 qemu_log_unlock();
c896fe29
FB
4009 }
4010#endif
4011
bef16ab4
RH
4012#ifdef CONFIG_DEBUG_TCG
4013 /* Ensure all labels referenced have been emitted. */
4014 {
4015 TCGLabel *l;
4016 bool error = false;
4017
4018 QSIMPLEQ_FOREACH(l, &s->labels, next) {
4019 if (unlikely(!l->present) && l->refs) {
4020 qemu_log_mask(CPU_LOG_TB_OP,
4021 "$L%d referenced but not present.\n", l->id);
4022 error = true;
4023 }
4024 }
4025 assert(!error);
4026 }
4027#endif
4028
c5cc28ff 4029#ifdef CONFIG_PROFILER
c3fac113 4030 atomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
c5cc28ff
AJ
4031#endif
4032
8f2e8c07 4033#ifdef USE_TCG_OPTIMIZATIONS
c45cb8bb 4034 tcg_optimize(s);
8f2e8c07
KB
4035#endif
4036
a23a9ec6 4037#ifdef CONFIG_PROFILER
c3fac113
EC
4038 atomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
4039 atomic_set(&prof->la_time, prof->la_time - profile_getclock());
a23a9ec6 4040#endif
c5cc28ff 4041
b4fc67c7 4042 reachable_code_pass(s);
b83eabea 4043 liveness_pass_1(s);
5a18407f 4044
b83eabea 4045 if (s->nb_indirects > 0) {
5a18407f 4046#ifdef DEBUG_DISAS
b83eabea
RH
4047 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
4048 && qemu_log_in_addr_range(tb->pc))) {
4049 qemu_log_lock();
4050 qemu_log("OP before indirect lowering:\n");
1894f69a 4051 tcg_dump_ops(s, false);
b83eabea
RH
4052 qemu_log("\n");
4053 qemu_log_unlock();
4054 }
5a18407f 4055#endif
b83eabea
RH
4056 /* Replace indirect temps with direct temps. */
4057 if (liveness_pass_2(s)) {
4058 /* If changes were made, re-run liveness. */
4059 liveness_pass_1(s);
5a18407f
RH
4060 }
4061 }
c5cc28ff 4062
a23a9ec6 4063#ifdef CONFIG_PROFILER
c3fac113 4064 atomic_set(&prof->la_time, prof->la_time + profile_getclock());
a23a9ec6 4065#endif
c896fe29
FB
4066
4067#ifdef DEBUG_DISAS
d977e1c2
AB
4068 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
4069 && qemu_log_in_addr_range(tb->pc))) {
1ee73216 4070 qemu_log_lock();
c5cc28ff 4071 qemu_log("OP after optimization and liveness analysis:\n");
1894f69a 4072 tcg_dump_ops(s, true);
93fcfe39 4073 qemu_log("\n");
1ee73216 4074 qemu_log_unlock();
c896fe29
FB
4075 }
4076#endif
4077
4078 tcg_reg_alloc_start(s);
4079
e7e168f4
EC
4080 s->code_buf = tb->tc.ptr;
4081 s->code_ptr = tb->tc.ptr;
c896fe29 4082
659ef5cb 4083#ifdef TCG_TARGET_NEED_LDST_LABELS
6001f772 4084 QSIMPLEQ_INIT(&s->ldst_labels);
659ef5cb 4085#endif
57a26946
RH
4086#ifdef TCG_TARGET_NEED_POOL_LABELS
4087 s->pool_labels = NULL;
4088#endif
9ecefc84 4089
fca8a500 4090 num_insns = -1;
15fa08f8 4091 QTAILQ_FOREACH(op, &s->ops, link) {
c45cb8bb 4092 TCGOpcode opc = op->opc;
b3db8758 4093
c896fe29 4094#ifdef CONFIG_PROFILER
c3fac113 4095 atomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
c896fe29 4096#endif
c45cb8bb
RH
4097
4098 switch (opc) {
c896fe29 4099 case INDEX_op_mov_i32:
c896fe29 4100 case INDEX_op_mov_i64:
d2fd745f 4101 case INDEX_op_mov_vec:
dd186292 4102 tcg_reg_alloc_mov(s, op);
c896fe29 4103 break;
e8996ee0 4104 case INDEX_op_movi_i32:
e8996ee0 4105 case INDEX_op_movi_i64:
d2fd745f 4106 case INDEX_op_dupi_vec:
dd186292 4107 tcg_reg_alloc_movi(s, op);
e8996ee0 4108 break;
bab1671f
RH
4109 case INDEX_op_dup_vec:
4110 tcg_reg_alloc_dup(s, op);
4111 break;
765b842a 4112 case INDEX_op_insn_start:
fca8a500 4113 if (num_insns >= 0) {
9f754620
RH
4114 size_t off = tcg_current_code_size(s);
4115 s->gen_insn_end_off[num_insns] = off;
4116 /* Assert that we do not overflow our stored offset. */
4117 assert(s->gen_insn_end_off[num_insns] == off);
fca8a500
RH
4118 }
4119 num_insns++;
bad729e2
RH
4120 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
4121 target_ulong a;
4122#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
efee3746 4123 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
bad729e2 4124#else
efee3746 4125 a = op->args[i];
bad729e2 4126#endif
fca8a500 4127 s->gen_insn_data[num_insns][i] = a;
bad729e2 4128 }
c896fe29 4129 break;
5ff9d6a4 4130 case INDEX_op_discard:
43439139 4131 temp_dead(s, arg_temp(op->args[0]));
5ff9d6a4 4132 break;
c896fe29 4133 case INDEX_op_set_label:
e8996ee0 4134 tcg_reg_alloc_bb_end(s, s->reserved_regs);
efee3746 4135 tcg_out_label(s, arg_label(op->args[0]), s->code_ptr);
c896fe29
FB
4136 break;
4137 case INDEX_op_call:
dd186292 4138 tcg_reg_alloc_call(s, op);
c45cb8bb 4139 break;
c896fe29 4140 default:
25c4d9cc 4141 /* Sanity check that we've not introduced any unhandled opcodes. */
be0f34b5 4142 tcg_debug_assert(tcg_op_supported(opc));
c896fe29
FB
4143 /* Note: in order to speed up the code, it would be much
4144 faster to have specialized register allocator functions for
4145 some common argument patterns */
dd186292 4146 tcg_reg_alloc_op(s, op);
c896fe29
FB
4147 break;
4148 }
8d8fdbae 4149#ifdef CONFIG_DEBUG_TCG
c896fe29
FB
4150 check_regs(s);
4151#endif
b125f9dc
RH
4152 /* Test for (pending) buffer overflow. The assumption is that any
4153 one operation beginning below the high water mark cannot overrun
4154 the buffer completely. Thus we can test for overflow after
4155 generating code without having to check during generation. */
644da9b3 4156 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
b125f9dc
RH
4157 return -1;
4158 }
6e6c4efe
RH
4159 /* Test for TB overflow, as seen by gen_insn_end_off. */
4160 if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
4161 return -2;
4162 }
c896fe29 4163 }
fca8a500
RH
4164 tcg_debug_assert(num_insns >= 0);
4165 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
c45cb8bb 4166
b76f0d8c 4167 /* Generate TB finalization at the end of block */
659ef5cb 4168#ifdef TCG_TARGET_NEED_LDST_LABELS
aeee05f5
RH
4169 i = tcg_out_ldst_finalize(s);
4170 if (i < 0) {
4171 return i;
23dceda6 4172 }
659ef5cb 4173#endif
57a26946 4174#ifdef TCG_TARGET_NEED_POOL_LABELS
1768987b
RH
4175 i = tcg_out_pool_finalize(s);
4176 if (i < 0) {
4177 return i;
57a26946
RH
4178 }
4179#endif
7ecd02a0
RH
4180 if (!tcg_resolve_relocs(s)) {
4181 return -2;
4182 }
c896fe29
FB
4183
4184 /* flush instruction cache */
1813e175 4185 flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
2aeabc08 4186
1813e175 4187 return tcg_current_code_size(s);
c896fe29
FB
4188}
4189
a23a9ec6 4190#ifdef CONFIG_PROFILER
3de2faa9 4191void tcg_dump_info(void)
a23a9ec6 4192{
c3fac113
EC
4193 TCGProfile prof = {};
4194 const TCGProfile *s;
4195 int64_t tb_count;
4196 int64_t tb_div_count;
4197 int64_t tot;
4198
4199 tcg_profile_snapshot_counters(&prof);
4200 s = &prof;
4201 tb_count = s->tb_count;
4202 tb_div_count = tb_count ? tb_count : 1;
4203 tot = s->interm_time + s->code_time;
a23a9ec6 4204
3de2faa9 4205 qemu_printf("JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
a23a9ec6 4206 tot, tot / 2.4e9);
3de2faa9
MA
4207 qemu_printf("translated TBs %" PRId64 " (aborted=%" PRId64
4208 " %0.1f%%)\n",
fca8a500
RH
4209 tb_count, s->tb_count1 - tb_count,
4210 (double)(s->tb_count1 - s->tb_count)
4211 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
3de2faa9 4212 qemu_printf("avg ops/TB %0.1f max=%d\n",
fca8a500 4213 (double)s->op_count / tb_div_count, s->op_count_max);
3de2faa9 4214 qemu_printf("deleted ops/TB %0.2f\n",
fca8a500 4215 (double)s->del_op_count / tb_div_count);
3de2faa9 4216 qemu_printf("avg temps/TB %0.2f max=%d\n",
fca8a500 4217 (double)s->temp_count / tb_div_count, s->temp_count_max);
3de2faa9 4218 qemu_printf("avg host code/TB %0.1f\n",
fca8a500 4219 (double)s->code_out_len / tb_div_count);
3de2faa9 4220 qemu_printf("avg search data/TB %0.1f\n",
fca8a500 4221 (double)s->search_out_len / tb_div_count);
a23a9ec6 4222
3de2faa9 4223 qemu_printf("cycles/op %0.1f\n",
a23a9ec6 4224 s->op_count ? (double)tot / s->op_count : 0);
3de2faa9 4225 qemu_printf("cycles/in byte %0.1f\n",
a23a9ec6 4226 s->code_in_len ? (double)tot / s->code_in_len : 0);
3de2faa9 4227 qemu_printf("cycles/out byte %0.1f\n",
a23a9ec6 4228 s->code_out_len ? (double)tot / s->code_out_len : 0);
3de2faa9 4229 qemu_printf("cycles/search byte %0.1f\n",
fca8a500
RH
4230 s->search_out_len ? (double)tot / s->search_out_len : 0);
4231 if (tot == 0) {
a23a9ec6 4232 tot = 1;
fca8a500 4233 }
3de2faa9 4234 qemu_printf(" gen_interm time %0.1f%%\n",
a23a9ec6 4235 (double)s->interm_time / tot * 100.0);
3de2faa9 4236 qemu_printf(" gen_code time %0.1f%%\n",
a23a9ec6 4237 (double)s->code_time / tot * 100.0);
3de2faa9 4238 qemu_printf("optim./code time %0.1f%%\n",
c5cc28ff
AJ
4239 (double)s->opt_time / (s->code_time ? s->code_time : 1)
4240 * 100.0);
3de2faa9 4241 qemu_printf("liveness/code time %0.1f%%\n",
a23a9ec6 4242 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
3de2faa9 4243 qemu_printf("cpu_restore count %" PRId64 "\n",
a23a9ec6 4244 s->restore_count);
3de2faa9 4245 qemu_printf(" avg cycles %0.1f\n",
a23a9ec6 4246 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
a23a9ec6
FB
4247}
4248#else
3de2faa9 4249void tcg_dump_info(void)
a23a9ec6 4250{
3de2faa9 4251 qemu_printf("[TCG profiler not compiled]\n");
a23a9ec6
FB
4252}
4253#endif
813da627
RH
4254
4255#ifdef ELF_HOST_MACHINE
5872bbf2
RH
4256/* In order to use this feature, the backend needs to do three things:
4257
4258 (1) Define ELF_HOST_MACHINE to indicate both what value to
4259 put into the ELF image and to indicate support for the feature.
4260
4261 (2) Define tcg_register_jit. This should create a buffer containing
4262 the contents of a .debug_frame section that describes the post-
4263 prologue unwind info for the tcg machine.
4264
4265 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
4266*/
813da627
RH
4267
4268/* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
4269typedef enum {
4270 JIT_NOACTION = 0,
4271 JIT_REGISTER_FN,
4272 JIT_UNREGISTER_FN
4273} jit_actions_t;
4274
4275struct jit_code_entry {
4276 struct jit_code_entry *next_entry;
4277 struct jit_code_entry *prev_entry;
4278 const void *symfile_addr;
4279 uint64_t symfile_size;
4280};
4281
4282struct jit_descriptor {
4283 uint32_t version;
4284 uint32_t action_flag;
4285 struct jit_code_entry *relevant_entry;
4286 struct jit_code_entry *first_entry;
4287};
4288
4289void __jit_debug_register_code(void) __attribute__((noinline));
4290void __jit_debug_register_code(void)
4291{
4292 asm("");
4293}
4294
4295/* Must statically initialize the version, because GDB may check
4296 the version before we can set it. */
4297struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
4298
4299/* End GDB interface. */
4300
4301static int find_string(const char *strtab, const char *str)
4302{
4303 const char *p = strtab + 1;
4304
4305 while (1) {
4306 if (strcmp(p, str) == 0) {
4307 return p - strtab;
4308 }
4309 p += strlen(p) + 1;
4310 }
4311}
4312
5872bbf2 4313static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
2c90784a
RH
4314 const void *debug_frame,
4315 size_t debug_frame_size)
813da627 4316{
5872bbf2
RH
4317 struct __attribute__((packed)) DebugInfo {
4318 uint32_t len;
4319 uint16_t version;
4320 uint32_t abbrev;
4321 uint8_t ptr_size;
4322 uint8_t cu_die;
4323 uint16_t cu_lang;
4324 uintptr_t cu_low_pc;
4325 uintptr_t cu_high_pc;
4326 uint8_t fn_die;
4327 char fn_name[16];
4328 uintptr_t fn_low_pc;
4329 uintptr_t fn_high_pc;
4330 uint8_t cu_eoc;
4331 };
813da627
RH
4332
4333 struct ElfImage {
4334 ElfW(Ehdr) ehdr;
4335 ElfW(Phdr) phdr;
5872bbf2
RH
4336 ElfW(Shdr) shdr[7];
4337 ElfW(Sym) sym[2];
4338 struct DebugInfo di;
4339 uint8_t da[24];
4340 char str[80];
4341 };
4342
4343 struct ElfImage *img;
4344
4345 static const struct ElfImage img_template = {
4346 .ehdr = {
4347 .e_ident[EI_MAG0] = ELFMAG0,
4348 .e_ident[EI_MAG1] = ELFMAG1,
4349 .e_ident[EI_MAG2] = ELFMAG2,
4350 .e_ident[EI_MAG3] = ELFMAG3,
4351 .e_ident[EI_CLASS] = ELF_CLASS,
4352 .e_ident[EI_DATA] = ELF_DATA,
4353 .e_ident[EI_VERSION] = EV_CURRENT,
4354 .e_type = ET_EXEC,
4355 .e_machine = ELF_HOST_MACHINE,
4356 .e_version = EV_CURRENT,
4357 .e_phoff = offsetof(struct ElfImage, phdr),
4358 .e_shoff = offsetof(struct ElfImage, shdr),
4359 .e_ehsize = sizeof(ElfW(Shdr)),
4360 .e_phentsize = sizeof(ElfW(Phdr)),
4361 .e_phnum = 1,
4362 .e_shentsize = sizeof(ElfW(Shdr)),
4363 .e_shnum = ARRAY_SIZE(img->shdr),
4364 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
abbb3eae
RH
4365#ifdef ELF_HOST_FLAGS
4366 .e_flags = ELF_HOST_FLAGS,
4367#endif
4368#ifdef ELF_OSABI
4369 .e_ident[EI_OSABI] = ELF_OSABI,
4370#endif
5872bbf2
RH
4371 },
4372 .phdr = {
4373 .p_type = PT_LOAD,
4374 .p_flags = PF_X,
4375 },
4376 .shdr = {
4377 [0] = { .sh_type = SHT_NULL },
4378 /* Trick: The contents of code_gen_buffer are not present in
4379 this fake ELF file; that got allocated elsewhere. Therefore
4380 we mark .text as SHT_NOBITS (similar to .bss) so that readers
4381 will not look for contents. We can record any address. */
4382 [1] = { /* .text */
4383 .sh_type = SHT_NOBITS,
4384 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
4385 },
4386 [2] = { /* .debug_info */
4387 .sh_type = SHT_PROGBITS,
4388 .sh_offset = offsetof(struct ElfImage, di),
4389 .sh_size = sizeof(struct DebugInfo),
4390 },
4391 [3] = { /* .debug_abbrev */
4392 .sh_type = SHT_PROGBITS,
4393 .sh_offset = offsetof(struct ElfImage, da),
4394 .sh_size = sizeof(img->da),
4395 },
4396 [4] = { /* .debug_frame */
4397 .sh_type = SHT_PROGBITS,
4398 .sh_offset = sizeof(struct ElfImage),
4399 },
4400 [5] = { /* .symtab */
4401 .sh_type = SHT_SYMTAB,
4402 .sh_offset = offsetof(struct ElfImage, sym),
4403 .sh_size = sizeof(img->sym),
4404 .sh_info = 1,
4405 .sh_link = ARRAY_SIZE(img->shdr) - 1,
4406 .sh_entsize = sizeof(ElfW(Sym)),
4407 },
4408 [6] = { /* .strtab */
4409 .sh_type = SHT_STRTAB,
4410 .sh_offset = offsetof(struct ElfImage, str),
4411 .sh_size = sizeof(img->str),
4412 }
4413 },
4414 .sym = {
4415 [1] = { /* code_gen_buffer */
4416 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
4417 .st_shndx = 1,
4418 }
4419 },
4420 .di = {
4421 .len = sizeof(struct DebugInfo) - 4,
4422 .version = 2,
4423 .ptr_size = sizeof(void *),
4424 .cu_die = 1,
4425 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
4426 .fn_die = 2,
4427 .fn_name = "code_gen_buffer"
4428 },
4429 .da = {
4430 1, /* abbrev number (the cu) */
4431 0x11, 1, /* DW_TAG_compile_unit, has children */
4432 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
4433 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4434 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4435 0, 0, /* end of abbrev */
4436 2, /* abbrev number (the fn) */
4437 0x2e, 0, /* DW_TAG_subprogram, no children */
4438 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
4439 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4440 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4441 0, 0, /* end of abbrev */
4442 0 /* no more abbrev */
4443 },
4444 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
4445 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
813da627
RH
4446 };
4447
4448 /* We only need a single jit entry; statically allocate it. */
4449 static struct jit_code_entry one_entry;
4450
5872bbf2 4451 uintptr_t buf = (uintptr_t)buf_ptr;
813da627 4452 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
2c90784a 4453 DebugFrameHeader *dfh;
813da627 4454
5872bbf2
RH
4455 img = g_malloc(img_size);
4456 *img = img_template;
813da627 4457
5872bbf2
RH
4458 img->phdr.p_vaddr = buf;
4459 img->phdr.p_paddr = buf;
4460 img->phdr.p_memsz = buf_size;
813da627 4461
813da627 4462 img->shdr[1].sh_name = find_string(img->str, ".text");
5872bbf2 4463 img->shdr[1].sh_addr = buf;
813da627
RH
4464 img->shdr[1].sh_size = buf_size;
4465
5872bbf2
RH
4466 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
4467 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
4468
4469 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
4470 img->shdr[4].sh_size = debug_frame_size;
4471
4472 img->shdr[5].sh_name = find_string(img->str, ".symtab");
4473 img->shdr[6].sh_name = find_string(img->str, ".strtab");
4474
4475 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
4476 img->sym[1].st_value = buf;
4477 img->sym[1].st_size = buf_size;
813da627 4478
5872bbf2 4479 img->di.cu_low_pc = buf;
45aba097 4480 img->di.cu_high_pc = buf + buf_size;
5872bbf2 4481 img->di.fn_low_pc = buf;
45aba097 4482 img->di.fn_high_pc = buf + buf_size;
813da627 4483
2c90784a
RH
4484 dfh = (DebugFrameHeader *)(img + 1);
4485 memcpy(dfh, debug_frame, debug_frame_size);
4486 dfh->fde.func_start = buf;
4487 dfh->fde.func_len = buf_size;
4488
813da627
RH
4489#ifdef DEBUG_JIT
4490 /* Enable this block to be able to debug the ELF image file creation.
4491 One can use readelf, objdump, or other inspection utilities. */
4492 {
4493 FILE *f = fopen("/tmp/qemu.jit", "w+b");
4494 if (f) {
5872bbf2 4495 if (fwrite(img, img_size, 1, f) != img_size) {
813da627
RH
4496 /* Avoid stupid unused return value warning for fwrite. */
4497 }
4498 fclose(f);
4499 }
4500 }
4501#endif
4502
4503 one_entry.symfile_addr = img;
4504 one_entry.symfile_size = img_size;
4505
4506 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
4507 __jit_debug_descriptor.relevant_entry = &one_entry;
4508 __jit_debug_descriptor.first_entry = &one_entry;
4509 __jit_debug_register_code();
4510}
4511#else
5872bbf2
RH
4512/* No support for the feature. Provide the entry point expected by exec.c,
4513 and implement the internal function we declared earlier. */
813da627
RH
4514
4515static void tcg_register_jit_int(void *buf, size_t size,
2c90784a
RH
4516 const void *debug_frame,
4517 size_t debug_frame_size)
813da627
RH
4518{
4519}
4520
4521void tcg_register_jit(void *buf, size_t buf_size)
4522{
4523}
4524#endif /* ELF_HOST_MACHINE */
db432672
RH
4525
4526#if !TCG_TARGET_MAYBE_vec
4527void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
4528{
4529 g_assert_not_reached();
4530}
4531#endif