OpenRCT2/test/testpaint/Addresses.cpp

88 lines
2.7 KiB
C++
Raw Normal View History

2017-12-03 23:07:24 +01:00
/*****************************************************************************
* Copyright (c) 2014-2018 OpenRCT2 developers
2017-12-03 23:07:24 +01:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-12-03 23:07:24 +01:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-12-03 23:07:24 +01:00
*****************************************************************************/
2018-01-11 16:13:17 +01:00
#include "Addresses.h"
2017-12-03 23:07:24 +01:00
#if defined(__GNUC__)
2018-06-22 22:29:03 +02:00
#ifdef __clang__
#define DISABLE_OPT __attribute__((noinline, optnone))
#else
#define DISABLE_OPT __attribute__((noinline, optimize("O0")))
#endif // __clang__
2017-12-03 23:07:24 +01:00
#else
#define DISABLE_OPT
#endif // defined(__GNUC__)
// This variable serves a purpose of identifying a crash if it has happened inside original code.
// When switching to original code, stack frame pointer is modified and prevents breakpad from providing stack trace.
volatile int32_t _originalAddress = 0;
2017-12-03 23:07:24 +01:00
2018-06-22 22:29:03 +02:00
int32_t DISABLE_OPT RCT2_CALLPROC_X(
int32_t address, int32_t _eax, int32_t _ebx, int32_t _ecx, int32_t _edx, int32_t _esi, int32_t _edi, int32_t _ebp)
2017-12-03 23:07:24 +01:00
{
int32_t result = 0;
2017-12-03 23:07:24 +01:00
_originalAddress = address;
#if defined(PLATFORM_X86) && !defined(NO_RCT2)
2018-06-22 22:29:03 +02:00
#ifdef _MSC_VER
2017-12-03 23:07:24 +01:00
__asm {
push ebp
push address
mov eax, _eax
mov ebx, _ebx
mov ecx, _ecx
mov edx, _edx
mov esi, _esi
mov edi, _edi
mov ebp, _ebp
call [esp]
lahf
pop ebp
pop ebp
2018-06-22 22:29:03 +02:00
/* Load result with flags */
2017-12-03 23:07:24 +01:00
mov result, eax
}
2018-06-22 22:29:03 +02:00
#else
__asm__ volatile("\
2017-12-03 23:07:24 +01:00
\n\
push %%ebx \n\
push %%ebp \n\
push %[address] \n\
mov %[eax], %%eax \n\
mov %[ebx], %%ebx \n\
mov %[ecx], %%ecx \n\
mov %[edx], %%edx \n\
mov %[esi], %%esi \n\
mov %[edi], %%edi \n\
mov %[ebp], %%ebp \n\
call *(%%esp) \n\
lahf \n\
add $4, %%esp \n\
pop %%ebp \n\
pop %%ebx \n\
/* Load result with flags */ \n\
mov %%eax, %[result] \n\
2018-06-22 22:29:03 +02:00
"
: [address] "+m"(address),
[eax] "+m"(_eax),
[ebx] "+m"(_ebx),
[ecx] "+m"(_ecx),
[edx] "+m"(_edx),
[esi] "+m"(_esi),
[edi] "+m"(_edi),
[ebp] "+m"(_ebp),
[result] "+m"(result)
:
: "eax", "ecx", "edx", "esi", "edi", "memory");
#endif
2017-12-03 23:07:24 +01:00
#endif // PLATFORM_X86
_originalAddress = 0;
// lahf only modifies ah, zero out the rest
return result & 0xFF00;
}