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"
34 #include <sys/types.h>
35 #include <sys/malloc.h>
37 #include <com/sun/star/uno/genfunc.hxx>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
43 #include "bridges/cpp_uno/shared/vtables.hxx"
48 using namespace ::rtl
;
49 using namespace ::com::sun::star::uno
;
54 //==================================================================================================
55 static void callVirtualMethod(
56 void * pAdjustedThisPtr
,
57 sal_Int32 nVtableIndex
,
58 void * pRegisterReturn
,
59 typelib_TypeClass eReturnType
,
61 sal_Int32
* pStackLongs
,
62 sal_Int32
/* nStackLongs */)
65 // parameter list is mixed list of * and values
66 // reference parameters are pointers
68 // the basic idea here is to use gpr[8] as a storage area for
69 // the future values of registers r3 to r10 needed for the call,
70 // and similarly fpr[13] as a storage area for the future values
71 // of floating point registers f1 to f13
73 unsigned long * mfunc
; // actual function to be invoked
75 int gpr
[8]; // storage for gpregisters, map to r3-r10
76 int off
; // offset used to find function
77 double fpr
[13]; // storage for fpregisters, map to f1-f13
78 int n
; // number of gprs mapped so far
79 int f
; // number of fprs mapped so far
80 volatile long *p
; // pointer to parameter overflow area
81 int c
; // character of parameter type being decoded
82 volatile double dret
; // temporary function return values
83 volatile int iret
, iret2
;
85 // Because of the Power PC calling conventions we could be passing
86 // parameters in both register types and on the stack. To create the
87 // stack parameter area we need we now simply allocate local
88 // variable storage param[] that is at least the size of the parameter stack
89 // (more than enough space) which we can overwrite the parameters into.
91 // Note: This keeps us from having to decode the signature twice and
92 // prevents problems with later local variables.
94 // FIXME: I do not believe the following is true but we will keep the
95 // FIXME: extra space just to be safe until proven otherwise
97 // Note: could require up to 2*nStackLongs words of parameter stack area
98 // if the call has many float parameters (i.e. floats take up only 1
99 // word on the stack but take 2 words in parameter area in the
103 // unsigned long param[(2*nStackLongs)];
105 /* now begin to load the C++ function arguments into storage */
110 /* set up a pointer to the stack parameter area */
111 __asm__ ( "addi %0,r1,24" : "=r" (p
) : /* no inputs */ );
113 // #i94421#, work around compiler error:
114 volatile long * pCopy
= p
;
115 (void) pCopy
; // avoid warning about unused variable
118 // if (! pAdjustedThisPtr )CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
121 // now we need to parse the entire signature string
122 // until we get the END indicator
124 // treat complex return pointer like any other parameter
126 // parse the argument list up to the ending )
128 while (*pPT
!= 'X') {
132 case 'D': /* type is double */
134 fpr
[f
++] = *((double *)pStackLongs
); /* store in register */
138 *p
++ = *pStackLongs
; /* or on the parameter stack */
139 *p
++ = *(pStackLongs
+ 1);
144 case 'F': /* type is float */
145 /* floats are stored as 1 32 bit word on param stack */
147 fpr
[f
++] = *((float *)pStackLongs
);
151 *((float *)p
) = *((float *)pStackLongs
);
157 case 'H': /* type is long long */
160 gpr
[n
++] = *pStackLongs
;
167 gpr
[n
++] = *(pStackLongs
+1);
171 *p
++ = *(pStackLongs
+1);
177 gpr
[n
++] = *((unsigned short*)pStackLongs
);
180 *p
++ = *((unsigned short *)pStackLongs
);
187 gpr
[n
++] = *((char *)pStackLongs
);
190 *p
++ = *((char *)pStackLongs
);
197 gpr
[n
++] = *pStackLongs
;
209 /* figure out the address of the function we need to invoke */
211 off
= off
* 4; // 4 bytes per slot
212 mfunc
= *((unsigned long **)pAdjustedThisPtr
); // get the address of the vtable
213 mfunc
= (unsigned long *)((char *)mfunc
+ off
); // get the address from the vtable entry at offset
214 mfunc
= *((unsigned long **)mfunc
); // the function is stored at the address
215 ptr
= (void (*)())mfunc
;
217 /* Set up the machine registers and invoke the function */
219 __asm__
__volatile__ (
227 "lwz r10, 28(%0)\n\t"
237 "lfd f10, 72(%1)\n\t"
238 "lfd f11, 80(%1)\n\t"
239 "lfd f12, 88(%1)\n\t"
240 "lfd f13, 96(%1)\n\t"
241 : : "r" (gpr
), "r" (fpr
)
242 : "r0", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
243 "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9",
244 "f10", "f11", "f12", "f13"
250 __asm__
__volatile__ (
254 : : "m" (dret
), "m" (iret
), "m" (iret2
)
258 switch( eReturnType
)
260 case typelib_TypeClass_HYPER
:
261 case typelib_TypeClass_UNSIGNED_HYPER
:
262 ((long*)pRegisterReturn
)[1] = iret2
;
263 // fall thru on purpose
264 case typelib_TypeClass_LONG
:
265 case typelib_TypeClass_UNSIGNED_LONG
:
266 case typelib_TypeClass_ENUM
:
267 ((long*)pRegisterReturn
)[0] = iret
;
270 case typelib_TypeClass_CHAR
:
271 case typelib_TypeClass_SHORT
:
272 case typelib_TypeClass_UNSIGNED_SHORT
:
273 *(unsigned short*)pRegisterReturn
= (unsigned short)iret
;
276 case typelib_TypeClass_BOOLEAN
:
277 case typelib_TypeClass_BYTE
:
278 *(unsigned char*)pRegisterReturn
= (unsigned char)iret
;
281 case typelib_TypeClass_FLOAT
:
282 *(float*)pRegisterReturn
= (float)dret
;
285 case typelib_TypeClass_DOUBLE
:
286 *(double*)pRegisterReturn
= dret
;
294 //==================================================================================================
295 static void cpp_call(
296 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
297 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
298 typelib_TypeDescriptionReference
* pReturnTypeRef
,
299 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
300 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
302 // max space for: [complex ret ptr], values|ptr ...
304 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
305 char * pCppStackStart
= pCppStack
;
307 // need to know parameter types for callVirtualMethod so generate a signature string
308 char * pParamType
= (char *) alloca(nParams
+2);
309 char * pPT
= pParamType
;
312 typelib_TypeDescription
* pReturnTypeDescr
= 0;
313 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
314 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
316 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
318 if (pReturnTypeDescr
)
320 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
322 pCppReturn
= pUnoReturn
; // direct way for simple types
326 // complex return via ptr
327 pCppReturn
= *(void **)pCppStack
328 = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
329 ? alloca( pReturnTypeDescr
->nSize
)
330 : pUnoReturn
); // direct way
331 *pPT
++ = 'C'; //signify that a complex return type on stack
332 pCppStack
+= sizeof(void *);
336 void * pAdjustedThisPtr
= reinterpret_cast< void ** >(pThis
->getCppI())
337 + aVtableSlot
.offset
;
338 *(void**)pCppStack
= pAdjustedThisPtr
;
339 pCppStack
+= sizeof( void* );
343 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
345 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
346 // indizes of values this have to be converted (interface conversion cpp<=>uno)
347 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
348 // type descriptions for reconversions
349 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
351 sal_Int32 nTempIndizes
= 0;
353 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
355 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
356 typelib_TypeDescription
* pParamTypeDescr
= 0;
357 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
360 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
362 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
363 pThis
->getBridge()->getUno2Cpp() );
365 switch (pParamTypeDescr
->eTypeClass
)
368 // we need to know type of each param so that we know whether to use
369 // gpr or fpr to pass in parameters:
370 // Key: I - int, long, pointer, etc means pass in gpr
371 // B - byte value passed in gpr
372 // S - short value passed in gpr
373 // F - float value pass in fpr
374 // D - double value pass in fpr
375 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
376 // X - indicates end of parameter description string
378 case typelib_TypeClass_LONG
:
379 case typelib_TypeClass_UNSIGNED_LONG
:
380 case typelib_TypeClass_ENUM
:
383 case typelib_TypeClass_SHORT
:
384 case typelib_TypeClass_CHAR
:
385 case typelib_TypeClass_UNSIGNED_SHORT
:
388 case typelib_TypeClass_BOOLEAN
:
389 case typelib_TypeClass_BYTE
:
392 case typelib_TypeClass_FLOAT
:
395 case typelib_TypeClass_DOUBLE
:
397 pCppStack
+= sizeof(sal_Int32
); // extra long
399 case typelib_TypeClass_HYPER
:
400 case typelib_TypeClass_UNSIGNED_HYPER
:
402 pCppStack
+= sizeof(sal_Int32
); // extra long
408 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
410 else // ptr to complex value | ref
412 if (! rParam
.bIn
) // is pure out
414 // cpp out is constructed mem, uno out is not!
416 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
418 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
419 // will be released at reconversion
420 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
423 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
425 uno_copyAndConvertData(
426 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
427 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
429 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
430 // will be released at reconversion
431 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
435 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
437 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
441 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
444 // terminate the signature string
450 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
452 pAdjustedThisPtr
, aVtableSlot
.index
,
453 pCppReturn
, pReturnTypeDescr
->eTypeClass
, pParamType
,
454 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
455 // NO exception occured...
458 // reconvert temporary params
459 for ( ; nTempIndizes
--; )
461 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
462 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
464 if (pParams
[nIndex
].bIn
)
466 if (pParams
[nIndex
].bOut
) // inout
468 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
469 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
470 pThis
->getBridge()->getCpp2Uno() );
475 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
476 pThis
->getBridge()->getCpp2Uno() );
478 // destroy temp cpp param => cpp: every param was constructed
479 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
481 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
484 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
486 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
487 pThis
->getBridge()->getCpp2Uno() );
488 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
493 // fill uno exception
494 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
, *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
497 for ( ; nTempIndizes
--; )
499 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
500 // destroy temp cpp param => cpp: every param was constructed
501 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
502 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
505 if (pReturnTypeDescr
)
506 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
512 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
514 void unoInterfaceProxyDispatch(
515 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
516 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
519 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
520 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
* > (pUnoI
);
521 // typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
523 switch (pMemberDescr
->eTypeClass
)
525 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
528 VtableSlot
aVtableSlot(
531 typelib_InterfaceAttributeTypeDescription
const * >(
536 // dependent dispatch
539 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
541 pReturn
, pArgs
, ppException
);
546 typelib_MethodParameter aParam
;
548 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
549 aParam
.bIn
= sal_True
;
550 aParam
.bOut
= sal_False
;
552 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
553 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
554 typelib_typedescriptionreference_new(
555 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
557 // dependent dispatch
558 aVtableSlot
.index
+= 1; //get then set method
563 pReturn
, pArgs
, ppException
);
565 typelib_typedescriptionreference_release( pReturnTypeRef
);
570 case typelib_TypeClass_INTERFACE_METHOD
:
573 VtableSlot
aVtableSlot(
576 typelib_InterfaceMethodTypeDescription
const * >(
578 switch (aVtableSlot
.index
)
581 case 1: // acquire uno interface
582 (*pUnoI
->acquire
)( pUnoI
);
585 case 2: // release uno interface
586 (*pUnoI
->release
)( pUnoI
);
589 case 0: // queryInterface() opt
591 typelib_TypeDescription
* pTD
= 0;
592 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
595 uno_Interface
* pInterface
= 0;
596 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
597 pThis
->pBridge
->getUnoEnv(),
598 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
603 reinterpret_cast< uno_Any
* >( pReturn
),
604 &pInterface
, pTD
, 0 );
605 (*pInterface
->release
)( pInterface
);
606 TYPELIB_DANGER_RELEASE( pTD
);
610 TYPELIB_DANGER_RELEASE( pTD
);
612 } // else perform queryInterface()
614 // dependent dispatch
617 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
618 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
619 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
620 pReturn
, pArgs
, ppException
);
626 ::com::sun::star::uno::RuntimeException
aExc(
627 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
628 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
630 Type
const & rExcType
= ::getCppuType( &aExc
);
631 // binary identical null reference
632 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );