bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_sparc / uno2cpp.cxx
blob43b73257261ac304a000d07818c6b39d4c3a5955
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 <sal/config.h>
22 #include <exception>
23 #include <malloc.h>
24 #include <typeinfo>
26 #include <com/sun/star/uno/Exception.hpp>
27 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <com/sun/star/uno/genfunc.hxx>
29 #include <o3tl/runtimetooustring.hxx>
30 #include <uno/data.h>
32 #include "bridge.hxx"
33 #include "types.hxx"
34 #include "unointerfaceproxy.hxx"
35 #include "vtables.hxx"
37 #include "share.hxx"
39 using namespace com::sun::star::uno;
41 namespace
43 // The call instruction within the asm section of callVirtualMethod may throw
44 // exceptions. So that the compiler handles this correctly, it is important
45 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
46 // never happens at runtime), which in turn can throw exceptions, and (b)
47 // callVirtualMethod is not inlined at its call site (so that any exceptions are
48 // caught which are thrown from the instruction calling callVirtualMethod):
50 void callVirtualMethod( void * pAdjustedThisPtr,
51 sal_Int32 nVtableIndex,
52 void * pRegisterReturn,
53 typelib_TypeClass eReturnType,
54 sal_Int32 * pStackLongs,
55 sal_Int32 nStackLongs ) __attribute__((noinline));
57 void callVirtualMethod( void * pAdjustedThisPtr,
58 sal_Int32 /* nVtableIndex */,
59 void * pRegisterReturn,
60 typelib_TypeClass eReturnType,
61 #if OSL_DEBUG_LEVEL > 0
62 sal_Int32 * pStackLongs,
63 sal_Int32 nStackLongs)
64 #else
65 sal_Int32 * /*pStackLongs*/,
66 sal_Int32 /*nStackLongs*/)
67 #endif
69 // parameter list is mixed list of * and values
70 // reference parameters are pointers
72 assert(pStackLongs && pAdjustedThisPtr);
73 static_assert( (sizeof(void *) == 4) &&
74 (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
75 assert(nStackLongs && pStackLongs && "### no stack in callVirtualMethod !");
77 // never called
78 if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
80 long o0, o1; // for register returns
81 double f0d;
82 float f0f;
83 volatile long long saveReg[7];
85 __asm__ (
86 // save registers
87 "std %%l0, [%4]\n\t"
88 "mov %4, %%l0\n\t"
89 "mov %%l0, %%l1\n\t"
90 "add %%l0, 8, %%l0\n\t"
91 "std %%l2, [%%l0]\n\t"
92 "add %%l0, 8, %%l0\n\t"
93 "std %%l4, [%%l0]\n\t"
94 "add %%l0, 8, %%l0\n\t"
95 "std %%o0, [%%l0]\n\t"
96 "add %%l0, 8, %%l0\n\t"
97 "std %%o2, [%%l0]\n\t"
98 "add %%l0, 8, %%l0\n\t"
99 "std %%o4, [%%l0]\n\t"
100 "add %%l0, 8, %%l0\n\t"
101 "std %%l6, [%%l0]\n\t"
102 "mov %%l1, %%l7\n\t"
104 // increase our own stackframe if necessary
105 "mov %%sp, %%l3\n\t" // save stack ptr for readjustment
107 "subcc %%i5, 7, %%l0\n\t"
108 "ble .LmoveOn\n\t"
109 "nop\n\t"
111 "sll %%l0, 2, %%l0\n\t"
112 "add %%l0, 96, %%l0\n\t"
113 "mov %%sp, %%l1\n\t" // old stack ptr
114 "sub %%sp, %%l0, %%l0\n\t" // future stack ptr
115 "andcc %%l0, 7, %%g0\n\t" // align stack to 8
116 "be .LisAligned\n\t"
117 "nop\n\t"
118 "sub %%l0, 4, %%l0\n"
119 ".LisAligned:\n\t"
120 "mov %%l0, %%o5\n\t" // save newly computed stack ptr
121 "add %%g0, 16, %%o4\n"
123 // now copy longs down to save register window
124 // and local variables
125 ".LcopyDown:\n\t"
126 "ld [%%l1], %%l2\n\t"
127 "st %%l2,[%%l0]\n\t"
128 "add %%l0, 4, %%l0\n\t"
129 "add %%l1, 4, %%l1\n\t"
130 "subcc %%o4, 1, %%o4\n\t"
131 "bne .LcopyDown\n\t"
133 "mov %%o5, %%sp\n\t" // move new stack ptr (hopefully) atomically
134 // while register window is valid in both spaces
135 // (scheduling might hit in copyDown loop)
137 "sub %%i5, 7, %%l0\n\t" // copy parameters past the sixth to stack
138 "add %%i4, 28, %%l1\n\t"
139 "add %%sp, 92, %%l2\n"
140 ".LcopyLong:\n\t"
141 "ld [%%l1], %%o0\n\t"
142 "st %%o0, [%%l2]\n\t"
143 "add %%l1, 4, %%l1\n\t"
144 "add %%l2, 4, %%l2\n\t"
145 "subcc %%l0, 1, %%l0\n\t"
146 "bne .LcopyLong\n\t"
147 "nop\n"
149 ".LmoveOn:\n\t"
150 "mov %%i5, %%l0\n\t" // prepare out registers
151 "mov %%i4, %%l1\n\t"
153 "ld [%%l1], %%o0\n\t" // prepare complex return ptr
154 "st %%o0, [%%sp+64]\n\t"
155 "sub %%l0, 1, %%l0\n\t"
156 "add %%l1, 4, %%l1\n\t"
158 "ld [%%l1], %%o0\n\t"
159 "subcc %%l0, 1, %%l0\n\t"
160 "be .LdoCall\n\t"
161 "nop\n\t"
163 "add %%l1, 4, %%l1\n\t"
164 "ld [%%l1], %%o1\n\t"
165 "subcc %%l0, 1, %%l0\n\t"
166 "be .LdoCall\n\t"
167 "nop\n\t"
169 "add %%l1, 4, %%l1\n\t"
170 "ld [%%l1], %%o2\n\t"
171 "subcc %%l0, 1, %%l0\n\t"
172 "be .LdoCall\n\t"
173 "nop\n\t"
175 "add %%l1, 4, %%l1\n\t"
176 "ld [%%l1], %%o3\n\t"
177 "subcc %%l0, 1, %%l0\n\t"
178 "be .LdoCall\n\t"
179 "nop\n\t"
181 "add %%l1, 4, %%l1\n\t"
182 "ld [%%l1], %%o4\n\t"
183 "subcc %%l0, 1, %%l0\n\t"
184 "be .LdoCall\n\t"
185 "nop\n\t"
187 "add %%l1, 4, %%l1\n\t"
188 "ld [%%l1], %%o5\n"
190 ".LdoCall:\n\t"
191 "ld [%%i0], %%l0\n\t" // get vtable ptr
193 "sll %%i1, 2, %%l6\n\t"
194 // "add %%l6, 8, %%l6\n\t"
195 "add %%l6, %%l0, %%l0\n\t"
196 // // vtable has 8byte wide entries,
197 // // upper half contains 2 half words, of which the first
198 // // is the this ptr patch !
199 // // first entry is (or __tf)
201 // "ldsh [%%l0], %%l6\n\t" // load this ptr patch
202 // "add %%l6, %%o0, %%o0\n\t" // patch this ptr
204 // "add %%l0, 4, %%l0\n\t" // get virtual function ptr
205 "ld [%%l0], %%l0\n\t"
207 "ld [%%i4], %%l2\n\t"
208 "subcc %%l2, %%g0, %%l2\n\t"
209 "bne .LcomplexCall\n\t"
210 "nop\n\t"
211 "call %%l0\n\t"
212 "nop\n\t"
213 "ba .LcallReturned\n\t"
214 "nop\n"
215 ".LcomplexCall:\n\t"
216 "call %%l0\n\t"
217 "nop\n\t"
218 "unimp\n"
220 ".LcallReturned:\n\t"
221 "mov %%l3, %%sp\n\t" // readjust stack so that our locals are where they belong
222 "st %%o0, %0\n\t" // save possible return registers into our locals
223 "st %%o1, %1\n\t"
224 "std %%f0, %2\n\t"
225 "st %%f0, %3\n\t"
227 // restore registers
228 "ldd [%%l7], %%l0\n\t"
229 "add %%l7, 8, %%l7\n\t"
230 "ldd [%%l7], %%l2\n\t"
231 "add %%l7, 8, %%l7\n\t"
232 "ldd [%%l7], %%l4\n\t"
233 "add %%l7, 8, %%l7\n\t"
234 "ldd [%%l7], %%o0\n\t"
235 "add %%l7, 8, %%l7\n\t"
236 "ldd [%%l7], %%o2\n\t"
237 "add %%l7, 8, %%l7\n\t"
238 "ldd [%%l7], %%o4\n\t"
239 "add %%l7, 8, %%l7\n\t"
240 "ldd [%%l7], %%l6\n\t"
242 "=m"(o0),
243 "=m"(o1),
244 "=m"(f0d),
245 "=m"(f0f)
247 "r"(&saveReg[0])
249 "memory"
251 switch( eReturnType )
253 case typelib_TypeClass_HYPER:
254 case typelib_TypeClass_UNSIGNED_HYPER:
255 ((long*)pRegisterReturn)[1] = o1;
256 case typelib_TypeClass_LONG:
257 case typelib_TypeClass_UNSIGNED_LONG:
258 case typelib_TypeClass_ENUM:
259 ((long*)pRegisterReturn)[0] = o0;
260 break;
261 case typelib_TypeClass_CHAR:
262 case typelib_TypeClass_SHORT:
263 case typelib_TypeClass_UNSIGNED_SHORT:
264 *(unsigned short*)pRegisterReturn = (unsigned short)o0;
265 break;
266 case typelib_TypeClass_BOOLEAN:
267 case typelib_TypeClass_BYTE:
268 *(unsigned char*)pRegisterReturn = (unsigned char)o0;
269 break;
270 case typelib_TypeClass_FLOAT:
271 *(float*)pRegisterReturn = f0f;
272 break;
273 case typelib_TypeClass_DOUBLE:
274 *(double*)pRegisterReturn = f0d;
275 break;
276 default:
277 break;
281 static void cpp_call(
282 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
283 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
284 typelib_TypeDescriptionReference * pReturnTypeRef,
285 sal_Int32 nParams, typelib_MethodParameter * pParams,
286 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
288 // max space for: complex ret ptr, this, values|ptr ...
289 char * pCppStack =
290 (char *)alloca( (nParams+2) * sizeof(sal_Int64) );
291 char * pCppStackStart = pCppStack;
293 // return
294 typelib_TypeDescription * pReturnTypeDescr = 0;
295 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
296 assert(pReturnTypeDescr);
298 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
300 if (pReturnTypeDescr)
302 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
304 pCppReturn = pUnoReturn; // direct way for simple types
305 *(void**)pCppStack = NULL;
307 else
309 // complex return via ptr
310 pCppReturn = *(void **)pCppStack = (bridges::cpp_uno::shared::relatesToInterfaceType(pReturnTypeDescr )
311 ? alloca( pReturnTypeDescr->nSize )
312 : pUnoReturn); // direct way
314 pCppStack += sizeof(void*);
316 // push this
317 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
318 + aVtableSlot.offset;
319 *(void**)pCppStack = pAdjustedThisPtr;
320 pCppStack += sizeof( void* );
322 // stack space
323 static_assert(sizeof(void *) == sizeof(sal_Int32), "### unexpected size!");
324 // args
325 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
326 // indices of values this have to be converted (interface conversion cpp<=>uno)
327 sal_Int32 * pTempIndices = (sal_Int32 *)(pCppArgs + nParams);
328 // type descriptions for reconversions
329 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
331 sal_Int32 nTempIndices = 0;
333 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
335 const typelib_MethodParameter & rParam = pParams[nPos];
336 typelib_TypeDescription * pParamTypeDescr = 0;
337 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
338 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
340 pCppArgs[ nPos ] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack, pParamTypeDescr );
342 switch (pParamTypeDescr->eTypeClass)
344 case typelib_TypeClass_HYPER:
345 case typelib_TypeClass_UNSIGNED_HYPER:
346 case typelib_TypeClass_DOUBLE:
347 static_assert(sizeof (double) == sizeof (sal_Int64), "boo");
348 *reinterpret_cast< sal_Int32 * >(pCppStack) =
349 *reinterpret_cast< sal_Int32 const * >(pUnoArgs[ nPos ]);
350 pCppStack += sizeof (sal_Int32);
351 *reinterpret_cast< sal_Int32 * >(pCppStack) =
352 *(reinterpret_cast< sal_Int32 const * >(pUnoArgs[ nPos ] ) + 1);
353 break;
354 default:
355 uno_copyAndConvertData(
356 pCppArgs[nPos], pUnoArgs[nPos], pParamTypeDescr,
357 pThis->getBridge()->getUno2Cpp() );
358 break;
360 // no longer needed
361 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
363 else // ptr to complex value | ref
365 if (! rParam.bIn) // is pure out
367 // cpp out is constructed mem, uno out is not!
368 uno_constructData(
369 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
370 pParamTypeDescr );
371 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
372 // will be released at reconversion
373 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
375 // is in/inout
376 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
377 pParamTypeDescr ))
379 uno_copyAndConvertData(
380 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
381 pUnoArgs[nPos], pParamTypeDescr,
382 pThis->getBridge()->getUno2Cpp() );
384 pTempIndices[nTempIndices] = nPos; // has to be reconverted
385 // will be released at reconversion
386 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
388 else // direct way
390 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
391 // no longer needed
392 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
395 pCppStack += sizeof(sal_Int32); // standard parameter length
400 int nStackLongs = (pCppStack - pCppStackStart)/sizeof(sal_Int32);
401 assert( !( (pCppStack - pCppStackStart ) & 3) && "UNALIGNED STACK !!! (Please DO panic" );
403 if( nStackLongs & 1 )
404 // stack has to be 8 byte aligned
405 nStackLongs++;
406 try {
407 callVirtualMethod(
408 pAdjustedThisPtr,
409 aVtableSlot.index,
410 pCppReturn,
411 pReturnTypeDescr->eTypeClass,
412 (sal_Int32 *)pCppStackStart,
413 nStackLongs);
414 } catch (css::uno::Exception &) {
415 throw;
416 } catch (std::exception & e) {
417 throw css::uno::RuntimeException(
418 "C++ code threw " + o3tl::runtimeToOUString(typeid(e).name()) + ": "
419 + o3tl::runtimeToOUString(e.what()));
420 } catch (...) {
421 throw css::uno::RuntimeException("C++ code threw unknown exception");
423 // NO exception occurred...
424 *ppUnoExc = 0;
426 // reconvert temporary params
427 for ( ; nTempIndices--; )
429 sal_Int32 nIndex = pTempIndices[nTempIndices];
430 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
432 if (pParams[nIndex].bIn)
434 if (pParams[nIndex].bOut) // inout
436 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
437 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
438 pThis->getBridge()->getCpp2Uno() );
441 else // pure out
443 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
444 pThis->getBridge()->getCpp2Uno() );
446 // destroy temp cpp param => cpp: every param was constructed
447 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
449 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
451 // return value
452 if (pCppReturn && pUnoReturn != pCppReturn)
454 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
455 pThis->getBridge()->getCpp2Uno() );
456 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
459 catch( ... )
461 // get exception
462 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc, pThis->getBridge()->getCpp2Uno());
464 // temporary params
465 for ( ; nTempIndices--; )
467 sal_Int32 nIndex = pTempIndices[nTempIndices];
468 // destroy temp cpp param => cpp: every param was constructed
469 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndices], cpp_release );
470 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
472 // return type
473 if (pReturnTypeDescr)
474 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
480 namespace bridges { namespace cpp_uno { namespace shared {
482 void unoInterfaceProxyDispatch(
483 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
484 void * pReturn, void * pArgs[], uno_Any ** ppException )
486 #if defined BRIDGES_DEBUG
487 OString cstr( OUStringToOString( pMemberDescr->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
488 fprintf( stderr, "received dispatch( %s )\n", cstr.getStr() );
489 #endif
491 // is my surrogate
492 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
493 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
494 // typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
496 switch (pMemberDescr->eTypeClass)
498 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
500 VtableSlot aVtableSlot(
501 getVtableSlot(
502 reinterpret_cast<
503 typelib_InterfaceAttributeTypeDescription const * >(
504 pMemberDescr)));
505 if (pReturn)
507 // dependent dispatch
508 cpp_call(
509 pThis, aVtableSlot,
510 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
511 0, 0, // no params
512 pReturn, pArgs, ppException );
514 else
516 // is SET
517 typelib_MethodParameter aParam;
518 aParam.pTypeRef =
519 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
520 aParam.bIn = sal_True;
521 aParam.bOut = sal_False;
523 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
524 OUString aVoidName("void");
525 typelib_typedescriptionreference_new(
526 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
528 // dependent dispatch
529 aVtableSlot.index += 1; // get, then set method
530 cpp_call(
531 pThis, aVtableSlot,
532 pReturnTypeRef,
533 1, &aParam,
534 pReturn, pArgs, ppException );
536 typelib_typedescriptionreference_release( pReturnTypeRef );
539 break;
541 case typelib_TypeClass_INTERFACE_METHOD:
543 VtableSlot aVtableSlot(
544 getVtableSlot(
545 reinterpret_cast<
546 typelib_InterfaceMethodTypeDescription const * >(
547 pMemberDescr)));
548 switch (aVtableSlot.index)
550 // standard calls
551 case 1: // acquire uno interface
552 (*pUnoI->acquire)( pUnoI );
553 *ppException = 0;
554 break;
555 case 2: // release uno interface
556 (*pUnoI->release)( pUnoI );
557 *ppException = 0;
558 break;
559 case 0: // queryInterface() opt
561 typelib_TypeDescription * pTD = 0;
562 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
563 if (pTD)
565 uno_Interface * pInterface = 0;
566 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
567 pThis->pBridge->getUnoEnv(),
568 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
570 if (pInterface)
572 ::uno_any_construct(
573 reinterpret_cast< uno_Any * >( pReturn ),
574 &pInterface, pTD, 0 );
575 (*pInterface->release)( pInterface );
576 TYPELIB_DANGER_RELEASE( pTD );
577 *ppException = 0;
578 break;
580 TYPELIB_DANGER_RELEASE( pTD );
582 } // else perform queryInterface()
583 default:
584 // dependent dispatch
585 cpp_call(
586 pThis, aVtableSlot,
587 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
588 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
589 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
590 pReturn, pArgs, ppException );
592 break;
594 default:
596 ::com::sun::star::uno::RuntimeException aExc(
597 "illegal member type description!",
598 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
600 Type const & rExcType = cppu::UnoType<decltype(aExc)>::get();
601 // binary identical null reference
602 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
607 } } }
609 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */