暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

il2cpp-codegen.h 61KB

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