Skip to content

Commit

Permalink
Move call to GetProcAddress from externalcall.asm to extcall.cpp
Browse files Browse the repository at this point in the history
Another small reduction in assembler content for #531
  • Loading branch information
blairmcg committed Jul 21, 2020
1 parent 62d9029 commit 2c9dc1e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
51 changes: 9 additions & 42 deletions Core/DolphinVM/ExternalCall.asm
Expand Up @@ -54,6 +54,9 @@ extern CharacterGetCodePoint:near32
; We need to test the structure type specially
ArgSTRUCT EQU 50

getDllCallAddress EQU ?GetDllCallProcAddress@Interpreter@@CIP6GHXZPAUExternalMethodDescriptor@DolphinX@@PAV?$TOTE@VExternalLibrary@ST@@@@@Z
extern getDllCallAddress:near32

atoi PROTO C :DWORD
GetProcAddress PROTO STDCALL :HINSTANCE, :LPCSTR

Expand Down Expand Up @@ -255,7 +258,9 @@ performCall:
ret ; eax will be non-zero as otherwise we'd not be here

procAddressNotCached:
call getProcAddress ; Cache value 0, so lookup the proc address
neg edx
mov edx, [_SP+edx*OOPSIZE]
call getDllCallAddress ; Cache value 0, so lookup the proc address
test eax, eax ; Returns null if not a valid proc name
jnz performCall

Expand All @@ -267,46 +272,6 @@ procAddressNotCached:

asyncDLL32Call ENDP

getProcAddress PROC
ASSUME ecx:PTR CallDescriptor ; ecx points at descriptor bytes

push ebx ; Save EBX
mov ebx, ecx ; Save for later in safe register
ASSUME ebx:PTR CallDescriptor ; ebx now points at descriptor bytes
ASSUME ecx:NOTHING ; Now free to re-use ECX

; Get receiver from under args and extract the handle
mov eax, _SP
lea ecx, [edx*OOPSIZE] ; Offset of receiver down stack
sub eax, ecx ; eax now points at receiver in stack

; Get address of proc name in the descriptor into eax
; N.B. Arg Count cannot be greater than 255
mov dl, [ebx].m_argsLen
lea edx, DWORD PTR [ebx].m_args[edx]

mov eax, [eax] ; Load receiver Oop from stack
mov eax, (OTE PTR[eax]).m_location
mov eax, (ExternalLibrary PTR[eax]).m_handle ; Get handle Oop from receiver
mov eax, (OTE PTR[eax]).m_location

; Prepare for call to GetProcAddress
push edx ; Push address of proc name
push (ExternalHandle PTR[eax]).m_handle ; Push DLL handle for call to GetProcAddress
INVOKE atoi, edx
test eax, eax ; atoi() returned 0?
jz @F ; Yes, not an ordinal
mov [esp+4], eax ; Store down ordinal value instead

@@:
call GetProcAddress
mov [ebx].m_proc, eax ; Save down into cache
pop ebx ; restore EBX
ret ; Proc address in EAX
getProcAddress ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; A fastcall function (ecx is pMethod, edx is argCount)
BEGINPRIMITIVE primitiveDLL32Call
Expand Down Expand Up @@ -341,7 +306,9 @@ performCall:
ret

procAddressNotCached:
call getProcAddress ; Cache value 0, so lookup the proc address
neg edx
mov edx, [_SP+edx*OOPSIZE]
call getDllCallAddress ; Cache value 0, so lookup the proc address
test eax, eax ; Returns null if not a valid proc name
jnz performCall

Expand Down
5 changes: 3 additions & 2 deletions Core/DolphinVM/Interprt.h
Expand Up @@ -758,11 +758,12 @@ class Interpreter
private:

static BOOL __stdcall callExternalFunction(FARPROC pProc, argcount_t argCount, DolphinX::CallDescriptor* argTypes, BOOL isVirtual);

static FARPROC GetDllCallProcAddress(DolphinX::ExternalMethodDescriptor* descriptor, LibraryOTE* oteReceiver);

// Pushs object on stack instantiated from address, and returns size of object pushed
static void pushArgsAt(CallbackDescriptor* descriptor, argcount_t argCount, uint8_t* lpParms);
static argcount_t pushArgsAt(const ExternalDescriptor* descriptor, uint8_t* lpParms);

static void failTrace();

public:
Expand Down
9 changes: 9 additions & 0 deletions Core/DolphinVM/STExternal.h
Expand Up @@ -22,10 +22,12 @@ namespace ST
class ExternalStructure;
class ExternalAddress;
class ExternalHandle;
class ExternalLibrary;
}
typedef TOTE<ST::ExternalStructure> StructureOTE;
typedef TOTE<ST::ExternalAddress> AddressOTE;
typedef TOTE<ST::ExternalHandle> HandleOTE;
typedef TOTE<ST::ExternalLibrary> LibraryOTE;

struct COMThunk
{
Expand Down Expand Up @@ -105,6 +107,13 @@ namespace ST
OTE* m_literals[];
};

class ExternalLibrary : public Object
{
public:
HandleOTE* m_handle;
};


///////////////////////////////////////////////////////////////////////////////

// Answer either a SmallInteger or an ExternalHandle (if too big) to hold
Expand Down
16 changes: 16 additions & 0 deletions Core/DolphinVM/extcall.cpp
Expand Up @@ -218,6 +218,22 @@ BytesOTE* __fastcall NewGUID(GUID* rguid)
return ObjectMemory::newByteObject(Pointers.ClassGUID, sizeof(GUID), rguid);
}

FARPROC Interpreter::GetDllCallProcAddress(DolphinX::ExternalMethodDescriptor* descriptor, LibraryOTE* oteReceiver)
{
HMODULE hModule = static_cast<HMODULE>(oteReceiver->m_location->m_handle->m_location->m_handle);
LPCSTR procName = reinterpret_cast<LPCSTR>(descriptor->m_descriptor.m_args + descriptor->m_descriptor.m_argsLen);
int ordinal = atoi(procName);
if (ordinal != 0)
{
procName = reinterpret_cast<LPCSTR>(ordinal);
}

FARPROC proc = ::GetProcAddress(hModule, procName);
descriptor->m_proc = proc;

return proc;
}

///////////////////////////////////////////////////////////////////////////////
//
argcount_t Interpreter::pushArgsAt(const ExternalDescriptor* descriptor, uint8_t* lpParms)
Expand Down

0 comments on commit 2c9dc1e

Please sign in to comment.