Mercurial > pub > dyncall > dyncall
annotate dyncallback/dyncall_callback_x86.c @ 517:f8856e29b512
- tests: made "ordered" signature generation code shareable, and added possibility to specify return types, separately (mainly to include 'void')
author | Tassilo Philipp |
---|---|
date | Mon, 11 Apr 2022 14:50:35 +0200 |
parents | ddfb9577a00e |
children | 71c884e610f0 |
rev | line source |
---|---|
0 | 1 /* |
2 | |
3 Package: dyncall | |
4 Library: dyncallback | |
5 File: dyncallback/dyncall_callback_x86.c | |
6 Description: Callback - Implementation for x86 | |
7 License: | |
8 | |
364 | 9 Copyright (c) 2007-2020 Daniel Adler <dadler@uni-goettingen.de>, |
0 | 10 Tassilo Philipp <tphilipp@potion-studios.com> |
11 | |
12 Permission to use, copy, modify, and distribute this software for any | |
13 purpose with or without fee is hereby granted, provided that the above | |
14 copyright notice and this permission notice appear in all copies. | |
15 | |
16 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
17 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
18 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
19 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
20 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
21 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
22 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
23 | |
24 */ | |
25 | |
26 | |
152
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
27 #include "dyncall_callback.h" |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
28 #include "dyncall_alloc_wx.h" |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
29 #include "dyncall_thunk.h" |
0 | 30 #include "dyncall_args_x86.h" |
31 | |
152
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
32 /* Callback symbol. */ |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
33 extern void dcCallbackThunkEntry(); |
0 | 34 |
152
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
35 struct DCCallback |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
36 { |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
37 DCThunk thunk; /* offset 0, size 16 */ |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
38 DCCallbackHandler* handler; /* offset 16 */ |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
39 DCArgsVT* args_vt; /* offset 20 */ |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
40 size_t stack_cleanup; /* offset 24 */ |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
41 void* userdata; /* offset 28 */ |
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
42 }; |
0 | 43 |
44 | |
45 /* compute stacksize for callee cleanup calling conventions: | |
46 * | |
152
d48a8b8d2ef9
- integrated all headers containing DCCallback definition into the translation units used (arm64 already avoided this pointless header, so following that style)
cslag
parents:
0
diff
changeset
|
47 * cdecl,stdcall,thiscall_ms,fastcall_ms,fastcall_gnu |
0 | 48 */ |
49 | |
50 static int dcbCleanupSize_x86_cdecl(const char* signature) | |
51 { | |
52 return 0; | |
53 } | |
54 | |
55 static int dcbCleanupSize_x86_std(const char* signature) | |
56 { | |
57 const char* ptr = signature; | |
58 int size = 0; | |
59 char ch; | |
60 while( (ch = *ptr++) != DC_SIGCHAR_ENDARG ) { | |
61 switch(ch) { | |
62 case DC_SIGCHAR_BOOL: | |
63 case DC_SIGCHAR_CHAR: | |
64 case DC_SIGCHAR_SHORT: | |
65 case DC_SIGCHAR_INT: | |
66 case DC_SIGCHAR_LONG: | |
67 case DC_SIGCHAR_POINTER: | |
68 case DC_SIGCHAR_UCHAR: | |
69 case DC_SIGCHAR_USHORT: | |
70 case DC_SIGCHAR_UINT: | |
71 case DC_SIGCHAR_ULONG: | |
72 case DC_SIGCHAR_STRING: | |
73 case DC_SIGCHAR_FLOAT: | |
74 size += 4; | |
75 break; | |
76 case DC_SIGCHAR_DOUBLE: | |
77 case DC_SIGCHAR_LONGLONG: | |
78 case DC_SIGCHAR_ULONGLONG: | |
79 size += 8; | |
80 break; | |
81 } | |
82 } | |
83 return size; | |
84 } | |
85 | |
86 static int dcbCleanupSize_x86_this_ms(const char* signature) | |
87 { | |
88 const char* ptr = signature; | |
89 int size = 0; | |
90 char ch; | |
91 while( (ch = *ptr++) != DC_SIGCHAR_ENDARG ) | |
92 { | |
93 switch(ch) | |
94 { | |
95 case DC_SIGCHAR_BOOL: | |
96 case DC_SIGCHAR_CHAR: | |
97 case DC_SIGCHAR_SHORT: | |
98 case DC_SIGCHAR_INT: | |
99 case DC_SIGCHAR_LONG: | |
100 case DC_SIGCHAR_POINTER: | |
101 case DC_SIGCHAR_UCHAR: | |
102 case DC_SIGCHAR_USHORT: | |
103 case DC_SIGCHAR_UINT: | |
104 case DC_SIGCHAR_ULONG: | |
105 case DC_SIGCHAR_STRING: | |
106 case DC_SIGCHAR_FLOAT: | |
107 size += 4; | |
108 break; | |
109 case DC_SIGCHAR_DOUBLE: | |
110 case DC_SIGCHAR_LONGLONG: | |
111 case DC_SIGCHAR_ULONGLONG: | |
112 size += 8; | |
113 break; | |
114 } | |
115 } | |
116 return size; | |
117 } | |
118 | |
119 static int dcbCleanupSize_x86_fast_ms(const char* signature) | |
120 { | |
121 const char* ptr = signature; | |
122 int size = 0; | |
123 int regs = 0; | |
124 char ch; | |
125 while( (ch = *ptr++) != DC_SIGCHAR_ENDARG ) | |
126 { | |
127 switch(ch) | |
128 { | |
129 case DC_SIGCHAR_BOOL: | |
130 case DC_SIGCHAR_CHAR: | |
131 case DC_SIGCHAR_SHORT: | |
132 case DC_SIGCHAR_INT: | |
133 case DC_SIGCHAR_LONG: | |
134 case DC_SIGCHAR_POINTER: | |
135 case DC_SIGCHAR_UCHAR: | |
136 case DC_SIGCHAR_USHORT: | |
137 case DC_SIGCHAR_UINT: | |
138 case DC_SIGCHAR_ULONG: | |
139 case DC_SIGCHAR_STRING: | |
140 if (regs < 2) regs++; | |
141 else size += 4; | |
142 break; | |
143 case DC_SIGCHAR_FLOAT: | |
144 size += 4; | |
145 break; | |
146 case DC_SIGCHAR_DOUBLE: | |
147 size += 8; | |
148 break; | |
149 case DC_SIGCHAR_LONGLONG: | |
150 case DC_SIGCHAR_ULONGLONG: | |
151 size += 8; | |
152 break; | |
153 } | |
154 } | |
155 return size; | |
156 } | |
157 | |
158 static int dcbCleanupSize_x86_fast_gnu(const char* signature) | |
159 { | |
160 const char* ptr = signature; | |
161 char ch; | |
162 int size = 0; | |
163 int regs = 0; | |
164 while( (ch = *ptr++) != DC_SIGCHAR_ENDARG ) { | |
165 switch(ch) { | |
166 case DC_SIGCHAR_FLOAT: | |
167 size += 4; | |
168 break; | |
169 case DC_SIGCHAR_DOUBLE: | |
170 size += 8; | |
171 break; | |
172 case DC_SIGCHAR_LONGLONG: | |
173 case DC_SIGCHAR_ULONGLONG: | |
174 regs = 2; | |
175 size += 8; | |
176 break; | |
177 default: | |
178 if (regs < 2) regs++; | |
179 else size += 4; | |
180 break; | |
181 } | |
182 } | |
183 return size; | |
184 } | |
185 | |
186 void dcbInitCallback(DCCallback* pcb, const char* signature, DCCallbackHandler* handler, void* userdata) | |
187 { | |
188 const char* ptr; | |
189 int mode; | |
190 pcb->handler = handler; | |
191 pcb->userdata = userdata; | |
192 | |
193 ptr = signature; | |
194 | |
195 /* x86 hints: */ | |
196 | |
197 mode = DC_CALL_C_X86_CDECL; | |
198 | |
466
ddfb9577a00e
introduced platform-native thiscall mode (DC_CALL_C_DEFAULT_THIS), as needed
Tassilo Philipp
parents:
364
diff
changeset
|
199 if(*ptr == DC_SIGCHAR_CC_PREFIX) |
0 | 200 { |
466
ddfb9577a00e
introduced platform-native thiscall mode (DC_CALL_C_DEFAULT_THIS), as needed
Tassilo Philipp
parents:
364
diff
changeset
|
201 mode = dcGetModeFromCCSigChar(ptr[1]); |
ddfb9577a00e
introduced platform-native thiscall mode (DC_CALL_C_DEFAULT_THIS), as needed
Tassilo Philipp
parents:
364
diff
changeset
|
202 ptr += 2; |
0 | 203 } |
204 | |
205 /* x86 configuration: */ | |
206 | |
207 switch(mode) { | |
208 case DC_CALL_C_X86_CDECL: | |
209 pcb->args_vt = &dcArgsVT_default; | |
210 pcb->stack_cleanup = dcbCleanupSize_x86_cdecl(ptr); | |
211 break; | |
212 case DC_CALL_C_X86_WIN32_STD: | |
213 pcb->args_vt = &dcArgsVT_default; | |
214 pcb->stack_cleanup = dcbCleanupSize_x86_std(ptr); | |
215 break; | |
216 case DC_CALL_C_X86_WIN32_FAST_MS: | |
217 pcb->args_vt = &dcArgsVT_fast_ms; | |
218 pcb->stack_cleanup = dcbCleanupSize_x86_fast_ms(ptr); | |
219 break; | |
220 case DC_CALL_C_X86_WIN32_FAST_GNU: | |
221 pcb->args_vt = &dcArgsVT_fast_gnu; | |
222 pcb->stack_cleanup = dcbCleanupSize_x86_fast_gnu(ptr); | |
223 break; | |
364 | 224 case DC_CALL_C_X86_WIN32_THIS_MS: |
225 pcb->args_vt = &dcArgsVT_this_ms; | |
226 pcb->stack_cleanup = dcbCleanupSize_x86_this_ms(ptr); | |
227 break; | |
0 | 228 } |
229 | |
230 #if defined(DC_PLAN9) | |
231 /* HACK for Plan9 - 'reuse' pcb->stack_cleanup as a flag | |
232 to indicate if return value is 64bit. The field is not | |
233 used anyways as the caller is responsible to clean up | |
234 the stack in Plan9. If set to '1' the callback kernel | |
235 takes into account an extra stack-parameter (pointer | |
236 to 64bit return value). | |
237 I thought of introducing a new field, but for one single | |
238 x86 calling convention out of many, it seemed overkill | |
239 to change the struct for everybody else. Maybe renaming | |
240 would be a good idea, though. ~ Tassilo | |
241 */ | |
242 while(*ptr) { | |
243 if(*ptr++ == DC_SIGCHAR_ENDARG) { | |
244 pcb->stack_cleanup = (*ptr == DC_SIGCHAR_LONGLONG) || (*ptr == DC_SIGCHAR_ULONGLONG); | |
245 break; | |
246 } | |
247 } | |
248 #endif | |
249 } | |
250 | |
251 /* | |
252 * callback constructor | |
253 */ | |
254 DCCallback* dcbNewCallback(const char* signature, DCCallbackHandler* handler, void* userdata) | |
255 { | |
256 int err; | |
257 DCCallback* pcb; | |
258 err = dcAllocWX(sizeof(DCCallback), (void**) &pcb); | |
202 | 259 if(err) |
260 return NULL; | |
0 | 261 |
262 dcbInitThunk(&pcb->thunk, dcCallbackThunkEntry); | |
263 dcbInitCallback(pcb, signature, handler, userdata); | |
202 | 264 |
265 err = dcInitExecWX(pcb, sizeof(DCCallback)); | |
266 if(err) { | |
267 dcFreeWX(pcb, sizeof(DCCallback)); | |
268 return NULL; | |
269 } | |
270 | |
0 | 271 return pcb; |
272 } | |
273 | |
274 /* | |
275 * free | |
276 */ | |
277 | |
278 void dcbFreeCallback(DCCallback* pcb) | |
279 { | |
280 dcFreeWX(pcb, sizeof(DCCallback)); | |
281 } | |
282 | |
283 void* dcbGetUserData(DCCallback* pcb) | |
284 { | |
285 return pcb->userdata; | |
286 } |