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"
46 extern "C" { extern void (*privateSnippetExecutor
)(); }
48 using namespace ::com::sun::star::uno
;
52 //==================================================================================================
53 static typelib_TypeClass
cpp2uno_call(
54 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
55 const typelib_TypeDescription
* pMemberTypeDescr
,
56 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
57 sal_Int32 nParams
, typelib_MethodParameter
* pParams
, long r8
,
58 void ** gpreg
, void ** fpreg
, void ** ovrflw
,
59 sal_Int64
* pRegisterReturn
/* space for register return */ )
62 fprintf(stderr
, "as far as cpp2uno_call\n");
65 int ng
= 0; //number of gpr registers used
66 int nf
= 0; //number of fpr regsiters used
68 // gpreg: [ret *], this, [gpr params]
69 // fpreg: [fpr params]
70 // ovrflw: [gpr or fpr params (properly aligned)]
73 typelib_TypeDescription
* pReturnTypeDescr
= 0;
75 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
77 void * pUnoReturn
= 0;
78 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
82 if ( ia64::return_in_hidden_param( pReturnTypeRef
) ) // complex return via ptr passed as hidden parameter reg (pCppReturn)
84 pCppReturn
= *(void **)gpreg
;
88 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
89 ? alloca( pReturnTypeDescr
->nSize
)
90 : pCppReturn
); // direct way
92 else if ( ia64::return_via_r8_buffer( pReturnTypeRef
) ) // complex return via ptr passed in r8
94 pCppReturn
= (void *)r8
;
96 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
97 ? alloca( pReturnTypeDescr
->nSize
)
98 : pCppReturn
); // direct way
102 pUnoReturn
= pRegisterReturn
; // direct way for simple types
109 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64
), "### unexpected size!" );
111 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
112 void ** pCppArgs
= pUnoArgs
+ nParams
;
113 // indizes of values this have to be converted (interface conversion cpp<=>uno)
114 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
115 // type descriptions for reconversions
116 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
118 sal_Int32 nTempIndizes
= 0;
119 bool bOverFlowUsed
= false;
120 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
122 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
123 typelib_TypeDescription
* pParamTypeDescr
= 0;
124 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
127 fprintf(stderr
, "arg %d of %d\n", nPos
, nParams
);
130 //I think it is impossible to get UNO to pass structs as parameters by copy
131 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
134 fprintf(stderr
, "simple\n");
137 switch (pParamTypeDescr
->eTypeClass
)
139 case typelib_TypeClass_FLOAT
:
140 if (nf
< ia64::MAX_SSE_REGS
&& ng
< ia64::MAX_GPR_REGS
)
142 float tmp
= (float) (*((double *)fpreg
));
143 (*((float *) fpreg
)) = tmp
;
144 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
++;
151 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
152 bOverFlowUsed
= true;
154 if (bOverFlowUsed
) ovrflw
++;
156 case typelib_TypeClass_DOUBLE
:
157 if (nf
< ia64::MAX_SSE_REGS
&& ng
< ia64::MAX_GPR_REGS
)
159 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = fpreg
++;
166 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
167 bOverFlowUsed
= true;
169 if (bOverFlowUsed
) ovrflw
++;
171 case typelib_TypeClass_BYTE
:
172 case typelib_TypeClass_BOOLEAN
:
173 case typelib_TypeClass_CHAR
:
174 case typelib_TypeClass_SHORT
:
175 case typelib_TypeClass_UNSIGNED_SHORT
:
176 case typelib_TypeClass_ENUM
:
177 case typelib_TypeClass_LONG
:
178 case typelib_TypeClass_UNSIGNED_LONG
:
180 if (ng
< ia64::MAX_GPR_REGS
)
182 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = gpreg
++;
187 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = ovrflw
;
188 bOverFlowUsed
= true;
190 if (bOverFlowUsed
) ovrflw
++;
195 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
197 else // ptr to complex value | ref
200 fprintf(stderr
, "complex, ng is %d\n", ng
);
202 void *pCppStack
; //temporary stack pointer
204 if (ng
< ia64::MAX_GPR_REGS
)
206 pCppArgs
[nPos
] = pCppStack
= *gpreg
++;
211 pCppArgs
[nPos
] = pCppStack
= *ovrflw
;
212 bOverFlowUsed
= true;
214 if (bOverFlowUsed
) ovrflw
++;
216 if (! rParam
.bIn
) // is pure out
218 // uno out is unconstructed mem!
219 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
220 pTempIndizes
[nTempIndizes
] = nPos
;
221 // will be released at reconversion
222 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
225 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr
))
227 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
228 pCppStack
, pParamTypeDescr
,
229 pThis
->getBridge()->getCpp2Uno() );
230 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
231 // will be released at reconversion
232 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
236 pUnoArgs
[nPos
] = pCppStack
;
238 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
244 fprintf(stderr
, "end of params\n");
248 uno_Any aUnoExc
; // Any will be constructed by callee
249 uno_Any
* pUnoExc
= &aUnoExc
;
251 // invoke uno dispatch call
252 (*pThis
->getUnoI()->pDispatcher
)( pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
254 // in case an exception occured...
257 // destruct temporary in/inout params
258 for ( ; nTempIndizes
--; )
260 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
262 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
263 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndizes
], 0 );
264 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
266 if (pReturnTypeDescr
)
267 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
269 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
270 // has to destruct the any
272 return typelib_TypeClass_VOID
;
274 else // else no exception occured...
277 for ( ; nTempIndizes
--; )
279 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
280 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndizes
];
282 if (pParams
[nIndex
].bOut
) // inout/out
284 // convert and assign
285 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
286 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
287 pThis
->getBridge()->getUno2Cpp() );
289 // destroy temp uno param
290 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
292 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
295 if (pCppReturn
) // has complex return
297 if (pUnoReturn
!= pCppReturn
) // needs reconversion
299 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
300 pThis
->getBridge()->getUno2Cpp() );
301 // destroy temp uno return
302 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
304 // complex return ptr is set to return reg
305 *(void **)pRegisterReturn
= pCppReturn
;
307 if (pReturnTypeDescr
)
309 typelib_TypeClass eRet
= ia64::return_via_r8_buffer(pReturnTypeRef
) ? typelib_TypeClass_VOID
: (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
310 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
314 return typelib_TypeClass_VOID
;
319 //==================================================================================================
320 static typelib_TypeClass
cpp_mediate(
321 sal_uInt64 nOffsetAndIndex
,
322 void ** gpreg
, void ** fpreg
, long sp
, long r8
,
323 sal_Int64
* pRegisterReturn
/* space for register return */ )
325 OSL_ENSURE( sizeof(sal_Int64
)==sizeof(void *), "### unexpected!" );
327 sal_Int32 nVtableOffset
= (nOffsetAndIndex
>> 32);
328 sal_Int32 nFunctionIndex
= (nOffsetAndIndex
& 0xFFFFFFFF);
330 void ** ovrflw
= (void**)(sp
);
332 // gpreg: [ret *], this, [other gpr params]
333 // fpreg: [fpr params]
334 // ovrflw: [gpr or fpr params (properly aligned)]
337 if (nFunctionIndex
& 0x80000000 )
339 nFunctionIndex
&= 0x7fffffff;
342 fprintf(stderr
, "pThis is gpreg[1]\n");
349 fprintf(stderr
, "pThis is gpreg[0]\n");
354 fprintf(stderr
, "pThis is %p\n", pThis
);
357 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
360 fprintf(stderr
, "pThis is now %p\n", pThis
);
363 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
364 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
367 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
370 fprintf(stderr
, "indexes are %d %d\n", nFunctionIndex
, pTypeDescr
->nMapFunctionIndexToMemberIndex
);
373 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
, "### illegal vtable index!" );
374 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
376 throw RuntimeException(
377 rtl::OUString::createFromAscii("illegal vtable index!"),
378 (XInterface
*)pThis
);
381 // determine called method
382 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
383 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
, "### illegal member index!" );
386 fprintf(stderr
, "members are %d %d\n", nMemberPos
, pTypeDescr
->nAllMembers
);
389 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
391 typelib_TypeClass eRet
;
392 switch (aMemberDescr
.get()->eTypeClass
)
394 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
396 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
400 pCppI
, aMemberDescr
.get(),
401 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
403 r8
, gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
408 typelib_MethodParameter aParam
;
410 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
411 aParam
.bIn
= sal_True
;
412 aParam
.bOut
= sal_False
;
415 pCppI
, aMemberDescr
.get(),
416 0, // indicates void return
418 r8
, gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
422 case typelib_TypeClass_INTERFACE_METHOD
:
425 switch (nFunctionIndex
)
428 pCppI
->acquireProxy(); // non virtual call!
429 eRet
= typelib_TypeClass_VOID
;
432 pCppI
->releaseProxy(); // non virtual call!
433 eRet
= typelib_TypeClass_VOID
;
435 case 0: // queryInterface() opt
437 typelib_TypeDescription
* pTD
= 0;
438 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( gpreg
[2] )->getTypeLibType() );
441 XInterface
* pInterface
= 0;
442 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
443 pCppI
->getBridge()->getCppEnv(),
444 (void **)&pInterface
, pCppI
->getOid().pData
,
445 (typelib_InterfaceTypeDescription
*)pTD
);
450 reinterpret_cast< uno_Any
* >( gpreg
[0] ),
451 &pInterface
, pTD
, cpp_acquire
);
452 pInterface
->release();
453 TYPELIB_DANGER_RELEASE( pTD
);
454 *(void **)pRegisterReturn
= gpreg
[0];
455 eRet
= typelib_TypeClass_ANY
;
458 TYPELIB_DANGER_RELEASE( pTD
);
460 } // else perform queryInterface()
463 pCppI
, aMemberDescr
.get(),
464 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
465 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
466 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
467 r8
, gpreg
, fpreg
, ovrflw
, pRegisterReturn
);
474 fprintf(stderr
, "screwed\n");
477 throw RuntimeException(
478 rtl::OUString::createFromAscii("no member description found!"),
479 (XInterface
*)pThis
);
481 eRet
= typelib_TypeClass_VOID
;
486 fprintf(stderr
, "end of cpp_mediate\n");
492 extern "C" ia64::RegReturn
cpp_vtable_call(
493 long in0
, long in1
, long in2
, long in3
, long in4
, long in5
, long in6
, long in7
,
497 register long r15
asm("r15");
500 register long r14
asm("r14");
501 long nOffsetAndIndex
= r14
;
503 long sp
= (long)&firstonstack
;
505 sal_uInt64 gpreg
[ia64::MAX_GPR_REGS
];
515 double fpreg
[ia64::MAX_SSE_REGS
];
516 register double f8
asm("f8"); fpreg
[0] = f8
;
517 register double f9
asm("f9"); fpreg
[1] = f9
;
518 register double f10
asm("f10"); fpreg
[2] = f10
;
519 register double f11
asm("f11"); fpreg
[3] = f11
;
520 register double f12
asm("f12"); fpreg
[4] = f12
;
521 register double f13
asm("f13"); fpreg
[5] = f13
;
522 register double f14
asm("f14"); fpreg
[6] = f14
;
523 register double f15
asm("f15"); fpreg
[7] = f15
;
526 fprintf(stderr
, "cpp_vtable_call called with %lx\n", nOffsetAndIndex
);
527 fprintf(stderr
, "adump is %lx %lx %lx %lx %lx %lx %lx %lx\n", in0
, in1
, in2
, in3
, in4
, in5
, in6
, in7
);
528 fprintf(stderr
, "bdump is %f %f %f %f %f %f %f %f\n", f8
, f9
, f10
, f11
, f12
, f13
, f14
, f15
);
531 volatile long nRegReturn
[4] = { 0 };
533 typelib_TypeClass aType
=
534 cpp_mediate( nOffsetAndIndex
, (void**)gpreg
, (void**)fpreg
, sp
, r8
, (sal_Int64
*)&nRegReturn
[0]);
539 case typelib_TypeClass_VOID
:
541 case typelib_TypeClass_BOOLEAN
:
542 case typelib_TypeClass_BYTE
:
543 case typelib_TypeClass_CHAR
:
544 case typelib_TypeClass_UNSIGNED_SHORT
:
545 case typelib_TypeClass_SHORT
:
546 case typelib_TypeClass_ENUM
:
547 case typelib_TypeClass_UNSIGNED_LONG
:
548 case typelib_TypeClass_LONG
:
549 case typelib_TypeClass_UNSIGNED_HYPER
:
550 case typelib_TypeClass_HYPER
:
551 ret
.r8
= nRegReturn
[0];
553 case typelib_TypeClass_FLOAT
:
554 asm volatile("ldfs f8=%0" : : "m"((*((float*)&nRegReturn
))) : "f8");
556 case typelib_TypeClass_DOUBLE
:
557 asm volatile("ldfd f8=%0" : : "m"((*((double*)&nRegReturn
))) : "f8");
559 case typelib_TypeClass_STRUCT
:
560 case typelib_TypeClass_EXCEPTION
:
562 ret
.r8
= nRegReturn
[0];
563 ret
.r9
= nRegReturn
[1];
564 ret
.r10
= nRegReturn
[2];
565 ret
.r11
= nRegReturn
[3];
576 const int codeSnippetSize
= 40;
578 bridges::cpp_uno::shared::VtableFactory::Slot
codeSnippet( unsigned char * code
, sal_PtrDiff writetoexecdiff
, sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
,
579 bool bHasHiddenParam
)
582 fprintf(stderr
, "size is %d\n", codeSnippetSize
);
583 fprintf(stderr
,"in codeSnippet functionIndex is %x\n", nFunctionIndex
);
584 fprintf(stderr
,"in codeSnippet vtableOffset is %x\n", nVtableOffset
);
587 sal_uInt64 nOffsetAndIndex
= ( ( (sal_uInt64
) nVtableOffset
) << 32 ) | ( (sal_uInt64
) nFunctionIndex
);
589 if ( bHasHiddenParam
)
590 nOffsetAndIndex
|= 0x80000000;
592 long *raw
= (long *)code
;
594 bridges::cpp_uno::shared::VtableFactory::Slot
* destination
= (bridges::cpp_uno::shared::VtableFactory::Slot
*)cpp_vtable_call
;
596 raw
[0] = (long)&privateSnippetExecutor
;
597 raw
[1] = (long)&raw
[2];
598 raw
[2] = nOffsetAndIndex
;
599 raw
[3] = destination
->gp_value
;
601 return *(bridges::cpp_uno::shared::VtableFactory::Slot
*)(code
+writetoexecdiff
);
605 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
609 bridges::cpp_uno::shared::VtableFactory::Slot
* bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
611 return static_cast< Slot
* >(block
) + 2;
615 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
618 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
621 bridges::cpp_uno::shared::VtableFactory::Slot
* bridges::cpp_uno::shared::VtableFactory::initializeBlock(void * block
, sal_Int32 slotCount
)
623 Slot
* slots
= mapBlockToVtable(block
);
627 return slots
+ slotCount
;
630 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
631 Slot
** in_slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
632 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
633 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
635 (*in_slots
) -= functionCount
;
636 Slot
* slots
= *in_slots
;
638 fprintf(stderr
, "in addLocalFunctions functionOffset is %x\n",functionOffset
);
639 fprintf(stderr
, "in addLocalFunctions vtableOffset is %x\n",vtableOffset
);
642 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
643 typelib_TypeDescription
* member
= 0;
644 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
645 OSL_ASSERT(member
!= 0);
646 switch (member
->eTypeClass
) {
647 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
649 *slots
++ = codeSnippet(
650 code
, writetoexecdiff
, functionOffset
++, vtableOffset
,
651 ia64::return_in_hidden_param(
653 typelib_InterfaceAttributeTypeDescription
* >(
654 member
)->pAttributeTypeRef
));
655 code
+= codeSnippetSize
;
659 if (!reinterpret_cast<
660 typelib_InterfaceAttributeTypeDescription
* >(
663 *slots
++ = codeSnippet(code
, writetoexecdiff
, functionOffset
++, vtableOffset
, false);
664 code
+= codeSnippetSize
;
668 case typelib_TypeClass_INTERFACE_METHOD
:
669 *slots
++ = codeSnippet(
670 code
, writetoexecdiff
, functionOffset
++, vtableOffset
,
671 ia64::return_in_hidden_param(
673 typelib_InterfaceMethodTypeDescription
* >(
674 member
)->pReturnTypeRef
));
675 code
+= codeSnippetSize
;
682 TYPELIB_DANGER_RELEASE(member
);
687 /* vi:set tabstop=4 shiftwidth=4 expandtab: */