1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 // Implement shared vtbl methods.
7 
8 #include "xptcprivate.h"
9 #include "xptiprivate.h"
10 
11 // The Linux/PPC ABI (aka PPC/SYSV ABI) passes the first 8 integral
12 // parameters and the first 8 floating point parameters in registers
13 // (r3-r10 and f1-f8), no stack space is allocated for these by the
14 // caller.  The rest of the parameters are passed in the callers stack
15 // area. The stack pointer has to retain 16-byte alignment, longlongs
16 // and doubles are aligned on 8-byte boundaries.
17 
18 #define PARAM_BUFFER_COUNT     16
19 #define GPR_COUNT               8
20 #define FPR_COUNT               8
21 
22 // PrepareAndDispatch() is called by SharedStub() and calls the actual method.
23 //
24 // - 'args[]' contains the arguments passed on stack
25 // - 'gprData[]' contains the arguments passed in integer registers
26 // - 'fprData[]' contains the arguments passed in floating point registers
27 //
28 // The parameters are mapped into an array of type 'nsXPTCMiniVariant'
29 // and then the method gets called.
30 
31 extern "C" nsresult ATTRIBUTE_USED
PrepareAndDispatch(nsXPTCStubBase * self,uint32_t methodIndex,uint32_t * args,uint32_t * gprData,double * fprData)32 PrepareAndDispatch(nsXPTCStubBase* self,
33                    uint32_t methodIndex,
34                    uint32_t* args,
35                    uint32_t *gprData,
36                    double *fprData)
37 {
38     nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT];
39     nsXPTCMiniVariant* dispatchParams = nullptr;
40     const nsXPTMethodInfo* info = nullptr;
41     uint32_t paramCount;
42     uint32_t i;
43     nsresult result = NS_ERROR_FAILURE;
44 
45     NS_ASSERTION(self,"no self");
46 
47     self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info);
48     NS_ASSERTION(info,"no method info");
49     if (! info)
50         return NS_ERROR_UNEXPECTED;
51 
52     paramCount = info->GetParamCount();
53 
54     // setup variant array pointer
55     if(paramCount > PARAM_BUFFER_COUNT)
56         dispatchParams = new nsXPTCMiniVariant[paramCount];
57     else
58         dispatchParams = paramBuffer;
59 
60     NS_ASSERTION(dispatchParams,"no place for params");
61     if (!dispatchParams)
62         return NS_ERROR_OUT_OF_MEMORY;
63 
64     uint32_t* ap = args;
65     uint32_t gpr = 1;    // skip one GPR register
66     uint32_t fpr = 0;
67     uint32_t tempu32;
68     uint64_t tempu64;
69 
70     for(i = 0; i < paramCount; i++) {
71         const nsXPTParamInfo& param = info->GetParam(i);
72         const nsXPTType& type = param.GetType();
73         nsXPTCMiniVariant* dp = &dispatchParams[i];
74 
75         if (!param.IsOut() && type == nsXPTType::T_DOUBLE) {
76             if (fpr < FPR_COUNT)
77                 dp->val.d = fprData[fpr++];
78             else {
79                 if ((uint32_t) ap & 4) ap++; // doubles are 8-byte aligned on stack
80                 dp->val.d = *(double*) ap;
81                 ap += 2;
82             }
83             continue;
84         }
85         else if (!param.IsOut() && type == nsXPTType::T_FLOAT) {
86             if (fpr < FPR_COUNT)
87                 dp->val.f = (float) fprData[fpr++]; // in registers floats are passed as doubles
88             else
89                 dp->val.f = *(float*) ap++;
90             continue;
91         }
92         else if (!param.IsOut() && (type == nsXPTType::T_I64
93                                     || type == nsXPTType::T_U64)) {
94             if (gpr & 1) gpr++; // longlongs are aligned in odd/even register pairs, eg. r5/r6
95             if ((gpr + 1) < GPR_COUNT) {
96                 tempu64 = *(uint64_t*) &gprData[gpr];
97                 gpr += 2;
98             }
99             else {
100                 if ((uint32_t) ap & 4) ap++; // longlongs are 8-byte aligned on stack
101                 tempu64 = *(uint64_t*) ap;
102                 ap += 2;
103             }
104         }
105         else {
106             if (gpr < GPR_COUNT)
107                 tempu32 = gprData[gpr++];
108             else
109                 tempu32 = *ap++;
110         }
111 
112         if(param.IsOut() || !type.IsArithmetic()) {
113             if (type == nsXPTType::T_JSVAL)
114                 dp->val.p = *((void**) tempu32);
115             else
116                 dp->val.p = (void*) tempu32;
117             continue;
118         }
119 
120         switch(type) {
121         case nsXPTType::T_I8:      dp->val.i8  = (int8_t)   tempu32; break;
122         case nsXPTType::T_I16:     dp->val.i16 = (int16_t)  tempu32; break;
123         case nsXPTType::T_I32:     dp->val.i32 = (int32_t)  tempu32; break;
124         case nsXPTType::T_I64:     dp->val.i64 = (int64_t)  tempu64; break;
125         case nsXPTType::T_U8:      dp->val.u8  = (uint8_t)  tempu32; break;
126         case nsXPTType::T_U16:     dp->val.u16 = (uint16_t) tempu32; break;
127         case nsXPTType::T_U32:     dp->val.u32 = (uint32_t) tempu32; break;
128         case nsXPTType::T_U64:     dp->val.u64 = (uint64_t) tempu64; break;
129         case nsXPTType::T_BOOL:    dp->val.b   = (bool)     tempu32; break;
130         case nsXPTType::T_CHAR:    dp->val.c   = (char)     tempu32; break;
131         case nsXPTType::T_WCHAR:   dp->val.wc  = (wchar_t)  tempu32; break;
132 
133         default:
134             NS_ERROR("bad type");
135             break;
136         }
137     }
138 
139     result = self->mOuter->CallMethod((uint16_t)methodIndex,
140                                       info,
141                                       dispatchParams);
142 
143     if (dispatchParams != paramBuffer)
144         delete [] dispatchParams;
145 
146     return result;
147 }
148 
149 // Load r11 with the constant 'n' and branch to SharedStub().
150 //
151 // XXX Yes, it's ugly that we're relying on gcc's name-mangling here;
152 // however, it's quick, dirty, and'll break when the ABI changes on
153 // us, which is what we want ;-).
154 
155 
156 // gcc-3 version
157 //
158 // As G++3 ABI contains the length of the functionname in the mangled
159 // name, it is difficult to get a generic assembler mechanism like
160 // in the G++ 2.95 case.
161 // Create names would be like:
162 // _ZN14nsXPTCStubBase5Stub1Ev
163 // _ZN14nsXPTCStubBase6Stub12Ev
164 // _ZN14nsXPTCStubBase7Stub123Ev
165 // _ZN14nsXPTCStubBase8Stub1234Ev
166 // etc.
167 // Use assembler directives to get the names right...
168 
169 # define STUB_ENTRY(n)							\
170 __asm__ (								\
171 	".align	2 \n\t"							\
172 	".if	"#n" < 10 \n\t"						\
173 	".globl	_ZN14nsXPTCStubBase5Stub"#n"Ev \n\t"			\
174 	".type	_ZN14nsXPTCStubBase5Stub"#n"Ev,@function \n\n"		\
175 "_ZN14nsXPTCStubBase5Stub"#n"Ev: \n\t"					\
176 									\
177 	".elseif "#n" < 100 \n\t"					\
178 	".globl	_ZN14nsXPTCStubBase6Stub"#n"Ev \n\t"			\
179 	".type	_ZN14nsXPTCStubBase6Stub"#n"Ev,@function \n\n"		\
180 "_ZN14nsXPTCStubBase6Stub"#n"Ev: \n\t"					\
181 									\
182 	".elseif "#n" < 1000 \n\t"					\
183 	".globl	_ZN14nsXPTCStubBase7Stub"#n"Ev \n\t"			\
184 	".type	_ZN14nsXPTCStubBase7Stub"#n"Ev,@function \n\n"		\
185 "_ZN14nsXPTCStubBase7Stub"#n"Ev: \n\t"					\
186 									\
187 	".else \n\t"							\
188 	".err	\"stub number "#n" >= 1000 not yet supported\"\n"	\
189 	".endif \n\t"							\
190 									\
191 	"li	11,"#n" \n\t"						\
192 	"b	SharedStub@local \n"					\
193 );
194 
195 #define SENTINEL_ENTRY(n)                            \
196 nsresult nsXPTCStubBase::Sentinel##n()               \
197 {                                                    \
198   NS_ERROR("nsXPTCStubBase::Sentinel called"); \
199   return NS_ERROR_NOT_IMPLEMENTED;                   \
200 }
201 
202 #include "xptcstubsdef.inc"
203