Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_linux_powerpc / uno2cpp.cxx
blob0f6f00421f13b0ff04355a0aa931c86d40ebb22a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uno2cpp.cxx,v $
10 * $Revision: 1.9 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
34 #include <malloc.h>
36 #include <com/sun/star/uno/genfunc.hxx>
37 #include <uno/data.h>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/types.hxx"
41 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/vtables.hxx"
44 #include "share.hxx"
47 using namespace ::rtl;
48 using namespace ::com::sun::star::uno;
50 namespace
54 //==================================================================================================
55 static void callVirtualMethod(
56 void * pAdjustedThisPtr,
57 sal_Int32 nVtableIndex,
58 void * pRegisterReturn,
59 typelib_TypeClass eReturnType,
60 char * pPT,
61 sal_Int32 * pStackLongs,
62 sal_Int32 nStackLongs)
65 // parameter list is mixed list of * and values
66 // reference parameters are pointers
68 // the basic idea here is to use gpr[8] as a storage area for
69 // the future values of registers r3 to r10 needed for the call,
70 // and similarly fpr[8] as a storage area for the future values
71 // of floating point registers f1 to f8
73 unsigned long * mfunc; // actual function to be invoked
74 void (*ptr)();
75 int gpr[8]; // storage for gpregisters, map to r3-r10
76 int off; // offset used to find function
77 double fpr[8]; // storage for fpregisters, map to f1-f8
78 int n; // number of gprs mapped so far
79 int f; // number of fprs mapped so far
80 long *p; // pointer to parameter overflow area
81 int c; // character of parameter type being decoded
82 double dret; // temporary function return values
83 int iret, iret2;
85 // Because of the Power PC calling conventions we could be passing
86 // parameters in both register types and on the stack. To create the
87 // stack parameter area we need we now simply allocate local
88 // variable storage param[] that is at least the size of the parameter stack
89 // (more than enough space) which we can overwrite the parameters into.
91 // Note: This keeps us from having to decode the signature twice and
92 // prevents problems with later local variables.
94 // Note: could require up to 2*nStackLongs words of parameter stack area
95 // if the call has many float parameters (i.e. floats take up only 1
96 // word on the stack but take 2 words in parameter area in the
97 // stack frame .
99 // Update! floats on the outgoing parameter stack only take up 1 word
100 // (stfs is used) which is not correct according to the ABI but we
101 // will match what the compiler does until this is figured out
103 // this grows the current stack to the appropriate size
104 // and sets the outgoing stack pointer p to the right place
105 __asm__ __volatile__ (
106 "rlwinm %0,%0,3,3,28\n\t"
107 "addi %0,%0,22\n\t"
108 "rlwinm %0,%0,0,4,28\n\t"
109 "lwz 0,0(1)\n\t"
110 "subf 1,%0,1\n\t"
111 "stw 0,0(1)\n\t"
112 : : "r" (nStackLongs) : "0" );
114 __asm__ __volatile__ ( "addi %0,1,8" : "=r" (p) : );
116 // never called
117 // if (! pAdjustedThisPtr ) dummy_can_throw_anything("xxx"); // address something
120 // now begin to load the C++ function arguments into storage
121 n = 0;
122 f = 0;
124 // now we need to parse the entire signature string */
125 // until we get the END indicator */
127 // treat complex return pointer like any other parameter //
129 #if 0
130 /* Let's figure out what is really going on here*/
131 fprintf(stderr,"callVirtualMethod paramters string is %s\n",pPT);
132 int k = nStackLongs;
133 long * q = (long *)pStackLongs;
134 while (k > 0) {
135 fprintf(stderr,"uno stack is: %x\n",*q);
136 k--;
137 q++;
139 #endif
141 /* parse the argument list up to the ending ) */
142 while (*pPT != 'X') {
143 c = *pPT;
144 switch (c) {
145 case 'D': /* type is double */
146 if (f < 8) {
147 fpr[f++] = *((double *)pStackLongs); /* store in register */
148 } else {
149 if (((long) p) & 4)
150 p++;
151 *p++ = *pStackLongs; /* or on the parameter stack */
152 *p++ = *(pStackLongs + 1);
154 pStackLongs += 2;
155 break;
157 case 'F': /* type is float */
158 /* this assumes that floats are stored as 1 32 bit word on param
159 stack and that if passed in parameter stack to C, should be
160 as double word.
162 Whoops: the abi is not actually followed by gcc, need to
163 store floats as a *single* word on outgoing parameter stack
164 to match what gcc actually does
166 if (f < 8) {
167 fpr[f++] = *((float *)pStackLongs);
168 } else {
169 #if 0 /* if abi were followed */
170 if (((long) p) & 4)
171 p++;
172 *((double *)p) = *((float *)pStackLongs);
173 p += 2;
174 #else
175 *((float *)p) = *((float *)pStackLongs);
176 p += 1;
177 #endif
179 pStackLongs += 1;
180 break;
182 case 'H': /* type is long long */
183 if (n & 1) n++; /* note even elements gpr[] will map to
184 odd registers*/
185 if (n <= 6) {
186 gpr[n++] = *pStackLongs;
187 gpr[n++] = *(pStackLongs+1);
188 } else {
189 if (((long) p) & 4)
190 p++;
191 *p++ = *pStackLongs;
192 *p++ = *(pStackLongs+1);
194 pStackLongs += 2;
195 break;
197 case 'S':
198 if (n < 8) {
199 gpr[n++] = *((unsigned short*)pStackLongs);
200 } else {
201 *p++ = *((unsigned short *)pStackLongs);
203 pStackLongs += 1;
204 break;
206 case 'B':
207 if (n < 8) {
208 gpr[n++] = *((char *)pStackLongs);
209 } else {
210 *p++ = *((char *)pStackLongs);
212 pStackLongs += 1;
213 break;
215 default:
216 if (n < 8) {
217 gpr[n++] = *pStackLongs;
218 } else {
219 *p++ = *pStackLongs;
221 pStackLongs += 1;
222 break;
224 pPT++;
227 /* figure out the address of the function we need to invoke */
228 off = nVtableIndex;
229 off = off * 4; // 4 bytes per slot
230 mfunc = *((unsigned long **)pAdjustedThisPtr); // get the address of the vtable
231 mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
232 mfunc = *((unsigned long **)mfunc); // the function is stored at the address
233 ptr = (void (*)())mfunc;
235 /* Set up the machine registers and invoke the function */
237 __asm__ __volatile__ (
238 "lwz 3, 0(%0)\n\t"
239 "lwz 4, 4(%0)\n\t"
240 "lwz 5, 8(%0)\n\t"
241 "lwz 6, 12(%0)\n\t"
242 "lwz 7, 16(%0)\n\t"
243 "lwz 8, 20(%0)\n\t"
244 "lwz 9, 24(%0)\n\t"
245 "lwz 10, 28(%0)\n\t"
246 "lfd 1, 0(%1)\n\t"
247 "lfd 2, 8(%1)\n\t"
248 "lfd 3, 16(%1)\n\t"
249 "lfd 4, 24(%1)\n\t"
250 "lfd 5, 32(%1)\n\t"
251 "lfd 6, 40(%1)\n\t"
252 "lfd 7, 48(%1)\n\t"
253 "lfd 8, 56(%1)\n\t"
254 : : "r" (gpr), "r" (fpr)
255 : "0", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
258 (*ptr)();
260 __asm__ __volatile__ (
261 "mr %1, 3\n\t"
262 "mr %2, 4\n\t"
263 "fmr %0, 1\n\t"
264 : "=f" (dret), "=r" (iret), "=r" (iret2) : );
266 switch( eReturnType )
268 case typelib_TypeClass_HYPER:
269 case typelib_TypeClass_UNSIGNED_HYPER:
270 ((long*)pRegisterReturn)[0] = iret;
271 ((long*)pRegisterReturn)[1] = iret2;
272 case typelib_TypeClass_LONG:
273 case typelib_TypeClass_UNSIGNED_LONG:
274 case typelib_TypeClass_ENUM:
275 ((long*)pRegisterReturn)[0] = iret;
276 break;
277 case typelib_TypeClass_CHAR:
278 case typelib_TypeClass_SHORT:
279 case typelib_TypeClass_UNSIGNED_SHORT:
280 *(unsigned short*)pRegisterReturn = (unsigned short)iret;
281 break;
282 case typelib_TypeClass_BOOLEAN:
283 case typelib_TypeClass_BYTE:
284 *(unsigned char*)pRegisterReturn = (unsigned char)iret;
285 break;
286 case typelib_TypeClass_FLOAT:
287 *(float*)pRegisterReturn = (float)dret;
288 break;
289 case typelib_TypeClass_DOUBLE:
290 *(double*)pRegisterReturn = dret;
291 break;
296 //==================================================================================================
297 static void cpp_call(
298 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
299 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
300 typelib_TypeDescriptionReference * pReturnTypeRef,
301 sal_Int32 nParams, typelib_MethodParameter * pParams,
302 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
304 // max space for: [complex ret ptr], values|ptr ...
305 char * pCppStack =
306 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
307 char * pCppStackStart = pCppStack;
309 // need to know parameter types for callVirtualMethod so generate a signature string
310 char * pParamType = (char *) alloca(nParams+2);
311 char * pPT = pParamType;
313 // return
314 typelib_TypeDescription * pReturnTypeDescr = 0;
315 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
316 // OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
318 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
320 if (pReturnTypeDescr)
322 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
324 pCppReturn = pUnoReturn; // direct way for simple types
326 else
328 // complex return via ptr
329 pCppReturn = *(void **)pCppStack =
330 (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
331 ? alloca( pReturnTypeDescr->nSize ): pUnoReturn); // direct way
332 *pPT++ = 'I'; //signify that a complex return type on stack
333 pCppStack += sizeof(void *);
336 // push this
337 void* pAdjustedThisPtr = reinterpret_cast< void **>(pThis->getCppI()) + aVtableSlot.offset;
338 *(void**)pCppStack = pAdjustedThisPtr;
339 pCppStack += sizeof( void* );
340 *pPT++ = 'I';
342 // stack space
343 // OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
344 // args
345 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
346 // indizes of values this have to be converted (interface conversion cpp<=>uno)
347 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
348 // type descriptions for reconversions
349 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
351 sal_Int32 nTempIndizes = 0;
353 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
355 const typelib_MethodParameter & rParam = pParams[nPos];
356 typelib_TypeDescription * pParamTypeDescr = 0;
357 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
359 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
361 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
362 pThis->getBridge()->getUno2Cpp() );
364 switch (pParamTypeDescr->eTypeClass)
367 // we need to know type of each param so that we know whether to use
368 // gpr or fpr to pass in parameters:
369 // Key: I - int, long, pointer, etc means pass in gpr
370 // B - byte value passed in gpr
371 // S - short value passed in gpr
372 // F - float value pass in fpr
373 // D - double value pass in fpr
374 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
375 // X - indicates end of parameter description string
377 case typelib_TypeClass_LONG:
378 case typelib_TypeClass_UNSIGNED_LONG:
379 case typelib_TypeClass_ENUM:
380 *pPT++ = 'I';
381 break;
382 case typelib_TypeClass_SHORT:
383 case typelib_TypeClass_CHAR:
384 case typelib_TypeClass_UNSIGNED_SHORT:
385 *pPT++ = 'S';
386 break;
387 case typelib_TypeClass_BOOLEAN:
388 case typelib_TypeClass_BYTE:
389 *pPT++ = 'B';
390 break;
391 case typelib_TypeClass_FLOAT:
392 *pPT++ = 'F';
393 break;
394 case typelib_TypeClass_DOUBLE:
395 *pPT++ = 'D';
396 pCppStack += sizeof(sal_Int32); // extra long
397 break;
398 case typelib_TypeClass_HYPER:
399 case typelib_TypeClass_UNSIGNED_HYPER:
400 *pPT++ = 'H';
401 pCppStack += sizeof(sal_Int32); // extra long
404 // no longer needed
405 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
407 else // ptr to complex value | ref
409 if (! rParam.bIn) // is pure out
411 // cpp out is constructed mem, uno out is not!
412 uno_constructData(
413 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
414 pParamTypeDescr );
415 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
416 // will be released at reconversion
417 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
419 // is in/inout
420 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
422 uno_copyAndConvertData(
423 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
424 pUnoArgs[nPos], pParamTypeDescr,
425 pThis->getBridge()->getUno2Cpp() );
427 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
428 // will be released at reconversion
429 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
431 else // direct way
433 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
434 // no longer needed
435 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
437 // KBH: FIXME: is this the right way to pass these
438 *pPT++='I';
440 pCppStack += sizeof(sal_Int32); // standard parameter length
443 // terminate the signature string
444 *pPT++='X';
445 *pPT=0;
449 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
450 callVirtualMethod(
451 pAdjustedThisPtr, aVtableSlot.index,
452 pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
453 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
454 // NO exception occured...
455 *ppUnoExc = 0;
457 // reconvert temporary params
458 for ( ; nTempIndizes--; )
460 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
461 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
463 if (pParams[nIndex].bIn)
465 if (pParams[nIndex].bOut) // inout
467 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
468 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
469 pThis->getBridge()->getCpp2Uno() );
472 else // pure out
474 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
475 pThis->getBridge()->getCpp2Uno() );
477 // destroy temp cpp param => cpp: every param was constructed
478 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
480 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
482 // return value
483 if (pCppReturn && pUnoReturn != pCppReturn)
485 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
486 pThis->getBridge()->getCpp2Uno() );
487 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
490 catch (...)
492 // fill uno exception
493 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
494 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
496 // temporary params
497 for ( ; nTempIndizes--; )
499 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
500 // destroy temp cpp param => cpp: every param was constructed
501 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
502 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
504 // return type
505 if (pReturnTypeDescr)
506 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
512 namespace bridges { namespace cpp_uno { namespace shared {
514 void unoInterfaceProxyDispatch(
515 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
516 void * pReturn, void * pArgs[], uno_Any ** ppException )
518 // is my surrogate
519 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
520 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
521 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
523 switch (pMemberDescr->eTypeClass)
525 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
528 VtableSlot aVtableSlot(
529 getVtableSlot(
530 reinterpret_cast<
531 typelib_InterfaceAttributeTypeDescription const * >(
532 pMemberDescr)));
534 if (pReturn)
536 // dependent dispatch
537 cpp_call(
538 pThis, aVtableSlot,
539 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
540 0, 0, // no params
541 pReturn, pArgs, ppException );
543 else
545 // is SET
546 typelib_MethodParameter aParam;
547 aParam.pTypeRef =
548 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
549 aParam.bIn = sal_True;
550 aParam.bOut = sal_False;
552 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
553 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
554 typelib_typedescriptionreference_new(
555 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
557 // dependent dispatch
558 aVtableSlot.index += 1; //get then set method
559 cpp_call(
560 pThis, aVtableSlot,
561 pReturnTypeRef,
562 1, &aParam,
563 pReturn, pArgs, ppException );
565 typelib_typedescriptionreference_release( pReturnTypeRef );
568 break;
570 case typelib_TypeClass_INTERFACE_METHOD:
573 VtableSlot aVtableSlot(
574 getVtableSlot(
575 reinterpret_cast<
576 typelib_InterfaceMethodTypeDescription const * >(
577 pMemberDescr)));
578 switch (aVtableSlot.index)
580 // standard calls
581 case 1: // acquire uno interface
582 (*pUnoI->acquire)( pUnoI );
583 *ppException = 0;
584 break;
585 case 2: // release uno interface
586 (*pUnoI->release)( pUnoI );
587 *ppException = 0;
588 break;
589 case 0: // queryInterface() opt
591 typelib_TypeDescription * pTD = 0;
592 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
593 if (pTD)
595 uno_Interface * pInterface = 0;
596 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
597 pThis->pBridge->getUnoEnv(),
598 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
600 if (pInterface)
602 ::uno_any_construct(
603 reinterpret_cast< uno_Any * >( pReturn ),
604 &pInterface, pTD, 0 );
605 (*pInterface->release)( pInterface );
606 TYPELIB_DANGER_RELEASE( pTD );
607 *ppException = 0;
608 break;
610 TYPELIB_DANGER_RELEASE( pTD );
612 } // else perform queryInterface()
613 default:
614 // dependent dispatch
615 cpp_call(
616 pThis, aVtableSlot,
617 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
618 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
619 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
620 pReturn, pArgs, ppException );
622 break;
624 default:
626 ::com::sun::star::uno::RuntimeException aExc(
627 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
628 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
630 Type const & rExcType = ::getCppuType( &aExc );
631 // binary identical null reference
632 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
637 } } }