Statistics
| Branch: | Revision:

root / thunk.h @ 5e1d0985

History | View | Annotate | Download (4.2 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 3ef693a0 bellard
 * License along with this library; if not, write to the Free Software
18 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 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 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 24374901 bellard
            return HOST_LONG_SIZE;
104 24374901 bellard
        } else {
105 992f48a0 blueswir1
            return TARGET_ABI_BITS / 8;
106 24374901 bellard
        }
107 24374901 bellard
        break;
108 24374901 bellard
    case TYPE_ARRAY:
109 24374901 bellard
        size = type_ptr[1];
110 26553115 j_mayer
        return size * thunk_type_size_array(type_ptr + 2, is_host);
111 24374901 bellard
    case TYPE_STRUCT:
112 24374901 bellard
        se = struct_entries + type_ptr[1];
113 24374901 bellard
        return se->size[is_host];
114 24374901 bellard
    default:
115 24374901 bellard
        return -1;
116 24374901 bellard
    }
117 24374901 bellard
}
118 24374901 bellard
119 24374901 bellard
static inline int thunk_type_align(const argtype *type_ptr, int is_host)
120 24374901 bellard
{
121 24374901 bellard
    int type;
122 24374901 bellard
    const StructEntry *se;
123 24374901 bellard
124 24374901 bellard
    type = *type_ptr;
125 24374901 bellard
    switch(type) {
126 24374901 bellard
    case TYPE_CHAR:
127 24374901 bellard
        return 1;
128 24374901 bellard
    case TYPE_SHORT:
129 24374901 bellard
        return 2;
130 24374901 bellard
    case TYPE_INT:
131 24374901 bellard
        return 4;
132 24374901 bellard
    case TYPE_LONGLONG:
133 24374901 bellard
    case TYPE_ULONGLONG:
134 24374901 bellard
        return 8;
135 24374901 bellard
    case TYPE_LONG:
136 24374901 bellard
    case TYPE_ULONG:
137 24374901 bellard
    case TYPE_PTRVOID:
138 24374901 bellard
    case TYPE_PTR:
139 24374901 bellard
        if (is_host) {
140 24374901 bellard
            return HOST_LONG_SIZE;
141 24374901 bellard
        } else {
142 992f48a0 blueswir1
            return TARGET_ABI_BITS / 8;
143 24374901 bellard
        }
144 24374901 bellard
        break;
145 24374901 bellard
    case TYPE_ARRAY:
146 26553115 j_mayer
        return thunk_type_align_array(type_ptr + 2, is_host);
147 24374901 bellard
    case TYPE_STRUCT:
148 24374901 bellard
        se = struct_entries + type_ptr[1];
149 24374901 bellard
        return se->align[is_host];
150 24374901 bellard
    default:
151 24374901 bellard
        return -1;
152 24374901 bellard
    }
153 24374901 bellard
}
154 24374901 bellard
155 0ad041d4 bellard
#endif /* NO_THUNK_TYPE_SIZE */
156 0ad041d4 bellard
157 5fafdf24 ths
unsigned int target_to_host_bitmask(unsigned int x86_mask,
158 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl);
159 5fafdf24 ths
unsigned int host_to_target_bitmask(unsigned int alpha_mask,
160 b39bc503 blueswir1
                                    const bitmask_transtbl * trans_tbl);
161 31e31b8a bellard
162 31e31b8a bellard
#endif