Update ooo320-m1
[ooovba.git] / bridges / source / cpp_uno / gcc3_linux_s390 / uno2cpp.cxx
blobf0d4145f69075807ec430f2b13d0e80ef39ab72d
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.6 $
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 <malloc.h>
36 #include <com/sun/star/uno/genfunc.hxx>
37 #include <uno/data.h>
39 #include "bridges/cpp_uno/shared/bridge.hxx"
40 #include "bridges/cpp_uno/shared/types.hxx"
41 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx"
42 #include "bridges/cpp_uno/shared/vtables.hxx"
44 #include "share.hxx"
46 #include <stdio.h>
47 #include <string.h>
50 using namespace ::rtl;
51 using namespace ::com::sun::star::uno;
53 namespace
55 static sal_Int32
56 invoke_count_words(char * pPT)
58 sal_Int32 overflow = 0, gpr = 0, fpr = 0;
59 int c; // character of parameter type being decoded
61 while (*pPT != 'X') {
62 c = *pPT;
63 switch (c) {
64 case 'D': /* type is double */
65 if (fpr < 2) fpr++; else overflow+=2;
66 break;
68 case 'F': /* type is float */
69 if (fpr < 2) fpr++; else overflow++;
70 break;
72 case 'H': /* type is long long */
73 if (gpr < 4) gpr+=2; else gpr=5, overflow+=2;
74 break;
76 case 'S':
77 case 'T':
78 case 'B':
79 case 'C':
80 if (gpr < 5) gpr++; else overflow++;
81 break;
83 default:
84 if (gpr < 5) gpr++; else overflow++;
85 break;
87 pPT++;
89 /* Round up number of overflow words to ensure stack
90 stays aligned to 8 bytes. */
91 return (overflow + 1) & ~1;
94 static void
95 //invoke_copy_to_stack(sal_Int32 paramCount, sal_Int32 * pStackLongs, char * pPT, sal_Int32* d_ov, sal_Int32 overflow)
96 invoke_copy_to_stack(sal_Int32 * pStackLongs, char * pPT, sal_Int32* d_ov, sal_Int32 overflow)
98 sal_Int32 *d_gpr = d_ov + overflow;
99 sal_Int64 *d_fpr = (sal_Int64 *)(d_gpr + 5);
100 sal_Int32 gpr = 0, fpr = 0;
101 char c;
103 while (*pPT != 'X') {
104 c = *pPT;
105 switch (c) {
106 case 'D': /* type is double */
107 if (fpr < 2)
108 *((double*) d_fpr) = *((double *)pStackLongs), d_fpr++, fpr++;
109 else
110 *((double*) d_ov ) = *((double *)pStackLongs), d_ov+=2;
112 pStackLongs += 2;
113 break;
115 case 'F': /* type is float */
116 if (fpr < 2) {
117 *((sal_Int64*) d_fpr) = 0;
118 *((float*) d_fpr) = *((float *)pStackLongs), d_fpr++, fpr++;
120 else {
121 *((sal_Int64*) d_ov) = 0;
122 *((float*) d_ov ) = *((float *)pStackLongs), d_ov++;
125 pStackLongs += 1;
126 break;
128 case 'H': /* type is long long */
129 if (gpr < 4) {
130 *((sal_Int64*) d_gpr) = *((sal_Int64*) pStackLongs), d_gpr+=2, gpr+=2;
132 else {
133 *((sal_Int64*) d_ov ) = *((sal_Int64*) pStackLongs), d_ov+=2, gpr=5;
135 pStackLongs += 2;
136 break;
138 case 'S':
139 if (gpr < 5)
140 *((sal_uInt32*)d_gpr) = *((unsigned short*)pStackLongs), d_gpr++, gpr++;
141 else
142 *((sal_uInt32*)d_ov ) = *((unsigned short*)pStackLongs), d_ov++;
143 pStackLongs += 1;
144 break;
146 case 'T':
147 if (gpr < 5)
148 *((sal_Int32*)d_gpr) = *((signed short*)pStackLongs), d_gpr++, gpr++;
149 else
150 *((sal_Int32*)d_ov ) = *((signed short*)pStackLongs), d_ov++;
151 pStackLongs += 1;
152 break;
154 case 'B':
155 if (gpr < 5)
156 *((sal_uInt32*)d_gpr) = *((unsigned char*)pStackLongs), d_gpr++, gpr++;
157 else
158 *((sal_uInt32*)d_ov ) = *((unsigned char*)pStackLongs), d_ov++;
159 pStackLongs += 1;
160 break;
162 case 'C':
163 if (gpr < 5)
164 *((sal_Int32*)d_gpr) = *((signed char*)pStackLongs), d_gpr++, gpr++;
165 else
166 *((sal_Int32*)d_ov ) = *((signed char*)pStackLongs), d_ov++;
167 pStackLongs += 1;
168 break;
170 default:
171 if (gpr < 5)
172 *((sal_Int32*)d_gpr) = *pStackLongs, d_gpr++, gpr++;
173 else
174 *((sal_Int32*)d_ov ) = *pStackLongs, d_ov++;
175 pStackLongs += 1;
176 break;
178 pPT++;
182 //==================================================================================================
183 static void callVirtualMethod(
184 void * pThis,
185 sal_Int32 nVtableIndex,
186 void * pRegisterReturn,
187 typelib_TypeClass eReturnType,
188 char * pPT,
189 sal_Int32 * pStackLongs,
190 sal_Int32 nStackLongs)
193 // parameter list is mixed list of * and values
194 // reference parameters are pointers
196 // the basic idea here is to use gpr[5] as a storage area for
197 // the future values of registers r2 to r6 needed for the call,
198 // and similarly fpr[2] as a storage area for the future values
199 // of floating point registers f0 to f2
201 sal_Int32 *vtable = *(sal_Int32 **)pThis;
202 // sal_Int32 method = vtable[nVtableIndex + 2];
203 sal_Int32 method = vtable[nVtableIndex];
204 sal_Int32 overflow = invoke_count_words (pPT);
205 sal_Int32 result;
206 volatile double dret; // temporary function return values
207 volatile float fret;
208 volatile int iret, iret2;
210 void * dummy = alloca(32); // dummy alloca to force r11 usage for exception handling
212 __asm__ __volatile__
214 "lr 7,15\n\t"
215 "ahi 7,-48\n\t"
217 "lr 3,%2\n\t"
218 "sll 3,2\n\t"
219 "lcr 3,3\n\t"
220 "l 2,0(15)\n\t"
221 "la 15,0(3,7)\n\t"
222 "st 2,0(15)\n\t"
224 "lr 2,%0\n\t"
225 "lr 3,%1\n\t"
226 "la 4,96(15)\n\t"
227 "lr 5,%2\n\t"
228 "basr 14,%3\n\t"
230 "ld 0,116(7)\n\t"
231 "ld 2,124(7)\n\t"
232 "lm 2,6,96(7)\n\t"
234 : "r" (pStackLongs),
235 "r" (pPT),
236 "r" (overflow),
237 "a" (invoke_copy_to_stack),
238 "a" (method),
239 "X" (dummy)
240 : "2", "3", "4", "5", "6", "7", "memory"
242 // "basr 14,%8\n\t"
244 (*(void (*)())method)();
246 __asm__ __volatile__
248 "la 15,48(7)\n\t"
250 "lr %2,2\n\t"
251 "lr %3,3\n\t"
252 "ler %0,0\n\t"
253 "ldr %1,0\n\t"
255 : "=f" (fret), "=f" (dret), "=r" (iret), "=r" (iret2)
258 switch( eReturnType )
260 case typelib_TypeClass_HYPER:
261 case typelib_TypeClass_UNSIGNED_HYPER:
262 // ((long*)pRegisterReturn)[0] = iret;
263 ((long*)pRegisterReturn)[1] = iret2;
264 case typelib_TypeClass_LONG:
265 case typelib_TypeClass_UNSIGNED_LONG:
266 case typelib_TypeClass_ENUM:
267 ((long*)pRegisterReturn)[0] = iret;
268 break;
269 case typelib_TypeClass_CHAR:
270 case typelib_TypeClass_SHORT:
271 case typelib_TypeClass_UNSIGNED_SHORT:
272 *(unsigned short*)pRegisterReturn = (unsigned short)iret;
273 break;
274 case typelib_TypeClass_BOOLEAN:
275 case typelib_TypeClass_BYTE:
276 *(unsigned char*)pRegisterReturn = (unsigned char)iret;
277 break;
278 case typelib_TypeClass_FLOAT:
279 *(float*)pRegisterReturn = fret;
280 break;
281 case typelib_TypeClass_DOUBLE:
282 *(double*)pRegisterReturn = dret;
283 break;
288 //============================================================================
289 static void cpp_call(
290 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis,
291 bridges::cpp_uno::shared::VtableSlot aVtableSlot,
292 typelib_TypeDescriptionReference * pReturnTypeRef,
293 sal_Int32 nParams, typelib_MethodParameter * pParams,
294 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc )
296 // max space for: [complex ret ptr], values|ptr ...
297 char * pCppStack =
298 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) );
299 char * pCppStackStart = pCppStack;
301 // need to know parameter types for callVirtualMethod so generate a signature string
302 char * pParamType = (char *) alloca(nParams+2);
303 char * pPT = pParamType;
305 // return
306 typelib_TypeDescription * pReturnTypeDescr = 0;
307 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
308 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" );
310 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion
312 if (pReturnTypeDescr)
314 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
316 pCppReturn = pUnoReturn; // direct way for simple types
318 else
320 // complex return via ptr
321 pCppReturn = *(void **)pCppStack = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
322 ? alloca( pReturnTypeDescr->nSize )
323 : pUnoReturn); // direct way
324 *pPT++ = 'I'; //signify that a complex return type on stack
325 pCppStack += sizeof(void *);
328 // push "this" pointer
329 void * pAdjustedThisPtr = reinterpret_cast< void ** >( pThis->getCppI() ) + aVtableSlot.offset;
330 *(void**)pCppStack = pAdjustedThisPtr;
331 pCppStack += sizeof( void* );
332 *pPT++ = 'I';
334 // stack space
335 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" );
336 // args
337 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams );
338 // indizes of values this have to be converted (interface conversion cpp<=>uno)
339 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams);
340 // type descriptions for reconversions
341 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams));
343 sal_Int32 nTempIndizes = 0;
345 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
347 const typelib_MethodParameter & rParam = pParams[nPos];
348 typelib_TypeDescription * pParamTypeDescr = 0;
349 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
351 if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
353 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr,
354 pThis->getBridge()->getUno2Cpp() );
356 switch (pParamTypeDescr->eTypeClass)
359 // we need to know type of each param so that we know whether to use
360 // gpr or fpr to pass in parameters:
361 // Key: I - int, long, pointer, etc means pass in gpr
362 // B - byte value passed in gpr
363 // S - short value passed in gpr
364 // F - float value pass in fpr
365 // D - double value pass in fpr
366 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc
367 // X - indicates end of parameter description string
369 case typelib_TypeClass_LONG:
370 case typelib_TypeClass_UNSIGNED_LONG:
371 case typelib_TypeClass_ENUM:
372 *pPT++ = 'I';
373 break;
374 case typelib_TypeClass_SHORT:
375 *pPT++ = 'T';
376 break;
377 case typelib_TypeClass_CHAR:
378 case typelib_TypeClass_UNSIGNED_SHORT:
379 *pPT++ = 'S';
380 break;
381 case typelib_TypeClass_BOOLEAN:
382 *pPT++ = 'B';
383 break;
384 case typelib_TypeClass_BYTE:
385 *pPT++ = 'C';
386 break;
387 case typelib_TypeClass_FLOAT:
388 *pPT++ = 'F';
389 break;
390 case typelib_TypeClass_DOUBLE:
391 *pPT++ = 'D';
392 pCppStack += sizeof(sal_Int32); // extra long
393 break;
394 case typelib_TypeClass_HYPER:
395 case typelib_TypeClass_UNSIGNED_HYPER:
396 *pPT++ = 'H';
397 pCppStack += sizeof(sal_Int32); // extra long
400 // no longer needed
401 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
403 else // ptr to complex value | ref
405 if (! rParam.bIn) // is pure out
407 // cpp out is constructed mem, uno out is not!
408 uno_constructData(
409 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
410 pParamTypeDescr );
411 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call
412 // will be released at reconversion
413 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
415 // is in/inout
416 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
418 uno_copyAndConvertData(
419 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ),
420 pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() );
422 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
423 // will be released at reconversion
424 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
426 else // direct way
428 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos];
429 // no longer needed
430 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
432 // KBH: FIXME: is this the right way to pass these
433 *pPT++='I';
435 pCppStack += sizeof(sal_Int32); // standard parameter length
438 // terminate the signature string
439 *pPT++='X';
440 *pPT=0;
444 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" );
445 callVirtualMethod(
446 pAdjustedThisPtr, aVtableSlot.index,
447 pCppReturn, pReturnTypeDescr->eTypeClass, pParamType,
448 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) );
449 // NO exception occured...
450 *ppUnoExc = 0;
452 // reconvert temporary params
453 for ( ; nTempIndizes--; )
455 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
456 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
458 if (pParams[nIndex].bIn)
460 if (pParams[nIndex].bOut) // inout
462 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value
463 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
464 pThis->getBridge()->getCpp2Uno() );
467 else // pure out
469 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr,
470 pThis->getBridge()->getCpp2Uno() );
472 // destroy temp cpp param => cpp: every param was constructed
473 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
475 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
477 // return value
478 if (pCppReturn && pUnoReturn != pCppReturn)
480 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr,
481 pThis->getBridge()->getCpp2Uno() );
482 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release );
485 catch (...)
487 // fill uno exception
488 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions,
489 *ppUnoExc, pThis->getBridge()->getCpp2Uno() );
492 // temporary params
493 for ( ; nTempIndizes--; )
495 sal_Int32 nIndex = pTempIndizes[nTempIndizes];
496 // destroy temp cpp param => cpp: every param was constructed
497 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release );
498 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
500 // return type
501 if (pReturnTypeDescr)
502 TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
507 namespace bridges { namespace cpp_uno { namespace shared {
509 void unoInterfaceProxyDispatch(
510 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr,
511 void * pReturn, void * pArgs[], uno_Any ** ppException )
513 #ifdef CMC_DEBUG
514 fprintf(stderr, "unoInterfaceProxyDispatch\n");
515 #endif
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 } } }
638 /* vi:set tabstop=4 shiftwidth=4 expandtab: */