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 /* Platform specific code to invoke XPCOM methods on native objects */
7 
8 #include "xptcprivate.h"
9 
10 /* Prototype specifies unmangled function name and disables unused warning */
11 static void
12 invoke_copy_to_stack(uint64_t* d, uint32_t paramCount, nsXPTCVariant* s)
13 __asm__("invoke_copy_to_stack") __attribute__((used));
14 
15 static void
invoke_copy_to_stack(uint64_t * d,uint32_t paramCount,nsXPTCVariant * s)16 invoke_copy_to_stack(uint64_t* d, uint32_t paramCount, nsXPTCVariant* s)
17 {
18     const uint8_t NUM_ARG_REGS = 6-1;        // -1 for "this" pointer
19 
20     for(uint32_t i = 0; i < paramCount; i++, d++, s++)
21     {
22         if(s->IsPtrData())
23         {
24             *d = (uint64_t)s->ptr;
25             continue;
26         }
27         switch(s->type)
28         {
29         case nsXPTType::T_I8     : *d = (uint64_t)s->val.i8;     break;
30         case nsXPTType::T_I16    : *d = (uint64_t)s->val.i16;    break;
31         case nsXPTType::T_I32    : *d = (uint64_t)s->val.i32;    break;
32         case nsXPTType::T_I64    : *d = (uint64_t)s->val.i64;    break;
33         case nsXPTType::T_U8     : *d = (uint64_t)s->val.u8;     break;
34         case nsXPTType::T_U16    : *d = (uint64_t)s->val.u16;    break;
35         case nsXPTType::T_U32    : *d = (uint64_t)s->val.u32;    break;
36         case nsXPTType::T_U64    : *d = (uint64_t)s->val.u64;    break;
37         case nsXPTType::T_FLOAT  :
38             if(i < NUM_ARG_REGS)
39             {
40                 // convert floats to doubles if they are to be passed
41                 // via registers so we can just deal with doubles later
42                 union { uint64_t u64; double d; } t;
43                 t.d = (double)s->val.f;
44                 *d = t.u64;
45             }
46             else
47                 // otherwise copy to stack normally
48                 *d = (uint64_t)s->val.u32;
49             break;
50         case nsXPTType::T_DOUBLE : *d = (uint64_t)s->val.u64;    break;
51         case nsXPTType::T_BOOL   : *d = (uint64_t)s->val.b;      break;
52         case nsXPTType::T_CHAR   : *d = (uint64_t)s->val.c;      break;
53         case nsXPTType::T_WCHAR  : *d = (uint64_t)s->val.wc;     break;
54         default:
55             // all the others are plain pointer types
56             *d = (uint64_t)s->val.p;
57             break;
58         }
59     }
60 }
61 
62 /*
63  * EXPORT_XPCOM_API(nsresult)
64  * NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex,
65  *                  uint32_t paramCount, nsXPTCVariant* params)
66  */
67 __asm__(
68     "#### NS_InvokeByIndex ####\n"
69 ".text\n\t"
70     ".align 5\n\t"
71     ".globl NS_InvokeByIndex\n\t"
72     ".ent NS_InvokeByIndex\n"
73 "NS_InvokeByIndex:\n\t"
74     ".frame $15,32,$26,0\n\t"
75     ".mask 0x4008000,-32\n\t"
76     "ldgp $29,0($27)\n"
77 "$NS_InvokeByIndex..ng:\n\t"
78     "subq $30,32,$30\n\t"
79     "stq $26,0($30)\n\t"
80     "stq $15,8($30)\n\t"
81     "bis $30,$30,$15\n\t"
82     ".prologue 1\n\t"
83 
84     /*
85      * Allocate enough stack space to hold the greater of 6 or "paramCount"+1
86      * parameters. (+1 for "this" pointer)  Room for at least 6 parameters
87      * is required for storage of those passed via registers.
88      */
89 
90     "bis $31,5,$2\n\t"      /* count = MAX(5, "paramCount") */
91     "cmplt $2,$18,$1\n\t"
92     "cmovne $1,$18,$2\n\t"
93     "s8addq $2,16,$1\n\t"   /* room for count+1 params (8 bytes each) */
94     "bic $1,15,$1\n\t"      /* stack space is rounded up to 0 % 16 */
95     "subq $30,$1,$30\n\t"
96 
97     "stq $16,0($30)\n\t"    /* save "that" (as "this" pointer) */
98     "stq $17,16($15)\n\t"   /* save "methodIndex" */
99 
100     "addq $30,8,$16\n\t"    /* pass stack pointer */
101     "bis $18,$18,$17\n\t"   /* pass "paramCount" */
102     "bis $19,$19,$18\n\t"   /* pass "params" */
103     "bsr $26,$invoke_copy_to_stack..ng\n\t"     /* call invoke_copy_to_stack */
104 
105     /*
106      * Copy the first 6 parameters to registers and remove from stack frame.
107      * Both the integer and floating point registers are set for each parameter
108      * except the first which is the "this" pointer.  (integer only)
109      * The floating point registers are all set as doubles since the
110      * invoke_copy_to_stack function should have converted the floats.
111      */
112     "ldq $16,0($30)\n\t"    /* integer registers */
113     "ldq $17,8($30)\n\t"
114     "ldq $18,16($30)\n\t"
115     "ldq $19,24($30)\n\t"
116     "ldq $20,32($30)\n\t"
117     "ldq $21,40($30)\n\t"
118     "ldt $f17,8($30)\n\t"   /* floating point registers */
119     "ldt $f18,16($30)\n\t"
120     "ldt $f19,24($30)\n\t"
121     "ldt $f20,32($30)\n\t"
122     "ldt $f21,40($30)\n\t"
123 
124     "addq $30,48,$30\n\t"   /* remove params from stack */
125 
126     /*
127      * Call the virtual function with the constructed stack frame.
128      */
129     "bis $16,$16,$1\n\t"    /* load "this" */
130     "ldq $2,16($15)\n\t"    /* load "methodIndex" */
131     "ldq $1,0($1)\n\t"      /* load vtable */
132     "s8addq $2,$31,$2\n\t"  /* vtable index = "methodIndex" * 8 */
133     "addq $1,$2,$1\n\t"
134     "ldq $27,0($1)\n\t"     /* load address of function */
135     "jsr $26,($27),0\n\t"   /* call virtual function */
136     "ldgp $29,0($26)\n\t"
137 
138     "bis $15,$15,$30\n\t"
139     "ldq $26,0($30)\n\t"
140     "ldq $15,8($30)\n\t"
141     "addq $30,32,$30\n\t"
142     "ret $31,($26),1\n\t"
143     ".end NS_InvokeByIndex"
144     );
145