michael@0: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Implement shared vtbl methods. */ michael@0: michael@0: #include "xptcprivate.h" michael@0: #include "xptiprivate.h" michael@0: michael@0: #ifndef WIN32 michael@0: #error "This code is for Win32 only" michael@0: #endif michael@0: michael@0: extern "C" { michael@0: michael@0: #ifndef __GNUC__ michael@0: static michael@0: #endif michael@0: nsresult __stdcall michael@0: PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, michael@0: uint32_t* args, uint32_t* stackBytesToPop) michael@0: { michael@0: #define PARAM_BUFFER_COUNT 16 michael@0: michael@0: nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT]; michael@0: nsXPTCMiniVariant* dispatchParams = nullptr; michael@0: const nsXPTMethodInfo* info = nullptr; michael@0: uint8_t paramCount; michael@0: uint8_t i; michael@0: nsresult result = NS_ERROR_FAILURE; michael@0: michael@0: // If anything fails before stackBytesToPop can be set then michael@0: // the failure is completely catastrophic! michael@0: michael@0: NS_ASSERTION(self,"no self"); michael@0: michael@0: self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info); michael@0: NS_ASSERTION(info,"no method info"); michael@0: michael@0: paramCount = info->GetParamCount(); michael@0: michael@0: // setup variant array pointer michael@0: if(paramCount > PARAM_BUFFER_COUNT) michael@0: dispatchParams = new nsXPTCMiniVariant[paramCount]; michael@0: else michael@0: dispatchParams = paramBuffer; michael@0: NS_ASSERTION(dispatchParams,"no place for params"); michael@0: michael@0: uint32_t* ap = args; michael@0: for(i = 0; i < paramCount; i++, ap++) michael@0: { michael@0: const nsXPTParamInfo& param = info->GetParam(i); michael@0: const nsXPTType& type = param.GetType(); michael@0: nsXPTCMiniVariant* dp = &dispatchParams[i]; michael@0: michael@0: if(param.IsOut() || !type.IsArithmetic()) michael@0: { michael@0: dp->val.p = (void*) *ap; michael@0: continue; michael@0: } michael@0: // else michael@0: switch(type) michael@0: { michael@0: case nsXPTType::T_I8 : dp->val.i8 = *((int8_t*) ap); break; michael@0: case nsXPTType::T_I16 : dp->val.i16 = *((int16_t*) ap); break; michael@0: case nsXPTType::T_I32 : dp->val.i32 = *((int32_t*) ap); break; michael@0: case nsXPTType::T_I64 : dp->val.i64 = *((int64_t*) ap); ap++; break; michael@0: case nsXPTType::T_U8 : dp->val.u8 = *((uint8_t*) ap); break; michael@0: case nsXPTType::T_U16 : dp->val.u16 = *((uint16_t*)ap); break; michael@0: case nsXPTType::T_U32 : dp->val.u32 = *((uint32_t*)ap); break; michael@0: case nsXPTType::T_U64 : dp->val.u64 = *((uint64_t*)ap); ap++; break; michael@0: case nsXPTType::T_FLOAT : dp->val.f = *((float*) ap); break; michael@0: case nsXPTType::T_DOUBLE : dp->val.d = *((double*) ap); ap++; break; michael@0: case nsXPTType::T_BOOL : dp->val.b = *((bool*) ap); break; michael@0: case nsXPTType::T_CHAR : dp->val.c = *((char*) ap); break; michael@0: case nsXPTType::T_WCHAR : dp->val.wc = *((wchar_t*) ap); break; michael@0: default: michael@0: NS_ERROR("bad type"); michael@0: break; michael@0: } michael@0: } michael@0: *stackBytesToPop = ((uint32_t)ap) - ((uint32_t)args); michael@0: michael@0: result = self->mOuter->CallMethod((uint16_t)methodIndex, info, dispatchParams); michael@0: michael@0: if(dispatchParams != paramBuffer) michael@0: delete [] dispatchParams; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: } // extern "C" michael@0: michael@0: // declspec(naked) is broken in gcc michael@0: #ifndef __GNUC__ michael@0: static michael@0: __declspec(naked) michael@0: void SharedStub(void) michael@0: { michael@0: __asm { michael@0: push ebp // set up simple stack frame michael@0: mov ebp, esp // stack has: ebp/vtbl_index/retaddr/this/args michael@0: push ecx // make room for a ptr michael@0: lea eax, [ebp-4] // pointer to stackBytesToPop michael@0: push eax michael@0: lea eax, [ebp+12] // pointer to args michael@0: push eax michael@0: push ecx // vtbl_index michael@0: mov eax, [ebp+8] // this michael@0: push eax michael@0: call PrepareAndDispatch michael@0: mov edx, [ebp+4] // return address michael@0: mov ecx, [ebp-4] // stackBytesToPop michael@0: add ecx, 8 // for 'this' and return address michael@0: mov esp, ebp michael@0: pop ebp michael@0: add esp, ecx // fix up stack pointer michael@0: jmp edx // simulate __stdcall return michael@0: } michael@0: } michael@0: michael@0: // these macros get expanded (many times) in the file #included below michael@0: #define STUB_ENTRY(n) \ michael@0: __declspec(naked) nsresult __stdcall nsXPTCStubBase::Stub##n() \ michael@0: { __asm mov ecx, n __asm jmp SharedStub } michael@0: michael@0: #else michael@0: michael@0: asm(".text\n\t" michael@0: ".align 4\n\t" michael@0: "SharedStub:\n\t" michael@0: "push %ebp\n\t" michael@0: "mov %esp, %ebp\n\t" michael@0: "push %ecx\n\t" michael@0: "lea -4(%ebp), %eax\n\t" michael@0: "push %eax\n\t" michael@0: "lea 12(%ebp), %eax\n\t" michael@0: "push %eax\n\t" michael@0: "push %ecx\n\t" michael@0: "movl 8(%ebp), %eax\n\t" michael@0: "push %eax\n\t" michael@0: "call _PrepareAndDispatch@16\n\t" michael@0: "mov 4(%ebp), %edx\n\t" michael@0: "mov -4(%ebp), %ecx\n\t" michael@0: "add $8, %ecx\n\t" michael@0: "mov %ebp, %esp\n\t" michael@0: "pop %ebp\n\t" michael@0: "add %ecx, %esp\n\t" michael@0: "jmp *%edx" michael@0: ); michael@0: michael@0: #define STUB_ENTRY(n) \ michael@0: asm(".text\n\t" \ michael@0: ".align 4\n\t" \ michael@0: ".if " #n " < 10\n\t" \ michael@0: ".globl __ZN14nsXPTCStubBase5Stub" #n "Ev@4\n\t" \ michael@0: ".def __ZN14nsXPTCStubBase5Stub" #n "Ev@4; \n\t" \ michael@0: ".scl 3\n\t" \ michael@0: ".type 46\n\t" \ michael@0: ".endef\n\t" \ michael@0: "__ZN14nsXPTCStubBase5Stub" #n "Ev@4:\n\t" \ michael@0: ".elseif " #n " < 100\n\t" \ michael@0: ".globl __ZN14nsXPTCStubBase6Stub" #n "Ev@4\n\t" \ michael@0: ".def __ZN14nsXPTCStubBase6Stub" #n "Ev@4\n\t" \ michael@0: ".scl 3\n\t" \ michael@0: ".type 46\n\t" \ michael@0: ".endef\n\t" \ michael@0: "__ZN14nsXPTCStubBase6Stub" #n "Ev@4:\n\t" \ michael@0: ".elseif " #n " < 1000\n\t" \ michael@0: ".globl __ZN14nsXPTCStubBase7Stub" #n "Ev@4\n\t" \ michael@0: ".def __ZN14nsXPTCStubBase7Stub" #n "Ev@4\n\t" \ michael@0: ".scl 3\n\t" \ michael@0: ".type 46\n\t" \ michael@0: ".endef\n\t" \ michael@0: "__ZN14nsXPTCStubBase7Stub" #n "Ev@4:\n\t" \ michael@0: ".else\n\t" \ michael@0: ".err \"stub number " #n " >= 1000 not yet supported\"\n\t" \ michael@0: ".endif\n\t" \ michael@0: "mov $" #n ", %ecx\n\t" \ michael@0: "jmp SharedStub"); michael@0: michael@0: #endif /* __GNUC__ */ michael@0: michael@0: #define SENTINEL_ENTRY(n) \ michael@0: nsresult __stdcall nsXPTCStubBase::Sentinel##n() \ michael@0: { \ michael@0: NS_ERROR("nsXPTCStubBase::Sentinel called"); \ michael@0: return NS_ERROR_NOT_IMPLEMENTED; \ michael@0: } michael@0: michael@0: #ifdef _MSC_VER michael@0: #pragma warning(disable : 4035) // OK to have no return value michael@0: #endif michael@0: #include "xptcstubsdef.inc" michael@0: #ifdef _MSC_VER michael@0: #pragma warning(default : 4035) // restore default michael@0: #endif michael@0: michael@0: void michael@0: #ifdef __GNUC__ michael@0: __cdecl michael@0: #endif michael@0: xptc_dummy() michael@0: { michael@0: }