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