Il2CppCompatibleDef.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include "codegen/il2cpp-codegen-metadata.h"
  4. #include "il2cpp-class-internals.h"
  5. #include "vm/Array.h"
  6. #include "vm/Type.h"
  7. #include "vm/Runtime.h"
  8. #include "vm/GlobalMetadataFileInternals.h"
  9. #include "vm/MetadataAlloc.h"
  10. #include "icalls/mscorlib/System/Type.h"
  11. #include "gc/GarbageCollector.h"
  12. #if HYBRIDCLR_UNITY_2020
  13. #include "icalls/mscorlib/System/MonoType.h"
  14. #elif HYBRIDCLR_UNITY_2021_OR_NEW
  15. #include "icalls/mscorlib/System/RuntimeType.h"
  16. #include "icalls/mscorlib/System/RuntimeTypeHandle.h"
  17. #elif HYBRIDCLR_UNITY_2019
  18. #include "icalls/mscorlib/System/MonoType.h"
  19. #if IL2CPP_SIZEOF_VOID_P == 8
  20. #define PLATFORM_ARCH_64 1
  21. #else
  22. #define PLATFORM_ARCH_64 0
  23. #endif
  24. #elif !defined(HYBRIDCLR_UNITY_VERSION)
  25. #error "please run 'HybridCLR/Generate/All' before building"
  26. #else
  27. #error "unsupported unity version"
  28. #endif
  29. #if IL2CPP_BYTE_ORDER != IL2CPP_LITTLE_ENDIAN
  30. #error "only support litten endian"
  31. #endif
  32. #if PLATFORM_ARCH_64
  33. #define HYBRIDCLR_ARCH_64 1
  34. #else
  35. #define HYBRIDCLR_ARCH_64 0
  36. #endif
  37. #define PTR_SIZE IL2CPP_SIZEOF_VOID_P
  38. #if HYBRIDCLR_ARCH_64 || HYBRIDCLR_TARGET_X86
  39. #define SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS 1
  40. #else
  41. #define SUPPORT_MEMORY_NOT_ALIGMENT_ACCESS 0
  42. #endif
  43. #ifndef ENABLE_PLACEHOLDER_DLL
  44. #define ENABLE_PLACEHOLDER_DLL 1
  45. #endif
  46. #if IL2CPP_ENABLE_WRITE_BARRIERS
  47. #define HYBRIDCLR_ENABLE_WRITE_BARRIERS 1
  48. #else
  49. #define HYBRIDCLR_ENABLE_WRITE_BARRIERS 0
  50. #endif
  51. #ifndef HYBRIDCLR_ENABLE_PROFILER
  52. #define HYBRIDCLR_ENABLE_PROFILER (IL2CPP_ENABLE_PROFILER && (IL2CPP_DEBUG || HYBRIDCLR_ENABLE_PROFILER_IN_RELEASE_BUILD))
  53. #endif
  54. #ifndef HYBRIDCLR_ENABLE_STRACKTRACE
  55. #define HYBRIDCLR_ENABLE_STRACKTRACE (IL2CPP_ENABLE_STACKTRACE_SENTRIES && (IL2CPP_DEBUG || HYBRIDCLR_ENABLE_STRACE_TRACE_IN_WEBGL_RELEASE_BUILD))
  56. #endif
  57. #if UNITY_ENGINE_TUANJIE
  58. #define HYBRIDCLR_MALLOC(size) IL2CPP_MALLOC(size, IL2CPP_MEM_META_POOL)
  59. #define HYBRIDCLR_MALLOC_ALIGNED(size, alignment) IL2CPP_MALLOC_ALIGNED(size, alignment, IL2CPP_MEM_META_POOL)
  60. #define HYBRIDCLR_MALLOC_ZERO(size) IL2CPP_MALLOC_ZERO(size, IL2CPP_MEM_META_POOL)
  61. #define HYBRIDCLR_CALLOC(count, size) IL2CPP_CALLOC(count, size, IL2CPP_MEM_META_POOL)
  62. #define HYBRIDCLR_FREE(ptr) IL2CPP_FREE(ptr, IL2CPP_MEM_META_POOL)
  63. #define HYBRIDCLR_FREE_ALIGNED(ptr) IL2CPP_FREE_ALIGNED(ptr, IL2CPP_MEM_META_POOL)
  64. #define HYBRIDCLR_METADATA_MALLOC(size) il2cpp::vm::MetadataMalloc(size, IL2CPP_MSTAT_TYPE)
  65. #define HYBRIDCLR_METADATA_CALLOC(count, size) il2cpp::vm::MetadataCalloc(count, size, IL2CPP_MSTAT_TYPE)
  66. #else
  67. #define HYBRIDCLR_MALLOC(size) IL2CPP_MALLOC(size)
  68. #define HYBRIDCLR_MALLOC_ALIGNED(size, alignment) IL2CPP_MALLOC_ALIGNED(size, alignment)
  69. #define HYBRIDCLR_MALLOC_ZERO(size) IL2CPP_MALLOC_ZERO(size)
  70. #define HYBRIDCLR_CALLOC(count, size) IL2CPP_CALLOC(count, size)
  71. #define HYBRIDCLR_FREE(ptr) IL2CPP_FREE(ptr)
  72. #define HYBRIDCLR_FREE_ALIGNED(ptr) IL2CPP_FREE_ALIGNED(ptr)
  73. #define HYBRIDCLR_METADATA_MALLOC(size) il2cpp::vm::MetadataMalloc(size)
  74. #define HYBRIDCLR_METADATA_CALLOC(count, size) il2cpp::vm::MetadataCalloc(count, size)
  75. #endif
  76. namespace hybridclr
  77. {
  78. extern const char* g_placeHolderAssemblies[];
  79. Il2CppMethodPointer InitAndGetInterpreterDirectlyCallMethodPointerSlow(MethodInfo* method);
  80. inline Il2CppMethodPointer InitAndGetInterpreterDirectlyCallMethodPointer(const MethodInfo* method)
  81. {
  82. Il2CppMethodPointer methodPointer = method->methodPointerCallByInterp;
  83. if (methodPointer)
  84. {
  85. return methodPointer;
  86. }
  87. if (method->initInterpCallMethodPointer)
  88. {
  89. return methodPointer;
  90. }
  91. return InitAndGetInterpreterDirectlyCallMethodPointerSlow(const_cast<MethodInfo*>(method));
  92. }
  93. inline Il2CppMethodPointer InitAndGetInterpreterDirectlyCallVirtualMethodPointer(const MethodInfo* method)
  94. {
  95. Il2CppMethodPointer methodPointer = method->virtualMethodPointerCallByInterp;
  96. if (methodPointer)
  97. {
  98. return methodPointer;
  99. }
  100. if (method->initInterpCallMethodPointer)
  101. {
  102. return methodPointer;
  103. }
  104. InitAndGetInterpreterDirectlyCallMethodPointerSlow(const_cast<MethodInfo*>(method));
  105. return method->virtualMethodPointerCallByInterp;
  106. }
  107. inline void HYBRIDCLR_SET_WRITE_BARRIER(void** ptr)
  108. {
  109. #if HYBRIDCLR_ENABLE_WRITE_BARRIERS
  110. il2cpp::gc::GarbageCollector::SetWriteBarrier(ptr);
  111. #endif
  112. }
  113. inline void HYBRIDCLR_SET_WRITE_BARRIER(void** ptr, size_t size)
  114. {
  115. #if HYBRIDCLR_ENABLE_WRITE_BARRIERS
  116. il2cpp::gc::GarbageCollector::SetWriteBarrier(ptr, size);
  117. #endif
  118. }
  119. }
  120. #if HYBRIDCLR_UNITY_2019 || HYBRIDCLR_UNITY_2020
  121. inline bool IS_CLASS_VALUE_TYPE(const Il2CppClass* klass)
  122. {
  123. return klass->valuetype;
  124. }
  125. inline bool IS_CCTOR_FINISH_OR_NO_CCTOR(const Il2CppClass* klass)
  126. {
  127. return (klass->cctor_finished) || !(klass->has_cctor);
  128. }
  129. inline const Il2CppType* GET_METHOD_PARAMETER_TYPE(const ParameterInfo& param)
  130. {
  131. return param.parameter_type;
  132. }
  133. inline uint32_t GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(const Il2CppCustomAttributeTypeRange& tr)
  134. {
  135. return tr.start;
  136. }
  137. inline void SET_IL2CPPTYPE_VALUE_TYPE(Il2CppType& type, bool v)
  138. {
  139. }
  140. inline void COPY_IL2CPPTYPE_VALUE_TYPE_FLAG(Il2CppType& dst, const Il2CppType& src)
  141. {
  142. }
  143. #define GET_ARRAY_ELEMENT_ADDRESS load_array_elema
  144. #define VALUE_TYPE_METHOD_POINTER_IS_ADJUST_METHOD 1
  145. namespace hybridclr
  146. {
  147. inline Il2CppReflectionType* GetReflectionTypeFromName(Il2CppString* name)
  148. {
  149. return il2cpp::icalls::mscorlib::System::Type::internal_from_name(name, true, false);
  150. }
  151. inline void ConstructDelegate(Il2CppDelegate* delegate, Il2CppObject* target, const MethodInfo* method)
  152. {
  153. delegate->method_ptr = InitAndGetInterpreterDirectlyCallVirtualMethodPointer(method);
  154. delegate->method = method;
  155. delegate->target = target;
  156. #if HYBRIDCLR_ENABLE_WRITE_BARRIERS
  157. if (target)
  158. {
  159. HYBRIDCLR_SET_WRITE_BARRIER((void**)&delegate->target);
  160. }
  161. #endif
  162. //il2cpp::vm::Type::ConstructDelegate(delegate, target, InitAndGetInterpreterDirectlyCallMethodPointer(method), method);
  163. }
  164. inline const MethodInfo* GetGenericVirtualMethod(const MethodInfo* result, const MethodInfo* inflateMethod)
  165. {
  166. return il2cpp::vm::Runtime::GetGenericVirtualMethod(result, inflateMethod);
  167. }
  168. inline void* GetNulllableDataOffset(void* nullableObj, Il2CppClass* nullableClass)
  169. {
  170. uint32_t field_offset = nullableClass->fields[0].offset - sizeof(Il2CppObject); // offset of value field
  171. return (uint8_t*)nullableObj + field_offset;
  172. }
  173. inline uint8_t* GetNulllableHasValueOffset(void* nullableObj, Il2CppClass* nullableClass)
  174. {
  175. uint32_t field_offset = nullableClass->fields[1].offset - sizeof(Il2CppObject); // offset of has_value field
  176. return (uint8_t*)nullableObj + field_offset;
  177. }
  178. inline Il2CppString* GetKlassFullName(const Il2CppType* type)
  179. {
  180. Il2CppReflectionType* refType = il2cpp::icalls::mscorlib::System::Type::internal_from_handle((intptr_t)type);
  181. return il2cpp::icalls::mscorlib::System::MonoType::getFullName(refType, false, false);
  182. }
  183. inline std::string GetKlassFullName2(const Il2CppType* type)
  184. {
  185. Il2CppReflectionType* refType = il2cpp::icalls::mscorlib::System::Type::internal_from_handle((intptr_t)type);
  186. return il2cpp::icalls::mscorlib::System::MonoType::getFullName2(refType, false, false);
  187. }
  188. }
  189. #elif HYBRIDCLR_UNITY_2021_OR_NEW
  190. inline bool IS_CLASS_VALUE_TYPE(const Il2CppClass* klass)
  191. {
  192. return klass->byval_arg.valuetype;
  193. }
  194. inline bool IS_CCTOR_FINISH_OR_NO_CCTOR(const Il2CppClass* klass)
  195. {
  196. return klass->cctor_finished_or_no_cctor;
  197. }
  198. inline const Il2CppType* GET_METHOD_PARAMETER_TYPE(const Il2CppType* param)
  199. {
  200. return param;
  201. }
  202. inline uint32_t GET_CUSTOM_ATTRIBUTE_TYPE_RANGE_START(const Il2CppCustomAttributeTypeRange& tr)
  203. {
  204. return tr.startOffset;
  205. }
  206. inline void SET_IL2CPPTYPE_VALUE_TYPE(Il2CppType& type, bool v)
  207. {
  208. type.valuetype = v;
  209. }
  210. inline void COPY_IL2CPPTYPE_VALUE_TYPE_FLAG(Il2CppType& dst, const Il2CppType& src)
  211. {
  212. dst.valuetype = src.valuetype;
  213. }
  214. #define GET_ARRAY_ELEMENT_ADDRESS il2cpp_array_addr_with_size
  215. #define VALUE_TYPE_METHOD_POINTER_IS_ADJUST_METHOD 0
  216. namespace hybridclr
  217. {
  218. inline Il2CppReflectionType* GetReflectionTypeFromName(Il2CppString* name)
  219. {
  220. return il2cpp::icalls::mscorlib::System::RuntimeTypeHandle::internal_from_name(name, nullptr, nullptr, true, false, false);
  221. }
  222. inline void ConstructDelegate(Il2CppDelegate* delegate, Il2CppObject* target, const MethodInfo* method)
  223. {
  224. delegate->target = target;
  225. delegate->method = method;
  226. delegate->invoke_impl = InitAndGetInterpreterDirectlyCallVirtualMethodPointer(method);
  227. delegate->invoke_impl_this = target;
  228. #if HYBRIDCLR_ENABLE_WRITE_BARRIERS
  229. if (target)
  230. {
  231. HYBRIDCLR_SET_WRITE_BARRIER((void**)&delegate->target);
  232. HYBRIDCLR_SET_WRITE_BARRIER((void**)&delegate->invoke_impl_this);
  233. }
  234. #endif
  235. }
  236. inline const MethodInfo* GetGenericVirtualMethod(const MethodInfo* result, const MethodInfo* inflateMethod)
  237. {
  238. #if HYBRIDCLR_UNITY_2021
  239. VirtualInvokeData vid;
  240. il2cpp::vm::Runtime::GetGenericVirtualMethod(result, inflateMethod, &vid);
  241. return vid.method;
  242. #else
  243. return il2cpp::metadata::GenericMethod::GetGenericVirtualMethod(result, inflateMethod);
  244. #endif
  245. }
  246. inline void* GetNulllableDataOffset(void* nullableObj, Il2CppClass* nullableClass)
  247. {
  248. uint32_t field_offset = nullableClass->fields[1].offset - sizeof(Il2CppObject); // offset of value field
  249. return (uint8_t*)nullableObj + field_offset;
  250. }
  251. inline uint8_t* GetNulllableHasValueOffset(void* nullableObj, Il2CppClass* nullableClass)
  252. {
  253. uint32_t field_offset = nullableClass->fields[0].offset - sizeof(Il2CppObject); // offset of has_value field
  254. return (uint8_t*)nullableObj + field_offset;
  255. }
  256. inline Il2CppString* GetKlassFullName(const Il2CppType* type)
  257. {
  258. Il2CppReflectionType* refType = il2cpp::icalls::mscorlib::System::Type::internal_from_handle((intptr_t)type);
  259. return il2cpp::icalls::mscorlib::System::RuntimeType::getFullName((Il2CppReflectionRuntimeType*)refType, false, false);
  260. }
  261. inline std::string GetKlassFullName2(const Il2CppType* type)
  262. {
  263. Il2CppReflectionType* refType = il2cpp::icalls::mscorlib::System::Type::internal_from_handle((intptr_t)type);
  264. return il2cpp::icalls::mscorlib::System::RuntimeType::getFullName2((Il2CppReflectionRuntimeType*)refType, false, false);
  265. }
  266. }
  267. #endif