1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
26 #include <com/sun/star/uno/Exception.hpp>
27 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <com/sun/star/uno/genfunc.hxx>
29 #include <o3tl/runtimetooustring.hxx>
34 #include "unointerfaceproxy.hxx"
35 #include "vtables.hxx"
39 using namespace com::sun::star::uno
;
43 // The call instruction within the asm section of callVirtualMethod may throw
44 // exceptions. So that the compiler handles this correctly, it is important
45 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
46 // never happens at runtime), which in turn can throw exceptions, and (b)
47 // callVirtualMethod is not inlined at its call site (so that any exceptions are
48 // caught which are thrown from the instruction calling callVirtualMethod):
50 void callVirtualMethod( void * pAdjustedThisPtr
,
51 sal_Int32 nVtableIndex
,
52 void * pRegisterReturn
,
53 typelib_TypeClass eReturnType
,
54 sal_Int32
* pStackLongs
,
55 sal_Int32 nStackLongs
) __attribute__((noinline
));
57 void callVirtualMethod( void * pAdjustedThisPtr
,
58 sal_Int32
/* nVtableIndex */,
59 void * pRegisterReturn
,
60 typelib_TypeClass eReturnType
,
61 #if OSL_DEBUG_LEVEL > 0
62 sal_Int32
* pStackLongs
,
63 sal_Int32 nStackLongs
)
65 sal_Int32
* /*pStackLongs*/,
66 sal_Int32
/*nStackLongs*/)
69 // parameter list is mixed list of * and values
70 // reference parameters are pointers
72 assert(pStackLongs
&& pAdjustedThisPtr
);
73 static_assert( (sizeof(void *) == 4) &&
74 (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
75 assert(nStackLongs
&& pStackLongs
&& "### no stack in callVirtualMethod !");
78 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
80 long o0
, o1
; // for register returns
83 volatile long long saveReg
[7];
90 "add %%l0, 8, %%l0\n\t"
91 "std %%l2, [%%l0]\n\t"
92 "add %%l0, 8, %%l0\n\t"
93 "std %%l4, [%%l0]\n\t"
94 "add %%l0, 8, %%l0\n\t"
95 "std %%o0, [%%l0]\n\t"
96 "add %%l0, 8, %%l0\n\t"
97 "std %%o2, [%%l0]\n\t"
98 "add %%l0, 8, %%l0\n\t"
99 "std %%o4, [%%l0]\n\t"
100 "add %%l0, 8, %%l0\n\t"
101 "std %%l6, [%%l0]\n\t"
104 // increase our own stackframe if necessary
105 "mov %%sp, %%l3\n\t" // save stack ptr for readjustment
107 "subcc %%i5, 7, %%l0\n\t"
111 "sll %%l0, 2, %%l0\n\t"
112 "add %%l0, 96, %%l0\n\t"
113 "mov %%sp, %%l1\n\t" // old stack ptr
114 "sub %%sp, %%l0, %%l0\n\t" // future stack ptr
115 "andcc %%l0, 7, %%g0\n\t" // align stack to 8
118 "sub %%l0, 4, %%l0\n"
120 "mov %%l0, %%o5\n\t" // save newly computed stack ptr
121 "add %%g0, 16, %%o4\n"
123 // now copy longs down to save register window
124 // and local variables
126 "ld [%%l1], %%l2\n\t"
128 "add %%l0, 4, %%l0\n\t"
129 "add %%l1, 4, %%l1\n\t"
130 "subcc %%o4, 1, %%o4\n\t"
133 "mov %%o5, %%sp\n\t" // move new stack ptr (hopefully) atomically
134 // while register window is valid in both spaces
135 // (scheduling might hit in copyDown loop)
137 "sub %%i5, 7, %%l0\n\t" // copy parameters past the sixth to stack
138 "add %%i4, 28, %%l1\n\t"
139 "add %%sp, 92, %%l2\n"
141 "ld [%%l1], %%o0\n\t"
142 "st %%o0, [%%l2]\n\t"
143 "add %%l1, 4, %%l1\n\t"
144 "add %%l2, 4, %%l2\n\t"
145 "subcc %%l0, 1, %%l0\n\t"
150 "mov %%i5, %%l0\n\t" // prepare out registers
153 "ld [%%l1], %%o0\n\t" // prepare complex return ptr
154 "st %%o0, [%%sp+64]\n\t"
155 "sub %%l0, 1, %%l0\n\t"
156 "add %%l1, 4, %%l1\n\t"
158 "ld [%%l1], %%o0\n\t"
159 "subcc %%l0, 1, %%l0\n\t"
163 "add %%l1, 4, %%l1\n\t"
164 "ld [%%l1], %%o1\n\t"
165 "subcc %%l0, 1, %%l0\n\t"
169 "add %%l1, 4, %%l1\n\t"
170 "ld [%%l1], %%o2\n\t"
171 "subcc %%l0, 1, %%l0\n\t"
175 "add %%l1, 4, %%l1\n\t"
176 "ld [%%l1], %%o3\n\t"
177 "subcc %%l0, 1, %%l0\n\t"
181 "add %%l1, 4, %%l1\n\t"
182 "ld [%%l1], %%o4\n\t"
183 "subcc %%l0, 1, %%l0\n\t"
187 "add %%l1, 4, %%l1\n\t"
191 "ld [%%i0], %%l0\n\t" // get vtable ptr
193 "sll %%i1, 2, %%l6\n\t"
194 // "add %%l6, 8, %%l6\n\t"
195 "add %%l6, %%l0, %%l0\n\t"
196 // // vtable has 8byte wide entries,
197 // // upper half contains 2 half words, of which the first
198 // // is the this ptr patch !
199 // // first entry is (or __tf)
201 // "ldsh [%%l0], %%l6\n\t" // load this ptr patch
202 // "add %%l6, %%o0, %%o0\n\t" // patch this ptr
204 // "add %%l0, 4, %%l0\n\t" // get virtual function ptr
205 "ld [%%l0], %%l0\n\t"
207 "ld [%%i4], %%l2\n\t"
208 "subcc %%l2, %%g0, %%l2\n\t"
209 "bne .LcomplexCall\n\t"
213 "ba .LcallReturned\n\t"
220 ".LcallReturned:\n\t"
221 "mov %%l3, %%sp\n\t" // readjust stack so that our locals are where they belong
222 "st %%o0, %0\n\t" // save possible return registers into our locals
228 "ldd [%%l7], %%l0\n\t"
229 "add %%l7, 8, %%l7\n\t"
230 "ldd [%%l7], %%l2\n\t"
231 "add %%l7, 8, %%l7\n\t"
232 "ldd [%%l7], %%l4\n\t"
233 "add %%l7, 8, %%l7\n\t"
234 "ldd [%%l7], %%o0\n\t"
235 "add %%l7, 8, %%l7\n\t"
236 "ldd [%%l7], %%o2\n\t"
237 "add %%l7, 8, %%l7\n\t"
238 "ldd [%%l7], %%o4\n\t"
239 "add %%l7, 8, %%l7\n\t"
240 "ldd [%%l7], %%l6\n\t"
251 switch( eReturnType
)
253 case typelib_TypeClass_HYPER
:
254 case typelib_TypeClass_UNSIGNED_HYPER
:
255 ((long*)pRegisterReturn
)[1] = o1
;
256 case typelib_TypeClass_LONG
:
257 case typelib_TypeClass_UNSIGNED_LONG
:
258 case typelib_TypeClass_ENUM
:
259 ((long*)pRegisterReturn
)[0] = o0
;
261 case typelib_TypeClass_CHAR
:
262 case typelib_TypeClass_SHORT
:
263 case typelib_TypeClass_UNSIGNED_SHORT
:
264 *(unsigned short*)pRegisterReturn
= (unsigned short)o0
;
266 case typelib_TypeClass_BOOLEAN
:
267 case typelib_TypeClass_BYTE
:
268 *(unsigned char*)pRegisterReturn
= (unsigned char)o0
;
270 case typelib_TypeClass_FLOAT
:
271 *(float*)pRegisterReturn
= f0f
;
273 case typelib_TypeClass_DOUBLE
:
274 *(double*)pRegisterReturn
= f0d
;
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 ...
290 (char *)alloca( (nParams
+2) * sizeof(sal_Int64
) );
291 char * pCppStackStart
= pCppStack
;
294 typelib_TypeDescription
* pReturnTypeDescr
= 0;
295 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
296 assert(pReturnTypeDescr
);
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
;
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*);
317 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
318 + aVtableSlot
.offset
;
319 *(void**)pCppStack
= pAdjustedThisPtr
;
320 pCppStack
+= sizeof( void* );
323 static_assert(sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!");
325 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
326 // indices of values this have to be converted (interface conversion cpp<=>uno)
327 sal_Int32
* pTempIndices
= (sal_Int32
*)(pCppArgs
+ nParams
);
328 // type descriptions for reconversions
329 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
331 sal_Int32 nTempIndices
= 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 static_assert(sizeof (double) == sizeof (sal_Int64
), "boo");
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);
355 uno_copyAndConvertData(
356 pCppArgs
[nPos
], pUnoArgs
[nPos
], pParamTypeDescr
,
357 pThis
->getBridge()->getUno2Cpp() );
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!
369 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
371 pTempIndices
[nTempIndices
] = nPos
; // default constructed for cpp call
372 // will be released at reconversion
373 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
376 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
379 uno_copyAndConvertData(
380 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
381 pUnoArgs
[nPos
], pParamTypeDescr
,
382 pThis
->getBridge()->getUno2Cpp() );
384 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
385 // will be released at reconversion
386 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
390 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
392 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
395 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
400 int nStackLongs
= (pCppStack
- pCppStackStart
)/sizeof(sal_Int32
);
401 assert( !( (pCppStack
- pCppStackStart
) & 3) && "UNALIGNED STACK !!! (Please DO panic" );
403 if( nStackLongs
& 1 )
404 // stack has to be 8 byte aligned
411 pReturnTypeDescr
->eTypeClass
,
412 (sal_Int32
*)pCppStackStart
,
414 } catch (css::uno::Exception
&) {
416 } catch (std::exception
& e
) {
417 throw css::uno::RuntimeException(
418 "C++ code threw " + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
419 + o3tl::runtimeToOUString(e
.what()));
421 throw css::uno::RuntimeException("C++ code threw unknown exception");
423 // NO exception occurred...
426 // reconvert temporary params
427 for ( ; nTempIndices
--; )
429 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
430 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
432 if (pParams
[nIndex
].bIn
)
434 if (pParams
[nIndex
].bOut
) // inout
436 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
437 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
438 pThis
->getBridge()->getCpp2Uno() );
443 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
444 pThis
->getBridge()->getCpp2Uno() );
446 // destroy temp cpp param => cpp: every param was constructed
447 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
449 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
452 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
454 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
455 pThis
->getBridge()->getCpp2Uno() );
456 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
462 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
465 for ( ; nTempIndices
--; )
467 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
468 // destroy temp cpp param => cpp: every param was constructed
469 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], cpp_release
);
470 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
473 if (pReturnTypeDescr
)
474 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
480 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
482 void unoInterfaceProxyDispatch(
483 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
484 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
486 #if defined BRIDGES_DEBUG
487 OString
cstr( OUStringToOString( pMemberDescr
->pTypeName
, RTL_TEXTENCODING_ASCII_US
) );
488 fprintf( stderr
, "received dispatch( %s )\n", cstr
.getStr() );
492 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
493 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
494 // typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
496 switch (pMemberDescr
->eTypeClass
)
498 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
500 VtableSlot
aVtableSlot(
503 typelib_InterfaceAttributeTypeDescription
const * >(
507 // dependent dispatch
510 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
512 pReturn
, pArgs
, ppException
);
517 typelib_MethodParameter aParam
;
519 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
520 aParam
.bIn
= sal_True
;
521 aParam
.bOut
= sal_False
;
523 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
524 OUString
aVoidName("void");
525 typelib_typedescriptionreference_new(
526 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
528 // dependent dispatch
529 aVtableSlot
.index
+= 1; // get, then set method
534 pReturn
, pArgs
, ppException
);
536 typelib_typedescriptionreference_release( pReturnTypeRef
);
541 case typelib_TypeClass_INTERFACE_METHOD
:
543 VtableSlot
aVtableSlot(
546 typelib_InterfaceMethodTypeDescription
const * >(
548 switch (aVtableSlot
.index
)
551 case 1: // acquire uno interface
552 (*pUnoI
->acquire
)( pUnoI
);
555 case 2: // release uno interface
556 (*pUnoI
->release
)( pUnoI
);
559 case 0: // queryInterface() opt
561 typelib_TypeDescription
* pTD
= 0;
562 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
565 uno_Interface
* pInterface
= 0;
566 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
567 pThis
->pBridge
->getUnoEnv(),
568 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
573 reinterpret_cast< uno_Any
* >( pReturn
),
574 &pInterface
, pTD
, 0 );
575 (*pInterface
->release
)( pInterface
);
576 TYPELIB_DANGER_RELEASE( pTD
);
580 TYPELIB_DANGER_RELEASE( pTD
);
582 } // else perform queryInterface()
584 // dependent dispatch
587 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
588 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
589 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
590 pReturn
, pArgs
, ppException
);
596 ::com::sun::star::uno::RuntimeException
aExc(
597 "illegal member type description!",
598 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
600 Type
const & rExcType
= cppu::UnoType
<decltype(aExc
)>::get();
601 // binary identical null reference
602 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
609 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */