root / thunk.h @ 20495218
History | View | Annotate | Download (4 kB)
1 | 3ef693a0 | bellard | /*
|
---|---|---|---|
2 | 3ef693a0 | bellard | * Generic thunking code to convert data between host and target CPU
|
3 | 3ef693a0 | bellard | *
|
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 | 3ef693a0 | bellard | * License along with this library; if not, write to the Free Software
|
18 | 3ef693a0 | bellard | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
19 | 3ef693a0 | bellard | */
|
20 | 31e31b8a | bellard | #ifndef THUNK_H
|
21 | 31e31b8a | bellard | #define THUNK_H
|
22 | 31e31b8a | bellard | |
23 | 31e31b8a | bellard | #include <inttypes.h> |
24 | 35b66fc4 | bellard | #include "cpu.h" |
25 | 34313956 | bellard | |
26 | 31e31b8a | bellard | /* types enums definitions */
|
27 | 31e31b8a | bellard | |
28 | 31e31b8a | bellard | typedef enum argtype { |
29 | 31e31b8a | bellard | TYPE_NULL, |
30 | 31e31b8a | bellard | TYPE_CHAR, |
31 | 31e31b8a | bellard | TYPE_SHORT, |
32 | 31e31b8a | bellard | TYPE_INT, |
33 | 31e31b8a | bellard | TYPE_LONG, |
34 | 31e31b8a | bellard | TYPE_ULONG, |
35 | 31e31b8a | bellard | TYPE_PTRVOID, /* pointer on unknown data */
|
36 | 31e31b8a | bellard | TYPE_LONGLONG, |
37 | 31e31b8a | bellard | TYPE_ULONGLONG, |
38 | 31e31b8a | bellard | TYPE_PTR, |
39 | 31e31b8a | bellard | TYPE_ARRAY, |
40 | 31e31b8a | bellard | TYPE_STRUCT, |
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 | 31e31b8a | bellard | void thunk_register_struct_direct(int id, const char *name, StructEntry *se1); |
72 | 31e31b8a | bellard | const argtype *thunk_convert(void *dst, const void *src, |
73 | 31e31b8a | bellard | const argtype *type_ptr, int to_host); |
74 | 0ad041d4 | bellard | #ifndef NO_THUNK_TYPE_SIZE
|
75 | 31e31b8a | bellard | |
76 | 24374901 | bellard | extern StructEntry struct_entries[];
|
77 | 24374901 | bellard | |
78 | 24374901 | bellard | static inline int thunk_type_size(const argtype *type_ptr, int is_host) |
79 | 24374901 | bellard | { |
80 | 24374901 | bellard | int type, size;
|
81 | 24374901 | bellard | const StructEntry *se;
|
82 | 24374901 | bellard | |
83 | 24374901 | bellard | type = *type_ptr; |
84 | 24374901 | bellard | switch(type) {
|
85 | 24374901 | bellard | case TYPE_CHAR:
|
86 | 24374901 | bellard | return 1; |
87 | 24374901 | bellard | case TYPE_SHORT:
|
88 | 24374901 | bellard | return 2; |
89 | 24374901 | bellard | case TYPE_INT:
|
90 | 24374901 | bellard | return 4; |
91 | 24374901 | bellard | case TYPE_LONGLONG:
|
92 | 24374901 | bellard | case TYPE_ULONGLONG:
|
93 | 24374901 | bellard | return 8; |
94 | 24374901 | bellard | case TYPE_LONG:
|
95 | 24374901 | bellard | case TYPE_ULONG:
|
96 | 24374901 | bellard | case TYPE_PTRVOID:
|
97 | 24374901 | bellard | case TYPE_PTR:
|
98 | 24374901 | bellard | if (is_host) {
|
99 | 24374901 | bellard | return HOST_LONG_SIZE;
|
100 | 24374901 | bellard | } else {
|
101 | 24374901 | bellard | return TARGET_LONG_SIZE;
|
102 | 24374901 | bellard | } |
103 | 24374901 | bellard | break;
|
104 | 24374901 | bellard | case TYPE_ARRAY:
|
105 | 24374901 | bellard | size = type_ptr[1];
|
106 | 24374901 | bellard | return size * thunk_type_size(type_ptr + 2, is_host); |
107 | 24374901 | bellard | case TYPE_STRUCT:
|
108 | 24374901 | bellard | se = struct_entries + type_ptr[1];
|
109 | 24374901 | bellard | return se->size[is_host];
|
110 | 24374901 | bellard | default:
|
111 | 24374901 | bellard | return -1; |
112 | 24374901 | bellard | } |
113 | 24374901 | bellard | } |
114 | 24374901 | bellard | |
115 | 24374901 | bellard | static inline int thunk_type_align(const argtype *type_ptr, int is_host) |
116 | 24374901 | bellard | { |
117 | 24374901 | bellard | int type;
|
118 | 24374901 | bellard | const StructEntry *se;
|
119 | 24374901 | bellard | |
120 | 24374901 | bellard | type = *type_ptr; |
121 | 24374901 | bellard | switch(type) {
|
122 | 24374901 | bellard | case TYPE_CHAR:
|
123 | 24374901 | bellard | return 1; |
124 | 24374901 | bellard | case TYPE_SHORT:
|
125 | 24374901 | bellard | return 2; |
126 | 24374901 | bellard | case TYPE_INT:
|
127 | 24374901 | bellard | return 4; |
128 | 24374901 | bellard | case TYPE_LONGLONG:
|
129 | 24374901 | bellard | case TYPE_ULONGLONG:
|
130 | 24374901 | bellard | return 8; |
131 | 24374901 | bellard | case TYPE_LONG:
|
132 | 24374901 | bellard | case TYPE_ULONG:
|
133 | 24374901 | bellard | case TYPE_PTRVOID:
|
134 | 24374901 | bellard | case TYPE_PTR:
|
135 | 24374901 | bellard | if (is_host) {
|
136 | 24374901 | bellard | return HOST_LONG_SIZE;
|
137 | 24374901 | bellard | } else {
|
138 | 24374901 | bellard | return TARGET_LONG_SIZE;
|
139 | 24374901 | bellard | } |
140 | 24374901 | bellard | break;
|
141 | 24374901 | bellard | case TYPE_ARRAY:
|
142 | 24374901 | bellard | return thunk_type_align(type_ptr + 2, is_host); |
143 | 24374901 | bellard | case TYPE_STRUCT:
|
144 | 24374901 | bellard | se = struct_entries + type_ptr[1];
|
145 | 24374901 | bellard | return se->align[is_host];
|
146 | 24374901 | bellard | default:
|
147 | 24374901 | bellard | return -1; |
148 | 24374901 | bellard | } |
149 | 24374901 | bellard | } |
150 | 24374901 | bellard | |
151 | 0ad041d4 | bellard | #endif /* NO_THUNK_TYPE_SIZE */ |
152 | 0ad041d4 | bellard | |
153 | 31e31b8a | bellard | unsigned int target_to_host_bitmask(unsigned int x86_mask, |
154 | 31e31b8a | bellard | bitmask_transtbl * trans_tbl); |
155 | 31e31b8a | bellard | unsigned int host_to_target_bitmask(unsigned int alpha_mask, |
156 | 31e31b8a | bellard | bitmask_transtbl * trans_tbl); |
157 | 31e31b8a | bellard | |
158 | 31e31b8a | bellard | #endif |