Statistics
| Branch: | Revision:

root / thunk.h @ 7d13299d

History | View | Annotate | Download (4.6 kB)

1
#ifndef THUNK_H
2
#define THUNK_H
3

    
4
#include <inttypes.h>
5
#include "config.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
#ifdef WORDS_BIGENDIAN
46
#define BSWAP_NEEDED
47
#endif
48

    
49
/* XXX: autoconf */
50
#define TARGET_I386
51
#define TARGET_LONG_BITS 32
52

    
53

    
54
#if defined(__alpha__)
55
#define HOST_LONG_BITS 64
56
#else
57
#define HOST_LONG_BITS 32
58
#endif
59

    
60
#define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
61
#define HOST_LONG_SIZE (TARGET_LONG_BITS / 8)
62

    
63
static inline uint16_t bswap16(uint16_t x)
64
{
65
    return bswap_16(x);
66
}
67

    
68
static inline uint32_t bswap32(uint32_t x) 
69
{
70
    return bswap_32(x);
71
}
72

    
73
static inline uint64_t bswap64(uint64_t x) 
74
{
75
    return bswap_64(x);
76
}
77

    
78
static void inline bswap16s(uint16_t *s)
79
{
80
    *s = bswap16(*s);
81
}
82

    
83
static void inline bswap32s(uint32_t *s)
84
{
85
    *s = bswap32(*s);
86
}
87

    
88
static void inline bswap64s(uint64_t *s)
89
{
90
    *s = bswap64(*s);
91
}
92

    
93
#ifdef BSWAP_NEEDED
94

    
95
static inline uint16_t tswap16(uint16_t s)
96
{
97
    return bswap16(s);
98
}
99

    
100
static inline uint32_t tswap32(uint32_t s)
101
{
102
    return bswap32(s);
103
}
104

    
105
static inline uint64_t tswap64(uint64_t s)
106
{
107
    return bswap64(s);
108
}
109

    
110
static void inline tswap16s(uint16_t *s)
111
{
112
    *s = bswap16(*s);
113
}
114

    
115
static void inline tswap32s(uint32_t *s)
116
{
117
    *s = bswap32(*s);
118
}
119

    
120
static void inline tswap64s(uint64_t *s)
121
{
122
    *s = bswap64(*s);
123
}
124

    
125
#else
126

    
127
static inline uint16_t tswap16(uint16_t s)
128
{
129
    return s;
130
}
131

    
132
static inline uint32_t tswap32(uint32_t s)
133
{
134
    return s;
135
}
136

    
137
static inline uint64_t tswap64(uint64_t s)
138
{
139
    return s;
140
}
141

    
142
static void inline tswap16s(uint16_t *s)
143
{
144
}
145

    
146
static void inline tswap32s(uint32_t *s)
147
{
148
}
149

    
150
static void inline tswap64s(uint64_t *s)
151
{
152
}
153

    
154
#endif
155

    
156
#if TARGET_LONG_SIZE == 4
157
#define tswapl(s) tswap32(s)
158
#define tswapls(s) tswap32s((uint32_t *)(s))
159
#else
160
#define tswapl(s) tswap64(s)
161
#define tswapls(s) tswap64s((uint64_t *)(s))
162
#endif
163

    
164
#if TARGET_LONG_SIZE == 4
165
typedef int32_t target_long;
166
typedef uint32_t target_ulong;
167
#elif TARGET_LONG_SIZE == 8
168
typedef int64_t target_long;
169
typedef uint64_t target_ulong;
170
#else
171
#error TARGET_LONG_SIZE undefined
172
#endif
173

    
174
/* types enums definitions */
175

    
176
typedef enum argtype {
177
    TYPE_NULL,
178
    TYPE_CHAR,
179
    TYPE_SHORT,
180
    TYPE_INT,
181
    TYPE_LONG,
182
    TYPE_ULONG,
183
    TYPE_PTRVOID, /* pointer on unknown data */
184
    TYPE_LONGLONG,
185
    TYPE_ULONGLONG,
186
    TYPE_PTR,
187
    TYPE_ARRAY,
188
    TYPE_STRUCT,
189
} argtype;
190

    
191
#define MK_PTR(type) TYPE_PTR, type
192
#define MK_ARRAY(type, size) TYPE_ARRAY, size, type
193
#define MK_STRUCT(id) TYPE_STRUCT, id
194

    
195
#define THUNK_TARGET 0
196
#define THUNK_HOST   1
197

    
198
typedef struct {
199
    /* standard struct handling */
200
    const argtype *field_types;
201
    int nb_fields;
202
    int *field_offsets[2];
203
    /* special handling */
204
    void (*convert[2])(void *dst, const void *src);
205
    int size[2];
206
    int align[2];
207
    const char *name;
208
} StructEntry;
209

    
210
/* Translation table for bitmasks... */
211
typedef struct bitmask_transtbl {
212
        unsigned int        x86_mask;
213
        unsigned int        x86_bits;
214
        unsigned int        alpha_mask;
215
        unsigned int        alpha_bits;
216
} bitmask_transtbl;
217

    
218
void thunk_register_struct(int id, const char *name, const argtype *types);
219
void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
220
const argtype *thunk_convert(void *dst, const void *src, 
221
                             const argtype *type_ptr, int to_host);
222

    
223
unsigned int target_to_host_bitmask(unsigned int x86_mask, 
224
                                    bitmask_transtbl * trans_tbl);
225
unsigned int host_to_target_bitmask(unsigned int alpha_mask, 
226
                                    bitmask_transtbl * trans_tbl);
227

    
228
#endif