LibreOffice Module bridges (master) 1
gcc3_linux_m68k/cpp2uno.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <malloc.h>
21#include <typeinfo>
22
23#include <rtl/alloc.h>
24#include <sal/log.hxx>
25
26#include <com/sun/star/uno/genfunc.hxx>
27#include "com/sun/star/uno/RuntimeException.hpp"
28#include <uno/data.h>
29#include <typelib/typedescription.hxx>
30
31#include "bridge.hxx"
32#include "cppinterfaceproxy.hxx"
33#include "types.hxx"
34#include "vtablefactory.hxx"
35
36#include "share.hxx"
37
38#include <dlfcn.h>
39
40
41using namespace ::osl;
42using namespace ::com::sun::star::uno;
43
44namespace
45{
46
47 static typelib_TypeClass cpp2uno_call(
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 */ )
54 {
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");
60#endif
61 // return
62 typelib_TypeDescription * pReturnTypeDescr = 0;
63 if (pReturnTypeRef)
64 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
65
66 void * pUnoReturn = 0;
67 // complex return ptr: if != 0 && != pUnoReturn, reconversion need
68 void * pCppReturn = 0;
69
70 if (pReturnTypeDescr)
71 {
72 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
73 {
74#if OSL_DEBUG_LEVEL > 2
75 fprintf(stderr, "simple return\n");
76#endif
77 pUnoReturn = pRegisterReturn; // direct way for simple types
78 }
79 else // complex return via ptr (pCppReturn)
80 {
81#if OSL_DEBUG_LEVEL > 2
82 fprintf(stderr, "complex return\n");
83#endif
84 pCppReturn = (void *)r8;
85
86 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
87 ? alloca( pReturnTypeDescr->nSize )
88 : pCppReturn); // direct way
89 }
90 }
91 // pop this
92 pCppStack += sizeof( void* );
93
94 // stack space
95 static_assert( sizeof(void *) == sizeof(sal_Int32),
96 "### unexpected size!" );
97 // parameters
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
101 // cpp<=>uno)
102 sal_Int32 * pTempIndices = (sal_Int32 *)(pUnoArgs + (2 * nParams));
103 // type descriptions for reconversions
104 typelib_TypeDescription ** ppTempParamTypeDescr =
105 (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
106
107 sal_Int32 nTempIndices = 0;
108
109 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
110 {
111 const typelib_MethodParameter & rParam = pParams[nPos];
112 typelib_TypeDescription * pParamTypeDescr = 0;
113 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
114
115 if (!rParam.bOut &&
117 {
118 switch (pParamTypeDescr->eTypeClass)
119 {
120 case typelib_TypeClass_BYTE:
121 case typelib_TypeClass_BOOLEAN:
122 pCppArgs[nPos] = pCppStack + 3;
123 pUnoArgs[nPos] = pCppStack + 3;
124 break;
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;
130 break;
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
137 break;
138 default:
139 pCppArgs[nPos] = pCppStack;
140 pUnoArgs[nPos] = pCppStack;
141 break;
142 }
143 // no longer needed
144 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
145 }
146 else // ptr to complex value | ref
147 {
148 pCppArgs[nPos] = *(void **)pCppStack;
149
150 if (! rParam.bIn) // is pure out
151 {
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;
157 }
158 // is in/inout
160 pParamTypeDescr ))
161 {
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;
168 }
169 else // direct way
170 {
171 pUnoArgs[nPos] = *(void **)pCppStack;
172 // no longer needed
173 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
174 }
175 }
176 pCppStack += sizeof(sal_Int32); // standard parameter length
177 }
178
179 // ExceptionHolder
180 uno_Any aUnoExc; // Any will be constructed by callee
181 uno_Any * pUnoExc = &aUnoExc;
182
183#if OSL_DEBUG_LEVEL > 2
184 fprintf(stderr, "before dispatch\n");
185#endif
186 // invoke uno dispatch call
187 (*pThis->getUnoI()->pDispatcher)(
188 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
189
190#if OSL_DEBUG_LEVEL > 2
191 fprintf(stderr, "after dispatch\n");
192#endif
193
194 // in case an exception occurred...
195 if (pUnoExc)
196 {
197 // destruct temporary in/inout params
198 for ( ; nTempIndices--; )
199 {
200 sal_Int32 nIndex = pTempIndices[nTempIndices];
201
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] );
206 }
207 if (pReturnTypeDescr)
208 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
209
211 pThis->getBridge()->getUno2Cpp() ); // has to destruct the any
212 // is here for dummy
213 return typelib_TypeClass_VOID;
214 }
215 else // else no exception occurred...
216 {
217 // temporary params
218 for ( ; nTempIndices--; )
219 {
220 sal_Int32 nIndex = pTempIndices[nTempIndices];
221 typelib_TypeDescription * pParamTypeDescr =
222 ppTempParamTypeDescr[nTempIndices];
223
224 if (pParams[nIndex].bOut) // inout/out
225 {
226 // convert and assign
227 uno_destructData( pCppArgs[nIndex], pParamTypeDescr,
228 cpp_release );
229 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex],
230 pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
231 }
232 // destroy temp uno param
233 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
234
235 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
236 }
237 // return
238 if (pCppReturn) // has complex return
239 {
240 if (pUnoReturn != pCppReturn) // needs reconversion
241 {
242 uno_copyAndConvertData( pCppReturn, pUnoReturn,
243 pReturnTypeDescr, pThis->getBridge()->getUno2Cpp() );
244 // destroy temp uno return
245 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
246 }
247 // complex return ptr is set to return reg
248 *(void **)pRegisterReturn = pCppReturn;
249 }
250 if (pReturnTypeDescr)
251 {
252 typelib_TypeClass eRet =
253 (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
254 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
255 return eRet;
256 }
257 else
258 return typelib_TypeClass_VOID;
259 }
260 }
261
262
263 static typelib_TypeClass cpp_mediate(
264 sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
265 long sp, long r8,
266 sal_Int64 * pRegisterReturn /* space for register return */ )
267 {
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]);
274#endif
275 static_assert( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
276
277 void *pThis = pCallStack[0];
278
279 pThis = static_cast< char * >(pThis) - nVtableOffset;
280
283 pThis);
284
285 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
286
287 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
288 {
289 SAL_WARN(
290 "bridges",
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);
299 }
300
301 // determine called method
302 assert(nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex);
303 sal_Int32 nMemberPos =
304 pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
305 assert(nMemberPos < pTypeDescr->nAllMembers);
306
307 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
308
309 typelib_TypeClass eRet;
310 switch (aMemberDescr.get()->eTypeClass)
311 {
312 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
313 {
314 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] ==
315 nFunctionIndex)
316 {
317 // is GET method
318 eRet = cpp2uno_call(
319 pCppI, aMemberDescr.get(),
320 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
321 0, 0, // no params
322 r8, pCallStack, pRegisterReturn );
323 }
324 else
325 {
326 // is SET method
327 typelib_MethodParameter aParam;
328 aParam.pTypeRef =
329 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
330 aParam.bIn = sal_True;
331 aParam.bOut = sal_False;
332
333 eRet = cpp2uno_call(
334 pCppI, aMemberDescr.get(),
335 0, // indicates void return
336 1, &aParam,
337 r8, pCallStack, pRegisterReturn );
338 }
339 break;
340 }
341 case typelib_TypeClass_INTERFACE_METHOD:
342 {
343 // is METHOD
344 switch (nFunctionIndex)
345 {
346 case 1: // acquire()
347 pCppI->acquireProxy(); // non virtual call!
348 eRet = typelib_TypeClass_VOID;
349 break;
350 case 2: // release()
351 pCppI->releaseProxy(); // non virtual call!
352 eRet = typelib_TypeClass_VOID;
353 break;
354 case 0: // queryInterface() opt
355 {
356 typelib_TypeDescription * pTD = 0;
357 TYPELIB_DANGER_GET(&pTD,
358 reinterpret_cast<Type *>(pCallStack[1])->getTypeLibType());
359 if (pTD)
360 {
361 XInterface * pInterface = 0;
362 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
363 pCppI->getBridge()->getCppEnv(),
364 (void **)&pInterface, pCppI->getOid().pData,
365 (typelib_InterfaceTypeDescription *)pTD );
366
367 if (pInterface)
368 {
369 ::uno_any_construct(
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;
376 break;
377 }
378 TYPELIB_DANGER_RELEASE( pTD );
379 }
380 } // else perform queryInterface()
381 default:
382 eRet = cpp2uno_call(
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 );
388 }
389 break;
390 }
391 default:
392 {
393 throw RuntimeException( "no member description found!", (XInterface *)pCppI );
394 }
395 }
396
397 return eRet;
398 }
399}
400
406extern "C" sal_Int64 cpp_vtable_call( long firstonstack )
407{
408 register long d0 asm("d0");
409 long functionIndex = d0;
410
411 register long a1 asm("a1");
412 long r8 = a1;
413
414 register long d1 asm("d1");
415 long vtableOffset = d1;
416
417 long sp = (long)&firstonstack;
418
419 sal_Int64 nRegReturn;
420 cpp_mediate( functionIndex, vtableOffset, sp, r8, &nRegReturn );
421 return nRegReturn;
422}
423
424namespace
425{
426 const int codeSnippetSize = 20;
427
428 //some m68k info: http://www2.biglobe.ne.jp/~inaba/trampolines.html
429
430 unsigned char *codeSnippet(unsigned char* code, sal_Int32 functionIndex,
431 sal_Int32 vtableOffset)
432 {
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;
442 }
443}
444
446
449{
450 return static_cast< Slot * >(block) + 2;
451}
452
454 sal_Int32 slotCount)
455{
456 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
457}
458
459namespace {
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:
462struct ProxyRtti {};
463}
464
467 void * block, sal_Int32 slotCount, sal_Int32,
468 typelib_InterfaceTypeDescription *)
469{
470 Slot * slots = mapBlockToVtable(block);
471 slots[-2].fn = 0;
472 slots[-1].fn = &typeid(ProxyRtti);
473 return slots + slotCount;
474}
475
477 Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
478 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
479 sal_Int32 functionCount, sal_Int32 vtableOffset)
480{
481 (*slots) -= functionCount;
482 Slot * s = *slots;
483 for (sal_Int32 i = 0; i < type->nMembers; ++i)
484 {
485 typelib_TypeDescription * member = 0;
486 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
487 assert(member != 0);
488 switch (member->eTypeClass)
489 {
490 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
491 // Getter:
492 (s++)->fn = code + writetoexecdiff;
493 code = codeSnippet(code, functionOffset++, vtableOffset);
494 // Setter:
495 if (!reinterpret_cast<
496 typelib_InterfaceAttributeTypeDescription * >(
497 member)->bReadOnly)
498 {
499 (s++)->fn = code + writetoexecdiff;
500 code = codeSnippet(code, functionOffset++, vtableOffset);
501 }
502 break;
503 case typelib_TypeClass_INTERFACE_METHOD:
504 {
505 (s++)->fn = code + writetoexecdiff;
506
507 typelib_InterfaceMethodTypeDescription *pMethodTD =
508 reinterpret_cast<
509 typelib_InterfaceMethodTypeDescription * >(member);
510
511 code = codeSnippet(code, functionOffset++, vtableOffset);
512 break;
513 }
514 default:
515 assert(false);
516 break;
517 }
518 TYPELIB_DANGER_RELEASE(member);
519 }
520 return code;
521}
522
524 unsigned char const * /*beg*/, unsigned char const * /*end*/)
525{
526}
527
528/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
uno_Mapping * getUno2Cpp()
Definition: bridge.hxx:73
uno_ExtEnvironment * getCppEnv()
Definition: bridge.hxx:69
uno_Mapping * getCpp2Uno()
Definition: bridge.hxx:72
A cpp proxy wrapping a uno interface.
static CppInterfaceProxy * castInterfaceToProxy(void *pInterface)
typelib_InterfaceTypeDescription * getTypeDescr()
static Slot * mapBlockToVtable(void *block)
Given a pointer to a block, turn it into a vtable pointer.
static void flushCode(unsigned char const *begin, unsigned char const *end)
Flush all the generated code snippets of a vtable, on platforms that require it.
static unsigned char * addLocalFunctions(Slot **slots, unsigned char *code, sal_PtrDiff writetoexecdiff, typelib_InterfaceTypeDescription const *type, sal_Int32 functionOffset, sal_Int32 functionCount, sal_Int32 vtableOffset)
Fill the vtable slots corresponding to all local (i.e., not inherited) functions of a given interface...
static Slot * initializeBlock(void *block, sal_Int32 slotCount, sal_Int32 vtableNumber, typelib_InterfaceTypeDescription *type)
Initialize a raw vtable block.
static std::size_t getBlockSize(sal_Int32 slotCount)
Calculate the size of a raw vtable block.
void SAL_CALL uno_destructData(void *pValue, typelib_TypeDescription *pTypeDescr, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void SAL_CALL uno_copyAndConvertData(void *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
sal_Int64 cpp_vtable_call(long firstonstack)
is called on incoming vtable calls (called by asm snippets)
const int codeSnippetSize
static unsigned char * codeSnippet(unsigned char *code, sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset, bool bHasHiddenParam)
static int cpp2uno_call(bridges::cpp_uno::shared::CppInterfaceProxy *pThis, const typelib_TypeDescription *pMemberTypeDescr, typelib_TypeDescriptionReference *pReturnTypeRef, sal_Int32 nParams, typelib_MethodParameter *pParams, void **gpreg, void **fpreg, void **ovrflw, sal_uInt64 *pRegisterReturn)
sal_Int32 nIndex
void * p
sal_uInt16 nPos
#define SAL_WARN(area, stream)
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
struct _uno_Any uno_Any
Definition: msvc/except.hxx:32
typelib_TypeClass __cdecl cpp_mediate(void **pCallStack, const sal_Int32 nFunctionIndex, const sal_Int32 nVtableOffset, sal_Int64 *const pRegisterReturn)
void raiseException(uno_Any *pUnoExc, uno_Mapping *pUno2Cpp)
bool isSimpleType(typelib_TypeClass typeClass)
Determines whether a type is a "simple" type (VOID, BOOLEAN, BYTE, SHORT, UNSIGNED SHORT,...
Definition: types.cxx:28
bool relatesToInterfaceType(typelib_TypeDescription const *type)
Determines whether a type relates to an interface type (is itself an interface type,...
Definition: types.cxx:41
Type
int i
sal_Unicode code
#define sal_True
#define sal_False
ResultType type