il2cpp-codegen-common.h
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#pragma once
#include "il2cpp-config.h"
#include <cmath>
#include "il2cpp-object-internals.h"
#include "il2cpp-class-internals.h"
#include "il2cpp-tabledefs.h"
#include "vm-utils/Debugger.h"
#include "utils/LeaveTargetStack.h"
#include "utils/Output.h"
REAL_NORETURN IL2CPP_NO_INLINE void il2cpp_codegen_no_return();
#if IL2CPP_COMPILER_MSVC
#define DEFAULT_CALL STDCALL
#else
#define DEFAULT_CALL
#endif
#if defined(__ARMCC_VERSION)
inline double bankers_round(double x)
{
return __builtin_round(x);
}
inline float bankers_roundf(float x)
{
return __builtin_roundf(x);
}
#else
inline double bankers_round(double x)
{
double integerPart;
if (x >= 0.0)
{
if (modf(x, &integerPart) == 0.5)
return (int64_t)integerPart % 2 == 0 ? integerPart : integerPart + 1.0;
return floor(x + 0.5);
}
else
{
if (modf(x, &integerPart) == -0.5)
return (int64_t)integerPart % 2 == 0 ? integerPart : integerPart - 1.0;
return ceil(x - 0.5);
}
}
inline float bankers_roundf(float x)
{
double integerPart;
if (x >= 0.0f)
{
if (modf(x, &integerPart) == 0.5)
return (int64_t)integerPart % 2 == 0 ? (float)integerPart : (float)integerPart + 1.0f;
return floorf(x + 0.5f);
}
else
{
if (modf(x, &integerPart) == -0.5)
return (int64_t)integerPart % 2 == 0 ? (float)integerPart : (float)integerPart - 1.0f;
return ceilf(x - 0.5f);
}
}
#endif
// returns true if overflow occurs
inline bool il2cpp_codegen_check_mul_overflow_i64(int64_t a, int64_t b, int64_t imin, int64_t imax)
{
// TODO: use a better algorithm without division
uint64_t ua = (uint64_t)llabs(a);
uint64_t ub = (uint64_t)llabs(b);
uint64_t c;
if ((a > 0 && b > 0) || (a <= 0 && b <= 0))
c = (uint64_t)llabs(imax);
else
c = (uint64_t)llabs(imin);
return ua != 0 && ub > c / ua;
}
inline bool il2cpp_codegen_check_mul_oveflow_u64(uint64_t a, uint64_t b)
{
return b != 0 && (a * b) / b != a;
}
inline int32_t il2cpp_codegen_abs(uint32_t value)
{
return abs(static_cast<int32_t>(value));
}
inline int32_t il2cpp_codegen_abs(int32_t value)
{
return abs(value);
}
inline int64_t il2cpp_codegen_abs(uint64_t value)
{
return llabs(static_cast<int64_t>(value));
}
inline int64_t il2cpp_codegen_abs(int64_t value)
{
return llabs(value);
}
template<typename TInput, typename TOutput, typename TFloat>
inline TOutput il2cpp_codegen_cast_floating_point(TFloat value)
{
#if IL2CPP_TARGET_ARM64 || IL2CPP_TARGET_ARMV7
// On ARM, a cast from a floating point to integer value will use
// the min or max value if the cast is out of range (instead of
// overflowing like x86/x64). So first do a cast to the output
// type (which is signed in .NET - the value stack does not have
// unsigned types) to try to get the value into a range that will
// actually be cast.
if (value < 0)
return (TOutput)((TInput)(TOutput)value);
#endif
return (TOutput)((TInput)value);
}
// Exception support macros
#define IL2CPP_LEAVE(Offset, Target) \
__leave_targets.push(Offset); \
goto Target;
#define IL2CPP_END_FINALLY(Id) \
goto __CLEANUP_ ## Id;
#define IL2CPP_CLEANUP(Id) \
__CLEANUP_ ## Id:
#define IL2CPP_RETHROW_IF_UNHANDLED(ExcType) \
if(__last_unhandled_exception) { \
ExcType _tmp_exception_local = __last_unhandled_exception; \
__last_unhandled_exception = 0; \
il2cpp_codegen_raise_exception(_tmp_exception_local); \
}
#define IL2CPP_JUMP_TBL(Offset, Target) \
if(!__leave_targets.empty() && __leave_targets.top() == Offset) { \
__leave_targets.pop(); \
goto Target; \
}
#define IL2CPP_END_CLEANUP(Offset, Target) \
if(!__leave_targets.empty() && __leave_targets.top() == Offset) \
goto Target;
#define IL2CPP_RAISE_MANAGED_EXCEPTION(message, lastManagedFrame) \
do {\
il2cpp_codegen_raise_exception((Exception_t*)message, (MethodInfo*)lastManagedFrame);\
il2cpp_codegen_no_return();\
} while (0)
#if IL2CPP_ENABLE_WRITE_BARRIERS
void Il2CppCodeGenWriteBarrier(void** targetAddress, void* object);
#else
inline void Il2CppCodeGenWriteBarrier(void** targetAddress, void* object) {}
#endif
void il2cpp_codegen_memory_barrier();
template<typename T>
inline T VolatileRead(T* location)
{
T result = *location;
il2cpp_codegen_memory_barrier();
return result;
}
template<typename T>
inline void VolatileWrite(T** location, T* value)
{
il2cpp_codegen_memory_barrier();
*location = value;
Il2CppCodeGenWriteBarrier((void**)location, value);
}
template<typename T>
inline void VolatileWrite(T* location, T value)
{
il2cpp_codegen_memory_barrier();
*location = value;
}
inline void il2cpp_codegen_write_to_stdout(const char* str)
{
il2cpp::utils::Output::WriteToStdout(str);
}
#if IL2CPP_TARGET_LUMIN
inline void il2cpp_codegen_write_to_stdout_args(const char* str, ...)
{
va_list args, local;
char* buffer = nullptr;
va_start(args, str);
va_copy(local, args);
int size = vsnprintf(nullptr, 0, str, local);
if (size < 0)
{
va_end(local);
va_end(args);
return;
}
va_end(local);
va_copy(local, args);
buffer = new char[size + 1];
vsnprintf(buffer, size + 1, str, local);
il2cpp::utils::Output::WriteToStdout(buffer);
if (buffer != nullptr)
delete[] buffer;
va_end(local);
va_end(args);
}
#endif
inline void il2cpp_codegen_write_to_stderr(const char* str)
{
il2cpp::utils::Output::WriteToStderr(str);
}
#if IL2CPP_TARGET_LUMIN
inline void il2cpp_codegen_write_to_stderr_args(const char* str, ...)
{
va_list args, local;
char* buffer = nullptr;
va_start(args, str);
va_copy(local, args);
int size = vsnprintf(nullptr, 0, str, local);
if (size < 0)
{
va_end(local);
va_end(args);
return;
}
va_end(local);
va_copy(local, args);
buffer = new char[size + 1];
vsnprintf(buffer, size + 1, str, local);
il2cpp::utils::Output::WriteToStderr(buffer);
if (buffer != nullptr)
delete[] buffer;
va_end(local);
va_end(args);
}
#endif
REAL_NORETURN void il2cpp_codegen_abort();
inline bool il2cpp_codegen_check_add_overflow(int64_t left, int64_t right)
{
return (right >= 0 && left > kIl2CppInt64Max - right) ||
(left < 0 && right < kIl2CppInt64Min - left);
}
inline bool il2cpp_codegen_check_sub_overflow(int64_t left, int64_t right)
{
return (right >= 0 && left < kIl2CppInt64Min + right) ||
(right < 0 && left > kIl2CppInt64Max + right);
}
template<bool, class T, class U>
struct pick_first;
template<class T, class U>
struct pick_first<true, T, U>
{
typedef T type;
};
template<class T, class U>
struct pick_first<false, T, U>
{
typedef U type;
};
template<class T, class U>
struct pick_bigger
{
typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;
};
template<typename T, typename U>
inline typename pick_bigger<T, U>::type il2cpp_codegen_multiply(T left, U right)
{
return left * right;
}
template<typename T, typename U>
inline typename pick_bigger<T, U>::type il2cpp_codegen_add(T left, U right)
{
return left + right;
}
template<typename T, typename U>
inline typename pick_bigger<T, U>::type il2cpp_codegen_subtract(T left, U right)
{
return left - right;
}
inline void il2cpp_codegen_memcpy(void* dest, const void* src, size_t count)
{
memcpy(dest, src, count);
}
inline void il2cpp_codegen_memset(void* ptr, int value, size_t num)
{
memset(ptr, value, num);
}
inline void il2cpp_codegen_register_debugger_data(const Il2CppDebuggerMetadataRegistration *data)
{
#if IL2CPP_MONO_DEBUGGER
il2cpp::utils::Debugger::RegisterMetadata(data);
#endif
}
inline void il2cpp_codegen_check_sequence_point(Il2CppSequencePointExecutionContext* executionContext, Il2CppSequencePoint* seqPoint)
{
#if IL2CPP_MONO_DEBUGGER
il2cpp::utils::Debugger::CheckSequencePoint(executionContext, seqPoint);
#endif
}
inline void il2cpp_codegen_check_sequence_point_entry(Il2CppSequencePointExecutionContext* executionContext, Il2CppSequencePoint* seqPoint)
{
#if IL2CPP_MONO_DEBUGGER
il2cpp::utils::Debugger::CheckSequencePointEntry(executionContext, seqPoint);
#endif
}
inline void il2cpp_codegen_check_sequence_point_exit(Il2CppSequencePointExecutionContext* executionContext, Il2CppSequencePoint* seqPoint)
{
#if IL2CPP_MONO_DEBUGGER
il2cpp::utils::Debugger::CheckSequencePointExit(executionContext, seqPoint);
#endif
}
inline void il2cpp_codegen_check_pause_point()
{
#if IL2CPP_MONO_DEBUGGER
il2cpp::utils::Debugger::CheckPausePoint();
#endif
}
class MethodExitSequencePointChecker
{
private:
Il2CppSequencePoint* m_seqPoint;
Il2CppSequencePointExecutionContext* m_seqPointStorage;
public:
MethodExitSequencePointChecker(Il2CppSequencePointExecutionContext* seqPointStorage, Il2CppSequencePoint* seqPoint) :
m_seqPointStorage(seqPointStorage), m_seqPoint(seqPoint)
{
}
~MethodExitSequencePointChecker()
{
#if IL2CPP_MONO_DEBUGGER
il2cpp_codegen_check_sequence_point_exit(m_seqPointStorage, m_seqPoint);
#endif
}
};
#ifdef _MSC_VER
#define IL2CPP_DISABLE_OPTIMIZATIONS __pragma(optimize("", off))
#define IL2CPP_ENABLE_OPTIMIZATIONS __pragma(optimize("", on))
#elif IL2CPP_TARGET_LINUX
#define IL2CPP_DISABLE_OPTIMIZATIONS
#define IL2CPP_ENABLE_OPTIMIZATIONS
#else
#define IL2CPP_DISABLE_OPTIMIZATIONS __attribute__ ((optnone))
#define IL2CPP_ENABLE_OPTIMIZATIONS
#endif
// NativeArray macros
#define IL2CPP_NATIVEARRAY_GET_ITEM(TElementType, TTField, TIndex) \
*(reinterpret_cast<TElementType*>(TTField) + TIndex)
#define IL2CPP_NATIVEARRAY_SET_ITEM(TElementType, TTField, TIndex, TValue) \
*(reinterpret_cast<TElementType*>(TTField) + TIndex) = TValue;
#define IL2CPP_NATIVEARRAY_GET_LENGTH(TLengthField) \
(TLengthField)
// Array Unsafe
#define IL2CPP_ARRAY_UNSAFE_LOAD(TArray, TIndex) \
(TArray)->GetAtUnchecked(static_cast<il2cpp_array_size_t>(TIndex))
inline bool il2cpp_codegen_object_reference_equals(const RuntimeObject *obj1, const RuntimeObject *obj2)
{
return obj1 == obj2;
}
inline bool il2cpp_codegen_platform_is_osx_or_ios()
{
return IL2CPP_TARGET_OSX != 0 || IL2CPP_TARGET_IOS != 0;
}
inline bool il2cpp_codegen_platform_is_freebsd()
{
// we don't currently support FreeBSD
return false;
}
inline bool il2cpp_codegen_platform_disable_libc_pinvoke()
{
return IL2CPP_PLATFORM_DISABLE_LIBC_PINVOKE;
}