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 .
23 #include <rtl/alloc.h>
24 #include <sal/log.hxx>
26 #include <com/sun/star/uno/genfunc.hxx>
27 #include "com/sun/star/uno/RuntimeException.hpp"
29 #include <typelib/typedescription.hxx>
32 #include "cppinterfaceproxy.hxx"
34 #include "vtablefactory.hxx"
41 using namespace ::osl
;
42 using namespace ::com::sun::star::uno
;
47 static typelib_TypeClass
cpp2uno_call(
48 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
49 const typelib_TypeDescription
* pMemberTypeDescr
,
50 typelib_TypeDescriptionReference
* pReturnTypeRef
,
51 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
52 long r8
, void ** pCallStack
,
53 sal_Int64
* pRegisterReturn
/* space for register return */ )
55 // pCallStack: ret, [return ptr], this, params
56 char * pTopStack
= (char *)(pCallStack
+ 0);
57 char * pCppStack
= pTopStack
;
58 #if OSL_DEBUG_LEVEL > 2
59 fprintf(stderr
, "cpp2uno_call\n");
62 typelib_TypeDescription
* pReturnTypeDescr
= 0;
64 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
66 void * pUnoReturn
= 0;
67 // complex return ptr: if != 0 && != pUnoReturn, reconversion need
68 void * pCppReturn
= 0;
72 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
74 #if OSL_DEBUG_LEVEL > 2
75 fprintf(stderr
, "simple return\n");
77 pUnoReturn
= pRegisterReturn
; // direct way for simple types
79 else // complex return via ptr (pCppReturn)
81 #if OSL_DEBUG_LEVEL > 2
82 fprintf(stderr
, "complex return\n");
84 pCppReturn
= (void *)r8
;
86 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
87 ? alloca( pReturnTypeDescr
->nSize
)
88 : pCppReturn
); // direct way
92 pCppStack
+= sizeof( void* );
95 static_assert( sizeof(void *) == sizeof(sal_Int32
),
96 "### unexpected size!" );
98 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
99 void ** pCppArgs
= pUnoArgs
+ nParams
;
100 // indices of values this have to be converted (interface conversion
102 sal_Int32
* pTempIndices
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
103 // type descriptions for reconversions
104 typelib_TypeDescription
** ppTempParamTypeDescr
=
105 (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
107 sal_Int32 nTempIndices
= 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 bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
118 switch (pParamTypeDescr
->eTypeClass
)
120 case typelib_TypeClass_BYTE
:
121 case typelib_TypeClass_BOOLEAN
:
122 pCppArgs
[nPos
] = pCppStack
+ 3;
123 pUnoArgs
[nPos
] = pCppStack
+ 3;
125 case typelib_TypeClass_CHAR
:
126 case typelib_TypeClass_SHORT
:
127 case typelib_TypeClass_UNSIGNED_SHORT
:
128 pCppArgs
[nPos
] = pCppStack
+ 2;
129 pUnoArgs
[nPos
] = pCppStack
+ 2;
131 case typelib_TypeClass_HYPER
:
132 case typelib_TypeClass_UNSIGNED_HYPER
:
133 case typelib_TypeClass_DOUBLE
:
134 pCppArgs
[nPos
] = pCppStack
;
135 pUnoArgs
[nPos
] = pCppStack
;
136 pCppStack
+= sizeof(sal_Int32
); // extra long
139 pCppArgs
[nPos
] = pCppStack
;
140 pUnoArgs
[nPos
] = pCppStack
;
144 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
146 else // ptr to complex value | ref
148 pCppArgs
[nPos
] = *(void **)pCppStack
;
150 if (! rParam
.bIn
) // is pure out
152 // uno out is unconstructed mem!
153 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
154 pTempIndices
[nTempIndices
] = nPos
;
155 // will be released at reconversion
156 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
159 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
162 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
163 *(void **)pCppStack
, pParamTypeDescr
,
164 pThis
->getBridge()->getCpp2Uno() );
165 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
166 // will be released at reconversion
167 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
171 pUnoArgs
[nPos
] = *(void **)pCppStack
;
173 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
176 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
180 uno_Any aUnoExc
; // Any will be constructed by callee
181 uno_Any
* pUnoExc
= &aUnoExc
;
183 #if OSL_DEBUG_LEVEL > 2
184 fprintf(stderr
, "before dispatch\n");
186 // invoke uno dispatch call
187 (*pThis
->getUnoI()->pDispatcher
)(
188 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
190 #if OSL_DEBUG_LEVEL > 2
191 fprintf(stderr
, "after dispatch\n");
194 // in case an exception occurred...
197 // destruct temporary in/inout params
198 for ( ; nTempIndices
--; )
200 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
202 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
203 uno_destructData( pUnoArgs
[nIndex
],
204 ppTempParamTypeDescr
[nTempIndices
], 0 );
205 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
207 if (pReturnTypeDescr
)
208 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
210 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
,
211 pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
213 return typelib_TypeClass_VOID
;
215 else // else no exception occurred...
218 for ( ; nTempIndices
--; )
220 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
221 typelib_TypeDescription
* pParamTypeDescr
=
222 ppTempParamTypeDescr
[nTempIndices
];
224 if (pParams
[nIndex
].bOut
) // inout/out
226 // convert and assign
227 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
,
229 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
],
230 pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
232 // destroy temp uno param
233 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
235 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
238 if (pCppReturn
) // has complex return
240 if (pUnoReturn
!= pCppReturn
) // needs reconversion
242 uno_copyAndConvertData( pCppReturn
, pUnoReturn
,
243 pReturnTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
244 // destroy temp uno return
245 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
247 // complex return ptr is set to return reg
248 *(void **)pRegisterReturn
= pCppReturn
;
250 if (pReturnTypeDescr
)
252 typelib_TypeClass eRet
=
253 (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
254 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
258 return typelib_TypeClass_VOID
;
263 static typelib_TypeClass
cpp_mediate(
264 sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
,
266 sal_Int64
* pRegisterReturn
/* space for register return */ )
268 void ** pCallStack
= (void**)(sp
);
269 #if OSL_DEBUG_LEVEL > 2
270 fprintf(stderr
, "cpp_mediate with\n");
271 fprintf(stderr
, "%x %x\n", nFunctionIndex
, nVtableOffset
);
272 fprintf(stderr
, "and %x %x\n", pCallStack
, pRegisterReturn
);
273 fprintf(stderr
, "and %x %x\n", pCallStack
[0], pCallStack
[1]);
275 static_assert( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
277 void *pThis
= pCallStack
[0];
279 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
281 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
=
282 bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
285 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
287 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
291 "illegal " << OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
292 << " vtable index " << nFunctionIndex
<< "/"
293 << pTypeDescr
->nMapFunctionIndexToMemberIndex
);
294 throw RuntimeException(
295 ("illegal " + OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
296 + " vtable index " + OUString::number(nFunctionIndex
) + "/"
297 + OUString::number(pTypeDescr
->nMapFunctionIndexToMemberIndex
)),
298 (XInterface
*)pCppI
);
301 // determine called method
302 assert(nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
);
303 sal_Int32 nMemberPos
=
304 pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
305 assert(nMemberPos
< pTypeDescr
->nAllMembers
);
307 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
309 typelib_TypeClass eRet
;
310 switch (aMemberDescr
.get()->eTypeClass
)
312 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
314 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] ==
319 pCppI
, aMemberDescr
.get(),
320 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
322 r8
, pCallStack
, pRegisterReturn
);
327 typelib_MethodParameter aParam
;
329 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
330 aParam
.bIn
= sal_True
;
331 aParam
.bOut
= sal_False
;
334 pCppI
, aMemberDescr
.get(),
335 0, // indicates void return
337 r8
, pCallStack
, pRegisterReturn
);
341 case typelib_TypeClass_INTERFACE_METHOD
:
344 switch (nFunctionIndex
)
347 pCppI
->acquireProxy(); // non virtual call!
348 eRet
= typelib_TypeClass_VOID
;
351 pCppI
->releaseProxy(); // non virtual call!
352 eRet
= typelib_TypeClass_VOID
;
354 case 0: // queryInterface() opt
356 typelib_TypeDescription
* pTD
= 0;
357 TYPELIB_DANGER_GET(&pTD
,
358 reinterpret_cast<Type
*>(pCallStack
[1])->getTypeLibType());
361 XInterface
* pInterface
= 0;
362 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
363 pCppI
->getBridge()->getCppEnv(),
364 (void **)&pInterface
, pCppI
->getOid().pData
,
365 (typelib_InterfaceTypeDescription
*)pTD
);
370 reinterpret_cast< uno_Any
* >( r8
),
371 &pInterface
, pTD
, cpp_acquire
);
372 pInterface
->release();
373 TYPELIB_DANGER_RELEASE( pTD
);
374 *(void **)pRegisterReturn
= (void*)r8
;
375 eRet
= typelib_TypeClass_ANY
;
378 TYPELIB_DANGER_RELEASE( pTD
);
380 } // else perform queryInterface()
383 pCppI
, aMemberDescr
.get(),
384 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
385 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
386 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
387 r8
, pCallStack
, pRegisterReturn
);
393 throw RuntimeException( "no member description found!", (XInterface
*)pCppI
);
402 * is called on incoming vtable calls
403 * (called by asm snippets)
406 extern "C" sal_Int64
cpp_vtable_call( long firstonstack
)
408 register long d0
asm("d0");
409 long functionIndex
= d0
;
411 register long a1
asm("a1");
414 register long d1
asm("d1");
415 long vtableOffset
= d1
;
417 long sp
= (long)&firstonstack
;
419 sal_Int64 nRegReturn
;
420 cpp_mediate( functionIndex
, vtableOffset
, sp
, r8
, &nRegReturn
);
426 const int codeSnippetSize
= 20;
428 //some m68k info: http://www2.biglobe.ne.jp/~inaba/trampolines.html
430 unsigned char *codeSnippet(unsigned char* code
, sal_Int32 functionIndex
,
431 sal_Int32 vtableOffset
)
433 unsigned char * p
= code
;
434 *(short *)&p
[0] = 0x203C; //movel functionIndex,d0
435 *(long *)&p
[2] = functionIndex
;
436 *(short *)&p
[6] = 0x223C; //movel functionIndex,d1
437 *(long *)&p
[8] = vtableOffset
;
438 *(short *)&p
[12] = 0x4EF9; //jmp cpp_vtable_call
439 *(long *)&p
[14] = (long)&cpp_vtable_call
;
440 *(short *)&p
[18] = 0x4E71; //nop
441 return code
+ codeSnippetSize
;
445 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void const * fn
; };
447 bridges::cpp_uno::shared::VtableFactory::Slot
*
448 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
450 return static_cast< Slot
* >(block
) + 2;
453 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
456 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
460 // Some dummy type whose RTTI is used in the synthesized proxy vtables to make uses of dynamic_cast
461 // on such proxy objects not crash:
465 bridges::cpp_uno::shared::VtableFactory::Slot
*
466 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
467 void * block
, sal_Int32 slotCount
, sal_Int32
,
468 typelib_InterfaceTypeDescription
*)
470 Slot
* slots
= mapBlockToVtable(block
);
472 slots
[-1].fn
= &typeid(ProxyRtti
);
473 return slots
+ slotCount
;
476 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
477 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
478 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
479 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
481 (*slots
) -= functionCount
;
483 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
)
485 typelib_TypeDescription
* member
= 0;
486 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
488 switch (member
->eTypeClass
)
490 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
492 (s
++)->fn
= code
+ writetoexecdiff
;
493 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
495 if (!reinterpret_cast<
496 typelib_InterfaceAttributeTypeDescription
* >(
499 (s
++)->fn
= code
+ writetoexecdiff
;
500 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
503 case typelib_TypeClass_INTERFACE_METHOD
:
505 (s
++)->fn
= code
+ writetoexecdiff
;
507 typelib_InterfaceMethodTypeDescription
*pMethodTD
=
509 typelib_InterfaceMethodTypeDescription
* >(member
);
511 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
518 TYPELIB_DANGER_RELEASE(member
);
523 void bridges::cpp_uno::shared::VtableFactory::flushCode(
524 unsigned char const * /*beg*/, unsigned char const * /*end*/)
528 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */