Fixed a few prototypes in the USER driver.
[wine] / tools / winebuild / relay.c
1 /*
2  * Relay calls helper routines
3  *
4  * Copyright 1993 Robert J. Amstadt
5  * Copyright 1995 Martin von Loewis
6  * Copyright 1995, 1996, 1997 Alexandre Julliard
7  * Copyright 1997 Eric Youngdale
8  * Copyright 1999 Ulrich Weigand
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26 #include "wine/port.h"
27
28 #include <ctype.h>
29
30 #include "thread.h"
31 #include "wine/winbase16.h"
32
33 #include "build.h"
34
35 static void function_header( FILE *outfile, const char *name )
36 {
37     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
38     fprintf( outfile, "\t%s\n", func_declaration(name) );
39     fprintf( outfile, "\t.globl %s\n", asm_name(name) );
40     fprintf( outfile, "%s:\n", asm_name(name) );
41 }
42
43
44 static void function_footer( FILE *outfile, const char *name )
45 {
46     const char *size = func_size( name );
47     if (size[0]) fprintf( outfile, "\t%s\n", size );
48 }
49
50 static inline const char *data16_prefix(void)
51 {
52     return (target_platform == PLATFORM_SVR4) ? "\tdata16\n" : "";
53 }
54
55 /*******************************************************************
56  *         BuildCallFrom16Core
57  *
58  * This routine builds the core routines used in 16->32 thunks:
59  * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
60  *
61  * These routines are intended to be called via a far call (with 32-bit
62  * operand size) from 16-bit code.  The 16-bit code stub must push %bp,
63  * the 32-bit entry point to be called, and the argument conversion
64  * routine to be used (see stack layout below).
65  *
66  * The core routine completes the STACK16FRAME on the 16-bit stack and
67  * switches to the 32-bit stack.  Then, the argument conversion routine
68  * is called; it gets passed the 32-bit entry point and a pointer to the
69  * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
70  * use conversion routines automatically generated by BuildCallFrom16,
71  * or write your own for special purposes.)
72  *
73  * The conversion routine must call the 32-bit entry point, passing it
74  * the converted arguments, and return its return value to the core.
75  * After the conversion routine has returned, the core switches back
76  * to the 16-bit stack, converts the return value to the DX:AX format
77  * (CallFrom16Long), and returns to the 16-bit call stub.  All parameters,
78  * including %bp, are popped off the stack.
79  *
80  * The 16-bit call stub now returns to the caller, popping the 16-bit
81  * arguments if necessary (pascal calling convention).
82  *
83  * In the case of a 'register' function, CallFrom16Register fills a
84  * CONTEXT86 structure with the values all registers had at the point
85  * the first instruction of the 16-bit call stub was about to be
86  * executed.  A pointer to this CONTEXT86 is passed as third parameter
87  * to the argument conversion routine, which typically passes it on
88  * to the called 32-bit entry point.
89  *
90  * CallFrom16Thunk is a special variant used by the implementation of
91  * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
92  * implemented as follows:
93  * On entry, the EBX register is set up to contain a flat pointer to the
94  * 16-bit stack such that EBX+22 points to the first argument.
95  * Then, the entry point is called, while EBP is set up to point
96  * to the return address (on the 32-bit stack).
97  * The called function returns with CX set to the number of bytes
98  * to be popped of the caller's stack.
99  *
100  * Stack layout upon entry to the core routine (STACK16FRAME):
101  *  ...           ...
102  * (sp+24) word   first 16-bit arg
103  * (sp+22) word   cs
104  * (sp+20) word   ip
105  * (sp+18) word   bp
106  * (sp+14) long   32-bit entry point (reused for Win16 mutex recursion count)
107  * (sp+12) word   ip of actual entry point (necessary for relay debugging)
108  * (sp+8)  long   relay (argument conversion) function entry point
109  * (sp+4)  long   cs of 16-bit entry point
110  * (sp)    long   ip of 16-bit entry point
111  *
112  * Added on the stack:
113  * (sp-2)  word   saved gs
114  * (sp-4)  word   saved fs
115  * (sp-6)  word   saved es
116  * (sp-8)  word   saved ds
117  * (sp-12) long   saved ebp
118  * (sp-16) long   saved ecx
119  * (sp-20) long   saved edx
120  * (sp-24) long   saved previous stack
121  */
122 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
123 {
124     const char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long";
125
126     /* Function header */
127     if (thunk) function_header( outfile, "__wine_call_from_16_thunk" );
128     else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" );
129     else if (short_ret) function_header( outfile, "__wine_call_from_16_word" );
130     else function_header( outfile, "__wine_call_from_16_long" );
131
132     /* Create STACK16FRAME (except STACK32FRAME link) */
133     fprintf( outfile, "\tpushw %%gs\n" );
134     fprintf( outfile, "\tpushw %%fs\n" );
135     fprintf( outfile, "\tpushw %%es\n" );
136     fprintf( outfile, "\tpushw %%ds\n" );
137     fprintf( outfile, "\tpushl %%ebp\n" );
138     fprintf( outfile, "\tpushl %%ecx\n" );
139     fprintf( outfile, "\tpushl %%edx\n" );
140
141     /* Save original EFlags register */
142     if (reg_func) fprintf( outfile, "\tpushfl\n" );
143
144     if ( UsePIC )
145     {
146         fprintf( outfile, "\tcall .Lcall_from_16_%s.getpc\n", name );
147         fprintf( outfile, ".Lcall_from_16_%s.getpc:\n", name );
148         fprintf( outfile, "\tpopl %%ecx\n" );
149         fprintf( outfile, "\t.byte 0x2e\n\tmovl %s-.Lcall_from_16_%s.getpc(%%ecx),%%edx\n",
150                  asm_name("CallTo16_DataSelector"), name );
151     }
152     else
153         fprintf( outfile, "\t.byte 0x2e\n\tmovl %s,%%edx\n", asm_name("CallTo16_DataSelector") );
154
155     /* Load 32-bit segment registers */
156     fprintf( outfile, "%s\tmovw %%dx, %%ds\n", data16_prefix() );
157     fprintf( outfile, "%s\tmovw %%dx, %%es\n", data16_prefix() );
158
159     if ( UsePIC )
160         fprintf( outfile, "\tmovw %s-.Lcall_from_16_%s.getpc(%%ecx), %%fs\n",
161                  asm_name("CallTo16_TebSelector"), name );
162     else
163         fprintf( outfile, "\tmovw %s, %%fs\n", asm_name("CallTo16_TebSelector") );
164
165     fprintf( outfile, "\t.byte 0x64\n\tmov (%d),%%gs\n", STRUCTOFFSET(TEB,gs_sel) );
166
167     /* Translate STACK16FRAME base to flat offset in %edx */
168     fprintf( outfile, "\tmovw %%ss, %%dx\n" );
169     fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
170     fprintf( outfile, "\tshrl $1, %%edx\n" );
171     if (UsePIC)
172     {
173         fprintf( outfile, "\taddl wine_ldt_copy_ptr-.Lcall_from_16_%s.getpc(%%ecx),%%edx\n", name );
174         fprintf( outfile, "\tmovl (%%edx), %%edx\n" );
175     }
176     else
177         fprintf( outfile, "\tmovl %s(%%edx), %%edx\n", asm_name("wine_ldt_copy") );
178     fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
179     fprintf( outfile, "\tleal %d(%%ebp,%%edx), %%edx\n", reg_func ? 0 : -4 );
180
181     /* Get saved flags into %ecx */
182     if (reg_func) fprintf( outfile, "\tpopl %%ecx\n" );
183
184     /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
185     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
186     fprintf( outfile, "\tpushl %%ebp\n" );
187
188     /* Switch stacks */
189     fprintf( outfile, "%s\t.byte 0x64\n\tmovw %%ss, (%d)\n", data16_prefix(), STACKOFFSET + 2 );
190     fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
191     fprintf( outfile, "\tpushl %%ds\n" );
192     fprintf( outfile, "\tpopl %%ss\n" );
193     fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
194     fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
195
196
197     /* At this point:
198        STACK16FRAME is completely set up
199        DS, ES, SS: flat data segment
200        FS: current TEB
201        ESP: points to last STACK32FRAME
202        EBP: points to ebp member of last STACK32FRAME
203        EDX: points to current STACK16FRAME
204        ECX: contains saved flags
205        all other registers: unchanged */
206
207     /* Special case: C16ThkSL stub */
208     if ( thunk )
209     {
210         /* Set up registers as expected and call thunk */
211         fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
212         fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
213
214         fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
215
216         /* Switch stack back */
217         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
218         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
219         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
220
221         /* Restore registers and return directly to caller */
222         fprintf( outfile, "\taddl $8, %%esp\n" );
223         fprintf( outfile, "\tpopl %%ebp\n" );
224         fprintf( outfile, "\tpopw %%ds\n" );
225         fprintf( outfile, "\tpopw %%es\n" );
226         fprintf( outfile, "\tpopw %%fs\n" );
227         fprintf( outfile, "\tpopw %%gs\n" );
228         fprintf( outfile, "\taddl $20, %%esp\n" );
229
230         fprintf( outfile, "\txorb %%ch, %%ch\n" );
231         fprintf( outfile, "\tpopl %%ebx\n" );
232         fprintf( outfile, "\taddw %%cx, %%sp\n" );
233         fprintf( outfile, "\tpush %%ebx\n" );
234
235         fprintf( outfile, "\t.byte 0x66\n" );
236         fprintf( outfile, "\tlret\n" );
237
238         return;
239     }
240
241
242     /* Build register CONTEXT */
243     if ( reg_func )
244     {
245         fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
246
247         fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
248
249         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
250         fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
251         fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
252         fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
253
254         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
255         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
256         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
257         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
258         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
259         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
260
261         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
262         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
263         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
264         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
265         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
266         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
267         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
268         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
269
270         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
271         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
272         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
273         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
274
275         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
276         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
277         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
278         fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
279         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
280 #if 0
281         fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
282 #endif
283
284         /* Push address of CONTEXT86 structure -- popped by the relay routine */
285         fprintf( outfile, "\tpushl %%esp\n" );
286     }
287
288     /* Call relay routine (which will call the API entry point) */
289     fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
290     fprintf( outfile, "\tpushl %%eax\n" );
291     fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
292     fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
293
294     if ( reg_func )
295     {
296         fprintf( outfile, "\tleal -%d(%%ebp), %%ebx\n",
297                  sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
298
299         /* Switch stack back */
300         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
301         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
302         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
303
304         /* Get return address to CallFrom16 stub */
305         fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
306         fprintf( outfile, "\tpopl %%eax\n" );
307         fprintf( outfile, "\tpopl %%edx\n" );
308
309         /* Restore all registers from CONTEXT */
310         fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
311         fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
312         fprintf( outfile, "\taddl $4, %%esp\n" );  /* room for final return address */
313
314         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
315         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
316         fprintf( outfile, "\tpushl %%edx\n" );
317         fprintf( outfile, "\tpushl %%eax\n" );
318         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
319         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
320
321         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegEs) );
322         fprintf( outfile, "\tpopl %%es\n" );
323         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegFs) );
324         fprintf( outfile, "\tpopl %%fs\n" );
325         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegGs) );
326         fprintf( outfile, "\tpopl %%gs\n" );
327
328         fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
329         fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
330         fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
331         fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
332         fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
333         fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
334         fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
335
336         fprintf( outfile, "\tpopl %%ds\n" );
337         fprintf( outfile, "\tpopfl\n" );
338         fprintf( outfile, "\tlret\n" );
339     }
340     else
341     {
342         /* Switch stack back */
343         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
344         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
345         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
346
347         /* Restore registers */
348         fprintf( outfile, "\tpopl %%edx\n" );
349         fprintf( outfile, "\tpopl %%ecx\n" );
350         fprintf( outfile, "\tpopl %%ebp\n" );
351         fprintf( outfile, "\tpopw %%ds\n" );
352         fprintf( outfile, "\tpopw %%es\n" );
353         fprintf( outfile, "\tpopw %%fs\n" );
354         fprintf( outfile, "\tpopw %%gs\n" );
355
356         /* Prepare return value and set flags accordingly */
357         if ( !short_ret )
358             fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
359         fprintf( outfile, "\torl %%eax, %%eax\n" );
360
361         /* Return to return stub which will return to caller */
362         fprintf( outfile, "\tlret $12\n" );
363     }
364     if (thunk) function_footer( outfile, "__wine_call_from_16_thunk" );
365     else if (reg_func) function_footer( outfile, "__wine_call_from_16_regs" );
366     else if (short_ret) function_footer( outfile, "__wine_call_from_16_word" );
367     else function_footer( outfile, "__wine_call_from_16_long" );
368 }
369
370
371 /*******************************************************************
372  *         BuildCallTo16Core
373  *
374  * This routine builds the core routines used in 32->16 thunks:
375  *
376  * extern DWORD WINAPI wine_call_to_16( FARPROC16 target, DWORD cbArgs, PEXCEPTION_HANDLER handler );
377  * extern void WINAPI wine_call_to_16_regs( CONTEXT86 *context, DWORD cbArgs, PEXCEPTION_HANDLER handler );
378  *
379  * These routines can be called directly from 32-bit code.
380  *
381  * All routines expect that the 16-bit stack contents (arguments) and the
382  * return address (segptr to CallTo16_Ret) were already set up by the
383  * caller; nb_args must contain the number of bytes to be conserved.  The
384  * 16-bit SS:SP will be set accordinly.
385  *
386  * All other registers are either taken from the CONTEXT86 structure
387  * or else set to default values.  The target routine address is either
388  * given directly or taken from the CONTEXT86.
389  */
390 static void BuildCallTo16Core( FILE *outfile, int reg_func )
391 {
392     const char *name = reg_func ? "wine_call_to_16_regs" : "wine_call_to_16";
393
394     /* Function header */
395     function_header( outfile, name );
396
397     /* Function entry sequence */
398     fprintf( outfile, "\tpushl %%ebp\n" );
399     fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
400
401     /* Save the 32-bit registers */
402     fprintf( outfile, "\tpushl %%ebx\n" );
403     fprintf( outfile, "\tpushl %%esi\n" );
404     fprintf( outfile, "\tpushl %%edi\n" );
405     fprintf( outfile, "\t.byte 0x64\n\tmov %%gs,(%d)\n", STRUCTOFFSET(TEB,gs_sel) );
406
407     /* Setup exception frame */
408     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
409     fprintf( outfile, "\tpushl 16(%%ebp)\n" ); /* handler */
410     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
411     fprintf( outfile, "\t.byte 0x64\n\tmovl %%esp,(%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
412
413     /* Call the actual CallTo16 routine (simulate a lcall) */
414     fprintf( outfile, "\tpushl %%cs\n" );
415     fprintf( outfile, "\tcall .L%s\n", name );
416
417     /* Remove exception frame */
418     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STRUCTOFFSET(TEB,Tib.ExceptionList) );
419     fprintf( outfile, "\taddl $4, %%esp\n" );
420     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
421
422     if ( !reg_func )
423     {
424         /* Convert return value */
425         fprintf( outfile, "\tandl $0xffff,%%eax\n" );
426         fprintf( outfile, "\tshll $16,%%edx\n" );
427         fprintf( outfile, "\torl %%edx,%%eax\n" );
428     }
429     else
430     {
431         /*
432          * Modify CONTEXT86 structure to contain new values
433          *
434          * NOTE:  We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
435          *        The segment registers as well as ESI and EDI should
436          *        not be modified by a well-behaved 16-bit routine in
437          *        any case.  [If necessary, we could restore them as well,
438          *        at the cost of a somewhat less efficient return path.]
439          */
440
441         fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target) - STACK32OFFSET(edi));
442                 /* everything above edi has been popped already */
443
444         fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
445         fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
446         fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
447         fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
448         fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
449         fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
450                  /* The return glue code saved %esp into %esi */
451     }
452
453     /* Restore the 32-bit registers */
454     fprintf( outfile, "\tpopl %%edi\n" );
455     fprintf( outfile, "\tpopl %%esi\n" );
456     fprintf( outfile, "\tpopl %%ebx\n" );
457
458     /* Function exit sequence */
459     fprintf( outfile, "\tpopl %%ebp\n" );
460     fprintf( outfile, "\tret $12\n" );
461
462
463     /* Start of the actual CallTo16 routine */
464
465     fprintf( outfile, ".L%s:\n", name );
466
467     /* Switch to the 16-bit stack */
468     fprintf( outfile, "\tmovl %%esp,%%edx\n" );
469     fprintf( outfile, "%s\t.byte 0x64\n\tmovw (%d),%%ss\n", data16_prefix(), STACKOFFSET + 2);
470     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
471     fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
472
473     /* Make %bp point to the previous stackframe (built by CallFrom16) */
474     fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
475     fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
476
477     /* Add the specified offset to the new sp */
478     fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
479
480     if (reg_func)
481     {
482         /* Push the called routine address */
483         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
484         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
485         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
486
487         /* Get the registers */
488         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
489         fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegEs) );
490         fprintf( outfile, "\tpopl %%es\n" );
491         fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegFs) );
492         fprintf( outfile, "\tpopl %%fs\n" );
493         fprintf( outfile, "\tpushl %d(%%edx)\n", CONTEXTOFFSET(SegGs) );
494         fprintf( outfile, "\tpopl %%gs\n" );
495         fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
496         fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
497         fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
498         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
499         fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
500         fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
501         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
502
503         /* Get the 16-bit ds */
504         fprintf( outfile, "\tpopw %%ds\n" );
505     }
506     else  /* not a register function */
507     {
508         /* Push the called routine address */
509         fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
510
511         /* Set %fs and %gs to the value saved by the last CallFrom16 */
512         fprintf( outfile, "\tpushw %d(%%ebp)\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
513         fprintf( outfile, "\tpopw %%fs\n" );
514         fprintf( outfile, "\tpushw %d(%%ebp)\n", STACK16OFFSET(gs)-STACK16OFFSET(bp) );
515         fprintf( outfile, "\tpopw %%gs\n" );
516
517         /* Set %ds and %es (and %ax just in case) equal to %ss */
518         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
519         fprintf( outfile, "\tmovw %%ax,%%ds\n" );
520         fprintf( outfile, "\tmovw %%ax,%%es\n" );
521     }
522
523     /* Jump to the called routine */
524     fprintf( outfile, "\t.byte 0x66\n" );
525     fprintf( outfile, "\tlret\n" );
526
527     /* Function footer */
528     function_footer( outfile, name );
529 }
530
531
532 /*******************************************************************
533  *         BuildRet16Func
534  *
535  * Build the return code for 16-bit callbacks
536  */
537 static void BuildRet16Func( FILE *outfile )
538 {
539     function_header( outfile, "__wine_call_to_16_ret" );
540
541     /* Save %esp into %esi */
542     fprintf( outfile, "\tmovl %%esp,%%esi\n" );
543
544     /* Restore 32-bit segment registers */
545
546     fprintf( outfile, "\t.byte 0x2e\n\tmovl %s", asm_name("CallTo16_DataSelector") );
547     fprintf( outfile, "-%s,%%edi\n", asm_name("__wine_call16_start") );
548     fprintf( outfile, "%s\tmovw %%di,%%ds\n", data16_prefix() );
549     fprintf( outfile, "%s\tmovw %%di,%%es\n", data16_prefix() );
550
551     fprintf( outfile, "\t.byte 0x2e\n\tmov %s", asm_name("CallTo16_TebSelector") );
552     fprintf( outfile, "-%s,%%fs\n", asm_name("__wine_call16_start") );
553
554     fprintf( outfile, "\t.byte 0x64\n\tmov (%d),%%gs\n", STRUCTOFFSET(TEB,gs_sel) );
555
556     /* Restore the 32-bit stack */
557
558     fprintf( outfile, "%s\tmovw %%di,%%ss\n", data16_prefix() );
559     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
560
561     /* Return to caller */
562
563     fprintf( outfile, "\tlret\n" );
564     function_footer( outfile, "__wine_call_to_16_ret" );
565 }
566
567
568 /*******************************************************************
569  *         BuildCallTo32CBClient
570  *
571  * Call a CBClient relay stub from 32-bit code (KERNEL.620).
572  *
573  * Since the relay stub is itself 32-bit, this should not be a problem;
574  * unfortunately, the relay stubs are expected to switch back to a
575  * 16-bit stack (and 16-bit code) after completion :-(
576  *
577  * This would conflict with our 16- vs. 32-bit stack handling, so
578  * we simply switch *back* to our 32-bit stack before returning to
579  * the caller ...
580  *
581  * The CBClient relay stub expects to be called with the following
582  * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
583  * stack at the designated places:
584  *
585  *    ...
586  *  (ebp+14) original arguments to the callback routine
587  *  (ebp+10) far return address to original caller
588  *  (ebp+6)  Thunklet target address
589  *  (ebp+2)  Thunklet relay ID code
590  *  (ebp)    BP (saved by CBClientGlueSL)
591  *  (ebp-2)  SI (saved by CBClientGlueSL)
592  *  (ebp-4)  DI (saved by CBClientGlueSL)
593  *  (ebp-6)  DS (saved by CBClientGlueSL)
594  *
595  *   ...     buffer space used by the 16-bit side glue for temp copies
596  *
597  *  (ebx+4)  far return address to 16-bit side glue code
598  *  (ebx)    saved 16-bit ss:sp (pointing to ebx+4)
599  *
600  * The 32-bit side glue code accesses both the original arguments (via ebp)
601  * and the temporary copies prepared by the 16-bit side glue (via ebx).
602  * After completion, the stub will load ss:sp from the buffer at ebx
603  * and perform a far return to 16-bit code.
604  *
605  * To trick the relay stub into returning to us, we replace the 16-bit
606  * return address to the glue code by a cs:ip pair pointing to our
607  * return entry point (the original return address is saved first).
608  * Our return stub thus called will then reload the 32-bit ss:esp and
609  * return to 32-bit code (by using and ss:esp value that we have also
610  * pushed onto the 16-bit stack before and a cs:eip values found at
611  * that position on the 32-bit stack).  The ss:esp to be restored is
612  * found relative to the 16-bit stack pointer at:
613  *
614  *  (ebx-4)   ss  (flat)
615  *  (ebx-8)   sp  (32-bit stack pointer)
616  *
617  * The second variant of this routine, CALL32_CBClientEx, which is used
618  * to implement KERNEL.621, has to cope with yet another problem: Here,
619  * the 32-bit side directly returns to the caller of the CBClient thunklet,
620  * restoring registers saved by CBClientGlueSL and cleaning up the stack.
621  * As we have to return to our 32-bit code first, we have to adapt the
622  * layout of our temporary area so as to include values for the registers
623  * that are to be restored, and later (in the implementation of KERNEL.621)
624  * we *really* restore them. The return stub restores DS, DI, SI, and BP
625  * from the stack, skips the next 8 bytes (CBClient relay code / target),
626  * and then performs a lret NN, where NN is the number of arguments to be
627  * removed. Thus, we prepare our temporary area as follows:
628  *
629  *     (ebx+22) 16-bit cs  (this segment)
630  *     (ebx+20) 16-bit ip  ('16-bit' return entry point)
631  *     (ebx+16) 32-bit ss  (flat)
632  *     (ebx+12) 32-bit sp  (32-bit stack pointer)
633  *     (ebx+10) 16-bit bp  (points to ebx+24)
634  *     (ebx+8)  16-bit si  (ignored)
635  *     (ebx+6)  16-bit di  (ignored)
636  *     (ebx+4)  16-bit ds  (we actually use the flat DS here)
637  *     (ebx+2)  16-bit ss  (16-bit stack segment)
638  *     (ebx+0)  16-bit sp  (points to ebx+4)
639  *
640  * Note that we ensure that DS is not changed and remains the flat segment,
641  * and the 32-bit stack pointer our own return stub needs fits just
642  * perfectly into the 8 bytes that are skipped by the Windows stub.
643  * One problem is that we have to determine the number of removed arguments,
644  * as these have to be really removed in KERNEL.621. Thus, the BP value
645  * that we place in the temporary area to be restored, contains the value
646  * that SP would have if no arguments were removed. By comparing the actual
647  * value of SP with this value in our return stub we can compute the number
648  * of removed arguments. This is then returned to KERNEL.621.
649  *
650  * The stack layout of this function:
651  * (ebp+20)  nArgs     pointer to variable receiving nr. of args (Ex only)
652  * (ebp+16)  esi       pointer to caller's esi value
653  * (ebp+12)  arg       ebp value to be set for relay stub
654  * (ebp+8)   func      CBClient relay stub address
655  * (ebp+4)   ret addr
656  * (ebp)     ebp
657  */
658 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
659 {
660     function_header( outfile, isEx ? "CALL32_CBClientEx" : "CALL32_CBClient" );
661
662     /* Entry code */
663
664     fprintf( outfile, "\tpushl %%ebp\n" );
665     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
666     fprintf( outfile, "\tpushl %%edi\n" );
667     fprintf( outfile, "\tpushl %%esi\n" );
668     fprintf( outfile, "\tpushl %%ebx\n" );
669
670     /* Get pointer to temporary area and save the 32-bit stack pointer */
671
672     fprintf( outfile, "\tmovl 16(%%ebp), %%ebx\n" );
673     fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
674
675     if ( !isEx )
676         fprintf( outfile, "\tmovl %%eax, -8(%%ebx)\n" );
677     else
678         fprintf( outfile, "\tmovl %%eax, 12(%%ebx)\n" );
679
680     /* Set up registers and call CBClient relay stub (simulating a far call) */
681
682     fprintf( outfile, "\tmovl 20(%%ebp), %%esi\n" );
683     fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
684
685     fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
686     fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
687
688     fprintf( outfile, "\tpushl %%cs\n" );
689     fprintf( outfile, "\tcall *%%eax\n" );
690
691     /* Return new esi value to caller */
692
693     fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
694     fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
695
696     /* Return argument size to caller */
697     if ( isEx )
698     {
699         fprintf( outfile, "\tmovl 36(%%esp), %%ebx\n" );
700         fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
701     }
702
703     /* Restore registers and return */
704
705     fprintf( outfile, "\tpopl %%ebx\n" );
706     fprintf( outfile, "\tpopl %%esi\n" );
707     fprintf( outfile, "\tpopl %%edi\n" );
708     fprintf( outfile, "\tpopl %%ebp\n" );
709     fprintf( outfile, "\tret\n" );
710     function_footer( outfile, isEx ? "CALL32_CBClientEx" : "CALL32_CBClient" );
711
712     /* '16-bit' return stub */
713
714     function_header( outfile, isEx ? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
715     if ( !isEx )
716     {
717         fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
718         fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
719     }
720     else
721     {
722         fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
723         fprintf( outfile, "\tsubw %%bp, %%sp\n" );
724         fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
725         fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
726     }
727     fprintf( outfile, "\tlret\n" );
728     function_footer( outfile, isEx ? "CALL32_CBClientEx_Ret" : "CALL32_CBClient_Ret" );
729 }
730
731
732 /*******************************************************************
733  *         BuildCallFrom32Regs
734  *
735  * Build a 32-bit-to-Wine call-back function for a 'register' function.
736  * 'args' is the number of dword arguments.
737  *
738  * Stack layout:
739  *   ...
740  * (ebp+12)  first arg
741  * (ebp+8)   ret addr to user code
742  * (ebp+4)   ret addr to relay code
743  * (ebp+0)   saved ebp
744  * (ebp-128) buffer area to allow stack frame manipulation
745  * (ebp-332) CONTEXT86 struct
746  * (ebp-336) CONTEXT86 *argument
747  *  ....     other arguments copied from (ebp+12)
748  *
749  * The entry point routine is called with a CONTEXT* extra argument,
750  * following the normal args. In this context structure, EIP_reg
751  * contains the return address to user code, and ESP_reg the stack
752  * pointer on return (with the return address and arguments already
753  * removed).
754  */
755 static void BuildCallFrom32Regs( FILE *outfile )
756 {
757     static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
758
759     /* Function header */
760
761     function_header( outfile, "__wine_call_from_32_regs" );
762
763     /* Allocate some buffer space on the stack */
764
765     fprintf( outfile, "\tpushl %%ebp\n" );
766     fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
767     fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
768
769     /* Build the context structure */
770
771     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
772     fprintf( outfile, "\tpushfl\n" );
773     fprintf( outfile, "\tpopl %%eax\n" );
774     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
775     fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
776     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
777     fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
778     fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
779     fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
780     fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
781     fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
782
783     fprintf( outfile, "\txorl %%eax,%%eax\n" );
784     fprintf( outfile, "\tmovw %%cs,%%ax\n" );
785     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
786     fprintf( outfile, "\tmovw %%es,%%ax\n" );
787     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
788     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
789     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
790     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
791     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
792     fprintf( outfile, "\tmovw %%ss,%%ax\n" );
793     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
794     fprintf( outfile, "\tmovw %%ds,%%ax\n" );
795     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
796     fprintf( outfile, "\tmovw %%ax,%%es\n" );  /* set %es equal to %ds just in case */
797
798     fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
799     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
800
801     fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
802     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
803
804     /* Transfer the arguments */
805
806     fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" );   /* get relay code addr */
807     fprintf( outfile, "\tpushl %%esp\n" );           /* push ptr to context struct */
808     fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
809     fprintf( outfile, "\tjecxz 1f\n" );
810     fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
811     fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" );  /* get %esp at time of call */
812     fprintf( outfile, "\tmovl %%esp,%%edi\n" );
813     fprintf( outfile, "\tshrl $2,%%ecx\n" );
814     fprintf( outfile, "\tcld\n" );
815     fprintf( outfile, "\trep\n\tmovsl\n" );  /* copy args */
816
817     fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
818     fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
819     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
820
821     /* Call the entry point */
822
823     fprintf( outfile, "\taddl (%%ebx),%%ebx\n" );
824     fprintf( outfile, "\tcall *%%ebx\n" );
825     fprintf( outfile, "\tleal -%d(%%ebp),%%ecx\n", STACK_SPACE );
826
827     /* Restore the context structure */
828
829     fprintf( outfile, "2:\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegEs) );
830     fprintf( outfile, "\tpopl %%es\n" );
831     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegFs) );
832     fprintf( outfile, "\tpopl %%fs\n" );
833     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegGs) );
834     fprintf( outfile, "\tpopl %%gs\n" );
835
836     fprintf( outfile, "\tmovl %d(%%ecx),%%edi\n", CONTEXTOFFSET(Edi) );
837     fprintf( outfile, "\tmovl %d(%%ecx),%%esi\n", CONTEXTOFFSET(Esi) );
838     fprintf( outfile, "\tmovl %d(%%ecx),%%edx\n", CONTEXTOFFSET(Edx) );
839     fprintf( outfile, "\tmovl %d(%%ecx),%%ebx\n", CONTEXTOFFSET(Ebx) );
840     fprintf( outfile, "\tmovl %d(%%ecx),%%eax\n", CONTEXTOFFSET(Eax) );
841     fprintf( outfile, "\tmovl %d(%%ecx),%%ebp\n", CONTEXTOFFSET(Ebp) );
842
843     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegSs) );
844     fprintf( outfile, "\tpopl %%ss\n" );
845     fprintf( outfile, "\tmovl %d(%%ecx),%%esp\n", CONTEXTOFFSET(Esp) );
846
847     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(EFlags) );
848     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegCs) );
849     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(Eip) );
850     fprintf( outfile, "\tpushl %d(%%ecx)\n", CONTEXTOFFSET(SegDs) );
851     fprintf( outfile, "\tmovl %d(%%ecx),%%ecx\n", CONTEXTOFFSET(Ecx) );
852
853     fprintf( outfile, "\tpopl %%ds\n" );
854     fprintf( outfile, "\tiret\n" );
855     function_footer( outfile, "__wine_call_from_32_regs" );
856
857     function_header( outfile, "__wine_call_from_32_restore_regs" );
858     fprintf( outfile, "\tleal 4(%%esp),%%ecx\n" );
859     fprintf( outfile, "\tjmp 2b\n" );
860     function_footer( outfile, "__wine_call_from_32_restore_regs" );
861 }
862
863
864 /*******************************************************************
865  *         BuildPendingEventCheck
866  *
867  * Build a function that checks whether there are any
868  * pending DPMI events.
869  *
870  * Stack layout:
871  *   
872  * (sp+12) long   eflags
873  * (sp+6)  long   cs
874  * (sp+2)  long   ip
875  * (sp)    word   fs
876  *
877  * On entry to function, fs register points to a valid TEB.
878  * On exit from function, stack will be popped.
879  */
880 static void BuildPendingEventCheck( FILE *outfile )
881 {
882     /* Function header */
883
884     function_header( outfile, "DPMI_PendingEventCheck" );
885
886     /* Check for pending events. */
887
888     fprintf( outfile, "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
889              STRUCTOFFSET(TEB,vm86_pending) );
890     fprintf( outfile, "\tje %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
891
892     fprintf( outfile, "\t.byte 0x64\n\ttestl $0xffffffff,(%d)\n",
893              STRUCTOFFSET(TEB,dpmi_vif) );
894
895     fprintf( outfile, "\tje %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
896
897     /* Process pending events. */
898
899     fprintf( outfile, "\tsti\n" );
900
901     /* Start cleanup. Restore fs register. */
902
903     fprintf( outfile, "\t.globl %s\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
904     fprintf( outfile, "%s:\n", asm_name("DPMI_PendingEventCheck_Cleanup") );
905     fprintf( outfile, "\tpopw %%fs\n" );
906
907     /* Return from function. */
908
909     fprintf( outfile, "\t.globl %s\n", asm_name("DPMI_PendingEventCheck_Return") );
910     fprintf( outfile, "%s:\n", asm_name("DPMI_PendingEventCheck_Return") );
911     fprintf( outfile, "\tiret\n" );
912
913     function_footer( outfile, "DPMI_PendingEventCheck" );
914 }
915
916
917 /*******************************************************************
918  *         BuildRelays16
919  *
920  * Build all the 16-bit relay callbacks
921  */
922 void BuildRelays16( FILE *outfile )
923 {
924     if (target_cpu != CPU_x86)
925     {
926         fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
927         return;
928     }
929
930     /* File header */
931
932     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
933     fprintf( outfile, "\t.text\n" );
934
935     fprintf( outfile, "%s:\n\n", asm_name("__wine_spec_thunk_text_16") );
936
937     fprintf( outfile, "\t.globl %s\n", asm_name("__wine_call16_start") );
938     fprintf( outfile, "%s:\n", asm_name("__wine_call16_start") );
939
940     /* Standard CallFrom16 routine (WORD return) */
941     BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
942
943     /* Standard CallFrom16 routine (DWORD return) */
944     BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
945
946     /* Register CallFrom16 routine */
947     BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
948
949     /* C16ThkSL CallFrom16 routine */
950     BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
951
952     /* Standard CallTo16 routine */
953     BuildCallTo16Core( outfile, 0 );
954
955     /* Register CallTo16 routine */
956     BuildCallTo16Core( outfile, 1 );
957
958     /* Standard CallTo16 return stub */
959     BuildRet16Func( outfile );
960
961     /* CBClientThunkSL routine */
962     BuildCallTo32CBClient( outfile, FALSE );
963
964     /* CBClientThunkSLEx routine */
965     BuildCallTo32CBClient( outfile, TRUE  );
966
967     /* Pending DPMI events check stub */
968     BuildPendingEventCheck( outfile );
969
970     fprintf( outfile, "\t.globl %s\n", asm_name("__wine_call16_end") );
971     fprintf( outfile, "%s:\n", asm_name("__wine_call16_end") );
972     function_footer( outfile, "__wine_spec_thunk_text_16" );
973
974     /* Declare the return address and data selector variables */
975     fprintf( outfile, "\n\t.data\n\t.align %d\n", get_alignment(4) );
976     fprintf( outfile, "\t.globl %s\n", asm_name("CallTo16_DataSelector") );
977     fprintf( outfile, "%s:\t.long 0\n", asm_name("CallTo16_DataSelector") );
978     fprintf( outfile, "\t.globl %s\n", asm_name("CallTo16_TebSelector") );
979     fprintf( outfile, "%s:\t.long 0\n", asm_name("CallTo16_TebSelector") );
980     if (UsePIC) fprintf( outfile, "wine_ldt_copy_ptr:\t.long %s\n", asm_name("wine_ldt_copy") );
981 }
982
983 /*******************************************************************
984  *         BuildRelays32
985  *
986  * Build all the 32-bit relay callbacks
987  */
988 void BuildRelays32( FILE *outfile )
989 {
990     if (target_cpu != CPU_x86)
991     {
992         fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
993         return;
994     }
995
996     /* File header */
997
998     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
999     fprintf( outfile, "\t.text\n" );
1000     fprintf( outfile, "%s:\n\n", asm_name("__wine_spec_thunk_text_32") );
1001
1002     /* 32-bit register entry point */
1003     BuildCallFrom32Regs( outfile );
1004
1005     function_footer( outfile, "__wine_spec_thunk_text_32" );
1006 }