]> git.proxmox.com Git - mirror_qemu.git/blame - tcg/tcg.h
Fix build failure with old kernel headers (loop.h is incompatible with
[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
101
102#if TCG_TARGET_REG_BITS == 32
103#define TCG_TYPE_PTR TCG_TYPE_I32
104#else
105#define TCG_TYPE_PTR TCG_TYPE_I64
106#endif
107
108typedef tcg_target_ulong TCGArg;
109
ac56dd48
PB
110/* Define a type and accessor macros for varables. Using a struct is
111 nice because it gives some level of type safely. Ideally the compiler
112 be able to see through all this. However in practice this is not true,
113 expecially on targets with braindamaged ABIs (e.g. i386).
114 We use plain int by default to avoid this runtime overhead.
115 Users of tcg_gen_* don't need to know about any of this, and should
116 treat TCGv as an opaque type. */
117
118//#define DEBUG_TCGV 1
119
120#ifdef DEBUG_TCGV
121
122typedef struct
123{
124 int n;
125} TCGv;
126
127#define MAKE_TCGV(i) __extension__ \
128 ({ TCGv make_tcgv_tmp = {i}; make_tcgv_tmp;})
129#define GET_TCGV(t) ((t).n)
130#if TCG_TARGET_REG_BITS == 32
131#define TCGV_HIGH(t) MAKE_TCGV(GET_TCGV(t) + 1)
132#endif
133
134#else /* !DEBUG_TCGV */
135
136typedef int TCGv;
137#define MAKE_TCGV(x) (x)
138#define GET_TCGV(t) (t)
139#if TCG_TARGET_REG_BITS == 32
140#define TCGV_HIGH(t) ((t) + 1)
141#endif
142
143#endif /* DEBUG_TCGV */
144
c896fe29
FB
145/* call flags */
146#define TCG_CALL_TYPE_MASK 0x000f
147#define TCG_CALL_TYPE_STD 0x0000 /* standard C call */
148#define TCG_CALL_TYPE_REGPARM_1 0x0001 /* i386 style regparm call (1 reg) */
149#define TCG_CALL_TYPE_REGPARM_2 0x0002 /* i386 style regparm call (2 regs) */
150#define TCG_CALL_TYPE_REGPARM 0x0003 /* i386 style regparm call (3 regs) */
c6e113f5
FB
151/* A pure function only reads its arguments and globals variables and
152 cannot raise exceptions. Hence a call to a pure function can be
153 safely suppressed if the return value is not used. */
154#define TCG_CALL_PURE 0x0010
c896fe29 155
39cf05d3
FB
156/* used to align parameters */
157#define TCG_CALL_DUMMY_TCGV MAKE_TCGV(-1)
158#define TCG_CALL_DUMMY_ARG ((TCGArg)(-1))
159
c896fe29
FB
160typedef enum {
161 TCG_COND_EQ,
162 TCG_COND_NE,
163 TCG_COND_LT,
164 TCG_COND_GE,
165 TCG_COND_LE,
166 TCG_COND_GT,
167 /* unsigned */
168 TCG_COND_LTU,
169 TCG_COND_GEU,
170 TCG_COND_LEU,
171 TCG_COND_GTU,
172} TCGCond;
173
174#define TEMP_VAL_DEAD 0
175#define TEMP_VAL_REG 1
176#define TEMP_VAL_MEM 2
177#define TEMP_VAL_CONST 3
178
179/* XXX: optimize memory layout */
180typedef struct TCGTemp {
181 TCGType base_type;
182 TCGType type;
183 int val_type;
184 int reg;
185 tcg_target_long val;
186 int mem_reg;
187 tcg_target_long mem_offset;
188 unsigned int fixed_reg:1;
189 unsigned int mem_coherent:1;
190 unsigned int mem_allocated:1;
191 const char *name;
192} TCGTemp;
193
194typedef struct TCGHelperInfo {
4dc81f28 195 tcg_target_ulong func;
c896fe29
FB
196 const char *name;
197} TCGHelperInfo;
198
199typedef struct TCGContext TCGContext;
200
201typedef void TCGMacroFunc(TCGContext *s, int macro_id, const int *dead_args);
202
203struct TCGContext {
204 uint8_t *pool_cur, *pool_end;
205 TCGPool *pool_first, *pool_current;
206 TCGLabel *labels;
207 int nb_labels;
208 TCGTemp *temps; /* globals first, temps after */
209 int nb_globals;
210 int nb_temps;
211 /* constant indexes (end of temp array) */
212 int const_start;
213 int const_end;
214
215 /* goto_tb support */
216 uint8_t *code_buf;
217 unsigned long *tb_next;
218 uint16_t *tb_next_offset;
219 uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
220
221 uint16_t *op_dead_iargs; /* for each operation, each bit tells if the
222 corresponding input argument is dead */
223 /* tells in which temporary a given register is. It does not take
224 into account fixed registers */
225 int reg_to_temp[TCG_TARGET_NB_REGS];
226 TCGRegSet reserved_regs;
227 tcg_target_long current_frame_offset;
228 tcg_target_long frame_start;
229 tcg_target_long frame_end;
230 int frame_reg;
231
232 uint8_t *code_ptr;
233 TCGTemp static_temps[TCG_MAX_TEMPS];
234
235 TCGMacroFunc *macro_func;
236 TCGHelperInfo *helpers;
237 int nb_helpers;
238 int allocated_helpers;
a23a9ec6
FB
239
240#ifdef CONFIG_PROFILER
241 /* profiling info */
242 int64_t tb_count1;
243 int64_t tb_count;
244 int64_t op_count; /* total insn count */
245 int op_count_max; /* max insn per TB */
246 int64_t temp_count;
247 int temp_count_max;
248 int64_t old_op_count;
249 int64_t del_op_count;
250 int64_t code_in_len;
251 int64_t code_out_len;
252 int64_t interm_time;
253 int64_t code_time;
254 int64_t la_time;
255 int64_t restore_count;
256 int64_t restore_time;
257#endif
c896fe29
FB
258};
259
260extern TCGContext tcg_ctx;
261extern uint16_t *gen_opc_ptr;
262extern TCGArg *gen_opparam_ptr;
263extern uint16_t gen_opc_buf[];
264extern TCGArg gen_opparam_buf[];
265
266/* pool based memory allocation */
267
268void *tcg_malloc_internal(TCGContext *s, int size);
269void tcg_pool_reset(TCGContext *s);
270void tcg_pool_delete(TCGContext *s);
271
272static inline void *tcg_malloc(int size)
273{
274 TCGContext *s = &tcg_ctx;
275 uint8_t *ptr, *ptr_end;
276 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
277 ptr = s->pool_cur;
278 ptr_end = ptr + size;
279 if (unlikely(ptr_end > s->pool_end)) {
280 return tcg_malloc_internal(&tcg_ctx, size);
281 } else {
282 s->pool_cur = ptr_end;
283 return ptr;
284 }
285}
286
287void tcg_context_init(TCGContext *s);
288void tcg_func_start(TCGContext *s);
289
290int dyngen_code(TCGContext *s, uint8_t *gen_code_buf);
623e265c 291int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
c896fe29
FB
292
293void tcg_set_frame(TCGContext *s, int reg,
294 tcg_target_long start, tcg_target_long size);
295void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func);
ac56dd48 296TCGv tcg_global_reg_new(TCGType type, int reg, const char *name);
6a8d7b76
FB
297TCGv tcg_global_reg2_new_hack(TCGType type, int reg1, int reg2,
298 const char *name);
ac56dd48
PB
299TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
300 const char *name);
301TCGv tcg_temp_new(TCGType type);
302char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg);
a23a9ec6
FB
303void tcg_dump_info(FILE *f,
304 int (*cpu_fprintf)(FILE *f, const char *fmt, ...));
c896fe29
FB
305
306#define TCG_CT_ALIAS 0x80
307#define TCG_CT_IALIAS 0x40
308#define TCG_CT_REG 0x01
309#define TCG_CT_CONST 0x02 /* any constant of register size */
310
311typedef struct TCGArgConstraint {
5ff9d6a4
FB
312 uint16_t ct;
313 uint8_t alias_index;
c896fe29
FB
314 union {
315 TCGRegSet regs;
316 } u;
317} TCGArgConstraint;
318
319#define TCG_MAX_OP_ARGS 16
320
321#define TCG_OPF_BB_END 0x01 /* instruction defines the end of a basic
322 block */
b03cce8e
FB
323#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers
324 and potentially update globals. */
325#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects : it
326 cannot be removed if its output
327 are not used */
c896fe29
FB
328
329typedef struct TCGOpDef {
330 const char *name;
331 uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
332 uint8_t flags;
333 uint16_t copy_size;
334 TCGArgConstraint *args_ct;
335 int *sorted_args;
336} TCGOpDef;
337
338typedef struct TCGTargetOpDef {
339 int op;
340 const char *args_ct_str[TCG_MAX_OP_ARGS];
341} TCGTargetOpDef;
342
343extern TCGOpDef tcg_op_defs[];
344
345void tcg_target_init(TCGContext *s);
b03cce8e 346void tcg_target_qemu_prologue(TCGContext *s);
c896fe29
FB
347
348#define tcg_abort() \
349do {\
350 fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
351 abort();\
352} while (0)
353
354void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
355
ac56dd48
PB
356void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
357 unsigned int nb_rets, const TCGv *rets,
358 unsigned int nb_params, const TCGv *args1);
359void tcg_gen_shifti_i64(TCGv ret, TCGv arg1,
c896fe29
FB
360 int c, int right, int arith);
361
362/* only used for debugging purposes */
363void tcg_register_helper(void *func, const char *name);
364#define TCG_HELPER(func) tcg_register_helper(func, #func)
365const char *tcg_helper_get_name(TCGContext *s, void *func);
366void tcg_dump_ops(TCGContext *s, FILE *outfile);
367
368void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
ac56dd48
PB
369TCGv tcg_const_i32(int32_t val);
370TCGv tcg_const_i64(int64_t val);
c896fe29
FB
371
372#if TCG_TARGET_REG_BITS == 32
373#define tcg_const_ptr tcg_const_i32
374#define tcg_add_ptr tcg_add_i32
375#define tcg_sub_ptr tcg_sub_i32
376#else
377#define tcg_const_ptr tcg_const_i64
378#define tcg_add_ptr tcg_add_i64
379#define tcg_sub_ptr tcg_sub_i64
380#endif
381
382void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type,
383 int label_index, long addend);
384void tcg_reg_alloc_start(TCGContext *s);
385void tcg_reg_alloc_bb_end(TCGContext *s);
386void tcg_liveness_analysis(TCGContext *s);
387const TCGArg *tcg_gen_code_op(TCGContext *s, int opc, const TCGArg *args1,
388 unsigned int dead_iargs);
389
390const TCGArg *dyngen_op(TCGContext *s, int opc, const TCGArg *opparam_ptr);
391
392/* tcg-runtime.c */
393int64_t tcg_helper_shl_i64(int64_t arg1, int64_t arg2);
394int64_t tcg_helper_shr_i64(int64_t arg1, int64_t arg2);
395int64_t tcg_helper_sar_i64(int64_t arg1, int64_t arg2);
396int64_t tcg_helper_div_i64(int64_t arg1, int64_t arg2);
397int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2);
398uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2);
399uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2);
b03cce8e
FB
400
401extern uint8_t code_gen_prologue[];
402#define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)