xref: /AOO41X/main/bridges/source/cpp_uno/gcc3_freebsd_intel/uno2cpp.cxx (revision b2937f997bda0a05141a2d862a64f7be893955b7)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_bridges.hxx"
26 
27 #include <stdlib.h>
28 
29 #include <com/sun/star/uno/genfunc.hxx>
30 #include "com/sun/star/uno/RuntimeException.hpp"
31 #include <uno/data.h>
32 
33 #include "bridges/cpp_uno/shared/bridge.hxx"
34 #include "bridges/cpp_uno/shared/types.hxx"
35 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
36 #include "bridges/cpp_uno/shared/vtables.hxx"
37 
38 #include "share.hxx"
39 
40 using namespace ::rtl;
41 using namespace ::com::sun::star::uno;
42 
43 namespace
44 {
45 
46 //==================================================================================================
47 // The call instruction within the asm section of callVirtualMethod may throw
48 // exceptions.  So that the compiler handles this correctly, it is important
49 // that (a) callVirtualMethod might call dummy_can_throw_anything (although this
50 // never happens at runtime), which in turn can throw exceptions, and (b)
51 // callVirtualMethod is not inlined at its call site (so that any exceptions are
52 // caught which are thrown from the instruction calling callVirtualMethod):
53 void callVirtualMethod(
54     void * pAdjustedThisPtr,
55     sal_Int32 nVtableIndex,
56     void * pRegisterReturn,
57     typelib_TypeClass eReturnType,
58     sal_Int32 * pStackLongs,
59     sal_Int32 nStackLongs ) __attribute__((noinline));
60 
61 void callVirtualMethod(
62     void * pAdjustedThisPtr,
63     sal_Int32 nVtableIndex,
64     void * pRegisterReturn,
65     typelib_TypeClass eReturnType,
66     sal_Int32 * pStackLongs,
67     sal_Int32 nStackLongs )
68 {
69     // parameter list is mixed list of * and values
70     // reference parameters are pointers
71 
72     OSL_ENSURE( pStackLongs && pAdjustedThisPtr, "### null ptr!" );
73     OSL_ENSURE( (sizeof(void *) == 4) && (sizeof(sal_Int32) == 4), "### unexpected size of int!" );
74     OSL_ENSURE( nStackLongs && pStackLongs, "### no stack in callVirtualMethod !" );
75 
76     // never called
77     if (! pAdjustedThisPtr) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
78 
79     volatile long edx = 0, eax = 0; // for register returns
80     void * stackptr;
81     asm volatile (
82         "mov   %%esp, %6\n\t"
83         // copy values
84         "mov   %0, %%eax\n\t"
85         "mov   %%eax, %%edx\n\t"
86         "dec   %%edx\n\t"
87         "shl   $2, %%edx\n\t"
88         "add   %1, %%edx\n"
89         "Lcopy:\n\t"
90         "pushl 0(%%edx)\n\t"
91         "sub   $4, %%edx\n\t"
92         "dec   %%eax\n\t"
93         "jne   Lcopy\n\t"
94         // do the actual call
95         "mov   %2, %%edx\n\t"
96         "mov   0(%%edx), %%edx\n\t"
97         "mov   %3, %%eax\n\t"
98         "shl   $2, %%eax\n\t"
99         "add   %%eax, %%edx\n\t"
100         "mov   0(%%edx), %%edx\n\t"
101         "call  *%%edx\n\t"
102         // save return registers
103         "mov   %%eax, %4\n\t"
104         "mov   %%edx, %5\n\t"
105         // cleanup stack
106         "mov   %6, %%esp\n\t"
107         :
108         : "m"(nStackLongs), "m"(pStackLongs), "m"(pAdjustedThisPtr),
109           "m"(nVtableIndex), "m"(eax), "m"(edx), "m"(stackptr)
110         : "eax", "edx" );
111     switch( eReturnType )
112     {
113         case typelib_TypeClass_HYPER:
114         case typelib_TypeClass_UNSIGNED_HYPER:
115             ((long*)pRegisterReturn)[1] = edx;
116         case typelib_TypeClass_LONG:
117         case typelib_TypeClass_UNSIGNED_LONG:
118         case typelib_TypeClass_CHAR:
119         case typelib_TypeClass_ENUM:
120             ((long*)pRegisterReturn)[0] = eax;
121             break;
122         case typelib_TypeClass_SHORT:
123         case typelib_TypeClass_UNSIGNED_SHORT:
124             *(unsigned short*)pRegisterReturn = eax;
125             break;
126         case typelib_TypeClass_BOOLEAN:
127         case typelib_TypeClass_BYTE:
128             *(unsigned char*)pRegisterReturn = eax;
129             break;
130         case typelib_TypeClass_FLOAT:
131             asm ( "fstps %0" : : "m"(*(char *)pRegisterReturn) );
132             break;
133         case typelib_TypeClass_DOUBLE:
134             asm ( "fstpl %0\n\t" : : "m"(*(char *)pRegisterReturn) );
135             break;
136         default:
137             break;
138     }
139 }
140 
141 //==================================================================================================
142 static void cpp_call(
143     bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
144     bridges::cpp_uno::shared::VtableSlot aVtableSlot,
145     typelib_TypeDescriptionReference * pReturnTypeRef,
146     sal_Int32 nParams, typelib_MethodParameter * pParams,
147     void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
148 {
149     // max space for: [complex ret ptr], values|ptr ...
150     char * pCppStack        =
151         (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
152     char * pCppStackStart   = pCppStack;
153 
154     // return
155     typelib_TypeDescription * pReturnTypeDescr = 0;
156     TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
157     OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
158 
159     void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
160 
161     if (pReturnTypeDescr)
162     {
163         if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
164         {
165             pCppReturn = pUnoReturn; // direct way for simple types
166         }
167         else
168         {
169             // complex return via ptr
170             pCppReturn = *(void **)pCppStack
171                 = (bridges::cpp_uno::shared::relatesToInterfaceType(
172                        pReturnTypeDescr )
173                    ? alloca( pReturnTypeDescr->nSize )
174                    : pUnoReturn); // direct way
175             pCppStack += sizeof(void *);
176         }
177     }
178     // push this
179     void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
180         + aVtableSlot.offset;
181     *(void**)pCppStack = pAdjustedThisPtr;
182     pCppStack += sizeof( void* );
183 
184     // stack space
185     OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
186     // args
187     void ** pCppArgs  = (void **)alloca( 3 * sizeof(void *) * nParams );
188     // indizes of values this have to be converted (interface conversion cpp<=>uno)
189     sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
190     // type descriptions for reconversions
191     typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
192 
193     sal_Int32 nTempIndizes   = 0;
194 
195     for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
196     {
197         const typelib_MethodParameter & rParam = pParams[nPos];
198         typelib_TypeDescription * pParamTypeDescr = 0;
199         TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
200 
201         if (!rParam.bOut
202             && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
203         {
204             uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
205                                     pThis->getBridge()->getUno2Cpp() );
206 
207             switch (pParamTypeDescr->eTypeClass)
208             {
209             case typelib_TypeClass_HYPER:
210             case typelib_TypeClass_UNSIGNED_HYPER:
211             case typelib_TypeClass_DOUBLE:
212                 pCppStack += sizeof(sal_Int32); // extra long
213                 break;
214             default:
215                 break;
216             }
217             // no longer needed
218             TYPELIB_DANGER_RELEASE( pParamTypeDescr );
219         }
220         else // ptr to complex value | ref
221         {
222             if (! rParam.bIn) // is pure out
223             {
224                 // cpp out is constructed mem, uno out is not!
225                 uno_constructData(
226                     *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
227                     pParamTypeDescr );
228                 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
229                 // will be released at reconversion
230                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
231             }
232             // is in/inout
233             else if (bridges::cpp_uno::shared::relatesToInterfaceType(
234                          pParamTypeDescr ))
235             {
236                 uno_copyAndConvertData(
237                     *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
238                     pUnoArgs[nPos], pParamTypeDescr,
239                     pThis->getBridge()->getUno2Cpp() );
240 
241                 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
242                 // will be released at reconversion
243                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
244             }
245             else // direct way
246             {
247                 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
248                 // no longer needed
249                 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
250             }
251         }
252         pCppStack += sizeof(sal_Int32); // standard parameter length
253     }
254 
255     try
256     {
257         OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
258         callVirtualMethod(
259             pAdjustedThisPtr, aVtableSlot.index,
260             pCppReturn, pReturnTypeDescr->eTypeClass,
261             (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
262         // NO exception occured...
263         *ppUnoExc = 0;
264 
265         // reconvert temporary params
266         for ( ; nTempIndizes--; )
267         {
268             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
269             typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
270 
271             if (pParams[nIndex].bIn)
272             {
273                 if (pParams[nIndex].bOut) // inout
274                 {
275                     uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
276                     uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
277                                             pThis->getBridge()->getCpp2Uno() );
278                 }
279             }
280             else // pure out
281             {
282                 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
283                                         pThis->getBridge()->getCpp2Uno() );
284             }
285             // destroy temp cpp param => cpp: every param was constructed
286             uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
287 
288             TYPELIB_DANGER_RELEASE( pParamTypeDescr );
289         }
290         // return value
291         if (pCppReturn && pUnoReturn != pCppReturn)
292         {
293             uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
294                                     pThis->getBridge()->getCpp2Uno() );
295             uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
296         }
297     }
298     catch (...)
299     {
300         // fill uno exception
301         fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
302 
303         // temporary params
304         for ( ; nTempIndizes--; )
305         {
306             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
307             // destroy temp cpp param => cpp: every param was constructed
308             uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
309             TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
310         }
311         // return type
312         if (pReturnTypeDescr)
313             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
314     }
315 }
316 
317 }
318 
319 namespace bridges { namespace cpp_uno { namespace shared {
320 
321 void unoInterfaceProxyDispatch(
322     uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
323     void * pReturn, void * pArgs[], uno_Any ** ppException )
324 {
325     // is my surrogate
326     bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
327         = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * >(pUnoI);
328 
329     switch (pMemberDescr->eTypeClass)
330     {
331     case typelib_TypeClass_INTERFACE_ATTRIBUTE:
332     {
333         VtableSlot aVtableSlot(
334             getVtableSlot(
335                 reinterpret_cast<
336                     typelib_InterfaceAttributeTypeDescription const * >(
337                         pMemberDescr)));
338         if (pReturn)
339         {
340             // dependent dispatch
341             cpp_call(
342                 pThis, aVtableSlot,
343                 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
344                 0, 0, // no params
345                 pReturn, pArgs, ppException );
346         }
347         else
348         {
349             // is SET
350             typelib_MethodParameter aParam;
351             aParam.pTypeRef =
352                 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
353             aParam.bIn      = sal_True;
354             aParam.bOut     = sal_False;
355 
356             typelib_TypeDescriptionReference * pReturnTypeRef = 0;
357             OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
358             typelib_typedescriptionreference_new(
359                 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
360 
361             // dependent dispatch
362             aVtableSlot.index += 1; // get, then set method
363             cpp_call(
364                 pThis, aVtableSlot,
365                 pReturnTypeRef,
366                 1, &aParam,
367                 pReturn, pArgs, ppException );
368 
369             typelib_typedescriptionreference_release( pReturnTypeRef );
370         }
371 
372         break;
373     }
374     case typelib_TypeClass_INTERFACE_METHOD:
375     {
376         VtableSlot aVtableSlot(
377             getVtableSlot(
378                 reinterpret_cast<
379                     typelib_InterfaceMethodTypeDescription const * >(
380                         pMemberDescr)));
381         switch (aVtableSlot.index)
382         {
383             // standard calls
384         case 1: // acquire uno interface
385             (*pUnoI->acquire)( pUnoI );
386             *ppException = 0;
387             break;
388         case 2: // release uno interface
389             (*pUnoI->release)( pUnoI );
390             *ppException = 0;
391             break;
392         case 0: // queryInterface() opt
393         {
394             typelib_TypeDescription * pTD = 0;
395             TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
396             if (pTD)
397             {
398                 uno_Interface * pInterface = 0;
399                 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
400                     pThis->pBridge->getUnoEnv(),
401                     (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
402 
403                 if (pInterface)
404                 {
405                     ::uno_any_construct(
406                         reinterpret_cast< uno_Any * >( pReturn ),
407                         &pInterface, pTD, 0 );
408                     (*pInterface->release)( pInterface );
409                     TYPELIB_DANGER_RELEASE( pTD );
410                     *ppException = 0;
411                     break;
412                 }
413                 TYPELIB_DANGER_RELEASE( pTD );
414             }
415         } // else perform queryInterface()
416         default:
417             // dependent dispatch
418             cpp_call(
419                 pThis, aVtableSlot,
420                 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
421                 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
422                 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
423                 pReturn, pArgs, ppException );
424         }
425         break;
426     }
427     default:
428     {
429         ::com::sun::star::uno::RuntimeException aExc(
430             OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
431             ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
432 
433         Type const & rExcType = ::getCppuType( &aExc );
434         // binary identical null reference
435         ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
436     }
437     }
438 }
439 
440 } } }
441