translate-all.c 4.1 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

B
bellard 已提交
36
target_ulong gen_opc_pc[OPC_BUF_SIZE];
P
pbrook 已提交
37
uint16_t gen_opc_icount[OPC_BUF_SIZE];
B
bellard 已提交
38 39
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];

B
bellard 已提交
40 41 42 43 44
void cpu_gen_init(void)
{
    tcg_context_init(&tcg_ctx); 
}

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

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

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

67 68
    gen_intermediate_code(env, tb);

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

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

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

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

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

123
    gen_intermediate_code_pc(env, tb);
124

P
pbrook 已提交
125 126 127 128 129 130 131
    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 已提交
132
    /* find opc index corresponding to search_pc */
133
    tc_ptr = (uintptr_t)tb->tc_ptr;
B
bellard 已提交
134 135
    if (searched_pc < tc_ptr)
        return -1;
B
bellard 已提交
136 137 138 139 140 141 142 143 144

    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 已提交
145
    j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
B
bellard 已提交
146 147
    if (j < 0)
        return -1;
B
bellard 已提交
148 149 150
    /* now find start of instruction before */
    while (gen_opc_instr_start[j] == 0)
        j--;
P
pbrook 已提交
151
    env->icount_decr.u16.low -= gen_opc_icount[j];
152

153
    restore_state_to_opc(env, tb, j);
B
bellard 已提交
154 155

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