tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_s390x / uno2cpp.cxx
blobe2078ec6ca6e708ee0e800662b00c9a7387a0f44
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 #include <sal/config.h>
22 #include <exception>
23 #include <malloc.h>
24 #include <typeinfo>
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>
30 #include <uno/data.h>
32 #include "bridge.hxx"
33 #include "types.hxx"
34 #include "unointerfaceproxy.hxx"
35 #include "vtables.hxx"
37 #include "share.hxx"
39 #include <stdio.h>
40 #include <string.h>
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);
49 #endif
50 switch (eTypeClass)
52 case typelib_TypeClass_HYPER:
53 case typelib_TypeClass_UNSIGNED_HYPER:
54 *pRegisterReturn = r2;
55 break;
56 case typelib_TypeClass_LONG:
57 case typelib_TypeClass_UNSIGNED_LONG:
58 case typelib_TypeClass_ENUM:
59 *(unsigned int*)pRegisterReturn = (unsigned int)r2;
60 break;
61 case typelib_TypeClass_CHAR:
62 case typelib_TypeClass_SHORT:
63 case typelib_TypeClass_UNSIGNED_SHORT:
64 *(unsigned short*)pRegisterReturn = (unsigned short)r2;
65 break;
66 case typelib_TypeClass_BOOLEAN:
67 case typelib_TypeClass_BYTE:
68 *(unsigned char*)pRegisterReturn = (unsigned char)r2;
69 break;
70 case typelib_TypeClass_FLOAT:
71 *reinterpret_cast<float *>( pRegisterReturn ) = f0;
72 break;
73 case typelib_TypeClass_DOUBLE:
74 *reinterpret_cast<double *>( pRegisterReturn ) = f0;
75 break;
76 default:
77 break;
79 #if OSL_DEBUG_LEVEL > 2
80 fprintf(stderr, "end of MapReturn with %x\n", pRegisterReturn ? *pRegisterReturn : 0);
81 #endif
84 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
85 { \
86 if ( nr < s390x::MAX_SSE_REGS ) \
87 { \
88 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
89 } \
90 else \
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 ); \
97 else \
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 ); \
103 else \
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 ); \
109 else \
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 ); \
115 else \
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 ); \
121 else \
122 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
124 namespace
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);
156 #endif
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
175 case 4:
176 asm volatile("ld 6,%0" :: "m"(pFPR[3]) : "16");
177 case 3:
178 asm volatile("ld 4,%0" :: "m"(pFPR[2]) : "16");
179 case 2:
180 asm volatile("ld 2,%0" :: "m"(pFPR[1]) : "16");
181 case 1:
182 asm volatile("ld 0,%0" :: "m"(pFPR[0]) : "16");
183 default:
184 break;
187 volatile long r2;
188 volatile double f0;
190 (*pFunc)(pGPR[0], pGPR[1], pGPR[2], pGPR[3], pGPR[4]);
192 __asm__ __volatile__ (
193 "lgr %0,2\n\t"
194 "ldr %1,0\n\t"
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];
215 sal_uInt32 nGPR = 0;
217 double pFPR[s390x::MAX_SSE_REGS];
218 sal_uInt32 nFPR = 0;
220 // return
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
233 else
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 );
247 // stack space
248 static_assert(sizeof(void *) == sizeof(sal_Int64), "### unexpected size!");
249 // args
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 );
274 break;
275 case typelib_TypeClass_LONG:
276 case typelib_TypeClass_UNSIGNED_LONG:
277 case typelib_TypeClass_ENUM:
278 INSERT_INT32( pCppArgs[nPos], nGPR, pGPR, pStack );
279 break;
280 case typelib_TypeClass_SHORT:
281 case typelib_TypeClass_CHAR:
282 case typelib_TypeClass_UNSIGNED_SHORT:
283 INSERT_INT16( pCppArgs[nPos], nGPR, pGPR, pStack );
284 break;
285 case typelib_TypeClass_BOOLEAN:
286 case typelib_TypeClass_BYTE:
287 INSERT_INT8( pCppArgs[nPos], nGPR, pGPR, pStack );
288 break;
289 case typelib_TypeClass_FLOAT:
290 INSERT_FLOAT( pCppArgs[nPos], nFPR, pFPR, pStack );
291 break;
292 case typelib_TypeClass_DOUBLE:
293 INSERT_DOUBLE( pCppArgs[nPos], nFPR, pFPR, pStack );
294 break;
295 default:
296 break;
299 // no longer needed
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!
307 uno_constructData(
308 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
309 pParamTypeDescr );
310 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
311 // will be released at reconversion
312 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
314 // is in/inout
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;
325 else // direct way
327 pCppArgs[nPos] = pUnoArgs[nPos];
328 // no longer needed
329 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
331 INSERT_INT64( &(pCppArgs[nPos]), nGPR, pGPR, pStack );
337 try {
338 callVirtualMethod(
339 pAdjustedThisPtr, aVtableSlot.index,
340 pCppReturn, pReturnTypeDescr,
341 pStackStart, (pStack - pStackStart),
342 pGPR, nGPR,
343 pFPR, nFPR );
344 } catch (css::uno::Exception &) {
345 throw;
346 } catch (std::exception & e) {
347 throw css::uno::RuntimeException(
348 "C++ code threw " + o3tl::runtimeToOUString(typeid(e).name()) + ": "
349 + o3tl::runtimeToOUString(e.what()));
350 } catch (...) {
351 throw css::uno::RuntimeException("C++ code threw unknown exception");
353 // NO exception occurred...
354 *ppUnoExc = 0;
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() );
371 else // pure out
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 );
381 // return value
382 if (pCppReturn && pUnoReturn != pCppReturn)
384 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
385 pThis->getBridge()->getCpp2Uno() );
386 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
389 catch (...)
391 // fill uno exception
392 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc, pThis->getBridge()->getCpp2Uno());
395 // temporary params
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] );
403 // return type
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");
418 #endif
421 // is my surrogate
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(
431 getVtableSlot(
432 reinterpret_cast<
433 typelib_InterfaceAttributeTypeDescription const * >(
434 pMemberDescr)));
436 if (pReturn)
438 // dependent dispatch
439 cpp_call(
440 pThis, aVtableSlot,
441 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
442 0, 0, // no params
443 pReturn, pArgs, ppException );
445 else
447 // is SET
448 typelib_MethodParameter aParam;
449 aParam.pTypeRef =
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
461 cpp_call(
462 pThis, aVtableSlot,
463 pReturnTypeRef,
464 1, &aParam,
465 pReturn, pArgs, ppException );
467 typelib_typedescriptionreference_release( pReturnTypeRef );
470 break;
472 case typelib_TypeClass_INTERFACE_METHOD:
475 VtableSlot aVtableSlot(
476 getVtableSlot(
477 reinterpret_cast<
478 typelib_InterfaceMethodTypeDescription const * >(
479 pMemberDescr)));
480 switch (aVtableSlot.index)
482 // standard calls
483 case 1: // acquire uno interface
484 (*pUnoI->acquire)( pUnoI );
485 *ppException = 0;
486 break;
487 case 2: // release uno interface
488 (*pUnoI->release)( pUnoI );
489 *ppException = 0;
490 break;
491 case 0: // queryInterface() opt
493 typelib_TypeDescription * pTD = 0;
494 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
495 if (pTD)
497 uno_Interface * pInterface = 0;
498 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
499 pThis->pBridge->getUnoEnv(),
500 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
502 if (pInterface)
504 ::uno_any_construct(
505 reinterpret_cast< uno_Any * >( pReturn ),
506 &pInterface, pTD, 0 );
507 (*pInterface->release)( pInterface );
508 TYPELIB_DANGER_RELEASE( pTD );
509 *ppException = 0;
510 break;
512 TYPELIB_DANGER_RELEASE( pTD );
514 } // else perform queryInterface()
515 default:
516 // dependent dispatch
517 cpp_call(
518 pThis, aVtableSlot,
519 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
520 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
521 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
522 pReturn, pArgs, ppException );
524 break;
526 default:
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: */