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 .
22 #include <sal/config.h>
27 #include <com/sun/star/uno/Exception.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <o3tl/runtimetooustring.hxx>
33 #include "unointerfaceproxy.hxx"
34 #include "vtables.hxx"
38 using namespace ::com::sun::star::uno
;
42 bool is_hfa_struct(const typelib_TypeDescription
* type
)
44 const typelib_CompoundTypeDescription
* p
45 = reinterpret_cast< const typelib_CompoundTypeDescription
* >(type
);
48 for (sal_Int32 i
= 0; i
< p
->nMembers
; ++i
)
50 if ((p
->ppTypeRefs
[i
]->eTypeClass
!= typelib_TypeClass_FLOAT
&&
51 p
->ppTypeRefs
[i
]->eTypeClass
!= typelib_TypeClass_DOUBLE
) ||
52 p
->ppTypeRefs
[i
]->eTypeClass
!= p
->ppTypeRefs
[0]->eTypeClass
)
58 bool return_in_x8( typelib_TypeDescriptionReference
*pTypeRef
)
60 if (bridges::cpp_uno::shared::isSimpleType(pTypeRef
))
62 else if (pTypeRef
->eTypeClass
== typelib_TypeClass_STRUCT
|| pTypeRef
->eTypeClass
== typelib_TypeClass_EXCEPTION
)
64 typelib_TypeDescription
* pTypeDescr
= 0;
65 TYPELIB_DANGER_GET( &pTypeDescr
, pTypeRef
);
67 // A Composite Type not larger than 16 bytes is returned in x0, x1
68 bool bRet
= pTypeDescr
->nSize
> 16;
70 if (is_hfa_struct(pTypeDescr
))
73 TYPELIB_DANGER_RELEASE( pTypeDescr
);
80 void MapReturn(sal_uInt64 x0
, sal_uInt64 x1
, typelib_TypeDescriptionReference
*pReturnType
, sal_uInt64
*pRegisterReturn
)
82 switch( pReturnType
->eTypeClass
)
84 case typelib_TypeClass_HYPER
:
85 case typelib_TypeClass_UNSIGNED_HYPER
:
86 pRegisterReturn
[1] = x1
;
88 case typelib_TypeClass_LONG
:
89 case typelib_TypeClass_UNSIGNED_LONG
:
90 case typelib_TypeClass_ENUM
:
91 case typelib_TypeClass_CHAR
:
92 case typelib_TypeClass_SHORT
:
93 case typelib_TypeClass_UNSIGNED_SHORT
:
94 case typelib_TypeClass_BOOLEAN
:
95 case typelib_TypeClass_BYTE
:
96 pRegisterReturn
[0] = x0
;
98 case typelib_TypeClass_FLOAT
:
99 register float fret
asm("s0");
100 #pragma GCC diagnostic push
101 #pragma GCC diagnostic ignored "-Wuninitialized"
102 *(float*)pRegisterReturn
= fret
;
103 #pragma GCC diagnostic pop
105 case typelib_TypeClass_DOUBLE
:
106 register double dret
asm("d0");
107 #pragma GCC diagnostic push
108 #pragma GCC diagnostic ignored "-Wuninitialized"
109 *(double*)pRegisterReturn
= dret
;
110 #pragma GCC diagnostic pop
112 case typelib_TypeClass_STRUCT
:
113 case typelib_TypeClass_EXCEPTION
:
114 if (!arm::return_in_x8(pReturnType
))
116 pRegisterReturn
[0] = x0
;
117 pRegisterReturn
[1] = x1
;
127 void callVirtualMethod(
129 sal_Int32 nVtableIndex
,
130 void *pRegisterReturn
,
131 typelib_TypeDescriptionReference
*pReturnType
,
139 CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
144 sal_uInt32 nStackBytes
= ( ( nStack
+ 3 ) >> 2 ) * 16;
145 sal_uInt32
*stack
= (sal_uInt32
*) alloca( nStackBytes
);
146 memcpy( stack
, pStack
, nStackBytes
);
149 sal_uInt64 pMethod
= *((sal_uInt64
*)pThis
);
150 pMethod
+= 8 * nVtableIndex
;
151 pMethod
= *((sal_uInt64
*)pMethod
);
153 // For value returned in registers
159 " ldp x0, x1, %[pgpr_0]\n"
160 " ldp x2, x3, %[pgpr_2]\n"
161 " ldp x4, x5, %[pgpr_4]\n"
162 " ldp x6, x7, %[pgpr_6]\n"
163 " ldr x8, %[pregisterreturn]\n"
164 " ldp d0, d1, %[pfpr_0]\n"
165 " ldp d2, d3, %[pfpr_2]\n"
166 " ldp d4, d5, %[pfpr_4]\n"
167 " ldp d6, d7, %[pfpr_6]\n"
171 : [x0
]"=m" (x0
), [x1
]"=m" (x1
)
172 : [pgpr_0
]"m" (pGPR
[0]),
173 [pgpr_2
]"m" (pGPR
[2]),
174 [pgpr_4
]"m" (pGPR
[4]),
175 [pgpr_6
]"m" (pGPR
[6]),
176 [pregisterreturn
]"m" (pRegisterReturn
),
177 [pfpr_0
]"m" (pFPR
[0]),
178 [pfpr_2
]"m" (pFPR
[2]),
179 [pfpr_4
]"m" (pFPR
[4]),
180 [pfpr_6
]"m" (pFPR
[6]),
181 [pmethod
]"r" (pMethod
)
182 : "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7"
185 MapReturn(x0
, x1
, pReturnType
, (sal_uInt64
*) pRegisterReturn
);
189 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
190 if ( nr < arm::MAX_GPR_REGS ) \
191 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
193 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
195 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
196 if ( nr < arm::MAX_GPR_REGS ) \
197 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
199 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
201 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
202 if ( nr < arm::MAX_GPR_REGS ) \
203 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
205 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
207 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
208 if ( nr < arm::MAX_GPR_REGS ) \
209 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
211 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
213 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
214 if ( nr < arm::MAX_FPR_REGS ) \
215 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
217 *pDS++ = *reinterpret_cast<double *>( pSV );
219 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
220 INSERT_DOUBLE( pSV, nr, pGPR, pDS )
223 static void cpp_call(
224 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
225 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
226 typelib_TypeDescriptionReference
* pReturnTypeRef
,
227 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
228 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
230 // max space for: values|ptr ...
231 sal_uInt64
* pStack
= (sal_uInt64
*)alloca( (nParams
+2) * sizeof(sal_Int64
) );
232 sal_uInt64
* pStackStart
= pStack
;
234 sal_uInt64 pGPR
[arm::MAX_GPR_REGS
];
237 // storage and counter for SIMD/FP registers
238 double pFPR
[arm::MAX_FPR_REGS
];
242 typelib_TypeDescription
* pReturnTypeDescr
= 0;
243 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
244 assert( pReturnTypeDescr
);
246 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
248 if (pReturnTypeDescr
)
250 if (!arm::return_in_x8( pReturnTypeRef
) )
251 pCppReturn
= pUnoReturn
; // direct way for simple types
254 // complex return via x8
255 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
256 ? alloca( pReturnTypeDescr
->nSize
)
257 : pUnoReturn
); // direct way
261 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
262 + aVtableSlot
.offset
;
263 INSERT_INT64( &pAdjustedThisPtr
, nGPR
, pGPR
, pStack
);
267 void ** pCppArgs
= (void **)alloca( sizeof(void *) * nParams
);
269 // indices of values this have to be converted (interface conversion cpp<=>uno)
270 int * pTempIndices
= (int *)alloca( sizeof(int) * nParams
);
272 // type descriptions for reconversions
273 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)alloca( sizeof(void *) * nParams
);
275 sal_Int32 nTempIndices
= 0;
277 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
279 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
280 typelib_TypeDescription
* pParamTypeDescr
= 0;
281 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
283 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
285 uno_copyAndConvertData( pCppArgs
[nPos
] = alloca(8), pUnoArgs
[nPos
],
286 pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
288 switch (pParamTypeDescr
->eTypeClass
)
290 case typelib_TypeClass_HYPER
:
291 case typelib_TypeClass_UNSIGNED_HYPER
:
292 INSERT_INT64( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
294 case typelib_TypeClass_LONG
:
295 case typelib_TypeClass_UNSIGNED_LONG
:
296 case typelib_TypeClass_ENUM
:
297 INSERT_INT32( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
299 case typelib_TypeClass_SHORT
:
300 case typelib_TypeClass_CHAR
:
301 case typelib_TypeClass_UNSIGNED_SHORT
:
302 INSERT_INT16( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
304 case typelib_TypeClass_BOOLEAN
:
305 case typelib_TypeClass_BYTE
:
306 INSERT_INT8( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
308 case typelib_TypeClass_FLOAT
:
309 INSERT_FLOAT( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
311 case typelib_TypeClass_DOUBLE
:
312 INSERT_DOUBLE( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
318 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
320 else // ptr to complex value | ref
322 if (! rParam
.bIn
) // is pure out
324 // cpp out is constructed mem, uno out is not!
326 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
328 pTempIndices
[nTempIndices
] = nPos
; // default constructed for cpp call
329 // will be released at reconversion
330 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
333 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
335 uno_copyAndConvertData(
336 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
337 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
339 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
340 // will be released at reconversion
341 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
345 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
347 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
349 INSERT_INT64( &(pCppArgs
[nPos
]), nGPR
, pGPR
, pStack
);
353 assert( nGPR
<= arm::MAX_GPR_REGS
);
354 assert( nFPR
<= arm::MAX_FPR_REGS
);
360 pAdjustedThisPtr
, aVtableSlot
.index
,
361 pCppReturn
, pReturnTypeRef
,
363 (pStack
- pStackStart
),
366 } catch (css::uno::Exception
&) {
368 } catch (std::exception
& e
) {
369 throw css::uno::RuntimeException(
370 "C++ code threw " + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
371 + o3tl::runtimeToOUString(e
.what()));
373 throw css::uno::RuntimeException("C++ code threw unknown exception");
376 // NO exception occurred...
379 // reconvert temporary params
380 for ( ; nTempIndices
--; )
382 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
383 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
385 if (pParams
[nIndex
].bIn
)
387 if (pParams
[nIndex
].bOut
) // inout
389 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
390 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
391 pThis
->getBridge()->getCpp2Uno() );
396 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
397 pThis
->getBridge()->getCpp2Uno() );
399 // destroy temp cpp param => cpp: every param was constructed
400 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
402 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
405 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
407 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
408 pThis
->getBridge()->getCpp2Uno() );
409 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
414 // fill uno exception
415 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
418 for ( ; nTempIndices
--; )
420 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
421 // destroy temp cpp param => cpp: every param was constructed
422 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], cpp_release
);
423 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
427 if (pReturnTypeDescr
)
428 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
433 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
435 void unoInterfaceProxyDispatch(
436 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
437 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
440 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
441 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* >(pUnoI
);
442 #if OSL_DEBUG_LEVEL > 0
443 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
446 switch (pMemberDescr
->eTypeClass
)
448 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
450 #if OSL_DEBUG_LEVEL > 0
451 // determine vtable call index
452 sal_Int32 nMemberPos
= ((typelib_InterfaceMemberTypeDescription
*)pMemberDescr
)->nPosition
;
453 assert( nMemberPos
< pTypeDescr
->nAllMembers
&& "### member pos out of range!");
456 VtableSlot
aVtableSlot(
458 reinterpret_cast<typelib_InterfaceAttributeTypeDescription
const *>
463 // dependent dispatch
466 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
468 pReturn
, pArgs
, ppException
);
473 typelib_MethodParameter aParam
;
475 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
476 aParam
.bIn
= sal_True
;
477 aParam
.bOut
= sal_False
;
479 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
480 OUString
aVoidName("void");
481 typelib_typedescriptionreference_new(
482 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
484 // dependent dispatch
485 aVtableSlot
.index
+= 1;
487 pThis
, aVtableSlot
, // get, then set method
490 pReturn
, pArgs
, ppException
);
492 typelib_typedescriptionreference_release( pReturnTypeRef
);
497 case typelib_TypeClass_INTERFACE_METHOD
:
499 #if OSL_DEBUG_LEVEL > 0
500 // determine vtable call index
501 sal_Int32 nMemberPos
= ((typelib_InterfaceMemberTypeDescription
*)pMemberDescr
)->nPosition
;
502 assert(nMemberPos
< pTypeDescr
->nAllMembers
&& "### member pos out of range!");
505 VtableSlot
aVtableSlot(
507 reinterpret_cast<typelib_InterfaceMethodTypeDescription
const *>
510 switch (aVtableSlot
.index
)
513 case 1: // acquire uno interface
514 (*pUnoI
->acquire
)( pUnoI
);
517 case 2: // release uno interface
518 (*pUnoI
->release
)( pUnoI
);
521 case 0: // queryInterface() opt
523 typelib_TypeDescription
* pTD
= 0;
524 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
527 uno_Interface
* pInterface
= 0;
528 (*pThis
->getBridge()->getUnoEnv()->getRegisteredInterface
)(
529 pThis
->getBridge()->getUnoEnv(),
530 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
535 reinterpret_cast< uno_Any
* >( pReturn
),
536 &pInterface
, pTD
, 0 );
537 (*pInterface
->release
)( pInterface
);
538 TYPELIB_DANGER_RELEASE( pTD
);
542 TYPELIB_DANGER_RELEASE( pTD
);
544 } // else perform queryInterface()
547 // dependent dispatch
550 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
551 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
552 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
553 pReturn
, pArgs
, ppException
);
559 ::com::sun::star::uno::RuntimeException
aExc(
560 "illegal member type description!",
561 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
563 Type
const & rExcType
= cppu::UnoType
<decltype(aExc
)>::get();
564 // binary identical null reference
565 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */