Added support in winebuild for resolving function imports (-sym option).
[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 "builtin16.h"
15 #include "module.h"
16 #include "neexe.h"
17 #include "stackframe.h"
18
19 #include "build.h"
20
21 #ifdef __i386__
22 extern unsigned short __get_cs(void);
23 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" );
24 #else
25 static inline unsigned short __get_cs(void) { return 0; }
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 = 0;
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
88     pModule = (NE_MODULE *)buffer;
89     memset( pModule, 0, sizeof(*pModule) );
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 #ifdef __i386__  /* FIXME: Alignment problems! */
130
131       /* Segment table */
132
133     pSegment = (SEGTABLEENTRY *)pstr;
134     pModule->seg_table = (int)pSegment - (int)pModule;
135     pSegment->filepos = 0;
136     pSegment->size = max_code_offset;
137     pSegment->flags = 0;
138     pSegment->minsize = max_code_offset;
139     pSegment->hSeg = 0;
140     pSegment++;
141
142     pModule->dgroup_entry = (int)pSegment - (int)pModule;
143     pSegment->filepos = 0;
144     pSegment->size = max_data_offset;
145     pSegment->flags = NE_SEGFLAGS_DATA;
146     pSegment->minsize = max_data_offset;
147     pSegment->hSeg = 0;
148     pSegment++;
149
150       /* Resource table */
151
152     pstr = (char *)pSegment;
153     pModule->res_table = (int)pstr - (int)pModule;
154     pstr += output_res16_directory( pstr );
155
156       /* Imported names table */
157
158     pModule->import_table = (int)pstr - (int)pModule;
159     *pstr++ = 0;
160     *pstr++ = 0;
161
162       /* Resident names table */
163
164     pModule->name_table = (int)pstr - (int)pModule;
165     /* First entry is module name */
166     *pstr = strlen(DLLName );
167     strcpy( pstr + 1, DLLName );
168     pstr += *pstr + 1;
169     *(WORD *)pstr = 0;
170     pstr += sizeof(WORD);
171     /* Store all ordinals */
172     for (i = 1; i <= Limit; i++)
173     {
174         ORDDEF *odp = Ordinals[i];
175         if (!odp || !odp->name[0]) continue;
176         *pstr = strlen( odp->name );
177         strcpy( pstr + 1, odp->name );
178         strupper( pstr + 1 );
179         pstr += *pstr + 1;
180         *(WORD *)pstr = i;
181         pstr += sizeof(WORD);
182     }
183     *pstr++ = 0;
184
185       /* Entry table */
186
187     pModule->entry_table = (int)pstr - (int)pModule;
188     for (i = 1; i <= Limit; i++)
189     {
190         int selector = 0;
191         ORDDEF *odp = Ordinals[i];
192         if (!odp) continue;
193
194         switch (odp->type)
195         {
196         case TYPE_CDECL:
197         case TYPE_PASCAL:
198         case TYPE_PASCAL_16:
199         case TYPE_REGISTER:
200         case TYPE_INTERRUPT:
201         case TYPE_STUB:
202             selector = 1;  /* Code selector */
203             break;
204
205         case TYPE_BYTE:
206         case TYPE_WORD:
207         case TYPE_LONG:
208             selector = 2;  /* Data selector */
209             break;
210
211         case TYPE_ABS:
212             selector = 0xfe;  /* Constant selector */
213             break;
214
215         default:
216             selector = 0;  /* Invalid selector */
217             break;
218         }
219
220         if ( !selector )
221            continue;
222
223         if ( bundle && bundle->last+1 == i )
224             bundle->last++;
225         else
226         {
227             if ( bundle )
228                 bundle->next = (char *)pstr - (char *)pModule;
229
230             bundle = (ET_BUNDLE *)pstr;
231             bundle->first = i-1;
232             bundle->last = i;
233             bundle->next = 0;
234             pstr += sizeof(ET_BUNDLE);
235         }
236
237         /* FIXME: is this really correct ?? */
238         entry = (ET_ENTRY *)pstr;
239         entry->type = 0xff;  /* movable */
240         entry->flags = 3; /* exported & public data */
241         entry->segnum = selector;
242         entry->offs = odp->offset;
243         pstr += sizeof(ET_ENTRY);
244     }
245     *pstr++ = 0;
246 #endif
247
248       /* Dump the module content */
249
250     dump_bytes( outfile, (char *)pModule, (int)pstr - (int)pModule, "Module", 0 );
251     return (int)pstr - (int)pModule;
252 }
253
254
255 /*******************************************************************
256  *         BuildCallFrom16Func
257  *
258  * Build a 16-bit-to-Wine callback glue function. 
259  *
260  * The generated routines are intended to be used as argument conversion 
261  * routines to be called by the CallFrom16... core. Thus, the prototypes of
262  * the generated routines are (see also CallFrom16):
263  *
264  *  extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
265  *  extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
266  *  extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args, 
267  *                                                   CONTEXT86 *context );
268  *  extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args, 
269  *                                                   CONTEXT86 *context );
270  *
271  * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl), 
272  * and each 'x' is an argument  ('w'=word, 's'=signed word, 'l'=long, 
273  * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
274  * 'T'=segmented pointer to null-terminated string).
275  *
276  * The generated routines fetch the arguments from the 16-bit stack (pointed
277  * to by 'args'); the offsets of the single argument values are computed 
278  * according to the calling convention and the argument types.  Then, the
279  * 32-bit entry point is called with these arguments.
280  * 
281  * For register functions, the arguments (if present) are converted just
282  * the same as for normal functions, but in addition the CONTEXT86 pointer 
283  * filled with the current register values is passed to the 32-bit routine.
284  * (An 'intr' interrupt handler routine is treated exactly like a register 
285  * routine, except that upon return, the flags word pushed onto the stack 
286  * by the interrupt is removed by the 16-bit call stub.)
287  *
288  */
289 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
290 {
291     int i, pos, argsize = 0;
292     int short_ret = 0;
293     int reg_func = 0;
294     int usecdecl = 0;
295     char *args = profile + 7;
296     char *ret_type;
297
298     /* Parse function type */
299
300     if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
301     else if (strncmp( "p_", profile, 2 ))
302     {
303         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
304         return;
305     }
306
307     if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
308     else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
309     else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
310     else if (strncmp( "long_", profile + 2, 5 ))
311     {
312         fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
313         return;
314     }
315
316     for ( i = 0; args[i]; i++ )
317         switch ( args[i] )
318         {
319         case 'w':  /* word */
320         case 's':  /* s_word */
321             argsize += 2;
322             break;
323         
324         case 'l':  /* long or segmented pointer */
325         case 'T':  /* segmented pointer to null-terminated string */
326         case 'p':  /* linear pointer */
327         case 't':  /* linear pointer to null-terminated string */
328             argsize += 4;
329             break;
330         }
331
332     ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
333
334     fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ", 
335                       ret_type, profile );
336     args = profile + 7;
337     for ( i = 0; args[i]; i++ )
338     {
339         if ( i ) fprintf( outfile, ", " );
340         switch (args[i])
341         {
342         case 'w':           fprintf( outfile, "WORD" ); break;
343         case 's':           fprintf( outfile, "INT16" ); break;
344         case 'l': case 'T': fprintf( outfile, "LONG" ); break;
345         case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
346         }
347     }
348     if ( reg_func )
349         fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
350     else if ( !i )
351         fprintf( outfile, "void" );
352     fprintf( outfile, " );\n" );
353     
354     fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
355              local? "static " : "", ret_type, prefix, profile,
356              reg_func? ", struct _CONTEXT86 *context" : "" );
357
358     fprintf( outfile, "    %s((proc_%s_t) proc) (\n",
359              reg_func? "" : "return ", profile );
360     args = profile + 7;
361     pos = !usecdecl? argsize : 0;
362     for ( i = 0; args[i]; i++ )
363     {
364         if ( i ) fprintf( outfile, ",\n" );
365         fprintf( outfile, "        " );
366         switch (args[i])
367         {
368         case 'w':  /* word */
369             if ( !usecdecl ) pos -= 2;
370             fprintf( outfile, "*(WORD *)(args+%d)", pos );
371             if (  usecdecl ) pos += 2;
372             break;
373
374         case 's':  /* s_word */
375             if ( !usecdecl ) pos -= 2;
376             fprintf( outfile, "*(INT16 *)(args+%d)", pos );
377             if (  usecdecl ) pos += 2;
378             break;
379
380         case 'l':  /* long or segmented pointer */
381         case 'T':  /* segmented pointer to null-terminated string */
382             if ( !usecdecl ) pos -= 4;
383             fprintf( outfile, "*(LONG *)(args+%d)", pos );
384             if (  usecdecl ) pos += 4;
385             break;
386
387         case 'p':  /* linear pointer */
388         case 't':  /* linear pointer to null-terminated string */
389             if ( !usecdecl ) pos -= 4;
390             fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
391             if (  usecdecl ) pos += 4;
392             break;
393
394         default:
395             fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
396         }
397     }
398     if ( reg_func )
399         fprintf( outfile, "%s        context", i? ",\n" : "" );
400     fprintf( outfile, " );\n}\n\n" );
401 }
402
403
404 /*******************************************************************
405  *         BuildCallTo16Func
406  *
407  * Build a Wine-to-16-bit callback glue function. 
408  *
409  * Prototypes for the CallTo16 functions:
410  *   extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
411  *   extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
412  * 
413  * These routines are provided solely for convenience; they simply
414  * write the arguments onto the 16-bit stack, and call the appropriate
415  * CallTo16... core routine.
416  *
417  * If you have more sophisticated argument conversion requirements than
418  * are provided by these routines, you might as well call the core 
419  * routines by yourself.
420  *
421  */
422 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
423 {
424     char *args = profile + 5;
425     int i, argsize = 0, short_ret = 0;
426
427     if (!strncmp( "word_", profile, 5 )) short_ret = 1;
428     else if (strncmp( "long_", profile, 5 ))
429     {
430         fprintf( stderr, "Invalid function name '%s'.\n", profile );
431         exit(1);
432     }
433
434     fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
435              short_ret? "WORD" : "LONG", prefix, profile );
436     args = profile + 5;
437     for ( i = 0; args[i]; i++ )
438     {
439         fprintf( outfile, ", " );
440         switch (args[i])
441         {
442         case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
443         case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
444         }
445         fprintf( outfile, " arg%d", i+1 );
446     }
447     fprintf( outfile, " )\n{\n" );
448
449     if ( argsize > 0 )
450         fprintf( outfile, "    LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
451
452     args = profile + 5;
453     for ( i = 0; args[i]; i++ )
454     {
455         switch (args[i])
456         {
457         case 'w': fprintf( outfile, "    args -= sizeof(WORD); *(WORD" ); break;
458         case 'l': fprintf( outfile, "    args -= sizeof(LONG); *(LONG" ); break;
459         default:  fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
460                                    args[i] );
461         }
462         fprintf( outfile, " *)args = arg%d;\n", i+1 );
463     }
464
465     fprintf( outfile, "    return CallTo16%s( proc, %d );\n}\n\n",
466              short_ret? "Word" : "Long", argsize );
467 }
468
469
470 /*******************************************************************
471  *         Spec16TypeCompare
472  */
473 static int Spec16TypeCompare( const void *e1, const void *e2 )
474 {
475     const ORDDEF *odp1 = *(const ORDDEF **)e1;
476     const ORDDEF *odp2 = *(const ORDDEF **)e2;
477
478     int type1 = (odp1->type == TYPE_CDECL) ? 0
479               : (odp1->type == TYPE_REGISTER) ? 3
480               : (odp1->type == TYPE_INTERRUPT) ? 4
481               : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
482
483     int type2 = (odp2->type == TYPE_CDECL) ? 0
484               : (odp2->type == TYPE_REGISTER) ? 3
485               : (odp2->type == TYPE_INTERRUPT) ? 4
486               : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
487
488     int retval = type1 - type2;
489     if ( !retval )
490         retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
491
492     return retval;
493 }
494
495
496 /*******************************************************************
497  *         BuildSpec16File
498  *
499  * Build a Win16 assembly file from a spec file.
500  */
501 void BuildSpec16File( FILE *outfile )
502 {
503     ORDDEF **type, **typelist;
504     int i, nFuncs, nTypes;
505     int code_offset, data_offset, module_size, res_size;
506     unsigned char *data;
507     unsigned short code_selector = __get_cs();
508
509     /* File header */
510
511     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
512              input_file_name );
513     fprintf( outfile, "#include \"builtin16.h\"\n\n" );
514
515     fprintf( outfile, "extern void RELAY_Unimplemented16(void);\n\n" );
516
517     data = (unsigned char *)xmalloc( 0x10000 );
518     memset( data, 0, 16 );
519     data_offset = 16;
520     strupper( DLLName );
521
522     /* Build sorted list of all argument types, without duplicates */
523
524     typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
525
526     for (i = nFuncs = 0; i <= Limit; i++)
527     {
528         ORDDEF *odp = Ordinals[i];
529         if (!odp) continue;
530         switch (odp->type)
531         {
532           case TYPE_REGISTER:
533           case TYPE_INTERRUPT:
534           case TYPE_CDECL:
535           case TYPE_PASCAL:
536           case TYPE_PASCAL_16:
537           case TYPE_STUB:
538             typelist[nFuncs++] = odp;
539
540           default:
541             break;
542         }
543     }
544
545     qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
546
547     i = nTypes = 0;
548     while ( i < nFuncs )
549     {
550         typelist[nTypes++] = typelist[i++];
551         while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
552             i++;
553     }
554
555     /* Output CallFrom16 routines needed by this .spec file */
556
557     for ( i = 0; i < nTypes; i++ )
558     {
559         char profile[101];
560
561         sprintf( profile, "%s_%s_%s",
562                  (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
563                  (typelist[i]->type == TYPE_REGISTER) ? "regs" :
564                  (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
565                  (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
566                  typelist[i]->u.func.arg_types );
567
568         BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
569     }
570
571     /* Output the DLL functions prototypes */
572
573     for (i = 0; i <= Limit; i++)
574     {
575         ORDDEF *odp = Ordinals[i];
576         if (!odp) continue;
577         switch(odp->type)
578         {
579         case TYPE_REGISTER:
580         case TYPE_INTERRUPT:
581         case TYPE_CDECL:
582         case TYPE_PASCAL:
583         case TYPE_PASCAL_16:
584             fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
585             break;
586         default:
587             break;
588         }
589     }
590
591     /* Output code segment */
592
593     fprintf( outfile, "\nstatic struct\n{\n    CALLFROM16   call[%d];\n"
594                       "    ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n  {\n",
595                       nTypes, nFuncs );
596     code_offset = 0;
597
598     for ( i = 0; i < nTypes; i++ )
599     {
600         char profile[101], *arg;
601         int argsize = 0;
602
603         sprintf( profile, "%s_%s_%s", 
604                           (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
605                           (typelist[i]->type == TYPE_REGISTER) ? "regs" :
606                           (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
607                           (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
608                           typelist[i]->u.func.arg_types );
609
610         if ( typelist[i]->type != TYPE_CDECL )
611             for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
612                 switch ( *arg )
613                 {
614                 case 'w':  /* word */
615                 case 's':  /* s_word */
616                     argsize += 2;
617                     break;
618         
619                 case 'l':  /* long or segmented pointer */
620                 case 'T':  /* segmented pointer to null-terminated string */
621                 case 'p':  /* linear pointer */
622                 case 't':  /* linear pointer to null-terminated string */
623                     argsize += 4;
624                     break;
625                 }
626
627         if ( typelist[i]->type == TYPE_INTERRUPT )
628             argsize += 2;
629
630         fprintf( outfile, "    { 0x68, %s_CallFrom16_%s, 0x9a, CallFrom16%s,\n",
631                  DLLName, profile, 
632                  (typelist[i]->type == TYPE_REGISTER 
633                   || typelist[i]->type == TYPE_INTERRUPT)? "Register":
634                  typelist[i]->type == TYPE_PASCAL_16? "Word" : "Long" );
635         if (argsize)
636             fprintf( outfile, "        0x%04x, 0x66, 0xca, %d, \"%s\" },\n",
637                      code_selector, argsize, profile );
638         else
639             fprintf( outfile, "        0x%04x, 0x66, 0xcb, 0x9090, \"%s\" },\n",
640                      code_selector, profile );
641
642         code_offset += sizeof(CALLFROM16);
643     }
644     fprintf( outfile, "  },\n  {\n" );
645
646     for (i = 0; i <= Limit; i++)
647     {
648         ORDDEF *odp = Ordinals[i];
649         if (!odp) continue;
650         switch (odp->type)
651         {
652           case TYPE_ABS:
653             odp->offset = LOWORD(odp->u.abs.value);
654             break;
655
656           case TYPE_BYTE:
657             odp->offset = data_offset;
658             data_offset += StoreVariableCode( data + data_offset, 1, odp);
659             break;
660
661           case TYPE_WORD:
662             odp->offset = data_offset;
663             data_offset += StoreVariableCode( data + data_offset, 2, odp);
664             break;
665
666           case TYPE_LONG:
667             odp->offset = data_offset;
668             data_offset += StoreVariableCode( data + data_offset, 4, odp);
669             break;
670
671           case TYPE_REGISTER:
672           case TYPE_INTERRUPT:
673           case TYPE_CDECL:
674           case TYPE_PASCAL:
675           case TYPE_PASCAL_16:
676           case TYPE_STUB:
677             type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
678             assert( type );
679
680             fprintf( outfile, "    /* %s.%d */ ", DLLName, i );
681             fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d  /* %s_%s_%s */ },\n",
682                               odp->u.func.link_name,
683                               (type-typelist)*sizeof(CALLFROM16) - 
684                               (code_offset + sizeof(ENTRYPOINT16)),
685                               (odp->type == TYPE_CDECL) ? "c" : "p",
686                               (odp->type == TYPE_REGISTER) ? "regs" :
687                               (odp->type == TYPE_INTERRUPT) ? "intr" :
688                               (odp->type == TYPE_PASCAL_16) ? "word" : "long",
689                               odp->u.func.arg_types );
690                                  
691             odp->offset = code_offset;
692             code_offset += sizeof(ENTRYPOINT16);
693             break;
694                 
695           default:
696             fprintf(stderr,"build: function type %d not available for Win16\n",
697                     odp->type);
698             exit(1);
699         }
700     }
701
702     fprintf( outfile, "    }\n};\n" );
703
704     /* Output data segment */
705
706     dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
707
708     /* Build the module */
709
710     module_size = BuildModule16( outfile, code_offset, data_offset );
711     res_size = output_res16_data( outfile );
712
713     /* Output the DLL descriptor */
714
715     fprintf( outfile, "\nstatic const BUILTIN16_DESCRIPTOR descriptor = \n{\n" );
716     fprintf( outfile, "    \"%s\",\n", DLLName );
717     fprintf( outfile, "    Module,\n" );
718     fprintf( outfile, "    sizeof(Module),\n" );
719     fprintf( outfile, "    &Code_Segment,\n" );
720     fprintf( outfile, "    Data_Segment,\n" );
721     fprintf( outfile, "    \"%s\",\n", owner_name );
722     fprintf( outfile, "    %s\n", res_size ? "resource_data" : "0" );
723     fprintf( outfile, "};\n" );
724
725     /* Output the DLL constructor */
726
727     fprintf( outfile, "#ifdef __GNUC__\n" );
728     fprintf( outfile, "static void %s_init(void) __attribute__((constructor));\n", DLLName );
729     fprintf( outfile, "#else /* defined(__GNUC__) */\n" );
730     fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
731     fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
732     fprintf( outfile, "    \"\\tcall %s_init\\n\"\n", DLLName );
733     fprintf( outfile, "    \"\\t.previous\\n\");\n" );
734     fprintf( outfile, "}\n" );
735     fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
736     fprintf( outfile, "static void %s_init(void) { BUILTIN_RegisterDLL( &descriptor ); }\n",
737              DLLName );
738 }
739
740
741 /*******************************************************************
742  *         BuildGlue
743  *
744  * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
745  */
746 void BuildGlue( FILE *outfile, FILE *infile )
747 {
748     char buffer[1024];
749
750     /* File header */
751
752     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
753              input_file_name );
754     fprintf( outfile, "#include \"builtin16.h\"\n" );
755     fprintf( outfile, "#include \"stackframe.h\"\n\n" );
756
757     fprintf( outfile, "extern WORD CALLBACK CallTo16Word( FARPROC16 target, INT nArgs );\n" );
758     fprintf( outfile, "extern LONG CALLBACK CallTo16Long( FARPROC16 target, INT nArgs );\n" );
759
760     /* Build the callback glue functions */
761
762     while (fgets( buffer, sizeof(buffer), infile ))
763     {
764         if (strstr( buffer, "### start build ###" )) break;
765     }
766     while (fgets( buffer, sizeof(buffer), infile ))
767     {
768         char *p; 
769         if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
770         {
771             char *q, *profile = p + strlen( "CallFrom16_" );
772             for (q = profile; (*q == '_') || isalpha(*q); q++ )
773                 ;
774             *q = '\0';
775             for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
776                 ;
777             if ( ++q < p ) p[-1] = '\0'; else q = "";
778             BuildCallFrom16Func( outfile, profile, q, FALSE );
779         }
780         if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
781         {
782             char *q, *profile = p + strlen( "CallTo16_" );
783             for (q = profile; (*q == '_') || isalpha(*q); q++ )
784                 ;
785             *q = '\0';
786             for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
787                 ;
788             if ( ++q < p ) p[-1] = '\0'; else q = "";
789             BuildCallTo16Func( outfile, profile, q );
790         }
791         if (strstr( buffer, "### stop build ###" )) break;
792     }
793
794     fclose( infile );
795 }