Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_alpha / uno2cpp.cxx
blob0c4745f0a37e2e70f757ee852f594aa0e4202641
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 r0, typelib_TypeClass eTypeClass, sal_uInt64* pRegisterReturn)
51 register float fret asm("$f0");
52 register double dret asm("$f0");
54 #if OSL_DEBUG_LEVEL > 2
55 fprintf(stderr,"Mapping Return with %lx %ld %f\n", r0, r0, dret);
56 #endif
57 switch (eTypeClass)
59 case typelib_TypeClass_HYPER:
60 case typelib_TypeClass_UNSIGNED_HYPER:
61 *pRegisterReturn = r0;
62 break;
63 case typelib_TypeClass_LONG:
64 case typelib_TypeClass_UNSIGNED_LONG:
65 case typelib_TypeClass_ENUM:
66 *(unsigned int*)pRegisterReturn = (unsigned int)r0;
67 break;
68 case typelib_TypeClass_CHAR:
69 case typelib_TypeClass_SHORT:
70 case typelib_TypeClass_UNSIGNED_SHORT:
71 *(unsigned short*)pRegisterReturn = (unsigned short)r0;
72 break;
73 case typelib_TypeClass_BOOLEAN:
74 case typelib_TypeClass_BYTE:
75 *(unsigned char*)pRegisterReturn = (unsigned char)r0;
76 break;
77 case typelib_TypeClass_FLOAT:
78 *reinterpret_cast<float *>( pRegisterReturn ) = fret;
79 break;
80 case typelib_TypeClass_DOUBLE:
81 *reinterpret_cast<double *>( pRegisterReturn ) = dret;
82 break;
83 default:
84 break;
86 #if OSL_DEBUG_LEVEL > 2
87 fprintf(stderr, "end of MapReturn with %x\n", pRegisterReturn ? *pRegisterReturn : 0);
88 #endif
91 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
92 { \
93 if ( nr < axp::MAX_WORDS_IN_REGS ) \
94 { \
95 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
96 } \
97 else \
98 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); \
101 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
102 if ( nr < axp::MAX_WORDS_IN_REGS ) \
103 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
104 else \
105 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
107 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
108 if ( nr < axp::MAX_WORDS_IN_REGS ) \
109 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
110 else \
111 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
113 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
114 if ( nr < axp::MAX_WORDS_IN_REGS ) \
115 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
116 else \
117 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
119 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
120 if ( nr < axp::MAX_WORDS_IN_REGS ) \
121 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
122 else \
123 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
125 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
126 if ( nr < axp::MAX_WORDS_IN_REGS ) \
127 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
128 else \
129 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
131 namespace
133 //==================================================================================================
134 void callVirtualMethod(
135 void * pThis, sal_Int32 nVtableIndex,
136 void * pRegisterReturn, typelib_TypeDescription * pReturnTypeDescr,
137 sal_uInt64 *pStack, sal_uInt32 nStack,
138 sal_uInt64 *pGPR, sal_uInt32 nGPR,
139 double *pFPR, sal_uInt32 nFPR)
141 // Should not happen, but...
142 if ( nFPR > axp::MAX_SSE_REGS )
143 nFPR = axp::MAX_SSE_REGS;
144 if ( nGPR > axp::MAX_GPR_REGS )
145 nGPR = axp::MAX_GPR_REGS;
147 #if OSL_DEBUG_LEVEL > 2
148 // Let's figure out what is really going on here
150 fprintf( stderr, "= nStack is %d\n", nStack );
151 fprintf( stderr, "= callVirtualMethod() =\nGPR's (%d): ", nGPR );
152 for ( unsigned int i = 0; i < nGPR; ++i )
153 fprintf( stderr, "0x%lx, ", pGPR[i] );
154 fprintf( stderr, "\nFPR's (%d): ", nFPR );
155 for ( unsigned int i = 0; i < nFPR; ++i )
156 fprintf( stderr, "0x%lx (%f), ", pFPR[i], pFPR[i] );
157 fprintf( stderr, "\nStack (%d): ", nStack );
158 for ( unsigned int i = 0; i < nStack; ++i )
159 fprintf( stderr, "0x%lx, ", pStack[i] );
160 fprintf( stderr, "\n" );
161 fprintf( stderr, "pRegisterReturn is %p\n", pRegisterReturn);
163 #endif
165 // Load parameters to stack, if necessary
166 // Stack, if used, must be 8-bytes aligned
167 sal_uInt64 *stack = (sal_uInt64 *) __builtin_alloca( nStack * 8 );
168 memcpy( stack, pStack, nStack * 8 );
170 // To get pointer to method
171 // a) get the address of the vtable
172 sal_uInt64 pMethod = *((sal_uInt64 *)pThis);
173 // b) get the address from the vtable entry at offset
174 pMethod += 8 * nVtableIndex;
175 pMethod = *((sal_uInt64 *)pMethod);
177 typedef void (* FunctionCall )( sal_uInt64, sal_uInt64, sal_uInt64, sal_uInt64, sal_uInt64, sal_uInt64 );
178 FunctionCall pFunc = (FunctionCall)pMethod;
180 switch (nFPR) //deliberate fall through
182 case 6:
183 asm volatile("ldt $f16,%0" :: "m"(pFPR[5]) : "$f16");
184 case 5:
185 asm volatile("ldt $f17,%0" :: "m"(pFPR[4]) : "$f17");
186 case 4:
187 asm volatile("ldt $f18,%0" :: "m"(pFPR[3]) : "$f18");
188 case 3:
189 asm volatile("ldt $f19,%0" :: "m"(pFPR[2]) : "$f19");
190 case 2:
191 asm volatile("ldt $f20,%0" :: "m"(pFPR[1]) : "$f20");
192 case 1:
193 asm volatile("ldt $f21,%0" :: "m"(pFPR[0]) : "$f21");
194 default:
195 break;
198 (*pFunc)(pGPR[0], pGPR[1], pGPR[2], pGPR[3], pGPR[4], pGPR[5]);
199 register sal_uInt64 r0 __asm__("$0");
200 MapReturn(r0, pReturnTypeDescr->eTypeClass, (sal_uInt64*)pRegisterReturn);
204 //============================================================================
205 static void cpp_call(
206 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
207 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
208 typelib_TypeDescriptionReference * pReturnTypeRef,
209 sal_Int32 nParams, typelib_MethodParameter * pParams,
210 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
212 // max space for: [complex ret ptr], values|ptr ...
213 sal_uInt64 * pStack = (sal_uInt64 *)alloca( (nParams+3) * sizeof(sal_Int64) );
214 sal_uInt64 * pStackStart = pStack;
216 sal_uInt64 pGPR[axp::MAX_GPR_REGS];
217 double pFPR[axp::MAX_SSE_REGS];
218 sal_uInt32 nRegs = 0;
220 // return
221 typelib_TypeDescription * pReturnTypeDescr = 0;
222 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
223 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
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, nRegs, pGPR, pStack );
242 // push "this" pointer
243 void * pAdjustedThisPtr = reinterpret_cast< void ** >( pThis->getCppI() ) + aVtableSlot.offset;
245 INSERT_INT64( &pAdjustedThisPtr, nRegs, pGPR, pStack );
247 // stack space
248 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64), "### unexpected size!" );
249 // args
250 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
251 // indizes of values this have to be converted (interface conversion cpp<=>uno)
252 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
253 // type descriptions for reconversions
254 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
256 sal_Int32 nTempIndizes = 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], nRegs, 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], nRegs, 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], nRegs, pGPR, pStack );
284 break;
285 case typelib_TypeClass_BOOLEAN:
286 case typelib_TypeClass_BYTE:
287 INSERT_INT8( pCppArgs[nPos], nRegs, pGPR, pStack );
288 break;
289 case typelib_TypeClass_FLOAT:
290 INSERT_FLOAT( pCppArgs[nPos], nRegs, pFPR, pStack );
291 break;
292 case typelib_TypeClass_DOUBLE:
293 INSERT_DOUBLE( pCppArgs[nPos], nRegs, 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 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
311 // will be released at reconversion
312 ppTempParamTypeDescr[nTempIndizes++] = 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 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
322 // will be released at reconversion
323 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
325 else // direct way
327 pCppArgs[nPos] = pUnoArgs[nPos];
328 // no longer needed
329 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
331 INSERT_INT64( &(pCppArgs[nPos]), nRegs, pGPR, pStack );
337 callVirtualMethod(
338 pAdjustedThisPtr, aVtableSlot.index,
339 pCppReturn, pReturnTypeDescr,
340 pStackStart, (pStack - pStackStart),
341 pGPR, nRegs,
342 pFPR, nRegs );
343 // NO exception occurred...
344 *ppUnoExc = 0;
346 // reconvert temporary params
347 for ( ; nTempIndizes--; )
349 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
350 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
352 if (pParams[nIndex].bIn)
354 if (pParams[nIndex].bOut) // inout
356 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
357 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
358 pThis->getBridge()->getCpp2Uno() );
361 else // pure out
363 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
364 pThis->getBridge()->getCpp2Uno() );
366 // destroy temp cpp param => cpp: every param was constructed
367 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
369 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
371 // return value
372 if (pCppReturn && pUnoReturn != pCppReturn)
374 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
375 pThis->getBridge()->getCpp2Uno() );
376 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
379 catch (...)
381 // fill uno exception
382 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
383 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
386 // temporary params
387 for ( ; nTempIndizes--; )
389 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
390 // destroy temp cpp param => cpp: every param was constructed
391 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
392 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
394 // return type
395 if (pReturnTypeDescr)
396 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
401 namespace bridges { namespace cpp_uno { namespace shared {
403 void unoInterfaceProxyDispatch(
404 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
405 void * pReturn, void * pArgs[], uno_Any ** ppException )
407 #if OSL_DEBUG_LEVEL > 2
408 fprintf(stderr, "unoInterfaceProxyDispatch\n");
409 #endif
412 // is my surrogate
413 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
414 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
416 switch (pMemberDescr->eTypeClass)
418 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
421 VtableSlot aVtableSlot(
422 getVtableSlot(
423 reinterpret_cast<
424 typelib_InterfaceAttributeTypeDescription const * >(
425 pMemberDescr)));
427 if (pReturn)
429 // dependent dispatch
430 cpp_call(
431 pThis, aVtableSlot,
432 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
433 0, 0, // no params
434 pReturn, pArgs, ppException );
436 else
438 // is SET
439 typelib_MethodParameter aParam;
440 aParam.pTypeRef =
441 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
442 aParam.bIn = sal_True;
443 aParam.bOut = sal_False;
445 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
446 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
447 typelib_typedescriptionreference_new(
448 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
450 // dependent dispatch
451 aVtableSlot.index += 1; //get then set method
452 cpp_call(
453 pThis, aVtableSlot,
454 pReturnTypeRef,
455 1, &aParam,
456 pReturn, pArgs, ppException );
458 typelib_typedescriptionreference_release( pReturnTypeRef );
461 break;
463 case typelib_TypeClass_INTERFACE_METHOD:
466 VtableSlot aVtableSlot(
467 getVtableSlot(
468 reinterpret_cast<
469 typelib_InterfaceMethodTypeDescription const * >(
470 pMemberDescr)));
471 switch (aVtableSlot.index)
473 // standard calls
474 case 1: // acquire uno interface
475 (*pUnoI->acquire)( pUnoI );
476 *ppException = 0;
477 break;
478 case 2: // release uno interface
479 (*pUnoI->release)( pUnoI );
480 *ppException = 0;
481 break;
482 case 0: // queryInterface() opt
484 typelib_TypeDescription * pTD = 0;
485 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
486 if (pTD)
488 uno_Interface * pInterface = 0;
489 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
490 pThis->pBridge->getUnoEnv(),
491 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
493 if (pInterface)
495 ::uno_any_construct(
496 reinterpret_cast< uno_Any * >( pReturn ),
497 &pInterface, pTD, 0 );
498 (*pInterface->release)( pInterface );
499 TYPELIB_DANGER_RELEASE( pTD );
500 *ppException = 0;
501 break;
503 TYPELIB_DANGER_RELEASE( pTD );
505 } // else perform queryInterface()
506 default:
507 // dependent dispatch
508 cpp_call(
509 pThis, aVtableSlot,
510 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
511 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
512 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
513 pReturn, pArgs, ppException );
515 break;
517 default:
519 ::com::sun::star::uno::RuntimeException aExc(
520 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
521 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
523 Type const & rExcType = ::getCppuType( &aExc );
524 // binary identical null reference
525 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
530 } } }
532 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */