merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_os2_intel / cpp2uno.cxx
blob8fd59414d86d98285231d98646265d031ba897c4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: cpp2uno.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include <sal/alloca.h>
32 #include <stdio.h>
34 #include <com/sun/star/uno/genfunc.hxx>
35 #include "com/sun/star/uno/RuntimeException.hpp"
36 #include <uno/data.h>
37 #include <typelib/typedescription.hxx>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
44 #include "share.hxx"
46 using namespace ::com::sun::star::uno;
48 namespace
51 //==================================================================================================
52 static typelib_TypeClass cpp2uno_call(
53 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
54 const typelib_TypeDescription * pMemberTypeDescr,
55 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
56 sal_Int32 nParams, typelib_MethodParameter * pParams,
57 void ** pCallStack,
58 sal_Int64 * pRegisterReturn /* space for register return */ )
60 // pCallStack: ret, [return ptr], this, params
61 char * pCppStack = (char *)(pCallStack +1);
63 // return
64 typelib_TypeDescription * pReturnTypeDescr = 0;
65 if (pReturnTypeRef)
66 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
68 void * pUnoReturn = 0;
69 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
71 if (pReturnTypeDescr)
73 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
75 pUnoReturn = pRegisterReturn; // direct way for simple types
77 else // complex return via ptr (pCppReturn)
79 pCppReturn = *(void **)pCppStack;
80 pCppStack += sizeof(void *);
82 pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType(
83 pReturnTypeDescr )
84 ? alloca( pReturnTypeDescr->nSize )
85 : pCppReturn); // direct way
88 // pop this
89 pCppStack += sizeof( void* );
91 // stack space
92 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
93 // parameters
94 void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
95 void ** pCppArgs = pUnoArgs + nParams;
96 // indizes of values this have to be converted (interface conversion cpp<=>uno)
97 sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
98 // type descriptions for reconversions
99 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
101 sal_Int32 nTempIndizes = 0;
103 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
105 const typelib_MethodParameter & rParam = pParams[nPos];
106 typelib_TypeDescription * pParamTypeDescr = 0;
107 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
109 if (!rParam.bOut
110 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
111 // value
113 pCppArgs[nPos] = pCppStack;
114 pUnoArgs[nPos] = pCppStack;
115 switch (pParamTypeDescr->eTypeClass)
117 case typelib_TypeClass_HYPER:
118 case typelib_TypeClass_UNSIGNED_HYPER:
119 case typelib_TypeClass_DOUBLE:
120 pCppStack += sizeof(sal_Int32); // extra long
122 // no longer needed
123 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
125 else // ptr to complex value | ref
127 pCppArgs[nPos] = *(void **)pCppStack;
129 if (! rParam.bIn) // is pure out
131 // uno out is unconstructed mem!
132 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
133 pTempIndizes[nTempIndizes] = nPos;
134 // will be released at reconversion
135 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
137 // is in/inout
138 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
139 pParamTypeDescr ))
141 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
142 *(void **)pCppStack, pParamTypeDescr,
143 pThis->getBridge()->getCpp2Uno() );
144 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
145 // will be released at reconversion
146 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
148 else // direct way
150 pUnoArgs[nPos] = *(void **)pCppStack;
151 // no longer needed
152 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
155 pCppStack += sizeof(sal_Int32); // standard parameter length
158 // ExceptionHolder
159 uno_Any aUnoExc = {0}; // Any will be constructed by callee
160 uno_Any * pUnoExc = &aUnoExc;
162 // invoke uno dispatch call
163 (*pThis->getUnoI()->pDispatcher)(
164 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
165 #if OSL_DEBUG_LEVEL > 1
166 if (pUnoExc != NULL)
167 fprintf( stderr, ">cpp2uno_call pUnoExc %x\n", pUnoExc);
168 #endif
170 // in case an exception occured...
171 if (pUnoExc)
173 // destruct temporary in/inout params
174 for ( ; nTempIndizes--; )
176 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
178 if (pParams[nIndex].bIn) // is in/inout => was constructed
179 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
180 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
182 if (pReturnTypeDescr)
183 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
185 CPPU_CURRENT_NAMESPACE::raiseException(
186 &aUnoExc, pThis->getBridge()->getUno2Cpp() );
187 // has to destruct the any
188 // is here for dummy
189 return typelib_TypeClass_VOID;
191 else // else no exception occured...
193 // temporary params
194 for ( ; nTempIndizes--; )
196 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
197 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
199 if (pParams[nIndex].bOut) // inout/out
201 // convert and assign
202 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
203 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
204 pThis->getBridge()->getUno2Cpp() );
206 // destroy temp uno param
207 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
209 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
211 // return
212 if (pCppReturn) // has complex return
214 if (pUnoReturn != pCppReturn) // needs reconversion
216 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
217 pThis->getBridge()->getUno2Cpp() );
218 // destroy temp uno return
219 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
221 // complex return ptr is set to eax
222 *(void **)pRegisterReturn = pCppReturn;
224 if (pReturnTypeDescr)
226 typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
227 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
228 return eRet;
230 else
231 return typelib_TypeClass_VOID;
236 //==================================================================================================
237 static typelib_TypeClass cpp_mediate(
238 sal_Int32 nFunctionIndex,
239 sal_Int32 nVtableOffset,
240 void ** pCallStack,
241 sal_Int64 * pRegisterReturn /* space for register return */ )
243 OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
245 // pCallStack: ret adr, [ret *], this, params
246 void * pThis;
247 if( nFunctionIndex & 0x80000000 )
249 nFunctionIndex &= 0x7fffffff;
250 pThis = pCallStack[2];
252 else
254 pThis = pCallStack[1];
256 pThis = static_cast< char * >(pThis) - nVtableOffset;
257 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
258 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
259 pThis);
261 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
263 OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
264 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
266 throw RuntimeException(
267 rtl::OUString::createFromAscii("illegal vtable index!"),
268 (XInterface *)pThis );
271 // determine called method
272 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
273 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
275 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
277 typelib_TypeClass eRet;
278 switch (aMemberDescr.get()->eTypeClass)
280 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
282 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
284 // is GET method
285 eRet = cpp2uno_call(
286 pCppI, aMemberDescr.get(),
287 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
288 0, 0, // no params
289 pCallStack, pRegisterReturn );
291 else
293 // is SET method
294 typelib_MethodParameter aParam;
295 aParam.pTypeRef =
296 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
297 aParam.bIn = sal_True;
298 aParam.bOut = sal_False;
300 eRet = cpp2uno_call(
301 pCppI, aMemberDescr.get(),
302 0, // indicates void return
303 1, &aParam,
304 pCallStack, pRegisterReturn );
306 break;
308 case typelib_TypeClass_INTERFACE_METHOD:
310 // is METHOD
311 switch (nFunctionIndex)
313 case 1: // acquire()
314 pCppI->acquireProxy(); // non virtual call!
315 eRet = typelib_TypeClass_VOID;
316 break;
317 case 2: // release()
318 pCppI->releaseProxy(); // non virtual call!
319 eRet = typelib_TypeClass_VOID;
320 break;
321 case 0: // queryInterface() opt
323 typelib_TypeDescription * pTD = 0;
324 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[3] )->getTypeLibType() );
325 if (pTD)
327 XInterface * pInterface = 0;
328 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
329 pCppI->getBridge()->getCppEnv(),
330 (void **)&pInterface, pCppI->getOid().pData,
331 (typelib_InterfaceTypeDescription *)pTD );
333 if (pInterface)
335 ::uno_any_construct(
336 reinterpret_cast< uno_Any * >( pCallStack[1] ),
337 &pInterface, pTD, cpp_acquire );
338 pInterface->release();
339 TYPELIB_DANGER_RELEASE( pTD );
340 *(void **)pRegisterReturn = pCallStack[1];
341 eRet = typelib_TypeClass_ANY;
342 break;
344 TYPELIB_DANGER_RELEASE( pTD );
346 } // else perform queryInterface()
347 default:
348 eRet = cpp2uno_call(
349 pCppI, aMemberDescr.get(),
350 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
351 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
352 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
353 pCallStack, pRegisterReturn );
355 break;
357 default:
359 throw RuntimeException(
360 rtl::OUString::createFromAscii("no member description found!"),
361 (XInterface *)pThis );
362 // is here for dummy
363 eRet = typelib_TypeClass_VOID;
367 return eRet;
370 //==================================================================================================
372 * is called on incoming vtable calls
373 * (called by asm snippets)
375 static void cpp_vtable_call(
376 int nFunctionIndex, int nVtableOffset, void** pCallStack )
377 __attribute__((regparm(3)));
379 void cpp_vtable_call( int nFunctionIndex, int nVtableOffset, void** pCallStack )
381 volatile long nRegReturn[2];
382 typelib_TypeClass aType = cpp_mediate(
383 nFunctionIndex, nVtableOffset, pCallStack, (sal_Int64*)nRegReturn );
385 switch( aType )
387 case typelib_TypeClass_HYPER:
388 case typelib_TypeClass_UNSIGNED_HYPER:
389 __asm__( "movl %1, %%edx\n\t"
390 "movl %0, %%eax\n"
391 : : "m"(nRegReturn[0]), "m"(nRegReturn[1]) );
392 break;
393 case typelib_TypeClass_FLOAT:
394 __asm__( "flds %0\n\t"
395 "fstp %%st(0)\n\t"
396 "flds %0\n"
397 : : "m"(*(float *)nRegReturn) );
398 break;
399 case typelib_TypeClass_DOUBLE:
400 __asm__( "fldl %0\n\t"
401 "fstp %%st(0)\n\t"
402 "fldl %0\n"
403 : : "m"(*(double *)nRegReturn) );
404 break;
405 // case typelib_TypeClass_UNSIGNED_SHORT:
406 // case typelib_TypeClass_SHORT:
407 // __asm__( "movswl %0, %%eax\n"
408 // : : "m"(nRegReturn) );
409 // break;
410 default:
411 __asm__( "movl %0, %%eax\n"
412 : : "m"(nRegReturn[0]) );
413 break;
418 //==================================================================================================
419 int const codeSnippetSize = 20;
421 unsigned char * codeSnippet(
422 unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
423 bool simpleRetType)
425 if (!simpleRetType) {
426 functionIndex |= 0x80000000;
428 unsigned char * p = code;
429 OSL_ASSERT(sizeof (sal_Int32) == 4);
430 // mov function_index, %eax:
431 *p++ = 0xB8;
432 *reinterpret_cast< sal_Int32 * >(p) = functionIndex;
433 p += sizeof (sal_Int32);
434 // mov vtable_offset, %edx:
435 *p++ = 0xBA;
436 *reinterpret_cast< sal_Int32 * >(p) = vtableOffset;
437 p += sizeof (sal_Int32);
438 // mov %esp, %ecx:
439 *p++ = 0x89;
440 *p++ = 0xE1;
441 // jmp cpp_vtable_call:
442 *p++ = 0xE9;
443 *reinterpret_cast< sal_Int32 * >(p)
444 = ((unsigned char *) cpp_vtable_call) - p - sizeof (sal_Int32);
445 p += sizeof (sal_Int32);
446 OSL_ASSERT(p - code <= codeSnippetSize);
447 return code + codeSnippetSize;
452 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
454 bridges::cpp_uno::shared::VtableFactory::Slot *
455 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
457 return static_cast< Slot * >(block) + 2;
460 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
461 sal_Int32 slotCount)
463 return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
466 bridges::cpp_uno::shared::VtableFactory::Slot *
467 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
468 void * block, sal_Int32 slotCount)
470 Slot * slots = mapBlockToVtable(block);
471 slots[-2].fn = 0;
472 slots[-1].fn = 0;
473 return slots + slotCount;
476 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
477 Slot ** slots, unsigned char * code,
478 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
479 sal_Int32 functionCount, sal_Int32 vtableOffset)
481 (*slots) -= functionCount;
482 Slot * s = *slots;
483 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
484 typelib_TypeDescription * member = 0;
485 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
486 OSL_ASSERT(member != 0);
487 switch (member->eTypeClass) {
488 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
489 // Getter:
490 (s++)->fn = code;
491 code = codeSnippet(
492 code, functionOffset++, vtableOffset,
493 bridges::cpp_uno::shared::isSimpleType(
494 reinterpret_cast<
495 typelib_InterfaceAttributeTypeDescription * >(
496 member)->pAttributeTypeRef));
497 // Setter:
498 if (!reinterpret_cast<
499 typelib_InterfaceAttributeTypeDescription * >(
500 member)->bReadOnly)
502 (s++)->fn = code;
503 code = codeSnippet(code, functionOffset++, vtableOffset, true);
505 break;
507 case typelib_TypeClass_INTERFACE_METHOD:
508 (s++)->fn = code;
509 code = codeSnippet(
510 code, functionOffset++, vtableOffset,
511 bridges::cpp_uno::shared::isSimpleType(
512 reinterpret_cast<
513 typelib_InterfaceMethodTypeDescription * >(
514 member)->pReturnTypeRef));
515 break;
517 default:
518 OSL_ASSERT(false);
519 break;
521 TYPELIB_DANGER_RELEASE(member);
523 return code;
526 void bridges::cpp_uno::shared::VtableFactory::flushCode(
527 unsigned char const *, unsigned char const *)