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 #include <sal/alloca.h>
41 using namespace com::sun::star::uno
;
46 // The call instruction within the asm section of callVirtualMethod may throw
47 // exceptions. So that the compiler handles this correctly, it is important
48 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
49 // never happens at runtime), which in turn can throw exceptions, and (b)
50 // callVirtualMethod is not inlined at its call site (so that any exceptions are
51 // caught which are thrown from the instruction calling callVirtualMethod):
53 void callVirtualMethod( void * pAdjustedThisPtr
,
54 sal_Int32 nVtableIndex
,
55 void * pRegisterReturn
,
56 typelib_TypeClass eReturnType
,
57 sal_Int32
* pStackLongs
,
58 sal_Int32 nStackLongs
) __attribute__((noinline
));
60 void callVirtualMethod( void * pAdjustedThisPtr
,
61 sal_Int32 nVtableIndex
,
62 void * pRegisterReturn
,
63 typelib_TypeClass eReturnType
,
64 sal_Int32
* pStackLongs
,
65 sal_Int32 nStackLongs
)
67 // parameter list is mixed list of * and values
68 // reference parameters are pointers
70 assert( pStackLongs
&& pAdjustedThisPtr
);
71 static_assert( (sizeof(void *) == 4) &&
72 (sizeof(sal_Int32
) == 4), "### unexpected size of int!" );
73 assert(nStackLongs
&& pStackLongs
&& "### no stack in callVirtualMethod !");
76 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
78 long o0
, o1
; // for register returns
81 volatile long long saveReg
[7];
88 "add %%l0, 8, %%l0\n\t"
89 "std %%l2, [%%l0]\n\t"
90 "add %%l0, 8, %%l0\n\t"
91 "std %%l4, [%%l0]\n\t"
92 "add %%l0, 8, %%l0\n\t"
93 "std %%o0, [%%l0]\n\t"
94 "add %%l0, 8, %%l0\n\t"
95 "std %%o2, [%%l0]\n\t"
96 "add %%l0, 8, %%l0\n\t"
97 "std %%o4, [%%l0]\n\t"
98 "add %%l0, 8, %%l0\n\t"
99 "std %%l6, [%%l0]\n\t"
102 // increase our own stackframe if necessary
103 "mov %%sp, %%l3\n\t" // save stack ptr for readjustment
105 "subcc %%i5, 7, %%l0\n\t"
109 "sll %%l0, 2, %%l0\n\t"
110 "add %%l0, 96, %%l0\n\t"
111 "mov %%sp, %%l1\n\t" // old stack ptr
112 "sub %%sp, %%l0, %%l0\n\t" // future stack ptr
113 "andcc %%l0, 7, %%g0\n\t" // align stack to 8
116 "sub %%l0, 4, %%l0\n"
118 "mov %%l0, %%o5\n\t" // save newly computed stack ptr
119 "add %%g0, 16, %%o4\n"
121 // now copy longs down to save register window
122 // and local variables
124 "ld [%%l1], %%l2\n\t"
126 "add %%l0, 4, %%l0\n\t"
127 "add %%l1, 4, %%l1\n\t"
128 "subcc %%o4, 1, %%o4\n\t"
131 "mov %%o5, %%sp\n\t" // move new stack ptr (hopefully) atomically
132 // while register window is valid in both spaces
133 // (scheduling might hit in copyDown loop)
135 "sub %%i5, 7, %%l0\n\t" // copy parameters past the sixth to stack
136 "add %%i4, 28, %%l1\n\t"
137 "add %%sp, 92, %%l2\n"
139 "ld [%%l1], %%o0\n\t"
140 "st %%o0, [%%l2]\n\t"
141 "add %%l1, 4, %%l1\n\t"
142 "add %%l2, 4, %%l2\n\t"
143 "subcc %%l0, 1, %%l0\n\t"
148 "mov %%i5, %%l0\n\t" // prepare out registers
151 "ld [%%l1], %%o0\n\t" // prepare complex return ptr
152 "st %%o0, [%%sp+64]\n\t"
153 "sub %%l0, 1, %%l0\n\t"
154 "add %%l1, 4, %%l1\n\t"
156 "ld [%%l1], %%o0\n\t"
157 "subcc %%l0, 1, %%l0\n\t"
161 "add %%l1, 4, %%l1\n\t"
162 "ld [%%l1], %%o1\n\t"
163 "subcc %%l0, 1, %%l0\n\t"
167 "add %%l1, 4, %%l1\n\t"
168 "ld [%%l1], %%o2\n\t"
169 "subcc %%l0, 1, %%l0\n\t"
173 "add %%l1, 4, %%l1\n\t"
174 "ld [%%l1], %%o3\n\t"
175 "subcc %%l0, 1, %%l0\n\t"
179 "add %%l1, 4, %%l1\n\t"
180 "ld [%%l1], %%o4\n\t"
181 "subcc %%l0, 1, %%l0\n\t"
185 "add %%l1, 4, %%l1\n\t"
189 "ld [%%i0], %%l0\n\t" // get vtable ptr
191 "sll %%i1, 2, %%l6\n\t"
192 // "add %%l6, 8, %%l6\n\t"
193 "add %%l6, %%l0, %%l0\n\t"
194 // // vtable has 8byte wide entries,
195 // // upper half contains 2 half words, of which the first
196 // // is the this ptr patch !
197 // // first entry is (or __tf)
199 // "ldsh [%%l0], %%l6\n\t" // load this ptr patch
200 // "add %%l6, %%o0, %%o0\n\t" // patch this ptr
202 // "add %%l0, 4, %%l0\n\t" // get virtual function ptr
203 "ld [%%l0], %%l0\n\t"
205 "ld [%%i4], %%l2\n\t"
206 "subcc %%l2, %%g0, %%l2\n\t"
207 "bne .LcomplexCall\n\t"
211 "ba .LcallReturned\n\t"
218 ".LcallReturned:\n\t"
219 "mov %%l3, %%sp\n\t" // readjust stack so that our locals are where they belong
220 "st %%o0, %0\n\t" // save possible return registers into our locals
226 "ldd [%%l7], %%l0\n\t"
227 "add %%l7, 8, %%l7\n\t"
228 "ldd [%%l7], %%l2\n\t"
229 "add %%l7, 8, %%l7\n\t"
230 "ldd [%%l7], %%l4\n\t"
231 "add %%l7, 8, %%l7\n\t"
232 "ldd [%%l7], %%o0\n\t"
233 "add %%l7, 8, %%l7\n\t"
234 "ldd [%%l7], %%o2\n\t"
235 "add %%l7, 8, %%l7\n\t"
236 "ldd [%%l7], %%o4\n\t"
237 "add %%l7, 8, %%l7\n\t"
238 "ldd [%%l7], %%l6\n\t"
248 switch( eReturnType
)
250 case typelib_TypeClass_HYPER
:
251 case typelib_TypeClass_UNSIGNED_HYPER
:
252 ((long*)pRegisterReturn
)[1] = o1
;
253 case typelib_TypeClass_LONG
:
254 case typelib_TypeClass_UNSIGNED_LONG
:
255 case typelib_TypeClass_ENUM
:
256 ((long*)pRegisterReturn
)[0] = o0
;
258 case typelib_TypeClass_CHAR
:
259 case typelib_TypeClass_SHORT
:
260 case typelib_TypeClass_UNSIGNED_SHORT
:
261 *(unsigned short*)pRegisterReturn
= (unsigned short)o0
;
263 case typelib_TypeClass_BOOLEAN
:
264 case typelib_TypeClass_BYTE
:
265 *(unsigned char*)pRegisterReturn
= (unsigned char)o0
;
267 case typelib_TypeClass_FLOAT
:
268 *(float*)pRegisterReturn
= f0f
;
270 case typelib_TypeClass_DOUBLE
:
271 *(double*)pRegisterReturn
= f0d
;
276 static void cpp_call(
277 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
278 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
279 typelib_TypeDescriptionReference
* pReturnTypeRef
,
280 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
281 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
283 // max space for: complex ret ptr, this, values|ptr ...
285 (char *)alloca( (nParams
+2) * sizeof(sal_Int64
) );
286 char * pCppStackStart
= pCppStack
;
289 typelib_TypeDescription
* pReturnTypeDescr
= 0;
290 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
291 assert(pReturnTypeDescr
);
293 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
295 if (pReturnTypeDescr
)
297 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
299 pCppReturn
= pUnoReturn
; // direct way for simple types
300 *(void**)pCppStack
= NULL
;
304 // complex return via ptr
305 pCppReturn
= *(void **)pCppStack
= (bridges::cpp_uno::shared::relatesToInterfaceType(pReturnTypeDescr
)
306 ? alloca( pReturnTypeDescr
->nSize
)
307 : pUnoReturn
); // direct way
309 pCppStack
+= sizeof(void*);
312 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
313 + aVtableSlot
.offset
;
314 *(void**)pCppStack
= pAdjustedThisPtr
;
315 pCppStack
+= sizeof( void* );
318 static_assert(sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!");
320 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
321 // indices of values this have to be converted (interface conversion cpp<=>uno)
322 sal_Int32
* pTempIndices
= (sal_Int32
*)(pCppArgs
+ nParams
);
323 // type descriptions for reconversions
324 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
326 sal_Int32 nTempIndices
= 0;
328 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
330 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
331 typelib_TypeDescription
* pParamTypeDescr
= 0;
332 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
333 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
335 pCppArgs
[ nPos
] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack
, pParamTypeDescr
);
337 switch (pParamTypeDescr
->eTypeClass
)
339 case typelib_TypeClass_HYPER
:
340 case typelib_TypeClass_UNSIGNED_HYPER
:
341 case typelib_TypeClass_DOUBLE
:
342 static_assert(sizeof (double) == sizeof (sal_Int64
), "boo");
343 *reinterpret_cast< sal_Int32
* >(pCppStack
) =
344 *reinterpret_cast< sal_Int32
const * >(pUnoArgs
[ nPos
]);
345 pCppStack
+= sizeof (sal_Int32
);
346 *reinterpret_cast< sal_Int32
* >(pCppStack
) =
347 *(reinterpret_cast< sal_Int32
const * >(pUnoArgs
[ nPos
] ) + 1);
350 uno_copyAndConvertData(
351 pCppArgs
[nPos
], pUnoArgs
[nPos
], pParamTypeDescr
,
352 pThis
->getBridge()->getUno2Cpp() );
356 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
358 else // ptr to complex value | ref
360 if (! rParam
.bIn
) // is pure out
362 // cpp out is constructed mem, uno out is not!
364 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
366 pTempIndices
[nTempIndices
] = nPos
; // default constructed for cpp call
367 // will be released at reconversion
368 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
371 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
374 uno_copyAndConvertData(
375 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
376 pUnoArgs
[nPos
], pParamTypeDescr
,
377 pThis
->getBridge()->getUno2Cpp() );
379 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
380 // will be released at reconversion
381 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
385 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
387 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
390 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
395 int nStackLongs
= (pCppStack
- pCppStackStart
)/sizeof(sal_Int32
);
396 assert( !( (pCppStack
- pCppStackStart
) & 3) && "UNALIGNED STACK !!! (Please DO panic");
398 if( nStackLongs
& 1 )
399 // stack has to be 8 byte aligned
406 pReturnTypeDescr
->eTypeClass
,
407 (sal_Int32
*)pCppStackStart
,
409 } catch (css::uno::Exception
&) {
411 } catch (std::exception
& e
) {
412 throw css::uno::RuntimeException(
413 "C++ code threw " + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
414 + o3tl::runtimeToOUString(e
.what()));
416 throw css::uno::RuntimeException("C++ code threw unknown exception");
418 // NO exception occurred...
421 // reconvert temporary params
422 for ( ; nTempIndices
--; )
424 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
425 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
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 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
460 for ( ; nTempIndices
--; )
462 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
463 // destroy temp cpp param => cpp: every param was constructed
464 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], cpp_release
);
465 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
468 if (pReturnTypeDescr
)
469 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
475 namespace bridges::cpp_uno::shared
{
477 void unoInterfaceProxyDispatch(
478 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
479 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
481 #if defined BRIDGES_DEBUG
482 OString
cstr( OUStringToOString( pMemberDescr
->pTypeName
, RTL_TEXTENCODING_ASCII_US
) );
483 fprintf( stderr
, "received dispatch( %s )\n", cstr
.getStr() );
487 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
488 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
489 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
491 switch (pMemberDescr
->eTypeClass
)
493 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
495 VtableSlot
aVtableSlot(
498 typelib_InterfaceAttributeTypeDescription
const * >(
502 // dependent dispatch
505 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
507 pReturn
, pArgs
, ppException
);
512 typelib_MethodParameter aParam
;
514 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
515 aParam
.bIn
= sal_True
;
516 aParam
.bOut
= sal_False
;
518 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
519 OUString
aVoidName("void");
520 typelib_typedescriptionreference_new(
521 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
523 // dependent dispatch
524 aVtableSlot
.index
+= 1; // get, then set method
529 pReturn
, pArgs
, ppException
);
531 typelib_typedescriptionreference_release( pReturnTypeRef
);
536 case typelib_TypeClass_INTERFACE_METHOD
:
538 VtableSlot
aVtableSlot(
541 typelib_InterfaceMethodTypeDescription
const * >(
543 switch (aVtableSlot
.index
)
546 case 1: // acquire uno interface
547 (*pUnoI
->acquire
)( pUnoI
);
550 case 2: // release uno interface
551 (*pUnoI
->release
)( pUnoI
);
554 case 0: // queryInterface() opt
556 typelib_TypeDescription
* pTD
= 0;
557 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
560 uno_Interface
* pInterface
= 0;
561 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
562 pThis
->pBridge
->getUnoEnv(),
563 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
568 reinterpret_cast< uno_Any
* >( pReturn
),
569 &pInterface
, pTD
, 0 );
570 (*pInterface
->release
)( pInterface
);
571 TYPELIB_DANGER_RELEASE( pTD
);
575 TYPELIB_DANGER_RELEASE( pTD
);
577 } // else perform queryInterface()
579 // dependent dispatch
582 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
583 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
584 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
585 pReturn
, pArgs
, ppException
);
591 ::com::sun::star::uno::RuntimeException
aExc(
592 "illegal member type description!",
593 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
595 Type
const & rExcType
= cppu::UnoType
<decltype(aExc
)>::get();
596 // binary identical null reference
597 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
604 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */