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 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_bridges.hxx"
35 #include <com/sun/star/uno/genfunc.hxx>
37 #include <typelib/typedescription.hxx>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
47 using namespace ::com::sun::star::uno
;
51 static typelib_TypeClass
cpp2uno_call(
52 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
53 const typelib_TypeDescription
* pMemberTypeDescr
,
54 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
55 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
56 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
57 sal_Int64
* pRegisterReturn
/* space for register return */ )
60 fprintf(stderr
, "as far as cpp2uno_call\n");
62 int ng
= 0; //number of gpr registers used
63 int nf
= 0; //number of fpr regsiters used
65 // gpreg: [ret *], this, [gpr params]
66 // fpreg: [fpr params]
67 // ovrflw: [gpr or fpr params (properly aligned)]
70 typelib_TypeDescription
* pReturnTypeDescr
= 0;
72 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
74 void * pUnoReturn
= 0;
75 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
79 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
81 pUnoReturn
= pRegisterReturn
; // direct way for simple types
83 else // complex return via ptr (pCppReturn)
85 pCppReturn
= *(void **)gpreg
;
89 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
90 ? alloca( pReturnTypeDescr
->nSize
)
91 : pCppReturn
); // direct way
99 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64
), "### unexpected size!" );
101 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
102 void ** pCppArgs
= pUnoArgs
+ nParams
;
103 // indizes of values this have to be converted (interface conversion cpp<=>uno)
104 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
105 // type descriptions for reconversions
106 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
108 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
);
116 fprintf(stderr
, "arg %d of %d\n", nPos
, nParams
);
119 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
122 fprintf(stderr
, "simple\n");
125 switch (pParamTypeDescr
->eTypeClass
)
127 case typelib_TypeClass_FLOAT
:
128 case typelib_TypeClass_DOUBLE
:
129 if (nf
< s390x::MAX_SSE_REGS
)
131 if (pParamTypeDescr
->eTypeClass
== typelib_TypeClass_FLOAT
)
133 float tmp
= (float) (*((double *)fpreg
));
134 (*((float *) fpreg
)) = tmp
;
137 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
++;
142 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
146 case typelib_TypeClass_BYTE
:
147 case typelib_TypeClass_BOOLEAN
:
148 if (ng
< s390x::MAX_GPR_REGS
)
150 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)gpreg
) + (sizeof(void*)-1));
156 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-1));
160 case typelib_TypeClass_CHAR
:
161 case typelib_TypeClass_SHORT
:
162 case typelib_TypeClass_UNSIGNED_SHORT
:
163 if (ng
< s390x::MAX_GPR_REGS
)
165 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)gpreg
) + (sizeof(void*)-2));
171 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-2));
175 case typelib_TypeClass_ENUM
:
176 case typelib_TypeClass_LONG
:
177 case typelib_TypeClass_UNSIGNED_LONG
:
178 if (ng
< s390x::MAX_GPR_REGS
)
180 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)gpreg
) + (sizeof(void*)-4));
186 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = (((char *)ovrflw
) + (sizeof(void*)-4));
191 if (ng
< s390x::MAX_GPR_REGS
)
193 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
++;
198 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
205 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
207 else // ptr to complex value | ref
210 fprintf(stderr
, "complex, ng is %d\n", ng
);
213 void *pCppStack
; //temporary stack pointer
215 if (ng
< s390x::MAX_GPR_REGS
)
217 pCppArgs
[nPos
] = pCppStack
= *gpreg
++;
222 pCppArgs
[nPos
] = pCppStack
= *ovrflw
;
226 if (! rParam
.bIn
) // is pure out
228 // uno out is unconstructed mem!
229 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
230 pTempIndizes
[nTempIndizes
] = nPos
;
231 // will be released at reconversion
232 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
235 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
237 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
238 pCppStack
, pParamTypeDescr
,
239 pThis
->getBridge()->getCpp2Uno() );
240 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
241 // will be released at reconversion
242 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
246 pUnoArgs
[nPos
] = pCppStack
;
248 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
254 fprintf(stderr
, "end of params\n");
258 uno_Any aUnoExc
; // Any will be constructed by callee
259 uno_Any
* pUnoExc
= &aUnoExc
;
261 // invoke uno dispatch call
262 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
264 // in case an exception occured...
267 // destruct temporary in/inout params
268 for ( ; nTempIndizes
--; )
270 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
272 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
273 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
274 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
276 if (pReturnTypeDescr
)
277 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
279 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
281 return typelib_TypeClass_VOID
;
283 else // else no exception occured...
286 for ( ; nTempIndizes
--; )
288 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
289 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
291 if (pParams
[nIndex
].bOut
) // inout/out
293 // convert and assign
294 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
295 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
296 pThis
->getBridge()->getUno2Cpp() );
298 // destroy temp uno param
299 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
301 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
304 if (pCppReturn
) // has complex return
306 if (pUnoReturn
!= pCppReturn
) // needs reconversion
308 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
309 pThis
->getBridge()->getUno2Cpp() );
310 // destroy temp uno return
311 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
313 // complex return ptr is set to return reg
314 *(void **)pRegisterReturn
= pCppReturn
;
316 if (pReturnTypeDescr
)
318 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
319 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
323 return typelib_TypeClass_VOID
;
328 //============================================================================
329 static typelib_TypeClass
cpp_mediate(
330 sal_uInt64 nOffsetAndIndex
,
331 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
332 sal_Int64
* pRegisterReturn
/* space for register return */ )
334 OSL_ENSURE( sizeof(sal_Int64
)==sizeof(void *), "### unexpected!" );
336 sal_Int32 nVtableOffset
= (nOffsetAndIndex
>> 32);
337 sal_Int32 nFunctionIndex
= (nOffsetAndIndex
& 0xFFFFFFFF);
340 fprintf(stderr
, "nVTableOffset, nFunctionIndex are %x %x\n", nVtableOffset
, nFunctionIndex
);
344 // Let's figure out what is really going on here
346 fprintf( stderr
, "= cpp_mediate () =\nGPR's (%d): ", 5 );
347 for ( unsigned int i
= 0; i
< 5; ++i
)
348 fprintf( stderr
, "0x%lx, ", gpreg
[i
] );
349 fprintf( stderr
, "\n");
350 fprintf( stderr
, "\nFPR's (%d): ", 4 );
351 for ( unsigned int i
= 0; i
< 4; ++i
)
352 fprintf( stderr
, "0x%lx (%f), ", fpreg
[i
], fpreg
[i
] );
353 fprintf( stderr
, "\n");
358 // gpreg: [ret *], this, [other gpr params]
359 // fpreg: [fpr params]
360 // ovrflw: [gpr or fpr params (properly aligned)]
362 // _this_ ptr is patched cppu_XInterfaceProxy object
364 if( nFunctionIndex
& 0x80000000 )
366 nFunctionIndex
&= 0x7fffffff;
374 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
376 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
377 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
380 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
383 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
384 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
386 throw RuntimeException(
387 rtl::OUString::createFromAscii("illegal vtable index!"),
388 (XInterface
*)pCppI
);
391 // determine called method
392 OSL_ENSURE( nVtableCall
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
393 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
394 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
396 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
398 typelib_TypeClass eRet
;
399 switch (aMemberDescr
.get()->eTypeClass
)
401 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
403 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
407 pCppI
, aMemberDescr
.get(),
408 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
410 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
415 typelib_MethodParameter aParam
;
417 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
418 aParam
.bIn
= sal_True
;
419 aParam
.bOut
= sal_False
;
422 pCppI
, aMemberDescr
.get(),
423 0, // indicates void return
425 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
429 case typelib_TypeClass_INTERFACE_METHOD
:
432 switch (nFunctionIndex
)
435 pCppI
->acquireProxy(); // non virtual call!
436 eRet
= typelib_TypeClass_VOID
;
439 pCppI
->releaseProxy(); // non virtual call!
440 eRet
= typelib_TypeClass_VOID
;
442 case 0: // queryInterface() opt
444 typelib_TypeDescription
* pTD
= 0;
445 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
448 XInterface
* pInterface
= 0;
449 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
450 pCppI
->getBridge()->getCppEnv(),
451 (void **)&pInterface
, pCppI
->getOid().pData
,
452 (typelib_InterfaceTypeDescription
*)pTD
);
457 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
458 &pInterface
, pTD
, cpp_acquire
);
459 pInterface
->release();
460 TYPELIB_DANGER_RELEASE( pTD
);
461 *(void **)pRegisterReturn
= gpreg
[0];
462 eRet
= typelib_TypeClass_ANY
;
465 TYPELIB_DANGER_RELEASE( pTD
);
467 } // else perform queryInterface()
470 pCppI
, aMemberDescr
.get(),
471 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
472 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
473 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
474 gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
480 throw RuntimeException(
481 rtl::OUString::createFromAscii("no member description found!"),
482 (XInterface
*)pCppI
);
484 eRet
= typelib_TypeClass_VOID
;
491 long privateSnippetExecutor(long r2
, long r3
, long r4
, long r5
, long r6
, long firstonstack
)
493 register long r0
asm("r0");
494 sal_uInt32 nOffsetAndIndex
= r0
;
496 long sp
= (long)&firstonstack
;
498 sal_uInt64 gpreg
[s390x::MAX_GPR_REGS
];
505 double fpreg
[s390x::MAX_SSE_REGS
];
506 register double f0
asm("f0"); fpreg
[0] = f0
;
507 register double f2
asm("f2"); fpreg
[1] = f2
;
508 register double f4
asm("f4"); fpreg
[2] = f4
;
509 register double f6
asm("f6"); fpreg
[3] = f6
;
511 volatile long nRegReturn
[1];
513 fprintf(stderr
, "before mediate with %lx\n",nOffsetAndIndex
);
514 fprintf(stderr
, "doubles are %f %f %f %f\n", fpreg
[0], fpreg
[1], fpreg
[2], fpreg
[3]);
516 typelib_TypeClass aType
=
517 cpp_mediate( nOffsetAndIndex
, (void**)gpreg
, (void**)fpreg
, (void**)sp
,
518 (sal_Int64
*)nRegReturn
);
520 fprintf(stderr
, "after mediate ret is %lx %ld\n", nRegReturn
[0], nRegReturn
[0]);
525 case typelib_TypeClass_VOID
:
526 case typelib_TypeClass_BOOLEAN
:
527 case typelib_TypeClass_BYTE
:
528 case typelib_TypeClass_CHAR
:
529 case typelib_TypeClass_UNSIGNED_SHORT
:
530 case typelib_TypeClass_SHORT
:
531 case typelib_TypeClass_ENUM
:
532 case typelib_TypeClass_UNSIGNED_LONG
:
533 case typelib_TypeClass_LONG
:
536 case typelib_TypeClass_FLOAT
:
538 double tmp
= (double) (*((float *)nRegReturn
));
539 (*((double *) nRegReturn
)) = tmp
;
541 //deliberate fall through
542 case typelib_TypeClass_DOUBLE
:
543 __asm__ ( "ld 0,%0\n\t"
544 : : "m" (*((double*)nRegReturn
)) );
547 return nRegReturn
[0];
550 const int codeSnippetSize
= 32;
552 unsigned char *codeSnippet( unsigned char * code
, sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
, bool simple_ret_type
)
554 sal_uInt64 nOffsetAndIndex
= ( ( (sal_uInt64
) nVtableOffset
) << 32 ) | ( (sal_Int64
) nFunctionIndex
);
556 if (! simple_ret_type
)
557 nOffsetAndIndex
|= 0x80000000;
559 unsigned char * p
= code
;
560 *(short *)&p
[0] = 0x0d10; /* basr %r1,0 */
561 *(short *)&p
[2] = 0xeb01; /* lmg %r0,%r1,14(%r1) */
562 *(short *)&p
[4] = 0x100e;
563 *(short *)&p
[6] = 0x0004;
564 *(short *)&p
[8] = 0x07f1; /* br %r1 */
565 *(long *)&p
[16] = (long)nOffsetAndIndex
;
566 *(long *)&p
[24] = (long)&privateSnippetExecutor
;
567 return (code
+ codeSnippetSize
);
571 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
575 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
577 bridges::cpp_uno::shared::VtableFactory::Slot
*
578 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
580 return static_cast< Slot
* >(block
) + 2;
583 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
586 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
589 bridges::cpp_uno::shared::VtableFactory::Slot
*
590 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
591 void * block
, sal_Int32 slotCount
)
593 Slot
* slots
= mapBlockToVtable(block
);
596 return slots
+ slotCount
;
599 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
600 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
601 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
602 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
604 (*slots
) -= functionCount
;
607 fprintf(stderr
, "in addLocalFunctions functionOffset is %x\n",functionOffset
);
608 fprintf(stderr
, "in addLocalFunctions vtableOffset is %x\n",vtableOffset
);
611 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
612 typelib_TypeDescription
* member
= 0;
613 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
614 OSL_ASSERT(member
!= 0);
615 switch (member
->eTypeClass
) {
616 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
618 (s
++)->fn
= code
+ writetoexecdiff
;
620 code
, functionOffset
++, vtableOffset
,
621 bridges::cpp_uno::shared::isSimpleType(
623 typelib_InterfaceAttributeTypeDescription
* >(
624 member
)->pAttributeTypeRef
));
627 if (!reinterpret_cast<
628 typelib_InterfaceAttributeTypeDescription
* >(
631 (s
++)->fn
= code
+ writetoexecdiff
;
632 code
= codeSnippet(code
, functionOffset
++, vtableOffset
, true);
636 case typelib_TypeClass_INTERFACE_METHOD
:
637 (s
++)->fn
= code
+ writetoexecdiff
;
639 code
, functionOffset
++, vtableOffset
,
640 bridges::cpp_uno::shared::isSimpleType(
642 typelib_InterfaceMethodTypeDescription
* >(
643 member
)->pReturnTypeRef
));
650 TYPELIB_DANGER_RELEASE(member
);
655 /* vi:set tabstop=4 shiftwidth=4 expandtab: */