2 * Copyright 2012 Piotr Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define VTABLE_ADD_FUNC(name) "\t.quad " THISCALL_NAME(name) "\n"
25 #define __ASM_VTABLE(name,funcs) \
28 "\t.quad " __ASM_NAME(#name "_rtti") "\n" \
29 "\t.globl " __ASM_NAME("MSVCP_" #name "_vtable") "\n" \
30 __ASM_NAME("MSVCP_" #name "_vtable") ":\n" \
35 #define VTABLE_ADD_FUNC(name) "\t.long " THISCALL_NAME(name) "\n"
37 #define __ASM_VTABLE(name,funcs) \
40 "\t.long " __ASM_NAME(#name "_rtti") "\n" \
41 "\t.globl " __ASM_NAME("MSVCP_" #name "_vtable") "\n" \
42 __ASM_NAME("MSVCP_" #name "_vtable") ":\n" \
49 #define DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
50 static type_info name ## _type_info = { \
51 &MSVCP_type_info_vtable, \
56 static const rtti_base_descriptor name ## _rtti_base_descriptor = { \
63 #define DEFINE_RTTI_DATA(name, off, base_classes_no, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
64 DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
66 static const rtti_base_array name ## _rtti_base_array = { \
68 &name ## _rtti_base_descriptor, \
81 static const rtti_object_hierarchy name ## _hierarchy = { \
85 &name ## _rtti_base_array \
88 const rtti_object_locator name ## _rtti = { \
92 &name ## _type_info, \
96 #define DEFINE_CXX_TYPE_INFO(type) \
97 static const cxx_type_info type ## _cxx_type_info = { \
99 & type ##_type_info, \
102 (cxx_copy_ctor)THISCALL(MSVCP_ ## type ##_copy_ctor) \
105 #define DEFINE_CXX_DATA(type, base_no, cl1, cl2, cl3, cl4, dtor) \
106 DEFINE_CXX_TYPE_INFO(type) \
108 static const cxx_type_info_table type ## _cxx_type_table = { \
111 & type ## _cxx_type_info, \
119 static const cxx_exception_type type ## _cxx_type = { \
121 (cxx_copy_ctor)THISCALL(dtor), \
123 & type ## _cxx_type_table \
128 #define __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
129 static type_info name ## _type_info = { \
130 &MSVCP_type_info_vtable, \
135 static rtti_base_descriptor name ## _rtti_base_descriptor = { \
142 #define DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
143 __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
145 static void init_ ## name ## _rtti(char *base) \
147 name ## _rtti_base_descriptor.type_descriptor = (char*)&name ## _type_info - base; \
150 #define DEFINE_RTTI_DATA(name, off, base_classes_no, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
151 __DEFINE_RTTI_BASE(name, base_classes_no, mangled_name) \
153 static rtti_base_array name ## _rtti_base_array = { \
168 static rtti_object_hierarchy name ## _hierarchy = { \
175 rtti_object_locator name ## _rtti = { \
184 static void init_ ## name ## _rtti(char *base) \
186 name ## _rtti_base_descriptor.type_descriptor = (char*)&name ## _type_info - base; \
187 name ## _rtti_base_array.bases[0] = (char*)&name ## _rtti_base_descriptor - base; \
188 name ## _rtti_base_array.bases[1] = (char*)cl1 - base; \
189 name ## _rtti_base_array.bases[2] = (char*)cl2 - base; \
190 name ## _rtti_base_array.bases[3] = (char*)cl3 - base; \
191 name ## _rtti_base_array.bases[4] = (char*)cl4 - base; \
192 name ## _rtti_base_array.bases[5] = (char*)cl5 - base; \
193 name ## _rtti_base_array.bases[6] = (char*)cl6 - base; \
194 name ## _rtti_base_array.bases[7] = (char*)cl7 - base; \
195 name ## _rtti_base_array.bases[8] = (char*)cl8 - base; \
196 name ## _rtti_base_array.bases[9] = (char*)cl9 - base; \
197 name ## _hierarchy.base_classes = (char*)&name ## _rtti_base_array - base; \
198 name ## _rtti.type_descriptor = (char*)&name ## _type_info - base; \
199 name ## _rtti.type_hierarchy = (char*)&name ## _hierarchy - base; \
200 name ## _rtti.object_locator = (char*)&name ## _rtti - base; \
203 #define DEFINE_CXX_TYPE_INFO(type) \
204 static cxx_type_info type ## _cxx_type_info = { \
212 static void init_ ## type ## _cxx_type_info(char *base) \
214 type ## _cxx_type_info.type_info = (char *)&type ## _type_info - base; \
215 type ## _cxx_type_info.copy_ctor = (char *)MSVCP_ ## type ## _copy_ctor - base; \
218 #define DEFINE_CXX_DATA(type, base_no, cl1, cl2, cl3, cl4, dtor) \
220 DEFINE_CXX_TYPE_INFO(type) \
222 static cxx_type_info_table type ## _cxx_type_table = { \
233 static cxx_exception_type type ##_cxx_type = { \
240 static void init_ ## type ## _cxx(char *base) \
242 init_ ## type ## _cxx_type_info(base); \
243 type ## _cxx_type_table.info[0] = (char *)&type ## _cxx_type_info - base; \
244 type ## _cxx_type_table.info[1] = (char *)cl1 - base; \
245 type ## _cxx_type_table.info[2] = (char *)cl2 - base; \
246 type ## _cxx_type_table.info[3] = (char *)cl3 - base; \
247 type ## _cxx_type_table.info[4] = (char *)cl4 - base; \
248 type ## _cxx_type.destructor = (char *)dtor - base; \
249 type ## _cxx_type.type_info_table = (char *)&type ## _cxx_type_table - base; \
254 #define DEFINE_RTTI_DATA0(name, off, mangled_name) \
255 DEFINE_RTTI_DATA(name, off, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
256 #define DEFINE_RTTI_DATA1(name, off, cl1, mangled_name) \
257 DEFINE_RTTI_DATA(name, off, 1, cl1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
258 #define DEFINE_RTTI_DATA2(name, off, cl1, cl2, mangled_name) \
259 DEFINE_RTTI_DATA(name, off, 2, cl1, cl2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
260 #define DEFINE_RTTI_DATA3(name, off, cl1, cl2, cl3, mangled_name) \
261 DEFINE_RTTI_DATA(name, off, 3, cl1, cl2, cl3, NULL, NULL, NULL, NULL, NULL, NULL, mangled_name)
262 #define DEFINE_RTTI_DATA4(name, off, cl1, cl2, cl3, cl4, mangled_name) \
263 DEFINE_RTTI_DATA(name, off, 4, cl1, cl2, cl3, cl4, NULL, NULL, NULL, NULL, NULL, mangled_name)
264 #define DEFINE_RTTI_DATA5(name, off, cl1, cl2, cl3, cl4, cl5, mangled_name) \
265 DEFINE_RTTI_DATA(name, off, 5, cl1, cl2, cl3, cl4, cl5, NULL, NULL, NULL, NULL, mangled_name)
266 #define DEFINE_RTTI_DATA8(name, off, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, mangled_name) \
267 DEFINE_RTTI_DATA(name, off, 8, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, NULL, mangled_name)
268 #define DEFINE_RTTI_DATA9(name, off, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name) \
269 DEFINE_RTTI_DATA(name, off, 9, cl1, cl2, cl3, cl4, cl5, cl6, cl7, cl8, cl9, mangled_name)
271 #define DEFINE_CXX_DATA0(name, dtor) \
272 DEFINE_CXX_DATA(name, 0, NULL, NULL, NULL, NULL, dtor)
273 #define DEFINE_CXX_DATA1(name, cl1, dtor) \
274 DEFINE_CXX_DATA(name, 1, cl1, NULL, NULL, NULL, dtor)
275 #define DEFINE_CXX_DATA2(name, cl1, cl2, dtor) \
276 DEFINE_CXX_DATA(name, 2, cl1, cl2, NULL, NULL, dtor)
277 #define DEFINE_CXX_DATA3(name, cl1, cl2, cl3, dtor) \
278 DEFINE_CXX_DATA(name, 3, cl1, cl2, cl3, NULL, dtor)
279 #define DEFINE_CXX_DATA4(name, cl1, cl2, cl3, cl4, dtor) \
280 DEFINE_CXX_DATA(name, 4, cl1, cl2, cl3, cl4, dtor)
282 #ifdef __ASM_USE_THISCALL_WRAPPER
284 #define CALL_VTBL_FUNC(this, off, ret, type, args) ((ret (WINAPI*)type)&vtbl_wrapper_##off)args
286 extern void *vtbl_wrapper_0
;
287 extern void *vtbl_wrapper_4
;
288 extern void *vtbl_wrapper_8
;
289 extern void *vtbl_wrapper_12
;
290 extern void *vtbl_wrapper_16
;
291 extern void *vtbl_wrapper_20
;
292 extern void *vtbl_wrapper_24
;
293 extern void *vtbl_wrapper_28
;
294 extern void *vtbl_wrapper_32
;
295 extern void *vtbl_wrapper_36
;
296 extern void *vtbl_wrapper_40
;
297 extern void *vtbl_wrapper_44
;
298 extern void *vtbl_wrapper_48
;
299 extern void *vtbl_wrapper_52
;
300 extern void *vtbl_wrapper_56
;
304 #define CALL_VTBL_FUNC(this, off, ret, type, args) ((ret (__thiscall***)type)this)[0][off/4]args
308 /* exception object */
309 typedef void (*vtable_ptr
)(void);
310 typedef struct __exception
312 const vtable_ptr
*vtable
;
313 char *name
; /* Name of this exception, always a new copy for each object */
314 int do_free
; /* Whether to free 'name' in our dtor */
317 /* Internal: throws selected exception */
318 typedef enum __exception_type
{
323 EXCEPTION_LOGIC_ERROR
,
324 EXCEPTION_LENGTH_ERROR
,
325 EXCEPTION_OUT_OF_RANGE
,
326 EXCEPTION_INVALID_ARGUMENT
,
327 EXCEPTION_RUNTIME_ERROR
,
329 EXCEPTION_RANGE_ERROR
,
331 void throw_exception(exception_type
, const char *);
334 typedef struct __type_info
336 const vtable_ptr
*vtable
;
337 char *name
; /* Unmangled name, allocated lazily */
338 char mangled
[128]; /* Variable length, but we declare it large enough for static RTTI */
341 extern const vtable_ptr MSVCP_type_info_vtable
;
343 /* offsets for computing the this pointer */
346 int this_offset
; /* offset of base class this pointer from start of object */
347 int vbase_descr
; /* offset of virtual base class descriptor */
348 int vbase_offset
; /* offset of this pointer offset in virtual base class descriptor */
351 /* dlls/msvcrt/cppexcept.h */
352 typedef void (*cxx_copy_ctor
)(void);
356 typedef struct _rtti_base_descriptor
358 const type_info
*type_descriptor
;
359 int num_base_classes
;
360 this_ptr_offsets offsets
; /* offsets for computing the this pointer */
361 unsigned int attributes
;
362 } rtti_base_descriptor
;
364 typedef struct _rtti_base_array
366 const rtti_base_descriptor
*bases
[10]; /* First element is the class itself */
369 typedef struct _rtti_object_hierarchy
371 unsigned int signature
;
372 unsigned int attributes
;
373 int array_len
; /* Size of the array pointed to by 'base_classes' */
374 const rtti_base_array
*base_classes
;
375 } rtti_object_hierarchy
;
377 typedef struct _rtti_object_locator
379 unsigned int signature
;
380 int base_class_offset
;
382 const type_info
*type_descriptor
;
383 const rtti_object_hierarchy
*type_hierarchy
;
384 } rtti_object_locator
;
389 const type_info
*type_info
;
390 this_ptr_offsets offsets
;
392 cxx_copy_ctor copy_ctor
;
398 const cxx_type_info
*info
[5];
399 } cxx_type_info_table
;
404 void (*destructor
)(void);
405 void* /*cxx_exc_custom_handler*/ custom_handler
;
406 const cxx_type_info_table
*type_info_table
;
407 } cxx_exception_type
;
413 unsigned int type_descriptor
;
414 int num_base_classes
;
415 this_ptr_offsets offsets
; /* offsets for computing the this pointer */
416 unsigned int attributes
;
417 } rtti_base_descriptor
;
421 unsigned int bases
[10]; /* First element is the class itself */
426 unsigned int signature
;
427 unsigned int attributes
;
428 int array_len
; /* Size of the array pointed to by 'base_classes' */
429 unsigned int base_classes
;
430 } rtti_object_hierarchy
;
434 unsigned int signature
;
435 int base_class_offset
;
437 unsigned int type_descriptor
;
438 unsigned int type_hierarchy
;
439 unsigned int object_locator
;
440 } rtti_object_locator
;
445 unsigned int type_info
;
446 this_ptr_offsets offsets
;
448 unsigned int copy_ctor
;
454 unsigned int info
[5];
455 } cxx_type_info_table
;
460 unsigned int destructor
;
461 unsigned int custom_handler
;
462 unsigned int type_info_table
;
463 } cxx_exception_type
;
467 #define CREATE_TYPE_INFO_VTABLE\
468 static void MSVCP_type_info_dtor(type_info * _this) \
473 DEFINE_THISCALL_WRAPPER(MSVCP_type_info_vector_dtor,8) \
474 void * __thiscall MSVCP_type_info_vector_dtor(type_info * _this, unsigned int flags) \
478 /* we have an array, with the number of elements stored before the first object */ \
479 INT_PTR i, *ptr = (INT_PTR *)_this - 1; \
481 for (i = *ptr - 1; i >= 0; i--) MSVCP_type_info_dtor(_this + i); \
486 MSVCP_type_info_dtor(_this); \
487 if (flags & 1) free(_this); \
492 DEFINE_RTTI_DATA0( type_info, 0, ".?AVtype_info@@" ) \
494 __ASM_BLOCK_BEGIN(type_info_vtables) \
495 __ASM_VTABLE(type_info, \
496 VTABLE_ADD_FUNC(MSVCP_type_info_vector_dtor)); \