161dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
361dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
461dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
561dff127SAndrew Rist  * distributed with this work for additional information
661dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
761dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
861dff127SAndrew Rist  * "License"); you may not use this file except in compliance
961dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
1061dff127SAndrew Rist  *
1161dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1261dff127SAndrew Rist  *
1361dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1461dff127SAndrew Rist  * software distributed under the License is distributed on an
1561dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1661dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
1761dff127SAndrew Rist  * specific language governing permissions and limitations
1861dff127SAndrew Rist  * under the License.
1961dff127SAndrew Rist  *
2061dff127SAndrew Rist  *************************************************************/
2161dff127SAndrew Rist 
2261dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <malloc.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
30cdf0e10cSrcweir #include <uno/data.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx"
33cdf0e10cSrcweir #include "bridges/cpp_uno/shared/types.hxx"
34cdf0e10cSrcweir #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
35cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtables.hxx"
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include "share.hxx"
38cdf0e10cSrcweir 
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace ::rtl;
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir 
43cdf0e10cSrcweir namespace
44cdf0e10cSrcweir {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir //==================================================================================================
callVirtualMethod(void * pAdjustedThisPtr,sal_Int32 nVtableIndex,void * pRegisterReturn,typelib_TypeClass eReturnType,char * pPT,sal_Int32 * pStackLongs,sal_Int32 nStackLongs)48cdf0e10cSrcweir static void callVirtualMethod(
49cdf0e10cSrcweir     void * pAdjustedThisPtr,
50cdf0e10cSrcweir     sal_Int32 nVtableIndex,
51cdf0e10cSrcweir     void * pRegisterReturn,
52cdf0e10cSrcweir     typelib_TypeClass eReturnType,
53cdf0e10cSrcweir     char * pPT,
54cdf0e10cSrcweir     sal_Int32 * pStackLongs,
55cdf0e10cSrcweir     sal_Int32 nStackLongs)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir 
58cdf0e10cSrcweir   // parameter list is mixed list of * and values
59cdf0e10cSrcweir   // reference parameters are pointers
60cdf0e10cSrcweir 
61cdf0e10cSrcweir   // the basic idea here is to use gpr[8] as a storage area for
62cdf0e10cSrcweir   // the future values of registers r3 to r10 needed for the call,
63cdf0e10cSrcweir   // and similarly fpr[8] as a storage area for the future values
64cdf0e10cSrcweir   // of floating point registers f1 to f8
65cdf0e10cSrcweir 
66cdf0e10cSrcweir      unsigned long * mfunc;        // actual function to be invoked
67cdf0e10cSrcweir      void (*ptr)();
68cdf0e10cSrcweir      int gpr[8];                   // storage for gpregisters, map to r3-r10
69cdf0e10cSrcweir      int off;                      // offset used to find function
70cdf0e10cSrcweir #ifndef __NO_FPRS__
71cdf0e10cSrcweir      double fpr[8];                // storage for fpregisters, map to f1-f8
72cdf0e10cSrcweir      int f;                        // number of fprs mapped so far
73cdf0e10cSrcweir      double dret;                  // temporary function return values
74cdf0e10cSrcweir #endif
75cdf0e10cSrcweir      int n;                        // number of gprs mapped so far
76cdf0e10cSrcweir      long *p;                      // pointer to parameter overflow area
77cdf0e10cSrcweir      int c;                        // character of parameter type being decoded
78cdf0e10cSrcweir      int iret, iret2;
79cdf0e10cSrcweir 
80cdf0e10cSrcweir      // Because of the Power PC calling conventions we could be passing
81cdf0e10cSrcweir      // parameters in both register types and on the stack. To create the
82cdf0e10cSrcweir      // stack parameter area we need we now simply allocate local
83cdf0e10cSrcweir      // variable storage param[] that is at least the size of the parameter stack
84cdf0e10cSrcweir      // (more than enough space) which we can overwrite the parameters into.
85cdf0e10cSrcweir 
86cdf0e10cSrcweir      // Note: This keeps us from having to decode the signature twice and
87cdf0e10cSrcweir      // prevents problems with later local variables.
88cdf0e10cSrcweir 
89cdf0e10cSrcweir      // Note: could require up to  2*nStackLongs words of parameter stack area
90cdf0e10cSrcweir      // if the call has many float parameters (i.e. floats take up only 1
91cdf0e10cSrcweir      // word on the stack but double takes 2 words in parameter area in the
92cdf0e10cSrcweir      // stack frame .
93cdf0e10cSrcweir 
94cdf0e10cSrcweir      // Update! floats on the outgoing parameter stack only take up 1 word
95cdf0e10cSrcweir      // (stfs is used) which is not correct according to the ABI but we
96cdf0e10cSrcweir      // will match what the compiler does until this is figured out
97cdf0e10cSrcweir 
98cdf0e10cSrcweir      // this grows the current stack to the appropriate size
99cdf0e10cSrcweir      // and sets the outgoing stack pointer p to the right place
100cdf0e10cSrcweir      __asm__ __volatile__ (
101cdf0e10cSrcweir           "rlwinm %0,%0,3,3,28\n\t"
102cdf0e10cSrcweir           "addi %0,%0,22\n\t"
103cdf0e10cSrcweir           "rlwinm %0,%0,0,4,28\n\t"
104cdf0e10cSrcweir           "lwz 0,0(1)\n\t"
105cdf0e10cSrcweir           "subf 1,%0,1\n\t"
106cdf0e10cSrcweir           "stw 0,0(1)\n\t"
107cdf0e10cSrcweir           : : "r" (nStackLongs) : "0" );
108cdf0e10cSrcweir 
109cdf0e10cSrcweir      __asm__ __volatile__ ( "addi %0,1,8" : "=r" (p) : );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir      // never called
112cdf0e10cSrcweir      // if (! pAdjustedThisPtr ) dummy_can_throw_anything("xxx"); // address something
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 
115cdf0e10cSrcweir      // now begin to load the C++ function arguments into storage
116cdf0e10cSrcweir      n = 0;
117cdf0e10cSrcweir #ifndef __NO_FPRS__
118cdf0e10cSrcweir      f = 0;
119cdf0e10cSrcweir #endif
120cdf0e10cSrcweir 
121cdf0e10cSrcweir      // now we need to parse the entire signature string */
122cdf0e10cSrcweir      // until we get the END indicator */
123cdf0e10cSrcweir 
124cdf0e10cSrcweir      // treat complex return pointer like any other parameter //
125cdf0e10cSrcweir 
126cdf0e10cSrcweir #if 0
127cdf0e10cSrcweir      /* Let's figure out what is really going on here*/
128*07a3d7f1SPedro Giffuni      fprintf(stderr,"callVirtualMethod parameters string is %s\n",pPT);
129cdf0e10cSrcweir      int k = nStackLongs;
130cdf0e10cSrcweir      long * q = (long *)pStackLongs;
131cdf0e10cSrcweir      while (k > 0) {
132cdf0e10cSrcweir        fprintf(stderr,"uno stack is: %x\n",*q);
133cdf0e10cSrcweir        k--;
134cdf0e10cSrcweir        q++;
135cdf0e10cSrcweir      }
136cdf0e10cSrcweir #endif
137cdf0e10cSrcweir 
138cdf0e10cSrcweir      /* parse the argument list up to the ending ) */
139cdf0e10cSrcweir      while (*pPT != 'X') {
140cdf0e10cSrcweir        c = *pPT;
141cdf0e10cSrcweir        switch (c) {
142cdf0e10cSrcweir        case 'D':                   /* type is double */
143cdf0e10cSrcweir #ifndef __NO_FPRS__
144cdf0e10cSrcweir             if (f < 8) {
145cdf0e10cSrcweir                fpr[f++] = *((double *)pStackLongs);   /* store in register */
146cdf0e10cSrcweir #else
147cdf0e10cSrcweir             if (n & 1)
148cdf0e10cSrcweir                n++;
149cdf0e10cSrcweir             if (n < 8) {
150cdf0e10cSrcweir                gpr[n++] = *pStackLongs;
151cdf0e10cSrcweir                gpr[n++] = *(pStackLongs+1);
152cdf0e10cSrcweir #endif
153cdf0e10cSrcweir 	    } else {
154cdf0e10cSrcweir 	       if (((long) p) & 4)
155cdf0e10cSrcweir 	          p++;
156cdf0e10cSrcweir                *p++ = *pStackLongs;       /* or on the parameter stack */
157cdf0e10cSrcweir                *p++ = *(pStackLongs + 1);
158cdf0e10cSrcweir 	    }
159cdf0e10cSrcweir             pStackLongs += 2;
160cdf0e10cSrcweir             break;
161cdf0e10cSrcweir 
162cdf0e10cSrcweir        case 'F':                   /* type is float */
163cdf0e10cSrcweir 	 /* this assumes that floats are stored as 1 32 bit word on param
164cdf0e10cSrcweir 	    stack and that if passed in parameter stack to C, should be
165cdf0e10cSrcweir 	    as double word.
166cdf0e10cSrcweir 
167cdf0e10cSrcweir             Whoops: the abi is not actually followed by gcc, need to
168cdf0e10cSrcweir             store floats as a *single* word on outgoing parameter stack
169cdf0e10cSrcweir             to match what gcc actually does
170cdf0e10cSrcweir 	 */
171cdf0e10cSrcweir #ifndef __NO_FPRS__
172cdf0e10cSrcweir             if (f < 8) {
173cdf0e10cSrcweir                fpr[f++] = *((float *)pStackLongs);
174cdf0e10cSrcweir #else
175cdf0e10cSrcweir             if (n < 8) {
176cdf0e10cSrcweir                gpr[n++] = *pStackLongs;
177cdf0e10cSrcweir #endif
178cdf0e10cSrcweir 	    } else {
179cdf0e10cSrcweir #if 0 /* if abi were followed */
180cdf0e10cSrcweir 	       if (((long) p) & 4)
181cdf0e10cSrcweir 	          p++;
182cdf0e10cSrcweir 	       *((double *)p) = *((float *)pStackLongs);
183cdf0e10cSrcweir                p += 2;
184cdf0e10cSrcweir #else
185cdf0e10cSrcweir 	       *((float *)p) = *((float *)pStackLongs);
186cdf0e10cSrcweir                p += 1;
187cdf0e10cSrcweir #endif
188cdf0e10cSrcweir 	    }
189cdf0e10cSrcweir             pStackLongs += 1;
190cdf0e10cSrcweir             break;
191cdf0e10cSrcweir 
192cdf0e10cSrcweir        case 'H':                /* type is long long */
193cdf0e10cSrcweir             if (n & 1) n++; 	/* note even elements gpr[] will map to
194cdf0e10cSrcweir                                    odd registers*/
195cdf0e10cSrcweir             if (n <= 6) {
196cdf0e10cSrcweir                gpr[n++] = *pStackLongs;
197cdf0e10cSrcweir                gpr[n++] = *(pStackLongs+1);
198cdf0e10cSrcweir 	    } else {
199cdf0e10cSrcweir 	       if (((long) p) & 4)
200cdf0e10cSrcweir 	          p++;
201cdf0e10cSrcweir                *p++ = *pStackLongs;
202cdf0e10cSrcweir                *p++ = *(pStackLongs+1);
203cdf0e10cSrcweir 	    }
204cdf0e10cSrcweir             pStackLongs += 2;
205cdf0e10cSrcweir             break;
206cdf0e10cSrcweir 
207cdf0e10cSrcweir        case 'S':
208cdf0e10cSrcweir             if (n < 8) {
209cdf0e10cSrcweir                gpr[n++] = *((unsigned short*)pStackLongs);
210cdf0e10cSrcweir 	    } else {
211cdf0e10cSrcweir                *p++ = *((unsigned short *)pStackLongs);
212cdf0e10cSrcweir 	    }
213cdf0e10cSrcweir             pStackLongs += 1;
214cdf0e10cSrcweir             break;
215cdf0e10cSrcweir 
216cdf0e10cSrcweir        case 'B':
217cdf0e10cSrcweir             if (n < 8) {
218cdf0e10cSrcweir                gpr[n++] = *((char *)pStackLongs);
219cdf0e10cSrcweir 	    } else {
220cdf0e10cSrcweir                *p++ = *((char *)pStackLongs);
221cdf0e10cSrcweir 	    }
222cdf0e10cSrcweir             pStackLongs += 1;
223cdf0e10cSrcweir             break;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir        default:
226cdf0e10cSrcweir             if (n < 8) {
227cdf0e10cSrcweir                gpr[n++] = *pStackLongs;
228cdf0e10cSrcweir 	    } else {
229cdf0e10cSrcweir                *p++ = *pStackLongs;
230cdf0e10cSrcweir 	    }
231cdf0e10cSrcweir             pStackLongs += 1;
232cdf0e10cSrcweir             break;
233cdf0e10cSrcweir        }
234cdf0e10cSrcweir        pPT++;
235cdf0e10cSrcweir      }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir      /* figure out the address of the function we need to invoke */
238cdf0e10cSrcweir      off = nVtableIndex;
239cdf0e10cSrcweir      off = off * 4;                         // 4 bytes per slot
240cdf0e10cSrcweir      mfunc = *((unsigned long **)pAdjustedThisPtr);    // get the address of the vtable
241cdf0e10cSrcweir      mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
242cdf0e10cSrcweir      mfunc = *((unsigned long **)mfunc);                 // the function is stored at the address
243cdf0e10cSrcweir      ptr = (void (*)())mfunc;
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     /* Set up the machine registers and invoke the function */
246cdf0e10cSrcweir 
247cdf0e10cSrcweir     __asm__ __volatile__ (
248cdf0e10cSrcweir 		"lwz	3,	0(%0)\n\t"
249cdf0e10cSrcweir 		"lwz	4,	4(%0)\n\t"
250cdf0e10cSrcweir 		"lwz	5,	8(%0)\n\t"
251cdf0e10cSrcweir 		"lwz	6,	12(%0)\n\t"
252cdf0e10cSrcweir 		"lwz	7,	16(%0)\n\t"
253cdf0e10cSrcweir 		"lwz	8,	20(%0)\n\t"
254cdf0e10cSrcweir 		"lwz	9,	24(%0)\n\t"
255cdf0e10cSrcweir 		"lwz	10,	28(%0)\n\t"
256cdf0e10cSrcweir #ifndef __NO_FPRS__
257cdf0e10cSrcweir 		"lfd	1,	0(%1)\n\t"
258cdf0e10cSrcweir 		"lfd	2,	8(%1)\n\t"
259cdf0e10cSrcweir 		"lfd	3,	16(%1)\n\t"
260cdf0e10cSrcweir 		"lfd	4,	24(%1)\n\t"
261cdf0e10cSrcweir 		"lfd	5,	32(%1)\n\t"
262cdf0e10cSrcweir 		"lfd	6,	40(%1)\n\t"
263cdf0e10cSrcweir 		"lfd	7,	48(%1)\n\t"
264cdf0e10cSrcweir 		"lfd	8,	56(%1)\n\t"
265cdf0e10cSrcweir 	        : : "r" (gpr), "r" (fpr)
266cdf0e10cSrcweir #else
267cdf0e10cSrcweir 	        : : "r" (gpr)
268cdf0e10cSrcweir #endif
269cdf0e10cSrcweir 		: "0", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
270cdf0e10cSrcweir     );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir     (*ptr)();
273cdf0e10cSrcweir 
274cdf0e10cSrcweir     __asm__ __volatile__ (
275cdf0e10cSrcweir        "mr     %0,     3\n\t"
276cdf0e10cSrcweir        "mr     %1,     4\n\t"
277cdf0e10cSrcweir #ifndef __NO_FPRS__
278cdf0e10cSrcweir        "fmr    %2,     1\n\t"
279cdf0e10cSrcweir        : "=r" (iret), "=r" (iret2), "=f" (dret)
280cdf0e10cSrcweir #else
281cdf0e10cSrcweir        : "=r" (iret), "=r" (iret2)
282cdf0e10cSrcweir #endif
283cdf0e10cSrcweir        : );
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     switch( eReturnType )
286cdf0e10cSrcweir 	{
287cdf0e10cSrcweir 		case typelib_TypeClass_HYPER:
288cdf0e10cSrcweir 		case typelib_TypeClass_UNSIGNED_HYPER:
289cdf0e10cSrcweir 		        ((long*)pRegisterReturn)[0] = iret;
290cdf0e10cSrcweir 			((long*)pRegisterReturn)[1] = iret2;
291cdf0e10cSrcweir 		case typelib_TypeClass_LONG:
292cdf0e10cSrcweir 		case typelib_TypeClass_UNSIGNED_LONG:
293cdf0e10cSrcweir 		case typelib_TypeClass_ENUM:
294cdf0e10cSrcweir 			((long*)pRegisterReturn)[0] = iret;
295cdf0e10cSrcweir 			break;
296cdf0e10cSrcweir 		case typelib_TypeClass_CHAR:
297cdf0e10cSrcweir 		case typelib_TypeClass_SHORT:
298cdf0e10cSrcweir 		case typelib_TypeClass_UNSIGNED_SHORT:
299cdf0e10cSrcweir 		        *(unsigned short*)pRegisterReturn = (unsigned short)iret;
300cdf0e10cSrcweir 			break;
301cdf0e10cSrcweir 		case typelib_TypeClass_BOOLEAN:
302cdf0e10cSrcweir 		case typelib_TypeClass_BYTE:
303cdf0e10cSrcweir 		        *(unsigned char*)pRegisterReturn = (unsigned char)iret;
304cdf0e10cSrcweir 			break;
305cdf0e10cSrcweir 		case typelib_TypeClass_FLOAT:
306cdf0e10cSrcweir #ifndef __NO_FPRS__
307cdf0e10cSrcweir 		        *(float*)pRegisterReturn = (float)dret;
308cdf0e10cSrcweir #else
309cdf0e10cSrcweir 		        ((unsigned int*)pRegisterReturn)[0] = iret;
310cdf0e10cSrcweir #endif
311cdf0e10cSrcweir 			break;
312cdf0e10cSrcweir 		case typelib_TypeClass_DOUBLE:
313cdf0e10cSrcweir #ifndef __NO_FPRS__
314cdf0e10cSrcweir 			*(double*)pRegisterReturn = dret;
315cdf0e10cSrcweir #else
316cdf0e10cSrcweir 			((unsigned int*)pRegisterReturn)[0] = iret;
317cdf0e10cSrcweir 			((unsigned int*)pRegisterReturn)[1] = iret2;
318cdf0e10cSrcweir #endif
319cdf0e10cSrcweir 			break;
320cdf0e10cSrcweir 		default:
321cdf0e10cSrcweir 			break;
322cdf0e10cSrcweir 	}
323cdf0e10cSrcweir }
324cdf0e10cSrcweir 
325cdf0e10cSrcweir 
326cdf0e10cSrcweir //==================================================================================================
327cdf0e10cSrcweir static void cpp_call(
328cdf0e10cSrcweir 	bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
329cdf0e10cSrcweir 	bridges::cpp_uno::shared::VtableSlot  aVtableSlot,
330cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef,
331cdf0e10cSrcweir 	sal_Int32 nParams, typelib_MethodParameter * pParams,
332cdf0e10cSrcweir 	void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
333cdf0e10cSrcweir {
334cdf0e10cSrcweir   	// max space for: [complex ret ptr], values|ptr ...
335cdf0e10cSrcweir   	char * pCppStack		=
336cdf0e10cSrcweir   		(char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
337cdf0e10cSrcweir   	char * pCppStackStart	= pCppStack;
338cdf0e10cSrcweir 
339cdf0e10cSrcweir         // need to know parameter types for callVirtualMethod so generate a signature string
340cdf0e10cSrcweir         char * pParamType = (char *) alloca(nParams+2);
341cdf0e10cSrcweir         char * pPT = pParamType;
342cdf0e10cSrcweir 
343cdf0e10cSrcweir 	// return
344cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
345cdf0e10cSrcweir 	TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
346cdf0e10cSrcweir 	// OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
347cdf0e10cSrcweir 
348cdf0e10cSrcweir 	void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
349cdf0e10cSrcweir 
350cdf0e10cSrcweir 	if (pReturnTypeDescr)
351cdf0e10cSrcweir 	{
352cdf0e10cSrcweir 		if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
353cdf0e10cSrcweir 		{
354cdf0e10cSrcweir 			pCppReturn = pUnoReturn; // direct way for simple types
355cdf0e10cSrcweir 		}
356cdf0e10cSrcweir 		else
357cdf0e10cSrcweir 		{
358cdf0e10cSrcweir 			// complex return via ptr
359cdf0e10cSrcweir 			pCppReturn = *(void **)pCppStack =
360cdf0e10cSrcweir                               (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
361cdf0e10cSrcweir 			       ? alloca( pReturnTypeDescr->nSize ): pUnoReturn); // direct way
362cdf0e10cSrcweir                         *pPT++ = 'I'; //signify that a complex return type on stack
363cdf0e10cSrcweir 			pCppStack += sizeof(void *);
364cdf0e10cSrcweir 		}
365cdf0e10cSrcweir 	}
366cdf0e10cSrcweir 	// push this
367cdf0e10cSrcweir         void* pAdjustedThisPtr = reinterpret_cast< void **>(pThis->getCppI()) + aVtableSlot.offset;
368cdf0e10cSrcweir 	*(void**)pCppStack = pAdjustedThisPtr;
369cdf0e10cSrcweir 	pCppStack += sizeof( void* );
370cdf0e10cSrcweir         *pPT++ = 'I';
371cdf0e10cSrcweir 
372cdf0e10cSrcweir 	// stack space
373cdf0e10cSrcweir 	// OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
374cdf0e10cSrcweir 	// args
375cdf0e10cSrcweir 	void ** pCppArgs  = (void **)alloca( 3 * sizeof(void *) * nParams );
376cdf0e10cSrcweir 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
377cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
378cdf0e10cSrcweir 	// type descriptions for reconversions
379cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
380cdf0e10cSrcweir 
381cdf0e10cSrcweir 	sal_Int32 nTempIndizes   = 0;
382cdf0e10cSrcweir 
383cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
384cdf0e10cSrcweir 	{
385cdf0e10cSrcweir 		const typelib_MethodParameter & rParam = pParams[nPos];
386cdf0e10cSrcweir 		typelib_TypeDescription * pParamTypeDescr = 0;
387cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
388cdf0e10cSrcweir 
389cdf0e10cSrcweir 		if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
390cdf0e10cSrcweir 		{
391cdf0e10cSrcweir 			uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
392cdf0e10cSrcweir 									pThis->getBridge()->getUno2Cpp() );
393cdf0e10cSrcweir 
394cdf0e10cSrcweir 			switch (pParamTypeDescr->eTypeClass)
395cdf0e10cSrcweir 			{
396cdf0e10cSrcweir 
397cdf0e10cSrcweir                           // we need to know type of each param so that we know whether to use
398cdf0e10cSrcweir                           // gpr or fpr to pass in parameters:
399cdf0e10cSrcweir                           // Key: I - int, long, pointer, etc means pass in gpr
400cdf0e10cSrcweir                           //      B - byte value passed in gpr
401cdf0e10cSrcweir                           //      S - short value passed in gpr
402cdf0e10cSrcweir                           //      F - float value pass in fpr
403cdf0e10cSrcweir                           //      D - double value pass in fpr
404cdf0e10cSrcweir                           //      H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
405cdf0e10cSrcweir                           //      X - indicates end of parameter description string
406cdf0e10cSrcweir 
407cdf0e10cSrcweir 		          case typelib_TypeClass_LONG:
408cdf0e10cSrcweir 		          case typelib_TypeClass_UNSIGNED_LONG:
409cdf0e10cSrcweir 		          case typelib_TypeClass_ENUM:
410cdf0e10cSrcweir 			    *pPT++ = 'I';
411cdf0e10cSrcweir 			    break;
412cdf0e10cSrcweir  		          case typelib_TypeClass_SHORT:
413cdf0e10cSrcweir 		          case typelib_TypeClass_CHAR:
414cdf0e10cSrcweir 		          case typelib_TypeClass_UNSIGNED_SHORT:
415cdf0e10cSrcweir                             *pPT++ = 'S';
416cdf0e10cSrcweir                             break;
417cdf0e10cSrcweir 		          case typelib_TypeClass_BOOLEAN:
418cdf0e10cSrcweir 		          case typelib_TypeClass_BYTE:
419cdf0e10cSrcweir                             *pPT++ = 'B';
420cdf0e10cSrcweir                             break;
421cdf0e10cSrcweir 		          case typelib_TypeClass_FLOAT:
422cdf0e10cSrcweir                             *pPT++ = 'F';
423cdf0e10cSrcweir 			    break;
424cdf0e10cSrcweir 		        case typelib_TypeClass_DOUBLE:
425cdf0e10cSrcweir 			    *pPT++ = 'D';
426cdf0e10cSrcweir 			    pCppStack += sizeof(sal_Int32); // extra long
427cdf0e10cSrcweir 			    break;
428cdf0e10cSrcweir 			case typelib_TypeClass_HYPER:
429cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_HYPER:
430cdf0e10cSrcweir 			    *pPT++ = 'H';
431cdf0e10cSrcweir 			    pCppStack += sizeof(sal_Int32); // extra long
432cdf0e10cSrcweir 			default:
433cdf0e10cSrcweir 			    break;
434cdf0e10cSrcweir 			}
435cdf0e10cSrcweir 
436cdf0e10cSrcweir 			// no longer needed
437cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
438cdf0e10cSrcweir 		}
439cdf0e10cSrcweir 		else // ptr to complex value | ref
440cdf0e10cSrcweir 		{
441cdf0e10cSrcweir 			if (! rParam.bIn) // is pure out
442cdf0e10cSrcweir 			{
443cdf0e10cSrcweir 				// cpp out is constructed mem, uno out is not!
444cdf0e10cSrcweir 				uno_constructData(
445cdf0e10cSrcweir 					*(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
446cdf0e10cSrcweir 					pParamTypeDescr );
447cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
448cdf0e10cSrcweir 				// will be released at reconversion
449cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
450cdf0e10cSrcweir 			}
451cdf0e10cSrcweir 			// is in/inout
452cdf0e10cSrcweir 			else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
453cdf0e10cSrcweir 			{
454cdf0e10cSrcweir 				uno_copyAndConvertData(
455cdf0e10cSrcweir 					*(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
456cdf0e10cSrcweir 					pUnoArgs[nPos], pParamTypeDescr,
457cdf0e10cSrcweir                                         pThis->getBridge()->getUno2Cpp() );
458cdf0e10cSrcweir 
459cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
460cdf0e10cSrcweir 				// will be released at reconversion
461cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
462cdf0e10cSrcweir 			}
463cdf0e10cSrcweir 			else // direct way
464cdf0e10cSrcweir 			{
465cdf0e10cSrcweir 				*(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
466cdf0e10cSrcweir 				// no longer needed
467cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
468cdf0e10cSrcweir 			}
469cdf0e10cSrcweir                         // KBH: FIXME: is this the right way to pass these
470cdf0e10cSrcweir                         *pPT++='I';
471cdf0e10cSrcweir 		}
472cdf0e10cSrcweir 		pCppStack += sizeof(sal_Int32); // standard parameter length
473cdf0e10cSrcweir 	}
474cdf0e10cSrcweir 
475cdf0e10cSrcweir         // terminate the signature string
476cdf0e10cSrcweir         *pPT++='X';
477cdf0e10cSrcweir         *pPT=0;
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 	try
480cdf0e10cSrcweir 	{
481cdf0e10cSrcweir 		OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
482cdf0e10cSrcweir 		callVirtualMethod(
483cdf0e10cSrcweir 			pAdjustedThisPtr, aVtableSlot.index,
484cdf0e10cSrcweir 			pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
485cdf0e10cSrcweir 			(sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
486*07a3d7f1SPedro Giffuni 		// NO exception occurred...
487cdf0e10cSrcweir 		*ppUnoExc = 0;
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 		// reconvert temporary params
490cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
491cdf0e10cSrcweir 		{
492cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
493cdf0e10cSrcweir 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
494cdf0e10cSrcweir 
495cdf0e10cSrcweir 			if (pParams[nIndex].bIn)
496cdf0e10cSrcweir 			{
497cdf0e10cSrcweir 				if (pParams[nIndex].bOut) // inout
498cdf0e10cSrcweir 				{
499cdf0e10cSrcweir 					uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
500cdf0e10cSrcweir 					uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
501cdf0e10cSrcweir 											pThis->getBridge()->getCpp2Uno() );
502cdf0e10cSrcweir 				}
503cdf0e10cSrcweir 			}
504cdf0e10cSrcweir 			else // pure out
505cdf0e10cSrcweir 			{
506cdf0e10cSrcweir 				uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
507cdf0e10cSrcweir 										pThis->getBridge()->getCpp2Uno() );
508cdf0e10cSrcweir 			}
509cdf0e10cSrcweir 			// destroy temp cpp param => cpp: every param was constructed
510cdf0e10cSrcweir 			uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
511cdf0e10cSrcweir 
512cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
513cdf0e10cSrcweir 		}
514cdf0e10cSrcweir 		// return value
515cdf0e10cSrcweir 		if (pCppReturn && pUnoReturn != pCppReturn)
516cdf0e10cSrcweir 		{
517cdf0e10cSrcweir 			uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
518cdf0e10cSrcweir 									pThis->getBridge()->getCpp2Uno() );
519cdf0e10cSrcweir 			uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
520cdf0e10cSrcweir 		}
521cdf0e10cSrcweir 	}
522cdf0e10cSrcweir  	catch (...)
523cdf0e10cSrcweir  	{
524cdf0e10cSrcweir   		// fill uno exception
525cdf0e10cSrcweir 		fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
526cdf0e10cSrcweir                                   *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
527cdf0e10cSrcweir 
528cdf0e10cSrcweir 		// temporary params
529cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
530cdf0e10cSrcweir 		{
531cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
532cdf0e10cSrcweir 			// destroy temp cpp param => cpp: every param was constructed
533cdf0e10cSrcweir 			uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
534cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
535cdf0e10cSrcweir 		}
536cdf0e10cSrcweir 		// return type
537cdf0e10cSrcweir 		if (pReturnTypeDescr)
538cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
539cdf0e10cSrcweir 	}
540cdf0e10cSrcweir }
541cdf0e10cSrcweir 
542cdf0e10cSrcweir }
543cdf0e10cSrcweir 
544cdf0e10cSrcweir namespace bridges { namespace cpp_uno { namespace shared {
545cdf0e10cSrcweir 
546cdf0e10cSrcweir void unoInterfaceProxyDispatch(
547cdf0e10cSrcweir 	uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
548cdf0e10cSrcweir 	void * pReturn, void * pArgs[], uno_Any ** ppException )
549cdf0e10cSrcweir {
550cdf0e10cSrcweir 	// is my surrogate
551cdf0e10cSrcweir         bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
552cdf0e10cSrcweir             = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
553cdf0e10cSrcweir 
554cdf0e10cSrcweir 	switch (pMemberDescr->eTypeClass)
555cdf0e10cSrcweir 	{
556cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
557cdf0e10cSrcweir 	{
558cdf0e10cSrcweir 
559cdf0e10cSrcweir         VtableSlot aVtableSlot(
560cdf0e10cSrcweir             getVtableSlot(
561cdf0e10cSrcweir                 reinterpret_cast<
562cdf0e10cSrcweir                     typelib_InterfaceAttributeTypeDescription const * >(
563cdf0e10cSrcweir                         pMemberDescr)));
564cdf0e10cSrcweir 
565cdf0e10cSrcweir 		if (pReturn)
566cdf0e10cSrcweir 		{
567cdf0e10cSrcweir 			// dependent dispatch
568cdf0e10cSrcweir 			cpp_call(
569cdf0e10cSrcweir 				pThis, aVtableSlot,
570cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
571cdf0e10cSrcweir 				0, 0, // no params
572cdf0e10cSrcweir 				pReturn, pArgs, ppException );
573cdf0e10cSrcweir 		}
574cdf0e10cSrcweir 		else
575cdf0e10cSrcweir 		{
576cdf0e10cSrcweir 			// is SET
577cdf0e10cSrcweir 			typelib_MethodParameter aParam;
578cdf0e10cSrcweir 			aParam.pTypeRef =
579cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
580cdf0e10cSrcweir 			aParam.bIn		= sal_True;
581cdf0e10cSrcweir 			aParam.bOut		= sal_False;
582cdf0e10cSrcweir 
583cdf0e10cSrcweir 			typelib_TypeDescriptionReference * pReturnTypeRef = 0;
584cdf0e10cSrcweir 			OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
585cdf0e10cSrcweir 			typelib_typedescriptionreference_new(
586cdf0e10cSrcweir 				&pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
587cdf0e10cSrcweir 
588cdf0e10cSrcweir 			// dependent dispatch
589cdf0e10cSrcweir                         aVtableSlot.index += 1; //get then set method
590cdf0e10cSrcweir 			cpp_call(
591cdf0e10cSrcweir 				pThis, aVtableSlot,
592cdf0e10cSrcweir 				pReturnTypeRef,
593cdf0e10cSrcweir 				1, &aParam,
594cdf0e10cSrcweir 				pReturn, pArgs, ppException );
595cdf0e10cSrcweir 
596cdf0e10cSrcweir 			typelib_typedescriptionreference_release( pReturnTypeRef );
597cdf0e10cSrcweir 		}
598cdf0e10cSrcweir 
599cdf0e10cSrcweir 		break;
600cdf0e10cSrcweir 	}
601cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_METHOD:
602cdf0e10cSrcweir 	{
603cdf0e10cSrcweir 
604cdf0e10cSrcweir         VtableSlot aVtableSlot(
605cdf0e10cSrcweir             getVtableSlot(
606cdf0e10cSrcweir                 reinterpret_cast<
607cdf0e10cSrcweir                     typelib_InterfaceMethodTypeDescription const * >(
608cdf0e10cSrcweir                         pMemberDescr)));
609cdf0e10cSrcweir 		switch (aVtableSlot.index)
610cdf0e10cSrcweir 		{
611cdf0e10cSrcweir 			// standard calls
612cdf0e10cSrcweir 		case 1: // acquire uno interface
613cdf0e10cSrcweir 			(*pUnoI->acquire)( pUnoI );
614cdf0e10cSrcweir 			*ppException = 0;
615cdf0e10cSrcweir 			break;
616cdf0e10cSrcweir 		case 2: // release uno interface
617cdf0e10cSrcweir 			(*pUnoI->release)( pUnoI );
618cdf0e10cSrcweir 			*ppException = 0;
619cdf0e10cSrcweir 			break;
620cdf0e10cSrcweir 		case 0: // queryInterface() opt
621cdf0e10cSrcweir 		{
622cdf0e10cSrcweir 			typelib_TypeDescription * pTD = 0;
623cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
624cdf0e10cSrcweir 			if (pTD)
625cdf0e10cSrcweir 			{
626cdf0e10cSrcweir                 uno_Interface * pInterface = 0;
627cdf0e10cSrcweir                 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
628cdf0e10cSrcweir                     pThis->pBridge->getUnoEnv(),
629cdf0e10cSrcweir                     (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
630cdf0e10cSrcweir 
631cdf0e10cSrcweir                 if (pInterface)
632cdf0e10cSrcweir                 {
633cdf0e10cSrcweir                     ::uno_any_construct(
634cdf0e10cSrcweir                         reinterpret_cast< uno_Any * >( pReturn ),
635cdf0e10cSrcweir                         &pInterface, pTD, 0 );
636cdf0e10cSrcweir                     (*pInterface->release)( pInterface );
637cdf0e10cSrcweir                     TYPELIB_DANGER_RELEASE( pTD );
638cdf0e10cSrcweir                     *ppException = 0;
639cdf0e10cSrcweir                     break;
640cdf0e10cSrcweir                 }
641cdf0e10cSrcweir                 TYPELIB_DANGER_RELEASE( pTD );
642cdf0e10cSrcweir             }
643cdf0e10cSrcweir 		} // else perform queryInterface()
644cdf0e10cSrcweir 		default:
645cdf0e10cSrcweir 			// dependent dispatch
646cdf0e10cSrcweir 			cpp_call(
647cdf0e10cSrcweir 				pThis, aVtableSlot,
648cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
649cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
650cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
651cdf0e10cSrcweir 				pReturn, pArgs, ppException );
652cdf0e10cSrcweir 		}
653cdf0e10cSrcweir 		break;
654cdf0e10cSrcweir 	}
655cdf0e10cSrcweir 	default:
656cdf0e10cSrcweir 	{
657cdf0e10cSrcweir 		::com::sun::star::uno::RuntimeException aExc(
658cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
659cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
660cdf0e10cSrcweir 
661cdf0e10cSrcweir 		Type const & rExcType = ::getCppuType( &aExc );
662cdf0e10cSrcweir 		// binary identical null reference
663cdf0e10cSrcweir 		::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
664cdf0e10cSrcweir 	}
665cdf0e10cSrcweir 	}
666cdf0e10cSrcweir }
667cdf0e10cSrcweir 
668cdf0e10cSrcweir } } }
669