Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_s390x / uno2cpp.cxx
blob6e3ec49ee0ff660bf044b96d83f1444d23d3878a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <malloc.h>
32 #include <com/sun/star/uno/genfunc.hxx>
33 #include <uno/data.h>
35 #include "bridges/cpp_uno/shared/bridge.hxx"
36 #include "bridges/cpp_uno/shared/types.hxx"
37 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
38 #include "bridges/cpp_uno/shared/vtables.hxx"
40 #include "share.hxx"
42 #include <stdio.h>
43 #include <string.h>
46 using namespace ::rtl;
47 using namespace ::com::sun::star::uno;
49 void MapReturn(long r2, double f0, typelib_TypeClass eTypeClass, sal_uInt64* pRegisterReturn)
51 #if OSL_DEBUG_LEVEL > 2
52 fprintf(stderr,"Mapping Return with %lx %ld %f\n", r2, r2, f0);
53 #endif
54 switch (eTypeClass)
56 case typelib_TypeClass_HYPER:
57 case typelib_TypeClass_UNSIGNED_HYPER:
58 *pRegisterReturn = r2;
59 break;
60 case typelib_TypeClass_LONG:
61 case typelib_TypeClass_UNSIGNED_LONG:
62 case typelib_TypeClass_ENUM:
63 *(unsigned int*)pRegisterReturn = (unsigned int)r2;
64 break;
65 case typelib_TypeClass_CHAR:
66 case typelib_TypeClass_SHORT:
67 case typelib_TypeClass_UNSIGNED_SHORT:
68 *(unsigned short*)pRegisterReturn = (unsigned short)r2;
69 break;
70 case typelib_TypeClass_BOOLEAN:
71 case typelib_TypeClass_BYTE:
72 *(unsigned char*)pRegisterReturn = (unsigned char)r2;
73 break;
74 case typelib_TypeClass_FLOAT:
75 *reinterpret_cast<float *>( pRegisterReturn ) = f0;
76 break;
77 case typelib_TypeClass_DOUBLE:
78 *reinterpret_cast<double *>( pRegisterReturn ) = f0;
79 break;
80 default:
81 break;
83 #if OSL_DEBUG_LEVEL > 2
84 fprintf(stderr, "end of MapReturn with %x\n", pRegisterReturn ? *pRegisterReturn : 0);
85 #endif
88 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
89 { \
90 if ( nr < s390x::MAX_SSE_REGS ) \
91 { \
92 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
93 } \
94 else \
95 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); \
98 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
99 if ( nr < s390x::MAX_SSE_REGS ) \
100 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
101 else \
102 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
104 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
105 if ( nr < s390x::MAX_GPR_REGS ) \
106 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
107 else \
108 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
110 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
111 if ( nr < s390x::MAX_GPR_REGS ) \
112 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
113 else \
114 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
116 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
117 if ( nr < s390x::MAX_GPR_REGS ) \
118 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
119 else \
120 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
122 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
123 if ( nr < s390x::MAX_GPR_REGS ) \
124 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
125 else \
126 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
128 namespace
130 //==================================================================================================
131 void callVirtualMethod(
132 void * pThis, sal_Int32 nVtableIndex,
133 void * pRegisterReturn, typelib_TypeDescription * pReturnTypeDescr,
134 sal_uInt64 *pStack, sal_uInt32 nStack,
135 sal_uInt64 *pGPR, sal_uInt32 nGPR,
136 double *pFPR, sal_uInt32 nFPR)
138 // Should not happen, but...
139 if ( nFPR > s390x::MAX_SSE_REGS )
140 nFPR = s390x::MAX_SSE_REGS;
141 if ( nGPR > s390x::MAX_GPR_REGS )
142 nGPR = s390x::MAX_GPR_REGS;
144 #if OSL_DEBUG_LEVEL > 2
145 // Let's figure out what is really going on here
147 fprintf( stderr, "= nStack is %d\n", nStack );
148 fprintf( stderr, "= callVirtualMethod() =\nGPR's (%d): ", nGPR );
149 for ( unsigned int i = 0; i < nGPR; ++i )
150 fprintf( stderr, "0x%lx, ", pGPR[i] );
151 fprintf( stderr, "\nFPR's (%d): ", nFPR );
152 for ( unsigned int i = 0; i < nFPR; ++i )
153 fprintf( stderr, "0x%lx (%f), ", pFPR[i], pFPR[i] );
154 fprintf( stderr, "\nStack (%d): ", nStack );
155 for ( unsigned int i = 0; i < nStack; ++i )
156 fprintf( stderr, "0x%lx, ", pStack[i] );
157 fprintf( stderr, "\n" );
158 fprintf( stderr, "pRegisterReturn is %p\n", pRegisterReturn);
160 #endif
162 // Load parameters to stack, if necessary
163 // Stack, if used, must be 8-bytes aligned
164 sal_uInt64 *stack = (sal_uInt64 *) __builtin_alloca( nStack * 8 );
165 memcpy( stack, pStack, nStack * 8 );
167 // To get pointer to method
168 // a) get the address of the vtable
169 sal_uInt64 pMethod = *((sal_uInt64 *)pThis);
170 // b) get the address from the vtable entry at offset
171 pMethod += 8 * nVtableIndex;
172 pMethod = *((sal_uInt64 *)pMethod);
174 typedef void (* FunctionCall )( sal_uInt64, sal_uInt64, sal_uInt64, sal_uInt64, sal_uInt64 );
175 FunctionCall pFunc = (FunctionCall)pMethod;
177 switch (nFPR) //deliberate fall through
179 case 4:
180 asm volatile("ld 6,%0" :: "m"(pFPR[3]) : "16");
181 case 3:
182 asm volatile("ld 4,%0" :: "m"(pFPR[2]) : "16");
183 case 2:
184 asm volatile("ld 2,%0" :: "m"(pFPR[1]) : "16");
185 case 1:
186 asm volatile("ld 0,%0" :: "m"(pFPR[0]) : "16");
187 default:
188 break;
191 volatile long r2;
192 volatile double f0;
194 (*pFunc)(pGPR[0], pGPR[1], pGPR[2], pGPR[3], pGPR[4]);
196 __asm__ __volatile__ (
197 "lgr %0,2\n\t"
198 "ldr %1,0\n\t"
199 : "=r" (r2), "=f" (f0)
203 MapReturn(r2, f0, pReturnTypeDescr->eTypeClass, (sal_uInt64*)pRegisterReturn);
207 //============================================================================
208 static void cpp_call(
209 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
210 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
211 typelib_TypeDescriptionReference * pReturnTypeRef,
212 sal_Int32 nParams, typelib_MethodParameter * pParams,
213 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
215 // max space for: [complex ret ptr], values|ptr ...
216 sal_uInt64 * pStack = (sal_uInt64 *)alloca( (nParams+3) * sizeof(sal_Int64) );
217 sal_uInt64 * pStackStart = pStack;
219 sal_uInt64 pGPR[s390x::MAX_GPR_REGS];
220 sal_uInt32 nGPR = 0;
222 double pFPR[s390x::MAX_SSE_REGS];
223 sal_uInt32 nFPR = 0;
225 // return
226 typelib_TypeDescription * pReturnTypeDescr = 0;
227 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
228 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
230 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
232 if (pReturnTypeDescr)
234 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
236 pCppReturn = pUnoReturn; // direct way for simple types
238 else
240 // complex return via ptr
241 pCppReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
242 ? alloca( pReturnTypeDescr->nSize )
243 : pUnoReturn); // direct way
244 INSERT_INT64( &pCppReturn, nGPR, pGPR, pStack );
247 // push "this" pointer
248 void * pAdjustedThisPtr = reinterpret_cast< void ** >( pThis->getCppI() ) + aVtableSlot.offset;
250 INSERT_INT64( &pAdjustedThisPtr, nGPR, pGPR, pStack );
252 // stack space
253 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64), "### unexpected size!" );
254 // args
255 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
256 // indizes of values this have to be converted (interface conversion cpp<=>uno)
257 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
258 // type descriptions for reconversions
259 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
261 sal_Int32 nTempIndizes = 0;
263 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
265 const typelib_MethodParameter & rParam = pParams[nPos];
266 typelib_TypeDescription * pParamTypeDescr = 0;
267 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
269 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
271 uno_copyAndConvertData( pCppArgs[nPos] = alloca( 8 ), pUnoArgs[nPos], pParamTypeDescr,
272 pThis->getBridge()->getUno2Cpp() );
274 switch (pParamTypeDescr->eTypeClass)
276 case typelib_TypeClass_HYPER:
277 case typelib_TypeClass_UNSIGNED_HYPER:
278 INSERT_INT64( pCppArgs[nPos], nGPR, pGPR, pStack );
279 break;
280 case typelib_TypeClass_LONG:
281 case typelib_TypeClass_UNSIGNED_LONG:
282 case typelib_TypeClass_ENUM:
283 INSERT_INT32( pCppArgs[nPos], nGPR, pGPR, pStack );
284 break;
285 case typelib_TypeClass_SHORT:
286 case typelib_TypeClass_CHAR:
287 case typelib_TypeClass_UNSIGNED_SHORT:
288 INSERT_INT16( pCppArgs[nPos], nGPR, pGPR, pStack );
289 break;
290 case typelib_TypeClass_BOOLEAN:
291 case typelib_TypeClass_BYTE:
292 INSERT_INT8( pCppArgs[nPos], nGPR, pGPR, pStack );
293 break;
294 case typelib_TypeClass_FLOAT:
295 INSERT_FLOAT( pCppArgs[nPos], nFPR, pFPR, pStack );
296 break;
297 case typelib_TypeClass_DOUBLE:
298 INSERT_DOUBLE( pCppArgs[nPos], nFPR, pFPR, pStack );
299 break;
300 default:
301 break;
304 // no longer needed
305 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
307 else // ptr to complex value | ref
309 if (! rParam.bIn) // is pure out
311 // cpp out is constructed mem, uno out is not!
312 uno_constructData(
313 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
314 pParamTypeDescr );
315 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
316 // will be released at reconversion
317 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
319 // is in/inout
320 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
322 uno_copyAndConvertData(
323 pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
324 pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
326 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
327 // will be released at reconversion
328 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
330 else // direct way
332 pCppArgs[nPos] = pUnoArgs[nPos];
333 // no longer needed
334 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
336 INSERT_INT64( &(pCppArgs[nPos]), nGPR, pGPR, pStack );
342 callVirtualMethod(
343 pAdjustedThisPtr, aVtableSlot.index,
344 pCppReturn, pReturnTypeDescr,
345 pStackStart, (pStack - pStackStart),
346 pGPR, nGPR,
347 pFPR, nFPR );
348 // NO exception occurred...
349 *ppUnoExc = 0;
351 // reconvert temporary params
352 for ( ; nTempIndizes--; )
354 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
355 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
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() );
366 else // pure out
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 );
376 // return value
377 if (pCppReturn && pUnoReturn != pCppReturn)
379 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
380 pThis->getBridge()->getCpp2Uno() );
381 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
384 catch (...)
386 // fill uno exception
387 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
388 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
391 // temporary params
392 for ( ; nTempIndizes--; )
394 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
395 // destroy temp cpp param => cpp: every param was constructed
396 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
397 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
399 // return type
400 if (pReturnTypeDescr)
401 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
406 namespace bridges { namespace cpp_uno { namespace shared {
408 void unoInterfaceProxyDispatch(
409 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
410 void * pReturn, void * pArgs[], uno_Any ** ppException )
412 #if OSL_DEBUG_LEVEL > 2
413 fprintf(stderr, "unoInterfaceProxyDispatch\n");
414 #endif
417 // is my surrogate
418 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
419 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
421 switch (pMemberDescr->eTypeClass)
423 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
426 VtableSlot aVtableSlot(
427 getVtableSlot(
428 reinterpret_cast<
429 typelib_InterfaceAttributeTypeDescription const * >(
430 pMemberDescr)));
432 if (pReturn)
434 // dependent dispatch
435 cpp_call(
436 pThis, aVtableSlot,
437 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
438 0, 0, // no params
439 pReturn, pArgs, ppException );
441 else
443 // is SET
444 typelib_MethodParameter aParam;
445 aParam.pTypeRef =
446 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
447 aParam.bIn = sal_True;
448 aParam.bOut = sal_False;
450 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
451 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
452 typelib_typedescriptionreference_new(
453 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
455 // dependent dispatch
456 aVtableSlot.index += 1; //get then set method
457 cpp_call(
458 pThis, aVtableSlot,
459 pReturnTypeRef,
460 1, &aParam,
461 pReturn, pArgs, ppException );
463 typelib_typedescriptionreference_release( pReturnTypeRef );
466 break;
468 case typelib_TypeClass_INTERFACE_METHOD:
471 VtableSlot aVtableSlot(
472 getVtableSlot(
473 reinterpret_cast<
474 typelib_InterfaceMethodTypeDescription const * >(
475 pMemberDescr)));
476 switch (aVtableSlot.index)
478 // standard calls
479 case 1: // acquire uno interface
480 (*pUnoI->acquire)( pUnoI );
481 *ppException = 0;
482 break;
483 case 2: // release uno interface
484 (*pUnoI->release)( pUnoI );
485 *ppException = 0;
486 break;
487 case 0: // queryInterface() opt
489 typelib_TypeDescription * pTD = 0;
490 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
491 if (pTD)
493 uno_Interface * pInterface = 0;
494 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
495 pThis->pBridge->getUnoEnv(),
496 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
498 if (pInterface)
500 ::uno_any_construct(
501 reinterpret_cast< uno_Any * >( pReturn ),
502 &pInterface, pTD, 0 );
503 (*pInterface->release)( pInterface );
504 TYPELIB_DANGER_RELEASE( pTD );
505 *ppException = 0;
506 break;
508 TYPELIB_DANGER_RELEASE( pTD );
510 } // else perform queryInterface()
511 default:
512 // dependent dispatch
513 cpp_call(
514 pThis, aVtableSlot,
515 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
516 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
517 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
518 pReturn, pArgs, ppException );
520 break;
522 default:
524 ::com::sun::star::uno::RuntimeException aExc(
525 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
526 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
528 Type const & rExcType = ::getCppuType( &aExc );
529 // binary identical null reference
530 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
535 } } }
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */