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