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 .
22 #include <rtl/alloc.h>
23 #include <sal/log.hxx>
25 #include <com/sun/star/uno/genfunc.hxx>
26 #include "com/sun/star/uno/RuntimeException.hpp"
28 #include <typelib/typedescription.hxx>
31 #include "cppinterfaceproxy.hxx"
33 #include "vtablefactory.hxx"
40 using namespace ::osl
;
41 using namespace ::com::sun::star::uno
;
46 static typelib_TypeClass
cpp2uno_call(
47 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
48 const typelib_TypeDescription
* pMemberTypeDescr
,
49 typelib_TypeDescriptionReference
* pReturnTypeRef
,
50 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
51 long r8
, void ** pCallStack
,
52 sal_Int64
* pRegisterReturn
/* space for register return */ )
54 // pCallStack: ret, [return ptr], this, params
55 char * pTopStack
= (char *)(pCallStack
+ 0);
56 char * pCppStack
= pTopStack
;
57 #if OSL_DEBUG_LEVEL > 2
58 fprintf(stderr
, "cpp2uno_call\n");
61 typelib_TypeDescription
* pReturnTypeDescr
= 0;
63 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
65 void * pUnoReturn
= 0;
66 // complex return ptr: if != 0 && != pUnoReturn, reconversion need
67 void * pCppReturn
= 0;
71 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
73 #if OSL_DEBUG_LEVEL > 2
74 fprintf(stderr
, "simple return\n");
76 pUnoReturn
= pRegisterReturn
; // direct way for simple types
78 else // complex return via ptr (pCppReturn)
80 #if OSL_DEBUG_LEVEL > 2
81 fprintf(stderr
, "complex return\n");
83 pCppReturn
= (void *)r8
;
85 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
86 ? alloca( pReturnTypeDescr
->nSize
)
87 : pCppReturn
); // direct way
91 pCppStack
+= sizeof( void* );
94 static_assert( sizeof(void *) == sizeof(sal_Int32
),
95 "### unexpected size!" );
97 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
98 void ** pCppArgs
= pUnoArgs
+ nParams
;
99 // indices of values this have to be converted (interface conversion
101 sal_Int32
* pTempIndices
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
102 // type descriptions for reconversions
103 typelib_TypeDescription
** ppTempParamTypeDescr
=
104 (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
106 sal_Int32 nTempIndices
= 0;
108 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
110 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
111 typelib_TypeDescription
* pParamTypeDescr
= 0;
112 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
115 bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
117 switch (pParamTypeDescr
->eTypeClass
)
119 case typelib_TypeClass_BYTE
:
120 case typelib_TypeClass_BOOLEAN
:
121 pCppArgs
[nPos
] = pCppStack
+ 3;
122 pUnoArgs
[nPos
] = pCppStack
+ 3;
124 case typelib_TypeClass_CHAR
:
125 case typelib_TypeClass_SHORT
:
126 case typelib_TypeClass_UNSIGNED_SHORT
:
127 pCppArgs
[nPos
] = pCppStack
+ 2;
128 pUnoArgs
[nPos
] = pCppStack
+ 2;
130 case typelib_TypeClass_HYPER
:
131 case typelib_TypeClass_UNSIGNED_HYPER
:
132 case typelib_TypeClass_DOUBLE
:
133 pCppArgs
[nPos
] = pCppStack
;
134 pUnoArgs
[nPos
] = pCppStack
;
135 pCppStack
+= sizeof(sal_Int32
); // extra long
138 pCppArgs
[nPos
] = pCppStack
;
139 pUnoArgs
[nPos
] = pCppStack
;
143 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
145 else // ptr to complex value | ref
147 pCppArgs
[nPos
] = *(void **)pCppStack
;
149 if (! rParam
.bIn
) // is pure out
151 // uno out is unconstructed mem!
152 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
153 pTempIndices
[nTempIndices
] = nPos
;
154 // will be released at reconversion
155 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
158 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
161 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
162 *(void **)pCppStack
, pParamTypeDescr
,
163 pThis
->getBridge()->getCpp2Uno() );
164 pTempIndices
[nTempIndices
] = nPos
; // has to be reconverted
165 // will be released at reconversion
166 ppTempParamTypeDescr
[nTempIndices
++] = pParamTypeDescr
;
170 pUnoArgs
[nPos
] = *(void **)pCppStack
;
172 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
175 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
179 uno_Any aUnoExc
; // Any will be constructed by callee
180 uno_Any
* pUnoExc
= &aUnoExc
;
182 #if OSL_DEBUG_LEVEL > 2
183 fprintf(stderr
, "before dispatch\n");
185 // invoke uno dispatch call
186 (*pThis
->getUnoI()->pDispatcher
)(
187 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
189 #if OSL_DEBUG_LEVEL > 2
190 fprintf(stderr
, "after dispatch\n");
193 // in case an exception occurred...
196 // destruct temporary in/inout params
197 for ( ; nTempIndices
--; )
199 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
201 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
202 uno_destructData( pUnoArgs
[nIndex
],
203 ppTempParamTypeDescr
[nTempIndices
], 0 );
204 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndices
] );
206 if (pReturnTypeDescr
)
207 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
209 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
,
210 pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
212 return typelib_TypeClass_VOID
;
214 else // else no exception occurred...
217 for ( ; nTempIndices
--; )
219 sal_Int32 nIndex
= pTempIndices
[nTempIndices
];
220 typelib_TypeDescription
* pParamTypeDescr
=
221 ppTempParamTypeDescr
[nTempIndices
];
223 if (pParams
[nIndex
].bOut
) // inout/out
225 // convert and assign
226 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
,
228 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
],
229 pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
231 // destroy temp uno param
232 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
234 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
237 if (pCppReturn
) // has complex return
239 if (pUnoReturn
!= pCppReturn
) // needs reconversion
241 uno_copyAndConvertData( pCppReturn
, pUnoReturn
,
242 pReturnTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
243 // destroy temp uno return
244 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
246 // complex return ptr is set to return reg
247 *(void **)pRegisterReturn
= pCppReturn
;
249 if (pReturnTypeDescr
)
251 typelib_TypeClass eRet
=
252 (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
253 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
257 return typelib_TypeClass_VOID
;
262 static typelib_TypeClass
cpp_mediate(
263 sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
,
265 sal_Int64
* pRegisterReturn
/* space for register return */ )
267 void ** pCallStack
= (void**)(sp
);
268 #if OSL_DEBUG_LEVEL > 2
269 fprintf(stderr
, "cpp_mediate with\n");
270 fprintf(stderr
, "%x %x\n", nFunctionIndex
, nVtableOffset
);
271 fprintf(stderr
, "and %x %x\n", pCallStack
, pRegisterReturn
);
272 fprintf(stderr
, "and %x %x\n", pCallStack
[0], pCallStack
[1]);
274 static_assert( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
276 void *pThis
= pCallStack
[0];
278 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
280 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
=
281 bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
284 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
286 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
290 "illegal " << OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
291 << " vtable index " << nFunctionIndex
<< "/"
292 << pTypeDescr
->nMapFunctionIndexToMemberIndex
);
293 throw RuntimeException(
294 ("illegal " + OUString::unacquired(&pTypeDescr
->aBase
.pTypeName
)
295 + " vtable index " + OUString::number(nFunctionIndex
) + "/"
296 + OUString::number(pTypeDescr
->nMapFunctionIndexToMemberIndex
)),
297 (XInterface
*)pCppI
);
300 // determine called method
301 assert(nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
);
302 sal_Int32 nMemberPos
=
303 pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
304 assert(nMemberPos
< pTypeDescr
->nAllMembers
);
306 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
308 typelib_TypeClass eRet
;
309 switch (aMemberDescr
.get()->eTypeClass
)
311 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
313 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] ==
318 pCppI
, aMemberDescr
.get(),
319 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
321 r8
, pCallStack
, pRegisterReturn
);
326 typelib_MethodParameter aParam
;
328 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
329 aParam
.bIn
= sal_True
;
330 aParam
.bOut
= sal_False
;
333 pCppI
, aMemberDescr
.get(),
334 0, // indicates void return
336 r8
, pCallStack
, pRegisterReturn
);
340 case typelib_TypeClass_INTERFACE_METHOD
:
343 switch (nFunctionIndex
)
346 pCppI
->acquireProxy(); // non virtual call!
347 eRet
= typelib_TypeClass_VOID
;
350 pCppI
->releaseProxy(); // non virtual call!
351 eRet
= typelib_TypeClass_VOID
;
353 case 0: // queryInterface() opt
355 typelib_TypeDescription
* pTD
= 0;
356 TYPELIB_DANGER_GET(&pTD
,
357 reinterpret_cast<Type
*>(pCallStack
[1])->getTypeLibType());
360 XInterface
* pInterface
= 0;
361 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
362 pCppI
->getBridge()->getCppEnv(),
363 (void **)&pInterface
, pCppI
->getOid().pData
,
364 (typelib_InterfaceTypeDescription
*)pTD
);
369 reinterpret_cast< uno_Any
* >( r8
),
370 &pInterface
, pTD
, cpp_acquire
);
371 pInterface
->release();
372 TYPELIB_DANGER_RELEASE( pTD
);
373 *(void **)pRegisterReturn
= (void*)r8
;
374 eRet
= typelib_TypeClass_ANY
;
377 TYPELIB_DANGER_RELEASE( pTD
);
379 } // else perform queryInterface()
382 pCppI
, aMemberDescr
.get(),
383 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
384 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
385 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
386 r8
, pCallStack
, pRegisterReturn
);
392 throw RuntimeException( "no member description found!", (XInterface
*)pCppI
);
401 * is called on incoming vtable calls
402 * (called by asm snippets)
405 extern "C" sal_Int64
cpp_vtable_call( long firstonstack
)
407 register long d0
asm("d0");
408 long functionIndex
= d0
;
410 register long a1
asm("a1");
413 register long d1
asm("d1");
414 long vtableOffset
= d1
;
416 long sp
= (long)&firstonstack
;
418 sal_Int64 nRegReturn
;
419 cpp_mediate( functionIndex
, vtableOffset
, sp
, r8
, &nRegReturn
);
425 const int codeSnippetSize
= 20;
427 //some m68k info: http://www2.biglobe.ne.jp/~inaba/trampolines.html
429 unsigned char *codeSnippet(unsigned char* code
, sal_Int32 functionIndex
,
430 sal_Int32 vtableOffset
)
432 unsigned char * p
= code
;
433 *(short *)&p
[0] = 0x203C; //movel functionIndex,d0
434 *(long *)&p
[2] = functionIndex
;
435 *(short *)&p
[6] = 0x223C; //movel functionIndex,d1
436 *(long *)&p
[8] = vtableOffset
;
437 *(short *)&p
[12] = 0x4EF9; //jmp cpp_vtable_call
438 *(long *)&p
[14] = (long)&cpp_vtable_call
;
439 *(short *)&p
[18] = 0x4E71; //nop
440 return code
+ codeSnippetSize
;
444 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
446 bridges::cpp_uno::shared::VtableFactory::Slot
*
447 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
449 return static_cast< Slot
* >(block
) + 2;
452 std::size_t bridges::cpp_uno::shared::VtableFactory::getBlockSize(
455 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
458 bridges::cpp_uno::shared::VtableFactory::Slot
*
459 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
460 void * block
, sal_Int32 slotCount
, sal_Int32
,
461 typelib_InterfaceTypeDescription
*)
463 Slot
* slots
= mapBlockToVtable(block
);
466 return slots
+ slotCount
;
469 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
470 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
471 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
472 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
474 (*slots
) -= functionCount
;
476 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
)
478 typelib_TypeDescription
* member
= 0;
479 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
481 switch (member
->eTypeClass
)
483 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
485 (s
++)->fn
= code
+ writetoexecdiff
;
486 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
488 if (!reinterpret_cast<
489 typelib_InterfaceAttributeTypeDescription
* >(
492 (s
++)->fn
= code
+ writetoexecdiff
;
493 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
496 case typelib_TypeClass_INTERFACE_METHOD
:
498 (s
++)->fn
= code
+ writetoexecdiff
;
500 typelib_InterfaceMethodTypeDescription
*pMethodTD
=
502 typelib_InterfaceMethodTypeDescription
* >(member
);
504 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
511 TYPELIB_DANGER_RELEASE(member
);
516 void bridges::cpp_uno::shared::VtableFactory::flushCode(
517 unsigned char const * /*beg*/, unsigned char const * /*end*/)
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */