Statistics
| Branch: | Revision:

root / thunk.h @ 8c5e95d8

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 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 5fafdf24 ths
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 26553115 j_mayer
int thunk_type_size_array(const argtype *type_ptr, int is_host);
79 26553115 j_mayer
int thunk_type_align_array(const argtype *type_ptr, int is_host);
80 26553115 j_mayer
81 24374901 bellard
static inline int thunk_type_size(const argtype *type_ptr, int is_host)
82 24374901 bellard
{
83 24374901 bellard
    int type, size;
84 24374901 bellard
    const StructEntry *se;
85 24374901 bellard
86 24374901 bellard
    type = *type_ptr;
87 24374901 bellard
    switch(type) {
88 24374901 bellard
    case TYPE_CHAR:
89 24374901 bellard
        return 1;
90 24374901 bellard
    case TYPE_SHORT:
91 24374901 bellard
        return 2;
92 24374901 bellard
    case TYPE_INT:
93 24374901 bellard
        return 4;
94 24374901 bellard
    case TYPE_LONGLONG:
95 24374901 bellard
    case TYPE_ULONGLONG:
96 24374901 bellard
        return 8;
97 24374901 bellard
    case TYPE_LONG:
98 24374901 bellard
    case TYPE_ULONG:
99 24374901 bellard
    case TYPE_PTRVOID:
100 24374901 bellard
    case TYPE_PTR:
101 24374901 bellard
        if (is_host) {
102 24374901 bellard
            return HOST_LONG_SIZE;
103 24374901 bellard
        } else {
104 992f48a0 blueswir1
            return TARGET_ABI_BITS / 8;
105 24374901 bellard
        }
106 24374901 bellard
        break;
107 24374901 bellard
    case TYPE_ARRAY:
108 24374901 bellard
        size = type_ptr[1];
109 26553115 j_mayer
        return size * thunk_type_size_array(type_ptr + 2, is_host);
110 24374901 bellard
    case TYPE_STRUCT:
111 24374901 bellard
        se = struct_entries + type_ptr[1];
112 24374901 bellard
        return se->size[is_host];
113 24374901 bellard
    default:
114 24374901 bellard
        return -1;
115 24374901 bellard
    }
116 24374901 bellard
}
117 24374901 bellard
118 24374901 bellard
static inline int thunk_type_align(const argtype *type_ptr, int is_host)
119 24374901 bellard
{
120 24374901 bellard
    int type;
121 24374901 bellard
    const StructEntry *se;
122 24374901 bellard
123 24374901 bellard
    type = *type_ptr;
124 24374901 bellard
    switch(type) {
125 24374901 bellard
    case TYPE_CHAR:
126 24374901 bellard
        return 1;
127 24374901 bellard
    case TYPE_SHORT:
128 24374901 bellard
        return 2;
129 24374901 bellard
    case TYPE_INT:
130 24374901 bellard
        return 4;
131 24374901 bellard
    case TYPE_LONGLONG:
132 24374901 bellard
    case TYPE_ULONGLONG:
133 24374901 bellard
        return 8;
134 24374901 bellard
    case TYPE_LONG:
135 24374901 bellard
    case TYPE_ULONG:
136 24374901 bellard
    case TYPE_PTRVOID:
137 24374901 bellard
    case TYPE_PTR:
138 24374901 bellard
        if (is_host) {
139 24374901 bellard
            return HOST_LONG_SIZE;
140 24374901 bellard
        } else {
141 992f48a0 blueswir1
            return TARGET_ABI_BITS / 8;
142 24374901 bellard
        }
143 24374901 bellard
        break;
144 24374901 bellard
    case TYPE_ARRAY:
145 26553115 j_mayer
        return thunk_type_align_array(type_ptr + 2, is_host);
146 24374901 bellard
    case TYPE_STRUCT:
147 24374901 bellard
        se = struct_entries + type_ptr[1];
148 24374901 bellard
        return se->align[is_host];
149 24374901 bellard
    default:
150 24374901 bellard
        return -1;
151 24374901 bellard
    }
152 24374901 bellard
}
153 24374901 bellard
154 0ad041d4 bellard
#endif /* NO_THUNK_TYPE_SIZE */
155 0ad041d4 bellard
156 5fafdf24 ths
unsigned int target_to_host_bitmask(unsigned int x86_mask,
157 31e31b8a bellard
                                    bitmask_transtbl * trans_tbl);
158 5fafdf24 ths
unsigned int host_to_target_bitmask(unsigned int alpha_mask,
159 31e31b8a bellard
                                    bitmask_transtbl * trans_tbl);
160 31e31b8a bellard
161 31e31b8a bellard
#endif