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