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"
43 using namespace ::com::sun::star::uno
;
45 void MapReturn(long r2
, double f0
, typelib_TypeClass eTypeClass
, sal_uInt64
* pRegisterReturn
)
47 #if OSL_DEBUG_LEVEL > 2
48 fprintf(stderr
,"Mapping Return with %lx %ld %f\n", r2
, r2
, f0
);
52 case typelib_TypeClass_HYPER
:
53 case typelib_TypeClass_UNSIGNED_HYPER
:
54 *pRegisterReturn
= r2
;
56 case typelib_TypeClass_LONG
:
57 case typelib_TypeClass_UNSIGNED_LONG
:
58 case typelib_TypeClass_ENUM
:
59 *(unsigned int*)pRegisterReturn
= (unsigned int)r2
;
61 case typelib_TypeClass_CHAR
:
62 case typelib_TypeClass_SHORT
:
63 case typelib_TypeClass_UNSIGNED_SHORT
:
64 *(unsigned short*)pRegisterReturn
= (unsigned short)r2
;
66 case typelib_TypeClass_BOOLEAN
:
67 case typelib_TypeClass_BYTE
:
68 *(unsigned char*)pRegisterReturn
= (unsigned char)r2
;
70 case typelib_TypeClass_FLOAT
:
71 *reinterpret_cast<float *>( pRegisterReturn
) = f0
;
73 case typelib_TypeClass_DOUBLE
:
74 *reinterpret_cast<double *>( pRegisterReturn
) = f0
;
79 #if OSL_DEBUG_LEVEL > 2
80 fprintf(stderr
, "end of MapReturn with %x\n", pRegisterReturn
? *pRegisterReturn
: 0);
84 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
86 if ( nr < s390x::MAX_SSE_REGS ) \
88 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
91 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); \
94 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
95 if ( nr < s390x::MAX_SSE_REGS ) \
96 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
98 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
100 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
101 if ( nr < s390x::MAX_GPR_REGS ) \
102 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
104 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
106 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
107 if ( nr < s390x::MAX_GPR_REGS ) \
108 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
110 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
112 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
113 if ( nr < s390x::MAX_GPR_REGS ) \
114 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
116 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
118 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
119 if ( nr < s390x::MAX_GPR_REGS ) \
120 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
122 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
127 void callVirtualMethod(
128 void * pThis
, sal_Int32 nVtableIndex
,
129 void * pRegisterReturn
, typelib_TypeDescription
* pReturnTypeDescr
,
130 sal_uInt64
*pStack
, sal_uInt32 nStack
,
131 sal_uInt64
*pGPR
, sal_uInt32 nGPR
,
132 double *pFPR
, sal_uInt32 nFPR
)
134 // Should not happen, but...
135 if ( nFPR
> s390x::MAX_SSE_REGS
)
136 nFPR
= s390x::MAX_SSE_REGS
;
137 if ( nGPR
> s390x::MAX_GPR_REGS
)
138 nGPR
= s390x::MAX_GPR_REGS
;
140 #if OSL_DEBUG_LEVEL > 2
141 // Let's figure out what is really going on here
143 fprintf( stderr
, "= nStack is %d\n", nStack
);
144 fprintf( stderr
, "= callVirtualMethod() =\nGPR's (%d): ", nGPR
);
145 for ( unsigned int i
= 0; i
< nGPR
; ++i
)
146 fprintf( stderr
, "0x%lx, ", pGPR
[i
] );
147 fprintf( stderr
, "\nFPR's (%d): ", nFPR
);
148 for ( unsigned int i
= 0; i
< nFPR
; ++i
)
149 fprintf( stderr
, "0x%lx (%f), ", pFPR
[i
], pFPR
[i
] );
150 fprintf( stderr
, "\nStack (%d): ", nStack
);
151 for ( unsigned int i
= 0; i
< nStack
; ++i
)
152 fprintf( stderr
, "0x%lx, ", pStack
[i
] );
153 fprintf( stderr
, "\n" );
154 fprintf( stderr
, "pRegisterReturn is %p\n", pRegisterReturn
);
158 // Load parameters to stack, if necessary
159 // Stack, if used, must be 8-bytes aligned
160 sal_uInt64
*stack
= (sal_uInt64
*) __builtin_alloca( nStack
* 8 );
161 memcpy( stack
, pStack
, nStack
* 8 );
163 // To get pointer to method
164 // a) get the address of the vtable
165 sal_uInt64 pMethod
= *((sal_uInt64
*)pThis
);
166 // b) get the address from the vtable entry at offset
167 pMethod
+= 8 * nVtableIndex
;
168 pMethod
= *((sal_uInt64
*)pMethod
);
170 typedef void (* FunctionCall
)( sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
);
171 FunctionCall pFunc
= (FunctionCall
)pMethod
;
173 switch (nFPR
) //deliberate fall through
176 asm volatile("ld 6,%0" :: "m"(pFPR
[3]) : "16");
178 asm volatile("ld 4,%0" :: "m"(pFPR
[2]) : "16");
180 asm volatile("ld 2,%0" :: "m"(pFPR
[1]) : "16");
182 asm volatile("ld 0,%0" :: "m"(pFPR
[0]) : "16");
190 (*pFunc
)(pGPR
[0], pGPR
[1], pGPR
[2], pGPR
[3], pGPR
[4]);
192 __asm__
__volatile__ (
195 : "=r" (r2
), "=f" (f0
)
199 MapReturn(r2
, f0
, pReturnTypeDescr
->eTypeClass
, (sal_uInt64
*)pRegisterReturn
);
203 static void cpp_call(
204 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
205 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
206 typelib_TypeDescriptionReference
* pReturnTypeRef
,
207 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
208 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
210 // max space for: [complex ret ptr], values|ptr ...
211 sal_uInt64
* pStack
= (sal_uInt64
*)alloca( (nParams
+3) * sizeof(sal_Int64
) );
212 sal_uInt64
* pStackStart
= pStack
;
214 sal_uInt64 pGPR
[s390x::MAX_GPR_REGS
];
217 double pFPR
[s390x::MAX_SSE_REGS
];
221 typelib_TypeDescription
* pReturnTypeDescr
= 0;
222 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
223 assert(pReturnTypeDescr
);
225 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
227 if (pReturnTypeDescr
)
229 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
231 pCppReturn
= pUnoReturn
; // direct way for simple types
235 // complex return via ptr
236 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
237 ? alloca( pReturnTypeDescr
->nSize
)
238 : pUnoReturn
); // direct way
239 INSERT_INT64( &pCppReturn
, nGPR
, pGPR
, pStack
);
242 // push "this" pointer
243 void * pAdjustedThisPtr
= reinterpret_cast< void ** >( pThis
->getCppI() ) + aVtableSlot
.offset
;
245 INSERT_INT64( &pAdjustedThisPtr
, nGPR
, pGPR
, pStack
);
248 static_assert(sizeof(void *) == sizeof(sal_Int64
), "### unexpected size!");
250 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
251 // indices of values this have to be converted (interface conversion cpp<=>uno)
252 sal_Int32
* pTempIndices
= (sal_Int32
*)(pCppArgs
+ nParams
);
253 // type descriptions for reconversions
254 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
256 sal_Int32 nTempIndices
= 0;
258 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
260 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
261 typelib_TypeDescription
* pParamTypeDescr
= 0;
262 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
264 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
266 uno_copyAndConvertData( pCppArgs
[nPos
] = alloca( 8 ), pUnoArgs
[nPos
], pParamTypeDescr
,
267 pThis
->getBridge()->getUno2Cpp() );
269 switch (pParamTypeDescr
->eTypeClass
)
271 case typelib_TypeClass_HYPER
:
272 case typelib_TypeClass_UNSIGNED_HYPER
:
273 INSERT_INT64( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
275 case typelib_TypeClass_LONG
:
276 case typelib_TypeClass_UNSIGNED_LONG
:
277 case typelib_TypeClass_ENUM
:
278 INSERT_INT32( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
280 case typelib_TypeClass_SHORT
:
281 case typelib_TypeClass_CHAR
:
282 case typelib_TypeClass_UNSIGNED_SHORT
:
283 INSERT_INT16( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
285 case typelib_TypeClass_BOOLEAN
:
286 case typelib_TypeClass_BYTE
:
287 INSERT_INT8( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
289 case typelib_TypeClass_FLOAT
:
290 INSERT_FLOAT( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
292 case typelib_TypeClass_DOUBLE
:
293 INSERT_DOUBLE( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
300 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
302 else // ptr to complex value | ref
304 if (! rParam
.bIn
) // is pure out
306 // cpp out is constructed mem, uno out is not!
308 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
310 pTempIndices
[nTempIndices
] = nPos
; // default constructed for cpp call
311 // will be released at reconversion
312 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
315 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
317 uno_copyAndConvertData(
318 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
319 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
321 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
322 // will be released at reconversion
323 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
327 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
329 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
331 INSERT_INT64( &(pCppArgs
[nPos
]), nGPR
, pGPR
, pStack
);
339 pAdjustedThisPtr
, aVtableSlot
.index
,
340 pCppReturn
, pReturnTypeDescr
,
341 pStackStart
, (pStack
- pStackStart
),
344 } catch (css::uno::Exception
&) {
346 } catch (std::exception
& e
) {
347 throw css::uno::RuntimeException(
348 "C++ code threw " + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
349 + o3tl::runtimeToOUString(e
.what()));
351 throw css::uno::RuntimeException("C++ code threw unknown exception");
353 // NO exception occurred...
356 // reconvert temporary params
357 for ( ; nTempIndices
--; )
359 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
360 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
362 if (pParams
[nIndex
].bIn
)
364 if (pParams
[nIndex
].bOut
) // inout
366 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
367 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
368 pThis
->getBridge()->getCpp2Uno() );
373 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
374 pThis
->getBridge()->getCpp2Uno() );
376 // destroy temp cpp param => cpp: every param was constructed
377 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
379 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
382 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
384 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
385 pThis
->getBridge()->getCpp2Uno() );
386 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
391 // fill uno exception
392 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
396 for ( ; nTempIndices
--; )
398 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
399 // destroy temp cpp param => cpp: every param was constructed
400 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], cpp_release
);
401 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
404 if (pReturnTypeDescr
)
405 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
410 namespace bridges::cpp_uno::shared
{
412 void unoInterfaceProxyDispatch(
413 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
414 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
416 #if OSL_DEBUG_LEVEL > 2
417 fprintf(stderr
, "unoInterfaceProxyDispatch\n");
422 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
423 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
425 switch (pMemberDescr
->eTypeClass
)
427 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
430 VtableSlot
aVtableSlot(
433 typelib_InterfaceAttributeTypeDescription
const * >(
438 // dependent dispatch
441 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
443 pReturn
, pArgs
, ppException
);
448 typelib_MethodParameter aParam
;
450 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
451 aParam
.bIn
= sal_True
;
452 aParam
.bOut
= sal_False
;
454 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
455 OUString
aVoidName("void");
456 typelib_typedescriptionreference_new(
457 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
459 // dependent dispatch
460 aVtableSlot
.index
+= 1; //get then set method
465 pReturn
, pArgs
, ppException
);
467 typelib_typedescriptionreference_release( pReturnTypeRef
);
472 case typelib_TypeClass_INTERFACE_METHOD
:
475 VtableSlot
aVtableSlot(
478 typelib_InterfaceMethodTypeDescription
const * >(
480 switch (aVtableSlot
.index
)
483 case 1: // acquire uno interface
484 (*pUnoI
->acquire
)( pUnoI
);
487 case 2: // release uno interface
488 (*pUnoI
->release
)( pUnoI
);
491 case 0: // queryInterface() opt
493 typelib_TypeDescription
* pTD
= 0;
494 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
497 uno_Interface
* pInterface
= 0;
498 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
499 pThis
->pBridge
->getUnoEnv(),
500 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
505 reinterpret_cast< uno_Any
* >( pReturn
),
506 &pInterface
, pTD
, 0 );
507 (*pInterface
->release
)( pInterface
);
508 TYPELIB_DANGER_RELEASE( pTD
);
512 TYPELIB_DANGER_RELEASE( pTD
);
514 } // else perform queryInterface()
516 // dependent dispatch
519 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
520 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
521 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
522 pReturn
, pArgs
, ppException
);
528 ::com::sun::star::uno::RuntimeException
aExc(
529 "illegal member type description!",
530 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
532 Type
const & rExcType
= cppu::UnoType
<decltype(aExc
)>::get();
533 // binary identical null reference
534 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */