1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cpp2uno.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
36 #include <com/sun/star/uno/genfunc.hxx>
38 #include <typelib/typedescription.hxx>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
49 using namespace ::com::sun::star::uno
;
54 //==================================================================================================
55 static typelib_TypeClass
cpp2uno_call(
56 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
57 const typelib_TypeDescription
* pMemberTypeDescr
,
58 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
59 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
60 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
61 sal_Int64
* pRegisterReturn
/* space for register return */ )
63 int ng
= 0; //number of gpr registers used
64 int nf
= 0; //number of fpr regsiters used
65 void ** pCppStack
; //temporary stack pointer
67 // gpreg: [ret *], this, [gpr params]
68 // fpreg: [fpr params]
69 // ovrflw: [gpr or fpr params (properly aligned)]
72 typelib_TypeDescription
* pReturnTypeDescr
= 0;
74 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
76 void * pUnoReturn
= 0;
77 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
81 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
83 pUnoReturn
= pRegisterReturn
; // direct way for simple types
85 else // complex return via ptr (pCppReturn)
87 pCppReturn
= *(void **)gpreg
;
91 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
92 ? alloca( pReturnTypeDescr
->nSize
)
93 : pCppReturn
); // direct way
101 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
103 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
104 void ** pCppArgs
= pUnoArgs
+ nParams
;
105 // indizes of values this have to be converted (interface conversion cpp<=>uno)
106 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
107 // type descriptions for reconversions
108 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
110 sal_Int32 nTempIndizes
= 0;
112 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
114 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
115 typelib_TypeDescription
* pParamTypeDescr
= 0;
116 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
118 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
122 switch (pParamTypeDescr
->eTypeClass
)
125 case typelib_TypeClass_DOUBLE
:
127 pCppArgs
[nPos
] = fpreg
;
128 pUnoArgs
[nPos
] = fpreg
;
132 if (((long)ovrflw
) & 4) ovrflw
++;
133 pCppArgs
[nPos
] = ovrflw
;
134 pUnoArgs
[nPos
] = ovrflw
;
139 case typelib_TypeClass_FLOAT
:
140 // fpreg are all double values so need to
141 // modify fpreg to be a single word float value
143 float tmp
= (float) (*((double *)fpreg
));
144 (*((float *) fpreg
)) = tmp
;
145 pCppArgs
[nPos
] = fpreg
;
146 pUnoArgs
[nPos
] = fpreg
;
150 #if 0 /* abi is not being followed correctly */
151 if (((long)ovrflw
) & 4) ovrflw
++;
152 float tmp
= (float) (*((double *)ovrflw
));
153 (*((float *) ovrflw
)) = tmp
;
154 pCppArgs
[nPos
] = ovrflw
;
155 pUnoArgs
[nPos
] = ovrflw
;
158 pCppArgs
[nPos
] = ovrflw
;
159 pUnoArgs
[nPos
] = ovrflw
;
165 case typelib_TypeClass_HYPER
:
166 case typelib_TypeClass_UNSIGNED_HYPER
:
172 pCppArgs
[nPos
] = gpreg
;
173 pUnoArgs
[nPos
] = gpreg
;
177 if (((long)ovrflw
) & 4) ovrflw
++;
178 pCppArgs
[nPos
] = ovrflw
;
179 pUnoArgs
[nPos
] = ovrflw
;
184 case typelib_TypeClass_BYTE
:
185 case typelib_TypeClass_BOOLEAN
:
187 pCppArgs
[nPos
] = (((char *)gpreg
) + 3);
188 pUnoArgs
[nPos
] = (((char *)gpreg
) + 3);
192 pCppArgs
[nPos
] = (((char *)ovrflw
) + 3);
193 pUnoArgs
[nPos
] = (((char *)ovrflw
) + 3);
199 case typelib_TypeClass_CHAR
:
200 case typelib_TypeClass_SHORT
:
201 case typelib_TypeClass_UNSIGNED_SHORT
:
203 pCppArgs
[nPos
] = (((char *)gpreg
)+ 2);
204 pUnoArgs
[nPos
] = (((char *)gpreg
)+ 2);
208 pCppArgs
[nPos
] = (((char *)ovrflw
) + 2);
209 pUnoArgs
[nPos
] = (((char *)ovrflw
) + 2);
217 pCppArgs
[nPos
] = gpreg
;
218 pUnoArgs
[nPos
] = gpreg
;
222 pCppArgs
[nPos
] = ovrflw
;
223 pUnoArgs
[nPos
] = ovrflw
;
230 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
232 else // ptr to complex value | ref
236 pCppArgs
[nPos
] = *(void **)gpreg
;
241 pCppArgs
[nPos
] = *(void **)ovrflw
;
246 if (! rParam
.bIn
) // is pure out
248 // uno out is unconstructed mem!
249 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
250 pTempIndizes
[nTempIndizes
] = nPos
;
251 // will be released at reconversion
252 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
255 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
257 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
258 *(void **)pCppStack
, pParamTypeDescr
,
259 pThis
->getBridge()->getCpp2Uno() );
260 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
261 // will be released at reconversion
262 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
266 pUnoArgs
[nPos
] = *(void **)pCppStack
;
268 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
274 uno_Any aUnoExc
; // Any will be constructed by callee
275 uno_Any
* pUnoExc
= &aUnoExc
;
277 // invoke uno dispatch call
278 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
280 // in case an exception occured...
283 // destruct temporary in/inout params
284 for ( ; nTempIndizes
--; )
286 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
288 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
289 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
290 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
292 if (pReturnTypeDescr
)
293 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
295 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
296 // has to destruct the any
298 return typelib_TypeClass_VOID
;
300 else // else no exception occured...
303 for ( ; nTempIndizes
--; )
305 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
306 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
308 if (pParams
[nIndex
].bOut
) // inout/out
310 // convert and assign
311 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
312 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
313 pThis
->getBridge()->getUno2Cpp() );
315 // destroy temp uno param
316 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
318 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
321 if (pCppReturn
) // has complex return
323 if (pUnoReturn
!= pCppReturn
) // needs reconversion
325 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
326 pThis
->getBridge()->getUno2Cpp() );
327 // destroy temp uno return
328 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
330 // complex return ptr is set to return reg
331 *(void **)pRegisterReturn
= pCppReturn
;
333 if (pReturnTypeDescr
)
335 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
336 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
340 return typelib_TypeClass_VOID
;
345 //==================================================================================================
346 static typelib_TypeClass
cpp_mediate(
347 sal_Int32 nFunctionIndex
,
348 sal_Int32 nVtableOffset
,
349 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
350 sal_Int64
* pRegisterReturn
/* space for register return */ )
352 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
354 // gpreg: [ret *], this, [other gpr params]
355 // fpreg: [fpr params]
356 // ovrflw: [gpr or fpr params (properly aligned)]
359 if (nFunctionIndex
& 0x80000000 )
361 nFunctionIndex
&= 0x7fffffff;
369 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
370 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
371 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
374 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
376 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
377 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
379 throw RuntimeException(
380 rtl::OUString::createFromAscii("illegal vtable index!"),
381 (XInterface
*)pThis
);
384 // determine called method
385 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
386 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
388 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
390 typelib_TypeClass eRet
;
391 switch (aMemberDescr
.get()->eTypeClass
)
393 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
395 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
399 pCppI
, aMemberDescr
.get(),
400 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
402 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
407 typelib_MethodParameter aParam
;
409 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
410 aParam
.bIn
= sal_True
;
411 aParam
.bOut
= sal_False
;
414 pCppI
, aMemberDescr
.get(),
415 0, // indicates void return
417 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
421 case typelib_TypeClass_INTERFACE_METHOD
:
424 switch (nFunctionIndex
)
427 pCppI
->acquireProxy(); // non virtual call!
428 eRet
= typelib_TypeClass_VOID
;
431 pCppI
->releaseProxy(); // non virtual call!
432 eRet
= typelib_TypeClass_VOID
;
434 case 0: // queryInterface() opt
436 typelib_TypeDescription
* pTD
= 0;
437 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
440 XInterface
* pInterface
= 0;
441 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
442 pCppI
->getBridge()->getCppEnv(),
443 (void **)&pInterface
, pCppI
->getOid().pData
,
444 (typelib_InterfaceTypeDescription
*)pTD
);
449 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
450 &pInterface
, pTD
, cpp_acquire
);
451 pInterface
->release();
452 TYPELIB_DANGER_RELEASE( pTD
);
453 *(void **)pRegisterReturn
= gpreg
[0];
454 eRet
= typelib_TypeClass_ANY
;
457 TYPELIB_DANGER_RELEASE( pTD
);
459 } // else perform queryInterface()
462 pCppI
, aMemberDescr
.get(),
463 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
464 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
465 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
466 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
472 throw RuntimeException(
473 rtl::OUString::createFromAscii("no member description found!"),
474 (XInterface
*)pThis
);
476 eRet
= typelib_TypeClass_VOID
;
483 //==================================================================================================
485 * is called on incoming vtable calls
486 * (called by asm snippets)
488 static void cpp_vtable_call( int nFunctionIndex
, int nVtableOffset
, void** gpregptr
, void** fpregptr
, void** ovrflw
)
493 memcpy( gpreg
, gpregptr
, 32);
494 memcpy( fpreg
, fpregptr
, 64);
496 volatile long nRegReturn
[2];
498 // fprintf(stderr,"in cpp_vtable_call nFunctionIndex is %x\n",nFunctionIndex);
499 // fprintf(stderr,"in cpp_vtable_call nVtableOffset is %x\n",nVtableOffset);
502 sal_Bool bComplex
= nFunctionIndex
& 0x80000000 ? sal_True
: sal_False
;
504 typelib_TypeClass aType
=
505 cpp_mediate( nFunctionIndex
, nVtableOffset
, (void**)gpreg
, (void**)fpreg
, ovrflw
, (sal_Int64
*)nRegReturn
);
510 // move return value into register space
511 // (will be loaded by machine code snippet)
513 case typelib_TypeClass_BOOLEAN
:
514 case typelib_TypeClass_BYTE
:
515 __asm__( "lbz 3,%0\n\t" : :
516 "m"(nRegReturn
[0]) );
519 case typelib_TypeClass_CHAR
:
520 case typelib_TypeClass_SHORT
:
521 case typelib_TypeClass_UNSIGNED_SHORT
:
522 __asm__( "lhz 3,%0\n\t" : :
523 "m"(nRegReturn
[0]) );
526 case typelib_TypeClass_FLOAT
:
527 __asm__( "lfs 1,%0\n\t" : :
528 "m" (*((float*)nRegReturn
)) );
531 case typelib_TypeClass_DOUBLE
:
532 __asm__( "lfd 1,%0\n\t" : :
533 "m" (*((double*)nRegReturn
)) );
536 case typelib_TypeClass_HYPER
:
537 case typelib_TypeClass_UNSIGNED_HYPER
:
538 __asm__( "lwz 4,%0\n\t" : :
539 "m"(nRegReturn
[1]) ); // fall through
542 __asm__( "lwz 3,%0\n\t" : :
543 "m"(nRegReturn
[0]) );
549 int const codeSnippetSize
= 108;
551 unsigned char * codeSnippet( unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
555 // fprintf(stderr,"in codeSnippet functionIndex is %x\n", functionIndex);
556 // fprintf(stderr,"in codeSnippet vtableOffset is %x\n", vtableOffset);
559 if (! simpleRetType
)
560 functionIndex
|= 0x80000000;
562 unsigned long * p
= (unsigned long *) code
;
564 // OSL_ASSERT( sizeof (long) == 4 );
565 OSL_ASSERT((((unsigned long)code
) & 0x3) == 0 ); //aligned to 4 otherwise a mistake
567 /* generate this code */
568 // # so first save gpr 3 to gpr 10 (aligned to 4)
579 // # next save fpr 1 to fpr 8 (aligned to 8)
589 // # now here is where cpp_vtable_call must go
594 // # now load up the functionIndex
598 // # now load up the vtableOffset
602 // #now load up the pointer to the saved gpr registers
605 // #now load up the pointer to the saved fpr registers
608 // #now load up the pointer to the overflow call stack
628 * p
++ = 0x3c600000 | (((unsigned long)cpp_vtable_call
) >> 16);
629 * p
++ = 0x60630000 | (((unsigned long)cpp_vtable_call
) & 0x0000FFFF);
631 * p
++ = 0x3c600000 | (((unsigned long)functionIndex
) >> 16);
632 * p
++ = 0x60630000 | (((unsigned long)functionIndex
) & 0x0000FFFF);
633 * p
++ = 0x3c800000 | (((unsigned long)vtableOffset
) >> 16);
634 * p
++ = 0x60840000 | (((unsigned long)vtableOffset
) & 0x0000FFFF);
639 return (code
+ codeSnippetSize
);
646 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const * bptr
, unsigned char const * eptr
)
648 int const lineSize
= 32;
649 for (unsigned char const * p
= bptr
; p
< eptr
+ lineSize
; p
+= lineSize
) {
650 __asm__
volatile ("dcbst 0, %0" : : "r"(p
) : "memory");
652 __asm__
volatile ("sync" : : : "memory");
653 for (unsigned char const * p
= bptr
; p
< eptr
+ lineSize
; p
+= lineSize
) {
654 __asm__
volatile ("icbi 0, %0" : : "r"(p
) : "memory");
656 __asm__
volatile ("isync" : : : "memory");
659 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
661 bridges::cpp_uno::shared::VtableFactory::Slot
*
662 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
664 return static_cast< Slot
* >(block
) + 2;
667 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
670 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
673 bridges::cpp_uno::shared::VtableFactory::Slot
*
674 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
675 void * block
, sal_Int32 slotCount
)
677 Slot
* slots
= mapBlockToVtable(block
);
680 return slots
+ slotCount
;
683 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
684 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
685 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
686 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
688 (*slots
) -= functionCount
;
690 // fprintf(stderr, "in addLocalFunctions functionOffset is %x\n",functionOffset);
691 // fprintf(stderr, "in addLocalFunctions vtableOffset is %x\n",vtableOffset);
694 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
695 typelib_TypeDescription
* member
= 0;
696 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
697 OSL_ASSERT(member
!= 0);
698 switch (member
->eTypeClass
) {
699 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
701 (s
++)->fn
= code
+ writetoexecdiff
;
703 code
, functionOffset
++, vtableOffset
,
704 bridges::cpp_uno::shared::isSimpleType(
706 typelib_InterfaceAttributeTypeDescription
* >(
707 member
)->pAttributeTypeRef
));
710 if (!reinterpret_cast<
711 typelib_InterfaceAttributeTypeDescription
* >(
714 (s
++)->fn
= code
+ writetoexecdiff
;
715 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
719 case typelib_TypeClass_INTERFACE_METHOD
:
720 (s
++)->fn
= code
+ writetoexecdiff
;
722 code
, functionOffset
++, vtableOffset
,
723 bridges::cpp_uno::shared::isSimpleType(
725 typelib_InterfaceMethodTypeDescription
* >(
726 member
)->pReturnTypeRef
));
733 TYPELIB_DANGER_RELEASE(member
);