Bump for 3.6-28
[LibreOffice.git] / bridges / source / cpp_uno / gcc3_linux_mips / uno2cpp.cxx
blobef4ede959215864e70f53033589a91b6d68c5f01
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #include <malloc.h>
31 #include <com/sun/star/uno/genfunc.hxx>
32 #include <uno/data.h>
34 #include "bridges/cpp_uno/shared/bridge.hxx"
35 #include "bridges/cpp_uno/shared/types.hxx"
36 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
37 #include "bridges/cpp_uno/shared/vtables.hxx"
39 #include "share.hxx"
41 //#define BRDEBUG
42 #ifdef BRDEBUG
43 #include <stdio.h>
44 #endif
47 using namespace ::rtl;
48 using namespace ::com::sun::star::uno;
50 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 unsigned long * mfunc; // actual function to be invoked
69 void (*ptr)();
70 int gpr[4]; // storage for gpregisters, map to a0-a3
71 int off; // offset used to find function
72 int nw; // number of words mapped
73 long *p; // pointer to parameter overflow area
74 int c; // character of parameter type being decoded
75 int iret, iret2; // temporary function return values
77 // never called
78 if (! pAdjustedThisPtr ) CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something
80 #ifdef BRDEBUG
81 fprintf(stderr,"in CallVirtualMethod\n");
82 #endif
84 // Because of the MIPS O32 calling conventions we could be passing
85 // parameters in both register types and on the stack. To create the
86 // stack parameter area we need we now simply allocate local
87 // variable storage param[] that is at least the size of the parameter stack
88 // (more than enough space) which we can overwrite the parameters into.
90 /* p = sp - 512; new sp will be p - 16, but we don't change sp
91 * at this time to avoid breaking ABI--not sure whether changing sp will break
92 * references to local variables. For the same reason, we use abosulte value.
94 __asm__ __volatile__ (
95 "addiu $2,$29,-512\n\t"
96 "move %0,$2\n\t"
97 :"=r"(p): : "$2","$29" );
99 #ifdef BRDEBUG
100 if (nStackLongs * 4 > 512 )
101 fprintf(stderr,"too many arguments");
102 #endif
104 // now begin to load the C++ function arguments into storage
105 nw = 0;
107 // now we need to parse the entire signature string */
108 // until we get the END indicator */
110 // treat complex return pointer like any other parameter //
112 #ifdef BRDEBUG
113 fprintf(stderr,"overflow area pointer p=%p\n",p);
115 /* Let's figure out what is really going on here*/
116 fprintf(stderr,"callVirtualMethod parameters string is %s\n",pPT);
117 int k = nStackLongs;
118 long * q = (long *)pStackLongs;
119 while (k > 0) {
120 fprintf(stderr,"uno stack is: %x\n",(unsigned int)*q);
121 k--;
122 q++;
124 #endif
126 /* parse the argument list up to the ending ) */
127 while (*pPT != 'X') {
128 c = *pPT;
129 switch (c) {
130 case 'D': /* type is double */
131 /* treat the same as long long */
132 case 'H': /* type is long long */
133 if (nw & 1) nw++; /* note even elements gpr[] will map to
134 odd registers*/
135 if (nw < 4) {
136 gpr[nw++] = *pStackLongs;
137 gpr[nw++] = *(pStackLongs+1);
138 } else {
139 if (((long) p) & 4)
140 p++;
141 *p++ = *pStackLongs;
142 *p++ = *(pStackLongs+1);
144 pStackLongs += 2;
145 break;
147 case 'S':
148 if (nw < 4) {
149 gpr[nw++] = *((unsigned short*)pStackLongs);
150 } else {
151 *p++ = *((unsigned short *)pStackLongs);
153 pStackLongs += 1;
154 break;
156 case 'B':
157 if (nw < 4) {
158 gpr[nw++] = *((char *)pStackLongs);
159 } else {
160 *p++ = *((char *)pStackLongs);
162 pStackLongs += 1;
163 break;
165 default:
166 if (nw < 4) {
167 gpr[nw++] = *pStackLongs;
168 } else {
169 *p++ = *pStackLongs;
171 pStackLongs += 1;
172 break;
174 pPT++;
177 /* figure out the address of the function we need to invoke */
178 off = nVtableIndex;
179 off = off * 4; // 4 bytes per slot
180 mfunc = *((unsigned long **)pAdjustedThisPtr); // get the address of the vtable
181 mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset
182 mfunc = *((unsigned long **)mfunc); // the function is stored at the address
183 ptr = (void (*)())mfunc;
185 #ifdef BRDEBUG
186 fprintf(stderr,"calling function %p\n",mfunc);
187 #endif
189 /* Set up the machine registers and invoke the function */
191 __asm__ __volatile__ (
192 "lw $4, 0(%0)\n\t"
193 "lw $5, 4(%0)\n\t"
194 "lw $6, 8(%0)\n\t"
195 "lw $7, 12(%0)\n\t"
196 : : "r" (gpr)
197 : "$4", "$5", "$6", "$7"
200 __asm__ __volatile__ ("addiu $29,$29,-528\r\n":::"$29");
202 (*ptr)();
204 __asm__ __volatile__ ("addiu $29,$29,528\r\n":::"$29");
206 __asm__ __volatile__ (
207 "sw $2,%0 \n\t"
208 "sw $3,%1 \n\t"
209 : "=m" (iret), "=m" (iret2) : );
210 register float fret asm("$f0");
211 register double dret asm("$f0");
213 switch( eReturnType )
215 case typelib_TypeClass_HYPER:
216 case typelib_TypeClass_UNSIGNED_HYPER:
217 ((long*)pRegisterReturn)[1] = iret2; // fall through
218 case typelib_TypeClass_LONG:
219 case typelib_TypeClass_UNSIGNED_LONG:
220 case typelib_TypeClass_ENUM:
221 ((long*)pRegisterReturn)[0] = iret;
222 break;
223 case typelib_TypeClass_CHAR:
224 case typelib_TypeClass_SHORT:
225 case typelib_TypeClass_UNSIGNED_SHORT:
226 *(unsigned short*)pRegisterReturn = (unsigned short)iret;
227 break;
228 case typelib_TypeClass_BOOLEAN:
229 case typelib_TypeClass_BYTE:
230 *(unsigned char*)pRegisterReturn = (unsigned char)iret;
231 break;
232 case typelib_TypeClass_FLOAT:
233 *(float*)pRegisterReturn = fret;
234 break;
235 case typelib_TypeClass_DOUBLE:
236 *(double*)pRegisterReturn = dret;
237 break;
238 default:
239 break;
244 //==================================================================================================
245 static void cpp_call(
246 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
247 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
248 typelib_TypeDescriptionReference * pReturnTypeRef,
249 sal_Int32 nParams, typelib_MethodParameter * pParams,
250 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
252 // max space for: [complex ret ptr], values|ptr ...
253 char * pCppStack =
254 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
255 char * pCppStackStart = pCppStack;
257 // need to know parameter types for callVirtualMethod so generate a signature string
258 char * pParamType = (char *) alloca(nParams+2);
259 char * pPT = pParamType;
261 #ifdef BRDEBUG
262 fprintf(stderr,"in cpp_call\n");
263 #endif
265 // return
266 typelib_TypeDescription * pReturnTypeDescr = 0;
267 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
268 // OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
270 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
272 if (pReturnTypeDescr)
274 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
276 pCppReturn = pUnoReturn; // direct way for simple types
278 else
280 // complex return via ptr
281 pCppReturn = *(void **)pCppStack =
282 (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
283 ? alloca( pReturnTypeDescr->nSize ): pUnoReturn); // direct way
284 *pPT++ = 'I'; //signify that a complex return type on stack
285 pCppStack += sizeof(void *);
288 // push this
289 void* pAdjustedThisPtr = reinterpret_cast< void **>(pThis->getCppI()) + aVtableSlot.offset;
290 *(void**)pCppStack = pAdjustedThisPtr;
291 pCppStack += sizeof( void* );
292 *pPT++ = 'I';
294 // stack space
295 // OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
296 // args
297 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
298 // indizes of values this have to be converted (interface conversion cpp<=>uno)
299 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
300 // type descriptions for reconversions
301 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
303 sal_Int32 nTempIndizes = 0;
305 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
307 const typelib_MethodParameter & rParam = pParams[nPos];
308 typelib_TypeDescription * pParamTypeDescr = 0;
309 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
311 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
313 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
314 pThis->getBridge()->getUno2Cpp() );
316 switch (pParamTypeDescr->eTypeClass)
319 // we need to know type of each param so that we know whether to use
320 // gpr or fpr to pass in parameters:
321 // Key: I - int, long, pointer, etc means pass in gpr
322 // B - byte value passed in gpr
323 // S - short value passed in gpr
324 // F - float value pass in fpr
325 // D - double value pass in fpr
326 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
327 // X - indicates end of parameter description string
329 case typelib_TypeClass_LONG:
330 case typelib_TypeClass_UNSIGNED_LONG:
331 case typelib_TypeClass_ENUM:
332 *pPT++ = 'I';
333 break;
334 case typelib_TypeClass_SHORT:
335 case typelib_TypeClass_CHAR:
336 case typelib_TypeClass_UNSIGNED_SHORT:
337 *pPT++ = 'S';
338 break;
339 case typelib_TypeClass_BOOLEAN:
340 case typelib_TypeClass_BYTE:
341 *pPT++ = 'B';
342 break;
343 case typelib_TypeClass_FLOAT:
344 *pPT++ = 'F';
345 break;
346 case typelib_TypeClass_DOUBLE:
347 *pPT++ = 'D';
348 pCppStack += sizeof(sal_Int32); // extra long
349 break;
350 case typelib_TypeClass_HYPER:
351 case typelib_TypeClass_UNSIGNED_HYPER:
352 *pPT++ = 'H';
353 pCppStack += sizeof(sal_Int32); // extra long
354 break;
355 default:
356 break;
359 // no longer needed
360 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
362 else // ptr to complex value | ref
364 if (! rParam.bIn) // is pure out
366 // cpp out is constructed mem, uno out is not!
367 uno_constructData(
368 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
369 pParamTypeDescr );
370 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
371 // will be released at reconversion
372 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
374 // is in/inout
375 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
377 uno_copyAndConvertData(
378 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
379 pUnoArgs[nPos], pParamTypeDescr,
380 pThis->getBridge()->getUno2Cpp() );
382 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
383 // will be released at reconversion
384 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
386 else // direct way
388 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
389 // no longer needed
390 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
392 // KBH: FIXME: is this the right way to pass these
393 *pPT++='I';
395 pCppStack += sizeof(sal_Int32); // standard parameter length
398 // terminate the signature string
399 *pPT++='X';
400 *pPT=0;
404 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
405 callVirtualMethod(
406 pAdjustedThisPtr, aVtableSlot.index,
407 pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
408 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
409 // NO exception occurred...
410 *ppUnoExc = 0;
412 // reconvert temporary params
413 for ( ; nTempIndizes--; )
415 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
416 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
418 if (pParams[nIndex].bIn)
420 if (pParams[nIndex].bOut) // inout
422 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
423 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
424 pThis->getBridge()->getCpp2Uno() );
427 else // pure out
429 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
430 pThis->getBridge()->getCpp2Uno() );
432 // destroy temp cpp param => cpp: every param was constructed
433 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
435 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
437 // return value
438 if (pCppReturn && pUnoReturn != pCppReturn)
440 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
441 pThis->getBridge()->getCpp2Uno() );
442 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
445 catch (...)
447 // fill uno exception
448 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
449 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
451 // temporary params
452 for ( ; nTempIndizes--; )
454 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
455 // destroy temp cpp param => cpp: every param was constructed
456 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
457 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
459 // return type
460 if (pReturnTypeDescr)
461 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
468 namespace bridges { namespace cpp_uno { namespace shared {
470 //==================================================================================================
471 void unoInterfaceProxyDispatch(
472 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
473 void * pReturn, void * pArgs[], uno_Any ** ppException )
475 // is my surrogate
476 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis
477 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy *> (pUnoI);
478 //typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr;
480 #ifdef BRDEBUG
481 fprintf(stderr,"in dispatch\n");
482 #endif
484 switch (pMemberDescr->eTypeClass)
486 case typelib_TypeClass_INTERFACE_ATTRIBUTE:
489 VtableSlot aVtableSlot(
490 getVtableSlot(
491 reinterpret_cast<
492 typelib_InterfaceAttributeTypeDescription const * >(
493 pMemberDescr)));
495 if (pReturn)
497 // dependent dispatch
498 cpp_call(
499 pThis, aVtableSlot,
500 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef,
501 0, 0, // no params
502 pReturn, pArgs, ppException );
504 else
506 // is SET
507 typelib_MethodParameter aParam;
508 aParam.pTypeRef =
509 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef;
510 aParam.bIn = sal_True;
511 aParam.bOut = sal_False;
513 typelib_TypeDescriptionReference * pReturnTypeRef = 0;
514 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") );
515 typelib_typedescriptionreference_new(
516 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData );
518 // dependent dispatch
519 aVtableSlot.index += 1; //get then set method
520 cpp_call(
521 pThis, aVtableSlot,
522 pReturnTypeRef,
523 1, &aParam,
524 pReturn, pArgs, ppException );
526 typelib_typedescriptionreference_release( pReturnTypeRef );
529 break;
531 case typelib_TypeClass_INTERFACE_METHOD:
534 VtableSlot aVtableSlot(
535 getVtableSlot(
536 reinterpret_cast<
537 typelib_InterfaceMethodTypeDescription const * >(
538 pMemberDescr)));
539 switch (aVtableSlot.index)
541 // standard calls
542 case 1: // acquire uno interface
543 (*pUnoI->acquire)( pUnoI );
544 *ppException = 0;
545 break;
546 case 2: // release uno interface
547 (*pUnoI->release)( pUnoI );
548 *ppException = 0;
549 break;
550 case 0: // queryInterface() opt
552 typelib_TypeDescription * pTD = 0;
553 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() );
554 if (pTD)
556 uno_Interface * pInterface = 0;
557 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)(
558 pThis->pBridge->getUnoEnv(),
559 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD );
561 if (pInterface)
563 ::uno_any_construct(
564 reinterpret_cast< uno_Any * >( pReturn ),
565 &pInterface, pTD, 0 );
566 (*pInterface->release)( pInterface );
567 TYPELIB_DANGER_RELEASE( pTD );
568 *ppException = 0;
569 break;
571 TYPELIB_DANGER_RELEASE( pTD );
573 } // else perform queryInterface()
574 default:
575 // dependent dispatch
576 cpp_call(
577 pThis, aVtableSlot,
578 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef,
579 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams,
580 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams,
581 pReturn, pArgs, ppException );
583 break;
585 default:
587 ::com::sun::star::uno::RuntimeException aExc(
588 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ),
589 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() );
591 Type const & rExcType = ::getCppuType( &aExc );
592 // binary identical null reference
593 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 );
599 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */