mkxp-z/binding/miniffi.cpp

56 lines
1.7 KiB
C++
Raw Normal View History

2020-12-31 16:58:47 -05:00
#include "miniffi.h"
#include <assert.h>
#if defined(__MINGW64__) || defined(__linux__) || defined(__APPLE__)
mffi_value miniffi_call_intern(MINIFFI_FUNC target, MiniFFIFuncArgs *p, int nparams) {
assert(nparams <= 10);
2020-12-31 16:58:47 -05:00
return target(p->params[0], p->params[1], p->params[2], p->params[3],
p->params[4], p->params[5], p->params[6],
p->params[7], p->params[8], p->params[9]);
2020-12-31 16:58:47 -05:00
}
#else // 32-bit Windows
2020-12-31 16:58:47 -05:00
#define INTEL_ASM ".intel_syntax noprefix\n"
2020-12-31 19:59:53 -05:00
__attribute__((noinline))
mffi_value miniffi_call_intern(MINIFFI_FUNC target, MiniFFIFuncArgs *p, int nparams) {
2020-12-31 16:58:47 -05:00
mffi_value ret;
void *old_esp = 0;
asm volatile(INTEL_ASM
"mov [edi], esp\n"
"test ebx, ebx\n"
"jz mffi_call_void\n"
2020-12-31 16:58:47 -05:00
"shl ebx, 2\n"
"mov ecx, ebx\n"
"mffi_call_loop:\n"
2020-12-31 16:58:47 -05:00
"sub ecx, 4\n"
"mov ebx, [esi+ecx]\n"
"push ebx\n"
"test ecx, ecx\n"
"jnz mffi_call_loop\n"
2020-12-31 16:58:47 -05:00
"mffi_call_void:\n"
2020-12-31 16:58:47 -05:00
"call edx\n"
: "=a"(ret)
2020-12-31 19:59:53 -05:00
: "b"(nparams), "S"(p), "d"(target), "D"(&old_esp)
2020-12-31 16:58:47 -05:00
: "ecx"
);
// If esp doesn't match, this was probably a cdecl and not a stdcall.
// Move the stack pointer back to where it should be
asm volatile(INTEL_ASM
"mov edx, [edi]\n"
"cmp edx, esp\n"
"cmovne esp, edx"
:
: "D"(&old_esp)
: "edx"
);
return ret;
}
#endif