1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 // MARKER(update_precomp.py): autogen include statement, do not remove 29 #include "precompiled_bridges.hxx" 30 31 #include <sys/types.h> 32 #include <sys/malloc.h> 33 34 #include <com/sun/star/uno/genfunc.hxx> 35 #include <uno/data.h> 36 37 #include "bridges/cpp_uno/shared/bridge.hxx" 38 #include "bridges/cpp_uno/shared/types.hxx" 39 #include "bridges/cpp_uno/shared/unointerfaceproxy.hxx" 40 #include "bridges/cpp_uno/shared/vtables.hxx" 41 42 #include "share.hxx" 43 44 45 using namespace ::rtl; 46 using namespace ::com::sun::star::uno; 47 48 namespace 49 { 50 51 //================================================================================================== 52 static void callVirtualMethod( 53 void * pAdjustedThisPtr, 54 sal_Int32 nVtableIndex, 55 void * pRegisterReturn, 56 typelib_TypeClass eReturnType, 57 char * pPT, 58 sal_Int32 * pStackLongs, 59 sal_Int32 /* nStackLongs */) 60 { 61 62 // parameter list is mixed list of * and values 63 // reference parameters are pointers 64 65 // the basic idea here is to use gpr[8] as a storage area for 66 // the future values of registers r3 to r10 needed for the call, 67 // and similarly fpr[13] as a storage area for the future values 68 // of floating point registers f1 to f13 69 70 unsigned long * mfunc; // actual function to be invoked 71 void (*ptr)(); 72 int gpr[8]; // storage for gpregisters, map to r3-r10 73 int off; // offset used to find function 74 double fpr[13]; // storage for fpregisters, map to f1-f13 75 int n; // number of gprs mapped so far 76 int f; // number of fprs mapped so far 77 volatile long *p; // pointer to parameter overflow area 78 int c; // character of parameter type being decoded 79 volatile double dret; // temporary function return values 80 volatile int iret, iret2; 81 82 // Because of the Power PC calling conventions we could be passing 83 // parameters in both register types and on the stack. To create the 84 // stack parameter area we need we now simply allocate local 85 // variable storage param[] that is at least the size of the parameter stack 86 // (more than enough space) which we can overwrite the parameters into. 87 88 // Note: This keeps us from having to decode the signature twice and 89 // prevents problems with later local variables. 90 91 // FIXME: I do not believe the following is true but we will keep the 92 // FIXME: extra space just to be safe until proven otherwise 93 94 // Note: could require up to 2*nStackLongs words of parameter stack area 95 // if the call has many float parameters (i.e. floats take up only 1 96 // word on the stack but take 2 words in parameter area in the 97 // stack frame . 98 99 100 // unsigned long param[(2*nStackLongs)]; 101 102 /* now begin to load the C++ function arguments into storage */ 103 n = 0; 104 f = 0; 105 106 107 /* set up a pointer to the stack parameter area */ 108 __asm__ ( "addi %0,r1,24" : "=r" (p) : /* no inputs */ ); 109 110 // #i94421#, work around compiler error: 111 volatile long * pCopy = p; 112 (void) pCopy; // avoid warning about unused variable 113 114 // never called 115 // if (! pAdjustedThisPtr )CPPU_CURRENT_NAMESPACE::dummy_can_throw_anything("xxx"); // address something 116 117 118 // now we need to parse the entire signature string 119 // until we get the END indicator 120 121 // treat complex return pointer like any other parameter 122 123 // parse the argument list up to the ending ) 124 125 while (*pPT != 'X') { 126 c = *pPT; 127 switch (c) { 128 129 case 'D': /* type is double */ 130 if (f < 13) { 131 fpr[f++] = *((double *)pStackLongs); /* store in register */ 132 n+=2; 133 p+=2; 134 } else { 135 *p++ = *pStackLongs; /* or on the parameter stack */ 136 *p++ = *(pStackLongs + 1); 137 } 138 pStackLongs += 2; 139 break; 140 141 case 'F': /* type is float */ 142 /* floats are stored as 1 32 bit word on param stack */ 143 if (f < 13) { 144 fpr[f++] = *((float *)pStackLongs); 145 n+=1; 146 p++; 147 } else { 148 *((float *)p) = *((float *)pStackLongs); 149 p += 1; 150 } 151 pStackLongs += 1; 152 break; 153 154 case 'H': /* type is long long */ 155 if (n < 8) 156 { 157 gpr[n++] = *pStackLongs; 158 p++; 159 } 160 else 161 *p++ = *pStackLongs; 162 if(n < 8) 163 { 164 gpr[n++] = *(pStackLongs+1); 165 p++; 166 } 167 else 168 *p++ = *(pStackLongs+1); 169 pStackLongs += 2; 170 break; 171 172 case 'S': 173 if (n < 8) { 174 gpr[n++] = *((unsigned short*)pStackLongs); 175 p++; 176 } else { 177 *p++ = *((unsigned short *)pStackLongs); 178 } 179 pStackLongs += 1; 180 break; 181 182 case 'B': 183 if (n < 8) { 184 gpr[n++] = *((char *)pStackLongs); 185 p++; 186 } else { 187 *p++ = *((char *)pStackLongs); 188 } 189 pStackLongs += 1; 190 break; 191 192 default: 193 if (n < 8) { 194 gpr[n++] = *pStackLongs; 195 p++; 196 } else { 197 *p++ = *pStackLongs; 198 } 199 pStackLongs += 1; 200 break; 201 } 202 pPT++; 203 } 204 205 206 /* figure out the address of the function we need to invoke */ 207 off = nVtableIndex; 208 off = off * 4; // 4 bytes per slot 209 mfunc = *((unsigned long **)pAdjustedThisPtr); // get the address of the vtable 210 mfunc = (unsigned long *)((char *)mfunc + off); // get the address from the vtable entry at offset 211 mfunc = *((unsigned long **)mfunc); // the function is stored at the address 212 ptr = (void (*)())mfunc; 213 214 /* Set up the machine registers and invoke the function */ 215 216 __asm__ __volatile__ ( 217 "lwz r3, 0(%0)\n\t" 218 "lwz r4, 4(%0)\n\t" 219 "lwz r5, 8(%0)\n\t" 220 "lwz r6, 12(%0)\n\t" 221 "lwz r7, 16(%0)\n\t" 222 "lwz r8, 20(%0)\n\t" 223 "lwz r9, 24(%0)\n\t" 224 "lwz r10, 28(%0)\n\t" 225 "lfd f1, 0(%1)\n\t" 226 "lfd f2, 8(%1)\n\t" 227 "lfd f3, 16(%1)\n\t" 228 "lfd f4, 24(%1)\n\t" 229 "lfd f5, 32(%1)\n\t" 230 "lfd f6, 40(%1)\n\t" 231 "lfd f7, 48(%1)\n\t" 232 "lfd f8, 56(%1)\n\t" 233 "lfd f9, 64(%1)\n\t" 234 "lfd f10, 72(%1)\n\t" 235 "lfd f11, 80(%1)\n\t" 236 "lfd f12, 88(%1)\n\t" 237 "lfd f13, 96(%1)\n\t" 238 : : "r" (gpr), "r" (fpr) 239 : "r0", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10", 240 "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", 241 "f10", "f11", "f12", "f13" 242 ); 243 244 (*ptr)(); 245 246 247 __asm__ __volatile__ ( 248 "stw r3, %1\n\t" 249 "stw r4, %2\n\t" 250 "stfd f1, %0\n\t" 251 : : "m" (dret), "m" (iret), "m" (iret2) 252 ); 253 254 255 switch( eReturnType ) 256 { 257 case typelib_TypeClass_HYPER: 258 case typelib_TypeClass_UNSIGNED_HYPER: 259 ((long*)pRegisterReturn)[1] = iret2; 260 // fall thru on purpose 261 case typelib_TypeClass_LONG: 262 case typelib_TypeClass_UNSIGNED_LONG: 263 case typelib_TypeClass_ENUM: 264 ((long*)pRegisterReturn)[0] = iret; 265 break; 266 267 case typelib_TypeClass_CHAR: 268 case typelib_TypeClass_SHORT: 269 case typelib_TypeClass_UNSIGNED_SHORT: 270 *(unsigned short*)pRegisterReturn = (unsigned short)iret; 271 break; 272 273 case typelib_TypeClass_BOOLEAN: 274 case typelib_TypeClass_BYTE: 275 *(unsigned char*)pRegisterReturn = (unsigned char)iret; 276 break; 277 278 case typelib_TypeClass_FLOAT: 279 *(float*)pRegisterReturn = (float)dret; 280 break; 281 282 case typelib_TypeClass_DOUBLE: 283 *(double*)pRegisterReturn = dret; 284 break; 285 default: 286 break; 287 } 288 } 289 290 291 //================================================================================================== 292 static void cpp_call( 293 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis, 294 bridges::cpp_uno::shared::VtableSlot aVtableSlot, 295 typelib_TypeDescriptionReference * pReturnTypeRef, 296 sal_Int32 nParams, typelib_MethodParameter * pParams, 297 void * pUnoReturn, void * pUnoArgs[], uno_Any ** ppUnoExc ) 298 { 299 // max space for: [complex ret ptr], values|ptr ... 300 char * pCppStack = 301 (char *)alloca( sizeof(sal_Int32) + ((nParams+2) * sizeof(sal_Int64)) ); 302 char * pCppStackStart = pCppStack; 303 304 // need to know parameter types for callVirtualMethod so generate a signature string 305 char * pParamType = (char *) alloca(nParams+2); 306 char * pPT = pParamType; 307 308 // return 309 typelib_TypeDescription * pReturnTypeDescr = 0; 310 TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef ); 311 OSL_ENSURE( pReturnTypeDescr, "### expected return type description!" ); 312 313 void * pCppReturn = 0; // if != 0 && != pUnoReturn, needs reconversion 314 315 if (pReturnTypeDescr) 316 { 317 if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr )) 318 { 319 pCppReturn = pUnoReturn; // direct way for simple types 320 } 321 else 322 { 323 // complex return via ptr 324 pCppReturn = *(void **)pCppStack 325 = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr ) 326 ? alloca( pReturnTypeDescr->nSize ) 327 : pUnoReturn); // direct way 328 *pPT++ = 'C'; //signify that a complex return type on stack 329 pCppStack += sizeof(void *); 330 } 331 } 332 // push this 333 void * pAdjustedThisPtr = reinterpret_cast< void ** >(pThis->getCppI()) 334 + aVtableSlot.offset; 335 *(void**)pCppStack = pAdjustedThisPtr; 336 pCppStack += sizeof( void* ); 337 *pPT++ = 'I'; 338 339 // stack space 340 OSL_ENSURE( sizeof(void *) == sizeof(sal_Int32), "### unexpected size!" ); 341 // args 342 void ** pCppArgs = (void **)alloca( 3 * sizeof(void *) * nParams ); 343 // indizes of values this have to be converted (interface conversion cpp<=>uno) 344 sal_Int32 * pTempIndizes = (sal_Int32 *)(pCppArgs + nParams); 345 // type descriptions for reconversions 346 typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pCppArgs + (2 * nParams)); 347 348 sal_Int32 nTempIndizes = 0; 349 350 for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos ) 351 { 352 const typelib_MethodParameter & rParam = pParams[nPos]; 353 typelib_TypeDescription * pParamTypeDescr = 0; 354 TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef ); 355 356 if (!rParam.bOut 357 && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr )) 358 { 359 uno_copyAndConvertData( pCppArgs[nPos] = pCppStack, pUnoArgs[nPos], pParamTypeDescr, 360 pThis->getBridge()->getUno2Cpp() ); 361 362 switch (pParamTypeDescr->eTypeClass) 363 { 364 365 // we need to know type of each param so that we know whether to use 366 // gpr or fpr to pass in parameters: 367 // Key: I - int, long, pointer, etc means pass in gpr 368 // B - byte value passed in gpr 369 // S - short value passed in gpr 370 // F - float value pass in fpr 371 // D - double value pass in fpr 372 // H - long long int pass in proper pairs of gpr (3,4) (5,6), etc 373 // X - indicates end of parameter description string 374 375 case typelib_TypeClass_LONG: 376 case typelib_TypeClass_UNSIGNED_LONG: 377 case typelib_TypeClass_ENUM: 378 *pPT++ = 'I'; 379 break; 380 case typelib_TypeClass_SHORT: 381 case typelib_TypeClass_CHAR: 382 case typelib_TypeClass_UNSIGNED_SHORT: 383 *pPT++ = 'S'; 384 break; 385 case typelib_TypeClass_BOOLEAN: 386 case typelib_TypeClass_BYTE: 387 *pPT++ = 'B'; 388 break; 389 case typelib_TypeClass_FLOAT: 390 *pPT++ = 'F'; 391 break; 392 case typelib_TypeClass_DOUBLE: 393 *pPT++ = 'D'; 394 pCppStack += sizeof(sal_Int32); // extra long 395 break; 396 case typelib_TypeClass_HYPER: 397 case typelib_TypeClass_UNSIGNED_HYPER: 398 *pPT++ = 'H'; 399 pCppStack += sizeof(sal_Int32); // extra long 400 default: 401 break; 402 } 403 404 // no longer needed 405 TYPELIB_DANGER_RELEASE( pParamTypeDescr ); 406 } 407 else // ptr to complex value | ref 408 { 409 if (! rParam.bIn) // is pure out 410 { 411 // cpp out is constructed mem, uno out is not! 412 uno_constructData( 413 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ), 414 pParamTypeDescr ); 415 pTempIndizes[nTempIndizes] = nPos; // default constructed for cpp call 416 // will be released at reconversion 417 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr; 418 } 419 // is in/inout 420 else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr )) 421 { 422 uno_copyAndConvertData( 423 *(void **)pCppStack = pCppArgs[nPos] = alloca( pParamTypeDescr->nSize ), 424 pUnoArgs[nPos], pParamTypeDescr, pThis->getBridge()->getUno2Cpp() ); 425 426 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted 427 // will be released at reconversion 428 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr; 429 } 430 else // direct way 431 { 432 *(void **)pCppStack = pCppArgs[nPos] = pUnoArgs[nPos]; 433 // no longer needed 434 TYPELIB_DANGER_RELEASE( pParamTypeDescr ); 435 } 436 *pPT++='I'; 437 } 438 pCppStack += sizeof(sal_Int32); // standard parameter length 439 } 440 441 // terminate the signature string 442 *pPT++='X'; 443 *pPT=0; 444 445 try 446 { 447 OSL_ENSURE( !( (pCppStack - pCppStackStart ) & 3), "UNALIGNED STACK !!! (Please DO panic)" ); 448 callVirtualMethod( 449 pAdjustedThisPtr, aVtableSlot.index, 450 pCppReturn, pReturnTypeDescr->eTypeClass, pParamType, 451 (sal_Int32 *)pCppStackStart, (pCppStack - pCppStackStart) / sizeof(sal_Int32) ); 452 // NO exception occured... 453 *ppUnoExc = 0; 454 455 // reconvert temporary params 456 for ( ; nTempIndizes--; ) 457 { 458 sal_Int32 nIndex = pTempIndizes[nTempIndizes]; 459 typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes]; 460 461 if (pParams[nIndex].bIn) 462 { 463 if (pParams[nIndex].bOut) // inout 464 { 465 uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 ); // destroy uno value 466 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr, 467 pThis->getBridge()->getCpp2Uno() ); 468 } 469 } 470 else // pure out 471 { 472 uno_copyAndConvertData( pUnoArgs[nIndex], pCppArgs[nIndex], pParamTypeDescr, 473 pThis->getBridge()->getCpp2Uno() ); 474 } 475 // destroy temp cpp param => cpp: every param was constructed 476 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release ); 477 478 TYPELIB_DANGER_RELEASE( pParamTypeDescr ); 479 } 480 // return value 481 if (pCppReturn && pUnoReturn != pCppReturn) 482 { 483 uno_copyAndConvertData( pUnoReturn, pCppReturn, pReturnTypeDescr, 484 pThis->getBridge()->getCpp2Uno() ); 485 uno_destructData( pCppReturn, pReturnTypeDescr, cpp_release ); 486 } 487 } 488 catch (...) 489 { 490 // fill uno exception 491 fillUnoException( CPPU_CURRENT_NAMESPACE::__cxa_get_globals()->caughtExceptions, *ppUnoExc, pThis->getBridge()->getCpp2Uno() ); 492 493 // temporary params 494 for ( ; nTempIndizes--; ) 495 { 496 sal_Int32 nIndex = pTempIndizes[nTempIndizes]; 497 // destroy temp cpp param => cpp: every param was constructed 498 uno_destructData( pCppArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], cpp_release ); 499 TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] ); 500 } 501 // return type 502 if (pReturnTypeDescr) 503 TYPELIB_DANGER_RELEASE( pReturnTypeDescr ); 504 } 505 } 506 507 } 508 509 namespace bridges { namespace cpp_uno { namespace shared { 510 511 void unoInterfaceProxyDispatch( 512 uno_Interface * pUnoI, const typelib_TypeDescription * pMemberDescr, 513 void * pReturn, void * pArgs[], uno_Any ** ppException ) 514 { 515 // is my surrogate 516 bridges::cpp_uno::shared::UnoInterfaceProxy * pThis 517 = static_cast< bridges::cpp_uno::shared::UnoInterfaceProxy * > (pUnoI); 518 // typelib_InterfaceTypeDescription * pTypeDescr = pThis->pTypeDescr; 519 520 switch (pMemberDescr->eTypeClass) 521 { 522 case typelib_TypeClass_INTERFACE_ATTRIBUTE: 523 { 524 525 VtableSlot aVtableSlot( 526 getVtableSlot( 527 reinterpret_cast< 528 typelib_InterfaceAttributeTypeDescription const * >( 529 pMemberDescr))); 530 531 if (pReturn) 532 { 533 // dependent dispatch 534 cpp_call( 535 pThis, aVtableSlot, 536 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef, 537 0, 0, // no params 538 pReturn, pArgs, ppException ); 539 } 540 else 541 { 542 // is SET 543 typelib_MethodParameter aParam; 544 aParam.pTypeRef = 545 ((typelib_InterfaceAttributeTypeDescription *)pMemberDescr)->pAttributeTypeRef; 546 aParam.bIn = sal_True; 547 aParam.bOut = sal_False; 548 549 typelib_TypeDescriptionReference * pReturnTypeRef = 0; 550 OUString aVoidName( RTL_CONSTASCII_USTRINGPARAM("void") ); 551 typelib_typedescriptionreference_new( 552 &pReturnTypeRef, typelib_TypeClass_VOID, aVoidName.pData ); 553 554 // dependent dispatch 555 aVtableSlot.index += 1; //get then set method 556 cpp_call( 557 pThis, aVtableSlot, 558 pReturnTypeRef, 559 1, &aParam, 560 pReturn, pArgs, ppException ); 561 562 typelib_typedescriptionreference_release( pReturnTypeRef ); 563 } 564 565 break; 566 } 567 case typelib_TypeClass_INTERFACE_METHOD: 568 { 569 570 VtableSlot aVtableSlot( 571 getVtableSlot( 572 reinterpret_cast< 573 typelib_InterfaceMethodTypeDescription const * >( 574 pMemberDescr))); 575 switch (aVtableSlot.index) 576 { 577 // standard calls 578 case 1: // acquire uno interface 579 (*pUnoI->acquire)( pUnoI ); 580 *ppException = 0; 581 break; 582 case 2: // release uno interface 583 (*pUnoI->release)( pUnoI ); 584 *ppException = 0; 585 break; 586 case 0: // queryInterface() opt 587 { 588 typelib_TypeDescription * pTD = 0; 589 TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( pArgs[0] )->getTypeLibType() ); 590 if (pTD) 591 { 592 uno_Interface * pInterface = 0; 593 (*pThis->pBridge->getUnoEnv()->getRegisteredInterface)( 594 pThis->pBridge->getUnoEnv(), 595 (void **)&pInterface, pThis->oid.pData, (typelib_InterfaceTypeDescription *)pTD ); 596 597 if (pInterface) 598 { 599 ::uno_any_construct( 600 reinterpret_cast< uno_Any * >( pReturn ), 601 &pInterface, pTD, 0 ); 602 (*pInterface->release)( pInterface ); 603 TYPELIB_DANGER_RELEASE( pTD ); 604 *ppException = 0; 605 break; 606 } 607 TYPELIB_DANGER_RELEASE( pTD ); 608 } 609 } // else perform queryInterface() 610 default: 611 // dependent dispatch 612 cpp_call( 613 pThis, aVtableSlot, 614 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pReturnTypeRef, 615 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->nParams, 616 ((typelib_InterfaceMethodTypeDescription *)pMemberDescr)->pParams, 617 pReturn, pArgs, ppException ); 618 } 619 break; 620 } 621 default: 622 { 623 ::com::sun::star::uno::RuntimeException aExc( 624 OUString( RTL_CONSTASCII_USTRINGPARAM("illegal member type description!") ), 625 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >() ); 626 627 Type const & rExcType = ::getCppuType( &aExc ); 628 // binary identical null reference 629 ::uno_type_any_construct( *ppException, &aExc, rExcType.getTypeLibType(), 0 ); 630 } 631 } 632 } 633 634 } } } 635