/
dyncall_callback_x86_gas.S
123 lines (97 loc) · 2.74 KB
/
dyncall_callback_x86_gas.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Package: dyncall
Library: dyncallback
File: dyncallback/dyncall_callback_x86_gas.s
Description: Callback Thunk - Implementation for x86 in GNU as
License:
Copyright (c) 2007-2009 Daniel Adler <dadler@uni-goettingen.de>,
Tassilo Philipp <tphilipp@potion-studios.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
.intel_syntax
.text
#include "../dyncall/dyncall_macros.h"
#if defined(DC__OS_Win32) || defined(DC__OS_Cygwin) || defined(DC__OS_MinGW)
#define CSYM(X) _##X
#else
#define CSYM(X) X
#endif
.globl CSYM(dcCallbackThunkEntry)
DCThunk_size = 16
DCArgs_size = 20
DCValue_size = 8
CTX_thunk = 0
CTX_phandler = 16
CTX_pargsvt = 20
CTX_stack_cleanup = 24
CTX_userdata = 28
frame_arg0 = 8
frame_ret = 4
frame_parent = 0
frame_CTX = -4
frame_DCArgs = -24
frame_DCValue = -32
CSYM(dcCallbackThunkEntry):
push %ebp
mov %ebp, %esp
// local variable frame_CTX
push %eax # EAX = CTX*
// initialize DCArgs
push 0 # fast_count
push %edx # fast_data[1]
push %ecx # fast_data[0]
lea %ecx, [%ebp+frame_arg0] # compute arg stack address
push %ecx # stack_ptr
push [%eax+CTX_pargsvt] # virtual table*
mov %ecx, %esp # ECX = DCArgs*
// initialze DCValue
push 0
push 0
mov %edx, %esp # EDX = DCValue*
// call handler (context
push [%eax+CTX_userdata] # userdata
push %edx # DCValue*
push %ecx # DCArgs*
push %eax # DCCallback*
call [%eax+CTX_phandler]
// cleanup stack
mov %esp, %ebp # reset esp to frame
pop %ecx # skip parent frame
pop %ecx # pop return-address
mov %edx, [%ebp+frame_CTX]
add %esp, [%edx+CTX_stack_cleanup] # cleanup stack
push %ecx # push back return address
lea %edx, [%ebp+frame_DCValue]
mov %ebp, [%ebp] # EBP = parent frame
// handle return value
cmp %al, 'd'
je .return_f64
cmp %al, 'f'
je .return_f32
cmp %al, 'l'
je .return_i64
cmp %al, 'i'
je .return_i32
ret
.return_i32:
mov %eax, [%edx]
ret
.return_i64:
mov %eax, [%edx]
mov %edx, [%edx+4]
ret
.return_f32:
fld dword ptr [%edx]
ret
.return_f64:
fld qword ptr [%edx]
ret