il2cpp-codegen-common-small.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #pragma once
  2. #include "il2cpp-object-internals.h"
  3. inline void il2cpp_codegen_initobj(void* value, size_t size)
  4. {
  5. memset(value, 0, size);
  6. }
  7. template<typename TInput, typename TOutput, typename TFloat>
  8. inline TOutput il2cpp_codegen_cast_floating_point(TFloat value)
  9. {
  10. #if IL2CPP_TARGET_ARM64 || IL2CPP_TARGET_ARMV7
  11. // On ARM, a cast from a floating point to integer value will use
  12. // the min or max value if the cast is out of range (instead of
  13. // overflowing like x86/x64). So first do a cast to the output
  14. // type (which is signed in .NET - the value stack does not have
  15. // unsigned types) to try to get the value into a range that will
  16. // actually be cast.
  17. if (value < 0)
  18. return (TOutput)((TInput)(TOutput)value);
  19. #endif
  20. return (TOutput)((TInput)value);
  21. }
  22. template<bool, class T, class U>
  23. struct pick_first;
  24. template<class T, class U>
  25. struct pick_first<true, T, U>
  26. {
  27. typedef T type;
  28. };
  29. template<class T, class U>
  30. struct pick_first<false, T, U>
  31. {
  32. typedef U type;
  33. };
  34. template<class T, class U>
  35. struct pick_bigger
  36. {
  37. typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;
  38. };
  39. template<typename T, typename U>
  40. inline typename pick_bigger<T, U>::type il2cpp_codegen_multiply(T left, U right)
  41. {
  42. return left * right;
  43. }
  44. template<typename T, typename U>
  45. inline typename pick_bigger<T, U>::type il2cpp_codegen_add(T left, U right)
  46. {
  47. return left + right;
  48. }
  49. template<typename T, typename U>
  50. inline typename pick_bigger<T, U>::type il2cpp_codegen_subtract(T left, U right)
  51. {
  52. return left - right;
  53. }
  54. NORETURN void il2cpp_codegen_raise_exception(Exception_t* ex, RuntimeMethod* lastManagedFrame = NULL);
  55. // NativeArray macros
  56. #define IL2CPP_NATIVEARRAY_GET_ITEM(TElementType, TTField, TIndex) \
  57. *(reinterpret_cast<TElementType*>(TTField) + TIndex)
  58. #define IL2CPP_NATIVEARRAY_SET_ITEM(TElementType, TTField, TIndex, TValue) \
  59. *(reinterpret_cast<TElementType*>(TTField) + TIndex) = TValue;
  60. #define IL2CPP_NATIVEARRAY_GET_LENGTH(TLengthField) \
  61. (TLengthField)
  62. #if IL2CPP_TINY
  63. #include "utils/StringUtils.h"
  64. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str);
  65. inline String_t* il2cpp_codegen_string_new_from_char_array(Il2CppArray* characterArray, size_t startIndex, size_t length)
  66. {
  67. il2cpp_array_size_t arraySize = characterArray->max_length;
  68. if (startIndex + length > arraySize || startIndex < 0)
  69. il2cpp_codegen_raise_exception(NULL);
  70. return il2cpp_codegen_string_new_utf16(il2cpp::utils::StringView<Il2CppChar>(reinterpret_cast<Il2CppChar*>(characterArray + 1), startIndex, length));
  71. }
  72. inline int il2cpp_codegen_get_offset_to_string_data()
  73. {
  74. return offsetof(Il2CppString, chars);
  75. }
  76. inline int32_t il2cpp_codegen_get_array_length(Il2CppArray* szArray)
  77. {
  78. return static_cast<int32_t>(szArray->max_length);
  79. }
  80. int il2cpp_codegen_double_to_string(double value, uint8_t* format, uint8_t* buffer, int bufferLength);
  81. struct Delegate_t;
  82. inline intptr_t il2cpp_codegen_marshal_get_function_pointer_for_delegate(const Delegate_t* d)
  83. {
  84. return reinterpret_cast<intptr_t>(reinterpret_cast<const Il2CppDelegate*>(d)->m_ReversePInvokeWrapperPtr);
  85. }
  86. #endif // IL2CPP_TINY