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, see <http://www.gnu.org/licenses/>.
|
---|
18 | */
|
---|
19 |
|
---|
20 | /*
|
---|
21 | * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
|
---|
22 | * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
|
---|
23 | * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
|
---|
24 | * a choice of LGPL license versions is made available with the language indicating
|
---|
25 | * that LGPLv2 or any later version may be used, or where a choice of which version
|
---|
26 | * of the LGPL is applied is otherwise unspecified.
|
---|
27 | */
|
---|
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 | /* 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) || defined(TARGET_SH4)
|
---|
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(void)
|
---|
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, int *gen_code_size_ptr)
|
---|
96 | {
|
---|
97 | TCGContext *s = &tcg_ctx;
|
---|
98 | uint8_t *gen_code_buf;
|
---|
99 | int gen_code_size;
|
---|
100 | #ifdef CONFIG_PROFILER
|
---|
101 | int64_t ti;
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | #ifdef CONFIG_PROFILER
|
---|
105 | s->tb_count1++; /* includes aborted translations because of
|
---|
106 | exceptions */
|
---|
107 | ti = profile_getclock();
|
---|
108 | #endif
|
---|
109 |
|
---|
110 | #ifdef VBOX
|
---|
111 | RAWEx_ProfileStart(env, STATS_QEMU_COMPILATION);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | tcg_func_start(s);
|
---|
115 |
|
---|
116 | gen_intermediate_code(env, tb);
|
---|
117 |
|
---|
118 | /* generate machine code */
|
---|
119 | gen_code_buf = tb->tc_ptr;
|
---|
120 | tb->tb_next_offset[0] = 0xffff;
|
---|
121 | tb->tb_next_offset[1] = 0xffff;
|
---|
122 | s->tb_next_offset = tb->tb_next_offset;
|
---|
123 | #ifdef USE_DIRECT_JUMP
|
---|
124 | s->tb_jmp_offset = tb->tb_jmp_offset;
|
---|
125 | s->tb_next = NULL;
|
---|
126 | /* the following two entries are optional (only used for string ops) */
|
---|
127 | /* XXX: not used ? */
|
---|
128 | tb->tb_jmp_offset[2] = 0xffff;
|
---|
129 | tb->tb_jmp_offset[3] = 0xffff;
|
---|
130 | #else
|
---|
131 | s->tb_jmp_offset = NULL;
|
---|
132 | s->tb_next = tb->tb_next;
|
---|
133 | #endif
|
---|
134 |
|
---|
135 | #ifdef CONFIG_PROFILER
|
---|
136 | s->tb_count++;
|
---|
137 | s->interm_time += profile_getclock() - ti;
|
---|
138 | s->code_time -= profile_getclock();
|
---|
139 | #endif
|
---|
140 | gen_code_size = tcg_gen_code(s, gen_code_buf);
|
---|
141 | *gen_code_size_ptr = gen_code_size;
|
---|
142 | #ifdef CONFIG_PROFILER
|
---|
143 | s->code_time += profile_getclock();
|
---|
144 | s->code_in_len += tb->size;
|
---|
145 | s->code_out_len += gen_code_size;
|
---|
146 | #endif
|
---|
147 |
|
---|
148 | #ifdef VBOX
|
---|
149 | RAWEx_ProfileStop(env, STATS_QEMU_COMPILATION);
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | #ifdef DEBUG_DISAS
|
---|
153 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
|
---|
154 | qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
|
---|
155 | log_disas(tb->tc_ptr, *gen_code_size_ptr);
|
---|
156 | qemu_log("\n");
|
---|
157 | qemu_log_flush();
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 | return 0;
|
---|
161 | }
|
---|
162 |
|
---|
163 | /* The cpu state corresponding to 'searched_pc' is restored.
|
---|
164 | */
|
---|
165 | int cpu_restore_state(TranslationBlock *tb,
|
---|
166 | CPUState *env, unsigned long searched_pc,
|
---|
167 | void *puc)
|
---|
168 | {
|
---|
169 | TCGContext *s = &tcg_ctx;
|
---|
170 | int j;
|
---|
171 | unsigned long tc_ptr;
|
---|
172 | #ifdef CONFIG_PROFILER
|
---|
173 | int64_t ti;
|
---|
174 | #endif
|
---|
175 |
|
---|
176 | #ifdef CONFIG_PROFILER
|
---|
177 | ti = profile_getclock();
|
---|
178 | #endif
|
---|
179 | tcg_func_start(s);
|
---|
180 |
|
---|
181 | gen_intermediate_code_pc(env, tb);
|
---|
182 |
|
---|
183 | if (use_icount) {
|
---|
184 | /* Reset the cycle counter to the start of the block. */
|
---|
185 | env->icount_decr.u16.low += tb->icount;
|
---|
186 | /* Clear the IO flag. */
|
---|
187 | env->can_do_io = 0;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /* find opc index corresponding to search_pc */
|
---|
191 | tc_ptr = (unsigned long)tb->tc_ptr;
|
---|
192 | if (searched_pc < tc_ptr)
|
---|
193 | return -1;
|
---|
194 |
|
---|
195 | s->tb_next_offset = tb->tb_next_offset;
|
---|
196 | #ifdef USE_DIRECT_JUMP
|
---|
197 | s->tb_jmp_offset = tb->tb_jmp_offset;
|
---|
198 | s->tb_next = NULL;
|
---|
199 | #else
|
---|
200 | s->tb_jmp_offset = NULL;
|
---|
201 | s->tb_next = tb->tb_next;
|
---|
202 | #endif
|
---|
203 | j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
|
---|
204 | if (j < 0)
|
---|
205 | return -1;
|
---|
206 | /* now find start of instruction before */
|
---|
207 | while (gen_opc_instr_start[j] == 0)
|
---|
208 | j--;
|
---|
209 | env->icount_decr.u16.low -= gen_opc_icount[j];
|
---|
210 |
|
---|
211 | gen_pc_load(env, tb, searched_pc, j, puc);
|
---|
212 |
|
---|
213 | #ifdef CONFIG_PROFILER
|
---|
214 | s->restore_time += profile_getclock() - ti;
|
---|
215 | s->restore_count++;
|
---|
216 | #endif
|
---|
217 | return 0;
|
---|
218 | }
|
---|