176cf6069SPedro Giffuni /**************************************************************
276cf6069SPedro Giffuni  *
376cf6069SPedro Giffuni  * Licensed to the Apache Software Foundation (ASF) under one
476cf6069SPedro Giffuni  * or more contributor license agreements.  See the NOTICE file
576cf6069SPedro Giffuni  * distributed with this work for additional information
676cf6069SPedro Giffuni  * regarding copyright ownership.  The ASF licenses this file
776cf6069SPedro Giffuni  * to you under the Apache License, Version 2.0 (the
876cf6069SPedro Giffuni  * "License"); you may not use this file except in compliance
976cf6069SPedro Giffuni  * with the License.  You may obtain a copy of the License at
1076cf6069SPedro Giffuni  *
1176cf6069SPedro Giffuni  *   http://www.apache.org/licenses/LICENSE-2.0
1276cf6069SPedro Giffuni  *
1376cf6069SPedro Giffuni  * Unless required by applicable law or agreed to in writing,
1476cf6069SPedro Giffuni  * software distributed under the License is distributed on an
1576cf6069SPedro Giffuni  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1676cf6069SPedro Giffuni  * KIND, either express or implied.  See the License for the
1776cf6069SPedro Giffuni  * specific language governing permissions and limitations
1876cf6069SPedro Giffuni  * under the License.
1976cf6069SPedro Giffuni  *
2076cf6069SPedro Giffuni  *************************************************************/
2176cf6069SPedro Giffuni 
2276cf6069SPedro Giffuni 
2376cf6069SPedro Giffuni 
2476cf6069SPedro Giffuni // MARKER(update_precomp.py): autogen include statement, do not remove
2576cf6069SPedro Giffuni #include "precompiled_bridges.hxx"
2676cf6069SPedro Giffuni 
27*969b7f0aSPedro Giffuni #include <stdlib.h>
2876cf6069SPedro Giffuni 
2976cf6069SPedro Giffuni #include <com/sun/star/uno/genfunc.hxx>
3076cf6069SPedro Giffuni #include <uno/data.h>
3176cf6069SPedro Giffuni 
3276cf6069SPedro Giffuni #include "bridges/cpp_uno/shared/bridge.hxx"
3376cf6069SPedro Giffuni #include "bridges/cpp_uno/shared/types.hxx"
3476cf6069SPedro Giffuni #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
3576cf6069SPedro Giffuni #include "bridges/cpp_uno/shared/vtables.hxx"
3676cf6069SPedro Giffuni 
3776cf6069SPedro Giffuni #include "share.hxx"
3876cf6069SPedro Giffuni 
3976cf6069SPedro Giffuni 
4076cf6069SPedro Giffuni using namespace ::rtl;
4176cf6069SPedro Giffuni using namespace ::com::sun::star::uno;
4276cf6069SPedro Giffuni 
4376cf6069SPedro Giffuni namespace
4476cf6069SPedro Giffuni {
4576cf6069SPedro Giffuni 
4676cf6069SPedro Giffuni 
4776cf6069SPedro Giffuni //==================================================================================================
callVirtualMethod(void * pAdjustedThisPtr,sal_Int32 nVtableIndex,void * pRegisterReturn,typelib_TypeClass eReturnType,char * pPT,sal_Int32 * pStackLongs,sal_Int32 nStackLongs)4876cf6069SPedro Giffuni static void callVirtualMethod(
4976cf6069SPedro Giffuni     void * pAdjustedThisPtr,
5076cf6069SPedro Giffuni     sal_Int32 nVtableIndex,
5176cf6069SPedro Giffuni     void * pRegisterReturn,
5276cf6069SPedro Giffuni     typelib_TypeClass eReturnType,
5376cf6069SPedro Giffuni     char * pPT,
5476cf6069SPedro Giffuni     sal_Int32 * pStackLongs,
5576cf6069SPedro Giffuni     sal_Int32 nStackLongs)
5676cf6069SPedro Giffuni {
5776cf6069SPedro Giffuni 
5876cf6069SPedro Giffuni   // parameter list is mixed list of * and values
5976cf6069SPedro Giffuni   // reference parameters are pointers
6076cf6069SPedro Giffuni 
6176cf6069SPedro Giffuni   // the basic idea here is to use gpr[8] as a storage area for
6276cf6069SPedro Giffuni   // the future values of registers r3 to r10 needed for the call,
6376cf6069SPedro Giffuni   // and similarly fpr[8] as a storage area for the future values
6476cf6069SPedro Giffuni   // of floating point registers f1 to f8
6576cf6069SPedro Giffuni 
6676cf6069SPedro Giffuni      unsigned long * mfunc;        // actual function to be invoked
6776cf6069SPedro Giffuni      void (*ptr)();
6876cf6069SPedro Giffuni      int gpr[8];                   // storage for gpregisters, map to r3-r10
6976cf6069SPedro Giffuni      int off;                      // offset used to find function
7076cf6069SPedro Giffuni #ifndef __NO_FPRS__
7176cf6069SPedro Giffuni      double fpr[8];                // storage for fpregisters, map to f1-f8
7276cf6069SPedro Giffuni      int f;                        // number of fprs mapped so far
7376cf6069SPedro Giffuni      double dret;                  // temporary function return values
7476cf6069SPedro Giffuni #endif
7576cf6069SPedro Giffuni      int n;                        // number of gprs mapped so far
7676cf6069SPedro Giffuni      long *p;                      // pointer to parameter overflow area
7776cf6069SPedro Giffuni      int c;                        // character of parameter type being decoded
7876cf6069SPedro Giffuni      int iret, iret2;
7976cf6069SPedro Giffuni 
8076cf6069SPedro Giffuni      // Because of the Power PC calling conventions we could be passing
8176cf6069SPedro Giffuni      // parameters in both register types and on the stack. To create the
8276cf6069SPedro Giffuni      // stack parameter area we need we now simply allocate local
8376cf6069SPedro Giffuni      // variable storage param[] that is at least the size of the parameter stack
8476cf6069SPedro Giffuni      // (more than enough space) which we can overwrite the parameters into.
8576cf6069SPedro Giffuni 
8676cf6069SPedro Giffuni      // Note: This keeps us from having to decode the signature twice and
8776cf6069SPedro Giffuni      // prevents problems with later local variables.
8876cf6069SPedro Giffuni 
8976cf6069SPedro Giffuni      // Note: could require up to  2*nStackLongs words of parameter stack area
9076cf6069SPedro Giffuni      // if the call has many float parameters (i.e. floats take up only 1
9176cf6069SPedro Giffuni      // word on the stack but double takes 2 words in parameter area in the
9276cf6069SPedro Giffuni      // stack frame .
9376cf6069SPedro Giffuni 
9476cf6069SPedro Giffuni      // Update! floats on the outgoing parameter stack only take up 1 word
9576cf6069SPedro Giffuni      // (stfs is used) which is not correct according to the ABI but we
9676cf6069SPedro Giffuni      // will match what the compiler does until this is figured out
9776cf6069SPedro Giffuni 
9876cf6069SPedro Giffuni      // this grows the current stack to the appropriate size
9976cf6069SPedro Giffuni      // and sets the outgoing stack pointer p to the right place
10076cf6069SPedro Giffuni      __asm__ __volatile__ (
10176cf6069SPedro Giffuni           "rlwinm %0,%0,3,3,28\n\t"
10276cf6069SPedro Giffuni           "addi %0,%0,22\n\t"
10376cf6069SPedro Giffuni           "rlwinm %0,%0,0,4,28\n\t"
10476cf6069SPedro Giffuni           "lwz 0,0(1)\n\t"
10576cf6069SPedro Giffuni           "subf 1,%0,1\n\t"
10676cf6069SPedro Giffuni           "stw 0,0(1)\n\t"
10776cf6069SPedro Giffuni           : : "r" (nStackLongs) : "0" );
10876cf6069SPedro Giffuni 
10976cf6069SPedro Giffuni      __asm__ __volatile__ ( "addi %0,1,8" : "=r" (p) : );
11076cf6069SPedro Giffuni 
11176cf6069SPedro Giffuni      // never called
11276cf6069SPedro Giffuni      // if (! pAdjustedThisPtr ) dummy_can_throw_anything("xxx"); // address something
11376cf6069SPedro Giffuni 
11476cf6069SPedro Giffuni 
11576cf6069SPedro Giffuni      // now begin to load the C++ function arguments into storage
11676cf6069SPedro Giffuni      n = 0;
11776cf6069SPedro Giffuni #ifndef __NO_FPRS__
11876cf6069SPedro Giffuni      f = 0;
11976cf6069SPedro Giffuni #endif
12076cf6069SPedro Giffuni 
12176cf6069SPedro Giffuni      // now we need to parse the entire signature string */
12276cf6069SPedro Giffuni      // until we get the END indicator */
12376cf6069SPedro Giffuni 
12476cf6069SPedro Giffuni      // treat complex return pointer like any other parameter //
12576cf6069SPedro Giffuni 
12676cf6069SPedro Giffuni #if 0
12776cf6069SPedro Giffuni      /* Let's figure out what is really going on here*/
12876cf6069SPedro Giffuni      fprintf(stderr,"callVirtualMethod parameters string is %s\n",pPT);
12976cf6069SPedro Giffuni      int k = nStackLongs;
13076cf6069SPedro Giffuni      long * q = (long *)pStackLongs;
13176cf6069SPedro Giffuni      while (k > 0) {
13276cf6069SPedro Giffuni        fprintf(stderr,"uno stack is: %x\n",*q);
13376cf6069SPedro Giffuni        k--;
13476cf6069SPedro Giffuni        q++;
13576cf6069SPedro Giffuni      }
13676cf6069SPedro Giffuni #endif
13776cf6069SPedro Giffuni 
13876cf6069SPedro Giffuni      /* parse the argument list up to the ending ) */
13976cf6069SPedro Giffuni      while (*pPT != 'X') {
14076cf6069SPedro Giffuni        c = *pPT;
14176cf6069SPedro Giffuni        switch (c) {
14276cf6069SPedro Giffuni        case 'D':                   /* type is double */
14376cf6069SPedro Giffuni #ifndef __NO_FPRS__
14476cf6069SPedro Giffuni             if (f < 8) {
14576cf6069SPedro Giffuni                fpr[f++] = *((double *)pStackLongs);   /* store in register */
14676cf6069SPedro Giffuni #else
14776cf6069SPedro Giffuni             if (n & 1)
14876cf6069SPedro Giffuni                n++;
14976cf6069SPedro Giffuni             if (n < 8) {
15076cf6069SPedro Giffuni                gpr[n++] = *pStackLongs;
15176cf6069SPedro Giffuni                gpr[n++] = *(pStackLongs+1);
15276cf6069SPedro Giffuni #endif
15376cf6069SPedro Giffuni 	    } else {
15476cf6069SPedro Giffuni 	       if (((long) p) & 4)
15576cf6069SPedro Giffuni 	          p++;
15676cf6069SPedro Giffuni                *p++ = *pStackLongs;       /* or on the parameter stack */
15776cf6069SPedro Giffuni                *p++ = *(pStackLongs + 1);
15876cf6069SPedro Giffuni 	    }
15976cf6069SPedro Giffuni             pStackLongs += 2;
16076cf6069SPedro Giffuni             break;
16176cf6069SPedro Giffuni 
16276cf6069SPedro Giffuni        case 'F':                   /* type is float */
16376cf6069SPedro Giffuni 	 /* this assumes that floats are stored as 1 32 bit word on param
16476cf6069SPedro Giffuni 	    stack and that if passed in parameter stack to C, should be
16576cf6069SPedro Giffuni 	    as double word.
16676cf6069SPedro Giffuni 
16776cf6069SPedro Giffuni             Whoops: the abi is not actually followed by gcc, need to
16876cf6069SPedro Giffuni             store floats as a *single* word on outgoing parameter stack
16976cf6069SPedro Giffuni             to match what gcc actually does
17076cf6069SPedro Giffuni 	 */
17176cf6069SPedro Giffuni #ifndef __NO_FPRS__
17276cf6069SPedro Giffuni             if (f < 8) {
17376cf6069SPedro Giffuni                fpr[f++] = *((float *)pStackLongs);
17476cf6069SPedro Giffuni #else
17576cf6069SPedro Giffuni             if (n < 8) {
17676cf6069SPedro Giffuni                gpr[n++] = *pStackLongs;
17776cf6069SPedro Giffuni #endif
17876cf6069SPedro Giffuni 	    } else {
17976cf6069SPedro Giffuni #if 0 /* if abi were followed */
18076cf6069SPedro Giffuni 	       if (((long) p) & 4)
18176cf6069SPedro Giffuni 	          p++;
18276cf6069SPedro Giffuni 	       *((double *)p) = *((float *)pStackLongs);
18376cf6069SPedro Giffuni                p += 2;
18476cf6069SPedro Giffuni #else
18576cf6069SPedro Giffuni 	       *((float *)p) = *((float *)pStackLongs);
18676cf6069SPedro Giffuni                p += 1;
18776cf6069SPedro Giffuni #endif
18876cf6069SPedro Giffuni 	    }
18976cf6069SPedro Giffuni             pStackLongs += 1;
19076cf6069SPedro Giffuni             break;
19176cf6069SPedro Giffuni 
19276cf6069SPedro Giffuni        case 'H':                /* type is long long */
19376cf6069SPedro Giffuni             if (n & 1) n++; 	/* note even elements gpr[] will map to
19476cf6069SPedro Giffuni                                    odd registers*/
19576cf6069SPedro Giffuni             if (n <= 6) {
19676cf6069SPedro Giffuni                gpr[n++] = *pStackLongs;
19776cf6069SPedro Giffuni                gpr[n++] = *(pStackLongs+1);
19876cf6069SPedro Giffuni 	    } else {
19976cf6069SPedro Giffuni 	       if (((long) p) & 4)
20076cf6069SPedro Giffuni 	          p++;
20176cf6069SPedro Giffuni                *p++ = *pStackLongs;
20276cf6069SPedro Giffuni                *p++ = *(pStackLongs+1);
20376cf6069SPedro Giffuni 	    }
20476cf6069SPedro Giffuni             pStackLongs += 2;
20576cf6069SPedro Giffuni             break;
20676cf6069SPedro Giffuni 
20776cf6069SPedro Giffuni        case 'S':
20876cf6069SPedro Giffuni             if (n < 8) {
20976cf6069SPedro Giffuni                gpr[n++] = *((unsigned short*)pStackLongs);
21076cf6069SPedro Giffuni 	    } else {
21176cf6069SPedro Giffuni                *p++ = *((unsigned short *)pStackLongs);
21276cf6069SPedro Giffuni 	    }
21376cf6069SPedro Giffuni             pStackLongs += 1;
21476cf6069SPedro Giffuni             break;
21576cf6069SPedro Giffuni 
21676cf6069SPedro Giffuni        case 'B':
21776cf6069SPedro Giffuni             if (n < 8) {
21876cf6069SPedro Giffuni                gpr[n++] = *((char *)pStackLongs);
21976cf6069SPedro Giffuni 	    } else {
22076cf6069SPedro Giffuni                *p++ = *((char *)pStackLongs);
22176cf6069SPedro Giffuni 	    }
22276cf6069SPedro Giffuni             pStackLongs += 1;
22376cf6069SPedro Giffuni             break;
22476cf6069SPedro Giffuni 
22576cf6069SPedro Giffuni        default:
22676cf6069SPedro Giffuni             if (n < 8) {
22776cf6069SPedro Giffuni                gpr[n++] = *pStackLongs;
22876cf6069SPedro Giffuni 	    } else {
22976cf6069SPedro Giffuni                *p++ = *pStackLongs;
23076cf6069SPedro Giffuni 	    }
23176cf6069SPedro Giffuni             pStackLongs += 1;
23276cf6069SPedro Giffuni             break;
23376cf6069SPedro Giffuni        }
23476cf6069SPedro Giffuni        pPT++;
23576cf6069SPedro Giffuni      }
23676cf6069SPedro Giffuni 
23776cf6069SPedro Giffuni      /* figure out the address of the function we need to invoke */
23876cf6069SPedro Giffuni      off = nVtableIndex;
23976cf6069SPedro Giffuni      off = off * 4;                         // 4 bytes per slot
24076cf6069SPedro Giffuni      mfunc = *((unsigned long **)pAdjustedThisPtr);    // get the address of the vtable
24176cf6069SPedro Giffuni      mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
24276cf6069SPedro Giffuni      mfunc = *((unsigned long **)mfunc);                 // the function is stored at the address
24376cf6069SPedro Giffuni      ptr = (void (*)())mfunc;
24476cf6069SPedro Giffuni 
24576cf6069SPedro Giffuni     /* Set up the machine registers and invoke the function */
24676cf6069SPedro Giffuni 
24776cf6069SPedro Giffuni     __asm__ __volatile__ (
24876cf6069SPedro Giffuni 		"lwz	3,	0(%0)\n\t"
24976cf6069SPedro Giffuni 		"lwz	4,	4(%0)\n\t"
25076cf6069SPedro Giffuni 		"lwz	5,	8(%0)\n\t"
25176cf6069SPedro Giffuni 		"lwz	6,	12(%0)\n\t"
25276cf6069SPedro Giffuni 		"lwz	7,	16(%0)\n\t"
25376cf6069SPedro Giffuni 		"lwz	8,	20(%0)\n\t"
25476cf6069SPedro Giffuni 		"lwz	9,	24(%0)\n\t"
25576cf6069SPedro Giffuni 		"lwz	10,	28(%0)\n\t"
25676cf6069SPedro Giffuni #ifndef __NO_FPRS__
25776cf6069SPedro Giffuni 		"lfd	1,	0(%1)\n\t"
25876cf6069SPedro Giffuni 		"lfd	2,	8(%1)\n\t"
25976cf6069SPedro Giffuni 		"lfd	3,	16(%1)\n\t"
26076cf6069SPedro Giffuni 		"lfd	4,	24(%1)\n\t"
26176cf6069SPedro Giffuni 		"lfd	5,	32(%1)\n\t"
26276cf6069SPedro Giffuni 		"lfd	6,	40(%1)\n\t"
26376cf6069SPedro Giffuni 		"lfd	7,	48(%1)\n\t"
26476cf6069SPedro Giffuni 		"lfd	8,	56(%1)\n\t"
26576cf6069SPedro Giffuni 	        : : "r" (gpr), "r" (fpr)
26676cf6069SPedro Giffuni #else
26776cf6069SPedro Giffuni 	        : : "r" (gpr)
26876cf6069SPedro Giffuni #endif
26976cf6069SPedro Giffuni 		: "0", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
27076cf6069SPedro Giffuni     );
27176cf6069SPedro Giffuni 
27276cf6069SPedro Giffuni     (*ptr)();
27376cf6069SPedro Giffuni 
27476cf6069SPedro Giffuni     __asm__ __volatile__ (
27576cf6069SPedro Giffuni        "mr     %0,     3\n\t"
27676cf6069SPedro Giffuni        "mr     %1,     4\n\t"
27776cf6069SPedro Giffuni #ifndef __NO_FPRS__
27876cf6069SPedro Giffuni        "fmr    %2,     1\n\t"
27976cf6069SPedro Giffuni        : "=r" (iret), "=r" (iret2), "=f" (dret)
28076cf6069SPedro Giffuni #else
28176cf6069SPedro Giffuni        : "=r" (iret), "=r" (iret2)
28276cf6069SPedro Giffuni #endif
28376cf6069SPedro Giffuni        : );
28476cf6069SPedro Giffuni 
28576cf6069SPedro Giffuni     switch( eReturnType )
28676cf6069SPedro Giffuni 	{
28776cf6069SPedro Giffuni 		case typelib_TypeClass_HYPER:
28876cf6069SPedro Giffuni 		case typelib_TypeClass_UNSIGNED_HYPER:
28976cf6069SPedro Giffuni 		        ((long*)pRegisterReturn)[0] = iret;
29076cf6069SPedro Giffuni 			((long*)pRegisterReturn)[1] = iret2;
29176cf6069SPedro Giffuni 		case typelib_TypeClass_LONG:
29276cf6069SPedro Giffuni 		case typelib_TypeClass_UNSIGNED_LONG:
29376cf6069SPedro Giffuni 		case typelib_TypeClass_ENUM:
29476cf6069SPedro Giffuni 			((long*)pRegisterReturn)[0] = iret;
29576cf6069SPedro Giffuni 			break;
29676cf6069SPedro Giffuni 		case typelib_TypeClass_CHAR:
29776cf6069SPedro Giffuni 		case typelib_TypeClass_SHORT:
29876cf6069SPedro Giffuni 		case typelib_TypeClass_UNSIGNED_SHORT:
29976cf6069SPedro Giffuni 		        *(unsigned short*)pRegisterReturn = (unsigned short)iret;
30076cf6069SPedro Giffuni 			break;
30176cf6069SPedro Giffuni 		case typelib_TypeClass_BOOLEAN:
30276cf6069SPedro Giffuni 		case typelib_TypeClass_BYTE:
30376cf6069SPedro Giffuni 		        *(unsigned char*)pRegisterReturn = (unsigned char)iret;
30476cf6069SPedro Giffuni 			break;
30576cf6069SPedro Giffuni 		case typelib_TypeClass_FLOAT:
30676cf6069SPedro Giffuni #ifndef __NO_FPRS__
30776cf6069SPedro Giffuni 		        *(float*)pRegisterReturn = (float)dret;
30876cf6069SPedro Giffuni #else
30976cf6069SPedro Giffuni 		        ((unsigned int*)pRegisterReturn)[0] = iret;
31076cf6069SPedro Giffuni #endif
31176cf6069SPedro Giffuni 			break;
31276cf6069SPedro Giffuni 		case typelib_TypeClass_DOUBLE:
31376cf6069SPedro Giffuni #ifndef __NO_FPRS__
31476cf6069SPedro Giffuni 			*(double*)pRegisterReturn = dret;
31576cf6069SPedro Giffuni #else
31676cf6069SPedro Giffuni 			((unsigned int*)pRegisterReturn)[0] = iret;
31776cf6069SPedro Giffuni 			((unsigned int*)pRegisterReturn)[1] = iret2;
31876cf6069SPedro Giffuni #endif
31976cf6069SPedro Giffuni 			break;
32076cf6069SPedro Giffuni 		default:
32176cf6069SPedro Giffuni 			break;
32276cf6069SPedro Giffuni 	}
32376cf6069SPedro Giffuni }
32476cf6069SPedro Giffuni 
32576cf6069SPedro Giffuni 
32676cf6069SPedro Giffuni //==================================================================================================
32776cf6069SPedro Giffuni static void cpp_call(
32876cf6069SPedro Giffuni 	bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
32976cf6069SPedro Giffuni 	bridges::cpp_uno::shared::VtableSlot  aVtableSlot,
33076cf6069SPedro Giffuni 	typelib_TypeDescriptionReference * pReturnTypeRef,
33176cf6069SPedro Giffuni 	sal_Int32 nParams, typelib_MethodParameter * pParams,
33276cf6069SPedro Giffuni 	void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
33376cf6069SPedro Giffuni {
33476cf6069SPedro Giffuni   	// max space for: [complex ret ptr], values|ptr ...
33576cf6069SPedro Giffuni   	char * pCppStack		=
33676cf6069SPedro Giffuni   		(char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
33776cf6069SPedro Giffuni   	char * pCppStackStart	= pCppStack;
33876cf6069SPedro Giffuni 
33976cf6069SPedro Giffuni         // need to know parameter types for callVirtualMethod so generate a signature string
34076cf6069SPedro Giffuni         char * pParamType = (char *) alloca(nParams+2);
34176cf6069SPedro Giffuni         char * pPT = pParamType;
34276cf6069SPedro Giffuni 
34376cf6069SPedro Giffuni 	// return
34476cf6069SPedro Giffuni 	typelib_TypeDescription * pReturnTypeDescr = 0;
34576cf6069SPedro Giffuni 	TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
34676cf6069SPedro Giffuni 	// OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
34776cf6069SPedro Giffuni 
34876cf6069SPedro Giffuni 	void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
34976cf6069SPedro Giffuni 
35076cf6069SPedro Giffuni 	if (pReturnTypeDescr)
35176cf6069SPedro Giffuni 	{
35276cf6069SPedro Giffuni 		if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
35376cf6069SPedro Giffuni 		{
35476cf6069SPedro Giffuni 			pCppReturn = pUnoReturn; // direct way for simple types
35576cf6069SPedro Giffuni 		}
35676cf6069SPedro Giffuni 		else
35776cf6069SPedro Giffuni 		{
35876cf6069SPedro Giffuni 			// complex return via ptr
35976cf6069SPedro Giffuni 			pCppReturn = *(void **)pCppStack =
36076cf6069SPedro Giffuni                               (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
36176cf6069SPedro Giffuni 			       ? alloca( pReturnTypeDescr->nSize ): pUnoReturn); // direct way
36276cf6069SPedro Giffuni                         *pPT++ = 'I'; //signify that a complex return type on stack
36376cf6069SPedro Giffuni 			pCppStack += sizeof(void *);
36476cf6069SPedro Giffuni 		}
36576cf6069SPedro Giffuni 	}
36676cf6069SPedro Giffuni 	// push this
36776cf6069SPedro Giffuni         void* pAdjustedThisPtr = reinterpret_cast< void **>(pThis->getCppI()) + aVtableSlot.offset;
36876cf6069SPedro Giffuni 	*(void**)pCppStack = pAdjustedThisPtr;
36976cf6069SPedro Giffuni 	pCppStack += sizeof( void* );
37076cf6069SPedro Giffuni         *pPT++ = 'I';
37176cf6069SPedro Giffuni 
37276cf6069SPedro Giffuni 	// stack space
37376cf6069SPedro Giffuni 	// OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
37476cf6069SPedro Giffuni 	// args
37576cf6069SPedro Giffuni 	void ** pCppArgs  = (void **)alloca( 3 * sizeof(void *) * nParams );
37676cf6069SPedro Giffuni 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
37776cf6069SPedro Giffuni 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
37876cf6069SPedro Giffuni 	// type descriptions for reconversions
37976cf6069SPedro Giffuni 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
38076cf6069SPedro Giffuni 
38176cf6069SPedro Giffuni 	sal_Int32 nTempIndizes   = 0;
38276cf6069SPedro Giffuni 
38376cf6069SPedro Giffuni 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
38476cf6069SPedro Giffuni 	{
38576cf6069SPedro Giffuni 		const typelib_MethodParameter & rParam = pParams[nPos];
38676cf6069SPedro Giffuni 		typelib_TypeDescription * pParamTypeDescr = 0;
38776cf6069SPedro Giffuni 		TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
38876cf6069SPedro Giffuni 
38976cf6069SPedro Giffuni 		if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
39076cf6069SPedro Giffuni 		{
39176cf6069SPedro Giffuni 			uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
39276cf6069SPedro Giffuni 									pThis->getBridge()->getUno2Cpp() );
39376cf6069SPedro Giffuni 
39476cf6069SPedro Giffuni 			switch (pParamTypeDescr->eTypeClass)
39576cf6069SPedro Giffuni 			{
39676cf6069SPedro Giffuni 
39776cf6069SPedro Giffuni                           // we need to know type of each param so that we know whether to use
39876cf6069SPedro Giffuni                           // gpr or fpr to pass in parameters:
39976cf6069SPedro Giffuni                           // Key: I - int, long, pointer, etc means pass in gpr
40076cf6069SPedro Giffuni                           //      B - byte value passed in gpr
40176cf6069SPedro Giffuni                           //      S - short value passed in gpr
40276cf6069SPedro Giffuni                           //      F - float value pass in fpr
40376cf6069SPedro Giffuni                           //      D - double value pass in fpr
40476cf6069SPedro Giffuni                           //      H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
40576cf6069SPedro Giffuni                           //      X - indicates end of parameter description string
40676cf6069SPedro Giffuni 
40776cf6069SPedro Giffuni 		          case typelib_TypeClass_LONG:
40876cf6069SPedro Giffuni 		          case typelib_TypeClass_UNSIGNED_LONG:
40976cf6069SPedro Giffuni 		          case typelib_TypeClass_ENUM:
41076cf6069SPedro Giffuni 			    *pPT++ = 'I';
41176cf6069SPedro Giffuni 			    break;
41276cf6069SPedro Giffuni  		          case typelib_TypeClass_SHORT:
41376cf6069SPedro Giffuni 		          case typelib_TypeClass_CHAR:
41476cf6069SPedro Giffuni 		          case typelib_TypeClass_UNSIGNED_SHORT:
41576cf6069SPedro Giffuni                             *pPT++ = 'S';
41676cf6069SPedro Giffuni                             break;
41776cf6069SPedro Giffuni 		          case typelib_TypeClass_BOOLEAN:
41876cf6069SPedro Giffuni 		          case typelib_TypeClass_BYTE:
41976cf6069SPedro Giffuni                             *pPT++ = 'B';
42076cf6069SPedro Giffuni                             break;
42176cf6069SPedro Giffuni 		          case typelib_TypeClass_FLOAT:
42276cf6069SPedro Giffuni                             *pPT++ = 'F';
42376cf6069SPedro Giffuni 			    break;
42476cf6069SPedro Giffuni 		        case typelib_TypeClass_DOUBLE:
42576cf6069SPedro Giffuni 			    *pPT++ = 'D';
42676cf6069SPedro Giffuni 			    pCppStack += sizeof(sal_Int32); // extra long
42776cf6069SPedro Giffuni 			    break;
42876cf6069SPedro Giffuni 			case typelib_TypeClass_HYPER:
42976cf6069SPedro Giffuni 			case typelib_TypeClass_UNSIGNED_HYPER:
43076cf6069SPedro Giffuni 			    *pPT++ = 'H';
43176cf6069SPedro Giffuni 			    pCppStack += sizeof(sal_Int32); // extra long
43276cf6069SPedro Giffuni 			default:
43376cf6069SPedro Giffuni 			    break;
43476cf6069SPedro Giffuni 			}
43576cf6069SPedro Giffuni 
43676cf6069SPedro Giffuni 			// no longer needed
43776cf6069SPedro Giffuni 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
43876cf6069SPedro Giffuni 		}
43976cf6069SPedro Giffuni 		else // ptr to complex value | ref
44076cf6069SPedro Giffuni 		{
44176cf6069SPedro Giffuni 			if (! rParam.bIn) // is pure out
44276cf6069SPedro Giffuni 			{
44376cf6069SPedro Giffuni 				// cpp out is constructed mem, uno out is not!
44476cf6069SPedro Giffuni 				uno_constructData(
44576cf6069SPedro Giffuni 					*(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
44676cf6069SPedro Giffuni 					pParamTypeDescr );
44776cf6069SPedro Giffuni 				pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
44876cf6069SPedro Giffuni 				// will be released at reconversion
44976cf6069SPedro Giffuni 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
45076cf6069SPedro Giffuni 			}
45176cf6069SPedro Giffuni 			// is in/inout
45276cf6069SPedro Giffuni 			else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
45376cf6069SPedro Giffuni 			{
45476cf6069SPedro Giffuni 				uno_copyAndConvertData(
45576cf6069SPedro Giffuni 					*(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
45676cf6069SPedro Giffuni 					pUnoArgs[nPos], pParamTypeDescr,
45776cf6069SPedro Giffuni                                         pThis->getBridge()->getUno2Cpp() );
45876cf6069SPedro Giffuni 
45976cf6069SPedro Giffuni 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
46076cf6069SPedro Giffuni 				// will be released at reconversion
46176cf6069SPedro Giffuni 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
46276cf6069SPedro Giffuni 			}
46376cf6069SPedro Giffuni 			else // direct way
46476cf6069SPedro Giffuni 			{
46576cf6069SPedro Giffuni 				*(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
46676cf6069SPedro Giffuni 				// no longer needed
46776cf6069SPedro Giffuni 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
46876cf6069SPedro Giffuni 			}
46976cf6069SPedro Giffuni                         // KBH: FIXME: is this the right way to pass these
47076cf6069SPedro Giffuni                         *pPT++='I';
47176cf6069SPedro Giffuni 		}
47276cf6069SPedro Giffuni 		pCppStack += sizeof(sal_Int32); // standard parameter length
47376cf6069SPedro Giffuni 	}
47476cf6069SPedro Giffuni 
47576cf6069SPedro Giffuni         // terminate the signature string
47676cf6069SPedro Giffuni         *pPT++='X';
47776cf6069SPedro Giffuni         *pPT=0;
47876cf6069SPedro Giffuni 
47976cf6069SPedro Giffuni 	try
48076cf6069SPedro Giffuni 	{
48176cf6069SPedro Giffuni 		OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
48276cf6069SPedro Giffuni 		callVirtualMethod(
48376cf6069SPedro Giffuni 			pAdjustedThisPtr, aVtableSlot.index,
48476cf6069SPedro Giffuni 			pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
48576cf6069SPedro Giffuni 			(sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
48676cf6069SPedro Giffuni 		// NO exception occurred...
48776cf6069SPedro Giffuni 		*ppUnoExc = 0;
48876cf6069SPedro Giffuni 
48976cf6069SPedro Giffuni 		// reconvert temporary params
49076cf6069SPedro Giffuni 		for ( ; nTempIndizes--; )
49176cf6069SPedro Giffuni 		{
49276cf6069SPedro Giffuni 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
49376cf6069SPedro Giffuni 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
49476cf6069SPedro Giffuni 
49576cf6069SPedro Giffuni 			if (pParams[nIndex].bIn)
49676cf6069SPedro Giffuni 			{
49776cf6069SPedro Giffuni 				if (pParams[nIndex].bOut) // inout
49876cf6069SPedro Giffuni 				{
49976cf6069SPedro Giffuni 					uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
50076cf6069SPedro Giffuni 					uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
50176cf6069SPedro Giffuni 											pThis->getBridge()->getCpp2Uno() );
50276cf6069SPedro Giffuni 				}
50376cf6069SPedro Giffuni 			}
50476cf6069SPedro Giffuni 			else // pure out
50576cf6069SPedro Giffuni 			{
50676cf6069SPedro Giffuni 				uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
50776cf6069SPedro Giffuni 										pThis->getBridge()->getCpp2Uno() );
50876cf6069SPedro Giffuni 			}
50976cf6069SPedro Giffuni 			// destroy temp cpp param => cpp: every param was constructed
51076cf6069SPedro Giffuni 			uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
51176cf6069SPedro Giffuni 
51276cf6069SPedro Giffuni 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
51376cf6069SPedro Giffuni 		}
51476cf6069SPedro Giffuni 		// return value
51576cf6069SPedro Giffuni 		if (pCppReturn && pUnoReturn != pCppReturn)
51676cf6069SPedro Giffuni 		{
51776cf6069SPedro Giffuni 			uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
51876cf6069SPedro Giffuni 									pThis->getBridge()->getCpp2Uno() );
51976cf6069SPedro Giffuni 			uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
52076cf6069SPedro Giffuni 		}
52176cf6069SPedro Giffuni 	}
52276cf6069SPedro Giffuni  	catch (...)
52376cf6069SPedro Giffuni  	{
52476cf6069SPedro Giffuni   		// fill uno exception
52576cf6069SPedro Giffuni 		fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
52676cf6069SPedro Giffuni                                   *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
52776cf6069SPedro Giffuni 
52876cf6069SPedro Giffuni 		// temporary params
52976cf6069SPedro Giffuni 		for ( ; nTempIndizes--; )
53076cf6069SPedro Giffuni 		{
53176cf6069SPedro Giffuni 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
53276cf6069SPedro Giffuni 			// destroy temp cpp param => cpp: every param was constructed
53376cf6069SPedro Giffuni 			uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
53476cf6069SPedro Giffuni 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
53576cf6069SPedro Giffuni 		}
53676cf6069SPedro Giffuni 		// return type
53776cf6069SPedro Giffuni 		if (pReturnTypeDescr)
53876cf6069SPedro Giffuni 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
53976cf6069SPedro Giffuni 	}
54076cf6069SPedro Giffuni }
54176cf6069SPedro Giffuni 
54276cf6069SPedro Giffuni }
54376cf6069SPedro Giffuni 
54476cf6069SPedro Giffuni namespace bridges { namespace cpp_uno { namespace shared {
54576cf6069SPedro Giffuni 
54676cf6069SPedro Giffuni void unoInterfaceProxyDispatch(
54776cf6069SPedro Giffuni 	uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
54876cf6069SPedro Giffuni 	void * pReturn, void * pArgs[], uno_Any ** ppException )
54976cf6069SPedro Giffuni {
55076cf6069SPedro Giffuni 	// is my surrogate
55176cf6069SPedro Giffuni         bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
55276cf6069SPedro Giffuni             = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
55376cf6069SPedro Giffuni 
55476cf6069SPedro Giffuni 	switch (pMemberDescr->eTypeClass)
55576cf6069SPedro Giffuni 	{
55676cf6069SPedro Giffuni 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
55776cf6069SPedro Giffuni 	{
55876cf6069SPedro Giffuni 
55976cf6069SPedro Giffuni         VtableSlot aVtableSlot(
56076cf6069SPedro Giffuni             getVtableSlot(
56176cf6069SPedro Giffuni                 reinterpret_cast<
56276cf6069SPedro Giffuni                     typelib_InterfaceAttributeTypeDescription const * >(
56376cf6069SPedro Giffuni                         pMemberDescr)));
56476cf6069SPedro Giffuni 
56576cf6069SPedro Giffuni 		if (pReturn)
56676cf6069SPedro Giffuni 		{
56776cf6069SPedro Giffuni 			// dependent dispatch
56876cf6069SPedro Giffuni 			cpp_call(
56976cf6069SPedro Giffuni 				pThis, aVtableSlot,
57076cf6069SPedro Giffuni 				((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
57176cf6069SPedro Giffuni 				0, 0, // no params
57276cf6069SPedro Giffuni 				pReturn, pArgs, ppException );
57376cf6069SPedro Giffuni 		}
57476cf6069SPedro Giffuni 		else
57576cf6069SPedro Giffuni 		{
57676cf6069SPedro Giffuni 			// is SET
57776cf6069SPedro Giffuni 			typelib_MethodParameter aParam;
57876cf6069SPedro Giffuni 			aParam.pTypeRef =
57976cf6069SPedro Giffuni 				((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
58076cf6069SPedro Giffuni 			aParam.bIn		= sal_True;
58176cf6069SPedro Giffuni 			aParam.bOut		= sal_False;
58276cf6069SPedro Giffuni 
58376cf6069SPedro Giffuni 			typelib_TypeDescriptionReference * pReturnTypeRef = 0;
58476cf6069SPedro Giffuni 			OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
58576cf6069SPedro Giffuni 			typelib_typedescriptionreference_new(
58676cf6069SPedro Giffuni 				&pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
58776cf6069SPedro Giffuni 
58876cf6069SPedro Giffuni 			// dependent dispatch
58976cf6069SPedro Giffuni                         aVtableSlot.index += 1; //get then set method
59076cf6069SPedro Giffuni 			cpp_call(
59176cf6069SPedro Giffuni 				pThis, aVtableSlot,
59276cf6069SPedro Giffuni 				pReturnTypeRef,
59376cf6069SPedro Giffuni 				1, &aParam,
59476cf6069SPedro Giffuni 				pReturn, pArgs, ppException );
59576cf6069SPedro Giffuni 
59676cf6069SPedro Giffuni 			typelib_typedescriptionreference_release( pReturnTypeRef );
59776cf6069SPedro Giffuni 		}
59876cf6069SPedro Giffuni 
59976cf6069SPedro Giffuni 		break;
60076cf6069SPedro Giffuni 	}
60176cf6069SPedro Giffuni 	case typelib_TypeClass_INTERFACE_METHOD:
60276cf6069SPedro Giffuni 	{
60376cf6069SPedro Giffuni 
60476cf6069SPedro Giffuni         VtableSlot aVtableSlot(
60576cf6069SPedro Giffuni             getVtableSlot(
60676cf6069SPedro Giffuni                 reinterpret_cast<
60776cf6069SPedro Giffuni                     typelib_InterfaceMethodTypeDescription const * >(
60876cf6069SPedro Giffuni                         pMemberDescr)));
60976cf6069SPedro Giffuni 		switch (aVtableSlot.index)
61076cf6069SPedro Giffuni 		{
61176cf6069SPedro Giffuni 			// standard calls
61276cf6069SPedro Giffuni 		case 1: // acquire uno interface
61376cf6069SPedro Giffuni 			(*pUnoI->acquire)( pUnoI );
61476cf6069SPedro Giffuni 			*ppException = 0;
61576cf6069SPedro Giffuni 			break;
61676cf6069SPedro Giffuni 		case 2: // release uno interface
61776cf6069SPedro Giffuni 			(*pUnoI->release)( pUnoI );
61876cf6069SPedro Giffuni 			*ppException = 0;
61976cf6069SPedro Giffuni 			break;
62076cf6069SPedro Giffuni 		case 0: // queryInterface() opt
62176cf6069SPedro Giffuni 		{
62276cf6069SPedro Giffuni 			typelib_TypeDescription * pTD = 0;
62376cf6069SPedro Giffuni 			TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
62476cf6069SPedro Giffuni 			if (pTD)
62576cf6069SPedro Giffuni 			{
62676cf6069SPedro Giffuni                 uno_Interface * pInterface = 0;
62776cf6069SPedro Giffuni                 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
62876cf6069SPedro Giffuni                     pThis->pBridge->getUnoEnv(),
62976cf6069SPedro Giffuni                     (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
63076cf6069SPedro Giffuni 
63176cf6069SPedro Giffuni                 if (pInterface)
63276cf6069SPedro Giffuni                 {
63376cf6069SPedro Giffuni                     ::uno_any_construct(
63476cf6069SPedro Giffuni                         reinterpret_cast< uno_Any * >( pReturn ),
63576cf6069SPedro Giffuni                         &pInterface, pTD, 0 );
63676cf6069SPedro Giffuni                     (*pInterface->release)( pInterface );
63776cf6069SPedro Giffuni                     TYPELIB_DANGER_RELEASE( pTD );
63876cf6069SPedro Giffuni                     *ppException = 0;
63976cf6069SPedro Giffuni                     break;
64076cf6069SPedro Giffuni                 }
64176cf6069SPedro Giffuni                 TYPELIB_DANGER_RELEASE( pTD );
64276cf6069SPedro Giffuni             }
64376cf6069SPedro Giffuni 		} // else perform queryInterface()
64476cf6069SPedro Giffuni 		default:
64576cf6069SPedro Giffuni 			// dependent dispatch
64676cf6069SPedro Giffuni 			cpp_call(
64776cf6069SPedro Giffuni 				pThis, aVtableSlot,
64876cf6069SPedro Giffuni 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
64976cf6069SPedro Giffuni 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
65076cf6069SPedro Giffuni 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
65176cf6069SPedro Giffuni 				pReturn, pArgs, ppException );
65276cf6069SPedro Giffuni 		}
65376cf6069SPedro Giffuni 		break;
65476cf6069SPedro Giffuni 	}
65576cf6069SPedro Giffuni 	default:
65676cf6069SPedro Giffuni 	{
65776cf6069SPedro Giffuni 		::com::sun::star::uno::RuntimeException aExc(
65876cf6069SPedro Giffuni 			OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
65976cf6069SPedro Giffuni 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
66076cf6069SPedro Giffuni 
66176cf6069SPedro Giffuni 		Type const & rExcType = ::getCppuType( &aExc );
66276cf6069SPedro Giffuni 		// binary identical null reference
66376cf6069SPedro Giffuni 		::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
66476cf6069SPedro Giffuni 	}
66576cf6069SPedro Giffuni 	}
66676cf6069SPedro Giffuni }
66776cf6069SPedro Giffuni 
66876cf6069SPedro Giffuni } } }
669