il2cpp-codegen-il2cpp.h 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646
  1. #pragma once
  2. #include "il2cpp-codegen-common.h"
  3. #include "il2cpp-pinvoke-support.h"
  4. #include "il2cpp-object-internals.h"
  5. #include "il2cpp-class-internals.h"
  6. #include "il2cpp-tabledefs.h"
  7. #include "icalls/mscorlib/System.Runtime.InteropServices/Marshal.h"
  8. #include "vm-utils/icalls/mscorlib/System.Threading/Interlocked.h"
  9. #include "vm-utils/VmThreadUtils.h"
  10. #include "vm-utils/Debugger.h"
  11. #include "vm-utils/Finally.h"
  12. #include "vm/ClassInlines.h"
  13. #include "vm/ScopedThreadAttacher.h"
  14. #include "vm/Il2CppHStringReference.h"
  15. #include "vm/String.h"
  16. #include "utils/ExceptionSupportStack.h"
  17. #include "utils/Output.h"
  18. #include <cstdlib>
  19. #include <cstddef>
  20. REAL_NORETURN IL2CPP_NO_INLINE void il2cpp_codegen_no_return();
  21. NORETURN void il2cpp_codegen_raise_exception(Exception_t *ex, MethodInfo* lastManagedFrame);
  22. NORETURN void il2cpp_codegen_rethrow_exception(Exception_t *ex);
  23. NORETURN void il2cpp_codegen_raise_exception(il2cpp_hresult_t hresult, bool defaultToCOMException);
  24. // Exception support macros
  25. #define IL2CPP_PUSH_ACTIVE_EXCEPTION(Exception) \
  26. __active_exceptions.push(Exception)
  27. #if HYBRIDCLR_UNITY_VERSION >= 20220311
  28. #define IL2CPP_POP_ACTIVE_EXCEPTION(ExcType) \
  29. (ExcType)__active_exceptions.pop()
  30. #else
  31. #define IL2CPP_POP_ACTIVE_EXCEPTION() \
  32. __active_exceptions.pop()
  33. #endif
  34. #define IL2CPP_GET_ACTIVE_EXCEPTION(ExcType) \
  35. (ExcType)__active_exceptions.top()
  36. #define IL2CPP_RAISE_NULL_REFERENCE_EXCEPTION() \
  37. do {\
  38. il2cpp_codegen_raise_null_reference_exception();\
  39. il2cpp_codegen_no_return();\
  40. } while (0)
  41. #define IL2CPP_RAISE_MANAGED_EXCEPTION(ex, lastManagedFrame) \
  42. do {\
  43. il2cpp_codegen_raise_exception((Exception_t*)ex, (RuntimeMethod*)lastManagedFrame);\
  44. il2cpp_codegen_no_return();\
  45. } while (0)
  46. #define IL2CPP_RETHROW_MANAGED_EXCEPTION(ex) \
  47. do {\
  48. il2cpp_codegen_rethrow_exception((Exception_t*)ex);\
  49. il2cpp_codegen_no_return();\
  50. } while (0)
  51. #if IL2CPP_ENABLE_PROFILER
  52. void il2cpp_codegen_profiler_method_enter(const RuntimeMethod* method);
  53. void il2cpp_codegen_profiler_method_exit(const RuntimeMethod* method);
  54. #endif
  55. struct ProfilerMethodSentry
  56. {
  57. ProfilerMethodSentry(const RuntimeMethod* method)
  58. #if IL2CPP_ENABLE_PROFILER
  59. : m_method(method)
  60. #endif
  61. {
  62. #if IL2CPP_ENABLE_PROFILER
  63. il2cpp_codegen_profiler_method_enter(m_method);
  64. #endif
  65. }
  66. ~ProfilerMethodSentry()
  67. {
  68. #if IL2CPP_ENABLE_PROFILER
  69. il2cpp_codegen_profiler_method_exit(m_method);
  70. #endif
  71. }
  72. private:
  73. const RuntimeMethod* m_method;
  74. };
  75. void il2cpp_codegen_stacktrace_push_frame(Il2CppStackFrameInfo& frame);
  76. void il2cpp_codegen_stacktrace_pop_frame();
  77. uint32_t il2cpp_codegen_sizeof(RuntimeClass* klass);
  78. struct StackTraceSentry
  79. {
  80. StackTraceSentry(const RuntimeMethod* method) : m_method(method)
  81. {
  82. Il2CppStackFrameInfo frame_info = { 0 };
  83. frame_info.method = method;
  84. il2cpp_codegen_stacktrace_push_frame(frame_info);
  85. }
  86. ~StackTraceSentry()
  87. {
  88. il2cpp_codegen_stacktrace_pop_frame();
  89. }
  90. private:
  91. const RuntimeMethod* m_method;
  92. };
  93. #define IL2CPP_FAKE_BOX_SENTRY (MonitorData*)UINTPTR_MAX
  94. template<typename T>
  95. struct Il2CppFakeBox : RuntimeObject
  96. {
  97. T m_Value;
  98. Il2CppFakeBox(RuntimeClass* boxedType, T* value)
  99. {
  100. klass = boxedType;
  101. monitor = IL2CPP_FAKE_BOX_SENTRY;
  102. m_Value = *value;
  103. }
  104. };
  105. struct Il2CppFakeBoxBuffer : RuntimeObject
  106. {
  107. char m_Value[];
  108. Il2CppFakeBoxBuffer(RuntimeClass* boxedType, void* value)
  109. {
  110. klass = boxedType;
  111. monitor = IL2CPP_FAKE_BOX_SENTRY;
  112. memcpy(&m_Value, value, il2cpp_codegen_sizeof(boxedType));
  113. }
  114. static uint32_t SizeNeededFor(RuntimeClass* boxedType)
  115. {
  116. return sizeof(Il2CppFakeBoxBuffer) + il2cpp_codegen_sizeof(boxedType);
  117. }
  118. };
  119. inline bool il2cpp_codegen_is_fake_boxed_object(RuntimeObject* object)
  120. {
  121. return object->monitor == IL2CPP_FAKE_BOX_SENTRY;
  122. }
  123. // TODO: This file should contain all the functions and type declarations needed for the generated code.
  124. // Hopefully, we stop including everything in the generated code and know exactly what dependencies we have.
  125. // Note that all parameter and return types should match the generated types not the runtime types.
  126. void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions);
  127. typedef void (*MetadataInitializerCleanupFunc)();
  128. void il2cpp_codegen_register_metadata_initialized_cleanup(MetadataInitializerCleanupFunc cleanup);
  129. // type registration
  130. void* il2cpp_codegen_get_thread_static_data(RuntimeClass* klass);
  131. String_t* il2cpp_codegen_string_new_wrapper(const char* str);
  132. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str);
  133. Type_t* il2cpp_codegen_type_get_object(const RuntimeType* type);
  134. void il2cpp_codegen_raise_execution_engine_exception_if_method_is_not_found(const RuntimeMethod* method);
  135. void il2cpp_codegen_raise_execution_engine_exception(const RuntimeMethod* method);
  136. void il2cpp_codegen_raise_execution_engine_exception_missing_virtual(const RuntimeMethod* method);
  137. NORETURN void il2cpp_codegen_raise_out_of_memory_exception();
  138. NORETURN void il2cpp_codegen_raise_null_reference_exception();
  139. NORETURN void il2cpp_codegen_raise_divide_by_zero_exception();
  140. NORETURN void il2cpp_codegen_raise_index_out_of_range_exception();
  141. Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg);
  142. Exception_t* il2cpp_codegen_get_argument_null_exception(const char* param);
  143. Exception_t* il2cpp_codegen_get_overflow_exception();
  144. Exception_t* il2cpp_codegen_get_not_supported_exception(const char* msg);
  145. Exception_t* il2cpp_codegen_get_array_type_mismatch_exception();
  146. Exception_t* il2cpp_codegen_get_invalid_cast_exception(const char* msg);
  147. Exception_t* il2cpp_codegen_get_invalid_operation_exception(const char* msg);
  148. Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg);
  149. Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg);
  150. Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception();
  151. Exception_t* il2cpp_codegen_get_engine_execution_exception(const char* msg);
  152. Exception_t* il2cpp_codegen_get_index_out_of_range_exception();
  153. Exception_t* il2cpp_codegen_get_exception(il2cpp_hresult_t hresult, bool defaultToCOMException);
  154. inline RuntimeClass* il2cpp_codegen_object_class(RuntimeObject* obj)
  155. {
  156. return obj->klass;
  157. }
  158. // OpCode.IsInst
  159. RuntimeObject* IsInst(RuntimeObject *obj, RuntimeClass* targetType);
  160. inline RuntimeObject* IsInstSealed(RuntimeObject *obj, RuntimeClass* targetType)
  161. {
  162. #if IL2CPP_DEBUG
  163. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_SEALED) != 0);
  164. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_INTERFACE) == 0);
  165. #endif
  166. if (!obj)
  167. return NULL;
  168. // optimized version to compare sealed classes
  169. return (obj->klass == targetType ? obj : NULL);
  170. }
  171. inline RuntimeObject* IsInstClass(RuntimeObject *obj, RuntimeClass* targetType)
  172. {
  173. #if IL2CPP_DEBUG
  174. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_INTERFACE) == 0);
  175. #endif
  176. if (!obj)
  177. return NULL;
  178. // optimized version to compare classes
  179. //return il2cpp::vm::ClassInlines::HasParentUnsafe(obj->klass, targetType) ? obj : NULL;
  180. //[WL]
  181. return il2cpp::vm::ClassInlines::HasParent(obj->klass, targetType) ? obj : NULL;
  182. }
  183. // OpCode.Castclass
  184. NORETURN void RaiseInvalidCastException(RuntimeObject* obj, RuntimeClass* targetType);
  185. inline RuntimeObject* Castclass(RuntimeObject *obj, RuntimeClass* targetType)
  186. {
  187. if (!obj)
  188. return NULL;
  189. RuntimeObject* result = IsInst(obj, targetType);
  190. if (result)
  191. return result;
  192. RaiseInvalidCastException(obj, targetType);
  193. return NULL;
  194. }
  195. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, RuntimeClass* targetType)
  196. {
  197. if (!obj)
  198. return NULL;
  199. RuntimeObject* result = IsInstSealed(obj, targetType);
  200. if (result)
  201. return result;
  202. RaiseInvalidCastException(obj, targetType);
  203. return NULL;
  204. }
  205. inline RuntimeObject* CastclassClass(RuntimeObject *obj, RuntimeClass* targetType)
  206. {
  207. if (!obj)
  208. return NULL;
  209. RuntimeObject* result = IsInstClass(obj, targetType);
  210. if (result)
  211. return result;
  212. RaiseInvalidCastException(obj, targetType);
  213. return NULL;
  214. }
  215. inline void NullCheck(void* this_ptr)
  216. {
  217. if (this_ptr != NULL)
  218. return;
  219. il2cpp_codegen_raise_null_reference_exception();
  220. }
  221. // OpCode.Box
  222. RuntimeObject* Box(RuntimeClass* type, void* data);
  223. // OpCode.UnBox
  224. void* Unbox_internal(Il2CppObject* obj);
  225. inline void* UnBox(RuntimeObject* obj)
  226. {
  227. NullCheck(obj);
  228. return Unbox_internal(obj);
  229. }
  230. inline void* UnBox(RuntimeObject* obj, RuntimeClass* expectedBoxedClass)
  231. {
  232. NullCheck(obj);
  233. if (obj->klass->element_class == expectedBoxedClass->element_class)
  234. return Unbox_internal(obj);
  235. RaiseInvalidCastException(obj, expectedBoxedClass);
  236. return NULL;
  237. }
  238. void UnBoxNullable_internal(RuntimeObject* obj, RuntimeClass* nullableClass, void* storage);
  239. inline void UnBoxNullable(RuntimeObject* obj, RuntimeClass* nullableClass, void* storage)
  240. {
  241. // We only need to do type checks if obj is not null
  242. // Unboxing null nullable is perfectly valid and returns an instance that has no value
  243. if (obj != NULL)
  244. {
  245. if (obj->klass != nullableClass->element_class)
  246. RaiseInvalidCastException(obj, nullableClass->element_class);
  247. }
  248. UnBoxNullable_internal(obj, nullableClass, storage);
  249. }
  250. // Implements OpCode.UnBox_Any at runtime, obj may be any type, even a reference type
  251. // unboxStorage must always be a buffer large enough to store the unboxed result
  252. // It always returns a pointer to a data buffer - either a pointer to a value type or a pointer to a reference type
  253. void* UnBox_Any(RuntimeObject* obj, RuntimeClass* expectedBoxedClass, void* unboxStorage);
  254. // objBuffer is a pointer to the obj - either a pointer to a value type or a pointer to a reference type
  255. bool il2cpp_codegen_would_box_to_non_null(RuntimeClass* klass, void* objBuffer);
  256. int32_t il2cpp_codgen_class_get_instance_size(RuntimeClass* klass);
  257. inline uint32_t il2cpp_codegen_sizeof(RuntimeClass* klass)
  258. {
  259. IL2CPP_ASSERT(klass->size_inited);
  260. return klass->stack_slot_size;
  261. }
  262. inline bool il2cpp_codegen_method_is_virtual(RuntimeMethod* method)
  263. {
  264. return method->slot != kInvalidIl2CppMethodSlot;
  265. }
  266. inline bool il2cpp_codegen_object_is_of_sealed_type(RuntimeObject* obj)
  267. {
  268. IL2CPP_ASSERT(obj);
  269. return (obj->klass->flags & TYPE_ATTRIBUTE_SEALED) != 0;
  270. }
  271. bool il2cpp_codegen_method_is_generic_instance_method(RuntimeMethod* method);
  272. RuntimeClass* il2cpp_codegen_method_get_declaring_type(const RuntimeMethod* method);
  273. bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method);
  274. inline uint16_t il2cpp_codegen_method_get_slot(const RuntimeMethod* method)
  275. {
  276. return method->slot;
  277. }
  278. IL2CPP_FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_virtual_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj)
  279. {
  280. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_virtual_invoke_data got called on a non-virtual method");
  281. //[WL]
  282. //return obj->klass->vtable[slot];
  283. if (obj->klass->is_vtable_initialized && obj->klass->vtable[slot].method != NULL) {
  284. return obj->klass->vtable[slot];
  285. }else
  286. return il2cpp::vm::ClassInlines::GetVirtualInvokeData(slot, obj);
  287. }
  288. IL2CPP_FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, RuntimeObject* obj, const RuntimeClass* declaringInterface)
  289. {
  290. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_interface_invoke_data got called on a non-virtual method");
  291. return il2cpp::vm::ClassInlines::GetInterfaceInvokeDataFromVTable(obj, declaringInterface, slot);
  292. }
  293. const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method_internal(const RuntimeMethod* methodDefinition, const RuntimeMethod* inflatedMethod);
  294. IL2CPP_FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method(const RuntimeMethod* method, const RuntimeObject* obj)
  295. {
  296. uint16_t slot = method->slot;
  297. //const RuntimeMethod* methodDefinition = obj->klass->vtable[slot].method;
  298. //[WL]
  299. const RuntimeMethod* methodDefinition = NULL;
  300. if (obj->klass->is_vtable_initialized && obj->klass->vtable[slot].method != NULL) {
  301. methodDefinition = obj->klass->vtable[slot].method;
  302. }
  303. else {
  304. const VirtualInvokeData& data = il2cpp::vm::ClassInlines::GetVirtualInvokeData(slot, obj);
  305. methodDefinition = data.method;
  306. }
  307. return il2cpp_codegen_get_generic_virtual_method_internal(methodDefinition, method);
  308. }
  309. IL2CPP_FORCE_INLINE void il2cpp_codegen_get_generic_virtual_invoke_data(const RuntimeMethod* method, const RuntimeObject* obj, VirtualInvokeData* invokeData)
  310. {
  311. invokeData->method = il2cpp_codegen_get_generic_virtual_method(method, obj);
  312. invokeData->methodPtr = invokeData->method->virtualMethodPointer;
  313. IL2CPP_ASSERT(invokeData->method);
  314. }
  315. IL2CPP_FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_interface_method(const RuntimeMethod* method, RuntimeObject* obj)
  316. {
  317. const RuntimeMethod* methodDefinition = il2cpp::vm::ClassInlines::GetInterfaceInvokeDataFromVTable(obj, method->klass, method->slot).method;
  318. return il2cpp_codegen_get_generic_virtual_method_internal(methodDefinition, method);
  319. }
  320. IL2CPP_FORCE_INLINE void il2cpp_codegen_get_generic_interface_invoke_data(const RuntimeMethod* method, RuntimeObject* obj, VirtualInvokeData* invokeData)
  321. {
  322. invokeData->method = il2cpp_codegen_get_generic_interface_method(method, obj);
  323. invokeData->methodPtr = invokeData->method->virtualMethodPointer;
  324. IL2CPP_ASSERT(invokeData->method);
  325. }
  326. inline RuntimeClass* InitializedTypeInfo(RuntimeClass* klass)
  327. {
  328. return il2cpp::vm::ClassInlines::InitFromCodegen(klass);
  329. }
  330. RuntimeClass* il2cpp_codegen_class_from_type_internal(const RuntimeType* type);
  331. inline RuntimeClass* il2cpp_codegen_class_from_type(const RuntimeType *type)
  332. {
  333. return InitializedTypeInfo(il2cpp_codegen_class_from_type_internal(type));
  334. }
  335. inline const RuntimeType* il2cpp_codegen_type_from_class(RuntimeClass *klass)
  336. {
  337. return &klass->byval_arg;
  338. }
  339. inline void* InterlockedExchangeImplRef(void** location, void* value)
  340. {
  341. return il2cpp::icalls::mscorlib::System::Threading::Interlocked::ExchangePointer(location, value);
  342. }
  343. template<typename T>
  344. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  345. {
  346. return (T)il2cpp::icalls::mscorlib::System::Threading::Interlocked::CompareExchange_T((void**)location, value, comparand);
  347. }
  348. template<typename T>
  349. inline T InterlockedExchangeImpl(T* location, T value)
  350. {
  351. return (T)InterlockedExchangeImplRef((void**)location, value);
  352. }
  353. void il2cpp_codegen_memory_barrier();
  354. inline void GetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, void* value)
  355. {
  356. // GetGenericValueImpl is only called from the class libs internally and T is never a field
  357. IL2CPP_ASSERT_STACK_PTR(value);
  358. memcpy(value, il2cpp_array_addr_with_size(thisPtr, thisPtr->klass->element_size, pos), thisPtr->klass->element_size);
  359. }
  360. void SetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, void* value);
  361. RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length);
  362. RuntimeArray* GenArrayNew(RuntimeClass* arrayType, il2cpp_array_size_t* dimensions);
  363. // Performance optimization as detailed here: http://blogs.msdn.com/b/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx
  364. // Since array size is a signed int32_t, a single unsigned check can be performed to determine if index is less than array size.
  365. // Negative indices will map to a unsigned number greater than or equal to 2^31 which is larger than allowed for a valid array.
  366. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  367. do { \
  368. if (((uint32_t)(index)) >= ((uint32_t)length)) il2cpp_codegen_raise_index_out_of_range_exception(); \
  369. } while (0)
  370. bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass);
  371. RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass);
  372. Il2CppMethodPointer il2cpp_codegen_resolve_icall(const char* name);
  373. Il2CppMethodPointer il2cpp_codegen_resolve(const PInvokeArguments& pinvokeArgs);
  374. template<typename FunctionPointerType, size_t dynamicLibraryLength, size_t entryPointLength>
  375. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const Il2CppNativeChar(&nativeDynamicLibrary)[dynamicLibraryLength], const char(&entryPoint)[entryPointLength],
  376. Il2CppCallConvention callingConvention, Il2CppCharSet charSet, int parameterSize, bool isNoMangle)
  377. {
  378. const PInvokeArguments pinvokeArgs =
  379. {
  380. il2cpp::utils::StringView<Il2CppNativeChar>(nativeDynamicLibrary),
  381. il2cpp::utils::StringView<char>(entryPoint),
  382. callingConvention,
  383. charSet,
  384. parameterSize,
  385. isNoMangle
  386. };
  387. return reinterpret_cast<FunctionPointerType>(il2cpp_codegen_resolve(pinvokeArgs));
  388. }
  389. void* il2cpp_codegen_marshal_allocate(size_t size);
  390. template<typename T>
  391. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  392. {
  393. return static_cast<T*>(il2cpp_codegen_marshal_allocate((il2cpp_array_size_t)(sizeof(T) * length)));
  394. }
  395. template<typename T>
  396. inline T* il2cpp_codegen_marshal_allocate()
  397. {
  398. return static_cast<T*>(il2cpp_codegen_marshal_allocate(sizeof(T)));
  399. }
  400. char* il2cpp_codegen_marshal_string(String_t* string);
  401. void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters);
  402. Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string);
  403. void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters);
  404. Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string);
  405. String_t* il2cpp_codegen_marshal_string_result(const char* value);
  406. String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value);
  407. String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value);
  408. void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value);
  409. char* il2cpp_codegen_marshal_empty_string_builder(StringBuilder_t* stringBuilder);
  410. char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder);
  411. Il2CppChar* il2cpp_codegen_marshal_empty_wstring_builder(StringBuilder_t* stringBuilder);
  412. Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder);
  413. void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer);
  414. void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer);
  415. Il2CppHString il2cpp_codegen_create_hstring(String_t* str);
  416. String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring);
  417. void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring);
  418. void il2cpp_codegen_marshal_type_to_native(Type_t* type, Il2CppWindowsRuntimeTypeName& nativeType);
  419. const Il2CppType* il2cpp_codegen_marshal_type_from_native_internal(Il2CppWindowsRuntimeTypeName& nativeType);
  420. inline Type_t* il2cpp_codegen_marshal_type_from_native(Il2CppWindowsRuntimeTypeName& nativeType)
  421. {
  422. const Il2CppType* type = il2cpp_codegen_marshal_type_from_native_internal(nativeType);
  423. if (type == NULL)
  424. return NULL;
  425. return il2cpp_codegen_type_get_object(type);
  426. }
  427. void il2cpp_codegen_delete_native_type(Il2CppWindowsRuntimeTypeName& nativeType);
  428. void il2cpp_codegen_marshal_free(void* ptr);
  429. Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d);
  430. Il2CppDelegate* il2cpp_codegen_marshal_function_ptr_to_delegate_internal(void* functionPtr, Il2CppClass* delegateType);
  431. bool il2cpp_codegen_is_marshalled_delegate(MulticastDelegate_t* d);
  432. #if !IL2CPP_TINY
  433. inline void* il2cpp_codegen_get_reverse_pinvoke_function_ptr(const MulticastDelegate_t* d)
  434. {
  435. return ((Il2CppDelegate*)d)->delegate_trampoline;
  436. }
  437. #endif
  438. template<typename T>
  439. inline T* il2cpp_codegen_marshal_function_ptr_to_delegate(Il2CppMethodPointer functionPtr, RuntimeClass* delegateType)
  440. {
  441. return (T*)il2cpp_codegen_marshal_function_ptr_to_delegate_internal(reinterpret_cast<void*>(functionPtr), delegateType);
  442. }
  443. void il2cpp_codegen_marshal_store_last_error();
  444. template<typename R, typename S>
  445. inline R il2cpp_codegen_cast_struct(S* s)
  446. {
  447. static_assert(sizeof(S) == sizeof(R), "Types with different sizes passed to il2cpp_codegen_cast_struct");
  448. R r;
  449. il2cpp_codegen_memcpy(&r, s, sizeof(R));
  450. return r;
  451. }
  452. #if _DEBUG
  453. void il2cpp_codegen_marshal_allocate_push_allocation_frame();
  454. void il2cpp_codegen_marshal_allocate_pop_allocation_frame();
  455. bool il2cpp_codegen_marshal_allocate_has_unfreed_allocations();
  456. void il2cpp_codegen_marshal_allocate_clear_all_tracked_allocations();
  457. struct ScopedMarshallingAllocationFrame
  458. {
  459. ScopedMarshallingAllocationFrame()
  460. {
  461. il2cpp_codegen_marshal_allocate_push_allocation_frame();
  462. }
  463. ~ScopedMarshallingAllocationFrame()
  464. {
  465. il2cpp_codegen_marshal_allocate_pop_allocation_frame();
  466. }
  467. };
  468. struct ScopedMarshallingAllocationCheck
  469. {
  470. ~ScopedMarshallingAllocationCheck()
  471. {
  472. if (il2cpp_codegen_marshal_allocate_has_unfreed_allocations())
  473. il2cpp_codegen_raise_exception(il2cpp_codegen_get_invalid_operation_exception("Error in marshaling allocation. Some memory has been leaked."));
  474. }
  475. private:
  476. ScopedMarshallingAllocationFrame m_AllocationFrame;
  477. };
  478. struct ScopedMarshalingAllocationClearer
  479. {
  480. ~ScopedMarshalingAllocationClearer()
  481. {
  482. il2cpp_codegen_marshal_allocate_clear_all_tracked_allocations();
  483. }
  484. private:
  485. ScopedMarshallingAllocationFrame m_AllocationFrame;
  486. };
  487. #endif
  488. inline void DivideByZeroCheck(int64_t denominator)
  489. {
  490. if (denominator != 0)
  491. return;
  492. il2cpp_codegen_raise_divide_by_zero_exception();
  493. }
  494. bool MethodIsStatic(const RuntimeMethod* method);
  495. bool MethodHasParameters(const RuntimeMethod* method);
  496. void il2cpp_codegen_runtime_class_init(RuntimeClass* klass);
  497. inline void il2cpp_codegen_runtime_class_init_inline(RuntimeClass* klass)
  498. {
  499. if (!klass->cctor_finished_or_no_cctor)
  500. il2cpp_codegen_runtime_class_init(klass);
  501. }
  502. RuntimeClass* il2cpp_rgctx_data_no_init(const Il2CppRGCTXData* rgctxVar, int32_t index);
  503. // generic sharing
  504. inline RuntimeClass* il2cpp_rgctx_data(const Il2CppRGCTXData* rgctxVar, int32_t index) {
  505. return InitializedTypeInfo(il2cpp_rgctx_data_no_init(rgctxVar,index));
  506. }
  507. const Il2CppType* il2cpp_rgctx_type(const Il2CppRGCTXData* rgctxVar, int32_t index);
  508. const MethodInfo* il2cpp_rgctx_method(const Il2CppRGCTXData* rgctxVar, int32_t index);
  509. inline FieldInfo* il2cpp_rgctx_field(RuntimeClass* klass, int32_t index)
  510. {
  511. return klass->fields + index;
  512. }
  513. inline bool il2cpp_rgctx_is_initialized(const RuntimeMethod* method)
  514. {
  515. IL2CPP_ASSERT(method->is_inflated);
  516. return method->rgctx_data != NULL;
  517. }
  518. inline void il2cpp_rgctx_method_init(const RuntimeMethod* method)
  519. {
  520. il2cpp::vm::ClassInlines::InitRgcxFromCodegen(method);
  521. }
  522. inline uintptr_t il2cpp_array_calc_byte_offset(RuntimeArray* runtimeArray, il2cpp_array_size_t index)
  523. {
  524. return runtimeArray->klass->element_size * index;
  525. }
  526. inline void ArrayElementTypeCheck(RuntimeArray* array, void* value)
  527. {
  528. #if !IL2CPP_TINY
  529. if (value != NULL && IsInst((RuntimeObject*)value, array->klass->element_class) == NULL)
  530. il2cpp_codegen_raise_exception(il2cpp_codegen_get_array_type_mismatch_exception());
  531. #endif
  532. }
  533. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot)
  534. {
  535. if (!pThis)
  536. il2cpp_codegen_raise_null_reference_exception();
  537. //[WL]
  538. if (pThis->klass->is_vtable_initialized && pThis->klass->vtable[slot].method != NULL) {
  539. return pThis->klass->vtable[slot].method;
  540. }
  541. else {
  542. const VirtualInvokeData& data = il2cpp::vm::ClassInlines::GetVirtualInvokeData(slot, pThis);
  543. return data.method;
  544. }
  545. }
  546. inline const RuntimeMethod* GetInterfaceMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot, RuntimeClass* declaringInterface)
  547. {
  548. if (!pThis)
  549. il2cpp_codegen_raise_null_reference_exception();
  550. return il2cpp::vm::ClassInlines::GetInterfaceInvokeDataFromVTable(pThis, declaringInterface, slot).method;
  551. }
  552. void il2cpp_codegen_initialize_runtime_metadata(uintptr_t* metadataPointer);
  553. void* il2cpp_codegen_initialize_runtime_metadata_inline(uintptr_t* metadataPointer);
  554. inline bool il2cpp_codegen_type_is_value_type(const RuntimeType* type)
  555. {
  556. return type->valuetype;
  557. }
  558. inline bool il2cpp_codegen_class_is_value_type(RuntimeClass* type)
  559. {
  560. return il2cpp_codegen_type_is_value_type(&type->byval_arg);
  561. }
  562. bool il2cpp_codegen_class_is_nullable(RuntimeClass* type);
  563. inline bool il2cpp_codegen_type_implements_virtual_method(RuntimeClass* type, const RuntimeMethod* method)
  564. {
  565. IL2CPP_ASSERT(il2cpp_codegen_class_is_value_type(type));
  566. return method->klass == type;
  567. }
  568. MethodBase_t* il2cpp_codegen_get_method_object_internal(const RuntimeMethod* method, RuntimeClass* refclass);
  569. const RuntimeClass* il2cpp_codegen_get_generic_type_definition(const RuntimeClass* klass);
  570. const RuntimeMethod* il2cpp_codegen_get_generic_method_definition(const RuntimeMethod* method);
  571. const RuntimeMethod* il2cpp_codegen_get_generic_instance_method_from_method_definition(RuntimeClass* genericInstanceClass, const RuntimeMethod* methodDefinition);
  572. inline MethodBase_t* il2cpp_codegen_get_method_object(const RuntimeMethod* method)
  573. {
  574. if (method->is_inflated)
  575. method = il2cpp_codegen_get_generic_method_definition(method);
  576. return il2cpp_codegen_get_method_object_internal(method, method->klass);
  577. }
  578. Type_t* il2cpp_codegen_get_type(String_t* typeName, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod);
  579. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod);
  580. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, bool ignoreCase, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod);
  581. Assembly_t* il2cpp_codegen_get_executing_assembly(const RuntimeMethod* method);
  582. // Atomic
  583. void* il2cpp_codegen_atomic_compare_exchange_pointer(void** dest, void* exchange, void* comparand);
  584. // COM
  585. void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant);
  586. RuntimeObject* il2cpp_codegen_com_marshal_variant_result(const Il2CppVariant* variant);
  587. void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant);
  588. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray);
  589. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray);
  590. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray);
  591. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray);
  592. void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray);
  593. void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity);
  594. void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw);
  595. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass);
  596. template<typename T>
  597. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iunknown(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  598. {
  599. #if IL2CPP_TRIM_COM
  600. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_rcw_from_iunknown);
  601. return NULL;
  602. #else
  603. return static_cast<T*>(il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(unknown, fallbackClass));
  604. #endif
  605. }
  606. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass);
  607. template<typename T>
  608. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  609. {
  610. #if IL2CPP_TRIM_COM
  611. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_rcw_from_iinspectable);
  612. return NULL;
  613. #else
  614. return static_cast<T*>(il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(unknown, fallbackClass));
  615. #endif
  616. }
  617. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(Il2CppIUnknown* unknown, RuntimeClass* objectClass);
  618. template<typename T>
  619. inline T* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  620. {
  621. #if IL2CPP_TRIM_COM
  622. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_rcw_for_sealed_class);
  623. return NULL;
  624. #else
  625. return static_cast<T*>(il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(unknown, objectClass));
  626. #endif
  627. }
  628. Il2CppIUnknown* il2cpp_codegen_com_query_interface_internal(Il2CppComObject* rcw, const Il2CppGuid& guid);
  629. Il2CppIUnknown* il2cpp_codegen_com_query_interface_no_throw_internal(Il2CppComObject* rcw, const Il2CppGuid& guid);
  630. void il2cpp_codegen_com_cache_queried_interface(Il2CppComObject* rcw, const Il2CppGuid& guid, Il2CppIUnknown* queriedInterface);
  631. template<typename T>
  632. inline T* il2cpp_codegen_com_query_interface(Il2CppComObject* rcw)
  633. {
  634. #if IL2CPP_TRIM_COM
  635. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_query_interface);
  636. return NULL;
  637. #else
  638. return static_cast<T*>(il2cpp_codegen_com_query_interface_internal(rcw, T::IID));
  639. #endif
  640. }
  641. template<typename T>
  642. inline T* il2cpp_codegen_com_query_interface_no_throw(Il2CppComObject* rcw)
  643. {
  644. #if IL2CPP_TRIM_COM
  645. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_query_interface_no_throw);
  646. return NULL;
  647. #else
  648. return static_cast<T*>(il2cpp_codegen_com_query_interface_no_throw_internal(rcw, T::IID));
  649. #endif
  650. }
  651. void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw);
  652. Il2CppIUnknown* il2cpp_codegen_com_get_or_create_ccw_internal(RuntimeObject* obj, const Il2CppGuid& iid);
  653. template<typename InterfaceType>
  654. inline InterfaceType* il2cpp_codegen_com_get_or_create_ccw(RuntimeObject* obj)
  655. {
  656. #if IL2CPP_TRIM_COM
  657. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_ccw);
  658. return NULL;
  659. #else
  660. return static_cast<InterfaceType*>(il2cpp_codegen_com_get_or_create_ccw_internal(obj, InterfaceType::IID));
  661. #endif
  662. }
  663. inline intptr_t il2cpp_codegen_com_get_iunknown_for_object(RuntimeObject* obj)
  664. {
  665. #if IL2CPP_TRIM_COM
  666. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_iunknown_for_object);
  667. return NULL;
  668. #else
  669. return reinterpret_cast<intptr_t>(il2cpp_codegen_com_get_or_create_ccw_internal(obj, Il2CppIUnknown::IID));
  670. #endif
  671. }
  672. Il2CppObject* il2cpp_codegen_com_unpack_ccw(Il2CppIUnknown* obj);
  673. inline void il2cpp_codegen_com_raise_exception_if_failed(il2cpp_hresult_t hr, bool defaultToCOMException)
  674. {
  675. #if IL2CPP_TRIM_COM
  676. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_raise_exception_if_failed);
  677. #else
  678. // Copied from il2cpp::vm::Exception::RaiseIfFailed to keep inlined
  679. if (IL2CPP_HR_FAILED(hr))
  680. il2cpp_codegen_raise_exception(hr, defaultToCOMException);
  681. #endif
  682. }
  683. inline RuntimeException* il2cpp_codegen_com_get_exception(il2cpp_hresult_t hr, bool defaultToCOMException)
  684. {
  685. #if IL2CPP_TRIM_COM
  686. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_exception);
  687. #else
  688. return (RuntimeException*)il2cpp_codegen_get_exception(hr, defaultToCOMException);
  689. #endif
  690. }
  691. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(const char* fromType, const char* toType);
  692. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(RuntimeObject* value, const char* fromType, const char* toType);
  693. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index);
  694. 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);
  695. void il2cpp_codegen_store_exception_info(RuntimeException* ex, String_t* exceptionString);
  696. Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName);
  697. // delegate
  698. Il2CppAsyncResult* il2cpp_codegen_delegate_begin_invoke(RuntimeDelegate* delegate, void** params, RuntimeDelegate* asyncCallback, RuntimeObject* state);
  699. RuntimeObject* il2cpp_codegen_delegate_end_invoke(Il2CppAsyncResult* asyncResult, void **out_args);
  700. void il2cpp_codegen_set_closed_delegate_invoke(RuntimeObject* delegate, RuntimeObject* target, void* methodPtr);
  701. inline const Il2CppGenericInst* il2cpp_codegen_get_generic_class_inst(RuntimeClass* genericClass)
  702. {
  703. IL2CPP_ASSERT(genericClass->generic_class);
  704. return genericClass->generic_class->context.class_inst;
  705. }
  706. RuntimeClass* il2cpp_codegen_get_generic_argument(RuntimeClass* klass, uint32_t argNum);
  707. // Inflate a generic class from an existing generic instance
  708. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst);
  709. // Inflate a generic class from types
  710. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const RuntimeType*, /*const RuntimeType*, const RuntimeType* */ ...);
  711. inline void* il2cpp_codegen_static_fields_for(RuntimeClass* klass)
  712. {
  713. return klass->static_fields;
  714. }
  715. void il2cpp_codegen_assert_field_size(RuntimeField* field, size_t size);
  716. void* il2cpp_codegen_get_instance_field_data_pointer(void* instance, RuntimeField* field);
  717. inline void* il2cpp_codegen_get_instance_field_data_pointer(intptr_t instance, RuntimeField* field)
  718. {
  719. return il2cpp_codegen_get_instance_field_data_pointer((void*)instance, field);
  720. }
  721. inline void* il2cpp_codegen_get_instance_field_data_pointer(uintptr_t instance, RuntimeField* field)
  722. {
  723. return il2cpp_codegen_get_instance_field_data_pointer((void*)instance, field);
  724. }
  725. void il2cpp_codegen_write_instance_field_data(void* instance, RuntimeField* field, void* data, uint32_t size);
  726. inline void il2cpp_codegen_write_instance_field_data(intptr_t instance, RuntimeField* field, void* data, uint32_t size)
  727. {
  728. il2cpp_codegen_write_instance_field_data((void*)instance, field, data, size);
  729. }
  730. inline void il2cpp_codegen_write_instance_field_data(uintptr_t instance, RuntimeField* field, void* data, uint32_t size)
  731. {
  732. il2cpp_codegen_write_instance_field_data((void*)instance, field, data, size);
  733. }
  734. void* il2cpp_codegen_get_static_field_data_pointer(RuntimeField* field);
  735. void il2cpp_codegen_write_static_field_data(RuntimeField* field, void* data, uint32_t size);
  736. void* il2cpp_codegen_get_thread_static_field_data_pointer(RuntimeField* field);
  737. void il2cpp_codegen_write_thread_static_field_data(RuntimeField* field, void* data, uint32_t size);
  738. template<typename T>
  739. void il2cpp_codegen_write_instance_field_data(void* instance, RuntimeField* field, no_infer<T> data)
  740. {
  741. il2cpp_codegen_assert_field_size(field, sizeof(T));
  742. void* fieldPointer = il2cpp_codegen_get_instance_field_data_pointer(instance, field);
  743. *(T*)fieldPointer = data;
  744. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  745. }
  746. template<typename T>
  747. inline void il2cpp_codegen_write_instance_field_data(intptr_t instance, RuntimeField* field, no_infer<T> data)
  748. {
  749. il2cpp_codegen_write_instance_field_data<T>((void*)instance, field, data);
  750. }
  751. template<typename T>
  752. inline void il2cpp_codegen_write_instance_field_data(uintptr_t instance, RuntimeField* field, no_infer<T> data)
  753. {
  754. il2cpp_codegen_write_instance_field_data<T>((void*)instance, field, data);
  755. }
  756. template<typename T>
  757. void il2cpp_codegen_write_static_field_data(RuntimeField* field, no_infer<T> data)
  758. {
  759. il2cpp_codegen_assert_field_size(field, sizeof(T));
  760. void* fieldPointer = il2cpp_codegen_get_static_field_data_pointer(field);
  761. *(T*)fieldPointer = data;
  762. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  763. }
  764. template<typename T>
  765. void il2cpp_codegen_write_thread_static_field_data(RuntimeField* field, no_infer<T> data)
  766. {
  767. il2cpp_codegen_assert_field_size(field, sizeof(T));
  768. void* fieldPointer = il2cpp_codegen_get_thread_static_field_data_pointer(field);
  769. *(T*)fieldPointer = data;
  770. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  771. }
  772. inline Il2CppMethodPointer il2cpp_codegen_get_method_pointer(const RuntimeMethod* method)
  773. {
  774. return method->virtualMethodPointer;
  775. }
  776. inline Il2CppMethodPointer il2cpp_codegen_get_direct_method_pointer(const RuntimeMethod* method)
  777. {
  778. return method->methodPointer;
  779. }
  780. inline const RuntimeType* il2cpp_codegen_method_return_type(const RuntimeMethod* method)
  781. {
  782. return method->return_type;
  783. }
  784. inline int il2cpp_codegen_method_parameter_count(const RuntimeMethod* method)
  785. {
  786. return method->parameters_count;
  787. }
  788. inline const RuntimeType* il2cpp_codegen_method_parameter_type(const RuntimeMethod* method, int parameterIndex)
  789. {
  790. IL2CPP_ASSERT(parameterIndex >= 0 && parameterIndex < method->parameters_count);
  791. return method->parameters[parameterIndex];
  792. }
  793. inline bool il2cpp_codegen_is_import_or_windows_runtime(const RuntimeObject *object)
  794. {
  795. #if IL2CPP_TRIM_COM
  796. return false;
  797. #else
  798. return object->klass->is_import_or_windows_runtime;
  799. #endif
  800. }
  801. inline intptr_t il2cpp_codegen_get_com_interface_for_object(Il2CppObject* object, Type_t* type)
  802. {
  803. #if IL2CPP_TRIM_COM
  804. return NULL;
  805. #else
  806. return il2cpp::icalls::mscorlib::System::Runtime::InteropServices::Marshal::GetCCW(object, reinterpret_cast<Il2CppReflectionType*>(type));
  807. #endif
  808. }
  809. NORETURN void il2cpp_codegen_raise_profile_exception(const RuntimeMethod* method);
  810. void il2cpp_codegen_array_unsafe_mov(RuntimeClass * destClass, void* dest, RuntimeClass * srcClass, void* src);
  811. NORETURN void il2cpp_codegen_array_unsafe_mov_type_exception(const RuntimeType * destType, const RuntimeType* srcType);
  812. template<typename T>
  813. T il2cpp_codegen_read_to_aligned_value(void* src)
  814. {
  815. T value;
  816. memcpy(&value, src, sizeof(T));
  817. return value;
  818. }
  819. template<typename TDest>
  820. inline void il2cpp_codegen_array_unsafe_mov_primitive(const RuntimeType * destType, TDest* dest, const RuntimeType* srcType, void* src)
  821. {
  822. switch (srcType->type)
  823. {
  824. case IL2CPP_TYPE_BOOLEAN:
  825. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<bool>(src);
  826. break;
  827. case IL2CPP_TYPE_I1:
  828. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<int8_t>(src);
  829. break;
  830. case IL2CPP_TYPE_U1:
  831. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<uint8_t>(src);
  832. break;
  833. case IL2CPP_TYPE_I2:
  834. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<int16_t>(src);
  835. break;
  836. case IL2CPP_TYPE_CHAR:
  837. case IL2CPP_TYPE_U2:
  838. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<uint16_t>(src);
  839. break;
  840. case IL2CPP_TYPE_I4:
  841. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<int32_t>(src);
  842. break;
  843. case IL2CPP_TYPE_U4:
  844. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<uint32_t>(src);
  845. break;
  846. case IL2CPP_TYPE_I8:
  847. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<int64_t>(src);
  848. break;
  849. case IL2CPP_TYPE_U8:
  850. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<uint64_t>(src);
  851. break;
  852. case IL2CPP_TYPE_I:
  853. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<intptr_t>(src);
  854. break;
  855. case IL2CPP_TYPE_U:
  856. *dest = (TDest)il2cpp_codegen_read_to_aligned_value<uintptr_t>(src);
  857. break;
  858. default:
  859. il2cpp_codegen_array_unsafe_mov_type_exception(destType, srcType);
  860. }
  861. }
  862. template<typename TDest, bool checkOverflow, bool inputUnsigned>
  863. TDest il2cpp_codegen_conv(const RuntimeClass* srcType, void* src, const RuntimeMethod* method)
  864. {
  865. switch (srcType->castClass->byval_arg.type)
  866. {
  867. case IL2CPP_TYPE_BOOLEAN:
  868. case IL2CPP_TYPE_I1:
  869. return il2cpp_codegen_conv<TDest, int8_t, int32_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<int8_t>(src), method);
  870. case IL2CPP_TYPE_U1:
  871. return il2cpp_codegen_conv<TDest, uint8_t, int32_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<uint8_t>(src), method);
  872. case IL2CPP_TYPE_I2:
  873. return il2cpp_codegen_conv<TDest, int16_t, int32_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<int16_t>(src), method);
  874. case IL2CPP_TYPE_CHAR:
  875. case IL2CPP_TYPE_U2:
  876. return il2cpp_codegen_conv<TDest, uint16_t, int32_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<uint16_t>(src), method);
  877. case IL2CPP_TYPE_I4:
  878. return il2cpp_codegen_conv<TDest, int32_t, int32_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<int32_t>(src), method);
  879. case IL2CPP_TYPE_U4:
  880. return il2cpp_codegen_conv<TDest, uint32_t, int32_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<uint32_t>(src), method);
  881. case IL2CPP_TYPE_I8:
  882. return il2cpp_codegen_conv<TDest, int64_t, int64_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<int64_t>(src), method);
  883. case IL2CPP_TYPE_U8:
  884. return il2cpp_codegen_conv<TDest, uint64_t, int64_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<uint64_t>(src), method);
  885. case IL2CPP_TYPE_I:
  886. case IL2CPP_TYPE_PTR:
  887. case IL2CPP_TYPE_OBJECT:
  888. return il2cpp_codegen_conv<TDest, intptr_t, intptr_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<intptr_t>(src), method);
  889. case IL2CPP_TYPE_U:
  890. return il2cpp_codegen_conv<TDest, uintptr_t, intptr_t, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<uintptr_t>(src), method);
  891. case IL2CPP_TYPE_R4:
  892. return il2cpp_codegen_conv<TDest, float, float, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<float>(src), method);
  893. case IL2CPP_TYPE_R8:
  894. return il2cpp_codegen_conv<TDest, double, double, checkOverflow, inputUnsigned>(il2cpp_codegen_read_to_aligned_value<double>(src), method);
  895. default:
  896. IL2CPP_RAISE_MANAGED_EXCEPTION(il2cpp_codegen_get_invalid_operation_exception("Unsupported conv opcode"), method);
  897. }
  898. return 0;
  899. }
  900. // objBuffer is a pointer to the obj, either a pointer to a struct's data or a pointer to a reference type pointer
  901. void il2cpp_codegen_runtime_constrained_call(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* boxBuffer, void* objBuffer, void** args, void* retVal);
  902. void* il2cpp_codegen_runtime_box_constrained_this(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* obj);
  903. template<typename T>
  904. inline void* il2cpp_codegen_unsafe_cast(T* ptr)
  905. {
  906. return reinterpret_cast<void*>(ptr);
  907. }
  908. inline void il2cpp_codegen_by_reference_constructor(Il2CppByReference* byReference, void* value)
  909. {
  910. IL2CPP_ASSERT_STACK_PTR(byReference);
  911. byReference->value = (intptr_t)value;
  912. }
  913. inline intptr_t il2cpp_codegen_by_reference_get_value(Il2CppByReference* byReference)
  914. {
  915. return byReference->value;
  916. }
  917. #define IL2CPP_BY_REFERENCE_GET_VALUE(TReturnType, byReference) (TReturnType*)il2cpp_codegen_by_reference_get_value(byReference)
  918. bool il2cpp_codegen_is_reference_or_contains_references(const RuntimeMethod* method);
  919. bool il2cpp_codegen_is_unmanaged(const RuntimeMethod* method);
  920. #if IL2CPP_TINY
  921. // Add intrinsics used by Tiny.
  922. #include "utils/MemoryUtils.h"
  923. Type_t* il2cpp_codegen_get_type(Il2CppObject* obj);
  924. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* genArray, int32_t dimension)
  925. {
  926. if (genArray->bounds == NULL)
  927. return il2cpp_codegen_get_array_length(genArray);
  928. return static_cast<int32_t>(genArray->bounds[dimension].length);
  929. }
  930. MulticastDelegate_t* il2cpp_codegen_create_combined_delegate(Type_t* type, Il2CppArray* delegates, int delegateCount);
  931. inline String_t* il2cpp_codegen_marshal_ptr_to_string_ansi(intptr_t ptr)
  932. {
  933. return il2cpp_codegen_marshal_string_result(reinterpret_cast<const char*>(ptr));
  934. }
  935. inline intptr_t il2cpp_codegen_marshal_string_to_co_task_mem_ansi(String_t* ptr)
  936. {
  937. return reinterpret_cast<intptr_t>(il2cpp_codegen_marshal_string(ptr));
  938. }
  939. inline void il2cpp_codegen_marshal_string_free_co_task_mem(intptr_t ptr)
  940. {
  941. il2cpp_codegen_marshal_free(reinterpret_cast<void*>(ptr));
  942. }
  943. struct Delegate_t;
  944. inline String_t* il2cpp_codegen_string_new_length(int length)
  945. {
  946. return reinterpret_cast<String_t*>(il2cpp::vm::String::NewSize(length));
  947. }
  948. Type_t* il2cpp_codegen_get_base_type(const Type_t* t);
  949. bool il2cpp_codegen_is_assignable_from(Type_t* left, Type_t* right);
  950. template<typename T>
  951. struct Il2CppReversePInvokeMethodHolder
  952. {
  953. Il2CppReversePInvokeMethodHolder(T** storageAddress) :
  954. m_LastValue(*storageAddress),
  955. m_StorageAddress(storageAddress)
  956. {
  957. }
  958. ~Il2CppReversePInvokeMethodHolder()
  959. {
  960. *m_StorageAddress = m_LastValue;
  961. }
  962. private:
  963. T* const m_LastValue;
  964. T** const m_StorageAddress;
  965. };
  966. void il2cpp_codegen_no_reverse_pinvoke_wrapper(const char* methodName, const char* reason);
  967. bool il2cpp_codegen_type_is_interface(Type_t* t);
  968. bool il2cpp_codegen_type_is_abstract(Type_t* t);
  969. bool il2cpp_codegen_type_is_pointer(Type_t* t);
  970. NORETURN void il2cpp_codegen_raise_exception(const char* message);
  971. #endif
  972. template<typename T>
  973. inline T* il2cpp_span_get_item(T* refPtrValue, int32_t index, int32_t length)
  974. {
  975. IL2CPP_ARRAY_BOUNDS_CHECK(index, length);
  976. return &refPtrValue[index];
  977. }
  978. template<typename T>
  979. inline T* il2cpp_unsafe_unbox(RuntimeObject* obj, RuntimeClass* klass)
  980. {
  981. return reinterpret_cast<T*>(UnBox(obj, klass));
  982. }
  983. #if IL2CPP_COMPILER_MSVC
  984. #define DEFAULT_CALL STDCALL
  985. #else
  986. #define DEFAULT_CALL
  987. #endif
  988. #if defined(__ARMCC_VERSION)
  989. inline double bankers_round(double x)
  990. {
  991. return __builtin_round(x);
  992. }
  993. inline float bankers_roundf(float x)
  994. {
  995. return __builtin_roundf(x);
  996. }
  997. #else
  998. inline double bankers_round(double x)
  999. {
  1000. double integerPart;
  1001. if (x >= 0.0)
  1002. {
  1003. if (modf(x, &integerPart) == 0.5)
  1004. return (int64_t)integerPart % 2 == 0 ? integerPart : integerPart + 1.0;
  1005. return floor(x + 0.5);
  1006. }
  1007. else
  1008. {
  1009. if (modf(x, &integerPart) == -0.5)
  1010. return (int64_t)integerPart % 2 == 0 ? integerPart : integerPart - 1.0;
  1011. return ceil(x - 0.5);
  1012. }
  1013. }
  1014. inline float bankers_roundf(float x)
  1015. {
  1016. double integerPart;
  1017. if (x >= 0.0f)
  1018. {
  1019. if (modf(x, &integerPart) == 0.5)
  1020. return (int64_t)integerPart % 2 == 0 ? (float)integerPart : (float)integerPart + 1.0f;
  1021. return floorf(x + 0.5f);
  1022. }
  1023. else
  1024. {
  1025. if (modf(x, &integerPart) == -0.5)
  1026. return (int64_t)integerPart % 2 == 0 ? (float)integerPart : (float)integerPart - 1.0f;
  1027. return ceilf(x - 0.5f);
  1028. }
  1029. }
  1030. #endif
  1031. // returns true if overflow occurs
  1032. inline bool il2cpp_codegen_check_mul_overflow_i64(int64_t a, int64_t b, int64_t imin, int64_t imax)
  1033. {
  1034. // TODO: use a better algorithm without division
  1035. uint64_t ua = (uint64_t)llabs(a);
  1036. uint64_t ub = (uint64_t)llabs(b);
  1037. uint64_t c;
  1038. if ((a > 0 && b > 0) || (a <= 0 && b <= 0))
  1039. c = (uint64_t)llabs(imax);
  1040. else
  1041. c = (uint64_t)llabs(imin);
  1042. return ua != 0 && ub > c / ua;
  1043. }
  1044. inline bool il2cpp_codegen_check_mul_oveflow_u64(uint64_t a, uint64_t b)
  1045. {
  1046. return b != 0 && (a * b) / b != a;
  1047. }
  1048. inline int32_t il2cpp_codegen_abs(uint32_t value)
  1049. {
  1050. return abs(static_cast<int32_t>(value));
  1051. }
  1052. inline int32_t il2cpp_codegen_abs(int32_t value)
  1053. {
  1054. return abs(value);
  1055. }
  1056. inline int64_t il2cpp_codegen_abs(uint64_t value)
  1057. {
  1058. return llabs(static_cast<int64_t>(value));
  1059. }
  1060. inline int64_t il2cpp_codegen_abs(int64_t value)
  1061. {
  1062. return llabs(value);
  1063. }
  1064. void il2cpp_codegen_memory_barrier();
  1065. template<typename T>
  1066. inline T VolatileRead(T* location)
  1067. {
  1068. T result = *location;
  1069. il2cpp_codegen_memory_barrier();
  1070. return result;
  1071. }
  1072. template<typename T, typename U>
  1073. inline void VolatileWrite(T** location, U* value)
  1074. {
  1075. il2cpp_codegen_memory_barrier();
  1076. *location = value;
  1077. Il2CppCodeGenWriteBarrier((void**)location, value);
  1078. }
  1079. template<typename T, typename U>
  1080. inline void VolatileWrite(T* location, U value)
  1081. {
  1082. il2cpp_codegen_memory_barrier();
  1083. *location = value;
  1084. }
  1085. inline void il2cpp_codegen_write_to_stdout(const char* str)
  1086. {
  1087. il2cpp::utils::Output::WriteToStdout(str);
  1088. }
  1089. inline void il2cpp_codegen_write_to_stderr(const char* str)
  1090. {
  1091. il2cpp::utils::Output::WriteToStderr(str);
  1092. }
  1093. REAL_NORETURN void il2cpp_codegen_abort();
  1094. inline bool il2cpp_codegen_check_add_overflow(int64_t left, int64_t right)
  1095. {
  1096. return (right >= 0 && left > kIl2CppInt64Max - right) ||
  1097. (left < 0 && right < kIl2CppInt64Min - left);
  1098. }
  1099. inline bool il2cpp_codegen_check_sub_overflow(int64_t left, int64_t right)
  1100. {
  1101. return (right >= 0 && left < kIl2CppInt64Min + right) ||
  1102. (right < 0 && left > kIl2CppInt64Max + right);
  1103. }
  1104. inline void il2cpp_codegen_register_debugger_data(const Il2CppDebuggerMetadataRegistration *data)
  1105. {
  1106. #if IL2CPP_MONO_DEBUGGER
  1107. il2cpp::utils::Debugger::RegisterMetadata(data);
  1108. #endif
  1109. }
  1110. inline void il2cpp_codegen_check_sequence_point(Il2CppSequencePointExecutionContext* executionContext, Il2CppSequencePoint* seqPoint)
  1111. {
  1112. #if IL2CPP_MONO_DEBUGGER
  1113. il2cpp::utils::Debugger::CheckSequencePoint(executionContext, seqPoint);
  1114. #endif
  1115. }
  1116. inline void il2cpp_codegen_check_sequence_point_entry(Il2CppSequencePointExecutionContext* executionContext, Il2CppSequencePoint* seqPoint)
  1117. {
  1118. #if IL2CPP_MONO_DEBUGGER
  1119. il2cpp::utils::Debugger::CheckSequencePointEntry(executionContext, seqPoint);
  1120. #endif
  1121. }
  1122. inline void il2cpp_codegen_check_sequence_point_exit(Il2CppSequencePointExecutionContext* executionContext, Il2CppSequencePoint* seqPoint)
  1123. {
  1124. #if IL2CPP_MONO_DEBUGGER
  1125. il2cpp::utils::Debugger::CheckSequencePointExit(executionContext, seqPoint);
  1126. #endif
  1127. }
  1128. inline void il2cpp_codegen_check_pause_point()
  1129. {
  1130. #if IL2CPP_MONO_DEBUGGER
  1131. il2cpp::utils::Debugger::CheckPausePoint();
  1132. #endif
  1133. }
  1134. class MethodExitSequencePointChecker
  1135. {
  1136. private:
  1137. Il2CppSequencePoint* m_seqPoint;
  1138. Il2CppSequencePointExecutionContext* m_seqPointStorage;
  1139. public:
  1140. MethodExitSequencePointChecker(Il2CppSequencePointExecutionContext* seqPointStorage, Il2CppSequencePoint* seqPoint) :
  1141. m_seqPointStorage(seqPointStorage), m_seqPoint(seqPoint)
  1142. {
  1143. }
  1144. ~MethodExitSequencePointChecker()
  1145. {
  1146. #if IL2CPP_MONO_DEBUGGER
  1147. il2cpp_codegen_check_sequence_point_exit(m_seqPointStorage, m_seqPoint);
  1148. #endif
  1149. }
  1150. };
  1151. #ifdef _MSC_VER
  1152. #define IL2CPP_DISABLE_OPTIMIZATIONS __pragma(optimize("", off))
  1153. #define IL2CPP_ENABLE_OPTIMIZATIONS __pragma(optimize("", on))
  1154. #elif IL2CPP_TARGET_LINUX || IL2CPP_TARGET_QNX
  1155. #define IL2CPP_DISABLE_OPTIMIZATIONS
  1156. #define IL2CPP_ENABLE_OPTIMIZATIONS
  1157. #else
  1158. #define IL2CPP_DISABLE_OPTIMIZATIONS __attribute__ ((optnone))
  1159. #define IL2CPP_ENABLE_OPTIMIZATIONS
  1160. #endif
  1161. // Array Unsafe
  1162. #define IL2CPP_ARRAY_UNSAFE_LOAD(TArray, TIndex) \
  1163. (TArray)->GetAtUnchecked(static_cast<il2cpp_array_size_t>(TIndex))
  1164. inline bool il2cpp_codegen_object_reference_equals(const RuntimeObject *obj1, const RuntimeObject *obj2)
  1165. {
  1166. return obj1 == obj2;
  1167. }
  1168. inline bool il2cpp_codegen_platform_is_osx_or_ios()
  1169. {
  1170. return IL2CPP_TARGET_OSX != 0 || IL2CPP_TARGET_IOS != 0;
  1171. }
  1172. inline bool il2cpp_codegen_platform_is_freebsd()
  1173. {
  1174. // we don't currently support FreeBSD
  1175. return false;
  1176. }
  1177. inline bool il2cpp_codegen_platform_is_uwp()
  1178. {
  1179. return IL2CPP_TARGET_WINRT != 0;
  1180. }
  1181. inline bool il2cpp_codegen_platform_disable_libc_pinvoke()
  1182. {
  1183. return IL2CPP_PLATFORM_DISABLE_LIBC_PINVOKE;
  1184. }
  1185. template<typename T>
  1186. inline T il2cpp_unsafe_read_unaligned(void* location)
  1187. {
  1188. T result;
  1189. #if IL2CPP_TARGET_ARMV7 || IL2CPP_TARGET_JAVASCRIPT
  1190. memcpy(&result, location, sizeof(T));
  1191. #else
  1192. result = *((T*)location);
  1193. #endif
  1194. return result;
  1195. }
  1196. template<typename T>
  1197. inline void il2cpp_unsafe_write_unaligned(void* location, T value)
  1198. {
  1199. #if IL2CPP_TARGET_ARMV7 || IL2CPP_TARGET_JAVASCRIPT
  1200. memcpy(location, &value, sizeof(T));
  1201. #else
  1202. *((T*)location) = value;
  1203. #endif
  1204. }
  1205. template<typename T>
  1206. inline T il2cpp_unsafe_read(void* location)
  1207. {
  1208. return *((T*)location);
  1209. }
  1210. template<typename T>
  1211. inline void il2cpp_unsafe_write(void* location, T value)
  1212. {
  1213. *((T*)location) = value;
  1214. }
  1215. template<typename T, typename TOffset>
  1216. inline T* il2cpp_unsafe_add(void* source, TOffset offset)
  1217. {
  1218. return reinterpret_cast<T*>(source) + offset;
  1219. }
  1220. template<typename T, typename TOffset>
  1221. inline T* il2cpp_unsafe_add_byte_offset(void* source, TOffset offset)
  1222. {
  1223. return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(source) + offset);
  1224. }
  1225. template<typename T, typename TOffset>
  1226. inline T* il2cpp_unsafe_subtract(void* source, TOffset offset)
  1227. {
  1228. return reinterpret_cast<T*>(source) - offset;
  1229. }
  1230. template<typename T, typename TOffset>
  1231. inline T* il2cpp_unsafe_subtract_byte_offset(void* source, TOffset offset)
  1232. {
  1233. return reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(source) - offset);
  1234. }
  1235. template<typename T>
  1236. inline T il2cpp_unsafe_as(void* source)
  1237. {
  1238. return reinterpret_cast<T>(source);
  1239. }
  1240. template<typename T>
  1241. inline T* il2cpp_unsafe_as_ref(void* source)
  1242. {
  1243. return reinterpret_cast<T*>(source);
  1244. }
  1245. inline void* il2cpp_unsafe_as_pointer(void* source)
  1246. {
  1247. return source;
  1248. }
  1249. template<typename T>
  1250. inline T* il2cpp_unsafe_null_ref()
  1251. {
  1252. return reinterpret_cast<T*>(NULL);
  1253. }
  1254. inline bool il2cpp_unsafe_are_same(void* left, void* right)
  1255. {
  1256. return left == right;
  1257. }
  1258. inline bool il2cpp_unsafe_is_addr_gt(void* left, void* right)
  1259. {
  1260. return left > right;
  1261. }
  1262. inline bool il2cpp_unsafe_is_addr_lt(void* left, void* right)
  1263. {
  1264. return left < right;
  1265. }
  1266. inline bool il2cpp_unsafe_is_null_ref(void* source)
  1267. {
  1268. return source == NULL;
  1269. }
  1270. template<typename T>
  1271. inline int32_t il2cpp_unsafe_sizeof()
  1272. {
  1273. return sizeof(T);
  1274. }
  1275. inline intptr_t il2cpp_unsafe_byte_offset(void* origin, void* target)
  1276. {
  1277. return reinterpret_cast<uint8_t*>(target) - reinterpret_cast<uint8_t*>(origin);
  1278. }