1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/genfunc.hxx>
21 #include <sal/log.hxx>
22 #include <typelib/typedescription.hxx>
25 #include "cppinterfaceproxy.hxx"
27 #include "vtablefactory.hxx"
29 #include <sal/alloca.h>
31 using namespace com::sun::star::uno
;
36 static typelib_TypeClass
cpp2uno_call(
37 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
38 const typelib_TypeDescription
* pMemberTypeDescr
,
39 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
40 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
42 sal_Int64
* pRegisterReturn
/* space for register return */ )
44 // pCallStack: [ret ptr], this, params
45 char * pCppStack
= (char *)pCallStack
;
48 typelib_TypeDescription
* pReturnTypeDescr
= 0;
50 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
52 void * pUnoReturn
= 0;
53 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
57 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
58 pUnoReturn
= pRegisterReturn
; // direct way for simple types
59 else // complex return via ptr (pCppReturn)
61 pCppReturn
= *(void**)pCppStack
;
62 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
64 ? alloca( pReturnTypeDescr
->nSize
)
65 : pCppReturn
); // direct way
66 pCppStack
+= sizeof( void* );
70 pCppStack
+= sizeof( void* );
73 static_assert(sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!");
75 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
76 void ** pCppArgs
= pUnoArgs
+ nParams
;
77 // indices of values this have to be converted (interface conversion cpp<=>uno)
78 sal_Int32
* pTempIndices
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
79 // type descriptions for reconversions
80 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
82 sal_Int32 nTempIndices
= 0;
84 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
86 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
87 typelib_TypeDescription
* pParamTypeDescr
= 0;
88 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
90 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
92 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack
, pParamTypeDescr
);
93 switch (pParamTypeDescr
->eTypeClass
)
95 case typelib_TypeClass_HYPER
:
96 case typelib_TypeClass_UNSIGNED_HYPER
:
97 case typelib_TypeClass_DOUBLE
:
99 if ((reinterpret_cast< long >(pCppStack
) & 7) != 0)
101 static_assert(sizeof (double) == sizeof (sal_Int64
), "boo");
102 void * pDest
= alloca( sizeof (sal_Int64
) );
103 *reinterpret_cast< sal_Int32
* >(pDest
) =
104 *reinterpret_cast< sal_Int32
const * >(pCppStack
);
105 *(reinterpret_cast< sal_Int32
* >(pDest
) + 1) =
106 *(reinterpret_cast< sal_Int32
const * >(pCppStack
) + 1);
107 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = pDest
;
109 pCppStack
+= sizeof (sal_Int32
); // extra long
114 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
116 else // ptr to complex value | ref
118 pCppArgs
[nPos
] = *(void **)pCppStack
;
120 if (! rParam
.bIn
) // is pure out
122 // uno out is unconstructed mem!
123 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
124 pTempIndices
[nTempIndices
] = nPos
;
125 // will be released at reconversion
126 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
129 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
132 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
133 *(void **)pCppStack
, pParamTypeDescr
,
134 pThis
->getBridge()->getCpp2Uno() );
135 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
136 // will be released at reconversion
137 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
141 pUnoArgs
[nPos
] = *(void **)pCppStack
;
143 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
146 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
150 uno_Any aUnoExc
; // Any will be constructed by callee
151 uno_Any
* pUnoExc
= &aUnoExc
;
153 // invoke uno dispatch call
154 (*pThis
->getUnoI()->pDispatcher
)(pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
156 // in case an exception occurred...
159 // destruct temporary in/inout params
160 for ( ; nTempIndices
--; )
162 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
164 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
165 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], 0 );
166 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
168 if (pReturnTypeDescr
)
169 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
170 CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
171 // has to destruct the any
173 return typelib_TypeClass_VOID
;
175 else // else no exception occurred...
178 for ( ; nTempIndices
--; )
180 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
181 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
183 if (pParams
[nIndex
].bOut
) // inout/out
185 // convert and assign
186 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
187 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
188 pThis
->getBridge()->getUno2Cpp() );
190 // destroy temp uno param
191 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
193 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
196 if (pCppReturn
) // has complex return
198 if (pUnoReturn
!= pCppReturn
) // needs reconversion
200 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
201 pThis
->getBridge()->getUno2Cpp() );
202 // destroy temp uno return
203 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
205 // complex return ptr is set to eax
206 *(void **)pRegisterReturn
= pCppReturn
;
208 if (pReturnTypeDescr
)
210 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
211 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
215 return typelib_TypeClass_VOID
;
220 static typelib_TypeClass
cpp_mediate(
221 sal_Int32 nFunctionIndex
,
222 sal_Int32 nVtableOffset
,
224 sal_Int64
* pRegisterReturn
/* space for register return */ )
226 static_assert( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
228 // pCallStack: this, params
229 // eventual [ret*] lies at pCallStack -1
230 // so count down pCallStack by one to keep it simple
231 // pCallStack: this, params
232 // eventual [ret*] lies at pCallStack -1
233 // so count down pCallStack by one to keep it simple
234 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
235 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
236 static_cast< char * >(*pCallStack
) - nVtableOffset
);
237 if ((nFunctionIndex
& 0x80000000) != 0) {
238 nFunctionIndex
&= 0x7FFFFFFF;
242 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
244 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
248 "illegal " << OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
249 << " vtable index " << nFunctionIndex
<< "/"
250 << pTypeDescr
->nMapFunctionIndexToMemberIndex
);
251 throw RuntimeException(
252 ("illegal " + OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
253 + " vtable index " + OUString::number(nFunctionIndex
) + "/"
254 + OUString::number(pTypeDescr
->nMapFunctionIndexToMemberIndex
)),
255 (XInterface
*)pCppI
);
258 // determine called method
259 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
260 assert(nMemberPos
< pTypeDescr
->nAllMembers
);
262 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
264 #if defined BRIDGES_DEBUG
265 OString
cstr( OUStringToOString( aMemberDescr
.get()->pTypeName
, RTL_TEXTENCODING_ASCII_US
) );
266 fprintf( stderr
, "calling %s, nFunctionIndex=%d\n", cstr
.getStr(), nFunctionIndex
);
269 typelib_TypeClass eRet
;
270 switch (aMemberDescr
.get()->eTypeClass
)
272 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
274 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
278 pCppI
, aMemberDescr
.get(),
279 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
281 pCallStack
, pRegisterReturn
);
286 typelib_MethodParameter aParam
;
288 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
289 aParam
.bIn
= sal_True
;
290 aParam
.bOut
= sal_False
;
293 pCppI
, aMemberDescr
.get(),
294 0, // indicates void return
296 pCallStack
, pRegisterReturn
);
300 case typelib_TypeClass_INTERFACE_METHOD
:
303 switch (nFunctionIndex
)
306 pCppI
->acquireProxy(); // non virtual call!
307 eRet
= typelib_TypeClass_VOID
;
310 pCppI
->releaseProxy(); // non virtual call!
311 eRet
= typelib_TypeClass_VOID
;
313 case 0: // queryInterface() opt
315 typelib_TypeDescription
* pTD
= 0;
316 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[2] )->getTypeLibType() );
319 XInterface
* pInterface
= 0;
320 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
321 pCppI
->getBridge()->getCppEnv(),
322 (void **)&pInterface
, pCppI
->getOid().pData
, (typelib_InterfaceTypeDescription
*)pTD
);
327 reinterpret_cast< uno_Any
* >( pCallStack
[0] ),
328 &pInterface
, pTD
, cpp_acquire
);
329 pInterface
->release();
330 TYPELIB_DANGER_RELEASE( pTD
);
331 *(void **)pRegisterReturn
= pCallStack
[0];
332 eRet
= typelib_TypeClass_ANY
;
335 TYPELIB_DANGER_RELEASE( pTD
);
337 } // else perform queryInterface()
340 pCppI
, aMemberDescr
.get(),
341 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
342 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
343 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
344 pCallStack
, pRegisterReturn
);
350 throw RuntimeException("no member description found!", (XInterface
*)pCppI
);
358 * is called on incoming vtable calls
359 * (called by asm snippets)
361 static void cpp_vtable_call()
363 volatile sal_Int64 nRegReturn
;
368 __asm__( "st %%i0, %0\n\t"
371 : : "m"(nFunctionIndex
), "m"(pCallStack
), "m"(vTableOffset
) );
373 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
376 sal_Bool bComplex
= (nFunctionIndex
& 0x80000000) ? sal_True
: sal_False
;
377 typelib_TypeClass aType
=
378 cpp_mediate( nFunctionIndex
, vTableOffset
, pCallStack
+17, (sal_Int64
*)&nRegReturn
);
382 case typelib_TypeClass_BOOLEAN
:
383 case typelib_TypeClass_BYTE
:
384 __asm__( "ld %0, %%l0\n\t"
385 "ldsb [%%l0], %%i0\n"
386 : : "m"(&nRegReturn
) );
388 case typelib_TypeClass_CHAR
:
389 case typelib_TypeClass_SHORT
:
390 case typelib_TypeClass_UNSIGNED_SHORT
:
391 __asm__( "ld %0, %%l0\n\t"
392 "ldsh [%%l0], %%i0\n"
393 : : "m"(&nRegReturn
) );
395 case typelib_TypeClass_HYPER
:
396 case typelib_TypeClass_UNSIGNED_HYPER
:
398 __asm__( "ld %0, %%l0\n\t"
399 "ld [%%l0], %%i0\n\t"
401 "ld [%%l0], %%i1\n\t"
402 : : "m"(&nRegReturn
), "m"(((long*)&nRegReturn
) +1) );
405 case typelib_TypeClass_FLOAT
:
406 __asm__( "ld %0, %%l0\n\t"
408 : : "m"(&nRegReturn
) );
410 case typelib_TypeClass_DOUBLE
:
411 __asm__( "ld %0, %%l0\n\t"
413 : : "m"(&nRegReturn
) );
415 case typelib_TypeClass_VOID
:
418 __asm__( "ld %0, %%l0\n\t"
420 : : "m"(&nRegReturn
) );
426 __asm__( "add %i7, 4, %i7\n\t" );
427 // after call to complex return valued function there is an unimp instruction
433 int const codeSnippetSize
= 56;
434 unsigned char * codeSnippet(
435 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
438 sal_uInt32 index
= functionIndex
;
439 if (!simpleRetType
) {
442 unsigned int * p
= reinterpret_cast< unsigned int * >(code
);
443 static_assert(sizeof (unsigned int) == 4);
456 // sethi %hi(index), %o0:
457 *p
++ = 0x11000000 | (index
>> 10);
458 // or %o0, %lo(index), %o0:
459 *p
++ = 0x90122000 | (index
& 0x3FF);
460 // sethi %hi(vtableOffset), %o2:
461 *p
++ = 0x15000000 | (vtableOffset
>> 10);
462 // or %o2, %lo(vtableOffset), %o2:
463 *p
++ = 0x9412A000 | (vtableOffset
& 0x3FF);
464 // sethi %hi(cpp_vtable_call), %o3:
465 *p
++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) >> 10);
466 // or %o3, %lo(cpp_vtable_call), %o3:
467 *p
++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) & 0x3FF);
472 assert(reinterpret_cast< unsigned char * >(p
) - code
<= codeSnippetSize
);
473 return code
+ codeSnippetSize
;
478 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
480 bridges::cpp_uno::shared::VtableFactory::Slot
*
481 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
483 return static_cast< Slot
* >(block
) + 2;
486 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
489 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
492 bridges::cpp_uno::shared::VtableFactory::Slot
*
493 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
494 void * block
, sal_Int32 slotCount
, sal_Int32
,
495 typelib_InterfaceTypeDescription
*)
497 Slot
* slots
= mapBlockToVtable(block
);
498 slots
[-2].fn
= 0; //null
499 slots
[-1].fn
= 0; //destructor
500 return slots
+ slotCount
;
503 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
504 Slot
** slots
, unsigned char * code
,
505 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
506 sal_Int32 functionCount
, sal_Int32 vTableOffset
)
508 (*slots
) -= functionCount
;
510 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
511 typelib_TypeDescription
* member
= 0;
512 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
514 switch (member
->eTypeClass
) {
515 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
519 code
, functionOffset
++, vTableOffset
,
520 bridges::cpp_uno::shared::isSimpleType(
522 typelib_InterfaceAttributeTypeDescription
* >(
523 member
)->pAttributeTypeRef
));
525 if (!reinterpret_cast<
526 typelib_InterfaceAttributeTypeDescription
* >(
530 code
= codeSnippet(code
, functionOffset
++, vTableOffset
, true);
534 case typelib_TypeClass_INTERFACE_METHOD
:
537 code
, functionOffset
++, vTableOffset
,
538 bridges::cpp_uno::shared::isSimpleType(
540 typelib_InterfaceMethodTypeDescription
* >(
541 member
)->pReturnTypeRef
));
548 TYPELIB_DANGER_RELEASE(member
);
553 void bridges::cpp_uno::shared::VtableFactory::flushCode(
554 unsigned char const *, unsigned char const *)
556 //TODO: IZ 25819 flush the instruction cache (there probably is OS support for this)
559 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */