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