]>
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 | |
8167ee88 | 17 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
d19893da FB |
18 | */ |
19 | #include <stdarg.h> | |
20 | #include <stdlib.h> | |
21 | #include <stdio.h> | |
22 | #include <string.h> | |
23 | #include <inttypes.h> | |
24 | ||
25 | #include "config.h" | |
2054396a | 26 | |
af5ad107 | 27 | #define NO_CPU_IO_DEFS |
d3eead2e FB |
28 | #include "cpu.h" |
29 | #include "exec-all.h" | |
d19893da | 30 | #include "disas.h" |
57fec1fe | 31 | #include "tcg.h" |
29e922b6 | 32 | #include "qemu-timer.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]; | |
2e70f6ef | 41 | uint16_t gen_opc_icount[OPC_BUF_SIZE]; |
d19893da | 42 | uint8_t gen_opc_instr_start[OPC_BUF_SIZE]; |
d19893da | 43 | |
57fec1fe | 44 | /* XXX: suppress that */ |
d07bde88 BS |
45 | unsigned long code_gen_max_block_size(void) |
46 | { | |
47 | static unsigned long max; | |
48 | ||
49 | if (max == 0) { | |
a208e54a | 50 | max = TCG_MAX_OP_SIZE; |
d07bde88 | 51 | #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max; |
57fec1fe | 52 | #include "tcg-opc.h" |
d07bde88 BS |
53 | #undef DEF |
54 | max *= OPC_MAX_SIZE; | |
55 | } | |
56 | ||
57 | return max; | |
58 | } | |
59 | ||
57fec1fe FB |
60 | void cpu_gen_init(void) |
61 | { | |
62 | tcg_context_init(&tcg_ctx); | |
63 | tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf), | |
a20e31dc | 64 | CPU_TEMP_BUF_NLONGS * sizeof(long)); |
57fec1fe FB |
65 | } |
66 | ||
d19893da | 67 | /* return non zero if the very first instruction is invalid so that |
5fafdf24 | 68 | the virtual CPU can trigger an exception. |
d19893da FB |
69 | |
70 | '*gen_code_size_ptr' contains the size of the generated code (host | |
71 | code). | |
72 | */ | |
d07bde88 | 73 | int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr) |
d19893da | 74 | { |
57fec1fe | 75 | TCGContext *s = &tcg_ctx; |
d19893da FB |
76 | uint8_t *gen_code_buf; |
77 | int gen_code_size; | |
57fec1fe FB |
78 | #ifdef CONFIG_PROFILER |
79 | int64_t ti; | |
80 | #endif | |
81 | ||
82 | #ifdef CONFIG_PROFILER | |
b67d9a52 FB |
83 | s->tb_count1++; /* includes aborted translations because of |
84 | exceptions */ | |
57fec1fe FB |
85 | ti = profile_getclock(); |
86 | #endif | |
87 | tcg_func_start(s); | |
d19893da | 88 | |
2cfc5f17 TS |
89 | gen_intermediate_code(env, tb); |
90 | ||
ec6338ba | 91 | /* generate machine code */ |
57fec1fe | 92 | gen_code_buf = tb->tc_ptr; |
ec6338ba FB |
93 | tb->tb_next_offset[0] = 0xffff; |
94 | tb->tb_next_offset[1] = 0xffff; | |
57fec1fe | 95 | s->tb_next_offset = tb->tb_next_offset; |
4cbb86e1 | 96 | #ifdef USE_DIRECT_JUMP |
57fec1fe FB |
97 | s->tb_jmp_offset = tb->tb_jmp_offset; |
98 | s->tb_next = NULL; | |
d19893da | 99 | #else |
57fec1fe FB |
100 | s->tb_jmp_offset = NULL; |
101 | s->tb_next = tb->tb_next; | |
d19893da | 102 | #endif |
57fec1fe FB |
103 | |
104 | #ifdef CONFIG_PROFILER | |
b67d9a52 FB |
105 | s->tb_count++; |
106 | s->interm_time += profile_getclock() - ti; | |
107 | s->code_time -= profile_getclock(); | |
57fec1fe | 108 | #endif |
54604f74 | 109 | gen_code_size = tcg_gen_code(s, gen_code_buf); |
d19893da | 110 | *gen_code_size_ptr = gen_code_size; |
57fec1fe | 111 | #ifdef CONFIG_PROFILER |
b67d9a52 FB |
112 | s->code_time += profile_getclock(); |
113 | s->code_in_len += tb->size; | |
114 | s->code_out_len += gen_code_size; | |
57fec1fe FB |
115 | #endif |
116 | ||
d19893da | 117 | #ifdef DEBUG_DISAS |
8fec2b8c | 118 | if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) { |
93fcfe39 AL |
119 | qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr); |
120 | log_disas(tb->tc_ptr, *gen_code_size_ptr); | |
121 | qemu_log("\n"); | |
31b1a7b4 | 122 | qemu_log_flush(); |
d19893da FB |
123 | } |
124 | #endif | |
125 | return 0; | |
126 | } | |
127 | ||
5fafdf24 | 128 | /* The cpu state corresponding to 'searched_pc' is restored. |
d19893da | 129 | */ |
5fafdf24 | 130 | int cpu_restore_state(TranslationBlock *tb, |
58fe2f10 FB |
131 | CPUState *env, unsigned long searched_pc, |
132 | void *puc) | |
d19893da | 133 | { |
57fec1fe FB |
134 | TCGContext *s = &tcg_ctx; |
135 | int j; | |
d19893da | 136 | unsigned long tc_ptr; |
57fec1fe FB |
137 | #ifdef CONFIG_PROFILER |
138 | int64_t ti; | |
139 | #endif | |
140 | ||
141 | #ifdef CONFIG_PROFILER | |
142 | ti = profile_getclock(); | |
143 | #endif | |
144 | tcg_func_start(s); | |
d19893da | 145 | |
2cfc5f17 | 146 | gen_intermediate_code_pc(env, tb); |
3b46e624 | 147 | |
2e70f6ef PB |
148 | if (use_icount) { |
149 | /* Reset the cycle counter to the start of the block. */ | |
150 | env->icount_decr.u16.low += tb->icount; | |
151 | /* Clear the IO flag. */ | |
152 | env->can_do_io = 0; | |
153 | } | |
154 | ||
d19893da FB |
155 | /* find opc index corresponding to search_pc */ |
156 | tc_ptr = (unsigned long)tb->tc_ptr; | |
157 | if (searched_pc < tc_ptr) | |
158 | return -1; | |
57fec1fe FB |
159 | |
160 | s->tb_next_offset = tb->tb_next_offset; | |
161 | #ifdef USE_DIRECT_JUMP | |
162 | s->tb_jmp_offset = tb->tb_jmp_offset; | |
163 | s->tb_next = NULL; | |
164 | #else | |
165 | s->tb_jmp_offset = NULL; | |
166 | s->tb_next = tb->tb_next; | |
167 | #endif | |
54604f74 | 168 | j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr); |
57fec1fe FB |
169 | if (j < 0) |
170 | return -1; | |
d19893da FB |
171 | /* now find start of instruction before */ |
172 | while (gen_opc_instr_start[j] == 0) | |
173 | j--; | |
2e70f6ef | 174 | env->icount_decr.u16.low -= gen_opc_icount[j]; |
3b46e624 | 175 | |
d2856f1a | 176 | gen_pc_load(env, tb, searched_pc, j, puc); |
57fec1fe FB |
177 | |
178 | #ifdef CONFIG_PROFILER | |
b67d9a52 FB |
179 | s->restore_time += profile_getclock() - ti; |
180 | s->restore_count++; | |
57fec1fe | 181 | #endif |
d19893da FB |
182 | return 0; |
183 | } |