correct target_ulong definition
[qemu] / thunk.h
1 /*
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  */
20 #ifndef THUNK_H
21 #define THUNK_H
22
23 #include <inttypes.h>
24 #include "cpu.h"
25
26 #include "bswap.h"
27
28 #if defined(WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
29 #define BSWAP_NEEDED
30 #endif
31
32 #ifdef BSWAP_NEEDED
33
34 static inline uint16_t tswap16(uint16_t s)
35 {
36     return bswap16(s);
37 }
38
39 static inline uint32_t tswap32(uint32_t s)
40 {
41     return bswap32(s);
42 }
43
44 static inline uint64_t tswap64(uint64_t s)
45 {
46     return bswap64(s);
47 }
48
49 static inline void tswap16s(uint16_t *s)
50 {
51     *s = bswap16(*s);
52 }
53
54 static inline void tswap32s(uint32_t *s)
55 {
56     *s = bswap32(*s);
57 }
58
59 static inline void tswap64s(uint64_t *s)
60 {
61     *s = bswap64(*s);
62 }
63
64 #else
65
66 static inline uint16_t tswap16(uint16_t s)
67 {
68     return s;
69 }
70
71 static inline uint32_t tswap32(uint32_t s)
72 {
73     return s;
74 }
75
76 static inline uint64_t tswap64(uint64_t s)
77 {
78     return s;
79 }
80
81 static inline void tswap16s(uint16_t *s)
82 {
83 }
84
85 static inline void tswap32s(uint32_t *s)
86 {
87 }
88
89 static inline void tswap64s(uint64_t *s)
90 {
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
103 /* types enums definitions */
104
105 typedef 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
127 typedef 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... */
140 typedef 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
147 void thunk_register_struct(int id, const char *name, const argtype *types);
148 void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
149 const argtype *thunk_convert(void *dst, const void *src, 
150                              const argtype *type_ptr, int to_host);
151 #ifndef NO_THUNK_TYPE_SIZE
152
153 extern StructEntry struct_entries[];
154
155 static 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
192 static 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
228 #endif /* NO_THUNK_TYPE_SIZE */
229
230 unsigned int target_to_host_bitmask(unsigned int x86_mask, 
231                                     bitmask_transtbl * trans_tbl);
232 unsigned int host_to_target_bitmask(unsigned int alpha_mask, 
233                                     bitmask_transtbl * trans_tbl);
234
235 #endif