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
33 extern int dyngen_code(uint8_t *gen_code_buf
,
34 uint16_t *label_offsets
, uint16_t *jmp_offsets
,
35 const uint16_t *opc_buf
, const uint32_t *opparam_buf
, const long *gen_labels
);
38 #define DEF(s, n, copy_size) INDEX_op_ ## s,
44 uint16_t gen_opc_buf
[OPC_BUF_SIZE
];
45 uint32_t gen_opparam_buf
[OPPARAM_BUF_SIZE
];
46 long gen_labels
[OPC_BUF_SIZE
];
49 target_ulong gen_opc_pc
[OPC_BUF_SIZE
];
50 uint8_t gen_opc_instr_start
[OPC_BUF_SIZE
];
51 #if defined(TARGET_I386)
52 uint8_t gen_opc_cc_op
[OPC_BUF_SIZE
];
53 #elif defined(TARGET_SPARC)
54 target_ulong gen_opc_npc
[OPC_BUF_SIZE
];
57 int code_copy_enabled
= 1;
60 static const char *op_str
[] = {
61 #define DEF(s, n, copy_size) #s,
66 static uint8_t op_nb_args
[] = {
67 #define DEF(s, n, copy_size) n,
72 static const unsigned short opc_copy_size
[] = {
73 #define DEF(s, n, copy_size) copy_size,
78 void dump_ops(const uint16_t *opc_buf
, const uint32_t *opparam_buf
)
80 const uint16_t *opc_ptr
;
81 const uint32_t *opparam_ptr
;
85 opparam_ptr
= opparam_buf
;
89 fprintf(logfile
, "0x%04x: %s",
90 (int)(opc_ptr
- opc_buf
- 1), op_str
[c
]);
91 for(i
= 0; i
< n
; i
++) {
92 fprintf(logfile
, " 0x%x", opparam_ptr
[i
]);
94 fprintf(logfile
, "\n");
95 if (c
== INDEX_op_end
)
103 /* compute label info */
104 static void dyngen_labels(long *gen_labels
, int nb_gen_labels
,
105 uint8_t *gen_code_buf
, const uint16_t *opc_buf
)
107 uint8_t *gen_code_ptr
;
109 unsigned long gen_code_addr
[OPC_BUF_SIZE
];
111 if (nb_gen_labels
== 0)
113 /* compute the address of each op code */
115 gen_code_ptr
= gen_code_buf
;
119 gen_code_addr
[i
] =(unsigned long)gen_code_ptr
;
120 if (c
== INDEX_op_end
)
122 gen_code_ptr
+= opc_copy_size
[c
];
126 /* compute the address of each label */
127 for(i
= 0; i
< nb_gen_labels
; i
++) {
128 gen_labels
[i
] = gen_code_addr
[gen_labels
[i
]];
132 /* return non zero if the very first instruction is invalid so that
133 the virtual CPU can trigger an exception.
135 '*gen_code_size_ptr' contains the size of the generated code (host
138 int cpu_gen_code(CPUState
*env
, TranslationBlock
*tb
,
139 int max_code_size
, int *gen_code_size_ptr
)
141 uint8_t *gen_code_buf
;
145 if (code_copy_enabled
&&
146 cpu_gen_code_copy(env
, tb
, max_code_size
, &gen_code_size
) == 0) {
147 /* nothing more to do */
151 if (gen_intermediate_code(env
, tb
) < 0)
154 /* generate machine code */
155 tb
->tb_next_offset
[0] = 0xffff;
156 tb
->tb_next_offset
[1] = 0xffff;
157 gen_code_buf
= tb
->tc_ptr
;
158 #ifdef USE_DIRECT_JUMP
159 /* the following two entries are optional (only used for string ops) */
160 tb
->tb_jmp_offset
[2] = 0xffff;
161 tb
->tb_jmp_offset
[3] = 0xffff;
163 dyngen_labels(gen_labels
, nb_gen_labels
, gen_code_buf
, gen_opc_buf
);
165 gen_code_size
= dyngen_code(gen_code_buf
, tb
->tb_next_offset
,
166 #ifdef USE_DIRECT_JUMP
171 gen_opc_buf
, gen_opparam_buf
, gen_labels
);
173 *gen_code_size_ptr
= gen_code_size
;
175 if (loglevel
& CPU_LOG_TB_OUT_ASM
) {
176 fprintf(logfile
, "OUT: [size=%d]\n", *gen_code_size_ptr
);
177 disas(logfile
, tb
->tc_ptr
, *gen_code_size_ptr
);
178 fprintf(logfile
, "\n");
185 /* The cpu state corresponding to 'searched_pc' is restored.
187 int cpu_restore_state(TranslationBlock
*tb
,
188 CPUState
*env
, unsigned long searched_pc
,
192 unsigned long tc_ptr
;
196 if (tb
->cflags
& CF_CODE_COPY
) {
197 return cpu_restore_state_copy(tb
, env
, searched_pc
, puc
);
200 if (gen_intermediate_code_pc(env
, tb
) < 0)
203 /* find opc index corresponding to search_pc */
204 tc_ptr
= (unsigned long)tb
->tc_ptr
;
205 if (searched_pc
< tc_ptr
)
208 opc_ptr
= gen_opc_buf
;
211 if (c
== INDEX_op_end
)
213 tc_ptr
+= opc_copy_size
[c
];
214 if (searched_pc
< tc_ptr
)
218 j
= opc_ptr
- gen_opc_buf
;
219 /* now find start of instruction before */
220 while (gen_opc_instr_start
[j
] == 0)
222 #if defined(TARGET_I386)
226 if (loglevel
& CPU_LOG_TB_OP
) {
228 fprintf(logfile
, "RESTORE:\n");
230 if (gen_opc_instr_start
[i
]) {
231 fprintf(logfile
, "0x%04x: " TARGET_FMT_lx
"\n", i
, gen_opc_pc
[i
]);
234 fprintf(logfile
, "spc=0x%08lx j=0x%x eip=" TARGET_FMT_lx
" cs_base=%x\n",
235 searched_pc
, j
, gen_opc_pc
[j
] - tb
->cs_base
,
236 (uint32_t)tb
->cs_base
);
239 env
->eip
= gen_opc_pc
[j
] - tb
->cs_base
;
240 cc_op
= gen_opc_cc_op
[j
];
241 if (cc_op
!= CC_OP_DYNAMIC
)
244 #elif defined(TARGET_ARM)
245 env
->regs
[15] = gen_opc_pc
[j
];
246 #elif defined(TARGET_SPARC)
247 /* XXX: restore npc too */
248 env
->pc
= gen_opc_pc
[j
];
249 env
->npc
= gen_opc_npc
[j
];
250 #elif defined(TARGET_PPC)
253 /* for PPC, we need to look at the micro operation to get the
255 env
->nip
= gen_opc_pc
[j
];
257 #if defined(CONFIG_USER_ONLY)
259 case INDEX_op_ ## op ## _raw
262 case INDEX_op_ ## op ## _user:\
263 case INDEX_op_ ## op ## _kernel
286 env
->access_type
= type
;