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 ************************************************************************/
31 #include <com/sun/star/uno/genfunc.hxx>
34 #include "bridges/cpp_uno/shared/bridge.hxx"
35 #include "bridges/cpp_uno/shared/types.hxx"
36 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
37 #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 unsigned long * mfunc
; // actual function to be invoked
70 int gpr
[4]; // storage for gpregisters, map to a0-a3
71 int off
; // offset used to find function
72 int nw
; // number of words mapped
73 long *p
; // pointer to parameter overflow area
74 int c
; // character of parameter type being decoded
75 int iret
, iret2
; // temporary function return values
78 if (! pAdjustedThisPtr
) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
81 fprintf(stderr
,"in CallVirtualMethod\n");
84 // Because of the MIPS O32 calling conventions we could be passing
85 // parameters in both register types and on the stack. To create the
86 // stack parameter area we need we now simply allocate local
87 // variable storage param[] that is at least the size of the parameter stack
88 // (more than enough space) which we can overwrite the parameters into.
90 /* p = sp - 512; new sp will be p - 16, but we don't change sp
91 * at this time to avoid breaking ABI--not sure whether changing sp will break
92 * references to local variables. For the same reason, we use abosulte value.
94 __asm__
__volatile__ (
95 "addiu $2,$29,-512\n\t"
97 :"=r"(p
): : "$2","$29" );
100 if (nStackLongs
* 4 > 512 )
101 fprintf(stderr
,"too many arguments");
104 // now begin to load the C++ function arguments into storage
107 // now we need to parse the entire signature string */
108 // until we get the END indicator */
110 // treat complex return pointer like any other parameter //
113 fprintf(stderr
,"overflow area pointer p=%p\n",p
);
115 /* Let's figure out what is really going on here*/
116 fprintf(stderr
,"callVirtualMethod parameters string is %s\n",pPT
);
118 long * q
= (long *)pStackLongs
;
120 fprintf(stderr
,"uno stack is: %x\n",(unsigned int)*q
);
126 /* parse the argument list up to the ending ) */
127 while (*pPT
!= 'X') {
130 case 'D': /* type is double */
131 /* treat the same as long long */
132 case 'H': /* type is long long */
133 if (nw
& 1) nw
++; /* note even elements gpr[] will map to
136 gpr
[nw
++] = *pStackLongs
;
137 gpr
[nw
++] = *(pStackLongs
+1);
142 *p
++ = *(pStackLongs
+1);
149 gpr
[nw
++] = *((unsigned short*)pStackLongs
);
151 *p
++ = *((unsigned short *)pStackLongs
);
158 gpr
[nw
++] = *((char *)pStackLongs
);
160 *p
++ = *((char *)pStackLongs
);
167 gpr
[nw
++] = *pStackLongs
;
177 /* figure out the address of the function we need to invoke */
179 off
= off
* 4; // 4 bytes per slot
180 mfunc
= *((unsigned long **)pAdjustedThisPtr
); // get the address of the vtable
181 mfunc
= (unsigned long *)((char *)mfunc
+ off
); // get the address from the vtable entry at offset
182 mfunc
= *((unsigned long **)mfunc
); // the function is stored at the address
183 ptr
= (void (*)())mfunc
;
186 fprintf(stderr
,"calling function %p\n",mfunc
);
189 /* Set up the machine registers and invoke the function */
191 __asm__
__volatile__ (
197 : "$4", "$5", "$6", "$7"
200 __asm__
__volatile__ ("addiu $29,$29,-528\r\n":::"$29");
204 __asm__
__volatile__ ("addiu $29,$29,528\r\n":::"$29");
206 __asm__
__volatile__ (
209 : "=m" (iret
), "=m" (iret2
) : );
210 register float fret
asm("$f0");
211 register double dret
asm("$f0");
213 switch( eReturnType
)
215 case typelib_TypeClass_HYPER
:
216 case typelib_TypeClass_UNSIGNED_HYPER
:
217 ((long*)pRegisterReturn
)[1] = iret2
; // fall through
218 case typelib_TypeClass_LONG
:
219 case typelib_TypeClass_UNSIGNED_LONG
:
220 case typelib_TypeClass_ENUM
:
221 ((long*)pRegisterReturn
)[0] = iret
;
223 case typelib_TypeClass_CHAR
:
224 case typelib_TypeClass_SHORT
:
225 case typelib_TypeClass_UNSIGNED_SHORT
:
226 *(unsigned short*)pRegisterReturn
= (unsigned short)iret
;
228 case typelib_TypeClass_BOOLEAN
:
229 case typelib_TypeClass_BYTE
:
230 *(unsigned char*)pRegisterReturn
= (unsigned char)iret
;
232 case typelib_TypeClass_FLOAT
:
233 *(float*)pRegisterReturn
= fret
;
235 case typelib_TypeClass_DOUBLE
:
236 *(double*)pRegisterReturn
= dret
;
244 //==================================================================================================
245 static void cpp_call(
246 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
247 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
248 typelib_TypeDescriptionReference
* pReturnTypeRef
,
249 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
250 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
252 // max space for: [complex ret ptr], values|ptr ...
254 (char *)alloca( sizeof(sal_Int32
) + ((nParams
+2) * sizeof(sal_Int64
)) );
255 char * pCppStackStart
= pCppStack
;
257 // need to know parameter types for callVirtualMethod so generate a signature string
258 char * pParamType
= (char *) alloca(nParams
+2);
259 char * pPT
= pParamType
;
262 fprintf(stderr
,"in cpp_call\n");
266 typelib_TypeDescription
* pReturnTypeDescr
= 0;
267 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
268 // OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
270 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
272 if (pReturnTypeDescr
)
274 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
276 pCppReturn
= pUnoReturn
; // direct way for simple types
280 // complex return via ptr
281 pCppReturn
= *(void **)pCppStack
=
282 (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
283 ? alloca( pReturnTypeDescr
->nSize
): pUnoReturn
); // direct way
284 *pPT
++ = 'I'; //signify that a complex return type on stack
285 pCppStack
+= sizeof(void *);
289 void* pAdjustedThisPtr
= reinterpret_cast< void **>(pThis
->getCppI()) + aVtableSlot
.offset
;
290 *(void**)pCppStack
= pAdjustedThisPtr
;
291 pCppStack
+= sizeof( void* );
295 // OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
297 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
298 // indizes of values this have to be converted (interface conversion cpp<=>uno)
299 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
300 // type descriptions for reconversions
301 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
303 sal_Int32 nTempIndizes
= 0;
305 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
307 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
308 typelib_TypeDescription
* pParamTypeDescr
= 0;
309 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
311 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
313 uno_copyAndConvertData( pCppArgs
[nPos
] = pCppStack
, pUnoArgs
[nPos
], pParamTypeDescr
,
314 pThis
->getBridge()->getUno2Cpp() );
316 switch (pParamTypeDescr
->eTypeClass
)
319 // we need to know type of each param so that we know whether to use
320 // gpr or fpr to pass in parameters:
321 // Key: I - int, long, pointer, etc means pass in gpr
322 // B - byte value passed in gpr
323 // S - short value passed in gpr
324 // F - float value pass in fpr
325 // D - double value pass in fpr
326 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
327 // X - indicates end of parameter description string
329 case typelib_TypeClass_LONG
:
330 case typelib_TypeClass_UNSIGNED_LONG
:
331 case typelib_TypeClass_ENUM
:
334 case typelib_TypeClass_SHORT
:
335 case typelib_TypeClass_CHAR
:
336 case typelib_TypeClass_UNSIGNED_SHORT
:
339 case typelib_TypeClass_BOOLEAN
:
340 case typelib_TypeClass_BYTE
:
343 case typelib_TypeClass_FLOAT
:
346 case typelib_TypeClass_DOUBLE
:
348 pCppStack
+= sizeof(sal_Int32
); // extra long
350 case typelib_TypeClass_HYPER
:
351 case typelib_TypeClass_UNSIGNED_HYPER
:
353 pCppStack
+= sizeof(sal_Int32
); // extra long
360 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
362 else // ptr to complex value | ref
364 if (! rParam
.bIn
) // is pure out
366 // cpp out is constructed mem, uno out is not!
368 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
370 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
371 // will be released at reconversion
372 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
375 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
377 uno_copyAndConvertData(
378 *(void **)pCppStack
= pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
379 pUnoArgs
[nPos
], pParamTypeDescr
,
380 pThis
->getBridge()->getUno2Cpp() );
382 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
383 // will be released at reconversion
384 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
388 *(void **)pCppStack
= pCppArgs
[nPos
] = pUnoArgs
[nPos
];
390 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
392 // KBH: FIXME: is this the right way to pass these
395 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
398 // terminate the signature string
404 OSL_ENSURE( !( (pCppStack
- pCppStackStart
) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
406 pAdjustedThisPtr
, aVtableSlot
.index
,
407 pCppReturn
, pReturnTypeDescr
->eTypeClass
, pParamType
,
408 (sal_Int32
*)pCppStackStart
, (pCppStack
- pCppStackStart
) / sizeof(sal_Int32
) );
409 // NO exception occurred...
412 // reconvert temporary params
413 for ( ; nTempIndizes
--; )
415 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
416 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
418 if (pParams
[nIndex
].bIn
)
420 if (pParams
[nIndex
].bOut
) // inout
422 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
423 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
424 pThis
->getBridge()->getCpp2Uno() );
429 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
430 pThis
->getBridge()->getCpp2Uno() );
432 // destroy temp cpp param => cpp: every param was constructed
433 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
435 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
438 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
440 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
441 pThis
->getBridge()->getCpp2Uno() );
442 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
447 // fill uno exception
448 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
449 *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
452 for ( ; nTempIndizes
--; )
454 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
455 // destroy temp cpp param => cpp: every param was constructed
456 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
457 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
460 if (pReturnTypeDescr
)
461 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
468 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
470 //==================================================================================================
471 void unoInterfaceProxyDispatch(
472 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
473 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
476 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
477 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
478 //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
481 fprintf(stderr
,"in dispatch\n");
484 switch (pMemberDescr
->eTypeClass
)
486 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
489 VtableSlot
aVtableSlot(
492 typelib_InterfaceAttributeTypeDescription
const * >(
497 // dependent dispatch
500 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
502 pReturn
, pArgs
, ppException
);
507 typelib_MethodParameter aParam
;
509 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
510 aParam
.bIn
= sal_True
;
511 aParam
.bOut
= sal_False
;
513 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
514 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
515 typelib_typedescriptionreference_new(
516 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
518 // dependent dispatch
519 aVtableSlot
.index
+= 1; //get then set method
524 pReturn
, pArgs
, ppException
);
526 typelib_typedescriptionreference_release( pReturnTypeRef
);
531 case typelib_TypeClass_INTERFACE_METHOD
:
534 VtableSlot
aVtableSlot(
537 typelib_InterfaceMethodTypeDescription
const * >(
539 switch (aVtableSlot
.index
)
542 case 1: // acquire uno interface
543 (*pUnoI
->acquire
)( pUnoI
);
546 case 2: // release uno interface
547 (*pUnoI
->release
)( pUnoI
);
550 case 0: // queryInterface() opt
552 typelib_TypeDescription
* pTD
= 0;
553 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
556 uno_Interface
* pInterface
= 0;
557 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
558 pThis
->pBridge
->getUnoEnv(),
559 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
564 reinterpret_cast< uno_Any
* >( pReturn
),
565 &pInterface
, pTD
, 0 );
566 (*pInterface
->release
)( pInterface
);
567 TYPELIB_DANGER_RELEASE( pTD
);
571 TYPELIB_DANGER_RELEASE( pTD
);
573 } // else perform queryInterface()
575 // dependent dispatch
578 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
579 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
580 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
581 pReturn
, pArgs
, ppException
);
587 ::com::sun::star::uno::RuntimeException
aExc(
588 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
589 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
591 Type
const & rExcType
= ::getCppuType( &aExc
);
592 // binary identical null reference
593 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
599 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */