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
)
51 return thunk_type_next_ptr(type_ptr
);
53 return thunk_type_next_ptr(type_ptr
+ 1);
61 static const argtype
*thunk_type_next_ptr(const argtype
*type_ptr
)
63 return thunk_type_next(type_ptr
);
66 void thunk_register_struct(int id
, const char *name
, const argtype
*types
)
68 const argtype
*type_ptr
;
70 int nb_fields
, offset
, max_align
, align
, size
, i
, j
;
72 se
= struct_entries
+ id
;
74 /* first we count the number of fields */
77 while (*type_ptr
!= TYPE_NULL
) {
78 type_ptr
= thunk_type_next(type_ptr
);
81 se
->field_types
= types
;
82 se
->nb_fields
= nb_fields
;
85 printf("struct %s: id=%d nb_fields=%d\n",
86 se
->name
, id
, se
->nb_fields
);
88 /* now we can alloc the data */
90 for(i
= 0;i
< 2; i
++) {
93 se
->field_offsets
[i
] = malloc(nb_fields
* sizeof(int));
94 type_ptr
= se
->field_types
;
95 for(j
= 0;j
< nb_fields
; j
++) {
96 size
= thunk_type_size(type_ptr
, i
);
97 align
= thunk_type_align(type_ptr
, i
);
98 offset
= (offset
+ align
- 1) & ~(align
- 1);
99 se
->field_offsets
[i
][j
] = offset
;
101 if (align
> max_align
)
103 type_ptr
= thunk_type_next(type_ptr
);
105 offset
= (offset
+ max_align
- 1) & ~(max_align
- 1);
106 se
->size
[i
] = offset
;
107 se
->align
[i
] = max_align
;
109 printf("%s: size=%d align=%d\n",
110 i
== THUNK_HOST
? "host" : "target", offset
, max_align
);
115 void thunk_register_struct_direct(int id
, const char *name
,
116 const StructEntry
*se1
)
119 se
= struct_entries
+ id
;
125 /* now we can define the main conversion functions */
126 const argtype
*thunk_convert(void *dst
, const void *src
,
127 const argtype
*type_ptr
, int to_host
)
134 *(uint8_t *)dst
= *(uint8_t *)src
;
137 *(uint16_t *)dst
= tswap16(*(uint16_t *)src
);
140 *(uint32_t *)dst
= tswap32(*(uint32_t *)src
);
144 *(uint64_t *)dst
= tswap64(*(uint64_t *)src
);
146 #if HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 32
150 *(uint32_t *)dst
= tswap32(*(uint32_t *)src
);
152 #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 32
157 if (type
== TYPE_LONG
) {
159 *(uint64_t *)dst
= (int32_t)tswap32(*(uint32_t *)src
);
161 *(uint64_t *)dst
= tswap32(*(uint32_t *)src
);
164 *(uint32_t *)dst
= tswap32(*(uint64_t *)src
& 0xffffffff);
167 #elif HOST_LONG_BITS == 64 && TARGET_ABI_BITS == 64
171 *(uint64_t *)dst
= tswap64(*(uint64_t *)src
);
173 #elif HOST_LONG_BITS == 32 && TARGET_ABI_BITS == 64
178 *(uint32_t *)dst
= tswap64(*(uint64_t *)src
);
180 if (type
== TYPE_LONG
) {
182 *(uint64_t *)dst
= tswap64(*(int32_t *)src
);
184 *(uint64_t *)dst
= tswap64(*(uint32_t *)src
);
189 #warning unsupported conversion
193 int array_length
, i
, dst_size
, src_size
;
197 array_length
= *type_ptr
++;
198 dst_size
= thunk_type_size(type_ptr
, to_host
);
199 src_size
= thunk_type_size(type_ptr
, 1 - to_host
);
202 for(i
= 0;i
< array_length
; i
++) {
203 thunk_convert(d
, s
, type_ptr
, to_host
);
207 type_ptr
= thunk_type_next(type_ptr
);
213 const StructEntry
*se
;
216 const argtype
*field_types
;
217 const int *dst_offsets
, *src_offsets
;
219 se
= struct_entries
+ *type_ptr
++;
220 if (se
->convert
[0] != NULL
) {
221 /* specific conversion is needed */
222 (*se
->convert
[to_host
])(dst
, src
);
224 /* standard struct conversion */
225 field_types
= se
->field_types
;
226 dst_offsets
= se
->field_offsets
[to_host
];
227 src_offsets
= se
->field_offsets
[1 - to_host
];
230 for(i
= 0;i
< se
->nb_fields
; i
++) {
231 field_types
= thunk_convert(d
+ dst_offsets
[i
],
233 field_types
, to_host
);
239 fprintf(stderr
, "Invalid type 0x%x\n", type
);
247 /* Utility function: Table-driven functions to translate bitmasks
248 * between X86 and Alpha formats...
250 unsigned int target_to_host_bitmask(unsigned int x86_mask
,
251 const bitmask_transtbl
* trans_tbl
)
253 const bitmask_transtbl
*btp
;
254 unsigned int alpha_mask
= 0;
256 for(btp
= trans_tbl
; btp
->x86_mask
&& btp
->alpha_mask
; btp
++) {
257 if((x86_mask
& btp
->x86_mask
) == btp
->x86_bits
) {
258 alpha_mask
|= btp
->alpha_bits
;
264 unsigned int host_to_target_bitmask(unsigned int alpha_mask
,
265 const bitmask_transtbl
* trans_tbl
)
267 const bitmask_transtbl
*btp
;
268 unsigned int x86_mask
= 0;
270 for(btp
= trans_tbl
; btp
->x86_mask
&& btp
->alpha_mask
; btp
++) {
271 if((alpha_mask
& btp
->alpha_mask
) == btp
->alpha_bits
) {
272 x86_mask
|= btp
->x86_bits
;
278 #ifndef NO_THUNK_TYPE_SIZE
279 int thunk_type_size_array(const argtype
*type_ptr
, int is_host
)
281 return thunk_type_size(type_ptr
, is_host
);
284 int thunk_type_align_array(const argtype
*type_ptr
, int is_host
)
286 return thunk_type_align(type_ptr
, is_host
);
288 #endif /* ndef NO_THUNK_TYPE_SIZE */