123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524 |
- #ifndef _NSEEL_GLUE_X86_H_
- #define _NSEEL_GLUE_X86_H_
- #include <intrin.h>
- #define GLUE_MAX_FPSTACK_SIZE 8
- #define GLUE_JMP_SET_OFFSET(endOfInstruction,offset) (((int *)(endOfInstruction))[-1] = (offset))
- static const unsigned char GLUE_JMP_NC[] = { 0xE9, 0,0,0,0, };
- static const unsigned char GLUE_JMP_IF_P1_Z[] = {0x85, 0xC0, 0x0F, 0x84, 0,0,0,0 };
- static const unsigned char GLUE_JMP_IF_P1_NZ[] = {0x85, 0xC0, 0x0F, 0x85, 0,0,0,0 };
- #define GLUE_FUNC_ENTER_SIZE 0
- #define GLUE_FUNC_LEAVE_SIZE 0
- const static unsigned int GLUE_FUNC_ENTER[1];
- const static unsigned int GLUE_FUNC_LEAVE[1];
-
-
-
-
- static const unsigned char GLUE_PUSH_P1PTR_AS_VALUE[] =
- {
- 0x83, 0xEC, 8,
- 0xff, 0x70, 0x4,
- 0xff, 0x30,
- };
- static int GLUE_POP_VALUE_TO_ADDR(unsigned char *buf, void *destptr)
- {
- if (buf)
- {
- *buf++ = 0xB8; *(void **) buf = destptr; buf+=4;
-
- *buf++ = 0x8f; *buf++ = 0x00;
- *buf++ = 0x8f; *buf++ = 0x40; *buf++ = 4;
-
- *buf++ = 0x59;
- *buf++ = 0x59;
- }
-
- return 12;
- }
- static int GLUE_COPY_VALUE_AT_P1_TO_PTR(unsigned char *buf, void *destptr)
- {
- if (buf)
- {
- *buf++ = 0x8B; *buf++ = 0x38;
- *buf++ = 0x8B; *buf++ = 0x48; *buf++ = 0x04;
-
-
- *buf++ = 0xB8; *(void **) buf = destptr; buf+=4;
- *buf++ = 0x89; *buf++ = 0x38;
- *buf++ = 0x89; *buf++ = 0x48; *buf++ = 0x04;
- }
-
- return 2 + 3 + 5 + 2 + 3;
- }
- static int GLUE_POP_FPSTACK_TO_PTR(unsigned char *buf, void *destptr)
- {
- if (buf)
- {
- *buf++ = 0xB8; *(void **) buf = destptr; buf+=4;
- *buf++ = 0xDD; *buf++ = 0x18;
- }
- return 1+4+2;
- }
- #define GLUE_MOV_PX_DIRECTVALUE_SIZE 5
- #define GLUE_MOV_PX_DIRECTVALUE_TOSTACK_SIZE 6
- static void GLUE_MOV_PX_DIRECTVALUE_GEN(void *b, INT_PTR v, int wv)
- {
- if (wv==-1)
- {
- const static unsigned char t[2] = {0xDD, 0x05};
- memcpy(b,t,2);
- b= ((unsigned char *)b)+2;
- }
- else
- {
- const static unsigned char tab[3] = {
- 0xB8 ,
- 0xBF ,
- 0xB9
- };
- *((unsigned char *)b) = tab[wv];
- b= ((unsigned char *)b)+1;
- }
- *(INT_PTR *)b = v;
- }
- const static unsigned char GLUE_PUSH_P1[4]={0x83, 0xEC, 12, 0x50};
- #define GLUE_STORE_P1_TO_STACK_AT_OFFS_SIZE(x) 7
- static void GLUE_STORE_P1_TO_STACK_AT_OFFS(void *b, int offs)
- {
- ((unsigned char *)b)[0] = 0x89;
- ((unsigned char *)b)[1] = 0x84;
- ((unsigned char *)b)[2] = 0x24;
- *(int *)((unsigned char *)b+3) = offs;
- }
- #define GLUE_MOVE_PX_STACKPTR_SIZE 2
- static void GLUE_MOVE_PX_STACKPTR_GEN(void *b, int wv)
- {
- static const unsigned char tab[3][GLUE_MOVE_PX_STACKPTR_SIZE]=
- {
- { 0x89, 0xe0 },
- { 0x89, 0xe7 },
- { 0x89, 0xe1 },
- };
- memcpy(b,tab[wv],GLUE_MOVE_PX_STACKPTR_SIZE);
- }
- #define GLUE_MOVE_STACK_SIZE 6
- static void GLUE_MOVE_STACK(void *b, int amt)
- {
- ((unsigned char *)b)[0] = 0x81;
- if (amt <0)
- {
- ((unsigned char *)b)[1] = 0xEC;
- *(int *)((char*)b+2) = -amt;
- }
- else
- {
- ((unsigned char *)b)[1] = 0xc4;
- *(int *)((char*)b+2) = amt;
- }
- }
- #define GLUE_POP_PX_SIZE 4
- static void GLUE_POP_PX(void *b, int wv)
- {
- static const unsigned char tab[3][GLUE_POP_PX_SIZE]=
- {
- {0x58, 0x83, 0xC4, 12 },
- {0x5F, 0x83, 0xC4, 12},
- {0x59, 0x83, 0xC4, 12},
- };
- memcpy(b,tab[wv],GLUE_POP_PX_SIZE);
- }
- #define GLUE_SET_PX_FROM_P1_SIZE 2
- static void GLUE_SET_PX_FROM_P1(void *b, int wv)
- {
- static const unsigned char tab[3][GLUE_SET_PX_FROM_P1_SIZE]={
- {0x90,0x90},
- {0x89,0xC7},
- {0x89,0xC1},
- };
- memcpy(b,tab[wv],GLUE_SET_PX_FROM_P1_SIZE);
- }
- #define GLUE_POP_FPSTACK_SIZE 2
- static const unsigned char GLUE_POP_FPSTACK[2] = { 0xDD, 0xD8 };
- static const unsigned char GLUE_POP_FPSTACK_TOSTACK[] = {
- 0x83, 0xEC, 16,
- 0xDD, 0x1C, 0x24
- };
- static const unsigned char GLUE_POP_STACK_TO_FPSTACK[] = {
- 0xDD, 0x04, 0x24,
- 0x83, 0xC4, 16
- };
-
- static const unsigned char GLUE_POP_FPSTACK_TO_WTP[] = {
- 0xDD, 0x1E,
- 0x83, 0xC6, 8,
- };
- #define GLUE_SET_PX_FROM_WTP_SIZE 2
- static void GLUE_SET_PX_FROM_WTP(void *b, int wv)
- {
- static const unsigned char tab[3][GLUE_SET_PX_FROM_WTP_SIZE]={
- {0x89,0xF0},
- {0x89,0xF7},
- {0x89,0xF1},
- };
- memcpy(b,tab[wv],GLUE_SET_PX_FROM_WTP_SIZE);
- }
- #define GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE 2
- static void GLUE_PUSH_VAL_AT_PX_TO_FPSTACK(void *b, int wv)
- {
- static const unsigned char tab[3][GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE]={
- {0xDD,0x00},
- {0xDD,0x07},
- {0xDD,0x01},
- };
- memcpy(b,tab[wv],GLUE_PUSH_VAL_AT_PX_TO_FPSTACK_SIZE);
- }
- #define GLUE_POP_FPSTACK_TO_WTP_TO_PX_SIZE (GLUE_SET_PX_FROM_WTP_SIZE + sizeof(GLUE_POP_FPSTACK_TO_WTP))
- static void GLUE_POP_FPSTACK_TO_WTP_TO_PX(unsigned char *buf, int wv)
- {
- GLUE_SET_PX_FROM_WTP(buf,wv);
- memcpy(buf + GLUE_SET_PX_FROM_WTP_SIZE,GLUE_POP_FPSTACK_TO_WTP,sizeof(GLUE_POP_FPSTACK_TO_WTP));
- };
- const static unsigned char GLUE_RET=0xC3;
- static int GLUE_RESET_WTP(unsigned char *out, void *ptr)
- {
- if (out)
- {
- *out++ = 0xBE;
- memcpy(out,&ptr,sizeof(void *));
- out+=sizeof(void *);
- }
- return 1+sizeof(void *);
- }
- #ifdef _MSC_VER
- #pragma warning(push)
- #pragma warning(disable: 4731)
- #endif
- #define GLUE_TABPTR_IGNORED
- #define GLUE_CALL_CODE(bp, cp, rt) do { \
- if (h->compile_flags&NSEEL_CODE_COMPILE_FLAG_NOFPSTATE) eel_callcode32_fast(cp, rt); \
- else eel_callcode32(cp, rt);\
- } while(0)
- static void eel_callcode32(INT_PTR cp, INT_PTR ramptr)
- {
- #ifndef NSEEL_EEL1_COMPAT_MODE
- short oldsw, newsw;
- #endif
- #ifdef _MSC_VER
- __asm
- {
- #ifndef NSEEL_EEL1_COMPAT_MODE
- fnstcw [oldsw]
- mov ax, [oldsw]
- or ax, 0xE3F
- mov [newsw], ax
- fldcw [newsw]
- #endif
-
- mov eax, cp
- mov ebx, ramptr
- pushad
- mov ebp, esp
- and esp, -16
-
-
-
-
-
-
-
-
- sub esp, 12
- call eax
- mov esp, ebp
- popad
- #ifndef NSEEL_EEL1_COMPAT_MODE
- fldcw [oldsw]
- #endif
- };
- #else
- __asm__(
- #ifndef NSEEL_EEL1_COMPAT_MODE
- "fnstcw %2\n"
- "movw %2, %%ax\n"
- "orw $0xE3F, %%ax\n"
- "movw %%ax, %3\n"
- "fldcw %3\n"
- #endif
- "pushl %%ebx\n"
- "movl %%ecx, %%ebx\n"
- "pushl %%ebp\n"
- "movl %%esp, %%ebp\n"
- "andl $-16, %%esp\n"
- "subl $12, %%esp\n"
- "call *%%edx\n"
- "leave\n"
- "popl %%ebx\n"
- #ifndef NSEEL_EEL1_COMPAT_MODE
- "fldcw %2\n"
- #endif
- ::
- "d" (cp), "c" (ramptr)
- #ifndef NSEEL_EEL1_COMPAT_MODE
- , "m" (oldsw), "m" (newsw)
- #endif
- : "%eax","%esi","%edi");
- #endif
- }
- void eel_enterfp(int s[2])
- {
- #ifdef _MSC_VER
- __asm
- {
- mov ecx, s
- fnstcw [ecx]
- mov ax, [ecx]
- or ax, 0xE3F
- mov [ecx+4], ax
- fldcw [ecx+4]
- };
- #else
- __asm__(
- "fnstcw (%%ecx)\n"
- "movw (%%ecx), %%ax\n"
- "orw $0xE3F, %%ax\n"
- "movw %%ax, 4(%%ecx)\n"
- "fldcw 4(%%ecx)\n"
- :: "c" (s) : "%eax");
- #endif
- }
- void eel_leavefp(int s[2])
- {
- #ifdef _MSC_VER
- __asm
- {
- mov ecx, s
- fldcw [ecx]
- };
- #else
- __asm__(
- "fldcw (%%ecx)\n"
- :: "c" (s) : "%eax");
- #endif
- }
- static void eel_callcode32_fast(INT_PTR cp, INT_PTR ramptr)
- {
- #ifdef _MSC_VER
- __asm
- {
- mov eax, cp
- mov ebx, ramptr
- pushad
- mov ebp, esp
- and esp, -16
-
-
-
-
-
-
-
-
- sub esp, 12
- call eax
- mov esp, ebp
- popad
- };
- #else
- __asm__(
- "pushl %%ebx\n"
- "movl %%ecx, %%ebx\n"
- "pushl %%ebp\n"
- "movl %%esp, %%ebp\n"
- "andl $-16, %%esp\n"
- "subl $12, %%esp\n"
- "call *%%edx\n"
- "leave\n"
- "popl %%ebx\n"
- ::
- "d" (cp), "c" (ramptr)
- : "%eax","%esi","%edi");
- #endif
- }
- #ifdef _MSC_VER
- #pragma warning(pop)
- #endif
- static unsigned char *EEL_GLUE_set_immediate(void *_p, INT_PTR newv)
- {
- char *p=(char*)_p;
- INT_PTR scan = 0xFEFEFEFE;
- while (*(INT_PTR *)p != scan) p++;
- *(INT_PTR *)p = newv;
- return (unsigned char *) (((INT_PTR*)p)+1);
- }
- #define INT_TO_LECHARS(x) ((x)&0xff),(((x)>>8)&0xff), (((x)>>16)&0xff), (((x)>>24)&0xff)
- #define GLUE_INLINE_LOOPS
- static const unsigned char GLUE_LOOP_LOADCNT[]={
- 0xDB, 0x1E,
- 0x8B, 0x0E,
- 0x81, 0xf9, 1,0,0,0,
- 0x0F, 0x8C, 0,0,0,0,
- };
- #if NSEEL_LOOPFUNC_SUPPORT_MAXLEN > 0
- #define GLUE_LOOP_CLAMPCNT_SIZE sizeof(GLUE_LOOP_CLAMPCNT)
- static const unsigned char GLUE_LOOP_CLAMPCNT[]={
- 0x81, 0xf9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN),
- 0x0F, 0x8C, 5,0,0,0,
- 0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN),
- };
- #else
- #define GLUE_LOOP_CLAMPCNT_SIZE 0
- #define GLUE_LOOP_CLAMPCNT ""
- #endif
- #define GLUE_LOOP_BEGIN_SIZE sizeof(GLUE_LOOP_BEGIN)
- static const unsigned char GLUE_LOOP_BEGIN[]={
- 0x56,
- 0x51,
- 0x81, 0xEC, 0x08, 0,0,0,
- };
- static const unsigned char GLUE_LOOP_END[]={
- 0x81, 0xC4, 0x08, 0,0,0,
- 0x59,
- 0x5E,
- 0x49,
- 0x0f, 0x85, 0,0,0,0,
- };
- #if NSEEL_LOOPFUNC_SUPPORT_MAXLEN > 0
- #define GLUE_WHILE_SETUP_SIZE sizeof(GLUE_WHILE_SETUP)
- static const unsigned char GLUE_WHILE_SETUP[]={
- 0xB9, INT_TO_LECHARS(NSEEL_LOOPFUNC_SUPPORT_MAXLEN),
- };
- static const unsigned char GLUE_WHILE_BEGIN[]={
- 0x56,
- 0x51,
- 0x81, 0xEC, 0x08, 0,0,0,
- };
- static const unsigned char GLUE_WHILE_END[]={
- 0x81, 0xC4, 0x08, 0,0,0,
- 0x59,
- 0x5E,
- 0x49,
- 0x0f, 0x84, 0,0,0,0,
- };
- #else
- #define GLUE_WHILE_SETUP_SIZE 0
- #define GLUE_WHILE_SETUP ""
- #define GLUE_WHILE_END_NOJUMP
- static const unsigned char GLUE_WHILE_BEGIN[]={
- 0x56,
- 0x81, 0xEC, 12, 0,0,0,
- };
- static const unsigned char GLUE_WHILE_END[]={
- 0x81, 0xC4, 12, 0,0,0,
- 0x5E,
- };
- #endif
- static const unsigned char GLUE_WHILE_CHECK_RV[] = {
- 0x85, 0xC0,
- 0x0F, 0x85, 0,0,0,0
- };
- static const unsigned char GLUE_SET_P1_Z[] = { 0x29, 0xC0 };
- static const unsigned char GLUE_SET_P1_NZ[] = { 0xb0, 0x01 };
- #define GLUE_HAS_FXCH
- static const unsigned char GLUE_FXCH[] = {0xd9, 0xc9};
- #define GLUE_HAS_FLDZ
- static const unsigned char GLUE_FLDZ[] = {0xd9, 0xee};
- #define GLUE_HAS_FLD1
- static const unsigned char GLUE_FLD1[] = {0xd9, 0xe8};
- static EEL_F negativezeropointfive=-0.5f;
- static EEL_F onepointfive=1.5f;
- #define GLUE_INVSQRT_NEEDREPL &negativezeropointfive, &onepointfive,
- #define GLUE_HAS_NATIVE_TRIGSQRTLOG
- static void *GLUE_realAddress(void *fn, void *fn_e, int *size)
- {
- static const unsigned char sig[12] = { 0x89, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 };
- unsigned char *p = (unsigned char *)fn;
- #if defined(_DEBUG) && defined(_MSC_VER)
- if (*p == 0xE9)
- {
- p += 5 + *(int *)(p+1);
- }
- #endif
- while (memcmp(p,sig,sizeof(sig))) p++;
- p+=sizeof(sig);
- fn = p;
- while (memcmp(p,sig,sizeof(sig))) p++;
- *size = p - (unsigned char *)fn;
- return fn;
- }
- #endif
|