bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_solaris_sparc / cpp2uno.cxx
blob639b8da3dc882428b9261c81b4f9af5a633aee9c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/genfunc.hxx>
21 #include <sal/log.hxx>
22 #include <typelib/typedescription.hxx>
23 #include <uno/data.h>
24 #include "bridge.hxx"
25 #include "cppinterfaceproxy.hxx"
26 #include "types.hxx"
27 #include "vtablefactory.hxx"
28 #include "share.hxx"
29 #include <sal/alloca.h>
31 using namespace com::sun::star::uno;
33 namespace
36 static typelib_TypeClass cpp2uno_call(
37 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
38 const typelib_TypeDescription * pMemberTypeDescr,
39 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
40 sal_Int32 nParams, typelib_MethodParameter * pParams,
41 void ** pCallStack,
42 sal_Int64 * pRegisterReturn /* space for register return */ )
44 // pCallStack: [ret ptr], this, params
45 char * pCppStack = (char *)pCallStack;
47 // return
48 typelib_TypeDescription * pReturnTypeDescr = 0;
49 if (pReturnTypeRef)
50 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
52 void * pUnoReturn = 0;
53 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
55 if (pReturnTypeDescr)
57 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
58 pUnoReturn = pRegisterReturn; // direct way for simple types
59 else // complex return via ptr (pCppReturn)
61 pCppReturn = *(void**)pCppStack;
62 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
63 pReturnTypeDescr )
64 ? alloca( pReturnTypeDescr->nSize )
65 : pCppReturn); // direct way
66 pCppStack += sizeof( void* );
69 // pop this
70 pCppStack += sizeof( void* );
72 // stack space
73 static_assert(sizeof(void *) == sizeof(sal_Int32), "### unexpected size!");
74 // parameters
75 void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
76 void ** pCppArgs = pUnoArgs + nParams;
77 // indices of values this have to be converted (interface conversion cpp<=>uno)
78 sal_Int32 * pTempIndices = (sal_Int32 *)(pUnoArgs + (2 * nParams));
79 // type descriptions for reconversions
80 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
82 sal_Int32 nTempIndices = 0;
84 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
86 const typelib_MethodParameter & rParam = pParams[nPos];
87 typelib_TypeDescription * pParamTypeDescr = 0;
88 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
90 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr )) // value
92 pCppArgs[nPos] = pUnoArgs[nPos] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack, pParamTypeDescr);
93 switch (pParamTypeDescr->eTypeClass)
95 case typelib_TypeClass_HYPER:
96 case typelib_TypeClass_UNSIGNED_HYPER:
97 case typelib_TypeClass_DOUBLE:
99 if ((reinterpret_cast< long >(pCppStack) & 7) != 0)
101 static_assert(sizeof (double) == sizeof (sal_Int64), "boo");
102 void * pDest = alloca( sizeof (sal_Int64) );
103 *reinterpret_cast< sal_Int32 * >(pDest) =
104 *reinterpret_cast< sal_Int32 const * >(pCppStack);
105 *(reinterpret_cast< sal_Int32 * >(pDest) + 1) =
106 *(reinterpret_cast< sal_Int32 const * >(pCppStack) + 1);
107 pCppArgs[nPos] = pUnoArgs[nPos] = pDest;
109 pCppStack += sizeof (sal_Int32); // extra long
110 break;
113 // no longer needed
114 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
116 else // ptr to complex value | ref
118 pCppArgs[nPos] = *(void **)pCppStack;
120 if (! rParam.bIn) // is pure out
122 // uno out is unconstructed mem!
123 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
124 pTempIndices[nTempIndices] = nPos;
125 // will be released at reconversion
126 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
128 // is in/inout
129 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
130 pParamTypeDescr ))
132 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
133 *(void **)pCppStack, pParamTypeDescr,
134 pThis->getBridge()->getCpp2Uno() );
135 pTempIndices[nTempIndices] = nPos; // has to be reconverted
136 // will be released at reconversion
137 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
139 else // direct way
141 pUnoArgs[nPos] = *(void **)pCppStack;
142 // no longer needed
143 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
146 pCppStack += sizeof(sal_Int32); // standard parameter length
149 // ExceptionHolder
150 uno_Any aUnoExc; // Any will be constructed by callee
151 uno_Any * pUnoExc = &aUnoExc;
153 // invoke uno dispatch call
154 (*pThis->getUnoI()->pDispatcher)(pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
156 // in case an exception occurred...
157 if (pUnoExc)
159 // destruct temporary in/inout params
160 for ( ; nTempIndices--; )
162 sal_Int32 nIndex = pTempIndices[nTempIndices];
164 if (pParams[nIndex].bIn) // is in/inout => was constructed
165 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndices], 0 );
166 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
168 if (pReturnTypeDescr)
169 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
170 CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc, pThis->getBridge()->getUno2Cpp() );
171 // has to destruct the any
172 // is here for dummy
173 return typelib_TypeClass_VOID;
175 else // else no exception occurred...
177 // temporary params
178 for ( ; nTempIndices--; )
180 sal_Int32 nIndex = pTempIndices[nTempIndices];
181 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
183 if (pParams[nIndex].bOut) // inout/out
185 // convert and assign
186 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
187 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
188 pThis->getBridge()->getUno2Cpp() );
190 // destroy temp uno param
191 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
193 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
195 // return
196 if (pCppReturn) // has complex return
198 if (pUnoReturn != pCppReturn) // needs reconversion
200 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
201 pThis->getBridge()->getUno2Cpp() );
202 // destroy temp uno return
203 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
205 // complex return ptr is set to eax
206 *(void **)pRegisterReturn = pCppReturn;
208 if (pReturnTypeDescr)
210 typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
211 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
212 return eRet;
214 else
215 return typelib_TypeClass_VOID;
220 static typelib_TypeClass cpp_mediate(
221 sal_Int32 nFunctionIndex,
222 sal_Int32 nVtableOffset,
223 void ** pCallStack,
224 sal_Int64 * pRegisterReturn /* space for register return */ )
226 static_assert( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
228 // pCallStack: this, params
229 // eventual [ret*] lies at pCallStack -1
230 // so count down pCallStack by one to keep it simple
231 // pCallStack: this, params
232 // eventual [ret*] lies at pCallStack -1
233 // so count down pCallStack by one to keep it simple
234 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
235 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
236 static_cast< char * >(*pCallStack) - nVtableOffset);
237 if ((nFunctionIndex & 0x80000000) != 0) {
238 nFunctionIndex &= 0x7FFFFFFF;
239 --pCallStack;
242 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
244 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
246 SAL_WARN(
247 "bridges",
248 "illegal " << OUString::unacquired(&pTypeDescr->aBase.pTypeName)
249 << " vtable index " << nFunctionIndex << "/"
250 << pTypeDescr->nMapFunctionIndexToMemberIndex);
251 throw RuntimeException(
252 ("illegal " + OUString::unacquired(&pTypeDescr->aBase.pTypeName)
253 + " vtable index " + OUString::number(nFunctionIndex) + "/"
254 + OUString::number(pTypeDescr->nMapFunctionIndexToMemberIndex)),
255 (XInterface *)pCppI);
258 // determine called method
259 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
260 assert(nMemberPos < pTypeDescr->nAllMembers);
262 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
264 #if defined BRIDGES_DEBUG
265 OString cstr( OUStringToOString( aMemberDescr.get()->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
266 fprintf( stderr, "calling %s, nFunctionIndex=%d\n", cstr.getStr(), nFunctionIndex );
267 #endif
269 typelib_TypeClass eRet;
270 switch (aMemberDescr.get()->eTypeClass)
272 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
274 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
276 // is GET method
277 eRet = cpp2uno_call(
278 pCppI, aMemberDescr.get(),
279 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
280 0, 0, // no params
281 pCallStack, pRegisterReturn );
283 else
285 // is SET method
286 typelib_MethodParameter aParam;
287 aParam.pTypeRef =
288 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
289 aParam.bIn = sal_True;
290 aParam.bOut = sal_False;
292 eRet = cpp2uno_call(
293 pCppI, aMemberDescr.get(),
294 0, // indicates void return
295 1, &aParam,
296 pCallStack, pRegisterReturn );
298 break;
300 case typelib_TypeClass_INTERFACE_METHOD:
302 // is METHOD
303 switch (nFunctionIndex)
305 case 1: // acquire()
306 pCppI->acquireProxy(); // non virtual call!
307 eRet = typelib_TypeClass_VOID;
308 break;
309 case 2: // release()
310 pCppI->releaseProxy(); // non virtual call!
311 eRet = typelib_TypeClass_VOID;
312 break;
313 case 0: // queryInterface() opt
315 typelib_TypeDescription * pTD = 0;
316 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[2] )->getTypeLibType() );
317 if (pTD)
319 XInterface * pInterface = 0;
320 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
321 pCppI->getBridge()->getCppEnv(),
322 (void **)&pInterface, pCppI->getOid().pData, (typelib_InterfaceTypeDescription *)pTD );
324 if (pInterface)
326 ::uno_any_construct(
327 reinterpret_cast< uno_Any * >( pCallStack[0] ),
328 &pInterface, pTD, cpp_acquire );
329 pInterface->release();
330 TYPELIB_DANGER_RELEASE( pTD );
331 *(void **)pRegisterReturn = pCallStack[0];
332 eRet = typelib_TypeClass_ANY;
333 break;
335 TYPELIB_DANGER_RELEASE( pTD );
337 } // else perform queryInterface()
338 default:
339 eRet = cpp2uno_call(
340 pCppI, aMemberDescr.get(),
341 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
342 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
343 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
344 pCallStack, pRegisterReturn );
346 break;
348 default:
350 throw RuntimeException("no member description found!", (XInterface *)pCppI );
353 return eRet;
358 * is called on incoming vtable calls
359 * (called by asm snippets)
361 static void cpp_vtable_call()
363 volatile sal_Int64 nRegReturn;
364 int nFunctionIndex;
365 void** pCallStack;
366 int vTableOffset;
368 __asm__( "st %%i0, %0\n\t"
369 "st %%i1, %1\n\t"
370 "st %%i2, %2\n\t"
371 : : "m"(nFunctionIndex), "m"(pCallStack), "m"(vTableOffset) );
373 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
374 // fflush(stderr);
376 sal_Bool bComplex = (nFunctionIndex & 0x80000000) ? sal_True : sal_False;
377 typelib_TypeClass aType =
378 cpp_mediate( nFunctionIndex, vTableOffset, pCallStack+17, (sal_Int64*)&nRegReturn );
380 switch( aType )
382 case typelib_TypeClass_BOOLEAN:
383 case typelib_TypeClass_BYTE:
384 __asm__( "ld %0, %%l0\n\t"
385 "ldsb [%%l0], %%i0\n"
386 : : "m"(&nRegReturn) );
387 break;
388 case typelib_TypeClass_CHAR:
389 case typelib_TypeClass_SHORT:
390 case typelib_TypeClass_UNSIGNED_SHORT:
391 __asm__( "ld %0, %%l0\n\t"
392 "ldsh [%%l0], %%i0\n"
393 : : "m"(&nRegReturn) );
394 break;
395 case typelib_TypeClass_HYPER:
396 case typelib_TypeClass_UNSIGNED_HYPER:
398 __asm__( "ld %0, %%l0\n\t"
399 "ld [%%l0], %%i0\n\t"
400 "ld %1, %%l0\n\t"
401 "ld [%%l0], %%i1\n\t"
402 : : "m"(&nRegReturn), "m"(((long*)&nRegReturn) +1) );
404 break;
405 case typelib_TypeClass_FLOAT:
406 __asm__( "ld %0, %%l0\n\t"
407 "ld [%%l0], %%f0\n"
408 : : "m"(&nRegReturn) );
409 break;
410 case typelib_TypeClass_DOUBLE:
411 __asm__( "ld %0, %%l0\n\t"
412 "ldd [%%l0], %%f0\n"
413 : : "m"(&nRegReturn) );
414 break;
415 case typelib_TypeClass_VOID:
416 break;
417 default:
418 __asm__( "ld %0, %%l0\n\t"
419 "ld [%%l0], %%i0\n"
420 : : "m"(&nRegReturn) );
421 break;
424 if( bComplex )
426 __asm__( "add %i7, 4, %i7\n\t" );
427 // after call to complex return valued function there is an unimp instruction
433 int const codeSnippetSize = 56;
434 unsigned char * codeSnippet(
435 unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
436 bool simpleRetType)
438 sal_uInt32 index = functionIndex;
439 if (!simpleRetType) {
440 index |= 0x80000000;
442 unsigned int * p = reinterpret_cast< unsigned int * >(code);
443 static_assert(sizeof (unsigned int) == 4);
444 // st %o0, [%sp+68]:
445 *p++ = 0xD023A044;
446 // st %o1, [%sp+72]:
447 *p++ = 0xD223A048;
448 // st %o2, [%sp+76]:
449 *p++ = 0xD423A04C;
450 // st %o3, [%sp+80]:
451 *p++ = 0xD623A050;
452 // st %o4, [%sp+84]:
453 *p++ = 0xD823A054;
454 // st %o5, [%sp+88]:
455 *p++ = 0xDA23A058;
456 // sethi %hi(index), %o0:
457 *p++ = 0x11000000 | (index >> 10);
458 // or %o0, %lo(index), %o0:
459 *p++ = 0x90122000 | (index & 0x3FF);
460 // sethi %hi(vtableOffset), %o2:
461 *p++ = 0x15000000 | (vtableOffset >> 10);
462 // or %o2, %lo(vtableOffset), %o2:
463 *p++ = 0x9412A000 | (vtableOffset & 0x3FF);
464 // sethi %hi(cpp_vtable_call), %o3:
465 *p++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call) >> 10);
466 // or %o3, %lo(cpp_vtable_call), %o3:
467 *p++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call) & 0x3FF);
468 // jmpl %o3, %g0:
469 *p++ = 0x81C2C000;
470 // mov %sp, %o1:
471 *p++ = 0x9210000E;
472 assert(reinterpret_cast< unsigned char * >(p) - code <= codeSnippetSize);
473 return code + codeSnippetSize;
476 } //end of namespace
478 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
480 bridges::cpp_uno::shared::VtableFactory::Slot *
481 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
483 return static_cast< Slot * >(block) + 2;
486 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
487 sal_Int32 slotCount)
489 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
492 bridges::cpp_uno::shared::VtableFactory::Slot *
493 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
494 void * block, sal_Int32 slotCount, sal_Int32,
495 typelib_InterfaceTypeDescription *)
497 Slot * slots = mapBlockToVtable(block);
498 slots[-2].fn = 0; //null
499 slots[-1].fn = 0; //destructor
500 return slots + slotCount;
503 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
504 Slot ** slots, unsigned char * code,
505 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
506 sal_Int32 functionCount, sal_Int32 vTableOffset)
508 (*slots) -= functionCount;
509 Slot * s = *slots;
510 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
511 typelib_TypeDescription * member = 0;
512 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
513 assert(member != 0);
514 switch (member->eTypeClass) {
515 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
516 // Getter:
517 (s++)->fn = code;
518 code = codeSnippet(
519 code, functionOffset++, vTableOffset,
520 bridges::cpp_uno::shared::isSimpleType(
521 reinterpret_cast<
522 typelib_InterfaceAttributeTypeDescription * >(
523 member)->pAttributeTypeRef));
524 // Setter:
525 if (!reinterpret_cast<
526 typelib_InterfaceAttributeTypeDescription * >(
527 member)->bReadOnly)
529 (s++)->fn = code;
530 code = codeSnippet(code, functionOffset++, vTableOffset, true);
532 break;
534 case typelib_TypeClass_INTERFACE_METHOD:
535 (s++)->fn = code;
536 code = codeSnippet(
537 code, functionOffset++, vTableOffset,
538 bridges::cpp_uno::shared::isSimpleType(
539 reinterpret_cast<
540 typelib_InterfaceMethodTypeDescription * >(
541 member)->pReturnTypeRef));
542 break;
544 default:
545 assert(false);
546 break;
548 TYPELIB_DANGER_RELEASE(member);
550 return code;
553 void bridges::cpp_uno::shared::VtableFactory::flushCode(
554 unsigned char const *, unsigned char const *)
556 //TODO: IZ 25819 flush the instruction cache (there probably is OS support for this)
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */