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"
34 #include <sal/alloca.h>
36 #include <com/sun/star/uno/genfunc.hxx>
38 #include <typelib/typedescription.hxx>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
45 #include "cc50_solaris_intel.hxx"
47 using namespace com::sun::star::uno
;
52 //==================================================================================================
54 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
55 const typelib_TypeDescription
* pMemberTypeDescr
,
56 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
57 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
59 sal_Int64
* pRegisterReturn
/* space for register return */ )
61 // pCallStack: ret, [return ptr], this, params
62 char * pCppStack
= (char *)(pCallStack
+1);
65 typelib_TypeDescription
* pReturnTypeDescr
= 0;
67 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
69 void * pUnoReturn
= 0;
70 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
74 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
76 pUnoReturn
= pRegisterReturn
; // direct way for simple types
78 else // complex return via ptr (pCppReturn)
80 pCppReturn
= *(void**)pCppStack
;
81 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
] = pUnoArgs
[ nPos
] = pCppStack
;
114 switch (pParamTypeDescr
->eTypeClass
)
116 case typelib_TypeClass_HYPER
:
117 case typelib_TypeClass_UNSIGNED_HYPER
:
118 case typelib_TypeClass_DOUBLE
:
119 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
)(
163 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
165 // in case no exception occured...
168 // destruct temporary in/inout params
169 for ( ; nTempIndizes
--; )
171 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
173 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
174 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
175 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
177 if (pReturnTypeDescr
)
178 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
180 CPPU_CURRENT_NAMESPACE::cc50_solaris_intel_raiseException(
181 &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
182 // has to destruct the any
184 else // else no exception occured...
187 for ( ; nTempIndizes
--; )
189 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
190 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
192 if (pParams
[nIndex
].bOut
) // inout/out
194 // convert and assign
196 pCppArgs
[nIndex
], pParamTypeDescr
,
197 reinterpret_cast< uno_ReleaseFunc
>(cpp_release
) );
198 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
199 pThis
->getBridge()->getUno2Cpp() );
201 // destroy temp uno param
202 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
204 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
207 if (pCppReturn
) // has complex return
209 if (pUnoReturn
!= pCppReturn
) // needs reconversion
211 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
212 pThis
->getBridge()->getUno2Cpp() );
213 // destroy temp uno return
214 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
216 // complex return ptr is set to eax
217 *(void **)pRegisterReturn
= pCppReturn
;
219 if (pReturnTypeDescr
)
221 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
227 //==================================================================================================
228 extern "C" void cpp_vtable_call(
229 int nFunctionIndex
, int nVtableOffset
, void** pCallStack
,
230 sal_Int64 nRegReturn
)
232 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
234 // pCallStack: ret adr, [ret *], this, params
236 if( nFunctionIndex
& 0x80000000 )
238 nFunctionIndex
&= 0x7fffffff;
239 pThis
= pCallStack
[2];
243 pThis
= pCallStack
[1];
245 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
246 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
247 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
250 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
252 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
,
253 "### illegal vtable index!" );
254 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
256 throw RuntimeException( rtl::OUString::createFromAscii("illegal vtable index!"), (XInterface
*)pThis
);
259 // determine called method
260 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
261 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
263 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
265 switch (aMemberDescr
.get()->eTypeClass
)
267 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
269 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
273 pCppI
, aMemberDescr
.get(),
274 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
276 pCallStack
, &nRegReturn
);
281 typelib_MethodParameter aParam
;
283 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
284 aParam
.bIn
= sal_True
;
285 aParam
.bOut
= sal_False
;
288 pCppI
, aMemberDescr
.get(),
289 0, // indicates void return
291 pCallStack
, &nRegReturn
);
295 case typelib_TypeClass_INTERFACE_METHOD
:
298 switch (nFunctionIndex
)
300 // standard XInterface vtable calls
302 pCppI
->acquireProxy(); // non virtual call!
305 pCppI
->releaseProxy(); // non virtual call!
307 case 0: // queryInterface() opt
309 typelib_TypeDescription
* pTD
= 0;
310 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[3] )->getTypeLibType() );
313 XInterface
* pInterface
= 0;
314 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
315 pCppI
->getBridge()->getCppEnv(),
316 (void **)&pInterface
, pCppI
->getOid().pData
,
317 (typelib_InterfaceTypeDescription
*)pTD
);
322 reinterpret_cast< uno_Any
* >( pCallStack
[1] ),
324 reinterpret_cast< uno_AcquireFunc
>(cpp_acquire
) );
325 pInterface
->release();
326 TYPELIB_DANGER_RELEASE( pTD
);
327 *(void **)&nRegReturn
= pCallStack
[1];
330 TYPELIB_DANGER_RELEASE( pTD
);
332 } // else perform queryInterface()
335 pCppI
, aMemberDescr
.get(),
336 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
337 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
338 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
339 pCallStack
, &nRegReturn
);
345 throw RuntimeException( rtl::OUString::createFromAscii("no member description found!"), (XInterface
*)pThis
);
350 //==================================================================================================
351 bool isSimpleStruct(typelib_TypeDescriptionReference
* type
) {
352 typelib_TypeDescription
* td
= 0;
353 TYPELIB_DANGER_GET(&td
, type
);
355 for (typelib_CompoundTypeDescription
* ctd
356 = reinterpret_cast< typelib_CompoundTypeDescription
* >(td
);
357 ctd
!= 0; ctd
= ctd
->pBaseTypeDescription
)
359 OSL_ASSERT(ctd
->aBase
.eTypeClass
== typelib_TypeClass_STRUCT
);
360 for (sal_Int32 i
= 0; i
< ctd
->nMembers
; ++i
) {
361 typelib_TypeClass c
= ctd
->ppTypeRefs
[i
]->eTypeClass
;
363 case typelib_TypeClass_STRING
:
364 case typelib_TypeClass_TYPE
:
365 case typelib_TypeClass_ANY
:
366 case typelib_TypeClass_SEQUENCE
:
367 case typelib_TypeClass_INTERFACE
:
369 case typelib_TypeClass_STRUCT
:
370 if (!isSimpleStruct(ctd
->ppTypeRefs
[i
])) {
376 c
<= typelib_TypeClass_DOUBLE
377 || c
== typelib_TypeClass_ENUM
);
382 TYPELIB_DANGER_RELEASE(td
);
386 extern "C" void privateSnippetExecutorGeneral();
387 extern "C" void privateSnippetExecutorVoid();
388 extern "C" void privateSnippetExecutorHyper();
389 extern "C" void privateSnippetExecutorFloat();
390 extern "C" void privateSnippetExecutorDouble();
391 extern "C" void privateSnippetExecutorStruct();
392 extern "C" typedef void (*PrivateSnippetExecutor
)();
394 int const codeSnippetSize
= 16;
396 unsigned char * codeSnippet(
397 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
398 typelib_TypeDescriptionReference
* returnType
)
400 typelib_TypeClass c
= returnType
== 0
401 ? typelib_TypeClass_VOID
: returnType
->eTypeClass
;
402 if (returnType
!= 0 && !bridges::cpp_uno::shared::isSimpleType(c
)) {
403 functionIndex
|= 0x80000000;
405 PrivateSnippetExecutor exec
;
407 case typelib_TypeClass_VOID
:
408 exec
= privateSnippetExecutorVoid
;
410 case typelib_TypeClass_HYPER
:
411 case typelib_TypeClass_UNSIGNED_HYPER
:
412 exec
= privateSnippetExecutorHyper
;
414 case typelib_TypeClass_FLOAT
:
415 exec
= privateSnippetExecutorFloat
;
417 case typelib_TypeClass_DOUBLE
:
418 exec
= privateSnippetExecutorDouble
;
420 case typelib_TypeClass_STRUCT
:
421 OSL_ASSERT(returnType
!= 0);
422 // For "simple" (more-or-less POD, but not exactly) structs, the caller
423 // pops the pointer to the return value off the stack, as documented in
424 // the Intel SYSV ABI; for other structs (which includes STRING, TYPE,
425 // ANY, sequences, and interfaces, btw.), the callee pops the pointer to
426 // the return value off the stack:
427 exec
= isSimpleStruct(returnType
)
428 ? privateSnippetExecutorStruct
: privateSnippetExecutorGeneral
;
431 exec
= privateSnippetExecutorGeneral
;
434 unsigned char * p
= code
;
435 OSL_ASSERT(sizeof (sal_Int32
) == 4);
436 // mov function_index, %eax:
438 *reinterpret_cast< sal_Int32
* >(p
) = functionIndex
;
439 p
+= sizeof (sal_Int32
);
440 // mov vtable_offset, %edx:
442 *reinterpret_cast< sal_Int32
* >(p
) = vtableOffset
;
443 p
+= sizeof (sal_Int32
);
444 // jmp privateSnippetExecutor:
447 void * e
= reinterpret_cast< void * >(exec
);
449 *reinterpret_cast< sal_Int32
* >(p
)
450 = static_cast< unsigned char * >(e
) - p
- sizeof (sal_Int32
);
451 p
+= sizeof (sal_Int32
);
452 OSL_ASSERT(p
- code
<= codeSnippetSize
);
453 return code
+ codeSnippetSize
;
458 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
460 bridges::cpp_uno::shared::VtableFactory::Slot
*
461 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
) {
462 return static_cast< Slot
* >(block
) + 1;
465 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
468 return (slotCount
+ 3) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
471 bridges::cpp_uno::shared::VtableFactory::Slot
*
472 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
473 void * block
, sal_Int32 slotCount
)
475 Slot
* slots
= mapBlockToVtable(block
) + 2;
476 slots
[-3].fn
= 0; // RTTI
477 slots
[-2].fn
= 0; // null
478 slots
[-1].fn
= 0; // destructor
479 return slots
+ slotCount
;
482 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
483 Slot
** slots
, unsigned char * code
,
484 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
485 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
487 (*slots
) -= functionCount
;
489 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
490 typelib_TypeDescription
* member
= 0;
491 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
492 OSL_ASSERT(member
!= 0);
493 switch (member
->eTypeClass
) {
494 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
498 code
, functionOffset
++, vtableOffset
,
499 reinterpret_cast< typelib_InterfaceAttributeTypeDescription
* >(
500 member
)->pAttributeTypeRef
);
502 if (!reinterpret_cast<
503 typelib_InterfaceAttributeTypeDescription
* >(
507 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, 0);
511 case typelib_TypeClass_INTERFACE_METHOD
:
514 code
, functionOffset
++, vtableOffset
,
515 reinterpret_cast< typelib_InterfaceMethodTypeDescription
* >(
516 member
)->pReturnTypeRef
);
523 TYPELIB_DANGER_RELEASE(member
);
528 void bridges::cpp_uno::shared::VtableFactory::flushCode(
529 unsigned char const *, unsigned char const *)