1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uno2cpp.cxx,v $
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 <com/sun/star/uno/genfunc.hxx>
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"
45 using namespace com::sun::star::uno
;
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
)
72 sal_Int32
* /*pStackLongs*/,
73 sal_Int32
/*nStackLongs*/)
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 !" );
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];
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"
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"
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
125 "sub %%l0, 4, %%l0\n"
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
133 "ld [%%l1], %%l2\n\t"
135 "add %%l0, 4, %%l0\n\t"
136 "add %%l1, 4, %%l1\n\t"
137 "subcc %%o4, 1, %%o4\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"
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"
157 "mov %%i5, %%l0\n\t" // prepare out registers
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"
170 "add %%l1, 4, %%l1\n\t"
171 "ld [%%l1], %%o1\n\t"
172 "subcc %%l0, 1, %%l0\n\t"
176 "add %%l1, 4, %%l1\n\t"
177 "ld [%%l1], %%o2\n\t"
178 "subcc %%l0, 1, %%l0\n\t"
182 "add %%l1, 4, %%l1\n\t"
183 "ld [%%l1], %%o3\n\t"
184 "subcc %%l0, 1, %%l0\n\t"
188 "add %%l1, 4, %%l1\n\t"
189 "ld [%%l1], %%o4\n\t"
190 "subcc %%l0, 1, %%l0\n\t"
194 "add %%l1, 4, %%l1\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"
220 "ba .LcallReturned\n\t"
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
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"
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
;
265 case typelib_TypeClass_CHAR
:
266 case typelib_TypeClass_SHORT
:
267 case typelib_TypeClass_UNSIGNED_SHORT
:
268 *(unsigned short*)pRegisterReturn
= (unsigned short)o0
;
270 case typelib_TypeClass_BOOLEAN
:
271 case typelib_TypeClass_BYTE
:
272 *(unsigned char*)pRegisterReturn
= (unsigned char)o0
;
274 case typelib_TypeClass_FLOAT
:
275 *(float*)pRegisterReturn
= f0f
;
277 case typelib_TypeClass_DOUBLE
:
278 *(double*)pRegisterReturn
= f0d
;
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 ...
295 (char *)alloca( (nParams
+2) * sizeof(sal_Int64
) );
296 char * pCppStackStart
= pCppStack
;
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
;
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*);
322 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
323 + aVtableSlot
.offset
;
324 *(void**)pCppStack
= pAdjustedThisPtr
;
325 pCppStack
+= sizeof( void* );
328 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
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);
360 uno_copyAndConvertData(
361 pCppArgs
[nPos
], pUnoArgs
[nPos
], pParamTypeDescr
,
362 pThis
->getBridge()->getUno2Cpp() );
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!
374 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
376 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
377 // will be released at reconversion
378 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
381 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
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
;
395 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
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
415 pReturnTypeDescr
->eTypeClass
,
416 (sal_Int32
*)pCppStackStart
,
418 // NO exception occured...
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() );
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
);
447 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
449 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
450 pThis
->getBridge()->getCpp2Uno() );
451 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
457 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
458 *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
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
] );
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() );
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(
499 typelib_InterfaceAttributeTypeDescription
const * >(
503 // dependent dispatch
506 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
508 pReturn
, pArgs
, ppException
);
513 typelib_MethodParameter aParam
;
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
530 pReturn
, pArgs
, ppException
);
532 typelib_typedescriptionreference_release( pReturnTypeRef
);
537 case typelib_TypeClass_INTERFACE_METHOD
:
539 VtableSlot
aVtableSlot(
542 typelib_InterfaceMethodTypeDescription
const * >(
544 switch (aVtableSlot
.index
)
547 case 1: // acquire uno interface
548 (*pUnoI
->acquire
)( pUnoI
);
551 case 2: // release uno interface
552 (*pUnoI
->release
)( pUnoI
);
555 case 0: // queryInterface() opt
557 typelib_TypeDescription
* pTD
= 0;
558 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
561 uno_Interface
* pInterface
= 0;
562 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
563 pThis
->pBridge
->getUnoEnv(),
564 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
569 reinterpret_cast< uno_Any
* >( pReturn
),
570 &pInterface
, pTD
, 0 );
571 (*pInterface
->release
)( pInterface
);
572 TYPELIB_DANGER_RELEASE( pTD
);
576 TYPELIB_DANGER_RELEASE( pTD
);
578 } // else perform queryInterface()
580 // dependent dispatch
583 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
584 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
585 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
586 pReturn
, pArgs
, ppException
);
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 );