LibreOffice Module bridges (master) 1
gcc3_linux_loongarch64/uno2cpp.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <exception>
23#include <malloc.h>
24#include <cstring>
25#include <typeinfo>
26
27#include <com/sun/star/uno/Exception.hpp>
28#include <com/sun/star/uno/RuntimeException.hpp>
29#include <com/sun/star/uno/genfunc.hxx>
31#include <uno/data.h>
32
33#include "bridge.hxx"
34#include "types.hxx"
35#include "unointerfaceproxy.hxx"
36#include "vtables.hxx"
37
38#include "share.hxx"
39#include "abi.hxx"
40
41using namespace ::com::sun::star::uno;
42
43namespace
44{
45void pushArgs(unsigned long value, unsigned long* const stack, sal_Int32* const sp,
46 unsigned long* const regs, sal_Int32* const nregs)
47{
48 (*nregs != 8 ? regs[(*nregs)++] : stack[(*sp)++]) = value;
49}
50
51static void callVirtualMethod(void* pAdjustedThisPtr, sal_Int32 nVtableIndex, void* pRegisterReturn,
52 typelib_TypeDescriptionReference* pReturnTypeRef, bool bSimpleReturn,
53 sal_uInt64* pStack, sal_uInt32 nStack, sal_uInt64* pGPR, double* pFPR)
54{
55 // Get pointer to method
56 sal_uInt64 pMethod = *((sal_uInt64*)pAdjustedThisPtr);
57 pMethod += 8 * nVtableIndex;
58 void* mfunc = (void*)*((sal_uInt64*)pMethod);
59 // Load parameters to stack, if necessary
60 sal_uInt64* pCallStack = NULL;
61 if (nStack)
62 {
63 // 16-bytes aligned
64 sal_uInt32 nStackBytes = ((nStack + 1) >> 1) * 16;
65 pCallStack = (sal_uInt64*)__builtin_alloca(nStackBytes);
66 std::memcpy(pCallStack, pStack, nStackBytes);
67 }
68
69 sal_Int64 gret[2];
70 double fret[2];
71 asm volatile(
72 // Fill the general purpose registers
73 "ld.d $r4,%[gpr],0 \n\t"
74 "ld.d $r5,%[gpr],8 \n\t"
75 "ld.d $r6,%[gpr],16 \n\t"
76 "ld.d $r7,%[gpr],24 \n\t"
77 "ld.d $r8,%[gpr],32 \n\t"
78 "ld.d $r9,%[gpr],40 \n\t"
79 "ld.d $r10,%[gpr],48 \n\t"
80 "ld.d $r11,%[gpr],56 \n\t"
81 // Fill the floating pointer registers
82 "fld.d $f0,%[fpr],0 \n\t"
83 "fld.d $f1,%[fpr],8 \n\t"
84 "fld.d $f2,%[fpr],16 \n\t"
85 "fld.d $f3,%[fpr],24 \n\t"
86 "fld.d $f4,%[fpr],32 \n\t"
87 "fld.d $f5,%[fpr],40 \n\t"
88 "fld.d $f6,%[fpr],48 \n\t"
89 "fld.d $f7,%[fpr],56 \n\t"
90 // Perform the call
91 "jirl $ra,%[mfunc],0 \n\t"
92 // Fill the return values
93 "move %[gret1], $a0 \n\t"
94 "move %[gret2], $a1 \n\t"
95 "fmov.d %[fret1], $f0 \n\t"
96 "fmov.d %[fret2], $f1 \n\t"
97 : [gret1] "=r"(gret[0]), [gret2] "=r"(gret[1]), [fret1] "=f"(fret[0]), [fret2] "=f"(fret[1])
98 : [gpr] "r"(pGPR), [fpr] "r"(pFPR), [mfunc] "r"(mfunc),
99 [stack] "m"(
100 pCallStack) // dummy input to prevent the compiler from optimizing the alloca out
101 : "$r4", "$r5", "$r6", "$r7", "$r8", "$r9", "$r10", "$r11", "$r1", "$f0", "$f1", "$f2",
102 "$f3", "$f4", "$f5", "$f6", "$f7", "memory");
103
104 switch (pReturnTypeRef->eTypeClass)
105 {
106 case typelib_TypeClass_HYPER:
107 case typelib_TypeClass_UNSIGNED_HYPER:
108 case typelib_TypeClass_LONG:
109 case typelib_TypeClass_UNSIGNED_LONG:
110 case typelib_TypeClass_ENUM:
111 case typelib_TypeClass_CHAR:
112 case typelib_TypeClass_SHORT:
113 case typelib_TypeClass_UNSIGNED_SHORT:
114 case typelib_TypeClass_BOOLEAN:
115 case typelib_TypeClass_BYTE:
116 *reinterpret_cast<sal_Int64*>(pRegisterReturn) = gret[0];
117 break;
118 case typelib_TypeClass_FLOAT:
119 case typelib_TypeClass_DOUBLE:
120 *reinterpret_cast<double*>(pRegisterReturn) = fret[0];
121 break;
122 case typelib_TypeClass_STRUCT:
123 case typelib_TypeClass_EXCEPTION:
124 {
125 sal_Int32 const nRetSize = pReturnTypeRef->pType->nSize;
126 if (bSimpleReturn && nRetSize <= 16 && nRetSize > 0)
127 {
128 loongarch64::fillReturn(pReturnTypeRef, gret, fret, pRegisterReturn);
129 }
130 break;
131 }
132 default:
133 break;
134 }
135}
136
139 typelib_TypeDescriptionReference* pReturnTypeRef, sal_Int32 nParams,
140 typelib_MethodParameter* pParams, void* pUnoReturn, void* pUnoArgs[],
141 uno_Any** ppUnoExc)
142{
143 // max space for: [complex ret ptr], values|ptr ...
144 sal_uInt64* pStack = (sal_uInt64*)__builtin_alloca(((nParams + 3) * sizeof(sal_Int64)));
145 sal_uInt64* pStackStart = pStack;
146 sal_Int32 sp = 0;
147
148 sal_uInt64 pGPR[MAX_GP_REGS];
149 sal_Int32 gCount = 0;
150 double pFPR[MAX_FP_REGS];
151 sal_Int32 fCount = 0;
152
153 // return
154 typelib_TypeDescription* pReturnTypeDescr = 0;
155 TYPELIB_DANGER_GET(&pReturnTypeDescr, pReturnTypeRef);
156 assert(pReturnTypeDescr);
157
158 void* pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
159
160 bool bSimpleReturn = true;
161 if (pReturnTypeDescr)
162 {
164 {
165 bSimpleReturn = false;
166 // complex return via ptr
167 pCppReturn = bridges::cpp_uno::shared::relatesToInterfaceType(pReturnTypeDescr)
168 ? __builtin_alloca(pReturnTypeDescr->nSize)
169 : pUnoReturn;
170 pGPR[gCount++] = reinterpret_cast<unsigned long>(pCppReturn);
171 }
172 else
173 {
174 pCppReturn = pUnoReturn; // direct way for simple types
175 }
176 }
177
178 // push this
179 void* pAdjustedThisPtr = reinterpret_cast<void**>(pThis->getCppI()) + aVtableSlot.offset;
180 pGPR[gCount++] = reinterpret_cast<unsigned long>(pAdjustedThisPtr);
181
182 // args
183 void** pCppArgs = (void**)alloca(3 * sizeof(void*) * nParams);
184 // indices of values this have to be converted (interface conversion cpp<=>uno)
185 sal_Int32* pTempIndices = (sal_Int32*)(pCppArgs + nParams);
186 // type descriptions for reconversions
187 typelib_TypeDescription** ppTempParamTypeDescr
188 = (typelib_TypeDescription**)(pCppArgs + (2 * nParams));
189
190 sal_Int32 nTempIndices = 0;
191
192 for (sal_Int32 nPos = 0; nPos < nParams; ++nPos)
193 {
194 const typelib_MethodParameter& rParam = pParams[nPos];
195 typelib_TypeDescription* pParamTypeDescr = 0;
196 TYPELIB_DANGER_GET(&pParamTypeDescr, rParam.pTypeRef);
197
198 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType(pParamTypeDescr))
199 {
200 uno_copyAndConvertData(pCppArgs[nPos] = alloca(8), pUnoArgs[nPos], pParamTypeDescr,
201 pThis->getBridge()->getUno2Cpp());
202
203 switch (pParamTypeDescr->eTypeClass)
204 {
205 case typelib_TypeClass_LONG:
206 case typelib_TypeClass_ENUM:
207 pushArgs(*static_cast<sal_Int32*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
208 break;
209 case typelib_TypeClass_UNSIGNED_LONG:
210 pushArgs(*static_cast<sal_uInt32*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
211 break;
212 case typelib_TypeClass_CHAR:
213 pushArgs(*static_cast<sal_Unicode*>(pCppArgs[nPos]), pStack, &sp, pGPR,
214 &gCount);
215 break;
216 case typelib_TypeClass_SHORT:
217 pushArgs(*static_cast<sal_Int16*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
218 break;
219 case typelib_TypeClass_UNSIGNED_SHORT:
220 pushArgs(*static_cast<sal_uInt16*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
221 break;
222 case typelib_TypeClass_BOOLEAN:
223 pushArgs(static_cast<unsigned long>(*static_cast<sal_Bool*>(pCppArgs[nPos])),
224 pStack, &sp, pGPR, &gCount);
225 break;
226 case typelib_TypeClass_BYTE:
227 pushArgs(*static_cast<sal_Int8*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
228 break;
229 case typelib_TypeClass_FLOAT:
230 case typelib_TypeClass_DOUBLE:
231 if (fCount != MAX_FP_REGS)
232 {
233 pFPR[fCount++] = *static_cast<double*>(pCppArgs[nPos]);
234 }
235 else if (gCount != MAX_GP_REGS)
236 {
237 pGPR[gCount++] = *static_cast<unsigned long*>(pCppArgs[nPos]);
238 }
239 else
240 {
241 pStack[sp++] = *static_cast<unsigned long*>(pCppArgs[nPos]);
242 }
243 break;
244 case typelib_TypeClass_HYPER:
245 pushArgs(*static_cast<sal_Int64*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
246 break;
247 case typelib_TypeClass_UNSIGNED_HYPER:
248 pushArgs(*static_cast<sal_uInt64*>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
249 break;
250 default:
251 break;
252 }
253
254 // no longer needed
255 TYPELIB_DANGER_RELEASE(pParamTypeDescr);
256 }
257 else // ptr to complex value | ref
258 {
259 if (!rParam.bIn) // is pure out
260 {
261 // cpp out is constructed mem, uno out is not!
262 uno_constructData(pCppArgs[nPos] = alloca(pParamTypeDescr->nSize), pParamTypeDescr);
263 pTempIndices[nTempIndices] = nPos; // default constructed for cpp call
264 // will be released at reconversion
265 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
266 }
267 // is in/inout
268 else if (bridges::cpp_uno::shared::relatesToInterfaceType(pParamTypeDescr))
269 {
270 uno_copyAndConvertData(pCppArgs[nPos] = alloca(pParamTypeDescr->nSize),
271 pUnoArgs[nPos], pParamTypeDescr,
272 pThis->getBridge()->getUno2Cpp());
273
274 pTempIndices[nTempIndices] = nPos; // has to be reconverted
275 // will be released at reconversion
276 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
277 }
278 else // direct way
279 {
280 pCppArgs[nPos] = pUnoArgs[nPos];
281 // no longer needed
282 TYPELIB_DANGER_RELEASE(pParamTypeDescr);
283 }
284 pushArgs(reinterpret_cast<unsigned long>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
285 }
286 }
287
288 try
289 {
290 try
291 {
292 callVirtualMethod(pAdjustedThisPtr, aVtableSlot.index, pCppReturn, pReturnTypeRef,
293 bSimpleReturn, pStackStart, sp, pGPR, pFPR);
294 }
295 catch (css::uno::Exception&)
296 {
297 throw;
298 }
299 catch (std::exception& e)
300 {
301 throw css::uno::RuntimeException("C++ code threw "
302 + o3tl::runtimeToOUString(typeid(e).name()) + ": "
303 + o3tl::runtimeToOUString(e.what()));
304 }
305 catch (...)
306 {
307 throw css::uno::RuntimeException("C++ code threw unknown exception");
308 }
309 // NO exception occurred...
310 *ppUnoExc = 0;
311
312 // reconvert temporary params
313 for (; nTempIndices--;)
314 {
315 sal_Int32 nIndex = pTempIndices[nTempIndices];
316 typelib_TypeDescription* pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
317
318 if (pParams[nIndex].bIn)
319 {
320 if (pParams[nIndex].bOut) // inout
321 {
322 uno_destructData(pUnoArgs[nIndex], pParamTypeDescr, 0); // destroy uno value
323 uno_copyAndConvertData(pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
324 pThis->getBridge()->getCpp2Uno());
325 }
326 }
327 else // pure out
328 {
329 uno_copyAndConvertData(pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
330 pThis->getBridge()->getCpp2Uno());
331 }
332 // destroy temp cpp param => cpp: every param was constructed
333 uno_destructData(pCppArgs[nIndex], pParamTypeDescr, cpp_release);
334
335 TYPELIB_DANGER_RELEASE(pParamTypeDescr);
336 }
337 // return value
338 if (pCppReturn && pUnoReturn != pCppReturn)
339 {
340 uno_copyAndConvertData(pUnoReturn, pCppReturn, pReturnTypeDescr,
341 pThis->getBridge()->getCpp2Uno());
342 uno_destructData(pCppReturn, pReturnTypeDescr, cpp_release);
343 }
344 }
345 catch (...)
346 {
347 // fill uno exception
349
350 // temporary params
351 for (; nTempIndices--;)
352 {
353 sal_Int32 nIndex = pTempIndices[nTempIndices];
354 // destroy temp cpp param => cpp: every param was constructed
355 uno_destructData(pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndices], cpp_release);
356 TYPELIB_DANGER_RELEASE(ppTempParamTypeDescr[nTempIndices]);
357 }
358 // return type
359 if (pReturnTypeDescr)
360 TYPELIB_DANGER_RELEASE(pReturnTypeDescr);
361 }
362}
363}
364
366{
367void unoInterfaceProxyDispatch(uno_Interface* pUnoI, const typelib_TypeDescription* pMemberDescr,
368 void* pReturn, void* pArgs[], uno_Any** ppException)
369{
370 // is my surrogate
372 = static_cast<bridges::cpp_uno::shared::UnoInterfaceProxy*>(pUnoI);
373 //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
374
375 switch (pMemberDescr->eTypeClass)
376 {
377 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
378 {
379 VtableSlot aVtableSlot(getVtableSlot(
380 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const*>(pMemberDescr)));
381
382 if (pReturn)
383 {
384 // dependent dispatch
385 cpp_call(
386 pThis, aVtableSlot,
387 ((typelib_InterfaceAttributeTypeDescription*)pMemberDescr)->pAttributeTypeRef,
388 0, 0, // no params
389 pReturn, pArgs, ppException);
390 }
391 else
392 {
393 // is SET
394 typelib_MethodParameter aParam;
395 aParam.pTypeRef
396 = ((typelib_InterfaceAttributeTypeDescription*)pMemberDescr)->pAttributeTypeRef;
397 aParam.bIn = sal_True;
398 aParam.bOut = sal_False;
399
400 typelib_TypeDescriptionReference* pReturnTypeRef = 0;
401 OUString aVoidName("void");
402 typelib_typedescriptionreference_new(&pReturnTypeRef, typelib_TypeClass_VOID,
403 aVoidName.pData);
404
405 // dependent dispatch
406 aVtableSlot.index += 1; //get then set method
407 cpp_call(pThis, aVtableSlot, pReturnTypeRef, 1, &aParam, pReturn, pArgs,
408 ppException);
409
411 }
412
413 break;
414 }
415 case typelib_TypeClass_INTERFACE_METHOD:
416 {
417 VtableSlot aVtableSlot(getVtableSlot(
418 reinterpret_cast<typelib_InterfaceMethodTypeDescription const*>(pMemberDescr)));
419 switch (aVtableSlot.index)
420 {
421 // standard calls
422 case 1: // acquire uno interface
423 (*pUnoI->acquire)(pUnoI);
424 *ppException = 0;
425 break;
426 case 2: // release uno interface
427 (*pUnoI->release)(pUnoI);
428 *ppException = 0;
429 break;
430 case 0: // queryInterface() opt
431 {
433 TYPELIB_DANGER_GET(&pTD, reinterpret_cast<Type*>(pArgs[0])->getTypeLibType());
434 if (pTD)
435 {
436 uno_Interface* pInterface = 0;
437 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
438 pThis->pBridge->getUnoEnv(), (void**)&pInterface, pThis->oid.pData,
439 (typelib_InterfaceTypeDescription*)pTD);
440
441 if (pInterface)
442 {
443 ::uno_any_construct(reinterpret_cast<uno_Any*>(pReturn), &pInterface,
444 pTD, 0);
445 (*pInterface->release)(pInterface);
446 TYPELIB_DANGER_RELEASE(pTD);
447 *ppException = 0;
448 break;
449 }
450 TYPELIB_DANGER_RELEASE(pTD);
451 }
452 } // else perform queryInterface()
453 default:
454 // dependent dispatch
455 cpp_call(
456 pThis, aVtableSlot,
457 ((typelib_InterfaceMethodTypeDescription*)pMemberDescr)->pReturnTypeRef,
458 ((typelib_InterfaceMethodTypeDescription*)pMemberDescr)->nParams,
459 ((typelib_InterfaceMethodTypeDescription*)pMemberDescr)->pParams, pReturn,
460 pArgs, ppException);
461 }
462 break;
463 }
464 default:
465 {
466 ::com::sun::star::uno::RuntimeException aExc(
467 "illegal member type description!",
469
470 Type const& rExcType = cppu::UnoType<decltype(aExc)>::get();
471 // binary identical null reference
472 ::uno_type_any_construct(*ppException, &aExc, rExcType.getTypeLibType(), 0);
473 }
474 }
475}
476}
477
478/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
uno_Mapping * getUno2Cpp()
Definition: bridge.hxx:73
uno_ExtEnvironment * getUnoEnv()
Definition: bridge.hxx:70
uno_Mapping * getCpp2Uno()
Definition: bridge.hxx:72
A uno proxy wrapping a cpp interface.
com::sun::star::uno::XInterface * getCppI()
void SAL_CALL uno_destructData(void *pValue, typelib_TypeDescription *pTypeDescr, uno_ReleaseFunc release) SAL_THROW_EXTERN_C()
void SAL_CALL uno_constructData(void *pMem, typelib_TypeDescription *pTypeDescr) SAL_THROW_EXTERN_C()
void SAL_CALL uno_copyAndConvertData(void *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_Mapping *mapping) SAL_THROW_EXTERN_C()
char const * name
void callVirtualMethod(void *pThis, sal_uInt32 nVtableIndex, void *pRegisterReturn, typelib_TypeDescription *pReturnTypeDescr, bool bRegisterReturn, sal_uInt32 *pStack, sal_uInt32 nStack, sal_uInt32 *pGPR, double *pFPR) __attribute__((noinline))
#define MAX_FP_REGS
#define MAX_GP_REGS
static void cpp_call(bridges::cpp_uno::shared::UnoInterfaceProxy *pThis, bridges::cpp_uno::shared::VtableSlot aVtableSlot, typelib_TypeDescriptionReference *pReturnTypeRef, sal_Int32 nParams, typelib_MethodParameter *pParams, void *pUnoReturn, void *pUnoArgs[], uno_Any **ppUnoExc)
sal_Int32 nIndex
sal_uInt16 nPos
return NULL
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
struct _uno_Any uno_Any
Definition: msvc/except.hxx:32
bool return_in_hidden_param(typelib_TypeDescriptionReference *pTypeRef)
void fillUnoException(uno_Any *pUnoExc, uno_Mapping *pCpp2Uno)
void unoInterfaceProxyDispatch(uno_Interface *pUnoI, typelib_TypeDescription const *pMemberDescr, void *pReturn, void **pArgs, uno_Any **ppException)
VtableSlot getVtableSlot(typelib_InterfaceAttributeTypeDescription const *ifcMember)
Calculates the vtable slot associated with an interface attribute member.
Definition: vtables.cxx:132
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
void fillReturn(typelib_TypeDescriptionReference *pTypeRef, sal_Int64 *gret, double *fret, void *pRegisterReturn)
OUString runtimeToOUString(char const *runtimeString)
Represents a vtable slot of a C++ class.
Definition: vtables.hxx:60
sal_Int32 index
The index within the vtable.
Definition: vtables.hxx:76
sal_Int32 offset
The offset of the vtable.
Definition: vtables.hxx:68
void SAL_CALL typelib_typedescriptionreference_new(typelib_TypeDescriptionReference **ppTDR, typelib_TypeClass eTypeClass, rtl_uString *pTypeName) SAL_THROW_EXTERN_C()
void SAL_CALL typelib_typedescriptionreference_release(typelib_TypeDescriptionReference *pRef) SAL_THROW_EXTERN_C()
#define sal_True
#define sal_False
unsigned char sal_Bool
sal_uInt16 sal_Unicode
signed char sal_Int8