1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uno2cpp.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
36 #include <com/sun/star/uno/genfunc.hxx>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/types.hxx"
41 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/vtables.hxx"
50 using namespace ::rtl
;
51 using namespace ::com::sun::star::uno
;
53 void MapReturn(long r3
, double dret
, typelib_TypeClass eTypeClass
, void *pRegisterReturn
)
57 case typelib_TypeClass_HYPER
:
58 case typelib_TypeClass_UNSIGNED_HYPER
:
59 *reinterpret_cast<sal_uInt64
*>( pRegisterReturn
) = r3
;
61 case typelib_TypeClass_LONG
:
62 case typelib_TypeClass_UNSIGNED_LONG
:
63 case typelib_TypeClass_ENUM
:
64 *reinterpret_cast<sal_uInt32
*>( pRegisterReturn
) = r3
;
66 case typelib_TypeClass_CHAR
:
67 case typelib_TypeClass_SHORT
:
68 case typelib_TypeClass_UNSIGNED_SHORT
:
69 *reinterpret_cast<sal_uInt16
*>( pRegisterReturn
) = (unsigned short)r3
;
71 case typelib_TypeClass_BOOLEAN
:
72 case typelib_TypeClass_BYTE
:
73 *reinterpret_cast<sal_uInt8
*>( pRegisterReturn
) = (unsigned char)r3
;
75 case typelib_TypeClass_FLOAT
:
76 *reinterpret_cast<float *>( pRegisterReturn
) = dret
;
78 case typelib_TypeClass_DOUBLE
:
79 *reinterpret_cast<double *>( pRegisterReturn
) = dret
;
88 //==================================================================================================
89 static void callVirtualMethod(void * pThis
, sal_uInt32 nVtableIndex
,
90 void * pRegisterReturn
, typelib_TypeDescription
* pReturnTypeDescr
,
91 sal_uInt64
*pStack
, sal_uInt32 nStack
,
92 sal_uInt64
*pGPR
, sal_uInt32 nGPR
,
93 double *pFPR
, sal_uInt32 nFPR
)
95 // Stack, if used, must be 16-bytes aligned
97 nStack
= ( nStack
+ 1 ) & ~1;
99 // Should not happen, but...
100 if ( nFPR
> ppc64::MAX_SSE_REGS
)
101 nFPR
= ppc64::MAX_SSE_REGS
;
102 if ( nGPR
> ppc64::MAX_GPR_REGS
)
103 nGPR
= ppc64::MAX_GPR_REGS
;
106 // Let's figure out what is really going on here
108 fprintf( stderr
, "= callVirtualMethod() =\nGPR's (%d): ", nGPR
);
109 for ( int i
= 0; i
< nGPR
; ++i
)
110 fprintf( stderr
, "0x%lx, ", pGPR
[i
] );
111 fprintf( stderr
, "\nFPR's (%d): ", nFPR
);
112 for ( int i
= 0; i
< nFPR
; ++i
)
113 fprintf( stderr
, "0x%lx (%f), ", pFPR
[i
], pFPR
[i
] );
114 fprintf( stderr
, "\nStack (%d): ", nStack
);
115 for ( int i
= 0; i
< nStack
; ++i
)
116 fprintf( stderr
, "0x%lx, ", pStack
[i
] );
117 fprintf( stderr
, "\n" );
121 // Load parameters to stack, if necessary
122 sal_uInt64
*stack
= (sal_uInt64
*) __builtin_alloca( nStack
* 8 );
123 memcpy( stack
, pStack
, nStack
* 8 );
125 // Get pointer to method
126 sal_uInt64 pMethod
= *((sal_uInt64
*)pThis
);
127 pMethod
+= 8 * nVtableIndex
;
128 pMethod
= *((sal_uInt64
*)pMethod
);
130 typedef void (* FunctionCall
)( sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
);
131 FunctionCall pFunc
= (FunctionCall
)pMethod
;
133 volatile double dret
;
136 __asm__
__volatile__ (
158 : : "r" (pGPR
), "r" (pFPR
)
159 : "r0", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
160 "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7", "fr8", "fr9",
161 "fr10", "fr11", "fr12", "fr13"
164 // tell gcc that r3 to r11 are not available to it for doing the TOC and exception munge on the func call
165 register sal_uInt64 r3
asm("r3");
166 register sal_uInt64 r4
asm("r4");
167 register sal_uInt64 r5
asm("r5");
168 register sal_uInt64 r6
asm("r6");
169 register sal_uInt64 r7
asm("r7");
170 register sal_uInt64 r8
asm("r8");
171 register sal_uInt64 r9
asm("r9");
172 register sal_uInt64 r10
asm("r10");
173 register sal_uInt64 r11
asm("r11");
175 (*pFunc
)(r3
, r4
, r5
, r6
, r7
, r8
, r9
, r10
);
178 __asm__
__volatile__ (
182 : "=f" (dret
), "=r" (r3
), "=r" (r4
) : );
184 MapReturn(r3
, dret
, pReturnTypeDescr
->eTypeClass
, pRegisterReturn
);
187 // Macros for easier insertion of values to registers or stack
188 // pSV - pointer to the source
189 // nr - order of the value [will be increased if stored to register]
190 // pFPR, pGPR - pointer to the registers
191 // pDS - pointer to the stack [will be increased if stored here]
193 // The value in %xmm register is already prepared to be retrieved as a float,
194 // thus we treat float and double the same
195 #define INSERT_FLOAT( pSV, nr, pFPR, pDS, bOverflow ) \
196 if ( nr < ppc64::MAX_SSE_REGS ) \
197 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
201 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
203 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS, bOverflow ) \
204 if ( nr < ppc64::MAX_SSE_REGS ) \
205 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
209 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
211 #define INSERT_INT64( pSV, nr, pGPR, pDS, bOverflow ) \
212 if ( nr < ppc64::MAX_GPR_REGS ) \
213 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
217 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
219 #define INSERT_INT32( pSV, nr, pGPR, pDS, bOverflow ) \
220 if ( nr < ppc64::MAX_GPR_REGS ) \
221 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
225 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
227 #define INSERT_INT16( pSV, nr, pGPR, pDS, bOverflow ) \
228 if ( nr < ppc64::MAX_GPR_REGS ) \
229 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
233 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
235 #define INSERT_INT8( pSV, nr, pGPR, pDS, bOverflow ) \
236 if ( nr < ppc64::MAX_GPR_REGS ) \
237 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
241 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
243 //==================================================================================================
244 static void cpp_call(
245 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
246 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
247 typelib_TypeDescriptionReference
* pReturnTypeRef
,
248 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
249 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
251 // max space for: [complex ret ptr], values|ptr ...
252 sal_uInt64
* pStack
= (sal_uInt64
*)alloca( (nParams
+3) * sizeof(sal_Int64
) );
253 sal_uInt64
* pStackStart
= pStack
;
255 sal_uInt64 pGPR
[ppc64::MAX_GPR_REGS
];
258 double pFPR
[ppc64::MAX_SSE_REGS
];
262 typelib_TypeDescription
* pReturnTypeDescr
= 0;
263 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
264 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
266 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
268 bool bOverFlow
= false;
270 if (pReturnTypeDescr
)
273 fprintf(stderr
, "return type is %d\n", pReturnTypeDescr
->eTypeClass
);
275 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
277 pCppReturn
= pUnoReturn
; // direct way for simple types
279 fprintf(stderr
, "simple return\n");
284 // complex return via ptr
285 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
286 ? alloca( pReturnTypeDescr
->nSize
) : pUnoReturn
);
288 fprintf(stderr
, "pCppReturn/pUnoReturn is %lx/%lx", pCppReturn
, pUnoReturn
);
290 INSERT_INT64( &pCppReturn
, nGPR
, pGPR
, pStack
, bOverFlow
);
293 // push "this" pointer
294 void * pAdjustedThisPtr
= reinterpret_cast< void ** >( pThis
->getCppI() ) + aVtableSlot
.offset
;
296 fprintf(stderr
, "this pointer is %p\n", pAdjustedThisPtr
);
298 INSERT_INT64( &pAdjustedThisPtr
, nGPR
, pGPR
, pStack
, bOverFlow
);
301 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
302 // indizes of values this have to be converted (interface conversion cpp<=>uno)
303 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
304 // type descriptions for reconversions
305 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
307 sal_Int32 nTempIndizes
= 0;
310 fprintf(stderr
, "n params is %d\n", nParams
);
313 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
315 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
316 typelib_TypeDescription
* pParamTypeDescr
= 0;
317 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
320 fprintf(stderr
, "param %d is %d %d %d\n", nPos
, rParam
.bOut
, bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
),
321 pParamTypeDescr
->eTypeClass
);
324 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
326 // uno_copyAndConvertData( pCppArgs[nPos] = alloca( 8 ), pUnoArgs[nPos], pParamTypeDescr,
327 uno_copyAndConvertData( pCppArgs
[nPos
] = pStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
328 pThis
->getBridge()->getUno2Cpp() );
329 switch (pParamTypeDescr
->eTypeClass
)
331 case typelib_TypeClass_HYPER
:
332 case typelib_TypeClass_UNSIGNED_HYPER
:
334 fprintf(stderr
, "hyper is %lx\n", pCppArgs
[nPos
]);
336 INSERT_INT64( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
, bOverFlow
);
338 case typelib_TypeClass_LONG
:
339 case typelib_TypeClass_UNSIGNED_LONG
:
340 case typelib_TypeClass_ENUM
:
342 fprintf(stderr
, "long is %x\n", pCppArgs
[nPos
]);
344 INSERT_INT32( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
, bOverFlow
);
346 case typelib_TypeClass_SHORT
:
347 case typelib_TypeClass_CHAR
:
348 case typelib_TypeClass_UNSIGNED_SHORT
:
349 INSERT_INT16( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
, bOverFlow
);
351 case typelib_TypeClass_BOOLEAN
:
352 case typelib_TypeClass_BYTE
:
353 INSERT_INT8( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
, bOverFlow
);
355 case typelib_TypeClass_FLOAT
:
356 INSERT_FLOAT( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
, bOverFlow
);
358 case typelib_TypeClass_DOUBLE
:
359 INSERT_DOUBLE( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
, bOverFlow
);
364 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
367 else // ptr to complex value | ref
370 fprintf(stderr
, "complex type again %d\n", rParam
.bIn
);
372 if (! rParam
.bIn
) // is pure out
375 fprintf(stderr
, "complex size is %d\n", pParamTypeDescr
->nSize
);
377 // cpp out is constructed mem, uno out is not!
379 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
381 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
382 // will be released at reconversion
383 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
386 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
389 fprintf(stderr
, "this one\n");
391 uno_copyAndConvertData(
392 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
393 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
395 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
396 // will be released at reconversion
397 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
402 fprintf(stderr
, "that one, passing %lx through\n", pUnoArgs
[nPos
]);
404 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
406 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
408 INSERT_INT64( &(pCppArgs
[nPos
]), nGPR
, pGPR
, pStack
, bOverFlow
);
415 pAdjustedThisPtr
, aVtableSlot
.index
,
416 pCppReturn
, pReturnTypeDescr
,
417 pStackStart
, ( pStack
- pStackStart
),
420 // NO exception occured...
423 // reconvert temporary params
424 for ( ; nTempIndizes
--; )
426 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
427 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
429 if (pParams
[nIndex
].bIn
)
431 if (pParams
[nIndex
].bOut
) // inout
433 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
434 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
435 pThis
->getBridge()->getCpp2Uno() );
440 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
441 pThis
->getBridge()->getCpp2Uno() );
443 // destroy temp cpp param => cpp: every param was constructed
444 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
446 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
449 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
451 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
452 pThis
->getBridge()->getCpp2Uno() );
453 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
458 // fill uno exception
459 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
460 *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
463 for ( ; nTempIndizes
--; )
465 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
466 // destroy temp cpp param => cpp: every param was constructed
467 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
468 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
471 if (pReturnTypeDescr
)
472 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
478 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
480 void unoInterfaceProxyDispatch(
481 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
482 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
485 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
486 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
487 typelib_InterfaceTypeDescription
* pTypeDescr
= pThis
->pTypeDescr
;
489 switch (pMemberDescr
->eTypeClass
)
491 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
494 VtableSlot
aVtableSlot(
497 typelib_InterfaceAttributeTypeDescription
const * >(
502 // dependent dispatch
505 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
507 pReturn
, pArgs
, ppException
);
512 typelib_MethodParameter aParam
;
514 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
515 aParam
.bIn
= sal_True
;
516 aParam
.bOut
= sal_False
;
518 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
519 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
520 typelib_typedescriptionreference_new(
521 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
523 // dependent dispatch
524 aVtableSlot
.index
+= 1; //get then set method
529 pReturn
, pArgs
, ppException
);
531 typelib_typedescriptionreference_release( pReturnTypeRef
);
536 case typelib_TypeClass_INTERFACE_METHOD
:
539 VtableSlot
aVtableSlot(
542 typelib_InterfaceMethodTypeDescription
const * >(
544 switch (aVtableSlot
.index
)
547 case 1: // acquire uno interface
548 (*pUnoI
->acquire
)( pUnoI
);
551 case 2: // release uno interface
552 (*pUnoI
->release
)( pUnoI
);
555 case 0: // queryInterface() opt
557 typelib_TypeDescription
* pTD
= 0;
558 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
561 uno_Interface
* pInterface
= 0;
562 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
563 pThis
->pBridge
->getUnoEnv(),
564 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
569 reinterpret_cast< uno_Any
* >( pReturn
),
570 &pInterface
, pTD
, 0 );
571 (*pInterface
->release
)( pInterface
);
572 TYPELIB_DANGER_RELEASE( pTD
);
576 TYPELIB_DANGER_RELEASE( pTD
);
578 } // else perform queryInterface()
580 // dependent dispatch
583 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
584 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
585 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
586 pReturn
, pArgs
, ppException
);
592 ::com::sun::star::uno::RuntimeException
aExc(
593 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
594 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
596 Type
const & rExcType
= ::getCppuType( &aExc
);
597 // binary identical null reference
598 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );