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 ************************************************************************/
32 #include <com/sun/star/uno/genfunc.hxx>
35 #include "bridges/cpp_uno/shared/bridge.hxx"
36 #include "bridges/cpp_uno/shared/types.hxx"
37 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
38 #include "bridges/cpp_uno/shared/vtables.hxx"
46 using namespace ::rtl
;
47 using namespace ::com::sun::star::uno
;
49 void MapReturn(long r2
, double f0
, typelib_TypeClass eTypeClass
, sal_uInt64
* pRegisterReturn
)
51 #if OSL_DEBUG_LEVEL > 2
52 fprintf(stderr
,"Mapping Return with %lx %ld %f\n", r2
, r2
, f0
);
56 case typelib_TypeClass_HYPER
:
57 case typelib_TypeClass_UNSIGNED_HYPER
:
58 *pRegisterReturn
= r2
;
60 case typelib_TypeClass_LONG
:
61 case typelib_TypeClass_UNSIGNED_LONG
:
62 case typelib_TypeClass_ENUM
:
63 *(unsigned int*)pRegisterReturn
= (unsigned int)r2
;
65 case typelib_TypeClass_CHAR
:
66 case typelib_TypeClass_SHORT
:
67 case typelib_TypeClass_UNSIGNED_SHORT
:
68 *(unsigned short*)pRegisterReturn
= (unsigned short)r2
;
70 case typelib_TypeClass_BOOLEAN
:
71 case typelib_TypeClass_BYTE
:
72 *(unsigned char*)pRegisterReturn
= (unsigned char)r2
;
74 case typelib_TypeClass_FLOAT
:
75 *reinterpret_cast<float *>( pRegisterReturn
) = f0
;
77 case typelib_TypeClass_DOUBLE
:
78 *reinterpret_cast<double *>( pRegisterReturn
) = f0
;
83 #if OSL_DEBUG_LEVEL > 2
84 fprintf(stderr
, "end of MapReturn with %x\n", pRegisterReturn
? *pRegisterReturn
: 0);
88 #define INSERT_FLOAT( pSV, nr, pFPR, pDS ) \
90 if ( nr < s390x::MAX_SSE_REGS ) \
92 pFPR[nr++] = *reinterpret_cast<float *>( pSV ); \
95 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); \
98 #define INSERT_DOUBLE( pSV, nr, pFPR, pDS ) \
99 if ( nr < s390x::MAX_SSE_REGS ) \
100 pFPR[nr++] = *reinterpret_cast<double *>( pSV ); \
102 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV ); // verbatim!
104 #define INSERT_INT64( pSV, nr, pGPR, pDS ) \
105 if ( nr < s390x::MAX_GPR_REGS ) \
106 pGPR[nr++] = *reinterpret_cast<sal_uInt64 *>( pSV ); \
108 *pDS++ = *reinterpret_cast<sal_uInt64 *>( pSV );
110 #define INSERT_INT32( pSV, nr, pGPR, pDS ) \
111 if ( nr < s390x::MAX_GPR_REGS ) \
112 pGPR[nr++] = *reinterpret_cast<sal_uInt32 *>( pSV ); \
114 *pDS++ = *reinterpret_cast<sal_uInt32 *>( pSV );
116 #define INSERT_INT16( pSV, nr, pGPR, pDS ) \
117 if ( nr < s390x::MAX_GPR_REGS ) \
118 pGPR[nr++] = *reinterpret_cast<sal_uInt16 *>( pSV ); \
120 *pDS++ = *reinterpret_cast<sal_uInt16 *>( pSV );
122 #define INSERT_INT8( pSV, nr, pGPR, pDS ) \
123 if ( nr < s390x::MAX_GPR_REGS ) \
124 pGPR[nr++] = *reinterpret_cast<sal_uInt8 *>( pSV ); \
126 *pDS++ = *reinterpret_cast<sal_uInt8 *>( pSV );
130 //==================================================================================================
131 void callVirtualMethod(
132 void * pThis
, sal_Int32 nVtableIndex
,
133 void * pRegisterReturn
, typelib_TypeDescription
* pReturnTypeDescr
,
134 sal_uInt64
*pStack
, sal_uInt32 nStack
,
135 sal_uInt64
*pGPR
, sal_uInt32 nGPR
,
136 double *pFPR
, sal_uInt32 nFPR
)
138 // Should not happen, but...
139 if ( nFPR
> s390x::MAX_SSE_REGS
)
140 nFPR
= s390x::MAX_SSE_REGS
;
141 if ( nGPR
> s390x::MAX_GPR_REGS
)
142 nGPR
= s390x::MAX_GPR_REGS
;
144 #if OSL_DEBUG_LEVEL > 2
145 // Let's figure out what is really going on here
147 fprintf( stderr
, "= nStack is %d\n", nStack
);
148 fprintf( stderr
, "= callVirtualMethod() =\nGPR's (%d): ", nGPR
);
149 for ( unsigned int i
= 0; i
< nGPR
; ++i
)
150 fprintf( stderr
, "0x%lx, ", pGPR
[i
] );
151 fprintf( stderr
, "\nFPR's (%d): ", nFPR
);
152 for ( unsigned int i
= 0; i
< nFPR
; ++i
)
153 fprintf( stderr
, "0x%lx (%f), ", pFPR
[i
], pFPR
[i
] );
154 fprintf( stderr
, "\nStack (%d): ", nStack
);
155 for ( unsigned int i
= 0; i
< nStack
; ++i
)
156 fprintf( stderr
, "0x%lx, ", pStack
[i
] );
157 fprintf( stderr
, "\n" );
158 fprintf( stderr
, "pRegisterReturn is %p\n", pRegisterReturn
);
162 // Load parameters to stack, if necessary
163 // Stack, if used, must be 8-bytes aligned
164 sal_uInt64
*stack
= (sal_uInt64
*) __builtin_alloca( nStack
* 8 );
165 memcpy( stack
, pStack
, nStack
* 8 );
167 // To get pointer to method
168 // a) get the address of the vtable
169 sal_uInt64 pMethod
= *((sal_uInt64
*)pThis
);
170 // b) get the address from the vtable entry at offset
171 pMethod
+= 8 * nVtableIndex
;
172 pMethod
= *((sal_uInt64
*)pMethod
);
174 typedef void (* FunctionCall
)( sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
, sal_uInt64
);
175 FunctionCall pFunc
= (FunctionCall
)pMethod
;
177 switch (nFPR
) //deliberate fall through
180 asm volatile("ld 6,%0" :: "m"(pFPR
[3]) : "16");
182 asm volatile("ld 4,%0" :: "m"(pFPR
[2]) : "16");
184 asm volatile("ld 2,%0" :: "m"(pFPR
[1]) : "16");
186 asm volatile("ld 0,%0" :: "m"(pFPR
[0]) : "16");
194 (*pFunc
)(pGPR
[0], pGPR
[1], pGPR
[2], pGPR
[3], pGPR
[4]);
196 __asm__
__volatile__ (
199 : "=r" (r2
), "=f" (f0
)
203 MapReturn(r2
, f0
, pReturnTypeDescr
->eTypeClass
, (sal_uInt64
*)pRegisterReturn
);
207 //============================================================================
208 static void cpp_call(
209 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
,
210 bridges::cpp_uno::shared::VtableSlot aVtableSlot
,
211 typelib_TypeDescriptionReference
* pReturnTypeRef
,
212 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
213 void * pUnoReturn
, void * pUnoArgs
[], uno_Any
** ppUnoExc
)
215 // max space for: [complex ret ptr], values|ptr ...
216 sal_uInt64
* pStack
= (sal_uInt64
*)alloca( (nParams
+3) * sizeof(sal_Int64
) );
217 sal_uInt64
* pStackStart
= pStack
;
219 sal_uInt64 pGPR
[s390x::MAX_GPR_REGS
];
222 double pFPR
[s390x::MAX_SSE_REGS
];
226 typelib_TypeDescription
* pReturnTypeDescr
= 0;
227 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
228 OSL_ENSURE( pReturnTypeDescr
, "### expected return type description!" );
230 void * pCppReturn
= 0; // if != 0 && != pUnoReturn, needs reconversion
232 if (pReturnTypeDescr
)
234 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
236 pCppReturn
= pUnoReturn
; // direct way for simple types
240 // complex return via ptr
241 pCppReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
242 ? alloca( pReturnTypeDescr
->nSize
)
243 : pUnoReturn
); // direct way
244 INSERT_INT64( &pCppReturn
, nGPR
, pGPR
, pStack
);
247 // push "this" pointer
248 void * pAdjustedThisPtr
= reinterpret_cast< void ** >( pThis
->getCppI() ) + aVtableSlot
.offset
;
250 INSERT_INT64( &pAdjustedThisPtr
, nGPR
, pGPR
, pStack
);
253 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64
), "### unexpected size!" );
255 void ** pCppArgs
= (void **)alloca( 3 * sizeof(void *) * nParams
);
256 // indizes of values this have to be converted (interface conversion cpp<=>uno)
257 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pCppArgs
+ nParams
);
258 // type descriptions for reconversions
259 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pCppArgs
+ (2 * nParams
));
261 sal_Int32 nTempIndizes
= 0;
263 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
265 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
266 typelib_TypeDescription
* pParamTypeDescr
= 0;
267 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
269 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
271 uno_copyAndConvertData( pCppArgs
[nPos
] = alloca( 8 ), pUnoArgs
[nPos
], pParamTypeDescr
,
272 pThis
->getBridge()->getUno2Cpp() );
274 switch (pParamTypeDescr
->eTypeClass
)
276 case typelib_TypeClass_HYPER
:
277 case typelib_TypeClass_UNSIGNED_HYPER
:
278 INSERT_INT64( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
280 case typelib_TypeClass_LONG
:
281 case typelib_TypeClass_UNSIGNED_LONG
:
282 case typelib_TypeClass_ENUM
:
283 INSERT_INT32( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
285 case typelib_TypeClass_SHORT
:
286 case typelib_TypeClass_CHAR
:
287 case typelib_TypeClass_UNSIGNED_SHORT
:
288 INSERT_INT16( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
290 case typelib_TypeClass_BOOLEAN
:
291 case typelib_TypeClass_BYTE
:
292 INSERT_INT8( pCppArgs
[nPos
], nGPR
, pGPR
, pStack
);
294 case typelib_TypeClass_FLOAT
:
295 INSERT_FLOAT( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
297 case typelib_TypeClass_DOUBLE
:
298 INSERT_DOUBLE( pCppArgs
[nPos
], nFPR
, pFPR
, pStack
);
305 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
307 else // ptr to complex value | ref
309 if (! rParam
.bIn
) // is pure out
311 // cpp out is constructed mem, uno out is not!
313 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
315 pTempIndizes
[nTempIndizes
] = nPos
; // default constructed for cpp call
316 // will be released at reconversion
317 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
320 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
322 uno_copyAndConvertData(
323 pCppArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
324 pUnoArgs
[nPos
], pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
326 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
327 // will be released at reconversion
328 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
332 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
334 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
336 INSERT_INT64( &(pCppArgs
[nPos
]), nGPR
, pGPR
, pStack
);
343 pAdjustedThisPtr
, aVtableSlot
.index
,
344 pCppReturn
, pReturnTypeDescr
,
345 pStackStart
, (pStack
- pStackStart
),
348 // NO exception occurred...
351 // reconvert temporary params
352 for ( ; nTempIndizes
--; )
354 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
355 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
357 if (pParams
[nIndex
].bIn
)
359 if (pParams
[nIndex
].bOut
) // inout
361 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 ); // destroy uno value
362 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
363 pThis
->getBridge()->getCpp2Uno() );
368 uno_copyAndConvertData( pUnoArgs
[nIndex
], pCppArgs
[nIndex
], pParamTypeDescr
,
369 pThis
->getBridge()->getCpp2Uno() );
371 // destroy temp cpp param => cpp: every param was constructed
372 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
374 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
377 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
379 uno_copyAndConvertData( pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
380 pThis
->getBridge()->getCpp2Uno() );
381 uno_destructData( pCppReturn
, pReturnTypeDescr
, cpp_release
);
386 // fill uno exception
387 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions
,
388 *ppUnoExc
, pThis
->getBridge()->getCpp2Uno() );
392 for ( ; nTempIndizes
--; )
394 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
395 // destroy temp cpp param => cpp: every param was constructed
396 uno_destructData( pCppArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], cpp_release
);
397 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
400 if (pReturnTypeDescr
)
401 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
406 namespace bridges
{ namespace cpp_uno
{ namespace shared
{
408 void unoInterfaceProxyDispatch(
409 uno_Interface
* pUnoI
, const typelib_TypeDescription
* pMemberDescr
,
410 void * pReturn
, void * pArgs
[], uno_Any
** ppException
)
412 #if OSL_DEBUG_LEVEL > 2
413 fprintf(stderr
, "unoInterfaceProxyDispatch\n");
418 bridges::cpp_uno::shared::UnoInterfaceProxy
* pThis
419 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy
*> (pUnoI
);
421 switch (pMemberDescr
->eTypeClass
)
423 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
426 VtableSlot
aVtableSlot(
429 typelib_InterfaceAttributeTypeDescription
const * >(
434 // dependent dispatch
437 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
439 pReturn
, pArgs
, ppException
);
444 typelib_MethodParameter aParam
;
446 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
;
447 aParam
.bIn
= sal_True
;
448 aParam
.bOut
= sal_False
;
450 typelib_TypeDescriptionReference
* pReturnTypeRef
= 0;
451 OUString
aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
452 typelib_typedescriptionreference_new(
453 &pReturnTypeRef
, typelib_TypeClass_VOID
, aVoidName
.pData
);
455 // dependent dispatch
456 aVtableSlot
.index
+= 1; //get then set method
461 pReturn
, pArgs
, ppException
);
463 typelib_typedescriptionreference_release( pReturnTypeRef
);
468 case typelib_TypeClass_INTERFACE_METHOD
:
471 VtableSlot
aVtableSlot(
474 typelib_InterfaceMethodTypeDescription
const * >(
476 switch (aVtableSlot
.index
)
479 case 1: // acquire uno interface
480 (*pUnoI
->acquire
)( pUnoI
);
483 case 2: // release uno interface
484 (*pUnoI
->release
)( pUnoI
);
487 case 0: // queryInterface() opt
489 typelib_TypeDescription
* pTD
= 0;
490 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pArgs
[0] )->getTypeLibType() );
493 uno_Interface
* pInterface
= 0;
494 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
495 pThis
->pBridge
->getUnoEnv(),
496 (void **)&pInterface
, pThis
->oid
.pData
, (typelib_InterfaceTypeDescription
*)pTD
);
501 reinterpret_cast< uno_Any
* >( pReturn
),
502 &pInterface
, pTD
, 0 );
503 (*pInterface
->release
)( pInterface
);
504 TYPELIB_DANGER_RELEASE( pTD
);
508 TYPELIB_DANGER_RELEASE( pTD
);
510 } // else perform queryInterface()
512 // dependent dispatch
515 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
516 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
517 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
,
518 pReturn
, pArgs
, ppException
);
524 ::com::sun::star::uno::RuntimeException
aExc(
525 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
526 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>() );
528 Type
const & rExcType
= ::getCppuType( &aExc
);
529 // binary identical null reference
530 ::uno_type_any_construct( *ppException
, &aExc
, rExcType
.getTypeLibType(), 0 );
537 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */