il2cpp-codegen.h 62 KB

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