[runtime] Add some documentation for the offsets stuff.
[mono.git] / mono / metadata / abi-details.h
1 /*
2  * Copyright 2014 Xamarin Inc
3  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
4  */
5 #ifndef __MONO_METADATA_ABI_DETAILS_H__
6 #define __MONO_METADATA_ABI_DETAILS_H__
7
8 #include <config.h>
9 #include <glib.h>
10
11 /*
12  * This file defines macros to compute sizes/alignments/field offsets which depend on
13  * the ABI. It is needed during cross compiling since the generated code needs to
14  * contain offsets which correspond to the ABI of the target, not the host.
15  * It defines the following macros:
16  * - MONO_ABI_SIZEOF(type) for every basic type
17  * - MONO_ABI_ALIGNOF(type) for every basic type
18  * - MONO_STRUCT_OFFSET(struct, field) for various runtime structures
19  * When not cross compiling, these correspond to the host ABI (i.e. sizeof/offsetof).
20  * When cross compiling, these are defined in a generated header file which is
21  * generated by the offsets tool in tools/offsets-tool. The name of the file
22  * is given by the --with-cross-offsets= configure argument.
23  */
24
25 #define MONO_ABI_ALIGNOF(type) MONO_ALIGN_ ## type
26 #define MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(type) typedef struct { char c; type x; } Mono_Align_Struct_ ##type;
27 #define MONO_CURRENT_ABI_ALIGNOF(type) ((int)G_STRUCT_OFFSET(Mono_Align_Struct_ ##type, x))
28 #define MONO_ABI_SIZEOF(type) MONO_SIZEOF_ ## type
29 #define MONO_CURRENT_ABI_SIZEOF(type) ((int)sizeof(type))
30
31 #undef DECL_OFFSET
32 #undef DECL_OFFSET2
33 #define DECL_OFFSET(struct,field) MONO_OFFSET_ ## struct ## _ ## field = -1,
34 #define DECL_OFFSET2(struct,field,offset) MONO_OFFSET_ ## struct ## _ ## field = offset,
35 #define DECL_ALIGN(type) MONO_ALIGN_ ##type = MONO_CURRENT_ABI_ALIGNOF (type),
36 #define DECL_ALIGN2(type,size) MONO_ALIGN_ ##type = size,
37 #define DECL_SIZE(type) MONO_SIZEOF_ ##type = MONO_CURRENT_ABI_SIZEOF (type),
38 #define DECL_SIZE2(type,size) MONO_SIZEOF_ ##type = size,
39
40 /* Needed by MONO_CURRENT_ABI_ALIGNOF */
41 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(gint8)
42 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(gint16)
43 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(gint32)
44 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(gint64)
45 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(float)
46 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(double)
47 MONO_CURRENT_ABI_ALIGNOF_TYPEDEF(gpointer)
48
49 enum {
50 #include "object-offsets.h"
51 };
52
53 #ifdef USED_CROSS_COMPILER_OFFSETS
54 #define MONO_STRUCT_OFFSET(struct,field) MONO_OFFSET_ ## struct ## _ ## field
55 #else
56 #if defined(HAS_CROSS_COMPILER_OFFSETS) || defined(MONO_CROSS_COMPILE)
57 #define MONO_STRUCT_OFFSET(struct,field) (MONO_OFFSET_ ## struct ## _ ## field == -1, G_STRUCT_OFFSET (struct,field))
58 #else
59 #define MONO_STRUCT_OFFSET(struct,field) G_STRUCT_OFFSET (struct,field)
60 #endif
61 #endif
62
63 #endif