Fixed broken forwards reported by Patrik Stridvall.
[wine/multimedia.git] / tools / winebuild / spec16.c
blob811a9718d64bf85593cec72078ad8133044cfd7e
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
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <ctype.h>
31 #include "wine/exception.h"
32 #include "wine/library.h"
33 #include "builtin16.h"
34 #include "module.h"
35 #include "stackframe.h"
37 #include "build.h"
40 /*******************************************************************
41 * output_file_header
43 * Output a file header with the common declarations we need.
45 static void output_file_header( FILE *outfile )
47 output_standard_file_header( outfile );
48 fprintf( outfile, "extern struct\n{\n" );
49 fprintf( outfile, " void *base[8192];\n" );
50 fprintf( outfile, " unsigned long limit[8192];\n" );
51 fprintf( outfile, " unsigned char flags[8192];\n" );
52 fprintf( outfile, "} wine_ldt_copy;\n\n" );
53 #ifdef __i386__
54 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
55 #else
56 fprintf( outfile, "#define __stdcall\n\n" );
57 #endif
61 /*******************************************************************
62 * StoreVariableCode
64 * Store a list of ints into a byte array.
66 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
68 int i;
70 switch(size)
72 case 1:
73 for (i = 0; i < odp->u.var.n_values; i++)
74 buffer[i] = odp->u.var.values[i];
75 break;
76 case 2:
77 for (i = 0; i < odp->u.var.n_values; i++)
78 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
79 break;
80 case 4:
81 for (i = 0; i < odp->u.var.n_values; i++)
82 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
83 break;
85 return odp->u.var.n_values * size;
89 /*******************************************************************
90 * BuildModule16
92 * Build the in-memory representation of a 16-bit NE module, and dump it
93 * as a byte stream into the assembly code.
95 static int BuildModule16( FILE *outfile, int max_code_offset,
96 int max_data_offset )
98 int i;
99 char *buffer;
100 NE_MODULE *pModule;
101 SEGTABLEENTRY *pSegment;
102 OFSTRUCT *pFileInfo;
103 BYTE *pstr;
104 ET_BUNDLE *bundle = 0;
105 ET_ENTRY entry;
107 /* Module layout:
108 * NE_MODULE Module
109 * OFSTRUCT File information
110 * SEGTABLEENTRY Segment 1 (code)
111 * SEGTABLEENTRY Segment 2 (data)
112 * WORD[2] Resource table (empty)
113 * BYTE[2] Imported names (empty)
114 * BYTE[n] Resident names table
115 * BYTE[n] Entry table
118 buffer = xmalloc( 0x10000 );
119 memset( buffer, 0, 0x10000 );
121 pModule = (NE_MODULE *)buffer;
122 pModule->magic = IMAGE_OS2_SIGNATURE;
123 pModule->count = 1;
124 pModule->next = 0;
125 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
126 pModule->dgroup = 2;
127 pModule->heap_size = DLLHeapSize;
128 pModule->stack_size = 0;
129 pModule->ip = 0;
130 pModule->cs = 0;
131 pModule->sp = 0;
132 pModule->ss = 0;
133 pModule->seg_count = 2;
134 pModule->modref_count = 0;
135 pModule->nrname_size = 0;
136 pModule->modref_table = 0;
137 pModule->nrname_fpos = 0;
138 pModule->moveable_entries = 0;
139 pModule->alignment = 0;
140 pModule->truetype = 0;
141 pModule->os_flags = NE_OSFLAGS_WINDOWS;
142 pModule->misc_flags = 0;
143 pModule->dlls_to_init = 0;
144 pModule->nrname_handle = 0;
145 pModule->min_swap_area = 0;
146 pModule->expected_version = 0;
147 pModule->module32 = 0;
148 pModule->self = 0;
149 pModule->self_loading_sel = 0;
151 /* File information */
153 pFileInfo = (OFSTRUCT *)(pModule + 1);
154 pModule->fileinfo = (int)pFileInfo - (int)pModule;
155 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
156 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
157 + strlen(DLLFileName);
158 strcpy( pFileInfo->szPathName, DLLFileName );
159 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
161 /* Segment table */
163 pstr = (char *)(((long)pstr + 3) & ~3);
164 pSegment = (SEGTABLEENTRY *)pstr;
165 pModule->seg_table = (int)pSegment - (int)pModule;
166 pSegment->filepos = 0;
167 pSegment->size = max_code_offset;
168 pSegment->flags = 0;
169 pSegment->minsize = max_code_offset;
170 pSegment->hSeg = 0;
171 pSegment++;
173 pModule->dgroup_entry = (int)pSegment - (int)pModule;
174 pSegment->filepos = 0;
175 pSegment->size = max_data_offset;
176 pSegment->flags = NE_SEGFLAGS_DATA;
177 pSegment->minsize = max_data_offset;
178 pSegment->hSeg = 0;
179 pSegment++;
181 /* Resource table */
183 pstr = (char *)pSegment;
184 pstr = (char *)(((long)pstr + 3) & ~3);
185 pModule->res_table = (int)pstr - (int)pModule;
186 pstr += output_res16_directory( pstr );
188 /* Imported names table */
190 pstr = (char *)(((long)pstr + 3) & ~3);
191 pModule->import_table = (int)pstr - (int)pModule;
192 *pstr++ = 0;
193 *pstr++ = 0;
195 /* Resident names table */
197 pstr = (char *)(((long)pstr + 3) & ~3);
198 pModule->name_table = (int)pstr - (int)pModule;
199 /* First entry is module name */
200 *pstr = strlen( DLLName );
201 strcpy( pstr + 1, DLLName );
202 strupper( pstr + 1 );
203 pstr += *pstr + 1;
204 *pstr++ = 0;
205 *pstr++ = 0;
206 /* Store all ordinals */
207 for (i = 1; i <= Limit; i++)
209 ORDDEF *odp = Ordinals[i];
210 WORD ord = i;
211 if (!odp || !odp->name[0]) continue;
212 *pstr = strlen( odp->name );
213 strcpy( pstr + 1, odp->name );
214 strupper( pstr + 1 );
215 pstr += *pstr + 1;
216 memcpy( pstr, &ord, sizeof(WORD) );
217 pstr += sizeof(WORD);
219 *pstr++ = 0;
221 /* Entry table */
223 pstr = (char *)(((long)pstr + 3) & ~3);
224 pModule->entry_table = (int)pstr - (int)pModule;
225 for (i = 1; i <= Limit; i++)
227 int selector = 0;
228 ORDDEF *odp = Ordinals[i];
229 if (!odp) continue;
231 switch (odp->type)
233 case TYPE_CDECL:
234 case TYPE_PASCAL:
235 case TYPE_PASCAL_16:
236 case TYPE_STUB:
237 selector = 1; /* Code selector */
238 break;
240 case TYPE_VARIABLE:
241 selector = 2; /* Data selector */
242 break;
244 case TYPE_ABS:
245 selector = 0xfe; /* Constant selector */
246 break;
248 default:
249 selector = 0; /* Invalid selector */
250 break;
253 if ( !selector )
254 continue;
256 if ( bundle && bundle->last+1 == i )
257 bundle->last++;
258 else
260 pstr = (char *)(((long)pstr + 1) & ~1);
261 if ( bundle )
262 bundle->next = (char *)pstr - (char *)pModule;
264 bundle = (ET_BUNDLE *)pstr;
265 bundle->first = i-1;
266 bundle->last = i;
267 bundle->next = 0;
268 pstr += sizeof(ET_BUNDLE);
271 /* FIXME: is this really correct ?? */
272 entry.type = 0xff; /* movable */
273 entry.flags = 3; /* exported & public data */
274 entry.segnum = selector;
275 entry.offs = odp->offset;
276 memcpy( pstr, &entry, sizeof(ET_ENTRY) );
277 pstr += sizeof(ET_ENTRY);
279 *pstr++ = 0;
281 /* Dump the module content */
283 pstr = (char *)(((long)pstr + 3) & ~3);
284 dump_bytes( outfile, (char *)pModule, (int)pstr - (int)pModule, "Module", 0 );
285 return (int)pstr - (int)pModule;
289 /*******************************************************************
290 * BuildCallFrom16Func
292 * Build a 16-bit-to-Wine callback glue function.
294 * The generated routines are intended to be used as argument conversion
295 * routines to be called by the CallFrom16... core. Thus, the prototypes of
296 * the generated routines are (see also CallFrom16):
298 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
299 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
300 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
301 * CONTEXT86 *context );
302 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
303 * CONTEXT86 *context );
305 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
306 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
307 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
308 * 'T'=segmented pointer to null-terminated string).
310 * The generated routines fetch the arguments from the 16-bit stack (pointed
311 * to by 'args'); the offsets of the single argument values are computed
312 * according to the calling convention and the argument types. Then, the
313 * 32-bit entry point is called with these arguments.
315 * For register functions, the arguments (if present) are converted just
316 * the same as for normal functions, but in addition the CONTEXT86 pointer
317 * filled with the current register values is passed to the 32-bit routine.
318 * (An 'intr' interrupt handler routine is treated exactly like a register
319 * routine, except that upon return, the flags word pushed onto the stack
320 * by the interrupt is removed by the 16-bit call stub.)
323 static void BuildCallFrom16Func( FILE *outfile, const char *profile, const char *prefix )
325 int i, pos, argsize = 0;
326 int short_ret = 0;
327 int reg_func = 0;
328 int usecdecl = 0;
329 const char *args = profile + 7;
330 char *ret_type;
332 /* Parse function type */
334 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
335 else if (strncmp( "p_", profile, 2 ))
337 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
338 return;
341 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
342 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
343 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
344 else if (strncmp( "long_", profile + 2, 5 ))
346 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
347 return;
350 for ( i = 0; args[i]; i++ )
351 switch ( args[i] )
353 case 'w': /* word */
354 case 's': /* s_word */
355 argsize += 2;
356 break;
357 case 'l': /* long or segmented pointer */
358 case 'T': /* segmented pointer to null-terminated string */
359 case 'p': /* linear pointer */
360 case 't': /* linear pointer to null-terminated string */
361 argsize += 4;
362 break;
365 ret_type = reg_func? "void" : short_ret ? "unsigned short" : "unsigned int";
367 fprintf( outfile, "typedef %s (__stdcall *proc_%s_t)( ", ret_type, profile );
368 args = profile + 7;
369 for ( i = 0; args[i]; i++ )
371 if ( i ) fprintf( outfile, ", " );
372 switch (args[i])
374 case 'w': fprintf( outfile, "unsigned short" ); break;
375 case 's': fprintf( outfile, "short" ); break;
376 case 'l': case 'T': fprintf( outfile, "unsigned int" ); break;
377 case 'p': case 't': fprintf( outfile, "void *" ); break;
380 if ( reg_func )
381 fprintf( outfile, "%svoid *", i? ", " : "" );
382 else if ( !i )
383 fprintf( outfile, "void" );
384 fprintf( outfile, " );\n" );
386 fprintf( outfile, "static %s __stdcall __wine_%s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
387 ret_type, make_c_identifier(prefix), profile, profile,
388 reg_func? ", void *context" : "" );
390 fprintf( outfile, "{\n %sproc(\n", reg_func ? "" : "return " );
391 args = profile + 7;
392 pos = !usecdecl? argsize : 0;
393 for ( i = 0; args[i]; i++ )
395 if ( i ) fprintf( outfile, ",\n" );
396 fprintf( outfile, " " );
397 switch (args[i])
399 case 'w': /* word */
400 if ( !usecdecl ) pos -= 2;
401 fprintf( outfile, "*(unsigned short *)(args+%d)", pos );
402 if ( usecdecl ) pos += 2;
403 break;
405 case 's': /* s_word */
406 if ( !usecdecl ) pos -= 2;
407 fprintf( outfile, "*(short *)(args+%d)", pos );
408 if ( usecdecl ) pos += 2;
409 break;
411 case 'l': /* long or segmented pointer */
412 case 'T': /* segmented pointer to null-terminated string */
413 if ( !usecdecl ) pos -= 4;
414 fprintf( outfile, "*(unsigned int *)(args+%d)", pos );
415 if ( usecdecl ) pos += 4;
416 break;
418 case 'p': /* linear pointer */
419 case 't': /* linear pointer to null-terminated string */
420 if ( !usecdecl ) pos -= 4;
421 fprintf( outfile, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
422 pos + 2, pos );
423 if ( usecdecl ) pos += 4;
424 break;
426 default:
427 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
430 if ( reg_func )
431 fprintf( outfile, "%s context", i? ",\n" : "" );
432 fprintf( outfile, " );\n}\n\n" );
436 /*******************************************************************
437 * BuildCallTo16Func
439 * Build a Wine-to-16-bit callback glue function.
441 * Prototypes for the CallTo16 functions:
442 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
443 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
445 * These routines are provided solely for convenience; they simply
446 * write the arguments onto the 16-bit stack, and call the appropriate
447 * wine_call_to_16... core routine.
449 * If you have more sophisticated argument conversion requirements than
450 * are provided by these routines, you might as well call the core
451 * routines by yourself.
454 static void BuildCallTo16Func( FILE *outfile, const char *profile, const char *prefix )
456 const char *args = profile + 5;
457 int i, argsize = 0, short_ret = 0;
459 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
460 else if (strncmp( "long_", profile, 5 ))
462 fprintf( stderr, "Invalid function name '%s'.\n", profile );
463 exit(1);
466 fprintf( outfile, "unsigned %s __stdcall %s_CallTo16_%s( void (*proc)()",
467 short_ret? "short" : "int", prefix, profile );
468 args = profile + 5;
469 for ( i = 0; args[i]; i++ )
471 fprintf( outfile, ", " );
472 switch (args[i])
474 case 'w': fprintf( outfile, "unsigned short" ); argsize += 2; break;
475 case 'l': fprintf( outfile, "unsigned int" ); argsize += 4; break;
477 fprintf( outfile, " arg%d", i+1 );
479 fprintf( outfile, " )\n{\n" );
481 #ifdef __i386__
482 if ( argsize > 0 )
484 fprintf( outfile, " char *args;\n" );
485 fprintf( outfile, " unsigned int cur_stack;\n\n" );
486 fprintf( outfile, "#ifdef __GNUC__\n" );
487 fprintf( outfile, " __asm__(\".byte 0x64\\n\\tmovl (0x%x),%%0\" : \"=r\" (cur_stack));\n",
488 STACKOFFSET );
489 fprintf( outfile, "#else\n" );
490 fprintf( outfile, " extern char *NtCurrentTeb(void);\n" );
491 fprintf( outfile, " cur_stack = *(unsigned int *)(NtCurrentTeb() + 0x%x);\n",
492 STACKOFFSET );
493 fprintf( outfile, "#endif\n" );
494 fprintf( outfile, " args = (char *)wine_ldt_copy.base[cur_stack >> 19] + (cur_stack & 0xffff);\n" );
497 args = profile + 5;
498 for ( i = 0; args[i]; i++ )
500 switch (args[i])
502 case 'w': fprintf( outfile, " args -= sizeof(unsigned short); *(unsigned short" ); break;
503 case 'l': fprintf( outfile, " args -= sizeof(unsigned int); *(unsigned int" ); break;
504 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
505 args[i] );
507 fprintf( outfile, " *)args = arg%d;\n", i+1 );
510 fprintf( outfile, " return wine_call_to_16_%s( proc, %d );\n}\n\n",
511 short_ret? "word" : "long", argsize );
512 #else /* __i386__ */
513 fprintf( outfile, " assert(0);\n}\n\n" );
514 #endif /* __i386__ */
518 /*******************************************************************
519 * get_function_name
521 static const char *get_function_name( const ORDDEF *odp )
523 static char buffer[80];
525 sprintf( buffer, "%s_%s_%s",
526 (odp->type == TYPE_CDECL) ? "c" : "p",
527 (odp->flags & FLAG_REGISTER) ? "regs" :
528 (odp->flags & FLAG_INTERRUPT) ? "intr" :
529 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
530 odp->u.func.arg_types );
531 return buffer;
535 /*******************************************************************
536 * Spec16TypeCompare
538 static int Spec16TypeCompare( const void *e1, const void *e2 )
540 const ORDDEF *odp1 = *(const ORDDEF **)e1;
541 const ORDDEF *odp2 = *(const ORDDEF **)e2;
542 int retval;
544 int type1 = (odp1->type == TYPE_CDECL) ? 0
545 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
547 int type2 = (odp2->type == TYPE_CDECL) ? 0
548 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
550 if (odp1->flags & FLAG_REGISTER) type1 += 4;
551 if (odp1->flags & FLAG_INTERRUPT) type1 += 8;
552 if (odp2->flags & FLAG_REGISTER) type2 += 4;
553 if (odp2->flags & FLAG_INTERRUPT) type2 += 8;
555 retval = type1 - type2;
556 if ( !retval )
557 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
559 return retval;
563 /*******************************************************************
564 * output_stub_funcs
566 * Output the functions for stub entry points
568 static void output_stub_funcs( FILE *outfile )
570 int i;
571 char *p;
573 for (i = 0; i <= Limit; i++)
575 ORDDEF *odp = Ordinals[i];
576 if (!odp || odp->type != TYPE_STUB) continue;
577 fprintf( outfile, "#ifdef __GNUC__\n" );
578 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
579 fprintf( outfile, "#endif\n" );
580 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
581 fprintf( outfile, " struct exc_record {\n" );
582 fprintf( outfile, " unsigned int code, flags;\n" );
583 fprintf( outfile, " void *rec, *addr;\n" );
584 fprintf( outfile, " unsigned int params;\n" );
585 fprintf( outfile, " const void *info[15];\n" );
586 fprintf( outfile, " } rec;\n\n" );
587 fprintf( outfile, " extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
588 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
589 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
590 fprintf( outfile, " rec.rec = 0;\n" );
591 fprintf( outfile, " rec.params = 2;\n" );
592 fprintf( outfile, " rec.info[0] = \"%s\";\n", DLLFileName );
593 fprintf( outfile, " rec.info[1] = func;\n" );
594 fprintf( outfile, "#ifdef __GNUC__\n" );
595 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
596 fprintf( outfile, "#else\n" );
597 fprintf( outfile, " rec.addr = 0;\n" );
598 fprintf( outfile, "#endif\n" );
599 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
600 break;
602 for (i = 0; i <= Limit; i++)
604 ORDDEF *odp = Ordinals[i];
605 if (!odp || odp->type != TYPE_STUB) continue;
606 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
607 strcpy( odp->link_name, "__wine_stub_" );
608 strcat( odp->link_name, odp->name );
609 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
610 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
611 odp->link_name, odp->name );
616 /*******************************************************************
617 * BuildSpec16File
619 * Build a Win16 assembly file from a spec file.
621 void BuildSpec16File( FILE *outfile )
623 ORDDEF **type, **typelist;
624 int i, nFuncs, nTypes;
625 int code_offset, data_offset, module_size, res_size;
626 unsigned char *data;
627 char constructor[100], destructor[100];
628 #ifdef __i386__
629 unsigned short code_selector = wine_get_cs();
630 #endif
632 /* File header */
634 output_file_header( outfile );
635 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
636 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
637 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
638 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
640 data = (unsigned char *)xmalloc( 0x10000 );
641 memset( data, 0, 16 );
642 data_offset = 16;
644 if (!DLLName[0]) /* set default name from file name */
646 char *p;
647 strcpy( DLLName, DLLFileName );
648 if ((p = strrchr( DLLName, '.' ))) *p = 0;
651 output_stub_funcs( outfile );
653 /* Build sorted list of all argument types, without duplicates */
655 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
657 for (i = nFuncs = 0; i <= Limit; i++)
659 ORDDEF *odp = Ordinals[i];
660 if (!odp) continue;
661 switch (odp->type)
663 case TYPE_CDECL:
664 case TYPE_PASCAL:
665 case TYPE_PASCAL_16:
666 case TYPE_STUB:
667 typelist[nFuncs++] = odp;
669 default:
670 break;
674 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
676 i = nTypes = 0;
677 while ( i < nFuncs )
679 typelist[nTypes++] = typelist[i++];
680 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
681 i++;
684 /* Output CallFrom16 routines needed by this .spec file */
685 #ifdef __i386__
686 for ( i = 0; i < nTypes; i++ )
688 char profile[101];
690 strcpy( profile, get_function_name( typelist[i] ));
691 BuildCallFrom16Func( outfile, profile, DLLFileName );
693 #endif
695 /* Output the DLL functions prototypes */
697 for (i = 0; i <= Limit; i++)
699 ORDDEF *odp = Ordinals[i];
700 if (!odp) continue;
701 switch(odp->type)
703 case TYPE_CDECL:
704 case TYPE_PASCAL:
705 case TYPE_PASCAL_16:
706 fprintf( outfile, "extern void %s();\n", odp->link_name );
707 break;
708 default:
709 break;
713 /* Output code segment */
715 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
716 fprintf( outfile, "\nstatic struct code_segment\n{\n" );
717 fprintf( outfile, " struct {\n" );
718 #ifdef __i386__
719 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
720 fprintf( outfile, " void *relay;\n" );
721 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
722 fprintf( outfile, " void *glue;\n" );
723 fprintf( outfile, " unsigned short flatcs;\n" );
724 #endif
725 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
726 fprintf( outfile, " unsigned short args;\n" );
727 fprintf( outfile, " unsigned int arg_types[2];\n" );
728 fprintf( outfile, " } call[%d];\n", nTypes );
729 fprintf( outfile, " struct {\n" );
730 #ifdef __i386__
731 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
732 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
733 #endif
734 fprintf( outfile, " void (*target)();\n" );
735 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
736 fprintf( outfile, " short callfrom16;\n" );
737 fprintf( outfile, " } entry[%d];\n", nFuncs );
738 fprintf( outfile, "} code_segment =\n{\n {\n" );
740 code_offset = 0;
742 for ( i = 0; i < nTypes; i++ )
744 char profile[101], *arg;
745 unsigned int arg_types[2];
746 int j, argsize = 0;
748 strcpy( profile, get_function_name( typelist[i] ));
749 if ( typelist[i]->type != TYPE_CDECL )
750 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
751 switch ( *arg )
753 case 'w': /* word */
754 case 's': /* s_word */
755 argsize += 2;
756 break;
757 case 'l': /* long or segmented pointer */
758 case 'T': /* segmented pointer to null-terminated string */
759 case 'p': /* linear pointer */
760 case 't': /* linear pointer to null-terminated string */
761 argsize += 4;
762 break;
765 if (typelist[i]->flags & FLAG_INTERRUPT) argsize += 2;
767 /* build the arg types bit fields */
768 arg_types[0] = arg_types[1] = 0;
769 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
771 int type = 0;
772 switch(typelist[i]->u.func.arg_types[j])
774 case 'w': type = ARG_WORD; break;
775 case 's': type = ARG_SWORD; break;
776 case 'l': type = ARG_LONG; break;
777 case 'p': type = ARG_PTR; break;
778 case 't': type = ARG_STR; break;
779 case 'T': type = ARG_SEGSTR; break;
781 arg_types[j / 10] |= type << (3 * (j % 10));
783 if (typelist[i]->flags & (FLAG_REGISTER|FLAG_INTERRUPT)) arg_types[0] |= ARG_REGISTER;
784 if (typelist[i]->type == TYPE_PASCAL_16) arg_types[0] |= ARG_RET16;
786 #ifdef __i386__
787 fprintf( outfile, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
788 make_c_identifier(DLLFileName), profile,
789 (typelist[i]->flags & (FLAG_REGISTER|FLAG_INTERRUPT)) ? "regs":
790 typelist[i]->type == TYPE_PASCAL_16? "word" : "long" );
791 if (argsize)
792 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
793 code_selector, argsize, arg_types[0], arg_types[1] );
794 else
795 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
796 code_selector, arg_types[0], arg_types[1] );
797 #else
798 if (argsize)
799 fprintf( outfile, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
800 argsize, arg_types[0], arg_types[1] );
801 else
802 fprintf( outfile, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
803 arg_types[0], arg_types[1] );
804 #endif
805 code_offset += sizeof(CALLFROM16);
807 fprintf( outfile, " },\n {\n" );
809 for (i = 0; i <= Limit; i++)
811 ORDDEF *odp = Ordinals[i];
812 if (!odp) continue;
813 switch (odp->type)
815 case TYPE_ABS:
816 odp->offset = LOWORD(odp->u.abs.value);
817 break;
819 case TYPE_VARIABLE:
820 odp->offset = data_offset;
821 data_offset += StoreVariableCode( data + data_offset, 4, odp);
822 break;
824 case TYPE_CDECL:
825 case TYPE_PASCAL:
826 case TYPE_PASCAL_16:
827 case TYPE_STUB:
828 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
829 assert( type );
831 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
832 #ifdef __i386__
833 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
834 #else
835 fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
836 #endif
837 odp->link_name,
838 (type-typelist)*sizeof(CALLFROM16) -
839 (code_offset + sizeof(ENTRYPOINT16)),
840 get_function_name( odp ) );
842 odp->offset = code_offset;
843 code_offset += sizeof(ENTRYPOINT16);
844 break;
846 default:
847 fprintf(stderr,"build: function type %d not available for Win16\n",
848 odp->type);
849 exit(1);
853 fprintf( outfile, " }\n};\n" );
855 /* Output data segment */
857 dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
859 /* Build the module */
861 module_size = BuildModule16( outfile, code_offset, data_offset );
862 res_size = output_res16_data( outfile );
864 /* Output the DLL descriptor */
866 fprintf( outfile, "#include \"poppack.h\"\n\n" );
868 fprintf( outfile, "static const struct dll_descriptor\n{\n" );
869 fprintf( outfile, " unsigned char *module_start;\n" );
870 fprintf( outfile, " int module_size;\n" );
871 fprintf( outfile, " struct code_segment *code_start;\n" );
872 fprintf( outfile, " unsigned char *data_start;\n" );
873 fprintf( outfile, " const char *owner;\n" );
874 fprintf( outfile, " const unsigned char *rsrc;\n" );
875 fprintf( outfile, "} descriptor =\n{\n" );
876 fprintf( outfile, " Module,\n" );
877 fprintf( outfile, " sizeof(Module),\n" );
878 fprintf( outfile, " &code_segment,\n" );
879 fprintf( outfile, " Data_Segment,\n" );
880 fprintf( outfile, " \"%s\",\n", owner_name );
881 fprintf( outfile, " %s\n", res_size ? "resource_data" : "0" );
882 fprintf( outfile, "};\n" );
884 /* Output the DLL constructor */
886 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(DLLFileName) );
887 sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(DLLFileName) );
888 output_dll_init( outfile, constructor, destructor );
890 fprintf( outfile,
891 "void %s(void)\n"
892 "{\n"
893 " extern void __wine_register_dll_16( const struct dll_descriptor *descr );\n"
894 " __wine_register_dll_16( &descriptor );\n"
895 "}\n", constructor );
896 fprintf( outfile,
897 "void %s(void)\n"
898 "{\n"
899 " extern void __wine_unregister_dll_16( const struct dll_descriptor *descr );\n"
900 " __wine_unregister_dll_16( &descriptor );\n"
901 "}\n", destructor );
905 /*******************************************************************
906 * BuildGlue
908 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
910 void BuildGlue( FILE *outfile, FILE *infile )
912 char buffer[1024];
914 /* File header */
916 output_file_header( outfile );
918 #ifdef __i386__
919 fprintf( outfile, "extern unsigned short __stdcall wine_call_to_16_word( void (*target)(), int args );\n" );
920 fprintf( outfile, "extern unsigned int __stdcall wine_call_to_16_long( void (*target)(), int args );\n\n" );
921 #else
922 fprintf( outfile, "#include <assert.h>\n\n" );
923 #endif
925 /* Build the callback glue functions */
927 while (fgets( buffer, sizeof(buffer), infile ))
929 if (strstr( buffer, "### start build ###" )) break;
931 while (fgets( buffer, sizeof(buffer), infile ))
933 char *p;
934 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
936 char *q, *profile = p + strlen( "CallTo16_" );
937 for (q = profile; (*q == '_') || isalpha(*q); q++ )
939 *q = '\0';
940 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
942 if ( ++q < p ) p[-1] = '\0'; else q = "";
943 BuildCallTo16Func( outfile, profile, q );
945 if (strstr( buffer, "### stop build ###" )) break;
948 fclose( infile );