root / thunk.h @ 564c8f99
History | View | Annotate | Download (5.4 kB)
1 | 3ef693a0 | bellard | /*
|
---|---|---|---|
2 | 3ef693a0 | bellard | * Generic thunking code to convert data between host and target CPU
|
3 | 3ef693a0 | bellard | *
|
4 | 3ef693a0 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 3ef693a0 | bellard | *
|
6 | 3ef693a0 | bellard | * This library is free software; you can redistribute it and/or
|
7 | 3ef693a0 | bellard | * modify it under the terms of the GNU Lesser General Public
|
8 | 3ef693a0 | bellard | * License as published by the Free Software Foundation; either
|
9 | 3ef693a0 | bellard | * version 2 of the License, or (at your option) any later version.
|
10 | 3ef693a0 | bellard | *
|
11 | 3ef693a0 | bellard | * This library is distributed in the hope that it will be useful,
|
12 | 3ef693a0 | bellard | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 3ef693a0 | bellard | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 3ef693a0 | bellard | * Lesser General Public License for more details.
|
15 | 3ef693a0 | bellard | *
|
16 | 3ef693a0 | bellard | * You should have received a copy of the GNU Lesser General Public
|
17 | 3ef693a0 | bellard | * License along with this library; if not, write to the Free Software
|
18 | 3ef693a0 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 3ef693a0 | bellard | */
|
20 | 31e31b8a | bellard | #ifndef THUNK_H
|
21 | 31e31b8a | bellard | #define THUNK_H
|
22 | 31e31b8a | bellard | |
23 | 31e31b8a | bellard | #include <inttypes.h> |
24 | 7d13299d | bellard | #include "config.h" |
25 | 34313956 | bellard | |
26 | 34313956 | bellard | #ifdef HAVE_BYTESWAP_H
|
27 | 31e31b8a | bellard | #include <byteswap.h> |
28 | 34313956 | bellard | #else
|
29 | 34313956 | bellard | |
30 | 34313956 | bellard | #define bswap_16(x) \
|
31 | 34313956 | bellard | ({ \ |
32 | 34313956 | bellard | uint16_t __x = (x); \ |
33 | 34313956 | bellard | ((uint16_t)( \ |
34 | 34313956 | bellard | (((uint16_t)(__x) & (uint16_t)0x00ffU) << 8) | \ |
35 | 34313956 | bellard | (((uint16_t)(__x) & (uint16_t)0xff00U) >> 8) )); \ |
36 | 34313956 | bellard | }) |
37 | 34313956 | bellard | |
38 | 34313956 | bellard | #define bswap_32(x) \
|
39 | 34313956 | bellard | ({ \ |
40 | 34313956 | bellard | uint32_t __x = (x); \ |
41 | 34313956 | bellard | ((uint32_t)( \ |
42 | 34313956 | bellard | (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \ |
43 | 34313956 | bellard | (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \ |
44 | 34313956 | bellard | (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
45 | 34313956 | bellard | (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) )); \ |
46 | 34313956 | bellard | }) |
47 | 34313956 | bellard | |
48 | 34313956 | bellard | #define bswap_64(x) \
|
49 | 34313956 | bellard | ({ \ |
50 | 367e86e8 | bellard | uint64_t __x = (x); \ |
51 | 367e86e8 | bellard | ((uint64_t)( \ |
52 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
53 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
54 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
55 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
56 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
57 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
58 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
59 | 367e86e8 | bellard | (uint64_t)(((uint64_t)(__x) & (uint64_t)0xff00000000000000ULL) >> 56) )); \ |
60 | 34313956 | bellard | }) |
61 | 34313956 | bellard | |
62 | 34313956 | bellard | #endif
|
63 | 31e31b8a | bellard | |
64 | 34313956 | bellard | #ifdef WORDS_BIGENDIAN
|
65 | 31e31b8a | bellard | #define BSWAP_NEEDED
|
66 | 31e31b8a | bellard | #endif
|
67 | 31e31b8a | bellard | |
68 | 367e86e8 | bellard | /* XXX: autoconf */
|
69 | 31e31b8a | bellard | #define TARGET_I386
|
70 | 31e31b8a | bellard | #define TARGET_LONG_BITS 32 |
71 | 31e31b8a | bellard | |
72 | 31e31b8a | bellard | |
73 | 0d330196 | bellard | #if defined(__alpha__) || defined (__ia64__)
|
74 | 31e31b8a | bellard | #define HOST_LONG_BITS 64 |
75 | 31e31b8a | bellard | #else
|
76 | 31e31b8a | bellard | #define HOST_LONG_BITS 32 |
77 | 31e31b8a | bellard | #endif
|
78 | 31e31b8a | bellard | |
79 | 31e31b8a | bellard | #define TARGET_LONG_SIZE (TARGET_LONG_BITS / 8) |
80 | 31e31b8a | bellard | #define HOST_LONG_SIZE (TARGET_LONG_BITS / 8) |
81 | 31e31b8a | bellard | |
82 | 31e31b8a | bellard | static inline uint16_t bswap16(uint16_t x) |
83 | 31e31b8a | bellard | { |
84 | 31e31b8a | bellard | return bswap_16(x);
|
85 | 31e31b8a | bellard | } |
86 | 31e31b8a | bellard | |
87 | 31e31b8a | bellard | static inline uint32_t bswap32(uint32_t x) |
88 | 31e31b8a | bellard | { |
89 | 31e31b8a | bellard | return bswap_32(x);
|
90 | 31e31b8a | bellard | } |
91 | 31e31b8a | bellard | |
92 | 31e31b8a | bellard | static inline uint64_t bswap64(uint64_t x) |
93 | 31e31b8a | bellard | { |
94 | 31e31b8a | bellard | return bswap_64(x);
|
95 | 31e31b8a | bellard | } |
96 | 31e31b8a | bellard | |
97 | bb0ebb1f | bellard | static inline void bswap16s(uint16_t *s) |
98 | 31e31b8a | bellard | { |
99 | 31e31b8a | bellard | *s = bswap16(*s); |
100 | 31e31b8a | bellard | } |
101 | 31e31b8a | bellard | |
102 | bb0ebb1f | bellard | static inline void bswap32s(uint32_t *s) |
103 | 31e31b8a | bellard | { |
104 | 31e31b8a | bellard | *s = bswap32(*s); |
105 | 31e31b8a | bellard | } |
106 | 31e31b8a | bellard | |
107 | bb0ebb1f | bellard | static inline void bswap64s(uint64_t *s) |
108 | 31e31b8a | bellard | { |
109 | 31e31b8a | bellard | *s = bswap64(*s); |
110 | 31e31b8a | bellard | } |
111 | 31e31b8a | bellard | |
112 | 31e31b8a | bellard | #ifdef BSWAP_NEEDED
|
113 | 31e31b8a | bellard | |
114 | 31e31b8a | bellard | static inline uint16_t tswap16(uint16_t s) |
115 | 31e31b8a | bellard | { |
116 | 31e31b8a | bellard | return bswap16(s);
|
117 | 31e31b8a | bellard | } |
118 | 31e31b8a | bellard | |
119 | 31e31b8a | bellard | static inline uint32_t tswap32(uint32_t s) |
120 | 31e31b8a | bellard | { |
121 | 31e31b8a | bellard | return bswap32(s);
|
122 | 31e31b8a | bellard | } |
123 | 31e31b8a | bellard | |
124 | 31e31b8a | bellard | static inline uint64_t tswap64(uint64_t s) |
125 | 31e31b8a | bellard | { |
126 | 31e31b8a | bellard | return bswap64(s);
|
127 | 31e31b8a | bellard | } |
128 | 31e31b8a | bellard | |
129 | bb0ebb1f | bellard | static inline void tswap16s(uint16_t *s) |
130 | 31e31b8a | bellard | { |
131 | 31e31b8a | bellard | *s = bswap16(*s); |
132 | 31e31b8a | bellard | } |
133 | 31e31b8a | bellard | |
134 | bb0ebb1f | bellard | static inline void tswap32s(uint32_t *s) |
135 | 31e31b8a | bellard | { |
136 | 31e31b8a | bellard | *s = bswap32(*s); |
137 | 31e31b8a | bellard | } |
138 | 31e31b8a | bellard | |
139 | bb0ebb1f | bellard | static inline void tswap64s(uint64_t *s) |
140 | 31e31b8a | bellard | { |
141 | 31e31b8a | bellard | *s = bswap64(*s); |
142 | 31e31b8a | bellard | } |
143 | 31e31b8a | bellard | |
144 | 31e31b8a | bellard | #else
|
145 | 31e31b8a | bellard | |
146 | 31e31b8a | bellard | static inline uint16_t tswap16(uint16_t s) |
147 | 31e31b8a | bellard | { |
148 | 31e31b8a | bellard | return s;
|
149 | 31e31b8a | bellard | } |
150 | 31e31b8a | bellard | |
151 | 31e31b8a | bellard | static inline uint32_t tswap32(uint32_t s) |
152 | 31e31b8a | bellard | { |
153 | 31e31b8a | bellard | return s;
|
154 | 31e31b8a | bellard | } |
155 | 31e31b8a | bellard | |
156 | 31e31b8a | bellard | static inline uint64_t tswap64(uint64_t s) |
157 | 31e31b8a | bellard | { |
158 | 31e31b8a | bellard | return s;
|
159 | 31e31b8a | bellard | } |
160 | 31e31b8a | bellard | |
161 | bb0ebb1f | bellard | static inline void tswap16s(uint16_t *s) |
162 | 31e31b8a | bellard | { |
163 | 31e31b8a | bellard | } |
164 | 31e31b8a | bellard | |
165 | bb0ebb1f | bellard | static inline void tswap32s(uint32_t *s) |
166 | 31e31b8a | bellard | { |
167 | 31e31b8a | bellard | } |
168 | 31e31b8a | bellard | |
169 | bb0ebb1f | bellard | static inline void tswap64s(uint64_t *s) |
170 | 31e31b8a | bellard | { |
171 | 31e31b8a | bellard | } |
172 | 31e31b8a | bellard | |
173 | 31e31b8a | bellard | #endif
|
174 | 31e31b8a | bellard | |
175 | 31e31b8a | bellard | #if TARGET_LONG_SIZE == 4 |
176 | 31e31b8a | bellard | #define tswapl(s) tswap32(s)
|
177 | 31e31b8a | bellard | #define tswapls(s) tswap32s((uint32_t *)(s))
|
178 | 31e31b8a | bellard | #else
|
179 | 31e31b8a | bellard | #define tswapl(s) tswap64(s)
|
180 | 31e31b8a | bellard | #define tswapls(s) tswap64s((uint64_t *)(s))
|
181 | 31e31b8a | bellard | #endif
|
182 | 31e31b8a | bellard | |
183 | 31e31b8a | bellard | #if TARGET_LONG_SIZE == 4 |
184 | 31e31b8a | bellard | typedef int32_t target_long;
|
185 | 31e31b8a | bellard | typedef uint32_t target_ulong;
|
186 | 31e31b8a | bellard | #elif TARGET_LONG_SIZE == 8 |
187 | 31e31b8a | bellard | typedef int64_t target_long;
|
188 | 31e31b8a | bellard | typedef uint64_t target_ulong;
|
189 | 31e31b8a | bellard | #else
|
190 | 31e31b8a | bellard | #error TARGET_LONG_SIZE undefined
|
191 | 31e31b8a | bellard | #endif
|
192 | 31e31b8a | bellard | |
193 | 31e31b8a | bellard | /* types enums definitions */
|
194 | 31e31b8a | bellard | |
195 | 31e31b8a | bellard | typedef enum argtype { |
196 | 31e31b8a | bellard | TYPE_NULL, |
197 | 31e31b8a | bellard | TYPE_CHAR, |
198 | 31e31b8a | bellard | TYPE_SHORT, |
199 | 31e31b8a | bellard | TYPE_INT, |
200 | 31e31b8a | bellard | TYPE_LONG, |
201 | 31e31b8a | bellard | TYPE_ULONG, |
202 | 31e31b8a | bellard | TYPE_PTRVOID, /* pointer on unknown data */
|
203 | 31e31b8a | bellard | TYPE_LONGLONG, |
204 | 31e31b8a | bellard | TYPE_ULONGLONG, |
205 | 31e31b8a | bellard | TYPE_PTR, |
206 | 31e31b8a | bellard | TYPE_ARRAY, |
207 | 31e31b8a | bellard | TYPE_STRUCT, |
208 | 31e31b8a | bellard | } argtype; |
209 | 31e31b8a | bellard | |
210 | 31e31b8a | bellard | #define MK_PTR(type) TYPE_PTR, type
|
211 | 31e31b8a | bellard | #define MK_ARRAY(type, size) TYPE_ARRAY, size, type
|
212 | 31e31b8a | bellard | #define MK_STRUCT(id) TYPE_STRUCT, id
|
213 | 31e31b8a | bellard | |
214 | 31e31b8a | bellard | #define THUNK_TARGET 0 |
215 | 31e31b8a | bellard | #define THUNK_HOST 1 |
216 | 31e31b8a | bellard | |
217 | 31e31b8a | bellard | typedef struct { |
218 | 31e31b8a | bellard | /* standard struct handling */
|
219 | 31e31b8a | bellard | const argtype *field_types;
|
220 | 31e31b8a | bellard | int nb_fields;
|
221 | 31e31b8a | bellard | int *field_offsets[2]; |
222 | 31e31b8a | bellard | /* special handling */
|
223 | 31e31b8a | bellard | void (*convert[2])(void *dst, const void *src); |
224 | 31e31b8a | bellard | int size[2]; |
225 | 31e31b8a | bellard | int align[2]; |
226 | 31e31b8a | bellard | const char *name; |
227 | 31e31b8a | bellard | } StructEntry; |
228 | 31e31b8a | bellard | |
229 | 31e31b8a | bellard | /* Translation table for bitmasks... */
|
230 | 31e31b8a | bellard | typedef struct bitmask_transtbl { |
231 | 31e31b8a | bellard | unsigned int x86_mask; |
232 | 31e31b8a | bellard | unsigned int x86_bits; |
233 | 31e31b8a | bellard | unsigned int alpha_mask; |
234 | 31e31b8a | bellard | unsigned int alpha_bits; |
235 | 31e31b8a | bellard | } bitmask_transtbl; |
236 | 31e31b8a | bellard | |
237 | 31e31b8a | bellard | void thunk_register_struct(int id, const char *name, const argtype *types); |
238 | 31e31b8a | bellard | void thunk_register_struct_direct(int id, const char *name, StructEntry *se1); |
239 | 31e31b8a | bellard | const argtype *thunk_convert(void *dst, const void *src, |
240 | 31e31b8a | bellard | const argtype *type_ptr, int to_host); |
241 | 31e31b8a | bellard | |
242 | 31e31b8a | bellard | unsigned int target_to_host_bitmask(unsigned int x86_mask, |
243 | 31e31b8a | bellard | bitmask_transtbl * trans_tbl); |
244 | 31e31b8a | bellard | unsigned int host_to_target_bitmask(unsigned int alpha_mask, |
245 | 31e31b8a | bellard | bitmask_transtbl * trans_tbl); |
246 | 31e31b8a | bellard | |
247 | 31e31b8a | bellard | #endif |