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