Changed the GDI driver interface to pass an opaque PHYSDEV pointer
[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
27 #include <ctype.h>
28
29 #include "thread.h"
30 #include "stackframe.h"
31
32 #include "build.h"
33
34 #ifdef __i386__
35
36 static void function_header( FILE *outfile, const char *name )
37 {
38     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
39 #ifdef USE_STABS
40     fprintf( outfile, "\t.stabs \"%s:F1\",36,0,0," PREFIX "%s\n", name, name);
41 #endif
42 #ifdef NEED_TYPE_IN_DEF
43     fprintf( outfile, "\t.def " PREFIX "%s; .scl 2; .type 32; .endef\n", name );
44 #else
45     fprintf( outfile, "\t.type " PREFIX "%s,@function\n", name );
46 #endif
47     fprintf( outfile, "\t.globl " PREFIX "%s\n", name );
48     fprintf( outfile, PREFIX "%s:\n", name );
49 }
50
51
52 /*******************************************************************
53  *         BuildCallFrom16Core
54  *
55  * This routine builds the core routines used in 16->32 thunks:
56  * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
57  *
58  * These routines are intended to be called via a far call (with 32-bit
59  * operand size) from 16-bit code.  The 16-bit code stub must push %bp,
60  * the 32-bit entry point to be called, and the argument conversion 
61  * routine to be used (see stack layout below).  
62  *
63  * The core routine completes the STACK16FRAME on the 16-bit stack and
64  * switches to the 32-bit stack.  Then, the argument conversion routine 
65  * is called; it gets passed the 32-bit entry point and a pointer to the 
66  * 16-bit arguments (on the 16-bit stack) as parameters. (You can either 
67  * use conversion routines automatically generated by BuildCallFrom16, 
68  * or write your own for special purposes.)
69  * 
70  * The conversion routine must call the 32-bit entry point, passing it
71  * the converted arguments, and return its return value to the core.  
72  * After the conversion routine has returned, the core switches back
73  * to the 16-bit stack, converts the return value to the DX:AX format
74  * (CallFrom16Long), and returns to the 16-bit call stub.  All parameters,
75  * including %bp, are popped off the stack.
76  *
77  * The 16-bit call stub now returns to the caller, popping the 16-bit
78  * arguments if necessary (pascal calling convention).
79  *
80  * In the case of a 'register' function, CallFrom16Register fills a
81  * CONTEXT86 structure with the values all registers had at the point
82  * the first instruction of the 16-bit call stub was about to be 
83  * executed.  A pointer to this CONTEXT86 is passed as third parameter 
84  * to the argument conversion routine, which typically passes it on
85  * to the called 32-bit entry point.
86  *
87  * CallFrom16Thunk is a special variant used by the implementation of 
88  * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is 
89  * implemented as follows:
90  * On entry, the EBX register is set up to contain a flat pointer to the
91  * 16-bit stack such that EBX+22 points to the first argument.
92  * Then, the entry point is called, while EBP is set up to point
93  * to the return address (on the 32-bit stack).
94  * The called function returns with CX set to the number of bytes
95  * to be popped of the caller's stack.
96  *
97  * Stack layout upon entry to the core routine (STACK16FRAME):
98  *  ...           ...
99  * (sp+24) word   first 16-bit arg
100  * (sp+22) word   cs
101  * (sp+20) word   ip
102  * (sp+18) word   bp
103  * (sp+14) long   32-bit entry point (reused for Win16 mutex recursion count)
104  * (sp+12) word   ip of actual entry point (necessary for relay debugging)
105  * (sp+8)  long   relay (argument conversion) function entry point
106  * (sp+4)  long   cs of 16-bit entry point
107  * (sp)    long   ip of 16-bit entry point
108  *
109  * Added on the stack:
110  * (sp-2)  word   saved gs
111  * (sp-4)  word   saved fs
112  * (sp-6)  word   saved es
113  * (sp-8)  word   saved ds
114  * (sp-12) long   saved ebp
115  * (sp-16) long   saved ecx
116  * (sp-20) long   saved edx
117  * (sp-24) long   saved previous stack
118  */
119 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
120 {
121     char *name = thunk? "thunk" : reg_func? "regs" : short_ret? "word" : "long";
122
123     /* Function header */
124     if (thunk) function_header( outfile, "__wine_call_from_16_thunk" );
125     else if (reg_func) function_header( outfile, "__wine_call_from_16_regs" );
126     else if (short_ret) function_header( outfile, "__wine_call_from_16_word" );
127     else function_header( outfile, "__wine_call_from_16_long" );
128
129     /* Create STACK16FRAME (except STACK32FRAME link) */
130     fprintf( outfile, "\tpushw %%gs\n" );
131     fprintf( outfile, "\tpushw %%fs\n" );
132     fprintf( outfile, "\tpushw %%es\n" );
133     fprintf( outfile, "\tpushw %%ds\n" );
134     fprintf( outfile, "\tpushl %%ebp\n" );
135     fprintf( outfile, "\tpushl %%ecx\n" );
136     fprintf( outfile, "\tpushl %%edx\n" );
137
138     /* Save original EFlags register */
139     fprintf( outfile, "\tpushfl\n" );
140
141     if ( UsePIC )
142     {
143         /* Get Global Offset Table into %ecx */
144         fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot1\n", name );
145         fprintf( outfile, ".L__wine_call_from_16_%s.getgot1:\n", name );
146         fprintf( outfile, "\tpopl %%ecx\n" );
147         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot1], %%ecx\n", name );
148     }
149
150     if (UsePIC)
151     {
152         fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector@GOT(%%ecx), %%edx\n" );
153         fprintf( outfile, "\t.byte 0x2e\n\tmovl (%%edx), %%edx\n" );
154     }
155     else
156         fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector,%%edx\n" );
157
158     /* Load 32-bit segment registers */
159 #ifdef __svr4__
160     fprintf( outfile, "\tdata16\n");
161 #endif
162     fprintf( outfile, "\tmovw %%dx, %%ds\n" );
163 #ifdef __svr4__
164     fprintf( outfile, "\tdata16\n");
165 #endif
166     fprintf( outfile, "\tmovw %%dx, %%es\n" );
167
168     if ( UsePIC )
169     {
170         fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
171         fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
172     }
173     else
174         fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
175
176     /* Get address of wine_ldt_copy array into %ecx */
177     if ( UsePIC )
178         fprintf( outfile, "\tmovl " PREFIX "wine_ldt_copy@GOT(%%ecx), %%ecx\n" );
179     else
180         fprintf( outfile, "\tmovl $" PREFIX "wine_ldt_copy, %%ecx\n" );
181
182     /* Translate STACK16FRAME base to flat offset in %edx */
183     fprintf( outfile, "\tmovw %%ss, %%dx\n" );
184     fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
185     fprintf( outfile, "\tshrl $1, %%edx\n" );
186     fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
187     fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
188     fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
189
190     /* Get saved flags into %ecx */
191     fprintf( outfile, "\tpopl %%ecx\n" );
192
193     /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
194     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
195     fprintf( outfile, "\tpushl %%ebp\n" );
196
197     /* Switch stacks */
198 #ifdef __svr4__
199     fprintf( outfile,"\tdata16\n");
200 #endif
201     fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
202     fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
203     fprintf( outfile, "\tpushl %%ds\n" );
204     fprintf( outfile, "\tpopl %%ss\n" );
205     fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
206     fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
207
208
209     /* At this point:
210        STACK16FRAME is completely set up
211        DS, ES, SS: flat data segment
212        FS: current TEB
213        ESP: points to last STACK32FRAME
214        EBP: points to ebp member of last STACK32FRAME
215        EDX: points to current STACK16FRAME
216        ECX: contains saved flags
217        all other registers: unchanged */
218
219     /* Special case: C16ThkSL stub */
220     if ( thunk )
221     {
222         /* Set up registers as expected and call thunk */
223         fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
224         fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
225
226         fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
227
228         /* Switch stack back */
229         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
230         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
231         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
232
233         /* Restore registers and return directly to caller */
234         fprintf( outfile, "\taddl $8, %%esp\n" );
235         fprintf( outfile, "\tpopl %%ebp\n" );
236         fprintf( outfile, "\tpopw %%ds\n" );
237         fprintf( outfile, "\tpopw %%es\n" );
238         fprintf( outfile, "\tpopw %%fs\n" );
239         fprintf( outfile, "\tpopw %%gs\n" );
240         fprintf( outfile, "\taddl $20, %%esp\n" );
241
242         fprintf( outfile, "\txorb %%ch, %%ch\n" );
243         fprintf( outfile, "\tpopl %%ebx\n" );
244         fprintf( outfile, "\taddw %%cx, %%sp\n" );
245         fprintf( outfile, "\tpush %%ebx\n" );
246
247         fprintf( outfile, "\t.byte 0x66\n" );
248         fprintf( outfile, "\tlret\n" );
249
250         return;
251     }
252
253
254     /* Build register CONTEXT */
255     if ( reg_func )
256     {
257         fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
258
259         fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );  
260
261         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
262         fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
263         fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
264         fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
265
266         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
267         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
268         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
269         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
270         fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
271         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
272
273         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
274         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
275         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
276         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
277         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
278         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
279         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
280         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
281
282         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
283         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
284         fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
285         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
286
287         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
288         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
289         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
290         fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
291         fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
292 #if 0
293         fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
294 #endif
295
296         /* Push address of CONTEXT86 structure -- popped by the relay routine */
297         fprintf( outfile, "\tpushl %%esp\n" );
298     }
299
300
301     /* Print debug info before call */
302     if ( debugging )
303     {
304         if ( UsePIC )
305         {
306             fprintf( outfile, "\tpushl %%ebx\n" );
307
308             /* Get Global Offset Table into %ebx (for PLT call) */
309             fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot2\n", name );
310             fprintf( outfile, ".L__wine_call_from_16_%s.getgot2:\n", name );
311             fprintf( outfile, "\tpopl %%ebx\n" );
312             fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot2], %%ebx\n", name );
313         }
314
315         fprintf( outfile, "\tpushl %%edx\n" );
316         if ( reg_func )
317             fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
318                               sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
319         else
320             fprintf( outfile, "\tpushl $0\n" );
321
322         if ( UsePIC )
323             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
324         else
325             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
326
327         fprintf( outfile, "\tpopl %%edx\n" );
328         fprintf( outfile, "\tpopl %%edx\n" );
329
330         if ( UsePIC )
331             fprintf( outfile, "\tpopl %%ebx\n" );
332     }
333
334     /* Call relay routine (which will call the API entry point) */
335     fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
336     fprintf( outfile, "\tpushl %%eax\n" );
337     fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
338     fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
339
340     /* Print debug info after call */
341     if ( debugging )
342     {
343         if ( UsePIC )
344         {
345             fprintf( outfile, "\tpushl %%ebx\n" );
346
347             /* Get Global Offset Table into %ebx (for PLT call) */
348             fprintf( outfile, "\tcall .L__wine_call_from_16_%s.getgot3\n", name );
349             fprintf( outfile, ".L__wine_call_from_16_%s.getgot3:\n", name );
350             fprintf( outfile, "\tpopl %%ebx\n" );
351             fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.L__wine_call_from_16_%s.getgot3], %%ebx\n", name );
352         }
353
354         fprintf( outfile, "\tpushl %%eax\n" );
355         if ( reg_func )
356             fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
357                               sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
358         else
359             fprintf( outfile, "\tpushl $0\n" );
360
361         if ( UsePIC )
362             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
363         else
364             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
365
366         fprintf( outfile, "\tpopl %%eax\n" );
367         fprintf( outfile, "\tpopl %%eax\n" );
368
369         if ( UsePIC )
370             fprintf( outfile, "\tpopl %%ebx\n" );
371     }
372
373
374     if ( reg_func )
375     {
376         fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
377
378         /* Switch stack back */
379         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
380         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
381         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
382
383         /* Get return address to CallFrom16 stub */
384         fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
385         fprintf( outfile, "\tpopl %%eax\n" );
386         fprintf( outfile, "\tpopl %%edx\n" );
387
388         /* Restore all registers from CONTEXT */
389         fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
390         fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
391         fprintf( outfile, "\taddl $4, %%esp\n" );  /* room for final return address */
392
393         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
394         fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
395         fprintf( outfile, "\tpushl %%edx\n" );
396         fprintf( outfile, "\tpushl %%eax\n" );
397         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
398         fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
399
400         fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
401         fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
402         fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
403
404         fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
405         fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
406         fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
407         fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
408         fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
409         fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
410         fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
411   
412         fprintf( outfile, "\tpopl %%ds\n" );
413         fprintf( outfile, "\tpopfl\n" );
414         fprintf( outfile, "\tlret\n" );
415     }
416     else
417     {
418         /* Switch stack back */
419         fprintf( outfile, "\t.byte 0x64\n\tmovw (%d), %%ss\n", STACKOFFSET+2 );
420         fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%esp\n", STACKOFFSET );
421         fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
422
423         /* Restore registers */
424         fprintf( outfile, "\tpopl %%edx\n" );
425         fprintf( outfile, "\tpopl %%ecx\n" );
426         fprintf( outfile, "\tpopl %%ebp\n" );
427         fprintf( outfile, "\tpopw %%ds\n" );
428         fprintf( outfile, "\tpopw %%es\n" );
429         fprintf( outfile, "\tpopw %%fs\n" );
430         fprintf( outfile, "\tpopw %%gs\n" );
431
432         /* Prepare return value and set flags accordingly */
433         if ( !short_ret )
434             fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
435         fprintf( outfile, "\torl %%eax, %%eax\n" );
436
437         /* Return to return stub which will return to caller */
438         fprintf( outfile, "\tlret $12\n" );
439     }
440 }
441   
442
443 /*******************************************************************
444  *         BuildCallTo16Core
445  *
446  * This routine builds the core routines used in 32->16 thunks:
447  *
448  *   extern void WINAPI wine_call_to_16_word( SEGPTR target, int nb_args );
449  *   extern void WINAPI wine_call_to_16_long( SEGPTR target, int nb_args );
450  *   extern void WINAPI wine_call_to_16_regs_short( const CONTEXT86 *context, int nb_args );
451  *   extern void WINAPI wine_call_to_16_regs_long ( const CONTEXT86 *context, int nb_args );
452  *
453  * These routines can be called directly from 32-bit code. 
454  *
455  * All routines expect that the 16-bit stack contents (arguments) were 
456  * already set up by the caller; nb_args must contain the number of bytes 
457  * to be conserved.  The 16-bit SS:SP will be set accordinly.
458  *
459  * All other registers are either taken from the CONTEXT86 structure 
460  * or else set to default values.  The target routine address is either
461  * given directly or taken from the CONTEXT86.
462  *
463  * If you want to call a 16-bit routine taking only standard argument types 
464  * (WORD and LONG), you can also have an appropriate argument conversion 
465  * stub automatically generated (see BuildCallTo16); you'd then call this
466  * stub, which in turn would prepare the 16-bit stack and call the appropiate
467  * core routine.
468  *
469  */
470 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
471 {
472     char *name = reg_func == 2 ? "regs_long" :
473                  reg_func == 1 ? "regs_short" :
474                  short_ret? "word" : "long";
475
476     /* Function header */
477     if (reg_func == 2) function_header( outfile, "wine_call_to_16_regs_long" );
478     else if (reg_func == 1) function_header( outfile, "wine_call_to_16_regs_short" );
479     else if (short_ret) function_header( outfile, "wine_call_to_16_word" );
480     else function_header( outfile, "wine_call_to_16_long" );
481
482     /* Function entry sequence */
483     fprintf( outfile, "\tpushl %%ebp\n" );
484     fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
485
486     /* Save the 32-bit registers */
487     fprintf( outfile, "\tpushl %%ebx\n" );
488     fprintf( outfile, "\tpushl %%ecx\n" );
489     fprintf( outfile, "\tpushl %%edx\n" );
490     fprintf( outfile, "\tpushl %%esi\n" );
491     fprintf( outfile, "\tpushl %%edi\n" );
492
493     if ( UsePIC )
494     {
495         /* Get Global Offset Table into %ebx */
496         fprintf( outfile, "\tcall .Lwine_call_to_16_%s.getgot1\n", name );
497         fprintf( outfile, ".Lwine_call_to_16_%s.getgot1:\n", name );
498         fprintf( outfile, "\tpopl %%ebx\n" );
499         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot1], %%ebx\n", name );
500     }
501
502     /* Enter Win16 Mutex */
503     if ( UsePIC )
504         fprintf( outfile, "\tcall " PREFIX "_EnterWin16Lock@PLT\n" );
505     else
506         fprintf( outfile, "\tcall " PREFIX "_EnterWin16Lock\n" );
507
508     /* Print debugging info */
509     if (debugging)
510     {
511         /* Push flags, number of arguments, and target */
512         fprintf( outfile, "\tpushl $%d\n", reg_func );
513         fprintf( outfile, "\tpushl 12(%%ebp)\n" );
514         fprintf( outfile, "\tpushl  8(%%ebp)\n" );
515
516         if ( UsePIC )
517             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
518         else
519             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
520
521         fprintf( outfile, "\taddl $12, %%esp\n" );
522     }
523
524     /* Get return address */
525     if ( UsePIC )
526     {
527         fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOT(%%ebx), %%ecx\n" );
528         fprintf( outfile, "\tmovl " PREFIX "(%%ecx), %%ecx\n" );
529     }
530     else
531         fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
532
533     /* Call the actual CallTo16 routine (simulate a lcall) */
534     fprintf( outfile, "\tpushl %%cs\n" );
535     fprintf( outfile, "\tcall .Lwine_call_to_16_%s\n", name );
536
537     if ( !reg_func )
538     {
539         /* Convert and push return value */
540         if ( short_ret )
541         {
542             fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
543             fprintf( outfile, "\tpushl %%eax\n" );
544         }
545         else
546         {
547             fprintf( outfile, "\tshll $16,%%edx\n" );
548             fprintf( outfile, "\tmovw %%ax,%%dx\n" );
549             fprintf( outfile, "\tpushl %%edx\n" );
550         }
551     }
552     else
553     {
554         /* 
555          * Modify CONTEXT86 structure to contain new values
556          *
557          * NOTE:  We restore only EAX, EBX, EDX, EDX, EBP, and ESP.
558          *        The segment registers as well as ESI and EDI should
559          *        not be modified by a well-behaved 16-bit routine in
560          *        any case.  [If necessary, we could restore them as well,
561          *        at the cost of a somewhat less efficient return path.]
562          */
563         
564         fprintf( outfile, "\tmovl %d(%%esp), %%edi\n", STACK32OFFSET(target)-12 );
565         fprintf( outfile, "\tmovl %%eax, %d(%%edi)\n", CONTEXTOFFSET(Eax) );
566         fprintf( outfile, "\tmovl %%ebx, %d(%%edi)\n", CONTEXTOFFSET(Ebx) );
567         fprintf( outfile, "\tmovl %%ecx, %d(%%edi)\n", CONTEXTOFFSET(Ecx) );
568         fprintf( outfile, "\tmovl %%edx, %d(%%edi)\n", CONTEXTOFFSET(Edx) );
569         fprintf( outfile, "\tmovl %%ebp, %d(%%edi)\n", CONTEXTOFFSET(Ebp) );
570         fprintf( outfile, "\tmovl %%esi, %d(%%edi)\n", CONTEXTOFFSET(Esp) );
571                  /* The return glue code saved %esp into %esi */
572
573         fprintf( outfile, "\tpushl %%edi\n" );
574     }
575
576     if ( UsePIC )
577     {
578         /* Get Global Offset Table into %ebx (might have been overwritten) */
579         fprintf( outfile, "\tcall .Lwine_call_to_16_%s.getgot2\n", name );
580         fprintf( outfile, ".Lwine_call_to_16_%s.getgot2:\n", name );
581         fprintf( outfile, "\tpopl %%ebx\n" );
582         fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.Lwine_call_to_16_%s.getgot2], %%ebx\n", name );
583     }
584
585     /* Print debugging info */
586     if (debugging)
587     {
588         fprintf( outfile, "\tpushl $%d\n", reg_func );
589
590         if ( UsePIC )
591             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
592         else
593             fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
594
595         fprintf( outfile, "\taddl $4, %%esp\n" );
596     }
597
598     /* Leave Win16 Mutex */
599     if ( UsePIC )
600         fprintf( outfile, "\tcall " PREFIX "_LeaveWin16Lock@PLT\n" );
601     else
602         fprintf( outfile, "\tcall " PREFIX "_LeaveWin16Lock\n" );
603
604     /* Get return value */
605     fprintf( outfile, "\tpopl %%eax\n" );
606
607     /* Restore the 32-bit registers */
608     fprintf( outfile, "\tpopl %%edi\n" );
609     fprintf( outfile, "\tpopl %%esi\n" );
610     fprintf( outfile, "\tpopl %%edx\n" );
611     fprintf( outfile, "\tpopl %%ecx\n" );
612     fprintf( outfile, "\tpopl %%ebx\n" );
613
614     /* Function exit sequence */
615     fprintf( outfile, "\tpopl %%ebp\n" );
616     fprintf( outfile, "\tret $8\n" );
617
618
619     /* Start of the actual CallTo16 routine */
620
621     fprintf( outfile, ".Lwine_call_to_16_%s:\n", name );
622
623     /* Complete STACK32FRAME */
624     fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
625     fprintf( outfile, "\tmovl %%esp,%%edx\n" );
626
627     /* Switch to the 16-bit stack */
628 #ifdef __svr4__
629     fprintf( outfile,"\tdata16\n");
630 #endif
631     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
632     fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
633     fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
634
635     /* Make %bp point to the previous stackframe (built by CallFrom16) */
636     fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
637     fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
638
639     /* Add the specified offset to the new sp */
640     fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
641
642     /* Push the return address 
643      * With sreg suffix, we push 16:16 address (normal lret)
644      * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
645      */
646     if (reg_func != 2)
647         fprintf( outfile, "\tpushl %%ecx\n" );
648     else 
649     {
650         fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
651         fprintf( outfile, "\tpushw $0\n" );
652         fprintf( outfile, "\tpushw %%ax\n" );
653         fprintf( outfile, "\tpushw $0\n" );
654         fprintf( outfile, "\tpushw %%cx\n" );
655     }
656
657     if (reg_func)
658     {
659         /* Push the called routine address */
660         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
661         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
662         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
663
664         /* Get the registers */
665         fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
666         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
667         fprintf( outfile, "\tmovw %%ax,%%es\n" );
668         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
669         fprintf( outfile, "\tmovw %%ax,%%fs\n" );
670         fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
671         fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
672         fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
673         fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
674         fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
675         fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
676         fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
677
678         /* Get the 16-bit ds */
679         fprintf( outfile, "\tpopw %%ds\n" );
680     }
681     else  /* not a register function */
682     {
683         /* Push the called routine address */
684         fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
685
686         /* Set %fs to the value saved by the last CallFrom16 */
687         fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
688         fprintf( outfile, "\tmovw %%ax,%%fs\n" );
689
690         /* Set %ds and %es (and %ax just in case) equal to %ss */
691         fprintf( outfile, "\tmovw %%ss,%%ax\n" );
692         fprintf( outfile, "\tmovw %%ax,%%ds\n" );
693         fprintf( outfile, "\tmovw %%ax,%%es\n" );
694     }
695
696     /* Jump to the called routine */
697     fprintf( outfile, "\t.byte 0x66\n" );
698     fprintf( outfile, "\tlret\n" );
699 }
700
701
702 /*******************************************************************
703  *         BuildRet16Func
704  *
705  * Build the return code for 16-bit callbacks
706  */
707 static void BuildRet16Func( FILE *outfile )
708 {
709     /* 
710      *  Note: This must reside in the .data section to allow
711      *        run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
712      */
713
714     function_header( outfile, "CallTo16_Ret" );
715
716     /* Save %esp into %esi */
717     fprintf( outfile, "\tmovl %%esp,%%esi\n" );
718
719     /* Restore 32-bit segment registers */
720
721     fprintf( outfile, "\t.byte 0x2e\n\tmovl " PREFIX "CallTo16_DataSelector-" PREFIX "Call16_Ret_Start,%%edi\n" );
722 #ifdef __svr4__
723     fprintf( outfile, "\tdata16\n");
724 #endif
725     fprintf( outfile, "\tmovw %%di,%%ds\n" );
726 #ifdef __svr4__
727     fprintf( outfile, "\tdata16\n");
728 #endif
729     fprintf( outfile, "\tmovw %%di,%%es\n" );
730
731     fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
732
733     /* Restore the 32-bit stack */
734
735 #ifdef __svr4__
736     fprintf( outfile, "\tdata16\n");
737 #endif
738     fprintf( outfile, "\tmovw %%di,%%ss\n" );
739     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
740     fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
741
742     /* Return to caller */
743
744     fprintf( outfile, "\tlret\n" );
745
746     /* Declare the return address and data selector variables */
747
748     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
749     fprintf( outfile, "\t.globl " PREFIX "CallTo16_DataSelector\n" );
750     fprintf( outfile, PREFIX "CallTo16_DataSelector:\t.long 0\n" );
751     fprintf( outfile, "\t.globl " PREFIX "CallTo16_RetAddr\n" );
752     fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
753 }
754
755
756 /*******************************************************************
757  *         BuildCallTo32CBClient
758  *
759  * Call a CBClient relay stub from 32-bit code (KERNEL.620).
760  *
761  * Since the relay stub is itself 32-bit, this should not be a problem;
762  * unfortunately, the relay stubs are expected to switch back to a 
763  * 16-bit stack (and 16-bit code) after completion :-(
764  *
765  * This would conflict with our 16- vs. 32-bit stack handling, so
766  * we simply switch *back* to our 32-bit stack before returning to
767  * the caller ...
768  *
769  * The CBClient relay stub expects to be called with the following
770  * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
771  * stack at the designated places:
772  *
773  *    ...
774  *  (ebp+14) original arguments to the callback routine
775  *  (ebp+10) far return address to original caller
776  *  (ebp+6)  Thunklet target address
777  *  (ebp+2)  Thunklet relay ID code
778  *  (ebp)    BP (saved by CBClientGlueSL)
779  *  (ebp-2)  SI (saved by CBClientGlueSL)
780  *  (ebp-4)  DI (saved by CBClientGlueSL)
781  *  (ebp-6)  DS (saved by CBClientGlueSL)
782  *
783  *   ...     buffer space used by the 16-bit side glue for temp copies
784  *
785  *  (ebx+4)  far return address to 16-bit side glue code
786  *  (ebx)    saved 16-bit ss:sp (pointing to ebx+4)
787  *
788  * The 32-bit side glue code accesses both the original arguments (via ebp)
789  * and the temporary copies prepared by the 16-bit side glue (via ebx).
790  * After completion, the stub will load ss:sp from the buffer at ebx
791  * and perform a far return to 16-bit code.  
792  *
793  * To trick the relay stub into returning to us, we replace the 16-bit
794  * return address to the glue code by a cs:ip pair pointing to our
795  * return entry point (the original return address is saved first).
796  * Our return stub thus called will then reload the 32-bit ss:esp and
797  * return to 32-bit code (by using and ss:esp value that we have also
798  * pushed onto the 16-bit stack before and a cs:eip values found at
799  * that position on the 32-bit stack).  The ss:esp to be restored is
800  * found relative to the 16-bit stack pointer at:
801  *
802  *  (ebx-4)   ss  (flat)
803  *  (ebx-8)   sp  (32-bit stack pointer)
804  *
805  * The second variant of this routine, CALL32_CBClientEx, which is used
806  * to implement KERNEL.621, has to cope with yet another problem: Here,
807  * the 32-bit side directly returns to the caller of the CBClient thunklet,
808  * restoring registers saved by CBClientGlueSL and cleaning up the stack.
809  * As we have to return to our 32-bit code first, we have to adapt the
810  * layout of our temporary area so as to include values for the registers
811  * that are to be restored, and later (in the implementation of KERNEL.621)
812  * we *really* restore them. The return stub restores DS, DI, SI, and BP
813  * from the stack, skips the next 8 bytes (CBClient relay code / target),
814  * and then performs a lret NN, where NN is the number of arguments to be
815  * removed. Thus, we prepare our temporary area as follows:
816  *
817  *     (ebx+22) 16-bit cs  (this segment)
818  *     (ebx+20) 16-bit ip  ('16-bit' return entry point)
819  *     (ebx+16) 32-bit ss  (flat)
820  *     (ebx+12) 32-bit sp  (32-bit stack pointer)
821  *     (ebx+10) 16-bit bp  (points to ebx+24)
822  *     (ebx+8)  16-bit si  (ignored)
823  *     (ebx+6)  16-bit di  (ignored)
824  *     (ebx+4)  16-bit ds  (we actually use the flat DS here)
825  *     (ebx+2)  16-bit ss  (16-bit stack segment)
826  *     (ebx+0)  16-bit sp  (points to ebx+4)
827  *
828  * Note that we ensure that DS is not changed and remains the flat segment,
829  * and the 32-bit stack pointer our own return stub needs fits just 
830  * perfectly into the 8 bytes that are skipped by the Windows stub.
831  * One problem is that we have to determine the number of removed arguments,
832  * as these have to be really removed in KERNEL.621. Thus, the BP value 
833  * that we place in the temporary area to be restored, contains the value 
834  * that SP would have if no arguments were removed. By comparing the actual
835  * value of SP with this value in our return stub we can compute the number
836  * of removed arguments. This is then returned to KERNEL.621.
837  *
838  * The stack layout of this function:
839  * (ebp+20)  nArgs     pointer to variable receiving nr. of args (Ex only)
840  * (ebp+16)  esi       pointer to caller's esi value
841  * (ebp+12)  arg       ebp value to be set for relay stub
842  * (ebp+8)   func      CBClient relay stub address
843  * (ebp+4)   ret addr
844  * (ebp)     ebp
845  */
846 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
847 {
848     char *name = isEx? "CBClientEx" : "CBClient";
849     int size = isEx? 24 : 12;
850
851     /* Function header */
852
853     fprintf( outfile, "\n\t.align %d\n", get_alignment(4) );
854 #ifdef USE_STABS
855     fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
856                       name, name );
857 #endif
858     fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
859     fprintf( outfile, PREFIX "CALL32_%s:\n", name );
860
861     /* Entry code */
862
863     fprintf( outfile, "\tpushl %%ebp\n" );
864     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
865     fprintf( outfile, "\tpushl %%edi\n" );
866     fprintf( outfile, "\tpushl %%esi\n" );
867     fprintf( outfile, "\tpushl %%ebx\n" );
868
869     /* Get the 16-bit stack */
870
871     fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
872     
873     /* Convert it to a flat address */
874
875     fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
876     fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
877     fprintf( outfile, "\tshrl $1,%%eax\n" );
878     fprintf( outfile, "\tmovl " PREFIX "wine_ldt_copy(%%eax),%%esi\n" );
879     fprintf( outfile, "\tmovw %%bx,%%ax\n" );
880     fprintf( outfile, "\taddl %%eax,%%esi\n" );
881
882     /* Allocate temporary area (simulate STACK16_PUSH) */
883
884     fprintf( outfile, "\tpushf\n" );
885     fprintf( outfile, "\tcld\n" );
886     fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
887     fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
888     fprintf( outfile, "\trep\n\tmovsb\n" );
889     fprintf( outfile, "\tpopf\n" );
890
891     fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
892
893     fprintf( outfile, "\tpushl %%edi\n" );  /* remember address */
894
895     /* Set up temporary area */
896
897     if ( !isEx )
898     {
899         fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
900
901         fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
902         fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" );    /* 32-bit sp */
903
904         fprintf( outfile, "\tmovw %%ss, %%ax\n" );
905         fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
906         fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" );    /* 32-bit ss */
907
908         fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
909         fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );    /* 16-bit ss:sp */
910
911         fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
912         fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );   /* overwrite return address */
913     }
914     else
915     {
916         fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
917         fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
918
919         fprintf( outfile, "\tmovw %%ds, %%ax\n" );
920         fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
921
922         fprintf( outfile, "\taddl $20, %%ebx\n" );
923         fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
924
925         fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
926         fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
927
928         fprintf( outfile, "\tmovw %%ss, %%ax\n" );
929         fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
930         fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
931
932         fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
933         fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
934     }
935
936     /* Set up registers and call CBClient relay stub (simulating a far call) */
937
938     fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
939     fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
940
941     fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
942     fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
943     fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
944
945     fprintf( outfile, "\tpushl %%cs\n" );
946     fprintf( outfile, "\tcall *%%eax\n" );
947
948     /* Return new esi value to caller */
949
950     fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
951     fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
952
953     /* Cleanup temporary area (simulate STACK16_POP) */
954
955     fprintf( outfile, "\tpop %%esi\n" );
956
957     fprintf( outfile, "\tpushf\n" );
958     fprintf( outfile, "\tstd\n" );
959     fprintf( outfile, "\tdec %%esi\n" );
960     fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
961     fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
962     fprintf( outfile, "\trep\n\tmovsb\n" );
963     fprintf( outfile, "\tpopf\n" );
964
965     fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
966
967     /* Return argument size to caller */
968     if ( isEx )
969     {
970         fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
971         fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
972     }
973
974     /* Restore registers and return */
975
976     fprintf( outfile, "\tpopl %%ebx\n" );
977     fprintf( outfile, "\tpopl %%esi\n" );
978     fprintf( outfile, "\tpopl %%edi\n" );
979     fprintf( outfile, "\tpopl %%ebp\n" );
980     fprintf( outfile, "\tret\n" );
981 }
982
983 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
984 {
985     char *name = isEx? "CBClientEx" : "CBClient";
986
987     /* '16-bit' return stub */
988
989     fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
990     fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
991
992     if ( !isEx )
993     {
994         fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
995         fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
996     }
997     else
998     {
999         fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
1000         fprintf( outfile, "\tsubw %%bp, %%sp\n" );
1001         fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1002         fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
1003     }
1004     fprintf( outfile, "\tlret\n" );
1005
1006     /* Declare the return address variable */
1007
1008     fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
1009     fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
1010 }
1011
1012
1013 /*******************************************************************
1014  *         BuildCallFrom32Regs
1015  *
1016  * Build a 32-bit-to-Wine call-back function for a 'register' function.
1017  * 'args' is the number of dword arguments.
1018  *
1019  * Stack layout:
1020  *   ...
1021  * (ebp+12)  first arg
1022  * (ebp+8)   ret addr to user code
1023  * (ebp+4)   ret addr to relay code
1024  * (ebp+0)   saved ebp
1025  * (ebp-128) buffer area to allow stack frame manipulation
1026  * (ebp-332) CONTEXT86 struct
1027  * (ebp-336) CONTEXT86 *argument
1028  *  ....     other arguments copied from (ebp+12)
1029  *
1030  * The entry point routine is called with a CONTEXT* extra argument,
1031  * following the normal args. In this context structure, EIP_reg
1032  * contains the return address to user code, and ESP_reg the stack
1033  * pointer on return (with the return address and arguments already
1034  * removed).
1035  */
1036 static void BuildCallFrom32Regs( FILE *outfile )
1037 {
1038     static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
1039
1040     /* Function header */
1041
1042     function_header( outfile, "__wine_call_from_32_regs" );
1043
1044     /* Allocate some buffer space on the stack */
1045
1046     fprintf( outfile, "\tpushl %%ebp\n" );
1047     fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
1048     fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
1049
1050     /* Build the context structure */
1051
1052     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1053     fprintf( outfile, "\tpushfl\n" );
1054     fprintf( outfile, "\tpopl %%eax\n" );
1055     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1056     fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
1057     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1058     fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1059     fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1060     fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1061     fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1062     fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1063
1064     fprintf( outfile, "\txorl %%eax,%%eax\n" );
1065     fprintf( outfile, "\tmovw %%cs,%%ax\n" );
1066     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
1067     fprintf( outfile, "\tmovw %%es,%%ax\n" );
1068     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1069     fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1070     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1071     fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1072     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1073     fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1074     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
1075     fprintf( outfile, "\tmovw %%ds,%%ax\n" );
1076     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
1077     fprintf( outfile, "\tmovw %%ax,%%es\n" );  /* set %es equal to %ds just in case */
1078
1079     fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
1080     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
1081
1082     fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
1083     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1084
1085     /* Transfer the arguments */
1086
1087     fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" );   /* get relay code addr */
1088     fprintf( outfile, "\tpushl %%esp\n" );           /* push ptr to context struct */
1089     fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
1090     fprintf( outfile, "\tjecxz 1f\n" );
1091     fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
1092     fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" );  /* get %esp at time of call */
1093     fprintf( outfile, "\tmovl %%esp,%%edi\n" );
1094     fprintf( outfile, "\tshrl $2,%%ecx\n" );
1095     fprintf( outfile, "\tcld\n" );
1096     fprintf( outfile, "\trep\n\tmovsl\n" );  /* copy args */
1097
1098     fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
1099     fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
1100     fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1101
1102     /* Call the entry point */
1103
1104     fprintf( outfile, "\tcall *0(%%ebx)\n" );
1105
1106     /* Store %eip and %ebp onto the new stack */
1107
1108     fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1109     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
1110     fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
1111     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
1112     fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
1113
1114     /* Restore the context structure */
1115
1116     /* Note: we don't bother to restore %cs, %ds and %ss
1117      *       changing them in 32-bit code is a recipe for disaster anyway
1118      */
1119     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
1120     fprintf( outfile, "\tmovw %%ax,%%es\n" );
1121     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
1122     fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1123     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
1124     fprintf( outfile, "\tmovw %%ax,%%gs\n" );
1125
1126     fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
1127     fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
1128     fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
1129     fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
1130     fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
1131
1132     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
1133     fprintf( outfile, "\tpushl %%eax\n" );
1134     fprintf( outfile, "\tpopfl\n" );
1135     fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
1136
1137     fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
1138     fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
1139     fprintf( outfile, "\tpopl %%ebp\n" );
1140     fprintf( outfile, "\tret\n" );
1141 }
1142
1143
1144 /*******************************************************************
1145  *         BuildRelays16
1146  *
1147  * Build all the 16-bit relay callbacks
1148  */
1149 void BuildRelays16( FILE *outfile )
1150 {
1151     char buffer[1024];
1152
1153     /* File header */
1154
1155     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1156     fprintf( outfile, "\t.text\n" );
1157
1158 #ifdef USE_STABS
1159     if (output_file_name)
1160     {
1161         getcwd(buffer, sizeof(buffer));
1162         fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1163
1164         /*
1165          * The stabs help the internal debugger as they are an indication that it
1166          * is sensible to step into a thunk/trampoline.
1167          */
1168         fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1169         fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1170         fprintf( outfile, "Code_Start:\n\n" );
1171     }
1172 #endif
1173     fprintf( outfile, PREFIX"Call16_Start:\n" );
1174     fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
1175     fprintf( outfile, "\t.byte 0\n\n" );
1176
1177     /* Standard CallFrom16 routine (WORD return) */
1178     BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
1179
1180     /* Standard CallFrom16 routine (DWORD return) */
1181     BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
1182
1183     /* Register CallFrom16 routine */
1184     BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
1185
1186     /* C16ThkSL CallFrom16 routine */
1187     BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
1188
1189     /* Standard CallTo16 routine (WORD return) */
1190     BuildCallTo16Core( outfile, TRUE, FALSE );
1191
1192     /* Standard CallTo16 routine (DWORD return) */
1193     BuildCallTo16Core( outfile, FALSE, FALSE );
1194
1195     /* Register CallTo16 routine (16:16 retf) */
1196     BuildCallTo16Core( outfile, FALSE, 1 );
1197
1198     /* Register CallTo16 routine (16:32 retf) */
1199     BuildCallTo16Core( outfile, FALSE, 2 );
1200
1201     /* CBClientThunkSL routine */
1202     BuildCallTo32CBClient( outfile, FALSE );
1203
1204     /* CBClientThunkSLEx routine */
1205     BuildCallTo32CBClient( outfile, TRUE  );
1206
1207     fprintf( outfile, PREFIX"Call16_End:\n" );
1208     fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
1209
1210 #ifdef USE_STABS
1211     fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1212     fprintf( outfile, ".Letext:\n");
1213
1214     /* Some versions of gdb need to have the filename data for
1215        each section, so output it again for the data section. */
1216     if (output_file_name)
1217     {
1218         fprintf( outfile, ".stabs \"%s/\",100,0,0,Data_Start\n", buffer);
1219         fprintf( outfile, ".stabs \"%s\",100,0,0,Data_Start\n", output_file_name );
1220         fprintf( outfile, "Data_Start:\n\n" );
1221     }
1222 #endif
1223
1224     /* The whole Call16_Ret segment must lie within the .data section */
1225     fprintf( outfile, "\n\t.data\n" );
1226     fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
1227     fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
1228
1229     /* Standard CallTo16 return stub */
1230     BuildRet16Func( outfile );
1231
1232     /* CBClientThunkSL return stub */
1233     BuildCallTo32CBClientRet( outfile, FALSE );
1234
1235     /* CBClientThunkSLEx return stub */
1236     BuildCallTo32CBClientRet( outfile, TRUE  );
1237
1238     /* End of Call16_Ret segment */
1239     fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
1240     fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
1241 }
1242
1243 /*******************************************************************
1244  *         BuildRelays32
1245  *
1246  * Build all the 32-bit relay callbacks
1247  */
1248 void BuildRelays32( FILE *outfile )
1249 {
1250     /* File header */
1251
1252     fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
1253     fprintf( outfile, "\t.text\n" );
1254
1255 #ifdef USE_STABS
1256     if (output_file_name)
1257     {
1258         char buffer[1024];
1259         getcwd(buffer, sizeof(buffer));
1260         fprintf( outfile, "\t.file\t\"%s\"\n", output_file_name );
1261
1262         /*
1263          * The stabs help the internal debugger as they are an indication that it
1264          * is sensible to step into a thunk/trampoline.
1265          */
1266         fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
1267         fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", output_file_name );
1268         fprintf( outfile, "Code_Start:\n\n" );
1269     }
1270 #endif
1271     /* 32-bit register entry point */
1272     BuildCallFrom32Regs( outfile );
1273
1274 #ifdef USE_STABS
1275     fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
1276     fprintf( outfile, ".Letext:\n");
1277 #endif
1278 }
1279
1280 #else /* __i386__ */
1281
1282 void BuildRelays16( FILE *outfile )
1283 {
1284     fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1285 }
1286
1287 void BuildRelays32( FILE *outfile )
1288 {
1289     fprintf( outfile, "/* File not used with this architecture. Do not edit! */\n\n" );
1290 }
1291
1292 #endif  /* __i386__ */
1293