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: #ifdef __GNUC__ michael@0: /* This tells gcc3.4+ not to optimize away symbols. michael@0: * @see http://gcc.gnu.org/gcc-3.4/changes.html michael@0: */ michael@0: #define DONT_DROP_OR_WARN __attribute__((used)) michael@0: #else michael@0: /* This tells older gccs not to warn about unused vairables. michael@0: * @see http://docs.freebsd.org/info/gcc/gcc.info.Variable_Attributes.html michael@0: */ michael@0: #define DONT_DROP_OR_WARN __attribute__((unused)) michael@0: #endif michael@0: michael@0: /* Specify explicitly a symbol for this function, don't try to guess the c++ mangled symbol. */ michael@0: static nsresult PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, uint32_t* args) asm("_PrepareAndDispatch") michael@0: DONT_DROP_OR_WARN; michael@0: michael@0: static nsresult ATTRIBUTE_USED michael@0: PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, uint32_t* args) 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; michael@0: uint8_t paramCount; michael@0: uint8_t i; michael@0: nsresult result = NS_ERROR_FAILURE; michael@0: michael@0: NS_ASSERTION(self,"no self"); michael@0: michael@0: self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info); 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: michael@0: NS_ASSERTION(dispatchParams,"no place for params"); michael@0: if (!dispatchParams) michael@0: return NS_ERROR_OUT_OF_MEMORY; 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: 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: /* michael@0: * This is our shared stub. michael@0: * michael@0: * r0 = Self. michael@0: * michael@0: * The Rules: michael@0: * We pass an (undefined) number of arguments into this function. michael@0: * The first 3 C++ arguments are in r1 - r3, the rest are built michael@0: * by the calling function on the stack. michael@0: * michael@0: * We are allowed to corrupt r0 - r3, ip, and lr. michael@0: * michael@0: * Other Info: michael@0: * We pass the stub number in using `ip'. michael@0: * michael@0: * Implementation: michael@0: * - We save r1 to r3 inclusive onto the stack, which will be michael@0: * immediately below the caller saved arguments. michael@0: * - setup r2 (PrepareAndDispatch's args pointer) to point at michael@0: * the base of all these arguments michael@0: * - Save LR (for the return address) michael@0: * - Set r1 (PrepareAndDispatch's methodindex argument) from ip michael@0: * - r0 is passed through (self) michael@0: * - Call PrepareAndDispatch michael@0: * - When the call returns, we return by loading the PC off the michael@0: * stack, and undoing the stack (one instruction)! michael@0: * michael@0: */ michael@0: __asm__ ("\n\ michael@0: .text \n\ michael@0: .align 2 \n\ michael@0: SharedStub: \n\ michael@0: stmfd sp!, {r1, r2, r3} \n\ michael@0: mov r2, sp \n\ michael@0: str lr, [sp, #-4]! \n\ michael@0: mov r1, ip \n\ michael@0: bl _PrepareAndDispatch \n\ michael@0: ldr pc, [sp], #16"); michael@0: michael@0: /* michael@0: * Create sets of stubs to call the SharedStub. michael@0: * We don't touch the stack here, nor any registers, other than IP. michael@0: * IP is defined to be corruptable by a called function, so we are michael@0: * safe to use it. michael@0: * michael@0: * This will work with or without optimisation. michael@0: */ michael@0: michael@0: /* michael@0: * Note : As G++3 ABI contains the length of the functionname in the michael@0: * mangled name, it is difficult to get a generic assembler mechanism like michael@0: * in the G++ 2.95 case. michael@0: * Create names would be like : michael@0: * _ZN14nsXPTCStubBase5Stub9Ev michael@0: * _ZN14nsXPTCStubBase6Stub13Ev michael@0: * _ZN14nsXPTCStubBase7Stub144Ev michael@0: * Use the assembler directives to get the names right... michael@0: */ michael@0: michael@0: #define STUB_ENTRY(n) \ michael@0: __asm__( \ michael@0: ".section \".text\"\n" \ michael@0: " .align 2\n" \ michael@0: " .iflt ("#n" - 10)\n" \ michael@0: " .globl _ZN14nsXPTCStubBase5Stub"#n"Ev\n" \ michael@0: " .type _ZN14nsXPTCStubBase5Stub"#n"Ev,#function\n" \ michael@0: "_ZN14nsXPTCStubBase5Stub"#n"Ev:\n" \ michael@0: " .else\n" \ michael@0: " .iflt ("#n" - 100)\n" \ michael@0: " .globl _ZN14nsXPTCStubBase6Stub"#n"Ev\n" \ michael@0: " .type _ZN14nsXPTCStubBase6Stub"#n"Ev,#function\n" \ michael@0: "_ZN14nsXPTCStubBase6Stub"#n"Ev:\n" \ michael@0: " .else\n" \ michael@0: " .iflt ("#n" - 1000)\n" \ michael@0: " .globl _ZN14nsXPTCStubBase7Stub"#n"Ev\n" \ michael@0: " .type _ZN14nsXPTCStubBase7Stub"#n"Ev,#function\n" \ michael@0: "_ZN14nsXPTCStubBase7Stub"#n"Ev:\n" \ michael@0: " .else\n" \ michael@0: " .err \"stub number "#n"> 1000 not yet supported\"\n" \ michael@0: " .endif\n" \ michael@0: " .endif\n" \ michael@0: " .endif\n" \ michael@0: " mov ip, #"#n"\n" \ michael@0: " b SharedStub\n\t"); michael@0: michael@0: #if 0 michael@0: /* michael@0: * This part is left in as comment : this is how the method definition michael@0: * should look like. michael@0: */ michael@0: michael@0: #define STUB_ENTRY(n) \ michael@0: nsresult nsXPTCStubBase::Stub##n () \ michael@0: { \ michael@0: __asm__ ( \ michael@0: " mov ip, #"#n"\n" \ michael@0: " b SharedStub\n\t"); \ michael@0: return 0; /* avoid warnings */ \ michael@0: } michael@0: #endif michael@0: michael@0: michael@0: #define SENTINEL_ENTRY(n) \ michael@0: nsresult 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: #include "xptcstubsdef.inc"