merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / cpp_uno / gcc3_linux_mips / uno2cpp.cxx
blob1888803e34fa9b89794a4a0fbd9168beaf9e34bf
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.4 $
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 <malloc.h>
33 #include <com/sun/star/uno/genfunc.hxx>
34 #include <uno/data.h>
36 #include "bridges/cpp_uno/shared/bridge.hxx"
37 #include "bridges/cpp_uno/shared/types.hxx"
38 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
39 #include "bridges/cpp_uno/shared/vtables.hxx"
41 #include "share.hxx"
43 //#define BRDEBUG
44 #ifdef BRDEBUG
45 #include <stdio.h>
46 #endif
49 using namespace ::rtl;
50 using namespace ::com::sun::star::uno;
52 namespace
56 //==================================================================================================
57 static void callVirtualMethod(
58 void * pAdjustedThisPtr,
59 sal_Int32 nVtableIndex,
60 void * pRegisterReturn,
61 typelib_TypeClass eReturnType,
62 char * pPT,
63 sal_Int32 * pStackLongs,
64 sal_Int32 /*nStackLongs*/)
67 // parameter list is mixed list of * and values
68 // reference parameters are pointers
70 unsigned long * mfunc; // actual function to be invoked
71 void (*ptr)();
72 int gpr[4]; // storage for gpregisters, map to a0-a3
73 int off; // offset used to find function
74 int nw; // number of words mapped
75 long *p; // pointer to parameter overflow area
76 int c; // character of parameter type being decoded
77 int iret, iret2; // temporary function return values
79 // never called
80 if (! pAdjustedThisPtr ) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
82 #ifdef BRDEBUG
83 fprintf(stderr,"in CallVirtualMethod\n");
84 #endif
86 // Because of the MIPS O32 calling conventions we could be passing
87 // parameters in both register types and on the stack. To create the
88 // stack parameter area we need we now simply allocate local
89 // variable storage param[] that is at least the size of the parameter stack
90 // (more than enough space) which we can overwrite the parameters into.
92 /* p = sp - 512; new sp will be p - 16, but we don't change sp
93 * at this time to avoid breaking ABI--not sure whether changing sp will break
94 * references to local variables. For the same reason, we use abosulte value.
96 __asm__ __volatile__ (
97 "addiu $2,$29,-512\n\t"
98 "move %0,$2\n\t"
99 :"=r"(p): : "$2","$29" );
101 #ifdef BRDEBUG
102 if (nStackLongs * 4 > 512 )
103 fprintf(stderr,"too many arguments");
104 #endif
106 // now begin to load the C++ function arguments into storage
107 nw = 0;
109 // now we need to parse the entire signature string */
110 // until we get the END indicator */
112 // treat complex return pointer like any other parameter //
114 #ifdef BRDEBUG
115 fprintf(stderr,"overflow area pointer p=%p\n",p);
117 /* Let's figure out what is really going on here*/
118 fprintf(stderr,"callVirtualMethod paramters string is %s\n",pPT);
119 int k = nStackLongs;
120 long * q = (long *)pStackLongs;
121 while (k > 0) {
122 fprintf(stderr,"uno stack is: %x\n",(unsigned int)*q);
123 k--;
124 q++;
126 #endif
128 /* parse the argument list up to the ending ) */
129 while (*pPT != 'X') {
130 c = *pPT;
131 switch (c) {
132 case 'D': /* type is double */
133 /* treat the same as long long */
134 case 'H': /* type is long long */
135 if (nw & 1) nw++; /* note even elements gpr[] will map to
136 odd registers*/
137 if (nw < 4) {
138 gpr[nw++] = *pStackLongs;
139 gpr[nw++] = *(pStackLongs+1);
140 } else {
141 if (((long) p) & 4)
142 p++;
143 *p++ = *pStackLongs;
144 *p++ = *(pStackLongs+1);
146 pStackLongs += 2;
147 break;
149 case 'S':
150 if (nw < 4) {
151 gpr[nw++] = *((unsigned short*)pStackLongs);
152 } else {
153 *p++ = *((unsigned short *)pStackLongs);
155 pStackLongs += 1;
156 break;
158 case 'B':
159 if (nw < 4) {
160 gpr[nw++] = *((char *)pStackLongs);
161 } else {
162 *p++ = *((char *)pStackLongs);
164 pStackLongs += 1;
165 break;
167 default:
168 if (nw < 4) {
169 gpr[nw++] = *pStackLongs;
170 } else {
171 *p++ = *pStackLongs;
173 pStackLongs += 1;
174 break;
176 pPT++;
179 /* figure out the address of the function we need to invoke */
180 off = nVtableIndex;
181 off = off * 4; // 4 bytes per slot
182 mfunc = *((unsigned long **)pAdjustedThisPtr); // get the address of the vtable
183 mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
184 mfunc = *((unsigned long **)mfunc); // the function is stored at the address
185 ptr = (void (*)())mfunc;
187 #ifdef BRDEBUG
188 fprintf(stderr,"calling function %p\n",mfunc);
189 #endif
191 /* Set up the machine registers and invoke the function */
193 __asm__ __volatile__ (
194 "lw $4, 0(%0)\n\t"
195 "lw $5, 4(%0)\n\t"
196 "lw $6, 8(%0)\n\t"
197 "lw $7, 12(%0)\n\t"
198 : : "r" (gpr)
199 : "$4", "$5", "$6", "$7"
202 __asm__ __volatile__ ("addiu $29,$29,-528\r\n":::"$29");
204 (*ptr)();
206 __asm__ __volatile__ ("addiu $29,$29,528\r\n":::"$29");
208 __asm__ __volatile__ (
209 "sw $2,%0 \n\t"
210 "sw $3,%1 \n\t"
211 : "=m" (iret), "=m" (iret2) : );
212 register float fret asm("$f0");
213 register double dret asm("$f0");
215 switch( eReturnType )
217 case typelib_TypeClass_HYPER:
218 case typelib_TypeClass_UNSIGNED_HYPER:
219 ((long*)pRegisterReturn)[1] = iret2; // fall through
220 case typelib_TypeClass_LONG:
221 case typelib_TypeClass_UNSIGNED_LONG:
222 case typelib_TypeClass_ENUM:
223 ((long*)pRegisterReturn)[0] = iret;
224 break;
225 case typelib_TypeClass_CHAR:
226 case typelib_TypeClass_SHORT:
227 case typelib_TypeClass_UNSIGNED_SHORT:
228 *(unsigned short*)pRegisterReturn = (unsigned short)iret;
229 break;
230 case typelib_TypeClass_BOOLEAN:
231 case typelib_TypeClass_BYTE:
232 *(unsigned char*)pRegisterReturn = (unsigned char)iret;
233 break;
234 case typelib_TypeClass_FLOAT:
235 *(float*)pRegisterReturn = fret;
236 break;
237 case typelib_TypeClass_DOUBLE:
238 *(double*)pRegisterReturn = dret;
239 break;
240 default:
241 break;
246 //==================================================================================================
247 static void cpp_call(
248 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
249 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
250 typelib_TypeDescriptionReference * pReturnTypeRef,
251 sal_Int32 nParams, typelib_MethodParameter * pParams,
252 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
254 // max space for: [complex ret ptr], values|ptr ...
255 char * pCppStack =
256 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
257 char * pCppStackStart = pCppStack;
259 // need to know parameter types for callVirtualMethod so generate a signature string
260 char * pParamType = (char *) alloca(nParams+2);
261 char * pPT = pParamType;
263 #ifdef BRDEBUG
264 fprintf(stderr,"in cpp_call\n");
265 #endif
267 // return
268 typelib_TypeDescription * pReturnTypeDescr = 0;
269 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
270 // OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
272 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
274 if (pReturnTypeDescr)
276 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
278 pCppReturn = pUnoReturn; // direct way for simple types
280 else
282 // complex return via ptr
283 pCppReturn = *(void **)pCppStack =
284 (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
285 ? alloca( pReturnTypeDescr->nSize ): pUnoReturn); // direct way
286 *pPT++ = 'I'; //signify that a complex return type on stack
287 pCppStack += sizeof(void *);
290 // push this
291 void* pAdjustedThisPtr = reinterpret_cast< void **>(pThis->getCppI()) + aVtableSlot.offset;
292 *(void**)pCppStack = pAdjustedThisPtr;
293 pCppStack += sizeof( void* );
294 *pPT++ = 'I';
296 // stack space
297 // OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
298 // args
299 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
300 // indizes of values this have to be converted (interface conversion cpp<=>uno)
301 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
302 // type descriptions for reconversions
303 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
305 sal_Int32 nTempIndizes = 0;
307 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
309 const typelib_MethodParameter & rParam = pParams[nPos];
310 typelib_TypeDescription * pParamTypeDescr = 0;
311 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
313 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
315 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
316 pThis->getBridge()->getUno2Cpp() );
318 switch (pParamTypeDescr->eTypeClass)
321 // we need to know type of each param so that we know whether to use
322 // gpr or fpr to pass in parameters:
323 // Key: I - int, long, pointer, etc means pass in gpr
324 // B - byte value passed in gpr
325 // S - short value passed in gpr
326 // F - float value pass in fpr
327 // D - double value pass in fpr
328 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
329 // X - indicates end of parameter description string
331 case typelib_TypeClass_LONG:
332 case typelib_TypeClass_UNSIGNED_LONG:
333 case typelib_TypeClass_ENUM:
334 *pPT++ = 'I';
335 break;
336 case typelib_TypeClass_SHORT:
337 case typelib_TypeClass_CHAR:
338 case typelib_TypeClass_UNSIGNED_SHORT:
339 *pPT++ = 'S';
340 break;
341 case typelib_TypeClass_BOOLEAN:
342 case typelib_TypeClass_BYTE:
343 *pPT++ = 'B';
344 break;
345 case typelib_TypeClass_FLOAT:
346 *pPT++ = 'F';
347 break;
348 case typelib_TypeClass_DOUBLE:
349 *pPT++ = 'D';
350 pCppStack += sizeof(sal_Int32); // extra long
351 break;
352 case typelib_TypeClass_HYPER:
353 case typelib_TypeClass_UNSIGNED_HYPER:
354 *pPT++ = 'H';
355 pCppStack += sizeof(sal_Int32); // extra long
356 break;
357 default:
358 break;
361 // no longer needed
362 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
364 else // ptr to complex value | ref
366 if (! rParam.bIn) // is pure out
368 // cpp out is constructed mem, uno out is not!
369 uno_constructData(
370 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
371 pParamTypeDescr );
372 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
373 // will be released at reconversion
374 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
376 // is in/inout
377 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
379 uno_copyAndConvertData(
380 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
381 pUnoArgs[nPos], pParamTypeDescr,
382 pThis->getBridge()->getUno2Cpp() );
384 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
385 // will be released at reconversion
386 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
388 else // direct way
390 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
391 // no longer needed
392 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
394 // KBH: FIXME: is this the right way to pass these
395 *pPT++='I';
397 pCppStack += sizeof(sal_Int32); // standard parameter length
400 // terminate the signature string
401 *pPT++='X';
402 *pPT=0;
406 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
407 callVirtualMethod(
408 pAdjustedThisPtr, aVtableSlot.index,
409 pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
410 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
411 // NO exception occured...
412 *ppUnoExc = 0;
414 // reconvert temporary params
415 for ( ; nTempIndizes--; )
417 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
418 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
420 if (pParams[nIndex].bIn)
422 if (pParams[nIndex].bOut) // inout
424 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
425 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
426 pThis->getBridge()->getCpp2Uno() );
429 else // pure out
431 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
432 pThis->getBridge()->getCpp2Uno() );
434 // destroy temp cpp param => cpp: every param was constructed
435 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
437 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
439 // return value
440 if (pCppReturn && pUnoReturn != pCppReturn)
442 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
443 pThis->getBridge()->getCpp2Uno() );
444 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
447 catch (...)
449 // fill uno exception
450 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
451 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
453 // temporary params
454 for ( ; nTempIndizes--; )
456 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
457 // destroy temp cpp param => cpp: every param was constructed
458 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
459 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
461 // return type
462 if (pReturnTypeDescr)
463 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
470 namespace bridges { namespace cpp_uno { namespace shared {
472 //==================================================================================================
473 void unoInterfaceProxyDispatch(
474 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
475 void * pReturn, void * pArgs[], uno_Any ** ppException )
477 // is my surrogate
478 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
479 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
480 //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
482 #ifdef BRDEBUG
483 fprintf(stderr,"in dispatch\n");
484 #endif
486 switch (pMemberDescr->eTypeClass)
488 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
491 VtableSlot aVtableSlot(
492 getVtableSlot(
493 reinterpret_cast<
494 typelib_InterfaceAttributeTypeDescription const * >(
495 pMemberDescr)));
497 if (pReturn)
499 // dependent dispatch
500 cpp_call(
501 pThis, aVtableSlot,
502 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
503 0, 0, // no params
504 pReturn, pArgs, ppException );
506 else
508 // is SET
509 typelib_MethodParameter aParam;
510 aParam.pTypeRef =
511 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
512 aParam.bIn = sal_True;
513 aParam.bOut = sal_False;
515 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
516 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
517 typelib_typedescriptionreference_new(
518 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
520 // dependent dispatch
521 aVtableSlot.index += 1; //get then set method
522 cpp_call(
523 pThis, aVtableSlot,
524 pReturnTypeRef,
525 1, &aParam,
526 pReturn, pArgs, ppException );
528 typelib_typedescriptionreference_release( pReturnTypeRef );
531 break;
533 case typelib_TypeClass_INTERFACE_METHOD:
536 VtableSlot aVtableSlot(
537 getVtableSlot(
538 reinterpret_cast<
539 typelib_InterfaceMethodTypeDescription const * >(
540 pMemberDescr)));
541 switch (aVtableSlot.index)
543 // standard calls
544 case 1: // acquire uno interface
545 (*pUnoI->acquire)( pUnoI );
546 *ppException = 0;
547 break;
548 case 2: // release uno interface
549 (*pUnoI->release)( pUnoI );
550 *ppException = 0;
551 break;
552 case 0: // queryInterface() opt
554 typelib_TypeDescription * pTD = 0;
555 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
556 if (pTD)
558 uno_Interface * pInterface = 0;
559 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
560 pThis->pBridge->getUnoEnv(),
561 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
563 if (pInterface)
565 ::uno_any_construct(
566 reinterpret_cast< uno_Any * >( pReturn ),
567 &pInterface, pTD, 0 );
568 (*pInterface->release)( pInterface );
569 TYPELIB_DANGER_RELEASE( pTD );
570 *ppException = 0;
571 break;
573 TYPELIB_DANGER_RELEASE( pTD );
575 } // else perform queryInterface()
576 default:
577 // dependent dispatch
578 cpp_call(
579 pThis, aVtableSlot,
580 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
581 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
582 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
583 pReturn, pArgs, ppException );
585 break;
587 default:
589 ::com::sun::star::uno::RuntimeException aExc(
590 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
591 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
593 Type const & rExcType = ::getCppuType( &aExc );
594 // binary identical null reference
595 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );