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