PageRenderTime 71ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 16ms app.codeStats 1ms

/Modules/_ctypes/libffi_msvc/win32.c

http://unladen-swallow.googlecode.com/
C | 267 lines | 144 code | 40 blank | 83 comment | 0 complexity | 3edb1646d7a0551a10e31f9b60f4fa4d MD5 | raw file
  1/* -----------------------------------------------------------------------
  2   win32.S - Copyright (c) 1996, 1998, 2001, 2002  Red Hat, Inc.
  3	     Copyright (c) 2001  John Beniton
  4	     Copyright (c) 2002  Ranjit Mathew
  5			
  6 
  7   X86 Foreign Function Interface
  8 
  9   Permission is hereby granted, free of charge, to any person obtaining
 10   a copy of this software and associated documentation files (the
 11   ``Software''), to deal in the Software without restriction, including
 12   without limitation the rights to use, copy, modify, merge, publish,
 13   distribute, sublicense, and/or sell copies of the Software, and to
 14   permit persons to whom the Software is furnished to do so, subject to
 15   the following conditions:
 16 
 17   The above copyright notice and this permission notice shall be included
 18   in all copies or substantial portions of the Software.
 19 
 20   THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS
 21   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 22   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 23   IN NO EVENT SHALL CYGNUS SOLUTIONS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 24   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 25   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 26   OTHER DEALINGS IN THE SOFTWARE.
 27   ----------------------------------------------------------------------- */
 28
 29/* theller: almost verbatim translation from gas syntax to MSVC inline
 30   assembler code. */
 31
 32/* theller: ffi_call_SYSV and ffi_call_STDCALL now return an integer - the
 33   difference of the stack pointer before and after the function call.  If
 34   everything is ok, zero is returned.  If stdcall functions are passed the
 35   wrong number of arguments, the difference will be nonzero. */
 36
 37#include <ffi.h>
 38#include <ffi_common.h>
 39
 40__declspec(naked) int
 41ffi_call_SYSV(void (* prepfunc)(char *, extended_cif *), /* 8 */
 42		 extended_cif *ecif, /* 12 */
 43		 unsigned bytes, /* 16 */
 44		 unsigned flags, /* 20 */
 45		 unsigned *rvalue, /* 24 */
 46		 void (*fn)()) /* 28 */
 47{
 48	_asm {
 49		push ebp
 50		mov ebp, esp
 51
 52		push esi // NEW: this register must be preserved across function calls
 53// XXX SAVE ESP NOW!
 54		mov esi, esp		// save stack pointer before the call
 55
 56// Make room for all of the new args.
 57		mov ecx, [ebp+16]
 58		sub esp, ecx		// sub esp, bytes
 59		
 60		mov eax, esp
 61
 62// Place all of the ffi_prep_args in position
 63		push [ebp + 12] // ecif
 64		push eax
 65		call [ebp + 8] // prepfunc
 66
 67// Return stack to previous state and call the function
 68		add esp, 8
 69// FIXME: Align the stack to a 128-bit boundary to avoid
 70// potential performance hits.
 71		call [ebp + 28]
 72// Remove the space we pushed for the args
 73		mov ecx, [ebp + 16]
 74		add esp, ecx
 75
 76// XXX ASSERT THAT ESP IS THE SAME NOW THAN BEFORE!
 77		sub esi, esp
 78
 79// Load %ecx with the return type code
 80		mov ecx, [ebp + 20]
 81
 82// If the return value pointer is NULL, assume no return value.
 83/*
 84  Intel asm is weird. We have to explicitely specify 'DWORD PTR' in the nexr instruction,
 85  otherwise only one BYTE will be compared (instead of a DWORD)!
 86 */
 87		cmp DWORD PTR [ebp + 24], 0
 88		jne sc_retint
 89
 90// Even if there is no space for the return value, we are
 91// obliged to handle floating-point values.
 92		cmp ecx, FFI_TYPE_FLOAT
 93		jne sc_noretval
 94//        fstp  %st(0)
 95		fstp st(0)
 96
 97		jmp sc_epilogue
 98
 99sc_retint:
100		cmp ecx, FFI_TYPE_INT
101		jne sc_retfloat
102//        # Load %ecx with the pointer to storage for the return value
103		mov ecx, [ebp + 24]
104		mov [ecx + 0], eax
105		jmp sc_epilogue
106
107sc_retfloat:
108		cmp ecx, FFI_TYPE_FLOAT
109		jne sc_retdouble
110// Load %ecx with the pointer to storage for the return value
111		mov ecx, [ebp+24]
112//        fstps (%ecx)
113		fstp DWORD PTR [ecx]
114		jmp sc_epilogue
115
116sc_retdouble:
117		cmp ecx, FFI_TYPE_DOUBLE
118		jne sc_retlongdouble
119//        movl  24(%ebp),%ecx
120		mov ecx, [ebp+24]
121		fstp QWORD PTR [ecx]
122		jmp sc_epilogue
123
124		jmp sc_retlongdouble // avoid warning about unused label
125sc_retlongdouble:
126		cmp ecx, FFI_TYPE_LONGDOUBLE
127		jne sc_retint64
128// Load %ecx with the pointer to storage for the return value
129		mov ecx, [ebp+24]
130//        fstpt (%ecx)
131		fstp QWORD PTR [ecx] /* XXX ??? */
132		jmp sc_epilogue
133
134sc_retint64:
135		cmp ecx, FFI_TYPE_SINT64
136		jne sc_retstruct
137// Load %ecx with the pointer to storage for the return value
138		mov ecx, [ebp+24]
139		mov [ecx+0], eax
140		mov [ecx+4], edx
141
142sc_retstruct:
143// Nothing to do!
144
145sc_noretval:
146sc_epilogue:
147		mov eax, esi
148		pop esi // NEW restore: must be preserved across function calls
149		mov esp, ebp
150		pop ebp
151		ret
152	}
153}
154
155__declspec(naked) int
156ffi_call_STDCALL(void (* prepfunc)(char *, extended_cif *), /* 8 */
157		 extended_cif *ecif, /* 12 */
158		 unsigned bytes, /* 16 */
159		 unsigned flags, /* 20 */
160		 unsigned *rvalue, /* 24 */
161		 void (*fn)()) /* 28 */
162{
163	_asm {
164		push ebp
165		mov ebp, esp
166
167		push esi // NEW: this register must be preserved across function calls
168
169// XXX SAVE ESP NOW!
170		mov esi, esp
171
172// Make room for all of the new args.
173		mov ecx, [ebp+16]
174		sub esp, ecx
175		
176		mov eax, esp
177
178// Place all of the ffi_prep_args in position
179		push [ebp + 12] // ecif
180		push eax
181		call [ebp + 8] // prepfunc
182
183// Return stack to previous state and call the function
184		add esp, 8
185// FIXME: Align the stack to a 128-bit boundary to avoid
186// potential performance hits.
187		call [ebp + 28]
188// stdcall functions pop arguments off the stack themselves
189
190// XXX IS ESP NOW THE SAME AS BEFORE?
191		sub esi, esp
192
193// Load %ecx with the return type code
194		mov ecx, [ebp + 20]
195
196// If the return value pointer is NULL, assume no return value.
197/*
198  Intel asm is weird. We have to explicitely specify 'DWORD PTR' in the nexr instruction,
199  otherwise only one BYTE will be compared (instead of a DWORD)!
200 */
201		cmp DWORD PTR [ebp + 24], 0
202		jne sc_retint
203
204// Even if there is no space for the return value, we are
205// obliged to handle floating-point values.
206		cmp ecx, FFI_TYPE_FLOAT
207		jne sc_noretval
208//        fstp  %st(0)
209		fstp st(0)
210
211		jmp sc_epilogue
212
213sc_retint:
214		cmp ecx, FFI_TYPE_INT
215		jne sc_retfloat
216//        # Load %ecx with the pointer to storage for the return value
217		mov ecx, [ebp + 24]
218		mov [ecx + 0], eax
219		jmp sc_epilogue
220
221sc_retfloat:
222		cmp ecx, FFI_TYPE_FLOAT
223		jne sc_retdouble
224// Load %ecx with the pointer to storage for the return value
225		mov ecx, [ebp+24]
226//        fstps (%ecx)
227		fstp DWORD PTR [ecx]
228		jmp sc_epilogue
229
230sc_retdouble:
231		cmp ecx, FFI_TYPE_DOUBLE
232		jne sc_retlongdouble
233//        movl  24(%ebp),%ecx
234		mov ecx, [ebp+24]
235		fstp QWORD PTR [ecx]
236		jmp sc_epilogue
237
238		jmp sc_retlongdouble // avoid warning about unused label
239sc_retlongdouble:
240		cmp ecx, FFI_TYPE_LONGDOUBLE
241		jne sc_retint64
242// Load %ecx with the pointer to storage for the return value
243		mov ecx, [ebp+24]
244//        fstpt (%ecx)
245		fstp QWORD PTR [ecx] /* XXX ??? */
246		jmp sc_epilogue
247
248sc_retint64:
249		cmp ecx, FFI_TYPE_SINT64
250		jne sc_retstruct
251// Load %ecx with the pointer to storage for the return value
252		mov ecx, [ebp+24]
253		mov [ecx+0], eax
254		mov [ecx+4], edx
255
256sc_retstruct:
257// Nothing to do!
258
259sc_noretval:
260sc_epilogue:
261		mov eax, esi
262		pop esi // NEW restore: must be preserved across function calls
263		mov esp, ebp
264		pop ebp
265		ret
266	}
267}