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