Use RaiseException instead of RtlRaiseException in 16-bit spec files
[wine/multimedia.git] / tools / winebuild / spec16.c
bloba4261b4aabfff14d526dce940a19d121caf48035
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, " extern void __stdcall RaiseException( unsigned int, unsigned int, unsigned int, const void ** );\n" );
620 fprintf( outfile, " const void *args[2];\n" );
621 fprintf( outfile, " args[0] = \"%s\";\n", spec->file_name );
622 fprintf( outfile, " args[1] = func;\n" );
623 fprintf( outfile, " for (;;) RaiseException( 0x%08x, %d, 2, args );\n}\n\n",
624 EXCEPTION_WINE_STUB, EH_NONCONTINUABLE );
625 break;
627 for (i = 0; i <= spec->limit; i++)
629 ORDDEF *odp = spec->ordinals[i];
630 if (!odp || odp->type != TYPE_STUB) continue;
631 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
632 strcpy( odp->link_name, "__wine_stub_" );
633 strcat( odp->link_name, odp->name );
634 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
635 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
636 odp->link_name, odp->name );
641 /*******************************************************************
642 * BuildSpec16File
644 * Build a Win16 assembly file from a spec file.
646 void BuildSpec16File( FILE *outfile, DLLSPEC *spec )
648 ORDDEF **type, **typelist;
649 int i, nFuncs, nTypes;
650 int code_offset, data_offset, res_size;
651 unsigned char *data;
652 char constructor[100], destructor[100];
653 #ifdef __i386__
654 unsigned short code_selector = get_cs();
655 #endif
657 /* File header */
659 output_file_header( outfile );
660 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
661 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
662 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
663 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
665 data = (unsigned char *)xmalloc( 0x10000 );
666 memset( data, 0, 16 );
667 data_offset = 16;
669 if (!spec->dll_name) /* set default name from file name */
671 char *p;
672 spec->dll_name = xstrdup( spec->file_name );
673 if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
676 output_stub_funcs( outfile, spec );
678 /* Build sorted list of all argument types, without duplicates */
680 typelist = (ORDDEF **)calloc( spec->limit+1, sizeof(ORDDEF *) );
682 for (i = nFuncs = 0; i <= spec->limit; i++)
684 ORDDEF *odp = spec->ordinals[i];
685 if (!odp) continue;
686 switch (odp->type)
688 case TYPE_CDECL:
689 case TYPE_PASCAL:
690 case TYPE_VARARGS:
691 case TYPE_STUB:
692 typelist[nFuncs++] = odp;
694 default:
695 break;
699 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
701 i = nTypes = 0;
702 while ( i < nFuncs )
704 typelist[nTypes++] = typelist[i++];
705 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
706 i++;
709 /* Output CallFrom16 routines needed by this .spec file */
710 #ifdef __i386__
711 for ( i = 0; i < nTypes; i++ )
713 char profile[101];
715 strcpy( profile, get_function_name( typelist[i] ));
716 BuildCallFrom16Func( outfile, profile, spec->file_name );
718 #endif
720 /* Output the DLL functions prototypes */
722 for (i = 0; i <= spec->limit; i++)
724 ORDDEF *odp = spec->ordinals[i];
725 if (!odp) continue;
726 switch(odp->type)
728 case TYPE_CDECL:
729 case TYPE_PASCAL:
730 case TYPE_VARARGS:
731 fprintf( outfile, "extern void %s();\n", odp->link_name );
732 break;
733 default:
734 break;
738 /* Output code segment */
740 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
741 fprintf( outfile, "\nstatic struct code_segment\n{\n" );
742 fprintf( outfile, " struct {\n" );
743 #ifdef __i386__
744 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
745 fprintf( outfile, " void *relay;\n" );
746 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
747 fprintf( outfile, " void *glue;\n" );
748 fprintf( outfile, " unsigned short flatcs;\n" );
749 #endif
750 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
751 fprintf( outfile, " unsigned short args;\n" );
752 fprintf( outfile, " unsigned int arg_types[2];\n" );
753 fprintf( outfile, " } call[%d];\n", nTypes );
754 fprintf( outfile, " struct {\n" );
755 #ifdef __i386__
756 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
757 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
758 #endif
759 fprintf( outfile, " void (*target)();\n" );
760 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
761 fprintf( outfile, " short callfrom16;\n" );
762 fprintf( outfile, " } entry[%d];\n", nFuncs );
763 fprintf( outfile, "} code_segment =\n{\n {\n" );
765 code_offset = 0;
767 for ( i = 0; i < nTypes; i++ )
769 char profile[101], *arg;
770 unsigned int arg_types[2];
771 int j, argsize = 0;
773 strcpy( profile, get_function_name( typelist[i] ));
774 if ( typelist[i]->type == TYPE_PASCAL )
775 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
776 switch ( *arg )
778 case 'w': /* word */
779 case 's': /* s_word */
780 argsize += 2;
781 break;
782 case 'l': /* long or segmented pointer */
783 case 'T': /* segmented pointer to null-terminated string */
784 case 'p': /* linear pointer */
785 case 't': /* linear pointer to null-terminated string */
786 argsize += 4;
787 break;
790 /* build the arg types bit fields */
791 arg_types[0] = arg_types[1] = 0;
792 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
794 int type = 0;
795 switch(typelist[i]->u.func.arg_types[j])
797 case 'w': type = ARG_WORD; break;
798 case 's': type = ARG_SWORD; break;
799 case 'l': type = ARG_LONG; break;
800 case 'p': type = ARG_PTR; break;
801 case 't': type = ARG_STR; break;
802 case 'T': type = ARG_SEGSTR; break;
804 arg_types[j / 10] |= type << (3 * (j % 10));
806 if (typelist[i]->flags & FLAG_REGISTER) arg_types[0] |= ARG_REGISTER;
807 if (typelist[i]->flags & FLAG_RET16) arg_types[0] |= ARG_RET16;
809 #ifdef __i386__
810 fprintf( outfile, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
811 make_c_identifier(spec->file_name), profile,
812 (typelist[i]->flags & FLAG_REGISTER) ? "regs":
813 (typelist[i]->flags & FLAG_RET16) ? "word" : "long" );
814 if (argsize)
815 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
816 code_selector, argsize, arg_types[0], arg_types[1] );
817 else
818 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
819 code_selector, arg_types[0], arg_types[1] );
820 #else
821 if (argsize)
822 fprintf( outfile, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
823 argsize, arg_types[0], arg_types[1] );
824 else
825 fprintf( outfile, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
826 arg_types[0], arg_types[1] );
827 #endif
828 code_offset += sizeof(CALLFROM16);
830 fprintf( outfile, " },\n {\n" );
832 for (i = 0; i <= spec->limit; i++)
834 ORDDEF *odp = spec->ordinals[i];
835 if (!odp) continue;
836 switch (odp->type)
838 case TYPE_ABS:
839 odp->offset = LOWORD(odp->u.abs.value);
840 break;
842 case TYPE_VARIABLE:
843 odp->offset = data_offset;
844 data_offset += StoreVariableCode( data + data_offset, 4, odp);
845 break;
847 case TYPE_CDECL:
848 case TYPE_PASCAL:
849 case TYPE_VARARGS:
850 case TYPE_STUB:
851 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
852 assert( type );
854 fprintf( outfile, " /* %s.%d */ ", spec->dll_name, i );
855 #ifdef __i386__
856 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
857 #else
858 fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
859 #endif
860 odp->link_name,
861 (type-typelist)*sizeof(CALLFROM16) -
862 (code_offset + sizeof(ENTRYPOINT16)),
863 get_function_name( odp ) );
865 odp->offset = code_offset;
866 code_offset += sizeof(ENTRYPOINT16);
867 break;
869 default:
870 fprintf(stderr,"build: function type %d not available for Win16\n",
871 odp->type);
872 exit(1);
876 fprintf( outfile, " }\n};\n" );
878 /* Build the module */
880 output_module_data( outfile, code_offset, data, data_offset, spec );
881 res_size = output_res16_data( outfile, spec );
883 /* Output the DLL descriptor */
885 fprintf( outfile, "#include \"poppack.h\"\n\n" );
887 fprintf( outfile, "static const struct dll_descriptor\n{\n" );
888 fprintf( outfile, " const struct module_data *module;\n" );
889 fprintf( outfile, " struct code_segment *code_start;\n" );
890 fprintf( outfile, " const unsigned char *rsrc;\n" );
891 fprintf( outfile, "} descriptor =\n{\n" );
892 fprintf( outfile, " &module,\n" );
893 fprintf( outfile, " &code_segment,\n" );
894 fprintf( outfile, " %s\n", res_size ? "resource_data" : "0" );
895 fprintf( outfile, "};\n" );
897 /* Output the DLL constructor */
899 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(spec->file_name) );
900 sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(spec->file_name) );
901 output_dll_init( outfile, constructor, destructor );
903 fprintf( outfile,
904 "void %s(void)\n"
905 "{\n"
906 " extern void __wine_dll_register_16( const struct dll_descriptor *descr, const char *file_name );\n"
907 " __wine_dll_register_16( &descriptor, \"%s\" );\n"
908 "}\n", constructor, spec->file_name );
909 fprintf( outfile,
910 "void %s(void)\n"
911 "{\n"
912 " extern void __wine_dll_unregister_16( const struct dll_descriptor *descr );\n"
913 " __wine_dll_unregister_16( &descriptor );\n"
914 "}\n", destructor );