Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_solaris_sparc / uno2cpp.cxx
blobb2967f78dcdaf89d8f5e4c7e317c40f8558192a7
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.7 $
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"
33 #include <malloc.h>
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include <uno/data.h>
37 #include "bridges/cpp_uno/shared/bridge.hxx"
38 #include "bridges/cpp_uno/shared/types.hxx"
39 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
40 #include "bridges/cpp_uno/shared/vtables.hxx"
42 #include "share.hxx"
44 #include <sal/alloca.h>
46 using namespace rtl;
47 using namespace com::sun::star::uno;
49 namespace
51 //==================================================================================================
52 // The call instruction within the asm section of callVirtualMethod may throw
53 // exceptions. So that the compiler handles this correctly, it is important
54 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
55 // never happens at runtime), which in turn can throw exceptions, and (b)
56 // callVirtualMethod is not inlined at its call site (so that any exceptions are
57 // caught which are thrown from the instruction calling callVirtualMethod):
59 void callVirtualMethod( void * pAdjustedThisPtr,
60 sal_Int32 nVtableIndex,
61 void * pRegisterReturn,
62 typelib_TypeClass eReturnType,
63 sal_Int32 * pStackLongs,
64 sal_Int32 nStackLongs ) __attribute__((noinline));
66 void callVirtualMethod( void * pAdjustedThisPtr,
67 sal_Int32 nVtableIndex,
68 void * pRegisterReturn,
69 typelib_TypeClass eReturnType,
70 sal_Int32 * pStackLongs,
71 sal_Int32 nStackLongs )
73 // parameter list is mixed list of * and values
74 // reference parameters are pointers
76 OSL_ENSURE( pStackLongs && pAdjustedThisPtr, "### null ptr!" );
77 OSL_ENSURE( (sizeof(void *) == 4) &&
78 (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
79 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
81 // never called
82 if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
84 volatile long o0 = 0, o1 = 0; // for register returns
85 volatile double f0d = 0;
86 volatile float f0f = 0;
87 volatile long long saveReg[7];
89 __asm__ (
90 // save registers
91 "std %%l0, [%4]\n\t"
92 "mov %4, %%l0\n\t"
93 "mov %%l0, %%l1\n\t"
94 "add %%l0, 8, %%l0\n\t"
95 "std %%l2, [%%l0]\n\t"
96 "add %%l0, 8, %%l0\n\t"
97 "std %%l4, [%%l0]\n\t"
98 "add %%l0, 8, %%l0\n\t"
99 "std %%o0, [%%l0]\n\t"
100 "add %%l0, 8, %%l0\n\t"
101 "std %%o2, [%%l0]\n\t"
102 "add %%l0, 8, %%l0\n\t"
103 "std %%o4, [%%l0]\n\t"
104 "add %%l0, 8, %%l0\n\t"
105 "std %%l6, [%%l0]\n\t"
106 "mov %%l1, %%l7\n\t"
108 // increase our own stackframe if necessary
109 "mov %%sp, %%l3\n\t" // save stack ptr for readjustment
111 "subcc %%i5, 7, %%l0\n\t"
112 "ble .LmoveOn\n\t"
113 "nop\n\t"
115 "sll %%l0, 2, %%l0\n\t"
116 "add %%l0, 96, %%l0\n\t"
117 "mov %%sp, %%l1\n\t" // old stack ptr
118 "sub %%sp, %%l0, %%l0\n\t" // future stack ptr
119 "andcc %%l0, 7, %%g0\n\t" // align stack to 8
120 "be .LisAligned\n\t"
121 "nop\n\t"
122 "sub %%l0, 4, %%l0\n"
123 ".LisAligned:\n\t"
124 "mov %%l0, %%o5\n\t" // save newly computed stack ptr
125 "add %%g0, 16, %%o4\n"
127 // now copy longs down to save register window
128 // and local variables
129 ".LcopyDown:\n\t"
130 "ld [%%l1], %%l2\n\t"
131 "st %%l2,[%%l0]\n\t"
132 "add %%l0, 4, %%l0\n\t"
133 "add %%l1, 4, %%l1\n\t"
134 "subcc %%o4, 1, %%o4\n\t"
135 "bne .LcopyDown\n\t"
137 "mov %%o5, %%sp\n\t" // move new stack ptr (hopefully) atomically
138 // while register window is valid in both spaces
139 // (scheduling might hit in copyDown loop)
141 "sub %%i5, 7, %%l0\n\t" // copy parameters past the sixth to stack
142 "add %%i4, 28, %%l1\n\t"
143 "add %%sp, 92, %%l2\n"
144 ".LcopyLong:\n\t"
145 "ld [%%l1], %%o0\n\t"
146 "st %%o0, [%%l2]\n\t"
147 "add %%l1, 4, %%l1\n\t"
148 "add %%l2, 4, %%l2\n\t"
149 "subcc %%l0, 1, %%l0\n\t"
150 "bne .LcopyLong\n\t"
151 "nop\n"
153 ".LmoveOn:\n\t"
154 "mov %%i5, %%l0\n\t" // prepare out registers
155 "mov %%i4, %%l1\n\t"
157 "ld [%%l1], %%o0\n\t" // prepare complex return ptr
158 "st %%o0, [%%sp+64]\n\t"
159 "sub %%l0, 1, %%l0\n\t"
160 "add %%l1, 4, %%l1\n\t"
162 "ld [%%l1], %%o0\n\t"
163 "subcc %%l0, 1, %%l0\n\t"
164 "be .LdoCall\n\t"
165 "nop\n\t"
167 "add %%l1, 4, %%l1\n\t"
168 "ld [%%l1], %%o1\n\t"
169 "subcc %%l0, 1, %%l0\n\t"
170 "be .LdoCall\n\t"
171 "nop\n\t"
173 "add %%l1, 4, %%l1\n\t"
174 "ld [%%l1], %%o2\n\t"
175 "subcc %%l0, 1, %%l0\n\t"
176 "be .LdoCall\n\t"
177 "nop\n\t"
179 "add %%l1, 4, %%l1\n\t"
180 "ld [%%l1], %%o3\n\t"
181 "subcc %%l0, 1, %%l0\n\t"
182 "be .LdoCall\n\t"
183 "nop\n\t"
185 "add %%l1, 4, %%l1\n\t"
186 "ld [%%l1], %%o4\n\t"
187 "subcc %%l0, 1, %%l0\n\t"
188 "be .LdoCall\n\t"
189 "nop\n\t"
191 "add %%l1, 4, %%l1\n\t"
192 "ld [%%l1], %%o5\n"
194 ".LdoCall:\n\t"
195 "ld [%%i0], %%l0\n\t" // get vtable ptr
197 "sll %%i1, 2, %%l6\n\t"
198 // "add %%l6, 8, %%l6\n\t"
199 "add %%l6, %%l0, %%l0\n\t"
200 // // vtable has 8byte wide entries,
201 // // upper half contains 2 half words, of which the first
202 // // is the this ptr patch !
203 // // first entry is (or __tf)
205 // "ldsh [%%l0], %%l6\n\t" // load this ptr patch
206 // "add %%l6, %%o0, %%o0\n\t" // patch this ptr
208 // "add %%l0, 4, %%l0\n\t" // get virtual function ptr
209 "ld [%%l0], %%l0\n\t"
211 "ld [%%i4], %%l2\n\t"
212 "subcc %%l2, %%g0, %%l2\n\t"
213 "bne .LcomplexCall\n\t"
214 "nop\n\t"
215 "call %%l0\n\t"
216 "nop\n\t"
217 "ba .LcallReturned\n\t"
218 "nop\n"
219 ".LcomplexCall:\n\t"
220 "call %%l0\n\t"
221 "nop\n\t"
222 "unimp\n"
224 ".LcallReturned:\n\t"
225 "mov %%l3, %%sp\n\t" // readjust stack so that our locals are where they belong
226 "st %%o0, %0\n\t" // save possible return registers into our locals
227 "st %%o1, %1\n\t"
228 "std %%f0, %2\n\t"
229 "st %%f0, %3\n\t"
231 // restore registers
232 "ldd [%%l7], %%l0\n\t"
233 "add %%l7, 8, %%l7\n\t"
234 "ldd [%%l7], %%l2\n\t"
235 "add %%l7, 8, %%l7\n\t"
236 "ldd [%%l7], %%l4\n\t"
237 "add %%l7, 8, %%l7\n\t"
238 "ldd [%%l7], %%o0\n\t"
239 "add %%l7, 8, %%l7\n\t"
240 "ldd [%%l7], %%o2\n\t"
241 "add %%l7, 8, %%l7\n\t"
242 "ldd [%%l7], %%o4\n\t"
243 "add %%l7, 8, %%l7\n\t"
244 "ldd [%%l7], %%l6\n\t"
246 "m"(o0),
247 "m"(o1),
248 "m"(f0d),
249 "m"(f0f),
250 "r"(&saveReg[0])
252 switch( eReturnType )
254 case typelib_TypeClass_HYPER:
255 case typelib_TypeClass_UNSIGNED_HYPER:
256 ((long*)pRegisterReturn)[1] = o1;
257 case typelib_TypeClass_LONG:
258 case typelib_TypeClass_UNSIGNED_LONG:
259 case typelib_TypeClass_ENUM:
260 ((long*)pRegisterReturn)[0] = o0;
261 break;
262 case typelib_TypeClass_CHAR:
263 case typelib_TypeClass_SHORT:
264 case typelib_TypeClass_UNSIGNED_SHORT:
265 *(unsigned short*)pRegisterReturn = (unsigned short)o0;
266 break;
267 case typelib_TypeClass_BOOLEAN:
268 case typelib_TypeClass_BYTE:
269 *(unsigned char*)pRegisterReturn = (unsigned char)o0;
270 break;
271 case typelib_TypeClass_FLOAT:
272 *(float*)pRegisterReturn = f0f;
273 break;
274 case typelib_TypeClass_DOUBLE:
275 *(double*)pRegisterReturn = f0d;
276 break;
280 //=================================================================================================
281 static void cpp_call(
282 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
283 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
284 typelib_TypeDescriptionReference * pReturnTypeRef,
285 sal_Int32 nParams, typelib_MethodParameter * pParams,
286 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
288 // max space for: complex ret ptr, this, values|ptr ...
289 char * pCppStack =
290 (char *)alloca( (nParams+2) * sizeof(sal_Int64) );
291 char * pCppStackStart = pCppStack;
293 // return
294 typelib_TypeDescription * pReturnTypeDescr = 0;
295 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
296 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
298 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
300 if (pReturnTypeDescr)
302 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
304 pCppReturn = pUnoReturn; // direct way for simple types
305 *(void**)pCppStack = NULL;
307 else
309 // complex return via ptr
310 pCppReturn = *(void **)pCppStack = (bridges::cpp_uno::shared::relatesToInterfaceType(pReturnTypeDescr )
311 ? alloca( pReturnTypeDescr->nSize )
312 : pUnoReturn); // direct way
314 pCppStack += sizeof(void*);
316 // push this
317 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
318 + aVtableSlot.offset;
319 *(void**)pCppStack = pAdjustedThisPtr;
320 pCppStack += sizeof( void* );
322 // stack space
323 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
324 // args
325 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
326 // indizes of values this have to be converted (interface conversion cpp<=>uno)
327 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
328 // type descriptions for reconversions
329 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
331 sal_Int32 nTempIndizes = 0;
333 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
335 const typelib_MethodParameter & rParam = pParams[nPos];
336 typelib_TypeDescription * pParamTypeDescr = 0;
337 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
338 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
340 pCppArgs[ nPos ] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack, pParamTypeDescr );
342 switch (pParamTypeDescr->eTypeClass)
344 case typelib_TypeClass_HYPER:
345 case typelib_TypeClass_UNSIGNED_HYPER:
346 case typelib_TypeClass_DOUBLE:
347 OSL_ASSERT( sizeof (double) == sizeof (sal_Int64) );
348 *reinterpret_cast< sal_Int32 * >(pCppStack) =
349 *reinterpret_cast< sal_Int32 const * >(pUnoArgs[ nPos ]);
350 pCppStack += sizeof (sal_Int32);
351 *reinterpret_cast< sal_Int32 * >(pCppStack) =
352 *(reinterpret_cast< sal_Int32 const * >(pUnoArgs[ nPos ] ) + 1);
353 break;
354 default:
355 uno_copyAndConvertData(
356 pCppArgs[nPos], pUnoArgs[nPos], pParamTypeDescr,
357 pThis->getBridge()->getUno2Cpp() );
358 break;
360 // no longer needed
361 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
363 else // ptr to complex value | ref
365 if (! rParam.bIn) // is pure out
367 // cpp out is constructed mem, uno out is not!
368 uno_constructData(
369 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
370 pParamTypeDescr );
371 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
372 // will be released at reconversion
373 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
375 // is in/inout
376 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
377 pParamTypeDescr ))
379 uno_copyAndConvertData(
380 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
381 pUnoArgs[nPos], pParamTypeDescr,
382 pThis->getBridge()->getUno2Cpp() );
384 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
385 // will be released at reconversion
386 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
388 else // direct way
390 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
391 // no longer needed
392 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
395 pCppStack += sizeof(sal_Int32); // standard parameter length
400 int nStackLongs = (pCppStack - pCppStackStart)/sizeof(sal_Int32);
401 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic" );
403 if( nStackLongs & 1 )
404 // stack has to be 8 byte aligned
405 nStackLongs++;
406 callVirtualMethod(
407 pAdjustedThisPtr,
408 aVtableSlot.index,
409 pCppReturn,
410 pReturnTypeDescr->eTypeClass,
411 (sal_Int32 *)pCppStackStart,
412 nStackLongs);
413 // NO exception occured...
414 *ppUnoExc = 0;
416 // reconvert temporary params
417 for ( ; nTempIndizes--; )
419 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
420 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
422 if (pParams[nIndex].bIn)
424 if (pParams[nIndex].bOut) // inout
426 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
427 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
428 pThis->getBridge()->getCpp2Uno() );
431 else // pure out
433 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
434 pThis->getBridge()->getCpp2Uno() );
436 // destroy temp cpp param => cpp: every param was constructed
437 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
439 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
441 // return value
442 if (pCppReturn && pUnoReturn != pCppReturn)
444 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
445 pThis->getBridge()->getCpp2Uno() );
446 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
449 catch( ... )
451 // get exception
452 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
453 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
455 // temporary params
456 for ( ; nTempIndizes--; )
458 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
459 // destroy temp cpp param => cpp: every param was constructed
460 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
461 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
463 // return type
464 if (pReturnTypeDescr)
465 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
471 namespace bridges { namespace cpp_uno { namespace shared {
473 void unoInterfaceProxyDispatch(
474 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
475 void * pReturn, void * pArgs[], uno_Any ** ppException )
477 #if defined BRIDGES_DEBUG
478 OString cstr( OUStringToOString( pMemberDescr->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
479 fprintf( stderr, "received dispatch( %s )\n", cstr.getStr() );
480 #endif
482 // is my surrogate
483 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
484 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
485 typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
487 switch (pMemberDescr->eTypeClass)
489 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
491 VtableSlot aVtableSlot(
492 getVtableSlot(
493 reinterpret_cast<
494 typelib_InterfaceAttributeTypeDescription const * >(
495 pMemberDescr)));
496 if (pReturn)
498 // dependent dispatch
499 cpp_call(
500 pThis, aVtableSlot,
501 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
502 0, 0, // no params
503 pReturn, pArgs, ppException );
505 else
507 // is SET
508 typelib_MethodParameter aParam;
509 aParam.pTypeRef =
510 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
511 aParam.bIn = sal_True;
512 aParam.bOut = sal_False;
514 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
515 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
516 typelib_typedescriptionreference_new(
517 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
519 // dependent dispatch
520 aVtableSlot.index += 1; // get, then set method
521 cpp_call(
522 pThis, aVtableSlot,
523 pReturnTypeRef,
524 1, &aParam,
525 pReturn, pArgs, ppException );
527 typelib_typedescriptionreference_release( pReturnTypeRef );
530 break;
532 case typelib_TypeClass_INTERFACE_METHOD:
534 VtableSlot aVtableSlot(
535 getVtableSlot(
536 reinterpret_cast<
537 typelib_InterfaceMethodTypeDescription const * >(
538 pMemberDescr)));
539 switch (aVtableSlot.index)
541 // standard calls
542 case 1: // acquire uno interface
543 (*pUnoI->acquire)( pUnoI );
544 *ppException = 0;
545 break;
546 case 2: // release uno interface
547 (*pUnoI->release)( pUnoI );
548 *ppException = 0;
549 break;
550 case 0: // queryInterface() opt
552 typelib_TypeDescription * pTD = 0;
553 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
554 if (pTD)
556 uno_Interface * pInterface = 0;
557 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
558 pThis->pBridge->getUnoEnv(),
559 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
561 if (pInterface)
563 ::uno_any_construct(
564 reinterpret_cast< uno_Any * >( pReturn ),
565 &pInterface, pTD, 0 );
566 (*pInterface->release)( pInterface );
567 TYPELIB_DANGER_RELEASE( pTD );
568 *ppException = 0;
569 break;
571 TYPELIB_DANGER_RELEASE( pTD );
573 } // else perform queryInterface()
574 default:
575 // dependent dispatch
576 cpp_call(
577 pThis, aVtableSlot,
578 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
579 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
580 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
581 pReturn, pArgs, ppException );
583 break;
585 default:
587 ::com::sun::star::uno::RuntimeException aExc(
588 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
589 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
591 Type const & rExcType = ::getCppuType( &aExc );
592 // binary identical null reference
593 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
598 } } }