merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_solaris_sparc / cpp2uno.cxx
blobd427f2d286b469ea6eb281589eb3a0842029d793
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: cpp2uno.cxx,v $
10 * $Revision: 1.8 $
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>
35 #include <uno/data.h>
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"
40 #include "share.hxx"
41 #include <sal/alloca.h>
43 using namespace com::sun::star::uno;
45 namespace
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,
53 void ** pCallStack,
54 sal_Int64 * pRegisterReturn /* space for register return */ )
56 // pCallStack: [ret ptr], this, params
57 char * pCppStack = (char *)pCallStack;
59 // return
60 typelib_TypeDescription * pReturnTypeDescr = 0;
61 if (pReturnTypeRef)
62 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
64 void * pUnoReturn = 0;
65 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
67 if (pReturnTypeDescr)
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(
75 pReturnTypeDescr )
76 ? alloca( pReturnTypeDescr->nSize )
77 : pCppReturn); // direct way
78 pCppStack += sizeof( void* );
81 // pop this
82 pCppStack += sizeof( void* );
84 // stack space
85 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
86 // parameters
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
122 break;
125 // no longer needed
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;
140 // is in/inout
141 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
142 pParamTypeDescr ))
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;
151 else // direct way
153 pUnoArgs[nPos] = *(void **)pCppStack;
154 // no longer needed
155 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
158 pCppStack += sizeof(sal_Int32); // standard parameter length
161 // ExceptionHolder
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...
169 if (pUnoExc)
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
184 // is here for dummy
185 return typelib_TypeClass_VOID;
187 else // else no exception occured...
189 // temporary params
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 );
207 // return
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 );
224 return eRet;
226 else
227 return typelib_TypeClass_VOID;
232 //==================================================================================================
233 static typelib_TypeClass cpp_mediate(
234 sal_Int32 nFunctionIndex,
235 sal_Int32 nVtableOffset,
236 void ** pCallStack,
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;
252 --pCallStack;
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 );
273 #endif
275 typelib_TypeClass eRet;
276 switch (aMemberDescr.get()->eTypeClass)
278 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
280 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
282 // is GET method
283 eRet = cpp2uno_call(
284 pCppI, aMemberDescr.get(),
285 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
286 0, 0, // no params
287 pCallStack, pRegisterReturn );
289 else
291 // is SET method
292 typelib_MethodParameter aParam;
293 aParam.pTypeRef =
294 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
295 aParam.bIn = sal_True;
296 aParam.bOut = sal_False;
298 eRet = cpp2uno_call(
299 pCppI, aMemberDescr.get(),
300 0, // indicates void return
301 1, &aParam,
302 pCallStack, pRegisterReturn );
304 break;
306 case typelib_TypeClass_INTERFACE_METHOD:
308 // is METHOD
309 switch (nFunctionIndex)
311 case 1: // acquire()
312 pCppI->acquireProxy(); // non virtual call!
313 eRet = typelib_TypeClass_VOID;
314 break;
315 case 2: // release()
316 pCppI->releaseProxy(); // non virtual call!
317 eRet = typelib_TypeClass_VOID;
318 break;
319 case 0: // queryInterface() opt
321 typelib_TypeDescription * pTD = 0;
322 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[2] )->getTypeLibType() );
323 if (pTD)
325 XInterface * pInterface = 0;
326 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
327 pCppI->getBridge()->getCppEnv(),
328 (void **)&pInterface, pCppI->getOid().pData, (typelib_InterfaceTypeDescription *)pTD );
330 if (pInterface)
332 ::uno_any_construct(
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;
339 break;
341 TYPELIB_DANGER_RELEASE( pTD );
343 } // else perform queryInterface()
344 default:
345 eRet = cpp2uno_call(
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 );
352 break;
354 default:
356 throw RuntimeException(rtl::OUString::createFromAscii("no member description found!"), (XInterface *)pCppI );
357 // is here for dummy
358 eRet = typelib_TypeClass_VOID;
361 return eRet;
366 //==================================================================================================
368 * is called on incoming vtable calls
369 * (called by asm snippets)
371 static void cpp_vtable_call()
373 volatile sal_Int64 nRegReturn;
374 int nFunctionIndex;
375 void** pCallStack;
376 int vTableOffset;
378 __asm__( "st %%i0, %0\n\t"
379 "st %%i1, %1\n\t"
380 "st %%i2, %2\n\t"
381 : : "m"(nFunctionIndex), "m"(pCallStack), "m"(vTableOffset) );
383 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
384 // fflush(stderr);
386 sal_Bool bComplex = nFunctionIndex & 0x80000000 ? sal_True : sal_False;
387 typelib_TypeClass aType =
388 cpp_mediate( nFunctionIndex, vTableOffset, pCallStack+17, (sal_Int64*)&nRegReturn );
390 switch( aType )
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) );
397 break;
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) );
404 break;
405 case typelib_TypeClass_HYPER:
406 case typelib_TypeClass_UNSIGNED_HYPER:
408 __asm__( "ld %0, %%l0\n\t"
409 "ld [%%l0], %%i0\n\t"
410 "ld %1, %%l0\n\t"
411 "ld [%%l0], %%i1\n\t"
412 : : "m"(&nRegReturn), "m"(((long*)&nRegReturn) +1) );
414 break;
415 case typelib_TypeClass_FLOAT:
416 __asm__( "ld %0, %%l0\n\t"
417 "ld [%%l0], %%f0\n"
418 : : "m"(&nRegReturn) );
419 break;
420 case typelib_TypeClass_DOUBLE:
421 __asm__( "ld %0, %%l0\n\t"
422 "ldd [%%l0], %%f0\n"
423 : : "m"(&nRegReturn) );
424 break;
425 case typelib_TypeClass_VOID:
426 break;
427 default:
428 __asm__( "ld %0, %%l0\n\t"
429 "ld [%%l0], %%i0\n"
430 : : "m"(&nRegReturn) );
431 break;
434 if( bComplex )
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,
446 bool simpleRetType)
448 sal_uInt32 index = functionIndex;
449 if (!simpleRetType) {
450 index |= 0x80000000;
452 unsigned int * p = reinterpret_cast< unsigned int * >(code);
453 OSL_ASSERT(sizeof (unsigned int) == 4);
454 // st %o0, [%sp+68]:
455 *p++ = 0xD023A044;
456 // st %o1, [%sp+72]:
457 *p++ = 0xD223A048;
458 // st %o2, [%sp+76]:
459 *p++ = 0xD423A04C;
460 // st %o3, [%sp+80]:
461 *p++ = 0xD623A050;
462 // st %o4, [%sp+84]:
463 *p++ = 0xD823A054;
464 // st %o5, [%sp+88]:
465 *p++ = 0xDA23A058;
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);
478 // jmpl %o3, %g0:
479 *p++ = 0x81C2C000;
480 // mov %sp, %o1:
481 *p++ = 0x9210000E;
482 OSL_ASSERT(
483 reinterpret_cast< unsigned char * >(p) - code <= codeSnippetSize);
484 return code + codeSnippetSize;
487 } //end of namespace
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(
498 sal_Int32 slotCount)
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;
519 Slot * s = *slots;
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:
526 // Getter:
527 (s++)->fn = code;
528 code = codeSnippet(
529 code, functionOffset++, vTableOffset,
530 bridges::cpp_uno::shared::isSimpleType(
531 reinterpret_cast<
532 typelib_InterfaceAttributeTypeDescription * >(
533 member)->pAttributeTypeRef));
534 // Setter:
535 if (!reinterpret_cast<
536 typelib_InterfaceAttributeTypeDescription * >(
537 member)->bReadOnly)
539 (s++)->fn = code;
540 code = codeSnippet(code, functionOffset++, vTableOffset, true);
542 break;
544 case typelib_TypeClass_INTERFACE_METHOD:
545 (s++)->fn = code;
546 code = codeSnippet(
547 code, functionOffset++, vTableOffset,
548 bridges::cpp_uno::shared::isSimpleType(
549 reinterpret_cast<
550 typelib_InterfaceMethodTypeDescription * >(
551 member)->pReturnTypeRef));
552 break;
554 default:
555 OSL_ASSERT(false);
556 break;
558 TYPELIB_DANGER_RELEASE(member);
560 return code;
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)