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, see <http://www.gnu.org/licenses/>.
28 #define MAX_STRUCTS 128
30 /* XXX: make it dynamic */
31 StructEntry struct_entries
[MAX_STRUCTS
];
33 static const argtype
*thunk_type_next_ptr(const argtype
*type_ptr
);
35 static inline const argtype
*thunk_type_next(const argtype
*type_ptr
)
52 return thunk_type_next_ptr(type_ptr
);
54 return thunk_type_next_ptr(type_ptr
+ 1);
62 static const argtype
*thunk_type_next_ptr(const argtype
*type_ptr
)
64 return thunk_type_next(type_ptr
);
67 void thunk_register_struct(int id
, const char *name
, const argtype
*types
)
69 const argtype
*type_ptr
;
71 int nb_fields
, offset
, max_align
, align
, size
, i
, j
;
73 se
= struct_entries
+ id
;
75 /* first we count the number of fields */
78 while (*type_ptr
!= TYPE_NULL
) {
79 type_ptr
= thunk_type_next(type_ptr
);
82 se
->field_types
= types
;
83 se
->nb_fields
= nb_fields
;
86 printf("struct %s: id=%d nb_fields=%d\n",
87 se
->name
, id
, se
->nb_fields
);
89 /* now we can alloc the data */
91 for(i
= 0;i
< 2; i
++) {
94 se
->field_offsets
[i
] = malloc(nb_fields
* sizeof(int));
95 type_ptr
= se
->field_types
;
96 for(j
= 0;j
< nb_fields
; j
++) {
97 size
= thunk_type_size(type_ptr
, i
);
98 align
= thunk_type_align(type_ptr
, i
);
99 offset
= (offset
+ align
- 1) & ~(align
- 1);
100 se
->field_offsets
[i
][j
] = offset
;
102 if (align
> max_align
)
104 type_ptr
= thunk_type_next(type_ptr
);
106 offset
= (offset
+ max_align
- 1) & ~(max_align
- 1);
107 se
->size
[i
] = offset
;
108 se
->align
[i
] = max_align
;
110 printf("%s: size=%d align=%d\n",
111 i
== THUNK_HOST
? "host" : "target", offset
, max_align
);
116 void thunk_register_struct_direct(int id
, const char *name
,
117 const StructEntry
*se1
)
120 se
= struct_entries
+ id
;
126 /* now we can define the main conversion functions */
127 const argtype
*thunk_convert(void *dst
, const void *src
,
128 const argtype
*type_ptr
, int to_host
)
135 *(uint8_t *)dst
= *(uint8_t *)src
;
138 *(uint16_t *)dst
= tswap16(*(uint16_t *)src
);
141 *(uint32_t *)dst
= tswap32(*(uint32_t *)src
);
145 *(uint64_t *)dst
= tswap64(*(uint64_t *)src
);
147 #if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
151 *(uint32_t *)dst
= tswap32(*(uint32_t *)src
);
153 #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
158 if (type
== TYPE_LONG
) {
160 *(uint64_t *)dst
= (int32_t)tswap32(*(uint32_t *)src
);
162 *(uint64_t *)dst
= tswap32(*(uint32_t *)src
);
165 *(uint32_t *)dst
= tswap32(*(uint64_t *)src
& 0xffffffff);
168 #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
172 *(uint64_t *)dst
= tswap64(*(uint64_t *)src
);
174 #elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64
179 *(uint32_t *)dst
= tswap64(*(uint64_t *)src
);
181 if (type
== TYPE_LONG
) {
183 *(uint64_t *)dst
= tswap64(*(int32_t *)src
);
185 *(uint64_t *)dst
= tswap64(*(uint32_t *)src
);
190 #warning unsupported conversion
195 switch (thunk_type_size(type_ptr
- 1, !to_host
)) {
197 val
= *(uint16_t *)src
;
200 val
= *(uint32_t *)src
;
203 val
= *(uint64_t *)src
;
206 switch (thunk_type_size(type_ptr
- 1, to_host
)) {
208 *(uint16_t *)dst
= tswap16(val
);
211 *(uint32_t *)dst
= tswap32(val
);
214 *(uint64_t *)dst
= tswap64(val
);
221 int array_length
, i
, dst_size
, src_size
;
225 array_length
= *type_ptr
++;
226 dst_size
= thunk_type_size(type_ptr
, to_host
);
227 src_size
= thunk_type_size(type_ptr
, 1 - to_host
);
230 for(i
= 0;i
< array_length
; i
++) {
231 thunk_convert(d
, s
, type_ptr
, to_host
);
235 type_ptr
= thunk_type_next(type_ptr
);
241 const StructEntry
*se
;
244 const argtype
*field_types
;
245 const int *dst_offsets
, *src_offsets
;
247 se
= struct_entries
+ *type_ptr
++;
248 if (se
->convert
[0] != NULL
) {
249 /* specific conversion is needed */
250 (*se
->convert
[to_host
])(dst
, src
);
252 /* standard struct conversion */
253 field_types
= se
->field_types
;
254 dst_offsets
= se
->field_offsets
[to_host
];
255 src_offsets
= se
->field_offsets
[1 - to_host
];
258 for(i
= 0;i
< se
->nb_fields
; i
++) {
259 field_types
= thunk_convert(d
+ dst_offsets
[i
],
261 field_types
, to_host
);
267 fprintf(stderr
, "Invalid type 0x%x\n", type
);
275 /* Utility function: Table-driven functions to translate bitmasks
276 * between X86 and Alpha formats...
278 unsigned int target_to_host_bitmask(unsigned int x86_mask
,
279 const bitmask_transtbl
* trans_tbl
)
281 const bitmask_transtbl
*btp
;
282 unsigned int alpha_mask
= 0;
284 for(btp
= trans_tbl
; btp
->x86_mask
&& btp
->alpha_mask
; btp
++) {
285 if((x86_mask
& btp
->x86_mask
) == btp
->x86_bits
) {
286 alpha_mask
|= btp
->alpha_bits
;
292 unsigned int host_to_target_bitmask(unsigned int alpha_mask
,
293 const bitmask_transtbl
* trans_tbl
)
295 const bitmask_transtbl
*btp
;
296 unsigned int x86_mask
= 0;
298 for(btp
= trans_tbl
; btp
->x86_mask
&& btp
->alpha_mask
; btp
++) {
299 if((alpha_mask
& btp
->alpha_mask
) == btp
->alpha_bits
) {
300 x86_mask
|= btp
->x86_bits
;
306 #ifndef NO_THUNK_TYPE_SIZE
307 int thunk_type_size_array(const argtype
*type_ptr
, int is_host
)
309 return thunk_type_size(type_ptr
, is_host
);
312 int thunk_type_align_array(const argtype
*type_ptr
, int is_host
)
314 return thunk_type_align(type_ptr
, is_host
);
316 #endif /* ndef NO_THUNK_TYPE_SIZE */