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