forked from cosimo/perl5-win32-api
-
Notifications
You must be signed in to change notification settings - Fork 3
/
call_i686.h
342 lines (321 loc) · 10.5 KB
/
call_i686.h
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
"I have a dream."
Define one assembler macro for everyone: gcc, Borland C & MSVC
Is it possible?
*/
/* The code in this file is not used anymore since each CC has its own asm
version.
The code below will compile under MSVC can be used to generate boilerplate for
the MASM code, but the ASM versions are more efficient, this file is #included
twice, once in API.xs and once (if applicable) in call_i686.c
*/
#if 0
PORTALIGN(1) const char bad_esp_msg [];
/* Borland C */
#if (defined(__BORLANDC__) && __BORLANDC__ >= 452)
#define ASM_LOAD_EAX(param,type) \
__asm { \
mov eax, type param ; \
push eax ; \
}
/* MSVC compilers */
#elif defined _MSC_VER
/* Disable warning about one missing macro parameter.
TODO: How we define a macro with an optional (empty) parameter? */
#pragma warning( disable : 4003 )
#define ASM_LOAD_EAX(param,type) { \
__asm { mov eax, type param }; \
__asm { push eax }; \
}
/* GCC-MinGW Compiler */
#elif (defined(__GNUC__))
#define ASM_LOAD_EAX(param,...) asm ("push %0" :: "g" (param));
#endif
#ifdef __GNUC__
# define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 \
+ __GNUC_PATCHLEVEL__)
# if GCC_VERSION >= 40400
# pragma GCC push_options
# endif
#endif
extern void __cdecl _RTC_CheckEsp();
/* params are arranged first used (left) to last used (right) */
#ifdef __cplusplus
extern "C"
#endif
void __fastcall Call_asm(const APIPARAM * param /*in caller, this a * to after the last
initialized struct, on entry, param is
always pointing to uninit memory*/,
const APIPARAM * const params_start,
const APICONTROL * const control,
APIPARAM_U * const retval)
{
/* int iParam; */
union{
long lParam;
float fParam;
double dParam;
/* char cParam; */
char *pParam;
LPBYTE ppParam;
__int64 qParam;
} p;
register int i = (size_t)param/(size_t)params_start;
/* #### PUSH THE PARAMETER ON THE (ASSEMBLER) STACK #### */
/* Start with last arg first, asm push goes down, not up, so first push must
be the last arg. On entry, if param == params_start, it means NO params
so if there is 1 param, param will be pointing the struct after the
last one, in other words, param will be a * to an uninit APIPARAM,
therefore -- it immediatly */
/* make gcc not trust ESP */
#ifdef __GNUC__
void * orig_esp;
register void * unused_gcc asm ("eax");
unused_gcc = alloca(1);
asm ("movl %%esp, %0" : "=g" (orig_esp));
#endif
while(param > params_start) {
param--;
p.qParam = param->q;
switch(param->t+1) {
/* the 8 byte types are implemented by "pushing the high 4 bytes", then falling
through to the "push low 4 bytes" that all the other types do */
case T_DOUBLE:
case T_QUAD:
#if (defined(_MSC_VER) || defined(BORLANDC))
__asm {
;very dangerous/compiler specific
;avoiding indirections, *(ebp+offset), then *(reg+offset[0 or 4])
push dword ptr [p+4];
};
#elif (defined(__GNUC__))
p.qParam = param->q;
/* probably uglier than necessary, but works */
asm ("pushl %0":: "g" (((unsigned int*)&p)[1]));
/* {
int idc;
printf ("dParam = ");
for (idc = 0; idc < sizeof(dParam); idc++) {
printf(" %2.2x",((unsigned char*)&dParam)[idc]);
}
printf(" %f\n", dParam);
} */
#endif /* VC VS GCC */
#ifdef WIN32_API_DEBUG
if(param->t+1 == T_QUAD)
printf("(XS)Win32::API::Call: parameter %d (Q) is %I64d\n", i, param->q);
else
printf("(XS)Win32::API::Call: parameter %d (D) is %f\n", i, param->d);
#endif
// break; /* end of case T_DOUBLE: case T_QUAD: */
#ifdef WIN32_API_DEBUG
/* this branch is all the 32 bit wide stack params together
on non-debug, either its special and a 8 byte, or its
not special and a 4 byte
*/
case T_POINTER:
case T_STRUCTURE:
case T_POINTERPOINTER:
case T_CODE:
case T_NUMBER:
case T_INTEGER:
case T_CHAR:
case T_NUMCHAR:
case T_FLOAT:
#else
default:
#endif
p.pParam = param->p;
#ifdef WIN32_API_DEBUG
if(param->t+1 == T_POINTER)
printf("(XS)Win32::API::Call: parameter %d (P) is 0x%X \"%s\"\n", i, param->l, param->p);
else if(param->t+1 == T_FLOAT)
printf("(XS)Win32::API::Call: parameter %d (F) is %f\n", i, param->f);
else
printf("(XS)Win32::API::Call: parameter %d (N) is %ld\n", i, param->l);
#endif
#if (defined(_MSC_VER) || defined(BORLANDC))
__asm {
push dword ptr [p];
};
#elif (defined(__GNUC__))
p.pParam = param->p;
/* probably uglier than necessary, but works */
asm ("pushl %0":: "g" (((unsigned int*)&p)[0]));
/* {
int idc;
printf ("dParam = ");
for (idc = 0; idc < sizeof(dParam); idc++) {
printf(" %2.2x",((unsigned char*)&dParam)[idc]);
}
printf(" %f\n", dParam);
} */
#endif /* VC VS GCC */
break;
#ifdef WIN32_API_DEBUG
default:
Perl_croak_nocontext("Win32::API::Call Call_asm unknown in type %u @%u", param->t + 1, i);
break;
#endif
}
i--;
}
/* #### NOW CALL THE FUNCTION #### */
//todo, copy retval->t to a c auto, do switch on test c auto, switch might optimize
//to being after the call instruction
{
unsigned char t = control->out;
switch(t WIN32_API_DEBUGM( & ~T_FLAG_UNSIGNED) ) { //unsign has no special treatment here
//group all EAX/EDX readers together, garbage high bytes will be tossed in Call()
#ifdef WIN32_API_DEBUG
/* do the type match only in debug mode, otherwise everything is a EAX/EDX unless
otherwise tested, see way down for the debug mode default: label */
case T_NUMBER:
case T_SHORT:
case T_CHAR:
case T_NUMCHAR:
case T_INTEGER:
case T_VOID:
case T_POINTER:
case T_QUAD:
switch(t & ~T_FLAG_UNSIGNED){
case T_NUMBER:
case T_SHORT:
case T_CHAR:
case T_NUMCHAR:
printf("(XS)Win32::API::Call: Calling ApiFunctionNumber()\n");
break;
case T_INTEGER:
printf("(XS)Win32::API::Call: Calling ApiFunctionInteger()\n");
break;
case T_VOID:
printf("(XS)Win32::API::Call: Calling ApiFunctionVoid() (tout=%d)\n", t);
break;
case T_POINTER:
printf("(XS)Win32::API::Call: Calling ApiFunctionPointer()\n");
break;
case T_QUAD:
printf("(XS)Win32::API::Call: Calling ApiFunctionQuad()\n");
break;
}
#else
default:
#endif /* WIN32_API_DEBUG */
//always capture edx, even if garbage, both lines below are 64 bit
STATIC_ASSERT(sizeof(retval->q) == 8);
retval->q = ((ApiQuad *) control->ApiFunction)();
#ifdef WIN32_API_DEBUG
switch(t & ~T_FLAG_UNSIGNED){
case T_SHORT:
printf("(XS)Win32::API::Call: ApiFunctionInteger (short) returned %hd\n", retval->s);
break;
case T_CHAR:
case T_NUMCHAR:
printf("(XS)Win32::API::Call: ApiFunctionInteger (char) returned %d\n", retval->c);
break;
case T_NUMBER: /* ptr always 32 */
case T_INTEGER:
printf("(XS)Win32::API::Call: ApiFunctionInteger returned %d\n", (int)retval->l);
break;
case T_VOID:
printf("(XS)Win32::API::Call: ApiFunctionVoid returned");
break;
case T_POINTER:
printf("(XS)Win32::API::Call: ApiFunctionPointer returned 0x%x '%s'\n", retval->p, retval->p);
break;
case T_QUAD:
printf("(XS)Win32::API::Call: ApiFunctionQuad returned %I64d\n", retval->q);
break;
}
#endif //WIN32_API_DEBUG
break;
case T_FLOAT:
#ifdef WIN32_API_DEBUG
printf("(XS)Win32::API::Call: Calling ApiFunctionFloat()\n");
#endif
retval->f = ((ApiFloat *) control->ApiFunction)();
#ifdef WIN32_API_DEBUG
printf("(XS)Win32::API::Call: ApiFunctionFloat returned %f\n", retval->f);
#endif
break;
case T_DOUBLE:
#ifdef WIN32_API_DEBUG
printf("(XS)Win32::API::Call: Calling ApiFunctionDouble()\n");
#endif
#if (defined(_MSC_VER) || defined(__BORLANDC__))
/*
_asm {
call dword ptr [ApiFunctionDouble]
fstp qword ptr [dReturn]
}
*/
retval->d = ((ApiDouble *) control->ApiFunction)();
#elif (defined(__GNUC__))
retval->d = ((ApiDouble *) control->ApiFunction)();
/*
asm ("call *%0"::"g" (ApiFunctionDouble));
asm ("fstpl %st(0)");
asm ("movl %0,(%esp)");
*/
#endif
#ifdef WIN32_API_DEBUG //use default: only in debug mode for perf
printf("(XS)Win32::API::Call: ApiFunctionDouble returned %f\n", retval->d);
break;
default:
croak("Win32::API::Call: unknown %s type", "out");
break;
#endif
}
}
//#ifdef _MSC_VER
//__asm {
// cmp esi, esp
// call _RTC_CheckEsp
//}
//#endif
// cleanup stack for _cdecl type functions.
//TODO investigate removing me on VC (possible), and GCC (WIP)
{
unsigned int stack_unwind = (control->whole_bf >> 6) & 0x3FFFC;
#if (defined(_MSC_VER) || defined(__BORLANDC__))
_asm {
add esp, stack_unwind
};
#elif (defined(__GNUC__))
asm (
"movl %0, %%eax\n"
"addl %%eax, %%esp\n"
: /* no output */
: "g" (stack_unwind) /* input */
: "%eax" /* modified registers */
);
{
register void * raw_esp asm("esp");
void * new_esp = raw_esp;
if(raw_esp != orig_esp) {
if(IsDebuggerPresent()) DebugBreak();
else Perl_croak_nocontext(bad_esp_msg, orig_esp, raw_esp);
}
}
#endif
}
}
#ifdef __GNUC__
# if GCC_VERSION >= 40400
# pragma GCC pop_options
# endif
#endif
#else /* Call_asm is in a different compliand */
#ifdef __cplusplus
extern "C"
#else
extern
#endif
void __fastcall Call_asm(const APIPARAM * param /*in caller, this a * to after the last
initialized struct, on entry, param is
always pointing to uninit memory*/,
const APIPARAM * const params_start,
const APICONTROL * const control,
APIPARAM_U * const retval);
#endif /* #if 0*/