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 ************************************************************************/
29 #include <com/sun/star/uno/genfunc.hxx>
30 #include <typelib/typedescription.hxx>
32 #include "bridges/cpp_uno/shared/bridge.hxx"
33 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
34 #include "bridges/cpp_uno/shared/types.hxx"
35 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
37 #include <sal/alloca.h>
39 using namespace com::sun::star::uno
;
43 //==================================================================================================
44 static typelib_TypeClass
cpp2uno_call(
45 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
46 const typelib_TypeDescription
* pMemberTypeDescr
,
47 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
48 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
50 sal_Int64
* pRegisterReturn
/* space for register return */ )
52 // pCallStack: [ret ptr], this, params
53 char * pCppStack
= (char *)pCallStack
;
56 typelib_TypeDescription
* pReturnTypeDescr
= 0;
58 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
60 void * pUnoReturn
= 0;
61 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
65 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
66 pUnoReturn
= pRegisterReturn
; // direct way for simple types
67 else // complex return via ptr (pCppReturn)
69 pCppReturn
= *(void**)pCppStack
;
70 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
72 ? alloca( pReturnTypeDescr
->nSize
)
73 : pCppReturn
); // direct way
74 pCppStack
+= sizeof( void* );
78 pCppStack
+= sizeof( void* );
81 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
83 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
84 void ** pCppArgs
= pUnoArgs
+ nParams
;
85 // indizes of values this have to be converted (interface conversion cpp<=>uno)
86 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
87 // type descriptions for reconversions
88 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
90 sal_Int32 nTempIndizes
= 0;
92 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
94 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
95 typelib_TypeDescription
* pParamTypeDescr
= 0;
96 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
98 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
100 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack
, pParamTypeDescr
);
101 switch (pParamTypeDescr
->eTypeClass
)
103 case typelib_TypeClass_HYPER
:
104 case typelib_TypeClass_UNSIGNED_HYPER
:
105 case typelib_TypeClass_DOUBLE
:
107 if ((reinterpret_cast< long >(pCppStack
) & 7) != 0)
109 OSL_ASSERT( sizeof (double) == sizeof (sal_Int64
) );
110 void * pDest
= alloca( sizeof (sal_Int64
) );
111 *reinterpret_cast< sal_Int32
* >(pDest
) =
112 *reinterpret_cast< sal_Int32
const * >(pCppStack
);
113 *(reinterpret_cast< sal_Int32
* >(pDest
) + 1) =
114 *(reinterpret_cast< sal_Int32
const * >(pCppStack
) + 1);
115 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = pDest
;
117 pCppStack
+= sizeof (sal_Int32
); // extra long
122 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
124 else // ptr to complex value | ref
126 pCppArgs
[nPos
] = *(void **)pCppStack
;
128 if (! rParam
.bIn
) // is pure out
130 // uno out is unconstructed mem!
131 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
132 pTempIndizes
[nTempIndizes
] = nPos
;
133 // will be released at reconversion
134 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
137 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
140 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
141 *(void **)pCppStack
, pParamTypeDescr
,
142 pThis
->getBridge()->getCpp2Uno() );
143 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
144 // will be released at reconversion
145 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
149 pUnoArgs
[nPos
] = *(void **)pCppStack
;
151 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
154 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
158 uno_Any aUnoExc
; // Any will be constructed by callee
159 uno_Any
* pUnoExc
= &aUnoExc
;
161 // invoke uno dispatch call
162 (*pThis
->getUnoI()->pDispatcher
)(pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
164 // in case an exception occurred...
167 // destruct temporary in/inout params
168 for ( ; nTempIndizes
--; )
170 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
172 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
173 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
174 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
176 if (pReturnTypeDescr
)
177 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
178 CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
179 // has to destruct the any
181 return typelib_TypeClass_VOID
;
183 else // else no exception occurred...
186 for ( ; nTempIndizes
--; )
188 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
189 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
191 if (pParams
[nIndex
].bOut
) // inout/out
193 // convert and assign
194 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
195 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
196 pThis
->getBridge()->getUno2Cpp() );
198 // destroy temp uno param
199 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
201 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
204 if (pCppReturn
) // has complex return
206 if (pUnoReturn
!= pCppReturn
) // needs reconversion
208 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
209 pThis
->getBridge()->getUno2Cpp() );
210 // destroy temp uno return
211 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
213 // complex return ptr is set to eax
214 *(void **)pRegisterReturn
= pCppReturn
;
216 if (pReturnTypeDescr
)
218 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
219 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
223 return typelib_TypeClass_VOID
;
228 //==================================================================================================
229 static typelib_TypeClass
cpp_mediate(
230 sal_Int32 nFunctionIndex
,
231 sal_Int32 nVtableOffset
,
233 sal_Int64
* pRegisterReturn
/* space for register return */ )
235 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
237 // pCallStack: this, params
238 // eventual [ret*] lies at pCallStack -1
239 // so count down pCallStack by one to keep it simple
240 // pCallStack: this, params
241 // eventual [ret*] lies at pCallStack -1
242 // so count down pCallStack by one to keep it simple
243 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
244 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
245 static_cast< char * >(*pCallStack
) - nVtableOffset
);
246 if ((nFunctionIndex
& 0x80000000) != 0) {
247 nFunctionIndex
&= 0x7FFFFFFF;
251 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
253 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
,
254 "### illegal vtable index!" );
255 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
257 throw RuntimeException( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal vtable index!" )), (XInterface
*)pCppI
);
260 // determine called method
261 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
262 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
264 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
266 #if defined BRIDGES_DEBUG
267 OString
cstr( OUStringToOString( aMemberDescr
.get()->pTypeName
, RTL_TEXTENCODING_ASCII_US
) );
268 fprintf( stderr
, "calling %s, nFunctionIndex=%d\n", cstr
.getStr(), nFunctionIndex
);
271 typelib_TypeClass eRet
;
272 switch (aMemberDescr
.get()->eTypeClass
)
274 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
276 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
280 pCppI
, aMemberDescr
.get(),
281 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
283 pCallStack
, pRegisterReturn
);
288 typelib_MethodParameter aParam
;
290 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
291 aParam
.bIn
= sal_True
;
292 aParam
.bOut
= sal_False
;
295 pCppI
, aMemberDescr
.get(),
296 0, // indicates void return
298 pCallStack
, pRegisterReturn
);
302 case typelib_TypeClass_INTERFACE_METHOD
:
305 switch (nFunctionIndex
)
308 pCppI
->acquireProxy(); // non virtual call!
309 eRet
= typelib_TypeClass_VOID
;
312 pCppI
->releaseProxy(); // non virtual call!
313 eRet
= typelib_TypeClass_VOID
;
315 case 0: // queryInterface() opt
317 typelib_TypeDescription
* pTD
= 0;
318 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[2] )->getTypeLibType() );
321 XInterface
* pInterface
= 0;
322 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
323 pCppI
->getBridge()->getCppEnv(),
324 (void **)&pInterface
, pCppI
->getOid().pData
, (typelib_InterfaceTypeDescription
*)pTD
);
329 reinterpret_cast< uno_Any
* >( pCallStack
[0] ),
330 &pInterface
, pTD
, cpp_acquire
);
331 pInterface
->release();
332 TYPELIB_DANGER_RELEASE( pTD
);
333 *(void **)pRegisterReturn
= pCallStack
[0];
334 eRet
= typelib_TypeClass_ANY
;
337 TYPELIB_DANGER_RELEASE( pTD
);
339 } // else perform queryInterface()
342 pCppI
, aMemberDescr
.get(),
343 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
344 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
345 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
346 pCallStack
, pRegisterReturn
);
352 throw RuntimeException(rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no member description found!" )), (XInterface
*)pCppI
);
360 //==================================================================================================
362 * is called on incoming vtable calls
363 * (called by asm snippets)
365 static void cpp_vtable_call()
367 volatile sal_Int64 nRegReturn
;
372 __asm__( "st %%i0, %0\n\t"
375 : : "m"(nFunctionIndex
), "m"(pCallStack
), "m"(vTableOffset
) );
377 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
380 sal_Bool bComplex
= nFunctionIndex
& 0x80000000 ? sal_True
: sal_False
;
381 typelib_TypeClass aType
=
382 cpp_mediate( nFunctionIndex
, vTableOffset
, pCallStack
+17, (sal_Int64
*)&nRegReturn
);
386 case typelib_TypeClass_BOOLEAN
:
387 case typelib_TypeClass_BYTE
:
388 __asm__( "ld %0, %%l0\n\t"
389 "ldsb [%%l0], %%i0\n"
390 : : "m"(&nRegReturn
) );
392 case typelib_TypeClass_CHAR
:
393 case typelib_TypeClass_SHORT
:
394 case typelib_TypeClass_UNSIGNED_SHORT
:
395 __asm__( "ld %0, %%l0\n\t"
396 "ldsh [%%l0], %%i0\n"
397 : : "m"(&nRegReturn
) );
399 case typelib_TypeClass_HYPER
:
400 case typelib_TypeClass_UNSIGNED_HYPER
:
402 __asm__( "ld %0, %%l0\n\t"
403 "ld [%%l0], %%i0\n\t"
405 "ld [%%l0], %%i1\n\t"
406 : : "m"(&nRegReturn
), "m"(((long*)&nRegReturn
) +1) );
409 case typelib_TypeClass_FLOAT
:
410 __asm__( "ld %0, %%l0\n\t"
412 : : "m"(&nRegReturn
) );
414 case typelib_TypeClass_DOUBLE
:
415 __asm__( "ld %0, %%l0\n\t"
417 : : "m"(&nRegReturn
) );
419 case typelib_TypeClass_VOID
:
422 __asm__( "ld %0, %%l0\n\t"
424 : : "m"(&nRegReturn
) );
430 __asm__( "add %i7, 4, %i7\n\t" );
431 // after call to complex return valued funcion there is an unimp instruction
435 //__________________________________________________________________________________________________
437 int const codeSnippetSize
= 56;
438 unsigned char * codeSnippet(
439 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
442 sal_uInt32 index
= functionIndex
;
443 if (!simpleRetType
) {
446 unsigned int * p
= reinterpret_cast< unsigned int * >(code
);
447 OSL_ASSERT(sizeof (unsigned int) == 4);
460 // sethi %hi(index), %o0:
461 *p
++ = 0x11000000 | (index
>> 10);
462 // or %o0, %lo(index), %o0:
463 *p
++ = 0x90122000 | (index
& 0x3FF);
464 // sethi %hi(vtableOffset), %o2:
465 *p
++ = 0x15000000 | (vtableOffset
>> 10);
466 // or %o2, %lo(vtableOffset), %o2:
467 *p
++ = 0x9412A000 | (vtableOffset
& 0x3FF);
468 // sethi %hi(cpp_vtable_call), %o3:
469 *p
++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) >> 10);
470 // or %o3, %lo(cpp_vtable_call), %o3:
471 *p
++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) & 0x3FF);
477 reinterpret_cast< unsigned char * >(p
) - code
<= codeSnippetSize
);
478 return code
+ codeSnippetSize
;
483 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
485 bridges::cpp_uno::shared::VtableFactory::Slot
*
486 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
488 return static_cast< Slot
* >(block
) + 2;
491 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
494 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
497 bridges::cpp_uno::shared::VtableFactory::Slot
*
498 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
499 void * block
, sal_Int32 slotCount
)
501 Slot
* slots
= mapBlockToVtable(block
);
502 slots
[-2].fn
= 0; //null
503 slots
[-1].fn
= 0; //destructor
504 return slots
+ slotCount
;
507 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
508 Slot
** slots
, unsigned char * code
,
509 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
510 sal_Int32 functionCount
, sal_Int32 vTableOffset
)
512 (*slots
) -= functionCount
;
514 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
515 typelib_TypeDescription
* member
= 0;
516 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
517 OSL_ASSERT(member
!= 0);
518 switch (member
->eTypeClass
) {
519 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
523 code
, functionOffset
++, vTableOffset
,
524 bridges::cpp_uno::shared::isSimpleType(
526 typelib_InterfaceAttributeTypeDescription
* >(
527 member
)->pAttributeTypeRef
));
529 if (!reinterpret_cast<
530 typelib_InterfaceAttributeTypeDescription
* >(
534 code
= codeSnippet(code
, functionOffset
++, vTableOffset
, true);
538 case typelib_TypeClass_INTERFACE_METHOD
:
541 code
, functionOffset
++, vTableOffset
,
542 bridges::cpp_uno::shared::isSimpleType(
544 typelib_InterfaceMethodTypeDescription
* >(
545 member
)->pReturnTypeRef
));
552 TYPELIB_DANGER_RELEASE(member
);
557 void bridges::cpp_uno::shared::VtableFactory::flushCode(
558 unsigned char const *, unsigned char const *)
560 //TODO: IZ 25819 flush the instruction cache (there probably is OS support for this)
563 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */