Merge remote branch 'kwolf/for-anthony' into HEAD
[qemu.git] / translate-all.c
blob91cbbc4929397c73e864e844ce25d1d4748c2806
1 /*
2 * Host code generation
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
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/>.
19 #include <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
25 #include "config.h"
27 #define NO_CPU_IO_DEFS
28 #include "cpu.h"
29 #include "exec-all.h"
30 #include "disas.h"
31 #include "tcg.h"
32 #include "qemu-timer.h"
34 /* code generation context */
35 TCGContext tcg_ctx;
37 uint16_t gen_opc_buf[OPC_BUF_SIZE];
38 TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
40 target_ulong gen_opc_pc[OPC_BUF_SIZE];
41 uint16_t gen_opc_icount[OPC_BUF_SIZE];
42 uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
44 /* XXX: suppress that */
45 unsigned long code_gen_max_block_size(void)
47 static unsigned long max;
49 if (max == 0) {
50 max = TCG_MAX_OP_SIZE;
51 #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
52 #include "tcg-opc.h"
53 #undef DEF
54 max *= OPC_MAX_SIZE;
57 return max;
60 void cpu_gen_init(void)
62 tcg_context_init(&tcg_ctx);
63 tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
64 CPU_TEMP_BUF_NLONGS * sizeof(long));
67 /* return non zero if the very first instruction is invalid so that
68 the virtual CPU can trigger an exception.
70 '*gen_code_size_ptr' contains the size of the generated code (host
71 code).
73 int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
75 TCGContext *s = &tcg_ctx;
76 uint8_t *gen_code_buf;
77 int gen_code_size;
78 #ifdef CONFIG_PROFILER
79 int64_t ti;
80 #endif
82 #ifdef CONFIG_PROFILER
83 s->tb_count1++; /* includes aborted translations because of
84 exceptions */
85 ti = profile_getclock();
86 #endif
87 tcg_func_start(s);
89 gen_intermediate_code(env, tb);
91 /* generate machine code */
92 gen_code_buf = tb->tc_ptr;
93 tb->tb_next_offset[0] = 0xffff;
94 tb->tb_next_offset[1] = 0xffff;
95 s->tb_next_offset = tb->tb_next_offset;
96 #ifdef USE_DIRECT_JUMP
97 s->tb_jmp_offset = tb->tb_jmp_offset;
98 s->tb_next = NULL;
99 #else
100 s->tb_jmp_offset = NULL;
101 s->tb_next = tb->tb_next;
102 #endif
104 #ifdef CONFIG_PROFILER
105 s->tb_count++;
106 s->interm_time += profile_getclock() - ti;
107 s->code_time -= profile_getclock();
108 #endif
109 gen_code_size = tcg_gen_code(s, gen_code_buf);
110 *gen_code_size_ptr = gen_code_size;
111 #ifdef CONFIG_PROFILER
112 s->code_time += profile_getclock();
113 s->code_in_len += tb->size;
114 s->code_out_len += gen_code_size;
115 #endif
117 #ifdef DEBUG_DISAS
118 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
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");
122 qemu_log_flush();
124 #endif
125 return 0;
128 /* The cpu state corresponding to 'searched_pc' is restored.
130 int cpu_restore_state(TranslationBlock *tb,
131 CPUState *env, unsigned long searched_pc,
132 void *puc)
134 TCGContext *s = &tcg_ctx;
135 int j;
136 unsigned long tc_ptr;
137 #ifdef CONFIG_PROFILER
138 int64_t ti;
139 #endif
141 #ifdef CONFIG_PROFILER
142 ti = profile_getclock();
143 #endif
144 tcg_func_start(s);
146 gen_intermediate_code_pc(env, tb);
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;
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;
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
168 j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
169 if (j < 0)
170 return -1;
171 /* now find start of instruction before */
172 while (gen_opc_instr_start[j] == 0)
173 j--;
174 env->icount_decr.u16.low -= gen_opc_icount[j];
176 gen_pc_load(env, tb, searched_pc, j, puc);
178 #ifdef CONFIG_PROFILER
179 s->restore_time += profile_getclock() - ti;
180 s->restore_count++;
181 #endif
182 return 0;