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