0.6.1-alt1
[qemu] / 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 /* types enums definitions */
27
28 typedef enum argtype {
29     TYPE_NULL,
30     TYPE_CHAR,
31     TYPE_SHORT,
32     TYPE_INT,
33     TYPE_LONG,
34     TYPE_ULONG,
35     TYPE_PTRVOID, /* pointer on unknown data */
36     TYPE_LONGLONG,
37     TYPE_ULONGLONG,
38     TYPE_PTR,
39     TYPE_ARRAY,
40     TYPE_STRUCT,
41 } argtype;
42
43 #define MK_PTR(type) TYPE_PTR, type
44 #define MK_ARRAY(type, size) TYPE_ARRAY, size, type
45 #define MK_STRUCT(id) TYPE_STRUCT, id
46
47 #define THUNK_TARGET 0
48 #define THUNK_HOST   1
49
50 typedef struct {
51     /* standard struct handling */
52     const argtype *field_types;
53     int nb_fields;
54     int *field_offsets[2];
55     /* special handling */
56     void (*convert[2])(void *dst, const void *src);
57     int size[2];
58     int align[2];
59     const char *name;
60 } StructEntry;
61
62 /* Translation table for bitmasks... */
63 typedef struct bitmask_transtbl {
64         unsigned int    x86_mask;
65         unsigned int    x86_bits;
66         unsigned int    alpha_mask;
67         unsigned int    alpha_bits;
68 } bitmask_transtbl;
69
70 void thunk_register_struct(int id, const char *name, const argtype *types);
71 void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
72 const argtype *thunk_convert(void *dst, const void *src, 
73                              const argtype *type_ptr, int to_host);
74 #ifndef NO_THUNK_TYPE_SIZE
75
76 extern StructEntry struct_entries[];
77
78 static inline int thunk_type_size(const argtype *type_ptr, int is_host)
79 {
80     int type, size;
81     const StructEntry *se;
82
83     type = *type_ptr;
84     switch(type) {
85     case TYPE_CHAR:
86         return 1;
87     case TYPE_SHORT:
88         return 2;
89     case TYPE_INT:
90         return 4;
91     case TYPE_LONGLONG:
92     case TYPE_ULONGLONG:
93         return 8;
94     case TYPE_LONG:
95     case TYPE_ULONG:
96     case TYPE_PTRVOID:
97     case TYPE_PTR:
98         if (is_host) {
99             return HOST_LONG_SIZE;
100         } else {
101             return TARGET_LONG_SIZE;
102         }
103         break;
104     case TYPE_ARRAY:
105         size = type_ptr[1];
106         return size * thunk_type_size(type_ptr + 2, is_host);
107     case TYPE_STRUCT:
108         se = struct_entries + type_ptr[1];
109         return se->size[is_host];
110     default:
111         return -1;
112     }
113 }
114
115 static inline int thunk_type_align(const argtype *type_ptr, int is_host)
116 {
117     int type;
118     const StructEntry *se;
119
120     type = *type_ptr;
121     switch(type) {
122     case TYPE_CHAR:
123         return 1;
124     case TYPE_SHORT:
125         return 2;
126     case TYPE_INT:
127         return 4;
128     case TYPE_LONGLONG:
129     case TYPE_ULONGLONG:
130         return 8;
131     case TYPE_LONG:
132     case TYPE_ULONG:
133     case TYPE_PTRVOID:
134     case TYPE_PTR:
135         if (is_host) {
136             return HOST_LONG_SIZE;
137         } else {
138             return TARGET_LONG_SIZE;
139         }
140         break;
141     case TYPE_ARRAY:
142         return thunk_type_align(type_ptr + 2, is_host);
143     case TYPE_STRUCT:
144         se = struct_entries + type_ptr[1];
145         return se->align[is_host];
146     default:
147         return -1;
148     }
149 }
150
151 #endif /* NO_THUNK_TYPE_SIZE */
152
153 unsigned int target_to_host_bitmask(unsigned int x86_mask, 
154                                     bitmask_transtbl * trans_tbl);
155 unsigned int host_to_target_bitmask(unsigned int alpha_mask, 
156                                     bitmask_transtbl * trans_tbl);
157
158 #endif