Statistics
| Branch: | Revision:

root / translate-all.c @ 7863667f

History | View | Annotate | Download (8.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 d19893da bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 d19893da bellard
33 4f716dc6 bellard
extern int dyngen_code(uint8_t *gen_code_buf,
34 4f716dc6 bellard
                       uint16_t *label_offsets, uint16_t *jmp_offsets,
35 4f716dc6 bellard
                       const uint16_t *opc_buf, const uint32_t *opparam_buf, const long *gen_labels);
36 4f716dc6 bellard
37 d19893da bellard
enum {
38 d19893da bellard
#define DEF(s, n, copy_size) INDEX_op_ ## s,
39 d3eead2e bellard
#include "opc.h"
40 d19893da bellard
#undef DEF
41 d19893da bellard
    NB_OPS,
42 d19893da bellard
};
43 d19893da bellard
44 d19893da bellard
uint16_t gen_opc_buf[OPC_BUF_SIZE];
45 d19893da bellard
uint32_t gen_opparam_buf[OPPARAM_BUF_SIZE];
46 c4687878 bellard
long gen_labels[OPC_BUF_SIZE];
47 c4687878 bellard
int nb_gen_labels;
48 c4687878 bellard
49 c4687878 bellard
target_ulong gen_opc_pc[OPC_BUF_SIZE];
50 d19893da bellard
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
51 f76af4b3 bellard
#if defined(TARGET_I386)
52 f76af4b3 bellard
uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
53 e95c8d51 bellard
#elif defined(TARGET_SPARC)
54 c4687878 bellard
target_ulong gen_opc_npc[OPC_BUF_SIZE];
55 c3278b7b bellard
target_ulong gen_opc_jump_pc[2];
56 30d6cb84 bellard
#elif defined(TARGET_MIPS)
57 30d6cb84 bellard
uint32_t gen_opc_hflags[OPC_BUF_SIZE];
58 f76af4b3 bellard
#endif
59 d19893da bellard
60 58fe2f10 bellard
int code_copy_enabled = 1;
61 58fe2f10 bellard
62 d19893da bellard
#ifdef DEBUG_DISAS
63 d19893da bellard
static const char *op_str[] = {
64 d19893da bellard
#define DEF(s, n, copy_size) #s,
65 d3eead2e bellard
#include "opc.h"
66 d19893da bellard
#undef DEF
67 d19893da bellard
};
68 d19893da bellard
69 d19893da bellard
static uint8_t op_nb_args[] = {
70 d19893da bellard
#define DEF(s, n, copy_size) n,
71 d3eead2e bellard
#include "opc.h"
72 d19893da bellard
#undef DEF
73 d19893da bellard
};
74 d19893da bellard
75 c4687878 bellard
static const unsigned short opc_copy_size[] = {
76 c4687878 bellard
#define DEF(s, n, copy_size) copy_size,
77 c4687878 bellard
#include "opc.h"
78 c4687878 bellard
#undef DEF
79 c4687878 bellard
};
80 c4687878 bellard
81 d19893da bellard
void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf)
82 d19893da bellard
{
83 d19893da bellard
    const uint16_t *opc_ptr;
84 d19893da bellard
    const uint32_t *opparam_ptr;
85 d19893da bellard
    int c, n, i;
86 d19893da bellard
87 d19893da bellard
    opc_ptr = opc_buf;
88 d19893da bellard
    opparam_ptr = opparam_buf;
89 d19893da bellard
    for(;;) {
90 d19893da bellard
        c = *opc_ptr++;
91 d19893da bellard
        n = op_nb_args[c];
92 5fafdf24 ths
        fprintf(logfile, "0x%04x: %s",
93 d19893da bellard
                (int)(opc_ptr - opc_buf - 1), op_str[c]);
94 d19893da bellard
        for(i = 0; i < n; i++) {
95 d19893da bellard
            fprintf(logfile, " 0x%x", opparam_ptr[i]);
96 d19893da bellard
        }
97 d19893da bellard
        fprintf(logfile, "\n");
98 d19893da bellard
        if (c == INDEX_op_end)
99 d19893da bellard
            break;
100 d19893da bellard
        opparam_ptr += n;
101 d19893da bellard
    }
102 d19893da bellard
}
103 d19893da bellard
104 d19893da bellard
#endif
105 d19893da bellard
106 c4687878 bellard
/* compute label info */
107 c4687878 bellard
static void dyngen_labels(long *gen_labels, int nb_gen_labels,
108 c4687878 bellard
                          uint8_t *gen_code_buf, const uint16_t *opc_buf)
