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