VirtualBox

source: vbox/trunk/src/recompiler/tcg/tcg.h@ 21918

最後變更 在這個檔案從21918是 18083,由 vboxsync 提交於 16 年 前

eol

  • 屬性 svn:eol-style 設為 native
檔案大小: 14.0 KB
 
1/*
2 * Tiny Code Generator for QEMU
3 *
4 * Copyright (c) 2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24/*
25 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
26 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
27 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
28 * a choice of LGPL license versions is made available with the language indicating
29 * that LGPLv2 or any later version may be used, or where a choice of which version
30 * of the LGPL is applied is otherwise unspecified.
31 */
32#include "tcg-target.h"
33
34#if TCG_TARGET_REG_BITS == 32
35typedef int32_t tcg_target_long;
36typedef uint32_t tcg_target_ulong;
37#define TCG_PRIlx PRIx32
38#define TCG_PRIld PRId32
39#elif TCG_TARGET_REG_BITS == 64
40typedef int64_t tcg_target_long;
41typedef uint64_t tcg_target_ulong;
42#define TCG_PRIlx PRIx64
43#define TCG_PRIld PRId64
44#else
45#error unsupported
46#endif
47
48#if TCG_TARGET_NB_REGS <= 32
49typedef uint32_t TCGRegSet;
50#elif TCG_TARGET_NB_REGS <= 64
51typedef uint64_t TCGRegSet;
52#else
53#error unsupported
54#endif
55
56enum {
57#define DEF(s, n, copy_size) INDEX_op_ ## s,
58#include "tcg-opc.h"
59#undef DEF
60 NB_OPS,
61};
62
63#define tcg_regset_clear(d) (d) = 0
64#define tcg_regset_set(d, s) (d) = (s)
65#define tcg_regset_set32(d, reg, val32) (d) |= (val32) << (reg)
66#define tcg_regset_set_reg(d, r) (d) |= 1 << (r)
67#define tcg_regset_reset_reg(d, r) (d) &= ~(1 << (r))
68#define tcg_regset_test_reg(d, r) (((d) >> (r)) & 1)
69#define tcg_regset_or(d, a, b) (d) = (a) | (b)
70#define tcg_regset_and(d, a, b) (d) = (a) & (b)
71#define tcg_regset_andnot(d, a, b) (d) = (a) & ~(b)
72#define tcg_regset_not(d, a) (d) = ~(a)
73
74typedef struct TCGRelocation {
75 struct TCGRelocation *next;
76 int type;
77 uint8_t *ptr;
78 tcg_target_long addend;
79} TCGRelocation;
80
81typedef struct TCGLabel {
82 int has_value;
83 union {
84 tcg_target_ulong value;
85 TCGRelocation *first_reloc;
86 } u;
87} TCGLabel;
88
89typedef struct TCGPool {
90 struct TCGPool *next;
91 int size;
92#ifndef VBOX
93 uint8_t data[0] __attribute__ ((aligned));
94#else
95 ALIGNED_MEMBER_DEF(uint8_t, data[0]);
96#endif
97} TCGPool;
98
99#define TCG_POOL_CHUNK_SIZE 32768
100
101#define TCG_MAX_LABELS 512
102
103#define TCG_MAX_TEMPS 512
104
105/* when the size of the arguments of a called function is smaller than
106 this value, they are statically allocated in the TB stack frame */
107#define TCG_STATIC_CALL_ARGS_SIZE 128
108
109typedef int TCGType;
110
111#define TCG_TYPE_I32 0
112#define TCG_TYPE_I64 1
113#define TCG_TYPE_COUNT 2 /* number of different types */
114
115#if TCG_TARGET_REG_BITS == 32
116#define TCG_TYPE_PTR TCG_TYPE_I32
117#else
118#define TCG_TYPE_PTR TCG_TYPE_I64
119#endif
120
121typedef tcg_target_ulong TCGArg;
122
123/* Define a type and accessor macros for varables. Using a struct is
124 nice because it gives some level of type safely. Ideally the compiler
125 be able to see through all this. However in practice this is not true,
126 expecially on targets with braindamaged ABIs (e.g. i386).
127 We use plain int by default to avoid this runtime overhead.
128 Users of tcg_gen_* don't need to know about any of this, and should
129 treat TCGv as an opaque type. */
130
131//#define DEBUG_TCGV 1
132
133#ifdef DEBUG_TCGV
134
135typedef struct
136{
137 int n;
138} TCGv;
139
140#define MAKE_TCGV(i) __extension__ \
141 ({ TCGv make_tcgv_tmp = {i}; make_tcgv_tmp;})
142#define GET_TCGV(t) ((t).n)
143#if TCG_TARGET_REG_BITS == 32
144#define TCGV_HIGH(t) MAKE_TCGV(GET_TCGV(t) + 1)
145#endif
146
147#else /* !DEBUG_TCGV */
148
149typedef int TCGv;
150#define MAKE_TCGV(x) (x)
151#define GET_TCGV(t) (t)
152#if TCG_TARGET_REG_BITS == 32
153#define TCGV_HIGH(t) ((t) + 1)
154#endif
155
156#endif /* DEBUG_TCGV */
157
158/* Dummy definition to avoid compiler warnings. */
159#define TCGV_UNUSED(x) x = MAKE_TCGV(-1)
160
161/* call flags */
162#define TCG_CALL_TYPE_MASK 0x000f
163#define TCG_CALL_TYPE_STD 0x0000 /* standard C call */
164#define TCG_CALL_TYPE_REGPARM_1 0x0001 /* i386 style regparm call (1 reg) */
165#define TCG_CALL_TYPE_REGPARM_2 0x0002 /* i386 style regparm call (2 regs) */
166#define TCG_CALL_TYPE_REGPARM 0x0003 /* i386 style regparm call (3 regs) */
167/* A pure function only reads its arguments and globals variables and
168 cannot raise exceptions. Hence a call to a pure function can be
169 safely suppressed if the return value is not used. */
170#define TCG_CALL_PURE 0x0010
171
172/* used to align parameters */
173#define TCG_CALL_DUMMY_TCGV MAKE_TCGV(-1)
174#define TCG_CALL_DUMMY_ARG ((TCGArg)(-1))
175
176typedef enum {
177 TCG_COND_EQ,
178 TCG_COND_NE,
179 TCG_COND_LT,
180 TCG_COND_GE,
181 TCG_COND_LE,
182 TCG_COND_GT,
183 /* unsigned */
184 TCG_COND_LTU,
185 TCG_COND_GEU,
186 TCG_COND_LEU,
187 TCG_COND_GTU,
188} TCGCond;
189
190#define TEMP_VAL_DEAD 0
191#define TEMP_VAL_REG 1
192#define TEMP_VAL_MEM 2
193#define TEMP_VAL_CONST 3
194
195/* XXX: optimize memory layout */
196typedef struct TCGTemp {
197 TCGType base_type;
198 TCGType type;
199 int val_type;
200 int reg;
201 tcg_target_long val;
202 int mem_reg;
203 tcg_target_long mem_offset;
204 unsigned int fixed_reg:1;
205 unsigned int mem_coherent:1;
206 unsigned int mem_allocated:1;
207 unsigned int temp_local:1; /* If true, the temp is saved accross
208 basic blocks. Otherwise, it is not
209 preserved accross basic blocks. */
210 unsigned int temp_allocated:1; /* never used for code gen */
211 /* index of next free temp of same base type, -1 if end */
212 int next_free_temp;
213 const char *name;
214} TCGTemp;
215
216typedef struct TCGHelperInfo {
217 tcg_target_ulong func;
218 const char *name;
219} TCGHelperInfo;
220
221typedef struct TCGContext TCGContext;
222
223struct TCGContext {
224 uint8_t *pool_cur, *pool_end;
225 TCGPool *pool_first, *pool_current;
226 TCGLabel *labels;
227 int nb_labels;
228 TCGTemp *temps; /* globals first, temps after */
229 int nb_globals;
230 int nb_temps;
231 /* index of free temps, -1 if none */
232 int first_free_temp[TCG_TYPE_COUNT * 2];
233
234 /* goto_tb support */
235 uint8_t *code_buf;
236 unsigned long *tb_next;
237 uint16_t *tb_next_offset;
238 uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
239
240 /* liveness analysis */
241 uint16_t *op_dead_iargs; /* for each operation, each bit tells if the
242 corresponding input argument is dead */
243
244 /* tells in which temporary a given register is. It does not take
245 into account fixed registers */
246 int reg_to_temp[TCG_TARGET_NB_REGS];
247 TCGRegSet reserved_regs;
248 tcg_target_long current_frame_offset;
249 tcg_target_long frame_start;
250 tcg_target_long frame_end;
251 int frame_reg;
252
253 uint8_t *code_ptr;
254 TCGTemp static_temps[TCG_MAX_TEMPS];
255
256 TCGHelperInfo *helpers;
257 int nb_helpers;
258 int allocated_helpers;
259 int helpers_sorted;
260
261#ifdef CONFIG_PROFILER
262 /* profiling info */
263 int64_t tb_count1;
264 int64_t tb_count;
265 int64_t op_count; /* total insn count */
266 int op_count_max; /* max insn per TB */
267 int64_t temp_count;
268 int temp_count_max;
269 int64_t old_op_count;
270 int64_t del_op_count;
271 int64_t code_in_len;
272 int64_t code_out_len;
273 int64_t interm_time;
274 int64_t code_time;
275 int64_t la_time;
276 int64_t restore_count;
277 int64_t restore_time;
278#endif
279};
280
281extern TCGContext tcg_ctx;
282extern uint16_t *gen_opc_ptr;
283extern TCGArg *gen_opparam_ptr;
284extern uint16_t gen_opc_buf[];
285extern TCGArg gen_opparam_buf[];
286
287/* pool based memory allocation */
288
289void *tcg_malloc_internal(TCGContext *s, int size);
290void tcg_pool_reset(TCGContext *s);
291void tcg_pool_delete(TCGContext *s);
292
293#ifndef VBOX
294static inline void *tcg_malloc(int size)
295#else
296DECLINLINE(void *) tcg_malloc(int size)
297#endif
298{
299 TCGContext *s = &tcg_ctx;
300 uint8_t *ptr, *ptr_end;
301 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
302 ptr = s->pool_cur;
303 ptr_end = ptr + size;
304 if (unlikely(ptr_end > s->pool_end)) {
305 return tcg_malloc_internal(&tcg_ctx, size);
306 } else {
307 s->pool_cur = ptr_end;
308 return ptr;
309 }
310}
311
312void tcg_context_init(TCGContext *s);
313void tcg_func_start(TCGContext *s);
314
315int dyngen_code(TCGContext *s, uint8_t *gen_code_buf);
316int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
317
318void tcg_set_frame(TCGContext *s, int reg,
319 tcg_target_long start, tcg_target_long size);
320TCGv tcg_global_reg_new(TCGType type, int reg, const char *name);
321TCGv tcg_global_reg2_new_hack(TCGType type, int reg1, int reg2,
322 const char *name);
323TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
324 const char *name);
325TCGv tcg_temp_new_internal(TCGType type, int temp_local);
326#ifndef VBOX
327static inline TCGv tcg_temp_new(TCGType type)
328#else
329DECLINLINE(TCGv) tcg_temp_new(TCGType type)
330#endif
331{
332 return tcg_temp_new_internal(type, 0);
333}
334#ifndef VBOX
335static inline TCGv tcg_temp_local_new(TCGType type)
336#else
337DECLINLINE(TCGv) tcg_temp_local_new(TCGType type)
338#endif
339{
340 return tcg_temp_new_internal(type, 1);
341}
342void tcg_temp_free(TCGv arg);
343char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg);
344void tcg_dump_info(FILE *f,
345 int (*cpu_fprintf)(FILE *f, const char *fmt, ...));
346
347#define TCG_CT_ALIAS 0x80
348#define TCG_CT_IALIAS 0x40
349#define TCG_CT_REG 0x01
350#define TCG_CT_CONST 0x02 /* any constant of register size */
351
352typedef struct TCGArgConstraint {
353 uint16_t ct;
354 uint8_t alias_index;
355 union {
356 TCGRegSet regs;
357 } u;
358} TCGArgConstraint;
359
360#define TCG_MAX_OP_ARGS 16
361
362#define TCG_OPF_BB_END 0x01 /* instruction defines the end of a basic
363 block */
364#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers
365 and potentially update globals. */
366#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects : it
367 cannot be removed if its output
368 are not used */
369
370typedef struct TCGOpDef {
371 const char *name;
372 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
373 uint8_t flags;
374 uint16_t copy_size;
375 TCGArgConstraint *args_ct;
376 int *sorted_args;
377} TCGOpDef;
378
379typedef struct TCGTargetOpDef {
380 int op;
381 const char *args_ct_str[TCG_MAX_OP_ARGS];
382} TCGTargetOpDef;
383
384extern TCGOpDef tcg_op_defs[];
385
386void tcg_target_init(TCGContext *s);
387void tcg_target_qemu_prologue(TCGContext *s);
388
389#ifndef VBOX
390#define tcg_abort() \
391do {\
392 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
393 abort();\
394} while (0)
395#else
396#define VBOX_STR(x) #x
397#define VBOX_XSTR(x) VBOX_STR(x)
398#define tcg_abort() \
399do {\
400 remAbort(-1, "TCG fatal error: "__FILE__":"VBOX_XSTR(__LINE__)); \
401} while (0)
402extern void qemu_qsort(void* base, size_t nmemb, size_t size,
403 int(*compar)(const void*, const void*));
404#define tcg_exit(status) \
405do {\
406 remAbort(-1, "TCG exit: "__FILE__":"VBOX_XSTR(__LINE__));\
407} while (0)
408#endif
409
410void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
411
412void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
413 unsigned int nb_rets, const TCGv *rets,
414 unsigned int nb_params, const TCGv *args1);
415void tcg_gen_shifti_i64(TCGv ret, TCGv arg1,
416 int c, int right, int arith);
417
418/* only used for debugging purposes */
419void tcg_register_helper(void *func, const char *name);
420#define TCG_HELPER(func) tcg_register_helper(func, #func)
421const char *tcg_helper_get_name(TCGContext *s, void *func);
422void tcg_dump_ops(TCGContext *s, FILE *outfile);
423
424void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
425TCGv tcg_const_i32(int32_t val);
426TCGv tcg_const_i64(int64_t val);
427
428#if TCG_TARGET_REG_BITS == 32
429#define tcg_const_ptr tcg_const_i32
430#define tcg_add_ptr tcg_add_i32
431#define tcg_sub_ptr tcg_sub_i32
432#else
433#define tcg_const_ptr tcg_const_i64
434#define tcg_add_ptr tcg_add_i64
435#define tcg_sub_ptr tcg_sub_i64
436#endif
437
438void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
439 int label_index, long addend);
440const TCGArg *tcg_gen_code_op(TCGContext *s, int opc, const TCGArg *args1,
441 unsigned int dead_iargs);
442
443const TCGArg *dyngen_op(TCGContext *s, int opc, const TCGArg *opparam_ptr);
444
445/* tcg-runtime.c */
446int64_t tcg_helper_shl_i64(int64_t arg1, int64_t arg2);
447int64_t tcg_helper_shr_i64(int64_t arg1, int64_t arg2);
448int64_t tcg_helper_sar_i64(int64_t arg1, int64_t arg2);
449int64_t tcg_helper_div_i64(int64_t arg1, int64_t arg2);
450int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2);
451uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2);
452uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2);
453
454#ifndef VBOX
455extern uint8_t code_gen_prologue[];
456#else
457extern uint8_t* code_gen_prologue;
458#endif
459
460#if defined(__powerpc__) && !defined(__powerpc64__)
461#define tcg_qemu_tb_exec(tb_ptr) \
462 ((long REGPARM __attribute__ ((longcall)) (*)(void *))code_gen_prologue)(tb_ptr)
463#else
464
465#if defined(VBOX) && defined(GCC_WITH_BUGGY_REGPARM)
466#define tcg_qemu_tb_exec(tb_ptr, ret) \
467 __asm__ __volatile__("call *%%ecx" : "=a"(ret) : "a"(tb_ptr), "c" (&code_gen_prologue[0]) : "memory", "%edx", "cc")
468#else
469#define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)
470#endif
471
472#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette