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 r0
, typelib_TypeClass eTypeClass
, sal_uInt64
* pRegisterReturn
)
47 register float fret
asm("$f0");
48 register double dret
asm("$f0");
50 #if OSL_DEBUG_LEVEL > 2
51 fprintf(stderr
,"Mapping Return with %lx %ld %f\n", r0
, r0
, dret
);
55 case typelib_TypeClass_HYPER
:
56 case typelib_TypeClass_UNSIGNED_HYPER
:
57 *pRegisterReturn
= r0
;
59 case typelib_TypeClass_LONG
:
60 case typelib_TypeClass_UNSIGNED_LONG
:
61 case typelib_TypeClass_ENUM
:
62 *(unsigned int*)pRegisterReturn
= (unsigned int)r0
;
64 case typelib_TypeClass_CHAR
:
65 case typelib_TypeClass_SHORT
:
66 case typelib_TypeClass_UNSIGNED_SHORT
:
67 *(unsigned short*)pRegisterReturn
= (unsigned short)r0
;
69 case typelib_TypeClass_BOOLEAN
:
70 case typelib_TypeClass_BYTE
:
71 *(unsigned char*)pRegisterReturn
= (unsigned char)r0
;
73 case typelib_TypeClass_FLOAT
:
74 *reinterpret_cast<float *>( pRegisterReturn
) = fret
;
76 case typelib_TypeClass_DOUBLE
:
77 *reinterpret_cast<double *>( pRegisterReturn
) = dret
;
82 #if OSL_DEBUG_LEVEL > 2
83 fprintf(stderr
, "end of MapReturn with %x\n", pRegisterReturn
? *pRegisterReturn
: 0);
87 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
89 if ( nr < axp::MAX_WORDS_IN_REGS ) \
91 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
94 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); \
97 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
98 if ( nr < axp::MAX_WORDS_IN_REGS ) \
99 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
101 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
103 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
104 if ( nr < axp::MAX_WORDS_IN_REGS ) \
105 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
107 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
109 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
110 if ( nr < axp::MAX_WORDS_IN_REGS ) \
111 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
113 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
115 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
116 if ( nr < axp::MAX_WORDS_IN_REGS ) \
117 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
119 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
121 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
122 if ( nr < axp::MAX_WORDS_IN_REGS ) \
123 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
125 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
130 void callVirtualMethod(
131 void * pThis
, sal_Int32 nVtableIndex
,
132 void * pRegisterReturn
, typelib_TypeDescription
* pReturnTypeDescr
,
133 sal_uInt64
*pStack
, sal_uInt32 nStack
,
134 sal_uInt64
*pGPR
, sal_uInt32 nGPR
,
135 double *pFPR
, sal_uInt32 nFPR
)
137 // Should not happen, but...
138 if ( nFPR
> axp::MAX_SSE_REGS
)
139 nFPR
= axp::MAX_SSE_REGS
;
140 if ( nGPR
> axp::MAX_GPR_REGS
)
141 nGPR
= axp::MAX_GPR_REGS
;
143 #if OSL_DEBUG_LEVEL > 2
144 // Let's figure out what is really going on here
146 fprintf( stderr
, "= nStack is %d\n", nStack
);
147 fprintf( stderr
, "= callVirtualMethod() =\nGPR's (%d): ", nGPR
);
148 for ( unsigned int i
= 0; i
< nGPR
; ++i
)
149 fprintf( stderr
, "0x%lx, ", pGPR
[i
] );
150 fprintf( stderr
, "\nFPR's (%d): ", nFPR
);
151 for ( unsigned int i
= 0; i
< nFPR
; ++i
)
152 fprintf( stderr
, "0x%lx (%f), ", pFPR
[i
], pFPR
[i
] );
153 fprintf( stderr
, "\nStack (%d): ", nStack
);
154 for ( unsigned int i
= 0; i
< nStack
; ++i
)
155 fprintf( stderr
, "0x%lx, ", pStack
[i
] );
156 fprintf( stderr
, "\n" );
157 fprintf( stderr
, "pRegisterReturn is %p\n", pRegisterReturn
);
161 // Load parameters to stack, if necessary
162 // Stack, if used, must be 8-bytes aligned
163 sal_uInt64
*stack
= (sal_uInt64
*) __builtin_alloca( nStack
* 8 );
164 memcpy( stack
, pStack
, nStack
* 8 );
166 // To get pointer to method
167 // a) get the address of the vtable
168 sal_uInt64 pMethod
= *((sal_uInt64
*)pThis
);
169 // b) get the address from the vtable entry at offset
170 pMethod
+= 8 * nVtableIndex
;
171 pMethod
= *((sal_uInt64
*)pMethod
);
173 typedef void (* FunctionCall
)( sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
);
174 FunctionCall pFunc
= (FunctionCall
)pMethod
;
176 switch (nFPR
) //deliberate fall through
179 asm volatile("ldt $f16,%0" :: "m"(pFPR
[5]) : "$f16");
181 asm volatile("ldt $f17,%0" :: "m"(pFPR
[4]) : "$f17");
183 asm volatile("ldt $f18,%0" :: "m"(pFPR
[3]) : "$f18");
185 asm volatile("ldt $f19,%0" :: "m"(pFPR
[2]) : "$f19");
187 asm volatile("ldt $f20,%0" :: "m"(pFPR
[1]) : "$f20");
189 asm volatile("ldt $f21,%0" :: "m"(pFPR
[0]) : "$f21");
194 (*pFunc
)(pGPR
[0], pGPR
[1], pGPR
[2], pGPR
[3], pGPR
[4], pGPR
[5]);
195 register sal_uInt64 r0
__asm__("$0");
196 MapReturn(r0
, pReturnTypeDescr
->eTypeClass
, (sal_uInt64
*)pRegisterReturn
);
200 static void cpp_call(
201 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
202 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
203 typelib_TypeDescriptionReference
* pReturnTypeRef
,
204 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
205 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
207 // max space for: [complex ret ptr], values|ptr ...
208 sal_uInt64
* pStack
= (sal_uInt64
*)alloca( (nParams
+3) * sizeof(sal_Int64
) );
209 sal_uInt64
* pStackStart
= pStack
;
211 sal_uInt64 pGPR
[axp::MAX_GPR_REGS
];
212 double pFPR
[axp::MAX_SSE_REGS
];
213 sal_uInt32 nRegs
= 0;
216 typelib_TypeDescription
* pReturnTypeDescr
= 0;
217 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
218 assert(pReturnTypeDescr
);
220 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
222 if (pReturnTypeDescr
)
224 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
226 pCppReturn
= pUnoReturn
; // direct way for simple types
230 // complex return via ptr
231 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
232 ? alloca( pReturnTypeDescr
->nSize
)
233 : pUnoReturn
); // direct way
234 INSERT_INT64( &pCppReturn
, nRegs
, pGPR
, pStack
);
237 // push "this" pointer
238 void * pAdjustedThisPtr
= reinterpret_cast< void ** >( pThis
->getCppI() ) + aVtableSlot
.offset
;
240 INSERT_INT64( &pAdjustedThisPtr
, nRegs
, pGPR
, pStack
);
243 static_assert(sizeof(void *) == sizeof(sal_Int64
), "### unexpected size!");
245 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
246 // indices of values this have to be converted (interface conversion cpp<=>uno)
247 sal_Int32
* pTempIndices
= (sal_Int32
*)(pCppArgs
+ nParams
);
248 // type descriptions for reconversions
249 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
251 sal_Int32 nTempIndices
= 0;
253 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
255 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
256 typelib_TypeDescription
* pParamTypeDescr
= 0;
257 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
259 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
261 uno_copyAndConvertData( pCppArgs
[nPos
] = alloca( 8 ), pUnoArgs
[nPos
], pParamTypeDescr
,
262 pThis
->getBridge()->getUno2Cpp() );
264 switch (pParamTypeDescr
->eTypeClass
)
266 case typelib_TypeClass_HYPER
:
267 case typelib_TypeClass_UNSIGNED_HYPER
:
268 INSERT_INT64( pCppArgs
[nPos
], nRegs
, pGPR
, pStack
);
270 case typelib_TypeClass_LONG
:
271 case typelib_TypeClass_UNSIGNED_LONG
:
272 case typelib_TypeClass_ENUM
:
273 INSERT_INT32( pCppArgs
[nPos
], nRegs
, pGPR
, pStack
);
275 case typelib_TypeClass_SHORT
:
276 case typelib_TypeClass_CHAR
:
277 case typelib_TypeClass_UNSIGNED_SHORT
:
278 INSERT_INT16( pCppArgs
[nPos
], nRegs
, pGPR
, pStack
);
280 case typelib_TypeClass_BOOLEAN
:
281 case typelib_TypeClass_BYTE
:
282 INSERT_INT8( pCppArgs
[nPos
], nRegs
, pGPR
, pStack
);
284 case typelib_TypeClass_FLOAT
:
285 INSERT_FLOAT( pCppArgs
[nPos
], nRegs
, pFPR
, pStack
);
287 case typelib_TypeClass_DOUBLE
:
288 INSERT_DOUBLE( pCppArgs
[nPos
], nRegs
, pFPR
, pStack
);
295 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
297 else // ptr to complex value | ref
299 if (! rParam
.bIn
) // is pure out
301 // cpp out is constructed mem, uno out is not!
303 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
305 pTempIndices
[nTempIndices
] = nPos
; // default constructed for cpp call
306 // will be released at reconversion
307 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
310 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
312 uno_copyAndConvertData(
313 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
314 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
316 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
317 // will be released at reconversion
318 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
322 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
324 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
326 INSERT_INT64( &(pCppArgs
[nPos
]), nRegs
, pGPR
, pStack
);
334 pAdjustedThisPtr
, aVtableSlot
.index
,
335 pCppReturn
, pReturnTypeDescr
,
336 pStackStart
, (pStack
- pStackStart
),
339 } catch (css::uno::Exception
&) {
341 } catch (std::exception
& e
) {
342 throw css::uno::RuntimeException(
343 "C++ code threw " + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
344 + o3tl::runtimeToOUString(e
.what()));
346 throw css::uno::RuntimeException("C++ code threw unknown exception");
348 // NO exception occurred...
351 // reconvert temporary params
352 for ( ; nTempIndices
--; )
354 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
355 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
357 if (pParams
[nIndex
].bIn
)
359 if (pParams
[nIndex
].bOut
) // inout
361 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
362 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
363 pThis
->getBridge()->getCpp2Uno() );
368 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
369 pThis
->getBridge()->getCpp2Uno() );
371 // destroy temp cpp param => cpp: every param was constructed
372 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
374 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
377 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
379 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
380 pThis
->getBridge()->getCpp2Uno() );
381 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
386 // fill uno exception
387 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
391 for ( ; nTempIndices
--; )
393 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
394 // destroy temp cpp param => cpp: every param was constructed
395 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], cpp_release
);
396 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
399 if (pReturnTypeDescr
)
400 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
405 namespace bridges::cpp_uno::shared
{
407 void unoInterfaceProxyDispatch(
408 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
409 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
411 #if OSL_DEBUG_LEVEL > 2
412 fprintf(stderr
, "unoInterfaceProxyDispatch\n");
417 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
418 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
420 switch (pMemberDescr
->eTypeClass
)
422 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
425 VtableSlot
aVtableSlot(
428 typelib_InterfaceAttributeTypeDescription
const * >(
433 // dependent dispatch
436 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
438 pReturn
, pArgs
, ppException
);
443 typelib_MethodParameter aParam
;
445 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
446 aParam
.bIn
= sal_True
;
447 aParam
.bOut
= sal_False
;
449 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
450 OUString
aVoidName("void");
451 typelib_typedescriptionreference_new(
452 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
454 // dependent dispatch
455 aVtableSlot
.index
+= 1; //get then set method
460 pReturn
, pArgs
, ppException
);
462 typelib_typedescriptionreference_release( pReturnTypeRef
);
467 case typelib_TypeClass_INTERFACE_METHOD
:
470 VtableSlot
aVtableSlot(
473 typelib_InterfaceMethodTypeDescription
const * >(
475 switch (aVtableSlot
.index
)
478 case 1: // acquire uno interface
479 (*pUnoI
->acquire
)( pUnoI
);
482 case 2: // release uno interface
483 (*pUnoI
->release
)( pUnoI
);
486 case 0: // queryInterface() opt
488 typelib_TypeDescription
* pTD
= 0;
489 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
492 uno_Interface
* pInterface
= 0;
493 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
494 pThis
->pBridge
->getUnoEnv(),
495 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
500 reinterpret_cast< uno_Any
* >( pReturn
),
501 &pInterface
, pTD
, 0 );
502 (*pInterface
->release
)( pInterface
);
503 TYPELIB_DANGER_RELEASE( pTD
);
507 TYPELIB_DANGER_RELEASE( pTD
);
509 } // else perform queryInterface()
511 // dependent dispatch
514 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
515 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
516 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
517 pReturn
, pArgs
, ppException
);
523 ::com::sun::star::uno::RuntimeException
aExc(
524 "illegal member type description!",
525 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
527 Type
const & rExcType
= cppu::UnoType
<decltype(aExc
)>::get();
528 // binary identical null reference
529 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
536 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */