root / translate-all.c @ 891b38e4
History | View | Annotate | Download (5.1 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 | d19893da | bellard | #define IN_OP_I386
|
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 | d19893da | bellard | #ifdef DEBUG_DISAS
|
52 | d19893da | bellard | static const char *op_str[] = { |
53 | d19893da | bellard | #define DEF(s, n, copy_size) #s, |
54 | d3eead2e | bellard | #include "opc.h" |
55 | d19893da | bellard | #undef DEF
|
56 | d19893da | bellard | }; |
57 | d19893da | bellard | |
58 | d19893da | bellard | static uint8_t op_nb_args[] = {
|
59 | d19893da | bellard | #define DEF(s, n, copy_size) n,
|
60 | d3eead2e | bellard | #include "opc.h" |
61 | d19893da | bellard | #undef DEF
|
62 | d19893da | bellard | }; |
63 | d19893da | bellard | |
64 | d19893da | bellard | void dump_ops(const uint16_t *opc_buf, const uint32_t *opparam_buf) |
65 | d19893da | bellard | { |
66 | d19893da | bellard | const uint16_t *opc_ptr;
|
67 | d19893da | bellard | const uint32_t *opparam_ptr;
|
68 | d19893da | bellard | int c, n, i;
|
69 | d19893da | bellard | |
70 | d19893da | bellard | opc_ptr = opc_buf; |
71 | d19893da | bellard | opparam_ptr = opparam_buf; |
72 | d19893da | bellard | for(;;) {
|
73 | d19893da | bellard | c = *opc_ptr++; |
74 | d19893da | bellard | n = op_nb_args[c]; |
75 | d19893da | bellard | fprintf(logfile, "0x%04x: %s",
|
76 | d19893da | bellard | (int)(opc_ptr - opc_buf - 1), op_str[c]); |
77 | d19893da | bellard | for(i = 0; i < n; i++) { |
78 | d19893da | bellard | fprintf(logfile, " 0x%x", opparam_ptr[i]);
|
79 | d19893da | bellard | } |
80 | d19893da | bellard | fprintf(logfile, "\n");
|
81 | d19893da | bellard | if (c == INDEX_op_end)
|
82 | d19893da | bellard | break;
|
83 | d19893da | bellard | opparam_ptr += n; |
84 | d19893da | bellard | } |
85 | d19893da | bellard | } |
86 | d19893da | bellard | |
87 | d19893da | bellard | #endif
|
88 | d19893da | bellard | |
89 | d19893da | bellard | /* return non zero if the very first instruction is invalid so that
|
90 | d19893da | bellard | the virtual CPU can trigger an exception.
|
91 | d19893da | bellard | |
92 | d19893da | bellard | '*gen_code_size_ptr' contains the size of the generated code (host
|
93 | d19893da | bellard | code).
|
94 | d19893da | bellard | */
|
95 | 4c3a88a2 | bellard | int cpu_gen_code(CPUState *env, TranslationBlock *tb,
|
96 | d19893da | bellard | int max_code_size, int *gen_code_size_ptr) |
97 | d19893da | bellard | { |
98 | d19893da | bellard | uint8_t *gen_code_buf; |
99 | d19893da | bellard | int gen_code_size;
|
100 | d19893da | bellard | |
101 | 4c3a88a2 | bellard | if (gen_intermediate_code(env, tb) < 0) |
102 | d19893da | bellard | return -1; |
103 | d19893da | bellard | |
104 | d19893da | bellard | /* generate machine code */
|
105 | d19893da | bellard | tb->tb_next_offset[0] = 0xffff; |
106 | d19893da | bellard | tb->tb_next_offset[1] = 0xffff; |
107 | d19893da | bellard | gen_code_buf = tb->tc_ptr; |
108 | 4cbb86e1 | bellard | #ifdef USE_DIRECT_JUMP
|
109 | 4cbb86e1 | bellard | /* the following two entries are optional (only used for string ops) */
|
110 | 4cbb86e1 | bellard | tb->tb_jmp_offset[2] = 0xffff; |
111 | 4cbb86e1 | bellard | tb->tb_jmp_offset[3] = 0xffff; |
112 | 4cbb86e1 | bellard | #endif
|
113 | d19893da | bellard | gen_code_size = dyngen_code(gen_code_buf, tb->tb_next_offset, |
114 | d19893da | bellard | #ifdef USE_DIRECT_JUMP
|
115 | d19893da | bellard | tb->tb_jmp_offset, |
116 | d19893da | bellard | #else
|
117 | d19893da | bellard | NULL,
|
118 | d19893da | bellard | #endif
|
119 | d19893da | bellard | gen_opc_buf, gen_opparam_buf); |
120 | d19893da | bellard | *gen_code_size_ptr = gen_code_size; |
121 | d19893da | bellard | #ifdef DEBUG_DISAS
|
122 | d19893da | bellard | if (loglevel) {
|
123 | d19893da | bellard | fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
|
124 | d19893da | bellard | disas(logfile, gen_code_buf, *gen_code_size_ptr, 1, 0); |
125 | d19893da | bellard | fprintf(logfile, "\n");
|
126 | d19893da | bellard | fflush(logfile); |
127 | d19893da | bellard | } |
128 | d19893da | bellard | #endif
|
129 | d19893da | bellard | return 0; |
130 | d19893da | bellard | } |
131 | d19893da | bellard | |
132 | d19893da | bellard | static const unsigned short opc_copy_size[] = { |
133 | d19893da | bellard | #define DEF(s, n, copy_size) copy_size,
|
134 | d3eead2e | bellard | #include "opc.h" |
135 | d19893da | bellard | #undef DEF
|
136 | d19893da | bellard | }; |
137 | d19893da | bellard | |
138 | f76af4b3 | bellard | /* The cpu state corresponding to 'searched_pc' is restored.
|
139 | d19893da | bellard | */
|
140 | f76af4b3 | bellard | int cpu_restore_state(TranslationBlock *tb,
|
141 | f76af4b3 | bellard | CPUState *env, unsigned long searched_pc) |
142 | d19893da | bellard | { |
143 | d19893da | bellard | int j, c;
|
144 | d19893da | bellard | unsigned long tc_ptr; |
145 | d19893da | bellard | uint16_t *opc_ptr; |
146 | d19893da | bellard | |
147 | 4c3a88a2 | bellard | if (gen_intermediate_code_pc(env, tb) < 0) |
148 | d19893da | bellard | return -1; |
149 | d19893da | bellard | |
150 | d19893da | bellard | /* find opc index corresponding to search_pc */
|
151 | d19893da | bellard | tc_ptr = (unsigned long)tb->tc_ptr; |
152 | d19893da | bellard | if (searched_pc < tc_ptr)
|
153 | d19893da | bellard | return -1; |
154 | d19893da | bellard | j = 0;
|
155 | d19893da | bellard | opc_ptr = gen_opc_buf; |
156 | d19893da | bellard | for(;;) {
|
157 | d19893da | bellard | c = *opc_ptr; |
158 | d19893da | bellard | if (c == INDEX_op_end)
|
159 | d19893da | bellard | return -1; |
160 | d19893da | bellard | tc_ptr += opc_copy_size[c]; |
161 | d19893da | bellard | if (searched_pc < tc_ptr)
|
162 | d19893da | bellard | break;
|
163 | d19893da | bellard | opc_ptr++; |
164 | d19893da | bellard | } |
165 | d19893da | bellard | j = opc_ptr - gen_opc_buf; |
166 | d19893da | bellard | /* now find start of instruction before */
|
167 | d19893da | bellard | while (gen_opc_instr_start[j] == 0) |
168 | d19893da | bellard | j--; |
169 | f76af4b3 | bellard | #if defined(TARGET_I386)
|
170 | f76af4b3 | bellard | { |
171 | f76af4b3 | bellard | int cc_op;
|
172 | 3c1cf9fa | bellard | #ifdef DEBUG_DISAS
|
173 | 3c1cf9fa | bellard | if (loglevel) {
|
174 | 3c1cf9fa | bellard | int i;
|
175 | 6e0374f6 | bellard | fprintf(logfile, "RESTORE:\n");
|
176 | 3c1cf9fa | bellard | for(i=0;i<=j; i++) { |
177 | 3c1cf9fa | bellard | if (gen_opc_instr_start[i]) {
|
178 | 6e0374f6 | bellard | fprintf(logfile, "0x%04x: 0x%08x\n", i, gen_opc_pc[i]);
|
179 | 3c1cf9fa | bellard | } |
180 | 3c1cf9fa | bellard | } |
181 | 6e0374f6 | bellard | fprintf(logfile, "spc=0x%08lx j=0x%x eip=0x%lx cs_base=%lx\n",
|
182 | 6e0374f6 | bellard | searched_pc, j, gen_opc_pc[j] - tb->cs_base, tb->cs_base); |
183 | 3c1cf9fa | bellard | } |
184 | 3c1cf9fa | bellard | #endif
|
185 | f76af4b3 | bellard | env->eip = gen_opc_pc[j] - tb->cs_base; |
186 | f76af4b3 | bellard | cc_op = gen_opc_cc_op[j]; |
187 | f76af4b3 | bellard | if (cc_op != CC_OP_DYNAMIC)
|
188 | f76af4b3 | bellard | env->cc_op = cc_op; |
189 | f76af4b3 | bellard | } |
190 | f76af4b3 | bellard | #elif defined(TARGET_ARM)
|
191 | f76af4b3 | bellard | env->regs[15] = gen_opc_pc[j];
|
192 | d3eead2e | bellard | #elif defined(TARGET_SPARC)
|
193 | d3eead2e | bellard | env->pc = gen_opc_pc[j]; |
194 | f76af4b3 | bellard | #endif
|
195 | d19893da | bellard | return 0; |
196 | d19893da | bellard | } |
197 | d19893da | bellard |