109 c4687878 bellard
{
110 c4687878 bellard
    uint8_t *gen_code_ptr;
111 c4687878 bellard
    int c, i;
112 c4687878 bellard
    unsigned long gen_code_addr[OPC_BUF_SIZE];
113 3b46e624 ths
114 c4687878 bellard
    if (nb_gen_labels == 0)
115 c4687878 bellard
        return;
116 c4687878 bellard
    /* compute the address of each op code */
117 3b46e624 ths
118 c4687878 bellard
    gen_code_ptr = gen_code_buf;
119 c4687878 bellard
    i = 0;
120 c4687878 bellard
    for(;;) {
121 c4687878 bellard
        c = opc_buf[i];
122 c4687878 bellard
        gen_code_addr[i] =(unsigned long)gen_code_ptr;
123 c4687878 bellard
        if (c == INDEX_op_end)
124 c4687878 bellard
            break;
125 c4687878 bellard
        gen_code_ptr += opc_copy_size[c];
126 c4687878 bellard
        i++;
127 c4687878 bellard
    }
128 3b46e624 ths
129 c4687878 bellard
    /* compute the address of each label */
130 c4687878 bellard
    for(i = 0; i < nb_gen_labels; i++) {
131 c4687878 bellard
        gen_labels[i] = gen_code_addr[gen_labels[i]];
132 c4687878 bellard
    }
133 c4687878 bellard
}
134 c4687878 bellard
135 d19893da bellard
/* return non zero if the very first instruction is invalid so that
136 5fafdf24 ths
   the virtual CPU can trigger an exception.
137 d19893da bellard

138 d19893da bellard
   '*gen_code_size_ptr' contains the size of the generated code (host
139 d19893da bellard
   code).
140 d19893da bellard
*/
141 4c3a88a2 bellard
int cpu_gen_code(CPUState *env, TranslationBlock *tb,
142 d19893da bellard
                 int max_code_size, int *gen_code_size_ptr)
143 d19893da bellard
{
144 d19893da bellard
    uint8_t *gen_code_buf;
145 d19893da bellard
    int gen_code_size;
146 d19893da bellard
147 ec6338ba bellard
    if (gen_intermediate_code(env, tb) < 0)
148 ec6338ba bellard
        return -1;
149 ec6338ba bellard
    
150 ec6338ba bellard
    /* generate machine code */
151 ec6338ba bellard
    tb->tb_next_offset[0] = 0xffff;
152 ec6338ba bellard
    tb->tb_next_offset[1] = 0xffff;
153 ec6338ba bellard
    gen_code_buf = tb->tc_ptr;
154 4cbb86e1 bellard
#ifdef USE_DIRECT_JUMP
155 ec6338ba bellard
    /* the following two entries are optional (only used for string ops) */
156 ec6338ba bellard
    tb->tb_jmp_offset[2] = 0xffff;
157 ec6338ba bellard
    tb->tb_jmp_offset[3] = 0xffff;
158 4cbb86e1 bellard
#endif
159 ec6338ba bellard
    dyngen_labels(gen_labels, nb_gen_labels, gen_code_buf, gen_opc_buf);
160 ec6338ba bellard
    
161 ec6338ba bellard
    gen_code_size = dyngen_code(gen_code_buf, tb->tb_next_offset,
162 d19893da bellard
#ifdef USE_DIRECT_JUMP
163 ec6338ba bellard
                                tb->tb_jmp_offset,
164 d19893da bellard
#else
165 ec6338ba bellard
                                NULL,
166 d19893da bellard
#endif
167 ec6338ba bellard
                                gen_opc_buf, gen_opparam_buf, gen_labels);
168 d19893da bellard
    *gen_code_size_ptr = gen_code_size;
169 d19893da bellard
#ifdef DEBUG_DISAS
170 f193c797 bellard
    if (loglevel & CPU_LOG_TB_OUT_ASM) {
171 d19893da bellard
        fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
172 c4687878 bellard
        disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
173 d19893da bellard
        fprintf(logfile, "\n");
174 d19893da bellard
        fflush(logfile);
175 d19893da bellard
    }
176 d19893da bellard
#endif
177 d19893da bellard
    return 0;
178 d19893da bellard
}
179 d19893da bellard
180 5fafdf24 ths
/* The cpu state corresponding to 'searched_pc' is restored.
181 d19893da bellard
 */
