Statistics
| Branch: | Revision:

root / thunk.h @ fec90ff0

History | View | Annotate | Download (4.9 kB)

1 3ef693a0 bellard
/*
2 3ef693a0 bellard
 *  Generic thunking code to convert data between host and target CPU
3 5fafdf24 ths
 *
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 8167ee88 Blue Swirl
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 3ef693a0 bellard
 */
19 31e31b8a bellard
#ifndef THUNK_H
20 31e31b8a bellard
#define THUNK_H
21 31e31b8a bellard
22 31e31b8a bellard
#include <inttypes.h>
23 35b66fc4 bellard
#include "cpu.h"
24 34313956 bellard
25 31e31b8a bellard
/* types enums definitions */
26 31e31b8a bellard
27 31e31b8a bellard
typedef enum argtype {
28 31e31b8a bellard
    TYPE_NULL,
29 31e31b8a bellard
    TYPE_CHAR,
30 31e31b8a bellard
    TYPE_SHORT,
31 31e31b8a bellard
    TYPE_INT,
32 31e31b8a bellard
    TYPE_LONG,
33 31e31b8a bellard
    TYPE_ULONG,
34 31e31b8a bellard
    TYPE_PTRVOID, /* pointer on unknown data */
35 31e31b8a bellard
    TYPE_LONGLONG,
36 31e31b8a bellard
    TYPE_ULONGLONG,
37 31e31b8a bellard
    TYPE_PTR,
38 31e31b8a bellard
    TYPE_ARRAY,
39 31e31b8a bellard
    TYPE_STRUCT,
40 6083abd9 Alexander Graf
    TYPE_OLDDEVT,
41 31e31b8a bellard
} argtype;
42 31e31b8a bellard
43 31e31b8a bellard
#define MK_PTR(type) TYPE_PTR, type
44 31e31b8a bellard
#define MK_ARRAY(type, size) TYPE_ARRAY, size, type
45 31e31b8a bellard
#define MK_STRUCT(id) TYPE_STRUCT, id
46 31e31b8a bellard
47 31e31b8a bellard
#define THUNK_TARGET 0
48 31e31b8a bellard
#define THUNK_HOST   1
49 31e31b8a bellard
50 31e31b8a bellard
typedef struct {
51 31e31b8a bellard
    /* standard struct handling */
52 31e31b8a bellard
    const argtype *field_types;
53 31e31b8a bellard
    int nb_fields;
54 31e31b8a bellard
    int *field_offsets[2];
55 31e31b8a bellard
    /* special handling */
56 31e31b8a bellard
    void (*convert[2])(void *dst, const void *src);
57 31e31b8a bellard
    int size[2];
58 31e31b8a bellard
    int align[2];
59 31e31b8a bellard
    const char *name;
60 31e31b8a bellard
} StructEntry;
61 31e31b8a bellard
62 31e31b8a bellard
/* Translation table for bitmasks... */
63 31e31b8a bellard
typedef struct bitmask_transtbl {
64 31e31b8a bellard
        unsigned int        x86_mask;
65 31e31b8a bellard
        unsigned int        x86_bits;
66 31e31b8a bellard
        unsigned int        alpha_mask;
67 31e31b8a bellard
        unsigned int        alpha_bits;
68 31e31b8a bellard
} bitmask_transtbl;
69 31e31b8a bellard
70 31e31b8a bellard
void thunk_register_struct(int id, const char *name, const argtype *types);
71 8e853dc7 blueswir1
void thunk_register_struct_direct(int id, const char *name,
72 8e853dc7 blueswir1
                                  const StructEntry *se1);
73 5fafdf24 ths
const argtype *thunk_convert(void *dst, const void *src,
74 31e31b8a bellard
                             const argtype *type_ptr, int to_host);
