xref: /trunk/main/bridges/source/cpp_uno/gcc3_freebsd_powerpc64/cpp2uno.cxx (revision 22994af5c7b170ab8a0cd369962243bf8b0a8831)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_bridges.hxx"
26 
27 #include <com/sun/star/uno/genfunc.hxx>
28 #include <uno/data.h>
29 #include <typelib/typedescription.hxx>
30 
31 #include <osl/endian.h>
32 #include "bridges/cpp_uno/shared/bridge.hxx"
33 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
34 #include "bridges/cpp_uno/shared/types.hxx"
35 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
36 
37 #include "share.hxx"
38 #include <stdio.h>
39 #include <string.h>
40 
41 #ifdef OSL_BIGENDIAN
42 #define IS_BIG_ENDIAN 1
43 #else
44 #define IS_BIG_ENDIAN 0
45 #endif
46 
47 using namespace ::com::sun::star::uno;
48 
49 namespace
50 {
51 
52 //==================================================================================================
cpp2uno_call(bridges::cpp_uno::shared::CppInterfaceProxy * pThis,const typelib_TypeDescription * pMemberTypeDescr,typelib_TypeDescriptionReference * pReturnTypeRef,sal_Int32 nParams,typelib_MethodParameter * pParams,void ** gpreg,void ** fpreg,void ** ovrflw,sal_Int64 * pRegisterReturn)53 static typelib_TypeClass cpp2uno_call(
54     bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
55     const typelib_TypeDescription * pMemberTypeDescr,
56     typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
57     sal_Int32 nParams, typelib_MethodParameter * pParams,
58         void ** gpreg, void ** fpreg, void ** ovrflw,
59     sal_Int64 * pRegisterReturn /* space for register return */ )
60 {
61 #ifdef CMC_DEBUG
62     fprintf(stderr, "as far as cpp2uno_call\n");
63 #endif
64 
65     int ng = 0; //number of gpr registers used
66     int nf = 0; //number of fpr regsiters used
67 
68     // gpreg:  [ret *], this, [gpr params]
69     // fpreg:  [fpr params]
70     // ovrflw: [gpr or fpr params (properly aligned)]
71 
72     // return
73     typelib_TypeDescription * pReturnTypeDescr = 0;
74     if (pReturnTypeRef)
75         TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
76 
77     void * pUnoReturn = 0;
78     void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
79     double * pFprReturn = reinterpret_cast< double * >( fpreg );
80     typelib_TypeClass eFprBase = typelib_TypeClass_VOID;
81     sal_Int32 nFprMembers = 0;
82 
83     if (pReturnTypeDescr)
84     {
85         if (!ppc64::return_in_hidden_param( pReturnTypeRef ))
86         {
87             nFprMembers = ppc64::return_in_fprs( pReturnTypeRef, eFprBase );
88             pUnoReturn = (nFprMembers != 0
89                           ? alloca( pReturnTypeDescr->nSize )
90                           : pRegisterReturn); // direct way for simple types
91         }
92         else // complex return via ptr (pCppReturn)
93         {
94             pCppReturn = *(void **)gpreg;
95             gpreg++;
96             ng++;
97 
98             pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
99                           ? alloca( pReturnTypeDescr->nSize )
100                           : pCppReturn); // direct way
101         }
102     }
103     // pop this
104     gpreg++;
105     ng++;
106 
107     // stack space
108     OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64), "### unexpected size!" );
109     // parameters
110     void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
111     void ** pCppArgs = pUnoArgs + nParams;
112     // indizes of values this have to be converted (interface conversion cpp<=>uno)
113     sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
114     // type descriptions for reconversions
115     typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
116 
117     sal_Int32 nTempIndizes = 0;
118     bool bOverFlowUsed = false;
119     for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
120     {
121         const typelib_MethodParameter & rParam = pParams[nPos];
122         typelib_TypeDescription * pParamTypeDescr = 0;
123         TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
124 
125 #ifdef CMC_DEBUG
126         fprintf(stderr, "arg %d of %d\n", nPos, nParams);
127 #endif
128 
129         if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr ))
130         {
131 #ifdef CMC_DEBUG
132             fprintf(stderr, "simple\n");
133 #endif
134 
135             switch (pParamTypeDescr->eTypeClass)
136             {
137                 case typelib_TypeClass_FLOAT:
138                 case typelib_TypeClass_DOUBLE:
139                     if (nf < ppc64::MAX_SSE_REGS)
140                     {
141                         if (pParamTypeDescr->eTypeClass == typelib_TypeClass_FLOAT)
142                         {
143                             float tmp = (float) (*((double *)fpreg));
144                             (*((float *) fpreg)) = tmp;
145                         }
146                         pCppArgs[nPos] = pUnoArgs[nPos] = fpreg++;
147                         nf++;
148                     }
149                     else
150                     {
151                         pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
152                         bOverFlowUsed = true;
153                     }
154                     if (bOverFlowUsed) ovrflw++;
155                     break;
156                 case typelib_TypeClass_BYTE:
157                 case typelib_TypeClass_BOOLEAN:
158                     if (ng < ppc64::MAX_GPR_REGS)
159                     {
160                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)gpreg) + 7*IS_BIG_ENDIAN);
161                         ng++;
162                         gpreg++;
163                     }
164                     else
165                     {
166                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + 7*IS_BIG_ENDIAN);
167                         bOverFlowUsed = true;
168                     }
169                     if (bOverFlowUsed) ovrflw++;
170                     break;
171                 case typelib_TypeClass_CHAR:
172                 case typelib_TypeClass_SHORT:
173                 case typelib_TypeClass_UNSIGNED_SHORT:
174                     if (ng < ppc64::MAX_GPR_REGS)
175                     {
176                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)gpreg) + 6*IS_BIG_ENDIAN);
177                         ng++;
178                         gpreg++;
179                     }
180                     else
181                     {
182                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + 6*IS_BIG_ENDIAN);
183                         bOverFlowUsed = true;
184                     }
185                     if (bOverFlowUsed) ovrflw++;
186                     break;
187         case typelib_TypeClass_ENUM:
188                 case typelib_TypeClass_LONG:
189                 case typelib_TypeClass_UNSIGNED_LONG:
190                     if (ng < ppc64::MAX_GPR_REGS)
191                     {
192                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)gpreg) + 4*IS_BIG_ENDIAN);
193                         ng++;
194                         gpreg++;
195                     }
196                     else
197                     {
198                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + 4*IS_BIG_ENDIAN);
199                         bOverFlowUsed = true;
200                     }
201                     if (bOverFlowUsed) ovrflw++;
202                     break;
203                 default:
204                     if (ng < ppc64::MAX_GPR_REGS)
205                     {
206                         pCppArgs[nPos] = pUnoArgs[nPos] = gpreg++;
207                         ng++;
208                     }
209                     else
210                     {
211                         pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
212                         bOverFlowUsed = true;
213                     }
214                     if (bOverFlowUsed) ovrflw++;
215                     break;
216                 }
217 
218                 // no longer needed
219                 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
220         }
221         else // ptr to complex value | ref
222         {
223 #ifdef CMC_DEBUG
224             fprintf(stderr, "complex, ng is %d\n", ng);
225 #endif
226             void *pCppStack; //temporary stack pointer
227 
228             if (ng < ppc64::MAX_GPR_REGS)
229             {
230                 pCppArgs[nPos] = pCppStack = *gpreg++;
231                 ng++;
232             }
233             else
234             {
235                 pCppArgs[nPos] = pCppStack = *ovrflw;
236                 bOverFlowUsed = true;
237             }
238             if (bOverFlowUsed) ovrflw++;
239 
240             if (! rParam.bIn) // is pure out
241             {
242                 // uno out is unconstructed mem!
243                 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
244                 pTempIndizes[nTempIndizes] = nPos;
245                 // will be released at reconversion
246                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
247             }
248             // is in/inout
249             else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
250             {
251                 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
252                                         pCppStack, pParamTypeDescr,
253                                         pThis->getBridge()->getCpp2Uno() );
254                 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
255                 // will be released at reconversion
256                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
257             }
258             else // direct way
259             {
260                 pUnoArgs[nPos] = pCppStack;
261                 // no longer needed
262                 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
263             }
264         }
265     }
266 
267 #ifdef CMC_DEBUG
268     fprintf(stderr, "end of params\n");
269 #endif
270 
271     // ExceptionHolder
272     uno_Any aUnoExc; // Any will be constructed by callee
273     uno_Any * pUnoExc = &aUnoExc;
274 
275     // invoke uno dispatch call
276     (*pThis->getUnoI()->pDispatcher)( pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
277 
278     // in case an exception occurred...
279     if (pUnoExc)
280     {
281         // destruct temporary in/inout params
282         for ( ; nTempIndizes--; )
283         {
284             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
285 
286             if (pParams[nIndex].bIn) // is in/inout => was constructed
287                 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
288             TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
289         }
290         if (pReturnTypeDescr)
291             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
292 
293         CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc, pThis->getBridge()->getUno2Cpp() );
294                 // has to destruct the any
295         // is here for dummy
296         return typelib_TypeClass_VOID;
297     }
298     else // else no exception occurred...
299     {
300         // temporary params
301         for ( ; nTempIndizes--; )
302         {
303             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
304             typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
305 
306             if (pParams[nIndex].bOut) // inout/out
307             {
308                 // convert and assign
309                 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
310                 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
311                                         pThis->getBridge()->getUno2Cpp() );
312             }
313             // destroy temp uno param
314             uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
315 
316             TYPELIB_DANGER_RELEASE( pParamTypeDescr );
317         }
318         // return
319         if (pCppReturn) // has complex return
320         {
321             if (pUnoReturn != pCppReturn) // needs reconversion
322             {
323                 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
324                                         pThis->getBridge()->getUno2Cpp() );
325                 // destroy temp uno return
326                 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
327             }
328             // complex return ptr is set to return reg
329             *(void **)pRegisterReturn = pCppReturn;
330         }
331         if (pReturnTypeDescr)
332         {
333             typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
334             if (nFprMembers != 0)
335             {
336                 for (sal_Int32 i = 0; i < nFprMembers; ++i)
337                     pFprReturn[i] = (eFprBase == typelib_TypeClass_FLOAT
338                                      ? static_cast< float * >( pUnoReturn )[i]
339                                      : static_cast< double * >( pUnoReturn )[i]);
340                 eRet = ppc64::TYPECLASS_FPRS;
341             }
342 #ifdef OSL_BIGENDIAN
343             else if (eRet == typelib_TypeClass_STRUCT && !pCppReturn && pReturnTypeDescr->nSize < 8)
344             {
345                 sal_uInt64 n = 0;
346                 memcpy( reinterpret_cast< char * >( &n ) + 8 - pReturnTypeDescr->nSize,
347                         pRegisterReturn, pReturnTypeDescr->nSize );
348                 *reinterpret_cast< sal_uInt64 * >( pRegisterReturn ) = n;
349             }
350 #endif
351             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
352             return eRet;
353         }
354         else
355             return typelib_TypeClass_VOID;
356     }
357 }
358 
359 #if defined(_CALL_ELF) && _CALL_ELF == 2
360 #  define PARAMSAVE 32
361 #else
362 #  define PARAMSAVE 48
363 #endif
364 
cpp_mediate(sal_uInt64 nOffsetAndIndex,void ** gpreg,void ** fpreg,long sp,sal_Int64 * pRegisterReturn)365 extern "C" typelib_TypeClass cpp_mediate(
366     sal_uInt64 nOffsetAndIndex,
367         void ** gpreg, void ** fpreg, long sp,
368     sal_Int64 * pRegisterReturn /* space for register return */ )
369 {
370     static_assert(sizeof(sal_Int64)==sizeof(void *), "### unexpected!");
371 
372     sal_Int32 nVtableOffset = (nOffsetAndIndex >> 32);
373     sal_Int32 nFunctionIndex = (nOffsetAndIndex & 0xFFFFFFFF);
374 
375     long sf = *(long*)sp;
376     void ** ovrflw = (void**)(sf + PARAMSAVE + 64);
377 
378     // gpreg:  [ret *], this, [other gpr params]
379     // fpreg:  [fpr params]
380     // ovrflw: [gpr or fpr params (properly aligned)]
381 
382     void * pThis;
383     if (nFunctionIndex & 0x80000000 )
384     {
385     nFunctionIndex &= 0x7fffffff;
386     pThis = gpreg[1];
387 #ifdef CMC_DEBUG
388     fprintf(stderr, "pThis is gpreg[1]\n");
389 #endif
390     }
391     else
392     {
393     pThis = gpreg[0];
394 #ifdef CMC_DEBUG
395     fprintf(stderr, "pThis is gpreg[0]\n");
396 #endif
397     }
398 
399 #ifdef CMC_DEBUG
400     fprintf(stderr, "pThis is %lx\n", pThis);
401 #endif
402 
403     pThis = static_cast< char * >(pThis) - nVtableOffset;
404 
405 #ifdef CMC_DEBUG
406     fprintf(stderr, "pThis is now %lx\n", pThis);
407 #endif
408 
409     bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
410         = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
411             pThis);
412 
413     typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
414 
415 #ifdef CMC_DEBUG
416     fprintf(stderr, "indexes are %d %d\n", nFunctionIndex, pTypeDescr->nMapFunctionIndexToMemberIndex);
417 #endif
418 
419     OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
420     if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
421     {
422         throw RuntimeException(
423             rtl::OUString::createFromAscii("illegal vtable index!"),
424             (XInterface *)pThis );
425     }
426 
427     // determine called method
428     sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
429     OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
430 
431 #ifdef CMC_DEBUG
432     fprintf(stderr, "members are %d %d\n", nMemberPos, pTypeDescr->nAllMembers);
433 #endif
434 
435     TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
436 
437     typelib_TypeClass eRet;
438     switch (aMemberDescr.get()->eTypeClass)
439     {
440     case typelib_TypeClass_INTERFACE_ATTRIBUTE:
441     {
442         if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
443         {
444             // is GET method
445             eRet = cpp2uno_call(
446                 pCppI, aMemberDescr.get(),
447                 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
448                 0, 0, // no params
449                 gpreg, fpreg, ovrflw, pRegisterReturn );
450         }
451         else
452         {
453             // is SET method
454             typelib_MethodParameter aParam;
455             aParam.pTypeRef =
456                 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
457             aParam.bIn      = sal_True;
458             aParam.bOut     = sal_False;
459 
460             eRet = cpp2uno_call(
461                 pCppI, aMemberDescr.get(),
462                 0, // indicates void return
463                 1, &aParam,
464                 gpreg, fpreg, ovrflw, pRegisterReturn );
465         }
466         break;
467     }
468     case typelib_TypeClass_INTERFACE_METHOD:
469     {
470         // is METHOD
471         switch (nFunctionIndex)
472         {
473         case 1: // acquire()
474             pCppI->acquireProxy(); // non virtual call!
475             eRet = typelib_TypeClass_VOID;
476             break;
477         case 2: // release()
478             pCppI->releaseProxy(); // non virtual call!
479             eRet = typelib_TypeClass_VOID;
480             break;
481         case 0: // queryInterface() opt
482         {
483             typelib_TypeDescription * pTD = 0;
484             TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( gpreg[2] )->getTypeLibType() );
485             if (pTD)
486             {
487                 XInterface * pInterface = 0;
488                 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
489                     pCppI->getBridge()->getCppEnv(),
490                     (void **)&pInterface, pCppI->getOid().pData,
491                     (typelib_InterfaceTypeDescription *)pTD );
492 
493                 if (pInterface)
494                 {
495                     ::uno_any_construct(
496                         reinterpret_cast< uno_Any * >( gpreg[0] ),
497                         &pInterface, pTD, cpp_acquire );
498                     pInterface->release();
499                     TYPELIB_DANGER_RELEASE( pTD );
500                     *(void **)pRegisterReturn = gpreg[0];
501                     eRet = typelib_TypeClass_ANY;
502                     break;
503                 }
504                 TYPELIB_DANGER_RELEASE( pTD );
505             }
506         } // else perform queryInterface()
507         default:
508             eRet = cpp2uno_call(
509                 pCppI, aMemberDescr.get(),
510                 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
511                 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
512                 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
513                 gpreg, fpreg, ovrflw, pRegisterReturn );
514         }
515         break;
516     }
517     default:
518     {
519 #ifdef CMC_DEBUG
520         fprintf(stderr, "screwed\n");
521 #endif
522 
523         throw RuntimeException(
524             rtl::OUString::createFromAscii("no member description found!"),
525             (XInterface *)pThis );
526         // is here for dummy
527         eRet = typelib_TypeClass_VOID;
528     }
529     }
530 
531 #ifdef CMC_DEBUG
532         fprintf(stderr, "end of cpp_mediate\n");
533 #endif
534     return eRet;
535 }
536 
537 extern "C" void privateSnippetExecutor( ... );
538 #if 0
539 {
540     sal_uInt64 gpreg[ppc64::MAX_GPR_REGS];
541 
542     register long r3 asm("r3"); gpreg[0] = r3;
543     register long r4 asm("r4"); gpreg[1] = r4;
544     register long r5 asm("r5"); gpreg[2] = r5;
545     register long r6 asm("r6"); gpreg[3] = r6;
546     register long r7 asm("r7"); gpreg[4] = r7;
547     register long r8 asm("r8"); gpreg[5] = r8;
548     register long r9 asm("r9"); gpreg[6] = r9;
549     register long r10 asm("r10"); gpreg[7] = r10;
550 
551     double fpreg[ppc64::MAX_SSE_REGS];
552 
553     __asm__ __volatile__ (
554         "stfd 1,   0(%0)\t\n"
555         "stfd 2,   8(%0)\t\n"
556         "stfd 3,  16(%0)\t\n"
557         "stfd 4,  24(%0)\t\n"
558         "stfd 5,  32(%0)\t\n"
559         "stfd 6,  40(%0)\t\n"
560         "stfd 7,  48(%0)\t\n"
561         "stfd 8,  56(%0)\t\n"
562         "stfd 9,  64(%0)\t\n"
563         "stfd 10, 72(%0)\t\n"
564         "stfd 11, 80(%0)\t\n"
565         "stfd 12, 88(%0)\t\n"
566         "stfd 13, 96(%0)\t\n"
567     : : "r" (fpreg)
568         : "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7", "fr8", "fr9",
569           "fr10", "fr11", "fr12", "fr13"
570     );
571 
572     register long r11 asm("r11");
573     const long nOffsetAndIndex = r11;
574 
575     register long r1 asm("r1");
576     const long sp = r1;
577 
578 #if defined(_CALL_ELF) && _CALL_ELF == 2
579     volatile long nRegReturn[2];
580 #else
581     volatile long nRegReturn[1];
582 #endif
583 
584     typelib_TypeClass aType =
585         cpp_mediate( nOffsetAndIndex, (void**)gpreg, (void**)fpreg, sp, (sal_Int64*)nRegReturn);
586 
587     switch( aType )
588     {
589         case typelib_TypeClass_VOID:
590         break;
591         case typelib_TypeClass_BOOLEAN:
592         case typelib_TypeClass_BYTE:
593             __asm__( "lbz 3,%0\n\t"
594                 : : "m" (nRegReturn[0]) );
595             break;
596         case typelib_TypeClass_CHAR:
597         case typelib_TypeClass_UNSIGNED_SHORT:
598             __asm__( "lhz 3,%0\n\t"
599                 : : "m" (nRegReturn[0]) );
600             break;
601         case typelib_TypeClass_SHORT:
602             __asm__( "lha 3,%0\n\t"
603                 : : "m" (nRegReturn[0]) );
604             break;
605         case typelib_TypeClass_ENUM:
606         case typelib_TypeClass_UNSIGNED_LONG:
607             __asm__( "lwz 3,%0\n\t"
608                 : : "m"(nRegReturn[0]) );
609             break;
610         case typelib_TypeClass_LONG:
611             __asm__( "lwa 3,%0\n\t"
612                 : : "m"(nRegReturn[0]) );
613             break;
614         case typelib_TypeClass_FLOAT:
615             __asm__( "lfs 1,%0\n\t"
616                 : : "m" (*((float*)nRegReturn)) );
617             break;
618         case typelib_TypeClass_DOUBLE:
619             __asm__( "lfd 1,%0\n\t"
620                 : : "m" (*((double*)nRegReturn)) );
621             break;
622         default:
623             __asm__( "ld 3,%0\n\t"
624                 : : "m" (nRegReturn[0]) );
625 #if defined(_CALL_ELF) && _CALL_ELF == 2
626             __asm__( "ld 4,%0\n\t"
627                 : : "m" (nRegReturn[1]) );
628 #endif
629             break;
630     }
631 }
632 #endif
633 
634 #if defined(_CALL_ELF) && _CALL_ELF == 2
635 const int codeSnippetSize = 32;
636 #else
637 const int codeSnippetSize = 24;
638 #endif
639 
codeSnippet(unsigned char * code,sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,bool simpleRetType)640 unsigned char *  codeSnippet( unsigned char * code, sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset,
641                               bool simpleRetType)
642 {
643 #ifdef CMC_DEBUG
644     fprintf(stderr,"in codeSnippet functionIndex is %x\n", nFunctionIndex);
645     fprintf(stderr,"in codeSnippet vtableOffset is %x\n", nVtableOffset);
646 #endif
647 
648     sal_uInt64 nOffsetAndIndex = ( ( (sal_uInt64) nVtableOffset ) << 32 ) | ( (sal_uInt64) nFunctionIndex );
649 
650     if ( !simpleRetType )
651         nOffsetAndIndex |= 0x80000000;
652 #if defined(_CALL_ELF) && _CALL_ELF == 2
653     unsigned int *raw = (unsigned int *)&code[0];
654 
655     raw[0] = 0xe96c0018;        /* 0:   ld      11,2f-0b(12)    */
656     raw[1] = 0xe98c0010;        /*      ld      12,1f-0b(12)    */
657     raw[2] = 0x7d8903a6;        /*      mtctr   12              */
658     raw[3] = 0x4e800420;        /*      bctr                    */
659                                 /* 1:   .quad   function_addr   */
660                                 /* 2:   .quad   context         */
661     *(void **)&raw[4] = (void *)privateSnippetExecutor;
662     *(void **)&raw[6] = (void*)nOffsetAndIndex;
663 #else
664     void ** raw = (void **)&code[0];
665     memcpy(raw, (char*) privateSnippetExecutor, 16);
666     raw[2] = (void*) nOffsetAndIndex;
667 #endif
668 #ifdef CMC_DEBUG
669     fprintf(stderr, "in: offset/index is %x %x %d, %lx\n",
670     nFunctionIndex, nVtableOffset, !simpleRetType, raw[2]);
671 #endif
672     return (code + codeSnippetSize);
673 }
674 
675 }
676 
flushCode(unsigned char const * bptr,unsigned char const * eptr)677 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const * bptr, unsigned char const * eptr)
678 {
679     int const lineSize = 32;
680     for (unsigned char const * p = bptr; p < eptr + lineSize; p += lineSize) {
681         __asm__ volatile ("dcbst 0, %0" : : "r"(p) : "memory");
682     }
683     __asm__ volatile ("sync" : : : "memory");
684     for (unsigned char const * p = bptr; p < eptr + lineSize; p += lineSize) {
685         __asm__ volatile ("icbi 0, %0" : : "r"(p) : "memory");
686     }
687     __asm__ volatile ("isync" : : : "memory");
688 }
689 
690 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
691 
692 bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)693 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
694 {
695     return static_cast< Slot * >(block) + 2;
696 }
697 
getBlockSize(sal_Int32 slotCount)698 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
699     sal_Int32 slotCount)
700 {
701     return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
702 }
703 
704 bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)705 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
706     void * block, sal_Int32 slotCount)
707 {
708     Slot * slots = mapBlockToVtable(block);
709     slots[-2].fn = 0;
710     slots[-1].fn = 0;
711     return slots + slotCount;
712 }
713 
addLocalFunctions(Slot ** slots,unsigned char * code,sal_PtrDiff writetoexecdiff,typelib_InterfaceTypeDescription const * type,sal_Int32 functionOffset,sal_Int32 functionCount,sal_Int32 vtableOffset)714 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
715     Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
716     typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
717     sal_Int32 functionCount, sal_Int32 vtableOffset)
718 {
719      (*slots) -= functionCount;
720      Slot * s = *slots;
721 #ifdef CMC_DEBUG
722     fprintf(stderr, "in addLocalFunctions functionOffset is %x\n",functionOffset);
723     fprintf(stderr, "in addLocalFunctions vtableOffset is %x\n",vtableOffset);
724 #endif
725 
726     for (sal_Int32 i = 0; i < type->nMembers; ++i) {
727         typelib_TypeDescription * member = 0;
728         TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
729         OSL_ASSERT(member != 0);
730         switch (member->eTypeClass) {
731         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
732             // Getter:
733             (s++)->fn = code + writetoexecdiff;
734             code = codeSnippet(
735                 code, functionOffset++, vtableOffset,
736                 !ppc64::return_in_hidden_param(
737                     reinterpret_cast<
738                     typelib_InterfaceAttributeTypeDescription * >(
739                         member)->pAttributeTypeRef));
740 
741             // Setter:
742             if (!reinterpret_cast<
743                 typelib_InterfaceAttributeTypeDescription * >(
744                     member)->bReadOnly)
745             {
746                 (s++)->fn = code + writetoexecdiff;
747                 code = codeSnippet(code, functionOffset++, vtableOffset, true);
748             }
749             break;
750 
751         case typelib_TypeClass_INTERFACE_METHOD:
752             (s++)->fn = code + writetoexecdiff;
753             code = codeSnippet(
754                 code, functionOffset++, vtableOffset,
755                 !ppc64::return_in_hidden_param(
756                     reinterpret_cast<
757                     typelib_InterfaceMethodTypeDescription * >(
758                         member)->pReturnTypeRef));
759             break;
760 
761         default:
762             OSL_ASSERT(false);
763             break;
764         }
765         TYPELIB_DANGER_RELEASE(member);
766     }
767     return code;
768 }
769 
770 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
771