]>
Commit | Line | Data |
---|---|---|
d19893da FB |
1 | /* |
2 | * Host code generation | |
5fafdf24 | 3 | * |
d19893da FB |
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 | #include <stdarg.h> | |
21 | #include <stdlib.h> | |
22 | #include <stdio.h> | |
23 | #include <string.h> | |
24 | #include <inttypes.h> | |
25 | ||
26 | #include "config.h" | |
2054396a | 27 | |
af5ad107 | 28 | #define NO_CPU_IO_DEFS |
d3eead2e FB |
29 | #include "cpu.h" |
30 | #include "exec-all.h" | |
d19893da | 31 | #include "disas.h" |
57fec1fe | 32 | #include "tcg.h" |
d19893da | 33 | |
57fec1fe FB |
34 | /* code generation context */ |
35 | TCGContext tcg_ctx; | |
d19893da | 36 | |
d19893da | 37 | uint16_t gen_opc_buf[OPC_BUF_SIZE]; |
57fec1fe | 38 | TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE]; |
c4687878 FB |
39 | |
40 | target_ulong gen_opc_pc[OPC_BUF_SIZE]; | |
d19893da | 41 | uint8_t gen_opc_instr_start[OPC_BUF_SIZE]; |
f76af4b3 FB |
42 | #if defined(TARGET_I386) |
43 | uint8_t gen_opc_cc_op[OPC_BUF_SIZE]; | |
e95c8d51 | 44 | #elif defined(TARGET_SPARC) |
c4687878 | 45 | target_ulong gen_opc_npc[OPC_BUF_SIZE]; |
c3278b7b | 46 | target_ulong gen_opc_jump_pc[2]; |
823029f9 | 47 | #elif defined(TARGET_MIPS) || defined(TARGET_SH4) |
30d6cb84 | 48 | uint32_t gen_opc_hflags[OPC_BUF_SIZE]; |
f76af4b3 | 49 | #endif |
d19893da | 50 | |
57fec1fe FB |
51 | #ifdef CONFIG_PROFILER |
52 | int64_t dyngen_tb_count1; | |
53 | int64_t dyngen_tb_count; | |
54 | int64_t dyngen_op_count; | |
55 | int64_t dyngen_old_op_count; | |
56 | int64_t dyngen_tcg_del_op_count; | |
57 | int dyngen_op_count_max; | |
58 | int64_t dyngen_code_in_len; | |
59 | int64_t dyngen_code_out_len; | |
60 | int64_t dyngen_interm_time; | |
61 | int64_t dyngen_code_time; | |
62 | int64_t dyngen_restore_count; | |
63 | int64_t dyngen_restore_time; | |
d19893da FB |
64 | #endif |
65 | ||
57fec1fe | 66 | /* XXX: suppress that */ |
d07bde88 BS |
67 | unsigned long code_gen_max_block_size(void) |
68 | { | |
69 | static unsigned long max; | |
70 | ||
71 | if (max == 0) { | |
a208e54a | 72 | max = TCG_MAX_OP_SIZE; |
d07bde88 | 73 | #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max; |
57fec1fe | 74 | #include "tcg-opc.h" |
d07bde88 BS |
75 | #undef DEF |
76 | max *= OPC_MAX_SIZE; | |
77 | } | |
78 | ||
79 | return max; | |
80 | } | |
81 | ||
57fec1fe FB |
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), | |
a20e31dc | 86 | CPU_TEMP_BUF_NLONGS * sizeof(long)); |
57fec1fe FB |
87 | } |
88 | ||
d19893da | 89 | /* return non zero if the very first instruction is invalid so that |
5fafdf24 | 90 | the virtual CPU can trigger an exception. |
d19893da FB |
91 | |
92 | '*gen_code_size_ptr' contains the size of the generated code (host | |
93 | code). | |
94 | */ | |
d07bde88 | 95 | int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr) |
d19893da | 96 | { |
57fec1fe | 97 | TCGContext *s = &tcg_ctx; |
d19893da FB |
98 | uint8_t *gen_code_buf; |
99 | int gen_code_size; | |
57fec1fe FB |
100 | #ifdef CONFIG_PROFILER |
101 | int64_t ti; | |
102 | #endif | |
103 | ||
104 | #ifdef CONFIG_PROFILER | |
105 | dyngen_tb_count1++; /* includes aborted translations because of | |
106 | exceptions */ | |
107 | ti = profile_getclock(); | |
108 | #endif | |
109 | tcg_func_start(s); | |
d19893da | 110 | |
ec6338ba FB |
111 | if (gen_intermediate_code(env, tb) < 0) |
112 | return -1; | |
113 | ||
114 | /* generate machine code */ | |
57fec1fe | 115 | gen_code_buf = tb->tc_ptr; |
ec6338ba FB |
116 | tb->tb_next_offset[0] = 0xffff; |
117 | tb->tb_next_offset[1] = 0xffff; | |
57fec1fe | 118 | s->tb_next_offset = tb->tb_next_offset; |
4cbb86e1 | 119 | #ifdef USE_DIRECT_JUMP |
57fec1fe FB |
120 | s->tb_jmp_offset = tb->tb_jmp_offset; |
121 | s->tb_next = NULL; | |
ec6338ba | 122 | /* the following two entries are optional (only used for string ops) */ |
57fec1fe | 123 | /* XXX: not used ? */ |
ec6338ba FB |
124 | tb->tb_jmp_offset[2] = 0xffff; |
125 | tb->tb_jmp_offset[3] = 0xffff; | |
d19893da | 126 | #else |
57fec1fe FB |
127 | s->tb_jmp_offset = NULL; |
128 | s->tb_next = tb->tb_next; | |
d19893da | 129 | #endif |
57fec1fe FB |
130 | |
131 | #ifdef CONFIG_PROFILER | |
132 | dyngen_tb_count++; | |
133 | dyngen_interm_time += profile_getclock() - ti; | |
134 | dyngen_code_time -= profile_getclock(); | |
135 | #endif | |
136 | gen_code_size = dyngen_code(s, gen_code_buf); | |
d19893da | 137 | *gen_code_size_ptr = gen_code_size; |
57fec1fe FB |
138 | #ifdef CONFIG_PROFILER |
139 | dyngen_code_time += profile_getclock(); | |
140 | dyngen_code_in_len += tb->size; | |
141 | dyngen_code_out_len += gen_code_size; | |
142 | #endif | |
143 | ||
d19893da | 144 | #ifdef DEBUG_DISAS |
f193c797 | 145 | if (loglevel & CPU_LOG_TB_OUT_ASM) { |
d19893da | 146 | fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr); |
c4687878 | 147 | disas(logfile, tb->tc_ptr, *gen_code_size_ptr); |
d19893da FB |
148 | fprintf(logfile, "\n"); |
149 | fflush(logfile); | |
150 | } | |
151 | #endif | |
152 | return 0; | |
153 | } | |
154 | ||
5fafdf24 | 155 | /* The cpu state corresponding to 'searched_pc' is restored. |
d19893da | 156 | */ |
5fafdf24 | 157 | int cpu_restore_state(TranslationBlock *tb, |
58fe2f10 FB |
158 | CPUState *env, unsigned long searched_pc, |
159 | void *puc) | |
d19893da | 160 | { |
57fec1fe FB |
161 | TCGContext *s = &tcg_ctx; |
162 | int j; | |
d19893da | 163 | unsigned long tc_ptr; |
57fec1fe FB |
164 | #ifdef CONFIG_PROFILER |
165 | int64_t ti; | |
166 | #endif | |
167 | ||
168 | #ifdef CONFIG_PROFILER | |
169 | ti = profile_getclock(); | |
170 | #endif | |
171 | tcg_func_start(s); | |
d19893da | 172 | |
4c3a88a2 | 173 | if (gen_intermediate_code_pc(env, tb) < 0) |
d19893da | 174 | return -1; |
3b46e624 | 175 | |
d19893da FB |
176 | /* find opc index corresponding to search_pc */ |
177 | tc_ptr = (unsigned long)tb->tc_ptr; | |
178 | if (searched_pc < tc_ptr) | |
179 | return -1; | |
57fec1fe FB |
180 | |
181 | s->tb_next_offset = tb->tb_next_offset; | |
182 | #ifdef USE_DIRECT_JUMP | |
183 | s->tb_jmp_offset = tb->tb_jmp_offset; | |
184 | s->tb_next = NULL; | |
185 | #else | |
186 | s->tb_jmp_offset = NULL; | |
187 | s->tb_next = tb->tb_next; | |
188 | #endif | |
623e265c | 189 | j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr); |
57fec1fe FB |
190 | if (j < 0) |
191 | return -1; | |
d19893da FB |
192 | /* now find start of instruction before */ |
193 | while (gen_opc_instr_start[j] == 0) | |
194 | j--; | |
3b46e624 | 195 | |
d2856f1a | 196 | gen_pc_load(env, tb, searched_pc, j, puc); |
57fec1fe FB |
197 | |
198 | #ifdef CONFIG_PROFILER | |
199 | dyngen_restore_time += profile_getclock() - ti; | |
200 | dyngen_restore_count++; | |
201 | #endif | |
d19893da FB |
202 | return 0; |
203 | } |