Statistics
| Branch: | Revision:

root / translate-all.c @ 486bd5a2

History | View | Annotate | Download (5.1 kB)

1 d19893da bellard
/*
2 d19893da bellard
 *  Host code generation
3 5fafdf24 ths
 *
4 d19893da bellard
 *  Copyright (c) 2003 Fabrice Bellard
5 d19893da bellard
 *
6 d19893da bellard
 * This library is free software; you can redistribute it and/or
7 d19893da bellard
 * modify it under the terms of the GNU Lesser General Public
8 d19893da bellard
 * License as published by the Free Software Foundation; either
9 d19893da bellard
 * version 2 of the License, or (at your option) any later version.
10 d19893da bellard
 *
11 d19893da bellard
 * This library is distributed in the hope that it will be useful,
12 d19893da bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 d19893da bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 d19893da bellard
 * Lesser General Public License for more details.
15 d19893da bellard
 *
16 d19893da bellard
 * You should have received a copy of the GNU Lesser General Public
17 d19893da bellard
 * License along with this library; if not, write to the Free Software
18 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19 d19893da bellard
 */
20 d19893da bellard
#include <stdarg.h>
21 d19893da bellard
#include <stdlib.h>
22 d19893da bellard
#include <stdio.h>
23 d19893da bellard
#include <string.h>
24 d19893da bellard
#include <inttypes.h>
25 d19893da bellard
26 d19893da bellard
#include "config.h"
27 2054396a bellard
28 af5ad107 bellard
#define NO_CPU_IO_DEFS
29 d3eead2e bellard
#include "cpu.h"
30 d3eead2e bellard
#include "exec-all.h"
31 d19893da bellard
#include "disas.h"
32 57fec1fe bellard
#include "tcg.h"
33 d19893da bellard
34 57fec1fe bellard
/* code generation context */
35 57fec1fe bellard
TCGContext tcg_ctx;
36 d19893da bellard
37 d19893da bellard
uint16_t gen_opc_buf[OPC_BUF_SIZE];
38 57fec1fe bellard
TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
39 c4687878 bellard
40 c4687878 bellard
target_ulong gen_opc_pc[OPC_BUF_SIZE];
41 2e70f6ef pbrook
uint16_t gen_opc_icount[OPC_BUF_SIZE];
42 d19893da bellard
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
43 f76af4b3 bellard
#if defined(TARGET_I386)
44 f76af4b3 bellard
uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
45 e95c8d51 bellard
#elif defined(TARGET_SPARC)
46 c4687878 bellard
target_ulong gen_opc_npc[OPC_BUF_SIZE];
47 c3278b7b bellard
target_ulong gen_opc_jump_pc[2];
48 823029f9 ths
#elif defined(TARGET_MIPS) || defined(TARGET_SH4)
49 30d6cb84 bellard
uint32_t gen_opc_hflags[OPC_BUF_SIZE];
50 f76af4b3 bellard
#endif
51 d19893da bellard
52 57fec1fe bellard
/* XXX: suppress that */
53 d07bde88 blueswir1
unsigned long code_gen_max_block_size(void)
54 d07bde88 blueswir1
{
55 d07bde88 blueswir1
    static unsigned long max;
56 d07bde88 blueswir1
57 d07bde88 blueswir1
    if (max == 0) {
58 a208e54a pbrook
        max = TCG_MAX_OP_SIZE;
59 d07bde88 blueswir1
#define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
60 57fec1fe bellard
#include "tcg-opc.h"
61 d07bde88 blueswir1
#undef DEF
62 d07bde88 blueswir1
        max *= OPC_MAX_SIZE;
63 d07bde88 blueswir1
    }
64 d07bde88 blueswir1
65 d07bde88 blueswir1
    return max;
66 d07bde88 blueswir1
}
67 d07bde88 blueswir1
68 57fec1fe bellard
void cpu_gen_init(void)
69 57fec1fe bellard
{
70 57fec1fe bellard
    tcg_context_init(&tcg_ctx); 
71 57fec1fe bellard
    tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
72 a20e31dc blueswir1
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
73 57fec1fe bellard
}
74 57fec1fe bellard
75 d19893da bellard
/* return non zero if the very first instruction is invalid so that
76 5fafdf24 ths
   the virtual CPU can trigger an exception.
77 d19893da bellard

78 d19893da bellard
   '*gen_code_size_ptr' contains the size of the generated code (host
79 d19893da bellard
   code).
80 d19893da bellard
*/
81 d07bde88 blueswir1
int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
82 d19893da bellard
{
83 57fec1fe bellard
    TCGContext *s = &tcg_ctx;
84 d19893da bellard
    uint8_t *gen_code_buf;
85 d19893da bellard
    int gen_code_size;
86 57fec1fe bellard
#ifdef CONFIG_PROFILER
87 57fec1fe bellard
    int64_t ti;
88 57fec1fe bellard
#endif
89 57fec1fe bellard
90 57fec1fe bellard
#ifdef CONFIG_PROFILER
91 b67d9a52 bellard
    s->tb_count1++; /* includes aborted translations because of
92 b67d9a52 bellard
                       exceptions */
93 57fec1fe bellard
    ti = profile_getclock();
94 57fec1fe bellard
#endif
95 57fec1fe bellard
    tcg_func_start(s);
96 d19893da bellard
97 2cfc5f17 ths
    gen_intermediate_code(env, tb);
98 2cfc5f17 ths
99 ec6338ba bellard
    /* generate machine code */
100 57fec1fe bellard
    gen_code_buf = tb->tc_ptr;
101 ec6338ba bellard
    tb->tb_next_offset[0] = 0xffff;
102 ec6338ba bellard
    tb->tb_next_offset[1] = 0xffff;
103 57fec1fe bellard
    s->tb_next_offset = tb->tb_next_offset;
104 4cbb86e1 bellard
#ifdef USE_DIRECT_JUMP
105 57fec1fe bellard
    s->tb_jmp_offset = tb->tb_jmp_offset;
106 57fec1fe bellard
    s->tb_next = NULL;
107 ec6338ba bellard
    /* the following two entries are optional (only used for string ops) */
108 57fec1fe bellard
    /* XXX: not used ? */
109 ec6338ba bellard
    tb->tb_jmp_offset[2] = 0xffff;
110 ec6338ba bellard
    tb->tb_jmp_offset[3] = 0xffff;
111 d19893da bellard
#else
112 57fec1fe bellard
    s->tb_jmp_offset = NULL;
113 57fec1fe bellard
    s->tb_next = tb->tb_next;
114 d19893da bellard
#endif
115 57fec1fe bellard
116 57fec1fe bellard
#ifdef CONFIG_PROFILER
117 b67d9a52 bellard
    s->tb_count++;
118 b67d9a52 bellard
    s->interm_time += profile_getclock() - ti;
119 b67d9a52 bellard
    s->code_time -= profile_getclock();
120 57fec1fe bellard
#endif
121 54604f74 aurel32
    gen_code_size = tcg_gen_code(s, gen_code_buf);
122 d19893da bellard
    *gen_code_size_ptr = gen_code_size;
123 57fec1fe bellard
#ifdef CONFIG_PROFILER
124 b67d9a52 bellard
    s->code_time += profile_getclock();
125 b67d9a52 bellard
    s->code_in_len += tb->size;
126 b67d9a52 bellard
    s->code_out_len += gen_code_size;
127 57fec1fe bellard
#endif
128 57fec1fe bellard
129 d19893da bellard
#ifdef DEBUG_DISAS
130 8fec2b8c aliguori
    if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
131 93fcfe39 aliguori
        qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
132 93fcfe39 aliguori
        log_disas(tb->tc_ptr, *gen_code_size_ptr);
133 93fcfe39 aliguori
        qemu_log("\n");
134 31b1a7b4 aliguori
        qemu_log_flush();
135 d19893da bellard
    }
