tcg: Rename and adjust liveness_pass_1 helpers
[qemu.git] / tcg / tcg.c
blob0afc6ba1e6d657ca0401b881489195f70acb551c
1 /*
2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 /* define it to use liveness analysis (better code) */
26 #define USE_TCG_OPTIMIZATIONS
28 #include "qemu/osdep.h"
30 /* Define to jump the ELF file used to communicate with GDB. */
31 #undef DEBUG_JIT
33 #include "qemu/error-report.h"
34 #include "qemu/cutils.h"
35 #include "qemu/host-utils.h"
36 #include "qemu/timer.h"
38 /* Note: the long term plan is to reduce the dependencies on the QEMU
39 CPU definitions. Currently they are used for qemu_ld/st
40 instructions */
41 #define NO_CPU_IO_DEFS
42 #include "cpu.h"
44 #include "exec/cpu-common.h"
45 #include "exec/exec-all.h"
47 #include "tcg-op.h"
49 #if UINTPTR_MAX == UINT32_MAX
50 # define ELF_CLASS ELFCLASS32
51 #else
52 # define ELF_CLASS ELFCLASS64
53 #endif
54 #ifdef HOST_WORDS_BIGENDIAN
55 # define ELF_DATA ELFDATA2MSB
56 #else
57 # define ELF_DATA ELFDATA2LSB
58 #endif
60 #include "elf.h"
61 #include "exec/log.h"
62 #include "sysemu/sysemu.h"
64 /* Forward declarations for functions declared in tcg-target.inc.c and
65 used here. */
66 static void tcg_target_init(TCGContext *s);
67 static const TCGTargetOpDef *tcg_target_op_def(TCGOpcode);
68 static void tcg_target_qemu_prologue(TCGContext *s);
69 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
70 intptr_t value, intptr_t addend);
72 /* The CIE and FDE header definitions will be common to all hosts. */
73 typedef struct {
74 uint32_t len __attribute__((aligned((sizeof(void *)))));
75 uint32_t id;
76 uint8_t version;
77 char augmentation[1];
78 uint8_t code_align;
79 uint8_t data_align;
80 uint8_t return_column;
81 } DebugFrameCIE;
83 typedef struct QEMU_PACKED {
84 uint32_t len __attribute__((aligned((sizeof(void *)))));
85 uint32_t cie_offset;
86 uintptr_t func_start;
87 uintptr_t func_len;
88 } DebugFrameFDEHeader;
90 typedef struct QEMU_PACKED {
91 DebugFrameCIE cie;
92 DebugFrameFDEHeader fde;
93 } DebugFrameHeader;
95 static void tcg_register_jit_int(void *buf, size_t size,
96 const void *debug_frame,
97 size_t debug_frame_size)
98 __attribute__((unused));
100 /* Forward declarations for functions declared and used in tcg-target.inc.c. */
101 static const char *target_parse_constraint(TCGArgConstraint *ct,
102 const char *ct_str, TCGType type);
103 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
104 intptr_t arg2);
105 static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
106 static void tcg_out_movi(TCGContext *s, TCGType type,
107 TCGReg ret, tcg_target_long arg);
108 static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
109 const int *const_args);
110 #if TCG_TARGET_MAYBE_vec
111 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
112 unsigned vece, const TCGArg *args,
113 const int *const_args);
114 #else
115 static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc, unsigned vecl,
116 unsigned vece, const TCGArg *args,
117 const int *const_args)
119 g_assert_not_reached();
121 #endif
122 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
123 intptr_t arg2);
124 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
125 TCGReg base, intptr_t ofs);
126 static void tcg_out_call(TCGContext *s, tcg_insn_unit *target);
127 static int tcg_target_const_match(tcg_target_long val, TCGType type,
128 const TCGArgConstraint *arg_ct);
129 #ifdef TCG_TARGET_NEED_LDST_LABELS
130 static bool tcg_out_ldst_finalize(TCGContext *s);
131 #endif
133 #define TCG_HIGHWATER 1024
135 static TCGContext **tcg_ctxs;
136 static unsigned int n_tcg_ctxs;
137 TCGv_env cpu_env = 0;
139 struct tcg_region_tree {
140 QemuMutex lock;
141 GTree *tree;
142 /* padding to avoid false sharing is computed at run-time */
146 * We divide code_gen_buffer into equally-sized "regions" that TCG threads
147 * dynamically allocate from as demand dictates. Given appropriate region
148 * sizing, this minimizes flushes even when some TCG threads generate a lot
149 * more code than others.
151 struct tcg_region_state {
152 QemuMutex lock;
154 /* fields set at init time */
155 void *start;
156 void *start_aligned;
157 void *end;
158 size_t n;
159 size_t size; /* size of one region */
160 size_t stride; /* .size + guard size */
162 /* fields protected by the lock */
163 size_t current; /* current region index */
164 size_t agg_size_full; /* aggregate size of full regions */
167 static struct tcg_region_state region;
169 * This is an array of struct tcg_region_tree's, with padding.
170 * We use void * to simplify the computation of region_trees[i]; each
171 * struct is found every tree_size bytes.
173 static void *region_trees;
174 static size_t tree_size;
175 static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
176 static TCGRegSet tcg_target_call_clobber_regs;
178 #if TCG_TARGET_INSN_UNIT_SIZE == 1
179 static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
181 *s->code_ptr++ = v;
184 static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
185 uint8_t v)
187 *p = v;
189 #endif
191 #if TCG_TARGET_INSN_UNIT_SIZE <= 2
192 static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
194 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
195 *s->code_ptr++ = v;
196 } else {
197 tcg_insn_unit *p = s->code_ptr;
198 memcpy(p, &v, sizeof(v));
199 s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
203 static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
204 uint16_t v)
206 if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
207 *p = v;
208 } else {
209 memcpy(p, &v, sizeof(v));
212 #endif
214 #if TCG_TARGET_INSN_UNIT_SIZE <= 4
215 static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
217 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
218 *s->code_ptr++ = v;
219 } else {
220 tcg_insn_unit *p = s->code_ptr;
221 memcpy(p, &v, sizeof(v));
222 s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
226 static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
227 uint32_t v)
229 if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
230 *p = v;
231 } else {
232 memcpy(p, &v, sizeof(v));
235 #endif
237 #if TCG_TARGET_INSN_UNIT_SIZE <= 8
238 static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
240 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
241 *s->code_ptr++ = v;
242 } else {
243 tcg_insn_unit *p = s->code_ptr;
244 memcpy(p, &v, sizeof(v));
245 s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
249 static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
250 uint64_t v)
252 if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
253 *p = v;
254 } else {
255 memcpy(p, &v, sizeof(v));
258 #endif
260 /* label relocation processing */
262 static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
263 TCGLabel *l, intptr_t addend)
265 TCGRelocation *r;
267 if (l->has_value) {
268 /* FIXME: This may break relocations on RISC targets that
269 modify instruction fields in place. The caller may not have
270 written the initial value. */
271 bool ok = patch_reloc(code_ptr, type, l->u.value, addend);
272 tcg_debug_assert(ok);
273 } else {
274 /* add a new relocation entry */
275 r = tcg_malloc(sizeof(TCGRelocation));
276 r->type = type;
277 r->ptr = code_ptr;
278 r->addend = addend;
279 r->next = l->u.first_reloc;
280 l->u.first_reloc = r;
284 static void tcg_out_label(TCGContext *s, TCGLabel *l, tcg_insn_unit *ptr)
286 intptr_t value = (intptr_t)ptr;
287 TCGRelocation *r;
289 tcg_debug_assert(!l->has_value);
291 for (r = l->u.first_reloc; r != NULL; r = r->next) {
292 bool ok = patch_reloc(r->ptr, r->type, value, r->addend);
293 tcg_debug_assert(ok);
296 l->has_value = 1;
297 l->u.value_ptr = ptr;
300 TCGLabel *gen_new_label(void)
302 TCGContext *s = tcg_ctx;
303 TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
305 *l = (TCGLabel){
306 .id = s->nb_labels++
309 return l;
312 static void set_jmp_reset_offset(TCGContext *s, int which)
314 size_t off = tcg_current_code_size(s);
315 s->tb_jmp_reset_offset[which] = off;
316 /* Make sure that we didn't overflow the stored offset. */
317 assert(s->tb_jmp_reset_offset[which] == off);
320 #include "tcg-target.inc.c"
322 /* compare a pointer @ptr and a tb_tc @s */
323 static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s)
325 if (ptr >= s->ptr + s->size) {
326 return 1;
327 } else if (ptr < s->ptr) {
328 return -1;
330 return 0;
333 static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp)
335 const struct tb_tc *a = ap;
336 const struct tb_tc *b = bp;
339 * When both sizes are set, we know this isn't a lookup.
340 * This is the most likely case: every TB must be inserted; lookups
341 * are a lot less frequent.
343 if (likely(a->size && b->size)) {
344 if (a->ptr > b->ptr) {
345 return 1;
346 } else if (a->ptr < b->ptr) {
347 return -1;
349 /* a->ptr == b->ptr should happen only on deletions */
350 g_assert(a->size == b->size);
351 return 0;
354 * All lookups have either .size field set to 0.
355 * From the glib sources we see that @ap is always the lookup key. However
356 * the docs provide no guarantee, so we just mark this case as likely.
358 if (likely(a->size == 0)) {
359 return ptr_cmp_tb_tc(a->ptr, b);
361 return ptr_cmp_tb_tc(b->ptr, a);
364 static void tcg_region_trees_init(void)
366 size_t i;
368 tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize);
369 region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size);
370 for (i = 0; i < region.n; i++) {
371 struct tcg_region_tree *rt = region_trees + i * tree_size;
373 qemu_mutex_init(&rt->lock);
374 rt->tree = g_tree_new(tb_tc_cmp);
378 static struct tcg_region_tree *tc_ptr_to_region_tree(void *p)
380 size_t region_idx;
382 if (p < region.start_aligned) {
383 region_idx = 0;
384 } else {
385 ptrdiff_t offset = p - region.start_aligned;
387 if (offset > region.stride * (region.n - 1)) {
388 region_idx = region.n - 1;
389 } else {
390 region_idx = offset / region.stride;
393 return region_trees + region_idx * tree_size;
396 void tcg_tb_insert(TranslationBlock *tb)
398 struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
400 qemu_mutex_lock(&rt->lock);
401 g_tree_insert(rt->tree, &tb->tc, tb);
402 qemu_mutex_unlock(&rt->lock);
405 void tcg_tb_remove(TranslationBlock *tb)
407 struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
409 qemu_mutex_lock(&rt->lock);
410 g_tree_remove(rt->tree, &tb->tc);
411 qemu_mutex_unlock(&rt->lock);
415 * Find the TB 'tb' such that
416 * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
417 * Return NULL if not found.
419 TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr)
421 struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr);
422 TranslationBlock *tb;
423 struct tb_tc s = { .ptr = (void *)tc_ptr };
425 qemu_mutex_lock(&rt->lock);
426 tb = g_tree_lookup(rt->tree, &s);
427 qemu_mutex_unlock(&rt->lock);
428 return tb;
431 static void tcg_region_tree_lock_all(void)
433 size_t i;
435 for (i = 0; i < region.n; i++) {
436 struct tcg_region_tree *rt = region_trees + i * tree_size;
438 qemu_mutex_lock(&rt->lock);
442 static void tcg_region_tree_unlock_all(void)
444 size_t i;
446 for (i = 0; i < region.n; i++) {
447 struct tcg_region_tree *rt = region_trees + i * tree_size;
449 qemu_mutex_unlock(&rt->lock);
453 void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)
455 size_t i;
457 tcg_region_tree_lock_all();
458 for (i = 0; i < region.n; i++) {
459 struct tcg_region_tree *rt = region_trees + i * tree_size;
461 g_tree_foreach(rt->tree, func, user_data);
463 tcg_region_tree_unlock_all();
466 size_t tcg_nb_tbs(void)
468 size_t nb_tbs = 0;
469 size_t i;
471 tcg_region_tree_lock_all();
472 for (i = 0; i < region.n; i++) {
473 struct tcg_region_tree *rt = region_trees + i * tree_size;
475 nb_tbs += g_tree_nnodes(rt->tree);
477 tcg_region_tree_unlock_all();
478 return nb_tbs;
481 static void tcg_region_tree_reset_all(void)
483 size_t i;
485 tcg_region_tree_lock_all();
486 for (i = 0; i < region.n; i++) {
487 struct tcg_region_tree *rt = region_trees + i * tree_size;
489 /* Increment the refcount first so that destroy acts as a reset */
490 g_tree_ref(rt->tree);
491 g_tree_destroy(rt->tree);
493 tcg_region_tree_unlock_all();
496 static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
498 void *start, *end;
500 start = region.start_aligned + curr_region * region.stride;
501 end = start + region.size;
503 if (curr_region == 0) {
504 start = region.start;
506 if (curr_region == region.n - 1) {
507 end = region.end;
510 *pstart = start;
511 *pend = end;
514 static void tcg_region_assign(TCGContext *s, size_t curr_region)
516 void *start, *end;
518 tcg_region_bounds(curr_region, &start, &end);
520 s->code_gen_buffer = start;
521 s->code_gen_ptr = start;
522 s->code_gen_buffer_size = end - start;
523 s->code_gen_highwater = end - TCG_HIGHWATER;
526 static bool tcg_region_alloc__locked(TCGContext *s)
528 if (region.current == region.n) {
529 return true;
531 tcg_region_assign(s, region.current);
532 region.current++;
533 return false;
537 * Request a new region once the one in use has filled up.
538 * Returns true on error.
540 static bool tcg_region_alloc(TCGContext *s)
542 bool err;
543 /* read the region size now; alloc__locked will overwrite it on success */
544 size_t size_full = s->code_gen_buffer_size;
546 qemu_mutex_lock(&region.lock);
547 err = tcg_region_alloc__locked(s);
548 if (!err) {
549 region.agg_size_full += size_full - TCG_HIGHWATER;
551 qemu_mutex_unlock(&region.lock);
552 return err;
556 * Perform a context's first region allocation.
557 * This function does _not_ increment region.agg_size_full.
559 static inline bool tcg_region_initial_alloc__locked(TCGContext *s)
561 return tcg_region_alloc__locked(s);
564 /* Call from a safe-work context */
565 void tcg_region_reset_all(void)
567 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
568 unsigned int i;
570 qemu_mutex_lock(&region.lock);
571 region.current = 0;
572 region.agg_size_full = 0;
574 for (i = 0; i < n_ctxs; i++) {
575 TCGContext *s = atomic_read(&tcg_ctxs[i]);
576 bool err = tcg_region_initial_alloc__locked(s);
578 g_assert(!err);
580 qemu_mutex_unlock(&region.lock);
582 tcg_region_tree_reset_all();
585 #ifdef CONFIG_USER_ONLY
586 static size_t tcg_n_regions(void)
588 return 1;
590 #else
592 * It is likely that some vCPUs will translate more code than others, so we
593 * first try to set more regions than max_cpus, with those regions being of
594 * reasonable size. If that's not possible we make do by evenly dividing
595 * the code_gen_buffer among the vCPUs.
597 static size_t tcg_n_regions(void)
599 size_t i;
601 /* Use a single region if all we have is one vCPU thread */
602 if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
603 return 1;
606 /* Try to have more regions than max_cpus, with each region being >= 2 MB */
607 for (i = 8; i > 0; i--) {
608 size_t regions_per_thread = i;
609 size_t region_size;
611 region_size = tcg_init_ctx.code_gen_buffer_size;
612 region_size /= max_cpus * regions_per_thread;
614 if (region_size >= 2 * 1024u * 1024) {
615 return max_cpus * regions_per_thread;
618 /* If we can't, then just allocate one region per vCPU thread */
619 return max_cpus;
621 #endif
624 * Initializes region partitioning.
626 * Called at init time from the parent thread (i.e. the one calling
627 * tcg_context_init), after the target's TCG globals have been set.
629 * Region partitioning works by splitting code_gen_buffer into separate regions,
630 * and then assigning regions to TCG threads so that the threads can translate
631 * code in parallel without synchronization.
633 * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
634 * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
635 * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
636 * must have been parsed before calling this function, since it calls
637 * qemu_tcg_mttcg_enabled().
639 * In user-mode we use a single region. Having multiple regions in user-mode
640 * is not supported, because the number of vCPU threads (recall that each thread
641 * spawned by the guest corresponds to a vCPU thread) is only bounded by the
642 * OS, and usually this number is huge (tens of thousands is not uncommon).
643 * Thus, given this large bound on the number of vCPU threads and the fact
644 * that code_gen_buffer is allocated at compile-time, we cannot guarantee
645 * that the availability of at least one region per vCPU thread.
647 * However, this user-mode limitation is unlikely to be a significant problem
648 * in practice. Multi-threaded guests share most if not all of their translated
649 * code, which makes parallel code generation less appealing than in softmmu.
651 void tcg_region_init(void)
653 void *buf = tcg_init_ctx.code_gen_buffer;
654 void *aligned;
655 size_t size = tcg_init_ctx.code_gen_buffer_size;
656 size_t page_size = qemu_real_host_page_size;
657 size_t region_size;
658 size_t n_regions;
659 size_t i;
661 n_regions = tcg_n_regions();
663 /* The first region will be 'aligned - buf' bytes larger than the others */
664 aligned = QEMU_ALIGN_PTR_UP(buf, page_size);
665 g_assert(aligned < tcg_init_ctx.code_gen_buffer + size);
667 * Make region_size a multiple of page_size, using aligned as the start.
668 * As a result of this we might end up with a few extra pages at the end of
669 * the buffer; we will assign those to the last region.
671 region_size = (size - (aligned - buf)) / n_regions;
672 region_size = QEMU_ALIGN_DOWN(region_size, page_size);
674 /* A region must have at least 2 pages; one code, one guard */
675 g_assert(region_size >= 2 * page_size);
677 /* init the region struct */
678 qemu_mutex_init(&region.lock);
679 region.n = n_regions;
680 region.size = region_size - page_size;
681 region.stride = region_size;
682 region.start = buf;
683 region.start_aligned = aligned;
684 /* page-align the end, since its last page will be a guard page */
685 region.end = QEMU_ALIGN_PTR_DOWN(buf + size, page_size);
686 /* account for that last guard page */
687 region.end -= page_size;
689 /* set guard pages */
690 for (i = 0; i < region.n; i++) {
691 void *start, *end;
692 int rc;
694 tcg_region_bounds(i, &start, &end);
695 rc = qemu_mprotect_none(end, page_size);
696 g_assert(!rc);
699 tcg_region_trees_init();
701 /* In user-mode we support only one ctx, so do the initial allocation now */
702 #ifdef CONFIG_USER_ONLY
704 bool err = tcg_region_initial_alloc__locked(tcg_ctx);
706 g_assert(!err);
708 #endif
712 * All TCG threads except the parent (i.e. the one that called tcg_context_init
713 * and registered the target's TCG globals) must register with this function
714 * before initiating translation.
716 * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
717 * of tcg_region_init() for the reasoning behind this.
719 * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
720 * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
721 * is not used anymore for translation once this function is called.
723 * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
724 * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
726 #ifdef CONFIG_USER_ONLY
727 void tcg_register_thread(void)
729 tcg_ctx = &tcg_init_ctx;
731 #else
732 void tcg_register_thread(void)
734 TCGContext *s = g_malloc(sizeof(*s));
735 unsigned int i, n;
736 bool err;
738 *s = tcg_init_ctx;
740 /* Relink mem_base. */
741 for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
742 if (tcg_init_ctx.temps[i].mem_base) {
743 ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
744 tcg_debug_assert(b >= 0 && b < n);
745 s->temps[i].mem_base = &s->temps[b];
749 /* Claim an entry in tcg_ctxs */
750 n = atomic_fetch_inc(&n_tcg_ctxs);
751 g_assert(n < max_cpus);
752 atomic_set(&tcg_ctxs[n], s);
754 tcg_ctx = s;
755 qemu_mutex_lock(&region.lock);
756 err = tcg_region_initial_alloc__locked(tcg_ctx);
757 g_assert(!err);
758 qemu_mutex_unlock(&region.lock);
760 #endif /* !CONFIG_USER_ONLY */
763 * Returns the size (in bytes) of all translated code (i.e. from all regions)
764 * currently in the cache.
765 * See also: tcg_code_capacity()
766 * Do not confuse with tcg_current_code_size(); that one applies to a single
767 * TCG context.
769 size_t tcg_code_size(void)
771 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
772 unsigned int i;
773 size_t total;
775 qemu_mutex_lock(&region.lock);
776 total = region.agg_size_full;
777 for (i = 0; i < n_ctxs; i++) {
778 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
779 size_t size;
781 size = atomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
782 g_assert(size <= s->code_gen_buffer_size);
783 total += size;
785 qemu_mutex_unlock(&region.lock);
786 return total;
790 * Returns the code capacity (in bytes) of the entire cache, i.e. including all
791 * regions.
792 * See also: tcg_code_size()
794 size_t tcg_code_capacity(void)
796 size_t guard_size, capacity;
798 /* no need for synchronization; these variables are set at init time */
799 guard_size = region.stride - region.size;
800 capacity = region.end + guard_size - region.start;
801 capacity -= region.n * (guard_size + TCG_HIGHWATER);
802 return capacity;
805 size_t tcg_tb_phys_invalidate_count(void)
807 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
808 unsigned int i;
809 size_t total = 0;
811 for (i = 0; i < n_ctxs; i++) {
812 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
814 total += atomic_read(&s->tb_phys_invalidate_count);
816 return total;
819 /* pool based memory allocation */
820 void *tcg_malloc_internal(TCGContext *s, int size)
822 TCGPool *p;
823 int pool_size;
825 if (size > TCG_POOL_CHUNK_SIZE) {
826 /* big malloc: insert a new pool (XXX: could optimize) */
827 p = g_malloc(sizeof(TCGPool) + size);
828 p->size = size;
829 p->next = s->pool_first_large;
830 s->pool_first_large = p;
831 return p->data;
832 } else {
833 p = s->pool_current;
834 if (!p) {
835 p = s->pool_first;
836 if (!p)
837 goto new_pool;
838 } else {
839 if (!p->next) {
840 new_pool:
841 pool_size = TCG_POOL_CHUNK_SIZE;
842 p = g_malloc(sizeof(TCGPool) + pool_size);
843 p->size = pool_size;
844 p->next = NULL;
845 if (s->pool_current)
846 s->pool_current->next = p;
847 else
848 s->pool_first = p;
849 } else {
850 p = p->next;
854 s->pool_current = p;
855 s->pool_cur = p->data + size;
856 s->pool_end = p->data + p->size;
857 return p->data;
860 void tcg_pool_reset(TCGContext *s)
862 TCGPool *p, *t;
863 for (p = s->pool_first_large; p; p = t) {
864 t = p->next;
865 g_free(p);
867 s->pool_first_large = NULL;
868 s->pool_cur = s->pool_end = NULL;
869 s->pool_current = NULL;
872 typedef struct TCGHelperInfo {
873 void *func;
874 const char *name;
875 unsigned flags;
876 unsigned sizemask;
877 } TCGHelperInfo;
879 #include "exec/helper-proto.h"
881 static const TCGHelperInfo all_helpers[] = {
882 #include "exec/helper-tcg.h"
884 static GHashTable *helper_table;
886 static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
887 static void process_op_defs(TCGContext *s);
888 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
889 TCGReg reg, const char *name);
891 void tcg_context_init(TCGContext *s)
893 int op, total_args, n, i;
894 TCGOpDef *def;
895 TCGArgConstraint *args_ct;
896 int *sorted_args;
897 TCGTemp *ts;
899 memset(s, 0, sizeof(*s));
900 s->nb_globals = 0;
902 /* Count total number of arguments and allocate the corresponding
903 space */
904 total_args = 0;
905 for(op = 0; op < NB_OPS; op++) {
906 def = &tcg_op_defs[op];
907 n = def->nb_iargs + def->nb_oargs;
908 total_args += n;
911 args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
912 sorted_args = g_malloc(sizeof(int) * total_args);
914 for(op = 0; op < NB_OPS; op++) {
915 def = &tcg_op_defs[op];
916 def->args_ct = args_ct;
917 def->sorted_args = sorted_args;
918 n = def->nb_iargs + def->nb_oargs;
919 sorted_args += n;
920 args_ct += n;
923 /* Register helpers. */
924 /* Use g_direct_hash/equal for direct pointer comparisons on func. */
925 helper_table = g_hash_table_new(NULL, NULL);
927 for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
928 g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
929 (gpointer)&all_helpers[i]);
932 tcg_target_init(s);
933 process_op_defs(s);
935 /* Reverse the order of the saved registers, assuming they're all at
936 the start of tcg_target_reg_alloc_order. */
937 for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
938 int r = tcg_target_reg_alloc_order[n];
939 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
940 break;
943 for (i = 0; i < n; ++i) {
944 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
946 for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
947 indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
950 tcg_ctx = s;
952 * In user-mode we simply share the init context among threads, since we
953 * use a single region. See the documentation tcg_region_init() for the
954 * reasoning behind this.
955 * In softmmu we will have at most max_cpus TCG threads.
957 #ifdef CONFIG_USER_ONLY
958 tcg_ctxs = &tcg_ctx;
959 n_tcg_ctxs = 1;
960 #else
961 tcg_ctxs = g_new(TCGContext *, max_cpus);
962 #endif
964 tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
965 ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
966 cpu_env = temp_tcgv_ptr(ts);
970 * Allocate TBs right before their corresponding translated code, making
971 * sure that TBs and code are on different cache lines.
973 TranslationBlock *tcg_tb_alloc(TCGContext *s)
975 uintptr_t align = qemu_icache_linesize;
976 TranslationBlock *tb;
977 void *next;
979 retry:
980 tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
981 next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
983 if (unlikely(next > s->code_gen_highwater)) {
984 if (tcg_region_alloc(s)) {
985 return NULL;
987 goto retry;
989 atomic_set(&s->code_gen_ptr, next);
990 s->data_gen_ptr = NULL;
991 return tb;
994 void tcg_prologue_init(TCGContext *s)
996 size_t prologue_size, total_size;
997 void *buf0, *buf1;
999 /* Put the prologue at the beginning of code_gen_buffer. */
1000 buf0 = s->code_gen_buffer;
1001 total_size = s->code_gen_buffer_size;
1002 s->code_ptr = buf0;
1003 s->code_buf = buf0;
1004 s->data_gen_ptr = NULL;
1005 s->code_gen_prologue = buf0;
1007 /* Compute a high-water mark, at which we voluntarily flush the buffer
1008 and start over. The size here is arbitrary, significantly larger
1009 than we expect the code generation for any one opcode to require. */
1010 s->code_gen_highwater = s->code_gen_buffer + (total_size - TCG_HIGHWATER);
1012 #ifdef TCG_TARGET_NEED_POOL_LABELS
1013 s->pool_labels = NULL;
1014 #endif
1016 /* Generate the prologue. */
1017 tcg_target_qemu_prologue(s);
1019 #ifdef TCG_TARGET_NEED_POOL_LABELS
1020 /* Allow the prologue to put e.g. guest_base into a pool entry. */
1022 bool ok = tcg_out_pool_finalize(s);
1023 tcg_debug_assert(ok);
1025 #endif
1027 buf1 = s->code_ptr;
1028 flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
1030 /* Deduct the prologue from the buffer. */
1031 prologue_size = tcg_current_code_size(s);
1032 s->code_gen_ptr = buf1;
1033 s->code_gen_buffer = buf1;
1034 s->code_buf = buf1;
1035 total_size -= prologue_size;
1036 s->code_gen_buffer_size = total_size;
1038 tcg_register_jit(s->code_gen_buffer, total_size);
1040 #ifdef DEBUG_DISAS
1041 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
1042 qemu_log_lock();
1043 qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
1044 if (s->data_gen_ptr) {
1045 size_t code_size = s->data_gen_ptr - buf0;
1046 size_t data_size = prologue_size - code_size;
1047 size_t i;
1049 log_disas(buf0, code_size);
1051 for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1052 if (sizeof(tcg_target_ulong) == 8) {
1053 qemu_log("0x%08" PRIxPTR ": .quad 0x%016" PRIx64 "\n",
1054 (uintptr_t)s->data_gen_ptr + i,
1055 *(uint64_t *)(s->data_gen_ptr + i));
1056 } else {
1057 qemu_log("0x%08" PRIxPTR ": .long 0x%08x\n",
1058 (uintptr_t)s->data_gen_ptr + i,
1059 *(uint32_t *)(s->data_gen_ptr + i));
1062 } else {
1063 log_disas(buf0, prologue_size);
1065 qemu_log("\n");
1066 qemu_log_flush();
1067 qemu_log_unlock();
1069 #endif
1071 /* Assert that goto_ptr is implemented completely. */
1072 if (TCG_TARGET_HAS_goto_ptr) {
1073 tcg_debug_assert(s->code_gen_epilogue != NULL);
1077 void tcg_func_start(TCGContext *s)
1079 tcg_pool_reset(s);
1080 s->nb_temps = s->nb_globals;
1082 /* No temps have been previously allocated for size or locality. */
1083 memset(s->free_temps, 0, sizeof(s->free_temps));
1085 s->nb_ops = 0;
1086 s->nb_labels = 0;
1087 s->current_frame_offset = s->frame_start;
1089 #ifdef CONFIG_DEBUG_TCG
1090 s->goto_tb_issue_mask = 0;
1091 #endif
1093 QTAILQ_INIT(&s->ops);
1094 QTAILQ_INIT(&s->free_ops);
1097 static inline TCGTemp *tcg_temp_alloc(TCGContext *s)
1099 int n = s->nb_temps++;
1100 tcg_debug_assert(n < TCG_MAX_TEMPS);
1101 return memset(&s->temps[n], 0, sizeof(TCGTemp));
1104 static inline TCGTemp *tcg_global_alloc(TCGContext *s)
1106 TCGTemp *ts;
1108 tcg_debug_assert(s->nb_globals == s->nb_temps);
1109 s->nb_globals++;
1110 ts = tcg_temp_alloc(s);
1111 ts->temp_global = 1;
1113 return ts;
1116 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1117 TCGReg reg, const char *name)
1119 TCGTemp *ts;
1121 if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
1122 tcg_abort();
1125 ts = tcg_global_alloc(s);
1126 ts->base_type = type;
1127 ts->type = type;
1128 ts->fixed_reg = 1;
1129 ts->reg = reg;
1130 ts->name = name;
1131 tcg_regset_set_reg(s->reserved_regs, reg);
1133 return ts;
1136 void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
1138 s->frame_start = start;
1139 s->frame_end = start + size;
1140 s->frame_temp
1141 = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
1144 TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
1145 intptr_t offset, const char *name)
1147 TCGContext *s = tcg_ctx;
1148 TCGTemp *base_ts = tcgv_ptr_temp(base);
1149 TCGTemp *ts = tcg_global_alloc(s);
1150 int indirect_reg = 0, bigendian = 0;
1151 #ifdef HOST_WORDS_BIGENDIAN
1152 bigendian = 1;
1153 #endif
1155 if (!base_ts->fixed_reg) {
1156 /* We do not support double-indirect registers. */
1157 tcg_debug_assert(!base_ts->indirect_reg);
1158 base_ts->indirect_base = 1;
1159 s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1160 ? 2 : 1);
1161 indirect_reg = 1;
1164 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1165 TCGTemp *ts2 = tcg_global_alloc(s);
1166 char buf[64];
1168 ts->base_type = TCG_TYPE_I64;
1169 ts->type = TCG_TYPE_I32;
1170 ts->indirect_reg = indirect_reg;
1171 ts->mem_allocated = 1;
1172 ts->mem_base = base_ts;
1173 ts->mem_offset = offset + bigendian * 4;
1174 pstrcpy(buf, sizeof(buf), name);
1175 pstrcat(buf, sizeof(buf), "_0");
1176 ts->name = strdup(buf);
1178 tcg_debug_assert(ts2 == ts + 1);
1179 ts2->base_type = TCG_TYPE_I64;
1180 ts2->type = TCG_TYPE_I32;
1181 ts2->indirect_reg = indirect_reg;
1182 ts2->mem_allocated = 1;
1183 ts2->mem_base = base_ts;
1184 ts2->mem_offset = offset + (1 - bigendian) * 4;
1185 pstrcpy(buf, sizeof(buf), name);
1186 pstrcat(buf, sizeof(buf), "_1");
1187 ts2->name = strdup(buf);
1188 } else {
1189 ts->base_type = type;
1190 ts->type = type;
1191 ts->indirect_reg = indirect_reg;
1192 ts->mem_allocated = 1;
1193 ts->mem_base = base_ts;
1194 ts->mem_offset = offset;
1195 ts->name = name;
1197 return ts;
1200 TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local)
1202 TCGContext *s = tcg_ctx;
1203 TCGTemp *ts;
1204 int idx, k;
1206 k = type + (temp_local ? TCG_TYPE_COUNT : 0);
1207 idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
1208 if (idx < TCG_MAX_TEMPS) {
1209 /* There is already an available temp with the right type. */
1210 clear_bit(idx, s->free_temps[k].l);
1212 ts = &s->temps[idx];
1213 ts->temp_allocated = 1;
1214 tcg_debug_assert(ts->base_type == type);
1215 tcg_debug_assert(ts->temp_local == temp_local);
1216 } else {
1217 ts = tcg_temp_alloc(s);
1218 if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1219 TCGTemp *ts2 = tcg_temp_alloc(s);
1221 ts->base_type = type;
1222 ts->type = TCG_TYPE_I32;
1223 ts->temp_allocated = 1;
1224 ts->temp_local = temp_local;
1226 tcg_debug_assert(ts2 == ts + 1);
1227 ts2->base_type = TCG_TYPE_I64;
1228 ts2->type = TCG_TYPE_I32;
1229 ts2->temp_allocated = 1;
1230 ts2->temp_local = temp_local;
1231 } else {
1232 ts->base_type = type;
1233 ts->type = type;
1234 ts->temp_allocated = 1;
1235 ts->temp_local = temp_local;
1239 #if defined(CONFIG_DEBUG_TCG)
1240 s->temps_in_use++;
1241 #endif
1242 return ts;
1245 TCGv_vec tcg_temp_new_vec(TCGType type)
1247 TCGTemp *t;
1249 #ifdef CONFIG_DEBUG_TCG
1250 switch (type) {
1251 case TCG_TYPE_V64:
1252 assert(TCG_TARGET_HAS_v64);
1253 break;
1254 case TCG_TYPE_V128:
1255 assert(TCG_TARGET_HAS_v128);
1256 break;
1257 case TCG_TYPE_V256:
1258 assert(TCG_TARGET_HAS_v256);
1259 break;
1260 default:
1261 g_assert_not_reached();
1263 #endif
1265 t = tcg_temp_new_internal(type, 0);
1266 return temp_tcgv_vec(t);
1269 /* Create a new temp of the same type as an existing temp. */
1270 TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1272 TCGTemp *t = tcgv_vec_temp(match);
1274 tcg_debug_assert(t->temp_allocated != 0);
1276 t = tcg_temp_new_internal(t->base_type, 0);
1277 return temp_tcgv_vec(t);
1280 void tcg_temp_free_internal(TCGTemp *ts)
1282 TCGContext *s = tcg_ctx;
1283 int k, idx;
1285 #if defined(CONFIG_DEBUG_TCG)
1286 s->temps_in_use--;
1287 if (s->temps_in_use < 0) {
1288 fprintf(stderr, "More temporaries freed than allocated!\n");
1290 #endif
1292 tcg_debug_assert(ts->temp_global == 0);
1293 tcg_debug_assert(ts->temp_allocated != 0);
1294 ts->temp_allocated = 0;
1296 idx = temp_idx(ts);
1297 k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
1298 set_bit(idx, s->free_temps[k].l);
1301 TCGv_i32 tcg_const_i32(int32_t val)
1303 TCGv_i32 t0;
1304 t0 = tcg_temp_new_i32();
1305 tcg_gen_movi_i32(t0, val);
1306 return t0;
1309 TCGv_i64 tcg_const_i64(int64_t val)
1311 TCGv_i64 t0;
1312 t0 = tcg_temp_new_i64();
1313 tcg_gen_movi_i64(t0, val);
1314 return t0;
1317 TCGv_i32 tcg_const_local_i32(int32_t val)
1319 TCGv_i32 t0;
1320 t0 = tcg_temp_local_new_i32();
1321 tcg_gen_movi_i32(t0, val);
1322 return t0;
1325 TCGv_i64 tcg_const_local_i64(int64_t val)
1327 TCGv_i64 t0;
1328 t0 = tcg_temp_local_new_i64();
1329 tcg_gen_movi_i64(t0, val);
1330 return t0;
1333 #if defined(CONFIG_DEBUG_TCG)
1334 void tcg_clear_temp_count(void)
1336 TCGContext *s = tcg_ctx;
1337 s->temps_in_use = 0;
1340 int tcg_check_temp_count(void)
1342 TCGContext *s = tcg_ctx;
1343 if (s->temps_in_use) {
1344 /* Clear the count so that we don't give another
1345 * warning immediately next time around.
1347 s->temps_in_use = 0;
1348 return 1;
1350 return 0;
1352 #endif
1354 /* Return true if OP may appear in the opcode stream.
1355 Test the runtime variable that controls each opcode. */
1356 bool tcg_op_supported(TCGOpcode op)
1358 const bool have_vec
1359 = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1361 switch (op) {
1362 case INDEX_op_discard:
1363 case INDEX_op_set_label:
1364 case INDEX_op_call:
1365 case INDEX_op_br:
1366 case INDEX_op_mb:
1367 case INDEX_op_insn_start:
1368 case INDEX_op_exit_tb:
1369 case INDEX_op_goto_tb:
1370 case INDEX_op_qemu_ld_i32:
1371 case INDEX_op_qemu_st_i32:
1372 case INDEX_op_qemu_ld_i64:
1373 case INDEX_op_qemu_st_i64:
1374 return true;
1376 case INDEX_op_goto_ptr:
1377 return TCG_TARGET_HAS_goto_ptr;
1379 case INDEX_op_mov_i32:
1380 case INDEX_op_movi_i32:
1381 case INDEX_op_setcond_i32:
1382 case INDEX_op_brcond_i32:
1383 case INDEX_op_ld8u_i32:
1384 case INDEX_op_ld8s_i32:
1385 case INDEX_op_ld16u_i32:
1386 case INDEX_op_ld16s_i32:
1387 case INDEX_op_ld_i32:
1388 case INDEX_op_st8_i32:
1389 case INDEX_op_st16_i32:
1390 case INDEX_op_st_i32:
1391 case INDEX_op_add_i32:
1392 case INDEX_op_sub_i32:
1393 case INDEX_op_mul_i32:
1394 case INDEX_op_and_i32:
1395 case INDEX_op_or_i32:
1396 case INDEX_op_xor_i32:
1397 case INDEX_op_shl_i32:
1398 case INDEX_op_shr_i32:
1399 case INDEX_op_sar_i32:
1400 return true;
1402 case INDEX_op_movcond_i32:
1403 return TCG_TARGET_HAS_movcond_i32;
1404 case INDEX_op_div_i32:
1405 case INDEX_op_divu_i32:
1406 return TCG_TARGET_HAS_div_i32;
1407 case INDEX_op_rem_i32:
1408 case INDEX_op_remu_i32:
1409 return TCG_TARGET_HAS_rem_i32;
1410 case INDEX_op_div2_i32:
1411 case INDEX_op_divu2_i32:
1412 return TCG_TARGET_HAS_div2_i32;
1413 case INDEX_op_rotl_i32:
1414 case INDEX_op_rotr_i32:
1415 return TCG_TARGET_HAS_rot_i32;
1416 case INDEX_op_deposit_i32:
1417 return TCG_TARGET_HAS_deposit_i32;
1418 case INDEX_op_extract_i32:
1419 return TCG_TARGET_HAS_extract_i32;
1420 case INDEX_op_sextract_i32:
1421 return TCG_TARGET_HAS_sextract_i32;
1422 case INDEX_op_add2_i32:
1423 return TCG_TARGET_HAS_add2_i32;
1424 case INDEX_op_sub2_i32:
1425 return TCG_TARGET_HAS_sub2_i32;
1426 case INDEX_op_mulu2_i32:
1427 return TCG_TARGET_HAS_mulu2_i32;
1428 case INDEX_op_muls2_i32:
1429 return TCG_TARGET_HAS_muls2_i32;
1430 case INDEX_op_muluh_i32:
1431 return TCG_TARGET_HAS_muluh_i32;
1432 case INDEX_op_mulsh_i32:
1433 return TCG_TARGET_HAS_mulsh_i32;
1434 case INDEX_op_ext8s_i32:
1435 return TCG_TARGET_HAS_ext8s_i32;
1436 case INDEX_op_ext16s_i32:
1437 return TCG_TARGET_HAS_ext16s_i32;
1438 case INDEX_op_ext8u_i32:
1439 return TCG_TARGET_HAS_ext8u_i32;
1440 case INDEX_op_ext16u_i32:
1441 return TCG_TARGET_HAS_ext16u_i32;
1442 case INDEX_op_bswap16_i32:
1443 return TCG_TARGET_HAS_bswap16_i32;
1444 case INDEX_op_bswap32_i32:
1445 return TCG_TARGET_HAS_bswap32_i32;
1446 case INDEX_op_not_i32:
1447 return TCG_TARGET_HAS_not_i32;
1448 case INDEX_op_neg_i32:
1449 return TCG_TARGET_HAS_neg_i32;
1450 case INDEX_op_andc_i32:
1451 return TCG_TARGET_HAS_andc_i32;
1452 case INDEX_op_orc_i32:
1453 return TCG_TARGET_HAS_orc_i32;
1454 case INDEX_op_eqv_i32:
1455 return TCG_TARGET_HAS_eqv_i32;
1456 case INDEX_op_nand_i32:
1457 return TCG_TARGET_HAS_nand_i32;
1458 case INDEX_op_nor_i32:
1459 return TCG_TARGET_HAS_nor_i32;
1460 case INDEX_op_clz_i32:
1461 return TCG_TARGET_HAS_clz_i32;
1462 case INDEX_op_ctz_i32:
1463 return TCG_TARGET_HAS_ctz_i32;
1464 case INDEX_op_ctpop_i32:
1465 return TCG_TARGET_HAS_ctpop_i32;
1467 case INDEX_op_brcond2_i32:
1468 case INDEX_op_setcond2_i32:
1469 return TCG_TARGET_REG_BITS == 32;
1471 case INDEX_op_mov_i64:
1472 case INDEX_op_movi_i64:
1473 case INDEX_op_setcond_i64:
1474 case INDEX_op_brcond_i64:
1475 case INDEX_op_ld8u_i64:
1476 case INDEX_op_ld8s_i64:
1477 case INDEX_op_ld16u_i64:
1478 case INDEX_op_ld16s_i64:
1479 case INDEX_op_ld32u_i64:
1480 case INDEX_op_ld32s_i64:
1481 case INDEX_op_ld_i64:
1482 case INDEX_op_st8_i64:
1483 case INDEX_op_st16_i64:
1484 case INDEX_op_st32_i64:
1485 case INDEX_op_st_i64:
1486 case INDEX_op_add_i64:
1487 case INDEX_op_sub_i64:
1488 case INDEX_op_mul_i64:
1489 case INDEX_op_and_i64:
1490 case INDEX_op_or_i64:
1491 case INDEX_op_xor_i64:
1492 case INDEX_op_shl_i64:
1493 case INDEX_op_shr_i64:
1494 case INDEX_op_sar_i64:
1495 case INDEX_op_ext_i32_i64:
1496 case INDEX_op_extu_i32_i64:
1497 return TCG_TARGET_REG_BITS == 64;
1499 case INDEX_op_movcond_i64:
1500 return TCG_TARGET_HAS_movcond_i64;
1501 case INDEX_op_div_i64:
1502 case INDEX_op_divu_i64:
1503 return TCG_TARGET_HAS_div_i64;
1504 case INDEX_op_rem_i64:
1505 case INDEX_op_remu_i64:
1506 return TCG_TARGET_HAS_rem_i64;
1507 case INDEX_op_div2_i64:
1508 case INDEX_op_divu2_i64:
1509 return TCG_TARGET_HAS_div2_i64;
1510 case INDEX_op_rotl_i64:
1511 case INDEX_op_rotr_i64:
1512 return TCG_TARGET_HAS_rot_i64;
1513 case INDEX_op_deposit_i64:
1514 return TCG_TARGET_HAS_deposit_i64;
1515 case INDEX_op_extract_i64:
1516 return TCG_TARGET_HAS_extract_i64;
1517 case INDEX_op_sextract_i64:
1518 return TCG_TARGET_HAS_sextract_i64;
1519 case INDEX_op_extrl_i64_i32:
1520 return TCG_TARGET_HAS_extrl_i64_i32;
1521 case INDEX_op_extrh_i64_i32:
1522 return TCG_TARGET_HAS_extrh_i64_i32;
1523 case INDEX_op_ext8s_i64:
1524 return TCG_TARGET_HAS_ext8s_i64;
1525 case INDEX_op_ext16s_i64:
1526 return TCG_TARGET_HAS_ext16s_i64;
1527 case INDEX_op_ext32s_i64:
1528 return TCG_TARGET_HAS_ext32s_i64;
1529 case INDEX_op_ext8u_i64:
1530 return TCG_TARGET_HAS_ext8u_i64;
1531 case INDEX_op_ext16u_i64:
1532 return TCG_TARGET_HAS_ext16u_i64;
1533 case INDEX_op_ext32u_i64:
1534 return TCG_TARGET_HAS_ext32u_i64;
1535 case INDEX_op_bswap16_i64:
1536 return TCG_TARGET_HAS_bswap16_i64;
1537 case INDEX_op_bswap32_i64:
1538 return TCG_TARGET_HAS_bswap32_i64;
1539 case INDEX_op_bswap64_i64:
1540 return TCG_TARGET_HAS_bswap64_i64;
1541 case INDEX_op_not_i64:
1542 return TCG_TARGET_HAS_not_i64;
1543 case INDEX_op_neg_i64:
1544 return TCG_TARGET_HAS_neg_i64;
1545 case INDEX_op_andc_i64:
1546 return TCG_TARGET_HAS_andc_i64;
1547 case INDEX_op_orc_i64:
1548 return TCG_TARGET_HAS_orc_i64;
1549 case INDEX_op_eqv_i64:
1550 return TCG_TARGET_HAS_eqv_i64;
1551 case INDEX_op_nand_i64:
1552 return TCG_TARGET_HAS_nand_i64;
1553 case INDEX_op_nor_i64:
1554 return TCG_TARGET_HAS_nor_i64;
1555 case INDEX_op_clz_i64:
1556 return TCG_TARGET_HAS_clz_i64;
1557 case INDEX_op_ctz_i64:
1558 return TCG_TARGET_HAS_ctz_i64;
1559 case INDEX_op_ctpop_i64:
1560 return TCG_TARGET_HAS_ctpop_i64;
1561 case INDEX_op_add2_i64:
1562 return TCG_TARGET_HAS_add2_i64;
1563 case INDEX_op_sub2_i64:
1564 return TCG_TARGET_HAS_sub2_i64;
1565 case INDEX_op_mulu2_i64:
1566 return TCG_TARGET_HAS_mulu2_i64;
1567 case INDEX_op_muls2_i64:
1568 return TCG_TARGET_HAS_muls2_i64;
1569 case INDEX_op_muluh_i64:
1570 return TCG_TARGET_HAS_muluh_i64;
1571 case INDEX_op_mulsh_i64:
1572 return TCG_TARGET_HAS_mulsh_i64;
1574 case INDEX_op_mov_vec:
1575 case INDEX_op_dup_vec:
1576 case INDEX_op_dupi_vec:
1577 case INDEX_op_ld_vec:
1578 case INDEX_op_st_vec:
1579 case INDEX_op_add_vec:
1580 case INDEX_op_sub_vec:
1581 case INDEX_op_and_vec:
1582 case INDEX_op_or_vec:
1583 case INDEX_op_xor_vec:
1584 case INDEX_op_cmp_vec:
1585 return have_vec;
1586 case INDEX_op_dup2_vec:
1587 return have_vec && TCG_TARGET_REG_BITS == 32;
1588 case INDEX_op_not_vec:
1589 return have_vec && TCG_TARGET_HAS_not_vec;
1590 case INDEX_op_neg_vec:
1591 return have_vec && TCG_TARGET_HAS_neg_vec;
1592 case INDEX_op_andc_vec:
1593 return have_vec && TCG_TARGET_HAS_andc_vec;
1594 case INDEX_op_orc_vec:
1595 return have_vec && TCG_TARGET_HAS_orc_vec;
1596 case INDEX_op_mul_vec:
1597 return have_vec && TCG_TARGET_HAS_mul_vec;
1598 case INDEX_op_shli_vec:
1599 case INDEX_op_shri_vec:
1600 case INDEX_op_sari_vec:
1601 return have_vec && TCG_TARGET_HAS_shi_vec;
1602 case INDEX_op_shls_vec:
1603 case INDEX_op_shrs_vec:
1604 case INDEX_op_sars_vec:
1605 return have_vec && TCG_TARGET_HAS_shs_vec;
1606 case INDEX_op_shlv_vec:
1607 case INDEX_op_shrv_vec:
1608 case INDEX_op_sarv_vec:
1609 return have_vec && TCG_TARGET_HAS_shv_vec;
1611 default:
1612 tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
1613 return true;
1617 /* Note: we convert the 64 bit args to 32 bit and do some alignment
1618 and endian swap. Maybe it would be better to do the alignment
1619 and endian swap in tcg_reg_alloc_call(). */
1620 void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)
1622 int i, real_args, nb_rets, pi;
1623 unsigned sizemask, flags;
1624 TCGHelperInfo *info;
1625 TCGOp *op;
1627 info = g_hash_table_lookup(helper_table, (gpointer)func);
1628 flags = info->flags;
1629 sizemask = info->sizemask;
1631 #if defined(__sparc__) && !defined(__arch64__) \
1632 && !defined(CONFIG_TCG_INTERPRETER)
1633 /* We have 64-bit values in one register, but need to pass as two
1634 separate parameters. Split them. */
1635 int orig_sizemask = sizemask;
1636 int orig_nargs = nargs;
1637 TCGv_i64 retl, reth;
1638 TCGTemp *split_args[MAX_OPC_PARAM];
1640 retl = NULL;
1641 reth = NULL;
1642 if (sizemask != 0) {
1643 for (i = real_args = 0; i < nargs; ++i) {
1644 int is_64bit = sizemask & (1 << (i+1)*2);
1645 if (is_64bit) {
1646 TCGv_i64 orig = temp_tcgv_i64(args[i]);
1647 TCGv_i32 h = tcg_temp_new_i32();
1648 TCGv_i32 l = tcg_temp_new_i32();
1649 tcg_gen_extr_i64_i32(l, h, orig);
1650 split_args[real_args++] = tcgv_i32_temp(h);
1651 split_args[real_args++] = tcgv_i32_temp(l);
1652 } else {
1653 split_args[real_args++] = args[i];
1656 nargs = real_args;
1657 args = split_args;
1658 sizemask = 0;
1660 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
1661 for (i = 0; i < nargs; ++i) {
1662 int is_64bit = sizemask & (1 << (i+1)*2);
1663 int is_signed = sizemask & (2 << (i+1)*2);
1664 if (!is_64bit) {
1665 TCGv_i64 temp = tcg_temp_new_i64();
1666 TCGv_i64 orig = temp_tcgv_i64(args[i]);
1667 if (is_signed) {
1668 tcg_gen_ext32s_i64(temp, orig);
1669 } else {
1670 tcg_gen_ext32u_i64(temp, orig);
1672 args[i] = tcgv_i64_temp(temp);
1675 #endif /* TCG_TARGET_EXTEND_ARGS */
1677 op = tcg_emit_op(INDEX_op_call);
1679 pi = 0;
1680 if (ret != NULL) {
1681 #if defined(__sparc__) && !defined(__arch64__) \
1682 && !defined(CONFIG_TCG_INTERPRETER)
1683 if (orig_sizemask & 1) {
1684 /* The 32-bit ABI is going to return the 64-bit value in
1685 the %o0/%o1 register pair. Prepare for this by using
1686 two return temporaries, and reassemble below. */
1687 retl = tcg_temp_new_i64();
1688 reth = tcg_temp_new_i64();
1689 op->args[pi++] = tcgv_i64_arg(reth);
1690 op->args[pi++] = tcgv_i64_arg(retl);
1691 nb_rets = 2;
1692 } else {
1693 op->args[pi++] = temp_arg(ret);
1694 nb_rets = 1;
1696 #else
1697 if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
1698 #ifdef HOST_WORDS_BIGENDIAN
1699 op->args[pi++] = temp_arg(ret + 1);
1700 op->args[pi++] = temp_arg(ret);
1701 #else
1702 op->args[pi++] = temp_arg(ret);
1703 op->args[pi++] = temp_arg(ret + 1);
1704 #endif
1705 nb_rets = 2;
1706 } else {
1707 op->args[pi++] = temp_arg(ret);
1708 nb_rets = 1;
1710 #endif
1711 } else {
1712 nb_rets = 0;
1714 TCGOP_CALLO(op) = nb_rets;
1716 real_args = 0;
1717 for (i = 0; i < nargs; i++) {
1718 int is_64bit = sizemask & (1 << (i+1)*2);
1719 if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
1720 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
1721 /* some targets want aligned 64 bit args */
1722 if (real_args & 1) {
1723 op->args[pi++] = TCG_CALL_DUMMY_ARG;
1724 real_args++;
1726 #endif
1727 /* If stack grows up, then we will be placing successive
1728 arguments at lower addresses, which means we need to
1729 reverse the order compared to how we would normally
1730 treat either big or little-endian. For those arguments
1731 that will wind up in registers, this still works for
1732 HPPA (the only current STACK_GROWSUP target) since the
1733 argument registers are *also* allocated in decreasing
1734 order. If another such target is added, this logic may
1735 have to get more complicated to differentiate between
1736 stack arguments and register arguments. */
1737 #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
1738 op->args[pi++] = temp_arg(args[i] + 1);
1739 op->args[pi++] = temp_arg(args[i]);
1740 #else
1741 op->args[pi++] = temp_arg(args[i]);
1742 op->args[pi++] = temp_arg(args[i] + 1);
1743 #endif
1744 real_args += 2;
1745 continue;
1748 op->args[pi++] = temp_arg(args[i]);
1749 real_args++;
1751 op->args[pi++] = (uintptr_t)func;
1752 op->args[pi++] = flags;
1753 TCGOP_CALLI(op) = real_args;
1755 /* Make sure the fields didn't overflow. */
1756 tcg_debug_assert(TCGOP_CALLI(op) == real_args);
1757 tcg_debug_assert(pi <= ARRAY_SIZE(op->args));
1759 #if defined(__sparc__) && !defined(__arch64__) \
1760 && !defined(CONFIG_TCG_INTERPRETER)
1761 /* Free all of the parts we allocated above. */
1762 for (i = real_args = 0; i < orig_nargs; ++i) {
1763 int is_64bit = orig_sizemask & (1 << (i+1)*2);
1764 if (is_64bit) {
1765 tcg_temp_free_internal(args[real_args++]);
1766 tcg_temp_free_internal(args[real_args++]);
1767 } else {
1768 real_args++;
1771 if (orig_sizemask & 1) {
1772 /* The 32-bit ABI returned two 32-bit pieces. Re-assemble them.
1773 Note that describing these as TCGv_i64 eliminates an unnecessary
1774 zero-extension that tcg_gen_concat_i32_i64 would create. */
1775 tcg_gen_concat32_i64(temp_tcgv_i64(ret), retl, reth);
1776 tcg_temp_free_i64(retl);
1777 tcg_temp_free_i64(reth);
1779 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
1780 for (i = 0; i < nargs; ++i) {
1781 int is_64bit = sizemask & (1 << (i+1)*2);
1782 if (!is_64bit) {
1783 tcg_temp_free_internal(args[i]);
1786 #endif /* TCG_TARGET_EXTEND_ARGS */
1789 static void tcg_reg_alloc_start(TCGContext *s)
1791 int i, n;
1792 TCGTemp *ts;
1794 for (i = 0, n = s->nb_globals; i < n; i++) {
1795 ts = &s->temps[i];
1796 ts->val_type = (ts->fixed_reg ? TEMP_VAL_REG : TEMP_VAL_MEM);
1798 for (n = s->nb_temps; i < n; i++) {
1799 ts = &s->temps[i];
1800 ts->val_type = (ts->temp_local ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
1801 ts->mem_allocated = 0;
1802 ts->fixed_reg = 0;
1805 memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
1808 static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
1809 TCGTemp *ts)
1811 int idx = temp_idx(ts);
1813 if (ts->temp_global) {
1814 pstrcpy(buf, buf_size, ts->name);
1815 } else if (ts->temp_local) {
1816 snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
1817 } else {
1818 snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
1820 return buf;
1823 static char *tcg_get_arg_str(TCGContext *s, char *buf,
1824 int buf_size, TCGArg arg)
1826 return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
1829 /* Find helper name. */
1830 static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
1832 const char *ret = NULL;
1833 if (helper_table) {
1834 TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val);
1835 if (info) {
1836 ret = info->name;
1839 return ret;
1842 static const char * const cond_name[] =
1844 [TCG_COND_NEVER] = "never",
1845 [TCG_COND_ALWAYS] = "always",
1846 [TCG_COND_EQ] = "eq",
1847 [TCG_COND_NE] = "ne",
1848 [TCG_COND_LT] = "lt",
1849 [TCG_COND_GE] = "ge",
1850 [TCG_COND_LE] = "le",
1851 [TCG_COND_GT] = "gt",
1852 [TCG_COND_LTU] = "ltu",
1853 [TCG_COND_GEU] = "geu",
1854 [TCG_COND_LEU] = "leu",
1855 [TCG_COND_GTU] = "gtu"
1858 static const char * const ldst_name[] =
1860 [MO_UB] = "ub",
1861 [MO_SB] = "sb",
1862 [MO_LEUW] = "leuw",
1863 [MO_LESW] = "lesw",
1864 [MO_LEUL] = "leul",
1865 [MO_LESL] = "lesl",
1866 [MO_LEQ] = "leq",
1867 [MO_BEUW] = "beuw",
1868 [MO_BESW] = "besw",
1869 [MO_BEUL] = "beul",
1870 [MO_BESL] = "besl",
1871 [MO_BEQ] = "beq",
1874 static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
1875 #ifdef ALIGNED_ONLY
1876 [MO_UNALN >> MO_ASHIFT] = "un+",
1877 [MO_ALIGN >> MO_ASHIFT] = "",
1878 #else
1879 [MO_UNALN >> MO_ASHIFT] = "",
1880 [MO_ALIGN >> MO_ASHIFT] = "al+",
1881 #endif
1882 [MO_ALIGN_2 >> MO_ASHIFT] = "al2+",
1883 [MO_ALIGN_4 >> MO_ASHIFT] = "al4+",
1884 [MO_ALIGN_8 >> MO_ASHIFT] = "al8+",
1885 [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
1886 [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
1887 [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
1890 static inline bool tcg_regset_single(TCGRegSet d)
1892 return (d & (d - 1)) == 0;
1895 static inline TCGReg tcg_regset_first(TCGRegSet d)
1897 if (TCG_TARGET_NB_REGS <= 32) {
1898 return ctz32(d);
1899 } else {
1900 return ctz64(d);
1904 static void tcg_dump_ops(TCGContext *s, bool have_prefs)
1906 char buf[128];
1907 TCGOp *op;
1909 QTAILQ_FOREACH(op, &s->ops, link) {
1910 int i, k, nb_oargs, nb_iargs, nb_cargs;
1911 const TCGOpDef *def;
1912 TCGOpcode c;
1913 int col = 0;
1915 c = op->opc;
1916 def = &tcg_op_defs[c];
1918 if (c == INDEX_op_insn_start) {
1919 nb_oargs = 0;
1920 col += qemu_log("\n ----");
1922 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
1923 target_ulong a;
1924 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
1925 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
1926 #else
1927 a = op->args[i];
1928 #endif
1929 col += qemu_log(" " TARGET_FMT_lx, a);
1931 } else if (c == INDEX_op_call) {
1932 /* variable number of arguments */
1933 nb_oargs = TCGOP_CALLO(op);
1934 nb_iargs = TCGOP_CALLI(op);
1935 nb_cargs = def->nb_cargs;
1937 /* function name, flags, out args */
1938 col += qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
1939 tcg_find_helper(s, op->args[nb_oargs + nb_iargs]),
1940 op->args[nb_oargs + nb_iargs + 1], nb_oargs);
1941 for (i = 0; i < nb_oargs; i++) {
1942 col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
1943 op->args[i]));
1945 for (i = 0; i < nb_iargs; i++) {
1946 TCGArg arg = op->args[nb_oargs + i];
1947 const char *t = "<dummy>";
1948 if (arg != TCG_CALL_DUMMY_ARG) {
1949 t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
1951 col += qemu_log(",%s", t);
1953 } else {
1954 col += qemu_log(" %s ", def->name);
1956 nb_oargs = def->nb_oargs;
1957 nb_iargs = def->nb_iargs;
1958 nb_cargs = def->nb_cargs;
1960 if (def->flags & TCG_OPF_VECTOR) {
1961 col += qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op),
1962 8 << TCGOP_VECE(op));
1965 k = 0;
1966 for (i = 0; i < nb_oargs; i++) {
1967 if (k != 0) {
1968 col += qemu_log(",");
1970 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
1971 op->args[k++]));
1973 for (i = 0; i < nb_iargs; i++) {
1974 if (k != 0) {
1975 col += qemu_log(",");
1977 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
1978 op->args[k++]));
1980 switch (c) {
1981 case INDEX_op_brcond_i32:
1982 case INDEX_op_setcond_i32:
1983 case INDEX_op_movcond_i32:
1984 case INDEX_op_brcond2_i32:
1985 case INDEX_op_setcond2_i32:
1986 case INDEX_op_brcond_i64:
1987 case INDEX_op_setcond_i64:
1988 case INDEX_op_movcond_i64:
1989 case INDEX_op_cmp_vec:
1990 if (op->args[k] < ARRAY_SIZE(cond_name)
1991 && cond_name[op->args[k]]) {
1992 col += qemu_log(",%s", cond_name[op->args[k++]]);
1993 } else {
1994 col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]);
1996 i = 1;
1997 break;
1998 case INDEX_op_qemu_ld_i32:
1999 case INDEX_op_qemu_st_i32:
2000 case INDEX_op_qemu_ld_i64:
2001 case INDEX_op_qemu_st_i64:
2003 TCGMemOpIdx oi = op->args[k++];
2004 TCGMemOp op = get_memop(oi);
2005 unsigned ix = get_mmuidx(oi);
2007 if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
2008 col += qemu_log(",$0x%x,%u", op, ix);
2009 } else {
2010 const char *s_al, *s_op;
2011 s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT];
2012 s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
2013 col += qemu_log(",%s%s,%u", s_al, s_op, ix);
2015 i = 1;
2017 break;
2018 default:
2019 i = 0;
2020 break;
2022 switch (c) {
2023 case INDEX_op_set_label:
2024 case INDEX_op_br:
2025 case INDEX_op_brcond_i32:
2026 case INDEX_op_brcond_i64:
2027 case INDEX_op_brcond2_i32:
2028 col += qemu_log("%s$L%d", k ? "," : "",
2029 arg_label(op->args[k])->id);
2030 i++, k++;
2031 break;
2032 default:
2033 break;
2035 for (; i < nb_cargs; i++, k++) {
2036 col += qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", op->args[k]);
2040 if (have_prefs || op->life) {
2041 for (; col < 40; ++col) {
2042 putc(' ', qemu_logfile);
2046 if (op->life) {
2047 unsigned life = op->life;
2049 if (life & (SYNC_ARG * 3)) {
2050 qemu_log(" sync:");
2051 for (i = 0; i < 2; ++i) {
2052 if (life & (SYNC_ARG << i)) {
2053 qemu_log(" %d", i);
2057 life /= DEAD_ARG;
2058 if (life) {
2059 qemu_log(" dead:");
2060 for (i = 0; life; ++i, life >>= 1) {
2061 if (life & 1) {
2062 qemu_log(" %d", i);
2068 if (have_prefs) {
2069 for (i = 0; i < nb_oargs; ++i) {
2070 TCGRegSet set = op->output_pref[i];
2072 if (i == 0) {
2073 qemu_log(" pref=");
2074 } else {
2075 qemu_log(",");
2077 if (set == 0) {
2078 qemu_log("none");
2079 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
2080 qemu_log("all");
2081 #ifdef CONFIG_DEBUG_TCG
2082 } else if (tcg_regset_single(set)) {
2083 TCGReg reg = tcg_regset_first(set);
2084 qemu_log("%s", tcg_target_reg_names[reg]);
2085 #endif
2086 } else if (TCG_TARGET_NB_REGS <= 32) {
2087 qemu_log("%#x", (uint32_t)set);
2088 } else {
2089 qemu_log("%#" PRIx64, (uint64_t)set);
2094 qemu_log("\n");
2098 /* we give more priority to constraints with less registers */
2099 static int get_constraint_priority(const TCGOpDef *def, int k)
2101 const TCGArgConstraint *arg_ct;
2103 int i, n;
2104 arg_ct = &def->args_ct[k];
2105 if (arg_ct->ct & TCG_CT_ALIAS) {
2106 /* an alias is equivalent to a single register */
2107 n = 1;
2108 } else {
2109 if (!(arg_ct->ct & TCG_CT_REG))
2110 return 0;
2111 n = 0;
2112 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2113 if (tcg_regset_test_reg(arg_ct->u.regs, i))
2114 n++;
2117 return TCG_TARGET_NB_REGS - n + 1;
2120 /* sort from highest priority to lowest */
2121 static void sort_constraints(TCGOpDef *def, int start, int n)
2123 int i, j, p1, p2, tmp;
2125 for(i = 0; i < n; i++)
2126 def->sorted_args[start + i] = start + i;
2127 if (n <= 1)
2128 return;
2129 for(i = 0; i < n - 1; i++) {
2130 for(j = i + 1; j < n; j++) {
2131 p1 = get_constraint_priority(def, def->sorted_args[start + i]);
2132 p2 = get_constraint_priority(def, def->sorted_args[start + j]);
2133 if (p1 < p2) {
2134 tmp = def->sorted_args[start + i];
2135 def->sorted_args[start + i] = def->sorted_args[start + j];
2136 def->sorted_args[start + j] = tmp;
2142 static void process_op_defs(TCGContext *s)
2144 TCGOpcode op;
2146 for (op = 0; op < NB_OPS; op++) {
2147 TCGOpDef *def = &tcg_op_defs[op];
2148 const TCGTargetOpDef *tdefs;
2149 TCGType type;
2150 int i, nb_args;
2152 if (def->flags & TCG_OPF_NOT_PRESENT) {
2153 continue;
2156 nb_args = def->nb_iargs + def->nb_oargs;
2157 if (nb_args == 0) {
2158 continue;
2161 tdefs = tcg_target_op_def(op);
2162 /* Missing TCGTargetOpDef entry. */
2163 tcg_debug_assert(tdefs != NULL);
2165 type = (def->flags & TCG_OPF_64BIT ? TCG_TYPE_I64 : TCG_TYPE_I32);
2166 for (i = 0; i < nb_args; i++) {
2167 const char *ct_str = tdefs->args_ct_str[i];
2168 /* Incomplete TCGTargetOpDef entry. */
2169 tcg_debug_assert(ct_str != NULL);
2171 def->args_ct[i].u.regs = 0;
2172 def->args_ct[i].ct = 0;
2173 while (*ct_str != '\0') {
2174 switch(*ct_str) {
2175 case '0' ... '9':
2177 int oarg = *ct_str - '0';
2178 tcg_debug_assert(ct_str == tdefs->args_ct_str[i]);
2179 tcg_debug_assert(oarg < def->nb_oargs);
2180 tcg_debug_assert(def->args_ct[oarg].ct & TCG_CT_REG);
2181 /* TCG_CT_ALIAS is for the output arguments.
2182 The input is tagged with TCG_CT_IALIAS. */
2183 def->args_ct[i] = def->args_ct[oarg];
2184 def->args_ct[oarg].ct |= TCG_CT_ALIAS;
2185 def->args_ct[oarg].alias_index = i;
2186 def->args_ct[i].ct |= TCG_CT_IALIAS;
2187 def->args_ct[i].alias_index = oarg;
2189 ct_str++;
2190 break;
2191 case '&':
2192 def->args_ct[i].ct |= TCG_CT_NEWREG;
2193 ct_str++;
2194 break;
2195 case 'i':
2196 def->args_ct[i].ct |= TCG_CT_CONST;
2197 ct_str++;
2198 break;
2199 default:
2200 ct_str = target_parse_constraint(&def->args_ct[i],
2201 ct_str, type);
2202 /* Typo in TCGTargetOpDef constraint. */
2203 tcg_debug_assert(ct_str != NULL);
2208 /* TCGTargetOpDef entry with too much information? */
2209 tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
2211 /* sort the constraints (XXX: this is just an heuristic) */
2212 sort_constraints(def, 0, def->nb_oargs);
2213 sort_constraints(def, def->nb_oargs, def->nb_iargs);
2217 void tcg_op_remove(TCGContext *s, TCGOp *op)
2219 TCGLabel *label;
2221 switch (op->opc) {
2222 case INDEX_op_br:
2223 label = arg_label(op->args[0]);
2224 label->refs--;
2225 break;
2226 case INDEX_op_brcond_i32:
2227 case INDEX_op_brcond_i64:
2228 label = arg_label(op->args[3]);
2229 label->refs--;
2230 break;
2231 case INDEX_op_brcond2_i32:
2232 label = arg_label(op->args[5]);
2233 label->refs--;
2234 break;
2235 default:
2236 break;
2239 QTAILQ_REMOVE(&s->ops, op, link);
2240 QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
2241 s->nb_ops--;
2243 #ifdef CONFIG_PROFILER
2244 atomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
2245 #endif
2248 static TCGOp *tcg_op_alloc(TCGOpcode opc)
2250 TCGContext *s = tcg_ctx;
2251 TCGOp *op;
2253 if (likely(QTAILQ_EMPTY(&s->free_ops))) {
2254 op = tcg_malloc(sizeof(TCGOp));
2255 } else {
2256 op = QTAILQ_FIRST(&s->free_ops);
2257 QTAILQ_REMOVE(&s->free_ops, op, link);
2259 memset(op, 0, offsetof(TCGOp, link));
2260 op->opc = opc;
2261 s->nb_ops++;
2263 return op;
2266 TCGOp *tcg_emit_op(TCGOpcode opc)
2268 TCGOp *op = tcg_op_alloc(opc);
2269 QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2270 return op;
2273 TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
2275 TCGOp *new_op = tcg_op_alloc(opc);
2276 QTAILQ_INSERT_BEFORE(old_op, new_op, link);
2277 return new_op;
2280 TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
2282 TCGOp *new_op = tcg_op_alloc(opc);
2283 QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
2284 return new_op;
2287 /* Reachable analysis : remove unreachable code. */
2288 static void reachable_code_pass(TCGContext *s)
2290 TCGOp *op, *op_next;
2291 bool dead = false;
2293 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2294 bool remove = dead;
2295 TCGLabel *label;
2296 int call_flags;
2298 switch (op->opc) {
2299 case INDEX_op_set_label:
2300 label = arg_label(op->args[0]);
2301 if (label->refs == 0) {
2303 * While there is an occasional backward branch, virtually
2304 * all branches generated by the translators are forward.
2305 * Which means that generally we will have already removed
2306 * all references to the label that will be, and there is
2307 * little to be gained by iterating.
2309 remove = true;
2310 } else {
2311 /* Once we see a label, insns become live again. */
2312 dead = false;
2313 remove = false;
2316 * Optimization can fold conditional branches to unconditional.
2317 * If we find a label with one reference which is preceded by
2318 * an unconditional branch to it, remove both. This needed to
2319 * wait until the dead code in between them was removed.
2321 if (label->refs == 1) {
2322 TCGOp *op_prev = QTAILQ_PREV(op, TCGOpHead, link);
2323 if (op_prev->opc == INDEX_op_br &&
2324 label == arg_label(op_prev->args[0])) {
2325 tcg_op_remove(s, op_prev);
2326 remove = true;
2330 break;
2332 case INDEX_op_br:
2333 case INDEX_op_exit_tb:
2334 case INDEX_op_goto_ptr:
2335 /* Unconditional branches; everything following is dead. */
2336 dead = true;
2337 break;
2339 case INDEX_op_call:
2340 /* Notice noreturn helper calls, raising exceptions. */
2341 call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1];
2342 if (call_flags & TCG_CALL_NO_RETURN) {
2343 dead = true;
2345 break;
2347 case INDEX_op_insn_start:
2348 /* Never remove -- we need to keep these for unwind. */
2349 remove = false;
2350 break;
2352 default:
2353 break;
2356 if (remove) {
2357 tcg_op_remove(s, op);
2362 #define TS_DEAD 1
2363 #define TS_MEM 2
2365 #define IS_DEAD_ARG(n) (arg_life & (DEAD_ARG << (n)))
2366 #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
2368 /* liveness analysis: end of function: all temps are dead, and globals
2369 should be in memory. */
2370 static void la_func_end(TCGContext *s, int ng, int nt)
2372 int i;
2374 for (i = 0; i < ng; ++i) {
2375 s->temps[i].state = TS_DEAD | TS_MEM;
2377 for (i = ng; i < nt; ++i) {
2378 s->temps[i].state = TS_DEAD;
2382 /* liveness analysis: end of basic block: all temps are dead, globals
2383 and local temps should be in memory. */
2384 static void la_bb_end(TCGContext *s, int ng, int nt)
2386 int i;
2388 for (i = 0; i < ng; ++i) {
2389 s->temps[i].state = TS_DEAD | TS_MEM;
2391 for (i = ng; i < nt; ++i) {
2392 s->temps[i].state = (s->temps[i].temp_local
2393 ? TS_DEAD | TS_MEM
2394 : TS_DEAD);
2398 /* Liveness analysis : update the opc_arg_life array to tell if a
2399 given input arguments is dead. Instructions updating dead
2400 temporaries are removed. */
2401 static void liveness_pass_1(TCGContext *s)
2403 int nb_globals = s->nb_globals;
2404 int nb_temps = s->nb_temps;
2405 TCGOp *op, *op_prev;
2407 la_func_end(s, nb_globals, nb_temps);
2409 QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, TCGOpHead, link, op_prev) {
2410 int i, nb_iargs, nb_oargs;
2411 TCGOpcode opc_new, opc_new2;
2412 bool have_opc_new2;
2413 TCGLifeData arg_life = 0;
2414 TCGTemp *arg_ts;
2415 TCGOpcode opc = op->opc;
2416 const TCGOpDef *def = &tcg_op_defs[opc];
2418 switch (opc) {
2419 case INDEX_op_call:
2421 int call_flags;
2423 nb_oargs = TCGOP_CALLO(op);
2424 nb_iargs = TCGOP_CALLI(op);
2425 call_flags = op->args[nb_oargs + nb_iargs + 1];
2427 /* pure functions can be removed if their result is unused */
2428 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
2429 for (i = 0; i < nb_oargs; i++) {
2430 arg_ts = arg_temp(op->args[i]);
2431 if (arg_ts->state != TS_DEAD) {
2432 goto do_not_remove_call;
2435 goto do_remove;
2437 do_not_remove_call:
2439 /* output args are dead */
2440 for (i = 0; i < nb_oargs; i++) {
2441 arg_ts = arg_temp(op->args[i]);
2442 if (arg_ts->state & TS_DEAD) {
2443 arg_life |= DEAD_ARG << i;
2445 if (arg_ts->state & TS_MEM) {
2446 arg_life |= SYNC_ARG << i;
2448 arg_ts->state = TS_DEAD;
2451 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
2452 TCG_CALL_NO_READ_GLOBALS))) {
2453 /* globals should go back to memory */
2454 for (i = 0; i < nb_globals; i++) {
2455 s->temps[i].state = TS_DEAD | TS_MEM;
2457 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
2458 /* globals should be synced to memory */
2459 for (i = 0; i < nb_globals; i++) {
2460 s->temps[i].state |= TS_MEM;
2464 /* record arguments that die in this helper */
2465 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2466 arg_ts = arg_temp(op->args[i]);
2467 if (arg_ts && arg_ts->state & TS_DEAD) {
2468 arg_life |= DEAD_ARG << i;
2471 /* input arguments are live for preceding opcodes */
2472 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2473 arg_ts = arg_temp(op->args[i]);
2474 if (arg_ts) {
2475 arg_ts->state &= ~TS_DEAD;
2479 break;
2480 case INDEX_op_insn_start:
2481 break;
2482 case INDEX_op_discard:
2483 /* mark the temporary as dead */
2484 arg_temp(op->args[0])->state = TS_DEAD;
2485 break;
2487 case INDEX_op_add2_i32:
2488 opc_new = INDEX_op_add_i32;
2489 goto do_addsub2;
2490 case INDEX_op_sub2_i32:
2491 opc_new = INDEX_op_sub_i32;
2492 goto do_addsub2;
2493 case INDEX_op_add2_i64:
2494 opc_new = INDEX_op_add_i64;
2495 goto do_addsub2;
2496 case INDEX_op_sub2_i64:
2497 opc_new = INDEX_op_sub_i64;
2498 do_addsub2:
2499 nb_iargs = 4;
2500 nb_oargs = 2;
2501 /* Test if the high part of the operation is dead, but not
2502 the low part. The result can be optimized to a simple
2503 add or sub. This happens often for x86_64 guest when the
2504 cpu mode is set to 32 bit. */
2505 if (arg_temp(op->args[1])->state == TS_DEAD) {
2506 if (arg_temp(op->args[0])->state == TS_DEAD) {
2507 goto do_remove;
2509 /* Replace the opcode and adjust the args in place,
2510 leaving 3 unused args at the end. */
2511 op->opc = opc = opc_new;
2512 op->args[1] = op->args[2];
2513 op->args[2] = op->args[4];
2514 /* Fall through and mark the single-word operation live. */
2515 nb_iargs = 2;
2516 nb_oargs = 1;
2518 goto do_not_remove;
2520 case INDEX_op_mulu2_i32:
2521 opc_new = INDEX_op_mul_i32;
2522 opc_new2 = INDEX_op_muluh_i32;
2523 have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
2524 goto do_mul2;
2525 case INDEX_op_muls2_i32:
2526 opc_new = INDEX_op_mul_i32;
2527 opc_new2 = INDEX_op_mulsh_i32;
2528 have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
2529 goto do_mul2;
2530 case INDEX_op_mulu2_i64:
2531 opc_new = INDEX_op_mul_i64;
2532 opc_new2 = INDEX_op_muluh_i64;
2533 have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
2534 goto do_mul2;
2535 case INDEX_op_muls2_i64:
2536 opc_new = INDEX_op_mul_i64;
2537 opc_new2 = INDEX_op_mulsh_i64;
2538 have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
2539 goto do_mul2;
2540 do_mul2:
2541 nb_iargs = 2;
2542 nb_oargs = 2;
2543 if (arg_temp(op->args[1])->state == TS_DEAD) {
2544 if (arg_temp(op->args[0])->state == TS_DEAD) {
2545 /* Both parts of the operation are dead. */
2546 goto do_remove;
2548 /* The high part of the operation is dead; generate the low. */
2549 op->opc = opc = opc_new;
2550 op->args[1] = op->args[2];
2551 op->args[2] = op->args[3];
2552 } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
2553 /* The low part of the operation is dead; generate the high. */
2554 op->opc = opc = opc_new2;
2555 op->args[0] = op->args[1];
2556 op->args[1] = op->args[2];
2557 op->args[2] = op->args[3];
2558 } else {
2559 goto do_not_remove;
2561 /* Mark the single-word operation live. */
2562 nb_oargs = 1;
2563 goto do_not_remove;
2565 default:
2566 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
2567 nb_iargs = def->nb_iargs;
2568 nb_oargs = def->nb_oargs;
2570 /* Test if the operation can be removed because all
2571 its outputs are dead. We assume that nb_oargs == 0
2572 implies side effects */
2573 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
2574 for (i = 0; i < nb_oargs; i++) {
2575 if (arg_temp(op->args[i])->state != TS_DEAD) {
2576 goto do_not_remove;
2579 goto do_remove;
2581 goto do_not_remove;
2583 do_remove:
2584 tcg_op_remove(s, op);
2585 break;
2587 do_not_remove:
2588 /* output args are dead */
2589 for (i = 0; i < nb_oargs; i++) {
2590 arg_ts = arg_temp(op->args[i]);
2591 if (arg_ts->state & TS_DEAD) {
2592 arg_life |= DEAD_ARG << i;
2594 if (arg_ts->state & TS_MEM) {
2595 arg_life |= SYNC_ARG << i;
2597 arg_ts->state = TS_DEAD;
2600 /* if end of basic block, update */
2601 if (def->flags & TCG_OPF_BB_END) {
2602 la_bb_end(s, nb_globals, nb_temps);
2603 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
2604 /* globals should be synced to memory */
2605 for (i = 0; i < nb_globals; i++) {
2606 s->temps[i].state |= TS_MEM;
2610 /* record arguments that die in this opcode */
2611 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2612 arg_ts = arg_temp(op->args[i]);
2613 if (arg_ts->state & TS_DEAD) {
2614 arg_life |= DEAD_ARG << i;
2617 /* input arguments are live for preceding opcodes */
2618 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2619 arg_temp(op->args[i])->state &= ~TS_DEAD;
2621 break;
2623 op->life = arg_life;
2624 op->output_pref[0] = 0;
2625 op->output_pref[1] = 0;
2629 /* Liveness analysis: Convert indirect regs to direct temporaries. */
2630 static bool liveness_pass_2(TCGContext *s)
2632 int nb_globals = s->nb_globals;
2633 int nb_temps, i;
2634 bool changes = false;
2635 TCGOp *op, *op_next;
2637 /* Create a temporary for each indirect global. */
2638 for (i = 0; i < nb_globals; ++i) {
2639 TCGTemp *its = &s->temps[i];
2640 if (its->indirect_reg) {
2641 TCGTemp *dts = tcg_temp_alloc(s);
2642 dts->type = its->type;
2643 dts->base_type = its->base_type;
2644 its->state_ptr = dts;
2645 } else {
2646 its->state_ptr = NULL;
2648 /* All globals begin dead. */
2649 its->state = TS_DEAD;
2651 for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
2652 TCGTemp *its = &s->temps[i];
2653 its->state_ptr = NULL;
2654 its->state = TS_DEAD;
2657 QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2658 TCGOpcode opc = op->opc;
2659 const TCGOpDef *def = &tcg_op_defs[opc];
2660 TCGLifeData arg_life = op->life;
2661 int nb_iargs, nb_oargs, call_flags;
2662 TCGTemp *arg_ts, *dir_ts;
2664 if (opc == INDEX_op_call) {
2665 nb_oargs = TCGOP_CALLO(op);
2666 nb_iargs = TCGOP_CALLI(op);
2667 call_flags = op->args[nb_oargs + nb_iargs + 1];
2668 } else {
2669 nb_iargs = def->nb_iargs;
2670 nb_oargs = def->nb_oargs;
2672 /* Set flags similar to how calls require. */
2673 if (def->flags & TCG_OPF_BB_END) {
2674 /* Like writing globals: save_globals */
2675 call_flags = 0;
2676 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
2677 /* Like reading globals: sync_globals */
2678 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
2679 } else {
2680 /* No effect on globals. */
2681 call_flags = (TCG_CALL_NO_READ_GLOBALS |
2682 TCG_CALL_NO_WRITE_GLOBALS);
2686 /* Make sure that input arguments are available. */
2687 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2688 arg_ts = arg_temp(op->args[i]);
2689 if (arg_ts) {
2690 dir_ts = arg_ts->state_ptr;
2691 if (dir_ts && arg_ts->state == TS_DEAD) {
2692 TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
2693 ? INDEX_op_ld_i32
2694 : INDEX_op_ld_i64);
2695 TCGOp *lop = tcg_op_insert_before(s, op, lopc);
2697 lop->args[0] = temp_arg(dir_ts);
2698 lop->args[1] = temp_arg(arg_ts->mem_base);
2699 lop->args[2] = arg_ts->mem_offset;
2701 /* Loaded, but synced with memory. */
2702 arg_ts->state = TS_MEM;
2707 /* Perform input replacement, and mark inputs that became dead.
2708 No action is required except keeping temp_state up to date
2709 so that we reload when needed. */
2710 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2711 arg_ts = arg_temp(op->args[i]);
2712 if (arg_ts) {
2713 dir_ts = arg_ts->state_ptr;
2714 if (dir_ts) {
2715 op->args[i] = temp_arg(dir_ts);
2716 changes = true;
2717 if (IS_DEAD_ARG(i)) {
2718 arg_ts->state = TS_DEAD;
2724 /* Liveness analysis should ensure that the following are
2725 all correct, for call sites and basic block end points. */
2726 if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
2727 /* Nothing to do */
2728 } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
2729 for (i = 0; i < nb_globals; ++i) {
2730 /* Liveness should see that globals are synced back,
2731 that is, either TS_DEAD or TS_MEM. */
2732 arg_ts = &s->temps[i];
2733 tcg_debug_assert(arg_ts->state_ptr == 0
2734 || arg_ts->state != 0);
2736 } else {
2737 for (i = 0; i < nb_globals; ++i) {
2738 /* Liveness should see that globals are saved back,
2739 that is, TS_DEAD, waiting to be reloaded. */
2740 arg_ts = &s->temps[i];
2741 tcg_debug_assert(arg_ts->state_ptr == 0
2742 || arg_ts->state == TS_DEAD);
2746 /* Outputs become available. */
2747 for (i = 0; i < nb_oargs; i++) {
2748 arg_ts = arg_temp(op->args[i]);
2749 dir_ts = arg_ts->state_ptr;
2750 if (!dir_ts) {
2751 continue;
2753 op->args[i] = temp_arg(dir_ts);
2754 changes = true;
2756 /* The output is now live and modified. */
2757 arg_ts->state = 0;
2759 /* Sync outputs upon their last write. */
2760 if (NEED_SYNC_ARG(i)) {
2761 TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
2762 ? INDEX_op_st_i32
2763 : INDEX_op_st_i64);
2764 TCGOp *sop = tcg_op_insert_after(s, op, sopc);
2766 sop->args[0] = temp_arg(dir_ts);
2767 sop->args[1] = temp_arg(arg_ts->mem_base);
2768 sop->args[2] = arg_ts->mem_offset;
2770 arg_ts->state = TS_MEM;
2772 /* Drop outputs that are dead. */
2773 if (IS_DEAD_ARG(i)) {
2774 arg_ts->state = TS_DEAD;
2779 return changes;
2782 #ifdef CONFIG_DEBUG_TCG
2783 static void dump_regs(TCGContext *s)
2785 TCGTemp *ts;
2786 int i;
2787 char buf[64];
2789 for(i = 0; i < s->nb_temps; i++) {
2790 ts = &s->temps[i];
2791 printf(" %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
2792 switch(ts->val_type) {
2793 case TEMP_VAL_REG:
2794 printf("%s", tcg_target_reg_names[ts->reg]);
2795 break;
2796 case TEMP_VAL_MEM:
2797 printf("%d(%s)", (int)ts->mem_offset,
2798 tcg_target_reg_names[ts->mem_base->reg]);
2799 break;
2800 case TEMP_VAL_CONST:
2801 printf("$0x%" TCG_PRIlx, ts->val);
2802 break;
2803 case TEMP_VAL_DEAD:
2804 printf("D");
2805 break;
2806 default:
2807 printf("???");
2808 break;
2810 printf("\n");
2813 for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2814 if (s->reg_to_temp[i] != NULL) {
2815 printf("%s: %s\n",
2816 tcg_target_reg_names[i],
2817 tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
2822 static void check_regs(TCGContext *s)
2824 int reg;
2825 int k;
2826 TCGTemp *ts;
2827 char buf[64];
2829 for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2830 ts = s->reg_to_temp[reg];
2831 if (ts != NULL) {
2832 if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
2833 printf("Inconsistency for register %s:\n",
2834 tcg_target_reg_names[reg]);
2835 goto fail;
2839 for (k = 0; k < s->nb_temps; k++) {
2840 ts = &s->temps[k];
2841 if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg
2842 && s->reg_to_temp[ts->reg] != ts) {
2843 printf("Inconsistency for temp %s:\n",
2844 tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
2845 fail:
2846 printf("reg state:\n");
2847 dump_regs(s);
2848 tcg_abort();
2852 #endif
2854 static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
2856 #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
2857 /* Sparc64 stack is accessed with offset of 2047 */
2858 s->current_frame_offset = (s->current_frame_offset +
2859 (tcg_target_long)sizeof(tcg_target_long) - 1) &
2860 ~(sizeof(tcg_target_long) - 1);
2861 #endif
2862 if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
2863 s->frame_end) {
2864 tcg_abort();
2866 ts->mem_offset = s->current_frame_offset;
2867 ts->mem_base = s->frame_temp;
2868 ts->mem_allocated = 1;
2869 s->current_frame_offset += sizeof(tcg_target_long);
2872 static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
2874 /* Mark a temporary as free or dead. If 'free_or_dead' is negative,
2875 mark it free; otherwise mark it dead. */
2876 static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
2878 if (ts->fixed_reg) {
2879 return;
2881 if (ts->val_type == TEMP_VAL_REG) {
2882 s->reg_to_temp[ts->reg] = NULL;
2884 ts->val_type = (free_or_dead < 0
2885 || ts->temp_local
2886 || ts->temp_global
2887 ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
2890 /* Mark a temporary as dead. */
2891 static inline void temp_dead(TCGContext *s, TCGTemp *ts)
2893 temp_free_or_dead(s, ts, 1);
2896 /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
2897 registers needs to be allocated to store a constant. If 'free_or_dead'
2898 is non-zero, subsequently release the temporary; if it is positive, the
2899 temp is dead; if it is negative, the temp is free. */
2900 static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
2901 TCGRegSet preferred_regs, int free_or_dead)
2903 if (ts->fixed_reg) {
2904 return;
2906 if (!ts->mem_coherent) {
2907 if (!ts->mem_allocated) {
2908 temp_allocate_frame(s, ts);
2910 switch (ts->val_type) {
2911 case TEMP_VAL_CONST:
2912 /* If we're going to free the temp immediately, then we won't
2913 require it later in a register, so attempt to store the
2914 constant to memory directly. */
2915 if (free_or_dead
2916 && tcg_out_sti(s, ts->type, ts->val,
2917 ts->mem_base->reg, ts->mem_offset)) {
2918 break;
2920 temp_load(s, ts, tcg_target_available_regs[ts->type],
2921 allocated_regs, preferred_regs);
2922 /* fallthrough */
2924 case TEMP_VAL_REG:
2925 tcg_out_st(s, ts->type, ts->reg,
2926 ts->mem_base->reg, ts->mem_offset);
2927 break;
2929 case TEMP_VAL_MEM:
2930 break;
2932 case TEMP_VAL_DEAD:
2933 default:
2934 tcg_abort();
2936 ts->mem_coherent = 1;
2938 if (free_or_dead) {
2939 temp_free_or_dead(s, ts, free_or_dead);
2943 /* free register 'reg' by spilling the corresponding temporary if necessary */
2944 static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
2946 TCGTemp *ts = s->reg_to_temp[reg];
2947 if (ts != NULL) {
2948 temp_sync(s, ts, allocated_regs, 0, -1);
2953 * tcg_reg_alloc:
2954 * @required_regs: Set of registers in which we must allocate.
2955 * @allocated_regs: Set of registers which must be avoided.
2956 * @preferred_regs: Set of registers we should prefer.
2957 * @rev: True if we search the registers in "indirect" order.
2959 * The allocated register must be in @required_regs & ~@allocated_regs,
2960 * but if we can put it in @preferred_regs we may save a move later.
2962 static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
2963 TCGRegSet allocated_regs,
2964 TCGRegSet preferred_regs, bool rev)
2966 int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
2967 TCGRegSet reg_ct[2];
2968 const int *order;
2970 reg_ct[1] = required_regs & ~allocated_regs;
2971 tcg_debug_assert(reg_ct[1] != 0);
2972 reg_ct[0] = reg_ct[1] & preferred_regs;
2974 /* Skip the preferred_regs option if it cannot be satisfied,
2975 or if the preference made no difference. */
2976 f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
2978 order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
2980 /* Try free registers, preferences first. */
2981 for (j = f; j < 2; j++) {
2982 TCGRegSet set = reg_ct[j];
2984 if (tcg_regset_single(set)) {
2985 /* One register in the set. */
2986 TCGReg reg = tcg_regset_first(set);
2987 if (s->reg_to_temp[reg] == NULL) {
2988 return reg;
2990 } else {
2991 for (i = 0; i < n; i++) {
2992 TCGReg reg = order[i];
2993 if (s->reg_to_temp[reg] == NULL &&
2994 tcg_regset_test_reg(set, reg)) {
2995 return reg;
3001 /* We must spill something. */
3002 for (j = f; j < 2; j++) {
3003 TCGRegSet set = reg_ct[j];
3005 if (tcg_regset_single(set)) {
3006 /* One register in the set. */
3007 TCGReg reg = tcg_regset_first(set);
3008 tcg_reg_free(s, reg, allocated_regs);
3009 return reg;
3010 } else {
3011 for (i = 0; i < n; i++) {
3012 TCGReg reg = order[i];
3013 if (tcg_regset_test_reg(set, reg)) {
3014 tcg_reg_free(s, reg, allocated_regs);
3015 return reg;
3021 tcg_abort();
3024 /* Make sure the temporary is in a register. If needed, allocate the register
3025 from DESIRED while avoiding ALLOCATED. */
3026 static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
3027 TCGRegSet allocated_regs, TCGRegSet preferred_regs)
3029 TCGReg reg;
3031 switch (ts->val_type) {
3032 case TEMP_VAL_REG:
3033 return;
3034 case TEMP_VAL_CONST:
3035 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
3036 preferred_regs, ts->indirect_base);
3037 tcg_out_movi(s, ts->type, reg, ts->val);
3038 ts->mem_coherent = 0;
3039 break;
3040 case TEMP_VAL_MEM:
3041 reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
3042 preferred_regs, ts->indirect_base);
3043 tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
3044 ts->mem_coherent = 1;
3045 break;
3046 case TEMP_VAL_DEAD:
3047 default:
3048 tcg_abort();
3050 ts->reg = reg;
3051 ts->val_type = TEMP_VAL_REG;
3052 s->reg_to_temp[reg] = ts;
3055 /* Save a temporary to memory. 'allocated_regs' is used in case a
3056 temporary registers needs to be allocated to store a constant. */
3057 static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
3059 /* The liveness analysis already ensures that globals are back
3060 in memory. Keep an tcg_debug_assert for safety. */
3061 tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg);
3064 /* save globals to their canonical location and assume they can be
3065 modified be the following code. 'allocated_regs' is used in case a
3066 temporary registers needs to be allocated to store a constant. */
3067 static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
3069 int i, n;
3071 for (i = 0, n = s->nb_globals; i < n; i++) {
3072 temp_save(s, &s->temps[i], allocated_regs);
3076 /* sync globals to their canonical location and assume they can be
3077 read by the following code. 'allocated_regs' is used in case a
3078 temporary registers needs to be allocated to store a constant. */
3079 static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
3081 int i, n;
3083 for (i = 0, n = s->nb_globals; i < n; i++) {
3084 TCGTemp *ts = &s->temps[i];
3085 tcg_debug_assert(ts->val_type != TEMP_VAL_REG
3086 || ts->fixed_reg
3087 || ts->mem_coherent);
3091 /* at the end of a basic block, we assume all temporaries are dead and
3092 all globals are stored at their canonical location. */
3093 static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
3095 int i;
3097 for (i = s->nb_globals; i < s->nb_temps; i++) {
3098 TCGTemp *ts = &s->temps[i];
3099 if (ts->temp_local) {
3100 temp_save(s, ts, allocated_regs);
3101 } else {
3102 /* The liveness analysis already ensures that temps are dead.
3103 Keep an tcg_debug_assert for safety. */
3104 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
3108 save_globals(s, allocated_regs);
3111 static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
3112 tcg_target_ulong val, TCGLifeData arg_life,
3113 TCGRegSet preferred_regs)
3115 if (ots->fixed_reg) {
3116 /* For fixed registers, we do not do any constant propagation. */
3117 tcg_out_movi(s, ots->type, ots->reg, val);
3118 return;
3121 /* The movi is not explicitly generated here. */
3122 if (ots->val_type == TEMP_VAL_REG) {
3123 s->reg_to_temp[ots->reg] = NULL;
3125 ots->val_type = TEMP_VAL_CONST;
3126 ots->val = val;
3127 ots->mem_coherent = 0;
3128 if (NEED_SYNC_ARG(0)) {
3129 temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
3130 } else if (IS_DEAD_ARG(0)) {
3131 temp_dead(s, ots);
3135 static void tcg_reg_alloc_movi(TCGContext *s, const TCGOp *op)
3137 TCGTemp *ots = arg_temp(op->args[0]);
3138 tcg_target_ulong val = op->args[1];
3140 tcg_reg_alloc_do_movi(s, ots, val, op->life, op->output_pref[0]);
3143 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
3145 const TCGLifeData arg_life = op->life;
3146 TCGRegSet allocated_regs, preferred_regs;
3147 TCGTemp *ts, *ots;
3148 TCGType otype, itype;
3150 allocated_regs = s->reserved_regs;
3151 preferred_regs = op->output_pref[0];
3152 ots = arg_temp(op->args[0]);
3153 ts = arg_temp(op->args[1]);
3155 /* Note that otype != itype for no-op truncation. */
3156 otype = ots->type;
3157 itype = ts->type;
3159 if (ts->val_type == TEMP_VAL_CONST) {
3160 /* propagate constant or generate sti */
3161 tcg_target_ulong val = ts->val;
3162 if (IS_DEAD_ARG(1)) {
3163 temp_dead(s, ts);
3165 tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
3166 return;
3169 /* If the source value is in memory we're going to be forced
3170 to have it in a register in order to perform the copy. Copy
3171 the SOURCE value into its own register first, that way we
3172 don't have to reload SOURCE the next time it is used. */
3173 if (ts->val_type == TEMP_VAL_MEM) {
3174 temp_load(s, ts, tcg_target_available_regs[itype],
3175 allocated_regs, preferred_regs);
3178 tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
3179 if (IS_DEAD_ARG(0) && !ots->fixed_reg) {
3180 /* mov to a non-saved dead register makes no sense (even with
3181 liveness analysis disabled). */
3182 tcg_debug_assert(NEED_SYNC_ARG(0));
3183 if (!ots->mem_allocated) {
3184 temp_allocate_frame(s, ots);
3186 tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset);
3187 if (IS_DEAD_ARG(1)) {
3188 temp_dead(s, ts);
3190 temp_dead(s, ots);
3191 } else {
3192 if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
3193 /* the mov can be suppressed */
3194 if (ots->val_type == TEMP_VAL_REG) {
3195 s->reg_to_temp[ots->reg] = NULL;
3197 ots->reg = ts->reg;
3198 temp_dead(s, ts);
3199 } else {
3200 if (ots->val_type != TEMP_VAL_REG) {
3201 /* When allocating a new register, make sure to not spill the
3202 input one. */
3203 tcg_regset_set_reg(allocated_regs, ts->reg);
3204 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
3205 allocated_regs, preferred_regs,
3206 ots->indirect_base);
3208 tcg_out_mov(s, otype, ots->reg, ts->reg);
3210 ots->val_type = TEMP_VAL_REG;
3211 ots->mem_coherent = 0;
3212 s->reg_to_temp[ots->reg] = ots;
3213 if (NEED_SYNC_ARG(0)) {
3214 temp_sync(s, ots, allocated_regs, 0, 0);
3219 static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
3221 const TCGLifeData arg_life = op->life;
3222 const TCGOpDef * const def = &tcg_op_defs[op->opc];
3223 TCGRegSet i_allocated_regs;
3224 TCGRegSet o_allocated_regs;
3225 int i, k, nb_iargs, nb_oargs;
3226 TCGReg reg;
3227 TCGArg arg;
3228 const TCGArgConstraint *arg_ct;
3229 TCGTemp *ts;
3230 TCGArg new_args[TCG_MAX_OP_ARGS];
3231 int const_args[TCG_MAX_OP_ARGS];
3233 nb_oargs = def->nb_oargs;
3234 nb_iargs = def->nb_iargs;
3236 /* copy constants */
3237 memcpy(new_args + nb_oargs + nb_iargs,
3238 op->args + nb_oargs + nb_iargs,
3239 sizeof(TCGArg) * def->nb_cargs);
3241 i_allocated_regs = s->reserved_regs;
3242 o_allocated_regs = s->reserved_regs;
3244 /* satisfy input constraints */
3245 for (k = 0; k < nb_iargs; k++) {
3246 TCGRegSet i_preferred_regs, o_preferred_regs;
3248 i = def->sorted_args[nb_oargs + k];
3249 arg = op->args[i];
3250 arg_ct = &def->args_ct[i];
3251 ts = arg_temp(arg);
3253 if (ts->val_type == TEMP_VAL_CONST
3254 && tcg_target_const_match(ts->val, ts->type, arg_ct)) {
3255 /* constant is OK for instruction */
3256 const_args[i] = 1;
3257 new_args[i] = ts->val;
3258 continue;
3261 i_preferred_regs = o_preferred_regs = 0;
3262 if (arg_ct->ct & TCG_CT_IALIAS) {
3263 o_preferred_regs = op->output_pref[arg_ct->alias_index];
3264 if (ts->fixed_reg) {
3265 /* if fixed register, we must allocate a new register
3266 if the alias is not the same register */
3267 if (arg != op->args[arg_ct->alias_index]) {
3268 goto allocate_in_reg;
3270 } else {
3271 /* if the input is aliased to an output and if it is
3272 not dead after the instruction, we must allocate
3273 a new register and move it */
3274 if (!IS_DEAD_ARG(i)) {
3275 goto allocate_in_reg;
3278 /* check if the current register has already been allocated
3279 for another input aliased to an output */
3280 if (ts->val_type == TEMP_VAL_REG) {
3281 int k2, i2;
3282 reg = ts->reg;
3283 for (k2 = 0 ; k2 < k ; k2++) {
3284 i2 = def->sorted_args[nb_oargs + k2];
3285 if ((def->args_ct[i2].ct & TCG_CT_IALIAS) &&
3286 reg == new_args[i2]) {
3287 goto allocate_in_reg;
3291 i_preferred_regs = o_preferred_regs;
3295 temp_load(s, ts, arg_ct->u.regs, i_allocated_regs, i_preferred_regs);
3296 reg = ts->reg;
3298 if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
3299 /* nothing to do : the constraint is satisfied */
3300 } else {
3301 allocate_in_reg:
3302 /* allocate a new register matching the constraint
3303 and move the temporary register into it */
3304 temp_load(s, ts, tcg_target_available_regs[ts->type],
3305 i_allocated_regs, 0);
3306 reg = tcg_reg_alloc(s, arg_ct->u.regs, i_allocated_regs,
3307 o_preferred_regs, ts->indirect_base);
3308 tcg_out_mov(s, ts->type, reg, ts->reg);
3310 new_args[i] = reg;
3311 const_args[i] = 0;
3312 tcg_regset_set_reg(i_allocated_regs, reg);
3315 /* mark dead temporaries and free the associated registers */
3316 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3317 if (IS_DEAD_ARG(i)) {
3318 temp_dead(s, arg_temp(op->args[i]));
3322 if (def->flags & TCG_OPF_BB_END) {
3323 tcg_reg_alloc_bb_end(s, i_allocated_regs);
3324 } else {
3325 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3326 /* XXX: permit generic clobber register list ? */
3327 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
3328 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
3329 tcg_reg_free(s, i, i_allocated_regs);
3333 if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3334 /* sync globals if the op has side effects and might trigger
3335 an exception. */
3336 sync_globals(s, i_allocated_regs);
3339 /* satisfy the output constraints */
3340 for(k = 0; k < nb_oargs; k++) {
3341 i = def->sorted_args[k];
3342 arg = op->args[i];
3343 arg_ct = &def->args_ct[i];
3344 ts = arg_temp(arg);
3345 if ((arg_ct->ct & TCG_CT_ALIAS)
3346 && !const_args[arg_ct->alias_index]) {
3347 reg = new_args[arg_ct->alias_index];
3348 } else if (arg_ct->ct & TCG_CT_NEWREG) {
3349 reg = tcg_reg_alloc(s, arg_ct->u.regs,
3350 i_allocated_regs | o_allocated_regs,
3351 op->output_pref[k], ts->indirect_base);
3352 } else {
3353 /* if fixed register, we try to use it */
3354 reg = ts->reg;
3355 if (ts->fixed_reg &&
3356 tcg_regset_test_reg(arg_ct->u.regs, reg)) {
3357 goto oarg_end;
3359 reg = tcg_reg_alloc(s, arg_ct->u.regs, o_allocated_regs,
3360 op->output_pref[k], ts->indirect_base);
3362 tcg_regset_set_reg(o_allocated_regs, reg);
3363 /* if a fixed register is used, then a move will be done afterwards */
3364 if (!ts->fixed_reg) {
3365 if (ts->val_type == TEMP_VAL_REG) {
3366 s->reg_to_temp[ts->reg] = NULL;
3368 ts->val_type = TEMP_VAL_REG;
3369 ts->reg = reg;
3370 /* temp value is modified, so the value kept in memory is
3371 potentially not the same */
3372 ts->mem_coherent = 0;
3373 s->reg_to_temp[reg] = ts;
3375 oarg_end:
3376 new_args[i] = reg;
3380 /* emit instruction */
3381 if (def->flags & TCG_OPF_VECTOR) {
3382 tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
3383 new_args, const_args);
3384 } else {
3385 tcg_out_op(s, op->opc, new_args, const_args);
3388 /* move the outputs in the correct register if needed */
3389 for(i = 0; i < nb_oargs; i++) {
3390 ts = arg_temp(op->args[i]);
3391 reg = new_args[i];
3392 if (ts->fixed_reg && ts->reg != reg) {
3393 tcg_out_mov(s, ts->type, ts->reg, reg);
3395 if (NEED_SYNC_ARG(i)) {
3396 temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
3397 } else if (IS_DEAD_ARG(i)) {
3398 temp_dead(s, ts);
3403 #ifdef TCG_TARGET_STACK_GROWSUP
3404 #define STACK_DIR(x) (-(x))
3405 #else
3406 #define STACK_DIR(x) (x)
3407 #endif
3409 static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
3411 const int nb_oargs = TCGOP_CALLO(op);
3412 const int nb_iargs = TCGOP_CALLI(op);
3413 const TCGLifeData arg_life = op->life;
3414 int flags, nb_regs, i;
3415 TCGReg reg;
3416 TCGArg arg;
3417 TCGTemp *ts;
3418 intptr_t stack_offset;
3419 size_t call_stack_size;
3420 tcg_insn_unit *func_addr;
3421 int allocate_args;
3422 TCGRegSet allocated_regs;
3424 func_addr = (tcg_insn_unit *)(intptr_t)op->args[nb_oargs + nb_iargs];
3425 flags = op->args[nb_oargs + nb_iargs + 1];
3427 nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
3428 if (nb_regs > nb_iargs) {
3429 nb_regs = nb_iargs;
3432 /* assign stack slots first */
3433 call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
3434 call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
3435 ~(TCG_TARGET_STACK_ALIGN - 1);
3436 allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
3437 if (allocate_args) {
3438 /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
3439 preallocate call stack */
3440 tcg_abort();
3443 stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
3444 for (i = nb_regs; i < nb_iargs; i++) {
3445 arg = op->args[nb_oargs + i];
3446 #ifdef TCG_TARGET_STACK_GROWSUP
3447 stack_offset -= sizeof(tcg_target_long);
3448 #endif
3449 if (arg != TCG_CALL_DUMMY_ARG) {
3450 ts = arg_temp(arg);
3451 temp_load(s, ts, tcg_target_available_regs[ts->type],
3452 s->reserved_regs, 0);
3453 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
3455 #ifndef TCG_TARGET_STACK_GROWSUP
3456 stack_offset += sizeof(tcg_target_long);
3457 #endif
3460 /* assign input registers */
3461 allocated_regs = s->reserved_regs;
3462 for (i = 0; i < nb_regs; i++) {
3463 arg = op->args[nb_oargs + i];
3464 if (arg != TCG_CALL_DUMMY_ARG) {
3465 ts = arg_temp(arg);
3466 reg = tcg_target_call_iarg_regs[i];
3467 tcg_reg_free(s, reg, allocated_regs);
3469 if (ts->val_type == TEMP_VAL_REG) {
3470 if (ts->reg != reg) {
3471 tcg_out_mov(s, ts->type, reg, ts->reg);
3473 } else {
3474 TCGRegSet arg_set = 0;
3476 tcg_regset_set_reg(arg_set, reg);
3477 temp_load(s, ts, arg_set, allocated_regs, 0);
3480 tcg_regset_set_reg(allocated_regs, reg);
3484 /* mark dead temporaries and free the associated registers */
3485 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
3486 if (IS_DEAD_ARG(i)) {
3487 temp_dead(s, arg_temp(op->args[i]));
3491 /* clobber call registers */
3492 for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
3493 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
3494 tcg_reg_free(s, i, allocated_regs);
3498 /* Save globals if they might be written by the helper, sync them if
3499 they might be read. */
3500 if (flags & TCG_CALL_NO_READ_GLOBALS) {
3501 /* Nothing to do */
3502 } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
3503 sync_globals(s, allocated_regs);
3504 } else {
3505 save_globals(s, allocated_regs);
3508 tcg_out_call(s, func_addr);
3510 /* assign output registers and emit moves if needed */
3511 for(i = 0; i < nb_oargs; i++) {
3512 arg = op->args[i];
3513 ts = arg_temp(arg);
3514 reg = tcg_target_call_oarg_regs[i];
3515 tcg_debug_assert(s->reg_to_temp[reg] == NULL);
3517 if (ts->fixed_reg) {
3518 if (ts->reg != reg) {
3519 tcg_out_mov(s, ts->type, ts->reg, reg);
3521 } else {
3522 if (ts->val_type == TEMP_VAL_REG) {
3523 s->reg_to_temp[ts->reg] = NULL;
3525 ts->val_type = TEMP_VAL_REG;
3526 ts->reg = reg;
3527 ts->mem_coherent = 0;
3528 s->reg_to_temp[reg] = ts;
3529 if (NEED_SYNC_ARG(i)) {
3530 temp_sync(s, ts, allocated_regs, 0, IS_DEAD_ARG(i));
3531 } else if (IS_DEAD_ARG(i)) {
3532 temp_dead(s, ts);
3538 #ifdef CONFIG_PROFILER
3540 /* avoid copy/paste errors */
3541 #define PROF_ADD(to, from, field) \
3542 do { \
3543 (to)->field += atomic_read(&((from)->field)); \
3544 } while (0)
3546 #define PROF_MAX(to, from, field) \
3547 do { \
3548 typeof((from)->field) val__ = atomic_read(&((from)->field)); \
3549 if (val__ > (to)->field) { \
3550 (to)->field = val__; \
3552 } while (0)
3554 /* Pass in a zero'ed @prof */
3555 static inline
3556 void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
3558 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
3559 unsigned int i;
3561 for (i = 0; i < n_ctxs; i++) {
3562 TCGContext *s = atomic_read(&tcg_ctxs[i]);
3563 const TCGProfile *orig = &s->prof;
3565 if (counters) {
3566 PROF_ADD(prof, orig, cpu_exec_time);
3567 PROF_ADD(prof, orig, tb_count1);
3568 PROF_ADD(prof, orig, tb_count);
3569 PROF_ADD(prof, orig, op_count);
3570 PROF_MAX(prof, orig, op_count_max);
3571 PROF_ADD(prof, orig, temp_count);
3572 PROF_MAX(prof, orig, temp_count_max);
3573 PROF_ADD(prof, orig, del_op_count);
3574 PROF_ADD(prof, orig, code_in_len);
3575 PROF_ADD(prof, orig, code_out_len);
3576 PROF_ADD(prof, orig, search_out_len);
3577 PROF_ADD(prof, orig, interm_time);
3578 PROF_ADD(prof, orig, code_time);
3579 PROF_ADD(prof, orig, la_time);
3580 PROF_ADD(prof, orig, opt_time);
3581 PROF_ADD(prof, orig, restore_count);
3582 PROF_ADD(prof, orig, restore_time);
3584 if (table) {
3585 int i;
3587 for (i = 0; i < NB_OPS; i++) {
3588 PROF_ADD(prof, orig, table_op_count[i]);
3594 #undef PROF_ADD
3595 #undef PROF_MAX
3597 static void tcg_profile_snapshot_counters(TCGProfile *prof)
3599 tcg_profile_snapshot(prof, true, false);
3602 static void tcg_profile_snapshot_table(TCGProfile *prof)
3604 tcg_profile_snapshot(prof, false, true);
3607 void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
3609 TCGProfile prof = {};
3610 int i;
3612 tcg_profile_snapshot_table(&prof);
3613 for (i = 0; i < NB_OPS; i++) {
3614 cpu_fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name,
3615 prof.table_op_count[i]);
3619 int64_t tcg_cpu_exec_time(void)
3621 unsigned int n_ctxs = atomic_read(&n_tcg_ctxs);
3622 unsigned int i;
3623 int64_t ret = 0;
3625 for (i = 0; i < n_ctxs; i++) {
3626 const TCGContext *s = atomic_read(&tcg_ctxs[i]);
3627 const TCGProfile *prof = &s->prof;
3629 ret += atomic_read(&prof->cpu_exec_time);
3631 return ret;
3633 #else
3634 void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
3636 cpu_fprintf(f, "[TCG profiler not compiled]\n");
3639 int64_t tcg_cpu_exec_time(void)
3641 error_report("%s: TCG profiler not compiled", __func__);
3642 exit(EXIT_FAILURE);
3644 #endif
3647 int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
3649 #ifdef CONFIG_PROFILER
3650 TCGProfile *prof = &s->prof;
3651 #endif
3652 int i, num_insns;
3653 TCGOp *op;
3655 #ifdef CONFIG_PROFILER
3657 int n = 0;
3659 QTAILQ_FOREACH(op, &s->ops, link) {
3660 n++;
3662 atomic_set(&prof->op_count, prof->op_count + n);
3663 if (n > prof->op_count_max) {
3664 atomic_set(&prof->op_count_max, n);
3667 n = s->nb_temps;
3668 atomic_set(&prof->temp_count, prof->temp_count + n);
3669 if (n > prof->temp_count_max) {
3670 atomic_set(&prof->temp_count_max, n);
3673 #endif
3675 #ifdef DEBUG_DISAS
3676 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
3677 && qemu_log_in_addr_range(tb->pc))) {
3678 qemu_log_lock();
3679 qemu_log("OP:\n");
3680 tcg_dump_ops(s, false);
3681 qemu_log("\n");
3682 qemu_log_unlock();
3684 #endif
3686 #ifdef CONFIG_PROFILER
3687 atomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
3688 #endif
3690 #ifdef USE_TCG_OPTIMIZATIONS
3691 tcg_optimize(s);
3692 #endif
3694 #ifdef CONFIG_PROFILER
3695 atomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
3696 atomic_set(&prof->la_time, prof->la_time - profile_getclock());
3697 #endif
3699 reachable_code_pass(s);
3700 liveness_pass_1(s);
3702 if (s->nb_indirects > 0) {
3703 #ifdef DEBUG_DISAS
3704 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
3705 && qemu_log_in_addr_range(tb->pc))) {
3706 qemu_log_lock();
3707 qemu_log("OP before indirect lowering:\n");
3708 tcg_dump_ops(s, false);
3709 qemu_log("\n");
3710 qemu_log_unlock();
3712 #endif
3713 /* Replace indirect temps with direct temps. */
3714 if (liveness_pass_2(s)) {
3715 /* If changes were made, re-run liveness. */
3716 liveness_pass_1(s);
3720 #ifdef CONFIG_PROFILER
3721 atomic_set(&prof->la_time, prof->la_time + profile_getclock());
3722 #endif
3724 #ifdef DEBUG_DISAS
3725 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
3726 && qemu_log_in_addr_range(tb->pc))) {
3727 qemu_log_lock();
3728 qemu_log("OP after optimization and liveness analysis:\n");
3729 tcg_dump_ops(s, true);
3730 qemu_log("\n");
3731 qemu_log_unlock();
3733 #endif
3735 tcg_reg_alloc_start(s);
3737 s->code_buf = tb->tc.ptr;
3738 s->code_ptr = tb->tc.ptr;
3740 #ifdef TCG_TARGET_NEED_LDST_LABELS
3741 QSIMPLEQ_INIT(&s->ldst_labels);
3742 #endif
3743 #ifdef TCG_TARGET_NEED_POOL_LABELS
3744 s->pool_labels = NULL;
3745 #endif
3747 num_insns = -1;
3748 QTAILQ_FOREACH(op, &s->ops, link) {
3749 TCGOpcode opc = op->opc;
3751 #ifdef CONFIG_PROFILER
3752 atomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
3753 #endif
3755 switch (opc) {
3756 case INDEX_op_mov_i32:
3757 case INDEX_op_mov_i64:
3758 case INDEX_op_mov_vec:
3759 tcg_reg_alloc_mov(s, op);
3760 break;
3761 case INDEX_op_movi_i32:
3762 case INDEX_op_movi_i64:
3763 case INDEX_op_dupi_vec:
3764 tcg_reg_alloc_movi(s, op);
3765 break;
3766 case INDEX_op_insn_start:
3767 if (num_insns >= 0) {
3768 size_t off = tcg_current_code_size(s);
3769 s->gen_insn_end_off[num_insns] = off;
3770 /* Assert that we do not overflow our stored offset. */
3771 assert(s->gen_insn_end_off[num_insns] == off);
3773 num_insns++;
3774 for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
3775 target_ulong a;
3776 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
3777 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
3778 #else
3779 a = op->args[i];
3780 #endif
3781 s->gen_insn_data[num_insns][i] = a;
3783 break;
3784 case INDEX_op_discard:
3785 temp_dead(s, arg_temp(op->args[0]));
3786 break;
3787 case INDEX_op_set_label:
3788 tcg_reg_alloc_bb_end(s, s->reserved_regs);
3789 tcg_out_label(s, arg_label(op->args[0]), s->code_ptr);
3790 break;
3791 case INDEX_op_call:
3792 tcg_reg_alloc_call(s, op);
3793 break;
3794 default:
3795 /* Sanity check that we've not introduced any unhandled opcodes. */
3796 tcg_debug_assert(tcg_op_supported(opc));
3797 /* Note: in order to speed up the code, it would be much
3798 faster to have specialized register allocator functions for
3799 some common argument patterns */
3800 tcg_reg_alloc_op(s, op);
3801 break;
3803 #ifdef CONFIG_DEBUG_TCG
3804 check_regs(s);
3805 #endif
3806 /* Test for (pending) buffer overflow. The assumption is that any
3807 one operation beginning below the high water mark cannot overrun
3808 the buffer completely. Thus we can test for overflow after
3809 generating code without having to check during generation. */
3810 if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
3811 return -1;
3814 tcg_debug_assert(num_insns >= 0);
3815 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
3817 /* Generate TB finalization at the end of block */
3818 #ifdef TCG_TARGET_NEED_LDST_LABELS
3819 if (!tcg_out_ldst_finalize(s)) {
3820 return -1;
3822 #endif
3823 #ifdef TCG_TARGET_NEED_POOL_LABELS
3824 if (!tcg_out_pool_finalize(s)) {
3825 return -1;
3827 #endif
3829 /* flush instruction cache */
3830 flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
3832 return tcg_current_code_size(s);
3835 #ifdef CONFIG_PROFILER
3836 void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
3838 TCGProfile prof = {};
3839 const TCGProfile *s;
3840 int64_t tb_count;
3841 int64_t tb_div_count;
3842 int64_t tot;
3844 tcg_profile_snapshot_counters(&prof);
3845 s = &prof;
3846 tb_count = s->tb_count;
3847 tb_div_count = tb_count ? tb_count : 1;
3848 tot = s->interm_time + s->code_time;
3850 cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
3851 tot, tot / 2.4e9);
3852 cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
3853 tb_count, s->tb_count1 - tb_count,
3854 (double)(s->tb_count1 - s->tb_count)
3855 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
3856 cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n",
3857 (double)s->op_count / tb_div_count, s->op_count_max);
3858 cpu_fprintf(f, "deleted ops/TB %0.2f\n",
3859 (double)s->del_op_count / tb_div_count);
3860 cpu_fprintf(f, "avg temps/TB %0.2f max=%d\n",
3861 (double)s->temp_count / tb_div_count, s->temp_count_max);
3862 cpu_fprintf(f, "avg host code/TB %0.1f\n",
3863 (double)s->code_out_len / tb_div_count);
3864 cpu_fprintf(f, "avg search data/TB %0.1f\n",
3865 (double)s->search_out_len / tb_div_count);
3867 cpu_fprintf(f, "cycles/op %0.1f\n",
3868 s->op_count ? (double)tot / s->op_count : 0);
3869 cpu_fprintf(f, "cycles/in byte %0.1f\n",
3870 s->code_in_len ? (double)tot / s->code_in_len : 0);
3871 cpu_fprintf(f, "cycles/out byte %0.1f\n",
3872 s->code_out_len ? (double)tot / s->code_out_len : 0);
3873 cpu_fprintf(f, "cycles/search byte %0.1f\n",
3874 s->search_out_len ? (double)tot / s->search_out_len : 0);
3875 if (tot == 0) {
3876 tot = 1;
3878 cpu_fprintf(f, " gen_interm time %0.1f%%\n",
3879 (double)s->interm_time / tot * 100.0);
3880 cpu_fprintf(f, " gen_code time %0.1f%%\n",
3881 (double)s->code_time / tot * 100.0);
3882 cpu_fprintf(f, "optim./code time %0.1f%%\n",
3883 (double)s->opt_time / (s->code_time ? s->code_time : 1)
3884 * 100.0);
3885 cpu_fprintf(f, "liveness/code time %0.1f%%\n",
3886 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
3887 cpu_fprintf(f, "cpu_restore count %" PRId64 "\n",
3888 s->restore_count);
3889 cpu_fprintf(f, " avg cycles %0.1f\n",
3890 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
3892 #else
3893 void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
3895 cpu_fprintf(f, "[TCG profiler not compiled]\n");
3897 #endif
3899 #ifdef ELF_HOST_MACHINE
3900 /* In order to use this feature, the backend needs to do three things:
3902 (1) Define ELF_HOST_MACHINE to indicate both what value to
3903 put into the ELF image and to indicate support for the feature.
3905 (2) Define tcg_register_jit. This should create a buffer containing
3906 the contents of a .debug_frame section that describes the post-
3907 prologue unwind info for the tcg machine.
3909 (3) Call tcg_register_jit_int, with the constructed .debug_frame.
3912 /* Begin GDB interface. THE FOLLOWING MUST MATCH GDB DOCS. */
3913 typedef enum {
3914 JIT_NOACTION = 0,
3915 JIT_REGISTER_FN,
3916 JIT_UNREGISTER_FN
3917 } jit_actions_t;
3919 struct jit_code_entry {
3920 struct jit_code_entry *next_entry;
3921 struct jit_code_entry *prev_entry;
3922 const void *symfile_addr;
3923 uint64_t symfile_size;
3926 struct jit_descriptor {
3927 uint32_t version;
3928 uint32_t action_flag;
3929 struct jit_code_entry *relevant_entry;
3930 struct jit_code_entry *first_entry;
3933 void __jit_debug_register_code(void) __attribute__((noinline));
3934 void __jit_debug_register_code(void)
3936 asm("");
3939 /* Must statically initialize the version, because GDB may check
3940 the version before we can set it. */
3941 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
3943 /* End GDB interface. */
3945 static int find_string(const char *strtab, const char *str)
3947 const char *p = strtab + 1;
3949 while (1) {
3950 if (strcmp(p, str) == 0) {
3951 return p - strtab;
3953 p += strlen(p) + 1;
3957 static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
3958 const void *debug_frame,
3959 size_t debug_frame_size)
3961 struct __attribute__((packed)) DebugInfo {
3962 uint32_t len;
3963 uint16_t version;
3964 uint32_t abbrev;
3965 uint8_t ptr_size;
3966 uint8_t cu_die;
3967 uint16_t cu_lang;
3968 uintptr_t cu_low_pc;
3969 uintptr_t cu_high_pc;
3970 uint8_t fn_die;
3971 char fn_name[16];
3972 uintptr_t fn_low_pc;
3973 uintptr_t fn_high_pc;
3974 uint8_t cu_eoc;
3977 struct ElfImage {
3978 ElfW(Ehdr) ehdr;
3979 ElfW(Phdr) phdr;
3980 ElfW(Shdr) shdr[7];
3981 ElfW(Sym) sym[2];
3982 struct DebugInfo di;
3983 uint8_t da[24];
3984 char str[80];
3987 struct ElfImage *img;
3989 static const struct ElfImage img_template = {
3990 .ehdr = {
3991 .e_ident[EI_MAG0] = ELFMAG0,
3992 .e_ident[EI_MAG1] = ELFMAG1,
3993 .e_ident[EI_MAG2] = ELFMAG2,
3994 .e_ident[EI_MAG3] = ELFMAG3,
3995 .e_ident[EI_CLASS] = ELF_CLASS,
3996 .e_ident[EI_DATA] = ELF_DATA,
3997 .e_ident[EI_VERSION] = EV_CURRENT,
3998 .e_type = ET_EXEC,
3999 .e_machine = ELF_HOST_MACHINE,
4000 .e_version = EV_CURRENT,
4001 .e_phoff = offsetof(struct ElfImage, phdr),
4002 .e_shoff = offsetof(struct ElfImage, shdr),
4003 .e_ehsize = sizeof(ElfW(Shdr)),
4004 .e_phentsize = sizeof(ElfW(Phdr)),
4005 .e_phnum = 1,
4006 .e_shentsize = sizeof(ElfW(Shdr)),
4007 .e_shnum = ARRAY_SIZE(img->shdr),
4008 .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
4009 #ifdef ELF_HOST_FLAGS
4010 .e_flags = ELF_HOST_FLAGS,
4011 #endif
4012 #ifdef ELF_OSABI
4013 .e_ident[EI_OSABI] = ELF_OSABI,
4014 #endif
4016 .phdr = {
4017 .p_type = PT_LOAD,
4018 .p_flags = PF_X,
4020 .shdr = {
4021 [0] = { .sh_type = SHT_NULL },
4022 /* Trick: The contents of code_gen_buffer are not present in
4023 this fake ELF file; that got allocated elsewhere. Therefore
4024 we mark .text as SHT_NOBITS (similar to .bss) so that readers
4025 will not look for contents. We can record any address. */
4026 [1] = { /* .text */
4027 .sh_type = SHT_NOBITS,
4028 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
4030 [2] = { /* .debug_info */
4031 .sh_type = SHT_PROGBITS,
4032 .sh_offset = offsetof(struct ElfImage, di),
4033 .sh_size = sizeof(struct DebugInfo),
4035 [3] = { /* .debug_abbrev */
4036 .sh_type = SHT_PROGBITS,
4037 .sh_offset = offsetof(struct ElfImage, da),
4038 .sh_size = sizeof(img->da),
4040 [4] = { /* .debug_frame */
4041 .sh_type = SHT_PROGBITS,
4042 .sh_offset = sizeof(struct ElfImage),
4044 [5] = { /* .symtab */
4045 .sh_type = SHT_SYMTAB,
4046 .sh_offset = offsetof(struct ElfImage, sym),
4047 .sh_size = sizeof(img->sym),
4048 .sh_info = 1,
4049 .sh_link = ARRAY_SIZE(img->shdr) - 1,
4050 .sh_entsize = sizeof(ElfW(Sym)),
4052 [6] = { /* .strtab */
4053 .sh_type = SHT_STRTAB,
4054 .sh_offset = offsetof(struct ElfImage, str),
4055 .sh_size = sizeof(img->str),
4058 .sym = {
4059 [1] = { /* code_gen_buffer */
4060 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
4061 .st_shndx = 1,
4064 .di = {
4065 .len = sizeof(struct DebugInfo) - 4,
4066 .version = 2,
4067 .ptr_size = sizeof(void *),
4068 .cu_die = 1,
4069 .cu_lang = 0x8001, /* DW_LANG_Mips_Assembler */
4070 .fn_die = 2,
4071 .fn_name = "code_gen_buffer"
4073 .da = {
4074 1, /* abbrev number (the cu) */
4075 0x11, 1, /* DW_TAG_compile_unit, has children */
4076 0x13, 0x5, /* DW_AT_language, DW_FORM_data2 */
4077 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4078 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4079 0, 0, /* end of abbrev */
4080 2, /* abbrev number (the fn) */
4081 0x2e, 0, /* DW_TAG_subprogram, no children */
4082 0x3, 0x8, /* DW_AT_name, DW_FORM_string */
4083 0x11, 0x1, /* DW_AT_low_pc, DW_FORM_addr */
4084 0x12, 0x1, /* DW_AT_high_pc, DW_FORM_addr */
4085 0, 0, /* end of abbrev */
4086 0 /* no more abbrev */
4088 .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
4089 ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
4092 /* We only need a single jit entry; statically allocate it. */
4093 static struct jit_code_entry one_entry;
4095 uintptr_t buf = (uintptr_t)buf_ptr;
4096 size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
4097 DebugFrameHeader *dfh;
4099 img = g_malloc(img_size);
4100 *img = img_template;
4102 img->phdr.p_vaddr = buf;
4103 img->phdr.p_paddr = buf;
4104 img->phdr.p_memsz = buf_size;
4106 img->shdr[1].sh_name = find_string(img->str, ".text");
4107 img->shdr[1].sh_addr = buf;
4108 img->shdr[1].sh_size = buf_size;
4110 img->shdr[2].sh_name = find_string(img->str, ".debug_info");
4111 img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
4113 img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
4114 img->shdr[4].sh_size = debug_frame_size;
4116 img->shdr[5].sh_name = find_string(img->str, ".symtab");
4117 img->shdr[6].sh_name = find_string(img->str, ".strtab");
4119 img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
4120 img->sym[1].st_value = buf;
4121 img->sym[1].st_size = buf_size;
4123 img->di.cu_low_pc = buf;
4124 img->di.cu_high_pc = buf + buf_size;
4125 img->di.fn_low_pc = buf;
4126 img->di.fn_high_pc = buf + buf_size;
4128 dfh = (DebugFrameHeader *)(img + 1);
4129 memcpy(dfh, debug_frame, debug_frame_size);
4130 dfh->fde.func_start = buf;
4131 dfh->fde.func_len = buf_size;
4133 #ifdef DEBUG_JIT
4134 /* Enable this block to be able to debug the ELF image file creation.
4135 One can use readelf, objdump, or other inspection utilities. */
4137 FILE *f = fopen("/tmp/qemu.jit", "w+b");
4138 if (f) {
4139 if (fwrite(img, img_size, 1, f) != img_size) {
4140 /* Avoid stupid unused return value warning for fwrite. */
4142 fclose(f);
4145 #endif
4147 one_entry.symfile_addr = img;
4148 one_entry.symfile_size = img_size;
4150 __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
4151 __jit_debug_descriptor.relevant_entry = &one_entry;
4152 __jit_debug_descriptor.first_entry = &one_entry;
4153 __jit_debug_register_code();
4155 #else
4156 /* No support for the feature. Provide the entry point expected by exec.c,
4157 and implement the internal function we declared earlier. */
4159 static void tcg_register_jit_int(void *buf, size_t size,
4160 const void *debug_frame,
4161 size_t debug_frame_size)
4165 void tcg_register_jit(void *buf, size_t buf_size)
4168 #endif /* ELF_HOST_MACHINE */
4170 #if !TCG_TARGET_MAYBE_vec
4171 void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
4173 g_assert_not_reached();
4175 #endif