merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / cc50_solaris_intel / cpp2uno.cxx
blobf96ce0dce56eb91126391ce452db626b9cc25894
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.17 $
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 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
34 #include <sal/alloca.h>
36 #include <com/sun/star/uno/genfunc.hxx>
37 #include <uno/data.h>
38 #include <typelib/typedescription.hxx>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/types.hxx"
43 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
45 #include "cc50_solaris_intel.hxx"
47 using namespace com::sun::star::uno;
49 namespace
52 //==================================================================================================
53 void cpp2uno_call(
54 bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
55 const typelib_TypeDescription * pMemberTypeDescr,
56 typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
57 sal_Int32 nParams, typelib_MethodParameter * pParams,
58 void ** pCallStack,
59 sal_Int64 * pRegisterReturn /* space for register return */ )
61 // pCallStack: ret, [return ptr], this, params
62 char * pCppStack = (char *)(pCallStack +1);
64 // return
65 typelib_TypeDescription * pReturnTypeDescr = 0;
66 if (pReturnTypeRef)
67 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
69 void * pUnoReturn = 0;
70 void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
72 if (pReturnTypeDescr)
74 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
76 pUnoReturn = pRegisterReturn; // direct way for simple types
78 else // complex return via ptr (pCppReturn)
80 pCppReturn = *(void**)pCppStack;
81 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 ] = pUnoArgs[ nPos ] = pCppStack;
114 switch (pParamTypeDescr->eTypeClass)
116 case typelib_TypeClass_HYPER:
117 case typelib_TypeClass_UNSIGNED_HYPER:
118 case typelib_TypeClass_DOUBLE:
119 pCppStack += sizeof(sal_Int32); // extra long
121 // no longer needed
122 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
124 else // ptr to complex value | ref
126 pCppArgs[nPos] = *(void **)pCppStack;
128 if (! rParam.bIn) // is pure out
130 // uno out is unconstructed mem!
131 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
132 pTempIndizes[nTempIndizes] = nPos;
133 // will be released at reconversion
134 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
136 // is in/inout
137 else if (bridges::cpp_uno::shared::relatesToInterfaceType(
138 pParamTypeDescr ))
140 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
141 *(void **)pCppStack, pParamTypeDescr,
142 pThis->getBridge()->getCpp2Uno() );
143 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
144 // will be released at reconversion
145 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
147 else // direct way
149 pUnoArgs[nPos] = *(void **)pCppStack;
150 // no longer needed
151 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
154 pCppStack += sizeof(sal_Int32); // standard parameter length
157 // ExceptionHolder
158 uno_Any aUnoExc; // Any will be constructed by callee
159 uno_Any * pUnoExc = &aUnoExc;
161 // invoke uno dispatch call
162 (*pThis->getUnoI()->pDispatcher)(
163 pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
165 // in case no exception occured...
166 if (pUnoExc)
168 // destruct temporary in/inout params
169 for ( ; nTempIndizes--; )
171 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
173 if (pParams[nIndex].bIn) // is in/inout => was constructed
174 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
175 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
177 if (pReturnTypeDescr)
178 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
180 CPPU_CURRENT_NAMESPACE::cc50_solaris_intel_raiseException(
181 &aUnoExc, pThis->getBridge()->getUno2Cpp() );
182 // has to destruct the any
184 else // else no exception occured...
186 // temporary params
187 for ( ; nTempIndizes--; )
189 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
190 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
192 if (pParams[nIndex].bOut) // inout/out
194 // convert and assign
195 uno_destructData(
196 pCppArgs[nIndex], pParamTypeDescr,
197 reinterpret_cast< uno_ReleaseFunc >(cpp_release) );
198 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
199 pThis->getBridge()->getUno2Cpp() );
201 // destroy temp uno param
202 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
204 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
206 // return
207 if (pCppReturn) // has complex return
209 if (pUnoReturn != pCppReturn) // needs reconversion
211 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
212 pThis->getBridge()->getUno2Cpp() );
213 // destroy temp uno return
214 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
216 // complex return ptr is set to eax
217 *(void **)pRegisterReturn = pCppReturn;
219 if (pReturnTypeDescr)
221 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
227 //==================================================================================================
228 extern "C" void cpp_vtable_call(
229 int nFunctionIndex, int nVtableOffset, void** pCallStack,
230 sal_Int64 nRegReturn )
232 OSL_ENSURE( sizeof(sal_Int32)==sizeof(void *), "### unexpected!" );
234 // pCallStack: ret adr, [ret *], this, params
235 void * pThis;
236 if( nFunctionIndex & 0x80000000 )
238 nFunctionIndex &= 0x7fffffff;
239 pThis = pCallStack[2];
241 else
243 pThis = pCallStack[1];
245 pThis = static_cast< char * >(pThis) - nVtableOffset;
246 bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
247 = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
248 pThis);
250 typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
252 OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex,
253 "### illegal vtable index!" );
254 if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
256 throw RuntimeException( rtl::OUString::createFromAscii("illegal vtable index!"), (XInterface *)pThis );
259 // determine called method
260 sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
261 OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
263 TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
265 switch (aMemberDescr.get()->eTypeClass)
267 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
269 if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
271 // is GET method
272 cpp2uno_call(
273 pCppI, aMemberDescr.get(),
274 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
275 0, 0, // no params
276 pCallStack, &nRegReturn );
278 else
280 // is SET method
281 typelib_MethodParameter aParam;
282 aParam.pTypeRef =
283 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
284 aParam.bIn = sal_True;
285 aParam.bOut = sal_False;
287 cpp2uno_call(
288 pCppI, aMemberDescr.get(),
289 0, // indicates void return
290 1, &aParam,
291 pCallStack, &nRegReturn );
293 break;
295 case typelib_TypeClass_INTERFACE_METHOD:
297 // is METHOD
298 switch (nFunctionIndex)
300 // standard XInterface vtable calls
301 case 1: // acquire()
302 pCppI->acquireProxy(); // non virtual call!
303 break;
304 case 2: // release()
305 pCppI->releaseProxy(); // non virtual call!
306 break;
307 case 0: // queryInterface() opt
309 typelib_TypeDescription * pTD = 0;
310 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pCallStack[3] )->getTypeLibType() );
311 if (pTD)
313 XInterface * pInterface = 0;
314 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
315 pCppI->getBridge()->getCppEnv(),
316 (void **)&pInterface, pCppI->getOid().pData,
317 (typelib_InterfaceTypeDescription *)pTD );
319 if (pInterface)
321 ::uno_any_construct(
322 reinterpret_cast< uno_Any * >( pCallStack[1] ),
323 &pInterface, pTD,
324 reinterpret_cast< uno_AcquireFunc >(cpp_acquire) );
325 pInterface->release();
326 TYPELIB_DANGER_RELEASE( pTD );
327 *(void **)&nRegReturn = pCallStack[1];
328 break;
330 TYPELIB_DANGER_RELEASE( pTD );
332 } // else perform queryInterface()
333 default:
334 cpp2uno_call(
335 pCppI, aMemberDescr.get(),
336 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
337 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
338 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
339 pCallStack, &nRegReturn );
341 break;
343 default:
345 throw RuntimeException( rtl::OUString::createFromAscii("no member description found!"), (XInterface *)pThis );
350 //==================================================================================================
351 bool isSimpleStruct(typelib_TypeDescriptionReference * type) {
352 typelib_TypeDescription * td = 0;
353 TYPELIB_DANGER_GET(&td, type);
354 OSL_ASSERT(td != 0);
355 for (typelib_CompoundTypeDescription * ctd
356 = reinterpret_cast< typelib_CompoundTypeDescription * >(td);
357 ctd != 0; ctd = ctd->pBaseTypeDescription)
359 OSL_ASSERT(ctd->aBase.eTypeClass == typelib_TypeClass_STRUCT);
360 for (sal_Int32 i = 0; i < ctd->nMembers; ++i) {
361 typelib_TypeClass c = ctd->ppTypeRefs[i]->eTypeClass;
362 switch (c) {
363 case typelib_TypeClass_STRING:
364 case typelib_TypeClass_TYPE:
365 case typelib_TypeClass_ANY:
366 case typelib_TypeClass_SEQUENCE:
367 case typelib_TypeClass_INTERFACE:
368 return false;
369 case typelib_TypeClass_STRUCT:
370 if (!isSimpleStruct(ctd->ppTypeRefs[i])) {
371 return false;
373 break;
374 default:
375 OSL_ASSERT(
376 c <= typelib_TypeClass_DOUBLE
377 || c == typelib_TypeClass_ENUM);
378 break;
382 TYPELIB_DANGER_RELEASE(td);
383 return true;
386 extern "C" void privateSnippetExecutorGeneral();
387 extern "C" void privateSnippetExecutorVoid();
388 extern "C" void privateSnippetExecutorHyper();
389 extern "C" void privateSnippetExecutorFloat();
390 extern "C" void privateSnippetExecutorDouble();
391 extern "C" void privateSnippetExecutorStruct();
392 extern "C" typedef void (*PrivateSnippetExecutor)();
394 int const codeSnippetSize = 16;
396 unsigned char * codeSnippet(
397 unsigned char * code, sal_Int32 functionIndex, sal_Int32 vtableOffset,
398 typelib_TypeDescriptionReference * returnType)
400 typelib_TypeClass c = returnType == 0
401 ? typelib_TypeClass_VOID : returnType->eTypeClass;
402 if (returnType != 0 && !bridges::cpp_uno::shared::isSimpleType(c)) {
403 functionIndex |= 0x80000000;
405 PrivateSnippetExecutor exec;
406 switch (c) {
407 case typelib_TypeClass_VOID:
408 exec = privateSnippetExecutorVoid;
409 break;
410 case typelib_TypeClass_HYPER:
411 case typelib_TypeClass_UNSIGNED_HYPER:
412 exec = privateSnippetExecutorHyper;
413 break;
414 case typelib_TypeClass_FLOAT:
415 exec = privateSnippetExecutorFloat;
416 break;
417 case typelib_TypeClass_DOUBLE:
418 exec = privateSnippetExecutorDouble;
419 break;
420 case typelib_TypeClass_STRUCT:
421 OSL_ASSERT(returnType != 0);
422 // For "simple" (more-or-less POD, but not exactly) structs, the caller
423 // pops the pointer to the return value off the stack, as documented in
424 // the Intel SYSV ABI; for other structs (which includes STRING, TYPE,
425 // ANY, sequences, and interfaces, btw.), the callee pops the pointer to
426 // the return value off the stack:
427 exec = isSimpleStruct(returnType)
428 ? privateSnippetExecutorStruct : privateSnippetExecutorGeneral;
429 break;
430 default:
431 exec = privateSnippetExecutorGeneral;
432 break;
434 unsigned char * p = code;
435 OSL_ASSERT(sizeof (sal_Int32) == 4);
436 // mov function_index, %eax:
437 *p++ = 0xB8;
438 *reinterpret_cast< sal_Int32 * >(p) = functionIndex;
439 p += sizeof (sal_Int32);
440 // mov vtable_offset, %edx:
441 *p++ = 0xBA;
442 *reinterpret_cast< sal_Int32 * >(p) = vtableOffset;
443 p += sizeof (sal_Int32);
444 // jmp privateSnippetExecutor:
445 *p++ = 0xE9;
446 #pragma disable_warn
447 void * e = reinterpret_cast< void * >(exec);
448 #pragma enable_warn
449 *reinterpret_cast< sal_Int32 * >(p)
450 = static_cast< unsigned char * >(e) - p - sizeof (sal_Int32);
451 p += sizeof (sal_Int32);
452 OSL_ASSERT(p - code <= codeSnippetSize);
453 return code + codeSnippetSize;
458 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
460 bridges::cpp_uno::shared::VtableFactory::Slot *
461 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block) {
462 return static_cast< Slot * >(block) + 1;
465 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
466 sal_Int32 slotCount)
468 return (slotCount + 3) * sizeof (Slot) + slotCount * codeSnippetSize;
471 bridges::cpp_uno::shared::VtableFactory::Slot *
472 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
473 void * block, sal_Int32 slotCount)
475 Slot * slots = mapBlockToVtable(block) + 2;
476 slots[-3].fn = 0; // RTTI
477 slots[-2].fn = 0; // null
478 slots[-1].fn = 0; // destructor
479 return slots + slotCount;
482 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
483 Slot ** slots, unsigned char * code,
484 typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
485 sal_Int32 functionCount, sal_Int32 vtableOffset)
487 (*slots) -= functionCount;
488 Slot * s = *slots;
489 for (sal_Int32 i = 0; i < type->nMembers; ++i) {
490 typelib_TypeDescription * member = 0;
491 TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
492 OSL_ASSERT(member != 0);
493 switch (member->eTypeClass) {
494 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
495 // Getter:
496 (s++)->fn = code;
497 code = codeSnippet(
498 code, functionOffset++, vtableOffset,
499 reinterpret_cast< typelib_InterfaceAttributeTypeDescription * >(
500 member)->pAttributeTypeRef);
501 // Setter:
502 if (!reinterpret_cast<
503 typelib_InterfaceAttributeTypeDescription * >(
504 member)->bReadOnly)
506 (s++)->fn = code;
507 code = codeSnippet(code, functionOffset++, vtableOffset, 0);
509 break;
511 case typelib_TypeClass_INTERFACE_METHOD:
512 (s++)->fn = code;
513 code = codeSnippet(
514 code, functionOffset++, vtableOffset,
515 reinterpret_cast< typelib_InterfaceMethodTypeDescription * >(
516 member)->pReturnTypeRef);
517 break;
519 default:
520 OSL_ASSERT(false);
521 break;
523 TYPELIB_DANGER_RELEASE(member);
525 return code;
528 void bridges::cpp_uno::shared::VtableFactory::flushCode(
529 unsigned char const *, unsigned char const *)