136 d19893da bellard
#endif
137 d19893da bellard
    return 0;
138 d19893da bellard
}
139 d19893da bellard
140 5fafdf24 ths
/* The cpu state corresponding to 'searched_pc' is restored.
141 d19893da bellard
 */
142 5fafdf24 ths
int cpu_restore_state(TranslationBlock *tb,
143 58fe2f10 bellard
                      CPUState *env, unsigned long searched_pc,
144 58fe2f10 bellard
                      void *puc)
145 d19893da bellard
{
146 57fec1fe bellard
    TCGContext *s = &tcg_ctx;
147 57fec1fe bellard
    int j;
148 d19893da bellard
    unsigned long tc_ptr;
149 57fec1fe bellard
#ifdef CONFIG_PROFILER
150 57fec1fe bellard
    int64_t ti;
151 57fec1fe bellard
#endif
152 57fec1fe bellard
153 57fec1fe bellard
#ifdef CONFIG_PROFILER
154 57fec1fe bellard
    ti = profile_getclock();
155 57fec1fe bellard
#endif
156 57fec1fe bellard
    tcg_func_start(s);
157 d19893da bellard
158 2cfc5f17 ths
    gen_intermediate_code_pc(env, tb);
159 3b46e624 ths
160 2e70f6ef pbrook
    if (use_icount) {
161 2e70f6ef pbrook
        /* Reset the cycle counter to the start of the block.  */
162 2e70f6ef pbrook
        env->icount_decr.u16.low += tb->icount;
163 2e70f6ef pbrook
        /* Clear the IO flag.  */
164 2e70f6ef pbrook
        env->can_do_io = 0;
165 2e70f6ef pbrook
    }
166 2e70f6ef pbrook
167 d19893da bellard
    /* find opc index corresponding to search_pc */
168 d19893da bellard
    tc_ptr = (unsigned long)tb->tc_ptr;
169 d19893da bellard
    if (searched_pc < tc_ptr)
170 d19893da bellard
        return -1;
171 57fec1fe bellard
172 57fec1fe bellard
    s->tb_next_offset = tb->tb_next_offset;
173 57fec1fe bellard
#ifdef USE_DIRECT_JUMP
174 57fec1fe bellard
    s->tb_jmp_offset = tb->tb_jmp_offset;
175 57fec1fe bellard
    s->tb_next = NULL;
176 57fec1fe bellard
#else
177 57fec1fe bellard
    s->tb_jmp_offset = NULL;
178 57fec1fe bellard
    s->tb_next = tb->tb_next;
179 57fec1fe bellard
#endif
180 54604f74 aurel32
    j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
181 57fec1fe bellard
    if (j < 0)
182 57fec1fe bellard
        return -1;
183 d19893da bellard
    /* now find start of instruction before */
184 d19893da bellard
    while (gen_opc_instr_start[j] == 0)
185 d19893da bellard
        j--;
186 2e70f6ef pbrook
    env->icount_decr.u16.low -= gen_opc_icount[j];
187 3b46e624 ths
188 d2856f1a aurel32
    gen_pc_load(env, tb, searched_pc, j, puc);
189 57fec1fe bellard
190 57fec1fe bellard
#ifdef CONFIG_PROFILER
191 b67d9a52 bellard
    s->restore_time += profile_getclock() - ti;
192 b67d9a52 bellard
    s->restore_count++;
193 57fec1fe bellard
#endif
194 d19893da bellard
    return 0;
195 d19893da bellard
}