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