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