2 * Generic thunking code to convert data between host and target CPU
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #ifdef HAVE_BYTESWAP_H
34 (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \
35 (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \
42 (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
43 (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
44 (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
45 (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \
52 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \
53 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
54 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
55 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
56 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
57 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
58 (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
59 (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \
64 #if defined(WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
69 #define TARGET_LONG_BITS 32
71 #if defined(__alpha__) || defined (__ia64__)
72 #define HOST_LONG_BITS 64
74 #define HOST_LONG_BITS 32
77 #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8)
78 #define HOST_LONG_SIZE (HOST_LONG_BITS / 8)
80 static inline uint16_t bswap16(uint16_t x
)
85 static inline uint32_t bswap32(uint32_t x
)
90 static inline uint64_t bswap64(uint64_t x
)
95 static inline void bswap16s(uint16_t *s
)
100 static inline void bswap32s(uint32_t *s
)
105 static inline void bswap64s(uint64_t *s
)
112 static inline uint16_t tswap16(uint16_t s
)
117 static inline uint32_t tswap32(uint32_t s
)
122 static inline uint64_t tswap64(uint64_t s
)
127 static inline void tswap16s(uint16_t *s
)
132 static inline void tswap32s(uint32_t *s
)
137 static inline void tswap64s(uint64_t *s
)
144 static inline uint16_t tswap16(uint16_t s
)
149 static inline uint32_t tswap32(uint32_t s
)
154 static inline uint64_t tswap64(uint64_t s
)
159 static inline void tswap16s(uint16_t *s
)
163 static inline void tswap32s(uint32_t *s
)
167 static inline void tswap64s(uint64_t *s
)
173 #if TARGET_LONG_SIZE == 4
174 #define tswapl(s) tswap32(s)
175 #define tswapls(s) tswap32s((uint32_t *)(s))
177 #define tswapl(s) tswap64(s)
178 #define tswapls(s) tswap64s((uint64_t *)(s))
181 #if TARGET_LONG_SIZE == 4
182 typedef int32_t target_long
;
183 typedef uint32_t target_ulong
;
184 #elif TARGET_LONG_SIZE == 8
185 typedef int64_t target_long
;
186 typedef uint64_t target_ulong
;
188 #error TARGET_LONG_SIZE undefined
191 /* types enums definitions */
193 typedef enum argtype
{
200 TYPE_PTRVOID
, /* pointer on unknown data */
208 #define MK_PTR(type) TYPE_PTR, type
209 #define MK_ARRAY(type, size) TYPE_ARRAY, size, type
210 #define MK_STRUCT(id) TYPE_STRUCT, id
212 #define THUNK_TARGET 0
216 /* standard struct handling */
217 const argtype
*field_types
;
219 int *field_offsets
[2];
220 /* special handling */
221 void (*convert
[2])(void *dst
, const void *src
);
227 /* Translation table for bitmasks... */
228 typedef struct bitmask_transtbl
{
229 unsigned int x86_mask
;
230 unsigned int x86_bits
;
231 unsigned int alpha_mask
;
232 unsigned int alpha_bits
;
235 void thunk_register_struct(int id
, const char *name
, const argtype
*types
);
236 void thunk_register_struct_direct(int id
, const char *name
, StructEntry
*se1
);
237 const argtype
*thunk_convert(void *dst
, const void *src
,
238 const argtype
*type_ptr
, int to_host
);
240 extern StructEntry struct_entries
[];
242 static inline int thunk_type_size(const argtype
*type_ptr
, int is_host
)
245 const StructEntry
*se
;
263 return HOST_LONG_SIZE
;
265 return TARGET_LONG_SIZE
;
270 return size
* thunk_type_size(type_ptr
+ 2, is_host
);
272 se
= struct_entries
+ type_ptr
[1];
273 return se
->size
[is_host
];
279 static inline int thunk_type_align(const argtype
*type_ptr
, int is_host
)
282 const StructEntry
*se
;
300 return HOST_LONG_SIZE
;
302 return TARGET_LONG_SIZE
;
306 return thunk_type_align(type_ptr
+ 2, is_host
);
308 se
= struct_entries
+ type_ptr
[1];
309 return se
->align
[is_host
];
315 unsigned int target_to_host_bitmask(unsigned int x86_mask
,
316 bitmask_transtbl
* trans_tbl
);
317 unsigned int host_to_target_bitmask(unsigned int alpha_mask
,
318 bitmask_transtbl
* trans_tbl
);