blob: b281319a5a5f95d234754dbc16eec03f9c0b6063 [file] [log] [blame]
bellard3ef693a2003-03-23 20:17:16 +00001/*
2 * Generic thunking code to convert data between host and target CPU
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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.
10 *
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.
15 *
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
19 */
bellard31e31b82003-02-18 22:55:36 +000020#ifndef THUNK_H
21#define THUNK_H
22
23#include <inttypes.h>
bellard35b66fc2004-01-24 15:26:06 +000024#include "cpu.h"
bellard34313952003-02-18 23:03:03 +000025
bellardabcd5da2003-08-10 21:38:48 +000026#include "bswap.h"
bellard31e31b82003-02-18 22:55:36 +000027
bellard24374902003-06-15 19:52:54 +000028#if defined(WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
bellard31e31b82003-02-18 22:55:36 +000029#define BSWAP_NEEDED
30#endif
31
bellard31e31b82003-02-18 22:55:36 +000032#ifdef BSWAP_NEEDED
33
34static inline uint16_t tswap16(uint16_t s)
35{
36 return bswap16(s);
37}
38
39static inline uint32_t tswap32(uint32_t s)
40{
41 return bswap32(s);
42}
43
44static inline uint64_t tswap64(uint64_t s)
45{
46 return bswap64(s);
47}
48
bellardbb0ebb12003-04-10 00:02:33 +000049static inline void tswap16s(uint16_t *s)
bellard31e31b82003-02-18 22:55:36 +000050{
51 *s = bswap16(*s);
52}
53
bellardbb0ebb12003-04-10 00:02:33 +000054static inline void tswap32s(uint32_t *s)
bellard31e31b82003-02-18 22:55:36 +000055{
56 *s = bswap32(*s);
57}
58
bellardbb0ebb12003-04-10 00:02:33 +000059static inline void tswap64s(uint64_t *s)
bellard31e31b82003-02-18 22:55:36 +000060{
61 *s = bswap64(*s);
62}
63
64#else
65
66static inline uint16_t tswap16(uint16_t s)
67{
68 return s;
69}
70
71static inline uint32_t tswap32(uint32_t s)
72{
73 return s;
74}
75
76static inline uint64_t tswap64(uint64_t s)
77{
78 return s;
79}
80
bellardbb0ebb12003-04-10 00:02:33 +000081static inline void tswap16s(uint16_t *s)
bellard31e31b82003-02-18 22:55:36 +000082{
83}
84
bellardbb0ebb12003-04-10 00:02:33 +000085static inline void tswap32s(uint32_t *s)
bellard31e31b82003-02-18 22:55:36 +000086{
87}
88
bellardbb0ebb12003-04-10 00:02:33 +000089static inline void tswap64s(uint64_t *s)
bellard31e31b82003-02-18 22:55:36 +000090{
91}
92
93#endif
94
95#if TARGET_LONG_SIZE == 4
96#define tswapl(s) tswap32(s)
97#define tswapls(s) tswap32s((uint32_t *)(s))
98#else
99#define tswapl(s) tswap64(s)
100#define tswapls(s) tswap64s((uint64_t *)(s))
101#endif
102
bellard31e31b82003-02-18 22:55:36 +0000103/* types enums definitions */
104
105typedef enum argtype {
106 TYPE_NULL,
107 TYPE_CHAR,
108 TYPE_SHORT,
109 TYPE_INT,
110 TYPE_LONG,
111 TYPE_ULONG,
112 TYPE_PTRVOID, /* pointer on unknown data */
113 TYPE_LONGLONG,
114 TYPE_ULONGLONG,
115 TYPE_PTR,
116 TYPE_ARRAY,
117 TYPE_STRUCT,
118} argtype;
119
120#define MK_PTR(type) TYPE_PTR, type
121#define MK_ARRAY(type, size) TYPE_ARRAY, size, type
122#define MK_STRUCT(id) TYPE_STRUCT, id
123
124#define THUNK_TARGET 0
125#define THUNK_HOST 1
126
127typedef struct {
128 /* standard struct handling */
129 const argtype *field_types;
130 int nb_fields;
131 int *field_offsets[2];
132 /* special handling */
133 void (*convert[2])(void *dst, const void *src);
134 int size[2];
135 int align[2];
136 const char *name;
137} StructEntry;
138
139/* Translation table for bitmasks... */
140typedef struct bitmask_transtbl {
141 unsigned int x86_mask;
142 unsigned int x86_bits;
143 unsigned int alpha_mask;
144 unsigned int alpha_bits;
145} bitmask_transtbl;
146
147void thunk_register_struct(int id, const char *name, const argtype *types);
148void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
149const argtype *thunk_convert(void *dst, const void *src,
150 const argtype *type_ptr, int to_host);
bellard0ad041d2003-06-25 22:11:41 +0000151#ifndef NO_THUNK_TYPE_SIZE
bellard31e31b82003-02-18 22:55:36 +0000152
bellard24374902003-06-15 19:52:54 +0000153extern StructEntry struct_entries[];
154
155static inline int thunk_type_size(const argtype *type_ptr, int is_host)
156{
157 int type, size;
158 const StructEntry *se;
159
160 type = *type_ptr;
161 switch(type) {
162 case TYPE_CHAR:
163 return 1;
164 case TYPE_SHORT:
165 return 2;
166 case TYPE_INT:
167 return 4;
168 case TYPE_LONGLONG:
169 case TYPE_ULONGLONG:
170 return 8;
171 case TYPE_LONG:
172 case TYPE_ULONG:
173 case TYPE_PTRVOID:
174 case TYPE_PTR:
175 if (is_host) {
176 return HOST_LONG_SIZE;
177 } else {
178 return TARGET_LONG_SIZE;
179 }
180 break;
181 case TYPE_ARRAY:
182 size = type_ptr[1];
183 return size * thunk_type_size(type_ptr + 2, is_host);
184 case TYPE_STRUCT:
185 se = struct_entries + type_ptr[1];
186 return se->size[is_host];
187 default:
188 return -1;
189 }
190}
191
192static inline int thunk_type_align(const argtype *type_ptr, int is_host)
193{
194 int type;
195 const StructEntry *se;
196
197 type = *type_ptr;
198 switch(type) {
199 case TYPE_CHAR:
200 return 1;
201 case TYPE_SHORT:
202 return 2;
203 case TYPE_INT:
204 return 4;
205 case TYPE_LONGLONG:
206 case TYPE_ULONGLONG:
207 return 8;
208 case TYPE_LONG:
209 case TYPE_ULONG:
210 case TYPE_PTRVOID:
211 case TYPE_PTR:
212 if (is_host) {
213 return HOST_LONG_SIZE;
214 } else {
215 return TARGET_LONG_SIZE;
216 }
217 break;
218 case TYPE_ARRAY:
219 return thunk_type_align(type_ptr + 2, is_host);
220 case TYPE_STRUCT:
221 se = struct_entries + type_ptr[1];
222 return se->align[is_host];
223 default:
224 return -1;
225 }
226}
227
bellard0ad041d2003-06-25 22:11:41 +0000228#endif /* NO_THUNK_TYPE_SIZE */
229
bellard31e31b82003-02-18 22:55:36 +0000230unsigned int target_to_host_bitmask(unsigned int x86_mask,
231 bitmask_transtbl * trans_tbl);
232unsigned int host_to_target_bitmask(unsigned int alpha_mask,
233 bitmask_transtbl * trans_tbl);
234
235#endif