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 <sal/alloca.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
30cdf0e10cSrcweir #include <uno/data.h>
31cdf0e10cSrcweir #include <typelib/typedescription.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx"
34cdf0e10cSrcweir #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
35cdf0e10cSrcweir #include "bridges/cpp_uno/shared/types.hxx"
36cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtablefactory.hxx"
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #include "cc50_solaris_intel.hxx"
39cdf0e10cSrcweir 
40cdf0e10cSrcweir using namespace com::sun::star::uno;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir namespace
43cdf0e10cSrcweir {
44cdf0e10cSrcweir 
45cdf0e10cSrcweir //==================================================================================================
cpp2uno_call(bridges::cpp_uno::shared::CppInterfaceProxy * pThis,const typelib_TypeDescription * pMemberTypeDescr,typelib_TypeDescriptionReference * pReturnTypeRef,sal_Int32 nParams,typelib_MethodParameter * pParams,void ** pCallStack,sal_Int64 * pRegisterReturn)46cdf0e10cSrcweir void cpp2uno_call(
47cdf0e10cSrcweir 	bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
48cdf0e10cSrcweir 	const typelib_TypeDescription * pMemberTypeDescr,
49cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
50cdf0e10cSrcweir 	sal_Int32 nParams, typelib_MethodParameter * pParams,
51cdf0e10cSrcweir 	void ** pCallStack,
52cdf0e10cSrcweir 	sal_Int64 * pRegisterReturn /* space for register return */ )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir 	// pCallStack: ret, [return ptr], this, params
55cdf0e10cSrcweir 	char * pCppStack = (char *)(pCallStack +1);
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 	// return
58cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
59cdf0e10cSrcweir 	if (pReturnTypeRef)
60cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	void * pUnoReturn = 0;
63cdf0e10cSrcweir 	void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 	if (pReturnTypeDescr)
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir 		if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
68cdf0e10cSrcweir 		{
69cdf0e10cSrcweir 			pUnoReturn = pRegisterReturn; // direct way for simple types
70cdf0e10cSrcweir 		}
71cdf0e10cSrcweir 		else // complex return via ptr (pCppReturn)
72cdf0e10cSrcweir 		{
73cdf0e10cSrcweir 			pCppReturn = *(void**)pCppStack;
74cdf0e10cSrcweir 			pCppStack += sizeof( void* );
75cdf0e10cSrcweir 			pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
76cdf0e10cSrcweir                               pReturnTypeDescr )
77cdf0e10cSrcweir 						  ? alloca( pReturnTypeDescr->nSize )
78cdf0e10cSrcweir 						  : pCppReturn); // direct way
79cdf0e10cSrcweir 		}
80cdf0e10cSrcweir 	}
81cdf0e10cSrcweir 	// pop this
82cdf0e10cSrcweir 	pCppStack += sizeof( void* );
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	// stack space
85cdf0e10cSrcweir 	OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
86cdf0e10cSrcweir 	// parameters
87cdf0e10cSrcweir 	void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
88cdf0e10cSrcweir 	void ** pCppArgs = pUnoArgs + nParams;
89cdf0e10cSrcweir 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
90cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
91cdf0e10cSrcweir 	// type descriptions for reconversions
92cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	sal_Int32 nTempIndizes   = 0;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
97cdf0e10cSrcweir 	{
98cdf0e10cSrcweir 		const typelib_MethodParameter & rParam = pParams[nPos];
99cdf0e10cSrcweir 		typelib_TypeDescription * pParamTypeDescr = 0;
100cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		if (!rParam.bOut
103cdf0e10cSrcweir             && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
104cdf0e10cSrcweir             // value
105cdf0e10cSrcweir 		{
106cdf0e10cSrcweir 			pCppArgs[ nPos ] = pUnoArgs[ nPos ] = pCppStack;
107cdf0e10cSrcweir 			switch (pParamTypeDescr->eTypeClass)
108cdf0e10cSrcweir 			{
109cdf0e10cSrcweir 			case typelib_TypeClass_HYPER:
110cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_HYPER:
111cdf0e10cSrcweir 			case typelib_TypeClass_DOUBLE:
112cdf0e10cSrcweir 				pCppStack += sizeof(sal_Int32); // extra long
113cdf0e10cSrcweir 			}
114cdf0e10cSrcweir 			// no longer needed
115cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir 		else // ptr to complex value | ref
118cdf0e10cSrcweir 		{
119cdf0e10cSrcweir 			pCppArgs[nPos] = *(void **)pCppStack;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir 			if (! rParam.bIn) // is pure out
122cdf0e10cSrcweir 			{
123cdf0e10cSrcweir 				// uno out is unconstructed mem!
124cdf0e10cSrcweir 				pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
125cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos;
126cdf0e10cSrcweir 				// will be released at reconversion
127cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
128cdf0e10cSrcweir 			}
129cdf0e10cSrcweir 			// is in/inout
130cdf0e10cSrcweir 			else if (bridges::cpp_uno::shared::relatesToInterfaceType(
131cdf0e10cSrcweir                          pParamTypeDescr ))
132cdf0e10cSrcweir 			{
133cdf0e10cSrcweir 				uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
134cdf0e10cSrcweir 										*(void **)pCppStack, pParamTypeDescr,
135cdf0e10cSrcweir 										pThis->getBridge()->getCpp2Uno() );
136cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
137cdf0e10cSrcweir 				// will be released at reconversion
138cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
139cdf0e10cSrcweir 			}
140cdf0e10cSrcweir 			else // direct way
141cdf0e10cSrcweir 			{
142cdf0e10cSrcweir 				pUnoArgs[nPos] = *(void **)pCppStack;
143cdf0e10cSrcweir 				// no longer needed
144cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
145cdf0e10cSrcweir 			}
146cdf0e10cSrcweir 		}
147cdf0e10cSrcweir 		pCppStack += sizeof(sal_Int32); // standard parameter length
148cdf0e10cSrcweir 	}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 	// ExceptionHolder
151cdf0e10cSrcweir 	uno_Any aUnoExc; // Any will be constructed by callee
152cdf0e10cSrcweir 	uno_Any * pUnoExc = &aUnoExc;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	// invoke uno dispatch call
155cdf0e10cSrcweir 	(*pThis->getUnoI()->pDispatcher)(
156cdf0e10cSrcweir         pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
157cdf0e10cSrcweir 
158*07a3d7f1SPedro Giffuni 	// in case no exception occurred...
159cdf0e10cSrcweir 	if (pUnoExc)
160cdf0e10cSrcweir 	{
161cdf0e10cSrcweir 		// destruct temporary in/inout params
162cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
163cdf0e10cSrcweir 		{
164cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 			if (pParams[nIndex].bIn) // is in/inout => was constructed
167cdf0e10cSrcweir 				uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
168cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
169cdf0e10cSrcweir 		}
170cdf0e10cSrcweir 		if (pReturnTypeDescr)
171cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
172cdf0e10cSrcweir 
173cdf0e10cSrcweir 		CPPU_CURRENT_NAMESPACE::cc50_solaris_intel_raiseException(
174cdf0e10cSrcweir             &aUnoExc, pThis->getBridge()->getUno2Cpp() );
175cdf0e10cSrcweir             // has to destruct the any
176cdf0e10cSrcweir 	}
177*07a3d7f1SPedro Giffuni 	else // else no exception occurred...
178cdf0e10cSrcweir 	{
179cdf0e10cSrcweir 		// temporary params
180cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
183cdf0e10cSrcweir 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 			if (pParams[nIndex].bOut) // inout/out
186cdf0e10cSrcweir 			{
187cdf0e10cSrcweir 				// convert and assign
188cdf0e10cSrcweir 				uno_destructData(
189cdf0e10cSrcweir                     pCppArgs[nIndex], pParamTypeDescr,
190cdf0e10cSrcweir                     reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
191cdf0e10cSrcweir 				uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
192cdf0e10cSrcweir 										pThis->getBridge()->getUno2Cpp() );
193cdf0e10cSrcweir 			}
194cdf0e10cSrcweir 			// destroy temp uno param
195cdf0e10cSrcweir 			uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
198cdf0e10cSrcweir 		}
199cdf0e10cSrcweir 		// return
200cdf0e10cSrcweir 		if (pCppReturn) // has complex return
201cdf0e10cSrcweir 		{
202cdf0e10cSrcweir 			if (pUnoReturn != pCppReturn) // needs reconversion
203cdf0e10cSrcweir 			{
204cdf0e10cSrcweir 				uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
205cdf0e10cSrcweir 										pThis->getBridge()->getUno2Cpp() );
206cdf0e10cSrcweir 				// destroy temp uno return
207cdf0e10cSrcweir 				uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
208cdf0e10cSrcweir 			}
209cdf0e10cSrcweir 			// complex return ptr is set to eax
210cdf0e10cSrcweir 			*(void **)pRegisterReturn = pCppReturn;
211cdf0e10cSrcweir 		}
212cdf0e10cSrcweir 		if (pReturnTypeDescr)
213cdf0e10cSrcweir 		{
214cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
215cdf0e10cSrcweir 		}
216cdf0e10cSrcweir 	}
217cdf0e10cSrcweir }
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220cdf0e10cSrcweir //==================================================================================================
cpp_vtable_call(int nFunctionIndex,int nVtableOffset,void ** pCallStack,sal_Int64 nRegReturn)221cdf0e10cSrcweir extern "C" void cpp_vtable_call(
222cdf0e10cSrcweir     int nFunctionIndex, int nVtableOffset, void** pCallStack,
223cdf0e10cSrcweir     sal_Int64 nRegReturn )
224cdf0e10cSrcweir {
225cdf0e10cSrcweir 	OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
226cdf0e10cSrcweir 
227cdf0e10cSrcweir 	// pCallStack: ret adr, [ret *], this, params
228cdf0e10cSrcweir     void * pThis;
229cdf0e10cSrcweir 	if( nFunctionIndex & 0x80000000 )
230cdf0e10cSrcweir 	{
231cdf0e10cSrcweir 		nFunctionIndex &= 0x7fffffff;
232cdf0e10cSrcweir 		pThis = pCallStack[2];
233cdf0e10cSrcweir 	}
234cdf0e10cSrcweir 	else
235cdf0e10cSrcweir     {
236cdf0e10cSrcweir 		pThis = pCallStack[1];
237cdf0e10cSrcweir     }
238cdf0e10cSrcweir     pThis = static_cast< char * >(pThis) - nVtableOffset;
239cdf0e10cSrcweir     bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
240cdf0e10cSrcweir         = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
241cdf0e10cSrcweir             pThis);
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
244cdf0e10cSrcweir 
245cdf0e10cSrcweir 	OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex,
246cdf0e10cSrcweir 				 "### illegal vtable index!" );
247cdf0e10cSrcweir 	if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
248cdf0e10cSrcweir 	{
249cdf0e10cSrcweir 		throw RuntimeException( rtl::OUString::createFromAscii("illegal vtable index!"), (XInterface *)pThis );
250cdf0e10cSrcweir 	}
251cdf0e10cSrcweir 
252cdf0e10cSrcweir 	// determine called method
253cdf0e10cSrcweir 	sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
254cdf0e10cSrcweir 	OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
255cdf0e10cSrcweir 
256cdf0e10cSrcweir 	TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	switch (aMemberDescr.get()->eTypeClass)
259cdf0e10cSrcweir 	{
260cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
261cdf0e10cSrcweir 	{
262cdf0e10cSrcweir 		if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
263cdf0e10cSrcweir 		{
264cdf0e10cSrcweir 			// is GET method
265cdf0e10cSrcweir 			cpp2uno_call(
266cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
267cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
268cdf0e10cSrcweir 				0, 0, // no params
269cdf0e10cSrcweir 				pCallStack, &nRegReturn );
270cdf0e10cSrcweir 		}
271cdf0e10cSrcweir 		else
272cdf0e10cSrcweir 		{
273cdf0e10cSrcweir 			// is SET method
274cdf0e10cSrcweir 			typelib_MethodParameter aParam;
275cdf0e10cSrcweir 			aParam.pTypeRef =
276cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
277cdf0e10cSrcweir 			aParam.bIn		= sal_True;
278cdf0e10cSrcweir 			aParam.bOut		= sal_False;
279cdf0e10cSrcweir 
280cdf0e10cSrcweir 			cpp2uno_call(
281cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
282cdf0e10cSrcweir 				0, // indicates void return
283cdf0e10cSrcweir 				1, &aParam,
284cdf0e10cSrcweir 				pCallStack, &nRegReturn );
285cdf0e10cSrcweir 		}
286cdf0e10cSrcweir 		break;
287cdf0e10cSrcweir 	}
288cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_METHOD:
289cdf0e10cSrcweir 	{
290cdf0e10cSrcweir 		// is METHOD
291cdf0e10cSrcweir 		switch (nFunctionIndex)
292cdf0e10cSrcweir 		{
293cdf0e10cSrcweir 			// standard XInterface vtable calls
294cdf0e10cSrcweir 		case 1: // acquire()
295cdf0e10cSrcweir 			pCppI->acquireProxy(); // non virtual call!
296cdf0e10cSrcweir 			break;
297cdf0e10cSrcweir 		case 2: // release()
298cdf0e10cSrcweir 			pCppI->releaseProxy(); // non virtual call!
299cdf0e10cSrcweir 			break;
300cdf0e10cSrcweir 		case 0: // queryInterface() opt
301cdf0e10cSrcweir 		{
302cdf0e10cSrcweir 			typelib_TypeDescription * pTD = 0;
303cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[3] )->getTypeLibType() );
304cdf0e10cSrcweir 			if (pTD)
305cdf0e10cSrcweir 			{
306cdf0e10cSrcweir                 XInterface * pInterface = 0;
307cdf0e10cSrcweir                 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
308cdf0e10cSrcweir                     pCppI->getBridge()->getCppEnv(),
309cdf0e10cSrcweir                     (void **)&pInterface, pCppI->getOid().pData,
310cdf0e10cSrcweir                     (typelib_InterfaceTypeDescription *)pTD );
311cdf0e10cSrcweir 
312cdf0e10cSrcweir                 if (pInterface)
313cdf0e10cSrcweir                 {
314cdf0e10cSrcweir                     ::uno_any_construct(
315cdf0e10cSrcweir                         reinterpret_cast< uno_Any * >( pCallStack[1] ),
316cdf0e10cSrcweir                         &pInterface, pTD,
317cdf0e10cSrcweir                         reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
318cdf0e10cSrcweir                     pInterface->release();
319cdf0e10cSrcweir                     TYPELIB_DANGER_RELEASE( pTD );
320cdf0e10cSrcweir                     *(void **)&nRegReturn = pCallStack[1];
321cdf0e10cSrcweir                     break;
322cdf0e10cSrcweir                 }
323cdf0e10cSrcweir                 TYPELIB_DANGER_RELEASE( pTD );
324cdf0e10cSrcweir             }
325cdf0e10cSrcweir 		} // else perform queryInterface()
326cdf0e10cSrcweir 		default:
327cdf0e10cSrcweir 			cpp2uno_call(
328cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
329cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
330cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
331cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
332cdf0e10cSrcweir 				pCallStack, &nRegReturn );
333cdf0e10cSrcweir 		}
334cdf0e10cSrcweir 		break;
335cdf0e10cSrcweir 	}
336cdf0e10cSrcweir 	default:
337cdf0e10cSrcweir 	{
338cdf0e10cSrcweir 		throw RuntimeException( rtl::OUString::createFromAscii("no member description found!"), (XInterface *)pThis );
339cdf0e10cSrcweir 	}
340cdf0e10cSrcweir 	}
341cdf0e10cSrcweir }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir //==================================================================================================
isSimpleStruct(typelib_TypeDescriptionReference * type)344cdf0e10cSrcweir bool isSimpleStruct(typelib_TypeDescriptionReference * type) {
345cdf0e10cSrcweir     typelib_TypeDescription * td = 0;
346cdf0e10cSrcweir     TYPELIB_DANGER_GET(&td, type);
347cdf0e10cSrcweir     OSL_ASSERT(td != 0);
348cdf0e10cSrcweir     for (typelib_CompoundTypeDescription * ctd
349cdf0e10cSrcweir              = reinterpret_cast< typelib_CompoundTypeDescription * >(td);
350cdf0e10cSrcweir          ctd != 0; ctd = ctd->pBaseTypeDescription)
351cdf0e10cSrcweir     {
352cdf0e10cSrcweir         OSL_ASSERT(ctd->aBase.eTypeClass == typelib_TypeClass_STRUCT);
353cdf0e10cSrcweir         for (sal_Int32 i = 0; i < ctd->nMembers; ++i) {
354cdf0e10cSrcweir             typelib_TypeClass c = ctd->ppTypeRefs[i]->eTypeClass;
355cdf0e10cSrcweir             switch (c) {
356cdf0e10cSrcweir             case typelib_TypeClass_STRING:
357cdf0e10cSrcweir             case typelib_TypeClass_TYPE:
358cdf0e10cSrcweir             case typelib_TypeClass_ANY:
359cdf0e10cSrcweir             case typelib_TypeClass_SEQUENCE:
360cdf0e10cSrcweir             case typelib_TypeClass_INTERFACE:
361cdf0e10cSrcweir                 return false;
362cdf0e10cSrcweir             case typelib_TypeClass_STRUCT:
363cdf0e10cSrcweir                 if (!isSimpleStruct(ctd->ppTypeRefs[i])) {
364cdf0e10cSrcweir                     return false;
365cdf0e10cSrcweir                 }
366cdf0e10cSrcweir                 break;
367cdf0e10cSrcweir             default:
368cdf0e10cSrcweir                 OSL_ASSERT(
369cdf0e10cSrcweir                     c <= typelib_TypeClass_DOUBLE
370cdf0e10cSrcweir                     || c == typelib_TypeClass_ENUM);
371cdf0e10cSrcweir                 break;
372cdf0e10cSrcweir             }
373cdf0e10cSrcweir         }
374cdf0e10cSrcweir     }
375cdf0e10cSrcweir     TYPELIB_DANGER_RELEASE(td);
376cdf0e10cSrcweir     return true;
377cdf0e10cSrcweir }
378cdf0e10cSrcweir 
379cdf0e10cSrcweir extern "C" void privateSnippetExecutorGeneral();
380cdf0e10cSrcweir extern "C" void privateSnippetExecutorVoid();
381cdf0e10cSrcweir extern "C" void privateSnippetExecutorHyper();
382cdf0e10cSrcweir extern "C" void privateSnippetExecutorFloat();
383cdf0e10cSrcweir extern "C" void privateSnippetExecutorDouble();
384cdf0e10cSrcweir extern "C" void privateSnippetExecutorStruct();
385cdf0e10cSrcweir extern "C" typedef void (*PrivateSnippetExecutor)();
386cdf0e10cSrcweir 
387cdf0e10cSrcweir int const codeSnippetSize = 16;
388cdf0e10cSrcweir 
codeSnippet(unsigned char * code,sal_Int32 functionIndex,sal_Int32 vtableOffset,typelib_TypeDescriptionReference * returnType)389cdf0e10cSrcweir unsigned char * codeSnippet(
390cdf0e10cSrcweir     unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
391cdf0e10cSrcweir     typelib_TypeDescriptionReference * returnType)
392cdf0e10cSrcweir {
393cdf0e10cSrcweir     typelib_TypeClass c = returnType == 0
394cdf0e10cSrcweir         ? typelib_TypeClass_VOID : returnType->eTypeClass;
395cdf0e10cSrcweir     if (returnType != 0 && !bridges::cpp_uno::shared::isSimpleType(c)) {
396cdf0e10cSrcweir         functionIndex |= 0x80000000;
397cdf0e10cSrcweir     }
398cdf0e10cSrcweir     PrivateSnippetExecutor exec;
399cdf0e10cSrcweir     switch (c) {
400cdf0e10cSrcweir     case typelib_TypeClass_VOID:
401cdf0e10cSrcweir         exec = privateSnippetExecutorVoid;
402cdf0e10cSrcweir         break;
403cdf0e10cSrcweir     case typelib_TypeClass_HYPER:
404cdf0e10cSrcweir     case typelib_TypeClass_UNSIGNED_HYPER:
405cdf0e10cSrcweir         exec = privateSnippetExecutorHyper;
406cdf0e10cSrcweir         break;
407cdf0e10cSrcweir     case typelib_TypeClass_FLOAT:
408cdf0e10cSrcweir         exec = privateSnippetExecutorFloat;
409cdf0e10cSrcweir         break;
410cdf0e10cSrcweir     case typelib_TypeClass_DOUBLE:
411cdf0e10cSrcweir         exec = privateSnippetExecutorDouble;
412cdf0e10cSrcweir         break;
413cdf0e10cSrcweir     case typelib_TypeClass_STRUCT:
414cdf0e10cSrcweir         OSL_ASSERT(returnType != 0);
415cdf0e10cSrcweir         // For "simple" (more-or-less POD, but not exactly) structs, the caller
416cdf0e10cSrcweir         // pops the pointer to the return value off the stack, as documented in
417cdf0e10cSrcweir         // the Intel SYSV ABI; for other structs (which includes STRING, TYPE,
418cdf0e10cSrcweir         // ANY, sequences, and interfaces, btw.), the callee pops the pointer to
419cdf0e10cSrcweir         // the return value off the stack:
420cdf0e10cSrcweir         exec = isSimpleStruct(returnType)
421cdf0e10cSrcweir             ? privateSnippetExecutorStruct : privateSnippetExecutorGeneral;
422cdf0e10cSrcweir         break;
423cdf0e10cSrcweir     default:
424cdf0e10cSrcweir         exec = privateSnippetExecutorGeneral;
425cdf0e10cSrcweir         break;
426cdf0e10cSrcweir     }
427cdf0e10cSrcweir     unsigned char * p = code;
428cdf0e10cSrcweir     OSL_ASSERT(sizeof (sal_Int32) == 4);
429cdf0e10cSrcweir     // mov function_index, %eax:
430cdf0e10cSrcweir     *p++ = 0xB8;
431cdf0e10cSrcweir     *reinterpret_cast< sal_Int32 * >(p) = functionIndex;
432cdf0e10cSrcweir     p += sizeof (sal_Int32);
433cdf0e10cSrcweir     // mov vtable_offset, %edx:
434cdf0e10cSrcweir     *p++ = 0xBA;
435cdf0e10cSrcweir     *reinterpret_cast< sal_Int32 * >(p) = vtableOffset;
436cdf0e10cSrcweir     p += sizeof (sal_Int32);
437cdf0e10cSrcweir     // jmp privateSnippetExecutor:
438cdf0e10cSrcweir     *p++ = 0xE9;
439cdf0e10cSrcweir #pragma disable_warn
440cdf0e10cSrcweir     void * e = reinterpret_cast< void * >(exec);
441cdf0e10cSrcweir #pragma enable_warn
442cdf0e10cSrcweir     *reinterpret_cast< sal_Int32 * >(p)
443cdf0e10cSrcweir         = static_cast< unsigned char * >(e) - p - sizeof (sal_Int32);
444cdf0e10cSrcweir     p += sizeof (sal_Int32);
445cdf0e10cSrcweir     OSL_ASSERT(p - code <= codeSnippetSize);
446cdf0e10cSrcweir     return code + codeSnippetSize;
447cdf0e10cSrcweir }
448cdf0e10cSrcweir 
449cdf0e10cSrcweir }
450cdf0e10cSrcweir 
451cdf0e10cSrcweir struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
452cdf0e10cSrcweir 
453cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)454cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block) {
455cdf0e10cSrcweir     return static_cast< Slot * >(block) + 1;
456cdf0e10cSrcweir }
457cdf0e10cSrcweir 
getBlockSize(sal_Int32 slotCount)458cdf0e10cSrcweir sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
459cdf0e10cSrcweir     sal_Int32 slotCount)
460cdf0e10cSrcweir {
461cdf0e10cSrcweir     return (slotCount + 3) * sizeof (Slot) + slotCount * codeSnippetSize;
462cdf0e10cSrcweir }
463cdf0e10cSrcweir 
464cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)465cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::initializeBlock(
466cdf0e10cSrcweir     void * block, sal_Int32 slotCount)
467cdf0e10cSrcweir {
468cdf0e10cSrcweir     Slot * slots = mapBlockToVtable(block) + 2;
469cdf0e10cSrcweir     slots[-3].fn = 0; // RTTI
470cdf0e10cSrcweir     slots[-2].fn = 0; // null
471cdf0e10cSrcweir     slots[-1].fn = 0; // destructor
472cdf0e10cSrcweir     return slots + slotCount;
473cdf0e10cSrcweir }
474cdf0e10cSrcweir 
addLocalFunctions(Slot ** slots,unsigned char * code,typelib_InterfaceTypeDescription const * type,sal_Int32 functionOffset,sal_Int32 functionCount,sal_Int32 vtableOffset)475cdf0e10cSrcweir unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
476cdf0e10cSrcweir     Slot ** slots, unsigned char * code,
477cdf0e10cSrcweir     typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
478cdf0e10cSrcweir     sal_Int32 functionCount, sal_Int32 vtableOffset)
479cdf0e10cSrcweir {
480cdf0e10cSrcweir     (*slots) -= functionCount;
481cdf0e10cSrcweir     Slot * s = *slots;
482cdf0e10cSrcweir     for (sal_Int32 i = 0; i < type->nMembers; ++i) {
483cdf0e10cSrcweir         typelib_TypeDescription * member = 0;
484cdf0e10cSrcweir         TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
485cdf0e10cSrcweir         OSL_ASSERT(member != 0);
486cdf0e10cSrcweir         switch (member->eTypeClass) {
487cdf0e10cSrcweir         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
488cdf0e10cSrcweir             // Getter:
489cdf0e10cSrcweir             (s++)->fn = code;
490cdf0e10cSrcweir             code = codeSnippet(
491cdf0e10cSrcweir                 code, functionOffset++, vtableOffset,
492cdf0e10cSrcweir                 reinterpret_cast< typelib_InterfaceAttributeTypeDescription * >(
493cdf0e10cSrcweir                     member)->pAttributeTypeRef);
494cdf0e10cSrcweir             // Setter:
495cdf0e10cSrcweir             if (!reinterpret_cast<
496cdf0e10cSrcweir                 typelib_InterfaceAttributeTypeDescription * >(
497cdf0e10cSrcweir                     member)->bReadOnly)
498cdf0e10cSrcweir             {
499cdf0e10cSrcweir                 (s++)->fn = code;
500cdf0e10cSrcweir                 code = codeSnippet(code, functionOffset++, vtableOffset, 0);
501cdf0e10cSrcweir             }
502cdf0e10cSrcweir             break;
503cdf0e10cSrcweir 
504cdf0e10cSrcweir         case typelib_TypeClass_INTERFACE_METHOD:
505cdf0e10cSrcweir             (s++)->fn = code;
506cdf0e10cSrcweir             code = codeSnippet(
507cdf0e10cSrcweir                 code, functionOffset++, vtableOffset,
508cdf0e10cSrcweir                 reinterpret_cast< typelib_InterfaceMethodTypeDescription * >(
509cdf0e10cSrcweir                     member)->pReturnTypeRef);
510cdf0e10cSrcweir             break;
511cdf0e10cSrcweir 
512cdf0e10cSrcweir         default:
513cdf0e10cSrcweir             OSL_ASSERT(false);
514cdf0e10cSrcweir             break;
515cdf0e10cSrcweir         }
516cdf0e10cSrcweir         TYPELIB_DANGER_RELEASE(member);
517cdf0e10cSrcweir     }
518cdf0e10cSrcweir     return code;
519cdf0e10cSrcweir }
520cdf0e10cSrcweir 
flushCode(unsigned char const *,unsigned char const *)521cdf0e10cSrcweir void bridges::cpp_uno::shared::VtableFactory::flushCode(
522cdf0e10cSrcweir     unsigned char const *, unsigned char const *)
523cdf0e10cSrcweir {}
524