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