update dev300-m58
[ooovba.git] / bridges / source / cpp_uno / cc50_solaris_sparc / cpp2uno.cxx
blob7fa0a4880ebbf6167bcc71305a96cc428d617e7e
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.25 $
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>
37 #include <uno/data.h>
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_sparc.hxx"
46 #include "flushcode.hxx"
48 using namespace com::sun::star::uno;
50 namespace
53 //==================================================================================================
54 static typelib_TypeClass cpp2uno_call(
55 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
56 const typelib_TypeDescription * pMemberTypeDescr,
57 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
58 sal_Int32 nParams, typelib_MethodParameter * pParams,
59 void ** pCallStack,
60 sal_Int64 * pRegisterReturn /* space for register return */ )
62 // pCallStack: [return ptr], this, params
63 char * pCppStack = (char *)pCallStack;
65 // return
66 typelib_TypeDescription * pReturnTypeDescr = 0;
67 if (pReturnTypeRef)
68 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
70 void * pUnoReturn = 0;
71 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
73 if (pReturnTypeDescr)
75 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
77 pUnoReturn = pRegisterReturn; // direct way for simple types
79 else // complex return via ptr (pCppReturn)
81 pCppReturn = *pCallStack;
82 pCppStack += sizeof( void* );
83 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
84 pReturnTypeDescr )
85 ? alloca( pReturnTypeDescr->nSize )
86 : pCppReturn); // direct way
89 // pop this
90 pCppStack += sizeof( void* );
92 // stack space
93 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
94 // parameters
95 void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
96 void ** pCppArgs = pUnoArgs + nParams;
97 // indizes of values this have to be converted (interface conversion cpp<=>uno)
98 sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
99 // type descriptions for reconversions
100 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
102 sal_Int32 nTempIndizes = 0;
104 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
106 const typelib_MethodParameter & rParam = pParams[nPos];
107 typelib_TypeDescription * pParamTypeDescr = 0;
108 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
110 if (!rParam.bOut
111 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
112 // value
114 pCppArgs[ nPos ] = pUnoArgs[ nPos ] =
115 CPPU_CURRENT_NAMESPACE::adjustPointer(
116 pCppStack, pParamTypeDescr );
117 switch (pParamTypeDescr->eTypeClass)
119 case typelib_TypeClass_HYPER:
120 case typelib_TypeClass_UNSIGNED_HYPER:
121 case typelib_TypeClass_DOUBLE:
122 pCppStack += sizeof(sal_Int32); // extra long
124 // no longer needed
125 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
127 else // ptr to complex value | ref
129 pCppArgs[nPos] = *(void **)pCppStack;
131 if (! rParam.bIn) // is pure out
133 // uno out is unconstructed mem!
134 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
135 pTempIndizes[nTempIndizes] = nPos;
136 // will be released at reconversion
137 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
139 // is in/inout
140 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
141 pParamTypeDescr ))
143 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
144 *(void **)pCppStack, pParamTypeDescr,
145 pThis->getBridge()->getCpp2Uno() );
146 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
147 // will be released at reconversion
148 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
150 else // direct way
152 pUnoArgs[nPos] = *(void **)pCppStack;
153 // no longer needed
154 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
157 pCppStack += sizeof(sal_Int32); // standard parameter length
160 // ExceptionHolder
161 uno_Any aUnoExc; // Any will be constructed by callee
162 uno_Any * pUnoExc = &aUnoExc;
164 // invoke uno dispatch call
165 (*pThis->getUnoI()->pDispatcher)(
166 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
168 // in case no 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 );
183 CPPU_CURRENT_NAMESPACE::cc50_solaris_sparc_raiseException(
184 &aUnoExc, pThis->getBridge()->getUno2Cpp() );
185 // has to destruct the any
186 // is here for dummy
187 return typelib_TypeClass_VOID;
189 else // else no exception occured...
191 // temporary params
192 for ( ; nTempIndizes--; )
194 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
195 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
197 if (pParams[nIndex].bOut) // inout/out
199 // convert and assign
200 uno_destructData(
201 pCppArgs[nIndex], pParamTypeDescr,
202 reinterpret_cast< uno_ReleaseFunc >(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 );
211 // return
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 );
228 return eRet;
230 else
231 return typelib_TypeClass_VOID;
236 //==================================================================================================
237 static typelib_TypeClass cpp_mediate(
238 sal_Int32 nFunctionIndex,
239 sal_Int32 nVtableOffset,
240 void ** pCallStack,
241 sal_Int64 * pRegisterReturn /* space for register return */ )
243 OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
245 // pCallStack: this, params
246 void * pThis;
247 if( nFunctionIndex & 0x80000000 )
249 nFunctionIndex &= 0x7fffffff;
250 pThis = pCallStack[1];
252 else
254 pThis = pCallStack[0];
256 pThis = static_cast< char * >(pThis) - nVtableOffset;
257 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
258 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
259 pThis);
261 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
263 OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex,
264 "### illegal vtable index!" );
265 // if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
266 // {
267 // RuntimeException aExc;
268 // aExc.Message = OUString::createFromAscii("illegal vtable index!");
269 // aExc.Context = (XInterface *)pThis;
270 // throw aExc;
271 // }
273 // determine called method
274 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
275 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
277 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
279 typelib_TypeClass eRet;
280 switch (aMemberDescr.get()->eTypeClass)
282 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
284 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
286 // is GET method
287 eRet = cpp2uno_call(
288 pCppI, aMemberDescr.get(),
289 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
290 0, 0, // no params
291 pCallStack, pRegisterReturn );
293 else
295 // is SET method
296 typelib_MethodParameter aParam;
297 aParam.pTypeRef =
298 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
299 aParam.bIn = sal_True;
300 aParam.bOut = sal_False;
302 eRet = cpp2uno_call(
303 pCppI, aMemberDescr.get(),
304 0, // indicates void return
305 1, &aParam,
306 pCallStack, pRegisterReturn );
308 break;
310 case typelib_TypeClass_INTERFACE_METHOD:
312 // is METHOD
313 switch (nFunctionIndex)
315 // standard XInterface vtable calls
316 case 1: // acquire()
317 pCppI->acquireProxy(); // non virtual call!
318 eRet = typelib_TypeClass_VOID;
319 break;
320 case 2: // release()
321 pCppI->releaseProxy(); // non virtual call!
322 eRet = typelib_TypeClass_VOID;
323 break;
324 case 0: // queryInterface() opt
326 typelib_TypeDescription * pTD = 0;
327 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[2] )->getTypeLibType() );
328 if (pTD)
330 XInterface * pInterface = 0;
331 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
332 pCppI->getBridge()->getCppEnv(),
333 (void **)&pInterface, pCppI->getOid().pData,
334 (typelib_InterfaceTypeDescription *)pTD );
336 if (pInterface)
338 ::uno_any_construct(
339 reinterpret_cast< uno_Any * >( pCallStack[0] ),
340 &pInterface, pTD,
341 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
342 pInterface->release();
343 TYPELIB_DANGER_RELEASE( pTD );
344 *(void **)pRegisterReturn = pCallStack[0];
345 eRet = typelib_TypeClass_ANY;
346 break;
348 TYPELIB_DANGER_RELEASE( pTD );
350 } // else perform queryInterface()
351 default:
352 eRet = cpp2uno_call(
353 pCppI, aMemberDescr.get(),
354 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
355 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
356 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
357 pCallStack, pRegisterReturn );
359 break;
361 // default:
362 // {
363 // RuntimeException aExc;
364 // aExc.Message = OUString::createFromAscii("no member description found!");
365 // aExc.Context = (XInterface *)pThis;
366 // throw aExc;
367 // // is here for dummy
368 // eRet = typelib_TypeClass_VOID;
369 // }
372 return eRet;
377 //==================================================================================================
378 extern "C" int cpp_vtable_call(
379 int nFunctionIndex, int nVtableOffset, void** pCallStack )
381 sal_Int64 nRegReturn;
382 typelib_TypeClass aType = cpp_mediate(
383 nFunctionIndex, nVtableOffset, pCallStack, &nRegReturn );
384 OSL_ASSERT( sizeof(void *) == sizeof(sal_Int32) );
385 switch( aType )
387 // move return value into register space
388 // (will be loaded by machine code snippet)
389 // Use pCallStack[1/2] instead of pCallStack[0/1], because the former is
390 // properly dword aligned:
391 case typelib_TypeClass_BOOLEAN:
392 case typelib_TypeClass_BYTE:
393 pCallStack[1] = (void*)*(char*)&nRegReturn;
394 break;
395 case typelib_TypeClass_CHAR:
396 case typelib_TypeClass_SHORT:
397 case typelib_TypeClass_UNSIGNED_SHORT:
398 pCallStack[1] = (void*)*(short*)&nRegReturn;
399 break;
400 case typelib_TypeClass_DOUBLE:
401 case typelib_TypeClass_HYPER:
402 case typelib_TypeClass_UNSIGNED_HYPER:
403 // move long to %i1
404 pCallStack[2] = ((void **)&nRegReturn)[ 1 ];
405 case typelib_TypeClass_FLOAT:
406 default:
407 // move long to %i0
408 pCallStack[1] = ((void **)&nRegReturn)[ 0 ];
409 break;
411 return aType;
414 //==================================================================================================
415 namespace {
417 extern "C" void privateSnippetExecutor();
419 int const codeSnippetSize = 7 * 4;
421 unsigned char * codeSnippet(
422 unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
423 bool simpleRetType)
425 sal_uInt32 index = functionIndex;
426 if (!simpleRetType) {
427 index |= 0x80000000;
429 unsigned int * p = reinterpret_cast< unsigned int * >(code);
430 OSL_ASSERT(sizeof (unsigned int) == 4);
431 // save %sp, -96, %sp ! 92 byte minimal stack frame, + 4 byte dword align:
432 *p++ = 0x9DE3BFA0;
433 // sethi %hi(privateSnippetExecutor), %l0:
434 *p++ = 0x21000000
435 | (reinterpret_cast< unsigned int >(privateSnippetExecutor) >> 10);
436 // sethi %hi(index), %o0:
437 *p++ = 0x11000000 | (index >> 10);
438 // or %o0, %lo(index), %o0:
439 *p++ = 0x90122000 | (index & 0x3FF);
440 // sethi %hi(vtableOffset), %o1:
441 *p++ = 0x13000000 | (vtableOffset >> 10);
442 // jmpl %l0, %lo(privateSnippetExecutor), %g0:
443 *p++ = 0x81C42000
444 | (reinterpret_cast< unsigned int >(privateSnippetExecutor) & 0x3FF);
445 // or %o1, %lo(vtableOffset), %o1:
446 *p++ = 0x92126000 | (vtableOffset & 0x3FF);
447 OSL_ASSERT(
448 reinterpret_cast< unsigned char * >(p) - code <= codeSnippetSize);
449 return code + codeSnippetSize;
454 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
456 bridges::cpp_uno::shared::VtableFactory::Slot *
457 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
459 return static_cast< Slot * >(block) + 1;
462 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
463 sal_Int32 slotCount)
465 return (slotCount + 3) * sizeof (Slot) + slotCount * codeSnippetSize;
468 bridges::cpp_uno::shared::VtableFactory::Slot *
469 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
470 void * block, sal_Int32 slotCount)
472 Slot * slots = mapBlockToVtable(block) + 2;
473 slots[-3].fn = 0; // RTTI
474 slots[-2].fn = 0; // null
475 slots[-1].fn = 0; // destructor
476 return slots + slotCount;
479 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
480 Slot ** slots, unsigned char * code,
481 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
482 sal_Int32 functionCount, sal_Int32 vtableOffset)
484 (*slots) -= functionCount;
485 Slot * s = *slots;
486 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
487 typelib_TypeDescription * member = 0;
488 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
489 OSL_ASSERT(member != 0);
490 switch (member->eTypeClass) {
491 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
492 // Getter:
493 (s++)->fn = code;
494 code = codeSnippet(
495 code, functionOffset++, vtableOffset,
496 bridges::cpp_uno::shared::isSimpleType(
497 reinterpret_cast<
498 typelib_InterfaceAttributeTypeDescription * >(
499 member)->pAttributeTypeRef));
500 // Setter:
501 if (!reinterpret_cast<
502 typelib_InterfaceAttributeTypeDescription * >(
503 member)->bReadOnly)
505 (s++)->fn = code;
506 code = codeSnippet(code, functionOffset++, vtableOffset, true);
508 break;
510 case typelib_TypeClass_INTERFACE_METHOD:
511 (s++)->fn = code;
512 code = codeSnippet(
513 code, functionOffset++, vtableOffset,
514 bridges::cpp_uno::shared::isSimpleType(
515 reinterpret_cast<
516 typelib_InterfaceMethodTypeDescription * >(
517 member)->pReturnTypeRef));
518 break;
520 default:
521 OSL_ASSERT(false);
522 break;
524 TYPELIB_DANGER_RELEASE(member);
526 return code;
529 void bridges::cpp_uno::shared::VtableFactory::flushCode(
530 unsigned char const * begin, unsigned char const * end)
532 bridges::cpp_uno::cc50_solaris_sparc::flushCode(begin, end);