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