Statistics
| Branch: | Revision:

root / tcg / tcg.h @ 6a8d7b76

History | View | Annotate | Download (10.9 kB)

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
27
typedef int32_t tcg_target_long;
28
typedef uint32_t tcg_target_ulong;
29
#define TCG_PRIlx PRIx32
30
#define TCG_PRIld PRId32
31
#elif TCG_TARGET_REG_BITS == 64
32
typedef int64_t tcg_target_long;
33
typedef 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
41
typedef uint32_t TCGRegSet;
42
#elif TCG_TARGET_NB_REGS <= 64
43
typedef uint64_t TCGRegSet;
44
#else
45
#error unsupported
46
#endif
47

    
48
enum {
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

    
66
typedef struct TCGRelocation {
67
    struct TCGRelocation *next;
68
    int type;
69
    uint8_t *ptr;
70
    tcg_target_long addend;
71
} TCGRelocation; 
72

    
73
typedef struct TCGLabel {
74
    int has_value;
75
    union {
76
        tcg_target_ulong value;
77
        TCGRelocation *first_reloc;
78
    } u;
79
} TCGLabel;
80

    
81
typedef struct TCGPool {
82
    struct TCGPool *next;
83
    int size;
84
    uint8_t data[0];
85
} TCGPool;
86

    
87
#define TCG_POOL_CHUNK_SIZE 32768
88

    
89
#define TCG_MAX_LABELS 512
90

    
91
#define TCG_MAX_TEMPS 512
92

    
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

    
97
typedef 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

    
108
typedef tcg_target_ulong TCGArg;
109

    
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

    
122
typedef 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

    
136
typedef 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

    
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) */
151

    
152
typedef enum {
153
    TCG_COND_EQ,
154
    TCG_COND_NE,
155
    TCG_COND_LT,
156
    TCG_COND_GE,
157
    TCG_COND_LE,
158
    TCG_COND_GT,
159
    /* unsigned */
160
    TCG_COND_LTU,
161
    TCG_COND_GEU,
162
    TCG_COND_LEU,
163
    TCG_COND_GTU,
164
} TCGCond;
165

    
166
#define TEMP_VAL_DEAD  0
167
#define TEMP_VAL_REG   1
168
#define TEMP_VAL_MEM   2
169
#define TEMP_VAL_CONST 3
170

    
171
/* XXX: optimize memory layout */
172
typedef struct TCGTemp {
173
    TCGType base_type;
174
    TCGType type;
175
    int val_type;
176
    int reg;
177
    tcg_target_long val;
178
    int mem_reg;
179
    tcg_target_long mem_offset;
180
    unsigned int fixed_reg:1;
181
    unsigned int mem_coherent:1;
182
    unsigned int mem_allocated:1;
183
    const char *name;
184
} TCGTemp;
185

    
186
typedef struct TCGHelperInfo {
187
    void *func;
188
    const char *name;
189
} TCGHelperInfo;
190

    
191
typedef struct TCGContext TCGContext;
192

    
193
typedef void TCGMacroFunc(TCGContext *s, int macro_id, const int *dead_args);
194

    
195
struct TCGContext {
196
    uint8_t *pool_cur, *pool_end;
197
    TCGPool *pool_first, *pool_current;
198
    TCGLabel *labels;
199
    int nb_labels;
200
    TCGTemp *temps; /* globals first, temps after */
201
    int nb_globals;
202
    int nb_temps;
203
    /* constant indexes (end of temp array) */
204
    int const_start;
205
    int const_end;
206

    
207
    /* goto_tb support */
208
    uint8_t *code_buf;
209
    unsigned long *tb_next;
210
    uint16_t *tb_next_offset;
211
    uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
212

    
213
    uint16_t *op_dead_iargs; /* for each operation, each bit tells if the
214
                                corresponding input argument is dead */
215
    /* tells in which temporary a given register is. It does not take
216
       into account fixed registers */
217
    int reg_to_temp[TCG_TARGET_NB_REGS];
218
    TCGRegSet reserved_regs;
219
    tcg_target_long current_frame_offset;
220
    tcg_target_long frame_start;
221
    tcg_target_long frame_end;
222
    int frame_reg;
223

    
224
    uint8_t *code_ptr;
225
    TCGTemp static_temps[TCG_MAX_TEMPS];
226

    
227
    TCGMacroFunc *macro_func;
228
    TCGHelperInfo *helpers;
229
    int nb_helpers;
230
    int allocated_helpers;
231
};
232

    
233
extern TCGContext tcg_ctx;
234
extern uint16_t *gen_opc_ptr;
235
extern TCGArg *gen_opparam_ptr;
236
extern uint16_t gen_opc_buf[];
237
extern TCGArg gen_opparam_buf[];
238

    
239
/* pool based memory allocation */
240

    
241
void *tcg_malloc_internal(TCGContext *s, int size);
242
void tcg_pool_reset(TCGContext *s);
243
void tcg_pool_delete(TCGContext *s);
244

    
245
static inline void *tcg_malloc(int size)
246
{
247
    TCGContext *s = &tcg_ctx;
248
    uint8_t *ptr, *ptr_end;
249
    size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
250
    ptr = s->pool_cur;
251
    ptr_end = ptr + size;
252
    if (unlikely(ptr_end > s->pool_end)) {
253
        return tcg_malloc_internal(&tcg_ctx, size);
254
    } else {
255
        s->pool_cur = ptr_end;
256
        return ptr;
257
    }
258
}
259

    
260
void tcg_context_init(TCGContext *s);
261
void tcg_func_start(TCGContext *s);
262

    
263
int dyngen_code(TCGContext *s, uint8_t *gen_code_buf);
264
int dyngen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
265

    
266
void tcg_set_frame(TCGContext *s, int reg,
267
                   tcg_target_long start, tcg_target_long size);
268
void tcg_set_macro_func(TCGContext *s, TCGMacroFunc *func);
269
TCGv tcg_global_reg_new(TCGType type, int reg, const char *name);
270
TCGv tcg_global_reg2_new_hack(TCGType type, int reg1, int reg2, 
271
                              const char *name);
272
TCGv tcg_global_mem_new(TCGType type, int reg, tcg_target_long offset,
273
                        const char *name);
274
TCGv tcg_temp_new(TCGType type);
275
char *tcg_get_arg_str(TCGContext *s, char *buf, int buf_size, TCGv arg);
276

    
277
#define TCG_CT_ALIAS  0x80
278
#define TCG_CT_IALIAS 0x40
279
#define TCG_CT_REG    0x01
280
#define TCG_CT_CONST  0x02 /* any constant of register size */
281

    
282
typedef struct TCGArgConstraint {
283
    uint16_t ct;
284
    uint8_t alias_index;
285
    union {
286
        TCGRegSet regs;
287
    } u;
288
} TCGArgConstraint;
289

    
290
#define TCG_MAX_OP_ARGS 16
291

    
292
#define TCG_OPF_BB_END     0x01 /* instruction defines the end of a basic
293
                                   block */
294
#define TCG_OPF_CALL_CLOBBER 0x02 /* instruction clobbers call registers 
295
                                   and potentially update globals. */
296
#define TCG_OPF_SIDE_EFFECTS 0x04 /* instruction has side effects : it
297
                                     cannot be removed if its output
298
                                     are not used */
299

    
300
typedef struct TCGOpDef {
301
    const char *name;
302
    uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
303
    uint8_t flags;
304
    uint16_t copy_size;
305
    TCGArgConstraint *args_ct;
306
    int *sorted_args;
307
} TCGOpDef;
308
        
309
typedef struct TCGTargetOpDef {
310
    int op;
311
    const char *args_ct_str[TCG_MAX_OP_ARGS];
312
} TCGTargetOpDef;
313

    
314
extern TCGOpDef tcg_op_defs[];
315

    
316
void tcg_target_init(TCGContext *s);
317
void tcg_target_qemu_prologue(TCGContext *s);
318

    
319
#define tcg_abort() \
320
do {\
321
    fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
322
    abort();\
323
} while (0)
324

    
325
void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
326

    
327
void tcg_gen_call(TCGContext *s, TCGv func, unsigned int flags,
328
                  unsigned int nb_rets, const TCGv *rets,
329
                  unsigned int nb_params, const TCGv *args1);
330
void tcg_gen_shifti_i64(TCGv ret, TCGv arg1, 
331
                        int c, int right, int arith);
332

    
333
/* only used for debugging purposes */
334
void tcg_register_helper(void *func, const char *name);
335
#define TCG_HELPER(func) tcg_register_helper(func, #func)
336
const char *tcg_helper_get_name(TCGContext *s, void *func);
337
void tcg_dump_ops(TCGContext *s, FILE *outfile);
338

    
339
void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
340
TCGv tcg_const_i32(int32_t val);
341
TCGv tcg_const_i64(int64_t val);
342

    
343
#if TCG_TARGET_REG_BITS == 32
344
#define tcg_const_ptr tcg_const_i32
345
#define tcg_add_ptr tcg_add_i32
346
#define tcg_sub_ptr tcg_sub_i32
347
#else
348
#define tcg_const_ptr tcg_const_i64
349
#define tcg_add_ptr tcg_add_i64
350
#define tcg_sub_ptr tcg_sub_i64
351
#endif
352

    
353
void tcg_out_reloc(TCGContext *s, uint8_t *code_ptr, int type, 
354
                   int label_index, long addend);
355
void tcg_reg_alloc_start(TCGContext *s);
356
void tcg_reg_alloc_bb_end(TCGContext *s);
357
void tcg_liveness_analysis(TCGContext *s);
358
const TCGArg *tcg_gen_code_op(TCGContext *s, int opc, const TCGArg *args1,
359
                              unsigned int dead_iargs);
360

    
361
const TCGArg *dyngen_op(TCGContext *s, int opc, const TCGArg *opparam_ptr);
362

    
363
/* tcg-runtime.c */
364
int64_t tcg_helper_shl_i64(int64_t arg1, int64_t arg2);
365
int64_t tcg_helper_shr_i64(int64_t arg1, int64_t arg2);
366
int64_t tcg_helper_sar_i64(int64_t arg1, int64_t arg2);
367
int64_t tcg_helper_div_i64(int64_t arg1, int64_t arg2);
368
int64_t tcg_helper_rem_i64(int64_t arg1, int64_t arg2);
369
uint64_t tcg_helper_divu_i64(uint64_t arg1, uint64_t arg2);
370
uint64_t tcg_helper_remu_i64(uint64_t arg1, uint64_t arg2);
371

    
372
extern uint8_t code_gen_prologue[];
373
#define tcg_qemu_tb_exec(tb_ptr) ((long REGPARM (*)(void *))code_gen_prologue)(tb_ptr)