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 /* Prototype specifies unmangled function name and disables unused warning */
11 static nsresult
12 PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, uint64_t* args)
13 __asm__("PrepareAndDispatch") ATTRIBUTE_USED;
14 
15 static nsresult
PrepareAndDispatch(nsXPTCStubBase * self,uint32_t methodIndex,uint64_t * args)16 PrepareAndDispatch(nsXPTCStubBase* self, uint32_t methodIndex, uint64_t* args)
17 {
18     const uint8_t PARAM_BUFFER_COUNT = 16;
19     const uint8_t NUM_ARG_REGS = 6-1;        // -1 for "this" pointer
20 
21     nsXPTCMiniVariant paramBuffer[PARAM_BUFFER_COUNT];
22     nsXPTCMiniVariant* dispatchParams = nullptr;
23     const nsXPTMethodInfo* info;
24     uint8_t paramCount;
25     uint8_t i;
26 
27     NS_ASSERTION(self,"no self");
28 
29     self->mEntry->GetMethodInfo(uint16_t(methodIndex), &info);
30 
31     paramCount = info->GetParamCount();
32 
33     // setup variant array pointer
34     if(paramCount > PARAM_BUFFER_COUNT)
35         dispatchParams = new nsXPTCMiniVariant[paramCount];
36     else
37         dispatchParams = paramBuffer;
38     NS_ASSERTION(dispatchParams,"no place for params");
39 
40     const uint8_t indexOfJSContext = info->IndexOfJSContext();
41 
42     // args[0] to args[NUM_ARG_REGS] hold floating point register values
43     uint64_t* ap = args + NUM_ARG_REGS;
44     for(i = 0; i < paramCount; i++, ap++)
45     {
46         const nsXPTParamInfo& param = info->GetParam(i);
47         const nsXPTType& type = param.GetType();
48         nsXPTCMiniVariant* dp = &dispatchParams[i];
49 
50         if (i == indexOfJSContext)
51             ap++;
52 
53         if(param.IsOut() || !type.IsArithmetic())
54         {
55             dp->val.p = (void*) *ap;
56             continue;
57         }
58         // else
59         switch(type)
60         {
61         case nsXPTType::T_I8     : dp->val.i8  = (int8_t)    *ap;    break;
62         case nsXPTType::T_I16    : dp->val.i16 = (int16_t)   *ap;    break;
63         case nsXPTType::T_I32    : dp->val.i32 = (int32_t)   *ap;    break;
64         case nsXPTType::T_I64    : dp->val.i64 = (int64_t)   *ap;    break;
65         case nsXPTType::T_U8     : dp->val.u8  = (uint8_t)   *ap;    break;
66         case nsXPTType::T_U16    : dp->val.u16 = (uint16_t)  *ap;    break;
67         case nsXPTType::T_U32    : dp->val.u32 = (uint32_t)  *ap;    break;
68         case nsXPTType::T_U64    : dp->val.u64 = (uint64_t)  *ap;    break;
69         case nsXPTType::T_FLOAT  :
70             if(i < NUM_ARG_REGS)
71             {
72                 // floats passed via registers are stored as doubles
73                 // in the first NUM_ARG_REGS entries in args
74                 dp->val.u64 = (uint64_t) args[i];
75                 dp->val.f = (float) dp->val.d;    // convert double to float
76             }
77             else
78                 dp->val.u32 = (uint32_t) *ap;
79             break;
80         case nsXPTType::T_DOUBLE :
81             // doubles passed via registers are also stored
82             // in the first NUM_ARG_REGS entries in args
83             dp->val.u64 = (i < NUM_ARG_REGS) ? args[i] : *ap;
84             break;
85         case nsXPTType::T_BOOL   : dp->val.b   = (bool)    *ap;    break;
86         case nsXPTType::T_CHAR   : dp->val.c   = (char)      *ap;    break;
87         case nsXPTType::T_WCHAR  : dp->val.wc  = (char16_t) *ap;    break;
88         default:
89             NS_ERROR("bad type");
90             break;
91         }
92     }
93 
94     nsresult result = self->mOuter->CallMethod((uint16_t)methodIndex, info,
95                                                dispatchParams);
96 
97     if(dispatchParams != paramBuffer)
98         delete [] dispatchParams;
99 
100     return result;
101 }
102 
103 /*
104  * SharedStub()
105  *  Collects arguments and calls PrepareAndDispatch.  The "methodIndex" is
106  *  passed to this function via $1 to preserve the argument registers.
107  */
108 __asm__(
109     "#### SharedStub ####\n"
110 ".text\n\t"
111     ".align 5\n\t"
112     ".ent SharedStub\n"
113 "SharedStub:\n\t"
114     ".frame $30,96,$26,0\n\t"
115     ".mask 0x4000000,-96\n\t"
116     "ldgp $29,0($27)\n"
117 "$SharedStub..ng:\n\t"
118     "subq $30,96,$30\n\t"
119     "stq $26,0($30)\n\t"
120     ".prologue 1\n\t"
121 
122     /*
123      * Store arguments passed via registers to the stack.
124      * Floating point registers are stored as doubles and converted
125      * to floats in PrepareAndDispatch if necessary.
126      */
127     "stt $f17,16($30)\n\t"   /* floating point registers */
128     "stt $f18,24($30)\n\t"
129     "stt $f19,32($30)\n\t"
130     "stt $f20,40($30)\n\t"
131     "stt $f21,48($30)\n\t"
132     "stq $17,56($30)\n\t"    /* integer registers */
133     "stq $18,64($30)\n\t"
134     "stq $19,72($30)\n\t"
135     "stq $20,80($30)\n\t"
136     "stq $21,88($30)\n\t"
137 
138     /*
139      * Call PrepareAndDispatch function.
140      */
141     "bis $1,$1,$17\n\t"      /* pass "methodIndex" */
142     "addq $30,16,$18\n\t"    /* pass "args" */
143     "bsr $26,$PrepareAndDispatch..ng\n\t"
144 
145     "ldq $26,0($30)\n\t"
146     "addq $30,96,$30\n\t"
147     "ret $31,($26),1\n\t"
148     ".end SharedStub"
149     );
150 
151 /*
152  * nsresult nsXPTCStubBase::Stub##n()
153  *  Sets register $1 to "methodIndex" and jumps to SharedStub.
154  */
155 #define STUB_MANGLED_ENTRY(n, symbol) \
156     "#### Stub"#n" ####"      "\n\t" \
157     ".text"                   "\n\t" \
158     ".align 5"                "\n\t" \
159     ".globl " symbol          "\n\t" \
160     ".ent " symbol            "\n"   \
161 symbol ":"                    "\n\t" \
162     ".frame $30,0,$26,0"      "\n\t" \
163     "ldgp $29,0($27)"         "\n"   \
164 "$" symbol "..ng:"            "\n\t" \
165     ".prologue 1"             "\n\t" \
166     "lda $1,"#n               "\n\t" \
167     "br $31,$SharedStub..ng"  "\n\t" \
168     ".end " symbol
169 
170 #define STUB_ENTRY(n) \
171 __asm__( \
172     ".if "#n" < 10"                                              "\n\t" \
173         STUB_MANGLED_ENTRY(n, "_ZN14nsXPTCStubBase5Stub"#n"Ev")  "\n\t" \
174     ".elseif "#n" < 100"                                         "\n\t" \
175         STUB_MANGLED_ENTRY(n, "_ZN14nsXPTCStubBase6Stub"#n"Ev")  "\n\t" \
176     ".elseif "#n" < 1000"                                        "\n\t" \
177         STUB_MANGLED_ENTRY(n, "_ZN14nsXPTCStubBase7Stub"#n"Ev")  "\n\t" \
178     ".else"                                                      "\n\t" \
179     ".err \"Stub"#n" >= 1000 not yet supported.\""               "\n\t" \
180     ".endif" \
181     );
182 
183 
184 #define SENTINEL_ENTRY(n) \
185 nsresult nsXPTCStubBase::Sentinel##n() \
186 { \
187     NS_ERROR("nsXPTCStubBase::Sentinel called"); \
188     return NS_ERROR_NOT_IMPLEMENTED; \
189 }
190 
191 #include "xptcstubsdef.inc"
192