tcg.c revision 3d40f685fa5cdd9cb665ae3cbf5f76113dafcb99
2N/A/*
2N/A * Tiny Code Generator for QEMU
2N/A *
2N/A * Copyright (c) 2008 Fabrice Bellard
2N/A *
2N/A * Permission is hereby granted, free of charge, to any person obtaining a copy
2N/A * of this software and associated documentation files (the "Software"), to deal
2N/A * in the Software without restriction, including without limitation the rights
2N/A * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2N/A * copies of the Software, and to permit persons to whom the Software is
2N/A * furnished to do so, subject to the following conditions:
2N/A *
2N/A * The above copyright notice and this permission notice shall be included in
2N/A * all copies or substantial portions of the Software.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
2N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2N/A * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2N/A * THE SOFTWARE.
2N/A */
2N/A
2N/A/* define it to use liveness analysis (better code) */
2N/A#define USE_LIVENESS_ANALYSIS
2N/A
2N/A#include "config.h"
2N/A
2N/A#if !defined(CONFIG_DEBUG_TCG) && !defined(NDEBUG)
2N/A/* define it to suppress various consistency checks (faster) */
2N/A#define NDEBUG
2N/A#endif
2N/A
2N/A#ifndef VBOX
2N/A#include <stdarg.h>
2N/A#include <stdlib.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <inttypes.h>
2N/A#ifdef _WIN32
2N/A#include <malloc.h>
2N/A#endif
2N/A#ifdef _AIX
2N/A#include <alloca.h>
2N/A#endif
2N/A#else /* VBOX */
2N/A# include <stdio.h>
2N/A# include "osdep.h"
2N/A#endif /* VBOX */
2N/A
2N/A#include "qemu-common.h"
2N/A#include "cache-utils.h"
2N/A#include "host-utils.h"
2N/A#include "qemu-timer.h"
2N/A
2N/A/* Note: the long term plan is to reduce the dependancies on the QEMU
2N/A CPU definitions. Currently they are used for qemu_ld/st
2N/A instructions */
2N/A#define NO_CPU_IO_DEFS
2N/A#include "cpu.h"
2N/A#include "exec-all.h"
2N/A
2N/A#include "tcg-op.h"
2N/A#include "elf.h"
2N/A
2N/A#if defined(CONFIG_USE_GUEST_BASE) && !defined(TCG_TARGET_HAS_GUEST_BASE)
2N/A#error GUEST_BASE not supported on this host.
2N/A#endif
2N/A
2N/A#ifdef VBOX
2N/A/*
2N/A * Liveness analysis doesn't work well with 32-bit hosts and 64-bit targets,
2N/A * second element of the register pair to store 64-bit value is considered
2N/A * dead, it seems. */
2N/A /** @todo re-test this */
2N/A# if defined(TARGET_X86_64) && (TCG_TARGET_REG_BITS == 32)
2N/A# undef USE_LIVENESS_ANALYSIS
2N/A# endif
2N/A#endif /* VBOX */
2N/A
2N/Astatic void tcg_target_init(TCGContext *s);
2N/Astatic void tcg_target_qemu_prologue(TCGContext *s);
2N/Astatic void patch_reloc(uint8_t *code_ptr, int type,
2N/A tcg_target_long value, tcg_target_long addend);
2N/A
2N/Astatic TCGOpDef tcg_op_defs[] = {
2N/A#define DEF(s, oargs, iargs, cargs, flags) { #s, oargs, iargs, cargs, iargs + oargs + cargs, flags },
2N/A#include "tcg-opc.h"
2N/A#undef DEF
2N/A};
2N/A
2N/Astatic TCGRegSet tcg_target_available_regs[2];
2N/Astatic TCGRegSet tcg_target_call_clobber_regs;
2N/A
2N/A/* XXX: move that inside the context */
2N/Auint16_t *gen_opc_ptr;
2N/ATCGArg *gen_opparam_ptr;
2N/A
2N/Astatic inline void tcg_out8(TCGContext *s, uint8_t v)
2N/A{
2N/A *s->code_ptr++ = v;
2N/A}
2N/A
2N/Astatic inline void tcg_out16(TCGContext *s, uint16_t v)
2N/A{
2N/A *(uint16_t *)s->code_ptr = v;
2N/A s->code_ptr += 2;
2N/A}
2N/A
2N/Astatic inline void tcg_out32(TCGContext *s, uint32_t v)
2N/A{
2N/A *(uint32_t *)s->code_ptr = v;
2N/A s->code_ptr += 4;
2N/A}
2N/A
2N/A/* label relocation processing */
2N/A
2N/Astatic void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
2N/A int label_index, tcg_target_long addend)
2N/A{
2N/A TCGLabel *l;
2N/A TCGRelocation *r;
2N/A
2N/A l = &s->labels[label_index];
2N/A if (l->has_value) {
2N/A /* FIXME: This may break relocations on RISC targets that
2N/A modify instruction fields in place. The caller may not have
2N/A written the initial value. */
2N/A patch_reloc(code_ptr, type, l->u.value, addend);
2N/A } else {
2N/A /* add a new relocation entry */
2N/A r = tcg_malloc(sizeof(TCGRelocation));
2N/A r->type = type;
2N/A r->ptr = code_ptr;
2N/A r->addend = addend;
2N/A r->next = l->u.first_reloc;
2N/A l->u.first_reloc = r;
2N/A }
2N/A}
2N/A
2N/Astatic void tcg_out_label(TCGContext *s, int label_index,
2N/A tcg_target_long value)
2N/A{
2N/A TCGLabel *l;
2N/A TCGRelocation *r;
2N/A
2N/A l = &s->labels[label_index];
2N/A if (l->has_value)
2N/A tcg_abort();
2N/A r = l->u.first_reloc;
2N/A while (r != NULL) {
2N/A patch_reloc(r->ptr, r->type, value, r->addend);
2N/A r = r->next;
2N/A }
2N/A l->has_value = 1;
2N/A l->u.value = value;
2N/A}
2N/A
2N/Aint gen_new_label(void)
2N/A{
2N/A TCGContext *s = &tcg_ctx;
2N/A int idx;
2N/A TCGLabel *l;
2N/A
2N/A if (s->nb_labels >= TCG_MAX_LABELS)
2N/A tcg_abort();
2N/A idx = s->nb_labels++;
2N/A l = &s->labels[idx];
2N/A l->has_value = 0;
2N/A l->u.first_reloc = NULL;
2N/A return idx;
2N/A}
2N/A
2N/A#include "tcg-target.c"
2N/A
2N/A/* pool based memory allocation */
2N/Avoid *tcg_malloc_internal(TCGContext *s, int size)
2N/A{
2N/A TCGPool *p;
2N/A int pool_size;
2N/A
2N/A if (size > TCG_POOL_CHUNK_SIZE) {
2N/A /* big malloc: insert a new pool (XXX: could optimize) */
2N/A p = qemu_malloc(sizeof(TCGPool) + size);
2N/A p->size = size;
2N/A if (s->pool_current)
2N/A s->pool_current->next = p;
2N/A else
2N/A s->pool_first = p;
2N/A p->next = s->pool_current;
2N/A } else {
2N/A p = s->pool_current;
2N/A if (!p) {
2N/A p = s->pool_first;
2N/A if (!p)
2N/A goto new_pool;
2N/A } else {
2N/A if (!p->next) {
2N/A new_pool:
2N/A pool_size = TCG_POOL_CHUNK_SIZE;
2N/A p = qemu_malloc(sizeof(TCGPool) + pool_size);
2N/A p->size = pool_size;
2N/A p->next = NULL;
2N/A if (s->pool_current)
2N/A s->pool_current->next = p;
2N/A else
2N/A s->pool_first = p;
2N/A } else {
2N/A p = p->next;
2N/A }
2N/A }
2N/A }
2N/A s->pool_current = p;
2N/A s->pool_cur = p->data + size;
2N/A s->pool_end = p->data + p->size;
2N/A return p->data;
2N/A}
2N/A
2N/Avoid tcg_pool_reset(TCGContext *s)
2N/A{
2N/A s->pool_cur = s->pool_end = NULL;
2N/A s->pool_current = NULL;
2N/A}
2N/A
2N/Avoid tcg_context_init(TCGContext *s)
2N/A{
2N/A int op, total_args, n;
2N/A TCGOpDef *def;
2N/A TCGArgConstraint *args_ct;
2N/A int *sorted_args;
2N/A
2N/A memset(s, 0, sizeof(*s));
2N/A s->temps = s->static_temps;
2N/A s->nb_globals = 0;
2N/A
2N/A /* Count total number of arguments and allocate the corresponding
2N/A space */
2N/A total_args = 0;
2N/A for(op = 0; op < NB_OPS; op++) {
2N/A def = &tcg_op_defs[op];
2N/A n = def->nb_iargs + def->nb_oargs;
2N/A total_args += n;
2N/A }
2N/A
2N/A args_ct = qemu_malloc(sizeof(TCGArgConstraint) * total_args);
2N/A sorted_args = qemu_malloc(sizeof(int) * total_args);
2N/A
2N/A for(op = 0; op < NB_OPS; op++) {
2N/A def = &tcg_op_defs[op];
2N/A def->args_ct = args_ct;
2N/A def->sorted_args = sorted_args;
2N/A n = def->nb_iargs + def->nb_oargs;
2N/A sorted_args += n;
2N/A args_ct += n;
2N/A }
2N/A
2N/A tcg_target_init(s);
2N/A}
2N/A
2N/Avoid tcg_prologue_init(TCGContext *s)
2N/A{
2N/A /* init global prologue and epilogue */
2N/A s->code_buf = code_gen_prologue;
2N/A s->code_ptr = s->code_buf;
2N/A tcg_target_qemu_prologue(s);
2N/A flush_icache_range((uintptr_t)s->code_buf,
2N/A (uintptr_t)s->code_ptr);
2N/A}
2N/A
2N/Avoid tcg_set_frame(TCGContext *s, int reg,
2N/A tcg_target_long start, tcg_target_long size)
2N/A{
2N/A s->frame_start = start;
2N/A s->frame_end = start + size;
2N/A s->frame_reg = reg;
2N/A}
2N/A
2N/Avoid tcg_func_start(TCGContext *s)
2N/A{
2N/A int i;
2N/A tcg_pool_reset(s);
2N/A s->nb_temps = s->nb_globals;
2N/A for(i = 0; i < (TCG_TYPE_COUNT * 2); i++)
2N/A s->first_free_temp[i] = -1;
2N/A s->labels = tcg_malloc(sizeof(TCGLabel) * TCG_MAX_LABELS);
2N/A s->nb_labels = 0;
2N/A s->current_frame_offset = s->frame_start;
2N/A
2N/A gen_opc_ptr = gen_opc_buf;
2N/A gen_opparam_ptr = gen_opparam_buf;
2N/A}
2N/A
2N/Astatic inline void tcg_temp_alloc(TCGContext *s, int n)
2N/A{
2N/A if (n > TCG_MAX_TEMPS)
2N/A tcg_abort();
2N/A}
2N/A
2N/Astatic inline int tcg_global_reg_new_internal(TCGType type, int reg,
2N/A const char *name)
2N/A{
2N/A TCGContext *s = &tcg_ctx;
2N/A TCGTemp *ts;
2N/A int idx;
2N/A
2N/A#if TCG_TARGET_REG_BITS == 32
2N/A if (type != TCG_TYPE_I32)
2N/A tcg_abort();
2N/A#endif
2N/A if (tcg_regset_test_reg(s->reserved_regs, reg))
2N/A tcg_abort();
2N/A idx = s->nb_globals;
2N/A tcg_temp_alloc(s, s->nb_globals + 1);
2N/A ts = &s->temps[s->nb_globals];
2N/A ts->base_type = type;
2N/A ts->type = type;
2N/A ts->fixed_reg = 1;
2N/A ts->reg = reg;
2N/A ts->name = name;
2N/A s->nb_globals++;
2N/A tcg_regset_set_reg(s->reserved_regs, reg);
2N/A return idx;
2N/A}
2N/A
2N/ATCGv_i32 tcg_global_reg_new_i32(int reg, const char *name)
2N/A{
2N/A int idx;
2N/A
2N/A idx = tcg_global_reg_new_internal(TCG_TYPE_I32, reg, name);
2N/A return MAKE_TCGV_I32(idx);
2N/A}
2N/A
2N/ATCGv_i64 tcg_global_reg_new_i64(int reg, const char *name)
2N/A{
2N/A int idx;
2N/A
2N/A idx = tcg_global_reg_new_internal(TCG_TYPE_I64, reg, name);
2N/A return MAKE_TCGV_I64(idx);
2N/A}
2N/A
2N/Astatic inline int tcg_global_mem_new_internal(TCGType type, int reg,
2N/A tcg_target_long offset,
2N/A const char *name)
2N/A{
2N/A TCGContext *s = &tcg_ctx;
2N/A TCGTemp *ts;
2N/A int idx;
2N/A
2N/A idx = s->nb_globals;
2N/A#if TCG_TARGET_REG_BITS == 32
2N/A if (type == TCG_TYPE_I64) {
2N/A char buf[64];
2N/A tcg_temp_alloc(s, s->nb_globals + 2);
2N/A ts = &s->temps[s->nb_globals];
2N/A ts->base_type = type;
2N/A ts->type = TCG_TYPE_I32;
2N/A ts->fixed_reg = 0;
2N/A ts->mem_allocated = 1;
2N/A ts->mem_reg = reg;
2N/A#ifdef TCG_TARGET_WORDS_BIGENDIAN
2N/A ts->mem_offset = offset + 4;
2N/A#else
2N/A ts->mem_offset = offset;
2N/A#endif
2N/A pstrcpy(buf, sizeof(buf), name);
2N/A pstrcat(buf, sizeof(buf), "_0");
2N/A ts->name = strdup(buf);
2N/A ts++;
2N/A
2N/A ts->base_type = type;
2N/A ts->type = TCG_TYPE_I32;
2N/A ts->fixed_reg = 0;
2N/A ts->mem_allocated = 1;
2N/A ts->mem_reg = reg;
2N/A#ifdef TCG_TARGET_WORDS_BIGENDIAN
2N/A ts->mem_offset = offset;
2N/A#else
2N/A ts->mem_offset = offset + 4;
2N/A#endif
2N/A pstrcpy(buf, sizeof(buf), name);
2N/A pstrcat(buf, sizeof(buf), "_1");
2N/A ts->name = strdup(buf);
2N/A
2N/A s->nb_globals += 2;
2N/A } else
2N/A#endif
2N/A {
2N/A tcg_temp_alloc(s, s->nb_globals + 1);
2N/A ts = &s->temps[s->nb_globals];
2N/A ts->base_type = type;
2N/A ts->type = type;
2N/A ts->fixed_reg = 0;
2N/A ts->mem_allocated = 1;
2N/A ts->mem_reg = reg;
2N/A ts->mem_offset = offset;
2N/A ts->name = name;
2N/A s->nb_globals++;
2N/A }
2N/A return idx;
2N/A}
2N/A
2N/ATCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
2N/A const char *name)
2N/A{
2N/A int idx;
2N/A
2N/A idx = tcg_global_mem_new_internal(TCG_TYPE_I32, reg, offset, name);
2N/A return MAKE_TCGV_I32(idx);
2N/A}
2N/A
2N/ATCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
2N/A const char *name)
2N/A{
2N/A int idx;
2N/A
2N/A idx = tcg_global_mem_new_internal(TCG_TYPE_I64, reg, offset, name);
2N/A return MAKE_TCGV_I64(idx);
2N/A}
2N/A
2N/Astatic inline int tcg_temp_new_internal(TCGType type, int temp_local)
2N/A{
2N/A TCGContext *s = &tcg_ctx;
2N/A TCGTemp *ts;
2N/A int idx, k;
2N/A
2N/A k = type;
2N/A if (temp_local)
2N/A k += TCG_TYPE_COUNT;
2N/A idx = s->first_free_temp[k];
2N/A if (idx != -1) {
2N/A /* There is already an available temp with the
2N/A right type */
2N/A ts = &s->temps[idx];
2N/A s->first_free_temp[k] = ts->next_free_temp;
2N/A ts->temp_allocated = 1;
2N/A assert(ts->temp_local == temp_local);
2N/A } else {
2N/A idx = s->nb_temps;
2N/A#if TCG_TARGET_REG_BITS == 32
2N/A if (type == TCG_TYPE_I64) {
2N/A tcg_temp_alloc(s, s->nb_temps + 2);
2N/A ts = &s->temps[s->nb_temps];
2N/A ts->base_type = type;
2N/A ts->type = TCG_TYPE_I32;
2N/A ts->temp_allocated = 1;
2N/A ts->temp_local = temp_local;
2N/A ts->name = NULL;
2N/A ts++;
2N/A ts->base_type = TCG_TYPE_I32;
2N/A ts->type = TCG_TYPE_I32;
2N/A ts->temp_allocated = 1;
2N/A ts->temp_local = temp_local;
2N/A ts->name = NULL;
2N/A s->nb_temps += 2;
2N/A } else
2N/A#endif
2N/A {
2N/A tcg_temp_alloc(s, s->nb_temps + 1);
2N/A ts = &s->temps[s->nb_temps];
2N/A ts->base_type = type;
2N/A ts->type = type;
2N/A ts->temp_allocated = 1;
2N/A ts->temp_local = temp_local;
2N/A ts->name = NULL;
2N/A s->nb_temps++;
2N/A }
2N/A }
2N/A return idx;
2N/A}
2N/A
2N/ATCGv_i32 tcg_temp_new_internal_i32(int temp_local)
2N/A{
2N/A int idx;
2N/A
2N/A idx = tcg_temp_new_internal(TCG_TYPE_I32, temp_local);
2N/A return MAKE_TCGV_I32(idx);
2N/A}
2N/A
2N/ATCGv_i64 tcg_temp_new_internal_i64(int temp_local)
2N/A{
2N/A int idx;
2N/A
2N/A idx = tcg_temp_new_internal(TCG_TYPE_I64, temp_local);
2N/A return MAKE_TCGV_I64(idx);
2N/A}
2N/A
2N/Astatic inline void tcg_temp_free_internal(int idx)
2N/A{
2N/A TCGContext *s = &tcg_ctx;
2N/A TCGTemp *ts;
2N/A int k;
2N/A
2N/A assert(idx >= s->nb_globals && idx < s->nb_temps);
2N/A ts = &s->temps[idx];
2N/A assert(ts->temp_allocated != 0);
2N/A ts->temp_allocated = 0;
2N/A k = ts->base_type;
2N/A if (ts->temp_local)
2N/A k += TCG_TYPE_COUNT;
2N/A ts->next_free_temp = s->first_free_temp[k];
2N/A s->first_free_temp[k] = idx;
2N/A}
2N/A
2N/Avoid tcg_temp_free_i32(TCGv_i32 arg)
2N/A{
2N/A tcg_temp_free_internal(GET_TCGV_I32(arg));
2N/A}
2N/A
2N/Avoid tcg_temp_free_i64(TCGv_i64 arg)
2N/A{
2N/A tcg_temp_free_internal(GET_TCGV_I64(arg));
2N/A}
2N/A
2N/ATCGv_i32 tcg_const_i32(int32_t val)
2N/A{
2N/A TCGv_i32 t0;
2N/A t0 = tcg_temp_new_i32();
2N/A tcg_gen_movi_i32(t0, val);
2N/A return t0;
2N/A}
2N/A
2N/ATCGv_i64 tcg_const_i64(int64_t val)
2N/A{
2N/A TCGv_i64 t0;
2N/A t0 = tcg_temp_new_i64();
2N/A tcg_gen_movi_i64(t0, val);
2N/A return t0;
2N/A}
2N/A
2N/ATCGv_i32 tcg_const_local_i32(int32_t val)
2N/A{
2N/A TCGv_i32 t0;
2N/A t0 = tcg_temp_local_new_i32();
2N/A tcg_gen_movi_i32(t0, val);
2N/A return t0;
2N/A}
2N/A
2N/ATCGv_i64 tcg_const_local_i64(int64_t val)
2N/A{
2N/A TCGv_i64 t0;
2N/A t0 = tcg_temp_local_new_i64();
2N/A tcg_gen_movi_i64(t0, val);
2N/A return t0;
2N/A}
2N/A
2N/Avoid tcg_register_helper(void *func, const char *name)
2N/A{
2N/A TCGContext *s = &tcg_ctx;
2N/A int n;
2N/A if ((s->nb_helpers + 1) > s->allocated_helpers) {
2N/A n = s->allocated_helpers;
2N/A if (n == 0) {
2N/A n = 4;
2N/A } else {
2N/A n *= 2;
2N/A }
2N/A#ifdef VBOX
2N/A s->helpers = qemu_realloc(s->helpers, n * sizeof(TCGHelperInfo));
2N/A#else
2N/A s->helpers = realloc(s->helpers, n * sizeof(TCGHelperInfo));
2N/A#endif
2N/A s->allocated_helpers = n;
2N/A }
2N/A s->helpers[s->nb_helpers].func = (tcg_target_ulong)func;
2N/A s->helpers[s->nb_helpers].name = name;
2N/A s->nb_helpers++;
2N/A}
2N/A
2N/A/* Note: we convert the 64 bit args to 32 bit and do some alignment
2N/A and endian swap. Maybe it would be better to do the alignment
2N/A and endian swap in tcg_reg_alloc_call(). */
2N/Avoid tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
2N/A int sizemask, TCGArg ret, int nargs, TCGArg *args)
2N/A{
2N/A#ifdef TCG_TARGET_I386
2N/A int call_type;
2N/A#endif
2N/A int i;
2N/A int real_args;
2N/A int nb_rets;
2N/A TCGArg *nparam;
2N/A
2N/A#if defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2N/A for (i = 0; i < nargs; ++i) {
2N/A int is_64bit = sizemask & (1 << (i+1)*2);
2N/A int is_signed = sizemask & (2 << (i+1)*2);
2N/A if (!is_64bit) {
2N/A TCGv_i64 temp = tcg_temp_new_i64();
2N/A TCGv_i64 orig = MAKE_TCGV_I64(args[i]);
2N/A if (is_signed) {
2N/A tcg_gen_ext32s_i64(temp, orig);
2N/A } else {
2N/A tcg_gen_ext32u_i64(temp, orig);
2N/A }
2N/A args[i] = GET_TCGV_I64(temp);
2N/A }
2N/A }
2N/A#endif /* TCG_TARGET_EXTEND_ARGS */
2N/A
2N/A *gen_opc_ptr++ = INDEX_op_call;
2N/A nparam = gen_opparam_ptr++;
2N/A#ifdef TCG_TARGET_I386
2N/A call_type = (flags & TCG_CALL_TYPE_MASK);
2N/A#endif
2N/A if (ret != TCG_CALL_DUMMY_ARG) {
2N/A#if TCG_TARGET_REG_BITS < 64
2N/A if (sizemask & 1) {
2N/A#ifdef TCG_TARGET_WORDS_BIGENDIAN
2N/A *gen_opparam_ptr++ = ret + 1;
2N/A *gen_opparam_ptr++ = ret;
2N/A#else
2N/A *gen_opparam_ptr++ = ret;
2N/A *gen_opparam_ptr++ = ret + 1;
2N/A#endif
2N/A nb_rets = 2;
2N/A } else
2N/A#endif
2N/A {
2N/A *gen_opparam_ptr++ = ret;
2N/A nb_rets = 1;
2N/A }
2N/A } else {
2N/A nb_rets = 0;
2N/A }
2N/A real_args = 0;
2N/A for (i = 0; i < nargs; i++) {
2N/A#if TCG_TARGET_REG_BITS < 64
2N/A int is_64bit = sizemask & (1 << (i+1)*2);
2N/A if (is_64bit) {
2N/A#ifdef TCG_TARGET_I386
2N/A /* REGPARM case: if the third parameter is 64 bit, it is
2N/A allocated on the stack */
2N/A if (i == 2 && call_type == TCG_CALL_TYPE_REGPARM) {
2N/A call_type = TCG_CALL_TYPE_REGPARM_2;
2N/A flags = (flags & ~TCG_CALL_TYPE_MASK) | call_type;
2N/A }
2N/A#endif
2N/A#ifdef TCG_TARGET_CALL_ALIGN_ARGS
2N/A /* some targets want aligned 64 bit args */
2N/A if (real_args & 1) {
2N/A *gen_opparam_ptr++ = TCG_CALL_DUMMY_ARG;
2N/A real_args++;
2N/A }
2N/A#endif
2N/A /* If stack grows up, then we will be placing successive
2N/A arguments at lower addresses, which means we need to
2N/A reverse the order compared to how we would normally
2N/A treat either big or little-endian. For those arguments
2N/A that will wind up in registers, this still works for
2N/A HPPA (the only current STACK_GROWSUP target) since the
2N/A argument registers are *also* allocated in decreasing
2N/A order. If another such target is added, this logic may
2N/A have to get more complicated to differentiate between
2N/A stack arguments and register arguments. */
2N/A#if defined(TCG_TARGET_WORDS_BIGENDIAN) != defined(TCG_TARGET_STACK_GROWSUP)
2N/A *gen_opparam_ptr++ = args[i] + 1;
2N/A *gen_opparam_ptr++ = args[i];
2N/A#else
2N/A *gen_opparam_ptr++ = args[i];
2N/A *gen_opparam_ptr++ = args[i] + 1;
2N/A#endif
2N/A real_args += 2;
2N/A continue;
2N/A }
2N/A#endif /* TCG_TARGET_REG_BITS < 64 */
2N/A
2N/A *gen_opparam_ptr++ = args[i];
2N/A real_args++;
2N/A }
2N/A *gen_opparam_ptr++ = GET_TCGV_PTR(func);
2N/A
2N/A *gen_opparam_ptr++ = flags;
2N/A
2N/A *nparam = (nb_rets << 16) | (real_args + 1);
2N/A
2N/A /* total parameters, needed to go backward in the instruction stream */
2N/A *gen_opparam_ptr++ = 1 + nb_rets + real_args + 3;
2N/A
2N/A#if defined(TCG_TARGET_EXTEND_ARGS) && TCG_TARGET_REG_BITS == 64
2N/A for (i = 0; i < nargs; ++i) {
2N/A int is_64bit = sizemask & (1 << (i+1)*2);
2N/A if (!is_64bit) {
2N/A TCGv_i64 temp = MAKE_TCGV_I64(args[i]);
2N/A tcg_temp_free_i64(temp);
2N/A }
2N/A }
2N/A#endif /* TCG_TARGET_EXTEND_ARGS */
2N/A}
2N/A
2N/A#if TCG_TARGET_REG_BITS == 32
2N/Avoid tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
2N/A int c, int right, int arith)
2N/A{
2N/A if (c == 0) {
2N/A tcg_gen_mov_i32(TCGV_LOW(ret), TCGV_LOW(arg1));
2N/A tcg_gen_mov_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1));
2N/A } else if (c >= 32) {
2N/A c -= 32;
2N/A if (right) {
2N/A if (arith) {
2N/A tcg_gen_sari_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c);
2N/A tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), 31);
2N/A } else {
2N/A tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_HIGH(arg1), c);
2N/A tcg_gen_movi_i32(TCGV_HIGH(ret), 0);
2N/A }
2N/A } else {
2N/A tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_LOW(arg1), c);
2N/A tcg_gen_movi_i32(TCGV_LOW(ret), 0);
2N/A }
2N/A } else {
2N/A TCGv_i32 t0, t1;
2N/A
2N/A t0 = tcg_temp_new_i32();
2N/A t1 = tcg_temp_new_i32();
2N/A if (right) {
2N/A tcg_gen_shli_i32(t0, TCGV_HIGH(arg1), 32 - c);
2N/A if (arith)
2N/A tcg_gen_sari_i32(t1, TCGV_HIGH(arg1), c);
2N/A else
2N/A tcg_gen_shri_i32(t1, TCGV_HIGH(arg1), c);
2N/A tcg_gen_shri_i32(TCGV_LOW(ret), TCGV_LOW(arg1), c);
2N/A tcg_gen_or_i32(TCGV_LOW(ret), TCGV_LOW(ret), t0);
2N/A tcg_gen_mov_i32(TCGV_HIGH(ret), t1);
2N/A } else {
2N/A tcg_gen_shri_i32(t0, TCGV_LOW(arg1), 32 - c);
2N/A /* Note: ret can be the same as arg1, so we use t1 */
2N/A tcg_gen_shli_i32(t1, TCGV_LOW(arg1), c);
2N/A tcg_gen_shli_i32(TCGV_HIGH(ret), TCGV_HIGH(arg1), c);
2N/A tcg_gen_or_i32(TCGV_HIGH(ret), TCGV_HIGH(ret), t0);
2N/A tcg_gen_mov_i32(TCGV_LOW(ret), t1);
2N/A }
2N/A tcg_temp_free_i32(t0);
2N/A tcg_temp_free_i32(t1);
2N/A }
2N/A}
2N/A#endif
2N/A
2N/A
2N/Astatic void tcg_reg_alloc_start(TCGContext *s)
2N/A{
2N/A int i;
2N/A TCGTemp *ts;
2N/A for(i = 0; i < s->nb_globals; i++) {
2N/A ts = &s->temps[i];
2N/A if (ts->fixed_reg) {
2N/A ts->val_type = TEMP_VAL_REG;
2N/A } else {
2N/A ts->val_type = TEMP_VAL_MEM;
2N/A }
2N/A }
2N/A for(i = s->nb_globals; i < s->nb_temps; i++) {
2N/A ts = &s->temps[i];
2N/A ts->val_type = TEMP_VAL_DEAD;
2N/A ts->mem_allocated = 0;
2N/A ts->fixed_reg = 0;
2N/A }
2N/A for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2N/A s->reg_to_temp[i] = -1;
2N/A }
2N/A}
2N/A
2N/Astatic char *tcg_get_arg_str_idx(TCGContext *s, char *buf, int buf_size,
2N/A int idx)
2N/A{
2N/A TCGTemp *ts;
2N/A
2N/A ts = &s->temps[idx];
2N/A if (idx < s->nb_globals) {
2N/A pstrcpy(buf, buf_size, ts->name);
2N/A } else {
2N/A if (ts->temp_local)
2N/A snprintf(buf, buf_size, "loc%d", idx - s->nb_globals);
2N/A else
2N/A snprintf(buf, buf_size, "tmp%d", idx - s->nb_globals);
2N/A }
2N/A return buf;
2N/A}
2N/A
2N/Achar *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg)
2N/A{
2N/A return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I32(arg));
2N/A}
2N/A
2N/Achar *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg)
2N/A{
2N/A return tcg_get_arg_str_idx(s, buf, buf_size, GET_TCGV_I64(arg));
2N/A}
2N/A
2N/Astatic int helper_cmp(const void *p1, const void *p2)
2N/A{
2N/A const TCGHelperInfo *th1 = p1;
2N/A const TCGHelperInfo *th2 = p2;
2N/A if (th1->func < th2->func)
2N/A return -1;
2N/A else if (th1->func == th2->func)
2N/A return 0;
2N/A else
2N/A return 1;
2N/A}
2N/A
2N/A/* find helper definition (Note: A hash table would be better) */
2N/Astatic TCGHelperInfo *tcg_find_helper(TCGContext *s, tcg_target_ulong val)
2N/A{
2N/A int m, m_min, m_max;
2N/A TCGHelperInfo *th;
2N/A tcg_target_ulong v;
2N/A
2N/A if (unlikely(!s->helpers_sorted)) {
2N/A#ifdef VBOX
2N/A qemu_qsort(s->helpers, s->nb_helpers, sizeof(TCGHelperInfo),
2N/A helper_cmp);
2N/A#else
2N/A qsort(s->helpers, s->nb_helpers, sizeof(TCGHelperInfo),
2N/A helper_cmp);
2N/A#endif
2N/A s->helpers_sorted = 1;
2N/A }
2N/A
2N/A /* binary search */
2N/A m_min = 0;
2N/A m_max = s->nb_helpers - 1;
2N/A while (m_min <= m_max) {
2N/A m = (m_min + m_max) >> 1;
2N/A th = &s->helpers[m];
2N/A v = th->func;
2N/A if (v == val)
2N/A return th;
2N/A else if (val < v) {
2N/A m_max = m - 1;
2N/A } else {
2N/A m_min = m + 1;
2N/A }
2N/A }
2N/A return NULL;
2N/A}
2N/A
2N/Astatic const char * const cond_name[] =
2N/A{
2N/A [TCG_COND_EQ] = "eq",
2N/A [TCG_COND_NE] = "ne",
2N/A [TCG_COND_LT] = "lt",
2N/A [TCG_COND_GE] = "ge",
2N/A [TCG_COND_LE] = "le",
2N/A [TCG_COND_GT] = "gt",
2N/A [TCG_COND_LTU] = "ltu",
2N/A [TCG_COND_GEU] = "geu",
2N/A [TCG_COND_LEU] = "leu",
2N/A [TCG_COND_GTU] = "gtu"
2N/A};
2N/A
2N/Avoid tcg_dump_ops(TCGContext *s, FILE *outfile)
2N/A{
2N/A const uint16_t *opc_ptr;
2N/A const TCGArg *args;
2N/A TCGArg arg;
2N/A TCGOpcode c;
2N/A int i, k, nb_oargs, nb_iargs, nb_cargs, first_insn;
2N/A const TCGOpDef *def;
2N/A char buf[128];
2N/A
2N/A first_insn = 1;
2N/A opc_ptr = gen_opc_buf;
2N/A args = gen_opparam_buf;
2N/A while (opc_ptr < gen_opc_ptr) {
2N/A c = *opc_ptr++;
2N/A def = &tcg_op_defs[c];
2N/A if (c == INDEX_op_debug_insn_start) {
2N/A uint64_t pc;
2N/A#if TARGET_LONG_BITS > TCG_TARGET_REG_BITS
2N/A pc = ((uint64_t)args[1] << 32) | args[0];
2N/A#else
2N/A pc = args[0];
2N/A#endif
2N/A if (!first_insn)
2N/A fprintf(outfile, "\n");
2N/A fprintf(outfile, " ---- 0x%" PRIx64, pc);
2N/A first_insn = 0;
2N/A nb_oargs = def->nb_oargs;
2N/A nb_iargs = def->nb_iargs;
2N/A nb_cargs = def->nb_cargs;
2N/A } else if (c == INDEX_op_call) {
2N/A TCGArg arg;
2N/A
2N/A /* variable number of arguments */
2N/A arg = *args++;
2N/A nb_oargs = arg >> 16;
2N/A nb_iargs = arg & 0xffff;
2N/A nb_cargs = def->nb_cargs;
2N/A
2N/A fprintf(outfile, " %s ", def->name);
2N/A
2N/A /* function name */
2N/A fprintf(outfile, "%s",
2N/A tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + nb_iargs - 1]));
2N/A /* flags */
2N/A fprintf(outfile, ",$0x%" TCG_PRIlx,
2N/A args[nb_oargs + nb_iargs]);
2N/A /* nb out args */
2N/A fprintf(outfile, ",$%d", nb_oargs);
2N/A for(i = 0; i < nb_oargs; i++) {
2N/A fprintf(outfile, ",");
2N/A fprintf(outfile, "%s",
2N/A tcg_get_arg_str_idx(s, buf, sizeof(buf), args[i]));
2N/A }
2N/A for(i = 0; i < (nb_iargs - 1); i++) {
2N/A fprintf(outfile, ",");
2N/A if (args[nb_oargs + i] == TCG_CALL_DUMMY_ARG) {
2N/A fprintf(outfile, "<dummy>");
2N/A } else {
2N/A fprintf(outfile, "%s",
2N/A tcg_get_arg_str_idx(s, buf, sizeof(buf), args[nb_oargs + i]));
2N/A }
2N/A }
2N/A } else if (c == INDEX_op_movi_i32
2N/A#if TCG_TARGET_REG_BITS == 64
2N/A || c == INDEX_op_movi_i64
2N/A#endif
2N/A ) {
2N/A tcg_target_ulong val;
2N/A TCGHelperInfo *th;
2N/A
2N/A nb_oargs = def->nb_oargs;
2N/A nb_iargs = def->nb_iargs;
2N/A nb_cargs = def->nb_cargs;
2N/A fprintf(outfile, " %s %s,$", def->name,
2N/A tcg_get_arg_str_idx(s, buf, sizeof(buf), args[0]));
2N/A val = args[1];
2N/A th = tcg_find_helper(s, val);
2N/A if (th) {
2N/A fprintf(outfile, "%s", th->name);
2N/A } else {
2N/A if (c == INDEX_op_movi_i32)
2N/A fprintf(outfile, "0x%x", (uint32_t)val);
2N/A else
2N/A fprintf(outfile, "0x%" PRIx64 , (uint64_t)val);
2N/A }
2N/A } else {
2N/A fprintf(outfile, " %s ", def->name);
2N/A if (c == INDEX_op_nopn) {
2N/A /* variable number of arguments */
2N/A nb_cargs = *args;
2N/A nb_oargs = 0;
2N/A nb_iargs = 0;
2N/A } else {
2N/A nb_oargs = def->nb_oargs;
2N/A nb_iargs = def->nb_iargs;
2N/A nb_cargs = def->nb_cargs;
2N/A }
2N/A
2N/A k = 0;
2N/A for(i = 0; i < nb_oargs; i++) {
2N/A if (k != 0)
2N/A fprintf(outfile, ",");
2N/A fprintf(outfile, "%s",
2N/A tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++]));
2N/A }
2N/A for(i = 0; i < nb_iargs; i++) {
2N/A if (k != 0)
2N/A fprintf(outfile, ",");
2N/A fprintf(outfile, "%s",
2N/A tcg_get_arg_str_idx(s, buf, sizeof(buf), args[k++]));
2N/A }
2N/A switch (c) {
2N/A case INDEX_op_brcond_i32:
2N/A#if TCG_TARGET_REG_BITS == 32
2N/A case INDEX_op_brcond2_i32:
2N/A#elif TCG_TARGET_REG_BITS == 64
2N/A case INDEX_op_brcond_i64:
2N/A#endif
2N/A case INDEX_op_setcond_i32:
2N/A#if TCG_TARGET_REG_BITS == 32
2N/A case INDEX_op_setcond2_i32:
2N/A#elif TCG_TARGET_REG_BITS == 64
2N/A case INDEX_op_setcond_i64:
2N/A#endif
2N/A if (args[k] < ARRAY_SIZE(cond_name) && cond_name[args[k]])
2N/A fprintf(outfile, ",%s", cond_name[args[k++]]);
2N/A else
2N/A fprintf(outfile, ",$0x%" TCG_PRIlx, args[k++]);
2N/A i = 1;
2N/A break;
2N/A default:
2N/A i = 0;
2N/A break;
2N/A }
2N/A for(; i < nb_cargs; i++) {
2N/A if (k != 0)
2N/A fprintf(outfile, ",");
2N/A arg = args[k++];
2N/A fprintf(outfile, "$0x%" TCG_PRIlx, arg);
2N/A }
2N/A }
2N/A fprintf(outfile, "\n");
2N/A args += nb_iargs + nb_oargs + nb_cargs;
2N/A }
2N/A}
2N/A
2N/A/* we give more priority to constraints with less registers */
2N/Astatic int get_constraint_priority(const TCGOpDef *def, int k)
2N/A{
2N/A const TCGArgConstraint *arg_ct;
2N/A
2N/A int i, n;
2N/A arg_ct = &def->args_ct[k];
2N/A if (arg_ct->ct & TCG_CT_ALIAS) {
2N/A /* an alias is equivalent to a single register */
2N/A n = 1;
2N/A } else {
2N/A if (!(arg_ct->ct & TCG_CT_REG))
2N/A return 0;
2N/A n = 0;
2N/A for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
2N/A if (tcg_regset_test_reg(arg_ct->u.regs, i))
2N/A n++;
2N/A }
2N/A }
2N/A return TCG_TARGET_NB_REGS - n + 1;
2N/A}
2N/A
2N/A/* sort from highest priority to lowest */
2N/Astatic void sort_constraints(TCGOpDef *def, int start, int n)
2N/A{
2N/A int i, j, p1, p2, tmp;
2N/A
2N/A for(i = 0; i < n; i++)
2N/A def->sorted_args[start + i] = start + i;
2N/A if (n <= 1)
2N/A return;
2N/A for(i = 0; i < n - 1; i++) {
2N/A for(j = i + 1; j < n; j++) {
2N/A p1 = get_constraint_priority(def, def->sorted_args[start + i]);
2N/A p2 = get_constraint_priority(def, def->sorted_args[start + j]);
2N/A if (p1 < p2) {
2N/A tmp = def->sorted_args[start + i];
2N/A def->sorted_args[start + i] = def->sorted_args[start + j];
2N/A def->sorted_args[start + j] = tmp;
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/Avoid tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs)
2N/A{
2N/A TCGOpcode op;
2N/A TCGOpDef *def;
2N/A const char *ct_str;
2N/A int i, nb_args;
2N/A
2N/A for(;;) {
2N/A if (tdefs->op == (TCGOpcode)-1)
2N/A break;
2N/A op = tdefs->op;
2N/A assert(op >= 0 && op < NB_OPS);
2N/A def = &tcg_op_defs[op];
2N/A#if defined(CONFIG_DEBUG_TCG)
2N/A /* Duplicate entry in op definitions? */
2N/A assert(!def->used);
2N/A def->used = 1;
2N/A#endif
2N/A nb_args = def->nb_iargs + def->nb_oargs;
2N/A for(i = 0; i < nb_args; i++) {
2N/A ct_str = tdefs->args_ct_str[i];
2N/A /* Incomplete TCGTargetOpDef entry? */
2N/A assert(ct_str != NULL);
2N/A tcg_regset_clear(def->args_ct[i].u.regs);
2N/A def->args_ct[i].ct = 0;
2N/A if (ct_str[0] >= '0' && ct_str[0] <= '9') {
2N/A int oarg;
2N/A oarg = ct_str[0] - '0';
2N/A assert(oarg < def->nb_oargs);
2N/A assert(def->args_ct[oarg].ct & TCG_CT_REG);
2N/A /* TCG_CT_ALIAS is for the output arguments. The input
2N/A argument is tagged with TCG_CT_IALIAS. */
2N/A def->args_ct[i] = def->args_ct[oarg];
2N/A def->args_ct[oarg].ct = TCG_CT_ALIAS;
2N/A def->args_ct[oarg].alias_index = i;
2N/A def->args_ct[i].ct |= TCG_CT_IALIAS;
2N/A def->args_ct[i].alias_index = oarg;
2N/A } else {
2N/A for(;;) {
2N/A if (*ct_str == '\0')
2N/A break;
2N/A switch(*ct_str) {
2N/A case 'i':
2N/A def->args_ct[i].ct |= TCG_CT_CONST;
2N/A ct_str++;
2N/A break;
2N/A default:
2N/A if (target_parse_constraint(&def->args_ct[i], &ct_str) < 0) {
2N/A fprintf(stderr, "Invalid constraint '%s' for arg %d of operation '%s'\n",
2N/A ct_str, i, def->name);
2N/A#ifndef VBOX
2N/A exit(1);
2N/A#else
2N/A tcg_exit(1);
2N/A#endif
2N/A }
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A /* TCGTargetOpDef entry with too much information? */
2N/A assert(i == TCG_MAX_OP_ARGS || tdefs->args_ct_str[i] == NULL);
2N/A
2N/A /* sort the constraints (XXX: this is just an heuristic) */
2N/A sort_constraints(def, 0, def->nb_oargs);
2N/A sort_constraints(def, def->nb_oargs, def->nb_iargs);
2N/A
2N/A#if 0
2N/A {
2N/A int i;
2N/A
2N/A printf("%s: sorted=", def->name);
2N/A for(i = 0; i < def->nb_oargs + def->nb_iargs; i++)
2N/A printf(" %d", def->sorted_args[i]);
2N/A printf("\n");
2N/A }
2N/A#endif
2N/A tdefs++;
2N/A }
2N/A
2N/A#if defined(CONFIG_DEBUG_TCG)
2N/A i = 0;
2N/A for (op = 0; op < ARRAY_SIZE(tcg_op_defs); op++) {
2N/A if (op < INDEX_op_call || op == INDEX_op_debug_insn_start) {
2N/A /* Wrong entry in op definitions? */
2N/A if (tcg_op_defs[op].used) {
2N/A fprintf(stderr, "Invalid op definition for %s\n",
2N/A tcg_op_defs[op].name);
2N/A i = 1;
2N/A }
2N/A } else {
2N/A /* Missing entry in op definitions? */
2N/A if (!tcg_op_defs[op].used) {
2N/A fprintf(stderr, "Missing op definition for %s\n",
2N/A tcg_op_defs[op].name);
2N/A i = 1;
2N/A }
2N/A }
2N/A }
2N/A if (i == 1) {
2N/A tcg_abort();
2N/A }
2N/A#endif
2N/A}
2N/A
2N/A#ifdef USE_LIVENESS_ANALYSIS
2N/A
2N/A/* set a nop for an operation using 'nb_args' */
2N/Astatic inline void tcg_set_nop(TCGContext *s, uint16_t *opc_ptr,
2N/A TCGArg *args, int nb_args)
2N/A{
2N/A if (nb_args == 0) {
2N/A *opc_ptr = INDEX_op_nop;
2N/A } else {
2N/A *opc_ptr = INDEX_op_nopn;
2N/A args[0] = nb_args;
2N/A args[nb_args - 1] = nb_args;
2N/A }
2N/A}
2N/A
2N/A/* liveness analysis: end of function: globals are live, temps are
2N/A dead. */
2N/A/* XXX: at this stage, not used as there would be little gains because
2N/A most TBs end with a conditional jump. */
2N/Astatic inline void tcg_la_func_end(TCGContext *s, uint8_t *dead_temps)
2N/A{
2N/A memset(dead_temps, 0, s->nb_globals);
2N/A memset(dead_temps + s->nb_globals, 1, s->nb_temps - s->nb_globals);
2N/A}
2N/A
2N/A/* liveness analysis: end of basic block: globals are live, temps are
2N/A dead, local temps are live. */
2N/Astatic inline void tcg_la_bb_end(TCGContext *s, uint8_t *dead_temps)
2N/A{
2N/A int i;
2N/A TCGTemp *ts;
2N/A
2N/A memset(dead_temps, 0, s->nb_globals);
2N/A ts = &s->temps[s->nb_globals];
2N/A for(i = s->nb_globals; i < s->nb_temps; i++) {
2N/A if (ts->temp_local)
2N/A dead_temps[i] = 0;
2N/A else
2N/A dead_temps[i] = 1;
2N/A ts++;
2N/A }
2N/A}
2N/A
2N/A/* Liveness analysis : update the opc_dead_iargs array to tell if a
2N/A given input arguments is dead. Instructions updating dead
2N/A temporaries are removed. */
2N/Astatic void tcg_liveness_analysis(TCGContext *s)
2N/A{
2N/A int i, op_index, nb_args, nb_iargs, nb_oargs, arg, nb_ops;
2N/A TCGOpcode op;
2N/A TCGArg *args;
2N/A const TCGOpDef *def;
2N/A uint8_t *dead_temps;
2N/A unsigned int dead_iargs;
2N/A
2N/A gen_opc_ptr++; /* skip end */
2N/A
2N/A nb_ops = gen_opc_ptr - gen_opc_buf;
2N/A
2N/A s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t));
2N/A
2N/A dead_temps = tcg_malloc(s->nb_temps);
2N/A memset(dead_temps, 1, s->nb_temps);
2N/A
2N/A args = gen_opparam_ptr;
2N/A op_index = nb_ops - 1;
2N/A while (op_index >= 0) {
2N/A op = gen_opc_buf[op_index];
2N/A def = &tcg_op_defs[op];
2N/A switch(op) {
2N/A case INDEX_op_call:
2N/A {
2N/A int call_flags;
2N/A
2N/A nb_args = args[-1];
2N/A args -= nb_args;
2N/A nb_iargs = args[0] & 0xffff;
2N/A nb_oargs = args[0] >> 16;
2N/A args++;
2N/A call_flags = args[nb_oargs + nb_iargs];
2N/A
2N/A /* pure functions can be removed if their result is not
2N/A used */
2N/A if (call_flags & TCG_CALL_PURE) {
2N/A for(i = 0; i < nb_oargs; i++) {
2N/A arg = args[i];
2N/A if (!dead_temps[arg])
2N/A goto do_not_remove_call;
2N/A }
2N/A tcg_set_nop(s, gen_opc_buf + op_index,
2N/A args - 1, nb_args);
2N/A } else {
2N/A do_not_remove_call:
2N/A
2N/A /* output args are dead */
2N/A for(i = 0; i < nb_oargs; i++) {
2N/A arg = args[i];
2N/A dead_temps[arg] = 1;
2N/A }
2N/A
2N/A if (!(call_flags & TCG_CALL_CONST)) {
2N/A /* globals are live (they may be used by the call) */
2N/A memset(dead_temps, 0, s->nb_globals);
2N/A }
2N/A
2N/A /* input args are live */
2N/A dead_iargs = 0;
2N/A for(i = 0; i < nb_iargs; i++) {
2N/A arg = args[i + nb_oargs];
2N/A if (arg != TCG_CALL_DUMMY_ARG) {
2N/A if (dead_temps[arg]) {
2N/A dead_iargs |= (1 << i);
2N/A }
2N/A dead_temps[arg] = 0;
2N/A }
}
s->op_dead_iargs[op_index] = dead_iargs;
}
args--;
}
break;
case INDEX_op_set_label:
args--;
/* mark end of basic block */
tcg_la_bb_end(s, dead_temps);
break;
case INDEX_op_debug_insn_start:
args -= def->nb_args;
break;
case INDEX_op_nopn:
nb_args = args[-1];
args -= nb_args;
break;
case INDEX_op_discard:
args--;
/* mark the temporary as dead */
dead_temps[args[0]] = 1;
break;
case INDEX_op_end:
break;
/* XXX: optimize by hardcoding common cases (e.g. triadic ops) */
default:
args -= def->nb_args;
nb_iargs = def->nb_iargs;
nb_oargs = def->nb_oargs;
/* Test if the operation can be removed because all
its outputs are dead. We assume that nb_oargs == 0
implies side effects */
if (!(def->flags & TCG_OPF_SIDE_EFFECTS) && nb_oargs != 0) {
for(i = 0; i < nb_oargs; i++) {
arg = args[i];
if (!dead_temps[arg])
goto do_not_remove;
}
tcg_set_nop(s, gen_opc_buf + op_index, args, def->nb_args);
#ifdef CONFIG_PROFILER
s->del_op_count++;
#endif
} else {
do_not_remove:
/* output args are dead */
for(i = 0; i < nb_oargs; i++) {
arg = args[i];
dead_temps[arg] = 1;
}
/* if end of basic block, update */
if (def->flags & TCG_OPF_BB_END) {
tcg_la_bb_end(s, dead_temps);
} else if (def->flags & TCG_OPF_CALL_CLOBBER) {
/* globals are live */
memset(dead_temps, 0, s->nb_globals);
}
/* input args are live */
dead_iargs = 0;
for(i = 0; i < nb_iargs; i++) {
arg = args[i + nb_oargs];
if (dead_temps[arg]) {
dead_iargs |= (1 << i);
}
dead_temps[arg] = 0;
}
s->op_dead_iargs[op_index] = dead_iargs;
}
break;
}
op_index--;
}
if (args != gen_opparam_buf)
tcg_abort();
}
#else
/* dummy liveness analysis */
static void tcg_liveness_analysis(TCGContext *s)
{
int nb_ops;
nb_ops = gen_opc_ptr - gen_opc_buf;
s->op_dead_iargs = tcg_malloc(nb_ops * sizeof(uint16_t));
memset(s->op_dead_iargs, 0, nb_ops * sizeof(uint16_t));
}
#endif
#ifndef NDEBUG
static void dump_regs(TCGContext *s)
{
TCGTemp *ts;
int i;
char buf[64];
for(i = 0; i < s->nb_temps; i++) {
ts = &s->temps[i];
printf(" %10s: ", tcg_get_arg_str_idx(s, buf, sizeof(buf), i));
switch(ts->val_type) {
case TEMP_VAL_REG:
printf("%s", tcg_target_reg_names[ts->reg]);
break;
case TEMP_VAL_MEM:
printf("%d(%s)", (int)ts->mem_offset, tcg_target_reg_names[ts->mem_reg]);
break;
case TEMP_VAL_CONST:
printf("$0x%" TCG_PRIlx, ts->val);
break;
case TEMP_VAL_DEAD:
printf("D");
break;
default:
printf("???");
break;
}
printf("\n");
}
for(i = 0; i < TCG_TARGET_NB_REGS; i++) {
if (s->reg_to_temp[i] >= 0) {
printf("%s: %s\n",
tcg_target_reg_names[i],
tcg_get_arg_str_idx(s, buf, sizeof(buf), s->reg_to_temp[i]));
}
}
}
static void check_regs(TCGContext *s)
{
int reg, k;
TCGTemp *ts;
char buf[64];
for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
k = s->reg_to_temp[reg];
if (k >= 0) {
ts = &s->temps[k];
if (ts->val_type != TEMP_VAL_REG ||
ts->reg != reg) {
printf("Inconsistency for register %s:\n",
tcg_target_reg_names[reg]);
goto fail;
}
}
}
for(k = 0; k < s->nb_temps; k++) {
ts = &s->temps[k];
if (ts->val_type == TEMP_VAL_REG &&
!ts->fixed_reg &&
s->reg_to_temp[ts->reg] != k) {
printf("Inconsistency for temp %s:\n",
tcg_get_arg_str_idx(s, buf, sizeof(buf), k));
fail:
printf("reg state:\n");
dump_regs(s);
tcg_abort();
}
}
}
#endif
static void temp_allocate_frame(TCGContext *s, int temp)
{
TCGTemp *ts;
ts = &s->temps[temp];
s->current_frame_offset = (s->current_frame_offset + sizeof(tcg_target_long) - 1) & ~(sizeof(tcg_target_long) - 1);
#ifndef VBOX
if (s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end)
#else
if ((tcg_target_long)s->current_frame_offset + sizeof(tcg_target_long) > s->frame_end)
#endif
tcg_abort();
ts->mem_offset = s->current_frame_offset;
ts->mem_reg = s->frame_reg;
ts->mem_allocated = 1;
s->current_frame_offset += sizeof(tcg_target_long);
}
/* free register 'reg' by spilling the corresponding temporary if necessary */
static void tcg_reg_free(TCGContext *s, int reg)
{
TCGTemp *ts;
int temp;
temp = s->reg_to_temp[reg];
if (temp != -1) {
ts = &s->temps[temp];
assert(ts->val_type == TEMP_VAL_REG);
if (!ts->mem_coherent) {
if (!ts->mem_allocated)
temp_allocate_frame(s, temp);
tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
}
ts->val_type = TEMP_VAL_MEM;
s->reg_to_temp[reg] = -1;
}
}
/* Allocate a register belonging to reg1 & ~reg2 */
static int tcg_reg_alloc(TCGContext *s, TCGRegSet reg1, TCGRegSet reg2)
{
int i, reg;
TCGRegSet reg_ct;
tcg_regset_andnot(reg_ct, reg1, reg2);
/* first try free registers */
for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
reg = tcg_target_reg_alloc_order[i];
if (tcg_regset_test_reg(reg_ct, reg) && s->reg_to_temp[reg] == -1)
return reg;
}
/* XXX: do better spill choice */
for(i = 0; i < ARRAY_SIZE(tcg_target_reg_alloc_order); i++) {
reg = tcg_target_reg_alloc_order[i];
if (tcg_regset_test_reg(reg_ct, reg)) {
tcg_reg_free(s, reg);
return reg;
}
}
tcg_abort();
}
/* save a temporary to memory. 'allocated_regs' is used in case a
temporary registers needs to be allocated to store a constant. */
static void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs)
{
TCGTemp *ts;
int reg;
ts = &s->temps[temp];
if (!ts->fixed_reg) {
switch(ts->val_type) {
case TEMP_VAL_REG:
tcg_reg_free(s, ts->reg);
break;
case TEMP_VAL_DEAD:
ts->val_type = TEMP_VAL_MEM;
break;
case TEMP_VAL_CONST:
reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
allocated_regs);
if (!ts->mem_allocated)
temp_allocate_frame(s, temp);
tcg_out_movi(s, ts->type, reg, ts->val);
tcg_out_st(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
ts->val_type = TEMP_VAL_MEM;
break;
case TEMP_VAL_MEM:
break;
default:
tcg_abort();
}
}
}
/* save globals to their cannonical location and assume they can be
modified be the following code. 'allocated_regs' is used in case a
temporary registers needs to be allocated to store a constant. */
static void save_globals(TCGContext *s, TCGRegSet allocated_regs)
{
int i;
for(i = 0; i < s->nb_globals; i++) {
temp_save(s, i, allocated_regs);
}
}
/* at the end of a basic block, we assume all temporaries are dead and
all globals are stored at their canonical location. */
static void tcg_reg_alloc_bb_end(TCGContext *s, TCGRegSet allocated_regs)
{
TCGTemp *ts;
int i;
for(i = s->nb_globals; i < s->nb_temps; i++) {
ts = &s->temps[i];
if (ts->temp_local) {
temp_save(s, i, allocated_regs);
} else {
if (ts->val_type == TEMP_VAL_REG) {
s->reg_to_temp[ts->reg] = -1;
}
ts->val_type = TEMP_VAL_DEAD;
}
}
save_globals(s, allocated_regs);
}
#define IS_DEAD_IARG(n) ((dead_iargs >> (n)) & 1)
static void tcg_reg_alloc_movi(TCGContext *s, const TCGArg *args)
{
TCGTemp *ots;
tcg_target_ulong val;
ots = &s->temps[args[0]];
val = args[1];
if (ots->fixed_reg) {
/* for fixed registers, we do not do any constant
propagation */
tcg_out_movi(s, ots->type, ots->reg, val);
} else {
/* The movi is not explicitly generated here */
if (ots->val_type == TEMP_VAL_REG)
s->reg_to_temp[ots->reg] = -1;
ots->val_type = TEMP_VAL_CONST;
ots->val = val;
}
}
static void tcg_reg_alloc_mov(TCGContext *s, const TCGOpDef *def,
const TCGArg *args,
unsigned int dead_iargs)
{
TCGTemp *ts, *ots;
int reg;
const TCGArgConstraint *arg_ct;
ots = &s->temps[args[0]];
ts = &s->temps[args[1]];
arg_ct = &def->args_ct[0];
/* XXX: always mark arg dead if IS_DEAD_IARG(0) */
if (ts->val_type == TEMP_VAL_REG) {
if (IS_DEAD_IARG(0) && !ts->fixed_reg && !ots->fixed_reg) {
/* the mov can be suppressed */
if (ots->val_type == TEMP_VAL_REG)
s->reg_to_temp[ots->reg] = -1;
reg = ts->reg;
s->reg_to_temp[reg] = -1;
ts->val_type = TEMP_VAL_DEAD;
} else {
if (ots->val_type == TEMP_VAL_REG) {
reg = ots->reg;
} else {
reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
}
if (ts->reg != reg) {
tcg_out_mov(s, ots->type, reg, ts->reg);
}
}
} else if (ts->val_type == TEMP_VAL_MEM) {
if (ots->val_type == TEMP_VAL_REG) {
reg = ots->reg;
} else {
reg = tcg_reg_alloc(s, arg_ct->u.regs, s->reserved_regs);
}
tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
} else if (ts->val_type == TEMP_VAL_CONST) {
if (ots->fixed_reg) {
reg = ots->reg;
tcg_out_movi(s, ots->type, reg, ts->val);
} else {
/* propagate constant */
if (ots->val_type == TEMP_VAL_REG)
s->reg_to_temp[ots->reg] = -1;
ots->val_type = TEMP_VAL_CONST;
ots->val = ts->val;
return;
}
} else {
tcg_abort();
}
s->reg_to_temp[reg] = args[0];
ots->reg = reg;
ots->val_type = TEMP_VAL_REG;
ots->mem_coherent = 0;
}
static void tcg_reg_alloc_op(TCGContext *s,
const TCGOpDef *def, TCGOpcode opc,
const TCGArg *args,
unsigned int dead_iargs)
{
TCGRegSet allocated_regs;
int i, k, nb_iargs, nb_oargs, reg;
TCGArg arg;
const TCGArgConstraint *arg_ct;
TCGTemp *ts;
TCGArg new_args[TCG_MAX_OP_ARGS];
int const_args[TCG_MAX_OP_ARGS];
nb_oargs = def->nb_oargs;
nb_iargs = def->nb_iargs;
/* copy constants */
memcpy(new_args + nb_oargs + nb_iargs,
args + nb_oargs + nb_iargs,
sizeof(TCGArg) * def->nb_cargs);
/* satisfy input constraints */
tcg_regset_set(allocated_regs, s->reserved_regs);
for(k = 0; k < nb_iargs; k++) {
i = def->sorted_args[nb_oargs + k];
arg = args[i];
arg_ct = &def->args_ct[i];
ts = &s->temps[arg];
if (ts->val_type == TEMP_VAL_MEM) {
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
ts->val_type = TEMP_VAL_REG;
ts->reg = reg;
ts->mem_coherent = 1;
s->reg_to_temp[reg] = arg;
} else if (ts->val_type == TEMP_VAL_CONST) {
if (tcg_target_const_match(ts->val, arg_ct)) {
/* constant is OK for instruction */
const_args[i] = 1;
new_args[i] = ts->val;
goto iarg_end;
} else {
/* need to move to a register */
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
tcg_out_movi(s, ts->type, reg, ts->val);
ts->val_type = TEMP_VAL_REG;
ts->reg = reg;
ts->mem_coherent = 0;
s->reg_to_temp[reg] = arg;
}
}
assert(ts->val_type == TEMP_VAL_REG);
if (arg_ct->ct & TCG_CT_IALIAS) {
if (ts->fixed_reg) {
/* if fixed register, we must allocate a new register
if the alias is not the same register */
if (arg != args[arg_ct->alias_index])
goto allocate_in_reg;
} else {
/* if the input is aliased to an output and if it is
not dead after the instruction, we must allocate
a new register and move it */
if (!IS_DEAD_IARG(i - nb_oargs))
goto allocate_in_reg;
}
}
reg = ts->reg;
if (tcg_regset_test_reg(arg_ct->u.regs, reg)) {
/* nothing to do : the constraint is satisfied */
} else {
allocate_in_reg:
/* allocate a new register matching the constraint
and move the temporary register into it */
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
tcg_out_mov(s, ts->type, reg, ts->reg);
}
new_args[i] = reg;
const_args[i] = 0;
tcg_regset_set_reg(allocated_regs, reg);
iarg_end: ;
}
if (def->flags & TCG_OPF_BB_END) {
tcg_reg_alloc_bb_end(s, allocated_regs);
} else {
/* mark dead temporaries and free the associated registers */
for(i = 0; i < nb_iargs; i++) {
arg = args[nb_oargs + i];
if (IS_DEAD_IARG(i)) {
ts = &s->temps[arg];
if (!ts->fixed_reg) {
if (ts->val_type == TEMP_VAL_REG)
s->reg_to_temp[ts->reg] = -1;
ts->val_type = TEMP_VAL_DEAD;
}
}
}
if (def->flags & TCG_OPF_CALL_CLOBBER) {
/* XXX: permit generic clobber register list ? */
for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
tcg_reg_free(s, reg);
}
}
/* XXX: for load/store we could do that only for the slow path
(i.e. when a memory callback is called) */
/* store globals and free associated registers (we assume the insn
can modify any global. */
save_globals(s, allocated_regs);
}
/* satisfy the output constraints */
tcg_regset_set(allocated_regs, s->reserved_regs);
for(k = 0; k < nb_oargs; k++) {
i = def->sorted_args[k];
arg = args[i];
arg_ct = &def->args_ct[i];
ts = &s->temps[arg];
if (arg_ct->ct & TCG_CT_ALIAS) {
reg = new_args[arg_ct->alias_index];
} else {
/* if fixed register, we try to use it */
reg = ts->reg;
if (ts->fixed_reg &&
tcg_regset_test_reg(arg_ct->u.regs, reg)) {
goto oarg_end;
}
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
}
tcg_regset_set_reg(allocated_regs, reg);
/* if a fixed register is used, then a move will be done afterwards */
if (!ts->fixed_reg) {
if (ts->val_type == TEMP_VAL_REG)
s->reg_to_temp[ts->reg] = -1;
ts->val_type = TEMP_VAL_REG;
ts->reg = reg;
/* temp value is modified, so the value kept in memory is
potentially not the same */
ts->mem_coherent = 0;
s->reg_to_temp[reg] = arg;
}
oarg_end:
new_args[i] = reg;
}
}
/* emit instruction */
tcg_out_op(s, opc, new_args, const_args);
/* move the outputs in the correct register if needed */
for(i = 0; i < nb_oargs; i++) {
ts = &s->temps[args[i]];
reg = new_args[i];
if (ts->fixed_reg && ts->reg != reg) {
tcg_out_mov(s, ts->type, ts->reg, reg);
}
}
}
#ifdef TCG_TARGET_STACK_GROWSUP
#define STACK_DIR(x) (-(x))
#else
#define STACK_DIR(x) (x)
#endif
static int tcg_reg_alloc_call(TCGContext *s, const TCGOpDef *def,
TCGOpcode opc, const TCGArg *args,
unsigned int dead_iargs)
{
int nb_iargs, nb_oargs, flags, nb_regs, i, reg, nb_params;
TCGArg arg, func_arg;
TCGTemp *ts;
tcg_target_long stack_offset, call_stack_size, func_addr;
int const_func_arg, allocate_args;
TCGRegSet allocated_regs;
const TCGArgConstraint *arg_ct;
arg = *args++;
nb_oargs = arg >> 16;
nb_iargs = arg & 0xffff;
nb_params = nb_iargs - 1;
flags = args[nb_oargs + nb_iargs];
nb_regs = tcg_target_get_call_iarg_regs_count(flags);
if (nb_regs > nb_params)
nb_regs = nb_params;
/* assign stack slots first */
/* XXX: preallocate call stack */
call_stack_size = (nb_params - nb_regs) * sizeof(tcg_target_long);
call_stack_size = (call_stack_size + TCG_TARGET_STACK_ALIGN - 1) &
~(TCG_TARGET_STACK_ALIGN - 1);
allocate_args = (call_stack_size > TCG_STATIC_CALL_ARGS_SIZE);
if (allocate_args) {
tcg_out_addi(s, TCG_REG_CALL_STACK, -STACK_DIR(call_stack_size));
}
stack_offset = TCG_TARGET_CALL_STACK_OFFSET;
for(i = nb_regs; i < nb_params; i++) {
arg = args[nb_oargs + i];
#ifdef TCG_TARGET_STACK_GROWSUP
stack_offset -= sizeof(tcg_target_long);
#endif
if (arg != TCG_CALL_DUMMY_ARG) {
ts = &s->temps[arg];
if (ts->val_type == TEMP_VAL_REG) {
tcg_out_st(s, ts->type, ts->reg, TCG_REG_CALL_STACK, stack_offset);
} else if (ts->val_type == TEMP_VAL_MEM) {
reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
s->reserved_regs);
/* XXX: not correct if reading values from the stack */
tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
} else if (ts->val_type == TEMP_VAL_CONST) {
reg = tcg_reg_alloc(s, tcg_target_available_regs[ts->type],
s->reserved_regs);
/* XXX: sign extend may be needed on some targets */
tcg_out_movi(s, ts->type, reg, ts->val);
tcg_out_st(s, ts->type, reg, TCG_REG_CALL_STACK, stack_offset);
} else {
tcg_abort();
}
}
#ifndef TCG_TARGET_STACK_GROWSUP
stack_offset += sizeof(tcg_target_long);
#endif
}
/* assign input registers */
tcg_regset_set(allocated_regs, s->reserved_regs);
for(i = 0; i < nb_regs; i++) {
arg = args[nb_oargs + i];
if (arg != TCG_CALL_DUMMY_ARG) {
ts = &s->temps[arg];
reg = tcg_target_call_iarg_regs[i];
tcg_reg_free(s, reg);
if (ts->val_type == TEMP_VAL_REG) {
if (ts->reg != reg) {
tcg_out_mov(s, ts->type, reg, ts->reg);
}
} else if (ts->val_type == TEMP_VAL_MEM) {
tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
} else if (ts->val_type == TEMP_VAL_CONST) {
/* XXX: sign extend ? */
tcg_out_movi(s, ts->type, reg, ts->val);
} else {
tcg_abort();
}
tcg_regset_set_reg(allocated_regs, reg);
}
}
/* assign function address */
func_arg = args[nb_oargs + nb_iargs - 1];
arg_ct = &def->args_ct[0];
ts = &s->temps[func_arg];
func_addr = ts->val;
const_func_arg = 0;
if (ts->val_type == TEMP_VAL_MEM) {
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
tcg_out_ld(s, ts->type, reg, ts->mem_reg, ts->mem_offset);
func_arg = reg;
tcg_regset_set_reg(allocated_regs, reg);
} else if (ts->val_type == TEMP_VAL_REG) {
reg = ts->reg;
if (!tcg_regset_test_reg(arg_ct->u.regs, reg)) {
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
tcg_out_mov(s, ts->type, reg, ts->reg);
}
func_arg = reg;
tcg_regset_set_reg(allocated_regs, reg);
} else if (ts->val_type == TEMP_VAL_CONST) {
if (tcg_target_const_match(func_addr, arg_ct)) {
const_func_arg = 1;
func_arg = func_addr;
} else {
reg = tcg_reg_alloc(s, arg_ct->u.regs, allocated_regs);
tcg_out_movi(s, ts->type, reg, func_addr);
func_arg = reg;
tcg_regset_set_reg(allocated_regs, reg);
}
} else {
tcg_abort();
}
/* mark dead temporaries and free the associated registers */
for(i = 0; i < nb_iargs; i++) {
arg = args[nb_oargs + i];
if (IS_DEAD_IARG(i)) {
ts = &s->temps[arg];
if (!ts->fixed_reg) {
if (ts->val_type == TEMP_VAL_REG)
s->reg_to_temp[ts->reg] = -1;
ts->val_type = TEMP_VAL_DEAD;
}
}
}
/* clobber call registers */
for(reg = 0; reg < TCG_TARGET_NB_REGS; reg++) {
if (tcg_regset_test_reg(tcg_target_call_clobber_regs, reg)) {
tcg_reg_free(s, reg);
}
}
/* store globals and free associated registers (we assume the call
can modify any global. */
if (!(flags & TCG_CALL_CONST)) {
save_globals(s, allocated_regs);
}
tcg_out_op(s, opc, &func_arg, &const_func_arg);
if (allocate_args) {
tcg_out_addi(s, TCG_REG_CALL_STACK, STACK_DIR(call_stack_size));
}
/* assign output registers and emit moves if needed */
for(i = 0; i < nb_oargs; i++) {
arg = args[i];
ts = &s->temps[arg];
reg = tcg_target_call_oarg_regs[i];
assert(s->reg_to_temp[reg] == -1);
if (ts->fixed_reg) {
if (ts->reg != reg) {
tcg_out_mov(s, ts->type, ts->reg, reg);
}
} else {
if (ts->val_type == TEMP_VAL_REG)
s->reg_to_temp[ts->reg] = -1;
ts->val_type = TEMP_VAL_REG;
ts->reg = reg;
ts->mem_coherent = 0;
s->reg_to_temp[reg] = arg;
}
}
return nb_iargs + nb_oargs + def->nb_cargs + 1;
}
#ifdef CONFIG_PROFILER
static int64_t tcg_table_op_count[NB_OPS];
static void dump_op_count(void)
{
int i;
FILE *f;
f = fopen("/tmp/op.log", "w");
for(i = INDEX_op_end; i < NB_OPS; i++) {
fprintf(f, "%s %" PRId64 "\n", tcg_op_defs[i].name, tcg_table_op_count[i]);
}
fclose(f);
}
#endif
static inline int tcg_gen_code_common(TCGContext *s, uint8_t *gen_code_buf,
intptr_t search_pc)
{
TCGOpcode opc;
int op_index;
const TCGOpDef *def;
unsigned int dead_iargs;
const TCGArg *args;
#ifdef DEBUG_DISAS
if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
qemu_log("OP:\n");
tcg_dump_ops(s, logfile);
qemu_log("\n");
}
#endif
#ifdef CONFIG_PROFILER
s->la_time -= profile_getclock();
#endif
tcg_liveness_analysis(s);
#ifdef CONFIG_PROFILER
s->la_time += profile_getclock();
#endif
#ifdef DEBUG_DISAS
# ifdef USE_LIVENESS_ANALYSIS /* vbox */
if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP_OPT))) {
qemu_log("OP after liveness analysis:\n");
tcg_dump_ops(s, logfile);
qemu_log("\n");
}
# endif /* USE_LIVENESS_ANALYSIS - vbox */
#endif
tcg_reg_alloc_start(s);
s->code_buf = gen_code_buf;
s->code_ptr = gen_code_buf;
args = gen_opparam_buf;
op_index = 0;
for(;;) {
opc = gen_opc_buf[op_index];
#ifdef CONFIG_PROFILER
tcg_table_op_count[opc]++;
#endif
def = &tcg_op_defs[opc];
#if 0
printf("%s: %d %d %d\n", def->name,
def->nb_oargs, def->nb_iargs, def->nb_cargs);
// dump_regs(s);
#endif
switch(opc) {
case INDEX_op_mov_i32:
#if TCG_TARGET_REG_BITS == 64
case INDEX_op_mov_i64:
#endif
dead_iargs = s->op_dead_iargs[op_index];
tcg_reg_alloc_mov(s, def, args, dead_iargs);
break;
case INDEX_op_movi_i32:
#if TCG_TARGET_REG_BITS == 64
case INDEX_op_movi_i64:
#endif
tcg_reg_alloc_movi(s, args);
break;
case INDEX_op_debug_insn_start:
/* debug instruction */
break;
case INDEX_op_nop:
case INDEX_op_nop1:
case INDEX_op_nop2:
case INDEX_op_nop3:
break;
case INDEX_op_nopn:
args += args[0];
goto next;
case INDEX_op_discard:
{
TCGTemp *ts;
ts = &s->temps[args[0]];
/* mark the temporary as dead */
if (!ts->fixed_reg) {
if (ts->val_type == TEMP_VAL_REG)
s->reg_to_temp[ts->reg] = -1;
ts->val_type = TEMP_VAL_DEAD;
}
}
break;
case INDEX_op_set_label:
tcg_reg_alloc_bb_end(s, s->reserved_regs);
tcg_out_label(s, args[0], (intptr_t)s->code_ptr);
break;
case INDEX_op_call:
dead_iargs = s->op_dead_iargs[op_index];
args += tcg_reg_alloc_call(s, def, opc, args, dead_iargs);
goto next;
case INDEX_op_end:
goto the_end;
default:
/* Note: in order to speed up the code, it would be much
faster to have specialized register allocator functions for
some common argument patterns */
dead_iargs = s->op_dead_iargs[op_index];
tcg_reg_alloc_op(s, def, opc, args, dead_iargs);
break;
}
args += def->nb_args;
next:
if (search_pc >= 0 && search_pc < s->code_ptr - gen_code_buf) {
return op_index;
}
op_index++;
#ifndef NDEBUG
check_regs(s);
#endif
}
the_end:
return -1;
}
int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf)
{
#ifdef CONFIG_PROFILER
{
int n;
n = (gen_opc_ptr - gen_opc_buf);
s->op_count += n;
if (n > s->op_count_max)
s->op_count_max = n;
s->temp_count += s->nb_temps;
if (s->nb_temps > s->temp_count_max)
s->temp_count_max = s->nb_temps;
}
#endif
tcg_gen_code_common(s, gen_code_buf, -1);
/* flush instruction cache */
flush_icache_range((uintptr_t)gen_code_buf,
(uintptr_t)s->code_ptr);
return s->code_ptr - gen_code_buf;
}
/* Return the index of the micro operation such as the pc after is <
offset bytes from the start of the TB. The contents of gen_code_buf must
not be changed, though writing the same values is ok.
Return -1 if not found. */
int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, intptr_t offset)
{
return tcg_gen_code_common(s, gen_code_buf, offset);
}
#ifdef CONFIG_PROFILER
void tcg_dump_info(FILE *f,
int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
{
TCGContext *s = &tcg_ctx;
int64_t tot;
tot = s->interm_time + s->code_time;
cpu_fprintf(f, "JIT cycles %" PRId64 " (%0.3f s at 2.4 GHz)\n",
tot, tot / 2.4e9);
cpu_fprintf(f, "translated TBs %" PRId64 " (aborted=%" PRId64 " %0.1f%%)\n",
s->tb_count,
s->tb_count1 - s->tb_count,
s->tb_count1 ? (double)(s->tb_count1 - s->tb_count) / s->tb_count1 * 100.0 : 0);
cpu_fprintf(f, "avg ops/TB %0.1f max=%d\n",
s->tb_count ? (double)s->op_count / s->tb_count : 0, s->op_count_max);
cpu_fprintf(f, "deleted ops/TB %0.2f\n",
s->tb_count ?
(double)s->del_op_count / s->tb_count : 0);
cpu_fprintf(f, "avg temps/TB %0.2f max=%d\n",
s->tb_count ?
(double)s->temp_count / s->tb_count : 0,
s->temp_count_max);
cpu_fprintf(f, "cycles/op %0.1f\n",
s->op_count ? (double)tot / s->op_count : 0);
cpu_fprintf(f, "cycles/in byte %0.1f\n",
s->code_in_len ? (double)tot / s->code_in_len : 0);
cpu_fprintf(f, "cycles/out byte %0.1f\n",
s->code_out_len ? (double)tot / s->code_out_len : 0);
if (tot == 0)
tot = 1;
cpu_fprintf(f, " gen_interm time %0.1f%%\n",
(double)s->interm_time / tot * 100.0);
cpu_fprintf(f, " gen_code time %0.1f%%\n",
(double)s->code_time / tot * 100.0);
cpu_fprintf(f, "liveness/code time %0.1f%%\n",
(double)s->la_time / (s->code_time ? s->code_time : 1) * 100.0);
cpu_fprintf(f, "cpu_restore count %" PRId64 "\n",
s->restore_count);
cpu_fprintf(f, " avg cycles %0.1f\n",
s->restore_count ? (double)s->restore_time / s->restore_count : 0);
dump_op_count();
}
#else
void tcg_dump_info(FILE *f,
int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
{
cpu_fprintf(f, "[TCG profiler not compiled]\n");
}
#endif