1 | /*
|
---|
2 | * Host code generation
|
---|
3 | *
|
---|
4 | * Copyright (c) 2003 Fabrice Bellard
|
---|
5 | *
|
---|
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.
|
---|
10 | *
|
---|
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.
|
---|
15 | *
|
---|
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
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
23 | * other than GPL or LGPL is available it will apply instead, Sun elects to use only
|
---|
24 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
25 | * a choice of LGPL license versions is made available with the language indicating
|
---|
26 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
27 | * of the LGPL is applied is otherwise unspecified.
|
---|
28 | */
|
---|
29 | #include <stdarg.h>
|
---|
30 | #include <stdlib.h>
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <inttypes.h>
|
---|
34 |
|
---|
35 | #include "config.h"
|
---|
36 |
|
---|
37 | #define NO_CPU_IO_DEFS
|
---|
38 | #include "cpu.h"
|
---|
39 | #include "exec-all.h"
|
---|
40 | #include "disas.h"
|
---|
41 | #include "tcg.h"
|
---|
42 |
|
---|
43 | #ifndef VBOX
|
---|
44 | /* code generation context */
|
---|
45 | TCGContext tcg_ctx;
|
---|
46 | #else
|
---|
47 | TCGContext g_tcg_ctx;
|
---|
48 | static TCGContext* getCompilerCtx(CPUState *env)
|
---|
49 | {
|
---|
50 | /** @todo nike: should be field in CPU env */
|
---|
51 | //return env->tcg_context;
|
---|
52 | return &g_tcg_ctx;
|
---|
53 | }
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | uint16_t gen_opc_buf[OPC_BUF_SIZE];
|
---|
57 | uint32_t gen_opparam_buf[OPPARAM_BUF_SIZE];
|
---|
58 | long gen_labels[OPC_BUF_SIZE];
|
---|
59 | int nb_gen_labels;
|
---|
60 |
|
---|
61 | target_ulong gen_opc_pc[OPC_BUF_SIZE];
|
---|
62 | uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
|
---|
63 | #if defined(TARGET_I386)
|
---|
64 | uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
|
---|
65 | #elif defined(TARGET_SPARC)
|
---|
66 | target_ulong gen_opc_npc[OPC_BUF_SIZE];
|
---|
67 | target_ulong gen_opc_jump_pc[2];
|
---|
68 | #elif defined(TARGET_MIPS)
|
---|
69 | uint32_t gen_opc_hflags[OPC_BUF_SIZE];
|
---|
70 | #endif
|
---|
71 |
|
---|
72 | /* XXX: suppress that */
|
---|
73 | unsigned long code_gen_max_block_size(void)
|
---|
74 | {
|
---|
75 | static unsigned long max;
|
---|
76 |
|
---|
77 | if (max == 0) {
|
---|
78 | max = TCG_MAX_OP_SIZE;
|
---|
79 | #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
|
---|
80 | #include "tcg-opc.h"
|
---|
81 | #undef DEF
|
---|
82 | max *= OPC_MAX_SIZE;
|
---|
83 | }
|
---|
84 |
|
---|
85 | return max;
|
---|
86 | }
|
---|
87 |
|
---|
88 | #ifndef VBOX
|
---|
89 | void cpu_gen_init()
|
---|
90 | {
|
---|
91 | tcg_context_init(&tcg_ctx);
|
---|
92 | tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
|
---|
93 | CPU_TEMP_BUF_NLONGS * sizeof(long));
|
---|
94 | }
|
---|
95 | #else
|
---|
96 | void cpu_gen_init(CPUState *env)
|
---|
97 | {
|
---|
98 | TCGContext* tcg_ctx = getCompilerCtx(env);
|
---|
99 | tcg_context_init(tcg_ctx);
|
---|
100 | tcg_set_frame(tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
|
---|
101 | CPU_TEMP_BUF_NLONGS * sizeof(long));
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | /* return non zero if the very first instruction is invalid so that
|
---|
106 | the virtual CPU can trigger an exception.
|
---|
107 |
|
---|
108 | '*gen_code_size_ptr' contains the size of the generated code (host
|
---|
109 | code).
|
---|
110 | */
|
---|
111 | int cpu_gen_code(CPUState *env, TranslationBlock *tb,
|
---|
112 | int max_code_size, int *gen_code_size_ptr)
|
---|
113 | {
|
---|
114 | #ifdef VBOX
|
---|
115 | TCGContext *s = getCompilerCtx(env);
|
---|
116 | #else
|
---|
117 | TCGContext *s = &tcg_ctx;
|
---|
118 | #endif
|
---|
119 | uint8_t *gen_code_buf;
|
---|
120 | int gen_code_size;
|
---|
121 | #ifdef CONFIG_PROFILER
|
---|
122 | int64_t ti;
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #ifdef CONFIG_PROFILER
|
---|
126 | s->tb_count1++; /* includes aborted translations because of
|
---|
127 | exceptions */
|
---|
128 | ti = profile_getclock();
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | #ifdef VBOX
|
---|
132 | RAWEx_ProfileStart(env, STATS_QEMU_COMPILATION);
|
---|
133 | tcg_func_start(s);
|
---|
134 |
|
---|
135 | if (gen_intermediate_code(env, tb) < 0)
|
---|
136 | {
|
---|
137 | RAWEx_ProfileStop(env, STATS_QEMU_COMPILATION);
|
---|
138 | return -1;
|
---|
139 | }
|
---|
140 | #else /* !VBOX */
|
---|
141 | tcg_func_start(s);
|
---|
142 |
|
---|
143 | if (gen_intermediate_code(env, tb) < 0)
|
---|
144 | return -1;
|
---|
145 | #endif /* !VBOX */
|
---|
146 |
|
---|
147 | /* generate machine code */
|
---|
148 | gen_code_buf = tb->tc_ptr;
|
---|
149 | tb->tb_next_offset[0] = 0xffff;
|
---|
150 | tb->tb_next_offset[1] = 0xffff;
|
---|
151 | s->tb_next_offset = tb->tb_next_offset;
|
---|
152 | #ifdef USE_DIRECT_JUMP
|
---|
153 | s->tb_jmp_offset = tb->tb_jmp_offset;
|
---|
154 | s->tb_next = NULL;
|
---|
155 | /* the following two entries are optional (only used for string ops) */
|
---|
156 | tb->tb_jmp_offset[2] = 0xffff;
|
---|
157 | tb->tb_jmp_offset[3] = 0xffff;
|
---|
158 | #else
|
---|
159 | s->tb_jmp_offset = NULL;
|
---|
160 | s->tb_next = tb->tb_next;
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | #ifdef CONFIG_PROFILER
|
---|
164 | s->tb_count++;
|
---|
165 | s->interm_time += profile_getclock() - ti;
|
---|
166 | s->code_time -= profile_getclock();
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | gen_code_size = dyngen_code(s, gen_code_buf);
|
---|
170 | *gen_code_size_ptr = gen_code_size;
|
---|
171 | #ifdef CONFIG_PROFILER
|
---|
172 | s->code_time += profile_getclock();
|
---|
173 | s->code_in_len += tb->size;
|
---|
174 | s->code_out_len += gen_code_size;
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | #ifdef VBOX
|
---|
178 | RAWEx_ProfileStop(env, STATS_QEMU_COMPILATION);
|
---|
179 | #endif
|
---|
180 |
|
---|
181 | #ifdef DEBUG_DISAS
|
---|
182 | if (loglevel & CPU_LOG_TB_OUT_ASM) {
|
---|
183 | fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
|
---|
184 | disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
|
---|
185 | fprintf(logfile, "\n");
|
---|
186 | fflush(logfile);
|
---|
187 | }
|
---|
188 | #endif
|
---|
189 | return 0;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /* The cpu state corresponding to 'searched_pc' is restored.
|
---|
193 | */
|
---|
194 | int cpu_restore_state(TranslationBlock *tb,
|
---|
195 | CPUState *env, unsigned long searched_pc,
|
---|
196 | void *puc)
|
---|
197 | {
|
---|
198 | #ifndef VBOX
|
---|
199 | TCGContext *s = &tcg_ctx;
|
---|
200 | #else
|
---|
201 | TCGContext *s = getCompilerCtx(env);
|
---|
202 | #endif
|
---|
203 | int j;
|
---|
204 | unsigned long tc_ptr;
|
---|
205 | #ifdef CONFIG_PROFILER
|
---|
206 | int64_t ti;
|
---|
207 | #endif
|
---|
208 |
|
---|
209 | #ifdef CONFIG_PROFILER
|
---|
210 | ti = profile_getclock();
|
---|
211 | #endif
|
---|
212 | tcg_func_start(s);
|
---|
213 |
|
---|
214 | #ifdef VBOX
|
---|
215 | /** @todo: what's right here? */
|
---|
216 | if (gen_intermediate_code_pc(env, tb) < 0)
|
---|
217 | return -1;
|
---|
218 | #else
|
---|
219 | gen_intermediate_code_pc(env, tb);
|
---|
220 | #endif
|
---|
221 |
|
---|
222 | if (use_icount) {
|
---|
223 | /* Reset the cycle counter to the start of the block. */
|
---|
224 | env->icount_decr.u16.low += tb->icount;
|
---|
225 | /* Clear the IO flag. */
|
---|
226 | env->can_do_io = 0;
|
---|
227 | }
|
---|
228 |
|
---|
229 | /* find opc index corresponding to search_pc */
|
---|
230 | tc_ptr = (unsigned long)tb->tc_ptr;
|
---|
231 | if (searched_pc < tc_ptr)
|
---|
232 | return -1;
|
---|
233 |
|
---|
234 | s->tb_next_offset = tb->tb_next_offset;
|
---|
235 | #ifdef USE_DIRECT_JUMP
|
---|
236 | s->tb_jmp_offset = tb->tb_jmp_offset;
|
---|
237 | s->tb_next = NULL;
|
---|
238 | #else
|
---|
239 | s->tb_jmp_offset = NULL;
|
---|
240 | s->tb_next = tb->tb_next;
|
---|
241 | #endif
|
---|
242 | j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
|
---|
243 | if (j < 0)
|
---|
244 | return -1;
|
---|
245 | /* now find start of instruction before */
|
---|
246 | while (gen_opc_instr_start[j] == 0)
|
---|
247 | j--;
|
---|
248 | env->icount_decr.u16.low -= gen_opc_icount[j];
|
---|
249 |
|
---|
250 | gen_pc_load(env, tb, searched_pc, j, puc);
|
---|
251 |
|
---|
252 | #ifdef CONFIG_PROFILER
|
---|
253 | s->restore_time += profile_getclock() - ti;
|
---|
254 | s->restore_count++;
|
---|
255 | #endif
|
---|
256 | return 0;
|
---|
257 | }
|
---|