]> Git Repo - qemu.git/blame - tcg/tcg.h
tcg: Add tcg_swap_cond.
[qemu.git] / tcg / tcg.h
CommitLineData
c896fe29
FB
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 */
f8393946 24#include "qemu-common.h"
c896fe29 25#include "tcg-target.h"
96e132e2 26#include "tcg-runtime.h"
c896fe29
FB
27
28#if TCG_TARGET_REG_BITS == 32
29typedef int32_t tcg_target_long;
30typedef uint32_t tcg_target_ulong;
31#define TCG_PRIlx PRIx32
32#define TCG_PRIld PRId32
33#elif TCG_TARGET_REG_BITS == 64
34typedef int64_t tcg_target_long;
35typedef 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
43typedef uint32_t TCGRegSet;
44#elif TCG_TARGET_NB_REGS <= 64
45typedef uint64_t TCGRegSet;
46#else
47#error unsupported
48#endif
49
50enum {
51#define DEF(s, n, copy_size) INDEX_op_ ## s,
52#include "tcg-opc.h"
53#undef DEF
54 NB_OPS,
55};
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)
7d301752
AJ
60#define tcg_regset_set_reg(d, r) (d) |= 1L << (r)
61#define tcg_regset_reset_reg(d, r) (d) &= ~(1L << (r))
c896fe29
FB
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
68typedef struct TCGRelocation {
69 struct TCGRelocation *next;
70 int type;
71 uint8_t *ptr;
72 tcg_target_long addend;
73} TCGRelocation;
74
75typedef struct TCGLabel {
c44f945a 76 int has_value;
c896fe29
FB
77 union {
78 tcg_target_ulong value;
79 TCGRelocation *first_reloc;
80 } u;
81} TCGLabel;
82
83typedef struct TCGPool {
84 struct TCGPool *next;
c44f945a
BS
85 int size;
86 uint8_t data[0] __attribute__ ((aligned));
c896fe29
FB
87} TCGPool;
88
89#define TCG_POOL_CHUNK_SIZE 32768
90
91#define TCG_MAX_LABELS 512
92
c4071c90 93#define TCG_MAX_TEMPS 512
c896fe29 94
b03cce8e
FB
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
c896fe29
FB
99typedef int TCGType;
100
101#define TCG_TYPE_I32 0
102#define TCG_TYPE_I64 1
e8996ee0 103#define TCG_TYPE_COUNT 2 /* number of different types */
c896fe29
FB
104
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
110
111typedef tcg_target_ulong TCGArg;
112
ac56dd48
PB
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
a7812ae4
PB
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.
123 */
ac56dd48 124
092c73ee 125#ifdef CONFIG_DEBUG_TCG
f8393946
AJ
126#define DEBUG_TCGV 1
127#endif
ac56dd48
PB
128
129#ifdef DEBUG_TCGV
130
131typedef struct
132{
a810a2de 133 int i32;
a7812ae4 134} TCGv_i32;
ac56dd48 135
a7812ae4
PB
136typedef struct
137{
a810a2de 138 int i64;
a7812ae4
PB
139} TCGv_i64;
140
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;})
a810a2de
BS
145#define GET_TCGV_I32(t) ((t).i32)
146#define GET_TCGV_I64(t) ((t).i64)
ac56dd48 147#if TCG_TARGET_REG_BITS == 32
a7812ae4
PB
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)
ac56dd48
PB
150#endif
151
152#else /* !DEBUG_TCGV */
153
a7812ae4
PB
154typedef int TCGv_i32;
155typedef 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)
44e6acb0 160
ac56dd48 161#if TCG_TARGET_REG_BITS == 32
a7812ae4 162#define TCGV_LOW(t) (t)
ac56dd48
PB
163#define TCGV_HIGH(t) ((t) + 1)
164#endif
165
166#endif /* DEBUG_TCGV */
167
43e860ef
AJ
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))
170
a50f5b91 171/* Dummy definition to avoid compiler warnings. */
a7812ae4
PB
172#define TCGV_UNUSED_I32(x) x = MAKE_TCGV_I32(-1)
173#define TCGV_UNUSED_I64(x) x = MAKE_TCGV_I64(-1)
a50f5b91 174
c896fe29
FB
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) */
3e00b3f5 181/* A pure function only reads its arguments and TCG global variables
34d5a9ff 182 and cannot raise exceptions. Hence a call to a pure function can be
c6e113f5
FB
183 safely suppressed if the return value is not used. */
184#define TCG_CALL_PURE 0x0010
b9c18f56 185/* A const function only reads its arguments and does not use TCG
3e00b3f5
AJ
186 global variables. Hence a call to such a function does not
187 save TCG global variables back to their canonical location. */
b9c18f56 188#define TCG_CALL_CONST 0x0020
c896fe29 189
39cf05d3 190/* used to align parameters */
a7812ae4 191#define TCG_CALL_DUMMY_TCGV MAKE_TCGV_I32(-1)
39cf05d3
FB
192#define TCG_CALL_DUMMY_ARG ((TCGArg)(-1))
193
c896fe29
FB
194typedef 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;
207
1c086220 208/* Invert the sense of the comparison. */
401d466d
RH
209static inline TCGCond tcg_invert_cond(TCGCond c)
210{
211 return (TCGCond)(c ^ 1);
212}
213
1c086220
RH
214/* Swap the operands in a comparison. */
215static inline TCGCond tcg_swap_cond(TCGCond c)
216{
217 int mask = (c < TCG_COND_LT ? 0 : c < TCG_COND_LTU ? 7 : 15);
218 return (TCGCond)(c ^ mask);
219}
220
ff44c2f3
RH
221static inline TCGCond tcg_unsigned_cond(TCGCond c)
222{
223 return (c >= TCG_COND_LT && c <= TCG_COND_GT ? c + 4 : c);
224}
225
c896fe29
FB
226#define TEMP_VAL_DEAD 0
227#define TEMP_VAL_REG 1
228#define TEMP_VAL_MEM 2
229#define TEMP_VAL_CONST 3
230
231/* XXX: optimize memory layout */
232typedef struct TCGTemp {
233 TCGType base_type;
234 TCGType type;
235 int val_type;
236 int reg;
237 tcg_target_long val;
238 int mem_reg;
239 tcg_target_long mem_offset;
240 unsigned int fixed_reg:1;
241 unsigned int mem_coherent:1;
242 unsigned int mem_allocated:1;
641d5fbe
FB
243 unsigned int temp_local:1; /* If true, the temp is saved accross
244 basic blocks. Otherwise, it is not
245 preserved accross basic blocks. */
e8996ee0
FB
246 unsigned int temp_allocated:1; /* never used for code gen */
247 /* index of next free temp of same base type, -1 if end */
248 int next_free_temp;
c896fe29
FB
249 const char *name;
250} TCGTemp;
251
252typedef struct TCGHelperInfo {
4dc81f28 253 tcg_target_ulong func;
c896fe29
FB
254 const char *name;
255} TCGHelperInfo;
256
257typedef struct TCGContext TCGContext;
258
c896fe29
FB
259struct TCGContext {
260 uint8_t *pool_cur, *pool_end;
261 TCGPool *pool_first, *pool_current;
262 TCGLabel *labels;
263 int nb_labels;
264 TCGTemp *temps; /* globals first, temps after */
265 int nb_globals;
266 int nb_temps;
641d5fbe
FB
267 /* index of free temps, -1 if none */
268 int first_free_temp[TCG_TYPE_COUNT * 2];
c896fe29
FB
269
270 /* goto_tb support */
271 uint8_t *code_buf;
272 unsigned long *tb_next;
273 uint16_t *tb_next_offset;
274 uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
275
641d5fbe 276 /* liveness analysis */
c896fe29
FB
277 uint16_t *op_dead_iargs; /* for each operation, each bit tells if the
278 corresponding input argument is dead */
641d5fbe 279
c896fe29
FB
280 /* tells in which temporary a given register is. It does not take
281 into account fixed registers */
282 int reg_to_temp[TCG_TARGET_NB_REGS];
283 TCGRegSet reserved_regs;
284 tcg_target_long current_frame_offset;
285 tcg_target_long frame_start;
286 tcg_target_long frame_end;
287 int frame_reg;
288
289 uint8_t *code_ptr;
290 TCGTemp static_temps[TCG_MAX_TEMPS];
291
c896fe29
FB
292 TCGHelperInfo *helpers;
293 int nb_helpers;
294 int allocated_helpers;
e8996ee0 295 int helpers_sorted;
a23a9ec6
FB
296
297#ifdef CONFIG_PROFILER
298 /* profiling info */
299 int64_t tb_count1;
300 int64_t tb_count;
301 int64_t op_count; /* total insn count */
302 int op_count_max; /* max insn per TB */
303 int64_t temp_count;
304 int temp_count_max;
a23a9ec6
FB
305 int64_t del_op_count;
306 int64_t code_in_len;
307 int64_t code_out_len;
308 int64_t interm_time;
309 int64_t code_time;
310 int64_t la_time;
311 int64_t restore_count;
312 int64_t restore_time;
313#endif
c896fe29
FB
314};
315
316extern TCGContext tcg_ctx;
317extern uint16_t *gen_opc_ptr;
318extern TCGArg *gen_opparam_ptr;
319extern uint16_t gen_opc_buf[];
320extern TCGArg gen_opparam_buf[];
321
322/* pool based memory allocation */
323
324void *tcg_malloc_internal(TCGContext *s, int size);
325void tcg_pool_reset(TCGContext *s);
326void tcg_pool_delete(TCGContext *s);
327
328static inline void *tcg_malloc(int size)
329{
330 TCGContext *s = &tcg_ctx;
331 uint8_t *ptr, *ptr_end;
332 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
333 ptr = s->pool_cur;
334 ptr_end = ptr + size;
335 if (unlikely(ptr_end > s->pool_end)) {
336 return tcg_malloc_internal(&tcg_ctx, size);
337 } else {
338 s->pool_cur = ptr_end;
339 return ptr;
340 }
341}
342
343void tcg_context_init(TCGContext *s);
344void tcg_func_start(TCGContext *s);
345
54604f74
AJ
346int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf);
347int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
c896fe29
FB
348
349void tcg_set_frame(TCGContext *s, int reg,
350 tcg_target_long start, tcg_target_long size);
a7812ae4
PB
351
352TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name);
353TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
354 const char *name);
355TCGv_i32 tcg_temp_new_internal_i32(int temp_local);
356static inline TCGv_i32 tcg_temp_new_i32(void)
357{
358 return tcg_temp_new_internal_i32(0);
359}
360static inline TCGv_i32 tcg_temp_local_new_i32(void)
361{
362 return tcg_temp_new_internal_i32(1);
363}
364void tcg_temp_free_i32(TCGv_i32 arg);
365char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg);
366
367TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name);
368TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
369 const char *name);
370TCGv_i64 tcg_temp_new_internal_i64(int temp_local);
371static inline TCGv_i64 tcg_temp_new_i64(void)
641d5fbe 372{
a7812ae4 373 return tcg_temp_new_internal_i64(0);
641d5fbe 374}
a7812ae4 375static inline TCGv_i64 tcg_temp_local_new_i64(void)
641d5fbe 376{
a7812ae4 377 return tcg_temp_new_internal_i64(1);
641d5fbe 378}
a7812ae4
PB
379void tcg_temp_free_i64(TCGv_i64 arg);
380char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg);
381
a23a9ec6
FB
382void tcg_dump_info(FILE *f,
383 int (*cpu_fprintf)(FILE *f, const char *fmt, ...));
c896fe29
FB
384
385#define TCG_CT_ALIAS 0x80
386#define TCG_CT_IALIAS 0x40
387#define TCG_CT_REG 0x01
388#define TCG_CT_CONST 0x02 /* any constant of register size */
389
390typedef struct TCGArgConstraint {
5ff9d6a4
FB
391 uint16_t ct;
392 uint8_t alias_index;
c896fe29
FB
393 union {
394 TCGRegSet regs;
395 } u;
396} TCGArgConstraint;
397
398#define TCG_MAX_OP_ARGS 16
399
400#define TCG_OPF_BB_END 0x01 /* instruction defines the end of a basic
401 block */
b03cce8e
FB
402#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers
403 and potentially update globals. */
404#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects : it
405 cannot be removed if its output
406 are not used */
c896fe29
FB
407
408typedef struct TCGOpDef {
409 const char *name;
410 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
411 uint8_t flags;
412 uint16_t copy_size;
413 TCGArgConstraint *args_ct;
414 int *sorted_args;
415} TCGOpDef;
416
417typedef struct TCGTargetOpDef {
418 int op;
419 const char *args_ct_str[TCG_MAX_OP_ARGS];
420} TCGTargetOpDef;
421
c896fe29 422void tcg_target_init(TCGContext *s);
b03cce8e 423void tcg_target_qemu_prologue(TCGContext *s);
c896fe29
FB
424
425#define tcg_abort() \
426do {\
427 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
428 abort();\
429} while (0)
430
431void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
432
c896fe29
FB
433#if TCG_TARGET_REG_BITS == 32
434#define tcg_const_ptr tcg_const_i32
435#define tcg_add_ptr tcg_add_i32
436#define tcg_sub_ptr tcg_sub_i32
a7812ae4
PB
437#define TCGv_ptr TCGv_i32
438#define GET_TCGV_PTR GET_TCGV_I32
439#define tcg_global_reg_new_ptr tcg_global_reg_new_i32
440#define tcg_global_mem_new_ptr tcg_global_mem_new_i32
441#define tcg_temp_new_ptr tcg_temp_new_i32
442#define tcg_temp_free_ptr tcg_temp_free_i32
c896fe29
FB
443#else
444#define tcg_const_ptr tcg_const_i64
445#define tcg_add_ptr tcg_add_i64
446#define tcg_sub_ptr tcg_sub_i64
a7812ae4
PB
447#define TCGv_ptr TCGv_i64
448#define GET_TCGV_PTR GET_TCGV_I64
449#define tcg_global_reg_new_ptr tcg_global_reg_new_i64
450#define tcg_global_mem_new_ptr tcg_global_mem_new_i64
451#define tcg_temp_new_ptr tcg_temp_new_i64
452#define tcg_temp_free_ptr tcg_temp_free_i64
c896fe29
FB
453#endif
454
a7812ae4
PB
455void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
456 int sizemask, TCGArg ret, int nargs, TCGArg *args);
457
458void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
459 int c, int right, int arith);
460
461/* only used for debugging purposes */
462void tcg_register_helper(void *func, const char *name);
463const char *tcg_helper_get_name(TCGContext *s, void *func);
464void tcg_dump_ops(TCGContext *s, FILE *outfile);
465
466void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
467TCGv_i32 tcg_const_i32(int32_t val);
468TCGv_i64 tcg_const_i64(int64_t val);
469TCGv_i32 tcg_const_local_i32(int32_t val);
470TCGv_i64 tcg_const_local_i64(int64_t val);
471
c896fe29
FB
472void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
473 int label_index, long addend);
c896fe29 474
b03cce8e 475extern uint8_t code_gen_prologue[];
e58ffeb3 476#if defined(_ARCH_PPC) && !defined(_ARCH_PPC64)
932a6909
FB
477#define tcg_qemu_tb_exec(tb_ptr) \
478 ((long REGPARM __attribute__ ((longcall)) (*)(void *))code_gen_prologue)(tb_ptr)
479#else
b03cce8e 480#define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)
932a6909 481#endif
This page took 0.384924 seconds and 4 git commands to generate.