1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <typelib/typedescription.hxx>
26 #include "cppinterfaceproxy.hxx"
28 #include "vtablefactory.hxx"
34 using namespace ::com::sun::star::uno
;
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]
53 typelib_TypeDescription
* pReturnTypeDescr
= 0;
55 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
57 void * pUnoReturn
= 0;
58 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
63 ovrflw
-= ppc::MAX_GPR_REGS
;
67 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
68 pUnoReturn
= pRegisterReturn
; // direct way for simple types
69 else // complex return via ptr (pCppReturn)
76 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
77 ? __builtin_alloca( pReturnTypeDescr
->nSize
)
78 : pCppReturn
); // direct way
87 static_assert(sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!");
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
))
109 switch (pParamTypeDescr
->eTypeClass
)
111 case typelib_TypeClass_BOOLEAN
:
112 case typelib_TypeClass_BYTE
:
113 if (ng
< ppc::MAX_GPR_REGS
)
118 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-1));
121 case typelib_TypeClass_CHAR
:
122 case typelib_TypeClass_SHORT
:
123 case typelib_TypeClass_UNSIGNED_SHORT
:
124 if (ng
< ppc::MAX_GPR_REGS
)
129 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-2));
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
)
145 case typelib_TypeClass_DOUBLE
:
146 if (nf
< ppc::MAX_SSE_REGS
)
148 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
;
154 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
156 if (ng
< ppc::MAX_GPR_REGS
)
163 case typelib_TypeClass_FLOAT
:
164 if (nf
< ppc::MAX_SSE_REGS
)
166 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
;
172 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
174 if (ng
< ppc::MAX_GPR_REGS
)
182 if (ng
< ppc::MAX_GPR_REGS
)
187 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
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
)
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
;
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
;
226 pUnoArgs
[nPos
] = pCppStack
;
228 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
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...
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
260 return typelib_TypeClass_VOID
;
262 else // else no exception occurred...
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
);
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
);
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)]
320 if( nFunctionIndex
& 0x8000 )
322 nFunctionIndex
&= 0x7fff;
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
)
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
)
366 pCppI
, aMemberDescr
.get(),
367 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
369 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
374 typelib_MethodParameter aParam
;
376 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
377 aParam
.bIn
= sal_True
;
378 aParam
.bOut
= sal_False
;
381 pCppI
, aMemberDescr
.get(),
382 0, // indicates void return
384 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
388 case typelib_TypeClass_INTERFACE_METHOD
:
391 switch (nFunctionIndex
)
394 pCppI
->acquireProxy(); // non virtual call!
395 eRet
= typelib_TypeClass_VOID
;
398 pCppI
->releaseProxy(); // non virtual call!
399 eRet
= typelib_TypeClass_VOID
;
401 case 0: // queryInterface() opt
403 typelib_TypeDescription
* pTD
= 0;
404 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
407 XInterface
* pInterface
= 0;
408 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
409 pCppI
->getBridge()->getCppEnv(),
410 (void **)&pInterface
, pCppI
->getOid().pData
, (typelib_InterfaceTypeDescription
*)pTD
);
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
;
423 TYPELIB_DANGER_RELEASE( pTD
);
425 } // else perform queryInterface()
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
);
438 throw RuntimeException( "no member description found!", (XInterface
*)pThis
);
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__(
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
];
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
]);
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
;
513 case typelib_TypeClass_BOOLEAN
:
514 pRegReturn
[0] = (sal_uInt32
)(*(char *)pRegReturn
);
516 case typelib_TypeClass_BYTE
:
517 pRegReturn
[0] = (sal_Int32
)(*(unsigned char *)pRegReturn
);
519 case typelib_TypeClass_SHORT
:
520 pRegReturn
[0] = (sal_Int32
)(*(short *)pRegReturn
);
522 case typelib_TypeClass_CHAR
:
523 case typelib_TypeClass_UNSIGNED_SHORT
:
524 pRegReturn
[0] = (sal_uInt32
)(*(unsigned short *)pRegReturn
);
526 case typelib_TypeClass_FLOAT
:
527 __asm__("lfs 1,%0\n\t" : : "m"(*((float*)&nRegReturn
)));
529 case typelib_TypeClass_DOUBLE
:
530 __asm__("lfd 1,%0\n\t" : : "m"(*((double*)&nRegReturn
)));
532 case typelib_TypeClass_HYPER
:
533 case typelib_TypeClass_UNSIGNED_HYPER
:
536 pRegReturn
[0] = (sal_uInt32
)(*(unsigned int*)pRegReturn
);
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(
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
);
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
;
607 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
608 typelib_TypeDescription
* member
= 0;
609 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
611 switch (member
->eTypeClass
) {
612 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
616 code
, functionOffset
++, vtableOffset
,
617 bridges::cpp_uno::shared::isSimpleType(
619 typelib_InterfaceAttributeTypeDescription
* >(
620 member
)->pAttributeTypeRef
));
623 if (!reinterpret_cast<
624 typelib_InterfaceAttributeTypeDescription
* >(
628 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
632 case typelib_TypeClass_INTERFACE_METHOD
:
635 code
, functionOffset
++, vtableOffset
,
636 bridges::cpp_uno::shared::isSimpleType(
638 typelib_InterfaceMethodTypeDescription
* >(
639 member
)->pReturnTypeRef
));
646 TYPELIB_DANGER_RELEASE(member
);
651 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */