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