bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_sparc / cpp2uno.cxx
blob8afbd04a71616a8ef3f16c9b67108bff332c2b33
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"
30 using namespace com::sun::star::uno;
32 namespace
35 static typelib_TypeClass cpp2uno_call(
36 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
37 const typelib_TypeDescription * pMemberTypeDescr,
38 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
39 sal_Int32 nParams, typelib_MethodParameter * pParams,
40 void ** pCallStack,
41 sal_Int64 * pRegisterReturn /* space for register return */ )
43 // pCallStack: [ret ptr], this, params
44 char * pCppStack = (char *)pCallStack;
46 // return
47 typelib_TypeDescription * pReturnTypeDescr = 0;
48 if (pReturnTypeRef)
49 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
51 void * pUnoReturn = 0;
52 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
54 if (pReturnTypeDescr)
56 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
57 pUnoReturn = pRegisterReturn; // direct way for simple types
58 else // complex return via ptr (pCppReturn)
60 pCppReturn = *(void**)pCppStack;
61 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
62 pReturnTypeDescr )
63 ? alloca( pReturnTypeDescr->nSize )
64 : pCppReturn); // direct way
65 pCppStack += sizeof( void* );
68 // pop this
69 pCppStack += sizeof( void* );
71 // stack space
72 static_assert(sizeof(void *) == sizeof(sal_Int32), "### unexpected size!");
73 // parameters
74 void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
75 void ** pCppArgs = pUnoArgs + nParams;
76 // indices of values this have to be converted (interface conversion cpp<=>uno)
77 sal_Int32 * pTempIndices = (sal_Int32 *)(pUnoArgs + (2 * nParams));
78 // type descriptions for reconversions
79 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
81 sal_Int32 nTempIndices = 0;
83 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
85 const typelib_MethodParameter & rParam = pParams[nPos];
86 typelib_TypeDescription * pParamTypeDescr = 0;
87 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
89 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr )) // value
91 pCppArgs[nPos] = pUnoArgs[nPos] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack, pParamTypeDescr);
92 switch (pParamTypeDescr->eTypeClass)
94 case typelib_TypeClass_HYPER:
95 case typelib_TypeClass_UNSIGNED_HYPER:
96 case typelib_TypeClass_DOUBLE:
98 if ((reinterpret_cast< long >(pCppStack) & 7) != 0)
100 static_assert(sizeof (double) == sizeof (sal_Int64), "boo");
101 void * pDest = alloca( sizeof (sal_Int64) );
102 *reinterpret_cast< sal_Int32 * >(pDest) =
103 *reinterpret_cast< sal_Int32 const * >(pCppStack);
104 *(reinterpret_cast< sal_Int32 * >(pDest) + 1) =
105 *(reinterpret_cast< sal_Int32 const * >(pCppStack) + 1);
106 pCppArgs[nPos] = pUnoArgs[nPos] = pDest;
108 pCppStack += sizeof (sal_Int32); // extra long
109 break;
110 default:
111 break;
114 // no longer needed
115 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
117 else // ptr to complex value | ref
119 pCppArgs[nPos] = *(void **)pCppStack;
121 if (! rParam.bIn) // is pure out
123 // uno out is unconstructed mem!
124 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
125 pTempIndices[nTempIndices] = nPos;
126 // will be released at reconversion
127 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
129 // is in/inout
130 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
131 pParamTypeDescr ))
133 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
134 *(void **)pCppStack, pParamTypeDescr,
135 pThis->getBridge()->getCpp2Uno() );
136 pTempIndices[nTempIndices] = nPos; // has to be reconverted
137 // will be released at reconversion
138 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
140 else // direct way
142 pUnoArgs[nPos] = *(void **)pCppStack;
143 // no longer needed
144 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
147 pCppStack += sizeof(sal_Int32); // standard parameter length
150 // ExceptionHolder
151 uno_Any aUnoExc; // Any will be constructed by callee
152 uno_Any * pUnoExc = &aUnoExc;
154 // invoke uno dispatch call
155 (*pThis->getUnoI()->pDispatcher)(pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
157 // in case an exception occurred...
158 if (pUnoExc)
160 // destruct temporary in/inout params
161 for ( ; nTempIndices--; )
163 sal_Int32 nIndex = pTempIndices[nTempIndices];
165 if (pParams[nIndex].bIn) // is in/inout => was constructed
166 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndices], 0 );
167 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
169 if (pReturnTypeDescr)
170 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
171 CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc, pThis->getBridge()->getUno2Cpp() );
172 // has to destruct the any
173 // is here for dummy
174 return typelib_TypeClass_VOID;
176 else // else no exception occurred...
178 // temporary params
179 for ( ; nTempIndices--; )
181 sal_Int32 nIndex = pTempIndices[nTempIndices];
182 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
184 if (pParams[nIndex].bOut) // inout/out
186 // convert and assign
187 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
188 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
189 pThis->getBridge()->getUno2Cpp() );
191 // destroy temp uno param
192 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
194 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
196 // return
197 if (pCppReturn) // has complex return
199 if (pUnoReturn != pCppReturn) // needs reconversion
201 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
202 pThis->getBridge()->getUno2Cpp() );
203 // destroy temp uno return
204 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
206 // complex return ptr is set to eax
207 *(void **)pRegisterReturn = pCppReturn;
209 if (pReturnTypeDescr)
211 typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
212 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
213 return eRet;
215 else
216 return typelib_TypeClass_VOID;
221 static typelib_TypeClass cpp_mediate(
222 sal_Int32 nFunctionIndex,
223 sal_Int32 nVtableOffset,
224 void ** pCallStack,
225 sal_Int64 * pRegisterReturn /* space for register return */ )
227 static_assert(sizeof(sal_Int32)==sizeof(void *), "### unexpected!");
229 // pCallStack: this, params
230 // eventual [ret*] lies at pCallStack -1
231 // so count down pCallStack by one to keep it simple
232 // pCallStack: this, params
233 // eventual [ret*] lies at pCallStack -1
234 // so count down pCallStack by one to keep it simple
235 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
236 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
237 static_cast< char * >(*pCallStack) - nVtableOffset);
238 if ((nFunctionIndex & 0x80000000) != 0) {
239 nFunctionIndex &= 0x7FFFFFFF;
240 --pCallStack;
243 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
245 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
247 SAL_WARN(
248 "bridges",
249 "illegal " << OUString::unacquired(&pTypeDescr->aBase.pTypeName)
250 << " vtable index " << nFunctionIndex << "/"
251 << pTypeDescr->nMapFunctionIndexToMemberIndex);
252 throw RuntimeException(
253 ("illegal " + OUString::unacquired(&pTypeDescr->aBase.pTypeName)
254 + " vtable index " + OUString::number(nFunctionIndex) + "/"
255 + OUString::number(pTypeDescr->nMapFunctionIndexToMemberIndex)),
256 (XInterface *)pCppI);
259 // determine called method
260 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
261 assert(nMemberPos < pTypeDescr->nAllMembers);
263 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
265 #if defined BRIDGES_DEBUG
266 OString cstr( OUStringToOString( aMemberDescr.get()->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
267 fprintf( stderr, "calling %s, nFunctionIndex=%d\n", cstr.getStr(), nFunctionIndex );
268 #endif
270 typelib_TypeClass eRet;
271 switch (aMemberDescr.get()->eTypeClass)
273 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
275 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
277 // is GET method
278 eRet = cpp2uno_call(
279 pCppI, aMemberDescr.get(),
280 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
281 0, 0, // no params
282 pCallStack, pRegisterReturn );
284 else
286 // is SET method
287 typelib_MethodParameter aParam;
288 aParam.pTypeRef =
289 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
290 aParam.bIn = sal_True;
291 aParam.bOut = sal_False;
293 eRet = cpp2uno_call(
294 pCppI, aMemberDescr.get(),
295 0, // indicates void return
296 1, &aParam,
297 pCallStack, pRegisterReturn );
299 break;
301 case typelib_TypeClass_INTERFACE_METHOD:
303 // is METHOD
304 switch (nFunctionIndex)
306 case 1: // acquire()
307 pCppI->acquireProxy(); // non virtual call!
308 eRet = typelib_TypeClass_VOID;
309 break;
310 case 2: // release()
311 pCppI->releaseProxy(); // non virtual call!
312 eRet = typelib_TypeClass_VOID;
313 break;
314 case 0: // queryInterface() opt
316 typelib_TypeDescription * pTD = 0;
317 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[2] )->getTypeLibType() );
318 if (pTD)
320 XInterface * pInterface = 0;
321 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
322 pCppI->getBridge()->getCppEnv(),
323 (void **)&pInterface, pCppI->getOid().pData, (typelib_InterfaceTypeDescription *)pTD );
325 if (pInterface)
327 ::uno_any_construct(
328 reinterpret_cast< uno_Any * >( pCallStack[0] ),
329 &pInterface, pTD, cpp_acquire );
330 pInterface->release();
331 TYPELIB_DANGER_RELEASE( pTD );
332 *(void **)pRegisterReturn = pCallStack[0];
333 eRet = typelib_TypeClass_ANY;
334 break;
336 TYPELIB_DANGER_RELEASE( pTD );
338 } // else perform queryInterface()
339 default:
340 eRet = cpp2uno_call(
341 pCppI, aMemberDescr.get(),
342 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
343 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
344 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
345 pCallStack, pRegisterReturn );
347 break;
349 default:
351 throw RuntimeException( "no member description found!", (XInterface *)pCppI );
354 return eRet;
359 * is called on incoming vtable calls
360 * (called by asm snippets)
362 static void cpp_vtable_call()
364 sal_Int64 nRegReturn;
365 int nFunctionIndex;
366 void** pCallStack;
367 int vTableOffset;
369 void * pRegReturn = &nRegReturn;
371 __asm__( "st %%i0, %0\n\t"
372 "st %%i1, %1\n\t"
373 "st %%i2, %2\n\t"
374 : : "m"(nFunctionIndex), "m"(pCallStack), "m"(vTableOffset) );
376 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
377 // fflush(stderr);
379 const sal_Bool bComplex = (nFunctionIndex & 0x80000000) ? sal_True : sal_False;
380 typelib_TypeClass aType =
381 cpp_mediate( nFunctionIndex, vTableOffset, pCallStack+17, (sal_Int64*)&nRegReturn );
383 switch( aType )
385 case typelib_TypeClass_BOOLEAN:
386 case typelib_TypeClass_BYTE:
387 __asm__( "ld %0, %%l0\n\t"
388 "ldsb [%%l0], %%i0\n"
389 : : "m"(pRegReturn) );
390 break;
391 case typelib_TypeClass_CHAR:
392 case typelib_TypeClass_SHORT:
393 case typelib_TypeClass_UNSIGNED_SHORT:
394 __asm__( "ld %0, %%l0\n\t"
395 "ldsh [%%l0], %%i0\n"
396 : : "m"(pRegReturn) );
397 break;
398 case typelib_TypeClass_HYPER:
399 case typelib_TypeClass_UNSIGNED_HYPER:
400 __asm__( "ld %0, %%l0\n\t"
401 "ld [%%l0], %%i0\n\t"
402 "add %%l0, 4, %%l0\n\t"
403 "ld [%%l0], %%i1\n\t"
404 : : "m"(pRegReturn) );
406 break;
407 case typelib_TypeClass_FLOAT:
408 __asm__( "ld %0, %%l0\n\t"
409 "ld [%%l0], %%f0\n"
410 : : "m"(pRegReturn) );
411 break;
412 case typelib_TypeClass_DOUBLE:
413 __asm__( "ld %0, %%l0\n\t"
414 "ldd [%%l0], %%f0\n"
415 : : "m"(pRegReturn) );
416 break;
417 case typelib_TypeClass_VOID:
418 break;
419 default:
420 __asm__( "ld %0, %%l0\n\t"
421 "ld [%%l0], %%i0\n"
422 : : "m"(pRegReturn) );
423 break;
426 if( bComplex )
428 __asm__( "add %i7, 4, %i7\n\t" );
429 // after call to complex return valued function there is an unimp instruction
435 int const codeSnippetSize = 56;
436 unsigned char * codeSnippet(
437 unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
438 bool simpleRetType)
440 sal_uInt32 index = functionIndex;
441 if (!simpleRetType) {
442 index |= 0x80000000;
444 unsigned int * p = reinterpret_cast< unsigned int * >(code);
445 static_assert(sizeof (unsigned int) == 4, "boo");
446 // st %o0, [%sp+68]:
447 *p++ = 0xD023A044;
448 // st %o1, [%sp+72]:
449 *p++ = 0xD223A048;
450 // st %o2, [%sp+76]:
451 *p++ = 0xD423A04C;
452 // st %o3, [%sp+80]:
453 *p++ = 0xD623A050;
454 // st %o4, [%sp+84]:
455 *p++ = 0xD823A054;
456 // st %o5, [%sp+88]:
457 *p++ = 0xDA23A058;
458 // sethi %hi(index), %o0:
459 *p++ = 0x11000000 | (index >> 10);
460 // or %o0, %lo(index), %o0:
461 *p++ = 0x90122000 | (index & 0x3FF);
462 // sethi %hi(vtableOffset), %o2:
463 *p++ = 0x15000000 | (vtableOffset >> 10);
464 // or %o2, %lo(vtableOffset), %o2:
465 *p++ = 0x9412A000 | (vtableOffset & 0x3FF);
466 // sethi %hi(cpp_vtable_call), %o3:
467 *p++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call) >> 10);
468 // or %o3, %lo(cpp_vtable_call), %o3:
469 *p++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call) & 0x3FF);
470 // jmpl %o3, %g0:
471 *p++ = 0x81C2C000;
472 // mov %sp, %o1:
473 *p++ = 0x9210000E;
474 assert(reinterpret_cast< unsigned char * >(p) - code <= codeSnippetSize);
475 return code + codeSnippetSize;
478 } //end of namespace
480 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
482 bridges::cpp_uno::shared::VtableFactory::Slot *
483 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
485 return static_cast< Slot * >(block) + 2;
488 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
489 sal_Int32 slotCount)
491 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
494 bridges::cpp_uno::shared::VtableFactory::Slot *
495 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
496 void * block, sal_Int32 slotCount, sal_Int32,
497 typelib_InterfaceTypeDescription *)
499 Slot * slots = mapBlockToVtable(block);
500 slots[-2].fn = 0; //null
501 slots[-1].fn = 0; //destructor
502 return slots + slotCount;
505 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
506 Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
507 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
508 sal_Int32 functionCount, sal_Int32 vTableOffset)
510 (*slots) -= functionCount;
511 Slot * s = *slots;
512 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
513 typelib_TypeDescription * member = 0;
514 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
515 assert(member != 0);
516 switch (member->eTypeClass) {
517 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
518 // Getter:
519 (s++)->fn = code + writetoexecdiff;
520 code = codeSnippet(
521 code, functionOffset++, vTableOffset,
522 bridges::cpp_uno::shared::isSimpleType(
523 reinterpret_cast<
524 typelib_InterfaceAttributeTypeDescription * >(
525 member)->pAttributeTypeRef));
526 // Setter:
527 if (!reinterpret_cast<
528 typelib_InterfaceAttributeTypeDescription * >(
529 member)->bReadOnly)
531 (s++)->fn = code + writetoexecdiff;
532 code = codeSnippet(code, functionOffset++, vTableOffset, true);
534 break;
536 case typelib_TypeClass_INTERFACE_METHOD:
537 (s++)->fn = code + writetoexecdiff;
538 code = codeSnippet(
539 code, functionOffset++, vTableOffset,
540 bridges::cpp_uno::shared::isSimpleType(
541 reinterpret_cast<
542 typelib_InterfaceMethodTypeDescription * >(
543 member)->pReturnTypeRef));
544 break;
546 default:
547 assert(false);
548 break;
550 TYPELIB_DANGER_RELEASE(member);
552 return code;
555 // use flush code from cc50_solaris_sparc
557 extern "C" void doFlushCode(unsigned long address, unsigned long count);
559 void bridges::cpp_uno::shared::VtableFactory::flushCode(
560 unsigned char const * begin, unsigned char const * end)
562 unsigned long n = end - begin;
563 if (n != 0) {
564 unsigned long adr = reinterpret_cast< unsigned long >(begin);
565 unsigned long off = adr & 7;
566 doFlushCode(adr - off, (n + off + 7) >> 3);
570 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */