abi-details.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * \file
  3. * Copyright 2014 Xamarin Inc
  4. * Licensed under the MIT license. See LICENSE file in the project root for full license information.
  5. */
  6. #ifndef __MONO_METADATA_ABI_DETAILS_H__
  7. #define __MONO_METADATA_ABI_DETAILS_H__
  8. #include <config.h>
  9. #include <glib.h>
  10. /*
  11. * This file defines macros to compute sizes/alignments/field offsets which depend on
  12. * the ABI. It is needed during cross compiling since the generated code needs to
  13. * contain offsets which correspond to the ABI of the target, not the host.
  14. * It defines the following macros:
  15. * - MONO_ABI_SIZEOF(type) for every basic type
  16. * - MONO_ABI_ALIGNOF(type) for every basic type
  17. * - MONO_STRUCT_OFFSET(struct, field) for various runtime structures
  18. * When not cross compiling, these correspond to the host ABI (i.e. sizeof/offsetof).
  19. * When cross compiling, these are defined in a generated header file which is
  20. * generated by the offsets tool in tools/offsets-tool. The name of the file
  21. * is given by the --with-cross-offsets= configure argument.
  22. */
  23. typedef enum {
  24. MONO_ALIGN_gint8,
  25. MONO_ALIGN_gint16,
  26. MONO_ALIGN_gint32,
  27. MONO_ALIGN_gint64,
  28. MONO_ALIGN_float,
  29. MONO_ALIGN_double,
  30. MONO_ALIGN_gpointer,
  31. MONO_ALIGN_COUNT
  32. } CoreTypeAlign;
  33. int mono_abi_alignment (CoreTypeAlign type);
  34. #define MONO_ABI_ALIGNOF(type) mono_abi_alignment (MONO_ALIGN_ ## type)
  35. #define MONO_ABI_SIZEOF(type) (MONO_STRUCT_SIZE (type))
  36. #define MONO_CURRENT_ABI_SIZEOF(type) ((int)sizeof(type))
  37. #undef DECL_OFFSET2
  38. #define DECL_OFFSET(struct,field) MONO_OFFSET_ ## struct ## _ ## field = -1,
  39. #define DECL_OFFSET2(struct,field,offset) MONO_OFFSET_ ## struct ## _ ## field = offset,
  40. #define DECL_ALIGN2(type,size)
  41. #define DECL_SIZE(type) MONO_SIZEOF_ ##type = -1,
  42. #define DECL_SIZE2(type,size) MONO_SIZEOF_ ##type = size,
  43. enum {
  44. #include "object-offsets.h"
  45. };
  46. #ifdef USED_CROSS_COMPILER_OFFSETS
  47. #define MONO_STRUCT_OFFSET(struct,field) MONO_OFFSET_ ## struct ## _ ## field
  48. #define MONO_STRUCT_OFFSET_CONSTANT(struct,field) MONO_STRUCT_OFFSET(struct,field)
  49. #define MONO_STRUCT_SIZE(struct) MONO_SIZEOF_ ## struct
  50. #else
  51. /* This macro is expanding to something that uses the comma operator in C [0].
  52. * The first part introduces an expression that uses the `MONO_OFFSET_*`
  53. * define. The result isn't used, but it forces the compiler to bail out if the
  54. * define doesn't exist; this happens when we forgot to add an entry in
  55. * `object-offsets.h` accordingly.
  56. *
  57. * [0] https://en.wikipedia.org/wiki/Comma_operator
  58. */
  59. #define MONO_STRUCT_OFFSET(struct,field) (MONO_OFFSET_ ## struct ## _ ## field == -1, G_STRUCT_OFFSET (struct,field))
  60. #define MONO_STRUCT_OFFSET_CONSTANT(struct,field) G_STRUCT_OFFSET (struct,field)
  61. #define MONO_STRUCT_SIZE(struct) (MONO_SIZEOF_ ## struct == -1, (int)sizeof(struct))
  62. #endif
  63. // #define MONO_SIZEOF_MonoObject (2 * MONO_ABI_SIZEOF(gpointer))
  64. #define MONO_SIZEOF_MonoObject (2 * MONO_SIZEOF_gpointer)
  65. #endif