Removed windows version checks from GetPrinterDriverDirectory tests.
[wine/multimedia.git] / tools / winebuild / spec16.c
blob54da5cd6581d9e91b6a5ad03a14ff2273c828b2d
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 ))
461 fatal_error( "Invalid function name '%s'\n", profile );
463 fprintf( outfile, "unsigned %s __stdcall %s_CallTo16_%s( void (*proc)()",
464 short_ret? "short" : "int", prefix, profile );
465 args = profile + 5;
466 for ( i = 0; args[i]; i++ )
468 fprintf( outfile, ", " );
469 switch (args[i])
471 case 'w': fprintf( outfile, "unsigned short" ); argsize += 2; break;
472 case 'l': fprintf( outfile, "unsigned int" ); argsize += 4; break;
473 default: fatal_error( "Invalid letter '%c' in function name '%s'\n", args[i], profile );
475 fprintf( outfile, " arg%d", i+1 );
477 fprintf( outfile, " )\n{\n" );
479 #ifdef __i386__
480 if ( argsize > 0 )
482 fprintf( outfile, " char *args;\n" );
483 fprintf( outfile, " unsigned int cur_stack;\n\n" );
484 fprintf( outfile, "#ifdef __GNUC__\n" );
485 fprintf( outfile, " __asm__(\".byte 0x64\\n\\tmovl (0x%x),%%0\" : \"=r\" (cur_stack));\n",
486 STACKOFFSET );
487 fprintf( outfile, "#else\n" );
488 fprintf( outfile, " extern char *NtCurrentTeb(void);\n" );
489 fprintf( outfile, " cur_stack = *(unsigned int *)(NtCurrentTeb() + 0x%x);\n",
490 STACKOFFSET );
491 fprintf( outfile, "#endif\n" );
492 fprintf( outfile, " args = (char *)wine_ldt_copy.base[cur_stack >> 19] + (cur_stack & 0xffff);\n" );
495 args = profile + 5;
496 for ( i = 0; args[i]; i++ )
498 switch (args[i])
500 case 'w': fprintf( outfile, " args -= sizeof(unsigned short); *(unsigned short" ); break;
501 case 'l': fprintf( outfile, " args -= sizeof(unsigned int); *(unsigned int" ); break;
502 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
503 args[i] );
505 fprintf( outfile, " *)args = arg%d;\n", i+1 );
508 fprintf( outfile, " return wine_call_to_16( proc, %d );\n}\n\n", argsize );
509 #else /* __i386__ */
510 fprintf( outfile, " assert(0);\n}\n\n" );
511 #endif /* __i386__ */
515 /*******************************************************************
516 * get_function_name
518 static const char *get_function_name( const ORDDEF *odp )
520 static char buffer[80];
522 sprintf( buffer, "%s_%s_%s",
523 (odp->type == TYPE_CDECL) ? "c" : "p",
524 (odp->flags & FLAG_REGISTER) ? "regs" :
525 (odp->flags & FLAG_INTERRUPT) ? "intr" :
526 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
527 odp->u.func.arg_types );
528 return buffer;
532 /*******************************************************************
533 * Spec16TypeCompare
535 static int Spec16TypeCompare( const void *e1, const void *e2 )
537 const ORDDEF *odp1 = *(const ORDDEF **)e1;
538 const ORDDEF *odp2 = *(const ORDDEF **)e2;
539 int retval;
541 int type1 = (odp1->type == TYPE_CDECL) ? 0
542 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
544 int type2 = (odp2->type == TYPE_CDECL) ? 0
545 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
547 if (odp1->flags & FLAG_REGISTER) type1 += 4;
548 if (odp1->flags & FLAG_INTERRUPT) type1 += 8;
549 if (odp2->flags & FLAG_REGISTER) type2 += 4;
550 if (odp2->flags & FLAG_INTERRUPT) type2 += 8;
552 retval = type1 - type2;
553 if ( !retval )
554 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
556 return retval;
560 /*******************************************************************
561 * output_stub_funcs
563 * Output the functions for stub entry points
565 static void output_stub_funcs( FILE *outfile )
567 int i;
568 char *p;
570 for (i = 0; i <= Limit; i++)
572 ORDDEF *odp = Ordinals[i];
573 if (!odp || odp->type != TYPE_STUB) continue;
574 fprintf( outfile, "#ifdef __GNUC__\n" );
575 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
576 fprintf( outfile, "#endif\n" );
577 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
578 fprintf( outfile, " struct exc_record {\n" );
579 fprintf( outfile, " unsigned int code, flags;\n" );
580 fprintf( outfile, " void *rec, *addr;\n" );
581 fprintf( outfile, " unsigned int params;\n" );
582 fprintf( outfile, " const void *info[15];\n" );
583 fprintf( outfile, " } rec;\n\n" );
584 fprintf( outfile, " extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
585 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
586 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
587 fprintf( outfile, " rec.rec = 0;\n" );
588 fprintf( outfile, " rec.params = 2;\n" );
589 fprintf( outfile, " rec.info[0] = \"%s\";\n", DLLFileName );
590 fprintf( outfile, " rec.info[1] = func;\n" );
591 fprintf( outfile, "#ifdef __GNUC__\n" );
592 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
593 fprintf( outfile, "#else\n" );
594 fprintf( outfile, " rec.addr = 0;\n" );
595 fprintf( outfile, "#endif\n" );
596 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
597 break;
599 for (i = 0; i <= Limit; i++)
601 ORDDEF *odp = Ordinals[i];
602 if (!odp || odp->type != TYPE_STUB) continue;
603 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
604 strcpy( odp->link_name, "__wine_stub_" );
605 strcat( odp->link_name, odp->name );
606 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
607 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
608 odp->link_name, odp->name );
613 /*******************************************************************
614 * BuildSpec16File
616 * Build a Win16 assembly file from a spec file.
618 void BuildSpec16File( FILE *outfile )
620 ORDDEF **type, **typelist;
621 int i, nFuncs, nTypes;
622 int code_offset, data_offset, module_size, res_size;
623 unsigned char *data;
624 char constructor[100], destructor[100];
625 #ifdef __i386__
626 unsigned short code_selector = wine_get_cs();
627 #endif
629 /* File header */
631 output_file_header( outfile );
632 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
633 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
634 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
635 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
637 data = (unsigned char *)xmalloc( 0x10000 );
638 memset( data, 0, 16 );
639 data_offset = 16;
641 if (!DLLName[0]) /* set default name from file name */
643 char *p;
644 strcpy( DLLName, DLLFileName );
645 if ((p = strrchr( DLLName, '.' ))) *p = 0;
648 output_stub_funcs( outfile );
650 /* Build sorted list of all argument types, without duplicates */
652 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
654 for (i = nFuncs = 0; i <= Limit; i++)
656 ORDDEF *odp = Ordinals[i];
657 if (!odp) continue;
658 switch (odp->type)
660 case TYPE_CDECL:
661 case TYPE_PASCAL:
662 case TYPE_PASCAL_16:
663 case TYPE_STUB:
664 typelist[nFuncs++] = odp;
666 default:
667 break;
671 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
673 i = nTypes = 0;
674 while ( i < nFuncs )
676 typelist[nTypes++] = typelist[i++];
677 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
678 i++;
681 /* Output CallFrom16 routines needed by this .spec file */
682 #ifdef __i386__
683 for ( i = 0; i < nTypes; i++ )
685 char profile[101];
687 strcpy( profile, get_function_name( typelist[i] ));
688 BuildCallFrom16Func( outfile, profile, DLLFileName );
690 #endif
692 /* Output the DLL functions prototypes */
694 for (i = 0; i <= Limit; i++)
696 ORDDEF *odp = Ordinals[i];
697 if (!odp) continue;
698 switch(odp->type)
700 case TYPE_CDECL:
701 case TYPE_PASCAL:
702 case TYPE_PASCAL_16:
703 fprintf( outfile, "extern void %s();\n", odp->link_name );
704 break;
705 default:
706 break;
710 /* Output code segment */
712 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
713 fprintf( outfile, "\nstatic struct code_segment\n{\n" );
714 fprintf( outfile, " struct {\n" );
715 #ifdef __i386__
716 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
717 fprintf( outfile, " void *relay;\n" );
718 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
719 fprintf( outfile, " void *glue;\n" );
720 fprintf( outfile, " unsigned short flatcs;\n" );
721 #endif
722 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
723 fprintf( outfile, " unsigned short args;\n" );
724 fprintf( outfile, " unsigned int arg_types[2];\n" );
725 fprintf( outfile, " } call[%d];\n", nTypes );
726 fprintf( outfile, " struct {\n" );
727 #ifdef __i386__
728 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
729 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
730 #endif
731 fprintf( outfile, " void (*target)();\n" );
732 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
733 fprintf( outfile, " short callfrom16;\n" );
734 fprintf( outfile, " } entry[%d];\n", nFuncs );
735 fprintf( outfile, "} code_segment =\n{\n {\n" );
737 code_offset = 0;
739 for ( i = 0; i < nTypes; i++ )
741 char profile[101], *arg;
742 unsigned int arg_types[2];
743 int j, argsize = 0;
745 strcpy( profile, get_function_name( typelist[i] ));
746 if ( typelist[i]->type != TYPE_CDECL )
747 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
748 switch ( *arg )
750 case 'w': /* word */
751 case 's': /* s_word */
752 argsize += 2;
753 break;
754 case 'l': /* long or segmented pointer */
755 case 'T': /* segmented pointer to null-terminated string */
756 case 'p': /* linear pointer */
757 case 't': /* linear pointer to null-terminated string */
758 argsize += 4;
759 break;
762 if (typelist[i]->flags & FLAG_INTERRUPT) argsize += 2;
764 /* build the arg types bit fields */
765 arg_types[0] = arg_types[1] = 0;
766 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
768 int type = 0;
769 switch(typelist[i]->u.func.arg_types[j])
771 case 'w': type = ARG_WORD; break;
772 case 's': type = ARG_SWORD; break;
773 case 'l': type = ARG_LONG; break;
774 case 'p': type = ARG_PTR; break;
775 case 't': type = ARG_STR; break;
776 case 'T': type = ARG_SEGSTR; break;
778 arg_types[j / 10] |= type << (3 * (j % 10));
780 if (typelist[i]->flags & (FLAG_REGISTER|FLAG_INTERRUPT)) arg_types[0] |= ARG_REGISTER;
781 if (typelist[i]->type == TYPE_PASCAL_16) arg_types[0] |= ARG_RET16;
783 #ifdef __i386__
784 fprintf( outfile, " { 0x68, __wine_%s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
785 make_c_identifier(DLLFileName), profile,
786 (typelist[i]->flags & (FLAG_REGISTER|FLAG_INTERRUPT)) ? "regs":
787 typelist[i]->type == TYPE_PASCAL_16? "word" : "long" );
788 if (argsize)
789 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
790 code_selector, argsize, arg_types[0], arg_types[1] );
791 else
792 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
793 code_selector, arg_types[0], arg_types[1] );
794 #else
795 if (argsize)
796 fprintf( outfile, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
797 argsize, arg_types[0], arg_types[1] );
798 else
799 fprintf( outfile, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
800 arg_types[0], arg_types[1] );
801 #endif
802 code_offset += sizeof(CALLFROM16);
804 fprintf( outfile, " },\n {\n" );
806 for (i = 0; i <= Limit; i++)
808 ORDDEF *odp = Ordinals[i];
809 if (!odp) continue;
810 switch (odp->type)
812 case TYPE_ABS:
813 odp->offset = LOWORD(odp->u.abs.value);
814 break;
816 case TYPE_VARIABLE:
817 odp->offset = data_offset;
818 data_offset += StoreVariableCode( data + data_offset, 4, odp);
819 break;
821 case TYPE_CDECL:
822 case TYPE_PASCAL:
823 case TYPE_PASCAL_16:
824 case TYPE_STUB:
825 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
826 assert( type );
828 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
829 #ifdef __i386__
830 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
831 #else
832 fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
833 #endif
834 odp->link_name,
835 (type-typelist)*sizeof(CALLFROM16) -
836 (code_offset + sizeof(ENTRYPOINT16)),
837 get_function_name( odp ) );
839 odp->offset = code_offset;
840 code_offset += sizeof(ENTRYPOINT16);
841 break;
843 default:
844 fprintf(stderr,"build: function type %d not available for Win16\n",
845 odp->type);
846 exit(1);
850 fprintf( outfile, " }\n};\n" );
852 /* Output data segment */
854 dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
856 /* Build the module */
858 module_size = BuildModule16( outfile, code_offset, data_offset );
859 res_size = output_res16_data( outfile );
861 /* Output the DLL descriptor */
863 fprintf( outfile, "#include \"poppack.h\"\n\n" );
865 fprintf( outfile, "static const struct dll_descriptor\n{\n" );
866 fprintf( outfile, " unsigned char *module_start;\n" );
867 fprintf( outfile, " int module_size;\n" );
868 fprintf( outfile, " struct code_segment *code_start;\n" );
869 fprintf( outfile, " unsigned char *data_start;\n" );
870 fprintf( outfile, " const char *owner;\n" );
871 fprintf( outfile, " const unsigned char *rsrc;\n" );
872 fprintf( outfile, "} descriptor =\n{\n" );
873 fprintf( outfile, " Module,\n" );
874 fprintf( outfile, " sizeof(Module),\n" );
875 fprintf( outfile, " &code_segment,\n" );
876 fprintf( outfile, " Data_Segment,\n" );
877 fprintf( outfile, " \"%s\",\n", owner_name );
878 fprintf( outfile, " %s\n", res_size ? "resource_data" : "0" );
879 fprintf( outfile, "};\n" );
881 /* Output the DLL constructor */
883 sprintf( constructor, "__wine_spec_%s_init", make_c_identifier(DLLFileName) );
884 sprintf( destructor, "__wine_spec_%s_fini", make_c_identifier(DLLFileName) );
885 output_dll_init( outfile, constructor, destructor );
887 fprintf( outfile,
888 "void %s(void)\n"
889 "{\n"
890 " extern void __wine_register_dll_16( const struct dll_descriptor *descr );\n"
891 " __wine_register_dll_16( &descriptor );\n"
892 "}\n", constructor );
893 fprintf( outfile,
894 "void %s(void)\n"
895 "{\n"
896 " extern void __wine_unregister_dll_16( const struct dll_descriptor *descr );\n"
897 " __wine_unregister_dll_16( &descriptor );\n"
898 "}\n", destructor );
902 /*******************************************************************
903 * BuildGlue
905 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
907 void BuildGlue( FILE *outfile, const char *srcdir, char **argv )
909 char buffer[1024];
911 /* File header */
913 output_file_header( outfile );
915 #ifdef __i386__
916 fprintf( outfile, "extern unsigned int __stdcall wine_call_to_16( void (*target)(), int args );\n\n" );
917 #else
918 fprintf( outfile, "#include <assert.h>\n\n" );
919 #endif
921 /* Build the callback glue functions */
923 while (*argv)
925 FILE *infile = open_input_file( srcdir, *argv );
927 while (fgets( buffer, sizeof(buffer), infile ))
929 current_line++;
930 if (strstr( buffer, "### start build ###" )) break;
932 while (fgets( buffer, sizeof(buffer), infile ))
934 char *p;
935 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
937 char *q, *profile = p + strlen( "CallTo16_" );
938 for (q = profile; (*q == '_') || isalpha(*q); q++ )
940 *q = '\0';
941 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
943 if ( ++q < p ) p[-1] = '\0'; else q = "";
944 BuildCallTo16Func( outfile, profile, q );
946 current_line++;
947 if (strstr( buffer, "### stop build ###" )) break;
949 close_input_file( infile );
950 argv++;