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