xref: /openbmc/qemu/tcg/tcg.c (revision cf60bce4)
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 suppress various consistency checks (faster) */
26c896fe29Sbellard #define NDEBUG
27c896fe29Sbellard 
28c896fe29Sbellard /* define it to use liveness analysis (better code) */
29c896fe29Sbellard #define USE_LIVENESS_ANALYSIS
30c896fe29Sbellard 
31c896fe29Sbellard #include <assert.h>
32c896fe29Sbellard #include <stdarg.h>
33c896fe29Sbellard #include <stdlib.h>
34c896fe29Sbellard #include <stdio.h>
35c896fe29Sbellard #include <string.h>
36c896fe29Sbellard #include <inttypes.h>
373fe43da7Sbellard #ifdef _WIN32
383fe43da7Sbellard #include <malloc.h>
393fe43da7Sbellard #endif
40c896fe29Sbellard 
41c896fe29Sbellard #include "config.h"
42ca10f867Saurel32 #include "qemu-common.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 #include "exec-all.h"
50c896fe29Sbellard 
51c896fe29Sbellard #include "tcg-op.h"
52c896fe29Sbellard #include "elf.h"
53c896fe29Sbellard 
54c896fe29Sbellard 
55c896fe29Sbellard static void patch_reloc(uint8_t *code_ptr, int type,
56f54b3f92Saurel32                         tcg_target_long value, tcg_target_long addend);
57c896fe29Sbellard 
58c896fe29Sbellard TCGOpDef tcg_op_defs[] = {
59c896fe29Sbellard #define DEF(s, n, copy_size) { #s, 0, 0, n, n, 0, copy_size },
60c896fe29Sbellard #define DEF2(s, iargs, oargs, cargs, flags) { #s, iargs, oargs, cargs, iargs + oargs + cargs, flags, 0 },
61c896fe29Sbellard #include "tcg-opc.h"
62c896fe29Sbellard #undef DEF
63c896fe29Sbellard #undef DEF2
64c896fe29Sbellard };
65c896fe29Sbellard 
66c896fe29Sbellard TCGRegSet tcg_target_available_regs[2];
67c896fe29Sbellard TCGRegSet tcg_target_call_clobber_regs;
68c896fe29Sbellard 
69c896fe29Sbellard /* XXX: move that inside the context */
70c896fe29Sbellard uint16_t *gen_opc_ptr;
71c896fe29Sbellard TCGArg *gen_opparam_ptr;
72c896fe29Sbellard 
73c896fe29Sbellard static inline void tcg_out8(TCGContext *s, uint8_t v)
74c896fe29Sbellard {
75c896fe29Sbellard     *s->code_ptr++ = v;
76c896fe29Sbellard }
77c896fe29Sbellard 
78c896fe29Sbellard static inline void tcg_out16(TCGContext *s, uint16_t v)
79c896fe29Sbellard {
80c896fe29Sbellard     *(uint16_t *)s->code_ptr = v;
81c896fe29Sbellard     s->code_ptr += 2;
82c896fe29Sbellard }
83c896fe29Sbellard 
84c896fe29Sbellard static inline void tcg_out32(TCGContext *s, uint32_t v)
85c896fe29Sbellard {
86c896fe29Sbellard     *(uint32_t *)s->code_ptr = v;
87c896fe29Sbellard     s->code_ptr += 4;
88c896fe29Sbellard }
89c896fe29Sbellard 
90c896fe29Sbellard /* label relocation processing */
91c896fe29Sbellard 
92c896fe29Sbellard void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
93c896fe29Sbellard                    int label_index, long addend)
94c896fe29Sbellard {
95c896fe29Sbellard     TCGLabel *l;
96c896fe29Sbellard     TCGRelocation *r;
97c896fe29Sbellard 
98c896fe29Sbellard     l = &s->labels[label_index];
99c896fe29Sbellard     if (l->has_value) {
100623e265cSpbrook         /* FIXME: This may break relocations on RISC targets that
101623e265cSpbrook            modify instruction fields in place.  The caller may not have
102623e265cSpbrook            written the initial value.  */
103f54b3f92Saurel32         patch_reloc(code_ptr, type, l->u.value, addend);
104c896fe29Sbellard     } else {
105c896fe29Sbellard         /* add a new relocation entry */
106c896fe29Sbellard         r = tcg_malloc(sizeof(TCGRelocation));
107c896fe29Sbellard         r->type = type;
108c896fe29Sbellard         r->ptr = code_ptr;
109c896fe29Sbellard         r->addend = addend;
110c896fe29Sbellard         r->next = l->u.first_reloc;
111c896fe29Sbellard         l->u.first_reloc = r;
112c896fe29Sbellard     }
113c896fe29Sbellard }
114c896fe29Sbellard 
115c896fe29Sbellard static void tcg_out_label(TCGContext *s, int label_index,
116c896fe29Sbellard                           tcg_target_long value)
117c896fe29Sbellard {
118c896fe29Sbellard     TCGLabel *l;
119c896fe29Sbellard     TCGRelocation *r;
120c896fe29Sbellard 
121c896fe29Sbellard     l = &s->labels[label_index];
122c896fe29Sbellard     if (l->has_value)
123c896fe29Sbellard         tcg_abort();
124c896fe29Sbellard     r = l->u.first_reloc;
125c896fe29Sbellard     while (r != NULL) {
126f54b3f92Saurel32         patch_reloc(r->ptr, r->type, value, r->addend);
127c896fe29Sbellard         r = r->next;
128c896fe29Sbellard     }
129c896fe29Sbellard     l->has_value = 1;
130c896fe29Sbellard     l->u.value = value;
131c896fe29Sbellard }
132c896fe29Sbellard 
133c896fe29Sbellard int gen_new_label(void)
134c896fe29Sbellard {
135c896fe29Sbellard     TCGContext *s = &tcg_ctx;
136c896fe29Sbellard     int idx;
137c896fe29Sbellard     TCGLabel *l;
138c896fe29Sbellard 
139c896fe29Sbellard     if (s->nb_labels >= TCG_MAX_LABELS)
140c896fe29Sbellard         tcg_abort();
141c896fe29Sbellard     idx = s->nb_labels++;
142c896fe29Sbellard     l = &s->labels[idx];
143c896fe29Sbellard     l->has_value = 0;
144c896fe29Sbellard     l->u.first_reloc = NULL;
145c896fe29Sbellard     return idx;
146c896fe29Sbellard }
147c896fe29Sbellard 
148c896fe29Sbellard #include "tcg-target.c"
149c896fe29Sbellard 
150c896fe29Sbellard /* pool based memory allocation */
151c896fe29Sbellard void *tcg_malloc_internal(TCGContext *s, int size)
152c896fe29Sbellard {
153c896fe29Sbellard     TCGPool *p;
154c896fe29Sbellard     int pool_size;
155c896fe29Sbellard 
156c896fe29Sbellard     if (size > TCG_POOL_CHUNK_SIZE) {
157c896fe29Sbellard         /* big malloc: insert a new pool (XXX: could optimize) */
158c896fe29Sbellard         p = qemu_malloc(sizeof(TCGPool) + size);
159c896fe29Sbellard         p->size = size;
160c896fe29Sbellard         if (s->pool_current)
161c896fe29Sbellard             s->pool_current->next = p;
162c896fe29Sbellard         else
163c896fe29Sbellard             s->pool_first = p;
164c896fe29Sbellard         p->next = s->pool_current;
165c896fe29Sbellard     } else {
166c896fe29Sbellard         p = s->pool_current;
167c896fe29Sbellard         if (!p) {
168c896fe29Sbellard             p = s->pool_first;
169c896fe29Sbellard             if (!p)
170c896fe29Sbellard                 goto new_pool;
171c896fe29Sbellard         } else {
172c896fe29Sbellard             if (!p->next) {
173c896fe29Sbellard             new_pool:
174c896fe29Sbellard                 pool_size = TCG_POOL_CHUNK_SIZE;
175c896fe29Sbellard                 p = qemu_malloc(sizeof(TCGPool) + pool_size);
176c896fe29Sbellard                 p->size = pool_size;
177c896fe29Sbellard                 p->next = NULL;
178c896fe29Sbellard                 if (s->pool_current)
179c896fe29Sbellard                     s->pool_current->next = p;
180c896fe29Sbellard                 else
181c896fe29Sbellard                     s->pool_first = p;
182c896fe29Sbellard             } else {
183c896fe29Sbellard                 p = p->next;
184c896fe29Sbellard             }
185c896fe29Sbellard         }
186c896fe29Sbellard     }
187c896fe29Sbellard     s->pool_current = p;
188c896fe29Sbellard     s->pool_cur = p->data + size;
189c896fe29Sbellard     s->pool_end = p->data + p->size;
190c896fe29Sbellard     return p->data;
191c896fe29Sbellard }
192c896fe29Sbellard 
193c896fe29Sbellard void tcg_pool_reset(TCGContext *s)
194c896fe29Sbellard {
195c896fe29Sbellard     s->pool_cur = s->pool_end = NULL;
196c896fe29Sbellard     s->pool_current = NULL;
197c896fe29Sbellard }
198c896fe29Sbellard 
199c896fe29Sbellard /* free all the pool */
200c896fe29Sbellard void tcg_pool_free(TCGContext *s)
201c896fe29Sbellard {
202c896fe29Sbellard     TCGPool *p, *p1;
203c896fe29Sbellard 
204c896fe29Sbellard     for(p = s->pool_first; p != NULL; p = p1) {
205c896fe29Sbellard         p1 = p->next;
206c896fe29Sbellard         qemu_free(p);
207c896fe29Sbellard     }
208c896fe29Sbellard     s->pool_first = NULL;
209c896fe29Sbellard     s->pool_cur = s->pool_end = NULL;
210c896fe29Sbellard }
211c896fe29Sbellard 
212c896fe29Sbellard void tcg_context_init(TCGContext *s)
213c896fe29Sbellard {
214c896fe29Sbellard     int op, total_args, n;
215c896fe29Sbellard     TCGOpDef *def;
216c896fe29Sbellard     TCGArgConstraint *args_ct;
217c896fe29Sbellard     int *sorted_args;
218c896fe29Sbellard 
219c896fe29Sbellard     memset(s, 0, sizeof(*s));
220c896fe29Sbellard     s->temps = s->static_temps;
221c896fe29Sbellard     s->nb_globals = 0;
222c896fe29Sbellard 
223c896fe29Sbellard     /* Count total number of arguments and allocate the corresponding
224c896fe29Sbellard        space */
225c896fe29Sbellard     total_args = 0;
226c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
227c896fe29Sbellard         def = &tcg_op_defs[op];
228c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
229c896fe29Sbellard         total_args += n;
230c896fe29Sbellard     }
231c896fe29Sbellard 
232c896fe29Sbellard     args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args);
233c896fe29Sbellard     sorted_args = qemu_malloc(sizeof(int) * total_args);
234c896fe29Sbellard 
235c896fe29Sbellard     for(op = 0; op < NB_OPS; op++) {
236c896fe29Sbellard         def = &tcg_op_defs[op];
237c896fe29Sbellard         def->args_ct = args_ct;
238c896fe29Sbellard         def->sorted_args = sorted_args;
239c896fe29Sbellard         n = def->nb_iargs + def->nb_oargs;
240c896fe29Sbellard         sorted_args += n;
241c896fe29Sbellard         args_ct += n;
242c896fe29Sbellard     }
243c896fe29Sbellard 
244c896fe29Sbellard     tcg_target_init(s);
245b03cce8eSbellard 
246b03cce8eSbellard     /* init global prologue and epilogue */
247b03cce8eSbellard     s->code_buf = code_gen_prologue;
248b03cce8eSbellard     s->code_ptr = s->code_buf;
249b03cce8eSbellard     tcg_target_qemu_prologue(s);
250b03cce8eSbellard     flush_icache_range((unsigned long)s->code_buf,
251b03cce8eSbellard                        (unsigned long)s->code_ptr);
252c896fe29Sbellard }
253c896fe29Sbellard 
254c896fe29Sbellard void tcg_set_frame(TCGContext *s, int reg,
255c896fe29Sbellard                    tcg_target_long start, tcg_target_long size)
256c896fe29Sbellard {
257c896fe29Sbellard     s->frame_start = start;
258c896fe29Sbellard     s->frame_end = start + size;
259c896fe29Sbellard     s->frame_reg = reg;
260c896fe29Sbellard }
261c896fe29Sbellard 
262c896fe29Sbellard void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func)
263c896fe29Sbellard {
264c896fe29Sbellard     s->macro_func = func;
265c896fe29Sbellard }
266c896fe29Sbellard 
267c896fe29Sbellard void tcg_func_start(TCGContext *s)
268c896fe29Sbellard {
269c896fe29Sbellard     tcg_pool_reset(s);
270c896fe29Sbellard     s->nb_temps = s->nb_globals;
271c896fe29Sbellard     s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS);
272c896fe29Sbellard     s->nb_labels = 0;
273c896fe29Sbellard     s->current_frame_offset = s->frame_start;
274c896fe29Sbellard 
275c896fe29Sbellard     gen_opc_ptr = gen_opc_buf;
276c896fe29Sbellard     gen_opparam_ptr = gen_opparam_buf;
277c896fe29Sbellard }
278c896fe29Sbellard 
279c896fe29Sbellard static inline void tcg_temp_alloc(TCGContext *s, int n)
280c896fe29Sbellard {
281c896fe29Sbellard     if (n > TCG_MAX_TEMPS)
282c896fe29Sbellard         tcg_abort();
283c896fe29Sbellard }
284c896fe29Sbellard 
285ac56dd48Spbrook TCGv tcg_global_reg_new(TCGType type, int reg, const char *name)
286c896fe29Sbellard {
287c896fe29Sbellard     TCGContext *s = &tcg_ctx;
288c896fe29Sbellard     TCGTemp *ts;
289c896fe29Sbellard     int idx;
290c896fe29Sbellard 
291c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
292c896fe29Sbellard     if (type != TCG_TYPE_I32)
293c896fe29Sbellard         tcg_abort();
294c896fe29Sbellard #endif
295c896fe29Sbellard     if (tcg_regset_test_reg(s->reserved_regs, reg))
296c896fe29Sbellard         tcg_abort();
297c896fe29Sbellard     idx = s->nb_globals;
298c896fe29Sbellard     tcg_temp_alloc(s, s->nb_globals + 1);
299c896fe29Sbellard     ts = &s->temps[s->nb_globals];
300c896fe29Sbellard     ts->base_type = type;
301c896fe29Sbellard     ts->type = type;
302c896fe29Sbellard     ts->fixed_reg = 1;
303c896fe29Sbellard     ts->reg = reg;
304c896fe29Sbellard     ts->val_type = TEMP_VAL_REG;
305c896fe29Sbellard     ts->name = name;
306c896fe29Sbellard     s->nb_globals++;
307c896fe29Sbellard     tcg_regset_set_reg(s->reserved_regs, reg);
308ac56dd48Spbrook     return MAKE_TCGV(idx);
309c896fe29Sbellard }
310c896fe29Sbellard 
3116a8d7b76Sbellard #if TCG_TARGET_REG_BITS == 32
3126a8d7b76Sbellard /* temporary hack to avoid register shortage for tcg_qemu_st64() */
3136a8d7b76Sbellard TCGv tcg_global_reg2_new_hack(TCGType type, int reg1, int reg2,
3146a8d7b76Sbellard                               const char *name)
3156a8d7b76Sbellard {
3166a8d7b76Sbellard     TCGContext *s = &tcg_ctx;
3176a8d7b76Sbellard     TCGTemp *ts;
3186a8d7b76Sbellard     int idx;
3196a8d7b76Sbellard     char buf[64];
3206a8d7b76Sbellard 
3216a8d7b76Sbellard     if (type != TCG_TYPE_I64)
3226a8d7b76Sbellard         tcg_abort();
3236a8d7b76Sbellard     idx = s->nb_globals;
3246a8d7b76Sbellard     tcg_temp_alloc(s, s->nb_globals + 2);
3256a8d7b76Sbellard     ts = &s->temps[s->nb_globals];
3266a8d7b76Sbellard     ts->base_type = type;
3276a8d7b76Sbellard     ts->type = TCG_TYPE_I32;
3286a8d7b76Sbellard     ts->fixed_reg = 1;
3296a8d7b76Sbellard     ts->reg = reg1;
3306a8d7b76Sbellard     ts->val_type = TEMP_VAL_REG;
3316a8d7b76Sbellard     pstrcpy(buf, sizeof(buf), name);
3326a8d7b76Sbellard     pstrcat(buf, sizeof(buf), "_0");
3336a8d7b76Sbellard     ts->name = strdup(buf);
3346a8d7b76Sbellard 
3356a8d7b76Sbellard     ts++;
3366a8d7b76Sbellard     ts->base_type = type;
3376a8d7b76Sbellard     ts->type = TCG_TYPE_I32;
3386a8d7b76Sbellard     ts->fixed_reg = 1;
3396a8d7b76Sbellard     ts->reg = reg2;
3406a8d7b76Sbellard     ts->val_type = TEMP_VAL_REG;
3416a8d7b76Sbellard     pstrcpy(buf, sizeof(buf), name);
3426a8d7b76Sbellard     pstrcat(buf, sizeof(buf), "_1");
3436a8d7b76Sbellard     ts->name = strdup(buf);
3446a8d7b76Sbellard 
3456a8d7b76Sbellard     s->nb_globals += 2;
3466a8d7b76Sbellard     return MAKE_TCGV(idx);
3476a8d7b76Sbellard }
3486a8d7b76Sbellard #endif
3496a8d7b76Sbellard 
350ac56dd48Spbrook TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
351c896fe29Sbellard                         const char *name)
352c896fe29Sbellard {
353c896fe29Sbellard     TCGContext *s = &tcg_ctx;
354c896fe29Sbellard     TCGTemp *ts;
355c896fe29Sbellard     int idx;
356c896fe29Sbellard 
357c896fe29Sbellard     idx = s->nb_globals;
358c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
359c896fe29Sbellard     if (type == TCG_TYPE_I64) {
360c896fe29Sbellard         char buf[64];
361c896fe29Sbellard         tcg_temp_alloc(s, s->nb_globals + 1);
362c896fe29Sbellard         ts = &s->temps[s->nb_globals];
363c896fe29Sbellard         ts->base_type = type;
364c896fe29Sbellard         ts->type = TCG_TYPE_I32;
365c896fe29Sbellard         ts->fixed_reg = 0;
366c896fe29Sbellard         ts->mem_allocated = 1;
367c896fe29Sbellard         ts->mem_reg = reg;
368c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN
369c896fe29Sbellard         ts->mem_offset = offset + 4;
370c896fe29Sbellard #else
371c896fe29Sbellard         ts->mem_offset = offset;
372c896fe29Sbellard #endif
373c896fe29Sbellard         ts->val_type = TEMP_VAL_MEM;
374c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
375c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_0");
376c896fe29Sbellard         ts->name = strdup(buf);
377c896fe29Sbellard         ts++;
378c896fe29Sbellard 
379c896fe29Sbellard         ts->base_type = type;
380c896fe29Sbellard         ts->type = TCG_TYPE_I32;
381c896fe29Sbellard         ts->fixed_reg = 0;
382c896fe29Sbellard         ts->mem_allocated = 1;
383c896fe29Sbellard         ts->mem_reg = reg;
384c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN
385c896fe29Sbellard         ts->mem_offset = offset;
386c896fe29Sbellard #else
387c896fe29Sbellard         ts->mem_offset = offset + 4;
388c896fe29Sbellard #endif
389c896fe29Sbellard         ts->val_type = TEMP_VAL_MEM;
390c896fe29Sbellard         pstrcpy(buf, sizeof(buf), name);
391c896fe29Sbellard         pstrcat(buf, sizeof(buf), "_1");
392c896fe29Sbellard         ts->name = strdup(buf);
393c896fe29Sbellard 
394c896fe29Sbellard         s->nb_globals += 2;
395c896fe29Sbellard     } else
396c896fe29Sbellard #endif
397c896fe29Sbellard     {
398c896fe29Sbellard         tcg_temp_alloc(s, s->nb_globals + 1);
399c896fe29Sbellard         ts = &s->temps[s->nb_globals];
400c896fe29Sbellard         ts->base_type = type;
401c896fe29Sbellard         ts->type = type;
402c896fe29Sbellard         ts->fixed_reg = 0;
403c896fe29Sbellard         ts->mem_allocated = 1;
404c896fe29Sbellard         ts->mem_reg = reg;
405c896fe29Sbellard         ts->mem_offset = offset;
406c896fe29Sbellard         ts->val_type = TEMP_VAL_MEM;
407c896fe29Sbellard         ts->name = name;
408c896fe29Sbellard         s->nb_globals++;
409c896fe29Sbellard     }
410ac56dd48Spbrook     return MAKE_TCGV(idx);
411c896fe29Sbellard }
412c896fe29Sbellard 
413ac56dd48Spbrook TCGv tcg_temp_new(TCGType type)
414c896fe29Sbellard {
415c896fe29Sbellard     TCGContext *s = &tcg_ctx;
416c896fe29Sbellard     TCGTemp *ts;
417c896fe29Sbellard     int idx;
418c896fe29Sbellard 
419c896fe29Sbellard     idx = s->nb_temps;
420c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
421c896fe29Sbellard     if (type == TCG_TYPE_I64) {
422c896fe29Sbellard         tcg_temp_alloc(s, s->nb_temps + 1);
423c896fe29Sbellard         ts = &s->temps[s->nb_temps];
424c896fe29Sbellard         ts->base_type = type;
425c896fe29Sbellard         ts->type = TCG_TYPE_I32;
4265ff9d6a4Sbellard         ts->fixed_reg = 0;
427c896fe29Sbellard         ts->val_type = TEMP_VAL_DEAD;
428c896fe29Sbellard         ts->mem_allocated = 0;
429c896fe29Sbellard         ts->name = NULL;
430c896fe29Sbellard         ts++;
431c896fe29Sbellard         ts->base_type = TCG_TYPE_I32;
432c896fe29Sbellard         ts->type = TCG_TYPE_I32;
433c896fe29Sbellard         ts->val_type = TEMP_VAL_DEAD;
4345ff9d6a4Sbellard         ts->fixed_reg = 0;
435c896fe29Sbellard         ts->mem_allocated = 0;
436c896fe29Sbellard         ts->name = NULL;
437c896fe29Sbellard         s->nb_temps += 2;
438c896fe29Sbellard     } else
439c896fe29Sbellard #endif
440c896fe29Sbellard     {
441c896fe29Sbellard         tcg_temp_alloc(s, s->nb_temps + 1);
442c896fe29Sbellard         ts = &s->temps[s->nb_temps];
443c896fe29Sbellard         ts->base_type = type;
444c896fe29Sbellard         ts->type = type;
4455ff9d6a4Sbellard         ts->fixed_reg = 0;
446c896fe29Sbellard         ts->val_type = TEMP_VAL_DEAD;
447c896fe29Sbellard         ts->mem_allocated = 0;
448c896fe29Sbellard         ts->name = NULL;
449c896fe29Sbellard         s->nb_temps++;
450c896fe29Sbellard     }
451ac56dd48Spbrook     return MAKE_TCGV(idx);
452c896fe29Sbellard }
453c896fe29Sbellard 
454ac56dd48Spbrook TCGv tcg_const_i32(int32_t val)
455c896fe29Sbellard {
456c896fe29Sbellard     TCGContext *s = &tcg_ctx;
457c896fe29Sbellard     TCGTemp *ts;
458c896fe29Sbellard     int idx;
459c896fe29Sbellard 
460c896fe29Sbellard     idx = s->nb_temps;
461c896fe29Sbellard     tcg_temp_alloc(s, idx + 1);
462c896fe29Sbellard     ts = &s->temps[idx];
463c896fe29Sbellard     ts->base_type = ts->type = TCG_TYPE_I32;
464c896fe29Sbellard     ts->val_type = TEMP_VAL_CONST;
465c896fe29Sbellard     ts->name = NULL;
466c896fe29Sbellard     ts->val = val;
467c896fe29Sbellard     s->nb_temps++;
468ac56dd48Spbrook     return MAKE_TCGV(idx);
469c896fe29Sbellard }
470c896fe29Sbellard 
471ac56dd48Spbrook TCGv tcg_const_i64(int64_t val)
472c896fe29Sbellard {
473c896fe29Sbellard     TCGContext *s = &tcg_ctx;
474c896fe29Sbellard     TCGTemp *ts;
475c896fe29Sbellard     int idx;
476c896fe29Sbellard 
477c896fe29Sbellard     idx = s->nb_temps;
478c896fe29Sbellard #if TCG_TARGET_REG_BITS == 32
479c896fe29Sbellard     tcg_temp_alloc(s, idx + 2);
480c896fe29Sbellard     ts = &s->temps[idx];
481c896fe29Sbellard     ts->base_type = TCG_TYPE_I64;
482c896fe29Sbellard     ts->type = TCG_TYPE_I32;
483c896fe29Sbellard     ts->val_type = TEMP_VAL_CONST;
484c896fe29Sbellard     ts->name = NULL;
485c896fe29Sbellard     ts->val = val;
486c896fe29Sbellard     ts++;
487c896fe29Sbellard     ts->base_type = TCG_TYPE_I32;
488c896fe29Sbellard     ts->type = TCG_TYPE_I32;
489c896fe29Sbellard     ts->val_type = TEMP_VAL_CONST;
490c896fe29Sbellard     ts->name = NULL;
491c896fe29Sbellard     ts->val = val >> 32;
492c896fe29Sbellard     s->nb_temps += 2;
493c896fe29Sbellard #else
494c896fe29Sbellard     tcg_temp_alloc(s, idx + 1);
495c896fe29Sbellard     ts = &s->temps[idx];
496c896fe29Sbellard     ts->base_type = ts->type = TCG_TYPE_I64;
497c896fe29Sbellard     ts->val_type = TEMP_VAL_CONST;
498c896fe29Sbellard     ts->name = NULL;
499c896fe29Sbellard     ts->val = val;
500c896fe29Sbellard     s->nb_temps++;
501c896fe29Sbellard #endif
502ac56dd48Spbrook     return MAKE_TCGV(idx);
503c896fe29Sbellard }
504c896fe29Sbellard 
505c896fe29Sbellard void tcg_register_helper(void *func, const char *name)
506c896fe29Sbellard {
507c896fe29Sbellard     TCGContext *s = &tcg_ctx;
508c896fe29Sbellard     int n;
509c896fe29Sbellard     if ((s->nb_helpers + 1) > s->allocated_helpers) {
510c896fe29Sbellard         n = s->allocated_helpers;
511c896fe29Sbellard         if (n == 0) {
512c896fe29Sbellard             n = 4;
513c896fe29Sbellard         } else {
514c896fe29Sbellard             n *= 2;
515c896fe29Sbellard         }
516c896fe29Sbellard         s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo));
517c896fe29Sbellard         s->allocated_helpers = n;
518c896fe29Sbellard     }
519c896fe29Sbellard     s->helpers[s->nb_helpers].func = func;
520c896fe29Sbellard     s->helpers[s->nb_helpers].name = name;
521c896fe29Sbellard     s->nb_helpers++;
522c896fe29Sbellard }
523c896fe29Sbellard 
524c896fe29Sbellard const char *tcg_helper_get_name(TCGContext *s, void *func)
525c896fe29Sbellard {
526c896fe29Sbellard     int i;
527c896fe29Sbellard 
528c896fe29Sbellard     for(i = 0; i < s->nb_helpers; i++) {
529c896fe29Sbellard         if (s->helpers[i].func == func)
530c896fe29Sbellard             return s->helpers[i].name;
531c896fe29Sbellard     }
532c896fe29Sbellard     return NULL;
533c896fe29Sbellard }
534c896fe29Sbellard 
535ac56dd48Spbrook static inline TCGType tcg_get_base_type(TCGContext *s, TCGv arg)
536c896fe29Sbellard {
537ac56dd48Spbrook     return s->temps[GET_TCGV(arg)].base_type;
538c896fe29Sbellard }
539c896fe29Sbellard 
540ac56dd48Spbrook static void tcg_gen_call_internal(TCGContext *s, TCGv func,
541c896fe29Sbellard                                   unsigned int flags,
542ac56dd48Spbrook                                   unsigned int nb_rets, const TCGv *rets,
543ac56dd48Spbrook                                   unsigned int nb_params, const TCGv *params)
544c896fe29Sbellard {
545c896fe29Sbellard     int i;
546c896fe29Sbellard     *gen_opc_ptr++ = INDEX_op_call;
547c896fe29Sbellard     *gen_opparam_ptr++ = (nb_rets << 16) | (nb_params + 1);
548c896fe29Sbellard     for(i = 0; i < nb_rets; i++) {
549ac56dd48Spbrook         *gen_opparam_ptr++ = GET_TCGV(rets[i]);
550c896fe29Sbellard     }
551c896fe29Sbellard     for(i = 0; i < nb_params; i++) {
552ac56dd48Spbrook         *gen_opparam_ptr++ = GET_TCGV(params[i]);
553c896fe29Sbellard     }
554ac56dd48Spbrook     *gen_opparam_ptr++ = GET_TCGV(func);
555c896fe29Sbellard 
556c896fe29Sbellard     *gen_opparam_ptr++ = flags;
557c896fe29Sbellard     /* total parameters, needed to go backward in the instruction stream */
558c896fe29Sbellard     *gen_opparam_ptr++ = 1 + nb_rets + nb_params + 3;
559c896fe29Sbellard }
560c896fe29Sbellard 
561c896fe29Sbellard 
562c896fe29Sbellard #if TCG_TARGET_REG_BITS < 64
563c896fe29Sbellard /* Note: we convert the 64 bit args to 32 bit */
564ac56dd48Spbrook void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
565ac56dd48Spbrook                   unsigned int nb_rets, const TCGv *rets,
566ac56dd48Spbrook                   unsigned int nb_params, const TCGv *args1)
567c896fe29Sbellard {
568ac56dd48Spbrook     TCGv ret, *args2, rets_2[2], arg;
569c896fe29Sbellard     int j, i, call_type;
570c896fe29Sbellard 
571c896fe29Sbellard     if (nb_rets == 1) {
572c896fe29Sbellard         ret = rets[0];
573c896fe29Sbellard         if (tcg_get_base_type(s, ret) == TCG_TYPE_I64) {
574c896fe29Sbellard             nb_rets = 2;
575c896fe29Sbellard             rets_2[0] = ret;
576ac56dd48Spbrook             rets_2[1] = TCGV_HIGH(ret);
577c896fe29Sbellard             rets = rets_2;
578c896fe29Sbellard         }
579c896fe29Sbellard     }
580ac56dd48Spbrook     args2 = alloca((nb_params * 2) * sizeof(TCGv));
581c896fe29Sbellard     j = 0;
582c896fe29Sbellard     call_type = (flags & TCG_CALL_TYPE_MASK);
583c896fe29Sbellard     for(i = 0; i < nb_params; i++) {
584c896fe29Sbellard         arg = args1[i];
585c896fe29Sbellard         if (tcg_get_base_type(s, arg) == TCG_TYPE_I64) {
586c896fe29Sbellard #ifdef TCG_TARGET_I386
587c896fe29Sbellard             /* REGPARM case: if the third parameter is 64 bit, it is
588c896fe29Sbellard                allocated on the stack */
589c896fe29Sbellard             if (j == 2 && call_type == TCG_CALL_TYPE_REGPARM) {
590c896fe29Sbellard                 call_type = TCG_CALL_TYPE_REGPARM_2;
591c896fe29Sbellard                 flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type;
592c896fe29Sbellard             }
593c896fe29Sbellard             args2[j++] = arg;
594ac56dd48Spbrook             args2[j++] = TCGV_HIGH(arg);
595c896fe29Sbellard #else
596c896fe29Sbellard #ifdef TCG_TARGET_WORDS_BIGENDIAN
597a0d69e00Sblueswir1             args2[j++] = TCGV_HIGH(arg);
598c896fe29Sbellard             args2[j++] = arg;
599c896fe29Sbellard #else
600c896fe29Sbellard             args2[j++] = arg;
601ac56dd48Spbrook             args2[j++] = TCGV_HIGH(arg);
602c896fe29Sbellard #endif
603c896fe29Sbellard #endif
604c896fe29Sbellard         } else {
605c896fe29Sbellard             args2[j++] = arg;
606c896fe29Sbellard         }
607c896fe29Sbellard     }
608c896fe29Sbellard     tcg_gen_call_internal(s, func, flags,
609c896fe29Sbellard                           nb_rets, rets, j, args2);
610c896fe29Sbellard }
611c896fe29Sbellard #else
612ac56dd48Spbrook void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
613ac56dd48Spbrook                   unsigned int nb_rets, const TCGv *rets,
614ac56dd48Spbrook                   unsigned int nb_params, const TCGv *args1)
615c896fe29Sbellard {
616c896fe29Sbellard     tcg_gen_call_internal(s, func, flags,
617c896fe29Sbellard                           nb_rets, rets, nb_params, args1);
618c896fe29Sbellard }
619c896fe29Sbellard #endif
620c896fe29Sbellard 
621ac56dd48Spbrook #if TCG_TARGET_REG_BITS == 32
622ac56dd48Spbrook void tcg_gen_shifti_i64(TCGv ret, TCGv arg1,
623c896fe29Sbellard                         int c, int right, int arith)
624c896fe29Sbellard {
625*cf60bce4Sbellard     if (c == 0) {
626*cf60bce4Sbellard         tcg_gen_mov_i32(ret, arg1);
627*cf60bce4Sbellard         tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1));
628*cf60bce4Sbellard     } else if (c >= 32) {
629c896fe29Sbellard         c -= 32;
630c896fe29Sbellard         if (right) {
631c896fe29Sbellard             if (arith) {
632ac56dd48Spbrook                 tcg_gen_sari_i32(ret, TCGV_HIGH(arg1), c);
633ac56dd48Spbrook                 tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31);
634c896fe29Sbellard             } else {
635ac56dd48Spbrook                 tcg_gen_shri_i32(ret, TCGV_HIGH(arg1), c);
636ac56dd48Spbrook                 tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
637c896fe29Sbellard             }
638c896fe29Sbellard         } else {
639ac56dd48Spbrook             tcg_gen_shli_i32(TCGV_HIGH(ret), arg1, c);
640c896fe29Sbellard             tcg_gen_movi_i32(ret, 0);
641c896fe29Sbellard         }
642c896fe29Sbellard     } else {
643ac56dd48Spbrook         TCGv t0, t1;
644c896fe29Sbellard 
645c896fe29Sbellard         t0 = tcg_temp_new(TCG_TYPE_I32);
646c896fe29Sbellard         t1 = tcg_temp_new(TCG_TYPE_I32);
647c896fe29Sbellard         if (right) {
648ac56dd48Spbrook             tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c);
649c896fe29Sbellard             if (arith)
650ac56dd48Spbrook                 tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c);
651c896fe29Sbellard             else
652ac56dd48Spbrook                 tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c);
653c896fe29Sbellard             tcg_gen_shri_i32(ret, arg1, c);
654c896fe29Sbellard             tcg_gen_or_i32(ret, ret, t0);
655ac56dd48Spbrook             tcg_gen_mov_i32(TCGV_HIGH(ret), t1);
656c896fe29Sbellard         } else {
657c896fe29Sbellard             tcg_gen_shri_i32(t0, arg1, 32 - c);
658c896fe29Sbellard             /* Note: ret can be the same as arg1, so we use t1 */
659c896fe29Sbellard             tcg_gen_shli_i32(t1, arg1, c);
660ac56dd48Spbrook             tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c);
661ac56dd48Spbrook             tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0);
662c896fe29Sbellard             tcg_gen_mov_i32(ret, t1);
663c896fe29Sbellard         }
664c896fe29Sbellard     }
665c896fe29Sbellard }
666ac56dd48Spbrook #endif
667c896fe29Sbellard 
668c896fe29Sbellard void tcg_reg_alloc_start(TCGContext *s)
669c896fe29Sbellard {
670c896fe29Sbellard     int i;
671c896fe29Sbellard     TCGTemp *ts;
672c896fe29Sbellard     for(i = 0; i < s->nb_globals; i++) {
673c896fe29Sbellard         ts = &s->temps[i];
674c896fe29Sbellard         if (ts->fixed_reg) {
675c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
676c896fe29Sbellard         } else {
677c896fe29Sbellard             ts->val_type = TEMP_VAL_MEM;
678c896fe29Sbellard         }
679c896fe29Sbellard     }
680c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
681c896fe29Sbellard         s->reg_to_temp[i] = -1;
682c896fe29Sbellard     }
683c896fe29Sbellard }
684c896fe29Sbellard 
685ac56dd48Spbrook static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size,
686ac56dd48Spbrook                                  int idx)
687c896fe29Sbellard {
688c896fe29Sbellard     TCGTemp *ts;
689ac56dd48Spbrook 
690ac56dd48Spbrook     ts = &s->temps[idx];
691ac56dd48Spbrook     if (idx < s->nb_globals) {
692ac56dd48Spbrook         pstrcpy(buf, buf_size, ts->name);
693c896fe29Sbellard     } else {
694c896fe29Sbellard         if (ts->val_type == TEMP_VAL_CONST) {
695c896fe29Sbellard             snprintf(buf, buf_size, "$0x%" TCG_PRIlx , ts->val);
696c896fe29Sbellard         } else {
697ac56dd48Spbrook             snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
698c896fe29Sbellard         }
699c896fe29Sbellard     }
700c896fe29Sbellard     return buf;
701c896fe29Sbellard }
702c896fe29Sbellard 
703ac56dd48Spbrook char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg)
704ac56dd48Spbrook {
705ac56dd48Spbrook     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV(arg));
706ac56dd48Spbrook }
707ac56dd48Spbrook 
708c896fe29Sbellard void tcg_dump_ops(TCGContext *s, FILE *outfile)
709c896fe29Sbellard {
710c896fe29Sbellard     const uint16_t *opc_ptr;
711c896fe29Sbellard     const TCGArg *args;
712c896fe29Sbellard     TCGArg arg;
713c896fe29Sbellard     int c, i, k, nb_oargs, nb_iargs, nb_cargs;
714c896fe29Sbellard     const TCGOpDef *def;
715c896fe29Sbellard     char buf[128];
716c896fe29Sbellard 
717c896fe29Sbellard     opc_ptr = gen_opc_buf;
718c896fe29Sbellard     args = gen_opparam_buf;
719c896fe29Sbellard     while (opc_ptr < gen_opc_ptr) {
720c896fe29Sbellard         c = *opc_ptr++;
721c896fe29Sbellard         def = &tcg_op_defs[c];
722c896fe29Sbellard         fprintf(outfile, " %s ", def->name);
723c896fe29Sbellard         if (c == INDEX_op_call) {
724c896fe29Sbellard             TCGArg arg;
725c896fe29Sbellard             /* variable number of arguments */
726c896fe29Sbellard             arg = *args++;
727c896fe29Sbellard             nb_oargs = arg >> 16;
728c896fe29Sbellard             nb_iargs = arg & 0xffff;
729c896fe29Sbellard             nb_cargs = def->nb_cargs;
730b03cce8eSbellard 
731b03cce8eSbellard             /* function name */
732b03cce8eSbellard             /* XXX: dump helper name for call */
733b03cce8eSbellard             fprintf(outfile, "%s",
734b03cce8eSbellard                     tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + nb_iargs - 1]));
735b03cce8eSbellard             /* flags */
736b03cce8eSbellard             fprintf(outfile, ",$0x%" TCG_PRIlx,
737b03cce8eSbellard                     args[nb_oargs + nb_iargs]);
738b03cce8eSbellard             /* nb out args */
739b03cce8eSbellard             fprintf(outfile, ",$%d", nb_oargs);
740b03cce8eSbellard             for(i = 0; i < nb_oargs; i++) {
741b03cce8eSbellard                 fprintf(outfile, ",");
742b03cce8eSbellard                 fprintf(outfile, "%s",
743b03cce8eSbellard                         tcg_get_arg_str_idx(s, buf, sizeof(buf), args[i]));
744b03cce8eSbellard             }
745b03cce8eSbellard             for(i = 0; i < (nb_iargs - 1); i++) {
746b03cce8eSbellard                 fprintf(outfile, ",");
747b03cce8eSbellard                 fprintf(outfile, "%s",
748b03cce8eSbellard                         tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + i]));
749b03cce8eSbellard             }
750b03cce8eSbellard         } else {
751b03cce8eSbellard             if (c == INDEX_op_nopn) {
752c896fe29Sbellard                 /* variable number of arguments */
753c896fe29Sbellard                 nb_cargs = *args;
754c896fe29Sbellard                 nb_oargs = 0;
755c896fe29Sbellard                 nb_iargs = 0;
756c896fe29Sbellard             } else {
757c896fe29Sbellard                 nb_oargs = def->nb_oargs;
758c896fe29Sbellard                 nb_iargs = def->nb_iargs;
759c896fe29Sbellard                 nb_cargs = def->nb_cargs;
760c896fe29Sbellard             }
761c896fe29Sbellard 
762c896fe29Sbellard             k = 0;
763c896fe29Sbellard             for(i = 0; i < nb_oargs; i++) {
764c896fe29Sbellard                 if (k != 0)
765c896fe29Sbellard                     fprintf(outfile, ",");
766ac56dd48Spbrook                 fprintf(outfile, "%s",
767ac56dd48Spbrook                         tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++]));
768c896fe29Sbellard             }
769c896fe29Sbellard             for(i = 0; i < nb_iargs; i++) {
770c896fe29Sbellard                 if (k != 0)
771c896fe29Sbellard                     fprintf(outfile, ",");
772ac56dd48Spbrook                 fprintf(outfile, "%s",
773ac56dd48Spbrook                         tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++]));
774c896fe29Sbellard             }
775c896fe29Sbellard             for(i = 0; i < nb_cargs; i++) {
776c896fe29Sbellard                 if (k != 0)
777c896fe29Sbellard                     fprintf(outfile, ",");
778c896fe29Sbellard                 arg = args[k++];
779c896fe29Sbellard                 fprintf(outfile, "$0x%" TCG_PRIlx, arg);
780c896fe29Sbellard             }
781b03cce8eSbellard         }
782c896fe29Sbellard         fprintf(outfile, "\n");
783c896fe29Sbellard         args += nb_iargs + nb_oargs + nb_cargs;
784c896fe29Sbellard     }
785c896fe29Sbellard }
786c896fe29Sbellard 
787c896fe29Sbellard /* we give more priority to constraints with less registers */
788c896fe29Sbellard static int get_constraint_priority(const TCGOpDef *def, int k)
789c896fe29Sbellard {
790c896fe29Sbellard     const TCGArgConstraint *arg_ct;
791c896fe29Sbellard 
792c896fe29Sbellard     int i, n;
793c896fe29Sbellard     arg_ct = &def->args_ct[k];
794c896fe29Sbellard     if (arg_ct->ct & TCG_CT_ALIAS) {
795c896fe29Sbellard         /* an alias is equivalent to a single register */
796c896fe29Sbellard         n = 1;
797c896fe29Sbellard     } else {
798c896fe29Sbellard         if (!(arg_ct->ct & TCG_CT_REG))
799c896fe29Sbellard             return 0;
800c896fe29Sbellard         n = 0;
801c896fe29Sbellard         for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
802c896fe29Sbellard             if (tcg_regset_test_reg(arg_ct->u.regs, i))
803c896fe29Sbellard                 n++;
804c896fe29Sbellard         }
805c896fe29Sbellard     }
806c896fe29Sbellard     return TCG_TARGET_NB_REGS - n + 1;
807c896fe29Sbellard }
808c896fe29Sbellard 
809c896fe29Sbellard /* sort from highest priority to lowest */
810c896fe29Sbellard static void sort_constraints(TCGOpDef *def, int start, int n)
811c896fe29Sbellard {
812c896fe29Sbellard     int i, j, p1, p2, tmp;
813c896fe29Sbellard 
814c896fe29Sbellard     for(i = 0; i < n; i++)
815c896fe29Sbellard         def->sorted_args[start + i] = start + i;
816c896fe29Sbellard     if (n <= 1)
817c896fe29Sbellard         return;
818c896fe29Sbellard     for(i = 0; i < n - 1; i++) {
819c896fe29Sbellard         for(j = i + 1; j < n; j++) {
820c896fe29Sbellard             p1 = get_constraint_priority(def, def->sorted_args[start + i]);
821c896fe29Sbellard             p2 = get_constraint_priority(def, def->sorted_args[start + j]);
822c896fe29Sbellard             if (p1 < p2) {
823c896fe29Sbellard                 tmp = def->sorted_args[start + i];
824c896fe29Sbellard                 def->sorted_args[start + i] = def->sorted_args[start + j];
825c896fe29Sbellard                 def->sorted_args[start + j] = tmp;
826c896fe29Sbellard             }
827c896fe29Sbellard         }
828c896fe29Sbellard     }
829c896fe29Sbellard }
830c896fe29Sbellard 
831c896fe29Sbellard void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
832c896fe29Sbellard {
833c896fe29Sbellard     int op;
834c896fe29Sbellard     TCGOpDef *def;
835c896fe29Sbellard     const char *ct_str;
836c896fe29Sbellard     int i, nb_args;
837c896fe29Sbellard 
838c896fe29Sbellard     for(;;) {
839c896fe29Sbellard         if (tdefs->op < 0)
840c896fe29Sbellard             break;
841c896fe29Sbellard         op = tdefs->op;
842c896fe29Sbellard         assert(op >= 0 && op < NB_OPS);
843c896fe29Sbellard         def = &tcg_op_defs[op];
844c896fe29Sbellard         nb_args = def->nb_iargs + def->nb_oargs;
845c896fe29Sbellard         for(i = 0; i < nb_args; i++) {
846c896fe29Sbellard             ct_str = tdefs->args_ct_str[i];
847c896fe29Sbellard             tcg_regset_clear(def->args_ct[i].u.regs);
848c896fe29Sbellard             def->args_ct[i].ct = 0;
849c896fe29Sbellard             if (ct_str[0] >= '0' && ct_str[0] <= '9') {
850c896fe29Sbellard                 int oarg;
851c896fe29Sbellard                 oarg = ct_str[0] - '0';
852c896fe29Sbellard                 assert(oarg < def->nb_oargs);
853c896fe29Sbellard                 assert(def->args_ct[oarg].ct & TCG_CT_REG);
854c896fe29Sbellard                 /* TCG_CT_ALIAS is for the output arguments. The input
8555ff9d6a4Sbellard                    argument is tagged with TCG_CT_IALIAS. */
856c896fe29Sbellard                 def->args_ct[i] = def->args_ct[oarg];
8575ff9d6a4Sbellard                 def->args_ct[oarg].ct = TCG_CT_ALIAS;
8585ff9d6a4Sbellard                 def->args_ct[oarg].alias_index = i;
859c896fe29Sbellard                 def->args_ct[i].ct |= TCG_CT_IALIAS;
8605ff9d6a4Sbellard                 def->args_ct[i].alias_index = oarg;
861c896fe29Sbellard             } else {
862c896fe29Sbellard                 for(;;) {
863c896fe29Sbellard                     if (*ct_str == '\0')
864c896fe29Sbellard                         break;
865c896fe29Sbellard                     switch(*ct_str) {
866c896fe29Sbellard                     case 'i':
867c896fe29Sbellard                         def->args_ct[i].ct |= TCG_CT_CONST;
868c896fe29Sbellard                         ct_str++;
869c896fe29Sbellard                         break;
870c896fe29Sbellard                     default:
871c896fe29Sbellard                         if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
872c896fe29Sbellard                             fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
873c896fe29Sbellard                                     ct_str, i, def->name);
874c896fe29Sbellard                             exit(1);
875c896fe29Sbellard                         }
876c896fe29Sbellard                     }
877c896fe29Sbellard                 }
878c896fe29Sbellard             }
879c896fe29Sbellard         }
880c896fe29Sbellard 
881c896fe29Sbellard         /* sort the constraints (XXX: this is just an heuristic) */
882c896fe29Sbellard         sort_constraints(def, 0, def->nb_oargs);
883c896fe29Sbellard         sort_constraints(def, def->nb_oargs, def->nb_iargs);
884c896fe29Sbellard 
885c896fe29Sbellard #if 0
886c896fe29Sbellard         {
887c896fe29Sbellard             int i;
888c896fe29Sbellard 
889c896fe29Sbellard             printf("%s: sorted=", def->name);
890c896fe29Sbellard             for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
891c896fe29Sbellard                 printf(" %d", def->sorted_args[i]);
892c896fe29Sbellard             printf("\n");
893c896fe29Sbellard         }
894c896fe29Sbellard #endif
895c896fe29Sbellard         tdefs++;
896c896fe29Sbellard     }
897c896fe29Sbellard 
898c896fe29Sbellard }
899c896fe29Sbellard 
900c896fe29Sbellard #ifdef USE_LIVENESS_ANALYSIS
901c896fe29Sbellard 
902c896fe29Sbellard /* set a nop for an operation using 'nb_args' */
903c896fe29Sbellard static inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr,
904c896fe29Sbellard                                TCGArg *args, int nb_args)
905c896fe29Sbellard {
906c896fe29Sbellard     if (nb_args == 0) {
907c896fe29Sbellard         *opc_ptr = INDEX_op_nop;
908c896fe29Sbellard     } else {
909c896fe29Sbellard         *opc_ptr = INDEX_op_nopn;
910c896fe29Sbellard         args[0] = nb_args;
911c896fe29Sbellard         args[nb_args - 1] = nb_args;
912c896fe29Sbellard     }
913c896fe29Sbellard }
914c896fe29Sbellard 
915c896fe29Sbellard /* liveness analysis: end of basic block: globals are live, temps are dead */
916c896fe29Sbellard static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps)
917c896fe29Sbellard {
918c896fe29Sbellard     memset(dead_temps, 0, s->nb_globals);
919c896fe29Sbellard     memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals);
920c896fe29Sbellard }
921c896fe29Sbellard 
922c896fe29Sbellard /* Liveness analysis : update the opc_dead_iargs array to tell if a
923c896fe29Sbellard    given input arguments is dead. Instructions updating dead
924c896fe29Sbellard    temporaries are removed. */
925c896fe29Sbellard void tcg_liveness_analysis(TCGContext *s)
926c896fe29Sbellard {
927c896fe29Sbellard     int i, op_index, op, nb_args, nb_iargs, nb_oargs, arg, nb_ops;
928c896fe29Sbellard     TCGArg *args;
929c896fe29Sbellard     const TCGOpDef *def;
930c896fe29Sbellard     uint8_t *dead_temps;
931c896fe29Sbellard     unsigned int dead_iargs;
932c896fe29Sbellard 
933c896fe29Sbellard     gen_opc_ptr++; /* skip end */
934c896fe29Sbellard 
935c896fe29Sbellard     nb_ops = gen_opc_ptr - gen_opc_buf;
936c896fe29Sbellard 
937c896fe29Sbellard     /* XXX: make it really dynamic */
938c896fe29Sbellard     s->op_dead_iargs = tcg_malloc(OPC_BUF_SIZE * sizeof(uint16_t));
939c896fe29Sbellard 
940c896fe29Sbellard     dead_temps = tcg_malloc(s->nb_temps);
941c896fe29Sbellard     memset(dead_temps, 1, s->nb_temps);
942c896fe29Sbellard 
943c896fe29Sbellard     args = gen_opparam_ptr;
944c896fe29Sbellard     op_index = nb_ops - 1;
945c896fe29Sbellard     while (op_index >= 0) {
946c896fe29Sbellard         op = gen_opc_buf[op_index];
947c896fe29Sbellard         def = &tcg_op_defs[op];
948c896fe29Sbellard         switch(op) {
949c896fe29Sbellard         case INDEX_op_call:
950c6e113f5Sbellard             {
951c6e113f5Sbellard                 int call_flags;
952c6e113f5Sbellard 
953c896fe29Sbellard                 nb_args = args[-1];
954c896fe29Sbellard                 args -= nb_args;
955c896fe29Sbellard                 nb_iargs = args[0] & 0xffff;
956c896fe29Sbellard                 nb_oargs = args[0] >> 16;
957c896fe29Sbellard                 args++;
958c6e113f5Sbellard                 call_flags = args[nb_oargs + nb_iargs];
959c6e113f5Sbellard 
960c6e113f5Sbellard                 /* pure functions can be removed if their result is not
961c6e113f5Sbellard                    used */
962c6e113f5Sbellard                 if (call_flags & TCG_CALL_PURE) {
963c6e113f5Sbellard                     for(i = 0; i < nb_oargs; i++) {
964c6e113f5Sbellard                         arg = args[i];
965c6e113f5Sbellard                         if (!dead_temps[arg])
966c6e113f5Sbellard                             goto do_not_remove_call;
967c6e113f5Sbellard                     }
968c6e113f5Sbellard                     tcg_set_nop(s, gen_opc_buf + op_index,
969c6e113f5Sbellard                                 args - 1, nb_args);
970c6e113f5Sbellard                 } else {
971c6e113f5Sbellard                 do_not_remove_call:
972c896fe29Sbellard 
973c896fe29Sbellard                     /* output args are dead */
974c896fe29Sbellard                     for(i = 0; i < nb_oargs; i++) {
975c896fe29Sbellard                         arg = args[i];
976c896fe29Sbellard                         dead_temps[arg] = 1;
977c896fe29Sbellard                     }
978c896fe29Sbellard 
979c896fe29Sbellard                     /* globals are live (they may be used by the call) */
980c896fe29Sbellard                     memset(dead_temps, 0, s->nb_globals);
981c896fe29Sbellard 
982c896fe29Sbellard                     /* input args are live */
983c896fe29Sbellard                     dead_iargs = 0;
984c896fe29Sbellard                     for(i = 0; i < nb_iargs; i++) {
985c896fe29Sbellard                         arg = args[i + nb_oargs];
986c896fe29Sbellard                         if (dead_temps[arg]) {
987c896fe29Sbellard                             dead_iargs |= (1 << i);
988c896fe29Sbellard                         }
989c896fe29Sbellard                         dead_temps[arg] = 0;
990c896fe29Sbellard                     }
991c896fe29Sbellard                     s->op_dead_iargs[op_index] = dead_iargs;
992c6e113f5Sbellard                 }
993c896fe29Sbellard                 args--;
994c6e113f5Sbellard             }
995c896fe29Sbellard             break;
996c896fe29Sbellard         case INDEX_op_set_label:
997c896fe29Sbellard             args--;
998c896fe29Sbellard             /* mark end of basic block */
999c896fe29Sbellard             tcg_la_bb_end(s, dead_temps);
1000c896fe29Sbellard             break;
1001c896fe29Sbellard         case INDEX_op_nopn:
1002c896fe29Sbellard             nb_args = args[-1];
1003c896fe29Sbellard             args -= nb_args;
1004c896fe29Sbellard             break;
10055ff9d6a4Sbellard         case INDEX_op_discard:
10065ff9d6a4Sbellard             args--;
10075ff9d6a4Sbellard             /* mark the temporary as dead */
10085ff9d6a4Sbellard             dead_temps[args[0]] = 1;
10095ff9d6a4Sbellard             break;
1010c896fe29Sbellard         case INDEX_op_macro_2:
1011c896fe29Sbellard             {
1012c896fe29Sbellard                 int dead_args[2], macro_id;
1013c896fe29Sbellard                 int saved_op_index, saved_arg_index;
1014c896fe29Sbellard                 int macro_op_index, macro_arg_index;
1015c896fe29Sbellard                 int macro_end_op_index, macro_end_arg_index;
1016c896fe29Sbellard                 int last_nb_temps;
1017c896fe29Sbellard 
1018c896fe29Sbellard                 nb_args = 3;
1019c896fe29Sbellard                 args -= nb_args;
1020c896fe29Sbellard                 dead_args[0] = dead_temps[args[0]];
1021c896fe29Sbellard                 dead_args[1] = dead_temps[args[1]];
1022c896fe29Sbellard                 macro_id = args[2];
1023c896fe29Sbellard 
1024c896fe29Sbellard                 /* call the macro function which generate code
1025c896fe29Sbellard                    depending on the live outputs */
1026c896fe29Sbellard                 saved_op_index = op_index;
1027c896fe29Sbellard                 saved_arg_index = args - gen_opparam_buf;
1028c896fe29Sbellard 
1029c896fe29Sbellard                 /* add a macro start instruction */
1030c896fe29Sbellard                 *gen_opc_ptr++ = INDEX_op_macro_start;
1031c896fe29Sbellard                 *gen_opparam_ptr++ = saved_op_index;
1032c896fe29Sbellard                 *gen_opparam_ptr++ = saved_arg_index;
1033c896fe29Sbellard 
1034c896fe29Sbellard                 macro_op_index = gen_opc_ptr - gen_opc_buf;
1035c896fe29Sbellard                 macro_arg_index = gen_opparam_ptr -  gen_opparam_buf;
1036c896fe29Sbellard 
1037c896fe29Sbellard                 last_nb_temps = s->nb_temps;
1038c896fe29Sbellard 
1039c896fe29Sbellard                 s->macro_func(s, macro_id, dead_args);
1040c896fe29Sbellard 
1041c896fe29Sbellard                 /* realloc temp info (XXX: make it faster) */
1042c896fe29Sbellard                 if (s->nb_temps > last_nb_temps) {
1043c896fe29Sbellard                     uint8_t *new_dead_temps;
1044c896fe29Sbellard 
1045c896fe29Sbellard                     new_dead_temps = tcg_malloc(s->nb_temps);
1046c896fe29Sbellard                     memcpy(new_dead_temps, dead_temps, last_nb_temps);
1047c896fe29Sbellard                     memset(new_dead_temps + last_nb_temps, 1,
1048c896fe29Sbellard                            s->nb_temps - last_nb_temps);
1049c896fe29Sbellard                     dead_temps = new_dead_temps;
1050c896fe29Sbellard                 }
1051c896fe29Sbellard 
1052c896fe29Sbellard                 macro_end_op_index = gen_opc_ptr - gen_opc_buf;
1053c896fe29Sbellard                 macro_end_arg_index = gen_opparam_ptr - gen_opparam_buf;
1054c896fe29Sbellard 
1055c896fe29Sbellard                 /* end of macro: add a goto to the next instruction */
1056c896fe29Sbellard                 *gen_opc_ptr++ = INDEX_op_macro_end;
1057c896fe29Sbellard                 *gen_opparam_ptr++ = op_index + 1;
1058c896fe29Sbellard                 *gen_opparam_ptr++ = saved_arg_index + nb_args;
1059c896fe29Sbellard 
1060c896fe29Sbellard                 /* modify the macro operation to be a macro_goto */
1061c896fe29Sbellard                 gen_opc_buf[op_index] = INDEX_op_macro_goto;
1062c896fe29Sbellard                 args[0] = macro_op_index;
1063c896fe29Sbellard                 args[1] = macro_arg_index;
1064c896fe29Sbellard                 args[2] = 0; /* dummy third arg to match the
1065c896fe29Sbellard                                 macro parameters */
1066c896fe29Sbellard 
1067c896fe29Sbellard                 /* set the next instruction to the end of the macro */
1068c896fe29Sbellard                 op_index = macro_end_op_index;
1069c896fe29Sbellard                 args = macro_end_arg_index + gen_opparam_buf;
1070c896fe29Sbellard             }
1071c896fe29Sbellard             break;
1072c896fe29Sbellard         case INDEX_op_macro_start:
1073c896fe29Sbellard             args -= 2;
1074c896fe29Sbellard             op_index = args[0];
1075c896fe29Sbellard             args = gen_opparam_buf + args[1];
1076c896fe29Sbellard             break;
1077c896fe29Sbellard         case INDEX_op_macro_goto:
1078c896fe29Sbellard         case INDEX_op_macro_end:
1079c896fe29Sbellard             tcg_abort(); /* should never happen in liveness analysis */
1080c896fe29Sbellard         case INDEX_op_end:
1081c896fe29Sbellard             break;
1082c896fe29Sbellard             /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1083c896fe29Sbellard         default:
1084c896fe29Sbellard             if (op > INDEX_op_end) {
1085c896fe29Sbellard                 args -= def->nb_args;
1086c896fe29Sbellard                 nb_iargs = def->nb_iargs;
1087c896fe29Sbellard                 nb_oargs = def->nb_oargs;
1088c896fe29Sbellard 
1089c896fe29Sbellard                 /* Test if the operation can be removed because all
10905ff9d6a4Sbellard                    its outputs are dead. We assume that nb_oargs == 0
10915ff9d6a4Sbellard                    implies side effects */
10925ff9d6a4Sbellard                 if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
1093c896fe29Sbellard                     for(i = 0; i < nb_oargs; i++) {
1094c896fe29Sbellard                         arg = args[i];
1095c896fe29Sbellard                         if (!dead_temps[arg])
1096c896fe29Sbellard                             goto do_not_remove;
1097c896fe29Sbellard                     }
1098c896fe29Sbellard                     tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args);
1099c896fe29Sbellard #ifdef CONFIG_PROFILER
1100c896fe29Sbellard                     {
1101c896fe29Sbellard                         extern int64_t dyngen_tcg_del_op_count;
1102c896fe29Sbellard                         dyngen_tcg_del_op_count++;
1103c896fe29Sbellard                     }
1104c896fe29Sbellard #endif
1105c896fe29Sbellard                 } else {
1106c896fe29Sbellard                 do_not_remove:
1107c896fe29Sbellard 
1108c896fe29Sbellard                     /* output args are dead */
1109c896fe29Sbellard                     for(i = 0; i < nb_oargs; i++) {
1110c896fe29Sbellard                         arg = args[i];
1111c896fe29Sbellard                         dead_temps[arg] = 1;
1112c896fe29Sbellard                     }
1113c896fe29Sbellard 
1114c896fe29Sbellard                     /* if end of basic block, update */
1115c896fe29Sbellard                     if (def->flags & TCG_OPF_BB_END) {
1116c896fe29Sbellard                         tcg_la_bb_end(s, dead_temps);
1117b03cce8eSbellard                     } else if (def->flags & TCG_OPF_CALL_CLOBBER) {
1118b03cce8eSbellard                         /* globals are live */
1119b03cce8eSbellard                         memset(dead_temps, 0, s->nb_globals);
1120c896fe29Sbellard                     }
1121c896fe29Sbellard 
1122c896fe29Sbellard                     /* input args are live */
1123c896fe29Sbellard                     dead_iargs = 0;
1124c896fe29Sbellard                     for(i = 0; i < nb_iargs; i++) {
1125c896fe29Sbellard                         arg = args[i + nb_oargs];
1126c896fe29Sbellard                         if (dead_temps[arg]) {
1127c896fe29Sbellard                             dead_iargs |= (1 << i);
1128c896fe29Sbellard                         }
1129c896fe29Sbellard                         dead_temps[arg] = 0;
1130c896fe29Sbellard                     }
1131c896fe29Sbellard                     s->op_dead_iargs[op_index] = dead_iargs;
1132c896fe29Sbellard                 }
1133c896fe29Sbellard             } else {
1134c896fe29Sbellard                 /* legacy dyngen operations */
1135c896fe29Sbellard                 args -= def->nb_args;
1136c896fe29Sbellard                 /* mark end of basic block */
1137c896fe29Sbellard                 tcg_la_bb_end(s, dead_temps);
1138c896fe29Sbellard             }
1139c896fe29Sbellard             break;
1140c896fe29Sbellard         }
1141c896fe29Sbellard         op_index--;
1142c896fe29Sbellard     }
1143c896fe29Sbellard 
1144c896fe29Sbellard     if (args != gen_opparam_buf)
1145c896fe29Sbellard         tcg_abort();
1146c896fe29Sbellard }
1147c896fe29Sbellard #else
1148c896fe29Sbellard /* dummy liveness analysis */
1149c896fe29Sbellard void tcg_liveness_analysis(TCGContext *s)
1150c896fe29Sbellard {
1151c896fe29Sbellard     int nb_ops;
1152c896fe29Sbellard     nb_ops = gen_opc_ptr - gen_opc_buf;
1153c896fe29Sbellard 
1154c896fe29Sbellard     s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t));
1155c896fe29Sbellard     memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t));
1156c896fe29Sbellard }
1157c896fe29Sbellard #endif
1158c896fe29Sbellard 
1159c896fe29Sbellard #ifndef NDEBUG
1160c896fe29Sbellard static void dump_regs(TCGContext *s)
1161c896fe29Sbellard {
1162c896fe29Sbellard     TCGTemp *ts;
1163c896fe29Sbellard     int i;
1164c896fe29Sbellard     char buf[64];
1165c896fe29Sbellard 
1166c896fe29Sbellard     for(i = 0; i < s->nb_temps; i++) {
1167c896fe29Sbellard         ts = &s->temps[i];
1168ac56dd48Spbrook         printf("  %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
1169c896fe29Sbellard         switch(ts->val_type) {
1170c896fe29Sbellard         case TEMP_VAL_REG:
1171c896fe29Sbellard             printf("%s", tcg_target_reg_names[ts->reg]);
1172c896fe29Sbellard             break;
1173c896fe29Sbellard         case TEMP_VAL_MEM:
1174c896fe29Sbellard             printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
1175c896fe29Sbellard             break;
1176c896fe29Sbellard         case TEMP_VAL_CONST:
1177c896fe29Sbellard             printf("$0x%" TCG_PRIlx, ts->val);
1178c896fe29Sbellard             break;
1179c896fe29Sbellard         case TEMP_VAL_DEAD:
1180c896fe29Sbellard             printf("D");
1181c896fe29Sbellard             break;
1182c896fe29Sbellard         default:
1183c896fe29Sbellard             printf("???");
1184c896fe29Sbellard             break;
1185c896fe29Sbellard         }
1186c896fe29Sbellard         printf("\n");
1187c896fe29Sbellard     }
1188c896fe29Sbellard 
1189c896fe29Sbellard     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1190c896fe29Sbellard         if (s->reg_to_temp[i] >= 0) {
1191c896fe29Sbellard             printf("%s: %s\n",
1192c896fe29Sbellard                    tcg_target_reg_names[i],
1193ac56dd48Spbrook                    tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i]));
1194c896fe29Sbellard         }
1195c896fe29Sbellard     }
1196c896fe29Sbellard }
1197c896fe29Sbellard 
1198c896fe29Sbellard static void check_regs(TCGContext *s)
1199c896fe29Sbellard {
1200c896fe29Sbellard     int reg, k;
1201c896fe29Sbellard     TCGTemp *ts;
1202c896fe29Sbellard     char buf[64];
1203c896fe29Sbellard 
1204c896fe29Sbellard     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1205c896fe29Sbellard         k = s->reg_to_temp[reg];
1206c896fe29Sbellard         if (k >= 0) {
1207c896fe29Sbellard             ts = &s->temps[k];
1208c896fe29Sbellard             if (ts->val_type != TEMP_VAL_REG ||
1209c896fe29Sbellard                 ts->reg != reg) {
1210c896fe29Sbellard                 printf("Inconsistency for register %s:\n",
1211c896fe29Sbellard                        tcg_target_reg_names[reg]);
1212b03cce8eSbellard                 goto fail;
1213c896fe29Sbellard             }
1214c896fe29Sbellard         }
1215c896fe29Sbellard     }
1216c896fe29Sbellard     for(k = 0; k < s->nb_temps; k++) {
1217c896fe29Sbellard         ts = &s->temps[k];
1218c896fe29Sbellard         if (ts->val_type == TEMP_VAL_REG &&
1219c896fe29Sbellard             !ts->fixed_reg &&
1220c896fe29Sbellard             s->reg_to_temp[ts->reg] != k) {
1221c896fe29Sbellard                 printf("Inconsistency for temp %s:\n",
1222ac56dd48Spbrook                        tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
1223b03cce8eSbellard         fail:
1224c896fe29Sbellard                 printf("reg state:\n");
1225c896fe29Sbellard                 dump_regs(s);
1226c896fe29Sbellard                 tcg_abort();
1227c896fe29Sbellard         }
1228b03cce8eSbellard         if (ts->val_type == TEMP_VAL_CONST && k < s->nb_globals) {
1229b03cce8eSbellard             printf("constant forbidden in global %s\n",
1230b03cce8eSbellard                    tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
1231b03cce8eSbellard             goto fail;
1232b03cce8eSbellard         }
1233c896fe29Sbellard     }
1234c896fe29Sbellard }
1235c896fe29Sbellard #endif
1236c896fe29Sbellard 
1237c896fe29Sbellard static void temp_allocate_frame(TCGContext *s, int temp)
1238c896fe29Sbellard {
1239c896fe29Sbellard     TCGTemp *ts;
1240c896fe29Sbellard     ts = &s->temps[temp];
1241c896fe29Sbellard     s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1);
1242c896fe29Sbellard     if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end)
12435ff9d6a4Sbellard         tcg_abort();
1244c896fe29Sbellard     ts->mem_offset = s->current_frame_offset;
1245c896fe29Sbellard     ts->mem_reg = s->frame_reg;
1246c896fe29Sbellard     ts->mem_allocated = 1;
1247c896fe29Sbellard     s->current_frame_offset += sizeof(tcg_target_long);
1248c896fe29Sbellard }
1249c896fe29Sbellard 
1250c896fe29Sbellard /* free register 'reg' by spilling the corresponding temporary if necessary */
1251c896fe29Sbellard static void tcg_reg_free(TCGContext *s, int reg)
1252c896fe29Sbellard {
1253c896fe29Sbellard     TCGTemp *ts;
1254c896fe29Sbellard     int temp;
1255c896fe29Sbellard 
1256c896fe29Sbellard     temp = s->reg_to_temp[reg];
1257c896fe29Sbellard     if (temp != -1) {
1258c896fe29Sbellard         ts = &s->temps[temp];
1259c896fe29Sbellard         assert(ts->val_type == TEMP_VAL_REG);
1260c896fe29Sbellard         if (!ts->mem_coherent) {
1261c896fe29Sbellard             if (!ts->mem_allocated)
1262c896fe29Sbellard                 temp_allocate_frame(s, temp);
1263e4d5434cSblueswir1             tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1264c896fe29Sbellard         }
1265c896fe29Sbellard         ts->val_type = TEMP_VAL_MEM;
1266c896fe29Sbellard         s->reg_to_temp[reg] = -1;
1267c896fe29Sbellard     }
1268c896fe29Sbellard }
1269c896fe29Sbellard 
1270c896fe29Sbellard /* Allocate a register belonging to reg1 & ~reg2 */
1271c896fe29Sbellard static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
1272c896fe29Sbellard {
1273c896fe29Sbellard     int i, reg;
1274c896fe29Sbellard     TCGRegSet reg_ct;
1275c896fe29Sbellard 
1276c896fe29Sbellard     tcg_regset_andnot(reg_ct, reg1, reg2);
1277c896fe29Sbellard 
1278c896fe29Sbellard     /* first try free registers */
12790954d0d9Sblueswir1     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1280c896fe29Sbellard         reg = tcg_target_reg_alloc_order[i];
1281c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
1282c896fe29Sbellard             return reg;
1283c896fe29Sbellard     }
1284c896fe29Sbellard 
1285c896fe29Sbellard     /* XXX: do better spill choice */
12860954d0d9Sblueswir1     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1287c896fe29Sbellard         reg = tcg_target_reg_alloc_order[i];
1288c896fe29Sbellard         if (tcg_regset_test_reg(reg_ct, reg)) {
1289c896fe29Sbellard             tcg_reg_free(s, reg);
1290c896fe29Sbellard             return reg;
1291c896fe29Sbellard         }
1292c896fe29Sbellard     }
1293c896fe29Sbellard 
1294c896fe29Sbellard     tcg_abort();
1295c896fe29Sbellard }
1296c896fe29Sbellard 
1297e5097dc8Sbellard /* save globals to their cannonical location and assume they can be
1298e5097dc8Sbellard    modified be the following code. */
1299e5097dc8Sbellard static void save_globals(TCGContext *s)
1300c896fe29Sbellard {
1301c896fe29Sbellard     TCGTemp *ts;
1302c896fe29Sbellard     int i;
1303c896fe29Sbellard 
1304c896fe29Sbellard     for(i = 0; i < s->nb_globals; i++) {
1305c896fe29Sbellard         ts = &s->temps[i];
1306c896fe29Sbellard         if (!ts->fixed_reg) {
1307c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
1308c896fe29Sbellard                 tcg_reg_free(s, ts->reg);
1309e5097dc8Sbellard             } else if (ts->val_type == TEMP_VAL_DEAD) {
1310e5097dc8Sbellard                 ts->val_type = TEMP_VAL_MEM;
1311c896fe29Sbellard             }
1312c896fe29Sbellard         }
1313c896fe29Sbellard     }
1314e5097dc8Sbellard }
1315e5097dc8Sbellard 
1316e5097dc8Sbellard /* at the end of a basic block, we assume all temporaries are dead and
1317e5097dc8Sbellard    all globals are stored at their canonical location */
1318e5097dc8Sbellard /* XXX: optimize by handling constants in another array ? */
1319e5097dc8Sbellard void tcg_reg_alloc_bb_end(TCGContext *s)
1320e5097dc8Sbellard {
1321e5097dc8Sbellard     TCGTemp *ts;
1322e5097dc8Sbellard     int i;
1323e5097dc8Sbellard 
1324e5097dc8Sbellard     save_globals(s);
1325c896fe29Sbellard 
1326c896fe29Sbellard     for(i = s->nb_globals; i < s->nb_temps; i++) {
1327c896fe29Sbellard         ts = &s->temps[i];
1328c896fe29Sbellard         if (ts->val_type != TEMP_VAL_CONST) {
1329c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG) {
1330c896fe29Sbellard                 s->reg_to_temp[ts->reg] = -1;
1331c896fe29Sbellard             }
1332c896fe29Sbellard             ts->val_type = TEMP_VAL_DEAD;
1333c896fe29Sbellard         }
1334c896fe29Sbellard     }
1335c896fe29Sbellard }
1336c896fe29Sbellard 
1337c896fe29Sbellard #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1)
1338c896fe29Sbellard 
1339c896fe29Sbellard static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1340c896fe29Sbellard                               const TCGArg *args,
1341c896fe29Sbellard                               unsigned int dead_iargs)
1342c896fe29Sbellard {
1343c896fe29Sbellard     TCGTemp *ts, *ots;
1344c896fe29Sbellard     int reg;
1345c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1346c896fe29Sbellard 
1347c896fe29Sbellard     ots = &s->temps[args[0]];
1348c896fe29Sbellard     ts = &s->temps[args[1]];
1349c896fe29Sbellard     arg_ct = &def->args_ct[0];
1350c896fe29Sbellard 
1351c896fe29Sbellard     if (ts->val_type == TEMP_VAL_REG) {
1352c896fe29Sbellard         if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) {
1353c896fe29Sbellard             /* the mov can be suppressed */
1354c896fe29Sbellard             if (ots->val_type == TEMP_VAL_REG)
1355c896fe29Sbellard                 s->reg_to_temp[ots->reg] = -1;
1356c896fe29Sbellard             reg = ts->reg;
1357c896fe29Sbellard             s->reg_to_temp[reg] = -1;
1358c896fe29Sbellard             ts->val_type = TEMP_VAL_DEAD;
1359c896fe29Sbellard         } else {
1360c896fe29Sbellard             if (ots->val_type == TEMP_VAL_REG) {
1361c896fe29Sbellard                 reg = ots->reg;
1362c896fe29Sbellard             } else {
1363c896fe29Sbellard                 reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1364c896fe29Sbellard             }
1365c896fe29Sbellard             if (ts->reg != reg) {
1366c896fe29Sbellard                 tcg_out_mov(s, reg, ts->reg);
1367c896fe29Sbellard             }
1368c896fe29Sbellard         }
1369c896fe29Sbellard     } else if (ts->val_type == TEMP_VAL_MEM) {
1370c896fe29Sbellard         if (ots->val_type == TEMP_VAL_REG) {
1371c896fe29Sbellard             reg = ots->reg;
1372c896fe29Sbellard         } else {
1373c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1374c896fe29Sbellard         }
1375e4d5434cSblueswir1         tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1376c896fe29Sbellard     } else if (ts->val_type == TEMP_VAL_CONST) {
1377c896fe29Sbellard         if (ots->val_type == TEMP_VAL_REG) {
1378c896fe29Sbellard             reg = ots->reg;
1379c896fe29Sbellard         } else {
1380c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
1381c896fe29Sbellard         }
1382c896fe29Sbellard         tcg_out_movi(s, ots->type, reg, ts->val);
1383c896fe29Sbellard     } else {
1384c896fe29Sbellard         tcg_abort();
1385c896fe29Sbellard     }
1386c896fe29Sbellard     s->reg_to_temp[reg] = args[0];
1387c896fe29Sbellard     ots->reg = reg;
1388c896fe29Sbellard     ots->val_type = TEMP_VAL_REG;
1389c896fe29Sbellard     ots->mem_coherent = 0;
1390c896fe29Sbellard }
1391c896fe29Sbellard 
1392c896fe29Sbellard static void tcg_reg_alloc_op(TCGContext *s,
1393c896fe29Sbellard                              const TCGOpDef *def, int opc,
1394c896fe29Sbellard                              const TCGArg *args,
1395c896fe29Sbellard                              unsigned int dead_iargs)
1396c896fe29Sbellard {
1397c896fe29Sbellard     TCGRegSet allocated_regs;
1398c896fe29Sbellard     int i, k, nb_iargs, nb_oargs, reg;
1399c896fe29Sbellard     TCGArg arg;
1400c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1401c896fe29Sbellard     TCGTemp *ts;
1402c896fe29Sbellard     TCGArg new_args[TCG_MAX_OP_ARGS];
1403c896fe29Sbellard     int const_args[TCG_MAX_OP_ARGS];
1404c896fe29Sbellard 
1405c896fe29Sbellard     nb_oargs = def->nb_oargs;
1406c896fe29Sbellard     nb_iargs = def->nb_iargs;
1407c896fe29Sbellard 
1408c896fe29Sbellard     /* copy constants */
1409c896fe29Sbellard     memcpy(new_args + nb_oargs + nb_iargs,
1410c896fe29Sbellard            args + nb_oargs + nb_iargs,
1411c896fe29Sbellard            sizeof(TCGArg) * def->nb_cargs);
1412c896fe29Sbellard 
1413c896fe29Sbellard     /* satisfy input constraints */
1414c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
1415c896fe29Sbellard     for(k = 0; k < nb_iargs; k++) {
1416c896fe29Sbellard         i = def->sorted_args[nb_oargs + k];
1417c896fe29Sbellard         arg = args[i];
1418c896fe29Sbellard         arg_ct = &def->args_ct[i];
1419c896fe29Sbellard         ts = &s->temps[arg];
1420c896fe29Sbellard         if (ts->val_type == TEMP_VAL_MEM) {
1421c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1422e4d5434cSblueswir1             tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1423c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
1424c896fe29Sbellard             ts->reg = reg;
1425c896fe29Sbellard             ts->mem_coherent = 1;
1426c896fe29Sbellard             s->reg_to_temp[reg] = arg;
1427c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1428c896fe29Sbellard             if (tcg_target_const_match(ts->val, arg_ct)) {
1429c896fe29Sbellard                 /* constant is OK for instruction */
1430c896fe29Sbellard                 const_args[i] = 1;
1431c896fe29Sbellard                 new_args[i] = ts->val;
1432c896fe29Sbellard                 goto iarg_end;
1433c896fe29Sbellard             } else {
1434c896fe29Sbellard                 /* need to move to a register*/
1435c896fe29Sbellard                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1436c896fe29Sbellard                 tcg_out_movi(s, ts->type, reg, ts->val);
1437c896fe29Sbellard                 goto iarg_end1;
1438c896fe29Sbellard             }
1439c896fe29Sbellard         }
1440c896fe29Sbellard         assert(ts->val_type == TEMP_VAL_REG);
14415ff9d6a4Sbellard         if (arg_ct->ct & TCG_CT_IALIAS) {
14425ff9d6a4Sbellard             if (ts->fixed_reg) {
14435ff9d6a4Sbellard                 /* if fixed register, we must allocate a new register
14445ff9d6a4Sbellard                    if the alias is not the same register */
14455ff9d6a4Sbellard                 if (arg != args[arg_ct->alias_index])
14465ff9d6a4Sbellard                     goto allocate_in_reg;
14475ff9d6a4Sbellard             } else {
1448c896fe29Sbellard                 /* if the input is aliased to an output and if it is
1449c896fe29Sbellard                    not dead after the instruction, we must allocate
1450c896fe29Sbellard                    a new register and move it */
14515ff9d6a4Sbellard                 if (!IS_DEAD_IARG(i - nb_oargs))
1452c896fe29Sbellard                     goto allocate_in_reg;
1453c896fe29Sbellard             }
14545ff9d6a4Sbellard         }
1455c896fe29Sbellard         reg = ts->reg;
1456c896fe29Sbellard         if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1457c896fe29Sbellard             /* nothing to do : the constraint is satisfied */
1458c896fe29Sbellard         } else {
1459c896fe29Sbellard         allocate_in_reg:
1460c896fe29Sbellard             /* allocate a new register matching the constraint
1461c896fe29Sbellard                and move the temporary register into it */
1462c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1463c896fe29Sbellard             tcg_out_mov(s, reg, ts->reg);
1464c896fe29Sbellard         }
1465c896fe29Sbellard     iarg_end1:
1466c896fe29Sbellard         new_args[i] = reg;
1467c896fe29Sbellard         const_args[i] = 0;
1468c896fe29Sbellard         tcg_regset_set_reg(allocated_regs, reg);
1469c896fe29Sbellard     iarg_end: ;
1470c896fe29Sbellard     }
1471c896fe29Sbellard 
1472c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
1473c896fe29Sbellard     for(i = 0; i < nb_iargs; i++) {
1474c896fe29Sbellard         arg = args[nb_oargs + i];
1475c896fe29Sbellard         if (IS_DEAD_IARG(i)) {
1476c896fe29Sbellard             ts = &s->temps[arg];
1477c896fe29Sbellard             if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
1478c896fe29Sbellard                 if (ts->val_type == TEMP_VAL_REG)
1479c896fe29Sbellard                     s->reg_to_temp[ts->reg] = -1;
1480c896fe29Sbellard                 ts->val_type = TEMP_VAL_DEAD;
1481c896fe29Sbellard             }
1482c896fe29Sbellard         }
1483c896fe29Sbellard     }
1484c896fe29Sbellard 
1485c896fe29Sbellard     if (def->flags & TCG_OPF_CALL_CLOBBER) {
1486b03cce8eSbellard         /* XXX: permit generic clobber register list ? */
1487c896fe29Sbellard         for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1488c896fe29Sbellard             if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1489c896fe29Sbellard                 tcg_reg_free(s, reg);
1490c896fe29Sbellard             }
1491c896fe29Sbellard         }
1492b03cce8eSbellard         /* XXX: for load/store we could do that only for the slow path
1493b03cce8eSbellard            (i.e. when a memory callback is called) */
1494b03cce8eSbellard 
1495b03cce8eSbellard         /* store globals and free associated registers (we assume the insn
1496b03cce8eSbellard            can modify any global. */
1497e5097dc8Sbellard         save_globals(s);
1498c896fe29Sbellard     }
1499c896fe29Sbellard 
1500c896fe29Sbellard     /* satisfy the output constraints */
1501c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
1502c896fe29Sbellard     for(k = 0; k < nb_oargs; k++) {
1503c896fe29Sbellard         i = def->sorted_args[k];
1504c896fe29Sbellard         arg = args[i];
1505c896fe29Sbellard         arg_ct = &def->args_ct[i];
1506c896fe29Sbellard         ts = &s->temps[arg];
1507c896fe29Sbellard         if (arg_ct->ct & TCG_CT_ALIAS) {
15085ff9d6a4Sbellard             reg = new_args[arg_ct->alias_index];
1509c896fe29Sbellard         } else {
1510c896fe29Sbellard             /* if fixed register, we try to use it */
1511c896fe29Sbellard             reg = ts->reg;
1512c896fe29Sbellard             if (ts->fixed_reg &&
1513c896fe29Sbellard                 tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1514c896fe29Sbellard                 goto oarg_end;
1515c896fe29Sbellard             }
1516c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1517c896fe29Sbellard         }
1518c896fe29Sbellard         tcg_regset_set_reg(allocated_regs, reg);
1519c896fe29Sbellard         /* if a fixed register is used, then a move will be done afterwards */
1520c896fe29Sbellard         if (!ts->fixed_reg) {
1521c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG)
1522c896fe29Sbellard                 s->reg_to_temp[ts->reg] = -1;
1523c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
1524c896fe29Sbellard             ts->reg = reg;
1525c896fe29Sbellard             /* temp value is modified, so the value kept in memory is
1526c896fe29Sbellard                potentially not the same */
1527c896fe29Sbellard             ts->mem_coherent = 0;
1528c896fe29Sbellard             s->reg_to_temp[reg] = arg;
1529c896fe29Sbellard         }
1530c896fe29Sbellard     oarg_end:
1531c896fe29Sbellard         new_args[i] = reg;
1532c896fe29Sbellard     }
1533c896fe29Sbellard 
1534c896fe29Sbellard     if (def->flags & TCG_OPF_BB_END)
1535c896fe29Sbellard         tcg_reg_alloc_bb_end(s);
1536c896fe29Sbellard 
1537c896fe29Sbellard     /* emit instruction */
1538c896fe29Sbellard     tcg_out_op(s, opc, new_args, const_args);
1539c896fe29Sbellard 
1540c896fe29Sbellard     /* move the outputs in the correct register if needed */
1541c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
1542c896fe29Sbellard         ts = &s->temps[args[i]];
1543c896fe29Sbellard         reg = new_args[i];
1544c896fe29Sbellard         if (ts->fixed_reg && ts->reg != reg) {
1545c896fe29Sbellard             tcg_out_mov(s, ts->reg, reg);
1546c896fe29Sbellard         }
1547c896fe29Sbellard     }
1548c896fe29Sbellard }
1549c896fe29Sbellard 
1550b03cce8eSbellard #ifdef TCG_TARGET_STACK_GROWSUP
1551b03cce8eSbellard #define STACK_DIR(x) (-(x))
1552b03cce8eSbellard #else
1553b03cce8eSbellard #define STACK_DIR(x) (x)
1554b03cce8eSbellard #endif
1555b03cce8eSbellard 
1556c896fe29Sbellard static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def,
1557c896fe29Sbellard                               int opc, const TCGArg *args,
1558c896fe29Sbellard                               unsigned int dead_iargs)
1559c896fe29Sbellard {
1560c896fe29Sbellard     int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params;
1561c896fe29Sbellard     TCGArg arg, func_arg;
1562c896fe29Sbellard     TCGTemp *ts;
1563f54b3f92Saurel32     tcg_target_long stack_offset, call_stack_size, func_addr;
1564b03cce8eSbellard     int const_func_arg, allocate_args;
1565c896fe29Sbellard     TCGRegSet allocated_regs;
1566c896fe29Sbellard     const TCGArgConstraint *arg_ct;
1567c896fe29Sbellard 
1568c896fe29Sbellard     arg = *args++;
1569c896fe29Sbellard 
1570c896fe29Sbellard     nb_oargs = arg >> 16;
1571c896fe29Sbellard     nb_iargs = arg & 0xffff;
1572c896fe29Sbellard     nb_params = nb_iargs - 1;
1573c896fe29Sbellard 
1574c896fe29Sbellard     flags = args[nb_oargs + nb_iargs];
1575c896fe29Sbellard 
1576c896fe29Sbellard     nb_regs = tcg_target_get_call_iarg_regs_count(flags);
1577c896fe29Sbellard     if (nb_regs > nb_params)
1578c896fe29Sbellard         nb_regs = nb_params;
1579c896fe29Sbellard 
1580c896fe29Sbellard     /* assign stack slots first */
1581c896fe29Sbellard     /* XXX: preallocate call stack */
1582c896fe29Sbellard     call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long);
1583c896fe29Sbellard     call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
1584c896fe29Sbellard         ~(TCG_TARGET_STACK_ALIGN - 1);
1585b03cce8eSbellard     allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
1586b03cce8eSbellard     if (allocate_args) {
1587b03cce8eSbellard         tcg_out_addi(s, TCG_REG_CALL_STACK, -STACK_DIR(call_stack_size));
1588b03cce8eSbellard     }
1589b03cce8eSbellard     /* XXX: on some architectures it does not start at zero */
1590c896fe29Sbellard     stack_offset = 0;
1591c896fe29Sbellard     for(i = nb_regs; i < nb_params; i++) {
1592c896fe29Sbellard         arg = args[nb_oargs + i];
1593c896fe29Sbellard         ts = &s->temps[arg];
1594c896fe29Sbellard         if (ts->val_type == TEMP_VAL_REG) {
1595e4d5434cSblueswir1             tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
1596c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_MEM) {
1597c896fe29Sbellard             reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
1598c896fe29Sbellard                                 s->reserved_regs);
1599c896fe29Sbellard             /* XXX: not correct if reading values from the stack */
1600e4d5434cSblueswir1             tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1601e4d5434cSblueswir1             tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
1602c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1603c896fe29Sbellard             reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
1604c896fe29Sbellard                                 s->reserved_regs);
1605c896fe29Sbellard             /* XXX: sign extend may be needed on some targets */
1606c896fe29Sbellard             tcg_out_movi(s, ts->type, reg, ts->val);
1607e4d5434cSblueswir1             tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
1608c896fe29Sbellard         } else {
1609c896fe29Sbellard             tcg_abort();
1610c896fe29Sbellard         }
1611b03cce8eSbellard         /* XXX: not necessarily in the same order */
1612b03cce8eSbellard         stack_offset += STACK_DIR(sizeof(tcg_target_long));
1613c896fe29Sbellard     }
1614c896fe29Sbellard 
1615c896fe29Sbellard     /* assign input registers */
1616c896fe29Sbellard     tcg_regset_set(allocated_regs, s->reserved_regs);
1617c896fe29Sbellard     for(i = 0; i < nb_regs; i++) {
1618c896fe29Sbellard         arg = args[nb_oargs + i];
1619c896fe29Sbellard         ts = &s->temps[arg];
1620c896fe29Sbellard         reg = tcg_target_call_iarg_regs[i];
1621c896fe29Sbellard         tcg_reg_free(s, reg);
1622c896fe29Sbellard         if (ts->val_type == TEMP_VAL_REG) {
1623c896fe29Sbellard             if (ts->reg != reg) {
1624c896fe29Sbellard                 tcg_out_mov(s, reg, ts->reg);
1625c896fe29Sbellard             }
1626c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_MEM) {
1627e4d5434cSblueswir1             tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1628c896fe29Sbellard         } else if (ts->val_type == TEMP_VAL_CONST) {
1629c896fe29Sbellard             /* XXX: sign extend ? */
1630c896fe29Sbellard             tcg_out_movi(s, ts->type, reg, ts->val);
1631c896fe29Sbellard         } else {
1632c896fe29Sbellard             tcg_abort();
1633c896fe29Sbellard         }
1634c896fe29Sbellard         tcg_regset_set_reg(allocated_regs, reg);
1635c896fe29Sbellard     }
1636c896fe29Sbellard 
1637c896fe29Sbellard     /* assign function address */
1638c896fe29Sbellard     func_arg = args[nb_oargs + nb_iargs - 1];
1639c896fe29Sbellard     arg_ct = &def->args_ct[0];
1640c896fe29Sbellard     ts = &s->temps[func_arg];
1641f54b3f92Saurel32     func_addr = ts->val;
1642c896fe29Sbellard     const_func_arg = 0;
1643c896fe29Sbellard     if (ts->val_type == TEMP_VAL_MEM) {
1644c896fe29Sbellard         reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1645e4d5434cSblueswir1         tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1646c896fe29Sbellard         func_arg = reg;
1647c896fe29Sbellard     } else if (ts->val_type == TEMP_VAL_REG) {
1648c896fe29Sbellard         reg = ts->reg;
1649c896fe29Sbellard         if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) {
1650c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1651c896fe29Sbellard             tcg_out_mov(s, reg, ts->reg);
1652c896fe29Sbellard         }
1653c896fe29Sbellard         func_arg = reg;
1654c896fe29Sbellard     } else if (ts->val_type == TEMP_VAL_CONST) {
1655f54b3f92Saurel32         if (tcg_target_const_match(func_addr, arg_ct)) {
1656c896fe29Sbellard             const_func_arg = 1;
1657f54b3f92Saurel32             func_arg = func_addr;
1658c896fe29Sbellard         } else {
1659c896fe29Sbellard             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
1660f54b3f92Saurel32             tcg_out_movi(s, ts->type, reg, func_addr);
1661c896fe29Sbellard             func_arg = reg;
1662c896fe29Sbellard         }
1663c896fe29Sbellard     } else {
1664c896fe29Sbellard         tcg_abort();
1665c896fe29Sbellard     }
1666c896fe29Sbellard 
1667c896fe29Sbellard     /* mark dead temporaries and free the associated registers */
1668c6e113f5Sbellard     for(i = 0; i < nb_iargs; i++) {
1669c896fe29Sbellard         arg = args[nb_oargs + i];
1670c896fe29Sbellard         if (IS_DEAD_IARG(i)) {
1671c896fe29Sbellard             ts = &s->temps[arg];
1672c896fe29Sbellard             if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
1673c896fe29Sbellard                 if (ts->val_type == TEMP_VAL_REG)
1674c896fe29Sbellard                     s->reg_to_temp[ts->reg] = -1;
1675c896fe29Sbellard                 ts->val_type = TEMP_VAL_DEAD;
1676c896fe29Sbellard             }
1677c896fe29Sbellard         }
1678c896fe29Sbellard     }
1679c896fe29Sbellard 
1680c896fe29Sbellard     /* clobber call registers */
1681c896fe29Sbellard     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1682c896fe29Sbellard         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
1683c896fe29Sbellard             tcg_reg_free(s, reg);
1684c896fe29Sbellard         }
1685c896fe29Sbellard     }
1686c896fe29Sbellard 
1687c896fe29Sbellard     /* store globals and free associated registers (we assume the call
1688c896fe29Sbellard        can modify any global. */
1689e5097dc8Sbellard     save_globals(s);
1690c896fe29Sbellard 
1691c896fe29Sbellard     tcg_out_op(s, opc, &func_arg, &const_func_arg);
1692c896fe29Sbellard 
1693b03cce8eSbellard     if (allocate_args) {
1694b03cce8eSbellard         tcg_out_addi(s, TCG_REG_CALL_STACK, STACK_DIR(call_stack_size));
1695b03cce8eSbellard     }
1696c896fe29Sbellard 
1697c896fe29Sbellard     /* assign output registers and emit moves if needed */
1698c896fe29Sbellard     for(i = 0; i < nb_oargs; i++) {
1699c896fe29Sbellard         arg = args[i];
1700c896fe29Sbellard         ts = &s->temps[arg];
1701c896fe29Sbellard         reg = tcg_target_call_oarg_regs[i];
1702c896fe29Sbellard         tcg_reg_free(s, reg);
1703c896fe29Sbellard         if (ts->fixed_reg) {
1704c896fe29Sbellard             if (ts->reg != reg) {
1705c896fe29Sbellard                 tcg_out_mov(s, ts->reg, reg);
1706c896fe29Sbellard             }
1707c896fe29Sbellard         } else {
1708c896fe29Sbellard             if (ts->val_type == TEMP_VAL_REG)
1709c896fe29Sbellard                 s->reg_to_temp[ts->reg] = -1;
1710c896fe29Sbellard             ts->val_type = TEMP_VAL_REG;
1711c896fe29Sbellard             ts->reg = reg;
1712c896fe29Sbellard             ts->mem_coherent = 0;
1713c896fe29Sbellard             s->reg_to_temp[reg] = arg;
1714c896fe29Sbellard         }
1715c896fe29Sbellard     }
1716c896fe29Sbellard 
1717c896fe29Sbellard     return nb_iargs + nb_oargs + def->nb_cargs + 1;
1718c896fe29Sbellard }
1719c896fe29Sbellard 
1720c896fe29Sbellard #ifdef CONFIG_PROFILER
1721c896fe29Sbellard 
1722c896fe29Sbellard static int64_t dyngen_table_op_count[NB_OPS];
1723c896fe29Sbellard 
1724c896fe29Sbellard void dump_op_count(void)
1725c896fe29Sbellard {
1726c896fe29Sbellard     int i;
1727c896fe29Sbellard     FILE *f;
1728c896fe29Sbellard     f = fopen("/tmp/op1.log", "w");
1729c896fe29Sbellard     for(i = 0; i < INDEX_op_end; i++) {
1730c896fe29Sbellard         fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]);
1731c896fe29Sbellard     }
1732c896fe29Sbellard     fclose(f);
1733c896fe29Sbellard     f = fopen("/tmp/op2.log", "w");
1734c896fe29Sbellard     for(i = INDEX_op_end; i < NB_OPS; i++) {
1735c896fe29Sbellard         fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, dyngen_table_op_count[i]);
1736c896fe29Sbellard     }
1737c896fe29Sbellard     fclose(f);
1738c896fe29Sbellard }
1739c896fe29Sbellard #endif
1740c896fe29Sbellard 
1741c896fe29Sbellard 
1742c896fe29Sbellard static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
17432ba1eeb6Spbrook                                       long search_pc)
1744c896fe29Sbellard {
1745c896fe29Sbellard     int opc, op_index, macro_op_index;
1746c896fe29Sbellard     const TCGOpDef *def;
1747c896fe29Sbellard     unsigned int dead_iargs;
1748c896fe29Sbellard     const TCGArg *args;
1749c896fe29Sbellard 
1750c896fe29Sbellard #ifdef DEBUG_DISAS
1751c896fe29Sbellard     if (unlikely(loglevel & CPU_LOG_TB_OP)) {
1752c896fe29Sbellard         fprintf(logfile, "OP:\n");
1753c896fe29Sbellard         tcg_dump_ops(s, logfile);
1754c896fe29Sbellard         fprintf(logfile, "\n");
1755c896fe29Sbellard     }
1756c896fe29Sbellard #endif
1757c896fe29Sbellard 
1758c896fe29Sbellard     tcg_liveness_analysis(s);
1759c896fe29Sbellard 
1760c896fe29Sbellard #ifdef DEBUG_DISAS
1761c896fe29Sbellard     if (unlikely(loglevel & CPU_LOG_TB_OP_OPT)) {
1762c896fe29Sbellard         fprintf(logfile, "OP after la:\n");
1763c896fe29Sbellard         tcg_dump_ops(s, logfile);
1764c896fe29Sbellard         fprintf(logfile, "\n");
1765c896fe29Sbellard     }
1766c896fe29Sbellard #endif
1767c896fe29Sbellard 
1768c896fe29Sbellard     tcg_reg_alloc_start(s);
1769c896fe29Sbellard 
1770c896fe29Sbellard     s->code_buf = gen_code_buf;
1771c896fe29Sbellard     s->code_ptr = gen_code_buf;
1772c896fe29Sbellard 
1773c896fe29Sbellard     macro_op_index = -1;
1774c896fe29Sbellard     args = gen_opparam_buf;
1775c896fe29Sbellard     op_index = 0;
1776b3db8758Sblueswir1 
1777c896fe29Sbellard     for(;;) {
1778c896fe29Sbellard         opc = gen_opc_buf[op_index];
1779c896fe29Sbellard #ifdef CONFIG_PROFILER
1780c896fe29Sbellard         dyngen_table_op_count[opc]++;
1781c896fe29Sbellard #endif
1782c896fe29Sbellard         def = &tcg_op_defs[opc];
1783c896fe29Sbellard #if 0
1784c896fe29Sbellard         printf("%s: %d %d %d\n", def->name,
1785c896fe29Sbellard                def->nb_oargs, def->nb_iargs, def->nb_cargs);
1786c896fe29Sbellard         //        dump_regs(s);
1787c896fe29Sbellard #endif
1788c896fe29Sbellard         switch(opc) {
1789c896fe29Sbellard         case INDEX_op_mov_i32:
1790c896fe29Sbellard #if TCG_TARGET_REG_BITS == 64
1791c896fe29Sbellard         case INDEX_op_mov_i64:
1792c896fe29Sbellard #endif
1793c896fe29Sbellard             dead_iargs = s->op_dead_iargs[op_index];
1794c896fe29Sbellard             tcg_reg_alloc_mov(s, def, args, dead_iargs);
1795c896fe29Sbellard             break;
1796c896fe29Sbellard         case INDEX_op_nop:
1797c896fe29Sbellard         case INDEX_op_nop1:
1798c896fe29Sbellard         case INDEX_op_nop2:
1799c896fe29Sbellard         case INDEX_op_nop3:
1800c896fe29Sbellard             break;
1801c896fe29Sbellard         case INDEX_op_nopn:
1802c896fe29Sbellard             args += args[0];
1803c896fe29Sbellard             goto next;
18045ff9d6a4Sbellard         case INDEX_op_discard:
18055ff9d6a4Sbellard             {
18065ff9d6a4Sbellard                 TCGTemp *ts;
18075ff9d6a4Sbellard                 ts = &s->temps[args[0]];
18085ff9d6a4Sbellard                 /* mark the temporary as dead */
18095ff9d6a4Sbellard                 if (ts->val_type != TEMP_VAL_CONST && !ts->fixed_reg) {
18105ff9d6a4Sbellard                     if (ts->val_type == TEMP_VAL_REG)
18115ff9d6a4Sbellard                         s->reg_to_temp[ts->reg] = -1;
18125ff9d6a4Sbellard                     ts->val_type = TEMP_VAL_DEAD;
18135ff9d6a4Sbellard                 }
18145ff9d6a4Sbellard             }
18155ff9d6a4Sbellard             break;
1816c896fe29Sbellard         case INDEX_op_macro_goto:
1817c896fe29Sbellard             macro_op_index = op_index; /* only used for exceptions */
1818c896fe29Sbellard             op_index = args[0] - 1;
1819c896fe29Sbellard             args = gen_opparam_buf + args[1];
1820c896fe29Sbellard             goto next;
1821c896fe29Sbellard         case INDEX_op_macro_end:
1822c896fe29Sbellard             macro_op_index = -1; /* only used for exceptions */
1823c896fe29Sbellard             op_index = args[0] - 1;
1824c896fe29Sbellard             args = gen_opparam_buf + args[1];
1825c896fe29Sbellard             goto next;
1826c896fe29Sbellard         case INDEX_op_macro_start:
1827c896fe29Sbellard             /* must never happen here */
1828c896fe29Sbellard             tcg_abort();
1829c896fe29Sbellard         case INDEX_op_set_label:
1830c896fe29Sbellard             tcg_reg_alloc_bb_end(s);
1831c896fe29Sbellard             tcg_out_label(s, args[0], (long)s->code_ptr);
1832c896fe29Sbellard             break;
1833c896fe29Sbellard         case INDEX_op_call:
1834c896fe29Sbellard             dead_iargs = s->op_dead_iargs[op_index];
1835c896fe29Sbellard             args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs);
1836c896fe29Sbellard             goto next;
1837c896fe29Sbellard         case INDEX_op_end:
1838c896fe29Sbellard             goto the_end;
1839cf2be984Sblueswir1 
1840bf6247fbSblueswir1 #ifdef CONFIG_DYNGEN_OP
1841c896fe29Sbellard         case 0 ... INDEX_op_end - 1:
1842c896fe29Sbellard             /* legacy dyngen ops */
1843c896fe29Sbellard #ifdef CONFIG_PROFILER
1844c896fe29Sbellard             {
1845c896fe29Sbellard                 extern int64_t dyngen_old_op_count;
1846c896fe29Sbellard                 dyngen_old_op_count++;
1847c896fe29Sbellard             }
1848c896fe29Sbellard #endif
1849c896fe29Sbellard             tcg_reg_alloc_bb_end(s);
18502ba1eeb6Spbrook             if (search_pc >= 0) {
1851c896fe29Sbellard                 s->code_ptr += def->copy_size;
1852c896fe29Sbellard                 args += def->nb_args;
1853c896fe29Sbellard             } else {
1854c896fe29Sbellard                 args = dyngen_op(s, opc, args);
1855c896fe29Sbellard             }
1856c896fe29Sbellard             goto next;
1857cf2be984Sblueswir1 #endif
1858c896fe29Sbellard         default:
1859c896fe29Sbellard             /* Note: in order to speed up the code, it would be much
1860c896fe29Sbellard                faster to have specialized register allocator functions for
1861c896fe29Sbellard                some common argument patterns */
1862c896fe29Sbellard             dead_iargs = s->op_dead_iargs[op_index];
1863c896fe29Sbellard             tcg_reg_alloc_op(s, def, opc, args, dead_iargs);
1864c896fe29Sbellard             break;
1865c896fe29Sbellard         }
1866c896fe29Sbellard         args += def->nb_args;
1867c896fe29Sbellard     next: ;
18682ba1eeb6Spbrook         if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) {
1869c896fe29Sbellard             if (macro_op_index >= 0)
1870c896fe29Sbellard                 return macro_op_index;
1871c896fe29Sbellard             else
1872c896fe29Sbellard                 return op_index;
1873c896fe29Sbellard         }
1874c896fe29Sbellard         op_index++;
1875c896fe29Sbellard #ifndef NDEBUG
1876c896fe29Sbellard         check_regs(s);
1877c896fe29Sbellard #endif
1878c896fe29Sbellard     }
1879c896fe29Sbellard  the_end:
1880c896fe29Sbellard     return -1;
1881c896fe29Sbellard }
1882c896fe29Sbellard 
1883c896fe29Sbellard int dyngen_code(TCGContext *s, uint8_t *gen_code_buf)
1884c896fe29Sbellard {
1885c896fe29Sbellard #ifdef CONFIG_PROFILER
1886c896fe29Sbellard     {
1887c896fe29Sbellard         extern int64_t dyngen_op_count;
1888c896fe29Sbellard         extern int dyngen_op_count_max;
1889c896fe29Sbellard         int n;
1890c896fe29Sbellard         n = (gen_opc_ptr - gen_opc_buf);
1891c896fe29Sbellard         dyngen_op_count += n;
1892c896fe29Sbellard         if (n > dyngen_op_count_max)
1893c896fe29Sbellard             dyngen_op_count_max = n;
1894c896fe29Sbellard     }
1895c896fe29Sbellard #endif
1896c896fe29Sbellard 
18972ba1eeb6Spbrook     tcg_gen_code_common(s, gen_code_buf, -1);
1898c896fe29Sbellard 
1899c896fe29Sbellard     /* flush instruction cache */
1900c896fe29Sbellard     flush_icache_range((unsigned long)gen_code_buf,
1901c896fe29Sbellard                        (unsigned long)s->code_ptr);
1902c896fe29Sbellard     return s->code_ptr -  gen_code_buf;
1903c896fe29Sbellard }
1904c896fe29Sbellard 
19052ba1eeb6Spbrook /* Return the index of the micro operation such as the pc after is <
1906623e265cSpbrook    offset bytes from the start of the TB.  The contents of gen_code_buf must
1907623e265cSpbrook    not be changed, though writing the same values is ok.
1908623e265cSpbrook    Return -1 if not found. */
1909623e265cSpbrook int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset)
1910c896fe29Sbellard {
1911623e265cSpbrook     return tcg_gen_code_common(s, gen_code_buf, offset);
1912c896fe29Sbellard }
1913