Introduce -machine command option.
[qemu/wangdongxu.git] / translate-all.c
blob2ca190ca8fe8ab010200f7280dca0bbac20f5bb6
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 void cpu_gen_init(void)
46 tcg_context_init(&tcg_ctx);
47 tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
48 CPU_TEMP_BUF_NLONGS * sizeof(long));
51 /* return non zero if the very first instruction is invalid so that
52 the virtual CPU can trigger an exception.
54 '*gen_code_size_ptr' contains the size of the generated code (host
55 code).
57 int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
59 TCGContext *s = &tcg_ctx;
60 uint8_t *gen_code_buf;
61 int gen_code_size;
62 #ifdef CONFIG_PROFILER
63 int64_t ti;
64 #endif
66 #ifdef CONFIG_PROFILER
67 s->tb_count1++; /* includes aborted translations because of
68 exceptions */
69 ti = profile_getclock();
70 #endif
71 tcg_func_start(s);
73 gen_intermediate_code(env, tb);
75 /* generate machine code */
76 gen_code_buf = tb->tc_ptr;
77 tb->tb_next_offset[0] = 0xffff;
78 tb->tb_next_offset[1] = 0xffff;
79 s->tb_next_offset = tb->tb_next_offset;
80 #ifdef USE_DIRECT_JUMP
81 s->tb_jmp_offset = tb->tb_jmp_offset;
82 s->tb_next = NULL;
83 #else
84 s->tb_jmp_offset = NULL;
85 s->tb_next = tb->tb_next;
86 #endif
88 #ifdef CONFIG_PROFILER
89 s->tb_count++;
90 s->interm_time += profile_getclock() - ti;
91 s->code_time -= profile_getclock();
92 #endif
93 gen_code_size = tcg_gen_code(s, gen_code_buf);
94 *gen_code_size_ptr = gen_code_size;
95 #ifdef CONFIG_PROFILER
96 s->code_time += profile_getclock();
97 s->code_in_len += tb->size;
98 s->code_out_len += gen_code_size;
99 #endif
101 #ifdef DEBUG_DISAS
102 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
103 qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
104 log_disas(tb->tc_ptr, *gen_code_size_ptr);
105 qemu_log("\n");
106 qemu_log_flush();
108 #endif
109 return 0;
112 /* The cpu state corresponding to 'searched_pc' is restored.
114 int cpu_restore_state(TranslationBlock *tb,
115 CPUState *env, unsigned long searched_pc)
117 TCGContext *s = &tcg_ctx;
118 int j;
119 unsigned long tc_ptr;
120 #ifdef CONFIG_PROFILER
121 int64_t ti;
122 #endif
124 #ifdef CONFIG_PROFILER
125 ti = profile_getclock();
126 #endif
127 tcg_func_start(s);
129 gen_intermediate_code_pc(env, tb);
131 if (use_icount) {
132 /* Reset the cycle counter to the start of the block. */
133 env->icount_decr.u16.low += tb->icount;
134 /* Clear the IO flag. */
135 env->can_do_io = 0;
138 /* find opc index corresponding to search_pc */
139 tc_ptr = (unsigned long)tb->tc_ptr;
140 if (searched_pc < tc_ptr)
141 return -1;
143 s->tb_next_offset = tb->tb_next_offset;
144 #ifdef USE_DIRECT_JUMP
145 s->tb_jmp_offset = tb->tb_jmp_offset;
146 s->tb_next = NULL;
147 #else
148 s->tb_jmp_offset = NULL;
149 s->tb_next = tb->tb_next;
150 #endif
151 j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
152 if (j < 0)
153 return -1;
154 /* now find start of instruction before */
155 while (gen_opc_instr_start[j] == 0)
156 j--;
157 env->icount_decr.u16.low -= gen_opc_icount[j];
159 restore_state_to_opc(env, tb, j);
161 #ifdef CONFIG_PROFILER
162 s->restore_time += profile_getclock() - ti;
163 s->restore_count++;
164 #endif
165 return 0;