push eb25bf65c4616aa55a810ed5c29198b1a080b208
[wine/hacks.git] / tools / winebuild / spec16.c
blob7c6fc45960934f91af93253051890b02e3cc528e
1 /*
2 * 16-bit spec files
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
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
31 #include "wine/winbase16.h"
33 #include "build.h"
35 /* sequences of nops to fill a certain number of words */
36 static const char * const nop_sequence[4] =
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 */
44 static inline int is_function( const ORDDEF *odp )
46 return (odp->type == TYPE_CDECL ||
47 odp->type == TYPE_PASCAL ||
48 odp->type == TYPE_VARARGS ||
49 odp->type == TYPE_STUB);
52 /*******************************************************************
53 * output_entries
55 * Output entries for individual symbols in the entry table.
57 static void output_entries( DLLSPEC *spec, int first, int count )
59 int i;
61 for (i = 0; i < count; i++)
63 ORDDEF *odp = spec->ordinals[first + i];
64 output( "\t.byte 0x03\n" ); /* flags: exported & public data */
65 switch (odp->type)
67 case TYPE_CDECL:
68 case TYPE_PASCAL:
69 case TYPE_VARARGS:
70 case TYPE_STUB:
71 output( "\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 output( "\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 output( "\t%s 0x%04x /* %s */\n",
82 get_asm_short_keyword(), odp->u.abs.value, odp->name );
83 break;
84 default:
85 assert(0);
91 /*******************************************************************
92 * output_entry_table
94 static void output_entry_table( DLLSPEC *spec )
96 int i, prev = 0, prev_sel = -1, bundle_count = 0;
98 for (i = 1; i <= spec->limit; i++)
100 int selector = 0;
101 ORDDEF *odp = spec->ordinals[i];
102 if (!odp) continue;
104 switch (odp->type)
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;
122 if (prev + 1 != i || prev_sel != selector || bundle_count == 255)
124 /* need to start a new bundle */
126 /* flush previous bundle */
127 if (bundle_count)
129 output( "\t/* %s.%d - %s.%d */\n",
130 spec->dll_name, prev - bundle_count + 1, spec->dll_name, prev );
131 output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
132 output_entries( spec, prev - bundle_count + 1, bundle_count );
135 if (prev + 1 != i)
137 int skip = i - (prev + 1);
138 while (skip > 255)
140 output( "\t.byte 0xff,0x00\n" );
141 skip -= 255;
143 output( "\t.byte 0x%02x,0x00\n", skip );
146 bundle_count = 0;
147 prev_sel = selector;
149 bundle_count++;
150 prev = i;
153 /* flush last bundle */
154 if (bundle_count)
156 output( "\t.byte 0x%02x,0x%02x\n", bundle_count, prev_sel );
157 output_entries( spec, prev - bundle_count + 1, bundle_count );
159 output( "\t.byte 0x00\n" );
163 /*******************************************************************
164 * output_resident_name
166 static void output_resident_name( const char *string, int ordinal )
168 unsigned int i, len = strlen(string);
170 output( "\t.byte 0x%02x", len );
171 for (i = 0; i < len; i++) output( ",0x%02x", (unsigned char)toupper(string[i]) );
172 output( " /* %s */\n", string );
173 output( "\t%s %u\n", get_asm_short_keyword(), ordinal );
177 /*******************************************************************
178 * get_callfrom16_name
180 static const char *get_callfrom16_name( const ORDDEF *odp )
182 static char buffer[80];
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;
194 /*******************************************************************
195 * get_relay_name
197 static const char *get_relay_name( const ORDDEF *odp )
199 static char buffer[80];
200 char *p;
202 switch(odp->type)
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);
217 strcat( buffer, odp->u.func.arg_types );
218 for (p = buffer + 2; *p; p++)
220 /* map string types to the corresponding plain pointer type */
221 if (*p == 't') *p = 'p';
222 else if (*p == 'T') *p = 'l';
224 if (odp->flags & FLAG_REGISTER) strcat( buffer, "_regs" );
225 return buffer;
229 /*******************************************************************
230 * get_function_argsize
232 static int get_function_argsize( const ORDDEF *odp )
234 const char *args;
235 int argsize = 0;
237 for (args = odp->u.func.arg_types; *args; args++)
239 switch (*args)
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);
255 return argsize;
259 /*******************************************************************
260 * output_call16_function
262 * Build a 16-bit-to-Wine callback glue function.
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):
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 );
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).
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.
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.
286 static void output_call16_function( ORDDEF *odp )
288 char name[256];
289 int i, pos, stack_words;
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' );
294 sprintf( name, ".L__wine_spec_call16_%s", get_relay_name(odp) );
296 output( "\t.align %d\n", get_alignment(4) );
297 output( "\t%s\n", func_declaration(name) );
298 output( "%s:\n", name );
299 output( "\tpushl %%ebp\n" );
300 output( "\tmovl %%esp,%%ebp\n" );
301 stack_words = 2;
302 if (needs_ldt)
304 output( "\tpushl %%esi\n" );
305 stack_words++;
306 if (UsePIC)
308 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
309 output( "1:\tmovl wine_ldt_copy_ptr-1b(%%eax),%%esi\n" );
311 else
312 output( "\tmovl $%s,%%esi\n", asm_name("wine_ldt_copy") );
315 /* preserve 16-byte stack alignment */
316 stack_words += strlen(args);
317 if ((odp->flags & FLAG_REGISTER) || (odp->type == TYPE_VARARGS)) stack_words++;
318 if (stack_words % 4) output( "\tsubl $%d,%%esp\n", 16 - 4 * (stack_words % 4) );
320 if (args[0] || odp->type == TYPE_VARARGS)
321 output( "\tmovl 12(%%ebp),%%ecx\n" ); /* args */
323 if (odp->flags & FLAG_REGISTER)
325 output( "\tpushl 16(%%ebp)\n" ); /* context */
327 else if (odp->type == TYPE_VARARGS)
329 output( "\tleal %d(%%ecx),%%eax\n", argsize );
330 output( "\tpushl %%eax\n" ); /* va_list16 */
333 pos = (odp->type == TYPE_PASCAL) ? 0 : argsize;
334 for (i = strlen(args) - 1; i >= 0; i--)
336 switch (args[i])
338 case 'w': /* word */
339 if (odp->type != TYPE_PASCAL) pos -= 2;
340 output( "\tmovzwl %d(%%ecx),%%eax\n", pos );
341 output( "\tpushl %%eax\n" );
342 if (odp->type == TYPE_PASCAL) pos += 2;
343 break;
345 case 's': /* s_word */
346 if (odp->type != TYPE_PASCAL) pos -= 2;
347 output( "\tmovswl %d(%%ecx),%%eax\n", pos );
348 output( "\tpushl %%eax\n" );
349 if (odp->type == TYPE_PASCAL) pos += 2;
350 break;
352 case 'l': /* long or segmented pointer */
353 case 'T': /* segmented pointer to null-terminated string */
354 if (odp->type != TYPE_PASCAL) pos -= 4;
355 output( "\tpushl %d(%%ecx)\n", pos );
356 if (odp->type == TYPE_PASCAL) pos += 4;
357 break;
359 case 'p': /* linear pointer */
360 case 't': /* linear pointer to null-terminated string */
361 if (odp->type != TYPE_PASCAL) pos -= 4;
362 output( "\tmovzwl %d(%%ecx),%%edx\n", pos + 2 ); /* sel */
363 output( "\tshr $3,%%edx\n" );
364 output( "\tmovzwl %d(%%ecx),%%eax\n", pos ); /* offset */
365 output( "\taddl (%%esi,%%edx,4),%%eax\n" );
366 output( "\tpushl %%eax\n" );
367 if (odp->type == TYPE_PASCAL) pos += 4;
368 break;
370 default:
371 assert(0);
375 output( "\tcall *8(%%ebp)\n" );
377 if (needs_ldt) output( "\tmovl -4(%%ebp),%%esi\n" );
379 output( "\tleave\n" );
380 output( "\tret\n" );
381 output_function_size( name );
385 /*******************************************************************
386 * callfrom16_type_compare
388 * Compare two callfrom16 sequences.
390 static int callfrom16_type_compare( const void *e1, const void *e2 )
392 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
393 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
394 int retval;
395 int type1 = odp1->type;
396 int type2 = odp2->type;
398 if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
399 if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
401 if ((retval = type1 - type2) != 0) return retval;
403 type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
404 type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
406 if ((retval = type1 - type2) != 0) return retval;
408 return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
412 /*******************************************************************
413 * relay_type_compare
415 * Same as callfrom16_type_compare but ignores differences that don't affect the resulting relay function.
417 static int relay_type_compare( const void *e1, const void *e2 )
419 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
420 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
421 char name1[80];
423 strcpy( name1, get_relay_name(odp1) );
424 return strcmp( name1, get_relay_name(odp2) );
428 /*******************************************************************
429 * sort_func_list
431 * Sort a list of functions, removing duplicates.
433 static int sort_func_list( ORDDEF **list, int count,
434 int (*compare)(const void *, const void *) )
436 int i, j;
438 if (!count) return 0;
439 qsort( list, count, sizeof(*list), compare );
441 for (i = j = 0; i < count; i++)
443 if (compare( &list[j], &list[i] )) list[++j] = list[i];
445 return j + 1;
449 /*******************************************************************
450 * output_init_code
452 * Output the dll initialization code.
454 static void output_init_code( const DLLSPEC *spec )
456 char name[80];
458 sprintf( name, ".L__wine_spec_%s_init", make_c_identifier(spec->dll_name) );
460 output( "\n/* dll initialization code */\n\n" );
461 output( "\t.text\n" );
462 output( "\t.align 4\n" );
463 output( "\t%s\n", func_declaration(name) );
464 output( "%s:\n", name );
465 output( "\tsubl $4,%%esp\n" );
466 if (UsePIC)
468 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
469 output( "1:\tleal .L__wine_spec_file_name-1b(%%eax),%%ecx\n" );
470 output( "\tpushl %%ecx\n" );
471 output( "\tleal .L__wine_spec_dos_header-1b(%%eax),%%ecx\n" );
472 output( "\tpushl %%ecx\n" );
474 else
476 output( "\tpushl $.L__wine_spec_file_name\n" );
477 output( "\tpushl $.L__wine_spec_dos_header\n" );
479 output( "\tcall %s\n", asm_name("__wine_dll_register_16") );
480 output( "\taddl $12,%%esp\n" );
481 output( "\tret\n" );
482 output_function_size( name );
484 sprintf( name, ".L__wine_spec_%s_fini", make_c_identifier(spec->dll_name) );
486 output( "\t.align 4\n" );
487 output( "\t%s\n", func_declaration(name) );
488 output( "%s:\n", name );
489 output( "\tsubl $8,%%esp\n" );
490 if (UsePIC)
492 output( "\tcall %s\n", asm_name("__wine_spec_get_pc_thunk_eax") );
493 output( "1:\tleal .L__wine_spec_dos_header-1b(%%eax),%%ecx\n" );
494 output( "\tpushl %%ecx\n" );
496 else
498 output( "\tpushl $.L__wine_spec_dos_header\n" );
500 output( "\tcall %s\n", asm_name("__wine_dll_unregister_16") );
501 output( "\taddl $12,%%esp\n" );
502 output( "\tret\n" );
503 output_function_size( name );
505 if (target_platform == PLATFORM_APPLE)
507 output( "\t.mod_init_func\n" );
508 output( "\t.align %d\n", get_alignment(4) );
509 output( "\t.long .L__wine_spec_%s_init\n", make_c_identifier(spec->dll_name) );
510 output( "\t.mod_term_func\n" );
511 output( "\t.align %d\n", get_alignment(4) );
512 output( "\t.long .L__wine_spec_%s_fini\n", make_c_identifier(spec->dll_name) );
514 else
516 output( "\t.section \".init\",\"ax\"\n" );
517 output( "\tcall .L__wine_spec_%s_init\n", make_c_identifier(spec->dll_name) );
518 output( "\t.section \".fini\",\"ax\"\n" );
519 output( "\tcall .L__wine_spec_%s_fini\n", make_c_identifier(spec->dll_name) );
524 /*******************************************************************
525 * output_module16
527 * Output code for a 16-bit module.
529 static void output_module16( DLLSPEC *spec )
531 ORDDEF **typelist;
532 ORDDEF *entry_point = NULL;
533 int i, j, nb_funcs;
535 if (!spec->file_name)
537 char *p;
538 spec->file_name = xstrdup( output_file_name );
539 if ((p = strrchr( spec->file_name, '.' ))) *p = 0;
541 if (!spec->dll_name) /* set default name from file name */
543 char *p;
544 spec->dll_name = xstrdup( spec->file_name );
545 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
548 /* store the main entry point as ordinal 0 */
550 if (!spec->ordinals)
552 spec->ordinals = xmalloc( sizeof(spec->ordinals[0]) );
553 spec->ordinals[0] = NULL;
555 if (spec->init_func && !(spec->characteristics & IMAGE_FILE_DLL))
557 entry_point = xmalloc( sizeof(*entry_point) );
558 entry_point->type = TYPE_PASCAL;
559 entry_point->ordinal = 0;
560 entry_point->lineno = 0;
561 entry_point->flags = FLAG_REGISTER;
562 entry_point->name = NULL;
563 entry_point->link_name = xstrdup( spec->init_func );
564 entry_point->export_name = NULL;
565 entry_point->u.func.arg_types[0] = 0;
566 assert( !spec->ordinals[0] );
567 spec->ordinals[0] = entry_point;
570 /* Build sorted list of all argument types, without duplicates */
572 typelist = xmalloc( (spec->limit + 1) * sizeof(*typelist) );
574 for (i = nb_funcs = 0; i <= spec->limit; i++)
576 ORDDEF *odp = spec->ordinals[i];
577 if (!odp) continue;
578 if (is_function( odp )) typelist[nb_funcs++] = odp;
581 nb_funcs = sort_func_list( typelist, nb_funcs, callfrom16_type_compare );
583 /* Output the module structure */
585 output( "\n/* module data */\n\n" );
586 output( "\t.data\n" );
587 output( "\t.align %d\n", get_alignment(4) );
588 output( ".L__wine_spec_dos_header:\n" );
589 output( "\t%s 0x%04x\n", get_asm_short_keyword(), /* e_magic */
590 IMAGE_DOS_SIGNATURE );
591 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cblp */
592 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cp */
593 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_crlc */
594 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cparhdr */
595 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_minalloc */
596 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_maxalloc */
597 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ss */
598 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_sp */
599 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_csum */
600 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ip */
601 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_cs */
602 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_lfarlc */
603 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_ovno */
604 output( "\t%s 0,0,0,0\n", get_asm_short_keyword() ); /* e_res */
605 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_oemid */
606 output( "\t%s 0\n", get_asm_short_keyword() ); /* e_oeminfo */
607 output( "\t%s 0,0,0,0,0,0,0,0,0,0\n", get_asm_short_keyword() ); /* e_res2 */
608 output( "\t.long .L__wine_spec_ne_header-.L__wine_spec_dos_header\n" );/* e_lfanew */
610 output( ".L__wine_spec_ne_header:\n" );
611 output( "\t%s 0x%04x\n", get_asm_short_keyword(), /* ne_magic */
612 IMAGE_OS2_SIGNATURE );
613 output( "\t.byte 0\n" ); /* ne_ver */
614 output( "\t.byte 0\n" ); /* ne_rev */
615 output( "\t%s .L__wine_spec_ne_enttab-.L__wine_spec_ne_header\n", /* ne_enttab */
616 get_asm_short_keyword() );
617 output( "\t%s .L__wine_spec_ne_enttab_end-.L__wine_spec_ne_enttab\n", /* ne_cbenttab */
618 get_asm_short_keyword() );
619 output( "\t.long 0\n" ); /* ne_crc */
620 output( "\t%s 0x%04x\n", get_asm_short_keyword(), /* ne_flags */
621 NE_FFLAGS_SINGLEDATA |
622 ((spec->characteristics & IMAGE_FILE_DLL) ? NE_FFLAGS_LIBMODULE : 0) );
623 output( "\t%s 2\n", get_asm_short_keyword() ); /* ne_autodata */
624 output( "\t%s %u\n", get_asm_short_keyword(), spec->heap_size ); /* ne_heap */
625 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_stack */
626 if (!entry_point) output( "\t.long 0\n" ); /* ne_csip */
627 else output( "\t%s .L__wine_%s_0-.L__wine_spec_code_segment,1\n",
628 get_asm_short_keyword(), make_c_identifier(spec->dll_name) );
629 output( "\t%s 0,2\n", get_asm_short_keyword() ); /* ne_sssp */
630 output( "\t%s 2\n", get_asm_short_keyword() ); /* ne_cseg */
631 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cmod */
632 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cbnrestab */
633 output( "\t%s .L__wine_spec_ne_segtab-.L__wine_spec_ne_header\n", /* ne_segtab */
634 get_asm_short_keyword() );
635 output( "\t%s .L__wine_spec_ne_rsrctab-.L__wine_spec_ne_header\n", /* ne_rsrctab */
636 get_asm_short_keyword() );
637 output( "\t%s .L__wine_spec_ne_restab-.L__wine_spec_ne_header\n", /* ne_restab */
638 get_asm_short_keyword() );
639 output( "\t%s .L__wine_spec_ne_modtab-.L__wine_spec_ne_header\n", /* ne_modtab */
640 get_asm_short_keyword() );
641 output( "\t%s .L__wine_spec_ne_imptab-.L__wine_spec_ne_header\n", /* ne_imptab */
642 get_asm_short_keyword() );
643 output( "\t.long 0\n" ); /* ne_nrestab */
644 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cmovent */
645 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_align */
646 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_cres */
647 output( "\t.byte 0x%02x\n", NE_OSFLAGS_WINDOWS ); /* ne_exetyp */
648 output( "\t.byte 0x%02x\n", NE_AFLAGS_FASTLOAD ); /* ne_flagsothers */
649 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_pretthunks */
650 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_psegrefbytes */
651 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_swaparea */
652 output( "\t%s 0\n", get_asm_short_keyword() ); /* ne_expver */
654 /* segment table */
656 output( "\n.L__wine_spec_ne_segtab:\n" );
658 /* code segment entry */
660 output( "\t%s .L__wine_spec_code_segment-.L__wine_spec_dos_header\n", /* filepos */
661 get_asm_short_keyword() );
662 output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* size */
663 get_asm_short_keyword() );
664 output( "\t%s 0x%04x\n", get_asm_short_keyword(), NE_SEGFLAGS_32BIT ); /* flags */
665 output( "\t%s .L__wine_spec_code_segment_end-.L__wine_spec_code_segment\n", /* minsize */
666 get_asm_short_keyword() );
668 /* data segment entry */
670 output( "\t%s .L__wine_spec_data_segment-.L__wine_spec_dos_header\n", /* filepos */
671 get_asm_short_keyword() );
672 output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* size */
673 get_asm_short_keyword() );
674 output( "\t%s 0x%04x\n", get_asm_short_keyword(), NE_SEGFLAGS_DATA ); /* flags */
675 output( "\t%s .L__wine_spec_data_segment_end-.L__wine_spec_data_segment\n", /* minsize */
676 get_asm_short_keyword() );
678 /* resource directory */
680 output_res16_directory( spec );
682 /* resident names table */
684 output( "\n\t.align %d\n", get_alignment(2) );
685 output( ".L__wine_spec_ne_restab:\n" );
686 output_resident_name( spec->dll_name, 0 );
687 for (i = 1; i <= spec->limit; i++)
689 ORDDEF *odp = spec->ordinals[i];
690 if (!odp || !odp->name[0]) continue;
691 output_resident_name( odp->name, i );
693 output( "\t.byte 0\n" );
695 /* imported names table */
697 output( "\n\t.align %d\n", get_alignment(2) );
698 output( ".L__wine_spec_ne_modtab:\n" );
699 output( ".L__wine_spec_ne_imptab:\n" );
700 output( "\t.byte 0,0\n" );
702 /* entry table */
704 output( "\n.L__wine_spec_ne_enttab:\n" );
705 output_entry_table( spec );
706 output( ".L__wine_spec_ne_enttab_end:\n" );
708 /* code segment */
710 output( "\n\t.align %d\n", get_alignment(2) );
711 output( ".L__wine_spec_code_segment:\n" );
713 for ( i = 0; i < nb_funcs; i++ )
715 unsigned int arg_types[2];
716 int nop_words, argsize = 0;
718 if ( typelist[i]->type == TYPE_PASCAL )
719 argsize = get_function_argsize( typelist[i] );
721 /* build the arg types bit fields */
722 arg_types[0] = arg_types[1] = 0;
723 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
725 int type = 0;
726 switch(typelist[i]->u.func.arg_types[j])
728 case 'w': type = ARG_WORD; break;
729 case 's': type = ARG_SWORD; break;
730 case 'l': type = ARG_LONG; break;
731 case 'p': type = ARG_PTR; break;
732 case 't': type = ARG_STR; break;
733 case 'T': type = ARG_SEGSTR; break;
735 arg_types[j / 10] |= type << (3 * (j % 10));
737 if (typelist[i]->type == TYPE_VARARGS) arg_types[j / 10] |= ARG_VARARG << (3 * (j % 10));
739 output( ".L__wine_spec_callfrom16_%s:\n", get_callfrom16_name(typelist[i]) );
740 output( "\tpushl $.L__wine_spec_call16_%s\n", get_relay_name(typelist[i]) );
741 output( "\tlcall $0,$0\n" );
743 if (typelist[i]->flags & FLAG_REGISTER)
745 nop_words = 4;
747 else if (typelist[i]->flags & FLAG_RET16)
749 output( "\torw %%ax,%%ax\n" );
750 output( "\tnop\n" ); /* so that the lretw is aligned */
751 nop_words = 2;
753 else
755 output( "\tshld $16,%%eax,%%edx\n" );
756 output( "\torl %%eax,%%eax\n" );
757 nop_words = 1;
760 if (argsize)
762 output( "\tlretw $%u\n", argsize );
763 nop_words--;
765 else output( "\tlretw\n" );
767 if (nop_words) output( "\t%s\n", nop_sequence[nop_words-1] );
769 /* the movl is here so that the code contains only valid instructions, */
770 /* it's never actually executed, we only care about the arg_types[] values */
771 output( "\t%s 0x86c7\n", get_asm_short_keyword() );
772 output( "\t.long 0x%08x,0x%08x\n", arg_types[0], arg_types[1] );
775 for (i = 0; i <= spec->limit; i++)
777 ORDDEF *odp = spec->ordinals[i];
778 if (!odp || !is_function( odp )) continue;
779 output( ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i );
780 output( "\tpushw %%bp\n" );
781 output( "\tpushl $%s\n",
782 asm_name( odp->type == TYPE_STUB ? get_stub_name( odp, spec ) : odp->link_name ));
783 output( "\tcallw .L__wine_spec_callfrom16_%s\n", get_callfrom16_name( odp ) );
785 output( ".L__wine_spec_code_segment_end:\n" );
787 /* data segment */
789 output( "\n.L__wine_spec_data_segment:\n" );
790 output( "\t.byte 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\n" ); /* instance data */
791 for (i = 0; i <= spec->limit; i++)
793 ORDDEF *odp = spec->ordinals[i];
794 if (!odp || odp->type != TYPE_VARIABLE) continue;
795 output( ".L__wine_%s_%u:\n", make_c_identifier(spec->dll_name), i );
796 output( "\t.long " );
797 for (j = 0; j < odp->u.var.n_values-1; j++)
798 output( "0x%08x,", odp->u.var.values[j] );
799 output( "0x%08x\n", odp->u.var.values[j] );
801 output( ".L__wine_spec_data_segment_end:\n" );
803 /* resource data */
805 if (spec->nb_resources)
807 output( "\n.L__wine_spec_resource_data:\n" );
808 output_res16_data( spec );
811 output( "\t.byte 0\n" ); /* make sure the last symbol points to something */
813 /* relay functions */
815 nb_funcs = sort_func_list( typelist, nb_funcs, relay_type_compare );
816 if (nb_funcs)
818 output( "\n/* relay functions */\n\n" );
819 output( "\t.text\n" );
820 for ( i = 0; i < nb_funcs; i++ ) output_call16_function( typelist[i] );
821 output( "\t.data\n" );
822 output( "wine_ldt_copy_ptr:\n" );
823 output( "\t.long %s\n", asm_name("wine_ldt_copy") );
826 free( typelist );
830 /*******************************************************************
831 * BuildSpec16File
833 * Build a Win16 assembly file from a spec file.
835 void BuildSpec16File( DLLSPEC *spec )
837 output_standard_file_header();
838 output_module16( spec );
839 output_init_code( spec );
841 output( "\n\t%s\n", get_asm_string_section() );
842 output( ".L__wine_spec_file_name:\n" );
843 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec->file_name );
845 output_stubs( spec );
846 output_get_pc_thunk();
847 output_gnu_stack_note();
851 /*******************************************************************
852 * output_spec16_file
854 * Output the complete data for a spec 16-bit file.
856 void output_spec16_file( DLLSPEC *spec16 )
858 DLLSPEC *spec32 = alloc_dll_spec();
860 spec32->file_name = xstrdup( spec16->file_name );
862 if (spec16->characteristics & IMAGE_FILE_DLL)
864 spec32->characteristics = IMAGE_FILE_DLL;
865 spec32->init_func = xstrdup( "__wine_spec_dll_entry" );
868 resolve_imports( spec16 );
869 add_16bit_exports( spec32, spec16 );
871 output_standard_file_header();
872 output_module( spec32 );
873 output_module16( spec16 );
874 output_stubs( spec16 );
875 output_exports( spec32 );
876 output_imports( spec16 );
877 output_resources( spec16 );
878 if (spec16->main_module)
880 output( "\n\t%s\n", get_asm_string_section() );
881 output( ".L__wine_spec_main_module:\n" );
882 output( "\t%s \"%s\"\n", get_asm_string_keyword(), spec16->main_module );
884 output_gnu_stack_note();
885 free_dll_spec( spec32 );