Renamed CallTo16* functions to wine_call_to_16* and export them from
[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/exception.h"
16 #include "builtin16.h"
17 #include "module.h"
18 #include "neexe.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 #else
27 static inline unsigned short __get_cs(void) { return 0; }
28 #endif /* __i386__ */
29
30
31 /*******************************************************************
32  *         StoreVariableCode
33  *
34  * Store a list of ints into a byte array.
35  */
36 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
37 {
38     int i;
39
40     switch(size)
41     {
42     case 1:
43         for (i = 0; i < odp->u.var.n_values; i++)
44             buffer[i] = odp->u.var.values[i];
45         break;
46     case 2:
47         for (i = 0; i < odp->u.var.n_values; i++)
48             ((unsigned short *)buffer)[i] = odp->u.var.values[i];
49         break;
50     case 4:
51         for (i = 0; i < odp->u.var.n_values; i++)
52             ((unsigned int *)buffer)[i] = odp->u.var.values[i];
53         break;
54     }
55     return odp->u.var.n_values * size;
56 }
57
58
59 /*******************************************************************
60  *         BuildModule16
61  *
62  * Build the in-memory representation of a 16-bit NE module, and dump it
63  * as a byte stream into the assembly code.
64  */
65 static int BuildModule16( FILE *outfile, int max_code_offset,
66                           int max_data_offset )
67 {
68     int i;
69     char *buffer;
70     NE_MODULE *pModule;
71     SEGTABLEENTRY *pSegment;
72     OFSTRUCT *pFileInfo;
73     BYTE *pstr;
74     ET_BUNDLE *bundle = 0;
75     ET_ENTRY *entry = 0;
76
77     /*   Module layout:
78      * NE_MODULE       Module
79      * OFSTRUCT        File information
80      * SEGTABLEENTRY   Segment 1 (code)
81      * SEGTABLEENTRY   Segment 2 (data)
82      * WORD[2]         Resource table (empty)
83      * BYTE[2]         Imported names (empty)
84      * BYTE[n]         Resident names table
85      * BYTE[n]         Entry table
86      */
87
88     buffer = xmalloc( 0x10000 );
89
90     pModule = (NE_MODULE *)buffer;
91     memset( pModule, 0, sizeof(*pModule) );
92     pModule->magic = IMAGE_OS2_SIGNATURE;
93     pModule->count = 1;
94     pModule->next = 0;
95     pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
96     pModule->dgroup = 2;
97     pModule->heap_size = DLLHeapSize;
98     pModule->stack_size = 0;
99     pModule->ip = 0;
100     pModule->cs = 0;
101     pModule->sp = 0;
102     pModule->ss = 0;
103     pModule->seg_count = 2;
104     pModule->modref_count = 0;
105     pModule->nrname_size = 0;
106     pModule->modref_table = 0;
107     pModule->nrname_fpos = 0;
108     pModule->moveable_entries = 0;
109     pModule->alignment = 0;
110     pModule->truetype = 0;
111     pModule->os_flags = NE_OSFLAGS_WINDOWS;
112     pModule->misc_flags = 0;
113     pModule->dlls_to_init  = 0;
114     pModule->nrname_handle = 0;
115     pModule->min_swap_area = 0;
116     pModule->expected_version = 0;
117     pModule->module32 = 0;
118     pModule->self = 0;
119     pModule->self_loading_sel = 0;
120
121       /* File information */
122
123     pFileInfo = (OFSTRUCT *)(pModule + 1);
124     pModule->fileinfo = (int)pFileInfo - (int)pModule;
125     memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
126     pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
127                         + strlen(DLLFileName);
128     strcpy( pFileInfo->szPathName, DLLFileName );
129     pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
130         
131 #ifdef __i386__  /* FIXME: Alignment problems! */
132
133       /* Segment table */
134
135     pSegment = (SEGTABLEENTRY *)pstr;
136     pModule->seg_table = (int)pSegment - (int)pModule;
137     pSegment->filepos = 0;
138     pSegment->size = max_code_offset;
139     pSegment->flags = 0;
140     pSegment->minsize = max_code_offset;
141     pSegment->hSeg = 0;
142     pSegment++;
143
144     pModule->dgroup_entry = (int)pSegment - (int)pModule;
145     pSegment->filepos = 0;
146     pSegment->size = max_data_offset;
147     pSegment->flags = NE_SEGFLAGS_DATA;
148     pSegment->minsize = max_data_offset;
149     pSegment->hSeg = 0;
150     pSegment++;
151
152       /* Resource table */
153
154     pstr = (char *)pSegment;
155     pModule->res_table = (int)pstr - (int)pModule;
156     pstr += output_res16_directory( pstr );
157
158       /* Imported names table */
159
160     pModule->import_table = (int)pstr - (int)pModule;
161     *pstr++ = 0;
162     *pstr++ = 0;
163
164       /* Resident names table */
165
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     *(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         *(WORD *)pstr = i;
183         pstr += sizeof(WORD);
184     }
185     *pstr++ = 0;
186
187       /* Entry table */
188
189     pModule->entry_table = (int)pstr - (int)pModule;
190     for (i = 1; i <= Limit; i++)
191     {
192         int selector = 0;
193         ORDDEF *odp = Ordinals[i];
194         if (!odp) continue;
195
196         switch (odp->type)
197         {
198         case TYPE_CDECL:
199         case TYPE_PASCAL:
200         case TYPE_PASCAL_16:
201         case TYPE_REGISTER:
202         case TYPE_INTERRUPT:
203         case TYPE_STUB:
204             selector = 1;  /* Code selector */
205             break;
206
207         case TYPE_VARIABLE:
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, "((char*)wine_ldt_copy.base[*(WORD*)(args+%d) >> 3] + *(WORD*)(args+%d))",
391                      pos + 2, pos );
392             if (  usecdecl ) pos += 4;
393             break;
394
395         default:
396             fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
397         }
398     }
399     if ( reg_func )
400         fprintf( outfile, "%s        context", i? ",\n" : "" );
401     fprintf( outfile, " );\n}\n\n" );
402 }
403
404
405 /*******************************************************************
406  *         BuildCallTo16Func
407  *
408  * Build a Wine-to-16-bit callback glue function. 
409  *
410  * Prototypes for the CallTo16 functions:
411  *   extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
412  *   extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
413  * 
414  * These routines are provided solely for convenience; they simply
415  * write the arguments onto the 16-bit stack, and call the appropriate
416  * wine_call_to_16... core routine.
417  *
418  * If you have more sophisticated argument conversion requirements than
419  * are provided by these routines, you might as well call the core 
420  * routines by yourself.
421  *
422  */
423 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
424 {
425     char *args = profile + 5;
426     int i, argsize = 0, short_ret = 0;
427
428     if (!strncmp( "word_", profile, 5 )) short_ret = 1;
429     else if (strncmp( "long_", profile, 5 ))
430     {
431         fprintf( stderr, "Invalid function name '%s'.\n", profile );
432         exit(1);
433     }
434
435     fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
436              short_ret? "WORD" : "LONG", prefix, profile );
437     args = profile + 5;
438     for ( i = 0; args[i]; i++ )
439     {
440         fprintf( outfile, ", " );
441         switch (args[i])
442         {
443         case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
444         case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
445         }
446         fprintf( outfile, " arg%d", i+1 );
447     }
448     fprintf( outfile, " )\n{\n" );
449
450     if ( argsize > 0 )
451         fprintf( outfile, "    LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
452
453     args = profile + 5;
454     for ( i = 0; args[i]; i++ )
455     {
456         switch (args[i])
457         {
458         case 'w': fprintf( outfile, "    args -= sizeof(WORD); *(WORD" ); break;
459         case 'l': fprintf( outfile, "    args -= sizeof(LONG); *(LONG" ); break;
460         default:  fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
461                                    args[i] );
462         }
463         fprintf( outfile, " *)args = arg%d;\n", i+1 );
464     }
465
466     fprintf( outfile, "    return wine_call_to_16_%s( proc, %d );\n}\n\n",
467              short_ret? "word" : "long", argsize );
468 }
469
470
471 /*******************************************************************
472  *         Spec16TypeCompare
473  */
474 static int Spec16TypeCompare( const void *e1, const void *e2 )
475 {
476     const ORDDEF *odp1 = *(const ORDDEF **)e1;
477     const ORDDEF *odp2 = *(const ORDDEF **)e2;
478
479     int type1 = (odp1->type == TYPE_CDECL) ? 0
480               : (odp1->type == TYPE_REGISTER) ? 3
481               : (odp1->type == TYPE_INTERRUPT) ? 4
482               : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
483
484     int type2 = (odp2->type == TYPE_CDECL) ? 0
485               : (odp2->type == TYPE_REGISTER) ? 3
486               : (odp2->type == TYPE_INTERRUPT) ? 4
487               : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
488
489     int retval = type1 - type2;
490     if ( !retval )
491         retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
492
493     return retval;
494 }
495
496
497 /*******************************************************************
498  *         output_stub_funcs
499  *
500  * Output the functions for stub entry points
501 */
502 static void output_stub_funcs( FILE *outfile )
503 {
504     int i;
505     char *p;
506
507     for (i = 0; i <= Limit; i++)
508     {
509         ORDDEF *odp = Ordinals[i];
510         if (!odp || odp->type != TYPE_STUB) continue;
511         fprintf( outfile, "#ifdef __GNUC__\n" );
512         fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
513         fprintf( outfile, "#endif\n" );
514         fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
515         fprintf( outfile, "  struct exc_record {\n" );
516         fprintf( outfile, "    unsigned int code, flags;\n" );
517         fprintf( outfile, "    void *rec, *addr;\n" );
518         fprintf( outfile, "    unsigned int params;\n" );
519         fprintf( outfile, "    const void *info[15];\n" );
520         fprintf( outfile, "  } rec;\n" );
521         fprintf( outfile, "  extern void RtlRaiseException( struct exc_record * );\n\n" );
522         fprintf( outfile, "  rec.code    = 0x%08x;\n", EXCEPTION_WINE_STUB );
523         fprintf( outfile, "  rec.flags   = %d;\n", EH_NONCONTINUABLE );
524         fprintf( outfile, "  rec.rec     = 0;\n" );
525         fprintf( outfile, "  rec.params  = 2;\n" );
526         fprintf( outfile, "  rec.info[0] = dllname;\n" );
527         fprintf( outfile, "  rec.info[1] = func;\n" );
528         fprintf( outfile, "#ifdef __GNUC__\n" );
529         fprintf( outfile, "  rec.addr = __builtin_return_address(1);\n" );
530         fprintf( outfile, "#else\n" );
531         fprintf( outfile, "  rec.addr = 0;\n" );
532         fprintf( outfile, "#endif\n" );
533         fprintf( outfile, "  for (;;) RtlRaiseException( &rec );\n}\n\n" );
534         break;
535     }
536     for (i = 0; i <= Limit; i++)
537     {
538         ORDDEF *odp = Ordinals[i];
539         if (!odp || odp->type != TYPE_STUB) continue;
540         strcpy( odp->u.func.link_name, "__stub_" );
541         strcat( odp->u.func.link_name, odp->name );
542         for (p = odp->u.func.link_name; *p; p++) if (!isalnum(*p)) *p = '_';
543         fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
544                  odp->u.func.link_name, odp->name );
545     }
546 }
547
548
549 /*******************************************************************
550  *         BuildSpec16File
551  *
552  * Build a Win16 assembly file from a spec file.
553  */
554 void BuildSpec16File( FILE *outfile )
555 {
556     ORDDEF **type, **typelist;
557     int i, nFuncs, nTypes;
558     int code_offset, data_offset, module_size, res_size;
559     unsigned char *data;
560     unsigned short code_selector = __get_cs();
561
562     /* File header */
563
564     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
565              input_file_name );
566     fprintf( outfile, "#include \"builtin16.h\"\n\n" );
567
568     fprintf( outfile, "extern struct\n{\n" );
569     fprintf( outfile, "  void *base[8192];\n" );
570     fprintf( outfile, "  unsigned long limit[8192];\n" );
571     fprintf( outfile, "  unsigned char flags[8192];\n" );
572     fprintf( outfile, "} wine_ldt_copy;\n\n" );
573
574     data = (unsigned char *)xmalloc( 0x10000 );
575     memset( data, 0, 16 );
576     data_offset = 16;
577     strupper( DLLName );
578
579     fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLName );
580     output_stub_funcs( outfile );
581
582     /* Build sorted list of all argument types, without duplicates */
583
584     typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
585
586     for (i = nFuncs = 0; i <= Limit; i++)
587     {
588         ORDDEF *odp = Ordinals[i];
589         if (!odp) continue;
590         switch (odp->type)
591         {
592           case TYPE_REGISTER:
593           case TYPE_INTERRUPT:
594           case TYPE_CDECL:
595           case TYPE_PASCAL:
596           case TYPE_PASCAL_16:
597           case TYPE_STUB:
598             typelist[nFuncs++] = odp;
599
600           default:
601             break;
602         }
603     }
604
605     qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
606
607     i = nTypes = 0;
608     while ( i < nFuncs )
609     {
610         typelist[nTypes++] = typelist[i++];
611         while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
612             i++;
613     }
614
615     /* Output CallFrom16 routines needed by this .spec file */
616
617     for ( i = 0; i < nTypes; i++ )
618     {
619         char profile[101];
620
621         sprintf( profile, "%s_%s_%s",
622                  (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
623                  (typelist[i]->type == TYPE_REGISTER) ? "regs" :
624                  (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
625                  (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
626                  typelist[i]->u.func.arg_types );
627
628         BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
629     }
630
631     /* Output the DLL functions prototypes */
632
633     for (i = 0; i <= Limit; i++)
634     {
635         ORDDEF *odp = Ordinals[i];
636         if (!odp) continue;
637         switch(odp->type)
638         {
639         case TYPE_REGISTER:
640         case TYPE_INTERRUPT:
641         case TYPE_CDECL:
642         case TYPE_PASCAL:
643         case TYPE_PASCAL_16:
644             fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
645             break;
646         default:
647             break;
648         }
649     }
650
651     /* Output code segment */
652
653     fprintf( outfile, "\nstatic struct\n{\n    CALLFROM16   call[%d];\n"
654                       "    ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n  {\n",
655                       nTypes, nFuncs );
656     code_offset = 0;
657
658     for ( i = 0; i < nTypes; i++ )
659     {
660         char profile[101], *arg;
661         int argsize = 0;
662
663         sprintf( profile, "%s_%s_%s", 
664                           (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
665                           (typelist[i]->type == TYPE_REGISTER) ? "regs" :
666                           (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
667                           (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
668                           typelist[i]->u.func.arg_types );
669
670         if ( typelist[i]->type != TYPE_CDECL )
671             for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
672                 switch ( *arg )
673                 {
674                 case 'w':  /* word */
675                 case 's':  /* s_word */
676                     argsize += 2;
677                     break;
678                 case 'l':  /* long or segmented pointer */
679                 case 'T':  /* segmented pointer to null-terminated string */
680                 case 'p':  /* linear pointer */
681                 case 't':  /* linear pointer to null-terminated string */
682                     argsize += 4;
683                     break;
684                 }
685
686         if ( typelist[i]->type == TYPE_INTERRUPT )
687             argsize += 2;
688
689         fprintf( outfile, "    { 0x68, %s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
690                  DLLName, profile, 
691                  (typelist[i]->type == TYPE_REGISTER 
692                   || typelist[i]->type == TYPE_INTERRUPT)? "regs":
693                  typelist[i]->type == TYPE_PASCAL_16? "word" : "long" );
694         if (argsize)
695             fprintf( outfile, "        0x%04x, 0x66, 0xca, %d, \"%s\" },\n",
696                      code_selector, argsize, profile );
697         else
698             fprintf( outfile, "        0x%04x, 0x66, 0xcb, 0x9090, \"%s\" },\n",
699                      code_selector, profile );
700
701         code_offset += sizeof(CALLFROM16);
702     }
703     fprintf( outfile, "  },\n  {\n" );
704
705     for (i = 0; i <= Limit; i++)
706     {
707         ORDDEF *odp = Ordinals[i];
708         if (!odp) continue;
709         switch (odp->type)
710         {
711           case TYPE_ABS:
712             odp->offset = LOWORD(odp->u.abs.value);
713             break;
714
715           case TYPE_VARIABLE:
716             odp->offset = data_offset;
717             data_offset += StoreVariableCode( data + data_offset, 4, odp);
718             break;
719
720           case TYPE_REGISTER:
721           case TYPE_INTERRUPT:
722           case TYPE_CDECL:
723           case TYPE_PASCAL:
724           case TYPE_PASCAL_16:
725           case TYPE_STUB:
726             type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
727             assert( type );
728
729             fprintf( outfile, "    /* %s.%d */ ", DLLName, i );
730             fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d  /* %s_%s_%s */ },\n",
731                      odp->u.func.link_name,
732                      (type-typelist)*sizeof(CALLFROM16) -
733                      (code_offset + sizeof(ENTRYPOINT16)),
734                      (odp->type == TYPE_CDECL) ? "c" : "p",
735                      (odp->type == TYPE_REGISTER) ? "regs" :
736                      (odp->type == TYPE_INTERRUPT) ? "intr" :
737                      (odp->type == TYPE_PASCAL_16) ? "word" : "long",
738                      odp->u.func.arg_types );
739             odp->offset = code_offset;
740             code_offset += sizeof(ENTRYPOINT16);
741             break;
742
743           default:
744             fprintf(stderr,"build: function type %d not available for Win16\n",
745                     odp->type);
746             exit(1);
747         }
748     }
749
750     fprintf( outfile, "    }\n};\n" );
751
752     /* Output data segment */
753
754     dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
755
756     /* Build the module */
757
758     module_size = BuildModule16( outfile, code_offset, data_offset );
759     res_size = output_res16_data( outfile );
760
761     /* Output the DLL descriptor */
762
763     fprintf( outfile, "\nstatic const BUILTIN16_DESCRIPTOR descriptor = \n{\n" );
764     fprintf( outfile, "    \"%s\",\n", DLLName );
765     fprintf( outfile, "    Module,\n" );
766     fprintf( outfile, "    sizeof(Module),\n" );
767     fprintf( outfile, "    &Code_Segment,\n" );
768     fprintf( outfile, "    Data_Segment,\n" );
769     fprintf( outfile, "    \"%s\",\n", owner_name );
770     fprintf( outfile, "    %s\n", res_size ? "resource_data" : "0" );
771     fprintf( outfile, "};\n" );
772
773     /* Output the DLL constructor */
774
775     fprintf( outfile, "#ifdef __GNUC__\n" );
776     fprintf( outfile, "static void %s_init(void) __attribute__((constructor));\n", DLLName );
777     fprintf( outfile, "#else /* defined(__GNUC__) */\n" );
778     fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
779     fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
780     fprintf( outfile, "    \"\\tcall %s_init\\n\"\n", DLLName );
781     fprintf( outfile, "    \"\\t.previous\\n\");\n" );
782     fprintf( outfile, "}\n" );
783     fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
784     fprintf( outfile, "static void %s_init(void) { __wine_register_dll_16( &descriptor ); }\n",
785              DLLName );
786 }
787
788
789 /*******************************************************************
790  *         BuildGlue
791  *
792  * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
793  */
794 void BuildGlue( FILE *outfile, FILE *infile )
795 {
796     char buffer[1024];
797
798     /* File header */
799
800     fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
801              input_file_name );
802     fprintf( outfile, "#include \"builtin16.h\"\n" );
803     fprintf( outfile, "#include \"stackframe.h\"\n\n" );
804
805     fprintf( outfile, "extern WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs );\n" );
806     fprintf( outfile, "extern LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs );\n" );
807
808     /* Build the callback glue functions */
809
810     while (fgets( buffer, sizeof(buffer), infile ))
811     {
812         if (strstr( buffer, "### start build ###" )) break;
813     }
814     while (fgets( buffer, sizeof(buffer), infile ))
815     {
816         char *p; 
817         if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
818         {
819             char *q, *profile = p + strlen( "CallFrom16_" );
820             for (q = profile; (*q == '_') || isalpha(*q); q++ )
821                 ;
822             *q = '\0';
823             for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
824                 ;
825             if ( ++q < p ) p[-1] = '\0'; else q = "";
826             BuildCallFrom16Func( outfile, profile, q, FALSE );
827         }
828         if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
829         {
830             char *q, *profile = p + strlen( "CallTo16_" );
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             BuildCallTo16Func( outfile, profile, q );
838         }
839         if (strstr( buffer, "### stop build ###" )) break;
840     }
841
842     fclose( infile );
843 }