Renamed __wine_(un)register_dll_16 to __wine_dll_(un)register_16 for
[wine/gsoc_dplay.git] / tools / winebuild / spec16.c
blob0ef6b97e6c377e322cfe2b845e844469df1ae7ce
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
31 #include "wine/exception.h"
32 #include "wine/winbase16.h"
34 #include "build.h"
37 /*******************************************************************
38 * get_cs
40 #ifdef __i386__
41 static inline unsigned short get_cs(void)
43 unsigned short res;
44 #ifdef __GNUC__
45 __asm__("movw %%cs,%w0" : "=r"(res));
46 #elif defined(_MSC_VER)
47 __asm { mov res, cs }
48 #else
49 res = 0;
50 #endif
51 return res;
53 #endif /* __i386__ */
56 /*******************************************************************
57 * output_file_header
59 * Output a file header with the common declarations we need.
61 static void output_file_header( FILE *outfile )
63 output_standard_file_header( outfile );
64 fprintf( outfile, "extern struct\n{\n" );
65 fprintf( outfile, " void *base[8192];\n" );
66 fprintf( outfile, " unsigned long limit[8192];\n" );
67 fprintf( outfile, " unsigned char flags[8192];\n" );
68 fprintf( outfile, "} wine_ldt_copy;\n\n" );
69 #ifdef __i386__
70 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
71 #else
72 fprintf( outfile, "#define __stdcall\n\n" );
73 #endif
77 /*******************************************************************
78 * StoreVariableCode
80 * Store a list of ints into a byte array.
82 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
84 int i;
86 switch(size)
88 case 1:
89 for (i = 0; i < odp->u.var.n_values; i++)
90 buffer[i] = odp->u.var.values[i];
91 break;
92 case 2:
93 for (i = 0; i < odp->u.var.n_values; i++)
94 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
95 break;
96 case 4:
97 for (i = 0; i < odp->u.var.n_values; i++)
98 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
99 break;
101 return odp->u.var.n_values * size;
105 /*******************************************************************
106 * output_entry_table
108 static int output_entry_table( unsigned char **ret_buff, DLLSPEC *spec )
110 int i, prev = 0, prev_sel = -1;
111 unsigned char *pstr, *buffer;
112 unsigned char *bundle = NULL;
114 buffer = xmalloc( spec->limit * 5 ); /* we use at most 5 bytes per entry-point */
115 pstr = buffer;
117 for (i = 1; i <= spec->limit; i++)
119 int selector = 0;
120 WORD offset;
121 ORDDEF *odp = spec->ordinals[i];
122 if (!odp) continue;
124 switch (odp->type)
126 case TYPE_CDECL:
127 case TYPE_PASCAL:
128 case TYPE_VARARGS:
129 case TYPE_STUB:
130 selector = 1; /* Code selector */
131 break;
132 case TYPE_VARIABLE:
133 selector = 2; /* Data selector */
134 break;
135 case TYPE_ABS:
136 selector = 0xfe; /* Constant selector */
137 break;
138 default:
139 continue;
142 if (!bundle || prev + 1 != i || prev_sel != selector || *bundle == 255)
144 /* need to start a new bundle */
146 if (prev + 1 != i)
148 int skip = i - (prev + 1);
149 while (skip > 255)
151 *pstr++ = 255;
152 *pstr++ = 0;
153 skip -= 255;
155 *pstr++ = skip;
156 *pstr++ = 0;
159 bundle = pstr;
160 *pstr++ = 0;
161 *pstr++ = selector;
162 prev_sel = selector;
164 /* output the entry */
165 *pstr++ = 3; /* flags: exported & public data */
166 offset = odp->offset;
167 memcpy( pstr, &offset, sizeof(WORD) );
168 pstr += sizeof(WORD);
169 (*bundle)++; /* increment bundle entry count */
170 prev = i;
172 *pstr++ = 0;
173 if ((pstr - buffer) & 1) *pstr++ = 0;
174 *ret_buff = xrealloc( buffer, pstr - buffer );
175 return pstr - buffer;
179 /*******************************************************************
180 * output_bytes
182 static void output_bytes( FILE *outfile, const void *buffer, unsigned int size )
184 unsigned int i;
185 const unsigned char *ptr = buffer;
187 fprintf( outfile, " {" );
188 for (i = 0; i < size; i++)
190 if (!(i & 7)) fprintf( outfile, "\n " );
191 fprintf( outfile, " 0x%02x", *ptr++ );
192 if (i < size - 1) fprintf( outfile, "," );
194 fprintf( outfile, "\n },\n" );
198 /*******************************************************************
199 * output_module_data
201 * Output the 16-bit NE module structure.
203 static void output_module_data( FILE *outfile, int max_code_offset, const void *data_segment,
204 unsigned int data_size, DLLSPEC *spec )
206 unsigned char *res_buffer, *et_buffer;
207 unsigned char string[256];
208 int i;
209 unsigned int segtable_offset, resdir_offset, impnames_offset, resnames_offset, et_offset, data_offset;
210 unsigned int resnames_size, resdir_size, et_size;
212 /* DOS header */
214 fprintf( outfile, "static const struct module_data\n{\n" );
215 fprintf( outfile, " struct\n {\n" );
216 fprintf( outfile, " unsigned short e_magic;\n" );
217 fprintf( outfile, " unsigned short e_cblp;\n" );
218 fprintf( outfile, " unsigned short e_cp;\n" );
219 fprintf( outfile, " unsigned short e_crlc;\n" );
220 fprintf( outfile, " unsigned short e_cparhdr;\n" );
221 fprintf( outfile, " unsigned short e_minalloc;\n" );
222 fprintf( outfile, " unsigned short e_maxalloc;\n" );
223 fprintf( outfile, " unsigned short e_ss;\n" );
224 fprintf( outfile, " unsigned short e_sp;\n" );
225 fprintf( outfile, " unsigned short e_csum;\n" );
226 fprintf( outfile, " unsigned short e_ip;\n" );
227 fprintf( outfile, " unsigned short e_cs;\n" );
228 fprintf( outfile, " unsigned short e_lfarlc;\n" );
229 fprintf( outfile, " unsigned short e_ovno;\n" );
230 fprintf( outfile, " unsigned short e_res[4];\n" );
231 fprintf( outfile, " unsigned short e_oemid;\n" );
232 fprintf( outfile, " unsigned short e_oeminfo;\n" );
233 fprintf( outfile, " unsigned short e_res2[10];\n" );
234 fprintf( outfile, " unsigned int e_lfanew;\n" );
235 fprintf( outfile, " } dos_header;\n" );
237 /* NE header */
239 fprintf( outfile, " struct\n {\n" );
240 fprintf( outfile, " unsigned short ne_magic;\n" );
241 fprintf( outfile, " unsigned char ne_ver;\n" );
242 fprintf( outfile, " unsigned char ne_rev;\n" );
243 fprintf( outfile, " unsigned short ne_enttab;\n" );
244 fprintf( outfile, " unsigned short ne_cbenttab;\n" );
245 fprintf( outfile, " int ne_crc;\n" );
246 fprintf( outfile, " unsigned short ne_flags;\n" );
247 fprintf( outfile, " unsigned short ne_autodata;\n" );
248 fprintf( outfile, " unsigned short ne_heap;\n" );
249 fprintf( outfile, " unsigned short ne_stack;\n" );
250 fprintf( outfile, " unsigned int ne_csip;\n" );
251 fprintf( outfile, " unsigned int ne_sssp;\n" );
252 fprintf( outfile, " unsigned short ne_cseg;\n" );
253 fprintf( outfile, " unsigned short ne_cmod;\n" );
254 fprintf( outfile, " unsigned short ne_cbnrestab;\n" );
255 fprintf( outfile, " unsigned short ne_segtab;\n" );
256 fprintf( outfile, " unsigned short ne_rsrctab;\n" );
257 fprintf( outfile, " unsigned short ne_restab;\n" );
258 fprintf( outfile, " unsigned short ne_modtab;\n" );
259 fprintf( outfile, " unsigned short ne_imptab;\n" );
260 fprintf( outfile, " unsigned int ne_nrestab;\n" );
261 fprintf( outfile, " unsigned short ne_cmovent;\n" );
262 fprintf( outfile, " unsigned short ne_align;\n" );
263 fprintf( outfile, " unsigned short ne_cres;\n" );
264 fprintf( outfile, " unsigned char ne_exetyp;\n" );
265 fprintf( outfile, " unsigned char ne_flagsothers;\n" );
266 fprintf( outfile, " unsigned short ne_pretthunks;\n" );
267 fprintf( outfile, " unsigned short ne_psegrefbytes;\n" );
268 fprintf( outfile, " unsigned short ne_swaparea;\n" );
269 fprintf( outfile, " unsigned short ne_expver;\n" );
270 fprintf( outfile, " } os2_header;\n" );
272 /* segment table */
274 segtable_offset = 64;
275 fprintf( outfile, " struct\n {\n" );
276 fprintf( outfile, " unsigned short filepos;\n" );
277 fprintf( outfile, " unsigned short size;\n" );
278 fprintf( outfile, " unsigned short flags;\n" );
279 fprintf( outfile, " unsigned short minsize;\n" );
280 fprintf( outfile, " } segtable[2];\n" );
282 /* resource directory */
284 resdir_offset = segtable_offset + 2 * 8;
285 resdir_size = output_res16_directory( &res_buffer, spec );
286 fprintf( outfile, " unsigned char resdir[%d];\n", resdir_size );
288 /* resident names table */
290 resnames_offset = resdir_offset + resdir_size;
291 fprintf( outfile, " struct\n {\n" );
292 fprintf( outfile, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_0;\n",
293 strlen( spec->dll_name ) );
294 resnames_size = 3 + strlen( spec->dll_name );
295 for (i = 1; i <= spec->limit; i++)
297 ORDDEF *odp = spec->ordinals[i];
298 if (!odp || !odp->name[0]) continue;
299 fprintf( outfile, " struct { unsigned char len; char name[%d]; unsigned short ord; } name_%d;\n",
300 strlen(odp->name), i );
301 resnames_size += 3 + strlen( odp->name );
303 fprintf( outfile, " unsigned char name_last[%d];\n", 2 - (resnames_size & 1) );
304 resnames_size = (resnames_size + 2) & ~1;
305 fprintf( outfile, " } resnames;\n" );
307 /* imported names table */
309 impnames_offset = resnames_offset + resnames_size;
310 fprintf( outfile, " unsigned char impnames[2];\n" );
312 /* entry table */
314 et_offset = impnames_offset + 2;
315 et_size = output_entry_table( &et_buffer, spec );
316 fprintf( outfile, " unsigned char entry_table[%d];\n", et_size );
318 /* data segment */
320 data_offset = et_offset + et_size;
321 fprintf( outfile, " unsigned char data_segment[%d];\n", data_size );
322 if (data_offset + data_size >= 0x10000)
323 fatal_error( "Not supported yet: 16-bit module data larger than 64K\n" );
325 /* DOS header */
327 fprintf( outfile, "} module =\n{\n {\n" );
328 fprintf( outfile, " 0x%04x,\n", IMAGE_DOS_SIGNATURE ); /* e_magic */
329 fprintf( outfile, " 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n" );
330 fprintf( outfile, " { 0, 0, 0, 0, }, 0, 0,\n" );
331 fprintf( outfile, " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },\n" );
332 fprintf( outfile, " sizeof(module.dos_header)\n" ); /* e_lfanew */
334 /* NE header */
336 fprintf( outfile, " },\n {\n" );
337 fprintf( outfile, " 0x%04x,\n", IMAGE_OS2_SIGNATURE ); /* ne_magic */
338 fprintf( outfile, " 0, 0,\n" );
339 fprintf( outfile, " %d,\n", et_offset ); /* ne_enttab */
340 fprintf( outfile, " sizeof(module.entry_table),\n" ); /* ne_cbenttab */
341 fprintf( outfile, " 0,\n" ); /* ne_crc */
342 fprintf( outfile, " 0x%04x,\n", /* ne_flags */
343 NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE );
344 fprintf( outfile, " 2,\n" ); /* ne_autodata */
345 fprintf( outfile, " %d,\n", spec->heap_size ); /* ne_heap */
346 fprintf( outfile, " 0, 0, 0,\n" );
347 fprintf( outfile, " 2,\n" ); /* ne_cseg */
348 fprintf( outfile, " 0,\n" ); /* ne_cmod */
349 fprintf( outfile, " 0,\n" ); /* ne_cbnrestab */
350 fprintf( outfile, " %d,\n", segtable_offset ); /* ne_segtab */
351 fprintf( outfile, " %d,\n", resdir_offset ); /* ne_rsrctab */
352 fprintf( outfile, " %d,\n", resnames_offset ); /* ne_restab */
353 fprintf( outfile, " %d,\n", impnames_offset ); /* ne_modtab */
354 fprintf( outfile, " %d,\n", impnames_offset ); /* ne_imptab */
355 fprintf( outfile, " 0,\n" ); /* ne_nrestab */
356 fprintf( outfile, " 0,\n" ); /* ne_cmovent */
357 fprintf( outfile, " 0,\n" ); /* ne_align */
358 fprintf( outfile, " 0,\n" ); /* ne_cres */
359 fprintf( outfile, " 0x%04x,\n", NE_OSFLAGS_WINDOWS ); /* ne_exetyp */
360 fprintf( outfile, " 0x%04x,\n", NE_AFLAGS_FASTLOAD ); /* ne_flagsothers */
361 fprintf( outfile, " 0,\n" ); /* ne_pretthunks */
362 fprintf( outfile, " sizeof(module),\n" ); /* ne_psegrefbytes */
363 fprintf( outfile, " 0,\n" ); /* ne_swaparea */
364 fprintf( outfile, " 0\n" ); /* ne_expver */
365 fprintf( outfile, " },\n" );
367 /* segment table */
369 fprintf( outfile, " {\n" );
370 fprintf( outfile, " { 0, %d, 0x%04x, %d },\n",
371 max_code_offset, NE_SEGFLAGS_32BIT, max_code_offset );
372 fprintf( outfile, " { %d, %d, 0x%04x, %d },\n",
373 data_offset, data_size, NE_SEGFLAGS_DATA, data_size );
374 fprintf( outfile, " },\n" );
376 /* resource directory */
378 output_bytes( outfile, res_buffer, resdir_size );
379 free( res_buffer );
381 /* resident names table */
383 fprintf( outfile, " {\n" );
384 strcpy( string, spec->dll_name );
385 fprintf( outfile, " { %d, \"%s\", 0 },\n", strlen(string), strupper(string) );
386 for (i = 1; i <= spec->limit; i++)
388 ORDDEF *odp = spec->ordinals[i];
389 if (!odp || !odp->name[0]) continue;
390 strcpy( string, odp->name );
391 fprintf( outfile, " { %d, \"%s\", %d },\n", strlen(string), strupper(string), i );
393 fprintf( outfile, " { 0 }\n },\n" );
395 /* imported names table */
397 fprintf( outfile, " { 0, 0 },\n" );
399 /* entry table */
401 output_bytes( outfile, et_buffer, et_size );
402 free( et_buffer );
404 /* data_segment */
406 output_bytes( outfile, data_segment, data_size );
408 fprintf( outfile, "};\n" );
412 #ifdef __i386__
413 /*******************************************************************
414 * BuildCallFrom16Func
416 * Build a 16-bit-to-Wine callback glue function.
418 * The generated routines are intended to be used as argument conversion
419 * routines to be called by the CallFrom16... core. Thus, the prototypes of
420 * the generated routines are (see also CallFrom16):
422 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
423 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
424 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
425 * CONTEXT86 *context );
427 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
428 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
429 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
430 * 'T'=segmented pointer to null-terminated string).
432 * The generated routines fetch the arguments from the 16-bit stack (pointed
433 * to by 'args'); the offsets of the single argument values are computed
434 * according to the calling convention and the argument types. Then, the
435 * 32-bit entry point is called with these arguments.
437 * For register functions, the arguments (if present) are converted just
438 * the same as for normal functions, but in addition the CONTEXT86 pointer
439 * filled with the current register values is passed to the 32-bit routine.
441 static void BuildCallFrom16Func( FILE *outfile, const char *profile, const char *prefix )
443 int i, pos, argsize = 0;
444 int short_ret = 0;
445 int reg_func = 0;
446 int usecdecl = 0;
447 int varargs = 0;
448 const char *args = profile + 7;
449 const char *ret_type;
451 /* Parse function type */
453 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
454 else if (!strncmp( "v_", profile, 2 )) varargs = usecdecl = 1;
455 else if (strncmp( "p_", profile, 2 ))
457 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
458 return;
461 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
462 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
463 else if (strncmp( "long_", profile + 2, 5 ))
465 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
466 return;
469 for ( i = 0; args[i]; i++ )
470 switch ( args[i] )
472 case 'w': /* word */
473 case 's': /* s_word */
474 argsize += 2;
475 break;
476 case 'l': /* long or segmented pointer */
477 case 'T': /* segmented pointer to null-terminated string */
478 case 'p': /* linear pointer */
479 case 't': /* linear pointer to null-terminated string */
480 argsize += 4;
481 break;
484 ret_type = reg_func? "void" : short_ret ? "unsigned short" : "unsigned int";
486 fprintf( outfile, "typedef %s (%s*proc_%s_t)( ",
487 ret_type, usecdecl ? "" : "__stdcall ", profile );
488 args = profile + 7;
489 for ( i = 0; args[i]; i++ )
491 if ( i ) fprintf( outfile, ", " );
492 switch (args[i])
494 case 'w': fprintf( outfile, "unsigned short" ); break;
495 case 's': fprintf( outfile, "short" ); break;
496 case 'l': case 'T': fprintf( outfile, "unsigned int" ); break;
497 case 'p': case 't': fprintf( outfile, "void *" ); break;
500 if (reg_func || varargs)
501 fprintf( outfile, "%svoid *", i? ", " : "" );
502 else if ( !i )
503 fprintf( outfile, "void" );
504 fprintf( outfile, " );\n" );
506 fprintf( outfile, "static %s __stdcall __wine_%s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
507 ret_type, make_c_identifier(prefix), profile, profile,
508 reg_func? ", void *context" : "" );
510 fprintf( outfile, "{\n %sproc(\n", reg_func ? "" : "return " );
511 args = profile + 7;
512 pos = !usecdecl? argsize : 0;
513 for ( i = 0; args[i]; i++ )
515 if ( i ) fprintf( outfile, ",\n" );
516 fprintf( outfile, " " );
517 switch (args[i])
519 case 'w': /* word */
520 if ( !usecdecl ) pos -= 2;
521 fprintf( outfile, "*(unsigned short *)(args+%d)", pos );
522 if ( usecdecl ) pos += 2;
523 break;
525 case 's': /* s_word */
526 if ( !usecdecl ) pos -= 2;
527 fprintf( outfile, "*(short *)(args+%d)", pos );
528 if ( usecdecl ) pos += 2;
529 break;
531 case 'l': /* long or segmented pointer */
532 case 'T': /* segmented pointer to null-terminated string */
533 if ( !usecdecl ) pos -= 4;
534 fprintf( outfile, "*(unsigned int *)(args+%d)", pos );
535 if ( usecdecl ) pos += 4;
536 break;
538 case 'p': /* linear pointer */
539 case 't': /* linear pointer to null-terminated string */
540 if ( !usecdecl ) pos -= 4;
541 fprintf( outfile, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
542 pos + 2, pos );
543 if ( usecdecl ) pos += 4;
544 break;
546 default:
547 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
550 if ( reg_func )
551 fprintf( outfile, "%s context", i? ",\n" : "" );
552 else if (varargs)
553 fprintf( outfile, "%s args + %d", i? ",\n" : "", argsize );
554 fprintf( outfile, " );\n}\n\n" );
556 #endif
559 /*******************************************************************
560 * get_function_name
562 static const char *get_function_name( const ORDDEF *odp )
564 static char buffer[80];
566 sprintf( buffer, "%s_%s_%s",
567 (odp->type == TYPE_PASCAL) ? "p" :
568 (odp->type == TYPE_VARARGS) ? "v" : "c",
569 (odp->flags & FLAG_REGISTER) ? "regs" :
570 (odp->flags & FLAG_RET16) ? "word" : "long",
571 odp->u.func.arg_types );
572 return buffer;
576 /*******************************************************************
577 * Spec16TypeCompare
579 static int Spec16TypeCompare( const void *e1, const void *e2 )
581 const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
582 const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
583 int retval;
584 int type1 = odp1->type;
585 int type2 = odp2->type;
587 if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
588 if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
590 if ((retval = type1 - type2) != 0) return retval;
592 type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
593 type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
595 if ((retval = type1 - type2) != 0) return retval;
597 return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
601 /*******************************************************************
602 * output_stub_funcs
604 * Output the functions for stub entry points
606 static void output_stub_funcs( FILE *outfile, const DLLSPEC *spec )
608 int i;
609 char *p;
611 for (i = 0; i <= spec->limit; i++)
613 ORDDEF *odp = spec->ordinals[i];
614 if (!odp || odp->type != TYPE_STUB) continue;
615 fprintf( outfile, "#ifdef __GNUC__\n" );
616 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
617 fprintf( outfile, "#endif\n" );
618 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
619 fprintf( outfile, " struct exc_record {\n" );
620 fprintf( outfile, " unsigned int code, flags;\n" );
621 fprintf( outfile, " void *rec, *addr;\n" );
622 fprintf( outfile, " unsigned int params;\n" );
623 fprintf( outfile, " const void *info[15];\n" );
624 fprintf( outfile, " } rec;\n\n" );
625 fprintf( outfile, " extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
626 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
627 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
628 fprintf( outfile, " rec.rec = 0;\n" );
629 fprintf( outfile, " rec.params = 2;\n" );
630 fprintf( outfile, " rec.info[0] = \"%s\";\n", spec->file_name );
631 fprintf( outfile, " rec.info[1] = func;\n" );
632 fprintf( outfile, "#ifdef __GNUC__\n" );
633 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
634 fprintf( outfile, "#else\n" );
635 fprintf( outfile, " rec.addr = 0;\n" );
636 fprintf( outfile, "#endif\n" );
637 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
638 break;
640 for (i = 0; i <= spec->limit; i++)
642 ORDDEF *odp = spec->ordinals[i];
643 if (!odp || odp->type != TYPE_STUB) continue;
644 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
645 strcpy( odp->link_name, "__wine_stub_" );
646 strcat( odp->link_name, odp->name );
647 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
648 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
649 odp->link_name, odp->name );
654 /*******************************************************************
655 * BuildSpec16File
657 * Build a Win16 assembly file from a spec file.
659 void BuildSpec16File( FILE *outfile, DLLSPEC *spec )
661 ORDDEF **type, **typelist;
662 int i, nFuncs, nTypes;
663 int code_offset, data_offset, res_size;
664 unsigned char *data;
665 char constructor[100], destructor[100];
666 #ifdef __i386__
667 unsigned short code_selector = get_cs();
668 #endif
670 /* File header */
672 output_file_header( outfile );
673 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
674 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
675 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
676 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
678 data = (unsigned char *)xmalloc( 0x10000 );
679 memset( data, 0, 16 );
680 data_offset = 16;
682 if (!spec->dll_name) /* set default name from file name */
684 char *p;
685 spec->dll_name = xstrdup( spec->file_name );
686 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
689 output_stub_funcs( outfile, spec );
691 /* Build sorted list of all argument types, without duplicates */
693 typelist = (ORDDEF **)calloc( spec->limit+1, sizeof(ORDDEF *) );
695 for (i = nFuncs = 0; i <= spec->limit; i++)
697 ORDDEF *odp = spec->ordinals[i];
698 if (!odp) continue;
699 switch (odp->type)
701 case TYPE_CDECL:
702 case TYPE_PASCAL:
703 case TYPE_VARARGS:
704 case TYPE_STUB:
705 typelist[nFuncs++] = odp;
707 default:
708 break;
712 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
714 i = nTypes = 0;
715 while ( i < nFuncs )
717 typelist[nTypes++] = typelist[i++];
718 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
719 i++;
722 /* Output CallFrom16 routines needed by this .spec file */
723 #ifdef __i386__
724 for ( i = 0; i < nTypes; i++ )
726 char profile[101];
728 strcpy( profile, get_function_name( typelist[i] ));
729 BuildCallFrom16Func( outfile, profile, spec->file_name );
731 #endif
733 /* Output the DLL functions prototypes */
735 for (i = 0; i <= spec->limit; i++)
737 ORDDEF *odp = spec->ordinals[i];
738 if (!odp) continue;
739 switch(odp->type)
741 case TYPE_CDECL:
742 case TYPE_PASCAL:
743 case TYPE_VARARGS:
744 fprintf( outfile, "extern void %s();\n", odp->link_name );
745 break;
746 default:
747 break;
751 /* Output code segment */
753 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
754 fprintf( outfile, "\nstatic struct code_segment\n{\n" );
755 fprintf( outfile, " struct {\n" );
756 #ifdef __i386__
757 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
758 fprintf( outfile, " void *relay;\n" );
759 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
760 fprintf( outfile, " void *glue;\n" );
761 fprintf( outfile, " unsigned short flatcs;\n" );
762 #endif
763 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
764 fprintf( outfile, " unsigned short args;\n" );
765 fprintf( outfile, " unsigned int arg_types[2];\n" );
766 fprintf( outfile, " } call[%d];\n", nTypes );
767 fprintf( outfile, " struct {\n" );
768 #ifdef __i386__
769 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
770 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
771 #endif
772 fprintf( outfile, " void (*target)();\n" );
773 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
774 fprintf( outfile, " short callfrom16;\n" );
775 fprintf( outfile, " } entry[%d];\n", nFuncs );
776 fprintf( outfile, "} code_segment =\n{\n {\n" );
778 code_offset = 0;
780 for ( i = 0; i < nTypes; i++ )
782 char profile[101], *arg;
783 unsigned int arg_types[2];
784 int j, argsize = 0;
786 strcpy( profile, get_function_name( typelist[i] ));
787 if ( typelist[i]->type == TYPE_PASCAL )
788 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
789 switch ( *arg )
791 case 'w': /* word */
792 case 's': /* s_word */
793 argsize += 2;
794 break;
795 case 'l': /* long or segmented pointer */
796 case 'T': /* segmented pointer to null-terminated string */
797 case 'p': /* linear pointer */
798 case 't': /* linear pointer to null-terminated string */
799 argsize += 4;
800 break;
803 /* build the arg types bit fields */
804 arg_types[0] = arg_types[1] = 0;
805 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
807 int type = 0;
808 switch(typelist[i]->u.func.arg_types[j])
810 case 'w': type = ARG_WORD; break;
811 case 's': type = ARG_SWORD; break;
812 case 'l': type = ARG_LONG; break;
813 case 'p': type = ARG_PTR; break;
814 case 't': type = ARG_STR; break;
815 case 'T': type = ARG_SEGSTR; break;
817 arg_types[j / 10] |= type << (3 * (j % 10));
819 if (typelist[i]->flags & FLAG_REGISTER) arg_types[0] |= ARG_REGISTER;
820 if (typelist[i]->flags & FLAG_RET16) arg_types[0] |= ARG_RET16;
822 #ifdef __i386__
823 fprintf( outfile, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
824 make_c_identifier(spec->file_name), profile,
825 (typelist[i]->flags & FLAG_REGISTER) ? "regs":
826 (typelist[i]->flags & FLAG_RET16) ? "word" : "long" );
827 if (argsize)
828 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
829 code_selector, argsize, arg_types[0], arg_types[1] );
830 else
831 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
832 code_selector, arg_types[0], arg_types[1] );
833 #else
834 if (argsize)
835 fprintf( outfile, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
836 argsize, arg_types[0], arg_types[1] );
837 else
838 fprintf( outfile, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
839 arg_types[0], arg_types[1] );
840 #endif
841 code_offset += sizeof(CALLFROM16);
843 fprintf( outfile, " },\n {\n" );
845 for (i = 0; i <= spec->limit; i++)
847 ORDDEF *odp = spec->ordinals[i];
848 if (!odp) continue;
849 switch (odp->type)
851 case TYPE_ABS:
852 odp->offset = LOWORD(odp->u.abs.value);
853 break;
855 case TYPE_VARIABLE:
856 odp->offset = data_offset;
857 data_offset += StoreVariableCode( data + data_offset, 4, odp);
858 break;
860 case TYPE_CDECL:
861 case TYPE_PASCAL:
862 case TYPE_VARARGS:
863 case TYPE_STUB:
864 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
865 assert( type );
867 fprintf( outfile, " /* %s.%d */ ", spec->dll_name, i );
868 #ifdef __i386__
869 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
870 #else
871 fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
872 #endif
873 odp->link_name,
874 (type-typelist)*sizeof(CALLFROM16) -
875 (code_offset + sizeof(ENTRYPOINT16)),
876 get_function_name( odp ) );
878 odp->offset = code_offset;
879 code_offset += sizeof(ENTRYPOINT16);
880 break;
882 default:
883 fprintf(stderr,"build: function type %d not available for Win16\n",
884 odp->type);
885 exit(1);
889 fprintf( outfile, " }\n};\n" );
891 /* Build the module */
893 output_module_data( outfile, code_offset, data, data_offset, spec );
894 res_size = output_res16_data( outfile, spec );
896 /* Output the DLL descriptor */
898 fprintf( outfile, "#include \"poppack.h\"\n\n" );
900 fprintf( outfile, "static const struct dll_descriptor\n{\n" );
901 fprintf( outfile, " const struct module_data *module;\n" );
902 fprintf( outfile, " struct code_segment *code_start;\n" );
903 fprintf( outfile, " const unsigned char *rsrc;\n" );
904 fprintf( outfile, "} descriptor =\n{\n" );
905 fprintf( outfile, " &module,\n" );
906 fprintf( outfile, " &code_segment,\n" );
907 fprintf( outfile, " %s\n", res_size ? "resource_data" : "0" );
908 fprintf( outfile, "};\n" );
910 /* Output the DLL constructor */
912 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(spec->file_name) );
913 sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(spec->file_name) );
914 output_dll_init( outfile, constructor, destructor );
916 fprintf( outfile,
917 "void %s(void)\n"
918 "{\n"
919 " extern void __wine_dll_register_16( const struct dll_descriptor *descr, const char *file_name );\n"
920 " __wine_dll_register_16( &descriptor, \"%s\" );\n"
921 "}\n", constructor, spec->file_name );
922 fprintf( outfile,
923 "void %s(void)\n"
924 "{\n"
925 " extern void __wine_dll_unregister_16( const struct dll_descriptor *descr );\n"
926 " __wine_dll_unregister_16( &descriptor );\n"
927 "}\n", destructor );