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
29 #if defined(TARGET_I386)
31 #define OPC_CPU_H "opc-i386.h"
32 #elif defined(TARGET_ARM)
34 #define OPC_CPU_H "opc-arm.h"
36 #error unsupported target CPU
43 #define DEF(s, n, copy_size) INDEX_op_ ## s,
50 #if defined(TARGET_I386)
52 #elif defined(TARGET_ARM)
55 #error unsupported target CPU
58 uint16_t gen_opc_buf[OPC_BUF_SIZE];
59 uint32_t gen_opparam_buf[OPPARAM_BUF_SIZE];
60 uint32_t gen_opc_pc[OPC_BUF_SIZE];
61 uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
62 #if defined(TARGET_I386)
63 uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
67 static const char *op_str[] = {
68 #define DEF(s, n, copy_size) #s,
73 static uint8_t op_nb_args[] = {
74 #define DEF(s, n, copy_size) n,
79 void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf)
81 const uint16_t *opc_ptr;
82 const uint32_t *opparam_ptr;
86 opparam_ptr = opparam_buf;
90 fprintf(logfile, "0x%04x: %s",
91 (int)(opc_ptr - opc_buf - 1), op_str[c]);
92 for(i = 0; i < n; i++) {
93 fprintf(logfile, " 0x%x", opparam_ptr[i]);
95 fprintf(logfile, "\n");
96 if (c == INDEX_op_end)
104 /* return non zero if the very first instruction is invalid so that
105 the virtual CPU can trigger an exception.
107 '*gen_code_size_ptr' contains the size of the generated code (host
110 int cpu_gen_code(TranslationBlock *tb,
111 int max_code_size, int *gen_code_size_ptr)
113 uint8_t *gen_code_buf;
116 if (gen_intermediate_code(tb) < 0)
119 /* generate machine code */
120 tb->tb_next_offset[0] = 0xffff;
121 tb->tb_next_offset[1] = 0xffff;
122 gen_code_buf = tb->tc_ptr;
123 gen_code_size = dyngen_code(gen_code_buf, tb->tb_next_offset,
124 #ifdef USE_DIRECT_JUMP
129 gen_opc_buf, gen_opparam_buf);
130 *gen_code_size_ptr = gen_code_size;
133 fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
134 disas(logfile, gen_code_buf, *gen_code_size_ptr, 1, 0);
135 fprintf(logfile, "\n");
142 static const unsigned short opc_copy_size[] = {
143 #define DEF(s, n, copy_size) copy_size,
148 /* The cpu state corresponding to 'searched_pc' is restored.
150 int cpu_restore_state(TranslationBlock *tb,
151 CPUState *env, unsigned long searched_pc)
154 unsigned long tc_ptr;
157 if (gen_intermediate_code_pc(tb) < 0)
160 /* find opc index corresponding to search_pc */
161 tc_ptr = (unsigned long)tb->tc_ptr;
162 if (searched_pc < tc_ptr)
165 opc_ptr = gen_opc_buf;
168 if (c == INDEX_op_end)
170 tc_ptr += opc_copy_size[c];
171 if (searched_pc < tc_ptr)
175 j = opc_ptr - gen_opc_buf;
176 /* now find start of instruction before */
177 while (gen_opc_instr_start[j] == 0)
179 #if defined(TARGET_I386)
185 fprintf(logfile, "RESTORE:\n");
187 if (gen_opc_instr_start[i]) {
188 fprintf(logfile, "0x%04x: 0x%08x\n", i, gen_opc_pc[i]);
191 fprintf(logfile, "spc=0x%08lx j=0x%x eip=0x%lx cs_base=%lx\n",
192 searched_pc, j, gen_opc_pc[j] - tb->cs_base, tb->cs_base);
195 env->eip = gen_opc_pc[j] - tb->cs_base;
196 cc_op = gen_opc_cc_op[j];
197 if (cc_op != CC_OP_DYNAMIC)
200 #elif defined(TARGET_ARM)
201 env->regs[15] = gen_opc_pc[j];