blob: d650fa4c1352427d44fb93b5d1b586484c43defb [file] [log] [blame]
bellard3ef693a2003-03-23 20:17:16 +00001/*
2 * Generic thunking code to convert data between host and target CPU
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard3ef693a2003-03-23 20:17:16 +00004 * 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 */
bellard31e31b82003-02-18 22:55:36 +000020#ifndef THUNK_H
21#define THUNK_H
22
23#include <inttypes.h>
bellard35b66fc2004-01-24 15:26:06 +000024#include "cpu.h"
bellard34313952003-02-18 23:03:03 +000025
bellard31e31b82003-02-18 22:55:36 +000026/* types enums definitions */
27
28typedef 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
50typedef 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... */
63typedef 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
70void thunk_register_struct(int id, const char *name, const argtype *types);
71void thunk_register_struct_direct(int id, const char *name, StructEntry *se1);
ths5fafdf22007-09-16 21:08:06 +000072const argtype *thunk_convert(void *dst, const void *src,
bellard31e31b82003-02-18 22:55:36 +000073 const argtype *type_ptr, int to_host);
bellard0ad041d2003-06-25 22:11:41 +000074#ifndef NO_THUNK_TYPE_SIZE
bellard31e31b82003-02-18 22:55:36 +000075
bellard24374902003-06-15 19:52:54 +000076extern StructEntry struct_entries[];
77
j_mayer26553112007-11-19 01:06:24 +000078int thunk_type_size_array(const argtype *type_ptr, int is_host);
79int thunk_type_align_array(const argtype *type_ptr, int is_host);
80
bellard24374902003-06-15 19:52:54 +000081static inline int thunk_type_size(const argtype *type_ptr, int is_host)
82{
83 int type, size;
84 const StructEntry *se;
85
86 type = *type_ptr;
87 switch(type) {
88 case TYPE_CHAR:
89 return 1;
90 case TYPE_SHORT:
91 return 2;
92 case TYPE_INT:
93 return 4;
94 case TYPE_LONGLONG:
95 case TYPE_ULONGLONG:
96 return 8;
97 case TYPE_LONG:
98 case TYPE_ULONG:
99 case TYPE_PTRVOID:
100 case TYPE_PTR:
101 if (is_host) {
102 return HOST_LONG_SIZE;
103 } else {
blueswir1992f48a2007-10-14 16:27:31 +0000104 return TARGET_ABI_BITS / 8;
bellard24374902003-06-15 19:52:54 +0000105 }
106 break;
107 case TYPE_ARRAY:
108 size = type_ptr[1];
j_mayer26553112007-11-19 01:06:24 +0000109 return size * thunk_type_size_array(type_ptr + 2, is_host);
bellard24374902003-06-15 19:52:54 +0000110 case TYPE_STRUCT:
111 se = struct_entries + type_ptr[1];
112 return se->size[is_host];
113 default:
114 return -1;
115 }
116}
117
118static inline int thunk_type_align(const argtype *type_ptr, int is_host)
119{
120 int type;
121 const StructEntry *se;
122
123 type = *type_ptr;
124 switch(type) {
125 case TYPE_CHAR:
126 return 1;
127 case TYPE_SHORT:
128 return 2;
129 case TYPE_INT:
130 return 4;
131 case TYPE_LONGLONG:
132 case TYPE_ULONGLONG:
133 return 8;
134 case TYPE_LONG:
135 case TYPE_ULONG:
136 case TYPE_PTRVOID:
137 case TYPE_PTR:
138 if (is_host) {
139 return HOST_LONG_SIZE;
140 } else {
blueswir1992f48a2007-10-14 16:27:31 +0000141 return TARGET_ABI_BITS / 8;
bellard24374902003-06-15 19:52:54 +0000142 }
143 break;
144 case TYPE_ARRAY:
j_mayer26553112007-11-19 01:06:24 +0000145 return thunk_type_align_array(type_ptr + 2, is_host);
bellard24374902003-06-15 19:52:54 +0000146 case TYPE_STRUCT:
147 se = struct_entries + type_ptr[1];
148 return se->align[is_host];
149 default:
150 return -1;
151 }
152}
153
bellard0ad041d2003-06-25 22:11:41 +0000154#endif /* NO_THUNK_TYPE_SIZE */
155
ths5fafdf22007-09-16 21:08:06 +0000156unsigned int target_to_host_bitmask(unsigned int x86_mask,
bellard31e31b82003-02-18 22:55:36 +0000157 bitmask_transtbl * trans_tbl);
ths5fafdf22007-09-16 21:08:06 +0000158unsigned int host_to_target_bitmask(unsigned int alpha_mask,
bellard31e31b82003-02-18 22:55:36 +0000159 bitmask_transtbl * trans_tbl);
160
161#endif