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 .
21 #include <com/sun/star/uno/genfunc.hxx>
22 #include <sal/log.hxx>
24 #include <typelib/typedescription.hxx>
27 #include "cppinterfaceproxy.hxx"
29 #include "vtablefactory.hxx"
36 // "Calling Standard for Alpha Systems"
37 // (Tru64 UNIX Version 5.1 or higher, August 2000)
38 //http://www.tru64unix.compaq.com/docs/base_doc/DOCUMENTATION/V51_HTML/ARH9MBTE/TITLE.HTM
40 using namespace ::com::sun::star::uno
;
44 static typelib_TypeClass
cpp2uno_call(
45 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
46 const typelib_TypeDescription
* pMemberTypeDescr
,
47 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
48 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
49 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
50 sal_Int64
* pRegisterReturn
/* space for register return */ )
52 #if OSL_DEBUG_LEVEL > 2
53 fprintf(stderr
, "as far as cpp2uno_call\n");
55 int nregs
= 0; //number of words passed in registers
57 // gpreg: [ret *], this, [gpr params]
58 // fpreg: [fpr params]
59 // ovrflw: [gpr or fpr params (properly aligned)]
62 typelib_TypeDescription
* pReturnTypeDescr
= 0;
64 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
66 void * pUnoReturn
= 0;
67 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
71 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
73 pUnoReturn
= pRegisterReturn
; // direct way for simple types
75 else // complex return via ptr (pCppReturn)
77 pCppReturn
= *(void **)gpreg
;
82 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
83 ? alloca( pReturnTypeDescr
->nSize
)
84 : pCppReturn
); // direct way
93 static_assert(sizeof(void *) == sizeof(sal_Int64
), "### unexpected size!");
95 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
96 void ** pCppArgs
= pUnoArgs
+ nParams
;
97 // indices of values this have to be converted (interface conversion cpp<=>uno)
98 sal_Int32
* pTempIndices
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
99 // type descriptions for reconversions
100 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
102 sal_Int32 nTempIndices
= 0;
103 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
105 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
106 typelib_TypeDescription
* pParamTypeDescr
= 0;
107 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
109 #if OSL_DEBUG_LEVEL > 2
110 fprintf(stderr
, "arg %d of %d\n", nPos
, nParams
);
113 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
115 #if OSL_DEBUG_LEVEL > 2
116 fprintf(stderr
, "simple type is %d\n", pParamTypeDescr
->eTypeClass
);
119 switch (pParamTypeDescr
->eTypeClass
)
121 case typelib_TypeClass_FLOAT
:
122 case typelib_TypeClass_DOUBLE
:
123 if (nregs
< axp::MAX_WORDS_IN_REGS
)
125 if (pParamTypeDescr
->eTypeClass
== typelib_TypeClass_FLOAT
)
127 float tmp
= (float) (*((double *)fpreg
));
128 (*((float *) fpreg
)) = tmp
;
131 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
;
138 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
142 case typelib_TypeClass_BYTE
:
143 case typelib_TypeClass_BOOLEAN
:
144 if (nregs
< axp::MAX_WORDS_IN_REGS
)
146 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
;
153 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
157 case typelib_TypeClass_CHAR
:
158 case typelib_TypeClass_SHORT
:
159 case typelib_TypeClass_UNSIGNED_SHORT
:
160 if (nregs
< axp::MAX_WORDS_IN_REGS
)
162 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
;
169 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
173 case typelib_TypeClass_ENUM
:
174 case typelib_TypeClass_LONG
:
175 case typelib_TypeClass_UNSIGNED_LONG
:
176 if (nregs
< axp::MAX_WORDS_IN_REGS
)
178 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
;
185 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
190 if (nregs
< axp::MAX_WORDS_IN_REGS
)
192 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
;
199 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
206 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
208 else // ptr to complex value | ref
210 #if OSL_DEBUG_LEVEL > 2
211 fprintf(stderr
, "complex, nregs is %d\n", nregs
);
214 void *pCppStack
; //temporary stack pointer
216 if (nregs
< axp::MAX_WORDS_IN_REGS
)
218 pCppArgs
[nPos
] = pCppStack
= *gpreg
;
225 pCppArgs
[nPos
] = pCppStack
= *ovrflw
;
229 if (! rParam
.bIn
) // is pure out
231 // uno out is unconstructed mem!
232 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
233 pTempIndices
[nTempIndices
] = nPos
;
234 // will be released at reconversion
235 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
238 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
240 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
241 pCppStack
, pParamTypeDescr
,
242 pThis
->getBridge()->getCpp2Uno() );
243 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
244 // will be released at reconversion
245 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
249 pUnoArgs
[nPos
] = pCppStack
;
251 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
256 #if OSL_DEBUG_LEVEL > 2
257 fprintf(stderr
, "end of params\n");
261 uno_Any aUnoExc
; // Any will be constructed by callee
262 uno_Any
* pUnoExc
= &aUnoExc
;
264 // invoke uno dispatch call
265 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
267 // in case an exception occurred...
270 // destruct temporary in/inout params
271 for ( ; nTempIndices
--; )
273 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
275 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
276 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], 0 );
277 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
279 if (pReturnTypeDescr
)
280 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
282 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
284 return typelib_TypeClass_VOID
;
286 else // else no exception occurred...
289 for ( ; nTempIndices
--; )
291 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
292 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
294 if (pParams
[nIndex
].bOut
) // inout/out
296 // convert and assign
297 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
298 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
299 pThis
->getBridge()->getUno2Cpp() );
301 // destroy temp uno param
302 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
304 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
307 if (pCppReturn
) // has complex return
309 if (pUnoReturn
!= pCppReturn
) // needs reconversion
311 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
312 pThis
->getBridge()->getUno2Cpp() );
313 // destroy temp uno return
314 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
316 // complex return ptr is set to return reg
317 *(void **)pRegisterReturn
= pCppReturn
;
319 if (pReturnTypeDescr
)
321 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
322 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
326 return typelib_TypeClass_VOID
;
331 static typelib_TypeClass
cpp_mediate(
332 sal_uInt64 nOffsetAndIndex
,
333 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
334 sal_Int64
* pRegisterReturn
/* space for register return */ )
336 static_assert(sizeof(sal_Int64
)==sizeof(void *), "### unexpected!");
338 sal_Int32 nVtableOffset
= (nOffsetAndIndex
>> 32);
339 sal_Int32 nFunctionIndex
= (nOffsetAndIndex
& 0xFFFFFFFF);
341 #if OSL_DEBUG_LEVEL > 2
342 fprintf(stderr
, "nVTableOffset, nFunctionIndex are %x %x\n", nVtableOffset
, nFunctionIndex
);
345 #if OSL_DEBUG_LEVEL > 2
346 // Let's figure out what is really going on here
348 fprintf( stderr
, "= cpp_mediate () =\nGPR's (%d): ", 6 );
349 for ( unsigned int i
= 0; i
< 6; ++i
)
350 fprintf( stderr
, "0x%lx, ", gpreg
[i
] );
351 fprintf( stderr
, "\n");
352 fprintf( stderr
, "\nFPR's (%d): ", 6 );
353 for ( unsigned int i
= 0; i
< 6; ++i
)
354 fprintf( stderr
, "0x%lx (%f), ", fpreg
[i
], fpreg
[i
] );
355 fprintf( stderr
, "\n");
360 // gpreg: [ret *], this, [other gpr params]
361 // fpreg: [fpr params]
362 // ovrflw: [gpr or fpr params (properly aligned)]
364 // _this_ ptr is patched cppu_XInterfaceProxy object
366 if( nFunctionIndex
& 0x80000000 )
368 nFunctionIndex
&= 0x7fffffff;
376 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
378 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
379 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
382 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
385 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
389 "illegal " << OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
390 << " vtable index " << nFunctionIndex
<< "/"
391 << pTypeDescr
->nMapFunctionIndexToMemberIndex
);
392 throw RuntimeException(
393 ("illegal " + OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
394 + " vtable index " + OUString::number(nFunctionIndex
) + "/"
395 + OUString::number(pTypeDescr
->nMapFunctionIndexToMemberIndex
)),
396 (XInterface
*)pCppI
);
399 // determine called method
400 assert(nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
);
401 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
402 assert(nMemberPos
< pTypeDescr
->nAllMembers
);
404 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
406 typelib_TypeClass eRet
;
407 switch (aMemberDescr
.get()->eTypeClass
)
409 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
411 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
415 pCppI
, aMemberDescr
.get(),
416 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
418 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
423 typelib_MethodParameter aParam
;
425 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
426 aParam
.bIn
= sal_True
;
427 aParam
.bOut
= sal_False
;
430 pCppI
, aMemberDescr
.get(),
431 0, // indicates void return
433 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
437 case typelib_TypeClass_INTERFACE_METHOD
:
440 switch (nFunctionIndex
)
443 pCppI
->acquireProxy(); // non virtual call!
444 eRet
= typelib_TypeClass_VOID
;
447 pCppI
->releaseProxy(); // non virtual call!
448 eRet
= typelib_TypeClass_VOID
;
450 case 0: // queryInterface() opt
452 typelib_TypeDescription
* pTD
= 0;
453 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
456 XInterface
* pInterface
= 0;
457 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
458 pCppI
->getBridge()->getCppEnv(),
459 (void **)&pInterface
, pCppI
->getOid().pData
,
460 (typelib_InterfaceTypeDescription
*)pTD
);
465 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
466 &pInterface
, pTD
, cpp_acquire
);
467 pInterface
->release();
468 TYPELIB_DANGER_RELEASE( pTD
);
469 *(void **)pRegisterReturn
= gpreg
[0];
470 eRet
= typelib_TypeClass_ANY
;
473 TYPELIB_DANGER_RELEASE( pTD
);
475 } // else perform queryInterface()
478 pCppI
, aMemberDescr
.get(),
479 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
480 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
481 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
482 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
488 throw RuntimeException( "no member description found!", (XInterface
*)pCppI
);
495 long cpp_vtable_call(long r16
, long r17
, long r18
, long r19
, long r20
, long r21
, long firstonstack
)
497 register long r1
asm("$1");
498 sal_uInt64 nOffsetAndIndex
= r1
;
500 long sp
= (long)&firstonstack
;
502 sal_uInt64 gpreg
[axp::MAX_GPR_REGS
];
510 double fpreg
[axp::MAX_SSE_REGS
];
511 register double f16
asm("$f16"); fpreg
[0] = f16
;
512 register double f17
asm("$f17"); fpreg
[1] = f17
;
513 register double f18
asm("$f18"); fpreg
[2] = f18
;
514 register double f19
asm("$f19"); fpreg
[3] = f19
;
515 register double f20
asm("$f20"); fpreg
[4] = f20
;
516 register double f21
asm("$f21"); fpreg
[5] = f21
;
518 volatile long nRegReturn
[1];
519 #if OSL_DEBUG_LEVEL > 2
520 fprintf(stderr
, "before mediate with %lx\n",nOffsetAndIndex
);
521 fprintf(stderr
, "non-doubles are %x %x %x %x %x %x\n", gpreg
[0], gpreg
[1], gpreg
[2], gpreg
[3], gpreg
[4], gpreg
[5]);
522 fprintf(stderr
, "doubles are %f %f %f %f %f %f\n", fpreg
[0], fpreg
[1], fpreg
[2], fpreg
[3], fpreg
[4], fpreg
[5]);
524 typelib_TypeClass aType
=
525 cpp_mediate( nOffsetAndIndex
, (void**)gpreg
, (void**)fpreg
, (void**)sp
,
526 (sal_Int64
*)nRegReturn
);
527 #if OSL_DEBUG_LEVEL > 2
528 fprintf(stderr
, "after mediate ret is %lx %ld\n", nRegReturn
[0], nRegReturn
[0]);
533 case typelib_TypeClass_BOOLEAN
:
534 case typelib_TypeClass_BYTE
:
535 nRegReturn
[0] = (unsigned long)(*(unsigned char *)nRegReturn
);
537 case typelib_TypeClass_CHAR
:
538 case typelib_TypeClass_UNSIGNED_SHORT
:
539 case typelib_TypeClass_SHORT
:
540 nRegReturn
[0] = (unsigned long)(*(unsigned short *)nRegReturn
);
542 case typelib_TypeClass_ENUM
:
543 case typelib_TypeClass_UNSIGNED_LONG
:
544 case typelib_TypeClass_LONG
:
545 nRegReturn
[0] = (unsigned long)(*(unsigned int *)nRegReturn
);
547 case typelib_TypeClass_VOID
:
550 case typelib_TypeClass_FLOAT
:
552 double tmp
= (double) (*((float *)nRegReturn
));
553 (*((double *) nRegReturn
)) = tmp
;
555 //deliberate fall through
556 case typelib_TypeClass_DOUBLE
:
557 __asm__ ( "ldt $f0,%0\n\t"
558 : : "m" (*((double*)nRegReturn
)) : "$f0");
561 return nRegReturn
[0];
564 const int codeSnippetSize
= 32;
566 unsigned char *codeSnippet( unsigned char * code
, sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
, bool simple_ret_type
)
568 if (! simple_ret_type
)
569 nFunctionIndex
|= 0x80000000;
571 unsigned char * p
= code
;
572 *(unsigned int*)&p
[0] = 0x47fb0401; /* mov $27,$1 */
573 *(unsigned int*)&p
[4] = 0xa43b0010; /* ldq $1,16($27) */
574 *(unsigned int*)&p
[8] = 0xa77b0018; /* ldq $27,24($27) */
575 *(unsigned int*)&p
[12] = 0x6bfb0000; /* jmp $31,($27),0 */
576 *(unsigned int*)&p
[16] = nFunctionIndex
;
577 *(unsigned int*)&p
[20] = nVtableOffset
;
578 *(unsigned long*)&p
[24] = (unsigned long)cpp_vtable_call
;
579 return (code
+ codeSnippetSize
);
583 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
585 //http://www.gnu.org/software/lightning/manual/html_node/Standard-functions.html
586 __asm__
__volatile__("call_pal 0x86");
589 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void const * fn
; };
591 bridges::cpp_uno::shared::VtableFactory::Slot
*
592 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
594 return static_cast< Slot
* >(block
) + 2;
597 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
600 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
604 // Some dummy type whose RTTI is used in the synthesized proxy vtables to make uses of dynamic_cast
605 // on such proxy objects not crash:
609 bridges::cpp_uno::shared::VtableFactory::Slot
*
610 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
611 void * block
, sal_Int32 slotCount
, sal_Int32
,
612 typelib_InterfaceTypeDescription
*)
614 Slot
* slots
= mapBlockToVtable(block
);
616 slots
[-1].fn
= &typeid(ProxyRtti
);
617 return slots
+ slotCount
;
620 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
621 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
622 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
623 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
625 (*slots
) -= functionCount
;
627 #if OSL_DEBUG_LEVEL > 2
628 fprintf(stderr
, "in addLocalFunctions functionOffset is %x\n",functionOffset
);
629 fprintf(stderr
, "in addLocalFunctions vtableOffset is %x\n",vtableOffset
);
632 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
633 typelib_TypeDescription
* member
= 0;
634 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
636 switch (member
->eTypeClass
) {
637 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
639 (s
++)->fn
= code
+ writetoexecdiff
;
641 code
, functionOffset
++, vtableOffset
,
642 bridges::cpp_uno::shared::isSimpleType(
644 typelib_InterfaceAttributeTypeDescription
* >(
645 member
)->pAttributeTypeRef
));
648 if (!reinterpret_cast<
649 typelib_InterfaceAttributeTypeDescription
* >(
652 (s
++)->fn
= code
+ writetoexecdiff
;
653 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
657 case typelib_TypeClass_INTERFACE_METHOD
:
658 (s
++)->fn
= code
+ writetoexecdiff
;
660 code
, functionOffset
++, vtableOffset
,
661 bridges::cpp_uno::shared::isSimpleType(
663 typelib_InterfaceMethodTypeDescription
* >(
664 member
)->pReturnTypeRef
));
671 TYPELIB_DANGER_RELEASE(member
);
676 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */