xref: /trunk/main/bridges/source/cpp_uno/gcc3_freebsd_aarch64/cpp2uno.cxx (revision f6c78b21b9451944eb43023c08ad1a3c8ad54ae4)
19c15e1abSPedro Giffuni /*
29c15e1abSPedro Giffuni  * Licensed to the Apache Software Foundation (ASF) under one
39c15e1abSPedro Giffuni  * or more contributor license agreements.  See the NOTICE file
49c15e1abSPedro Giffuni  * distributed with this work for additional information
59c15e1abSPedro Giffuni  * regarding copyright ownership.  The ASF licenses this file
69c15e1abSPedro Giffuni  * to you under the Apache License, Version 2.0 (the
79c15e1abSPedro Giffuni  * "License"); you may not use this file except in compliance
89c15e1abSPedro Giffuni  * with the License.  You may obtain a copy of the License at
99c15e1abSPedro Giffuni  *
109c15e1abSPedro Giffuni  *   http://www.apache.org/licenses/LICENSE-2.0
119c15e1abSPedro Giffuni  *
129c15e1abSPedro Giffuni  * Unless required by applicable law or agreed to in writing,
139c15e1abSPedro Giffuni  * software distributed under the License is distributed on an
149c15e1abSPedro Giffuni  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
159c15e1abSPedro Giffuni  * KIND, either express or implied.  See the License for the
169c15e1abSPedro Giffuni  * specific language governing permissions and limitations
179c15e1abSPedro Giffuni  * under the License.
189c15e1abSPedro Giffuni  */
199c15e1abSPedro Giffuni 
209c15e1abSPedro Giffuni 
219c15e1abSPedro Giffuni // MARKER(update_precomp.py): autogen include statement, do not remove
229c15e1abSPedro Giffuni #include "precompiled_bridges.hxx"
239c15e1abSPedro Giffuni 
249c15e1abSPedro Giffuni #include <stdio.h>
259c15e1abSPedro Giffuni #include <stdlib.h>
26*f6c78b21SPedro F. Giffuni #include <unordered_map>
279c15e1abSPedro Giffuni 
289c15e1abSPedro Giffuni #include <rtl/alloc.h>
299c15e1abSPedro Giffuni #include <osl/mutex.hxx>
309c15e1abSPedro Giffuni 
319c15e1abSPedro Giffuni #include <com/sun/star/uno/genfunc.hxx>
329c15e1abSPedro Giffuni #include "com/sun/star/uno/RuntimeException.hpp"
339c15e1abSPedro Giffuni #include <uno/data.h>
349c15e1abSPedro Giffuni #include <typelib/typedescription.hxx>
359c15e1abSPedro Giffuni 
369c15e1abSPedro Giffuni #include "bridges/cpp_uno/shared/bridge.hxx"
379c15e1abSPedro Giffuni #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
389c15e1abSPedro Giffuni #include "bridges/cpp_uno/shared/types.hxx"
399c15e1abSPedro Giffuni #include "bridges/cpp_uno/shared/vtablefactory.hxx"
409c15e1abSPedro Giffuni 
419c15e1abSPedro Giffuni #include "abi.hxx"
429c15e1abSPedro Giffuni #include "share.hxx"
439c15e1abSPedro Giffuni 
449c15e1abSPedro Giffuni using namespace ::osl;
459c15e1abSPedro Giffuni using namespace ::rtl;
469c15e1abSPedro Giffuni using namespace ::com::sun::star::uno;
479c15e1abSPedro Giffuni 
489c15e1abSPedro Giffuni //==================================================================================================
499c15e1abSPedro Giffuni 
509c15e1abSPedro Giffuni // Perform the UNO call
519c15e1abSPedro Giffuni //
529c15e1abSPedro Giffuni // We must convert the parameters stored in gpreg, fpreg and ovrflw to UNO
539c15e1abSPedro Giffuni // arguments and call pThis->getUnoI()->pDispatcher.
549c15e1abSPedro Giffuni //
559c15e1abSPedro Giffuni // gpreg:  this, [gpr params x0..x7]   (the indirect-result ptr is x8, separate)
569c15e1abSPedro Giffuni // fpreg:  [fpr params d0..d7]
579c15e1abSPedro Giffuni // ovrflw: [gpr or fpr params (properly aligned)]
589c15e1abSPedro Giffuni //
599c15e1abSPedro Giffuni // On AArch64 a structure bigger than 16 bytes is returned via the buffer
609c15e1abSPedro Giffuni // addressed by x8 (pIndirectReturn); 'this' is always x0 = gpreg[0].
619c15e1abSPedro Giffuni // Simple types are returned in x0,x1 (int) or d0,d1 (fp); HFAs in d0..d3;
629c15e1abSPedro Giffuni // non-HFA structures <= 16 bytes in x0,x1.
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,unsigned char * ovrflw,void * pIndirectReturn,sal_uInt64 * pRegisterReturn)639c15e1abSPedro Giffuni static typelib_TypeClass cpp2uno_call(
649c15e1abSPedro Giffuni     bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
659c15e1abSPedro Giffuni     const typelib_TypeDescription * pMemberTypeDescr,
669c15e1abSPedro Giffuni     typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
679c15e1abSPedro Giffuni     sal_Int32 nParams, typelib_MethodParameter * pParams,
689c15e1abSPedro Giffuni     void ** gpreg, void ** fpreg, unsigned char * ovrflw,
699c15e1abSPedro Giffuni     void * pIndirectReturn, // AArch64 x8 indirect-result pointer (0 if none)
709c15e1abSPedro Giffuni     sal_uInt64 * pRegisterReturn /* space for register return */ )
719c15e1abSPedro Giffuni {
729c15e1abSPedro Giffuni     unsigned int nr_gpr = 0; //number of gpr registers used
739c15e1abSPedro Giffuni     unsigned int nr_fpr = 0; //number of fpr registers used
749c15e1abSPedro Giffuni     sal_uInt32 stackOffset = 0;
759c15e1abSPedro Giffuni 
769c15e1abSPedro Giffuni     // return
779c15e1abSPedro Giffuni     typelib_TypeDescription * pReturnTypeDescr = 0;
789c15e1abSPedro Giffuni     if (pReturnTypeRef)
799c15e1abSPedro Giffuni         TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
809c15e1abSPedro Giffuni 
819c15e1abSPedro Giffuni     void * pUnoReturn = 0;
829c15e1abSPedro Giffuni     void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
839c15e1abSPedro Giffuni 
849c15e1abSPedro Giffuni     if ( pReturnTypeDescr )
859c15e1abSPedro Giffuni     {
869c15e1abSPedro Giffuni         if ( aarch64::return_in_hidden_param( pReturnTypeRef ) )
879c15e1abSPedro Giffuni         {
889c15e1abSPedro Giffuni             // AArch64: the indirect-result pointer arrives in x8, NOT in the
899c15e1abSPedro Giffuni             // first general-purpose argument register (unlike x86-64 SysV).
909c15e1abSPedro Giffuni             // So we take it from pIndirectReturn and do NOT consume a gpreg
919c15e1abSPedro Giffuni             // slot here; 'this' still occupies gpreg[0] below.
929c15e1abSPedro Giffuni             pCppReturn = pIndirectReturn;
939c15e1abSPedro Giffuni 
949c15e1abSPedro Giffuni             pUnoReturn = ( bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
959c15e1abSPedro Giffuni                            ? alloca( pReturnTypeDescr->nSize )
969c15e1abSPedro Giffuni                            : pCppReturn ); // direct way
979c15e1abSPedro Giffuni         }
989c15e1abSPedro Giffuni         else
999c15e1abSPedro Giffuni             pUnoReturn = pRegisterReturn; // direct way for simple types
1009c15e1abSPedro Giffuni     }
1019c15e1abSPedro Giffuni 
1029c15e1abSPedro Giffuni     // pop this (x0)
1039c15e1abSPedro Giffuni     gpreg++;
1049c15e1abSPedro Giffuni     nr_gpr++;
1059c15e1abSPedro Giffuni 
1069c15e1abSPedro Giffuni     // stack space
1079c15e1abSPedro Giffuni     // parameters
1089c15e1abSPedro Giffuni     void ** pUnoArgs = reinterpret_cast<void **>(alloca( 4 * sizeof(void *) * nParams ));
1099c15e1abSPedro Giffuni     void ** pCppArgs = pUnoArgs + nParams;
1109c15e1abSPedro Giffuni     // indices of values this have to be converted (interface conversion cpp<=>uno)
1119c15e1abSPedro Giffuni     sal_Int32 * pTempIndizes = reinterpret_cast<sal_Int32 *>(pUnoArgs + (2 * nParams));
1129c15e1abSPedro Giffuni     // type descriptions for reconversions
1139c15e1abSPedro Giffuni     typelib_TypeDescription ** ppTempParamTypeDescr = reinterpret_cast<typelib_TypeDescription **>(pUnoArgs + (3 * nParams));
1149c15e1abSPedro Giffuni 
1159c15e1abSPedro Giffuni     sal_Int32 nTempIndizes = 0;
1169c15e1abSPedro Giffuni 
1179c15e1abSPedro Giffuni     for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
1189c15e1abSPedro Giffuni     {
1199c15e1abSPedro Giffuni         const typelib_MethodParameter & rParam = pParams[nPos];
1209c15e1abSPedro Giffuni 
1219c15e1abSPedro Giffuni         int nUsedGPR = 0;
1229c15e1abSPedro Giffuni         int nUsedFPR = 0;
123dcfbcfccSPedro Giffuni         aarch64::examine_argument( rParam.pTypeRef, false, nUsedGPR, nUsedFPR );
1249c15e1abSPedro Giffuni         if ( !rParam.bOut && bridges::cpp_uno::shared::isSimpleType( rParam.pTypeRef ) ) // value
1259c15e1abSPedro Giffuni         {
1269c15e1abSPedro Giffuni             OSL_ASSERT( ( nUsedFPR == 1 && nUsedGPR == 0 ) || ( nUsedFPR == 0 && nUsedGPR == 1 ) );
1279c15e1abSPedro Giffuni 
1289c15e1abSPedro Giffuni             if ( nUsedFPR == 1 )
1299c15e1abSPedro Giffuni             {
1309c15e1abSPedro Giffuni                 if ( nr_fpr < aarch64::MAX_FPR_REGS )
1319c15e1abSPedro Giffuni                 {
1329c15e1abSPedro Giffuni                     pCppArgs[nPos] = pUnoArgs[nPos] = fpreg++;
1339c15e1abSPedro Giffuni                     nr_fpr++;
1349c15e1abSPedro Giffuni                 }
1359c15e1abSPedro Giffuni                 else
1369c15e1abSPedro Giffuni                 {
1379c15e1abSPedro Giffuni                     stackOffset = aarch64::align_stack_offset(
1389c15e1abSPedro Giffuni                         stackOffset, rParam.pTypeRef );
1399c15e1abSPedro Giffuni                     pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw + stackOffset;
1409c15e1abSPedro Giffuni                     stackOffset += aarch64::stack_size( rParam.pTypeRef );
1419c15e1abSPedro Giffuni                 }
1429c15e1abSPedro Giffuni             }
1439c15e1abSPedro Giffuni             else if ( nUsedGPR == 1 )
1449c15e1abSPedro Giffuni             {
1459c15e1abSPedro Giffuni                 if ( nr_gpr < aarch64::MAX_GPR_REGS )
1469c15e1abSPedro Giffuni                 {
1479c15e1abSPedro Giffuni                     pCppArgs[nPos] = pUnoArgs[nPos] = gpreg++;
1489c15e1abSPedro Giffuni                     nr_gpr++;
1499c15e1abSPedro Giffuni                 }
1509c15e1abSPedro Giffuni                 else
1519c15e1abSPedro Giffuni                 {
1529c15e1abSPedro Giffuni                     stackOffset = aarch64::align_stack_offset(
1539c15e1abSPedro Giffuni                         stackOffset, rParam.pTypeRef );
1549c15e1abSPedro Giffuni                     pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw + stackOffset;
1559c15e1abSPedro Giffuni                     stackOffset += aarch64::stack_size( rParam.pTypeRef );
1569c15e1abSPedro Giffuni                 }
1579c15e1abSPedro Giffuni             }
1589c15e1abSPedro Giffuni         }
1599c15e1abSPedro Giffuni         else // struct <= 16 bytes || ptr to complex value || ref
1609c15e1abSPedro Giffuni         {
1619c15e1abSPedro Giffuni             typelib_TypeDescription * pParamTypeDescr = 0;
1629c15e1abSPedro Giffuni             TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
1639c15e1abSPedro Giffuni 
1649c15e1abSPedro Giffuni             void *pCppStack = 0;
1659c15e1abSPedro Giffuni             if ( nr_gpr < aarch64::MAX_GPR_REGS )
1669c15e1abSPedro Giffuni             {
1679c15e1abSPedro Giffuni                 pCppArgs[nPos] = pCppStack = *gpreg++;
1689c15e1abSPedro Giffuni                 nr_gpr++;
1699c15e1abSPedro Giffuni             }
1709c15e1abSPedro Giffuni             else
1719c15e1abSPedro Giffuni             {
1729c15e1abSPedro Giffuni                 stackOffset = (stackOffset + sizeof(void *) - 1) &
1739c15e1abSPedro Giffuni                     ~(sizeof(void *) - 1);
1749c15e1abSPedro Giffuni                 pCppArgs[nPos] = pCppStack =
1759c15e1abSPedro Giffuni                     *reinterpret_cast<void **>( ovrflw + stackOffset );
1769c15e1abSPedro Giffuni                 stackOffset += sizeof(void *);
1779c15e1abSPedro Giffuni             }
1789c15e1abSPedro Giffuni 
1799c15e1abSPedro Giffuni             if (! rParam.bIn) // is pure out
1809c15e1abSPedro Giffuni             {
1819c15e1abSPedro Giffuni                 // uno out is unconstructed mem!
1829c15e1abSPedro Giffuni                 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
1839c15e1abSPedro Giffuni                 pTempIndizes[nTempIndizes] = nPos;
1849c15e1abSPedro Giffuni                 // will be released at reconversion
1859c15e1abSPedro Giffuni                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
1869c15e1abSPedro Giffuni             }
1879c15e1abSPedro Giffuni             else if ( bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ) ) // is in/inout
1889c15e1abSPedro Giffuni             {
1899c15e1abSPedro Giffuni                 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
1909c15e1abSPedro Giffuni                                         pCppStack, pParamTypeDescr,
1919c15e1abSPedro Giffuni                                         pThis->getBridge()->getCpp2Uno() );
1929c15e1abSPedro Giffuni                 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
1939c15e1abSPedro Giffuni                 // will be released at reconversion
1949c15e1abSPedro Giffuni                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
1959c15e1abSPedro Giffuni             }
196dcfbcfccSPedro Giffuni             else // direct way
1979c15e1abSPedro Giffuni             {
198dcfbcfccSPedro Giffuni                 // struct in registers/stack: copy it directly
1999c15e1abSPedro Giffuni                 pUnoArgs[nPos] = pCppStack;
200dcfbcfccSPedro Giffuni                 // no longer needed
2019c15e1abSPedro Giffuni                 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
2029c15e1abSPedro Giffuni             }
2039c15e1abSPedro Giffuni         }
204dcfbcfccSPedro Giffuni     }
2059c15e1abSPedro Giffuni 
2069c15e1abSPedro Giffuni     // ExceptionHolder
2079c15e1abSPedro Giffuni     uno_Any aUnoExc; // Any will be constructed by callee
2089c15e1abSPedro Giffuni     uno_Any * pUnoExc = &aUnoExc;
2099c15e1abSPedro Giffuni 
2109c15e1abSPedro Giffuni     // invoke uno dispatch call
2119c15e1abSPedro Giffuni     (*pThis->getUnoI()->pDispatcher)( pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
2129c15e1abSPedro Giffuni 
2139c15e1abSPedro Giffuni     // in case an exception occurred...
2149c15e1abSPedro Giffuni     if ( pUnoExc )
2159c15e1abSPedro Giffuni     {
2169c15e1abSPedro Giffuni         // destruct temporary in/inout params
2179c15e1abSPedro Giffuni         for ( ; nTempIndizes--; )
2189c15e1abSPedro Giffuni         {
2199c15e1abSPedro Giffuni             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
2209c15e1abSPedro Giffuni 
2219c15e1abSPedro Giffuni             if (pParams[nIndex].bIn) // is in/inout => was constructed
2229c15e1abSPedro Giffuni                 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
2239c15e1abSPedro Giffuni             TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
2249c15e1abSPedro Giffuni         }
2259c15e1abSPedro Giffuni         if (pReturnTypeDescr)
2269c15e1abSPedro Giffuni             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
2279c15e1abSPedro Giffuni 
2289c15e1abSPedro Giffuni         CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc, pThis->getBridge()->getUno2Cpp() ); // has to destruct the any
2299c15e1abSPedro Giffuni         // is here for dummy
2309c15e1abSPedro Giffuni         return typelib_TypeClass_VOID;
2319c15e1abSPedro Giffuni     }
2329c15e1abSPedro Giffuni     else // else no exception occurred...
2339c15e1abSPedro Giffuni     {
2349c15e1abSPedro Giffuni         // temporary params
2359c15e1abSPedro Giffuni         for ( ; nTempIndizes--; )
2369c15e1abSPedro Giffuni         {
2379c15e1abSPedro Giffuni             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
2389c15e1abSPedro Giffuni             typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
2399c15e1abSPedro Giffuni 
2409c15e1abSPedro Giffuni             if ( pParams[nIndex].bOut ) // inout/out
2419c15e1abSPedro Giffuni             {
2429c15e1abSPedro Giffuni                 // convert and assign
2439c15e1abSPedro Giffuni                 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
2449c15e1abSPedro Giffuni                 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
2459c15e1abSPedro Giffuni                                         pThis->getBridge()->getUno2Cpp() );
2469c15e1abSPedro Giffuni             }
2479c15e1abSPedro Giffuni             // destroy temp uno param
2489c15e1abSPedro Giffuni             uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
2499c15e1abSPedro Giffuni 
2509c15e1abSPedro Giffuni             TYPELIB_DANGER_RELEASE( pParamTypeDescr );
2519c15e1abSPedro Giffuni         }
2529c15e1abSPedro Giffuni         // return
2539c15e1abSPedro Giffuni         if ( pCppReturn ) // has complex return
2549c15e1abSPedro Giffuni         {
2559c15e1abSPedro Giffuni             if ( pUnoReturn != pCppReturn ) // needs reconversion
2569c15e1abSPedro Giffuni             {
2579c15e1abSPedro Giffuni                 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
2589c15e1abSPedro Giffuni                                         pThis->getBridge()->getUno2Cpp() );
2599c15e1abSPedro Giffuni                 // destroy temp uno return
2609c15e1abSPedro Giffuni                 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
2619c15e1abSPedro Giffuni             }
2629c15e1abSPedro Giffuni             // complex return ptr is set to return reg
2639c15e1abSPedro Giffuni             *reinterpret_cast<void **>(pRegisterReturn) = pCppReturn;
2649c15e1abSPedro Giffuni         }
2659c15e1abSPedro Giffuni         if ( pReturnTypeDescr )
2669c15e1abSPedro Giffuni         {
2679c15e1abSPedro Giffuni             typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
2689c15e1abSPedro Giffuni             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
2699c15e1abSPedro Giffuni             return eRet;
2709c15e1abSPedro Giffuni         }
2719c15e1abSPedro Giffuni         else
2729c15e1abSPedro Giffuni             return typelib_TypeClass_VOID;
2739c15e1abSPedro Giffuni     }
2749c15e1abSPedro Giffuni }
2759c15e1abSPedro Giffuni 
2769c15e1abSPedro Giffuni 
2779c15e1abSPedro Giffuni //==================================================================================================
cpp_vtable_call(sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,void ** gpreg,void ** fpreg,unsigned char * ovrflw,void * pIndirectReturn,sal_uInt64 * pRegisterReturn)2789c15e1abSPedro Giffuni extern "C" sal_uInt32 cpp_vtable_call(
2799c15e1abSPedro Giffuni     sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
2809c15e1abSPedro Giffuni     void ** gpreg, void ** fpreg, unsigned char * ovrflw,
2819c15e1abSPedro Giffuni     void * pIndirectReturn, // AArch64 x8 indirect-result pointer (0 if none)
2829c15e1abSPedro Giffuni     sal_uInt64 * pRegisterReturn /* space for register return */ )
2839c15e1abSPedro Giffuni {
2849c15e1abSPedro Giffuni     // gpreg:  this, [other gpr params x0..x7]
2859c15e1abSPedro Giffuni     // fpreg:  [fpr params d0..d7]
2869c15e1abSPedro Giffuni     // ovrflw: [gpr or fpr params (properly aligned)]
2879c15e1abSPedro Giffuni     // pIndirectReturn: x8 (the hidden return buffer), when bit 0x80000000 set.
2889c15e1abSPedro Giffuni     //
2899c15e1abSPedro Giffuni     // On AArch64 'this' is ALWAYS x0 = gpreg[0]; the hidden return pointer is
2909c15e1abSPedro Giffuni     // the separate x8 register, not a displaced first GPR (unlike x86-64 SysV
2919c15e1abSPedro Giffuni     // where it occupied gpreg[0] and 'this' moved to gpreg[1]).
2929c15e1abSPedro Giffuni     if ( nFunctionIndex & 0x80000000 )
2939c15e1abSPedro Giffuni         nFunctionIndex &= 0x7fffffff;
2949c15e1abSPedro Giffuni 
2959c15e1abSPedro Giffuni     void * pThis = gpreg[0];
2969c15e1abSPedro Giffuni     pThis = static_cast<char *>( pThis ) - nVtableOffset;
2979c15e1abSPedro Giffuni 
2989c15e1abSPedro Giffuni     bridges::cpp_uno::shared::CppInterfaceProxy * pCppI =
2999c15e1abSPedro Giffuni         bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy( pThis );
3009c15e1abSPedro Giffuni 
3019c15e1abSPedro Giffuni     typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
3029c15e1abSPedro Giffuni 
3039c15e1abSPedro Giffuni     OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!\n" );
3049c15e1abSPedro Giffuni     if ( nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex )
3059c15e1abSPedro Giffuni     {
3069c15e1abSPedro Giffuni         throw RuntimeException( OUString::createFromAscii("illegal vtable index!"),
3079c15e1abSPedro Giffuni                                 reinterpret_cast<XInterface *>( pCppI ) );
3089c15e1abSPedro Giffuni     }
3099c15e1abSPedro Giffuni 
3109c15e1abSPedro Giffuni     // determine called method
3119c15e1abSPedro Giffuni     sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
3129c15e1abSPedro Giffuni     OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!\n" );
3139c15e1abSPedro Giffuni 
3149c15e1abSPedro Giffuni     TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
3159c15e1abSPedro Giffuni 
3169c15e1abSPedro Giffuni     sal_uInt32 eRet;
3179c15e1abSPedro Giffuni     switch ( aMemberDescr.get()->eTypeClass )
3189c15e1abSPedro Giffuni     {
3199c15e1abSPedro Giffuni         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
3209c15e1abSPedro Giffuni         {
3219c15e1abSPedro Giffuni             typelib_TypeDescriptionReference *pAttrTypeRef =
3229c15e1abSPedro Giffuni                 reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( aMemberDescr.get() )->pAttributeTypeRef;
3239c15e1abSPedro Giffuni 
3249c15e1abSPedro Giffuni             if ( pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex )
3259c15e1abSPedro Giffuni             {
3269c15e1abSPedro Giffuni                 // is GET method
3279c15e1abSPedro Giffuni                     eRet = cpp2uno_call( pCppI, aMemberDescr.get(), pAttrTypeRef,
3289c15e1abSPedro Giffuni                         0, 0, // no params
3299c15e1abSPedro Giffuni                         gpreg, fpreg, ovrflw, pIndirectReturn, pRegisterReturn );
3309c15e1abSPedro Giffuni                     eRet = aarch64::get_return_kind( pAttrTypeRef );
3319c15e1abSPedro Giffuni             }
3329c15e1abSPedro Giffuni             else
3339c15e1abSPedro Giffuni             {
3349c15e1abSPedro Giffuni                 // is SET method
3359c15e1abSPedro Giffuni                 typelib_MethodParameter aParam;
3369c15e1abSPedro Giffuni                 aParam.pTypeRef = pAttrTypeRef;
3379c15e1abSPedro Giffuni                 aParam.bIn      = sal_True;
3389c15e1abSPedro Giffuni                 aParam.bOut     = sal_False;
3399c15e1abSPedro Giffuni 
3409c15e1abSPedro Giffuni                 eRet = cpp2uno_call( pCppI, aMemberDescr.get(),
3419c15e1abSPedro Giffuni                         0, // indicates void return
3429c15e1abSPedro Giffuni                         1, &aParam,
3439c15e1abSPedro Giffuni                         gpreg, fpreg, ovrflw, pIndirectReturn, pRegisterReturn );
3449c15e1abSPedro Giffuni             }
3459c15e1abSPedro Giffuni             break;
3469c15e1abSPedro Giffuni         }
3479c15e1abSPedro Giffuni         case typelib_TypeClass_INTERFACE_METHOD:
3489c15e1abSPedro Giffuni         {
3499c15e1abSPedro Giffuni             // is METHOD
3509c15e1abSPedro Giffuni             switch ( nFunctionIndex )
3519c15e1abSPedro Giffuni             {
3529c15e1abSPedro Giffuni                 case 1: // acquire()
3539c15e1abSPedro Giffuni                     pCppI->acquireProxy(); // non virtual call!
3549c15e1abSPedro Giffuni                     eRet = typelib_TypeClass_VOID;
3559c15e1abSPedro Giffuni                     break;
3569c15e1abSPedro Giffuni                 case 2: // release()
3579c15e1abSPedro Giffuni                     pCppI->releaseProxy(); // non virtual call!
3589c15e1abSPedro Giffuni                     eRet = typelib_TypeClass_VOID;
3599c15e1abSPedro Giffuni                     break;
3609c15e1abSPedro Giffuni                 case 0: // queryInterface() opt
3619c15e1abSPedro Giffuni                 {
3629c15e1abSPedro Giffuni                     // queryInterface([in] type) returns an Any (> 16 bytes),
3639c15e1abSPedro Giffuni                     // so on AArch64 the result buffer is x8 (pIndirectReturn),
3649c15e1abSPedro Giffuni                     // 'this' is gpreg[0], and the type argument is the first
3659c15e1abSPedro Giffuni                     // real parameter, gpreg[1].
3669c15e1abSPedro Giffuni                     typelib_TypeDescription * pTD = 0;
3679c15e1abSPedro Giffuni                     TYPELIB_DANGER_GET( &pTD, reinterpret_cast<Type *>( gpreg[1] )->getTypeLibType() );
3689c15e1abSPedro Giffuni                     if ( pTD )
3699c15e1abSPedro Giffuni                     {
3709c15e1abSPedro Giffuni                         XInterface * pInterface = 0;
3719c15e1abSPedro Giffuni                         (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)
3729c15e1abSPedro Giffuni                             ( pCppI->getBridge()->getCppEnv(),
3739c15e1abSPedro Giffuni                               reinterpret_cast<void **>(&pInterface),
3749c15e1abSPedro Giffuni                               pCppI->getOid().pData,
3759c15e1abSPedro Giffuni                               reinterpret_cast<typelib_InterfaceTypeDescription *>( pTD ) );
3769c15e1abSPedro Giffuni 
3779c15e1abSPedro Giffuni                         if ( pInterface )
3789c15e1abSPedro Giffuni                         {
3799c15e1abSPedro Giffuni                             ::uno_any_construct( reinterpret_cast<uno_Any *>( pIndirectReturn ),
3809c15e1abSPedro Giffuni                                                  &pInterface, pTD, cpp_acquire );
3819c15e1abSPedro Giffuni 
3829c15e1abSPedro Giffuni                             pInterface->release();
3839c15e1abSPedro Giffuni                             TYPELIB_DANGER_RELEASE( pTD );
3849c15e1abSPedro Giffuni 
3859c15e1abSPedro Giffuni                             reinterpret_cast<void **>( pRegisterReturn )[0] = pIndirectReturn;
3869c15e1abSPedro Giffuni                             eRet = typelib_TypeClass_ANY;
3879c15e1abSPedro Giffuni                             break;
3889c15e1abSPedro Giffuni                         }
3899c15e1abSPedro Giffuni                         TYPELIB_DANGER_RELEASE( pTD );
3909c15e1abSPedro Giffuni                     }
3919c15e1abSPedro Giffuni                 } // else perform queryInterface()
3929c15e1abSPedro Giffuni                 default:
3939c15e1abSPedro Giffuni                 {
3949c15e1abSPedro Giffuni                     typelib_InterfaceMethodTypeDescription *pMethodTD =
3959c15e1abSPedro Giffuni                         reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( aMemberDescr.get() );
3969c15e1abSPedro Giffuni 
3979c15e1abSPedro Giffuni                     eRet = cpp2uno_call( pCppI, aMemberDescr.get(),
3989c15e1abSPedro Giffuni                                          pMethodTD->pReturnTypeRef,
3999c15e1abSPedro Giffuni                                          pMethodTD->nParams,
4009c15e1abSPedro Giffuni                                          pMethodTD->pParams,
4019c15e1abSPedro Giffuni                                          gpreg, fpreg, ovrflw, pIndirectReturn, pRegisterReturn );
4029c15e1abSPedro Giffuni                     eRet = aarch64::get_return_kind( pMethodTD->pReturnTypeRef );
4039c15e1abSPedro Giffuni                 }
4049c15e1abSPedro Giffuni             }
4059c15e1abSPedro Giffuni             break;
4069c15e1abSPedro Giffuni         }
4079c15e1abSPedro Giffuni         default:
4089c15e1abSPedro Giffuni         {
4099c15e1abSPedro Giffuni             throw RuntimeException( OUString::createFromAscii("no member description found!"),
4109c15e1abSPedro Giffuni                                     reinterpret_cast<XInterface *>( pCppI ) );
4119c15e1abSPedro Giffuni             // is here for dummy
4129c15e1abSPedro Giffuni             eRet = typelib_TypeClass_VOID;
4139c15e1abSPedro Giffuni         }
4149c15e1abSPedro Giffuni     }
4159c15e1abSPedro Giffuni 
4169c15e1abSPedro Giffuni     return eRet;
4179c15e1abSPedro Giffuni }
418dcfbcfccSPedro Giffuni 
419dcfbcfccSPedro Giffuni 
420dcfbcfccSPedro Giffuni //==================================================================================================
421dcfbcfccSPedro Giffuni extern "C" void privateSnippetExecutor( void );
422dcfbcfccSPedro Giffuni 
423dcfbcfccSPedro Giffuni const int codeSnippetSize = 40;
424dcfbcfccSPedro Giffuni 
codeSnippet(unsigned char * code,sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,bool bHasHiddenParam)425dcfbcfccSPedro Giffuni unsigned char * codeSnippet( unsigned char * code,
426dcfbcfccSPedro Giffuni     sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
427dcfbcfccSPedro Giffuni     bool bHasHiddenParam ) SAL_THROW( () )
428dcfbcfccSPedro Giffuni {
429dcfbcfccSPedro Giffuni     sal_uInt64 nOffsetAndIndex = ( static_cast<sal_uInt64>( nVtableOffset ) << 32 ) | static_cast<sal_uInt64>( nFunctionIndex );
430dcfbcfccSPedro Giffuni 
431dcfbcfccSPedro Giffuni     if ( bHasHiddenParam )
432dcfbcfccSPedro Giffuni         nOffsetAndIndex |= 0x80000000;
433dcfbcfccSPedro Giffuni 
434dcfbcfccSPedro Giffuni     sal_uInt32 * p = reinterpret_cast<sal_uInt32 *>( code );
435dcfbcfccSPedro Giffuni 
436dcfbcfccSPedro Giffuni     p[0] = 0x58000000 | ( 6 << 5 ) | 16;
437dcfbcfccSPedro Giffuni     p[1] = 0x58000000 | ( 7 << 5 ) | 17;
438dcfbcfccSPedro Giffuni     p[2] = 0xD61F0000 | ( 17 << 5 );
439dcfbcfccSPedro Giffuni     p[3] = 0xD503201F; // NOP
440dcfbcfccSPedro Giffuni     p[4] = 0xD503201F; // NOP
441dcfbcfccSPedro Giffuni 
442dcfbcfccSPedro Giffuni     *reinterpret_cast<sal_uInt64 *>( code + 24 ) = nOffsetAndIndex;
443dcfbcfccSPedro Giffuni     *reinterpret_cast<sal_uInt64 *>( code + 32 ) = reinterpret_cast<sal_uInt64>( privateSnippetExecutor );
444dcfbcfccSPedro Giffuni 
445dcfbcfccSPedro Giffuni     return code + codeSnippetSize;
446dcfbcfccSPedro Giffuni }
447dcfbcfccSPedro Giffuni 
448dcfbcfccSPedro Giffuni //==================================================================================================
449dcfbcfccSPedro Giffuni struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
450dcfbcfccSPedro Giffuni 
451dcfbcfccSPedro Giffuni bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)452dcfbcfccSPedro Giffuni bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
453dcfbcfccSPedro Giffuni {
454dcfbcfccSPedro Giffuni     return static_cast< Slot * >(block) + 2;
455dcfbcfccSPedro Giffuni }
456dcfbcfccSPedro Giffuni 
457dcfbcfccSPedro Giffuni //==================================================================================================
getBlockSize(sal_Int32 slotCount)458dcfbcfccSPedro Giffuni sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
459dcfbcfccSPedro Giffuni     sal_Int32 slotCount)
460dcfbcfccSPedro Giffuni {
461dcfbcfccSPedro Giffuni     return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
462dcfbcfccSPedro Giffuni }
463dcfbcfccSPedro Giffuni 
464dcfbcfccSPedro Giffuni //==================================================================================================
465dcfbcfccSPedro Giffuni bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)466dcfbcfccSPedro Giffuni bridges::cpp_uno::shared::VtableFactory::initializeBlock(
467dcfbcfccSPedro Giffuni     void * block, sal_Int32 slotCount)
468dcfbcfccSPedro Giffuni {
469dcfbcfccSPedro Giffuni     Slot * slots = mapBlockToVtable(block);
470dcfbcfccSPedro Giffuni     slots[-2].fn = 0;
471dcfbcfccSPedro Giffuni     slots[-1].fn = 0;
472dcfbcfccSPedro Giffuni     return slots + slotCount;
473dcfbcfccSPedro Giffuni }
474dcfbcfccSPedro Giffuni 
475dcfbcfccSPedro Giffuni //==================================================================================================
476dcfbcfccSPedro Giffuni 
addLocalFunctions(Slot ** slots,unsigned char * code,sal_PtrDiff writetoexecdiff,typelib_InterfaceTypeDescription const * type,sal_Int32 nFunctionOffset,sal_Int32 functionCount,sal_Int32 nVtableOffset)477dcfbcfccSPedro Giffuni unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
478dcfbcfccSPedro Giffuni     Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
479dcfbcfccSPedro Giffuni     typelib_InterfaceTypeDescription const * type, sal_Int32 nFunctionOffset,
480dcfbcfccSPedro Giffuni     sal_Int32 functionCount, sal_Int32 nVtableOffset )
481dcfbcfccSPedro Giffuni {
482dcfbcfccSPedro Giffuni     (*slots) -= functionCount;
483dcfbcfccSPedro Giffuni     Slot * s = *slots;
484dcfbcfccSPedro Giffuni     for ( sal_Int32 nPos = 0; nPos < type->nMembers; ++nPos )
485dcfbcfccSPedro Giffuni     {
486dcfbcfccSPedro Giffuni         typelib_TypeDescription * pTD = 0;
487dcfbcfccSPedro Giffuni 
488dcfbcfccSPedro Giffuni         TYPELIB_DANGER_GET( &pTD, type->ppMembers[ nPos ] );
489dcfbcfccSPedro Giffuni         OSL_ASSERT( pTD );
490dcfbcfccSPedro Giffuni 
491dcfbcfccSPedro Giffuni         if ( typelib_TypeClass_INTERFACE_ATTRIBUTE == pTD->eTypeClass )
492dcfbcfccSPedro Giffuni         {
493dcfbcfccSPedro Giffuni             typelib_InterfaceAttributeTypeDescription *pAttrTD =
494dcfbcfccSPedro Giffuni                 reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( pTD );
495dcfbcfccSPedro Giffuni 
496dcfbcfccSPedro Giffuni             (s++)->fn = code + writetoexecdiff;
497dcfbcfccSPedro Giffuni             code = codeSnippet( code, nFunctionOffset++, nVtableOffset,
498dcfbcfccSPedro Giffuni                                 aarch64::return_in_hidden_param( pAttrTD->pAttributeTypeRef ) );
499dcfbcfccSPedro Giffuni 
500dcfbcfccSPedro Giffuni             if ( ! pAttrTD->bReadOnly )
501dcfbcfccSPedro Giffuni             {
502dcfbcfccSPedro Giffuni                 (s++)->fn = code + writetoexecdiff;
503dcfbcfccSPedro Giffuni                 code = codeSnippet( code, nFunctionOffset++, nVtableOffset, false );
504dcfbcfccSPedro Giffuni             }
505dcfbcfccSPedro Giffuni         }
506dcfbcfccSPedro Giffuni         else if ( typelib_TypeClass_INTERFACE_METHOD == pTD->eTypeClass )
507dcfbcfccSPedro Giffuni         {
508dcfbcfccSPedro Giffuni             typelib_InterfaceMethodTypeDescription *pMethodTD =
509dcfbcfccSPedro Giffuni                 reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( pTD );
510dcfbcfccSPedro Giffuni 
511dcfbcfccSPedro Giffuni             (s++)->fn = code + writetoexecdiff;
512dcfbcfccSPedro Giffuni             code = codeSnippet( code, nFunctionOffset++, nVtableOffset,
513dcfbcfccSPedro Giffuni                                 aarch64::return_in_hidden_param( pMethodTD->pReturnTypeRef ) );
514dcfbcfccSPedro Giffuni         }
515dcfbcfccSPedro Giffuni         else
516dcfbcfccSPedro Giffuni             OSL_ASSERT( false );
517dcfbcfccSPedro Giffuni 
518dcfbcfccSPedro Giffuni         TYPELIB_DANGER_RELEASE( pTD );
519dcfbcfccSPedro Giffuni     }
520dcfbcfccSPedro Giffuni     return code;
521dcfbcfccSPedro Giffuni }
522dcfbcfccSPedro Giffuni 
523dcfbcfccSPedro Giffuni //==================================================================================================
flushCode(unsigned char const * begin,unsigned char const * end)524dcfbcfccSPedro Giffuni void bridges::cpp_uno::shared::VtableFactory::flushCode(
525dcfbcfccSPedro Giffuni     unsigned char const * begin, unsigned char const * end )
526dcfbcfccSPedro Giffuni {
527dcfbcfccSPedro Giffuni     __clear_cache(
528dcfbcfccSPedro Giffuni         const_cast<unsigned char *>(begin),
529dcfbcfccSPedro Giffuni         const_cast<unsigned char *>(end) );
530dcfbcfccSPedro Giffuni }
531