bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_ios / uno2cpp.cxx
blob30ae398816c76faf5c4dda644e7581ac7d3d6680
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifdef __arm64
22 #include <sal/config.h>
24 #include <exception>
25 #include <typeinfo>
27 #include <com/sun/star/uno/Exception.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <o3tl/runtimetooustring.hxx>
31 #include "bridge.hxx"
32 #include "types.hxx"
33 #include "unointerfaceproxy.hxx"
34 #include "vtables.hxx"
36 #include "share.hxx"
38 using namespace ::com::sun::star::uno;
40 namespace arm
42 bool is_hfa_struct(const typelib_TypeDescription * type)
44 const typelib_CompoundTypeDescription * p
45 = reinterpret_cast< const typelib_CompoundTypeDescription * >(type);
46 if (p->nMembers >= 4)
47 return false;
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)
53 return false;
55 return true;
58 bool return_in_x8( typelib_TypeDescriptionReference *pTypeRef )
60 if (bridges::cpp_uno::shared::isSimpleType(pTypeRef))
61 return false;
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))
71 bRet = false;
73 TYPELIB_DANGER_RELEASE( pTypeDescr );
74 return bRet;
76 return true;
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;
87 [[fallthrough]];
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;
97 break;
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
104 break;
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
111 break;
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;
119 break;
120 default:
121 break;
125 namespace
127 void callVirtualMethod(
128 void *pThis,
129 sal_Int32 nVtableIndex,
130 void *pRegisterReturn,
131 typelib_TypeDescriptionReference *pReturnType,
132 sal_uInt64 *pStack,
133 int nStack,
134 sal_uInt64 *pGPR,
135 double *pFPR)
137 // never called
138 if (! pThis)
139 CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
141 if ( nStack )
143 // 16-bytes aligned
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
154 sal_uInt64 x0;
155 sal_uInt64 x1;
157 __asm__ __volatile__
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"
168 " blr %[pmethod]\n"
169 " str x0, %[x0]\n"
170 " str x1, %[x1]\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 ); \
192 else \
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 ); \
198 else \
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 ); \
204 else \
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 ); \
210 else \
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 ); \
216 else \
217 *pDS++ = *reinterpret_cast<double *>( pSV );
219 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
220 INSERT_DOUBLE( pSV, nr, pGPR, pDS )
222 namespace {
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];
235 int nGPR = 0;
237 // storage and counter for SIMD/FP registers
238 double pFPR[arm::MAX_FPR_REGS];
239 int nFPR = 0;
241 // return
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
252 else
254 // complex return via x8
255 pCppReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
256 ? alloca( pReturnTypeDescr->nSize )
257 : pUnoReturn); // direct way
260 // push this
261 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
262 + aVtableSlot.offset;
263 INSERT_INT64( &pAdjustedThisPtr, nGPR, pGPR, pStack );
265 // stack space
266 // args
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 );
293 break;
294 case typelib_TypeClass_LONG:
295 case typelib_TypeClass_UNSIGNED_LONG:
296 case typelib_TypeClass_ENUM:
297 INSERT_INT32( pCppArgs[nPos], nGPR, pGPR, pStack );
298 break;
299 case typelib_TypeClass_SHORT:
300 case typelib_TypeClass_CHAR:
301 case typelib_TypeClass_UNSIGNED_SHORT:
302 INSERT_INT16( pCppArgs[nPos], nGPR, pGPR, pStack );
303 break;
304 case typelib_TypeClass_BOOLEAN:
305 case typelib_TypeClass_BYTE:
306 INSERT_INT8( pCppArgs[nPos], nGPR, pGPR, pStack );
307 break;
308 case typelib_TypeClass_FLOAT:
309 INSERT_FLOAT( pCppArgs[nPos], nFPR, pFPR, pStack );
310 break;
311 case typelib_TypeClass_DOUBLE:
312 INSERT_DOUBLE( pCppArgs[nPos], nFPR, pFPR, pStack );
313 break;
314 default:
315 break;
317 // no longer needed
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!
325 uno_constructData(
326 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
327 pParamTypeDescr );
328 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
329 // will be released at reconversion
330 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
332 // is in/inout
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;
343 else // direct way
345 pCppArgs[nPos] = pUnoArgs[nPos];
346 // no longer needed
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 );
358 try {
359 callVirtualMethod(
360 pAdjustedThisPtr, aVtableSlot.index,
361 pCppReturn, pReturnTypeRef,
362 pStackStart,
363 (pStack - pStackStart),
364 pGPR,
365 pFPR);
366 } catch (css::uno::Exception &) {
367 throw;
368 } catch (std::exception & e) {
369 throw css::uno::RuntimeException(
370 "C++ code threw " + o3tl::runtimeToOUString(typeid(e).name()) + ": "
371 + o3tl::runtimeToOUString(e.what()));
372 } catch (...) {
373 throw css::uno::RuntimeException("C++ code threw unknown exception");
376 // NO exception occurred...
377 *ppUnoExc = 0;
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() );
394 else // pure out
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 );
404 // return value
405 if (pCppReturn && pUnoReturn != pCppReturn)
407 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
408 pThis->getBridge()->getCpp2Uno() );
409 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
412 catch (...)
414 // fill uno exception
415 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc, pThis->getBridge()->getCpp2Uno());
417 // temporary params
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] );
426 // return type
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 )
439 // is my surrogate
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;
444 #endif
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!");
454 #endif
456 VtableSlot aVtableSlot(
457 getVtableSlot(
458 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const *>
459 (pMemberDescr)));
461 if (pReturn)
463 // dependent dispatch
464 cpp_call(
465 pThis, aVtableSlot,
466 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
467 0, 0, // no params
468 pReturn, pArgs, ppException );
470 else
472 // is SET
473 typelib_MethodParameter aParam;
474 aParam.pTypeRef =
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;
486 cpp_call(
487 pThis, aVtableSlot, // get, then set method
488 pReturnTypeRef,
489 1, &aParam,
490 pReturn, pArgs, ppException );
492 typelib_typedescriptionreference_release( pReturnTypeRef );
495 break;
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!");
503 #endif
505 VtableSlot aVtableSlot(
506 getVtableSlot(
507 reinterpret_cast<typelib_InterfaceMethodTypeDescription const *>
508 (pMemberDescr)));
510 switch (aVtableSlot.index)
512 // standard calls
513 case 1: // acquire uno interface
514 (*pUnoI->acquire)( pUnoI );
515 *ppException = 0;
516 break;
517 case 2: // release uno interface
518 (*pUnoI->release)( pUnoI );
519 *ppException = 0;
520 break;
521 case 0: // queryInterface() opt
523 typelib_TypeDescription * pTD = 0;
524 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
525 if (pTD)
527 uno_Interface * pInterface = 0;
528 (*pThis->getBridge()->getUnoEnv()->getRegisteredInterface)(
529 pThis->getBridge()->getUnoEnv(),
530 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
532 if (pInterface)
534 ::uno_any_construct(
535 reinterpret_cast< uno_Any * >( pReturn ),
536 &pInterface, pTD, 0 );
537 (*pInterface->release)( pInterface );
538 TYPELIB_DANGER_RELEASE( pTD );
539 *ppException = 0;
540 break;
542 TYPELIB_DANGER_RELEASE( pTD );
544 } // else perform queryInterface()
545 [[fallthrough]];
546 default:
547 // dependent dispatch
548 cpp_call(
549 pThis, aVtableSlot,
550 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
551 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
552 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
553 pReturn, pArgs, ppException );
555 break;
557 default:
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 );
570 } } }
572 #endif
574 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */