Back to index

lightning-sunbird  0.9+nobinonly
xptcinvoke_x86_64_linux.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 // Platform specific code to invoke XPCOM methods on native objects
00040 
00041 #include "xptcprivate.h"
00042 
00043 // 6 integral parameters are passed in registers
00044 const PRUint32 GPR_COUNT = 6;
00045 
00046 // 8 floating point parameters are passed in SSE registers
00047 const PRUint32 FPR_COUNT = 8;
00048 
00049 // Remember that these 'words' are 64-bit long
00050 static inline void
00051 invoke_count_words(PRUint32 paramCount, nsXPTCVariant * s,
00052                    PRUint32 & nr_gpr, PRUint32 & nr_fpr, PRUint32 & nr_stack)
00053 {
00054     nr_gpr = 1; // skip one GP register for 'that'
00055     nr_fpr = 0;
00056     nr_stack = 0;
00057 
00058     /* Compute number of eightbytes of class MEMORY.  */
00059     for (uint32 i = 0; i < paramCount; i++, s++) {
00060         if (!s->IsPtrData()
00061             && (s->type == nsXPTType::T_FLOAT || s->type == nsXPTType::T_DOUBLE)) {
00062             if (nr_fpr < FPR_COUNT)
00063                 nr_fpr++;
00064             else
00065                 nr_stack++;
00066         }
00067         else {
00068             if (nr_gpr < GPR_COUNT)
00069                 nr_gpr++;
00070             else
00071                 nr_stack++;
00072         }
00073     }
00074 }
00075 
00076 static void
00077 invoke_copy_to_stack(PRUint64 * d, PRUint32 paramCount, nsXPTCVariant * s,
00078                      PRUint64 * gpregs, double * fpregs)
00079 {
00080     PRUint32 nr_gpr = 1; // skip one GP register for 'that'
00081     PRUint32 nr_fpr = 0;
00082     PRUint64 value;
00083 
00084     for (uint32 i = 0; i < paramCount; i++, s++) {
00085         if (s->IsPtrData())
00086             value = (PRUint64) s->ptr;
00087         else {
00088             switch (s->type) {
00089             case nsXPTType::T_FLOAT:                                break;
00090             case nsXPTType::T_DOUBLE:                               break;
00091             case nsXPTType::T_I8:     value = s->val.i8;            break;
00092             case nsXPTType::T_I16:    value = s->val.i16;           break;
00093             case nsXPTType::T_I32:    value = s->val.i32;           break;
00094             case nsXPTType::T_I64:    value = s->val.i64;           break;
00095             case nsXPTType::T_U8:     value = s->val.u8;            break;
00096             case nsXPTType::T_U16:    value = s->val.u16;           break;
00097             case nsXPTType::T_U32:    value = s->val.u32;           break;
00098             case nsXPTType::T_U64:    value = s->val.u64;           break;
00099             case nsXPTType::T_BOOL:   value = s->val.b;             break;
00100             case nsXPTType::T_CHAR:   value = s->val.c;             break;
00101             case nsXPTType::T_WCHAR:  value = s->val.wc;            break;
00102             default:                  value = (PRUint64) s->val.p;  break;
00103             }
00104         }
00105 
00106         if (!s->IsPtrData() && s->type == nsXPTType::T_DOUBLE) {
00107             if (nr_fpr < FPR_COUNT)
00108                 fpregs[nr_fpr++] = s->val.d;
00109             else {
00110                 *((double *)d) = s->val.d;
00111                 d++;
00112             }
00113         }
00114         else if (!s->IsPtrData() && s->type == nsXPTType::T_FLOAT) {
00115             if (nr_fpr < FPR_COUNT)
00116                 // The value in %xmm register is already prepared to
00117                 // be retrieved as a float. Therefore, we pass the
00118                 // value verbatim, as a double without conversion.
00119                 fpregs[nr_fpr++] = s->val.d;
00120             else {
00121                 *((float *)d) = s->val.f;
00122                 d++;
00123             }
00124         }
00125         else {
00126             if (nr_gpr < GPR_COUNT)
00127                 gpregs[nr_gpr++] = value;
00128             else
00129                 *d++ = value;
00130         }
00131     }
00132 }
00133 
00134 extern "C"
00135 XPTC_PUBLIC_API(nsresult)
00136 XPTC_InvokeByIndex(nsISupports * that, PRUint32 methodIndex,
00137                    PRUint32 paramCount, nsXPTCVariant * params)
00138 {
00139     PRUint32 nr_gpr, nr_fpr, nr_stack;
00140     invoke_count_words(paramCount, params, nr_gpr, nr_fpr, nr_stack);
00141     
00142     // Stack, if used, must be 16-bytes aligned
00143     if (nr_stack)
00144         nr_stack = (nr_stack + 1) & ~1;
00145 
00146     // Load parameters to stack, if necessary
00147     PRUint64 *stack = (PRUint64 *) __builtin_alloca(nr_stack * 8);
00148     PRUint64 gpregs[GPR_COUNT];
00149     double fpregs[FPR_COUNT];
00150     invoke_copy_to_stack(stack, paramCount, params, gpregs, fpregs);
00151 
00152     // Load FPR registers from fpregs[]
00153     register double d0 asm("xmm0");
00154     register double d1 asm("xmm1");
00155     register double d2 asm("xmm2");
00156     register double d3 asm("xmm3");
00157     register double d4 asm("xmm4");
00158     register double d5 asm("xmm5");
00159     register double d6 asm("xmm6");
00160     register double d7 asm("xmm7");
00161 
00162     switch (nr_fpr) {
00163 #define ARG_FPR(N) \
00164     case N+1: d##N = fpregs[N];
00165         ARG_FPR(7);
00166         ARG_FPR(6);
00167         ARG_FPR(5);
00168         ARG_FPR(4);
00169         ARG_FPR(3);
00170         ARG_FPR(2);
00171         ARG_FPR(1);
00172         ARG_FPR(0);
00173     case 0:;
00174 #undef ARG_FPR
00175     }
00176     
00177     // Load GPR registers from gpregs[]
00178     register PRUint64 a0 asm("rdi");
00179     register PRUint64 a1 asm("rsi");
00180     register PRUint64 a2 asm("rdx");
00181     register PRUint64 a3 asm("rcx");
00182     register PRUint64 a4 asm("r8");
00183     register PRUint64 a5 asm("r9");
00184     
00185     switch (nr_gpr) {
00186 #define ARG_GPR(N) \
00187     case N+1: a##N = gpregs[N];
00188         ARG_GPR(5);
00189         ARG_GPR(4);
00190         ARG_GPR(3);
00191         ARG_GPR(2);
00192         ARG_GPR(1);
00193     case 1: a0 = (PRUint64) that;
00194     case 0:;
00195 #undef ARG_GPR
00196     }
00197 
00198     // Ensure that assignments to SSE registers won't be optimized away
00199     asm("" ::
00200         "x" (d0), "x" (d1), "x" (d2), "x" (d3),
00201         "x" (d4), "x" (d5), "x" (d6), "x" (d7));
00202     
00203     // Get pointer to method
00204     PRUint64 methodAddress = *((PRUint64 *)that);
00205     methodAddress += 8 * methodIndex;
00206     methodAddress = *((PRUint64 *)methodAddress);
00207     
00208     typedef PRUint32 (*Method)(PRUint64, PRUint64, PRUint64, PRUint64, PRUint64, PRUint64);
00209     PRUint32 result = ((Method)methodAddress)(a0, a1, a2, a3, a4, a5);
00210     return result;
00211 }