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: cpp2uno.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"
33 #include <com/sun/star/uno/genfunc.hxx>
34 #include <typelib/typedescription.hxx>
36 #include "bridges/cpp_uno/shared/bridge.hxx"
37 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
38 #include "bridges/cpp_uno/shared/types.hxx"
39 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
41 #include <sal/alloca.h>
43 using namespace com::sun::star::uno
;
47 //==================================================================================================
48 static typelib_TypeClass
cpp2uno_call(
49 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
50 const typelib_TypeDescription
* pMemberTypeDescr
,
51 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
52 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
54 sal_Int64
* pRegisterReturn
/* space for register return */ )
56 // pCallStack: [ret ptr], this, params
57 char * pCppStack
= (char *)pCallStack
;
60 typelib_TypeDescription
* pReturnTypeDescr
= 0;
62 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
64 void * pUnoReturn
= 0;
65 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
69 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
70 pUnoReturn
= pRegisterReturn
; // direct way for simple types
71 else // complex return via ptr (pCppReturn)
73 pCppReturn
= *(void**)pCppStack
;
74 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
76 ? alloca( pReturnTypeDescr
->nSize
)
77 : pCppReturn
); // direct way
78 pCppStack
+= sizeof( void* );
82 pCppStack
+= sizeof( void* );
85 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
87 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
88 void ** pCppArgs
= pUnoArgs
+ nParams
;
89 // indizes of values this have to be converted (interface conversion cpp<=>uno)
90 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
91 // type descriptions for reconversions
92 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
94 sal_Int32 nTempIndizes
= 0;
96 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
98 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
99 typelib_TypeDescription
* pParamTypeDescr
= 0;
100 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
102 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
104 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack
, pParamTypeDescr
);
105 switch (pParamTypeDescr
->eTypeClass
)
107 case typelib_TypeClass_HYPER
:
108 case typelib_TypeClass_UNSIGNED_HYPER
:
109 case typelib_TypeClass_DOUBLE
:
111 if ((reinterpret_cast< long >(pCppStack
) & 7) != 0)
113 OSL_ASSERT( sizeof (double) == sizeof (sal_Int64
) );
114 void * pDest
= alloca( sizeof (sal_Int64
) );
115 *reinterpret_cast< sal_Int32
* >(pDest
) =
116 *reinterpret_cast< sal_Int32
const * >(pCppStack
);
117 *(reinterpret_cast< sal_Int32
* >(pDest
) + 1) =
118 *(reinterpret_cast< sal_Int32
const * >(pCppStack
) + 1);
119 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = pDest
;
121 pCppStack
+= sizeof (sal_Int32
); // extra long
126 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
128 else // ptr to complex value | ref
130 pCppArgs
[nPos
] = *(void **)pCppStack
;
132 if (! rParam
.bIn
) // is pure out
134 // uno out is unconstructed mem!
135 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
136 pTempIndizes
[nTempIndizes
] = nPos
;
137 // will be released at reconversion
138 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
141 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
144 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
145 *(void **)pCppStack
, pParamTypeDescr
,
146 pThis
->getBridge()->getCpp2Uno() );
147 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
148 // will be released at reconversion
149 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
153 pUnoArgs
[nPos
] = *(void **)pCppStack
;
155 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
158 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
162 uno_Any aUnoExc
; // Any will be constructed by callee
163 uno_Any
* pUnoExc
= &aUnoExc
;
165 // invoke uno dispatch call
166 (*pThis
->getUnoI()->pDispatcher
)(pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
168 // in case an exception occured...
171 // destruct temporary in/inout params
172 for ( ; nTempIndizes
--; )
174 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
176 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
177 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
178 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
180 if (pReturnTypeDescr
)
181 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
182 CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
183 // has to destruct the any
185 return typelib_TypeClass_VOID
;
187 else // else no exception occured...
190 for ( ; nTempIndizes
--; )
192 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
193 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
195 if (pParams
[nIndex
].bOut
) // inout/out
197 // convert and assign
198 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
199 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
200 pThis
->getBridge()->getUno2Cpp() );
202 // destroy temp uno param
203 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
205 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
208 if (pCppReturn
) // has complex return
210 if (pUnoReturn
!= pCppReturn
) // needs reconversion
212 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
213 pThis
->getBridge()->getUno2Cpp() );
214 // destroy temp uno return
215 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
217 // complex return ptr is set to eax
218 *(void **)pRegisterReturn
= pCppReturn
;
220 if (pReturnTypeDescr
)
222 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
223 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
227 return typelib_TypeClass_VOID
;
232 //==================================================================================================
233 static typelib_TypeClass
cpp_mediate(
234 sal_Int32 nFunctionIndex
,
235 sal_Int32 nVtableOffset
,
237 sal_Int64
* pRegisterReturn
/* space for register return */ )
239 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
241 // pCallStack: this, params
242 // eventual [ret*] lies at pCallStack -1
243 // so count down pCallStack by one to keep it simple
244 // pCallStack: this, params
245 // eventual [ret*] lies at pCallStack -1
246 // so count down pCallStack by one to keep it simple
247 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
248 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
249 static_cast< char * >(*pCallStack
) - nVtableOffset
);
250 if ((nFunctionIndex
& 0x80000000) != 0) {
251 nFunctionIndex
&= 0x7FFFFFFF;
255 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
257 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
,
258 "### illegal vtable index!" );
259 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
261 throw RuntimeException( rtl::OUString::createFromAscii("illegal vtable index!"), (XInterface
*)pCppI
);
264 // determine called method
265 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
266 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
268 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
270 #if defined BRIDGES_DEBUG
271 OString
cstr( OUStringToOString( aMemberDescr
.get()->pTypeName
, RTL_TEXTENCODING_ASCII_US
) );
272 fprintf( stderr
, "calling %s, nFunctionIndex=%d\n", cstr
.getStr(), nFunctionIndex
);
275 typelib_TypeClass eRet
;
276 switch (aMemberDescr
.get()->eTypeClass
)
278 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
280 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
284 pCppI
, aMemberDescr
.get(),
285 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
287 pCallStack
, pRegisterReturn
);
292 typelib_MethodParameter aParam
;
294 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
295 aParam
.bIn
= sal_True
;
296 aParam
.bOut
= sal_False
;
299 pCppI
, aMemberDescr
.get(),
300 0, // indicates void return
302 pCallStack
, pRegisterReturn
);
306 case typelib_TypeClass_INTERFACE_METHOD
:
309 switch (nFunctionIndex
)
312 pCppI
->acquireProxy(); // non virtual call!
313 eRet
= typelib_TypeClass_VOID
;
316 pCppI
->releaseProxy(); // non virtual call!
317 eRet
= typelib_TypeClass_VOID
;
319 case 0: // queryInterface() opt
321 typelib_TypeDescription
* pTD
= 0;
322 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[2] )->getTypeLibType() );
325 XInterface
* pInterface
= 0;
326 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
327 pCppI
->getBridge()->getCppEnv(),
328 (void **)&pInterface
, pCppI
->getOid().pData
, (typelib_InterfaceTypeDescription
*)pTD
);
333 reinterpret_cast< uno_Any
* >( pCallStack
[0] ),
334 &pInterface
, pTD
, cpp_acquire
);
335 pInterface
->release();
336 TYPELIB_DANGER_RELEASE( pTD
);
337 *(void **)pRegisterReturn
= pCallStack
[0];
338 eRet
= typelib_TypeClass_ANY
;
341 TYPELIB_DANGER_RELEASE( pTD
);
343 } // else perform queryInterface()
346 pCppI
, aMemberDescr
.get(),
347 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
348 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
349 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
350 pCallStack
, pRegisterReturn
);
356 throw RuntimeException(rtl::OUString::createFromAscii("no member description found!"), (XInterface
*)pCppI
);
358 eRet
= typelib_TypeClass_VOID
;
366 //==================================================================================================
368 * is called on incoming vtable calls
369 * (called by asm snippets)
371 static void cpp_vtable_call()
373 volatile sal_Int64 nRegReturn
;
378 __asm__( "st %%i0, %0\n\t"
381 : : "m"(nFunctionIndex
), "m"(pCallStack
), "m"(vTableOffset
) );
383 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
386 sal_Bool bComplex
= nFunctionIndex
& 0x80000000 ? sal_True
: sal_False
;
387 typelib_TypeClass aType
=
388 cpp_mediate( nFunctionIndex
, vTableOffset
, pCallStack
+17, (sal_Int64
*)&nRegReturn
);
392 case typelib_TypeClass_BOOLEAN
:
393 case typelib_TypeClass_BYTE
:
394 __asm__( "ld %0, %%l0\n\t"
395 "ldsb [%%l0], %%i0\n"
396 : : "m"(&nRegReturn
) );
398 case typelib_TypeClass_CHAR
:
399 case typelib_TypeClass_SHORT
:
400 case typelib_TypeClass_UNSIGNED_SHORT
:
401 __asm__( "ld %0, %%l0\n\t"
402 "ldsh [%%l0], %%i0\n"
403 : : "m"(&nRegReturn
) );
405 case typelib_TypeClass_HYPER
:
406 case typelib_TypeClass_UNSIGNED_HYPER
:
408 __asm__( "ld %0, %%l0\n\t"
409 "ld [%%l0], %%i0\n\t"
411 "ld [%%l0], %%i1\n\t"
412 : : "m"(&nRegReturn
), "m"(((long*)&nRegReturn
) +1) );
415 case typelib_TypeClass_FLOAT
:
416 __asm__( "ld %0, %%l0\n\t"
418 : : "m"(&nRegReturn
) );
420 case typelib_TypeClass_DOUBLE
:
421 __asm__( "ld %0, %%l0\n\t"
423 : : "m"(&nRegReturn
) );
425 case typelib_TypeClass_VOID
:
428 __asm__( "ld %0, %%l0\n\t"
430 : : "m"(&nRegReturn
) );
436 __asm__( "add %i7, 4, %i7\n\t" );
437 // after call to complex return valued funcion there is an unimp instruction
441 //__________________________________________________________________________________________________
443 int const codeSnippetSize
= 56;
444 unsigned char * codeSnippet(
445 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
448 sal_uInt32 index
= functionIndex
;
449 if (!simpleRetType
) {
452 unsigned int * p
= reinterpret_cast< unsigned int * >(code
);
453 OSL_ASSERT(sizeof (unsigned int) == 4);
466 // sethi %hi(index), %o0:
467 *p
++ = 0x11000000 | (index
>> 10);
468 // or %o0, %lo(index), %o0:
469 *p
++ = 0x90122000 | (index
& 0x3FF);
470 // sethi %hi(vtableOffset), %o2:
471 *p
++ = 0x15000000 | (vtableOffset
>> 10);
472 // or %o2, %lo(vtableOffset), %o2:
473 *p
++ = 0x9412A000 | (vtableOffset
& 0x3FF);
474 // sethi %hi(cpp_vtable_call), %o3:
475 *p
++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) >> 10);
476 // or %o3, %lo(cpp_vtable_call), %o3:
477 *p
++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) & 0x3FF);
483 reinterpret_cast< unsigned char * >(p
) - code
<= codeSnippetSize
);
484 return code
+ codeSnippetSize
;
489 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
491 bridges::cpp_uno::shared::VtableFactory::Slot
*
492 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
494 return static_cast< Slot
* >(block
) + 2;
497 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
500 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
503 bridges::cpp_uno::shared::VtableFactory::Slot
*
504 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
505 void * block
, sal_Int32 slotCount
)
507 Slot
* slots
= mapBlockToVtable(block
);
508 slots
[-2].fn
= 0; //null
509 slots
[-1].fn
= 0; //destructor
510 return slots
+ slotCount
;
513 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
514 Slot
** slots
, unsigned char * code
,
515 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
516 sal_Int32 functionCount
, sal_Int32 vTableOffset
)
518 (*slots
) -= functionCount
;
520 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
521 typelib_TypeDescription
* member
= 0;
522 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
523 OSL_ASSERT(member
!= 0);
524 switch (member
->eTypeClass
) {
525 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
529 code
, functionOffset
++, vTableOffset
,
530 bridges::cpp_uno::shared::isSimpleType(
532 typelib_InterfaceAttributeTypeDescription
* >(
533 member
)->pAttributeTypeRef
));
535 if (!reinterpret_cast<
536 typelib_InterfaceAttributeTypeDescription
* >(
540 code
= codeSnippet(code
, functionOffset
++, vTableOffset
, true);
544 case typelib_TypeClass_INTERFACE_METHOD
:
547 code
, functionOffset
++, vTableOffset
,
548 bridges::cpp_uno::shared::isSimpleType(
550 typelib_InterfaceMethodTypeDescription
* >(
551 member
)->pReturnTypeRef
));
558 TYPELIB_DANGER_RELEASE(member
);
563 void bridges::cpp_uno::shared::VtableFactory::flushCode(
564 unsigned char const *, unsigned char const *)
566 //TODO: IZ 25819 flush the instruction cache (there probably is OS support for this)