Added a few more Unicode digits from Unicode version 4.1.
[wine] / tools / winebuild / spec16.c
1 /*
2  * 16-bit spec files
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 <assert.h>
29 #include <ctype.h>
30
31 #include "wine/winbase16.h"
32
33 #include "build.h"
34
35 /* sequences of nops to fill a certain number of words */
36 static const char * const nop_sequence[4] =
37 {
38     ".byte 0x89,0xf6",  /* mov %esi,%esi */
39     ".byte 0x8d,0x74,0x26,0x00",  /* lea 0x00(%esi),%esi */
40     ".byte 0x8d,0xb6,0x00,0x00,0x00,0x00",  /* lea 0x00000000(%esi),%esi */
41     ".byte 0x8d,0x74,0x26,0x00,0x8d,0x74,0x26,0x00" /* lea 0x00(%esi),%esi; lea 0x00(%esi),%esi */
42 };
43
44 static inline int is_function( const ORDDEF *odp )
45 {
46     return (odp->type == TYPE_CDECL ||
47             odp->type == TYPE_PASCAL ||
48             odp->type == TYPE_VARARGS ||
49             odp->type == TYPE_STUB);
50 }
51
52 /*******************************************************************
53  *         output_entries
54  *
55  * Output entries for individual symbols in the entry table.
56  */
57 static void output_entries( FILE *outfile, DLLSPEC *spec, int first, int count )
58 {
59     int i;
60
61     for (i = 0; i < count; i++)
62     {
63         ORDDEF *odp = spec->ordinals[first + i];
64         fprintf( outfile, "\t.byte 0x03\n" );  /* flags: exported & public data */
65         switch (odp->type)
66         {
67         case TYPE_CDECL:
68         case TYPE_PASCAL:
69         case TYPE_VARARGS:
70         case TYPE_STUB:
71             fprintf( outfile, "\t%s .L__wine_%s_%u-.L__wine_spec_code_segment\n",
72                      get_asm_short_keyword(),
73                      make_c_identifier(spec->dll_name), first + i );
74             break;
75         case TYPE_VARIABLE:
76             fprintf( outfile, "\t%s .L__wine_%s_%u-.L__wine_spec_data_segment\n",
77                      get_asm_short_keyword(),
78                      make_c_identifier(spec->dll_name), first + i );
79             break;
80         case TYPE_ABS:
81             fprintf( outfile, "\t%s 0x%04x  /* %s */\n",
82                      get_asm_short_keyword(), odp->u.abs.value, odp->name );
83             break;
84         default:
85             assert(0);
86         }
87     }
88 }
89
90
91 /*******************************************************************
92  *         output_entry_table
93  */
94 static void output_entry_table( FILE *outfile, DLLSPEC *spec )
95 {
96     int i, prev = 0, prev_sel = -1, bundle_count = 0;
97
98     for (i = 1; i <= spec->limit; i++)
99     {
100         int selector = 0;
101         ORDDEF *odp = spec->ordinals[i];
102         if (!odp) continue;
103
104         switch (odp->type)
105         {
106         case TYPE_CDECL:
107         case TYPE_PASCAL:
108         case TYPE_VARARGS:
109         case TYPE_STUB:
110             selector = 1;  /* Code selector */
111             break;
112         case TYPE_VARIABLE:
113             selector = 2;  /* Data selector */
114             break;
115         case TYPE_ABS:
116             selector = 0xfe;  /* Constant selector */
117             break;
118         default:
119             continue;
120         }
121
122         if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
123         {
124             /* need to start a new bundle */
125
126             /* flush previous bundle */
127             if (bundle_count)
128             {
129                 fprintf( outfile, "\t/* %s.%d - %s.%d */\n",
130                          spec->dll_name, prev - bundle_count + 1, spec->dll_name, prev );
131                 fprintf( outfile, "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
132                 output_entries( outfile, spec, prev - bundle_count + 1, bundle_count );
133             }
134
135             if (prev + 1 != i)
136             {
137                 int skip = i - (prev + 1);
138                 while (skip > 255)
139                 {
140                     fprintf( outfile, "\t.byte 0xff,0x00\n" );
141                     skip -= 255;
142                 }
143                 fprintf( outfile, "\t.byte 0x%02x,0x00\n", skip );
144             }
145
146             bundle_count = 0;
147             prev_sel = selector;
148         }
149         bundle_count++;
150         prev = i;
151     }
152
153     /* flush last bundle */
154     if (bundle_count)
155     {
156         fprintf( outfile, "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
157         output_entries( outfile, spec, prev - bundle_count + 1, bundle_count );
158     }
159     fprintf( outfile, "\t.byte 0x00\n" );
160 }
161
162
163 /*******************************************************************
164  *         output_resident_name
165  */
166 static void output_resident_name( FILE *outfile, const char *string, int ordinal )
167 {
168     unsigned int i, len = strlen(string);
169
170     fprintf( outfile, "\t.byte 0x%02x", len );
171     for (i = 0; i < len; i++) fprintf( outfile, ",0x%02x", (unsigned char)toupper(string[i]) );
172     fprintf( outfile, " /* %s */\n", string );
173     fprintf( outfile, "\t%s %u\n", get_asm_short_keyword(), ordinal );
174 }
175
176
177 /*******************************************************************
178  *         get_callfrom16_name
179  */
180 static const char *get_callfrom16_name( const ORDDEF *odp )
181 {
182     static char buffer[80];
183
184     sprintf( buffer, "%s_%s_%s",
185              (odp->type == TYPE_PASCAL) ? "p" :
186              (odp->type == TYPE_VARARGS) ? "v" : "c",
187              (odp->flags & FLAG_REGISTER) ? "regs" :
188              (odp->flags & FLAG_RET16) ? "word" : "long",
189              odp->u.func.arg_types );
190     return buffer;
191 }
192
193
194 /*******************************************************************
195  *         get_relay_name
196  */
197 static const char *get_relay_name( const ORDDEF *odp )
198 {
199     static char buffer[80];
200     char *p;
201
202     switch(odp->type)
203     {
204     case TYPE_PASCAL:
205         strcpy( buffer, "p_" );
206         break;
207     case TYPE_VARARGS:
208         strcpy( buffer, "v_" );
209         break;
210     case TYPE_CDECL:
211     case TYPE_STUB:
212         strcpy( buffer, "c_" );
213         break;
214     default:
215         assert(0);
216     }
217     strcat( buffer, odp->u.func.arg_types );
218     for (p = buffer + 2; *p; p++)
219     {
220         /* map string types to the corresponding plain pointer type */
221         if (*p == 't') *p = 'p';
222         else if (*p == 'T') *p = 'l';
223     }
224     if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
225     return buffer;
226 }
227
228
229 /*******************************************************************
230  *         get_function_argsize
231  */
232 static int get_function_argsize( const ORDDEF *odp )
233 {
234     const char *args;
235     int argsize = 0;
236
237     for (args = odp->u.func.arg_types; *args; args++)
238     {
239         switch (*args)
240         {
241         case 'w':  /* word */
242         case 's':  /* s_word */
243             argsize += 2;
244             break;
245         case 'l':  /* long or segmented pointer */
246         case 'T':  /* segmented pointer to null-terminated string */
247         case 'p':  /* linear pointer */
248         case 't':  /* linear pointer to null-terminated string */
249             argsize += 4;
250             break;
251         default:
252             assert(0);
253         }
254     }
255     return argsize;
256 }
257
258
259 /*******************************************************************
260  *         output_call16_function
261  *
262  * Build a 16-bit-to-Wine callback glue function.
263  *
264  * The generated routines are intended to be used as argument conversion
265  * routines to be called by the CallFrom16... core. Thus, the prototypes of
266  * the generated routines are (see also CallFrom16):
267  *
268  *  extern WORD WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
269  *  extern LONG WINAPI __wine_spec_call16_C_xxx( FARPROC func, LPBYTE args );
270  *  extern void WINAPI __wine_spec_call16_C_xxx_regs( FARPROC func, LPBYTE args, CONTEXT86 *context );
271  *
272  * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
273  * and each 'x' is an argument  ('w'=word, 's'=signed word, 'l'=long,
274  * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
275  * 'T'=segmented pointer to null-terminated string).
276  *
277  * The generated routines fetch the arguments from the 16-bit stack (pointed
278  * to by 'args'); the offsets of the single argument values are computed
279  * according to the calling convention and the argument types.  Then, the
280  * 32-bit entry point is called with these arguments.
281  *
282  * For register functions, the arguments (if present) are converted just
283  * the same as for normal functions, but in addition the CONTEXT86 pointer
284  * filled with the current register values is passed to the 32-bit routine.
285  */
286 static void output_call16_function( FILE *outfile, ORDDEF *odp )
287 {
288     char name[256];
289     int i, pos;
290     const char *args = odp->u.func.arg_types;
291     int argsize = get_function_argsize( odp );
292     int needs_ldt = strchr( args, 'p' ) || strchr( args, 't' );
293
294     sprintf( name, ".L__wine_spec_call16_%s", get_relay_name(odp) );
295
296     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
297     fprintf( outfile, "\t%s\n", func_declaration(name) );
298     fprintf( outfile, "%s:\n", name );
299     fprintf( outfile, "\tpushl %%ebp\n" );
300     fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
301     if (needs_ldt)
302     {
303         fprintf( outfile, "\tpushl %%esi\n" );
304         if (UsePIC)
305         {
306             fprintf( outfile, "\tcall 1f\n" );
307             fprintf( outfile, "1:\tpopl %%eax\n" );
308             fprintf( outfile, "\tmovl wine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
309         }
310         else
311             fprintf( outfile, "\tmovl $%s,%%esi\n", asm_name("wine_ldt_copy") );
312     }
313
314     if (args[0] || odp->type == TYPE_VARARGS)
315         fprintf( outfile, "\tmovl 12(%%ebp),%%ecx\n" );  /* args */
316
317     if (odp->flags & FLAG_REGISTER)
318     {
319         fprintf( outfile, "\tpushl 16(%%ebp)\n" );  /* context */
320     }
321     else if (odp->type == TYPE_VARARGS)
322     {
323         fprintf( outfile, "\tleal %d(%%ecx),%%eax\n", argsize );
324         fprintf( outfile, "\tpushl %%eax\n" );  /* va_list16 */
325     }
326
327     pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
328     for (i = strlen(args) - 1; i >= 0; i--)
329     {
330         switch (args[i])
331         {
332         case 'w':  /* word */
333             if (odp->type != TYPE_PASCAL) pos -= 2;
334             fprintf( outfile, "\tmovzwl %d(%%ecx),%%eax\n", pos );
335             fprintf( outfile, "\tpushl %%eax\n" );
336             if (odp->type == TYPE_PASCAL) pos += 2;
337             break;
338
339         case 's':  /* s_word */
340             if (odp->type != TYPE_PASCAL) pos -= 2;
341             fprintf( outfile, "\tmovswl %d(%%ecx),%%eax\n", pos );
342             fprintf( outfile, "\tpushl %%eax\n" );
343             if (odp->type == TYPE_PASCAL) pos += 2;
344             break;
345
346         case 'l':  /* long or segmented pointer */
347         case 'T':  /* segmented pointer to null-terminated string */
348             if (odp->type != TYPE_PASCAL) pos -= 4;
349             fprintf( outfile, "\tpushl %d(%%ecx)\n", pos );
350             if (odp->type == TYPE_PASCAL) pos += 4;
351             break;
352
353         case 'p':  /* linear pointer */
354         case 't':  /* linear pointer to null-terminated string */
355             if (odp->type != TYPE_PASCAL) pos -= 4;
356             fprintf( outfile, "\tmovzwl %d(%%ecx),%%edx\n", pos + 2 ); /* sel */
357             fprintf( outfile, "\tshr $3,%%edx\n" );
358             fprintf( outfile, "\tmovzwl %d(%%ecx),%%eax\n", pos ); /* offset */
359             fprintf( outfile, "\taddl (%%esi,%%edx,4),%%eax\n" );
360             fprintf( outfile, "\tpushl %%eax\n" );
361             if (odp->type == TYPE_PASCAL) pos += 4;
362             break;
363
364         default:
365             assert(0);
366         }
367     }
368
369     fprintf( outfile, "\tcall *8(%%ebp)\n" );
370
371     if (needs_ldt) fprintf( outfile, "\tmovl -4(%%ebp),%%esi\n" );
372     if (odp->flags & FLAG_RET16) fprintf( outfile, "\tmovzwl %%ax,%%eax\n" );
373
374     fprintf( outfile, "\tleave\n" );
375     fprintf( outfile, "\tret\n" );
376     output_function_size( outfile, name );
377 }
378
379
380 /*******************************************************************
381  *         callfrom16_type_compare
382  *
383  * Compare two callfrom16 sequences.
384  */
385 static int callfrom16_type_compare( const void *e1, const void *e2 )
386 {
387     const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
388     const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
389     int retval;
390     int type1 = odp1->type;
391     int type2 = odp2->type;
392
393     if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
394     if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
395
396     if ((retval = type1 - type2) != 0) return retval;
397
398     type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
399     type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
400
401     if ((retval = type1 - type2) != 0) return retval;
402
403     return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
404 }
405
406
407 /*******************************************************************
408  *         relay_type_compare
409  *
410  * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
411  */
412 static int relay_type_compare( const void *e1, const void *e2 )
413 {
414     const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
415     const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
416     char name1[80];
417
418     strcpy( name1, get_relay_name(odp1) );
419     return strcmp( name1, get_relay_name(odp2) );
420 }
421
422
423 /*******************************************************************
424  *         sort_func_list
425  *
426  * Sort a list of functions, removing duplicates.
427  */
428 static int sort_func_list( ORDDEF **list, int count,
429                            int (*compare)(const void *, const void *) )
430 {
431     int i, j;
432
433     qsort( list, count, sizeof(*list), compare );
434
435     for (i = j = 0; i < count; i++)
436     {
437         if (compare( &list[j], &list[i] )) list[++j] = list[i];
438     }
439     return j + 1;
440 }
441
442
443 /*******************************************************************
444  *         output_init_code
445  *
446  * Output the dll initialization code.
447  */
448 static void output_init_code( FILE *outfile, const DLLSPEC *spec, const char *header_name )
449 {
450     char name[80];
451
452     sprintf( name, ".L__wine_spec_%s_init", make_c_identifier(spec->dll_name) );
453
454     fprintf( outfile, "\n/* dll initialization code */\n\n" );
455     fprintf( outfile, "\t.text\n" );
456     fprintf( outfile, "\t.align 4\n" );
457     fprintf( outfile, "\t%s\n", func_declaration(name) );
458     fprintf( outfile, "%s:\n", name );
459     if (UsePIC)
460     {
461         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
462         fprintf( outfile, "1:\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
463         fprintf( outfile, "\tpushl %%ecx\n" );
464         fprintf( outfile, "\tleal %s-1b(%%eax),%%ecx\n", header_name );
465         fprintf( outfile, "\tpushl %%ecx\n" );
466     }
467     else
468     {
469         fprintf( outfile, "\tpushl $.L__wine_spec_file_name\n" );
470         fprintf( outfile, "\tpushl $%s\n", header_name );
471     }
472     fprintf( outfile, "\tcall %s\n", asm_name("__wine_dll_register_16") );
473     fprintf( outfile, "\taddl $8,%%esp\n" );
474     fprintf( outfile, "\tret\n" );
475     output_function_size( outfile, name );
476
477     sprintf( name, ".L__wine_spec_%s_fini", make_c_identifier(spec->dll_name) );
478
479     fprintf( outfile, "\t.align 4\n" );
480     fprintf( outfile, "\t%s\n", func_declaration(name) );
481     fprintf( outfile, "%s:\n", name );
482     if (UsePIC)
483     {
484         fprintf( outfile, "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
485         fprintf( outfile, "1:\tleal %s-1b(%%eax),%%ecx\n", header_name );
486         fprintf( outfile, "\tpushl %%ecx\n" );
487     }
488     else
489     {
490         fprintf( outfile, "\tpushl $%s\n", header_name );
491     }
492     fprintf( outfile, "\tcall %s\n", asm_name("__wine_dll_unregister_16") );
493     fprintf( outfile, "\taddl $4,%%esp\n" );
494     fprintf( outfile, "\tret\n" );
495     output_function_size( outfile, name );
496
497     if (target_platform == PLATFORM_APPLE)
498     {
499         fprintf( outfile, "\t.mod_init_func\n" );
500         fprintf( outfile, "\t.align %d\n", get_alignment(4) );
501         fprintf( outfile, "\t.long .L__wine_spec_%s_init\n", make_c_identifier(spec->dll_name) );
502         fprintf( outfile, "\t.mod_term_func\n" );
503         fprintf( outfile, "\t.align %d\n", get_alignment(4) );
504         fprintf( outfile, "\t.long .L__wine_spec_%s_fini\n", make_c_identifier(spec->dll_name) );
505     }
506     else
507     {
508         fprintf( outfile, "\t.section \".init\",\"ax\"\n" );
509         fprintf( outfile, "\tcall .L__wine_spec_%s_init\n", make_c_identifier(spec->dll_name) );
510         fprintf( outfile, "\t.section \".fini\",\"ax\"\n" );
511         fprintf( outfile, "\tcall .L__wine_spec_%s_fini\n", make_c_identifier(spec->dll_name) );
512     }
513 }
514
515
516 /*******************************************************************
517  *         BuildSpec16File
518  *
519  * Build a Win16 assembly file from a spec file.
520  */
521 void BuildSpec16File( FILE *outfile, DLLSPEC *spec )
522 {
523     ORDDEF **typelist;
524     int i, j, nb_funcs;
525     char header_name[256];
526
527     /* File header */
528
529     output_standard_file_header( outfile );
530
531     if (!spec->dll_name)  /* set default name from file name */
532     {
533         char *p;
534         spec->dll_name = xstrdup( spec->file_name );
535         if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
536     }
537
538     /* Build sorted list of all argument types, without duplicates */
539
540     typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) );
541
542     for (i = nb_funcs = 0; i <= spec->limit; i++)
543     {
544         ORDDEF *odp = spec->ordinals[i];
545         if (!odp) continue;
546         if (is_function( odp )) typelist[nb_funcs++] = odp;
547     }
548
549     nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
550
551     /* Output the module structure */
552
553     sprintf( header_name, "__wine_spec_%s_dos_header", make_c_identifier(spec->dll_name) );
554     fprintf( outfile, "\n/* module data */\n\n" );
555     fprintf( outfile, "\t.data\n" );
556     fprintf( outfile, "\t.align %d\n", get_alignment(4) );
557     fprintf( outfile, "%s:\n", header_name );
558     fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(),                      /* e_magic */
559              IMAGE_DOS_SIGNATURE );
560     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_cblp */
561     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_cp */
562     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_crlc */
563     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_cparhdr */
564     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_minalloc */
565     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_maxalloc */
566     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_ss */
567     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_sp */
568     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_csum */
569     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_ip */
570     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_cs */
571     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_lfarlc */
572     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_ovno */
573     fprintf( outfile, "\t%s 0,0,0,0\n", get_asm_short_keyword() );                   /* e_res */
574     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_oemid */
575     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* e_oeminfo */
576     fprintf( outfile, "\t%s 0,0,0,0,0,0,0,0,0,0\n", get_asm_short_keyword() );       /* e_res2 */
577     fprintf( outfile, "\t.long .L__wine_spec_ne_header-%s\n", header_name );         /* e_lfanew */
578
579     fprintf( outfile, ".L__wine_spec_ne_header:\n" );
580     fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(),                      /* ne_magic */
581              IMAGE_OS2_SIGNATURE );
582     fprintf( outfile, "\t.byte 0\n" );                                               /* ne_ver */
583     fprintf( outfile, "\t.byte 0\n" );                                               /* ne_rev */
584     fprintf( outfile, "\t%s .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n",      /* ne_enttab */
585              get_asm_short_keyword() );
586     fprintf( outfile, "\t%s .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n",  /* ne_cbenttab */
587              get_asm_short_keyword() );
588     fprintf( outfile, "\t.long 0\n" );                                               /* ne_crc */
589     fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(),                      /* ne_flags */
590              NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE );
591     fprintf( outfile, "\t%s 2\n", get_asm_short_keyword() );                         /* ne_autodata */
592     fprintf( outfile, "\t%s %u\n", get_asm_short_keyword(), spec->heap_size );       /* ne_heap */
593     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* ne_stack */
594     fprintf( outfile, "\t.long 0\n" );                                               /* ne_csip */
595     fprintf( outfile, "\t.long 0\n" );                                               /* ne_sssp */
596     fprintf( outfile, "\t%s 2\n", get_asm_short_keyword() );                         /* ne_cseg */
597     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* ne_cmod */
598     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );                         /* ne_cbnrestab */
599     fprintf( outfile, "\t%s .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n",      /* ne_segtab */
600              get_asm_short_keyword() );
601     fprintf( outfile, "\t%s .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n",     /* ne_rsrctab */
602              get_asm_short_keyword() );
603     fprintf( outfile, "\t%s .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n",      /* ne_restab */
604              get_asm_short_keyword() );
605     fprintf( outfile, "\t%s .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n",      /* ne_modtab */
606              get_asm_short_keyword() );
607     fprintf( outfile, "\t%s .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n",      /* ne_imptab */
608              get_asm_short_keyword() );
609     fprintf( outfile, "\t.long 0\n" );                                   /* ne_nrestab */
610     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_cmovent */
611     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_align */
612     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_cres */
613     fprintf( outfile, "\t.byte 0x%02x\n", NE_OSFLAGS_WINDOWS );          /* ne_exetyp */
614     fprintf( outfile, "\t.byte 0x%02x\n", NE_AFLAGS_FASTLOAD );          /* ne_flagsothers */
615     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_pretthunks */
616     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_psegrefbytes */
617     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_swaparea */
618     fprintf( outfile, "\t%s 0\n", get_asm_short_keyword() );             /* ne_expver */
619
620     /* segment table */
621
622     fprintf( outfile, "\n.L__wine_spec_ne_segtab:\n" );
623
624     /* code segment entry */
625
626     fprintf( outfile, "\t%s .L__wine_spec_code_segment-%s\n",  /* filepos */
627              get_asm_short_keyword(), header_name );
628     fprintf( outfile, "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* size */
629              get_asm_short_keyword() );
630     fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), NE_SEGFLAGS_32BIT );      /* flags */
631     fprintf( outfile, "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* minsize */
632              get_asm_short_keyword() );
633
634     /* data segment entry */
635
636     fprintf( outfile, "\t%s .L__wine_spec_data_segment-%s\n",  /* filepos */
637              get_asm_short_keyword(), header_name );
638     fprintf( outfile, "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* size */
639              get_asm_short_keyword() );
640     fprintf( outfile, "\t%s 0x%04x\n", get_asm_short_keyword(), NE_SEGFLAGS_DATA );      /* flags */
641     fprintf( outfile, "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* minsize */
642              get_asm_short_keyword() );
643
644     /* resource directory */
645
646     output_res16_directory( outfile, spec, header_name );
647
648     /* resident names table */
649
650     fprintf( outfile, "\n\t.align %d\n", get_alignment(2) );
651     fprintf( outfile, ".L__wine_spec_ne_restab:\n" );
652     output_resident_name( outfile, spec->dll_name, 0 );
653     for (i = 1; i <= spec->limit; i++)
654     {
655         ORDDEF *odp = spec->ordinals[i];
656         if (!odp || !odp->name[0]) continue;
657         output_resident_name( outfile, odp->name, i );
658     }
659     fprintf( outfile, "\t.byte 0\n" );
660
661     /* imported names table */
662
663     fprintf( outfile, "\n\t.align %d\n", get_alignment(2) );
664     fprintf( outfile, ".L__wine_spec_ne_modtab:\n" );
665     fprintf( outfile, ".L__wine_spec_ne_imptab:\n" );
666     fprintf( outfile, "\t.byte 0,0\n" );
667
668     /* entry table */
669
670     fprintf( outfile, "\n.L__wine_spec_ne_enttab:\n" );
671     output_entry_table( outfile, spec );
672     fprintf( outfile, ".L__wine_spec_ne_enttab_end:\n" );
673
674     /* code segment */
675
676     fprintf( outfile, "\n\t.align %d\n", get_alignment(2) );
677     fprintf( outfile, ".L__wine_spec_code_segment:\n" );
678
679     for ( i = 0; i < nb_funcs; i++ )
680     {
681         unsigned int arg_types[2];
682         int j, nop_words, argsize = 0;
683
684         if ( typelist[i]->type == TYPE_PASCAL )
685             argsize = get_function_argsize( typelist[i] );
686
687         /* build the arg types bit fields */
688         arg_types[0] = arg_types[1] = 0;
689         for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
690         {
691             int type = 0;
692             switch(typelist[i]->u.func.arg_types[j])
693             {
694             case 'w': type = ARG_WORD; break;
695             case 's': type = ARG_SWORD; break;
696             case 'l': type = ARG_LONG; break;
697             case 'p': type = ARG_PTR; break;
698             case 't': type = ARG_STR; break;
699             case 'T': type = ARG_SEGSTR; break;
700             }
701             arg_types[j / 10] |= type << (3 * (j % 10));
702         }
703         if (typelist[i]->type == TYPE_VARARGS) arg_types[j / 10] |= ARG_VARARG << (3 * (j % 10));
704
705         fprintf( outfile, ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
706         fprintf( outfile, "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
707         fprintf( outfile, "\tlcall $0,$0\n" );
708
709         if (typelist[i]->flags & FLAG_REGISTER)
710         {
711             nop_words = 4;
712         }
713         else if (typelist[i]->flags & FLAG_RET16)
714         {
715             fprintf( outfile, "\torw %%ax,%%ax\n" );
716             fprintf( outfile, "\tnop\n" );  /* so that the lretw is aligned */
717             nop_words = 2;
718         }
719         else
720         {
721             fprintf( outfile, "shld $16,%%eax,%%edx\n" );
722             fprintf( outfile, "orl %%eax,%%eax\n" );
723             nop_words = 1;
724         }
725
726         if (argsize)
727         {
728             fprintf( outfile, "lretw $%u\n", argsize );
729             nop_words--;
730         }
731         else fprintf( outfile, "lretw\n" );
732
733         if (nop_words) fprintf( outfile, "\t%s\n", nop_sequence[nop_words-1] );
734
735         /* the movl is here so that the code contains only valid instructions, */
736         /* it's never actually executed, we only care about the arg_types[] values */
737         fprintf( outfile, "\t%s 0x86c7\n", get_asm_short_keyword() );
738         fprintf( outfile, "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
739     }
740
741     for (i = 0; i <= spec->limit; i++)
742     {
743         ORDDEF *odp = spec->ordinals[i];
744         if (!odp || !is_function( odp )) continue;
745         fprintf( outfile, ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i );
746         fprintf( outfile, "\tpushw %%bp\n" );
747         fprintf( outfile, "\tpushl $%s\n",
748                  asm_name( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : odp->link_name ));
749         fprintf( outfile, "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
750     }
751     fprintf( outfile, ".L__wine_spec_code_segment_end:\n" );
752
753     /* data segment */
754
755     fprintf( outfile, "\n.L__wine_spec_data_segment:\n" );
756     fprintf( outfile, "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" );  /* instance data */
757     for (i = 0; i <= spec->limit; i++)
758     {
759         ORDDEF *odp = spec->ordinals[i];
760         if (!odp || odp->type != TYPE_VARIABLE) continue;
761         fprintf( outfile, ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i );
762         fprintf( outfile, "\t.long " );
763         for (j = 0; j < odp->u.var.n_values-1; j++)
764             fprintf( outfile, "0x%08x,", odp->u.var.values[j] );
765         fprintf( outfile, "0x%08x\n", odp->u.var.values[j] );
766     }
767     fprintf( outfile, ".L__wine_spec_data_segment_end:\n" );
768
769     /* resource data */
770
771     if (spec->nb_resources)
772     {
773         fprintf( outfile, "\n.L__wine_spec_resource_data:\n" );
774         output_res16_data( outfile, spec );
775     }
776
777     fprintf( outfile, "\t.byte 0\n" );  /* make sure the last symbol points to something */
778
779     /* relay functions */
780
781     nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
782     if (nb_funcs)
783     {
784         fprintf( outfile, "\n/* relay functions */\n\n" );
785         fprintf( outfile, "\t.text\n" );
786         for ( i = 0; i < nb_funcs; i++ ) output_call16_function( outfile, typelist[i] );
787         fprintf( outfile, "\t.data\n" );
788         fprintf( outfile, "wine_ldt_copy_ptr:\n" );
789         fprintf( outfile, "\t.long %s\n", asm_name("wine_ldt_copy") );
790     }
791
792     fprintf( outfile, "\n\t%s\n", get_asm_string_section() );
793     fprintf( outfile, ".L__wine_spec_file_name:\n" );
794     fprintf( outfile, "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
795
796     output_stubs( outfile, spec );
797     output_get_pc_thunk( outfile );
798     output_init_code( outfile, spec, header_name );
799
800     free( typelist );
801 }