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 /* free all the pool */
200 void tcg_pool_free(TCGContext
*s
)
204 for(p
= s
->pool_first
; p
!= NULL
; p
= p1
) {
208 s
->pool_first
= NULL
;
209 s
->pool_cur
= s
->pool_end
= NULL
;
212 void tcg_context_init(TCGContext
*s
)
214 int op
, total_args
, n
;
216 TCGArgConstraint
*args_ct
;
219 memset(s
, 0, sizeof(*s
));
220 s
->temps
= s
->static_temps
;
223 /* Count total number of arguments and allocate the corresponding
226 for(op
= 0; op
< NB_OPS
; op
++) {
227 def
= &tcg_op_defs
[op
];
228 n
= def
->nb_iargs
+ def
->nb_oargs
;
232 args_ct
= qemu_malloc(sizeof(TCGArgConstraint
) * total_args
);
233 sorted_args
= qemu_malloc(sizeof(int) * total_args
);
235 for(op
= 0; op
< NB_OPS
; op
++) {
236 def
= &tcg_op_defs
[op
];
237 def
->args_ct
= args_ct
;
238 def
->sorted_args
= sorted_args
;
239 n
= def
->nb_iargs
+ def
->nb_oargs
;
246 /* init global prologue and epilogue */
247 s
->code_buf
= code_gen_prologue
;
248 s
->code_ptr
= s
->code_buf
;
249 tcg_target_qemu_prologue(s
);
250 flush_icache_range((unsigned long)s
->code_buf
,
251 (unsigned long)s
->code_ptr
);
254 void tcg_set_frame(TCGContext
*s
, int reg
,
255 tcg_target_long start
, tcg_target_long size
)
257 s
->frame_start
= start
;
258 s
->frame_end
= start
+ size
;
262 void tcg_func_start(TCGContext
*s
)
266 s
->nb_temps
= s
->nb_globals
;
267 for(i
= 0; i
< (TCG_TYPE_COUNT
* 2); i
++)
268 s
->first_free_temp
[i
] = -1;
269 s
->labels
= tcg_malloc(sizeof(TCGLabel
) * TCG_MAX_LABELS
);
271 s
->current_frame_offset
= s
->frame_start
;
273 gen_opc_ptr
= gen_opc_buf
;
274 gen_opparam_ptr
= gen_opparam_buf
;
277 static inline void tcg_temp_alloc(TCGContext
*s
, int n
)
279 if (n
> TCG_MAX_TEMPS
)
283 TCGv
tcg_global_reg_new(TCGType type
, int reg
, const char *name
)
285 TCGContext
*s
= &tcg_ctx
;
289 #if TCG_TARGET_REG_BITS == 32
290 if (type
!= TCG_TYPE_I32
)
293 if (tcg_regset_test_reg(s
->reserved_regs
, reg
))
296 tcg_temp_alloc(s
, s
->nb_globals
+ 1);
297 ts
= &s
->temps
[s
->nb_globals
];
298 ts
->base_type
= type
;
304 tcg_regset_set_reg(s
->reserved_regs
, reg
);
305 return MAKE_TCGV(idx
);
308 #if TCG_TARGET_REG_BITS == 32
309 /* temporary hack to avoid register shortage for tcg_qemu_st64() */
310 TCGv
tcg_global_reg2_new_hack(TCGType type
, int reg1
, int reg2
,
313 TCGContext
*s
= &tcg_ctx
;
318 if (type
!= TCG_TYPE_I64
)
321 tcg_temp_alloc(s
, s
->nb_globals
+ 2);
322 ts
= &s
->temps
[s
->nb_globals
];
323 ts
->base_type
= type
;
324 ts
->type
= TCG_TYPE_I32
;
327 pstrcpy(buf
, sizeof(buf
), name
);
328 pstrcat(buf
, sizeof(buf
), "_0");
329 ts
->name
= strdup(buf
);
332 ts
->base_type
= type
;
333 ts
->type
= TCG_TYPE_I32
;
336 pstrcpy(buf
, sizeof(buf
), name
);
337 pstrcat(buf
, sizeof(buf
), "_1");
338 ts
->name
= strdup(buf
);
341 return MAKE_TCGV(idx
);
345 TCGv
tcg_global_mem_new(TCGType type
, int reg
, tcg_target_long offset
,
348 TCGContext
*s
= &tcg_ctx
;
353 #if TCG_TARGET_REG_BITS == 32
354 if (type
== TCG_TYPE_I64
) {
356 tcg_temp_alloc(s
, s
->nb_globals
+ 1);
357 ts
= &s
->temps
[s
->nb_globals
];
358 ts
->base_type
= type
;
359 ts
->type
= TCG_TYPE_I32
;
361 ts
->mem_allocated
= 1;
363 #ifdef TCG_TARGET_WORDS_BIGENDIAN
364 ts
->mem_offset
= offset
+ 4;
366 ts
->mem_offset
= offset
;
368 pstrcpy(buf
, sizeof(buf
), name
);
369 pstrcat(buf
, sizeof(buf
), "_0");
370 ts
->name
= strdup(buf
);
373 ts
->base_type
= type
;
374 ts
->type
= TCG_TYPE_I32
;
376 ts
->mem_allocated
= 1;
378 #ifdef TCG_TARGET_WORDS_BIGENDIAN
379 ts
->mem_offset
= offset
;
381 ts
->mem_offset
= offset
+ 4;
383 pstrcpy(buf
, sizeof(buf
), name
);
384 pstrcat(buf
, sizeof(buf
), "_1");
385 ts
->name
= strdup(buf
);
391 tcg_temp_alloc(s
, s
->nb_globals
+ 1);
392 ts
= &s
->temps
[s
->nb_globals
];
393 ts
->base_type
= type
;
396 ts
->mem_allocated
= 1;
398 ts
->mem_offset
= offset
;
402 return MAKE_TCGV(idx
);
405 TCGv
tcg_temp_new_internal(TCGType type
, int temp_local
)
407 TCGContext
*s
= &tcg_ctx
;
414 idx
= s
->first_free_temp
[k
];
416 /* There is already an available temp with the
419 s
->first_free_temp
[k
] = ts
->next_free_temp
;
420 ts
->temp_allocated
= 1;
421 assert(ts
->temp_local
== temp_local
);
424 #if TCG_TARGET_REG_BITS == 32
425 if (type
== TCG_TYPE_I64
) {
426 tcg_temp_alloc(s
, s
->nb_temps
+ 1);
427 ts
= &s
->temps
[s
->nb_temps
];
428 ts
->base_type
= type
;
429 ts
->type
= TCG_TYPE_I32
;
430 ts
->temp_allocated
= 1;
431 ts
->temp_local
= temp_local
;
434 ts
->base_type
= TCG_TYPE_I32
;
435 ts
->type
= TCG_TYPE_I32
;
436 ts
->temp_allocated
= 1;
437 ts
->temp_local
= temp_local
;
443 tcg_temp_alloc(s
, s
->nb_temps
+ 1);
444 ts
= &s
->temps
[s
->nb_temps
];
445 ts
->base_type
= type
;
447 ts
->temp_allocated
= 1;
448 ts
->temp_local
= temp_local
;
453 return MAKE_TCGV(idx
);
456 void tcg_temp_free(TCGv arg
)
458 TCGContext
*s
= &tcg_ctx
;
460 int idx
= GET_TCGV(arg
);
463 assert(idx
>= s
->nb_globals
&& idx
< s
->nb_temps
);
465 assert(ts
->temp_allocated
!= 0);
466 ts
->temp_allocated
= 0;
470 ts
->next_free_temp
= s
->first_free_temp
[k
];
471 s
->first_free_temp
[k
] = idx
;
475 TCGv
tcg_const_i32(int32_t val
)
478 t0
= tcg_temp_new(TCG_TYPE_I32
);
479 tcg_gen_movi_i32(t0
, val
);
483 TCGv
tcg_const_i64(int64_t val
)
486 t0
= tcg_temp_new(TCG_TYPE_I64
);
487 tcg_gen_movi_i64(t0
, val
);
491 void tcg_register_helper(void *func
, const char *name
)
493 TCGContext
*s
= &tcg_ctx
;
495 if ((s
->nb_helpers
+ 1) > s
->allocated_helpers
) {
496 n
= s
->allocated_helpers
;
502 s
->helpers
= realloc(s
->helpers
, n
* sizeof(TCGHelperInfo
));
503 s
->allocated_helpers
= n
;
505 s
->helpers
[s
->nb_helpers
].func
= (tcg_target_ulong
)func
;
506 s
->helpers
[s
->nb_helpers
].name
= name
;
510 static inline TCGType
tcg_get_base_type(TCGContext
*s
, TCGv arg
)
512 return s
->temps
[GET_TCGV(arg
)].base_type
;
515 static void tcg_gen_call_internal(TCGContext
*s
, TCGv func
,
517 unsigned int nb_rets
, const TCGv
*rets
,
518 unsigned int nb_params
, const TCGv
*params
)
521 *gen_opc_ptr
++ = INDEX_op_call
;
522 *gen_opparam_ptr
++ = (nb_rets
<< 16) | (nb_params
+ 1);
523 for(i
= 0; i
< nb_rets
; i
++) {
524 *gen_opparam_ptr
++ = GET_TCGV(rets
[i
]);
526 for(i
= 0; i
< nb_params
; i
++) {
527 *gen_opparam_ptr
++ = GET_TCGV(params
[i
]);
529 *gen_opparam_ptr
++ = GET_TCGV(func
);
531 *gen_opparam_ptr
++ = flags
;
532 /* total parameters, needed to go backward in the instruction stream */
533 *gen_opparam_ptr
++ = 1 + nb_rets
+ nb_params
+ 3;
537 #if TCG_TARGET_REG_BITS < 64
538 /* Note: we convert the 64 bit args to 32 bit and do some alignment
539 and endian swap. Maybe it would be better to do the alignment
540 and endian swap in tcg_reg_alloc_call(). */
541 void tcg_gen_call(TCGContext
*s
, TCGv func
, unsigned int flags
,
542 unsigned int nb_rets
, const TCGv
*rets
,
543 unsigned int nb_params
, const TCGv
*args1
)
545 TCGv ret
, *args2
, rets_2
[2], arg
;
550 if (tcg_get_base_type(s
, ret
) == TCG_TYPE_I64
) {
552 #ifdef TCG_TARGET_WORDS_BIGENDIAN
553 rets_2
[0] = TCGV_HIGH(ret
);
557 rets_2
[1] = TCGV_HIGH(ret
);
562 args2
= alloca((nb_params
* 3) * sizeof(TCGv
));
564 call_type
= (flags
& TCG_CALL_TYPE_MASK
);
565 for(i
= 0; i
< nb_params
; i
++) {
567 if (tcg_get_base_type(s
, arg
) == TCG_TYPE_I64
) {
568 #ifdef TCG_TARGET_I386
569 /* REGPARM case: if the third parameter is 64 bit, it is
570 allocated on the stack */
571 if (j
== 2 && call_type
== TCG_CALL_TYPE_REGPARM
) {
572 call_type
= TCG_CALL_TYPE_REGPARM_2
;
573 flags
= (flags
& ~TCG_CALL_TYPE_MASK
) | call_type
;
576 args2
[j
++] = TCGV_HIGH(arg
);
578 #ifdef TCG_TARGET_CALL_ALIGN_ARGS
579 /* some targets want aligned 64 bit args */
581 args2
[j
++] = TCG_CALL_DUMMY_ARG
;
584 #ifdef TCG_TARGET_WORDS_BIGENDIAN
585 args2
[j
++] = TCGV_HIGH(arg
);
589 args2
[j
++] = TCGV_HIGH(arg
);
596 tcg_gen_call_internal(s
, func
, flags
,
597 nb_rets
, rets
, j
, args2
);
600 void tcg_gen_call(TCGContext
*s
, TCGv func
, unsigned int flags
,
601 unsigned int nb_rets
, const TCGv
*rets
,
602 unsigned int nb_params
, const TCGv
*args1
)
604 tcg_gen_call_internal(s
, func
, flags
,
605 nb_rets
, rets
, nb_params
, args1
);
609 #if TCG_TARGET_REG_BITS == 32
610 void tcg_gen_shifti_i64(TCGv ret
, TCGv arg1
,
611 int c
, int right
, int arith
)
614 tcg_gen_mov_i32(ret
, arg1
);
615 tcg_gen_mov_i32(TCGV_HIGH(ret
), TCGV_HIGH(arg1
));
616 } else if (c
>= 32) {
620 tcg_gen_sari_i32(ret
, TCGV_HIGH(arg1
), c
);
621 tcg_gen_sari_i32(TCGV_HIGH(ret
), TCGV_HIGH(arg1
), 31);
623 tcg_gen_shri_i32(ret
, TCGV_HIGH(arg1
), c
);
624 tcg_gen_movi_i32(TCGV_HIGH(ret
), 0);
627 tcg_gen_shli_i32(TCGV_HIGH(ret
), arg1
, c
);
628 tcg_gen_movi_i32(ret
, 0);
633 t0
= tcg_temp_new(TCG_TYPE_I32
);
634 t1
= tcg_temp_new(TCG_TYPE_I32
);
636 tcg_gen_shli_i32(t0
, TCGV_HIGH(arg1
), 32 - c
);
638 tcg_gen_sari_i32(t1
, TCGV_HIGH(arg1
), c
);
640 tcg_gen_shri_i32(t1
, TCGV_HIGH(arg1
), c
);
641 tcg_gen_shri_i32(ret
, arg1
, c
);
642 tcg_gen_or_i32(ret
, ret
, t0
);
643 tcg_gen_mov_i32(TCGV_HIGH(ret
), t1
);
645 tcg_gen_shri_i32(t0
, arg1
, 32 - c
);
646 /* Note: ret can be the same as arg1, so we use t1 */
647 tcg_gen_shli_i32(t1
, arg1
, c
);
648 tcg_gen_shli_i32(TCGV_HIGH(ret
), TCGV_HIGH(arg1
), c
);
649 tcg_gen_or_i32(TCGV_HIGH(ret
), TCGV_HIGH(ret
), t0
);
650 tcg_gen_mov_i32(ret
, t1
);
658 void tcg_reg_alloc_start(TCGContext
*s
)
662 for(i
= 0; i
< s
->nb_globals
; i
++) {
665 ts
->val_type
= TEMP_VAL_REG
;
667 ts
->val_type
= TEMP_VAL_MEM
;
670 for(i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
672 ts
->val_type
= TEMP_VAL_DEAD
;
673 ts
->mem_allocated
= 0;
676 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
677 s
->reg_to_temp
[i
] = -1;
681 static char *tcg_get_arg_str_idx(TCGContext
*s
, char *buf
, int buf_size
,
687 if (idx
< s
->nb_globals
) {
688 pstrcpy(buf
, buf_size
, ts
->name
);
691 snprintf(buf
, buf_size
, "loc%d", idx
- s
->nb_globals
);
693 snprintf(buf
, buf_size
, "tmp%d", idx
- s
->nb_globals
);
698 char *tcg_get_arg_str(TCGContext
*s
, char *buf
, int buf_size
, TCGv arg
)
700 return tcg_get_arg_str_idx(s
, buf
, buf_size
, GET_TCGV(arg
));
703 static int helper_cmp(const void *p1
, const void *p2
)
705 const TCGHelperInfo
*th1
= p1
;
706 const TCGHelperInfo
*th2
= p2
;
707 if (th1
->func
< th2
->func
)
709 else if (th1
->func
== th2
->func
)
715 /* find helper definition (Note: A hash table would be better) */
716 static TCGHelperInfo
*tcg_find_helper(TCGContext
*s
, tcg_target_ulong val
)
722 if (unlikely(!s
->helpers_sorted
)) {
723 qsort(s
->helpers
, s
->nb_helpers
, sizeof(TCGHelperInfo
),
725 s
->helpers_sorted
= 1;
730 m_max
= s
->nb_helpers
- 1;
731 while (m_min
<= m_max
) {
732 m
= (m_min
+ m_max
) >> 1;
746 void tcg_dump_ops(TCGContext
*s
, FILE *outfile
)
748 const uint16_t *opc_ptr
;
751 int c
, i
, k
, nb_oargs
, nb_iargs
, nb_cargs
, first_insn
;
756 opc_ptr
= gen_opc_buf
;
757 args
= gen_opparam_buf
;
758 while (opc_ptr
< gen_opc_ptr
) {
760 def
= &tcg_op_defs
[c
];
761 if (c
== INDEX_op_debug_insn_start
) {
763 #if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
764 pc
= ((uint64_t)args
[1] << 32) | args
[0];
769 fprintf(outfile
, "\n");
770 fprintf(outfile
, " ---- 0x%" PRIx64
, pc
);
772 nb_oargs
= def
->nb_oargs
;
773 nb_iargs
= def
->nb_iargs
;
774 nb_cargs
= def
->nb_cargs
;
775 } else if (c
== INDEX_op_call
) {
778 /* variable number of arguments */
780 nb_oargs
= arg
>> 16;
781 nb_iargs
= arg
& 0xffff;
782 nb_cargs
= def
->nb_cargs
;
784 fprintf(outfile
, " %s ", def
->name
);
787 fprintf(outfile
, "%s",
788 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[nb_oargs
+ nb_iargs
- 1]));
790 fprintf(outfile
, ",$0x%" TCG_PRIlx
,
791 args
[nb_oargs
+ nb_iargs
]);
793 fprintf(outfile
, ",$%d", nb_oargs
);
794 for(i
= 0; i
< nb_oargs
; i
++) {
795 fprintf(outfile
, ",");
796 fprintf(outfile
, "%s",
797 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[i
]));
799 for(i
= 0; i
< (nb_iargs
- 1); i
++) {
800 fprintf(outfile
, ",");
801 if (args
[nb_oargs
+ i
] == TCG_CALL_DUMMY_ARG
) {
802 fprintf(outfile
, "<dummy>");
804 fprintf(outfile
, "%s",
805 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[nb_oargs
+ i
]));
808 } else if (c
== INDEX_op_movi_i32
809 #if TCG_TARGET_REG_BITS == 64
810 || c
== INDEX_op_movi_i64
813 tcg_target_ulong val
;
816 nb_oargs
= def
->nb_oargs
;
817 nb_iargs
= def
->nb_iargs
;
818 nb_cargs
= def
->nb_cargs
;
819 fprintf(outfile
, " %s %s,$", def
->name
,
820 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[0]));
822 th
= tcg_find_helper(s
, val
);
824 fprintf(outfile
, th
->name
);
826 if (c
== INDEX_op_movi_i32
)
827 fprintf(outfile
, "0x%x", (uint32_t)val
);
829 fprintf(outfile
, "0x%" PRIx64
, (uint64_t)val
);
832 fprintf(outfile
, " %s ", def
->name
);
833 if (c
== INDEX_op_nopn
) {
834 /* variable number of arguments */
839 nb_oargs
= def
->nb_oargs
;
840 nb_iargs
= def
->nb_iargs
;
841 nb_cargs
= def
->nb_cargs
;
845 for(i
= 0; i
< nb_oargs
; i
++) {
847 fprintf(outfile
, ",");
848 fprintf(outfile
, "%s",
849 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[k
++]));
851 for(i
= 0; i
< nb_iargs
; i
++) {
853 fprintf(outfile
, ",");
854 fprintf(outfile
, "%s",
855 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), args
[k
++]));
857 for(i
= 0; i
< nb_cargs
; i
++) {
859 fprintf(outfile
, ",");
861 fprintf(outfile
, "$0x%" TCG_PRIlx
, arg
);
864 fprintf(outfile
, "\n");
865 args
+= nb_iargs
+ nb_oargs
+ nb_cargs
;
869 /* we give more priority to constraints with less registers */
870 static int get_constraint_priority(const TCGOpDef
*def
, int k
)
872 const TCGArgConstraint
*arg_ct
;
875 arg_ct
= &def
->args_ct
[k
];
876 if (arg_ct
->ct
& TCG_CT_ALIAS
) {
877 /* an alias is equivalent to a single register */
880 if (!(arg_ct
->ct
& TCG_CT_REG
))
883 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
884 if (tcg_regset_test_reg(arg_ct
->u
.regs
, i
))
888 return TCG_TARGET_NB_REGS
- n
+ 1;
891 /* sort from highest priority to lowest */
892 static void sort_constraints(TCGOpDef
*def
, int start
, int n
)
894 int i
, j
, p1
, p2
, tmp
;
896 for(i
= 0; i
< n
; i
++)
897 def
->sorted_args
[start
+ i
] = start
+ i
;
900 for(i
= 0; i
< n
- 1; i
++) {
901 for(j
= i
+ 1; j
< n
; j
++) {
902 p1
= get_constraint_priority(def
, def
->sorted_args
[start
+ i
]);
903 p2
= get_constraint_priority(def
, def
->sorted_args
[start
+ j
]);
905 tmp
= def
->sorted_args
[start
+ i
];
906 def
->sorted_args
[start
+ i
] = def
->sorted_args
[start
+ j
];
907 def
->sorted_args
[start
+ j
] = tmp
;
913 void tcg_add_target_add_op_defs(const TCGTargetOpDef
*tdefs
)
924 assert(op
>= 0 && op
< NB_OPS
);
925 def
= &tcg_op_defs
[op
];
926 nb_args
= def
->nb_iargs
+ def
->nb_oargs
;
927 for(i
= 0; i
< nb_args
; i
++) {
928 ct_str
= tdefs
->args_ct_str
[i
];
929 tcg_regset_clear(def
->args_ct
[i
].u
.regs
);
930 def
->args_ct
[i
].ct
= 0;
931 if (ct_str
[0] >= '0' && ct_str
[0] <= '9') {
933 oarg
= ct_str
[0] - '0';
934 assert(oarg
< def
->nb_oargs
);
935 assert(def
->args_ct
[oarg
].ct
& TCG_CT_REG
);
936 /* TCG_CT_ALIAS is for the output arguments. The input
937 argument is tagged with TCG_CT_IALIAS. */
938 def
->args_ct
[i
] = def
->args_ct
[oarg
];
939 def
->args_ct
[oarg
].ct
= TCG_CT_ALIAS
;
940 def
->args_ct
[oarg
].alias_index
= i
;
941 def
->args_ct
[i
].ct
|= TCG_CT_IALIAS
;
942 def
->args_ct
[i
].alias_index
= oarg
;
949 def
->args_ct
[i
].ct
|= TCG_CT_CONST
;
953 if (target_parse_constraint(&def
->args_ct
[i
], &ct_str
) < 0) {
954 fprintf(stderr
, "Invalid constraint '%s' for arg %d of operation '%s'\n",
955 ct_str
, i
, def
->name
);
963 /* sort the constraints (XXX: this is just an heuristic) */
964 sort_constraints(def
, 0, def
->nb_oargs
);
965 sort_constraints(def
, def
->nb_oargs
, def
->nb_iargs
);
971 printf("%s: sorted=", def
->name
);
972 for(i
= 0; i
< def
->nb_oargs
+ def
->nb_iargs
; i
++)
973 printf(" %d", def
->sorted_args
[i
]);
982 #ifdef USE_LIVENESS_ANALYSIS
984 /* set a nop for an operation using 'nb_args' */
985 static inline void tcg_set_nop(TCGContext
*s
, uint16_t *opc_ptr
,
986 TCGArg
*args
, int nb_args
)
989 *opc_ptr
= INDEX_op_nop
;
991 *opc_ptr
= INDEX_op_nopn
;
993 args
[nb_args
- 1] = nb_args
;
997 /* liveness analysis: end of function: globals are live, temps are
999 /* XXX: at this stage, not used as there would be little gains because
1000 most TBs end with a conditional jump. */
1001 static inline void tcg_la_func_end(TCGContext
*s
, uint8_t *dead_temps
)
1003 memset(dead_temps
, 0, s
->nb_globals
);
1004 memset(dead_temps
+ s
->nb_globals
, 1, s
->nb_temps
- s
->nb_globals
);
1007 /* liveness analysis: end of basic block: globals are live, temps are
1008 dead, local temps are live. */
1009 static inline void tcg_la_bb_end(TCGContext
*s
, uint8_t *dead_temps
)
1014 memset(dead_temps
, 0, s
->nb_globals
);
1015 ts
= &s
->temps
[s
->nb_globals
];
1016 for(i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
1025 /* Liveness analysis : update the opc_dead_iargs array to tell if a
1026 given input arguments is dead. Instructions updating dead
1027 temporaries are removed. */
1028 void tcg_liveness_analysis(TCGContext
*s
)
1030 int i
, op_index
, op
, nb_args
, nb_iargs
, nb_oargs
, arg
, nb_ops
;
1032 const TCGOpDef
*def
;
1033 uint8_t *dead_temps
;
1034 unsigned int dead_iargs
;
1036 gen_opc_ptr
++; /* skip end */
1038 nb_ops
= gen_opc_ptr
- gen_opc_buf
;
1040 /* XXX: make it really dynamic */
1041 s
->op_dead_iargs
= tcg_malloc(OPC_BUF_SIZE
* sizeof(uint16_t));
1043 dead_temps
= tcg_malloc(s
->nb_temps
);
1044 memset(dead_temps
, 1, s
->nb_temps
);
1046 args
= gen_opparam_ptr
;
1047 op_index
= nb_ops
- 1;
1048 while (op_index
>= 0) {
1049 op
= gen_opc_buf
[op_index
];
1050 def
= &tcg_op_defs
[op
];
1058 nb_iargs
= args
[0] & 0xffff;
1059 nb_oargs
= args
[0] >> 16;
1061 call_flags
= args
[nb_oargs
+ nb_iargs
];
1063 /* pure functions can be removed if their result is not
1065 if (call_flags
& TCG_CALL_PURE
) {
1066 for(i
= 0; i
< nb_oargs
; i
++) {
1068 if (!dead_temps
[arg
])
1069 goto do_not_remove_call
;
1071 tcg_set_nop(s
, gen_opc_buf
+ op_index
,
1076 /* output args are dead */
1077 for(i
= 0; i
< nb_oargs
; i
++) {
1079 dead_temps
[arg
] = 1;
1082 /* globals are live (they may be used by the call) */
1083 memset(dead_temps
, 0, s
->nb_globals
);
1085 /* input args are live */
1087 for(i
= 0; i
< nb_iargs
; i
++) {
1088 arg
= args
[i
+ nb_oargs
];
1089 if (arg
!= TCG_CALL_DUMMY_ARG
) {
1090 if (dead_temps
[arg
]) {
1091 dead_iargs
|= (1 << i
);
1093 dead_temps
[arg
] = 0;
1096 s
->op_dead_iargs
[op_index
] = dead_iargs
;
1101 case INDEX_op_set_label
:
1103 /* mark end of basic block */
1104 tcg_la_bb_end(s
, dead_temps
);
1106 case INDEX_op_debug_insn_start
:
1107 args
-= def
->nb_args
;
1113 case INDEX_op_discard
:
1115 /* mark the temporary as dead */
1116 dead_temps
[args
[0]] = 1;
1120 /* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
1122 if (op
> INDEX_op_end
) {
1123 args
-= def
->nb_args
;
1124 nb_iargs
= def
->nb_iargs
;
1125 nb_oargs
= def
->nb_oargs
;
1127 /* Test if the operation can be removed because all
1128 its outputs are dead. We assume that nb_oargs == 0
1129 implies side effects */
1130 if (!(def
->flags
& TCG_OPF_SIDE_EFFECTS
) && nb_oargs
!= 0) {
1131 for(i
= 0; i
< nb_oargs
; i
++) {
1133 if (!dead_temps
[arg
])
1136 tcg_set_nop(s
, gen_opc_buf
+ op_index
, args
, def
->nb_args
);
1137 #ifdef CONFIG_PROFILER
1143 /* output args are dead */
1144 for(i
= 0; i
< nb_oargs
; i
++) {
1146 dead_temps
[arg
] = 1;
1149 /* if end of basic block, update */
1150 if (def
->flags
& TCG_OPF_BB_END
) {
1151 tcg_la_bb_end(s
, dead_temps
);
1152 } else if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
1153 /* globals are live */
1154 memset(dead_temps
, 0, s
->nb_globals
);
1157 /* input args are live */
1159 for(i
= 0; i
< nb_iargs
; i
++) {
1160 arg
= args
[i
+ nb_oargs
];
1161 if (dead_temps
[arg
]) {
1162 dead_iargs
|= (1 << i
);
1164 dead_temps
[arg
] = 0;
1166 s
->op_dead_iargs
[op_index
] = dead_iargs
;
1169 /* legacy dyngen operations */
1170 args
-= def
->nb_args
;
1171 /* mark end of basic block */
1172 tcg_la_bb_end(s
, dead_temps
);
1179 if (args
!= gen_opparam_buf
)
1183 /* dummy liveness analysis */
1184 void tcg_liveness_analysis(TCGContext
*s
)
1187 nb_ops
= gen_opc_ptr
- gen_opc_buf
;
1189 s
->op_dead_iargs
= tcg_malloc(nb_ops
* sizeof(uint16_t));
1190 memset(s
->op_dead_iargs
, 0, nb_ops
* sizeof(uint16_t));
1195 static void dump_regs(TCGContext
*s
)
1201 for(i
= 0; i
< s
->nb_temps
; i
++) {
1203 printf(" %10s: ", tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), i
));
1204 switch(ts
->val_type
) {
1206 printf("%s", tcg_target_reg_names
[ts
->reg
]);
1209 printf("%d(%s)", (int)ts
->mem_offset
, tcg_target_reg_names
[ts
->mem_reg
]);
1211 case TEMP_VAL_CONST
:
1212 printf("$0x%" TCG_PRIlx
, ts
->val
);
1224 for(i
= 0; i
< TCG_TARGET_NB_REGS
; i
++) {
1225 if (s
->reg_to_temp
[i
] >= 0) {
1227 tcg_target_reg_names
[i
],
1228 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), s
->reg_to_temp
[i
]));
1233 static void check_regs(TCGContext
*s
)
1239 for(reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
1240 k
= s
->reg_to_temp
[reg
];
1243 if (ts
->val_type
!= TEMP_VAL_REG
||
1245 printf("Inconsistency for register %s:\n",
1246 tcg_target_reg_names
[reg
]);
1251 for(k
= 0; k
< s
->nb_temps
; k
++) {
1253 if (ts
->val_type
== TEMP_VAL_REG
&&
1255 s
->reg_to_temp
[ts
->reg
] != k
) {
1256 printf("Inconsistency for temp %s:\n",
1257 tcg_get_arg_str_idx(s
, buf
, sizeof(buf
), k
));
1259 printf("reg state:\n");
1267 static void temp_allocate_frame(TCGContext
*s
, int temp
)
1270 ts
= &s
->temps
[temp
];
1271 s
->current_frame_offset
= (s
->current_frame_offset
+ sizeof(tcg_target_long
) - 1) & ~(sizeof(tcg_target_long
) - 1);
1272 if (s
->current_frame_offset
+ sizeof(tcg_target_long
) > s
->frame_end
)
1274 ts
->mem_offset
= s
->current_frame_offset
;
1275 ts
->mem_reg
= s
->frame_reg
;
1276 ts
->mem_allocated
= 1;
1277 s
->current_frame_offset
+= sizeof(tcg_target_long
);
1280 /* free register 'reg' by spilling the corresponding temporary if necessary */
1281 static void tcg_reg_free(TCGContext
*s
, int reg
)
1286 temp
= s
->reg_to_temp
[reg
];
1288 ts
= &s
->temps
[temp
];
1289 assert(ts
->val_type
== TEMP_VAL_REG
);
1290 if (!ts
->mem_coherent
) {
1291 if (!ts
->mem_allocated
)
1292 temp_allocate_frame(s
, temp
);
1293 tcg_out_st(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1295 ts
->val_type
= TEMP_VAL_MEM
;
1296 s
->reg_to_temp
[reg
] = -1;
1300 /* Allocate a register belonging to reg1 & ~reg2 */
1301 static int tcg_reg_alloc(TCGContext
*s
, TCGRegSet reg1
, TCGRegSet reg2
)
1306 tcg_regset_andnot(reg_ct
, reg1
, reg2
);
1308 /* first try free registers */
1309 for(i
= 0; i
< ARRAY_SIZE(tcg_target_reg_alloc_order
); i
++) {
1310 reg
= tcg_target_reg_alloc_order
[i
];
1311 if (tcg_regset_test_reg(reg_ct
, reg
) && s
->reg_to_temp
[reg
] == -1)
1315 /* XXX: do better spill choice */
1316 for(i
= 0; i
< ARRAY_SIZE(tcg_target_reg_alloc_order
); i
++) {
1317 reg
= tcg_target_reg_alloc_order
[i
];
1318 if (tcg_regset_test_reg(reg_ct
, reg
)) {
1319 tcg_reg_free(s
, reg
);
1327 /* save a temporary to memory. 'allocated_regs' is used in case a
1328 temporary registers needs to be allocated to store a constant. */
1329 static void temp_save(TCGContext
*s
, int temp
, TCGRegSet allocated_regs
)
1334 ts
= &s
->temps
[temp
];
1335 if (!ts
->fixed_reg
) {
1336 switch(ts
->val_type
) {
1338 tcg_reg_free(s
, ts
->reg
);
1341 ts
->val_type
= TEMP_VAL_MEM
;
1343 case TEMP_VAL_CONST
:
1344 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
1346 if (!ts
->mem_allocated
)
1347 temp_allocate_frame(s
, temp
);
1348 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1349 tcg_out_st(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1350 ts
->val_type
= TEMP_VAL_MEM
;
1360 /* save globals to their cannonical location and assume they can be
1361 modified be the following code. 'allocated_regs' is used in case a
1362 temporary registers needs to be allocated to store a constant. */
1363 static void save_globals(TCGContext
*s
, TCGRegSet allocated_regs
)
1367 for(i
= 0; i
< s
->nb_globals
; i
++) {
1368 temp_save(s
, i
, allocated_regs
);
1372 /* at the end of a basic block, we assume all temporaries are dead and
1373 all globals are stored at their canonical location. */
1374 static void tcg_reg_alloc_bb_end(TCGContext
*s
, TCGRegSet allocated_regs
)
1379 for(i
= s
->nb_globals
; i
< s
->nb_temps
; i
++) {
1381 if (ts
->temp_local
) {
1382 temp_save(s
, i
, allocated_regs
);
1384 if (ts
->val_type
== TEMP_VAL_REG
) {
1385 s
->reg_to_temp
[ts
->reg
] = -1;
1387 ts
->val_type
= TEMP_VAL_DEAD
;
1391 save_globals(s
, allocated_regs
);
1394 #define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1)
1396 static void tcg_reg_alloc_movi(TCGContext
*s
, const TCGArg
*args
)
1399 tcg_target_ulong val
;
1401 ots
= &s
->temps
[args
[0]];
1404 if (ots
->fixed_reg
) {
1405 /* for fixed registers, we do not do any constant
1407 tcg_out_movi(s
, ots
->type
, ots
->reg
, val
);
1409 /* The movi is not explicitely generated here */
1410 if (ots
->val_type
== TEMP_VAL_REG
)
1411 s
->reg_to_temp
[ots
->reg
] = -1;
1412 ots
->val_type
= TEMP_VAL_CONST
;
1417 static void tcg_reg_alloc_mov(TCGContext
*s
, const TCGOpDef
*def
,
1419 unsigned int dead_iargs
)
1423 const TCGArgConstraint
*arg_ct
;
1425 ots
= &s
->temps
[args
[0]];
1426 ts
= &s
->temps
[args
[1]];
1427 arg_ct
= &def
->args_ct
[0];
1429 /* XXX: always mark arg dead if IS_DEAD_IARG(0) */
1430 if (ts
->val_type
== TEMP_VAL_REG
) {
1431 if (IS_DEAD_IARG(0) && !ts
->fixed_reg
&& !ots
->fixed_reg
) {
1432 /* the mov can be suppressed */
1433 if (ots
->val_type
== TEMP_VAL_REG
)
1434 s
->reg_to_temp
[ots
->reg
] = -1;
1436 s
->reg_to_temp
[reg
] = -1;
1437 ts
->val_type
= TEMP_VAL_DEAD
;
1439 if (ots
->val_type
== TEMP_VAL_REG
) {
1442 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, s
->reserved_regs
);
1444 if (ts
->reg
!= reg
) {
1445 tcg_out_mov(s
, reg
, ts
->reg
);
1448 } else if (ts
->val_type
== TEMP_VAL_MEM
) {
1449 if (ots
->val_type
== TEMP_VAL_REG
) {
1452 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, s
->reserved_regs
);
1454 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1455 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1456 if (ots
->fixed_reg
) {
1458 tcg_out_movi(s
, ots
->type
, reg
, ts
->val
);
1460 /* propagate constant */
1461 if (ots
->val_type
== TEMP_VAL_REG
)
1462 s
->reg_to_temp
[ots
->reg
] = -1;
1463 ots
->val_type
= TEMP_VAL_CONST
;
1470 s
->reg_to_temp
[reg
] = args
[0];
1472 ots
->val_type
= TEMP_VAL_REG
;
1473 ots
->mem_coherent
= 0;
1476 static void tcg_reg_alloc_op(TCGContext
*s
,
1477 const TCGOpDef
*def
, int opc
,
1479 unsigned int dead_iargs
)
1481 TCGRegSet allocated_regs
;
1482 int i
, k
, nb_iargs
, nb_oargs
, reg
;
1484 const TCGArgConstraint
*arg_ct
;
1486 TCGArg new_args
[TCG_MAX_OP_ARGS
];
1487 int const_args
[TCG_MAX_OP_ARGS
];
1489 nb_oargs
= def
->nb_oargs
;
1490 nb_iargs
= def
->nb_iargs
;
1492 /* copy constants */
1493 memcpy(new_args
+ nb_oargs
+ nb_iargs
,
1494 args
+ nb_oargs
+ nb_iargs
,
1495 sizeof(TCGArg
) * def
->nb_cargs
);
1497 /* satisfy input constraints */
1498 tcg_regset_set(allocated_regs
, s
->reserved_regs
);
1499 for(k
= 0; k
< nb_iargs
; k
++) {
1500 i
= def
->sorted_args
[nb_oargs
+ k
];
1502 arg_ct
= &def
->args_ct
[i
];
1503 ts
= &s
->temps
[arg
];
1504 if (ts
->val_type
== TEMP_VAL_MEM
) {
1505 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1506 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1507 ts
->val_type
= TEMP_VAL_REG
;
1509 ts
->mem_coherent
= 1;
1510 s
->reg_to_temp
[reg
] = arg
;
1511 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1512 if (tcg_target_const_match(ts
->val
, arg_ct
)) {
1513 /* constant is OK for instruction */
1515 new_args
[i
] = ts
->val
;
1518 /* need to move to a register */
1519 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1520 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1521 ts
->val_type
= TEMP_VAL_REG
;
1523 ts
->mem_coherent
= 0;
1524 s
->reg_to_temp
[reg
] = arg
;
1527 assert(ts
->val_type
== TEMP_VAL_REG
);
1528 if (arg_ct
->ct
& TCG_CT_IALIAS
) {
1529 if (ts
->fixed_reg
) {
1530 /* if fixed register, we must allocate a new register
1531 if the alias is not the same register */
1532 if (arg
!= args
[arg_ct
->alias_index
])
1533 goto allocate_in_reg
;
1535 /* if the input is aliased to an output and if it is
1536 not dead after the instruction, we must allocate
1537 a new register and move it */
1538 if (!IS_DEAD_IARG(i
- nb_oargs
))
1539 goto allocate_in_reg
;
1543 if (tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
1544 /* nothing to do : the constraint is satisfied */
1547 /* allocate a new register matching the constraint
1548 and move the temporary register into it */
1549 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1550 tcg_out_mov(s
, reg
, ts
->reg
);
1554 tcg_regset_set_reg(allocated_regs
, reg
);
1558 if (def
->flags
& TCG_OPF_BB_END
) {
1559 tcg_reg_alloc_bb_end(s
, allocated_regs
);
1561 /* mark dead temporaries and free the associated registers */
1562 for(i
= 0; i
< nb_iargs
; i
++) {
1563 arg
= args
[nb_oargs
+ i
];
1564 if (IS_DEAD_IARG(i
)) {
1565 ts
= &s
->temps
[arg
];
1566 if (!ts
->fixed_reg
) {
1567 if (ts
->val_type
== TEMP_VAL_REG
)
1568 s
->reg_to_temp
[ts
->reg
] = -1;
1569 ts
->val_type
= TEMP_VAL_DEAD
;
1574 if (def
->flags
& TCG_OPF_CALL_CLOBBER
) {
1575 /* XXX: permit generic clobber register list ? */
1576 for(reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
1577 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, reg
)) {
1578 tcg_reg_free(s
, reg
);
1581 /* XXX: for load/store we could do that only for the slow path
1582 (i.e. when a memory callback is called) */
1584 /* store globals and free associated registers (we assume the insn
1585 can modify any global. */
1586 save_globals(s
, allocated_regs
);
1589 /* satisfy the output constraints */
1590 tcg_regset_set(allocated_regs
, s
->reserved_regs
);
1591 for(k
= 0; k
< nb_oargs
; k
++) {
1592 i
= def
->sorted_args
[k
];
1594 arg_ct
= &def
->args_ct
[i
];
1595 ts
= &s
->temps
[arg
];
1596 if (arg_ct
->ct
& TCG_CT_ALIAS
) {
1597 reg
= new_args
[arg_ct
->alias_index
];
1599 /* if fixed register, we try to use it */
1601 if (ts
->fixed_reg
&&
1602 tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
1605 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1607 tcg_regset_set_reg(allocated_regs
, reg
);
1608 /* if a fixed register is used, then a move will be done afterwards */
1609 if (!ts
->fixed_reg
) {
1610 if (ts
->val_type
== TEMP_VAL_REG
)
1611 s
->reg_to_temp
[ts
->reg
] = -1;
1612 ts
->val_type
= TEMP_VAL_REG
;
1614 /* temp value is modified, so the value kept in memory is
1615 potentially not the same */
1616 ts
->mem_coherent
= 0;
1617 s
->reg_to_temp
[reg
] = arg
;
1624 /* emit instruction */
1625 tcg_out_op(s
, opc
, new_args
, const_args
);
1627 /* move the outputs in the correct register if needed */
1628 for(i
= 0; i
< nb_oargs
; i
++) {
1629 ts
= &s
->temps
[args
[i
]];
1631 if (ts
->fixed_reg
&& ts
->reg
!= reg
) {
1632 tcg_out_mov(s
, ts
->reg
, reg
);
1637 #ifdef TCG_TARGET_STACK_GROWSUP
1638 #define STACK_DIR(x) (-(x))
1640 #define STACK_DIR(x) (x)
1643 static int tcg_reg_alloc_call(TCGContext
*s
, const TCGOpDef
*def
,
1644 int opc
, const TCGArg
*args
,
1645 unsigned int dead_iargs
)
1647 int nb_iargs
, nb_oargs
, flags
, nb_regs
, i
, reg
, nb_params
;
1648 TCGArg arg
, func_arg
;
1650 tcg_target_long stack_offset
, call_stack_size
, func_addr
;
1651 int const_func_arg
, allocate_args
;
1652 TCGRegSet allocated_regs
;
1653 const TCGArgConstraint
*arg_ct
;
1657 nb_oargs
= arg
>> 16;
1658 nb_iargs
= arg
& 0xffff;
1659 nb_params
= nb_iargs
- 1;
1661 flags
= args
[nb_oargs
+ nb_iargs
];
1663 nb_regs
= tcg_target_get_call_iarg_regs_count(flags
);
1664 if (nb_regs
> nb_params
)
1665 nb_regs
= nb_params
;
1667 /* assign stack slots first */
1668 /* XXX: preallocate call stack */
1669 call_stack_size
= (nb_params
- nb_regs
) * sizeof(tcg_target_long
);
1670 call_stack_size
= (call_stack_size
+ TCG_TARGET_STACK_ALIGN
- 1) &
1671 ~(TCG_TARGET_STACK_ALIGN
- 1);
1672 allocate_args
= (call_stack_size
> TCG_STATIC_CALL_ARGS_SIZE
);
1673 if (allocate_args
) {
1674 tcg_out_addi(s
, TCG_REG_CALL_STACK
, -STACK_DIR(call_stack_size
));
1677 stack_offset
= TCG_TARGET_CALL_STACK_OFFSET
;
1678 for(i
= nb_regs
; i
< nb_params
; i
++) {
1679 arg
= args
[nb_oargs
+ i
];
1680 #ifdef TCG_TARGET_STACK_GROWSUP
1681 stack_offset
-= sizeof(tcg_target_long
);
1683 if (arg
!= TCG_CALL_DUMMY_ARG
) {
1684 ts
= &s
->temps
[arg
];
1685 if (ts
->val_type
== TEMP_VAL_REG
) {
1686 tcg_out_st(s
, ts
->type
, ts
->reg
, TCG_REG_CALL_STACK
, stack_offset
);
1687 } else if (ts
->val_type
== TEMP_VAL_MEM
) {
1688 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
1690 /* XXX: not correct if reading values from the stack */
1691 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1692 tcg_out_st(s
, ts
->type
, reg
, TCG_REG_CALL_STACK
, stack_offset
);
1693 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1694 reg
= tcg_reg_alloc(s
, tcg_target_available_regs
[ts
->type
],
1696 /* XXX: sign extend may be needed on some targets */
1697 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1698 tcg_out_st(s
, ts
->type
, reg
, TCG_REG_CALL_STACK
, stack_offset
);
1703 #ifndef TCG_TARGET_STACK_GROWSUP
1704 stack_offset
+= sizeof(tcg_target_long
);
1708 /* assign input registers */
1709 tcg_regset_set(allocated_regs
, s
->reserved_regs
);
1710 for(i
= 0; i
< nb_regs
; i
++) {
1711 arg
= args
[nb_oargs
+ i
];
1712 if (arg
!= TCG_CALL_DUMMY_ARG
) {
1713 ts
= &s
->temps
[arg
];
1714 reg
= tcg_target_call_iarg_regs
[i
];
1715 tcg_reg_free(s
, reg
);
1716 if (ts
->val_type
== TEMP_VAL_REG
) {
1717 if (ts
->reg
!= reg
) {
1718 tcg_out_mov(s
, reg
, ts
->reg
);
1720 } else if (ts
->val_type
== TEMP_VAL_MEM
) {
1721 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1722 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1723 /* XXX: sign extend ? */
1724 tcg_out_movi(s
, ts
->type
, reg
, ts
->val
);
1728 tcg_regset_set_reg(allocated_regs
, reg
);
1732 /* assign function address */
1733 func_arg
= args
[nb_oargs
+ nb_iargs
- 1];
1734 arg_ct
= &def
->args_ct
[0];
1735 ts
= &s
->temps
[func_arg
];
1736 func_addr
= ts
->val
;
1738 if (ts
->val_type
== TEMP_VAL_MEM
) {
1739 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1740 tcg_out_ld(s
, ts
->type
, reg
, ts
->mem_reg
, ts
->mem_offset
);
1742 tcg_regset_set_reg(allocated_regs
, reg
);
1743 } else if (ts
->val_type
== TEMP_VAL_REG
) {
1745 if (!tcg_regset_test_reg(arg_ct
->u
.regs
, reg
)) {
1746 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1747 tcg_out_mov(s
, reg
, ts
->reg
);
1750 tcg_regset_set_reg(allocated_regs
, reg
);
1751 } else if (ts
->val_type
== TEMP_VAL_CONST
) {
1752 if (tcg_target_const_match(func_addr
, arg_ct
)) {
1754 func_arg
= func_addr
;
1756 reg
= tcg_reg_alloc(s
, arg_ct
->u
.regs
, allocated_regs
);
1757 tcg_out_movi(s
, ts
->type
, reg
, func_addr
);
1759 tcg_regset_set_reg(allocated_regs
, reg
);
1766 /* mark dead temporaries and free the associated registers */
1767 for(i
= 0; i
< nb_iargs
; i
++) {
1768 arg
= args
[nb_oargs
+ i
];
1769 if (IS_DEAD_IARG(i
)) {
1770 ts
= &s
->temps
[arg
];
1771 if (!ts
->fixed_reg
) {
1772 if (ts
->val_type
== TEMP_VAL_REG
)
1773 s
->reg_to_temp
[ts
->reg
] = -1;
1774 ts
->val_type
= TEMP_VAL_DEAD
;
1779 /* clobber call registers */
1780 for(reg
= 0; reg
< TCG_TARGET_NB_REGS
; reg
++) {
1781 if (tcg_regset_test_reg(tcg_target_call_clobber_regs
, reg
)) {
1782 tcg_reg_free(s
, reg
);
1786 /* store globals and free associated registers (we assume the call
1787 can modify any global. */
1788 save_globals(s
, allocated_regs
);
1790 tcg_out_op(s
, opc
, &func_arg
, &const_func_arg
);
1792 if (allocate_args
) {
1793 tcg_out_addi(s
, TCG_REG_CALL_STACK
, STACK_DIR(call_stack_size
));
1796 /* assign output registers and emit moves if needed */
1797 for(i
= 0; i
< nb_oargs
; i
++) {
1799 ts
= &s
->temps
[arg
];
1800 reg
= tcg_target_call_oarg_regs
[i
];
1801 assert(s
->reg_to_temp
[reg
] == -1);
1802 if (ts
->fixed_reg
) {
1803 if (ts
->reg
!= reg
) {
1804 tcg_out_mov(s
, ts
->reg
, reg
);
1807 if (ts
->val_type
== TEMP_VAL_REG
)
1808 s
->reg_to_temp
[ts
->reg
] = -1;
1809 ts
->val_type
= TEMP_VAL_REG
;
1811 ts
->mem_coherent
= 0;
1812 s
->reg_to_temp
[reg
] = arg
;
1816 return nb_iargs
+ nb_oargs
+ def
->nb_cargs
+ 1;
1819 #ifdef CONFIG_PROFILER
1821 static int64_t dyngen_table_op_count
[NB_OPS
];
1823 void dump_op_count(void)
1827 f
= fopen("/tmp/op1.log", "w");
1828 for(i
= 0; i
< INDEX_op_end
; i
++) {
1829 fprintf(f
, "%s %" PRId64
"\n", tcg_op_defs
[i
].name
, dyngen_table_op_count
[i
]);
1832 f
= fopen("/tmp/op2.log", "w");
1833 for(i
= INDEX_op_end
; i
< NB_OPS
; i
++) {
1834 fprintf(f
, "%s %" PRId64
"\n", tcg_op_defs
[i
].name
, dyngen_table_op_count
[i
]);
1841 static inline int tcg_gen_code_common(TCGContext
*s
, uint8_t *gen_code_buf
,
1845 const TCGOpDef
*def
;
1846 unsigned int dead_iargs
;
1850 if (unlikely(loglevel
& CPU_LOG_TB_OP
)) {
1851 fprintf(logfile
, "OP:\n");
1852 tcg_dump_ops(s
, logfile
);
1853 fprintf(logfile
, "\n");
1857 #ifdef CONFIG_PROFILER
1858 s
->la_time
-= profile_getclock();
1860 tcg_liveness_analysis(s
);
1861 #ifdef CONFIG_PROFILER
1862 s
->la_time
+= profile_getclock();
1866 if (unlikely(loglevel
& CPU_LOG_TB_OP_OPT
)) {
1867 fprintf(logfile
, "OP after la:\n");
1868 tcg_dump_ops(s
, logfile
);
1869 fprintf(logfile
, "\n");
1873 tcg_reg_alloc_start(s
);
1875 s
->code_buf
= gen_code_buf
;
1876 s
->code_ptr
= gen_code_buf
;
1878 args
= gen_opparam_buf
;
1882 opc
= gen_opc_buf
[op_index
];
1883 #ifdef CONFIG_PROFILER
1884 dyngen_table_op_count
[opc
]++;
1886 def
= &tcg_op_defs
[opc
];
1888 printf("%s: %d %d %d\n", def
->name
,
1889 def
->nb_oargs
, def
->nb_iargs
, def
->nb_cargs
);
1893 case INDEX_op_mov_i32
:
1894 #if TCG_TARGET_REG_BITS == 64
1895 case INDEX_op_mov_i64
:
1897 dead_iargs
= s
->op_dead_iargs
[op_index
];
1898 tcg_reg_alloc_mov(s
, def
, args
, dead_iargs
);
1900 case INDEX_op_movi_i32
:
1901 #if TCG_TARGET_REG_BITS == 64
1902 case INDEX_op_movi_i64
:
1904 tcg_reg_alloc_movi(s
, args
);
1906 case INDEX_op_debug_insn_start
:
1907 /* debug instruction */
1917 case INDEX_op_discard
:
1920 ts
= &s
->temps
[args
[0]];
1921 /* mark the temporary as dead */
1922 if (!ts
->fixed_reg
) {
1923 if (ts
->val_type
== TEMP_VAL_REG
)
1924 s
->reg_to_temp
[ts
->reg
] = -1;
1925 ts
->val_type
= TEMP_VAL_DEAD
;
1929 case INDEX_op_set_label
:
1930 tcg_reg_alloc_bb_end(s
, s
->reserved_regs
);
1931 tcg_out_label(s
, args
[0], (long)s
->code_ptr
);
1934 dead_iargs
= s
->op_dead_iargs
[op_index
];
1935 args
+= tcg_reg_alloc_call(s
, def
, opc
, args
, dead_iargs
);
1940 #ifdef CONFIG_DYNGEN_OP
1941 case 0 ... INDEX_op_end
- 1:
1942 /* legacy dyngen ops */
1943 #ifdef CONFIG_PROFILER
1946 tcg_reg_alloc_bb_end(s
, s
->reserved_regs
);
1947 if (search_pc
>= 0) {
1948 s
->code_ptr
+= def
->copy_size
;
1949 args
+= def
->nb_args
;
1951 args
= dyngen_op(s
, opc
, args
);
1956 /* Note: in order to speed up the code, it would be much
1957 faster to have specialized register allocator functions for
1958 some common argument patterns */
1959 dead_iargs
= s
->op_dead_iargs
[op_index
];
1960 tcg_reg_alloc_op(s
, def
, opc
, args
, dead_iargs
);
1963 args
+= def
->nb_args
;
1965 if (search_pc
>= 0 && search_pc
< s
->code_ptr
- gen_code_buf
) {
1977 int dyngen_code(TCGContext
*s
, uint8_t *gen_code_buf
)
1979 #ifdef CONFIG_PROFILER
1982 n
= (gen_opc_ptr
- gen_opc_buf
);
1984 if (n
> s
->op_count_max
)
1985 s
->op_count_max
= n
;
1987 s
->temp_count
+= s
->nb_temps
;
1988 if (s
->nb_temps
> s
->temp_count_max
)
1989 s
->temp_count_max
= s
->nb_temps
;
1993 tcg_gen_code_common(s
, gen_code_buf
, -1);
1995 /* flush instruction cache */
1996 flush_icache_range((unsigned long)gen_code_buf
,
1997 (unsigned long)s
->code_ptr
);
1998 return s
->code_ptr
- gen_code_buf
;
2001 /* Return the index of the micro operation such as the pc after is <
2002 offset bytes from the start of the TB. The contents of gen_code_buf must
2003 not be changed, though writing the same values is ok.
2004 Return -1 if not found. */
2005 int dyngen_code_search_pc(TCGContext
*s
, uint8_t *gen_code_buf
, long offset
)
2007 return tcg_gen_code_common(s
, gen_code_buf
, offset
);
2010 #ifdef CONFIG_PROFILER
2011 void tcg_dump_info(FILE *f
,
2012 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
2014 TCGContext
*s
= &tcg_ctx
;
2017 tot
= s
->interm_time
+ s
->code_time
;
2018 cpu_fprintf(f
, "JIT cycles %" PRId64
" (%0.3f s at 2.4 GHz)\n",
2020 cpu_fprintf(f
, "translated TBs %" PRId64
" (aborted=%" PRId64
" %0.1f%%)\n",
2022 s
->tb_count1
- s
->tb_count
,
2023 s
->tb_count1
? (double)(s
->tb_count1
- s
->tb_count
) / s
->tb_count1
* 100.0 : 0);
2024 cpu_fprintf(f
, "avg ops/TB %0.1f max=%d\n",
2025 s
->tb_count
? (double)s
->op_count
/ s
->tb_count
: 0, s
->op_count_max
);
2026 cpu_fprintf(f
, "old ops/total ops %0.1f%%\n",
2027 s
->op_count
? (double)s
->old_op_count
/ s
->op_count
* 100.0 : 0);
2028 cpu_fprintf(f
, "deleted ops/TB %0.2f\n",
2030 (double)s
->del_op_count
/ s
->tb_count
: 0);
2031 cpu_fprintf(f
, "avg temps/TB %0.2f max=%d\n",
2033 (double)s
->temp_count
/ s
->tb_count
: 0,
2036 cpu_fprintf(f
, "cycles/op %0.1f\n",
2037 s
->op_count
? (double)tot
/ s
->op_count
: 0);
2038 cpu_fprintf(f
, "cycles/in byte %0.1f\n",
2039 s
->code_in_len
? (double)tot
/ s
->code_in_len
: 0);
2040 cpu_fprintf(f
, "cycles/out byte %0.1f\n",
2041 s
->code_out_len
? (double)tot
/ s
->code_out_len
: 0);
2044 cpu_fprintf(f
, " gen_interm time %0.1f%%\n",
2045 (double)s
->interm_time
/ tot
* 100.0);
2046 cpu_fprintf(f
, " gen_code time %0.1f%%\n",
2047 (double)s
->code_time
/ tot
* 100.0);
2048 cpu_fprintf(f
, "liveness/code time %0.1f%%\n",
2049 (double)s
->la_time
/ (s
->code_time
? s
->code_time
: 1) * 100.0);
2050 cpu_fprintf(f
, "cpu_restore count %" PRId64
"\n",
2052 cpu_fprintf(f
, " avg cycles %0.1f\n",
2053 s
->restore_count
? (double)s
->restore_time
/ s
->restore_count
: 0);
2055 extern void dump_op_count(void);
2060 void tcg_dump_info(FILE *f
,
2061 int (*cpu_fprintf
)(FILE *f
, const char *fmt
, ...))
2063 cpu_fprintf(f
, "[TCG profiler not compiled]\n");