x86: add fptag_vmstate to the state
[armpft.git] / tcg / tcg.h
blob83655777c0ddd628f1724387298450b1a7f32edb
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 enum {
51 #define DEF(s, n, copy_size) INDEX_op_ ## s,
52 #include "tcg-opc.h"
53 #undef DEF
54 NB_OPS,
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) |= 1 << (r)
61 #define tcg_regset_reset_reg(d, r) (d) &= ~(1 << (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)
68 typedef struct TCGRelocation {
69 struct TCGRelocation *next;
70 int type;
71 uint8_t *ptr;
72 tcg_target_long addend;
73 } TCGRelocation;
75 typedef struct TCGLabel {
76 int has_value;
77 union {
78 tcg_target_ulong value;
79 TCGRelocation *first_reloc;
80 } u;
81 } TCGLabel;
83 typedef struct TCGPool {
84 struct TCGPool *next;
85 int size;
86 uint8_t data[0] __attribute__ ((aligned));
87 } TCGPool;
89 #define TCG_POOL_CHUNK_SIZE 32768
91 #define TCG_MAX_LABELS 512
93 #define TCG_MAX_TEMPS 512
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
99 typedef int TCGType;
101 #define TCG_TYPE_I32 0
102 #define TCG_TYPE_I64 1
103 #define TCG_TYPE_COUNT 2 /* number of different types */
105 #if TCG_TARGET_REG_BITS == 32
106 #define TCG_TYPE_PTR TCG_TYPE_I32
107 #else
108 #define TCG_TYPE_PTR TCG_TYPE_I64
109 #endif
111 typedef tcg_target_ulong TCGArg;
113 /* Define a type and accessor macros for varables. Using a struct is
114 nice because it gives some level of type safely. Ideally the compiler
115 be able to see through all this. However in practice this is not true,
116 expecially on targets with braindamaged ABIs (e.g. i386).
117 We use plain int by default to avoid this runtime overhead.
118 Users of tcg_gen_* don't need to know about any of this, and should
119 treat TCGv as an opaque type.
120 In additon we do typechecking for different types of variables. TCGv_i32
121 and TCGv_i64 are 32/64-bit variables respectively. TCGv and TCGv_ptr
122 are aliases for target_ulong and host pointer sized values respectively.
125 #ifdef CONFIG_DEBUG_TCG
126 #define DEBUG_TCGV 1
127 #endif
129 #ifdef DEBUG_TCGV
131 typedef struct
133 int i32;
134 } TCGv_i32;
136 typedef struct
138 int i64;
139 } TCGv_i64;
141 #define MAKE_TCGV_I32(i) __extension__ \
142 ({ TCGv_i32 make_tcgv_tmp = {i}; make_tcgv_tmp;})
143 #define MAKE_TCGV_I64(i) __extension__ \
144 ({ TCGv_i64 make_tcgv_tmp = {i}; make_tcgv_tmp;})
145 #define GET_TCGV_I32(t) ((t).i32)
146 #define GET_TCGV_I64(t) ((t).i64)
147 #if TCG_TARGET_REG_BITS == 32
148 #define TCGV_LOW(t) MAKE_TCGV_I32(GET_TCGV_I64(t))
149 #define TCGV_HIGH(t) MAKE_TCGV_I32(GET_TCGV_I64(t) + 1)
150 #endif
152 #else /* !DEBUG_TCGV */
154 typedef int TCGv_i32;
155 typedef int TCGv_i64;
156 #define MAKE_TCGV_I32(x) (x)
157 #define MAKE_TCGV_I64(x) (x)
158 #define GET_TCGV_I32(t) (t)
159 #define GET_TCGV_I64(t) (t)
161 #if TCG_TARGET_REG_BITS == 32
162 #define TCGV_LOW(t) (t)
163 #define TCGV_HIGH(t) ((t) + 1)
164 #endif
166 #endif /* DEBUG_TCGV */
168 #define TCGV_EQUAL_I32(a, b) (GET_TCGV_I32(a) == GET_TCGV_I32(b))
169 #define TCGV_EQUAL_I64(a, b) (GET_TCGV_I64(a) == GET_TCGV_I64(b))
171 /* Dummy definition to avoid compiler warnings. */
172 #define TCGV_UNUSED_I32(x) x = MAKE_TCGV_I32(-1)
173 #define TCGV_UNUSED_I64(x) x = MAKE_TCGV_I64(-1)
175 /* call flags */
176 #define TCG_CALL_TYPE_MASK 0x000f
177 #define TCG_CALL_TYPE_STD 0x0000 /* standard C call */
178 #define TCG_CALL_TYPE_REGPARM_1 0x0001 /* i386 style regparm call (1 reg) */
179 #define TCG_CALL_TYPE_REGPARM_2 0x0002 /* i386 style regparm call (2 regs) */
180 #define TCG_CALL_TYPE_REGPARM 0x0003 /* i386 style regparm call (3 regs) */
181 /* A pure function only reads its arguments and TCG global variables
182 and cannot raise exceptions. Hence a call to a pure function can be
183 safely suppressed if the return value is not used. */
184 #define TCG_CALL_PURE 0x0010
185 /* A const function only reads its arguments and does not use TCG
186 global variables. Hence a call to such a function does not
187 save TCG global variables back to their canonical location. */
188 #define TCG_CALL_CONST 0x0020
190 /* used to align parameters */
191 #define TCG_CALL_DUMMY_TCGV MAKE_TCGV_I32(-1)
192 #define TCG_CALL_DUMMY_ARG ((TCGArg)(-1))
194 typedef enum {
195 TCG_COND_EQ,
196 TCG_COND_NE,
197 TCG_COND_LT,
198 TCG_COND_GE,
199 TCG_COND_LE,
200 TCG_COND_GT,
201 /* unsigned */
202 TCG_COND_LTU,
203 TCG_COND_GEU,
204 TCG_COND_LEU,
205 TCG_COND_GTU,
206 } TCGCond;
208 #define TEMP_VAL_DEAD 0
209 #define TEMP_VAL_REG 1
210 #define TEMP_VAL_MEM 2
211 #define TEMP_VAL_CONST 3
213 /* XXX: optimize memory layout */
214 typedef struct TCGTemp {
215 TCGType base_type;
216 TCGType type;
217 int val_type;
218 int reg;
219 tcg_target_long val;
220 int mem_reg;
221 tcg_target_long mem_offset;
222 unsigned int fixed_reg:1;
223 unsigned int mem_coherent:1;
224 unsigned int mem_allocated:1;
225 unsigned int temp_local:1; /* If true, the temp is saved accross
226 basic blocks. Otherwise, it is not
227 preserved accross basic blocks. */
228 unsigned int temp_allocated:1; /* never used for code gen */
229 /* index of next free temp of same base type, -1 if end */
230 int next_free_temp;
231 const char *name;
232 } TCGTemp;
234 typedef struct TCGHelperInfo {
235 tcg_target_ulong func;
236 const char *name;
237 } TCGHelperInfo;
239 typedef struct TCGContext TCGContext;
241 struct TCGContext {
242 uint8_t *pool_cur, *pool_end;
243 TCGPool *pool_first, *pool_current;
244 TCGLabel *labels;
245 int nb_labels;
246 TCGTemp *temps; /* globals first, temps after */
247 int nb_globals;
248 int nb_temps;
249 /* index of free temps, -1 if none */
250 int first_free_temp[TCG_TYPE_COUNT * 2];
252 /* goto_tb support */
253 uint8_t *code_buf;
254 unsigned long *tb_next;
255 uint16_t *tb_next_offset;
256 uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
258 /* liveness analysis */
259 uint16_t *op_dead_iargs; /* for each operation, each bit tells if the
260 corresponding input argument is dead */
262 /* tells in which temporary a given register is. It does not take
263 into account fixed registers */
264 int reg_to_temp[TCG_TARGET_NB_REGS];
265 TCGRegSet reserved_regs;
266 tcg_target_long current_frame_offset;
267 tcg_target_long frame_start;
268 tcg_target_long frame_end;
269 int frame_reg;
271 uint8_t *code_ptr;
272 TCGTemp static_temps[TCG_MAX_TEMPS];
274 TCGHelperInfo *helpers;
275 int nb_helpers;
276 int allocated_helpers;
277 int helpers_sorted;
279 #ifdef CONFIG_PROFILER
280 /* profiling info */
281 int64_t tb_count1;
282 int64_t tb_count;
283 int64_t op_count; /* total insn count */
284 int op_count_max; /* max insn per TB */
285 int64_t temp_count;
286 int temp_count_max;
287 int64_t del_op_count;
288 int64_t code_in_len;
289 int64_t code_out_len;
290 int64_t interm_time;
291 int64_t code_time;
292 int64_t la_time;
293 int64_t restore_count;
294 int64_t restore_time;
295 #endif
298 extern TCGContext tcg_ctx;
299 extern uint16_t *gen_opc_ptr;
300 extern TCGArg *gen_opparam_ptr;
301 extern uint16_t gen_opc_buf[];
302 extern TCGArg gen_opparam_buf[];
304 /* pool based memory allocation */
306 void *tcg_malloc_internal(TCGContext *s, int size);
307 void tcg_pool_reset(TCGContext *s);
308 void tcg_pool_delete(TCGContext *s);
310 static inline void *tcg_malloc(int size)
312 TCGContext *s = &tcg_ctx;
313 uint8_t *ptr, *ptr_end;
314 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
315 ptr = s->pool_cur;
316 ptr_end = ptr + size;
317 if (unlikely(ptr_end > s->pool_end)) {
318 return tcg_malloc_internal(&tcg_ctx, size);
319 } else {
320 s->pool_cur = ptr_end;
321 return ptr;
325 void tcg_context_init(TCGContext *s);
326 void tcg_func_start(TCGContext *s);
328 int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf);
329 int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
331 void tcg_set_frame(TCGContext *s, int reg,
332 tcg_target_long start, tcg_target_long size);
334 TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name);
335 TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
336 const char *name);
337 TCGv_i32 tcg_temp_new_internal_i32(int temp_local);
338 static inline TCGv_i32 tcg_temp_new_i32(void)
340 return tcg_temp_new_internal_i32(0);
342 static inline TCGv_i32 tcg_temp_local_new_i32(void)
344 return tcg_temp_new_internal_i32(1);
346 void tcg_temp_free_i32(TCGv_i32 arg);
347 char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg);
349 TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name);
350 TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
351 const char *name);
352 TCGv_i64 tcg_temp_new_internal_i64(int temp_local);
353 static inline TCGv_i64 tcg_temp_new_i64(void)
355 return tcg_temp_new_internal_i64(0);
357 static inline TCGv_i64 tcg_temp_local_new_i64(void)
359 return tcg_temp_new_internal_i64(1);
361 void tcg_temp_free_i64(TCGv_i64 arg);
362 char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg);
364 void tcg_dump_info(FILE *f,
365 int (*cpu_fprintf)(FILE *f, const char *fmt, ...));
367 #define TCG_CT_ALIAS 0x80
368 #define TCG_CT_IALIAS 0x40
369 #define TCG_CT_REG 0x01
370 #define TCG_CT_CONST 0x02 /* any constant of register size */
372 typedef struct TCGArgConstraint {
373 uint16_t ct;
374 uint8_t alias_index;
375 union {
376 TCGRegSet regs;
377 } u;
378 } TCGArgConstraint;
380 #define TCG_MAX_OP_ARGS 16
382 #define TCG_OPF_BB_END 0x01 /* instruction defines the end of a basic
383 block */
384 #define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers
385 and potentially update globals. */
386 #define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects : it
387 cannot be removed if its output
388 are not used */
390 typedef struct TCGOpDef {
391 const char *name;
392 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
393 uint8_t flags;
394 uint16_t copy_size;
395 TCGArgConstraint *args_ct;
396 int *sorted_args;
397 } TCGOpDef;
399 typedef struct TCGTargetOpDef {
400 int op;
401 const char *args_ct_str[TCG_MAX_OP_ARGS];
402 } TCGTargetOpDef;
404 void tcg_target_init(TCGContext *s);
405 void tcg_target_qemu_prologue(TCGContext *s);
407 #define tcg_abort() \
408 do {\
409 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
410 abort();\
411 } while (0)
413 void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
415 #if TCG_TARGET_REG_BITS == 32
416 #define tcg_const_ptr tcg_const_i32
417 #define tcg_add_ptr tcg_add_i32
418 #define tcg_sub_ptr tcg_sub_i32
419 #define TCGv_ptr TCGv_i32
420 #define GET_TCGV_PTR GET_TCGV_I32
421 #define tcg_global_reg_new_ptr tcg_global_reg_new_i32
422 #define tcg_global_mem_new_ptr tcg_global_mem_new_i32
423 #define tcg_temp_new_ptr tcg_temp_new_i32
424 #define tcg_temp_free_ptr tcg_temp_free_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 #define TCGv_ptr TCGv_i64
430 #define GET_TCGV_PTR GET_TCGV_I64
431 #define tcg_global_reg_new_ptr tcg_global_reg_new_i64
432 #define tcg_global_mem_new_ptr tcg_global_mem_new_i64
433 #define tcg_temp_new_ptr tcg_temp_new_i64
434 #define tcg_temp_free_ptr tcg_temp_free_i64
435 #endif
437 void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
438 int sizemask, TCGArg ret, int nargs, TCGArg *args);
440 void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
441 int c, int right, int arith);
443 /* only used for debugging purposes */
444 void tcg_register_helper(void *func, const char *name);
445 const char *tcg_helper_get_name(TCGContext *s, void *func);
446 void tcg_dump_ops(TCGContext *s, FILE *outfile);
448 void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
449 TCGv_i32 tcg_const_i32(int32_t val);
450 TCGv_i64 tcg_const_i64(int64_t val);
451 TCGv_i32 tcg_const_local_i32(int32_t val);
452 TCGv_i64 tcg_const_local_i64(int64_t val);
454 void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
455 int label_index, long addend);
457 extern uint8_t code_gen_prologue[];
458 #if defined(_ARCH_PPC) && !defined(_ARCH_PPC64)
459 #define tcg_qemu_tb_exec(tb_ptr) \
460 ((long REGPARM __attribute__ ((longcall)) (*)(void *))code_gen_prologue)(tb_ptr)
461 #else
462 #define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)
463 #endif