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