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