VirtualBox

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

Last change on this file since 33366 was 29520, checked in by vboxsync, 15 years ago

fix wrong license headers

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

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