Statistics
| Branch: | Revision:

root / thunk.h @ 367e86e8

History | View | Annotate | Download (4.7 kB)

1 31e31b8a bellard
#ifndef THUNK_H
2 31e31b8a bellard
#define THUNK_H
3 31e31b8a bellard
4 31e31b8a bellard
#include <inttypes.h>
5 34313956 bellard
#include <endian.h>
6 34313956 bellard
7 34313956 bellard
#ifdef HAVE_BYTESWAP_H
8 31e31b8a bellard
#include <byteswap.h>
9 34313956 bellard
#else
10 34313956 bellard
11 34313956 bellard
#define bswap_16(x) \
12 34313956 bellard
({ \
13 34313956 bellard
        uint16_t __x = (x); \
14 34313956 bellard
        ((uint16_t)( \
15 34313956 bellard
                (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
16 34313956 bellard
                (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
17 34313956 bellard
})
18 34313956 bellard
19 34313956 bellard
#define bswap_32(x) \
20 34313956 bellard
({ \
21 34313956 bellard
        uint32_t __x = (x); \
22 34313956 bellard
        ((uint32_t)( \
23 34313956 bellard
                (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
24 34313956 bellard
                (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) <<  8) | \
25 34313956 bellard
                (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >>  8) | \
26 34313956 bellard
                (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
27 34313956 bellard
})
28 34313956 bellard
29 34313956 bellard
#define bswap_64(x) \
30 34313956 bellard
({ \
31 367e86e8 bellard
        uint64_t __x = (x); \
32 367e86e8 bellard
        ((uint64_t)( \
33 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
34 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
35 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
36 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
37 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
38 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
39 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
40 367e86e8 bellard
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
41 34313956 bellard
})
42 34313956 bellard
43 34313956 bellard
#endif
44 31e31b8a bellard
45 31e31b8a bellard
#undef WORDS_BIGENDIAN
46 31e31b8a bellard
#if __BYTE_ORDER == __BIG_ENDIAN
47 31e31b8a bellard
#define WORDS_BIGENDIAN
48 31e31b8a bellard
#endif
49 31e31b8a bellard
50 34313956 bellard
#ifdef WORDS_BIGENDIAN
51 31e31b8a bellard
#define BSWAP_NEEDED
52 31e31b8a bellard
#endif
53 31e31b8a bellard
54 367e86e8 bellard
/* XXX: autoconf */
55 31e31b8a bellard
#define TARGET_I386
56 31e31b8a bellard
#define TARGET_LONG_BITS 32
57 31e31b8a bellard
58 31e31b8a bellard
59 31e31b8a bellard
#if defined(__alpha__)
60 31e31b8a bellard
#define HOST_LONG_BITS 64
61 31e31b8a bellard
#else
62 31e31b8a bellard
#define HOST_LONG_BITS 32
63 31e31b8a bellard
#endif
64 31e31b8a bellard
65 31e31b8a bellard
#define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
66 31e31b8a bellard
#define HOST_LONG_SIZE (TARGET_LONG_BITS / 8)
67 31e31b8a bellard
68 31e31b8a bellard
static inline uint16_t bswap16(uint16_t x)
69 31e31b8a bellard
{
70 31e31b8a bellard
    return bswap_16(x);
71 31e31b8a bellard
}
72 31e31b8a bellard
73 31e31b8a bellard
static inline uint32_t bswap32(uint32_t x) 
74 31e31b8a bellard
{
75 31e31b8a bellard
    return bswap_32(x);
76 31e31b8a bellard
}
77 31e31b8a bellard
78 31e31b8a bellard
static inline uint64_t bswap64(uint64_t x) 
79 31e31b8a bellard
{
80 31e31b8a bellard
    return bswap_64(x);
81 31e31b8a bellard
}
82 31e31b8a bellard
83 31e31b8a bellard
static void inline bswap16s(uint16_t *s)
84 31e31b8a bellard
{
85 31e31b8a bellard
    *s = bswap16(*s);
86 31e31b8a bellard
}
87 31e31b8a bellard
88 31e31b8a bellard
static void inline bswap32s(uint32_t *s)
89 31e31b8a bellard
{
90 31e31b8a bellard
    *s = bswap32(*s);
91 31e31b8a bellard
}
92 31e31b8a bellard
93 31e31b8a bellard
static void inline bswap64s(uint64_t *s)
94 31e31b8a bellard
{
95 31e31b8a bellard
    *s = bswap64(*s);
96 31e31b8a bellard
}
97 31e31b8a bellard
98 31e31b8a bellard
#ifdef BSWAP_NEEDED
99 31e31b8a bellard
100 31e31b8a bellard
static inline uint16_t tswap16(uint16_t s)
101 31e31b8a bellard
{
102 31e31b8a bellard
    return bswap16(s);
103 31e31b8a bellard
}
104 31e31b8a bellard
105 31e31b8a bellard
static inline uint32_t tswap32(uint32_t s)
106 31e31b8a bellard
{
107 31e31b8a bellard
    return bswap32(s);
108 31e31b8a bellard
}
109 31e31b8a bellard
110 31e31b8a bellard
static inline uint64_t tswap64(uint64_t s)
111 31e31b8a bellard
{
112 31e31b8a bellard
    return bswap64(s);
113 31e31b8a bellard
}
114 31e31b8a bellard
115 31e31b8a bellard
static void inline tswap16s(uint16_t *s)
116 31e31b8a bellard
{
117 31e31b8a bellard
    *s = bswap16(*s);
118 31e31b8a bellard
}
119 31e31b8a bellard
120 31e31b8a bellard
static void inline tswap32s(uint32_t *s)
121 31e31b8a bellard
{
122 31e31b8a bellard
    *s = bswap32(*s);
123 31e31b8a bellard
}
124 31e31b8a bellard
125 31e31b8a bellard
static void inline tswap64s(uint64_t *s)
126 31e31b8a bellard
{
127 31e31b8a bellard
    *s = bswap64(*s);
128 31e31b8a bellard
}
129 31e31b8a bellard
130 31e31b8a bellard
#else
131 31e31b8a bellard
132 31e31b8a bellard
static inline uint16_t tswap16(uint16_t s)
133 31e31b8a bellard
{
134 31e31b8a bellard
    return s;
135 31e31b8a bellard
}
136 31e31b8a bellard
137 31e31b8a bellard
static inline uint32_t tswap32(uint32_t s)
138 31e31b8a bellard
{
139 31e31b8a bellard
    return s;
140 31e31b8a bellard
}
141 31e31b8a bellard
142 31e31b8a bellard
static inline uint64_t tswap64(uint64_t s)
143 31e31b8a bellard
{
144 31e31b8a bellard
    return s;
145 31e31b8a bellard
}
146 31e31b8a bellard
147 31e31b8a bellard
static void inline tswap16s(uint16_t *s)
148 31e31b8a bellard
{
149 31e31b8a bellard
}
150 31e31b8a bellard
151 31e31b8a bellard
static void inline tswap32s(uint32_t *s)
152 31e31b8a bellard
{
153 31e31b8a bellard
}
154 31e31b8a bellard
155 31e31b8a bellard
static void inline tswap64s(uint64_t *s)
156 31e31b8a bellard
{
157 31e31b8a bellard
}
158 31e31b8a bellard
159 31e31b8a bellard
#endif
160 31e31b8a bellard
161 31e31b8a bellard
#if TARGET_LONG_SIZE == 4
162 31e31b8a bellard
#define tswapl(s) tswap32(s)
163 31e31b8a bellard
#define tswapls(s) tswap32s((uint32_t *)(s))
164 31e31b8a bellard
#else
165 31e31b8a bellard
#define tswapl(s) tswap64(s)
166 31e31b8a bellard
#define tswapls(s) tswap64s((uint64_t *)(s))
167 31e31b8a bellard
#endif
168 31e31b8a bellard
169 31e31b8a bellard
#if TARGET_LONG_SIZE == 4
170 31e31b8a bellard
typedef int32_t target_long;
171 31e31b8a bellard
typedef uint32_t target_ulong;
172 31e31b8a bellard
#elif TARGET_LONG_SIZE == 8
173 31e31b8a bellard
typedef int64_t target_long;
174 31e31b8a bellard
typedef uint64_t target_ulong;
175 31e31b8a bellard
#else
176 31e31b8a bellard
#error TARGET_LONG_SIZE undefined
177 31e31b8a bellard
#endif
178 31e31b8a bellard
179 31e31b8a bellard
/* types enums definitions */
180 31e31b8a bellard
181 31e31b8a bellard
typedef enum argtype {
182 31e31b8a bellard
    TYPE_NULL,
183 31e31b8a bellard
    TYPE_CHAR,
184 31e31b8a bellard
    TYPE_SHORT,
185 31e31b8a bellard
    TYPE_INT,
186 31e31b8a bellard
    TYPE_LONG,
187 31e31b8a bellard
    TYPE_ULONG,
188 31e31b8a bellard
    TYPE_PTRVOID, /* pointer on unknown data */
189 31e31b8a bellard
    TYPE_LONGLONG,
190 31e31b8a bellard
    TYPE_ULONGLONG,
191 31e31b8a bellard
    TYPE_PTR,
192 31e31b8a bellard
    TYPE_ARRAY,
193 31e31b8a bellard
    TYPE_STRUCT,
194 31e31b8a bellard
} argtype;
195 31e31b8a bellard
196 31e31b8a bellard
#define MK_PTR(type) TYPE_PTR, type
197 31e31b8a bellard
#define MK_ARRAY(type, size) TYPE_ARRAY, size, type
198 31e31b8a bellard
#define MK_STRUCT(id) TYPE_STRUCT, id
199 31e31b8a bellard
200 31e31b8a bellard
#define THUNK_TARGET 0
201 31e31b8a bellard
#define THUNK_HOST   1
202 31e31b8a bellard
203 31e31b8a bellard
typedef struct {
204 31e31b8a bellard
    /* standard struct handling */
205 31e31b8a bellard
    const argtype *field_types;
206 31e31b8a bellard
    int nb_fields;
207 31e31b8a bellard
    int *field_offsets[2];
208 31e31b8a bellard
    /* special handling */
209 31e31b8a bellard
    void (*convert[2])(void *dst, const void *src);
210 31e31b8a bellard
    int size[2];
211 31e31b8a bellard
    int align[2];
212 31e31b8a bellard
    const char *name;
213 31e31b8a bellard
} StructEntry;
214 31e31b8a bellard
215 31e31b8a bellard
/* Translation table for bitmasks... */
216 31e31b8a bellard
typedef struct bitmask_transtbl {
217 31e31b8a bellard
        unsigned int        x86_mask;
218 31e31b8a bellard
        unsigned int        x86_bits;
219 31e31b8a bellard
        unsigned int        alpha_mask;
220 31e31b8a bellard
        unsigned int        alpha_bits;
221 31e31b8a bellard
} bitmask_transtbl;
222 31e31b8a bellard
223 31e31b8a bellard
void thunk_register_struct(int id, const char *name, const argtype *types);
224 31e31b8a bellard
void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
225 31e31b8a bellard
const argtype *thunk_convert(void *dst, const void *src, 
226 31e31b8a bellard
                             const argtype *type_ptr, int to_host);
227 31e31b8a bellard
228 31e31b8a bellard
unsigned int target_to_host_bitmask(unsigned int x86_mask, 
229 31e31b8a bellard
                                    bitmask_transtbl * trans_tbl);
230 31e31b8a bellard
unsigned int host_to_target_bitmask(unsigned int alpha_mask, 
231 31e31b8a bellard
                                    bitmask_transtbl * trans_tbl);
232 31e31b8a bellard
233 31e31b8a bellard
#endif