merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_macosx_powerpc / uno2cpp.cxx
blob3e2079029c95694f9a7c3396c6a3da21ceb9406a
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: uno2cpp.cxx,v $
10 * $Revision: 1.9 $
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 <sys/types.h>
35 #include <sys/malloc.h>
37 #include <com/sun/star/uno/genfunc.hxx>
38 #include <uno/data.h>
40 #include "bridges/cpp_uno/shared/bridge.hxx"
41 #include "bridges/cpp_uno/shared/types.hxx"
42 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
43 #include "bridges/cpp_uno/shared/vtables.hxx"
45 #include "share.hxx"
48 using namespace ::rtl;
49 using namespace ::com::sun::star::uno;
51 namespace
54 //==================================================================================================
55 static void callVirtualMethod(
56 void * pAdjustedThisPtr,
57 sal_Int32 nVtableIndex,
58 void * pRegisterReturn,
59 typelib_TypeClass eReturnType,
60 char * pPT,
61 sal_Int32 * pStackLongs,
62 sal_Int32 /* nStackLongs */)
65 // parameter list is mixed list of * and values
66 // reference parameters are pointers
68 // the basic idea here is to use gpr[8] as a storage area for
69 // the future values of registers r3 to r10 needed for the call,
70 // and similarly fpr[13] as a storage area for the future values
71 // of floating point registers f1 to f13
73 unsigned long * mfunc; // actual function to be invoked
74 void (*ptr)();
75 int gpr[8]; // storage for gpregisters, map to r3-r10
76 int off; // offset used to find function
77 double fpr[13]; // storage for fpregisters, map to f1-f13
78 int n; // number of gprs mapped so far
79 int f; // number of fprs mapped so far
80 volatile long *p; // pointer to parameter overflow area
81 int c; // character of parameter type being decoded
82 volatile double dret; // temporary function return values
83 volatile int iret, iret2;
85 // Because of the Power PC calling conventions we could be passing
86 // parameters in both register types and on the stack. To create the
87 // stack parameter area we need we now simply allocate local
88 // variable storage param[] that is at least the size of the parameter stack
89 // (more than enough space) which we can overwrite the parameters into.
91 // Note: This keeps us from having to decode the signature twice and
92 // prevents problems with later local variables.
94 // FIXME: I do not believe the following is true but we will keep the
95 // FIXME: extra space just to be safe until proven otherwise
97 // Note: could require up to 2*nStackLongs words of parameter stack area
98 // if the call has many float parameters (i.e. floats take up only 1
99 // word on the stack but take 2 words in parameter area in the
100 // stack frame .
103 // unsigned long param[(2*nStackLongs)];
105 /* now begin to load the C++ function arguments into storage */
106 n = 0;
107 f = 0;
110 /* set up a pointer to the stack parameter area */
111 __asm__ ( "addi %0,r1,24" : "=r" (p) : /* no inputs */ );
113 // #i94421#, work around compiler error:
114 volatile long * pCopy = p;
115 (void) pCopy; // avoid warning about unused variable
117 // never called
118 // if (! pAdjustedThisPtr )CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
121 // now we need to parse the entire signature string
122 // until we get the END indicator
124 // treat complex return pointer like any other parameter
126 // parse the argument list up to the ending )
128 while (*pPT != 'X') {
129 c = *pPT;
130 switch (c) {
132 case 'D': /* type is double */
133 if (f < 13) {
134 fpr[f++] = *((double *)pStackLongs); /* store in register */
135 n+=2;
136 p+=2;
137 } else {
138 *p++ = *pStackLongs; /* or on the parameter stack */
139 *p++ = *(pStackLongs + 1);
141 pStackLongs += 2;
142 break;
144 case 'F': /* type is float */
145 /* floats are stored as 1 32 bit word on param stack */
146 if (f < 13) {
147 fpr[f++] = *((float *)pStackLongs);
148 n+=1;
149 p++;
150 } else {
151 *((float *)p) = *((float *)pStackLongs);
152 p += 1;
154 pStackLongs += 1;
155 break;
157 case 'H': /* type is long long */
158 if (n < 8)
160 gpr[n++] = *pStackLongs;
161 p++;
163 else
164 *p++ = *pStackLongs;
165 if(n < 8)
167 gpr[n++] = *(pStackLongs+1);
168 p++;
170 else
171 *p++ = *(pStackLongs+1);
172 pStackLongs += 2;
173 break;
175 case 'S':
176 if (n < 8) {
177 gpr[n++] = *((unsigned short*)pStackLongs);
178 p++;
179 } else {
180 *p++ = *((unsigned short *)pStackLongs);
182 pStackLongs += 1;
183 break;
185 case 'B':
186 if (n < 8) {
187 gpr[n++] = *((char *)pStackLongs);
188 p++;
189 } else {
190 *p++ = *((char *)pStackLongs);
192 pStackLongs += 1;
193 break;
195 default:
196 if (n < 8) {
197 gpr[n++] = *pStackLongs;
198 p++;
199 } else {
200 *p++ = *pStackLongs;
202 pStackLongs += 1;
203 break;
205 pPT++;
209 /* figure out the address of the function we need to invoke */
210 off = nVtableIndex;
211 off = off * 4; // 4 bytes per slot
212 mfunc = *((unsigned long **)pAdjustedThisPtr); // get the address of the vtable
213 mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
214 mfunc = *((unsigned long **)mfunc); // the function is stored at the address
215 ptr = (void (*)())mfunc;
217 /* Set up the machine registers and invoke the function */
219 __asm__ __volatile__ (
220 "lwz r3, 0(%0)\n\t"
221 "lwz r4, 4(%0)\n\t"
222 "lwz r5, 8(%0)\n\t"
223 "lwz r6, 12(%0)\n\t"
224 "lwz r7, 16(%0)\n\t"
225 "lwz r8, 20(%0)\n\t"
226 "lwz r9, 24(%0)\n\t"
227 "lwz r10, 28(%0)\n\t"
228 "lfd f1, 0(%1)\n\t"
229 "lfd f2, 8(%1)\n\t"
230 "lfd f3, 16(%1)\n\t"
231 "lfd f4, 24(%1)\n\t"
232 "lfd f5, 32(%1)\n\t"
233 "lfd f6, 40(%1)\n\t"
234 "lfd f7, 48(%1)\n\t"
235 "lfd f8, 56(%1)\n\t"
236 "lfd f9, 64(%1)\n\t"
237 "lfd f10, 72(%1)\n\t"
238 "lfd f11, 80(%1)\n\t"
239 "lfd f12, 88(%1)\n\t"
240 "lfd f13, 96(%1)\n\t"
241 : : "r" (gpr), "r" (fpr)
242 : "r0", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
243 "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9",
244 "f10", "f11", "f12", "f13"
247 (*ptr)();
250 __asm__ __volatile__ (
251 "stw r3, %1\n\t"
252 "stw r4, %2\n\t"
253 "stfd f1, %0\n\t"
254 : : "m" (dret), "m" (iret), "m" (iret2)
258 switch( eReturnType )
260 case typelib_TypeClass_HYPER:
261 case typelib_TypeClass_UNSIGNED_HYPER:
262 ((long*)pRegisterReturn)[1] = iret2;
263 // fall thru on purpose
264 case typelib_TypeClass_LONG:
265 case typelib_TypeClass_UNSIGNED_LONG:
266 case typelib_TypeClass_ENUM:
267 ((long*)pRegisterReturn)[0] = iret;
268 break;
270 case typelib_TypeClass_CHAR:
271 case typelib_TypeClass_SHORT:
272 case typelib_TypeClass_UNSIGNED_SHORT:
273 *(unsigned short*)pRegisterReturn = (unsigned short)iret;
274 break;
276 case typelib_TypeClass_BOOLEAN:
277 case typelib_TypeClass_BYTE:
278 *(unsigned char*)pRegisterReturn = (unsigned char)iret;
279 break;
281 case typelib_TypeClass_FLOAT:
282 *(float*)pRegisterReturn = (float)dret;
283 break;
285 case typelib_TypeClass_DOUBLE:
286 *(double*)pRegisterReturn = dret;
287 break;
288 default:
289 break;
294 //==================================================================================================
295 static void cpp_call(
296 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
297 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
298 typelib_TypeDescriptionReference * pReturnTypeRef,
299 sal_Int32 nParams, typelib_MethodParameter * pParams,
300 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
302 // max space for: [complex ret ptr], values|ptr ...
303 char * pCppStack =
304 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
305 char * pCppStackStart = pCppStack;
307 // need to know parameter types for callVirtualMethod so generate a signature string
308 char * pParamType = (char *) alloca(nParams+2);
309 char * pPT = pParamType;
311 // return
312 typelib_TypeDescription * pReturnTypeDescr = 0;
313 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
314 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
316 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
318 if (pReturnTypeDescr)
320 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
322 pCppReturn = pUnoReturn; // direct way for simple types
324 else
326 // complex return via ptr
327 pCppReturn = *(void **)pCppStack
328 = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
329 ? alloca( pReturnTypeDescr->nSize )
330 : pUnoReturn); // direct way
331 *pPT++ = 'C'; //signify that a complex return type on stack
332 pCppStack += sizeof(void *);
335 // push this
336 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI())
337 + aVtableSlot.offset;
338 *(void**)pCppStack = pAdjustedThisPtr;
339 pCppStack += sizeof( void* );
340 *pPT++ = 'I';
342 // stack space
343 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
344 // args
345 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
346 // indizes of values this have to be converted (interface conversion cpp<=>uno)
347 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
348 // type descriptions for reconversions
349 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
351 sal_Int32 nTempIndizes = 0;
353 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
355 const typelib_MethodParameter & rParam = pParams[nPos];
356 typelib_TypeDescription * pParamTypeDescr = 0;
357 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
359 if (!rParam.bOut
360 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
362 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
363 pThis->getBridge()->getUno2Cpp() );
365 switch (pParamTypeDescr->eTypeClass)
368 // we need to know type of each param so that we know whether to use
369 // gpr or fpr to pass in parameters:
370 // Key: I - int, long, pointer, etc means pass in gpr
371 // B - byte value passed in gpr
372 // S - short value passed in gpr
373 // F - float value pass in fpr
374 // D - double value pass in fpr
375 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
376 // X - indicates end of parameter description string
378 case typelib_TypeClass_LONG:
379 case typelib_TypeClass_UNSIGNED_LONG:
380 case typelib_TypeClass_ENUM:
381 *pPT++ = 'I';
382 break;
383 case typelib_TypeClass_SHORT:
384 case typelib_TypeClass_CHAR:
385 case typelib_TypeClass_UNSIGNED_SHORT:
386 *pPT++ = 'S';
387 break;
388 case typelib_TypeClass_BOOLEAN:
389 case typelib_TypeClass_BYTE:
390 *pPT++ = 'B';
391 break;
392 case typelib_TypeClass_FLOAT:
393 *pPT++ = 'F';
394 break;
395 case typelib_TypeClass_DOUBLE:
396 *pPT++ = 'D';
397 pCppStack += sizeof(sal_Int32); // extra long
398 break;
399 case typelib_TypeClass_HYPER:
400 case typelib_TypeClass_UNSIGNED_HYPER:
401 *pPT++ = 'H';
402 pCppStack += sizeof(sal_Int32); // extra long
403 default:
404 break;
407 // no longer needed
408 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
410 else // ptr to complex value | ref
412 if (! rParam.bIn) // is pure out
414 // cpp out is constructed mem, uno out is not!
415 uno_constructData(
416 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
417 pParamTypeDescr );
418 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
419 // will be released at reconversion
420 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
422 // is in/inout
423 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
425 uno_copyAndConvertData(
426 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
427 pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
429 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
430 // will be released at reconversion
431 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
433 else // direct way
435 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
436 // no longer needed
437 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
439 *pPT++='I';
441 pCppStack += sizeof(sal_Int32); // standard parameter length
444 // terminate the signature string
445 *pPT++='X';
446 *pPT=0;
450 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
451 callVirtualMethod(
452 pAdjustedThisPtr, aVtableSlot.index,
453 pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
454 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
455 // NO exception occured...
456 *ppUnoExc = 0;
458 // reconvert temporary params
459 for ( ; nTempIndizes--; )
461 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
462 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
464 if (pParams[nIndex].bIn)
466 if (pParams[nIndex].bOut) // inout
468 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
469 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
470 pThis->getBridge()->getCpp2Uno() );
473 else // pure out
475 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
476 pThis->getBridge()->getCpp2Uno() );
478 // destroy temp cpp param => cpp: every param was constructed
479 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
481 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
483 // return value
484 if (pCppReturn && pUnoReturn != pCppReturn)
486 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
487 pThis->getBridge()->getCpp2Uno() );
488 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
491 catch (...)
493 // fill uno exception
494 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
496 // temporary params
497 for ( ; nTempIndizes--; )
499 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
500 // destroy temp cpp param => cpp: every param was constructed
501 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
502 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
504 // return type
505 if (pReturnTypeDescr)
506 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
512 namespace bridges { namespace cpp_uno { namespace shared {
514 void unoInterfaceProxyDispatch(
515 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
516 void * pReturn, void * pArgs[], uno_Any ** ppException )
518 // is my surrogate
519 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
520 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * > (pUnoI);
521 // typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
523 switch (pMemberDescr->eTypeClass)
525 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
528 VtableSlot aVtableSlot(
529 getVtableSlot(
530 reinterpret_cast<
531 typelib_InterfaceAttributeTypeDescription const * >(
532 pMemberDescr)));
534 if (pReturn)
536 // dependent dispatch
537 cpp_call(
538 pThis, aVtableSlot,
539 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
540 0, 0, // no params
541 pReturn, pArgs, ppException );
543 else
545 // is SET
546 typelib_MethodParameter aParam;
547 aParam.pTypeRef =
548 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
549 aParam.bIn = sal_True;
550 aParam.bOut = sal_False;
552 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
553 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
554 typelib_typedescriptionreference_new(
555 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
557 // dependent dispatch
558 aVtableSlot.index += 1; //get then set method
559 cpp_call(
560 pThis, aVtableSlot,
561 pReturnTypeRef,
562 1, &aParam,
563 pReturn, pArgs, ppException );
565 typelib_typedescriptionreference_release( pReturnTypeRef );
568 break;
570 case typelib_TypeClass_INTERFACE_METHOD:
573 VtableSlot aVtableSlot(
574 getVtableSlot(
575 reinterpret_cast<
576 typelib_InterfaceMethodTypeDescription const * >(
577 pMemberDescr)));
578 switch (aVtableSlot.index)
580 // standard calls
581 case 1: // acquire uno interface
582 (*pUnoI->acquire)( pUnoI );
583 *ppException = 0;
584 break;
585 case 2: // release uno interface
586 (*pUnoI->release)( pUnoI );
587 *ppException = 0;
588 break;
589 case 0: // queryInterface() opt
591 typelib_TypeDescription * pTD = 0;
592 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
593 if (pTD)
595 uno_Interface * pInterface = 0;
596 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
597 pThis->pBridge->getUnoEnv(),
598 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
600 if (pInterface)
602 ::uno_any_construct(
603 reinterpret_cast< uno_Any * >( pReturn ),
604 &pInterface, pTD, 0 );
605 (*pInterface->release)( pInterface );
606 TYPELIB_DANGER_RELEASE( pTD );
607 *ppException = 0;
608 break;
610 TYPELIB_DANGER_RELEASE( pTD );
612 } // else perform queryInterface()
613 default:
614 // dependent dispatch
615 cpp_call(
616 pThis, aVtableSlot,
617 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
618 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
619 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
620 pReturn, pArgs, ppException );
622 break;
624 default:
626 ::com::sun::star::uno::RuntimeException aExc(
627 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
628 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
630 Type const & rExcType = ::getCppuType( &aExc );
631 // binary identical null reference
632 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
637 } } }