added code16 tests
[qemu/ar7.git] / thunk.h
bloba7338ad76a03682cc93625494cce7e7ea417fb11
1 #ifndef THUNK_H
2 #define THUNK_H
4 #include <inttypes.h>
5 #include "config.h"
7 #ifdef HAVE_BYTESWAP_H
8 #include <byteswap.h>
9 #else
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) )); \
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) )); \
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) )); \
43 #endif
45 #ifdef WORDS_BIGENDIAN
46 #define BSWAP_NEEDED
47 #endif
49 /* XXX: autoconf */
50 #define TARGET_I386
51 #define TARGET_LONG_BITS 32
54 #if defined(__alpha__)
55 #define HOST_LONG_BITS 64
56 #else
57 #define HOST_LONG_BITS 32
58 #endif
60 #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
61 #define HOST_LONG_SIZE (TARGET_LONG_BITS / 8)
63 static inline uint16_t bswap16(uint16_t x)
65 return bswap_16(x);
68 static inline uint32_t bswap32(uint32_t x)
70 return bswap_32(x);
73 static inline uint64_t bswap64(uint64_t x)
75 return bswap_64(x);
78 static void inline bswap16s(uint16_t *s)
80 *s = bswap16(*s);
83 static void inline bswap32s(uint32_t *s)
85 *s = bswap32(*s);
88 static void inline bswap64s(uint64_t *s)
90 *s = bswap64(*s);
93 #ifdef BSWAP_NEEDED
95 static inline uint16_t tswap16(uint16_t s)
97 return bswap16(s);
100 static inline uint32_t tswap32(uint32_t s)
102 return bswap32(s);
105 static inline uint64_t tswap64(uint64_t s)
107 return bswap64(s);
110 static void inline tswap16s(uint16_t *s)
112 *s = bswap16(*s);
115 static void inline tswap32s(uint32_t *s)
117 *s = bswap32(*s);
120 static void inline tswap64s(uint64_t *s)
122 *s = bswap64(*s);
125 #else
127 static inline uint16_t tswap16(uint16_t s)
129 return s;
132 static inline uint32_t tswap32(uint32_t s)
134 return s;
137 static inline uint64_t tswap64(uint64_t s)
139 return s;
142 static void inline tswap16s(uint16_t *s)
146 static void inline tswap32s(uint32_t *s)
150 static void inline tswap64s(uint64_t *s)
154 #endif
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
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
174 /* types enums definitions */
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;
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
195 #define THUNK_TARGET 0
196 #define THUNK_HOST 1
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;
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;
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);
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);
228 #endif