Allocate entry points dynamically to allow for a larger number of
[wine.git] / tools / winebuild / spec16.c
blob58a495326b1ac0e7dca69bf837bb971ff9342ce0
1 /*
2 * 16-bit spec files
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 */
11 #include <assert.h>
12 #include <ctype.h>
14 #include "config.h"
15 #include "wine/exception.h"
16 #include "builtin16.h"
17 #include "module.h"
18 #include "stackframe.h"
20 #include "build.h"
22 #ifdef __i386__
23 extern unsigned short __get_cs(void);
24 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" );
25 #else
26 static inline unsigned short __get_cs(void) { return 0; }
27 #endif /* __i386__ */
30 /*******************************************************************
31 * StoreVariableCode
33 * Store a list of ints into a byte array.
35 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
37 int i;
39 switch(size)
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;
54 return odp->u.var.n_values * size;
58 /*******************************************************************
59 * BuildModule16
61 * Build the in-memory representation of a 16-bit NE module, and dump it
62 * as a byte stream into the assembly code.
64 static int BuildModule16( FILE *outfile, int max_code_offset,
65 int max_data_offset )
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 = 0;
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
87 buffer = xmalloc( 0x10000 );
89 pModule = (NE_MODULE *)buffer;
90 memset( pModule, 0, sizeof(*pModule) );
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;
120 /* File information */
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;
130 #ifdef __i386__ /* FIXME: Alignment problems! */
132 /* Segment table */
134 pSegment = (SEGTABLEENTRY *)pstr;
135 pModule->seg_table = (int)pSegment - (int)pModule;
136 pSegment->filepos = 0;
137 pSegment->size = max_code_offset;
138 pSegment->flags = 0;
139 pSegment->minsize = max_code_offset;
140 pSegment->hSeg = 0;
141 pSegment++;
143 pModule->dgroup_entry = (int)pSegment - (int)pModule;
144 pSegment->filepos = 0;
145 pSegment->size = max_data_offset;
146 pSegment->flags = NE_SEGFLAGS_DATA;
147 pSegment->minsize = max_data_offset;
148 pSegment->hSeg = 0;
149 pSegment++;
151 /* Resource table */
153 pstr = (char *)pSegment;
154 pModule->res_table = (int)pstr - (int)pModule;
155 pstr += output_res16_directory( pstr );
157 /* Imported names table */
159 pModule->import_table = (int)pstr - (int)pModule;
160 *pstr++ = 0;
161 *pstr++ = 0;
163 /* Resident names table */
165 pModule->name_table = (int)pstr - (int)pModule;
166 /* First entry is module name */
167 *pstr = strlen(DLLName );
168 strcpy( pstr + 1, DLLName );
169 pstr += *pstr + 1;
170 *(WORD *)pstr = 0;
171 pstr += sizeof(WORD);
172 /* Store all ordinals */
173 for (i = 1; i <= Limit; i++)
175 ORDDEF *odp = Ordinals[i];
176 if (!odp || !odp->name[0]) continue;
177 *pstr = strlen( odp->name );
178 strcpy( pstr + 1, odp->name );
179 strupper( pstr + 1 );
180 pstr += *pstr + 1;
181 *(WORD *)pstr = i;
182 pstr += sizeof(WORD);
184 *pstr++ = 0;
186 /* Entry table */
188 pModule->entry_table = (int)pstr - (int)pModule;
189 for (i = 1; i <= Limit; i++)
191 int selector = 0;
192 ORDDEF *odp = Ordinals[i];
193 if (!odp) continue;
195 switch (odp->type)
197 case TYPE_CDECL:
198 case TYPE_PASCAL:
199 case TYPE_PASCAL_16:
200 case TYPE_REGISTER:
201 case TYPE_INTERRUPT:
202 case TYPE_STUB:
203 selector = 1; /* Code selector */
204 break;
206 case TYPE_VARIABLE:
207 selector = 2; /* Data selector */
208 break;
210 case TYPE_ABS:
211 selector = 0xfe; /* Constant selector */
212 break;
214 default:
215 selector = 0; /* Invalid selector */
216 break;
219 if ( !selector )
220 continue;
222 if ( bundle && bundle->last+1 == i )
223 bundle->last++;
224 else
226 if ( bundle )
227 bundle->next = (char *)pstr - (char *)pModule;
229 bundle = (ET_BUNDLE *)pstr;
230 bundle->first = i-1;
231 bundle->last = i;
232 bundle->next = 0;
233 pstr += sizeof(ET_BUNDLE);
236 /* FIXME: is this really correct ?? */
237 entry = (ET_ENTRY *)pstr;
238 entry->type = 0xff; /* movable */
239 entry->flags = 3; /* exported & public data */
240 entry->segnum = selector;
241 entry->offs = odp->offset;
242 pstr += sizeof(ET_ENTRY);
244 *pstr++ = 0;
245 #endif
247 /* Dump the module content */
249 dump_bytes( outfile, (char *)pModule, (int)pstr - (int)pModule, "Module", 0 );
250 return (int)pstr - (int)pModule;
254 /*******************************************************************
255 * BuildCallFrom16Func
257 * Build a 16-bit-to-Wine callback glue function.
259 * The generated routines are intended to be used as argument conversion
260 * routines to be called by the CallFrom16... core. Thus, the prototypes of
261 * the generated routines are (see also CallFrom16):
263 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
264 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
265 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
266 * CONTEXT86 *context );
267 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
268 * CONTEXT86 *context );
270 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
271 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
272 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
273 * 'T'=segmented pointer to null-terminated string).
275 * The generated routines fetch the arguments from the 16-bit stack (pointed
276 * to by 'args'); the offsets of the single argument values are computed
277 * according to the calling convention and the argument types. Then, the
278 * 32-bit entry point is called with these arguments.
280 * For register functions, the arguments (if present) are converted just
281 * the same as for normal functions, but in addition the CONTEXT86 pointer
282 * filled with the current register values is passed to the 32-bit routine.
283 * (An 'intr' interrupt handler routine is treated exactly like a register
284 * routine, except that upon return, the flags word pushed onto the stack
285 * by the interrupt is removed by the 16-bit call stub.)
288 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
290 int i, pos, argsize = 0;
291 int short_ret = 0;
292 int reg_func = 0;
293 int usecdecl = 0;
294 char *args = profile + 7;
295 char *ret_type;
297 /* Parse function type */
299 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
300 else if (strncmp( "p_", profile, 2 ))
302 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
303 return;
306 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
307 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
308 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
309 else if (strncmp( "long_", profile + 2, 5 ))
311 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
312 return;
315 for ( i = 0; args[i]; i++ )
316 switch ( args[i] )
318 case 'w': /* word */
319 case 's': /* s_word */
320 argsize += 2;
321 break;
323 case 'l': /* long or segmented pointer */
324 case 'T': /* segmented pointer to null-terminated string */
325 case 'p': /* linear pointer */
326 case 't': /* linear pointer to null-terminated string */
327 argsize += 4;
328 break;
331 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
333 fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ",
334 ret_type, profile );
335 args = profile + 7;
336 for ( i = 0; args[i]; i++ )
338 if ( i ) fprintf( outfile, ", " );
339 switch (args[i])
341 case 'w': fprintf( outfile, "WORD" ); break;
342 case 's': fprintf( outfile, "INT16" ); break;
343 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
344 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
347 if ( reg_func )
348 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
349 else if ( !i )
350 fprintf( outfile, "void" );
351 fprintf( outfile, " );\n" );
353 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
354 local? "static " : "", ret_type, prefix, profile,
355 reg_func? ", struct _CONTEXT86 *context" : "" );
357 fprintf( outfile, " %s((proc_%s_t) proc) (\n",
358 reg_func? "" : "return ", profile );
359 args = profile + 7;
360 pos = !usecdecl? argsize : 0;
361 for ( i = 0; args[i]; i++ )
363 if ( i ) fprintf( outfile, ",\n" );
364 fprintf( outfile, " " );
365 switch (args[i])
367 case 'w': /* word */
368 if ( !usecdecl ) pos -= 2;
369 fprintf( outfile, "*(WORD *)(args+%d)", pos );
370 if ( usecdecl ) pos += 2;
371 break;
373 case 's': /* s_word */
374 if ( !usecdecl ) pos -= 2;
375 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
376 if ( usecdecl ) pos += 2;
377 break;
379 case 'l': /* long or segmented pointer */
380 case 'T': /* segmented pointer to null-terminated string */
381 if ( !usecdecl ) pos -= 4;
382 fprintf( outfile, "*(LONG *)(args+%d)", pos );
383 if ( usecdecl ) pos += 4;
384 break;
386 case 'p': /* linear pointer */
387 case 't': /* linear pointer to null-terminated string */
388 if ( !usecdecl ) pos -= 4;
389 fprintf( outfile, "((char*)wine_ldt_copy.base[*(WORD*)(args+%d) >> 3] + *(WORD*)(args+%d))",
390 pos + 2, pos );
391 if ( usecdecl ) pos += 4;
392 break;
394 default:
395 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
398 if ( reg_func )
399 fprintf( outfile, "%s context", i? ",\n" : "" );
400 fprintf( outfile, " );\n}\n\n" );
404 /*******************************************************************
405 * BuildCallTo16Func
407 * Build a Wine-to-16-bit callback glue function.
409 * Prototypes for the CallTo16 functions:
410 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
411 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
413 * These routines are provided solely for convenience; they simply
414 * write the arguments onto the 16-bit stack, and call the appropriate
415 * wine_call_to_16... core routine.
417 * If you have more sophisticated argument conversion requirements than
418 * are provided by these routines, you might as well call the core
419 * routines by yourself.
422 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
424 char *args = profile + 5;
425 int i, argsize = 0, short_ret = 0;
427 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
428 else if (strncmp( "long_", profile, 5 ))
430 fprintf( stderr, "Invalid function name '%s'.\n", profile );
431 exit(1);
434 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
435 short_ret? "WORD" : "LONG", prefix, profile );
436 args = profile + 5;
437 for ( i = 0; args[i]; i++ )
439 fprintf( outfile, ", " );
440 switch (args[i])
442 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
443 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
445 fprintf( outfile, " arg%d", i+1 );
447 fprintf( outfile, " )\n{\n" );
449 if ( argsize > 0 )
450 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
452 args = profile + 5;
453 for ( i = 0; args[i]; i++ )
455 switch (args[i])
457 case 'w': fprintf( outfile, " args -= sizeof(WORD); *(WORD" ); break;
458 case 'l': fprintf( outfile, " args -= sizeof(LONG); *(LONG" ); break;
459 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
460 args[i] );
462 fprintf( outfile, " *)args = arg%d;\n", i+1 );
465 fprintf( outfile, " return wine_call_to_16_%s( proc, %d );\n}\n\n",
466 short_ret? "word" : "long", argsize );
470 /*******************************************************************
471 * Spec16TypeCompare
473 static int Spec16TypeCompare( const void *e1, const void *e2 )
475 const ORDDEF *odp1 = *(const ORDDEF **)e1;
476 const ORDDEF *odp2 = *(const ORDDEF **)e2;
478 int type1 = (odp1->type == TYPE_CDECL) ? 0
479 : (odp1->type == TYPE_REGISTER) ? 3
480 : (odp1->type == TYPE_INTERRUPT) ? 4
481 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
483 int type2 = (odp2->type == TYPE_CDECL) ? 0
484 : (odp2->type == TYPE_REGISTER) ? 3
485 : (odp2->type == TYPE_INTERRUPT) ? 4
486 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
488 int retval = type1 - type2;
489 if ( !retval )
490 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
492 return retval;
496 /*******************************************************************
497 * output_stub_funcs
499 * Output the functions for stub entry points
501 static void output_stub_funcs( FILE *outfile )
503 int i;
504 char *p;
506 for (i = 0; i <= Limit; i++)
508 ORDDEF *odp = Ordinals[i];
509 if (!odp || odp->type != TYPE_STUB) continue;
510 fprintf( outfile, "#ifdef __GNUC__\n" );
511 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
512 fprintf( outfile, "#endif\n" );
513 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
514 fprintf( outfile, " struct exc_record {\n" );
515 fprintf( outfile, " unsigned int code, flags;\n" );
516 fprintf( outfile, " void *rec, *addr;\n" );
517 fprintf( outfile, " unsigned int params;\n" );
518 fprintf( outfile, " const void *info[15];\n" );
519 fprintf( outfile, " } rec;\n" );
520 fprintf( outfile, " extern void RtlRaiseException( struct exc_record * );\n\n" );
521 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
522 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
523 fprintf( outfile, " rec.rec = 0;\n" );
524 fprintf( outfile, " rec.params = 2;\n" );
525 fprintf( outfile, " rec.info[0] = dllname;\n" );
526 fprintf( outfile, " rec.info[1] = func;\n" );
527 fprintf( outfile, "#ifdef __GNUC__\n" );
528 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
529 fprintf( outfile, "#else\n" );
530 fprintf( outfile, " rec.addr = 0;\n" );
531 fprintf( outfile, "#endif\n" );
532 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
533 break;
535 for (i = 0; i <= Limit; i++)
537 ORDDEF *odp = Ordinals[i];
538 if (!odp || odp->type != TYPE_STUB) continue;
539 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
540 strcpy( odp->link_name, "__wine_stub_" );
541 strcat( odp->link_name, odp->name );
542 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
543 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
544 odp->link_name, odp->name );
549 /*******************************************************************
550 * BuildSpec16File
552 * Build a Win16 assembly file from a spec file.
554 void BuildSpec16File( FILE *outfile )
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();
562 /* File header */
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" );
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" );
574 data = (unsigned char *)xmalloc( 0x10000 );
575 memset( data, 0, 16 );
576 data_offset = 16;
577 strupper( DLLName );
579 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLName );
580 output_stub_funcs( outfile );
582 /* Build sorted list of all argument types, without duplicates */
584 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
586 for (i = nFuncs = 0; i <= Limit; i++)
588 ORDDEF *odp = Ordinals[i];
589 if (!odp) continue;
590 switch (odp->type)
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;
600 default:
601 break;
605 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
607 i = nTypes = 0;
608 while ( i < nFuncs )
610 typelist[nTypes++] = typelist[i++];
611 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
612 i++;
615 /* Output CallFrom16 routines needed by this .spec file */
617 for ( i = 0; i < nTypes; i++ )
619 char profile[101];
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 );
628 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
631 /* Output the DLL functions prototypes */
633 for (i = 0; i <= Limit; i++)
635 ORDDEF *odp = Ordinals[i];
636 if (!odp) continue;
637 switch(odp->type)
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->link_name );
645 break;
646 default:
647 break;
651 /* Output code segment */
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;
658 for ( i = 0; i < nTypes; i++ )
660 char profile[101], *arg;
661 int argsize = 0;
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 );
670 if ( typelist[i]->type != TYPE_CDECL )
671 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
672 switch ( *arg )
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;
686 if ( typelist[i]->type == TYPE_INTERRUPT )
687 argsize += 2;
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 );
701 code_offset += sizeof(CALLFROM16);
703 fprintf( outfile, " },\n {\n" );
705 for (i = 0; i <= Limit; i++)
707 ORDDEF *odp = Ordinals[i];
708 if (!odp) continue;
709 switch (odp->type)
711 case TYPE_ABS:
712 odp->offset = LOWORD(odp->u.abs.value);
713 break;
715 case TYPE_VARIABLE:
716 odp->offset = data_offset;
717 data_offset += StoreVariableCode( data + data_offset, 4, odp);
718 break;
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 );
729 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
730 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s_%s_%s */ },\n",
731 odp->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;
743 default:
744 fprintf(stderr,"build: function type %d not available for Win16\n",
745 odp->type);
746 exit(1);
750 fprintf( outfile, " }\n};\n" );
752 /* Output data segment */
754 dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
756 /* Build the module */
758 module_size = BuildModule16( outfile, code_offset, data_offset );
759 res_size = output_res16_data( outfile );
761 /* Output the DLL descriptor */
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" );
773 /* Output the DLL constructor */
775 fprintf( outfile,
776 "#ifndef __GNUC__\n"
777 "static void __asm__dummy_dll_init(void) {\n"
778 "#endif /* defined(__GNUC__) */\n"
779 "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n"
780 " \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n"
781 " \"\\t.previous\\n\");\n"
782 "#ifndef __GNUC__\n"
783 "}\n"
784 "#endif /* defined(__GNUC__) */\n\n"
785 "void __wine_spec_%s_init(void)\n"
786 "{\n"
787 " __wine_register_dll_16( &descriptor );\n"
788 "}\n", DLLName, DLLName );
792 /*******************************************************************
793 * BuildGlue
795 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
797 void BuildGlue( FILE *outfile, FILE *infile )
799 char buffer[1024];
801 /* File header */
803 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
804 input_file_name );
805 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
807 fprintf( outfile, "extern WORD WINAPI wine_call_to_16_word( FARPROC16 target, INT nArgs );\n" );
808 fprintf( outfile, "extern LONG WINAPI wine_call_to_16_long( FARPROC16 target, INT nArgs );\n" );
810 /* Build the callback glue functions */
812 while (fgets( buffer, sizeof(buffer), infile ))
814 if (strstr( buffer, "### start build ###" )) break;
816 while (fgets( buffer, sizeof(buffer), infile ))
818 char *p;
819 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
821 char *q, *profile = p + strlen( "CallFrom16_" );
822 for (q = profile; (*q == '_') || isalpha(*q); q++ )
824 *q = '\0';
825 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
827 if ( ++q < p ) p[-1] = '\0'; else q = "";
828 BuildCallFrom16Func( outfile, profile, q, FALSE );
830 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
832 char *q, *profile = p + strlen( "CallTo16_" );
833 for (q = profile; (*q == '_') || isalpha(*q); q++ )
835 *q = '\0';
836 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
838 if ( ++q < p ) p[-1] = '\0'; else q = "";
839 BuildCallTo16Func( outfile, profile, q );
841 if (strstr( buffer, "### stop build ###" )) break;
844 fclose( infile );