Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_macosx_intel / uno2cpp.cxx
blob5a7f8b13e5745e97f1012bdc8bc91eeb35f2034a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: uno2cpp.cxx,v $
10 * $Revision: 1.6 $
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 <stdio.h>
36 // #include <malloc.h>
38 #include <com/sun/star/uno/genfunc.hxx>
39 #include "com/sun/star/uno/RuntimeException.hpp"
40 #include <uno/data.h>
42 #include "bridges/cpp_uno/shared/bridge.hxx"
43 #include "bridges/cpp_uno/shared/types.hxx"
44 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
45 #include "bridges/cpp_uno/shared/vtables.hxx"
47 #include "share.hxx"
49 using namespace ::rtl;
50 using namespace ::com::sun::star::uno;
52 namespace
55 //==================================================================================================
56 // The call instruction within the asm section of callVirtualMethod may throw
57 // exceptions. So that the compiler handles this correctly, it is important
58 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
59 // never happens at runtime), which in turn can throw exceptions, and (b)
60 // callVirtualMethod is not inlined at its call site (so that any exceptions are
61 // caught which are thrown from the instruction calling callVirtualMethod):
62 void callVirtualMethod(
63 void * pAdjustedThisPtr,
64 sal_Int32 nVtableIndex,
65 void * pRegisterReturn,
66 typelib_TypeDescription * pReturnTypeDescr, bool bSimpleReturn,
67 sal_Int32 * pStackLongs,
68 sal_Int32 nStackLongs ) __attribute__((noinline));
70 void callVirtualMethod(
71 void * pAdjustedThisPtr,
72 sal_Int32 nVtableIndex,
73 void * pRegisterReturn,
74 typelib_TypeDescription * pReturnTypeDescr, bool bSimpleReturn,
75 sal_Int32 * pStackLongs,
76 sal_Int32 nStackLongs )
78 // parameter list is mixed list of * and values
79 // reference parameters are pointers
81 OSL_ENSURE( pStackLongs && pAdjustedThisPtr, "### null ptr!" );
82 OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
83 OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
85 // never called
86 if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
88 volatile long edx = 0, eax = 0; // for register returns
89 void * stackptr;
90 asm volatile (
91 "mov %%esp, %6\n\t"
92 "mov %0, %%eax\n\t"
93 "mov %%eax, %%edx\n\t"
94 // stack padding to keep stack aligned:
95 "shl $2, %%eax\n\t"
96 "neg %%eax\n\t"
97 "add %%esp, %%eax\n\t"
98 "and $0xf, %%eax\n\t"
99 "sub %%eax, %%esp\n\t"
100 // copy:
101 "mov %%edx, %%eax\n\t"
102 "dec %%edx\n\t"
103 "shl $2, %%edx\n\t"
104 "add %1, %%edx\n"
105 "Lcopy:\n\t"
106 "pushl 0(%%edx)\n\t"
107 "sub $4, %%edx\n\t"
108 "dec %%eax\n\t"
109 "jne Lcopy\n\t"
110 // do the actual call
111 "mov %2, %%edx\n\t"
112 "mov 0(%%edx), %%edx\n\t"
113 "mov %3, %%eax\n\t"
114 "shl $2, %%eax\n\t"
115 "add %%eax, %%edx\n\t"
116 "mov 0(%%edx), %%edx\n\t"
117 "call *%%edx\n\t"
118 // save return registers
119 "mov %%eax, %4\n\t"
120 "mov %%edx, %5\n\t"
121 // cleanup stack
122 "mov %6, %%esp\n\t"
124 : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
125 "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
126 : "eax", "edx" );
127 switch( pReturnTypeDescr->eTypeClass )
129 case typelib_TypeClass_VOID:
130 break;
131 case typelib_TypeClass_HYPER:
132 case typelib_TypeClass_UNSIGNED_HYPER:
133 ((long*)pRegisterReturn)[1] = edx;
134 case typelib_TypeClass_LONG:
135 case typelib_TypeClass_UNSIGNED_LONG:
136 case typelib_TypeClass_CHAR:
137 case typelib_TypeClass_ENUM:
138 ((long*)pRegisterReturn)[0] = eax;
139 break;
140 case typelib_TypeClass_SHORT:
141 case typelib_TypeClass_UNSIGNED_SHORT:
142 *(unsigned short*)pRegisterReturn = eax;
143 break;
144 case typelib_TypeClass_BOOLEAN:
145 case typelib_TypeClass_BYTE:
146 *(unsigned char*)pRegisterReturn = eax;
147 break;
148 case typelib_TypeClass_FLOAT:
149 asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
150 break;
151 case typelib_TypeClass_DOUBLE:
152 asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
153 break;
154 default: {
155 sal_Int32 const nRetSize = pReturnTypeDescr->nSize;
156 if (bSimpleReturn && nRetSize <= 8 && nRetSize > 0) {
157 if (nRetSize > 4)
158 static_cast<long *>(pRegisterReturn)[1] = edx;
159 static_cast<long *>(pRegisterReturn)[0] = eax;
161 break;
166 //==================================================================================================
167 static void cpp_call(
168 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
169 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
170 typelib_TypeDescriptionReference * pReturnTypeRef,
171 sal_Int32 nParams, typelib_MethodParameter * pParams,
172 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
174 // max space for: [complex ret ptr], values|ptr ...
175 char * pCppStack =
176 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
177 char * pCppStackStart = pCppStack;
179 // return
180 typelib_TypeDescription * pReturnTypeDescr = 0;
181 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
182 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
184 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
185 bool bSimpleReturn = true;
187 if (pReturnTypeDescr)
189 bSimpleReturn = CPPU_CURRENT_NAMESPACE::isSimpleReturnType(
190 pReturnTypeDescr);
191 if (bSimpleReturn)
193 pCppReturn = pUnoReturn; // direct way for simple types
195 else
197 pCppReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
198 pReturnTypeDescr )
199 ? alloca( pReturnTypeDescr->nSize )
200 : pUnoReturn); // direct way
201 // complex return via ptr
202 *(void **)pCppStack = pCppReturn;
203 pCppStack += sizeof(void *);
206 // push this
207 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
208 + aVtableSlot.offset;
209 *(void**)pCppStack = pAdjustedThisPtr;
210 pCppStack += sizeof( void* );
212 // stack space
213 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
214 // args
215 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
216 // indizes of values this have to be converted (interface conversion cpp<=>uno)
217 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
218 // type descriptions for reconversions
219 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
221 sal_Int32 nTempIndizes = 0;
223 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
225 const typelib_MethodParameter & rParam = pParams[nPos];
226 typelib_TypeDescription * pParamTypeDescr = 0;
227 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
229 if (!rParam.bOut
230 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
232 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
233 pThis->getBridge()->getUno2Cpp() );
235 switch (pParamTypeDescr->eTypeClass)
237 case typelib_TypeClass_HYPER:
238 case typelib_TypeClass_UNSIGNED_HYPER:
239 case typelib_TypeClass_DOUBLE:
240 pCppStack += sizeof(sal_Int32); // extra long
241 default:
242 break;
244 // no longer needed
245 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
247 else // ptr to complex value | ref
249 if (! rParam.bIn) // is pure out
251 // cpp out is constructed mem, uno out is not!
252 uno_constructData(
253 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
254 pParamTypeDescr );
255 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
256 // will be released at reconversion
257 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
259 // is in/inout
260 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
261 pParamTypeDescr ))
263 uno_copyAndConvertData(
264 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
265 pUnoArgs[nPos], pParamTypeDescr,
266 pThis->getBridge()->getUno2Cpp() );
268 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
269 // will be released at reconversion
270 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
272 else // direct way
274 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
275 // no longer needed
276 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
279 pCppStack += sizeof(sal_Int32); // standard parameter length
284 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
285 callVirtualMethod(
286 pAdjustedThisPtr, aVtableSlot.index,
287 pCppReturn, pReturnTypeDescr, bSimpleReturn,
288 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
289 // NO exception occured...
290 *ppUnoExc = 0;
292 // reconvert temporary params
293 for ( ; nTempIndizes--; )
295 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
296 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
298 if (pParams[nIndex].bIn)
300 if (pParams[nIndex].bOut) // inout
302 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
303 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
304 pThis->getBridge()->getCpp2Uno() );
307 else // pure out
309 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
310 pThis->getBridge()->getCpp2Uno() );
312 // destroy temp cpp param => cpp: every param was constructed
313 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
315 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
317 // return value
318 if (pCppReturn && pUnoReturn != pCppReturn)
320 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
321 pThis->getBridge()->getCpp2Uno() );
322 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
325 catch (...)
327 #if OSL_DEBUG_LEVEL > 1
328 fprintf( stderr, "caught C++ exception\n" );
329 #endif
330 // fill uno exception
331 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
333 // temporary params
334 for ( ; nTempIndizes--; )
336 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
337 // destroy temp cpp param => cpp: every param was constructed
338 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
339 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
341 // return type
342 if (pReturnTypeDescr)
343 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
349 namespace CPPU_CURRENT_NAMESPACE {
350 bool isSimpleReturnType(typelib_TypeDescription * pTD, bool recursive)
352 if (bridges::cpp_uno::shared::isSimpleType( pTD ))
353 return true;
354 // Only structs of exactly 1, 2, 4, or 8 bytes are returned through
355 // registers, see <http://developer.apple.com/documentation/DeveloperTools/
356 // Conceptual/LowLevelABI/Articles/IA32.html>:
357 if (pTD->eTypeClass == typelib_TypeClass_STRUCT &&
358 (recursive || pTD->nSize <= 2 || pTD->nSize == 4 || pTD->nSize == 8))
360 typelib_CompoundTypeDescription *const pCompTD =
361 (typelib_CompoundTypeDescription *) pTD;
362 for ( sal_Int32 pos = pCompTD->nMembers; pos--; ) {
363 typelib_TypeDescription * pMemberTD = 0;
364 TYPELIB_DANGER_GET( &pMemberTD, pCompTD->ppTypeRefs[pos] );
365 bool const b = isSimpleReturnType(pMemberTD, true);
366 TYPELIB_DANGER_RELEASE( pMemberTD );
367 if (! b)
368 return false;
370 return true;
372 return false;
376 //==================================================================================================
378 namespace bridges { namespace cpp_uno { namespace shared {
379 void unoInterfaceProxyDispatch(
380 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
381 void * pReturn, void * pArgs[], uno_Any ** ppException )
383 // is my surrogate
384 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
385 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
387 switch (pMemberDescr->eTypeClass)
389 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
391 VtableSlot aVtableSlot(
392 getVtableSlot(
393 reinterpret_cast<
394 typelib_InterfaceAttributeTypeDescription const * >(
395 pMemberDescr)));
396 if (pReturn)
398 // dependent dispatch
399 cpp_call(
400 pThis, aVtableSlot,
401 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
402 0, 0, // no params
403 pReturn, pArgs, ppException );
405 else
407 // is SET
408 typelib_MethodParameter aParam;
409 aParam.pTypeRef =
410 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
411 aParam.bIn = sal_True;
412 aParam.bOut = sal_False;
414 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
415 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
416 typelib_typedescriptionreference_new(
417 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
419 // dependent dispatch
420 aVtableSlot.index += 1; // get, then set method
421 cpp_call(
422 pThis, aVtableSlot,
423 pReturnTypeRef,
424 1, &aParam,
425 pReturn, pArgs, ppException );
427 typelib_typedescriptionreference_release( pReturnTypeRef );
430 break;
432 case typelib_TypeClass_INTERFACE_METHOD:
434 VtableSlot aVtableSlot(
435 getVtableSlot(
436 reinterpret_cast<
437 typelib_InterfaceMethodTypeDescription const * >(
438 pMemberDescr)));
439 switch (aVtableSlot.index)
441 // standard calls
442 case 1: // acquire uno interface
443 (*pUnoI->acquire)( pUnoI );
444 *ppException = 0;
445 break;
446 case 2: // release uno interface
447 (*pUnoI->release)( pUnoI );
448 *ppException = 0;
449 break;
450 case 0: // queryInterface() opt
452 typelib_TypeDescription * pTD = 0;
453 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
454 if (pTD)
456 uno_Interface * pInterface = 0;
457 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
458 pThis->pBridge->getUnoEnv(),
459 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
461 if (pInterface)
463 ::uno_any_construct(
464 reinterpret_cast< uno_Any * >( pReturn ),
465 &pInterface, pTD, 0 );
466 (*pInterface->release)( pInterface );
467 TYPELIB_DANGER_RELEASE( pTD );
468 *ppException = 0;
469 break;
471 TYPELIB_DANGER_RELEASE( pTD );
473 } // else perform queryInterface()
474 default:
475 // dependent dispatch
476 cpp_call(
477 pThis, aVtableSlot,
478 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
479 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
480 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
481 pReturn, pArgs, ppException );
483 break;
485 default:
487 ::com::sun::star::uno::RuntimeException aExc(
488 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
489 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
491 Type const & rExcType = ::getCppuType( &aExc );
492 // binary identical null reference
493 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
498 } } }