il2cpp-codegen-tiny.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. #pragma once
  2. #include "il2cpp-codegen-common-small.h"
  3. #include "il2cpp-object-internals.h"
  4. #include "il2cpp-debug-metadata.h"
  5. #include "os/Memory.h"
  6. #include "vm/Array.h"
  7. #include "vm/Exception.h"
  8. #include "vm/Object.h"
  9. #include "vm/PlatformInvoke.h"
  10. #include "vm/ScopedThreadAttacher.h"
  11. #include "vm/String.h"
  12. #include "vm/Runtime.h"
  13. #include "vm/Type.h"
  14. #include "vm/TypeUniverse.h"
  15. #include "utils/ExceptionSupportStack.h"
  16. #include "utils/MemoryUtils.h"
  17. #include "utils/StringView.h"
  18. #include <string>
  19. #include "gc/gc_wrapper.h"
  20. #include "vm-utils/icalls/mscorlib/System.Threading/Interlocked.h"
  21. struct Exception_t;
  22. struct Delegate_t;
  23. struct MulticastDelegate_t;
  24. struct String_t;
  25. struct Type_t;
  26. typedef Il2CppObject RuntimeObject;
  27. typedef Il2CppArray RuntimeArray;
  28. #if IL2CPP_COMPILER_MSVC
  29. #define DEFAULT_CALL STDCALL
  30. #else
  31. #define DEFAULT_CALL
  32. #endif
  33. inline void il2cpp_codegen_memcpy(void* dest, const void* src, size_t count)
  34. {
  35. memcpy(dest, src, count);
  36. }
  37. inline void il2cpp_codegen_memset(void* ptr, int value, size_t num)
  38. {
  39. memset(ptr, value, num);
  40. }
  41. inline RuntimeObject* il2cpp_codegen_object_new(size_t size, TinyType* typeInfo)
  42. {
  43. return (RuntimeObject*)tiny::vm::Object::New(size, typeInfo);
  44. }
  45. inline Il2CppObject* Box(TinyType* type, void* value, size_t size)
  46. {
  47. COMPILE_TIME_CONST size_t alignedObjectSize = IL2CPP_ALIGNED_OBJECT_SIZE;
  48. Il2CppObject* obj = il2cpp_codegen_object_new(size + alignedObjectSize, type);
  49. memcpy(reinterpret_cast<uint8_t*>(obj) + alignedObjectSize, value, size);
  50. return obj;
  51. }
  52. template<typename NullableType, typename ArgumentType>
  53. inline Il2CppObject* BoxNullable(TinyType* type, NullableType* value)
  54. {
  55. /*
  56. From ECMA-335, I.8.2.4 Boxing and unboxing of values:
  57. All value types have an operation called box. Boxing a value of any value type produces its boxed value;
  58. i.e., a value of the corresponding boxed type containing a bitwise copy of the original value. If the
  59. value type is a nullable type defined as an instantiation of the value type System.Nullable<T> the result
  60. is a null reference or bitwise copy of its Value property of type T, depending on its HasValue property
  61. (false and true, respectively).
  62. */
  63. uint32_t valueSize = sizeof(ArgumentType);
  64. bool hasValue = *reinterpret_cast<bool*>(reinterpret_cast<uint8_t*>(value) + valueSize);
  65. if (!hasValue)
  66. return NULL;
  67. return Box(type, value, valueSize);
  68. }
  69. inline void* UnBox(Il2CppObject* obj)
  70. {
  71. return tiny::vm::Object::Unbox(obj);
  72. }
  73. inline void* UnBox(Il2CppObject* obj, TinyType* expectedBoxedType)
  74. {
  75. COMPILE_TIME_CONST size_t alignedObjectSize = IL2CPP_ALIGNED_OBJECT_SIZE;
  76. if (obj->klass == expectedBoxedType)
  77. return reinterpret_cast<uint8_t*>(obj) + alignedObjectSize;
  78. tiny::vm::Exception::RaiseInvalidCastException(obj, expectedBoxedType);
  79. return NULL;
  80. }
  81. template<typename ArgumentType>
  82. inline void UnBoxNullable(Il2CppObject* obj, TinyType* expectedBoxedClass, void* storage)
  83. {
  84. // We only need to do type checks if obj is not null
  85. // Unboxing null nullable is perfectly valid and returns an instance that has no value
  86. if (obj != NULL)
  87. {
  88. if (obj->klass != expectedBoxedClass)
  89. tiny::vm::Exception::RaiseInvalidCastException(obj, expectedBoxedClass);
  90. }
  91. uint32_t valueSize = sizeof(ArgumentType);
  92. if (obj == NULL)
  93. {
  94. memset(storage, 0, valueSize);
  95. *(static_cast<uint8_t*>(storage) + valueSize) = false;
  96. }
  97. else
  98. {
  99. memcpy(storage, UnBox(obj), valueSize);
  100. *(static_cast<uint8_t*>(storage) + valueSize) = true;
  101. }
  102. }
  103. inline bool il2cpp_codegen_is_fake_boxed_object(RuntimeObject* object)
  104. {
  105. return false;
  106. }
  107. // Exception support macros
  108. #define IL2CPP_PUSH_ACTIVE_EXCEPTION(Exception) \
  109. __active_exceptions.push(Exception)
  110. #define IL2CPP_POP_ACTIVE_EXCEPTION() \
  111. __active_exceptions.pop()
  112. #define IL2CPP_GET_ACTIVE_EXCEPTION(ExcType) \
  113. (ExcType)__active_exceptions.top()
  114. #define IL2CPP_LEAVE(Offset, Target) \
  115. __leave_targets.push(Offset); \
  116. goto Target;
  117. #define IL2CPP_END_FINALLY(Id) \
  118. goto __CLEANUP_ ## Id;
  119. #define IL2CPP_CLEANUP(Id) \
  120. __CLEANUP_ ## Id:
  121. #define IL2CPP_RETHROW_IF_UNHANDLED(ExcType) \
  122. if(__last_unhandled_exception) { \
  123. ExcType _tmp_exception_local = __last_unhandled_exception; \
  124. __last_unhandled_exception = 0; \
  125. il2cpp_codegen_raise_exception(_tmp_exception_local); \
  126. }
  127. #define IL2CPP_JUMP_TBL(Offset, Target) \
  128. if(!__leave_targets.empty() && __leave_targets.top() == Offset) { \
  129. __leave_targets.pop(); \
  130. goto Target; \
  131. }
  132. #define IL2CPP_END_CLEANUP(Offset, Target) \
  133. if(!__leave_targets.empty() && __leave_targets.top() == Offset) \
  134. goto Target;
  135. template<typename T>
  136. inline void Il2CppCodeGenWriteBarrier(T** targetAddress, T* object)
  137. {
  138. // TODO
  139. }
  140. inline void il2cpp_codegen_memory_barrier()
  141. {
  142. // The joy of singlethreading
  143. }
  144. inline TinyType* LookupTypeInfoFromCursor(uint32_t typeCursor)
  145. {
  146. return reinterpret_cast<TinyType*>(Il2CppGetTinyTypeUniverse() + typeCursor);
  147. }
  148. inline String_t* LookupStringFromCursor(uint32_t stringCursor)
  149. {
  150. return reinterpret_cast<String_t*>(Il2CppGetStringLiterals() + stringCursor);
  151. }
  152. inline bool HasParentOrIs(const TinyType* type, const TinyType* targetType)
  153. {
  154. IL2CPP_ASSERT(type != NULL);
  155. IL2CPP_ASSERT(targetType != NULL);
  156. if (type == targetType)
  157. return true;
  158. uint16_t typeHierarchySize = type->typeHierarchySize;
  159. uint16_t targetTypeHierarchySize = targetType->typeHierarchySize;
  160. if (typeHierarchySize <= targetTypeHierarchySize)
  161. return false;
  162. if (type->GetTypeHierarchy()[targetTypeHierarchySize] == targetType)
  163. return true;
  164. return false;
  165. }
  166. inline bool IsAssignableFrom(const TinyType* klass, const TinyType* oklass)
  167. {
  168. if (HasParentOrIs(oklass, klass))
  169. return true;
  170. const TinyType* const* interfaces = oklass->GetInterfaces();
  171. uint8_t size = oklass->interfacesSize;
  172. for (uint8_t i = 0; i != size; i++)
  173. {
  174. if (interfaces[i] == klass)
  175. return true;
  176. }
  177. return false;
  178. }
  179. inline Il2CppObject* IsInst(Il2CppObject* obj, TinyType* klass)
  180. {
  181. if (!obj)
  182. return NULL;
  183. TinyType* objClass = obj->klass;
  184. if (IsAssignableFrom(klass, objClass))
  185. return obj;
  186. return NULL;
  187. }
  188. inline RuntimeObject* IsInstClass(RuntimeObject* obj, TinyType* targetType)
  189. {
  190. IL2CPP_ASSERT(targetType != NULL);
  191. if (!obj)
  192. return NULL;
  193. if (HasParentOrIs(obj->klass, targetType))
  194. return obj;
  195. return NULL;
  196. }
  197. inline RuntimeObject* IsInstSealed(RuntimeObject* obj, TinyType* targetType)
  198. {
  199. if (!obj)
  200. return NULL;
  201. // optimized version to compare sealed classes
  202. return (obj->klass == targetType ? obj : NULL);
  203. }
  204. inline RuntimeObject* Castclass(RuntimeObject* obj, TinyType* targetType)
  205. {
  206. if (!obj)
  207. return NULL;
  208. RuntimeObject* result = IsInst(obj, targetType);
  209. if (result)
  210. return result;
  211. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  212. return NULL;
  213. }
  214. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, TinyType* targetType)
  215. {
  216. if (!obj)
  217. return NULL;
  218. RuntimeObject* result = IsInstSealed(obj, targetType);
  219. if (result)
  220. return result;
  221. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  222. return NULL;
  223. }
  224. inline RuntimeObject* CastclassClass(RuntimeObject *obj, TinyType* targetType)
  225. {
  226. if (!obj)
  227. return NULL;
  228. RuntimeObject* result = IsInstClass(obj, targetType);
  229. if (result)
  230. return result;
  231. tiny::vm::Exception::RaiseInvalidCastException(obj, targetType);
  232. return NULL;
  233. }
  234. inline bool il2cpp_codegen_is_assignable_from(Type_t* left, Type_t* right)
  235. {
  236. if (right == NULL)
  237. return false;
  238. return IsAssignableFrom(reinterpret_cast<Il2CppReflectionType*>(left)->typeHandle, reinterpret_cast<Il2CppReflectionType*>(right)->typeHandle);
  239. }
  240. // il2cpp generates direct calls to this specific name
  241. inline bool il2cpp_codegen_class_is_assignable_from(TinyType* left, TinyType* right)
  242. {
  243. if (right == NULL)
  244. return false;
  245. return IsAssignableFrom(left, right);
  246. }
  247. inline TinyType* il2cpp_codegen_object_class(RuntimeObject *obj)
  248. {
  249. return obj->klass;
  250. }
  251. inline String_t* il2cpp_codegen_string_new_length(int length)
  252. {
  253. return reinterpret_cast<String_t*>(tiny::vm::String::NewLen(length));
  254. }
  255. inline String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  256. {
  257. return (String_t*)tiny::vm::String::NewLen(str.Str(), static_cast<uint32_t>(str.Length()));
  258. }
  259. template<typename T>
  260. inline Il2CppArray* SZArrayNew(TinyType* arrayType, uint32_t elementSize, uint32_t arrayLength)
  261. {
  262. return tiny::vm::Array::New<T>(arrayType, elementSize, arrayLength);
  263. }
  264. template<size_t N>
  265. inline Il2CppMultidimensionalArray<N>* GenArrayNew(TinyType* arrayType, uint32_t elementSize, il2cpp_array_size_t(&dimensions)[N])
  266. {
  267. il2cpp_array_size_t arrayLength = elementSize;
  268. for (uint32_t i = 0; i < N; i++)
  269. arrayLength *= dimensions[i];
  270. Il2CppMultidimensionalArray<N>* genArray = static_cast<Il2CppMultidimensionalArray<N>*>(il2cpp_codegen_object_new(sizeof(Il2CppMultidimensionalArray<N>) + elementSize * arrayLength, arrayType));
  271. for (uint32_t i = 0; i < N; i++)
  272. genArray->bounds[i] = dimensions[i];
  273. return genArray;
  274. }
  275. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* genArray, int32_t dimension)
  276. {
  277. return static_cast<int32_t>(reinterpret_cast<Il2CppMultidimensionalArray<1>*>(genArray)->bounds[dimension]);
  278. }
  279. inline Type_t* il2cpp_codegen_get_type(Il2CppObject* obj)
  280. {
  281. return reinterpret_cast<Type_t*>(tiny::vm::Type::GetTypeFromHandle((intptr_t)obj->klass));
  282. }
  283. inline Type_t* il2cpp_codegen_get_base_type(const Type_t* t)
  284. {
  285. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  286. const TinyType* tinyType = type->typeHandle;
  287. uint8_t typeHierarchySize = tinyType->typeHierarchySize;
  288. if (typeHierarchySize == 0)
  289. return NULL;
  290. return const_cast<Type_t*>(reinterpret_cast<const Type_t*>(tiny::vm::Type::GetTypeFromHandle((intptr_t)(tinyType->GetTypeHierarchy()[typeHierarchySize - 1]))));
  291. }
  292. inline MulticastDelegate_t* il2cpp_codegen_create_combined_delegate(Type_t* type, Il2CppArray* delegates, int delegateCount)
  293. {
  294. Il2CppMulticastDelegate* result = static_cast<Il2CppMulticastDelegate*>(il2cpp_codegen_object_new(sizeof(Il2CppMulticastDelegate), const_cast<TinyType*>(reinterpret_cast<Il2CppReflectionType*>(type)->typeHandle)));
  295. result->delegates = delegates;
  296. result->delegateCount = delegateCount;
  297. return reinterpret_cast<MulticastDelegate_t*>(result);
  298. }
  299. inline const VirtualInvokeData& il2cpp_codegen_get_virtual_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj)
  300. {
  301. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_virtual_invoke_data got called on a non-virtual method");
  302. return obj->klass->GetVTable()[slot];
  303. }
  304. inline const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, const Il2CppObject* obj, TinyType* declaringInterface)
  305. {
  306. for (int i = 0; i < obj->klass->interfacesSize; ++i)
  307. {
  308. if (obj->klass->GetInterfaces()[i] == declaringInterface)
  309. return il2cpp_codegen_get_virtual_invoke_data((Il2CppMethodSlot)obj->klass->GetInterfaceOffsets()[i] + slot, obj);
  310. }
  311. tiny::vm::Exception::Raise();
  312. IL2CPP_UNREACHABLE;
  313. }
  314. inline Exception_t* il2cpp_codegen_get_overflow_exception()
  315. {
  316. return NULL;
  317. }
  318. inline Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  319. {
  320. return NULL;
  321. }
  322. inline Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  323. {
  324. return NULL;
  325. }
  326. NORETURN inline void il2cpp_codegen_no_return()
  327. {
  328. IL2CPP_UNREACHABLE;
  329. }
  330. NORETURN inline void il2cpp_codegen_raise_exception(Exception_t* ex, RuntimeMethod* lastManagedFrame)
  331. {
  332. tiny::vm::Exception::Raise((Il2CppException*)ex);
  333. IL2CPP_UNREACHABLE;
  334. }
  335. NORETURN inline void il2cpp_codegen_raise_exception(const char* message)
  336. {
  337. tiny::vm::Exception::Raise(message);
  338. IL2CPP_UNREACHABLE;
  339. }
  340. NORETURN void il2cpp_codegen_raise_generic_virtual_method_exception(const char* methodFullName);
  341. inline Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  342. {
  343. return NULL;
  344. }
  345. #define IL2CPP_RAISE_MANAGED_EXCEPTION(ex, lastManagedFrame) \
  346. do {\
  347. il2cpp_codegen_raise_exception(ex);\
  348. IL2CPP_UNREACHABLE;\
  349. } while (0)
  350. #if _DEBUG
  351. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  352. do { \
  353. if (((uint32_t)(index)) >= ((uint32_t)length)) tiny::vm::Exception::RaiseGetIndexOutOfRangeException(); \
  354. } while (0)
  355. #else
  356. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length)
  357. #endif
  358. inline void ArrayElementTypeCheck(Il2CppArray* array, void* value)
  359. {
  360. }
  361. template<typename FunctionPointerType, size_t dynamicLibraryLength, size_t entryPointLength>
  362. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const Il2CppNativeChar(&nativeDynamicLibrary)[dynamicLibraryLength], const char(&entryPoint)[entryPointLength],
  363. Il2CppCallConvention callingConvention, Il2CppCharSet charSet, int parameterSize, bool isNoMangle)
  364. {
  365. const PInvokeArguments pinvokeArgs =
  366. {
  367. il2cpp::utils::StringView<Il2CppNativeChar>(nativeDynamicLibrary),
  368. il2cpp::utils::StringView<char>(entryPoint),
  369. callingConvention,
  370. charSet,
  371. parameterSize,
  372. isNoMangle
  373. };
  374. return reinterpret_cast<FunctionPointerType>(tiny::vm::PlatformInvoke::Resolve(pinvokeArgs));
  375. }
  376. template<typename T>
  377. inline T* il2cpp_codegen_marshal_allocate()
  378. {
  379. return static_cast<T*>(tiny::vm::PlatformInvoke::MarshalAllocate(sizeof(T)));
  380. }
  381. inline char* il2cpp_codegen_marshal_string(String_t* string)
  382. {
  383. if (string == NULL)
  384. return NULL;
  385. Il2CppString* managedString = ((Il2CppString*)string);
  386. return tiny::vm::PlatformInvoke::MarshalCSharpStringToCppString(managedString->chars, managedString->length);
  387. }
  388. inline void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, uint32_t numberOfCharacters)
  389. {
  390. IL2CPP_ASSERT(numberOfCharacters > 0);
  391. if (string == NULL)
  392. {
  393. *buffer = '\0';
  394. return;
  395. }
  396. Il2CppString* managedString = ((Il2CppString*)string);
  397. tiny::vm::PlatformInvoke::MarshalCSharpStringToFixedCppStringBuffer(managedString->chars, managedString->length, buffer, numberOfCharacters);
  398. }
  399. inline Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  400. {
  401. if (string == NULL)
  402. return NULL;
  403. Il2CppString* managedString = ((Il2CppString*)string);
  404. return tiny::vm::PlatformInvoke::MarshalCSharpStringToCppWString(managedString->chars, managedString->length);
  405. }
  406. inline void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, uint32_t numberOfCharacters)
  407. {
  408. IL2CPP_ASSERT(numberOfCharacters > 0);
  409. if (string == NULL)
  410. {
  411. *buffer = '\0';
  412. return;
  413. }
  414. Il2CppString* managedString = ((Il2CppString*)string);
  415. tiny::vm::PlatformInvoke::MarshalCSharpStringToFixedCppWStringBuffer(managedString->chars, managedString->length, buffer, numberOfCharacters);
  416. }
  417. inline String_t* il2cpp_codegen_marshal_string_result(const char* value)
  418. {
  419. if (value == NULL)
  420. return NULL;
  421. return reinterpret_cast<String_t*>(tiny::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value));
  422. }
  423. inline String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  424. {
  425. if (value == NULL)
  426. return NULL;
  427. return reinterpret_cast<String_t*>(tiny::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value));
  428. }
  429. template<typename T>
  430. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  431. {
  432. return static_cast<T*>(tiny::vm::PlatformInvoke::MarshalAllocate(sizeof(T) * length));
  433. }
  434. inline void il2cpp_codegen_marshal_free(void* ptr)
  435. {
  436. tiny::vm::PlatformInvoke::MarshalFree(ptr);
  437. }
  438. inline String_t* il2cpp_codegen_marshal_ptr_to_string_ansi(intptr_t ptr)
  439. {
  440. return il2cpp_codegen_marshal_string_result(reinterpret_cast<const char*>(ptr));
  441. }
  442. inline intptr_t il2cpp_codegen_marshal_string_to_co_task_mem_ansi(String_t* ptr)
  443. {
  444. return reinterpret_cast<intptr_t>(il2cpp_codegen_marshal_string(ptr));
  445. }
  446. inline void il2cpp_codegen_marshal_string_free_co_task_mem(intptr_t ptr)
  447. {
  448. il2cpp_codegen_marshal_free(reinterpret_cast<void*>(ptr));
  449. }
  450. template<typename T>
  451. struct Il2CppReversePInvokeMethodHolder
  452. {
  453. Il2CppReversePInvokeMethodHolder(T** storageAddress) :
  454. m_LastValue(*storageAddress),
  455. m_StorageAddress(storageAddress)
  456. {
  457. }
  458. ~Il2CppReversePInvokeMethodHolder()
  459. {
  460. *m_StorageAddress = m_LastValue;
  461. }
  462. private:
  463. T* const m_LastValue;
  464. T** const m_StorageAddress;
  465. };
  466. inline void* InterlockedExchangeImplRef(void** location, void* value)
  467. {
  468. return tiny::icalls::mscorlib::System::Threading::Interlocked::ExchangePointer(location, value);
  469. }
  470. template<typename T>
  471. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  472. {
  473. return (T)tiny::icalls::mscorlib::System::Threading::Interlocked::CompareExchange_T((void**)location, value, comparand);
  474. }
  475. template<typename T>
  476. inline T InterlockedExchangeImpl(T* location, T value)
  477. {
  478. return (T)InterlockedExchangeImplRef((void**)location, value);
  479. }
  480. void il2cpp_codegen_stacktrace_push_frame(TinyStackFrameInfo& frame);
  481. void il2cpp_codegen_stacktrace_pop_frame();
  482. struct StackTraceSentry
  483. {
  484. StackTraceSentry(const RuntimeMethod* method) : m_method(method)
  485. {
  486. TinyStackFrameInfo frame_info;
  487. frame_info.method = (TinyMethod*)method;
  488. il2cpp_codegen_stacktrace_push_frame(frame_info);
  489. }
  490. ~StackTraceSentry()
  491. {
  492. il2cpp_codegen_stacktrace_pop_frame();
  493. }
  494. private:
  495. const RuntimeMethod* m_method;
  496. };
  497. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot)
  498. {
  499. if (!pThis)
  500. tiny::vm::Exception::Raise();
  501. return (const RuntimeMethod*)pThis->klass->GetVTable()[slot];
  502. }
  503. inline void il2cpp_codegen_no_reverse_pinvoke_wrapper(const char* methodName, const char* reason)
  504. {
  505. std::string message = "No reverse pinvoke wrapper exists for method: '";
  506. message += methodName;
  507. message += "' because ";
  508. message += reason;
  509. tiny::vm::Runtime::FailFast(message.c_str());
  510. }
  511. #define IL2CPP_TINY_IS_INTERFACE 1
  512. #define IL2CPP_TINY_IS_ABSTRACT 2
  513. #define IL2CPP_TINY_IS_POINTER 4
  514. inline bool il2cpp_codegen_type_is_interface(Type_t* t)
  515. {
  516. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  517. const TinyType* tinyType = type->typeHandle;
  518. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_INTERFACE)
  519. return true;
  520. return false;
  521. }
  522. inline bool il2cpp_codegen_type_is_abstract(Type_t* t)
  523. {
  524. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  525. const TinyType* tinyType = type->typeHandle;
  526. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_ABSTRACT)
  527. return true;
  528. return false;
  529. }
  530. inline bool il2cpp_codegen_type_is_pointer(Type_t* t)
  531. {
  532. const Il2CppReflectionType* type = reinterpret_cast<const Il2CppReflectionType*>(t);
  533. const TinyType* tinyType = type->typeHandle;
  534. if (IL2CPP_TINY_ADDITIONAL_TYPE_METADATA(tinyType->packedVtableSizeAndAdditionalTypeMetadata) & IL2CPP_TINY_IS_POINTER)
  535. return true;
  536. return false;
  537. }
  538. template<typename T>
  539. void ArrayGetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, T* value)
  540. {
  541. memcpy(value, ((uint8_t*)thisPtr) + sizeof(RuntimeArray) + pos * sizeof(T), sizeof(T));
  542. }
  543. template<typename T>
  544. void ArraySetGenericValueImpl(RuntimeArray * thisPtr, int32_t pos, T* value)
  545. {
  546. memcpy(((uint8_t*)thisPtr) + sizeof(RuntimeArray) + pos * sizeof(T), value, sizeof(T));
  547. }
  548. void il2cpp_codegen_marshal_store_last_error();
  549. template<typename T>
  550. inline void* il2cpp_codegen_unsafe_cast(T* ptr)
  551. {
  552. return reinterpret_cast<void*>(ptr);
  553. }