Statistics
| Branch: | Revision:

root / tcg / tcg.h @ 623e265c

History | View | Annotate | Download (10.2 kB)

1 c896fe29 bellard
/*
2 c896fe29 bellard
 * Tiny Code Generator for QEMU
3 c896fe29 bellard
 *
4 c896fe29 bellard
 * Copyright (c) 2008 Fabrice Bellard
5 c896fe29 bellard
 *
6 c896fe29 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 c896fe29 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 c896fe29 bellard
 * in the Software without restriction, including without limitation the rights
9 c896fe29 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 c896fe29 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 c896fe29 bellard
 * furnished to do so, subject to the following conditions:
12 c896fe29 bellard
 *
13 c896fe29 bellard
 * The above copyright notice and this permission notice shall be included in
14 c896fe29 bellard
 * all copies or substantial portions of the Software.
15 c896fe29 bellard
 *
16 c896fe29 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 c896fe29 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 c896fe29 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 c896fe29 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 c896fe29 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 c896fe29 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 c896fe29 bellard
 * THE SOFTWARE.
23 c896fe29 bellard
 */
24 c896fe29 bellard
#include "tcg-target.h"
25 c896fe29 bellard
26 c896fe29 bellard
#if TCG_TARGET_REG_BITS == 32
27 c896fe29 bellard
typedef int32_t tcg_target_long;
28 c896fe29 bellard
typedef uint32_t tcg_target_ulong;
29 c896fe29 bellard
#define TCG_PRIlx PRIx32
30 c896fe29 bellard
#define TCG_PRIld PRId32
31 c896fe29 bellard
#elif TCG_TARGET_REG_BITS == 64
32 c896fe29 bellard
typedef int64_t tcg_target_long;
33 c896fe29 bellard
typedef uint64_t tcg_target_ulong;
34 c896fe29 bellard
#define TCG_PRIlx PRIx64
35 c896fe29 bellard
#define TCG_PRIld PRId64
36 c896fe29 bellard
#else
37 c896fe29 bellard
#error unsupported
38 c896fe29 bellard
#endif
39 c896fe29 bellard
40 c896fe29 bellard
#if TCG_TARGET_NB_REGS <= 32
41 c896fe29 bellard
typedef uint32_t TCGRegSet;
42 c896fe29 bellard
#elif TCG_TARGET_NB_REGS <= 64
43 c896fe29 bellard
typedef uint64_t TCGRegSet;
44 c896fe29 bellard
#else
45 c896fe29 bellard
#error unsupported
46 c896fe29 bellard
#endif
47 c896fe29 bellard
48 c896fe29 bellard
enum {
49 c896fe29 bellard
#define DEF(s, n, copy_size) INDEX_op_ ## s,
50 c896fe29 bellard
#include "tcg-opc.h"
51 c896fe29 bellard
#undef DEF
52 c896fe29 bellard
    NB_OPS,
53 c896fe29 bellard
};
54 c896fe29 bellard
55 c896fe29 bellard
#define tcg_regset_clear(d) (d) = 0
56 c896fe29 bellard
#define tcg_regset_set(d, s) (d) = (s)
57 c896fe29 bellard
#define tcg_regset_set32(d, reg, val32) (d) |= (val32) << (reg)
58 c896fe29 bellard
#define tcg_regset_set_reg(d, r) (d) |= 1 << (r)
59 c896fe29 bellard
#define tcg_regset_reset_reg(d, r) (d) &= ~(1 << (r))
60 c896fe29 bellard
#define tcg_regset_test_reg(d, r) (((d) >> (r)) & 1)
61 c896fe29 bellard
#define tcg_regset_or(d, a, b) (d) = (a) | (b)
62 c896fe29 bellard
#define tcg_regset_and(d, a, b) (d) = (a) & (b)
63 c896fe29 bellard
#define tcg_regset_andnot(d, a, b) (d) = (a) & ~(b)
64 c896fe29 bellard
#define tcg_regset_not(d, a) (d) = ~(a)
65 c896fe29 bellard
66 c896fe29 bellard
typedef struct TCGRelocation {
67 c896fe29 bellard
    struct TCGRelocation *next;
68 c896fe29 bellard
    int type;
69 c896fe29 bellard
    uint8_t *ptr;
70 c896fe29 bellard
    tcg_target_long addend;
71 c896fe29 bellard
} TCGRelocation; 
72 c896fe29 bellard
73 c896fe29 bellard
typedef struct TCGLabel {
74 c896fe29 bellard
    int has_value;
75 c896fe29 bellard
    union {
76 c896fe29 bellard
        tcg_target_ulong value;
77 c896fe29 bellard
        TCGRelocation *first_reloc;
78 c896fe29 bellard
    } u;
79 c896fe29 bellard
} TCGLabel;
80 c896fe29 bellard
81 c896fe29 bellard
typedef struct TCGPool {
82 c896fe29 bellard
    struct TCGPool *next;
83 c896fe29 bellard
    int size;
84 c896fe29 bellard
    uint8_t data[0];
85 c896fe29 bellard
} TCGPool;
86 c896fe29 bellard
87 c896fe29 bellard
#define TCG_POOL_CHUNK_SIZE 32768
88 c896fe29 bellard
89 c896fe29 bellard
#define TCG_MAX_LABELS 512
90 c896fe29 bellard
91 c896fe29 bellard
#define TCG_MAX_TEMPS 256
92 c896fe29 bellard
93 c896fe29 bellard
typedef int TCGType;
94 c896fe29 bellard
95 c896fe29 bellard
#define TCG_TYPE_I32 0
96 c896fe29 bellard
#define TCG_TYPE_I64 1
97 c896fe29 bellard
98 c896fe29 bellard
#if TCG_TARGET_REG_BITS == 32
99 c896fe29 bellard
#define TCG_TYPE_PTR TCG_TYPE_I32
100 c896fe29 bellard
#else
101 c896fe29 bellard
#define TCG_TYPE_PTR TCG_TYPE_I64
102 c896fe29 bellard
#endif
103 c896fe29 bellard
104 c896fe29 bellard
typedef tcg_target_ulong TCGArg;
105 c896fe29 bellard
106 ac56dd48 pbrook
/* Define a type and accessor macros for varables.  Using a struct is
107 ac56dd48 pbrook
   nice because it gives some level of type safely.  Ideally the compiler
108 ac56dd48 pbrook
   be able to see through all this.  However in practice this is not true,
109 ac56dd48 pbrook
   expecially on targets with braindamaged ABIs (e.g. i386).
110 ac56dd48 pbrook
   We use plain int by default to avoid this runtime overhead.
111 ac56dd48 pbrook
   Users of tcg_gen_* don't need to know about any of this, and should
112 ac56dd48 pbrook
   treat TCGv as an opaque type.  */
