xref: /openbmc/qemu/tcg/tcg.c (revision e5138db5)
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"
40902b3d5cSmalc #include "cache-utils.h"
41379f6698SPaul Brook #include "host-utils.h"
422d8ebcf9SRichard Henderson #include "qemu-timer.h"
43c896fe29Sbellard 
44c896fe29Sbellard /* Note: the long term plan is to reduce the dependancies on the QEMU
45c896fe29Sbellard    CPU definitions. Currently they are used for qemu_ld/st
46c896fe29Sbellard    instructions */
47c896fe29Sbellard #define NO_CPU_IO_DEFS
48c896fe29Sbellard #include "cpu.h"
49c896fe29Sbellard 
50c896fe29Sbellard #include "tcg-op.h"
51813da627SRichard Henderson 
52813da627SRichard Henderson #if TCG_TARGET_REG_BITS == 64
53813da627SRichard Henderson # define ELF_CLASS  ELFCLASS64
54813da627SRichard Henderson #else
55813da627SRichard Henderson # define ELF_CLASS  ELFCLASS32
56813da627SRichard Henderson #endif
57813da627SRichard Henderson #ifdef HOST_WORDS_BIGENDIAN
58813da627SRichard Henderson # define ELF_DATA   ELFDATA2MSB
59813da627SRichard Henderson #else
60813da627SRichard Henderson # define ELF_DATA   ELFDATA2LSB
61813da627SRichard Henderson #endif
62813da627SRichard Henderson 
63c896fe29Sbellard #include "elf.h"
64c896fe29Sbellard 
65c0ad3001SStefan Weil /* Forward declarations for functions declared in tcg-target.c and used here. */
66e4d58b41SRichard Henderson static void tcg_target_init(TCGContext *s);
67e4d58b41SRichard Henderson static void tcg_target_qemu_prologue(TCGContext *s);
68c896fe29Sbellard static void patch_reloc(uint8_t *code_ptr, int type,
69f54b3f92Saurel32                         tcg_target_long value, tcg_target_long addend);
70c896fe29Sbellard 
71813da627SRichard Henderson static void tcg_register_jit_int(void *buf, size_t size,
72813da627SRichard Henderson                                  void *debug_frame, size_t debug_frame_size)
73813da627SRichard Henderson     __attribute__((unused));
74813da627SRichard Henderson 
75c0ad3001SStefan Weil /* Forward declarations for functions declared and used in tcg-target.c. */
76c0ad3001SStefan Weil static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str);
772a534affSRichard Henderson static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
78c0ad3001SStefan Weil                        tcg_target_long arg2);
792a534affSRichard Henderson static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
80c0ad3001SStefan Weil static void tcg_out_movi(TCGContext *s, TCGType type,
812a534affSRichard Henderson                          TCGReg ret, tcg_target_long arg);
82c0ad3001SStefan Weil static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
83c0ad3001SStefan Weil                        const int *const_args);
842a534affSRichard Henderson static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
85c0ad3001SStefan Weil                        tcg_target_long arg2);
86c0ad3001SStefan Weil static int tcg_target_const_match(tcg_target_long val,
87c0ad3001SStefan Weil                                   const TCGArgConstraint *arg_ct);
88c0ad3001SStefan Weil 
898399ad59SRichard Henderson TCGOpDef tcg_op_defs[] = {
900e2029a0SAurelien Jarno #define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags },
91c896fe29Sbellard #include "tcg-opc.h"
92c896fe29Sbellard #undef DEF
93c896fe29Sbellard };
942a24374aSStefan Weil const size_t tcg_op_defs_max = ARRAY_SIZE(tcg_op_defs);
95c896fe29Sbellard 
96b1d8e52eSblueswir1 static TCGRegSet tcg_target_available_regs[2];
97b1d8e52eSblueswir1 static TCGRegSet tcg_target_call_clobber_regs;
98c896fe29Sbellard 
99c896fe29Sbellard static inline void tcg_out8(TCGContext *s, uint8_t v)
100c896fe29Sbellard {
101c896fe29Sbellard     *s->code_ptr++ = v;
102c896fe29Sbellard }
103c896fe29Sbellard 
104c896fe29Sbellard static inline void tcg_out16(TCGContext *s, uint16_t v)
105c896fe29Sbellard {
106c896fe29Sbellard     *(uint16_t *)s->code_ptr = v;
107c896fe29Sbellard     s->code_ptr += 2;
108c896fe29Sbellard }
109c896fe29Sbellard 
110c896fe29Sbellard static inline void tcg_out32(TCGContext *s, uint32_t v)
111c896fe29Sbellard {
112c896fe29Sbellard     *(uint32_t *)s->code_ptr = v;
113c896fe29Sbellard     s->code_ptr += 4;
114c896fe29Sbellard }
115c896fe29Sbellard 
116c896fe29Sbellard /* label relocation processing */
117c896fe29Sbellard 
118a5ad5916SStefan Weil static void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
119c896fe29Sbellard                           int label_index, long addend)
120c896fe29Sbellard {
121c896fe29Sbellard     TCGLabel *l;
122c896fe29Sbellard     TCGRelocation *r;
123c896fe29Sbellard 
124c896fe29Sbellard     l = &s->labels[label_index];
125c896fe29Sbellard     if (l->has_value) {
126623e265cSpbrook         /* FIXME: This may break relocations on RISC targets that
127623e265cSpbrook            modify instruction fields in place.  The caller may not have
128623e265cSpbrook            written the initial value.  */
129f54b3f92Saurel32         patch_reloc(code_ptr, type, l->u.value, addend);
130c896fe29Sbellard     } else {
131c896fe29Sbellard         /* add a new relocation entry */
132c896fe29Sbellard         r = tcg_malloc(sizeof(TCGRelocation));
133c896fe29Sbellard         r->type = type;
134c896fe29Sbellard         r->ptr = code_ptr;
135c896fe29Sbellard         r->addend = addend;
136c896fe29Sbellard         r->next = l->u.first_reloc;
137c896fe29Sbellard         l->u.first_reloc = r;
138c896fe29Sbellard     }
139c896fe29Sbellard }
140c896fe29Sbellard 
1419d6fca70SStefan Weil static void tcg_out_label(TCGContext *s, int label_index, void *ptr)
142c896fe29Sbellard {
143c896fe29Sbellard     TCGLabel *l;
144c896fe29Sbellard     TCGRelocation *r;
1459d6fca70SStefan Weil     tcg_target_long value = (tcg_target_long)ptr;
146c896fe29Sbellard 
147c896fe29Sbellard     l = &s->labels[label_index];
148c896fe29Sbellard     if (l->has_value)
149c896fe29Sbellard         tcg_abort();
150c896fe29Sbellard     r = l->u.first_reloc;
151c896fe29Sbellard     while (r != NULL) {
152f54b3f92Saurel32         patch_reloc(r->ptr, r->type, value, r->addend);
153c896fe29Sbellard         r = r->next;
154c896fe29Sbellard     }
155c896fe29Sbellard     l->has_value = 1;
156c896fe29Sbellard     l->u.value = value;
157c896fe29Sbellard }
158c896fe29Sbellard 
159c896fe29Sbellard int gen_new_label(void)
160c896fe29Sbellard {
161c896fe29Sbellard     TCGContext *s = &tcg_ctx;
162c896fe29Sbellard     int idx;
163c896fe29Sbellard     TCGLabel *l;
164c896fe29Sbellard 
165c896fe29Sbellard     if (s->nb_labels >= TCG_MAX_LABELS)
166c896fe29Sbellard         tcg_abort();
167c896fe29Sbellard     idx = s->nb_labels++;
168c896fe29Sbellard     l = &s->labels[idx];
169c896fe29Sbellard     l->has_value = 0;
170c896fe29Sbellard     l->u.first_reloc = NULL;
171c896fe29Sbellard     return idx;
172c896fe29Sbellard }
173c896fe29Sbellard 
174c896fe29Sbellard #include "tcg-target.c"
175c896fe29Sbellard 
176c896fe29Sbellard /* pool based memory allocation */
177c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size)
178c896fe29Sbellard {
179c896fe29Sbellard     TCGPool *p;
180c896fe29Sbellard     int pool_size;
181c896fe29Sbellard 
182c896fe29Sbellard     if (size > TCG_POOL_CHUNK_SIZE) {
183c896fe29Sbellard         /* big malloc: insert a new pool (XXX: could optimize) */
1847267c094SAnthony Liguori         p = g_malloc(sizeof(TCGPool) + size);
185c896fe29Sbellard         p->size = size;
1864055299eSKirill Batuzov         p->next = s->pool_first_large;
1874055299eSKirill Batuzov         s->pool_first_large = p;
1884055299eSKirill Batuzov         return p->data;
189c896fe29Sbellard     } else {
190c896fe29Sbellard         p = s->pool_current;
191c896fe29Sbellard         if (!p) {
192c896fe29Sbellard             p = s->pool_first;
193c896fe29Sbellard             if (!p)
194c896fe29Sbellard                 goto new_pool;
195c896fe29Sbellard         } else {
196c896fe29Sbellard             if (!p->next) {
197c896fe29Sbellard             new_pool:
198c896fe29Sbellard                 pool_size = TCG_POOL_CHUNK_SIZE;
1997267c094SAnthony Liguori                 p = g_malloc(sizeof(TCGPool) + pool_size);
200c896fe29Sbellard                 p->size = pool_size;
201c896fe29Sbellard                 p->next = NULL;
202c896fe29Sbellard                 if (s->pool_current)
203c896fe29Sbellard                     s->pool_current->next = p;
204c896fe29Sbellard                 else
205c896fe29Sbellard                     s->pool_first = p;
206c896fe29Sbellard             } else {
207c896fe29Sbellard                 p = p->next;
208c896fe29Sbellard             }
209c896fe29Sbellard         }
210c896fe29Sbellard     }
211c896fe29Sbellard     s->pool_current = p;
212c896fe29Sbellard     s->pool_cur = p->data + size;
213c896fe29Sbellard     s->pool_end = p->data + p->size;
214c896fe29Sbellard     return p->data;
215c896fe29Sbellard }
216c896fe29Sbellard 
217c896fe29Sbellard void tcg_pool_reset(TCGContext *s)
218c896fe29Sbellard {
2194055299eSKirill Batuzov     TCGPool *p, *t;
2204055299eSKirill Batuzov     for (p = s->pool_first_large; p; p = t) {
2214055299eSKirill Batuzov         t = p->next;
2224055299eSKirill Batuzov         g_free(p);
2234055299eSKirill Batuzov     }
2244055299eSKirill Batuzov     s->pool_first_large = NULL;
225c896fe29Sbellard     s->pool_cur = s->pool_end = NULL;
226c896fe29Sbellard     s->pool_current = NULL;
227c896fe29Sbellard }
228c896fe29Sbellard 
229c896fe29Sbellard void tcg_context_init(TCGContext *s)
230c896fe29Sbellard {
231c896fe29Sbellard     int op, total_args, n;
232c896fe29Sbellard     TCGOpDef *def;
233c896fe29Sbellard     TCGArgConstraint *args_ct;
234c896fe29Sbellard     int *sorted_args;
235c896fe29Sbellard 
236c896fe29Sbellard     memset(s, 0, sizeof(*s));
237c896fe29Sbellard     s->nb_globals = 0;
238c896fe29Sbellard 
239c896fe29Sbellard     /* Count total number of arguments and allocate the corresponding
240c896fe29Sbellard        space */
241c896fe29Sbellard     total_args = 0;
242c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
243c896fe29Sbellard         def = &tcg_op_defs[op];
244c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
245c896fe29Sbellard         total_args += n;
246c896fe29Sbellard     }
247c896fe29Sbellard 
2487267c094SAnthony Liguori     args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
2497267c094SAnthony Liguori     sorted_args = g_malloc(sizeof(int) * total_args);
250c896fe29Sbellard 
251c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
252c896fe29Sbellard         def = &tcg_op_defs[op];
253c896fe29Sbellard         def->args_ct = args_ct;
254c896fe29Sbellard         def->sorted_args = sorted_args;
255c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
256c896fe29Sbellard         sorted_args += n;
257c896fe29Sbellard         args_ct += n;
258c896fe29Sbellard     }
259c896fe29Sbellard 
260c896fe29Sbellard     tcg_target_init(s);
2619002ec79SRichard Henderson }
262b03cce8eSbellard 
2639002ec79SRichard Henderson void tcg_prologue_init(TCGContext *s)
2649002ec79SRichard Henderson {
265b03cce8eSbellard     /* init global prologue and epilogue */
266b03cce8eSbellard     s->code_buf = code_gen_prologue;
267b03cce8eSbellard     s->code_ptr = s->code_buf;
268b03cce8eSbellard     tcg_target_qemu_prologue(s);
2692aeabc08SStefan Weil     flush_icache_range((tcg_target_ulong)s->code_buf,
2702aeabc08SStefan Weil                        (tcg_target_ulong)s->code_ptr);
271c896fe29Sbellard }
272c896fe29Sbellard 
273c896fe29Sbellard void tcg_set_frame(TCGContext *s, int reg,
274c896fe29Sbellard                    tcg_target_long start, tcg_target_long size)
275c896fe29Sbellard {
276c896fe29Sbellard     s->frame_start = start;
277c896fe29Sbellard     s->frame_end = start + size;
278c896fe29Sbellard     s->frame_reg = reg;
279c896fe29Sbellard }
280c896fe29Sbellard 
281c896fe29Sbellard void tcg_func_start(TCGContext *s)
282c896fe29Sbellard {
283e8996ee0Sbellard     int i;
284c896fe29Sbellard     tcg_pool_reset(s);
285c896fe29Sbellard     s->nb_temps = s->nb_globals;
286641d5fbeSbellard     for(i = 0; i < (TCG_TYPE_COUNT * 2); i++)
287e8996ee0Sbellard         s->first_free_temp[i] = -1;
288c896fe29Sbellard     s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS);
289c896fe29Sbellard     s->nb_labels = 0;
290c896fe29Sbellard     s->current_frame_offset = s->frame_start;
291c896fe29Sbellard 
2920a209d4bSRichard Henderson #ifdef CONFIG_DEBUG_TCG
2930a209d4bSRichard Henderson     s->goto_tb_issue_mask = 0;
2940a209d4bSRichard Henderson #endif
2950a209d4bSRichard Henderson 
29692414b31SEvgeny Voevodin     s->gen_opc_ptr = s->gen_opc_buf;
2971ff0a2c5SEvgeny Voevodin     s->gen_opparam_ptr = s->gen_opparam_buf;
298b76f0d8cSYeongkyoon Lee 
299b76f0d8cSYeongkyoon Lee #if defined(CONFIG_QEMU_LDST_OPTIMIZATION) && defined(CONFIG_SOFTMMU)
300b76f0d8cSYeongkyoon Lee     /* Initialize qemu_ld/st labels to assist code generation at the end of TB
301b76f0d8cSYeongkyoon Lee        for TLB miss cases at the end of TB */
302b76f0d8cSYeongkyoon Lee     s->qemu_ldst_labels = tcg_malloc(sizeof(TCGLabelQemuLdst) *
303b76f0d8cSYeongkyoon Lee                                      TCG_MAX_QEMU_LDST);
304b76f0d8cSYeongkyoon Lee     s->nb_qemu_ldst_labels = 0;
305b76f0d8cSYeongkyoon Lee #endif
306c896fe29Sbellard }
307c896fe29Sbellard 
308c896fe29Sbellard static inline void tcg_temp_alloc(TCGContext *s, int n)
309c896fe29Sbellard {
310c896fe29Sbellard     if (n > TCG_MAX_TEMPS)
311c896fe29Sbellard         tcg_abort();
312c896fe29Sbellard }
313c896fe29Sbellard 
314a7812ae4Spbrook static inline int tcg_global_reg_new_internal(TCGType type, int reg,
315a7812ae4Spbrook                                               const char *name)
316c896fe29Sbellard {
317c896fe29Sbellard     TCGContext *s = &tcg_ctx;
318c896fe29Sbellard     TCGTemp *ts;
319c896fe29Sbellard     int idx;
320c896fe29Sbellard 
321c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
322c896fe29Sbellard     if (type != TCG_TYPE_I32)
323c896fe29Sbellard         tcg_abort();
324c896fe29Sbellard #endif
325c896fe29Sbellard     if (tcg_regset_test_reg(s->reserved_regs, reg))
326c896fe29Sbellard         tcg_abort();
327c896fe29Sbellard     idx = s->nb_globals;
328c896fe29Sbellard     tcg_temp_alloc(s, s->nb_globals + 1);
329c896fe29Sbellard     ts = &s->temps[s->nb_globals];
330c896fe29Sbellard     ts->base_type = type;
331c896fe29Sbellard     ts->type = type;
332c896fe29Sbellard     ts->fixed_reg = 1;
333c896fe29Sbellard     ts->reg = reg;
334c896fe29Sbellard     ts->name = name;
335c896fe29Sbellard     s->nb_globals++;
336c896fe29Sbellard     tcg_regset_set_reg(s->reserved_regs, reg);
337a7812ae4Spbrook     return idx;
338a7812ae4Spbrook }
339a7812ae4Spbrook 
340a7812ae4Spbrook TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name)
341a7812ae4Spbrook {
342a7812ae4Spbrook     int idx;
343a7812ae4Spbrook 
344a7812ae4Spbrook     idx = tcg_global_reg_new_internal(TCG_TYPE_I32, reg, name);
345a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
346a7812ae4Spbrook }
347a7812ae4Spbrook 
348a7812ae4Spbrook TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name)
349a7812ae4Spbrook {
350a7812ae4Spbrook     int idx;
351a7812ae4Spbrook 
352a7812ae4Spbrook     idx = tcg_global_reg_new_internal(TCG_TYPE_I64, reg, name);
353a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
354c896fe29Sbellard }
355c896fe29Sbellard 
356a7812ae4Spbrook static inline int tcg_global_mem_new_internal(TCGType type, int reg,
357a7812ae4Spbrook                                               tcg_target_long offset,
358c896fe29Sbellard                                               const char *name)
359c896fe29Sbellard {
360c896fe29Sbellard     TCGContext *s = &tcg_ctx;
361c896fe29Sbellard     TCGTemp *ts;
362c896fe29Sbellard     int idx;
363c896fe29Sbellard 
364c896fe29Sbellard     idx = s->nb_globals;
365c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
366c896fe29Sbellard     if (type == TCG_TYPE_I64) {
367c896fe29Sbellard         char buf[64];
368c588979bSths         tcg_temp_alloc(s, s->nb_globals + 2);
369c896fe29Sbellard         ts = &s->temps[s->nb_globals];
370c896fe29Sbellard         ts->base_type = type;
371c896fe29Sbellard         ts->type = TCG_TYPE_I32;
372c896fe29Sbellard         ts->fixed_reg = 0;
373c896fe29Sbellard         ts->mem_allocated = 1;
374c896fe29Sbellard         ts->mem_reg = reg;
375c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN
376c896fe29Sbellard         ts->mem_offset = offset + 4;
377c896fe29Sbellard #else
378c896fe29Sbellard         ts->mem_offset = offset;
379c896fe29Sbellard #endif
380c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
381c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_0");
382c896fe29Sbellard         ts->name = strdup(buf);
383c896fe29Sbellard         ts++;
384c896fe29Sbellard 
385c896fe29Sbellard         ts->base_type = type;
386c896fe29Sbellard         ts->type = TCG_TYPE_I32;
387c896fe29Sbellard         ts->fixed_reg = 0;
388c896fe29Sbellard         ts->mem_allocated = 1;
389c896fe29Sbellard         ts->mem_reg = reg;
390c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN
391c896fe29Sbellard         ts->mem_offset = offset;
392c896fe29Sbellard #else
393c896fe29Sbellard         ts->mem_offset = offset + 4;
394c896fe29Sbellard #endif
395c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
396c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_1");
397c896fe29Sbellard         ts->name = strdup(buf);
398c896fe29Sbellard 
399c896fe29Sbellard         s->nb_globals += 2;
400c896fe29Sbellard     } else
401c896fe29Sbellard #endif
402c896fe29Sbellard     {
403c896fe29Sbellard         tcg_temp_alloc(s, s->nb_globals + 1);
404c896fe29Sbellard         ts = &s->temps[s->nb_globals];
405c896fe29Sbellard         ts->base_type = type;
406c896fe29Sbellard         ts->type = type;
407c896fe29Sbellard         ts->fixed_reg = 0;
408c896fe29Sbellard         ts->mem_allocated = 1;
409c896fe29Sbellard         ts->mem_reg = reg;
410c896fe29Sbellard         ts->mem_offset = offset;
411c896fe29Sbellard         ts->name = name;
412c896fe29Sbellard         s->nb_globals++;
413c896fe29Sbellard     }
414a7812ae4Spbrook     return idx;
415c896fe29Sbellard }
416c896fe29Sbellard 
417a7812ae4Spbrook TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
418a7812ae4Spbrook                                 const char *name)
419a7812ae4Spbrook {
420a7812ae4Spbrook     int idx;
421a7812ae4Spbrook 
422a7812ae4Spbrook     idx = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name);
423a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
424a7812ae4Spbrook }
425a7812ae4Spbrook 
426a7812ae4Spbrook TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
427a7812ae4Spbrook                                 const char *name)
428a7812ae4Spbrook {
429a7812ae4Spbrook     int idx;
430a7812ae4Spbrook 
431a7812ae4Spbrook     idx = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name);
432a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
433a7812ae4Spbrook }
434a7812ae4Spbrook 
435a7812ae4Spbrook static inline int tcg_temp_new_internal(TCGType type, int temp_local)
436c896fe29Sbellard {
437c896fe29Sbellard     TCGContext *s = &tcg_ctx;
438c896fe29Sbellard     TCGTemp *ts;
439641d5fbeSbellard     int idx, k;
440c896fe29Sbellard 
441641d5fbeSbellard     k = type;
442641d5fbeSbellard     if (temp_local)
443641d5fbeSbellard         k += TCG_TYPE_COUNT;
444641d5fbeSbellard     idx = s->first_free_temp[k];
445e8996ee0Sbellard     if (idx != -1) {
446e8996ee0Sbellard         /* There is already an available temp with the
447e8996ee0Sbellard            right type */
448e8996ee0Sbellard         ts = &s->temps[idx];
449641d5fbeSbellard         s->first_free_temp[k] = ts->next_free_temp;
450e8996ee0Sbellard         ts->temp_allocated = 1;
451641d5fbeSbellard         assert(ts->temp_local == temp_local);
452e8996ee0Sbellard     } else {
453c896fe29Sbellard         idx = s->nb_temps;
454c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
455c896fe29Sbellard         if (type == TCG_TYPE_I64) {
4568df1ca4bSths             tcg_temp_alloc(s, s->nb_temps + 2);
457c896fe29Sbellard             ts = &s->temps[s->nb_temps];
458c896fe29Sbellard             ts->base_type = type;
459c896fe29Sbellard             ts->type = TCG_TYPE_I32;
460e8996ee0Sbellard             ts->temp_allocated = 1;
461641d5fbeSbellard             ts->temp_local = temp_local;
462c896fe29Sbellard             ts->name = NULL;
463c896fe29Sbellard             ts++;
464c896fe29Sbellard             ts->base_type = TCG_TYPE_I32;
465c896fe29Sbellard             ts->type = TCG_TYPE_I32;
466e8996ee0Sbellard             ts->temp_allocated = 1;
467641d5fbeSbellard             ts->temp_local = temp_local;
468c896fe29Sbellard             ts->name = NULL;
469c896fe29Sbellard             s->nb_temps += 2;
470c896fe29Sbellard         } else
471c896fe29Sbellard #endif
472c896fe29Sbellard         {
473c896fe29Sbellard             tcg_temp_alloc(s, s->nb_temps + 1);
474c896fe29Sbellard             ts = &s->temps[s->nb_temps];
475c896fe29Sbellard             ts->base_type = type;
476c896fe29Sbellard             ts->type = type;
477e8996ee0Sbellard             ts->temp_allocated = 1;
478641d5fbeSbellard             ts->temp_local = temp_local;
479c896fe29Sbellard             ts->name = NULL;
480c896fe29Sbellard             s->nb_temps++;
481c896fe29Sbellard         }
482e8996ee0Sbellard     }
48327bfd83cSPeter Maydell 
48427bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
48527bfd83cSPeter Maydell     s->temps_in_use++;
48627bfd83cSPeter Maydell #endif
487a7812ae4Spbrook     return idx;
488c896fe29Sbellard }
489c896fe29Sbellard 
490a7812ae4Spbrook TCGv_i32 tcg_temp_new_internal_i32(int temp_local)
491a7812ae4Spbrook {
492a7812ae4Spbrook     int idx;
493a7812ae4Spbrook 
494a7812ae4Spbrook     idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local);
495a7812ae4Spbrook     return MAKE_TCGV_I32(idx);
496a7812ae4Spbrook }
497a7812ae4Spbrook 
498a7812ae4Spbrook TCGv_i64 tcg_temp_new_internal_i64(int temp_local)
499a7812ae4Spbrook {
500a7812ae4Spbrook     int idx;
501a7812ae4Spbrook 
502a7812ae4Spbrook     idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local);
503a7812ae4Spbrook     return MAKE_TCGV_I64(idx);
504a7812ae4Spbrook }
505a7812ae4Spbrook 
506a7812ae4Spbrook static inline void tcg_temp_free_internal(int idx)
507c896fe29Sbellard {
508c896fe29Sbellard     TCGContext *s = &tcg_ctx;
509c896fe29Sbellard     TCGTemp *ts;
510641d5fbeSbellard     int k;
511c896fe29Sbellard 
51227bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
51327bfd83cSPeter Maydell     s->temps_in_use--;
51427bfd83cSPeter Maydell     if (s->temps_in_use < 0) {
51527bfd83cSPeter Maydell         fprintf(stderr, "More temporaries freed than allocated!\n");
51627bfd83cSPeter Maydell     }
51727bfd83cSPeter Maydell #endif
51827bfd83cSPeter Maydell 
519e8996ee0Sbellard     assert(idx >= s->nb_globals && idx < s->nb_temps);
520c896fe29Sbellard     ts = &s->temps[idx];
521e8996ee0Sbellard     assert(ts->temp_allocated != 0);
522e8996ee0Sbellard     ts->temp_allocated = 0;
523641d5fbeSbellard     k = ts->base_type;
524641d5fbeSbellard     if (ts->temp_local)
525641d5fbeSbellard         k += TCG_TYPE_COUNT;
526641d5fbeSbellard     ts->next_free_temp = s->first_free_temp[k];
527641d5fbeSbellard     s->first_free_temp[k] = idx;
528e8996ee0Sbellard }
529e8996ee0Sbellard 
530a7812ae4Spbrook void tcg_temp_free_i32(TCGv_i32 arg)
531e8996ee0Sbellard {
532a7812ae4Spbrook     tcg_temp_free_internal(GET_TCGV_I32(arg));
533a7812ae4Spbrook }
534a7812ae4Spbrook 
535a7812ae4Spbrook void tcg_temp_free_i64(TCGv_i64 arg)
536a7812ae4Spbrook {
537a7812ae4Spbrook     tcg_temp_free_internal(GET_TCGV_I64(arg));
538a7812ae4Spbrook }
539a7812ae4Spbrook 
540a7812ae4Spbrook TCGv_i32 tcg_const_i32(int32_t val)
541a7812ae4Spbrook {
542a7812ae4Spbrook     TCGv_i32 t0;
543a7812ae4Spbrook     t0 = tcg_temp_new_i32();
544e8996ee0Sbellard     tcg_gen_movi_i32(t0, val);
545e8996ee0Sbellard     return t0;
546c896fe29Sbellard }
547c896fe29Sbellard 
548a7812ae4Spbrook TCGv_i64 tcg_const_i64(int64_t val)
549c896fe29Sbellard {
550a7812ae4Spbrook     TCGv_i64 t0;
551a7812ae4Spbrook     t0 = tcg_temp_new_i64();
552e8996ee0Sbellard     tcg_gen_movi_i64(t0, val);
553e8996ee0Sbellard     return t0;
554c896fe29Sbellard }
555c896fe29Sbellard 
556a7812ae4Spbrook TCGv_i32 tcg_const_local_i32(int32_t val)
557bdffd4a9Saurel32 {
558a7812ae4Spbrook     TCGv_i32 t0;
559a7812ae4Spbrook     t0 = tcg_temp_local_new_i32();
560bdffd4a9Saurel32     tcg_gen_movi_i32(t0, val);
561bdffd4a9Saurel32     return t0;
562bdffd4a9Saurel32 }
563bdffd4a9Saurel32 
564a7812ae4Spbrook TCGv_i64 tcg_const_local_i64(int64_t val)
565bdffd4a9Saurel32 {
566a7812ae4Spbrook     TCGv_i64 t0;
567a7812ae4Spbrook     t0 = tcg_temp_local_new_i64();
568bdffd4a9Saurel32     tcg_gen_movi_i64(t0, val);
569bdffd4a9Saurel32     return t0;
570bdffd4a9Saurel32 }
571bdffd4a9Saurel32 
57227bfd83cSPeter Maydell #if defined(CONFIG_DEBUG_TCG)
57327bfd83cSPeter Maydell void tcg_clear_temp_count(void)
57427bfd83cSPeter Maydell {
57527bfd83cSPeter Maydell     TCGContext *s = &tcg_ctx;
57627bfd83cSPeter Maydell     s->temps_in_use = 0;
57727bfd83cSPeter Maydell }
57827bfd83cSPeter Maydell 
57927bfd83cSPeter Maydell int tcg_check_temp_count(void)
58027bfd83cSPeter Maydell {
58127bfd83cSPeter Maydell     TCGContext *s = &tcg_ctx;
58227bfd83cSPeter Maydell     if (s->temps_in_use) {
58327bfd83cSPeter Maydell         /* Clear the count so that we don't give another
58427bfd83cSPeter Maydell          * warning immediately next time around.
58527bfd83cSPeter Maydell          */
58627bfd83cSPeter Maydell         s->temps_in_use = 0;
58727bfd83cSPeter Maydell         return 1;
58827bfd83cSPeter Maydell     }
58927bfd83cSPeter Maydell     return 0;
59027bfd83cSPeter Maydell }
59127bfd83cSPeter Maydell #endif
59227bfd83cSPeter Maydell 
593c896fe29Sbellard void tcg_register_helper(void *func, const char *name)
594c896fe29Sbellard {
595c896fe29Sbellard     TCGContext *s = &tcg_ctx;
596c896fe29Sbellard     int n;
597c896fe29Sbellard     if ((s->nb_helpers + 1) > s->allocated_helpers) {
598c896fe29Sbellard         n = s->allocated_helpers;
599c896fe29Sbellard         if (n == 0) {
600c896fe29Sbellard             n = 4;
601c896fe29Sbellard         } else {
602c896fe29Sbellard             n *= 2;
603c896fe29Sbellard         }
604c896fe29Sbellard         s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo));
605c896fe29Sbellard         s->allocated_helpers = n;
606c896fe29Sbellard     }
6074dc81f28Sbellard     s->helpers[s->nb_helpers].func = (tcg_target_ulong)func;
608c896fe29Sbellard     s->helpers[s->nb_helpers].name = name;
609c896fe29Sbellard     s->nb_helpers++;
610c896fe29Sbellard }
611c896fe29Sbellard 
61239cf05d3Sbellard /* Note: we convert the 64 bit args to 32 bit and do some alignment
61339cf05d3Sbellard    and endian swap. Maybe it would be better to do the alignment
61439cf05d3Sbellard    and endian swap in tcg_reg_alloc_call(). */
615a7812ae4Spbrook void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
616a7812ae4Spbrook                    int sizemask, TCGArg ret, int nargs, TCGArg *args)
617c896fe29Sbellard {
618a7812ae4Spbrook     int i;
619a7812ae4Spbrook     int real_args;
620a7812ae4Spbrook     int nb_rets;
621a7812ae4Spbrook     TCGArg *nparam;
6222bece2c8SRichard Henderson 
6232bece2c8SRichard Henderson #if defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
6242bece2c8SRichard Henderson     for (i = 0; i < nargs; ++i) {
6252bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
6262bece2c8SRichard Henderson         int is_signed = sizemask & (2 << (i+1)*2);
6272bece2c8SRichard Henderson         if (!is_64bit) {
6282bece2c8SRichard Henderson             TCGv_i64 temp = tcg_temp_new_i64();
6292bece2c8SRichard Henderson             TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
6302bece2c8SRichard Henderson             if (is_signed) {
6312bece2c8SRichard Henderson                 tcg_gen_ext32s_i64(temp, orig);
6322bece2c8SRichard Henderson             } else {
6332bece2c8SRichard Henderson                 tcg_gen_ext32u_i64(temp, orig);
6342bece2c8SRichard Henderson             }
6352bece2c8SRichard Henderson             args[i] = GET_TCGV_I64(temp);
6362bece2c8SRichard Henderson         }
6372bece2c8SRichard Henderson     }
6382bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */
6392bece2c8SRichard Henderson 
640efd7f486SEvgeny Voevodin     *s->gen_opc_ptr++ = INDEX_op_call;
641c4afe5c4SEvgeny Voevodin     nparam = s->gen_opparam_ptr++;
642a7812ae4Spbrook     if (ret != TCG_CALL_DUMMY_ARG) {
643a7812ae4Spbrook #if TCG_TARGET_REG_BITS < 64
644a7812ae4Spbrook         if (sizemask & 1) {
645a7812ae4Spbrook #ifdef TCG_TARGET_WORDS_BIGENDIAN
646c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = ret + 1;
647c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = ret;
648a7812ae4Spbrook #else
649c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = ret;
650c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = ret + 1;
651a7812ae4Spbrook #endif
652a7812ae4Spbrook             nb_rets = 2;
653a7812ae4Spbrook         } else
654a7812ae4Spbrook #endif
655a7812ae4Spbrook         {
656c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = ret;
657a7812ae4Spbrook             nb_rets = 1;
658a7812ae4Spbrook         }
659a7812ae4Spbrook     } else {
660a7812ae4Spbrook         nb_rets = 0;
661a7812ae4Spbrook     }
662a7812ae4Spbrook     real_args = 0;
663a7812ae4Spbrook     for (i = 0; i < nargs; i++) {
664a7812ae4Spbrook #if TCG_TARGET_REG_BITS < 64
6652bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
6662bece2c8SRichard Henderson         if (is_64bit) {
66739cf05d3Sbellard #ifdef TCG_TARGET_CALL_ALIGN_ARGS
66839cf05d3Sbellard             /* some targets want aligned 64 bit args */
669ebd486d5Smalc             if (real_args & 1) {
670c4afe5c4SEvgeny Voevodin                 *s->gen_opparam_ptr++ = TCG_CALL_DUMMY_ARG;
671ebd486d5Smalc                 real_args++;
67239cf05d3Sbellard             }
67339cf05d3Sbellard #endif
6743f90f252SRichard Henderson 	    /* If stack grows up, then we will be placing successive
6753f90f252SRichard Henderson 	       arguments at lower addresses, which means we need to
6763f90f252SRichard Henderson 	       reverse the order compared to how we would normally
6773f90f252SRichard Henderson 	       treat either big or little-endian.  For those arguments
6783f90f252SRichard Henderson 	       that will wind up in registers, this still works for
6793f90f252SRichard Henderson 	       HPPA (the only current STACK_GROWSUP target) since the
6803f90f252SRichard Henderson 	       argument registers are *also* allocated in decreasing
6813f90f252SRichard Henderson 	       order.  If another such target is added, this logic may
6823f90f252SRichard Henderson 	       have to get more complicated to differentiate between
6833f90f252SRichard Henderson 	       stack arguments and register arguments.  */
6843f90f252SRichard Henderson #if defined(TCG_TARGET_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
685c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = args[i] + 1;
686c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = args[i];
687c896fe29Sbellard #else
688c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = args[i];
689c4afe5c4SEvgeny Voevodin             *s->gen_opparam_ptr++ = args[i] + 1;
690c896fe29Sbellard #endif
691a7812ae4Spbrook             real_args += 2;
6922bece2c8SRichard Henderson             continue;
6932bece2c8SRichard Henderson         }
6942bece2c8SRichard Henderson #endif /* TCG_TARGET_REG_BITS < 64 */
6952bece2c8SRichard Henderson 
696c4afe5c4SEvgeny Voevodin         *s->gen_opparam_ptr++ = args[i];
697a7812ae4Spbrook         real_args++;
698c896fe29Sbellard     }
699c4afe5c4SEvgeny Voevodin     *s->gen_opparam_ptr++ = GET_TCGV_PTR(func);
700a7812ae4Spbrook 
701c4afe5c4SEvgeny Voevodin     *s->gen_opparam_ptr++ = flags;
702a7812ae4Spbrook 
703a7812ae4Spbrook     *nparam = (nb_rets << 16) | (real_args + 1);
704a7812ae4Spbrook 
705a7812ae4Spbrook     /* total parameters, needed to go backward in the instruction stream */
706c4afe5c4SEvgeny Voevodin     *s->gen_opparam_ptr++ = 1 + nb_rets + real_args + 3;
7072bece2c8SRichard Henderson 
7082bece2c8SRichard Henderson #if defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
7092bece2c8SRichard Henderson     for (i = 0; i < nargs; ++i) {
7102bece2c8SRichard Henderson         int is_64bit = sizemask & (1 << (i+1)*2);
7112bece2c8SRichard Henderson         if (!is_64bit) {
7122bece2c8SRichard Henderson             TCGv_i64 temp = MAKE_TCGV_I64(args[i]);
7132bece2c8SRichard Henderson             tcg_temp_free_i64(temp);
7142bece2c8SRichard Henderson         }
7152bece2c8SRichard Henderson     }
7162bece2c8SRichard Henderson #endif /* TCG_TARGET_EXTEND_ARGS */
717a7812ae4Spbrook }
718c896fe29Sbellard 
719ac56dd48Spbrook #if TCG_TARGET_REG_BITS == 32
720a7812ae4Spbrook void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
721c896fe29Sbellard                         int c, int right, int arith)
722c896fe29Sbellard {
723cf60bce4Sbellard     if (c == 0) {
724a7812ae4Spbrook         tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg1));
725cf60bce4Sbellard         tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1));
726cf60bce4Sbellard     } else if (c >= 32) {
727c896fe29Sbellard         c -= 32;
728c896fe29Sbellard         if (right) {
729c896fe29Sbellard             if (arith) {
730a7812ae4Spbrook                 tcg_gen_sari_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c);
731ac56dd48Spbrook                 tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31);
732c896fe29Sbellard             } else {
733a7812ae4Spbrook                 tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c);
734ac56dd48Spbrook                 tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
735c896fe29Sbellard             }
736c896fe29Sbellard         } else {
737a7812ae4Spbrook             tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_LOW(arg1), c);
738a7812ae4Spbrook             tcg_gen_movi_i32(TCGV_LOW(ret), 0);
739c896fe29Sbellard         }
740c896fe29Sbellard     } else {
741a7812ae4Spbrook         TCGv_i32 t0, t1;
742c896fe29Sbellard 
743a7812ae4Spbrook         t0 = tcg_temp_new_i32();
744a7812ae4Spbrook         t1 = tcg_temp_new_i32();
745c896fe29Sbellard         if (right) {
746ac56dd48Spbrook             tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c);
747c896fe29Sbellard             if (arith)
748ac56dd48Spbrook                 tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c);
749c896fe29Sbellard             else
750ac56dd48Spbrook                 tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c);
751a7812ae4Spbrook             tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_LOW(arg1), c);
752a7812ae4Spbrook             tcg_gen_or_i32(TCGV_LOW(ret), TCGV_LOW(ret), t0);
753ac56dd48Spbrook             tcg_gen_mov_i32(TCGV_HIGH(ret), t1);
754c896fe29Sbellard         } else {
755a7812ae4Spbrook             tcg_gen_shri_i32(t0, TCGV_LOW(arg1), 32 - c);
756c896fe29Sbellard             /* Note: ret can be the same as arg1, so we use t1 */
757a7812ae4Spbrook             tcg_gen_shli_i32(t1, TCGV_LOW(arg1), c);
758ac56dd48Spbrook             tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c);
759ac56dd48Spbrook             tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0);
760a7812ae4Spbrook             tcg_gen_mov_i32(TCGV_LOW(ret), t1);
761c896fe29Sbellard         }
762a7812ae4Spbrook         tcg_temp_free_i32(t0);
763a7812ae4Spbrook         tcg_temp_free_i32(t1);
764c896fe29Sbellard     }
765c896fe29Sbellard }
766ac56dd48Spbrook #endif
767c896fe29Sbellard 
768be210acbSRichard Henderson 
7698fcd3692Sblueswir1 static void tcg_reg_alloc_start(TCGContext *s)
770c896fe29Sbellard {
771c896fe29Sbellard     int i;
772c896fe29Sbellard     TCGTemp *ts;
773c896fe29Sbellard     for(i = 0; i < s->nb_globals; i++) {
774c896fe29Sbellard         ts = &s->temps[i];
775c896fe29Sbellard         if (ts->fixed_reg) {
776c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
777c896fe29Sbellard         } else {
778c896fe29Sbellard             ts->val_type = TEMP_VAL_MEM;
779c896fe29Sbellard         }
780c896fe29Sbellard     }
781e8996ee0Sbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
782e8996ee0Sbellard         ts = &s->temps[i];
7837dfd8c6aSAurelien Jarno         if (ts->temp_local) {
7847dfd8c6aSAurelien Jarno             ts->val_type = TEMP_VAL_MEM;
7857dfd8c6aSAurelien Jarno         } else {
786e8996ee0Sbellard             ts->val_type = TEMP_VAL_DEAD;
7877dfd8c6aSAurelien Jarno         }
788e8996ee0Sbellard         ts->mem_allocated = 0;
789e8996ee0Sbellard         ts->fixed_reg = 0;
790e8996ee0Sbellard     }
791c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
792c896fe29Sbellard         s->reg_to_temp[i] = -1;
793c896fe29Sbellard     }
794c896fe29Sbellard }
795c896fe29Sbellard 
796ac56dd48Spbrook static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size,
797ac56dd48Spbrook                                  int idx)
798c896fe29Sbellard {
799c896fe29Sbellard     TCGTemp *ts;
800ac56dd48Spbrook 
8017f6f0ae5SStefan Weil     assert(idx >= 0 && idx < s->nb_temps);
802ac56dd48Spbrook     ts = &s->temps[idx];
8037f6f0ae5SStefan Weil     assert(ts);
804ac56dd48Spbrook     if (idx < s->nb_globals) {
805ac56dd48Spbrook         pstrcpy(buf, buf_size, ts->name);
806c896fe29Sbellard     } else {
807641d5fbeSbellard         if (ts->temp_local)
808641d5fbeSbellard             snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
809641d5fbeSbellard         else
810ac56dd48Spbrook             snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
811c896fe29Sbellard     }
812c896fe29Sbellard     return buf;
813c896fe29Sbellard }
814c896fe29Sbellard 
815a7812ae4Spbrook char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg)
816ac56dd48Spbrook {
817a7812ae4Spbrook     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I32(arg));
818a7812ae4Spbrook }
819a7812ae4Spbrook 
820a7812ae4Spbrook char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg)
821a7812ae4Spbrook {
822a810a2deSblueswir1     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I64(arg));
823ac56dd48Spbrook }
824ac56dd48Spbrook 
825e8996ee0Sbellard static int helper_cmp(const void *p1, const void *p2)
826e8996ee0Sbellard {
827e8996ee0Sbellard     const TCGHelperInfo *th1 = p1;
828e8996ee0Sbellard     const TCGHelperInfo *th2 = p2;
829e8996ee0Sbellard     if (th1->func < th2->func)
830e8996ee0Sbellard         return -1;
831e8996ee0Sbellard     else if (th1->func == th2->func)
832e8996ee0Sbellard         return 0;
833e8996ee0Sbellard     else
834e8996ee0Sbellard         return 1;
835e8996ee0Sbellard }
836e8996ee0Sbellard 
837e8996ee0Sbellard /* find helper definition (Note: A hash table would be better) */
8384dc81f28Sbellard static TCGHelperInfo *tcg_find_helper(TCGContext *s, tcg_target_ulong val)
8394dc81f28Sbellard {
840e8996ee0Sbellard     int m, m_min, m_max;
841e8996ee0Sbellard     TCGHelperInfo *th;
842e8996ee0Sbellard     tcg_target_ulong v;
843e8996ee0Sbellard 
844e8996ee0Sbellard     if (unlikely(!s->helpers_sorted)) {
845e8996ee0Sbellard         qsort(s->helpers, s->nb_helpers, sizeof(TCGHelperInfo),
846e8996ee0Sbellard               helper_cmp);
847e8996ee0Sbellard         s->helpers_sorted = 1;
848e8996ee0Sbellard     }
849e8996ee0Sbellard 
850e8996ee0Sbellard     /* binary search */
851e8996ee0Sbellard     m_min = 0;
852e8996ee0Sbellard     m_max = s->nb_helpers - 1;
853e8996ee0Sbellard     while (m_min <= m_max) {
854e8996ee0Sbellard         m = (m_min + m_max) >> 1;
855e8996ee0Sbellard         th = &s->helpers[m];
856e8996ee0Sbellard         v = th->func;
857e8996ee0Sbellard         if (v == val)
858e8996ee0Sbellard             return th;
859e8996ee0Sbellard         else if (val < v) {
860e8996ee0Sbellard             m_max = m - 1;
861e8996ee0Sbellard         } else {
862e8996ee0Sbellard             m_min = m + 1;
863e8996ee0Sbellard         }
8644dc81f28Sbellard     }
8654dc81f28Sbellard     return NULL;
8664dc81f28Sbellard }
8674dc81f28Sbellard 
868f48f3edeSblueswir1 static const char * const cond_name[] =
869f48f3edeSblueswir1 {
8700aed257fSRichard Henderson     [TCG_COND_NEVER] = "never",
8710aed257fSRichard Henderson     [TCG_COND_ALWAYS] = "always",
872f48f3edeSblueswir1     [TCG_COND_EQ] = "eq",
873f48f3edeSblueswir1     [TCG_COND_NE] = "ne",
874f48f3edeSblueswir1     [TCG_COND_LT] = "lt",
875f48f3edeSblueswir1     [TCG_COND_GE] = "ge",
876f48f3edeSblueswir1     [TCG_COND_LE] = "le",
877f48f3edeSblueswir1     [TCG_COND_GT] = "gt",
878f48f3edeSblueswir1     [TCG_COND_LTU] = "ltu",
879f48f3edeSblueswir1     [TCG_COND_GEU] = "geu",
880f48f3edeSblueswir1     [TCG_COND_LEU] = "leu",
881f48f3edeSblueswir1     [TCG_COND_GTU] = "gtu"
882f48f3edeSblueswir1 };
883f48f3edeSblueswir1 
884eeacee4dSBlue Swirl void tcg_dump_ops(TCGContext *s)
885c896fe29Sbellard {
886c896fe29Sbellard     const uint16_t *opc_ptr;
887c896fe29Sbellard     const TCGArg *args;
888c896fe29Sbellard     TCGArg arg;
889a9751609SRichard Henderson     TCGOpcode c;
890a9751609SRichard Henderson     int i, k, nb_oargs, nb_iargs, nb_cargs, first_insn;
891c896fe29Sbellard     const TCGOpDef *def;
892c896fe29Sbellard     char buf[128];
893c896fe29Sbellard 
8947e4597d7Sbellard     first_insn = 1;
89592414b31SEvgeny Voevodin     opc_ptr = s->gen_opc_buf;
8961ff0a2c5SEvgeny Voevodin     args = s->gen_opparam_buf;
897efd7f486SEvgeny Voevodin     while (opc_ptr < s->gen_opc_ptr) {
898c896fe29Sbellard         c = *opc_ptr++;
899c896fe29Sbellard         def = &tcg_op_defs[c];
9007e4597d7Sbellard         if (c == INDEX_op_debug_insn_start) {
9017e4597d7Sbellard             uint64_t pc;
9027e4597d7Sbellard #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
9037e4597d7Sbellard             pc = ((uint64_t)args[1] << 32) | args[0];
9047e4597d7Sbellard #else
9057e4597d7Sbellard             pc = args[0];
9067e4597d7Sbellard #endif
907eeacee4dSBlue Swirl             if (!first_insn) {
908eeacee4dSBlue Swirl                 qemu_log("\n");
909eeacee4dSBlue Swirl             }
910eeacee4dSBlue Swirl             qemu_log(" ---- 0x%" PRIx64, pc);
9117e4597d7Sbellard             first_insn = 0;
9127e4597d7Sbellard             nb_oargs = def->nb_oargs;
9137e4597d7Sbellard             nb_iargs = def->nb_iargs;
9147e4597d7Sbellard             nb_cargs = def->nb_cargs;
9157e4597d7Sbellard         } else if (c == INDEX_op_call) {
916c896fe29Sbellard             TCGArg arg;
9174dc81f28Sbellard 
918c896fe29Sbellard             /* variable number of arguments */
919c896fe29Sbellard             arg = *args++;
920c896fe29Sbellard             nb_oargs = arg >> 16;
921c896fe29Sbellard             nb_iargs = arg & 0xffff;
922c896fe29Sbellard             nb_cargs = def->nb_cargs;
923b03cce8eSbellard 
924eeacee4dSBlue Swirl             qemu_log(" %s ", def->name);
9257e4597d7Sbellard 
926b03cce8eSbellard             /* function name */
927eeacee4dSBlue Swirl             qemu_log("%s",
928eeacee4dSBlue Swirl                      tcg_get_arg_str_idx(s, buf, sizeof(buf),
929eeacee4dSBlue Swirl                                          args[nb_oargs + nb_iargs - 1]));
930b03cce8eSbellard             /* flags */
931eeacee4dSBlue Swirl             qemu_log(",$0x%" TCG_PRIlx, args[nb_oargs + nb_iargs]);
932b03cce8eSbellard             /* nb out args */
933eeacee4dSBlue Swirl             qemu_log(",$%d", nb_oargs);
934b03cce8eSbellard             for(i = 0; i < nb_oargs; i++) {
935eeacee4dSBlue Swirl                 qemu_log(",");
936eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
937eeacee4dSBlue Swirl                                                    args[i]));
938b03cce8eSbellard             }
939b03cce8eSbellard             for(i = 0; i < (nb_iargs - 1); i++) {
940eeacee4dSBlue Swirl                 qemu_log(",");
94139cf05d3Sbellard                 if (args[nb_oargs + i] == TCG_CALL_DUMMY_ARG) {
942eeacee4dSBlue Swirl                     qemu_log("<dummy>");
94339cf05d3Sbellard                 } else {
944eeacee4dSBlue Swirl                     qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
945eeacee4dSBlue Swirl                                                        args[nb_oargs + i]));
946b03cce8eSbellard                 }
94739cf05d3Sbellard             }
948fba3161fSAurelien Jarno         } else if (c == INDEX_op_movi_i32 || c == INDEX_op_movi_i64) {
949e8996ee0Sbellard             tcg_target_ulong val;
950e8996ee0Sbellard             TCGHelperInfo *th;
951e8996ee0Sbellard 
952e8996ee0Sbellard             nb_oargs = def->nb_oargs;
953e8996ee0Sbellard             nb_iargs = def->nb_iargs;
954e8996ee0Sbellard             nb_cargs = def->nb_cargs;
955eeacee4dSBlue Swirl             qemu_log(" %s %s,$", def->name,
956e8996ee0Sbellard                      tcg_get_arg_str_idx(s, buf, sizeof(buf), args[0]));
957e8996ee0Sbellard             val = args[1];
958e8996ee0Sbellard             th = tcg_find_helper(s, val);
959e8996ee0Sbellard             if (th) {
960eeacee4dSBlue Swirl                 qemu_log("%s", th->name);
961e8996ee0Sbellard             } else {
962eeacee4dSBlue Swirl                 if (c == INDEX_op_movi_i32) {
963eeacee4dSBlue Swirl                     qemu_log("0x%x", (uint32_t)val);
964eeacee4dSBlue Swirl                 } else {
965eeacee4dSBlue Swirl                     qemu_log("0x%" PRIx64 , (uint64_t)val);
966eeacee4dSBlue Swirl                 }
967e8996ee0Sbellard             }
968b03cce8eSbellard         } else {
969eeacee4dSBlue Swirl             qemu_log(" %s ", def->name);
970b03cce8eSbellard             if (c == INDEX_op_nopn) {
971c896fe29Sbellard                 /* variable number of arguments */
972c896fe29Sbellard                 nb_cargs = *args;
973c896fe29Sbellard                 nb_oargs = 0;
974c896fe29Sbellard                 nb_iargs = 0;
975c896fe29Sbellard             } else {
976c896fe29Sbellard                 nb_oargs = def->nb_oargs;
977c896fe29Sbellard                 nb_iargs = def->nb_iargs;
978c896fe29Sbellard                 nb_cargs = def->nb_cargs;
979c896fe29Sbellard             }
980c896fe29Sbellard 
981c896fe29Sbellard             k = 0;
982c896fe29Sbellard             for(i = 0; i < nb_oargs; i++) {
983eeacee4dSBlue Swirl                 if (k != 0) {
984eeacee4dSBlue Swirl                     qemu_log(",");
985eeacee4dSBlue Swirl                 }
986eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
987eeacee4dSBlue Swirl                                                    args[k++]));
988c896fe29Sbellard             }
989c896fe29Sbellard             for(i = 0; i < nb_iargs; i++) {
990eeacee4dSBlue Swirl                 if (k != 0) {
991eeacee4dSBlue Swirl                     qemu_log(",");
992eeacee4dSBlue Swirl                 }
993eeacee4dSBlue Swirl                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
994eeacee4dSBlue Swirl                                                    args[k++]));
995c896fe29Sbellard             }
996be210acbSRichard Henderson             switch (c) {
997be210acbSRichard Henderson             case INDEX_op_brcond_i32:
998ffc5ea09SRichard Henderson             case INDEX_op_setcond_i32:
999ffc5ea09SRichard Henderson             case INDEX_op_movcond_i32:
1000be210acbSRichard Henderson             case INDEX_op_brcond2_i32:
1001be210acbSRichard Henderson             case INDEX_op_setcond2_i32:
1002ffc5ea09SRichard Henderson             case INDEX_op_brcond_i64:
1003be210acbSRichard Henderson             case INDEX_op_setcond_i64:
1004ffc5ea09SRichard Henderson             case INDEX_op_movcond_i64:
1005eeacee4dSBlue Swirl                 if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]]) {
1006eeacee4dSBlue Swirl                     qemu_log(",%s", cond_name[args[k++]]);
1007eeacee4dSBlue Swirl                 } else {
1008eeacee4dSBlue Swirl                     qemu_log(",$0x%" TCG_PRIlx, args[k++]);
1009eeacee4dSBlue Swirl                 }
1010f48f3edeSblueswir1                 i = 1;
1011be210acbSRichard Henderson                 break;
1012be210acbSRichard Henderson             default:
1013f48f3edeSblueswir1                 i = 0;
1014be210acbSRichard Henderson                 break;
1015be210acbSRichard Henderson             }
1016f48f3edeSblueswir1             for(; i < nb_cargs; i++) {
1017eeacee4dSBlue Swirl                 if (k != 0) {
1018eeacee4dSBlue Swirl                     qemu_log(",");
1019eeacee4dSBlue Swirl                 }
1020c896fe29Sbellard                 arg = args[k++];
1021eeacee4dSBlue Swirl                 qemu_log("$0x%" TCG_PRIlx, arg);
1022c896fe29Sbellard             }
1023b03cce8eSbellard         }
1024eeacee4dSBlue Swirl         qemu_log("\n");
1025c896fe29Sbellard         args += nb_iargs + nb_oargs + nb_cargs;
1026c896fe29Sbellard     }
1027c896fe29Sbellard }
1028c896fe29Sbellard 
1029c896fe29Sbellard /* we give more priority to constraints with less registers */
1030c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k)
1031c896fe29Sbellard {
1032c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1033c896fe29Sbellard 
1034c896fe29Sbellard     int i, n;
1035c896fe29Sbellard     arg_ct = &def->args_ct[k];
1036c896fe29Sbellard     if (arg_ct->ct & TCG_CT_ALIAS) {
1037c896fe29Sbellard         /* an alias is equivalent to a single register */
1038c896fe29Sbellard         n = 1;
1039c896fe29Sbellard     } else {
1040c896fe29Sbellard         if (!(arg_ct->ct & TCG_CT_REG))
1041c896fe29Sbellard             return 0;
1042c896fe29Sbellard         n = 0;
1043c896fe29Sbellard         for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1044c896fe29Sbellard             if (tcg_regset_test_reg(arg_ct->u.regs, i))
1045c896fe29Sbellard                 n++;
1046c896fe29Sbellard         }
1047c896fe29Sbellard     }
1048c896fe29Sbellard     return TCG_TARGET_NB_REGS - n + 1;
1049c896fe29Sbellard }
1050c896fe29Sbellard 
1051c896fe29Sbellard /* sort from highest priority to lowest */
1052c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n)
1053c896fe29Sbellard {
1054c896fe29Sbellard     int i, j, p1, p2, tmp;
1055c896fe29Sbellard 
1056c896fe29Sbellard     for(i = 0; i < n; i++)
1057c896fe29Sbellard         def->sorted_args[start + i] = start + i;
1058c896fe29Sbellard     if (n <= 1)
1059c896fe29Sbellard         return;
1060c896fe29Sbellard     for(i = 0; i < n - 1; i++) {
1061c896fe29Sbellard         for(j = i + 1; j < n; j++) {
1062c896fe29Sbellard             p1 = get_constraint_priority(def, def->sorted_args[start + i]);
1063c896fe29Sbellard             p2 = get_constraint_priority(def, def->sorted_args[start + j]);
1064c896fe29Sbellard             if (p1 < p2) {
1065c896fe29Sbellard                 tmp = def->sorted_args[start + i];
1066c896fe29Sbellard                 def->sorted_args[start + i] = def->sorted_args[start + j];
1067c896fe29Sbellard                 def->sorted_args[start + j] = tmp;
1068c896fe29Sbellard             }
1069c896fe29Sbellard         }
1070c896fe29Sbellard     }
1071c896fe29Sbellard }
1072c896fe29Sbellard 
1073c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
1074c896fe29Sbellard {
1075a9751609SRichard Henderson     TCGOpcode op;
1076c896fe29Sbellard     TCGOpDef *def;
1077c896fe29Sbellard     const char *ct_str;
1078c896fe29Sbellard     int i, nb_args;
1079c896fe29Sbellard 
1080c896fe29Sbellard     for(;;) {
1081a9751609SRichard Henderson         if (tdefs->op == (TCGOpcode)-1)
1082c896fe29Sbellard             break;
1083c896fe29Sbellard         op = tdefs->op;
1084c3b08d0eSStefan Weil         assert((unsigned)op < NB_OPS);
1085c896fe29Sbellard         def = &tcg_op_defs[op];
1086c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG)
1087c68aaa18SStefan Weil         /* Duplicate entry in op definitions? */
1088c68aaa18SStefan Weil         assert(!def->used);
1089c68aaa18SStefan Weil         def->used = 1;
1090c68aaa18SStefan Weil #endif
1091c896fe29Sbellard         nb_args = def->nb_iargs + def->nb_oargs;
1092c896fe29Sbellard         for(i = 0; i < nb_args; i++) {
1093c896fe29Sbellard             ct_str = tdefs->args_ct_str[i];
1094c68aaa18SStefan Weil             /* Incomplete TCGTargetOpDef entry? */
1095c68aaa18SStefan Weil             assert(ct_str != NULL);
1096c896fe29Sbellard             tcg_regset_clear(def->args_ct[i].u.regs);
1097c896fe29Sbellard             def->args_ct[i].ct = 0;
1098c896fe29Sbellard             if (ct_str[0] >= '0' && ct_str[0] <= '9') {
1099c896fe29Sbellard                 int oarg;
1100c896fe29Sbellard                 oarg = ct_str[0] - '0';
1101c896fe29Sbellard                 assert(oarg < def->nb_oargs);
1102c896fe29Sbellard                 assert(def->args_ct[oarg].ct & TCG_CT_REG);
1103c896fe29Sbellard                 /* TCG_CT_ALIAS is for the output arguments. The input
11045ff9d6a4Sbellard                    argument is tagged with TCG_CT_IALIAS. */
1105c896fe29Sbellard                 def->args_ct[i] = def->args_ct[oarg];
11065ff9d6a4Sbellard                 def->args_ct[oarg].ct = TCG_CT_ALIAS;
11075ff9d6a4Sbellard                 def->args_ct[oarg].alias_index = i;
1108c896fe29Sbellard                 def->args_ct[i].ct |= TCG_CT_IALIAS;
11095ff9d6a4Sbellard                 def->args_ct[i].alias_index = oarg;
1110c896fe29Sbellard             } else {
1111c896fe29Sbellard                 for(;;) {
1112c896fe29Sbellard                     if (*ct_str == '\0')
1113c896fe29Sbellard                         break;
1114c896fe29Sbellard                     switch(*ct_str) {
1115c896fe29Sbellard                     case 'i':
1116c896fe29Sbellard                         def->args_ct[i].ct |= TCG_CT_CONST;
1117c896fe29Sbellard                         ct_str++;
1118c896fe29Sbellard                         break;
1119c896fe29Sbellard                     default:
1120c896fe29Sbellard                         if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
1121c896fe29Sbellard                             fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
1122c896fe29Sbellard                                     ct_str, i, def->name);
1123c896fe29Sbellard                             exit(1);
1124c896fe29Sbellard                         }
1125c896fe29Sbellard                     }
1126c896fe29Sbellard                 }
1127c896fe29Sbellard             }
1128c896fe29Sbellard         }
1129c896fe29Sbellard 
1130c68aaa18SStefan Weil         /* TCGTargetOpDef entry with too much information? */
1131c68aaa18SStefan Weil         assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
1132c68aaa18SStefan Weil 
1133c896fe29Sbellard         /* sort the constraints (XXX: this is just an heuristic) */
1134c896fe29Sbellard         sort_constraints(def, 0, def->nb_oargs);
1135c896fe29Sbellard         sort_constraints(def, def->nb_oargs, def->nb_iargs);
1136c896fe29Sbellard 
1137c896fe29Sbellard #if 0
1138c896fe29Sbellard         {
1139c896fe29Sbellard             int i;
1140c896fe29Sbellard 
1141c896fe29Sbellard             printf("%s: sorted=", def->name);
1142c896fe29Sbellard             for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
1143c896fe29Sbellard                 printf(" %d", def->sorted_args[i]);
1144c896fe29Sbellard             printf("\n");
1145c896fe29Sbellard         }
1146c896fe29Sbellard #endif
1147c896fe29Sbellard         tdefs++;
1148c896fe29Sbellard     }
1149c896fe29Sbellard 
1150c68aaa18SStefan Weil #if defined(CONFIG_DEBUG_TCG)
1151a9751609SRichard Henderson     i = 0;
1152c68aaa18SStefan Weil     for (op = 0; op < ARRAY_SIZE(tcg_op_defs); op++) {
1153f412c762SRichard Henderson         const TCGOpDef *def = &tcg_op_defs[op];
1154f412c762SRichard Henderson         if (op < INDEX_op_call
1155f412c762SRichard Henderson             || op == INDEX_op_debug_insn_start
1156f412c762SRichard Henderson             || (def->flags & TCG_OPF_NOT_PRESENT)) {
1157c68aaa18SStefan Weil             /* Wrong entry in op definitions? */
1158f412c762SRichard Henderson             if (def->used) {
1159f412c762SRichard Henderson                 fprintf(stderr, "Invalid op definition for %s\n", def->name);
1160a9751609SRichard Henderson                 i = 1;
1161a9751609SRichard Henderson             }
1162c68aaa18SStefan Weil         } else {
1163c68aaa18SStefan Weil             /* Missing entry in op definitions? */
1164f412c762SRichard Henderson             if (!def->used) {
1165f412c762SRichard Henderson                 fprintf(stderr, "Missing op definition for %s\n", def->name);
1166a9751609SRichard Henderson                 i = 1;
1167c68aaa18SStefan Weil             }
1168c68aaa18SStefan Weil         }
1169a9751609SRichard Henderson     }
1170a9751609SRichard Henderson     if (i == 1) {
1171a9751609SRichard Henderson         tcg_abort();
1172a9751609SRichard Henderson     }
1173c68aaa18SStefan Weil #endif
1174c896fe29Sbellard }
1175c896fe29Sbellard 
1176c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS
1177c896fe29Sbellard 
1178c896fe29Sbellard /* set a nop for an operation using 'nb_args' */
1179c896fe29Sbellard static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr,
1180c896fe29Sbellard                                TCGArg *args, int nb_args)
1181c896fe29Sbellard {
1182c896fe29Sbellard     if (nb_args == 0) {
1183c896fe29Sbellard         *opc_ptr = INDEX_op_nop;
1184c896fe29Sbellard     } else {
1185c896fe29Sbellard         *opc_ptr = INDEX_op_nopn;
1186c896fe29Sbellard         args[0] = nb_args;
1187c896fe29Sbellard         args[nb_args - 1] = nb_args;
1188c896fe29Sbellard     }
1189c896fe29Sbellard }
1190c896fe29Sbellard 
11919c43b68dSAurelien Jarno /* liveness analysis: end of function: all temps are dead, and globals
11929c43b68dSAurelien Jarno    should be in memory. */
11939c43b68dSAurelien Jarno static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps,
11949c43b68dSAurelien Jarno                                    uint8_t *mem_temps)
1195c896fe29Sbellard {
11969c43b68dSAurelien Jarno     memset(dead_temps, 1, s->nb_temps);
11979c43b68dSAurelien Jarno     memset(mem_temps, 1, s->nb_globals);
11989c43b68dSAurelien Jarno     memset(mem_temps + s->nb_globals, 0, s->nb_temps - s->nb_globals);
1199c896fe29Sbellard }
1200c896fe29Sbellard 
12019c43b68dSAurelien Jarno /* liveness analysis: end of basic block: all temps are dead, globals
12029c43b68dSAurelien Jarno    and local temps should be in memory. */
12039c43b68dSAurelien Jarno static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
12049c43b68dSAurelien Jarno                                  uint8_t *mem_temps)
1205641d5fbeSbellard {
1206641d5fbeSbellard     int i;
1207641d5fbeSbellard 
12089c43b68dSAurelien Jarno     memset(dead_temps, 1, s->nb_temps);
12099c43b68dSAurelien Jarno     memset(mem_temps, 1, s->nb_globals);
1210641d5fbeSbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
12119c43b68dSAurelien Jarno         mem_temps[i] = s->temps[i].temp_local;
1212641d5fbeSbellard     }
1213641d5fbeSbellard }
1214641d5fbeSbellard 
1215866cb6cbSAurelien Jarno /* Liveness analysis : update the opc_dead_args array to tell if a
1216c896fe29Sbellard    given input arguments is dead. Instructions updating dead
1217c896fe29Sbellard    temporaries are removed. */
12188fcd3692Sblueswir1 static void tcg_liveness_analysis(TCGContext *s)
1219c896fe29Sbellard {
1220a9751609SRichard Henderson     int i, op_index, nb_args, nb_iargs, nb_oargs, arg, nb_ops;
1221a9751609SRichard Henderson     TCGOpcode op;
1222c896fe29Sbellard     TCGArg *args;
1223c896fe29Sbellard     const TCGOpDef *def;
12249c43b68dSAurelien Jarno     uint8_t *dead_temps, *mem_temps;
1225ec7a869dSAurelien Jarno     uint16_t dead_args;
1226ec7a869dSAurelien Jarno     uint8_t sync_args;
1227c896fe29Sbellard 
1228efd7f486SEvgeny Voevodin     s->gen_opc_ptr++; /* skip end */
1229c896fe29Sbellard 
123092414b31SEvgeny Voevodin     nb_ops = s->gen_opc_ptr - s->gen_opc_buf;
1231c896fe29Sbellard 
1232866cb6cbSAurelien Jarno     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1233ec7a869dSAurelien Jarno     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1234c896fe29Sbellard 
1235c896fe29Sbellard     dead_temps = tcg_malloc(s->nb_temps);
12369c43b68dSAurelien Jarno     mem_temps = tcg_malloc(s->nb_temps);
12379c43b68dSAurelien Jarno     tcg_la_func_end(s, dead_temps, mem_temps);
1238c896fe29Sbellard 
1239c4afe5c4SEvgeny Voevodin     args = s->gen_opparam_ptr;
1240c896fe29Sbellard     op_index = nb_ops - 1;
1241c896fe29Sbellard     while (op_index >= 0) {
124292414b31SEvgeny Voevodin         op = s->gen_opc_buf[op_index];
1243c896fe29Sbellard         def = &tcg_op_defs[op];
1244c896fe29Sbellard         switch(op) {
1245c896fe29Sbellard         case INDEX_op_call:
1246c6e113f5Sbellard             {
1247c6e113f5Sbellard                 int call_flags;
1248c6e113f5Sbellard 
1249c896fe29Sbellard                 nb_args = args[-1];
1250c896fe29Sbellard                 args -= nb_args;
1251c896fe29Sbellard                 nb_iargs = args[0] & 0xffff;
1252c896fe29Sbellard                 nb_oargs = args[0] >> 16;
1253c896fe29Sbellard                 args++;
1254c6e113f5Sbellard                 call_flags = args[nb_oargs + nb_iargs];
1255c6e113f5Sbellard 
1256c6e113f5Sbellard                 /* pure functions can be removed if their result is not
1257c6e113f5Sbellard                    used */
125878505279SAurelien Jarno                 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
1259c6e113f5Sbellard                     for(i = 0; i < nb_oargs; i++) {
1260c6e113f5Sbellard                         arg = args[i];
12619c43b68dSAurelien Jarno                         if (!dead_temps[arg] || mem_temps[arg]) {
1262c6e113f5Sbellard                             goto do_not_remove_call;
1263c6e113f5Sbellard                         }
12649c43b68dSAurelien Jarno                     }
126592414b31SEvgeny Voevodin                     tcg_set_nop(s, s->gen_opc_buf + op_index,
1266c6e113f5Sbellard                                 args - 1, nb_args);
1267c6e113f5Sbellard                 } else {
1268c6e113f5Sbellard                 do_not_remove_call:
1269c896fe29Sbellard 
1270c896fe29Sbellard                     /* output args are dead */
12716b64b624SAurelien Jarno                     dead_args = 0;
1272ec7a869dSAurelien Jarno                     sync_args = 0;
1273c896fe29Sbellard                     for(i = 0; i < nb_oargs; i++) {
1274c896fe29Sbellard                         arg = args[i];
12756b64b624SAurelien Jarno                         if (dead_temps[arg]) {
12766b64b624SAurelien Jarno                             dead_args |= (1 << i);
12776b64b624SAurelien Jarno                         }
12789c43b68dSAurelien Jarno                         if (mem_temps[arg]) {
12799c43b68dSAurelien Jarno                             sync_args |= (1 << i);
12809c43b68dSAurelien Jarno                         }
1281c896fe29Sbellard                         dead_temps[arg] = 1;
12829c43b68dSAurelien Jarno                         mem_temps[arg] = 0;
1283c896fe29Sbellard                     }
1284c896fe29Sbellard 
128578505279SAurelien Jarno                     if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
128678505279SAurelien Jarno                         /* globals should be synced to memory */
128778505279SAurelien Jarno                         memset(mem_temps, 1, s->nb_globals);
128878505279SAurelien Jarno                     }
128978505279SAurelien Jarno                     if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
129078505279SAurelien Jarno                                         TCG_CALL_NO_READ_GLOBALS))) {
12919c43b68dSAurelien Jarno                         /* globals should go back to memory */
12929c43b68dSAurelien Jarno                         memset(dead_temps, 1, s->nb_globals);
1293b9c18f56Saurel32                     }
1294c896fe29Sbellard 
1295c896fe29Sbellard                     /* input args are live */
1296866cb6cbSAurelien Jarno                     for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
1297866cb6cbSAurelien Jarno                         arg = args[i];
129839cf05d3Sbellard                         if (arg != TCG_CALL_DUMMY_ARG) {
1299c896fe29Sbellard                             if (dead_temps[arg]) {
1300866cb6cbSAurelien Jarno                                 dead_args |= (1 << i);
1301c896fe29Sbellard                             }
1302c896fe29Sbellard                             dead_temps[arg] = 0;
1303c896fe29Sbellard                         }
130439cf05d3Sbellard                     }
1305866cb6cbSAurelien Jarno                     s->op_dead_args[op_index] = dead_args;
1306ec7a869dSAurelien Jarno                     s->op_sync_args[op_index] = sync_args;
1307c6e113f5Sbellard                 }
1308c896fe29Sbellard                 args--;
1309c6e113f5Sbellard             }
1310c896fe29Sbellard             break;
13117e4597d7Sbellard         case INDEX_op_debug_insn_start:
13127e4597d7Sbellard             args -= def->nb_args;
13137e4597d7Sbellard             break;
1314c896fe29Sbellard         case INDEX_op_nopn:
1315c896fe29Sbellard             nb_args = args[-1];
1316c896fe29Sbellard             args -= nb_args;
1317c896fe29Sbellard             break;
13185ff9d6a4Sbellard         case INDEX_op_discard:
13195ff9d6a4Sbellard             args--;
13205ff9d6a4Sbellard             /* mark the temporary as dead */
13215ff9d6a4Sbellard             dead_temps[args[0]] = 1;
13229c43b68dSAurelien Jarno             mem_temps[args[0]] = 0;
13235ff9d6a4Sbellard             break;
1324c896fe29Sbellard         case INDEX_op_end:
1325c896fe29Sbellard             break;
13261305c451SRichard Henderson 
13271305c451SRichard Henderson         case INDEX_op_add2_i32:
13281305c451SRichard Henderson         case INDEX_op_sub2_i32:
13291305c451SRichard Henderson             args -= 6;
13301305c451SRichard Henderson             nb_iargs = 4;
13311305c451SRichard Henderson             nb_oargs = 2;
13321305c451SRichard Henderson             /* Test if the high part of the operation is dead, but not
13331305c451SRichard Henderson                the low part.  The result can be optimized to a simple
13341305c451SRichard Henderson                add or sub.  This happens often for x86_64 guest when the
13351305c451SRichard Henderson                cpu mode is set to 32 bit.  */
13363c5645faSKirill Batuzov             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
13373c5645faSKirill Batuzov                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
13381305c451SRichard Henderson                     goto do_remove;
13391305c451SRichard Henderson                 }
13401305c451SRichard Henderson                 /* Create the single operation plus nop.  */
13411305c451SRichard Henderson                 if (op == INDEX_op_add2_i32) {
13421305c451SRichard Henderson                     op = INDEX_op_add_i32;
13431305c451SRichard Henderson                 } else {
13441305c451SRichard Henderson                     op = INDEX_op_sub_i32;
13451305c451SRichard Henderson                 }
134692414b31SEvgeny Voevodin                 s->gen_opc_buf[op_index] = op;
13471305c451SRichard Henderson                 args[1] = args[2];
13481305c451SRichard Henderson                 args[2] = args[4];
134992414b31SEvgeny Voevodin                 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
135092414b31SEvgeny Voevodin                 tcg_set_nop(s, s->gen_opc_buf + op_index + 1, args + 3, 3);
13511305c451SRichard Henderson                 /* Fall through and mark the single-word operation live.  */
13521305c451SRichard Henderson                 nb_iargs = 2;
13531305c451SRichard Henderson                 nb_oargs = 1;
13541305c451SRichard Henderson             }
13551305c451SRichard Henderson             goto do_not_remove;
13561305c451SRichard Henderson 
13571414968aSRichard Henderson         case INDEX_op_mulu2_i32:
13581414968aSRichard Henderson             args -= 4;
13591414968aSRichard Henderson             nb_iargs = 2;
13601414968aSRichard Henderson             nb_oargs = 2;
13611414968aSRichard Henderson             /* Likewise, test for the high part of the operation dead.  */
13623c5645faSKirill Batuzov             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
13633c5645faSKirill Batuzov                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
13641414968aSRichard Henderson                     goto do_remove;
13651414968aSRichard Henderson                 }
136692414b31SEvgeny Voevodin                 s->gen_opc_buf[op_index] = op = INDEX_op_mul_i32;
13671414968aSRichard Henderson                 args[1] = args[2];
13681414968aSRichard Henderson                 args[2] = args[3];
136992414b31SEvgeny Voevodin                 assert(s->gen_opc_buf[op_index + 1] == INDEX_op_nop);
137092414b31SEvgeny Voevodin                 tcg_set_nop(s, s->gen_opc_buf + op_index + 1, args + 3, 1);
13711414968aSRichard Henderson                 /* Fall through and mark the single-word operation live.  */
13721414968aSRichard Henderson                 nb_oargs = 1;
13731414968aSRichard Henderson             }
13741414968aSRichard Henderson             goto do_not_remove;
13751414968aSRichard Henderson 
1376c896fe29Sbellard         default:
13771305c451SRichard Henderson             /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1378c896fe29Sbellard             args -= def->nb_args;
1379c896fe29Sbellard             nb_iargs = def->nb_iargs;
1380c896fe29Sbellard             nb_oargs = def->nb_oargs;
1381c896fe29Sbellard 
1382c896fe29Sbellard             /* Test if the operation can be removed because all
13835ff9d6a4Sbellard                its outputs are dead. We assume that nb_oargs == 0
13845ff9d6a4Sbellard                implies side effects */
13855ff9d6a4Sbellard             if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
1386c896fe29Sbellard                 for(i = 0; i < nb_oargs; i++) {
1387c896fe29Sbellard                     arg = args[i];
13889c43b68dSAurelien Jarno                     if (!dead_temps[arg] || mem_temps[arg]) {
1389c896fe29Sbellard                         goto do_not_remove;
1390c896fe29Sbellard                     }
13919c43b68dSAurelien Jarno                 }
13921305c451SRichard Henderson             do_remove:
139392414b31SEvgeny Voevodin                 tcg_set_nop(s, s->gen_opc_buf + op_index, args, def->nb_args);
1394c896fe29Sbellard #ifdef CONFIG_PROFILER
1395a23a9ec6Sbellard                 s->del_op_count++;
1396c896fe29Sbellard #endif
1397c896fe29Sbellard             } else {
1398c896fe29Sbellard             do_not_remove:
1399c896fe29Sbellard 
1400c896fe29Sbellard                 /* output args are dead */
14016b64b624SAurelien Jarno                 dead_args = 0;
1402ec7a869dSAurelien Jarno                 sync_args = 0;
1403c896fe29Sbellard                 for(i = 0; i < nb_oargs; i++) {
1404c896fe29Sbellard                     arg = args[i];
14056b64b624SAurelien Jarno                     if (dead_temps[arg]) {
14066b64b624SAurelien Jarno                         dead_args |= (1 << i);
14076b64b624SAurelien Jarno                     }
14089c43b68dSAurelien Jarno                     if (mem_temps[arg]) {
14099c43b68dSAurelien Jarno                         sync_args |= (1 << i);
14109c43b68dSAurelien Jarno                     }
1411c896fe29Sbellard                     dead_temps[arg] = 1;
14129c43b68dSAurelien Jarno                     mem_temps[arg] = 0;
1413c896fe29Sbellard                 }
1414c896fe29Sbellard 
1415c896fe29Sbellard                 /* if end of basic block, update */
1416c896fe29Sbellard                 if (def->flags & TCG_OPF_BB_END) {
14179c43b68dSAurelien Jarno                     tcg_la_bb_end(s, dead_temps, mem_temps);
14183d5c5f87SAurelien Jarno                 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
14193d5c5f87SAurelien Jarno                     /* globals should be synced to memory */
14209c43b68dSAurelien Jarno                     memset(mem_temps, 1, s->nb_globals);
1421c896fe29Sbellard                 }
1422c896fe29Sbellard 
1423c896fe29Sbellard                 /* input args are live */
1424866cb6cbSAurelien Jarno                 for(i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1425866cb6cbSAurelien Jarno                     arg = args[i];
1426c896fe29Sbellard                     if (dead_temps[arg]) {
1427866cb6cbSAurelien Jarno                         dead_args |= (1 << i);
1428c896fe29Sbellard                     }
1429c896fe29Sbellard                     dead_temps[arg] = 0;
1430c896fe29Sbellard                 }
1431866cb6cbSAurelien Jarno                 s->op_dead_args[op_index] = dead_args;
1432ec7a869dSAurelien Jarno                 s->op_sync_args[op_index] = sync_args;
1433c896fe29Sbellard             }
1434c896fe29Sbellard             break;
1435c896fe29Sbellard         }
1436c896fe29Sbellard         op_index--;
1437c896fe29Sbellard     }
1438c896fe29Sbellard 
14391ff0a2c5SEvgeny Voevodin     if (args != s->gen_opparam_buf) {
1440c896fe29Sbellard         tcg_abort();
1441c896fe29Sbellard     }
14421ff0a2c5SEvgeny Voevodin }
1443c896fe29Sbellard #else
1444c896fe29Sbellard /* dummy liveness analysis */
1445655feed5Smalc static void tcg_liveness_analysis(TCGContext *s)
1446c896fe29Sbellard {
1447c896fe29Sbellard     int nb_ops;
144892414b31SEvgeny Voevodin     nb_ops = s->gen_opc_ptr - s->gen_opc_buf;
1449c896fe29Sbellard 
1450866cb6cbSAurelien Jarno     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1451866cb6cbSAurelien Jarno     memset(s->op_dead_args, 0, nb_ops * sizeof(uint16_t));
1452ec7a869dSAurelien Jarno     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1453ec7a869dSAurelien Jarno     memset(s->op_sync_args, 0, nb_ops * sizeof(uint8_t));
1454c896fe29Sbellard }
1455c896fe29Sbellard #endif
1456c896fe29Sbellard 
1457c896fe29Sbellard #ifndef NDEBUG
1458c896fe29Sbellard static void dump_regs(TCGContext *s)
1459c896fe29Sbellard {
1460c896fe29Sbellard     TCGTemp *ts;
1461c896fe29Sbellard     int i;
1462c896fe29Sbellard     char buf[64];
1463c896fe29Sbellard 
1464c896fe29Sbellard     for(i = 0; i < s->nb_temps; i++) {
1465c896fe29Sbellard         ts = &s->temps[i];
1466ac56dd48Spbrook         printf("  %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
1467c896fe29Sbellard         switch(ts->val_type) {
1468c896fe29Sbellard         case TEMP_VAL_REG:
1469c896fe29Sbellard             printf("%s", tcg_target_reg_names[ts->reg]);
1470c896fe29Sbellard             break;
1471c896fe29Sbellard         case TEMP_VAL_MEM:
1472c896fe29Sbellard             printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
1473c896fe29Sbellard             break;
1474c896fe29Sbellard         case TEMP_VAL_CONST:
1475c896fe29Sbellard             printf("$0x%" TCG_PRIlx, ts->val);
1476c896fe29Sbellard             break;
1477c896fe29Sbellard         case TEMP_VAL_DEAD:
1478c896fe29Sbellard             printf("D");
1479c896fe29Sbellard             break;
1480c896fe29Sbellard         default:
1481c896fe29Sbellard             printf("???");
1482c896fe29Sbellard             break;
1483c896fe29Sbellard         }
1484c896fe29Sbellard         printf("\n");
1485c896fe29Sbellard     }
1486c896fe29Sbellard 
1487c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1488c896fe29Sbellard         if (s->reg_to_temp[i] >= 0) {
1489c896fe29Sbellard             printf("%s: %s\n",
1490c896fe29Sbellard                    tcg_target_reg_names[i],
1491ac56dd48Spbrook                    tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i]));
1492c896fe29Sbellard         }
1493c896fe29Sbellard     }
1494c896fe29Sbellard }
1495c896fe29Sbellard 
1496c896fe29Sbellard static void check_regs(TCGContext *s)
1497c896fe29Sbellard {
1498c896fe29Sbellard     int reg, k;
1499c896fe29Sbellard     TCGTemp *ts;
1500c896fe29Sbellard     char buf[64];
1501c896fe29Sbellard 
1502c896fe29Sbellard     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1503c896fe29Sbellard         k = s->reg_to_temp[reg];
1504c896fe29Sbellard         if (k >= 0) {
1505c896fe29Sbellard             ts = &s->temps[k];
1506c896fe29Sbellard             if (ts->val_type != TEMP_VAL_REG ||
1507c896fe29Sbellard                 ts->reg != reg) {
1508c896fe29Sbellard                 printf("Inconsistency for register %s:\n",
1509c896fe29Sbellard                        tcg_target_reg_names[reg]);
1510b03cce8eSbellard                 goto fail;
1511c896fe29Sbellard             }
1512c896fe29Sbellard         }
1513c896fe29Sbellard     }
1514c896fe29Sbellard     for(k = 0; k < s->nb_temps; k++) {
1515c896fe29Sbellard         ts = &s->temps[k];
1516c896fe29Sbellard         if (ts->val_type == TEMP_VAL_REG &&
1517c896fe29Sbellard             !ts->fixed_reg &&
1518c896fe29Sbellard             s->reg_to_temp[ts->reg] != k) {
1519c896fe29Sbellard                 printf("Inconsistency for temp %s:\n",
1520ac56dd48Spbrook                        tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
1521b03cce8eSbellard         fail:
1522c896fe29Sbellard                 printf("reg state:\n");
1523c896fe29Sbellard                 dump_regs(s);
1524c896fe29Sbellard                 tcg_abort();
1525c896fe29Sbellard         }
1526c896fe29Sbellard     }
1527c896fe29Sbellard }
1528c896fe29Sbellard #endif
1529c896fe29Sbellard 
1530c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp)
1531c896fe29Sbellard {
1532c896fe29Sbellard     TCGTemp *ts;
1533c896fe29Sbellard     ts = &s->temps[temp];
15349b9c37c3SRichard Henderson #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
15359b9c37c3SRichard Henderson     /* Sparc64 stack is accessed with offset of 2047 */
1536b591dc59SBlue Swirl     s->current_frame_offset = (s->current_frame_offset +
1537b591dc59SBlue Swirl                                (tcg_target_long)sizeof(tcg_target_long) - 1) &
1538b591dc59SBlue Swirl         ~(sizeof(tcg_target_long) - 1);
1539f44c9960SBlue Swirl #endif
1540b591dc59SBlue Swirl     if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
1541b591dc59SBlue Swirl         s->frame_end) {
15425ff9d6a4Sbellard         tcg_abort();
1543b591dc59SBlue Swirl     }
1544c896fe29Sbellard     ts->mem_offset = s->current_frame_offset;
1545c896fe29Sbellard     ts->mem_reg = s->frame_reg;
1546c896fe29Sbellard     ts->mem_allocated = 1;
1547b591dc59SBlue Swirl     s->current_frame_offset += (tcg_target_long)sizeof(tcg_target_long);
1548c896fe29Sbellard }
1549c896fe29Sbellard 
15507f6ceedfSAurelien Jarno /* sync register 'reg' by saving it to the corresponding temporary */
15517f6ceedfSAurelien Jarno static inline void tcg_reg_sync(TCGContext *s, int reg)
1552c896fe29Sbellard {
1553c896fe29Sbellard     TCGTemp *ts;
1554c896fe29Sbellard     int temp;
1555c896fe29Sbellard 
1556c896fe29Sbellard     temp = s->reg_to_temp[reg];
1557c896fe29Sbellard     ts = &s->temps[temp];
1558c896fe29Sbellard     assert(ts->val_type == TEMP_VAL_REG);
15597f6ceedfSAurelien Jarno     if (!ts->mem_coherent && !ts->fixed_reg) {
15607f6ceedfSAurelien Jarno         if (!ts->mem_allocated) {
1561c896fe29Sbellard             temp_allocate_frame(s, temp);
15627f6ceedfSAurelien Jarno         }
1563e4d5434cSblueswir1         tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1564c896fe29Sbellard     }
15657f6ceedfSAurelien Jarno     ts->mem_coherent = 1;
15667f6ceedfSAurelien Jarno }
15677f6ceedfSAurelien Jarno 
15687f6ceedfSAurelien Jarno /* free register 'reg' by spilling the corresponding temporary if necessary */
15697f6ceedfSAurelien Jarno static void tcg_reg_free(TCGContext *s, int reg)
15707f6ceedfSAurelien Jarno {
15717f6ceedfSAurelien Jarno     int temp;
15727f6ceedfSAurelien Jarno 
15737f6ceedfSAurelien Jarno     temp = s->reg_to_temp[reg];
15747f6ceedfSAurelien Jarno     if (temp != -1) {
15757f6ceedfSAurelien Jarno         tcg_reg_sync(s, reg);
15767f6ceedfSAurelien Jarno         s->temps[temp].val_type = TEMP_VAL_MEM;
1577c896fe29Sbellard         s->reg_to_temp[reg] = -1;
1578c896fe29Sbellard     }
1579c896fe29Sbellard }
1580c896fe29Sbellard 
1581c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */
1582c896fe29Sbellard static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
1583c896fe29Sbellard {
1584c896fe29Sbellard     int i, reg;
1585c896fe29Sbellard     TCGRegSet reg_ct;
1586c896fe29Sbellard 
1587c896fe29Sbellard     tcg_regset_andnot(reg_ct, reg1, reg2);
1588c896fe29Sbellard 
1589c896fe29Sbellard     /* first try free registers */
15900954d0d9Sblueswir1     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1591c896fe29Sbellard         reg = tcg_target_reg_alloc_order[i];
1592c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
1593c896fe29Sbellard             return reg;
1594c896fe29Sbellard     }
1595c896fe29Sbellard 
1596c896fe29Sbellard     /* XXX: do better spill choice */
15970954d0d9Sblueswir1     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1598c896fe29Sbellard         reg = tcg_target_reg_alloc_order[i];
1599c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg)) {
1600c896fe29Sbellard             tcg_reg_free(s, reg);
1601c896fe29Sbellard             return reg;
1602c896fe29Sbellard         }
1603c896fe29Sbellard     }
1604c896fe29Sbellard 
1605c896fe29Sbellard     tcg_abort();
1606c896fe29Sbellard }
1607c896fe29Sbellard 
1608639368ddSAurelien Jarno /* mark a temporary as dead. */
1609639368ddSAurelien Jarno static inline void temp_dead(TCGContext *s, int temp)
1610639368ddSAurelien Jarno {
1611639368ddSAurelien Jarno     TCGTemp *ts;
1612639368ddSAurelien Jarno 
1613639368ddSAurelien Jarno     ts = &s->temps[temp];
1614639368ddSAurelien Jarno     if (!ts->fixed_reg) {
1615639368ddSAurelien Jarno         if (ts->val_type == TEMP_VAL_REG) {
1616639368ddSAurelien Jarno             s->reg_to_temp[ts->reg] = -1;
1617639368ddSAurelien Jarno         }
1618*e5138db5SAurelien Jarno         if (temp < s->nb_globals || ts->temp_local) {
1619639368ddSAurelien Jarno             ts->val_type = TEMP_VAL_MEM;
1620639368ddSAurelien Jarno         } else {
1621639368ddSAurelien Jarno             ts->val_type = TEMP_VAL_DEAD;
1622639368ddSAurelien Jarno         }
1623639368ddSAurelien Jarno     }
1624639368ddSAurelien Jarno }
1625639368ddSAurelien Jarno 
16261ad80729SAurelien Jarno /* sync a temporary to memory. 'allocated_regs' is used in case a
1627e8996ee0Sbellard    temporary registers needs to be allocated to store a constant. */
16281ad80729SAurelien Jarno static inline void temp_sync(TCGContext *s, int temp, TCGRegSet allocated_regs)
1629c896fe29Sbellard {
1630c896fe29Sbellard     TCGTemp *ts;
1631c896fe29Sbellard 
1632641d5fbeSbellard     ts = &s->temps[temp];
1633c896fe29Sbellard     if (!ts->fixed_reg) {
1634e8996ee0Sbellard         switch(ts->val_type) {
16351ad80729SAurelien Jarno         case TEMP_VAL_CONST:
16361ad80729SAurelien Jarno             ts->reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
16371ad80729SAurelien Jarno                                     allocated_regs);
16381ad80729SAurelien Jarno             ts->val_type = TEMP_VAL_REG;
16391ad80729SAurelien Jarno             s->reg_to_temp[ts->reg] = temp;
16401ad80729SAurelien Jarno             ts->mem_coherent = 0;
16411ad80729SAurelien Jarno             tcg_out_movi(s, ts->type, ts->reg, ts->val);
16421ad80729SAurelien Jarno             /* fallthrough*/
1643e8996ee0Sbellard         case TEMP_VAL_REG:
16441ad80729SAurelien Jarno             tcg_reg_sync(s, ts->reg);
1645e8996ee0Sbellard             break;
1646e8996ee0Sbellard         case TEMP_VAL_DEAD:
1647e8996ee0Sbellard         case TEMP_VAL_MEM:
1648e8996ee0Sbellard             break;
1649e8996ee0Sbellard         default:
1650e8996ee0Sbellard             tcg_abort();
1651c896fe29Sbellard         }
1652c896fe29Sbellard     }
1653c896fe29Sbellard }
1654641d5fbeSbellard 
16551ad80729SAurelien Jarno /* save a temporary to memory. 'allocated_regs' is used in case a
16561ad80729SAurelien Jarno    temporary registers needs to be allocated to store a constant. */
16571ad80729SAurelien Jarno static inline void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs)
16581ad80729SAurelien Jarno {
16592c0366f0SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
16602c0366f0SAurelien Jarno     /* The liveness analysis already ensures that globals are back
16612c0366f0SAurelien Jarno        in memory. Keep an assert for safety. */
16622c0366f0SAurelien Jarno     assert(s->temps[temp].val_type == TEMP_VAL_MEM || s->temps[temp].fixed_reg);
16632c0366f0SAurelien Jarno #else
16641ad80729SAurelien Jarno     temp_sync(s, temp, allocated_regs);
16651ad80729SAurelien Jarno     temp_dead(s, temp);
16662c0366f0SAurelien Jarno #endif
16671ad80729SAurelien Jarno }
16681ad80729SAurelien Jarno 
16699814dd27SDong Xu Wang /* save globals to their canonical location and assume they can be
1670641d5fbeSbellard    modified be the following code. 'allocated_regs' is used in case a
1671641d5fbeSbellard    temporary registers needs to be allocated to store a constant. */
1672641d5fbeSbellard static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
1673641d5fbeSbellard {
1674641d5fbeSbellard     int i;
1675641d5fbeSbellard 
1676641d5fbeSbellard     for(i = 0; i < s->nb_globals; i++) {
1677641d5fbeSbellard         temp_save(s, i, allocated_regs);
1678641d5fbeSbellard     }
1679e5097dc8Sbellard }
1680e5097dc8Sbellard 
16813d5c5f87SAurelien Jarno /* sync globals to their canonical location and assume they can be
16823d5c5f87SAurelien Jarno    read by the following code. 'allocated_regs' is used in case a
16833d5c5f87SAurelien Jarno    temporary registers needs to be allocated to store a constant. */
16843d5c5f87SAurelien Jarno static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
16853d5c5f87SAurelien Jarno {
16863d5c5f87SAurelien Jarno     int i;
16873d5c5f87SAurelien Jarno 
16883d5c5f87SAurelien Jarno     for (i = 0; i < s->nb_globals; i++) {
16893d5c5f87SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
16903d5c5f87SAurelien Jarno         assert(s->temps[i].val_type != TEMP_VAL_REG || s->temps[i].fixed_reg ||
16913d5c5f87SAurelien Jarno                s->temps[i].mem_coherent);
16923d5c5f87SAurelien Jarno #else
16933d5c5f87SAurelien Jarno         temp_sync(s, i, allocated_regs);
16943d5c5f87SAurelien Jarno #endif
16953d5c5f87SAurelien Jarno     }
16963d5c5f87SAurelien Jarno }
16973d5c5f87SAurelien Jarno 
1698e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and
1699e8996ee0Sbellard    all globals are stored at their canonical location. */
1700e8996ee0Sbellard static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
1701e5097dc8Sbellard {
1702e5097dc8Sbellard     TCGTemp *ts;
1703e5097dc8Sbellard     int i;
1704e5097dc8Sbellard 
1705c896fe29Sbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
1706c896fe29Sbellard         ts = &s->temps[i];
1707641d5fbeSbellard         if (ts->temp_local) {
1708641d5fbeSbellard             temp_save(s, i, allocated_regs);
1709641d5fbeSbellard         } else {
17102c0366f0SAurelien Jarno #ifdef USE_LIVENESS_ANALYSIS
17112c0366f0SAurelien Jarno             /* The liveness analysis already ensures that temps are dead.
17122c0366f0SAurelien Jarno                Keep an assert for safety. */
17132c0366f0SAurelien Jarno             assert(ts->val_type == TEMP_VAL_DEAD);
17142c0366f0SAurelien Jarno #else
1715639368ddSAurelien Jarno             temp_dead(s, i);
17162c0366f0SAurelien Jarno #endif
1717c896fe29Sbellard         }
1718641d5fbeSbellard     }
1719e8996ee0Sbellard 
1720e8996ee0Sbellard     save_globals(s, allocated_regs);
1721c896fe29Sbellard }
1722c896fe29Sbellard 
1723866cb6cbSAurelien Jarno #define IS_DEAD_ARG(n) ((dead_args >> (n)) & 1)
1724ec7a869dSAurelien Jarno #define NEED_SYNC_ARG(n) ((sync_args >> (n)) & 1)
1725c896fe29Sbellard 
1726ec7a869dSAurelien Jarno static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
1727ec7a869dSAurelien Jarno                                uint16_t dead_args, uint8_t sync_args)
1728e8996ee0Sbellard {
1729e8996ee0Sbellard     TCGTemp *ots;
1730e8996ee0Sbellard     tcg_target_ulong val;
1731e8996ee0Sbellard 
1732e8996ee0Sbellard     ots = &s->temps[args[0]];
1733e8996ee0Sbellard     val = args[1];
1734e8996ee0Sbellard 
1735e8996ee0Sbellard     if (ots->fixed_reg) {
1736e8996ee0Sbellard         /* for fixed registers, we do not do any constant
1737e8996ee0Sbellard            propagation */
1738e8996ee0Sbellard         tcg_out_movi(s, ots->type, ots->reg, val);
1739e8996ee0Sbellard     } else {
17401235fc06Sths         /* The movi is not explicitly generated here */
1741e8996ee0Sbellard         if (ots->val_type == TEMP_VAL_REG)
1742e8996ee0Sbellard             s->reg_to_temp[ots->reg] = -1;
1743e8996ee0Sbellard         ots->val_type = TEMP_VAL_CONST;
1744e8996ee0Sbellard         ots->val = val;
1745e8996ee0Sbellard     }
1746ec7a869dSAurelien Jarno     if (NEED_SYNC_ARG(0)) {
1747ec7a869dSAurelien Jarno         temp_sync(s, args[0], s->reserved_regs);
1748ec7a869dSAurelien Jarno     }
17494c4e1ab2SAurelien Jarno     if (IS_DEAD_ARG(0)) {
17504c4e1ab2SAurelien Jarno         temp_dead(s, args[0]);
17514c4e1ab2SAurelien Jarno     }
1752e8996ee0Sbellard }
1753e8996ee0Sbellard 
1754c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1755ec7a869dSAurelien Jarno                               const TCGArg *args, uint16_t dead_args,
1756ec7a869dSAurelien Jarno                               uint8_t sync_args)
1757c896fe29Sbellard {
1758c29c1d7eSAurelien Jarno     TCGRegSet allocated_regs;
1759c896fe29Sbellard     TCGTemp *ts, *ots;
1760c29c1d7eSAurelien Jarno     const TCGArgConstraint *arg_ct, *oarg_ct;
1761c896fe29Sbellard 
1762c29c1d7eSAurelien Jarno     tcg_regset_set(allocated_regs, s->reserved_regs);
1763c896fe29Sbellard     ots = &s->temps[args[0]];
1764c896fe29Sbellard     ts = &s->temps[args[1]];
1765c29c1d7eSAurelien Jarno     oarg_ct = &def->args_ct[0];
1766c29c1d7eSAurelien Jarno     arg_ct = &def->args_ct[1];
1767c896fe29Sbellard 
1768c29c1d7eSAurelien Jarno     /* If the source value is not in a register, and we're going to be
1769c29c1d7eSAurelien Jarno        forced to have it in a register in order to perform the copy,
1770c29c1d7eSAurelien Jarno        then copy the SOURCE value into its own register first.  That way
1771c29c1d7eSAurelien Jarno        we don't have to reload SOURCE the next time it is used. */
1772c29c1d7eSAurelien Jarno     if (((NEED_SYNC_ARG(0) || ots->fixed_reg) && ts->val_type != TEMP_VAL_REG)
1773c29c1d7eSAurelien Jarno         || ts->val_type == TEMP_VAL_MEM) {
1774c29c1d7eSAurelien Jarno         ts->reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1775c29c1d7eSAurelien Jarno         if (ts->val_type == TEMP_VAL_MEM) {
1776c29c1d7eSAurelien Jarno             tcg_out_ld(s, ts->type, ts->reg, ts->mem_reg, ts->mem_offset);
1777c29c1d7eSAurelien Jarno             ts->mem_coherent = 1;
1778c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1779c29c1d7eSAurelien Jarno             tcg_out_movi(s, ts->type, ts->reg, ts->val);
1780c29c1d7eSAurelien Jarno         }
1781c29c1d7eSAurelien Jarno         s->reg_to_temp[ts->reg] = args[1];
1782c29c1d7eSAurelien Jarno         ts->val_type = TEMP_VAL_REG;
1783c29c1d7eSAurelien Jarno     }
1784c29c1d7eSAurelien Jarno 
1785c29c1d7eSAurelien Jarno     if (IS_DEAD_ARG(0) && !ots->fixed_reg) {
1786c29c1d7eSAurelien Jarno         /* mov to a non-saved dead register makes no sense (even with
1787c29c1d7eSAurelien Jarno            liveness analysis disabled). */
1788c29c1d7eSAurelien Jarno         assert(NEED_SYNC_ARG(0));
1789c29c1d7eSAurelien Jarno         /* The code above should have moved the temp to a register. */
1790c29c1d7eSAurelien Jarno         assert(ts->val_type == TEMP_VAL_REG);
1791c29c1d7eSAurelien Jarno         if (!ots->mem_allocated) {
1792c29c1d7eSAurelien Jarno             temp_allocate_frame(s, args[0]);
1793c29c1d7eSAurelien Jarno         }
1794c29c1d7eSAurelien Jarno         tcg_out_st(s, ots->type, ts->reg, ots->mem_reg, ots->mem_offset);
1795c29c1d7eSAurelien Jarno         if (IS_DEAD_ARG(1)) {
1796c29c1d7eSAurelien Jarno             temp_dead(s, args[1]);
1797c29c1d7eSAurelien Jarno         }
1798c29c1d7eSAurelien Jarno         temp_dead(s, args[0]);
1799c29c1d7eSAurelien Jarno     } else if (ts->val_type == TEMP_VAL_CONST) {
1800e8996ee0Sbellard         /* propagate constant */
1801c29c1d7eSAurelien Jarno         if (ots->val_type == TEMP_VAL_REG) {
1802e8996ee0Sbellard             s->reg_to_temp[ots->reg] = -1;
1803c29c1d7eSAurelien Jarno         }
1804e8996ee0Sbellard         ots->val_type = TEMP_VAL_CONST;
1805e8996ee0Sbellard         ots->val = ts->val;
1806e8996ee0Sbellard     } else {
1807c29c1d7eSAurelien Jarno         /* The code in the first if block should have moved the
1808c29c1d7eSAurelien Jarno            temp to a register. */
1809c29c1d7eSAurelien Jarno         assert(ts->val_type == TEMP_VAL_REG);
1810c29c1d7eSAurelien Jarno         if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
1811c29c1d7eSAurelien Jarno             /* the mov can be suppressed */
1812c29c1d7eSAurelien Jarno             if (ots->val_type == TEMP_VAL_REG) {
1813c29c1d7eSAurelien Jarno                 s->reg_to_temp[ots->reg] = -1;
1814c896fe29Sbellard             }
1815c29c1d7eSAurelien Jarno             ots->reg = ts->reg;
1816c29c1d7eSAurelien Jarno             temp_dead(s, args[1]);
1817c29c1d7eSAurelien Jarno         } else {
1818c29c1d7eSAurelien Jarno             if (ots->val_type != TEMP_VAL_REG) {
1819c29c1d7eSAurelien Jarno                 /* When allocating a new register, make sure to not spill the
1820c29c1d7eSAurelien Jarno                    input one. */
1821c29c1d7eSAurelien Jarno                 tcg_regset_set_reg(allocated_regs, ts->reg);
1822c29c1d7eSAurelien Jarno                 ots->reg = tcg_reg_alloc(s, oarg_ct->u.regs, allocated_regs);
1823c29c1d7eSAurelien Jarno             }
1824c29c1d7eSAurelien Jarno             tcg_out_mov(s, ots->type, ots->reg, ts->reg);
1825c29c1d7eSAurelien Jarno         }
1826c896fe29Sbellard         ots->val_type = TEMP_VAL_REG;
1827c896fe29Sbellard         ots->mem_coherent = 0;
1828c29c1d7eSAurelien Jarno         s->reg_to_temp[ots->reg] = args[0];
1829ec7a869dSAurelien Jarno         if (NEED_SYNC_ARG(0)) {
1830c29c1d7eSAurelien Jarno             tcg_reg_sync(s, ots->reg);
1831c29c1d7eSAurelien Jarno         }
1832ec7a869dSAurelien Jarno     }
1833c896fe29Sbellard }
1834c896fe29Sbellard 
1835c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s,
1836a9751609SRichard Henderson                              const TCGOpDef *def, TCGOpcode opc,
1837ec7a869dSAurelien Jarno                              const TCGArg *args, uint16_t dead_args,
1838ec7a869dSAurelien Jarno                              uint8_t sync_args)
1839c896fe29Sbellard {
1840c896fe29Sbellard     TCGRegSet allocated_regs;
1841c896fe29Sbellard     int i, k, nb_iargs, nb_oargs, reg;
1842c896fe29Sbellard     TCGArg arg;
1843c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1844c896fe29Sbellard     TCGTemp *ts;
1845c896fe29Sbellard     TCGArg new_args[TCG_MAX_OP_ARGS];
1846c896fe29Sbellard     int const_args[TCG_MAX_OP_ARGS];
1847c896fe29Sbellard 
1848c896fe29Sbellard     nb_oargs = def->nb_oargs;
1849c896fe29Sbellard     nb_iargs = def->nb_iargs;
1850c896fe29Sbellard 
1851c896fe29Sbellard     /* copy constants */
1852c896fe29Sbellard     memcpy(new_args + nb_oargs + nb_iargs,
1853c896fe29Sbellard            args + nb_oargs + nb_iargs,
1854c896fe29Sbellard            sizeof(TCGArg) * def->nb_cargs);
1855c896fe29Sbellard 
1856c896fe29Sbellard     /* satisfy input constraints */
1857c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
1858c896fe29Sbellard     for(k = 0; k < nb_iargs; k++) {
1859c896fe29Sbellard         i = def->sorted_args[nb_oargs + k];
1860c896fe29Sbellard         arg = args[i];
1861c896fe29Sbellard         arg_ct = &def->args_ct[i];
1862c896fe29Sbellard         ts = &s->temps[arg];
1863c896fe29Sbellard         if (ts->val_type == TEMP_VAL_MEM) {
1864c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1865e4d5434cSblueswir1             tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1866c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
1867c896fe29Sbellard             ts->reg = reg;
1868c896fe29Sbellard             ts->mem_coherent = 1;
1869c896fe29Sbellard             s->reg_to_temp[reg] = arg;
1870c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1871c896fe29Sbellard             if (tcg_target_const_match(ts->val, arg_ct)) {
1872c896fe29Sbellard                 /* constant is OK for instruction */
1873c896fe29Sbellard                 const_args[i] = 1;
1874c896fe29Sbellard                 new_args[i] = ts->val;
1875c896fe29Sbellard                 goto iarg_end;
1876c896fe29Sbellard             } else {
1877c896fe29Sbellard                 /* need to move to a register */
1878c896fe29Sbellard                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1879c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
1880e8996ee0Sbellard                 ts->val_type = TEMP_VAL_REG;
1881e8996ee0Sbellard                 ts->reg = reg;
1882e8996ee0Sbellard                 ts->mem_coherent = 0;
1883e8996ee0Sbellard                 s->reg_to_temp[reg] = arg;
1884c896fe29Sbellard             }
1885c896fe29Sbellard         }
1886c896fe29Sbellard         assert(ts->val_type == TEMP_VAL_REG);
18875ff9d6a4Sbellard         if (arg_ct->ct & TCG_CT_IALIAS) {
18885ff9d6a4Sbellard             if (ts->fixed_reg) {
18895ff9d6a4Sbellard                 /* if fixed register, we must allocate a new register
18905ff9d6a4Sbellard                    if the alias is not the same register */
18915ff9d6a4Sbellard                 if (arg != args[arg_ct->alias_index])
18925ff9d6a4Sbellard                     goto allocate_in_reg;
18935ff9d6a4Sbellard             } else {
1894c896fe29Sbellard                 /* if the input is aliased to an output and if it is
1895c896fe29Sbellard                    not dead after the instruction, we must allocate
1896c896fe29Sbellard                    a new register and move it */
1897866cb6cbSAurelien Jarno                 if (!IS_DEAD_ARG(i)) {
1898c896fe29Sbellard                     goto allocate_in_reg;
1899c896fe29Sbellard                 }
19005ff9d6a4Sbellard             }
1901866cb6cbSAurelien Jarno         }
1902c896fe29Sbellard         reg = ts->reg;
1903c896fe29Sbellard         if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1904c896fe29Sbellard             /* nothing to do : the constraint is satisfied */
1905c896fe29Sbellard         } else {
1906c896fe29Sbellard         allocate_in_reg:
1907c896fe29Sbellard             /* allocate a new register matching the constraint
1908c896fe29Sbellard                and move the temporary register into it */
1909c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
19103b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, reg, ts->reg);
1911c896fe29Sbellard         }
1912c896fe29Sbellard         new_args[i] = reg;
1913c896fe29Sbellard         const_args[i] = 0;
1914c896fe29Sbellard         tcg_regset_set_reg(allocated_regs, reg);
1915c896fe29Sbellard     iarg_end: ;
1916c896fe29Sbellard     }
1917c896fe29Sbellard 
1918c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
1919866cb6cbSAurelien Jarno     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1920866cb6cbSAurelien Jarno         if (IS_DEAD_ARG(i)) {
1921639368ddSAurelien Jarno             temp_dead(s, args[i]);
1922c896fe29Sbellard         }
1923c896fe29Sbellard     }
1924c896fe29Sbellard 
1925a52ad07eSAurelien Jarno     if (def->flags & TCG_OPF_BB_END) {
1926a52ad07eSAurelien Jarno         tcg_reg_alloc_bb_end(s, allocated_regs);
1927a52ad07eSAurelien Jarno     } else {
1928c896fe29Sbellard         if (def->flags & TCG_OPF_CALL_CLOBBER) {
1929b03cce8eSbellard             /* XXX: permit generic clobber register list ? */
1930c896fe29Sbellard             for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1931c896fe29Sbellard                 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1932c896fe29Sbellard                     tcg_reg_free(s, reg);
1933c896fe29Sbellard                 }
1934c896fe29Sbellard             }
19353d5c5f87SAurelien Jarno         }
19363d5c5f87SAurelien Jarno         if (def->flags & TCG_OPF_SIDE_EFFECTS) {
19373d5c5f87SAurelien Jarno             /* sync globals if the op has side effects and might trigger
19383d5c5f87SAurelien Jarno                an exception. */
19393d5c5f87SAurelien Jarno             sync_globals(s, allocated_regs);
1940c896fe29Sbellard         }
1941c896fe29Sbellard 
1942c896fe29Sbellard         /* satisfy the output constraints */
1943c896fe29Sbellard         tcg_regset_set(allocated_regs, s->reserved_regs);
1944c896fe29Sbellard         for(k = 0; k < nb_oargs; k++) {
1945c896fe29Sbellard             i = def->sorted_args[k];
1946c896fe29Sbellard             arg = args[i];
1947c896fe29Sbellard             arg_ct = &def->args_ct[i];
1948c896fe29Sbellard             ts = &s->temps[arg];
1949c896fe29Sbellard             if (arg_ct->ct & TCG_CT_ALIAS) {
19505ff9d6a4Sbellard                 reg = new_args[arg_ct->alias_index];
1951c896fe29Sbellard             } else {
1952c896fe29Sbellard                 /* if fixed register, we try to use it */
1953c896fe29Sbellard                 reg = ts->reg;
1954c896fe29Sbellard                 if (ts->fixed_reg &&
1955c896fe29Sbellard                     tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1956c896fe29Sbellard                     goto oarg_end;
1957c896fe29Sbellard                 }
1958c896fe29Sbellard                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1959c896fe29Sbellard             }
1960c896fe29Sbellard             tcg_regset_set_reg(allocated_regs, reg);
1961c896fe29Sbellard             /* if a fixed register is used, then a move will be done afterwards */
1962c896fe29Sbellard             if (!ts->fixed_reg) {
1963639368ddSAurelien Jarno                 if (ts->val_type == TEMP_VAL_REG) {
1964639368ddSAurelien Jarno                     s->reg_to_temp[ts->reg] = -1;
1965639368ddSAurelien Jarno                 }
1966c896fe29Sbellard                 ts->val_type = TEMP_VAL_REG;
1967c896fe29Sbellard                 ts->reg = reg;
1968c896fe29Sbellard                 /* temp value is modified, so the value kept in memory is
1969c896fe29Sbellard                    potentially not the same */
1970c896fe29Sbellard                 ts->mem_coherent = 0;
1971c896fe29Sbellard                 s->reg_to_temp[reg] = arg;
1972c896fe29Sbellard             }
1973c896fe29Sbellard         oarg_end:
1974c896fe29Sbellard             new_args[i] = reg;
1975c896fe29Sbellard         }
1976e8996ee0Sbellard     }
1977c896fe29Sbellard 
1978c896fe29Sbellard     /* emit instruction */
1979c896fe29Sbellard     tcg_out_op(s, opc, new_args, const_args);
1980c896fe29Sbellard 
1981c896fe29Sbellard     /* move the outputs in the correct register if needed */
1982c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
1983c896fe29Sbellard         ts = &s->temps[args[i]];
1984c896fe29Sbellard         reg = new_args[i];
1985c896fe29Sbellard         if (ts->fixed_reg && ts->reg != reg) {
19863b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, ts->reg, reg);
1987c896fe29Sbellard         }
1988ec7a869dSAurelien Jarno         if (NEED_SYNC_ARG(i)) {
1989ec7a869dSAurelien Jarno             tcg_reg_sync(s, reg);
1990ec7a869dSAurelien Jarno         }
1991ec7a869dSAurelien Jarno         if (IS_DEAD_ARG(i)) {
1992ec7a869dSAurelien Jarno             temp_dead(s, args[i]);
1993ec7a869dSAurelien Jarno         }
1994c896fe29Sbellard     }
1995c896fe29Sbellard }
1996c896fe29Sbellard 
1997b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP
1998b03cce8eSbellard #define STACK_DIR(x) (-(x))
1999b03cce8eSbellard #else
2000b03cce8eSbellard #define STACK_DIR(x) (x)
2001b03cce8eSbellard #endif
2002b03cce8eSbellard 
2003c896fe29Sbellard static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def,
2004a9751609SRichard Henderson                               TCGOpcode opc, const TCGArg *args,
2005ec7a869dSAurelien Jarno                               uint16_t dead_args, uint8_t sync_args)
2006c896fe29Sbellard {
2007c896fe29Sbellard     int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params;
2008c896fe29Sbellard     TCGArg arg, func_arg;
2009c896fe29Sbellard     TCGTemp *ts;
2010f54b3f92Saurel32     tcg_target_long stack_offset, call_stack_size, func_addr;
2011b03cce8eSbellard     int const_func_arg, allocate_args;
2012c896fe29Sbellard     TCGRegSet allocated_regs;
2013c896fe29Sbellard     const TCGArgConstraint *arg_ct;
2014c896fe29Sbellard 
2015c896fe29Sbellard     arg = *args++;
2016c896fe29Sbellard 
2017c896fe29Sbellard     nb_oargs = arg >> 16;
2018c896fe29Sbellard     nb_iargs = arg & 0xffff;
2019c896fe29Sbellard     nb_params = nb_iargs - 1;
2020c896fe29Sbellard 
2021c896fe29Sbellard     flags = args[nb_oargs + nb_iargs];
2022c896fe29Sbellard 
20236e17d0c5SStefan Weil     nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2024c896fe29Sbellard     if (nb_regs > nb_params)
2025c896fe29Sbellard         nb_regs = nb_params;
2026c896fe29Sbellard 
2027c896fe29Sbellard     /* assign stack slots first */
2028c896fe29Sbellard     call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long);
2029c896fe29Sbellard     call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
2030c896fe29Sbellard         ~(TCG_TARGET_STACK_ALIGN - 1);
2031b03cce8eSbellard     allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
2032b03cce8eSbellard     if (allocate_args) {
2033345649c0SBlue Swirl         /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
2034345649c0SBlue Swirl            preallocate call stack */
2035345649c0SBlue Swirl         tcg_abort();
2036b03cce8eSbellard     }
203739cf05d3Sbellard 
203839cf05d3Sbellard     stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
2039c896fe29Sbellard     for(i = nb_regs; i < nb_params; i++) {
2040c896fe29Sbellard         arg = args[nb_oargs + i];
204139cf05d3Sbellard #ifdef TCG_TARGET_STACK_GROWSUP
204239cf05d3Sbellard         stack_offset -= sizeof(tcg_target_long);
204339cf05d3Sbellard #endif
204439cf05d3Sbellard         if (arg != TCG_CALL_DUMMY_ARG) {
2045c896fe29Sbellard             ts = &s->temps[arg];
2046c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
2047e4d5434cSblueswir1                 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
2048c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_MEM) {
2049c896fe29Sbellard                 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
2050c896fe29Sbellard                                     s->reserved_regs);
2051c896fe29Sbellard                 /* XXX: not correct if reading values from the stack */
2052e4d5434cSblueswir1                 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2053e4d5434cSblueswir1                 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
2054c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_CONST) {
2055c896fe29Sbellard                 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
2056c896fe29Sbellard                                     s->reserved_regs);
2057c896fe29Sbellard                 /* XXX: sign extend may be needed on some targets */
2058c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
2059e4d5434cSblueswir1                 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
2060c896fe29Sbellard             } else {
2061c896fe29Sbellard                 tcg_abort();
2062c896fe29Sbellard             }
206339cf05d3Sbellard         }
206439cf05d3Sbellard #ifndef TCG_TARGET_STACK_GROWSUP
206539cf05d3Sbellard         stack_offset += sizeof(tcg_target_long);
206639cf05d3Sbellard #endif
2067c896fe29Sbellard     }
2068c896fe29Sbellard 
2069c896fe29Sbellard     /* assign input registers */
2070c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
2071c896fe29Sbellard     for(i = 0; i < nb_regs; i++) {
2072c896fe29Sbellard         arg = args[nb_oargs + i];
207339cf05d3Sbellard         if (arg != TCG_CALL_DUMMY_ARG) {
2074c896fe29Sbellard             ts = &s->temps[arg];
2075c896fe29Sbellard             reg = tcg_target_call_iarg_regs[i];
2076c896fe29Sbellard             tcg_reg_free(s, reg);
2077c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
2078c896fe29Sbellard                 if (ts->reg != reg) {
20793b6dac34SRichard Henderson                     tcg_out_mov(s, ts->type, reg, ts->reg);
2080c896fe29Sbellard                 }
2081c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_MEM) {
2082e4d5434cSblueswir1                 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2083c896fe29Sbellard             } else if (ts->val_type == TEMP_VAL_CONST) {
2084c896fe29Sbellard                 /* XXX: sign extend ? */
2085c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
2086c896fe29Sbellard             } else {
2087c896fe29Sbellard                 tcg_abort();
2088c896fe29Sbellard             }
2089c896fe29Sbellard             tcg_regset_set_reg(allocated_regs, reg);
2090c896fe29Sbellard         }
209139cf05d3Sbellard     }
2092c896fe29Sbellard 
2093c896fe29Sbellard     /* assign function address */
2094c896fe29Sbellard     func_arg = args[nb_oargs + nb_iargs - 1];
2095c896fe29Sbellard     arg_ct = &def->args_ct[0];
2096c896fe29Sbellard     ts = &s->temps[func_arg];
2097f54b3f92Saurel32     func_addr = ts->val;
2098c896fe29Sbellard     const_func_arg = 0;
2099c896fe29Sbellard     if (ts->val_type == TEMP_VAL_MEM) {
2100c896fe29Sbellard         reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2101e4d5434cSblueswir1         tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2102c896fe29Sbellard         func_arg = reg;
2103e8996ee0Sbellard         tcg_regset_set_reg(allocated_regs, reg);
2104c896fe29Sbellard     } else if (ts->val_type == TEMP_VAL_REG) {
2105c896fe29Sbellard         reg = ts->reg;
2106c896fe29Sbellard         if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) {
2107c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
21083b6dac34SRichard Henderson             tcg_out_mov(s, ts->type, reg, ts->reg);
2109c896fe29Sbellard         }
2110c896fe29Sbellard         func_arg = reg;
2111e8996ee0Sbellard         tcg_regset_set_reg(allocated_regs, reg);
2112c896fe29Sbellard     } else if (ts->val_type == TEMP_VAL_CONST) {
2113f54b3f92Saurel32         if (tcg_target_const_match(func_addr, arg_ct)) {
2114c896fe29Sbellard             const_func_arg = 1;
2115f54b3f92Saurel32             func_arg = func_addr;
2116c896fe29Sbellard         } else {
2117c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2118f54b3f92Saurel32             tcg_out_movi(s, ts->type, reg, func_addr);
2119c896fe29Sbellard             func_arg = reg;
2120e8996ee0Sbellard             tcg_regset_set_reg(allocated_regs, reg);
2121c896fe29Sbellard         }
2122c896fe29Sbellard     } else {
2123c896fe29Sbellard         tcg_abort();
2124c896fe29Sbellard     }
2125c896fe29Sbellard 
2126e8996ee0Sbellard 
2127c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
2128866cb6cbSAurelien Jarno     for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2129866cb6cbSAurelien Jarno         if (IS_DEAD_ARG(i)) {
2130639368ddSAurelien Jarno             temp_dead(s, args[i]);
2131c896fe29Sbellard         }
2132c896fe29Sbellard     }
2133c896fe29Sbellard 
2134c896fe29Sbellard     /* clobber call registers */
2135c896fe29Sbellard     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2136c896fe29Sbellard         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
2137c896fe29Sbellard             tcg_reg_free(s, reg);
2138c896fe29Sbellard         }
2139c896fe29Sbellard     }
2140c896fe29Sbellard 
214178505279SAurelien Jarno     /* Save globals if they might be written by the helper, sync them if
214278505279SAurelien Jarno        they might be read. */
214378505279SAurelien Jarno     if (flags & TCG_CALL_NO_READ_GLOBALS) {
214478505279SAurelien Jarno         /* Nothing to do */
214578505279SAurelien Jarno     } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
214678505279SAurelien Jarno         sync_globals(s, allocated_regs);
214778505279SAurelien Jarno     } else {
2148e8996ee0Sbellard         save_globals(s, allocated_regs);
2149b9c18f56Saurel32     }
2150c896fe29Sbellard 
2151c896fe29Sbellard     tcg_out_op(s, opc, &func_arg, &const_func_arg);
2152c896fe29Sbellard 
2153c896fe29Sbellard     /* assign output registers and emit moves if needed */
2154c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
2155c896fe29Sbellard         arg = args[i];
2156c896fe29Sbellard         ts = &s->temps[arg];
2157c896fe29Sbellard         reg = tcg_target_call_oarg_regs[i];
2158e8996ee0Sbellard         assert(s->reg_to_temp[reg] == -1);
2159c896fe29Sbellard         if (ts->fixed_reg) {
2160c896fe29Sbellard             if (ts->reg != reg) {
21613b6dac34SRichard Henderson                 tcg_out_mov(s, ts->type, ts->reg, reg);
2162c896fe29Sbellard             }
2163c896fe29Sbellard         } else {
2164639368ddSAurelien Jarno             if (ts->val_type == TEMP_VAL_REG) {
2165639368ddSAurelien Jarno                 s->reg_to_temp[ts->reg] = -1;
2166639368ddSAurelien Jarno             }
2167c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
2168c896fe29Sbellard             ts->reg = reg;
2169c896fe29Sbellard             ts->mem_coherent = 0;
2170c896fe29Sbellard             s->reg_to_temp[reg] = arg;
2171ec7a869dSAurelien Jarno             if (NEED_SYNC_ARG(i)) {
2172ec7a869dSAurelien Jarno                 tcg_reg_sync(s, reg);
2173ec7a869dSAurelien Jarno             }
2174ec7a869dSAurelien Jarno             if (IS_DEAD_ARG(i)) {
2175ec7a869dSAurelien Jarno                 temp_dead(s, args[i]);
2176c896fe29Sbellard             }
2177c896fe29Sbellard         }
21788c11ad25SAurelien Jarno     }
2179c896fe29Sbellard 
2180c896fe29Sbellard     return nb_iargs + nb_oargs + def->nb_cargs + 1;
2181c896fe29Sbellard }
2182c896fe29Sbellard 
2183c896fe29Sbellard #ifdef CONFIG_PROFILER
2184c896fe29Sbellard 
218554604f74Saurel32 static int64_t tcg_table_op_count[NB_OPS];
2186c896fe29Sbellard 
2187871e6c35SBlue Swirl static void dump_op_count(void)
2188c896fe29Sbellard {
2189c896fe29Sbellard     int i;
2190c896fe29Sbellard     FILE *f;
219154604f74Saurel32     f = fopen("/tmp/op.log", "w");
2192c896fe29Sbellard     for(i = INDEX_op_end; i < NB_OPS; i++) {
219354604f74Saurel32         fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, tcg_table_op_count[i]);
2194c896fe29Sbellard     }
2195c896fe29Sbellard     fclose(f);
2196c896fe29Sbellard }
2197c896fe29Sbellard #endif
2198c896fe29Sbellard 
2199c896fe29Sbellard 
2200c896fe29Sbellard static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
22012ba1eeb6Spbrook                                       long search_pc)
2202c896fe29Sbellard {
2203a9751609SRichard Henderson     TCGOpcode opc;
2204a9751609SRichard Henderson     int op_index;
2205c896fe29Sbellard     const TCGOpDef *def;
2206c896fe29Sbellard     const TCGArg *args;
2207c896fe29Sbellard 
2208c896fe29Sbellard #ifdef DEBUG_DISAS
22098fec2b8cSaliguori     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
221093fcfe39Saliguori         qemu_log("OP:\n");
2211eeacee4dSBlue Swirl         tcg_dump_ops(s);
221293fcfe39Saliguori         qemu_log("\n");
2213c896fe29Sbellard     }
2214c896fe29Sbellard #endif
2215c896fe29Sbellard 
2216c5cc28ffSAurelien Jarno #ifdef CONFIG_PROFILER
2217c5cc28ffSAurelien Jarno     s->opt_time -= profile_getclock();
2218c5cc28ffSAurelien Jarno #endif
2219c5cc28ffSAurelien Jarno 
22208f2e8c07SKirill Batuzov #ifdef USE_TCG_OPTIMIZATIONS
2221c4afe5c4SEvgeny Voevodin     s->gen_opparam_ptr =
22221ff0a2c5SEvgeny Voevodin         tcg_optimize(s, s->gen_opc_ptr, s->gen_opparam_buf, tcg_op_defs);
22238f2e8c07SKirill Batuzov #endif
22248f2e8c07SKirill Batuzov 
2225a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2226c5cc28ffSAurelien Jarno     s->opt_time += profile_getclock();
2227a23a9ec6Sbellard     s->la_time -= profile_getclock();
2228a23a9ec6Sbellard #endif
2229c5cc28ffSAurelien Jarno 
2230c896fe29Sbellard     tcg_liveness_analysis(s);
2231c5cc28ffSAurelien Jarno 
2232a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2233a23a9ec6Sbellard     s->la_time += profile_getclock();
2234a23a9ec6Sbellard #endif
2235c896fe29Sbellard 
2236c896fe29Sbellard #ifdef DEBUG_DISAS
22378fec2b8cSaliguori     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) {
2238c5cc28ffSAurelien Jarno         qemu_log("OP after optimization and liveness analysis:\n");
2239eeacee4dSBlue Swirl         tcg_dump_ops(s);
224093fcfe39Saliguori         qemu_log("\n");
2241c896fe29Sbellard     }
2242c896fe29Sbellard #endif
2243c896fe29Sbellard 
2244c896fe29Sbellard     tcg_reg_alloc_start(s);
2245c896fe29Sbellard 
2246c896fe29Sbellard     s->code_buf = gen_code_buf;
2247c896fe29Sbellard     s->code_ptr = gen_code_buf;
2248c896fe29Sbellard 
22491ff0a2c5SEvgeny Voevodin     args = s->gen_opparam_buf;
2250c896fe29Sbellard     op_index = 0;
2251b3db8758Sblueswir1 
2252c896fe29Sbellard     for(;;) {
225392414b31SEvgeny Voevodin         opc = s->gen_opc_buf[op_index];
2254c896fe29Sbellard #ifdef CONFIG_PROFILER
225554604f74Saurel32         tcg_table_op_count[opc]++;
2256c896fe29Sbellard #endif
2257c896fe29Sbellard         def = &tcg_op_defs[opc];
2258c896fe29Sbellard #if 0
2259c896fe29Sbellard         printf("%s: %d %d %d\n", def->name,
2260c896fe29Sbellard                def->nb_oargs, def->nb_iargs, def->nb_cargs);
2261c896fe29Sbellard         //        dump_regs(s);
2262c896fe29Sbellard #endif
2263c896fe29Sbellard         switch(opc) {
2264c896fe29Sbellard         case INDEX_op_mov_i32:
2265c896fe29Sbellard         case INDEX_op_mov_i64:
2266ec7a869dSAurelien Jarno             tcg_reg_alloc_mov(s, def, args, s->op_dead_args[op_index],
2267ec7a869dSAurelien Jarno                               s->op_sync_args[op_index]);
2268c896fe29Sbellard             break;
2269e8996ee0Sbellard         case INDEX_op_movi_i32:
2270e8996ee0Sbellard         case INDEX_op_movi_i64:
2271ec7a869dSAurelien Jarno             tcg_reg_alloc_movi(s, args, s->op_dead_args[op_index],
2272ec7a869dSAurelien Jarno                                s->op_sync_args[op_index]);
2273e8996ee0Sbellard             break;
22747e4597d7Sbellard         case INDEX_op_debug_insn_start:
22757e4597d7Sbellard             /* debug instruction */
22767e4597d7Sbellard             break;
2277c896fe29Sbellard         case INDEX_op_nop:
2278c896fe29Sbellard         case INDEX_op_nop1:
2279c896fe29Sbellard         case INDEX_op_nop2:
2280c896fe29Sbellard         case INDEX_op_nop3:
2281c896fe29Sbellard             break;
2282c896fe29Sbellard         case INDEX_op_nopn:
2283c896fe29Sbellard             args += args[0];
2284c896fe29Sbellard             goto next;
22855ff9d6a4Sbellard         case INDEX_op_discard:
2286639368ddSAurelien Jarno             temp_dead(s, args[0]);
22875ff9d6a4Sbellard             break;
2288c896fe29Sbellard         case INDEX_op_set_label:
2289e8996ee0Sbellard             tcg_reg_alloc_bb_end(s, s->reserved_regs);
22909d6fca70SStefan Weil             tcg_out_label(s, args[0], s->code_ptr);
2291c896fe29Sbellard             break;
2292c896fe29Sbellard         case INDEX_op_call:
2293ec7a869dSAurelien Jarno             args += tcg_reg_alloc_call(s, def, opc, args,
2294ec7a869dSAurelien Jarno                                        s->op_dead_args[op_index],
2295ec7a869dSAurelien Jarno                                        s->op_sync_args[op_index]);
2296c896fe29Sbellard             goto next;
2297c896fe29Sbellard         case INDEX_op_end:
2298c896fe29Sbellard             goto the_end;
2299c896fe29Sbellard         default:
230025c4d9ccSRichard Henderson             /* Sanity check that we've not introduced any unhandled opcodes. */
230125c4d9ccSRichard Henderson             if (def->flags & TCG_OPF_NOT_PRESENT) {
230225c4d9ccSRichard Henderson                 tcg_abort();
230325c4d9ccSRichard Henderson             }
2304c896fe29Sbellard             /* Note: in order to speed up the code, it would be much
2305c896fe29Sbellard                faster to have specialized register allocator functions for
2306c896fe29Sbellard                some common argument patterns */
2307ec7a869dSAurelien Jarno             tcg_reg_alloc_op(s, def, opc, args, s->op_dead_args[op_index],
2308ec7a869dSAurelien Jarno                              s->op_sync_args[op_index]);
2309c896fe29Sbellard             break;
2310c896fe29Sbellard         }
2311c896fe29Sbellard         args += def->nb_args;
23128df1ca4bSths     next:
23132ba1eeb6Spbrook         if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) {
2314c896fe29Sbellard             return op_index;
2315c896fe29Sbellard         }
2316c896fe29Sbellard         op_index++;
2317c896fe29Sbellard #ifndef NDEBUG
2318c896fe29Sbellard         check_regs(s);
2319c896fe29Sbellard #endif
2320c896fe29Sbellard     }
2321c896fe29Sbellard  the_end:
2322b76f0d8cSYeongkyoon Lee #if defined(CONFIG_QEMU_LDST_OPTIMIZATION) && defined(CONFIG_SOFTMMU)
2323b76f0d8cSYeongkyoon Lee     /* Generate TB finalization at the end of block */
2324b76f0d8cSYeongkyoon Lee     tcg_out_tb_finalize(s);
2325b76f0d8cSYeongkyoon Lee #endif
2326c896fe29Sbellard     return -1;
2327c896fe29Sbellard }
2328c896fe29Sbellard 
232954604f74Saurel32 int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf)
2330c896fe29Sbellard {
2331c896fe29Sbellard #ifdef CONFIG_PROFILER
2332c896fe29Sbellard     {
2333c896fe29Sbellard         int n;
233492414b31SEvgeny Voevodin         n = (s->gen_opc_ptr - s->gen_opc_buf);
2335a23a9ec6Sbellard         s->op_count += n;
2336a23a9ec6Sbellard         if (n > s->op_count_max)
2337a23a9ec6Sbellard             s->op_count_max = n;
2338a23a9ec6Sbellard 
2339a23a9ec6Sbellard         s->temp_count += s->nb_temps;
2340a23a9ec6Sbellard         if (s->nb_temps > s->temp_count_max)
2341a23a9ec6Sbellard             s->temp_count_max = s->nb_temps;
2342c896fe29Sbellard     }
2343c896fe29Sbellard #endif
2344c896fe29Sbellard 
23452ba1eeb6Spbrook     tcg_gen_code_common(s, gen_code_buf, -1);
2346c896fe29Sbellard 
2347c896fe29Sbellard     /* flush instruction cache */
23482aeabc08SStefan Weil     flush_icache_range((tcg_target_ulong)gen_code_buf,
23492aeabc08SStefan Weil                        (tcg_target_ulong)s->code_ptr);
23502aeabc08SStefan Weil 
2351c896fe29Sbellard     return s->code_ptr -  gen_code_buf;
2352c896fe29Sbellard }
2353c896fe29Sbellard 
23542ba1eeb6Spbrook /* Return the index of the micro operation such as the pc after is <
2355623e265cSpbrook    offset bytes from the start of the TB.  The contents of gen_code_buf must
2356623e265cSpbrook    not be changed, though writing the same values is ok.
2357623e265cSpbrook    Return -1 if not found. */
235854604f74Saurel32 int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset)
2359c896fe29Sbellard {
2360623e265cSpbrook     return tcg_gen_code_common(s, gen_code_buf, offset);
2361c896fe29Sbellard }
2362a23a9ec6Sbellard 
2363a23a9ec6Sbellard #ifdef CONFIG_PROFILER
2364405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2365a23a9ec6Sbellard {
2366a23a9ec6Sbellard     TCGContext *s = &tcg_ctx;
2367a23a9ec6Sbellard     int64_t tot;
2368a23a9ec6Sbellard 
2369a23a9ec6Sbellard     tot = s->interm_time + s->code_time;
2370a23a9ec6Sbellard     cpu_fprintf(f, "JIT cycles          %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2371a23a9ec6Sbellard                 tot, tot / 2.4e9);
2372a23a9ec6Sbellard     cpu_fprintf(f, "translated TBs      %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
2373a23a9ec6Sbellard                 s->tb_count,
2374a23a9ec6Sbellard                 s->tb_count1 - s->tb_count,
2375a23a9ec6Sbellard                 s->tb_count1 ? (double)(s->tb_count1 - s->tb_count) / s->tb_count1 * 100.0 : 0);
2376a23a9ec6Sbellard     cpu_fprintf(f, "avg ops/TB          %0.1f max=%d\n",
2377a23a9ec6Sbellard                 s->tb_count ? (double)s->op_count / s->tb_count : 0, s->op_count_max);
2378a23a9ec6Sbellard     cpu_fprintf(f, "deleted ops/TB      %0.2f\n",
2379a23a9ec6Sbellard                 s->tb_count ?
2380a23a9ec6Sbellard                 (double)s->del_op_count / s->tb_count : 0);
2381a23a9ec6Sbellard     cpu_fprintf(f, "avg temps/TB        %0.2f max=%d\n",
2382a23a9ec6Sbellard                 s->tb_count ?
2383a23a9ec6Sbellard                 (double)s->temp_count / s->tb_count : 0,
2384a23a9ec6Sbellard                 s->temp_count_max);
2385a23a9ec6Sbellard 
2386a23a9ec6Sbellard     cpu_fprintf(f, "cycles/op           %0.1f\n",
2387a23a9ec6Sbellard                 s->op_count ? (double)tot / s->op_count : 0);
2388a23a9ec6Sbellard     cpu_fprintf(f, "cycles/in byte      %0.1f\n",
2389a23a9ec6Sbellard                 s->code_in_len ? (double)tot / s->code_in_len : 0);
2390a23a9ec6Sbellard     cpu_fprintf(f, "cycles/out byte     %0.1f\n",
2391a23a9ec6Sbellard                 s->code_out_len ? (double)tot / s->code_out_len : 0);
2392a23a9ec6Sbellard     if (tot == 0)
2393a23a9ec6Sbellard         tot = 1;
2394a23a9ec6Sbellard     cpu_fprintf(f, "  gen_interm time   %0.1f%%\n",
2395a23a9ec6Sbellard                 (double)s->interm_time / tot * 100.0);
2396a23a9ec6Sbellard     cpu_fprintf(f, "  gen_code time     %0.1f%%\n",
2397a23a9ec6Sbellard                 (double)s->code_time / tot * 100.0);
2398c5cc28ffSAurelien Jarno     cpu_fprintf(f, "optim./code time    %0.1f%%\n",
2399c5cc28ffSAurelien Jarno                 (double)s->opt_time / (s->code_time ? s->code_time : 1)
2400c5cc28ffSAurelien Jarno                 * 100.0);
2401a23a9ec6Sbellard     cpu_fprintf(f, "liveness/code time  %0.1f%%\n",
2402a23a9ec6Sbellard                 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
2403a23a9ec6Sbellard     cpu_fprintf(f, "cpu_restore count   %" PRId64 "\n",
2404a23a9ec6Sbellard                 s->restore_count);
2405a23a9ec6Sbellard     cpu_fprintf(f, "  avg cycles        %0.1f\n",
2406a23a9ec6Sbellard                 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
2407871e6c35SBlue Swirl 
2408a23a9ec6Sbellard     dump_op_count();
2409a23a9ec6Sbellard }
2410a23a9ec6Sbellard #else
2411405cf9ffSStefan Weil void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2412a23a9ec6Sbellard {
241324bf7b3aSbellard     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2414a23a9ec6Sbellard }
2415a23a9ec6Sbellard #endif
2416813da627SRichard Henderson 
2417813da627SRichard Henderson #ifdef ELF_HOST_MACHINE
24185872bbf2SRichard Henderson /* In order to use this feature, the backend needs to do three things:
24195872bbf2SRichard Henderson 
24205872bbf2SRichard Henderson    (1) Define ELF_HOST_MACHINE to indicate both what value to
24215872bbf2SRichard Henderson        put into the ELF image and to indicate support for the feature.
24225872bbf2SRichard Henderson 
24235872bbf2SRichard Henderson    (2) Define tcg_register_jit.  This should create a buffer containing
24245872bbf2SRichard Henderson        the contents of a .debug_frame section that describes the post-
24255872bbf2SRichard Henderson        prologue unwind info for the tcg machine.
24265872bbf2SRichard Henderson 
24275872bbf2SRichard Henderson    (3) Call tcg_register_jit_int, with the constructed .debug_frame.
24285872bbf2SRichard Henderson */
2429813da627SRichard Henderson 
2430813da627SRichard Henderson /* Begin GDB interface.  THE FOLLOWING MUST MATCH GDB DOCS.  */
2431813da627SRichard Henderson typedef enum {
2432813da627SRichard Henderson     JIT_NOACTION = 0,
2433813da627SRichard Henderson     JIT_REGISTER_FN,
2434813da627SRichard Henderson     JIT_UNREGISTER_FN
2435813da627SRichard Henderson } jit_actions_t;
2436813da627SRichard Henderson 
2437813da627SRichard Henderson struct jit_code_entry {
2438813da627SRichard Henderson     struct jit_code_entry *next_entry;
2439813da627SRichard Henderson     struct jit_code_entry *prev_entry;
2440813da627SRichard Henderson     const void *symfile_addr;
2441813da627SRichard Henderson     uint64_t symfile_size;
2442813da627SRichard Henderson };
2443813da627SRichard Henderson 
2444813da627SRichard Henderson struct jit_descriptor {
2445813da627SRichard Henderson     uint32_t version;
2446813da627SRichard Henderson     uint32_t action_flag;
2447813da627SRichard Henderson     struct jit_code_entry *relevant_entry;
2448813da627SRichard Henderson     struct jit_code_entry *first_entry;
2449813da627SRichard Henderson };
2450813da627SRichard Henderson 
2451813da627SRichard Henderson void __jit_debug_register_code(void) __attribute__((noinline));
2452813da627SRichard Henderson void __jit_debug_register_code(void)
2453813da627SRichard Henderson {
2454813da627SRichard Henderson     asm("");
2455813da627SRichard Henderson }
2456813da627SRichard Henderson 
2457813da627SRichard Henderson /* Must statically initialize the version, because GDB may check
2458813da627SRichard Henderson    the version before we can set it.  */
2459813da627SRichard Henderson struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
2460813da627SRichard Henderson 
2461813da627SRichard Henderson /* End GDB interface.  */
2462813da627SRichard Henderson 
2463813da627SRichard Henderson static int find_string(const char *strtab, const char *str)
2464813da627SRichard Henderson {
2465813da627SRichard Henderson     const char *p = strtab + 1;
2466813da627SRichard Henderson 
2467813da627SRichard Henderson     while (1) {
2468813da627SRichard Henderson         if (strcmp(p, str) == 0) {
2469813da627SRichard Henderson             return p - strtab;
2470813da627SRichard Henderson         }
2471813da627SRichard Henderson         p += strlen(p) + 1;
2472813da627SRichard Henderson     }
2473813da627SRichard Henderson }
2474813da627SRichard Henderson 
24755872bbf2SRichard Henderson static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
2476813da627SRichard Henderson                                  void *debug_frame, size_t debug_frame_size)
2477813da627SRichard Henderson {
24785872bbf2SRichard Henderson     struct __attribute__((packed)) DebugInfo {
24795872bbf2SRichard Henderson         uint32_t  len;
24805872bbf2SRichard Henderson         uint16_t  version;
24815872bbf2SRichard Henderson         uint32_t  abbrev;
24825872bbf2SRichard Henderson         uint8_t   ptr_size;
24835872bbf2SRichard Henderson         uint8_t   cu_die;
24845872bbf2SRichard Henderson         uint16_t  cu_lang;
24855872bbf2SRichard Henderson         uintptr_t cu_low_pc;
24865872bbf2SRichard Henderson         uintptr_t cu_high_pc;
24875872bbf2SRichard Henderson         uint8_t   fn_die;
24885872bbf2SRichard Henderson         char      fn_name[16];
24895872bbf2SRichard Henderson         uintptr_t fn_low_pc;
24905872bbf2SRichard Henderson         uintptr_t fn_high_pc;
24915872bbf2SRichard Henderson         uint8_t   cu_eoc;
24925872bbf2SRichard Henderson     };
2493813da627SRichard Henderson 
2494813da627SRichard Henderson     struct ElfImage {
2495813da627SRichard Henderson         ElfW(Ehdr) ehdr;
2496813da627SRichard Henderson         ElfW(Phdr) phdr;
24975872bbf2SRichard Henderson         ElfW(Shdr) shdr[7];
24985872bbf2SRichard Henderson         ElfW(Sym)  sym[2];
24995872bbf2SRichard Henderson         struct DebugInfo di;
25005872bbf2SRichard Henderson         uint8_t    da[24];
25015872bbf2SRichard Henderson         char       str[80];
25025872bbf2SRichard Henderson     };
25035872bbf2SRichard Henderson 
25045872bbf2SRichard Henderson     struct ElfImage *img;
25055872bbf2SRichard Henderson 
25065872bbf2SRichard Henderson     static const struct ElfImage img_template = {
25075872bbf2SRichard Henderson         .ehdr = {
25085872bbf2SRichard Henderson             .e_ident[EI_MAG0] = ELFMAG0,
25095872bbf2SRichard Henderson             .e_ident[EI_MAG1] = ELFMAG1,
25105872bbf2SRichard Henderson             .e_ident[EI_MAG2] = ELFMAG2,
25115872bbf2SRichard Henderson             .e_ident[EI_MAG3] = ELFMAG3,
25125872bbf2SRichard Henderson             .e_ident[EI_CLASS] = ELF_CLASS,
25135872bbf2SRichard Henderson             .e_ident[EI_DATA] = ELF_DATA,
25145872bbf2SRichard Henderson             .e_ident[EI_VERSION] = EV_CURRENT,
25155872bbf2SRichard Henderson             .e_type = ET_EXEC,
25165872bbf2SRichard Henderson             .e_machine = ELF_HOST_MACHINE,
25175872bbf2SRichard Henderson             .e_version = EV_CURRENT,
25185872bbf2SRichard Henderson             .e_phoff = offsetof(struct ElfImage, phdr),
25195872bbf2SRichard Henderson             .e_shoff = offsetof(struct ElfImage, shdr),
25205872bbf2SRichard Henderson             .e_ehsize = sizeof(ElfW(Shdr)),
25215872bbf2SRichard Henderson             .e_phentsize = sizeof(ElfW(Phdr)),
25225872bbf2SRichard Henderson             .e_phnum = 1,
25235872bbf2SRichard Henderson             .e_shentsize = sizeof(ElfW(Shdr)),
25245872bbf2SRichard Henderson             .e_shnum = ARRAY_SIZE(img->shdr),
25255872bbf2SRichard Henderson             .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
2526abbb3eaeSRichard Henderson #ifdef ELF_HOST_FLAGS
2527abbb3eaeSRichard Henderson             .e_flags = ELF_HOST_FLAGS,
2528abbb3eaeSRichard Henderson #endif
2529abbb3eaeSRichard Henderson #ifdef ELF_OSABI
2530abbb3eaeSRichard Henderson             .e_ident[EI_OSABI] = ELF_OSABI,
2531abbb3eaeSRichard Henderson #endif
25325872bbf2SRichard Henderson         },
25335872bbf2SRichard Henderson         .phdr = {
25345872bbf2SRichard Henderson             .p_type = PT_LOAD,
25355872bbf2SRichard Henderson             .p_flags = PF_X,
25365872bbf2SRichard Henderson         },
25375872bbf2SRichard Henderson         .shdr = {
25385872bbf2SRichard Henderson             [0] = { .sh_type = SHT_NULL },
25395872bbf2SRichard Henderson             /* Trick: The contents of code_gen_buffer are not present in
25405872bbf2SRichard Henderson                this fake ELF file; that got allocated elsewhere.  Therefore
25415872bbf2SRichard Henderson                we mark .text as SHT_NOBITS (similar to .bss) so that readers
25425872bbf2SRichard Henderson                will not look for contents.  We can record any address.  */
25435872bbf2SRichard Henderson             [1] = { /* .text */
25445872bbf2SRichard Henderson                 .sh_type = SHT_NOBITS,
25455872bbf2SRichard Henderson                 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
25465872bbf2SRichard Henderson             },
25475872bbf2SRichard Henderson             [2] = { /* .debug_info */
25485872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
25495872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, di),
25505872bbf2SRichard Henderson                 .sh_size = sizeof(struct DebugInfo),
25515872bbf2SRichard Henderson             },
25525872bbf2SRichard Henderson             [3] = { /* .debug_abbrev */
25535872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
25545872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, da),
25555872bbf2SRichard Henderson                 .sh_size = sizeof(img->da),
25565872bbf2SRichard Henderson             },
25575872bbf2SRichard Henderson             [4] = { /* .debug_frame */
25585872bbf2SRichard Henderson                 .sh_type = SHT_PROGBITS,
25595872bbf2SRichard Henderson                 .sh_offset = sizeof(struct ElfImage),
25605872bbf2SRichard Henderson             },
25615872bbf2SRichard Henderson             [5] = { /* .symtab */
25625872bbf2SRichard Henderson                 .sh_type = SHT_SYMTAB,
25635872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, sym),
25645872bbf2SRichard Henderson                 .sh_size = sizeof(img->sym),
25655872bbf2SRichard Henderson                 .sh_info = 1,
25665872bbf2SRichard Henderson                 .sh_link = ARRAY_SIZE(img->shdr) - 1,
25675872bbf2SRichard Henderson                 .sh_entsize = sizeof(ElfW(Sym)),
25685872bbf2SRichard Henderson             },
25695872bbf2SRichard Henderson             [6] = { /* .strtab */
25705872bbf2SRichard Henderson                 .sh_type = SHT_STRTAB,
25715872bbf2SRichard Henderson                 .sh_offset = offsetof(struct ElfImage, str),
25725872bbf2SRichard Henderson                 .sh_size = sizeof(img->str),
25735872bbf2SRichard Henderson             }
25745872bbf2SRichard Henderson         },
25755872bbf2SRichard Henderson         .sym = {
25765872bbf2SRichard Henderson             [1] = { /* code_gen_buffer */
25775872bbf2SRichard Henderson                 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
25785872bbf2SRichard Henderson                 .st_shndx = 1,
25795872bbf2SRichard Henderson             }
25805872bbf2SRichard Henderson         },
25815872bbf2SRichard Henderson         .di = {
25825872bbf2SRichard Henderson             .len = sizeof(struct DebugInfo) - 4,
25835872bbf2SRichard Henderson             .version = 2,
25845872bbf2SRichard Henderson             .ptr_size = sizeof(void *),
25855872bbf2SRichard Henderson             .cu_die = 1,
25865872bbf2SRichard Henderson             .cu_lang = 0x8001,  /* DW_LANG_Mips_Assembler */
25875872bbf2SRichard Henderson             .fn_die = 2,
25885872bbf2SRichard Henderson             .fn_name = "code_gen_buffer"
25895872bbf2SRichard Henderson         },
25905872bbf2SRichard Henderson         .da = {
25915872bbf2SRichard Henderson             1,          /* abbrev number (the cu) */
25925872bbf2SRichard Henderson             0x11, 1,    /* DW_TAG_compile_unit, has children */
25935872bbf2SRichard Henderson             0x13, 0x5,  /* DW_AT_language, DW_FORM_data2 */
25945872bbf2SRichard Henderson             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
25955872bbf2SRichard Henderson             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
25965872bbf2SRichard Henderson             0, 0,       /* end of abbrev */
25975872bbf2SRichard Henderson             2,          /* abbrev number (the fn) */
25985872bbf2SRichard Henderson             0x2e, 0,    /* DW_TAG_subprogram, no children */
25995872bbf2SRichard Henderson             0x3, 0x8,   /* DW_AT_name, DW_FORM_string */
26005872bbf2SRichard Henderson             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
26015872bbf2SRichard Henderson             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
26025872bbf2SRichard Henderson             0, 0,       /* end of abbrev */
26035872bbf2SRichard Henderson             0           /* no more abbrev */
26045872bbf2SRichard Henderson         },
26055872bbf2SRichard Henderson         .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
26065872bbf2SRichard Henderson                ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
2607813da627SRichard Henderson     };
2608813da627SRichard Henderson 
2609813da627SRichard Henderson     /* We only need a single jit entry; statically allocate it.  */
2610813da627SRichard Henderson     static struct jit_code_entry one_entry;
2611813da627SRichard Henderson 
26125872bbf2SRichard Henderson     uintptr_t buf = (uintptr_t)buf_ptr;
2613813da627SRichard Henderson     size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
2614813da627SRichard Henderson 
26155872bbf2SRichard Henderson     img = g_malloc(img_size);
26165872bbf2SRichard Henderson     *img = img_template;
2617813da627SRichard Henderson     memcpy(img + 1, debug_frame, debug_frame_size);
2618813da627SRichard Henderson 
26195872bbf2SRichard Henderson     img->phdr.p_vaddr = buf;
26205872bbf2SRichard Henderson     img->phdr.p_paddr = buf;
26215872bbf2SRichard Henderson     img->phdr.p_memsz = buf_size;
2622813da627SRichard Henderson 
26235872bbf2SRichard Henderson     img->shdr[1].sh_name = find_string(img->str, ".text");
26245872bbf2SRichard Henderson     img->shdr[1].sh_addr = buf;
26255872bbf2SRichard Henderson     img->shdr[1].sh_size = buf_size;
2626813da627SRichard Henderson 
26275872bbf2SRichard Henderson     img->shdr[2].sh_name = find_string(img->str, ".debug_info");
26285872bbf2SRichard Henderson     img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
26295872bbf2SRichard Henderson 
26305872bbf2SRichard Henderson     img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
26315872bbf2SRichard Henderson     img->shdr[4].sh_size = debug_frame_size;
26325872bbf2SRichard Henderson 
26335872bbf2SRichard Henderson     img->shdr[5].sh_name = find_string(img->str, ".symtab");
26345872bbf2SRichard Henderson     img->shdr[6].sh_name = find_string(img->str, ".strtab");
26355872bbf2SRichard Henderson 
26365872bbf2SRichard Henderson     img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
26375872bbf2SRichard Henderson     img->sym[1].st_value = buf;
26385872bbf2SRichard Henderson     img->sym[1].st_size = buf_size;
26395872bbf2SRichard Henderson 
26405872bbf2SRichard Henderson     img->di.cu_low_pc = buf;
26415872bbf2SRichard Henderson     img->di.cu_high_pc = buf_size;
26425872bbf2SRichard Henderson     img->di.fn_low_pc = buf;
26435872bbf2SRichard Henderson     img->di.fn_high_pc = buf_size;
2644813da627SRichard Henderson 
2645813da627SRichard Henderson #ifdef DEBUG_JIT
2646813da627SRichard Henderson     /* Enable this block to be able to debug the ELF image file creation.
2647813da627SRichard Henderson        One can use readelf, objdump, or other inspection utilities.  */
2648813da627SRichard Henderson     {
2649813da627SRichard Henderson         FILE *f = fopen("/tmp/qemu.jit", "w+b");
2650813da627SRichard Henderson         if (f) {
26515872bbf2SRichard Henderson             if (fwrite(img, img_size, 1, f) != img_size) {
2652813da627SRichard Henderson                 /* Avoid stupid unused return value warning for fwrite.  */
2653813da627SRichard Henderson             }
2654813da627SRichard Henderson             fclose(f);
2655813da627SRichard Henderson         }
2656813da627SRichard Henderson     }
2657813da627SRichard Henderson #endif
2658813da627SRichard Henderson 
2659813da627SRichard Henderson     one_entry.symfile_addr = img;
2660813da627SRichard Henderson     one_entry.symfile_size = img_size;
2661813da627SRichard Henderson 
2662813da627SRichard Henderson     __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
2663813da627SRichard Henderson     __jit_debug_descriptor.relevant_entry = &one_entry;
2664813da627SRichard Henderson     __jit_debug_descriptor.first_entry = &one_entry;
2665813da627SRichard Henderson     __jit_debug_register_code();
2666813da627SRichard Henderson }
2667813da627SRichard Henderson #else
26685872bbf2SRichard Henderson /* No support for the feature.  Provide the entry point expected by exec.c,
26695872bbf2SRichard Henderson    and implement the internal function we declared earlier.  */
2670813da627SRichard Henderson 
2671813da627SRichard Henderson static void tcg_register_jit_int(void *buf, size_t size,
2672813da627SRichard Henderson                                  void *debug_frame, size_t debug_frame_size)
2673813da627SRichard Henderson {
2674813da627SRichard Henderson }
2675813da627SRichard Henderson 
2676813da627SRichard Henderson void tcg_register_jit(void *buf, size_t buf_size)
2677813da627SRichard Henderson {
2678813da627SRichard Henderson }
2679813da627SRichard Henderson #endif /* ELF_HOST_MACHINE */
2680