update debian/changelog
[qemu] / translate-all.c
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 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
24
25 #include "config.h"
26
27 #define NO_CPU_IO_DEFS
28 #include "cpu.h"
29 #include "exec-all.h"
30 #include "disas.h"
31 #include "tcg.h"
32
33 /* code generation context */
34 TCGContext tcg_ctx;
35
36 uint16_t gen_opc_buf[OPC_BUF_SIZE];
37 TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
38
39 target_ulong gen_opc_pc[OPC_BUF_SIZE];
40 uint16_t gen_opc_icount[OPC_BUF_SIZE];
41 uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
42 #if defined(TARGET_I386)
43 uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
44 #elif defined(TARGET_SPARC)
45 target_ulong gen_opc_npc[OPC_BUF_SIZE];
46 target_ulong gen_opc_jump_pc[2];
47 #elif defined(TARGET_MIPS) || defined(TARGET_SH4)
48 uint32_t gen_opc_hflags[OPC_BUF_SIZE];
49 #endif
50
51 /* XXX: suppress that */
52 unsigned long code_gen_max_block_size(void)
53 {
54     static unsigned long max;
55
56     if (max == 0) {
57         max = TCG_MAX_OP_SIZE;
58 #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
59 #include "tcg-opc.h"
60 #undef DEF
61         max *= OPC_MAX_SIZE;
62     }
63
64     return max;
65 }
66
67 void cpu_gen_init(void)
68 {
69     tcg_context_init(&tcg_ctx); 
70     tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
71                   CPU_TEMP_BUF_NLONGS * sizeof(long));
72 }
73
74 /* return non zero if the very first instruction is invalid so that
75    the virtual CPU can trigger an exception.
76
77    '*gen_code_size_ptr' contains the size of the generated code (host
78    code).
79 */
80 int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
81 {
82     TCGContext *s = &tcg_ctx;
83     uint8_t *gen_code_buf;
84     int gen_code_size;
85 #ifdef CONFIG_PROFILER
86     int64_t ti;
87 #endif
88
89 #ifdef CONFIG_PROFILER
90     s->tb_count1++; /* includes aborted translations because of
91                        exceptions */
92     ti = profile_getclock();
93 #endif
94     tcg_func_start(s);
95
96     gen_intermediate_code(env, tb);
97
98     /* generate machine code */
99     gen_code_buf = tb->tc_ptr;
100     tb->tb_next_offset[0] = 0xffff;
101     tb->tb_next_offset[1] = 0xffff;
102     s->tb_next_offset = tb->tb_next_offset;
103 #ifdef USE_DIRECT_JUMP
104     s->tb_jmp_offset = tb->tb_jmp_offset;
105     s->tb_next = NULL;
106 #else
107     s->tb_jmp_offset = NULL;
108     s->tb_next = tb->tb_next;
109 #endif
110
111 #ifdef CONFIG_PROFILER
112     s->tb_count++;
113     s->interm_time += profile_getclock() - ti;
114     s->code_time -= profile_getclock();
115 #endif
116     gen_code_size = tcg_gen_code(s, gen_code_buf);
117     *gen_code_size_ptr = gen_code_size;
118 #ifdef CONFIG_PROFILER
119     s->code_time += profile_getclock();
120     s->code_in_len += tb->size;
121     s->code_out_len += gen_code_size;
122 #endif
123
124 #ifdef DEBUG_DISAS
125     if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
126         qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
127         log_disas(tb->tc_ptr, *gen_code_size_ptr);
128         qemu_log("\n");
129         qemu_log_flush();
130     }
131 #endif
132     return 0;
133 }
134
135 /* The cpu state corresponding to 'searched_pc' is restored.
136  */
137 int cpu_restore_state(TranslationBlock *tb,
138                       CPUState *env, unsigned long searched_pc,
139                       void *puc)
140 {
141     TCGContext *s = &tcg_ctx;
142     int j;
143     unsigned long tc_ptr;
144 #ifdef CONFIG_PROFILER
145     int64_t ti;
146 #endif
147
148 #ifdef CONFIG_PROFILER
149     ti = profile_getclock();
150 #endif
151     tcg_func_start(s);
152
153     gen_intermediate_code_pc(env, tb);
154
155     if (use_icount) {
156         /* Reset the cycle counter to the start of the block.  */
157         env->icount_decr.u16.low += tb->icount;
158         /* Clear the IO flag.  */
159         env->can_do_io = 0;
160     }
161
162     /* find opc index corresponding to search_pc */
163     tc_ptr = (unsigned long)tb->tc_ptr;
164     if (searched_pc < tc_ptr)
165         return -1;
166
167     s->tb_next_offset = tb->tb_next_offset;
168 #ifdef USE_DIRECT_JUMP
169     s->tb_jmp_offset = tb->tb_jmp_offset;
170     s->tb_next = NULL;
171 #else
172     s->tb_jmp_offset = NULL;
173     s->tb_next = tb->tb_next;
174 #endif
175     j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
176     if (j < 0)
177         return -1;
178     /* now find start of instruction before */
179     while (gen_opc_instr_start[j] == 0)
180         j--;
181     env->icount_decr.u16.low -= gen_opc_icount[j];
182
183     gen_pc_load(env, tb, searched_pc, j, puc);
184
185 #ifdef CONFIG_PROFILER
186     s->restore_time += profile_getclock() - ti;
187     s->restore_count++;
188 #endif
189     return 0;
190 }