]> Git Repo - qemu.git/blob - tcg/tcg.c
tcg: Do not set guard pages on the rx portion of code_gen_buffer
[qemu.git] / tcg / tcg.c
1 /*
2  * Tiny Code Generator for QEMU
3  *
4  * Copyright (c) 2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 /* define it to use liveness analysis (better code) */
26 #define USE_TCG_OPTIMIZATIONS
27
28 #include "qemu/osdep.h"
29
30 /* Define to jump the ELF file used to communicate with GDB.  */
31 #undef DEBUG_JIT
32
33 #include "qemu/error-report.h"
34 #include "qemu/cutils.h"
35 #include "qemu/host-utils.h"
36 #include "qemu/qemu-print.h"
37 #include "qemu/timer.h"
38 #include "qemu/cacheflush.h"
39
40 /* Note: the long term plan is to reduce the dependencies on the QEMU
41    CPU definitions. Currently they are used for qemu_ld/st
42    instructions */
43 #define NO_CPU_IO_DEFS
44 #include "cpu.h"
45
46 #include "exec/exec-all.h"
47
48 #if !defined(CONFIG_USER_ONLY)
49 #include "hw/boards.h"
50 #endif
51
52 #include "tcg/tcg-op.h"
53
54 #if UINTPTR_MAX == UINT32_MAX
55 # define ELF_CLASS  ELFCLASS32
56 #else
57 # define ELF_CLASS  ELFCLASS64
58 #endif
59 #ifdef HOST_WORDS_BIGENDIAN
60 # define ELF_DATA   ELFDATA2MSB
61 #else
62 # define ELF_DATA   ELFDATA2LSB
63 #endif
64
65 #include "elf.h"
66 #include "exec/log.h"
67 #include "sysemu/sysemu.h"
68
69 /* Forward declarations for functions declared in tcg-target.c.inc and
70    used here. */
71 static void tcg_target_init(TCGContext *s);
72 static void tcg_target_qemu_prologue(TCGContext *s);
73 static bool patch_reloc(tcg_insn_unit *code_ptr, int type,
74                         intptr_t value, intptr_t addend);
75
76 /* The CIE and FDE header definitions will be common to all hosts.  */
77 typedef struct {
78     uint32_t len __attribute__((aligned((sizeof(void *)))));
79     uint32_t id;
80     uint8_t version;
81     char augmentation[1];
82     uint8_t code_align;
83     uint8_t data_align;
84     uint8_t return_column;
85 } DebugFrameCIE;
86
87 typedef struct QEMU_PACKED {
88     uint32_t len __attribute__((aligned((sizeof(void *)))));
89     uint32_t cie_offset;
90     uintptr_t func_start;
91     uintptr_t func_len;
92 } DebugFrameFDEHeader;
93
94 typedef struct QEMU_PACKED {
95     DebugFrameCIE cie;
96     DebugFrameFDEHeader fde;
97 } DebugFrameHeader;
98
99 static void tcg_register_jit_int(const void *buf, size_t size,
100                                  const void *debug_frame,
101                                  size_t debug_frame_size)
102     __attribute__((unused));
103
104 /* Forward declarations for functions declared and used in tcg-target.c.inc. */
105 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
106                        intptr_t arg2);
107 static bool tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
108 static void tcg_out_movi(TCGContext *s, TCGType type,
109                          TCGReg ret, tcg_target_long arg);
110 static void tcg_out_op(TCGContext *s, TCGOpcode opc,
111                        const TCGArg args[TCG_MAX_OP_ARGS],
112                        const int const_args[TCG_MAX_OP_ARGS]);
113 #if TCG_TARGET_MAYBE_vec
114 static bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
115                             TCGReg dst, TCGReg src);
116 static bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
117                              TCGReg dst, TCGReg base, intptr_t offset);
118 static void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
119                              TCGReg dst, int64_t arg);
120 static void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
121                            unsigned vecl, unsigned vece,
122                            const TCGArg args[TCG_MAX_OP_ARGS],
123                            const int const_args[TCG_MAX_OP_ARGS]);
124 #else
125 static inline bool tcg_out_dup_vec(TCGContext *s, TCGType type, unsigned vece,
126                                    TCGReg dst, TCGReg src)
127 {
128     g_assert_not_reached();
129 }
130 static inline bool tcg_out_dupm_vec(TCGContext *s, TCGType type, unsigned vece,
131                                     TCGReg dst, TCGReg base, intptr_t offset)
132 {
133     g_assert_not_reached();
134 }
135 static inline void tcg_out_dupi_vec(TCGContext *s, TCGType type, unsigned vece,
136                                     TCGReg dst, int64_t arg)
137 {
138     g_assert_not_reached();
139 }
140 static inline void tcg_out_vec_op(TCGContext *s, TCGOpcode opc,
141                                   unsigned vecl, unsigned vece,
142                                   const TCGArg args[TCG_MAX_OP_ARGS],
143                                   const int const_args[TCG_MAX_OP_ARGS])
144 {
145     g_assert_not_reached();
146 }
147 #endif
148 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
149                        intptr_t arg2);
150 static bool tcg_out_sti(TCGContext *s, TCGType type, TCGArg val,
151                         TCGReg base, intptr_t ofs);
152 static void tcg_out_call(TCGContext *s, const tcg_insn_unit *target);
153 static int tcg_target_const_match(tcg_target_long val, TCGType type,
154                                   const TCGArgConstraint *arg_ct);
155 #ifdef TCG_TARGET_NEED_LDST_LABELS
156 static int tcg_out_ldst_finalize(TCGContext *s);
157 #endif
158
159 #define TCG_HIGHWATER 1024
160
161 static TCGContext **tcg_ctxs;
162 static unsigned int n_tcg_ctxs;
163 TCGv_env cpu_env = 0;
164 const void *tcg_code_gen_epilogue;
165 uintptr_t tcg_splitwx_diff;
166
167 #ifndef CONFIG_TCG_INTERPRETER
168 tcg_prologue_fn *tcg_qemu_tb_exec;
169 #endif
170
171 struct tcg_region_tree {
172     QemuMutex lock;
173     GTree *tree;
174     /* padding to avoid false sharing is computed at run-time */
175 };
176
177 /*
178  * We divide code_gen_buffer into equally-sized "regions" that TCG threads
179  * dynamically allocate from as demand dictates. Given appropriate region
180  * sizing, this minimizes flushes even when some TCG threads generate a lot
181  * more code than others.
182  */
183 struct tcg_region_state {
184     QemuMutex lock;
185
186     /* fields set at init time */
187     void *start;
188     void *start_aligned;
189     void *end;
190     size_t n;
191     size_t size; /* size of one region */
192     size_t stride; /* .size + guard size */
193
194     /* fields protected by the lock */
195     size_t current; /* current region index */
196     size_t agg_size_full; /* aggregate size of full regions */
197 };
198
199 static struct tcg_region_state region;
200 /*
201  * This is an array of struct tcg_region_tree's, with padding.
202  * We use void * to simplify the computation of region_trees[i]; each
203  * struct is found every tree_size bytes.
204  */
205 static void *region_trees;
206 static size_t tree_size;
207 static TCGRegSet tcg_target_available_regs[TCG_TYPE_COUNT];
208 static TCGRegSet tcg_target_call_clobber_regs;
209
210 #if TCG_TARGET_INSN_UNIT_SIZE == 1
211 static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
212 {
213     *s->code_ptr++ = v;
214 }
215
216 static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
217                                                       uint8_t v)
218 {
219     *p = v;
220 }
221 #endif
222
223 #if TCG_TARGET_INSN_UNIT_SIZE <= 2
224 static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
225 {
226     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
227         *s->code_ptr++ = v;
228     } else {
229         tcg_insn_unit *p = s->code_ptr;
230         memcpy(p, &v, sizeof(v));
231         s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
232     }
233 }
234
235 static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
236                                                        uint16_t v)
237 {
238     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
239         *p = v;
240     } else {
241         memcpy(p, &v, sizeof(v));
242     }
243 }
244 #endif
245
246 #if TCG_TARGET_INSN_UNIT_SIZE <= 4
247 static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
248 {
249     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
250         *s->code_ptr++ = v;
251     } else {
252         tcg_insn_unit *p = s->code_ptr;
253         memcpy(p, &v, sizeof(v));
254         s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
255     }
256 }
257
258 static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
259                                                        uint32_t v)
260 {
261     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
262         *p = v;
263     } else {
264         memcpy(p, &v, sizeof(v));
265     }
266 }
267 #endif
268
269 #if TCG_TARGET_INSN_UNIT_SIZE <= 8
270 static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
271 {
272     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
273         *s->code_ptr++ = v;
274     } else {
275         tcg_insn_unit *p = s->code_ptr;
276         memcpy(p, &v, sizeof(v));
277         s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
278     }
279 }
280
281 static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
282                                                        uint64_t v)
283 {
284     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
285         *p = v;
286     } else {
287         memcpy(p, &v, sizeof(v));
288     }
289 }
290 #endif
291
292 /* label relocation processing */
293
294 static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
295                           TCGLabel *l, intptr_t addend)
296 {
297     TCGRelocation *r = tcg_malloc(sizeof(TCGRelocation));
298
299     r->type = type;
300     r->ptr = code_ptr;
301     r->addend = addend;
302     QSIMPLEQ_INSERT_TAIL(&l->relocs, r, next);
303 }
304
305 static void tcg_out_label(TCGContext *s, TCGLabel *l)
306 {
307     tcg_debug_assert(!l->has_value);
308     l->has_value = 1;
309     l->u.value_ptr = tcg_splitwx_to_rx(s->code_ptr);
310 }
311
312 TCGLabel *gen_new_label(void)
313 {
314     TCGContext *s = tcg_ctx;
315     TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
316
317     memset(l, 0, sizeof(TCGLabel));
318     l->id = s->nb_labels++;
319     QSIMPLEQ_INIT(&l->relocs);
320
321     QSIMPLEQ_INSERT_TAIL(&s->labels, l, next);
322
323     return l;
324 }
325
326 static bool tcg_resolve_relocs(TCGContext *s)
327 {
328     TCGLabel *l;
329
330     QSIMPLEQ_FOREACH(l, &s->labels, next) {
331         TCGRelocation *r;
332         uintptr_t value = l->u.value;
333
334         QSIMPLEQ_FOREACH(r, &l->relocs, next) {
335             if (!patch_reloc(r->ptr, r->type, value, r->addend)) {
336                 return false;
337             }
338         }
339     }
340     return true;
341 }
342
343 static void set_jmp_reset_offset(TCGContext *s, int which)
344 {
345     /*
346      * We will check for overflow at the end of the opcode loop in
347      * tcg_gen_code, where we bound tcg_current_code_size to UINT16_MAX.
348      */
349     s->tb_jmp_reset_offset[which] = tcg_current_code_size(s);
350 }
351
352 /* Signal overflow, starting over with fewer guest insns. */
353 static void QEMU_NORETURN tcg_raise_tb_overflow(TCGContext *s)
354 {
355     siglongjmp(s->jmp_trans, -2);
356 }
357
358 #define C_PFX1(P, A)                    P##A
359 #define C_PFX2(P, A, B)                 P##A##_##B
360 #define C_PFX3(P, A, B, C)              P##A##_##B##_##C
361 #define C_PFX4(P, A, B, C, D)           P##A##_##B##_##C##_##D
362 #define C_PFX5(P, A, B, C, D, E)        P##A##_##B##_##C##_##D##_##E
363 #define C_PFX6(P, A, B, C, D, E, F)     P##A##_##B##_##C##_##D##_##E##_##F
364
365 /* Define an enumeration for the various combinations. */
366
367 #define C_O0_I1(I1)                     C_PFX1(c_o0_i1_, I1),
368 #define C_O0_I2(I1, I2)                 C_PFX2(c_o0_i2_, I1, I2),
369 #define C_O0_I3(I1, I2, I3)             C_PFX3(c_o0_i3_, I1, I2, I3),
370 #define C_O0_I4(I1, I2, I3, I4)         C_PFX4(c_o0_i4_, I1, I2, I3, I4),
371
372 #define C_O1_I1(O1, I1)                 C_PFX2(c_o1_i1_, O1, I1),
373 #define C_O1_I2(O1, I1, I2)             C_PFX3(c_o1_i2_, O1, I1, I2),
374 #define C_O1_I3(O1, I1, I2, I3)         C_PFX4(c_o1_i3_, O1, I1, I2, I3),
375 #define C_O1_I4(O1, I1, I2, I3, I4)     C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4),
376
377 #define C_N1_I2(O1, I1, I2)             C_PFX3(c_n1_i2_, O1, I1, I2),
378
379 #define C_O2_I1(O1, O2, I1)             C_PFX3(c_o2_i1_, O1, O2, I1),
380 #define C_O2_I2(O1, O2, I1, I2)         C_PFX4(c_o2_i2_, O1, O2, I1, I2),
381 #define C_O2_I3(O1, O2, I1, I2, I3)     C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3),
382 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4),
383
384 typedef enum {
385 #include "tcg-target-con-set.h"
386 } TCGConstraintSetIndex;
387
388 static TCGConstraintSetIndex tcg_target_op_def(TCGOpcode);
389
390 #undef C_O0_I1
391 #undef C_O0_I2
392 #undef C_O0_I3
393 #undef C_O0_I4
394 #undef C_O1_I1
395 #undef C_O1_I2
396 #undef C_O1_I3
397 #undef C_O1_I4
398 #undef C_N1_I2
399 #undef C_O2_I1
400 #undef C_O2_I2
401 #undef C_O2_I3
402 #undef C_O2_I4
403
404 /* Put all of the constraint sets into an array, indexed by the enum. */
405
406 #define C_O0_I1(I1)                     { .args_ct_str = { #I1 } },
407 #define C_O0_I2(I1, I2)                 { .args_ct_str = { #I1, #I2 } },
408 #define C_O0_I3(I1, I2, I3)             { .args_ct_str = { #I1, #I2, #I3 } },
409 #define C_O0_I4(I1, I2, I3, I4)         { .args_ct_str = { #I1, #I2, #I3, #I4 } },
410
411 #define C_O1_I1(O1, I1)                 { .args_ct_str = { #O1, #I1 } },
412 #define C_O1_I2(O1, I1, I2)             { .args_ct_str = { #O1, #I1, #I2 } },
413 #define C_O1_I3(O1, I1, I2, I3)         { .args_ct_str = { #O1, #I1, #I2, #I3 } },
414 #define C_O1_I4(O1, I1, I2, I3, I4)     { .args_ct_str = { #O1, #I1, #I2, #I3, #I4 } },
415
416 #define C_N1_I2(O1, I1, I2)             { .args_ct_str = { "&" #O1, #I1, #I2 } },
417
418 #define C_O2_I1(O1, O2, I1)             { .args_ct_str = { #O1, #O2, #I1 } },
419 #define C_O2_I2(O1, O2, I1, I2)         { .args_ct_str = { #O1, #O2, #I1, #I2 } },
420 #define C_O2_I3(O1, O2, I1, I2, I3)     { .args_ct_str = { #O1, #O2, #I1, #I2, #I3 } },
421 #define C_O2_I4(O1, O2, I1, I2, I3, I4) { .args_ct_str = { #O1, #O2, #I1, #I2, #I3, #I4 } },
422
423 static const TCGTargetOpDef constraint_sets[] = {
424 #include "tcg-target-con-set.h"
425 };
426
427
428 #undef C_O0_I1
429 #undef C_O0_I2
430 #undef C_O0_I3
431 #undef C_O0_I4
432 #undef C_O1_I1
433 #undef C_O1_I2
434 #undef C_O1_I3
435 #undef C_O1_I4
436 #undef C_N1_I2
437 #undef C_O2_I1
438 #undef C_O2_I2
439 #undef C_O2_I3
440 #undef C_O2_I4
441
442 /* Expand the enumerator to be returned from tcg_target_op_def(). */
443
444 #define C_O0_I1(I1)                     C_PFX1(c_o0_i1_, I1)
445 #define C_O0_I2(I1, I2)                 C_PFX2(c_o0_i2_, I1, I2)
446 #define C_O0_I3(I1, I2, I3)             C_PFX3(c_o0_i3_, I1, I2, I3)
447 #define C_O0_I4(I1, I2, I3, I4)         C_PFX4(c_o0_i4_, I1, I2, I3, I4)
448
449 #define C_O1_I1(O1, I1)                 C_PFX2(c_o1_i1_, O1, I1)
450 #define C_O1_I2(O1, I1, I2)             C_PFX3(c_o1_i2_, O1, I1, I2)
451 #define C_O1_I3(O1, I1, I2, I3)         C_PFX4(c_o1_i3_, O1, I1, I2, I3)
452 #define C_O1_I4(O1, I1, I2, I3, I4)     C_PFX5(c_o1_i4_, O1, I1, I2, I3, I4)
453
454 #define C_N1_I2(O1, I1, I2)             C_PFX3(c_n1_i2_, O1, I1, I2)
455
456 #define C_O2_I1(O1, O2, I1)             C_PFX3(c_o2_i1_, O1, O2, I1)
457 #define C_O2_I2(O1, O2, I1, I2)         C_PFX4(c_o2_i2_, O1, O2, I1, I2)
458 #define C_O2_I3(O1, O2, I1, I2, I3)     C_PFX5(c_o2_i3_, O1, O2, I1, I2, I3)
459 #define C_O2_I4(O1, O2, I1, I2, I3, I4) C_PFX6(c_o2_i4_, O1, O2, I1, I2, I3, I4)
460
461 #include "tcg-target.c.inc"
462
463 /* compare a pointer @ptr and a tb_tc @s */
464 static int ptr_cmp_tb_tc(const void *ptr, const struct tb_tc *s)
465 {
466     if (ptr >= s->ptr + s->size) {
467         return 1;
468     } else if (ptr < s->ptr) {
469         return -1;
470     }
471     return 0;
472 }
473
474 static gint tb_tc_cmp(gconstpointer ap, gconstpointer bp)
475 {
476     const struct tb_tc *a = ap;
477     const struct tb_tc *b = bp;
478
479     /*
480      * When both sizes are set, we know this isn't a lookup.
481      * This is the most likely case: every TB must be inserted; lookups
482      * are a lot less frequent.
483      */
484     if (likely(a->size && b->size)) {
485         if (a->ptr > b->ptr) {
486             return 1;
487         } else if (a->ptr < b->ptr) {
488             return -1;
489         }
490         /* a->ptr == b->ptr should happen only on deletions */
491         g_assert(a->size == b->size);
492         return 0;
493     }
494     /*
495      * All lookups have either .size field set to 0.
496      * From the glib sources we see that @ap is always the lookup key. However
497      * the docs provide no guarantee, so we just mark this case as likely.
498      */
499     if (likely(a->size == 0)) {
500         return ptr_cmp_tb_tc(a->ptr, b);
501     }
502     return ptr_cmp_tb_tc(b->ptr, a);
503 }
504
505 static void tcg_region_trees_init(void)
506 {
507     size_t i;
508
509     tree_size = ROUND_UP(sizeof(struct tcg_region_tree), qemu_dcache_linesize);
510     region_trees = qemu_memalign(qemu_dcache_linesize, region.n * tree_size);
511     for (i = 0; i < region.n; i++) {
512         struct tcg_region_tree *rt = region_trees + i * tree_size;
513
514         qemu_mutex_init(&rt->lock);
515         rt->tree = g_tree_new(tb_tc_cmp);
516     }
517 }
518
519 static struct tcg_region_tree *tc_ptr_to_region_tree(const void *p)
520 {
521     size_t region_idx;
522
523     /*
524      * Like tcg_splitwx_to_rw, with no assert.  The pc may come from
525      * a signal handler over which the caller has no control.
526      */
527     if (!in_code_gen_buffer(p)) {
528         p -= tcg_splitwx_diff;
529         if (!in_code_gen_buffer(p)) {
530             return NULL;
531         }
532     }
533
534     if (p < region.start_aligned) {
535         region_idx = 0;
536     } else {
537         ptrdiff_t offset = p - region.start_aligned;
538
539         if (offset > region.stride * (region.n - 1)) {
540             region_idx = region.n - 1;
541         } else {
542             region_idx = offset / region.stride;
543         }
544     }
545     return region_trees + region_idx * tree_size;
546 }
547
548 void tcg_tb_insert(TranslationBlock *tb)
549 {
550     struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
551
552     g_assert(rt != NULL);
553     qemu_mutex_lock(&rt->lock);
554     g_tree_insert(rt->tree, &tb->tc, tb);
555     qemu_mutex_unlock(&rt->lock);
556 }
557
558 void tcg_tb_remove(TranslationBlock *tb)
559 {
560     struct tcg_region_tree *rt = tc_ptr_to_region_tree(tb->tc.ptr);
561
562     g_assert(rt != NULL);
563     qemu_mutex_lock(&rt->lock);
564     g_tree_remove(rt->tree, &tb->tc);
565     qemu_mutex_unlock(&rt->lock);
566 }
567
568 /*
569  * Find the TB 'tb' such that
570  * tb->tc.ptr <= tc_ptr < tb->tc.ptr + tb->tc.size
571  * Return NULL if not found.
572  */
573 TranslationBlock *tcg_tb_lookup(uintptr_t tc_ptr)
574 {
575     struct tcg_region_tree *rt = tc_ptr_to_region_tree((void *)tc_ptr);
576     TranslationBlock *tb;
577     struct tb_tc s = { .ptr = (void *)tc_ptr };
578
579     if (rt == NULL) {
580         return NULL;
581     }
582
583     qemu_mutex_lock(&rt->lock);
584     tb = g_tree_lookup(rt->tree, &s);
585     qemu_mutex_unlock(&rt->lock);
586     return tb;
587 }
588
589 static void tcg_region_tree_lock_all(void)
590 {
591     size_t i;
592
593     for (i = 0; i < region.n; i++) {
594         struct tcg_region_tree *rt = region_trees + i * tree_size;
595
596         qemu_mutex_lock(&rt->lock);
597     }
598 }
599
600 static void tcg_region_tree_unlock_all(void)
601 {
602     size_t i;
603
604     for (i = 0; i < region.n; i++) {
605         struct tcg_region_tree *rt = region_trees + i * tree_size;
606
607         qemu_mutex_unlock(&rt->lock);
608     }
609 }
610
611 void tcg_tb_foreach(GTraverseFunc func, gpointer user_data)
612 {
613     size_t i;
614
615     tcg_region_tree_lock_all();
616     for (i = 0; i < region.n; i++) {
617         struct tcg_region_tree *rt = region_trees + i * tree_size;
618
619         g_tree_foreach(rt->tree, func, user_data);
620     }
621     tcg_region_tree_unlock_all();
622 }
623
624 size_t tcg_nb_tbs(void)
625 {
626     size_t nb_tbs = 0;
627     size_t i;
628
629     tcg_region_tree_lock_all();
630     for (i = 0; i < region.n; i++) {
631         struct tcg_region_tree *rt = region_trees + i * tree_size;
632
633         nb_tbs += g_tree_nnodes(rt->tree);
634     }
635     tcg_region_tree_unlock_all();
636     return nb_tbs;
637 }
638
639 static gboolean tcg_region_tree_traverse(gpointer k, gpointer v, gpointer data)
640 {
641     TranslationBlock *tb = v;
642
643     tb_destroy(tb);
644     return FALSE;
645 }
646
647 static void tcg_region_tree_reset_all(void)
648 {
649     size_t i;
650
651     tcg_region_tree_lock_all();
652     for (i = 0; i < region.n; i++) {
653         struct tcg_region_tree *rt = region_trees + i * tree_size;
654
655         g_tree_foreach(rt->tree, tcg_region_tree_traverse, NULL);
656         /* Increment the refcount first so that destroy acts as a reset */
657         g_tree_ref(rt->tree);
658         g_tree_destroy(rt->tree);
659     }
660     tcg_region_tree_unlock_all();
661 }
662
663 static void tcg_region_bounds(size_t curr_region, void **pstart, void **pend)
664 {
665     void *start, *end;
666
667     start = region.start_aligned + curr_region * region.stride;
668     end = start + region.size;
669
670     if (curr_region == 0) {
671         start = region.start;
672     }
673     if (curr_region == region.n - 1) {
674         end = region.end;
675     }
676
677     *pstart = start;
678     *pend = end;
679 }
680
681 static void tcg_region_assign(TCGContext *s, size_t curr_region)
682 {
683     void *start, *end;
684
685     tcg_region_bounds(curr_region, &start, &end);
686
687     s->code_gen_buffer = start;
688     s->code_gen_ptr = start;
689     s->code_gen_buffer_size = end - start;
690     s->code_gen_highwater = end - TCG_HIGHWATER;
691 }
692
693 static bool tcg_region_alloc__locked(TCGContext *s)
694 {
695     if (region.current == region.n) {
696         return true;
697     }
698     tcg_region_assign(s, region.current);
699     region.current++;
700     return false;
701 }
702
703 /*
704  * Request a new region once the one in use has filled up.
705  * Returns true on error.
706  */
707 static bool tcg_region_alloc(TCGContext *s)
708 {
709     bool err;
710     /* read the region size now; alloc__locked will overwrite it on success */
711     size_t size_full = s->code_gen_buffer_size;
712
713     qemu_mutex_lock(&region.lock);
714     err = tcg_region_alloc__locked(s);
715     if (!err) {
716         region.agg_size_full += size_full - TCG_HIGHWATER;
717     }
718     qemu_mutex_unlock(&region.lock);
719     return err;
720 }
721
722 /*
723  * Perform a context's first region allocation.
724  * This function does _not_ increment region.agg_size_full.
725  */
726 static inline bool tcg_region_initial_alloc__locked(TCGContext *s)
727 {
728     return tcg_region_alloc__locked(s);
729 }
730
731 /* Call from a safe-work context */
732 void tcg_region_reset_all(void)
733 {
734     unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
735     unsigned int i;
736
737     qemu_mutex_lock(&region.lock);
738     region.current = 0;
739     region.agg_size_full = 0;
740
741     for (i = 0; i < n_ctxs; i++) {
742         TCGContext *s = qatomic_read(&tcg_ctxs[i]);
743         bool err = tcg_region_initial_alloc__locked(s);
744
745         g_assert(!err);
746     }
747     qemu_mutex_unlock(&region.lock);
748
749     tcg_region_tree_reset_all();
750 }
751
752 #ifdef CONFIG_USER_ONLY
753 static size_t tcg_n_regions(void)
754 {
755     return 1;
756 }
757 #else
758 /*
759  * It is likely that some vCPUs will translate more code than others, so we
760  * first try to set more regions than max_cpus, with those regions being of
761  * reasonable size. If that's not possible we make do by evenly dividing
762  * the code_gen_buffer among the vCPUs.
763  */
764 static size_t tcg_n_regions(void)
765 {
766     size_t i;
767
768     /* Use a single region if all we have is one vCPU thread */
769 #if !defined(CONFIG_USER_ONLY)
770     MachineState *ms = MACHINE(qdev_get_machine());
771     unsigned int max_cpus = ms->smp.max_cpus;
772 #endif
773     if (max_cpus == 1 || !qemu_tcg_mttcg_enabled()) {
774         return 1;
775     }
776
777     /* Try to have more regions than max_cpus, with each region being >= 2 MB */
778     for (i = 8; i > 0; i--) {
779         size_t regions_per_thread = i;
780         size_t region_size;
781
782         region_size = tcg_init_ctx.code_gen_buffer_size;
783         region_size /= max_cpus * regions_per_thread;
784
785         if (region_size >= 2 * 1024u * 1024) {
786             return max_cpus * regions_per_thread;
787         }
788     }
789     /* If we can't, then just allocate one region per vCPU thread */
790     return max_cpus;
791 }
792 #endif
793
794 /*
795  * Initializes region partitioning.
796  *
797  * Called at init time from the parent thread (i.e. the one calling
798  * tcg_context_init), after the target's TCG globals have been set.
799  *
800  * Region partitioning works by splitting code_gen_buffer into separate regions,
801  * and then assigning regions to TCG threads so that the threads can translate
802  * code in parallel without synchronization.
803  *
804  * In softmmu the number of TCG threads is bounded by max_cpus, so we use at
805  * least max_cpus regions in MTTCG. In !MTTCG we use a single region.
806  * Note that the TCG options from the command-line (i.e. -accel accel=tcg,[...])
807  * must have been parsed before calling this function, since it calls
808  * qemu_tcg_mttcg_enabled().
809  *
810  * In user-mode we use a single region.  Having multiple regions in user-mode
811  * is not supported, because the number of vCPU threads (recall that each thread
812  * spawned by the guest corresponds to a vCPU thread) is only bounded by the
813  * OS, and usually this number is huge (tens of thousands is not uncommon).
814  * Thus, given this large bound on the number of vCPU threads and the fact
815  * that code_gen_buffer is allocated at compile-time, we cannot guarantee
816  * that the availability of at least one region per vCPU thread.
817  *
818  * However, this user-mode limitation is unlikely to be a significant problem
819  * in practice. Multi-threaded guests share most if not all of their translated
820  * code, which makes parallel code generation less appealing than in softmmu.
821  */
822 void tcg_region_init(void)
823 {
824     void *buf = tcg_init_ctx.code_gen_buffer;
825     void *aligned;
826     size_t size = tcg_init_ctx.code_gen_buffer_size;
827     size_t page_size = qemu_real_host_page_size;
828     size_t region_size;
829     size_t n_regions;
830     size_t i;
831
832     n_regions = tcg_n_regions();
833
834     /* The first region will be 'aligned - buf' bytes larger than the others */
835     aligned = QEMU_ALIGN_PTR_UP(buf, page_size);
836     g_assert(aligned < tcg_init_ctx.code_gen_buffer + size);
837     /*
838      * Make region_size a multiple of page_size, using aligned as the start.
839      * As a result of this we might end up with a few extra pages at the end of
840      * the buffer; we will assign those to the last region.
841      */
842     region_size = (size - (aligned - buf)) / n_regions;
843     region_size = QEMU_ALIGN_DOWN(region_size, page_size);
844
845     /* A region must have at least 2 pages; one code, one guard */
846     g_assert(region_size >= 2 * page_size);
847
848     /* init the region struct */
849     qemu_mutex_init(&region.lock);
850     region.n = n_regions;
851     region.size = region_size - page_size;
852     region.stride = region_size;
853     region.start = buf;
854     region.start_aligned = aligned;
855     /* page-align the end, since its last page will be a guard page */
856     region.end = QEMU_ALIGN_PTR_DOWN(buf + size, page_size);
857     /* account for that last guard page */
858     region.end -= page_size;
859
860     /*
861      * Set guard pages in the rw buffer, as that's the one into which
862      * buffer overruns could occur.  Do not set guard pages in the rx
863      * buffer -- let that one use hugepages throughout.
864      */
865     for (i = 0; i < region.n; i++) {
866         void *start, *end;
867         int rc;
868
869         tcg_region_bounds(i, &start, &end);
870         rc = qemu_mprotect_none(end, page_size);
871         g_assert(!rc);
872     }
873
874     tcg_region_trees_init();
875
876     /* In user-mode we support only one ctx, so do the initial allocation now */
877 #ifdef CONFIG_USER_ONLY
878     {
879         bool err = tcg_region_initial_alloc__locked(tcg_ctx);
880
881         g_assert(!err);
882     }
883 #endif
884 }
885
886 #ifdef CONFIG_DEBUG_TCG
887 const void *tcg_splitwx_to_rx(void *rw)
888 {
889     /* Pass NULL pointers unchanged. */
890     if (rw) {
891         g_assert(in_code_gen_buffer(rw));
892         rw += tcg_splitwx_diff;
893     }
894     return rw;
895 }
896
897 void *tcg_splitwx_to_rw(const void *rx)
898 {
899     /* Pass NULL pointers unchanged. */
900     if (rx) {
901         rx -= tcg_splitwx_diff;
902         /* Assert that we end with a pointer in the rw region. */
903         g_assert(in_code_gen_buffer(rx));
904     }
905     return (void *)rx;
906 }
907 #endif /* CONFIG_DEBUG_TCG */
908
909 static void alloc_tcg_plugin_context(TCGContext *s)
910 {
911 #ifdef CONFIG_PLUGIN
912     s->plugin_tb = g_new0(struct qemu_plugin_tb, 1);
913     s->plugin_tb->insns =
914         g_ptr_array_new_with_free_func(qemu_plugin_insn_cleanup_fn);
915 #endif
916 }
917
918 /*
919  * All TCG threads except the parent (i.e. the one that called tcg_context_init
920  * and registered the target's TCG globals) must register with this function
921  * before initiating translation.
922  *
923  * In user-mode we just point tcg_ctx to tcg_init_ctx. See the documentation
924  * of tcg_region_init() for the reasoning behind this.
925  *
926  * In softmmu each caller registers its context in tcg_ctxs[]. Note that in
927  * softmmu tcg_ctxs[] does not track tcg_ctx_init, since the initial context
928  * is not used anymore for translation once this function is called.
929  *
930  * Not tracking tcg_init_ctx in tcg_ctxs[] in softmmu keeps code that iterates
931  * over the array (e.g. tcg_code_size() the same for both softmmu and user-mode.
932  */
933 #ifdef CONFIG_USER_ONLY
934 void tcg_register_thread(void)
935 {
936     tcg_ctx = &tcg_init_ctx;
937 }
938 #else
939 void tcg_register_thread(void)
940 {
941     MachineState *ms = MACHINE(qdev_get_machine());
942     TCGContext *s = g_malloc(sizeof(*s));
943     unsigned int i, n;
944     bool err;
945
946     *s = tcg_init_ctx;
947
948     /* Relink mem_base.  */
949     for (i = 0, n = tcg_init_ctx.nb_globals; i < n; ++i) {
950         if (tcg_init_ctx.temps[i].mem_base) {
951             ptrdiff_t b = tcg_init_ctx.temps[i].mem_base - tcg_init_ctx.temps;
952             tcg_debug_assert(b >= 0 && b < n);
953             s->temps[i].mem_base = &s->temps[b];
954         }
955     }
956
957     /* Claim an entry in tcg_ctxs */
958     n = qatomic_fetch_inc(&n_tcg_ctxs);
959     g_assert(n < ms->smp.max_cpus);
960     qatomic_set(&tcg_ctxs[n], s);
961
962     if (n > 0) {
963         alloc_tcg_plugin_context(s);
964     }
965
966     tcg_ctx = s;
967     qemu_mutex_lock(&region.lock);
968     err = tcg_region_initial_alloc__locked(tcg_ctx);
969     g_assert(!err);
970     qemu_mutex_unlock(&region.lock);
971 }
972 #endif /* !CONFIG_USER_ONLY */
973
974 /*
975  * Returns the size (in bytes) of all translated code (i.e. from all regions)
976  * currently in the cache.
977  * See also: tcg_code_capacity()
978  * Do not confuse with tcg_current_code_size(); that one applies to a single
979  * TCG context.
980  */
981 size_t tcg_code_size(void)
982 {
983     unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
984     unsigned int i;
985     size_t total;
986
987     qemu_mutex_lock(&region.lock);
988     total = region.agg_size_full;
989     for (i = 0; i < n_ctxs; i++) {
990         const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
991         size_t size;
992
993         size = qatomic_read(&s->code_gen_ptr) - s->code_gen_buffer;
994         g_assert(size <= s->code_gen_buffer_size);
995         total += size;
996     }
997     qemu_mutex_unlock(&region.lock);
998     return total;
999 }
1000
1001 /*
1002  * Returns the code capacity (in bytes) of the entire cache, i.e. including all
1003  * regions.
1004  * See also: tcg_code_size()
1005  */
1006 size_t tcg_code_capacity(void)
1007 {
1008     size_t guard_size, capacity;
1009
1010     /* no need for synchronization; these variables are set at init time */
1011     guard_size = region.stride - region.size;
1012     capacity = region.end + guard_size - region.start;
1013     capacity -= region.n * (guard_size + TCG_HIGHWATER);
1014     return capacity;
1015 }
1016
1017 size_t tcg_tb_phys_invalidate_count(void)
1018 {
1019     unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
1020     unsigned int i;
1021     size_t total = 0;
1022
1023     for (i = 0; i < n_ctxs; i++) {
1024         const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
1025
1026         total += qatomic_read(&s->tb_phys_invalidate_count);
1027     }
1028     return total;
1029 }
1030
1031 /* pool based memory allocation */
1032 void *tcg_malloc_internal(TCGContext *s, int size)
1033 {
1034     TCGPool *p;
1035     int pool_size;
1036     
1037     if (size > TCG_POOL_CHUNK_SIZE) {
1038         /* big malloc: insert a new pool (XXX: could optimize) */
1039         p = g_malloc(sizeof(TCGPool) + size);
1040         p->size = size;
1041         p->next = s->pool_first_large;
1042         s->pool_first_large = p;
1043         return p->data;
1044     } else {
1045         p = s->pool_current;
1046         if (!p) {
1047             p = s->pool_first;
1048             if (!p)
1049                 goto new_pool;
1050         } else {
1051             if (!p->next) {
1052             new_pool:
1053                 pool_size = TCG_POOL_CHUNK_SIZE;
1054                 p = g_malloc(sizeof(TCGPool) + pool_size);
1055                 p->size = pool_size;
1056                 p->next = NULL;
1057                 if (s->pool_current) 
1058                     s->pool_current->next = p;
1059                 else
1060                     s->pool_first = p;
1061             } else {
1062                 p = p->next;
1063             }
1064         }
1065     }
1066     s->pool_current = p;
1067     s->pool_cur = p->data + size;
1068     s->pool_end = p->data + p->size;
1069     return p->data;
1070 }
1071
1072 void tcg_pool_reset(TCGContext *s)
1073 {
1074     TCGPool *p, *t;
1075     for (p = s->pool_first_large; p; p = t) {
1076         t = p->next;
1077         g_free(p);
1078     }
1079     s->pool_first_large = NULL;
1080     s->pool_cur = s->pool_end = NULL;
1081     s->pool_current = NULL;
1082 }
1083
1084 typedef struct TCGHelperInfo {
1085     void *func;
1086     const char *name;
1087     unsigned flags;
1088     unsigned sizemask;
1089 } TCGHelperInfo;
1090
1091 #include "exec/helper-proto.h"
1092
1093 static const TCGHelperInfo all_helpers[] = {
1094 #include "exec/helper-tcg.h"
1095 };
1096 static GHashTable *helper_table;
1097
1098 static int indirect_reg_alloc_order[ARRAY_SIZE(tcg_target_reg_alloc_order)];
1099 static void process_op_defs(TCGContext *s);
1100 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1101                                             TCGReg reg, const char *name);
1102
1103 void tcg_context_init(TCGContext *s)
1104 {
1105     int op, total_args, n, i;
1106     TCGOpDef *def;
1107     TCGArgConstraint *args_ct;
1108     TCGTemp *ts;
1109
1110     memset(s, 0, sizeof(*s));
1111     s->nb_globals = 0;
1112
1113     /* Count total number of arguments and allocate the corresponding
1114        space */
1115     total_args = 0;
1116     for(op = 0; op < NB_OPS; op++) {
1117         def = &tcg_op_defs[op];
1118         n = def->nb_iargs + def->nb_oargs;
1119         total_args += n;
1120     }
1121
1122     args_ct = g_new0(TCGArgConstraint, total_args);
1123
1124     for(op = 0; op < NB_OPS; op++) {
1125         def = &tcg_op_defs[op];
1126         def->args_ct = args_ct;
1127         n = def->nb_iargs + def->nb_oargs;
1128         args_ct += n;
1129     }
1130
1131     /* Register helpers.  */
1132     /* Use g_direct_hash/equal for direct pointer comparisons on func.  */
1133     helper_table = g_hash_table_new(NULL, NULL);
1134
1135     for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
1136         g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
1137                             (gpointer)&all_helpers[i]);
1138     }
1139
1140     tcg_target_init(s);
1141     process_op_defs(s);
1142
1143     /* Reverse the order of the saved registers, assuming they're all at
1144        the start of tcg_target_reg_alloc_order.  */
1145     for (n = 0; n < ARRAY_SIZE(tcg_target_reg_alloc_order); ++n) {
1146         int r = tcg_target_reg_alloc_order[n];
1147         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, r)) {
1148             break;
1149         }
1150     }
1151     for (i = 0; i < n; ++i) {
1152         indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[n - 1 - i];
1153     }
1154     for (; i < ARRAY_SIZE(tcg_target_reg_alloc_order); ++i) {
1155         indirect_reg_alloc_order[i] = tcg_target_reg_alloc_order[i];
1156     }
1157
1158     alloc_tcg_plugin_context(s);
1159
1160     tcg_ctx = s;
1161     /*
1162      * In user-mode we simply share the init context among threads, since we
1163      * use a single region. See the documentation tcg_region_init() for the
1164      * reasoning behind this.
1165      * In softmmu we will have at most max_cpus TCG threads.
1166      */
1167 #ifdef CONFIG_USER_ONLY
1168     tcg_ctxs = &tcg_ctx;
1169     n_tcg_ctxs = 1;
1170 #else
1171     MachineState *ms = MACHINE(qdev_get_machine());
1172     unsigned int max_cpus = ms->smp.max_cpus;
1173     tcg_ctxs = g_new(TCGContext *, max_cpus);
1174 #endif
1175
1176     tcg_debug_assert(!tcg_regset_test_reg(s->reserved_regs, TCG_AREG0));
1177     ts = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, TCG_AREG0, "env");
1178     cpu_env = temp_tcgv_ptr(ts);
1179 }
1180
1181 /*
1182  * Allocate TBs right before their corresponding translated code, making
1183  * sure that TBs and code are on different cache lines.
1184  */
1185 TranslationBlock *tcg_tb_alloc(TCGContext *s)
1186 {
1187     uintptr_t align = qemu_icache_linesize;
1188     TranslationBlock *tb;
1189     void *next;
1190
1191  retry:
1192     tb = (void *)ROUND_UP((uintptr_t)s->code_gen_ptr, align);
1193     next = (void *)ROUND_UP((uintptr_t)(tb + 1), align);
1194
1195     if (unlikely(next > s->code_gen_highwater)) {
1196         if (tcg_region_alloc(s)) {
1197             return NULL;
1198         }
1199         goto retry;
1200     }
1201     qatomic_set(&s->code_gen_ptr, next);
1202     s->data_gen_ptr = NULL;
1203     return tb;
1204 }
1205
1206 void tcg_prologue_init(TCGContext *s)
1207 {
1208     size_t prologue_size, total_size;
1209     void *buf0, *buf1;
1210
1211     /* Put the prologue at the beginning of code_gen_buffer.  */
1212     buf0 = s->code_gen_buffer;
1213     total_size = s->code_gen_buffer_size;
1214     s->code_ptr = buf0;
1215     s->code_buf = buf0;
1216     s->data_gen_ptr = NULL;
1217
1218     /*
1219      * The region trees are not yet configured, but tcg_splitwx_to_rx
1220      * needs the bounds for an assert.
1221      */
1222     region.start = buf0;
1223     region.end = buf0 + total_size;
1224
1225 #ifndef CONFIG_TCG_INTERPRETER
1226     tcg_qemu_tb_exec = (tcg_prologue_fn *)tcg_splitwx_to_rx(buf0);
1227 #endif
1228
1229     /* Compute a high-water mark, at which we voluntarily flush the buffer
1230        and start over.  The size here is arbitrary, significantly larger
1231        than we expect the code generation for any one opcode to require.  */
1232     s->code_gen_highwater = s->code_gen_buffer + (total_size - TCG_HIGHWATER);
1233
1234 #ifdef TCG_TARGET_NEED_POOL_LABELS
1235     s->pool_labels = NULL;
1236 #endif
1237
1238     qemu_thread_jit_write();
1239     /* Generate the prologue.  */
1240     tcg_target_qemu_prologue(s);
1241
1242 #ifdef TCG_TARGET_NEED_POOL_LABELS
1243     /* Allow the prologue to put e.g. guest_base into a pool entry.  */
1244     {
1245         int result = tcg_out_pool_finalize(s);
1246         tcg_debug_assert(result == 0);
1247     }
1248 #endif
1249
1250     buf1 = s->code_ptr;
1251 #ifndef CONFIG_TCG_INTERPRETER
1252     flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(buf0), (uintptr_t)buf0,
1253                         tcg_ptr_byte_diff(buf1, buf0));
1254 #endif
1255
1256     /* Deduct the prologue from the buffer.  */
1257     prologue_size = tcg_current_code_size(s);
1258     s->code_gen_ptr = buf1;
1259     s->code_gen_buffer = buf1;
1260     s->code_buf = buf1;
1261     total_size -= prologue_size;
1262     s->code_gen_buffer_size = total_size;
1263
1264     tcg_register_jit(tcg_splitwx_to_rx(s->code_gen_buffer), total_size);
1265
1266 #ifdef DEBUG_DISAS
1267     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
1268         FILE *logfile = qemu_log_lock();
1269         qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
1270         if (s->data_gen_ptr) {
1271             size_t code_size = s->data_gen_ptr - buf0;
1272             size_t data_size = prologue_size - code_size;
1273             size_t i;
1274
1275             log_disas(buf0, code_size);
1276
1277             for (i = 0; i < data_size; i += sizeof(tcg_target_ulong)) {
1278                 if (sizeof(tcg_target_ulong) == 8) {
1279                     qemu_log("0x%08" PRIxPTR ":  .quad  0x%016" PRIx64 "\n",
1280                              (uintptr_t)s->data_gen_ptr + i,
1281                              *(uint64_t *)(s->data_gen_ptr + i));
1282                 } else {
1283                     qemu_log("0x%08" PRIxPTR ":  .long  0x%08x\n",
1284                              (uintptr_t)s->data_gen_ptr + i,
1285                              *(uint32_t *)(s->data_gen_ptr + i));
1286                 }
1287             }
1288         } else {
1289             log_disas(buf0, prologue_size);
1290         }
1291         qemu_log("\n");
1292         qemu_log_flush();
1293         qemu_log_unlock(logfile);
1294     }
1295 #endif
1296
1297     /* Assert that goto_ptr is implemented completely.  */
1298     if (TCG_TARGET_HAS_goto_ptr) {
1299         tcg_debug_assert(tcg_code_gen_epilogue != NULL);
1300     }
1301 }
1302
1303 void tcg_func_start(TCGContext *s)
1304 {
1305     tcg_pool_reset(s);
1306     s->nb_temps = s->nb_globals;
1307
1308     /* No temps have been previously allocated for size or locality.  */
1309     memset(s->free_temps, 0, sizeof(s->free_temps));
1310
1311     /* No constant temps have been previously allocated. */
1312     for (int i = 0; i < TCG_TYPE_COUNT; ++i) {
1313         if (s->const_table[i]) {
1314             g_hash_table_remove_all(s->const_table[i]);
1315         }
1316     }
1317
1318     s->nb_ops = 0;
1319     s->nb_labels = 0;
1320     s->current_frame_offset = s->frame_start;
1321
1322 #ifdef CONFIG_DEBUG_TCG
1323     s->goto_tb_issue_mask = 0;
1324 #endif
1325
1326     QTAILQ_INIT(&s->ops);
1327     QTAILQ_INIT(&s->free_ops);
1328     QSIMPLEQ_INIT(&s->labels);
1329 }
1330
1331 static TCGTemp *tcg_temp_alloc(TCGContext *s)
1332 {
1333     int n = s->nb_temps++;
1334
1335     if (n >= TCG_MAX_TEMPS) {
1336         tcg_raise_tb_overflow(s);
1337     }
1338     return memset(&s->temps[n], 0, sizeof(TCGTemp));
1339 }
1340
1341 static TCGTemp *tcg_global_alloc(TCGContext *s)
1342 {
1343     TCGTemp *ts;
1344
1345     tcg_debug_assert(s->nb_globals == s->nb_temps);
1346     tcg_debug_assert(s->nb_globals < TCG_MAX_TEMPS);
1347     s->nb_globals++;
1348     ts = tcg_temp_alloc(s);
1349     ts->kind = TEMP_GLOBAL;
1350
1351     return ts;
1352 }
1353
1354 static TCGTemp *tcg_global_reg_new_internal(TCGContext *s, TCGType type,
1355                                             TCGReg reg, const char *name)
1356 {
1357     TCGTemp *ts;
1358
1359     if (TCG_TARGET_REG_BITS == 32 && type != TCG_TYPE_I32) {
1360         tcg_abort();
1361     }
1362
1363     ts = tcg_global_alloc(s);
1364     ts->base_type = type;
1365     ts->type = type;
1366     ts->kind = TEMP_FIXED;
1367     ts->reg = reg;
1368     ts->name = name;
1369     tcg_regset_set_reg(s->reserved_regs, reg);
1370
1371     return ts;
1372 }
1373
1374 void tcg_set_frame(TCGContext *s, TCGReg reg, intptr_t start, intptr_t size)
1375 {
1376     s->frame_start = start;
1377     s->frame_end = start + size;
1378     s->frame_temp
1379         = tcg_global_reg_new_internal(s, TCG_TYPE_PTR, reg, "_frame");
1380 }
1381
1382 TCGTemp *tcg_global_mem_new_internal(TCGType type, TCGv_ptr base,
1383                                      intptr_t offset, const char *name)
1384 {
1385     TCGContext *s = tcg_ctx;
1386     TCGTemp *base_ts = tcgv_ptr_temp(base);
1387     TCGTemp *ts = tcg_global_alloc(s);
1388     int indirect_reg = 0, bigendian = 0;
1389 #ifdef HOST_WORDS_BIGENDIAN
1390     bigendian = 1;
1391 #endif
1392
1393     switch (base_ts->kind) {
1394     case TEMP_FIXED:
1395         break;
1396     case TEMP_GLOBAL:
1397         /* We do not support double-indirect registers.  */
1398         tcg_debug_assert(!base_ts->indirect_reg);
1399         base_ts->indirect_base = 1;
1400         s->nb_indirects += (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64
1401                             ? 2 : 1);
1402         indirect_reg = 1;
1403         break;
1404     default:
1405         g_assert_not_reached();
1406     }
1407
1408     if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1409         TCGTemp *ts2 = tcg_global_alloc(s);
1410         char buf[64];
1411
1412         ts->base_type = TCG_TYPE_I64;
1413         ts->type = TCG_TYPE_I32;
1414         ts->indirect_reg = indirect_reg;
1415         ts->mem_allocated = 1;
1416         ts->mem_base = base_ts;
1417         ts->mem_offset = offset + bigendian * 4;
1418         pstrcpy(buf, sizeof(buf), name);
1419         pstrcat(buf, sizeof(buf), "_0");
1420         ts->name = strdup(buf);
1421
1422         tcg_debug_assert(ts2 == ts + 1);
1423         ts2->base_type = TCG_TYPE_I64;
1424         ts2->type = TCG_TYPE_I32;
1425         ts2->indirect_reg = indirect_reg;
1426         ts2->mem_allocated = 1;
1427         ts2->mem_base = base_ts;
1428         ts2->mem_offset = offset + (1 - bigendian) * 4;
1429         pstrcpy(buf, sizeof(buf), name);
1430         pstrcat(buf, sizeof(buf), "_1");
1431         ts2->name = strdup(buf);
1432     } else {
1433         ts->base_type = type;
1434         ts->type = type;
1435         ts->indirect_reg = indirect_reg;
1436         ts->mem_allocated = 1;
1437         ts->mem_base = base_ts;
1438         ts->mem_offset = offset;
1439         ts->name = name;
1440     }
1441     return ts;
1442 }
1443
1444 TCGTemp *tcg_temp_new_internal(TCGType type, bool temp_local)
1445 {
1446     TCGContext *s = tcg_ctx;
1447     TCGTempKind kind = temp_local ? TEMP_LOCAL : TEMP_NORMAL;
1448     TCGTemp *ts;
1449     int idx, k;
1450
1451     k = type + (temp_local ? TCG_TYPE_COUNT : 0);
1452     idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
1453     if (idx < TCG_MAX_TEMPS) {
1454         /* There is already an available temp with the right type.  */
1455         clear_bit(idx, s->free_temps[k].l);
1456
1457         ts = &s->temps[idx];
1458         ts->temp_allocated = 1;
1459         tcg_debug_assert(ts->base_type == type);
1460         tcg_debug_assert(ts->kind == kind);
1461     } else {
1462         ts = tcg_temp_alloc(s);
1463         if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1464             TCGTemp *ts2 = tcg_temp_alloc(s);
1465
1466             ts->base_type = type;
1467             ts->type = TCG_TYPE_I32;
1468             ts->temp_allocated = 1;
1469             ts->kind = kind;
1470
1471             tcg_debug_assert(ts2 == ts + 1);
1472             ts2->base_type = TCG_TYPE_I64;
1473             ts2->type = TCG_TYPE_I32;
1474             ts2->temp_allocated = 1;
1475             ts2->kind = kind;
1476         } else {
1477             ts->base_type = type;
1478             ts->type = type;
1479             ts->temp_allocated = 1;
1480             ts->kind = kind;
1481         }
1482     }
1483
1484 #if defined(CONFIG_DEBUG_TCG)
1485     s->temps_in_use++;
1486 #endif
1487     return ts;
1488 }
1489
1490 TCGv_vec tcg_temp_new_vec(TCGType type)
1491 {
1492     TCGTemp *t;
1493
1494 #ifdef CONFIG_DEBUG_TCG
1495     switch (type) {
1496     case TCG_TYPE_V64:
1497         assert(TCG_TARGET_HAS_v64);
1498         break;
1499     case TCG_TYPE_V128:
1500         assert(TCG_TARGET_HAS_v128);
1501         break;
1502     case TCG_TYPE_V256:
1503         assert(TCG_TARGET_HAS_v256);
1504         break;
1505     default:
1506         g_assert_not_reached();
1507     }
1508 #endif
1509
1510     t = tcg_temp_new_internal(type, 0);
1511     return temp_tcgv_vec(t);
1512 }
1513
1514 /* Create a new temp of the same type as an existing temp.  */
1515 TCGv_vec tcg_temp_new_vec_matching(TCGv_vec match)
1516 {
1517     TCGTemp *t = tcgv_vec_temp(match);
1518
1519     tcg_debug_assert(t->temp_allocated != 0);
1520
1521     t = tcg_temp_new_internal(t->base_type, 0);
1522     return temp_tcgv_vec(t);
1523 }
1524
1525 void tcg_temp_free_internal(TCGTemp *ts)
1526 {
1527     TCGContext *s = tcg_ctx;
1528     int k, idx;
1529
1530     /* In order to simplify users of tcg_constant_*, silently ignore free. */
1531     if (ts->kind == TEMP_CONST) {
1532         return;
1533     }
1534
1535 #if defined(CONFIG_DEBUG_TCG)
1536     s->temps_in_use--;
1537     if (s->temps_in_use < 0) {
1538         fprintf(stderr, "More temporaries freed than allocated!\n");
1539     }
1540 #endif
1541
1542     tcg_debug_assert(ts->kind < TEMP_GLOBAL);
1543     tcg_debug_assert(ts->temp_allocated != 0);
1544     ts->temp_allocated = 0;
1545
1546     idx = temp_idx(ts);
1547     k = ts->base_type + (ts->kind == TEMP_NORMAL ? 0 : TCG_TYPE_COUNT);
1548     set_bit(idx, s->free_temps[k].l);
1549 }
1550
1551 TCGTemp *tcg_constant_internal(TCGType type, int64_t val)
1552 {
1553     TCGContext *s = tcg_ctx;
1554     GHashTable *h = s->const_table[type];
1555     TCGTemp *ts;
1556
1557     if (h == NULL) {
1558         h = g_hash_table_new(g_int64_hash, g_int64_equal);
1559         s->const_table[type] = h;
1560     }
1561
1562     ts = g_hash_table_lookup(h, &val);
1563     if (ts == NULL) {
1564         ts = tcg_temp_alloc(s);
1565
1566         if (TCG_TARGET_REG_BITS == 32 && type == TCG_TYPE_I64) {
1567             TCGTemp *ts2 = tcg_temp_alloc(s);
1568
1569             ts->base_type = TCG_TYPE_I64;
1570             ts->type = TCG_TYPE_I32;
1571             ts->kind = TEMP_CONST;
1572             ts->temp_allocated = 1;
1573             /*
1574              * Retain the full value of the 64-bit constant in the low
1575              * part, so that the hash table works.  Actual uses will
1576              * truncate the value to the low part.
1577              */
1578             ts->val = val;
1579
1580             tcg_debug_assert(ts2 == ts + 1);
1581             ts2->base_type = TCG_TYPE_I64;
1582             ts2->type = TCG_TYPE_I32;
1583             ts2->kind = TEMP_CONST;
1584             ts2->temp_allocated = 1;
1585             ts2->val = val >> 32;
1586         } else {
1587             ts->base_type = type;
1588             ts->type = type;
1589             ts->kind = TEMP_CONST;
1590             ts->temp_allocated = 1;
1591             ts->val = val;
1592         }
1593         g_hash_table_insert(h, &ts->val, ts);
1594     }
1595
1596     return ts;
1597 }
1598
1599 TCGv_vec tcg_constant_vec(TCGType type, unsigned vece, int64_t val)
1600 {
1601     val = dup_const(vece, val);
1602     return temp_tcgv_vec(tcg_constant_internal(type, val));
1603 }
1604
1605 TCGv_vec tcg_constant_vec_matching(TCGv_vec match, unsigned vece, int64_t val)
1606 {
1607     TCGTemp *t = tcgv_vec_temp(match);
1608
1609     tcg_debug_assert(t->temp_allocated != 0);
1610     return tcg_constant_vec(t->base_type, vece, val);
1611 }
1612
1613 TCGv_i32 tcg_const_i32(int32_t val)
1614 {
1615     TCGv_i32 t0;
1616     t0 = tcg_temp_new_i32();
1617     tcg_gen_movi_i32(t0, val);
1618     return t0;
1619 }
1620
1621 TCGv_i64 tcg_const_i64(int64_t val)
1622 {
1623     TCGv_i64 t0;
1624     t0 = tcg_temp_new_i64();
1625     tcg_gen_movi_i64(t0, val);
1626     return t0;
1627 }
1628
1629 TCGv_i32 tcg_const_local_i32(int32_t val)
1630 {
1631     TCGv_i32 t0;
1632     t0 = tcg_temp_local_new_i32();
1633     tcg_gen_movi_i32(t0, val);
1634     return t0;
1635 }
1636
1637 TCGv_i64 tcg_const_local_i64(int64_t val)
1638 {
1639     TCGv_i64 t0;
1640     t0 = tcg_temp_local_new_i64();
1641     tcg_gen_movi_i64(t0, val);
1642     return t0;
1643 }
1644
1645 #if defined(CONFIG_DEBUG_TCG)
1646 void tcg_clear_temp_count(void)
1647 {
1648     TCGContext *s = tcg_ctx;
1649     s->temps_in_use = 0;
1650 }
1651
1652 int tcg_check_temp_count(void)
1653 {
1654     TCGContext *s = tcg_ctx;
1655     if (s->temps_in_use) {
1656         /* Clear the count so that we don't give another
1657          * warning immediately next time around.
1658          */
1659         s->temps_in_use = 0;
1660         return 1;
1661     }
1662     return 0;
1663 }
1664 #endif
1665
1666 /* Return true if OP may appear in the opcode stream.
1667    Test the runtime variable that controls each opcode.  */
1668 bool tcg_op_supported(TCGOpcode op)
1669 {
1670     const bool have_vec
1671         = TCG_TARGET_HAS_v64 | TCG_TARGET_HAS_v128 | TCG_TARGET_HAS_v256;
1672
1673     switch (op) {
1674     case INDEX_op_discard:
1675     case INDEX_op_set_label:
1676     case INDEX_op_call:
1677     case INDEX_op_br:
1678     case INDEX_op_mb:
1679     case INDEX_op_insn_start:
1680     case INDEX_op_exit_tb:
1681     case INDEX_op_goto_tb:
1682     case INDEX_op_qemu_ld_i32:
1683     case INDEX_op_qemu_st_i32:
1684     case INDEX_op_qemu_ld_i64:
1685     case INDEX_op_qemu_st_i64:
1686         return true;
1687
1688     case INDEX_op_qemu_st8_i32:
1689         return TCG_TARGET_HAS_qemu_st8_i32;
1690
1691     case INDEX_op_goto_ptr:
1692         return TCG_TARGET_HAS_goto_ptr;
1693
1694     case INDEX_op_mov_i32:
1695     case INDEX_op_setcond_i32:
1696     case INDEX_op_brcond_i32:
1697     case INDEX_op_ld8u_i32:
1698     case INDEX_op_ld8s_i32:
1699     case INDEX_op_ld16u_i32:
1700     case INDEX_op_ld16s_i32:
1701     case INDEX_op_ld_i32:
1702     case INDEX_op_st8_i32:
1703     case INDEX_op_st16_i32:
1704     case INDEX_op_st_i32:
1705     case INDEX_op_add_i32:
1706     case INDEX_op_sub_i32:
1707     case INDEX_op_mul_i32:
1708     case INDEX_op_and_i32:
1709     case INDEX_op_or_i32:
1710     case INDEX_op_xor_i32:
1711     case INDEX_op_shl_i32:
1712     case INDEX_op_shr_i32:
1713     case INDEX_op_sar_i32:
1714         return true;
1715
1716     case INDEX_op_movcond_i32:
1717         return TCG_TARGET_HAS_movcond_i32;
1718     case INDEX_op_div_i32:
1719     case INDEX_op_divu_i32:
1720         return TCG_TARGET_HAS_div_i32;
1721     case INDEX_op_rem_i32:
1722     case INDEX_op_remu_i32:
1723         return TCG_TARGET_HAS_rem_i32;
1724     case INDEX_op_div2_i32:
1725     case INDEX_op_divu2_i32:
1726         return TCG_TARGET_HAS_div2_i32;
1727     case INDEX_op_rotl_i32:
1728     case INDEX_op_rotr_i32:
1729         return TCG_TARGET_HAS_rot_i32;
1730     case INDEX_op_deposit_i32:
1731         return TCG_TARGET_HAS_deposit_i32;
1732     case INDEX_op_extract_i32:
1733         return TCG_TARGET_HAS_extract_i32;
1734     case INDEX_op_sextract_i32:
1735         return TCG_TARGET_HAS_sextract_i32;
1736     case INDEX_op_extract2_i32:
1737         return TCG_TARGET_HAS_extract2_i32;
1738     case INDEX_op_add2_i32:
1739         return TCG_TARGET_HAS_add2_i32;
1740     case INDEX_op_sub2_i32:
1741         return TCG_TARGET_HAS_sub2_i32;
1742     case INDEX_op_mulu2_i32:
1743         return TCG_TARGET_HAS_mulu2_i32;
1744     case INDEX_op_muls2_i32:
1745         return TCG_TARGET_HAS_muls2_i32;
1746     case INDEX_op_muluh_i32:
1747         return TCG_TARGET_HAS_muluh_i32;
1748     case INDEX_op_mulsh_i32:
1749         return TCG_TARGET_HAS_mulsh_i32;
1750     case INDEX_op_ext8s_i32:
1751         return TCG_TARGET_HAS_ext8s_i32;
1752     case INDEX_op_ext16s_i32:
1753         return TCG_TARGET_HAS_ext16s_i32;
1754     case INDEX_op_ext8u_i32:
1755         return TCG_TARGET_HAS_ext8u_i32;
1756     case INDEX_op_ext16u_i32:
1757         return TCG_TARGET_HAS_ext16u_i32;
1758     case INDEX_op_bswap16_i32:
1759         return TCG_TARGET_HAS_bswap16_i32;
1760     case INDEX_op_bswap32_i32:
1761         return TCG_TARGET_HAS_bswap32_i32;
1762     case INDEX_op_not_i32:
1763         return TCG_TARGET_HAS_not_i32;
1764     case INDEX_op_neg_i32:
1765         return TCG_TARGET_HAS_neg_i32;
1766     case INDEX_op_andc_i32:
1767         return TCG_TARGET_HAS_andc_i32;
1768     case INDEX_op_orc_i32:
1769         return TCG_TARGET_HAS_orc_i32;
1770     case INDEX_op_eqv_i32:
1771         return TCG_TARGET_HAS_eqv_i32;
1772     case INDEX_op_nand_i32:
1773         return TCG_TARGET_HAS_nand_i32;
1774     case INDEX_op_nor_i32:
1775         return TCG_TARGET_HAS_nor_i32;
1776     case INDEX_op_clz_i32:
1777         return TCG_TARGET_HAS_clz_i32;
1778     case INDEX_op_ctz_i32:
1779         return TCG_TARGET_HAS_ctz_i32;
1780     case INDEX_op_ctpop_i32:
1781         return TCG_TARGET_HAS_ctpop_i32;
1782
1783     case INDEX_op_brcond2_i32:
1784     case INDEX_op_setcond2_i32:
1785         return TCG_TARGET_REG_BITS == 32;
1786
1787     case INDEX_op_mov_i64:
1788     case INDEX_op_setcond_i64:
1789     case INDEX_op_brcond_i64:
1790     case INDEX_op_ld8u_i64:
1791     case INDEX_op_ld8s_i64:
1792     case INDEX_op_ld16u_i64:
1793     case INDEX_op_ld16s_i64:
1794     case INDEX_op_ld32u_i64:
1795     case INDEX_op_ld32s_i64:
1796     case INDEX_op_ld_i64:
1797     case INDEX_op_st8_i64:
1798     case INDEX_op_st16_i64:
1799     case INDEX_op_st32_i64:
1800     case INDEX_op_st_i64:
1801     case INDEX_op_add_i64:
1802     case INDEX_op_sub_i64:
1803     case INDEX_op_mul_i64:
1804     case INDEX_op_and_i64:
1805     case INDEX_op_or_i64:
1806     case INDEX_op_xor_i64:
1807     case INDEX_op_shl_i64:
1808     case INDEX_op_shr_i64:
1809     case INDEX_op_sar_i64:
1810     case INDEX_op_ext_i32_i64:
1811     case INDEX_op_extu_i32_i64:
1812         return TCG_TARGET_REG_BITS == 64;
1813
1814     case INDEX_op_movcond_i64:
1815         return TCG_TARGET_HAS_movcond_i64;
1816     case INDEX_op_div_i64:
1817     case INDEX_op_divu_i64:
1818         return TCG_TARGET_HAS_div_i64;
1819     case INDEX_op_rem_i64:
1820     case INDEX_op_remu_i64:
1821         return TCG_TARGET_HAS_rem_i64;
1822     case INDEX_op_div2_i64:
1823     case INDEX_op_divu2_i64:
1824         return TCG_TARGET_HAS_div2_i64;
1825     case INDEX_op_rotl_i64:
1826     case INDEX_op_rotr_i64:
1827         return TCG_TARGET_HAS_rot_i64;
1828     case INDEX_op_deposit_i64:
1829         return TCG_TARGET_HAS_deposit_i64;
1830     case INDEX_op_extract_i64:
1831         return TCG_TARGET_HAS_extract_i64;
1832     case INDEX_op_sextract_i64:
1833         return TCG_TARGET_HAS_sextract_i64;
1834     case INDEX_op_extract2_i64:
1835         return TCG_TARGET_HAS_extract2_i64;
1836     case INDEX_op_extrl_i64_i32:
1837         return TCG_TARGET_HAS_extrl_i64_i32;
1838     case INDEX_op_extrh_i64_i32:
1839         return TCG_TARGET_HAS_extrh_i64_i32;
1840     case INDEX_op_ext8s_i64:
1841         return TCG_TARGET_HAS_ext8s_i64;
1842     case INDEX_op_ext16s_i64:
1843         return TCG_TARGET_HAS_ext16s_i64;
1844     case INDEX_op_ext32s_i64:
1845         return TCG_TARGET_HAS_ext32s_i64;
1846     case INDEX_op_ext8u_i64:
1847         return TCG_TARGET_HAS_ext8u_i64;
1848     case INDEX_op_ext16u_i64:
1849         return TCG_TARGET_HAS_ext16u_i64;
1850     case INDEX_op_ext32u_i64:
1851         return TCG_TARGET_HAS_ext32u_i64;
1852     case INDEX_op_bswap16_i64:
1853         return TCG_TARGET_HAS_bswap16_i64;
1854     case INDEX_op_bswap32_i64:
1855         return TCG_TARGET_HAS_bswap32_i64;
1856     case INDEX_op_bswap64_i64:
1857         return TCG_TARGET_HAS_bswap64_i64;
1858     case INDEX_op_not_i64:
1859         return TCG_TARGET_HAS_not_i64;
1860     case INDEX_op_neg_i64:
1861         return TCG_TARGET_HAS_neg_i64;
1862     case INDEX_op_andc_i64:
1863         return TCG_TARGET_HAS_andc_i64;
1864     case INDEX_op_orc_i64:
1865         return TCG_TARGET_HAS_orc_i64;
1866     case INDEX_op_eqv_i64:
1867         return TCG_TARGET_HAS_eqv_i64;
1868     case INDEX_op_nand_i64:
1869         return TCG_TARGET_HAS_nand_i64;
1870     case INDEX_op_nor_i64:
1871         return TCG_TARGET_HAS_nor_i64;
1872     case INDEX_op_clz_i64:
1873         return TCG_TARGET_HAS_clz_i64;
1874     case INDEX_op_ctz_i64:
1875         return TCG_TARGET_HAS_ctz_i64;
1876     case INDEX_op_ctpop_i64:
1877         return TCG_TARGET_HAS_ctpop_i64;
1878     case INDEX_op_add2_i64:
1879         return TCG_TARGET_HAS_add2_i64;
1880     case INDEX_op_sub2_i64:
1881         return TCG_TARGET_HAS_sub2_i64;
1882     case INDEX_op_mulu2_i64:
1883         return TCG_TARGET_HAS_mulu2_i64;
1884     case INDEX_op_muls2_i64:
1885         return TCG_TARGET_HAS_muls2_i64;
1886     case INDEX_op_muluh_i64:
1887         return TCG_TARGET_HAS_muluh_i64;
1888     case INDEX_op_mulsh_i64:
1889         return TCG_TARGET_HAS_mulsh_i64;
1890
1891     case INDEX_op_mov_vec:
1892     case INDEX_op_dup_vec:
1893     case INDEX_op_dupm_vec:
1894     case INDEX_op_ld_vec:
1895     case INDEX_op_st_vec:
1896     case INDEX_op_add_vec:
1897     case INDEX_op_sub_vec:
1898     case INDEX_op_and_vec:
1899     case INDEX_op_or_vec:
1900     case INDEX_op_xor_vec:
1901     case INDEX_op_cmp_vec:
1902         return have_vec;
1903     case INDEX_op_dup2_vec:
1904         return have_vec && TCG_TARGET_REG_BITS == 32;
1905     case INDEX_op_not_vec:
1906         return have_vec && TCG_TARGET_HAS_not_vec;
1907     case INDEX_op_neg_vec:
1908         return have_vec && TCG_TARGET_HAS_neg_vec;
1909     case INDEX_op_abs_vec:
1910         return have_vec && TCG_TARGET_HAS_abs_vec;
1911     case INDEX_op_andc_vec:
1912         return have_vec && TCG_TARGET_HAS_andc_vec;
1913     case INDEX_op_orc_vec:
1914         return have_vec && TCG_TARGET_HAS_orc_vec;
1915     case INDEX_op_mul_vec:
1916         return have_vec && TCG_TARGET_HAS_mul_vec;
1917     case INDEX_op_shli_vec:
1918     case INDEX_op_shri_vec:
1919     case INDEX_op_sari_vec:
1920         return have_vec && TCG_TARGET_HAS_shi_vec;
1921     case INDEX_op_shls_vec:
1922     case INDEX_op_shrs_vec:
1923     case INDEX_op_sars_vec:
1924         return have_vec && TCG_TARGET_HAS_shs_vec;
1925     case INDEX_op_shlv_vec:
1926     case INDEX_op_shrv_vec:
1927     case INDEX_op_sarv_vec:
1928         return have_vec && TCG_TARGET_HAS_shv_vec;
1929     case INDEX_op_rotli_vec:
1930         return have_vec && TCG_TARGET_HAS_roti_vec;
1931     case INDEX_op_rotls_vec:
1932         return have_vec && TCG_TARGET_HAS_rots_vec;
1933     case INDEX_op_rotlv_vec:
1934     case INDEX_op_rotrv_vec:
1935         return have_vec && TCG_TARGET_HAS_rotv_vec;
1936     case INDEX_op_ssadd_vec:
1937     case INDEX_op_usadd_vec:
1938     case INDEX_op_sssub_vec:
1939     case INDEX_op_ussub_vec:
1940         return have_vec && TCG_TARGET_HAS_sat_vec;
1941     case INDEX_op_smin_vec:
1942     case INDEX_op_umin_vec:
1943     case INDEX_op_smax_vec:
1944     case INDEX_op_umax_vec:
1945         return have_vec && TCG_TARGET_HAS_minmax_vec;
1946     case INDEX_op_bitsel_vec:
1947         return have_vec && TCG_TARGET_HAS_bitsel_vec;
1948     case INDEX_op_cmpsel_vec:
1949         return have_vec && TCG_TARGET_HAS_cmpsel_vec;
1950
1951     default:
1952         tcg_debug_assert(op > INDEX_op_last_generic && op < NB_OPS);
1953         return true;
1954     }
1955 }
1956
1957 /* Note: we convert the 64 bit args to 32 bit and do some alignment
1958    and endian swap. Maybe it would be better to do the alignment
1959    and endian swap in tcg_reg_alloc_call(). */
1960 void tcg_gen_callN(void *func, TCGTemp *ret, int nargs, TCGTemp **args)
1961 {
1962     int i, real_args, nb_rets, pi;
1963     unsigned sizemask, flags;
1964     TCGHelperInfo *info;
1965     TCGOp *op;
1966
1967     info = g_hash_table_lookup(helper_table, (gpointer)func);
1968     flags = info->flags;
1969     sizemask = info->sizemask;
1970
1971 #ifdef CONFIG_PLUGIN
1972     /* detect non-plugin helpers */
1973     if (tcg_ctx->plugin_insn && unlikely(strncmp(info->name, "plugin_", 7))) {
1974         tcg_ctx->plugin_insn->calls_helpers = true;
1975     }
1976 #endif
1977
1978 #if defined(__sparc__) && !defined(__arch64__) \
1979     && !defined(CONFIG_TCG_INTERPRETER)
1980     /* We have 64-bit values in one register, but need to pass as two
1981        separate parameters.  Split them.  */
1982     int orig_sizemask = sizemask;
1983     int orig_nargs = nargs;
1984     TCGv_i64 retl, reth;
1985     TCGTemp *split_args[MAX_OPC_PARAM];
1986
1987     retl = NULL;
1988     reth = NULL;
1989     if (sizemask != 0) {
1990         for (i = real_args = 0; i < nargs; ++i) {
1991             int is_64bit = sizemask & (1 << (i+1)*2);
1992             if (is_64bit) {
1993                 TCGv_i64 orig = temp_tcgv_i64(args[i]);
1994                 TCGv_i32 h = tcg_temp_new_i32();
1995                 TCGv_i32 l = tcg_temp_new_i32();
1996                 tcg_gen_extr_i64_i32(l, h, orig);
1997                 split_args[real_args++] = tcgv_i32_temp(h);
1998                 split_args[real_args++] = tcgv_i32_temp(l);
1999             } else {
2000                 split_args[real_args++] = args[i];
2001             }
2002         }
2003         nargs = real_args;
2004         args = split_args;
2005         sizemask = 0;
2006     }
2007 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2008     for (i = 0; i < nargs; ++i) {
2009         int is_64bit = sizemask & (1 << (i+1)*2);
2010         int is_signed = sizemask & (2 << (i+1)*2);
2011         if (!is_64bit) {
2012             TCGv_i64 temp = tcg_temp_new_i64();
2013             TCGv_i64 orig = temp_tcgv_i64(args[i]);
2014             if (is_signed) {
2015                 tcg_gen_ext32s_i64(temp, orig);
2016             } else {
2017                 tcg_gen_ext32u_i64(temp, orig);
2018             }
2019             args[i] = tcgv_i64_temp(temp);
2020         }
2021     }
2022 #endif /* TCG_TARGET_EXTEND_ARGS */
2023
2024     op = tcg_emit_op(INDEX_op_call);
2025
2026     pi = 0;
2027     if (ret != NULL) {
2028 #if defined(__sparc__) && !defined(__arch64__) \
2029     && !defined(CONFIG_TCG_INTERPRETER)
2030         if (orig_sizemask & 1) {
2031             /* The 32-bit ABI is going to return the 64-bit value in
2032                the %o0/%o1 register pair.  Prepare for this by using
2033                two return temporaries, and reassemble below.  */
2034             retl = tcg_temp_new_i64();
2035             reth = tcg_temp_new_i64();
2036             op->args[pi++] = tcgv_i64_arg(reth);
2037             op->args[pi++] = tcgv_i64_arg(retl);
2038             nb_rets = 2;
2039         } else {
2040             op->args[pi++] = temp_arg(ret);
2041             nb_rets = 1;
2042         }
2043 #else
2044         if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
2045 #ifdef HOST_WORDS_BIGENDIAN
2046             op->args[pi++] = temp_arg(ret + 1);
2047             op->args[pi++] = temp_arg(ret);
2048 #else
2049             op->args[pi++] = temp_arg(ret);
2050             op->args[pi++] = temp_arg(ret + 1);
2051 #endif
2052             nb_rets = 2;
2053         } else {
2054             op->args[pi++] = temp_arg(ret);
2055             nb_rets = 1;
2056         }
2057 #endif
2058     } else {
2059         nb_rets = 0;
2060     }
2061     TCGOP_CALLO(op) = nb_rets;
2062
2063     real_args = 0;
2064     for (i = 0; i < nargs; i++) {
2065         int is_64bit = sizemask & (1 << (i+1)*2);
2066         if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
2067 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
2068             /* some targets want aligned 64 bit args */
2069             if (real_args & 1) {
2070                 op->args[pi++] = TCG_CALL_DUMMY_ARG;
2071                 real_args++;
2072             }
2073 #endif
2074            /* If stack grows up, then we will be placing successive
2075               arguments at lower addresses, which means we need to
2076               reverse the order compared to how we would normally
2077               treat either big or little-endian.  For those arguments
2078               that will wind up in registers, this still works for
2079               HPPA (the only current STACK_GROWSUP target) since the
2080               argument registers are *also* allocated in decreasing
2081               order.  If another such target is added, this logic may
2082               have to get more complicated to differentiate between
2083               stack arguments and register arguments.  */
2084 #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
2085             op->args[pi++] = temp_arg(args[i] + 1);
2086             op->args[pi++] = temp_arg(args[i]);
2087 #else
2088             op->args[pi++] = temp_arg(args[i]);
2089             op->args[pi++] = temp_arg(args[i] + 1);
2090 #endif
2091             real_args += 2;
2092             continue;
2093         }
2094
2095         op->args[pi++] = temp_arg(args[i]);
2096         real_args++;
2097     }
2098     op->args[pi++] = (uintptr_t)func;
2099     op->args[pi++] = flags;
2100     TCGOP_CALLI(op) = real_args;
2101
2102     /* Make sure the fields didn't overflow.  */
2103     tcg_debug_assert(TCGOP_CALLI(op) == real_args);
2104     tcg_debug_assert(pi <= ARRAY_SIZE(op->args));
2105
2106 #if defined(__sparc__) && !defined(__arch64__) \
2107     && !defined(CONFIG_TCG_INTERPRETER)
2108     /* Free all of the parts we allocated above.  */
2109     for (i = real_args = 0; i < orig_nargs; ++i) {
2110         int is_64bit = orig_sizemask & (1 << (i+1)*2);
2111         if (is_64bit) {
2112             tcg_temp_free_internal(args[real_args++]);
2113             tcg_temp_free_internal(args[real_args++]);
2114         } else {
2115             real_args++;
2116         }
2117     }
2118     if (orig_sizemask & 1) {
2119         /* The 32-bit ABI returned two 32-bit pieces.  Re-assemble them.
2120            Note that describing these as TCGv_i64 eliminates an unnecessary
2121            zero-extension that tcg_gen_concat_i32_i64 would create.  */
2122         tcg_gen_concat32_i64(temp_tcgv_i64(ret), retl, reth);
2123         tcg_temp_free_i64(retl);
2124         tcg_temp_free_i64(reth);
2125     }
2126 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2127     for (i = 0; i < nargs; ++i) {
2128         int is_64bit = sizemask & (1 << (i+1)*2);
2129         if (!is_64bit) {
2130             tcg_temp_free_internal(args[i]);
2131         }
2132     }
2133 #endif /* TCG_TARGET_EXTEND_ARGS */
2134 }
2135
2136 static void tcg_reg_alloc_start(TCGContext *s)
2137 {
2138     int i, n;
2139
2140     for (i = 0, n = s->nb_temps; i < n; i++) {
2141         TCGTemp *ts = &s->temps[i];
2142         TCGTempVal val = TEMP_VAL_MEM;
2143
2144         switch (ts->kind) {
2145         case TEMP_CONST:
2146             val = TEMP_VAL_CONST;
2147             break;
2148         case TEMP_FIXED:
2149             val = TEMP_VAL_REG;
2150             break;
2151         case TEMP_GLOBAL:
2152             break;
2153         case TEMP_NORMAL:
2154             val = TEMP_VAL_DEAD;
2155             /* fall through */
2156         case TEMP_LOCAL:
2157             ts->mem_allocated = 0;
2158             break;
2159         default:
2160             g_assert_not_reached();
2161         }
2162         ts->val_type = val;
2163     }
2164
2165     memset(s->reg_to_temp, 0, sizeof(s->reg_to_temp));
2166 }
2167
2168 static char *tcg_get_arg_str_ptr(TCGContext *s, char *buf, int buf_size,
2169                                  TCGTemp *ts)
2170 {
2171     int idx = temp_idx(ts);
2172
2173     switch (ts->kind) {
2174     case TEMP_FIXED:
2175     case TEMP_GLOBAL:
2176         pstrcpy(buf, buf_size, ts->name);
2177         break;
2178     case TEMP_LOCAL:
2179         snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
2180         break;
2181     case TEMP_NORMAL:
2182         snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
2183         break;
2184     case TEMP_CONST:
2185         switch (ts->type) {
2186         case TCG_TYPE_I32:
2187             snprintf(buf, buf_size, "$0x%x", (int32_t)ts->val);
2188             break;
2189 #if TCG_TARGET_REG_BITS > 32
2190         case TCG_TYPE_I64:
2191             snprintf(buf, buf_size, "$0x%" PRIx64, ts->val);
2192             break;
2193 #endif
2194         case TCG_TYPE_V64:
2195         case TCG_TYPE_V128:
2196         case TCG_TYPE_V256:
2197             snprintf(buf, buf_size, "v%d$0x%" PRIx64,
2198                      64 << (ts->type - TCG_TYPE_V64), ts->val);
2199             break;
2200         default:
2201             g_assert_not_reached();
2202         }
2203         break;
2204     }
2205     return buf;
2206 }
2207
2208 static char *tcg_get_arg_str(TCGContext *s, char *buf,
2209                              int buf_size, TCGArg arg)
2210 {
2211     return tcg_get_arg_str_ptr(s, buf, buf_size, arg_temp(arg));
2212 }
2213
2214 /* Find helper name.  */
2215 static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
2216 {
2217     const char *ret = NULL;
2218     if (helper_table) {
2219         TCGHelperInfo *info = g_hash_table_lookup(helper_table, (gpointer)val);
2220         if (info) {
2221             ret = info->name;
2222         }
2223     }
2224     return ret;
2225 }
2226
2227 static const char * const cond_name[] =
2228 {
2229     [TCG_COND_NEVER] = "never",
2230     [TCG_COND_ALWAYS] = "always",
2231     [TCG_COND_EQ] = "eq",
2232     [TCG_COND_NE] = "ne",
2233     [TCG_COND_LT] = "lt",
2234     [TCG_COND_GE] = "ge",
2235     [TCG_COND_LE] = "le",
2236     [TCG_COND_GT] = "gt",
2237     [TCG_COND_LTU] = "ltu",
2238     [TCG_COND_GEU] = "geu",
2239     [TCG_COND_LEU] = "leu",
2240     [TCG_COND_GTU] = "gtu"
2241 };
2242
2243 static const char * const ldst_name[] =
2244 {
2245     [MO_UB]   = "ub",
2246     [MO_SB]   = "sb",
2247     [MO_LEUW] = "leuw",
2248     [MO_LESW] = "lesw",
2249     [MO_LEUL] = "leul",
2250     [MO_LESL] = "lesl",
2251     [MO_LEQ]  = "leq",
2252     [MO_BEUW] = "beuw",
2253     [MO_BESW] = "besw",
2254     [MO_BEUL] = "beul",
2255     [MO_BESL] = "besl",
2256     [MO_BEQ]  = "beq",
2257 };
2258
2259 static const char * const alignment_name[(MO_AMASK >> MO_ASHIFT) + 1] = {
2260 #ifdef TARGET_ALIGNED_ONLY
2261     [MO_UNALN >> MO_ASHIFT]    = "un+",
2262     [MO_ALIGN >> MO_ASHIFT]    = "",
2263 #else
2264     [MO_UNALN >> MO_ASHIFT]    = "",
2265     [MO_ALIGN >> MO_ASHIFT]    = "al+",
2266 #endif
2267     [MO_ALIGN_2 >> MO_ASHIFT]  = "al2+",
2268     [MO_ALIGN_4 >> MO_ASHIFT]  = "al4+",
2269     [MO_ALIGN_8 >> MO_ASHIFT]  = "al8+",
2270     [MO_ALIGN_16 >> MO_ASHIFT] = "al16+",
2271     [MO_ALIGN_32 >> MO_ASHIFT] = "al32+",
2272     [MO_ALIGN_64 >> MO_ASHIFT] = "al64+",
2273 };
2274
2275 static inline bool tcg_regset_single(TCGRegSet d)
2276 {
2277     return (d & (d - 1)) == 0;
2278 }
2279
2280 static inline TCGReg tcg_regset_first(TCGRegSet d)
2281 {
2282     if (TCG_TARGET_NB_REGS <= 32) {
2283         return ctz32(d);
2284     } else {
2285         return ctz64(d);
2286     }
2287 }
2288
2289 static void tcg_dump_ops(TCGContext *s, bool have_prefs)
2290 {
2291     char buf[128];
2292     TCGOp *op;
2293
2294     QTAILQ_FOREACH(op, &s->ops, link) {
2295         int i, k, nb_oargs, nb_iargs, nb_cargs;
2296         const TCGOpDef *def;
2297         TCGOpcode c;
2298         int col = 0;
2299
2300         c = op->opc;
2301         def = &tcg_op_defs[c];
2302
2303         if (c == INDEX_op_insn_start) {
2304             nb_oargs = 0;
2305             col += qemu_log("\n ----");
2306
2307             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
2308                 target_ulong a;
2309 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2310                 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
2311 #else
2312                 a = op->args[i];
2313 #endif
2314                 col += qemu_log(" " TARGET_FMT_lx, a);
2315             }
2316         } else if (c == INDEX_op_call) {
2317             /* variable number of arguments */
2318             nb_oargs = TCGOP_CALLO(op);
2319             nb_iargs = TCGOP_CALLI(op);
2320             nb_cargs = def->nb_cargs;
2321
2322             /* function name, flags, out args */
2323             col += qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
2324                             tcg_find_helper(s, op->args[nb_oargs + nb_iargs]),
2325                             op->args[nb_oargs + nb_iargs + 1], nb_oargs);
2326             for (i = 0; i < nb_oargs; i++) {
2327                 col += qemu_log(",%s", tcg_get_arg_str(s, buf, sizeof(buf),
2328                                                        op->args[i]));
2329             }
2330             for (i = 0; i < nb_iargs; i++) {
2331                 TCGArg arg = op->args[nb_oargs + i];
2332                 const char *t = "<dummy>";
2333                 if (arg != TCG_CALL_DUMMY_ARG) {
2334                     t = tcg_get_arg_str(s, buf, sizeof(buf), arg);
2335                 }
2336                 col += qemu_log(",%s", t);
2337             }
2338         } else {
2339             col += qemu_log(" %s ", def->name);
2340
2341             nb_oargs = def->nb_oargs;
2342             nb_iargs = def->nb_iargs;
2343             nb_cargs = def->nb_cargs;
2344
2345             if (def->flags & TCG_OPF_VECTOR) {
2346                 col += qemu_log("v%d,e%d,", 64 << TCGOP_VECL(op),
2347                                 8 << TCGOP_VECE(op));
2348             }
2349
2350             k = 0;
2351             for (i = 0; i < nb_oargs; i++) {
2352                 if (k != 0) {
2353                     col += qemu_log(",");
2354                 }
2355                 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
2356                                                       op->args[k++]));
2357             }
2358             for (i = 0; i < nb_iargs; i++) {
2359                 if (k != 0) {
2360                     col += qemu_log(",");
2361                 }
2362                 col += qemu_log("%s", tcg_get_arg_str(s, buf, sizeof(buf),
2363                                                       op->args[k++]));
2364             }
2365             switch (c) {
2366             case INDEX_op_brcond_i32:
2367             case INDEX_op_setcond_i32:
2368             case INDEX_op_movcond_i32:
2369             case INDEX_op_brcond2_i32:
2370             case INDEX_op_setcond2_i32:
2371             case INDEX_op_brcond_i64:
2372             case INDEX_op_setcond_i64:
2373             case INDEX_op_movcond_i64:
2374             case INDEX_op_cmp_vec:
2375             case INDEX_op_cmpsel_vec:
2376                 if (op->args[k] < ARRAY_SIZE(cond_name)
2377                     && cond_name[op->args[k]]) {
2378                     col += qemu_log(",%s", cond_name[op->args[k++]]);
2379                 } else {
2380                     col += qemu_log(",$0x%" TCG_PRIlx, op->args[k++]);
2381                 }
2382                 i = 1;
2383                 break;
2384             case INDEX_op_qemu_ld_i32:
2385             case INDEX_op_qemu_st_i32:
2386             case INDEX_op_qemu_st8_i32:
2387             case INDEX_op_qemu_ld_i64:
2388             case INDEX_op_qemu_st_i64:
2389                 {
2390                     TCGMemOpIdx oi = op->args[k++];
2391                     MemOp op = get_memop(oi);
2392                     unsigned ix = get_mmuidx(oi);
2393
2394                     if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
2395                         col += qemu_log(",$0x%x,%u", op, ix);
2396                     } else {
2397                         const char *s_al, *s_op;
2398                         s_al = alignment_name[(op & MO_AMASK) >> MO_ASHIFT];
2399                         s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
2400                         col += qemu_log(",%s%s,%u", s_al, s_op, ix);
2401                     }
2402                     i = 1;
2403                 }
2404                 break;
2405             default:
2406                 i = 0;
2407                 break;
2408             }
2409             switch (c) {
2410             case INDEX_op_set_label:
2411             case INDEX_op_br:
2412             case INDEX_op_brcond_i32:
2413             case INDEX_op_brcond_i64:
2414             case INDEX_op_brcond2_i32:
2415                 col += qemu_log("%s$L%d", k ? "," : "",
2416                                 arg_label(op->args[k])->id);
2417                 i++, k++;
2418                 break;
2419             default:
2420                 break;
2421             }
2422             for (; i < nb_cargs; i++, k++) {
2423                 col += qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", op->args[k]);
2424             }
2425         }
2426
2427         if (have_prefs || op->life) {
2428
2429             QemuLogFile *logfile;
2430
2431             rcu_read_lock();
2432             logfile = qatomic_rcu_read(&qemu_logfile);
2433             if (logfile) {
2434                 for (; col < 40; ++col) {
2435                     putc(' ', logfile->fd);
2436                 }
2437             }
2438             rcu_read_unlock();
2439         }
2440
2441         if (op->life) {
2442             unsigned life = op->life;
2443
2444             if (life & (SYNC_ARG * 3)) {
2445                 qemu_log("  sync:");
2446                 for (i = 0; i < 2; ++i) {
2447                     if (life & (SYNC_ARG << i)) {
2448                         qemu_log(" %d", i);
2449                     }
2450                 }
2451             }
2452             life /= DEAD_ARG;
2453             if (life) {
2454                 qemu_log("  dead:");
2455                 for (i = 0; life; ++i, life >>= 1) {
2456                     if (life & 1) {
2457                         qemu_log(" %d", i);
2458                     }
2459                 }
2460             }
2461         }
2462
2463         if (have_prefs) {
2464             for (i = 0; i < nb_oargs; ++i) {
2465                 TCGRegSet set = op->output_pref[i];
2466
2467                 if (i == 0) {
2468                     qemu_log("  pref=");
2469                 } else {
2470                     qemu_log(",");
2471                 }
2472                 if (set == 0) {
2473                     qemu_log("none");
2474                 } else if (set == MAKE_64BIT_MASK(0, TCG_TARGET_NB_REGS)) {
2475                     qemu_log("all");
2476 #ifdef CONFIG_DEBUG_TCG
2477                 } else if (tcg_regset_single(set)) {
2478                     TCGReg reg = tcg_regset_first(set);
2479                     qemu_log("%s", tcg_target_reg_names[reg]);
2480 #endif
2481                 } else if (TCG_TARGET_NB_REGS <= 32) {
2482                     qemu_log("%#x", (uint32_t)set);
2483                 } else {
2484                     qemu_log("%#" PRIx64, (uint64_t)set);
2485                 }
2486             }
2487         }
2488
2489         qemu_log("\n");
2490     }
2491 }
2492
2493 /* we give more priority to constraints with less registers */
2494 static int get_constraint_priority(const TCGOpDef *def, int k)
2495 {
2496     const TCGArgConstraint *arg_ct = &def->args_ct[k];
2497     int n;
2498
2499     if (arg_ct->oalias) {
2500         /* an alias is equivalent to a single register */
2501         n = 1;
2502     } else {
2503         n = ctpop64(arg_ct->regs);
2504     }
2505     return TCG_TARGET_NB_REGS - n + 1;
2506 }
2507
2508 /* sort from highest priority to lowest */
2509 static void sort_constraints(TCGOpDef *def, int start, int n)
2510 {
2511     int i, j;
2512     TCGArgConstraint *a = def->args_ct;
2513
2514     for (i = 0; i < n; i++) {
2515         a[start + i].sort_index = start + i;
2516     }
2517     if (n <= 1) {
2518         return;
2519     }
2520     for (i = 0; i < n - 1; i++) {
2521         for (j = i + 1; j < n; j++) {
2522             int p1 = get_constraint_priority(def, a[start + i].sort_index);
2523             int p2 = get_constraint_priority(def, a[start + j].sort_index);
2524             if (p1 < p2) {
2525                 int tmp = a[start + i].sort_index;
2526                 a[start + i].sort_index = a[start + j].sort_index;
2527                 a[start + j].sort_index = tmp;
2528             }
2529         }
2530     }
2531 }
2532
2533 static void process_op_defs(TCGContext *s)
2534 {
2535     TCGOpcode op;
2536
2537     for (op = 0; op < NB_OPS; op++) {
2538         TCGOpDef *def = &tcg_op_defs[op];
2539         const TCGTargetOpDef *tdefs;
2540         int i, nb_args;
2541
2542         if (def->flags & TCG_OPF_NOT_PRESENT) {
2543             continue;
2544         }
2545
2546         nb_args = def->nb_iargs + def->nb_oargs;
2547         if (nb_args == 0) {
2548             continue;
2549         }
2550
2551         /*
2552          * Macro magic should make it impossible, but double-check that
2553          * the array index is in range.  Since the signness of an enum
2554          * is implementation defined, force the result to unsigned.
2555          */
2556         unsigned con_set = tcg_target_op_def(op);
2557         tcg_debug_assert(con_set < ARRAY_SIZE(constraint_sets));
2558         tdefs = &constraint_sets[con_set];
2559
2560         for (i = 0; i < nb_args; i++) {
2561             const char *ct_str = tdefs->args_ct_str[i];
2562             /* Incomplete TCGTargetOpDef entry. */
2563             tcg_debug_assert(ct_str != NULL);
2564
2565             while (*ct_str != '\0') {
2566                 switch(*ct_str) {
2567                 case '0' ... '9':
2568                     {
2569                         int oarg = *ct_str - '0';
2570                         tcg_debug_assert(ct_str == tdefs->args_ct_str[i]);
2571                         tcg_debug_assert(oarg < def->nb_oargs);
2572                         tcg_debug_assert(def->args_ct[oarg].regs != 0);
2573                         def->args_ct[i] = def->args_ct[oarg];
2574                         /* The output sets oalias.  */
2575                         def->args_ct[oarg].oalias = true;
2576                         def->args_ct[oarg].alias_index = i;
2577                         /* The input sets ialias. */
2578                         def->args_ct[i].ialias = true;
2579                         def->args_ct[i].alias_index = oarg;
2580                     }
2581                     ct_str++;
2582                     break;
2583                 case '&':
2584                     def->args_ct[i].newreg = true;
2585                     ct_str++;
2586                     break;
2587                 case 'i':
2588                     def->args_ct[i].ct |= TCG_CT_CONST;
2589                     ct_str++;
2590                     break;
2591
2592                 /* Include all of the target-specific constraints. */
2593
2594 #undef CONST
2595 #define CONST(CASE, MASK) \
2596     case CASE: def->args_ct[i].ct |= MASK; ct_str++; break;
2597 #define REGS(CASE, MASK) \
2598     case CASE: def->args_ct[i].regs |= MASK; ct_str++; break;
2599
2600 #include "tcg-target-con-str.h"
2601
2602 #undef REGS
2603 #undef CONST
2604                 default:
2605                     /* Typo in TCGTargetOpDef constraint. */
2606                     g_assert_not_reached();
2607                 }
2608             }
2609         }
2610
2611         /* TCGTargetOpDef entry with too much information? */
2612         tcg_debug_assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
2613
2614         /* sort the constraints (XXX: this is just an heuristic) */
2615         sort_constraints(def, 0, def->nb_oargs);
2616         sort_constraints(def, def->nb_oargs, def->nb_iargs);
2617     }
2618 }
2619
2620 void tcg_op_remove(TCGContext *s, TCGOp *op)
2621 {
2622     TCGLabel *label;
2623
2624     switch (op->opc) {
2625     case INDEX_op_br:
2626         label = arg_label(op->args[0]);
2627         label->refs--;
2628         break;
2629     case INDEX_op_brcond_i32:
2630     case INDEX_op_brcond_i64:
2631         label = arg_label(op->args[3]);
2632         label->refs--;
2633         break;
2634     case INDEX_op_brcond2_i32:
2635         label = arg_label(op->args[5]);
2636         label->refs--;
2637         break;
2638     default:
2639         break;
2640     }
2641
2642     QTAILQ_REMOVE(&s->ops, op, link);
2643     QTAILQ_INSERT_TAIL(&s->free_ops, op, link);
2644     s->nb_ops--;
2645
2646 #ifdef CONFIG_PROFILER
2647     qatomic_set(&s->prof.del_op_count, s->prof.del_op_count + 1);
2648 #endif
2649 }
2650
2651 static TCGOp *tcg_op_alloc(TCGOpcode opc)
2652 {
2653     TCGContext *s = tcg_ctx;
2654     TCGOp *op;
2655
2656     if (likely(QTAILQ_EMPTY(&s->free_ops))) {
2657         op = tcg_malloc(sizeof(TCGOp));
2658     } else {
2659         op = QTAILQ_FIRST(&s->free_ops);
2660         QTAILQ_REMOVE(&s->free_ops, op, link);
2661     }
2662     memset(op, 0, offsetof(TCGOp, link));
2663     op->opc = opc;
2664     s->nb_ops++;
2665
2666     return op;
2667 }
2668
2669 TCGOp *tcg_emit_op(TCGOpcode opc)
2670 {
2671     TCGOp *op = tcg_op_alloc(opc);
2672     QTAILQ_INSERT_TAIL(&tcg_ctx->ops, op, link);
2673     return op;
2674 }
2675
2676 TCGOp *tcg_op_insert_before(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
2677 {
2678     TCGOp *new_op = tcg_op_alloc(opc);
2679     QTAILQ_INSERT_BEFORE(old_op, new_op, link);
2680     return new_op;
2681 }
2682
2683 TCGOp *tcg_op_insert_after(TCGContext *s, TCGOp *old_op, TCGOpcode opc)
2684 {
2685     TCGOp *new_op = tcg_op_alloc(opc);
2686     QTAILQ_INSERT_AFTER(&s->ops, old_op, new_op, link);
2687     return new_op;
2688 }
2689
2690 /* Reachable analysis : remove unreachable code.  */
2691 static void reachable_code_pass(TCGContext *s)
2692 {
2693     TCGOp *op, *op_next;
2694     bool dead = false;
2695
2696     QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
2697         bool remove = dead;
2698         TCGLabel *label;
2699         int call_flags;
2700
2701         switch (op->opc) {
2702         case INDEX_op_set_label:
2703             label = arg_label(op->args[0]);
2704             if (label->refs == 0) {
2705                 /*
2706                  * While there is an occasional backward branch, virtually
2707                  * all branches generated by the translators are forward.
2708                  * Which means that generally we will have already removed
2709                  * all references to the label that will be, and there is
2710                  * little to be gained by iterating.
2711                  */
2712                 remove = true;
2713             } else {
2714                 /* Once we see a label, insns become live again.  */
2715                 dead = false;
2716                 remove = false;
2717
2718                 /*
2719                  * Optimization can fold conditional branches to unconditional.
2720                  * If we find a label with one reference which is preceded by
2721                  * an unconditional branch to it, remove both.  This needed to
2722                  * wait until the dead code in between them was removed.
2723                  */
2724                 if (label->refs == 1) {
2725                     TCGOp *op_prev = QTAILQ_PREV(op, link);
2726                     if (op_prev->opc == INDEX_op_br &&
2727                         label == arg_label(op_prev->args[0])) {
2728                         tcg_op_remove(s, op_prev);
2729                         remove = true;
2730                     }
2731                 }
2732             }
2733             break;
2734
2735         case INDEX_op_br:
2736         case INDEX_op_exit_tb:
2737         case INDEX_op_goto_ptr:
2738             /* Unconditional branches; everything following is dead.  */
2739             dead = true;
2740             break;
2741
2742         case INDEX_op_call:
2743             /* Notice noreturn helper calls, raising exceptions.  */
2744             call_flags = op->args[TCGOP_CALLO(op) + TCGOP_CALLI(op) + 1];
2745             if (call_flags & TCG_CALL_NO_RETURN) {
2746                 dead = true;
2747             }
2748             break;
2749
2750         case INDEX_op_insn_start:
2751             /* Never remove -- we need to keep these for unwind.  */
2752             remove = false;
2753             break;
2754
2755         default:
2756             break;
2757         }
2758
2759         if (remove) {
2760             tcg_op_remove(s, op);
2761         }
2762     }
2763 }
2764
2765 #define TS_DEAD  1
2766 #define TS_MEM   2
2767
2768 #define IS_DEAD_ARG(n)   (arg_life & (DEAD_ARG << (n)))
2769 #define NEED_SYNC_ARG(n) (arg_life & (SYNC_ARG << (n)))
2770
2771 /* For liveness_pass_1, the register preferences for a given temp.  */
2772 static inline TCGRegSet *la_temp_pref(TCGTemp *ts)
2773 {
2774     return ts->state_ptr;
2775 }
2776
2777 /* For liveness_pass_1, reset the preferences for a given temp to the
2778  * maximal regset for its type.
2779  */
2780 static inline void la_reset_pref(TCGTemp *ts)
2781 {
2782     *la_temp_pref(ts)
2783         = (ts->state == TS_DEAD ? 0 : tcg_target_available_regs[ts->type]);
2784 }
2785
2786 /* liveness analysis: end of function: all temps are dead, and globals
2787    should be in memory. */
2788 static void la_func_end(TCGContext *s, int ng, int nt)
2789 {
2790     int i;
2791
2792     for (i = 0; i < ng; ++i) {
2793         s->temps[i].state = TS_DEAD | TS_MEM;
2794         la_reset_pref(&s->temps[i]);
2795     }
2796     for (i = ng; i < nt; ++i) {
2797         s->temps[i].state = TS_DEAD;
2798         la_reset_pref(&s->temps[i]);
2799     }
2800 }
2801
2802 /* liveness analysis: end of basic block: all temps are dead, globals
2803    and local temps should be in memory. */
2804 static void la_bb_end(TCGContext *s, int ng, int nt)
2805 {
2806     int i;
2807
2808     for (i = 0; i < nt; ++i) {
2809         TCGTemp *ts = &s->temps[i];
2810         int state;
2811
2812         switch (ts->kind) {
2813         case TEMP_FIXED:
2814         case TEMP_GLOBAL:
2815         case TEMP_LOCAL:
2816             state = TS_DEAD | TS_MEM;
2817             break;
2818         case TEMP_NORMAL:
2819         case TEMP_CONST:
2820             state = TS_DEAD;
2821             break;
2822         default:
2823             g_assert_not_reached();
2824         }
2825         ts->state = state;
2826         la_reset_pref(ts);
2827     }
2828 }
2829
2830 /* liveness analysis: sync globals back to memory.  */
2831 static void la_global_sync(TCGContext *s, int ng)
2832 {
2833     int i;
2834
2835     for (i = 0; i < ng; ++i) {
2836         int state = s->temps[i].state;
2837         s->temps[i].state = state | TS_MEM;
2838         if (state == TS_DEAD) {
2839             /* If the global was previously dead, reset prefs.  */
2840             la_reset_pref(&s->temps[i]);
2841         }
2842     }
2843 }
2844
2845 /*
2846  * liveness analysis: conditional branch: all temps are dead,
2847  * globals and local temps should be synced.
2848  */
2849 static void la_bb_sync(TCGContext *s, int ng, int nt)
2850 {
2851     la_global_sync(s, ng);
2852
2853     for (int i = ng; i < nt; ++i) {
2854         TCGTemp *ts = &s->temps[i];
2855         int state;
2856
2857         switch (ts->kind) {
2858         case TEMP_LOCAL:
2859             state = ts->state;
2860             ts->state = state | TS_MEM;
2861             if (state != TS_DEAD) {
2862                 continue;
2863             }
2864             break;
2865         case TEMP_NORMAL:
2866             s->temps[i].state = TS_DEAD;
2867             break;
2868         case TEMP_CONST:
2869             continue;
2870         default:
2871             g_assert_not_reached();
2872         }
2873         la_reset_pref(&s->temps[i]);
2874     }
2875 }
2876
2877 /* liveness analysis: sync globals back to memory and kill.  */
2878 static void la_global_kill(TCGContext *s, int ng)
2879 {
2880     int i;
2881
2882     for (i = 0; i < ng; i++) {
2883         s->temps[i].state = TS_DEAD | TS_MEM;
2884         la_reset_pref(&s->temps[i]);
2885     }
2886 }
2887
2888 /* liveness analysis: note live globals crossing calls.  */
2889 static void la_cross_call(TCGContext *s, int nt)
2890 {
2891     TCGRegSet mask = ~tcg_target_call_clobber_regs;
2892     int i;
2893
2894     for (i = 0; i < nt; i++) {
2895         TCGTemp *ts = &s->temps[i];
2896         if (!(ts->state & TS_DEAD)) {
2897             TCGRegSet *pset = la_temp_pref(ts);
2898             TCGRegSet set = *pset;
2899
2900             set &= mask;
2901             /* If the combination is not possible, restart.  */
2902             if (set == 0) {
2903                 set = tcg_target_available_regs[ts->type] & mask;
2904             }
2905             *pset = set;
2906         }
2907     }
2908 }
2909
2910 /* Liveness analysis : update the opc_arg_life array to tell if a
2911    given input arguments is dead. Instructions updating dead
2912    temporaries are removed. */
2913 static void liveness_pass_1(TCGContext *s)
2914 {
2915     int nb_globals = s->nb_globals;
2916     int nb_temps = s->nb_temps;
2917     TCGOp *op, *op_prev;
2918     TCGRegSet *prefs;
2919     int i;
2920
2921     prefs = tcg_malloc(sizeof(TCGRegSet) * nb_temps);
2922     for (i = 0; i < nb_temps; ++i) {
2923         s->temps[i].state_ptr = prefs + i;
2924     }
2925
2926     /* ??? Should be redundant with the exit_tb that ends the TB.  */
2927     la_func_end(s, nb_globals, nb_temps);
2928
2929     QTAILQ_FOREACH_REVERSE_SAFE(op, &s->ops, link, op_prev) {
2930         int nb_iargs, nb_oargs;
2931         TCGOpcode opc_new, opc_new2;
2932         bool have_opc_new2;
2933         TCGLifeData arg_life = 0;
2934         TCGTemp *ts;
2935         TCGOpcode opc = op->opc;
2936         const TCGOpDef *def = &tcg_op_defs[opc];
2937
2938         switch (opc) {
2939         case INDEX_op_call:
2940             {
2941                 int call_flags;
2942                 int nb_call_regs;
2943
2944                 nb_oargs = TCGOP_CALLO(op);
2945                 nb_iargs = TCGOP_CALLI(op);
2946                 call_flags = op->args[nb_oargs + nb_iargs + 1];
2947
2948                 /* pure functions can be removed if their result is unused */
2949                 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
2950                     for (i = 0; i < nb_oargs; i++) {
2951                         ts = arg_temp(op->args[i]);
2952                         if (ts->state != TS_DEAD) {
2953                             goto do_not_remove_call;
2954                         }
2955                     }
2956                     goto do_remove;
2957                 }
2958             do_not_remove_call:
2959
2960                 /* Output args are dead.  */
2961                 for (i = 0; i < nb_oargs; i++) {
2962                     ts = arg_temp(op->args[i]);
2963                     if (ts->state & TS_DEAD) {
2964                         arg_life |= DEAD_ARG << i;
2965                     }
2966                     if (ts->state & TS_MEM) {
2967                         arg_life |= SYNC_ARG << i;
2968                     }
2969                     ts->state = TS_DEAD;
2970                     la_reset_pref(ts);
2971
2972                     /* Not used -- it will be tcg_target_call_oarg_regs[i].  */
2973                     op->output_pref[i] = 0;
2974                 }
2975
2976                 if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
2977                                     TCG_CALL_NO_READ_GLOBALS))) {
2978                     la_global_kill(s, nb_globals);
2979                 } else if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
2980                     la_global_sync(s, nb_globals);
2981                 }
2982
2983                 /* Record arguments that die in this helper.  */
2984                 for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2985                     ts = arg_temp(op->args[i]);
2986                     if (ts && ts->state & TS_DEAD) {
2987                         arg_life |= DEAD_ARG << i;
2988                     }
2989                 }
2990
2991                 /* For all live registers, remove call-clobbered prefs.  */
2992                 la_cross_call(s, nb_temps);
2993
2994                 nb_call_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2995
2996                 /* Input arguments are live for preceding opcodes.  */
2997                 for (i = 0; i < nb_iargs; i++) {
2998                     ts = arg_temp(op->args[i + nb_oargs]);
2999                     if (ts && ts->state & TS_DEAD) {
3000                         /* For those arguments that die, and will be allocated
3001                          * in registers, clear the register set for that arg,
3002                          * to be filled in below.  For args that will be on
3003                          * the stack, reset to any available reg.
3004                          */
3005                         *la_temp_pref(ts)
3006                             = (i < nb_call_regs ? 0 :
3007                                tcg_target_available_regs[ts->type]);
3008                         ts->state &= ~TS_DEAD;
3009                     }
3010                 }
3011
3012                 /* For each input argument, add its input register to prefs.
3013                    If a temp is used once, this produces a single set bit.  */
3014                 for (i = 0; i < MIN(nb_call_regs, nb_iargs); i++) {
3015                     ts = arg_temp(op->args[i + nb_oargs]);
3016                     if (ts) {
3017                         tcg_regset_set_reg(*la_temp_pref(ts),
3018                                            tcg_target_call_iarg_regs[i]);
3019                     }
3020                 }
3021             }
3022             break;
3023         case INDEX_op_insn_start:
3024             break;
3025         case INDEX_op_discard:
3026             /* mark the temporary as dead */
3027             ts = arg_temp(op->args[0]);
3028             ts->state = TS_DEAD;
3029             la_reset_pref(ts);
3030             break;
3031
3032         case INDEX_op_add2_i32:
3033             opc_new = INDEX_op_add_i32;
3034             goto do_addsub2;
3035         case INDEX_op_sub2_i32:
3036             opc_new = INDEX_op_sub_i32;
3037             goto do_addsub2;
3038         case INDEX_op_add2_i64:
3039             opc_new = INDEX_op_add_i64;
3040             goto do_addsub2;
3041         case INDEX_op_sub2_i64:
3042             opc_new = INDEX_op_sub_i64;
3043         do_addsub2:
3044             nb_iargs = 4;
3045             nb_oargs = 2;
3046             /* Test if the high part of the operation is dead, but not
3047                the low part.  The result can be optimized to a simple
3048                add or sub.  This happens often for x86_64 guest when the
3049                cpu mode is set to 32 bit.  */
3050             if (arg_temp(op->args[1])->state == TS_DEAD) {
3051                 if (arg_temp(op->args[0])->state == TS_DEAD) {
3052                     goto do_remove;
3053                 }
3054                 /* Replace the opcode and adjust the args in place,
3055                    leaving 3 unused args at the end.  */
3056                 op->opc = opc = opc_new;
3057                 op->args[1] = op->args[2];
3058                 op->args[2] = op->args[4];
3059                 /* Fall through and mark the single-word operation live.  */
3060                 nb_iargs = 2;
3061                 nb_oargs = 1;
3062             }
3063             goto do_not_remove;
3064
3065         case INDEX_op_mulu2_i32:
3066             opc_new = INDEX_op_mul_i32;
3067             opc_new2 = INDEX_op_muluh_i32;
3068             have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
3069             goto do_mul2;
3070         case INDEX_op_muls2_i32:
3071             opc_new = INDEX_op_mul_i32;
3072             opc_new2 = INDEX_op_mulsh_i32;
3073             have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
3074             goto do_mul2;
3075         case INDEX_op_mulu2_i64:
3076             opc_new = INDEX_op_mul_i64;
3077             opc_new2 = INDEX_op_muluh_i64;
3078             have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
3079             goto do_mul2;
3080         case INDEX_op_muls2_i64:
3081             opc_new = INDEX_op_mul_i64;
3082             opc_new2 = INDEX_op_mulsh_i64;
3083             have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
3084             goto do_mul2;
3085         do_mul2:
3086             nb_iargs = 2;
3087             nb_oargs = 2;
3088             if (arg_temp(op->args[1])->state == TS_DEAD) {
3089                 if (arg_temp(op->args[0])->state == TS_DEAD) {
3090                     /* Both parts of the operation are dead.  */
3091                     goto do_remove;
3092                 }
3093                 /* The high part of the operation is dead; generate the low. */
3094                 op->opc = opc = opc_new;
3095                 op->args[1] = op->args[2];
3096                 op->args[2] = op->args[3];
3097             } else if (arg_temp(op->args[0])->state == TS_DEAD && have_opc_new2) {
3098                 /* The low part of the operation is dead; generate the high. */
3099                 op->opc = opc = opc_new2;
3100                 op->args[0] = op->args[1];
3101                 op->args[1] = op->args[2];
3102                 op->args[2] = op->args[3];
3103             } else {
3104                 goto do_not_remove;
3105             }
3106             /* Mark the single-word operation live.  */
3107             nb_oargs = 1;
3108             goto do_not_remove;
3109
3110         default:
3111             /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
3112             nb_iargs = def->nb_iargs;
3113             nb_oargs = def->nb_oargs;
3114
3115             /* Test if the operation can be removed because all
3116                its outputs are dead. We assume that nb_oargs == 0
3117                implies side effects */
3118             if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
3119                 for (i = 0; i < nb_oargs; i++) {
3120                     if (arg_temp(op->args[i])->state != TS_DEAD) {
3121                         goto do_not_remove;
3122                     }
3123                 }
3124                 goto do_remove;
3125             }
3126             goto do_not_remove;
3127
3128         do_remove:
3129             tcg_op_remove(s, op);
3130             break;
3131
3132         do_not_remove:
3133             for (i = 0; i < nb_oargs; i++) {
3134                 ts = arg_temp(op->args[i]);
3135
3136                 /* Remember the preference of the uses that followed.  */
3137                 op->output_pref[i] = *la_temp_pref(ts);
3138
3139                 /* Output args are dead.  */
3140                 if (ts->state & TS_DEAD) {
3141                     arg_life |= DEAD_ARG << i;
3142                 }
3143                 if (ts->state & TS_MEM) {
3144                     arg_life |= SYNC_ARG << i;
3145                 }
3146                 ts->state = TS_DEAD;
3147                 la_reset_pref(ts);
3148             }
3149
3150             /* If end of basic block, update.  */
3151             if (def->flags & TCG_OPF_BB_EXIT) {
3152                 la_func_end(s, nb_globals, nb_temps);
3153             } else if (def->flags & TCG_OPF_COND_BRANCH) {
3154                 la_bb_sync(s, nb_globals, nb_temps);
3155             } else if (def->flags & TCG_OPF_BB_END) {
3156                 la_bb_end(s, nb_globals, nb_temps);
3157             } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3158                 la_global_sync(s, nb_globals);
3159                 if (def->flags & TCG_OPF_CALL_CLOBBER) {
3160                     la_cross_call(s, nb_temps);
3161                 }
3162             }
3163
3164             /* Record arguments that die in this opcode.  */
3165             for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3166                 ts = arg_temp(op->args[i]);
3167                 if (ts->state & TS_DEAD) {
3168                     arg_life |= DEAD_ARG << i;
3169                 }
3170             }
3171
3172             /* Input arguments are live for preceding opcodes.  */
3173             for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3174                 ts = arg_temp(op->args[i]);
3175                 if (ts->state & TS_DEAD) {
3176                     /* For operands that were dead, initially allow
3177                        all regs for the type.  */
3178                     *la_temp_pref(ts) = tcg_target_available_regs[ts->type];
3179                     ts->state &= ~TS_DEAD;
3180                 }
3181             }
3182
3183             /* Incorporate constraints for this operand.  */
3184             switch (opc) {
3185             case INDEX_op_mov_i32:
3186             case INDEX_op_mov_i64:
3187                 /* Note that these are TCG_OPF_NOT_PRESENT and do not
3188                    have proper constraints.  That said, special case
3189                    moves to propagate preferences backward.  */
3190                 if (IS_DEAD_ARG(1)) {
3191                     *la_temp_pref(arg_temp(op->args[0]))
3192                         = *la_temp_pref(arg_temp(op->args[1]));
3193                 }
3194                 break;
3195
3196             default:
3197                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
3198                     const TCGArgConstraint *ct = &def->args_ct[i];
3199                     TCGRegSet set, *pset;
3200
3201                     ts = arg_temp(op->args[i]);
3202                     pset = la_temp_pref(ts);
3203                     set = *pset;
3204
3205                     set &= ct->regs;
3206                     if (ct->ialias) {
3207                         set &= op->output_pref[ct->alias_index];
3208                     }
3209                     /* If the combination is not possible, restart.  */
3210                     if (set == 0) {
3211                         set = ct->regs;
3212                     }
3213                     *pset = set;
3214                 }
3215                 break;
3216             }
3217             break;
3218         }
3219         op->life = arg_life;
3220     }
3221 }
3222
3223 /* Liveness analysis: Convert indirect regs to direct temporaries.  */
3224 static bool liveness_pass_2(TCGContext *s)
3225 {
3226     int nb_globals = s->nb_globals;
3227     int nb_temps, i;
3228     bool changes = false;
3229     TCGOp *op, *op_next;
3230
3231     /* Create a temporary for each indirect global.  */
3232     for (i = 0; i < nb_globals; ++i) {
3233         TCGTemp *its = &s->temps[i];
3234         if (its->indirect_reg) {
3235             TCGTemp *dts = tcg_temp_alloc(s);
3236             dts->type = its->type;
3237             dts->base_type = its->base_type;
3238             its->state_ptr = dts;
3239         } else {
3240             its->state_ptr = NULL;
3241         }
3242         /* All globals begin dead.  */
3243         its->state = TS_DEAD;
3244     }
3245     for (nb_temps = s->nb_temps; i < nb_temps; ++i) {
3246         TCGTemp *its = &s->temps[i];
3247         its->state_ptr = NULL;
3248         its->state = TS_DEAD;
3249     }
3250
3251     QTAILQ_FOREACH_SAFE(op, &s->ops, link, op_next) {
3252         TCGOpcode opc = op->opc;
3253         const TCGOpDef *def = &tcg_op_defs[opc];
3254         TCGLifeData arg_life = op->life;
3255         int nb_iargs, nb_oargs, call_flags;
3256         TCGTemp *arg_ts, *dir_ts;
3257
3258         if (opc == INDEX_op_call) {
3259             nb_oargs = TCGOP_CALLO(op);
3260             nb_iargs = TCGOP_CALLI(op);
3261             call_flags = op->args[nb_oargs + nb_iargs + 1];
3262         } else {
3263             nb_iargs = def->nb_iargs;
3264             nb_oargs = def->nb_oargs;
3265
3266             /* Set flags similar to how calls require.  */
3267             if (def->flags & TCG_OPF_COND_BRANCH) {
3268                 /* Like reading globals: sync_globals */
3269                 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3270             } else if (def->flags & TCG_OPF_BB_END) {
3271                 /* Like writing globals: save_globals */
3272                 call_flags = 0;
3273             } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
3274                 /* Like reading globals: sync_globals */
3275                 call_flags = TCG_CALL_NO_WRITE_GLOBALS;
3276             } else {
3277                 /* No effect on globals.  */
3278                 call_flags = (TCG_CALL_NO_READ_GLOBALS |
3279                               TCG_CALL_NO_WRITE_GLOBALS);
3280             }
3281         }
3282
3283         /* Make sure that input arguments are available.  */
3284         for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
3285             arg_ts = arg_temp(op->args[i]);
3286             if (arg_ts) {
3287                 dir_ts = arg_ts->state_ptr;
3288                 if (dir_ts && arg_ts->state == TS_DEAD) {
3289                     TCGOpcode lopc = (arg_ts->type == TCG_TYPE_I32
3290                                       ? INDEX_op_ld_i32
3291                                       : INDEX_op_ld_i64);
3292                     TCGOp *lop = tcg_op_insert_before(s, op, lopc);
3293
3294                     lop->args[0] = temp_arg(dir_ts);
3295                     lop->args[1] = temp_arg(arg_ts->mem_base);
3296                     lop->args[2] = arg_ts->mem_offset;
3297
3298                     /* Loaded, but synced with memory.  */
3299                     arg_ts->state = TS_MEM;
3300                 }
3301             }
3302         }
3303
3304         /* Perform input replacement, and mark inputs that became dead.
3305            No action is required except keeping temp_state up to date
3306            so that we reload when needed.  */
3307         for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
3308             arg_ts = arg_temp(op->args[i]);
3309             if (arg_ts) {
3310                 dir_ts = arg_ts->state_ptr;
3311                 if (dir_ts) {
3312                     op->args[i] = temp_arg(dir_ts);
3313                     changes = true;
3314                     if (IS_DEAD_ARG(i)) {
3315                         arg_ts->state = TS_DEAD;
3316                     }
3317                 }
3318             }
3319         }
3320
3321         /* Liveness analysis should ensure that the following are
3322            all correct, for call sites and basic block end points.  */
3323         if (call_flags & TCG_CALL_NO_READ_GLOBALS) {
3324             /* Nothing to do */
3325         } else if (call_flags & TCG_CALL_NO_WRITE_GLOBALS) {
3326             for (i = 0; i < nb_globals; ++i) {
3327                 /* Liveness should see that globals are synced back,
3328                    that is, either TS_DEAD or TS_MEM.  */
3329                 arg_ts = &s->temps[i];
3330                 tcg_debug_assert(arg_ts->state_ptr == 0
3331                                  || arg_ts->state != 0);
3332             }
3333         } else {
3334             for (i = 0; i < nb_globals; ++i) {
3335                 /* Liveness should see that globals are saved back,
3336                    that is, TS_DEAD, waiting to be reloaded.  */
3337                 arg_ts = &s->temps[i];
3338                 tcg_debug_assert(arg_ts->state_ptr == 0
3339                                  || arg_ts->state == TS_DEAD);
3340             }
3341         }
3342
3343         /* Outputs become available.  */
3344         if (opc == INDEX_op_mov_i32 || opc == INDEX_op_mov_i64) {
3345             arg_ts = arg_temp(op->args[0]);
3346             dir_ts = arg_ts->state_ptr;
3347             if (dir_ts) {
3348                 op->args[0] = temp_arg(dir_ts);
3349                 changes = true;
3350
3351                 /* The output is now live and modified.  */
3352                 arg_ts->state = 0;
3353
3354                 if (NEED_SYNC_ARG(0)) {
3355                     TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
3356                                       ? INDEX_op_st_i32
3357                                       : INDEX_op_st_i64);
3358                     TCGOp *sop = tcg_op_insert_after(s, op, sopc);
3359                     TCGTemp *out_ts = dir_ts;
3360
3361                     if (IS_DEAD_ARG(0)) {
3362                         out_ts = arg_temp(op->args[1]);
3363                         arg_ts->state = TS_DEAD;
3364                         tcg_op_remove(s, op);
3365                     } else {
3366                         arg_ts->state = TS_MEM;
3367                     }
3368
3369                     sop->args[0] = temp_arg(out_ts);
3370                     sop->args[1] = temp_arg(arg_ts->mem_base);
3371                     sop->args[2] = arg_ts->mem_offset;
3372                 } else {
3373                     tcg_debug_assert(!IS_DEAD_ARG(0));
3374                 }
3375             }
3376         } else {
3377             for (i = 0; i < nb_oargs; i++) {
3378                 arg_ts = arg_temp(op->args[i]);
3379                 dir_ts = arg_ts->state_ptr;
3380                 if (!dir_ts) {
3381                     continue;
3382                 }
3383                 op->args[i] = temp_arg(dir_ts);
3384                 changes = true;
3385
3386                 /* The output is now live and modified.  */
3387                 arg_ts->state = 0;
3388
3389                 /* Sync outputs upon their last write.  */
3390                 if (NEED_SYNC_ARG(i)) {
3391                     TCGOpcode sopc = (arg_ts->type == TCG_TYPE_I32
3392                                       ? INDEX_op_st_i32
3393                                       : INDEX_op_st_i64);
3394                     TCGOp *sop = tcg_op_insert_after(s, op, sopc);
3395
3396                     sop->args[0] = temp_arg(dir_ts);
3397                     sop->args[1] = temp_arg(arg_ts->mem_base);
3398                     sop->args[2] = arg_ts->mem_offset;
3399
3400                     arg_ts->state = TS_MEM;
3401                 }
3402                 /* Drop outputs that are dead.  */
3403                 if (IS_DEAD_ARG(i)) {
3404                     arg_ts->state = TS_DEAD;
3405                 }
3406             }
3407         }
3408     }
3409
3410     return changes;
3411 }
3412
3413 #ifdef CONFIG_DEBUG_TCG
3414 static void dump_regs(TCGContext *s)
3415 {
3416     TCGTemp *ts;
3417     int i;
3418     char buf[64];
3419
3420     for(i = 0; i < s->nb_temps; i++) {
3421         ts = &s->temps[i];
3422         printf("  %10s: ", tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
3423         switch(ts->val_type) {
3424         case TEMP_VAL_REG:
3425             printf("%s", tcg_target_reg_names[ts->reg]);
3426             break;
3427         case TEMP_VAL_MEM:
3428             printf("%d(%s)", (int)ts->mem_offset,
3429                    tcg_target_reg_names[ts->mem_base->reg]);
3430             break;
3431         case TEMP_VAL_CONST:
3432             printf("$0x%" PRIx64, ts->val);
3433             break;
3434         case TEMP_VAL_DEAD:
3435             printf("D");
3436             break;
3437         default:
3438             printf("???");
3439             break;
3440         }
3441         printf("\n");
3442     }
3443
3444     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
3445         if (s->reg_to_temp[i] != NULL) {
3446             printf("%s: %s\n", 
3447                    tcg_target_reg_names[i], 
3448                    tcg_get_arg_str_ptr(s, buf, sizeof(buf), s->reg_to_temp[i]));
3449         }
3450     }
3451 }
3452
3453 static void check_regs(TCGContext *s)
3454 {
3455     int reg;
3456     int k;
3457     TCGTemp *ts;
3458     char buf[64];
3459
3460     for (reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
3461         ts = s->reg_to_temp[reg];
3462         if (ts != NULL) {
3463             if (ts->val_type != TEMP_VAL_REG || ts->reg != reg) {
3464                 printf("Inconsistency for register %s:\n", 
3465                        tcg_target_reg_names[reg]);
3466                 goto fail;
3467             }
3468         }
3469     }
3470     for (k = 0; k < s->nb_temps; k++) {
3471         ts = &s->temps[k];
3472         if (ts->val_type == TEMP_VAL_REG
3473             && ts->kind != TEMP_FIXED
3474             && s->reg_to_temp[ts->reg] != ts) {
3475             printf("Inconsistency for temp %s:\n",
3476                    tcg_get_arg_str_ptr(s, buf, sizeof(buf), ts));
3477         fail:
3478             printf("reg state:\n");
3479             dump_regs(s);
3480             tcg_abort();
3481         }
3482     }
3483 }
3484 #endif
3485
3486 static void temp_allocate_frame(TCGContext *s, TCGTemp *ts)
3487 {
3488 #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
3489     /* Sparc64 stack is accessed with offset of 2047 */
3490     s->current_frame_offset = (s->current_frame_offset +
3491                                (tcg_target_long)sizeof(tcg_target_long) - 1) &
3492         ~(sizeof(tcg_target_long) - 1);
3493 #endif
3494     if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
3495         s->frame_end) {
3496         tcg_abort();
3497     }
3498     ts->mem_offset = s->current_frame_offset;
3499     ts->mem_base = s->frame_temp;
3500     ts->mem_allocated = 1;
3501     s->current_frame_offset += sizeof(tcg_target_long);
3502 }
3503
3504 static void temp_load(TCGContext *, TCGTemp *, TCGRegSet, TCGRegSet, TCGRegSet);
3505
3506 /* Mark a temporary as free or dead.  If 'free_or_dead' is negative,
3507    mark it free; otherwise mark it dead.  */
3508 static void temp_free_or_dead(TCGContext *s, TCGTemp *ts, int free_or_dead)
3509 {
3510     TCGTempVal new_type;
3511
3512     switch (ts->kind) {
3513     case TEMP_FIXED:
3514         return;
3515     case TEMP_GLOBAL:
3516     case TEMP_LOCAL:
3517         new_type = TEMP_VAL_MEM;
3518         break;
3519     case TEMP_NORMAL:
3520         new_type = free_or_dead < 0 ? TEMP_VAL_MEM : TEMP_VAL_DEAD;
3521         break;
3522     case TEMP_CONST:
3523         new_type = TEMP_VAL_CONST;
3524         break;
3525     default:
3526         g_assert_not_reached();
3527     }
3528     if (ts->val_type == TEMP_VAL_REG) {
3529         s->reg_to_temp[ts->reg] = NULL;
3530     }
3531     ts->val_type = new_type;
3532 }
3533
3534 /* Mark a temporary as dead.  */
3535 static inline void temp_dead(TCGContext *s, TCGTemp *ts)
3536 {
3537     temp_free_or_dead(s, ts, 1);
3538 }
3539
3540 /* Sync a temporary to memory. 'allocated_regs' is used in case a temporary
3541    registers needs to be allocated to store a constant.  If 'free_or_dead'
3542    is non-zero, subsequently release the temporary; if it is positive, the
3543    temp is dead; if it is negative, the temp is free.  */
3544 static void temp_sync(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs,
3545                       TCGRegSet preferred_regs, int free_or_dead)
3546 {
3547     if (!temp_readonly(ts) && !ts->mem_coherent) {
3548         if (!ts->mem_allocated) {
3549             temp_allocate_frame(s, ts);
3550         }
3551         switch (ts->val_type) {
3552         case TEMP_VAL_CONST:
3553             /* If we're going to free the temp immediately, then we won't
3554                require it later in a register, so attempt to store the
3555                constant to memory directly.  */
3556             if (free_or_dead
3557                 && tcg_out_sti(s, ts->type, ts->val,
3558                                ts->mem_base->reg, ts->mem_offset)) {
3559                 break;
3560             }
3561             temp_load(s, ts, tcg_target_available_regs[ts->type],
3562                       allocated_regs, preferred_regs);
3563             /* fallthrough */
3564
3565         case TEMP_VAL_REG:
3566             tcg_out_st(s, ts->type, ts->reg,
3567                        ts->mem_base->reg, ts->mem_offset);
3568             break;
3569
3570         case TEMP_VAL_MEM:
3571             break;
3572
3573         case TEMP_VAL_DEAD:
3574         default:
3575             tcg_abort();
3576         }
3577         ts->mem_coherent = 1;
3578     }
3579     if (free_or_dead) {
3580         temp_free_or_dead(s, ts, free_or_dead);
3581     }
3582 }
3583
3584 /* free register 'reg' by spilling the corresponding temporary if necessary */
3585 static void tcg_reg_free(TCGContext *s, TCGReg reg, TCGRegSet allocated_regs)
3586 {
3587     TCGTemp *ts = s->reg_to_temp[reg];
3588     if (ts != NULL) {
3589         temp_sync(s, ts, allocated_regs, 0, -1);
3590     }
3591 }
3592
3593 /**
3594  * tcg_reg_alloc:
3595  * @required_regs: Set of registers in which we must allocate.
3596  * @allocated_regs: Set of registers which must be avoided.
3597  * @preferred_regs: Set of registers we should prefer.
3598  * @rev: True if we search the registers in "indirect" order.
3599  *
3600  * The allocated register must be in @required_regs & ~@allocated_regs,
3601  * but if we can put it in @preferred_regs we may save a move later.
3602  */
3603 static TCGReg tcg_reg_alloc(TCGContext *s, TCGRegSet required_regs,
3604                             TCGRegSet allocated_regs,
3605                             TCGRegSet preferred_regs, bool rev)
3606 {
3607     int i, j, f, n = ARRAY_SIZE(tcg_target_reg_alloc_order);
3608     TCGRegSet reg_ct[2];
3609     const int *order;
3610
3611     reg_ct[1] = required_regs & ~allocated_regs;
3612     tcg_debug_assert(reg_ct[1] != 0);
3613     reg_ct[0] = reg_ct[1] & preferred_regs;
3614
3615     /* Skip the preferred_regs option if it cannot be satisfied,
3616        or if the preference made no difference.  */
3617     f = reg_ct[0] == 0 || reg_ct[0] == reg_ct[1];
3618
3619     order = rev ? indirect_reg_alloc_order : tcg_target_reg_alloc_order;
3620
3621     /* Try free registers, preferences first.  */
3622     for (j = f; j < 2; j++) {
3623         TCGRegSet set = reg_ct[j];
3624
3625         if (tcg_regset_single(set)) {
3626             /* One register in the set.  */
3627             TCGReg reg = tcg_regset_first(set);
3628             if (s->reg_to_temp[reg] == NULL) {
3629                 return reg;
3630             }
3631         } else {
3632             for (i = 0; i < n; i++) {
3633                 TCGReg reg = order[i];
3634                 if (s->reg_to_temp[reg] == NULL &&
3635                     tcg_regset_test_reg(set, reg)) {
3636                     return reg;
3637                 }
3638             }
3639         }
3640     }
3641
3642     /* We must spill something.  */
3643     for (j = f; j < 2; j++) {
3644         TCGRegSet set = reg_ct[j];
3645
3646         if (tcg_regset_single(set)) {
3647             /* One register in the set.  */
3648             TCGReg reg = tcg_regset_first(set);
3649             tcg_reg_free(s, reg, allocated_regs);
3650             return reg;
3651         } else {
3652             for (i = 0; i < n; i++) {
3653                 TCGReg reg = order[i];
3654                 if (tcg_regset_test_reg(set, reg)) {
3655                     tcg_reg_free(s, reg, allocated_regs);
3656                     return reg;
3657                 }
3658             }
3659         }
3660     }
3661
3662     tcg_abort();
3663 }
3664
3665 /* Make sure the temporary is in a register.  If needed, allocate the register
3666    from DESIRED while avoiding ALLOCATED.  */
3667 static void temp_load(TCGContext *s, TCGTemp *ts, TCGRegSet desired_regs,
3668                       TCGRegSet allocated_regs, TCGRegSet preferred_regs)
3669 {
3670     TCGReg reg;
3671
3672     switch (ts->val_type) {
3673     case TEMP_VAL_REG:
3674         return;
3675     case TEMP_VAL_CONST:
3676         reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
3677                             preferred_regs, ts->indirect_base);
3678         if (ts->type <= TCG_TYPE_I64) {
3679             tcg_out_movi(s, ts->type, reg, ts->val);
3680         } else {
3681             uint64_t val = ts->val;
3682             MemOp vece = MO_64;
3683
3684             /*
3685              * Find the minimal vector element that matches the constant.
3686              * The targets will, in general, have to do this search anyway,
3687              * do this generically.
3688              */
3689             if (val == dup_const(MO_8, val)) {
3690                 vece = MO_8;
3691             } else if (val == dup_const(MO_16, val)) {
3692                 vece = MO_16;
3693             } else if (val == dup_const(MO_32, val)) {
3694                 vece = MO_32;
3695             }
3696
3697             tcg_out_dupi_vec(s, ts->type, vece, reg, ts->val);
3698         }
3699         ts->mem_coherent = 0;
3700         break;
3701     case TEMP_VAL_MEM:
3702         reg = tcg_reg_alloc(s, desired_regs, allocated_regs,
3703                             preferred_regs, ts->indirect_base);
3704         tcg_out_ld(s, ts->type, reg, ts->mem_base->reg, ts->mem_offset);
3705         ts->mem_coherent = 1;
3706         break;
3707     case TEMP_VAL_DEAD:
3708     default:
3709         tcg_abort();
3710     }
3711     ts->reg = reg;
3712     ts->val_type = TEMP_VAL_REG;
3713     s->reg_to_temp[reg] = ts;
3714 }
3715
3716 /* Save a temporary to memory. 'allocated_regs' is used in case a
3717    temporary registers needs to be allocated to store a constant.  */
3718 static void temp_save(TCGContext *s, TCGTemp *ts, TCGRegSet allocated_regs)
3719 {
3720     /* The liveness analysis already ensures that globals are back
3721        in memory. Keep an tcg_debug_assert for safety. */
3722     tcg_debug_assert(ts->val_type == TEMP_VAL_MEM || temp_readonly(ts));
3723 }
3724
3725 /* save globals to their canonical location and assume they can be
3726    modified be the following code. 'allocated_regs' is used in case a
3727    temporary registers needs to be allocated to store a constant. */
3728 static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
3729 {
3730     int i, n;
3731
3732     for (i = 0, n = s->nb_globals; i < n; i++) {
3733         temp_save(s, &s->temps[i], allocated_regs);
3734     }
3735 }
3736
3737 /* sync globals to their canonical location and assume they can be
3738    read by the following code. 'allocated_regs' is used in case a
3739    temporary registers needs to be allocated to store a constant. */
3740 static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
3741 {
3742     int i, n;
3743
3744     for (i = 0, n = s->nb_globals; i < n; i++) {
3745         TCGTemp *ts = &s->temps[i];
3746         tcg_debug_assert(ts->val_type != TEMP_VAL_REG
3747                          || ts->kind == TEMP_FIXED
3748                          || ts->mem_coherent);
3749     }
3750 }
3751
3752 /* at the end of a basic block, we assume all temporaries are dead and
3753    all globals are stored at their canonical location. */
3754 static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
3755 {
3756     int i;
3757
3758     for (i = s->nb_globals; i < s->nb_temps; i++) {
3759         TCGTemp *ts = &s->temps[i];
3760
3761         switch (ts->kind) {
3762         case TEMP_LOCAL:
3763             temp_save(s, ts, allocated_regs);
3764             break;
3765         case TEMP_NORMAL:
3766             /* The liveness analysis already ensures that temps are dead.
3767                Keep an tcg_debug_assert for safety. */
3768             tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
3769             break;
3770         case TEMP_CONST:
3771             /* Similarly, we should have freed any allocated register. */
3772             tcg_debug_assert(ts->val_type == TEMP_VAL_CONST);
3773             break;
3774         default:
3775             g_assert_not_reached();
3776         }
3777     }
3778
3779     save_globals(s, allocated_regs);
3780 }
3781
3782 /*
3783  * At a conditional branch, we assume all temporaries are dead and
3784  * all globals and local temps are synced to their location.
3785  */
3786 static void tcg_reg_alloc_cbranch(TCGContext *s, TCGRegSet allocated_regs)
3787 {
3788     sync_globals(s, allocated_regs);
3789
3790     for (int i = s->nb_globals; i < s->nb_temps; i++) {
3791         TCGTemp *ts = &s->temps[i];
3792         /*
3793          * The liveness analysis already ensures that temps are dead.
3794          * Keep tcg_debug_asserts for safety.
3795          */
3796         switch (ts->kind) {
3797         case TEMP_LOCAL:
3798             tcg_debug_assert(ts->val_type != TEMP_VAL_REG || ts->mem_coherent);
3799             break;
3800         case TEMP_NORMAL:
3801             tcg_debug_assert(ts->val_type == TEMP_VAL_DEAD);
3802             break;
3803         case TEMP_CONST:
3804             break;
3805         default:
3806             g_assert_not_reached();
3807         }
3808     }
3809 }
3810
3811 /*
3812  * Specialized code generation for INDEX_op_mov_* with a constant.
3813  */
3814 static void tcg_reg_alloc_do_movi(TCGContext *s, TCGTemp *ots,
3815                                   tcg_target_ulong val, TCGLifeData arg_life,
3816                                   TCGRegSet preferred_regs)
3817 {
3818     /* ENV should not be modified.  */
3819     tcg_debug_assert(!temp_readonly(ots));
3820
3821     /* The movi is not explicitly generated here.  */
3822     if (ots->val_type == TEMP_VAL_REG) {
3823         s->reg_to_temp[ots->reg] = NULL;
3824     }
3825     ots->val_type = TEMP_VAL_CONST;
3826     ots->val = val;
3827     ots->mem_coherent = 0;
3828     if (NEED_SYNC_ARG(0)) {
3829         temp_sync(s, ots, s->reserved_regs, preferred_regs, IS_DEAD_ARG(0));
3830     } else if (IS_DEAD_ARG(0)) {
3831         temp_dead(s, ots);
3832     }
3833 }
3834
3835 /*
3836  * Specialized code generation for INDEX_op_mov_*.
3837  */
3838 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOp *op)
3839 {
3840     const TCGLifeData arg_life = op->life;
3841     TCGRegSet allocated_regs, preferred_regs;
3842     TCGTemp *ts, *ots;
3843     TCGType otype, itype;
3844
3845     allocated_regs = s->reserved_regs;
3846     preferred_regs = op->output_pref[0];
3847     ots = arg_temp(op->args[0]);
3848     ts = arg_temp(op->args[1]);
3849
3850     /* ENV should not be modified.  */
3851     tcg_debug_assert(!temp_readonly(ots));
3852
3853     /* Note that otype != itype for no-op truncation.  */
3854     otype = ots->type;
3855     itype = ts->type;
3856
3857     if (ts->val_type == TEMP_VAL_CONST) {
3858         /* propagate constant or generate sti */
3859         tcg_target_ulong val = ts->val;
3860         if (IS_DEAD_ARG(1)) {
3861             temp_dead(s, ts);
3862         }
3863         tcg_reg_alloc_do_movi(s, ots, val, arg_life, preferred_regs);
3864         return;
3865     }
3866
3867     /* If the source value is in memory we're going to be forced
3868        to have it in a register in order to perform the copy.  Copy
3869        the SOURCE value into its own register first, that way we
3870        don't have to reload SOURCE the next time it is used. */
3871     if (ts->val_type == TEMP_VAL_MEM) {
3872         temp_load(s, ts, tcg_target_available_regs[itype],
3873                   allocated_regs, preferred_regs);
3874     }
3875
3876     tcg_debug_assert(ts->val_type == TEMP_VAL_REG);
3877     if (IS_DEAD_ARG(0)) {
3878         /* mov to a non-saved dead register makes no sense (even with
3879            liveness analysis disabled). */
3880         tcg_debug_assert(NEED_SYNC_ARG(0));
3881         if (!ots->mem_allocated) {
3882             temp_allocate_frame(s, ots);
3883         }
3884         tcg_out_st(s, otype, ts->reg, ots->mem_base->reg, ots->mem_offset);
3885         if (IS_DEAD_ARG(1)) {
3886             temp_dead(s, ts);
3887         }
3888         temp_dead(s, ots);
3889     } else {
3890         if (IS_DEAD_ARG(1) && ts->kind != TEMP_FIXED) {
3891             /* the mov can be suppressed */
3892             if (ots->val_type == TEMP_VAL_REG) {
3893                 s->reg_to_temp[ots->reg] = NULL;
3894             }
3895             ots->reg = ts->reg;
3896             temp_dead(s, ts);
3897         } else {
3898             if (ots->val_type != TEMP_VAL_REG) {
3899                 /* When allocating a new register, make sure to not spill the
3900                    input one. */
3901                 tcg_regset_set_reg(allocated_regs, ts->reg);
3902                 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
3903                                          allocated_regs, preferred_regs,
3904                                          ots->indirect_base);
3905             }
3906             if (!tcg_out_mov(s, otype, ots->reg, ts->reg)) {
3907                 /*
3908                  * Cross register class move not supported.
3909                  * Store the source register into the destination slot
3910                  * and leave the destination temp as TEMP_VAL_MEM.
3911                  */
3912                 assert(!temp_readonly(ots));
3913                 if (!ts->mem_allocated) {
3914                     temp_allocate_frame(s, ots);
3915                 }
3916                 tcg_out_st(s, ts->type, ts->reg,
3917                            ots->mem_base->reg, ots->mem_offset);
3918                 ots->mem_coherent = 1;
3919                 temp_free_or_dead(s, ots, -1);
3920                 return;
3921             }
3922         }
3923         ots->val_type = TEMP_VAL_REG;
3924         ots->mem_coherent = 0;
3925         s->reg_to_temp[ots->reg] = ots;
3926         if (NEED_SYNC_ARG(0)) {
3927             temp_sync(s, ots, allocated_regs, 0, 0);
3928         }
3929     }
3930 }
3931
3932 /*
3933  * Specialized code generation for INDEX_op_dup_vec.
3934  */
3935 static void tcg_reg_alloc_dup(TCGContext *s, const TCGOp *op)
3936 {
3937     const TCGLifeData arg_life = op->life;
3938     TCGRegSet dup_out_regs, dup_in_regs;
3939     TCGTemp *its, *ots;
3940     TCGType itype, vtype;
3941     intptr_t endian_fixup;
3942     unsigned vece;
3943     bool ok;
3944
3945     ots = arg_temp(op->args[0]);
3946     its = arg_temp(op->args[1]);
3947
3948     /* ENV should not be modified.  */
3949     tcg_debug_assert(!temp_readonly(ots));
3950
3951     itype = its->type;
3952     vece = TCGOP_VECE(op);
3953     vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
3954
3955     if (its->val_type == TEMP_VAL_CONST) {
3956         /* Propagate constant via movi -> dupi.  */
3957         tcg_target_ulong val = its->val;
3958         if (IS_DEAD_ARG(1)) {
3959             temp_dead(s, its);
3960         }
3961         tcg_reg_alloc_do_movi(s, ots, val, arg_life, op->output_pref[0]);
3962         return;
3963     }
3964
3965     dup_out_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
3966     dup_in_regs = tcg_op_defs[INDEX_op_dup_vec].args_ct[1].regs;
3967
3968     /* Allocate the output register now.  */
3969     if (ots->val_type != TEMP_VAL_REG) {
3970         TCGRegSet allocated_regs = s->reserved_regs;
3971
3972         if (!IS_DEAD_ARG(1) && its->val_type == TEMP_VAL_REG) {
3973             /* Make sure to not spill the input register. */
3974             tcg_regset_set_reg(allocated_regs, its->reg);
3975         }
3976         ots->reg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
3977                                  op->output_pref[0], ots->indirect_base);
3978         ots->val_type = TEMP_VAL_REG;
3979         ots->mem_coherent = 0;
3980         s->reg_to_temp[ots->reg] = ots;
3981     }
3982
3983     switch (its->val_type) {
3984     case TEMP_VAL_REG:
3985         /*
3986          * The dup constriaints must be broad, covering all possible VECE.
3987          * However, tcg_op_dup_vec() gets to see the VECE and we allow it
3988          * to fail, indicating that extra moves are required for that case.
3989          */
3990         if (tcg_regset_test_reg(dup_in_regs, its->reg)) {
3991             if (tcg_out_dup_vec(s, vtype, vece, ots->reg, its->reg)) {
3992                 goto done;
3993             }
3994             /* Try again from memory or a vector input register.  */
3995         }
3996         if (!its->mem_coherent) {
3997             /*
3998              * The input register is not synced, and so an extra store
3999              * would be required to use memory.  Attempt an integer-vector
4000              * register move first.  We do not have a TCGRegSet for this.
4001              */
4002             if (tcg_out_mov(s, itype, ots->reg, its->reg)) {
4003                 break;
4004             }
4005             /* Sync the temp back to its slot and load from there.  */
4006             temp_sync(s, its, s->reserved_regs, 0, 0);
4007         }
4008         /* fall through */
4009
4010     case TEMP_VAL_MEM:
4011 #ifdef HOST_WORDS_BIGENDIAN
4012         endian_fixup = itype == TCG_TYPE_I32 ? 4 : 8;
4013         endian_fixup -= 1 << vece;
4014 #else
4015         endian_fixup = 0;
4016 #endif
4017         if (tcg_out_dupm_vec(s, vtype, vece, ots->reg, its->mem_base->reg,
4018                              its->mem_offset + endian_fixup)) {
4019             goto done;
4020         }
4021         tcg_out_ld(s, itype, ots->reg, its->mem_base->reg, its->mem_offset);
4022         break;
4023
4024     default:
4025         g_assert_not_reached();
4026     }
4027
4028     /* We now have a vector input register, so dup must succeed. */
4029     ok = tcg_out_dup_vec(s, vtype, vece, ots->reg, ots->reg);
4030     tcg_debug_assert(ok);
4031
4032  done:
4033     if (IS_DEAD_ARG(1)) {
4034         temp_dead(s, its);
4035     }
4036     if (NEED_SYNC_ARG(0)) {
4037         temp_sync(s, ots, s->reserved_regs, 0, 0);
4038     }
4039     if (IS_DEAD_ARG(0)) {
4040         temp_dead(s, ots);
4041     }
4042 }
4043
4044 static void tcg_reg_alloc_op(TCGContext *s, const TCGOp *op)
4045 {
4046     const TCGLifeData arg_life = op->life;
4047     const TCGOpDef * const def = &tcg_op_defs[op->opc];
4048     TCGRegSet i_allocated_regs;
4049     TCGRegSet o_allocated_regs;
4050     int i, k, nb_iargs, nb_oargs;
4051     TCGReg reg;
4052     TCGArg arg;
4053     const TCGArgConstraint *arg_ct;
4054     TCGTemp *ts;
4055     TCGArg new_args[TCG_MAX_OP_ARGS];
4056     int const_args[TCG_MAX_OP_ARGS];
4057
4058     nb_oargs = def->nb_oargs;
4059     nb_iargs = def->nb_iargs;
4060
4061     /* copy constants */
4062     memcpy(new_args + nb_oargs + nb_iargs, 
4063            op->args + nb_oargs + nb_iargs,
4064            sizeof(TCGArg) * def->nb_cargs);
4065
4066     i_allocated_regs = s->reserved_regs;
4067     o_allocated_regs = s->reserved_regs;
4068
4069     /* satisfy input constraints */ 
4070     for (k = 0; k < nb_iargs; k++) {
4071         TCGRegSet i_preferred_regs, o_preferred_regs;
4072
4073         i = def->args_ct[nb_oargs + k].sort_index;
4074         arg = op->args[i];
4075         arg_ct = &def->args_ct[i];
4076         ts = arg_temp(arg);
4077
4078         if (ts->val_type == TEMP_VAL_CONST
4079             && tcg_target_const_match(ts->val, ts->type, arg_ct)) {
4080             /* constant is OK for instruction */
4081             const_args[i] = 1;
4082             new_args[i] = ts->val;
4083             continue;
4084         }
4085
4086         i_preferred_regs = o_preferred_regs = 0;
4087         if (arg_ct->ialias) {
4088             o_preferred_regs = op->output_pref[arg_ct->alias_index];
4089
4090             /*
4091              * If the input is readonly, then it cannot also be an
4092              * output and aliased to itself.  If the input is not
4093              * dead after the instruction, we must allocate a new
4094              * register and move it.
4095              */
4096             if (temp_readonly(ts) || !IS_DEAD_ARG(i)) {
4097                 goto allocate_in_reg;
4098             }
4099
4100             /*
4101              * Check if the current register has already been allocated
4102              * for another input aliased to an output.
4103              */
4104             if (ts->val_type == TEMP_VAL_REG) {
4105                 reg = ts->reg;
4106                 for (int k2 = 0; k2 < k; k2++) {
4107                     int i2 = def->args_ct[nb_oargs + k2].sort_index;
4108                     if (def->args_ct[i2].ialias && reg == new_args[i2]) {
4109                         goto allocate_in_reg;
4110                     }
4111                 }
4112             }
4113             i_preferred_regs = o_preferred_regs;
4114         }
4115
4116         temp_load(s, ts, arg_ct->regs, i_allocated_regs, i_preferred_regs);
4117         reg = ts->reg;
4118
4119         if (!tcg_regset_test_reg(arg_ct->regs, reg)) {
4120  allocate_in_reg:
4121             /*
4122              * Allocate a new register matching the constraint
4123              * and move the temporary register into it.
4124              */
4125             temp_load(s, ts, tcg_target_available_regs[ts->type],
4126                       i_allocated_regs, 0);
4127             reg = tcg_reg_alloc(s, arg_ct->regs, i_allocated_regs,
4128                                 o_preferred_regs, ts->indirect_base);
4129             if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
4130                 /*
4131                  * Cross register class move not supported.  Sync the
4132                  * temp back to its slot and load from there.
4133                  */
4134                 temp_sync(s, ts, i_allocated_regs, 0, 0);
4135                 tcg_out_ld(s, ts->type, reg,
4136                            ts->mem_base->reg, ts->mem_offset);
4137             }
4138         }
4139         new_args[i] = reg;
4140         const_args[i] = 0;
4141         tcg_regset_set_reg(i_allocated_regs, reg);
4142     }
4143     
4144     /* mark dead temporaries and free the associated registers */
4145     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
4146         if (IS_DEAD_ARG(i)) {
4147             temp_dead(s, arg_temp(op->args[i]));
4148         }
4149     }
4150
4151     if (def->flags & TCG_OPF_COND_BRANCH) {
4152         tcg_reg_alloc_cbranch(s, i_allocated_regs);
4153     } else if (def->flags & TCG_OPF_BB_END) {
4154         tcg_reg_alloc_bb_end(s, i_allocated_regs);
4155     } else {
4156         if (def->flags & TCG_OPF_CALL_CLOBBER) {
4157             /* XXX: permit generic clobber register list ? */ 
4158             for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
4159                 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
4160                     tcg_reg_free(s, i, i_allocated_regs);
4161                 }
4162             }
4163         }
4164         if (def->flags & TCG_OPF_SIDE_EFFECTS) {
4165             /* sync globals if the op has side effects and might trigger
4166                an exception. */
4167             sync_globals(s, i_allocated_regs);
4168         }
4169         
4170         /* satisfy the output constraints */
4171         for(k = 0; k < nb_oargs; k++) {
4172             i = def->args_ct[k].sort_index;
4173             arg = op->args[i];
4174             arg_ct = &def->args_ct[i];
4175             ts = arg_temp(arg);
4176
4177             /* ENV should not be modified.  */
4178             tcg_debug_assert(!temp_readonly(ts));
4179
4180             if (arg_ct->oalias && !const_args[arg_ct->alias_index]) {
4181                 reg = new_args[arg_ct->alias_index];
4182             } else if (arg_ct->newreg) {
4183                 reg = tcg_reg_alloc(s, arg_ct->regs,
4184                                     i_allocated_regs | o_allocated_regs,
4185                                     op->output_pref[k], ts->indirect_base);
4186             } else {
4187                 reg = tcg_reg_alloc(s, arg_ct->regs, o_allocated_regs,
4188                                     op->output_pref[k], ts->indirect_base);
4189             }
4190             tcg_regset_set_reg(o_allocated_regs, reg);
4191             if (ts->val_type == TEMP_VAL_REG) {
4192                 s->reg_to_temp[ts->reg] = NULL;
4193             }
4194             ts->val_type = TEMP_VAL_REG;
4195             ts->reg = reg;
4196             /*
4197              * Temp value is modified, so the value kept in memory is
4198              * potentially not the same.
4199              */
4200             ts->mem_coherent = 0;
4201             s->reg_to_temp[reg] = ts;
4202             new_args[i] = reg;
4203         }
4204     }
4205
4206     /* emit instruction */
4207     if (def->flags & TCG_OPF_VECTOR) {
4208         tcg_out_vec_op(s, op->opc, TCGOP_VECL(op), TCGOP_VECE(op),
4209                        new_args, const_args);
4210     } else {
4211         tcg_out_op(s, op->opc, new_args, const_args);
4212     }
4213
4214     /* move the outputs in the correct register if needed */
4215     for(i = 0; i < nb_oargs; i++) {
4216         ts = arg_temp(op->args[i]);
4217
4218         /* ENV should not be modified.  */
4219         tcg_debug_assert(!temp_readonly(ts));
4220
4221         if (NEED_SYNC_ARG(i)) {
4222             temp_sync(s, ts, o_allocated_regs, 0, IS_DEAD_ARG(i));
4223         } else if (IS_DEAD_ARG(i)) {
4224             temp_dead(s, ts);
4225         }
4226     }
4227 }
4228
4229 static bool tcg_reg_alloc_dup2(TCGContext *s, const TCGOp *op)
4230 {
4231     const TCGLifeData arg_life = op->life;
4232     TCGTemp *ots, *itsl, *itsh;
4233     TCGType vtype = TCGOP_VECL(op) + TCG_TYPE_V64;
4234
4235     /* This opcode is only valid for 32-bit hosts, for 64-bit elements. */
4236     tcg_debug_assert(TCG_TARGET_REG_BITS == 32);
4237     tcg_debug_assert(TCGOP_VECE(op) == MO_64);
4238
4239     ots = arg_temp(op->args[0]);
4240     itsl = arg_temp(op->args[1]);
4241     itsh = arg_temp(op->args[2]);
4242
4243     /* ENV should not be modified.  */
4244     tcg_debug_assert(!temp_readonly(ots));
4245
4246     /* Allocate the output register now.  */
4247     if (ots->val_type != TEMP_VAL_REG) {
4248         TCGRegSet allocated_regs = s->reserved_regs;
4249         TCGRegSet dup_out_regs =
4250             tcg_op_defs[INDEX_op_dup_vec].args_ct[0].regs;
4251
4252         /* Make sure to not spill the input registers. */
4253         if (!IS_DEAD_ARG(1) && itsl->val_type == TEMP_VAL_REG) {
4254             tcg_regset_set_reg(allocated_regs, itsl->reg);
4255         }
4256         if (!IS_DEAD_ARG(2) && itsh->val_type == TEMP_VAL_REG) {
4257             tcg_regset_set_reg(allocated_regs, itsh->reg);
4258         }
4259
4260         ots->reg = tcg_reg_alloc(s, dup_out_regs, allocated_regs,
4261                                  op->output_pref[0], ots->indirect_base);
4262         ots->val_type = TEMP_VAL_REG;
4263         ots->mem_coherent = 0;
4264         s->reg_to_temp[ots->reg] = ots;
4265     }
4266
4267     /* Promote dup2 of immediates to dupi_vec. */
4268     if (itsl->val_type == TEMP_VAL_CONST && itsh->val_type == TEMP_VAL_CONST) {
4269         uint64_t val = deposit64(itsl->val, 32, 32, itsh->val);
4270         MemOp vece = MO_64;
4271
4272         if (val == dup_const(MO_8, val)) {
4273             vece = MO_8;
4274         } else if (val == dup_const(MO_16, val)) {
4275             vece = MO_16;
4276         } else if (val == dup_const(MO_32, val)) {
4277             vece = MO_32;
4278         }
4279
4280         tcg_out_dupi_vec(s, vtype, vece, ots->reg, val);
4281         goto done;
4282     }
4283
4284     /* If the two inputs form one 64-bit value, try dupm_vec. */
4285     if (itsl + 1 == itsh && itsl->base_type == TCG_TYPE_I64) {
4286         if (!itsl->mem_coherent) {
4287             temp_sync(s, itsl, s->reserved_regs, 0, 0);
4288         }
4289         if (!itsh->mem_coherent) {
4290             temp_sync(s, itsh, s->reserved_regs, 0, 0);
4291         }
4292 #ifdef HOST_WORDS_BIGENDIAN
4293         TCGTemp *its = itsh;
4294 #else
4295         TCGTemp *its = itsl;
4296 #endif
4297         if (tcg_out_dupm_vec(s, vtype, MO_64, ots->reg,
4298                              its->mem_base->reg, its->mem_offset)) {
4299             goto done;
4300         }
4301     }
4302
4303     /* Fall back to generic expansion. */
4304     return false;
4305
4306  done:
4307     if (IS_DEAD_ARG(1)) {
4308         temp_dead(s, itsl);
4309     }
4310     if (IS_DEAD_ARG(2)) {
4311         temp_dead(s, itsh);
4312     }
4313     if (NEED_SYNC_ARG(0)) {
4314         temp_sync(s, ots, s->reserved_regs, 0, IS_DEAD_ARG(0));
4315     } else if (IS_DEAD_ARG(0)) {
4316         temp_dead(s, ots);
4317     }
4318     return true;
4319 }
4320
4321 #ifdef TCG_TARGET_STACK_GROWSUP
4322 #define STACK_DIR(x) (-(x))
4323 #else
4324 #define STACK_DIR(x) (x)
4325 #endif
4326
4327 static void tcg_reg_alloc_call(TCGContext *s, TCGOp *op)
4328 {
4329     const int nb_oargs = TCGOP_CALLO(op);
4330     const int nb_iargs = TCGOP_CALLI(op);
4331     const TCGLifeData arg_life = op->life;
4332     int flags, nb_regs, i;
4333     TCGReg reg;
4334     TCGArg arg;
4335     TCGTemp *ts;
4336     intptr_t stack_offset;
4337     size_t call_stack_size;
4338     tcg_insn_unit *func_addr;
4339     int allocate_args;
4340     TCGRegSet allocated_regs;
4341
4342     func_addr = (tcg_insn_unit *)(intptr_t)op->args[nb_oargs + nb_iargs];
4343     flags = op->args[nb_oargs + nb_iargs + 1];
4344
4345     nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
4346     if (nb_regs > nb_iargs) {
4347         nb_regs = nb_iargs;
4348     }
4349
4350     /* assign stack slots first */
4351     call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
4352     call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & 
4353         ~(TCG_TARGET_STACK_ALIGN - 1);
4354     allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
4355     if (allocate_args) {
4356         /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
4357            preallocate call stack */
4358         tcg_abort();
4359     }
4360
4361     stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
4362     for (i = nb_regs; i < nb_iargs; i++) {
4363         arg = op->args[nb_oargs + i];
4364 #ifdef TCG_TARGET_STACK_GROWSUP
4365         stack_offset -= sizeof(tcg_target_long);
4366 #endif
4367         if (arg != TCG_CALL_DUMMY_ARG) {
4368             ts = arg_temp(arg);
4369             temp_load(s, ts, tcg_target_available_regs[ts->type],
4370                       s->reserved_regs, 0);
4371             tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
4372         }
4373 #ifndef TCG_TARGET_STACK_GROWSUP
4374         stack_offset += sizeof(tcg_target_long);
4375 #endif
4376     }
4377     
4378     /* assign input registers */
4379     allocated_regs = s->reserved_regs;
4380     for (i = 0; i < nb_regs; i++) {
4381         arg = op->args[nb_oargs + i];
4382         if (arg != TCG_CALL_DUMMY_ARG) {
4383             ts = arg_temp(arg);
4384             reg = tcg_target_call_iarg_regs[i];
4385
4386             if (ts->val_type == TEMP_VAL_REG) {
4387                 if (ts->reg != reg) {
4388                     tcg_reg_free(s, reg, allocated_regs);
4389                     if (!tcg_out_mov(s, ts->type, reg, ts->reg)) {
4390                         /*
4391                          * Cross register class move not supported.  Sync the
4392                          * temp back to its slot and load from there.
4393                          */
4394                         temp_sync(s, ts, allocated_regs, 0, 0);
4395                         tcg_out_ld(s, ts->type, reg,
4396                                    ts->mem_base->reg, ts->mem_offset);
4397                     }
4398                 }
4399             } else {
4400                 TCGRegSet arg_set = 0;
4401
4402                 tcg_reg_free(s, reg, allocated_regs);
4403                 tcg_regset_set_reg(arg_set, reg);
4404                 temp_load(s, ts, arg_set, allocated_regs, 0);
4405             }
4406
4407             tcg_regset_set_reg(allocated_regs, reg);
4408         }
4409     }
4410     
4411     /* mark dead temporaries and free the associated registers */
4412     for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
4413         if (IS_DEAD_ARG(i)) {
4414             temp_dead(s, arg_temp(op->args[i]));
4415         }
4416     }
4417     
4418     /* clobber call registers */
4419     for (i = 0; i < TCG_TARGET_NB_REGS; i++) {
4420         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, i)) {
4421             tcg_reg_free(s, i, allocated_regs);
4422         }
4423     }
4424
4425     /* Save globals if they might be written by the helper, sync them if
4426        they might be read. */
4427     if (flags & TCG_CALL_NO_READ_GLOBALS) {
4428         /* Nothing to do */
4429     } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
4430         sync_globals(s, allocated_regs);
4431     } else {
4432         save_globals(s, allocated_regs);
4433     }
4434
4435     tcg_out_call(s, func_addr);
4436
4437     /* assign output registers and emit moves if needed */
4438     for(i = 0; i < nb_oargs; i++) {
4439         arg = op->args[i];
4440         ts = arg_temp(arg);
4441
4442         /* ENV should not be modified.  */
4443         tcg_debug_assert(!temp_readonly(ts));
4444
4445         reg = tcg_target_call_oarg_regs[i];
4446         tcg_debug_assert(s->reg_to_temp[reg] == NULL);
4447         if (ts->val_type == TEMP_VAL_REG) {
4448             s->reg_to_temp[ts->reg] = NULL;
4449         }
4450         ts->val_type = TEMP_VAL_REG;
4451         ts->reg = reg;
4452         ts->mem_coherent = 0;
4453         s->reg_to_temp[reg] = ts;
4454         if (NEED_SYNC_ARG(i)) {
4455             temp_sync(s, ts, allocated_regs, 0, IS_DEAD_ARG(i));
4456         } else if (IS_DEAD_ARG(i)) {
4457             temp_dead(s, ts);
4458         }
4459     }
4460 }
4461
4462 #ifdef CONFIG_PROFILER
4463
4464 /* avoid copy/paste errors */
4465 #define PROF_ADD(to, from, field)                       \
4466     do {                                                \
4467         (to)->field += qatomic_read(&((from)->field));  \
4468     } while (0)
4469
4470 #define PROF_MAX(to, from, field)                                       \
4471     do {                                                                \
4472         typeof((from)->field) val__ = qatomic_read(&((from)->field));   \
4473         if (val__ > (to)->field) {                                      \
4474             (to)->field = val__;                                        \
4475         }                                                               \
4476     } while (0)
4477
4478 /* Pass in a zero'ed @prof */
4479 static inline
4480 void tcg_profile_snapshot(TCGProfile *prof, bool counters, bool table)
4481 {
4482     unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
4483     unsigned int i;
4484
4485     for (i = 0; i < n_ctxs; i++) {
4486         TCGContext *s = qatomic_read(&tcg_ctxs[i]);
4487         const TCGProfile *orig = &s->prof;
4488
4489         if (counters) {
4490             PROF_ADD(prof, orig, cpu_exec_time);
4491             PROF_ADD(prof, orig, tb_count1);
4492             PROF_ADD(prof, orig, tb_count);
4493             PROF_ADD(prof, orig, op_count);
4494             PROF_MAX(prof, orig, op_count_max);
4495             PROF_ADD(prof, orig, temp_count);
4496             PROF_MAX(prof, orig, temp_count_max);
4497             PROF_ADD(prof, orig, del_op_count);
4498             PROF_ADD(prof, orig, code_in_len);
4499             PROF_ADD(prof, orig, code_out_len);
4500             PROF_ADD(prof, orig, search_out_len);
4501             PROF_ADD(prof, orig, interm_time);
4502             PROF_ADD(prof, orig, code_time);
4503             PROF_ADD(prof, orig, la_time);
4504             PROF_ADD(prof, orig, opt_time);
4505             PROF_ADD(prof, orig, restore_count);
4506             PROF_ADD(prof, orig, restore_time);
4507         }
4508         if (table) {
4509             int i;
4510
4511             for (i = 0; i < NB_OPS; i++) {
4512                 PROF_ADD(prof, orig, table_op_count[i]);
4513             }
4514         }
4515     }
4516 }
4517
4518 #undef PROF_ADD
4519 #undef PROF_MAX
4520
4521 static void tcg_profile_snapshot_counters(TCGProfile *prof)
4522 {
4523     tcg_profile_snapshot(prof, true, false);
4524 }
4525
4526 static void tcg_profile_snapshot_table(TCGProfile *prof)
4527 {
4528     tcg_profile_snapshot(prof, false, true);
4529 }
4530
4531 void tcg_dump_op_count(void)
4532 {
4533     TCGProfile prof = {};
4534     int i;
4535
4536     tcg_profile_snapshot_table(&prof);
4537     for (i = 0; i < NB_OPS; i++) {
4538         qemu_printf("%s %" PRId64 "\n", tcg_op_defs[i].name,
4539                     prof.table_op_count[i]);
4540     }
4541 }
4542
4543 int64_t tcg_cpu_exec_time(void)
4544 {
4545     unsigned int n_ctxs = qatomic_read(&n_tcg_ctxs);
4546     unsigned int i;
4547     int64_t ret = 0;
4548
4549     for (i = 0; i < n_ctxs; i++) {
4550         const TCGContext *s = qatomic_read(&tcg_ctxs[i]);
4551         const TCGProfile *prof = &s->prof;
4552
4553         ret += qatomic_read(&prof->cpu_exec_time);
4554     }
4555     return ret;
4556 }
4557 #else
4558 void tcg_dump_op_count(void)
4559 {
4560     qemu_printf("[TCG profiler not compiled]\n");
4561 }
4562
4563 int64_t tcg_cpu_exec_time(void)
4564 {
4565     error_report("%s: TCG profiler not compiled", __func__);
4566     exit(EXIT_FAILURE);
4567 }
4568 #endif
4569
4570
4571 int tcg_gen_code(TCGContext *s, TranslationBlock *tb)
4572 {
4573 #ifdef CONFIG_PROFILER
4574     TCGProfile *prof = &s->prof;
4575 #endif
4576     int i, num_insns;
4577     TCGOp *op;
4578
4579 #ifdef CONFIG_PROFILER
4580     {
4581         int n = 0;
4582
4583         QTAILQ_FOREACH(op, &s->ops, link) {
4584             n++;
4585         }
4586         qatomic_set(&prof->op_count, prof->op_count + n);
4587         if (n > prof->op_count_max) {
4588             qatomic_set(&prof->op_count_max, n);
4589         }
4590
4591         n = s->nb_temps;
4592         qatomic_set(&prof->temp_count, prof->temp_count + n);
4593         if (n > prof->temp_count_max) {
4594             qatomic_set(&prof->temp_count_max, n);
4595         }
4596     }
4597 #endif
4598
4599 #ifdef DEBUG_DISAS
4600     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP)
4601                  && qemu_log_in_addr_range(tb->pc))) {
4602         FILE *logfile = qemu_log_lock();
4603         qemu_log("OP:\n");
4604         tcg_dump_ops(s, false);
4605         qemu_log("\n");
4606         qemu_log_unlock(logfile);
4607     }
4608 #endif
4609
4610 #ifdef CONFIG_DEBUG_TCG
4611     /* Ensure all labels referenced have been emitted.  */
4612     {
4613         TCGLabel *l;
4614         bool error = false;
4615
4616         QSIMPLEQ_FOREACH(l, &s->labels, next) {
4617             if (unlikely(!l->present) && l->refs) {
4618                 qemu_log_mask(CPU_LOG_TB_OP,
4619                               "$L%d referenced but not present.\n", l->id);
4620                 error = true;
4621             }
4622         }
4623         assert(!error);
4624     }
4625 #endif
4626
4627 #ifdef CONFIG_PROFILER
4628     qatomic_set(&prof->opt_time, prof->opt_time - profile_getclock());
4629 #endif
4630
4631 #ifdef USE_TCG_OPTIMIZATIONS
4632     tcg_optimize(s);
4633 #endif
4634
4635 #ifdef CONFIG_PROFILER
4636     qatomic_set(&prof->opt_time, prof->opt_time + profile_getclock());
4637     qatomic_set(&prof->la_time, prof->la_time - profile_getclock());
4638 #endif
4639
4640     reachable_code_pass(s);
4641     liveness_pass_1(s);
4642
4643     if (s->nb_indirects > 0) {
4644 #ifdef DEBUG_DISAS
4645         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_IND)
4646                      && qemu_log_in_addr_range(tb->pc))) {
4647             FILE *logfile = qemu_log_lock();
4648             qemu_log("OP before indirect lowering:\n");
4649             tcg_dump_ops(s, false);
4650             qemu_log("\n");
4651             qemu_log_unlock(logfile);
4652         }
4653 #endif
4654         /* Replace indirect temps with direct temps.  */
4655         if (liveness_pass_2(s)) {
4656             /* If changes were made, re-run liveness.  */
4657             liveness_pass_1(s);
4658         }
4659     }
4660
4661 #ifdef CONFIG_PROFILER
4662     qatomic_set(&prof->la_time, prof->la_time + profile_getclock());
4663 #endif
4664
4665 #ifdef DEBUG_DISAS
4666     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT)
4667                  && qemu_log_in_addr_range(tb->pc))) {
4668         FILE *logfile = qemu_log_lock();
4669         qemu_log("OP after optimization and liveness analysis:\n");
4670         tcg_dump_ops(s, true);
4671         qemu_log("\n");
4672         qemu_log_unlock(logfile);
4673     }
4674 #endif
4675
4676     tcg_reg_alloc_start(s);
4677
4678     /*
4679      * Reset the buffer pointers when restarting after overflow.
4680      * TODO: Move this into translate-all.c with the rest of the
4681      * buffer management.  Having only this done here is confusing.
4682      */
4683     s->code_buf = tcg_splitwx_to_rw(tb->tc.ptr);
4684     s->code_ptr = s->code_buf;
4685
4686 #ifdef TCG_TARGET_NEED_LDST_LABELS
4687     QSIMPLEQ_INIT(&s->ldst_labels);
4688 #endif
4689 #ifdef TCG_TARGET_NEED_POOL_LABELS
4690     s->pool_labels = NULL;
4691 #endif
4692
4693     num_insns = -1;
4694     QTAILQ_FOREACH(op, &s->ops, link) {
4695         TCGOpcode opc = op->opc;
4696
4697 #ifdef CONFIG_PROFILER
4698         qatomic_set(&prof->table_op_count[opc], prof->table_op_count[opc] + 1);
4699 #endif
4700
4701         switch (opc) {
4702         case INDEX_op_mov_i32:
4703         case INDEX_op_mov_i64:
4704         case INDEX_op_mov_vec:
4705             tcg_reg_alloc_mov(s, op);
4706             break;
4707         case INDEX_op_dup_vec:
4708             tcg_reg_alloc_dup(s, op);
4709             break;
4710         case INDEX_op_insn_start:
4711             if (num_insns >= 0) {
4712                 size_t off = tcg_current_code_size(s);
4713                 s->gen_insn_end_off[num_insns] = off;
4714                 /* Assert that we do not overflow our stored offset.  */
4715                 assert(s->gen_insn_end_off[num_insns] == off);
4716             }
4717             num_insns++;
4718             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
4719                 target_ulong a;
4720 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
4721                 a = deposit64(op->args[i * 2], 32, 32, op->args[i * 2 + 1]);
4722 #else
4723                 a = op->args[i];
4724 #endif
4725                 s->gen_insn_data[num_insns][i] = a;
4726             }
4727             break;
4728         case INDEX_op_discard:
4729             temp_dead(s, arg_temp(op->args[0]));
4730             break;
4731         case INDEX_op_set_label:
4732             tcg_reg_alloc_bb_end(s, s->reserved_regs);
4733             tcg_out_label(s, arg_label(op->args[0]));
4734             break;
4735         case INDEX_op_call:
4736             tcg_reg_alloc_call(s, op);
4737             break;
4738         case INDEX_op_dup2_vec:
4739             if (tcg_reg_alloc_dup2(s, op)) {
4740                 break;
4741             }
4742             /* fall through */
4743         default:
4744             /* Sanity check that we've not introduced any unhandled opcodes. */
4745             tcg_debug_assert(tcg_op_supported(opc));
4746             /* Note: in order to speed up the code, it would be much
4747                faster to have specialized register allocator functions for
4748                some common argument patterns */
4749             tcg_reg_alloc_op(s, op);
4750             break;
4751         }
4752 #ifdef CONFIG_DEBUG_TCG
4753         check_regs(s);
4754 #endif
4755         /* Test for (pending) buffer overflow.  The assumption is that any
4756            one operation beginning below the high water mark cannot overrun
4757            the buffer completely.  Thus we can test for overflow after
4758            generating code without having to check during generation.  */
4759         if (unlikely((void *)s->code_ptr > s->code_gen_highwater)) {
4760             return -1;
4761         }
4762         /* Test for TB overflow, as seen by gen_insn_end_off.  */
4763         if (unlikely(tcg_current_code_size(s) > UINT16_MAX)) {
4764             return -2;
4765         }
4766     }
4767     tcg_debug_assert(num_insns >= 0);
4768     s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
4769
4770     /* Generate TB finalization at the end of block */
4771 #ifdef TCG_TARGET_NEED_LDST_LABELS
4772     i = tcg_out_ldst_finalize(s);
4773     if (i < 0) {
4774         return i;
4775     }
4776 #endif
4777 #ifdef TCG_TARGET_NEED_POOL_LABELS
4778     i = tcg_out_pool_finalize(s);
4779     if (i < 0) {
4780         return i;
4781     }
4782 #endif
4783     if (!tcg_resolve_relocs(s)) {
4784         return -2;
4785     }
4786
4787 #ifndef CONFIG_TCG_INTERPRETER
4788     /* flush instruction cache */
4789     flush_idcache_range((uintptr_t)tcg_splitwx_to_rx(s->code_buf),
4790                         (uintptr_t)s->code_buf,
4791                         tcg_ptr_byte_diff(s->code_ptr, s->code_buf));
4792 #endif
4793
4794     return tcg_current_code_size(s);
4795 }
4796
4797 #ifdef CONFIG_PROFILER
4798 void tcg_dump_info(void)
4799 {
4800     TCGProfile prof = {};
4801     const TCGProfile *s;
4802     int64_t tb_count;
4803     int64_t tb_div_count;
4804     int64_t tot;
4805
4806     tcg_profile_snapshot_counters(&prof);
4807     s = &prof;
4808     tb_count = s->tb_count;
4809     tb_div_count = tb_count ? tb_count : 1;
4810     tot = s->interm_time + s->code_time;
4811
4812     qemu_printf("JIT cycles          %" PRId64 " (%0.3f s at 2.4 GHz)\n",
4813                 tot, tot / 2.4e9);
4814     qemu_printf("translated TBs      %" PRId64 " (aborted=%" PRId64
4815                 " %0.1f%%)\n",
4816                 tb_count, s->tb_count1 - tb_count,
4817                 (double)(s->tb_count1 - s->tb_count)
4818                 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
4819     qemu_printf("avg ops/TB          %0.1f max=%d\n",
4820                 (double)s->op_count / tb_div_count, s->op_count_max);
4821     qemu_printf("deleted ops/TB      %0.2f\n",
4822                 (double)s->del_op_count / tb_div_count);
4823     qemu_printf("avg temps/TB        %0.2f max=%d\n",
4824                 (double)s->temp_count / tb_div_count, s->temp_count_max);
4825     qemu_printf("avg host code/TB    %0.1f\n",
4826                 (double)s->code_out_len / tb_div_count);
4827     qemu_printf("avg search data/TB  %0.1f\n",
4828                 (double)s->search_out_len / tb_div_count);
4829     
4830     qemu_printf("cycles/op           %0.1f\n",
4831                 s->op_count ? (double)tot / s->op_count : 0);
4832     qemu_printf("cycles/in byte      %0.1f\n",
4833                 s->code_in_len ? (double)tot / s->code_in_len : 0);
4834     qemu_printf("cycles/out byte     %0.1f\n",
4835                 s->code_out_len ? (double)tot / s->code_out_len : 0);
4836     qemu_printf("cycles/search byte     %0.1f\n",
4837                 s->search_out_len ? (double)tot / s->search_out_len : 0);
4838     if (tot == 0) {
4839         tot = 1;
4840     }
4841     qemu_printf("  gen_interm time   %0.1f%%\n",
4842                 (double)s->interm_time / tot * 100.0);
4843     qemu_printf("  gen_code time     %0.1f%%\n",
4844                 (double)s->code_time / tot * 100.0);
4845     qemu_printf("optim./code time    %0.1f%%\n",
4846                 (double)s->opt_time / (s->code_time ? s->code_time : 1)
4847                 * 100.0);
4848     qemu_printf("liveness/code time  %0.1f%%\n",
4849                 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
4850     qemu_printf("cpu_restore count   %" PRId64 "\n",
4851                 s->restore_count);
4852     qemu_printf("  avg cycles        %0.1f\n",
4853                 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
4854 }
4855 #else
4856 void tcg_dump_info(void)
4857 {
4858     qemu_printf("[TCG profiler not compiled]\n");
4859 }
4860 #endif
4861
4862 #ifdef ELF_HOST_MACHINE
4863 /* In order to use this feature, the backend needs to do three things:
4864
4865    (1) Define ELF_HOST_MACHINE to indicate both what value to
4866        put into the ELF image and to indicate support for the feature.
4867
4868    (2) Define tcg_register_jit.  This should create a buffer containing
4869        the contents of a .debug_frame section that describes the post-
4870        prologue unwind info for the tcg machine.
4871
4872    (3) Call tcg_register_jit_int, with the constructed .debug_frame.
4873 */
4874
4875 /* Begin GDB interface.  THE FOLLOWING MUST MATCH GDB DOCS.  */
4876 typedef enum {
4877     JIT_NOACTION = 0,
4878     JIT_REGISTER_FN,
4879     JIT_UNREGISTER_FN
4880 } jit_actions_t;
4881
4882 struct jit_code_entry {
4883     struct jit_code_entry *next_entry;
4884     struct jit_code_entry *prev_entry;
4885     const void *symfile_addr;
4886     uint64_t symfile_size;
4887 };
4888
4889 struct jit_descriptor {
4890     uint32_t version;
4891     uint32_t action_flag;
4892     struct jit_code_entry *relevant_entry;
4893     struct jit_code_entry *first_entry;
4894 };
4895
4896 void __jit_debug_register_code(void) __attribute__((noinline));
4897 void __jit_debug_register_code(void)
4898 {
4899     asm("");
4900 }
4901
4902 /* Must statically initialize the version, because GDB may check
4903    the version before we can set it.  */
4904 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
4905
4906 /* End GDB interface.  */
4907
4908 static int find_string(const char *strtab, const char *str)
4909 {
4910     const char *p = strtab + 1;
4911
4912     while (1) {
4913         if (strcmp(p, str) == 0) {
4914             return p - strtab;
4915         }
4916         p += strlen(p) + 1;
4917     }
4918 }
4919
4920 static void tcg_register_jit_int(const void *buf_ptr, size_t buf_size,
4921                                  const void *debug_frame,
4922                                  size_t debug_frame_size)
4923 {
4924     struct __attribute__((packed)) DebugInfo {
4925         uint32_t  len;
4926         uint16_t  version;
4927         uint32_t  abbrev;
4928         uint8_t   ptr_size;
4929         uint8_t   cu_die;
4930         uint16_t  cu_lang;
4931         uintptr_t cu_low_pc;
4932         uintptr_t cu_high_pc;
4933         uint8_t   fn_die;
4934         char      fn_name[16];
4935         uintptr_t fn_low_pc;
4936         uintptr_t fn_high_pc;
4937         uint8_t   cu_eoc;
4938     };
4939
4940     struct ElfImage {
4941         ElfW(Ehdr) ehdr;
4942         ElfW(Phdr) phdr;
4943         ElfW(Shdr) shdr[7];
4944         ElfW(Sym)  sym[2];
4945         struct DebugInfo di;
4946         uint8_t    da[24];
4947         char       str[80];
4948     };
4949
4950     struct ElfImage *img;
4951
4952     static const struct ElfImage img_template = {
4953         .ehdr = {
4954             .e_ident[EI_MAG0] = ELFMAG0,
4955             .e_ident[EI_MAG1] = ELFMAG1,
4956             .e_ident[EI_MAG2] = ELFMAG2,
4957             .e_ident[EI_MAG3] = ELFMAG3,
4958             .e_ident[EI_CLASS] = ELF_CLASS,
4959             .e_ident[EI_DATA] = ELF_DATA,
4960             .e_ident[EI_VERSION] = EV_CURRENT,
4961             .e_type = ET_EXEC,
4962             .e_machine = ELF_HOST_MACHINE,
4963             .e_version = EV_CURRENT,
4964             .e_phoff = offsetof(struct ElfImage, phdr),
4965             .e_shoff = offsetof(struct ElfImage, shdr),
4966             .e_ehsize = sizeof(ElfW(Shdr)),
4967             .e_phentsize = sizeof(ElfW(Phdr)),
4968             .e_phnum = 1,
4969             .e_shentsize = sizeof(ElfW(Shdr)),
4970             .e_shnum = ARRAY_SIZE(img->shdr),
4971             .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
4972 #ifdef ELF_HOST_FLAGS
4973             .e_flags = ELF_HOST_FLAGS,
4974 #endif
4975 #ifdef ELF_OSABI
4976             .e_ident[EI_OSABI] = ELF_OSABI,
4977 #endif
4978         },
4979         .phdr = {
4980             .p_type = PT_LOAD,
4981             .p_flags = PF_X,
4982         },
4983         .shdr = {
4984             [0] = { .sh_type = SHT_NULL },
4985             /* Trick: The contents of code_gen_buffer are not present in
4986                this fake ELF file; that got allocated elsewhere.  Therefore
4987                we mark .text as SHT_NOBITS (similar to .bss) so that readers
4988                will not look for contents.  We can record any address.  */
4989             [1] = { /* .text */
4990                 .sh_type = SHT_NOBITS,
4991                 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
4992             },
4993             [2] = { /* .debug_info */
4994                 .sh_type = SHT_PROGBITS,
4995                 .sh_offset = offsetof(struct ElfImage, di),
4996                 .sh_size = sizeof(struct DebugInfo),
4997             },
4998             [3] = { /* .debug_abbrev */
4999                 .sh_type = SHT_PROGBITS,
5000                 .sh_offset = offsetof(struct ElfImage, da),
5001                 .sh_size = sizeof(img->da),
5002             },
5003             [4] = { /* .debug_frame */
5004                 .sh_type = SHT_PROGBITS,
5005                 .sh_offset = sizeof(struct ElfImage),
5006             },
5007             [5] = { /* .symtab */
5008                 .sh_type = SHT_SYMTAB,
5009                 .sh_offset = offsetof(struct ElfImage, sym),
5010                 .sh_size = sizeof(img->sym),
5011                 .sh_info = 1,
5012                 .sh_link = ARRAY_SIZE(img->shdr) - 1,
5013                 .sh_entsize = sizeof(ElfW(Sym)),
5014             },
5015             [6] = { /* .strtab */
5016                 .sh_type = SHT_STRTAB,
5017                 .sh_offset = offsetof(struct ElfImage, str),
5018                 .sh_size = sizeof(img->str),
5019             }
5020         },
5021         .sym = {
5022             [1] = { /* code_gen_buffer */
5023                 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
5024                 .st_shndx = 1,
5025             }
5026         },
5027         .di = {
5028             .len = sizeof(struct DebugInfo) - 4,
5029             .version = 2,
5030             .ptr_size = sizeof(void *),
5031             .cu_die = 1,
5032             .cu_lang = 0x8001,  /* DW_LANG_Mips_Assembler */
5033             .fn_die = 2,
5034             .fn_name = "code_gen_buffer"
5035         },
5036         .da = {
5037             1,          /* abbrev number (the cu) */
5038             0x11, 1,    /* DW_TAG_compile_unit, has children */
5039             0x13, 0x5,  /* DW_AT_language, DW_FORM_data2 */
5040             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
5041             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
5042             0, 0,       /* end of abbrev */
5043             2,          /* abbrev number (the fn) */
5044             0x2e, 0,    /* DW_TAG_subprogram, no children */
5045             0x3, 0x8,   /* DW_AT_name, DW_FORM_string */
5046             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
5047             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
5048             0, 0,       /* end of abbrev */
5049             0           /* no more abbrev */
5050         },
5051         .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
5052                ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
5053     };
5054
5055     /* We only need a single jit entry; statically allocate it.  */
5056     static struct jit_code_entry one_entry;
5057
5058     uintptr_t buf = (uintptr_t)buf_ptr;
5059     size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
5060     DebugFrameHeader *dfh;
5061
5062     img = g_malloc(img_size);
5063     *img = img_template;
5064
5065     img->phdr.p_vaddr = buf;
5066     img->phdr.p_paddr = buf;
5067     img->phdr.p_memsz = buf_size;
5068
5069     img->shdr[1].sh_name = find_string(img->str, ".text");
5070     img->shdr[1].sh_addr = buf;
5071     img->shdr[1].sh_size = buf_size;
5072
5073     img->shdr[2].sh_name = find_string(img->str, ".debug_info");
5074     img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
5075
5076     img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
5077     img->shdr[4].sh_size = debug_frame_size;
5078
5079     img->shdr[5].sh_name = find_string(img->str, ".symtab");
5080     img->shdr[6].sh_name = find_string(img->str, ".strtab");
5081
5082     img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
5083     img->sym[1].st_value = buf;
5084     img->sym[1].st_size = buf_size;
5085
5086     img->di.cu_low_pc = buf;
5087     img->di.cu_high_pc = buf + buf_size;
5088     img->di.fn_low_pc = buf;
5089     img->di.fn_high_pc = buf + buf_size;
5090
5091     dfh = (DebugFrameHeader *)(img + 1);
5092     memcpy(dfh, debug_frame, debug_frame_size);
5093     dfh->fde.func_start = buf;
5094     dfh->fde.func_len = buf_size;
5095
5096 #ifdef DEBUG_JIT
5097     /* Enable this block to be able to debug the ELF image file creation.
5098        One can use readelf, objdump, or other inspection utilities.  */
5099     {
5100         FILE *f = fopen("/tmp/qemu.jit", "w+b");
5101         if (f) {
5102             if (fwrite(img, img_size, 1, f) != img_size) {
5103                 /* Avoid stupid unused return value warning for fwrite.  */
5104             }
5105             fclose(f);
5106         }
5107     }
5108 #endif
5109
5110     one_entry.symfile_addr = img;
5111     one_entry.symfile_size = img_size;
5112
5113     __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
5114     __jit_debug_descriptor.relevant_entry = &one_entry;
5115     __jit_debug_descriptor.first_entry = &one_entry;
5116     __jit_debug_register_code();
5117 }
5118 #else
5119 /* No support for the feature.  Provide the entry point expected by exec.c,
5120    and implement the internal function we declared earlier.  */
5121
5122 static void tcg_register_jit_int(const void *buf, size_t size,
5123                                  const void *debug_frame,
5124                                  size_t debug_frame_size)
5125 {
5126 }
5127
5128 void tcg_register_jit(const void *buf, size_t buf_size)
5129 {
5130 }
5131 #endif /* ELF_HOST_MACHINE */
5132
5133 #if !TCG_TARGET_MAYBE_vec
5134 void tcg_expand_vec_op(TCGOpcode o, TCGType t, unsigned e, TCGArg a0, ...)
5135 {
5136     g_assert_not_reached();
5137 }
5138 #endif
This page took 0.313172 seconds and 4 git commands to generate.