Prevent crash when no URL is specified.
[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/exception.h"
32 #include "stackframe.h"
33 #include "builtin16.h"
34 #include "module.h"
35
36 #include "build.h"
37
38
39 /*******************************************************************
40  *         get_cs
41  */
42 #ifdef __i386__
43 static inline unsigned short get_cs(void)
44 {
45     unsigned short res;
46 #ifdef __GNUC__
47     __asm__("movw %%cs,%w0" : "=r"(res));
48 #elif defined(_MSC_VER)
49     __asm { mov res, cs }
50 #else
51     res = 0;
52 #endif
53     return res;
54 }
55 #endif /* __i386__ */
56
57
58 /*******************************************************************
59  *         output_file_header
60  *
61  * Output a file header with the common declarations we need.
62  */
63 static void output_file_header( FILE *outfile )
64 {
65     output_standard_file_header( outfile );
66     fprintf( outfile, "extern struct\n{\n" );
67     fprintf( outfile, "  void *base[8192];\n" );
68     fprintf( outfile, "  unsigned long limit[8192];\n" );
69     fprintf( outfile, "  unsigned char flags[8192];\n" );
70     fprintf( outfile, "} wine_ldt_copy;\n\n" );
71 #ifdef __i386__
72     fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
73 #else
74     fprintf( outfile, "#define __stdcall\n\n" );
75 #endif
76 }
77
78
79 /*******************************************************************
80  *         StoreVariableCode
81  *
82  * Store a list of ints into a byte array.
83  */
84 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
85 {
86     int i;
87
88     switch(size)
89     {
90     case 1:
91         for (i = 0; i < odp->u.var.n_values; i++)
92             buffer[i] = odp->u.var.values[i];
93         break;
94     case 2:
95         for (i = 0; i < odp->u.var.n_values; i++)
96             ((unsigned short *)buffer)[i] = odp->u.var.values[i];
97         break;
98     case 4:
99         for (i = 0; i < odp->u.var.n_values; i++)
100             ((unsigned int *)buffer)[i] = odp->u.var.values[i];
101         break;
102     }
103     return odp->u.var.n_values * size;
104 }
105
106
107 /*******************************************************************
108  *         BuildModule16
109  *
110  * Build the in-memory representation of a 16-bit NE module, and dump it
111  * as a byte stream into the assembly code.
112  */
113 static int BuildModule16( FILE *outfile, int max_code_offset,
114                           int max_data_offset, DLLSPEC *spec )
115 {
116     int i;
117     char *buffer;
118     NE_MODULE *pModule;
119     SEGTABLEENTRY *pSegment;
120     OFSTRUCT *pFileInfo;
121     BYTE *pstr;
122     ET_BUNDLE *bundle = 0;
123     ET_ENTRY entry;
124
125     /*   Module layout:
126      * NE_MODULE       Module
127      * OFSTRUCT        File information
128      * SEGTABLEENTRY   Segment 1 (code)
129      * SEGTABLEENTRY   Segment 2 (data)
130      * WORD[2]         Resource table (empty)
131      * BYTE[2]         Imported names (empty)
132      * BYTE[n]         Resident names table
133      * BYTE[n]         Entry table
134      */
135
136     buffer = xmalloc( 0x10000 );
137     memset( buffer, 0, 0x10000 );
138
139     pModule = (NE_MODULE *)buffer;
140     pModule->magic = IMAGE_OS2_SIGNATURE;
141     pModule->count = 1;
142     pModule->next = 0;
143     pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
144     pModule->dgroup = 2;
145     pModule->heap_size = spec->heap_size;
146     pModule->stack_size = 0;
147     pModule->ip = 0;
148     pModule->cs = 0;
149     pModule->sp = 0;
150     pModule->ss = 0;
151     pModule->seg_count = 2;
152     pModule->modref_count = 0;
153     pModule->nrname_size = 0;
154     pModule->modref_table = 0;
155     pModule->nrname_fpos = 0;
156     pModule->moveable_entries = 0;
157     pModule->alignment = 0;
158     pModule->truetype = 0;
159     pModule->os_flags = NE_OSFLAGS_WINDOWS;
160     pModule->misc_flags = 0;
161     pModule->dlls_to_init  = 0;
162     pModule->nrname_handle = 0;
163     pModule->min_swap_area = 0;
164     pModule->expected_version = 0;
165     pModule->module32 = 0;
166     pModule->self = 0;
167     pModule->self_loading_sel = 0;
168
169       /* File information */
170
171     pFileInfo = (OFSTRUCT *)(pModule + 1);
172     pModule->fileinfo = (int)pFileInfo - (int)pModule;
173     memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
174     pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
175                         + strlen(spec->file_name);
176     strcpy( pFileInfo->szPathName, spec->file_name );
177     pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
178
179       /* Segment table */
180
181     pstr = (char *)(((long)pstr + 3) & ~3);
182     pSegment = (SEGTABLEENTRY *)pstr;
183     pModule->seg_table = (int)pSegment - (int)pModule;
184     pSegment->filepos = 0;
185     pSegment->size = max_code_offset;
186     pSegment->flags = 0;
187     pSegment->minsize = max_code_offset;
188     pSegment->hSeg = 0;
189     pSegment++;
190
191     pModule->dgroup_entry = (int)pSegment - (int)pModule;
192     pSegment->filepos = 0;
193     pSegment->size = max_data_offset;
194     pSegment->flags = NE_SEGFLAGS_DATA;
195     pSegment->minsize = max_data_offset;
196     pSegment->hSeg = 0;
197     pSegment++;
198
199       /* Resource table */
200
201     pstr = (char *)pSegment;
202     pstr = (char *)(((long)pstr + 3) & ~3);
203     pModule->res_table = (int)pstr - (int)pModule;
204     pstr += output_res16_directory( pstr, spec );
205
206       /* Imported names table */
207
208     pstr = (char *)(((long)pstr + 3) & ~3);
209     pModule->import_table = (int)pstr - (int)pModule;
210     *pstr++ = 0;
211     *pstr++ = 0;
212
213       /* Resident names table */
214
215     pstr = (char *)(((long)pstr + 3) & ~3);
216     pModule->name_table = (int)pstr - (int)pModule;
217     /* First entry is module name */
218     *pstr = strlen( spec->dll_name );
219     strcpy( pstr + 1, spec->dll_name );
220     strupper( pstr + 1 );
221     pstr += *pstr + 1;
222     *pstr++ = 0;
223     *pstr++ = 0;
224     /* Store all ordinals */
225     for (i = 1; i <= spec->limit; i++)
226     {
227         ORDDEF *odp = spec->ordinals[i];
228         WORD ord = i;
229         if (!odp || !odp->name[0]) continue;
230         *pstr = strlen( odp->name );
231         strcpy( pstr + 1, odp->name );
232         strupper( pstr + 1 );
233         pstr += *pstr + 1;
234         memcpy( pstr, &ord, sizeof(WORD) );
235         pstr += sizeof(WORD);
236     }
237     *pstr++ = 0;
238
239       /* Entry table */
240
241     pstr = (char *)(((long)pstr + 3) & ~3);
242     pModule->entry_table = (int)pstr - (int)pModule;
243     for (i = 1; i <= spec->limit; i++)
244     {
245         int selector = 0;
246         ORDDEF *odp = spec->ordinals[i];
247         if (!odp) continue;
248
249         switch (odp->type)
250         {
251         case TYPE_CDECL:
252         case TYPE_PASCAL:
253         case TYPE_VARARGS:
254         case TYPE_STUB:
255             selector = 1;  /* Code selector */
256             break;
257
258         case TYPE_VARIABLE:
259             selector = 2;  /* Data selector */
260             break;
261
262         case TYPE_ABS:
263             selector = 0xfe;  /* Constant selector */
264             break;
265
266         default:
267             selector = 0;  /* Invalid selector */
268             break;
269         }
270
271         if ( !selector )
272            continue;
273
274         if ( bundle && bundle->last+1 == i )
275             bundle->last++;
276         else
277         {
278             pstr = (char *)(((long)pstr + 1) & ~1);
279             if ( bundle )
280                 bundle->next = (char *)pstr - (char *)pModule;
281
282             bundle = (ET_BUNDLE *)pstr;
283             bundle->first = i-1;
284             bundle->last = i;
285             bundle->next = 0;
286             pstr += sizeof(ET_BUNDLE);
287         }
288
289         /* FIXME: is this really correct ?? */
290         entry.type = 0xff;  /* movable */
291         entry.flags = 3; /* exported & public data */
292         entry.segnum = selector;
293         entry.offs = odp->offset;
294         memcpy( pstr, &entry, sizeof(ET_ENTRY) );
295         pstr += sizeof(ET_ENTRY);
296     }
297     *pstr++ = 0;
298
299       /* Dump the module content */
300
301     pstr = (char *)(((long)pstr + 3) & ~3);
302     dump_bytes( outfile, (char *)pModule, (int)pstr - (int)pModule, "Module", 0 );
303     return (int)pstr - (int)pModule;
304 }
305
306
307 #ifdef __i386__
308 /*******************************************************************
309  *         BuildCallFrom16Func
310  *
311  * Build a 16-bit-to-Wine callback glue function.
312  *
313  * The generated routines are intended to be used as argument conversion
314  * routines to be called by the CallFrom16... core. Thus, the prototypes of
315  * the generated routines are (see also CallFrom16):
316  *
317  *  extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
318  *  extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
319  *  extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
320  *                                                   CONTEXT86 *context );
321  *
322  * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
323  * and each 'x' is an argument  ('w'=word, 's'=signed word, 'l'=long,
324  * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
325  * 'T'=segmented pointer to null-terminated string).
326  *
327  * The generated routines fetch the arguments from the 16-bit stack (pointed
328  * to by 'args'); the offsets of the single argument values are computed
329  * according to the calling convention and the argument types.  Then, the
330  * 32-bit entry point is called with these arguments.
331  *
332  * For register functions, the arguments (if present) are converted just
333  * the same as for normal functions, but in addition the CONTEXT86 pointer
334  * filled with the current register values is passed to the 32-bit routine.
335  */
336 static void BuildCallFrom16Func( FILE *outfile, const char *profile, const char *prefix )
337 {
338     int i, pos, argsize = 0;
339     int short_ret = 0;
340     int reg_func = 0;
341     int usecdecl = 0;
342     int varargs = 0;
343     const char *args = profile + 7;
344     const char *ret_type;
345
346     /* Parse function type */
347
348     if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
349     else if (!strncmp( "v_", profile, 2 )) varargs = usecdecl = 1;
350     else if (strncmp( "p_", profile, 2 ))
351     {
352         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
353         return;
354     }
355
356     if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
357     else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
358     else if (strncmp( "long_", profile + 2, 5 ))
359     {
360         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
361         return;
362     }
363
364     for ( i = 0; args[i]; i++ )
365         switch ( args[i] )
366         {
367         case 'w':  /* word */
368         case 's':  /* s_word */
369             argsize += 2;
370             break;
371         case 'l':  /* long or segmented pointer */
372         case 'T':  /* segmented pointer to null-terminated string */
373         case 'p':  /* linear pointer */
374         case 't':  /* linear pointer to null-terminated string */
375             argsize += 4;
376             break;
377         }
378
379     ret_type = reg_func? "void" : short_ret ? "unsigned short" : "unsigned int";
380
381     fprintf( outfile, "typedef %s (%s*proc_%s_t)( ",
382              ret_type, usecdecl ? "" : "__stdcall ", profile );
383     args = profile + 7;
384     for ( i = 0; args[i]; i++ )
385     {
386         if ( i ) fprintf( outfile, ", " );
387         switch (args[i])
388         {
389         case 'w':           fprintf( outfile, "unsigned short" ); break;
390         case 's':           fprintf( outfile, "short" ); break;
391         case 'l': case 'T': fprintf( outfile, "unsigned int" ); break;
392         case 'p': case 't': fprintf( outfile, "void *" ); break;
393         }
394     }
395     if (reg_func || varargs)
396         fprintf( outfile, "%svoid *", i? ", " : "" );
397     else if ( !i )
398         fprintf( outfile, "void" );
399     fprintf( outfile, " );\n" );
400
401     fprintf( outfile, "static %s __stdcall __wine_%s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
402              ret_type, make_c_identifier(prefix), profile, profile,
403              reg_func? ", void *context" : "" );
404
405     fprintf( outfile, "{\n    %sproc(\n", reg_func ? "" : "return " );
406     args = profile + 7;
407     pos = !usecdecl? argsize : 0;
408     for ( i = 0; args[i]; i++ )
409     {
410         if ( i ) fprintf( outfile, ",\n" );
411         fprintf( outfile, "        " );
412         switch (args[i])
413         {
414         case 'w':  /* word */
415             if ( !usecdecl ) pos -= 2;
416             fprintf( outfile, "*(unsigned short *)(args+%d)", pos );
417             if (  usecdecl ) pos += 2;
418             break;
419
420         case 's':  /* s_word */
421             if ( !usecdecl ) pos -= 2;
422             fprintf( outfile, "*(short *)(args+%d)", pos );
423             if (  usecdecl ) pos += 2;
424             break;
425
426         case 'l':  /* long or segmented pointer */
427         case 'T':  /* segmented pointer to null-terminated string */
428             if ( !usecdecl ) pos -= 4;
429             fprintf( outfile, "*(unsigned int *)(args+%d)", pos );
430             if (  usecdecl ) pos += 4;
431             break;
432
433         case 'p':  /* linear pointer */
434         case 't':  /* linear pointer to null-terminated string */
435             if ( !usecdecl ) pos -= 4;
436             fprintf( outfile, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
437                      pos + 2, pos );
438             if (  usecdecl ) pos += 4;
439             break;
440
441         default:
442             fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
443         }
444     }
445     if ( reg_func )
446         fprintf( outfile, "%s        context", i? ",\n" : "" );
447     else if (varargs)
448         fprintf( outfile, "%s        args + %d", i? ",\n" : "", argsize );
449     fprintf( outfile, " );\n}\n\n" );
450 }
451 #endif
452
453
454 /*******************************************************************
455  *         get_function_name
456  */
457 static const char *get_function_name( const ORDDEF *odp )
458 {
459     static char buffer[80];
460
461     sprintf( buffer, "%s_%s_%s",
462              (odp->type == TYPE_PASCAL) ? "p" :
463              (odp->type == TYPE_VARARGS) ? "v" : "c",
464              (odp->flags & FLAG_REGISTER) ? "regs" :
465              (odp->flags & FLAG_RET16) ? "word" : "long",
466              odp->u.func.arg_types );
467     return buffer;
468 }
469
470
471 /*******************************************************************
472  *         Spec16TypeCompare
473  */
474 static int Spec16TypeCompare( const void *e1, const void *e2 )
475 {
476     const ORDDEF *odp1 = *(const ORDDEF * const *)e1;
477     const ORDDEF *odp2 = *(const ORDDEF * const *)e2;
478     int retval;
479     int type1 = odp1->type;
480     int type2 = odp2->type;
481
482     if (type1 == TYPE_STUB) type1 = TYPE_CDECL;
483     if (type2 == TYPE_STUB) type2 = TYPE_CDECL;
484
485     if ((retval = type1 - type2) != 0) return retval;
486
487     type1 = odp1->flags & (FLAG_RET16|FLAG_REGISTER);
488     type2 = odp2->flags & (FLAG_RET16|FLAG_REGISTER);
489
490     if ((retval = type1 - type2) != 0) return retval;
491
492     return strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
493 }
494
495
496 /*******************************************************************
497  *         output_stub_funcs
498  *
499  * Output the functions for stub entry points
500 */
501 static void output_stub_funcs( FILE *outfile, const DLLSPEC *spec )
502 {
503     int i;
504     char *p;
505
506     for (i = 0; i <= spec->limit; i++)
507     {
508         ORDDEF *odp = spec->ordinals[i];
509         if (!odp || odp->type != TYPE_STUB) continue;
510         fprintf( outfile, "#ifdef __GNUC__\n" );
511         fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
512         fprintf( outfile, "#endif\n" );
513         fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
514         fprintf( outfile, "  struct exc_record {\n" );
515         fprintf( outfile, "    unsigned int code, flags;\n" );
516         fprintf( outfile, "    void *rec, *addr;\n" );
517         fprintf( outfile, "    unsigned int params;\n" );
518         fprintf( outfile, "    const void *info[15];\n" );
519         fprintf( outfile, "  } rec;\n\n" );
520         fprintf( outfile, "  extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
521         fprintf( outfile, "  rec.code    = 0x%08x;\n", EXCEPTION_WINE_STUB );
522         fprintf( outfile, "  rec.flags   = %d;\n", EH_NONCONTINUABLE );
523         fprintf( outfile, "  rec.rec     = 0;\n" );
524         fprintf( outfile, "  rec.params  = 2;\n" );
525         fprintf( outfile, "  rec.info[0] = \"%s\";\n", spec->file_name );
526         fprintf( outfile, "  rec.info[1] = func;\n" );
527         fprintf( outfile, "#ifdef __GNUC__\n" );
528         fprintf( outfile, "  rec.addr = __builtin_return_address(1);\n" );
529         fprintf( outfile, "#else\n" );
530         fprintf( outfile, "  rec.addr = 0;\n" );
531         fprintf( outfile, "#endif\n" );
532         fprintf( outfile, "  for (;;) RtlRaiseException( &rec );\n}\n\n" );
533         break;
534     }
535     for (i = 0; i <= spec->limit; i++)
536     {
537         ORDDEF *odp = spec->ordinals[i];
538         if (!odp || odp->type != TYPE_STUB) continue;
539         odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
540         strcpy( odp->link_name, "__wine_stub_" );
541         strcat( odp->link_name, odp->name );
542         for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
543         fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
544                  odp->link_name, odp->name );
545     }
546 }
547
548
549 /*******************************************************************
550  *         BuildSpec16File
551  *
552  * Build a Win16 assembly file from a spec file.
553  */
554 void BuildSpec16File( FILE *outfile, DLLSPEC *spec )
555 {
556     ORDDEF **type, **typelist;
557     int i, nFuncs, nTypes;
558     int code_offset, data_offset, module_size, res_size;
559     unsigned char *data;
560     char constructor[100], destructor[100];
561 #ifdef __i386__
562     unsigned short code_selector = get_cs();
563 #endif
564
565     /* File header */
566
567     output_file_header( outfile );
568     fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
569     fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
570     fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
571     fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
572
573     data = (unsigned char *)xmalloc( 0x10000 );
574     memset( data, 0, 16 );
575     data_offset = 16;
576
577     if (!spec->dll_name)  /* set default name from file name */
578     {
579         char *p;
580         spec->dll_name = xstrdup( spec->file_name );
581         if ((p = strrchr( spec->dll_name, '.' ))) *p = 0;
582     }
583
584     output_stub_funcs( outfile, spec );
585
586     /* Build sorted list of all argument types, without duplicates */
587
588     typelist = (ORDDEF **)calloc( spec->limit+1, sizeof(ORDDEF *) );
589
590     for (i = nFuncs = 0; i <= spec->limit; i++)
591     {
592         ORDDEF *odp = spec->ordinals[i];
593         if (!odp) continue;
594         switch (odp->type)
595         {
596           case TYPE_CDECL:
597           case TYPE_PASCAL:
598           case TYPE_VARARGS:
599           case TYPE_STUB:
600             typelist[nFuncs++] = odp;
601
602           default:
603             break;
604         }
605     }
606
607     qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
608
609     i = nTypes = 0;
610     while ( i < nFuncs )
611     {
612         typelist[nTypes++] = typelist[i++];
613         while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
614             i++;
615     }
616
617     /* Output CallFrom16 routines needed by this .spec file */
618 #ifdef __i386__
619     for ( i = 0; i < nTypes; i++ )
620     {
621         char profile[101];
622
623         strcpy( profile, get_function_name( typelist[i] ));
624         BuildCallFrom16Func( outfile, profile, spec->file_name );
625     }
626 #endif
627
628     /* Output the DLL functions prototypes */
629
630     for (i = 0; i <= spec->limit; i++)
631     {
632         ORDDEF *odp = spec->ordinals[i];
633         if (!odp) continue;
634         switch(odp->type)
635         {
636         case TYPE_CDECL:
637         case TYPE_PASCAL:
638         case TYPE_VARARGS:
639             fprintf( outfile, "extern void %s();\n", odp->link_name );
640             break;
641         default:
642             break;
643         }
644     }
645
646     /* Output code segment */
647
648     fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
649     fprintf( outfile, "\nstatic struct code_segment\n{\n" );
650     fprintf( outfile, "  struct {\n" );
651 #ifdef __i386__
652     fprintf( outfile, "    unsigned char pushl;\n" );      /* pushl $relay */
653     fprintf( outfile, "    void *relay;\n" );
654     fprintf( outfile, "    unsigned char lcall;\n" );      /* lcall __FLATCS__:glue */
655     fprintf( outfile, "    void *glue;\n" );
656     fprintf( outfile, "    unsigned short flatcs;\n" );
657 #endif
658     fprintf( outfile, "    unsigned short lret;\n" );      /* lret $args */
659     fprintf( outfile, "    unsigned short args;\n" );
660     fprintf( outfile, "    unsigned int arg_types[2];\n" );
661     fprintf( outfile, "  } call[%d];\n", nTypes );
662     fprintf( outfile, "  struct {\n" );
663 #ifdef __i386__
664     fprintf( outfile, "    unsigned short pushw_bp;\n" );  /* pushw %bp */
665     fprintf( outfile, "    unsigned char pushl;\n" );      /* pushl $target */
666 #endif
667     fprintf( outfile, "    void (*target)();\n" );
668     fprintf( outfile, "    unsigned short call;\n" );      /* call CALLFROM16 */
669     fprintf( outfile, "    short callfrom16;\n" );
670     fprintf( outfile, "  } entry[%d];\n", nFuncs );
671     fprintf( outfile, "} code_segment =\n{\n  {\n" );
672
673     code_offset = 0;
674
675     for ( i = 0; i < nTypes; i++ )
676     {
677         char profile[101], *arg;
678         unsigned int arg_types[2];
679         int j, argsize = 0;
680
681         strcpy( profile, get_function_name( typelist[i] ));
682         if ( typelist[i]->type == TYPE_PASCAL )
683             for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
684                 switch ( *arg )
685                 {
686                 case 'w':  /* word */
687                 case 's':  /* s_word */
688                     argsize += 2;
689                     break;
690                 case 'l':  /* long or segmented pointer */
691                 case 'T':  /* segmented pointer to null-terminated string */
692                 case 'p':  /* linear pointer */
693                 case 't':  /* linear pointer to null-terminated string */
694                     argsize += 4;
695                     break;
696                 }
697
698         /* build the arg types bit fields */
699         arg_types[0] = arg_types[1] = 0;
700         for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
701         {
702             int type = 0;
703             switch(typelist[i]->u.func.arg_types[j])
704             {
705             case 'w': type = ARG_WORD; break;
706             case 's': type = ARG_SWORD; break;
707             case 'l': type = ARG_LONG; break;
708             case 'p': type = ARG_PTR; break;
709             case 't': type = ARG_STR; break;
710             case 'T': type = ARG_SEGSTR; break;
711             }
712             arg_types[j / 10] |= type << (3 * (j % 10));
713         }
714         if (typelist[i]->flags & FLAG_REGISTER) arg_types[0] |= ARG_REGISTER;
715         if (typelist[i]->flags & FLAG_RET16) arg_types[0] |= ARG_RET16;
716
717 #ifdef __i386__
718         fprintf( outfile, "    { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
719                  make_c_identifier(spec->file_name), profile,
720                  (typelist[i]->flags & FLAG_REGISTER) ? "regs":
721                  (typelist[i]->flags & FLAG_RET16) ? "word" : "long" );
722         if (argsize)
723             fprintf( outfile, "      0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
724                      code_selector, argsize, arg_types[0], arg_types[1] );
725         else
726             fprintf( outfile, "      0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
727                      code_selector, arg_types[0], arg_types[1] );
728 #else
729         if (argsize)
730             fprintf( outfile, "    { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
731                      argsize, arg_types[0], arg_types[1] );
732         else
733             fprintf( outfile, "     { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
734                      arg_types[0], arg_types[1] );
735 #endif
736         code_offset += sizeof(CALLFROM16);
737     }
738     fprintf( outfile, "  },\n  {\n" );
739
740     for (i = 0; i <= spec->limit; i++)
741     {
742         ORDDEF *odp = spec->ordinals[i];
743         if (!odp) continue;
744         switch (odp->type)
745         {
746           case TYPE_ABS:
747             odp->offset = LOWORD(odp->u.abs.value);
748             break;
749
750           case TYPE_VARIABLE:
751             odp->offset = data_offset;
752             data_offset += StoreVariableCode( data + data_offset, 4, odp);
753             break;
754
755           case TYPE_CDECL:
756           case TYPE_PASCAL:
757           case TYPE_VARARGS:
758           case TYPE_STUB:
759             type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
760             assert( type );
761
762             fprintf( outfile, "    /* %s.%d */ ", spec->dll_name, i );
763 #ifdef __i386__
764             fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d  /* %s */ },\n",
765 #else
766             fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
767 #endif
768                      odp->link_name,
769                      (type-typelist)*sizeof(CALLFROM16) -
770                      (code_offset + sizeof(ENTRYPOINT16)),
771                      get_function_name( odp ) );
772
773             odp->offset = code_offset;
774             code_offset += sizeof(ENTRYPOINT16);
775             break;
776
777           default:
778             fprintf(stderr,"build: function type %d not available for Win16\n",
779                     odp->type);
780             exit(1);
781         }
782     }
783
784     fprintf( outfile, "    }\n};\n" );
785
786     /* Output data segment */
787
788     dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
789
790     /* Build the module */
791
792     module_size = BuildModule16( outfile, code_offset, data_offset, spec );
793     res_size = output_res16_data( outfile, spec );
794
795     /* Output the DLL descriptor */
796
797     fprintf( outfile, "#include \"poppack.h\"\n\n" );
798
799     fprintf( outfile, "static const struct dll_descriptor\n{\n" );
800     fprintf( outfile, "    unsigned char *module_start;\n" );
801     fprintf( outfile, "    int module_size;\n" );
802     fprintf( outfile, "    struct code_segment *code_start;\n" );
803     fprintf( outfile, "    unsigned char *data_start;\n" );
804     fprintf( outfile, "    const char *owner;\n" );
805     fprintf( outfile, "    const unsigned char *rsrc;\n" );
806     fprintf( outfile, "} descriptor =\n{\n" );
807     fprintf( outfile, "    Module,\n" );
808     fprintf( outfile, "    sizeof(Module),\n" );
809     fprintf( outfile, "    &code_segment,\n" );
810     fprintf( outfile, "    Data_Segment,\n" );
811     fprintf( outfile, "    \"%s\",\n", spec->owner_name );
812     fprintf( outfile, "    %s\n", res_size ? "resource_data" : "0" );
813     fprintf( outfile, "};\n" );
814
815     /* Output the DLL constructor */
816
817     sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(spec->file_name) );
818     sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(spec->file_name) );
819     output_dll_init( outfile, constructor, destructor );
820
821     fprintf( outfile,
822              "void %s(void)\n"
823              "{\n"
824              "    extern void __wine_register_dll_16( const struct dll_descriptor *descr );\n"
825              "    __wine_register_dll_16( &descriptor );\n"
826              "}\n", constructor );
827     fprintf( outfile,
828              "void %s(void)\n"
829              "{\n"
830              "    extern void __wine_unregister_dll_16( const struct dll_descriptor *descr );\n"
831              "    __wine_unregister_dll_16( &descriptor );\n"
832              "}\n", destructor );
833 }