1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <com/sun/star/uno/genfunc.hxx>
32 #include <typelib/typedescription.hxx>
34 #include "bridges/cpp_uno/shared/bridge.hxx"
35 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
36 #include "bridges/cpp_uno/shared/types.hxx"
37 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
43 using namespace ::com::sun::star::uno
;
48 static typelib_TypeClass
cpp2uno_call(
49 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
50 const typelib_TypeDescription
* pMemberTypeDescr
,
51 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
52 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
53 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
54 sal_Int64
* pRegisterReturn
/* space for register return */ )
56 int ng
= 0; //number of gpr registers used
57 int nf
= 0; //number of fpr regsiters used
58 void ** pCppStack
; //temporary stack pointer
60 // gpreg: [ret *], this, [gpr params]
61 // fpreg: [fpr params]
62 // ovrflw: [gpr or fpr params (properly aligned)]
65 typelib_TypeDescription
* pReturnTypeDescr
= 0;
67 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
69 void * pUnoReturn
= 0;
70 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
74 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
76 pUnoReturn
= pRegisterReturn
; // direct way for simple types
78 else // complex return via ptr (pCppReturn)
80 pCppReturn
= *(void **)gpreg
;
84 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
85 ? alloca( pReturnTypeDescr
->nSize
)
86 : pCppReturn
); // direct way
94 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
96 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
97 void ** pCppArgs
= pUnoArgs
+ nParams
;
98 // indizes of values this have to be converted (interface conversion cpp<=>uno)
99 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
100 // type descriptions for reconversions
101 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
103 sal_Int32 nTempIndizes
= 0;
105 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
107 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
108 typelib_TypeDescription
* pParamTypeDescr
= 0;
109 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
111 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
114 switch (pParamTypeDescr
->eTypeClass
)
117 case typelib_TypeClass_DOUBLE
:
119 pCppArgs
[nPos
] = fpreg
;
120 pUnoArgs
[nPos
] = fpreg
;
124 pCppArgs
[nPos
] = ovrflw
;
125 pUnoArgs
[nPos
] = ovrflw
;
130 case typelib_TypeClass_FLOAT
:
131 // fpreg are all double values so need to
132 // modify fpreg to be a single word float value
134 // float tmp = (float) (*((double *)fpreg));
135 // (*((float *) fpreg)) = tmp;
136 pCppArgs
[nPos
] = fpreg
;
137 pUnoArgs
[nPos
] = fpreg
;
141 pCppArgs
[nPos
] = ovrflw
;
142 pUnoArgs
[nPos
] = ovrflw
;
147 case typelib_TypeClass_HYPER
:
148 case typelib_TypeClass_UNSIGNED_HYPER
:
150 pCppArgs
[nPos
] = gpreg
;
151 pUnoArgs
[nPos
] = gpreg
;
155 pCppArgs
[nPos
] = ovrflw
;
156 pUnoArgs
[nPos
] = ovrflw
;
161 case typelib_TypeClass_BYTE
:
162 case typelib_TypeClass_BOOLEAN
:
164 pCppArgs
[nPos
] = (((char *)gpreg
) + 3);
165 pUnoArgs
[nPos
] = (((char *)gpreg
) + 3);
169 pCppArgs
[nPos
] = (((char *)ovrflw
) + 3);
170 pUnoArgs
[nPos
] = (((char *)ovrflw
) + 3);
176 case typelib_TypeClass_CHAR
:
177 case typelib_TypeClass_SHORT
:
178 case typelib_TypeClass_UNSIGNED_SHORT
:
180 pCppArgs
[nPos
] = (((char *)gpreg
)+ 2);
181 pUnoArgs
[nPos
] = (((char *)gpreg
)+ 2);
185 pCppArgs
[nPos
] = (((char *)ovrflw
) + 2);
186 pUnoArgs
[nPos
] = (((char *)ovrflw
) + 2);
194 pCppArgs
[nPos
] = gpreg
;
195 pUnoArgs
[nPos
] = gpreg
;
199 pCppArgs
[nPos
] = ovrflw
;
200 pUnoArgs
[nPos
] = ovrflw
;
207 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
209 else // ptr to complex value | ref
213 pCppArgs
[nPos
] = *(void **)gpreg
;
218 pCppArgs
[nPos
] = *(void **)ovrflw
;
223 if (! rParam
.bIn
) // is pure out
225 // uno out is unconstructed mem!
226 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
227 pTempIndizes
[nTempIndizes
] = nPos
;
228 // will be released at reconversion
229 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
232 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
234 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
235 *(void **)pCppStack
, pParamTypeDescr
,
236 pThis
->getBridge()->getCpp2Uno() );
237 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
238 // will be released at reconversion
239 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
243 pUnoArgs
[nPos
] = *(void **)pCppStack
;
245 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
251 uno_Any aUnoExc
; // Any will be constructed by callee
252 uno_Any
* pUnoExc
= &aUnoExc
;
254 // invoke uno dispatch call
255 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
257 // in case an exception occurred...
260 // destruct temporary in/inout params
261 for ( ; nTempIndizes
--; )
263 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
265 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
266 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
267 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
269 if (pReturnTypeDescr
)
270 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
272 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
274 return typelib_TypeClass_VOID
;
276 else // else no exception occurred...
279 for ( ; nTempIndizes
--; )
281 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
282 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
284 if (pParams
[nIndex
].bOut
) // inout/out
286 // convert and assign
287 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
288 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
289 pThis
->getBridge()->getUno2Cpp() );
291 // destroy temp uno param
292 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
294 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
297 if (pCppReturn
) // has complex return
299 if (pUnoReturn
!= pCppReturn
) // needs reconversion
301 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
302 pThis
->getBridge()->getUno2Cpp() );
303 // destroy temp uno return
304 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
306 // complex return ptr is set to return reg
307 *(void **)pRegisterReturn
= pCppReturn
;
309 if (pReturnTypeDescr
)
311 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
312 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
316 return typelib_TypeClass_VOID
;
321 //============================================================================
322 static typelib_TypeClass
cpp_mediate(
323 sal_uInt32 nOffsetAndIndex
,
324 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
325 sal_Int64
* pRegisterReturn
/* space for register return */ )
327 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
329 sal_Int16 nVtableOffset
= (nOffsetAndIndex
>> 16);
330 sal_Int16 nFunctionIndex
= (nOffsetAndIndex
& 0xFFFF);
332 // gpreg: [ret *], this, [other gpr params]
333 // fpreg: [fpr params]
334 // ovrflw: [gpr or fpr params (properly aligned)]
336 // _this_ ptr is patched cppu_XInterfaceProxy object
338 if( nFunctionIndex
& 0x8000 )
340 nFunctionIndex
&= 0x7fff;
348 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
350 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
351 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
354 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
357 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
358 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
360 throw RuntimeException(
361 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal vtable index!" )),
362 (XInterface
*)pCppI
);
365 // determine called method
366 OSL_ENSURE( nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
367 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
368 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
370 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
372 typelib_TypeClass eRet
;
373 switch (aMemberDescr
.get()->eTypeClass
)
375 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
377 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
381 pCppI
, aMemberDescr
.get(),
382 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
384 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
389 typelib_MethodParameter aParam
;
391 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
392 aParam
.bIn
= sal_True
;
393 aParam
.bOut
= sal_False
;
396 pCppI
, aMemberDescr
.get(),
397 0, // indicates void return
399 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
403 case typelib_TypeClass_INTERFACE_METHOD
:
406 switch (nFunctionIndex
)
409 pCppI
->acquireProxy(); // non virtual call!
410 eRet
= typelib_TypeClass_VOID
;
413 pCppI
->releaseProxy(); // non virtual call!
414 eRet
= typelib_TypeClass_VOID
;
416 case 0: // queryInterface() opt
418 typelib_TypeDescription
* pTD
= 0;
419 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
422 XInterface
* pInterface
= 0;
423 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
424 pCppI
->getBridge()->getCppEnv(),
425 (void **)&pInterface
, pCppI
->getOid().pData
,
426 (typelib_InterfaceTypeDescription
*)pTD
);
431 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
432 &pInterface
, pTD
, cpp_acquire
);
433 pInterface
->release();
434 TYPELIB_DANGER_RELEASE( pTD
);
435 *(void **)pRegisterReturn
= gpreg
[0];
436 eRet
= typelib_TypeClass_ANY
;
439 TYPELIB_DANGER_RELEASE( pTD
);
441 } // else perform queryInterface()
444 pCppI
, aMemberDescr
.get(),
445 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
446 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
447 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
448 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
454 throw RuntimeException(
455 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no member description found!" )),
456 (XInterface
*)pCppI
);
463 //==================================================================================================
465 * is called on incoming vtable calls
466 * (called by asm snippets)
468 static void privateSnippetExecutor( sal_uInt32 nOffsetAndIndex
, void** gpregptr
, void** fpregptr
, void** ovrflw
)
470 #if OSL_DEBUG_LEVEL > 2
471 fprintf(stderr
, "privateSnippetExecutor\n");
478 memcpy( gpreg
, gpregptr
, 32);
479 memcpy( fpreg
, fpregptr
, 64);
481 volatile long nRegReturn
[2];
483 typelib_TypeClass aType
=
484 cpp_mediate( nOffsetAndIndex
, (void**)gpreg
, (void**)fpreg
, ovrflw
,
485 (sal_Int64
*)nRegReturn
);
490 // move return value into register space
491 // (will be loaded by machine code snippet)
493 case typelib_TypeClass_BOOLEAN
:
495 unsigned long tmp
= (unsigned long)(*(unsigned char *)nRegReturn
);
496 __asm__
volatile ( "l 2,%0\n\t" : :
500 case typelib_TypeClass_BYTE
:
502 long tmp
= (long)(*(signed char *)nRegReturn
);
503 __asm__
volatile ( "l 2,%0\n\t" : :
507 case typelib_TypeClass_CHAR
:
508 case typelib_TypeClass_UNSIGNED_SHORT
:
510 unsigned long tmp
= (unsigned long)(*(unsigned short *)nRegReturn
);
511 __asm__
volatile ( "l 2,%0\n\t" : :
515 case typelib_TypeClass_SHORT
:
517 long tmp
= (long)(*(short *)nRegReturn
);
518 __asm__
volatile ( "l 2,%0\n\t" : :
522 case typelib_TypeClass_FLOAT
:
523 __asm__
volatile ( "le 0,%0\n\t" : :
524 "m" (*((float*)nRegReturn
)) : "16" );
527 case typelib_TypeClass_DOUBLE
:
528 __asm__
volatile ( "ld 0,%0\n\t" : :
529 "m" (*((double*)nRegReturn
)) : "16" );
532 case typelib_TypeClass_HYPER
:
533 case typelib_TypeClass_UNSIGNED_HYPER
:
534 __asm__
volatile ( "lm 2,3,%0\n\t" : :
535 "m"(nRegReturn
[0]) : "2", "3" );
539 __asm__
volatile ( "l 2,%0\n\t" : :
540 "m"(nRegReturn
[0]) : "2" );
545 const int codeSnippetSize
= 50;
547 unsigned char* codeSnippet( unsigned char * code
, sal_Int16 nFunctionIndex
, sal_Int16 nVtableOffset
, bool simple_ret_type
)
549 sal_uInt32 nOffsetAndIndex
= ( ( nVtableOffset
) << 16 ) | ( nFunctionIndex
);
551 if (! simple_ret_type
)
552 nOffsetAndIndex
|= 0x8000;
554 OSL_ASSERT( sizeof (long) == 4 );
556 /* generate this code */
559 // .long privateSnippetExecutor
560 // .long nOffsetAndIndex
561 // stm %r2,%r6,8(%r15)
573 unsigned char * p
= code
;
574 *reinterpret_cast< sal_Int16
* >(p
) = 0x180d;
575 p
+= sizeof(sal_Int16
);
576 *reinterpret_cast< sal_Int32
* >(p
) = 0xa7d50006;
577 p
+= sizeof(sal_Int32
);
578 *reinterpret_cast< sal_Int32
* >(p
) =
579 reinterpret_cast< sal_Int32
>(privateSnippetExecutor
);
580 p
+= sizeof(sal_Int32
);
581 *reinterpret_cast< sal_Int32
* >(p
) = nOffsetAndIndex
;
582 p
+= sizeof(sal_Int32
);
583 *reinterpret_cast< sal_Int32
* >(p
) = 0x9026f008;
584 p
+= sizeof(sal_Int32
);
585 *reinterpret_cast< sal_Int32
* >(p
) = 0x6000f040;
586 p
+= sizeof(sal_Int32
);
587 *reinterpret_cast< sal_Int32
* >(p
) = 0x6020f048;
588 p
+= sizeof(sal_Int32
);
589 *reinterpret_cast< sal_Int32
* >(p
) = 0x5820d004;
590 p
+= sizeof(sal_Int32
);
591 *reinterpret_cast< sal_Int32
* >(p
) = 0x4130f008;
592 p
+= sizeof(sal_Int32
);
593 *reinterpret_cast< sal_Int32
* >(p
) = 0x4140f040;
594 p
+= sizeof(sal_Int32
);
595 *reinterpret_cast< sal_Int32
* >(p
) = 0x4150f060;
596 p
+= sizeof(sal_Int32
);
597 *reinterpret_cast< sal_Int32
* >(p
) = 0x5810d000;
598 p
+= sizeof(sal_Int32
);
599 *reinterpret_cast< sal_Int16
* >(p
) = 0x18d0;
600 p
+= sizeof(sal_Int16
);
601 *reinterpret_cast< sal_Int16
* >(p
) = 0x07f1;
602 p
+= sizeof(sal_Int16
);
604 return (code
+ codeSnippetSize
);
608 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
612 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
614 bridges::cpp_uno::shared::VtableFactory::Slot
*
615 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
617 return static_cast< Slot
* >(block
) + 2;
620 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
623 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
626 bridges::cpp_uno::shared::VtableFactory::Slot
*
627 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
628 void * block
, sal_Int32 slotCount
)
630 Slot
* slots
= mapBlockToVtable(block
);
633 return slots
+ slotCount
;
636 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
637 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
638 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
639 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
641 (*slots
) -= functionCount
;
643 #if OSL_DEBUG_LEVEL > 2
644 fprintf(stderr
, "in addLocalFunctions functionOffset is %x\n",functionOffset
);
645 fprintf(stderr
, "in addLocalFunctions vtableOffset is %x\n",vtableOffset
);
648 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
649 typelib_TypeDescription
* member
= 0;
650 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
651 OSL_ASSERT(member
!= 0);
652 switch (member
->eTypeClass
) {
653 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
655 (s
++)->fn
= code
+ writetoexecdiff
;
657 code
, functionOffset
++, vtableOffset
,
658 bridges::cpp_uno::shared::isSimpleType(
660 typelib_InterfaceAttributeTypeDescription
* >(
661 member
)->pAttributeTypeRef
));
664 if (!reinterpret_cast<
665 typelib_InterfaceAttributeTypeDescription
* >(
668 (s
++)->fn
= code
+ writetoexecdiff
;
669 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
673 case typelib_TypeClass_INTERFACE_METHOD
:
674 (s
++)->fn
= code
+ writetoexecdiff
;
676 code
, functionOffset
++, vtableOffset
,
677 bridges::cpp_uno::shared::isSimpleType(
679 typelib_InterfaceMethodTypeDescription
* >(
680 member
)->pReturnTypeRef
));
687 TYPELIB_DANGER_RELEASE(member
);
692 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */