root / translate-all.c @ c4dfa5b7
History | View | Annotate | Download (6.5 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 | d19893da | bellard | enum {
|
34 | d19893da | bellard | #define DEF(s, n, copy_size) INDEX_op_ ## s, |
35 | d3eead2e | bellard | #include "opc.h" |
36 | d19893da | bellard | #undef DEF
|
37 | d19893da | bellard | NB_OPS, |
38 | d19893da | bellard | }; |
39 | d19893da | bellard | |
40 | d19893da | bellard | #include "dyngen.h" |
41 | d3eead2e | bellard | #include "op.h" |
42 | d19893da | bellard | |
43 | d19893da | bellard | uint16_t gen_opc_buf[OPC_BUF_SIZE]; |
44 | d19893da | bellard | uint32_t gen_opparam_buf[OPPARAM_BUF_SIZE]; |
45 | d19893da | bellard | uint32_t gen_opc_pc[OPC_BUF_SIZE]; |
46 | d19893da | bellard | uint8_t gen_opc_instr_start[OPC_BUF_SIZE]; |
47 | f76af4b3 | bellard | #if defined(TARGET_I386)
|
48 | f76af4b3 | bellard | uint8_t gen_opc_cc_op[OPC_BUF_SIZE]; |
49 | f76af4b3 | bellard | #endif
|
50 | d19893da | bellard | |
51 | 58fe2f10 | bellard | int code_copy_enabled = 1; |
52 | 58fe2f10 | bellard | |
53 | d19893da | bellard | #ifdef DEBUG_DISAS
|
54 | d19893da | bellard | static const char *op_str[] = { |
55 | d19893da | bellard | #define DEF(s, n, copy_size) #s, |
56 | d3eead2e | bellard | #include "opc.h" |
57 | d19893da | bellard | #undef DEF
|
58 | d19893da | bellard | }; |
59 | d19893da | bellard | |
60 | d19893da | bellard | static uint8_t op_nb_args[] = {
|
61 | d19893da | bellard | #define DEF(s, n, copy_size) n,
|
62 | d3eead2e | bellard | #include "opc.h" |
63 | d19893da | bellard | #undef DEF
|
64 | d19893da | bellard | }; |
65 | d19893da | bellard | |
66 | d19893da | bellard | void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf) |
67 | d19893da | bellard | { |
68 | d19893da | bellard | const uint16_t *opc_ptr;
|
69 | d19893da | bellard | const uint32_t *opparam_ptr;
|
70 | d19893da | bellard | int c, n, i;
|
71 | d19893da | bellard | |
72 | d19893da | bellard | opc_ptr = opc_buf; |
73 | d19893da | bellard | opparam_ptr = opparam_buf; |
74 | d19893da | bellard | for(;;) {
|
75 | d19893da | bellard | c = *opc_ptr++; |
76 | d19893da | bellard | n = op_nb_args[c]; |
77 | d19893da | bellard | fprintf(logfile, "0x%04x: %s",
|
78 | d19893da | bellard | (int)(opc_ptr - opc_buf - 1), op_str[c]); |
79 | d19893da | bellard | for(i = 0; i < n; i++) { |
80 | d19893da | bellard | fprintf(logfile, " 0x%x", opparam_ptr[i]);
|
81 | d19893da | bellard | } |
82 | d19893da | bellard | fprintf(logfile, "\n");
|
83 | d19893da | bellard | if (c == INDEX_op_end)
|
84 | d19893da | bellard | break;
|
85 | d19893da | bellard | opparam_ptr += n; |
86 | d19893da | bellard | } |
87 | d19893da | bellard | } |
88 | d19893da | bellard | |
89 | d19893da | bellard | #endif
|
90 | d19893da | bellard | |
91 | d19893da | bellard | /* return non zero if the very first instruction is invalid so that
|
92 | d19893da | bellard | the virtual CPU can trigger an exception.
|
93 | d19893da | bellard | |
94 | d19893da | bellard | '*gen_code_size_ptr' contains the size of the generated code (host
|
95 | d19893da | bellard | code).
|
96 | d19893da | bellard | */
|
97 | 4c3a88a2 | bellard | int cpu_gen_code(CPUState *env, TranslationBlock *tb,
|
98 | d19893da | bellard | int max_code_size, int *gen_code_size_ptr) |
99 | d19893da | bellard | { |
100 | d19893da | bellard | uint8_t *gen_code_buf; |
101 | d19893da | bellard | int gen_code_size;
|
102 | d19893da | bellard | |
103 | 58fe2f10 | bellard | #ifdef USE_CODE_COPY
|
104 | 58fe2f10 | bellard | if (code_copy_enabled &&
|
105 | 58fe2f10 | bellard | cpu_gen_code_copy(env, tb, max_code_size, &gen_code_size) == 0) {
|
106 | 58fe2f10 | bellard | /* nothing more to do */
|
107 | 58fe2f10 | bellard | } else
|
108 | 58fe2f10 | bellard | #endif
|
109 | 58fe2f10 | bellard | { |
110 | 58fe2f10 | bellard | if (gen_intermediate_code(env, tb) < 0) |
111 | 58fe2f10 | bellard | return -1; |
112 | d19893da | bellard | |
113 | 58fe2f10 | bellard | /* generate machine code */
|
114 | 58fe2f10 | bellard | tb->tb_next_offset[0] = 0xffff; |
115 | 58fe2f10 | bellard | tb->tb_next_offset[1] = 0xffff; |
116 | 58fe2f10 | bellard | gen_code_buf = tb->tc_ptr; |
117 | 4cbb86e1 | bellard | #ifdef USE_DIRECT_JUMP
|
118 | 58fe2f10 | bellard | /* the following two entries are optional (only used for string ops) */
|
119 | 58fe2f10 | bellard | tb->tb_jmp_offset[2] = 0xffff; |
120 | 58fe2f10 | bellard | tb->tb_jmp_offset[3] = 0xffff; |
121 | 4cbb86e1 | bellard | #endif
|
122 | 58fe2f10 | bellard | gen_code_size = dyngen_code(gen_code_buf, tb->tb_next_offset, |
123 | d19893da | bellard | #ifdef USE_DIRECT_JUMP
|
124 | 58fe2f10 | bellard | tb->tb_jmp_offset, |
125 | d19893da | bellard | #else
|
126 | 58fe2f10 | bellard | NULL,
|
127 | d19893da | bellard | #endif
|
128 | 58fe2f10 | bellard | gen_opc_buf, gen_opparam_buf); |
129 | 58fe2f10 | bellard | } |
130 | d19893da | bellard | *gen_code_size_ptr = gen_code_size; |
131 | d19893da | bellard | #ifdef DEBUG_DISAS
|
132 | f193c797 | bellard | if (loglevel & CPU_LOG_TB_OUT_ASM) {
|
133 | d19893da | bellard | fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
|
134 | 58fe2f10 | bellard | disas(logfile, tb->tc_ptr, *gen_code_size_ptr, 1, 0); |
135 | d19893da | bellard | fprintf(logfile, "\n");
|
136 | d19893da | bellard | fflush(logfile); |
137 | d19893da | bellard | } |
138 | d19893da | bellard | #endif
|
139 | d19893da | bellard | return 0; |
140 | d19893da | bellard | } |
141 | d19893da | bellard | |
142 | d19893da | bellard | static const unsigned short opc_copy_size[] = { |
143 | d19893da | bellard | #define DEF(s, n, copy_size) copy_size,
|
144 | d3eead2e | bellard | #include "opc.h" |
145 | d19893da | bellard | #undef DEF
|
146 | d19893da | bellard | }; |
147 | d19893da | bellard | |
148 | f76af4b3 | bellard | /* The cpu state corresponding to 'searched_pc' is restored.
|
149 | d19893da | bellard | */
|
150 | f76af4b3 | bellard | int cpu_restore_state(TranslationBlock *tb,
|
151 | 58fe2f10 | bellard | CPUState *env, unsigned long searched_pc, |
152 | 58fe2f10 | bellard | void *puc)
|
153 | d19893da | bellard | { |
154 | d19893da | bellard | int j, c;
|
155 | d19893da | bellard | unsigned long tc_ptr; |
156 | d19893da | bellard | uint16_t *opc_ptr; |
157 | d19893da | bellard | |
158 | 58fe2f10 | bellard | #ifdef USE_CODE_COPY
|
159 | 58fe2f10 | bellard | if (tb->cflags & CF_CODE_COPY) {
|
160 | 58fe2f10 | bellard | return cpu_restore_state_copy(tb, env, searched_pc, puc);
|
161 | 58fe2f10 | bellard | } |
162 | 58fe2f10 | bellard | #endif
|
163 | 4c3a88a2 | bellard | if (gen_intermediate_code_pc(env, tb) < 0) |
164 | d19893da | bellard | return -1; |
165 | d19893da | bellard | |
166 | d19893da | bellard | /* find opc index corresponding to search_pc */
|
167 | d19893da | bellard | tc_ptr = (unsigned long)tb->tc_ptr; |
168 | d19893da | bellard | if (searched_pc < tc_ptr)
|
169 | d19893da | bellard | return -1; |
170 | d19893da | bellard | j = 0;
|
171 | d19893da | bellard | opc_ptr = gen_opc_buf; |
172 | d19893da | bellard | for(;;) {
|
173 | d19893da | bellard | c = *opc_ptr; |
174 | d19893da | bellard | if (c == INDEX_op_end)
|
175 | d19893da | bellard | return -1; |
176 | d19893da | bellard | tc_ptr += opc_copy_size[c]; |
177 | d19893da | bellard | if (searched_pc < tc_ptr)
|
178 | d19893da | bellard | break;
|
179 | d19893da | bellard | opc_ptr++; |
180 | d19893da | bellard | } |
181 | d19893da | bellard | j = opc_ptr - gen_opc_buf; |
182 | d19893da | bellard | /* now find start of instruction before */
|
183 | d19893da | bellard | while (gen_opc_instr_start[j] == 0) |
184 | d19893da | bellard | j--; |
185 | f76af4b3 | bellard | #if defined(TARGET_I386)
|
186 | f76af4b3 | bellard | { |
187 | f76af4b3 | bellard | int cc_op;
|
188 | 3c1cf9fa | bellard | #ifdef DEBUG_DISAS
|
189 | f193c797 | bellard | if (loglevel & CPU_LOG_TB_OP) {
|
190 | 3c1cf9fa | bellard | int i;
|
191 | 6e0374f6 | bellard | fprintf(logfile, "RESTORE:\n");
|
192 | 3c1cf9fa | bellard | for(i=0;i<=j; i++) { |
193 | 3c1cf9fa | bellard | if (gen_opc_instr_start[i]) {
|
194 | 6e0374f6 | bellard | fprintf(logfile, "0x%04x: 0x%08x\n", i, gen_opc_pc[i]);
|
195 | 3c1cf9fa | bellard | } |
196 | 3c1cf9fa | bellard | } |
197 | 4f2ac237 | bellard | fprintf(logfile, "spc=0x%08lx j=0x%x eip=0x%x cs_base=%x\n",
|
198 | 6e0374f6 | bellard | searched_pc, j, gen_opc_pc[j] - tb->cs_base, tb->cs_base); |
199 | 3c1cf9fa | bellard | } |
200 | 3c1cf9fa | bellard | #endif
|
201 | f76af4b3 | bellard | env->eip = gen_opc_pc[j] - tb->cs_base; |
202 | f76af4b3 | bellard | cc_op = gen_opc_cc_op[j]; |
203 | f76af4b3 | bellard | if (cc_op != CC_OP_DYNAMIC)
|
204 | f76af4b3 | bellard | env->cc_op = cc_op; |
205 | f76af4b3 | bellard | } |
206 | f76af4b3 | bellard | #elif defined(TARGET_ARM)
|
207 | f76af4b3 | bellard | env->regs[15] = gen_opc_pc[j];
|
208 | d3eead2e | bellard | #elif defined(TARGET_SPARC)
|
209 | 58fe2f10 | bellard | /* XXX: restore npc too */
|
210 | 6dca2016 | bellard | env->pc = gen_opc_pc[j]; |
211 | 6dca2016 | bellard | #elif defined(TARGET_PPC)
|
212 | af5ad107 | bellard | { |
213 | af5ad107 | bellard | int type;
|
214 | af5ad107 | bellard | /* for PPC, we need to look at the micro operation to get the
|
215 | af5ad107 | bellard | access type */
|
216 | af5ad107 | bellard | env->nip = gen_opc_pc[j]; |
217 | af5ad107 | bellard | switch(c) {
|
218 | af5ad107 | bellard | #if defined(CONFIG_USER_ONLY)
|
219 | af5ad107 | bellard | #define CASE3(op)\
|
220 | af5ad107 | bellard | case INDEX_op_ ## op ## _raw |
221 | af5ad107 | bellard | #else
|
222 | af5ad107 | bellard | #define CASE3(op)\
|
223 | af5ad107 | bellard | case INDEX_op_ ## op ## _user:\ |
224 | af5ad107 | bellard | case INDEX_op_ ## op ## _kernel |
225 | af5ad107 | bellard | #endif
|
226 | af5ad107 | bellard | |
227 | af5ad107 | bellard | CASE3(stfd): |
228 | af5ad107 | bellard | CASE3(stfs): |
229 | af5ad107 | bellard | CASE3(lfd): |
230 | af5ad107 | bellard | CASE3(lfs): |
231 | af5ad107 | bellard | type = ACCESS_FLOAT; |
232 | af5ad107 | bellard | break;
|
233 | a541f297 | bellard | CASE3(lwarx): |
234 | a541f297 | bellard | type = ACCESS_RES; |
235 | a541f297 | bellard | break;
|
236 | af5ad107 | bellard | CASE3(stwcx): |
237 | af5ad107 | bellard | type = ACCESS_RES; |
238 | af5ad107 | bellard | break;
|
239 | af5ad107 | bellard | CASE3(eciwx): |
240 | af5ad107 | bellard | CASE3(ecowx): |
241 | af5ad107 | bellard | type = ACCESS_EXT; |
242 | af5ad107 | bellard | break;
|
243 | af5ad107 | bellard | default:
|
244 | af5ad107 | bellard | type = ACCESS_INT; |
245 | af5ad107 | bellard | break;
|
246 | af5ad107 | bellard | } |
247 | af5ad107 | bellard | env->access_type = type; |
248 | af5ad107 | bellard | } |
249 | f76af4b3 | bellard | #endif
|
250 | d19893da | bellard | return 0; |
251 | d19893da | bellard | } |