Merge remote-tracking branch 'qemu-kvm-tmp/memory/core' into staging
[qemu.git] / tcg / tcg.h
blobdc5e9c92d2be2ba49533122ae643b5488a200e4c
1 /*
2 * Tiny Code Generator for QEMU
4 * Copyright (c) 2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu-common.h"
25 #include "tcg-target.h"
26 #include "tcg-runtime.h"
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
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
50 /* Turn some undef macros into false macros. */
51 #if TCG_TARGET_REG_BITS == 32
52 #define TCG_TARGET_HAS_div_i64 0
53 #define TCG_TARGET_HAS_div2_i64 0
54 #define TCG_TARGET_HAS_rot_i64 0
55 #define TCG_TARGET_HAS_ext8s_i64 0
56 #define TCG_TARGET_HAS_ext16s_i64 0
57 #define TCG_TARGET_HAS_ext32s_i64 0
58 #define TCG_TARGET_HAS_ext8u_i64 0
59 #define TCG_TARGET_HAS_ext16u_i64 0
60 #define TCG_TARGET_HAS_ext32u_i64 0
61 #define TCG_TARGET_HAS_bswap16_i64 0
62 #define TCG_TARGET_HAS_bswap32_i64 0
63 #define TCG_TARGET_HAS_bswap64_i64 0
64 #define TCG_TARGET_HAS_neg_i64 0
65 #define TCG_TARGET_HAS_not_i64 0
66 #define TCG_TARGET_HAS_andc_i64 0
67 #define TCG_TARGET_HAS_orc_i64 0
68 #define TCG_TARGET_HAS_eqv_i64 0
69 #define TCG_TARGET_HAS_nand_i64 0
70 #define TCG_TARGET_HAS_nor_i64 0
71 #define TCG_TARGET_HAS_deposit_i64 0
72 #endif
74 /* Only one of DIV or DIV2 should be defined. */
75 #if defined(TCG_TARGET_HAS_div_i32)
76 #define TCG_TARGET_HAS_div2_i32 0
77 #elif defined(TCG_TARGET_HAS_div2_i32)
78 #define TCG_TARGET_HAS_div_i32 0
79 #endif
80 #if defined(TCG_TARGET_HAS_div_i64)
81 #define TCG_TARGET_HAS_div2_i64 0
82 #elif defined(TCG_TARGET_HAS_div2_i64)
83 #define TCG_TARGET_HAS_div_i64 0
84 #endif
86 typedef enum TCGOpcode {
87 #define DEF(name, oargs, iargs, cargs, flags) INDEX_op_ ## name,
88 #include "tcg-opc.h"
89 #undef DEF
90 NB_OPS,
91 } TCGOpcode;
93 #define tcg_regset_clear(d) (d) = 0
94 #define tcg_regset_set(d, s) (d) = (s)
95 #define tcg_regset_set32(d, reg, val32) (d) |= (val32) << (reg)
96 #define tcg_regset_set_reg(d, r) (d) |= 1L << (r)
97 #define tcg_regset_reset_reg(d, r) (d) &= ~(1L << (r))
98 #define tcg_regset_test_reg(d, r) (((d) >> (r)) & 1)
99 #define tcg_regset_or(d, a, b) (d) = (a) | (b)
100 #define tcg_regset_and(d, a, b) (d) = (a) & (b)
101 #define tcg_regset_andnot(d, a, b) (d) = (a) & ~(b)
102 #define tcg_regset_not(d, a) (d) = ~(a)
104 typedef struct TCGRelocation {
105 struct TCGRelocation *next;
106 int type;
107 uint8_t *ptr;
108 tcg_target_long addend;
109 } TCGRelocation;
111 typedef struct TCGLabel {
112 int has_value;
113 union {
114 tcg_target_ulong value;
115 TCGRelocation *first_reloc;
116 } u;
117 } TCGLabel;
119 typedef struct TCGPool {
120 struct TCGPool *next;
121 int size;
122 uint8_t data[0] __attribute__ ((aligned));
123 } TCGPool;
125 #define TCG_POOL_CHUNK_SIZE 32768
127 #define TCG_MAX_LABELS 512
129 #define TCG_MAX_TEMPS 512
131 /* when the size of the arguments of a called function is smaller than
132 this value, they are statically allocated in the TB stack frame */
133 #define TCG_STATIC_CALL_ARGS_SIZE 128
135 typedef enum TCGType {
136 TCG_TYPE_I32,
137 TCG_TYPE_I64,
138 TCG_TYPE_COUNT, /* number of different types */
140 /* An alias for the size of the host register. */
141 #if TCG_TARGET_REG_BITS == 32
142 TCG_TYPE_REG = TCG_TYPE_I32,
143 #else
144 TCG_TYPE_REG = TCG_TYPE_I64,
145 #endif
147 /* An alias for the size of the native pointer. We don't currently
148 support any hosts with 64-bit registers and 32-bit pointers. */
149 TCG_TYPE_PTR = TCG_TYPE_REG,
151 /* An alias for the size of the target "long", aka register. */
152 #if TARGET_LONG_BITS == 64
153 TCG_TYPE_TL = TCG_TYPE_I64,
154 #else
155 TCG_TYPE_TL = TCG_TYPE_I32,
156 #endif
157 } TCGType;
159 typedef tcg_target_ulong TCGArg;
161 /* Define a type and accessor macros for varables. Using a struct is
162 nice because it gives some level of type safely. Ideally the compiler
163 be able to see through all this. However in practice this is not true,
164 expecially on targets with braindamaged ABIs (e.g. i386).
165 We use plain int by default to avoid this runtime overhead.
166 Users of tcg_gen_* don't need to know about any of this, and should
167 treat TCGv as an opaque type.
168 In addition we do typechecking for different types of variables. TCGv_i32
169 and TCGv_i64 are 32/64-bit variables respectively. TCGv and TCGv_ptr
170 are aliases for target_ulong and host pointer sized values respectively.
173 #ifdef CONFIG_DEBUG_TCG
174 #define DEBUG_TCGV 1
175 #endif
177 #ifdef DEBUG_TCGV
179 typedef struct
181 int i32;
182 } TCGv_i32;
184 typedef struct
186 int i64;
187 } TCGv_i64;
189 typedef struct {
190 int iptr;
191 } TCGv_ptr;
193 #define MAKE_TCGV_I32(i) __extension__ \
194 ({ TCGv_i32 make_tcgv_tmp = {i}; make_tcgv_tmp;})
195 #define MAKE_TCGV_I64(i) __extension__ \
196 ({ TCGv_i64 make_tcgv_tmp = {i}; make_tcgv_tmp;})
197 #define MAKE_TCGV_PTR(i) __extension__ \
198 ({ TCGv_ptr make_tcgv_tmp = {i}; make_tcgv_tmp; })
199 #define GET_TCGV_I32(t) ((t).i32)
200 #define GET_TCGV_I64(t) ((t).i64)
201 #define GET_TCGV_PTR(t) ((t).iptr)
202 #if TCG_TARGET_REG_BITS == 32
203 #define TCGV_LOW(t) MAKE_TCGV_I32(GET_TCGV_I64(t))
204 #define TCGV_HIGH(t) MAKE_TCGV_I32(GET_TCGV_I64(t) + 1)
205 #endif
207 #else /* !DEBUG_TCGV */
209 typedef int TCGv_i32;
210 typedef int TCGv_i64;
211 #if TCG_TARGET_REG_BITS == 32
212 #define TCGv_ptr TCGv_i32
213 #else
214 #define TCGv_ptr TCGv_i64
215 #endif
216 #define MAKE_TCGV_I32(x) (x)
217 #define MAKE_TCGV_I64(x) (x)
218 #define MAKE_TCGV_PTR(x) (x)
219 #define GET_TCGV_I32(t) (t)
220 #define GET_TCGV_I64(t) (t)
221 #define GET_TCGV_PTR(t) (t)
223 #if TCG_TARGET_REG_BITS == 32
224 #define TCGV_LOW(t) (t)
225 #define TCGV_HIGH(t) ((t) + 1)
226 #endif
228 #endif /* DEBUG_TCGV */
230 #define TCGV_EQUAL_I32(a, b) (GET_TCGV_I32(a) == GET_TCGV_I32(b))
231 #define TCGV_EQUAL_I64(a, b) (GET_TCGV_I64(a) == GET_TCGV_I64(b))
233 /* Dummy definition to avoid compiler warnings. */
234 #define TCGV_UNUSED_I32(x) x = MAKE_TCGV_I32(-1)
235 #define TCGV_UNUSED_I64(x) x = MAKE_TCGV_I64(-1)
237 /* call flags */
238 #define TCG_CALL_TYPE_MASK 0x000f
239 #define TCG_CALL_TYPE_STD 0x0000 /* standard C call */
240 #define TCG_CALL_TYPE_REGPARM_1 0x0001 /* i386 style regparm call (1 reg) */
241 #define TCG_CALL_TYPE_REGPARM_2 0x0002 /* i386 style regparm call (2 regs) */
242 #define TCG_CALL_TYPE_REGPARM 0x0003 /* i386 style regparm call (3 regs) */
243 /* A pure function only reads its arguments and TCG global variables
244 and cannot raise exceptions. Hence a call to a pure function can be
245 safely suppressed if the return value is not used. */
246 #define TCG_CALL_PURE 0x0010
247 /* A const function only reads its arguments and does not use TCG
248 global variables. Hence a call to such a function does not
249 save TCG global variables back to their canonical location. */
250 #define TCG_CALL_CONST 0x0020
252 /* used to align parameters */
253 #define TCG_CALL_DUMMY_TCGV MAKE_TCGV_I32(-1)
254 #define TCG_CALL_DUMMY_ARG ((TCGArg)(-1))
256 typedef enum {
257 TCG_COND_EQ,
258 TCG_COND_NE,
259 TCG_COND_LT,
260 TCG_COND_GE,
261 TCG_COND_LE,
262 TCG_COND_GT,
263 /* unsigned */
264 TCG_COND_LTU,
265 TCG_COND_GEU,
266 TCG_COND_LEU,
267 TCG_COND_GTU,
268 } TCGCond;
270 /* Invert the sense of the comparison. */
271 static inline TCGCond tcg_invert_cond(TCGCond c)
273 return (TCGCond)(c ^ 1);
276 /* Swap the operands in a comparison. */
277 static inline TCGCond tcg_swap_cond(TCGCond c)
279 int mask = (c < TCG_COND_LT ? 0 : c < TCG_COND_LTU ? 7 : 15);
280 return (TCGCond)(c ^ mask);
283 static inline TCGCond tcg_unsigned_cond(TCGCond c)
285 return (c >= TCG_COND_LT && c <= TCG_COND_GT ? c + 4 : c);
288 #define TEMP_VAL_DEAD 0
289 #define TEMP_VAL_REG 1
290 #define TEMP_VAL_MEM 2
291 #define TEMP_VAL_CONST 3
293 /* XXX: optimize memory layout */
294 typedef struct TCGTemp {
295 TCGType base_type;
296 TCGType type;
297 int val_type;
298 int reg;
299 tcg_target_long val;
300 int mem_reg;
301 tcg_target_long mem_offset;
302 unsigned int fixed_reg:1;
303 unsigned int mem_coherent:1;
304 unsigned int mem_allocated:1;
305 unsigned int temp_local:1; /* If true, the temp is saved across
306 basic blocks. Otherwise, it is not
307 preserved across basic blocks. */
308 unsigned int temp_allocated:1; /* never used for code gen */
309 /* index of next free temp of same base type, -1 if end */
310 int next_free_temp;
311 const char *name;
312 } TCGTemp;
314 typedef struct TCGHelperInfo {
315 tcg_target_ulong func;
316 const char *name;
317 } TCGHelperInfo;
319 typedef struct TCGContext TCGContext;
321 struct TCGContext {
322 uint8_t *pool_cur, *pool_end;
323 TCGPool *pool_first, *pool_current;
324 TCGLabel *labels;
325 int nb_labels;
326 TCGTemp *temps; /* globals first, temps after */
327 int nb_globals;
328 int nb_temps;
329 /* index of free temps, -1 if none */
330 int first_free_temp[TCG_TYPE_COUNT * 2];
332 /* goto_tb support */
333 uint8_t *code_buf;
334 unsigned long *tb_next;
335 uint16_t *tb_next_offset;
336 uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
338 /* liveness analysis */
339 uint16_t *op_dead_args; /* for each operation, each bit tells if the
340 corresponding argument is dead */
342 /* tells in which temporary a given register is. It does not take
343 into account fixed registers */
344 int reg_to_temp[TCG_TARGET_NB_REGS];
345 TCGRegSet reserved_regs;
346 tcg_target_long current_frame_offset;
347 tcg_target_long frame_start;
348 tcg_target_long frame_end;
349 int frame_reg;
351 uint8_t *code_ptr;
352 TCGTemp static_temps[TCG_MAX_TEMPS];
354 TCGHelperInfo *helpers;
355 int nb_helpers;
356 int allocated_helpers;
357 int helpers_sorted;
359 #ifdef CONFIG_PROFILER
360 /* profiling info */
361 int64_t tb_count1;
362 int64_t tb_count;
363 int64_t op_count; /* total insn count */
364 int op_count_max; /* max insn per TB */
365 int64_t temp_count;
366 int temp_count_max;
367 int64_t del_op_count;
368 int64_t code_in_len;
369 int64_t code_out_len;
370 int64_t interm_time;
371 int64_t code_time;
372 int64_t la_time;
373 int64_t restore_count;
374 int64_t restore_time;
375 #endif
377 #ifdef CONFIG_DEBUG_TCG
378 int temps_in_use;
379 #endif
382 extern TCGContext tcg_ctx;
383 extern uint16_t *gen_opc_ptr;
384 extern TCGArg *gen_opparam_ptr;
385 extern uint16_t gen_opc_buf[];
386 extern TCGArg gen_opparam_buf[];
388 /* pool based memory allocation */
390 void *tcg_malloc_internal(TCGContext *s, int size);
391 void tcg_pool_reset(TCGContext *s);
392 void tcg_pool_delete(TCGContext *s);
394 static inline void *tcg_malloc(int size)
396 TCGContext *s = &tcg_ctx;
397 uint8_t *ptr, *ptr_end;
398 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
399 ptr = s->pool_cur;
400 ptr_end = ptr + size;
401 if (unlikely(ptr_end > s->pool_end)) {
402 return tcg_malloc_internal(&tcg_ctx, size);
403 } else {
404 s->pool_cur = ptr_end;
405 return ptr;
409 void tcg_context_init(TCGContext *s);
410 void tcg_prologue_init(TCGContext *s);
411 void tcg_func_start(TCGContext *s);
413 int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf);
414 int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
416 void tcg_set_frame(TCGContext *s, int reg,
417 tcg_target_long start, tcg_target_long size);
419 TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name);
420 TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
421 const char *name);
422 TCGv_i32 tcg_temp_new_internal_i32(int temp_local);
423 static inline TCGv_i32 tcg_temp_new_i32(void)
425 return tcg_temp_new_internal_i32(0);
427 static inline TCGv_i32 tcg_temp_local_new_i32(void)
429 return tcg_temp_new_internal_i32(1);
431 void tcg_temp_free_i32(TCGv_i32 arg);
432 char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg);
434 TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name);
435 TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
436 const char *name);
437 TCGv_i64 tcg_temp_new_internal_i64(int temp_local);
438 static inline TCGv_i64 tcg_temp_new_i64(void)
440 return tcg_temp_new_internal_i64(0);
442 static inline TCGv_i64 tcg_temp_local_new_i64(void)
444 return tcg_temp_new_internal_i64(1);
446 void tcg_temp_free_i64(TCGv_i64 arg);
447 char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg);
449 static inline bool tcg_arg_is_local(TCGContext *s, TCGArg arg)
451 return s->temps[arg].temp_local;
454 #if defined(CONFIG_DEBUG_TCG)
455 /* If you call tcg_clear_temp_count() at the start of a section of
456 * code which is not supposed to leak any TCG temporaries, then
457 * calling tcg_check_temp_count() at the end of the section will
458 * return 1 if the section did in fact leak a temporary.
460 void tcg_clear_temp_count(void);
461 int tcg_check_temp_count(void);
462 #else
463 #define tcg_clear_temp_count() do { } while (0)
464 #define tcg_check_temp_count() 0
465 #endif
467 void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf);
469 #define TCG_CT_ALIAS 0x80
470 #define TCG_CT_IALIAS 0x40
471 #define TCG_CT_REG 0x01
472 #define TCG_CT_CONST 0x02 /* any constant of register size */
474 typedef struct TCGArgConstraint {
475 uint16_t ct;
476 uint8_t alias_index;
477 union {
478 TCGRegSet regs;
479 } u;
480 } TCGArgConstraint;
482 #define TCG_MAX_OP_ARGS 16
484 /* Bits for TCGOpDef->flags, 8 bits available. */
485 enum {
486 /* Instruction defines the end of a basic block. */
487 TCG_OPF_BB_END = 0x01,
488 /* Instruction clobbers call registers and potentially update globals. */
489 TCG_OPF_CALL_CLOBBER = 0x02,
490 /* Instruction has side effects: it cannot be removed
491 if its outputs are not used. */
492 TCG_OPF_SIDE_EFFECTS = 0x04,
493 /* Instruction operands are 64-bits (otherwise 32-bits). */
494 TCG_OPF_64BIT = 0x08,
495 /* Instruction is optional and not implemented by the host. */
496 TCG_OPF_NOT_PRESENT = 0x10,
499 typedef struct TCGOpDef {
500 const char *name;
501 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
502 uint8_t flags;
503 TCGArgConstraint *args_ct;
504 int *sorted_args;
505 #if defined(CONFIG_DEBUG_TCG)
506 int used;
507 #endif
508 } TCGOpDef;
510 extern TCGOpDef tcg_op_defs[];
512 typedef struct TCGTargetOpDef {
513 TCGOpcode op;
514 const char *args_ct_str[TCG_MAX_OP_ARGS];
515 } TCGTargetOpDef;
517 #define tcg_abort() \
518 do {\
519 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
520 abort();\
521 } while (0)
523 void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
525 #if TCG_TARGET_REG_BITS == 32
526 #define TCGV_NAT_TO_PTR(n) MAKE_TCGV_PTR(GET_TCGV_I32(n))
527 #define TCGV_PTR_TO_NAT(n) MAKE_TCGV_I32(GET_TCGV_PTR(n))
529 #define tcg_const_ptr(V) TCGV_NAT_TO_PTR(tcg_const_i32(V))
530 #define tcg_global_reg_new_ptr(R, N) \
531 TCGV_NAT_TO_PTR(tcg_global_reg_new_i32((R), (N)))
532 #define tcg_global_mem_new_ptr(R, O, N) \
533 TCGV_NAT_TO_PTR(tcg_global_mem_new_i32((R), (O), (N)))
534 #define tcg_temp_new_ptr() TCGV_NAT_TO_PTR(tcg_temp_new_i32())
535 #define tcg_temp_free_ptr(T) tcg_temp_free_i32(TCGV_PTR_TO_NAT(T))
536 #else
537 #define TCGV_NAT_TO_PTR(n) MAKE_TCGV_PTR(GET_TCGV_I64(n))
538 #define TCGV_PTR_TO_NAT(n) MAKE_TCGV_I64(GET_TCGV_PTR(n))
540 #define tcg_const_ptr(V) TCGV_NAT_TO_PTR(tcg_const_i64(V))
541 #define tcg_global_reg_new_ptr(R, N) \
542 TCGV_NAT_TO_PTR(tcg_global_reg_new_i64((R), (N)))
543 #define tcg_global_mem_new_ptr(R, O, N) \
544 TCGV_NAT_TO_PTR(tcg_global_mem_new_i64((R), (O), (N)))
545 #define tcg_temp_new_ptr() TCGV_NAT_TO_PTR(tcg_temp_new_i64())
546 #define tcg_temp_free_ptr(T) tcg_temp_free_i64(TCGV_PTR_TO_NAT(T))
547 #endif
549 void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
550 int sizemask, TCGArg ret, int nargs, TCGArg *args);
552 void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
553 int c, int right, int arith);
555 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr, TCGArg *args,
556 TCGOpDef *tcg_op_def);
558 /* only used for debugging purposes */
559 void tcg_register_helper(void *func, const char *name);
560 const char *tcg_helper_get_name(TCGContext *s, void *func);
561 void tcg_dump_ops(TCGContext *s, FILE *outfile);
563 void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
564 TCGv_i32 tcg_const_i32(int32_t val);
565 TCGv_i64 tcg_const_i64(int64_t val);
566 TCGv_i32 tcg_const_local_i32(int32_t val);
567 TCGv_i64 tcg_const_local_i64(int64_t val);
569 extern uint8_t code_gen_prologue[];
570 #if defined(_ARCH_PPC) && !defined(_ARCH_PPC64)
571 #define tcg_qemu_tb_exec(env, tb_ptr) \
572 ((long REGPARM __attribute__ ((longcall)) (*)(void *, void *))code_gen_prologue)(env, tb_ptr)
573 #else
574 #define tcg_qemu_tb_exec(env, tb_ptr) \
575 ((long REGPARM (*)(void *, void *))code_gen_prologue)(env, tb_ptr)
576 #endif