75 0ad041d4 bellard
#ifndef NO_THUNK_TYPE_SIZE
76 31e31b8a bellard
77 24374901 bellard
extern StructEntry struct_entries[];
78 24374901 bellard
79 26553115 j_mayer
int thunk_type_size_array(const argtype *type_ptr, int is_host);
80 26553115 j_mayer
int thunk_type_align_array(const argtype *type_ptr, int is_host);
81 26553115 j_mayer
82 24374901 bellard
static inline int thunk_type_size(const argtype *type_ptr, int is_host)
83 24374901 bellard
{
84 24374901 bellard
    int type, size;
85 24374901 bellard
    const StructEntry *se;
86 24374901 bellard
87 24374901 bellard
    type = *type_ptr;
88 24374901 bellard
    switch(type) {
89 24374901 bellard
    case TYPE_CHAR:
90 24374901 bellard
        return 1;
91 24374901 bellard
    case TYPE_SHORT:
92 24374901 bellard
        return 2;
93 24374901 bellard
    case TYPE_INT:
94 24374901 bellard
        return 4;
95 24374901 bellard
    case TYPE_LONGLONG:
96 24374901 bellard
    case TYPE_ULONGLONG:
97 24374901 bellard
        return 8;
98 24374901 bellard
    case TYPE_LONG:
99 24374901 bellard
    case TYPE_ULONG:
100 24374901 bellard
    case TYPE_PTRVOID:
101 24374901 bellard
    case TYPE_PTR:
102 24374901 bellard
        if (is_host) {
103 9c6ecf3e Stefan Weil
            return sizeof(void *);
104 24374901 bellard
        } else {
105 992f48a0 blueswir1
            return TARGET_ABI_BITS / 8;
106 24374901 bellard
        }
107 24374901 bellard
        break;
108 6083abd9 Alexander Graf
    case TYPE_OLDDEVT:
109 6083abd9 Alexander Graf
        if (is_host) {
110 6083abd9 Alexander Graf
#if defined(HOST_X86_64)
111 6083abd9 Alexander Graf
            return 8;
112 6083abd9 Alexander Graf
#elif defined(HOST_ALPHA) || defined(HOST_IA64) || defined(HOST_MIPS) || \
113 6083abd9 Alexander Graf
      defined(HOST_PARISC) || defined(HOST_SPARC64)
114 6083abd9 Alexander Graf
            return 4;
115 6083abd9 Alexander Graf
#elif defined(HOST_PPC)
116 65074706 Alexander Graf
            return sizeof(void *);
117 6083abd9 Alexander Graf
#else
118 6083abd9 Alexander Graf
            return 2;
119 6083abd9 Alexander Graf
#endif
120 6083abd9 Alexander Graf
        } else {
121 6083abd9 Alexander Graf
#if defined(TARGET_X86_64)
122 6083abd9 Alexander Graf
            return 8;
123 6083abd9 Alexander Graf
#elif defined(TARGET_ALPHA) || defined(TARGET_IA64) || defined(TARGET_MIPS) || \
124 6083abd9 Alexander Graf
      defined(TARGET_PARISC) || defined(TARGET_SPARC64)
125 6083abd9 Alexander Graf
            return 4;
126 6083abd9 Alexander Graf
#elif defined(TARGET_PPC)
127 6083abd9 Alexander Graf
            return TARGET_ABI_BITS / 8;
128 6083abd9 Alexander Graf
#else
129 6083abd9 Alexander Graf
            return 2;
130 6083abd9 Alexander Graf
#endif
131 6083abd9 Alexander Graf
        }
132 6083abd9 Alexander Graf
        break;
133 24374901 bellard
    case TYPE_ARRAY:
134 24374901 bellard
        size = type_ptr[1];
135 26553115 j_mayer
        return size * thunk_type_size_array(type_ptr + 2, is_host);
136 24374901 bellard
    case TYPE_STRUCT:
137 24374901 bellard
        se = struct_entries + type_ptr[1];
138 24374901 bellard
        return se->size[is_host];
139 24374901 bellard
    default:
140 24374901 bellard
        return -1;
141 24374901 bellard
    }
142 24374901 bellard
}
143 24374901 bellard
144 24374901 bellard
static inline int thunk_type_align(const argtype *type_ptr, int is_host)
145 24374901 bellard
{
146 24374901 bellard
    int type;
147 24374901 bellard
    const StructEntry *se;
148 24374901 bellard
149 24374901 bellard
    type = *type_ptr;
150 24374901 bellard
    switch(type) {
151 24374901 bellard
    case TYPE_CHAR:
152 24374901 bellard
        return 1;
153 24374901 bellard
    case TYPE_SHORT:
154 24374901 bellard
        return 2;
155 24374901 bellard
    case TYPE_INT:
156 24374901 bellard
        return 4;
157 24374901 bellard
    case TYPE_LONGLONG:
158 24374901 bellard
    case TYPE_ULONGLONG:
159 24374901 bellard
        return 8;
160 24374901 bellard
    case TYPE_LONG:
161 24374901 bellard
    case TYPE_ULONG:
162 24374901 bellard
    case TYPE_PTRVOID:
163 24374901 bellard
    case TYPE_PTR:
164 24374901 bellard
        if (is_host) {
165 9c6ecf3e Stefan Weil
            return sizeof(void *);
166 24374901 bellard
        } else {
167 992f48a0 blueswir1
            return TARGET_ABI_BITS / 8;
168 24374901 bellard
        }
169 24374901 bellard
        break;
170 6083abd9 Alexander Graf
    case TYPE_OLDDEVT:
171 6083abd9 Alexander Graf
        return thunk_type_size(type_ptr, is_host);
172 24374901 bellard
    case TYPE_ARRAY:
173 26553115 j_mayer
        return thunk_type_align_array(type_ptr + 2, is_host);
174 24374901 bellard
    case TYPE_STRUCT:
175 24374901 bellard
        se = struct_entries + type_ptr[1];
176 24374901 bellard
        return se->align[is_host];
177 24374901 bellard
    default:
178 24374901 bellard
        return -1;
179 24374901 bellard
    }
180 24374901 bellard
}
181 24374901 bellard
182 0ad041d4 bellard
#endif /* NO_THUNK_TYPE_SIZE */
183 0ad041d4 bellard
184 5fafdf24 ths
unsigned int target_to_host_bitmask(unsigned int x86_mask,
185 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl);
186 5fafdf24 ths
unsigned int host_to_target_bitmask(unsigned int alpha_mask,
187 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl);
188 31e31b8a bellard
189 31e31b8a bellard
#endif