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