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 #include <sal/alloca.h>
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
37 #include <typelib/typedescription.hxx>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
46 using namespace ::com::sun::star::uno
;
51 //==================================================================================================
52 static typelib_TypeClass
cpp2uno_call(
53 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
54 const typelib_TypeDescription
* pMemberTypeDescr
,
55 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
56 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
58 sal_Int64
* pRegisterReturn
/* space for register return */ )
60 // pCallStack: ret, [return ptr], this, params
61 char * pCppStack
= (char *)(pCallStack
+1);
64 typelib_TypeDescription
* pReturnTypeDescr
= 0;
66 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
68 void * pUnoReturn
= 0;
69 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
73 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
75 pUnoReturn
= pRegisterReturn
; // direct way for simple types
77 else // complex return via ptr (pCppReturn)
79 pCppReturn
= *(void **)pCppStack
;
80 pCppStack
+= sizeof(void *);
82 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
84 ? alloca( pReturnTypeDescr
->nSize
)
85 : pCppReturn
); // direct way
89 pCppStack
+= sizeof( void* );
92 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
94 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
95 void ** pCppArgs
= pUnoArgs
+ nParams
;
96 // indizes of values this have to be converted (interface conversion cpp<=>uno)
97 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
98 // type descriptions for reconversions
99 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
101 sal_Int32 nTempIndizes
= 0;
103 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
105 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
106 typelib_TypeDescription
* pParamTypeDescr
= 0;
107 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
110 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
113 pCppArgs
[nPos
] = pCppStack
;
114 pUnoArgs
[nPos
] = pCppStack
;
115 switch (pParamTypeDescr
->eTypeClass
)
117 case typelib_TypeClass_HYPER
:
118 case typelib_TypeClass_UNSIGNED_HYPER
:
119 case typelib_TypeClass_DOUBLE
:
120 pCppStack
+= sizeof(sal_Int32
); // extra long
123 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
125 else // ptr to complex value | ref
127 pCppArgs
[nPos
] = *(void **)pCppStack
;
129 if (! rParam
.bIn
) // is pure out
131 // uno out is unconstructed mem!
132 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
133 pTempIndizes
[nTempIndizes
] = nPos
;
134 // will be released at reconversion
135 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
138 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
141 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
142 *(void **)pCppStack
, pParamTypeDescr
,
143 pThis
->getBridge()->getCpp2Uno() );
144 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
145 // will be released at reconversion
146 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
150 pUnoArgs
[nPos
] = *(void **)pCppStack
;
152 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
155 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
159 uno_Any aUnoExc
= {0}; // Any will be constructed by callee
160 uno_Any
* pUnoExc
= &aUnoExc
;
162 // invoke uno dispatch call
163 (*pThis
->getUnoI()->pDispatcher
)(
164 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
165 #if OSL_DEBUG_LEVEL > 1
167 fprintf( stderr
, ">cpp2uno_call pUnoExc %x\n", pUnoExc
);
170 // in case an exception occured...
173 // destruct temporary in/inout params
174 for ( ; nTempIndizes
--; )
176 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
178 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
179 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
180 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
182 if (pReturnTypeDescr
)
183 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
185 CPPU_CURRENT_NAMESPACE::raiseException(
186 &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
187 // has to destruct the any
189 return typelib_TypeClass_VOID
;
191 else // else no exception occured...
194 for ( ; nTempIndizes
--; )
196 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
197 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
199 if (pParams
[nIndex
].bOut
) // inout/out
201 // convert and assign
202 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
203 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
204 pThis
->getBridge()->getUno2Cpp() );
206 // destroy temp uno param
207 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
209 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
212 if (pCppReturn
) // has complex return
214 if (pUnoReturn
!= pCppReturn
) // needs reconversion
216 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
217 pThis
->getBridge()->getUno2Cpp() );
218 // destroy temp uno return
219 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
221 // complex return ptr is set to eax
222 *(void **)pRegisterReturn
= pCppReturn
;
224 if (pReturnTypeDescr
)
226 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
227 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
231 return typelib_TypeClass_VOID
;
236 //==================================================================================================
237 static typelib_TypeClass
cpp_mediate(
238 sal_Int32 nFunctionIndex
,
239 sal_Int32 nVtableOffset
,
241 sal_Int64
* pRegisterReturn
/* space for register return */ )
243 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
245 // pCallStack: ret adr, [ret *], this, params
247 if( nFunctionIndex
& 0x80000000 )
249 nFunctionIndex
&= 0x7fffffff;
250 pThis
= pCallStack
[2];
254 pThis
= pCallStack
[1];
256 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
257 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
258 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
261 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
263 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
264 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
266 throw RuntimeException(
267 rtl::OUString::createFromAscii("illegal vtable index!"),
268 (XInterface
*)pThis
);
271 // determine called method
272 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
273 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
275 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
277 typelib_TypeClass eRet
;
278 switch (aMemberDescr
.get()->eTypeClass
)
280 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
282 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
286 pCppI
, aMemberDescr
.get(),
287 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
289 pCallStack
, pRegisterReturn
);
294 typelib_MethodParameter aParam
;
296 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
297 aParam
.bIn
= sal_True
;
298 aParam
.bOut
= sal_False
;
301 pCppI
, aMemberDescr
.get(),
302 0, // indicates void return
304 pCallStack
, pRegisterReturn
);
308 case typelib_TypeClass_INTERFACE_METHOD
:
311 switch (nFunctionIndex
)
314 pCppI
->acquireProxy(); // non virtual call!
315 eRet
= typelib_TypeClass_VOID
;
318 pCppI
->releaseProxy(); // non virtual call!
319 eRet
= typelib_TypeClass_VOID
;
321 case 0: // queryInterface() opt
323 typelib_TypeDescription
* pTD
= 0;
324 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[3] )->getTypeLibType() );
327 XInterface
* pInterface
= 0;
328 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
329 pCppI
->getBridge()->getCppEnv(),
330 (void **)&pInterface
, pCppI
->getOid().pData
,
331 (typelib_InterfaceTypeDescription
*)pTD
);
336 reinterpret_cast< uno_Any
* >( pCallStack
[1] ),
337 &pInterface
, pTD
, cpp_acquire
);
338 pInterface
->release();
339 TYPELIB_DANGER_RELEASE( pTD
);
340 *(void **)pRegisterReturn
= pCallStack
[1];
341 eRet
= typelib_TypeClass_ANY
;
344 TYPELIB_DANGER_RELEASE( pTD
);
346 } // else perform queryInterface()
349 pCppI
, aMemberDescr
.get(),
350 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
351 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
352 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
353 pCallStack
, pRegisterReturn
);
359 throw RuntimeException(
360 rtl::OUString::createFromAscii("no member description found!"),
361 (XInterface
*)pThis
);
363 eRet
= typelib_TypeClass_VOID
;
370 //==================================================================================================
372 * is called on incoming vtable calls
373 * (called by asm snippets)
375 static void cpp_vtable_call(
376 int nFunctionIndex
, int nVtableOffset
, void** pCallStack
)
377 __attribute__((regparm(3)));
379 void cpp_vtable_call( int nFunctionIndex
, int nVtableOffset
, void** pCallStack
)
381 volatile long nRegReturn
[2];
382 typelib_TypeClass aType
= cpp_mediate(
383 nFunctionIndex
, nVtableOffset
, pCallStack
, (sal_Int64
*)nRegReturn
);
387 case typelib_TypeClass_HYPER
:
388 case typelib_TypeClass_UNSIGNED_HYPER
:
389 __asm__( "movl %1, %%edx\n\t"
391 : : "m"(nRegReturn
[0]), "m"(nRegReturn
[1]) );
393 case typelib_TypeClass_FLOAT
:
394 __asm__( "flds %0\n\t"
397 : : "m"(*(float *)nRegReturn
) );
399 case typelib_TypeClass_DOUBLE
:
400 __asm__( "fldl %0\n\t"
403 : : "m"(*(double *)nRegReturn
) );
405 // case typelib_TypeClass_UNSIGNED_SHORT:
406 // case typelib_TypeClass_SHORT:
407 // __asm__( "movswl %0, %%eax\n"
408 // : : "m"(nRegReturn) );
411 __asm__( "movl %0, %%eax\n"
412 : : "m"(nRegReturn
[0]) );
418 //==================================================================================================
419 int const codeSnippetSize
= 20;
421 unsigned char * codeSnippet(
422 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
425 if (!simpleRetType
) {
426 functionIndex
|= 0x80000000;
428 unsigned char * p
= code
;
429 OSL_ASSERT(sizeof (sal_Int32
) == 4);
430 // mov function_index, %eax:
432 *reinterpret_cast< sal_Int32
* >(p
) = functionIndex
;
433 p
+= sizeof (sal_Int32
);
434 // mov vtable_offset, %edx:
436 *reinterpret_cast< sal_Int32
* >(p
) = vtableOffset
;
437 p
+= sizeof (sal_Int32
);
441 // jmp cpp_vtable_call:
443 *reinterpret_cast< sal_Int32
* >(p
)
444 = ((unsigned char *) cpp_vtable_call
) - p
- sizeof (sal_Int32
);
445 p
+= sizeof (sal_Int32
);
446 OSL_ASSERT(p
- code
<= codeSnippetSize
);
447 return code
+ codeSnippetSize
;
452 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
454 bridges::cpp_uno::shared::VtableFactory::Slot
*
455 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
457 return static_cast< Slot
* >(block
) + 2;
460 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
463 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
466 bridges::cpp_uno::shared::VtableFactory::Slot
*
467 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
468 void * block
, sal_Int32 slotCount
)
470 Slot
* slots
= mapBlockToVtable(block
);
473 return slots
+ slotCount
;
476 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
477 Slot
** slots
, unsigned char * code
,
478 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
479 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
481 (*slots
) -= functionCount
;
483 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
484 typelib_TypeDescription
* member
= 0;
485 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
486 OSL_ASSERT(member
!= 0);
487 switch (member
->eTypeClass
) {
488 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
492 code
, functionOffset
++, vtableOffset
,
493 bridges::cpp_uno::shared::isSimpleType(
495 typelib_InterfaceAttributeTypeDescription
* >(
496 member
)->pAttributeTypeRef
));
498 if (!reinterpret_cast<
499 typelib_InterfaceAttributeTypeDescription
* >(
503 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
507 case typelib_TypeClass_INTERFACE_METHOD
:
510 code
, functionOffset
++, vtableOffset
,
511 bridges::cpp_uno::shared::isSimpleType(
513 typelib_InterfaceMethodTypeDescription
* >(
514 member
)->pReturnTypeRef
));
521 TYPELIB_DANGER_RELEASE(member
);
526 void bridges::cpp_uno::shared::VtableFactory::flushCode(
527 unsigned char const *, unsigned char const *)