1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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>
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>
35 #include "unointerfaceproxy.hxx"
36 #include "vtables.hxx"
41 using namespace ::com::sun::star::uno
;
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
;
64 sal_uInt32 nStackBytes
= ((nStack
+ 1) >> 1) * 16;
65 pCallStack
= (sal_uInt64
*)__builtin_alloca(nStackBytes
);
66 std::memcpy(pCallStack
, pStack
, nStackBytes
);
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"
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
),
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];
118 case typelib_TypeClass_FLOAT
:
119 case typelib_TypeClass_DOUBLE
:
120 *reinterpret_cast<double*>(pRegisterReturn
) = fret
[0];
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
);
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
[],
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
;
148 sal_uInt64 pGPR
[MAX_GP_REGS
];
149 sal_Int32 gCount
= 0;
150 double pFPR
[MAX_FP_REGS
];
151 sal_Int32 fCount
= 0;
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
)
170 pGPR
[gCount
++] = reinterpret_cast<unsigned long>(pCppReturn
);
174 pCppReturn
= pUnoReturn
; // direct way for simple types
179 void* pAdjustedThisPtr
= reinterpret_cast<void**>(pThis
->getCppI()) + aVtableSlot
.offset
;
180 pGPR
[gCount
++] = reinterpret_cast<unsigned long>(pAdjustedThisPtr
);
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
);
209 case typelib_TypeClass_UNSIGNED_LONG
:
210 pushArgs(*static_cast<sal_uInt32
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
, &gCount
);
212 case typelib_TypeClass_CHAR
:
213 pushArgs(*static_cast<sal_Unicode
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
,
216 case typelib_TypeClass_SHORT
:
217 pushArgs(*static_cast<sal_Int16
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
, &gCount
);
219 case typelib_TypeClass_UNSIGNED_SHORT
:
220 pushArgs(*static_cast<sal_uInt16
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
, &gCount
);
222 case typelib_TypeClass_BOOLEAN
:
223 pushArgs(static_cast<unsigned long>(*static_cast<sal_Bool
*>(pCppArgs
[nPos
])),
224 pStack
, &sp
, pGPR
, &gCount
);
226 case typelib_TypeClass_BYTE
:
227 pushArgs(*static_cast<sal_Int8
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
, &gCount
);
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
]);
241 pStack
[sp
++] = *static_cast<unsigned long*>(pCppArgs
[nPos
]);
244 case typelib_TypeClass_HYPER
:
245 pushArgs(*static_cast<sal_Int64
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
, &gCount
);
247 case typelib_TypeClass_UNSIGNED_HYPER
:
248 pushArgs(*static_cast<sal_uInt64
*>(pCppArgs
[nPos
]), pStack
, &sp
, pGPR
, &gCount
);
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
;
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
;
280 pCppArgs
[nPos
] = pUnoArgs
[nPos
];
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
&)
299 catch (std::exception
& e
)
301 throw css::uno::RuntimeException("C++ code threw "
302 + o3tl::runtimeToOUString(typeid(e
).name()) + ": "
303 + o3tl::runtimeToOUString(e
.what()));
307 throw css::uno::RuntimeException("C++ code threw unknown exception");
309 // NO exception occurred...
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());
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
);
338 if (pCppReturn
&& pUnoReturn
!= pCppReturn
)
340 uno_copyAndConvertData(pUnoReturn
, pCppReturn
, pReturnTypeDescr
,
341 pThis
->getBridge()->getCpp2Uno());
342 uno_destructData(pCppReturn
, pReturnTypeDescr
, cpp_release
);
347 // fill uno exception
348 CPPU_CURRENT_NAMESPACE::fillUnoException(*ppUnoExc
, pThis
->getBridge()->getCpp2Uno());
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
]);
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
)
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
)));
384 // dependent dispatch
387 ((typelib_InterfaceAttributeTypeDescription
*)pMemberDescr
)->pAttributeTypeRef
,
389 pReturn
, pArgs
, ppException
);
394 typelib_MethodParameter aParam
;
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
,
405 // dependent dispatch
406 aVtableSlot
.index
+= 1; //get then set method
407 cpp_call(pThis
, aVtableSlot
, pReturnTypeRef
, 1, &aParam
, pReturn
, pArgs
,
410 typelib_typedescriptionreference_release(pReturnTypeRef
);
415 case typelib_TypeClass_INTERFACE_METHOD
:
417 VtableSlot
aVtableSlot(getVtableSlot(
418 reinterpret_cast<typelib_InterfaceMethodTypeDescription
const*>(pMemberDescr
)));
419 switch (aVtableSlot
.index
)
422 case 1: // acquire uno interface
423 (*pUnoI
->acquire
)(pUnoI
);
426 case 2: // release uno interface
427 (*pUnoI
->release
)(pUnoI
);
430 case 0: // queryInterface() opt
432 typelib_TypeDescription
* pTD
= 0;
433 TYPELIB_DANGER_GET(&pTD
, reinterpret_cast<Type
*>(pArgs
[0])->getTypeLibType());
436 uno_Interface
* pInterface
= 0;
437 (*pThis
->pBridge
->getUnoEnv()->getRegisteredInterface
)(
438 pThis
->pBridge
->getUnoEnv(), (void**)&pInterface
, pThis
->oid
.pData
,
439 (typelib_InterfaceTypeDescription
*)pTD
);
443 ::uno_any_construct(reinterpret_cast<uno_Any
*>(pReturn
), &pInterface
,
445 (*pInterface
->release
)(pInterface
);
446 TYPELIB_DANGER_RELEASE(pTD
);
450 TYPELIB_DANGER_RELEASE(pTD
);
452 } // else perform queryInterface()
455 // dependent dispatch
458 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pReturnTypeRef
,
459 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->nParams
,
460 ((typelib_InterfaceMethodTypeDescription
*)pMemberDescr
)->pParams
, pReturn
,
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: */