1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/reflect/xptcall/src/md/unix/xptcstubs_x86_64_darwin.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,190 @@ 1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +// Implement shared vtbl methods. 1.11 + 1.12 +// Keep this in sync with the linux version. 1.13 + 1.14 +#include "xptcprivate.h" 1.15 +#include "xptiprivate.h" 1.16 + 1.17 +// The Darwin/x86-64 ABI passes the first 6 integer parameters and the 1.18 +// first 8 floating point parameters in registers (rdi, rsi, rdx, rcx, 1.19 +// r8, r9 and xmm0-xmm7), no stack space is allocated for these by the 1.20 +// caller. The rest of the parameters are passed in the callers stack 1.21 +// area. 1.22 + 1.23 +const uint32_t PARAM_BUFFER_COUNT = 16; 1.24 +const uint32_t GPR_COUNT = 6; 1.25 +const uint32_t FPR_COUNT = 8; 1.26 + 1.27 +// PrepareAndDispatch() is called by SharedStub() and calls the actual method. 1.28 +// 1.29 +// - 'args[]' contains the arguments passed on stack 1.30 +// - 'gpregs[]' contains the arguments passed in integer registers 1.31 +// - 'fpregs[]' contains the arguments passed in floating point registers 1.32 +// 1.33 +// The parameters are mapped into an array of type 'nsXPTCMiniVariant' 1.34 +// and then the method gets called. 1.35 + 1.36 +extern "C" nsresult ATTRIBUTE_USED 1.37 +PrepareAndDispatch(nsXPTCStubBase * self, uint32_t methodIndex, 1.38 + uint64_t * args, uint64_t * gpregs, double *fpregs) 1.39 +{ 1.40 + nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT]; 1.41 + nsXPTCMiniVariant* dispatchParams = nullptr; 1.42 + const nsXPTMethodInfo* info; 1.43 + uint32_t paramCount; 1.44 + uint32_t i; 1.45 + nsresult result = NS_ERROR_FAILURE; 1.46 + 1.47 + NS_ASSERTION(self,"no self"); 1.48 + 1.49 + self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info); 1.50 + NS_ASSERTION(info,"no method info"); 1.51 + if (!info) 1.52 + return NS_ERROR_UNEXPECTED; 1.53 + 1.54 + paramCount = info->GetParamCount(); 1.55 + 1.56 + // setup variant array pointer 1.57 + if (paramCount > PARAM_BUFFER_COUNT) 1.58 + dispatchParams = new nsXPTCMiniVariant[paramCount]; 1.59 + else 1.60 + dispatchParams = paramBuffer; 1.61 + 1.62 + NS_ASSERTION(dispatchParams,"no place for params"); 1.63 + if (!dispatchParams) 1.64 + return NS_ERROR_OUT_OF_MEMORY; 1.65 + 1.66 + uint64_t* ap = args; 1.67 + uint32_t nr_gpr = 1; // skip one GPR register for 'that' 1.68 + uint32_t nr_fpr = 0; 1.69 + uint64_t value; 1.70 + 1.71 + for (i = 0; i < paramCount; i++) { 1.72 + const nsXPTParamInfo& param = info->GetParam(i); 1.73 + const nsXPTType& type = param.GetType(); 1.74 + nsXPTCMiniVariant* dp = &dispatchParams[i]; 1.75 + 1.76 + if (!param.IsOut() && type == nsXPTType::T_DOUBLE) { 1.77 + if (nr_fpr < FPR_COUNT) 1.78 + dp->val.d = fpregs[nr_fpr++]; 1.79 + else 1.80 + dp->val.d = *(double*) ap++; 1.81 + continue; 1.82 + } 1.83 + else if (!param.IsOut() && type == nsXPTType::T_FLOAT) { 1.84 + if (nr_fpr < FPR_COUNT) 1.85 + // The value in %xmm register is already prepared to 1.86 + // be retrieved as a float. Therefore, we pass the 1.87 + // value verbatim, as a double without conversion. 1.88 + dp->val.d = fpregs[nr_fpr++]; 1.89 + else 1.90 + dp->val.f = *(float*) ap++; 1.91 + continue; 1.92 + } 1.93 + else { 1.94 + if (nr_gpr < GPR_COUNT) 1.95 + value = gpregs[nr_gpr++]; 1.96 + else 1.97 + value = *ap++; 1.98 + } 1.99 + 1.100 + if (param.IsOut() || !type.IsArithmetic()) { 1.101 + dp->val.p = (void*) value; 1.102 + continue; 1.103 + } 1.104 + 1.105 + switch (type) { 1.106 + case nsXPTType::T_I8: dp->val.i8 = (int8_t) value; break; 1.107 + case nsXPTType::T_I16: dp->val.i16 = (int16_t) value; break; 1.108 + case nsXPTType::T_I32: dp->val.i32 = (int32_t) value; break; 1.109 + case nsXPTType::T_I64: dp->val.i64 = (int64_t) value; break; 1.110 + case nsXPTType::T_U8: dp->val.u8 = (uint8_t) value; break; 1.111 + case nsXPTType::T_U16: dp->val.u16 = (uint16_t) value; break; 1.112 + case nsXPTType::T_U32: dp->val.u32 = (uint32_t) value; break; 1.113 + case nsXPTType::T_U64: dp->val.u64 = (uint64_t) value; break; 1.114 + // Cast to uint8_t first, to remove garbage on upper 56 bits. 1.115 + case nsXPTType::T_BOOL: dp->val.b = (bool)(uint8_t) value; break; 1.116 + case nsXPTType::T_CHAR: dp->val.c = (char) value; break; 1.117 + case nsXPTType::T_WCHAR: dp->val.wc = (wchar_t) value; break; 1.118 + 1.119 + default: 1.120 + NS_ERROR("bad type"); 1.121 + break; 1.122 + } 1.123 + } 1.124 + 1.125 + result = self->mOuter->CallMethod((uint16_t) methodIndex, info, dispatchParams); 1.126 + 1.127 + if (dispatchParams != paramBuffer) 1.128 + delete [] dispatchParams; 1.129 + 1.130 + return result; 1.131 +} 1.132 + 1.133 +// Darwin/x86-64 uses gcc >= 4.2 1.134 + 1.135 +#define STUB_ENTRY(n) \ 1.136 +asm(".section __TEXT,__text\n\t" \ 1.137 + ".align 3\n\t" \ 1.138 + ".if " #n " < 10\n\t" \ 1.139 + ".globl __ZN14nsXPTCStubBase5Stub" #n "Ev\n\t" \ 1.140 + "__ZN14nsXPTCStubBase5Stub" #n "Ev:\n\t" \ 1.141 + ".elseif " #n " < 100\n\t" \ 1.142 + ".globl __ZN14nsXPTCStubBase6Stub" #n "Ev\n\t" \ 1.143 + "__ZN14nsXPTCStubBase6Stub" #n "Ev:\n\t" \ 1.144 + ".elseif " #n " < 1000\n\t" \ 1.145 + ".globl __ZN14nsXPTCStubBase7Stub" #n "Ev\n\t" \ 1.146 + "__ZN14nsXPTCStubBase7Stub" #n "Ev:\n\t" \ 1.147 + ".else\n\t" \ 1.148 + ".err \"stub number " #n " >= 1000 not yet supported\"\n\t" \ 1.149 + ".endif\n\t" \ 1.150 + "movl $" #n ", %eax\n\t" \ 1.151 + "jmp SharedStub\n\t"); 1.152 + 1.153 +// static nsresult SharedStub(uint32_t methodIndex) 1.154 +asm(".section __TEXT,__text\n\t" 1.155 + ".align 3\n\t" 1.156 + "SharedStub:\n\t" 1.157 + // make room for gpregs (48), fpregs (64) 1.158 + "pushq %rbp\n\t" 1.159 + "movq %rsp,%rbp\n\t" 1.160 + "subq $112,%rsp\n\t" 1.161 + // save GP registers 1.162 + "movq %rdi,-112(%rbp)\n\t" 1.163 + "movq %rsi,-104(%rbp)\n\t" 1.164 + "movq %rdx, -96(%rbp)\n\t" 1.165 + "movq %rcx, -88(%rbp)\n\t" 1.166 + "movq %r8 , -80(%rbp)\n\t" 1.167 + "movq %r9 , -72(%rbp)\n\t" 1.168 + "leaq -112(%rbp),%rcx\n\t" 1.169 + // save FP registers 1.170 + "movsd %xmm0,-64(%rbp)\n\t" 1.171 + "movsd %xmm1,-56(%rbp)\n\t" 1.172 + "movsd %xmm2,-48(%rbp)\n\t" 1.173 + "movsd %xmm3,-40(%rbp)\n\t" 1.174 + "movsd %xmm4,-32(%rbp)\n\t" 1.175 + "movsd %xmm5,-24(%rbp)\n\t" 1.176 + "movsd %xmm6,-16(%rbp)\n\t" 1.177 + "movsd %xmm7, -8(%rbp)\n\t" 1.178 + "leaq -64(%rbp),%r8\n\t" 1.179 + // rdi has the 'self' pointer already 1.180 + "movl %eax,%esi\n\t" 1.181 + "leaq 16(%rbp),%rdx\n\t" 1.182 + "call _PrepareAndDispatch\n\t" 1.183 + "leave\n\t" 1.184 + "ret\n\t"); 1.185 + 1.186 +#define SENTINEL_ENTRY(n) \ 1.187 +nsresult nsXPTCStubBase::Sentinel##n() \ 1.188 +{ \ 1.189 + NS_ERROR("nsXPTCStubBase::Sentinel called"); \ 1.190 + return NS_ERROR_NOT_IMPLEMENTED; \ 1.191 +} 1.192 + 1.193 +#include "xptcstubsdef.inc"