Statistics
| Branch: | Revision:

root / translate-all.c @ efc0a514

History | View | Annotate | Download (4.9 kB)

1
/*
2
 *  Host code generation
3
 *
4
 *  Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2 of the License, or (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * 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/>.
18
 */
19
#include <stdarg.h>
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <inttypes.h>
24

    
25
#include "config.h"
26

    
27
#define NO_CPU_IO_DEFS
28
#include "cpu.h"
29
#include "exec-all.h"
30
#include "disas.h"
31
#include "tcg.h"
32
#include "qemu-timer.h"
33

    
34
/* code generation context */
35
TCGContext tcg_ctx;
36

    
37
uint16_t gen_opc_buf[OPC_BUF_SIZE];
38
TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
39

    
40
target_ulong gen_opc_pc[OPC_BUF_SIZE];
41
uint16_t gen_opc_icount[OPC_BUF_SIZE];
42
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
43
#if defined(TARGET_I386)
44
uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
45
#elif defined(TARGET_SPARC)
46
target_ulong gen_opc_npc[OPC_BUF_SIZE];
47
target_ulong gen_opc_jump_pc[2];
48
#elif defined(TARGET_MIPS) || defined(TARGET_SH4)
49
uint32_t gen_opc_hflags[OPC_BUF_SIZE];
50
#endif
51

    
52
/* XXX: suppress that */
53
unsigned long code_gen_max_block_size(void)
54
{
55
    static unsigned long max;
56

    
57
    if (max == 0) {
58
        max = TCG_MAX_OP_SIZE;
59
#define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
60
#include "tcg-opc.h"
61
#undef DEF
62
        max *= OPC_MAX_SIZE;
63
    }
64

    
65
    return max;
66
}
67

    
68
void cpu_gen_init(void)
69
{
70
    tcg_context_init(&tcg_ctx); 
71
    tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
72
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
73
}
74

    
75
/* return non zero if the very first instruction is invalid so that
76
   the virtual CPU can trigger an exception.
77

78
   '*gen_code_size_ptr' contains the size of the generated code (host
79
   code).
80
*/
81
int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
82
{
83
    TCGContext *s = &tcg_ctx;
84
    uint8_t *gen_code_buf;
85
    int gen_code_size;
86
#ifdef CONFIG_PROFILER
87
    int64_t ti;
88
#endif
89

    
90
#ifdef CONFIG_PROFILER
91
    s->tb_count1++; /* includes aborted translations because of
92
                       exceptions */
93
    ti = profile_getclock();
94
#endif
95
    tcg_func_start(s);
96

    
97
    gen_intermediate_code(env, tb);
98

    
99
    /* generate machine code */
100
    gen_code_buf = tb->tc_ptr;
101
    tb->tb_next_offset[0] = 0xffff;
102
    tb->tb_next_offset[1] = 0xffff;
103
    s->tb_next_offset = tb->tb_next_offset;
104
#ifdef USE_DIRECT_JUMP
105
    s->tb_jmp_offset = tb->tb_jmp_offset;
106
    s->tb_next = NULL;
107
#else
108
    s->tb_jmp_offset = NULL;
109
    s->tb_next = tb->tb_next;
110
#endif
111

    
112
#ifdef CONFIG_PROFILER
113
    s->tb_count++;
114
    s->interm_time += profile_getclock() - ti;
115
    s->code_time -= profile_getclock();
116
#endif
117
    gen_code_size = tcg_gen_code(s, gen_code_buf);
118
    *gen_code_size_ptr = gen_code_size;
119
#ifdef CONFIG_PROFILER
120
    s->code_time += profile_getclock();
121
    s->code_in_len += tb->size;
122
    s->code_out_len += gen_code_size;
123
#endif
124

    
125
#ifdef DEBUG_DISAS
126
    if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
127
        qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
128
        log_disas(tb->tc_ptr, *gen_code_size_ptr);
129
        qemu_log("\n");
130
        qemu_log_flush();
131
    }
132
#endif
133
    return 0;
134
}
135

    
136
/* The cpu state corresponding to 'searched_pc' is restored.
137
 */
138
int cpu_restore_state(TranslationBlock *tb,
139
                      CPUState *env, unsigned long searched_pc,
140
                      void *puc)
141
{
142
    TCGContext *s = &tcg_ctx;
143
    int j;
144
    unsigned long tc_ptr;
145
#ifdef CONFIG_PROFILER
146
    int64_t ti;
147
#endif
148

    
149
#ifdef CONFIG_PROFILER
150
    ti = profile_getclock();
151
#endif
152
    tcg_func_start(s);
153

    
154
    gen_intermediate_code_pc(env, tb);
155

    
156
    if (use_icount) {
157
        /* Reset the cycle counter to the start of the block.  */
158
        env->icount_decr.u16.low += tb->icount;
159
        /* Clear the IO flag.  */
160
        env->can_do_io = 0;
161
    }
162

    
163
    /* find opc index corresponding to search_pc */
164
    tc_ptr = (unsigned long)tb->tc_ptr;
165
    if (searched_pc < tc_ptr)
166
        return -1;
167

    
168
    s->tb_next_offset = tb->tb_next_offset;
169
#ifdef USE_DIRECT_JUMP
170
    s->tb_jmp_offset = tb->tb_jmp_offset;
171
    s->tb_next = NULL;
172
#else
173
    s->tb_jmp_offset = NULL;
174
    s->tb_next = tb->tb_next;
175
#endif
176
    j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
177
    if (j < 0)
178
        return -1;
179
    /* now find start of instruction before */
180
    while (gen_opc_instr_start[j] == 0)
181
        j--;
182
    env->icount_decr.u16.low -= gen_opc_icount[j];
183

    
184
    gen_pc_load(env, tb, searched_pc, j, puc);
185

    
186
#ifdef CONFIG_PROFILER
187
    s->restore_time += profile_getclock() - ti;
188
    s->restore_count++;
189
#endif
190
    return 0;
191
}