xref: /openbmc/qemu/tcg/tcg.c (revision 8d8fdbae)
1c896fe29Sbellard /*
2c896fe29Sbellard  * Tiny Code Generator for QEMU
3c896fe29Sbellard  *
4c896fe29Sbellard  * Copyright (c) 2008 Fabrice Bellard
5c896fe29Sbellard  *
6c896fe29Sbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
7c896fe29Sbellard  * of this software and associated documentation files (the "Software"), to deal
8c896fe29Sbellard  * in the Software without restriction, including without limitation the rights
9c896fe29Sbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10c896fe29Sbellard  * copies of the Software, and to permit persons to whom the Software is
11c896fe29Sbellard  * furnished to do so, subject to the following conditions:
12c896fe29Sbellard  *
13c896fe29Sbellard  * The above copyright notice and this permission notice shall be included in
14c896fe29Sbellard  * all copies or substantial portions of the Software.
15c896fe29Sbellard  *
16c896fe29Sbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17c896fe29Sbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18c896fe29Sbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19c896fe29Sbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20c896fe29Sbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21c896fe29Sbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22c896fe29Sbellard  * THE SOFTWARE.
23c896fe29Sbellard  */
24c896fe29Sbellard 
25c896fe29Sbellard /* define it to use liveness analysis (better code) */
26c896fe29Sbellard #define USE_LIVENESS_ANALYSIS
278f2e8c07SKirill Batuzov #define USE_TCG_OPTIMIZATIONS
28c896fe29Sbellard 
29757e725bSPeter Maydell #include "qemu/osdep.h"
30cca82982Saurel32 
31813da627SRichard Henderson /* Define to jump the ELF file used to communicate with GDB.  */
32813da627SRichard Henderson #undef DEBUG_JIT
33813da627SRichard Henderson 
34f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
351de7afc9SPaolo Bonzini #include "qemu/host-utils.h"
361de7afc9SPaolo Bonzini #include "qemu/timer.h"
37c896fe29Sbellard 
38c5d3c498SStefan Weil /* Note: the long term plan is to reduce the dependencies on the QEMU
39c896fe29Sbellard    CPU definitions. Currently they are used for qemu_ld/st
40c896fe29Sbellard    instructions */
41c896fe29Sbellard #define NO_CPU_IO_DEFS
42c896fe29Sbellard #include "cpu.h"
43c896fe29Sbellard 
44c896fe29Sbellard #include "tcg-op.h"
45813da627SRichard Henderson 
46edee2579SRichard Henderson #if UINTPTR_MAX == UINT32_MAX
47813da627SRichard Henderson # define ELF_CLASS  ELFCLASS32
48edee2579SRichard Henderson #else
49edee2579SRichard Henderson # define ELF_CLASS  ELFCLASS64
50813da627SRichard Henderson #endif
51813da627SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
52813da627SRichard Henderson # define ELF_DATA   ELFDATA2MSB
53813da627SRichard Henderson #else
54813da627SRichard Henderson # define ELF_DATA   ELFDATA2LSB
55813da627SRichard Henderson #endif
56813da627SRichard Henderson 
57c896fe29Sbellard #include "elf.h"
58508127e2SPaolo Bonzini #include "exec/log.h"
59c896fe29Sbellard 
60ce151109SPeter Maydell /* Forward declarations for functions declared in tcg-target.inc.c and
61ce151109SPeter Maydell    used here. */
62e4d58b41SRichard Henderson static void tcg_target_init(TCGContext *s);
63e4d58b41SRichard Henderson static void tcg_target_qemu_prologue(TCGContext *s);
641813e175SRichard Henderson static void patch_reloc(tcg_insn_unit *code_ptr, int type,
652ba7fae2SRichard Henderson                         intptr_t value, intptr_t addend);
66c896fe29Sbellard 
67497a22ebSRichard Henderson /* The CIE and FDE header definitions will be common to all hosts.  */
68497a22ebSRichard Henderson typedef struct {
69497a22ebSRichard Henderson     uint32_t len __attribute__((aligned((sizeof(void *)))));
70497a22ebSRichard Henderson     uint32_t id;
71497a22ebSRichard Henderson     uint8_t version;
72497a22ebSRichard Henderson     char augmentation[1];
73497a22ebSRichard Henderson     uint8_t code_align;
74497a22ebSRichard Henderson     uint8_t data_align;
75497a22ebSRichard Henderson     uint8_t return_column;
76497a22ebSRichard Henderson } DebugFrameCIE;
77497a22ebSRichard Henderson 
78497a22ebSRichard Henderson typedef struct QEMU_PACKED {
79497a22ebSRichard Henderson     uint32_t len __attribute__((aligned((sizeof(void *)))));
80497a22ebSRichard Henderson     uint32_t cie_offset;
81edee2579SRichard Henderson     uintptr_t func_start;
82edee2579SRichard Henderson     uintptr_t func_len;
83497a22ebSRichard Henderson } DebugFrameFDEHeader;
84497a22ebSRichard Henderson 
852c90784aSRichard Henderson typedef struct QEMU_PACKED {
862c90784aSRichard Henderson     DebugFrameCIE cie;
872c90784aSRichard Henderson     DebugFrameFDEHeader fde;
882c90784aSRichard Henderson } DebugFrameHeader;
892c90784aSRichard Henderson 
90813da627SRichard Henderson static void tcg_register_jit_int(void *buf, size_t size,
912c90784aSRichard Henderson                                  const void *debug_frame,
922c90784aSRichard Henderson                                  size_t debug_frame_size)
93813da627SRichard Henderson     __attribute__((unused));
94813da627SRichard Henderson 
95ce151109SPeter Maydell /* Forward declarations for functions declared and used in tcg-target.inc.c. */
96c0ad3001SStefan Weil static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str);
972a534affSRichard Henderson static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
98a05b5b9bSRichard Henderson                        intptr_t arg2);
992a534affSRichard Henderson static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
100c0ad3001SStefan Weil static void tcg_out_movi(TCGContext *s, TCGType type,
1012a534affSRichard Henderson                          TCGReg ret, tcg_target_long arg);
102c0ad3001SStefan Weil static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
103c0ad3001SStefan Weil                        const int *const_args);
1042a534affSRichard Henderson static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
105a05b5b9bSRichard Henderson                        intptr_t arg2);
106cf066674SRichard Henderson static void tcg_out_call(TCGContext *s, tcg_insn_unit *target);
107f6c6afc1SRichard Henderson static int tcg_target_const_match(tcg_target_long val, TCGType type,
108c0ad3001SStefan Weil                                   const TCGArgConstraint *arg_ct);
1099ecefc84SRichard Henderson static void tcg_out_tb_init(TCGContext *s);
11023dceda6SRichard Henderson static bool tcg_out_tb_finalize(TCGContext *s);
1119ecefc84SRichard Henderson 
112c0ad3001SStefan Weil 
113c896fe29Sbellard 
114b1d8e52eSblueswir1 static TCGRegSet tcg_target_available_regs[2];
115b1d8e52eSblueswir1 static TCGRegSet tcg_target_call_clobber_regs;
116c896fe29Sbellard 
1171813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE == 1
1184196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
119c896fe29Sbellard {
120c896fe29Sbellard     *s->code_ptr++ = v;
121c896fe29Sbellard }
122c896fe29Sbellard 
1234196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
1244196dca6SPeter Maydell                                                       uint8_t v)
1255c53bb81SPeter Maydell {
1261813e175SRichard Henderson     *p = v;
1275c53bb81SPeter Maydell }
1281813e175SRichard Henderson #endif
1295c53bb81SPeter Maydell 
1301813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE <= 2
1314196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
132c896fe29Sbellard {
1331813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
1341813e175SRichard Henderson         *s->code_ptr++ = v;
1351813e175SRichard Henderson     } else {
1361813e175SRichard Henderson         tcg_insn_unit *p = s->code_ptr;
1374387345aSPeter Maydell         memcpy(p, &v, sizeof(v));
1381813e175SRichard Henderson         s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
1391813e175SRichard Henderson     }
140c896fe29Sbellard }
141c896fe29Sbellard 
1424196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
1434196dca6SPeter Maydell                                                        uint16_t v)
1445c53bb81SPeter Maydell {
1451813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
1461813e175SRichard Henderson         *p = v;
1471813e175SRichard Henderson     } else {
1485c53bb81SPeter Maydell         memcpy(p, &v, sizeof(v));
1495c53bb81SPeter Maydell     }
1501813e175SRichard Henderson }
1511813e175SRichard Henderson #endif
1525c53bb81SPeter Maydell 
1531813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE <= 4
1544196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
155c896fe29Sbellard {
1561813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
1571813e175SRichard Henderson         *s->code_ptr++ = v;
1581813e175SRichard Henderson     } else {
1591813e175SRichard Henderson         tcg_insn_unit *p = s->code_ptr;
1604387345aSPeter Maydell         memcpy(p, &v, sizeof(v));
1611813e175SRichard Henderson         s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
1621813e175SRichard Henderson     }
163c896fe29Sbellard }
164c896fe29Sbellard 
1654196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
1664196dca6SPeter Maydell                                                        uint32_t v)
1675c53bb81SPeter Maydell {
1681813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
1691813e175SRichard Henderson         *p = v;
1701813e175SRichard Henderson     } else {
1715c53bb81SPeter Maydell         memcpy(p, &v, sizeof(v));
1725c53bb81SPeter Maydell     }
1731813e175SRichard Henderson }
1741813e175SRichard Henderson #endif
1755c53bb81SPeter Maydell 
1761813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE <= 8
1774196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
178ac26eb69SRichard Henderson {
1791813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
1801813e175SRichard Henderson         *s->code_ptr++ = v;
1811813e175SRichard Henderson     } else {
1821813e175SRichard Henderson         tcg_insn_unit *p = s->code_ptr;
1834387345aSPeter Maydell         memcpy(p, &v, sizeof(v));
1841813e175SRichard Henderson         s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
1851813e175SRichard Henderson     }
186ac26eb69SRichard Henderson }
187ac26eb69SRichard Henderson 
1884196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
1894196dca6SPeter Maydell                                                        uint64_t v)
1905c53bb81SPeter Maydell {
1911813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
1921813e175SRichard Henderson         *p = v;
1931813e175SRichard Henderson     } else {
1945c53bb81SPeter Maydell         memcpy(p, &v, sizeof(v));
1955c53bb81SPeter Maydell     }
1961813e175SRichard Henderson }
1971813e175SRichard Henderson #endif
1985c53bb81SPeter Maydell 
199c896fe29Sbellard /* label relocation processing */
200c896fe29Sbellard 
2011813e175SRichard Henderson static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
202bec16311SRichard Henderson                           TCGLabel *l, intptr_t addend)
203c896fe29Sbellard {
204c896fe29Sbellard     TCGRelocation *r;
205c896fe29Sbellard 
206c896fe29Sbellard     if (l->has_value) {
207623e265cSpbrook         /* FIXME: This may break relocations on RISC targets that
208623e265cSpbrook            modify instruction fields in place.  The caller may not have
209623e265cSpbrook            written the initial value.  */
210f54b3f92Saurel32         patch_reloc(code_ptr, type, l->u.value, addend);
211c896fe29Sbellard     } else {
212c896fe29Sbellard         /* add a new relocation entry */
213c896fe29Sbellard         r = tcg_malloc(sizeof(TCGRelocation));
214c896fe29Sbellard         r->type = type;
215c896fe29Sbellard         r->ptr = code_ptr;
216c896fe29Sbellard         r->addend = addend;
217c896fe29Sbellard         r->next = l->u.first_reloc;
218c896fe29Sbellard         l->u.first_reloc = r;
219c896fe29Sbellard     }
220c896fe29Sbellard }
221c896fe29Sbellard 
222bec16311SRichard Henderson static void tcg_out_label(TCGContext *s, TCGLabel *l, tcg_insn_unit *ptr)
223c896fe29Sbellard {
2242ba7fae2SRichard Henderson     intptr_t value = (intptr_t)ptr;
2251813e175SRichard Henderson     TCGRelocation *r;
226c896fe29Sbellard 
227eabb7b91SAurelien Jarno     tcg_debug_assert(!l->has_value);
2281813e175SRichard Henderson 
2291813e175SRichard Henderson     for (r = l->u.first_reloc; r != NULL; r = r->next) {
230f54b3f92Saurel32         patch_reloc(r->ptr, r->type, value, r->addend);
231c896fe29Sbellard     }
2321813e175SRichard Henderson 
233c896fe29Sbellard     l->has_value = 1;
2341813e175SRichard Henderson     l->u.value_ptr = ptr;
235c896fe29Sbellard }
236c896fe29Sbellard 
23742a268c2SRichard Henderson TCGLabel *gen_new_label(void)
238c896fe29Sbellard {
239c896fe29Sbellard     TCGContext *s = &tcg_ctx;
24051e3972cSRichard Henderson     TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
241c896fe29Sbellard 
24251e3972cSRichard Henderson     *l = (TCGLabel){
24351e3972cSRichard Henderson         .id = s->nb_labels++
24451e3972cSRichard Henderson     };
24542a268c2SRichard Henderson 
24642a268c2SRichard Henderson     return l;
247c896fe29Sbellard }
248c896fe29Sbellard 
249ce151109SPeter Maydell #include "tcg-target.inc.c"
250c896fe29Sbellard 
251c896fe29Sbellard /* pool based memory allocation */
252c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size)
253c896fe29Sbellard {
254c896fe29Sbellard     TCGPool *p;
255c896fe29Sbellard     int pool_size;
256c896fe29Sbellard 
257c896fe29Sbellard     if (size > TCG_POOL_CHUNK_SIZE) {
258c896fe29Sbellard         /* big malloc: insert a new pool (XXX: could optimize) */
2597267c094SAnthony Liguori         p = g_malloc(sizeof(TCGPool) + size);
260c896fe29Sbellard         p->size = size;
2614055299eSKirill Batuzov         p->next = s->pool_first_large;
2624055299eSKirill Batuzov         s->pool_first_large = p;
2634055299eSKirill Batuzov         return p->data;
264c896fe29Sbellard     } else {
265c896fe29Sbellard         p = s->pool_current;
266c896fe29Sbellard         if (!p) {
267c896fe29Sbellard             p = s->pool_first;
268c896fe29Sbellard             if (!p)
269c896fe29Sbellard                 goto new_pool;
270c896fe29Sbellard         } else {
271c896fe29Sbellard             if (!p->next) {
272c896fe29Sbellard             new_pool:
273c896fe29Sbellard                 pool_size = TCG_POOL_CHUNK_SIZE;
2747267c094SAnthony Liguori                 p = g_malloc(sizeof(TCGPool) + pool_size);
275c896fe29Sbellard                 p->size = pool_size;
276c896fe29Sbellard                 p->next = NULL;
277c896fe29Sbellard                 if (s->pool_current)
278c896fe29Sbellard                     s->pool_current->next = p;
279c896fe29Sbellard                 else
280c896fe29Sbellard                     s->pool_first = p;
281c896fe29Sbellard             } else {
282c896fe29Sbellard                 p = p->next;
283c896fe29Sbellard             }
284c896fe29Sbellard         }
285c896fe29Sbellard     }
286c896fe29Sbellard     s->pool_current = p;
287c896fe29Sbellard     s->pool_cur = p->data + size;
288c896fe29Sbellard     s->pool_end = p->data + p->size;
289c896fe29Sbellard     return p->data;
290c896fe29Sbellard }
291c896fe29Sbellard 
292c896fe29Sbellard void tcg_pool_reset(TCGContext *s)
293c896fe29Sbellard {
2944055299eSKirill Batuzov     TCGPool *p, *t;
2954055299eSKirill Batuzov     for (p = s->pool_first_large; p; p = t) {
2964055299eSKirill Batuzov         t = p->next;
2974055299eSKirill Batuzov         g_free(p);
2984055299eSKirill Batuzov     }
2994055299eSKirill Batuzov     s->pool_first_large = NULL;
300c896fe29Sbellard     s->pool_cur = s->pool_end = NULL;
301c896fe29Sbellard     s->pool_current = NULL;
302c896fe29Sbellard }
303c896fe29Sbellard 
304100b5e01SRichard Henderson typedef struct TCGHelperInfo {
305100b5e01SRichard Henderson     void *func;
306100b5e01SRichard Henderson     const char *name;
307afb49896SRichard Henderson     unsigned flags;
308afb49896SRichard Henderson     unsigned sizemask;
309100b5e01SRichard Henderson } TCGHelperInfo;
310100b5e01SRichard Henderson 
3112ef6175aSRichard Henderson #include "exec/helper-proto.h"
3122ef6175aSRichard Henderson 
313100b5e01SRichard Henderson static const TCGHelperInfo all_helpers[] = {
3142ef6175aSRichard Henderson #include "exec/helper-tcg.h"
315100b5e01SRichard Henderson };
316100b5e01SRichard Henderson 
31791478cefSRichard Henderson static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
31891478cefSRichard Henderson 
319c896fe29Sbellard void tcg_context_init(TCGContext *s)
320c896fe29Sbellard {
321100b5e01SRichard Henderson     int op, total_args, n, i;
322c896fe29Sbellard     TCGOpDef *def;
323c896fe29Sbellard     TCGArgConstraint *args_ct;
324c896fe29Sbellard     int *sorted_args;
32584fd9dd3SRichard Henderson     GHashTable *helper_table;
326c896fe29Sbellard 
327c896fe29Sbellard     memset(s, 0, sizeof(*s));
328c896fe29Sbellard     s->nb_globals = 0;
329c896fe29Sbellard 
330c896fe29Sbellard     /* Count total number of arguments and allocate the corresponding
331c896fe29Sbellard        space */
332c896fe29Sbellard     total_args = 0;
333c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
334c896fe29Sbellard         def = &tcg_op_defs[op];
335c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
336c896fe29Sbellard         total_args += n;
337c896fe29Sbellard     }
338c896fe29Sbellard 
3397267c094SAnthony Liguori     args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
3407267c094SAnthony Liguori     sorted_args = g_malloc(sizeof(int) * total_args);
341c896fe29Sbellard 
342c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
343c896fe29Sbellard         def = &tcg_op_defs[op];
344c896fe29Sbellard         def->args_ct = args_ct;
345c896fe29Sbellard         def->sorted_args = sorted_args;
346c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
347c896fe29Sbellard         sorted_args += n;
348c896fe29Sbellard         args_ct += n;
349c896fe29Sbellard     }
350c896fe29Sbellard 
3515cd8f621SRichard Henderson     /* Register helpers.  */
35284fd9dd3SRichard Henderson     /* Use g_direct_hash/equal for direct pointer comparisons on func.  */
35384fd9dd3SRichard Henderson     s->helpers = helper_table = g_hash_table_new(NULL, NULL);
35484fd9dd3SRichard Henderson 
355100b5e01SRichard Henderson     for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
35684fd9dd3SRichard Henderson         g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
35772866e82SRichard Henderson                             (gpointer)&all_helpers[i]);
358100b5e01SRichard Henderson     }
3595cd8f621SRichard Henderson 
360c896fe29Sbellard     tcg_target_init(s);
36191478cefSRichard Henderson 
36291478cefSRichard Henderson     /* Reverse the order of the saved registers, assuming they're all at
36391478cefSRichard Henderson        the start of tcg_target_reg_alloc_order.  */
36491478cefSRichard Henderson     for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
36591478cefSRichard Henderson         int r = tcg_target_reg_alloc_order[n];
36691478cefSRichard Henderson         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
36791478cefSRichard Henderson             break;
36891478cefSRichard Henderson         }
36991478cefSRichard Henderson     }
37091478cefSRichard Henderson     for (i = 0; i < n; ++i) {
37191478cefSRichard Henderson         indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
37291478cefSRichard Henderson     }
37391478cefSRichard Henderson     for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
37491478cefSRichard Henderson         indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
37591478cefSRichard Henderson     }
3769002ec79SRichard Henderson }
377b03cce8eSbellard 
3789002ec79SRichard Henderson void tcg_prologue_init(TCGContext *s)
3799002ec79SRichard Henderson {
3808163b749SRichard Henderson     size_t prologue_size, total_size;
3818163b749SRichard Henderson     void *buf0, *buf1;
3828163b749SRichard Henderson 
3838163b749SRichard Henderson     /* Put the prologue at the beginning of code_gen_buffer.  */
3848163b749SRichard Henderson     buf0 = s->code_gen_buffer;
3858163b749SRichard Henderson     s->code_ptr = buf0;
3868163b749SRichard Henderson     s->code_buf = buf0;
3878163b749SRichard Henderson     s->code_gen_prologue = buf0;
3888163b749SRichard Henderson 
3898163b749SRichard Henderson     /* Generate the prologue.  */
390b03cce8eSbellard     tcg_target_qemu_prologue(s);
3918163b749SRichard Henderson     buf1 = s->code_ptr;
3928163b749SRichard Henderson     flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
3938163b749SRichard Henderson 
3948163b749SRichard Henderson     /* Deduct the prologue from the buffer.  */
3958163b749SRichard Henderson     prologue_size = tcg_current_code_size(s);
3968163b749SRichard Henderson     s->code_gen_ptr = buf1;
3978163b749SRichard Henderson     s->code_gen_buffer = buf1;
3988163b749SRichard Henderson     s->code_buf = buf1;
3998163b749SRichard Henderson     total_size = s->code_gen_buffer_size - prologue_size;
4008163b749SRichard Henderson     s->code_gen_buffer_size = total_size;
4018163b749SRichard Henderson 
402b125f9dcSRichard Henderson     /* Compute a high-water mark, at which we voluntarily flush the buffer
403b125f9dcSRichard Henderson        and start over.  The size here is arbitrary, significantly larger
404b125f9dcSRichard Henderson        than we expect the code generation for any one opcode to require.  */
40523dceda6SRichard Henderson     s->code_gen_highwater = s->code_gen_buffer + (total_size - 1024);
4068163b749SRichard Henderson 
4078163b749SRichard Henderson     tcg_register_jit(s->code_gen_buffer, total_size);
408d6b64b2bSRichard Henderson 
409d6b64b2bSRichard Henderson #ifdef DEBUG_DISAS
410d6b64b2bSRichard Henderson     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
4118163b749SRichard Henderson         qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
4128163b749SRichard Henderson         log_disas(buf0, prologue_size);
413d6b64b2bSRichard Henderson         qemu_log("\n");
414d6b64b2bSRichard Henderson         qemu_log_flush();
415d6b64b2bSRichard Henderson     }
416d6b64b2bSRichard Henderson #endif
417c896fe29Sbellard }
418c896fe29Sbellard 
419c896fe29Sbellard void tcg_func_start(TCGContext *s)
420c896fe29Sbellard {
421c896fe29Sbellard     tcg_pool_reset(s);
422c896fe29Sbellard     s->nb_temps = s->nb_globals;
4230ec9eabcSRichard Henderson 
4240ec9eabcSRichard Henderson     /* No temps have been previously allocated for size or locality.  */
4250ec9eabcSRichard Henderson     memset(s->free_temps, 0, sizeof(s->free_temps));
4260ec9eabcSRichard Henderson 
427c896fe29Sbellard     s->nb_labels = 0;
428c896fe29Sbellard     s->current_frame_offset = s->frame_start;
429c896fe29Sbellard 
4300a209d4bSRichard Henderson #ifdef CONFIG_DEBUG_TCG
4310a209d4bSRichard Henderson     s->goto_tb_issue_mask = 0;
4320a209d4bSRichard Henderson #endif
4330a209d4bSRichard Henderson 
434c45cb8bbSRichard Henderson     s->gen_first_op_idx = 0;
435c45cb8bbSRichard Henderson     s->gen_last_op_idx = -1;
436c45cb8bbSRichard Henderson     s->gen_next_op_idx = 0;
437c45cb8bbSRichard Henderson     s->gen_next_parm_idx = 0;
438b76f0d8cSYeongkyoon Lee 
4399ecefc84SRichard Henderson     s->be = tcg_malloc(sizeof(TCGBackendData));
440c896fe29Sbellard }
441c896fe29Sbellard 
4427ca4b752SRichard Henderson static inline int temp_idx(TCGContext *s, TCGTemp *ts)
443c896fe29Sbellard {
4447ca4b752SRichard Henderson     ptrdiff_t n = ts - s->temps;
4457ca4b752SRichard Henderson     tcg_debug_assert(n >= 0 && n < s->nb_temps);
4467ca4b752SRichard Henderson     return n;
4477ca4b752SRichard Henderson }
4487ca4b752SRichard Henderson 
4497ca4b752SRichard Henderson static inline TCGTemp *tcg_temp_alloc(TCGContext *s)
4507ca4b752SRichard Henderson {
4517ca4b752SRichard Henderson     int n = s->nb_temps++;
4527ca4b752SRichard Henderson     tcg_debug_assert(n < TCG_MAX_TEMPS);
4537ca4b752SRichard Henderson     return memset(&s->temps[n], 0, sizeof(TCGTemp));
4547ca4b752SRichard Henderson }
4557ca4b752SRichard Henderson 
4567ca4b752SRichard Henderson static inline TCGTemp *tcg_global_alloc(TCGContext *s)
4577ca4b752SRichard Henderson {
4587ca4b752SRichard Henderson     tcg_debug_assert(s->nb_globals == s->nb_temps);
4597ca4b752SRichard Henderson     s->nb_globals++;
4607ca4b752SRichard Henderson     return tcg_temp_alloc(s);
461c896fe29Sbellard }
462c896fe29Sbellard 
463b3a62939SRichard Henderson static int tcg_global_reg_new_internal(TCGContext *s, TCGType type,
464b6638662SRichard Henderson                                        TCGReg reg, const char *name)
465c896fe29Sbellard {
466c896fe29Sbellard     TCGTemp *ts;
467c896fe29Sbellard 
468b3a62939SRichard Henderson     if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
469c896fe29Sbellard         tcg_abort();
470b3a62939SRichard Henderson     }
4717ca4b752SRichard Henderson 
4727ca4b752SRichard Henderson     ts = tcg_global_alloc(s);
473c896fe29Sbellard     ts->base_type = type;
474c896fe29Sbellard     ts->type = type;
475c896fe29Sbellard     ts->fixed_reg = 1;
476c896fe29Sbellard     ts->reg = reg;
477c896fe29Sbellard     ts->name = name;
478c896fe29Sbellard     tcg_regset_set_reg(s->reserved_regs, reg);
4797ca4b752SRichard Henderson 
4807ca4b752SRichard Henderson     return temp_idx(s, ts);
481a7812ae4Spbrook }
482a7812ae4Spbrook 
483b6638662SRichard Henderson void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
484a7812ae4Spbrook {
485a7812ae4Spbrook     int idx;
486b3a62939SRichard Henderson     s->frame_start = start;
487b3a62939SRichard Henderson     s->frame_end = start + size;
488b3a62939SRichard Henderson     idx = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
489b3a62939SRichard Henderson     s->frame_temp = &s->temps[idx];
490b3a62939SRichard Henderson }
491a7812ae4Spbrook 
492b6638662SRichard Henderson TCGv_i32 tcg_global_reg_new_i32(TCGReg reg, const char *name)
493b3a62939SRichard Henderson {
494b3a62939SRichard Henderson     TCGContext *s = &tcg_ctx;
495b3a62939SRichard Henderson     int idx;
496b3a62939SRichard Henderson 
497b3a62939SRichard Henderson     if (tcg_regset_test_reg(s->reserved_regs, reg)) {
498b3a62939SRichard Henderson         tcg_abort();
499b3a62939SRichard Henderson     }
500b3a62939SRichard Henderson     idx = tcg_global_reg_new_internal(s, TCG_TYPE_I32, reg, name);
501a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
502a7812ae4Spbrook }
503a7812ae4Spbrook 
504b6638662SRichard Henderson TCGv_i64 tcg_global_reg_new_i64(TCGReg reg, const char *name)
505a7812ae4Spbrook {
506b3a62939SRichard Henderson     TCGContext *s = &tcg_ctx;
507a7812ae4Spbrook     int idx;
508a7812ae4Spbrook 
509b3a62939SRichard Henderson     if (tcg_regset_test_reg(s->reserved_regs, reg)) {
510b3a62939SRichard Henderson         tcg_abort();
511b3a62939SRichard Henderson     }
512b3a62939SRichard Henderson     idx = tcg_global_reg_new_internal(s, TCG_TYPE_I64, reg, name);
513a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
514c896fe29Sbellard }
515c896fe29Sbellard 
516e1ccc054SRichard Henderson int tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
517e1ccc054SRichard Henderson                                 intptr_t offset, const char *name)
518c896fe29Sbellard {
519c896fe29Sbellard     TCGContext *s = &tcg_ctx;
5207ca4b752SRichard Henderson     TCGTemp *base_ts = &s->temps[GET_TCGV_PTR(base)];
5217ca4b752SRichard Henderson     TCGTemp *ts = tcg_global_alloc(s);
522b3915dbbSRichard Henderson     int indirect_reg = 0, bigendian = 0;
5237ca4b752SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
5247ca4b752SRichard Henderson     bigendian = 1;
5257ca4b752SRichard Henderson #endif
526c896fe29Sbellard 
527b3915dbbSRichard Henderson     if (!base_ts->fixed_reg) {
528b3915dbbSRichard Henderson         indirect_reg = 1;
529b3915dbbSRichard Henderson         base_ts->indirect_base = 1;
530b3915dbbSRichard Henderson     }
531b3915dbbSRichard Henderson 
5327ca4b752SRichard Henderson     if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
5337ca4b752SRichard Henderson         TCGTemp *ts2 = tcg_global_alloc(s);
534c896fe29Sbellard         char buf[64];
5357ca4b752SRichard Henderson 
5367ca4b752SRichard Henderson         ts->base_type = TCG_TYPE_I64;
537c896fe29Sbellard         ts->type = TCG_TYPE_I32;
538b3915dbbSRichard Henderson         ts->indirect_reg = indirect_reg;
539c896fe29Sbellard         ts->mem_allocated = 1;
540b3a62939SRichard Henderson         ts->mem_base = base_ts;
5417ca4b752SRichard Henderson         ts->mem_offset = offset + bigendian * 4;
542c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
543c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_0");
544c896fe29Sbellard         ts->name = strdup(buf);
545c896fe29Sbellard 
5467ca4b752SRichard Henderson         tcg_debug_assert(ts2 == ts + 1);
5477ca4b752SRichard Henderson         ts2->base_type = TCG_TYPE_I64;
5487ca4b752SRichard Henderson         ts2->type = TCG_TYPE_I32;
549b3915dbbSRichard Henderson         ts2->indirect_reg = indirect_reg;
5507ca4b752SRichard Henderson         ts2->mem_allocated = 1;
5517ca4b752SRichard Henderson         ts2->mem_base = base_ts;
5527ca4b752SRichard Henderson         ts2->mem_offset = offset + (1 - bigendian) * 4;
553c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
554c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_1");
555c896fe29Sbellard         ts->name = strdup(buf);
5567ca4b752SRichard Henderson     } else {
557c896fe29Sbellard         ts->base_type = type;
558c896fe29Sbellard         ts->type = type;
559b3915dbbSRichard Henderson         ts->indirect_reg = indirect_reg;
560c896fe29Sbellard         ts->mem_allocated = 1;
561b3a62939SRichard Henderson         ts->mem_base = base_ts;
562c896fe29Sbellard         ts->mem_offset = offset;
563c896fe29Sbellard         ts->name = name;
564c896fe29Sbellard     }
5657ca4b752SRichard Henderson     return temp_idx(s, ts);
566c896fe29Sbellard }
567c896fe29Sbellard 
5687ca4b752SRichard Henderson static int tcg_temp_new_internal(TCGType type, int temp_local)
569c896fe29Sbellard {
570c896fe29Sbellard     TCGContext *s = &tcg_ctx;
571c896fe29Sbellard     TCGTemp *ts;
572641d5fbeSbellard     int idx, k;
573c896fe29Sbellard 
5740ec9eabcSRichard Henderson     k = type + (temp_local ? TCG_TYPE_COUNT : 0);
5750ec9eabcSRichard Henderson     idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
5760ec9eabcSRichard Henderson     if (idx < TCG_MAX_TEMPS) {
5770ec9eabcSRichard Henderson         /* There is already an available temp with the right type.  */
5780ec9eabcSRichard Henderson         clear_bit(idx, s->free_temps[k].l);
5790ec9eabcSRichard Henderson 
580e8996ee0Sbellard         ts = &s->temps[idx];
581e8996ee0Sbellard         ts->temp_allocated = 1;
5827ca4b752SRichard Henderson         tcg_debug_assert(ts->base_type == type);
5837ca4b752SRichard Henderson         tcg_debug_assert(ts->temp_local == temp_local);
584e8996ee0Sbellard     } else {
5857ca4b752SRichard Henderson         ts = tcg_temp_alloc(s);
5867ca4b752SRichard Henderson         if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
5877ca4b752SRichard Henderson             TCGTemp *ts2 = tcg_temp_alloc(s);
5887ca4b752SRichard Henderson 
589c896fe29Sbellard             ts->base_type = type;
590c896fe29Sbellard             ts->type = TCG_TYPE_I32;
591e8996ee0Sbellard             ts->temp_allocated = 1;
592641d5fbeSbellard             ts->temp_local = temp_local;
5937ca4b752SRichard Henderson 
5947ca4b752SRichard Henderson             tcg_debug_assert(ts2 == ts + 1);
5957ca4b752SRichard Henderson             ts2->base_type = TCG_TYPE_I64;
5967ca4b752SRichard Henderson             ts2->type = TCG_TYPE_I32;
5977ca4b752SRichard Henderson             ts2->temp_allocated = 1;
5987ca4b752SRichard Henderson             ts2->temp_local = temp_local;
5997ca4b752SRichard Henderson         } else {
600c896fe29Sbellard             ts->base_type = type;
601c896fe29Sbellard             ts->type = type;
602e8996ee0Sbellard             ts->temp_allocated = 1;
603641d5fbeSbellard             ts->temp_local = temp_local;
604c896fe29Sbellard         }
6057ca4b752SRichard Henderson         idx = temp_idx(s, ts);
606e8996ee0Sbellard     }
60727bfd83cSPeter Maydell 
60827bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
60927bfd83cSPeter Maydell     s->temps_in_use++;
61027bfd83cSPeter Maydell #endif
611a7812ae4Spbrook     return idx;
612c896fe29Sbellard }
613c896fe29Sbellard 
614a7812ae4Spbrook TCGv_i32 tcg_temp_new_internal_i32(int temp_local)
615a7812ae4Spbrook {
616a7812ae4Spbrook     int idx;
617a7812ae4Spbrook 
618a7812ae4Spbrook     idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local);
619a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
620a7812ae4Spbrook }
621a7812ae4Spbrook 
622a7812ae4Spbrook TCGv_i64 tcg_temp_new_internal_i64(int temp_local)
623a7812ae4Spbrook {
624a7812ae4Spbrook     int idx;
625a7812ae4Spbrook 
626a7812ae4Spbrook     idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local);
627a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
628a7812ae4Spbrook }
629a7812ae4Spbrook 
6300ec9eabcSRichard Henderson static void tcg_temp_free_internal(int idx)
631c896fe29Sbellard {
632c896fe29Sbellard     TCGContext *s = &tcg_ctx;
633c896fe29Sbellard     TCGTemp *ts;
634641d5fbeSbellard     int k;
635c896fe29Sbellard 
63627bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
63727bfd83cSPeter Maydell     s->temps_in_use--;
63827bfd83cSPeter Maydell     if (s->temps_in_use < 0) {
63927bfd83cSPeter Maydell         fprintf(stderr, "More temporaries freed than allocated!\n");
64027bfd83cSPeter Maydell     }
64127bfd83cSPeter Maydell #endif
64227bfd83cSPeter Maydell 
643eabb7b91SAurelien Jarno     tcg_debug_assert(idx >= s->nb_globals && idx < s->nb_temps);
644c896fe29Sbellard     ts = &s->temps[idx];
645eabb7b91SAurelien Jarno     tcg_debug_assert(ts->temp_allocated != 0);
646e8996ee0Sbellard     ts->temp_allocated = 0;
6470ec9eabcSRichard Henderson 
64818d13fa2SAlexander Graf     k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
6490ec9eabcSRichard Henderson     set_bit(idx, s->free_temps[k].l);
650e8996ee0Sbellard }
651e8996ee0Sbellard 
652a7812ae4Spbrook void tcg_temp_free_i32(TCGv_i32 arg)
653e8996ee0Sbellard {
654a7812ae4Spbrook     tcg_temp_free_internal(GET_TCGV_I32(arg));
655a7812ae4Spbrook }
656a7812ae4Spbrook 
657a7812ae4Spbrook void tcg_temp_free_i64(TCGv_i64 arg)
658a7812ae4Spbrook {
659a7812ae4Spbrook     tcg_temp_free_internal(GET_TCGV_I64(arg));
660a7812ae4Spbrook }
661a7812ae4Spbrook 
662a7812ae4Spbrook TCGv_i32 tcg_const_i32(int32_t val)
663a7812ae4Spbrook {
664a7812ae4Spbrook     TCGv_i32 t0;
665a7812ae4Spbrook     t0 = tcg_temp_new_i32();
666e8996ee0Sbellard     tcg_gen_movi_i32(t0, val);
667e8996ee0Sbellard     return t0;
668c896fe29Sbellard }
669c896fe29Sbellard 
670a7812ae4Spbrook TCGv_i64 tcg_const_i64(int64_t val)
671c896fe29Sbellard {
672a7812ae4Spbrook     TCGv_i64 t0;
673a7812ae4Spbrook     t0 = tcg_temp_new_i64();
674e8996ee0Sbellard     tcg_gen_movi_i64(t0, val);
675e8996ee0Sbellard     return t0;
676c896fe29Sbellard }
677c896fe29Sbellard 
678a7812ae4Spbrook TCGv_i32 tcg_const_local_i32(int32_t val)
679bdffd4a9Saurel32 {
680a7812ae4Spbrook     TCGv_i32 t0;
681a7812ae4Spbrook     t0 = tcg_temp_local_new_i32();
682bdffd4a9Saurel32     tcg_gen_movi_i32(t0, val);
683bdffd4a9Saurel32     return t0;
684bdffd4a9Saurel32 }
685bdffd4a9Saurel32 
686a7812ae4Spbrook TCGv_i64 tcg_const_local_i64(int64_t val)
687bdffd4a9Saurel32 {
688a7812ae4Spbrook     TCGv_i64 t0;
689a7812ae4Spbrook     t0 = tcg_temp_local_new_i64();
690bdffd4a9Saurel32     tcg_gen_movi_i64(t0, val);
691bdffd4a9Saurel32     return t0;
692bdffd4a9Saurel32 }
693bdffd4a9Saurel32 
69427bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
69527bfd83cSPeter Maydell void tcg_clear_temp_count(void)
69627bfd83cSPeter Maydell {
69727bfd83cSPeter Maydell     TCGContext *s = &tcg_ctx;
69827bfd83cSPeter Maydell     s->temps_in_use = 0;
69927bfd83cSPeter Maydell }
70027bfd83cSPeter Maydell 
70127bfd83cSPeter Maydell int tcg_check_temp_count(void)
70227bfd83cSPeter Maydell {
70327bfd83cSPeter Maydell     TCGContext *s = &tcg_ctx;
70427bfd83cSPeter Maydell     if (s->temps_in_use) {
70527bfd83cSPeter Maydell         /* Clear the count so that we don't give another
70627bfd83cSPeter Maydell          * warning immediately next time around.
70727bfd83cSPeter Maydell          */
70827bfd83cSPeter Maydell         s->temps_in_use = 0;
70927bfd83cSPeter Maydell         return 1;
71027bfd83cSPeter Maydell     }
71127bfd83cSPeter Maydell     return 0;
71227bfd83cSPeter Maydell }
71327bfd83cSPeter Maydell #endif
71427bfd83cSPeter Maydell 
71539cf05d3Sbellard /* Note: we convert the 64 bit args to 32 bit and do some alignment
71639cf05d3Sbellard    and endian swap. Maybe it would be better to do the alignment
71739cf05d3Sbellard    and endian swap in tcg_reg_alloc_call(). */
718bbb8a1b4SRichard Henderson void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
719bbb8a1b4SRichard Henderson                    int nargs, TCGArg *args)
720c896fe29Sbellard {
721c45cb8bbSRichard Henderson     int i, real_args, nb_rets, pi, pi_first;
722bbb8a1b4SRichard Henderson     unsigned sizemask, flags;
723afb49896SRichard Henderson     TCGHelperInfo *info;
724afb49896SRichard Henderson 
725afb49896SRichard Henderson     info = g_hash_table_lookup(s->helpers, (gpointer)func);
726bbb8a1b4SRichard Henderson     flags = info->flags;
727bbb8a1b4SRichard Henderson     sizemask = info->sizemask;
7282bece2c8SRichard Henderson 
72934b1a49cSRichard Henderson #if defined(__sparc__) && !defined(__arch64__) \
73034b1a49cSRichard Henderson     && !defined(CONFIG_TCG_INTERPRETER)
73134b1a49cSRichard Henderson     /* We have 64-bit values in one register, but need to pass as two
73234b1a49cSRichard Henderson        separate parameters.  Split them.  */
73334b1a49cSRichard Henderson     int orig_sizemask = sizemask;
73434b1a49cSRichard Henderson     int orig_nargs = nargs;
73534b1a49cSRichard Henderson     TCGv_i64 retl, reth;
73634b1a49cSRichard Henderson 
73734b1a49cSRichard Henderson     TCGV_UNUSED_I64(retl);
73834b1a49cSRichard Henderson     TCGV_UNUSED_I64(reth);
73934b1a49cSRichard Henderson     if (sizemask != 0) {
74034b1a49cSRichard Henderson         TCGArg *split_args = __builtin_alloca(sizeof(TCGArg) * nargs * 2);
74134b1a49cSRichard Henderson         for (i = real_args = 0; i < nargs; ++i) {
74234b1a49cSRichard Henderson             int is_64bit = sizemask & (1 << (i+1)*2);
74334b1a49cSRichard Henderson             if (is_64bit) {
74434b1a49cSRichard Henderson                 TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
74534b1a49cSRichard Henderson                 TCGv_i32 h = tcg_temp_new_i32();
74634b1a49cSRichard Henderson                 TCGv_i32 l = tcg_temp_new_i32();
74734b1a49cSRichard Henderson                 tcg_gen_extr_i64_i32(l, h, orig);
74834b1a49cSRichard Henderson                 split_args[real_args++] = GET_TCGV_I32(h);
74934b1a49cSRichard Henderson                 split_args[real_args++] = GET_TCGV_I32(l);
75034b1a49cSRichard Henderson             } else {
75134b1a49cSRichard Henderson                 split_args[real_args++] = args[i];
75234b1a49cSRichard Henderson             }
75334b1a49cSRichard Henderson         }
75434b1a49cSRichard Henderson         nargs = real_args;
75534b1a49cSRichard Henderson         args = split_args;
75634b1a49cSRichard Henderson         sizemask = 0;
75734b1a49cSRichard Henderson     }
75834b1a49cSRichard Henderson #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
7592bece2c8SRichard Henderson     for (i = 0; i < nargs; ++i) {
7602bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
7612bece2c8SRichard Henderson         int is_signed = sizemask & (2 << (i+1)*2);
7622bece2c8SRichard Henderson         if (!is_64bit) {
7632bece2c8SRichard Henderson             TCGv_i64 temp = tcg_temp_new_i64();
7642bece2c8SRichard Henderson             TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
7652bece2c8SRichard Henderson             if (is_signed) {
7662bece2c8SRichard Henderson                 tcg_gen_ext32s_i64(temp, orig);
7672bece2c8SRichard Henderson             } else {
7682bece2c8SRichard Henderson                 tcg_gen_ext32u_i64(temp, orig);
7692bece2c8SRichard Henderson             }
7702bece2c8SRichard Henderson             args[i] = GET_TCGV_I64(temp);
7712bece2c8SRichard Henderson         }
7722bece2c8SRichard Henderson     }
7732bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */
7742bece2c8SRichard Henderson 
775c45cb8bbSRichard Henderson     pi_first = pi = s->gen_next_parm_idx;
776a7812ae4Spbrook     if (ret != TCG_CALL_DUMMY_ARG) {
77734b1a49cSRichard Henderson #if defined(__sparc__) && !defined(__arch64__) \
77834b1a49cSRichard Henderson     && !defined(CONFIG_TCG_INTERPRETER)
77934b1a49cSRichard Henderson         if (orig_sizemask & 1) {
78034b1a49cSRichard Henderson             /* The 32-bit ABI is going to return the 64-bit value in
78134b1a49cSRichard Henderson                the %o0/%o1 register pair.  Prepare for this by using
78234b1a49cSRichard Henderson                two return temporaries, and reassemble below.  */
78334b1a49cSRichard Henderson             retl = tcg_temp_new_i64();
78434b1a49cSRichard Henderson             reth = tcg_temp_new_i64();
785c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = GET_TCGV_I64(reth);
786c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = GET_TCGV_I64(retl);
78734b1a49cSRichard Henderson             nb_rets = 2;
78834b1a49cSRichard Henderson         } else {
789c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
79034b1a49cSRichard Henderson             nb_rets = 1;
79134b1a49cSRichard Henderson         }
79234b1a49cSRichard Henderson #else
79334b1a49cSRichard Henderson         if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
79402eb19d0SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
795c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret + 1;
796c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
797a7812ae4Spbrook #else
798c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
799c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret + 1;
800a7812ae4Spbrook #endif
801a7812ae4Spbrook             nb_rets = 2;
80234b1a49cSRichard Henderson         } else {
803c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
804a7812ae4Spbrook             nb_rets = 1;
805a7812ae4Spbrook         }
80634b1a49cSRichard Henderson #endif
807a7812ae4Spbrook     } else {
808a7812ae4Spbrook         nb_rets = 0;
809a7812ae4Spbrook     }
810a7812ae4Spbrook     real_args = 0;
811a7812ae4Spbrook     for (i = 0; i < nargs; i++) {
8122bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
813bbb8a1b4SRichard Henderson         if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
81439cf05d3Sbellard #ifdef TCG_TARGET_CALL_ALIGN_ARGS
81539cf05d3Sbellard             /* some targets want aligned 64 bit args */
816ebd486d5Smalc             if (real_args & 1) {
817c45cb8bbSRichard Henderson                 s->gen_opparam_buf[pi++] = TCG_CALL_DUMMY_ARG;
818ebd486d5Smalc                 real_args++;
81939cf05d3Sbellard             }
82039cf05d3Sbellard #endif
8213f90f252SRichard Henderson 	    /* If stack grows up, then we will be placing successive
8223f90f252SRichard Henderson 	       arguments at lower addresses, which means we need to
8233f90f252SRichard Henderson 	       reverse the order compared to how we would normally
8243f90f252SRichard Henderson 	       treat either big or little-endian.  For those arguments
8253f90f252SRichard Henderson 	       that will wind up in registers, this still works for
8263f90f252SRichard Henderson 	       HPPA (the only current STACK_GROWSUP target) since the
8273f90f252SRichard Henderson 	       argument registers are *also* allocated in decreasing
8283f90f252SRichard Henderson 	       order.  If another such target is added, this logic may
8293f90f252SRichard Henderson 	       have to get more complicated to differentiate between
8303f90f252SRichard Henderson 	       stack arguments and register arguments.  */
83102eb19d0SRichard Henderson #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
832c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i] + 1;
833c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i];
834c896fe29Sbellard #else
835c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i];
836c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i] + 1;
837c896fe29Sbellard #endif
838a7812ae4Spbrook             real_args += 2;
8392bece2c8SRichard Henderson             continue;
8402bece2c8SRichard Henderson         }
8412bece2c8SRichard Henderson 
842c45cb8bbSRichard Henderson         s->gen_opparam_buf[pi++] = args[i];
843a7812ae4Spbrook         real_args++;
844c896fe29Sbellard     }
845c45cb8bbSRichard Henderson     s->gen_opparam_buf[pi++] = (uintptr_t)func;
846c45cb8bbSRichard Henderson     s->gen_opparam_buf[pi++] = flags;
847a7812ae4Spbrook 
848c45cb8bbSRichard Henderson     i = s->gen_next_op_idx;
849c45cb8bbSRichard Henderson     tcg_debug_assert(i < OPC_BUF_SIZE);
850c45cb8bbSRichard Henderson     tcg_debug_assert(pi <= OPPARAM_BUF_SIZE);
851a7812ae4Spbrook 
852c45cb8bbSRichard Henderson     /* Set links for sequential allocation during translation.  */
853c45cb8bbSRichard Henderson     s->gen_op_buf[i] = (TCGOp){
854c45cb8bbSRichard Henderson         .opc = INDEX_op_call,
855c45cb8bbSRichard Henderson         .callo = nb_rets,
856c45cb8bbSRichard Henderson         .calli = real_args,
857c45cb8bbSRichard Henderson         .args = pi_first,
858c45cb8bbSRichard Henderson         .prev = i - 1,
859c45cb8bbSRichard Henderson         .next = i + 1
860c45cb8bbSRichard Henderson     };
861c45cb8bbSRichard Henderson 
862c45cb8bbSRichard Henderson     /* Make sure the calli field didn't overflow.  */
863c45cb8bbSRichard Henderson     tcg_debug_assert(s->gen_op_buf[i].calli == real_args);
864c45cb8bbSRichard Henderson 
865c45cb8bbSRichard Henderson     s->gen_last_op_idx = i;
866c45cb8bbSRichard Henderson     s->gen_next_op_idx = i + 1;
867c45cb8bbSRichard Henderson     s->gen_next_parm_idx = pi;
8682bece2c8SRichard Henderson 
86934b1a49cSRichard Henderson #if defined(__sparc__) && !defined(__arch64__) \
87034b1a49cSRichard Henderson     && !defined(CONFIG_TCG_INTERPRETER)
87134b1a49cSRichard Henderson     /* Free all of the parts we allocated above.  */
87234b1a49cSRichard Henderson     for (i = real_args = 0; i < orig_nargs; ++i) {
87334b1a49cSRichard Henderson         int is_64bit = orig_sizemask & (1 << (i+1)*2);
87434b1a49cSRichard Henderson         if (is_64bit) {
87534b1a49cSRichard Henderson             TCGv_i32 h = MAKE_TCGV_I32(args[real_args++]);
87634b1a49cSRichard Henderson             TCGv_i32 l = MAKE_TCGV_I32(args[real_args++]);
87734b1a49cSRichard Henderson             tcg_temp_free_i32(h);
87834b1a49cSRichard Henderson             tcg_temp_free_i32(l);
87934b1a49cSRichard Henderson         } else {
88034b1a49cSRichard Henderson             real_args++;
88134b1a49cSRichard Henderson         }
88234b1a49cSRichard Henderson     }
88334b1a49cSRichard Henderson     if (orig_sizemask & 1) {
88434b1a49cSRichard Henderson         /* The 32-bit ABI returned two 32-bit pieces.  Re-assemble them.
88534b1a49cSRichard Henderson            Note that describing these as TCGv_i64 eliminates an unnecessary
88634b1a49cSRichard Henderson            zero-extension that tcg_gen_concat_i32_i64 would create.  */
88734b1a49cSRichard Henderson         tcg_gen_concat32_i64(MAKE_TCGV_I64(ret), retl, reth);
88834b1a49cSRichard Henderson         tcg_temp_free_i64(retl);
88934b1a49cSRichard Henderson         tcg_temp_free_i64(reth);
89034b1a49cSRichard Henderson     }
89134b1a49cSRichard Henderson #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
8922bece2c8SRichard Henderson     for (i = 0; i < nargs; ++i) {
8932bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
8942bece2c8SRichard Henderson         if (!is_64bit) {
8952bece2c8SRichard Henderson             TCGv_i64 temp = MAKE_TCGV_I64(args[i]);
8962bece2c8SRichard Henderson             tcg_temp_free_i64(temp);
8972bece2c8SRichard Henderson         }
8982bece2c8SRichard Henderson     }
8992bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */
900a7812ae4Spbrook }
901c896fe29Sbellard 
9028fcd3692Sblueswir1 static void tcg_reg_alloc_start(TCGContext *s)
903c896fe29Sbellard {
904c896fe29Sbellard     int i;
905c896fe29Sbellard     TCGTemp *ts;
906c896fe29Sbellard     for(i = 0; i < s->nb_globals; i++) {
907c896fe29Sbellard         ts = &s->temps[i];
908c896fe29Sbellard         if (ts->fixed_reg) {
909c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
910c896fe29Sbellard         } else {
911c896fe29Sbellard             ts->val_type = TEMP_VAL_MEM;
912c896fe29Sbellard         }
913c896fe29Sbellard     }
914e8996ee0Sbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
915e8996ee0Sbellard         ts = &s->temps[i];
9167dfd8c6aSAurelien Jarno         if (ts->temp_local) {
9177dfd8c6aSAurelien Jarno             ts->val_type = TEMP_VAL_MEM;
9187dfd8c6aSAurelien Jarno         } else {
919e8996ee0Sbellard             ts->val_type = TEMP_VAL_DEAD;
9207dfd8c6aSAurelien Jarno         }
921e8996ee0Sbellard         ts->mem_allocated = 0;
922e8996ee0Sbellard         ts->fixed_reg = 0;
923e8996ee0Sbellard     }
924f8b2f202SRichard Henderson 
925f8b2f202SRichard Henderson     memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
926c896fe29Sbellard }
927c896fe29Sbellard 
928f8b2f202SRichard Henderson static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
929f8b2f202SRichard Henderson                                  TCGTemp *ts)
930c896fe29Sbellard {
931f8b2f202SRichard Henderson     int idx = temp_idx(s, ts);
932ac56dd48Spbrook 
933ac56dd48Spbrook     if (idx < s->nb_globals) {
934ac56dd48Spbrook         pstrcpy(buf, buf_size, ts->name);
935f8b2f202SRichard Henderson     } else if (ts->temp_local) {
936641d5fbeSbellard         snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
937f8b2f202SRichard Henderson     } else {
938ac56dd48Spbrook         snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
939c896fe29Sbellard     }
940c896fe29Sbellard     return buf;
941c896fe29Sbellard }
942c896fe29Sbellard 
943f8b2f202SRichard Henderson static char *tcg_get_arg_str_idx(TCGContext *s, char *buf,
944f8b2f202SRichard Henderson                                  int buf_size, int idx)
945f8b2f202SRichard Henderson {
946eabb7b91SAurelien Jarno     tcg_debug_assert(idx >= 0 && idx < s->nb_temps);
947f8b2f202SRichard Henderson     return tcg_get_arg_str_ptr(s, buf, buf_size, &s->temps[idx]);
948f8b2f202SRichard Henderson }
949f8b2f202SRichard Henderson 
9506e085f72SRichard Henderson /* Find helper name.  */
9516e085f72SRichard Henderson static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
952e8996ee0Sbellard {
9536e085f72SRichard Henderson     const char *ret = NULL;
9546e085f72SRichard Henderson     if (s->helpers) {
95572866e82SRichard Henderson         TCGHelperInfo *info = g_hash_table_lookup(s->helpers, (gpointer)val);
95672866e82SRichard Henderson         if (info) {
95772866e82SRichard Henderson             ret = info->name;
95872866e82SRichard Henderson         }
959e8996ee0Sbellard     }
9606e085f72SRichard Henderson     return ret;
9614dc81f28Sbellard }
9624dc81f28Sbellard 
963f48f3edeSblueswir1 static const char * const cond_name[] =
964f48f3edeSblueswir1 {
9650aed257fSRichard Henderson     [TCG_COND_NEVER] = "never",
9660aed257fSRichard Henderson     [TCG_COND_ALWAYS] = "always",
967f48f3edeSblueswir1     [TCG_COND_EQ] = "eq",
968f48f3edeSblueswir1     [TCG_COND_NE] = "ne",
969f48f3edeSblueswir1     [TCG_COND_LT] = "lt",
970f48f3edeSblueswir1     [TCG_COND_GE] = "ge",
971f48f3edeSblueswir1     [TCG_COND_LE] = "le",
972f48f3edeSblueswir1     [TCG_COND_GT] = "gt",
973f48f3edeSblueswir1     [TCG_COND_LTU] = "ltu",
974f48f3edeSblueswir1     [TCG_COND_GEU] = "geu",
975f48f3edeSblueswir1     [TCG_COND_LEU] = "leu",
976f48f3edeSblueswir1     [TCG_COND_GTU] = "gtu"
977f48f3edeSblueswir1 };
978f48f3edeSblueswir1 
979f713d6adSRichard Henderson static const char * const ldst_name[] =
980f713d6adSRichard Henderson {
981f713d6adSRichard Henderson     [MO_UB]   = "ub",
982f713d6adSRichard Henderson     [MO_SB]   = "sb",
983f713d6adSRichard Henderson     [MO_LEUW] = "leuw",
984f713d6adSRichard Henderson     [MO_LESW] = "lesw",
985f713d6adSRichard Henderson     [MO_LEUL] = "leul",
986f713d6adSRichard Henderson     [MO_LESL] = "lesl",
987f713d6adSRichard Henderson     [MO_LEQ]  = "leq",
988f713d6adSRichard Henderson     [MO_BEUW] = "beuw",
989f713d6adSRichard Henderson     [MO_BESW] = "besw",
990f713d6adSRichard Henderson     [MO_BEUL] = "beul",
991f713d6adSRichard Henderson     [MO_BESL] = "besl",
992f713d6adSRichard Henderson     [MO_BEQ]  = "beq",
993f713d6adSRichard Henderson };
994f713d6adSRichard Henderson 
995eeacee4dSBlue Swirl void tcg_dump_ops(TCGContext *s)
996c896fe29Sbellard {
997c896fe29Sbellard     char buf[128];
998c45cb8bbSRichard Henderson     TCGOp *op;
999c45cb8bbSRichard Henderson     int oi;
1000c896fe29Sbellard 
1001c45cb8bbSRichard Henderson     for (oi = s->gen_first_op_idx; oi >= 0; oi = op->next) {
1002c45cb8bbSRichard Henderson         int i, k, nb_oargs, nb_iargs, nb_cargs;
1003c45cb8bbSRichard Henderson         const TCGOpDef *def;
1004c45cb8bbSRichard Henderson         const TCGArg *args;
1005c45cb8bbSRichard Henderson         TCGOpcode c;
1006c45cb8bbSRichard Henderson 
1007c45cb8bbSRichard Henderson         op = &s->gen_op_buf[oi];
1008c45cb8bbSRichard Henderson         c = op->opc;
1009c896fe29Sbellard         def = &tcg_op_defs[c];
1010c45cb8bbSRichard Henderson         args = &s->gen_opparam_buf[op->args];
1011c45cb8bbSRichard Henderson 
1012765b842aSRichard Henderson         if (c == INDEX_op_insn_start) {
10139aef40edSRichard Henderson             qemu_log("%s ----", oi != s->gen_first_op_idx ? "\n" : "");
10149aef40edSRichard Henderson 
10159aef40edSRichard Henderson             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
10169aef40edSRichard Henderson                 target_ulong a;
10177e4597d7Sbellard #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
10189aef40edSRichard Henderson                 a = ((target_ulong)args[i * 2 + 1] << 32) | args[i * 2];
10197e4597d7Sbellard #else
10209aef40edSRichard Henderson                 a = args[i];
10217e4597d7Sbellard #endif
10229aef40edSRichard Henderson                 qemu_log(" " TARGET_FMT_lx, a);
1023eeacee4dSBlue Swirl             }
10247e4597d7Sbellard         } else if (c == INDEX_op_call) {
1025c896fe29Sbellard             /* variable number of arguments */
1026c45cb8bbSRichard Henderson             nb_oargs = op->callo;
1027c45cb8bbSRichard Henderson             nb_iargs = op->calli;
1028c896fe29Sbellard             nb_cargs = def->nb_cargs;
1029b03cce8eSbellard 
1030cf066674SRichard Henderson             /* function name, flags, out args */
1031cf066674SRichard Henderson             qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
1032cf066674SRichard Henderson                      tcg_find_helper(s, args[nb_oargs + nb_iargs]),
1033cf066674SRichard Henderson                      args[nb_oargs + nb_iargs + 1], nb_oargs);
1034b03cce8eSbellard             for (i = 0; i < nb_oargs; i++) {
1035cf066674SRichard Henderson                 qemu_log(",%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1036eeacee4dSBlue Swirl                                                    args[i]));
1037b03cce8eSbellard             }
1038cf066674SRichard Henderson             for (i = 0; i < nb_iargs; i++) {
1039cf066674SRichard Henderson                 TCGArg arg = args[nb_oargs + i];
1040cf066674SRichard Henderson                 const char *t = "<dummy>";
1041cf066674SRichard Henderson                 if (arg != TCG_CALL_DUMMY_ARG) {
1042cf066674SRichard Henderson                     t = tcg_get_arg_str_idx(s, buf, sizeof(buf), arg);
1043b03cce8eSbellard                 }
1044cf066674SRichard Henderson                 qemu_log(",%s", t);
1045e8996ee0Sbellard             }
1046b03cce8eSbellard         } else {
1047eeacee4dSBlue Swirl             qemu_log(" %s ", def->name);
1048c45cb8bbSRichard Henderson 
1049c896fe29Sbellard             nb_oargs = def->nb_oargs;
1050c896fe29Sbellard             nb_iargs = def->nb_iargs;
1051c896fe29Sbellard             nb_cargs = def->nb_cargs;
1052c896fe29Sbellard 
1053c896fe29Sbellard             k = 0;
1054c896fe29Sbellard             for (i = 0; i < nb_oargs; i++) {
1055eeacee4dSBlue Swirl                 if (k != 0) {
1056eeacee4dSBlue Swirl                     qemu_log(",");
1057eeacee4dSBlue Swirl                 }
1058eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1059eeacee4dSBlue Swirl                                                    args[k++]));
1060c896fe29Sbellard             }
1061c896fe29Sbellard             for (i = 0; i < nb_iargs; i++) {
1062eeacee4dSBlue Swirl                 if (k != 0) {
1063eeacee4dSBlue Swirl                     qemu_log(",");
1064eeacee4dSBlue Swirl                 }
1065eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1066eeacee4dSBlue Swirl                                                    args[k++]));
1067c896fe29Sbellard             }
1068be210acbSRichard Henderson             switch (c) {
1069be210acbSRichard Henderson             case INDEX_op_brcond_i32:
1070ffc5ea09SRichard Henderson             case INDEX_op_setcond_i32:
1071ffc5ea09SRichard Henderson             case INDEX_op_movcond_i32:
1072be210acbSRichard Henderson             case INDEX_op_brcond2_i32:
1073be210acbSRichard Henderson             case INDEX_op_setcond2_i32:
1074ffc5ea09SRichard Henderson             case INDEX_op_brcond_i64:
1075be210acbSRichard Henderson             case INDEX_op_setcond_i64:
1076ffc5ea09SRichard Henderson             case INDEX_op_movcond_i64:
1077eeacee4dSBlue Swirl                 if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]]) {
1078eeacee4dSBlue Swirl                     qemu_log(",%s", cond_name[args[k++]]);
1079eeacee4dSBlue Swirl                 } else {
1080eeacee4dSBlue Swirl                     qemu_log(",$0x%" TCG_PRIlx, args[k++]);
1081eeacee4dSBlue Swirl                 }
1082f48f3edeSblueswir1                 i = 1;
1083be210acbSRichard Henderson                 break;
1084f713d6adSRichard Henderson             case INDEX_op_qemu_ld_i32:
1085f713d6adSRichard Henderson             case INDEX_op_qemu_st_i32:
1086f713d6adSRichard Henderson             case INDEX_op_qemu_ld_i64:
1087f713d6adSRichard Henderson             case INDEX_op_qemu_st_i64:
108859227d5dSRichard Henderson                 {
108959227d5dSRichard Henderson                     TCGMemOpIdx oi = args[k++];
109059227d5dSRichard Henderson                     TCGMemOp op = get_memop(oi);
109159227d5dSRichard Henderson                     unsigned ix = get_mmuidx(oi);
109259227d5dSRichard Henderson 
109359c4b7e8SRichard Henderson                     if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
109459227d5dSRichard Henderson                         qemu_log(",$0x%x,%u", op, ix);
109559c4b7e8SRichard Henderson                     } else {
109659c4b7e8SRichard Henderson                         const char *s_al = "", *s_op;
109759c4b7e8SRichard Henderson                         if (op & MO_AMASK) {
109859c4b7e8SRichard Henderson                             if ((op & MO_AMASK) == MO_ALIGN) {
109959c4b7e8SRichard Henderson                                 s_al = "al+";
110059c4b7e8SRichard Henderson                             } else {
110159c4b7e8SRichard Henderson                                 s_al = "un+";
110259c4b7e8SRichard Henderson                             }
110359c4b7e8SRichard Henderson                         }
110459c4b7e8SRichard Henderson                         s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
110559c4b7e8SRichard Henderson                         qemu_log(",%s%s,%u", s_al, s_op, ix);
1106f713d6adSRichard Henderson                     }
1107f713d6adSRichard Henderson                     i = 1;
110859227d5dSRichard Henderson                 }
1109f713d6adSRichard Henderson                 break;
1110be210acbSRichard Henderson             default:
1111f48f3edeSblueswir1                 i = 0;
1112be210acbSRichard Henderson                 break;
1113be210acbSRichard Henderson             }
111451e3972cSRichard Henderson             switch (c) {
111551e3972cSRichard Henderson             case INDEX_op_set_label:
111651e3972cSRichard Henderson             case INDEX_op_br:
111751e3972cSRichard Henderson             case INDEX_op_brcond_i32:
111851e3972cSRichard Henderson             case INDEX_op_brcond_i64:
111951e3972cSRichard Henderson             case INDEX_op_brcond2_i32:
112051e3972cSRichard Henderson                 qemu_log("%s$L%d", k ? "," : "", arg_label(args[k])->id);
112151e3972cSRichard Henderson                 i++, k++;
112251e3972cSRichard Henderson                 break;
112351e3972cSRichard Henderson             default:
112451e3972cSRichard Henderson                 break;
1125eeacee4dSBlue Swirl             }
112651e3972cSRichard Henderson             for (; i < nb_cargs; i++, k++) {
112751e3972cSRichard Henderson                 qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", args[k]);
1128c896fe29Sbellard             }
1129b03cce8eSbellard         }
1130eeacee4dSBlue Swirl         qemu_log("\n");
1131c896fe29Sbellard     }
1132c896fe29Sbellard }
1133c896fe29Sbellard 
1134c896fe29Sbellard /* we give more priority to constraints with less registers */
1135c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k)
1136c896fe29Sbellard {
1137c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1138c896fe29Sbellard 
1139c896fe29Sbellard     int i, n;
1140c896fe29Sbellard     arg_ct = &def->args_ct[k];
1141c896fe29Sbellard     if (arg_ct->ct & TCG_CT_ALIAS) {
1142c896fe29Sbellard         /* an alias is equivalent to a single register */
1143c896fe29Sbellard         n = 1;
1144c896fe29Sbellard     } else {
1145c896fe29Sbellard         if (!(arg_ct->ct & TCG_CT_REG))
1146c896fe29Sbellard             return 0;
1147c896fe29Sbellard         n = 0;
1148c896fe29Sbellard         for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1149c896fe29Sbellard             if (tcg_regset_test_reg(arg_ct->u.regs, i))
1150c896fe29Sbellard                 n++;
1151c896fe29Sbellard         }
1152c896fe29Sbellard     }
1153c896fe29Sbellard     return TCG_TARGET_NB_REGS - n + 1;
1154c896fe29Sbellard }
1155c896fe29Sbellard 
1156c896fe29Sbellard /* sort from highest priority to lowest */
1157c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n)
1158c896fe29Sbellard {
1159c896fe29Sbellard     int i, j, p1, p2, tmp;
1160c896fe29Sbellard 
1161c896fe29Sbellard     for(i = 0; i < n; i++)
1162c896fe29Sbellard         def->sorted_args[start + i] = start + i;
1163c896fe29Sbellard     if (n <= 1)
1164c896fe29Sbellard         return;
1165c896fe29Sbellard     for(i = 0; i < n - 1; i++) {
1166c896fe29Sbellard         for(j = i + 1; j < n; j++) {
1167c896fe29Sbellard             p1 = get_constraint_priority(def, def->sorted_args[start + i]);
1168c896fe29Sbellard             p2 = get_constraint_priority(def, def->sorted_args[start + j]);
1169c896fe29Sbellard             if (p1 < p2) {
1170c896fe29Sbellard                 tmp = def->sorted_args[start + i];
1171c896fe29Sbellard                 def->sorted_args[start + i] = def->sorted_args[start + j];
1172c896fe29Sbellard                 def->sorted_args[start + j] = tmp;
1173c896fe29Sbellard             }
1174c896fe29Sbellard         }
1175c896fe29Sbellard     }
1176c896fe29Sbellard }
1177c896fe29Sbellard 
1178c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
1179c896fe29Sbellard {
1180a9751609SRichard Henderson     TCGOpcode op;
1181c896fe29Sbellard     TCGOpDef *def;
1182c896fe29Sbellard     const char *ct_str;
1183c896fe29Sbellard     int i, nb_args;
1184c896fe29Sbellard 
1185c896fe29Sbellard     for(;;) {
1186a9751609SRichard Henderson         if (tdefs->op == (TCGOpcode)-1)
1187c896fe29Sbellard             break;
1188c896fe29Sbellard         op = tdefs->op;
1189eabb7b91SAurelien Jarno         tcg_debug_assert((unsigned)op < NB_OPS);
1190c896fe29Sbellard         def = &tcg_op_defs[op];
1191c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG)
1192c68aaa18SStefan Weil         /* Duplicate entry in op definitions? */
1193eabb7b91SAurelien Jarno         tcg_debug_assert(!def->used);
1194c68aaa18SStefan Weil         def->used = 1;
1195c68aaa18SStefan Weil #endif
1196c896fe29Sbellard         nb_args = def->nb_iargs + def->nb_oargs;
1197c896fe29Sbellard         for(i = 0; i < nb_args; i++) {
1198c896fe29Sbellard             ct_str = tdefs->args_ct_str[i];
1199c68aaa18SStefan Weil             /* Incomplete TCGTargetOpDef entry? */
1200eabb7b91SAurelien Jarno             tcg_debug_assert(ct_str != NULL);
1201c896fe29Sbellard             tcg_regset_clear(def->args_ct[i].u.regs);
1202c896fe29Sbellard             def->args_ct[i].ct = 0;
1203c896fe29Sbellard             if (ct_str[0] >= '0' && ct_str[0] <= '9') {
1204c896fe29Sbellard                 int oarg;
1205c896fe29Sbellard                 oarg = ct_str[0] - '0';
1206eabb7b91SAurelien Jarno                 tcg_debug_assert(oarg < def->nb_oargs);
1207eabb7b91SAurelien Jarno                 tcg_debug_assert(def->args_ct[oarg].ct & TCG_CT_REG);
1208c896fe29Sbellard                 /* TCG_CT_ALIAS is for the output arguments. The input
12095ff9d6a4Sbellard                    argument is tagged with TCG_CT_IALIAS. */
1210c896fe29Sbellard                 def->args_ct[i] = def->args_ct[oarg];
12115ff9d6a4Sbellard                 def->args_ct[oarg].ct = TCG_CT_ALIAS;
12125ff9d6a4Sbellard                 def->args_ct[oarg].alias_index = i;
1213c896fe29Sbellard                 def->args_ct[i].ct |= TCG_CT_IALIAS;
12145ff9d6a4Sbellard                 def->args_ct[i].alias_index = oarg;
1215c896fe29Sbellard             } else {
1216c896fe29Sbellard                 for(;;) {
1217c896fe29Sbellard                     if (*ct_str == '\0')
1218c896fe29Sbellard                         break;
1219c896fe29Sbellard                     switch(*ct_str) {
1220c896fe29Sbellard                     case 'i':
1221c896fe29Sbellard                         def->args_ct[i].ct |= TCG_CT_CONST;
1222c896fe29Sbellard                         ct_str++;
1223c896fe29Sbellard                         break;
1224c896fe29Sbellard                     default:
1225c896fe29Sbellard                         if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
1226c896fe29Sbellard                             fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
1227c896fe29Sbellard                                     ct_str, i, def->name);
1228c896fe29Sbellard                             exit(1);
1229c896fe29Sbellard                         }
1230c896fe29Sbellard                     }
1231c896fe29Sbellard                 }
1232c896fe29Sbellard             }
1233c896fe29Sbellard         }
1234c896fe29Sbellard 
1235c68aaa18SStefan Weil         /* TCGTargetOpDef entry with too much information? */
1236eabb7b91SAurelien Jarno         tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
1237c68aaa18SStefan Weil 
1238c896fe29Sbellard         /* sort the constraints (XXX: this is just an heuristic) */
1239c896fe29Sbellard         sort_constraints(def, 0, def->nb_oargs);
1240c896fe29Sbellard         sort_constraints(def, def->nb_oargs, def->nb_iargs);
1241c896fe29Sbellard 
1242c896fe29Sbellard #if 0
1243c896fe29Sbellard         {
1244c896fe29Sbellard             int i;
1245c896fe29Sbellard 
1246c896fe29Sbellard             printf("%s: sorted=", def->name);
1247c896fe29Sbellard             for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
1248c896fe29Sbellard                 printf(" %d", def->sorted_args[i]);
1249c896fe29Sbellard             printf("\n");
1250c896fe29Sbellard         }
1251c896fe29Sbellard #endif
1252c896fe29Sbellard         tdefs++;
1253c896fe29Sbellard     }
1254c896fe29Sbellard 
1255c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG)
1256a9751609SRichard Henderson     i = 0;
12577d8f787dSPeter Crosthwaite     for (op = 0; op < tcg_op_defs_max; op++) {
1258f412c762SRichard Henderson         const TCGOpDef *def = &tcg_op_defs[op];
1259c1a61f6cSRichard Henderson         if (def->flags & TCG_OPF_NOT_PRESENT) {
1260c68aaa18SStefan Weil             /* Wrong entry in op definitions? */
1261f412c762SRichard Henderson             if (def->used) {
1262f412c762SRichard Henderson                 fprintf(stderr, "Invalid op definition for %s\n", def->name);
1263a9751609SRichard Henderson                 i = 1;
1264a9751609SRichard Henderson             }
1265c68aaa18SStefan Weil         } else {
1266c68aaa18SStefan Weil             /* Missing entry in op definitions? */
1267f412c762SRichard Henderson             if (!def->used) {
1268f412c762SRichard Henderson                 fprintf(stderr, "Missing op definition for %s\n", def->name);
1269a9751609SRichard Henderson                 i = 1;
1270c68aaa18SStefan Weil             }
1271c68aaa18SStefan Weil         }
1272a9751609SRichard Henderson     }
1273a9751609SRichard Henderson     if (i == 1) {
1274a9751609SRichard Henderson         tcg_abort();
1275a9751609SRichard Henderson     }
1276c68aaa18SStefan Weil #endif
1277c896fe29Sbellard }
1278c896fe29Sbellard 
12790c627cdcSRichard Henderson void tcg_op_remove(TCGContext *s, TCGOp *op)
12800c627cdcSRichard Henderson {
12810c627cdcSRichard Henderson     int next = op->next;
12820c627cdcSRichard Henderson     int prev = op->prev;
12830c627cdcSRichard Henderson 
12840c627cdcSRichard Henderson     if (next >= 0) {
12850c627cdcSRichard Henderson         s->gen_op_buf[next].prev = prev;
12860c627cdcSRichard Henderson     } else {
12870c627cdcSRichard Henderson         s->gen_last_op_idx = prev;
12880c627cdcSRichard Henderson     }
12890c627cdcSRichard Henderson     if (prev >= 0) {
12900c627cdcSRichard Henderson         s->gen_op_buf[prev].next = next;
12910c627cdcSRichard Henderson     } else {
12920c627cdcSRichard Henderson         s->gen_first_op_idx = next;
12930c627cdcSRichard Henderson     }
12940c627cdcSRichard Henderson 
129515fc7daaSRichard Henderson     memset(op, -1, sizeof(*op));
12960c627cdcSRichard Henderson 
12970c627cdcSRichard Henderson #ifdef CONFIG_PROFILER
12980c627cdcSRichard Henderson     s->del_op_count++;
12990c627cdcSRichard Henderson #endif
13000c627cdcSRichard Henderson }
13010c627cdcSRichard Henderson 
1302c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS
13039c43b68dSAurelien Jarno /* liveness analysis: end of function: all temps are dead, and globals
13049c43b68dSAurelien Jarno    should be in memory. */
13059c43b68dSAurelien Jarno static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps,
13069c43b68dSAurelien Jarno                                    uint8_t *mem_temps)
1307c896fe29Sbellard {
13089c43b68dSAurelien Jarno     memset(dead_temps, 1, s->nb_temps);
13099c43b68dSAurelien Jarno     memset(mem_temps, 1, s->nb_globals);
13109c43b68dSAurelien Jarno     memset(mem_temps + s->nb_globals, 0, s->nb_temps - s->nb_globals);
1311c896fe29Sbellard }
1312c896fe29Sbellard 
13139c43b68dSAurelien Jarno /* liveness analysis: end of basic block: all temps are dead, globals
13149c43b68dSAurelien Jarno    and local temps should be in memory. */
13159c43b68dSAurelien Jarno static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
13169c43b68dSAurelien Jarno                                  uint8_t *mem_temps)
1317641d5fbeSbellard {
1318641d5fbeSbellard     int i;
1319641d5fbeSbellard 
13209c43b68dSAurelien Jarno     memset(dead_temps, 1, s->nb_temps);
13219c43b68dSAurelien Jarno     memset(mem_temps, 1, s->nb_globals);
1322641d5fbeSbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
13239c43b68dSAurelien Jarno         mem_temps[i] = s->temps[i].temp_local;
1324641d5fbeSbellard     }
1325641d5fbeSbellard }
1326641d5fbeSbellard 
1327866cb6cbSAurelien Jarno /* Liveness analysis : update the opc_dead_args array to tell if a
1328c896fe29Sbellard    given input arguments is dead. Instructions updating dead
1329c896fe29Sbellard    temporaries are removed. */
13308fcd3692Sblueswir1 static void tcg_liveness_analysis(TCGContext *s)
1331c896fe29Sbellard {
13329c43b68dSAurelien Jarno     uint8_t *dead_temps, *mem_temps;
1333c45cb8bbSRichard Henderson     int oi, oi_prev, nb_ops;
1334c896fe29Sbellard 
1335c45cb8bbSRichard Henderson     nb_ops = s->gen_next_op_idx;
1336866cb6cbSAurelien Jarno     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1337ec7a869dSAurelien Jarno     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1338c896fe29Sbellard 
1339c896fe29Sbellard     dead_temps = tcg_malloc(s->nb_temps);
13409c43b68dSAurelien Jarno     mem_temps = tcg_malloc(s->nb_temps);
13419c43b68dSAurelien Jarno     tcg_la_func_end(s, dead_temps, mem_temps);
1342c896fe29Sbellard 
1343c45cb8bbSRichard Henderson     for (oi = s->gen_last_op_idx; oi >= 0; oi = oi_prev) {
1344c45cb8bbSRichard Henderson         int i, nb_iargs, nb_oargs;
1345c45cb8bbSRichard Henderson         TCGOpcode opc_new, opc_new2;
1346c45cb8bbSRichard Henderson         bool have_opc_new2;
1347c45cb8bbSRichard Henderson         uint16_t dead_args;
1348c45cb8bbSRichard Henderson         uint8_t sync_args;
1349c45cb8bbSRichard Henderson         TCGArg arg;
1350c45cb8bbSRichard Henderson 
1351c45cb8bbSRichard Henderson         TCGOp * const op = &s->gen_op_buf[oi];
1352c45cb8bbSRichard Henderson         TCGArg * const args = &s->gen_opparam_buf[op->args];
1353c45cb8bbSRichard Henderson         TCGOpcode opc = op->opc;
1354c45cb8bbSRichard Henderson         const TCGOpDef *def = &tcg_op_defs[opc];
1355c45cb8bbSRichard Henderson 
1356c45cb8bbSRichard Henderson         oi_prev = op->prev;
1357c45cb8bbSRichard Henderson 
1358c45cb8bbSRichard Henderson         switch (opc) {
1359c896fe29Sbellard         case INDEX_op_call:
1360c6e113f5Sbellard             {
1361c6e113f5Sbellard                 int call_flags;
1362c6e113f5Sbellard 
1363c45cb8bbSRichard Henderson                 nb_oargs = op->callo;
1364c45cb8bbSRichard Henderson                 nb_iargs = op->calli;
1365cf066674SRichard Henderson                 call_flags = args[nb_oargs + nb_iargs + 1];
1366c6e113f5Sbellard 
1367c45cb8bbSRichard Henderson                 /* pure functions can be removed if their result is unused */
136878505279SAurelien Jarno                 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
1369c6e113f5Sbellard                     for (i = 0; i < nb_oargs; i++) {
1370c6e113f5Sbellard                         arg = args[i];
13719c43b68dSAurelien Jarno                         if (!dead_temps[arg] || mem_temps[arg]) {
1372c6e113f5Sbellard                             goto do_not_remove_call;
1373c6e113f5Sbellard                         }
13749c43b68dSAurelien Jarno                     }
1375c45cb8bbSRichard Henderson                     goto do_remove;
1376c6e113f5Sbellard                 } else {
1377c6e113f5Sbellard                 do_not_remove_call:
1378c896fe29Sbellard 
1379c896fe29Sbellard                     /* output args are dead */
13806b64b624SAurelien Jarno                     dead_args = 0;
1381ec7a869dSAurelien Jarno                     sync_args = 0;
1382c896fe29Sbellard                     for (i = 0; i < nb_oargs; i++) {
1383c896fe29Sbellard                         arg = args[i];
13846b64b624SAurelien Jarno                         if (dead_temps[arg]) {
13856b64b624SAurelien Jarno                             dead_args |= (1 << i);
13866b64b624SAurelien Jarno                         }
13879c43b68dSAurelien Jarno                         if (mem_temps[arg]) {
13889c43b68dSAurelien Jarno                             sync_args |= (1 << i);
13899c43b68dSAurelien Jarno                         }
1390c896fe29Sbellard                         dead_temps[arg] = 1;
13919c43b68dSAurelien Jarno                         mem_temps[arg] = 0;
1392c896fe29Sbellard                     }
1393c896fe29Sbellard 
139478505279SAurelien Jarno                     if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
139578505279SAurelien Jarno                         /* globals should be synced to memory */
139678505279SAurelien Jarno                         memset(mem_temps, 1, s->nb_globals);
139778505279SAurelien Jarno                     }
139878505279SAurelien Jarno                     if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
139978505279SAurelien Jarno                                         TCG_CALL_NO_READ_GLOBALS))) {
14009c43b68dSAurelien Jarno                         /* globals should go back to memory */
14019c43b68dSAurelien Jarno                         memset(dead_temps, 1, s->nb_globals);
1402b9c18f56Saurel32                     }
1403c896fe29Sbellard 
1404c19f47bfSAurelien Jarno                     /* record arguments that die in this helper */
1405866cb6cbSAurelien Jarno                     for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
1406866cb6cbSAurelien Jarno                         arg = args[i];
140739cf05d3Sbellard                         if (arg != TCG_CALL_DUMMY_ARG) {
1408c896fe29Sbellard                             if (dead_temps[arg]) {
1409866cb6cbSAurelien Jarno                                 dead_args |= (1 << i);
1410c896fe29Sbellard                             }
1411c896fe29Sbellard                         }
141239cf05d3Sbellard                     }
141367cc32ebSVeres Lajos                     /* input arguments are live for preceding opcodes */
1414c19f47bfSAurelien Jarno                     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1415c19f47bfSAurelien Jarno                         arg = args[i];
1416c19f47bfSAurelien Jarno                         dead_temps[arg] = 0;
1417c19f47bfSAurelien Jarno                     }
1418c45cb8bbSRichard Henderson                     s->op_dead_args[oi] = dead_args;
1419c45cb8bbSRichard Henderson                     s->op_sync_args[oi] = sync_args;
1420c6e113f5Sbellard                 }
1421c6e113f5Sbellard             }
1422c896fe29Sbellard             break;
1423765b842aSRichard Henderson         case INDEX_op_insn_start:
1424c896fe29Sbellard             break;
14255ff9d6a4Sbellard         case INDEX_op_discard:
14265ff9d6a4Sbellard             /* mark the temporary as dead */
14275ff9d6a4Sbellard             dead_temps[args[0]] = 1;
14289c43b68dSAurelien Jarno             mem_temps[args[0]] = 0;
14295ff9d6a4Sbellard             break;
14301305c451SRichard Henderson 
14311305c451SRichard Henderson         case INDEX_op_add2_i32:
1432c45cb8bbSRichard Henderson             opc_new = INDEX_op_add_i32;
1433f1fae40cSRichard Henderson             goto do_addsub2;
14341305c451SRichard Henderson         case INDEX_op_sub2_i32:
1435c45cb8bbSRichard Henderson             opc_new = INDEX_op_sub_i32;
1436f1fae40cSRichard Henderson             goto do_addsub2;
1437f1fae40cSRichard Henderson         case INDEX_op_add2_i64:
1438c45cb8bbSRichard Henderson             opc_new = INDEX_op_add_i64;
1439f1fae40cSRichard Henderson             goto do_addsub2;
1440f1fae40cSRichard Henderson         case INDEX_op_sub2_i64:
1441c45cb8bbSRichard Henderson             opc_new = INDEX_op_sub_i64;
1442f1fae40cSRichard Henderson         do_addsub2:
14431305c451SRichard Henderson             nb_iargs = 4;
14441305c451SRichard Henderson             nb_oargs = 2;
14451305c451SRichard Henderson             /* Test if the high part of the operation is dead, but not
14461305c451SRichard Henderson                the low part.  The result can be optimized to a simple
14471305c451SRichard Henderson                add or sub.  This happens often for x86_64 guest when the
14481305c451SRichard Henderson                cpu mode is set to 32 bit.  */
14493c5645faSKirill Batuzov             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
14503c5645faSKirill Batuzov                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
14511305c451SRichard Henderson                     goto do_remove;
14521305c451SRichard Henderson                 }
1453c45cb8bbSRichard Henderson                 /* Replace the opcode and adjust the args in place,
1454c45cb8bbSRichard Henderson                    leaving 3 unused args at the end.  */
1455c45cb8bbSRichard Henderson                 op->opc = opc = opc_new;
14561305c451SRichard Henderson                 args[1] = args[2];
14571305c451SRichard Henderson                 args[2] = args[4];
14581305c451SRichard Henderson                 /* Fall through and mark the single-word operation live.  */
14591305c451SRichard Henderson                 nb_iargs = 2;
14601305c451SRichard Henderson                 nb_oargs = 1;
14611305c451SRichard Henderson             }
14621305c451SRichard Henderson             goto do_not_remove;
14631305c451SRichard Henderson 
14641414968aSRichard Henderson         case INDEX_op_mulu2_i32:
1465c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i32;
1466c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_muluh_i32;
1467c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
146803271524SRichard Henderson             goto do_mul2;
1469f1fae40cSRichard Henderson         case INDEX_op_muls2_i32:
1470c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i32;
1471c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_mulsh_i32;
1472c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
1473f1fae40cSRichard Henderson             goto do_mul2;
1474f1fae40cSRichard Henderson         case INDEX_op_mulu2_i64:
1475c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i64;
1476c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_muluh_i64;
1477c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
147803271524SRichard Henderson             goto do_mul2;
1479f1fae40cSRichard Henderson         case INDEX_op_muls2_i64:
1480c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i64;
1481c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_mulsh_i64;
1482c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
148303271524SRichard Henderson             goto do_mul2;
1484f1fae40cSRichard Henderson         do_mul2:
14851414968aSRichard Henderson             nb_iargs = 2;
14861414968aSRichard Henderson             nb_oargs = 2;
14873c5645faSKirill Batuzov             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
14883c5645faSKirill Batuzov                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
148903271524SRichard Henderson                     /* Both parts of the operation are dead.  */
14901414968aSRichard Henderson                     goto do_remove;
14911414968aSRichard Henderson                 }
149203271524SRichard Henderson                 /* The high part of the operation is dead; generate the low. */
1493c45cb8bbSRichard Henderson                 op->opc = opc = opc_new;
14941414968aSRichard Henderson                 args[1] = args[2];
14951414968aSRichard Henderson                 args[2] = args[3];
1496c45cb8bbSRichard Henderson             } else if (have_opc_new2 && dead_temps[args[0]]
149703271524SRichard Henderson                        && !mem_temps[args[0]]) {
149803271524SRichard Henderson                 /* The low part of the operation is dead; generate the high. */
1499c45cb8bbSRichard Henderson                 op->opc = opc = opc_new2;
150003271524SRichard Henderson                 args[0] = args[1];
150103271524SRichard Henderson                 args[1] = args[2];
150203271524SRichard Henderson                 args[2] = args[3];
150303271524SRichard Henderson             } else {
150403271524SRichard Henderson                 goto do_not_remove;
150503271524SRichard Henderson             }
150603271524SRichard Henderson             /* Mark the single-word operation live.  */
15071414968aSRichard Henderson             nb_oargs = 1;
15081414968aSRichard Henderson             goto do_not_remove;
15091414968aSRichard Henderson 
1510c896fe29Sbellard         default:
15111305c451SRichard Henderson             /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1512c896fe29Sbellard             nb_iargs = def->nb_iargs;
1513c896fe29Sbellard             nb_oargs = def->nb_oargs;
1514c896fe29Sbellard 
1515c896fe29Sbellard             /* Test if the operation can be removed because all
15165ff9d6a4Sbellard                its outputs are dead. We assume that nb_oargs == 0
15175ff9d6a4Sbellard                implies side effects */
15185ff9d6a4Sbellard             if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
1519c896fe29Sbellard                 for (i = 0; i < nb_oargs; i++) {
1520c896fe29Sbellard                     arg = args[i];
15219c43b68dSAurelien Jarno                     if (!dead_temps[arg] || mem_temps[arg]) {
1522c896fe29Sbellard                         goto do_not_remove;
1523c896fe29Sbellard                     }
15249c43b68dSAurelien Jarno                 }
15251305c451SRichard Henderson             do_remove:
15260c627cdcSRichard Henderson                 tcg_op_remove(s, op);
1527c896fe29Sbellard             } else {
1528c896fe29Sbellard             do_not_remove:
1529c896fe29Sbellard                 /* output args are dead */
15306b64b624SAurelien Jarno                 dead_args = 0;
1531ec7a869dSAurelien Jarno                 sync_args = 0;
1532c896fe29Sbellard                 for (i = 0; i < nb_oargs; i++) {
1533c896fe29Sbellard                     arg = args[i];
15346b64b624SAurelien Jarno                     if (dead_temps[arg]) {
15356b64b624SAurelien Jarno                         dead_args |= (1 << i);
15366b64b624SAurelien Jarno                     }
15379c43b68dSAurelien Jarno                     if (mem_temps[arg]) {
15389c43b68dSAurelien Jarno                         sync_args |= (1 << i);
15399c43b68dSAurelien Jarno                     }
1540c896fe29Sbellard                     dead_temps[arg] = 1;
15419c43b68dSAurelien Jarno                     mem_temps[arg] = 0;
1542c896fe29Sbellard                 }
1543c896fe29Sbellard 
1544c896fe29Sbellard                 /* if end of basic block, update */
1545c896fe29Sbellard                 if (def->flags & TCG_OPF_BB_END) {
15469c43b68dSAurelien Jarno                     tcg_la_bb_end(s, dead_temps, mem_temps);
15473d5c5f87SAurelien Jarno                 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
15483d5c5f87SAurelien Jarno                     /* globals should be synced to memory */
15499c43b68dSAurelien Jarno                     memset(mem_temps, 1, s->nb_globals);
1550c896fe29Sbellard                 }
1551c896fe29Sbellard 
1552c19f47bfSAurelien Jarno                 /* record arguments that die in this opcode */
1553866cb6cbSAurelien Jarno                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1554866cb6cbSAurelien Jarno                     arg = args[i];
1555c896fe29Sbellard                     if (dead_temps[arg]) {
1556866cb6cbSAurelien Jarno                         dead_args |= (1 << i);
1557c896fe29Sbellard                     }
1558c19f47bfSAurelien Jarno                 }
155967cc32ebSVeres Lajos                 /* input arguments are live for preceding opcodes */
1560c19f47bfSAurelien Jarno                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1561c19f47bfSAurelien Jarno                     arg = args[i];
1562c896fe29Sbellard                     dead_temps[arg] = 0;
1563c896fe29Sbellard                 }
1564c45cb8bbSRichard Henderson                 s->op_dead_args[oi] = dead_args;
1565c45cb8bbSRichard Henderson                 s->op_sync_args[oi] = sync_args;
1566c896fe29Sbellard             }
1567c896fe29Sbellard             break;
1568c896fe29Sbellard         }
1569c896fe29Sbellard     }
15701ff0a2c5SEvgeny Voevodin }
1571c896fe29Sbellard #else
1572c896fe29Sbellard /* dummy liveness analysis */
1573655feed5Smalc static void tcg_liveness_analysis(TCGContext *s)
1574c896fe29Sbellard {
157520157705SRichard Henderson     int nb_ops = s->gen_next_op_idx;
1576c896fe29Sbellard 
1577866cb6cbSAurelien Jarno     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1578866cb6cbSAurelien Jarno     memset(s->op_dead_args, 0, nb_ops * sizeof(uint16_t));
1579ec7a869dSAurelien Jarno     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1580ec7a869dSAurelien Jarno     memset(s->op_sync_args, 0, nb_ops * sizeof(uint8_t));
1581c896fe29Sbellard }
1582c896fe29Sbellard #endif
1583c896fe29Sbellard 
1584*8d8fdbaeSAurelien Jarno #ifdef CONFIG_DEBUG_TCG
1585c896fe29Sbellard static void dump_regs(TCGContext *s)
1586c896fe29Sbellard {
1587c896fe29Sbellard     TCGTemp *ts;
1588c896fe29Sbellard     int i;
1589c896fe29Sbellard     char buf[64];
1590c896fe29Sbellard 
1591c896fe29Sbellard     for(i = 0; i < s->nb_temps; i++) {
1592c896fe29Sbellard         ts = &s->temps[i];
1593ac56dd48Spbrook         printf("  %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
1594c896fe29Sbellard         switch(ts->val_type) {
1595c896fe29Sbellard         case TEMP_VAL_REG:
1596c896fe29Sbellard             printf("%s", tcg_target_reg_names[ts->reg]);
1597c896fe29Sbellard             break;
1598c896fe29Sbellard         case TEMP_VAL_MEM:
1599b3a62939SRichard Henderson             printf("%d(%s)", (int)ts->mem_offset,
1600b3a62939SRichard Henderson                    tcg_target_reg_names[ts->mem_base->reg]);
1601c896fe29Sbellard             break;
1602c896fe29Sbellard         case TEMP_VAL_CONST:
1603c896fe29Sbellard             printf("$0x%" TCG_PRIlx, ts->val);
1604c896fe29Sbellard             break;
1605c896fe29Sbellard         case TEMP_VAL_DEAD:
1606c896fe29Sbellard             printf("D");
1607c896fe29Sbellard             break;
1608c896fe29Sbellard         default:
1609c896fe29Sbellard             printf("???");
1610c896fe29Sbellard             break;
1611c896fe29Sbellard         }
1612c896fe29Sbellard         printf("\n");
1613c896fe29Sbellard     }
1614c896fe29Sbellard 
1615c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1616f8b2f202SRichard Henderson         if (s->reg_to_temp[i] != NULL) {
1617c896fe29Sbellard             printf("%s: %s\n",
1618c896fe29Sbellard                    tcg_target_reg_names[i],
1619f8b2f202SRichard Henderson                    tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
1620c896fe29Sbellard         }
1621c896fe29Sbellard     }
1622c896fe29Sbellard }
1623c896fe29Sbellard 
1624c896fe29Sbellard static void check_regs(TCGContext *s)
1625c896fe29Sbellard {
1626869938aeSRichard Henderson     int reg;
1627b6638662SRichard Henderson     int k;
1628c896fe29Sbellard     TCGTemp *ts;
1629c896fe29Sbellard     char buf[64];
1630c896fe29Sbellard 
1631c896fe29Sbellard     for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1632f8b2f202SRichard Henderson         ts = s->reg_to_temp[reg];
1633f8b2f202SRichard Henderson         if (ts != NULL) {
1634f8b2f202SRichard Henderson             if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
1635c896fe29Sbellard                 printf("Inconsistency for register %s:\n",
1636c896fe29Sbellard                        tcg_target_reg_names[reg]);
1637b03cce8eSbellard                 goto fail;
1638c896fe29Sbellard             }
1639c896fe29Sbellard         }
1640c896fe29Sbellard     }
1641c896fe29Sbellard     for (k = 0; k < s->nb_temps; k++) {
1642c896fe29Sbellard         ts = &s->temps[k];
1643f8b2f202SRichard Henderson         if (ts->val_type == TEMP_VAL_REG && !ts->fixed_reg
1644f8b2f202SRichard Henderson             && s->reg_to_temp[ts->reg] != ts) {
1645c896fe29Sbellard             printf("Inconsistency for temp %s:\n",
1646f8b2f202SRichard Henderson                    tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
1647b03cce8eSbellard         fail:
1648c896fe29Sbellard             printf("reg state:\n");
1649c896fe29Sbellard             dump_regs(s);
1650c896fe29Sbellard             tcg_abort();
1651c896fe29Sbellard         }
1652c896fe29Sbellard     }
1653c896fe29Sbellard }
1654c896fe29Sbellard #endif
1655c896fe29Sbellard 
1656c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp)
1657c896fe29Sbellard {
1658c896fe29Sbellard     TCGTemp *ts;
1659c896fe29Sbellard     ts = &s->temps[temp];
16609b9c37c3SRichard Henderson #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
16619b9c37c3SRichard Henderson     /* Sparc64 stack is accessed with offset of 2047 */
1662b591dc59SBlue Swirl     s->current_frame_offset = (s->current_frame_offset +
1663b591dc59SBlue Swirl                                (tcg_target_long)sizeof(tcg_target_long) - 1) &
1664b591dc59SBlue Swirl         ~(sizeof(tcg_target_long) - 1);
1665f44c9960SBlue Swirl #endif
1666b591dc59SBlue Swirl     if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
1667b591dc59SBlue Swirl         s->frame_end) {
16685ff9d6a4Sbellard         tcg_abort();
1669b591dc59SBlue Swirl     }
1670c896fe29Sbellard     ts->mem_offset = s->current_frame_offset;
1671b3a62939SRichard Henderson     ts->mem_base = s->frame_temp;
1672c896fe29Sbellard     ts->mem_allocated = 1;
1673e2c6d1b4SRichard Henderson     s->current_frame_offset += sizeof(tcg_target_long);
1674c896fe29Sbellard }
1675c896fe29Sbellard 
1676b3915dbbSRichard Henderson static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet);
1677b3915dbbSRichard Henderson 
16787f6ceedfSAurelien Jarno /* sync register 'reg' by saving it to the corresponding temporary */
1679b3915dbbSRichard Henderson static void tcg_reg_sync(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
1680c896fe29Sbellard {
1681f8b2f202SRichard Henderson     TCGTemp *ts = s->reg_to_temp[reg];
1682c896fe29Sbellard 
1683eabb7b91SAurelien Jarno     tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
16847f6ceedfSAurelien Jarno     if (!ts->mem_coherent && !ts->fixed_reg) {
16857f6ceedfSAurelien Jarno         if (!ts->mem_allocated) {
1686f8b2f202SRichard Henderson             temp_allocate_frame(s, temp_idx(s, ts));
1687b3915dbbSRichard Henderson         } else if (ts->indirect_reg) {
1688b3915dbbSRichard Henderson             tcg_regset_set_reg(allocated_regs, ts->reg);
1689b3915dbbSRichard Henderson             temp_load(s, ts->mem_base,
1690b3915dbbSRichard Henderson                       tcg_target_available_regs[TCG_TYPE_PTR],
1691b3915dbbSRichard Henderson                       allocated_regs);
16927f6ceedfSAurelien Jarno         }
1693b3a62939SRichard Henderson         tcg_out_st(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
1694c896fe29Sbellard     }
16957f6ceedfSAurelien Jarno     ts->mem_coherent = 1;
16967f6ceedfSAurelien Jarno }
16977f6ceedfSAurelien Jarno 
16987f6ceedfSAurelien Jarno /* free register 'reg' by spilling the corresponding temporary if necessary */
1699b3915dbbSRichard Henderson static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
17007f6ceedfSAurelien Jarno {
1701f8b2f202SRichard Henderson     TCGTemp *ts = s->reg_to_temp[reg];
17027f6ceedfSAurelien Jarno 
1703f8b2f202SRichard Henderson     if (ts != NULL) {
1704b3915dbbSRichard Henderson         tcg_reg_sync(s, reg, allocated_regs);
1705f8b2f202SRichard Henderson         ts->val_type = TEMP_VAL_MEM;
1706f8b2f202SRichard Henderson         s->reg_to_temp[reg] = NULL;
1707c896fe29Sbellard     }
1708c896fe29Sbellard }
1709c896fe29Sbellard 
1710c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */
1711b3915dbbSRichard Henderson static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet desired_regs,
171291478cefSRichard Henderson                             TCGRegSet allocated_regs, bool rev)
1713c896fe29Sbellard {
171491478cefSRichard Henderson     int i, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
171591478cefSRichard Henderson     const int *order;
1716b6638662SRichard Henderson     TCGReg reg;
1717c896fe29Sbellard     TCGRegSet reg_ct;
1718c896fe29Sbellard 
1719b3915dbbSRichard Henderson     tcg_regset_andnot(reg_ct, desired_regs, allocated_regs);
172091478cefSRichard Henderson     order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
1721c896fe29Sbellard 
1722c896fe29Sbellard     /* first try free registers */
172391478cefSRichard Henderson     for(i = 0; i < n; i++) {
172491478cefSRichard Henderson         reg = order[i];
1725f8b2f202SRichard Henderson         if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == NULL)
1726c896fe29Sbellard             return reg;
1727c896fe29Sbellard     }
1728c896fe29Sbellard 
1729c896fe29Sbellard     /* XXX: do better spill choice */
173091478cefSRichard Henderson     for(i = 0; i < n; i++) {
173191478cefSRichard Henderson         reg = order[i];
1732c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg)) {
1733b3915dbbSRichard Henderson             tcg_reg_free(s, reg, allocated_regs);
1734c896fe29Sbellard             return reg;
1735c896fe29Sbellard         }
1736c896fe29Sbellard     }
1737c896fe29Sbellard 
1738c896fe29Sbellard     tcg_abort();
1739c896fe29Sbellard }
1740c896fe29Sbellard 
174140ae5c62SRichard Henderson /* Make sure the temporary is in a register.  If needed, allocate the register
174240ae5c62SRichard Henderson    from DESIRED while avoiding ALLOCATED.  */
174340ae5c62SRichard Henderson static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
174440ae5c62SRichard Henderson                       TCGRegSet allocated_regs)
174540ae5c62SRichard Henderson {
174640ae5c62SRichard Henderson     TCGReg reg;
174740ae5c62SRichard Henderson 
174840ae5c62SRichard Henderson     switch (ts->val_type) {
174940ae5c62SRichard Henderson     case TEMP_VAL_REG:
175040ae5c62SRichard Henderson         return;
175140ae5c62SRichard Henderson     case TEMP_VAL_CONST:
175291478cefSRichard Henderson         reg = tcg_reg_alloc(s, desired_regs, allocated_regs, ts->indirect_base);
175340ae5c62SRichard Henderson         tcg_out_movi(s, ts->type, reg, ts->val);
175440ae5c62SRichard Henderson         ts->mem_coherent = 0;
175540ae5c62SRichard Henderson         break;
175640ae5c62SRichard Henderson     case TEMP_VAL_MEM:
175791478cefSRichard Henderson         reg = tcg_reg_alloc(s, desired_regs, allocated_regs, ts->indirect_base);
1758b3915dbbSRichard Henderson         if (ts->indirect_reg) {
1759b3915dbbSRichard Henderson             tcg_regset_set_reg(allocated_regs, reg);
1760b3915dbbSRichard Henderson             temp_load(s, ts->mem_base,
1761b3915dbbSRichard Henderson                       tcg_target_available_regs[TCG_TYPE_PTR],
1762b3915dbbSRichard Henderson                       allocated_regs);
1763b3915dbbSRichard Henderson         }
176440ae5c62SRichard Henderson         tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
176540ae5c62SRichard Henderson         ts->mem_coherent = 1;
176640ae5c62SRichard Henderson         break;
176740ae5c62SRichard Henderson     case TEMP_VAL_DEAD:
176840ae5c62SRichard Henderson     default:
176940ae5c62SRichard Henderson         tcg_abort();
177040ae5c62SRichard Henderson     }
177140ae5c62SRichard Henderson     ts->reg = reg;
177240ae5c62SRichard Henderson     ts->val_type = TEMP_VAL_REG;
177340ae5c62SRichard Henderson     s->reg_to_temp[reg] = ts;
177440ae5c62SRichard Henderson }
177540ae5c62SRichard Henderson 
1776639368ddSAurelien Jarno /* mark a temporary as dead. */
1777f8bf00f1SRichard Henderson static inline void temp_dead(TCGContext *s, TCGTemp *ts)
1778639368ddSAurelien Jarno {
1779f8bf00f1SRichard Henderson     if (ts->fixed_reg) {
1780f8bf00f1SRichard Henderson         return;
1781f8bf00f1SRichard Henderson     }
1782639368ddSAurelien Jarno     if (ts->val_type == TEMP_VAL_REG) {
1783f8b2f202SRichard Henderson         s->reg_to_temp[ts->reg] = NULL;
1784639368ddSAurelien Jarno     }
1785f8bf00f1SRichard Henderson     ts->val_type = (temp_idx(s, ts) < s->nb_globals || ts->temp_local
1786f8bf00f1SRichard Henderson                     ? TEMP_VAL_MEM : TEMP_VAL_DEAD);
1787639368ddSAurelien Jarno }
1788639368ddSAurelien Jarno 
17891ad80729SAurelien Jarno /* sync a temporary to memory. 'allocated_regs' is used in case a
1790e8996ee0Sbellard    temporary registers needs to be allocated to store a constant. */
179112b9b11aSRichard Henderson static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
1792c896fe29Sbellard {
179312b9b11aSRichard Henderson     if (ts->fixed_reg) {
179412b9b11aSRichard Henderson         return;
179512b9b11aSRichard Henderson     }
1796e8996ee0Sbellard     switch (ts->val_type) {
17971ad80729SAurelien Jarno     case TEMP_VAL_CONST:
179840ae5c62SRichard Henderson         temp_load(s, ts, tcg_target_available_regs[ts->type], allocated_regs);
17991ad80729SAurelien Jarno         /* fallthrough */
1800e8996ee0Sbellard     case TEMP_VAL_REG:
1801b3915dbbSRichard Henderson         tcg_reg_sync(s, ts->reg, allocated_regs);
1802e8996ee0Sbellard         break;
1803e8996ee0Sbellard     case TEMP_VAL_DEAD:
1804e8996ee0Sbellard     case TEMP_VAL_MEM:
1805e8996ee0Sbellard         break;
1806e8996ee0Sbellard     default:
1807e8996ee0Sbellard         tcg_abort();
1808c896fe29Sbellard     }
1809c896fe29Sbellard }
1810641d5fbeSbellard 
18111ad80729SAurelien Jarno /* save a temporary to memory. 'allocated_regs' is used in case a
18121ad80729SAurelien Jarno    temporary registers needs to be allocated to store a constant. */
1813b13eb728SRichard Henderson static inline void temp_save(TCGContext *s, TCGTemp *ts,
1814b13eb728SRichard Henderson                              TCGRegSet allocated_regs)
18151ad80729SAurelien Jarno {
18162c0366f0SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
1817b3915dbbSRichard Henderson     /* ??? Liveness does not yet incorporate indirect bases.  */
1818b3915dbbSRichard Henderson     if (!ts->indirect_base) {
18192c0366f0SAurelien Jarno         /* The liveness analysis already ensures that globals are back
1820eabb7b91SAurelien Jarno            in memory. Keep an tcg_debug_assert for safety. */
1821f8bf00f1SRichard Henderson         tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || ts->fixed_reg);
1822b3915dbbSRichard Henderson         return;
1823b3915dbbSRichard Henderson     }
1824b3915dbbSRichard Henderson #endif
182512b9b11aSRichard Henderson     temp_sync(s, ts, allocated_regs);
1826f8bf00f1SRichard Henderson     temp_dead(s, ts);
18271ad80729SAurelien Jarno }
18281ad80729SAurelien Jarno 
18299814dd27SDong Xu Wang /* save globals to their canonical location and assume they can be
1830641d5fbeSbellard    modified be the following code. 'allocated_regs' is used in case a
1831641d5fbeSbellard    temporary registers needs to be allocated to store a constant. */
1832641d5fbeSbellard static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
1833641d5fbeSbellard {
1834641d5fbeSbellard     int i;
1835641d5fbeSbellard 
1836641d5fbeSbellard     for (i = 0; i < s->nb_globals; i++) {
1837b13eb728SRichard Henderson         temp_save(s, &s->temps[i], allocated_regs);
1838641d5fbeSbellard     }
1839e5097dc8Sbellard }
1840e5097dc8Sbellard 
18413d5c5f87SAurelien Jarno /* sync globals to their canonical location and assume they can be
18423d5c5f87SAurelien Jarno    read by the following code. 'allocated_regs' is used in case a
18433d5c5f87SAurelien Jarno    temporary registers needs to be allocated to store a constant. */
18443d5c5f87SAurelien Jarno static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
18453d5c5f87SAurelien Jarno {
18463d5c5f87SAurelien Jarno     int i;
18473d5c5f87SAurelien Jarno 
18483d5c5f87SAurelien Jarno     for (i = 0; i < s->nb_globals; i++) {
184912b9b11aSRichard Henderson         TCGTemp *ts = &s->temps[i];
18503d5c5f87SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
1851b3915dbbSRichard Henderson         /* ??? Liveness does not yet incorporate indirect bases.  */
1852b3915dbbSRichard Henderson         if (!ts->indirect_base) {
185312b9b11aSRichard Henderson             tcg_debug_assert(ts->val_type != TEMP_VAL_REG
185412b9b11aSRichard Henderson                              || ts->fixed_reg
185512b9b11aSRichard Henderson                              || ts->mem_coherent);
1856b3915dbbSRichard Henderson             continue;
1857b3915dbbSRichard Henderson         }
18583d5c5f87SAurelien Jarno #endif
1859b3915dbbSRichard Henderson         temp_sync(s, ts, allocated_regs);
18603d5c5f87SAurelien Jarno     }
18613d5c5f87SAurelien Jarno }
18623d5c5f87SAurelien Jarno 
1863e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and
1864e8996ee0Sbellard    all globals are stored at their canonical location. */
1865e8996ee0Sbellard static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
1866e5097dc8Sbellard {
1867e5097dc8Sbellard     int i;
1868e5097dc8Sbellard 
1869c896fe29Sbellard     for (i = s->nb_globals; i < s->nb_temps; i++) {
1870b13eb728SRichard Henderson         TCGTemp *ts = &s->temps[i];
1871641d5fbeSbellard         if (ts->temp_local) {
1872b13eb728SRichard Henderson             temp_save(s, ts, allocated_regs);
1873641d5fbeSbellard         } else {
18742c0366f0SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
1875b3915dbbSRichard Henderson             /* ??? Liveness does not yet incorporate indirect bases.  */
1876b3915dbbSRichard Henderson             if (!ts->indirect_base) {
18772c0366f0SAurelien Jarno                 /* The liveness analysis already ensures that temps are dead.
1878eabb7b91SAurelien Jarno                    Keep an tcg_debug_assert for safety. */
1879eabb7b91SAurelien Jarno                 tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
1880b3915dbbSRichard Henderson                 continue;
1881b3915dbbSRichard Henderson             }
18822c0366f0SAurelien Jarno #endif
1883b3915dbbSRichard Henderson             temp_dead(s, ts);
1884c896fe29Sbellard         }
1885641d5fbeSbellard     }
1886e8996ee0Sbellard 
1887e8996ee0Sbellard     save_globals(s, allocated_regs);
1888c896fe29Sbellard }
1889c896fe29Sbellard 
1890866cb6cbSAurelien Jarno #define IS_DEAD_ARG(n) ((dead_args >> (n)) & 1)
1891ec7a869dSAurelien Jarno #define NEED_SYNC_ARG(n) ((sync_args >> (n)) & 1)
1892c896fe29Sbellard 
1893ec7a869dSAurelien Jarno static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
1894ec7a869dSAurelien Jarno                                uint16_t dead_args, uint8_t sync_args)
1895e8996ee0Sbellard {
1896e8996ee0Sbellard     TCGTemp *ots;
1897e8996ee0Sbellard     tcg_target_ulong val;
1898e8996ee0Sbellard 
1899e8996ee0Sbellard     ots = &s->temps[args[0]];
1900e8996ee0Sbellard     val = args[1];
1901e8996ee0Sbellard 
1902e8996ee0Sbellard     if (ots->fixed_reg) {
1903e8996ee0Sbellard         /* for fixed registers, we do not do any constant
1904e8996ee0Sbellard            propagation */
1905e8996ee0Sbellard         tcg_out_movi(s, ots->type, ots->reg, val);
1906e8996ee0Sbellard     } else {
19071235fc06Sths         /* The movi is not explicitly generated here */
1908f8b2f202SRichard Henderson         if (ots->val_type == TEMP_VAL_REG) {
1909f8b2f202SRichard Henderson             s->reg_to_temp[ots->reg] = NULL;
1910f8b2f202SRichard Henderson         }
1911e8996ee0Sbellard         ots->val_type = TEMP_VAL_CONST;
1912e8996ee0Sbellard         ots->val = val;
1913e8996ee0Sbellard     }
1914ec7a869dSAurelien Jarno     if (NEED_SYNC_ARG(0)) {
191512b9b11aSRichard Henderson         temp_sync(s, ots, s->reserved_regs);
1916ec7a869dSAurelien Jarno     }
19174c4e1ab2SAurelien Jarno     if (IS_DEAD_ARG(0)) {
1918f8bf00f1SRichard Henderson         temp_dead(s, ots);
19194c4e1ab2SAurelien Jarno     }
1920e8996ee0Sbellard }
1921e8996ee0Sbellard 
1922c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1923ec7a869dSAurelien Jarno                               const TCGArg *args, uint16_t dead_args,
1924ec7a869dSAurelien Jarno                               uint8_t sync_args)
1925c896fe29Sbellard {
1926c29c1d7eSAurelien Jarno     TCGRegSet allocated_regs;
1927c896fe29Sbellard     TCGTemp *ts, *ots;
1928450445d5SRichard Henderson     TCGType otype, itype;
1929c896fe29Sbellard 
1930c29c1d7eSAurelien Jarno     tcg_regset_set(allocated_regs, s->reserved_regs);
1931c896fe29Sbellard     ots = &s->temps[args[0]];
1932c896fe29Sbellard     ts = &s->temps[args[1]];
1933450445d5SRichard Henderson 
1934450445d5SRichard Henderson     /* Note that otype != itype for no-op truncation.  */
1935450445d5SRichard Henderson     otype = ots->type;
1936450445d5SRichard Henderson     itype = ts->type;
1937c896fe29Sbellard 
1938c29c1d7eSAurelien Jarno     /* If the source value is not in a register, and we're going to be
1939c29c1d7eSAurelien Jarno        forced to have it in a register in order to perform the copy,
1940c29c1d7eSAurelien Jarno        then copy the SOURCE value into its own register first.  That way
1941c29c1d7eSAurelien Jarno        we don't have to reload SOURCE the next time it is used. */
1942c29c1d7eSAurelien Jarno     if (((NEED_SYNC_ARG(0) || ots->fixed_reg) && ts->val_type != TEMP_VAL_REG)
1943c29c1d7eSAurelien Jarno         || ts->val_type == TEMP_VAL_MEM) {
194440ae5c62SRichard Henderson         temp_load(s, ts, tcg_target_available_regs[itype], allocated_regs);
1945c29c1d7eSAurelien Jarno     }
1946c29c1d7eSAurelien Jarno 
1947c29c1d7eSAurelien Jarno     if (IS_DEAD_ARG(0) && !ots->fixed_reg) {
1948c29c1d7eSAurelien Jarno         /* mov to a non-saved dead register makes no sense (even with
1949c29c1d7eSAurelien Jarno            liveness analysis disabled). */
1950eabb7b91SAurelien Jarno         tcg_debug_assert(NEED_SYNC_ARG(0));
1951c29c1d7eSAurelien Jarno         /* The code above should have moved the temp to a register. */
1952eabb7b91SAurelien Jarno         tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
1953c29c1d7eSAurelien Jarno         if (!ots->mem_allocated) {
1954c29c1d7eSAurelien Jarno             temp_allocate_frame(s, args[0]);
1955c29c1d7eSAurelien Jarno         }
1956b3915dbbSRichard Henderson         if (ots->indirect_reg) {
1957b3915dbbSRichard Henderson             tcg_regset_set_reg(allocated_regs, ts->reg);
1958b3915dbbSRichard Henderson             temp_load(s, ots->mem_base,
1959b3915dbbSRichard Henderson                       tcg_target_available_regs[TCG_TYPE_PTR],
1960b3915dbbSRichard Henderson                       allocated_regs);
1961b3915dbbSRichard Henderson         }
1962b3a62939SRichard Henderson         tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset);
1963c29c1d7eSAurelien Jarno         if (IS_DEAD_ARG(1)) {
1964f8bf00f1SRichard Henderson             temp_dead(s, ts);
1965c29c1d7eSAurelien Jarno         }
1966f8bf00f1SRichard Henderson         temp_dead(s, ots);
1967c29c1d7eSAurelien Jarno     } else if (ts->val_type == TEMP_VAL_CONST) {
1968e8996ee0Sbellard         /* propagate constant */
1969c29c1d7eSAurelien Jarno         if (ots->val_type == TEMP_VAL_REG) {
1970f8b2f202SRichard Henderson             s->reg_to_temp[ots->reg] = NULL;
1971c29c1d7eSAurelien Jarno         }
1972e8996ee0Sbellard         ots->val_type = TEMP_VAL_CONST;
1973e8996ee0Sbellard         ots->val = ts->val;
19747df69deaSAurelien Jarno         if (IS_DEAD_ARG(1)) {
1975f8bf00f1SRichard Henderson             temp_dead(s, ts);
19767df69deaSAurelien Jarno         }
1977e8996ee0Sbellard     } else {
1978c29c1d7eSAurelien Jarno         /* The code in the first if block should have moved the
1979c29c1d7eSAurelien Jarno            temp to a register. */
1980eabb7b91SAurelien Jarno         tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
1981c29c1d7eSAurelien Jarno         if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
1982c29c1d7eSAurelien Jarno             /* the mov can be suppressed */
1983c29c1d7eSAurelien Jarno             if (ots->val_type == TEMP_VAL_REG) {
1984f8b2f202SRichard Henderson                 s->reg_to_temp[ots->reg] = NULL;
1985c896fe29Sbellard             }
1986c29c1d7eSAurelien Jarno             ots->reg = ts->reg;
1987f8bf00f1SRichard Henderson             temp_dead(s, ts);
1988c29c1d7eSAurelien Jarno         } else {
1989c29c1d7eSAurelien Jarno             if (ots->val_type != TEMP_VAL_REG) {
1990c29c1d7eSAurelien Jarno                 /* When allocating a new register, make sure to not spill the
1991c29c1d7eSAurelien Jarno                    input one. */
1992c29c1d7eSAurelien Jarno                 tcg_regset_set_reg(allocated_regs, ts->reg);
1993450445d5SRichard Henderson                 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
199491478cefSRichard Henderson                                          allocated_regs, ots->indirect_base);
1995c29c1d7eSAurelien Jarno             }
1996450445d5SRichard Henderson             tcg_out_mov(s, otype, ots->reg, ts->reg);
1997c29c1d7eSAurelien Jarno         }
1998c896fe29Sbellard         ots->val_type = TEMP_VAL_REG;
1999c896fe29Sbellard         ots->mem_coherent = 0;
2000f8b2f202SRichard Henderson         s->reg_to_temp[ots->reg] = ots;
2001ec7a869dSAurelien Jarno         if (NEED_SYNC_ARG(0)) {
2002b3915dbbSRichard Henderson             tcg_reg_sync(s, ots->reg, allocated_regs);
2003c29c1d7eSAurelien Jarno         }
2004ec7a869dSAurelien Jarno     }
2005c896fe29Sbellard }
2006c896fe29Sbellard 
2007c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s,
2008a9751609SRichard Henderson                              const TCGOpDef *def, TCGOpcode opc,
2009ec7a869dSAurelien Jarno                              const TCGArg *args, uint16_t dead_args,
2010ec7a869dSAurelien Jarno                              uint8_t sync_args)
2011c896fe29Sbellard {
2012c896fe29Sbellard     TCGRegSet allocated_regs;
2013b6638662SRichard Henderson     int i, k, nb_iargs, nb_oargs;
2014b6638662SRichard Henderson     TCGReg reg;
2015c896fe29Sbellard     TCGArg arg;
2016c896fe29Sbellard     const TCGArgConstraint *arg_ct;
2017c896fe29Sbellard     TCGTemp *ts;
2018c896fe29Sbellard     TCGArg new_args[TCG_MAX_OP_ARGS];
2019c896fe29Sbellard     int const_args[TCG_MAX_OP_ARGS];
2020c896fe29Sbellard 
2021c896fe29Sbellard     nb_oargs = def->nb_oargs;
2022c896fe29Sbellard     nb_iargs = def->nb_iargs;
2023c896fe29Sbellard 
2024c896fe29Sbellard     /* copy constants */
2025c896fe29Sbellard     memcpy(new_args + nb_oargs + nb_iargs,
2026c896fe29Sbellard            args + nb_oargs + nb_iargs,
2027c896fe29Sbellard            sizeof(TCGArg) * def->nb_cargs);
2028c896fe29Sbellard 
2029c896fe29Sbellard     /* satisfy input constraints */
2030c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
2031c896fe29Sbellard     for(k = 0; k < nb_iargs; k++) {
2032c896fe29Sbellard         i = def->sorted_args[nb_oargs + k];
2033c896fe29Sbellard         arg = args[i];
2034c896fe29Sbellard         arg_ct = &def->args_ct[i];
2035c896fe29Sbellard         ts = &s->temps[arg];
203640ae5c62SRichard Henderson 
203740ae5c62SRichard Henderson         if (ts->val_type == TEMP_VAL_CONST
203840ae5c62SRichard Henderson             && tcg_target_const_match(ts->val, ts->type, arg_ct)) {
2039c896fe29Sbellard             /* constant is OK for instruction */
2040c896fe29Sbellard             const_args[i] = 1;
2041c896fe29Sbellard             new_args[i] = ts->val;
2042c896fe29Sbellard             goto iarg_end;
2043c896fe29Sbellard         }
204440ae5c62SRichard Henderson 
204540ae5c62SRichard Henderson         temp_load(s, ts, arg_ct->u.regs, allocated_regs);
204640ae5c62SRichard Henderson 
20475ff9d6a4Sbellard         if (arg_ct->ct & TCG_CT_IALIAS) {
20485ff9d6a4Sbellard             if (ts->fixed_reg) {
20495ff9d6a4Sbellard                 /* if fixed register, we must allocate a new register
20505ff9d6a4Sbellard                    if the alias is not the same register */
20515ff9d6a4Sbellard                 if (arg != args[arg_ct->alias_index])
20525ff9d6a4Sbellard                     goto allocate_in_reg;
20535ff9d6a4Sbellard             } else {
2054c896fe29Sbellard                 /* if the input is aliased to an output and if it is
2055c896fe29Sbellard                    not dead after the instruction, we must allocate
2056c896fe29Sbellard                    a new register and move it */
2057866cb6cbSAurelien Jarno                 if (!IS_DEAD_ARG(i)) {
2058c896fe29Sbellard                     goto allocate_in_reg;
2059c896fe29Sbellard                 }
20607e1df267SAurelien Jarno                 /* check if the current register has already been allocated
20617e1df267SAurelien Jarno                    for another input aliased to an output */
20627e1df267SAurelien Jarno                 int k2, i2;
20637e1df267SAurelien Jarno                 for (k2 = 0 ; k2 < k ; k2++) {
20647e1df267SAurelien Jarno                     i2 = def->sorted_args[nb_oargs + k2];
20657e1df267SAurelien Jarno                     if ((def->args_ct[i2].ct & TCG_CT_IALIAS) &&
20667e1df267SAurelien Jarno                         (new_args[i2] == ts->reg)) {
20677e1df267SAurelien Jarno                         goto allocate_in_reg;
20687e1df267SAurelien Jarno                     }
20697e1df267SAurelien Jarno                 }
20705ff9d6a4Sbellard             }
2071866cb6cbSAurelien Jarno         }
2072c896fe29Sbellard         reg = ts->reg;
2073c896fe29Sbellard         if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
2074c896fe29Sbellard             /* nothing to do : the constraint is satisfied */
2075c896fe29Sbellard         } else {
2076c896fe29Sbellard         allocate_in_reg:
2077c896fe29Sbellard             /* allocate a new register matching the constraint
2078c896fe29Sbellard                and move the temporary register into it */
207991478cefSRichard Henderson             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs,
208091478cefSRichard Henderson                                 ts->indirect_base);
20813b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, reg, ts->reg);
2082c896fe29Sbellard         }
2083c896fe29Sbellard         new_args[i] = reg;
2084c896fe29Sbellard         const_args[i] = 0;
2085c896fe29Sbellard         tcg_regset_set_reg(allocated_regs, reg);
2086c896fe29Sbellard     iarg_end: ;
2087c896fe29Sbellard     }
2088c896fe29Sbellard 
2089c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
2090866cb6cbSAurelien Jarno     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2091866cb6cbSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2092f8bf00f1SRichard Henderson             temp_dead(s, &s->temps[args[i]]);
2093c896fe29Sbellard         }
2094c896fe29Sbellard     }
2095c896fe29Sbellard 
2096a52ad07eSAurelien Jarno     if (def->flags & TCG_OPF_BB_END) {
2097a52ad07eSAurelien Jarno         tcg_reg_alloc_bb_end(s, allocated_regs);
2098a52ad07eSAurelien Jarno     } else {
2099c896fe29Sbellard         if (def->flags & TCG_OPF_CALL_CLOBBER) {
2100b03cce8eSbellard             /* XXX: permit generic clobber register list ? */
2101c8074023SRichard Henderson             for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
2102c8074023SRichard Henderson                 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
2103b3915dbbSRichard Henderson                     tcg_reg_free(s, i, allocated_regs);
2104c896fe29Sbellard                 }
2105c896fe29Sbellard             }
21063d5c5f87SAurelien Jarno         }
21073d5c5f87SAurelien Jarno         if (def->flags & TCG_OPF_SIDE_EFFECTS) {
21083d5c5f87SAurelien Jarno             /* sync globals if the op has side effects and might trigger
21093d5c5f87SAurelien Jarno                an exception. */
21103d5c5f87SAurelien Jarno             sync_globals(s, allocated_regs);
2111c896fe29Sbellard         }
2112c896fe29Sbellard 
2113c896fe29Sbellard         /* satisfy the output constraints */
2114c896fe29Sbellard         tcg_regset_set(allocated_regs, s->reserved_regs);
2115c896fe29Sbellard         for(k = 0; k < nb_oargs; k++) {
2116c896fe29Sbellard             i = def->sorted_args[k];
2117c896fe29Sbellard             arg = args[i];
2118c896fe29Sbellard             arg_ct = &def->args_ct[i];
2119c896fe29Sbellard             ts = &s->temps[arg];
2120c896fe29Sbellard             if (arg_ct->ct & TCG_CT_ALIAS) {
21215ff9d6a4Sbellard                 reg = new_args[arg_ct->alias_index];
2122c896fe29Sbellard             } else {
2123c896fe29Sbellard                 /* if fixed register, we try to use it */
2124c896fe29Sbellard                 reg = ts->reg;
2125c896fe29Sbellard                 if (ts->fixed_reg &&
2126c896fe29Sbellard                     tcg_regset_test_reg(arg_ct->u.regs, reg)) {
2127c896fe29Sbellard                     goto oarg_end;
2128c896fe29Sbellard                 }
212991478cefSRichard Henderson                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs,
213091478cefSRichard Henderson                                     ts->indirect_base);
2131c896fe29Sbellard             }
2132c896fe29Sbellard             tcg_regset_set_reg(allocated_regs, reg);
2133c896fe29Sbellard             /* if a fixed register is used, then a move will be done afterwards */
2134c896fe29Sbellard             if (!ts->fixed_reg) {
2135639368ddSAurelien Jarno                 if (ts->val_type == TEMP_VAL_REG) {
2136f8b2f202SRichard Henderson                     s->reg_to_temp[ts->reg] = NULL;
2137639368ddSAurelien Jarno                 }
2138c896fe29Sbellard                 ts->val_type = TEMP_VAL_REG;
2139c896fe29Sbellard                 ts->reg = reg;
2140c896fe29Sbellard                 /* temp value is modified, so the value kept in memory is
2141c896fe29Sbellard                    potentially not the same */
2142c896fe29Sbellard                 ts->mem_coherent = 0;
2143f8b2f202SRichard Henderson                 s->reg_to_temp[reg] = ts;
2144c896fe29Sbellard             }
2145c896fe29Sbellard         oarg_end:
2146c896fe29Sbellard             new_args[i] = reg;
2147c896fe29Sbellard         }
2148e8996ee0Sbellard     }
2149c896fe29Sbellard 
2150c896fe29Sbellard     /* emit instruction */
2151c896fe29Sbellard     tcg_out_op(s, opc, new_args, const_args);
2152c896fe29Sbellard 
2153c896fe29Sbellard     /* move the outputs in the correct register if needed */
2154c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
2155c896fe29Sbellard         ts = &s->temps[args[i]];
2156c896fe29Sbellard         reg = new_args[i];
2157c896fe29Sbellard         if (ts->fixed_reg && ts->reg != reg) {
21583b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, ts->reg, reg);
2159c896fe29Sbellard         }
2160ec7a869dSAurelien Jarno         if (NEED_SYNC_ARG(i)) {
2161b3915dbbSRichard Henderson             tcg_reg_sync(s, reg, allocated_regs);
2162ec7a869dSAurelien Jarno         }
2163ec7a869dSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2164f8bf00f1SRichard Henderson             temp_dead(s, ts);
2165ec7a869dSAurelien Jarno         }
2166c896fe29Sbellard     }
2167c896fe29Sbellard }
2168c896fe29Sbellard 
2169b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP
2170b03cce8eSbellard #define STACK_DIR(x) (-(x))
2171b03cce8eSbellard #else
2172b03cce8eSbellard #define STACK_DIR(x) (x)
2173b03cce8eSbellard #endif
2174b03cce8eSbellard 
2175c45cb8bbSRichard Henderson static void tcg_reg_alloc_call(TCGContext *s, int nb_oargs, int nb_iargs,
2176c45cb8bbSRichard Henderson                                const TCGArg * const args, uint16_t dead_args,
2177c45cb8bbSRichard Henderson                                uint8_t sync_args)
2178c896fe29Sbellard {
2179b6638662SRichard Henderson     int flags, nb_regs, i;
2180b6638662SRichard Henderson     TCGReg reg;
2181cf066674SRichard Henderson     TCGArg arg;
2182c896fe29Sbellard     TCGTemp *ts;
2183d3452f1fSRichard Henderson     intptr_t stack_offset;
2184d3452f1fSRichard Henderson     size_t call_stack_size;
2185cf066674SRichard Henderson     tcg_insn_unit *func_addr;
2186cf066674SRichard Henderson     int allocate_args;
2187c896fe29Sbellard     TCGRegSet allocated_regs;
2188c896fe29Sbellard 
2189cf066674SRichard Henderson     func_addr = (tcg_insn_unit *)(intptr_t)args[nb_oargs + nb_iargs];
2190cf066674SRichard Henderson     flags = args[nb_oargs + nb_iargs + 1];
2191c896fe29Sbellard 
21926e17d0c5SStefan Weil     nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2193c45cb8bbSRichard Henderson     if (nb_regs > nb_iargs) {
2194c45cb8bbSRichard Henderson         nb_regs = nb_iargs;
2195cf066674SRichard Henderson     }
2196c896fe29Sbellard 
2197c896fe29Sbellard     /* assign stack slots first */
2198c45cb8bbSRichard Henderson     call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
2199c896fe29Sbellard     call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
2200c896fe29Sbellard         ~(TCG_TARGET_STACK_ALIGN - 1);
2201b03cce8eSbellard     allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
2202b03cce8eSbellard     if (allocate_args) {
2203345649c0SBlue Swirl         /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
2204345649c0SBlue Swirl            preallocate call stack */
2205345649c0SBlue Swirl         tcg_abort();
2206b03cce8eSbellard     }
220739cf05d3Sbellard 
220839cf05d3Sbellard     stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
2209c45cb8bbSRichard Henderson     for(i = nb_regs; i < nb_iargs; i++) {
2210c896fe29Sbellard         arg = args[nb_oargs + i];
221139cf05d3Sbellard #ifdef TCG_TARGET_STACK_GROWSUP
221239cf05d3Sbellard         stack_offset -= sizeof(tcg_target_long);
221339cf05d3Sbellard #endif
221439cf05d3Sbellard         if (arg != TCG_CALL_DUMMY_ARG) {
2215c896fe29Sbellard             ts = &s->temps[arg];
221640ae5c62SRichard Henderson             temp_load(s, ts, tcg_target_available_regs[ts->type],
221740ae5c62SRichard Henderson                       s->reserved_regs);
2218e4d5434cSblueswir1             tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
221939cf05d3Sbellard         }
222039cf05d3Sbellard #ifndef TCG_TARGET_STACK_GROWSUP
222139cf05d3Sbellard         stack_offset += sizeof(tcg_target_long);
222239cf05d3Sbellard #endif
2223c896fe29Sbellard     }
2224c896fe29Sbellard 
2225c896fe29Sbellard     /* assign input registers */
2226c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
2227c896fe29Sbellard     for(i = 0; i < nb_regs; i++) {
2228c896fe29Sbellard         arg = args[nb_oargs + i];
222939cf05d3Sbellard         if (arg != TCG_CALL_DUMMY_ARG) {
2230c896fe29Sbellard             ts = &s->temps[arg];
2231c896fe29Sbellard             reg = tcg_target_call_iarg_regs[i];
2232b3915dbbSRichard Henderson             tcg_reg_free(s, reg, allocated_regs);
223340ae5c62SRichard Henderson 
2234c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
2235c896fe29Sbellard                 if (ts->reg != reg) {
22363b6dac34SRichard Henderson                     tcg_out_mov(s, ts->type, reg, ts->reg);
2237c896fe29Sbellard                 }
2238c896fe29Sbellard             } else {
223940ae5c62SRichard Henderson                 TCGRegSet arg_set;
224040ae5c62SRichard Henderson 
224140ae5c62SRichard Henderson                 tcg_regset_clear(arg_set);
224240ae5c62SRichard Henderson                 tcg_regset_set_reg(arg_set, reg);
224340ae5c62SRichard Henderson                 temp_load(s, ts, arg_set, allocated_regs);
2244c896fe29Sbellard             }
224540ae5c62SRichard Henderson 
2246c896fe29Sbellard             tcg_regset_set_reg(allocated_regs, reg);
2247c896fe29Sbellard         }
224839cf05d3Sbellard     }
2249c896fe29Sbellard 
2250c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
2251866cb6cbSAurelien Jarno     for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2252866cb6cbSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2253f8bf00f1SRichard Henderson             temp_dead(s, &s->temps[args[i]]);
2254c896fe29Sbellard         }
2255c896fe29Sbellard     }
2256c896fe29Sbellard 
2257c896fe29Sbellard     /* clobber call registers */
2258c8074023SRichard Henderson     for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
2259c8074023SRichard Henderson         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
2260b3915dbbSRichard Henderson             tcg_reg_free(s, i, allocated_regs);
2261c896fe29Sbellard         }
2262c896fe29Sbellard     }
2263c896fe29Sbellard 
226478505279SAurelien Jarno     /* Save globals if they might be written by the helper, sync them if
226578505279SAurelien Jarno        they might be read. */
226678505279SAurelien Jarno     if (flags & TCG_CALL_NO_READ_GLOBALS) {
226778505279SAurelien Jarno         /* Nothing to do */
226878505279SAurelien Jarno     } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
226978505279SAurelien Jarno         sync_globals(s, allocated_regs);
227078505279SAurelien Jarno     } else {
2271e8996ee0Sbellard         save_globals(s, allocated_regs);
2272b9c18f56Saurel32     }
2273c896fe29Sbellard 
2274cf066674SRichard Henderson     tcg_out_call(s, func_addr);
2275c896fe29Sbellard 
2276c896fe29Sbellard     /* assign output registers and emit moves if needed */
2277c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
2278c896fe29Sbellard         arg = args[i];
2279c896fe29Sbellard         ts = &s->temps[arg];
2280c896fe29Sbellard         reg = tcg_target_call_oarg_regs[i];
2281eabb7b91SAurelien Jarno         tcg_debug_assert(s->reg_to_temp[reg] == NULL);
228234b1a49cSRichard Henderson 
2283c896fe29Sbellard         if (ts->fixed_reg) {
2284c896fe29Sbellard             if (ts->reg != reg) {
22853b6dac34SRichard Henderson                 tcg_out_mov(s, ts->type, ts->reg, reg);
2286c896fe29Sbellard             }
2287c896fe29Sbellard         } else {
2288639368ddSAurelien Jarno             if (ts->val_type == TEMP_VAL_REG) {
2289f8b2f202SRichard Henderson                 s->reg_to_temp[ts->reg] = NULL;
2290639368ddSAurelien Jarno             }
2291c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
2292c896fe29Sbellard             ts->reg = reg;
2293c896fe29Sbellard             ts->mem_coherent = 0;
2294f8b2f202SRichard Henderson             s->reg_to_temp[reg] = ts;
2295ec7a869dSAurelien Jarno             if (NEED_SYNC_ARG(i)) {
2296b3915dbbSRichard Henderson                 tcg_reg_sync(s, reg, allocated_regs);
2297ec7a869dSAurelien Jarno             }
2298ec7a869dSAurelien Jarno             if (IS_DEAD_ARG(i)) {
2299f8bf00f1SRichard Henderson                 temp_dead(s, ts);
2300c896fe29Sbellard             }
2301c896fe29Sbellard         }
23028c11ad25SAurelien Jarno     }
2303c896fe29Sbellard }
2304c896fe29Sbellard 
2305c896fe29Sbellard #ifdef CONFIG_PROFILER
2306c896fe29Sbellard 
230754604f74Saurel32 static int64_t tcg_table_op_count[NB_OPS];
2308c896fe29Sbellard 
2309246ae24dSMax Filippov void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
2310c896fe29Sbellard {
2311c896fe29Sbellard     int i;
2312d70724ceSzhanghailiang 
231315fc7daaSRichard Henderson     for (i = 0; i < NB_OPS; i++) {
2314246ae24dSMax Filippov         cpu_fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name,
2315246ae24dSMax Filippov                     tcg_table_op_count[i]);
2316c896fe29Sbellard     }
2317c896fe29Sbellard }
2318246ae24dSMax Filippov #else
2319246ae24dSMax Filippov void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
2320246ae24dSMax Filippov {
2321246ae24dSMax Filippov     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2322246ae24dSMax Filippov }
2323c896fe29Sbellard #endif
2324c896fe29Sbellard 
2325c896fe29Sbellard 
23265bd2ec3dSAlex Bennée int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
2327c896fe29Sbellard {
2328fca8a500SRichard Henderson     int i, oi, oi_next, num_insns;
2329c896fe29Sbellard 
233004fe6400SRichard Henderson #ifdef CONFIG_PROFILER
233104fe6400SRichard Henderson     {
233204fe6400SRichard Henderson         int n;
233304fe6400SRichard Henderson 
233404fe6400SRichard Henderson         n = s->gen_last_op_idx + 1;
233504fe6400SRichard Henderson         s->op_count += n;
233604fe6400SRichard Henderson         if (n > s->op_count_max) {
233704fe6400SRichard Henderson             s->op_count_max = n;
233804fe6400SRichard Henderson         }
233904fe6400SRichard Henderson 
234004fe6400SRichard Henderson         n = s->nb_temps;
234104fe6400SRichard Henderson         s->temp_count += n;
234204fe6400SRichard Henderson         if (n > s->temp_count_max) {
234304fe6400SRichard Henderson             s->temp_count_max = n;
234404fe6400SRichard Henderson         }
234504fe6400SRichard Henderson     }
234604fe6400SRichard Henderson #endif
234704fe6400SRichard Henderson 
2348c896fe29Sbellard #ifdef DEBUG_DISAS
2349d977e1c2SAlex Bennée     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
2350d977e1c2SAlex Bennée                  && qemu_log_in_addr_range(tb->pc))) {
235193fcfe39Saliguori         qemu_log("OP:\n");
2352eeacee4dSBlue Swirl         tcg_dump_ops(s);
235393fcfe39Saliguori         qemu_log("\n");
2354c896fe29Sbellard     }
2355c896fe29Sbellard #endif
2356c896fe29Sbellard 
2357c5cc28ffSAurelien Jarno #ifdef CONFIG_PROFILER
2358c5cc28ffSAurelien Jarno     s->opt_time -= profile_getclock();
2359c5cc28ffSAurelien Jarno #endif
2360c5cc28ffSAurelien Jarno 
23618f2e8c07SKirill Batuzov #ifdef USE_TCG_OPTIMIZATIONS
2362c45cb8bbSRichard Henderson     tcg_optimize(s);
23638f2e8c07SKirill Batuzov #endif
23648f2e8c07SKirill Batuzov 
2365a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2366c5cc28ffSAurelien Jarno     s->opt_time += profile_getclock();
2367a23a9ec6Sbellard     s->la_time -= profile_getclock();
2368a23a9ec6Sbellard #endif
2369c5cc28ffSAurelien Jarno 
2370c896fe29Sbellard     tcg_liveness_analysis(s);
2371c5cc28ffSAurelien Jarno 
2372a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2373a23a9ec6Sbellard     s->la_time += profile_getclock();
2374a23a9ec6Sbellard #endif
2375c896fe29Sbellard 
2376c896fe29Sbellard #ifdef DEBUG_DISAS
2377d977e1c2SAlex Bennée     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
2378d977e1c2SAlex Bennée                  && qemu_log_in_addr_range(tb->pc))) {
2379c5cc28ffSAurelien Jarno         qemu_log("OP after optimization and liveness analysis:\n");
2380eeacee4dSBlue Swirl         tcg_dump_ops(s);
238193fcfe39Saliguori         qemu_log("\n");
2382c896fe29Sbellard     }
2383c896fe29Sbellard #endif
2384c896fe29Sbellard 
2385c896fe29Sbellard     tcg_reg_alloc_start(s);
2386c896fe29Sbellard 
23875bd2ec3dSAlex Bennée     s->code_buf = tb->tc_ptr;
23885bd2ec3dSAlex Bennée     s->code_ptr = tb->tc_ptr;
2389c896fe29Sbellard 
23909ecefc84SRichard Henderson     tcg_out_tb_init(s);
23919ecefc84SRichard Henderson 
2392fca8a500SRichard Henderson     num_insns = -1;
2393c45cb8bbSRichard Henderson     for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
2394c45cb8bbSRichard Henderson         TCGOp * const op = &s->gen_op_buf[oi];
2395c45cb8bbSRichard Henderson         TCGArg * const args = &s->gen_opparam_buf[op->args];
2396c45cb8bbSRichard Henderson         TCGOpcode opc = op->opc;
2397c45cb8bbSRichard Henderson         const TCGOpDef *def = &tcg_op_defs[opc];
2398c45cb8bbSRichard Henderson         uint16_t dead_args = s->op_dead_args[oi];
2399c45cb8bbSRichard Henderson         uint8_t sync_args = s->op_sync_args[oi];
2400b3db8758Sblueswir1 
2401c45cb8bbSRichard Henderson         oi_next = op->next;
2402c896fe29Sbellard #ifdef CONFIG_PROFILER
240354604f74Saurel32         tcg_table_op_count[opc]++;
2404c896fe29Sbellard #endif
2405c45cb8bbSRichard Henderson 
2406c896fe29Sbellard         switch (opc) {
2407c896fe29Sbellard         case INDEX_op_mov_i32:
2408c896fe29Sbellard         case INDEX_op_mov_i64:
2409c45cb8bbSRichard Henderson             tcg_reg_alloc_mov(s, def, args, dead_args, sync_args);
2410c896fe29Sbellard             break;
2411e8996ee0Sbellard         case INDEX_op_movi_i32:
2412e8996ee0Sbellard         case INDEX_op_movi_i64:
2413c45cb8bbSRichard Henderson             tcg_reg_alloc_movi(s, args, dead_args, sync_args);
2414e8996ee0Sbellard             break;
2415765b842aSRichard Henderson         case INDEX_op_insn_start:
2416fca8a500SRichard Henderson             if (num_insns >= 0) {
2417fca8a500SRichard Henderson                 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
2418fca8a500SRichard Henderson             }
2419fca8a500SRichard Henderson             num_insns++;
2420bad729e2SRichard Henderson             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
2421bad729e2SRichard Henderson                 target_ulong a;
2422bad729e2SRichard Henderson #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2423bad729e2SRichard Henderson                 a = ((target_ulong)args[i * 2 + 1] << 32) | args[i * 2];
2424bad729e2SRichard Henderson #else
2425bad729e2SRichard Henderson                 a = args[i];
2426bad729e2SRichard Henderson #endif
2427fca8a500SRichard Henderson                 s->gen_insn_data[num_insns][i] = a;
2428bad729e2SRichard Henderson             }
2429c896fe29Sbellard             break;
24305ff9d6a4Sbellard         case INDEX_op_discard:
2431f8bf00f1SRichard Henderson             temp_dead(s, &s->temps[args[0]]);
24325ff9d6a4Sbellard             break;
2433c896fe29Sbellard         case INDEX_op_set_label:
2434e8996ee0Sbellard             tcg_reg_alloc_bb_end(s, s->reserved_regs);
2435bec16311SRichard Henderson             tcg_out_label(s, arg_label(args[0]), s->code_ptr);
2436c896fe29Sbellard             break;
2437c896fe29Sbellard         case INDEX_op_call:
2438c45cb8bbSRichard Henderson             tcg_reg_alloc_call(s, op->callo, op->calli, args,
2439c45cb8bbSRichard Henderson                                dead_args, sync_args);
2440c45cb8bbSRichard Henderson             break;
2441c896fe29Sbellard         default:
244225c4d9ccSRichard Henderson             /* Sanity check that we've not introduced any unhandled opcodes. */
244325c4d9ccSRichard Henderson             if (def->flags & TCG_OPF_NOT_PRESENT) {
244425c4d9ccSRichard Henderson                 tcg_abort();
244525c4d9ccSRichard Henderson             }
2446c896fe29Sbellard             /* Note: in order to speed up the code, it would be much
2447c896fe29Sbellard                faster to have specialized register allocator functions for
2448c896fe29Sbellard                some common argument patterns */
2449c45cb8bbSRichard Henderson             tcg_reg_alloc_op(s, def, opc, args, dead_args, sync_args);
2450c896fe29Sbellard             break;
2451c896fe29Sbellard         }
2452*8d8fdbaeSAurelien Jarno #ifdef CONFIG_DEBUG_TCG
2453c896fe29Sbellard         check_regs(s);
2454c896fe29Sbellard #endif
2455b125f9dcSRichard Henderson         /* Test for (pending) buffer overflow.  The assumption is that any
2456b125f9dcSRichard Henderson            one operation beginning below the high water mark cannot overrun
2457b125f9dcSRichard Henderson            the buffer completely.  Thus we can test for overflow after
2458b125f9dcSRichard Henderson            generating code without having to check during generation.  */
2459644da9b3SJohn Clarke         if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
2460b125f9dcSRichard Henderson             return -1;
2461b125f9dcSRichard Henderson         }
2462c896fe29Sbellard     }
2463fca8a500SRichard Henderson     tcg_debug_assert(num_insns >= 0);
2464fca8a500SRichard Henderson     s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
2465c45cb8bbSRichard Henderson 
2466b76f0d8cSYeongkyoon Lee     /* Generate TB finalization at the end of block */
246723dceda6SRichard Henderson     if (!tcg_out_tb_finalize(s)) {
246823dceda6SRichard Henderson         return -1;
246923dceda6SRichard Henderson     }
2470c896fe29Sbellard 
2471c896fe29Sbellard     /* flush instruction cache */
24721813e175SRichard Henderson     flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
24732aeabc08SStefan Weil 
24741813e175SRichard Henderson     return tcg_current_code_size(s);
2475c896fe29Sbellard }
2476c896fe29Sbellard 
2477a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2478405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2479a23a9ec6Sbellard {
2480a23a9ec6Sbellard     TCGContext *s = &tcg_ctx;
2481fca8a500SRichard Henderson     int64_t tb_count = s->tb_count;
2482fca8a500SRichard Henderson     int64_t tb_div_count = tb_count ? tb_count : 1;
2483fca8a500SRichard Henderson     int64_t tot = s->interm_time + s->code_time;
2484a23a9ec6Sbellard 
2485a23a9ec6Sbellard     cpu_fprintf(f, "JIT cycles          %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2486a23a9ec6Sbellard                 tot, tot / 2.4e9);
2487a23a9ec6Sbellard     cpu_fprintf(f, "translated TBs      %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
2488fca8a500SRichard Henderson                 tb_count, s->tb_count1 - tb_count,
2489fca8a500SRichard Henderson                 (double)(s->tb_count1 - s->tb_count)
2490fca8a500SRichard Henderson                 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
2491a23a9ec6Sbellard     cpu_fprintf(f, "avg ops/TB          %0.1f max=%d\n",
2492fca8a500SRichard Henderson                 (double)s->op_count / tb_div_count, s->op_count_max);
2493a23a9ec6Sbellard     cpu_fprintf(f, "deleted ops/TB      %0.2f\n",
2494fca8a500SRichard Henderson                 (double)s->del_op_count / tb_div_count);
2495a23a9ec6Sbellard     cpu_fprintf(f, "avg temps/TB        %0.2f max=%d\n",
2496fca8a500SRichard Henderson                 (double)s->temp_count / tb_div_count, s->temp_count_max);
2497fca8a500SRichard Henderson     cpu_fprintf(f, "avg host code/TB    %0.1f\n",
2498fca8a500SRichard Henderson                 (double)s->code_out_len / tb_div_count);
2499fca8a500SRichard Henderson     cpu_fprintf(f, "avg search data/TB  %0.1f\n",
2500fca8a500SRichard Henderson                 (double)s->search_out_len / tb_div_count);
2501a23a9ec6Sbellard 
2502a23a9ec6Sbellard     cpu_fprintf(f, "cycles/op           %0.1f\n",
2503a23a9ec6Sbellard                 s->op_count ? (double)tot / s->op_count : 0);
2504a23a9ec6Sbellard     cpu_fprintf(f, "cycles/in byte      %0.1f\n",
2505a23a9ec6Sbellard                 s->code_in_len ? (double)tot / s->code_in_len : 0);
2506a23a9ec6Sbellard     cpu_fprintf(f, "cycles/out byte     %0.1f\n",
2507a23a9ec6Sbellard                 s->code_out_len ? (double)tot / s->code_out_len : 0);
2508fca8a500SRichard Henderson     cpu_fprintf(f, "cycles/search byte     %0.1f\n",
2509fca8a500SRichard Henderson                 s->search_out_len ? (double)tot / s->search_out_len : 0);
2510fca8a500SRichard Henderson     if (tot == 0) {
2511a23a9ec6Sbellard         tot = 1;
2512fca8a500SRichard Henderson     }
2513a23a9ec6Sbellard     cpu_fprintf(f, "  gen_interm time   %0.1f%%\n",
2514a23a9ec6Sbellard                 (double)s->interm_time / tot * 100.0);
2515a23a9ec6Sbellard     cpu_fprintf(f, "  gen_code time     %0.1f%%\n",
2516a23a9ec6Sbellard                 (double)s->code_time / tot * 100.0);
2517c5cc28ffSAurelien Jarno     cpu_fprintf(f, "optim./code time    %0.1f%%\n",
2518c5cc28ffSAurelien Jarno                 (double)s->opt_time / (s->code_time ? s->code_time : 1)
2519c5cc28ffSAurelien Jarno                 * 100.0);
2520a23a9ec6Sbellard     cpu_fprintf(f, "liveness/code time  %0.1f%%\n",
2521a23a9ec6Sbellard                 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
2522a23a9ec6Sbellard     cpu_fprintf(f, "cpu_restore count   %" PRId64 "\n",
2523a23a9ec6Sbellard                 s->restore_count);
2524a23a9ec6Sbellard     cpu_fprintf(f, "  avg cycles        %0.1f\n",
2525a23a9ec6Sbellard                 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
2526a23a9ec6Sbellard }
2527a23a9ec6Sbellard #else
2528405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2529a23a9ec6Sbellard {
253024bf7b3aSbellard     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2531a23a9ec6Sbellard }
2532a23a9ec6Sbellard #endif
2533813da627SRichard Henderson 
2534813da627SRichard Henderson #ifdef ELF_HOST_MACHINE
25355872bbf2SRichard Henderson /* In order to use this feature, the backend needs to do three things:
25365872bbf2SRichard Henderson 
25375872bbf2SRichard Henderson    (1) Define ELF_HOST_MACHINE to indicate both what value to
25385872bbf2SRichard Henderson        put into the ELF image and to indicate support for the feature.
25395872bbf2SRichard Henderson 
25405872bbf2SRichard Henderson    (2) Define tcg_register_jit.  This should create a buffer containing
25415872bbf2SRichard Henderson        the contents of a .debug_frame section that describes the post-
25425872bbf2SRichard Henderson        prologue unwind info for the tcg machine.
25435872bbf2SRichard Henderson 
25445872bbf2SRichard Henderson    (3) Call tcg_register_jit_int, with the constructed .debug_frame.
25455872bbf2SRichard Henderson */
2546813da627SRichard Henderson 
2547813da627SRichard Henderson /* Begin GDB interface.  THE FOLLOWING MUST MATCH GDB DOCS.  */
2548813da627SRichard Henderson typedef enum {
2549813da627SRichard Henderson     JIT_NOACTION = 0,
2550813da627SRichard Henderson     JIT_REGISTER_FN,
2551813da627SRichard Henderson     JIT_UNREGISTER_FN
2552813da627SRichard Henderson } jit_actions_t;
2553813da627SRichard Henderson 
2554813da627SRichard Henderson struct jit_code_entry {
2555813da627SRichard Henderson     struct jit_code_entry *next_entry;
2556813da627SRichard Henderson     struct jit_code_entry *prev_entry;
2557813da627SRichard Henderson     const void *symfile_addr;
2558813da627SRichard Henderson     uint64_t symfile_size;
2559813da627SRichard Henderson };
2560813da627SRichard Henderson 
2561813da627SRichard Henderson struct jit_descriptor {
2562813da627SRichard Henderson     uint32_t version;
2563813da627SRichard Henderson     uint32_t action_flag;
2564813da627SRichard Henderson     struct jit_code_entry *relevant_entry;
2565813da627SRichard Henderson     struct jit_code_entry *first_entry;
2566813da627SRichard Henderson };
2567813da627SRichard Henderson 
2568813da627SRichard Henderson void __jit_debug_register_code(void) __attribute__((noinline));
2569813da627SRichard Henderson void __jit_debug_register_code(void)
2570813da627SRichard Henderson {
2571813da627SRichard Henderson     asm("");
2572813da627SRichard Henderson }
2573813da627SRichard Henderson 
2574813da627SRichard Henderson /* Must statically initialize the version, because GDB may check
2575813da627SRichard Henderson    the version before we can set it.  */
2576813da627SRichard Henderson struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
2577813da627SRichard Henderson 
2578813da627SRichard Henderson /* End GDB interface.  */
2579813da627SRichard Henderson 
2580813da627SRichard Henderson static int find_string(const char *strtab, const char *str)
2581813da627SRichard Henderson {
2582813da627SRichard Henderson     const char *p = strtab + 1;
2583813da627SRichard Henderson 
2584813da627SRichard Henderson     while (1) {
2585813da627SRichard Henderson         if (strcmp(p, str) == 0) {
2586813da627SRichard Henderson             return p - strtab;
2587813da627SRichard Henderson         }
2588813da627SRichard Henderson         p += strlen(p) + 1;
2589813da627SRichard Henderson     }
2590813da627SRichard Henderson }
2591813da627SRichard Henderson 
25925872bbf2SRichard Henderson static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
25932c90784aSRichard Henderson                                  const void *debug_frame,
25942c90784aSRichard Henderson                                  size_t debug_frame_size)
2595813da627SRichard Henderson {
25965872bbf2SRichard Henderson     struct __attribute__((packed)) DebugInfo {
25975872bbf2SRichard Henderson         uint32_t  len;
25985872bbf2SRichard Henderson         uint16_t  version;
25995872bbf2SRichard Henderson         uint32_t  abbrev;
26005872bbf2SRichard Henderson         uint8_t   ptr_size;
26015872bbf2SRichard Henderson         uint8_t   cu_die;
26025872bbf2SRichard Henderson         uint16_t  cu_lang;
26035872bbf2SRichard Henderson         uintptr_t cu_low_pc;
26045872bbf2SRichard Henderson         uintptr_t cu_high_pc;
26055872bbf2SRichard Henderson         uint8_t   fn_die;
26065872bbf2SRichard Henderson         char      fn_name[16];
26075872bbf2SRichard Henderson         uintptr_t fn_low_pc;
26085872bbf2SRichard Henderson         uintptr_t fn_high_pc;
26095872bbf2SRichard Henderson         uint8_t   cu_eoc;
26105872bbf2SRichard Henderson     };
2611813da627SRichard Henderson 
2612813da627SRichard Henderson     struct ElfImage {
2613813da627SRichard Henderson         ElfW(Ehdr) ehdr;
2614813da627SRichard Henderson         ElfW(Phdr) phdr;
26155872bbf2SRichard Henderson         ElfW(Shdr) shdr[7];
26165872bbf2SRichard Henderson         ElfW(Sym)  sym[2];
26175872bbf2SRichard Henderson         struct DebugInfo di;
26185872bbf2SRichard Henderson         uint8_t    da[24];
26195872bbf2SRichard Henderson         char       str[80];
26205872bbf2SRichard Henderson     };
26215872bbf2SRichard Henderson 
26225872bbf2SRichard Henderson     struct ElfImage *img;
26235872bbf2SRichard Henderson 
26245872bbf2SRichard Henderson     static const struct ElfImage img_template = {
26255872bbf2SRichard Henderson         .ehdr = {
26265872bbf2SRichard Henderson             .e_ident[EI_MAG0] = ELFMAG0,
26275872bbf2SRichard Henderson             .e_ident[EI_MAG1] = ELFMAG1,
26285872bbf2SRichard Henderson             .e_ident[EI_MAG2] = ELFMAG2,
26295872bbf2SRichard Henderson             .e_ident[EI_MAG3] = ELFMAG3,
26305872bbf2SRichard Henderson             .e_ident[EI_CLASS] = ELF_CLASS,
26315872bbf2SRichard Henderson             .e_ident[EI_DATA] = ELF_DATA,
26325872bbf2SRichard Henderson             .e_ident[EI_VERSION] = EV_CURRENT,
26335872bbf2SRichard Henderson             .e_type = ET_EXEC,
26345872bbf2SRichard Henderson             .e_machine = ELF_HOST_MACHINE,
26355872bbf2SRichard Henderson             .e_version = EV_CURRENT,
26365872bbf2SRichard Henderson             .e_phoff = offsetof(struct ElfImage, phdr),
26375872bbf2SRichard Henderson             .e_shoff = offsetof(struct ElfImage, shdr),
26385872bbf2SRichard Henderson             .e_ehsize = sizeof(ElfW(Shdr)),
26395872bbf2SRichard Henderson             .e_phentsize = sizeof(ElfW(Phdr)),
26405872bbf2SRichard Henderson             .e_phnum = 1,
26415872bbf2SRichard Henderson             .e_shentsize = sizeof(ElfW(Shdr)),
26425872bbf2SRichard Henderson             .e_shnum = ARRAY_SIZE(img->shdr),
26435872bbf2SRichard Henderson             .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
2644abbb3eaeSRichard Henderson #ifdef ELF_HOST_FLAGS
2645abbb3eaeSRichard Henderson             .e_flags = ELF_HOST_FLAGS,
2646abbb3eaeSRichard Henderson #endif
2647abbb3eaeSRichard Henderson #ifdef ELF_OSABI
2648abbb3eaeSRichard Henderson             .e_ident[EI_OSABI] = ELF_OSABI,
2649abbb3eaeSRichard Henderson #endif
26505872bbf2SRichard Henderson         },
26515872bbf2SRichard Henderson         .phdr = {
26525872bbf2SRichard Henderson             .p_type = PT_LOAD,
26535872bbf2SRichard Henderson             .p_flags = PF_X,
26545872bbf2SRichard Henderson         },
26555872bbf2SRichard Henderson         .shdr = {
26565872bbf2SRichard Henderson             [0] = { .sh_type = SHT_NULL },
26575872bbf2SRichard Henderson             /* Trick: The contents of code_gen_buffer are not present in
26585872bbf2SRichard Henderson                this fake ELF file; that got allocated elsewhere.  Therefore
26595872bbf2SRichard Henderson                we mark .text as SHT_NOBITS (similar to .bss) so that readers
26605872bbf2SRichard Henderson                will not look for contents.  We can record any address.  */
26615872bbf2SRichard Henderson             [1] = { /* .text */
26625872bbf2SRichard Henderson                 .sh_type = SHT_NOBITS,
26635872bbf2SRichard Henderson                 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
26645872bbf2SRichard Henderson             },
26655872bbf2SRichard Henderson             [2] = { /* .debug_info */
26665872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
26675872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, di),
26685872bbf2SRichard Henderson                 .sh_size = sizeof(struct DebugInfo),
26695872bbf2SRichard Henderson             },
26705872bbf2SRichard Henderson             [3] = { /* .debug_abbrev */
26715872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
26725872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, da),
26735872bbf2SRichard Henderson                 .sh_size = sizeof(img->da),
26745872bbf2SRichard Henderson             },
26755872bbf2SRichard Henderson             [4] = { /* .debug_frame */
26765872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
26775872bbf2SRichard Henderson                 .sh_offset = sizeof(struct ElfImage),
26785872bbf2SRichard Henderson             },
26795872bbf2SRichard Henderson             [5] = { /* .symtab */
26805872bbf2SRichard Henderson                 .sh_type = SHT_SYMTAB,
26815872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, sym),
26825872bbf2SRichard Henderson                 .sh_size = sizeof(img->sym),
26835872bbf2SRichard Henderson                 .sh_info = 1,
26845872bbf2SRichard Henderson                 .sh_link = ARRAY_SIZE(img->shdr) - 1,
26855872bbf2SRichard Henderson                 .sh_entsize = sizeof(ElfW(Sym)),
26865872bbf2SRichard Henderson             },
26875872bbf2SRichard Henderson             [6] = { /* .strtab */
26885872bbf2SRichard Henderson                 .sh_type = SHT_STRTAB,
26895872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, str),
26905872bbf2SRichard Henderson                 .sh_size = sizeof(img->str),
26915872bbf2SRichard Henderson             }
26925872bbf2SRichard Henderson         },
26935872bbf2SRichard Henderson         .sym = {
26945872bbf2SRichard Henderson             [1] = { /* code_gen_buffer */
26955872bbf2SRichard Henderson                 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
26965872bbf2SRichard Henderson                 .st_shndx = 1,
26975872bbf2SRichard Henderson             }
26985872bbf2SRichard Henderson         },
26995872bbf2SRichard Henderson         .di = {
27005872bbf2SRichard Henderson             .len = sizeof(struct DebugInfo) - 4,
27015872bbf2SRichard Henderson             .version = 2,
27025872bbf2SRichard Henderson             .ptr_size = sizeof(void *),
27035872bbf2SRichard Henderson             .cu_die = 1,
27045872bbf2SRichard Henderson             .cu_lang = 0x8001,  /* DW_LANG_Mips_Assembler */
27055872bbf2SRichard Henderson             .fn_die = 2,
27065872bbf2SRichard Henderson             .fn_name = "code_gen_buffer"
27075872bbf2SRichard Henderson         },
27085872bbf2SRichard Henderson         .da = {
27095872bbf2SRichard Henderson             1,          /* abbrev number (the cu) */
27105872bbf2SRichard Henderson             0x11, 1,    /* DW_TAG_compile_unit, has children */
27115872bbf2SRichard Henderson             0x13, 0x5,  /* DW_AT_language, DW_FORM_data2 */
27125872bbf2SRichard Henderson             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
27135872bbf2SRichard Henderson             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
27145872bbf2SRichard Henderson             0, 0,       /* end of abbrev */
27155872bbf2SRichard Henderson             2,          /* abbrev number (the fn) */
27165872bbf2SRichard Henderson             0x2e, 0,    /* DW_TAG_subprogram, no children */
27175872bbf2SRichard Henderson             0x3, 0x8,   /* DW_AT_name, DW_FORM_string */
27185872bbf2SRichard Henderson             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
27195872bbf2SRichard Henderson             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
27205872bbf2SRichard Henderson             0, 0,       /* end of abbrev */
27215872bbf2SRichard Henderson             0           /* no more abbrev */
27225872bbf2SRichard Henderson         },
27235872bbf2SRichard Henderson         .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
27245872bbf2SRichard Henderson                ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
2725813da627SRichard Henderson     };
2726813da627SRichard Henderson 
2727813da627SRichard Henderson     /* We only need a single jit entry; statically allocate it.  */
2728813da627SRichard Henderson     static struct jit_code_entry one_entry;
2729813da627SRichard Henderson 
27305872bbf2SRichard Henderson     uintptr_t buf = (uintptr_t)buf_ptr;
2731813da627SRichard Henderson     size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
27322c90784aSRichard Henderson     DebugFrameHeader *dfh;
2733813da627SRichard Henderson 
27345872bbf2SRichard Henderson     img = g_malloc(img_size);
27355872bbf2SRichard Henderson     *img = img_template;
2736813da627SRichard Henderson 
27375872bbf2SRichard Henderson     img->phdr.p_vaddr = buf;
27385872bbf2SRichard Henderson     img->phdr.p_paddr = buf;
27395872bbf2SRichard Henderson     img->phdr.p_memsz = buf_size;
2740813da627SRichard Henderson 
27415872bbf2SRichard Henderson     img->shdr[1].sh_name = find_string(img->str, ".text");
27425872bbf2SRichard Henderson     img->shdr[1].sh_addr = buf;
27435872bbf2SRichard Henderson     img->shdr[1].sh_size = buf_size;
2744813da627SRichard Henderson 
27455872bbf2SRichard Henderson     img->shdr[2].sh_name = find_string(img->str, ".debug_info");
27465872bbf2SRichard Henderson     img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
27475872bbf2SRichard Henderson 
27485872bbf2SRichard Henderson     img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
27495872bbf2SRichard Henderson     img->shdr[4].sh_size = debug_frame_size;
27505872bbf2SRichard Henderson 
27515872bbf2SRichard Henderson     img->shdr[5].sh_name = find_string(img->str, ".symtab");
27525872bbf2SRichard Henderson     img->shdr[6].sh_name = find_string(img->str, ".strtab");
27535872bbf2SRichard Henderson 
27545872bbf2SRichard Henderson     img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
27555872bbf2SRichard Henderson     img->sym[1].st_value = buf;
27565872bbf2SRichard Henderson     img->sym[1].st_size = buf_size;
27575872bbf2SRichard Henderson 
27585872bbf2SRichard Henderson     img->di.cu_low_pc = buf;
275945aba097SRichard Henderson     img->di.cu_high_pc = buf + buf_size;
27605872bbf2SRichard Henderson     img->di.fn_low_pc = buf;
276145aba097SRichard Henderson     img->di.fn_high_pc = buf + buf_size;
2762813da627SRichard Henderson 
27632c90784aSRichard Henderson     dfh = (DebugFrameHeader *)(img + 1);
27642c90784aSRichard Henderson     memcpy(dfh, debug_frame, debug_frame_size);
27652c90784aSRichard Henderson     dfh->fde.func_start = buf;
27662c90784aSRichard Henderson     dfh->fde.func_len = buf_size;
27672c90784aSRichard Henderson 
2768813da627SRichard Henderson #ifdef DEBUG_JIT
2769813da627SRichard Henderson     /* Enable this block to be able to debug the ELF image file creation.
2770813da627SRichard Henderson        One can use readelf, objdump, or other inspection utilities.  */
2771813da627SRichard Henderson     {
2772813da627SRichard Henderson         FILE *f = fopen("/tmp/qemu.jit", "w+b");
2773813da627SRichard Henderson         if (f) {
27745872bbf2SRichard Henderson             if (fwrite(img, img_size, 1, f) != img_size) {
2775813da627SRichard Henderson                 /* Avoid stupid unused return value warning for fwrite.  */
2776813da627SRichard Henderson             }
2777813da627SRichard Henderson             fclose(f);
2778813da627SRichard Henderson         }
2779813da627SRichard Henderson     }
2780813da627SRichard Henderson #endif
2781813da627SRichard Henderson 
2782813da627SRichard Henderson     one_entry.symfile_addr = img;
2783813da627SRichard Henderson     one_entry.symfile_size = img_size;
2784813da627SRichard Henderson 
2785813da627SRichard Henderson     __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
2786813da627SRichard Henderson     __jit_debug_descriptor.relevant_entry = &one_entry;
2787813da627SRichard Henderson     __jit_debug_descriptor.first_entry = &one_entry;
2788813da627SRichard Henderson     __jit_debug_register_code();
2789813da627SRichard Henderson }
2790813da627SRichard Henderson #else
27915872bbf2SRichard Henderson /* No support for the feature.  Provide the entry point expected by exec.c,
27925872bbf2SRichard Henderson    and implement the internal function we declared earlier.  */
2793813da627SRichard Henderson 
2794813da627SRichard Henderson static void tcg_register_jit_int(void *buf, size_t size,
27952c90784aSRichard Henderson                                  const void *debug_frame,
27962c90784aSRichard Henderson                                  size_t debug_frame_size)
2797813da627SRichard Henderson {
2798813da627SRichard Henderson }
2799813da627SRichard Henderson 
2800813da627SRichard Henderson void tcg_register_jit(void *buf, size_t buf_size)
2801813da627SRichard Henderson {
2802813da627SRichard Henderson }
2803813da627SRichard Henderson #endif /* ELF_HOST_MACHINE */
2804