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 | * Oracle 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, Oracle 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 |
|
---|
30 | #include <stdarg.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <stdio.h>
|
---|
33 | #include <string.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 | /* code generation context */
|
---|
44 | TCGContext tcg_ctx;
|
---|
45 |
|
---|
46 | uint16_t gen_opc_buf[OPC_BUF_SIZE];
|
---|
47 | TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
|
---|
48 |
|
---|
49 | target_ulong gen_opc_pc[OPC_BUF_SIZE];
|
---|
50 | uint16_t gen_opc_icount[OPC_BUF_SIZE];
|
---|
51 | uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
|
---|
52 | #if defined(TARGET_I386)
|
---|
53 | uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
|
---|
54 | #elif defined(TARGET_SPARC)
|
---|
55 | target_ulong gen_opc_npc[OPC_BUF_SIZE];
|
---|
56 | target_ulong gen_opc_jump_pc[2];
|
---|
57 | #elif defined(TARGET_MIPS)
|
---|
58 | uint32_t gen_opc_hflags[OPC_BUF_SIZE];
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | /* XXX: suppress that */
|
---|
62 | unsigned long code_gen_max_block_size(void)
|
---|
63 | {
|
---|
64 | #ifdef VBOX
|
---|
65 | /* Just to suppress a lot of dummy warnings */
|
---|
66 | static long max;
|
---|
67 | #else
|
---|
68 | static unsigned long max;
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | if (max == 0) {
|
---|
72 | max = TCG_MAX_OP_SIZE;
|
---|
73 | #define DEF(s, n, copy_size) max = (copy_size > max) ? copy_size : max;
|
---|
74 | #include "tcg-opc.h"
|
---|
75 | #undef DEF
|
---|
76 | max *= OPC_MAX_SIZE;
|
---|
77 | }
|
---|
78 |
|
---|
79 | return max;
|
---|
80 | }
|
---|
81 |
|
---|
82 | void cpu_gen_init()
|
---|
83 | {
|
---|
84 | tcg_context_init(&tcg_ctx);
|
---|
85 | tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
|
---|
86 | CPU_TEMP_BUF_NLONGS * sizeof(long));
|
---|
87 | }
|
---|
88 |
|
---|
89 | /* return non zero if the very first instruction is invalid so that
|
---|
90 | the virtual CPU can trigger an exception.
|
---|
91 |
|
---|
92 | '*gen_code_size_ptr' contains the size of the generated code (host
|
---|
93 | code).
|
---|
94 | */
|
---|
95 | int cpu_gen_code(CPUState *env, TranslationBlock *tb,
|
---|
96 | int *gen_code_size_ptr)
|
---|
97 | {
|
---|
98 | TCGContext *s = &tcg_ctx;
|
---|
99 | uint8_t *gen_code_buf;
|
---|
100 | int gen_code_size;
|
---|
101 | #ifdef CONFIG_PROFILER
|
---|
102 | int64_t ti;
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | #ifdef CONFIG_PROFILER
|
---|
106 | s->tb_count1++; /* includes aborted translations because of
|
---|
107 | exceptions */
|
---|
108 | ti = profile_getclock();
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | #ifdef VBOX
|
---|
112 | RAWEx_ProfileStart(env, STATS_QEMU_COMPILATION);
|
---|
113 | tcg_func_start(s);
|
---|
114 |
|
---|
115 | gen_intermediate_code(env, tb);
|
---|
116 | #else /* !VBOX */
|
---|
117 | tcg_func_start(s);
|
---|
118 |
|
---|
119 | gen_intermediate_code(env, tb);
|
---|
120 | #endif /* !VBOX */
|
---|
121 |
|
---|
122 | /* generate machine code */
|
---|
123 | gen_code_buf = tb->tc_ptr;
|
---|
124 | tb->tb_next_offset[0] = 0xffff;
|
---|
125 | tb->tb_next_offset[1] = 0xffff;
|
---|
126 | s->tb_next_offset = tb->tb_next_offset;
|
---|
127 | #ifdef USE_DIRECT_JUMP
|
---|
128 | s->tb_jmp_offset = tb->tb_jmp_offset;
|
---|
129 | s->tb_next = NULL;
|
---|
130 | /* the following two entries are optional (only used for string ops) */
|
---|
131 | tb->tb_jmp_offset[2] = 0xffff;
|
---|
132 | tb->tb_jmp_offset[3] = 0xffff;
|
---|
133 | #else
|
---|
134 | s->tb_jmp_offset = NULL;
|
---|
135 | s->tb_next = tb->tb_next;
|
---|
136 | #endif
|
---|
137 |
|
---|
138 | #ifdef CONFIG_PROFILER
|
---|
139 | s->tb_count++;
|
---|
140 | s->interm_time += profile_getclock() - ti;
|
---|
141 | s->code_time -= profile_getclock();
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | gen_code_size = dyngen_code(s, gen_code_buf);
|
---|
145 | *gen_code_size_ptr = gen_code_size;
|
---|
146 | #ifdef CONFIG_PROFILER
|
---|
147 | s->code_time += profile_getclock();
|
---|
148 | s->code_in_len += tb->size;
|
---|
149 | s->code_out_len += gen_code_size;
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | #ifdef VBOX
|
---|
153 | RAWEx_ProfileStop(env, STATS_QEMU_COMPILATION);
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | #ifdef DEBUG_DISAS
|
---|
157 | if (loglevel & CPU_LOG_TB_OUT_ASM) {
|
---|
158 | fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
|
---|
159 | disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
|
---|
160 | fprintf(logfile, "\n");
|
---|
161 | fflush(logfile);
|
---|
162 | }
|
---|
163 | #endif
|
---|
164 | return 0;
|
---|
165 | }
|
---|
166 |
|
---|
167 | /* The cpu state corresponding to 'searched_pc' is restored.
|
---|
168 | */
|
---|
169 | int cpu_restore_state(TranslationBlock *tb,
|
---|
170 | CPUState *env, unsigned long searched_pc,
|
---|
171 | void *puc)
|
---|
172 | {
|
---|
173 | TCGContext *s = &tcg_ctx;
|
---|
174 | int j;
|
---|
175 | unsigned long tc_ptr;
|
---|
176 | #ifdef CONFIG_PROFILER
|
---|
177 | int64_t ti;
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | #ifdef CONFIG_PROFILER
|
---|
181 | ti = profile_getclock();
|
---|
182 | #endif
|
---|
183 | tcg_func_start(s);
|
---|
184 |
|
---|
185 | gen_intermediate_code_pc(env, tb);
|
---|
186 |
|
---|
187 | if (use_icount) {
|
---|
188 | /* Reset the cycle counter to the start of the block. */
|
---|
189 | env->icount_decr.u16.low += tb->icount;
|
---|
190 | /* Clear the IO flag. */
|
---|
191 | env->can_do_io = 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* find opc index corresponding to search_pc */
|
---|
195 | tc_ptr = (unsigned long)tb->tc_ptr;
|
---|
196 | if (searched_pc < tc_ptr)
|
---|
197 | return -1;
|
---|
198 |
|
---|
199 | s->tb_next_offset = tb->tb_next_offset;
|
---|
200 | #ifdef USE_DIRECT_JUMP
|
---|
201 | s->tb_jmp_offset = tb->tb_jmp_offset;
|
---|
202 | s->tb_next = NULL;
|
---|
203 | #else
|
---|
204 | s->tb_jmp_offset = NULL;
|
---|
205 | s->tb_next = tb->tb_next;
|
---|
206 | #endif
|
---|
207 | j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
|
---|
208 | if (j < 0)
|
---|
209 | return -1;
|
---|
210 | /* now find start of instruction before */
|
---|
211 | while (gen_opc_instr_start[j] == 0)
|
---|
212 | j--;
|
---|
213 | env->icount_decr.u16.low -= gen_opc_icount[j];
|
---|
214 |
|
---|
215 | gen_pc_load(env, tb, searched_pc, j, puc);
|
---|
216 |
|
---|
217 | #ifdef CONFIG_PROFILER
|
---|
218 | s->restore_time += profile_getclock() - ti;
|
---|
219 | s->restore_count++;
|
---|
220 | #endif
|
---|
221 | return 0;
|
---|
222 | }
|
---|