Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_solaris_sparc / cpp2uno.cxx
blob9a7aa3cb5d3d07ddfc4ad1e4ccb228d4c64c1478
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>
31 #include <uno/data.h>
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"
36 #include "share.hxx"
37 #include <sal/alloca.h>
39 using namespace com::sun::star::uno;
41 namespace
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,
49 void ** pCallStack,
50 sal_Int64 * pRegisterReturn /* space for register return */ )
52 // pCallStack: [ret ptr], this, params
53 char * pCppStack = (char *)pCallStack;
55 // return
56 typelib_TypeDescription * pReturnTypeDescr = 0;
57 if (pReturnTypeRef)
58 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
60 void * pUnoReturn = 0;
61 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
63 if (pReturnTypeDescr)
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(
71 pReturnTypeDescr )
72 ? alloca( pReturnTypeDescr->nSize )
73 : pCppReturn); // direct way
74 pCppStack += sizeof( void* );
77 // pop this
78 pCppStack += sizeof( void* );
80 // stack space
81 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
82 // parameters
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
118 break;
121 // no longer needed
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;
136 // is in/inout
137 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
138 pParamTypeDescr ))
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;
147 else // direct way
149 pUnoArgs[nPos] = *(void **)pCppStack;
150 // no longer needed
151 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
154 pCppStack += sizeof(sal_Int32); // standard parameter length
157 // ExceptionHolder
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...
165 if (pUnoExc)
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
180 // is here for dummy
181 return typelib_TypeClass_VOID;
183 else // else no exception occurred...
185 // temporary params
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 );
203 // return
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 );
220 return eRet;
222 else
223 return typelib_TypeClass_VOID;
228 //==================================================================================================
229 static typelib_TypeClass cpp_mediate(
230 sal_Int32 nFunctionIndex,
231 sal_Int32 nVtableOffset,
232 void ** pCallStack,
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;
248 --pCallStack;
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 );
269 #endif
271 typelib_TypeClass eRet;
272 switch (aMemberDescr.get()->eTypeClass)
274 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
276 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
278 // is GET method
279 eRet = cpp2uno_call(
280 pCppI, aMemberDescr.get(),
281 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
282 0, 0, // no params
283 pCallStack, pRegisterReturn );
285 else
287 // is SET method
288 typelib_MethodParameter aParam;
289 aParam.pTypeRef =
290 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
291 aParam.bIn = sal_True;
292 aParam.bOut = sal_False;
294 eRet = cpp2uno_call(
295 pCppI, aMemberDescr.get(),
296 0, // indicates void return
297 1, &aParam,
298 pCallStack, pRegisterReturn );
300 break;
302 case typelib_TypeClass_INTERFACE_METHOD:
304 // is METHOD
305 switch (nFunctionIndex)
307 case 1: // acquire()
308 pCppI->acquireProxy(); // non virtual call!
309 eRet = typelib_TypeClass_VOID;
310 break;
311 case 2: // release()
312 pCppI->releaseProxy(); // non virtual call!
313 eRet = typelib_TypeClass_VOID;
314 break;
315 case 0: // queryInterface() opt
317 typelib_TypeDescription * pTD = 0;
318 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[2] )->getTypeLibType() );
319 if (pTD)
321 XInterface * pInterface = 0;
322 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
323 pCppI->getBridge()->getCppEnv(),
324 (void **)&pInterface, pCppI->getOid().pData, (typelib_InterfaceTypeDescription *)pTD );
326 if (pInterface)
328 ::uno_any_construct(
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;
335 break;
337 TYPELIB_DANGER_RELEASE( pTD );
339 } // else perform queryInterface()
340 default:
341 eRet = cpp2uno_call(
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 );
348 break;
350 default:
352 throw RuntimeException(rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no member description found!" )), (XInterface *)pCppI );
355 return eRet;
360 //==================================================================================================
362 * is called on incoming vtable calls
363 * (called by asm snippets)
365 static void cpp_vtable_call()
367 volatile sal_Int64 nRegReturn;
368 int nFunctionIndex;
369 void** pCallStack;
370 int vTableOffset;
372 __asm__( "st %%i0, %0\n\t"
373 "st %%i1, %1\n\t"
374 "st %%i2, %2\n\t"
375 : : "m"(nFunctionIndex), "m"(pCallStack), "m"(vTableOffset) );
377 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
378 // fflush(stderr);
380 sal_Bool bComplex = nFunctionIndex & 0x80000000 ? sal_True : sal_False;
381 typelib_TypeClass aType =
382 cpp_mediate( nFunctionIndex, vTableOffset, pCallStack+17, (sal_Int64*)&nRegReturn );
384 switch( aType )
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) );
391 break;
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) );
398 break;
399 case typelib_TypeClass_HYPER:
400 case typelib_TypeClass_UNSIGNED_HYPER:
402 __asm__( "ld %0, %%l0\n\t"
403 "ld [%%l0], %%i0\n\t"
404 "ld %1, %%l0\n\t"
405 "ld [%%l0], %%i1\n\t"
406 : : "m"(&nRegReturn), "m"(((long*)&nRegReturn) +1) );
408 break;
409 case typelib_TypeClass_FLOAT:
410 __asm__( "ld %0, %%l0\n\t"
411 "ld [%%l0], %%f0\n"
412 : : "m"(&nRegReturn) );
413 break;
414 case typelib_TypeClass_DOUBLE:
415 __asm__( "ld %0, %%l0\n\t"
416 "ldd [%%l0], %%f0\n"
417 : : "m"(&nRegReturn) );
418 break;
419 case typelib_TypeClass_VOID:
420 break;
421 default:
422 __asm__( "ld %0, %%l0\n\t"
423 "ld [%%l0], %%i0\n"
424 : : "m"(&nRegReturn) );
425 break;
428 if( bComplex )
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,
440 bool simpleRetType)
442 sal_uInt32 index = functionIndex;
443 if (!simpleRetType) {
444 index |= 0x80000000;
446 unsigned int * p = reinterpret_cast< unsigned int * >(code);
447 OSL_ASSERT(sizeof (unsigned int) == 4);
448 // st %o0, [%sp+68]:
449 *p++ = 0xD023A044;
450 // st %o1, [%sp+72]:
451 *p++ = 0xD223A048;
452 // st %o2, [%sp+76]:
453 *p++ = 0xD423A04C;
454 // st %o3, [%sp+80]:
455 *p++ = 0xD623A050;
456 // st %o4, [%sp+84]:
457 *p++ = 0xD823A054;
458 // st %o5, [%sp+88]:
459 *p++ = 0xDA23A058;
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);
472 // jmpl %o3, %g0:
473 *p++ = 0x81C2C000;
474 // mov %sp, %o1:
475 *p++ = 0x9210000E;
476 OSL_ASSERT(
477 reinterpret_cast< unsigned char * >(p) - code <= codeSnippetSize);
478 return code + codeSnippetSize;
481 } //end of namespace
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(
492 sal_Int32 slotCount)
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;
513 Slot * s = *slots;
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:
520 // Getter:
521 (s++)->fn = code;
522 code = codeSnippet(
523 code, functionOffset++, vTableOffset,
524 bridges::cpp_uno::shared::isSimpleType(
525 reinterpret_cast<
526 typelib_InterfaceAttributeTypeDescription * >(
527 member)->pAttributeTypeRef));
528 // Setter:
529 if (!reinterpret_cast<
530 typelib_InterfaceAttributeTypeDescription * >(
531 member)->bReadOnly)
533 (s++)->fn = code;
534 code = codeSnippet(code, functionOffset++, vTableOffset, true);
536 break;
538 case typelib_TypeClass_INTERFACE_METHOD:
539 (s++)->fn = code;
540 code = codeSnippet(
541 code, functionOffset++, vTableOffset,
542 bridges::cpp_uno::shared::isSimpleType(
543 reinterpret_cast<
544 typelib_InterfaceMethodTypeDescription * >(
545 member)->pReturnTypeRef));
546 break;
548 default:
549 OSL_ASSERT(false);
550 break;
552 TYPELIB_DANGER_RELEASE(member);
554 return code;
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: */