Back to index

lightning-sunbird  0.9+nobinonly
invoke_test.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 
00040 typedef unsigned nsresult;
00041 typedef unsigned PRUint32;
00042 typedef unsigned nsXPCVariant;
00043 
00044 
00045 #if defined(WIN32)
00046 #define NS_IMETHOD virtual nsresult __stdcall
00047 #define NS_IMETHODIMP nsresult __stdcall
00048 #else
00049 #define NS_IMETHOD virtual nsresult
00050 #define NS_IMETHODIMP nsresult
00051 #endif
00052 
00053 
00054 class base{
00055 public:
00056   NS_IMETHOD ignored() = 0;
00057 };
00058 
00059 class foo : public base {
00060 public:
00061   NS_IMETHOD callme1(int i, int j) = 0;
00062   NS_IMETHOD callme2(int i, int j) = 0;
00063   NS_IMETHOD callme3(int i, int j) = 0;
00064 };
00065 
00066 class bar : public foo{
00067 public:
00068   NS_IMETHOD ignored();
00069   NS_IMETHOD callme1(int i, int j);
00070   NS_IMETHOD callme2(int i, int j);
00071   NS_IMETHOD callme3(int i, int j);
00072 };
00073 
00074 /*
00075 class baz : public base {
00076 public:
00077   NS_IMETHOD ignored();
00078   NS_IMETHOD callme1();
00079   NS_IMETHOD callme2();
00080   NS_IMETHOD callme3();
00081   void setfoo(foo* f) {other = f;}
00082 
00083   foo* other;
00084 };
00085 NS_IMETHODIMP baz::ignored(){return 0;}
00086 */
00087 
00088 NS_IMETHODIMP bar::ignored(){return 0;}
00089 
00090 NS_IMETHODIMP bar::callme1(int i, int j)
00091 {
00092   printf("called bar::callme1 with: %d %d\n", i, j);
00093   return 5;
00094 }
00095 
00096 NS_IMETHODIMP bar::callme2(int i, int j)
00097 {
00098   printf("called bar::callme2 with: %d %d\n", i, j);
00099   return 5;
00100 }
00101 
00102 NS_IMETHODIMP bar::callme3(int i, int j)
00103 {
00104   printf("called bar::callme3 with: %d %d\n", i, j);
00105   return 5;
00106 }
00107 
00108 void docall(foo* f, int i, int j){
00109   f->callme1(i, j); 
00110 }
00111 
00112 /***************************************************************************/
00113 #if defined(WIN32)
00114 
00115 static PRUint32 __stdcall
00116 invoke_count_words(PRUint32 paramCount, nsXPCVariant* s)
00117 {
00118     return paramCount;
00119 }    
00120 
00121 static void __stdcall
00122 invoke_copy_to_stack(PRUint32* d, PRUint32 paramCount, nsXPCVariant* s)
00123 {
00124     for(PRUint32 i = 0; i < paramCount; i++, d++, s++)
00125     {
00126         *((PRUint32*)d) = *((PRUint32*)s);
00127     }
00128 }
00129 
00130 static nsresult __stdcall
00131 DoInvoke(void* that, PRUint32 index,
00132          PRUint32 paramCount, nsXPCVariant* params)
00133 {
00134     __asm {
00135         push    params
00136         push    paramCount
00137         call    invoke_count_words  // stdcall, result in eax
00138         shl     eax,2               // *= 4
00139         sub     esp,eax             // make space for params
00140         mov     edx,esp
00141         push    params
00142         push    paramCount
00143         push    edx
00144         call    invoke_copy_to_stack // stdcall
00145         mov     ecx,that            // instance in ecx
00146         push    ecx                 // push this
00147         mov     edx,[ecx]           // vtable in edx
00148         mov     eax,index
00149         shl     eax,2               // *= 4
00150         add     edx,eax
00151         call    [edx]               // stdcall, i.e. callee cleans up stack.
00152     }
00153 }
00154 
00155 #else
00156 /***************************************************************************/
00157 // just Linux_x86 now. Add other later...
00158 
00159 static PRUint32 
00160 invoke_count_words(PRUint32 paramCount, nsXPCVariant* s)
00161 {
00162     return paramCount;
00163 }    
00164 
00165 static void 
00166 invoke_copy_to_stack(PRUint32* d, PRUint32 paramCount, nsXPCVariant* s)
00167 {
00168     for(PRUint32 i = 0; i < paramCount; i++, d++, s++)
00169     {
00170         *((PRUint32*)d) = *((PRUint32*)s);
00171     }
00172 }
00173 
00174 static nsresult
00175 DoInvoke(void* that, PRUint32 index,
00176          PRUint32 paramCount, nsXPCVariant* params)
00177 {
00178     PRUint32 result;
00179     void* fn_count = invoke_count_words;
00180     void* fn_copy = invoke_copy_to_stack;
00181 
00182  __asm__ __volatile__(
00183     "pushl %4\n\t"
00184     "pushl %3\n\t"
00185     "movl  %5, %%eax\n\t"
00186     "call  *%%eax\n\t"       /* count words */
00187     "addl  $0x8, %%esp\n\t"
00188     "shl   $2, %%eax\n\t"    /* *= 4 */
00189     "subl  %%eax, %%esp\n\t" /* make room for params */
00190     "movl  %%esp, %%edx\n\t"
00191     "pushl %4\n\t"
00192     "pushl %3\n\t"
00193     "pushl %%edx\n\t"
00194     "movl  %6, %%eax\n\t"
00195     "call  *%%eax\n\t"       /* copy params */
00196     "addl  $0xc, %%esp\n\t"
00197     "movl  %1, %%ecx\n\t"
00198     "pushl %%ecx\n\t"
00199     "movl  (%%ecx), %%edx\n\t"
00200     "movl  %2, %%eax\n\t"   /* function index */
00201     "shl   $2, %%eax\n\t"   /* *= 4 */
00202     "addl  $8, %%eax\n\t"   /* += 8 */
00203     "addl  %%eax, %%edx\n\t"
00204     "call  *(%%edx)\n\t"    /* safe to not cleanup esp */
00205     "movl  %%eax, %0"
00206     : "=g" (result)         /* %0 */
00207     : "g" (that),           /* %1 */
00208       "g" (index),          /* %2 */
00209       "g" (paramCount),     /* %3 */
00210       "g" (params),         /* %4 */
00211       "g" (fn_count),       /* %5 */
00212       "g" (fn_copy)         /* %6 */
00213     : "ax", "cx", "dx", "memory" 
00214     );
00215   
00216   return result;
00217 }    
00218 
00219 #endif
00220 /***************************************************************************/
00221 
00222 int main()
00223 {
00224   nsXPCVariant params1[2] = {1,2};
00225   nsXPCVariant params2[2] = {2,4};
00226   nsXPCVariant params3[2] = {3,6};
00227 
00228   foo* a = new bar();
00229 
00230 //  printf("calling via C++...\n");
00231 //  docall(a, 12, 24);
00232 
00233   printf("calling via ASM...\n");
00234   DoInvoke(a, 1, 2, params1);
00235   DoInvoke(a, 2, 2, params2);
00236   DoInvoke(a, 3, 2, params3);
00237 
00238   return 0;
00239 }