root / translate-all.c @ dfae6487
History | View | Annotate | Download (8.2 kB)
1 | d19893da | bellard | /*
|
---|---|---|---|
2 | d19893da | bellard | * Host code generation
|
3 | d19893da | bellard | *
|
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 | f76af4b3 | bellard | #endif
|
57 | d19893da | bellard | |
58 | 58fe2f10 | bellard | int code_copy_enabled = 1; |
59 | 58fe2f10 | bellard | |
60 | d19893da | bellard | #ifdef DEBUG_DISAS
|
61 | d19893da | bellard | static const char *op_str[] = { |
62 | d19893da | bellard | #define DEF(s, n, copy_size) #s, |
63 | d3eead2e | bellard | #include "opc.h" |
64 | d19893da | bellard | #undef DEF
|
65 | d19893da | bellard | }; |
66 | d19893da | bellard | |
67 | d19893da | bellard | static uint8_t op_nb_args[] = {
|
68 | d19893da | bellard | #define DEF(s, n, copy_size) n,
|
69 | d3eead2e | bellard | #include "opc.h" |
70 | d19893da | bellard | #undef DEF
|
71 | d19893da | bellard | }; |
72 | d19893da | bellard | |
73 | c4687878 | bellard | static const unsigned short opc_copy_size[] = { |
74 | c4687878 | bellard | #define DEF(s, n, copy_size) copy_size,
|
75 | c4687878 | bellard | #include "opc.h" |
76 | c4687878 | bellard | #undef DEF
|
77 | c4687878 | bellard | }; |
78 | c4687878 | bellard | |
79 | d19893da | bellard | void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf) |
80 | d19893da | bellard | { |
81 | d19893da | bellard | const uint16_t *opc_ptr;
|
82 | d19893da | bellard | const uint32_t *opparam_ptr;
|
83 | d19893da | bellard | int c, n, i;
|
84 | d19893da | bellard | |
85 | d19893da | bellard | opc_ptr = opc_buf; |
86 | d19893da | bellard | opparam_ptr = opparam_buf; |
87 | d19893da | bellard | for(;;) {
|
88 | d19893da | bellard | c = *opc_ptr++; |
89 | d19893da | bellard | n = op_nb_args[c]; |
90 | d19893da | bellard | fprintf(logfile, "0x%04x: %s",
|
91 | d19893da | bellard | (int)(opc_ptr - opc_buf - 1), op_str[c]); |
92 | d19893da | bellard | for(i = 0; i < n; i++) { |
93 | d19893da | bellard | fprintf(logfile, " 0x%x", opparam_ptr[i]);
|
94 | d19893da | bellard | } |
95 | d19893da | bellard | fprintf(logfile, "\n");
|
96 | d19893da | bellard | if (c == INDEX_op_end)
|
97 | d19893da | bellard | break;
|
98 | d19893da | bellard | opparam_ptr += n; |
99 | d19893da | bellard | } |
100 | d19893da | bellard | } |
101 | d19893da | bellard | |
102 | d19893da | bellard | #endif
|
103 | d19893da | bellard | |
104 | c4687878 | bellard | /* compute label info */
|
105 | c4687878 | bellard | static void dyngen_labels(long *gen_labels, int nb_gen_labels, |
106 | c4687878 | bellard | uint8_t *gen_code_buf, const uint16_t *opc_buf)
|
107 | c4687878 | bellard | { |
108 | c4687878 | bellard | uint8_t *gen_code_ptr; |
109 | c4687878 | bellard | int c, i;
|
110 | c4687878 | bellard | unsigned long gen_code_addr[OPC_BUF_SIZE]; |
111 | c4687878 | bellard | |
112 | c4687878 | bellard | if (nb_gen_labels == 0) |
113 | c4687878 | bellard | return;
|
114 | c4687878 | bellard | /* compute the address of each op code */
|
115 | c4687878 | bellard | |
116 | c4687878 | bellard | gen_code_ptr = gen_code_buf; |
117 | c4687878 | bellard | i = 0;
|
118 | c4687878 | bellard | for(;;) {
|
119 | c4687878 | bellard | c = opc_buf[i]; |
120 | c4687878 | bellard | gen_code_addr[i] =(unsigned long)gen_code_ptr; |
121 | c4687878 | bellard | if (c == INDEX_op_end)
|
122 | c4687878 | bellard | break;
|
123 | c4687878 | bellard | gen_code_ptr += opc_copy_size[c]; |
124 | c4687878 | bellard | i++; |
125 | c4687878 | bellard | } |
126 | c4687878 | bellard | |
127 | c4687878 | bellard | /* compute the address of each label */
|
128 | c4687878 | bellard | for(i = 0; i < nb_gen_labels; i++) { |
129 | c4687878 | bellard | gen_labels[i] = gen_code_addr[gen_labels[i]]; |
130 | c4687878 | bellard | } |
131 | c4687878 | bellard | } |
132 | c4687878 | bellard | |
133 | d19893da | bellard | /* return non zero if the very first instruction is invalid so that
|
134 | d19893da | bellard | the virtual CPU can trigger an exception.
|
135 | d19893da | bellard | |
136 | d19893da | bellard | '*gen_code_size_ptr' contains the size of the generated code (host
|
137 | d19893da | bellard | code).
|
138 | d19893da | bellard | */
|
139 | 4c3a88a2 | bellard | int cpu_gen_code(CPUState *env, TranslationBlock *tb,
|
140 | d19893da | bellard | int max_code_size, int *gen_code_size_ptr) |
141 | d19893da | bellard | { |
142 | d19893da | bellard | uint8_t *gen_code_buf; |
143 | d19893da | bellard | int gen_code_size;
|
144 | d19893da | bellard | |
145 | 58fe2f10 | bellard | #ifdef USE_CODE_COPY
|
146 | 58fe2f10 | bellard | if (code_copy_enabled &&
|
147 | 58fe2f10 | bellard | cpu_gen_code_copy(env, tb, max_code_size, &gen_code_size) == 0) {
|
148 | 58fe2f10 | bellard | /* nothing more to do */
|
149 | 58fe2f10 | bellard | } else
|
150 | 58fe2f10 | bellard | #endif
|
151 | 58fe2f10 | bellard | { |
152 | 58fe2f10 | bellard | if (gen_intermediate_code(env, tb) < 0) |
153 | 58fe2f10 | bellard | return -1; |
154 | d19893da | bellard | |
155 | 58fe2f10 | bellard | /* generate machine code */
|
156 | 58fe2f10 | bellard | tb->tb_next_offset[0] = 0xffff; |
157 | 58fe2f10 | bellard | tb->tb_next_offset[1] = 0xffff; |
158 | 58fe2f10 | bellard | gen_code_buf = tb->tc_ptr; |
159 | 4cbb86e1 | bellard | #ifdef USE_DIRECT_JUMP
|
160 | 58fe2f10 | bellard | /* the following two entries are optional (only used for string ops) */
|
161 | 58fe2f10 | bellard | tb->tb_jmp_offset[2] = 0xffff; |
162 | 58fe2f10 | bellard | tb->tb_jmp_offset[3] = 0xffff; |
163 | 4cbb86e1 | bellard | #endif
|
164 | c4687878 | bellard | dyngen_labels(gen_labels, nb_gen_labels, gen_code_buf, gen_opc_buf); |
165 | c4687878 | bellard | |
166 | 58fe2f10 | bellard | gen_code_size = dyngen_code(gen_code_buf, tb->tb_next_offset, |
167 | d19893da | bellard | #ifdef USE_DIRECT_JUMP
|
168 | 58fe2f10 | bellard | tb->tb_jmp_offset, |
169 | d19893da | bellard | #else
|
170 | 58fe2f10 | bellard | NULL,
|
171 | d19893da | bellard | #endif
|
172 | c4687878 | bellard | gen_opc_buf, gen_opparam_buf, gen_labels); |
173 | 58fe2f10 | bellard | } |
174 | d19893da | bellard | *gen_code_size_ptr = gen_code_size; |
175 | d19893da | bellard | #ifdef DEBUG_DISAS
|
176 | f193c797 | bellard | if (loglevel & CPU_LOG_TB_OUT_ASM) {
|
177 | d19893da | bellard | fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
|
178 | c4687878 | bellard | disas(logfile, tb->tc_ptr, *gen_code_size_ptr); |
179 | d19893da | bellard | fprintf(logfile, "\n");
|
180 | d19893da | bellard | fflush(logfile); |
181 | d19893da | bellard | } |
182 | d19893da | bellard | #endif
|
183 | d19893da | bellard | return 0; |
184 | d19893da | bellard | } |
185 | d19893da | bellard | |
186 | f76af4b3 | bellard | /* The cpu state corresponding to 'searched_pc' is restored.
|
187 | d19893da | bellard | */
|
188 | f76af4b3 | bellard | int cpu_restore_state(TranslationBlock *tb,
|
189 | 58fe2f10 | bellard | CPUState *env, unsigned long searched_pc, |
190 | 58fe2f10 | bellard | void *puc)
|
191 | d19893da | bellard | { |
192 | d19893da | bellard | int j, c;
|
193 | d19893da | bellard | unsigned long tc_ptr; |
194 | d19893da | bellard | uint16_t *opc_ptr; |
195 | d19893da | bellard | |
196 | 58fe2f10 | bellard | #ifdef USE_CODE_COPY
|
197 | 58fe2f10 | bellard | if (tb->cflags & CF_CODE_COPY) {
|
198 | 58fe2f10 | bellard | return cpu_restore_state_copy(tb, env, searched_pc, puc);
|
199 | 58fe2f10 | bellard | } |
200 | 58fe2f10 | bellard | #endif
|
201 | 4c3a88a2 | bellard | if (gen_intermediate_code_pc(env, tb) < 0) |
202 | d19893da | bellard | return -1; |
203 | d19893da | bellard | |
204 | d19893da | bellard | /* find opc index corresponding to search_pc */
|
205 | d19893da | bellard | tc_ptr = (unsigned long)tb->tc_ptr; |
206 | d19893da | bellard | if (searched_pc < tc_ptr)
|
207 | d19893da | bellard | return -1; |
208 | d19893da | bellard | j = 0;
|
209 | d19893da | bellard | opc_ptr = gen_opc_buf; |
210 | d19893da | bellard | for(;;) {
|
211 | d19893da | bellard | c = *opc_ptr; |
212 | d19893da | bellard | if (c == INDEX_op_end)
|
213 | d19893da | bellard | return -1; |
214 | d19893da | bellard | tc_ptr += opc_copy_size[c]; |
215 | d19893da | bellard | if (searched_pc < tc_ptr)
|
216 | d19893da | bellard | break;
|
217 | d19893da | bellard | opc_ptr++; |
218 | d19893da | bellard | } |
219 | d19893da | bellard | j = opc_ptr - gen_opc_buf; |
220 | d19893da | bellard | /* now find start of instruction before */
|
221 | d19893da | bellard | while (gen_opc_instr_start[j] == 0) |
222 | d19893da | bellard | j--; |
223 | f76af4b3 | bellard | #if defined(TARGET_I386)
|
224 | f76af4b3 | bellard | { |
225 | f76af4b3 | bellard | int cc_op;
|
226 | 3c1cf9fa | bellard | #ifdef DEBUG_DISAS
|
227 | f193c797 | bellard | if (loglevel & CPU_LOG_TB_OP) {
|
228 | 3c1cf9fa | bellard | int i;
|
229 | 6e0374f6 | bellard | fprintf(logfile, "RESTORE:\n");
|
230 | 3c1cf9fa | bellard | for(i=0;i<=j; i++) { |
231 | 3c1cf9fa | bellard | if (gen_opc_instr_start[i]) {
|
232 | c4687878 | bellard | fprintf(logfile, "0x%04x: " TARGET_FMT_lx "\n", i, gen_opc_pc[i]); |
233 | 3c1cf9fa | bellard | } |
234 | 3c1cf9fa | bellard | } |
235 | c4687878 | bellard | fprintf(logfile, "spc=0x%08lx j=0x%x eip=" TARGET_FMT_lx " cs_base=%x\n", |
236 | c4687878 | bellard | searched_pc, j, gen_opc_pc[j] - tb->cs_base, |
237 | c4687878 | bellard | (uint32_t)tb->cs_base); |
238 | 3c1cf9fa | bellard | } |
239 | 3c1cf9fa | bellard | #endif
|
240 | f76af4b3 | bellard | env->eip = gen_opc_pc[j] - tb->cs_base; |
241 | f76af4b3 | bellard | cc_op = gen_opc_cc_op[j]; |
242 | f76af4b3 | bellard | if (cc_op != CC_OP_DYNAMIC)
|
243 | f76af4b3 | bellard | env->cc_op = cc_op; |
244 | f76af4b3 | bellard | } |
245 | f76af4b3 | bellard | #elif defined(TARGET_ARM)
|
246 | f76af4b3 | bellard | env->regs[15] = gen_opc_pc[j];
|
247 | d3eead2e | bellard | #elif defined(TARGET_SPARC)
|
248 | c3278b7b | bellard | { |
249 | c3278b7b | bellard | target_ulong npc; |
250 | c3278b7b | bellard | env->pc = gen_opc_pc[j]; |
251 | c3278b7b | bellard | npc = gen_opc_npc[j]; |
252 | c3278b7b | bellard | if (npc == 1) { |
253 | c3278b7b | bellard | /* dynamic NPC: already stored */
|
254 | c3278b7b | bellard | } else if (npc == 2) { |
255 | c3278b7b | bellard | target_ulong t2 = (target_ulong)puc; |
256 | c3278b7b | bellard | /* jump PC: use T2 and the jump targets of the translation */
|
257 | c3278b7b | bellard | if (t2)
|
258 | c3278b7b | bellard | env->npc = gen_opc_jump_pc[0];
|
259 | c3278b7b | bellard | else
|
260 | c3278b7b | bellard | env->npc = gen_opc_jump_pc[1];
|
261 | c3278b7b | bellard | } else {
|
262 | c3278b7b | bellard | env->npc = npc; |
263 | c3278b7b | bellard | } |
264 | c3278b7b | bellard | } |
265 | 6dca2016 | bellard | #elif defined(TARGET_PPC)
|
266 | af5ad107 | bellard | { |
267 | af5ad107 | bellard | int type;
|
268 | af5ad107 | bellard | /* for PPC, we need to look at the micro operation to get the
|
269 | af5ad107 | bellard | access type */
|
270 | af5ad107 | bellard | env->nip = gen_opc_pc[j]; |
271 | af5ad107 | bellard | switch(c) {
|
272 | af5ad107 | bellard | #if defined(CONFIG_USER_ONLY)
|
273 | af5ad107 | bellard | #define CASE3(op)\
|
274 | af5ad107 | bellard | case INDEX_op_ ## op ## _raw |
275 | af5ad107 | bellard | #else
|
276 | af5ad107 | bellard | #define CASE3(op)\
|
277 | af5ad107 | bellard | case INDEX_op_ ## op ## _user:\ |
278 | af5ad107 | bellard | case INDEX_op_ ## op ## _kernel |
279 | af5ad107 | bellard | #endif
|
280 | af5ad107 | bellard | |
281 | af5ad107 | bellard | CASE3(stfd): |
282 | af5ad107 | bellard | CASE3(stfs): |
283 | af5ad107 | bellard | CASE3(lfd): |
284 | af5ad107 | bellard | CASE3(lfs): |
285 | af5ad107 | bellard | type = ACCESS_FLOAT; |
286 | af5ad107 | bellard | break;
|
287 | a541f297 | bellard | CASE3(lwarx): |
288 | a541f297 | bellard | type = ACCESS_RES; |
289 | a541f297 | bellard | break;
|
290 | af5ad107 | bellard | CASE3(stwcx): |
291 | af5ad107 | bellard | type = ACCESS_RES; |
292 | af5ad107 | bellard | break;
|
293 | af5ad107 | bellard | CASE3(eciwx): |
294 | af5ad107 | bellard | CASE3(ecowx): |
295 | af5ad107 | bellard | type = ACCESS_EXT; |
296 | af5ad107 | bellard | break;
|
297 | af5ad107 | bellard | default:
|
298 | af5ad107 | bellard | type = ACCESS_INT; |
299 | af5ad107 | bellard | break;
|
300 | af5ad107 | bellard | } |
301 | af5ad107 | bellard | env->access_type = type; |
302 | af5ad107 | bellard | } |
303 | 6af0bf9c | bellard | #elif defined(TARGET_MIPS)
|
304 | 6af0bf9c | bellard | env->PC = gen_opc_pc[j]; |
305 | f76af4b3 | bellard | #endif
|
306 | d19893da | bellard | return 0; |
307 | d19893da | bellard | } |