Statistics
| Branch: | Revision:

root / translate-all.c @ c596defd

History | View | Annotate | Download (4.9 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 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 d19893da bellard
uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
42 f76af4b3 bellard
#if defined(TARGET_I386)
43 f76af4b3 bellard
uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
44 e95c8d51 bellard
#elif defined(TARGET_SPARC)
45 c4687878 bellard
target_ulong gen_opc_npc[OPC_BUF_SIZE];
46 c3278b7b bellard
target_ulong gen_opc_jump_pc[2];
47 823029f9 ths
#elif defined(TARGET_MIPS) || defined(TARGET_SH4)
48 30d6cb84 bellard
uint32_t gen_opc_hflags[OPC_BUF_SIZE];
49 f76af4b3 bellard
#endif
50 d19893da bellard
51 57fec1fe bellard
/* XXX: suppress that */
52 d07bde88 blueswir1
unsigned long code_gen_max_block_size(void)
53 d07bde88 blueswir1
{
54 d07bde88 blueswir1
    static unsigned long max;
55 d07bde88 blueswir1
56 d07bde88 blueswir1
    if (max == 0) {
57 a208e54a pbrook
        max = TCG_MAX_OP_SIZE;
58 d07bde88 blueswir1
#define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
59 57fec1fe bellard
#include "tcg-opc.h"
60 d07bde88 blueswir1
#undef DEF
61 d07bde88 blueswir1
        max *= OPC_MAX_SIZE;
62 d07bde88 blueswir1
    }
63 d07bde88 blueswir1
64 d07bde88 blueswir1
    return max;
65 d07bde88 blueswir1
}
66 d07bde88 blueswir1
67 57fec1fe bellard
void cpu_gen_init(void)
68 57fec1fe bellard
{
69 57fec1fe bellard
    tcg_context_init(&tcg_ctx); 
70 57fec1fe bellard
    tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
71 a20e31dc blueswir1
                  CPU_TEMP_BUF_NLONGS * sizeof(long));
72 57fec1fe bellard
}
73 57fec1fe bellard
74 d19893da bellard
/* return non zero if the very first instruction is invalid so that
75 5fafdf24 ths
   the virtual CPU can trigger an exception.
76 d19893da bellard

77 d19893da bellard
   '*gen_code_size_ptr' contains the size of the generated code (host
78 d19893da bellard
   code).
79 d19893da bellard
*/
80 d07bde88 blueswir1
int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
81 d19893da bellard
{
82 57fec1fe bellard
    TCGContext *s = &tcg_ctx;
83 d19893da bellard
    uint8_t *gen_code_buf;
84 d19893da bellard
    int gen_code_size;
85 57fec1fe bellard
#ifdef CONFIG_PROFILER
86 57fec1fe bellard
    int64_t ti;
87 57fec1fe bellard
#endif
88 57fec1fe bellard
89 57fec1fe bellard
#ifdef CONFIG_PROFILER
90 b67d9a52 bellard
    s->tb_count1++; /* includes aborted translations because of
91 b67d9a52 bellard
                       exceptions */
92 57fec1fe bellard
    ti = profile_getclock();
93 57fec1fe bellard
#endif
94 57fec1fe bellard
    tcg_func_start(s);
95 d19893da bellard
96 ec6338ba bellard
    if (gen_intermediate_code(env, tb) < 0)
97 ec6338ba bellard
        return -1;
98 ec6338ba bellard
    
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 57fec1fe bellard
    gen_code_size = dyngen_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 f193c797 bellard
    if (loglevel & CPU_LOG_TB_OUT_ASM) {
131 d19893da bellard
        fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
132 c4687878 bellard
        disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
133 d19893da bellard
        fprintf(logfile, "\n");
134 d19893da bellard
        fflush(logfile);
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 4c3a88a2 bellard
    if (gen_intermediate_code_pc(env, tb) < 0)
159 d19893da bellard
        return -1;
160 3b46e624 ths
161 d19893da bellard
    /* find opc index corresponding to search_pc */
162 d19893da bellard
    tc_ptr = (unsigned long)tb->tc_ptr;
163 d19893da bellard
    if (searched_pc < tc_ptr)
164 d19893da bellard
        return -1;
165 57fec1fe bellard
166 57fec1fe bellard
    s->tb_next_offset = tb->tb_next_offset;
167 57fec1fe bellard
#ifdef USE_DIRECT_JUMP
168 57fec1fe bellard
    s->tb_jmp_offset = tb->tb_jmp_offset;
169 57fec1fe bellard
    s->tb_next = NULL;
170 57fec1fe bellard
#else
171 57fec1fe bellard
    s->tb_jmp_offset = NULL;
172 57fec1fe bellard
    s->tb_next = tb->tb_next;
173 57fec1fe bellard
#endif
174 623e265c pbrook
    j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
175 57fec1fe bellard
    if (j < 0)
176 57fec1fe bellard
        return -1;
177 d19893da bellard
    /* now find start of instruction before */
178 d19893da bellard
    while (gen_opc_instr_start[j] == 0)
179 d19893da bellard
        j--;
180 3b46e624 ths
181 d2856f1a aurel32
    gen_pc_load(env, tb, searched_pc, j, puc);
182 57fec1fe bellard
183 57fec1fe bellard
#ifdef CONFIG_PROFILER
184 b67d9a52 bellard
    s->restore_time += profile_getclock() - ti;
185 b67d9a52 bellard
    s->restore_count++;
186 57fec1fe bellard
#endif
187 d19893da bellard
    return 0;
188 d19893da bellard
}