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 "bridges/cpp_uno/shared/bridge.hxx"
32 #include "bridges/cpp_uno/shared/cppinterfaceproxy.hxx"
33 #include "bridges/cpp_uno/shared/types.hxx"
34 #include "bridges/cpp_uno/shared/vtablefactory.hxx"
35 
36 #include "share.hxx"
37 #include <stdio.h>
38 
39 using namespace ::com::sun::star::uno;
40 
41 namespace
42 {
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)43 static typelib_TypeClass cpp2uno_call(
44     bridges::cpp_uno::shared::CppInterfaceProxy * pThis,
45     const typelib_TypeDescription * pMemberTypeDescr,
46     typelib_TypeDescriptionReference * pReturnTypeRef, // 0 indicates void return
47     sal_Int32 nParams, typelib_MethodParameter * pParams,
48         void ** gpreg, void ** fpreg, void ** ovrflw,
49     sal_Int64 * pRegisterReturn /* space for register return */ )
50 {
51 #ifdef CMC_DEBUG
52     fprintf(stderr, "as far as cpp2uno_call\n");
53 #endif
54     int ng = 0; //number of gpr registers used
55     int nf = 0; //number of fpr regsiters used
56 
57     // gpreg:  [ret *], this, [gpr params]
58     // fpreg:  [fpr params]
59     // ovrflw: [gpr or fpr params (properly aligned)]
60 
61     // return
62     typelib_TypeDescription * pReturnTypeDescr = 0;
63     if (pReturnTypeRef)
64         TYPELIB_DANGER_GET( &pReturnTypeDescr, pReturnTypeRef );
65 
66     void * pUnoReturn = 0;
67     void * pCppReturn = 0; // complex return ptr: if != 0 && != pUnoReturn, reconversion need
68 
69     if (pReturnTypeDescr)
70     {
71         if (bridges::cpp_uno::shared::isSimpleType( pReturnTypeDescr ))
72         {
73             pUnoReturn = pRegisterReturn; // direct way for simple types
74         }
75         else // complex return via ptr (pCppReturn)
76         {
77             pCppReturn = *(void **)gpreg;
78             gpreg++;
79             ng++;
80 
81             pUnoReturn = (bridges::cpp_uno::shared::relatesToInterfaceType( pReturnTypeDescr )
82                           ? alloca( pReturnTypeDescr->nSize )
83                           : pCppReturn); // direct way
84         }
85     }
86     // pop this
87     gpreg++;
88     ng++;
89 
90     // stack space
91     OSL_ENSURE( sizeof(void *) == sizeof(sal_Int64), "### unexpected size!" );
92     // parameters
93     void ** pUnoArgs = (void **)alloca( 4 * sizeof(void *) * nParams );
94     void ** pCppArgs = pUnoArgs + nParams;
95     // indizes of values this have to be converted (interface conversion cpp<=>uno)
96     sal_Int32 * pTempIndizes = (sal_Int32 *)(pUnoArgs + (2 * nParams));
97     // type descriptions for reconversions
98     typelib_TypeDescription ** ppTempParamTypeDescr = (typelib_TypeDescription **)(pUnoArgs + (3 * nParams));
99 
100     sal_Int32 nTempIndizes   = 0;
101     for ( sal_Int32 nPos = 0; nPos < nParams; ++nPos )
102     {
103         const typelib_MethodParameter & rParam = pParams[nPos];
104         typelib_TypeDescription * pParamTypeDescr = 0;
105         TYPELIB_DANGER_GET( &pParamTypeDescr, rParam.pTypeRef );
106 
107 #ifdef CMC_DEBUG
108         fprintf(stderr, "arg %d of %d\n", nPos, nParams);
109 #endif
110 
111         if (!rParam.bOut && bridges::cpp_uno::shared::isSimpleType( pParamTypeDescr )) // value
112         {
113 #ifdef CMC_DEBUG
114             fprintf(stderr, "simple\n");
115 #endif
116 
117             switch (pParamTypeDescr->eTypeClass)
118             {
119                 case typelib_TypeClass_FLOAT:
120                 case typelib_TypeClass_DOUBLE:
121                     if (nf < s390x::MAX_SSE_REGS)
122                     {
123                         if (pParamTypeDescr->eTypeClass == typelib_TypeClass_FLOAT)
124                         {
125                             float tmp = (float) (*((double *)fpreg));
126                             (*((float *) fpreg)) = tmp;
127                         }
128 
129                         pCppArgs[nPos] = pUnoArgs[nPos] = fpreg++;
130                         nf++;
131                     }
132                     else
133                     {
134                         pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
135                         ovrflw++;
136                     }
137                     break;
138                 case typelib_TypeClass_BYTE:
139                 case typelib_TypeClass_BOOLEAN:
140                     if (ng < s390x::MAX_GPR_REGS)
141                     {
142                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)gpreg) + (sizeof(void*)-1));
143                         ng++;
144                         gpreg++;
145                     }
146                     else
147                     {
148                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + (sizeof(void*)-1));
149                         ovrflw++;
150                     }
151                     break;
152                 case typelib_TypeClass_CHAR:
153                 case typelib_TypeClass_SHORT:
154                 case typelib_TypeClass_UNSIGNED_SHORT:
155                     if (ng < s390x::MAX_GPR_REGS)
156                     {
157                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)gpreg) + (sizeof(void*)-2));
158                         ng++;
159                         gpreg++;
160                     }
161                     else
162                     {
163                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + (sizeof(void*)-2));
164                         ovrflw++;
165                     }
166                     break;
167                 case typelib_TypeClass_ENUM:
168                 case typelib_TypeClass_LONG:
169                 case typelib_TypeClass_UNSIGNED_LONG:
170                     if (ng < s390x::MAX_GPR_REGS)
171                     {
172                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)gpreg) + (sizeof(void*)-4));
173                         ng++;
174                         gpreg++;
175                     }
176                     else
177                     {
178                         pCppArgs[nPos] = pUnoArgs[nPos] = (((char *)ovrflw) + (sizeof(void*)-4));
179                         ovrflw++;
180                     }
181                     break;
182                 default:
183                     if (ng < s390x::MAX_GPR_REGS)
184                     {
185                         pCppArgs[nPos] = pUnoArgs[nPos] = gpreg++;
186                         ng++;
187                     }
188                     else
189                     {
190                         pCppArgs[nPos] = pUnoArgs[nPos] = ovrflw;
191                         ovrflw++;
192                     }
193                     break;
194             }
195 
196             // no longer needed
197             TYPELIB_DANGER_RELEASE( pParamTypeDescr );
198         }
199         else // ptr to complex value | ref
200         {
201 #ifdef CMC_DEBUG
202             fprintf(stderr, "complex, ng is %d\n", ng);
203 #endif
204 
205             void *pCppStack; //temporary stack pointer
206 
207             if (ng < s390x::MAX_GPR_REGS)
208             {
209                 pCppArgs[nPos] = pCppStack = *gpreg++;
210                 ng++;
211             }
212             else
213             {
214                 pCppArgs[nPos] = pCppStack = *ovrflw;
215                 ovrflw++;
216             }
217 
218             if (! rParam.bIn) // is pure out
219             {
220                 // uno out is unconstructed mem!
221                 pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize );
222                 pTempIndizes[nTempIndizes] = nPos;
223                 // will be released at reconversion
224                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
225             }
226             // is in/inout
227             else if (bridges::cpp_uno::shared::relatesToInterfaceType( pParamTypeDescr ))
228             {
229                 uno_copyAndConvertData( pUnoArgs[nPos] = alloca( pParamTypeDescr->nSize ),
230                                         pCppStack, pParamTypeDescr,
231                                         pThis->getBridge()->getCpp2Uno() );
232                 pTempIndizes[nTempIndizes] = nPos; // has to be reconverted
233                 // will be released at reconversion
234                 ppTempParamTypeDescr[nTempIndizes++] = pParamTypeDescr;
235             }
236             else // direct way
237             {
238                 pUnoArgs[nPos] = pCppStack;
239                 // no longer needed
240                 TYPELIB_DANGER_RELEASE( pParamTypeDescr );
241             }
242         }
243     }
244 
245 #ifdef CMC_DEBUG
246     fprintf(stderr, "end of params\n");
247 #endif
248 
249     // ExceptionHolder
250     uno_Any aUnoExc; // Any will be constructed by callee
251     uno_Any * pUnoExc = &aUnoExc;
252 
253     // invoke uno dispatch call
254     (*pThis->getUnoI()->pDispatcher)( pThis->getUnoI(), pMemberTypeDescr, pUnoReturn, pUnoArgs, &pUnoExc );
255 
256     // in case an exception occurred...
257     if (pUnoExc)
258     {
259         // destruct temporary in/inout params
260         for ( ; nTempIndizes--; )
261         {
262             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
263 
264             if (pParams[nIndex].bIn) // is in/inout => was constructed
265                 uno_destructData( pUnoArgs[nIndex], ppTempParamTypeDescr[nTempIndizes], 0 );
266             TYPELIB_DANGER_RELEASE( ppTempParamTypeDescr[nTempIndizes] );
267         }
268         if (pReturnTypeDescr)
269             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
270 
271         CPPU_CURRENT_NAMESPACE::raiseException( &aUnoExc, pThis->getBridge()->getUno2Cpp() ); // has to destruct the any
272         // is here for dummy
273         return typelib_TypeClass_VOID;
274     }
275     else // else no exception occurred...
276     {
277         // temporary params
278         for ( ; nTempIndizes--; )
279         {
280             sal_Int32 nIndex = pTempIndizes[nTempIndizes];
281             typelib_TypeDescription * pParamTypeDescr = ppTempParamTypeDescr[nTempIndizes];
282 
283             if (pParams[nIndex].bOut) // inout/out
284             {
285                 // convert and assign
286                 uno_destructData( pCppArgs[nIndex], pParamTypeDescr, cpp_release );
287                 uno_copyAndConvertData( pCppArgs[nIndex], pUnoArgs[nIndex], pParamTypeDescr,
288                                         pThis->getBridge()->getUno2Cpp() );
289             }
290             // destroy temp uno param
291             uno_destructData( pUnoArgs[nIndex], pParamTypeDescr, 0 );
292 
293             TYPELIB_DANGER_RELEASE( pParamTypeDescr );
294         }
295         // return
296         if (pCppReturn) // has complex return
297         {
298             if (pUnoReturn != pCppReturn) // needs reconversion
299             {
300                 uno_copyAndConvertData( pCppReturn, pUnoReturn, pReturnTypeDescr,
301                                         pThis->getBridge()->getUno2Cpp() );
302                 // destroy temp uno return
303                 uno_destructData( pUnoReturn, pReturnTypeDescr, 0 );
304             }
305             // complex return ptr is set to return reg
306             *(void **)pRegisterReturn = pCppReturn;
307         }
308         if (pReturnTypeDescr)
309         {
310             typelib_TypeClass eRet = (typelib_TypeClass)pReturnTypeDescr->eTypeClass;
311             TYPELIB_DANGER_RELEASE( pReturnTypeDescr );
312             return eRet;
313         }
314         else
315             return typelib_TypeClass_VOID;
316     }
317 }
318 
319 
320 //============================================================================
cpp_mediate(sal_uInt64 nOffsetAndIndex,void ** gpreg,void ** fpreg,void ** ovrflw,sal_Int64 * pRegisterReturn)321 static typelib_TypeClass cpp_mediate(
322     sal_uInt64 nOffsetAndIndex,
323     void ** gpreg, void ** fpreg, void ** ovrflw,
324     sal_Int64 * pRegisterReturn /* space for register return */ )
325 {
326     OSL_ENSURE( sizeof(sal_Int64)==sizeof(void *), "### unexpected!" );
327 
328     sal_Int32 nVtableOffset = (nOffsetAndIndex >> 32);
329     sal_Int32 nFunctionIndex = (nOffsetAndIndex & 0xFFFFFFFF);
330 
331 #ifdef CMC_DEBUG
332     fprintf(stderr, "nVTableOffset, nFunctionIndex are %x %x\n", nVtableOffset, nFunctionIndex);
333 #endif
334 
335 #ifdef CMC_DEBUG
336         // Let's figure out what is really going on here
337         {
338             fprintf( stderr, "= cpp_mediate () =\nGPR's (%d): ", 5 );
339             for ( unsigned int i = 0; i < 5; ++i )
340                 fprintf( stderr, "0x%lx, ", gpreg[i] );
341             fprintf( stderr, "\n");
342             fprintf( stderr, "\nFPR's (%d): ", 4 );
343             for ( unsigned int i = 0; i < 4; ++i )
344                 fprintf( stderr, "0x%lx (%f), ", fpreg[i], fpreg[i] );
345             fprintf( stderr, "\n");
346         }
347 #endif
348 
349 
350     // gpreg:  [ret *], this, [other gpr params]
351     // fpreg:  [fpr params]
352     // ovrflw: [gpr or fpr params (properly aligned)]
353 
354     // _this_ ptr is patched cppu_XInterfaceProxy object
355     void * pThis;
356     if( nFunctionIndex & 0x80000000 )
357     {
358         nFunctionIndex &= 0x7fffffff;
359         pThis = gpreg[1];
360     }
361     else
362     {
363         pThis = gpreg[0];
364     }
365 
366     pThis = static_cast< char * >(pThis) - nVtableOffset;
367 
368     bridges::cpp_uno::shared::CppInterfaceProxy * pCppI
369         = bridges::cpp_uno::shared::CppInterfaceProxy::castInterfaceToProxy(
370             pThis);
371 
372     typelib_InterfaceTypeDescription * pTypeDescr = pCppI->getTypeDescr();
373 
374 
375     OSL_ENSURE( nFunctionIndex < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
376     if (nFunctionIndex >= pTypeDescr->nMapFunctionIndexToMemberIndex)
377     {
378         throw RuntimeException(
379             rtl::OUString::createFromAscii("illegal vtable index!"),
380             (XInterface *)pCppI );
381     }
382 
383     // determine called method
384     OSL_ENSURE( nVtableCall < pTypeDescr->nMapFunctionIndexToMemberIndex, "### illegal vtable index!" );
385     sal_Int32 nMemberPos = pTypeDescr->pMapFunctionIndexToMemberIndex[nFunctionIndex];
386     OSL_ENSURE( nMemberPos < pTypeDescr->nAllMembers, "### illegal member index!" );
387 
388     TypeDescription aMemberDescr( pTypeDescr->ppAllMembers[nMemberPos] );
389 
390     typelib_TypeClass eRet;
391     switch (aMemberDescr.get()->eTypeClass)
392     {
393     case typelib_TypeClass_INTERFACE_ATTRIBUTE:
394     {
395         if (pTypeDescr->pMapMemberIndexToFunctionIndex[nMemberPos] == nFunctionIndex)
396         {
397             // is GET method
398             eRet = cpp2uno_call(
399                 pCppI, aMemberDescr.get(),
400                 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef,
401                 0, 0, // no params
402                 gpreg, fpreg, ovrflw, pRegisterReturn );
403         }
404         else
405         {
406             // is SET method
407             typelib_MethodParameter aParam;
408             aParam.pTypeRef =
409                 ((typelib_InterfaceAttributeTypeDescription *)aMemberDescr.get())->pAttributeTypeRef;
410             aParam.bIn      = sal_True;
411             aParam.bOut     = sal_False;
412 
413             eRet = cpp2uno_call(
414                 pCppI, aMemberDescr.get(),
415                 0, // indicates void return
416                 1, &aParam,
417                 gpreg, fpreg, ovrflw, pRegisterReturn );
418         }
419         break;
420     }
421     case typelib_TypeClass_INTERFACE_METHOD:
422     {
423         // is METHOD
424         switch (nFunctionIndex)
425         {
426         case 1: // acquire()
427             pCppI->acquireProxy(); // non virtual call!
428             eRet = typelib_TypeClass_VOID;
429             break;
430         case 2: // release()
431             pCppI->releaseProxy(); // non virtual call!
432             eRet = typelib_TypeClass_VOID;
433             break;
434         case 0: // queryInterface() opt
435         {
436             typelib_TypeDescription * pTD = 0;
437             TYPELIB_DANGER_GET( &pTD, reinterpret_cast< Type * >( gpreg[2] )->getTypeLibType() );
438             if (pTD)
439             {
440                 XInterface * pInterface = 0;
441                 (*pCppI->getBridge()->getCppEnv()->getRegisteredInterface)(
442                     pCppI->getBridge()->getCppEnv(),
443                     (void **)&pInterface, pCppI->getOid().pData,
444                     (typelib_InterfaceTypeDescription *)pTD );
445 
446                 if (pInterface)
447                 {
448                     ::uno_any_construct(
449                         reinterpret_cast< uno_Any * >( gpreg[0] ),
450                         &pInterface, pTD, cpp_acquire );
451                     pInterface->release();
452                     TYPELIB_DANGER_RELEASE( pTD );
453                     *(void **)pRegisterReturn = gpreg[0];
454                     eRet = typelib_TypeClass_ANY;
455                     break;
456                 }
457                 TYPELIB_DANGER_RELEASE( pTD );
458             }
459         } // else perform queryInterface()
460         default:
461             eRet = cpp2uno_call(
462                 pCppI, aMemberDescr.get(),
463                 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pReturnTypeRef,
464                 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->nParams,
465                 ((typelib_InterfaceMethodTypeDescription *)aMemberDescr.get())->pParams,
466                 gpreg, fpreg, ovrflw, pRegisterReturn );
467         }
468         break;
469     }
470     default:
471     {
472         throw RuntimeException(
473             rtl::OUString::createFromAscii("no member description found!"),
474             (XInterface *)pCppI );
475         // is here for dummy
476         eRet = typelib_TypeClass_VOID;
477     }
478     }
479 
480     return eRet;
481 }
482 
privateSnippetExecutor(long r2,long r3,long r4,long r5,long r6,long firstonstack)483 long privateSnippetExecutor(long r2, long r3, long r4, long r5, long r6, long firstonstack)
484 {
485     register long r0 asm("r0");
486     sal_uInt64 nOffsetAndIndex = r0;
487 
488     long sp = (long)&firstonstack;
489 
490     sal_uInt64 gpreg[s390x::MAX_GPR_REGS];
491     gpreg[0] = r2;
492     gpreg[1] = r3;
493     gpreg[2] = r4;
494     gpreg[3] = r5;
495     gpreg[4] = r6;
496 
497     double fpreg[s390x::MAX_SSE_REGS];
498     register double f0  asm("f0");  fpreg[0] = f0;
499     register double f2  asm("f2");  fpreg[1] = f2;
500     register double f4  asm("f4");  fpreg[2] = f4;
501     register double f6  asm("f6");  fpreg[3] = f6;
502 
503     volatile long nRegReturn[1];
504 #ifdef CMC_DEBUG
505     fprintf(stderr, "before mediate with %lx\n",nOffsetAndIndex);
506     fprintf(stderr, "doubles are %f %f %f %f\n", fpreg[0], fpreg[1], fpreg[2], fpreg[3]);
507 #endif
508     typelib_TypeClass aType =
509         cpp_mediate( nOffsetAndIndex, (void**)gpreg, (void**)fpreg, (void**)sp,
510             (sal_Int64*)nRegReturn );
511 #ifdef CMC_DEBUG
512     fprintf(stderr, "after mediate ret is %lx %ld\n", nRegReturn[0], nRegReturn[0]);
513 #endif
514 
515     switch( aType )
516     {
517         case typelib_TypeClass_BOOLEAN:
518         case typelib_TypeClass_BYTE:
519             nRegReturn[0] = (unsigned long)(*(unsigned char *)nRegReturn);
520             break;
521         case typelib_TypeClass_CHAR:
522         case typelib_TypeClass_UNSIGNED_SHORT:
523         case typelib_TypeClass_SHORT:
524             nRegReturn[0] = (unsigned long)(*(unsigned short *)nRegReturn);
525             break;
526         case typelib_TypeClass_ENUM:
527         case typelib_TypeClass_UNSIGNED_LONG:
528         case typelib_TypeClass_LONG:
529             nRegReturn[0] = (unsigned long)(*(unsigned int *)nRegReturn);
530             break;
531         case typelib_TypeClass_VOID:
532         default:
533             break;
534         case typelib_TypeClass_FLOAT:
535             {
536                 double tmp = (double) (*((float *)nRegReturn));
537                 (*((double *) nRegReturn)) = tmp;
538             }
539             //deliberate fall through
540         case typelib_TypeClass_DOUBLE:
541             __asm__ ( "ld 0,%0\n\t"
542                 : : "m" (*((double*)nRegReturn)) );
543             break;
544     }
545     return nRegReturn[0];
546 }
547 
548 const int codeSnippetSize = 32;
549 
codeSnippet(unsigned char * code,sal_Int32 nFunctionIndex,sal_Int32 nVtableOffset,bool simple_ret_type)550 unsigned char *codeSnippet( unsigned char * code, sal_Int32 nFunctionIndex, sal_Int32 nVtableOffset, bool simple_ret_type )
551 {
552     sal_uInt64 nOffsetAndIndex = ( ( (sal_uInt64) nVtableOffset ) << 32 ) | ( (sal_Int64) nFunctionIndex );
553 
554     if (! simple_ret_type)
555         nOffsetAndIndex |= 0x80000000;
556 
557     unsigned char * p = code;
558     *(short *)&p[0] = 0x0d10;   /* basr %r1,0 */
559     *(short *)&p[2] = 0xeb01;   /* lmg %r0,%r1,14(%r1) */
560     *(short *)&p[4] = 0x100e;
561     *(short *)&p[6] = 0x0004;
562     *(short *)&p[8] = 0x07f1;   /* br %r1 */
563     *(long  *)&p[16] = (long)nOffsetAndIndex;
564     *(long  *)&p[24] = (long)&privateSnippetExecutor;
565     return (code + codeSnippetSize);
566 }
567 }
568 
flushCode(unsigned char const *,unsigned char const *)569 void bridges::cpp_uno::shared::VtableFactory::flushCode(unsigned char const *, unsigned char const *)
570 {
571 }
572 
573 struct bridges::cpp_uno::shared::VtableFactory::Slot { void * fn; };
574 
575 bridges::cpp_uno::shared::VtableFactory::Slot *
mapBlockToVtable(void * block)576 bridges::cpp_uno::shared::VtableFactory::mapBlockToVtable(void * block)
577 {
578     return static_cast< Slot * >(block) + 2;
579 }
580 
getBlockSize(sal_Int32 slotCount)581 sal_Size bridges::cpp_uno::shared::VtableFactory::getBlockSize(
582     sal_Int32 slotCount)
583 {
584     return (slotCount + 2) * sizeof (Slot) + slotCount * codeSnippetSize;
585 }
586 
587 bridges::cpp_uno::shared::VtableFactory::Slot *
initializeBlock(void * block,sal_Int32 slotCount)588 bridges::cpp_uno::shared::VtableFactory::initializeBlock(
589     void * block, sal_Int32 slotCount)
590 {
591     Slot * slots = mapBlockToVtable(block);
592     slots[-2].fn = 0;
593     slots[-1].fn = 0;
594     return slots + slotCount;
595 }
596 
addLocalFunctions(Slot ** slots,unsigned char * code,sal_PtrDiff writetoexecdiff,typelib_InterfaceTypeDescription const * type,sal_Int32 functionOffset,sal_Int32 functionCount,sal_Int32 vtableOffset)597 unsigned char * bridges::cpp_uno::shared::VtableFactory::addLocalFunctions(
598     Slot ** slots, unsigned char * code, sal_PtrDiff writetoexecdiff,
599     typelib_InterfaceTypeDescription const * type, sal_Int32 functionOffset,
600     sal_Int32 functionCount, sal_Int32 vtableOffset)
601 {
602     (*slots) -= functionCount;
603     Slot * s = *slots;
604 #ifdef CMC_DEBUG
605     fprintf(stderr, "in addLocalFunctions functionOffset is %x\n",functionOffset);
606     fprintf(stderr, "in addLocalFunctions vtableOffset is %x\n",vtableOffset);
607 #endif
608 
609     for (sal_Int32 i = 0; i < type->nMembers; ++i) {
610         typelib_TypeDescription * member = 0;
611         TYPELIB_DANGER_GET(&member, type->ppMembers[i]);
612         OSL_ASSERT(member != 0);
613         switch (member->eTypeClass) {
614         case typelib_TypeClass_INTERFACE_ATTRIBUTE:
615             // Getter:
616             (s++)->fn = code + writetoexecdiff;
617             code = codeSnippet(
618                 code, functionOffset++, vtableOffset,
619                 bridges::cpp_uno::shared::isSimpleType(
620                     reinterpret_cast<
621                     typelib_InterfaceAttributeTypeDescription * >(
622                         member)->pAttributeTypeRef));
623 
624             // Setter:
625             if (!reinterpret_cast<
626                 typelib_InterfaceAttributeTypeDescription * >(
627                     member)->bReadOnly)
628             {
629                 (s++)->fn = code + writetoexecdiff;
630                 code = codeSnippet(code, functionOffset++, vtableOffset, true);
631             }
632             break;
633 
634         case typelib_TypeClass_INTERFACE_METHOD:
635             (s++)->fn = code + writetoexecdiff;
636             code = codeSnippet(
637                 code, functionOffset++, vtableOffset,
638                 bridges::cpp_uno::shared::isSimpleType(
639                     reinterpret_cast<
640                     typelib_InterfaceMethodTypeDescription * >(
641                         member)->pReturnTypeRef));
642             break;
643 
644         default:
645             OSL_ASSERT(false);
646             break;
647         }
648         TYPELIB_DANGER_RELEASE(member);
649     }
650     return code;
651 }
652 
653 /* vi:set tabstop=4 shiftwidth=4 expandtab: */
654