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