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 #include "xptc_gcc_x86_unix.h"
10 
11 extern "C" {
12 static void ATTRIBUTE_USED __attribute__ ((regparm(3)))
invoke_copy_to_stack(uint32_t paramCount,nsXPTCVariant * s,uint32_t * d)13 invoke_copy_to_stack(uint32_t paramCount, nsXPTCVariant* s, uint32_t* d)
14 {
15     for(uint32_t i = paramCount; i >0; i--, d++, s++)
16     {
17         if(s->IsPtrData())
18         {
19             *((void**)d) = s->ptr;
20             continue;
21         }
22 
23         switch(s->type)
24         {
25         case nsXPTType::T_I64    : *((int64_t*) d) = s->val.i64; d++;    break;
26         case nsXPTType::T_U64    : *((uint64_t*)d) = s->val.u64; d++;    break;
27         case nsXPTType::T_DOUBLE : *((double*)  d) = s->val.d;   d++;    break;
28         default                  : *((void**)d)    = s->val.p;           break;
29         }
30     }
31 }
32 } // extern "C"
33 
34 /*
35   EXPORT_XPCOM_API(nsresult)
36   NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex,
37                    uint32_t paramCount, nsXPTCVariant* params);
38 
39   Each param takes at most two 4-byte words.
40   It doesn't matter if we push too many words, and calculating the exact
41   amount takes time.
42 
43   that        = ebp + 0x08
44   methodIndex = ebp + 0x0c
45   paramCount  = ebp + 0x10
46   params      = ebp + 0x14
47 
48 */
49 
50 __asm__ (
51 	".text\n\t"
52 /* alignment here seems unimportant here; this was 16, now it's 2 which
53    is what xptcstubs uses. */
54 	".align 2\n\t"
55 	".globl " SYMBOL_UNDERSCORE "NS_InvokeByIndex\n\t"
56 #ifndef XP_MACOSX
57 	".type  " SYMBOL_UNDERSCORE "NS_InvokeByIndex,@function\n"
58 #endif
59 	SYMBOL_UNDERSCORE "NS_InvokeByIndex:\n\t"
60 	"pushl %ebp\n\t"
61 	"movl  %esp, %ebp\n\t"
62 	"movl  0x10(%ebp), %eax\n\t"
63 	"leal  0(,%eax,8),%edx\n\t"
64 
65         /* set up call frame for method. */
66 	"subl  %edx, %esp\n\t"       /* make room for params. */
67 /* Align to maximum x86 data size: 128 bits == 16 bytes == XMM register size.
68  * This is to avoid protection faults where SSE+ alignment of stack pointer
69  * is assumed and required, e.g. by GCC4's -ftree-vectorize option.
70  */
71 	"andl  $0xfffffff0, %esp\n\t"   /* drop(?) stack ptr to 128-bit align */
72 /* $esp should be aligned to a 16-byte boundary here (note we include an
73  * additional 4 bytes in a later push instruction). This will ensure $ebp
74  * in the function called below is aligned to a 0x8 boundary. SSE instructions
75  * like movapd/movdqa expect memory operand to be aligned on a 16-byte
76  * boundary. The GCC compiler will generate the memory operand using $ebp
77  * with an 8-byte offset.
78  */
79 	"subl  $0xc, %esp\n\t"          /* lower again; push/call below will re-align */
80 	"movl  %esp, %ecx\n\t"          /* ecx = d */
81 	"movl  8(%ebp), %edx\n\t"       /* edx = this */
82 	"pushl %edx\n\t"                /* push this. esp % 16 == 0 */
83 
84 	"movl  0x14(%ebp), %edx\n\t"
85 	"call  " SYMBOL_UNDERSCORE "invoke_copy_to_stack\n\t"
86 	"movl  0x08(%ebp), %ecx\n\t"	/* 'that' */
87 	"movl  (%ecx), %edx\n\t"
88 	"movl  0x0c(%ebp), %eax\n\t"    /* function index */
89 	"leal  (%edx,%eax,4), %edx\n\t"
90 	"call  *(%edx)\n\t"
91 	"movl  %ebp, %esp\n\t"
92 	"popl  %ebp\n\t"
93 	"ret\n"
94 #ifndef XP_MACOSX
95 	".size " SYMBOL_UNDERSCORE "NS_InvokeByIndex, . -" SYMBOL_UNDERSCORE "NS_InvokeByIndex\n\t"
96 #endif
97 );
98