2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 /* define it to suppress various consistency checks (faster) */
28 /* define it to use liveness analysis (better code) */
29 #define USE_LIVENESS_ANALYSIS
42 #include "qemu-common.h"
44 /* Note: the long term plan is to reduce the dependancies on the QEMU
45 CPU definitions. Currently they are used for qemu_ld/st
47 #define NO_CPU_IO_DEFS
55 static void patch_reloc(uint8_t *code_ptr
, int type
,
56 tcg_target_long value
, tcg_target_long addend
);
58 TCGOpDef tcg_op_defs
[] = {
59 #define DEF(s, n, copy_size) { #s, 0, 0, n, n, 0, copy_size },
60 #define DEF2(s, iargs, oargs, cargs, flags) { #s, iargs, oargs, cargs, iargs + oargs + cargs, flags, 0 },
66 TCGRegSet tcg_target_available_regs
[2];
67 TCGRegSet tcg_target_call_clobber_regs
;
69 /* XXX: move that inside the context */
70 uint16_t *gen_opc_ptr
;
71 TCGArg
*gen_opparam_ptr
;
73 static inline void tcg_out8(TCGContext
*s
, uint8_t v
)
78 static inline void tcg_out16(TCGContext
*s
, uint16_t v
)
80 *(uint16_t *)s
->code_ptr
= v
;
84 static inline void tcg_out32(TCGContext
*s
, uint32_t v
)
86 *(uint32_t *)s
->code_ptr
= v
;
90 /* label relocation processing */
92 void tcg_out_reloc(TCGContext
*s
, uint8_t *code_ptr
, int type
,
93 int label_index
, long addend
)
98 l
= &s
->labels
[label_index
];
100 /* FIXME: This may break relocations on RISC targets that
101 modify instruction fields in place. The caller may not have
102 written the initial value. */
103 patch_reloc(code_ptr
, type
, l
->u
.value
, addend
);
105 /* add a new relocation entry */
106 r
= tcg_malloc(sizeof(TCGRelocation
));
110 r
->next
= l
->u
.first_reloc
;
111 l
->u
.first_reloc
= r
;
115 static void tcg_out_label(TCGContext
*s
, int label_index
,
116 tcg_target_long value
)
121 l
= &s
->labels
[label_index
];
124 r
= l
->u
.first_reloc
;
126 patch_reloc(r
->ptr
, r
->type
, value
, r
->addend
);
133 int gen_new_label(void)
135 TCGContext
*s
= &tcg_ctx
;
139 if (s
->nb_labels
>= TCG_MAX_LABELS
)
141 idx
= s
->nb_labels
++;
144 l
->u
.first_reloc
= NULL
;
148 #include "tcg-target.c"
150 /* pool based memory allocation */
151 void *tcg_malloc_internal(TCGContext
*s
, int size
)
156 if (size
> TCG_POOL_CHUNK_SIZE
) {
157 /* big malloc: insert a new pool (XXX: could optimize) */
158 p
= qemu_malloc(sizeof(TCGPool
) + size
);
161 s
->pool_current
->next
= p
;
164 p
->next
= s
->pool_current
;
174 pool_size
= TCG_POOL_CHUNK_SIZE
;
175 p
= qemu_malloc(sizeof(TCGPool
) + pool_size
);
179 s
->pool_current
->next
= p
;
188 s
->pool_cur
= p
->data
+ size
;
189 s
->pool_end
= p
->data
+ p
->size
;
193 void tcg_pool_reset(TCGContext
*s
)
195 s
->pool_cur
= s
->pool_end
= NULL
;
196 s
->pool_current
= NULL
;
199 void tcg_context_init(TCGContext
*s
)
201 int op
, total_args
, n
;
203 TCGArgConstraint
*args_ct
;
206 memset(s
, 0, sizeof(*s
));
207 s
->temps
= s
->static_temps
;
210 /* Count total number of arguments and allocate the corresponding
213 for(op
= 0; op
< NB_OPS
; op
++) {
214 def
= &tcg_op_defs
[op
];
215 n
= def
->nb_iargs
+ def
->nb_oargs
;
219 args_ct
= qemu_malloc(sizeof(TCGArgConstraint
) * total_args
);
220 sorted_args
= qemu_malloc(sizeof(int) * total_args
);
222 for(op
= 0; op
< NB_OPS
; op
++) {
223 def
= &tcg_op_defs
[op
];
224 def
->args_ct
= args_ct
;
225 def
->sorted_args
= sorted_args
;
226 n
= def
->nb_iargs
+ def
->nb_oargs
;
233 /* init global prologue and epilogue */
234 s
->code_buf
= code_gen_prologue
;
235 s
->code_ptr
= s
->code_buf
;
236 tcg_target_qemu_prologue(s
);
237 flush_icache_range((unsigned long)s
->code_buf
,
238 (unsigned long)s
->code_ptr
);
241 void tcg_set_frame(TCGContext
*s
, int reg
,
242 tcg_target_long start
, tcg_target_long size
)
244 s
->frame_start
= start
;
245 s
->frame_end
= start
+ size
;
249 void tcg_func_start(TCGContext
*s
)
253 s
->nb_temps
= s
->nb_globals
;
254 for(i
= 0; i
< (TCG_TYPE_COUNT
* 2); i
++)
255 s
->first_free_temp
[i
] = -1;
256 s
->labels
= tcg_malloc(sizeof(TCGLabel
) * TCG_MAX_LABELS
);
258 s
->current_frame_offset
= s
->frame_start
;
260 gen_opc_ptr
= gen_opc_buf
;
261 gen_opparam_ptr
= gen_opparam_buf
;
264 static inline void tcg_temp_alloc(TCGContext
*s
, int n
)
266 if (n
> TCG_MAX_TEMPS
)
270 TCGv
tcg_global_reg_new(TCGType type
, int reg
, const char *name
)
272 TCGContext
*s
= &tcg_ctx
;
276 #if TCG_TARGET_REG_BITS == 32
277 if (type
!= TCG_TYPE_I32
)
280 if (tcg_regset_test_reg(s
->reserved_regs
, reg
))
283 tcg_temp_alloc(s
, s
->nb_globals
+ 1);
284 ts
= &s
->temps
[s
->nb_globals
];
285 ts
->base_type
= type
;
291 tcg_regset_set_reg(s
->reserved_regs
, reg
);
292 return MAKE_TCGV(idx
);
295 #if TCG_TARGET_REG_BITS == 32
296 /* temporary hack to avoid register shortage for tcg_qemu_st64() */
297 TCGv
tcg_global_reg2_new_hack(TCGType type
, int reg1
, int reg2
,
300 TCGContext
*s
= &tcg_ctx
;
305 if (type
!= TCG_TYPE_I64
)
308 tcg_temp_alloc(s
, s
->nb_globals
+ 2);
309 ts
= &s
->temps
[s
->nb_globals
];
310 ts
->base_type
= type
;
311 ts
->type
= TCG_TYPE_I32
;
314 pstrcpy(buf
, sizeof(buf
), name
);
315 pstrcat(buf
, sizeof(buf
), "_0");
316 ts
->name
= strdup(buf
);
319 ts
->base_type
= type
;
320 ts
->type
= TCG_TYPE_I32
;
323 pstrcpy(buf
, sizeof(buf
), name
);
324 pstrcat(buf
, sizeof(buf
), "_1");
325 ts
->name
= strdup(buf
);
328 return MAKE_TCGV(idx
);
332 TCGv
tcg_global_mem_new(TCGType type
, int reg
, tcg_target_long offset
,
335 TCGContext
*s
= &tcg_ctx
;
340 #if TCG_TARGET_REG_BITS == 32
341 if (type
== TCG_TYPE_I64
) {
343 tcg_temp_alloc(s
, s
->nb_globals
+ 2);
344 ts
= &s
->temps
[s
->nb_globals
];
345 ts
->base_type
= type
;
346 ts
->type
= TCG_TYPE_I32
;
348 ts
->mem_allocated
= 1;
350 #ifdef TCG_TARGET_WORDS_BIGENDIAN
351 ts
->mem_offset
= offset
+ 4;
353 ts
->mem_offset
= offset
;
355 pstrcpy(buf
, sizeof(buf
), name
);
356 pstrcat(buf
, sizeof(buf
), "_0");
357 ts
->name
= strdup(buf
);
360 ts
->base_type
= type
;
361 ts
->type
= TCG_TYPE_I32
;
363 ts
->mem_allocated
= 1;
365 #ifdef TCG_TARGET_WORDS_BIGENDIAN
366 ts
->mem_offset
= offset
;
368 ts
->mem_offset
= offset
+ 4;
370 pstrcpy(buf
, sizeof(buf
), name
);
371 pstrcat(buf
, sizeof(buf
), "_1");
372 ts
->name
= strdup(buf
);
378 tcg_temp_alloc(s
, s
->nb_globals
+ 1);
379 ts
= &s
->temps
[s
->nb_globals
];
380 ts
->base_type
= type
;
383 ts
->mem_allocated
= 1;
385 ts
->mem_offset
= offset
;
389 return MAKE_TCGV(idx
);
392 TCGv
tcg_temp_new_internal(TCGType type
, int temp_local
)
394 TCGContext
*s
= &tcg_ctx
;
401 idx
= s
->first_free_temp
[k
];
403 /* There is already an available temp with the
406 s
->first_free_temp
[k
] = ts
->next_free_temp
;
407 ts
->temp_allocated
= 1;
408 assert(ts
->temp_local
== temp_local
);
411 #if TCG_TARGET_REG_BITS == 32
412 if (type
== TCG_TYPE_I64
) {
413 tcg_temp_alloc(s
, s
->nb_temps
+ 2);
414 ts
= &s
->temps
[s
->nb_temps
];
415 ts
->base_type
= type
;
416 ts
->type
= TCG_TYPE_I32
;
417 ts
->temp_allocated
= 1;
418 ts
->temp_local
= temp_local
;
421 ts
->base_type
= TCG_TYPE_I32
;
422 ts
->type
= TCG_TYPE_I32
;
423 ts
->temp_allocated
= 1;
424 ts
->temp_local
= temp_local
;
430 tcg_temp_alloc(s
, s
->nb_temps
+ 1);
431 ts
= &s
->temps
[s
->nb_temps
];
432 ts
->base_type
= type
;
434 ts
->temp_allocated
= 1;
435 ts
->temp_local
= temp_local
;
440 return MAKE_TCGV(idx
);
443 void tcg_temp_free(TCGv arg
)
445 TCGContext
*s
= &tcg_ctx
;
447 int idx
= GET_TCGV(arg
);
450 assert(idx
>= s
->nb_globals
&& idx
< s
->nb_temps
);
452 assert(ts
->temp_allocated
!= 0);
453 ts
->temp_allocated
= 0;
457 ts
->next_free_temp
= s
->first_free_temp
[k
];
458 s
->first_free_temp
[k
] = idx
;
462 TCGv
tcg_const_i32(int32_t val
)
465 t0
= tcg_temp_new(TCG_TYPE_I32
);
466 tcg_gen_movi_i32(t0
, val
);
470 TCGv
tcg_const_i64(int64_t val
)
473 t0
= tcg_temp_new(TCG_TYPE_I64
);
474 tcg_gen_movi_i64(t0
, val
);
478 void tcg_register_helper(void *func
, const char *name
)
480 TCGContext
*s
= &tcg_ctx
;
482 if ((s
->nb_helpers
+ 1) > s
->allocated_helpers
) {
483 n
= s
->allocated_helpers
;
489 s
->helpers
= realloc(s
->helpers
, n
* sizeof(TCGHelperInfo
));
490 s
->allocated_helpers
= n
;
492 s
->helpers
[s
->nb_helpers
].func
= (tcg_target_ulong
)func
;
493 s
->helpers
[s
->nb_helpers
].name
= name
;
497 static inline TCGType
tcg_get_base_type(TCGContext
*s
, TCGv arg
)
499 return s
->temps
[GET_TCGV(arg
)].base_type
;
502 static void tcg_gen_call_internal(TCGContext
*s
, TCGv func
,
504 unsigned int nb_rets
, const TCGv
*rets
,
505 unsigned int nb_params
, const TCGv
*params
)
508 *gen_opc_ptr
++ = INDEX_op_call
;
509 *gen_opparam_ptr
++ = (nb_rets
<< 16) | (nb_params
+ 1);
510 for(i
= 0; i
< nb_rets
; i
++) {
511 *gen_opparam_ptr
++ = GET_TCGV(rets
[i
]);
513 for(i
= 0; i
< nb_params
; i
++) {
514 *gen_opparam_ptr
++ = GET_TCGV(params
[i
]);
516 *gen_opparam_ptr
++ = GET_TCGV(func
);
518 *gen_opparam_ptr
++ = flags
;
519 /* total parameters, needed to go backward in the instruction stream */
520 *gen_opparam_ptr
++ = 1 + nb_rets
+ nb_params
+ 3;
524 #if TCG_TARGET_REG_BITS < 64
525 /* Note: we convert the 64 bit args to 32 bit and do some alignment
526 and endian swap. Maybe it would be better to do the alignment
527 and endian swap in tcg_reg_alloc_call(). */
528 void tcg_gen_call(TCGContext
*s
, TCGv func
, unsigned int flags
,
529 unsigned int nb_rets
, const TCGv
*rets
,
530 unsigned int nb_params
, const TCGv
*args1
)
532 TCGv ret
, *args2
, rets_2
[2], arg
;
537 if (tcg_get_base_type(s
, ret
) == TCG_TYPE_I64
) {
539 #ifdef TCG_TARGET_WORDS_BIGENDIAN
540 rets_2
[0] = TCGV_HIGH(ret
);
544 rets_2
[1] = TCGV_HIGH(ret
);
549 args2
= alloca((nb_params
* 3) * sizeof(TCGv
));
551 call_type
= (flags
& TCG_CALL_TYPE_MASK
);
552 for(i
= 0; i
< nb_params
; i
++) {
554 if (tcg_get_base_type(s
, arg
) == TCG_TYPE_I64
) {
555 #ifdef TCG_TARGET_I386
556 /* REGPARM case: if the third parameter is 64 bit, it is
557 allocated on the stack */
558 if (j
== 2 && call_type
== TCG_CALL_TYPE_REGPARM
) {
559 call_type
= TCG_CALL_TYPE_REGPARM_2
;
560 flags
= (flags
& ~TCG_CALL_TYPE_MASK
) | call_type
;
563 args2
[j
++] = TCGV_HIGH(arg
);
565 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
566 /* some targets want aligned 64 bit args */
568 args2
[j
++] = TCG_CALL_DUMMY_ARG
;
571 #ifdef TCG_TARGET_WORDS_BIGENDIAN
572 args2
[j
++] = TCGV_HIGH(arg
);
576 args2
[j
++] = TCGV_HIGH(arg
);
583 tcg_gen_call_internal(s
, func
, flags
,
584 nb_rets
, rets
, j
, args2
);
587 void tcg_gen_call(TCGContext
*s
, TCGv func
, unsigned int flags
,
588 unsigned int nb_rets
, const TCGv
*rets
,
589 unsigned int nb_params
, const TCGv
*args1
)
591 tcg_gen_call_internal(s
, func
, flags
,
592 nb_rets
, rets
, nb_params
, args1
);
596 #if TCG_TARGET_REG_BITS == 32
597 void tcg_gen_shifti_i64(TCGv ret
, TCGv arg1
,
598 int c
, int right
, int arith
)
601 tcg_gen_mov_i32(ret
, arg1
);
602 tcg_gen_mov_i32(TCGV_HIGH(ret
), TCGV_HIGH(arg1
));
603 } else if (c
>= 32) {
607 tcg_gen_sari_i32(ret
, TCGV_HIGH(arg1
), c
);
608 tcg_gen_sari_i32(TCGV_HIGH(ret
), TCGV_HIGH(arg1
), 31);
610 tcg_gen_shri_i32(ret
, TCGV_HIGH(arg1
), c
);
611 tcg_gen_movi_i32(TCGV_HIGH(ret
), 0);
614 tcg_gen_shli_i32(TCGV_HIGH(ret
), arg1
, c
);
615 tcg_gen_movi_i32(ret
, 0);
620 t0
= tcg_temp_new(TCG_TYPE_I32
);
621 t1
= tcg_temp_new(TCG_TYPE_I32
);
623 tcg_gen_shli_i32(t0
, TCGV_HIGH(arg1
), 32 - c
);
625 tcg_gen_sari_i32(t1
, TCGV_HIGH(arg1
), c
);
627 tcg_gen_shri_i32(t1
, TCGV_HIGH(arg1
), c
);
628 tcg_gen_shri_i32(ret
, arg1
, c
);
629 tcg_gen_or_i32(ret
, ret
, t0
);
630 tcg_gen_mov_i32(TCGV_HIGH(ret
), t1
);
632 tcg_gen_shri_i32(t0
, arg1
, 32 - c
);
633 /* Note: ret can be the same as arg1, so we use t1 */
634 tcg_gen_shli_i32(t1
, arg1
, c
);
635 tcg_gen_shli_i32(TCGV_HIGH(ret
), TCGV_HIGH(arg1
), c
);
636 tcg_gen_or_i32(TCGV_HIGH(ret
), TCGV_HIGH(ret
), t0
);
637 tcg_gen_mov_i32(ret
, t1
);
645 static void tcg_reg_alloc_start(TCGContext
*s
)
649 for(i
= 0; i
< s
->nb_globals
; i
++) {
652 ts
->val_type
= TEMP_VAL_REG
;
654 ts
->val_type
= TEMP_VAL_MEM
;
657 for(i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
659 ts
->val_type
= TEMP_VAL_DEAD
;
660 ts
->mem_allocated
= 0;
663 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
664 s
->reg_to_temp
[i
] = -1;
668 static char *tcg_get_arg_str_idx(TCGContext
*s
, char *buf
, int buf_size
,
674 if (idx
< s
->nb_globals
) {
675 pstrcpy(buf
, buf_size
, ts
->name
);
678 snprintf(buf
, buf_size
, "loc%d", idx
- s
->nb_globals
);
680 snprintf(buf
, buf_size
, "tmp%d", idx
- s
->nb_globals
);
685 char *tcg_get_arg_str(TCGContext
*s
, char *buf
, int buf_size
, TCGv arg
)
687 return tcg_get_arg_str_idx(s
, buf
, buf_size
, GET_TCGV(arg
));
690 static int helper_cmp(const void *p1
, const void *p2
)
692 const TCGHelperInfo
*th1
= p1
;
693 const TCGHelperInfo
*th2
= p2
;
694 if (th1
->func
< th2
->func
)
696 else if (th1
->func
== th2
->func
)
702 /* find helper definition (Note: A hash table would be better) */
703 static TCGHelperInfo
*tcg_find_helper(TCGContext
*s
, tcg_target_ulong val
)
709 if (unlikely(!s
->helpers_sorted
)) {
710 qsort(s
->helpers
, s
->nb_helpers
, sizeof(TCGHelperInfo
),
712 s
->helpers_sorted
= 1;
717 m_max
= s
->nb_helpers
- 1;
718 while (m_min
<= m_max
) {
719 m
= (m_min
+ m_max
) >> 1;
733 void tcg_dump_ops(TCGContext
*s
, FILE *outfile
)
735 const uint16_t *opc_ptr
;
738 int c
, i
, k
, nb_oargs
, nb_iargs
, nb_cargs
, first_insn
;
743 opc_ptr
= gen_opc_buf
;
744 args
= gen_opparam_buf
;
745 while (opc_ptr
< gen_opc_ptr
) {
747 def
= &tcg_op_defs
[c
];
748 if (c
== INDEX_op_debug_insn_start
) {
750 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
751 pc
= ((uint64_t)args
[1] << 32) | args
[0];
756 fprintf(outfile
, "\n");
757 fprintf(outfile
, " ---- 0x%" PRIx64
, pc
);
759 nb_oargs
= def
->nb_oargs
;
760 nb_iargs
= def
->nb_iargs
;
761 nb_cargs
= def
->nb_cargs
;
762 } else if (c
== INDEX_op_call
) {
765 /* variable number of arguments */
767 nb_oargs
= arg
>> 16;
768 nb_iargs
= arg
& 0xffff;
769 nb_cargs
= def
->nb_cargs
;
771 fprintf(outfile
, " %s ", def
->name
);
774 fprintf(outfile
, "%s",
775 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[nb_oargs
+ nb_iargs
- 1]));
777 fprintf(outfile
, ",$0x%" TCG_PRIlx
,
778 args
[nb_oargs
+ nb_iargs
]);
780 fprintf(outfile
, ",$%d", nb_oargs
);
781 for(i
= 0; i
< nb_oargs
; i
++) {
782 fprintf(outfile
, ",");
783 fprintf(outfile
, "%s",
784 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[i
]));
786 for(i
= 0; i
< (nb_iargs
- 1); i
++) {
787 fprintf(outfile
, ",");
788 if (args
[nb_oargs
+ i
] == TCG_CALL_DUMMY_ARG
) {
789 fprintf(outfile
, "<dummy>");
791 fprintf(outfile
, "%s",
792 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[nb_oargs
+ i
]));
795 } else if (c
== INDEX_op_movi_i32
796 #if TCG_TARGET_REG_BITS == 64
797 || c
== INDEX_op_movi_i64
800 tcg_target_ulong val
;
803 nb_oargs
= def
->nb_oargs
;
804 nb_iargs
= def
->nb_iargs
;
805 nb_cargs
= def
->nb_cargs
;
806 fprintf(outfile
, " %s %s,$", def
->name
,
807 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[0]));
809 th
= tcg_find_helper(s
, val
);
811 fprintf(outfile
, th
->name
);
813 if (c
== INDEX_op_movi_i32
)
814 fprintf(outfile
, "0x%x", (uint32_t)val
);
816 fprintf(outfile
, "0x%" PRIx64
, (uint64_t)val
);
819 fprintf(outfile
, " %s ", def
->name
);
820 if (c
== INDEX_op_nopn
) {
821 /* variable number of arguments */
826 nb_oargs
= def
->nb_oargs
;
827 nb_iargs
= def
->nb_iargs
;
828 nb_cargs
= def
->nb_cargs
;
832 for(i
= 0; i
< nb_oargs
; i
++) {
834 fprintf(outfile
, ",");
835 fprintf(outfile
, "%s",
836 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[k
++]));
838 for(i
= 0; i
< nb_iargs
; i
++) {
840 fprintf(outfile
, ",");
841 fprintf(outfile
, "%s",
842 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[k
++]));
844 for(i
= 0; i
< nb_cargs
; i
++) {
846 fprintf(outfile
, ",");
848 fprintf(outfile
, "$0x%" TCG_PRIlx
, arg
);
851 fprintf(outfile
, "\n");
852 args
+= nb_iargs
+ nb_oargs
+ nb_cargs
;
856 /* we give more priority to constraints with less registers */
857 static int get_constraint_priority(const TCGOpDef
*def
, int k
)
859 const TCGArgConstraint
*arg_ct
;
862 arg_ct
= &def
->args_ct
[k
];
863 if (arg_ct
->ct
& TCG_CT_ALIAS
) {
864 /* an alias is equivalent to a single register */
867 if (!(arg_ct
->ct
& TCG_CT_REG
))
870 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
871 if (tcg_regset_test_reg(arg_ct
->u
.regs
, i
))
875 return TCG_TARGET_NB_REGS
- n
+ 1;
878 /* sort from highest priority to lowest */
879 static void sort_constraints(TCGOpDef
*def
, int start
, int n
)
881 int i
, j
, p1
, p2
, tmp
;
883 for(i
= 0; i
< n
; i
++)
884 def
->sorted_args
[start
+ i
] = start
+ i
;
887 for(i
= 0; i
< n
- 1; i
++) {
888 for(j
= i
+ 1; j
< n
; j
++) {
889 p1
= get_constraint_priority(def
, def
->sorted_args
[start
+ i
]);
890 p2
= get_constraint_priority(def
, def
->sorted_args
[start
+ j
]);
892 tmp
= def
->sorted_args
[start
+ i
];
893 def
->sorted_args
[start
+ i
] = def
->sorted_args
[start
+ j
];
894 def
->sorted_args
[start
+ j
] = tmp
;
900 void tcg_add_target_add_op_defs(const TCGTargetOpDef
*tdefs
)
911 assert(op
>= 0 && op
< NB_OPS
);
912 def
= &tcg_op_defs
[op
];
913 nb_args
= def
->nb_iargs
+ def
->nb_oargs
;
914 for(i
= 0; i
< nb_args
; i
++) {
915 ct_str
= tdefs
->args_ct_str
[i
];
916 tcg_regset_clear(def
->args_ct
[i
].u
.regs
);
917 def
->args_ct
[i
].ct
= 0;
918 if (ct_str
[0] >= '0' && ct_str
[0] <= '9') {
920 oarg
= ct_str
[0] - '0';
921 assert(oarg
< def
->nb_oargs
);
922 assert(def
->args_ct
[oarg
].ct
& TCG_CT_REG
);
923 /* TCG_CT_ALIAS is for the output arguments. The input
924 argument is tagged with TCG_CT_IALIAS. */
925 def
->args_ct
[i
] = def
->args_ct
[oarg
];
926 def
->args_ct
[oarg
].ct
= TCG_CT_ALIAS
;
927 def
->args_ct
[oarg
].alias_index
= i
;
928 def
->args_ct
[i
].ct
|= TCG_CT_IALIAS
;
929 def
->args_ct
[i
].alias_index
= oarg
;
936 def
->args_ct
[i
].ct
|= TCG_CT_CONST
;
940 if (target_parse_constraint(&def
->args_ct
[i
], &ct_str
) < 0) {
941 fprintf(stderr
, "Invalid constraint '%s' for arg %d of operation '%s'\n",
942 ct_str
, i
, def
->name
);
950 /* sort the constraints (XXX: this is just an heuristic) */
951 sort_constraints(def
, 0, def
->nb_oargs
);
952 sort_constraints(def
, def
->nb_oargs
, def
->nb_iargs
);
958 printf("%s: sorted=", def
->name
);
959 for(i
= 0; i
< def
->nb_oargs
+ def
->nb_iargs
; i
++)
960 printf(" %d", def
->sorted_args
[i
]);
969 #ifdef USE_LIVENESS_ANALYSIS
971 /* set a nop for an operation using 'nb_args' */
972 static inline void tcg_set_nop(TCGContext
*s
, uint16_t *opc_ptr
,
973 TCGArg
*args
, int nb_args
)
976 *opc_ptr
= INDEX_op_nop
;
978 *opc_ptr
= INDEX_op_nopn
;
980 args
[nb_args
- 1] = nb_args
;
984 /* liveness analysis: end of function: globals are live, temps are
986 /* XXX: at this stage, not used as there would be little gains because
987 most TBs end with a conditional jump. */
988 static inline void tcg_la_func_end(TCGContext
*s
, uint8_t *dead_temps
)
990 memset(dead_temps
, 0, s
->nb_globals
);
991 memset(dead_temps
+ s
->nb_globals
, 1, s
->nb_temps
- s
->nb_globals
);
994 /* liveness analysis: end of basic block: globals are live, temps are
995 dead, local temps are live. */
996 static inline void tcg_la_bb_end(TCGContext
*s
, uint8_t *dead_temps
)
1001 memset(dead_temps
, 0, s
->nb_globals
);
1002 ts
= &s
->temps
[s
->nb_globals
];
1003 for(i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
1012 /* Liveness analysis : update the opc_dead_iargs array to tell if a
1013 given input arguments is dead. Instructions updating dead
1014 temporaries are removed. */
1015 static void tcg_liveness_analysis(TCGContext
*s
)
1017 int i
, op_index
, op
, nb_args
, nb_iargs
, nb_oargs
, arg
, nb_ops
;
1019 const TCGOpDef
*def
;
1020 uint8_t *dead_temps
;
1021 unsigned int dead_iargs
;
1023 gen_opc_ptr
++; /* skip end */
1025 nb_ops
= gen_opc_ptr
- gen_opc_buf
;
1027 /* XXX: make it really dynamic */
1028 s
->op_dead_iargs
= tcg_malloc(OPC_BUF_SIZE
* sizeof(uint16_t));
1030 dead_temps
= tcg_malloc(s
->nb_temps
);
1031 memset(dead_temps
, 1, s
->nb_temps
);
1033 args
= gen_opparam_ptr
;
1034 op_index
= nb_ops
- 1;
1035 while (op_index
>= 0) {
1036 op
= gen_opc_buf
[op_index
];
1037 def
= &tcg_op_defs
[op
];
1045 nb_iargs
= args
[0] & 0xffff;
1046 nb_oargs
= args
[0] >> 16;
1048 call_flags
= args
[nb_oargs
+ nb_iargs
];
1050 /* pure functions can be removed if their result is not
1052 if (call_flags
& TCG_CALL_PURE
) {
1053 for(i
= 0; i
< nb_oargs
; i
++) {
1055 if (!dead_temps
[arg
])
1056 goto do_not_remove_call
;
1058 tcg_set_nop(s
, gen_opc_buf
+ op_index
,
1063 /* output args are dead */
1064 for(i
= 0; i
< nb_oargs
; i
++) {
1066 dead_temps
[arg
] = 1;
1069 /* globals are live (they may be used by the call) */
1070 memset(dead_temps
, 0, s
->nb_globals
);
1072 /* input args are live */
1074 for(i
= 0; i
< nb_iargs
; i
++) {
1075 arg
= args
[i
+ nb_oargs
];
1076 if (arg
!= TCG_CALL_DUMMY_ARG
) {
1077 if (dead_temps
[arg
]) {
1078 dead_iargs
|= (1 << i
);
1080 dead_temps
[arg
] = 0;
1083 s
->op_dead_iargs
[op_index
] = dead_iargs
;
1088 case INDEX_op_set_label
:
1090 /* mark end of basic block */
1091 tcg_la_bb_end(s
, dead_temps
);
1093 case INDEX_op_debug_insn_start
:
1094 args
-= def
->nb_args
;
1100 case INDEX_op_discard
:
1102 /* mark the temporary as dead */
1103 dead_temps
[args
[0]] = 1;
1107 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1109 if (op
> INDEX_op_end
) {
1110 args
-= def
->nb_args
;
1111 nb_iargs
= def
->nb_iargs
;
1112 nb_oargs
= def
->nb_oargs
;
1114 /* Test if the operation can be removed because all
1115 its outputs are dead. We assume that nb_oargs == 0
1116 implies side effects */
1117 if (!(def
->flags
& TCG_OPF_SIDE_EFFECTS
) && nb_oargs
!= 0) {
1118 for(i
= 0; i
< nb_oargs
; i
++) {
1120 if (!dead_temps
[arg
])
1123 tcg_set_nop(s
, gen_opc_buf
+ op_index
, args
, def
->nb_args
);
1124 #ifdef CONFIG_PROFILER
1130 /* output args are dead */
1131 for(i
= 0; i
< nb_oargs
; i
++) {
1133 dead_temps
[arg
] = 1;
1136 /* if end of basic block, update */
1137 if (def
->flags
& TCG_OPF_BB_END
) {
1138 tcg_la_bb_end(s
, dead_temps
);
1139 } else if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
1140 /* globals are live */
1141 memset(dead_temps
, 0, s
->nb_globals
);
1144 /* input args are live */
1146 for(i
= 0; i
< nb_iargs
; i
++) {
1147 arg
= args
[i
+ nb_oargs
];
1148 if (dead_temps
[arg
]) {
1149 dead_iargs
|= (1 << i
);
1151 dead_temps
[arg
] = 0;
1153 s
->op_dead_iargs
[op_index
] = dead_iargs
;
1156 /* legacy dyngen operations */
1157 args
-= def
->nb_args
;
1158 /* mark end of basic block */
1159 tcg_la_bb_end(s
, dead_temps
);
1166 if (args
!= gen_opparam_buf
)
1170 /* dummy liveness analysis */
1171 void tcg_liveness_analysis(TCGContext
*s
)
1174 nb_ops
= gen_opc_ptr
- gen_opc_buf
;
1176 s
->op_dead_iargs
= tcg_malloc(nb_ops
* sizeof(uint16_t));
1177 memset(s
->op_dead_iargs
, 0, nb_ops
* sizeof(uint16_t));
1182 static void dump_regs(TCGContext
*s
)
1188 for(i
= 0; i
< s
->nb_temps
; i
++) {
1190 printf(" %10s: ", tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), i
));
1191 switch(ts
->val_type
) {
1193 printf("%s", tcg_target_reg_names
[ts
->reg
]);
1196 printf("%d(%s)", (int)ts
->mem_offset
, tcg_target_reg_names
[ts
->mem_reg
]);
1198 case TEMP_VAL_CONST
:
1199 printf("$0x%" TCG_PRIlx
, ts
->val
);
1211 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
1212 if (s
->reg_to_temp
[i
] >= 0) {
1214 tcg_target_reg_names
[i
],
1215 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), s
->reg_to_temp
[i
]));
1220 static void check_regs(TCGContext
*s
)
1226 for(reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
1227 k
= s
->reg_to_temp
[reg
];
1230 if (ts
->val_type
!= TEMP_VAL_REG
||
1232 printf("Inconsistency for register %s:\n",
1233 tcg_target_reg_names
[reg
]);
1238 for(k
= 0; k
< s
->nb_temps
; k
++) {
1240 if (ts
->val_type
== TEMP_VAL_REG
&&
1242 s
->reg_to_temp
[ts
->reg
] != k
) {
1243 printf("Inconsistency for temp %s:\n",
1244 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), k
));
1246 printf("reg state:\n");
1254 static void temp_allocate_frame(TCGContext
*s
, int temp
)
1257 ts
= &s
->temps
[temp
];
1258 s
->current_frame_offset
= (s
->current_frame_offset
+ sizeof(tcg_target_long
) - 1) & ~(sizeof(tcg_target_long
) - 1);
1259 if (s
->current_frame_offset
+ sizeof(tcg_target_long
) > s
->frame_end
)
1261 ts
->mem_offset
= s
->current_frame_offset
;
1262 ts
->mem_reg
= s
->frame_reg
;
1263 ts
->mem_allocated
= 1;
1264 s
->current_frame_offset
+= sizeof(tcg_target_long
);
1267 /* free register 'reg' by spilling the corresponding temporary if necessary */
1268 static void tcg_reg_free(TCGContext
*s
, int reg
)
1273 temp
= s
->reg_to_temp
[reg
];
1275 ts
= &s
->temps
[temp
];
1276 assert(ts
->val_type
== TEMP_VAL_REG
);
1277 if (!ts
->mem_coherent
) {
1278 if (!ts
->mem_allocated
)
1279 temp_allocate_frame(s
, temp
);
1280 tcg_out_st(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1282 ts
->val_type
= TEMP_VAL_MEM
;
1283 s
->reg_to_temp
[reg
] = -1;
1287 /* Allocate a register belonging to reg1 & ~reg2 */
1288 static int tcg_reg_alloc(TCGContext
*s
, TCGRegSet reg1
, TCGRegSet reg2
)
1293 tcg_regset_andnot(reg_ct
, reg1
, reg2
);
1295 /* first try free registers */
1296 for(i
= 0; i
< ARRAY_SIZE(tcg_target_reg_alloc_order
); i
++) {
1297 reg
= tcg_target_reg_alloc_order
[i
];
1298 if (tcg_regset_test_reg(reg_ct
, reg
) && s
->reg_to_temp
[reg
] == -1)
1302 /* XXX: do better spill choice */
1303 for(i
= 0; i
< ARRAY_SIZE(tcg_target_reg_alloc_order
); i
++) {
1304 reg
= tcg_target_reg_alloc_order
[i
];
1305 if (tcg_regset_test_reg(reg_ct
, reg
)) {
1306 tcg_reg_free(s
, reg
);
1314 /* save a temporary to memory. 'allocated_regs' is used in case a
1315 temporary registers needs to be allocated to store a constant. */
1316 static void temp_save(TCGContext
*s
, int temp
, TCGRegSet allocated_regs
)
1321 ts
= &s
->temps
[temp
];
1322 if (!ts
->fixed_reg
) {
1323 switch(ts
->val_type
) {
1325 tcg_reg_free(s
, ts
->reg
);
1328 ts
->val_type
= TEMP_VAL_MEM
;
1330 case TEMP_VAL_CONST
:
1331 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
1333 if (!ts
->mem_allocated
)
1334 temp_allocate_frame(s
, temp
);
1335 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1336 tcg_out_st(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1337 ts
->val_type
= TEMP_VAL_MEM
;
1347 /* save globals to their cannonical location and assume they can be
1348 modified be the following code. 'allocated_regs' is used in case a
1349 temporary registers needs to be allocated to store a constant. */
1350 static void save_globals(TCGContext
*s
, TCGRegSet allocated_regs
)
1354 for(i
= 0; i
< s
->nb_globals
; i
++) {
1355 temp_save(s
, i
, allocated_regs
);
1359 /* at the end of a basic block, we assume all temporaries are dead and
1360 all globals are stored at their canonical location. */
1361 static void tcg_reg_alloc_bb_end(TCGContext
*s
, TCGRegSet allocated_regs
)
1366 for(i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
1368 if (ts
->temp_local
) {
1369 temp_save(s
, i
, allocated_regs
);
1371 if (ts
->val_type
== TEMP_VAL_REG
) {
1372 s
->reg_to_temp
[ts
->reg
] = -1;
1374 ts
->val_type
= TEMP_VAL_DEAD
;
1378 save_globals(s
, allocated_regs
);
1381 #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1)
1383 static void tcg_reg_alloc_movi(TCGContext
*s
, const TCGArg
*args
)
1386 tcg_target_ulong val
;
1388 ots
= &s
->temps
[args
[0]];
1391 if (ots
->fixed_reg
) {
1392 /* for fixed registers, we do not do any constant
1394 tcg_out_movi(s
, ots
->type
, ots
->reg
, val
);
1396 /* The movi is not explicitly generated here */
1397 if (ots
->val_type
== TEMP_VAL_REG
)
1398 s
->reg_to_temp
[ots
->reg
] = -1;
1399 ots
->val_type
= TEMP_VAL_CONST
;
1404 static void tcg_reg_alloc_mov(TCGContext
*s
, const TCGOpDef
*def
,
1406 unsigned int dead_iargs
)
1410 const TCGArgConstraint
*arg_ct
;
1412 ots
= &s
->temps
[args
[0]];
1413 ts
= &s
->temps
[args
[1]];
1414 arg_ct
= &def
->args_ct
[0];
1416 /* XXX: always mark arg dead if IS_DEAD_IARG(0) */
1417 if (ts
->val_type
== TEMP_VAL_REG
) {
1418 if (IS_DEAD_IARG(0) && !ts
->fixed_reg
&& !ots
->fixed_reg
) {
1419 /* the mov can be suppressed */
1420 if (ots
->val_type
== TEMP_VAL_REG
)
1421 s
->reg_to_temp
[ots
->reg
] = -1;
1423 s
->reg_to_temp
[reg
] = -1;
1424 ts
->val_type
= TEMP_VAL_DEAD
;
1426 if (ots
->val_type
== TEMP_VAL_REG
) {
1429 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, s
->reserved_regs
);
1431 if (ts
->reg
!= reg
) {
1432 tcg_out_mov(s
, reg
, ts
->reg
);
1435 } else if (ts
->val_type
== TEMP_VAL_MEM
) {
1436 if (ots
->val_type
== TEMP_VAL_REG
) {
1439 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, s
->reserved_regs
);
1441 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1442 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1443 if (ots
->fixed_reg
) {
1445 tcg_out_movi(s
, ots
->type
, reg
, ts
->val
);
1447 /* propagate constant */
1448 if (ots
->val_type
== TEMP_VAL_REG
)
1449 s
->reg_to_temp
[ots
->reg
] = -1;
1450 ots
->val_type
= TEMP_VAL_CONST
;
1457 s
->reg_to_temp
[reg
] = args
[0];
1459 ots
->val_type
= TEMP_VAL_REG
;
1460 ots
->mem_coherent
= 0;
1463 static void tcg_reg_alloc_op(TCGContext
*s
,
1464 const TCGOpDef
*def
, int opc
,
1466 unsigned int dead_iargs
)
1468 TCGRegSet allocated_regs
;
1469 int i
, k
, nb_iargs
, nb_oargs
, reg
;
1471 const TCGArgConstraint
*arg_ct
;
1473 TCGArg new_args
[TCG_MAX_OP_ARGS
];
1474 int const_args
[TCG_MAX_OP_ARGS
];
1476 nb_oargs
= def
->nb_oargs
;
1477 nb_iargs
= def
->nb_iargs
;
1479 /* copy constants */
1480 memcpy(new_args
+ nb_oargs
+ nb_iargs
,
1481 args
+ nb_oargs
+ nb_iargs
,
1482 sizeof(TCGArg
) * def
->nb_cargs
);
1484 /* satisfy input constraints */
1485 tcg_regset_set(allocated_regs
, s
->reserved_regs
);
1486 for(k
= 0; k
< nb_iargs
; k
++) {
1487 i
= def
->sorted_args
[nb_oargs
+ k
];
1489 arg_ct
= &def
->args_ct
[i
];
1490 ts
= &s
->temps
[arg
];
1491 if (ts
->val_type
== TEMP_VAL_MEM
) {
1492 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1493 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1494 ts
->val_type
= TEMP_VAL_REG
;
1496 ts
->mem_coherent
= 1;
1497 s
->reg_to_temp
[reg
] = arg
;
1498 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1499 if (tcg_target_const_match(ts
->val
, arg_ct
)) {
1500 /* constant is OK for instruction */
1502 new_args
[i
] = ts
->val
;
1505 /* need to move to a register */
1506 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1507 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1508 ts
->val_type
= TEMP_VAL_REG
;
1510 ts
->mem_coherent
= 0;
1511 s
->reg_to_temp
[reg
] = arg
;
1514 assert(ts
->val_type
== TEMP_VAL_REG
);
1515 if (arg_ct
->ct
& TCG_CT_IALIAS
) {
1516 if (ts
->fixed_reg
) {
1517 /* if fixed register, we must allocate a new register
1518 if the alias is not the same register */
1519 if (arg
!= args
[arg_ct
->alias_index
])
1520 goto allocate_in_reg
;
1522 /* if the input is aliased to an output and if it is
1523 not dead after the instruction, we must allocate
1524 a new register and move it */
1525 if (!IS_DEAD_IARG(i
- nb_oargs
))
1526 goto allocate_in_reg
;
1530 if (tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
1531 /* nothing to do : the constraint is satisfied */
1534 /* allocate a new register matching the constraint
1535 and move the temporary register into it */
1536 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1537 tcg_out_mov(s
, reg
, ts
->reg
);
1541 tcg_regset_set_reg(allocated_regs
, reg
);
1545 if (def
->flags
& TCG_OPF_BB_END
) {
1546 tcg_reg_alloc_bb_end(s
, allocated_regs
);
1548 /* mark dead temporaries and free the associated registers */
1549 for(i
= 0; i
< nb_iargs
; i
++) {
1550 arg
= args
[nb_oargs
+ i
];
1551 if (IS_DEAD_IARG(i
)) {
1552 ts
= &s
->temps
[arg
];
1553 if (!ts
->fixed_reg
) {
1554 if (ts
->val_type
== TEMP_VAL_REG
)
1555 s
->reg_to_temp
[ts
->reg
] = -1;
1556 ts
->val_type
= TEMP_VAL_DEAD
;
1561 if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
1562 /* XXX: permit generic clobber register list ? */
1563 for(reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
1564 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, reg
)) {
1565 tcg_reg_free(s
, reg
);
1568 /* XXX: for load/store we could do that only for the slow path
1569 (i.e. when a memory callback is called) */
1571 /* store globals and free associated registers (we assume the insn
1572 can modify any global. */
1573 save_globals(s
, allocated_regs
);
1576 /* satisfy the output constraints */
1577 tcg_regset_set(allocated_regs
, s
->reserved_regs
);
1578 for(k
= 0; k
< nb_oargs
; k
++) {
1579 i
= def
->sorted_args
[k
];
1581 arg_ct
= &def
->args_ct
[i
];
1582 ts
= &s
->temps
[arg
];
1583 if (arg_ct
->ct
& TCG_CT_ALIAS
) {
1584 reg
= new_args
[arg_ct
->alias_index
];
1586 /* if fixed register, we try to use it */
1588 if (ts
->fixed_reg
&&
1589 tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
1592 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1594 tcg_regset_set_reg(allocated_regs
, reg
);
1595 /* if a fixed register is used, then a move will be done afterwards */
1596 if (!ts
->fixed_reg
) {
1597 if (ts
->val_type
== TEMP_VAL_REG
)
1598 s
->reg_to_temp
[ts
->reg
] = -1;
1599 ts
->val_type
= TEMP_VAL_REG
;
1601 /* temp value is modified, so the value kept in memory is
1602 potentially not the same */
1603 ts
->mem_coherent
= 0;
1604 s
->reg_to_temp
[reg
] = arg
;
1611 /* emit instruction */
1612 tcg_out_op(s
, opc
, new_args
, const_args
);
1614 /* move the outputs in the correct register if needed */
1615 for(i
= 0; i
< nb_oargs
; i
++) {
1616 ts
= &s
->temps
[args
[i
]];
1618 if (ts
->fixed_reg
&& ts
->reg
!= reg
) {
1619 tcg_out_mov(s
, ts
->reg
, reg
);
1624 #ifdef TCG_TARGET_STACK_GROWSUP
1625 #define STACK_DIR(x) (-(x))
1627 #define STACK_DIR(x) (x)
1630 static int tcg_reg_alloc_call(TCGContext
*s
, const TCGOpDef
*def
,
1631 int opc
, const TCGArg
*args
,
1632 unsigned int dead_iargs
)
1634 int nb_iargs
, nb_oargs
, flags
, nb_regs
, i
, reg
, nb_params
;
1635 TCGArg arg
, func_arg
;
1637 tcg_target_long stack_offset
, call_stack_size
, func_addr
;
1638 int const_func_arg
, allocate_args
;
1639 TCGRegSet allocated_regs
;
1640 const TCGArgConstraint
*arg_ct
;
1644 nb_oargs
= arg
>> 16;
1645 nb_iargs
= arg
& 0xffff;
1646 nb_params
= nb_iargs
- 1;
1648 flags
= args
[nb_oargs
+ nb_iargs
];
1650 nb_regs
= tcg_target_get_call_iarg_regs_count(flags
);
1651 if (nb_regs
> nb_params
)
1652 nb_regs
= nb_params
;
1654 /* assign stack slots first */
1655 /* XXX: preallocate call stack */
1656 call_stack_size
= (nb_params
- nb_regs
) * sizeof(tcg_target_long
);
1657 call_stack_size
= (call_stack_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
1658 ~(TCG_TARGET_STACK_ALIGN
- 1);
1659 allocate_args
= (call_stack_size
> TCG_STATIC_CALL_ARGS_SIZE
);
1660 if (allocate_args
) {
1661 tcg_out_addi(s
, TCG_REG_CALL_STACK
, -STACK_DIR(call_stack_size
));
1664 stack_offset
= TCG_TARGET_CALL_STACK_OFFSET
;
1665 for(i
= nb_regs
; i
< nb_params
; i
++) {
1666 arg
= args
[nb_oargs
+ i
];
1667 #ifdef TCG_TARGET_STACK_GROWSUP
1668 stack_offset
-= sizeof(tcg_target_long
);
1670 if (arg
!= TCG_CALL_DUMMY_ARG
) {
1671 ts
= &s
->temps
[arg
];
1672 if (ts
->val_type
== TEMP_VAL_REG
) {
1673 tcg_out_st(s
, ts
->type
, ts
->reg
, TCG_REG_CALL_STACK
, stack_offset
);
1674 } else if (ts
->val_type
== TEMP_VAL_MEM
) {
1675 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
1677 /* XXX: not correct if reading values from the stack */
1678 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1679 tcg_out_st(s
, ts
->type
, reg
, TCG_REG_CALL_STACK
, stack_offset
);
1680 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1681 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
1683 /* XXX: sign extend may be needed on some targets */
1684 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1685 tcg_out_st(s
, ts
->type
, reg
, TCG_REG_CALL_STACK
, stack_offset
);
1690 #ifndef TCG_TARGET_STACK_GROWSUP
1691 stack_offset
+= sizeof(tcg_target_long
);
1695 /* assign input registers */
1696 tcg_regset_set(allocated_regs
, s
->reserved_regs
);
1697 for(i
= 0; i
< nb_regs
; i
++) {
1698 arg
= args
[nb_oargs
+ i
];
1699 if (arg
!= TCG_CALL_DUMMY_ARG
) {
1700 ts
= &s
->temps
[arg
];
1701 reg
= tcg_target_call_iarg_regs
[i
];
1702 tcg_reg_free(s
, reg
);
1703 if (ts
->val_type
== TEMP_VAL_REG
) {
1704 if (ts
->reg
!= reg
) {
1705 tcg_out_mov(s
, reg
, ts
->reg
);
1707 } else if (ts
->val_type
== TEMP_VAL_MEM
) {
1708 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1709 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1710 /* XXX: sign extend ? */
1711 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1715 tcg_regset_set_reg(allocated_regs
, reg
);
1719 /* assign function address */
1720 func_arg
= args
[nb_oargs
+ nb_iargs
- 1];
1721 arg_ct
= &def
->args_ct
[0];
1722 ts
= &s
->temps
[func_arg
];
1723 func_addr
= ts
->val
;
1725 if (ts
->val_type
== TEMP_VAL_MEM
) {
1726 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1727 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1729 tcg_regset_set_reg(allocated_regs
, reg
);
1730 } else if (ts
->val_type
== TEMP_VAL_REG
) {
1732 if (!tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
1733 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1734 tcg_out_mov(s
, reg
, ts
->reg
);
1737 tcg_regset_set_reg(allocated_regs
, reg
);
1738 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1739 if (tcg_target_const_match(func_addr
, arg_ct
)) {
1741 func_arg
= func_addr
;
1743 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1744 tcg_out_movi(s
, ts
->type
, reg
, func_addr
);
1746 tcg_regset_set_reg(allocated_regs
, reg
);
1753 /* mark dead temporaries and free the associated registers */
1754 for(i
= 0; i
< nb_iargs
; i
++) {
1755 arg
= args
[nb_oargs
+ i
];
1756 if (IS_DEAD_IARG(i
)) {
1757 ts
= &s
->temps
[arg
];
1758 if (!ts
->fixed_reg
) {
1759 if (ts
->val_type
== TEMP_VAL_REG
)
1760 s
->reg_to_temp
[ts
->reg
] = -1;
1761 ts
->val_type
= TEMP_VAL_DEAD
;
1766 /* clobber call registers */
1767 for(reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
1768 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, reg
)) {
1769 tcg_reg_free(s
, reg
);
1773 /* store globals and free associated registers (we assume the call
1774 can modify any global. */
1775 save_globals(s
, allocated_regs
);
1777 tcg_out_op(s
, opc
, &func_arg
, &const_func_arg
);
1779 if (allocate_args
) {
1780 tcg_out_addi(s
, TCG_REG_CALL_STACK
, STACK_DIR(call_stack_size
));
1783 /* assign output registers and emit moves if needed */
1784 for(i
= 0; i
< nb_oargs
; i
++) {
1786 ts
= &s
->temps
[arg
];
1787 reg
= tcg_target_call_oarg_regs
[i
];
1788 assert(s
->reg_to_temp
[reg
] == -1);
1789 if (ts
->fixed_reg
) {
1790 if (ts
->reg
!= reg
) {
1791 tcg_out_mov(s
, ts
->reg
, reg
);
1794 if (ts
->val_type
== TEMP_VAL_REG
)
1795 s
->reg_to_temp
[ts
->reg
] = -1;
1796 ts
->val_type
= TEMP_VAL_REG
;
1798 ts
->mem_coherent
= 0;
1799 s
->reg_to_temp
[reg
] = arg
;
1803 return nb_iargs
+ nb_oargs
+ def
->nb_cargs
+ 1;
1806 #ifdef CONFIG_PROFILER
1808 static int64_t dyngen_table_op_count
[NB_OPS
];
1810 void dump_op_count(void)
1814 f
= fopen("/tmp/op1.log", "w");
1815 for(i
= 0; i
< INDEX_op_end
; i
++) {
1816 fprintf(f
, "%s %" PRId64
"\n", tcg_op_defs
[i
].name
, dyngen_table_op_count
[i
]);
1819 f
= fopen("/tmp/op2.log", "w");
1820 for(i
= INDEX_op_end
; i
< NB_OPS
; i
++) {
1821 fprintf(f
, "%s %" PRId64
"\n", tcg_op_defs
[i
].name
, dyngen_table_op_count
[i
]);
1828 static inline int tcg_gen_code_common(TCGContext
*s
, uint8_t *gen_code_buf
,
1832 const TCGOpDef
*def
;
1833 unsigned int dead_iargs
;
1837 if (unlikely(loglevel
& CPU_LOG_TB_OP
)) {
1838 fprintf(logfile
, "OP:\n");
1839 tcg_dump_ops(s
, logfile
);
1840 fprintf(logfile
, "\n");
1844 #ifdef CONFIG_PROFILER
1845 s
->la_time
-= profile_getclock();
1847 tcg_liveness_analysis(s
);
1848 #ifdef CONFIG_PROFILER
1849 s
->la_time
+= profile_getclock();
1853 if (unlikely(loglevel
& CPU_LOG_TB_OP_OPT
)) {
1854 fprintf(logfile
, "OP after la:\n");
1855 tcg_dump_ops(s
, logfile
);
1856 fprintf(logfile
, "\n");
1860 tcg_reg_alloc_start(s
);
1862 s
->code_buf
= gen_code_buf
;
1863 s
->code_ptr
= gen_code_buf
;
1865 args
= gen_opparam_buf
;
1869 opc
= gen_opc_buf
[op_index
];
1870 #ifdef CONFIG_PROFILER
1871 dyngen_table_op_count
[opc
]++;
1873 def
= &tcg_op_defs
[opc
];
1875 printf("%s: %d %d %d\n", def
->name
,
1876 def
->nb_oargs
, def
->nb_iargs
, def
->nb_cargs
);
1880 case INDEX_op_mov_i32
:
1881 #if TCG_TARGET_REG_BITS == 64
1882 case INDEX_op_mov_i64
:
1884 dead_iargs
= s
->op_dead_iargs
[op_index
];
1885 tcg_reg_alloc_mov(s
, def
, args
, dead_iargs
);
1887 case INDEX_op_movi_i32
:
1888 #if TCG_TARGET_REG_BITS == 64
1889 case INDEX_op_movi_i64
:
1891 tcg_reg_alloc_movi(s
, args
);
1893 case INDEX_op_debug_insn_start
:
1894 /* debug instruction */
1904 case INDEX_op_discard
:
1907 ts
= &s
->temps
[args
[0]];
1908 /* mark the temporary as dead */
1909 if (!ts
->fixed_reg
) {
1910 if (ts
->val_type
== TEMP_VAL_REG
)
1911 s
->reg_to_temp
[ts
->reg
] = -1;
1912 ts
->val_type
= TEMP_VAL_DEAD
;
1916 case INDEX_op_set_label
:
1917 tcg_reg_alloc_bb_end(s
, s
->reserved_regs
);
1918 tcg_out_label(s
, args
[0], (long)s
->code_ptr
);
1921 dead_iargs
= s
->op_dead_iargs
[op_index
];
1922 args
+= tcg_reg_alloc_call(s
, def
, opc
, args
, dead_iargs
);
1927 #ifdef CONFIG_DYNGEN_OP
1928 case 0 ... INDEX_op_end
- 1:
1929 /* legacy dyngen ops */
1930 #ifdef CONFIG_PROFILER
1933 tcg_reg_alloc_bb_end(s
, s
->reserved_regs
);
1934 if (search_pc
>= 0) {
1935 s
->code_ptr
+= def
->copy_size
;
1936 args
+= def
->nb_args
;
1938 args
= dyngen_op(s
, opc
, args
);
1943 /* Note: in order to speed up the code, it would be much
1944 faster to have specialized register allocator functions for
1945 some common argument patterns */
1946 dead_iargs
= s
->op_dead_iargs
[op_index
];
1947 tcg_reg_alloc_op(s
, def
, opc
, args
, dead_iargs
);
1950 args
+= def
->nb_args
;
1952 if (search_pc
>= 0 && search_pc
< s
->code_ptr
- gen_code_buf
) {
1964 int dyngen_code(TCGContext
*s
, uint8_t *gen_code_buf
)
1966 #ifdef CONFIG_PROFILER
1969 n
= (gen_opc_ptr
- gen_opc_buf
);
1971 if (n
> s
->op_count_max
)
1972 s
->op_count_max
= n
;
1974 s
->temp_count
+= s
->nb_temps
;
1975 if (s
->nb_temps
> s
->temp_count_max
)
1976 s
->temp_count_max
= s
->nb_temps
;
1980 tcg_gen_code_common(s
, gen_code_buf
, -1);
1982 /* flush instruction cache */
1983 flush_icache_range((unsigned long)gen_code_buf
,
1984 (unsigned long)s
->code_ptr
);
1985 return s
->code_ptr
- gen_code_buf
;
1988 /* Return the index of the micro operation such as the pc after is <
1989 offset bytes from the start of the TB. The contents of gen_code_buf must
1990 not be changed, though writing the same values is ok.
1991 Return -1 if not found. */
1992 int dyngen_code_search_pc(TCGContext
*s
, uint8_t *gen_code_buf
, long offset
)
1994 return tcg_gen_code_common(s
, gen_code_buf
, offset
);
1997 #ifdef CONFIG_PROFILER
1998 void tcg_dump_info(FILE *f
,
1999 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
2001 TCGContext
*s
= &tcg_ctx
;
2004 tot
= s
->interm_time
+ s
->code_time
;
2005 cpu_fprintf(f
, "JIT cycles %" PRId64
" (%0.3f s at 2.4 GHz)\n",
2007 cpu_fprintf(f
, "translated TBs %" PRId64
" (aborted=%" PRId64
" %0.1f%%)\n",
2009 s
->tb_count1
- s
->tb_count
,
2010 s
->tb_count1
? (double)(s
->tb_count1
- s
->tb_count
) / s
->tb_count1
* 100.0 : 0);
2011 cpu_fprintf(f
, "avg ops/TB %0.1f max=%d\n",
2012 s
->tb_count
? (double)s
->op_count
/ s
->tb_count
: 0, s
->op_count_max
);
2013 cpu_fprintf(f
, "old ops/total ops %0.1f%%\n",
2014 s
->op_count
? (double)s
->old_op_count
/ s
->op_count
* 100.0 : 0);
2015 cpu_fprintf(f
, "deleted ops/TB %0.2f\n",
2017 (double)s
->del_op_count
/ s
->tb_count
: 0);
2018 cpu_fprintf(f
, "avg temps/TB %0.2f max=%d\n",
2020 (double)s
->temp_count
/ s
->tb_count
: 0,
2023 cpu_fprintf(f
, "cycles/op %0.1f\n",
2024 s
->op_count
? (double)tot
/ s
->op_count
: 0);
2025 cpu_fprintf(f
, "cycles/in byte %0.1f\n",
2026 s
->code_in_len
? (double)tot
/ s
->code_in_len
: 0);
2027 cpu_fprintf(f
, "cycles/out byte %0.1f\n",
2028 s
->code_out_len
? (double)tot
/ s
->code_out_len
: 0);
2031 cpu_fprintf(f
, " gen_interm time %0.1f%%\n",
2032 (double)s
->interm_time
/ tot
* 100.0);
2033 cpu_fprintf(f
, " gen_code time %0.1f%%\n",
2034 (double)s
->code_time
/ tot
* 100.0);
2035 cpu_fprintf(f
, "liveness/code time %0.1f%%\n",
2036 (double)s
->la_time
/ (s
->code_time
? s
->code_time
: 1) * 100.0);
2037 cpu_fprintf(f
, "cpu_restore count %" PRId64
"\n",
2039 cpu_fprintf(f
, " avg cycles %0.1f\n",
2040 s
->restore_count
? (double)s
->restore_time
/ s
->restore_count
: 0);
2042 extern void dump_op_count(void);
2047 void tcg_dump_info(FILE *f
,
2048 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
2050 cpu_fprintf(f
, "[TCG profiler not compiled]\n");