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