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