xref: /openbmc/qemu/tcg/tcg.c (revision 15fc7daa)
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 
29cca82982Saurel32 #include "config.h"
30cca82982Saurel32 
31813da627SRichard Henderson /* Define to jump the ELF file used to communicate with GDB.  */
32813da627SRichard Henderson #undef DEBUG_JIT
33813da627SRichard Henderson 
34a6c6f76cSBlue Swirl #if !defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG)
35cca82982Saurel32 /* define it to suppress various consistency checks (faster) */
36cca82982Saurel32 #define NDEBUG
37cca82982Saurel32 #endif
38cca82982Saurel32 
39ca10f867Saurel32 #include "qemu-common.h"
401de7afc9SPaolo Bonzini #include "qemu/host-utils.h"
411de7afc9SPaolo Bonzini #include "qemu/timer.h"
42c896fe29Sbellard 
43c5d3c498SStefan Weil /* Note: the long term plan is to reduce the dependencies on the QEMU
44c896fe29Sbellard    CPU definitions. Currently they are used for qemu_ld/st
45c896fe29Sbellard    instructions */
46c896fe29Sbellard #define NO_CPU_IO_DEFS
47c896fe29Sbellard #include "cpu.h"
48c896fe29Sbellard 
49c896fe29Sbellard #include "tcg-op.h"
50813da627SRichard Henderson 
51edee2579SRichard Henderson #if UINTPTR_MAX == UINT32_MAX
52813da627SRichard Henderson # define ELF_CLASS  ELFCLASS32
53edee2579SRichard Henderson #else
54edee2579SRichard Henderson # define ELF_CLASS  ELFCLASS64
55813da627SRichard Henderson #endif
56813da627SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
57813da627SRichard Henderson # define ELF_DATA   ELFDATA2MSB
58813da627SRichard Henderson #else
59813da627SRichard Henderson # define ELF_DATA   ELFDATA2LSB
60813da627SRichard Henderson #endif
61813da627SRichard Henderson 
62c896fe29Sbellard #include "elf.h"
63c896fe29Sbellard 
64c0ad3001SStefan Weil /* Forward declarations for functions declared in tcg-target.c and used here. */
65e4d58b41SRichard Henderson static void tcg_target_init(TCGContext *s);
66e4d58b41SRichard Henderson static void tcg_target_qemu_prologue(TCGContext *s);
671813e175SRichard Henderson static void patch_reloc(tcg_insn_unit *code_ptr, int type,
682ba7fae2SRichard Henderson                         intptr_t value, intptr_t addend);
69c896fe29Sbellard 
70497a22ebSRichard Henderson /* The CIE and FDE header definitions will be common to all hosts.  */
71497a22ebSRichard Henderson typedef struct {
72497a22ebSRichard Henderson     uint32_t len __attribute__((aligned((sizeof(void *)))));
73497a22ebSRichard Henderson     uint32_t id;
74497a22ebSRichard Henderson     uint8_t version;
75497a22ebSRichard Henderson     char augmentation[1];
76497a22ebSRichard Henderson     uint8_t code_align;
77497a22ebSRichard Henderson     uint8_t data_align;
78497a22ebSRichard Henderson     uint8_t return_column;
79497a22ebSRichard Henderson } DebugFrameCIE;
80497a22ebSRichard Henderson 
81497a22ebSRichard Henderson typedef struct QEMU_PACKED {
82497a22ebSRichard Henderson     uint32_t len __attribute__((aligned((sizeof(void *)))));
83497a22ebSRichard Henderson     uint32_t cie_offset;
84edee2579SRichard Henderson     uintptr_t func_start;
85edee2579SRichard Henderson     uintptr_t func_len;
86497a22ebSRichard Henderson } DebugFrameFDEHeader;
87497a22ebSRichard Henderson 
882c90784aSRichard Henderson typedef struct QEMU_PACKED {
892c90784aSRichard Henderson     DebugFrameCIE cie;
902c90784aSRichard Henderson     DebugFrameFDEHeader fde;
912c90784aSRichard Henderson } DebugFrameHeader;
922c90784aSRichard Henderson 
93813da627SRichard Henderson static void tcg_register_jit_int(void *buf, size_t size,
942c90784aSRichard Henderson                                  const void *debug_frame,
952c90784aSRichard Henderson                                  size_t debug_frame_size)
96813da627SRichard Henderson     __attribute__((unused));
97813da627SRichard Henderson 
98c0ad3001SStefan Weil /* Forward declarations for functions declared and used in tcg-target.c. */
99c0ad3001SStefan Weil static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str);
1002a534affSRichard Henderson static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
101a05b5b9bSRichard Henderson                        intptr_t arg2);
1022a534affSRichard Henderson static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
103c0ad3001SStefan Weil static void tcg_out_movi(TCGContext *s, TCGType type,
1042a534affSRichard Henderson                          TCGReg ret, tcg_target_long arg);
105c0ad3001SStefan Weil static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
106c0ad3001SStefan Weil                        const int *const_args);
1072a534affSRichard Henderson static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
108a05b5b9bSRichard Henderson                        intptr_t arg2);
109cf066674SRichard Henderson static void tcg_out_call(TCGContext *s, tcg_insn_unit *target);
110f6c6afc1SRichard Henderson static int tcg_target_const_match(tcg_target_long val, TCGType type,
111c0ad3001SStefan Weil                                   const TCGArgConstraint *arg_ct);
1129ecefc84SRichard Henderson static void tcg_out_tb_init(TCGContext *s);
1139ecefc84SRichard Henderson static void tcg_out_tb_finalize(TCGContext *s);
1149ecefc84SRichard Henderson 
115c0ad3001SStefan Weil 
1168399ad59SRichard Henderson TCGOpDef tcg_op_defs[] = {
1170e2029a0SAurelien Jarno #define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags },
118c896fe29Sbellard #include "tcg-opc.h"
119c896fe29Sbellard #undef DEF
120c896fe29Sbellard };
1212a24374aSStefan Weil const size_t tcg_op_defs_max = ARRAY_SIZE(tcg_op_defs);
122c896fe29Sbellard 
123b1d8e52eSblueswir1 static TCGRegSet tcg_target_available_regs[2];
124b1d8e52eSblueswir1 static TCGRegSet tcg_target_call_clobber_regs;
125c896fe29Sbellard 
1261813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE == 1
1274196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
128c896fe29Sbellard {
129c896fe29Sbellard     *s->code_ptr++ = v;
130c896fe29Sbellard }
131c896fe29Sbellard 
1324196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
1334196dca6SPeter Maydell                                                       uint8_t v)
1345c53bb81SPeter Maydell {
1351813e175SRichard Henderson     *p = v;
1365c53bb81SPeter Maydell }
1371813e175SRichard Henderson #endif
1385c53bb81SPeter Maydell 
1391813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE <= 2
1404196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
141c896fe29Sbellard {
1421813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
1431813e175SRichard Henderson         *s->code_ptr++ = v;
1441813e175SRichard Henderson     } else {
1451813e175SRichard Henderson         tcg_insn_unit *p = s->code_ptr;
1464387345aSPeter Maydell         memcpy(p, &v, sizeof(v));
1471813e175SRichard Henderson         s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
1481813e175SRichard Henderson     }
149c896fe29Sbellard }
150c896fe29Sbellard 
1514196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
1524196dca6SPeter Maydell                                                        uint16_t v)
1535c53bb81SPeter Maydell {
1541813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
1551813e175SRichard Henderson         *p = v;
1561813e175SRichard Henderson     } else {
1575c53bb81SPeter Maydell         memcpy(p, &v, sizeof(v));
1585c53bb81SPeter Maydell     }
1591813e175SRichard Henderson }
1601813e175SRichard Henderson #endif
1615c53bb81SPeter Maydell 
1621813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE <= 4
1634196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
164c896fe29Sbellard {
1651813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
1661813e175SRichard Henderson         *s->code_ptr++ = v;
1671813e175SRichard Henderson     } else {
1681813e175SRichard Henderson         tcg_insn_unit *p = s->code_ptr;
1694387345aSPeter Maydell         memcpy(p, &v, sizeof(v));
1701813e175SRichard Henderson         s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
1711813e175SRichard Henderson     }
172c896fe29Sbellard }
173c896fe29Sbellard 
1744196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
1754196dca6SPeter Maydell                                                        uint32_t v)
1765c53bb81SPeter Maydell {
1771813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
1781813e175SRichard Henderson         *p = v;
1791813e175SRichard Henderson     } else {
1805c53bb81SPeter Maydell         memcpy(p, &v, sizeof(v));
1815c53bb81SPeter Maydell     }
1821813e175SRichard Henderson }
1831813e175SRichard Henderson #endif
1845c53bb81SPeter Maydell 
1851813e175SRichard Henderson #if TCG_TARGET_INSN_UNIT_SIZE <= 8
1864196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
187ac26eb69SRichard Henderson {
1881813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
1891813e175SRichard Henderson         *s->code_ptr++ = v;
1901813e175SRichard Henderson     } else {
1911813e175SRichard Henderson         tcg_insn_unit *p = s->code_ptr;
1924387345aSPeter Maydell         memcpy(p, &v, sizeof(v));
1931813e175SRichard Henderson         s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
1941813e175SRichard Henderson     }
195ac26eb69SRichard Henderson }
196ac26eb69SRichard Henderson 
1974196dca6SPeter Maydell static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
1984196dca6SPeter Maydell                                                        uint64_t v)
1995c53bb81SPeter Maydell {
2001813e175SRichard Henderson     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
2011813e175SRichard Henderson         *p = v;
2021813e175SRichard Henderson     } else {
2035c53bb81SPeter Maydell         memcpy(p, &v, sizeof(v));
2045c53bb81SPeter Maydell     }
2051813e175SRichard Henderson }
2061813e175SRichard Henderson #endif
2075c53bb81SPeter Maydell 
208c896fe29Sbellard /* label relocation processing */
209c896fe29Sbellard 
2101813e175SRichard Henderson static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
2112ba7fae2SRichard Henderson                           int label_index, intptr_t addend)
212c896fe29Sbellard {
213c896fe29Sbellard     TCGLabel *l;
214c896fe29Sbellard     TCGRelocation *r;
215c896fe29Sbellard 
216c896fe29Sbellard     l = &s->labels[label_index];
217c896fe29Sbellard     if (l->has_value) {
218623e265cSpbrook         /* FIXME: This may break relocations on RISC targets that
219623e265cSpbrook            modify instruction fields in place.  The caller may not have
220623e265cSpbrook            written the initial value.  */
221f54b3f92Saurel32         patch_reloc(code_ptr, type, l->u.value, addend);
222c896fe29Sbellard     } else {
223c896fe29Sbellard         /* add a new relocation entry */
224c896fe29Sbellard         r = tcg_malloc(sizeof(TCGRelocation));
225c896fe29Sbellard         r->type = type;
226c896fe29Sbellard         r->ptr = code_ptr;
227c896fe29Sbellard         r->addend = addend;
228c896fe29Sbellard         r->next = l->u.first_reloc;
229c896fe29Sbellard         l->u.first_reloc = r;
230c896fe29Sbellard     }
231c896fe29Sbellard }
232c896fe29Sbellard 
2331813e175SRichard Henderson static void tcg_out_label(TCGContext *s, int label_index, tcg_insn_unit *ptr)
234c896fe29Sbellard {
2351813e175SRichard Henderson     TCGLabel *l = &s->labels[label_index];
2362ba7fae2SRichard Henderson     intptr_t value = (intptr_t)ptr;
2371813e175SRichard Henderson     TCGRelocation *r;
238c896fe29Sbellard 
2391813e175SRichard Henderson     assert(!l->has_value);
2401813e175SRichard Henderson 
2411813e175SRichard Henderson     for (r = l->u.first_reloc; r != NULL; r = r->next) {
242f54b3f92Saurel32         patch_reloc(r->ptr, r->type, value, r->addend);
243c896fe29Sbellard     }
2441813e175SRichard Henderson 
245c896fe29Sbellard     l->has_value = 1;
2461813e175SRichard Henderson     l->u.value_ptr = ptr;
247c896fe29Sbellard }
248c896fe29Sbellard 
249c896fe29Sbellard int gen_new_label(void)
250c896fe29Sbellard {
251c896fe29Sbellard     TCGContext *s = &tcg_ctx;
252c896fe29Sbellard     int idx;
253c896fe29Sbellard     TCGLabel *l;
254c896fe29Sbellard 
255c896fe29Sbellard     if (s->nb_labels >= TCG_MAX_LABELS)
256c896fe29Sbellard         tcg_abort();
257c896fe29Sbellard     idx = s->nb_labels++;
258c896fe29Sbellard     l = &s->labels[idx];
259c896fe29Sbellard     l->has_value = 0;
260c896fe29Sbellard     l->u.first_reloc = NULL;
261c896fe29Sbellard     return idx;
262c896fe29Sbellard }
263c896fe29Sbellard 
264c896fe29Sbellard #include "tcg-target.c"
265c896fe29Sbellard 
266c896fe29Sbellard /* pool based memory allocation */
267c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size)
268c896fe29Sbellard {
269c896fe29Sbellard     TCGPool *p;
270c896fe29Sbellard     int pool_size;
271c896fe29Sbellard 
272c896fe29Sbellard     if (size > TCG_POOL_CHUNK_SIZE) {
273c896fe29Sbellard         /* big malloc: insert a new pool (XXX: could optimize) */
2747267c094SAnthony Liguori         p = g_malloc(sizeof(TCGPool) + size);
275c896fe29Sbellard         p->size = size;
2764055299eSKirill Batuzov         p->next = s->pool_first_large;
2774055299eSKirill Batuzov         s->pool_first_large = p;
2784055299eSKirill Batuzov         return p->data;
279c896fe29Sbellard     } else {
280c896fe29Sbellard         p = s->pool_current;
281c896fe29Sbellard         if (!p) {
282c896fe29Sbellard             p = s->pool_first;
283c896fe29Sbellard             if (!p)
284c896fe29Sbellard                 goto new_pool;
285c896fe29Sbellard         } else {
286c896fe29Sbellard             if (!p->next) {
287c896fe29Sbellard             new_pool:
288c896fe29Sbellard                 pool_size = TCG_POOL_CHUNK_SIZE;
2897267c094SAnthony Liguori                 p = g_malloc(sizeof(TCGPool) + pool_size);
290c896fe29Sbellard                 p->size = pool_size;
291c896fe29Sbellard                 p->next = NULL;
292c896fe29Sbellard                 if (s->pool_current)
293c896fe29Sbellard                     s->pool_current->next = p;
294c896fe29Sbellard                 else
295c896fe29Sbellard                     s->pool_first = p;
296c896fe29Sbellard             } else {
297c896fe29Sbellard                 p = p->next;
298c896fe29Sbellard             }
299c896fe29Sbellard         }
300c896fe29Sbellard     }
301c896fe29Sbellard     s->pool_current = p;
302c896fe29Sbellard     s->pool_cur = p->data + size;
303c896fe29Sbellard     s->pool_end = p->data + p->size;
304c896fe29Sbellard     return p->data;
305c896fe29Sbellard }
306c896fe29Sbellard 
307c896fe29Sbellard void tcg_pool_reset(TCGContext *s)
308c896fe29Sbellard {
3094055299eSKirill Batuzov     TCGPool *p, *t;
3104055299eSKirill Batuzov     for (p = s->pool_first_large; p; p = t) {
3114055299eSKirill Batuzov         t = p->next;
3124055299eSKirill Batuzov         g_free(p);
3134055299eSKirill Batuzov     }
3144055299eSKirill Batuzov     s->pool_first_large = NULL;
315c896fe29Sbellard     s->pool_cur = s->pool_end = NULL;
316c896fe29Sbellard     s->pool_current = NULL;
317c896fe29Sbellard }
318c896fe29Sbellard 
319100b5e01SRichard Henderson typedef struct TCGHelperInfo {
320100b5e01SRichard Henderson     void *func;
321100b5e01SRichard Henderson     const char *name;
322afb49896SRichard Henderson     unsigned flags;
323afb49896SRichard Henderson     unsigned sizemask;
324100b5e01SRichard Henderson } TCGHelperInfo;
325100b5e01SRichard Henderson 
3262ef6175aSRichard Henderson #include "exec/helper-proto.h"
3272ef6175aSRichard Henderson 
328100b5e01SRichard Henderson static const TCGHelperInfo all_helpers[] = {
3292ef6175aSRichard Henderson #include "exec/helper-tcg.h"
330100b5e01SRichard Henderson };
331100b5e01SRichard Henderson 
332c896fe29Sbellard void tcg_context_init(TCGContext *s)
333c896fe29Sbellard {
334100b5e01SRichard Henderson     int op, total_args, n, i;
335c896fe29Sbellard     TCGOpDef *def;
336c896fe29Sbellard     TCGArgConstraint *args_ct;
337c896fe29Sbellard     int *sorted_args;
33884fd9dd3SRichard Henderson     GHashTable *helper_table;
339c896fe29Sbellard 
340c896fe29Sbellard     memset(s, 0, sizeof(*s));
341c896fe29Sbellard     s->nb_globals = 0;
342c896fe29Sbellard 
343c896fe29Sbellard     /* Count total number of arguments and allocate the corresponding
344c896fe29Sbellard        space */
345c896fe29Sbellard     total_args = 0;
346c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
347c896fe29Sbellard         def = &tcg_op_defs[op];
348c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
349c896fe29Sbellard         total_args += n;
350c896fe29Sbellard     }
351c896fe29Sbellard 
3527267c094SAnthony Liguori     args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
3537267c094SAnthony Liguori     sorted_args = g_malloc(sizeof(int) * total_args);
354c896fe29Sbellard 
355c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
356c896fe29Sbellard         def = &tcg_op_defs[op];
357c896fe29Sbellard         def->args_ct = args_ct;
358c896fe29Sbellard         def->sorted_args = sorted_args;
359c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
360c896fe29Sbellard         sorted_args += n;
361c896fe29Sbellard         args_ct += n;
362c896fe29Sbellard     }
363c896fe29Sbellard 
3645cd8f621SRichard Henderson     /* Register helpers.  */
36584fd9dd3SRichard Henderson     /* Use g_direct_hash/equal for direct pointer comparisons on func.  */
36684fd9dd3SRichard Henderson     s->helpers = helper_table = g_hash_table_new(NULL, NULL);
36784fd9dd3SRichard Henderson 
368100b5e01SRichard Henderson     for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
36984fd9dd3SRichard Henderson         g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
37072866e82SRichard Henderson                             (gpointer)&all_helpers[i]);
371100b5e01SRichard Henderson     }
3725cd8f621SRichard Henderson 
373c896fe29Sbellard     tcg_target_init(s);
3749002ec79SRichard Henderson }
375b03cce8eSbellard 
3769002ec79SRichard Henderson void tcg_prologue_init(TCGContext *s)
3779002ec79SRichard Henderson {
378b03cce8eSbellard     /* init global prologue and epilogue */
3790b0d3320SEvgeny Voevodin     s->code_buf = s->code_gen_prologue;
380b03cce8eSbellard     s->code_ptr = s->code_buf;
381b03cce8eSbellard     tcg_target_qemu_prologue(s);
382b93949efSRichard Henderson     flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
383d6b64b2bSRichard Henderson 
384d6b64b2bSRichard Henderson #ifdef DEBUG_DISAS
385d6b64b2bSRichard Henderson     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
3861813e175SRichard Henderson         size_t size = tcg_current_code_size(s);
387d6b64b2bSRichard Henderson         qemu_log("PROLOGUE: [size=%zu]\n", size);
388d6b64b2bSRichard Henderson         log_disas(s->code_buf, size);
389d6b64b2bSRichard Henderson         qemu_log("\n");
390d6b64b2bSRichard Henderson         qemu_log_flush();
391d6b64b2bSRichard Henderson     }
392d6b64b2bSRichard Henderson #endif
393c896fe29Sbellard }
394c896fe29Sbellard 
395e2c6d1b4SRichard Henderson void tcg_set_frame(TCGContext *s, int reg, intptr_t start, intptr_t size)
396c896fe29Sbellard {
397c896fe29Sbellard     s->frame_start = start;
398c896fe29Sbellard     s->frame_end = start + size;
399c896fe29Sbellard     s->frame_reg = reg;
400c896fe29Sbellard }
401c896fe29Sbellard 
402c896fe29Sbellard void tcg_func_start(TCGContext *s)
403c896fe29Sbellard {
404c896fe29Sbellard     tcg_pool_reset(s);
405c896fe29Sbellard     s->nb_temps = s->nb_globals;
4060ec9eabcSRichard Henderson 
4070ec9eabcSRichard Henderson     /* No temps have been previously allocated for size or locality.  */
4080ec9eabcSRichard Henderson     memset(s->free_temps, 0, sizeof(s->free_temps));
4090ec9eabcSRichard Henderson 
410c896fe29Sbellard     s->nb_labels = 0;
411c896fe29Sbellard     s->current_frame_offset = s->frame_start;
412c896fe29Sbellard 
4130a209d4bSRichard Henderson #ifdef CONFIG_DEBUG_TCG
4140a209d4bSRichard Henderson     s->goto_tb_issue_mask = 0;
4150a209d4bSRichard Henderson #endif
4160a209d4bSRichard Henderson 
417c45cb8bbSRichard Henderson     s->gen_first_op_idx = 0;
418c45cb8bbSRichard Henderson     s->gen_last_op_idx = -1;
419c45cb8bbSRichard Henderson     s->gen_next_op_idx = 0;
420c45cb8bbSRichard Henderson     s->gen_next_parm_idx = 0;
421b76f0d8cSYeongkyoon Lee 
4229ecefc84SRichard Henderson     s->be = tcg_malloc(sizeof(TCGBackendData));
423c896fe29Sbellard }
424c896fe29Sbellard 
425c896fe29Sbellard static inline void tcg_temp_alloc(TCGContext *s, int n)
426c896fe29Sbellard {
427c896fe29Sbellard     if (n > TCG_MAX_TEMPS)
428c896fe29Sbellard         tcg_abort();
429c896fe29Sbellard }
430c896fe29Sbellard 
431a7812ae4Spbrook static inline int tcg_global_reg_new_internal(TCGType type, int reg,
432a7812ae4Spbrook                                               const char *name)
433c896fe29Sbellard {
434c896fe29Sbellard     TCGContext *s = &tcg_ctx;
435c896fe29Sbellard     TCGTemp *ts;
436c896fe29Sbellard     int idx;
437c896fe29Sbellard 
438c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
439c896fe29Sbellard     if (type != TCG_TYPE_I32)
440c896fe29Sbellard         tcg_abort();
441c896fe29Sbellard #endif
442c896fe29Sbellard     if (tcg_regset_test_reg(s->reserved_regs, reg))
443c896fe29Sbellard         tcg_abort();
444c896fe29Sbellard     idx = s->nb_globals;
445c896fe29Sbellard     tcg_temp_alloc(s, s->nb_globals + 1);
446c896fe29Sbellard     ts = &s->temps[s->nb_globals];
447c896fe29Sbellard     ts->base_type = type;
448c896fe29Sbellard     ts->type = type;
449c896fe29Sbellard     ts->fixed_reg = 1;
450c896fe29Sbellard     ts->reg = reg;
451c896fe29Sbellard     ts->name = name;
452c896fe29Sbellard     s->nb_globals++;
453c896fe29Sbellard     tcg_regset_set_reg(s->reserved_regs, reg);
454a7812ae4Spbrook     return idx;
455a7812ae4Spbrook }
456a7812ae4Spbrook 
457a7812ae4Spbrook TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name)
458a7812ae4Spbrook {
459a7812ae4Spbrook     int idx;
460a7812ae4Spbrook 
461a7812ae4Spbrook     idx = tcg_global_reg_new_internal(TCG_TYPE_I32, reg, name);
462a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
463a7812ae4Spbrook }
464a7812ae4Spbrook 
465a7812ae4Spbrook TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name)
466a7812ae4Spbrook {
467a7812ae4Spbrook     int idx;
468a7812ae4Spbrook 
469a7812ae4Spbrook     idx = tcg_global_reg_new_internal(TCG_TYPE_I64, reg, name);
470a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
471c896fe29Sbellard }
472c896fe29Sbellard 
473a7812ae4Spbrook static inline int tcg_global_mem_new_internal(TCGType type, int reg,
4742f2f244dSRichard Henderson                                               intptr_t offset,
475c896fe29Sbellard                                               const char *name)
476c896fe29Sbellard {
477c896fe29Sbellard     TCGContext *s = &tcg_ctx;
478c896fe29Sbellard     TCGTemp *ts;
479c896fe29Sbellard     int idx;
480c896fe29Sbellard 
481c896fe29Sbellard     idx = s->nb_globals;
482c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
483c896fe29Sbellard     if (type == TCG_TYPE_I64) {
484c896fe29Sbellard         char buf[64];
485c588979bSths         tcg_temp_alloc(s, s->nb_globals + 2);
486c896fe29Sbellard         ts = &s->temps[s->nb_globals];
487c896fe29Sbellard         ts->base_type = type;
488c896fe29Sbellard         ts->type = TCG_TYPE_I32;
489c896fe29Sbellard         ts->fixed_reg = 0;
490c896fe29Sbellard         ts->mem_allocated = 1;
491c896fe29Sbellard         ts->mem_reg = reg;
49202eb19d0SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
493c896fe29Sbellard         ts->mem_offset = offset + 4;
494c896fe29Sbellard #else
495c896fe29Sbellard         ts->mem_offset = offset;
496c896fe29Sbellard #endif
497c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
498c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_0");
499c896fe29Sbellard         ts->name = strdup(buf);
500c896fe29Sbellard         ts++;
501c896fe29Sbellard 
502c896fe29Sbellard         ts->base_type = type;
503c896fe29Sbellard         ts->type = TCG_TYPE_I32;
504c896fe29Sbellard         ts->fixed_reg = 0;
505c896fe29Sbellard         ts->mem_allocated = 1;
506c896fe29Sbellard         ts->mem_reg = reg;
50702eb19d0SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
508c896fe29Sbellard         ts->mem_offset = offset;
509c896fe29Sbellard #else
510c896fe29Sbellard         ts->mem_offset = offset + 4;
511c896fe29Sbellard #endif
512c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
513c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_1");
514c896fe29Sbellard         ts->name = strdup(buf);
515c896fe29Sbellard 
516c896fe29Sbellard         s->nb_globals += 2;
517c896fe29Sbellard     } else
518c896fe29Sbellard #endif
519c896fe29Sbellard     {
520c896fe29Sbellard         tcg_temp_alloc(s, s->nb_globals + 1);
521c896fe29Sbellard         ts = &s->temps[s->nb_globals];
522c896fe29Sbellard         ts->base_type = type;
523c896fe29Sbellard         ts->type = type;
524c896fe29Sbellard         ts->fixed_reg = 0;
525c896fe29Sbellard         ts->mem_allocated = 1;
526c896fe29Sbellard         ts->mem_reg = reg;
527c896fe29Sbellard         ts->mem_offset = offset;
528c896fe29Sbellard         ts->name = name;
529c896fe29Sbellard         s->nb_globals++;
530c896fe29Sbellard     }
531a7812ae4Spbrook     return idx;
532c896fe29Sbellard }
533c896fe29Sbellard 
5342f2f244dSRichard Henderson TCGv_i32 tcg_global_mem_new_i32(int reg, intptr_t offset, const char *name)
535a7812ae4Spbrook {
5362f2f244dSRichard Henderson     int idx = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name);
537a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
538a7812ae4Spbrook }
539a7812ae4Spbrook 
5402f2f244dSRichard Henderson TCGv_i64 tcg_global_mem_new_i64(int reg, intptr_t offset, const char *name)
541a7812ae4Spbrook {
5422f2f244dSRichard Henderson     int idx = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name);
543a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
544a7812ae4Spbrook }
545a7812ae4Spbrook 
546a7812ae4Spbrook static inline int tcg_temp_new_internal(TCGType type, int temp_local)
547c896fe29Sbellard {
548c896fe29Sbellard     TCGContext *s = &tcg_ctx;
549c896fe29Sbellard     TCGTemp *ts;
550641d5fbeSbellard     int idx, k;
551c896fe29Sbellard 
5520ec9eabcSRichard Henderson     k = type + (temp_local ? TCG_TYPE_COUNT : 0);
5530ec9eabcSRichard Henderson     idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
5540ec9eabcSRichard Henderson     if (idx < TCG_MAX_TEMPS) {
5550ec9eabcSRichard Henderson         /* There is already an available temp with the right type.  */
5560ec9eabcSRichard Henderson         clear_bit(idx, s->free_temps[k].l);
5570ec9eabcSRichard Henderson 
558e8996ee0Sbellard         ts = &s->temps[idx];
559e8996ee0Sbellard         ts->temp_allocated = 1;
5600ec9eabcSRichard Henderson         assert(ts->base_type == type);
561641d5fbeSbellard         assert(ts->temp_local == temp_local);
562e8996ee0Sbellard     } else {
563c896fe29Sbellard         idx = s->nb_temps;
564c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
565c896fe29Sbellard         if (type == TCG_TYPE_I64) {
5668df1ca4bSths             tcg_temp_alloc(s, s->nb_temps + 2);
567c896fe29Sbellard             ts = &s->temps[s->nb_temps];
568c896fe29Sbellard             ts->base_type = type;
569c896fe29Sbellard             ts->type = TCG_TYPE_I32;
570e8996ee0Sbellard             ts->temp_allocated = 1;
571641d5fbeSbellard             ts->temp_local = temp_local;
572c896fe29Sbellard             ts->name = NULL;
573c896fe29Sbellard             ts++;
574f6aa2f7dSRichard Henderson             ts->base_type = type;
575c896fe29Sbellard             ts->type = TCG_TYPE_I32;
576e8996ee0Sbellard             ts->temp_allocated = 1;
577641d5fbeSbellard             ts->temp_local = temp_local;
578c896fe29Sbellard             ts->name = NULL;
579c896fe29Sbellard             s->nb_temps += 2;
580c896fe29Sbellard         } else
581c896fe29Sbellard #endif
582c896fe29Sbellard         {
583c896fe29Sbellard             tcg_temp_alloc(s, s->nb_temps + 1);
584c896fe29Sbellard             ts = &s->temps[s->nb_temps];
585c896fe29Sbellard             ts->base_type = type;
586c896fe29Sbellard             ts->type = type;
587e8996ee0Sbellard             ts->temp_allocated = 1;
588641d5fbeSbellard             ts->temp_local = temp_local;
589c896fe29Sbellard             ts->name = NULL;
590c896fe29Sbellard             s->nb_temps++;
591c896fe29Sbellard         }
592e8996ee0Sbellard     }
59327bfd83cSPeter Maydell 
59427bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
59527bfd83cSPeter Maydell     s->temps_in_use++;
59627bfd83cSPeter Maydell #endif
597a7812ae4Spbrook     return idx;
598c896fe29Sbellard }
599c896fe29Sbellard 
600a7812ae4Spbrook TCGv_i32 tcg_temp_new_internal_i32(int temp_local)
601a7812ae4Spbrook {
602a7812ae4Spbrook     int idx;
603a7812ae4Spbrook 
604a7812ae4Spbrook     idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local);
605a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
606a7812ae4Spbrook }
607a7812ae4Spbrook 
608a7812ae4Spbrook TCGv_i64 tcg_temp_new_internal_i64(int temp_local)
609a7812ae4Spbrook {
610a7812ae4Spbrook     int idx;
611a7812ae4Spbrook 
612a7812ae4Spbrook     idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local);
613a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
614a7812ae4Spbrook }
615a7812ae4Spbrook 
6160ec9eabcSRichard Henderson static void tcg_temp_free_internal(int idx)
617c896fe29Sbellard {
618c896fe29Sbellard     TCGContext *s = &tcg_ctx;
619c896fe29Sbellard     TCGTemp *ts;
620641d5fbeSbellard     int k;
621c896fe29Sbellard 
62227bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
62327bfd83cSPeter Maydell     s->temps_in_use--;
62427bfd83cSPeter Maydell     if (s->temps_in_use < 0) {
62527bfd83cSPeter Maydell         fprintf(stderr, "More temporaries freed than allocated!\n");
62627bfd83cSPeter Maydell     }
62727bfd83cSPeter Maydell #endif
62827bfd83cSPeter Maydell 
629e8996ee0Sbellard     assert(idx >= s->nb_globals && idx < s->nb_temps);
630c896fe29Sbellard     ts = &s->temps[idx];
631e8996ee0Sbellard     assert(ts->temp_allocated != 0);
632e8996ee0Sbellard     ts->temp_allocated = 0;
6330ec9eabcSRichard Henderson 
63418d13fa2SAlexander Graf     k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
6350ec9eabcSRichard Henderson     set_bit(idx, s->free_temps[k].l);
636e8996ee0Sbellard }
637e8996ee0Sbellard 
638a7812ae4Spbrook void tcg_temp_free_i32(TCGv_i32 arg)
639e8996ee0Sbellard {
640a7812ae4Spbrook     tcg_temp_free_internal(GET_TCGV_I32(arg));
641a7812ae4Spbrook }
642a7812ae4Spbrook 
643a7812ae4Spbrook void tcg_temp_free_i64(TCGv_i64 arg)
644a7812ae4Spbrook {
645a7812ae4Spbrook     tcg_temp_free_internal(GET_TCGV_I64(arg));
646a7812ae4Spbrook }
647a7812ae4Spbrook 
648a7812ae4Spbrook TCGv_i32 tcg_const_i32(int32_t val)
649a7812ae4Spbrook {
650a7812ae4Spbrook     TCGv_i32 t0;
651a7812ae4Spbrook     t0 = tcg_temp_new_i32();
652e8996ee0Sbellard     tcg_gen_movi_i32(t0, val);
653e8996ee0Sbellard     return t0;
654c896fe29Sbellard }
655c896fe29Sbellard 
656a7812ae4Spbrook TCGv_i64 tcg_const_i64(int64_t val)
657c896fe29Sbellard {
658a7812ae4Spbrook     TCGv_i64 t0;
659a7812ae4Spbrook     t0 = tcg_temp_new_i64();
660e8996ee0Sbellard     tcg_gen_movi_i64(t0, val);
661e8996ee0Sbellard     return t0;
662c896fe29Sbellard }
663c896fe29Sbellard 
664a7812ae4Spbrook TCGv_i32 tcg_const_local_i32(int32_t val)
665bdffd4a9Saurel32 {
666a7812ae4Spbrook     TCGv_i32 t0;
667a7812ae4Spbrook     t0 = tcg_temp_local_new_i32();
668bdffd4a9Saurel32     tcg_gen_movi_i32(t0, val);
669bdffd4a9Saurel32     return t0;
670bdffd4a9Saurel32 }
671bdffd4a9Saurel32 
672a7812ae4Spbrook TCGv_i64 tcg_const_local_i64(int64_t val)
673bdffd4a9Saurel32 {
674a7812ae4Spbrook     TCGv_i64 t0;
675a7812ae4Spbrook     t0 = tcg_temp_local_new_i64();
676bdffd4a9Saurel32     tcg_gen_movi_i64(t0, val);
677bdffd4a9Saurel32     return t0;
678bdffd4a9Saurel32 }
679bdffd4a9Saurel32 
68027bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
68127bfd83cSPeter Maydell void tcg_clear_temp_count(void)
68227bfd83cSPeter Maydell {
68327bfd83cSPeter Maydell     TCGContext *s = &tcg_ctx;
68427bfd83cSPeter Maydell     s->temps_in_use = 0;
68527bfd83cSPeter Maydell }
68627bfd83cSPeter Maydell 
68727bfd83cSPeter Maydell int tcg_check_temp_count(void)
68827bfd83cSPeter Maydell {
68927bfd83cSPeter Maydell     TCGContext *s = &tcg_ctx;
69027bfd83cSPeter Maydell     if (s->temps_in_use) {
69127bfd83cSPeter Maydell         /* Clear the count so that we don't give another
69227bfd83cSPeter Maydell          * warning immediately next time around.
69327bfd83cSPeter Maydell          */
69427bfd83cSPeter Maydell         s->temps_in_use = 0;
69527bfd83cSPeter Maydell         return 1;
69627bfd83cSPeter Maydell     }
69727bfd83cSPeter Maydell     return 0;
69827bfd83cSPeter Maydell }
69927bfd83cSPeter Maydell #endif
70027bfd83cSPeter Maydell 
70139cf05d3Sbellard /* Note: we convert the 64 bit args to 32 bit and do some alignment
70239cf05d3Sbellard    and endian swap. Maybe it would be better to do the alignment
70339cf05d3Sbellard    and endian swap in tcg_reg_alloc_call(). */
704bbb8a1b4SRichard Henderson void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
705bbb8a1b4SRichard Henderson                    int nargs, TCGArg *args)
706c896fe29Sbellard {
707c45cb8bbSRichard Henderson     int i, real_args, nb_rets, pi, pi_first;
708bbb8a1b4SRichard Henderson     unsigned sizemask, flags;
709afb49896SRichard Henderson     TCGHelperInfo *info;
710afb49896SRichard Henderson 
711afb49896SRichard Henderson     info = g_hash_table_lookup(s->helpers, (gpointer)func);
712bbb8a1b4SRichard Henderson     flags = info->flags;
713bbb8a1b4SRichard Henderson     sizemask = info->sizemask;
7142bece2c8SRichard Henderson 
71534b1a49cSRichard Henderson #if defined(__sparc__) && !defined(__arch64__) \
71634b1a49cSRichard Henderson     && !defined(CONFIG_TCG_INTERPRETER)
71734b1a49cSRichard Henderson     /* We have 64-bit values in one register, but need to pass as two
71834b1a49cSRichard Henderson        separate parameters.  Split them.  */
71934b1a49cSRichard Henderson     int orig_sizemask = sizemask;
72034b1a49cSRichard Henderson     int orig_nargs = nargs;
72134b1a49cSRichard Henderson     TCGv_i64 retl, reth;
72234b1a49cSRichard Henderson 
72334b1a49cSRichard Henderson     TCGV_UNUSED_I64(retl);
72434b1a49cSRichard Henderson     TCGV_UNUSED_I64(reth);
72534b1a49cSRichard Henderson     if (sizemask != 0) {
72634b1a49cSRichard Henderson         TCGArg *split_args = __builtin_alloca(sizeof(TCGArg) * nargs * 2);
72734b1a49cSRichard Henderson         for (i = real_args = 0; i < nargs; ++i) {
72834b1a49cSRichard Henderson             int is_64bit = sizemask & (1 << (i+1)*2);
72934b1a49cSRichard Henderson             if (is_64bit) {
73034b1a49cSRichard Henderson                 TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
73134b1a49cSRichard Henderson                 TCGv_i32 h = tcg_temp_new_i32();
73234b1a49cSRichard Henderson                 TCGv_i32 l = tcg_temp_new_i32();
73334b1a49cSRichard Henderson                 tcg_gen_extr_i64_i32(l, h, orig);
73434b1a49cSRichard Henderson                 split_args[real_args++] = GET_TCGV_I32(h);
73534b1a49cSRichard Henderson                 split_args[real_args++] = GET_TCGV_I32(l);
73634b1a49cSRichard Henderson             } else {
73734b1a49cSRichard Henderson                 split_args[real_args++] = args[i];
73834b1a49cSRichard Henderson             }
73934b1a49cSRichard Henderson         }
74034b1a49cSRichard Henderson         nargs = real_args;
74134b1a49cSRichard Henderson         args = split_args;
74234b1a49cSRichard Henderson         sizemask = 0;
74334b1a49cSRichard Henderson     }
74434b1a49cSRichard Henderson #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
7452bece2c8SRichard Henderson     for (i = 0; i < nargs; ++i) {
7462bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
7472bece2c8SRichard Henderson         int is_signed = sizemask & (2 << (i+1)*2);
7482bece2c8SRichard Henderson         if (!is_64bit) {
7492bece2c8SRichard Henderson             TCGv_i64 temp = tcg_temp_new_i64();
7502bece2c8SRichard Henderson             TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
7512bece2c8SRichard Henderson             if (is_signed) {
7522bece2c8SRichard Henderson                 tcg_gen_ext32s_i64(temp, orig);
7532bece2c8SRichard Henderson             } else {
7542bece2c8SRichard Henderson                 tcg_gen_ext32u_i64(temp, orig);
7552bece2c8SRichard Henderson             }
7562bece2c8SRichard Henderson             args[i] = GET_TCGV_I64(temp);
7572bece2c8SRichard Henderson         }
7582bece2c8SRichard Henderson     }
7592bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */
7602bece2c8SRichard Henderson 
761c45cb8bbSRichard Henderson     pi_first = pi = s->gen_next_parm_idx;
762a7812ae4Spbrook     if (ret != TCG_CALL_DUMMY_ARG) {
76334b1a49cSRichard Henderson #if defined(__sparc__) && !defined(__arch64__) \
76434b1a49cSRichard Henderson     && !defined(CONFIG_TCG_INTERPRETER)
76534b1a49cSRichard Henderson         if (orig_sizemask & 1) {
76634b1a49cSRichard Henderson             /* The 32-bit ABI is going to return the 64-bit value in
76734b1a49cSRichard Henderson                the %o0/%o1 register pair.  Prepare for this by using
76834b1a49cSRichard Henderson                two return temporaries, and reassemble below.  */
76934b1a49cSRichard Henderson             retl = tcg_temp_new_i64();
77034b1a49cSRichard Henderson             reth = tcg_temp_new_i64();
771c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = GET_TCGV_I64(reth);
772c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = GET_TCGV_I64(retl);
77334b1a49cSRichard Henderson             nb_rets = 2;
77434b1a49cSRichard Henderson         } else {
775c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
77634b1a49cSRichard Henderson             nb_rets = 1;
77734b1a49cSRichard Henderson         }
77834b1a49cSRichard Henderson #else
77934b1a49cSRichard Henderson         if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
78002eb19d0SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
781c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret + 1;
782c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
783a7812ae4Spbrook #else
784c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
785c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret + 1;
786a7812ae4Spbrook #endif
787a7812ae4Spbrook             nb_rets = 2;
78834b1a49cSRichard Henderson         } else {
789c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = ret;
790a7812ae4Spbrook             nb_rets = 1;
791a7812ae4Spbrook         }
79234b1a49cSRichard Henderson #endif
793a7812ae4Spbrook     } else {
794a7812ae4Spbrook         nb_rets = 0;
795a7812ae4Spbrook     }
796a7812ae4Spbrook     real_args = 0;
797a7812ae4Spbrook     for (i = 0; i < nargs; i++) {
7982bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
799bbb8a1b4SRichard Henderson         if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
80039cf05d3Sbellard #ifdef TCG_TARGET_CALL_ALIGN_ARGS
80139cf05d3Sbellard             /* some targets want aligned 64 bit args */
802ebd486d5Smalc             if (real_args & 1) {
803c45cb8bbSRichard Henderson                 s->gen_opparam_buf[pi++] = TCG_CALL_DUMMY_ARG;
804ebd486d5Smalc                 real_args++;
80539cf05d3Sbellard             }
80639cf05d3Sbellard #endif
8073f90f252SRichard Henderson 	    /* If stack grows up, then we will be placing successive
8083f90f252SRichard Henderson 	       arguments at lower addresses, which means we need to
8093f90f252SRichard Henderson 	       reverse the order compared to how we would normally
8103f90f252SRichard Henderson 	       treat either big or little-endian.  For those arguments
8113f90f252SRichard Henderson 	       that will wind up in registers, this still works for
8123f90f252SRichard Henderson 	       HPPA (the only current STACK_GROWSUP target) since the
8133f90f252SRichard Henderson 	       argument registers are *also* allocated in decreasing
8143f90f252SRichard Henderson 	       order.  If another such target is added, this logic may
8153f90f252SRichard Henderson 	       have to get more complicated to differentiate between
8163f90f252SRichard Henderson 	       stack arguments and register arguments.  */
81702eb19d0SRichard Henderson #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
818c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i] + 1;
819c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i];
820c896fe29Sbellard #else
821c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i];
822c45cb8bbSRichard Henderson             s->gen_opparam_buf[pi++] = args[i] + 1;
823c896fe29Sbellard #endif
824a7812ae4Spbrook             real_args += 2;
8252bece2c8SRichard Henderson             continue;
8262bece2c8SRichard Henderson         }
8272bece2c8SRichard Henderson 
828c45cb8bbSRichard Henderson         s->gen_opparam_buf[pi++] = args[i];
829a7812ae4Spbrook         real_args++;
830c896fe29Sbellard     }
831c45cb8bbSRichard Henderson     s->gen_opparam_buf[pi++] = (uintptr_t)func;
832c45cb8bbSRichard Henderson     s->gen_opparam_buf[pi++] = flags;
833a7812ae4Spbrook 
834c45cb8bbSRichard Henderson     i = s->gen_next_op_idx;
835c45cb8bbSRichard Henderson     tcg_debug_assert(i < OPC_BUF_SIZE);
836c45cb8bbSRichard Henderson     tcg_debug_assert(pi <= OPPARAM_BUF_SIZE);
837a7812ae4Spbrook 
838c45cb8bbSRichard Henderson     /* Set links for sequential allocation during translation.  */
839c45cb8bbSRichard Henderson     s->gen_op_buf[i] = (TCGOp){
840c45cb8bbSRichard Henderson         .opc = INDEX_op_call,
841c45cb8bbSRichard Henderson         .callo = nb_rets,
842c45cb8bbSRichard Henderson         .calli = real_args,
843c45cb8bbSRichard Henderson         .args = pi_first,
844c45cb8bbSRichard Henderson         .prev = i - 1,
845c45cb8bbSRichard Henderson         .next = i + 1
846c45cb8bbSRichard Henderson     };
847c45cb8bbSRichard Henderson 
848c45cb8bbSRichard Henderson     /* Make sure the calli field didn't overflow.  */
849c45cb8bbSRichard Henderson     tcg_debug_assert(s->gen_op_buf[i].calli == real_args);
850c45cb8bbSRichard Henderson 
851c45cb8bbSRichard Henderson     s->gen_last_op_idx = i;
852c45cb8bbSRichard Henderson     s->gen_next_op_idx = i + 1;
853c45cb8bbSRichard Henderson     s->gen_next_parm_idx = pi;
8542bece2c8SRichard Henderson 
85534b1a49cSRichard Henderson #if defined(__sparc__) && !defined(__arch64__) \
85634b1a49cSRichard Henderson     && !defined(CONFIG_TCG_INTERPRETER)
85734b1a49cSRichard Henderson     /* Free all of the parts we allocated above.  */
85834b1a49cSRichard Henderson     for (i = real_args = 0; i < orig_nargs; ++i) {
85934b1a49cSRichard Henderson         int is_64bit = orig_sizemask & (1 << (i+1)*2);
86034b1a49cSRichard Henderson         if (is_64bit) {
86134b1a49cSRichard Henderson             TCGv_i32 h = MAKE_TCGV_I32(args[real_args++]);
86234b1a49cSRichard Henderson             TCGv_i32 l = MAKE_TCGV_I32(args[real_args++]);
86334b1a49cSRichard Henderson             tcg_temp_free_i32(h);
86434b1a49cSRichard Henderson             tcg_temp_free_i32(l);
86534b1a49cSRichard Henderson         } else {
86634b1a49cSRichard Henderson             real_args++;
86734b1a49cSRichard Henderson         }
86834b1a49cSRichard Henderson     }
86934b1a49cSRichard Henderson     if (orig_sizemask & 1) {
87034b1a49cSRichard Henderson         /* The 32-bit ABI returned two 32-bit pieces.  Re-assemble them.
87134b1a49cSRichard Henderson            Note that describing these as TCGv_i64 eliminates an unnecessary
87234b1a49cSRichard Henderson            zero-extension that tcg_gen_concat_i32_i64 would create.  */
87334b1a49cSRichard Henderson         tcg_gen_concat32_i64(MAKE_TCGV_I64(ret), retl, reth);
87434b1a49cSRichard Henderson         tcg_temp_free_i64(retl);
87534b1a49cSRichard Henderson         tcg_temp_free_i64(reth);
87634b1a49cSRichard Henderson     }
87734b1a49cSRichard Henderson #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
8782bece2c8SRichard Henderson     for (i = 0; i < nargs; ++i) {
8792bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
8802bece2c8SRichard Henderson         if (!is_64bit) {
8812bece2c8SRichard Henderson             TCGv_i64 temp = MAKE_TCGV_I64(args[i]);
8822bece2c8SRichard Henderson             tcg_temp_free_i64(temp);
8832bece2c8SRichard Henderson         }
8842bece2c8SRichard Henderson     }
8852bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */
886a7812ae4Spbrook }
887c896fe29Sbellard 
8888fcd3692Sblueswir1 static void tcg_reg_alloc_start(TCGContext *s)
889c896fe29Sbellard {
890c896fe29Sbellard     int i;
891c896fe29Sbellard     TCGTemp *ts;
892c896fe29Sbellard     for(i = 0; i < s->nb_globals; i++) {
893c896fe29Sbellard         ts = &s->temps[i];
894c896fe29Sbellard         if (ts->fixed_reg) {
895c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
896c896fe29Sbellard         } else {
897c896fe29Sbellard             ts->val_type = TEMP_VAL_MEM;
898c896fe29Sbellard         }
899c896fe29Sbellard     }
900e8996ee0Sbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
901e8996ee0Sbellard         ts = &s->temps[i];
9027dfd8c6aSAurelien Jarno         if (ts->temp_local) {
9037dfd8c6aSAurelien Jarno             ts->val_type = TEMP_VAL_MEM;
9047dfd8c6aSAurelien Jarno         } else {
905e8996ee0Sbellard             ts->val_type = TEMP_VAL_DEAD;
9067dfd8c6aSAurelien Jarno         }
907e8996ee0Sbellard         ts->mem_allocated = 0;
908e8996ee0Sbellard         ts->fixed_reg = 0;
909e8996ee0Sbellard     }
910c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
911c896fe29Sbellard         s->reg_to_temp[i] = -1;
912c896fe29Sbellard     }
913c896fe29Sbellard }
914c896fe29Sbellard 
915ac56dd48Spbrook static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size,
916ac56dd48Spbrook                                  int idx)
917c896fe29Sbellard {
918c896fe29Sbellard     TCGTemp *ts;
919ac56dd48Spbrook 
9207f6f0ae5SStefan Weil     assert(idx >= 0 && idx < s->nb_temps);
921ac56dd48Spbrook     ts = &s->temps[idx];
922ac56dd48Spbrook     if (idx < s->nb_globals) {
923ac56dd48Spbrook         pstrcpy(buf, buf_size, ts->name);
924c896fe29Sbellard     } else {
925641d5fbeSbellard         if (ts->temp_local)
926641d5fbeSbellard             snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
927641d5fbeSbellard         else
928ac56dd48Spbrook             snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
929c896fe29Sbellard     }
930c896fe29Sbellard     return buf;
931c896fe29Sbellard }
932c896fe29Sbellard 
933a7812ae4Spbrook char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg)
934ac56dd48Spbrook {
935a7812ae4Spbrook     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I32(arg));
936a7812ae4Spbrook }
937a7812ae4Spbrook 
938a7812ae4Spbrook char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg)
939a7812ae4Spbrook {
940a810a2deSblueswir1     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I64(arg));
941ac56dd48Spbrook }
942ac56dd48Spbrook 
9436e085f72SRichard Henderson /* Find helper name.  */
9446e085f72SRichard Henderson static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
945e8996ee0Sbellard {
9466e085f72SRichard Henderson     const char *ret = NULL;
9476e085f72SRichard Henderson     if (s->helpers) {
94872866e82SRichard Henderson         TCGHelperInfo *info = g_hash_table_lookup(s->helpers, (gpointer)val);
94972866e82SRichard Henderson         if (info) {
95072866e82SRichard Henderson             ret = info->name;
95172866e82SRichard Henderson         }
952e8996ee0Sbellard     }
9536e085f72SRichard Henderson     return ret;
9544dc81f28Sbellard }
9554dc81f28Sbellard 
956f48f3edeSblueswir1 static const char * const cond_name[] =
957f48f3edeSblueswir1 {
9580aed257fSRichard Henderson     [TCG_COND_NEVER] = "never",
9590aed257fSRichard Henderson     [TCG_COND_ALWAYS] = "always",
960f48f3edeSblueswir1     [TCG_COND_EQ] = "eq",
961f48f3edeSblueswir1     [TCG_COND_NE] = "ne",
962f48f3edeSblueswir1     [TCG_COND_LT] = "lt",
963f48f3edeSblueswir1     [TCG_COND_GE] = "ge",
964f48f3edeSblueswir1     [TCG_COND_LE] = "le",
965f48f3edeSblueswir1     [TCG_COND_GT] = "gt",
966f48f3edeSblueswir1     [TCG_COND_LTU] = "ltu",
967f48f3edeSblueswir1     [TCG_COND_GEU] = "geu",
968f48f3edeSblueswir1     [TCG_COND_LEU] = "leu",
969f48f3edeSblueswir1     [TCG_COND_GTU] = "gtu"
970f48f3edeSblueswir1 };
971f48f3edeSblueswir1 
972f713d6adSRichard Henderson static const char * const ldst_name[] =
973f713d6adSRichard Henderson {
974f713d6adSRichard Henderson     [MO_UB]   = "ub",
975f713d6adSRichard Henderson     [MO_SB]   = "sb",
976f713d6adSRichard Henderson     [MO_LEUW] = "leuw",
977f713d6adSRichard Henderson     [MO_LESW] = "lesw",
978f713d6adSRichard Henderson     [MO_LEUL] = "leul",
979f713d6adSRichard Henderson     [MO_LESL] = "lesl",
980f713d6adSRichard Henderson     [MO_LEQ]  = "leq",
981f713d6adSRichard Henderson     [MO_BEUW] = "beuw",
982f713d6adSRichard Henderson     [MO_BESW] = "besw",
983f713d6adSRichard Henderson     [MO_BEUL] = "beul",
984f713d6adSRichard Henderson     [MO_BESL] = "besl",
985f713d6adSRichard Henderson     [MO_BEQ]  = "beq",
986f713d6adSRichard Henderson };
987f713d6adSRichard Henderson 
988eeacee4dSBlue Swirl void tcg_dump_ops(TCGContext *s)
989c896fe29Sbellard {
990c896fe29Sbellard     char buf[128];
991c45cb8bbSRichard Henderson     TCGOp *op;
992c45cb8bbSRichard Henderson     int oi;
993c896fe29Sbellard 
994c45cb8bbSRichard Henderson     for (oi = s->gen_first_op_idx; oi >= 0; oi = op->next) {
995c45cb8bbSRichard Henderson         int i, k, nb_oargs, nb_iargs, nb_cargs;
996c45cb8bbSRichard Henderson         const TCGOpDef *def;
997c45cb8bbSRichard Henderson         const TCGArg *args;
998c45cb8bbSRichard Henderson         TCGOpcode c;
999c45cb8bbSRichard Henderson 
1000c45cb8bbSRichard Henderson         op = &s->gen_op_buf[oi];
1001c45cb8bbSRichard Henderson         c = op->opc;
1002c896fe29Sbellard         def = &tcg_op_defs[c];
1003c45cb8bbSRichard Henderson         args = &s->gen_opparam_buf[op->args];
1004c45cb8bbSRichard Henderson 
10057e4597d7Sbellard         if (c == INDEX_op_debug_insn_start) {
10067e4597d7Sbellard             uint64_t pc;
10077e4597d7Sbellard #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
10087e4597d7Sbellard             pc = ((uint64_t)args[1] << 32) | args[0];
10097e4597d7Sbellard #else
10107e4597d7Sbellard             pc = args[0];
10117e4597d7Sbellard #endif
1012c45cb8bbSRichard Henderson             if (oi != s->gen_first_op_idx) {
1013eeacee4dSBlue Swirl                 qemu_log("\n");
1014eeacee4dSBlue Swirl             }
1015eeacee4dSBlue Swirl             qemu_log(" ---- 0x%" PRIx64, pc);
10167e4597d7Sbellard         } else if (c == INDEX_op_call) {
1017c896fe29Sbellard             /* variable number of arguments */
1018c45cb8bbSRichard Henderson             nb_oargs = op->callo;
1019c45cb8bbSRichard Henderson             nb_iargs = op->calli;
1020c896fe29Sbellard             nb_cargs = def->nb_cargs;
1021b03cce8eSbellard 
1022cf066674SRichard Henderson             /* function name, flags, out args */
1023cf066674SRichard Henderson             qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
1024cf066674SRichard Henderson                      tcg_find_helper(s, args[nb_oargs + nb_iargs]),
1025cf066674SRichard Henderson                      args[nb_oargs + nb_iargs + 1], nb_oargs);
1026b03cce8eSbellard             for (i = 0; i < nb_oargs; i++) {
1027cf066674SRichard Henderson                 qemu_log(",%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1028eeacee4dSBlue Swirl                                                    args[i]));
1029b03cce8eSbellard             }
1030cf066674SRichard Henderson             for (i = 0; i < nb_iargs; i++) {
1031cf066674SRichard Henderson                 TCGArg arg = args[nb_oargs + i];
1032cf066674SRichard Henderson                 const char *t = "<dummy>";
1033cf066674SRichard Henderson                 if (arg != TCG_CALL_DUMMY_ARG) {
1034cf066674SRichard Henderson                     t = tcg_get_arg_str_idx(s, buf, sizeof(buf), arg);
1035b03cce8eSbellard                 }
1036cf066674SRichard Henderson                 qemu_log(",%s", t);
1037e8996ee0Sbellard             }
1038b03cce8eSbellard         } else {
1039eeacee4dSBlue Swirl             qemu_log(" %s ", def->name);
1040c45cb8bbSRichard Henderson 
1041c896fe29Sbellard             nb_oargs = def->nb_oargs;
1042c896fe29Sbellard             nb_iargs = def->nb_iargs;
1043c896fe29Sbellard             nb_cargs = def->nb_cargs;
1044c896fe29Sbellard 
1045c896fe29Sbellard             k = 0;
1046c896fe29Sbellard             for (i = 0; i < nb_oargs; i++) {
1047eeacee4dSBlue Swirl                 if (k != 0) {
1048eeacee4dSBlue Swirl                     qemu_log(",");
1049eeacee4dSBlue Swirl                 }
1050eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1051eeacee4dSBlue Swirl                                                    args[k++]));
1052c896fe29Sbellard             }
1053c896fe29Sbellard             for (i = 0; i < nb_iargs; i++) {
1054eeacee4dSBlue Swirl                 if (k != 0) {
1055eeacee4dSBlue Swirl                     qemu_log(",");
1056eeacee4dSBlue Swirl                 }
1057eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1058eeacee4dSBlue Swirl                                                    args[k++]));
1059c896fe29Sbellard             }
1060be210acbSRichard Henderson             switch (c) {
1061be210acbSRichard Henderson             case INDEX_op_brcond_i32:
1062ffc5ea09SRichard Henderson             case INDEX_op_setcond_i32:
1063ffc5ea09SRichard Henderson             case INDEX_op_movcond_i32:
1064be210acbSRichard Henderson             case INDEX_op_brcond2_i32:
1065be210acbSRichard Henderson             case INDEX_op_setcond2_i32:
1066ffc5ea09SRichard Henderson             case INDEX_op_brcond_i64:
1067be210acbSRichard Henderson             case INDEX_op_setcond_i64:
1068ffc5ea09SRichard Henderson             case INDEX_op_movcond_i64:
1069eeacee4dSBlue Swirl                 if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]]) {
1070eeacee4dSBlue Swirl                     qemu_log(",%s", cond_name[args[k++]]);
1071eeacee4dSBlue Swirl                 } else {
1072eeacee4dSBlue Swirl                     qemu_log(",$0x%" TCG_PRIlx, args[k++]);
1073eeacee4dSBlue Swirl                 }
1074f48f3edeSblueswir1                 i = 1;
1075be210acbSRichard Henderson                 break;
1076f713d6adSRichard Henderson             case INDEX_op_qemu_ld_i32:
1077f713d6adSRichard Henderson             case INDEX_op_qemu_st_i32:
1078f713d6adSRichard Henderson             case INDEX_op_qemu_ld_i64:
1079f713d6adSRichard Henderson             case INDEX_op_qemu_st_i64:
1080f713d6adSRichard Henderson                 if (args[k] < ARRAY_SIZE(ldst_name) && ldst_name[args[k]]) {
1081f713d6adSRichard Henderson                     qemu_log(",%s", ldst_name[args[k++]]);
1082f713d6adSRichard Henderson                 } else {
1083f713d6adSRichard Henderson                     qemu_log(",$0x%" TCG_PRIlx, args[k++]);
1084f713d6adSRichard Henderson                 }
1085f713d6adSRichard Henderson                 i = 1;
1086f713d6adSRichard Henderson                 break;
1087be210acbSRichard Henderson             default:
1088f48f3edeSblueswir1                 i = 0;
1089be210acbSRichard Henderson                 break;
1090be210acbSRichard Henderson             }
1091f48f3edeSblueswir1             for (; i < nb_cargs; i++) {
1092eeacee4dSBlue Swirl                 if (k != 0) {
1093eeacee4dSBlue Swirl                     qemu_log(",");
1094eeacee4dSBlue Swirl                 }
1095c45cb8bbSRichard Henderson                 qemu_log("$0x%" TCG_PRIlx, args[k++]);
1096c896fe29Sbellard             }
1097b03cce8eSbellard         }
1098eeacee4dSBlue Swirl         qemu_log("\n");
1099c896fe29Sbellard     }
1100c896fe29Sbellard }
1101c896fe29Sbellard 
1102c896fe29Sbellard /* we give more priority to constraints with less registers */
1103c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k)
1104c896fe29Sbellard {
1105c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1106c896fe29Sbellard 
1107c896fe29Sbellard     int i, n;
1108c896fe29Sbellard     arg_ct = &def->args_ct[k];
1109c896fe29Sbellard     if (arg_ct->ct & TCG_CT_ALIAS) {
1110c896fe29Sbellard         /* an alias is equivalent to a single register */
1111c896fe29Sbellard         n = 1;
1112c896fe29Sbellard     } else {
1113c896fe29Sbellard         if (!(arg_ct->ct & TCG_CT_REG))
1114c896fe29Sbellard             return 0;
1115c896fe29Sbellard         n = 0;
1116c896fe29Sbellard         for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1117c896fe29Sbellard             if (tcg_regset_test_reg(arg_ct->u.regs, i))
1118c896fe29Sbellard                 n++;
1119c896fe29Sbellard         }
1120c896fe29Sbellard     }
1121c896fe29Sbellard     return TCG_TARGET_NB_REGS - n + 1;
1122c896fe29Sbellard }
1123c896fe29Sbellard 
1124c896fe29Sbellard /* sort from highest priority to lowest */
1125c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n)
1126c896fe29Sbellard {
1127c896fe29Sbellard     int i, j, p1, p2, tmp;
1128c896fe29Sbellard 
1129c896fe29Sbellard     for(i = 0; i < n; i++)
1130c896fe29Sbellard         def->sorted_args[start + i] = start + i;
1131c896fe29Sbellard     if (n <= 1)
1132c896fe29Sbellard         return;
1133c896fe29Sbellard     for(i = 0; i < n - 1; i++) {
1134c896fe29Sbellard         for(j = i + 1; j < n; j++) {
1135c896fe29Sbellard             p1 = get_constraint_priority(def, def->sorted_args[start + i]);
1136c896fe29Sbellard             p2 = get_constraint_priority(def, def->sorted_args[start + j]);
1137c896fe29Sbellard             if (p1 < p2) {
1138c896fe29Sbellard                 tmp = def->sorted_args[start + i];
1139c896fe29Sbellard                 def->sorted_args[start + i] = def->sorted_args[start + j];
1140c896fe29Sbellard                 def->sorted_args[start + j] = tmp;
1141c896fe29Sbellard             }
1142c896fe29Sbellard         }
1143c896fe29Sbellard     }
1144c896fe29Sbellard }
1145c896fe29Sbellard 
1146c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
1147c896fe29Sbellard {
1148a9751609SRichard Henderson     TCGOpcode op;
1149c896fe29Sbellard     TCGOpDef *def;
1150c896fe29Sbellard     const char *ct_str;
1151c896fe29Sbellard     int i, nb_args;
1152c896fe29Sbellard 
1153c896fe29Sbellard     for(;;) {
1154a9751609SRichard Henderson         if (tdefs->op == (TCGOpcode)-1)
1155c896fe29Sbellard             break;
1156c896fe29Sbellard         op = tdefs->op;
1157c3b08d0eSStefan Weil         assert((unsigned)op < NB_OPS);
1158c896fe29Sbellard         def = &tcg_op_defs[op];
1159c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG)
1160c68aaa18SStefan Weil         /* Duplicate entry in op definitions? */
1161c68aaa18SStefan Weil         assert(!def->used);
1162c68aaa18SStefan Weil         def->used = 1;
1163c68aaa18SStefan Weil #endif
1164c896fe29Sbellard         nb_args = def->nb_iargs + def->nb_oargs;
1165c896fe29Sbellard         for(i = 0; i < nb_args; i++) {
1166c896fe29Sbellard             ct_str = tdefs->args_ct_str[i];
1167c68aaa18SStefan Weil             /* Incomplete TCGTargetOpDef entry? */
1168c68aaa18SStefan Weil             assert(ct_str != NULL);
1169c896fe29Sbellard             tcg_regset_clear(def->args_ct[i].u.regs);
1170c896fe29Sbellard             def->args_ct[i].ct = 0;
1171c896fe29Sbellard             if (ct_str[0] >= '0' && ct_str[0] <= '9') {
1172c896fe29Sbellard                 int oarg;
1173c896fe29Sbellard                 oarg = ct_str[0] - '0';
1174c896fe29Sbellard                 assert(oarg < def->nb_oargs);
1175c896fe29Sbellard                 assert(def->args_ct[oarg].ct & TCG_CT_REG);
1176c896fe29Sbellard                 /* TCG_CT_ALIAS is for the output arguments. The input
11775ff9d6a4Sbellard                    argument is tagged with TCG_CT_IALIAS. */
1178c896fe29Sbellard                 def->args_ct[i] = def->args_ct[oarg];
11795ff9d6a4Sbellard                 def->args_ct[oarg].ct = TCG_CT_ALIAS;
11805ff9d6a4Sbellard                 def->args_ct[oarg].alias_index = i;
1181c896fe29Sbellard                 def->args_ct[i].ct |= TCG_CT_IALIAS;
11825ff9d6a4Sbellard                 def->args_ct[i].alias_index = oarg;
1183c896fe29Sbellard             } else {
1184c896fe29Sbellard                 for(;;) {
1185c896fe29Sbellard                     if (*ct_str == '\0')
1186c896fe29Sbellard                         break;
1187c896fe29Sbellard                     switch(*ct_str) {
1188c896fe29Sbellard                     case 'i':
1189c896fe29Sbellard                         def->args_ct[i].ct |= TCG_CT_CONST;
1190c896fe29Sbellard                         ct_str++;
1191c896fe29Sbellard                         break;
1192c896fe29Sbellard                     default:
1193c896fe29Sbellard                         if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
1194c896fe29Sbellard                             fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
1195c896fe29Sbellard                                     ct_str, i, def->name);
1196c896fe29Sbellard                             exit(1);
1197c896fe29Sbellard                         }
1198c896fe29Sbellard                     }
1199c896fe29Sbellard                 }
1200c896fe29Sbellard             }
1201c896fe29Sbellard         }
1202c896fe29Sbellard 
1203c68aaa18SStefan Weil         /* TCGTargetOpDef entry with too much information? */
1204c68aaa18SStefan Weil         assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
1205c68aaa18SStefan Weil 
1206c896fe29Sbellard         /* sort the constraints (XXX: this is just an heuristic) */
1207c896fe29Sbellard         sort_constraints(def, 0, def->nb_oargs);
1208c896fe29Sbellard         sort_constraints(def, def->nb_oargs, def->nb_iargs);
1209c896fe29Sbellard 
1210c896fe29Sbellard #if 0
1211c896fe29Sbellard         {
1212c896fe29Sbellard             int i;
1213c896fe29Sbellard 
1214c896fe29Sbellard             printf("%s: sorted=", def->name);
1215c896fe29Sbellard             for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
1216c896fe29Sbellard                 printf(" %d", def->sorted_args[i]);
1217c896fe29Sbellard             printf("\n");
1218c896fe29Sbellard         }
1219c896fe29Sbellard #endif
1220c896fe29Sbellard         tdefs++;
1221c896fe29Sbellard     }
1222c896fe29Sbellard 
1223c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG)
1224a9751609SRichard Henderson     i = 0;
1225c68aaa18SStefan Weil     for (op = 0; op < ARRAY_SIZE(tcg_op_defs); op++) {
1226f412c762SRichard Henderson         const TCGOpDef *def = &tcg_op_defs[op];
1227c1a61f6cSRichard Henderson         if (def->flags & TCG_OPF_NOT_PRESENT) {
1228c68aaa18SStefan Weil             /* Wrong entry in op definitions? */
1229f412c762SRichard Henderson             if (def->used) {
1230f412c762SRichard Henderson                 fprintf(stderr, "Invalid op definition for %s\n", def->name);
1231a9751609SRichard Henderson                 i = 1;
1232a9751609SRichard Henderson             }
1233c68aaa18SStefan Weil         } else {
1234c68aaa18SStefan Weil             /* Missing entry in op definitions? */
1235f412c762SRichard Henderson             if (!def->used) {
1236f412c762SRichard Henderson                 fprintf(stderr, "Missing op definition for %s\n", def->name);
1237a9751609SRichard Henderson                 i = 1;
1238c68aaa18SStefan Weil             }
1239c68aaa18SStefan Weil         }
1240a9751609SRichard Henderson     }
1241a9751609SRichard Henderson     if (i == 1) {
1242a9751609SRichard Henderson         tcg_abort();
1243a9751609SRichard Henderson     }
1244c68aaa18SStefan Weil #endif
1245c896fe29Sbellard }
1246c896fe29Sbellard 
12470c627cdcSRichard Henderson void tcg_op_remove(TCGContext *s, TCGOp *op)
12480c627cdcSRichard Henderson {
12490c627cdcSRichard Henderson     int next = op->next;
12500c627cdcSRichard Henderson     int prev = op->prev;
12510c627cdcSRichard Henderson 
12520c627cdcSRichard Henderson     if (next >= 0) {
12530c627cdcSRichard Henderson         s->gen_op_buf[next].prev = prev;
12540c627cdcSRichard Henderson     } else {
12550c627cdcSRichard Henderson         s->gen_last_op_idx = prev;
12560c627cdcSRichard Henderson     }
12570c627cdcSRichard Henderson     if (prev >= 0) {
12580c627cdcSRichard Henderson         s->gen_op_buf[prev].next = next;
12590c627cdcSRichard Henderson     } else {
12600c627cdcSRichard Henderson         s->gen_first_op_idx = next;
12610c627cdcSRichard Henderson     }
12620c627cdcSRichard Henderson 
1263*15fc7daaSRichard Henderson     memset(op, -1, sizeof(*op));
12640c627cdcSRichard Henderson 
12650c627cdcSRichard Henderson #ifdef CONFIG_PROFILER
12660c627cdcSRichard Henderson     s->del_op_count++;
12670c627cdcSRichard Henderson #endif
12680c627cdcSRichard Henderson }
12690c627cdcSRichard Henderson 
1270c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS
12719c43b68dSAurelien Jarno /* liveness analysis: end of function: all temps are dead, and globals
12729c43b68dSAurelien Jarno    should be in memory. */
12739c43b68dSAurelien Jarno static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps,
12749c43b68dSAurelien Jarno                                    uint8_t *mem_temps)
1275c896fe29Sbellard {
12769c43b68dSAurelien Jarno     memset(dead_temps, 1, s->nb_temps);
12779c43b68dSAurelien Jarno     memset(mem_temps, 1, s->nb_globals);
12789c43b68dSAurelien Jarno     memset(mem_temps + s->nb_globals, 0, s->nb_temps - s->nb_globals);
1279c896fe29Sbellard }
1280c896fe29Sbellard 
12819c43b68dSAurelien Jarno /* liveness analysis: end of basic block: all temps are dead, globals
12829c43b68dSAurelien Jarno    and local temps should be in memory. */
12839c43b68dSAurelien Jarno static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
12849c43b68dSAurelien Jarno                                  uint8_t *mem_temps)
1285641d5fbeSbellard {
1286641d5fbeSbellard     int i;
1287641d5fbeSbellard 
12889c43b68dSAurelien Jarno     memset(dead_temps, 1, s->nb_temps);
12899c43b68dSAurelien Jarno     memset(mem_temps, 1, s->nb_globals);
1290641d5fbeSbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
12919c43b68dSAurelien Jarno         mem_temps[i] = s->temps[i].temp_local;
1292641d5fbeSbellard     }
1293641d5fbeSbellard }
1294641d5fbeSbellard 
1295866cb6cbSAurelien Jarno /* Liveness analysis : update the opc_dead_args array to tell if a
1296c896fe29Sbellard    given input arguments is dead. Instructions updating dead
1297c896fe29Sbellard    temporaries are removed. */
12988fcd3692Sblueswir1 static void tcg_liveness_analysis(TCGContext *s)
1299c896fe29Sbellard {
13009c43b68dSAurelien Jarno     uint8_t *dead_temps, *mem_temps;
1301c45cb8bbSRichard Henderson     int oi, oi_prev, nb_ops;
1302c896fe29Sbellard 
1303c45cb8bbSRichard Henderson     nb_ops = s->gen_next_op_idx;
1304866cb6cbSAurelien Jarno     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1305ec7a869dSAurelien Jarno     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1306c896fe29Sbellard 
1307c896fe29Sbellard     dead_temps = tcg_malloc(s->nb_temps);
13089c43b68dSAurelien Jarno     mem_temps = tcg_malloc(s->nb_temps);
13099c43b68dSAurelien Jarno     tcg_la_func_end(s, dead_temps, mem_temps);
1310c896fe29Sbellard 
1311c45cb8bbSRichard Henderson     for (oi = s->gen_last_op_idx; oi >= 0; oi = oi_prev) {
1312c45cb8bbSRichard Henderson         int i, nb_iargs, nb_oargs;
1313c45cb8bbSRichard Henderson         TCGOpcode opc_new, opc_new2;
1314c45cb8bbSRichard Henderson         bool have_opc_new2;
1315c45cb8bbSRichard Henderson         uint16_t dead_args;
1316c45cb8bbSRichard Henderson         uint8_t sync_args;
1317c45cb8bbSRichard Henderson         TCGArg arg;
1318c45cb8bbSRichard Henderson 
1319c45cb8bbSRichard Henderson         TCGOp * const op = &s->gen_op_buf[oi];
1320c45cb8bbSRichard Henderson         TCGArg * const args = &s->gen_opparam_buf[op->args];
1321c45cb8bbSRichard Henderson         TCGOpcode opc = op->opc;
1322c45cb8bbSRichard Henderson         const TCGOpDef *def = &tcg_op_defs[opc];
1323c45cb8bbSRichard Henderson 
1324c45cb8bbSRichard Henderson         oi_prev = op->prev;
1325c45cb8bbSRichard Henderson 
1326c45cb8bbSRichard Henderson         switch (opc) {
1327c896fe29Sbellard         case INDEX_op_call:
1328c6e113f5Sbellard             {
1329c6e113f5Sbellard                 int call_flags;
1330c6e113f5Sbellard 
1331c45cb8bbSRichard Henderson                 nb_oargs = op->callo;
1332c45cb8bbSRichard Henderson                 nb_iargs = op->calli;
1333cf066674SRichard Henderson                 call_flags = args[nb_oargs + nb_iargs + 1];
1334c6e113f5Sbellard 
1335c45cb8bbSRichard Henderson                 /* pure functions can be removed if their result is unused */
133678505279SAurelien Jarno                 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
1337c6e113f5Sbellard                     for (i = 0; i < nb_oargs; i++) {
1338c6e113f5Sbellard                         arg = args[i];
13399c43b68dSAurelien Jarno                         if (!dead_temps[arg] || mem_temps[arg]) {
1340c6e113f5Sbellard                             goto do_not_remove_call;
1341c6e113f5Sbellard                         }
13429c43b68dSAurelien Jarno                     }
1343c45cb8bbSRichard Henderson                     goto do_remove;
1344c6e113f5Sbellard                 } else {
1345c6e113f5Sbellard                 do_not_remove_call:
1346c896fe29Sbellard 
1347c896fe29Sbellard                     /* output args are dead */
13486b64b624SAurelien Jarno                     dead_args = 0;
1349ec7a869dSAurelien Jarno                     sync_args = 0;
1350c896fe29Sbellard                     for (i = 0; i < nb_oargs; i++) {
1351c896fe29Sbellard                         arg = args[i];
13526b64b624SAurelien Jarno                         if (dead_temps[arg]) {
13536b64b624SAurelien Jarno                             dead_args |= (1 << i);
13546b64b624SAurelien Jarno                         }
13559c43b68dSAurelien Jarno                         if (mem_temps[arg]) {
13569c43b68dSAurelien Jarno                             sync_args |= (1 << i);
13579c43b68dSAurelien Jarno                         }
1358c896fe29Sbellard                         dead_temps[arg] = 1;
13599c43b68dSAurelien Jarno                         mem_temps[arg] = 0;
1360c896fe29Sbellard                     }
1361c896fe29Sbellard 
136278505279SAurelien Jarno                     if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
136378505279SAurelien Jarno                         /* globals should be synced to memory */
136478505279SAurelien Jarno                         memset(mem_temps, 1, s->nb_globals);
136578505279SAurelien Jarno                     }
136678505279SAurelien Jarno                     if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
136778505279SAurelien Jarno                                         TCG_CALL_NO_READ_GLOBALS))) {
13689c43b68dSAurelien Jarno                         /* globals should go back to memory */
13699c43b68dSAurelien Jarno                         memset(dead_temps, 1, s->nb_globals);
1370b9c18f56Saurel32                     }
1371c896fe29Sbellard 
1372c896fe29Sbellard                     /* input args are live */
1373866cb6cbSAurelien Jarno                     for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
1374866cb6cbSAurelien Jarno                         arg = args[i];
137539cf05d3Sbellard                         if (arg != TCG_CALL_DUMMY_ARG) {
1376c896fe29Sbellard                             if (dead_temps[arg]) {
1377866cb6cbSAurelien Jarno                                 dead_args |= (1 << i);
1378c896fe29Sbellard                             }
1379c896fe29Sbellard                             dead_temps[arg] = 0;
1380c896fe29Sbellard                         }
138139cf05d3Sbellard                     }
1382c45cb8bbSRichard Henderson                     s->op_dead_args[oi] = dead_args;
1383c45cb8bbSRichard Henderson                     s->op_sync_args[oi] = sync_args;
1384c6e113f5Sbellard                 }
1385c6e113f5Sbellard             }
1386c896fe29Sbellard             break;
13877e4597d7Sbellard         case INDEX_op_debug_insn_start:
1388c896fe29Sbellard             break;
13895ff9d6a4Sbellard         case INDEX_op_discard:
13905ff9d6a4Sbellard             /* mark the temporary as dead */
13915ff9d6a4Sbellard             dead_temps[args[0]] = 1;
13929c43b68dSAurelien Jarno             mem_temps[args[0]] = 0;
13935ff9d6a4Sbellard             break;
13941305c451SRichard Henderson 
13951305c451SRichard Henderson         case INDEX_op_add2_i32:
1396c45cb8bbSRichard Henderson             opc_new = INDEX_op_add_i32;
1397f1fae40cSRichard Henderson             goto do_addsub2;
13981305c451SRichard Henderson         case INDEX_op_sub2_i32:
1399c45cb8bbSRichard Henderson             opc_new = INDEX_op_sub_i32;
1400f1fae40cSRichard Henderson             goto do_addsub2;
1401f1fae40cSRichard Henderson         case INDEX_op_add2_i64:
1402c45cb8bbSRichard Henderson             opc_new = INDEX_op_add_i64;
1403f1fae40cSRichard Henderson             goto do_addsub2;
1404f1fae40cSRichard Henderson         case INDEX_op_sub2_i64:
1405c45cb8bbSRichard Henderson             opc_new = INDEX_op_sub_i64;
1406f1fae40cSRichard Henderson         do_addsub2:
14071305c451SRichard Henderson             nb_iargs = 4;
14081305c451SRichard Henderson             nb_oargs = 2;
14091305c451SRichard Henderson             /* Test if the high part of the operation is dead, but not
14101305c451SRichard Henderson                the low part.  The result can be optimized to a simple
14111305c451SRichard Henderson                add or sub.  This happens often for x86_64 guest when the
14121305c451SRichard Henderson                cpu mode is set to 32 bit.  */
14133c5645faSKirill Batuzov             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
14143c5645faSKirill Batuzov                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
14151305c451SRichard Henderson                     goto do_remove;
14161305c451SRichard Henderson                 }
1417c45cb8bbSRichard Henderson                 /* Replace the opcode and adjust the args in place,
1418c45cb8bbSRichard Henderson                    leaving 3 unused args at the end.  */
1419c45cb8bbSRichard Henderson                 op->opc = opc = opc_new;
14201305c451SRichard Henderson                 args[1] = args[2];
14211305c451SRichard Henderson                 args[2] = args[4];
14221305c451SRichard Henderson                 /* Fall through and mark the single-word operation live.  */
14231305c451SRichard Henderson                 nb_iargs = 2;
14241305c451SRichard Henderson                 nb_oargs = 1;
14251305c451SRichard Henderson             }
14261305c451SRichard Henderson             goto do_not_remove;
14271305c451SRichard Henderson 
14281414968aSRichard Henderson         case INDEX_op_mulu2_i32:
1429c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i32;
1430c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_muluh_i32;
1431c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
143203271524SRichard Henderson             goto do_mul2;
1433f1fae40cSRichard Henderson         case INDEX_op_muls2_i32:
1434c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i32;
1435c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_mulsh_i32;
1436c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
1437f1fae40cSRichard Henderson             goto do_mul2;
1438f1fae40cSRichard Henderson         case INDEX_op_mulu2_i64:
1439c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i64;
1440c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_muluh_i64;
1441c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
144203271524SRichard Henderson             goto do_mul2;
1443f1fae40cSRichard Henderson         case INDEX_op_muls2_i64:
1444c45cb8bbSRichard Henderson             opc_new = INDEX_op_mul_i64;
1445c45cb8bbSRichard Henderson             opc_new2 = INDEX_op_mulsh_i64;
1446c45cb8bbSRichard Henderson             have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
144703271524SRichard Henderson             goto do_mul2;
1448f1fae40cSRichard Henderson         do_mul2:
14491414968aSRichard Henderson             nb_iargs = 2;
14501414968aSRichard Henderson             nb_oargs = 2;
14513c5645faSKirill Batuzov             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
14523c5645faSKirill Batuzov                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
145303271524SRichard Henderson                     /* Both parts of the operation are dead.  */
14541414968aSRichard Henderson                     goto do_remove;
14551414968aSRichard Henderson                 }
145603271524SRichard Henderson                 /* The high part of the operation is dead; generate the low. */
1457c45cb8bbSRichard Henderson                 op->opc = opc = opc_new;
14581414968aSRichard Henderson                 args[1] = args[2];
14591414968aSRichard Henderson                 args[2] = args[3];
1460c45cb8bbSRichard Henderson             } else if (have_opc_new2 && dead_temps[args[0]]
146103271524SRichard Henderson                        && !mem_temps[args[0]]) {
146203271524SRichard Henderson                 /* The low part of the operation is dead; generate the high. */
1463c45cb8bbSRichard Henderson                 op->opc = opc = opc_new2;
146403271524SRichard Henderson                 args[0] = args[1];
146503271524SRichard Henderson                 args[1] = args[2];
146603271524SRichard Henderson                 args[2] = args[3];
146703271524SRichard Henderson             } else {
146803271524SRichard Henderson                 goto do_not_remove;
146903271524SRichard Henderson             }
147003271524SRichard Henderson             /* Mark the single-word operation live.  */
14711414968aSRichard Henderson             nb_oargs = 1;
14721414968aSRichard Henderson             goto do_not_remove;
14731414968aSRichard Henderson 
1474c896fe29Sbellard         default:
14751305c451SRichard Henderson             /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1476c896fe29Sbellard             nb_iargs = def->nb_iargs;
1477c896fe29Sbellard             nb_oargs = def->nb_oargs;
1478c896fe29Sbellard 
1479c896fe29Sbellard             /* Test if the operation can be removed because all
14805ff9d6a4Sbellard                its outputs are dead. We assume that nb_oargs == 0
14815ff9d6a4Sbellard                implies side effects */
14825ff9d6a4Sbellard             if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
1483c896fe29Sbellard                 for (i = 0; i < nb_oargs; i++) {
1484c896fe29Sbellard                     arg = args[i];
14859c43b68dSAurelien Jarno                     if (!dead_temps[arg] || mem_temps[arg]) {
1486c896fe29Sbellard                         goto do_not_remove;
1487c896fe29Sbellard                     }
14889c43b68dSAurelien Jarno                 }
14891305c451SRichard Henderson             do_remove:
14900c627cdcSRichard Henderson                 tcg_op_remove(s, op);
1491c896fe29Sbellard             } else {
1492c896fe29Sbellard             do_not_remove:
1493c896fe29Sbellard                 /* output args are dead */
14946b64b624SAurelien Jarno                 dead_args = 0;
1495ec7a869dSAurelien Jarno                 sync_args = 0;
1496c896fe29Sbellard                 for (i = 0; i < nb_oargs; i++) {
1497c896fe29Sbellard                     arg = args[i];
14986b64b624SAurelien Jarno                     if (dead_temps[arg]) {
14996b64b624SAurelien Jarno                         dead_args |= (1 << i);
15006b64b624SAurelien Jarno                     }
15019c43b68dSAurelien Jarno                     if (mem_temps[arg]) {
15029c43b68dSAurelien Jarno                         sync_args |= (1 << i);
15039c43b68dSAurelien Jarno                     }
1504c896fe29Sbellard                     dead_temps[arg] = 1;
15059c43b68dSAurelien Jarno                     mem_temps[arg] = 0;
1506c896fe29Sbellard                 }
1507c896fe29Sbellard 
1508c896fe29Sbellard                 /* if end of basic block, update */
1509c896fe29Sbellard                 if (def->flags & TCG_OPF_BB_END) {
15109c43b68dSAurelien Jarno                     tcg_la_bb_end(s, dead_temps, mem_temps);
15113d5c5f87SAurelien Jarno                 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
15123d5c5f87SAurelien Jarno                     /* globals should be synced to memory */
15139c43b68dSAurelien Jarno                     memset(mem_temps, 1, s->nb_globals);
1514c896fe29Sbellard                 }
1515c896fe29Sbellard 
1516c896fe29Sbellard                 /* input args are live */
1517866cb6cbSAurelien Jarno                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1518866cb6cbSAurelien Jarno                     arg = args[i];
1519c896fe29Sbellard                     if (dead_temps[arg]) {
1520866cb6cbSAurelien Jarno                         dead_args |= (1 << i);
1521c896fe29Sbellard                     }
1522c896fe29Sbellard                     dead_temps[arg] = 0;
1523c896fe29Sbellard                 }
1524c45cb8bbSRichard Henderson                 s->op_dead_args[oi] = dead_args;
1525c45cb8bbSRichard Henderson                 s->op_sync_args[oi] = sync_args;
1526c896fe29Sbellard             }
1527c896fe29Sbellard             break;
1528c896fe29Sbellard         }
1529c896fe29Sbellard     }
15301ff0a2c5SEvgeny Voevodin }
1531c896fe29Sbellard #else
1532c896fe29Sbellard /* dummy liveness analysis */
1533655feed5Smalc static void tcg_liveness_analysis(TCGContext *s)
1534c896fe29Sbellard {
1535c896fe29Sbellard     int nb_ops;
153692414b31SEvgeny Voevodin     nb_ops = s->gen_opc_ptr - s->gen_opc_buf;
1537c896fe29Sbellard 
1538866cb6cbSAurelien Jarno     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1539866cb6cbSAurelien Jarno     memset(s->op_dead_args, 0, nb_ops * sizeof(uint16_t));
1540ec7a869dSAurelien Jarno     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1541ec7a869dSAurelien Jarno     memset(s->op_sync_args, 0, nb_ops * sizeof(uint8_t));
1542c896fe29Sbellard }
1543c896fe29Sbellard #endif
1544c896fe29Sbellard 
1545c896fe29Sbellard #ifndef NDEBUG
1546c896fe29Sbellard static void dump_regs(TCGContext *s)
1547c896fe29Sbellard {
1548c896fe29Sbellard     TCGTemp *ts;
1549c896fe29Sbellard     int i;
1550c896fe29Sbellard     char buf[64];
1551c896fe29Sbellard 
1552c896fe29Sbellard     for(i = 0; i < s->nb_temps; i++) {
1553c896fe29Sbellard         ts = &s->temps[i];
1554ac56dd48Spbrook         printf("  %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
1555c896fe29Sbellard         switch(ts->val_type) {
1556c896fe29Sbellard         case TEMP_VAL_REG:
1557c896fe29Sbellard             printf("%s", tcg_target_reg_names[ts->reg]);
1558c896fe29Sbellard             break;
1559c896fe29Sbellard         case TEMP_VAL_MEM:
1560c896fe29Sbellard             printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
1561c896fe29Sbellard             break;
1562c896fe29Sbellard         case TEMP_VAL_CONST:
1563c896fe29Sbellard             printf("$0x%" TCG_PRIlx, ts->val);
1564c896fe29Sbellard             break;
1565c896fe29Sbellard         case TEMP_VAL_DEAD:
1566c896fe29Sbellard             printf("D");
1567c896fe29Sbellard             break;
1568c896fe29Sbellard         default:
1569c896fe29Sbellard             printf("???");
1570c896fe29Sbellard             break;
1571c896fe29Sbellard         }
1572c896fe29Sbellard         printf("\n");
1573c896fe29Sbellard     }
1574c896fe29Sbellard 
1575c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1576c896fe29Sbellard         if (s->reg_to_temp[i] >= 0) {
1577c896fe29Sbellard             printf("%s: %s\n",
1578c896fe29Sbellard                    tcg_target_reg_names[i],
1579ac56dd48Spbrook                    tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i]));
1580c896fe29Sbellard         }
1581c896fe29Sbellard     }
1582c896fe29Sbellard }
1583c896fe29Sbellard 
1584c896fe29Sbellard static void check_regs(TCGContext *s)
1585c896fe29Sbellard {
1586c896fe29Sbellard     int reg, k;
1587c896fe29Sbellard     TCGTemp *ts;
1588c896fe29Sbellard     char buf[64];
1589c896fe29Sbellard 
1590c896fe29Sbellard     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1591c896fe29Sbellard         k = s->reg_to_temp[reg];
1592c896fe29Sbellard         if (k >= 0) {
1593c896fe29Sbellard             ts = &s->temps[k];
1594c896fe29Sbellard             if (ts->val_type != TEMP_VAL_REG ||
1595c896fe29Sbellard                 ts->reg != reg) {
1596c896fe29Sbellard                 printf("Inconsistency for register %s:\n",
1597c896fe29Sbellard                        tcg_target_reg_names[reg]);
1598b03cce8eSbellard                 goto fail;
1599c896fe29Sbellard             }
1600c896fe29Sbellard         }
1601c896fe29Sbellard     }
1602c896fe29Sbellard     for(k = 0; k < s->nb_temps; k++) {
1603c896fe29Sbellard         ts = &s->temps[k];
1604c896fe29Sbellard         if (ts->val_type == TEMP_VAL_REG &&
1605c896fe29Sbellard             !ts->fixed_reg &&
1606c896fe29Sbellard             s->reg_to_temp[ts->reg] != k) {
1607c896fe29Sbellard                 printf("Inconsistency for temp %s:\n",
1608ac56dd48Spbrook                        tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
1609b03cce8eSbellard         fail:
1610c896fe29Sbellard                 printf("reg state:\n");
1611c896fe29Sbellard                 dump_regs(s);
1612c896fe29Sbellard                 tcg_abort();
1613c896fe29Sbellard         }
1614c896fe29Sbellard     }
1615c896fe29Sbellard }
1616c896fe29Sbellard #endif
1617c896fe29Sbellard 
1618c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp)
1619c896fe29Sbellard {
1620c896fe29Sbellard     TCGTemp *ts;
1621c896fe29Sbellard     ts = &s->temps[temp];
16229b9c37c3SRichard Henderson #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
16239b9c37c3SRichard Henderson     /* Sparc64 stack is accessed with offset of 2047 */
1624b591dc59SBlue Swirl     s->current_frame_offset = (s->current_frame_offset +
1625b591dc59SBlue Swirl                                (tcg_target_long)sizeof(tcg_target_long) - 1) &
1626b591dc59SBlue Swirl         ~(sizeof(tcg_target_long) - 1);
1627f44c9960SBlue Swirl #endif
1628b591dc59SBlue Swirl     if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
1629b591dc59SBlue Swirl         s->frame_end) {
16305ff9d6a4Sbellard         tcg_abort();
1631b591dc59SBlue Swirl     }
1632c896fe29Sbellard     ts->mem_offset = s->current_frame_offset;
1633c896fe29Sbellard     ts->mem_reg = s->frame_reg;
1634c896fe29Sbellard     ts->mem_allocated = 1;
1635e2c6d1b4SRichard Henderson     s->current_frame_offset += sizeof(tcg_target_long);
1636c896fe29Sbellard }
1637c896fe29Sbellard 
16387f6ceedfSAurelien Jarno /* sync register 'reg' by saving it to the corresponding temporary */
16397f6ceedfSAurelien Jarno static inline void tcg_reg_sync(TCGContext *s, int reg)
1640c896fe29Sbellard {
1641c896fe29Sbellard     TCGTemp *ts;
1642c896fe29Sbellard     int temp;
1643c896fe29Sbellard 
1644c896fe29Sbellard     temp = s->reg_to_temp[reg];
1645c896fe29Sbellard     ts = &s->temps[temp];
1646c896fe29Sbellard     assert(ts->val_type == TEMP_VAL_REG);
16477f6ceedfSAurelien Jarno     if (!ts->mem_coherent && !ts->fixed_reg) {
16487f6ceedfSAurelien Jarno         if (!ts->mem_allocated) {
1649c896fe29Sbellard             temp_allocate_frame(s, temp);
16507f6ceedfSAurelien Jarno         }
1651e4d5434cSblueswir1         tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1652c896fe29Sbellard     }
16537f6ceedfSAurelien Jarno     ts->mem_coherent = 1;
16547f6ceedfSAurelien Jarno }
16557f6ceedfSAurelien Jarno 
16567f6ceedfSAurelien Jarno /* free register 'reg' by spilling the corresponding temporary if necessary */
16577f6ceedfSAurelien Jarno static void tcg_reg_free(TCGContext *s, int reg)
16587f6ceedfSAurelien Jarno {
16597f6ceedfSAurelien Jarno     int temp;
16607f6ceedfSAurelien Jarno 
16617f6ceedfSAurelien Jarno     temp = s->reg_to_temp[reg];
16627f6ceedfSAurelien Jarno     if (temp != -1) {
16637f6ceedfSAurelien Jarno         tcg_reg_sync(s, reg);
16647f6ceedfSAurelien Jarno         s->temps[temp].val_type = TEMP_VAL_MEM;
1665c896fe29Sbellard         s->reg_to_temp[reg] = -1;
1666c896fe29Sbellard     }
1667c896fe29Sbellard }
1668c896fe29Sbellard 
1669c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */
1670c896fe29Sbellard static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
1671c896fe29Sbellard {
1672c896fe29Sbellard     int i, reg;
1673c896fe29Sbellard     TCGRegSet reg_ct;
1674c896fe29Sbellard 
1675c896fe29Sbellard     tcg_regset_andnot(reg_ct, reg1, reg2);
1676c896fe29Sbellard 
1677c896fe29Sbellard     /* first try free registers */
16780954d0d9Sblueswir1     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1679c896fe29Sbellard         reg = tcg_target_reg_alloc_order[i];
1680c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
1681c896fe29Sbellard             return reg;
1682c896fe29Sbellard     }
1683c896fe29Sbellard 
1684c896fe29Sbellard     /* XXX: do better spill choice */
16850954d0d9Sblueswir1     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1686c896fe29Sbellard         reg = tcg_target_reg_alloc_order[i];
1687c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg)) {
1688c896fe29Sbellard             tcg_reg_free(s, reg);
1689c896fe29Sbellard             return reg;
1690c896fe29Sbellard         }
1691c896fe29Sbellard     }
1692c896fe29Sbellard 
1693c896fe29Sbellard     tcg_abort();
1694c896fe29Sbellard }
1695c896fe29Sbellard 
1696639368ddSAurelien Jarno /* mark a temporary as dead. */
1697639368ddSAurelien Jarno static inline void temp_dead(TCGContext *s, int temp)
1698639368ddSAurelien Jarno {
1699639368ddSAurelien Jarno     TCGTemp *ts;
1700639368ddSAurelien Jarno 
1701639368ddSAurelien Jarno     ts = &s->temps[temp];
1702639368ddSAurelien Jarno     if (!ts->fixed_reg) {
1703639368ddSAurelien Jarno         if (ts->val_type == TEMP_VAL_REG) {
1704639368ddSAurelien Jarno             s->reg_to_temp[ts->reg] = -1;
1705639368ddSAurelien Jarno         }
1706e5138db5SAurelien Jarno         if (temp < s->nb_globals || ts->temp_local) {
1707639368ddSAurelien Jarno             ts->val_type = TEMP_VAL_MEM;
1708639368ddSAurelien Jarno         } else {
1709639368ddSAurelien Jarno             ts->val_type = TEMP_VAL_DEAD;
1710639368ddSAurelien Jarno         }
1711639368ddSAurelien Jarno     }
1712639368ddSAurelien Jarno }
1713639368ddSAurelien Jarno 
17141ad80729SAurelien Jarno /* sync a temporary to memory. 'allocated_regs' is used in case a
1715e8996ee0Sbellard    temporary registers needs to be allocated to store a constant. */
17161ad80729SAurelien Jarno static inline void temp_sync(TCGContext *s, int temp, TCGRegSet allocated_regs)
1717c896fe29Sbellard {
1718c896fe29Sbellard     TCGTemp *ts;
1719c896fe29Sbellard 
1720641d5fbeSbellard     ts = &s->temps[temp];
1721c896fe29Sbellard     if (!ts->fixed_reg) {
1722e8996ee0Sbellard         switch(ts->val_type) {
17231ad80729SAurelien Jarno         case TEMP_VAL_CONST:
17241ad80729SAurelien Jarno             ts->reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
17251ad80729SAurelien Jarno                                     allocated_regs);
17261ad80729SAurelien Jarno             ts->val_type = TEMP_VAL_REG;
17271ad80729SAurelien Jarno             s->reg_to_temp[ts->reg] = temp;
17281ad80729SAurelien Jarno             ts->mem_coherent = 0;
17291ad80729SAurelien Jarno             tcg_out_movi(s, ts->type, ts->reg, ts->val);
17301ad80729SAurelien Jarno             /* fallthrough*/
1731e8996ee0Sbellard         case TEMP_VAL_REG:
17321ad80729SAurelien Jarno             tcg_reg_sync(s, ts->reg);
1733e8996ee0Sbellard             break;
1734e8996ee0Sbellard         case TEMP_VAL_DEAD:
1735e8996ee0Sbellard         case TEMP_VAL_MEM:
1736e8996ee0Sbellard             break;
1737e8996ee0Sbellard         default:
1738e8996ee0Sbellard             tcg_abort();
1739c896fe29Sbellard         }
1740c896fe29Sbellard     }
1741c896fe29Sbellard }
1742641d5fbeSbellard 
17431ad80729SAurelien Jarno /* save a temporary to memory. 'allocated_regs' is used in case a
17441ad80729SAurelien Jarno    temporary registers needs to be allocated to store a constant. */
17451ad80729SAurelien Jarno static inline void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs)
17461ad80729SAurelien Jarno {
17472c0366f0SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
17482c0366f0SAurelien Jarno     /* The liveness analysis already ensures that globals are back
17492c0366f0SAurelien Jarno        in memory. Keep an assert for safety. */
17502c0366f0SAurelien Jarno     assert(s->temps[temp].val_type == TEMP_VAL_MEM || s->temps[temp].fixed_reg);
17512c0366f0SAurelien Jarno #else
17521ad80729SAurelien Jarno     temp_sync(s, temp, allocated_regs);
17531ad80729SAurelien Jarno     temp_dead(s, temp);
17542c0366f0SAurelien Jarno #endif
17551ad80729SAurelien Jarno }
17561ad80729SAurelien Jarno 
17579814dd27SDong Xu Wang /* save globals to their canonical location and assume they can be
1758641d5fbeSbellard    modified be the following code. 'allocated_regs' is used in case a
1759641d5fbeSbellard    temporary registers needs to be allocated to store a constant. */
1760641d5fbeSbellard static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
1761641d5fbeSbellard {
1762641d5fbeSbellard     int i;
1763641d5fbeSbellard 
1764641d5fbeSbellard     for(i = 0; i < s->nb_globals; i++) {
1765641d5fbeSbellard         temp_save(s, i, allocated_regs);
1766641d5fbeSbellard     }
1767e5097dc8Sbellard }
1768e5097dc8Sbellard 
17693d5c5f87SAurelien Jarno /* sync globals to their canonical location and assume they can be
17703d5c5f87SAurelien Jarno    read by the following code. 'allocated_regs' is used in case a
17713d5c5f87SAurelien Jarno    temporary registers needs to be allocated to store a constant. */
17723d5c5f87SAurelien Jarno static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
17733d5c5f87SAurelien Jarno {
17743d5c5f87SAurelien Jarno     int i;
17753d5c5f87SAurelien Jarno 
17763d5c5f87SAurelien Jarno     for (i = 0; i < s->nb_globals; i++) {
17773d5c5f87SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
17783d5c5f87SAurelien Jarno         assert(s->temps[i].val_type != TEMP_VAL_REG || s->temps[i].fixed_reg ||
17793d5c5f87SAurelien Jarno                s->temps[i].mem_coherent);
17803d5c5f87SAurelien Jarno #else
17813d5c5f87SAurelien Jarno         temp_sync(s, i, allocated_regs);
17823d5c5f87SAurelien Jarno #endif
17833d5c5f87SAurelien Jarno     }
17843d5c5f87SAurelien Jarno }
17853d5c5f87SAurelien Jarno 
1786e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and
1787e8996ee0Sbellard    all globals are stored at their canonical location. */
1788e8996ee0Sbellard static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
1789e5097dc8Sbellard {
1790e5097dc8Sbellard     TCGTemp *ts;
1791e5097dc8Sbellard     int i;
1792e5097dc8Sbellard 
1793c896fe29Sbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
1794c896fe29Sbellard         ts = &s->temps[i];
1795641d5fbeSbellard         if (ts->temp_local) {
1796641d5fbeSbellard             temp_save(s, i, allocated_regs);
1797641d5fbeSbellard         } else {
17982c0366f0SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
17992c0366f0SAurelien Jarno             /* The liveness analysis already ensures that temps are dead.
18002c0366f0SAurelien Jarno                Keep an assert for safety. */
18012c0366f0SAurelien Jarno             assert(ts->val_type == TEMP_VAL_DEAD);
18022c0366f0SAurelien Jarno #else
1803639368ddSAurelien Jarno             temp_dead(s, i);
18042c0366f0SAurelien Jarno #endif
1805c896fe29Sbellard         }
1806641d5fbeSbellard     }
1807e8996ee0Sbellard 
1808e8996ee0Sbellard     save_globals(s, allocated_regs);
1809c896fe29Sbellard }
1810c896fe29Sbellard 
1811866cb6cbSAurelien Jarno #define IS_DEAD_ARG(n) ((dead_args >> (n)) & 1)
1812ec7a869dSAurelien Jarno #define NEED_SYNC_ARG(n) ((sync_args >> (n)) & 1)
1813c896fe29Sbellard 
1814ec7a869dSAurelien Jarno static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
1815ec7a869dSAurelien Jarno                                uint16_t dead_args, uint8_t sync_args)
1816e8996ee0Sbellard {
1817e8996ee0Sbellard     TCGTemp *ots;
1818e8996ee0Sbellard     tcg_target_ulong val;
1819e8996ee0Sbellard 
1820e8996ee0Sbellard     ots = &s->temps[args[0]];
1821e8996ee0Sbellard     val = args[1];
1822e8996ee0Sbellard 
1823e8996ee0Sbellard     if (ots->fixed_reg) {
1824e8996ee0Sbellard         /* for fixed registers, we do not do any constant
1825e8996ee0Sbellard            propagation */
1826e8996ee0Sbellard         tcg_out_movi(s, ots->type, ots->reg, val);
1827e8996ee0Sbellard     } else {
18281235fc06Sths         /* The movi is not explicitly generated here */
1829e8996ee0Sbellard         if (ots->val_type == TEMP_VAL_REG)
1830e8996ee0Sbellard             s->reg_to_temp[ots->reg] = -1;
1831e8996ee0Sbellard         ots->val_type = TEMP_VAL_CONST;
1832e8996ee0Sbellard         ots->val = val;
1833e8996ee0Sbellard     }
1834ec7a869dSAurelien Jarno     if (NEED_SYNC_ARG(0)) {
1835ec7a869dSAurelien Jarno         temp_sync(s, args[0], s->reserved_regs);
1836ec7a869dSAurelien Jarno     }
18374c4e1ab2SAurelien Jarno     if (IS_DEAD_ARG(0)) {
18384c4e1ab2SAurelien Jarno         temp_dead(s, args[0]);
18394c4e1ab2SAurelien Jarno     }
1840e8996ee0Sbellard }
1841e8996ee0Sbellard 
1842c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1843ec7a869dSAurelien Jarno                               const TCGArg *args, uint16_t dead_args,
1844ec7a869dSAurelien Jarno                               uint8_t sync_args)
1845c896fe29Sbellard {
1846c29c1d7eSAurelien Jarno     TCGRegSet allocated_regs;
1847c896fe29Sbellard     TCGTemp *ts, *ots;
1848450445d5SRichard Henderson     TCGType otype, itype;
1849c896fe29Sbellard 
1850c29c1d7eSAurelien Jarno     tcg_regset_set(allocated_regs, s->reserved_regs);
1851c896fe29Sbellard     ots = &s->temps[args[0]];
1852c896fe29Sbellard     ts = &s->temps[args[1]];
1853450445d5SRichard Henderson 
1854450445d5SRichard Henderson     /* Note that otype != itype for no-op truncation.  */
1855450445d5SRichard Henderson     otype = ots->type;
1856450445d5SRichard Henderson     itype = ts->type;
1857c896fe29Sbellard 
1858c29c1d7eSAurelien Jarno     /* If the source value is not in a register, and we're going to be
1859c29c1d7eSAurelien Jarno        forced to have it in a register in order to perform the copy,
1860c29c1d7eSAurelien Jarno        then copy the SOURCE value into its own register first.  That way
1861c29c1d7eSAurelien Jarno        we don't have to reload SOURCE the next time it is used. */
1862c29c1d7eSAurelien Jarno     if (((NEED_SYNC_ARG(0) || ots->fixed_reg) && ts->val_type != TEMP_VAL_REG)
1863c29c1d7eSAurelien Jarno         || ts->val_type == TEMP_VAL_MEM) {
1864450445d5SRichard Henderson         ts->reg = tcg_reg_alloc(s, tcg_target_available_regs[itype],
1865af3cbfbeSRichard Henderson                                 allocated_regs);
1866c29c1d7eSAurelien Jarno         if (ts->val_type == TEMP_VAL_MEM) {
1867450445d5SRichard Henderson             tcg_out_ld(s, itype, ts->reg, ts->mem_reg, ts->mem_offset);
1868c29c1d7eSAurelien Jarno             ts->mem_coherent = 1;
1869c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1870450445d5SRichard Henderson             tcg_out_movi(s, itype, ts->reg, ts->val);
1871c29c1d7eSAurelien Jarno         }
1872c29c1d7eSAurelien Jarno         s->reg_to_temp[ts->reg] = args[1];
1873c29c1d7eSAurelien Jarno         ts->val_type = TEMP_VAL_REG;
1874c29c1d7eSAurelien Jarno     }
1875c29c1d7eSAurelien Jarno 
1876c29c1d7eSAurelien Jarno     if (IS_DEAD_ARG(0) && !ots->fixed_reg) {
1877c29c1d7eSAurelien Jarno         /* mov to a non-saved dead register makes no sense (even with
1878c29c1d7eSAurelien Jarno            liveness analysis disabled). */
1879c29c1d7eSAurelien Jarno         assert(NEED_SYNC_ARG(0));
1880c29c1d7eSAurelien Jarno         /* The code above should have moved the temp to a register. */
1881c29c1d7eSAurelien Jarno         assert(ts->val_type == TEMP_VAL_REG);
1882c29c1d7eSAurelien Jarno         if (!ots->mem_allocated) {
1883c29c1d7eSAurelien Jarno             temp_allocate_frame(s, args[0]);
1884c29c1d7eSAurelien Jarno         }
1885450445d5SRichard Henderson         tcg_out_st(s, otype, ts->reg, ots->mem_reg, ots->mem_offset);
1886c29c1d7eSAurelien Jarno         if (IS_DEAD_ARG(1)) {
1887c29c1d7eSAurelien Jarno             temp_dead(s, args[1]);
1888c29c1d7eSAurelien Jarno         }
1889c29c1d7eSAurelien Jarno         temp_dead(s, args[0]);
1890c29c1d7eSAurelien Jarno     } else if (ts->val_type == TEMP_VAL_CONST) {
1891e8996ee0Sbellard         /* propagate constant */
1892c29c1d7eSAurelien Jarno         if (ots->val_type == TEMP_VAL_REG) {
1893e8996ee0Sbellard             s->reg_to_temp[ots->reg] = -1;
1894c29c1d7eSAurelien Jarno         }
1895e8996ee0Sbellard         ots->val_type = TEMP_VAL_CONST;
1896e8996ee0Sbellard         ots->val = ts->val;
1897e8996ee0Sbellard     } else {
1898c29c1d7eSAurelien Jarno         /* The code in the first if block should have moved the
1899c29c1d7eSAurelien Jarno            temp to a register. */
1900c29c1d7eSAurelien Jarno         assert(ts->val_type == TEMP_VAL_REG);
1901c29c1d7eSAurelien Jarno         if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
1902c29c1d7eSAurelien Jarno             /* the mov can be suppressed */
1903c29c1d7eSAurelien Jarno             if (ots->val_type == TEMP_VAL_REG) {
1904c29c1d7eSAurelien Jarno                 s->reg_to_temp[ots->reg] = -1;
1905c896fe29Sbellard             }
1906c29c1d7eSAurelien Jarno             ots->reg = ts->reg;
1907c29c1d7eSAurelien Jarno             temp_dead(s, args[1]);
1908c29c1d7eSAurelien Jarno         } else {
1909c29c1d7eSAurelien Jarno             if (ots->val_type != TEMP_VAL_REG) {
1910c29c1d7eSAurelien Jarno                 /* When allocating a new register, make sure to not spill the
1911c29c1d7eSAurelien Jarno                    input one. */
1912c29c1d7eSAurelien Jarno                 tcg_regset_set_reg(allocated_regs, ts->reg);
1913450445d5SRichard Henderson                 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
1914af3cbfbeSRichard Henderson                                          allocated_regs);
1915c29c1d7eSAurelien Jarno             }
1916450445d5SRichard Henderson             tcg_out_mov(s, otype, ots->reg, ts->reg);
1917c29c1d7eSAurelien Jarno         }
1918c896fe29Sbellard         ots->val_type = TEMP_VAL_REG;
1919c896fe29Sbellard         ots->mem_coherent = 0;
1920c29c1d7eSAurelien Jarno         s->reg_to_temp[ots->reg] = args[0];
1921ec7a869dSAurelien Jarno         if (NEED_SYNC_ARG(0)) {
1922c29c1d7eSAurelien Jarno             tcg_reg_sync(s, ots->reg);
1923c29c1d7eSAurelien Jarno         }
1924ec7a869dSAurelien Jarno     }
1925c896fe29Sbellard }
1926c896fe29Sbellard 
1927c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s,
1928a9751609SRichard Henderson                              const TCGOpDef *def, TCGOpcode opc,
1929ec7a869dSAurelien Jarno                              const TCGArg *args, uint16_t dead_args,
1930ec7a869dSAurelien Jarno                              uint8_t sync_args)
1931c896fe29Sbellard {
1932c896fe29Sbellard     TCGRegSet allocated_regs;
1933c896fe29Sbellard     int i, k, nb_iargs, nb_oargs, reg;
1934c896fe29Sbellard     TCGArg arg;
1935c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1936c896fe29Sbellard     TCGTemp *ts;
1937c896fe29Sbellard     TCGArg new_args[TCG_MAX_OP_ARGS];
1938c896fe29Sbellard     int const_args[TCG_MAX_OP_ARGS];
1939c896fe29Sbellard 
1940c896fe29Sbellard     nb_oargs = def->nb_oargs;
1941c896fe29Sbellard     nb_iargs = def->nb_iargs;
1942c896fe29Sbellard 
1943c896fe29Sbellard     /* copy constants */
1944c896fe29Sbellard     memcpy(new_args + nb_oargs + nb_iargs,
1945c896fe29Sbellard            args + nb_oargs + nb_iargs,
1946c896fe29Sbellard            sizeof(TCGArg) * def->nb_cargs);
1947c896fe29Sbellard 
1948c896fe29Sbellard     /* satisfy input constraints */
1949c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
1950c896fe29Sbellard     for(k = 0; k < nb_iargs; k++) {
1951c896fe29Sbellard         i = def->sorted_args[nb_oargs + k];
1952c896fe29Sbellard         arg = args[i];
1953c896fe29Sbellard         arg_ct = &def->args_ct[i];
1954c896fe29Sbellard         ts = &s->temps[arg];
1955c896fe29Sbellard         if (ts->val_type == TEMP_VAL_MEM) {
1956c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1957e4d5434cSblueswir1             tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1958c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
1959c896fe29Sbellard             ts->reg = reg;
1960c896fe29Sbellard             ts->mem_coherent = 1;
1961c896fe29Sbellard             s->reg_to_temp[reg] = arg;
1962c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1963f6c6afc1SRichard Henderson             if (tcg_target_const_match(ts->val, ts->type, arg_ct)) {
1964c896fe29Sbellard                 /* constant is OK for instruction */
1965c896fe29Sbellard                 const_args[i] = 1;
1966c896fe29Sbellard                 new_args[i] = ts->val;
1967c896fe29Sbellard                 goto iarg_end;
1968c896fe29Sbellard             } else {
1969c896fe29Sbellard                 /* need to move to a register */
1970c896fe29Sbellard                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1971c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
1972e8996ee0Sbellard                 ts->val_type = TEMP_VAL_REG;
1973e8996ee0Sbellard                 ts->reg = reg;
1974e8996ee0Sbellard                 ts->mem_coherent = 0;
1975e8996ee0Sbellard                 s->reg_to_temp[reg] = arg;
1976c896fe29Sbellard             }
1977c896fe29Sbellard         }
1978c896fe29Sbellard         assert(ts->val_type == TEMP_VAL_REG);
19795ff9d6a4Sbellard         if (arg_ct->ct & TCG_CT_IALIAS) {
19805ff9d6a4Sbellard             if (ts->fixed_reg) {
19815ff9d6a4Sbellard                 /* if fixed register, we must allocate a new register
19825ff9d6a4Sbellard                    if the alias is not the same register */
19835ff9d6a4Sbellard                 if (arg != args[arg_ct->alias_index])
19845ff9d6a4Sbellard                     goto allocate_in_reg;
19855ff9d6a4Sbellard             } else {
1986c896fe29Sbellard                 /* if the input is aliased to an output and if it is
1987c896fe29Sbellard                    not dead after the instruction, we must allocate
1988c896fe29Sbellard                    a new register and move it */
1989866cb6cbSAurelien Jarno                 if (!IS_DEAD_ARG(i)) {
1990c896fe29Sbellard                     goto allocate_in_reg;
1991c896fe29Sbellard                 }
19925ff9d6a4Sbellard             }
1993866cb6cbSAurelien Jarno         }
1994c896fe29Sbellard         reg = ts->reg;
1995c896fe29Sbellard         if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1996c896fe29Sbellard             /* nothing to do : the constraint is satisfied */
1997c896fe29Sbellard         } else {
1998c896fe29Sbellard         allocate_in_reg:
1999c896fe29Sbellard             /* allocate a new register matching the constraint
2000c896fe29Sbellard                and move the temporary register into it */
2001c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
20023b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, reg, ts->reg);
2003c896fe29Sbellard         }
2004c896fe29Sbellard         new_args[i] = reg;
2005c896fe29Sbellard         const_args[i] = 0;
2006c896fe29Sbellard         tcg_regset_set_reg(allocated_regs, reg);
2007c896fe29Sbellard     iarg_end: ;
2008c896fe29Sbellard     }
2009c896fe29Sbellard 
2010c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
2011866cb6cbSAurelien Jarno     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2012866cb6cbSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2013639368ddSAurelien Jarno             temp_dead(s, args[i]);
2014c896fe29Sbellard         }
2015c896fe29Sbellard     }
2016c896fe29Sbellard 
2017a52ad07eSAurelien Jarno     if (def->flags & TCG_OPF_BB_END) {
2018a52ad07eSAurelien Jarno         tcg_reg_alloc_bb_end(s, allocated_regs);
2019a52ad07eSAurelien Jarno     } else {
2020c896fe29Sbellard         if (def->flags & TCG_OPF_CALL_CLOBBER) {
2021b03cce8eSbellard             /* XXX: permit generic clobber register list ? */
2022c896fe29Sbellard             for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2023c896fe29Sbellard                 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
2024c896fe29Sbellard                     tcg_reg_free(s, reg);
2025c896fe29Sbellard                 }
2026c896fe29Sbellard             }
20273d5c5f87SAurelien Jarno         }
20283d5c5f87SAurelien Jarno         if (def->flags & TCG_OPF_SIDE_EFFECTS) {
20293d5c5f87SAurelien Jarno             /* sync globals if the op has side effects and might trigger
20303d5c5f87SAurelien Jarno                an exception. */
20313d5c5f87SAurelien Jarno             sync_globals(s, allocated_regs);
2032c896fe29Sbellard         }
2033c896fe29Sbellard 
2034c896fe29Sbellard         /* satisfy the output constraints */
2035c896fe29Sbellard         tcg_regset_set(allocated_regs, s->reserved_regs);
2036c896fe29Sbellard         for(k = 0; k < nb_oargs; k++) {
2037c896fe29Sbellard             i = def->sorted_args[k];
2038c896fe29Sbellard             arg = args[i];
2039c896fe29Sbellard             arg_ct = &def->args_ct[i];
2040c896fe29Sbellard             ts = &s->temps[arg];
2041c896fe29Sbellard             if (arg_ct->ct & TCG_CT_ALIAS) {
20425ff9d6a4Sbellard                 reg = new_args[arg_ct->alias_index];
2043c896fe29Sbellard             } else {
2044c896fe29Sbellard                 /* if fixed register, we try to use it */
2045c896fe29Sbellard                 reg = ts->reg;
2046c896fe29Sbellard                 if (ts->fixed_reg &&
2047c896fe29Sbellard                     tcg_regset_test_reg(arg_ct->u.regs, reg)) {
2048c896fe29Sbellard                     goto oarg_end;
2049c896fe29Sbellard                 }
2050c896fe29Sbellard                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2051c896fe29Sbellard             }
2052c896fe29Sbellard             tcg_regset_set_reg(allocated_regs, reg);
2053c896fe29Sbellard             /* if a fixed register is used, then a move will be done afterwards */
2054c896fe29Sbellard             if (!ts->fixed_reg) {
2055639368ddSAurelien Jarno                 if (ts->val_type == TEMP_VAL_REG) {
2056639368ddSAurelien Jarno                     s->reg_to_temp[ts->reg] = -1;
2057639368ddSAurelien Jarno                 }
2058c896fe29Sbellard                 ts->val_type = TEMP_VAL_REG;
2059c896fe29Sbellard                 ts->reg = reg;
2060c896fe29Sbellard                 /* temp value is modified, so the value kept in memory is
2061c896fe29Sbellard                    potentially not the same */
2062c896fe29Sbellard                 ts->mem_coherent = 0;
2063c896fe29Sbellard                 s->reg_to_temp[reg] = arg;
2064c896fe29Sbellard             }
2065c896fe29Sbellard         oarg_end:
2066c896fe29Sbellard             new_args[i] = reg;
2067c896fe29Sbellard         }
2068e8996ee0Sbellard     }
2069c896fe29Sbellard 
2070c896fe29Sbellard     /* emit instruction */
2071c896fe29Sbellard     tcg_out_op(s, opc, new_args, const_args);
2072c896fe29Sbellard 
2073c896fe29Sbellard     /* move the outputs in the correct register if needed */
2074c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
2075c896fe29Sbellard         ts = &s->temps[args[i]];
2076c896fe29Sbellard         reg = new_args[i];
2077c896fe29Sbellard         if (ts->fixed_reg && ts->reg != reg) {
20783b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, ts->reg, reg);
2079c896fe29Sbellard         }
2080ec7a869dSAurelien Jarno         if (NEED_SYNC_ARG(i)) {
2081ec7a869dSAurelien Jarno             tcg_reg_sync(s, reg);
2082ec7a869dSAurelien Jarno         }
2083ec7a869dSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2084ec7a869dSAurelien Jarno             temp_dead(s, args[i]);
2085ec7a869dSAurelien Jarno         }
2086c896fe29Sbellard     }
2087c896fe29Sbellard }
2088c896fe29Sbellard 
2089b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP
2090b03cce8eSbellard #define STACK_DIR(x) (-(x))
2091b03cce8eSbellard #else
2092b03cce8eSbellard #define STACK_DIR(x) (x)
2093b03cce8eSbellard #endif
2094b03cce8eSbellard 
2095c45cb8bbSRichard Henderson static void tcg_reg_alloc_call(TCGContext *s, int nb_oargs, int nb_iargs,
2096c45cb8bbSRichard Henderson                                const TCGArg * const args, uint16_t dead_args,
2097c45cb8bbSRichard Henderson                                uint8_t sync_args)
2098c896fe29Sbellard {
2099c45cb8bbSRichard Henderson     int flags, nb_regs, i, reg;
2100cf066674SRichard Henderson     TCGArg arg;
2101c896fe29Sbellard     TCGTemp *ts;
2102d3452f1fSRichard Henderson     intptr_t stack_offset;
2103d3452f1fSRichard Henderson     size_t call_stack_size;
2104cf066674SRichard Henderson     tcg_insn_unit *func_addr;
2105cf066674SRichard Henderson     int allocate_args;
2106c896fe29Sbellard     TCGRegSet allocated_regs;
2107c896fe29Sbellard 
2108cf066674SRichard Henderson     func_addr = (tcg_insn_unit *)(intptr_t)args[nb_oargs + nb_iargs];
2109cf066674SRichard Henderson     flags = args[nb_oargs + nb_iargs + 1];
2110c896fe29Sbellard 
21116e17d0c5SStefan Weil     nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2112c45cb8bbSRichard Henderson     if (nb_regs > nb_iargs) {
2113c45cb8bbSRichard Henderson         nb_regs = nb_iargs;
2114cf066674SRichard Henderson     }
2115c896fe29Sbellard 
2116c896fe29Sbellard     /* assign stack slots first */
2117c45cb8bbSRichard Henderson     call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
2118c896fe29Sbellard     call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
2119c896fe29Sbellard         ~(TCG_TARGET_STACK_ALIGN - 1);
2120b03cce8eSbellard     allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
2121b03cce8eSbellard     if (allocate_args) {
2122345649c0SBlue Swirl         /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
2123345649c0SBlue Swirl            preallocate call stack */
2124345649c0SBlue Swirl         tcg_abort();
2125b03cce8eSbellard     }
212639cf05d3Sbellard 
212739cf05d3Sbellard     stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
2128c45cb8bbSRichard Henderson     for(i = nb_regs; i < nb_iargs; i++) {
2129c896fe29Sbellard         arg = args[nb_oargs + i];
213039cf05d3Sbellard #ifdef TCG_TARGET_STACK_GROWSUP
213139cf05d3Sbellard         stack_offset -= sizeof(tcg_target_long);
213239cf05d3Sbellard #endif
213339cf05d3Sbellard         if (arg != TCG_CALL_DUMMY_ARG) {
2134c896fe29Sbellard             ts = &s->temps[arg];
2135c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
2136e4d5434cSblueswir1                 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
2137c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_MEM) {
2138c896fe29Sbellard                 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
2139c896fe29Sbellard                                     s->reserved_regs);
2140c896fe29Sbellard                 /* XXX: not correct if reading values from the stack */
2141e4d5434cSblueswir1                 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2142e4d5434cSblueswir1                 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
2143c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_CONST) {
2144c896fe29Sbellard                 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
2145c896fe29Sbellard                                     s->reserved_regs);
2146c896fe29Sbellard                 /* XXX: sign extend may be needed on some targets */
2147c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
2148e4d5434cSblueswir1                 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
2149c896fe29Sbellard             } else {
2150c896fe29Sbellard                 tcg_abort();
2151c896fe29Sbellard             }
215239cf05d3Sbellard         }
215339cf05d3Sbellard #ifndef TCG_TARGET_STACK_GROWSUP
215439cf05d3Sbellard         stack_offset += sizeof(tcg_target_long);
215539cf05d3Sbellard #endif
2156c896fe29Sbellard     }
2157c896fe29Sbellard 
2158c896fe29Sbellard     /* assign input registers */
2159c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
2160c896fe29Sbellard     for(i = 0; i < nb_regs; i++) {
2161c896fe29Sbellard         arg = args[nb_oargs + i];
216239cf05d3Sbellard         if (arg != TCG_CALL_DUMMY_ARG) {
2163c896fe29Sbellard             ts = &s->temps[arg];
2164c896fe29Sbellard             reg = tcg_target_call_iarg_regs[i];
2165c896fe29Sbellard             tcg_reg_free(s, reg);
2166c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
2167c896fe29Sbellard                 if (ts->reg != reg) {
21683b6dac34SRichard Henderson                     tcg_out_mov(s, ts->type, reg, ts->reg);
2169c896fe29Sbellard                 }
2170c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_MEM) {
2171e4d5434cSblueswir1                 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2172c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_CONST) {
2173c896fe29Sbellard                 /* XXX: sign extend ? */
2174c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
2175c896fe29Sbellard             } else {
2176c896fe29Sbellard                 tcg_abort();
2177c896fe29Sbellard             }
2178c896fe29Sbellard             tcg_regset_set_reg(allocated_regs, reg);
2179c896fe29Sbellard         }
218039cf05d3Sbellard     }
2181c896fe29Sbellard 
2182c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
2183866cb6cbSAurelien Jarno     for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2184866cb6cbSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2185639368ddSAurelien Jarno             temp_dead(s, args[i]);
2186c896fe29Sbellard         }
2187c896fe29Sbellard     }
2188c896fe29Sbellard 
2189c896fe29Sbellard     /* clobber call registers */
2190c896fe29Sbellard     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2191c896fe29Sbellard         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
2192c896fe29Sbellard             tcg_reg_free(s, reg);
2193c896fe29Sbellard         }
2194c896fe29Sbellard     }
2195c896fe29Sbellard 
219678505279SAurelien Jarno     /* Save globals if they might be written by the helper, sync them if
219778505279SAurelien Jarno        they might be read. */
219878505279SAurelien Jarno     if (flags & TCG_CALL_NO_READ_GLOBALS) {
219978505279SAurelien Jarno         /* Nothing to do */
220078505279SAurelien Jarno     } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
220178505279SAurelien Jarno         sync_globals(s, allocated_regs);
220278505279SAurelien Jarno     } else {
2203e8996ee0Sbellard         save_globals(s, allocated_regs);
2204b9c18f56Saurel32     }
2205c896fe29Sbellard 
2206cf066674SRichard Henderson     tcg_out_call(s, func_addr);
2207c896fe29Sbellard 
2208c896fe29Sbellard     /* assign output registers and emit moves if needed */
2209c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
2210c896fe29Sbellard         arg = args[i];
2211c896fe29Sbellard         ts = &s->temps[arg];
2212c896fe29Sbellard         reg = tcg_target_call_oarg_regs[i];
2213e8996ee0Sbellard         assert(s->reg_to_temp[reg] == -1);
221434b1a49cSRichard Henderson 
2215c896fe29Sbellard         if (ts->fixed_reg) {
2216c896fe29Sbellard             if (ts->reg != reg) {
22173b6dac34SRichard Henderson                 tcg_out_mov(s, ts->type, ts->reg, reg);
2218c896fe29Sbellard             }
2219c896fe29Sbellard         } else {
2220639368ddSAurelien Jarno             if (ts->val_type == TEMP_VAL_REG) {
2221639368ddSAurelien Jarno                 s->reg_to_temp[ts->reg] = -1;
2222639368ddSAurelien Jarno             }
2223c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
2224c896fe29Sbellard             ts->reg = reg;
2225c896fe29Sbellard             ts->mem_coherent = 0;
2226c896fe29Sbellard             s->reg_to_temp[reg] = arg;
2227ec7a869dSAurelien Jarno             if (NEED_SYNC_ARG(i)) {
2228ec7a869dSAurelien Jarno                 tcg_reg_sync(s, reg);
2229ec7a869dSAurelien Jarno             }
2230ec7a869dSAurelien Jarno             if (IS_DEAD_ARG(i)) {
2231ec7a869dSAurelien Jarno                 temp_dead(s, args[i]);
2232c896fe29Sbellard             }
2233c896fe29Sbellard         }
22348c11ad25SAurelien Jarno     }
2235c896fe29Sbellard }
2236c896fe29Sbellard 
2237c896fe29Sbellard #ifdef CONFIG_PROFILER
2238c896fe29Sbellard 
223954604f74Saurel32 static int64_t tcg_table_op_count[NB_OPS];
2240c896fe29Sbellard 
2241246ae24dSMax Filippov void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
2242c896fe29Sbellard {
2243c896fe29Sbellard     int i;
2244d70724ceSzhanghailiang 
2245*15fc7daaSRichard Henderson     for (i = 0; i < NB_OPS; i++) {
2246246ae24dSMax Filippov         cpu_fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name,
2247246ae24dSMax Filippov                     tcg_table_op_count[i]);
2248c896fe29Sbellard     }
2249c896fe29Sbellard }
2250246ae24dSMax Filippov #else
2251246ae24dSMax Filippov void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
2252246ae24dSMax Filippov {
2253246ae24dSMax Filippov     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2254246ae24dSMax Filippov }
2255c896fe29Sbellard #endif
2256c896fe29Sbellard 
2257c896fe29Sbellard 
22581813e175SRichard Henderson static inline int tcg_gen_code_common(TCGContext *s,
22591813e175SRichard Henderson                                       tcg_insn_unit *gen_code_buf,
22602ba1eeb6Spbrook                                       long search_pc)
2261c896fe29Sbellard {
2262c45cb8bbSRichard Henderson     int oi, oi_next;
2263c896fe29Sbellard 
2264c896fe29Sbellard #ifdef DEBUG_DISAS
22658fec2b8cSaliguori     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
226693fcfe39Saliguori         qemu_log("OP:\n");
2267eeacee4dSBlue Swirl         tcg_dump_ops(s);
226893fcfe39Saliguori         qemu_log("\n");
2269c896fe29Sbellard     }
2270c896fe29Sbellard #endif
2271c896fe29Sbellard 
2272c5cc28ffSAurelien Jarno #ifdef CONFIG_PROFILER
2273c5cc28ffSAurelien Jarno     s->opt_time -= profile_getclock();
2274c5cc28ffSAurelien Jarno #endif
2275c5cc28ffSAurelien Jarno 
22768f2e8c07SKirill Batuzov #ifdef USE_TCG_OPTIMIZATIONS
2277c45cb8bbSRichard Henderson     tcg_optimize(s);
22788f2e8c07SKirill Batuzov #endif
22798f2e8c07SKirill Batuzov 
2280a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2281c5cc28ffSAurelien Jarno     s->opt_time += profile_getclock();
2282a23a9ec6Sbellard     s->la_time -= profile_getclock();
2283a23a9ec6Sbellard #endif
2284c5cc28ffSAurelien Jarno 
2285c896fe29Sbellard     tcg_liveness_analysis(s);
2286c5cc28ffSAurelien Jarno 
2287a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2288a23a9ec6Sbellard     s->la_time += profile_getclock();
2289a23a9ec6Sbellard #endif
2290c896fe29Sbellard 
2291c896fe29Sbellard #ifdef DEBUG_DISAS
22928fec2b8cSaliguori     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) {
2293c5cc28ffSAurelien Jarno         qemu_log("OP after optimization and liveness analysis:\n");
2294eeacee4dSBlue Swirl         tcg_dump_ops(s);
229593fcfe39Saliguori         qemu_log("\n");
2296c896fe29Sbellard     }
2297c896fe29Sbellard #endif
2298c896fe29Sbellard 
2299c896fe29Sbellard     tcg_reg_alloc_start(s);
2300c896fe29Sbellard 
2301c896fe29Sbellard     s->code_buf = gen_code_buf;
2302c896fe29Sbellard     s->code_ptr = gen_code_buf;
2303c896fe29Sbellard 
23049ecefc84SRichard Henderson     tcg_out_tb_init(s);
23059ecefc84SRichard Henderson 
2306c45cb8bbSRichard Henderson     for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
2307c45cb8bbSRichard Henderson         TCGOp * const op = &s->gen_op_buf[oi];
2308c45cb8bbSRichard Henderson         TCGArg * const args = &s->gen_opparam_buf[op->args];
2309c45cb8bbSRichard Henderson         TCGOpcode opc = op->opc;
2310c45cb8bbSRichard Henderson         const TCGOpDef *def = &tcg_op_defs[opc];
2311c45cb8bbSRichard Henderson         uint16_t dead_args = s->op_dead_args[oi];
2312c45cb8bbSRichard Henderson         uint8_t sync_args = s->op_sync_args[oi];
2313b3db8758Sblueswir1 
2314c45cb8bbSRichard Henderson         oi_next = op->next;
2315c896fe29Sbellard #ifdef CONFIG_PROFILER
231654604f74Saurel32         tcg_table_op_count[opc]++;
2317c896fe29Sbellard #endif
2318c45cb8bbSRichard Henderson 
2319c896fe29Sbellard         switch (opc) {
2320c896fe29Sbellard         case INDEX_op_mov_i32:
2321c896fe29Sbellard         case INDEX_op_mov_i64:
2322c45cb8bbSRichard Henderson             tcg_reg_alloc_mov(s, def, args, dead_args, sync_args);
2323c896fe29Sbellard             break;
2324e8996ee0Sbellard         case INDEX_op_movi_i32:
2325e8996ee0Sbellard         case INDEX_op_movi_i64:
2326c45cb8bbSRichard Henderson             tcg_reg_alloc_movi(s, args, dead_args, sync_args);
2327e8996ee0Sbellard             break;
23287e4597d7Sbellard         case INDEX_op_debug_insn_start:
2329c896fe29Sbellard             break;
23305ff9d6a4Sbellard         case INDEX_op_discard:
2331639368ddSAurelien Jarno             temp_dead(s, args[0]);
23325ff9d6a4Sbellard             break;
2333c896fe29Sbellard         case INDEX_op_set_label:
2334e8996ee0Sbellard             tcg_reg_alloc_bb_end(s, s->reserved_regs);
23359d6fca70SStefan Weil             tcg_out_label(s, args[0], s->code_ptr);
2336c896fe29Sbellard             break;
2337c896fe29Sbellard         case INDEX_op_call:
2338c45cb8bbSRichard Henderson             tcg_reg_alloc_call(s, op->callo, op->calli, args,
2339c45cb8bbSRichard Henderson                                dead_args, sync_args);
2340c45cb8bbSRichard Henderson             break;
2341c896fe29Sbellard         default:
234225c4d9ccSRichard Henderson             /* Sanity check that we've not introduced any unhandled opcodes. */
234325c4d9ccSRichard Henderson             if (def->flags & TCG_OPF_NOT_PRESENT) {
234425c4d9ccSRichard Henderson                 tcg_abort();
234525c4d9ccSRichard Henderson             }
2346c896fe29Sbellard             /* Note: in order to speed up the code, it would be much
2347c896fe29Sbellard                faster to have specialized register allocator functions for
2348c896fe29Sbellard                some common argument patterns */
2349c45cb8bbSRichard Henderson             tcg_reg_alloc_op(s, def, opc, args, dead_args, sync_args);
2350c896fe29Sbellard             break;
2351c896fe29Sbellard         }
23521813e175SRichard Henderson         if (search_pc >= 0 && search_pc < tcg_current_code_size(s)) {
2353c45cb8bbSRichard Henderson             return oi;
2354c896fe29Sbellard         }
2355c896fe29Sbellard #ifndef NDEBUG
2356c896fe29Sbellard         check_regs(s);
2357c896fe29Sbellard #endif
2358c896fe29Sbellard     }
2359c45cb8bbSRichard Henderson 
2360b76f0d8cSYeongkyoon Lee     /* Generate TB finalization at the end of block */
2361b76f0d8cSYeongkyoon Lee     tcg_out_tb_finalize(s);
2362c896fe29Sbellard     return -1;
2363c896fe29Sbellard }
2364c896fe29Sbellard 
23651813e175SRichard Henderson int tcg_gen_code(TCGContext *s, tcg_insn_unit *gen_code_buf)
2366c896fe29Sbellard {
2367c896fe29Sbellard #ifdef CONFIG_PROFILER
2368c896fe29Sbellard     {
2369c896fe29Sbellard         int n;
2370a23a9ec6Sbellard 
2371c45cb8bbSRichard Henderson         n = s->gen_last_op_idx + 1;
2372c45cb8bbSRichard Henderson         s->op_count += n;
2373c45cb8bbSRichard Henderson         if (n > s->op_count_max) {
2374c45cb8bbSRichard Henderson             s->op_count_max = n;
2375c45cb8bbSRichard Henderson         }
2376c45cb8bbSRichard Henderson 
2377c45cb8bbSRichard Henderson         n = s->nb_temps;
2378c45cb8bbSRichard Henderson         s->temp_count += n;
2379c45cb8bbSRichard Henderson         if (n > s->temp_count_max) {
2380c45cb8bbSRichard Henderson             s->temp_count_max = n;
2381c45cb8bbSRichard Henderson         }
2382c896fe29Sbellard     }
2383c896fe29Sbellard #endif
2384c896fe29Sbellard 
23852ba1eeb6Spbrook     tcg_gen_code_common(s, gen_code_buf, -1);
2386c896fe29Sbellard 
2387c896fe29Sbellard     /* flush instruction cache */
23881813e175SRichard Henderson     flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
23892aeabc08SStefan Weil 
23901813e175SRichard Henderson     return tcg_current_code_size(s);
2391c896fe29Sbellard }
2392c896fe29Sbellard 
23932ba1eeb6Spbrook /* Return the index of the micro operation such as the pc after is <
2394623e265cSpbrook    offset bytes from the start of the TB.  The contents of gen_code_buf must
2395623e265cSpbrook    not be changed, though writing the same values is ok.
2396623e265cSpbrook    Return -1 if not found. */
23971813e175SRichard Henderson int tcg_gen_code_search_pc(TCGContext *s, tcg_insn_unit *gen_code_buf,
23981813e175SRichard Henderson                            long offset)
2399c896fe29Sbellard {
2400623e265cSpbrook     return tcg_gen_code_common(s, gen_code_buf, offset);
2401c896fe29Sbellard }
2402a23a9ec6Sbellard 
2403a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2404405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2405a23a9ec6Sbellard {
2406a23a9ec6Sbellard     TCGContext *s = &tcg_ctx;
2407a23a9ec6Sbellard     int64_t tot;
2408a23a9ec6Sbellard 
2409a23a9ec6Sbellard     tot = s->interm_time + s->code_time;
2410a23a9ec6Sbellard     cpu_fprintf(f, "JIT cycles          %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2411a23a9ec6Sbellard                 tot, tot / 2.4e9);
2412a23a9ec6Sbellard     cpu_fprintf(f, "translated TBs      %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
2413a23a9ec6Sbellard                 s->tb_count,
2414a23a9ec6Sbellard                 s->tb_count1 - s->tb_count,
2415a23a9ec6Sbellard                 s->tb_count1 ? (double)(s->tb_count1 - s->tb_count) / s->tb_count1 * 100.0 : 0);
2416a23a9ec6Sbellard     cpu_fprintf(f, "avg ops/TB          %0.1f max=%d\n",
2417a23a9ec6Sbellard                 s->tb_count ? (double)s->op_count / s->tb_count : 0, s->op_count_max);
2418a23a9ec6Sbellard     cpu_fprintf(f, "deleted ops/TB      %0.2f\n",
2419a23a9ec6Sbellard                 s->tb_count ?
2420a23a9ec6Sbellard                 (double)s->del_op_count / s->tb_count : 0);
2421a23a9ec6Sbellard     cpu_fprintf(f, "avg temps/TB        %0.2f max=%d\n",
2422a23a9ec6Sbellard                 s->tb_count ?
2423a23a9ec6Sbellard                 (double)s->temp_count / s->tb_count : 0,
2424a23a9ec6Sbellard                 s->temp_count_max);
2425a23a9ec6Sbellard 
2426a23a9ec6Sbellard     cpu_fprintf(f, "cycles/op           %0.1f\n",
2427a23a9ec6Sbellard                 s->op_count ? (double)tot / s->op_count : 0);
2428a23a9ec6Sbellard     cpu_fprintf(f, "cycles/in byte      %0.1f\n",
2429a23a9ec6Sbellard                 s->code_in_len ? (double)tot / s->code_in_len : 0);
2430a23a9ec6Sbellard     cpu_fprintf(f, "cycles/out byte     %0.1f\n",
2431a23a9ec6Sbellard                 s->code_out_len ? (double)tot / s->code_out_len : 0);
2432a23a9ec6Sbellard     if (tot == 0)
2433a23a9ec6Sbellard         tot = 1;
2434a23a9ec6Sbellard     cpu_fprintf(f, "  gen_interm time   %0.1f%%\n",
2435a23a9ec6Sbellard                 (double)s->interm_time / tot * 100.0);
2436a23a9ec6Sbellard     cpu_fprintf(f, "  gen_code time     %0.1f%%\n",
2437a23a9ec6Sbellard                 (double)s->code_time / tot * 100.0);
2438c5cc28ffSAurelien Jarno     cpu_fprintf(f, "optim./code time    %0.1f%%\n",
2439c5cc28ffSAurelien Jarno                 (double)s->opt_time / (s->code_time ? s->code_time : 1)
2440c5cc28ffSAurelien Jarno                 * 100.0);
2441a23a9ec6Sbellard     cpu_fprintf(f, "liveness/code time  %0.1f%%\n",
2442a23a9ec6Sbellard                 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
2443a23a9ec6Sbellard     cpu_fprintf(f, "cpu_restore count   %" PRId64 "\n",
2444a23a9ec6Sbellard                 s->restore_count);
2445a23a9ec6Sbellard     cpu_fprintf(f, "  avg cycles        %0.1f\n",
2446a23a9ec6Sbellard                 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
2447a23a9ec6Sbellard }
2448a23a9ec6Sbellard #else
2449405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2450a23a9ec6Sbellard {
245124bf7b3aSbellard     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2452a23a9ec6Sbellard }
2453a23a9ec6Sbellard #endif
2454813da627SRichard Henderson 
2455813da627SRichard Henderson #ifdef ELF_HOST_MACHINE
24565872bbf2SRichard Henderson /* In order to use this feature, the backend needs to do three things:
24575872bbf2SRichard Henderson 
24585872bbf2SRichard Henderson    (1) Define ELF_HOST_MACHINE to indicate both what value to
24595872bbf2SRichard Henderson        put into the ELF image and to indicate support for the feature.
24605872bbf2SRichard Henderson 
24615872bbf2SRichard Henderson    (2) Define tcg_register_jit.  This should create a buffer containing
24625872bbf2SRichard Henderson        the contents of a .debug_frame section that describes the post-
24635872bbf2SRichard Henderson        prologue unwind info for the tcg machine.
24645872bbf2SRichard Henderson 
24655872bbf2SRichard Henderson    (3) Call tcg_register_jit_int, with the constructed .debug_frame.
24665872bbf2SRichard Henderson */
2467813da627SRichard Henderson 
2468813da627SRichard Henderson /* Begin GDB interface.  THE FOLLOWING MUST MATCH GDB DOCS.  */
2469813da627SRichard Henderson typedef enum {
2470813da627SRichard Henderson     JIT_NOACTION = 0,
2471813da627SRichard Henderson     JIT_REGISTER_FN,
2472813da627SRichard Henderson     JIT_UNREGISTER_FN
2473813da627SRichard Henderson } jit_actions_t;
2474813da627SRichard Henderson 
2475813da627SRichard Henderson struct jit_code_entry {
2476813da627SRichard Henderson     struct jit_code_entry *next_entry;
2477813da627SRichard Henderson     struct jit_code_entry *prev_entry;
2478813da627SRichard Henderson     const void *symfile_addr;
2479813da627SRichard Henderson     uint64_t symfile_size;
2480813da627SRichard Henderson };
2481813da627SRichard Henderson 
2482813da627SRichard Henderson struct jit_descriptor {
2483813da627SRichard Henderson     uint32_t version;
2484813da627SRichard Henderson     uint32_t action_flag;
2485813da627SRichard Henderson     struct jit_code_entry *relevant_entry;
2486813da627SRichard Henderson     struct jit_code_entry *first_entry;
2487813da627SRichard Henderson };
2488813da627SRichard Henderson 
2489813da627SRichard Henderson void __jit_debug_register_code(void) __attribute__((noinline));
2490813da627SRichard Henderson void __jit_debug_register_code(void)
2491813da627SRichard Henderson {
2492813da627SRichard Henderson     asm("");
2493813da627SRichard Henderson }
2494813da627SRichard Henderson 
2495813da627SRichard Henderson /* Must statically initialize the version, because GDB may check
2496813da627SRichard Henderson    the version before we can set it.  */
2497813da627SRichard Henderson struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
2498813da627SRichard Henderson 
2499813da627SRichard Henderson /* End GDB interface.  */
2500813da627SRichard Henderson 
2501813da627SRichard Henderson static int find_string(const char *strtab, const char *str)
2502813da627SRichard Henderson {
2503813da627SRichard Henderson     const char *p = strtab + 1;
2504813da627SRichard Henderson 
2505813da627SRichard Henderson     while (1) {
2506813da627SRichard Henderson         if (strcmp(p, str) == 0) {
2507813da627SRichard Henderson             return p - strtab;
2508813da627SRichard Henderson         }
2509813da627SRichard Henderson         p += strlen(p) + 1;
2510813da627SRichard Henderson     }
2511813da627SRichard Henderson }
2512813da627SRichard Henderson 
25135872bbf2SRichard Henderson static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
25142c90784aSRichard Henderson                                  const void *debug_frame,
25152c90784aSRichard Henderson                                  size_t debug_frame_size)
2516813da627SRichard Henderson {
25175872bbf2SRichard Henderson     struct __attribute__((packed)) DebugInfo {
25185872bbf2SRichard Henderson         uint32_t  len;
25195872bbf2SRichard Henderson         uint16_t  version;
25205872bbf2SRichard Henderson         uint32_t  abbrev;
25215872bbf2SRichard Henderson         uint8_t   ptr_size;
25225872bbf2SRichard Henderson         uint8_t   cu_die;
25235872bbf2SRichard Henderson         uint16_t  cu_lang;
25245872bbf2SRichard Henderson         uintptr_t cu_low_pc;
25255872bbf2SRichard Henderson         uintptr_t cu_high_pc;
25265872bbf2SRichard Henderson         uint8_t   fn_die;
25275872bbf2SRichard Henderson         char      fn_name[16];
25285872bbf2SRichard Henderson         uintptr_t fn_low_pc;
25295872bbf2SRichard Henderson         uintptr_t fn_high_pc;
25305872bbf2SRichard Henderson         uint8_t   cu_eoc;
25315872bbf2SRichard Henderson     };
2532813da627SRichard Henderson 
2533813da627SRichard Henderson     struct ElfImage {
2534813da627SRichard Henderson         ElfW(Ehdr) ehdr;
2535813da627SRichard Henderson         ElfW(Phdr) phdr;
25365872bbf2SRichard Henderson         ElfW(Shdr) shdr[7];
25375872bbf2SRichard Henderson         ElfW(Sym)  sym[2];
25385872bbf2SRichard Henderson         struct DebugInfo di;
25395872bbf2SRichard Henderson         uint8_t    da[24];
25405872bbf2SRichard Henderson         char       str[80];
25415872bbf2SRichard Henderson     };
25425872bbf2SRichard Henderson 
25435872bbf2SRichard Henderson     struct ElfImage *img;
25445872bbf2SRichard Henderson 
25455872bbf2SRichard Henderson     static const struct ElfImage img_template = {
25465872bbf2SRichard Henderson         .ehdr = {
25475872bbf2SRichard Henderson             .e_ident[EI_MAG0] = ELFMAG0,
25485872bbf2SRichard Henderson             .e_ident[EI_MAG1] = ELFMAG1,
25495872bbf2SRichard Henderson             .e_ident[EI_MAG2] = ELFMAG2,
25505872bbf2SRichard Henderson             .e_ident[EI_MAG3] = ELFMAG3,
25515872bbf2SRichard Henderson             .e_ident[EI_CLASS] = ELF_CLASS,
25525872bbf2SRichard Henderson             .e_ident[EI_DATA] = ELF_DATA,
25535872bbf2SRichard Henderson             .e_ident[EI_VERSION] = EV_CURRENT,
25545872bbf2SRichard Henderson             .e_type = ET_EXEC,
25555872bbf2SRichard Henderson             .e_machine = ELF_HOST_MACHINE,
25565872bbf2SRichard Henderson             .e_version = EV_CURRENT,
25575872bbf2SRichard Henderson             .e_phoff = offsetof(struct ElfImage, phdr),
25585872bbf2SRichard Henderson             .e_shoff = offsetof(struct ElfImage, shdr),
25595872bbf2SRichard Henderson             .e_ehsize = sizeof(ElfW(Shdr)),
25605872bbf2SRichard Henderson             .e_phentsize = sizeof(ElfW(Phdr)),
25615872bbf2SRichard Henderson             .e_phnum = 1,
25625872bbf2SRichard Henderson             .e_shentsize = sizeof(ElfW(Shdr)),
25635872bbf2SRichard Henderson             .e_shnum = ARRAY_SIZE(img->shdr),
25645872bbf2SRichard Henderson             .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
2565abbb3eaeSRichard Henderson #ifdef ELF_HOST_FLAGS
2566abbb3eaeSRichard Henderson             .e_flags = ELF_HOST_FLAGS,
2567abbb3eaeSRichard Henderson #endif
2568abbb3eaeSRichard Henderson #ifdef ELF_OSABI
2569abbb3eaeSRichard Henderson             .e_ident[EI_OSABI] = ELF_OSABI,
2570abbb3eaeSRichard Henderson #endif
25715872bbf2SRichard Henderson         },
25725872bbf2SRichard Henderson         .phdr = {
25735872bbf2SRichard Henderson             .p_type = PT_LOAD,
25745872bbf2SRichard Henderson             .p_flags = PF_X,
25755872bbf2SRichard Henderson         },
25765872bbf2SRichard Henderson         .shdr = {
25775872bbf2SRichard Henderson             [0] = { .sh_type = SHT_NULL },
25785872bbf2SRichard Henderson             /* Trick: The contents of code_gen_buffer are not present in
25795872bbf2SRichard Henderson                this fake ELF file; that got allocated elsewhere.  Therefore
25805872bbf2SRichard Henderson                we mark .text as SHT_NOBITS (similar to .bss) so that readers
25815872bbf2SRichard Henderson                will not look for contents.  We can record any address.  */
25825872bbf2SRichard Henderson             [1] = { /* .text */
25835872bbf2SRichard Henderson                 .sh_type = SHT_NOBITS,
25845872bbf2SRichard Henderson                 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
25855872bbf2SRichard Henderson             },
25865872bbf2SRichard Henderson             [2] = { /* .debug_info */
25875872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
25885872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, di),
25895872bbf2SRichard Henderson                 .sh_size = sizeof(struct DebugInfo),
25905872bbf2SRichard Henderson             },
25915872bbf2SRichard Henderson             [3] = { /* .debug_abbrev */
25925872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
25935872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, da),
25945872bbf2SRichard Henderson                 .sh_size = sizeof(img->da),
25955872bbf2SRichard Henderson             },
25965872bbf2SRichard Henderson             [4] = { /* .debug_frame */
25975872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
25985872bbf2SRichard Henderson                 .sh_offset = sizeof(struct ElfImage),
25995872bbf2SRichard Henderson             },
26005872bbf2SRichard Henderson             [5] = { /* .symtab */
26015872bbf2SRichard Henderson                 .sh_type = SHT_SYMTAB,
26025872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, sym),
26035872bbf2SRichard Henderson                 .sh_size = sizeof(img->sym),
26045872bbf2SRichard Henderson                 .sh_info = 1,
26055872bbf2SRichard Henderson                 .sh_link = ARRAY_SIZE(img->shdr) - 1,
26065872bbf2SRichard Henderson                 .sh_entsize = sizeof(ElfW(Sym)),
26075872bbf2SRichard Henderson             },
26085872bbf2SRichard Henderson             [6] = { /* .strtab */
26095872bbf2SRichard Henderson                 .sh_type = SHT_STRTAB,
26105872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, str),
26115872bbf2SRichard Henderson                 .sh_size = sizeof(img->str),
26125872bbf2SRichard Henderson             }
26135872bbf2SRichard Henderson         },
26145872bbf2SRichard Henderson         .sym = {
26155872bbf2SRichard Henderson             [1] = { /* code_gen_buffer */
26165872bbf2SRichard Henderson                 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
26175872bbf2SRichard Henderson                 .st_shndx = 1,
26185872bbf2SRichard Henderson             }
26195872bbf2SRichard Henderson         },
26205872bbf2SRichard Henderson         .di = {
26215872bbf2SRichard Henderson             .len = sizeof(struct DebugInfo) - 4,
26225872bbf2SRichard Henderson             .version = 2,
26235872bbf2SRichard Henderson             .ptr_size = sizeof(void *),
26245872bbf2SRichard Henderson             .cu_die = 1,
26255872bbf2SRichard Henderson             .cu_lang = 0x8001,  /* DW_LANG_Mips_Assembler */
26265872bbf2SRichard Henderson             .fn_die = 2,
26275872bbf2SRichard Henderson             .fn_name = "code_gen_buffer"
26285872bbf2SRichard Henderson         },
26295872bbf2SRichard Henderson         .da = {
26305872bbf2SRichard Henderson             1,          /* abbrev number (the cu) */
26315872bbf2SRichard Henderson             0x11, 1,    /* DW_TAG_compile_unit, has children */
26325872bbf2SRichard Henderson             0x13, 0x5,  /* DW_AT_language, DW_FORM_data2 */
26335872bbf2SRichard Henderson             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
26345872bbf2SRichard Henderson             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
26355872bbf2SRichard Henderson             0, 0,       /* end of abbrev */
26365872bbf2SRichard Henderson             2,          /* abbrev number (the fn) */
26375872bbf2SRichard Henderson             0x2e, 0,    /* DW_TAG_subprogram, no children */
26385872bbf2SRichard Henderson             0x3, 0x8,   /* DW_AT_name, DW_FORM_string */
26395872bbf2SRichard Henderson             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
26405872bbf2SRichard Henderson             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
26415872bbf2SRichard Henderson             0, 0,       /* end of abbrev */
26425872bbf2SRichard Henderson             0           /* no more abbrev */
26435872bbf2SRichard Henderson         },
26445872bbf2SRichard Henderson         .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
26455872bbf2SRichard Henderson                ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
2646813da627SRichard Henderson     };
2647813da627SRichard Henderson 
2648813da627SRichard Henderson     /* We only need a single jit entry; statically allocate it.  */
2649813da627SRichard Henderson     static struct jit_code_entry one_entry;
2650813da627SRichard Henderson 
26515872bbf2SRichard Henderson     uintptr_t buf = (uintptr_t)buf_ptr;
2652813da627SRichard Henderson     size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
26532c90784aSRichard Henderson     DebugFrameHeader *dfh;
2654813da627SRichard Henderson 
26555872bbf2SRichard Henderson     img = g_malloc(img_size);
26565872bbf2SRichard Henderson     *img = img_template;
2657813da627SRichard Henderson 
26585872bbf2SRichard Henderson     img->phdr.p_vaddr = buf;
26595872bbf2SRichard Henderson     img->phdr.p_paddr = buf;
26605872bbf2SRichard Henderson     img->phdr.p_memsz = buf_size;
2661813da627SRichard Henderson 
26625872bbf2SRichard Henderson     img->shdr[1].sh_name = find_string(img->str, ".text");
26635872bbf2SRichard Henderson     img->shdr[1].sh_addr = buf;
26645872bbf2SRichard Henderson     img->shdr[1].sh_size = buf_size;
2665813da627SRichard Henderson 
26665872bbf2SRichard Henderson     img->shdr[2].sh_name = find_string(img->str, ".debug_info");
26675872bbf2SRichard Henderson     img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
26685872bbf2SRichard Henderson 
26695872bbf2SRichard Henderson     img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
26705872bbf2SRichard Henderson     img->shdr[4].sh_size = debug_frame_size;
26715872bbf2SRichard Henderson 
26725872bbf2SRichard Henderson     img->shdr[5].sh_name = find_string(img->str, ".symtab");
26735872bbf2SRichard Henderson     img->shdr[6].sh_name = find_string(img->str, ".strtab");
26745872bbf2SRichard Henderson 
26755872bbf2SRichard Henderson     img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
26765872bbf2SRichard Henderson     img->sym[1].st_value = buf;
26775872bbf2SRichard Henderson     img->sym[1].st_size = buf_size;
26785872bbf2SRichard Henderson 
26795872bbf2SRichard Henderson     img->di.cu_low_pc = buf;
268045aba097SRichard Henderson     img->di.cu_high_pc = buf + buf_size;
26815872bbf2SRichard Henderson     img->di.fn_low_pc = buf;
268245aba097SRichard Henderson     img->di.fn_high_pc = buf + buf_size;
2683813da627SRichard Henderson 
26842c90784aSRichard Henderson     dfh = (DebugFrameHeader *)(img + 1);
26852c90784aSRichard Henderson     memcpy(dfh, debug_frame, debug_frame_size);
26862c90784aSRichard Henderson     dfh->fde.func_start = buf;
26872c90784aSRichard Henderson     dfh->fde.func_len = buf_size;
26882c90784aSRichard Henderson 
2689813da627SRichard Henderson #ifdef DEBUG_JIT
2690813da627SRichard Henderson     /* Enable this block to be able to debug the ELF image file creation.
2691813da627SRichard Henderson        One can use readelf, objdump, or other inspection utilities.  */
2692813da627SRichard Henderson     {
2693813da627SRichard Henderson         FILE *f = fopen("/tmp/qemu.jit", "w+b");
2694813da627SRichard Henderson         if (f) {
26955872bbf2SRichard Henderson             if (fwrite(img, img_size, 1, f) != img_size) {
2696813da627SRichard Henderson                 /* Avoid stupid unused return value warning for fwrite.  */
2697813da627SRichard Henderson             }
2698813da627SRichard Henderson             fclose(f);
2699813da627SRichard Henderson         }
2700813da627SRichard Henderson     }
2701813da627SRichard Henderson #endif
2702813da627SRichard Henderson 
2703813da627SRichard Henderson     one_entry.symfile_addr = img;
2704813da627SRichard Henderson     one_entry.symfile_size = img_size;
2705813da627SRichard Henderson 
2706813da627SRichard Henderson     __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
2707813da627SRichard Henderson     __jit_debug_descriptor.relevant_entry = &one_entry;
2708813da627SRichard Henderson     __jit_debug_descriptor.first_entry = &one_entry;
2709813da627SRichard Henderson     __jit_debug_register_code();
2710813da627SRichard Henderson }
2711813da627SRichard Henderson #else
27125872bbf2SRichard Henderson /* No support for the feature.  Provide the entry point expected by exec.c,
27135872bbf2SRichard Henderson    and implement the internal function we declared earlier.  */
2714813da627SRichard Henderson 
2715813da627SRichard Henderson static void tcg_register_jit_int(void *buf, size_t size,
27162c90784aSRichard Henderson                                  const void *debug_frame,
27172c90784aSRichard Henderson                                  size_t debug_frame_size)
2718813da627SRichard Henderson {
2719813da627SRichard Henderson }
2720813da627SRichard Henderson 
2721813da627SRichard Henderson void tcg_register_jit(void *buf, size_t buf_size)
2722813da627SRichard Henderson {
2723813da627SRichard Henderson }
2724813da627SRichard Henderson #endif /* ELF_HOST_MACHINE */
2725