tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_loongarch64 / uno2cpp.cxx
blobc4e7815e0a0af1bf959ba121836f1741cfd3c32f
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.
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 .
20 #include <sal/config.h>
22 #include <exception>
23 #include <malloc.h>
24 #include <cstring>
25 #include <typeinfo>
27 #include <com/sun/star/uno/Exception.hpp>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <com/sun/star/uno/genfunc.hxx>
30 #include <o3tl/runtimetooustring.hxx>
31 #include <uno/data.h>
33 #include "bridge.hxx"
34 #include "types.hxx"
35 #include "unointerfaceproxy.hxx"
36 #include "vtables.hxx"
38 #include "share.hxx"
39 #include "abi.hxx"
41 using namespace ::com::sun::star::uno;
43 namespace
45 void pushArgs(unsigned long value, unsigned long* const stack, sal_Int32* const sp,
46 unsigned long* const regs, sal_Int32* const nregs)
48 (*nregs != 8 ? regs[(*nregs)++] : stack[(*sp)++]) = value;
51 static 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)
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)
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);
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");
104 switch (pReturnTypeRef->eTypeClass)
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:
125 sal_Int32 const nRetSize = pReturnTypeRef->pType->nSize;
126 if (bSimpleReturn && nRetSize <= 16 && nRetSize > 0)
128 loongarch64::fillReturn(pReturnTypeRef, gret, fret, pRegisterReturn);
130 break;
132 default:
133 break;
137 static void cpp_call(bridges::cpp_uno::shared::UnoInterfaceProxy* pThis,
138 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
139 typelib_TypeDescriptionReference* pReturnTypeRef, sal_Int32 nParams,
140 typelib_MethodParameter* pParams, void* pUnoReturn, void* pUnoArgs[],
141 uno_Any** ppUnoExc)
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;
148 sal_uInt64 pGPR[MAX_GP_REGS];
149 sal_Int32 gCount = 0;
150 double pFPR[MAX_FP_REGS];
151 sal_Int32 fCount = 0;
153 // return
154 typelib_TypeDescription* pReturnTypeDescr = 0;
155 TYPELIB_DANGER_GET(&pReturnTypeDescr, pReturnTypeRef);
156 assert(pReturnTypeDescr);
158 void* pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
160 bool bSimpleReturn = true;
161 if (pReturnTypeDescr)
163 if (CPPU_CURRENT_NAMESPACE::return_in_hidden_param(pReturnTypeRef))
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);
172 else
174 pCppReturn = pUnoReturn; // direct way for simple types
178 // push this
179 void* pAdjustedThisPtr = reinterpret_cast<void**>(pThis->getCppI()) + aVtableSlot.offset;
180 pGPR[gCount++] = reinterpret_cast<unsigned long>(pAdjustedThisPtr);
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));
190 sal_Int32 nTempIndices = 0;
192 for (sal_Int32 nPos = 0; nPos < nParams; ++nPos)
194 const typelib_MethodParameter& rParam = pParams[nPos];
195 typelib_TypeDescription* pParamTypeDescr = 0;
196 TYPELIB_DANGER_GET(&pParamTypeDescr, rParam.pTypeRef);
198 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType(pParamTypeDescr))
200 uno_copyAndConvertData(pCppArgs[nPos] = alloca(8), pUnoArgs[nPos], pParamTypeDescr,
201 pThis->getBridge()->getUno2Cpp());
203 switch (pParamTypeDescr->eTypeClass)
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)
233 pFPR[fCount++] = *static_cast<double*>(pCppArgs[nPos]);
235 else if (gCount != MAX_GP_REGS)
237 pGPR[gCount++] = *static_cast<unsigned long*>(pCppArgs[nPos]);
239 else
241 pStack[sp++] = *static_cast<unsigned long*>(pCppArgs[nPos]);
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;
254 // no longer needed
255 TYPELIB_DANGER_RELEASE(pParamTypeDescr);
257 else // ptr to complex value | ref
259 if (!rParam.bIn) // is pure out
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;
267 // is in/inout
268 else if (bridges::cpp_uno::shared::relatesToInterfaceType(pParamTypeDescr))
270 uno_copyAndConvertData(pCppArgs[nPos] = alloca(pParamTypeDescr->nSize),
271 pUnoArgs[nPos], pParamTypeDescr,
272 pThis->getBridge()->getUno2Cpp());
274 pTempIndices[nTempIndices] = nPos; // has to be reconverted
275 // will be released at reconversion
276 ppTempParamTypeDescr[nTempIndices++] = pParamTypeDescr;
278 else // direct way
280 pCppArgs[nPos] = pUnoArgs[nPos];
281 // no longer needed
282 TYPELIB_DANGER_RELEASE(pParamTypeDescr);
284 pushArgs(reinterpret_cast<unsigned long>(pCppArgs[nPos]), pStack, &sp, pGPR, &gCount);
292 callVirtualMethod(pAdjustedThisPtr, aVtableSlot.index, pCppReturn, pReturnTypeRef,
293 bSimpleReturn, pStackStart, sp, pGPR, pFPR);
295 catch (css::uno::Exception&)
297 throw;
299 catch (std::exception& e)
301 throw css::uno::RuntimeException("C++ code threw "
302 + o3tl::runtimeToOUString(typeid(e).name()) + ": "
303 + o3tl::runtimeToOUString(e.what()));
305 catch (...)
307 throw css::uno::RuntimeException("C++ code threw unknown exception");
309 // NO exception occurred...
310 *ppUnoExc = 0;
312 // reconvert temporary params
313 for (; nTempIndices--;)
315 sal_Int32 nIndex = pTempIndices[nTempIndices];
316 typelib_TypeDescription* pParamTypeDescr = ppTempParamTypeDescr[nTempIndices];
318 if (pParams[nIndex].bIn)
320 if (pParams[nIndex].bOut) // inout
322 uno_destructData(pUnoArgs[nIndex], pParamTypeDescr, 0); // destroy uno value
323 uno_copyAndConvertData(pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
324 pThis->getBridge()->getCpp2Uno());
327 else // pure out
329 uno_copyAndConvertData(pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
330 pThis->getBridge()->getCpp2Uno());
332 // destroy temp cpp param => cpp: every param was constructed
333 uno_destructData(pCppArgs[nIndex], pParamTypeDescr, cpp_release);
335 TYPELIB_DANGER_RELEASE(pParamTypeDescr);
337 // return value
338 if (pCppReturn && pUnoReturn != pCppReturn)
340 uno_copyAndConvertData(pUnoReturn, pCppReturn, pReturnTypeDescr,
341 pThis->getBridge()->getCpp2Uno());
342 uno_destructData(pCppReturn, pReturnTypeDescr, cpp_release);
345 catch (...)
347 // fill uno exception
348 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc, pThis->getBridge()->getCpp2Uno());
350 // temporary params
351 for (; nTempIndices--;)
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]);
358 // return type
359 if (pReturnTypeDescr)
360 TYPELIB_DANGER_RELEASE(pReturnTypeDescr);
365 namespace bridges::cpp_uno::shared
367 void unoInterfaceProxyDispatch(uno_Interface* pUnoI, const typelib_TypeDescription* pMemberDescr,
368 void* pReturn, void* pArgs[], uno_Any** ppException)
370 // is my surrogate
371 bridges::cpp_uno::shared::UnoInterfaceProxy* pThis
372 = static_cast<bridges::cpp_uno::shared::UnoInterfaceProxy*>(pUnoI);
373 //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
375 switch (pMemberDescr->eTypeClass)
377 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
379 VtableSlot aVtableSlot(getVtableSlot(
380 reinterpret_cast<typelib_InterfaceAttributeTypeDescription const*>(pMemberDescr)));
382 if (pReturn)
384 // dependent dispatch
385 cpp_call(
386 pThis, aVtableSlot,
387 ((typelib_InterfaceAttributeTypeDescription*)pMemberDescr)->pAttributeTypeRef,
388 0, 0, // no params
389 pReturn, pArgs, ppException);
391 else
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;
400 typelib_TypeDescriptionReference* pReturnTypeRef = 0;
401 OUString aVoidName("void");
402 typelib_typedescriptionreference_new(&pReturnTypeRef, typelib_TypeClass_VOID,
403 aVoidName.pData);
405 // dependent dispatch
406 aVtableSlot.index += 1; //get then set method
407 cpp_call(pThis, aVtableSlot, pReturnTypeRef, 1, &aParam, pReturn, pArgs,
408 ppException);
410 typelib_typedescriptionreference_release(pReturnTypeRef);
413 break;
415 case typelib_TypeClass_INTERFACE_METHOD:
417 VtableSlot aVtableSlot(getVtableSlot(
418 reinterpret_cast<typelib_InterfaceMethodTypeDescription const*>(pMemberDescr)));
419 switch (aVtableSlot.index)
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
432 typelib_TypeDescription* pTD = 0;
433 TYPELIB_DANGER_GET(&pTD, reinterpret_cast<Type*>(pArgs[0])->getTypeLibType());
434 if (pTD)
436 uno_Interface* pInterface = 0;
437 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
438 pThis->pBridge->getUnoEnv(), (void**)&pInterface, pThis->oid.pData,
439 (typelib_InterfaceTypeDescription*)pTD);
441 if (pInterface)
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;
450 TYPELIB_DANGER_RELEASE(pTD);
452 } // else perform queryInterface()
453 [[fallthrough]];
454 default:
455 // dependent dispatch
456 cpp_call(
457 pThis, aVtableSlot,
458 ((typelib_InterfaceMethodTypeDescription*)pMemberDescr)->pReturnTypeRef,
459 ((typelib_InterfaceMethodTypeDescription*)pMemberDescr)->nParams,
460 ((typelib_InterfaceMethodTypeDescription*)pMemberDescr)->pParams, pReturn,
461 pArgs, ppException);
463 break;
465 default:
467 ::com::sun::star::uno::RuntimeException aExc(
468 "illegal member type description!",
469 ::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface>());
471 Type const& rExcType = cppu::UnoType<decltype(aExc)>::get();
472 // binary identical null reference
473 ::uno_type_any_construct(*ppException, &aExc, rExcType.getTypeLibType(), 0);
479 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */