1*61dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*61dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*61dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*61dff127SAndrew Rist  * distributed with this work for additional information
6*61dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*61dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*61dff127SAndrew Rist  * "License"); you may not use this file except in compliance
9*61dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*61dff127SAndrew Rist  *
11*61dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*61dff127SAndrew Rist  *
13*61dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*61dff127SAndrew Rist  * software distributed under the License is distributed on an
15*61dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*61dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
17*61dff127SAndrew Rist  * specific language governing permissions and limitations
18*61dff127SAndrew Rist  * under the License.
19*61dff127SAndrew Rist  *
20*61dff127SAndrew Rist  *************************************************************/
21*61dff127SAndrew Rist 
22*61dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir #include <com/sun/star/uno/genfunc.hxx>
27cdf0e10cSrcweir #include <typelib/typedescription.hxx>
28cdf0e10cSrcweir #include <uno/data.h>
29cdf0e10cSrcweir #include "bridges/cpp_uno/shared/bridge.hxx"
30cdf0e10cSrcweir #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
31cdf0e10cSrcweir #include "bridges/cpp_uno/shared/types.hxx"
32cdf0e10cSrcweir #include "bridges/cpp_uno/shared/vtablefactory.hxx"
33cdf0e10cSrcweir #include "share.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace com::sun::star::uno;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace
38cdf0e10cSrcweir {
39cdf0e10cSrcweir //==================================================================================================
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)40cdf0e10cSrcweir static typelib_TypeClass cpp2uno_call(
41cdf0e10cSrcweir 	 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
42cdf0e10cSrcweir 	const typelib_TypeDescription * pMemberTypeDescr,
43cdf0e10cSrcweir 	typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
44cdf0e10cSrcweir 	sal_Int32 nParams, typelib_MethodParameter * pParams,
45cdf0e10cSrcweir 	void ** pCallStack,
46cdf0e10cSrcweir 	sal_Int64 * pRegisterReturn /* space for register return */ )
47cdf0e10cSrcweir {
48cdf0e10cSrcweir 	// pCallStack: [ret ptr], this, params
49cdf0e10cSrcweir 	char * pCppStack = (char *)pCallStack;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir 	// return
52cdf0e10cSrcweir 	typelib_TypeDescription * pReturnTypeDescr = 0;
53cdf0e10cSrcweir 	if (pReturnTypeRef)
54cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 	void * pUnoReturn = 0;
57cdf0e10cSrcweir 	void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
58cdf0e10cSrcweir 
59cdf0e10cSrcweir 	if (pReturnTypeDescr)
60cdf0e10cSrcweir 	{
61cdf0e10cSrcweir 		if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
62cdf0e10cSrcweir 			pUnoReturn = pRegisterReturn; // direct way for simple types
63cdf0e10cSrcweir 		else // complex return via ptr (pCppReturn)
64cdf0e10cSrcweir 		{
65cdf0e10cSrcweir 			pCppReturn = *(void**)pCppStack;
66cdf0e10cSrcweir 			pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
67cdf0e10cSrcweir   	                             pReturnTypeDescr )
68cdf0e10cSrcweir 						  ? alloca( pReturnTypeDescr->nSize )
69cdf0e10cSrcweir 						  : pCppReturn); // direct way
70cdf0e10cSrcweir 			pCppStack += sizeof( void* );
71cdf0e10cSrcweir 		}
72cdf0e10cSrcweir 	}
73cdf0e10cSrcweir 	// pop this
74cdf0e10cSrcweir 	pCppStack += sizeof( void* );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 	// stack space
77cdf0e10cSrcweir 	OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
78cdf0e10cSrcweir 	// parameters
79cdf0e10cSrcweir 	void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
80cdf0e10cSrcweir 	void ** pCppArgs = pUnoArgs + nParams;
81cdf0e10cSrcweir 	// indizes of values this have to be converted (interface conversion cpp<=>uno)
82cdf0e10cSrcweir 	sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
83cdf0e10cSrcweir 	// type descriptions for reconversions
84cdf0e10cSrcweir 	typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	sal_Int32 nTempIndizes   = 0;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 	for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
89cdf0e10cSrcweir 	{
90cdf0e10cSrcweir 		const typelib_MethodParameter & rParam = pParams[nPos];
91cdf0e10cSrcweir 		typelib_TypeDescription * pParamTypeDescr = 0;
92cdf0e10cSrcweir 		TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	 	if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))  // value
95cdf0e10cSrcweir 		{
96cdf0e10cSrcweir 			pCppArgs[nPos] = pUnoArgs[nPos] = CPPU_CURRENT_NAMESPACE::adjustPointer(pCppStack, pParamTypeDescr);
97cdf0e10cSrcweir 			switch (pParamTypeDescr->eTypeClass)
98cdf0e10cSrcweir 			{
99cdf0e10cSrcweir 			case typelib_TypeClass_HYPER:
100cdf0e10cSrcweir 			case typelib_TypeClass_UNSIGNED_HYPER:
101cdf0e10cSrcweir             		case typelib_TypeClass_DOUBLE:
102cdf0e10cSrcweir             		{
103cdf0e10cSrcweir 			if ((reinterpret_cast< long >(pCppStack) & 7) != 0)
104cdf0e10cSrcweir               		{
105cdf0e10cSrcweir 		   		OSL_ASSERT( sizeof (double) == sizeof (sal_Int64) );
106cdf0e10cSrcweir                    		void * pDest = alloca( sizeof (sal_Int64) );
107cdf0e10cSrcweir                   	 	*reinterpret_cast< sal_Int32 * >(pDest) =
108cdf0e10cSrcweir                    		*reinterpret_cast< sal_Int32 const * >(pCppStack);
109cdf0e10cSrcweir                    		*(reinterpret_cast< sal_Int32 * >(pDest) + 1) =
110cdf0e10cSrcweir                    		*(reinterpret_cast< sal_Int32 const * >(pCppStack) + 1);
111cdf0e10cSrcweir                    		pCppArgs[nPos] = pUnoArgs[nPos] = pDest;
112cdf0e10cSrcweir 			}
113cdf0e10cSrcweir 		   	pCppStack += sizeof (sal_Int32); // extra long
114cdf0e10cSrcweir                    	break;
115cdf0e10cSrcweir 			default:
116cdf0e10cSrcweir 			break;
117cdf0e10cSrcweir 			}
118cdf0e10cSrcweir 			}
119cdf0e10cSrcweir 			// no longer needed
120cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
121cdf0e10cSrcweir 		}
122cdf0e10cSrcweir 		else // ptr to complex value | ref
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			pCppArgs[nPos] = *(void **)pCppStack;
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 			if (! rParam.bIn) // is pure out
127cdf0e10cSrcweir 			{
128cdf0e10cSrcweir 				// uno out is unconstructed mem!
129cdf0e10cSrcweir 				pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
130cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos;
131cdf0e10cSrcweir 				// will be released at reconversion
132cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
133cdf0e10cSrcweir 			}
134cdf0e10cSrcweir 			// is in/inout
135cdf0e10cSrcweir 			else if (bridges::cpp_uno::shared::relatesToInterfaceType(
136cdf0e10cSrcweir   	                        pParamTypeDescr ))
137cdf0e10cSrcweir 			{
138cdf0e10cSrcweir 				uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
139cdf0e10cSrcweir 										*(void **)pCppStack, pParamTypeDescr,
140cdf0e10cSrcweir 										  pThis->getBridge()->getCpp2Uno() );
141cdf0e10cSrcweir 				pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
142cdf0e10cSrcweir 				// will be released at reconversion
143cdf0e10cSrcweir 				ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
144cdf0e10cSrcweir 			}
145cdf0e10cSrcweir 			else // direct way
146cdf0e10cSrcweir 			{
147cdf0e10cSrcweir 				pUnoArgs[nPos] = *(void **)pCppStack;
148cdf0e10cSrcweir 				// no longer needed
149cdf0e10cSrcweir 				TYPELIB_DANGER_RELEASE( pParamTypeDescr );
150cdf0e10cSrcweir 			}
151cdf0e10cSrcweir 		}
152cdf0e10cSrcweir 		pCppStack += sizeof(sal_Int32); // standard parameter length
153cdf0e10cSrcweir 	}
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 	// ExceptionHolder
156cdf0e10cSrcweir 	uno_Any aUnoExc; // Any will be constructed by callee
157cdf0e10cSrcweir 	uno_Any * pUnoExc = &aUnoExc;
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 	// invoke uno dispatch call
160cdf0e10cSrcweir 	(*pThis->getUnoI()->pDispatcher)(pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	// in case an exception occured...
163cdf0e10cSrcweir 	if (pUnoExc)
164cdf0e10cSrcweir 	{
165cdf0e10cSrcweir 		// destruct temporary in/inout params
166cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
167cdf0e10cSrcweir 		{
168cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 			if (pParams[nIndex].bIn) // is in/inout => was constructed
171cdf0e10cSrcweir 				uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
172cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
173cdf0e10cSrcweir 		}
174cdf0e10cSrcweir 		if (pReturnTypeDescr)
175cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
176cdf0e10cSrcweir 		CPPU_CURRENT_NAMESPACE::raiseException(&aUnoExc, pThis->getBridge()->getUno2Cpp() );
177cdf0e10cSrcweir   	           // has to destruct the any
178cdf0e10cSrcweir 		// is here for dummy
179cdf0e10cSrcweir 		return typelib_TypeClass_VOID;
180cdf0e10cSrcweir 	}
181cdf0e10cSrcweir 	else // else no exception occured...
182cdf0e10cSrcweir 	{
183cdf0e10cSrcweir 		// temporary params
184cdf0e10cSrcweir 		for ( ; nTempIndizes--; )
185cdf0e10cSrcweir 		{
186cdf0e10cSrcweir 			sal_Int32 nIndex = pTempIndizes[nTempIndizes];
187cdf0e10cSrcweir 			typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 			if (pParams[nIndex].bOut) // inout/out
190cdf0e10cSrcweir 			{
191cdf0e10cSrcweir 				// convert and assign
192cdf0e10cSrcweir 				uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
193cdf0e10cSrcweir 				uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
194cdf0e10cSrcweir 									pThis->getBridge()->getUno2Cpp() );
195cdf0e10cSrcweir 			}
196cdf0e10cSrcweir 			// destroy temp uno param
197cdf0e10cSrcweir 			uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
198cdf0e10cSrcweir 
199cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pParamTypeDescr );
200cdf0e10cSrcweir 		}
201cdf0e10cSrcweir 		// return
202cdf0e10cSrcweir 		if (pCppReturn) // has complex return
203cdf0e10cSrcweir 		{
204cdf0e10cSrcweir 			if (pUnoReturn != pCppReturn) // needs reconversion
205cdf0e10cSrcweir 			{
206cdf0e10cSrcweir 				uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
207cdf0e10cSrcweir 										pThis->getBridge()->getUno2Cpp() );
208cdf0e10cSrcweir 				// destroy temp uno return
209cdf0e10cSrcweir 				uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
210cdf0e10cSrcweir 			}
211cdf0e10cSrcweir 			// complex return ptr is set to eax
212cdf0e10cSrcweir 			*(void **)pRegisterReturn = pCppReturn;
213cdf0e10cSrcweir 		}
214cdf0e10cSrcweir 		if (pReturnTypeDescr)
215cdf0e10cSrcweir 		{
216cdf0e10cSrcweir 			typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
217cdf0e10cSrcweir 			TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
218cdf0e10cSrcweir 			return eRet;
219cdf0e10cSrcweir 		}
220cdf0e10cSrcweir 		else
221cdf0e10cSrcweir 			return typelib_TypeClass_VOID;
222cdf0e10cSrcweir 	}
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 
226cdf0e10cSrcweir //==================================================================================================
cpp_mediate(sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,void ** pCallStack,sal_Int64 * pRegisterReturn)227cdf0e10cSrcweir static typelib_TypeClass cpp_mediate(
228cdf0e10cSrcweir 	sal_Int32	nFunctionIndex,
229cdf0e10cSrcweir 	sal_Int32	nVtableOffset,
230cdf0e10cSrcweir 	void **	pCallStack,
231cdf0e10cSrcweir 	sal_Int64 * pRegisterReturn /* space for register return */ )
232cdf0e10cSrcweir {
233cdf0e10cSrcweir 	OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
234cdf0e10cSrcweir 
235cdf0e10cSrcweir 	// pCallStack: this, params
236cdf0e10cSrcweir 	// eventual [ret*] lies at pCallStack -1
237cdf0e10cSrcweir 	// so count down pCallStack by one to keep it simple
238cdf0e10cSrcweir 	// pCallStack: this, params
239cdf0e10cSrcweir 	// eventual [ret*] lies at pCallStack -1
240cdf0e10cSrcweir 	// so count down pCallStack by one to keep it simple
241cdf0e10cSrcweir 	bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
242cdf0e10cSrcweir 		= bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
243cdf0e10cSrcweir 	static_cast< char * >(*pCallStack) - nVtableOffset);
244cdf0e10cSrcweir 	if ((nFunctionIndex & 0x80000000) != 0) {
245cdf0e10cSrcweir 		nFunctionIndex &= 0x7FFFFFFF;
246cdf0e10cSrcweir 		--pCallStack;
247cdf0e10cSrcweir 	}
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 	OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex,
252cdf0e10cSrcweir 				 "### illegal vtable index!" );
253cdf0e10cSrcweir 	if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
254cdf0e10cSrcweir 	{
255cdf0e10cSrcweir 		throw RuntimeException( rtl::OUString::createFromAscii("illegal vtable index!"), (XInterface *)pCppI );
256cdf0e10cSrcweir 	}
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	// determine called method
259cdf0e10cSrcweir 	sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
260cdf0e10cSrcweir 	OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
263cdf0e10cSrcweir 
264cdf0e10cSrcweir #if defined BRIDGES_DEBUG
265cdf0e10cSrcweir 	OString cstr( OUStringToOString( aMemberDescr.get()->pTypeName, RTL_TEXTENCODING_ASCII_US ) );
266cdf0e10cSrcweir 	fprintf( stderr, "calling %s, nFunctionIndex=%d\n", cstr.getStr(), nFunctionIndex );
267cdf0e10cSrcweir #endif
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	typelib_TypeClass eRet;
270cdf0e10cSrcweir 	switch (aMemberDescr.get()->eTypeClass)
271cdf0e10cSrcweir 	{
272cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_ATTRIBUTE:
273cdf0e10cSrcweir 	{
274cdf0e10cSrcweir 		if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
275cdf0e10cSrcweir 		{
276cdf0e10cSrcweir 			// is GET method
277cdf0e10cSrcweir 			eRet = cpp2uno_call(
278cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
279cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
280cdf0e10cSrcweir 				0, 0, // no params
281cdf0e10cSrcweir 				pCallStack, pRegisterReturn );
282cdf0e10cSrcweir 		}
283cdf0e10cSrcweir 		else
284cdf0e10cSrcweir 		{
285cdf0e10cSrcweir 			// is SET method
286cdf0e10cSrcweir 			typelib_MethodParameter aParam;
287cdf0e10cSrcweir 			aParam.pTypeRef =
288cdf0e10cSrcweir 				((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
289cdf0e10cSrcweir 			aParam.bIn		= sal_True;
290cdf0e10cSrcweir 			aParam.bOut		= sal_False;
291cdf0e10cSrcweir 
292cdf0e10cSrcweir 			eRet = cpp2uno_call(
293cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
294cdf0e10cSrcweir 				0, // indicates void return
295cdf0e10cSrcweir 				1, &aParam,
296cdf0e10cSrcweir 				pCallStack, pRegisterReturn );
297cdf0e10cSrcweir 		}
298cdf0e10cSrcweir 		break;
299cdf0e10cSrcweir 	}
300cdf0e10cSrcweir 	case typelib_TypeClass_INTERFACE_METHOD:
301cdf0e10cSrcweir 	{
302cdf0e10cSrcweir 		// is METHOD
303cdf0e10cSrcweir 		switch (nFunctionIndex)
304cdf0e10cSrcweir 		{
305cdf0e10cSrcweir 		case 1: // acquire()
306cdf0e10cSrcweir 			pCppI->acquireProxy(); // non virtual call!
307cdf0e10cSrcweir 			eRet = typelib_TypeClass_VOID;
308cdf0e10cSrcweir 			break;
309cdf0e10cSrcweir 		case 2: // release()
310cdf0e10cSrcweir 			pCppI->releaseProxy(); // non virtual call!
311cdf0e10cSrcweir 			eRet = typelib_TypeClass_VOID;
312cdf0e10cSrcweir 			break;
313cdf0e10cSrcweir 		case 0: // queryInterface() opt
314cdf0e10cSrcweir 		{
315cdf0e10cSrcweir 			typelib_TypeDescription * pTD = 0;
316cdf0e10cSrcweir 			TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[2] )->getTypeLibType() );
317cdf0e10cSrcweir 			if (pTD)
318cdf0e10cSrcweir 			{
319cdf0e10cSrcweir                 XInterface * pInterface = 0;
320cdf0e10cSrcweir 		(*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
321cdf0e10cSrcweir 		    pCppI->getBridge()->getCppEnv(),
322cdf0e10cSrcweir 		    (void **)&pInterface, pCppI->getOid().pData, (typelib_InterfaceTypeDescription *)pTD );
323cdf0e10cSrcweir 
324cdf0e10cSrcweir                 if (pInterface)
325cdf0e10cSrcweir                 {
326cdf0e10cSrcweir                     ::uno_any_construct(
327cdf0e10cSrcweir                         reinterpret_cast< uno_Any * >( pCallStack[0] ),
328cdf0e10cSrcweir                         &pInterface, pTD, cpp_acquire );
329cdf0e10cSrcweir                     pInterface->release();
330cdf0e10cSrcweir                     TYPELIB_DANGER_RELEASE( pTD );
331cdf0e10cSrcweir                     *(void **)pRegisterReturn = pCallStack[0];
332cdf0e10cSrcweir                     eRet = typelib_TypeClass_ANY;
333cdf0e10cSrcweir                     break;
334cdf0e10cSrcweir                 }
335cdf0e10cSrcweir                 TYPELIB_DANGER_RELEASE( pTD );
336cdf0e10cSrcweir             }
337cdf0e10cSrcweir 		} // else perform queryInterface()
338cdf0e10cSrcweir 		default:
339cdf0e10cSrcweir 			eRet = cpp2uno_call(
340cdf0e10cSrcweir 				pCppI, aMemberDescr.get(),
341cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
342cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
343cdf0e10cSrcweir 				((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
344cdf0e10cSrcweir 				pCallStack, pRegisterReturn );
345cdf0e10cSrcweir 		}
346cdf0e10cSrcweir 		break;
347cdf0e10cSrcweir 	}
348cdf0e10cSrcweir 	default:
349cdf0e10cSrcweir 	{
350cdf0e10cSrcweir 		throw RuntimeException(rtl::OUString::createFromAscii("no member description found!"), (XInterface *)pCppI );
351cdf0e10cSrcweir 		// is here for dummy
352cdf0e10cSrcweir 		eRet = typelib_TypeClass_VOID;
353cdf0e10cSrcweir 	}
354cdf0e10cSrcweir 	}
355cdf0e10cSrcweir 	return eRet;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 
359cdf0e10cSrcweir 
360cdf0e10cSrcweir //==================================================================================================
361cdf0e10cSrcweir /**
362cdf0e10cSrcweir  * is called on incoming vtable calls
363cdf0e10cSrcweir  * (called by asm snippets)
364cdf0e10cSrcweir  */
cpp_vtable_call()365cdf0e10cSrcweir static void cpp_vtable_call()
366cdf0e10cSrcweir {
367cdf0e10cSrcweir 	sal_Int64 nRegReturn;
368cdf0e10cSrcweir 	int nFunctionIndex;
369cdf0e10cSrcweir 	void** pCallStack;
370cdf0e10cSrcweir 	int vTableOffset;
371cdf0e10cSrcweir 
372cdf0e10cSrcweir void * pRegReturn = &nRegReturn;
373cdf0e10cSrcweir 
374cdf0e10cSrcweir 	__asm__( "st %%i0, %0\n\t"
375cdf0e10cSrcweir 			"st %%i1, %1\n\t"
376cdf0e10cSrcweir  			"st %%i2, %2\n\t"
377cdf0e10cSrcweir 			: : "m"(nFunctionIndex), "m"(pCallStack), "m"(vTableOffset) );
378cdf0e10cSrcweir 
379cdf0e10cSrcweir //	fprintf(stderr,"cpp_mediate nFunctionIndex=%x\n",nFunctionIndex);
380cdf0e10cSrcweir //	fflush(stderr);
381cdf0e10cSrcweir 
382cdf0e10cSrcweir 	sal_Bool bComplex = nFunctionIndex & 0x80000000 ? sal_True : sal_False;
383cdf0e10cSrcweir 	typelib_TypeClass aType =
384cdf0e10cSrcweir 		cpp_mediate( nFunctionIndex, vTableOffset, pCallStack+17, (sal_Int64*)&nRegReturn );
385cdf0e10cSrcweir 
386cdf0e10cSrcweir 	switch( aType )
387cdf0e10cSrcweir 	{
388cdf0e10cSrcweir 		case typelib_TypeClass_BOOLEAN:
389cdf0e10cSrcweir 		case typelib_TypeClass_BYTE:
390cdf0e10cSrcweir 			__asm__( "ld %0, %%l0\n\t"
391cdf0e10cSrcweir 					 "ldsb [%%l0], %%i0\n"
392cdf0e10cSrcweir 					 : : "m"(pRegReturn) );
393cdf0e10cSrcweir 			break;
394cdf0e10cSrcweir 		case typelib_TypeClass_CHAR:
395cdf0e10cSrcweir 		case typelib_TypeClass_SHORT:
396cdf0e10cSrcweir 		case typelib_TypeClass_UNSIGNED_SHORT:
397cdf0e10cSrcweir 			__asm__( "ld %0, %%l0\n\t"
398cdf0e10cSrcweir 					 "ldsh [%%l0], %%i0\n"
399cdf0e10cSrcweir 					 : : "m"(pRegReturn) );
400cdf0e10cSrcweir 			break;
401cdf0e10cSrcweir 		case typelib_TypeClass_HYPER:
402cdf0e10cSrcweir 		case typelib_TypeClass_UNSIGNED_HYPER:
403cdf0e10cSrcweir 			__asm__( "ld %0, %%l0\n\t"
404cdf0e10cSrcweir 					 "ld [%%l0], %%i0\n\t"
405cdf0e10cSrcweir 					 "add %%l0, 4, %%l0\n\t"
406cdf0e10cSrcweir 					 "ld [%%l0], %%i1\n\t"
407cdf0e10cSrcweir 					  : : "m"(pRegReturn) );
408cdf0e10cSrcweir 
409cdf0e10cSrcweir 			break;
410cdf0e10cSrcweir 		case typelib_TypeClass_FLOAT:
411cdf0e10cSrcweir 			__asm__( "ld %0, %%l0\n\t"
412cdf0e10cSrcweir 					 "ld [%%l0], %%f0\n"
413cdf0e10cSrcweir 					 : : "m"(pRegReturn) );
414cdf0e10cSrcweir 			break;
415cdf0e10cSrcweir 		case typelib_TypeClass_DOUBLE:
416cdf0e10cSrcweir 			__asm__( "ld %0, %%l0\n\t"
417cdf0e10cSrcweir 					 "ldd [%%l0], %%f0\n"
418cdf0e10cSrcweir 					 : : "m"(pRegReturn) );
419cdf0e10cSrcweir 			break;
420cdf0e10cSrcweir 		case typelib_TypeClass_VOID:
421cdf0e10cSrcweir 			break;
422cdf0e10cSrcweir 		default:
423cdf0e10cSrcweir 			__asm__( "ld %0, %%l0\n\t"
424cdf0e10cSrcweir 					 "ld [%%l0], %%i0\n"
425cdf0e10cSrcweir 					 : : "m"(pRegReturn) );
426cdf0e10cSrcweir 			break;
427cdf0e10cSrcweir 	}
428cdf0e10cSrcweir 
429cdf0e10cSrcweir 	if( bComplex )
430cdf0e10cSrcweir 	{
431cdf0e10cSrcweir 		__asm__( "add %i7, 4, %i7\n\t" );
432cdf0e10cSrcweir 		// after call to complex return valued funcion there is an unimp instruction
433cdf0e10cSrcweir 	}
434cdf0e10cSrcweir 
435cdf0e10cSrcweir }
436cdf0e10cSrcweir //__________________________________________________________________________________________________
437cdf0e10cSrcweir 
438cdf0e10cSrcweir int const codeSnippetSize = 56;
codeSnippet(unsigned char * code,sal_Int32 functionIndex,sal_Int32 vtableOffset,bool simpleRetType)439cdf0e10cSrcweir unsigned char * codeSnippet(
440cdf0e10cSrcweir     unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
441cdf0e10cSrcweir     bool simpleRetType)
442cdf0e10cSrcweir {
443cdf0e10cSrcweir     sal_uInt32 index = functionIndex;
444cdf0e10cSrcweir     if (!simpleRetType) {
445cdf0e10cSrcweir         index |= 0x80000000;
446cdf0e10cSrcweir     }
447cdf0e10cSrcweir     unsigned int * p = reinterpret_cast< unsigned int * >(code);
448cdf0e10cSrcweir     OSL_ASSERT(sizeof (unsigned int) == 4);
449cdf0e10cSrcweir     // st %o0, [%sp+68]:
450cdf0e10cSrcweir     *p++ = 0xD023A044;
451cdf0e10cSrcweir     // st %o1, [%sp+72]:
452cdf0e10cSrcweir     *p++ = 0xD223A048;
453cdf0e10cSrcweir     // st %o2, [%sp+76]:
454cdf0e10cSrcweir     *p++ = 0xD423A04C;
455cdf0e10cSrcweir     // st %o3, [%sp+80]:
456cdf0e10cSrcweir     *p++ = 0xD623A050;
457cdf0e10cSrcweir     // st %o4, [%sp+84]:
458cdf0e10cSrcweir     *p++ = 0xD823A054;
459cdf0e10cSrcweir     // st %o5, [%sp+88]:
460cdf0e10cSrcweir     *p++ = 0xDA23A058;
461cdf0e10cSrcweir     // sethi %hi(index), %o0:
462cdf0e10cSrcweir     *p++ = 0x11000000 | (index >> 10);
463cdf0e10cSrcweir     // or %o0, %lo(index), %o0:
464cdf0e10cSrcweir     *p++ = 0x90122000 | (index & 0x3FF);
465cdf0e10cSrcweir     // sethi %hi(vtableOffset), %o2:
466cdf0e10cSrcweir     *p++ = 0x15000000 | (vtableOffset >> 10);
467cdf0e10cSrcweir     // or %o2, %lo(vtableOffset), %o2:
468cdf0e10cSrcweir     *p++ = 0x9412A000 | (vtableOffset & 0x3FF);
469cdf0e10cSrcweir     // sethi %hi(cpp_vtable_call), %o3:
470cdf0e10cSrcweir     *p++ = 0x17000000 | (reinterpret_cast< unsigned int >(cpp_vtable_call) >> 10);
471cdf0e10cSrcweir     // or %o3, %lo(cpp_vtable_call), %o3:
472cdf0e10cSrcweir     *p++ = 0x9612E000 | (reinterpret_cast< unsigned int >(cpp_vtable_call) & 0x3FF);
473cdf0e10cSrcweir     // jmpl %o3, %g0:
474cdf0e10cSrcweir     *p++ = 0x81C2C000;
475cdf0e10cSrcweir     // mov %sp, %o1:
476cdf0e10cSrcweir     *p++ = 0x9210000E;
477cdf0e10cSrcweir     OSL_ASSERT(
478cdf0e10cSrcweir         reinterpret_cast< unsigned char * >(p) - code <= codeSnippetSize);
479cdf0e10cSrcweir     return code + codeSnippetSize;
480cdf0e10cSrcweir }
481cdf0e10cSrcweir 
482cdf0e10cSrcweir } //end of namespace
483cdf0e10cSrcweir 
484cdf0e10cSrcweir struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
485cdf0e10cSrcweir 
486cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)487cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
488cdf0e10cSrcweir {
489cdf0e10cSrcweir     return static_cast< Slot * >(block) + 2;
490cdf0e10cSrcweir }
491cdf0e10cSrcweir 
getBlockSize(sal_Int32 slotCount)492cdf0e10cSrcweir sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
493cdf0e10cSrcweir     sal_Int32 slotCount)
494cdf0e10cSrcweir {
495cdf0e10cSrcweir     return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
496cdf0e10cSrcweir }
497cdf0e10cSrcweir 
498cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)499cdf0e10cSrcweir bridges::cpp_uno::shared::VtableFactory::initializeBlock(
500cdf0e10cSrcweir     void * block, sal_Int32 slotCount)
501cdf0e10cSrcweir {
502cdf0e10cSrcweir     Slot * slots = mapBlockToVtable(block);
503cdf0e10cSrcweir     slots[-2].fn = 0; //null
504cdf0e10cSrcweir     slots[-1].fn = 0; //destructor
505cdf0e10cSrcweir     return slots + slotCount;
506cdf0e10cSrcweir }
507cdf0e10cSrcweir 
addLocalFunctions(Slot ** slots,unsigned char * code,sal_PtrDiff writetoexecdiff,typelib_InterfaceTypeDescription const * type,sal_Int32 functionOffset,sal_Int32 functionCount,sal_Int32 vTableOffset)508cdf0e10cSrcweir unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
509cdf0e10cSrcweir     Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
510cdf0e10cSrcweir     typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
511cdf0e10cSrcweir     sal_Int32 functionCount, sal_Int32 vTableOffset)
512cdf0e10cSrcweir {
513cdf0e10cSrcweir 	(*slots) -= functionCount;
514cdf0e10cSrcweir 	Slot * s = *slots;
515cdf0e10cSrcweir 	for (sal_Int32 i = 0; i < type->nMembers; ++i) {
516cdf0e10cSrcweir         typelib_TypeDescription * member = 0;
517cdf0e10cSrcweir         TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
518cdf0e10cSrcweir         OSL_ASSERT(member != 0);
519cdf0e10cSrcweir         switch (member->eTypeClass) {
520cdf0e10cSrcweir         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
521cdf0e10cSrcweir             // Getter:
522cdf0e10cSrcweir             (s++)->fn = code + writetoexecdiff;
523cdf0e10cSrcweir             code = codeSnippet(
524cdf0e10cSrcweir                 code, functionOffset++, vTableOffset,
525cdf0e10cSrcweir                 bridges::cpp_uno::shared::isSimpleType(
526cdf0e10cSrcweir                     reinterpret_cast<
527cdf0e10cSrcweir 		    typelib_InterfaceAttributeTypeDescription * >(
528cdf0e10cSrcweir 		    member)->pAttributeTypeRef));
529cdf0e10cSrcweir             // Setter:
530cdf0e10cSrcweir             if (!reinterpret_cast<
531cdf0e10cSrcweir                 typelib_InterfaceAttributeTypeDescription * >(
532cdf0e10cSrcweir                     member)->bReadOnly)
533cdf0e10cSrcweir             {
534cdf0e10cSrcweir                 (s++)->fn = code + writetoexecdiff;
535cdf0e10cSrcweir                 code = codeSnippet(code, functionOffset++, vTableOffset, true);
536cdf0e10cSrcweir             }
537cdf0e10cSrcweir             break;
538cdf0e10cSrcweir 
539cdf0e10cSrcweir         case typelib_TypeClass_INTERFACE_METHOD:
540cdf0e10cSrcweir             (s++)->fn = code + writetoexecdiff;
541cdf0e10cSrcweir             code = codeSnippet(
542cdf0e10cSrcweir                 code, functionOffset++, vTableOffset,
543cdf0e10cSrcweir                 bridges::cpp_uno::shared::isSimpleType(
544cdf0e10cSrcweir                     reinterpret_cast<
545cdf0e10cSrcweir                     typelib_InterfaceMethodTypeDescription * >(
546cdf0e10cSrcweir                         member)->pReturnTypeRef));
547cdf0e10cSrcweir             break;
548cdf0e10cSrcweir 
549cdf0e10cSrcweir         default:
550cdf0e10cSrcweir             OSL_ASSERT(false);
551cdf0e10cSrcweir             break;
552cdf0e10cSrcweir         }
553cdf0e10cSrcweir         TYPELIB_DANGER_RELEASE(member);
554cdf0e10cSrcweir     }
555cdf0e10cSrcweir     return code;
556cdf0e10cSrcweir }
557cdf0e10cSrcweir 
558cdf0e10cSrcweir // use flush code from cc50_solaris_sparc
559cdf0e10cSrcweir 
560cdf0e10cSrcweir extern "C" void doFlushCode(unsigned long address, unsigned long count);
561cdf0e10cSrcweir 
flushCode(unsigned char const * begin,unsigned char const * end)562cdf0e10cSrcweir void bridges::cpp_uno::shared::VtableFactory::flushCode(
563cdf0e10cSrcweir     unsigned char const * begin, unsigned char const * end)
564cdf0e10cSrcweir {
565cdf0e10cSrcweir     unsigned long n = end - begin;
566cdf0e10cSrcweir     if (n != 0) {
567cdf0e10cSrcweir         unsigned long adr = reinterpret_cast< unsigned long >(begin);
568cdf0e10cSrcweir         unsigned long off = adr & 7;
569cdf0e10cSrcweir         doFlushCode(adr - off, (n + off + 7) >> 3);
570cdf0e10cSrcweir     }
571cdf0e10cSrcweir }
572