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"
47 using namespace ::rtl
;
48 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[8] as a storage area for the future values
71 // of floating point registers f1 to f8
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
[8]; // storage for fpregisters, map to f1-f8
78 int n
; // number of gprs mapped so far
79 int f
; // number of fprs mapped so far
80 long *p
; // pointer to parameter overflow area
81 int c
; // character of parameter type being decoded
82 double dret
; // temporary function return values
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 // Note: could require up to 2*nStackLongs words of parameter stack area
95 // if the call has many float parameters (i.e. floats take up only 1
96 // word on the stack but take 2 words in parameter area in the
99 // Update! floats on the outgoing parameter stack only take up 1 word
100 // (stfs is used) which is not correct according to the ABI but we
101 // will match what the compiler does until this is figured out
103 // this grows the current stack to the appropriate size
104 // and sets the outgoing stack pointer p to the right place
105 __asm__
__volatile__ (
106 "rlwinm %0,%0,3,3,28\n\t"
108 "rlwinm %0,%0,0,4,28\n\t"
112 : : "r" (nStackLongs
) : "0" );
114 __asm__
__volatile__ ( "addi %0,1,8" : "=r" (p
) : );
117 // if (! pAdjustedThisPtr ) dummy_can_throw_anything("xxx"); // address something
120 // now begin to load the C++ function arguments into storage
124 // now we need to parse the entire signature string */
125 // until we get the END indicator */
127 // treat complex return pointer like any other parameter //
130 /* Let's figure out what is really going on here*/
131 fprintf(stderr
,"callVirtualMethod paramters string is %s\n",pPT
);
133 long * q
= (long *)pStackLongs
;
135 fprintf(stderr
,"uno stack is: %x\n",*q
);
141 /* parse the argument list up to the ending ) */
142 while (*pPT
!= 'X') {
145 case 'D': /* type is double */
147 fpr
[f
++] = *((double *)pStackLongs
); /* store in register */
151 *p
++ = *pStackLongs
; /* or on the parameter stack */
152 *p
++ = *(pStackLongs
+ 1);
157 case 'F': /* type is float */
158 /* this assumes that floats are stored as 1 32 bit word on param
159 stack and that if passed in parameter stack to C, should be
162 Whoops: the abi is not actually followed by gcc, need to
163 store floats as a *single* word on outgoing parameter stack
164 to match what gcc actually does
167 fpr
[f
++] = *((float *)pStackLongs
);
169 #if 0 /* if abi were followed */
172 *((double *)p
) = *((float *)pStackLongs
);
175 *((float *)p
) = *((float *)pStackLongs
);
182 case 'H': /* type is long long */
183 if (n
& 1) n
++; /* note even elements gpr[] will map to
186 gpr
[n
++] = *pStackLongs
;
187 gpr
[n
++] = *(pStackLongs
+1);
192 *p
++ = *(pStackLongs
+1);
199 gpr
[n
++] = *((unsigned short*)pStackLongs
);
201 *p
++ = *((unsigned short *)pStackLongs
);
208 gpr
[n
++] = *((char *)pStackLongs
);
210 *p
++ = *((char *)pStackLongs
);
217 gpr
[n
++] = *pStackLongs
;
227 /* figure out the address of the function we need to invoke */
229 off
= off
* 4; // 4 bytes per slot
230 mfunc
= *((unsigned long **)pAdjustedThisPtr
); // get the address of the vtable
231 mfunc
= (unsigned long *)((char *)mfunc
+ off
); // get the address from the vtable entry at offset
232 mfunc
= *((unsigned long **)mfunc
); // the function is stored at the address
233 ptr
= (void (*)())mfunc
;
235 /* Set up the machine registers and invoke the function */
237 __asm__
__volatile__ (
254 : : "r" (gpr
), "r" (fpr
)
255 : "0", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
260 __asm__
__volatile__ (
264 : "=f" (dret
), "=r" (iret
), "=r" (iret2
) : );
266 switch( eReturnType
)
268 case typelib_TypeClass_HYPER
:
269 case typelib_TypeClass_UNSIGNED_HYPER
:
270 ((long*)pRegisterReturn
)[0] = iret
;
271 ((long*)pRegisterReturn
)[1] = iret2
;
272 case typelib_TypeClass_LONG
:
273 case typelib_TypeClass_UNSIGNED_LONG
:
274 case typelib_TypeClass_ENUM
:
275 ((long*)pRegisterReturn
)[0] = iret
;
277 case typelib_TypeClass_CHAR
:
278 case typelib_TypeClass_SHORT
:
279 case typelib_TypeClass_UNSIGNED_SHORT
:
280 *(unsigned short*)pRegisterReturn
= (unsigned short)iret
;
282 case typelib_TypeClass_BOOLEAN
:
283 case typelib_TypeClass_BYTE
:
284 *(unsigned char*)pRegisterReturn
= (unsigned char)iret
;
286 case typelib_TypeClass_FLOAT
:
287 *(float*)pRegisterReturn
= (float)dret
;
289 case typelib_TypeClass_DOUBLE
:
290 *(double*)pRegisterReturn
= dret
;
296 //==================================================================================================
297 static void cpp_call(
298 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
299 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
300 typelib_TypeDescriptionReference
* pReturnTypeRef
,
301 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
302 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
304 // max space for: [complex ret ptr], values|ptr ...
306 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
307 char * pCppStackStart
= pCppStack
;
309 // need to know parameter types for callVirtualMethod so generate a signature string
310 char * pParamType
= (char *) alloca(nParams
+2);
311 char * pPT
= pParamType
;
314 typelib_TypeDescription
* pReturnTypeDescr
= 0;
315 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
316 // OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
318 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
320 if (pReturnTypeDescr
)
322 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
324 pCppReturn
= pUnoReturn
; // direct way for simple types
328 // complex return via ptr
329 pCppReturn
= *(void **)pCppStack
=
330 (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
331 ? alloca( pReturnTypeDescr
->nSize
): pUnoReturn
); // direct way
332 *pPT
++ = 'I'; //signify that a complex return type on stack
333 pCppStack
+= sizeof(void *);
337 void* pAdjustedThisPtr
= reinterpret_cast< void **>(pThis
->getCppI()) + 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
);
359 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
361 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
362 pThis
->getBridge()->getUno2Cpp() );
364 switch (pParamTypeDescr
->eTypeClass
)
367 // we need to know type of each param so that we know whether to use
368 // gpr or fpr to pass in parameters:
369 // Key: I - int, long, pointer, etc means pass in gpr
370 // B - byte value passed in gpr
371 // S - short value passed in gpr
372 // F - float value pass in fpr
373 // D - double value pass in fpr
374 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
375 // X - indicates end of parameter description string
377 case typelib_TypeClass_LONG
:
378 case typelib_TypeClass_UNSIGNED_LONG
:
379 case typelib_TypeClass_ENUM
:
382 case typelib_TypeClass_SHORT
:
383 case typelib_TypeClass_CHAR
:
384 case typelib_TypeClass_UNSIGNED_SHORT
:
387 case typelib_TypeClass_BOOLEAN
:
388 case typelib_TypeClass_BYTE
:
391 case typelib_TypeClass_FLOAT
:
394 case typelib_TypeClass_DOUBLE
:
396 pCppStack
+= sizeof(sal_Int32
); // extra long
398 case typelib_TypeClass_HYPER
:
399 case typelib_TypeClass_UNSIGNED_HYPER
:
401 pCppStack
+= sizeof(sal_Int32
); // extra long
405 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
407 else // ptr to complex value | ref
409 if (! rParam
.bIn
) // is pure out
411 // cpp out is constructed mem, uno out is not!
413 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
415 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
416 // will be released at reconversion
417 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
420 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
422 uno_copyAndConvertData(
423 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
424 pUnoArgs
[nPos
], pParamTypeDescr
,
425 pThis
->getBridge()->getUno2Cpp() );
427 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
428 // will be released at reconversion
429 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
433 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
435 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
437 // KBH: FIXME: is this the right way to pass these
440 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
443 // terminate the signature string
449 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
451 pAdjustedThisPtr
, aVtableSlot
.index
,
452 pCppReturn
, pReturnTypeDescr
->eTypeClass
, pParamType
,
453 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
454 // NO exception occured...
457 // reconvert temporary params
458 for ( ; nTempIndizes
--; )
460 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
461 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
463 if (pParams
[nIndex
].bIn
)
465 if (pParams
[nIndex
].bOut
) // inout
467 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
468 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
469 pThis
->getBridge()->getCpp2Uno() );
474 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
475 pThis
->getBridge()->getCpp2Uno() );
477 // destroy temp cpp param => cpp: every param was constructed
478 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
480 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
483 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
485 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
486 pThis
->getBridge()->getCpp2Uno() );
487 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
492 // fill uno exception
493 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
494 *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 );