il2cpp-codegen-common.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #pragma once
  2. #include "il2cpp-config.h"
  3. #include "il2cpp-object-internals.h"
  4. #include <cmath>
  5. #include <limits>
  6. #include <type_traits>
  7. #ifdef _MSC_VER
  8. #define IL2CPP_DISABLE_OPTIMIZATIONS __pragma(optimize("", off))
  9. #define IL2CPP_ENABLE_OPTIMIZATIONS __pragma(optimize("", on))
  10. #elif IL2CPP_TARGET_LINUX || IL2CPP_TARGET_QNX
  11. #define IL2CPP_DISABLE_OPTIMIZATIONS
  12. #define IL2CPP_ENABLE_OPTIMIZATIONS
  13. #else
  14. #define IL2CPP_DISABLE_OPTIMIZATIONS __attribute__ ((optnone))
  15. #define IL2CPP_ENABLE_OPTIMIZATIONS
  16. #endif
  17. #if defined(__GNUC__) || defined(__SNC__) || defined(__clang__)
  18. #define IL2CPP_FORCE_INLINE_FOR_MINIGAME inline __attribute__ ((always_inline))
  19. #elif defined(_MSC_VER)
  20. #define IL2CPP_FORCE_INLINE_FOR_MINIGAME __forceinline
  21. #else
  22. #define IL2CPP_FORCE_INLINE_FOR_MINIGAME inline
  23. #endif
  24. template<typename T>
  25. using no_infer = typename std::common_type<T>::type;
  26. #if IL2CPP_ENABLE_WRITE_BARRIERS
  27. void Il2CppCodeGenWriteBarrier(void** targetAddress, void* object);
  28. void Il2CppCodeGenWriteBarrierForType(const Il2CppType* type, void** targetAddress, void* object);
  29. void Il2CppCodeGenWriteBarrierForClass(Il2CppClass* klass, void** targetAddress, void* object);
  30. #else
  31. inline void Il2CppCodeGenWriteBarrier(void** targetAddress, void* object) {}
  32. inline void Il2CppCodeGenWriteBarrierForType(const Il2CppType* type, void** targetAddress, void* object) {}
  33. inline void Il2CppCodeGenWriteBarrierForClass(Il2CppClass* klass, void** targetAddress, void* object) {}
  34. #endif
  35. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(void* dest, const void* src, size_t count)
  36. {
  37. return memcpy(dest, src, count);
  38. }
  39. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(intptr_t dest, const void* src, size_t count)
  40. {
  41. return memcpy((void*)dest, src, count);
  42. }
  43. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(uintptr_t dest, const void* src, size_t count)
  44. {
  45. return memcpy((void*)dest, src, count);
  46. }
  47. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(void* dest, intptr_t src, size_t count)
  48. {
  49. return memcpy(dest, (void*)src, count);
  50. }
  51. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(intptr_t dest, intptr_t src, size_t count)
  52. {
  53. return memcpy((void*)dest, (void*)src, count);
  54. }
  55. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(uintptr_t dest, intptr_t src, size_t count)
  56. {
  57. return memcpy((void*)dest, (void*)src, count);
  58. }
  59. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(void* dest, uintptr_t src, size_t count)
  60. {
  61. return memcpy(dest, (void*)src, count);
  62. }
  63. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(intptr_t dest, uintptr_t src, size_t count)
  64. {
  65. return memcpy((void*)dest, (void*)src, count);
  66. }
  67. IL2CPP_FORCE_INLINE_FOR_MINIGAME void* il2cpp_codegen_memcpy(uintptr_t dest, uintptr_t src, size_t count)
  68. {
  69. return memcpy((void*)dest, (void*)src, count);
  70. }
  71. IL2CPP_FORCE_INLINE_FOR_MINIGAME void il2cpp_codegen_memset(void* ptr, int value, size_t num)
  72. {
  73. memset(ptr, value, num);
  74. }
  75. IL2CPP_FORCE_INLINE_FOR_MINIGAME void il2cpp_codegen_memset(intptr_t ptr, int value, size_t num)
  76. {
  77. memset((void*)ptr, value, num);
  78. }
  79. IL2CPP_FORCE_INLINE_FOR_MINIGAME void il2cpp_codegen_memset(uintptr_t ptr, int value, size_t num)
  80. {
  81. memset((void*)ptr, value, num);
  82. }
  83. IL2CPP_FORCE_INLINE_FOR_MINIGAME void il2cpp_codegen_initobj(void* value, size_t size)
  84. {
  85. memset(value, 0, size);
  86. }
  87. IL2CPP_FORCE_INLINE_FOR_MINIGAME void il2cpp_codegen_initobj(intptr_t value, size_t size)
  88. {
  89. memset((void*)value, 0, size);
  90. }
  91. IL2CPP_FORCE_INLINE_FOR_MINIGAME void il2cpp_codegen_initobj(uintptr_t value, size_t size)
  92. {
  93. memset((void*)value, 0, size);
  94. }
  95. template<typename TInput, typename TOutput, typename TFloat>
  96. inline TOutput il2cpp_codegen_cast_floating_point(TFloat value)
  97. {
  98. // In release builds and on ARM, a cast from a floating point to
  99. // integer value will use the min or max value if the cast is out
  100. // of range (instead of overflowing like x86/x64 debug builds).
  101. // So first do a cast to the output type (which is signed in
  102. // .NET - the value stack does not have unsigned types) to try to
  103. // get the value into a range that will actually be cast the way .NET does.
  104. if (value < 0)
  105. return (TOutput)((TInput)(TOutput)value);
  106. return (TOutput)((TInput)value);
  107. }
  108. // ARM targets handle a cast of floating point positive infinity (0x7F800000)
  109. // differently from Intel targets. The expected behavior for .NET is from Intel,
  110. // where the cast to a 32-bit int produces the value 0x80000000. On ARM, the sign
  111. // is unchanged, producing 0x7FFFFFFF. To work around this change the positive
  112. // infinity value to negative infinity.
  113. template<typename T>
  114. inline T il2cpp_codegen_cast_double_to_int(double value)
  115. {
  116. #if IL2CPP_TARGET_ARM64 || IL2CPP_TARGET_ARMV7
  117. if (value == HUGE_VAL)
  118. {
  119. if (std::is_same<T, int64_t>::value)
  120. return INT64_MIN;
  121. if (std::is_same<T, int32_t>::value)
  122. return INT32_MIN;
  123. return 0;
  124. }
  125. #endif
  126. return (T)value;
  127. }
  128. template<bool, class T, class U>
  129. struct pick_first;
  130. template<class T, class U>
  131. struct pick_first<true, T, U>
  132. {
  133. typedef T type;
  134. };
  135. template<class T, class U>
  136. struct pick_first<false, T, U>
  137. {
  138. typedef U type;
  139. };
  140. template<class T, class U>
  141. struct pick_bigger
  142. {
  143. typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;
  144. };
  145. template<typename T, typename U>
  146. IL2CPP_FORCE_INLINE_FOR_MINIGAME typename pick_bigger<T, U>::type il2cpp_codegen_multiply(T left, U right)
  147. {
  148. return left * right;
  149. }
  150. template<typename T, typename U>
  151. IL2CPP_FORCE_INLINE_FOR_MINIGAME typename pick_bigger<T, U>::type il2cpp_codegen_add(T left, U right)
  152. {
  153. return left + right;
  154. }
  155. template<typename T, typename U>
  156. IL2CPP_FORCE_INLINE_FOR_MINIGAME typename pick_bigger<T, U>::type il2cpp_codegen_subtract(T left, U right)
  157. {
  158. return left - right;
  159. }
  160. template<typename T>
  161. inline bool il2cpp_codegen_enum_has_flag(T enumValue, T flag)
  162. {
  163. return (enumValue & flag) == flag;
  164. }
  165. NORETURN void il2cpp_codegen_raise_exception(Exception_t* ex, RuntimeMethod* lastManagedFrame = NULL);
  166. // NativeArray macros
  167. #define IL2CPP_NATIVEARRAY_GET_ITEM(TElementType, TTField, TIndex) \
  168. *(reinterpret_cast<TElementType*>(TTField) + TIndex)
  169. #define IL2CPP_NATIVEARRAY_SET_ITEM(TElementType, TTField, TIndex, TValue) \
  170. *(reinterpret_cast<TElementType*>(TTField) + TIndex) = TValue;
  171. #define IL2CPP_NATIVEARRAY_GET_LENGTH(TLengthField) \
  172. (TLengthField)
  173. inline bool il2cpp_codegen_is_little_endian()
  174. {
  175. #if IL2CPP_BYTE_ORDER == IL2CPP_LITTLE_ENDIAN
  176. return true;
  177. #else
  178. return false;
  179. #endif
  180. }
  181. // This code ids used for Tiny and for libil2cpp with Tiny when the debugger is enabled.
  182. #if IL2CPP_TINY
  183. #include "utils/StringUtils.h"
  184. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str);
  185. inline String_t* il2cpp_codegen_string_new_from_char_array(Il2CppArray* characterArray, size_t startIndex, size_t length)
  186. {
  187. il2cpp_array_size_t arraySize = characterArray->max_length;
  188. if (startIndex + length > arraySize || startIndex < 0)
  189. il2cpp_codegen_raise_exception(NULL);
  190. return il2cpp_codegen_string_new_utf16(il2cpp::utils::StringView<Il2CppChar>(reinterpret_cast<Il2CppChar*>(characterArray + 1), startIndex, length));
  191. }
  192. inline int il2cpp_codegen_get_offset_to_string_data()
  193. {
  194. return offsetof(Il2CppString, chars);
  195. }
  196. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* szArray)
  197. {
  198. return static_cast<int32_t>(szArray->max_length);
  199. }
  200. int il2cpp_codegen_double_to_string(double value, uint8_t* format, uint8_t* buffer, int bufferLength);
  201. struct Delegate_t;
  202. inline intptr_t il2cpp_codegen_marshal_get_function_pointer_for_delegate(const Delegate_t* d)
  203. {
  204. return reinterpret_cast<intptr_t>(reinterpret_cast<const Il2CppDelegate*>(d)->m_ReversePInvokeWrapperPtr);
  205. }
  206. inline void* il2cpp_codegen_get_reverse_pinvoke_function_ptr(void* d)
  207. {
  208. return d;
  209. }
  210. #endif // IL2CPP_TINY
  211. NORETURN void il2cpp_codegen_raise_overflow_exception(const RuntimeMethod* method);
  212. template<typename T>
  213. constexpr bool il2cpp_codegen_is_floating_point_type()
  214. {
  215. return std::is_same<T, float>::value || std::is_same<T, double>::value;
  216. }
  217. template<typename TDest, typename TSource, typename TILStackType, bool checkOverflow, bool treatInputAsUnsigned, bool destIsFloatingPoint, bool sourceIsFloatingPoint>
  218. class ConvImpl
  219. {
  220. static TDest Conv(TSource srcValue, const RuntimeMethod* method);
  221. };
  222. template<typename TDest, typename TSource, typename TILStackType, bool checkOverflow, bool treatInputAsUnsigned>
  223. struct ConvImpl<TDest, TSource, TILStackType, checkOverflow, treatInputAsUnsigned, false, false>
  224. {
  225. // Integer type to integer type
  226. static TDest Conv(TSource srcValue, const RuntimeMethod* method)
  227. {
  228. IL2CPP_ASSERT(!il2cpp_codegen_is_floating_point_type<TDest>() && !il2cpp_codegen_is_floating_point_type<TSource>());
  229. TILStackType ilStackValue = (TILStackType)srcValue;
  230. if (checkOverflow)
  231. {
  232. typedef typename pick_bigger<TDest, TILStackType>::type CompType;
  233. if (!treatInputAsUnsigned && !std::is_unsigned<TDest>::value)
  234. {
  235. if ((CompType)ilStackValue > (CompType)std::numeric_limits<TDest>::max())
  236. il2cpp_codegen_raise_overflow_exception(method);
  237. if ((CompType)ilStackValue < (CompType)std::numeric_limits<TDest>::min())
  238. il2cpp_codegen_raise_overflow_exception(method);
  239. }
  240. if (treatInputAsUnsigned || std::is_unsigned<TDest>::value)
  241. {
  242. if ((typename std::make_unsigned<TILStackType>::type)ilStackValue > (typename std::make_unsigned<TDest>::type) std::numeric_limits<TDest>::max())
  243. il2cpp_codegen_raise_overflow_exception(method);
  244. if (!treatInputAsUnsigned && ilStackValue < 0)
  245. il2cpp_codegen_raise_overflow_exception(method);
  246. }
  247. }
  248. if (std::is_unsigned<TDest>::value)
  249. return (TDest)(typename std::make_unsigned<TILStackType>::type)ilStackValue;
  250. #if __cplusplus < 202022L
  251. // Prior to C++ 20 conversion of integer types to smaller types is undefined behavior
  252. // In most implementations it works as expected, except the optimizer is allowed to optimize it out
  253. if (sizeof(TDest) >= sizeof(TILStackType))
  254. return (TDest)ilStackValue;
  255. constexpr TILStackType mask = (TILStackType)std::numeric_limits<typename std::make_unsigned<TDest>::type>::max();
  256. return (TDest)(ilStackValue & mask);
  257. #else
  258. return (TDest)ilStackValue;
  259. #endif
  260. }
  261. };
  262. template<typename TDest, typename TSource, typename TILStackType, bool checkOverflow, bool treatInputAsUnsigned>
  263. struct ConvImpl<TDest, TSource, TILStackType, checkOverflow, treatInputAsUnsigned, false, true>
  264. {
  265. // Floating point type to integer type
  266. static TDest Conv(TSource srcValue, const RuntimeMethod* method)
  267. {
  268. IL2CPP_ASSERT(!il2cpp_codegen_is_floating_point_type<TDest>() && il2cpp_codegen_is_floating_point_type<TSource>());
  269. TILStackType ilStackValue = (TILStackType)srcValue;
  270. if (checkOverflow)
  271. {
  272. if (ilStackValue > (TILStackType)std::numeric_limits<TDest>::max())
  273. il2cpp_codegen_raise_overflow_exception(method);
  274. if (std::is_signed<TDest>::value && ilStackValue < (TILStackType)std::numeric_limits<TDest>::min())
  275. il2cpp_codegen_raise_overflow_exception(method);
  276. if (std::is_unsigned<TDest>::value && ilStackValue < 0)
  277. il2cpp_codegen_raise_overflow_exception(method);
  278. }
  279. if (std::is_same<TDest, typename std::make_unsigned<TDest>::type>::value)
  280. return il2cpp_codegen_cast_floating_point<typename std::make_signed<TDest>::type, TDest, TSource>(ilStackValue);
  281. return il2cpp_codegen_cast_double_to_int<TDest>(ilStackValue);
  282. }
  283. };
  284. template<typename TDest, typename TSource, typename TILStackType, bool checkOverflow, bool treatInputAsUnsigned>
  285. struct ConvImpl<TDest, TSource, TILStackType, checkOverflow, treatInputAsUnsigned, true, false>
  286. {
  287. // Integer type to floating point type
  288. static TDest Conv(TSource srcValue, const RuntimeMethod * method)
  289. {
  290. IL2CPP_ASSERT(il2cpp_codegen_is_floating_point_type<TDest>() && !il2cpp_codegen_is_floating_point_type<TSource>());
  291. TILStackType ilStackValue = (TILStackType)srcValue;
  292. if (treatInputAsUnsigned)
  293. return (TDest)(typename std::make_unsigned<TILStackType>::type)ilStackValue;
  294. return (TDest)ilStackValue;
  295. }
  296. };
  297. template<typename TDest, typename TSource, typename TILStackType, bool checkOverflow, bool treatInputAsUnsigned>
  298. struct ConvImpl<TDest, TSource, TILStackType, checkOverflow, treatInputAsUnsigned, true, true>
  299. {
  300. // Floating point to floating point type
  301. static TDest Conv(TSource srcValue, const RuntimeMethod* method)
  302. {
  303. IL2CPP_ASSERT(il2cpp_codegen_is_floating_point_type<TDest>() && il2cpp_codegen_is_floating_point_type<TSource>());
  304. return (TDest)srcValue;
  305. }
  306. };
  307. template<typename TDest, typename TSource, typename TILStackType, bool checkOverflow, bool treatInputAsUnsigned>
  308. TDest il2cpp_codegen_conv(TSource srcValue, const RuntimeMethod* method)
  309. {
  310. return ConvImpl<TDest, TSource, TILStackType, checkOverflow, treatInputAsUnsigned, il2cpp_codegen_is_floating_point_type<TDest>(), il2cpp_codegen_is_floating_point_type<TSource>()>::Conv(srcValue, method);
  311. }