1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/reflect/xptcall/src/md/win32/xptcinvoke_x86_gnu.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,106 @@ 1.4 +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* Platform specific code to invoke XPCOM methods on native objects */ 1.10 + 1.11 +#include "xptcprivate.h" 1.12 + 1.13 +extern "C" { 1.14 +void __attribute__ ((__used__)) __attribute__ ((regparm(3))) 1.15 +invoke_copy_to_stack(uint32_t paramCount, nsXPTCVariant* s, uint32_t* d) 1.16 +{ 1.17 + for(uint32_t i = paramCount; i >0; i--, d++, s++) 1.18 + { 1.19 + if(s->IsPtrData()) 1.20 + { 1.21 + *((void**)d) = s->ptr; 1.22 + continue; 1.23 + } 1.24 + 1.25 + switch(s->type) 1.26 + { 1.27 + case nsXPTType::T_I8 : *((int8_t*) d) = s->val.i8; break; 1.28 + case nsXPTType::T_I16 : *((int16_t*) d) = s->val.i16; break; 1.29 + case nsXPTType::T_I32 : *((int32_t*) d) = s->val.i32; break; 1.30 + case nsXPTType::T_I64 : *((int64_t*) d) = s->val.i64; d++; break; 1.31 + case nsXPTType::T_U8 : *((uint8_t*) d) = s->val.u8; break; 1.32 + case nsXPTType::T_U16 : *((uint16_t*)d) = s->val.u16; break; 1.33 + case nsXPTType::T_U32 : *((uint32_t*)d) = s->val.u32; break; 1.34 + case nsXPTType::T_U64 : *((uint64_t*)d) = s->val.u64; d++; break; 1.35 + case nsXPTType::T_FLOAT : *((float*) d) = s->val.f; break; 1.36 + case nsXPTType::T_DOUBLE : *((double*) d) = s->val.d; d++; break; 1.37 + case nsXPTType::T_BOOL : *((bool*) d) = s->val.b; break; 1.38 + case nsXPTType::T_CHAR : *((char*) d) = s->val.c; break; 1.39 + case nsXPTType::T_WCHAR : *((wchar_t*) d) = s->val.wc; break; 1.40 + default: 1.41 + // all the others are plain pointer types 1.42 + *((void**)d) = s->val.p; 1.43 + break; 1.44 + } 1.45 + } 1.46 +} 1.47 +} // extern "C" 1.48 + 1.49 +/* 1.50 + EXPORT_XPCOM_API(nsresult) 1.51 + NS_InvokeByIndex(nsISupports* that, uint32_t methodIndex, 1.52 + uint32_t paramCount, nsXPTCVariant* params); 1.53 + 1.54 + Each param takes at most two 4-byte words. 1.55 + It doesn't matter if we push too many words, and calculating the exact 1.56 + amount takes time. 1.57 + 1.58 + that = ebp + 0x08 1.59 + methodIndex = ebp + 0x0c 1.60 + paramCount = ebp + 0x10 1.61 + params = ebp + 0x14 1.62 + 1.63 +*/ 1.64 + 1.65 +__asm__ ( 1.66 + ".text\n\t" 1.67 +/* alignment here seems unimportant here; this was 16, now it's 2 which 1.68 + is what xptcstubs uses. */ 1.69 + ".align 2\n\t" 1.70 + ".globl _NS_InvokeByIndex\n\t" 1.71 + "_NS_InvokeByIndex:\n\t" 1.72 + "pushl %ebp\n\t" 1.73 + "movl %esp, %ebp\n\t" 1.74 + "movl 0x10(%ebp), %eax\n\t" 1.75 + "leal 0(,%eax,8),%edx\n\t" 1.76 + 1.77 + /* set up call frame for method. */ 1.78 + "subl %edx, %esp\n\t" /* make room for params. */ 1.79 +/* Align to maximum x86 data size: 128 bits == 16 bytes == XMM register size. 1.80 + * This is to avoid protection faults where SSE+ alignment of stack pointer 1.81 + * is assumed and required, e.g. by GCC4's -ftree-vectorize option. 1.82 + */ 1.83 + "andl $0xfffffff0, %esp\n\t" /* drop(?) stack ptr to 128-bit align */ 1.84 +/* $esp should be aligned to a 16-byte boundary here (note we include an 1.85 + * additional 4 bytes in a later push instruction). This will ensure $ebp 1.86 + * in the function called below is aligned to a 0x8 boundary. SSE instructions 1.87 + * like movapd/movdqa expect memory operand to be aligned on a 16-byte 1.88 + * boundary. The GCC compiler will generate the memory operand using $ebp 1.89 + * with an 8-byte offset. 1.90 + */ 1.91 + "subl $0xc, %esp\n\t" /* lower again; push/call below will re-align */ 1.92 + "movl %esp, %ecx\n\t" /* ecx = d */ 1.93 + "movl 8(%ebp), %edx\n\t" /* edx = this */ 1.94 + "pushl %edx\n\t" /* push this. esp % 16 == 0 */ 1.95 + 1.96 + "movl 0x14(%ebp), %edx\n\t" 1.97 + "call _invoke_copy_to_stack\n\t" 1.98 + "movl 0x08(%ebp), %ecx\n\t" /* 'that' */ 1.99 + "movl (%ecx), %edx\n\t" 1.100 + "movl 0x0c(%ebp), %eax\n\t" /* function index */ 1.101 + "leal (%edx,%eax,4), %edx\n\t" 1.102 + "call *(%edx)\n\t" 1.103 + "movl %ebp, %esp\n\t" 1.104 + "popl %ebp\n\t" 1.105 + "ret\n" 1.106 + ".section .drectve\n\t" 1.107 + ".ascii \" -export:NS_InvokeByIndex\"\n\t" 1.108 + ".text\n\t" 1.109 +);