4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #define NO_CPU_IO_DEFS
34 /* code generation context */
37 uint16_t gen_opc_buf[OPC_BUF_SIZE];
38 TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
40 target_ulong gen_opc_pc[OPC_BUF_SIZE];
41 uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
42 #if defined(TARGET_I386)
43 uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
44 #elif defined(TARGET_SPARC)
45 target_ulong gen_opc_npc[OPC_BUF_SIZE];
46 target_ulong gen_opc_jump_pc[2];
47 #elif defined(TARGET_MIPS) || defined(TARGET_SH4)
48 uint32_t gen_opc_hflags[OPC_BUF_SIZE];
51 int code_copy_enabled = 1;
53 #ifdef CONFIG_PROFILER
54 int64_t dyngen_tb_count1;
55 int64_t dyngen_tb_count;
56 int64_t dyngen_op_count;
57 int64_t dyngen_old_op_count;
58 int64_t dyngen_tcg_del_op_count;
59 int dyngen_op_count_max;
60 int64_t dyngen_code_in_len;
61 int64_t dyngen_code_out_len;
62 int64_t dyngen_interm_time;
63 int64_t dyngen_code_time;
64 int64_t dyngen_restore_count;
65 int64_t dyngen_restore_time;
68 /* XXX: suppress that */
69 unsigned long code_gen_max_block_size(void)
71 static unsigned long max;
74 #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
83 void cpu_gen_init(void)
85 tcg_context_init(&tcg_ctx);
86 tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
90 /* return non zero if the very first instruction is invalid so that
91 the virtual CPU can trigger an exception.
93 '*gen_code_size_ptr' contains the size of the generated code (host
96 int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
98 TCGContext *s = &tcg_ctx;
99 uint8_t *gen_code_buf;
101 #ifdef CONFIG_PROFILER
105 #ifdef CONFIG_PROFILER
106 dyngen_tb_count1++; /* includes aborted translations because of
108 ti = profile_getclock();
112 if (gen_intermediate_code(env, tb) < 0)
115 /* generate machine code */
116 gen_code_buf = tb->tc_ptr;
117 tb->tb_next_offset[0] = 0xffff;
118 tb->tb_next_offset[1] = 0xffff;
119 s->tb_next_offset = tb->tb_next_offset;
120 #ifdef USE_DIRECT_JUMP
121 s->tb_jmp_offset = tb->tb_jmp_offset;
123 /* the following two entries are optional (only used for string ops) */
124 /* XXX: not used ? */
125 tb->tb_jmp_offset[2] = 0xffff;
126 tb->tb_jmp_offset[3] = 0xffff;
128 s->tb_jmp_offset = NULL;
129 s->tb_next = tb->tb_next;
132 #ifdef CONFIG_PROFILER
134 dyngen_interm_time += profile_getclock() - ti;
135 dyngen_code_time -= profile_getclock();
137 gen_code_size = dyngen_code(s, gen_code_buf);
138 *gen_code_size_ptr = gen_code_size;
139 #ifdef CONFIG_PROFILER
140 dyngen_code_time += profile_getclock();
141 dyngen_code_in_len += tb->size;
142 dyngen_code_out_len += gen_code_size;
146 if (loglevel & CPU_LOG_TB_OUT_ASM) {
147 fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
148 disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
149 fprintf(logfile, "\n");
156 /* The cpu state corresponding to 'searched_pc' is restored.
158 int cpu_restore_state(TranslationBlock *tb,
159 CPUState *env, unsigned long searched_pc,
162 TCGContext *s = &tcg_ctx;
164 unsigned long tc_ptr;
165 #ifdef CONFIG_PROFILER
169 #ifdef CONFIG_PROFILER
170 ti = profile_getclock();
174 if (gen_intermediate_code_pc(env, tb) < 0)
177 /* find opc index corresponding to search_pc */
178 tc_ptr = (unsigned long)tb->tc_ptr;
179 if (searched_pc < tc_ptr)
182 s->tb_next_offset = tb->tb_next_offset;
183 #ifdef USE_DIRECT_JUMP
184 s->tb_jmp_offset = tb->tb_jmp_offset;
187 s->tb_jmp_offset = NULL;
188 s->tb_next = tb->tb_next;
190 j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
193 /* now find start of instruction before */
194 while (gen_opc_instr_start[j] == 0)
196 #if defined(TARGET_I386)
200 if (loglevel & CPU_LOG_TB_OP) {
202 fprintf(logfile, "RESTORE:\n");
204 if (gen_opc_instr_start[i]) {
205 fprintf(logfile, "0x%04x: " TARGET_FMT_lx "\n", i, gen_opc_pc[i]);
208 fprintf(logfile, "spc=0x%08lx j=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n",
209 searched_pc, j, gen_opc_pc[j] - tb->cs_base,
210 (uint32_t)tb->cs_base);
213 env->eip = gen_opc_pc[j] - tb->cs_base;
214 cc_op = gen_opc_cc_op[j];
215 if (cc_op != CC_OP_DYNAMIC)
218 #elif defined(TARGET_ARM)
219 env->regs[15] = gen_opc_pc[j];
220 #elif defined(TARGET_SPARC)
223 env->pc = gen_opc_pc[j];
224 npc = gen_opc_npc[j];
226 /* dynamic NPC: already stored */
227 } else if (npc == 2) {
228 target_ulong t2 = (target_ulong)(unsigned long)puc;
229 /* jump PC: use T2 and the jump targets of the translation */
231 env->npc = gen_opc_jump_pc[0];
233 env->npc = gen_opc_jump_pc[1];
238 #elif defined(TARGET_PPC)
241 /* for PPC, we need to look at the micro operation to get the
243 env->nip = gen_opc_pc[j];
246 #if defined(CONFIG_USER_ONLY)
248 case INDEX_op_ ## op ## _raw
251 case INDEX_op_ ## op ## _user:\
252 case INDEX_op_ ## op ## _kernel:\
253 case INDEX_op_ ## op ## _hypv
276 env->access_type = type;
278 #elif defined(TARGET_M68K)
279 env->pc = gen_opc_pc[j];
280 #elif defined(TARGET_MIPS)
281 env->PC[env->current_tc] = gen_opc_pc[j];
282 env->hflags &= ~MIPS_HFLAG_BMASK;
283 env->hflags |= gen_opc_hflags[j];
284 #elif defined(TARGET_ALPHA)
285 env->pc = gen_opc_pc[j];
286 #elif defined(TARGET_SH4)
287 env->pc = gen_opc_pc[j];
288 env->flags = gen_opc_hflags[j];
291 #ifdef CONFIG_PROFILER
292 dyngen_restore_time += profile_getclock() - ti;
293 dyngen_restore_count++;