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 ************************************************************************/
33 #include <com/sun/star/uno/genfunc.hxx>
36 #include "bridges/cpp_uno/shared/bridge.hxx"
37 #include "bridges/cpp_uno/shared/types.hxx"
38 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
39 #include "bridges/cpp_uno/shared/vtables.hxx"
49 using namespace ::rtl
;
50 using namespace ::com::sun::star::uno
;
56 //==================================================================================================
57 static void callVirtualMethod(
58 void * pAdjustedThisPtr
,
59 sal_Int32 nVtableIndex
,
60 void * pRegisterReturn
,
61 typelib_TypeClass eReturnType
,
63 sal_Int32
* pStackLongs
,
64 sal_Int32
/*nStackLongs*/)
67 // parameter list is mixed list of * and values
68 // reference parameters are pointers
70 unsigned long * mfunc
; // actual function to be invoked
72 int gpr
[4]; // storage for gpregisters, map to a0-a3
73 int off
; // offset used to find function
74 int nw
; // number of words mapped
75 long *p
; // pointer to parameter overflow area
76 int c
; // character of parameter type being decoded
77 int iret
, iret2
; // temporary function return values
80 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
83 fprintf(stderr
,"in CallVirtualMethod\n");
86 // Because of the MIPS O32 calling conventions we could be passing
87 // parameters in both register types and on the stack. To create the
88 // stack parameter area we need we now simply allocate local
89 // variable storage param[] that is at least the size of the parameter stack
90 // (more than enough space) which we can overwrite the parameters into.
92 /* p = sp - 512; new sp will be p - 16, but we don't change sp
93 * at this time to avoid breaking ABI--not sure whether changing sp will break
94 * references to local variables. For the same reason, we use abosulte value.
96 __asm__
__volatile__ (
97 "addiu $2,$29,-512\n\t"
99 :"=r"(p
): : "$2","$29" );
102 if (nStackLongs
* 4 > 512 )
103 fprintf(stderr
,"too many arguments");
106 // now begin to load the C++ function arguments into storage
109 // now we need to parse the entire signature string */
110 // until we get the END indicator */
112 // treat complex return pointer like any other parameter //
115 fprintf(stderr
,"overflow area pointer p=%p\n",p
);
117 /* Let's figure out what is really going on here*/
118 fprintf(stderr
,"callVirtualMethod paramters string is %s\n",pPT
);
120 long * q
= (long *)pStackLongs
;
122 fprintf(stderr
,"uno stack is: %x\n",(unsigned int)*q
);
128 /* parse the argument list up to the ending ) */
129 while (*pPT
!= 'X') {
132 case 'D': /* type is double */
133 /* treat the same as long long */
134 case 'H': /* type is long long */
135 if (nw
& 1) nw
++; /* note even elements gpr[] will map to
138 gpr
[nw
++] = *pStackLongs
;
139 gpr
[nw
++] = *(pStackLongs
+1);
144 *p
++ = *(pStackLongs
+1);
151 gpr
[nw
++] = *((unsigned short*)pStackLongs
);
153 *p
++ = *((unsigned short *)pStackLongs
);
160 gpr
[nw
++] = *((char *)pStackLongs
);
162 *p
++ = *((char *)pStackLongs
);
169 gpr
[nw
++] = *pStackLongs
;
179 /* figure out the address of the function we need to invoke */
181 off
= off
* 4; // 4 bytes per slot
182 mfunc
= *((unsigned long **)pAdjustedThisPtr
); // get the address of the vtable
183 mfunc
= (unsigned long *)((char *)mfunc
+ off
); // get the address from the vtable entry at offset
184 mfunc
= *((unsigned long **)mfunc
); // the function is stored at the address
185 ptr
= (void (*)())mfunc
;
188 fprintf(stderr
,"calling function %p\n",mfunc
);
191 /* Set up the machine registers and invoke the function */
193 __asm__
__volatile__ (
199 : "$4", "$5", "$6", "$7"
202 __asm__
__volatile__ ("addiu $29,$29,-528\r\n":::"$29");
206 __asm__
__volatile__ ("addiu $29,$29,528\r\n":::"$29");
208 __asm__
__volatile__ (
211 : "=m" (iret
), "=m" (iret2
) : );
212 register float fret
asm("$f0");
213 register double dret
asm("$f0");
215 switch( eReturnType
)
217 case typelib_TypeClass_HYPER
:
218 case typelib_TypeClass_UNSIGNED_HYPER
:
219 ((long*)pRegisterReturn
)[1] = iret2
; // fall through
220 case typelib_TypeClass_LONG
:
221 case typelib_TypeClass_UNSIGNED_LONG
:
222 case typelib_TypeClass_ENUM
:
223 ((long*)pRegisterReturn
)[0] = iret
;
225 case typelib_TypeClass_CHAR
:
226 case typelib_TypeClass_SHORT
:
227 case typelib_TypeClass_UNSIGNED_SHORT
:
228 *(unsigned short*)pRegisterReturn
= (unsigned short)iret
;
230 case typelib_TypeClass_BOOLEAN
:
231 case typelib_TypeClass_BYTE
:
232 *(unsigned char*)pRegisterReturn
= (unsigned char)iret
;
234 case typelib_TypeClass_FLOAT
:
235 *(float*)pRegisterReturn
= fret
;
237 case typelib_TypeClass_DOUBLE
:
238 *(double*)pRegisterReturn
= dret
;
246 //==================================================================================================
247 static void cpp_call(
248 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
249 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
250 typelib_TypeDescriptionReference
* pReturnTypeRef
,
251 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
252 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
254 // max space for: [complex ret ptr], values|ptr ...
256 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
257 char * pCppStackStart
= pCppStack
;
259 // need to know parameter types for callVirtualMethod so generate a signature string
260 char * pParamType
= (char *) alloca(nParams
+2);
261 char * pPT
= pParamType
;
264 fprintf(stderr
,"in cpp_call\n");
268 typelib_TypeDescription
* pReturnTypeDescr
= 0;
269 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
270 // OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
272 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
274 if (pReturnTypeDescr
)
276 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
278 pCppReturn
= pUnoReturn
; // direct way for simple types
282 // complex return via ptr
283 pCppReturn
= *(void **)pCppStack
=
284 (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
285 ? alloca( pReturnTypeDescr
->nSize
): pUnoReturn
); // direct way
286 *pPT
++ = 'I'; //signify that a complex return type on stack
287 pCppStack
+= sizeof(void *);
291 void* pAdjustedThisPtr
= reinterpret_cast< void **>(pThis
->getCppI()) + aVtableSlot
.offset
;
292 *(void**)pCppStack
= pAdjustedThisPtr
;
293 pCppStack
+= sizeof( void* );
297 // OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
299 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
300 // indizes of values this have to be converted (interface conversion cpp<=>uno)
301 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
302 // type descriptions for reconversions
303 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
305 sal_Int32 nTempIndizes
= 0;
307 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
309 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
310 typelib_TypeDescription
* pParamTypeDescr
= 0;
311 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
313 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
315 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
316 pThis
->getBridge()->getUno2Cpp() );
318 switch (pParamTypeDescr
->eTypeClass
)
321 // we need to know type of each param so that we know whether to use
322 // gpr or fpr to pass in parameters:
323 // Key: I - int, long, pointer, etc means pass in gpr
324 // B - byte value passed in gpr
325 // S - short value passed in gpr
326 // F - float value pass in fpr
327 // D - double value pass in fpr
328 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
329 // X - indicates end of parameter description string
331 case typelib_TypeClass_LONG
:
332 case typelib_TypeClass_UNSIGNED_LONG
:
333 case typelib_TypeClass_ENUM
:
336 case typelib_TypeClass_SHORT
:
337 case typelib_TypeClass_CHAR
:
338 case typelib_TypeClass_UNSIGNED_SHORT
:
341 case typelib_TypeClass_BOOLEAN
:
342 case typelib_TypeClass_BYTE
:
345 case typelib_TypeClass_FLOAT
:
348 case typelib_TypeClass_DOUBLE
:
350 pCppStack
+= sizeof(sal_Int32
); // extra long
352 case typelib_TypeClass_HYPER
:
353 case typelib_TypeClass_UNSIGNED_HYPER
:
355 pCppStack
+= sizeof(sal_Int32
); // extra long
362 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
364 else // ptr to complex value | ref
366 if (! rParam
.bIn
) // is pure out
368 // cpp out is constructed mem, uno out is not!
370 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
372 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
373 // will be released at reconversion
374 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
377 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
379 uno_copyAndConvertData(
380 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
381 pUnoArgs
[nPos
], pParamTypeDescr
,
382 pThis
->getBridge()->getUno2Cpp() );
384 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
385 // will be released at reconversion
386 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
390 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
392 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
394 // KBH: FIXME: is this the right way to pass these
397 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
400 // terminate the signature string
406 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
408 pAdjustedThisPtr
, aVtableSlot
.index
,
409 pCppReturn
, pReturnTypeDescr
->eTypeClass
, pParamType
,
410 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
411 // NO exception occured...
414 // reconvert temporary params
415 for ( ; nTempIndizes
--; )
417 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
418 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
420 if (pParams
[nIndex
].bIn
)
422 if (pParams
[nIndex
].bOut
) // inout
424 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
425 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
426 pThis
->getBridge()->getCpp2Uno() );
431 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
432 pThis
->getBridge()->getCpp2Uno() );
434 // destroy temp cpp param => cpp: every param was constructed
435 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
437 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
440 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
442 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
443 pThis
->getBridge()->getCpp2Uno() );
444 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
449 // fill uno exception
450 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
451 *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
454 for ( ; nTempIndizes
--; )
456 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
457 // destroy temp cpp param => cpp: every param was constructed
458 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
459 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
462 if (pReturnTypeDescr
)
463 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
470 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
472 //==================================================================================================
473 void unoInterfaceProxyDispatch(
474 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
475 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
478 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
479 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
480 //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
483 fprintf(stderr
,"in dispatch\n");
486 switch (pMemberDescr
->eTypeClass
)
488 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
491 VtableSlot
aVtableSlot(
494 typelib_InterfaceAttributeTypeDescription
const * >(
499 // dependent dispatch
502 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
504 pReturn
, pArgs
, ppException
);
509 typelib_MethodParameter aParam
;
511 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
512 aParam
.bIn
= sal_True
;
513 aParam
.bOut
= sal_False
;
515 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
516 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
517 typelib_typedescriptionreference_new(
518 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
520 // dependent dispatch
521 aVtableSlot
.index
+= 1; //get then set method
526 pReturn
, pArgs
, ppException
);
528 typelib_typedescriptionreference_release( pReturnTypeRef
);
533 case typelib_TypeClass_INTERFACE_METHOD
:
536 VtableSlot
aVtableSlot(
539 typelib_InterfaceMethodTypeDescription
const * >(
541 switch (aVtableSlot
.index
)
544 case 1: // acquire uno interface
545 (*pUnoI
->acquire
)( pUnoI
);
548 case 2: // release uno interface
549 (*pUnoI
->release
)( pUnoI
);
552 case 0: // queryInterface() opt
554 typelib_TypeDescription
* pTD
= 0;
555 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
558 uno_Interface
* pInterface
= 0;
559 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
560 pThis
->pBridge
->getUnoEnv(),
561 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
566 reinterpret_cast< uno_Any
* >( pReturn
),
567 &pInterface
, pTD
, 0 );
568 (*pInterface
->release
)( pInterface
);
569 TYPELIB_DANGER_RELEASE( pTD
);
573 TYPELIB_DANGER_RELEASE( pTD
);
575 } // else perform queryInterface()
577 // dependent dispatch
580 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
581 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
582 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
583 pReturn
, pArgs
, ppException
);
589 ::com::sun::star::uno::RuntimeException
aExc(
590 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
591 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
593 Type
const & rExcType
= ::getCppuType( &aExc
);
594 // binary identical null reference
595 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );