translate-all.c 4.2 KB
Newer Older
B
bellard 已提交
1 2
/*
 *  Host code generation
3
 *
B
bellard 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
B
bellard 已提交
18 19 20 21 22 23 24 25
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "config.h"
B
bellard 已提交
26

B
bellard 已提交
27
#define NO_CPU_IO_DEFS
B
bellard 已提交
28
#include "cpu.h"
B
bellard 已提交
29
#include "disas.h"
B
bellard 已提交
30
#include "tcg.h"
B
Blue Swirl 已提交
31
#include "qemu-timer.h"
B
bellard 已提交
32

B
bellard 已提交
33 34
/* code generation context */
TCGContext tcg_ctx;
B
bellard 已提交
35 36

uint16_t gen_opc_buf[OPC_BUF_SIZE];
B
bellard 已提交
37
TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
B
bellard 已提交
38 39

target_ulong gen_opc_pc[OPC_BUF_SIZE];
P
pbrook 已提交
40
uint16_t gen_opc_icount[OPC_BUF_SIZE];
B
bellard 已提交
41 42
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];

B
bellard 已提交
43 44 45 46 47
void cpu_gen_init(void)
{
    tcg_context_init(&tcg_ctx); 
}

B
bellard 已提交
48
/* return non zero if the very first instruction is invalid so that
49
   the virtual CPU can trigger an exception.
B
bellard 已提交
50 51 52 53

   '*gen_code_size_ptr' contains the size of the generated code (host
   code).
*/
54
int cpu_gen_code(CPUArchState *env, TranslationBlock *tb, int *gen_code_size_ptr)
B
bellard 已提交
55
{
B
bellard 已提交
56
    TCGContext *s = &tcg_ctx;
B
bellard 已提交
57 58
    uint8_t *gen_code_buf;
    int gen_code_size;
B
bellard 已提交
59 60 61 62 63
#ifdef CONFIG_PROFILER
    int64_t ti;
#endif

#ifdef CONFIG_PROFILER
B
bellard 已提交
64 65
    s->tb_count1++; /* includes aborted translations because of
                       exceptions */
B
bellard 已提交
66 67 68
    ti = profile_getclock();
#endif
    tcg_func_start(s);
B
bellard 已提交
69

70 71
    gen_intermediate_code(env, tb);

B
bellard 已提交
72
    /* generate machine code */
B
bellard 已提交
73
    gen_code_buf = tb->tc_ptr;
B
bellard 已提交
74 75
    tb->tb_next_offset[0] = 0xffff;
    tb->tb_next_offset[1] = 0xffff;
B
bellard 已提交
76
    s->tb_next_offset = tb->tb_next_offset;
77
#ifdef USE_DIRECT_JUMP
B
bellard 已提交
78 79
    s->tb_jmp_offset = tb->tb_jmp_offset;
    s->tb_next = NULL;
B
bellard 已提交
80
#else
B
bellard 已提交
81 82
    s->tb_jmp_offset = NULL;
    s->tb_next = tb->tb_next;
B
bellard 已提交
83
#endif
B
bellard 已提交
84 85

#ifdef CONFIG_PROFILER
B
bellard 已提交
86 87 88
    s->tb_count++;
    s->interm_time += profile_getclock() - ti;
    s->code_time -= profile_getclock();
B
bellard 已提交
89
#endif
A
aurel32 已提交
90
    gen_code_size = tcg_gen_code(s, gen_code_buf);
B
bellard 已提交
91
    *gen_code_size_ptr = gen_code_size;
B
bellard 已提交
92
#ifdef CONFIG_PROFILER
B
bellard 已提交
93 94 95
    s->code_time += profile_getclock();
    s->code_in_len += tb->size;
    s->code_out_len += gen_code_size;
B
bellard 已提交
96 97
#endif

B
bellard 已提交
98
#ifdef DEBUG_DISAS
99
    if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
100 101 102
        qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
        log_disas(tb->tc_ptr, *gen_code_size_ptr);
        qemu_log("\n");
103
        qemu_log_flush();
B
bellard 已提交
104 105 106 107 108
    }
#endif
    return 0;
}

109
/* The cpu state corresponding to 'searched_pc' is restored.
B
bellard 已提交
110
 */
111
int cpu_restore_state(TranslationBlock *tb,
112
                      CPUArchState *env, unsigned long searched_pc)
B
bellard 已提交
113
{
B
bellard 已提交
114 115
    TCGContext *s = &tcg_ctx;
    int j;
B
bellard 已提交
116
    unsigned long tc_ptr;
B
bellard 已提交
117 118 119 120 121 122 123 124
#ifdef CONFIG_PROFILER
    int64_t ti;
#endif

#ifdef CONFIG_PROFILER
    ti = profile_getclock();
#endif
    tcg_func_start(s);
B
bellard 已提交
125

126
    gen_intermediate_code_pc(env, tb);
127

P
pbrook 已提交
128 129 130 131 132 133 134
    if (use_icount) {
        /* Reset the cycle counter to the start of the block.  */
        env->icount_decr.u16.low += tb->icount;
        /* Clear the IO flag.  */
        env->can_do_io = 0;
    }

B
bellard 已提交
135 136 137 138
    /* find opc index corresponding to search_pc */
    tc_ptr = (unsigned long)tb->tc_ptr;
    if (searched_pc < tc_ptr)
        return -1;
B
bellard 已提交
139 140 141 142 143 144 145 146 147

    s->tb_next_offset = tb->tb_next_offset;
#ifdef USE_DIRECT_JUMP
    s->tb_jmp_offset = tb->tb_jmp_offset;
    s->tb_next = NULL;
#else
    s->tb_jmp_offset = NULL;
    s->tb_next = tb->tb_next;
#endif
A
aurel32 已提交
148
    j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
B
bellard 已提交
149 150
    if (j < 0)
        return -1;
B
bellard 已提交
151 152 153
    /* now find start of instruction before */
    while (gen_opc_instr_start[j] == 0)
        j--;
P
pbrook 已提交
154
    env->icount_decr.u16.low -= gen_opc_icount[j];
155

156
    restore_state_to_opc(env, tb, j);
B
bellard 已提交
157 158

#ifdef CONFIG_PROFILER
B
bellard 已提交
159 160
    s->restore_time += profile_getclock() - ti;
    s->restore_count++;
B
bellard 已提交
161
#endif
B
bellard 已提交
162 163
    return 0;
}