Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/. */
6 /* Implement shared vtbl methods. */
8 #include "xptcprivate.h"
9 #include "xptiprivate.h"
11 #ifdef __GNUC__
12 /* This tells gcc3.4+ not to optimize away symbols.
13 * @see http://gcc.gnu.org/gcc-3.4/changes.html
14 */
15 #define DONT_DROP_OR_WARN __attribute__((used))
16 #else
17 /* This tells older gccs not to warn about unused vairables.
18 * @see http://docs.freebsd.org/info/gcc/gcc.info.Variable_Attributes.html
19 */
20 #define DONT_DROP_OR_WARN __attribute__((unused))
21 #endif
23 /* Specify explicitly a symbol for this function, don't try to guess the c++ mangled symbol. */
24 static nsresult PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, uint32_t* args) asm("_PrepareAndDispatch")
25 DONT_DROP_OR_WARN;
27 static nsresult ATTRIBUTE_USED
28 PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, uint32_t* args)
29 {
30 #define PARAM_BUFFER_COUNT 16
32 nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT];
33 nsXPTCMiniVariant* dispatchParams = nullptr;
34 const nsXPTMethodInfo* info;
35 uint8_t paramCount;
36 uint8_t i;
37 nsresult result = NS_ERROR_FAILURE;
39 NS_ASSERTION(self,"no self");
41 self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info);
42 paramCount = info->GetParamCount();
44 // setup variant array pointer
45 if(paramCount > PARAM_BUFFER_COUNT)
46 dispatchParams = new nsXPTCMiniVariant[paramCount];
47 else
48 dispatchParams = paramBuffer;
50 NS_ASSERTION(dispatchParams,"no place for params");
51 if (!dispatchParams)
52 return NS_ERROR_OUT_OF_MEMORY;
54 uint32_t* ap = args;
55 for(i = 0; i < paramCount; i++, ap++)
56 {
57 const nsXPTParamInfo& param = info->GetParam(i);
58 const nsXPTType& type = param.GetType();
59 nsXPTCMiniVariant* dp = &dispatchParams[i];
61 if(param.IsOut() || !type.IsArithmetic())
62 {
63 dp->val.p = (void*) *ap;
64 continue;
65 }
66 // else
67 switch(type)
68 {
69 case nsXPTType::T_I8 : dp->val.i8 = *((int8_t*) ap); break;
70 case nsXPTType::T_I16 : dp->val.i16 = *((int16_t*) ap); break;
71 case nsXPTType::T_I32 : dp->val.i32 = *((int32_t*) ap); break;
72 case nsXPTType::T_I64 : dp->val.i64 = *((int64_t*) ap); ap++; break;
73 case nsXPTType::T_U8 : dp->val.u8 = *((uint8_t*) ap); break;
74 case nsXPTType::T_U16 : dp->val.u16 = *((uint16_t*)ap); break;
75 case nsXPTType::T_U32 : dp->val.u32 = *((uint32_t*)ap); break;
76 case nsXPTType::T_U64 : dp->val.u64 = *((uint64_t*)ap); ap++; break;
77 case nsXPTType::T_FLOAT : dp->val.f = *((float*) ap); break;
78 case nsXPTType::T_DOUBLE : dp->val.d = *((double*) ap); ap++; break;
79 case nsXPTType::T_BOOL : dp->val.b = *((bool*) ap); break;
80 case nsXPTType::T_CHAR : dp->val.c = *((char*) ap); break;
81 case nsXPTType::T_WCHAR : dp->val.wc = *((wchar_t*) ap); break;
82 default:
83 NS_ERROR("bad type");
84 break;
85 }
86 }
88 result = self->mOuter->CallMethod((uint16_t)methodIndex, info, dispatchParams);
90 if(dispatchParams != paramBuffer)
91 delete [] dispatchParams;
93 return result;
94 }
96 /*
97 * This is our shared stub.
98 *
99 * r0 = Self.
100 *
101 * The Rules:
102 * We pass an (undefined) number of arguments into this function.
103 * The first 3 C++ arguments are in r1 - r3, the rest are built
104 * by the calling function on the stack.
105 *
106 * We are allowed to corrupt r0 - r3, ip, and lr.
107 *
108 * Other Info:
109 * We pass the stub number in using `ip'.
110 *
111 * Implementation:
112 * - We save r1 to r3 inclusive onto the stack, which will be
113 * immediately below the caller saved arguments.
114 * - setup r2 (PrepareAndDispatch's args pointer) to point at
115 * the base of all these arguments
116 * - Save LR (for the return address)
117 * - Set r1 (PrepareAndDispatch's methodindex argument) from ip
118 * - r0 is passed through (self)
119 * - Call PrepareAndDispatch
120 * - When the call returns, we return by loading the PC off the
121 * stack, and undoing the stack (one instruction)!
122 *
123 */
124 __asm__ ("\n\
125 .text \n\
126 .align 2 \n\
127 SharedStub: \n\
128 stmfd sp!, {r1, r2, r3} \n\
129 mov r2, sp \n\
130 str lr, [sp, #-4]! \n\
131 mov r1, ip \n\
132 bl _PrepareAndDispatch \n\
133 ldr pc, [sp], #16");
135 /*
136 * Create sets of stubs to call the SharedStub.
137 * We don't touch the stack here, nor any registers, other than IP.
138 * IP is defined to be corruptable by a called function, so we are
139 * safe to use it.
140 *
141 * This will work with or without optimisation.
142 */
144 /*
145 * Note : As G++3 ABI contains the length of the functionname in the
146 * mangled name, it is difficult to get a generic assembler mechanism like
147 * in the G++ 2.95 case.
148 * Create names would be like :
149 * _ZN14nsXPTCStubBase5Stub9Ev
150 * _ZN14nsXPTCStubBase6Stub13Ev
151 * _ZN14nsXPTCStubBase7Stub144Ev
152 * Use the assembler directives to get the names right...
153 */
155 #define STUB_ENTRY(n) \
156 __asm__( \
157 ".section \".text\"\n" \
158 " .align 2\n" \
159 " .iflt ("#n" - 10)\n" \
160 " .globl _ZN14nsXPTCStubBase5Stub"#n"Ev\n" \
161 " .type _ZN14nsXPTCStubBase5Stub"#n"Ev,#function\n" \
162 "_ZN14nsXPTCStubBase5Stub"#n"Ev:\n" \
163 " .else\n" \
164 " .iflt ("#n" - 100)\n" \
165 " .globl _ZN14nsXPTCStubBase6Stub"#n"Ev\n" \
166 " .type _ZN14nsXPTCStubBase6Stub"#n"Ev,#function\n" \
167 "_ZN14nsXPTCStubBase6Stub"#n"Ev:\n" \
168 " .else\n" \
169 " .iflt ("#n" - 1000)\n" \
170 " .globl _ZN14nsXPTCStubBase7Stub"#n"Ev\n" \
171 " .type _ZN14nsXPTCStubBase7Stub"#n"Ev,#function\n" \
172 "_ZN14nsXPTCStubBase7Stub"#n"Ev:\n" \
173 " .else\n" \
174 " .err \"stub number "#n"> 1000 not yet supported\"\n" \
175 " .endif\n" \
176 " .endif\n" \
177 " .endif\n" \
178 " mov ip, #"#n"\n" \
179 " b SharedStub\n\t");
181 #if 0
182 /*
183 * This part is left in as comment : this is how the method definition
184 * should look like.
185 */
187 #define STUB_ENTRY(n) \
188 nsresult nsXPTCStubBase::Stub##n () \
189 { \
190 __asm__ ( \
191 " mov ip, #"#n"\n" \
192 " b SharedStub\n\t"); \
193 return 0; /* avoid warnings */ \
194 }
195 #endif
198 #define SENTINEL_ENTRY(n) \
199 nsresult nsXPTCStubBase::Sentinel##n() \
200 { \
201 NS_ERROR("nsXPTCStubBase::Sentinel called"); \
202 return NS_ERROR_NOT_IMPLEMENTED; \
203 }
205 #include "xptcstubsdef.inc"