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