182 5fafdf24 ths
int cpu_restore_state(TranslationBlock *tb,
183 58fe2f10 bellard
                      CPUState *env, unsigned long searched_pc,
184 58fe2f10 bellard
                      void *puc)
185 d19893da bellard
{
186 d19893da bellard
    int j, c;
187 d19893da bellard
    unsigned long tc_ptr;
188 d19893da bellard
    uint16_t *opc_ptr;
189 d19893da bellard
190 4c3a88a2 bellard
    if (gen_intermediate_code_pc(env, tb) < 0)
191 d19893da bellard
        return -1;
192 3b46e624 ths
193 d19893da bellard
    /* find opc index corresponding to search_pc */
194 d19893da bellard
    tc_ptr = (unsigned long)tb->tc_ptr;
195 d19893da bellard
    if (searched_pc < tc_ptr)
196 d19893da bellard
        return -1;
197 d19893da bellard
    j = 0;
198 d19893da bellard
    opc_ptr = gen_opc_buf;
199 d19893da bellard
    for(;;) {
200 d19893da bellard
        c = *opc_ptr;
201 d19893da bellard
        if (c == INDEX_op_end)
202 d19893da bellard
            return -1;
203 d19893da bellard
        tc_ptr += opc_copy_size[c];
204 d19893da bellard
        if (searched_pc < tc_ptr)
205 d19893da bellard
            break;
206 d19893da bellard
        opc_ptr++;
207 d19893da bellard
    }
208 d19893da bellard
    j = opc_ptr - gen_opc_buf;
209 d19893da bellard
    /* now find start of instruction before */
210 d19893da bellard
    while (gen_opc_instr_start[j] == 0)
211 d19893da bellard
        j--;
212 f76af4b3 bellard
#if defined(TARGET_I386)
213 f76af4b3 bellard
    {
214 f76af4b3 bellard
        int cc_op;
215 3c1cf9fa bellard
#ifdef DEBUG_DISAS
216 f193c797 bellard
        if (loglevel & CPU_LOG_TB_OP) {
217 3c1cf9fa bellard
            int i;
218 6e0374f6 bellard
            fprintf(logfile, "RESTORE:\n");
219 3c1cf9fa bellard
            for(i=0;i<=j; i++) {
220 3c1cf9fa bellard
                if (gen_opc_instr_start[i]) {
221 c4687878 bellard
                    fprintf(logfile, "0x%04x: " TARGET_FMT_lx "\n", i, gen_opc_pc[i]);
222 3c1cf9fa bellard
                }
223 3c1cf9fa bellard
            }
224 5fafdf24 ths
            fprintf(logfile, "spc=0x%08lx j=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
225 5fafdf24 ths
                    searched_pc, j, gen_opc_pc[j] - tb->cs_base,
226 c4687878 bellard
                    (uint32_t)tb->cs_base);
227 3c1cf9fa bellard
        }
228 3c1cf9fa bellard
#endif
229 f76af4b3 bellard
        env->eip = gen_opc_pc[j] - tb->cs_base;
230 f76af4b3 bellard
        cc_op = gen_opc_cc_op[j];
231 f76af4b3 bellard
        if (cc_op != CC_OP_DYNAMIC)
232 f76af4b3 bellard
            env->cc_op = cc_op;
233 f76af4b3 bellard
    }
234 f76af4b3 bellard
#elif defined(TARGET_ARM)
235 f76af4b3 bellard
    env->regs[15] = gen_opc_pc[j];
236 d3eead2e bellard
#elif defined(TARGET_SPARC)
237 c3278b7b bellard
    {
238 c3278b7b bellard
        target_ulong npc;
239 c3278b7b bellard
        env->pc = gen_opc_pc[j];
240 c3278b7b bellard
        npc = gen_opc_npc[j];
241 c3278b7b bellard
        if (npc == 1) {
242 c3278b7b bellard
            /* dynamic NPC: already stored */
243 c3278b7b bellard
        } else if (npc == 2) {
244 745cacc7 bellard
            target_ulong t2 = (target_ulong)(unsigned long)puc;
245 c3278b7b bellard
            /* jump PC: use T2 and the jump targets of the translation */
246 5fafdf24 ths
            if (t2)
247 c3278b7b bellard
                env->npc = gen_opc_jump_pc[0];
248 c3278b7b bellard
            else
249 c3278b7b bellard
                env->npc = gen_opc_jump_pc[1];
250 c3278b7b bellard
        } else {
251 c3278b7b bellard
            env->npc = npc;
252 c3278b7b bellard
        }
253 c3278b7b bellard
    }
254 6dca2016 bellard
#elif defined(TARGET_PPC)
255 af5ad107 bellard
    {
256 af5ad107 bellard
        int type;
257 af5ad107 bellard
        /* for PPC, we need to look at the micro operation to get the
258 af5ad107 bellard
           access type */
259 af5ad107 bellard
        env->nip = gen_opc_pc[j];
260 af5ad107 bellard
        switch(c) {
261 af5ad107 bellard
#if defined(CONFIG_USER_ONLY)
262 af5ad107 bellard
#define CASE3(op)\
263 af5ad107 bellard
        case INDEX_op_ ## op ## _raw
264 af5ad107 bellard
#else
265 af5ad107 bellard
#define CASE3(op)\
266 af5ad107 bellard
        case INDEX_op_ ## op ## _user:\
267 7863667f j_mayer
        case INDEX_op_ ## op ## _kernel:\
268 7863667f j_mayer
        case INDEX_op_ ## op ## _hypv
269 af5ad107 bellard
#endif
270 3b46e624 ths
271 af5ad107 bellard
        CASE3(stfd):
272 af5ad107 bellard
        CASE3(stfs):
273 af5ad107 bellard
        CASE3(lfd):
274 af5ad107 bellard
        CASE3(lfs):
275 af5ad107 bellard
            type = ACCESS_FLOAT;
276 af5ad107 bellard
            break;
277 a541f297 bellard
        CASE3(lwarx):
278 a541f297 bellard
            type = ACCESS_RES;
279 a541f297 bellard
            break;
280 af5ad107 bellard
        CASE3(stwcx):
281 af5ad107 bellard
            type = ACCESS_RES;
282 af5ad107 bellard
            break;
283 af5ad107 bellard
        CASE3(eciwx):
284 af5ad107 bellard
        CASE3(ecowx):
285 af5ad107 bellard
            type = ACCESS_EXT;
286 af5ad107 bellard
            break;
287 af5ad107 bellard
        default:
288 af5ad107 bellard
            type = ACCESS_INT;
289 af5ad107 bellard
            break;
290 af5ad107 bellard
        }
291 af5ad107 bellard
        env->access_type = type;
292 af5ad107 bellard
    }
293 e6e5906b pbrook
#elif defined(TARGET_M68K)
294 e6e5906b pbrook
    env->pc = gen_opc_pc[j];
295 6af0bf9c bellard
#elif defined(TARGET_MIPS)
296 ead9360e ths
    env->PC[env->current_tc] = gen_opc_pc[j];
297 30d6cb84 bellard
    env->hflags &= ~MIPS_HFLAG_BMASK;
298 30d6cb84 bellard
    env->hflags |= gen_opc_hflags[j];
299 eddf68a6 j_mayer
#elif defined(TARGET_ALPHA)
300 eddf68a6 j_mayer
    env->pc = gen_opc_pc[j];
301 f76af4b3 bellard
#endif
302 d19893da bellard
    return 0;
303 d19893da bellard
}