Statistics
| Branch: | Revision:

root / thunk.h @ 367e86e8

History | View | Annotate | Download (4.7 kB)

1
#ifndef THUNK_H
2
#define THUNK_H
3

    
4
#include <inttypes.h>
5
#include <endian.h>
6

    
7
#ifdef HAVE_BYTESWAP_H
8
#include <byteswap.h>
9
#else
10

    
11
#define bswap_16(x) \
12
({ \
13
        uint16_t __x = (x); \
14
        ((uint16_t)( \
15
                (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
16
                (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
17
})
18

    
19
#define bswap_32(x) \
20
({ \
21
        uint32_t __x = (x); \
22
        ((uint32_t)( \
23
                (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
24
                (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) <<  8) | \
25
                (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >>  8) | \
26
                (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
27
})
28

    
29
#define bswap_64(x) \
30
({ \
31
        uint64_t __x = (x); \
32
        ((uint64_t)( \
33
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
34
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
35
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
36
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) <<  8) | \
37
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >>  8) | \
38
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
39
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
40
                (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
41
})
42

    
43
#endif
44

    
45
#undef WORDS_BIGENDIAN
46
#if __BYTE_ORDER == __BIG_ENDIAN
47
#define WORDS_BIGENDIAN
48
#endif
49

    
50
#ifdef WORDS_BIGENDIAN
51
#define BSWAP_NEEDED
52
#endif
53

    
54
/* XXX: autoconf */
55
#define TARGET_I386
56
#define TARGET_LONG_BITS 32
57

    
58

    
59
#if defined(__alpha__)
60
#define HOST_LONG_BITS 64
61
#else
62
#define HOST_LONG_BITS 32
63
#endif
64

    
65
#define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
66
#define HOST_LONG_SIZE (TARGET_LONG_BITS / 8)
67

    
68
static inline uint16_t bswap16(uint16_t x)
69
{
70
    return bswap_16(x);
71
}
72

    
73
static inline uint32_t bswap32(uint32_t x) 
74
{
75
    return bswap_32(x);
76
}
77

    
78
static inline uint64_t bswap64(uint64_t x) 
79
{
80
    return bswap_64(x);
81
}
82

    
83
static void inline bswap16s(uint16_t *s)
84
{
85
    *s = bswap16(*s);
86
}
87

    
88
static void inline bswap32s(uint32_t *s)
89
{
90
    *s = bswap32(*s);
91
}
92

    
93
static void inline bswap64s(uint64_t *s)
94
{
95
    *s = bswap64(*s);
96
}
97

    
98
#ifdef BSWAP_NEEDED
99

    
100
static inline uint16_t tswap16(uint16_t s)
101
{
102
    return bswap16(s);
103
}
104

    
105
static inline uint32_t tswap32(uint32_t s)
106
{
107
    return bswap32(s);
108
}
109

    
110
static inline uint64_t tswap64(uint64_t s)
111
{
112
    return bswap64(s);
113
}
114

    
115
static void inline tswap16s(uint16_t *s)
116
{
117
    *s = bswap16(*s);
118
}
119

    
120
static void inline tswap32s(uint32_t *s)
121
{
122
    *s = bswap32(*s);
123
}
124

    
125
static void inline tswap64s(uint64_t *s)
126
{
127
    *s = bswap64(*s);
128
}
129

    
130
#else
131

    
132
static inline uint16_t tswap16(uint16_t s)
133
{
134
    return s;
135
}
136

    
137
static inline uint32_t tswap32(uint32_t s)
138
{
139
    return s;
140
}
141

    
142
static inline uint64_t tswap64(uint64_t s)
143
{
144
    return s;
145
}
146

    
147
static void inline tswap16s(uint16_t *s)
148
{
149
}
150

    
151
static void inline tswap32s(uint32_t *s)
152
{
153
}
154

    
155
static void inline tswap64s(uint64_t *s)
156
{
157
}
158

    
159
#endif
160

    
161
#if TARGET_LONG_SIZE == 4
162
#define tswapl(s) tswap32(s)
163
#define tswapls(s) tswap32s((uint32_t *)(s))
164
#else
165
#define tswapl(s) tswap64(s)
166
#define tswapls(s) tswap64s((uint64_t *)(s))
167
#endif
168

    
169
#if TARGET_LONG_SIZE == 4
170
typedef int32_t target_long;
171
typedef uint32_t target_ulong;
172
#elif TARGET_LONG_SIZE == 8
173
typedef int64_t target_long;
174
typedef uint64_t target_ulong;
175
#else
176
#error TARGET_LONG_SIZE undefined
177
#endif
178

    
179
/* types enums definitions */
180

    
181
typedef enum argtype {
182
    TYPE_NULL,
183
    TYPE_CHAR,
184
    TYPE_SHORT,
185
    TYPE_INT,
186
    TYPE_LONG,
187
    TYPE_ULONG,
188
    TYPE_PTRVOID, /* pointer on unknown data */
189
    TYPE_LONGLONG,
190
    TYPE_ULONGLONG,
191
    TYPE_PTR,
192
    TYPE_ARRAY,
193
    TYPE_STRUCT,
194
} argtype;
195

    
196
#define MK_PTR(type) TYPE_PTR, type
197
#define MK_ARRAY(type, size) TYPE_ARRAY, size, type
198
#define MK_STRUCT(id) TYPE_STRUCT, id
199

    
200
#define THUNK_TARGET 0
201
#define THUNK_HOST   1
202

    
203
typedef struct {
204
    /* standard struct handling */
205
    const argtype *field_types;
206
    int nb_fields;
207
    int *field_offsets[2];
208
    /* special handling */
209
    void (*convert[2])(void *dst, const void *src);
210
    int size[2];
211
    int align[2];
212
    const char *name;
213
} StructEntry;
214

    
215
/* Translation table for bitmasks... */
216
typedef struct bitmask_transtbl {
217
        unsigned int        x86_mask;
218
        unsigned int        x86_bits;
219
        unsigned int        alpha_mask;
220
        unsigned int        alpha_bits;
221
} bitmask_transtbl;
222

    
223
void thunk_register_struct(int id, const char *name, const argtype *types);
224
void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
225
const argtype *thunk_convert(void *dst, const void *src, 
226
                             const argtype *type_ptr, int to_host);
227

    
228
unsigned int target_to_host_bitmask(unsigned int x86_mask, 
229
                                    bitmask_transtbl * trans_tbl);
230
unsigned int host_to_target_bitmask(unsigned int alpha_mask, 
231
                                    bitmask_transtbl * trans_tbl);
232

    
233
#endif