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"
30 using namespace com::sun::star::uno
;
35 static typelib_TypeClass
cpp2uno_call(
36 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
37 const typelib_TypeDescription
* pMemberTypeDescr
,
38 typelib_TypeDescriptionReference
* pReturnTypeRef
, // 0 indicates void return
39 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
41 sal_Int64
* pRegisterReturn
/* space for register return */ )
43 // pCallStack: [ret ptr], this, params
44 char * pCppStack
= (char *)pCallStack
;
47 typelib_TypeDescription
* pReturnTypeDescr
= 0;
49 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
51 void * pUnoReturn
= 0;
52 void * pCppReturn
= 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
56 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
57 pUnoReturn
= pRegisterReturn
; // direct way for simple types
58 else // complex return via ptr (pCppReturn)
60 pCppReturn
= *(void**)pCppStack
;
61 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType(
63 ? alloca( pReturnTypeDescr
->nSize
)
64 : pCppReturn
); // direct way
65 pCppStack
+= sizeof( void* );
69 pCppStack
+= sizeof( void* );
72 static_assert(sizeof(void *) == sizeof(sal_Int32
), "### unexpected size!");
74 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
75 void ** pCppArgs
= pUnoArgs
+ nParams
;
76 // indices of values this have to be converted (interface conversion cpp<=>uno)
77 sal_Int32
* pTempIndices
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
78 // type descriptions for reconversions
79 typelib_TypeDescription
** ppTempParamTypeDescr
= (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
81 sal_Int32 nTempIndices
= 0;
83 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
85 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
86 typelib_TypeDescription
* pParamTypeDescr
= 0;
87 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
89 if (!rParam
.bOut
&& bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
)) // value
91 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack
, pParamTypeDescr
);
92 switch (pParamTypeDescr
->eTypeClass
)
94 case typelib_TypeClass_HYPER
:
95 case typelib_TypeClass_UNSIGNED_HYPER
:
96 case typelib_TypeClass_DOUBLE
:
98 if ((reinterpret_cast< long >(pCppStack
) & 7) != 0)
100 static_assert(sizeof (double) == sizeof (sal_Int64
), "boo");
101 void * pDest
= alloca( sizeof (sal_Int64
) );
102 *reinterpret_cast< sal_Int32
* >(pDest
) =
103 *reinterpret_cast< sal_Int32
const * >(pCppStack
);
104 *(reinterpret_cast< sal_Int32
* >(pDest
) + 1) =
105 *(reinterpret_cast< sal_Int32
const * >(pCppStack
) + 1);
106 pCppArgs
[nPos
] = pUnoArgs
[nPos
] = pDest
;
108 pCppStack
+= sizeof (sal_Int32
); // extra long
115 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
117 else // ptr to complex value | ref
119 pCppArgs
[nPos
] = *(void **)pCppStack
;
121 if (! rParam
.bIn
) // is pure out
123 // uno out is unconstructed mem!
124 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
125 pTempIndices
[nTempIndices
] = nPos
;
126 // will be released at reconversion
127 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
130 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
133 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
134 *(void **)pCppStack
, pParamTypeDescr
,
135 pThis
->getBridge()->getCpp2Uno() );
136 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
137 // will be released at reconversion
138 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
142 pUnoArgs
[nPos
] = *(void **)pCppStack
;
144 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
147 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
151 uno_Any aUnoExc
; // Any will be constructed by callee
152 uno_Any
* pUnoExc
= &aUnoExc
;
154 // invoke uno dispatch call
155 (*pThis
->getUnoI()->pDispatcher
)(pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
157 // in case an exception occurred...
160 // destruct temporary in/inout params
161 for ( ; nTempIndices
--; )
163 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
165 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
166 uno_destructData( pUnoArgs
[nIndex
], ppTempParamTypeDescr
[nTempIndices
], 0 );
167 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
169 if (pReturnTypeDescr
)
170 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
171 CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc
, pThis
->getBridge()->getUno2Cpp() );
172 // has to destruct the any
174 return typelib_TypeClass_VOID
;
176 else // else no exception occurred...
179 for ( ; nTempIndices
--; )
181 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
182 typelib_TypeDescription
* pParamTypeDescr
= ppTempParamTypeDescr
[nTempIndices
];
184 if (pParams
[nIndex
].bOut
) // inout/out
186 // convert and assign
187 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
, cpp_release
);
188 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
], pParamTypeDescr
,
189 pThis
->getBridge()->getUno2Cpp() );
191 // destroy temp uno param
192 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
194 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
197 if (pCppReturn
) // has complex return
199 if (pUnoReturn
!= pCppReturn
) // needs reconversion
201 uno_copyAndConvertData( pCppReturn
, pUnoReturn
, pReturnTypeDescr
,
202 pThis
->getBridge()->getUno2Cpp() );
203 // destroy temp uno return
204 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
206 // complex return ptr is set to eax
207 *(void **)pRegisterReturn
= pCppReturn
;
209 if (pReturnTypeDescr
)
211 typelib_TypeClass eRet
= (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
212 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
216 return typelib_TypeClass_VOID
;
221 static typelib_TypeClass
cpp_mediate(
222 sal_Int32 nFunctionIndex
,
223 sal_Int32 nVtableOffset
,
225 sal_Int64
* pRegisterReturn
/* space for register return */ )
227 static_assert(sizeof(sal_Int32
)==sizeof(void *), "### unexpected!");
229 // pCallStack: this, params
230 // eventual [ret*] lies at pCallStack -1
231 // so count down pCallStack by one to keep it simple
232 // pCallStack: this, params
233 // eventual [ret*] lies at pCallStack -1
234 // so count down pCallStack by one to keep it simple
235 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
236 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
237 static_cast< char * >(*pCallStack
) - nVtableOffset
);
238 if ((nFunctionIndex
& 0x80000000) != 0) {
239 nFunctionIndex
&= 0x7FFFFFFF;
243 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
245 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
249 "illegal " << OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
250 << " vtable index " << nFunctionIndex
<< "/"
251 << pTypeDescr
->nMapFunctionIndexToMemberIndex
);
252 throw RuntimeException(
253 ("illegal " + OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
254 + " vtable index " + OUString::number(nFunctionIndex
) + "/"
255 + OUString::number(pTypeDescr
->nMapFunctionIndexToMemberIndex
)),
256 (XInterface
*)pCppI
);
259 // determine called method
260 sal_Int32 nMemberPos
= pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
261 assert(nMemberPos
< pTypeDescr
->nAllMembers
);
263 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
265 #if defined BRIDGES_DEBUG
266 OString
cstr( OUStringToOString( aMemberDescr
.get()->pTypeName
, RTL_TEXTENCODING_ASCII_US
) );
267 fprintf( stderr
, "calling %s, nFunctionIndex=%d\n", cstr
.getStr(), nFunctionIndex
);
270 typelib_TypeClass eRet
;
271 switch (aMemberDescr
.get()->eTypeClass
)
273 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
275 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] == nFunctionIndex
)
279 pCppI
, aMemberDescr
.get(),
280 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
282 pCallStack
, pRegisterReturn
);
287 typelib_MethodParameter aParam
;
289 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
290 aParam
.bIn
= sal_True
;
291 aParam
.bOut
= sal_False
;
294 pCppI
, aMemberDescr
.get(),
295 0, // indicates void return
297 pCallStack
, pRegisterReturn
);
301 case typelib_TypeClass_INTERFACE_METHOD
:
304 switch (nFunctionIndex
)
307 pCppI
->acquireProxy(); // non virtual call!
308 eRet
= typelib_TypeClass_VOID
;
311 pCppI
->releaseProxy(); // non virtual call!
312 eRet
= typelib_TypeClass_VOID
;
314 case 0: // queryInterface() opt
316 typelib_TypeDescription
* pTD
= 0;
317 TYPELIB_DANGER_GET( &pTD
, reinterpret_cast< Type
* >( pCallStack
[2] )->getTypeLibType() );
320 XInterface
* pInterface
= 0;
321 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
322 pCppI
->getBridge()->getCppEnv(),
323 (void **)&pInterface
, pCppI
->getOid().pData
, (typelib_InterfaceTypeDescription
*)pTD
);
328 reinterpret_cast< uno_Any
* >( pCallStack
[0] ),
329 &pInterface
, pTD
, cpp_acquire
);
330 pInterface
->release();
331 TYPELIB_DANGER_RELEASE( pTD
);
332 *(void **)pRegisterReturn
= pCallStack
[0];
333 eRet
= typelib_TypeClass_ANY
;
336 TYPELIB_DANGER_RELEASE( pTD
);
338 } // else perform queryInterface()
341 pCppI
, aMemberDescr
.get(),
342 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
343 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
344 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
345 pCallStack
, pRegisterReturn
);
351 throw RuntimeException( "no member description found!", (XInterface
*)pCppI
);
359 * is called on incoming vtable calls
360 * (called by asm snippets)
362 static void cpp_vtable_call()
364 sal_Int64 nRegReturn
;
369 void * pRegReturn
= &nRegReturn
;
371 __asm__( "st %%i0, %0\n\t"
374 : : "m"(nFunctionIndex
), "m"(pCallStack
), "m"(vTableOffset
) );
376 // fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
379 const sal_Bool bComplex
= (nFunctionIndex
& 0x80000000) ? sal_True
: sal_False
;
380 typelib_TypeClass aType
=
381 cpp_mediate( nFunctionIndex
, vTableOffset
, pCallStack
+17, (sal_Int64
*)&nRegReturn
);
385 case typelib_TypeClass_BOOLEAN
:
386 case typelib_TypeClass_BYTE
:
387 __asm__( "ld %0, %%l0\n\t"
388 "ldsb [%%l0], %%i0\n"
389 : : "m"(pRegReturn
) );
391 case typelib_TypeClass_CHAR
:
392 case typelib_TypeClass_SHORT
:
393 case typelib_TypeClass_UNSIGNED_SHORT
:
394 __asm__( "ld %0, %%l0\n\t"
395 "ldsh [%%l0], %%i0\n"
396 : : "m"(pRegReturn
) );
398 case typelib_TypeClass_HYPER
:
399 case typelib_TypeClass_UNSIGNED_HYPER
:
400 __asm__( "ld %0, %%l0\n\t"
401 "ld [%%l0], %%i0\n\t"
402 "add %%l0, 4, %%l0\n\t"
403 "ld [%%l0], %%i1\n\t"
404 : : "m"(pRegReturn
) );
407 case typelib_TypeClass_FLOAT
:
408 __asm__( "ld %0, %%l0\n\t"
410 : : "m"(pRegReturn
) );
412 case typelib_TypeClass_DOUBLE
:
413 __asm__( "ld %0, %%l0\n\t"
415 : : "m"(pRegReturn
) );
417 case typelib_TypeClass_VOID
:
420 __asm__( "ld %0, %%l0\n\t"
422 : : "m"(pRegReturn
) );
428 __asm__( "add %i7, 4, %i7\n\t" );
429 // after call to complex return valued function there is an unimp instruction
435 int const codeSnippetSize
= 56;
436 unsigned char * codeSnippet(
437 unsigned char * code
, sal_Int32 functionIndex
, sal_Int32 vtableOffset
,
440 sal_uInt32 index
= functionIndex
;
441 if (!simpleRetType
) {
444 unsigned int * p
= reinterpret_cast< unsigned int * >(code
);
445 static_assert(sizeof (unsigned int) == 4, "boo");
458 // sethi %hi(index), %o0:
459 *p
++ = 0x11000000 | (index
>> 10);
460 // or %o0, %lo(index), %o0:
461 *p
++ = 0x90122000 | (index
& 0x3FF);
462 // sethi %hi(vtableOffset), %o2:
463 *p
++ = 0x15000000 | (vtableOffset
>> 10);
464 // or %o2, %lo(vtableOffset), %o2:
465 *p
++ = 0x9412A000 | (vtableOffset
& 0x3FF);
466 // sethi %hi(cpp_vtable_call), %o3:
467 *p
++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) >> 10);
468 // or %o3, %lo(cpp_vtable_call), %o3:
469 *p
++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call
) & 0x3FF);
474 assert(reinterpret_cast< unsigned char * >(p
) - code
<= codeSnippetSize
);
475 return code
+ codeSnippetSize
;
480 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
482 bridges::cpp_uno::shared::VtableFactory::Slot
*
483 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
485 return static_cast< Slot
* >(block
) + 2;
488 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
491 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
494 bridges::cpp_uno::shared::VtableFactory::Slot
*
495 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
496 void * block
, sal_Int32 slotCount
, sal_Int32
,
497 typelib_InterfaceTypeDescription
*)
499 Slot
* slots
= mapBlockToVtable(block
);
500 slots
[-2].fn
= 0; //null
501 slots
[-1].fn
= 0; //destructor
502 return slots
+ slotCount
;
505 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
506 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
507 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
508 sal_Int32 functionCount
, sal_Int32 vTableOffset
)
510 (*slots
) -= functionCount
;
512 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
) {
513 typelib_TypeDescription
* member
= 0;
514 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
516 switch (member
->eTypeClass
) {
517 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
519 (s
++)->fn
= code
+ writetoexecdiff
;
521 code
, functionOffset
++, vTableOffset
,
522 bridges::cpp_uno::shared::isSimpleType(
524 typelib_InterfaceAttributeTypeDescription
* >(
525 member
)->pAttributeTypeRef
));
527 if (!reinterpret_cast<
528 typelib_InterfaceAttributeTypeDescription
* >(
531 (s
++)->fn
= code
+ writetoexecdiff
;
532 code
= codeSnippet(code
, functionOffset
++, vTableOffset
, true);
536 case typelib_TypeClass_INTERFACE_METHOD
:
537 (s
++)->fn
= code
+ writetoexecdiff
;
539 code
, functionOffset
++, vTableOffset
,
540 bridges::cpp_uno::shared::isSimpleType(
542 typelib_InterfaceMethodTypeDescription
* >(
543 member
)->pReturnTypeRef
));
550 TYPELIB_DANGER_RELEASE(member
);
555 // use flush code from cc50_solaris_sparc
557 extern "C" void doFlushCode(unsigned long address
, unsigned long count
);
559 void bridges::cpp_uno::shared::VtableFactory::flushCode(
560 unsigned char const * begin
, unsigned char const * end
)
562 unsigned long n
= end
- begin
;
564 unsigned long adr
= reinterpret_cast< unsigned long >(begin
);
565 unsigned long off
= adr
& 7;
566 doFlushCode(adr
- off
, (n
+ off
+ 7) >> 3);
570 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */