Statistics
| Branch: | Revision:

root / tcg / tcg.h @ a2d8f1be

History | View | Annotate | Download (17.8 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 f8393946 aurel32
#include "qemu-common.h"
25 817b838e Stefan Weil
26 817b838e Stefan Weil
/* Target word size (must be identical to pointer size). */
27 817b838e Stefan Weil
#if UINTPTR_MAX == UINT32_MAX
28 817b838e Stefan Weil
# define TCG_TARGET_REG_BITS 32
29 817b838e Stefan Weil
#elif UINTPTR_MAX == UINT64_MAX
30 817b838e Stefan Weil
# define TCG_TARGET_REG_BITS 64
31 817b838e Stefan Weil
#else
32 817b838e Stefan Weil
# error Unknown pointer size for tcg target
33 817b838e Stefan Weil
#endif
34 817b838e Stefan Weil
35 c896fe29 bellard
#include "tcg-target.h"
36 96e132e2 Blue Swirl
#include "tcg-runtime.h"
37 c896fe29 bellard
38 c896fe29 bellard
#if TCG_TARGET_REG_BITS == 32
39 c896fe29 bellard
typedef int32_t tcg_target_long;
40 c896fe29 bellard
typedef uint32_t tcg_target_ulong;
41 c896fe29 bellard
#define TCG_PRIlx PRIx32
42 c896fe29 bellard
#define TCG_PRIld PRId32
43 c896fe29 bellard
#elif TCG_TARGET_REG_BITS == 64
44 c896fe29 bellard
typedef int64_t tcg_target_long;
45 c896fe29 bellard
typedef uint64_t tcg_target_ulong;
46 c896fe29 bellard
#define TCG_PRIlx PRIx64
47 c896fe29 bellard
#define TCG_PRIld PRId64
48 c896fe29 bellard
#else
49 c896fe29 bellard
#error unsupported
50 c896fe29 bellard
#endif
51 c896fe29 bellard
52 c896fe29 bellard
#if TCG_TARGET_NB_REGS <= 32
53 c896fe29 bellard
typedef uint32_t TCGRegSet;
54 c896fe29 bellard
#elif TCG_TARGET_NB_REGS <= 64
55 c896fe29 bellard
typedef uint64_t TCGRegSet;
56 c896fe29 bellard
#else
57 c896fe29 bellard
#error unsupported
58 c896fe29 bellard
#endif
59 c896fe29 bellard
60 25c4d9cc Richard Henderson
/* Turn some undef macros into false macros.  */
61 25c4d9cc Richard Henderson
#if TCG_TARGET_REG_BITS == 32
62 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_div_i64          0
63 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_div2_i64         0
64 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_rot_i64          0
65 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_ext8s_i64        0
66 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_ext16s_i64       0
67 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_ext32s_i64       0
68 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_ext8u_i64        0
69 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_ext16u_i64       0
70 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_ext32u_i64       0
71 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_bswap16_i64      0
72 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_bswap32_i64      0
73 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_bswap64_i64      0
74 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_neg_i64          0
75 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_not_i64          0
76 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_andc_i64         0
77 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_orc_i64          0
78 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_eqv_i64          0
79 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_nand_i64         0
80 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_nor_i64          0
81 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_deposit_i64      0
82 25c4d9cc Richard Henderson
#endif
83 25c4d9cc Richard Henderson
84 a4773324 Jan Kiszka
#ifndef TCG_TARGET_deposit_i32_valid
85 a4773324 Jan Kiszka
#define TCG_TARGET_deposit_i32_valid(ofs, len) 1
86 a4773324 Jan Kiszka
#endif
87 a4773324 Jan Kiszka
#ifndef TCG_TARGET_deposit_i64_valid
88 a4773324 Jan Kiszka
#define TCG_TARGET_deposit_i64_valid(ofs, len) 1
89 a4773324 Jan Kiszka
#endif
90 a4773324 Jan Kiszka
91 25c4d9cc Richard Henderson
/* Only one of DIV or DIV2 should be defined.  */
92 25c4d9cc Richard Henderson
#if defined(TCG_TARGET_HAS_div_i32)
93 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_div2_i32         0
94 25c4d9cc Richard Henderson
#elif defined(TCG_TARGET_HAS_div2_i32)
95 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_div_i32          0
96 25c4d9cc Richard Henderson
#endif
97 25c4d9cc Richard Henderson
#if defined(TCG_TARGET_HAS_div_i64)
98 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_div2_i64         0
99 25c4d9cc Richard Henderson
#elif defined(TCG_TARGET_HAS_div2_i64)
100 25c4d9cc Richard Henderson
#define TCG_TARGET_HAS_div_i64          0
101 25c4d9cc Richard Henderson
#endif
102 25c4d9cc Richard Henderson
103 a9751609 Richard Henderson
typedef enum TCGOpcode {
104 c61aaf7a Aurelien Jarno
#define DEF(name, oargs, iargs, cargs, flags) INDEX_op_ ## name,
105 c896fe29 bellard
#include "tcg-opc.h"
106 c896fe29 bellard
#undef DEF
107 c896fe29 bellard
    NB_OPS,
108 a9751609 Richard Henderson
} TCGOpcode;
109 c896fe29 bellard
110 c896fe29 bellard
#define tcg_regset_clear(d) (d) = 0
111 c896fe29 bellard
#define tcg_regset_set(d, s) (d) = (s)
112 c896fe29 bellard
#define tcg_regset_set32(d, reg, val32) (d) |= (val32) << (reg)
113 7d301752 Aurelien Jarno
#define tcg_regset_set_reg(d, r) (d) |= 1L << (r)
114 7d301752 Aurelien Jarno
#define tcg_regset_reset_reg(d, r) (d) &= ~(1L << (r))
115 c896fe29 bellard
#define tcg_regset_test_reg(d, r) (((d) >> (r)) & 1)
116 c896fe29 bellard
#define tcg_regset_or(d, a, b) (d) = (a) | (b)
117 c896fe29 bellard
#define tcg_regset_and(d, a, b) (d) = (a) & (b)
118 c896fe29 bellard
#define tcg_regset_andnot(d, a, b) (d) = (a) & ~(b)
119 c896fe29 bellard
#define tcg_regset_not(d, a) (d) = ~(a)
120 c896fe29 bellard
121 c896fe29 bellard
typedef struct TCGRelocation {
122 c896fe29 bellard
    struct TCGRelocation *next;
123 c896fe29 bellard
    int type;
124 c896fe29 bellard
    uint8_t *ptr;
125 c896fe29 bellard
    tcg_target_long addend;
126 c896fe29 bellard
} TCGRelocation; 
127 c896fe29 bellard
128 c896fe29 bellard
typedef struct TCGLabel {
129 c44f945a blueswir1
    int has_value;
130 c896fe29 bellard
    union {
131 c896fe29 bellard
        tcg_target_ulong value;
132 c896fe29 bellard
        TCGRelocation *first_reloc;
133 c896fe29 bellard
    } u;
134 c896fe29 bellard
} TCGLabel;
135 c896fe29 bellard
136 c896fe29 bellard
typedef struct TCGPool {
137 c896fe29 bellard
    struct TCGPool *next;
138 c44f945a blueswir1
    int size;
139 c44f945a blueswir1
    uint8_t data[0] __attribute__ ((aligned));
140 c896fe29 bellard
} TCGPool;
141 c896fe29 bellard
142 c896fe29 bellard
#define TCG_POOL_CHUNK_SIZE 32768
143 c896fe29 bellard
144 c896fe29 bellard
#define TCG_MAX_LABELS 512
145 c896fe29 bellard
146 c4071c90 blueswir1
#define TCG_MAX_TEMPS 512
147 c896fe29 bellard
148 b03cce8e bellard
/* when the size of the arguments of a called function is smaller than
149 b03cce8e bellard
   this value, they are statically allocated in the TB stack frame */
150 b03cce8e bellard
#define TCG_STATIC_CALL_ARGS_SIZE 128
151 b03cce8e bellard
152 c02244a5 Richard Henderson
typedef enum TCGType {
153 c02244a5 Richard Henderson
    TCG_TYPE_I32,
154 c02244a5 Richard Henderson
    TCG_TYPE_I64,
155 c02244a5 Richard Henderson
    TCG_TYPE_COUNT, /* number of different types */
156 c896fe29 bellard
157 3b6dac34 Richard Henderson
    /* An alias for the size of the host register.  */
158 c896fe29 bellard
#if TCG_TARGET_REG_BITS == 32
159 3b6dac34 Richard Henderson
    TCG_TYPE_REG = TCG_TYPE_I32,
160 c02244a5 Richard Henderson
#else
161 3b6dac34 Richard Henderson
    TCG_TYPE_REG = TCG_TYPE_I64,
162 c02244a5 Richard Henderson
#endif
163 3b6dac34 Richard Henderson
164 3b6dac34 Richard Henderson
    /* An alias for the size of the native pointer.  We don't currently
165 3b6dac34 Richard Henderson
       support any hosts with 64-bit registers and 32-bit pointers.  */
166 3b6dac34 Richard Henderson
    TCG_TYPE_PTR = TCG_TYPE_REG,
167 3b6dac34 Richard Henderson
168 3b6dac34 Richard Henderson
    /* An alias for the size of the target "long", aka register.  */
169 c02244a5 Richard Henderson
#if TARGET_LONG_BITS == 64
170 c02244a5 Richard Henderson
    TCG_TYPE_TL = TCG_TYPE_I64,
171 c896fe29 bellard
#else
172 c02244a5 Richard Henderson
    TCG_TYPE_TL = TCG_TYPE_I32,
173 c896fe29 bellard
#endif
174 c02244a5 Richard Henderson
} TCGType;
175 c896fe29 bellard
176 c896fe29 bellard
typedef tcg_target_ulong TCGArg;
177 c896fe29 bellard
178 8ef935b2 Stefan Weil
/* Define a type and accessor macros for variables.  Using a struct is
179 ac56dd48 pbrook
   nice because it gives some level of type safely.  Ideally the compiler
180 ac56dd48 pbrook
   be able to see through all this.  However in practice this is not true,
181 9814dd27 Dong Xu Wang
   especially on targets with braindamaged ABIs (e.g. i386).
182 ac56dd48 pbrook
   We use plain int by default to avoid this runtime overhead.
183 ac56dd48 pbrook
   Users of tcg_gen_* don't need to know about any of this, and should
184 a7812ae4 pbrook
   treat TCGv as an opaque type.
185 06ea77bc Stefan Weil
   In addition we do typechecking for different types of variables.  TCGv_i32
186 a7812ae4 pbrook
   and TCGv_i64 are 32/64-bit variables respectively.  TCGv and TCGv_ptr
187 a7812ae4 pbrook
   are aliases for target_ulong and host pointer sized values respectively.
188 a7812ae4 pbrook
 */
189 ac56dd48 pbrook
190 092c73ee Juan Quintela
#ifdef CONFIG_DEBUG_TCG
191 f8393946 aurel32
#define DEBUG_TCGV 1
192 f8393946 aurel32
#endif
193 ac56dd48 pbrook
194 ac56dd48 pbrook
#ifdef DEBUG_TCGV
195 ac56dd48 pbrook
196 ac56dd48 pbrook
typedef struct
197 ac56dd48 pbrook
{
198 a810a2de blueswir1
    int i32;
199 a7812ae4 pbrook
} TCGv_i32;
200 ac56dd48 pbrook
201 a7812ae4 pbrook
typedef struct
202 a7812ae4 pbrook
{
203 a810a2de blueswir1
    int i64;
204 a7812ae4 pbrook
} TCGv_i64;
205 a7812ae4 pbrook
206 ebecf363 Peter Maydell
typedef struct {
207 ebecf363 Peter Maydell
    int iptr;
208 ebecf363 Peter Maydell
} TCGv_ptr;
209 ebecf363 Peter Maydell
210 a7812ae4 pbrook
#define MAKE_TCGV_I32(i) __extension__                  \
211 a7812ae4 pbrook
    ({ TCGv_i32 make_tcgv_tmp = {i}; make_tcgv_tmp;})
212 a7812ae4 pbrook
#define MAKE_TCGV_I64(i) __extension__                  \
213 a7812ae4 pbrook
    ({ TCGv_i64 make_tcgv_tmp = {i}; make_tcgv_tmp;})
214 ebecf363 Peter Maydell
#define MAKE_TCGV_PTR(i) __extension__                  \
215 ebecf363 Peter Maydell
    ({ TCGv_ptr make_tcgv_tmp = {i}; make_tcgv_tmp; })
216 a810a2de blueswir1
#define GET_TCGV_I32(t) ((t).i32)
217 a810a2de blueswir1
#define GET_TCGV_I64(t) ((t).i64)
218 ebecf363 Peter Maydell
#define GET_TCGV_PTR(t) ((t).iptr)
219 ac56dd48 pbrook
#if TCG_TARGET_REG_BITS == 32
220 a7812ae4 pbrook
#define TCGV_LOW(t) MAKE_TCGV_I32(GET_TCGV_I64(t))
221 a7812ae4 pbrook
#define TCGV_HIGH(t) MAKE_TCGV_I32(GET_TCGV_I64(t) + 1)
222 ac56dd48 pbrook
#endif
223 ac56dd48 pbrook
224 ac56dd48 pbrook
#else /* !DEBUG_TCGV */
225 ac56dd48 pbrook
226 a7812ae4 pbrook
typedef int TCGv_i32;
227 a7812ae4 pbrook
typedef int TCGv_i64;
228 ebecf363 Peter Maydell
#if TCG_TARGET_REG_BITS == 32
229 ebecf363 Peter Maydell
#define TCGv_ptr TCGv_i32
230 ebecf363 Peter Maydell
#else
231 ebecf363 Peter Maydell
#define TCGv_ptr TCGv_i64
232 ebecf363 Peter Maydell
#endif
233 a7812ae4 pbrook
#define MAKE_TCGV_I32(x) (x)
234 a7812ae4 pbrook
#define MAKE_TCGV_I64(x) (x)
235 ebecf363 Peter Maydell
#define MAKE_TCGV_PTR(x) (x)
236 a7812ae4 pbrook
#define GET_TCGV_I32(t) (t)
237 a7812ae4 pbrook
#define GET_TCGV_I64(t) (t)
238 ebecf363 Peter Maydell
#define GET_TCGV_PTR(t) (t)
239 44e6acb0 aurel32
240 ac56dd48 pbrook
#if TCG_TARGET_REG_BITS == 32
241 a7812ae4 pbrook
#define TCGV_LOW(t) (t)
242 ac56dd48 pbrook
#define TCGV_HIGH(t) ((t) + 1)
243 ac56dd48 pbrook
#endif
244 ac56dd48 pbrook
245 ac56dd48 pbrook
#endif /* DEBUG_TCGV */
246 ac56dd48 pbrook
247 43e860ef aurel32
#define TCGV_EQUAL_I32(a, b) (GET_TCGV_I32(a) == GET_TCGV_I32(b))
248 43e860ef aurel32
#define TCGV_EQUAL_I64(a, b) (GET_TCGV_I64(a) == GET_TCGV_I64(b))
249 43e860ef aurel32
250 a50f5b91 pbrook
/* Dummy definition to avoid compiler warnings.  */
251 a7812ae4 pbrook
#define TCGV_UNUSED_I32(x) x = MAKE_TCGV_I32(-1)
252 a7812ae4 pbrook
#define TCGV_UNUSED_I64(x) x = MAKE_TCGV_I64(-1)
253 a50f5b91 pbrook
254 c896fe29 bellard
/* call flags */
255 c896fe29 bellard
#define TCG_CALL_TYPE_MASK      0x000f
256 c896fe29 bellard
#define TCG_CALL_TYPE_STD       0x0000 /* standard C call */
257 c896fe29 bellard
#define TCG_CALL_TYPE_REGPARM_1 0x0001 /* i386 style regparm call (1 reg) */
258 c896fe29 bellard
#define TCG_CALL_TYPE_REGPARM_2 0x0002 /* i386 style regparm call (2 regs) */
259 c896fe29 bellard
#define TCG_CALL_TYPE_REGPARM   0x0003 /* i386 style regparm call (3 regs) */
260 3e00b3f5 aurel32
/* A pure function only reads its arguments and TCG global variables
261 34d5a9ff aurel32
   and cannot raise exceptions. Hence a call to a pure function can be
262 c6e113f5 bellard
   safely suppressed if the return value is not used. */
263 c6e113f5 bellard
#define TCG_CALL_PURE           0x0010 
264 b9c18f56 aurel32
/* A const function only reads its arguments and does not use TCG
265 3e00b3f5 aurel32
   global variables. Hence a call to such a function does not
266 3e00b3f5 aurel32
   save TCG global variables back to their canonical location. */
267 b9c18f56 aurel32
#define TCG_CALL_CONST          0x0020
268 c896fe29 bellard
269 39cf05d3 bellard
/* used to align parameters */
270 a7812ae4 pbrook
#define TCG_CALL_DUMMY_TCGV     MAKE_TCGV_I32(-1)
271 39cf05d3 bellard
#define TCG_CALL_DUMMY_ARG      ((TCGArg)(-1))
272 39cf05d3 bellard
273 c896fe29 bellard
typedef enum {
274 c896fe29 bellard
    TCG_COND_EQ,
275 c896fe29 bellard
    TCG_COND_NE,
276 c896fe29 bellard
    TCG_COND_LT,
277 c896fe29 bellard
    TCG_COND_GE,
278 c896fe29 bellard
    TCG_COND_LE,
279 c896fe29 bellard
    TCG_COND_GT,
280 c896fe29 bellard
    /* unsigned */
281 c896fe29 bellard
    TCG_COND_LTU,
282 c896fe29 bellard
    TCG_COND_GEU,
283 c896fe29 bellard
    TCG_COND_LEU,
284 c896fe29 bellard
    TCG_COND_GTU,
285 c896fe29 bellard
} TCGCond;
286 c896fe29 bellard
287 1c086220 Richard Henderson
/* Invert the sense of the comparison.  */
288 401d466d Richard Henderson
static inline TCGCond tcg_invert_cond(TCGCond c)
289 401d466d Richard Henderson
{
290 401d466d Richard Henderson
    return (TCGCond)(c ^ 1);
291 401d466d Richard Henderson
}
292 401d466d Richard Henderson
293 1c086220 Richard Henderson
/* Swap the operands in a comparison.  */
294 1c086220 Richard Henderson
static inline TCGCond tcg_swap_cond(TCGCond c)
295 1c086220 Richard Henderson
{
296 1c086220 Richard Henderson
    int mask = (c < TCG_COND_LT ? 0 : c < TCG_COND_LTU ? 7 : 15);
297 1c086220 Richard Henderson
    return (TCGCond)(c ^ mask);
298 1c086220 Richard Henderson
}
299 1c086220 Richard Henderson
300 ff44c2f3 Richard Henderson
static inline TCGCond tcg_unsigned_cond(TCGCond c)
301 ff44c2f3 Richard Henderson
{
302 ff44c2f3 Richard Henderson
    return (c >= TCG_COND_LT && c <= TCG_COND_GT ? c + 4 : c);
303 ff44c2f3 Richard Henderson
}
304 ff44c2f3 Richard Henderson
305 c896fe29 bellard
#define TEMP_VAL_DEAD  0
306 c896fe29 bellard
#define TEMP_VAL_REG   1
307 c896fe29 bellard
#define TEMP_VAL_MEM   2
308 c896fe29 bellard
#define TEMP_VAL_CONST 3
309 c896fe29 bellard
310 c896fe29 bellard
/* XXX: optimize memory layout */
311 c896fe29 bellard
typedef struct TCGTemp {
312 c896fe29 bellard
    TCGType base_type;
313 c896fe29 bellard
    TCGType type;
314 c896fe29 bellard
    int val_type;
315 c896fe29 bellard
    int reg;
316 c896fe29 bellard
    tcg_target_long val;
317 c896fe29 bellard
    int mem_reg;
318 c896fe29 bellard
    tcg_target_long mem_offset;
319 c896fe29 bellard
    unsigned int fixed_reg:1;
320 c896fe29 bellard
    unsigned int mem_coherent:1;
321 c896fe29 bellard
    unsigned int mem_allocated:1;
322 5225d669 Stefan Weil
    unsigned int temp_local:1; /* If true, the temp is saved across
323 641d5fbe bellard
                                  basic blocks. Otherwise, it is not
324 5225d669 Stefan Weil
                                  preserved across basic blocks. */
325 e8996ee0 bellard
    unsigned int temp_allocated:1; /* never used for code gen */
326 e8996ee0 bellard
    /* index of next free temp of same base type, -1 if end */
327 e8996ee0 bellard
    int next_free_temp;
328 c896fe29 bellard
    const char *name;
329 c896fe29 bellard
} TCGTemp;
330 c896fe29 bellard
331 c896fe29 bellard
typedef struct TCGHelperInfo {
332 4dc81f28 bellard
    tcg_target_ulong func;
333 c896fe29 bellard
    const char *name;
334 c896fe29 bellard
} TCGHelperInfo;
335 c896fe29 bellard
336 c896fe29 bellard
typedef struct TCGContext TCGContext;
337 c896fe29 bellard
338 c896fe29 bellard
struct TCGContext {
339 c896fe29 bellard
    uint8_t *pool_cur, *pool_end;
340 c896fe29 bellard
    TCGPool *pool_first, *pool_current;
341 c896fe29 bellard
    TCGLabel *labels;
342 c896fe29 bellard
    int nb_labels;
343 c896fe29 bellard
    TCGTemp *temps; /* globals first, temps after */
344 c896fe29 bellard
    int nb_globals;
345 c896fe29 bellard
    int nb_temps;
346 641d5fbe bellard
    /* index of free temps, -1 if none */
347 641d5fbe bellard
    int first_free_temp[TCG_TYPE_COUNT * 2]; 
348 c896fe29 bellard
349 c896fe29 bellard
    /* goto_tb support */
350 c896fe29 bellard
    uint8_t *code_buf;
351 c896fe29 bellard
    unsigned long *tb_next;
352 c896fe29 bellard
    uint16_t *tb_next_offset;
353 c896fe29 bellard
    uint16_t *tb_jmp_offset; /* != NULL if USE_DIRECT_JUMP */
354 c896fe29 bellard
355 641d5fbe bellard
    /* liveness analysis */
356 866cb6cb Aurelien Jarno
    uint16_t *op_dead_args; /* for each operation, each bit tells if the
357 866cb6cb Aurelien Jarno
                               corresponding argument is dead */
358 641d5fbe bellard
    
359 c896fe29 bellard
    /* tells in which temporary a given register is. It does not take
360 c896fe29 bellard
       into account fixed registers */
361 c896fe29 bellard
    int reg_to_temp[TCG_TARGET_NB_REGS];
362 c896fe29 bellard
    TCGRegSet reserved_regs;
363 c896fe29 bellard
    tcg_target_long current_frame_offset;
364 c896fe29 bellard
    tcg_target_long frame_start;
365 c896fe29 bellard
    tcg_target_long frame_end;
366 c896fe29 bellard
    int frame_reg;
367 c896fe29 bellard
368 c896fe29 bellard
    uint8_t *code_ptr;
369 c896fe29 bellard
    TCGTemp static_temps[TCG_MAX_TEMPS];
370 c896fe29 bellard
371 c896fe29 bellard
    TCGHelperInfo *helpers;
372 c896fe29 bellard
    int nb_helpers;
373 c896fe29 bellard
    int allocated_helpers;
374 e8996ee0 bellard
    int helpers_sorted;
375 a23a9ec6 bellard
376 a23a9ec6 bellard
#ifdef CONFIG_PROFILER
377 a23a9ec6 bellard
    /* profiling info */
378 a23a9ec6 bellard
    int64_t tb_count1;
379 a23a9ec6 bellard
    int64_t tb_count;
380 a23a9ec6 bellard
    int64_t op_count; /* total insn count */
381 a23a9ec6 bellard
    int op_count_max; /* max insn per TB */
382 a23a9ec6 bellard
    int64_t temp_count;
383 a23a9ec6 bellard
    int temp_count_max;
384 a23a9ec6 bellard
    int64_t del_op_count;
385 a23a9ec6 bellard
    int64_t code_in_len;
386 a23a9ec6 bellard
    int64_t code_out_len;
387 a23a9ec6 bellard
    int64_t interm_time;
388 a23a9ec6 bellard
    int64_t code_time;
389 a23a9ec6 bellard
    int64_t la_time;
390 a23a9ec6 bellard
    int64_t restore_count;
391 a23a9ec6 bellard
    int64_t restore_time;
392 a23a9ec6 bellard
#endif
393 27bfd83c Peter Maydell
394 27bfd83c Peter Maydell
#ifdef CONFIG_DEBUG_TCG
395 27bfd83c Peter Maydell
    int temps_in_use;
396 27bfd83c Peter Maydell
#endif
397 c896fe29 bellard
};
398 c896fe29 bellard
399 c896fe29 bellard
extern TCGContext tcg_ctx;
400 c896fe29 bellard
extern uint16_t *gen_opc_ptr;
401 c896fe29 bellard
extern TCGArg *gen_opparam_ptr;
402 c896fe29 bellard
extern uint16_t gen_opc_buf[];
403 c896fe29 bellard
extern TCGArg gen_opparam_buf[];
404 c896fe29 bellard
405 c896fe29 bellard
/* pool based memory allocation */
406 c896fe29 bellard
407 c896fe29 bellard
void *tcg_malloc_internal(TCGContext *s, int size);
408 c896fe29 bellard
void tcg_pool_reset(TCGContext *s);
409 c896fe29 bellard
void tcg_pool_delete(TCGContext *s);
410 c896fe29 bellard
411 c896fe29 bellard
static inline void *tcg_malloc(int size)
412 c896fe29 bellard
{
413 c896fe29 bellard
    TCGContext *s = &tcg_ctx;
414 c896fe29 bellard
    uint8_t *ptr, *ptr_end;
415 c896fe29 bellard
    size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
416 c896fe29 bellard
    ptr = s->pool_cur;
417 c896fe29 bellard
    ptr_end = ptr + size;
418 c896fe29 bellard
    if (unlikely(ptr_end > s->pool_end)) {
419 c896fe29 bellard
        return tcg_malloc_internal(&tcg_ctx, size);
420 c896fe29 bellard
    } else {
421 c896fe29 bellard
        s->pool_cur = ptr_end;
422 c896fe29 bellard
        return ptr;
423 c896fe29 bellard
    }
424 c896fe29 bellard
}
425 c896fe29 bellard
426 c896fe29 bellard
void tcg_context_init(TCGContext *s);
427 9002ec79 Richard Henderson
void tcg_prologue_init(TCGContext *s);
428 c896fe29 bellard
void tcg_func_start(TCGContext *s);
429 c896fe29 bellard
430 54604f74 aurel32
int tcg_gen_code(TCGContext *s, uint8_t *gen_code_buf);
431 54604f74 aurel32
int tcg_gen_code_search_pc(TCGContext *s, uint8_t *gen_code_buf, long offset);
432 c896fe29 bellard
433 c896fe29 bellard
void tcg_set_frame(TCGContext *s, int reg,
434 c896fe29 bellard
                   tcg_target_long start, tcg_target_long size);
435 a7812ae4 pbrook
436 a7812ae4 pbrook
TCGv_i32 tcg_global_reg_new_i32(int reg, const char *name);
437 a7812ae4 pbrook
TCGv_i32 tcg_global_mem_new_i32(int reg, tcg_target_long offset,
438 a7812ae4 pbrook
                                const char *name);
439 a7812ae4 pbrook
TCGv_i32 tcg_temp_new_internal_i32(int temp_local);
440 a7812ae4 pbrook
static inline TCGv_i32 tcg_temp_new_i32(void)
441 a7812ae4 pbrook
{
442 a7812ae4 pbrook
    return tcg_temp_new_internal_i32(0);
443 a7812ae4 pbrook
}
444 a7812ae4 pbrook
static inline TCGv_i32 tcg_temp_local_new_i32(void)
445 a7812ae4 pbrook
{
446 a7812ae4 pbrook
    return tcg_temp_new_internal_i32(1);
447 a7812ae4 pbrook
}
448 a7812ae4 pbrook
void tcg_temp_free_i32(TCGv_i32 arg);
449 a7812ae4 pbrook
char *tcg_get_arg_str_i32(TCGContext *s, char *buf, int buf_size, TCGv_i32 arg);
450 a7812ae4 pbrook
451 a7812ae4 pbrook
TCGv_i64 tcg_global_reg_new_i64(int reg, const char *name);
452 a7812ae4 pbrook
TCGv_i64 tcg_global_mem_new_i64(int reg, tcg_target_long offset,
453 a7812ae4 pbrook
                                const char *name);
454 a7812ae4 pbrook
TCGv_i64 tcg_temp_new_internal_i64(int temp_local);
455 a7812ae4 pbrook
static inline TCGv_i64 tcg_temp_new_i64(void)
456 641d5fbe bellard
{
457 a7812ae4 pbrook
    return tcg_temp_new_internal_i64(0);
458 641d5fbe bellard
}
459 a7812ae4 pbrook
static inline TCGv_i64 tcg_temp_local_new_i64(void)
460 641d5fbe bellard
{
461 a7812ae4 pbrook
    return tcg_temp_new_internal_i64(1);
462 641d5fbe bellard
}
463 a7812ae4 pbrook
void tcg_temp_free_i64(TCGv_i64 arg);
464 a7812ae4 pbrook
char *tcg_get_arg_str_i64(TCGContext *s, char *buf, int buf_size, TCGv_i64 arg);
465 a7812ae4 pbrook
466 e31b0a7c Blue Swirl
static inline bool tcg_arg_is_local(TCGContext *s, TCGArg arg)
467 e31b0a7c Blue Swirl
{
468 e31b0a7c Blue Swirl
    return s->temps[arg].temp_local;
469 e31b0a7c Blue Swirl
}
470 e31b0a7c Blue Swirl
471 27bfd83c Peter Maydell
#if defined(CONFIG_DEBUG_TCG)
472 27bfd83c Peter Maydell
/* If you call tcg_clear_temp_count() at the start of a section of
473 27bfd83c Peter Maydell
 * code which is not supposed to leak any TCG temporaries, then
474 27bfd83c Peter Maydell
 * calling tcg_check_temp_count() at the end of the section will
475 27bfd83c Peter Maydell
 * return 1 if the section did in fact leak a temporary.
476 27bfd83c Peter Maydell
 */
477 27bfd83c Peter Maydell
void tcg_clear_temp_count(void);
478 27bfd83c Peter Maydell
int tcg_check_temp_count(void);
479 27bfd83c Peter Maydell
#else
480 27bfd83c Peter Maydell
#define tcg_clear_temp_count() do { } while (0)
481 27bfd83c Peter Maydell
#define tcg_check_temp_count() 0
482 27bfd83c Peter Maydell
#endif
483 27bfd83c Peter Maydell
484 405cf9ff Stefan Weil
void tcg_dump_info(FILE *f, fprintf_function cpu_fprintf);
485 c896fe29 bellard
486 c896fe29 bellard
#define TCG_CT_ALIAS  0x80
487 c896fe29 bellard
#define TCG_CT_IALIAS 0x40
488 c896fe29 bellard
#define TCG_CT_REG    0x01
489 c896fe29 bellard
#define TCG_CT_CONST  0x02 /* any constant of register size */
490 c896fe29 bellard
491 c896fe29 bellard
typedef struct TCGArgConstraint {
492 5ff9d6a4 bellard
    uint16_t ct;
493 5ff9d6a4 bellard
    uint8_t alias_index;
494 c896fe29 bellard
    union {
495 c896fe29 bellard
        TCGRegSet regs;
496 c896fe29 bellard
    } u;
497 c896fe29 bellard
} TCGArgConstraint;
498 c896fe29 bellard
499 c896fe29 bellard
#define TCG_MAX_OP_ARGS 16
500 c896fe29 bellard
501 8399ad59 Richard Henderson
/* Bits for TCGOpDef->flags, 8 bits available.  */
502 8399ad59 Richard Henderson
enum {
503 8399ad59 Richard Henderson
    /* Instruction defines the end of a basic block.  */
504 8399ad59 Richard Henderson
    TCG_OPF_BB_END       = 0x01,
505 8399ad59 Richard Henderson
    /* Instruction clobbers call registers and potentially update globals.  */
506 8399ad59 Richard Henderson
    TCG_OPF_CALL_CLOBBER = 0x02,
507 8399ad59 Richard Henderson
    /* Instruction has side effects: it cannot be removed
508 8399ad59 Richard Henderson
       if its outputs are not used.  */
509 8399ad59 Richard Henderson
    TCG_OPF_SIDE_EFFECTS = 0x04,
510 8399ad59 Richard Henderson
    /* Instruction operands are 64-bits (otherwise 32-bits).  */
511 8399ad59 Richard Henderson
    TCG_OPF_64BIT        = 0x08,
512 25c4d9cc Richard Henderson
    /* Instruction is optional and not implemented by the host.  */
513 25c4d9cc Richard Henderson
    TCG_OPF_NOT_PRESENT  = 0x10,
514 8399ad59 Richard Henderson
};
515 c896fe29 bellard
516 c896fe29 bellard
typedef struct TCGOpDef {
517 c896fe29 bellard
    const char *name;
518 c896fe29 bellard
    uint8_t nb_oargs, nb_iargs, nb_cargs, nb_args;
519 c896fe29 bellard
    uint8_t flags;
520 c896fe29 bellard
    TCGArgConstraint *args_ct;
521 c896fe29 bellard
    int *sorted_args;
522 c68aaa18 Stefan Weil
#if defined(CONFIG_DEBUG_TCG)
523 c68aaa18 Stefan Weil
    int used;
524 c68aaa18 Stefan Weil
#endif
525 c896fe29 bellard
} TCGOpDef;
526 8399ad59 Richard Henderson
527 8399ad59 Richard Henderson
extern TCGOpDef tcg_op_defs[];
528 2a24374a Stefan Weil
extern const size_t tcg_op_defs_max;
529 2a24374a Stefan Weil
530 c896fe29 bellard
typedef struct TCGTargetOpDef {
531 a9751609 Richard Henderson
    TCGOpcode op;
532 c896fe29 bellard
    const char *args_ct_str[TCG_MAX_OP_ARGS];
533 c896fe29 bellard
} TCGTargetOpDef;
534 c896fe29 bellard
535 c896fe29 bellard
#define tcg_abort() \
536 c896fe29 bellard
do {\
537 c896fe29 bellard
    fprintf(stderr, "%s:%d: tcg fatal error\n", __FILE__, __LINE__);\
538 c896fe29 bellard
    abort();\
539 c896fe29 bellard
} while (0)
540 c896fe29 bellard
541 c896fe29 bellard
void tcg_add_target_add_op_defs(const TCGTargetOpDef *tdefs);
542 c896fe29 bellard
543 c896fe29 bellard
#if TCG_TARGET_REG_BITS == 32
544 ebecf363 Peter Maydell
#define TCGV_NAT_TO_PTR(n) MAKE_TCGV_PTR(GET_TCGV_I32(n))
545 ebecf363 Peter Maydell
#define TCGV_PTR_TO_NAT(n) MAKE_TCGV_I32(GET_TCGV_PTR(n))
546 ebecf363 Peter Maydell
547 73f5e313 Peter Maydell
#define tcg_const_ptr(V) TCGV_NAT_TO_PTR(tcg_const_i32((tcg_target_long)(V)))
548 ebecf363 Peter Maydell
#define tcg_global_reg_new_ptr(R, N) \
549 ebecf363 Peter Maydell
    TCGV_NAT_TO_PTR(tcg_global_reg_new_i32((R), (N)))
550 ebecf363 Peter Maydell
#define tcg_global_mem_new_ptr(R, O, N) \
551 ebecf363 Peter Maydell
    TCGV_NAT_TO_PTR(tcg_global_mem_new_i32((R), (O), (N)))
552 ebecf363 Peter Maydell
#define tcg_temp_new_ptr() TCGV_NAT_TO_PTR(tcg_temp_new_i32())
553 ebecf363 Peter Maydell
#define tcg_temp_free_ptr(T) tcg_temp_free_i32(TCGV_PTR_TO_NAT(T))
554 c896fe29 bellard
#else
555 ebecf363 Peter Maydell
#define TCGV_NAT_TO_PTR(n) MAKE_TCGV_PTR(GET_TCGV_I64(n))
556 ebecf363 Peter Maydell
#define TCGV_PTR_TO_NAT(n) MAKE_TCGV_I64(GET_TCGV_PTR(n))
557 ebecf363 Peter Maydell
558 73f5e313 Peter Maydell
#define tcg_const_ptr(V) TCGV_NAT_TO_PTR(tcg_const_i64((tcg_target_long)(V)))
559 ebecf363 Peter Maydell
#define tcg_global_reg_new_ptr(R, N) \
560 ebecf363 Peter Maydell
    TCGV_NAT_TO_PTR(tcg_global_reg_new_i64((R), (N)))
561 ebecf363 Peter Maydell
#define tcg_global_mem_new_ptr(R, O, N) \
562 ebecf363 Peter Maydell
    TCGV_NAT_TO_PTR(tcg_global_mem_new_i64((R), (O), (N)))
563 ebecf363 Peter Maydell
#define tcg_temp_new_ptr() TCGV_NAT_TO_PTR(tcg_temp_new_i64())
564 ebecf363 Peter Maydell
#define tcg_temp_free_ptr(T) tcg_temp_free_i64(TCGV_PTR_TO_NAT(T))
565 c896fe29 bellard
#endif
566 c896fe29 bellard
567 a7812ae4 pbrook
void tcg_gen_callN(TCGContext *s, TCGv_ptr func, unsigned int flags,
568 a7812ae4 pbrook
                   int sizemask, TCGArg ret, int nargs, TCGArg *args);
569 a7812ae4 pbrook
570 a7812ae4 pbrook
void tcg_gen_shifti_i64(TCGv_i64 ret, TCGv_i64 arg1,
571 a7812ae4 pbrook
                        int c, int right, int arith);
572 a7812ae4 pbrook
573 8f2e8c07 Kirill Batuzov
TCGArg *tcg_optimize(TCGContext *s, uint16_t *tcg_opc_ptr, TCGArg *args,
574 8f2e8c07 Kirill Batuzov
                     TCGOpDef *tcg_op_def);
575 8f2e8c07 Kirill Batuzov
576 a7812ae4 pbrook
/* only used for debugging purposes */
577 a7812ae4 pbrook
void tcg_register_helper(void *func, const char *name);
578 a7812ae4 pbrook
const char *tcg_helper_get_name(TCGContext *s, void *func);
579 a7812ae4 pbrook
void tcg_dump_ops(TCGContext *s, FILE *outfile);
580 a7812ae4 pbrook
581 a7812ae4 pbrook
void dump_ops(const uint16_t *opc_buf, const TCGArg *opparam_buf);
582 a7812ae4 pbrook
TCGv_i32 tcg_const_i32(int32_t val);
583 a7812ae4 pbrook
TCGv_i64 tcg_const_i64(int64_t val);
584 a7812ae4 pbrook
TCGv_i32 tcg_const_local_i32(int32_t val);
585 a7812ae4 pbrook
TCGv_i64 tcg_const_local_i64(int64_t val);
586 a7812ae4 pbrook
587 b03cce8e bellard
extern uint8_t code_gen_prologue[];
588 ce285b17 Stefan Weil
589 ce285b17 Stefan Weil
/* TCG targets may use a different definition of tcg_qemu_tb_exec. */
590 ce285b17 Stefan Weil
#if !defined(tcg_qemu_tb_exec)
591 ce285b17 Stefan Weil
# define tcg_qemu_tb_exec(env, tb_ptr) \
592 cea5f9a2 Blue Swirl
    ((long REGPARM (*)(void *, void *))code_gen_prologue)(env, tb_ptr)
593 932a6909 bellard
#endif