bump product version to 6.3.0.0.beta1
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_aix_powerpc / cpp2uno.cxx
blob2e6fdd11a8201fcfb4429c65c70c88f21702013b
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 <uno/data.h>
23 #include <typelib/typedescription.hxx>
25 #include "bridge.hxx"
26 #include "cppinterfaceproxy.hxx"
27 #include "types.hxx"
28 #include "vtablefactory.hxx"
30 #include "share.hxx"
32 #include <string.h>
34 using namespace ::com::sun::star::uno;
36 namespace
40 static typelib_TypeClass cpp2uno_call(
41 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
42 const typelib_TypeDescription * pMemberTypeDescr,
43 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
44 sal_Int32 nParams, typelib_MethodParameter * pParams,
45 void ** gpreg, double * fpreg, void ** ovrflw,
46 sal_uInt64 * pRegisterReturn /* space for register return */ )
48 // gpreg: [ret *], this, [gpr params]
49 // fpreg: [fpr params]
50 // ovrflw: [remainder of params]
52 // return
53 typelib_TypeDescription * pReturnTypeDescr = 0;
54 if (pReturnTypeRef)
55 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
57 void * pUnoReturn = 0;
58 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
60 sal_Int32 ng = 0;
61 sal_Int32 nf = 0;
63 ovrflw -= ppc::MAX_GPR_REGS;
65 if (pReturnTypeDescr)
67 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
68 pUnoReturn = pRegisterReturn; // direct way for simple types
69 else // complex return via ptr (pCppReturn)
71 pCppReturn = *gpreg;
72 ++gpreg;
73 ++ng;
74 ++ovrflw;
76 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
77 ? __builtin_alloca( pReturnTypeDescr->nSize )
78 : pCppReturn); // direct way
81 // pop this
82 ++gpreg;
83 ++ng;
84 ++ovrflw;
86 // stack space
87 static_assert(sizeof(void *) == sizeof(sal_Int32), "### unexpected size!");
88 // parameters
89 void ** pUnoArgs = (void **)__builtin_alloca( 4 * sizeof(void *) * nParams );
90 void ** pCppArgs = pUnoArgs + nParams;
92 // indices of values this have to be converted (interface conversion cpp<=>uno)
93 sal_Int32 * pTempIndices = (sal_Int32 *)(pUnoArgs + (2 * nParams));
95 // type descriptions for reconversions
96 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
98 sal_Int32 nTempIndices = 0;
100 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
102 const typelib_MethodParameter & rParam = pParams[nPos];
103 typelib_TypeDescription * pParamTypeDescr = 0;
104 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
106 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
107 // value
109 switch (pParamTypeDescr->eTypeClass)
111 case typelib_TypeClass_BOOLEAN:
112 case typelib_TypeClass_BYTE:
113 if (ng < ppc::MAX_GPR_REGS)
115 *ovrflw = *gpreg++;
116 ++ng;
118 pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + (sizeof(void*)-1));
119 ++ovrflw;
120 break;
121 case typelib_TypeClass_CHAR:
122 case typelib_TypeClass_SHORT:
123 case typelib_TypeClass_UNSIGNED_SHORT:
124 if (ng < ppc::MAX_GPR_REGS)
126 *ovrflw = *gpreg++;
127 ++ng;
129 pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + (sizeof(void*)-2));
130 ++ovrflw;
131 break;
132 case typelib_TypeClass_HYPER:
133 case typelib_TypeClass_UNSIGNED_HYPER:
134 pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
135 for (int i = 0; i < 2; ++i)
137 if (ng < ppc::MAX_GPR_REGS)
139 *ovrflw = *gpreg++;
140 ++ng;
142 ++ovrflw;
144 break;
145 case typelib_TypeClass_DOUBLE:
146 if (nf < ppc::MAX_SSE_REGS)
148 pCppArgs[nPos] = pUnoArgs[nPos] = fpreg;
149 ++fpreg;
150 ++nf;
152 else
154 pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
156 if (ng < ppc::MAX_GPR_REGS)
158 ng+=2;
159 gpreg+=2;
161 ovrflw+=2;
162 break;
163 case typelib_TypeClass_FLOAT:
164 if (nf < ppc::MAX_SSE_REGS)
166 pCppArgs[nPos] = pUnoArgs[nPos] = fpreg;
167 ++fpreg;
168 ++nf;
170 else
172 pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
174 if (ng < ppc::MAX_GPR_REGS)
176 ++gpreg;
177 ++ng;
179 ++ovrflw;
180 break;
181 default:
182 if (ng < ppc::MAX_GPR_REGS)
184 *ovrflw = *gpreg++;
185 ++ng;
187 pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
188 ++ovrflw;
189 break;
192 // no longer needed
193 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
195 else // ptr to complex value | ref
197 void *pCppStack; //temporary stack pointer
199 if (ng < ppc::MAX_GPR_REGS)
201 *ovrflw = *gpreg++;
202 ++ng;
204 pCppArgs[nPos] = pCppStack = *ovrflw++;
206 if (! rParam.bIn) // is pure out
208 // uno out is unconstructed mem!
209 pUnoArgs[nPos] = __builtin_alloca( pParamTypeDescr->nSize );
210 pTempIndices[nTempIndices] = nPos;
211 // will be released at reconversion
212 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
214 // is in/inout
215 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
217 uno_copyAndConvertData( pUnoArgs[nPos] = __builtin_alloca( pParamTypeDescr->nSize ),
218 pCppStack, pParamTypeDescr,
219 pThis->getBridge()->getCpp2Uno() );
220 pTempIndices[nTempIndices] = nPos; // has to be reconverted
221 // will be released at reconversion
222 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
224 else // direct way
226 pUnoArgs[nPos] = pCppStack;
227 // no longer needed
228 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
233 // ExceptionHolder
234 uno_Any aUnoExc; // Any will be constructed by callee
235 uno_Any * pUnoExc = &aUnoExc;
237 // invoke uno dispatch call
238 (*pThis->getUnoI()->pDispatcher)(
239 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
241 // in case an exception occurred...
242 if (pUnoExc)
244 // destruct temporary in/inout params
245 for ( ; nTempIndices--; )
247 sal_Int32 nIndex = pTempIndices[nTempIndices];
249 if (pParams[nIndex].bIn) // is in/inout => was constructed
250 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndices], 0 );
251 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
253 if (pReturnTypeDescr)
254 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
256 CPPU_CURRENT_NAMESPACE::raiseException(
257 &aUnoExc, pThis->getBridge()->getUno2Cpp() );
258 // has to destruct the any
259 // is here for dummy
260 return typelib_TypeClass_VOID;
262 else // else no exception occurred...
264 // temporary params
265 for ( ; nTempIndices--; )
267 sal_Int32 nIndex = pTempIndices[nTempIndices];
268 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
270 if (pParams[nIndex].bOut) // inout/out
272 // convert and assign
273 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
274 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
275 pThis->getBridge()->getUno2Cpp() );
277 // destroy temp uno param
278 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
280 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
282 // return
283 if (pCppReturn) // has complex return
285 if (pUnoReturn != pCppReturn) // needs reconversion
287 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
288 pThis->getBridge()->getUno2Cpp() );
289 // destroy temp uno return
290 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
292 // complex return ptr is set to return reg
293 *(void **)pRegisterReturn = pCppReturn;
295 if (pReturnTypeDescr)
297 typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
298 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
299 return eRet;
301 else
302 return typelib_TypeClass_VOID;
307 static typelib_TypeClass cpp_mediate(
308 sal_Int32 nFunctionIndex,
309 sal_Int32 nVtableOffset,
310 void ** gpreg, double * fpreg, void ** ovrflw,
311 sal_uInt64 * pRegisterReturn /* space for register return */ )
313 static_assert(sizeof(sal_Int32)==sizeof(void *), "### unexpected!");
315 // gpreg: [ret *], this, [other gpr params]
316 // fpreg: [fpr params]
317 // ovrflw: [gpr or fpr params (in space allocated for all params properly aligned)]
319 void * pThis;
320 if( nFunctionIndex & 0x8000 )
322 nFunctionIndex &= 0x7fff;
323 pThis = gpreg[1];
325 else
327 pThis = gpreg[0];
330 pThis = static_cast< char * >(pThis) - nVtableOffset;
332 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
333 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(pThis);
335 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
337 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
339 SAL_WARN(
340 "bridges",
341 "illegal " << OUString::unacquired(&pTypeDescr->aBase.pTypeName)
342 << " vtable index " << nFunctionIndex << "/"
343 << pTypeDescr->nMapFunctionIndexToMemberIndex);
344 throw RuntimeException(
345 ("illegal " + OUString::unacquired(&pTypeDescr->aBase.pTypeName)
346 + " vtable index " + OUString::number(nFunctionIndex) + "/"
347 + OUString::number(pTypeDescr->nMapFunctionIndexToMemberIndex)),
348 (XInterface *)pThis);
351 // determine called method
352 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
353 assert(nMemberPos < pTypeDescr->nAllMembers);
355 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
357 typelib_TypeClass eRet;
358 switch (aMemberDescr.get()->eTypeClass)
360 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
362 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
364 // is GET method
365 eRet = cpp2uno_call(
366 pCppI, aMemberDescr.get(),
367 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
368 0, 0, // no params
369 gpreg, fpreg, ovrflw, pRegisterReturn );
371 else
373 // is SET method
374 typelib_MethodParameter aParam;
375 aParam.pTypeRef =
376 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
377 aParam.bIn = sal_True;
378 aParam.bOut = sal_False;
380 eRet = cpp2uno_call(
381 pCppI, aMemberDescr.get(),
382 0, // indicates void return
383 1, &aParam,
384 gpreg, fpreg, ovrflw, pRegisterReturn );
386 break;
388 case typelib_TypeClass_INTERFACE_METHOD:
390 // is METHOD
391 switch (nFunctionIndex)
393 case 1: // acquire()
394 pCppI->acquireProxy(); // non virtual call!
395 eRet = typelib_TypeClass_VOID;
396 break;
397 case 2: // release()
398 pCppI->releaseProxy(); // non virtual call!
399 eRet = typelib_TypeClass_VOID;
400 break;
401 case 0: // queryInterface() opt
403 typelib_TypeDescription * pTD = 0;
404 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( gpreg[2] )->getTypeLibType() );
405 if (pTD)
407 XInterface * pInterface = 0;
408 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
409 pCppI->getBridge()->getCppEnv(),
410 (void **)&pInterface, pCppI->getOid().pData, (typelib_InterfaceTypeDescription *)pTD );
412 if (pInterface)
414 ::uno_any_construct(
415 reinterpret_cast< uno_Any * >( gpreg[0] ),
416 &pInterface, pTD, cpp_acquire );
417 pInterface->release();
418 TYPELIB_DANGER_RELEASE( pTD );
419 *(void **)pRegisterReturn = gpreg[0];
420 eRet = typelib_TypeClass_ANY;
421 break;
423 TYPELIB_DANGER_RELEASE( pTD );
425 } // else perform queryInterface()
426 default:
427 eRet = cpp2uno_call(
428 pCppI, aMemberDescr.get(),
429 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
430 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
431 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
432 gpreg, fpreg, ovrflw, pRegisterReturn );
434 break;
436 default:
438 throw RuntimeException( "no member description found!", (XInterface *)pThis );
442 return eRet;
447 * is called on incoming vtable calls
448 * (called by asm snippets)
450 static sal_uInt64 cpp_vtable_call(sal_Int32 r3, sal_Int32 r4, sal_Int32 r5,
451 sal_Int32 r6, sal_Int32 r7, sal_Int32 r8, sal_Int32 r9,
452 sal_Int32 r10, sal_Int32 firstonstack)
454 volatile unsigned long nOffsetAndIndex;
456 __asm__ __volatile__(
457 "mr %0, 11\n\t"
458 : "=r" (nOffsetAndIndex) : );
460 sal_Int32 nVtableOffset = (nOffsetAndIndex >> 16);
461 sal_Int32 nFunctionIndex = (nOffsetAndIndex & 0xFFFF);
463 void ** ovrflw = (void**)&firstonstack;
465 sal_Int32 gpreg[ppc::MAX_GPR_REGS];
466 gpreg[0] = r3;
467 gpreg[1] = r4;
468 gpreg[2] = r5;
469 gpreg[3] = r6;
470 gpreg[4] = r7;
471 gpreg[5] = r8;
472 gpreg[6] = r9;
473 gpreg[7] = r10;
475 double fpreg[ppc::MAX_SSE_REGS];
476 register double d0 asm("fr1"); fpreg[0] = d0;
477 register double d1 asm("fr2"); fpreg[1] = d1;
478 register double d2 asm("fr3"); fpreg[2] = d2;
479 register double d3 asm("fr4"); fpreg[3] = d3;
480 register double d4 asm("fr5"); fpreg[4] = d4;
481 register double d5 asm("fr6"); fpreg[5] = d5;
482 register double d6 asm("fr7"); fpreg[6] = d6;
483 register double d7 asm("fr8"); fpreg[7] = d7;
484 register double d8 asm("fr9"); fpreg[8] = d8;
485 register double d9 asm("fr10"); fpreg[9] = d9;
486 register double d10 asm("fr11"); fpreg[10] = d10;
487 register double d11 asm("fr12"); fpreg[11] = d11;
488 register double d12 asm("fr13"); fpreg[12] = d12;
490 #if OSL_DEBUG_LEVEL > 2
491 for(int i = 0; i < 8; ++i)
493 fprintf(stderr, "general reg %d is %x\n", i, gpreg[i]);
495 for(int i = 0; i < 13; ++i)
497 fprintf(stderr, "sse reg %d is %f\n", i, fpreg[i]);
498 fprintf(stderr, "sse reg %d is %llx\n", i, fpreg[i]);
500 for(int i = -8; i < 8; ++i)
502 fprintf(stderr, "overflow arg %d is %x\n", i, ovrflw[i]);
504 #endif
505 sal_uInt64 nRegReturn=0;
507 typelib_TypeClass aType =
508 cpp_mediate( nFunctionIndex, nVtableOffset, (void**)gpreg, fpreg, ovrflw, &nRegReturn );
510 sal_uInt32 *pRegReturn = (sal_uInt32*)&nRegReturn;
511 switch( aType )
513 case typelib_TypeClass_BOOLEAN:
514 pRegReturn[0] = (sal_uInt32)(*(char *)pRegReturn);
515 break;
516 case typelib_TypeClass_BYTE:
517 pRegReturn[0] = (sal_Int32)(*(unsigned char *)pRegReturn);
518 break;
519 case typelib_TypeClass_SHORT:
520 pRegReturn[0] = (sal_Int32)(*(short *)pRegReturn);
521 break;
522 case typelib_TypeClass_CHAR:
523 case typelib_TypeClass_UNSIGNED_SHORT:
524 pRegReturn[0] = (sal_uInt32)(*(unsigned short *)pRegReturn);
525 break;
526 case typelib_TypeClass_FLOAT:
527 __asm__("lfs 1,%0\n\t" : : "m"(*((float*)&nRegReturn)));
528 break;
529 case typelib_TypeClass_DOUBLE:
530 __asm__("lfd 1,%0\n\t" : : "m"(*((double*)&nRegReturn)));
531 break;
532 case typelib_TypeClass_HYPER:
533 case typelib_TypeClass_UNSIGNED_HYPER:
534 break;
535 default:
536 pRegReturn[0] = (sal_uInt32)(*(unsigned int*)pRegReturn);
537 break;
539 return nRegReturn;
543 int const codeSnippetSize = 3 * sizeof(void*);
545 unsigned char * codeSnippet( unsigned char * code, sal_Int16 functionIndex,
546 sal_Int16 vtableOffset, bool simpleRetType )
548 sal_uInt32 nOffsetAndIndex = ( ( vtableOffset ) << 16 ) | (functionIndex );
549 if (! simpleRetType )
550 nOffsetAndIndex |= 0x8000;
552 void **raw = (void**)&code[0];
553 memcpy(raw, (char*)cpp_vtable_call, 2 * sizeof(void*));
554 raw[2] = (void*)nOffsetAndIndex;
556 return (code + codeSnippetSize);
561 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const * bptr, unsigned char const * eptr)
563 int const lineSize = 32;
564 for (unsigned char const * p = bptr; p < eptr + lineSize; p += lineSize) {
565 __asm__ volatile ("dcbst 0, %0" : : "r"(p) : "memory");
567 __asm__ volatile ("sync" : : : "memory");
568 for (unsigned char const * p = bptr; p < eptr + lineSize; p += lineSize) {
569 __asm__ volatile ("icbi 0, %0" : : "r"(p) : "memory");
571 __asm__ volatile ("isync" : : : "memory");
574 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
576 bridges::cpp_uno::shared::VtableFactory::Slot *
577 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
579 return static_cast< Slot * >(block) + 2;
582 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
583 sal_Int32 slotCount)
585 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
588 bridges::cpp_uno::shared::VtableFactory::Slot *
589 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
590 void * block, sal_Int32 slotCount, sal_Int32,
591 typelib_InterfaceTypeDescription *)
593 Slot * slots = mapBlockToVtable(block);
594 slots[-2].fn = 0;
595 slots[-1].fn = 0;
596 return slots + slotCount;
599 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
600 Slot ** slots, unsigned char * code,
601 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
602 sal_Int32 functionCount, sal_Int32 vtableOffset)
604 (*slots) -= functionCount;
605 Slot * s = *slots;
607 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
608 typelib_TypeDescription * member = 0;
609 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
610 assert(member != 0);
611 switch (member->eTypeClass) {
612 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
613 // Getter:
614 (s++)->fn = code;
615 code = codeSnippet(
616 code, functionOffset++, vtableOffset,
617 bridges::cpp_uno::shared::isSimpleType(
618 reinterpret_cast<
619 typelib_InterfaceAttributeTypeDescription * >(
620 member)->pAttributeTypeRef));
622 // Setter:
623 if (!reinterpret_cast<
624 typelib_InterfaceAttributeTypeDescription * >(
625 member)->bReadOnly)
627 (s++)->fn = code;
628 code = codeSnippet(code, functionOffset++, vtableOffset, true);
630 break;
632 case typelib_TypeClass_INTERFACE_METHOD:
633 (s++)->fn = code;
634 code = codeSnippet(
635 code, functionOffset++, vtableOffset,
636 bridges::cpp_uno::shared::isSimpleType(
637 reinterpret_cast<
638 typelib_InterfaceMethodTypeDescription * >(
639 member)->pReturnTypeRef));
640 break;
642 default:
643 assert(false);
644 break;
646 TYPELIB_DANGER_RELEASE(member);
648 return code;
651 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */