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
29 #define MAX_STRUCTS 128
31 /* XXX: make it dynamic */
32 StructEntry struct_entries[MAX_STRUCTS];
34 static inline const argtype *thunk_type_next(const argtype *type_ptr)
50 return thunk_type_next(type_ptr);
52 return thunk_type_next(type_ptr + 1);
60 void thunk_register_struct(int id, const char *name, const argtype *types)
62 const argtype *type_ptr;
64 int nb_fields, offset, max_align, align, size, i, j;
66 se = struct_entries + id;
68 /* first we count the number of fields */
71 while (*type_ptr != TYPE_NULL) {
72 type_ptr = thunk_type_next(type_ptr);
75 se->field_types = types;
76 se->nb_fields = nb_fields;
79 printf("struct %s: id=%d nb_fields=%d\n",
80 se->name, id, se->nb_fields);
82 /* now we can alloc the data */
84 for(i = 0;i < 2; i++) {
87 se->field_offsets[i] = malloc(nb_fields * sizeof(int));
88 type_ptr = se->field_types;
89 for(j = 0;j < nb_fields; j++) {
90 size = thunk_type_size(type_ptr, i);
91 align = thunk_type_align(type_ptr, i);
92 offset = (offset + align - 1) & ~(align - 1);
93 se->field_offsets[i][j] = offset;
95 if (align > max_align)
97 type_ptr = thunk_type_next(type_ptr);
99 offset = (offset + max_align - 1) & ~(max_align - 1);
100 se->size[i] = offset;
101 se->align[i] = max_align;
103 printf("%s: size=%d align=%d\n",
104 i == THUNK_HOST ? "host" : "target", offset, max_align);
109 void thunk_register_struct_direct(int id, const char *name, StructEntry *se1)
112 se = struct_entries + id;
118 /* now we can define the main conversion functions */
119 const argtype *thunk_convert(void *dst, const void *src,
120 const argtype *type_ptr, int to_host)
127 *(uint8_t *)dst = *(uint8_t *)src;
130 *(uint16_t *)dst = tswap16(*(uint16_t *)src);
133 *(uint32_t *)dst = tswap32(*(uint32_t *)src);
137 *(uint64_t *)dst = tswap64(*(uint64_t *)src);
139 #if HOST_LONG_BITS == 32 && TARGET_LONG_BITS == 32
143 *(uint32_t *)dst = tswap32(*(uint32_t *)src);
145 #elif HOST_LONG_BITS == 64 && TARGET_LONG_BITS == 32
150 *(uint64_t *)dst = tswap32(*(uint32_t *)src);
152 *(uint32_t *)dst = tswap32(*(uint64_t *)src & 0xffffffff);
156 #warning unsupported conversion
160 int array_length, i, dst_size, src_size;
164 array_length = *type_ptr++;
165 dst_size = thunk_type_size(type_ptr, to_host);
166 src_size = thunk_type_size(type_ptr, 1 - to_host);
169 for(i = 0;i < array_length; i++) {
170 thunk_convert(d, s, type_ptr, to_host);
174 type_ptr = thunk_type_next(type_ptr);
180 const StructEntry *se;
183 const argtype *field_types;
184 const int *dst_offsets, *src_offsets;
186 se = struct_entries + *type_ptr++;
187 if (se->convert[0] != NULL) {
188 /* specific conversion is needed */
189 (*se->convert[to_host])(dst, src);
191 /* standard struct conversion */
192 field_types = se->field_types;
193 dst_offsets = se->field_offsets[to_host];
194 src_offsets = se->field_offsets[1 - to_host];
197 for(i = 0;i < se->nb_fields; i++) {
198 field_types = thunk_convert(d + dst_offsets[i],
200 field_types, to_host);
206 fprintf(stderr, "Invalid type 0x%x\n", type);
214 /* Utility function: Table-driven functions to translate bitmasks
215 * between X86 and Alpha formats...
217 unsigned int target_to_host_bitmask(unsigned int x86_mask,
218 bitmask_transtbl * trans_tbl)
220 bitmask_transtbl * btp;
221 unsigned int alpha_mask = 0;
223 for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
224 if((x86_mask & btp->x86_mask) == btp->x86_bits) {
225 alpha_mask |= btp->alpha_bits;
231 unsigned int host_to_target_bitmask(unsigned int alpha_mask,
232 bitmask_transtbl * trans_tbl)
234 bitmask_transtbl * btp;
235 unsigned int x86_mask = 0;
237 for(btp = trans_tbl; btp->x86_mask && btp->alpha_mask; btp++) {
238 if((alpha_mask & btp->alpha_mask) == btp->alpha_bits) {
239 x86_mask |= btp->x86_bits;