Merge branch 'master' of git://repo.or.cz/qemu
[qemu/hppa.git] / translate-all.c
blob631f972ba62c5b6482196c5b6d52c678f0452aaf
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
26 #include "config.h"
28 #define NO_CPU_IO_DEFS
29 #include "cpu.h"
30 #include "exec-all.h"
31 #include "disas.h"
32 #include "tcg.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 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_HPPA)
48 target_ulong gen_opc_npc[OPC_BUF_SIZE];
49 #elif defined(TARGET_MIPS) || defined(TARGET_SH4)
50 uint32_t gen_opc_hflags[OPC_BUF_SIZE];
51 #endif
53 int code_copy_enabled = 1;
55 #ifdef CONFIG_PROFILER
56 int64_t dyngen_tb_count1;
57 int64_t dyngen_tb_count;
58 int64_t dyngen_op_count;
59 int64_t dyngen_old_op_count;
60 int64_t dyngen_tcg_del_op_count;
61 int dyngen_op_count_max;
62 int64_t dyngen_code_in_len;
63 int64_t dyngen_code_out_len;
64 int64_t dyngen_interm_time;
65 int64_t dyngen_code_time;
66 int64_t dyngen_restore_count;
67 int64_t dyngen_restore_time;
68 #endif
70 /* XXX: suppress that */
71 unsigned long code_gen_max_block_size(void)
73 static unsigned long max;
75 if (max == 0) {
76 max = TCG_MAX_OP_SIZE;
77 #define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
78 #include "tcg-opc.h"
79 #undef DEF
80 max *= OPC_MAX_SIZE;
83 return max;
86 void cpu_gen_init(void)
88 tcg_context_init(&tcg_ctx);
89 tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
90 CPU_TEMP_BUF_NLONGS * sizeof(long));
93 /* return non zero if the very first instruction is invalid so that
94 the virtual CPU can trigger an exception.
96 '*gen_code_size_ptr' contains the size of the generated code (host
97 code).
99 int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
101 TCGContext *s = &tcg_ctx;
102 uint8_t *gen_code_buf;
103 int gen_code_size;
104 #ifdef CONFIG_PROFILER
105 int64_t ti;
106 #endif
108 #ifdef CONFIG_PROFILER
109 dyngen_tb_count1++; /* includes aborted translations because of
110 exceptions */
111 ti = profile_getclock();
112 #endif
113 tcg_func_start(s);
115 if (gen_intermediate_code(env, tb) < 0)
116 return -1;
118 /* generate machine code */
119 gen_code_buf = tb->tc_ptr;
120 tb->tb_next_offset[0] = 0xffff;
121 tb->tb_next_offset[1] = 0xffff;
122 s->tb_next_offset = tb->tb_next_offset;
123 #ifdef USE_DIRECT_JUMP
124 s->tb_jmp_offset = tb->tb_jmp_offset;
125 s->tb_next = NULL;
126 /* the following two entries are optional (only used for string ops) */
127 /* XXX: not used ? */
128 tb->tb_jmp_offset[2] = 0xffff;
129 tb->tb_jmp_offset[3] = 0xffff;
130 #else
131 s->tb_jmp_offset = NULL;
132 s->tb_next = tb->tb_next;
133 #endif
135 #ifdef CONFIG_PROFILER
136 dyngen_tb_count++;
137 dyngen_interm_time += profile_getclock() - ti;
138 dyngen_code_time -= profile_getclock();
139 #endif
140 gen_code_size = dyngen_code(s, gen_code_buf);
141 *gen_code_size_ptr = gen_code_size;
142 #ifdef CONFIG_PROFILER
143 dyngen_code_time += profile_getclock();
144 dyngen_code_in_len += tb->size;
145 dyngen_code_out_len += gen_code_size;
146 #endif
148 #ifdef DEBUG_DISAS
149 if (loglevel & CPU_LOG_TB_OUT_ASM) {
150 fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
151 disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
152 fprintf(logfile, "\n");
153 fflush(logfile);
155 #endif
156 return 0;
159 /* The cpu state corresponding to 'searched_pc' is restored.
161 int cpu_restore_state(TranslationBlock *tb,
162 CPUState *env, unsigned long searched_pc,
163 void *puc)
165 TCGContext *s = &tcg_ctx;
166 int j;
167 unsigned long tc_ptr;
168 #ifdef CONFIG_PROFILER
169 int64_t ti;
170 #endif
172 #ifdef CONFIG_PROFILER
173 ti = profile_getclock();
174 #endif
175 tcg_func_start(s);
177 if (gen_intermediate_code_pc(env, tb) < 0)
178 return -1;
180 /* find opc index corresponding to search_pc */
181 tc_ptr = (unsigned long)tb->tc_ptr;
182 if (searched_pc < tc_ptr)
183 return -1;
185 s->tb_next_offset = tb->tb_next_offset;
186 #ifdef USE_DIRECT_JUMP
187 s->tb_jmp_offset = tb->tb_jmp_offset;
188 s->tb_next = NULL;
189 #else
190 s->tb_jmp_offset = NULL;
191 s->tb_next = tb->tb_next;
192 #endif
193 j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
194 if (j < 0)
195 return -1;
196 /* now find start of instruction before */
197 while (gen_opc_instr_start[j] == 0)
198 j--;
200 gen_pc_load(env, tb, searched_pc, j, puc);
202 #ifdef CONFIG_PROFILER
203 dyngen_restore_time += profile_getclock() - ti;
204 dyngen_restore_count++;
205 #endif
206 return 0;