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