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
24 #include "qemu-common.h"
26 /* Target word size (must be identical to pointer size). */
27 #if UINTPTR_MAX == UINT32_MAX
28 # define TCG_TARGET_REG_BITS 32
29 #elif UINTPTR_MAX == UINT64_MAX
30 # define TCG_TARGET_REG_BITS 64
32 # error Unknown pointer size for tcg target
35 #if TCG_TARGET_REG_BITS == 32
36 typedef int32_t tcg_target_long;
37 typedef uint32_t tcg_target_ulong;
38 #define TCG_PRIlx PRIx32
39 #define TCG_PRIld PRId32
40 #elif TCG_TARGET_REG_BITS == 64
41 typedef int64_t tcg_target_long;
42 typedef uint64_t tcg_target_ulong;
43 #define TCG_PRIlx PRIx64
44 #define TCG_PRIld PRId64
49 #include "tcg-target.h"
50 #include "tcg-runtime.h"
52 #if TCG_TARGET_NB_REGS <= 32
53 typedef uint32_t TCGRegSet;
54 #elif TCG_TARGET_NB_REGS <= 64
55 typedef uint64_t TCGRegSet;
60 /* Turn some undef macros into false macros. */
61 #if TCG_TARGET_REG_BITS == 32
62 #define TCG_TARGET_HAS_div_i64 0
63 #define TCG_TARGET_HAS_div2_i64 0
64 #define TCG_TARGET_HAS_rot_i64 0
65 #define TCG_TARGET_HAS_ext8s_i64 0
66 #define TCG_TARGET_HAS_ext16s_i64 0
67 #define TCG_TARGET_HAS_ext32s_i64 0
68 #define TCG_TARGET_HAS_ext8u_i64 0
69 #define TCG_TARGET_HAS_ext16u_i64 0
70 #define TCG_TARGET_HAS_ext32u_i64 0
71 #define TCG_TARGET_HAS_bswap16_i64 0
72 #define TCG_TARGET_HAS_bswap32_i64 0
73 #define TCG_TARGET_HAS_bswap64_i64 0
74 #define TCG_TARGET_HAS_neg_i64 0
75 #define TCG_TARGET_HAS_not_i64 0
76 #define TCG_TARGET_HAS_andc_i64 0
77 #define TCG_TARGET_HAS_orc_i64 0
78 #define TCG_TARGET_HAS_eqv_i64 0
79 #define TCG_TARGET_HAS_nand_i64 0
80 #define TCG_TARGET_HAS_nor_i64 0
81 #define TCG_TARGET_HAS_deposit_i64 0
82 #define TCG_TARGET_HAS_movcond_i64 0
85 #ifndef TCG_TARGET_deposit_i32_valid
86 #define TCG_TARGET_deposit_i32_valid(ofs, len) 1
88 #ifndef TCG_TARGET_deposit_i64_valid
89 #define TCG_TARGET_deposit_i64_valid(ofs, len) 1
92 /* Only one of DIV or DIV2 should be defined. */
93 #if defined(TCG_TARGET_HAS_div_i32)
94 #define TCG_TARGET_HAS_div2_i32 0
95 #elif defined(TCG_TARGET_HAS_div2_i32)
96 #define TCG_TARGET_HAS_div_i32 0
98 #if defined(TCG_TARGET_HAS_div_i64)
99 #define TCG_TARGET_HAS_div2_i64 0
100 #elif defined(TCG_TARGET_HAS_div2_i64)
101 #define TCG_TARGET_HAS_div_i64 0
104 typedef enum TCGOpcode {
105 #define DEF(name, oargs, iargs, cargs, flags) INDEX_op_ ## name,
111 #define tcg_regset_clear(d) (d) = 0
112 #define tcg_regset_set(d, s) (d) = (s)
113 #define tcg_regset_set32(d, reg, val32) (d) |= (val32) << (reg)
114 #define tcg_regset_set_reg(d, r) (d) |= 1L << (r)
115 #define tcg_regset_reset_reg(d, r) (d) &= ~(1L << (r))
116 #define tcg_regset_test_reg(d, r) (((d) >> (r)) & 1)
117 #define tcg_regset_or(d, a, b) (d) = (a) | (b)
118 #define tcg_regset_and(d, a, b) (d) = (a) & (b)
119 #define tcg_regset_andnot(d, a, b) (d) = (a) & ~(b)
120 #define tcg_regset_not(d, a) (d) = ~(a)
122 typedef struct TCGRelocation {
123 struct TCGRelocation *next;
126 tcg_target_long addend;
129 typedef struct TCGLabel {
132 tcg_target_ulong value;
133 TCGRelocation *first_reloc;
137 typedef struct TCGPool {
138 struct TCGPool *next;
140 uint8_t data[0] __attribute__ ((aligned));
143 #define TCG_POOL_CHUNK_SIZE 32768
145 #define TCG_MAX_LABELS 512
147 #define TCG_MAX_TEMPS 512
149 /* when the size of the arguments of a called function is smaller than
150 this value, they are statically allocated in the TB stack frame */
151 #define TCG_STATIC_CALL_ARGS_SIZE 128
153 typedef enum TCGType {
156 TCG_TYPE_COUNT, /* number of different types */
158 /* An alias for the size of the host register. */
159 #if TCG_TARGET_REG_BITS == 32
160 TCG_TYPE_REG = TCG_TYPE_I32,
162 TCG_TYPE_REG = TCG_TYPE_I64,
165 /* An alias for the size of the native pointer. We don't currently
166 support any hosts with 64-bit registers and 32-bit pointers. */
167 TCG_TYPE_PTR = TCG_TYPE_REG,
169 /* An alias for the size of the target "long", aka register. */
170 #if TARGET_LONG_BITS == 64
171 TCG_TYPE_TL = TCG_TYPE_I64,
173 TCG_TYPE_TL = TCG_TYPE_I32,
177 typedef tcg_target_ulong TCGArg;
179 /* Define a type and accessor macros for variables. Using a struct is
180 nice because it gives some level of type safely. Ideally the compiler
181 be able to see through all this. However in practice this is not true,
182 especially on targets with braindamaged ABIs (e.g. i386).
183 We use plain int by default to avoid this runtime overhead.
184 Users of tcg_gen_* don't need to know about any of this, and should
185 treat TCGv as an opaque type.
186 In addition we do typechecking for different types of variables. TCGv_i32
187 and TCGv_i64 are 32/64-bit variables respectively. TCGv and TCGv_ptr
188 are aliases for target_ulong and host pointer sized values respectively.
191 #ifdef CONFIG_DEBUG_TCG
211 #define MAKE_TCGV_I32(i) __extension__ \
212 ({ TCGv_i32 make_tcgv_tmp = {i}; make_tcgv_tmp;})
213 #define MAKE_TCGV_I64(i) __extension__ \
214 ({ TCGv_i64 make_tcgv_tmp = {i}; make_tcgv_tmp;})
215 #define MAKE_TCGV_PTR(i) __extension__ \
216 ({ TCGv_ptr make_tcgv_tmp = {i}; make_tcgv_tmp; })
217 #define GET_TCGV_I32(t) ((t).i32)
218 #define GET_TCGV_I64(t) ((t).i64)
219 #define GET_TCGV_PTR(t) ((t).iptr)
220 #if TCG_TARGET_REG_BITS == 32
221 #define TCGV_LOW(t) MAKE_TCGV_I32(GET_TCGV_I64(t))
222 #define TCGV_HIGH(t) MAKE_TCGV_I32(GET_TCGV_I64(t) + 1)
225 #else /* !DEBUG_TCGV */
227 typedef int TCGv_i32;
228 typedef int TCGv_i64;
229 #if TCG_TARGET_REG_BITS == 32
230 #define TCGv_ptr TCGv_i32
232 #define TCGv_ptr TCGv_i64
234 #define MAKE_TCGV_I32(x) (x)
235 #define MAKE_TCGV_I64(x) (x)
236 #define MAKE_TCGV_PTR(x) (x)
237 #define GET_TCGV_I32(t) (t)
238 #define GET_TCGV_I64(t) (t)
239 #define GET_TCGV_PTR(t) (t)
241 #if TCG_TARGET_REG_BITS == 32
242 #define TCGV_LOW(t) (t)
243 #define TCGV_HIGH(t) ((t) + 1)
246 #endif /* DEBUG_TCGV */
248 #define TCGV_EQUAL_I32(a, b) (GET_TCGV_I32(a) == GET_TCGV_I32(b))
249 #define TCGV_EQUAL_I64(a, b) (GET_TCGV_I64(a) == GET_TCGV_I64(b))
251 /* Dummy definition to avoid compiler warnings. */
252 #define TCGV_UNUSED_I32(x) x = MAKE_TCGV_I32(-1)
253 #define TCGV_UNUSED_I64(x) x = MAKE_TCGV_I64(-1)
256 /* A pure function only reads its arguments and TCG global variables
257 and cannot raise exceptions. Hence a call to a pure function can be
258 safely suppressed if the return value is not used. */
259 #define TCG_CALL_PURE 0x0010
260 /* A const function only reads its arguments and does not use TCG
261 global variables. Hence a call to such a function does not
262 save TCG global variables back to their canonical location. */
263 #define TCG_CALL_CONST 0x0020
265 /* used to align parameters */
266 #define TCG_CALL_DUMMY_TCGV MAKE_TCGV_I32(-1)
267 #define TCG_CALL_DUMMY_ARG ((TCGArg)(-1))
269 /* Conditions. Note that these are layed out for easy manipulation by
270 the the functions below:
271 bit 0 is used for inverting;
274 bit 3 is used with bit 0 for swapping signed/unsigned. */
277 TCG_COND_NEVER = 0 | 0 | 0 | 0,
278 TCG_COND_ALWAYS = 0 | 0 | 0 | 1,
279 TCG_COND_EQ = 8 | 0 | 0 | 0,
280 TCG_COND_NE = 8 | 0 | 0 | 1,
282 TCG_COND_LT = 0 | 0 | 2 | 0,
283 TCG_COND_GE = 0 | 0 | 2 | 1,
284 TCG_COND_LE = 8 | 0 | 2 | 0,
285 TCG_COND_GT = 8 | 0 | 2 | 1,
287 TCG_COND_LTU = 0 | 4 | 0 | 0,
288 TCG_COND_GEU = 0 | 4 | 0 | 1,
289 TCG_COND_LEU = 8 | 4 | 0 | 0,
290 TCG_COND_GTU = 8 | 4 | 0 | 1,
293 /* Invert the sense of the comparison. */
294 static inline TCGCond tcg_invert_cond(TCGCond c)
296 return (TCGCond)(c ^ 1);
299 /* Swap the operands in a comparison. */
300 static inline TCGCond tcg_swap_cond(TCGCond c)
302 return c & 6 ? (TCGCond)(c ^ 9) : c;
305 static inline TCGCond tcg_unsigned_cond(TCGCond c)
307 return c & 2 ? (TCGCond)(c ^ 6) : c;
310 static inline bool is_unsigned_cond(TCGCond c)
315 #define TEMP_VAL_DEAD 0
316 #define TEMP_VAL_REG 1
317 #define TEMP_VAL_MEM 2
318 #define TEMP_VAL_CONST 3
320 /* XXX: optimize memory layout */
321 typedef struct TCGTemp {
328 tcg_target_long mem_offset;
329 unsigned int fixed_reg:1;
330 unsigned int mem_coherent:1;
331 unsigned int mem_allocated:1;
332 unsigned int temp_local:1; /* If true, the temp is saved across
333 basic blocks. Otherwise, it is not
334 preserved across basic blocks. */
335 unsigned int temp_allocated:1; /* never used for code gen */
336 /* index of next free temp of same base type, -1 if end */
341 typedef struct TCGHelperInfo {
342 tcg_target_ulong func;
346 typedef struct TCGContext TCGContext;
349 uint8_t *pool_cur, *pool_end;
350 TCGPool *pool_first, *pool_current, *pool_first_large;
353 TCGTemp *temps; /* globals first, temps after */
356 /* index of free temps, -1 if none */
357 int first_free_temp[TCG_TYPE_COUNT * 2];
359 /* goto_tb support */
362 uint16_t *tb_next_offset;
363 uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
365 /* liveness analysis */
366 uint16_t *op_dead_args; /* for each operation, each bit tells if the
367 corresponding argument is dead */
369 /* tells in which temporary a given register is. It does not take
370 into account fixed registers */
371 int reg_to_temp[TCG_TARGET_NB_REGS];
372 TCGRegSet reserved_regs;
373 tcg_target_long current_frame_offset;
374 tcg_target_long frame_start;
375 tcg_target_long frame_end;
379 TCGTemp static_temps[TCG_MAX_TEMPS];
381 TCGHelperInfo *helpers;
383 int allocated_helpers;
386 #ifdef CONFIG_PROFILER
390 int64_t op_count; /* total insn count */
391 int op_count_max; /* max insn per TB */
394 int64_t del_op_count;
396 int64_t code_out_len;
401 int64_t restore_count;
402 int64_t restore_time;
405 #ifdef CONFIG_DEBUG_TCG
407 int goto_tb_issue_mask;
411 extern TCGContext tcg_ctx;
412 extern uint16_t *gen_opc_ptr;
413 extern TCGArg *gen_opparam_ptr;
414 extern uint16_t gen_opc_buf[];
415 extern TCGArg gen_opparam_buf[];
417 /* pool based memory allocation */
419 void *tcg_malloc_internal(TCGContext *s, int size);
420 void tcg_pool_reset(TCGContext *s);
421 void tcg_pool_delete(TCGContext *s);
423 static inline void *tcg_malloc(int size)
425 TCGContext *s = &tcg_ctx;
426 uint8_t *ptr, *ptr_end;
427 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
429 ptr_end = ptr + size;
430 if (unlikely(ptr_end > s->pool_end)) {
431 return tcg_malloc_internal(&tcg_ctx, size);
433 s->pool_cur = ptr_end;
438 void tcg_context_init(TCGContext *s);
439 void tcg_prologue_init(TCGContext *s);
440 void tcg_func_start(TCGContext *s);
442 int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf);
443 int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
445 void tcg_set_frame(TCGContext *s, int reg,
446 tcg_target_long start, tcg_target_long size);
448 TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name);
449 TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
451 TCGv_i32 tcg_temp_new_internal_i32(int temp_local);
452 static inline TCGv_i32 tcg_temp_new_i32(void)
454 return tcg_temp_new_internal_i32(0);
456 static inline TCGv_i32 tcg_temp_local_new_i32(void)
458 return tcg_temp_new_internal_i32(1);
460 void tcg_temp_free_i32(TCGv_i32 arg);
461 char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg);
463 TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name);
464 TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
466 TCGv_i64 tcg_temp_new_internal_i64(int temp_local);
467 static inline TCGv_i64 tcg_temp_new_i64(void)
469 return tcg_temp_new_internal_i64(0);
471 static inline TCGv_i64 tcg_temp_local_new_i64(void)
473 return tcg_temp_new_internal_i64(1);
475 void tcg_temp_free_i64(TCGv_i64 arg);
476 char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg);
478 #if defined(CONFIG_DEBUG_TCG)
479 /* If you call tcg_clear_temp_count() at the start of a section of
480 * code which is not supposed to leak any TCG temporaries, then
481 * calling tcg_check_temp_count() at the end of the section will
482 * return 1 if the section did in fact leak a temporary.
484 void tcg_clear_temp_count(void);
485 int tcg_check_temp_count(void);
487 #define tcg_clear_temp_count() do { } while (0)
488 #define tcg_check_temp_count() 0
491 void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf);
493 #define TCG_CT_ALIAS 0x80
494 #define TCG_CT_IALIAS 0x40
495 #define TCG_CT_REG 0x01
496 #define TCG_CT_CONST 0x02 /* any constant of register size */
498 typedef struct TCGArgConstraint {
506 #define TCG_MAX_OP_ARGS 16
508 /* Bits for TCGOpDef->flags, 8 bits available. */
510 /* Instruction defines the end of a basic block. */
511 TCG_OPF_BB_END = 0x01,
512 /* Instruction clobbers call registers and potentially update globals. */
513 TCG_OPF_CALL_CLOBBER = 0x02,
514 /* Instruction has side effects: it cannot be removed
515 if its outputs are not used. */
516 TCG_OPF_SIDE_EFFECTS = 0x04,
517 /* Instruction operands are 64-bits (otherwise 32-bits). */
518 TCG_OPF_64BIT = 0x08,
519 /* Instruction is optional and not implemented by the host. */
520 TCG_OPF_NOT_PRESENT = 0x10,
523 typedef struct TCGOpDef {
525 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
527 TCGArgConstraint *args_ct;
529 #if defined(CONFIG_DEBUG_TCG)
534 extern TCGOpDef tcg_op_defs[];
535 extern const size_t tcg_op_defs_max;
537 typedef struct TCGTargetOpDef {
539 const char *args_ct_str[TCG_MAX_OP_ARGS];
542 #define tcg_abort() \
544 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
548 #ifdef CONFIG_DEBUG_TCG
549 # define tcg_debug_assert(X) do { assert(X); } while (0)
550 #elif QEMU_GNUC_PREREQ(4, 5)
551 # define tcg_debug_assert(X) \
552 do { if (!(X)) { __builtin_unreachable(); } } while (0)
554 # define tcg_debug_assert(X) do { (void)(X); } while (0)
557 void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
559 #if TCG_TARGET_REG_BITS == 32
560 #define TCGV_NAT_TO_PTR(n) MAKE_TCGV_PTR(GET_TCGV_I32(n))
561 #define TCGV_PTR_TO_NAT(n) MAKE_TCGV_I32(GET_TCGV_PTR(n))
563 #define tcg_const_ptr(V) TCGV_NAT_TO_PTR(tcg_const_i32((tcg_target_long)(V)))
564 #define tcg_global_reg_new_ptr(R, N) \
565 TCGV_NAT_TO_PTR(tcg_global_reg_new_i32((R), (N)))
566 #define tcg_global_mem_new_ptr(R, O, N) \
567 TCGV_NAT_TO_PTR(tcg_global_mem_new_i32((R), (O), (N)))
568 #define tcg_temp_new_ptr() TCGV_NAT_TO_PTR(tcg_temp_new_i32())
569 #define tcg_temp_free_ptr(T) tcg_temp_free_i32(TCGV_PTR_TO_NAT(T))
571 #define TCGV_NAT_TO_PTR(n) MAKE_TCGV_PTR(GET_TCGV_I64(n))
572 #define TCGV_PTR_TO_NAT(n) MAKE_TCGV_I64(GET_TCGV_PTR(n))
574 #define tcg_const_ptr(V) TCGV_NAT_TO_PTR(tcg_const_i64((tcg_target_long)(V)))
575 #define tcg_global_reg_new_ptr(R, N) \
576 TCGV_NAT_TO_PTR(tcg_global_reg_new_i64((R), (N)))
577 #define tcg_global_mem_new_ptr(R, O, N) \
578 TCGV_NAT_TO_PTR(tcg_global_mem_new_i64((R), (O), (N)))
579 #define tcg_temp_new_ptr() TCGV_NAT_TO_PTR(tcg_temp_new_i64())
580 #define tcg_temp_free_ptr(T) tcg_temp_free_i64(TCGV_PTR_TO_NAT(T))
583 void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
584 int sizemask, TCGArg ret, int nargs, TCGArg *args);
586 void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
587 int c, int right, int arith);
589 TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr, TCGArg *args,
590 TCGOpDef *tcg_op_def);
592 /* only used for debugging purposes */
593 void tcg_register_helper(void *func, const char *name);
594 const char *tcg_helper_get_name(TCGContext *s, void *func);
595 void tcg_dump_ops(TCGContext *s);
597 void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
598 TCGv_i32 tcg_const_i32(int32_t val);
599 TCGv_i64 tcg_const_i64(int64_t val);
600 TCGv_i32 tcg_const_local_i32(int32_t val);
601 TCGv_i64 tcg_const_local_i64(int64_t val);
603 extern uint8_t code_gen_prologue[];
605 /* TCG targets may use a different definition of tcg_qemu_tb_exec. */
606 #if !defined(tcg_qemu_tb_exec)
607 # define tcg_qemu_tb_exec(env, tb_ptr) \
608 ((tcg_target_ulong (*)(void *, void *))code_gen_prologue)(env, tb_ptr)
611 void tcg_register_jit(void *buf, size_t buf_size);