1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <boost/unordered_map.hpp>
32 #include <rtl/alloc.h>
33 #include <osl/mutex.hxx>
35 #include <com/sun/star/uno/genfunc.hxx>
36 #include "com/sun/star/uno/RuntimeException.hpp"
38 #include <typelib/typedescription.hxx>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
50 using namespace ::osl
;
51 using namespace ::rtl
;
52 using namespace ::com::sun::star::uno
;
57 static typelib_TypeClass
cpp2uno_call(
58 bridges::cpp_uno::shared::CppInterfaceProxy
* pThis
,
59 const typelib_TypeDescription
* pMemberTypeDescr
,
60 typelib_TypeDescriptionReference
* pReturnTypeRef
,
61 sal_Int32 nParams
, typelib_MethodParameter
* pParams
,
62 long r8
, void ** pCallStack
,
63 sal_Int64
* pRegisterReturn
/* space for register return */ )
65 // pCallStack: ret, [return ptr], this, params
66 char * pTopStack
= (char *)(pCallStack
+ 0);
67 char * pCppStack
= pTopStack
;
68 #if OSL_DEBUG_LEVEL > 2
69 fprintf(stderr
, "cpp2uno_call\n");
72 typelib_TypeDescription
* pReturnTypeDescr
= 0;
74 TYPELIB_DANGER_GET( &pReturnTypeDescr
, pReturnTypeRef
);
76 void * pUnoReturn
= 0;
77 // complex return ptr: if != 0 && != pUnoReturn, reconversion need
78 void * pCppReturn
= 0;
82 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr
))
84 #if OSL_DEBUG_LEVEL > 2
85 fprintf(stderr
, "simple return\n");
87 pUnoReturn
= pRegisterReturn
; // direct way for simple types
89 else // complex return via ptr (pCppReturn)
91 #if OSL_DEBUG_LEVEL > 2
92 fprintf(stderr
, "complex return\n");
94 pCppReturn
= (void *)r8
;
96 pUnoReturn
= (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr
)
97 ? alloca( pReturnTypeDescr
->nSize
)
98 : pCppReturn
); // direct way
102 pCppStack
+= sizeof( void* );
105 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32
),
106 "### unexpected size!" );
108 void ** pUnoArgs
= (void **)alloca( 4 * sizeof(void *) * nParams
);
109 void ** pCppArgs
= pUnoArgs
+ nParams
;
110 // indizes of values this have to be converted (interface conversion
112 sal_Int32
* pTempIndizes
= (sal_Int32
*)(pUnoArgs
+ (2 * nParams
));
113 // type descriptions for reconversions
114 typelib_TypeDescription
** ppTempParamTypeDescr
=
115 (typelib_TypeDescription
**)(pUnoArgs
+ (3 * nParams
));
117 sal_Int32 nTempIndizes
= 0;
119 for ( sal_Int32 nPos
= 0; nPos
< nParams
; ++nPos
)
121 const typelib_MethodParameter
& rParam
= pParams
[nPos
];
122 typelib_TypeDescription
* pParamTypeDescr
= 0;
123 TYPELIB_DANGER_GET( &pParamTypeDescr
, rParam
.pTypeRef
);
126 bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr
))
128 switch (pParamTypeDescr
->eTypeClass
)
130 case typelib_TypeClass_BYTE
:
131 case typelib_TypeClass_BOOLEAN
:
132 pCppArgs
[nPos
] = pCppStack
+ 3;
133 pUnoArgs
[nPos
] = pCppStack
+ 3;
135 case typelib_TypeClass_CHAR
:
136 case typelib_TypeClass_SHORT
:
137 case typelib_TypeClass_UNSIGNED_SHORT
:
138 pCppArgs
[nPos
] = pCppStack
+ 2;
139 pUnoArgs
[nPos
] = pCppStack
+ 2;
141 case typelib_TypeClass_HYPER
:
142 case typelib_TypeClass_UNSIGNED_HYPER
:
143 case typelib_TypeClass_DOUBLE
:
144 pCppArgs
[nPos
] = pCppStack
;
145 pUnoArgs
[nPos
] = pCppStack
;
146 pCppStack
+= sizeof(sal_Int32
); // extra long
149 pCppArgs
[nPos
] = pCppStack
;
150 pUnoArgs
[nPos
] = pCppStack
;
154 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
156 else // ptr to complex value | ref
158 pCppArgs
[nPos
] = *(void **)pCppStack
;
160 if (! rParam
.bIn
) // is pure out
162 // uno out is unconstructed mem!
163 pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
);
164 pTempIndizes
[nTempIndizes
] = nPos
;
165 // will be released at reconversion
166 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
169 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
172 uno_copyAndConvertData( pUnoArgs
[nPos
] = alloca( pParamTypeDescr
->nSize
),
173 *(void **)pCppStack
, pParamTypeDescr
,
174 pThis
->getBridge()->getCpp2Uno() );
175 pTempIndizes
[nTempIndizes
] = nPos
; // has to be reconverted
176 // will be released at reconversion
177 ppTempParamTypeDescr
[nTempIndizes
++] = pParamTypeDescr
;
181 pUnoArgs
[nPos
] = *(void **)pCppStack
;
183 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
186 pCppStack
+= sizeof(sal_Int32
); // standard parameter length
190 uno_Any aUnoExc
; // Any will be constructed by callee
191 uno_Any
* pUnoExc
= &aUnoExc
;
193 #if OSL_DEBUG_LEVEL > 2
194 fprintf(stderr
, "before dispatch\n");
196 // invoke uno dispatch call
197 (*pThis
->getUnoI()->pDispatcher
)(
198 pThis
->getUnoI(), pMemberTypeDescr
, pUnoReturn
, pUnoArgs
, &pUnoExc
);
200 #if OSL_DEBUG_LEVEL > 2
201 fprintf(stderr
, "after dispatch\n");
204 // in case an exception occurred...
207 // destruct temporary in/inout params
208 for ( ; nTempIndizes
--; )
210 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
212 if (pParams
[nIndex
].bIn
) // is in/inout => was constructed
213 uno_destructData( pUnoArgs
[nIndex
],
214 ppTempParamTypeDescr
[nTempIndizes
], 0 );
215 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr
[nTempIndizes
] );
217 if (pReturnTypeDescr
)
218 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
220 CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc
,
221 pThis
->getBridge()->getUno2Cpp() ); // has to destruct the any
223 return typelib_TypeClass_VOID
;
225 else // else no exception occurred...
228 for ( ; nTempIndizes
--; )
230 sal_Int32 nIndex
= pTempIndizes
[nTempIndizes
];
231 typelib_TypeDescription
* pParamTypeDescr
=
232 ppTempParamTypeDescr
[nTempIndizes
];
234 if (pParams
[nIndex
].bOut
) // inout/out
236 // convert and assign
237 uno_destructData( pCppArgs
[nIndex
], pParamTypeDescr
,
239 uno_copyAndConvertData( pCppArgs
[nIndex
], pUnoArgs
[nIndex
],
240 pParamTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
242 // destroy temp uno param
243 uno_destructData( pUnoArgs
[nIndex
], pParamTypeDescr
, 0 );
245 TYPELIB_DANGER_RELEASE( pParamTypeDescr
);
248 if (pCppReturn
) // has complex return
250 if (pUnoReturn
!= pCppReturn
) // needs reconversion
252 uno_copyAndConvertData( pCppReturn
, pUnoReturn
,
253 pReturnTypeDescr
, pThis
->getBridge()->getUno2Cpp() );
254 // destroy temp uno return
255 uno_destructData( pUnoReturn
, pReturnTypeDescr
, 0 );
257 // complex return ptr is set to return reg
258 *(void **)pRegisterReturn
= pCppReturn
;
260 if (pReturnTypeDescr
)
262 typelib_TypeClass eRet
=
263 (typelib_TypeClass
)pReturnTypeDescr
->eTypeClass
;
264 TYPELIB_DANGER_RELEASE( pReturnTypeDescr
);
268 return typelib_TypeClass_VOID
;
273 //=====================================================================
274 static typelib_TypeClass
cpp_mediate(
275 sal_Int32 nFunctionIndex
, sal_Int32 nVtableOffset
,
277 sal_Int64
* pRegisterReturn
/* space for register return */ )
279 void ** pCallStack
= (void**)(sp
);
280 #if OSL_DEBUG_LEVEL > 2
281 fprintf(stderr
, "cpp_mediate with\n");
282 fprintf(stderr
, "%x %x\n", nFunctionIndex
, nVtableOffset
);
283 fprintf(stderr
, "and %x %x\n", pCallStack
, pRegisterReturn
);
284 fprintf(stderr
, "and %x %x\n", pCallStack
[0], pCallStack
[1]);
286 OSL_ENSURE( sizeof(sal_Int32
)==sizeof(void *), "### unexpected!" );
288 void *pThis
= pCallStack
[0];
290 pThis
= static_cast< char * >(pThis
) - nVtableOffset
;
292 bridges::cpp_uno::shared::CppInterfaceProxy
* pCppI
=
293 bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
296 typelib_InterfaceTypeDescription
* pTypeDescr
= pCppI
->getTypeDescr();
298 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
,
299 "### illegal vtable index!" );
300 if (nFunctionIndex
>= pTypeDescr
->nMapFunctionIndexToMemberIndex
)
302 throw RuntimeException(
303 OUString( RTL_CONSTASCII_USTRINGPARAM( "illegal vtable index!" )),
304 (XInterface
*)pCppI
);
307 // determine called method
308 OSL_ENSURE( nFunctionIndex
< pTypeDescr
->nMapFunctionIndexToMemberIndex
,
309 "### illegal vtable index!" );
310 sal_Int32 nMemberPos
=
311 pTypeDescr
->pMapFunctionIndexToMemberIndex
[nFunctionIndex
];
312 OSL_ENSURE( nMemberPos
< pTypeDescr
->nAllMembers
,
313 "### illegal member index!" );
315 TypeDescription
aMemberDescr( pTypeDescr
->ppAllMembers
[nMemberPos
] );
317 typelib_TypeClass eRet
;
318 switch (aMemberDescr
.get()->eTypeClass
)
320 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
322 if (pTypeDescr
->pMapMemberIndexToFunctionIndex
[nMemberPos
] ==
327 pCppI
, aMemberDescr
.get(),
328 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
,
330 r8
, pCallStack
, pRegisterReturn
);
335 typelib_MethodParameter aParam
;
337 ((typelib_InterfaceAttributeTypeDescription
*)aMemberDescr
.get())->pAttributeTypeRef
;
338 aParam
.bIn
= sal_True
;
339 aParam
.bOut
= sal_False
;
342 pCppI
, aMemberDescr
.get(),
343 0, // indicates void return
345 r8
, pCallStack
, pRegisterReturn
);
349 case typelib_TypeClass_INTERFACE_METHOD
:
352 switch (nFunctionIndex
)
355 pCppI
->acquireProxy(); // non virtual call!
356 eRet
= typelib_TypeClass_VOID
;
359 pCppI
->releaseProxy(); // non virtual call!
360 eRet
= typelib_TypeClass_VOID
;
362 case 0: // queryInterface() opt
364 typelib_TypeDescription
* pTD
= 0;
365 TYPELIB_DANGER_GET(&pTD
,
366 reinterpret_cast<Type
*>(pCallStack
[1])->getTypeLibType());
369 XInterface
* pInterface
= 0;
370 (*pCppI
->getBridge()->getCppEnv()->getRegisteredInterface
)(
371 pCppI
->getBridge()->getCppEnv(),
372 (void **)&pInterface
, pCppI
->getOid().pData
,
373 (typelib_InterfaceTypeDescription
*)pTD
);
378 reinterpret_cast< uno_Any
* >( r8
),
379 &pInterface
, pTD
, cpp_acquire
);
380 pInterface
->release();
381 TYPELIB_DANGER_RELEASE( pTD
);
382 *(void **)pRegisterReturn
= (void*)r8
;
383 eRet
= typelib_TypeClass_ANY
;
386 TYPELIB_DANGER_RELEASE( pTD
);
388 } // else perform queryInterface()
391 pCppI
, aMemberDescr
.get(),
392 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pReturnTypeRef
,
393 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->nParams
,
394 ((typelib_InterfaceMethodTypeDescription
*)aMemberDescr
.get())->pParams
,
395 r8
, pCallStack
, pRegisterReturn
);
401 throw RuntimeException(
402 OUString( RTL_CONSTASCII_USTRINGPARAM( "no member description found!" )),
403 (XInterface
*)pCppI
);
411 //=======================================================================
413 * is called on incoming vtable calls
414 * (called by asm snippets)
417 extern "C" sal_Int64
cpp_vtable_call( long firstonstack
)
419 register long d0
asm("d0");
420 long functionIndex
= d0
;
422 register long a1
asm("a1");
425 register long d1
asm("d1");
426 long vtableOffset
= d1
;
428 long sp
= (long)&firstonstack
;
430 sal_Int64 nRegReturn
;
431 cpp_mediate( functionIndex
, vtableOffset
, sp
, r8
, &nRegReturn
);
437 const int codeSnippetSize
= 20;
439 //some m68k info : http://www2.biglobe.ne.jp/~inaba/trampolines.html
441 unsigned char *codeSnippet(unsigned char* code
, sal_Int32 functionIndex
,
442 sal_Int32 vtableOffset
)
444 unsigned char * p
= code
;
445 *(short *)&p
[0] = 0x203C; //movel functionIndex,d0
446 *(long *)&p
[2] = functionIndex
;
447 *(short *)&p
[6] = 0x223C; //movel functionIndex,d1
448 *(long *)&p
[8] = vtableOffset
;
449 *(short *)&p
[12] = 0x4EF9; //jmp cpp_vtable_call
450 *(long *)&p
[14] = (long)&cpp_vtable_call
;
451 *(short *)&p
[18] = 0x4E71; //nop
452 return code
+ codeSnippetSize
;
456 struct bridges::cpp_uno::shared::VtableFactory::Slot
{ void * fn
; };
458 bridges::cpp_uno::shared::VtableFactory::Slot
*
459 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block
)
461 return static_cast< Slot
* >(block
) + 2;
464 sal_Size
bridges::cpp_uno::shared::VtableFactory::getBlockSize(
467 return (slotCount
+ 2) * sizeof (Slot
) + slotCount
* codeSnippetSize
;
470 bridges::cpp_uno::shared::VtableFactory::Slot
*
471 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
472 void * block
, sal_Int32 slotCount
)
474 Slot
* slots
= mapBlockToVtable(block
);
477 return slots
+ slotCount
;
480 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
481 Slot
** slots
, unsigned char * code
, sal_PtrDiff writetoexecdiff
,
482 typelib_InterfaceTypeDescription
const * type
, sal_Int32 functionOffset
,
483 sal_Int32 functionCount
, sal_Int32 vtableOffset
)
485 (*slots
) -= functionCount
;
487 for (sal_Int32 i
= 0; i
< type
->nMembers
; ++i
)
489 typelib_TypeDescription
* member
= 0;
490 TYPELIB_DANGER_GET(&member
, type
->ppMembers
[i
]);
491 OSL_ASSERT(member
!= 0);
492 switch (member
->eTypeClass
)
494 case typelib_TypeClass_INTERFACE_ATTRIBUTE
:
496 (s
++)->fn
= code
+ writetoexecdiff
;
497 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
499 if (!reinterpret_cast<
500 typelib_InterfaceAttributeTypeDescription
* >(
503 (s
++)->fn
= code
+ writetoexecdiff
;
504 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
507 case typelib_TypeClass_INTERFACE_METHOD
:
509 (s
++)->fn
= code
+ writetoexecdiff
;
511 typelib_InterfaceMethodTypeDescription
*pMethodTD
=
513 typelib_InterfaceMethodTypeDescription
* >(member
);
515 code
= codeSnippet(code
, functionOffset
++, vtableOffset
);
522 TYPELIB_DANGER_RELEASE(member
);
527 void bridges::cpp_uno::shared::VtableFactory::flushCode(
528 unsigned char const * /*beg*/, unsigned char const * /*end*/)
532 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */