VirtualBox

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

Last change on this file since 36143 was 36140, checked in by vboxsync, 14 years ago

rem: Re-synced to svn://svn.savannah.nongnu.org/qemu/trunk@5495 (repo UUID c046a42c-6fe2-441c-8c8c-71466251a162).

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