161dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
361dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
461dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
561dff127SAndrew Rist  * distributed with this work for additional information
661dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
761dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
861dff127SAndrew Rist  * "License"); you may not use this file except in compliance
961dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
1061dff127SAndrew Rist  *
1161dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1261dff127SAndrew Rist  *
1361dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1461dff127SAndrew Rist  * software distributed under the License is distributed on an
1561dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1661dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
1761dff127SAndrew Rist  * specific language governing permissions and limitations
1861dff127SAndrew Rist  * under the License.
1961dff127SAndrew Rist  *
2061dff127SAndrew Rist  *************************************************************/
2161dff127SAndrew Rist 
2261dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir 
27*de2c434cSPedro Giffuni #include <typeinfo>
28*de2c434cSPedro Giffuni #include <exception>
29*de2c434cSPedro Giffuni #include <cstddef>
30*de2c434cSPedro Giffuni #include <cxxabi.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <rtl/alloc.h>
33cdf0e10cSrcweir #include <osl/mutex.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
36cdf0e10cSrcweir #include "com/sun/star/uno/RuntimeException.hpp"
37cdf0e10cSrcweir #include <uno/data.h>
38cdf0e10cSrcweir #include <typelib/typedescription.hxx>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx"
41cdf0e10cSrcweir #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
42cdf0e10cSrcweir #include "bridges/cpp_uno/shared/types.hxx"
43cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtablefactory.hxx"
44cdf0e10cSrcweir 
45cdf0e10cSrcweir #include "abi.hxx"
46cdf0e10cSrcweir #include "share.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace ::osl;
49cdf0e10cSrcweir using namespace ::rtl;
50cdf0e10cSrcweir using namespace ::com::sun::star::uno;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir //==================================================================================================
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // Perform the UNO call
55cdf0e10cSrcweir //
56cdf0e10cSrcweir // We must convert the paramaters stored in gpreg, fpreg and ovrflw to UNO
57cdf0e10cSrcweir // arguments and call pThis->getUnoI()->pDispatcher.
58cdf0e10cSrcweir //
59cdf0e10cSrcweir // gpreg:  [ret *], this, [gpr params]
60cdf0e10cSrcweir // fpreg:  [fpr params]
61cdf0e10cSrcweir // ovrflw: [gpr or fpr params (properly aligned)]
62cdf0e10cSrcweir //
63cdf0e10cSrcweir // [ret *] is present when we are returning a structure bigger than 16 bytes
64cdf0e10cSrcweir // Simple types are returned in rax, rdx (int), or xmm0, xmm1 (fp).
65cdf0e10cSrcweir // Similarly structures <= 16 bytes are in rax, rdx, xmm0, xmm1 as necessary.
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,void ** ovrflw,sal_uInt64 * pRegisterReturn)66cdf0e10cSrcweir static typelib_TypeClass cpp2uno_call(
67cdf0e10cSrcweir 	bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
68cdf0e10cSrcweir 	const typelib_TypeDescription * pMemberTypeDescr,
69cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
70cdf0e10cSrcweir 	sal_Int32 nParams, typelib_MethodParameter * pParams,
71cdf0e10cSrcweir 	void ** gpreg, void ** fpreg, void ** ovrflw,
72cdf0e10cSrcweir 	sal_uInt64 * pRegisterReturn /* space for register return */ )
73cdf0e10cSrcweir {
7481293574SPedro Giffuni 	unsigned int nr_gpr = 0; //number of gpr registers used
7581293574SPedro Giffuni 	unsigned int nr_fpr = 0; //number of fpr registers used
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	// return
78cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
79cdf0e10cSrcweir 	if (pReturnTypeRef)
80cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	void * pUnoReturn = 0;
83cdf0e10cSrcweir 	void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 	if ( pReturnTypeDescr )
86cdf0e10cSrcweir 	{
87cdf0e10cSrcweir 		if ( x86_64::return_in_hidden_param( pReturnTypeRef ) )
88cdf0e10cSrcweir 		{
89cdf0e10cSrcweir 			pCppReturn = *gpreg++;
90cdf0e10cSrcweir 			nr_gpr++;
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 			pUnoReturn = ( bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
93cdf0e10cSrcweir 						   ? alloca( pReturnTypeDescr->nSize )
94cdf0e10cSrcweir 						   : pCppReturn ); // direct way
95cdf0e10cSrcweir 		}
96cdf0e10cSrcweir 		else
97cdf0e10cSrcweir 			pUnoReturn = pRegisterReturn; // direct way for simple types
98cdf0e10cSrcweir 	}
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	// pop this
101cdf0e10cSrcweir 	gpreg++;
102cdf0e10cSrcweir 	nr_gpr++;
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 	// stack space
105cdf0e10cSrcweir 	// parameters
106cdf0e10cSrcweir 	void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
107cdf0e10cSrcweir 	void ** pCppArgs = pUnoArgs + nParams;
108cdf0e10cSrcweir 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
109cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
110cdf0e10cSrcweir 	// type descriptions for reconversions
111cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	sal_Int32 nTempIndizes = 0;
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir 		const typelib_MethodParameter & rParam = pParams[nPos];
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 		int nUsedGPR = 0;
120cdf0e10cSrcweir 		int nUsedSSE = 0;
121942d46e3SPedro Giffuni #if OSL_DEBUG_LEVEL > 0
12281293574SPedro Giffuni 		bool bFitsRegisters =
12381293574SPedro Giffuni #endif
12481293574SPedro Giffuni 			x86_64::examine_argument( rParam.pTypeRef, false, nUsedGPR, nUsedSSE );
125942d46e3SPedro Giffuni 		if ( !rParam.bOut && bridges::cpp_uno::shared::isSimpleType( rParam.pTypeRef ) ) // value
126cdf0e10cSrcweir 		{
127cdf0e10cSrcweir 			// Simple types must fit exactly one register on x86_64
128cdf0e10cSrcweir 			OSL_ASSERT( bFitsRegisters && ( ( nUsedSSE == 1 && nUsedGPR == 0 ) || ( nUsedSSE == 0 && nUsedGPR == 1 ) ) );
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 			if ( nUsedSSE == 1 )
131cdf0e10cSrcweir 			{
132cdf0e10cSrcweir 				if ( nr_fpr < x86_64::MAX_SSE_REGS )
133cdf0e10cSrcweir 				{
134cdf0e10cSrcweir 					pCppArgs[nPos] = pUnoArgs[nPos] = fpreg++;
135cdf0e10cSrcweir 					nr_fpr++;
136cdf0e10cSrcweir 				}
137cdf0e10cSrcweir 				else
138cdf0e10cSrcweir 					pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw++;
139cdf0e10cSrcweir 			}
140cdf0e10cSrcweir 			else if ( nUsedGPR == 1 )
141cdf0e10cSrcweir 			{
142cdf0e10cSrcweir 				if ( nr_gpr < x86_64::MAX_GPR_REGS )
143cdf0e10cSrcweir 				{
144cdf0e10cSrcweir 					pCppArgs[nPos] = pUnoArgs[nPos] = gpreg++;
145cdf0e10cSrcweir 					nr_gpr++;
146cdf0e10cSrcweir 				}
147cdf0e10cSrcweir 				else
148cdf0e10cSrcweir 					pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw++;
149cdf0e10cSrcweir 			}
150cdf0e10cSrcweir 		}
151cdf0e10cSrcweir 		else // struct <= 16 bytes || ptr to complex value || ref
152cdf0e10cSrcweir 		{
153942d46e3SPedro Giffuni 			typelib_TypeDescription * pParamTypeDescr = 0;
154942d46e3SPedro Giffuni 			TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
155942d46e3SPedro Giffuni 
156cdf0e10cSrcweir 			void *pCppStack;
15781293574SPedro Giffuni 			if ( nr_gpr < x86_64::MAX_GPR_REGS )
158cdf0e10cSrcweir 			{
159cdf0e10cSrcweir 				pCppArgs[nPos] = pCppStack = *gpreg++;
160cdf0e10cSrcweir 				nr_gpr++;
161cdf0e10cSrcweir 			}
162cdf0e10cSrcweir 			else
163cdf0e10cSrcweir 				pCppArgs[nPos] = pCppStack = *ovrflw++;
164cdf0e10cSrcweir 
165cdf0e10cSrcweir 			if (! rParam.bIn) // is pure out
166cdf0e10cSrcweir 			{
167cdf0e10cSrcweir 				// uno out is unconstructed mem!
168cdf0e10cSrcweir 				pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
169cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos;
170cdf0e10cSrcweir 				// will be released at reconversion
171cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
172cdf0e10cSrcweir 			}
173cdf0e10cSrcweir 			else if ( bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ) ) // is in/inout
174cdf0e10cSrcweir 			{
175cdf0e10cSrcweir 				uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
176cdf0e10cSrcweir 										pCppStack, pParamTypeDescr,
177cdf0e10cSrcweir 										pThis->getBridge()->getCpp2Uno() );
178cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
179cdf0e10cSrcweir 				// will be released at reconversion
180cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
181cdf0e10cSrcweir 			}
182cdf0e10cSrcweir 			else // direct way
183cdf0e10cSrcweir 			{
184cdf0e10cSrcweir 				pUnoArgs[nPos] = pCppStack;
185cdf0e10cSrcweir 				// no longer needed
186cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
187cdf0e10cSrcweir 			}
188cdf0e10cSrcweir 		}
189cdf0e10cSrcweir 	}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 	// ExceptionHolder
192cdf0e10cSrcweir 	uno_Any aUnoExc; // Any will be constructed by callee
193cdf0e10cSrcweir 	uno_Any * pUnoExc = &aUnoExc;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	// invoke uno dispatch call
196cdf0e10cSrcweir 	(*pThis->getUnoI()->pDispatcher)( pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir 	// in case an exception occured...
199cdf0e10cSrcweir 	if ( pUnoExc )
200cdf0e10cSrcweir 	{
201cdf0e10cSrcweir 		// destruct temporary in/inout params
202cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 			if (pParams[nIndex].bIn) // is in/inout => was constructed
207cdf0e10cSrcweir 				uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
208cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
209cdf0e10cSrcweir 		}
210cdf0e10cSrcweir 		if (pReturnTypeDescr)
211cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 		CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc, pThis->getBridge()->getUno2Cpp() ); // has to destruct the any
214cdf0e10cSrcweir 		// is here for dummy
215cdf0e10cSrcweir 		return typelib_TypeClass_VOID;
216cdf0e10cSrcweir 	}
217cdf0e10cSrcweir 	else // else no exception occured...
218cdf0e10cSrcweir 	{
219cdf0e10cSrcweir 		// temporary params
220cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
221cdf0e10cSrcweir 		{
222cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
223cdf0e10cSrcweir 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 			if ( pParams[nIndex].bOut ) // inout/out
226cdf0e10cSrcweir 			{
227cdf0e10cSrcweir 				// convert and assign
228cdf0e10cSrcweir 				uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
229cdf0e10cSrcweir 				uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
230cdf0e10cSrcweir 										pThis->getBridge()->getUno2Cpp() );
231cdf0e10cSrcweir 			}
232cdf0e10cSrcweir 			// destroy temp uno param
233cdf0e10cSrcweir 			uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
236cdf0e10cSrcweir 		}
237cdf0e10cSrcweir 		// return
238cdf0e10cSrcweir 		if ( pCppReturn ) // has complex return
239cdf0e10cSrcweir 		{
240cdf0e10cSrcweir 			if ( pUnoReturn != pCppReturn ) // needs reconversion
241cdf0e10cSrcweir 			{
242cdf0e10cSrcweir 				uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
243cdf0e10cSrcweir 										pThis->getBridge()->getUno2Cpp() );
244cdf0e10cSrcweir 				// destroy temp uno return
245cdf0e10cSrcweir 				uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
246cdf0e10cSrcweir 			}
247cdf0e10cSrcweir 			// complex return ptr is set to return reg
248cdf0e10cSrcweir 			*(void **)pRegisterReturn = pCppReturn;
249cdf0e10cSrcweir 		}
250cdf0e10cSrcweir 		if ( pReturnTypeDescr )
251cdf0e10cSrcweir 		{
252cdf0e10cSrcweir 			typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
253cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
254cdf0e10cSrcweir 			return eRet;
255cdf0e10cSrcweir 		}
256cdf0e10cSrcweir 		else
257cdf0e10cSrcweir 			return typelib_TypeClass_VOID;
258cdf0e10cSrcweir 	}
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
261cdf0e10cSrcweir 
262cdf0e10cSrcweir //==================================================================================================
cpp_vtable_call(sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,void ** gpreg,void ** fpreg,void ** ovrflw,sal_uInt64 * pRegisterReturn)263cdf0e10cSrcweir extern "C" typelib_TypeClass cpp_vtable_call(
264cdf0e10cSrcweir 	sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
265cdf0e10cSrcweir 	void ** gpreg, void ** fpreg, void ** ovrflw,
266cdf0e10cSrcweir 	sal_uInt64 * pRegisterReturn /* space for register return */ )
267cdf0e10cSrcweir {
268cdf0e10cSrcweir 	// gpreg:  [ret *], this, [other gpr params]
269cdf0e10cSrcweir 	// fpreg:  [fpr params]
270cdf0e10cSrcweir 	// ovrflw: [gpr or fpr params (properly aligned)]
271cdf0e10cSrcweir 	void * pThis;
272cdf0e10cSrcweir 	if ( nFunctionIndex & 0x80000000 )
273cdf0e10cSrcweir 	{
274cdf0e10cSrcweir 		nFunctionIndex &= 0x7fffffff;
275cdf0e10cSrcweir 		pThis = gpreg[1];
276cdf0e10cSrcweir 	}
277cdf0e10cSrcweir 	else
278cdf0e10cSrcweir 	{
279cdf0e10cSrcweir 		pThis = gpreg[0];
280cdf0e10cSrcweir 	}
281cdf0e10cSrcweir 	pThis = static_cast<char *>( pThis ) - nVtableOffset;
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 	bridges::cpp_uno::shared::CppInterfaceProxy * pCppI =
284cdf0e10cSrcweir 		bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy( pThis );
285cdf0e10cSrcweir 
286cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
287cdf0e10cSrcweir 
288cdf0e10cSrcweir 	OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!\n" );
289cdf0e10cSrcweir 	if ( nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex )
290cdf0e10cSrcweir 	{
291cdf0e10cSrcweir 		throw RuntimeException( OUString::createFromAscii("illegal vtable index!"),
292cdf0e10cSrcweir 								reinterpret_cast<XInterface *>( pCppI ) );
293cdf0e10cSrcweir 	}
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	// determine called method
296cdf0e10cSrcweir 	sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
297cdf0e10cSrcweir 	OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!\n" );
298cdf0e10cSrcweir 
299cdf0e10cSrcweir 	TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	typelib_TypeClass eRet;
302cdf0e10cSrcweir 	switch ( aMemberDescr.get()->eTypeClass )
303cdf0e10cSrcweir 	{
304cdf0e10cSrcweir 		case typelib_TypeClass_INTERFACE_ATTRIBUTE:
305cdf0e10cSrcweir 		{
306cdf0e10cSrcweir 			typelib_TypeDescriptionReference *pAttrTypeRef =
307cdf0e10cSrcweir 				reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( aMemberDescr.get() )->pAttributeTypeRef;
308cdf0e10cSrcweir 
309cdf0e10cSrcweir 			if ( pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex )
310cdf0e10cSrcweir 			{
311cdf0e10cSrcweir 				// is GET method
312cdf0e10cSrcweir 				eRet = cpp2uno_call( pCppI, aMemberDescr.get(), pAttrTypeRef,
313cdf0e10cSrcweir 						0, 0, // no params
314cdf0e10cSrcweir 						gpreg, fpreg, ovrflw, pRegisterReturn );
315cdf0e10cSrcweir 			}
316cdf0e10cSrcweir 			else
317cdf0e10cSrcweir 			{
318cdf0e10cSrcweir 				// is SET method
319cdf0e10cSrcweir 				typelib_MethodParameter aParam;
320cdf0e10cSrcweir 				aParam.pTypeRef = pAttrTypeRef;
321cdf0e10cSrcweir 				aParam.bIn		= sal_True;
322cdf0e10cSrcweir 				aParam.bOut		= sal_False;
323cdf0e10cSrcweir 
324cdf0e10cSrcweir 				eRet = cpp2uno_call( pCppI, aMemberDescr.get(),
325cdf0e10cSrcweir 						0, // indicates void return
326cdf0e10cSrcweir 						1, &aParam,
327cdf0e10cSrcweir 						gpreg, fpreg, ovrflw, pRegisterReturn );
328cdf0e10cSrcweir 			}
329cdf0e10cSrcweir 			break;
330cdf0e10cSrcweir 		}
331cdf0e10cSrcweir 		case typelib_TypeClass_INTERFACE_METHOD:
332cdf0e10cSrcweir 		{
333cdf0e10cSrcweir 			// is METHOD
334cdf0e10cSrcweir 			switch ( nFunctionIndex )
335cdf0e10cSrcweir 			{
336cdf0e10cSrcweir 				case 1: // acquire()
337cdf0e10cSrcweir 					pCppI->acquireProxy(); // non virtual call!
338cdf0e10cSrcweir 					eRet = typelib_TypeClass_VOID;
339cdf0e10cSrcweir 					break;
340cdf0e10cSrcweir 				case 2: // release()
341cdf0e10cSrcweir 					pCppI->releaseProxy(); // non virtual call!
342cdf0e10cSrcweir 					eRet = typelib_TypeClass_VOID;
343cdf0e10cSrcweir 					break;
344cdf0e10cSrcweir 				case 0: // queryInterface() opt
345cdf0e10cSrcweir 				{
346cdf0e10cSrcweir 					typelib_TypeDescription * pTD = 0;
347cdf0e10cSrcweir 					TYPELIB_DANGER_GET( &pTD, reinterpret_cast<Type *>( gpreg[2] )->getTypeLibType() );
348cdf0e10cSrcweir 					if ( pTD )
349cdf0e10cSrcweir 					{
350cdf0e10cSrcweir 						XInterface * pInterface = 0;
351cdf0e10cSrcweir 						(*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)
352cdf0e10cSrcweir 							( pCppI->getBridge()->getCppEnv(),
353cdf0e10cSrcweir 							  (void **)&pInterface,
354cdf0e10cSrcweir 							  pCppI->getOid().pData,
355cdf0e10cSrcweir 							  reinterpret_cast<typelib_InterfaceTypeDescription *>( pTD ) );
356cdf0e10cSrcweir 
357cdf0e10cSrcweir 						if ( pInterface )
358cdf0e10cSrcweir 						{
359cdf0e10cSrcweir 							::uno_any_construct( reinterpret_cast<uno_Any *>( gpreg[0] ),
360cdf0e10cSrcweir 												 &pInterface, pTD, cpp_acquire );
361cdf0e10cSrcweir 
362cdf0e10cSrcweir 							pInterface->release();
363cdf0e10cSrcweir 							TYPELIB_DANGER_RELEASE( pTD );
364cdf0e10cSrcweir 
365cdf0e10cSrcweir 							reinterpret_cast<void **>( pRegisterReturn )[0] = gpreg[0];
366cdf0e10cSrcweir 							eRet = typelib_TypeClass_ANY;
367cdf0e10cSrcweir 							break;
368cdf0e10cSrcweir 						}
369cdf0e10cSrcweir 						TYPELIB_DANGER_RELEASE( pTD );
370cdf0e10cSrcweir 					}
371cdf0e10cSrcweir 				} // else perform queryInterface()
372cdf0e10cSrcweir 				default:
373cdf0e10cSrcweir 				{
374cdf0e10cSrcweir 					typelib_InterfaceMethodTypeDescription *pMethodTD =
375cdf0e10cSrcweir 						reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( aMemberDescr.get() );
376cdf0e10cSrcweir 
377cdf0e10cSrcweir 					eRet = cpp2uno_call( pCppI, aMemberDescr.get(),
378cdf0e10cSrcweir 										 pMethodTD->pReturnTypeRef,
379cdf0e10cSrcweir 										 pMethodTD->nParams,
380cdf0e10cSrcweir 										 pMethodTD->pParams,
381cdf0e10cSrcweir 										 gpreg, fpreg, ovrflw, pRegisterReturn );
382cdf0e10cSrcweir 				}
383cdf0e10cSrcweir 			}
384cdf0e10cSrcweir 			break;
385cdf0e10cSrcweir 		}
386cdf0e10cSrcweir 		default:
387cdf0e10cSrcweir 		{
388cdf0e10cSrcweir 			throw RuntimeException( OUString::createFromAscii("no member description found!"),
389cdf0e10cSrcweir 									reinterpret_cast<XInterface *>( pCppI ) );
390cdf0e10cSrcweir 			// is here for dummy
391cdf0e10cSrcweir 			eRet = typelib_TypeClass_VOID;
392cdf0e10cSrcweir 		}
393cdf0e10cSrcweir 	}
394cdf0e10cSrcweir 
395cdf0e10cSrcweir 	return eRet;
396cdf0e10cSrcweir }
397cdf0e10cSrcweir 
398cdf0e10cSrcweir //==================================================================================================
399cdf0e10cSrcweir extern "C" void privateSnippetExecutor( ... );
400cdf0e10cSrcweir 
401cdf0e10cSrcweir const int codeSnippetSize = 24;
402cdf0e10cSrcweir 
403cdf0e10cSrcweir // Generate a trampoline that redirects method calls to
404cdf0e10cSrcweir // privateSnippetExecutor().
405cdf0e10cSrcweir //
406cdf0e10cSrcweir // privateSnippetExecutor() saves all the registers that are used for
407cdf0e10cSrcweir // parameter passing on x86_64, and calls the cpp_vtable_call().
408cdf0e10cSrcweir // When it returns, privateSnippetExecutor() sets the return value.
409cdf0e10cSrcweir //
410cdf0e10cSrcweir // Note: The code snippet we build here must not create a stack frame,
411cdf0e10cSrcweir // otherwise the UNO exceptions stop working thanks to non-existing
412cdf0e10cSrcweir // unwinding info.
codeSnippet(unsigned char * code,sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,bool bHasHiddenParam)413cdf0e10cSrcweir unsigned char * codeSnippet( unsigned char * code,
414cdf0e10cSrcweir         sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
415cdf0e10cSrcweir         bool bHasHiddenParam ) SAL_THROW( () )
416cdf0e10cSrcweir {
417cdf0e10cSrcweir 	sal_uInt64 nOffsetAndIndex = ( ( (sal_uInt64) nVtableOffset ) << 32 ) | ( (sal_uInt64) nFunctionIndex );
418cdf0e10cSrcweir 
419cdf0e10cSrcweir 	if ( bHasHiddenParam )
420cdf0e10cSrcweir 		nOffsetAndIndex |= 0x80000000;
421cdf0e10cSrcweir 
422cdf0e10cSrcweir 	// movq $<nOffsetAndIndex>, %r10
423cdf0e10cSrcweir 	*reinterpret_cast<sal_uInt16 *>( code ) = 0xba49;
424cdf0e10cSrcweir 	*reinterpret_cast<sal_uInt64 *>( code + 2 ) = nOffsetAndIndex;
425cdf0e10cSrcweir 
426cdf0e10cSrcweir 	// movq $<address of the privateSnippetExecutor>, %r11
427cdf0e10cSrcweir 	*reinterpret_cast<sal_uInt16 *>( code + 10 ) = 0xbb49;
428cdf0e10cSrcweir 	*reinterpret_cast<sal_uInt64 *>( code + 12 ) = reinterpret_cast<sal_uInt64>( privateSnippetExecutor );
429cdf0e10cSrcweir 
430cdf0e10cSrcweir 	// jmpq *%r11
431cdf0e10cSrcweir 	*reinterpret_cast<sal_uInt32 *>( code + 20 ) = 0x00e3ff49;
432cdf0e10cSrcweir 
433cdf0e10cSrcweir 	return code + codeSnippetSize;
434cdf0e10cSrcweir }
435cdf0e10cSrcweir 
436cdf0e10cSrcweir //==================================================================================================
437cdf0e10cSrcweir struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
438cdf0e10cSrcweir 
439cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)440cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
441cdf0e10cSrcweir {
442cdf0e10cSrcweir     return static_cast< Slot * >(block) + 2;
443cdf0e10cSrcweir }
444cdf0e10cSrcweir 
445cdf0e10cSrcweir //==================================================================================================
getBlockSize(sal_Int32 slotCount)446cdf0e10cSrcweir sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
447cdf0e10cSrcweir     sal_Int32 slotCount)
448cdf0e10cSrcweir {
449cdf0e10cSrcweir     return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
450cdf0e10cSrcweir }
451cdf0e10cSrcweir 
452cdf0e10cSrcweir //==================================================================================================
453cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)454cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::initializeBlock(
455cdf0e10cSrcweir     void * block, sal_Int32 slotCount)
456cdf0e10cSrcweir {
457cdf0e10cSrcweir     Slot * slots = mapBlockToVtable(block);
458cdf0e10cSrcweir     slots[-2].fn = 0;
459cdf0e10cSrcweir     slots[-1].fn = 0;
460cdf0e10cSrcweir     return slots + slotCount;
461cdf0e10cSrcweir }
462cdf0e10cSrcweir 
463cdf0e10cSrcweir //==================================================================================================
464cdf0e10cSrcweir 
addLocalFunctions(Slot ** slots,unsigned char * code,sal_PtrDiff writetoexecdiff,typelib_InterfaceTypeDescription const * type,sal_Int32 nFunctionOffset,sal_Int32 functionCount,sal_Int32 nVtableOffset)465cdf0e10cSrcweir unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
46681293574SPedro Giffuni 	Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
467cdf0e10cSrcweir 	typelib_InterfaceTypeDescription const * type, sal_Int32 nFunctionOffset,
468cdf0e10cSrcweir 	sal_Int32 functionCount, sal_Int32 nVtableOffset )
469cdf0e10cSrcweir {
470cdf0e10cSrcweir 	(*slots) -= functionCount;
471cdf0e10cSrcweir 	Slot * s = *slots;
472cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < type->nMembers; ++nPos )
473cdf0e10cSrcweir 	{
474cdf0e10cSrcweir 		typelib_TypeDescription * pTD = 0;
475cdf0e10cSrcweir 
476cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pTD, type->ppMembers[ nPos ] );
477cdf0e10cSrcweir 		OSL_ASSERT( pTD );
478cdf0e10cSrcweir 
479cdf0e10cSrcweir 		if ( typelib_TypeClass_INTERFACE_ATTRIBUTE == pTD->eTypeClass )
480cdf0e10cSrcweir 		{
481cdf0e10cSrcweir 			typelib_InterfaceAttributeTypeDescription *pAttrTD =
482cdf0e10cSrcweir 				reinterpret_cast<typelib_InterfaceAttributeTypeDescription *>( pTD );
483cdf0e10cSrcweir 
484cdf0e10cSrcweir 			// get method
48581293574SPedro Giffuni 			(s++)->fn = code + writetoexecdiff;
486cdf0e10cSrcweir 			code = codeSnippet( code, nFunctionOffset++, nVtableOffset,
487cdf0e10cSrcweir 								x86_64::return_in_hidden_param( pAttrTD->pAttributeTypeRef ) );
488cdf0e10cSrcweir 
489cdf0e10cSrcweir 			if ( ! pAttrTD->bReadOnly )
490cdf0e10cSrcweir 			{
491cdf0e10cSrcweir 				// set method
49281293574SPedro Giffuni 				(s++)->fn = code + writetoexecdiff;
493cdf0e10cSrcweir 				code = codeSnippet( code, nFunctionOffset++, nVtableOffset, false );
494cdf0e10cSrcweir 			}
495cdf0e10cSrcweir 		}
496cdf0e10cSrcweir 		else if ( typelib_TypeClass_INTERFACE_METHOD == pTD->eTypeClass )
497cdf0e10cSrcweir 		{
498cdf0e10cSrcweir 			typelib_InterfaceMethodTypeDescription *pMethodTD =
499cdf0e10cSrcweir 				reinterpret_cast<typelib_InterfaceMethodTypeDescription *>( pTD );
500cdf0e10cSrcweir 
50181293574SPedro Giffuni 			(s++)->fn = code + writetoexecdiff;
502cdf0e10cSrcweir 			code = codeSnippet( code, nFunctionOffset++, nVtableOffset,
503cdf0e10cSrcweir 								x86_64::return_in_hidden_param( pMethodTD->pReturnTypeRef ) );
504cdf0e10cSrcweir 		}
505cdf0e10cSrcweir 		else
506cdf0e10cSrcweir 			OSL_ASSERT( false );
507cdf0e10cSrcweir 
508cdf0e10cSrcweir 		TYPELIB_DANGER_RELEASE( pTD );
509cdf0e10cSrcweir 	}
510cdf0e10cSrcweir 	return code;
511cdf0e10cSrcweir }
512cdf0e10cSrcweir 
513cdf0e10cSrcweir //==================================================================================================
flushCode(unsigned char const *,unsigned char const *)514cdf0e10cSrcweir void bridges::cpp_uno::shared::VtableFactory::flushCode(
515cdf0e10cSrcweir 	unsigned char const *, unsigned char const * )
516cdf0e10cSrcweir {
517cdf0e10cSrcweir }
518