LibreOffice Module bridges (master) 1
msvc_win32_arm64/uno2cpp.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 <cassert>
23#include <cstring>
24#include <exception>
25#include <typeinfo>
26
27#include <bridge.hxx>
28#include <types.hxx>
29#include <unointerfaceproxy.hxx>
30#include <vtables.hxx>
31#include <com/sun/star/uno/Exception.hpp>
32#include <com/sun/star/uno/RuntimeException.hpp>
33#include <com/sun/star/uno/genfunc.hxx>
34#include <rtl/textenc.h>
35#include <rtl/ustring.hxx>
36#include <sal/alloca.h>
37#include <sal/types.h>
38#include <typelib/typeclass.h>
39#include <typelib/typedescription.h>
40#include <uno/any2.h>
41#include <uno/data.h>
42
43#include "abi.hxx"
44#include <msvc/arm64.hxx>
45
46namespace
47{
48extern "C" void callVirtualFunction(sal_uInt64* stack, sal_uInt64* frame, sal_uInt64 function,
49 void* ret);
50
51void pushArgument(sal_uInt64 value, sal_uInt64* stack, sal_Int32& sp, sal_uInt64* regs,
52 sal_Int32& nregs)
53{
54 (nregs != 8 ? regs[nregs++] : stack[sp++]) = value;
55}
56
58 bridges::cpp_uno::shared::VtableSlot slot, typelib_TypeDescriptionReference* returnType,
59 const sal_Int32 count, typelib_MethodParameter* parameters, void* returnValue,
60 void** arguments, uno_Any** exception)
61{
62 static_assert(sizeof(sal_uInt64) == sizeof(void*));
63 typelib_TypeDescription* aReturnTD = nullptr;
64 TYPELIB_DANGER_GET(&aReturnTD, returnType);
65 const ReturnKind eRetKind = getReturnKind(aReturnTD);
66 const bool retConv = bridges::cpp_uno::shared::relatesToInterfaceType(aReturnTD);
67 void* ret = retConv ? alloca(aReturnTD->nSize) : returnValue;
68
69 sal_uInt64** thisPtr = reinterpret_cast<sal_uInt64**>(pProxy->getCppI()) + slot.offset;
70
71 sal_uInt64* gpr = static_cast<sal_uInt64*>(alloca((count + 16) * sizeof(sal_uInt64) + 32));
72 sal_uInt64* fpr = &gpr[8];
73 sal_uInt64* stack = &gpr[16];
74 sal_uInt64* frame = &gpr[16 + count];
75 void** cppArgs = static_cast<void**>(alloca(count * sizeof(void*)));
77 = static_cast<typelib_TypeDescription**>(alloca(count * sizeof(typelib_TypeDescription*)));
78
79 sal_Int32 sp = 0;
80 sal_Int32 nGPR = 0;
81 sal_Int32 nFPR = 0;
82 gpr[nGPR++] = reinterpret_cast<sal_uInt64>(thisPtr);
83
84 for (sal_Int32 i = 0; i != count; ++i)
85 {
86 if (!parameters[i].bOut && bridges::cpp_uno::shared::isSimpleType(parameters[i].pTypeRef))
87 {
88 cppArgs[i] = 0;
89 switch (parameters[i].pTypeRef->eTypeClass)
90 {
91 case typelib_TypeClass_BOOLEAN:
92 pushArgument(*static_cast<sal_Bool*>(arguments[i]), stack, sp, gpr, nGPR);
93 break;
94 case typelib_TypeClass_BYTE:
95 pushArgument(*static_cast<sal_Int8*>(arguments[i]), stack, sp, gpr, nGPR);
96 break;
97 case typelib_TypeClass_SHORT:
98 pushArgument(*static_cast<sal_Int16*>(arguments[i]), stack, sp, gpr, nGPR);
99 break;
100 case typelib_TypeClass_UNSIGNED_SHORT:
101 pushArgument(*static_cast<sal_uInt16*>(arguments[i]), stack, sp, gpr, nGPR);
102 break;
103 case typelib_TypeClass_LONG:
104 case typelib_TypeClass_ENUM:
105 pushArgument(*static_cast<sal_Int32*>(arguments[i]), stack, sp, gpr, nGPR);
106 break;
107 case typelib_TypeClass_UNSIGNED_LONG:
108 pushArgument(*static_cast<sal_uInt32*>(arguments[i]), stack, sp, gpr, nGPR);
109 break;
110 case typelib_TypeClass_HYPER:
111 pushArgument(*static_cast<sal_Int64*>(arguments[i]), stack, sp, gpr, nGPR);
112 break;
113 case typelib_TypeClass_UNSIGNED_HYPER:
114 pushArgument(*static_cast<sal_uInt64*>(arguments[i]), stack, sp, gpr, nGPR);
115 break;
116 case typelib_TypeClass_FLOAT:
117 pushArgument(*static_cast<sal_uInt32*>(arguments[i]), stack, sp, fpr, nFPR);
118 break;
119 case typelib_TypeClass_DOUBLE:
120 pushArgument(*static_cast<sal_uInt64*>(arguments[i]), stack, sp, fpr, nFPR);
121 break;
122 case typelib_TypeClass_CHAR:
123 pushArgument(*static_cast<sal_Unicode*>(arguments[i]), stack, sp, gpr, nGPR);
124 break;
125 default:
126 assert(false);
127 }
128 }
129 else
130 {
132 TYPELIB_DANGER_GET(&ptd, parameters[i].pTypeRef);
133 if (!parameters[i].bIn)
134 {
135 cppArgs[i] = alloca(ptd->nSize);
136 uno_constructData(cppArgs[i], ptd);
137 ptds[i] = ptd;
138 pushArgument(reinterpret_cast<sal_uInt64>(cppArgs[i]), stack, sp, gpr, nGPR);
139 }
141 {
142 cppArgs[i] = alloca(ptd->nSize);
143 uno_copyAndConvertData(cppArgs[i], arguments[i], ptd,
144 pProxy->getBridge()->getUno2Cpp());
145 ptds[i] = ptd;
146 pushArgument(reinterpret_cast<sal_uInt64>(cppArgs[i]), stack, sp, gpr, nGPR);
147 }
148 else
149 {
150 cppArgs[i] = 0;
151 pushArgument(reinterpret_cast<sal_uInt64>(arguments[i]), stack, sp, gpr, nGPR);
152 TYPELIB_DANGER_RELEASE(ptd);
153 }
154 }
155 }
156
157 __try
158 {
159 callVirtualFunction(stack, frame, (*thisPtr)[slot.index], ret);
160 }
161 __except (msvc_filterCppException(GetExceptionInformation(), *exception,
162 pProxy->getBridge()->getCpp2Uno()))
163 {
164 for (sal_Int32 i = 0; i != count; ++i)
165 {
166 if (cppArgs[i] != 0)
167 {
168 uno_destructData(cppArgs[i], ptds[i],
169 reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
170 TYPELIB_DANGER_RELEASE(ptds[i]);
171 }
172 }
173 TYPELIB_DANGER_RELEASE(aReturnTD);
174 return;
175 }
176
177 *exception = 0;
178 for (sal_Int32 i = 0; i != count; ++i)
179 {
180 if (cppArgs[i] != 0)
181 {
182 if (parameters[i].bOut)
183 {
184 if (parameters[i].bIn)
185 {
186 uno_destructData(arguments[i], ptds[i], 0);
187 }
188 uno_copyAndConvertData(arguments[i], cppArgs[i], ptds[i],
189 pProxy->getBridge()->getCpp2Uno());
190 }
191 uno_destructData(cppArgs[i], ptds[i],
192 reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
193 TYPELIB_DANGER_RELEASE(ptds[i]);
194 }
195 }
196
197 switch (eRetKind)
198 {
199 case RETURN_KIND_REG:
200 switch (aReturnTD->eTypeClass)
201 {
202 case typelib_TypeClass_VOID:
203 break;
204 case typelib_TypeClass_BOOLEAN:
205 case typelib_TypeClass_BYTE:
206 case typelib_TypeClass_SHORT:
207 case typelib_TypeClass_UNSIGNED_SHORT:
208 case typelib_TypeClass_LONG:
209 case typelib_TypeClass_UNSIGNED_LONG:
210 case typelib_TypeClass_HYPER:
211 case typelib_TypeClass_UNSIGNED_HYPER:
212 case typelib_TypeClass_CHAR:
213 case typelib_TypeClass_ENUM:
214 case typelib_TypeClass_STRUCT:
215 std::memcpy(ret, gpr, aReturnTD->nSize);
216 break;
217 case typelib_TypeClass_FLOAT:
218 case typelib_TypeClass_DOUBLE:
219 std::memcpy(ret, fpr, aReturnTD->nSize);
220 break;
221 default:
222 assert(false);
223 }
224 break;
226 switch (aReturnTD->nSize)
227 {
228 case 16:
229 std::memcpy(static_cast<char*>(ret) + 12, fpr + 3, 4);
230 [[fallthrough]];
231 case 12:
232 std::memcpy(static_cast<char*>(ret) + 8, fpr + 2, 4);
233 [[fallthrough]];
234 case 8:
235 std::memcpy(static_cast<char*>(ret) + 4, fpr + 1, 4);
236 [[fallthrough]];
237 case 4:
238 std::memcpy(ret, fpr, 4);
239 break;
240 default:
241 assert(false);
242 }
243 break;
245 std::memcpy(ret, fpr, aReturnTD->nSize);
246 break;
248 break;
249 }
250
251 if (retConv)
252 {
253 uno_copyAndConvertData(returnValue, ret, aReturnTD, pProxy->getBridge()->getCpp2Uno());
254 uno_destructData(ret, aReturnTD, reinterpret_cast<uno_ReleaseFunc>(css::uno::cpp_release));
255 }
256 TYPELIB_DANGER_RELEASE(aReturnTD);
257}
258}
259
261{
262void unoInterfaceProxyDispatch(uno_Interface* pUnoI, typelib_TypeDescription const* pMemberDescr,
263 void* pReturn, void** pArgs, uno_Any** ppException)
264{
265 UnoInterfaceProxy* pProxy = static_cast<UnoInterfaceProxy*>(pUnoI);
266 switch (pMemberDescr->eTypeClass)
267 {
268 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
269 {
270 typelib_InterfaceAttributeTypeDescription const* atd
271 = reinterpret_cast<typelib_InterfaceAttributeTypeDescription const*>(pMemberDescr);
272 VtableSlot slot(getVtableSlot(atd));
273 if (pReturn != 0)
274 { // getter
275 call(pProxy, slot, atd->pAttributeTypeRef, 0, 0, pReturn, pArgs, ppException);
276 }
277 else
278 { // setter
279 typelib_MethodParameter param = { 0, atd->pAttributeTypeRef, true, false };
280 typelib_TypeDescriptionReference* pReturnTD = nullptr;
281 typelib_typedescriptionreference_new(&pReturnTD, typelib_TypeClass_VOID,
282 OUString("void").pData);
283 slot.index += 1;
284 call(pProxy, slot, pReturnTD, 1, &param, pReturn, pArgs, ppException);
286 }
287 break;
288 }
289 case typelib_TypeClass_INTERFACE_METHOD:
290 {
291 typelib_InterfaceMethodTypeDescription const* mtd
292 = reinterpret_cast<typelib_InterfaceMethodTypeDescription const*>(pMemberDescr);
293 VtableSlot slot(getVtableSlot(mtd));
294 switch (slot.index)
295 {
296 case 1:
297 pUnoI->acquire(pUnoI);
298 *ppException = 0;
299 break;
300 case 2:
301 pUnoI->release(pUnoI);
302 *ppException = 0;
303 break;
304 case 0:
305 {
307 TYPELIB_DANGER_GET(
308 &td, (reinterpret_cast<css::uno::Type*>(pArgs[0])->getTypeLibType()));
309 if (td != 0)
310 {
311 uno_Interface* ifc = 0;
312 pProxy->pBridge->getUnoEnv()->getRegisteredInterface(
313 pProxy->pBridge->getUnoEnv(), reinterpret_cast<void**>(&ifc),
314 pProxy->oid.pData,
315 reinterpret_cast<typelib_InterfaceTypeDescription*>(td));
316 if (ifc != 0)
317 {
318 uno_any_construct(reinterpret_cast<uno_Any*>(pReturn), &ifc, td, 0);
319 ifc->release(ifc);
320 TYPELIB_DANGER_RELEASE(td);
321 *ppException = 0;
322 break;
323 }
324 TYPELIB_DANGER_RELEASE(td);
325 }
326 }
327 [[fallthrough]];
328 default:
329 call(pProxy, slot, mtd->pReturnTypeRef, mtd->nParams, mtd->pParams, pReturn,
330 pArgs, ppException);
331 break;
332 }
333 break;
334 }
335 default:
336 assert(false);
337 }
338}
339}
340
341/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_CALL uno_any_construct(uno_Any *pDest, void *pSource, typelib_TypeDescription *pTypeDescr, uno_AcquireFunc acquire) SAL_THROW_EXTERN_C()
void callVirtualFunction(unsigned long function, unsigned long *gpr, unsigned long *fpr, unsigned long *stack, sal_Int32 sp, void *ret)
uno_Mapping * getUno2Cpp()
Definition: bridge.hxx:73
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()
struct _typelib_TypeDescription typelib_TypeDescription
Definition: msvc/except.hxx:53
struct _uno_Any uno_Any
Definition: msvc/except.hxx:32
int msvc_filterCppException(EXCEPTION_POINTERS *pPointers, uno_Any *pUnoExc, uno_Mapping *pCpp2Uno)
@ RETURN_KIND_HFA_DOUBLE
@ RETURN_KIND_INDIRECT
@ RETURN_KIND_REG
@ RETURN_KIND_HFA_FLOAT
ReturnKind getReturnKind(typelib_TypeDescription const *type)
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
int i
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()
unsigned char sal_Bool
sal_uInt16 sal_Unicode
signed char sal_Int8