il2cpp-codegen-il2cpp.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. #pragma once
  2. #include "il2cpp-codegen-common-small.h"
  3. #include "il2cpp-codegen-common-big.h"
  4. #include "il2cpp-pinvoke-support.h"
  5. #include "icalls/mscorlib/System.Runtime.InteropServices/Marshal.h"
  6. #include "utils/Il2CppHStringReference.h"
  7. #include "vm-utils/icalls/mscorlib/System.Threading/Interlocked.h"
  8. #include "vm/ClassInlines.h"
  9. #include "vm/ScopedThreadAttacher.h"
  10. #include "vm/String.h"
  11. #if IL2CPP_ENABLE_PROFILER
  12. void il2cpp_codegen_profiler_method_enter(const RuntimeMethod* method);
  13. void il2cpp_codegen_profiler_method_exit(const RuntimeMethod* method);
  14. #endif
  15. struct ProfilerMethodSentry
  16. {
  17. ProfilerMethodSentry(const RuntimeMethod* method)
  18. #if IL2CPP_ENABLE_PROFILER
  19. : m_method(method)
  20. #endif
  21. {
  22. #if IL2CPP_ENABLE_PROFILER
  23. il2cpp_codegen_profiler_method_enter(m_method);
  24. #endif
  25. }
  26. ~ProfilerMethodSentry()
  27. {
  28. #if IL2CPP_ENABLE_PROFILER
  29. il2cpp_codegen_profiler_method_exit(m_method);
  30. #endif
  31. }
  32. private:
  33. const RuntimeMethod* m_method;
  34. };
  35. void il2cpp_codegen_stacktrace_push_frame(Il2CppStackFrameInfo& frame);
  36. void il2cpp_codegen_stacktrace_pop_frame();
  37. struct StackTraceSentry
  38. {
  39. StackTraceSentry(const RuntimeMethod* method) : m_method(method)
  40. {
  41. Il2CppStackFrameInfo frame_info = { 0 };
  42. frame_info.method = method;
  43. il2cpp_codegen_stacktrace_push_frame(frame_info);
  44. }
  45. ~StackTraceSentry()
  46. {
  47. il2cpp_codegen_stacktrace_pop_frame();
  48. }
  49. private:
  50. const RuntimeMethod* m_method;
  51. };
  52. #define IL2CPP_FAKE_BOX_SENTRY (MonitorData*)UINTPTR_MAX
  53. template<typename T>
  54. struct Il2CppFakeBox : RuntimeObject
  55. {
  56. T m_Value;
  57. Il2CppFakeBox(RuntimeClass* boxedType, T* value)
  58. {
  59. klass = boxedType;
  60. monitor = IL2CPP_FAKE_BOX_SENTRY;
  61. m_Value = *value;
  62. }
  63. };
  64. inline bool il2cpp_codegen_is_fake_boxed_object(RuntimeObject* object)
  65. {
  66. return object->monitor == IL2CPP_FAKE_BOX_SENTRY;
  67. }
  68. // TODO: This file should contain all the functions and type declarations needed for the generated code.
  69. // Hopefully, we stop including everything in the generated code and know exactly what dependencies we have.
  70. // Note that all parameter and return types should match the generated types not the runtime types.
  71. void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions);
  72. typedef void (*MetadataInitializerCleanupFunc)();
  73. void il2cpp_codegen_register_metadata_initialized_cleanup(MetadataInitializerCleanupFunc cleanup);
  74. // type registration
  75. void* il2cpp_codegen_get_thread_static_data(RuntimeClass* klass);
  76. String_t* il2cpp_codegen_string_new_wrapper(const char* str);
  77. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str);
  78. Type_t* il2cpp_codegen_type_get_object(const RuntimeType* type);
  79. NORETURN void il2cpp_codegen_raise_exception(Exception_t *ex, MethodInfo* lastManagedFrame);
  80. NORETURN void il2cpp_codegen_raise_exception(il2cpp_hresult_t hresult, bool defaultToCOMException);
  81. void il2cpp_codegen_raise_execution_engine_exception_if_method_is_not_found(const RuntimeMethod* method);
  82. void il2cpp_codegen_raise_execution_engine_exception(const RuntimeMethod* method);
  83. NORETURN void il2cpp_codegen_raise_out_of_memory_exception();
  84. NORETURN void il2cpp_codegen_raise_null_reference_exception();
  85. NORETURN void il2cpp_codegen_raise_divide_by_zero_exception();
  86. Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg);
  87. Exception_t* il2cpp_codegen_get_argument_null_exception(const char* param);
  88. Exception_t* il2cpp_codegen_get_overflow_exception();
  89. Exception_t* il2cpp_codegen_get_not_supported_exception(const char* msg);
  90. Exception_t* il2cpp_codegen_get_array_type_mismatch_exception();
  91. Exception_t* il2cpp_codegen_get_invalid_cast_exception(const char* msg);
  92. Exception_t* il2cpp_codegen_get_invalid_operation_exception(const char* msg);
  93. Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg);
  94. Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg);
  95. Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception();
  96. Exception_t* il2cpp_codegen_get_index_out_of_range_exception();
  97. Exception_t* il2cpp_codegen_get_exception(il2cpp_hresult_t hresult, bool defaultToCOMException);
  98. inline RuntimeClass* il2cpp_codegen_object_class(RuntimeObject* obj)
  99. {
  100. return obj->klass;
  101. }
  102. // OpCode.IsInst
  103. RuntimeObject* IsInst(RuntimeObject *obj, RuntimeClass* targetType);
  104. inline RuntimeObject* IsInstSealed(RuntimeObject *obj, RuntimeClass* targetType)
  105. {
  106. #if IL2CPP_DEBUG
  107. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_SEALED) != 0);
  108. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_INTERFACE) == 0);
  109. #endif
  110. if (!obj)
  111. return NULL;
  112. // optimized version to compare sealed classes
  113. return (obj->klass == targetType ? obj : NULL);
  114. }
  115. inline RuntimeObject* IsInstClass(RuntimeObject *obj, RuntimeClass* targetType)
  116. {
  117. #if IL2CPP_DEBUG
  118. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_INTERFACE) == 0);
  119. #endif
  120. if (!obj)
  121. return NULL;
  122. // optimized version to compare classes
  123. return il2cpp::vm::ClassInlines::HasParentUnsafe(obj->klass, targetType) ? obj : NULL;
  124. }
  125. // OpCode.Castclass
  126. NORETURN void RaiseInvalidCastException(RuntimeObject* obj, RuntimeClass* targetType);
  127. inline RuntimeObject* Castclass(RuntimeObject *obj, RuntimeClass* targetType)
  128. {
  129. if (!obj)
  130. return NULL;
  131. RuntimeObject* result = IsInst(obj, targetType);
  132. if (result)
  133. return result;
  134. RaiseInvalidCastException(obj, targetType);
  135. return NULL;
  136. }
  137. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, RuntimeClass* targetType)
  138. {
  139. if (!obj)
  140. return NULL;
  141. RuntimeObject* result = IsInstSealed(obj, targetType);
  142. if (result)
  143. return result;
  144. RaiseInvalidCastException(obj, targetType);
  145. return NULL;
  146. }
  147. inline RuntimeObject* CastclassClass(RuntimeObject *obj, RuntimeClass* targetType)
  148. {
  149. if (!obj)
  150. return NULL;
  151. RuntimeObject* result = IsInstClass(obj, targetType);
  152. if (result)
  153. return result;
  154. RaiseInvalidCastException(obj, targetType);
  155. return NULL;
  156. }
  157. inline void NullCheck(void* this_ptr)
  158. {
  159. if (this_ptr != NULL)
  160. return;
  161. il2cpp_codegen_raise_null_reference_exception();
  162. }
  163. // OpCode.Box
  164. RuntimeObject* Box(RuntimeClass* type, void* data);
  165. // OpCode.UnBox
  166. void* Unbox_internal(Il2CppObject* obj);
  167. inline void* UnBox(RuntimeObject* obj)
  168. {
  169. NullCheck(obj);
  170. return Unbox_internal(obj);
  171. }
  172. inline void* UnBox(RuntimeObject* obj, RuntimeClass* expectedBoxedClass)
  173. {
  174. NullCheck(obj);
  175. if (obj->klass->element_class == expectedBoxedClass->element_class)
  176. return Unbox_internal(obj);
  177. RaiseInvalidCastException(obj, expectedBoxedClass);
  178. return NULL;
  179. }
  180. void UnBoxNullable_internal(RuntimeObject* obj, RuntimeClass* expectedBoxedClass, void* storage);
  181. inline void UnBoxNullable(RuntimeObject* obj, RuntimeClass* expectedBoxedClass, void* storage)
  182. {
  183. // We only need to do type checks if obj is not null
  184. // Unboxing null nullable is perfectly valid and returns an instance that has no value
  185. if (obj != NULL)
  186. {
  187. if (obj->klass->element_class != expectedBoxedClass->element_class)
  188. RaiseInvalidCastException(obj, expectedBoxedClass);
  189. }
  190. UnBoxNullable_internal(obj, expectedBoxedClass, storage);
  191. }
  192. int32_t il2cpp_codgen_class_get_instance_size(RuntimeClass* klass);
  193. inline uint32_t il2cpp_codegen_sizeof(RuntimeClass* klass)
  194. {
  195. if (!klass->byval_arg.valuetype)
  196. {
  197. return sizeof(void*);
  198. }
  199. return il2cpp_codgen_class_get_instance_size(klass) - sizeof(RuntimeObject);
  200. }
  201. inline bool il2cpp_codegen_method_is_virtual(RuntimeMethod* method)
  202. {
  203. return method->slot != kInvalidIl2CppMethodSlot;
  204. }
  205. inline bool il2cpp_codegen_object_is_of_sealed_type(RuntimeObject* obj)
  206. {
  207. IL2CPP_ASSERT(obj);
  208. return (obj->klass->flags & TYPE_ATTRIBUTE_SEALED) != 0;
  209. }
  210. bool il2cpp_codegen_method_is_generic_instance(RuntimeMethod* method);
  211. RuntimeClass* il2cpp_codegen_method_get_declaring_type(RuntimeMethod* method);
  212. bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method);
  213. inline uint16_t il2cpp_codegen_method_get_slot(RuntimeMethod* method)
  214. {
  215. return method->slot;
  216. }
  217. IL2CPP_FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_virtual_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj)
  218. {
  219. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_virtual_invoke_data got called on a non-virtual method");
  220. return obj->klass->vtable[slot];
  221. }
  222. IL2CPP_FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, RuntimeObject* obj, const RuntimeClass* declaringInterface)
  223. {
  224. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_interface_invoke_data got called on a non-virtual method");
  225. return il2cpp::vm::ClassInlines::GetInterfaceInvokeDataFromVTable(obj, declaringInterface, slot);
  226. }
  227. const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method_internal(const RuntimeMethod* methodDefinition, const RuntimeMethod* inflatedMethod);
  228. IL2CPP_FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method(const RuntimeMethod* method, const RuntimeObject* obj)
  229. {
  230. uint16_t slot = method->slot;
  231. const RuntimeMethod* methodDefinition = obj->klass->vtable[slot].method;
  232. return il2cpp_codegen_get_generic_virtual_method_internal(methodDefinition, method);
  233. }
  234. IL2CPP_FORCE_INLINE void il2cpp_codegen_get_generic_virtual_invoke_data(const RuntimeMethod* method, const RuntimeObject* obj, VirtualInvokeData* invokeData)
  235. {
  236. const RuntimeMethod* targetRuntimeMethod = il2cpp_codegen_get_generic_virtual_method(method, obj);
  237. #if IL2CPP_DEBUG
  238. IL2CPP_ASSERT(targetRuntimeMethod);
  239. #endif
  240. invokeData->methodPtr = targetRuntimeMethod->methodPointer;
  241. invokeData->method = targetRuntimeMethod;
  242. }
  243. IL2CPP_FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_interface_method(const RuntimeMethod* method, RuntimeObject* obj)
  244. {
  245. const RuntimeMethod* methodDefinition = il2cpp::vm::ClassInlines::GetInterfaceInvokeDataFromVTable(obj, method->klass, method->slot).method;
  246. return il2cpp_codegen_get_generic_virtual_method_internal(methodDefinition, method);
  247. }
  248. IL2CPP_FORCE_INLINE void il2cpp_codegen_get_generic_interface_invoke_data(const RuntimeMethod* method, RuntimeObject* obj, VirtualInvokeData* invokeData)
  249. {
  250. const RuntimeMethod* targetRuntimeMethod = il2cpp_codegen_get_generic_interface_method(method, obj);
  251. #if IL2CPP_DEBUG
  252. IL2CPP_ASSERT(targetRuntimeMethod);
  253. #endif
  254. invokeData->methodPtr = targetRuntimeMethod->methodPointer;
  255. invokeData->method = targetRuntimeMethod;
  256. }
  257. inline RuntimeClass* InitializedTypeInfo(RuntimeClass* klass)
  258. {
  259. return il2cpp::vm::ClassInlines::InitFromCodegen(klass);
  260. }
  261. RuntimeClass* il2cpp_codegen_class_from_type_internal(const RuntimeType* type);
  262. inline RuntimeClass* il2cpp_codegen_class_from_type(const RuntimeType *type)
  263. {
  264. return InitializedTypeInfo(il2cpp_codegen_class_from_type_internal(type));
  265. }
  266. inline void* InterlockedExchangeImplRef(void** location, void* value)
  267. {
  268. return il2cpp::icalls::mscorlib::System::Threading::Interlocked::ExchangePointer(location, value);
  269. }
  270. template<typename T>
  271. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  272. {
  273. return (T)il2cpp::icalls::mscorlib::System::Threading::Interlocked::CompareExchange_T((void**)location, value, comparand);
  274. }
  275. template<typename T>
  276. inline T InterlockedExchangeImpl(T* location, T value)
  277. {
  278. return (T)InterlockedExchangeImplRef((void**)location, value);
  279. }
  280. void il2cpp_codegen_memory_barrier();
  281. inline void ArrayGetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, void* value)
  282. {
  283. memcpy(value, ((uint8_t*)thisPtr) + sizeof(RuntimeArray) + pos * thisPtr->klass->element_size, thisPtr->klass->element_size);
  284. }
  285. inline void ArraySetGenericValueImpl(RuntimeArray * thisPtr, int32_t pos, void* value)
  286. {
  287. memcpy(((uint8_t*)thisPtr) + sizeof(RuntimeArray) + pos * thisPtr->klass->element_size, value, thisPtr->klass->element_size);
  288. }
  289. RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length);
  290. RuntimeArray* GenArrayNew(RuntimeClass* arrayType, il2cpp_array_size_t* dimensions);
  291. // Performance optimization as detailed here: http://blogs.msdn.com/b/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx
  292. // Since array size is a signed int32_t, a single unsigned check can be performed to determine if index is less than array size.
  293. // Negative indices will map to a unsigned number greater than or equal to 2^31 which is larger than allowed for a valid array.
  294. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  295. do { \
  296. if (((uint32_t)(index)) >= ((uint32_t)length)) il2cpp_codegen_raise_exception (il2cpp_codegen_get_index_out_of_range_exception()); \
  297. } while (0)
  298. bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass);
  299. RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass);
  300. Il2CppMethodPointer il2cpp_codegen_resolve_icall(const char* name);
  301. Il2CppMethodPointer il2cpp_codegen_resolve(const PInvokeArguments& pinvokeArgs);
  302. template<typename FunctionPointerType, size_t dynamicLibraryLength, size_t entryPointLength>
  303. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const Il2CppNativeChar(&nativeDynamicLibrary)[dynamicLibraryLength], const char(&entryPoint)[entryPointLength],
  304. Il2CppCallConvention callingConvention, Il2CppCharSet charSet, int parameterSize, bool isNoMangle)
  305. {
  306. const PInvokeArguments pinvokeArgs =
  307. {
  308. il2cpp::utils::StringView<Il2CppNativeChar>(nativeDynamicLibrary),
  309. il2cpp::utils::StringView<char>(entryPoint),
  310. callingConvention,
  311. charSet,
  312. parameterSize,
  313. isNoMangle
  314. };
  315. return reinterpret_cast<FunctionPointerType>(il2cpp_codegen_resolve(pinvokeArgs));
  316. }
  317. void* il2cpp_codegen_marshal_allocate(size_t size);
  318. template<typename T>
  319. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  320. {
  321. return static_cast<T*>(il2cpp_codegen_marshal_allocate((il2cpp_array_size_t)(sizeof(T) * length)));
  322. }
  323. template<typename T>
  324. inline T* il2cpp_codegen_marshal_allocate()
  325. {
  326. return static_cast<T*>(il2cpp_codegen_marshal_allocate(sizeof(T)));
  327. }
  328. char* il2cpp_codegen_marshal_string(String_t* string);
  329. void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters);
  330. Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string);
  331. void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters);
  332. Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string);
  333. String_t* il2cpp_codegen_marshal_string_result(const char* value);
  334. String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value);
  335. String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value);
  336. void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value);
  337. char* il2cpp_codegen_marshal_empty_string_builder(StringBuilder_t* stringBuilder);
  338. char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder);
  339. Il2CppChar* il2cpp_codegen_marshal_empty_wstring_builder(StringBuilder_t* stringBuilder);
  340. Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder);
  341. void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer);
  342. void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer);
  343. Il2CppHString il2cpp_codegen_create_hstring(String_t* str);
  344. String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring);
  345. void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring);
  346. void il2cpp_codegen_marshal_type_to_native(Type_t* type, Il2CppWindowsRuntimeTypeName& nativeType);
  347. const Il2CppType* il2cpp_codegen_marshal_type_from_native_internal(Il2CppWindowsRuntimeTypeName& nativeType);
  348. inline Type_t* il2cpp_codegen_marshal_type_from_native(Il2CppWindowsRuntimeTypeName& nativeType)
  349. {
  350. const Il2CppType* type = il2cpp_codegen_marshal_type_from_native_internal(nativeType);
  351. if (type == NULL)
  352. return NULL;
  353. return il2cpp_codegen_type_get_object(type);
  354. }
  355. void il2cpp_codegen_delete_native_type(Il2CppWindowsRuntimeTypeName& nativeType);
  356. void il2cpp_codegen_marshal_free(void* ptr);
  357. Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d);
  358. Il2CppDelegate* il2cpp_codegen_marshal_function_ptr_to_delegate_internal(void* functionPtr, Il2CppClass* delegateType);
  359. template<typename T>
  360. inline T* il2cpp_codegen_marshal_function_ptr_to_delegate(Il2CppMethodPointer functionPtr, RuntimeClass* delegateType)
  361. {
  362. return (T*)il2cpp_codegen_marshal_function_ptr_to_delegate_internal(reinterpret_cast<void*>(functionPtr), delegateType);
  363. }
  364. void il2cpp_codegen_marshal_store_last_error();
  365. template<typename R, typename S>
  366. inline R il2cpp_codegen_cast_struct(S* s)
  367. {
  368. static_assert(sizeof(S) == sizeof(R), "Types with different sizes passed to il2cpp_codegen_cast_struct");
  369. R r;
  370. il2cpp_codegen_memcpy(&r, s, sizeof(R));
  371. return r;
  372. }
  373. #if _DEBUG
  374. void il2cpp_codegen_marshal_allocate_push_allocation_frame();
  375. void il2cpp_codegen_marshal_allocate_pop_allocation_frame();
  376. bool il2cpp_codegen_marshal_allocate_has_unfreed_allocations();
  377. void il2cpp_codegen_marshal_allocate_clear_all_tracked_allocations();
  378. struct ScopedMarshallingAllocationFrame
  379. {
  380. ScopedMarshallingAllocationFrame()
  381. {
  382. il2cpp_codegen_marshal_allocate_push_allocation_frame();
  383. }
  384. ~ScopedMarshallingAllocationFrame()
  385. {
  386. il2cpp_codegen_marshal_allocate_pop_allocation_frame();
  387. }
  388. };
  389. struct ScopedMarshallingAllocationCheck
  390. {
  391. ~ScopedMarshallingAllocationCheck()
  392. {
  393. if (il2cpp_codegen_marshal_allocate_has_unfreed_allocations())
  394. il2cpp_codegen_raise_exception(il2cpp_codegen_get_invalid_operation_exception("Error in marshaling allocation. Some memory has been leaked."));
  395. }
  396. private:
  397. ScopedMarshallingAllocationFrame m_AllocationFrame;
  398. };
  399. struct ScopedMarshalingAllocationClearer
  400. {
  401. ~ScopedMarshalingAllocationClearer()
  402. {
  403. il2cpp_codegen_marshal_allocate_clear_all_tracked_allocations();
  404. }
  405. private:
  406. ScopedMarshallingAllocationFrame m_AllocationFrame;
  407. };
  408. #endif
  409. inline void DivideByZeroCheck(int64_t denominator)
  410. {
  411. if (denominator != 0)
  412. return;
  413. il2cpp_codegen_raise_divide_by_zero_exception();
  414. }
  415. bool MethodIsStatic(const RuntimeMethod* method);
  416. bool MethodHasParameters(const RuntimeMethod* method);
  417. void il2cpp_codegen_runtime_class_init(RuntimeClass* klass);
  418. #define IL2CPP_RUNTIME_CLASS_INIT(klass) do { if((klass)->has_cctor && !(klass)->cctor_finished) il2cpp_codegen_runtime_class_init ((klass)); } while (0)
  419. // generic sharing
  420. #define IL2CPP_RGCTX_DATA(rgctxVar, index) (InitializedTypeInfo(rgctxVar[index].klass))
  421. #define IL2CPP_RGCTX_SIZEOF(rgctxVar, index) (il2cpp_codegen_sizeof(IL2CPP_RGCTX_DATA(rgctxVar, index)))
  422. #define IL2CPP_RGCTX_TYPE(rgctxVar, index) (rgctxVar[index].type)
  423. #define IL2CPP_RGCTX_METHOD_INFO(rgctxVar, index) (rgctxVar[index].method)
  424. #define IL2CPP_RGCTX_FIELD_INFO(klass, index) ((klass)->fields+index)
  425. inline void ArrayElementTypeCheck(RuntimeArray* array, void* value)
  426. {
  427. #if !IL2CPP_TINY
  428. if (value != NULL && IsInst((RuntimeObject*)value, array->klass->element_class) == NULL)
  429. il2cpp_codegen_raise_exception(il2cpp_codegen_get_array_type_mismatch_exception());
  430. #endif
  431. }
  432. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot)
  433. {
  434. if (!pThis)
  435. il2cpp_codegen_raise_null_reference_exception();
  436. return pThis->klass->vtable[slot].method;
  437. }
  438. inline const RuntimeMethod* GetInterfaceMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot, RuntimeClass* declaringInterface)
  439. {
  440. if (!pThis)
  441. il2cpp_codegen_raise_null_reference_exception();
  442. return il2cpp::vm::ClassInlines::GetInterfaceInvokeDataFromVTable(pThis, declaringInterface, slot).method;
  443. }
  444. void il2cpp_codegen_initialize_runtime_metadata(uintptr_t* metadataPointer);
  445. void* il2cpp_codegen_initialize_runtime_metadata_inline(uintptr_t* metadataPointer);
  446. bool il2cpp_codegen_class_is_value_type(RuntimeClass* type);
  447. inline bool il2cpp_codegen_type_implements_virtual_method(RuntimeClass* type, const RuntimeMethod* method)
  448. {
  449. IL2CPP_ASSERT(il2cpp_codegen_class_is_value_type(type));
  450. return method->klass == type;
  451. }
  452. MethodBase_t* il2cpp_codegen_get_method_object_internal(const RuntimeMethod* method, RuntimeClass* refclass);
  453. const RuntimeMethod* il2cpp_codegen_get_generic_method_definition(const RuntimeMethod* method);
  454. inline MethodBase_t* il2cpp_codegen_get_method_object(const RuntimeMethod* method)
  455. {
  456. if (method->is_inflated)
  457. method = il2cpp_codegen_get_generic_method_definition(method);
  458. return il2cpp_codegen_get_method_object_internal(method, method->klass);
  459. }
  460. Type_t* il2cpp_codegen_get_type(const RuntimeMethod* getTypeMethod, String_t* typeName, const RuntimeMethod* callingMethod);
  461. Type_t* il2cpp_codegen_get_type(const RuntimeMethod* getTypeMethod, String_t* typeName, bool throwOnError, const RuntimeMethod* callingMethod);
  462. Type_t* il2cpp_codegen_get_type(const RuntimeMethod* getTypeMethod, String_t* typeName, bool throwOnError, bool ignoreCase, const RuntimeMethod* callingMethod);
  463. Assembly_t* il2cpp_codegen_get_executing_assembly(const RuntimeMethod* method);
  464. // Atomic
  465. void* il2cpp_codegen_atomic_compare_exchange_pointer(void** dest, void* exchange, void* comparand);
  466. // COM
  467. void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant);
  468. RuntimeObject* il2cpp_codegen_com_marshal_variant_result(const Il2CppVariant* variant);
  469. void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant);
  470. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray);
  471. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray);
  472. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray);
  473. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray);
  474. void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray);
  475. void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity);
  476. void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw);
  477. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass);
  478. template<typename T>
  479. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iunknown(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  480. {
  481. return static_cast<T*>(il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(unknown, fallbackClass));
  482. }
  483. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass);
  484. template<typename T>
  485. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  486. {
  487. return static_cast<T*>(il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(unknown, fallbackClass));
  488. }
  489. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(Il2CppIUnknown* unknown, RuntimeClass* objectClass);
  490. template<typename T>
  491. inline T* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  492. {
  493. return static_cast<T*>(il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(unknown, objectClass));
  494. }
  495. Il2CppIUnknown* il2cpp_codegen_com_query_interface_internal(Il2CppComObject* rcw, const Il2CppGuid& guid);
  496. Il2CppIUnknown* il2cpp_codegen_com_query_interface_no_throw_internal(Il2CppComObject* rcw, const Il2CppGuid& guid);
  497. void il2cpp_codegen_com_cache_queried_interface(Il2CppComObject* rcw, const Il2CppGuid& guid, Il2CppIUnknown* queriedInterface);
  498. template<typename T>
  499. inline T* il2cpp_codegen_com_query_interface(Il2CppComObject* rcw)
  500. {
  501. return static_cast<T*>(il2cpp_codegen_com_query_interface_internal(rcw, T::IID));
  502. }
  503. template<typename T>
  504. inline T* il2cpp_codegen_com_query_interface_no_throw(Il2CppComObject* rcw)
  505. {
  506. return static_cast<T*>(il2cpp_codegen_com_query_interface_no_throw_internal(rcw, T::IID));
  507. }
  508. void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw);
  509. Il2CppIUnknown* il2cpp_codegen_com_get_or_create_ccw_internal(RuntimeObject* obj, const Il2CppGuid& iid);
  510. template<typename InterfaceType>
  511. inline InterfaceType* il2cpp_codegen_com_get_or_create_ccw(RuntimeObject* obj)
  512. {
  513. return static_cast<InterfaceType*>(il2cpp_codegen_com_get_or_create_ccw_internal(obj, InterfaceType::IID));
  514. }
  515. inline intptr_t il2cpp_codegen_com_get_iunknown_for_object(RuntimeObject* obj)
  516. {
  517. return reinterpret_cast<intptr_t>(il2cpp_codegen_com_get_or_create_ccw_internal(obj, Il2CppIUnknown::IID));
  518. }
  519. Il2CppObject* il2cpp_codegen_com_unpack_ccw(Il2CppIUnknown* obj);
  520. inline void il2cpp_codegen_com_raise_exception_if_failed(il2cpp_hresult_t hr, bool defaultToCOMException)
  521. {
  522. // Copied from il2cpp::vm::Exception::RaiseIfFailed to keep inlined
  523. if (IL2CPP_HR_FAILED(hr))
  524. il2cpp_codegen_raise_exception(hr, defaultToCOMException);
  525. }
  526. inline RuntimeException* il2cpp_codegen_com_get_exception(il2cpp_hresult_t hr, bool defaultToCOMException)
  527. {
  528. return (RuntimeException*)il2cpp_codegen_get_exception(hr, defaultToCOMException);
  529. }
  530. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(const char* fromType, const char* toType);
  531. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(RuntimeObject* value, const char* fromType, const char* toType);
  532. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index);
  533. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(RuntimeObject* value, const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index);
  534. void il2cpp_codegen_store_exception_info(RuntimeException* ex, String_t* exceptionString);
  535. Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName);
  536. // delegate
  537. Il2CppAsyncResult* il2cpp_codegen_delegate_begin_invoke(RuntimeDelegate* delegate, void** params, RuntimeDelegate* asyncCallback, RuntimeObject* state);
  538. RuntimeObject* il2cpp_codegen_delegate_end_invoke(Il2CppAsyncResult* asyncResult, void **out_args);
  539. inline const Il2CppGenericInst* il2cpp_codegen_get_generic_class_inst(RuntimeClass* genericClass)
  540. {
  541. IL2CPP_ASSERT(genericClass->generic_class);
  542. return genericClass->generic_class->context.class_inst;
  543. }
  544. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst);
  545. inline void* il2cpp_codegen_static_fields_for(RuntimeClass* klass)
  546. {
  547. return klass->static_fields;
  548. }
  549. inline Il2CppMethodPointer il2cpp_codegen_get_method_pointer(const RuntimeMethod* method)
  550. {
  551. return method->methodPointer;
  552. }
  553. inline const RuntimeType* il2cpp_codegen_method_return_type(const RuntimeMethod* method)
  554. {
  555. return method->return_type;
  556. }
  557. inline int il2cpp_codegen_method_parameter_count(const RuntimeMethod* method)
  558. {
  559. return method->parameters_count;
  560. }
  561. inline bool il2cpp_codegen_is_import_or_windows_runtime(const RuntimeObject *object)
  562. {
  563. return object->klass->is_import_or_windows_runtime;
  564. }
  565. inline intptr_t il2cpp_codegen_get_com_interface_for_object(Il2CppObject* object, Type_t* type)
  566. {
  567. return il2cpp::icalls::mscorlib::System::Runtime::InteropServices::Marshal::GetCCW(object, reinterpret_cast<Il2CppReflectionType*>(type));
  568. }
  569. NORETURN void il2cpp_codegen_raise_profile_exception(const RuntimeMethod* method);
  570. const char* il2cpp_codegen_get_field_data(RuntimeField* field);
  571. template<typename T>
  572. inline void* il2cpp_codegen_unsafe_cast(T* ptr)
  573. {
  574. return reinterpret_cast<void*>(ptr);
  575. }
  576. #if IL2CPP_TINY
  577. // Add intrinsics used by Tiny.
  578. #include "utils/MemoryUtils.h"
  579. Type_t* il2cpp_codegen_get_type(Il2CppObject* obj);
  580. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* genArray, int32_t dimension)
  581. {
  582. if (genArray->bounds == NULL)
  583. return il2cpp_codegen_get_array_length(genArray);
  584. return static_cast<int32_t>(genArray->bounds[dimension].length);
  585. }
  586. MulticastDelegate_t* il2cpp_codegen_create_combined_delegate(Type_t* type, Il2CppArray* delegates, int delegateCount);
  587. inline String_t* il2cpp_codegen_marshal_ptr_to_string_ansi(intptr_t ptr)
  588. {
  589. return il2cpp_codegen_marshal_string_result(reinterpret_cast<const char*>(ptr));
  590. }
  591. inline intptr_t il2cpp_codegen_marshal_string_to_co_task_mem_ansi(String_t* ptr)
  592. {
  593. return reinterpret_cast<intptr_t>(il2cpp_codegen_marshal_string(ptr));
  594. }
  595. inline void il2cpp_codegen_marshal_string_free_co_task_mem(intptr_t ptr)
  596. {
  597. il2cpp_codegen_marshal_free(reinterpret_cast<void*>(ptr));
  598. }
  599. struct Delegate_t;
  600. inline String_t* il2cpp_codegen_string_new_length(int length)
  601. {
  602. return reinterpret_cast<String_t*>(il2cpp::vm::String::NewSize(length));
  603. }
  604. Type_t* il2cpp_codegen_get_base_type(const Type_t* t);
  605. bool il2cpp_codegen_is_assignable_from(Type_t* left, Type_t* right);
  606. template<typename T>
  607. struct Il2CppReversePInvokeMethodHolder
  608. {
  609. Il2CppReversePInvokeMethodHolder(T** storageAddress) :
  610. m_LastValue(*storageAddress),
  611. m_StorageAddress(storageAddress)
  612. {
  613. }
  614. ~Il2CppReversePInvokeMethodHolder()
  615. {
  616. *m_StorageAddress = m_LastValue;
  617. }
  618. private:
  619. T* const m_LastValue;
  620. T** const m_StorageAddress;
  621. };
  622. void il2cpp_codegen_no_reverse_pinvoke_wrapper(const char* methodName, const char* reason);
  623. bool il2cpp_codegen_type_is_interface(Type_t* t);
  624. bool il2cpp_codegen_type_is_abstract(Type_t* t);
  625. bool il2cpp_codegen_type_is_pointer(Type_t* t);
  626. NORETURN void il2cpp_codegen_raise_exception(const char* message);
  627. #endif