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"
42 using namespace ::com::sun::star::uno
;
46 static typelib_TypeClass
cpp2uno_call(
47 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
48 const typelib_TypeDescription
* pMemberTypeDescr
,
49 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
50 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
51 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
52 sal_Int64
* pRegisterReturn
/* space for register return */ )
54 #if OSL_DEBUG_LEVEL > 2
55 fprintf(stderr
, "as far as cpp2uno_call\n");
57 int ng
= 0; //number of gpr registers used
58 int nf
= 0; //number of fpr regsiters used
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_Int64
), "### 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;
104 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
106 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
107 typelib_TypeDescription
* pParamTypeDescr
= 0;
108 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
110 #if OSL_DEBUG_LEVEL > 2
111 fprintf(stderr
, "arg %d of %d\n", nPos
, nParams
);
114 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
116 #if OSL_DEBUG_LEVEL > 2
117 fprintf(stderr
, "simple\n");
120 switch (pParamTypeDescr
->eTypeClass
)
122 case typelib_TypeClass_FLOAT
:
123 case typelib_TypeClass_DOUBLE
:
124 if (nf
< s390x::MAX_SSE_REGS
)
126 if (pParamTypeDescr
->eTypeClass
== typelib_TypeClass_FLOAT
)
128 float tmp
= (float) (*((double *)fpreg
));
129 (*((float *) fpreg
)) = tmp
;
132 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
++;
137 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
141 case typelib_TypeClass_BYTE
:
142 case typelib_TypeClass_BOOLEAN
:
143 if (ng
< s390x::MAX_GPR_REGS
)
145 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)gpreg
) + (sizeof(void*)-1));
151 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-1));
155 case typelib_TypeClass_CHAR
:
156 case typelib_TypeClass_SHORT
:
157 case typelib_TypeClass_UNSIGNED_SHORT
:
158 if (ng
< s390x::MAX_GPR_REGS
)
160 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)gpreg
) + (sizeof(void*)-2));
166 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-2));
170 case typelib_TypeClass_ENUM
:
171 case typelib_TypeClass_LONG
:
172 case typelib_TypeClass_UNSIGNED_LONG
:
173 if (ng
< s390x::MAX_GPR_REGS
)
175 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)gpreg
) + (sizeof(void*)-4));
181 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-4));
186 if (ng
< s390x::MAX_GPR_REGS
)
188 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
++;
193 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
200 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
202 else // ptr to complex value | ref
204 #if OSL_DEBUG_LEVEL > 2
205 fprintf(stderr
, "complex, ng is %d\n", ng
);
208 void *pCppStack
; //temporary stack pointer
210 if (ng
< s390x::MAX_GPR_REGS
)
212 pCppArgs
[nPos
] = pCppStack
= *gpreg
++;
217 pCppArgs
[nPos
] = pCppStack
= *ovrflw
;
221 if (! rParam
.bIn
) // is pure out
223 // uno out is unconstructed mem!
224 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
225 pTempIndizes
[nTempIndizes
] = nPos
;
226 // will be released at reconversion
227 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
230 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
232 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
233 pCppStack
, pParamTypeDescr
,
234 pThis
->getBridge()->getCpp2Uno() );
235 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
236 // will be released at reconversion
237 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
241 pUnoArgs
[nPos
] = pCppStack
;
243 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
248 #if OSL_DEBUG_LEVEL > 2
249 fprintf(stderr
, "end of params\n");
253 uno_Any aUnoExc
; // Any will be constructed by callee
254 uno_Any
* pUnoExc
= &aUnoExc
;
256 // invoke uno dispatch call
257 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
259 // in case an exception occurred...
262 // destruct temporary in/inout params
263 for ( ; nTempIndizes
--; )
265 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
267 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
268 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
269 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
271 if (pReturnTypeDescr
)
272 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
274 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
276 return typelib_TypeClass_VOID
;
278 else // else no exception occurred...
281 for ( ; nTempIndizes
--; )
283 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
284 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
286 if (pParams
[nIndex
].bOut
) // inout/out
288 // convert and assign
289 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
290 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
291 pThis
->getBridge()->getUno2Cpp() );
293 // destroy temp uno param
294 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
296 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
299 if (pCppReturn
) // has complex return
301 if (pUnoReturn
!= pCppReturn
) // needs reconversion
303 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
304 pThis
->getBridge()->getUno2Cpp() );
305 // destroy temp uno return
306 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
308 // complex return ptr is set to return reg
309 *(void **)pRegisterReturn
= pCppReturn
;
311 if (pReturnTypeDescr
)
313 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
314 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
318 return typelib_TypeClass_VOID
;
323 //============================================================================
324 static typelib_TypeClass
cpp_mediate(
325 sal_uInt64 nOffsetAndIndex
,
326 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
327 sal_Int64
* pRegisterReturn
/* space for register return */ )
329 OSL_ENSURE( sizeof(sal_Int64
)==sizeof(void *), "### unexpected!" );
331 sal_Int32 nVtableOffset
= (nOffsetAndIndex
>> 32);
332 sal_Int32 nFunctionIndex
= (nOffsetAndIndex
& 0xFFFFFFFF);
334 #if OSL_DEBUG_LEVEL > 2
335 fprintf(stderr
, "nVTableOffset, nFunctionIndex are %x %x\n", nVtableOffset
, nFunctionIndex
);
338 #if OSL_DEBUG_LEVEL > 2
339 // Let's figure out what is really going on here
341 fprintf( stderr
, "= cpp_mediate () =\nGPR's (%d): ", 5 );
342 for ( unsigned int i
= 0; i
< 5; ++i
)
343 fprintf( stderr
, "0x%lx, ", gpreg
[i
] );
344 fprintf( stderr
, "\n");
345 fprintf( stderr
, "\nFPR's (%d): ", 4 );
346 for ( unsigned int i
= 0; i
< 4; ++i
)
347 fprintf( stderr
, "0x%lx (%f), ", fpreg
[i
], fpreg
[i
] );
348 fprintf( stderr
, "\n");
353 // gpreg: [ret *], this, [other gpr params]
354 // fpreg: [fpr params]
355 // ovrflw: [gpr or fpr params (properly aligned)]
357 // _this_ ptr is patched cppu_XInterfaceProxy object
359 if( nFunctionIndex
& 0x80000000 )
361 nFunctionIndex
&= 0x7fffffff;
369 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
371 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
372 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
375 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
378 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
379 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
381 throw RuntimeException(
382 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal vtable index!" )),
383 (XInterface
*)pCppI
);
386 // determine called method
387 OSL_ENSURE( nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
388 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
389 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
391 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
393 typelib_TypeClass eRet
;
394 switch (aMemberDescr
.get()->eTypeClass
)
396 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
398 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
402 pCppI
, aMemberDescr
.get(),
403 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
405 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
410 typelib_MethodParameter aParam
;
412 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
413 aParam
.bIn
= sal_True
;
414 aParam
.bOut
= sal_False
;
417 pCppI
, aMemberDescr
.get(),
418 0, // indicates void return
420 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
424 case typelib_TypeClass_INTERFACE_METHOD
:
427 switch (nFunctionIndex
)
430 pCppI
->acquireProxy(); // non virtual call!
431 eRet
= typelib_TypeClass_VOID
;
434 pCppI
->releaseProxy(); // non virtual call!
435 eRet
= typelib_TypeClass_VOID
;
437 case 0: // queryInterface() opt
439 typelib_TypeDescription
* pTD
= 0;
440 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
443 XInterface
* pInterface
= 0;
444 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
445 pCppI
->getBridge()->getCppEnv(),
446 (void **)&pInterface
, pCppI
->getOid().pData
,
447 (typelib_InterfaceTypeDescription
*)pTD
);
452 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
453 &pInterface
, pTD
, cpp_acquire
);
454 pInterface
->release();
455 TYPELIB_DANGER_RELEASE( pTD
);
456 *(void **)pRegisterReturn
= gpreg
[0];
457 eRet
= typelib_TypeClass_ANY
;
460 TYPELIB_DANGER_RELEASE( pTD
);
462 } // else perform queryInterface()
465 pCppI
, aMemberDescr
.get(),
466 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
467 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
468 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
469 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
475 throw RuntimeException(
476 rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "no member description found!" )),
477 (XInterface
*)pCppI
);
484 long privateSnippetExecutor(long r2
, long r3
, long r4
, long r5
, long r6
, long firstonstack
)
486 register long r0
asm("r0");
487 sal_uInt64 nOffsetAndIndex
= r0
;
489 long sp
= (long)&firstonstack
;
491 sal_uInt64 gpreg
[s390x::MAX_GPR_REGS
];
498 double fpreg
[s390x::MAX_SSE_REGS
];
499 register double f0
asm("f0"); fpreg
[0] = f0
;
500 register double f2
asm("f2"); fpreg
[1] = f2
;
501 register double f4
asm("f4"); fpreg
[2] = f4
;
502 register double f6
asm("f6"); fpreg
[3] = f6
;
504 volatile long nRegReturn
[1];
505 #if OSL_DEBUG_LEVEL > 2
506 fprintf(stderr
, "before mediate with %lx\n",nOffsetAndIndex
);
507 fprintf(stderr
, "doubles are %f %f %f %f\n", fpreg
[0], fpreg
[1], fpreg
[2], fpreg
[3]);
509 typelib_TypeClass aType
=
510 cpp_mediate( nOffsetAndIndex
, (void**)gpreg
, (void**)fpreg
, (void**)sp
,
511 (sal_Int64
*)nRegReturn
);
512 #if OSL_DEBUG_LEVEL > 2
513 fprintf(stderr
, "after mediate ret is %lx %ld\n", nRegReturn
[0], nRegReturn
[0]);
518 case typelib_TypeClass_BOOLEAN
:
519 case typelib_TypeClass_BYTE
:
520 nRegReturn
[0] = (unsigned long)(*(unsigned char *)nRegReturn
);
522 case typelib_TypeClass_CHAR
:
523 case typelib_TypeClass_UNSIGNED_SHORT
:
524 case typelib_TypeClass_SHORT
:
525 nRegReturn
[0] = (unsigned long)(*(unsigned short *)nRegReturn
);
527 case typelib_TypeClass_ENUM
:
528 case typelib_TypeClass_UNSIGNED_LONG
:
529 case typelib_TypeClass_LONG
:
530 nRegReturn
[0] = (unsigned long)(*(unsigned int *)nRegReturn
);
532 case typelib_TypeClass_VOID
:
535 case typelib_TypeClass_FLOAT
:
537 double tmp
= (double) (*((float *)nRegReturn
));
538 (*((double *) nRegReturn
)) = tmp
;
540 //deliberate fall through
541 case typelib_TypeClass_DOUBLE
:
542 __asm__ ( "ld 0,%0\n\t"
543 : : "m" (*((double*)nRegReturn
)) );
546 return nRegReturn
[0];
549 const int codeSnippetSize
= 32;
551 unsigned char *codeSnippet( unsigned char * code
, sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
, bool simple_ret_type
)
553 sal_uInt64 nOffsetAndIndex
= ( ( (sal_uInt64
) nVtableOffset
) << 32 ) | ( (sal_Int64
) nFunctionIndex
);
555 if (! simple_ret_type
)
556 nOffsetAndIndex
|= 0x80000000;
558 unsigned char * p
= code
;
559 *(short *)&p
[0] = 0x0d10; /* basr %r1,0 */
560 *(short *)&p
[2] = 0xeb01; /* lmg %r0,%r1,14(%r1) */
561 *(short *)&p
[4] = 0x100e;
562 *(short *)&p
[6] = 0x0004;
563 *(short *)&p
[8] = 0x07f1; /* br %r1 */
564 *(long *)&p
[16] = (long)nOffsetAndIndex
;
565 *(long *)&p
[24] = (long)&privateSnippetExecutor
;
566 return (code
+ codeSnippetSize
);
570 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
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 sal_Size
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
)
592 Slot
* slots
= mapBlockToVtable(block
);
595 return slots
+ slotCount
;
598 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
599 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
600 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
601 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
603 (*slots
) -= functionCount
;
605 #if OSL_DEBUG_LEVEL > 2
606 fprintf(stderr
, "in addLocalFunctions functionOffset is %x\n",functionOffset
);
607 fprintf(stderr
, "in addLocalFunctions vtableOffset is %x\n",vtableOffset
);
610 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
611 typelib_TypeDescription
* member
= 0;
612 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
613 OSL_ASSERT(member
!= 0);
614 switch (member
->eTypeClass
) {
615 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
617 (s
++)->fn
= code
+ writetoexecdiff
;
619 code
, functionOffset
++, vtableOffset
,
620 bridges::cpp_uno::shared::isSimpleType(
622 typelib_InterfaceAttributeTypeDescription
* >(
623 member
)->pAttributeTypeRef
));
626 if (!reinterpret_cast<
627 typelib_InterfaceAttributeTypeDescription
* >(
630 (s
++)->fn
= code
+ writetoexecdiff
;
631 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
635 case typelib_TypeClass_INTERFACE_METHOD
:
636 (s
++)->fn
= code
+ writetoexecdiff
;
638 code
, functionOffset
++, vtableOffset
,
639 bridges::cpp_uno::shared::isSimpleType(
641 typelib_InterfaceMethodTypeDescription
* >(
642 member
)->pReturnTypeRef
));
649 TYPELIB_DANGER_RELEASE(member
);
654 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */