Back to index

lightning-sunbird  0.9+nobinonly
xptcstubs_openbsd_amd64.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 // Implement shared vtbl methods.
00040 
00041 #include "xptcprivate.h"
00042 
00043 // The OpenBSD/amd64 ABI passes the first 6 integer parameters and the
00044 // first 8 floating point parameters in registers (rdi, rsi, rdx, rcx,
00045 // r8, r9 and xmm0-xmm7), no stack space is allocated for these by the
00046 // caller.  The rest of the parameters are passed in the callers stack
00047 // area.
00048 
00049 const PRUint32 PARAM_BUFFER_COUNT   = 16;
00050 const PRUint32 GPR_COUNT            = 6;
00051 const PRUint32 FPR_COUNT            = 8;
00052 
00053 // PrepareAndDispatch() is called by SharedStub() and calls the actual method.
00054 //
00055 // - 'args[]' contains the arguments passed on stack
00056 // - 'gpregs[]' contains the arguments passed in integer registers
00057 // - 'fpregs[]' contains the arguments passed in floating point registers
00058 // 
00059 // The parameters are mapped into an array of type 'nsXPTCMiniVariant'
00060 // and then the method gets called.
00061 
00062 extern "C" nsresult
00063 PrepareAndDispatch(nsXPTCStubBase * self, PRUint32 methodIndex,
00064                    PRUint64 * args, PRUint64 * gpregs, double *fpregs)
00065 {
00066     nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT];
00067     nsXPTCMiniVariant* dispatchParams = NULL;
00068     nsIInterfaceInfo* iface_info = NULL;
00069     const nsXPTMethodInfo* info;
00070     PRUint32 paramCount;
00071     PRUint32 i;
00072     nsresult result = NS_ERROR_FAILURE;
00073 
00074     NS_ASSERTION(self,"no self");
00075 
00076     self->GetInterfaceInfo(&iface_info);
00077     NS_ASSERTION(iface_info,"no interface info");
00078     if (!iface_info)
00079         return NS_ERROR_UNEXPECTED;
00080 
00081     iface_info->GetMethodInfo(PRUint16(methodIndex), &info);
00082     NS_ASSERTION(info,"no method info");
00083     if (!info)
00084         return NS_ERROR_UNEXPECTED;
00085 
00086     paramCount = info->GetParamCount();
00087 
00088     // setup variant array pointer
00089     if (paramCount > PARAM_BUFFER_COUNT)
00090         dispatchParams = new nsXPTCMiniVariant[paramCount];
00091     else
00092         dispatchParams = paramBuffer;
00093 
00094     NS_ASSERTION(dispatchParams,"no place for params");
00095     if (!dispatchParams)
00096         return NS_ERROR_OUT_OF_MEMORY;
00097 
00098     PRUint64* ap = args;
00099     PRUint32 nr_gpr = 1;    // skip one GPR register for 'that'
00100     PRUint32 nr_fpr = 0;
00101     PRUint64 value;
00102 
00103     for (i = 0; i < paramCount; i++) {
00104         const nsXPTParamInfo& param = info->GetParam(i);
00105         const nsXPTType& type = param.GetType();
00106         nsXPTCMiniVariant* dp = &dispatchParams[i];
00107        
00108         if (!param.IsOut() && type == nsXPTType::T_DOUBLE) {
00109             if (nr_fpr < FPR_COUNT)
00110                 dp->val.d = fpregs[nr_fpr++];
00111             else
00112                 dp->val.d = *(double*) ap++;
00113             continue;
00114         }
00115         else if (!param.IsOut() && type == nsXPTType::T_FLOAT) {
00116             if (nr_fpr < FPR_COUNT)
00117                 // The value in %xmm register is already prepared to
00118                 // be retrieved as a float. Therefore, we pass the
00119                 // value verbatim, as a double without conversion.
00120                 dp->val.d = *(double*) ap++;
00121             else
00122                 dp->val.f = *(float*) ap++;
00123             continue;
00124         }
00125         else {
00126             if (nr_gpr < GPR_COUNT)
00127                 value = gpregs[nr_gpr++];
00128             else
00129                 value = *ap++;
00130         }
00131 
00132         if (param.IsOut() || !type.IsArithmetic()) {
00133             dp->val.p = (void*) value;
00134             continue;
00135         }
00136 
00137         switch (type) {
00138         case nsXPTType::T_I8:      dp->val.i8  = (PRInt8)   value; break;
00139         case nsXPTType::T_I16:     dp->val.i16 = (PRInt16)  value; break;
00140         case nsXPTType::T_I32:     dp->val.i32 = (PRInt32)  value; break;
00141         case nsXPTType::T_I64:     dp->val.i64 = (PRInt64)  value; break;
00142         case nsXPTType::T_U8:      dp->val.u8  = (PRUint8)  value; break;
00143         case nsXPTType::T_U16:     dp->val.u16 = (PRUint16) value; break;
00144         case nsXPTType::T_U32:     dp->val.u32 = (PRUint32) value; break;
00145         case nsXPTType::T_U64:     dp->val.u64 = (PRUint64) value; break;
00146         case nsXPTType::T_BOOL:    dp->val.b   = (PRBool)   value; break;
00147         case nsXPTType::T_CHAR:    dp->val.c   = (char)     value; break;
00148         case nsXPTType::T_WCHAR:   dp->val.wc  = (wchar_t)  value; break;
00149 
00150         default:
00151             NS_ASSERTION(0, "bad type");
00152             break;
00153         }
00154     }
00155 
00156     result = self->CallMethod((PRUint16) methodIndex, info, dispatchParams);
00157 
00158     NS_RELEASE(iface_info);
00159 
00160     if (dispatchParams != paramBuffer)
00161         delete [] dispatchParams;
00162 
00163     return result;
00164 }
00165 
00166 #if defined(__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100 /* G++ V3 ABI */
00167 // OpenBSD/amd64 uses gcc >= 3.1
00168 #define STUB_ENTRY(n) \
00169 asm(".section \".text\"\n\t" \
00170     ".align   2\n\t" \
00171     ".if      " #n " < 10\n\t" \
00172     ".globl   _ZN14nsXPTCStubBase5Stub" #n "Ev\n\t" \
00173     ".type    _ZN14nsXPTCStubBase5Stub" #n "Ev,@function\n" \
00174     "_ZN14nsXPTCStubBase5Stub" #n "Ev:\n\t" \
00175     ".elseif  " #n " < 100\n\t" \
00176     ".globl   _ZN14nsXPTCStubBase6Stub" #n "Ev\n\t" \
00177     ".type    _ZN14nsXPTCStubBase6Stub" #n "Ev,@function\n" \
00178     "_ZN14nsXPTCStubBase6Stub" #n "Ev:\n\t" \
00179     ".elseif    " #n " < 1000\n\t" \
00180     ".globl     _ZN14nsXPTCStubBase7Stub" #n "Ev\n\t" \
00181     ".type      _ZN14nsXPTCStubBase7Stub" #n "Ev,@function\n" \
00182     "_ZN14nsXPTCStubBase7Stub" #n "Ev:\n\t" \
00183     ".else\n\t" \
00184     ".err     \"stub number " #n " >= 1000 not yet supported\"\n\t" \
00185     ".endif\n\t" \
00186     "movl     $" #n ", %eax\n\t" \
00187     "jmp      SharedStub\n\t" \
00188     ".if      " #n " < 10\n\t" \
00189     ".size    _ZN14nsXPTCStubBase5Stub" #n "Ev,.-_ZN14nsXPTCStubBase5Stub" #n "Ev\n\t" \
00190     ".elseif  " #n " < 100\n\t" \
00191     ".size    _ZN14nsXPTCStubBase6Stub" #n "Ev,.-_ZN14nsXPTCStubBase6Stub" #n "Ev\n\t" \
00192     ".else\n\t" \
00193     ".size    _ZN14nsXPTCStubBase7Stub" #n "Ev,.-_ZN14nsXPTCStubBase7Stub" #n "Ev\n\t" \
00194     ".endif");
00195 
00196 // static nsresult SharedStub(PRUint32 methodIndex)
00197 asm(".section   \".text\"\n\t"
00198     ".align     2\n\t"
00199     ".type      SharedStub,@function\n\t"
00200     "SharedStub:\n\t"
00201     // make room for gpregs (48), fpregs (64)
00202     "pushq      %rbp\n\t"
00203     "movq       %rsp,%rbp\n\t"
00204     "subq       $112,%rsp\n\t"
00205     // save GP registers
00206     "movq       %rdi,-112(%rbp)\n\t"
00207     "movq       %rsi,-104(%rbp)\n\t"
00208     "movq       %rdx, -96(%rbp)\n\t"
00209     "movq       %rcx, -88(%rbp)\n\t"
00210     "movq       %r8 , -80(%rbp)\n\t"
00211     "movq       %r9 , -72(%rbp)\n\t"
00212     "leaq       -112(%rbp),%rcx\n\t"
00213     // save FP registers
00214     "movsd      %xmm0,-64(%rbp)\n\t"
00215     "movsd      %xmm1,-56(%rbp)\n\t"
00216     "movsd      %xmm2,-48(%rbp)\n\t"
00217     "movsd      %xmm3,-40(%rbp)\n\t"
00218     "movsd      %xmm4,-32(%rbp)\n\t"
00219     "movsd      %xmm5,-24(%rbp)\n\t"
00220     "movsd      %xmm6,-16(%rbp)\n\t"
00221     "movsd      %xmm7, -8(%rbp)\n\t"
00222     "leaq       -64(%rbp),%r8\n\t"
00223     // rdi has the 'self' pointer already
00224     "movl       %eax,%esi\n\t"
00225     "leaq       16(%rbp),%rdx\n\t"
00226     "call       PrepareAndDispatch@plt\n\t"
00227     "leave\n\t"
00228     "ret\n\t"
00229     ".size      SharedStub,.-SharedStub");
00230 
00231 #define SENTINEL_ENTRY(n) \
00232 nsresult nsXPTCStubBase::Sentinel##n() \
00233 { \
00234     NS_ASSERTION(0,"nsXPTCStubBase::Sentinel called"); \
00235     return NS_ERROR_NOT_IMPLEMENTED; \
00236 }
00237 
00238 #include "xptcstubsdef.inc"
00239 
00240 #else
00241 #error "Unsupported compiler. Use gcc >= 3.1 for OpenBSD/amd64."
00242 #endif /* __GNUC__ */