LibreOffice Module bridges (master) 1
gcc3_ios/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 <sal/config.h>
21
22#include <typeinfo>
23
24#include <com/sun/star/uno/RuntimeException.hpp>
25#include <sal/log.hxx>
26#include <uno/data.h>
27#include <typelib/typedescription.hxx>
28#include "bridge.hxx"
29#include "cppinterfaceproxy.hxx"
30#include "types.hxx"
31#include "vtablefactory.hxx"
32#include "share.hxx"
33
34
35
36extern "C" int codeSnippets[];
37const int nFunIndexes = 8;
38const int nVtableOffsets = 4;
39
40
41
42using namespace ::com::sun::star::uno;
43
44namespace
45{
46 static typelib_TypeClass cpp2uno_call(
48 const typelib_TypeDescription * pMemberTypeDescr,
49 typelib_TypeDescriptionReference * pReturnTypeRef,
50 sal_Int32 nParams,
51 typelib_MethodParameter * pParams,
52 void ** pCallStack,
53 sal_Int64 * pRegisterReturn /* space for register return */ )
54 {
55 // pCallStack: x8, lr, d0..d7, x0..x7, rest of params originally on stack
56 char *pTopStack = (char *)pCallStack;
57 char *pFloatRegs = pTopStack + 2;
58 char *pGPRegs = pTopStack + (2+8)*8;
59 char *pStackedArgs = pTopStack + (2+8+8)*8;
60
61 int nGPR = 0;
62 int nFPR = 0;
63
64 // return
65 typelib_TypeDescription * pReturnTypeDescr = 0;
66 if (pReturnTypeRef)
67 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
68
69 void * pUnoReturn = 0;
70 // complex return ptr: if != 0 && != pUnoReturn, reconversion need
71 void * pCppReturn = 0;
72
73 if (pReturnTypeDescr)
74 {
75 if (!arm::return_in_x8(pReturnTypeRef))
76 pUnoReturn = pRegisterReturn; // direct way for simple types
77 else // complex return via x8
78 {
79 pCppReturn = pCallStack[0];
80
82 pReturnTypeDescr )
83 ? alloca( pReturnTypeDescr->nSize )
84 : pCppReturn); // direct way
85 }
86 }
87
88 // Skip 'this'
89 pGPRegs += 8;
90 nGPR++;
91
92 // Parameters
93 void ** pUnoArgs = (void **)alloca( sizeof(void *) * nParams );
94 void ** pCppArgs = (void **)alloca( sizeof(void *) * nParams );
95
96 // Indices of values this have to be converted (interface conversion
97 // cpp<=>uno)
98 int * pTempIndices = (sal_Int32 *)alloca( sizeof(int) * nParams);
99
100 // Type descriptions for reconversions
101 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)alloca( sizeof(typelib_TypeDescription *) * nParams);
102
103 int nTempIndices = 0;
104
105 for ( int nPos = 0; nPos < nParams; ++nPos )
106 {
107 const typelib_MethodParameter & rParam = pParams[nPos];
108 typelib_TypeDescription * pParamTypeDescr = 0;
109 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
110
111 if (!rParam.bOut &&
113 {
114 if (nFPR < 8 && (pParamTypeDescr->eTypeClass == typelib_TypeClass_FLOAT ||
115 pParamTypeDescr->eTypeClass == typelib_TypeClass_DOUBLE))
116 {
117 pCppArgs[nPos] = pUnoArgs[nPos] = pFloatRegs;
118 pFloatRegs += 8;
119 nFPR++;
120 }
121 else if (pParamTypeDescr->eTypeClass == typelib_TypeClass_FLOAT)
122 {
123 if ((pStackedArgs - pTopStack) % 4)
124 pStackedArgs += 4 - ((pStackedArgs - pTopStack) % 4);
125 pCppArgs[nPos] = pUnoArgs[nPos] = pStackedArgs;
126 pStackedArgs += 4;
127 }
128 else if (pParamTypeDescr->eTypeClass == typelib_TypeClass_DOUBLE)
129 {
130 if ((pStackedArgs - pTopStack) % 8)
131 pStackedArgs += 8 - ((pStackedArgs - pTopStack) % 8);
132 pCppArgs[nPos] = pUnoArgs[nPos] = pStackedArgs;
133 pStackedArgs += 8;
134 }
135 else if (nGPR < 8)
136 {
137 pCppArgs[nPos] = pUnoArgs[nPos] = pGPRegs;
138 pGPRegs += 8;
139 nGPR++;
140 }
141 else
142 switch (pParamTypeDescr->eTypeClass)
143 {
144 case typelib_TypeClass_HYPER:
145 case typelib_TypeClass_UNSIGNED_HYPER:
146 if ((pStackedArgs - pTopStack) % 8)
147 pStackedArgs += 8 - ((pStackedArgs - pTopStack) % 8);
148 pCppArgs[nPos] = pUnoArgs[nPos] = pStackedArgs;
149 pStackedArgs += 8;
150 break;
151 case typelib_TypeClass_ENUM:
152 case typelib_TypeClass_LONG:
153 case typelib_TypeClass_UNSIGNED_LONG:
154 if ((pStackedArgs - pTopStack) % 4)
155 pStackedArgs += 4 - ((pStackedArgs - pTopStack) % 4);
156 pCppArgs[nPos] = pUnoArgs[nPos] = pStackedArgs;
157 pStackedArgs += 4;
158 break;
159 case typelib_TypeClass_CHAR:
160 case typelib_TypeClass_SHORT:
161 case typelib_TypeClass_UNSIGNED_SHORT:
162 if ((pStackedArgs - pTopStack) % 2)
163 pStackedArgs += 1;
164 pCppArgs[nPos] = pUnoArgs[nPos] = pStackedArgs;
165 pStackedArgs += 2;
166 break;
167 case typelib_TypeClass_BOOLEAN:
168 case typelib_TypeClass_BYTE:
169 pCppArgs[nPos] = pUnoArgs[nPos] = pStackedArgs;
170 pStackedArgs += 1;
171 break;
172 default:
173 assert(!"should not happen");
174 break;
175 }
176 // no longer needed
177 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
178 }
179 else // ptr to complex value | ref
180 {
181 if (nGPR < 8)
182 {
183 pCppArgs[nPos] = *(void **)pGPRegs;
184 pGPRegs += 8;
185 }
186 else
187 {
188 if ((pStackedArgs - pTopStack) % 8)
189 pStackedArgs += 8 - ((pStackedArgs - pTopStack) % 8);
190 pCppArgs[nPos] = pStackedArgs;
191 pStackedArgs += 8;
192 }
193
194 if (! rParam.bIn) // is pure out
195 {
196 // uno out is unconstructed mem!
197 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
198 pTempIndices[nTempIndices] = nPos;
199 // will be released at reconversion
200 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
201 }
202 // is in/inout
203 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
204 {
205 uno_copyAndConvertData( pUnoArgs[nPos] =
206 alloca( pParamTypeDescr->nSize ),
207 pCppArgs[nPos], pParamTypeDescr,
208 pThis->getBridge()->getCpp2Uno() );
209 pTempIndices[nTempIndices] = nPos; // has to be reconverted
210 // will be released at reconversion
211 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
212 }
213 else // direct way
214 {
215 pUnoArgs[nPos] = pCppArgs[nPos];
216 // no longer needed
217 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
218 }
219 }
220 }
221
222 // ExceptionHolder
223 uno_Any aUnoExc; // Any will be constructed by callee
224 uno_Any * pUnoExc = &aUnoExc;
225
226 // invoke uno dispatch call
227 (*pThis->getUnoI()->pDispatcher)(
228 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
229
230 // in case an exception occurred...
231 if (pUnoExc)
232 {
233 // destruct temporary in/inout params
234 for ( ; nTempIndices--; )
235 {
236 int nIndex = pTempIndices[nTempIndices];
237
238 if (pParams[nIndex].bIn) // is in/inout => was constructed
239 uno_destructData( pUnoArgs[nIndex],
240 ppTempParamTypeDescr[nTempIndices], 0 );
241 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndices] );
242 }
243 if (pReturnTypeDescr)
244 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
245
247 pThis->getBridge()->getUno2Cpp() ); // has to destruct the any
248 // is here for dummy
249 return typelib_TypeClass_VOID;
250 }
251 else // else no exception occurred...
252 {
253 // temporary params
254 for ( ; nTempIndices--; )
255 {
256 int nIndex = pTempIndices[nTempIndices];
257 typelib_TypeDescription * pParamTypeDescr =
258 ppTempParamTypeDescr[nTempIndices];
259
260 if (pParams[nIndex].bOut) // inout/out
261 {
262 // convert and assign
263 uno_destructData( pCppArgs[nIndex], pParamTypeDescr,
264 cpp_release );
265 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex],
266 pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
267 }
268 // destroy temp uno param
269 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
270
271 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
272 }
273 // return
274 if (pCppReturn) // has complex return
275 {
276 if (pUnoReturn != pCppReturn) // needs reconversion
277 {
278 uno_copyAndConvertData( pCppReturn, pUnoReturn,
279 pReturnTypeDescr, pThis->getBridge()->getUno2Cpp() );
280 // destroy temp uno return
281 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
282 }
283 *(void **)pRegisterReturn = pCppReturn;
284 }
285 if (pReturnTypeDescr)
286 {
287 typelib_TypeClass eRet =
288 (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
289 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
290 return eRet;
291 }
292 else
293 return typelib_TypeClass_VOID;
294 }
295 }
296
297
298 static void cpp_mediate(sal_Int32 nFunctionIndex,
299 sal_Int32 nVtableOffset,
300 void ** pCallStack)
301 {
302 sal_Int64 nRegReturn;
303 sal_Int64 *pRegisterReturn = &nRegReturn;
304
305 // pCallStack: x8, lr, d0..d7, x0..x7, rest of params originally on stack
306 // _this_ ptr is patched cppu_XInterfaceProxy object
307 void *pThis = pCallStack[2 + 8];
308
309 pThis = static_cast< char * >(pThis) - nVtableOffset;
312 pThis);
313
314 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
315
316 // determine called method
317 assert( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex );
318
319 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
320 {
321 throw RuntimeException( "illegal vtable index!", (XInterface *)pCppI );
322 }
323
324 sal_Int32 nMemberPos =
325 pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
326 assert( nMemberPos < pTypeDescr->nAllMembers );
327
328 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
329
330 typelib_TypeClass eRet;
331 switch (aMemberDescr.get()->eTypeClass)
332 {
333 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
334 {
335 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] ==
336 nFunctionIndex)
337 {
338 // is GET method
339 eRet = cpp2uno_call(
340 pCppI, aMemberDescr.get(),
341 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
342 0, 0, // no params
343 pCallStack, pRegisterReturn );
344 }
345 else
346 {
347 // is SET method
348 typelib_MethodParameter aParam;
349 aParam.pTypeRef =
350 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
351 aParam.bIn = sal_True;
352 aParam.bOut = sal_False;
353
354 eRet = cpp2uno_call(
355 pCppI, aMemberDescr.get(),
356 0, // indicates void return
357 1, &aParam,
358 pCallStack, pRegisterReturn );
359 }
360 break;
361 }
362 case typelib_TypeClass_INTERFACE_METHOD:
363 {
364 // is METHOD
365 switch (nFunctionIndex)
366 {
367 case 1: // acquire()
368 pCppI->acquireProxy(); // non virtual call!
369 eRet = typelib_TypeClass_VOID;
370 break;
371 case 2: // release()
372 pCppI->releaseProxy(); // non virtual call!
373 eRet = typelib_TypeClass_VOID;
374 break;
375 case 0: // queryInterface() opt
376 {
377 typelib_TypeDescription * pTD = 0;
378 TYPELIB_DANGER_GET(&pTD,
379 reinterpret_cast<Type *>(pCallStack[2])->getTypeLibType());
380 if (pTD)
381 {
382 XInterface * pInterface = 0;
383 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
384 pCppI->getBridge()->getCppEnv(),
385 (void **)&pInterface, pCppI->getOid().pData,
386 (typelib_InterfaceTypeDescription *)pTD );
387
388 if (pInterface)
389 {
390 ::uno_any_construct(
391 reinterpret_cast< uno_Any * >( pCallStack[0] ),
392 &pInterface, pTD, cpp_acquire );
393 pInterface->release();
394 TYPELIB_DANGER_RELEASE( pTD );
395 *(void **)pRegisterReturn = pCallStack[0];
396 eRet = typelib_TypeClass_ANY;
397 break;
398 }
399 TYPELIB_DANGER_RELEASE( pTD );
400 }
401 } // else perform queryInterface()
402 [[fallthrough]];
403 default:
404 eRet = cpp2uno_call(
405 pCppI, aMemberDescr.get(),
406 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
407 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
408 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
409 pCallStack, pRegisterReturn );
410 }
411 break;
412 }
413 default:
414 {
415 throw RuntimeException( "no member description found!", (XInterface *)pCppI );
416 }
417 }
418
419 (void)eRet;
420 return;
421 }
422}
423
429extern "C" void cpp_vtable_call( sal_Int32 func, sal_Int32 offset,
430 void **pStack )
431{
432 cpp_mediate(func, offset, pStack);
433}
434
435namespace
436{
437 unsigned char *codeSnippet(const typelib_InterfaceTypeDescription *type,
438 const typelib_TypeDescription *member,
439 sal_Int32 functionIndex,
440 sal_Int32 vtableOffset)
441 {
442 // For now temporarily assert when we get here. The intent is
443 // that we won't need the code snippets at all on iOS.
444 assert(false);
445
446 assert(functionIndex < nFunIndexes);
447 if (!(functionIndex < nFunIndexes))
448 return NULL;
449
450 assert(vtableOffset < nVtableOffsets);
451 if (!(vtableOffset < nVtableOffsets))
452 return NULL;
453
454 // The codeSnippets table is indexed by functionIndex and vtableOffset
455
456 int index = functionIndex*nVtableOffsets + vtableOffset;
457 unsigned char *result = ((unsigned char *) &codeSnippets) + codeSnippets[index];
458
459 SAL_INFO( "bridges", "codeSnippet(" << OUString(type->aBase.pTypeName) << "::" << OUString(member->pTypeName) << "): [" << functionIndex << "," << vtableOffset << "]=" << (void *) result << " (" << std::hex << ((int*)result)[0] << "," << ((int*)result)[1] << "," << ((int*)result)[2] << "," << ((int*)result)[3] << ")");
460
461 return result;
462 }
463}
464
466
469{
470 return static_cast< Slot * >(block) + 2;
471}
472
474 sal_Int32 slotCount)
475{
476 return (slotCount + 2) * sizeof (Slot);
477}
478
479namespace {
480// Some dummy type whose RTTI is used in the synthesized proxy vtables to make uses of dynamic_cast
481// on such proxy objects not crash:
482struct ProxyRtti {};
483}
484
487 void * block, sal_Int32 slotCount, sal_Int32,
488 typelib_InterfaceTypeDescription *)
489{
490 Slot * slots = mapBlockToVtable(block);
491 slots[-2].fn = 0;
492 slots[-1].fn = &typeid(ProxyRtti);
493 return slots + slotCount;
494}
495
497 Slot ** slots,
498 unsigned char * code,
499 typelib_InterfaceTypeDescription const * type,
500 sal_Int32 functionOffset,
501 sal_Int32 functionCount,
502 sal_Int32 vtableOffset)
503{
504 (*slots) -= functionCount;
505 Slot * s = *slots;
506 for (sal_Int32 i = 0; i < type->nMembers; ++i)
507 {
508 typelib_TypeDescription * member = 0;
509 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
510 assert(member != 0);
511 switch (member->eTypeClass)
512 {
513 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
514 {
515 typelib_InterfaceAttributeTypeDescription *pAttrTD =
516 reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( member );
517
518 // Getter:
519 (s++)->fn = codeSnippet( type, member, functionOffset++, vtableOffset );
520
521 // Setter:
522 if (!pAttrTD->bReadOnly)
523 {
524 (s++)->fn = codeSnippet( type, member, functionOffset++, vtableOffset );
525 }
526 break;
527 }
528 case typelib_TypeClass_INTERFACE_METHOD:
529 {
530 (s++)->fn = codeSnippet( type, member, functionOffset++, vtableOffset );
531 break;
532 }
533 default:
534 assert(false);
535 break;
536 }
537 TYPELIB_DANGER_RELEASE(member);
538 }
539 return code;
540}
541
542
543
545 unsigned char const *, unsigned char const *)
546{
547 // No dynamic code generation so nothing to flush
548}
549
550
551/* 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()
const int nVtableOffsets
int codeSnippets[]
const int nFunIndexes
void cpp_vtable_call(sal_Int32 func, sal_Int32 offset, void **pStack)
is called on incoming vtable calls (called by asm snippets)
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
sal_uInt16 nPos
#define SAL_INFO(area, stream)
return NULL
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 return_in_x8(typelib_TypeDescriptionReference *pTypeRef)
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
index
sal_Unicode code
#define sal_True
#define sal_False
Any result
ResultType type