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"
34 #include <com/sun/star/uno/genfunc.hxx>
36 #include <typelib/typedescription.hxx>
38 #include "bridges/cpp_uno/shared/bridge.hxx"
39 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
40 #include "bridges/cpp_uno/shared/types.hxx"
41 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
47 using namespace ::com::sun::star::uno
;
52 static typelib_TypeClass
cpp2uno_call(
53 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
54 const typelib_TypeDescription
* pMemberTypeDescr
,
55 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
56 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
57 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
58 sal_Int64
* pRegisterReturn
/* space for register return */ )
60 int ng
= 0; //number of gpr registers used
61 int nf
= 0; //number of fpr regsiters used
62 void ** pCppStack
; //temporary stack pointer
64 // gpreg: [ret *], this, [gpr params]
65 // fpreg: [fpr params]
66 // ovrflw: [gpr or fpr params (properly aligned)]
69 typelib_TypeDescription
* pReturnTypeDescr
= 0;
71 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
73 void * pUnoReturn
= 0;
74 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
78 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
80 pUnoReturn
= pRegisterReturn
; // direct way for simple types
82 else // complex return via ptr (pCppReturn)
84 pCppReturn
= *(void **)gpreg
;
88 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
89 ? alloca( pReturnTypeDescr
->nSize
)
90 : pCppReturn
); // direct way
98 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!" );
100 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
101 void ** pCppArgs
= pUnoArgs
+ nParams
;
102 // indizes of values this have to be converted (interface conversion cpp<=>uno)
103 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
104 // type descriptions for reconversions
105 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
107 sal_Int32 nTempIndizes
= 0;
109 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
111 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
112 typelib_TypeDescription
* pParamTypeDescr
= 0;
113 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
115 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
118 switch (pParamTypeDescr
->eTypeClass
)
121 case typelib_TypeClass_DOUBLE
:
123 pCppArgs
[nPos
] = fpreg
;
124 pUnoArgs
[nPos
] = fpreg
;
128 pCppArgs
[nPos
] = ovrflw
;
129 pUnoArgs
[nPos
] = ovrflw
;
134 case typelib_TypeClass_FLOAT
:
135 // fpreg are all double values so need to
136 // modify fpreg to be a single word float value
138 // float tmp = (float) (*((double *)fpreg));
139 // (*((float *) fpreg)) = tmp;
140 pCppArgs
[nPos
] = fpreg
;
141 pUnoArgs
[nPos
] = fpreg
;
145 pCppArgs
[nPos
] = ovrflw
;
146 pUnoArgs
[nPos
] = ovrflw
;
151 case typelib_TypeClass_HYPER
:
152 case typelib_TypeClass_UNSIGNED_HYPER
:
154 pCppArgs
[nPos
] = gpreg
;
155 pUnoArgs
[nPos
] = gpreg
;
159 pCppArgs
[nPos
] = ovrflw
;
160 pUnoArgs
[nPos
] = ovrflw
;
165 case typelib_TypeClass_BYTE
:
166 case typelib_TypeClass_BOOLEAN
:
168 pCppArgs
[nPos
] = (((char *)gpreg
) + 3);
169 pUnoArgs
[nPos
] = (((char *)gpreg
) + 3);
173 pCppArgs
[nPos
] = (((char *)ovrflw
) + 3);
174 pUnoArgs
[nPos
] = (((char *)ovrflw
) + 3);
180 case typelib_TypeClass_CHAR
:
181 case typelib_TypeClass_SHORT
:
182 case typelib_TypeClass_UNSIGNED_SHORT
:
184 pCppArgs
[nPos
] = (((char *)gpreg
)+ 2);
185 pUnoArgs
[nPos
] = (((char *)gpreg
)+ 2);
189 pCppArgs
[nPos
] = (((char *)ovrflw
) + 2);
190 pUnoArgs
[nPos
] = (((char *)ovrflw
) + 2);
198 pCppArgs
[nPos
] = gpreg
;
199 pUnoArgs
[nPos
] = gpreg
;
203 pCppArgs
[nPos
] = ovrflw
;
204 pUnoArgs
[nPos
] = ovrflw
;
211 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
213 else // ptr to complex value | ref
217 pCppArgs
[nPos
] = *(void **)gpreg
;
222 pCppArgs
[nPos
] = *(void **)ovrflw
;
227 if (! rParam
.bIn
) // is pure out
229 // uno out is unconstructed mem!
230 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
231 pTempIndizes
[nTempIndizes
] = nPos
;
232 // will be released at reconversion
233 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
236 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
238 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
239 *(void **)pCppStack
, pParamTypeDescr
,
240 pThis
->getBridge()->getCpp2Uno() );
241 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
242 // will be released at reconversion
243 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
247 pUnoArgs
[nPos
] = *(void **)pCppStack
;
249 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
255 uno_Any aUnoExc
; // Any will be constructed by callee
256 uno_Any
* pUnoExc
= &aUnoExc
;
258 // invoke uno dispatch call
259 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
261 // in case an exception occured...
264 // destruct temporary in/inout params
265 for ( ; nTempIndizes
--; )
267 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
269 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
270 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
271 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
273 if (pReturnTypeDescr
)
274 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
276 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
278 return typelib_TypeClass_VOID
;
280 else // else no exception occured...
283 for ( ; nTempIndizes
--; )
285 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
286 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
288 if (pParams
[nIndex
].bOut
) // inout/out
290 // convert and assign
291 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
292 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
293 pThis
->getBridge()->getUno2Cpp() );
295 // destroy temp uno param
296 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
298 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
301 if (pCppReturn
) // has complex return
303 if (pUnoReturn
!= pCppReturn
) // needs reconversion
305 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
306 pThis
->getBridge()->getUno2Cpp() );
307 // destroy temp uno return
308 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
310 // complex return ptr is set to return reg
311 *(void **)pRegisterReturn
= pCppReturn
;
313 if (pReturnTypeDescr
)
315 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
316 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
320 return typelib_TypeClass_VOID
;
325 //============================================================================
326 static typelib_TypeClass
cpp_mediate(
327 sal_uInt32 nOffsetAndIndex
,
328 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
329 sal_Int64
* pRegisterReturn
/* space for register return */ )
331 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
333 sal_Int16 nVtableOffset
= (nOffsetAndIndex
>> 16);
334 sal_Int16 nFunctionIndex
= (nOffsetAndIndex
& 0xFFFF);
336 // gpreg: [ret *], this, [other gpr params]
337 // fpreg: [fpr params]
338 // ovrflw: [gpr or fpr params (properly aligned)]
340 // _this_ ptr is patched cppu_XInterfaceProxy object
342 if( nFunctionIndex
& 0x8000 )
344 nFunctionIndex
&= 0x7fff;
352 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
354 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
355 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
358 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
361 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
362 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
364 throw RuntimeException(
365 rtl::OUString::createFromAscii("illegal vtable index!"),
366 (XInterface
*)pCppI
);
369 // determine called method
370 OSL_ENSURE( nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
371 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
372 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
374 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
376 typelib_TypeClass eRet
;
377 switch (aMemberDescr
.get()->eTypeClass
)
379 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
381 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
385 pCppI
, aMemberDescr
.get(),
386 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
388 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
393 typelib_MethodParameter aParam
;
395 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
396 aParam
.bIn
= sal_True
;
397 aParam
.bOut
= sal_False
;
400 pCppI
, aMemberDescr
.get(),
401 0, // indicates void return
403 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
407 case typelib_TypeClass_INTERFACE_METHOD
:
410 switch (nFunctionIndex
)
413 pCppI
->acquireProxy(); // non virtual call!
414 eRet
= typelib_TypeClass_VOID
;
417 pCppI
->releaseProxy(); // non virtual call!
418 eRet
= typelib_TypeClass_VOID
;
420 case 0: // queryInterface() opt
422 typelib_TypeDescription
* pTD
= 0;
423 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
426 XInterface
* pInterface
= 0;
427 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
428 pCppI
->getBridge()->getCppEnv(),
429 (void **)&pInterface
, pCppI
->getOid().pData
,
430 (typelib_InterfaceTypeDescription
*)pTD
);
435 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
436 &pInterface
, pTD
, cpp_acquire
);
437 pInterface
->release();
438 TYPELIB_DANGER_RELEASE( pTD
);
439 *(void **)pRegisterReturn
= gpreg
[0];
440 eRet
= typelib_TypeClass_ANY
;
443 TYPELIB_DANGER_RELEASE( pTD
);
445 } // else perform queryInterface()
448 pCppI
, aMemberDescr
.get(),
449 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
450 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
451 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
452 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
458 throw RuntimeException(
459 rtl::OUString::createFromAscii("no member description found!"),
460 (XInterface
*)pCppI
);
462 eRet
= typelib_TypeClass_VOID
;
469 //==================================================================================================
471 * is called on incoming vtable calls
472 * (called by asm snippets)
474 static void privateSnippetExecutor( sal_uInt32 nOffsetAndIndex
, void** gpregptr
, void** fpregptr
, void** ovrflw
)
477 fprintf(stderr
, "privateSnippetExecutor\n");
484 memcpy( gpreg
, gpregptr
, 32);
485 memcpy( fpreg
, fpregptr
, 64);
487 volatile long nRegReturn
[2];
489 typelib_TypeClass aType
=
490 cpp_mediate( nOffsetAndIndex
, (void**)gpreg
, (void**)fpreg
, ovrflw
,
491 (sal_Int64
*)nRegReturn
);
496 // move return value into register space
497 // (will be loaded by machine code snippet)
499 case typelib_TypeClass_BOOLEAN
:
501 unsigned long tmp
= (unsigned long)(*(unsigned char *)nRegReturn
);
502 __asm__
volatile ( "l 2,%0\n\t" : :
506 case typelib_TypeClass_BYTE
:
508 long tmp
= (long)(*(signed char *)nRegReturn
);
509 __asm__
volatile ( "l 2,%0\n\t" : :
513 case typelib_TypeClass_CHAR
:
514 case typelib_TypeClass_UNSIGNED_SHORT
:
516 unsigned long tmp
= (unsigned long)(*(unsigned short *)nRegReturn
);
517 __asm__
volatile ( "l 2,%0\n\t" : :
521 case typelib_TypeClass_SHORT
:
523 long tmp
= (long)(*(short *)nRegReturn
);
524 __asm__
volatile ( "l 2,%0\n\t" : :
528 case typelib_TypeClass_FLOAT
:
529 __asm__
volatile ( "le 0,%0\n\t" : :
530 "m" (*((float*)nRegReturn
)) : "16" );
533 case typelib_TypeClass_DOUBLE
:
534 __asm__
volatile ( "ld 0,%0\n\t" : :
535 "m" (*((double*)nRegReturn
)) : "16" );
538 case typelib_TypeClass_HYPER
:
539 case typelib_TypeClass_UNSIGNED_HYPER
:
540 __asm__
volatile ( "lm 2,3,%0\n\t" : :
541 "m"(nRegReturn
[0]) : "2", "3" );
545 __asm__
volatile ( "l 2,%0\n\t" : :
546 "m"(nRegReturn
[0]) : "2" );
551 const int codeSnippetSize
= 50;
553 unsigned char* codeSnippet( unsigned char * code
, sal_Int16 nFunctionIndex
, sal_Int16 nVtableOffset
, bool simple_ret_type
)
555 sal_uInt32 nOffsetAndIndex
= ( ( nVtableOffset
) << 16 ) | ( nFunctionIndex
);
557 if (! simple_ret_type
)
558 nOffsetAndIndex
|= 0x8000;
560 OSL_ASSERT( sizeof (long) == 4 );
562 /* generate this code */
565 // .long privateSnippetExecutor
566 // .long nOffsetAndIndex
567 // stm %r2,%r6,8(%r15)
579 unsigned char * p
= code
;
580 *reinterpret_cast< sal_Int16
* >(p
) = 0x180d;
581 p
+= sizeof(sal_Int16
);
582 *reinterpret_cast< sal_Int32
* >(p
) = 0xa7d50006;
583 p
+= sizeof(sal_Int32
);
584 *reinterpret_cast< sal_Int32
* >(p
) =
585 reinterpret_cast< sal_Int32
>(privateSnippetExecutor
);
586 p
+= sizeof(sal_Int32
);
587 *reinterpret_cast< sal_Int32
* >(p
) = nOffsetAndIndex
;
588 p
+= sizeof(sal_Int32
);
589 *reinterpret_cast< sal_Int32
* >(p
) = 0x9026f008;
590 p
+= sizeof(sal_Int32
);
591 *reinterpret_cast< sal_Int32
* >(p
) = 0x6000f040;
592 p
+= sizeof(sal_Int32
);
593 *reinterpret_cast< sal_Int32
* >(p
) = 0x6020f048;
594 p
+= sizeof(sal_Int32
);
595 *reinterpret_cast< sal_Int32
* >(p
) = 0x5820d004;
596 p
+= sizeof(sal_Int32
);
597 *reinterpret_cast< sal_Int32
* >(p
) = 0x4130f008;
598 p
+= sizeof(sal_Int32
);
599 *reinterpret_cast< sal_Int32
* >(p
) = 0x4140f040;
600 p
+= sizeof(sal_Int32
);
601 *reinterpret_cast< sal_Int32
* >(p
) = 0x4150f060;
602 p
+= sizeof(sal_Int32
);
603 *reinterpret_cast< sal_Int32
* >(p
) = 0x5810d000;
604 p
+= sizeof(sal_Int32
);
605 *reinterpret_cast< sal_Int16
* >(p
) = 0x18d0;
606 p
+= sizeof(sal_Int16
);
607 *reinterpret_cast< sal_Int16
* >(p
) = 0x07f1;
608 p
+= sizeof(sal_Int16
);
610 return (code
+ codeSnippetSize
);
614 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
618 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
620 bridges::cpp_uno::shared::VtableFactory::Slot
*
621 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
623 return static_cast< Slot
* >(block
) + 2;
626 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
629 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
632 bridges::cpp_uno::shared::VtableFactory::Slot
*
633 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
634 void * block
, sal_Int32 slotCount
)
636 Slot
* slots
= mapBlockToVtable(block
);
639 return slots
+ slotCount
;
642 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
643 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
644 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
645 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
647 (*slots
) -= functionCount
;
650 fprintf(stderr
, "in addLocalFunctions functionOffset is %x\n",functionOffset
);
651 fprintf(stderr
, "in addLocalFunctions vtableOffset is %x\n",vtableOffset
);
654 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
655 typelib_TypeDescription
* member
= 0;
656 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
657 OSL_ASSERT(member
!= 0);
658 switch (member
->eTypeClass
) {
659 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
661 (s
++)->fn
= code
+ writetoexecdiff
;
663 code
, functionOffset
++, vtableOffset
,
664 bridges::cpp_uno::shared::isSimpleType(
666 typelib_InterfaceAttributeTypeDescription
* >(
667 member
)->pAttributeTypeRef
));
670 if (!reinterpret_cast<
671 typelib_InterfaceAttributeTypeDescription
* >(
674 (s
++)->fn
= code
+ writetoexecdiff
;
675 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
679 case typelib_TypeClass_INTERFACE_METHOD
:
680 (s
++)->fn
= code
+ writetoexecdiff
;
682 code
, functionOffset
++, vtableOffset
,
683 bridges::cpp_uno::shared::isSimpleType(
685 typelib_InterfaceMethodTypeDescription
* >(
686 member
)->pReturnTypeRef
));
693 TYPELIB_DANGER_RELEASE(member
);
698 /* vi:set tabstop=4 shiftwidth=4 expandtab: */