113 ac56dd48 pbrook
114 ac56dd48 pbrook
//#define DEBUG_TCGV 1
115 ac56dd48 pbrook
116 ac56dd48 pbrook
#ifdef DEBUG_TCGV
117 ac56dd48 pbrook
118 ac56dd48 pbrook
typedef struct
119 ac56dd48 pbrook
{
120 ac56dd48 pbrook
    int n;
121 ac56dd48 pbrook
} TCGv;
122 ac56dd48 pbrook
123 ac56dd48 pbrook
#define MAKE_TCGV(i) __extension__ \
124 ac56dd48 pbrook
  ({ TCGv make_tcgv_tmp = {i}; make_tcgv_tmp;})
125 ac56dd48 pbrook
#define GET_TCGV(t) ((t).n)
126 ac56dd48 pbrook
#if TCG_TARGET_REG_BITS == 32
127 ac56dd48 pbrook
#define TCGV_HIGH(t) MAKE_TCGV(GET_TCGV(t) + 1)
128 ac56dd48 pbrook
#endif
129 ac56dd48 pbrook
130 ac56dd48 pbrook
#else /* !DEBUG_TCGV */
131 ac56dd48 pbrook
132 ac56dd48 pbrook
typedef int TCGv;
133 ac56dd48 pbrook
#define MAKE_TCGV(x) (x)
134 ac56dd48 pbrook
#define GET_TCGV(t) (t)
135 ac56dd48 pbrook
#if TCG_TARGET_REG_BITS == 32
136 ac56dd48 pbrook
#define TCGV_HIGH(t) ((t) + 1)
137 ac56dd48 pbrook
#endif
138 ac56dd48 pbrook
139 ac56dd48 pbrook
#endif /* DEBUG_TCGV */
140 ac56dd48 pbrook
141 c896fe29 bellard
/* call flags */
142 c896fe29 bellard
#define TCG_CALL_TYPE_MASK      0x000f
143 c896fe29 bellard
#define TCG_CALL_TYPE_STD       0x0000 /* standard C call */
144 c896fe29 bellard
#define TCG_CALL_TYPE_REGPARM_1 0x0001 /* i386 style regparm call (1 reg) */
145 c896fe29 bellard
#define TCG_CALL_TYPE_REGPARM_2 0x0002 /* i386 style regparm call (2 regs) */
146 c896fe29 bellard
#define TCG_CALL_TYPE_REGPARM   0x0003 /* i386 style regparm call (3 regs) */
147 c896fe29 bellard
148 c896fe29 bellard
typedef enum {
149 c896fe29 bellard
    TCG_COND_EQ,
150 c896fe29 bellard
    TCG_COND_NE,
151 c896fe29 bellard
    TCG_COND_LT,
152 c896fe29 bellard
    TCG_COND_GE,
153 c896fe29 bellard
    TCG_COND_LE,
154 c896fe29 bellard
    TCG_COND_GT,
155 c896fe29 bellard
    /* unsigned */
156 c896fe29 bellard
    TCG_COND_LTU,
157 c896fe29 bellard
    TCG_COND_GEU,
158 c896fe29 bellard
    TCG_COND_LEU,
159 c896fe29 bellard
    TCG_COND_GTU,
160 c896fe29 bellard
} TCGCond;
161 c896fe29 bellard
162 c896fe29 bellard
#define TEMP_VAL_DEAD  0
163 c896fe29 bellard
#define TEMP_VAL_REG   1
164 c896fe29 bellard
#define TEMP_VAL_MEM   2
165 c896fe29 bellard
#define TEMP_VAL_CONST 3
166 c896fe29 bellard
167 c896fe29 bellard
/* XXX: optimize memory layout */
168 c896fe29 bellard
typedef struct TCGTemp {
169 c896fe29 bellard
    TCGType base_type;
170 c896fe29 bellard
    TCGType type;
171 c896fe29 bellard
    int val_type;
172 c896fe29 bellard
    int reg;
173 c896fe29 bellard
    tcg_target_long val;
174 c896fe29 bellard
    int mem_reg;
175 c896fe29 bellard
    tcg_target_long mem_offset;
176 c896fe29 bellard
    unsigned int fixed_reg:1;
177 c896fe29 bellard
    unsigned int mem_coherent:1;
178 c896fe29 bellard
    unsigned int mem_allocated:1;
179 c896fe29 bellard
    const char *name;
180 c896fe29 bellard
} TCGTemp;
181 c896fe29 bellard
182 c896fe29 bellard
typedef struct TCGHelperInfo {
183 c896fe29 bellard
    void *func;
184 c896fe29 bellard
    const char *name;
185 c896fe29 bellard
} TCGHelperInfo;
186 c896fe29 bellard
187 c896fe29 bellard
typedef struct TCGContext TCGContext;
188 c896fe29 bellard
189 c896fe29 bellard
typedef void TCGMacroFunc(TCGContext *s, int macro_id, const int *dead_args);
190 c896fe29 bellard
191 c896fe29 bellard
struct TCGContext {
192 c896fe29 bellard
    uint8_t *pool_cur, *pool_end;
193 c896fe29 bellard
    TCGPool *pool_first, *pool_current;
194 c896fe29 bellard
    TCGLabel *labels;
195 c896fe29 bellard
    int nb_labels;
196 c896fe29 bellard
    TCGTemp *temps; /* globals first, temps after */
197 c896fe29 bellard
    int nb_globals;
198 c896fe29 bellard
    int nb_temps;
199 c896fe29 bellard
    /* constant indexes (end of temp array) */
200 c896fe29 bellard
    int const_start;
201 c896fe29 bellard
    int const_end;
202 c896fe29 bellard
203 c896fe29 bellard
    /* goto_tb support */
204 c896fe29 bellard
    uint8_t *code_buf;
205 c896fe29 bellard
    unsigned long *tb_next;
206 c896fe29 bellard
    uint16_t *tb_next_offset;
207 c896fe29 bellard
    uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
208 c896fe29 bellard
209 c896fe29 bellard
    uint16_t *op_dead_iargs; /* for each operation, each bit tells if the
210 c896fe29 bellard
                                corresponding input argument is dead */
211 c896fe29 bellard
    /* tells in which temporary a given register is. It does not take
212 c896fe29 bellard
       into account fixed registers */
213 c896fe29 bellard
    int reg_to_temp[TCG_TARGET_NB_REGS];
214 c896fe29 bellard
    TCGRegSet reserved_regs;
215 c896fe29 bellard
    tcg_target_long current_frame_offset;
216 c896fe29 bellard
    tcg_target_long frame_start;
217 c896fe29 bellard
    tcg_target_long frame_end;
218 c896fe29 bellard
    int frame_reg;
219 c896fe29 bellard
220 c896fe29 bellard
    uint8_t *code_ptr;
221 c896fe29 bellard
    TCGTemp static_temps[TCG_MAX_TEMPS];
222 c896fe29 bellard
223 c896fe29 bellard
    TCGMacroFunc *macro_func;
224 c896fe29 bellard
    TCGHelperInfo *helpers;
225 c896fe29 bellard
    int nb_helpers;
226 c896fe29 bellard
    int allocated_helpers;
227 c896fe29 bellard
};
228 c896fe29 bellard
229 c896fe29 bellard
extern TCGContext tcg_ctx;
230 c896fe29 bellard
extern uint16_t *gen_opc_ptr;
231 c896fe29 bellard
extern TCGArg *gen_opparam_ptr;
232 c896fe29 bellard
extern uint16_t gen_opc_buf[];
233 c896fe29 bellard
extern TCGArg gen_opparam_buf[];
234 c896fe29 bellard
235 c896fe29 bellard
/* pool based memory allocation */
236 c896fe29 bellard
237 c896fe29 bellard
void *tcg_malloc_internal(TCGContext *s, int size);
238 c896fe29 bellard
void tcg_pool_reset(TCGContext *s);
239 c896fe29 bellard
void tcg_pool_delete(TCGContext *s);
240 c896fe29 bellard
241 c896fe29 bellard
static inline void *tcg_malloc(int size)
242 c896fe29 bellard
{
243 c896fe29 bellard
    TCGContext *s = &tcg_ctx;
244 c896fe29 bellard
    uint8_t *ptr, *ptr_end;
245 c896fe29 bellard
    size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
246 c896fe29 bellard
    ptr = s->pool_cur;
247 c896fe29 bellard
    ptr_end = ptr + size;
248 c896fe29 bellard
    if (unlikely(ptr_end > s->pool_end)) {
249 c896fe29 bellard
        return tcg_malloc_internal(&tcg_ctx, size);
250 c896fe29 bellard
    } else {
251 c896fe29 bellard
        s->pool_cur = ptr_end;
252 c896fe29 bellard
        return ptr;
253 c896fe29 bellard
    }
254 c896fe29 bellard
}
255 c896fe29 bellard
256 c896fe29 bellard
void tcg_context_init(TCGContext *s);
257 c896fe29 bellard
void tcg_func_start(TCGContext *s);
258 c896fe29 bellard
259 c896fe29 bellard
int dyngen_code(TCGContext *s, uint8_t *gen_code_buf);
260 623e265c pbrook
int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
261 c896fe29 bellard
262 c896fe29 bellard
void tcg_set_frame(TCGContext *s, int reg,
263 c896fe29 bellard
                   tcg_target_long start, tcg_target_long size);
264 c896fe29 bellard
void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func);
265 ac56dd48 pbrook
TCGv tcg_global_reg_new(TCGType type, int reg, const char *name);
266 ac56dd48 pbrook
TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
267 ac56dd48 pbrook
                        const char *name);
268 ac56dd48 pbrook
TCGv tcg_temp_new(TCGType type);
269 ac56dd48 pbrook
char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg);
270 c896fe29 bellard
271 c896fe29 bellard
#define TCG_CT_ALIAS  0x80
272 c896fe29 bellard
#define TCG_CT_IALIAS 0x40
273 c896fe29 bellard
#define TCG_CT_REG    0x01
274 c896fe29 bellard
#define TCG_CT_CONST  0x02 /* any constant of register size */
275 c896fe29 bellard
276 c896fe29 bellard
typedef struct TCGArgConstraint {
277 5ff9d6a4 bellard
    uint16_t ct;
278 5ff9d6a4 bellard
    uint8_t alias_index;
279 c896fe29 bellard
    union {
280 c896fe29 bellard
        TCGRegSet regs;
281 c896fe29 bellard
    } u;
282 c896fe29 bellard
} TCGArgConstraint;
283 c896fe29 bellard
284 c896fe29 bellard
#define TCG_MAX_OP_ARGS 16
285 c896fe29 bellard
286 c896fe29 bellard
#define TCG_OPF_BB_END     0x01 /* instruction defines the end of a basic
287 c896fe29 bellard
                                   block */
288 c896fe29 bellard
#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers */
289 5ff9d6a4 bellard
#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects */
290 c896fe29 bellard
291 c896fe29 bellard
typedef struct TCGOpDef {
292 c896fe29 bellard
    const char *name;
293 c896fe29 bellard
    uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
294 c896fe29 bellard
    uint8_t flags;
295 c896fe29 bellard
    uint16_t copy_size;
296 c896fe29 bellard
    TCGArgConstraint *args_ct;
297 c896fe29 bellard
    int *sorted_args;
298 c896fe29 bellard
} TCGOpDef;
299 c896fe29 bellard
        
300 c896fe29 bellard
typedef struct TCGTargetOpDef {
301 c896fe29 bellard
    int op;
302 c896fe29 bellard
    const char *args_ct_str[TCG_MAX_OP_ARGS];
303 c896fe29 bellard
} TCGTargetOpDef;
304 c896fe29 bellard
305 c896fe29 bellard
extern TCGOpDef tcg_op_defs[];
306 c896fe29 bellard
307 c896fe29 bellard
void tcg_target_init(TCGContext *s);
308 c896fe29 bellard
309 c896fe29 bellard
#define tcg_abort() \
310 c896fe29 bellard
do {\
311 c896fe29 bellard
    fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
312 c896fe29 bellard
    abort();\
313 c896fe29 bellard
} while (0)
314 c896fe29 bellard
315 c896fe29 bellard
void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
316 c896fe29 bellard
317 ac56dd48 pbrook
void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
318 ac56dd48 pbrook
                  unsigned int nb_rets, const TCGv *rets,
319 ac56dd48 pbrook
                  unsigned int nb_params, const TCGv *args1);
320 ac56dd48 pbrook
void tcg_gen_shifti_i64(TCGv ret, TCGv arg1, 
321 c896fe29 bellard
                        int c, int right, int arith);
322 c896fe29 bellard
323 c896fe29 bellard
/* only used for debugging purposes */
324 c896fe29 bellard
void tcg_register_helper(void *func, const char *name);
325 c896fe29 bellard
#define TCG_HELPER(func) tcg_register_helper(func, #func)
326 c896fe29 bellard
const char *tcg_helper_get_name(TCGContext *s, void *func);
327 c896fe29 bellard
void tcg_dump_ops(TCGContext *s, FILE *outfile);
328 c896fe29 bellard
329 c896fe29 bellard
void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
330 ac56dd48 pbrook
TCGv tcg_const_i32(int32_t val);
331 ac56dd48 pbrook
TCGv tcg_const_i64(int64_t val);
332 c896fe29 bellard
333 c896fe29 bellard
#if TCG_TARGET_REG_BITS == 32
334 c896fe29 bellard
#define tcg_const_ptr tcg_const_i32
335 c896fe29 bellard
#define tcg_add_ptr tcg_add_i32
336 c896fe29 bellard
#define tcg_sub_ptr tcg_sub_i32
337 c896fe29 bellard
#else
338 c896fe29 bellard
#define tcg_const_ptr tcg_const_i64
339 c896fe29 bellard
#define tcg_add_ptr tcg_add_i64
340 c896fe29 bellard
#define tcg_sub_ptr tcg_sub_i64
341 c896fe29 bellard
#endif
342 c896fe29 bellard
343 c896fe29 bellard
void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, 
344 c896fe29 bellard
                   int label_index, long addend);
345 c896fe29 bellard
void tcg_reg_alloc_start(TCGContext *s);
346 c896fe29 bellard
void tcg_reg_alloc_bb_end(TCGContext *s);
347 c896fe29 bellard
void tcg_liveness_analysis(TCGContext *s);
348 c896fe29 bellard
const TCGArg *tcg_gen_code_op(TCGContext *s, int opc, const TCGArg *args1,
349 c896fe29 bellard
                              unsigned int dead_iargs);
350 c896fe29 bellard
351 c896fe29 bellard
const TCGArg *dyngen_op(TCGContext *s, int opc, const TCGArg *opparam_ptr);
352 c896fe29 bellard
353 c896fe29 bellard
/* tcg-runtime.c */
354 c896fe29 bellard
int64_t tcg_helper_shl_i64(int64_t arg1, int64_t arg2);
355 c896fe29 bellard
int64_t tcg_helper_shr_i64(int64_t arg1, int64_t arg2);
356 c896fe29 bellard
int64_t tcg_helper_sar_i64(int64_t arg1, int64_t arg2);
357 c896fe29 bellard
int64_t tcg_helper_div_i64(int64_t arg1, int64_t arg2);
358 c896fe29 bellard
int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2);
359 c896fe29 bellard
uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2);
360 c896fe29 bellard
uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2);