]> Git Repo - qemu.git/blob - tcg/tcg.c
tcg: Emit prologue to the beginning 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_LIVENESS_ANALYSIS
27 #define USE_TCG_OPTIMIZATIONS
28
29 #include "config.h"
30
31 /* Define to jump the ELF file used to communicate with GDB.  */
32 #undef DEBUG_JIT
33
34 #if !defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG)
35 /* define it to suppress various consistency checks (faster) */
36 #define NDEBUG
37 #endif
38
39 #include "qemu-common.h"
40 #include "qemu/host-utils.h"
41 #include "qemu/timer.h"
42
43 /* Note: the long term plan is to reduce the dependencies on the QEMU
44    CPU definitions. Currently they are used for qemu_ld/st
45    instructions */
46 #define NO_CPU_IO_DEFS
47 #include "cpu.h"
48
49 #include "tcg-op.h"
50
51 #if UINTPTR_MAX == UINT32_MAX
52 # define ELF_CLASS  ELFCLASS32
53 #else
54 # define ELF_CLASS  ELFCLASS64
55 #endif
56 #ifdef HOST_WORDS_BIGENDIAN
57 # define ELF_DATA   ELFDATA2MSB
58 #else
59 # define ELF_DATA   ELFDATA2LSB
60 #endif
61
62 #include "elf.h"
63
64 /* Forward declarations for functions declared in tcg-target.c and used here. */
65 static void tcg_target_init(TCGContext *s);
66 static void tcg_target_qemu_prologue(TCGContext *s);
67 static void patch_reloc(tcg_insn_unit *code_ptr, int type,
68                         intptr_t value, intptr_t addend);
69
70 /* The CIE and FDE header definitions will be common to all hosts.  */
71 typedef struct {
72     uint32_t len __attribute__((aligned((sizeof(void *)))));
73     uint32_t id;
74     uint8_t version;
75     char augmentation[1];
76     uint8_t code_align;
77     uint8_t data_align;
78     uint8_t return_column;
79 } DebugFrameCIE;
80
81 typedef struct QEMU_PACKED {
82     uint32_t len __attribute__((aligned((sizeof(void *)))));
83     uint32_t cie_offset;
84     uintptr_t func_start;
85     uintptr_t func_len;
86 } DebugFrameFDEHeader;
87
88 typedef struct QEMU_PACKED {
89     DebugFrameCIE cie;
90     DebugFrameFDEHeader fde;
91 } DebugFrameHeader;
92
93 static void tcg_register_jit_int(void *buf, size_t size,
94                                  const void *debug_frame,
95                                  size_t debug_frame_size)
96     __attribute__((unused));
97
98 /* Forward declarations for functions declared and used in tcg-target.c. */
99 static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str);
100 static void tcg_out_ld(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg1,
101                        intptr_t arg2);
102 static void tcg_out_mov(TCGContext *s, TCGType type, TCGReg ret, TCGReg arg);
103 static void tcg_out_movi(TCGContext *s, TCGType type,
104                          TCGReg ret, tcg_target_long arg);
105 static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args,
106                        const int *const_args);
107 static void tcg_out_st(TCGContext *s, TCGType type, TCGReg arg, TCGReg arg1,
108                        intptr_t arg2);
109 static void tcg_out_call(TCGContext *s, tcg_insn_unit *target);
110 static int tcg_target_const_match(tcg_target_long val, TCGType type,
111                                   const TCGArgConstraint *arg_ct);
112 static void tcg_out_tb_init(TCGContext *s);
113 static void tcg_out_tb_finalize(TCGContext *s);
114
115
116
117 static TCGRegSet tcg_target_available_regs[2];
118 static TCGRegSet tcg_target_call_clobber_regs;
119
120 #if TCG_TARGET_INSN_UNIT_SIZE == 1
121 static __attribute__((unused)) inline void tcg_out8(TCGContext *s, uint8_t v)
122 {
123     *s->code_ptr++ = v;
124 }
125
126 static __attribute__((unused)) inline void tcg_patch8(tcg_insn_unit *p,
127                                                       uint8_t v)
128 {
129     *p = v;
130 }
131 #endif
132
133 #if TCG_TARGET_INSN_UNIT_SIZE <= 2
134 static __attribute__((unused)) inline void tcg_out16(TCGContext *s, uint16_t v)
135 {
136     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
137         *s->code_ptr++ = v;
138     } else {
139         tcg_insn_unit *p = s->code_ptr;
140         memcpy(p, &v, sizeof(v));
141         s->code_ptr = p + (2 / TCG_TARGET_INSN_UNIT_SIZE);
142     }
143 }
144
145 static __attribute__((unused)) inline void tcg_patch16(tcg_insn_unit *p,
146                                                        uint16_t v)
147 {
148     if (TCG_TARGET_INSN_UNIT_SIZE == 2) {
149         *p = v;
150     } else {
151         memcpy(p, &v, sizeof(v));
152     }
153 }
154 #endif
155
156 #if TCG_TARGET_INSN_UNIT_SIZE <= 4
157 static __attribute__((unused)) inline void tcg_out32(TCGContext *s, uint32_t v)
158 {
159     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
160         *s->code_ptr++ = v;
161     } else {
162         tcg_insn_unit *p = s->code_ptr;
163         memcpy(p, &v, sizeof(v));
164         s->code_ptr = p + (4 / TCG_TARGET_INSN_UNIT_SIZE);
165     }
166 }
167
168 static __attribute__((unused)) inline void tcg_patch32(tcg_insn_unit *p,
169                                                        uint32_t v)
170 {
171     if (TCG_TARGET_INSN_UNIT_SIZE == 4) {
172         *p = v;
173     } else {
174         memcpy(p, &v, sizeof(v));
175     }
176 }
177 #endif
178
179 #if TCG_TARGET_INSN_UNIT_SIZE <= 8
180 static __attribute__((unused)) inline void tcg_out64(TCGContext *s, uint64_t v)
181 {
182     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
183         *s->code_ptr++ = v;
184     } else {
185         tcg_insn_unit *p = s->code_ptr;
186         memcpy(p, &v, sizeof(v));
187         s->code_ptr = p + (8 / TCG_TARGET_INSN_UNIT_SIZE);
188     }
189 }
190
191 static __attribute__((unused)) inline void tcg_patch64(tcg_insn_unit *p,
192                                                        uint64_t v)
193 {
194     if (TCG_TARGET_INSN_UNIT_SIZE == 8) {
195         *p = v;
196     } else {
197         memcpy(p, &v, sizeof(v));
198     }
199 }
200 #endif
201
202 /* label relocation processing */
203
204 static void tcg_out_reloc(TCGContext *s, tcg_insn_unit *code_ptr, int type,
205                           TCGLabel *l, intptr_t addend)
206 {
207     TCGRelocation *r;
208
209     if (l->has_value) {
210         /* FIXME: This may break relocations on RISC targets that
211            modify instruction fields in place.  The caller may not have 
212            written the initial value.  */
213         patch_reloc(code_ptr, type, l->u.value, addend);
214     } else {
215         /* add a new relocation entry */
216         r = tcg_malloc(sizeof(TCGRelocation));
217         r->type = type;
218         r->ptr = code_ptr;
219         r->addend = addend;
220         r->next = l->u.first_reloc;
221         l->u.first_reloc = r;
222     }
223 }
224
225 static void tcg_out_label(TCGContext *s, TCGLabel *l, tcg_insn_unit *ptr)
226 {
227     intptr_t value = (intptr_t)ptr;
228     TCGRelocation *r;
229
230     assert(!l->has_value);
231
232     for (r = l->u.first_reloc; r != NULL; r = r->next) {
233         patch_reloc(r->ptr, r->type, value, r->addend);
234     }
235
236     l->has_value = 1;
237     l->u.value_ptr = ptr;
238 }
239
240 TCGLabel *gen_new_label(void)
241 {
242     TCGContext *s = &tcg_ctx;
243     TCGLabel *l = tcg_malloc(sizeof(TCGLabel));
244
245     *l = (TCGLabel){
246         .id = s->nb_labels++
247     };
248
249     return l;
250 }
251
252 #include "tcg-target.c"
253
254 /* pool based memory allocation */
255 void *tcg_malloc_internal(TCGContext *s, int size)
256 {
257     TCGPool *p;
258     int pool_size;
259     
260     if (size > TCG_POOL_CHUNK_SIZE) {
261         /* big malloc: insert a new pool (XXX: could optimize) */
262         p = g_malloc(sizeof(TCGPool) + size);
263         p->size = size;
264         p->next = s->pool_first_large;
265         s->pool_first_large = p;
266         return p->data;
267     } else {
268         p = s->pool_current;
269         if (!p) {
270             p = s->pool_first;
271             if (!p)
272                 goto new_pool;
273         } else {
274             if (!p->next) {
275             new_pool:
276                 pool_size = TCG_POOL_CHUNK_SIZE;
277                 p = g_malloc(sizeof(TCGPool) + pool_size);
278                 p->size = pool_size;
279                 p->next = NULL;
280                 if (s->pool_current) 
281                     s->pool_current->next = p;
282                 else
283                     s->pool_first = p;
284             } else {
285                 p = p->next;
286             }
287         }
288     }
289     s->pool_current = p;
290     s->pool_cur = p->data + size;
291     s->pool_end = p->data + p->size;
292     return p->data;
293 }
294
295 void tcg_pool_reset(TCGContext *s)
296 {
297     TCGPool *p, *t;
298     for (p = s->pool_first_large; p; p = t) {
299         t = p->next;
300         g_free(p);
301     }
302     s->pool_first_large = NULL;
303     s->pool_cur = s->pool_end = NULL;
304     s->pool_current = NULL;
305 }
306
307 typedef struct TCGHelperInfo {
308     void *func;
309     const char *name;
310     unsigned flags;
311     unsigned sizemask;
312 } TCGHelperInfo;
313
314 #include "exec/helper-proto.h"
315
316 static const TCGHelperInfo all_helpers[] = {
317 #include "exec/helper-tcg.h"
318 };
319
320 void tcg_context_init(TCGContext *s)
321 {
322     int op, total_args, n, i;
323     TCGOpDef *def;
324     TCGArgConstraint *args_ct;
325     int *sorted_args;
326     GHashTable *helper_table;
327
328     memset(s, 0, sizeof(*s));
329     s->nb_globals = 0;
330     
331     /* Count total number of arguments and allocate the corresponding
332        space */
333     total_args = 0;
334     for(op = 0; op < NB_OPS; op++) {
335         def = &tcg_op_defs[op];
336         n = def->nb_iargs + def->nb_oargs;
337         total_args += n;
338     }
339
340     args_ct = g_malloc(sizeof(TCGArgConstraint) * total_args);
341     sorted_args = g_malloc(sizeof(int) * total_args);
342
343     for(op = 0; op < NB_OPS; op++) {
344         def = &tcg_op_defs[op];
345         def->args_ct = args_ct;
346         def->sorted_args = sorted_args;
347         n = def->nb_iargs + def->nb_oargs;
348         sorted_args += n;
349         args_ct += n;
350     }
351
352     /* Register helpers.  */
353     /* Use g_direct_hash/equal for direct pointer comparisons on func.  */
354     s->helpers = helper_table = g_hash_table_new(NULL, NULL);
355
356     for (i = 0; i < ARRAY_SIZE(all_helpers); ++i) {
357         g_hash_table_insert(helper_table, (gpointer)all_helpers[i].func,
358                             (gpointer)&all_helpers[i]);
359     }
360
361     tcg_target_init(s);
362 }
363
364 void tcg_prologue_init(TCGContext *s)
365 {
366     size_t prologue_size, total_size;
367     void *buf0, *buf1;
368
369     /* Put the prologue at the beginning of code_gen_buffer.  */
370     buf0 = s->code_gen_buffer;
371     s->code_ptr = buf0;
372     s->code_buf = buf0;
373     s->code_gen_prologue = buf0;
374
375     /* Generate the prologue.  */
376     tcg_target_qemu_prologue(s);
377     buf1 = s->code_ptr;
378     flush_icache_range((uintptr_t)buf0, (uintptr_t)buf1);
379
380     /* Deduct the prologue from the buffer.  */
381     prologue_size = tcg_current_code_size(s);
382     s->code_gen_ptr = buf1;
383     s->code_gen_buffer = buf1;
384     s->code_buf = buf1;
385     total_size = s->code_gen_buffer_size - prologue_size;
386     s->code_gen_buffer_size = total_size;
387
388     /* Compute a high-water mark, at which we voluntarily flush the
389        buffer and start over.  */
390     s->code_gen_buffer_max_size = total_size - TCG_MAX_OP_SIZE * OPC_BUF_SIZE;
391
392     tcg_register_jit(s->code_gen_buffer, total_size);
393
394 #ifdef DEBUG_DISAS
395     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
396         qemu_log("PROLOGUE: [size=%zu]\n", prologue_size);
397         log_disas(buf0, prologue_size);
398         qemu_log("\n");
399         qemu_log_flush();
400     }
401 #endif
402 }
403
404 void tcg_set_frame(TCGContext *s, int reg, intptr_t start, intptr_t size)
405 {
406     s->frame_start = start;
407     s->frame_end = start + size;
408     s->frame_reg = reg;
409 }
410
411 void tcg_func_start(TCGContext *s)
412 {
413     tcg_pool_reset(s);
414     s->nb_temps = s->nb_globals;
415
416     /* No temps have been previously allocated for size or locality.  */
417     memset(s->free_temps, 0, sizeof(s->free_temps));
418
419     s->nb_labels = 0;
420     s->current_frame_offset = s->frame_start;
421
422 #ifdef CONFIG_DEBUG_TCG
423     s->goto_tb_issue_mask = 0;
424 #endif
425
426     s->gen_first_op_idx = 0;
427     s->gen_last_op_idx = -1;
428     s->gen_next_op_idx = 0;
429     s->gen_next_parm_idx = 0;
430
431     s->be = tcg_malloc(sizeof(TCGBackendData));
432 }
433
434 static inline void tcg_temp_alloc(TCGContext *s, int n)
435 {
436     if (n > TCG_MAX_TEMPS)
437         tcg_abort();
438 }
439
440 static inline int tcg_global_reg_new_internal(TCGType type, int reg,
441                                               const char *name)
442 {
443     TCGContext *s = &tcg_ctx;
444     TCGTemp *ts;
445     int idx;
446
447 #if TCG_TARGET_REG_BITS == 32
448     if (type != TCG_TYPE_I32)
449         tcg_abort();
450 #endif
451     if (tcg_regset_test_reg(s->reserved_regs, reg))
452         tcg_abort();
453     idx = s->nb_globals;
454     tcg_temp_alloc(s, s->nb_globals + 1);
455     ts = &s->temps[s->nb_globals];
456     ts->base_type = type;
457     ts->type = type;
458     ts->fixed_reg = 1;
459     ts->reg = reg;
460     ts->name = name;
461     s->nb_globals++;
462     tcg_regset_set_reg(s->reserved_regs, reg);
463     return idx;
464 }
465
466 TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name)
467 {
468     int idx;
469
470     idx = tcg_global_reg_new_internal(TCG_TYPE_I32, reg, name);
471     return MAKE_TCGV_I32(idx);
472 }
473
474 TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name)
475 {
476     int idx;
477
478     idx = tcg_global_reg_new_internal(TCG_TYPE_I64, reg, name);
479     return MAKE_TCGV_I64(idx);
480 }
481
482 static inline int tcg_global_mem_new_internal(TCGType type, int reg,
483                                               intptr_t offset,
484                                               const char *name)
485 {
486     TCGContext *s = &tcg_ctx;
487     TCGTemp *ts;
488     int idx;
489
490     idx = s->nb_globals;
491 #if TCG_TARGET_REG_BITS == 32
492     if (type == TCG_TYPE_I64) {
493         char buf[64];
494         tcg_temp_alloc(s, s->nb_globals + 2);
495         ts = &s->temps[s->nb_globals];
496         ts->base_type = type;
497         ts->type = TCG_TYPE_I32;
498         ts->fixed_reg = 0;
499         ts->mem_allocated = 1;
500         ts->mem_reg = reg;
501 #ifdef HOST_WORDS_BIGENDIAN
502         ts->mem_offset = offset + 4;
503 #else
504         ts->mem_offset = offset;
505 #endif
506         pstrcpy(buf, sizeof(buf), name);
507         pstrcat(buf, sizeof(buf), "_0");
508         ts->name = strdup(buf);
509         ts++;
510
511         ts->base_type = type;
512         ts->type = TCG_TYPE_I32;
513         ts->fixed_reg = 0;
514         ts->mem_allocated = 1;
515         ts->mem_reg = reg;
516 #ifdef HOST_WORDS_BIGENDIAN
517         ts->mem_offset = offset;
518 #else
519         ts->mem_offset = offset + 4;
520 #endif
521         pstrcpy(buf, sizeof(buf), name);
522         pstrcat(buf, sizeof(buf), "_1");
523         ts->name = strdup(buf);
524
525         s->nb_globals += 2;
526     } else
527 #endif
528     {
529         tcg_temp_alloc(s, s->nb_globals + 1);
530         ts = &s->temps[s->nb_globals];
531         ts->base_type = type;
532         ts->type = type;
533         ts->fixed_reg = 0;
534         ts->mem_allocated = 1;
535         ts->mem_reg = reg;
536         ts->mem_offset = offset;
537         ts->name = name;
538         s->nb_globals++;
539     }
540     return idx;
541 }
542
543 TCGv_i32 tcg_global_mem_new_i32(int reg, intptr_t offset, const char *name)
544 {
545     int idx = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name);
546     return MAKE_TCGV_I32(idx);
547 }
548
549 TCGv_i64 tcg_global_mem_new_i64(int reg, intptr_t offset, const char *name)
550 {
551     int idx = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name);
552     return MAKE_TCGV_I64(idx);
553 }
554
555 static inline int tcg_temp_new_internal(TCGType type, int temp_local)
556 {
557     TCGContext *s = &tcg_ctx;
558     TCGTemp *ts;
559     int idx, k;
560
561     k = type + (temp_local ? TCG_TYPE_COUNT : 0);
562     idx = find_first_bit(s->free_temps[k].l, TCG_MAX_TEMPS);
563     if (idx < TCG_MAX_TEMPS) {
564         /* There is already an available temp with the right type.  */
565         clear_bit(idx, s->free_temps[k].l);
566
567         ts = &s->temps[idx];
568         ts->temp_allocated = 1;
569         assert(ts->base_type == type);
570         assert(ts->temp_local == temp_local);
571     } else {
572         idx = s->nb_temps;
573 #if TCG_TARGET_REG_BITS == 32
574         if (type == TCG_TYPE_I64) {
575             tcg_temp_alloc(s, s->nb_temps + 2);
576             ts = &s->temps[s->nb_temps];
577             ts->base_type = type;
578             ts->type = TCG_TYPE_I32;
579             ts->temp_allocated = 1;
580             ts->temp_local = temp_local;
581             ts->name = NULL;
582             ts++;
583             ts->base_type = type;
584             ts->type = TCG_TYPE_I32;
585             ts->temp_allocated = 1;
586             ts->temp_local = temp_local;
587             ts->name = NULL;
588             s->nb_temps += 2;
589         } else
590 #endif
591         {
592             tcg_temp_alloc(s, s->nb_temps + 1);
593             ts = &s->temps[s->nb_temps];
594             ts->base_type = type;
595             ts->type = type;
596             ts->temp_allocated = 1;
597             ts->temp_local = temp_local;
598             ts->name = NULL;
599             s->nb_temps++;
600         }
601     }
602
603 #if defined(CONFIG_DEBUG_TCG)
604     s->temps_in_use++;
605 #endif
606     return idx;
607 }
608
609 TCGv_i32 tcg_temp_new_internal_i32(int temp_local)
610 {
611     int idx;
612
613     idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local);
614     return MAKE_TCGV_I32(idx);
615 }
616
617 TCGv_i64 tcg_temp_new_internal_i64(int temp_local)
618 {
619     int idx;
620
621     idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local);
622     return MAKE_TCGV_I64(idx);
623 }
624
625 static void tcg_temp_free_internal(int idx)
626 {
627     TCGContext *s = &tcg_ctx;
628     TCGTemp *ts;
629     int k;
630
631 #if defined(CONFIG_DEBUG_TCG)
632     s->temps_in_use--;
633     if (s->temps_in_use < 0) {
634         fprintf(stderr, "More temporaries freed than allocated!\n");
635     }
636 #endif
637
638     assert(idx >= s->nb_globals && idx < s->nb_temps);
639     ts = &s->temps[idx];
640     assert(ts->temp_allocated != 0);
641     ts->temp_allocated = 0;
642
643     k = ts->base_type + (ts->temp_local ? TCG_TYPE_COUNT : 0);
644     set_bit(idx, s->free_temps[k].l);
645 }
646
647 void tcg_temp_free_i32(TCGv_i32 arg)
648 {
649     tcg_temp_free_internal(GET_TCGV_I32(arg));
650 }
651
652 void tcg_temp_free_i64(TCGv_i64 arg)
653 {
654     tcg_temp_free_internal(GET_TCGV_I64(arg));
655 }
656
657 TCGv_i32 tcg_const_i32(int32_t val)
658 {
659     TCGv_i32 t0;
660     t0 = tcg_temp_new_i32();
661     tcg_gen_movi_i32(t0, val);
662     return t0;
663 }
664
665 TCGv_i64 tcg_const_i64(int64_t val)
666 {
667     TCGv_i64 t0;
668     t0 = tcg_temp_new_i64();
669     tcg_gen_movi_i64(t0, val);
670     return t0;
671 }
672
673 TCGv_i32 tcg_const_local_i32(int32_t val)
674 {
675     TCGv_i32 t0;
676     t0 = tcg_temp_local_new_i32();
677     tcg_gen_movi_i32(t0, val);
678     return t0;
679 }
680
681 TCGv_i64 tcg_const_local_i64(int64_t val)
682 {
683     TCGv_i64 t0;
684     t0 = tcg_temp_local_new_i64();
685     tcg_gen_movi_i64(t0, val);
686     return t0;
687 }
688
689 #if defined(CONFIG_DEBUG_TCG)
690 void tcg_clear_temp_count(void)
691 {
692     TCGContext *s = &tcg_ctx;
693     s->temps_in_use = 0;
694 }
695
696 int tcg_check_temp_count(void)
697 {
698     TCGContext *s = &tcg_ctx;
699     if (s->temps_in_use) {
700         /* Clear the count so that we don't give another
701          * warning immediately next time around.
702          */
703         s->temps_in_use = 0;
704         return 1;
705     }
706     return 0;
707 }
708 #endif
709
710 /* Note: we convert the 64 bit args to 32 bit and do some alignment
711    and endian swap. Maybe it would be better to do the alignment
712    and endian swap in tcg_reg_alloc_call(). */
713 void tcg_gen_callN(TCGContext *s, void *func, TCGArg ret,
714                    int nargs, TCGArg *args)
715 {
716     int i, real_args, nb_rets, pi, pi_first;
717     unsigned sizemask, flags;
718     TCGHelperInfo *info;
719
720     info = g_hash_table_lookup(s->helpers, (gpointer)func);
721     flags = info->flags;
722     sizemask = info->sizemask;
723
724 #if defined(__sparc__) && !defined(__arch64__) \
725     && !defined(CONFIG_TCG_INTERPRETER)
726     /* We have 64-bit values in one register, but need to pass as two
727        separate parameters.  Split them.  */
728     int orig_sizemask = sizemask;
729     int orig_nargs = nargs;
730     TCGv_i64 retl, reth;
731
732     TCGV_UNUSED_I64(retl);
733     TCGV_UNUSED_I64(reth);
734     if (sizemask != 0) {
735         TCGArg *split_args = __builtin_alloca(sizeof(TCGArg) * nargs * 2);
736         for (i = real_args = 0; i < nargs; ++i) {
737             int is_64bit = sizemask & (1 << (i+1)*2);
738             if (is_64bit) {
739                 TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
740                 TCGv_i32 h = tcg_temp_new_i32();
741                 TCGv_i32 l = tcg_temp_new_i32();
742                 tcg_gen_extr_i64_i32(l, h, orig);
743                 split_args[real_args++] = GET_TCGV_I32(h);
744                 split_args[real_args++] = GET_TCGV_I32(l);
745             } else {
746                 split_args[real_args++] = args[i];
747             }
748         }
749         nargs = real_args;
750         args = split_args;
751         sizemask = 0;
752     }
753 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
754     for (i = 0; i < nargs; ++i) {
755         int is_64bit = sizemask & (1 << (i+1)*2);
756         int is_signed = sizemask & (2 << (i+1)*2);
757         if (!is_64bit) {
758             TCGv_i64 temp = tcg_temp_new_i64();
759             TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
760             if (is_signed) {
761                 tcg_gen_ext32s_i64(temp, orig);
762             } else {
763                 tcg_gen_ext32u_i64(temp, orig);
764             }
765             args[i] = GET_TCGV_I64(temp);
766         }
767     }
768 #endif /* TCG_TARGET_EXTEND_ARGS */
769
770     pi_first = pi = s->gen_next_parm_idx;
771     if (ret != TCG_CALL_DUMMY_ARG) {
772 #if defined(__sparc__) && !defined(__arch64__) \
773     && !defined(CONFIG_TCG_INTERPRETER)
774         if (orig_sizemask & 1) {
775             /* The 32-bit ABI is going to return the 64-bit value in
776                the %o0/%o1 register pair.  Prepare for this by using
777                two return temporaries, and reassemble below.  */
778             retl = tcg_temp_new_i64();
779             reth = tcg_temp_new_i64();
780             s->gen_opparam_buf[pi++] = GET_TCGV_I64(reth);
781             s->gen_opparam_buf[pi++] = GET_TCGV_I64(retl);
782             nb_rets = 2;
783         } else {
784             s->gen_opparam_buf[pi++] = ret;
785             nb_rets = 1;
786         }
787 #else
788         if (TCG_TARGET_REG_BITS < 64 && (sizemask & 1)) {
789 #ifdef HOST_WORDS_BIGENDIAN
790             s->gen_opparam_buf[pi++] = ret + 1;
791             s->gen_opparam_buf[pi++] = ret;
792 #else
793             s->gen_opparam_buf[pi++] = ret;
794             s->gen_opparam_buf[pi++] = ret + 1;
795 #endif
796             nb_rets = 2;
797         } else {
798             s->gen_opparam_buf[pi++] = ret;
799             nb_rets = 1;
800         }
801 #endif
802     } else {
803         nb_rets = 0;
804     }
805     real_args = 0;
806     for (i = 0; i < nargs; i++) {
807         int is_64bit = sizemask & (1 << (i+1)*2);
808         if (TCG_TARGET_REG_BITS < 64 && is_64bit) {
809 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
810             /* some targets want aligned 64 bit args */
811             if (real_args & 1) {
812                 s->gen_opparam_buf[pi++] = TCG_CALL_DUMMY_ARG;
813                 real_args++;
814             }
815 #endif
816             /* If stack grows up, then we will be placing successive
817                arguments at lower addresses, which means we need to
818                reverse the order compared to how we would normally
819                treat either big or little-endian.  For those arguments
820                that will wind up in registers, this still works for
821                HPPA (the only current STACK_GROWSUP target) since the
822                argument registers are *also* allocated in decreasing
823                order.  If another such target is added, this logic may
824                have to get more complicated to differentiate between
825                stack arguments and register arguments.  */
826 #if defined(HOST_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
827             s->gen_opparam_buf[pi++] = args[i] + 1;
828             s->gen_opparam_buf[pi++] = args[i];
829 #else
830             s->gen_opparam_buf[pi++] = args[i];
831             s->gen_opparam_buf[pi++] = args[i] + 1;
832 #endif
833             real_args += 2;
834             continue;
835         }
836
837         s->gen_opparam_buf[pi++] = args[i];
838         real_args++;
839     }
840     s->gen_opparam_buf[pi++] = (uintptr_t)func;
841     s->gen_opparam_buf[pi++] = flags;
842
843     i = s->gen_next_op_idx;
844     tcg_debug_assert(i < OPC_BUF_SIZE);
845     tcg_debug_assert(pi <= OPPARAM_BUF_SIZE);
846
847     /* Set links for sequential allocation during translation.  */
848     s->gen_op_buf[i] = (TCGOp){
849         .opc = INDEX_op_call,
850         .callo = nb_rets,
851         .calli = real_args,
852         .args = pi_first,
853         .prev = i - 1,
854         .next = i + 1
855     };
856
857     /* Make sure the calli field didn't overflow.  */
858     tcg_debug_assert(s->gen_op_buf[i].calli == real_args);
859
860     s->gen_last_op_idx = i;
861     s->gen_next_op_idx = i + 1;
862     s->gen_next_parm_idx = pi;
863
864 #if defined(__sparc__) && !defined(__arch64__) \
865     && !defined(CONFIG_TCG_INTERPRETER)
866     /* Free all of the parts we allocated above.  */
867     for (i = real_args = 0; i < orig_nargs; ++i) {
868         int is_64bit = orig_sizemask & (1 << (i+1)*2);
869         if (is_64bit) {
870             TCGv_i32 h = MAKE_TCGV_I32(args[real_args++]);
871             TCGv_i32 l = MAKE_TCGV_I32(args[real_args++]);
872             tcg_temp_free_i32(h);
873             tcg_temp_free_i32(l);
874         } else {
875             real_args++;
876         }
877     }
878     if (orig_sizemask & 1) {
879         /* The 32-bit ABI returned two 32-bit pieces.  Re-assemble them.
880            Note that describing these as TCGv_i64 eliminates an unnecessary
881            zero-extension that tcg_gen_concat_i32_i64 would create.  */
882         tcg_gen_concat32_i64(MAKE_TCGV_I64(ret), retl, reth);
883         tcg_temp_free_i64(retl);
884         tcg_temp_free_i64(reth);
885     }
886 #elif defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
887     for (i = 0; i < nargs; ++i) {
888         int is_64bit = sizemask & (1 << (i+1)*2);
889         if (!is_64bit) {
890             TCGv_i64 temp = MAKE_TCGV_I64(args[i]);
891             tcg_temp_free_i64(temp);
892         }
893     }
894 #endif /* TCG_TARGET_EXTEND_ARGS */
895 }
896
897 static void tcg_reg_alloc_start(TCGContext *s)
898 {
899     int i;
900     TCGTemp *ts;
901     for(i = 0; i < s->nb_globals; i++) {
902         ts = &s->temps[i];
903         if (ts->fixed_reg) {
904             ts->val_type = TEMP_VAL_REG;
905         } else {
906             ts->val_type = TEMP_VAL_MEM;
907         }
908     }
909     for(i = s->nb_globals; i < s->nb_temps; i++) {
910         ts = &s->temps[i];
911         if (ts->temp_local) {
912             ts->val_type = TEMP_VAL_MEM;
913         } else {
914             ts->val_type = TEMP_VAL_DEAD;
915         }
916         ts->mem_allocated = 0;
917         ts->fixed_reg = 0;
918     }
919     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
920         s->reg_to_temp[i] = -1;
921     }
922 }
923
924 static char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size,
925                                  int idx)
926 {
927     TCGTemp *ts;
928
929     assert(idx >= 0 && idx < s->nb_temps);
930     ts = &s->temps[idx];
931     if (idx < s->nb_globals) {
932         pstrcpy(buf, buf_size, ts->name);
933     } else {
934         if (ts->temp_local) 
935             snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
936         else
937             snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
938     }
939     return buf;
940 }
941
942 char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg)
943 {
944     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I32(arg));
945 }
946
947 char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg)
948 {
949     return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I64(arg));
950 }
951
952 /* Find helper name.  */
953 static inline const char *tcg_find_helper(TCGContext *s, uintptr_t val)
954 {
955     const char *ret = NULL;
956     if (s->helpers) {
957         TCGHelperInfo *info = g_hash_table_lookup(s->helpers, (gpointer)val);
958         if (info) {
959             ret = info->name;
960         }
961     }
962     return ret;
963 }
964
965 static const char * const cond_name[] =
966 {
967     [TCG_COND_NEVER] = "never",
968     [TCG_COND_ALWAYS] = "always",
969     [TCG_COND_EQ] = "eq",
970     [TCG_COND_NE] = "ne",
971     [TCG_COND_LT] = "lt",
972     [TCG_COND_GE] = "ge",
973     [TCG_COND_LE] = "le",
974     [TCG_COND_GT] = "gt",
975     [TCG_COND_LTU] = "ltu",
976     [TCG_COND_GEU] = "geu",
977     [TCG_COND_LEU] = "leu",
978     [TCG_COND_GTU] = "gtu"
979 };
980
981 static const char * const ldst_name[] =
982 {
983     [MO_UB]   = "ub",
984     [MO_SB]   = "sb",
985     [MO_LEUW] = "leuw",
986     [MO_LESW] = "lesw",
987     [MO_LEUL] = "leul",
988     [MO_LESL] = "lesl",
989     [MO_LEQ]  = "leq",
990     [MO_BEUW] = "beuw",
991     [MO_BESW] = "besw",
992     [MO_BEUL] = "beul",
993     [MO_BESL] = "besl",
994     [MO_BEQ]  = "beq",
995 };
996
997 void tcg_dump_ops(TCGContext *s)
998 {
999     char buf[128];
1000     TCGOp *op;
1001     int oi;
1002
1003     for (oi = s->gen_first_op_idx; oi >= 0; oi = op->next) {
1004         int i, k, nb_oargs, nb_iargs, nb_cargs;
1005         const TCGOpDef *def;
1006         const TCGArg *args;
1007         TCGOpcode c;
1008
1009         op = &s->gen_op_buf[oi];
1010         c = op->opc;
1011         def = &tcg_op_defs[c];
1012         args = &s->gen_opparam_buf[op->args];
1013
1014         if (c == INDEX_op_insn_start) {
1015             qemu_log("%s ----", oi != s->gen_first_op_idx ? "\n" : "");
1016
1017             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
1018                 target_ulong a;
1019 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
1020                 a = ((target_ulong)args[i * 2 + 1] << 32) | args[i * 2];
1021 #else
1022                 a = args[i];
1023 #endif
1024                 qemu_log(" " TARGET_FMT_lx, a);
1025             }
1026         } else if (c == INDEX_op_call) {
1027             /* variable number of arguments */
1028             nb_oargs = op->callo;
1029             nb_iargs = op->calli;
1030             nb_cargs = def->nb_cargs;
1031
1032             /* function name, flags, out args */
1033             qemu_log(" %s %s,$0x%" TCG_PRIlx ",$%d", def->name,
1034                      tcg_find_helper(s, args[nb_oargs + nb_iargs]),
1035                      args[nb_oargs + nb_iargs + 1], nb_oargs);
1036             for (i = 0; i < nb_oargs; i++) {
1037                 qemu_log(",%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1038                                                    args[i]));
1039             }
1040             for (i = 0; i < nb_iargs; i++) {
1041                 TCGArg arg = args[nb_oargs + i];
1042                 const char *t = "<dummy>";
1043                 if (arg != TCG_CALL_DUMMY_ARG) {
1044                     t = tcg_get_arg_str_idx(s, buf, sizeof(buf), arg);
1045                 }
1046                 qemu_log(",%s", t);
1047             }
1048         } else {
1049             qemu_log(" %s ", def->name);
1050
1051             nb_oargs = def->nb_oargs;
1052             nb_iargs = def->nb_iargs;
1053             nb_cargs = def->nb_cargs;
1054
1055             k = 0;
1056             for (i = 0; i < nb_oargs; i++) {
1057                 if (k != 0) {
1058                     qemu_log(",");
1059                 }
1060                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1061                                                    args[k++]));
1062             }
1063             for (i = 0; i < nb_iargs; i++) {
1064                 if (k != 0) {
1065                     qemu_log(",");
1066                 }
1067                 qemu_log("%s", tcg_get_arg_str_idx(s, buf, sizeof(buf),
1068                                                    args[k++]));
1069             }
1070             switch (c) {
1071             case INDEX_op_brcond_i32:
1072             case INDEX_op_setcond_i32:
1073             case INDEX_op_movcond_i32:
1074             case INDEX_op_brcond2_i32:
1075             case INDEX_op_setcond2_i32:
1076             case INDEX_op_brcond_i64:
1077             case INDEX_op_setcond_i64:
1078             case INDEX_op_movcond_i64:
1079                 if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]]) {
1080                     qemu_log(",%s", cond_name[args[k++]]);
1081                 } else {
1082                     qemu_log(",$0x%" TCG_PRIlx, args[k++]);
1083                 }
1084                 i = 1;
1085                 break;
1086             case INDEX_op_qemu_ld_i32:
1087             case INDEX_op_qemu_st_i32:
1088             case INDEX_op_qemu_ld_i64:
1089             case INDEX_op_qemu_st_i64:
1090                 {
1091                     TCGMemOpIdx oi = args[k++];
1092                     TCGMemOp op = get_memop(oi);
1093                     unsigned ix = get_mmuidx(oi);
1094
1095                     if (op & ~(MO_AMASK | MO_BSWAP | MO_SSIZE)) {
1096                         qemu_log(",$0x%x,%u", op, ix);
1097                     } else {
1098                         const char *s_al = "", *s_op;
1099                         if (op & MO_AMASK) {
1100                             if ((op & MO_AMASK) == MO_ALIGN) {
1101                                 s_al = "al+";
1102                             } else {
1103                                 s_al = "un+";
1104                             }
1105                         }
1106                         s_op = ldst_name[op & (MO_BSWAP | MO_SSIZE)];
1107                         qemu_log(",%s%s,%u", s_al, s_op, ix);
1108                     }
1109                     i = 1;
1110                 }
1111                 break;
1112             default:
1113                 i = 0;
1114                 break;
1115             }
1116             switch (c) {
1117             case INDEX_op_set_label:
1118             case INDEX_op_br:
1119             case INDEX_op_brcond_i32:
1120             case INDEX_op_brcond_i64:
1121             case INDEX_op_brcond2_i32:
1122                 qemu_log("%s$L%d", k ? "," : "", arg_label(args[k])->id);
1123                 i++, k++;
1124                 break;
1125             default:
1126                 break;
1127             }
1128             for (; i < nb_cargs; i++, k++) {
1129                 qemu_log("%s$0x%" TCG_PRIlx, k ? "," : "", args[k]);
1130             }
1131         }
1132         qemu_log("\n");
1133     }
1134 }
1135
1136 /* we give more priority to constraints with less registers */
1137 static int get_constraint_priority(const TCGOpDef *def, int k)
1138 {
1139     const TCGArgConstraint *arg_ct;
1140
1141     int i, n;
1142     arg_ct = &def->args_ct[k];
1143     if (arg_ct->ct & TCG_CT_ALIAS) {
1144         /* an alias is equivalent to a single register */
1145         n = 1;
1146     } else {
1147         if (!(arg_ct->ct & TCG_CT_REG))
1148             return 0;
1149         n = 0;
1150         for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1151             if (tcg_regset_test_reg(arg_ct->u.regs, i))
1152                 n++;
1153         }
1154     }
1155     return TCG_TARGET_NB_REGS - n + 1;
1156 }
1157
1158 /* sort from highest priority to lowest */
1159 static void sort_constraints(TCGOpDef *def, int start, int n)
1160 {
1161     int i, j, p1, p2, tmp;
1162
1163     for(i = 0; i < n; i++)
1164         def->sorted_args[start + i] = start + i;
1165     if (n <= 1)
1166         return;
1167     for(i = 0; i < n - 1; i++) {
1168         for(j = i + 1; j < n; j++) {
1169             p1 = get_constraint_priority(def, def->sorted_args[start + i]);
1170             p2 = get_constraint_priority(def, def->sorted_args[start + j]);
1171             if (p1 < p2) {
1172                 tmp = def->sorted_args[start + i];
1173                 def->sorted_args[start + i] = def->sorted_args[start + j];
1174                 def->sorted_args[start + j] = tmp;
1175             }
1176         }
1177     }
1178 }
1179
1180 void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
1181 {
1182     TCGOpcode op;
1183     TCGOpDef *def;
1184     const char *ct_str;
1185     int i, nb_args;
1186
1187     for(;;) {
1188         if (tdefs->op == (TCGOpcode)-1)
1189             break;
1190         op = tdefs->op;
1191         assert((unsigned)op < NB_OPS);
1192         def = &tcg_op_defs[op];
1193 #if defined(CONFIG_DEBUG_TCG)
1194         /* Duplicate entry in op definitions? */
1195         assert(!def->used);
1196         def->used = 1;
1197 #endif
1198         nb_args = def->nb_iargs + def->nb_oargs;
1199         for(i = 0; i < nb_args; i++) {
1200             ct_str = tdefs->args_ct_str[i];
1201             /* Incomplete TCGTargetOpDef entry? */
1202             assert(ct_str != NULL);
1203             tcg_regset_clear(def->args_ct[i].u.regs);
1204             def->args_ct[i].ct = 0;
1205             if (ct_str[0] >= '0' && ct_str[0] <= '9') {
1206                 int oarg;
1207                 oarg = ct_str[0] - '0';
1208                 assert(oarg < def->nb_oargs);
1209                 assert(def->args_ct[oarg].ct & TCG_CT_REG);
1210                 /* TCG_CT_ALIAS is for the output arguments. The input
1211                    argument is tagged with TCG_CT_IALIAS. */
1212                 def->args_ct[i] = def->args_ct[oarg];
1213                 def->args_ct[oarg].ct = TCG_CT_ALIAS;
1214                 def->args_ct[oarg].alias_index = i;
1215                 def->args_ct[i].ct |= TCG_CT_IALIAS;
1216                 def->args_ct[i].alias_index = oarg;
1217             } else {
1218                 for(;;) {
1219                     if (*ct_str == '\0')
1220                         break;
1221                     switch(*ct_str) {
1222                     case 'i':
1223                         def->args_ct[i].ct |= TCG_CT_CONST;
1224                         ct_str++;
1225                         break;
1226                     default:
1227                         if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
1228                             fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
1229                                     ct_str, i, def->name);
1230                             exit(1);
1231                         }
1232                     }
1233                 }
1234             }
1235         }
1236
1237         /* TCGTargetOpDef entry with too much information? */
1238         assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
1239
1240         /* sort the constraints (XXX: this is just an heuristic) */
1241         sort_constraints(def, 0, def->nb_oargs);
1242         sort_constraints(def, def->nb_oargs, def->nb_iargs);
1243
1244 #if 0
1245         {
1246             int i;
1247
1248             printf("%s: sorted=", def->name);
1249             for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
1250                 printf(" %d", def->sorted_args[i]);
1251             printf("\n");
1252         }
1253 #endif
1254         tdefs++;
1255     }
1256
1257 #if defined(CONFIG_DEBUG_TCG)
1258     i = 0;
1259     for (op = 0; op < tcg_op_defs_max; op++) {
1260         const TCGOpDef *def = &tcg_op_defs[op];
1261         if (def->flags & TCG_OPF_NOT_PRESENT) {
1262             /* Wrong entry in op definitions? */
1263             if (def->used) {
1264                 fprintf(stderr, "Invalid op definition for %s\n", def->name);
1265                 i = 1;
1266             }
1267         } else {
1268             /* Missing entry in op definitions? */
1269             if (!def->used) {
1270                 fprintf(stderr, "Missing op definition for %s\n", def->name);
1271                 i = 1;
1272             }
1273         }
1274     }
1275     if (i == 1) {
1276         tcg_abort();
1277     }
1278 #endif
1279 }
1280
1281 void tcg_op_remove(TCGContext *s, TCGOp *op)
1282 {
1283     int next = op->next;
1284     int prev = op->prev;
1285
1286     if (next >= 0) {
1287         s->gen_op_buf[next].prev = prev;
1288     } else {
1289         s->gen_last_op_idx = prev;
1290     }
1291     if (prev >= 0) {
1292         s->gen_op_buf[prev].next = next;
1293     } else {
1294         s->gen_first_op_idx = next;
1295     }
1296
1297     memset(op, -1, sizeof(*op));
1298
1299 #ifdef CONFIG_PROFILER
1300     s->del_op_count++;
1301 #endif
1302 }
1303
1304 #ifdef USE_LIVENESS_ANALYSIS
1305 /* liveness analysis: end of function: all temps are dead, and globals
1306    should be in memory. */
1307 static inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps,
1308                                    uint8_t *mem_temps)
1309 {
1310     memset(dead_temps, 1, s->nb_temps);
1311     memset(mem_temps, 1, s->nb_globals);
1312     memset(mem_temps + s->nb_globals, 0, s->nb_temps - s->nb_globals);
1313 }
1314
1315 /* liveness analysis: end of basic block: all temps are dead, globals
1316    and local temps should be in memory. */
1317 static inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps,
1318                                  uint8_t *mem_temps)
1319 {
1320     int i;
1321
1322     memset(dead_temps, 1, s->nb_temps);
1323     memset(mem_temps, 1, s->nb_globals);
1324     for(i = s->nb_globals; i < s->nb_temps; i++) {
1325         mem_temps[i] = s->temps[i].temp_local;
1326     }
1327 }
1328
1329 /* Liveness analysis : update the opc_dead_args array to tell if a
1330    given input arguments is dead. Instructions updating dead
1331    temporaries are removed. */
1332 static void tcg_liveness_analysis(TCGContext *s)
1333 {
1334     uint8_t *dead_temps, *mem_temps;
1335     int oi, oi_prev, nb_ops;
1336
1337     nb_ops = s->gen_next_op_idx;
1338     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1339     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1340     
1341     dead_temps = tcg_malloc(s->nb_temps);
1342     mem_temps = tcg_malloc(s->nb_temps);
1343     tcg_la_func_end(s, dead_temps, mem_temps);
1344
1345     for (oi = s->gen_last_op_idx; oi >= 0; oi = oi_prev) {
1346         int i, nb_iargs, nb_oargs;
1347         TCGOpcode opc_new, opc_new2;
1348         bool have_opc_new2;
1349         uint16_t dead_args;
1350         uint8_t sync_args;
1351         TCGArg arg;
1352
1353         TCGOp * const op = &s->gen_op_buf[oi];
1354         TCGArg * const args = &s->gen_opparam_buf[op->args];
1355         TCGOpcode opc = op->opc;
1356         const TCGOpDef *def = &tcg_op_defs[opc];
1357
1358         oi_prev = op->prev;
1359
1360         switch (opc) {
1361         case INDEX_op_call:
1362             {
1363                 int call_flags;
1364
1365                 nb_oargs = op->callo;
1366                 nb_iargs = op->calli;
1367                 call_flags = args[nb_oargs + nb_iargs + 1];
1368
1369                 /* pure functions can be removed if their result is unused */
1370                 if (call_flags & TCG_CALL_NO_SIDE_EFFECTS) {
1371                     for (i = 0; i < nb_oargs; i++) {
1372                         arg = args[i];
1373                         if (!dead_temps[arg] || mem_temps[arg]) {
1374                             goto do_not_remove_call;
1375                         }
1376                     }
1377                     goto do_remove;
1378                 } else {
1379                 do_not_remove_call:
1380
1381                     /* output args are dead */
1382                     dead_args = 0;
1383                     sync_args = 0;
1384                     for (i = 0; i < nb_oargs; i++) {
1385                         arg = args[i];
1386                         if (dead_temps[arg]) {
1387                             dead_args |= (1 << i);
1388                         }
1389                         if (mem_temps[arg]) {
1390                             sync_args |= (1 << i);
1391                         }
1392                         dead_temps[arg] = 1;
1393                         mem_temps[arg] = 0;
1394                     }
1395
1396                     if (!(call_flags & TCG_CALL_NO_READ_GLOBALS)) {
1397                         /* globals should be synced to memory */
1398                         memset(mem_temps, 1, s->nb_globals);
1399                     }
1400                     if (!(call_flags & (TCG_CALL_NO_WRITE_GLOBALS |
1401                                         TCG_CALL_NO_READ_GLOBALS))) {
1402                         /* globals should go back to memory */
1403                         memset(dead_temps, 1, s->nb_globals);
1404                     }
1405
1406                     /* record arguments that die in this helper */
1407                     for (i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
1408                         arg = args[i];
1409                         if (arg != TCG_CALL_DUMMY_ARG) {
1410                             if (dead_temps[arg]) {
1411                                 dead_args |= (1 << i);
1412                             }
1413                         }
1414                     }
1415                     /* input arguments are live for preceding opcodes */
1416                     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1417                         arg = args[i];
1418                         dead_temps[arg] = 0;
1419                     }
1420                     s->op_dead_args[oi] = dead_args;
1421                     s->op_sync_args[oi] = sync_args;
1422                 }
1423             }
1424             break;
1425         case INDEX_op_insn_start:
1426             break;
1427         case INDEX_op_discard:
1428             /* mark the temporary as dead */
1429             dead_temps[args[0]] = 1;
1430             mem_temps[args[0]] = 0;
1431             break;
1432
1433         case INDEX_op_add2_i32:
1434             opc_new = INDEX_op_add_i32;
1435             goto do_addsub2;
1436         case INDEX_op_sub2_i32:
1437             opc_new = INDEX_op_sub_i32;
1438             goto do_addsub2;
1439         case INDEX_op_add2_i64:
1440             opc_new = INDEX_op_add_i64;
1441             goto do_addsub2;
1442         case INDEX_op_sub2_i64:
1443             opc_new = INDEX_op_sub_i64;
1444         do_addsub2:
1445             nb_iargs = 4;
1446             nb_oargs = 2;
1447             /* Test if the high part of the operation is dead, but not
1448                the low part.  The result can be optimized to a simple
1449                add or sub.  This happens often for x86_64 guest when the
1450                cpu mode is set to 32 bit.  */
1451             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
1452                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
1453                     goto do_remove;
1454                 }
1455                 /* Replace the opcode and adjust the args in place,
1456                    leaving 3 unused args at the end.  */
1457                 op->opc = opc = opc_new;
1458                 args[1] = args[2];
1459                 args[2] = args[4];
1460                 /* Fall through and mark the single-word operation live.  */
1461                 nb_iargs = 2;
1462                 nb_oargs = 1;
1463             }
1464             goto do_not_remove;
1465
1466         case INDEX_op_mulu2_i32:
1467             opc_new = INDEX_op_mul_i32;
1468             opc_new2 = INDEX_op_muluh_i32;
1469             have_opc_new2 = TCG_TARGET_HAS_muluh_i32;
1470             goto do_mul2;
1471         case INDEX_op_muls2_i32:
1472             opc_new = INDEX_op_mul_i32;
1473             opc_new2 = INDEX_op_mulsh_i32;
1474             have_opc_new2 = TCG_TARGET_HAS_mulsh_i32;
1475             goto do_mul2;
1476         case INDEX_op_mulu2_i64:
1477             opc_new = INDEX_op_mul_i64;
1478             opc_new2 = INDEX_op_muluh_i64;
1479             have_opc_new2 = TCG_TARGET_HAS_muluh_i64;
1480             goto do_mul2;
1481         case INDEX_op_muls2_i64:
1482             opc_new = INDEX_op_mul_i64;
1483             opc_new2 = INDEX_op_mulsh_i64;
1484             have_opc_new2 = TCG_TARGET_HAS_mulsh_i64;
1485             goto do_mul2;
1486         do_mul2:
1487             nb_iargs = 2;
1488             nb_oargs = 2;
1489             if (dead_temps[args[1]] && !mem_temps[args[1]]) {
1490                 if (dead_temps[args[0]] && !mem_temps[args[0]]) {
1491                     /* Both parts of the operation are dead.  */
1492                     goto do_remove;
1493                 }
1494                 /* The high part of the operation is dead; generate the low. */
1495                 op->opc = opc = opc_new;
1496                 args[1] = args[2];
1497                 args[2] = args[3];
1498             } else if (have_opc_new2 && dead_temps[args[0]]
1499                        && !mem_temps[args[0]]) {
1500                 /* The low part of the operation is dead; generate the high. */
1501                 op->opc = opc = opc_new2;
1502                 args[0] = args[1];
1503                 args[1] = args[2];
1504                 args[2] = args[3];
1505             } else {
1506                 goto do_not_remove;
1507             }
1508             /* Mark the single-word operation live.  */
1509             nb_oargs = 1;
1510             goto do_not_remove;
1511
1512         default:
1513             /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1514             nb_iargs = def->nb_iargs;
1515             nb_oargs = def->nb_oargs;
1516
1517             /* Test if the operation can be removed because all
1518                its outputs are dead. We assume that nb_oargs == 0
1519                implies side effects */
1520             if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
1521                 for (i = 0; i < nb_oargs; i++) {
1522                     arg = args[i];
1523                     if (!dead_temps[arg] || mem_temps[arg]) {
1524                         goto do_not_remove;
1525                     }
1526                 }
1527             do_remove:
1528                 tcg_op_remove(s, op);
1529             } else {
1530             do_not_remove:
1531                 /* output args are dead */
1532                 dead_args = 0;
1533                 sync_args = 0;
1534                 for (i = 0; i < nb_oargs; i++) {
1535                     arg = args[i];
1536                     if (dead_temps[arg]) {
1537                         dead_args |= (1 << i);
1538                     }
1539                     if (mem_temps[arg]) {
1540                         sync_args |= (1 << i);
1541                     }
1542                     dead_temps[arg] = 1;
1543                     mem_temps[arg] = 0;
1544                 }
1545
1546                 /* if end of basic block, update */
1547                 if (def->flags & TCG_OPF_BB_END) {
1548                     tcg_la_bb_end(s, dead_temps, mem_temps);
1549                 } else if (def->flags & TCG_OPF_SIDE_EFFECTS) {
1550                     /* globals should be synced to memory */
1551                     memset(mem_temps, 1, s->nb_globals);
1552                 }
1553
1554                 /* record arguments that die in this opcode */
1555                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1556                     arg = args[i];
1557                     if (dead_temps[arg]) {
1558                         dead_args |= (1 << i);
1559                     }
1560                 }
1561                 /* input arguments are live for preceding opcodes */
1562                 for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
1563                     arg = args[i];
1564                     dead_temps[arg] = 0;
1565                 }
1566                 s->op_dead_args[oi] = dead_args;
1567                 s->op_sync_args[oi] = sync_args;
1568             }
1569             break;
1570         }
1571     }
1572 }
1573 #else
1574 /* dummy liveness analysis */
1575 static void tcg_liveness_analysis(TCGContext *s)
1576 {
1577     int nb_ops;
1578     nb_ops = s->gen_opc_ptr - s->gen_opc_buf;
1579
1580     s->op_dead_args = tcg_malloc(nb_ops * sizeof(uint16_t));
1581     memset(s->op_dead_args, 0, nb_ops * sizeof(uint16_t));
1582     s->op_sync_args = tcg_malloc(nb_ops * sizeof(uint8_t));
1583     memset(s->op_sync_args, 0, nb_ops * sizeof(uint8_t));
1584 }
1585 #endif
1586
1587 #ifndef NDEBUG
1588 static void dump_regs(TCGContext *s)
1589 {
1590     TCGTemp *ts;
1591     int i;
1592     char buf[64];
1593
1594     for(i = 0; i < s->nb_temps; i++) {
1595         ts = &s->temps[i];
1596         printf("  %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
1597         switch(ts->val_type) {
1598         case TEMP_VAL_REG:
1599             printf("%s", tcg_target_reg_names[ts->reg]);
1600             break;
1601         case TEMP_VAL_MEM:
1602             printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
1603             break;
1604         case TEMP_VAL_CONST:
1605             printf("$0x%" TCG_PRIlx, ts->val);
1606             break;
1607         case TEMP_VAL_DEAD:
1608             printf("D");
1609             break;
1610         default:
1611             printf("???");
1612             break;
1613         }
1614         printf("\n");
1615     }
1616
1617     for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
1618         if (s->reg_to_temp[i] >= 0) {
1619             printf("%s: %s\n", 
1620                    tcg_target_reg_names[i], 
1621                    tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i]));
1622         }
1623     }
1624 }
1625
1626 static void check_regs(TCGContext *s)
1627 {
1628     int reg, k;
1629     TCGTemp *ts;
1630     char buf[64];
1631
1632     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
1633         k = s->reg_to_temp[reg];
1634         if (k >= 0) {
1635             ts = &s->temps[k];
1636             if (ts->val_type != TEMP_VAL_REG ||
1637                 ts->reg != reg) {
1638                 printf("Inconsistency for register %s:\n", 
1639                        tcg_target_reg_names[reg]);
1640                 goto fail;
1641             }
1642         }
1643     }
1644     for(k = 0; k < s->nb_temps; k++) {
1645         ts = &s->temps[k];
1646         if (ts->val_type == TEMP_VAL_REG &&
1647             !ts->fixed_reg &&
1648             s->reg_to_temp[ts->reg] != k) {
1649                 printf("Inconsistency for temp %s:\n", 
1650                        tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
1651         fail:
1652                 printf("reg state:\n");
1653                 dump_regs(s);
1654                 tcg_abort();
1655         }
1656     }
1657 }
1658 #endif
1659
1660 static void temp_allocate_frame(TCGContext *s, int temp)
1661 {
1662     TCGTemp *ts;
1663     ts = &s->temps[temp];
1664 #if !(defined(__sparc__) && TCG_TARGET_REG_BITS == 64)
1665     /* Sparc64 stack is accessed with offset of 2047 */
1666     s->current_frame_offset = (s->current_frame_offset +
1667                                (tcg_target_long)sizeof(tcg_target_long) - 1) &
1668         ~(sizeof(tcg_target_long) - 1);
1669 #endif
1670     if (s->current_frame_offset + (tcg_target_long)sizeof(tcg_target_long) >
1671         s->frame_end) {
1672         tcg_abort();
1673     }
1674     ts->mem_offset = s->current_frame_offset;
1675     ts->mem_reg = s->frame_reg;
1676     ts->mem_allocated = 1;
1677     s->current_frame_offset += sizeof(tcg_target_long);
1678 }
1679
1680 /* sync register 'reg' by saving it to the corresponding temporary */
1681 static inline void tcg_reg_sync(TCGContext *s, int reg)
1682 {
1683     TCGTemp *ts;
1684     int temp;
1685
1686     temp = s->reg_to_temp[reg];
1687     ts = &s->temps[temp];
1688     assert(ts->val_type == TEMP_VAL_REG);
1689     if (!ts->mem_coherent && !ts->fixed_reg) {
1690         if (!ts->mem_allocated) {
1691             temp_allocate_frame(s, temp);
1692         }
1693         tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
1694     }
1695     ts->mem_coherent = 1;
1696 }
1697
1698 /* free register 'reg' by spilling the corresponding temporary if necessary */
1699 static void tcg_reg_free(TCGContext *s, int reg)
1700 {
1701     int temp;
1702
1703     temp = s->reg_to_temp[reg];
1704     if (temp != -1) {
1705         tcg_reg_sync(s, reg);
1706         s->temps[temp].val_type = TEMP_VAL_MEM;
1707         s->reg_to_temp[reg] = -1;
1708     }
1709 }
1710
1711 /* Allocate a register belonging to reg1 & ~reg2 */
1712 static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
1713 {
1714     int i, reg;
1715     TCGRegSet reg_ct;
1716
1717     tcg_regset_andnot(reg_ct, reg1, reg2);
1718
1719     /* first try free registers */
1720     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1721         reg = tcg_target_reg_alloc_order[i];
1722         if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
1723             return reg;
1724     }
1725
1726     /* XXX: do better spill choice */
1727     for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
1728         reg = tcg_target_reg_alloc_order[i];
1729         if (tcg_regset_test_reg(reg_ct, reg)) {
1730             tcg_reg_free(s, reg);
1731             return reg;
1732         }
1733     }
1734
1735     tcg_abort();
1736 }
1737
1738 /* mark a temporary as dead. */
1739 static inline void temp_dead(TCGContext *s, int temp)
1740 {
1741     TCGTemp *ts;
1742
1743     ts = &s->temps[temp];
1744     if (!ts->fixed_reg) {
1745         if (ts->val_type == TEMP_VAL_REG) {
1746             s->reg_to_temp[ts->reg] = -1;
1747         }
1748         if (temp < s->nb_globals || ts->temp_local) {
1749             ts->val_type = TEMP_VAL_MEM;
1750         } else {
1751             ts->val_type = TEMP_VAL_DEAD;
1752         }
1753     }
1754 }
1755
1756 /* sync a temporary to memory. 'allocated_regs' is used in case a
1757    temporary registers needs to be allocated to store a constant. */
1758 static inline void temp_sync(TCGContext *s, int temp, TCGRegSet allocated_regs)
1759 {
1760     TCGTemp *ts;
1761
1762     ts = &s->temps[temp];
1763     if (!ts->fixed_reg) {
1764         switch(ts->val_type) {
1765         case TEMP_VAL_CONST:
1766             ts->reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
1767                                     allocated_regs);
1768             ts->val_type = TEMP_VAL_REG;
1769             s->reg_to_temp[ts->reg] = temp;
1770             ts->mem_coherent = 0;
1771             tcg_out_movi(s, ts->type, ts->reg, ts->val);
1772             /* fallthrough*/
1773         case TEMP_VAL_REG:
1774             tcg_reg_sync(s, ts->reg);
1775             break;
1776         case TEMP_VAL_DEAD:
1777         case TEMP_VAL_MEM:
1778             break;
1779         default:
1780             tcg_abort();
1781         }
1782     }
1783 }
1784
1785 /* save a temporary to memory. 'allocated_regs' is used in case a
1786    temporary registers needs to be allocated to store a constant. */
1787 static inline void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs)
1788 {
1789 #ifdef USE_LIVENESS_ANALYSIS
1790     /* The liveness analysis already ensures that globals are back
1791        in memory. Keep an assert for safety. */
1792     assert(s->temps[temp].val_type == TEMP_VAL_MEM || s->temps[temp].fixed_reg);
1793 #else
1794     temp_sync(s, temp, allocated_regs);
1795     temp_dead(s, temp);
1796 #endif
1797 }
1798
1799 /* save globals to their canonical location and assume they can be
1800    modified be the following code. 'allocated_regs' is used in case a
1801    temporary registers needs to be allocated to store a constant. */
1802 static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
1803 {
1804     int i;
1805
1806     for(i = 0; i < s->nb_globals; i++) {
1807         temp_save(s, i, allocated_regs);
1808     }
1809 }
1810
1811 /* sync globals to their canonical location and assume they can be
1812    read by the following code. 'allocated_regs' is used in case a
1813    temporary registers needs to be allocated to store a constant. */
1814 static void sync_globals(TCGContext *s, TCGRegSet allocated_regs)
1815 {
1816     int i;
1817
1818     for (i = 0; i < s->nb_globals; i++) {
1819 #ifdef USE_LIVENESS_ANALYSIS
1820         assert(s->temps[i].val_type != TEMP_VAL_REG || s->temps[i].fixed_reg ||
1821                s->temps[i].mem_coherent);
1822 #else
1823         temp_sync(s, i, allocated_regs);
1824 #endif
1825     }
1826 }
1827
1828 /* at the end of a basic block, we assume all temporaries are dead and
1829    all globals are stored at their canonical location. */
1830 static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
1831 {
1832     TCGTemp *ts;
1833     int i;
1834
1835     for(i = s->nb_globals; i < s->nb_temps; i++) {
1836         ts = &s->temps[i];
1837         if (ts->temp_local) {
1838             temp_save(s, i, allocated_regs);
1839         } else {
1840 #ifdef USE_LIVENESS_ANALYSIS
1841             /* The liveness analysis already ensures that temps are dead.
1842                Keep an assert for safety. */
1843             assert(ts->val_type == TEMP_VAL_DEAD);
1844 #else
1845             temp_dead(s, i);
1846 #endif
1847         }
1848     }
1849
1850     save_globals(s, allocated_regs);
1851 }
1852
1853 #define IS_DEAD_ARG(n) ((dead_args >> (n)) & 1)
1854 #define NEED_SYNC_ARG(n) ((sync_args >> (n)) & 1)
1855
1856 static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args,
1857                                uint16_t dead_args, uint8_t sync_args)
1858 {
1859     TCGTemp *ots;
1860     tcg_target_ulong val;
1861
1862     ots = &s->temps[args[0]];
1863     val = args[1];
1864
1865     if (ots->fixed_reg) {
1866         /* for fixed registers, we do not do any constant
1867            propagation */
1868         tcg_out_movi(s, ots->type, ots->reg, val);
1869     } else {
1870         /* The movi is not explicitly generated here */
1871         if (ots->val_type == TEMP_VAL_REG)
1872             s->reg_to_temp[ots->reg] = -1;
1873         ots->val_type = TEMP_VAL_CONST;
1874         ots->val = val;
1875     }
1876     if (NEED_SYNC_ARG(0)) {
1877         temp_sync(s, args[0], s->reserved_regs);
1878     }
1879     if (IS_DEAD_ARG(0)) {
1880         temp_dead(s, args[0]);
1881     }
1882 }
1883
1884 static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
1885                               const TCGArg *args, uint16_t dead_args,
1886                               uint8_t sync_args)
1887 {
1888     TCGRegSet allocated_regs;
1889     TCGTemp *ts, *ots;
1890     TCGType otype, itype;
1891
1892     tcg_regset_set(allocated_regs, s->reserved_regs);
1893     ots = &s->temps[args[0]];
1894     ts = &s->temps[args[1]];
1895
1896     /* Note that otype != itype for no-op truncation.  */
1897     otype = ots->type;
1898     itype = ts->type;
1899
1900     /* If the source value is not in a register, and we're going to be
1901        forced to have it in a register in order to perform the copy,
1902        then copy the SOURCE value into its own register first.  That way
1903        we don't have to reload SOURCE the next time it is used. */
1904     if (((NEED_SYNC_ARG(0) || ots->fixed_reg) && ts->val_type != TEMP_VAL_REG)
1905         || ts->val_type == TEMP_VAL_MEM) {
1906         ts->reg = tcg_reg_alloc(s, tcg_target_available_regs[itype],
1907                                 allocated_regs);
1908         if (ts->val_type == TEMP_VAL_MEM) {
1909             tcg_out_ld(s, itype, ts->reg, ts->mem_reg, ts->mem_offset);
1910             ts->mem_coherent = 1;
1911         } else if (ts->val_type == TEMP_VAL_CONST) {
1912             tcg_out_movi(s, itype, ts->reg, ts->val);
1913             ts->mem_coherent = 0;
1914         }
1915         s->reg_to_temp[ts->reg] = args[1];
1916         ts->val_type = TEMP_VAL_REG;
1917     }
1918
1919     if (IS_DEAD_ARG(0) && !ots->fixed_reg) {
1920         /* mov to a non-saved dead register makes no sense (even with
1921            liveness analysis disabled). */
1922         assert(NEED_SYNC_ARG(0));
1923         /* The code above should have moved the temp to a register. */
1924         assert(ts->val_type == TEMP_VAL_REG);
1925         if (!ots->mem_allocated) {
1926             temp_allocate_frame(s, args[0]);
1927         }
1928         tcg_out_st(s, otype, ts->reg, ots->mem_reg, ots->mem_offset);
1929         if (IS_DEAD_ARG(1)) {
1930             temp_dead(s, args[1]);
1931         }
1932         temp_dead(s, args[0]);
1933     } else if (ts->val_type == TEMP_VAL_CONST) {
1934         /* propagate constant */
1935         if (ots->val_type == TEMP_VAL_REG) {
1936             s->reg_to_temp[ots->reg] = -1;
1937         }
1938         ots->val_type = TEMP_VAL_CONST;
1939         ots->val = ts->val;
1940         if (IS_DEAD_ARG(1)) {
1941             temp_dead(s, args[1]);
1942         }
1943     } else {
1944         /* The code in the first if block should have moved the
1945            temp to a register. */
1946         assert(ts->val_type == TEMP_VAL_REG);
1947         if (IS_DEAD_ARG(1) && !ts->fixed_reg && !ots->fixed_reg) {
1948             /* the mov can be suppressed */
1949             if (ots->val_type == TEMP_VAL_REG) {
1950                 s->reg_to_temp[ots->reg] = -1;
1951             }
1952             ots->reg = ts->reg;
1953             temp_dead(s, args[1]);
1954         } else {
1955             if (ots->val_type != TEMP_VAL_REG) {
1956                 /* When allocating a new register, make sure to not spill the
1957                    input one. */
1958                 tcg_regset_set_reg(allocated_regs, ts->reg);
1959                 ots->reg = tcg_reg_alloc(s, tcg_target_available_regs[otype],
1960                                          allocated_regs);
1961             }
1962             tcg_out_mov(s, otype, ots->reg, ts->reg);
1963         }
1964         ots->val_type = TEMP_VAL_REG;
1965         ots->mem_coherent = 0;
1966         s->reg_to_temp[ots->reg] = args[0];
1967         if (NEED_SYNC_ARG(0)) {
1968             tcg_reg_sync(s, ots->reg);
1969         }
1970     }
1971 }
1972
1973 static void tcg_reg_alloc_op(TCGContext *s, 
1974                              const TCGOpDef *def, TCGOpcode opc,
1975                              const TCGArg *args, uint16_t dead_args,
1976                              uint8_t sync_args)
1977 {
1978     TCGRegSet allocated_regs;
1979     int i, k, nb_iargs, nb_oargs, reg;
1980     TCGArg arg;
1981     const TCGArgConstraint *arg_ct;
1982     TCGTemp *ts;
1983     TCGArg new_args[TCG_MAX_OP_ARGS];
1984     int const_args[TCG_MAX_OP_ARGS];
1985
1986     nb_oargs = def->nb_oargs;
1987     nb_iargs = def->nb_iargs;
1988
1989     /* copy constants */
1990     memcpy(new_args + nb_oargs + nb_iargs, 
1991            args + nb_oargs + nb_iargs, 
1992            sizeof(TCGArg) * def->nb_cargs);
1993
1994     /* satisfy input constraints */ 
1995     tcg_regset_set(allocated_regs, s->reserved_regs);
1996     for(k = 0; k < nb_iargs; k++) {
1997         i = def->sorted_args[nb_oargs + k];
1998         arg = args[i];
1999         arg_ct = &def->args_ct[i];
2000         ts = &s->temps[arg];
2001         if (ts->val_type == TEMP_VAL_MEM) {
2002             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2003             tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2004             ts->val_type = TEMP_VAL_REG;
2005             ts->reg = reg;
2006             ts->mem_coherent = 1;
2007             s->reg_to_temp[reg] = arg;
2008         } else if (ts->val_type == TEMP_VAL_CONST) {
2009             if (tcg_target_const_match(ts->val, ts->type, arg_ct)) {
2010                 /* constant is OK for instruction */
2011                 const_args[i] = 1;
2012                 new_args[i] = ts->val;
2013                 goto iarg_end;
2014             } else {
2015                 /* need to move to a register */
2016                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2017                 tcg_out_movi(s, ts->type, reg, ts->val);
2018                 ts->val_type = TEMP_VAL_REG;
2019                 ts->reg = reg;
2020                 ts->mem_coherent = 0;
2021                 s->reg_to_temp[reg] = arg;
2022             }
2023         }
2024         assert(ts->val_type == TEMP_VAL_REG);
2025         if (arg_ct->ct & TCG_CT_IALIAS) {
2026             if (ts->fixed_reg) {
2027                 /* if fixed register, we must allocate a new register
2028                    if the alias is not the same register */
2029                 if (arg != args[arg_ct->alias_index])
2030                     goto allocate_in_reg;
2031             } else {
2032                 /* if the input is aliased to an output and if it is
2033                    not dead after the instruction, we must allocate
2034                    a new register and move it */
2035                 if (!IS_DEAD_ARG(i)) {
2036                     goto allocate_in_reg;
2037                 }
2038                 /* check if the current register has already been allocated
2039                    for another input aliased to an output */
2040                 int k2, i2;
2041                 for (k2 = 0 ; k2 < k ; k2++) {
2042                     i2 = def->sorted_args[nb_oargs + k2];
2043                     if ((def->args_ct[i2].ct & TCG_CT_IALIAS) &&
2044                         (new_args[i2] == ts->reg)) {
2045                         goto allocate_in_reg;
2046                     }
2047                 }
2048             }
2049         }
2050         reg = ts->reg;
2051         if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
2052             /* nothing to do : the constraint is satisfied */
2053         } else {
2054         allocate_in_reg:
2055             /* allocate a new register matching the constraint 
2056                and move the temporary register into it */
2057             reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2058             tcg_out_mov(s, ts->type, reg, ts->reg);
2059         }
2060         new_args[i] = reg;
2061         const_args[i] = 0;
2062         tcg_regset_set_reg(allocated_regs, reg);
2063     iarg_end: ;
2064     }
2065     
2066     /* mark dead temporaries and free the associated registers */
2067     for (i = nb_oargs; i < nb_oargs + nb_iargs; i++) {
2068         if (IS_DEAD_ARG(i)) {
2069             temp_dead(s, args[i]);
2070         }
2071     }
2072
2073     if (def->flags & TCG_OPF_BB_END) {
2074         tcg_reg_alloc_bb_end(s, allocated_regs);
2075     } else {
2076         if (def->flags & TCG_OPF_CALL_CLOBBER) {
2077             /* XXX: permit generic clobber register list ? */ 
2078             for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2079                 if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
2080                     tcg_reg_free(s, reg);
2081                 }
2082             }
2083         }
2084         if (def->flags & TCG_OPF_SIDE_EFFECTS) {
2085             /* sync globals if the op has side effects and might trigger
2086                an exception. */
2087             sync_globals(s, allocated_regs);
2088         }
2089         
2090         /* satisfy the output constraints */
2091         tcg_regset_set(allocated_regs, s->reserved_regs);
2092         for(k = 0; k < nb_oargs; k++) {
2093             i = def->sorted_args[k];
2094             arg = args[i];
2095             arg_ct = &def->args_ct[i];
2096             ts = &s->temps[arg];
2097             if (arg_ct->ct & TCG_CT_ALIAS) {
2098                 reg = new_args[arg_ct->alias_index];
2099             } else {
2100                 /* if fixed register, we try to use it */
2101                 reg = ts->reg;
2102                 if (ts->fixed_reg &&
2103                     tcg_regset_test_reg(arg_ct->u.regs, reg)) {
2104                     goto oarg_end;
2105                 }
2106                 reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
2107             }
2108             tcg_regset_set_reg(allocated_regs, reg);
2109             /* if a fixed register is used, then a move will be done afterwards */
2110             if (!ts->fixed_reg) {
2111                 if (ts->val_type == TEMP_VAL_REG) {
2112                     s->reg_to_temp[ts->reg] = -1;
2113                 }
2114                 ts->val_type = TEMP_VAL_REG;
2115                 ts->reg = reg;
2116                 /* temp value is modified, so the value kept in memory is
2117                    potentially not the same */
2118                 ts->mem_coherent = 0;
2119                 s->reg_to_temp[reg] = arg;
2120             }
2121         oarg_end:
2122             new_args[i] = reg;
2123         }
2124     }
2125
2126     /* emit instruction */
2127     tcg_out_op(s, opc, new_args, const_args);
2128     
2129     /* move the outputs in the correct register if needed */
2130     for(i = 0; i < nb_oargs; i++) {
2131         ts = &s->temps[args[i]];
2132         reg = new_args[i];
2133         if (ts->fixed_reg && ts->reg != reg) {
2134             tcg_out_mov(s, ts->type, ts->reg, reg);
2135         }
2136         if (NEED_SYNC_ARG(i)) {
2137             tcg_reg_sync(s, reg);
2138         }
2139         if (IS_DEAD_ARG(i)) {
2140             temp_dead(s, args[i]);
2141         }
2142     }
2143 }
2144
2145 #ifdef TCG_TARGET_STACK_GROWSUP
2146 #define STACK_DIR(x) (-(x))
2147 #else
2148 #define STACK_DIR(x) (x)
2149 #endif
2150
2151 static void tcg_reg_alloc_call(TCGContext *s, int nb_oargs, int nb_iargs,
2152                                const TCGArg * const args, uint16_t dead_args,
2153                                uint8_t sync_args)
2154 {
2155     int flags, nb_regs, i, reg;
2156     TCGArg arg;
2157     TCGTemp *ts;
2158     intptr_t stack_offset;
2159     size_t call_stack_size;
2160     tcg_insn_unit *func_addr;
2161     int allocate_args;
2162     TCGRegSet allocated_regs;
2163
2164     func_addr = (tcg_insn_unit *)(intptr_t)args[nb_oargs + nb_iargs];
2165     flags = args[nb_oargs + nb_iargs + 1];
2166
2167     nb_regs = ARRAY_SIZE(tcg_target_call_iarg_regs);
2168     if (nb_regs > nb_iargs) {
2169         nb_regs = nb_iargs;
2170     }
2171
2172     /* assign stack slots first */
2173     call_stack_size = (nb_iargs - nb_regs) * sizeof(tcg_target_long);
2174     call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) & 
2175         ~(TCG_TARGET_STACK_ALIGN - 1);
2176     allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
2177     if (allocate_args) {
2178         /* XXX: if more than TCG_STATIC_CALL_ARGS_SIZE is needed,
2179            preallocate call stack */
2180         tcg_abort();
2181     }
2182
2183     stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
2184     for(i = nb_regs; i < nb_iargs; i++) {
2185         arg = args[nb_oargs + i];
2186 #ifdef TCG_TARGET_STACK_GROWSUP
2187         stack_offset -= sizeof(tcg_target_long);
2188 #endif
2189         if (arg != TCG_CALL_DUMMY_ARG) {
2190             ts = &s->temps[arg];
2191             if (ts->val_type == TEMP_VAL_REG) {
2192                 tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
2193             } else if (ts->val_type == TEMP_VAL_MEM) {
2194                 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 
2195                                     s->reserved_regs);
2196                 /* XXX: not correct if reading values from the stack */
2197                 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2198                 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
2199             } else if (ts->val_type == TEMP_VAL_CONST) {
2200                 reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type], 
2201                                     s->reserved_regs);
2202                 /* XXX: sign extend may be needed on some targets */
2203                 tcg_out_movi(s, ts->type, reg, ts->val);
2204                 tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
2205             } else {
2206                 tcg_abort();
2207             }
2208         }
2209 #ifndef TCG_TARGET_STACK_GROWSUP
2210         stack_offset += sizeof(tcg_target_long);
2211 #endif
2212     }
2213     
2214     /* assign input registers */
2215     tcg_regset_set(allocated_regs, s->reserved_regs);
2216     for(i = 0; i < nb_regs; i++) {
2217         arg = args[nb_oargs + i];
2218         if (arg != TCG_CALL_DUMMY_ARG) {
2219             ts = &s->temps[arg];
2220             reg = tcg_target_call_iarg_regs[i];
2221             tcg_reg_free(s, reg);
2222             if (ts->val_type == TEMP_VAL_REG) {
2223                 if (ts->reg != reg) {
2224                     tcg_out_mov(s, ts->type, reg, ts->reg);
2225                 }
2226             } else if (ts->val_type == TEMP_VAL_MEM) {
2227                 tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
2228             } else if (ts->val_type == TEMP_VAL_CONST) {
2229                 /* XXX: sign extend ? */
2230                 tcg_out_movi(s, ts->type, reg, ts->val);
2231             } else {
2232                 tcg_abort();
2233             }
2234             tcg_regset_set_reg(allocated_regs, reg);
2235         }
2236     }
2237     
2238     /* mark dead temporaries and free the associated registers */
2239     for(i = nb_oargs; i < nb_iargs + nb_oargs; i++) {
2240         if (IS_DEAD_ARG(i)) {
2241             temp_dead(s, args[i]);
2242         }
2243     }
2244     
2245     /* clobber call registers */
2246     for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
2247         if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
2248             tcg_reg_free(s, reg);
2249         }
2250     }
2251
2252     /* Save globals if they might be written by the helper, sync them if
2253        they might be read. */
2254     if (flags & TCG_CALL_NO_READ_GLOBALS) {
2255         /* Nothing to do */
2256     } else if (flags & TCG_CALL_NO_WRITE_GLOBALS) {
2257         sync_globals(s, allocated_regs);
2258     } else {
2259         save_globals(s, allocated_regs);
2260     }
2261
2262     tcg_out_call(s, func_addr);
2263
2264     /* assign output registers and emit moves if needed */
2265     for(i = 0; i < nb_oargs; i++) {
2266         arg = args[i];
2267         ts = &s->temps[arg];
2268         reg = tcg_target_call_oarg_regs[i];
2269         assert(s->reg_to_temp[reg] == -1);
2270
2271         if (ts->fixed_reg) {
2272             if (ts->reg != reg) {
2273                 tcg_out_mov(s, ts->type, ts->reg, reg);
2274             }
2275         } else {
2276             if (ts->val_type == TEMP_VAL_REG) {
2277                 s->reg_to_temp[ts->reg] = -1;
2278             }
2279             ts->val_type = TEMP_VAL_REG;
2280             ts->reg = reg;
2281             ts->mem_coherent = 0;
2282             s->reg_to_temp[reg] = arg;
2283             if (NEED_SYNC_ARG(i)) {
2284                 tcg_reg_sync(s, reg);
2285             }
2286             if (IS_DEAD_ARG(i)) {
2287                 temp_dead(s, args[i]);
2288             }
2289         }
2290     }
2291 }
2292
2293 #ifdef CONFIG_PROFILER
2294
2295 static int64_t tcg_table_op_count[NB_OPS];
2296
2297 void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
2298 {
2299     int i;
2300
2301     for (i = 0; i < NB_OPS; i++) {
2302         cpu_fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name,
2303                     tcg_table_op_count[i]);
2304     }
2305 }
2306 #else
2307 void tcg_dump_op_count(FILE *f, fprintf_function cpu_fprintf)
2308 {
2309     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2310 }
2311 #endif
2312
2313
2314 int tcg_gen_code(TCGContext *s, tcg_insn_unit *gen_code_buf)
2315 {
2316     int i, oi, oi_next, num_insns;
2317
2318 #ifdef CONFIG_PROFILER
2319     {
2320         int n;
2321
2322         n = s->gen_last_op_idx + 1;
2323         s->op_count += n;
2324         if (n > s->op_count_max) {
2325             s->op_count_max = n;
2326         }
2327
2328         n = s->nb_temps;
2329         s->temp_count += n;
2330         if (n > s->temp_count_max) {
2331             s->temp_count_max = n;
2332         }
2333     }
2334 #endif
2335
2336 #ifdef DEBUG_DISAS
2337     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
2338         qemu_log("OP:\n");
2339         tcg_dump_ops(s);
2340         qemu_log("\n");
2341     }
2342 #endif
2343
2344 #ifdef CONFIG_PROFILER
2345     s->opt_time -= profile_getclock();
2346 #endif
2347
2348 #ifdef USE_TCG_OPTIMIZATIONS
2349     tcg_optimize(s);
2350 #endif
2351
2352 #ifdef CONFIG_PROFILER
2353     s->opt_time += profile_getclock();
2354     s->la_time -= profile_getclock();
2355 #endif
2356
2357     tcg_liveness_analysis(s);
2358
2359 #ifdef CONFIG_PROFILER
2360     s->la_time += profile_getclock();
2361 #endif
2362
2363 #ifdef DEBUG_DISAS
2364     if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) {
2365         qemu_log("OP after optimization and liveness analysis:\n");
2366         tcg_dump_ops(s);
2367         qemu_log("\n");
2368     }
2369 #endif
2370
2371     tcg_reg_alloc_start(s);
2372
2373     s->code_buf = gen_code_buf;
2374     s->code_ptr = gen_code_buf;
2375
2376     tcg_out_tb_init(s);
2377
2378     num_insns = -1;
2379     for (oi = s->gen_first_op_idx; oi >= 0; oi = oi_next) {
2380         TCGOp * const op = &s->gen_op_buf[oi];
2381         TCGArg * const args = &s->gen_opparam_buf[op->args];
2382         TCGOpcode opc = op->opc;
2383         const TCGOpDef *def = &tcg_op_defs[opc];
2384         uint16_t dead_args = s->op_dead_args[oi];
2385         uint8_t sync_args = s->op_sync_args[oi];
2386
2387         oi_next = op->next;
2388 #ifdef CONFIG_PROFILER
2389         tcg_table_op_count[opc]++;
2390 #endif
2391
2392         switch (opc) {
2393         case INDEX_op_mov_i32:
2394         case INDEX_op_mov_i64:
2395             tcg_reg_alloc_mov(s, def, args, dead_args, sync_args);
2396             break;
2397         case INDEX_op_movi_i32:
2398         case INDEX_op_movi_i64:
2399             tcg_reg_alloc_movi(s, args, dead_args, sync_args);
2400             break;
2401         case INDEX_op_insn_start:
2402             if (num_insns >= 0) {
2403                 s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
2404             }
2405             num_insns++;
2406             for (i = 0; i < TARGET_INSN_START_WORDS; ++i) {
2407                 target_ulong a;
2408 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2409                 a = ((target_ulong)args[i * 2 + 1] << 32) | args[i * 2];
2410 #else
2411                 a = args[i];
2412 #endif
2413                 s->gen_insn_data[num_insns][i] = a;
2414             }
2415             break;
2416         case INDEX_op_discard:
2417             temp_dead(s, args[0]);
2418             break;
2419         case INDEX_op_set_label:
2420             tcg_reg_alloc_bb_end(s, s->reserved_regs);
2421             tcg_out_label(s, arg_label(args[0]), s->code_ptr);
2422             break;
2423         case INDEX_op_call:
2424             tcg_reg_alloc_call(s, op->callo, op->calli, args,
2425                                dead_args, sync_args);
2426             break;
2427         default:
2428             /* Sanity check that we've not introduced any unhandled opcodes. */
2429             if (def->flags & TCG_OPF_NOT_PRESENT) {
2430                 tcg_abort();
2431             }
2432             /* Note: in order to speed up the code, it would be much
2433                faster to have specialized register allocator functions for
2434                some common argument patterns */
2435             tcg_reg_alloc_op(s, def, opc, args, dead_args, sync_args);
2436             break;
2437         }
2438 #ifndef NDEBUG
2439         check_regs(s);
2440 #endif
2441     }
2442     tcg_debug_assert(num_insns >= 0);
2443     s->gen_insn_end_off[num_insns] = tcg_current_code_size(s);
2444
2445     /* Generate TB finalization at the end of block */
2446     tcg_out_tb_finalize(s);
2447
2448     /* flush instruction cache */
2449     flush_icache_range((uintptr_t)s->code_buf, (uintptr_t)s->code_ptr);
2450
2451     return tcg_current_code_size(s);
2452 }
2453
2454 #ifdef CONFIG_PROFILER
2455 void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2456 {
2457     TCGContext *s = &tcg_ctx;
2458     int64_t tb_count = s->tb_count;
2459     int64_t tb_div_count = tb_count ? tb_count : 1;
2460     int64_t tot = s->interm_time + s->code_time;
2461
2462     cpu_fprintf(f, "JIT cycles          %" PRId64 " (%0.3f s at 2.4 GHz)\n",
2463                 tot, tot / 2.4e9);
2464     cpu_fprintf(f, "translated TBs      %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n", 
2465                 tb_count, s->tb_count1 - tb_count,
2466                 (double)(s->tb_count1 - s->tb_count)
2467                 / (s->tb_count1 ? s->tb_count1 : 1) * 100.0);
2468     cpu_fprintf(f, "avg ops/TB          %0.1f max=%d\n", 
2469                 (double)s->op_count / tb_div_count, s->op_count_max);
2470     cpu_fprintf(f, "deleted ops/TB      %0.2f\n",
2471                 (double)s->del_op_count / tb_div_count);
2472     cpu_fprintf(f, "avg temps/TB        %0.2f max=%d\n",
2473                 (double)s->temp_count / tb_div_count, s->temp_count_max);
2474     cpu_fprintf(f, "avg host code/TB    %0.1f\n",
2475                 (double)s->code_out_len / tb_div_count);
2476     cpu_fprintf(f, "avg search data/TB  %0.1f\n",
2477                 (double)s->search_out_len / tb_div_count);
2478     
2479     cpu_fprintf(f, "cycles/op           %0.1f\n", 
2480                 s->op_count ? (double)tot / s->op_count : 0);
2481     cpu_fprintf(f, "cycles/in byte      %0.1f\n", 
2482                 s->code_in_len ? (double)tot / s->code_in_len : 0);
2483     cpu_fprintf(f, "cycles/out byte     %0.1f\n", 
2484                 s->code_out_len ? (double)tot / s->code_out_len : 0);
2485     cpu_fprintf(f, "cycles/search byte     %0.1f\n",
2486                 s->search_out_len ? (double)tot / s->search_out_len : 0);
2487     if (tot == 0) {
2488         tot = 1;
2489     }
2490     cpu_fprintf(f, "  gen_interm time   %0.1f%%\n", 
2491                 (double)s->interm_time / tot * 100.0);
2492     cpu_fprintf(f, "  gen_code time     %0.1f%%\n", 
2493                 (double)s->code_time / tot * 100.0);
2494     cpu_fprintf(f, "optim./code time    %0.1f%%\n",
2495                 (double)s->opt_time / (s->code_time ? s->code_time : 1)
2496                 * 100.0);
2497     cpu_fprintf(f, "liveness/code time  %0.1f%%\n", 
2498                 (double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
2499     cpu_fprintf(f, "cpu_restore count   %" PRId64 "\n",
2500                 s->restore_count);
2501     cpu_fprintf(f, "  avg cycles        %0.1f\n",
2502                 s->restore_count ? (double)s->restore_time / s->restore_count : 0);
2503 }
2504 #else
2505 void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf)
2506 {
2507     cpu_fprintf(f, "[TCG profiler not compiled]\n");
2508 }
2509 #endif
2510
2511 #ifdef ELF_HOST_MACHINE
2512 /* In order to use this feature, the backend needs to do three things:
2513
2514    (1) Define ELF_HOST_MACHINE to indicate both what value to
2515        put into the ELF image and to indicate support for the feature.
2516
2517    (2) Define tcg_register_jit.  This should create a buffer containing
2518        the contents of a .debug_frame section that describes the post-
2519        prologue unwind info for the tcg machine.
2520
2521    (3) Call tcg_register_jit_int, with the constructed .debug_frame.
2522 */
2523
2524 /* Begin GDB interface.  THE FOLLOWING MUST MATCH GDB DOCS.  */
2525 typedef enum {
2526     JIT_NOACTION = 0,
2527     JIT_REGISTER_FN,
2528     JIT_UNREGISTER_FN
2529 } jit_actions_t;
2530
2531 struct jit_code_entry {
2532     struct jit_code_entry *next_entry;
2533     struct jit_code_entry *prev_entry;
2534     const void *symfile_addr;
2535     uint64_t symfile_size;
2536 };
2537
2538 struct jit_descriptor {
2539     uint32_t version;
2540     uint32_t action_flag;
2541     struct jit_code_entry *relevant_entry;
2542     struct jit_code_entry *first_entry;
2543 };
2544
2545 void __jit_debug_register_code(void) __attribute__((noinline));
2546 void __jit_debug_register_code(void)
2547 {
2548     asm("");
2549 }
2550
2551 /* Must statically initialize the version, because GDB may check
2552    the version before we can set it.  */
2553 struct jit_descriptor __jit_debug_descriptor = { 1, 0, 0, 0 };
2554
2555 /* End GDB interface.  */
2556
2557 static int find_string(const char *strtab, const char *str)
2558 {
2559     const char *p = strtab + 1;
2560
2561     while (1) {
2562         if (strcmp(p, str) == 0) {
2563             return p - strtab;
2564         }
2565         p += strlen(p) + 1;
2566     }
2567 }
2568
2569 static void tcg_register_jit_int(void *buf_ptr, size_t buf_size,
2570                                  const void *debug_frame,
2571                                  size_t debug_frame_size)
2572 {
2573     struct __attribute__((packed)) DebugInfo {
2574         uint32_t  len;
2575         uint16_t  version;
2576         uint32_t  abbrev;
2577         uint8_t   ptr_size;
2578         uint8_t   cu_die;
2579         uint16_t  cu_lang;
2580         uintptr_t cu_low_pc;
2581         uintptr_t cu_high_pc;
2582         uint8_t   fn_die;
2583         char      fn_name[16];
2584         uintptr_t fn_low_pc;
2585         uintptr_t fn_high_pc;
2586         uint8_t   cu_eoc;
2587     };
2588
2589     struct ElfImage {
2590         ElfW(Ehdr) ehdr;
2591         ElfW(Phdr) phdr;
2592         ElfW(Shdr) shdr[7];
2593         ElfW(Sym)  sym[2];
2594         struct DebugInfo di;
2595         uint8_t    da[24];
2596         char       str[80];
2597     };
2598
2599     struct ElfImage *img;
2600
2601     static const struct ElfImage img_template = {
2602         .ehdr = {
2603             .e_ident[EI_MAG0] = ELFMAG0,
2604             .e_ident[EI_MAG1] = ELFMAG1,
2605             .e_ident[EI_MAG2] = ELFMAG2,
2606             .e_ident[EI_MAG3] = ELFMAG3,
2607             .e_ident[EI_CLASS] = ELF_CLASS,
2608             .e_ident[EI_DATA] = ELF_DATA,
2609             .e_ident[EI_VERSION] = EV_CURRENT,
2610             .e_type = ET_EXEC,
2611             .e_machine = ELF_HOST_MACHINE,
2612             .e_version = EV_CURRENT,
2613             .e_phoff = offsetof(struct ElfImage, phdr),
2614             .e_shoff = offsetof(struct ElfImage, shdr),
2615             .e_ehsize = sizeof(ElfW(Shdr)),
2616             .e_phentsize = sizeof(ElfW(Phdr)),
2617             .e_phnum = 1,
2618             .e_shentsize = sizeof(ElfW(Shdr)),
2619             .e_shnum = ARRAY_SIZE(img->shdr),
2620             .e_shstrndx = ARRAY_SIZE(img->shdr) - 1,
2621 #ifdef ELF_HOST_FLAGS
2622             .e_flags = ELF_HOST_FLAGS,
2623 #endif
2624 #ifdef ELF_OSABI
2625             .e_ident[EI_OSABI] = ELF_OSABI,
2626 #endif
2627         },
2628         .phdr = {
2629             .p_type = PT_LOAD,
2630             .p_flags = PF_X,
2631         },
2632         .shdr = {
2633             [0] = { .sh_type = SHT_NULL },
2634             /* Trick: The contents of code_gen_buffer are not present in
2635                this fake ELF file; that got allocated elsewhere.  Therefore
2636                we mark .text as SHT_NOBITS (similar to .bss) so that readers
2637                will not look for contents.  We can record any address.  */
2638             [1] = { /* .text */
2639                 .sh_type = SHT_NOBITS,
2640                 .sh_flags = SHF_EXECINSTR | SHF_ALLOC,
2641             },
2642             [2] = { /* .debug_info */
2643                 .sh_type = SHT_PROGBITS,
2644                 .sh_offset = offsetof(struct ElfImage, di),
2645                 .sh_size = sizeof(struct DebugInfo),
2646             },
2647             [3] = { /* .debug_abbrev */
2648                 .sh_type = SHT_PROGBITS,
2649                 .sh_offset = offsetof(struct ElfImage, da),
2650                 .sh_size = sizeof(img->da),
2651             },
2652             [4] = { /* .debug_frame */
2653                 .sh_type = SHT_PROGBITS,
2654                 .sh_offset = sizeof(struct ElfImage),
2655             },
2656             [5] = { /* .symtab */
2657                 .sh_type = SHT_SYMTAB,
2658                 .sh_offset = offsetof(struct ElfImage, sym),
2659                 .sh_size = sizeof(img->sym),
2660                 .sh_info = 1,
2661                 .sh_link = ARRAY_SIZE(img->shdr) - 1,
2662                 .sh_entsize = sizeof(ElfW(Sym)),
2663             },
2664             [6] = { /* .strtab */
2665                 .sh_type = SHT_STRTAB,
2666                 .sh_offset = offsetof(struct ElfImage, str),
2667                 .sh_size = sizeof(img->str),
2668             }
2669         },
2670         .sym = {
2671             [1] = { /* code_gen_buffer */
2672                 .st_info = ELF_ST_INFO(STB_GLOBAL, STT_FUNC),
2673                 .st_shndx = 1,
2674             }
2675         },
2676         .di = {
2677             .len = sizeof(struct DebugInfo) - 4,
2678             .version = 2,
2679             .ptr_size = sizeof(void *),
2680             .cu_die = 1,
2681             .cu_lang = 0x8001,  /* DW_LANG_Mips_Assembler */
2682             .fn_die = 2,
2683             .fn_name = "code_gen_buffer"
2684         },
2685         .da = {
2686             1,          /* abbrev number (the cu) */
2687             0x11, 1,    /* DW_TAG_compile_unit, has children */
2688             0x13, 0x5,  /* DW_AT_language, DW_FORM_data2 */
2689             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
2690             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
2691             0, 0,       /* end of abbrev */
2692             2,          /* abbrev number (the fn) */
2693             0x2e, 0,    /* DW_TAG_subprogram, no children */
2694             0x3, 0x8,   /* DW_AT_name, DW_FORM_string */
2695             0x11, 0x1,  /* DW_AT_low_pc, DW_FORM_addr */
2696             0x12, 0x1,  /* DW_AT_high_pc, DW_FORM_addr */
2697             0, 0,       /* end of abbrev */
2698             0           /* no more abbrev */
2699         },
2700         .str = "\0" ".text\0" ".debug_info\0" ".debug_abbrev\0"
2701                ".debug_frame\0" ".symtab\0" ".strtab\0" "code_gen_buffer",
2702     };
2703
2704     /* We only need a single jit entry; statically allocate it.  */
2705     static struct jit_code_entry one_entry;
2706
2707     uintptr_t buf = (uintptr_t)buf_ptr;
2708     size_t img_size = sizeof(struct ElfImage) + debug_frame_size;
2709     DebugFrameHeader *dfh;
2710
2711     img = g_malloc(img_size);
2712     *img = img_template;
2713
2714     img->phdr.p_vaddr = buf;
2715     img->phdr.p_paddr = buf;
2716     img->phdr.p_memsz = buf_size;
2717
2718     img->shdr[1].sh_name = find_string(img->str, ".text");
2719     img->shdr[1].sh_addr = buf;
2720     img->shdr[1].sh_size = buf_size;
2721
2722     img->shdr[2].sh_name = find_string(img->str, ".debug_info");
2723     img->shdr[3].sh_name = find_string(img->str, ".debug_abbrev");
2724
2725     img->shdr[4].sh_name = find_string(img->str, ".debug_frame");
2726     img->shdr[4].sh_size = debug_frame_size;
2727
2728     img->shdr[5].sh_name = find_string(img->str, ".symtab");
2729     img->shdr[6].sh_name = find_string(img->str, ".strtab");
2730
2731     img->sym[1].st_name = find_string(img->str, "code_gen_buffer");
2732     img->sym[1].st_value = buf;
2733     img->sym[1].st_size = buf_size;
2734
2735     img->di.cu_low_pc = buf;
2736     img->di.cu_high_pc = buf + buf_size;
2737     img->di.fn_low_pc = buf;
2738     img->di.fn_high_pc = buf + buf_size;
2739
2740     dfh = (DebugFrameHeader *)(img + 1);
2741     memcpy(dfh, debug_frame, debug_frame_size);
2742     dfh->fde.func_start = buf;
2743     dfh->fde.func_len = buf_size;
2744
2745 #ifdef DEBUG_JIT
2746     /* Enable this block to be able to debug the ELF image file creation.
2747        One can use readelf, objdump, or other inspection utilities.  */
2748     {
2749         FILE *f = fopen("/tmp/qemu.jit", "w+b");
2750         if (f) {
2751             if (fwrite(img, img_size, 1, f) != img_size) {
2752                 /* Avoid stupid unused return value warning for fwrite.  */
2753             }
2754             fclose(f);
2755         }
2756     }
2757 #endif
2758
2759     one_entry.symfile_addr = img;
2760     one_entry.symfile_size = img_size;
2761
2762     __jit_debug_descriptor.action_flag = JIT_REGISTER_FN;
2763     __jit_debug_descriptor.relevant_entry = &one_entry;
2764     __jit_debug_descriptor.first_entry = &one_entry;
2765     __jit_debug_register_code();
2766 }
2767 #else
2768 /* No support for the feature.  Provide the entry point expected by exec.c,
2769    and implement the internal function we declared earlier.  */
2770
2771 static void tcg_register_jit_int(void *buf, size_t size,
2772                                  const void *debug_frame,
2773                                  size_t debug_frame_size)
2774 {
2775 }
2776
2777 void tcg_register_jit(void *buf, size_t buf_size)
2778 {
2779 }
2780 #endif /* ELF_HOST_MACHINE */
This page took 0.181038 seconds and 4 git commands to generate.