mshtml: Added IBindHost::CreateMoniker implementation.
[wine] / libs / wine / port.c
1 /*
2  * Wine portability routines
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/types.h>
27
28 #include "wine/library.h"
29
30 /* no longer used, for backwards compatibility only */
31 struct wine_pthread_functions;
32 static void *pthread_functions[8];
33
34 /***********************************************************************
35  *           wine_pthread_get_functions
36  */
37 void wine_pthread_get_functions( struct wine_pthread_functions *functions, size_t size )
38 {
39     memcpy( functions, &pthread_functions, min( size, sizeof(pthread_functions) ));
40 }
41
42
43 /***********************************************************************
44  *           wine_pthread_set_functions
45  */
46 void wine_pthread_set_functions( const struct wine_pthread_functions *functions, size_t size )
47 {
48     memcpy( &pthread_functions, functions, min( size, sizeof(pthread_functions) ));
49 }
50
51
52 /***********************************************************************
53  *           wine_switch_to_stack
54  *
55  * Switch to the specified stack and call the function.
56  */
57 #if defined(__sparc__) && defined(__GNUC__)
58 __ASM_GLOBAL_FUNC( wine_switch_to_stack,
59                    "mov %o0, %l0\n\t" /* store first argument */
60                    "mov %o1, %l1\n\t" /* store second argument */
61                    "sub %o2, 96, %sp\n\t" /* store stack */
62                    "call %l0, 0\n\t" /* call func */
63                    "mov %l1, %o0\n\t" /* delay slot:  arg for func */
64                    "ta 0x01") /* breakpoint - we never get here */
65 #elif defined(__powerpc__) && defined(__APPLE__)
66 __ASM_GLOBAL_FUNC( wine_switch_to_stack,
67                    "mtctr r3\n\t" /* func -> ctr */
68                    "mr r3,r4\n\t" /* args -> function param 1 (r3) */
69                    "mr r1,r5\n\t" /* stack */
70                    "subi r1,r1,0x100\n\t" /* adjust stack pointer */
71                    "bctrl\n" /* call ctr */
72                    "1:\tb 1b") /* loop */
73 #elif defined(__ALPHA__) && defined(__GNUC__)
74 __ASM_GLOBAL_FUNC( wine_switch_to_stack,
75                    "mov $16,$0\n\t" /* func */
76                    "mov $17,$16\n\t" /* arg */
77                    "mov $18,$30\n\t" /* stack */
78                    "jsr $31,($0),0\n\t" /* call func */
79                    "L1:\tbr $31,L1") /* loop */
80 #else
81 void DECLSPEC_NORETURN wine_switch_to_stack( void (*func)(void *), void *arg, void *stack )
82 {
83     wine_call_on_stack( (int (*)(void *))func, arg, stack );
84     abort();
85 }
86 #endif
87
88
89 /***********************************************************************
90  *           wine_call_on_stack
91  *
92  * Switch to the specified stack to call the function and return.
93  */
94
95 #if defined(__i386__) && defined(__GNUC__)
96 __ASM_GLOBAL_FUNC( wine_call_on_stack,
97                    "pushl %ebp\n\t"
98                    __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
99                    __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
100                    "pushl %esi\n\t"
101                    __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
102                    __ASM_CFI(".cfi_rel_offset %esi,0\n\t")
103                    "movl %esp,%esi\n\t"
104                    __ASM_CFI(".cfi_def_cfa_register %esi\n\t")
105                    "movl 12(%esp),%ecx\n\t"  /* func */
106                    "movl 16(%esp),%edx\n\t"  /* arg */
107                    "movl 20(%esp),%eax\n\t"  /* stack */
108                    "andl $~15,%eax\n\t"
109                    "subl $12,%eax\n\t"
110                    "movl %eax,%esp\n\t"
111                    "pushl %edx\n\t"
112                    "xorl %ebp,%ebp\n\t"
113                    "call *%ecx\n\t"
114                    "movl %esi,%esp\n\t"
115                    "popl %esi\n\t"
116                    __ASM_CFI(".cfi_adjust_cfa_offset -4\n\t")
117                    __ASM_CFI(".cfi_same_value %esi\n\t")
118                    "popl %ebp\n\t"
119                    __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
120                    __ASM_CFI(".cfi_same_value %ebp\n\t")
121                    "ret" )
122 #elif defined(__i386__) && defined(_MSC_VER)
123 __declspec(naked) int wine_call_on_stack( int (*func)(void *), void *arg, void *stack )
124 {
125   __asm push ebp;
126   __asm push esi;
127   __asm mov ecx, 12[esp];
128   __asm mov edx, 16[esp];
129   __asm mov esi, 20[esp];
130   __asm xchg esp, esi;
131   __asm push edx;
132   __asm xor ebp, ebp;
133   __asm call [ecx];
134   __asm mov esp, esi;
135   __asm pop esi;
136   __asm pop ebp
137   __asm ret;
138 }
139 #elif defined(__x86_64__) && defined(__GNUC__)
140 __ASM_GLOBAL_FUNC( wine_call_on_stack,
141                    "pushq %rbp\n\t"
142                    __ASM_CFI(".cfi_adjust_cfa_offset 8\n\t")
143                    __ASM_CFI(".cfi_rel_offset %rbp,0\n\t")
144                    "movq %rsp,%rbp\n\t"
145                    __ASM_CFI(".cfi_def_cfa_register %rbp\n\t")
146                    "movq %rdi,%rax\n\t" /* func */
147                    "movq %rsi,%rdi\n\t" /* arg */
148                    "andq $~15,%rdx\n\t" /* stack */
149                    "movq %rdx,%rsp\n\t"
150                    "callq *%rax\n\t"    /* call func */
151                    "movq %rbp,%rsp\n\t"
152                    __ASM_CFI(".cfi_def_cfa_register %rsp\n\t")
153                    "popq %rbp\n\t"
154                    __ASM_CFI(".cfi_adjust_cfa_offset -8\n\t")
155                    __ASM_CFI(".cfi_same_value %rbp\n\t")
156                    "ret")
157 #elif defined(__powerpc__) && defined(__GNUC__)
158 __ASM_GLOBAL_FUNC( wine_call_on_stack,
159                    "mflr 0\n\t"         /* get return address */
160                    "stw 0, 4(1)\n\t"    /* save return address */
161                    "subi 5, 5, 16\n\t" /* reserve space on new stack */
162                    "stw 1, 12(5)\n\t"   /* store old sp */
163                    "mtctr 3\n\t"        /* func -> ctr */
164                    "mr 3,4\n\t"         /* args -> function param 1 (r3) */
165                    "mr 1,5\n\t"         /* stack */
166                    "li 0, 0\n\t"        /* zero */
167                    "stw 0, 0(1)\n\t"    /* bottom of stack */
168                    "stwu 1, -16(1)\n\t" /* create a frame for this function */
169                    "bctrl\n\t"          /* call ctr */
170                    "lwz 1, 28(1)\n\t"   /* fetch old sp */
171                    "lwz 0, 4(1)\n\t"    /* fetch return address */
172                    "mtlr 0\n\t"         /* return address -> lr */
173                    "blr")               /* return */
174 #elif defined(__arm__) && defined(__GNUC__)
175 __ASM_GLOBAL_FUNC( wine_call_on_stack,
176                    "push {r4,LR}\n\t"   /* save return address on stack */
177                    "mov r4, sp\n\t"     /* store old sp in local var */
178                    "mov sp, r2\n\t"     /* stack */
179                    "mov r2, r0\n\t"     /* func -> scratch register */
180                    "mov r0, r1\n\t"     /* arg */
181                    "mov LR, PC\n\t"     /* return after branch */
182                    "mov PC, r2\n\t"     /* call func */
183                    "mov sp, r4\n\t"     /* restore old sp from local var */
184                    "pop {r4,PC}")       /* fetch return address into pc */
185 #else
186 #error You must implement wine_switch_to_stack for your platform
187 #endif