Add server side support for async IO on files.
[wine/wine-gecko.git] / tools / winebuild / spec16.c
blobef842d1bf07ea636e12a24da1a9118bf30d75cb7
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 "config.h"
12 #include "wine/port.h"
14 #include <assert.h>
15 #include <ctype.h>
17 #include "wine/exception.h"
18 #include "builtin16.h"
19 #include "module.h"
20 #include "stackframe.h"
22 #include "build.h"
24 #ifdef __i386__
25 extern unsigned short __get_cs(void);
26 __ASM_GLOBAL_FUNC( __get_cs, "movw %cs,%ax\n\tret" );
27 #endif /* __i386__ */
30 /*******************************************************************
31 * output_file_header
33 * Output a file header with the common declarations we need.
35 static void output_file_header( FILE *outfile )
37 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
38 input_file_name );
39 fprintf( outfile, "extern struct\n{\n" );
40 fprintf( outfile, " void *base[8192];\n" );
41 fprintf( outfile, " unsigned long limit[8192];\n" );
42 fprintf( outfile, " unsigned char flags[8192];\n" );
43 fprintf( outfile, "} wine_ldt_copy;\n\n" );
44 #ifdef __i386__
45 fprintf( outfile, "#define __stdcall __attribute__((__stdcall__))\n\n" );
46 #else
47 fprintf( outfile, "#define __stdcall\n\n" );
48 #endif
52 /*******************************************************************
53 * StoreVariableCode
55 * Store a list of ints into a byte array.
57 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
59 int i;
61 switch(size)
63 case 1:
64 for (i = 0; i < odp->u.var.n_values; i++)
65 buffer[i] = odp->u.var.values[i];
66 break;
67 case 2:
68 for (i = 0; i < odp->u.var.n_values; i++)
69 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
70 break;
71 case 4:
72 for (i = 0; i < odp->u.var.n_values; i++)
73 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
74 break;
76 return odp->u.var.n_values * size;
80 /*******************************************************************
81 * BuildModule16
83 * Build the in-memory representation of a 16-bit NE module, and dump it
84 * as a byte stream into the assembly code.
86 static int BuildModule16( FILE *outfile, int max_code_offset,
87 int max_data_offset )
89 int i;
90 char *buffer;
91 NE_MODULE *pModule;
92 SEGTABLEENTRY *pSegment;
93 OFSTRUCT *pFileInfo;
94 BYTE *pstr;
95 ET_BUNDLE *bundle = 0;
96 ET_ENTRY entry;
98 /* Module layout:
99 * NE_MODULE Module
100 * OFSTRUCT File information
101 * SEGTABLEENTRY Segment 1 (code)
102 * SEGTABLEENTRY Segment 2 (data)
103 * WORD[2] Resource table (empty)
104 * BYTE[2] Imported names (empty)
105 * BYTE[n] Resident names table
106 * BYTE[n] Entry table
109 buffer = xmalloc( 0x10000 );
110 memset( buffer, 0, 0x10000 );
112 pModule = (NE_MODULE *)buffer;
113 pModule->magic = IMAGE_OS2_SIGNATURE;
114 pModule->count = 1;
115 pModule->next = 0;
116 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
117 pModule->dgroup = 2;
118 pModule->heap_size = DLLHeapSize;
119 pModule->stack_size = 0;
120 pModule->ip = 0;
121 pModule->cs = 0;
122 pModule->sp = 0;
123 pModule->ss = 0;
124 pModule->seg_count = 2;
125 pModule->modref_count = 0;
126 pModule->nrname_size = 0;
127 pModule->modref_table = 0;
128 pModule->nrname_fpos = 0;
129 pModule->moveable_entries = 0;
130 pModule->alignment = 0;
131 pModule->truetype = 0;
132 pModule->os_flags = NE_OSFLAGS_WINDOWS;
133 pModule->misc_flags = 0;
134 pModule->dlls_to_init = 0;
135 pModule->nrname_handle = 0;
136 pModule->min_swap_area = 0;
137 pModule->expected_version = 0;
138 pModule->module32 = 0;
139 pModule->self = 0;
140 pModule->self_loading_sel = 0;
142 /* File information */
144 pFileInfo = (OFSTRUCT *)(pModule + 1);
145 pModule->fileinfo = (int)pFileInfo - (int)pModule;
146 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
147 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
148 + strlen(DLLFileName);
149 strcpy( pFileInfo->szPathName, DLLFileName );
150 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
152 /* Segment table */
154 pstr = (char *)(((long)pstr + 3) & ~3);
155 pSegment = (SEGTABLEENTRY *)pstr;
156 pModule->seg_table = (int)pSegment - (int)pModule;
157 pSegment->filepos = 0;
158 pSegment->size = max_code_offset;
159 pSegment->flags = 0;
160 pSegment->minsize = max_code_offset;
161 pSegment->hSeg = 0;
162 pSegment++;
164 pModule->dgroup_entry = (int)pSegment - (int)pModule;
165 pSegment->filepos = 0;
166 pSegment->size = max_data_offset;
167 pSegment->flags = NE_SEGFLAGS_DATA;
168 pSegment->minsize = max_data_offset;
169 pSegment->hSeg = 0;
170 pSegment++;
172 /* Resource table */
174 pstr = (char *)pSegment;
175 pstr = (char *)(((long)pstr + 3) & ~3);
176 pModule->res_table = (int)pstr - (int)pModule;
177 pstr += output_res16_directory( pstr );
179 /* Imported names table */
181 pstr = (char *)(((long)pstr + 3) & ~3);
182 pModule->import_table = (int)pstr - (int)pModule;
183 *pstr++ = 0;
184 *pstr++ = 0;
186 /* Resident names table */
188 pstr = (char *)(((long)pstr + 3) & ~3);
189 pModule->name_table = (int)pstr - (int)pModule;
190 /* First entry is module name */
191 *pstr = strlen( DLLName );
192 strcpy( pstr + 1, DLLName );
193 pstr += *pstr + 1;
194 PUT_UA_WORD( pstr, 0 );
195 pstr += sizeof(WORD);
196 /* Store all ordinals */
197 for (i = 1; i <= Limit; i++)
199 ORDDEF *odp = Ordinals[i];
200 if (!odp || !odp->name[0]) continue;
201 *pstr = strlen( odp->name );
202 strcpy( pstr + 1, odp->name );
203 strupper( pstr + 1 );
204 pstr += *pstr + 1;
205 PUT_UA_WORD( pstr, i );
206 pstr += sizeof(WORD);
208 *pstr++ = 0;
210 /* Entry table */
212 pstr = (char *)(((long)pstr + 3) & ~3);
213 pModule->entry_table = (int)pstr - (int)pModule;
214 for (i = 1; i <= Limit; i++)
216 int selector = 0;
217 ORDDEF *odp = Ordinals[i];
218 if (!odp) continue;
220 switch (odp->type)
222 case TYPE_CDECL:
223 case TYPE_PASCAL:
224 case TYPE_PASCAL_16:
225 case TYPE_STUB:
226 selector = 1; /* Code selector */
227 break;
229 case TYPE_VARIABLE:
230 selector = 2; /* Data selector */
231 break;
233 case TYPE_ABS:
234 selector = 0xfe; /* Constant selector */
235 break;
237 default:
238 selector = 0; /* Invalid selector */
239 break;
242 if ( !selector )
243 continue;
245 if ( bundle && bundle->last+1 == i )
246 bundle->last++;
247 else
249 pstr = (char *)(((long)pstr + 1) & ~1);
250 if ( bundle )
251 bundle->next = (char *)pstr - (char *)pModule;
253 bundle = (ET_BUNDLE *)pstr;
254 bundle->first = i-1;
255 bundle->last = i;
256 bundle->next = 0;
257 pstr += sizeof(ET_BUNDLE);
260 /* FIXME: is this really correct ?? */
261 entry.type = 0xff; /* movable */
262 entry.flags = 3; /* exported & public data */
263 entry.segnum = selector;
264 entry.offs = odp->offset;
265 memcpy( pstr, &entry, sizeof(ET_ENTRY) );
266 pstr += sizeof(ET_ENTRY);
268 *pstr++ = 0;
270 /* Dump the module content */
272 pstr = (char *)(((long)pstr + 3) & ~3);
273 dump_bytes( outfile, (char *)pModule, (int)pstr - (int)pModule, "Module", 0 );
274 return (int)pstr - (int)pModule;
278 /*******************************************************************
279 * BuildCallFrom16Func
281 * Build a 16-bit-to-Wine callback glue function.
283 * The generated routines are intended to be used as argument conversion
284 * routines to be called by the CallFrom16... core. Thus, the prototypes of
285 * the generated routines are (see also CallFrom16):
287 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
288 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
289 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
290 * CONTEXT86 *context );
291 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
292 * CONTEXT86 *context );
294 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
295 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
296 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
297 * 'T'=segmented pointer to null-terminated string).
299 * The generated routines fetch the arguments from the 16-bit stack (pointed
300 * to by 'args'); the offsets of the single argument values are computed
301 * according to the calling convention and the argument types. Then, the
302 * 32-bit entry point is called with these arguments.
304 * For register functions, the arguments (if present) are converted just
305 * the same as for normal functions, but in addition the CONTEXT86 pointer
306 * filled with the current register values is passed to the 32-bit routine.
307 * (An 'intr' interrupt handler routine is treated exactly like a register
308 * routine, except that upon return, the flags word pushed onto the stack
309 * by the interrupt is removed by the 16-bit call stub.)
312 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
314 int i, pos, argsize = 0;
315 int short_ret = 0;
316 int reg_func = 0;
317 int usecdecl = 0;
318 char *args = profile + 7;
319 char *ret_type;
321 /* Parse function type */
323 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
324 else if (strncmp( "p_", profile, 2 ))
326 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
327 return;
330 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
331 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
332 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
333 else if (strncmp( "long_", profile + 2, 5 ))
335 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
336 return;
339 for ( i = 0; args[i]; i++ )
340 switch ( args[i] )
342 case 'w': /* word */
343 case 's': /* s_word */
344 argsize += 2;
345 break;
346 case 'l': /* long or segmented pointer */
347 case 'T': /* segmented pointer to null-terminated string */
348 case 'p': /* linear pointer */
349 case 't': /* linear pointer to null-terminated string */
350 argsize += 4;
351 break;
354 ret_type = reg_func? "void" : short_ret ? "unsigned short" : "unsigned int";
356 fprintf( outfile, "typedef %s __stdcall (*proc_%s_t)( ", ret_type, profile );
357 args = profile + 7;
358 for ( i = 0; args[i]; i++ )
360 if ( i ) fprintf( outfile, ", " );
361 switch (args[i])
363 case 'w': fprintf( outfile, "unsigned short" ); break;
364 case 's': fprintf( outfile, "short" ); break;
365 case 'l': case 'T': fprintf( outfile, "unsigned int" ); break;
366 case 'p': case 't': fprintf( outfile, "void *" ); break;
369 if ( reg_func )
370 fprintf( outfile, "%svoid *", i? ", " : "" );
371 else if ( !i )
372 fprintf( outfile, "void" );
373 fprintf( outfile, " );\n" );
375 fprintf( outfile, "%s%s __stdcall %s_CallFrom16_%s( proc_%s_t proc, unsigned char *args%s )\n",
376 local? "static " : "", ret_type, prefix, profile, profile,
377 reg_func? ", void *context" : "" );
379 fprintf( outfile, "{\n %sproc(\n", reg_func ? "" : "return " );
380 args = profile + 7;
381 pos = !usecdecl? argsize : 0;
382 for ( i = 0; args[i]; i++ )
384 if ( i ) fprintf( outfile, ",\n" );
385 fprintf( outfile, " " );
386 switch (args[i])
388 case 'w': /* word */
389 if ( !usecdecl ) pos -= 2;
390 fprintf( outfile, "*(unsigned short *)(args+%d)", pos );
391 if ( usecdecl ) pos += 2;
392 break;
394 case 's': /* s_word */
395 if ( !usecdecl ) pos -= 2;
396 fprintf( outfile, "*(short *)(args+%d)", pos );
397 if ( usecdecl ) pos += 2;
398 break;
400 case 'l': /* long or segmented pointer */
401 case 'T': /* segmented pointer to null-terminated string */
402 if ( !usecdecl ) pos -= 4;
403 fprintf( outfile, "*(unsigned int *)(args+%d)", pos );
404 if ( usecdecl ) pos += 4;
405 break;
407 case 'p': /* linear pointer */
408 case 't': /* linear pointer to null-terminated string */
409 if ( !usecdecl ) pos -= 4;
410 fprintf( outfile, "((char*)wine_ldt_copy.base[*(unsigned short*)(args+%d) >> 3] + *(unsigned short*)(args+%d))",
411 pos + 2, pos );
412 if ( usecdecl ) pos += 4;
413 break;
415 default:
416 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
419 if ( reg_func )
420 fprintf( outfile, "%s context", i? ",\n" : "" );
421 fprintf( outfile, " );\n}\n\n" );
425 /*******************************************************************
426 * BuildCallTo16Func
428 * Build a Wine-to-16-bit callback glue function.
430 * Prototypes for the CallTo16 functions:
431 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
432 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
434 * These routines are provided solely for convenience; they simply
435 * write the arguments onto the 16-bit stack, and call the appropriate
436 * wine_call_to_16... core routine.
438 * If you have more sophisticated argument conversion requirements than
439 * are provided by these routines, you might as well call the core
440 * routines by yourself.
443 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
445 char *args = profile + 5;
446 int i, argsize = 0, short_ret = 0;
448 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
449 else if (strncmp( "long_", profile, 5 ))
451 fprintf( stderr, "Invalid function name '%s'.\n", profile );
452 exit(1);
455 fprintf( outfile, "unsigned %s __stdcall %s_CallTo16_%s( void (*proc)()",
456 short_ret? "short" : "int", prefix, profile );
457 args = profile + 5;
458 for ( i = 0; args[i]; i++ )
460 fprintf( outfile, ", " );
461 switch (args[i])
463 case 'w': fprintf( outfile, "unsigned short" ); argsize += 2; break;
464 case 'l': fprintf( outfile, "unsigned int" ); argsize += 4; break;
466 fprintf( outfile, " arg%d", i+1 );
468 fprintf( outfile, " )\n{\n" );
470 #ifdef __i386__
471 if ( argsize > 0 )
473 fprintf( outfile, " char *args;\n" );
474 fprintf( outfile, " unsigned int cur_stack;\n\n" );
475 fprintf( outfile, "#ifdef __GNUC__\n" );
476 fprintf( outfile, " __asm__(\".byte 0x64\\n\\tmovl (0x%x),%%0\" : \"=r\" (cur_stack));\n",
477 STACKOFFSET );
478 fprintf( outfile, "#else\n" );
479 fprintf( outfile, " extern char *NtCurrentTeb(void);\n" );
480 fprintf( outfile, " cur_stack = *(unsigned int *)(NtCurrentTeb() + 0x%x);\n",
481 STACKOFFSET );
482 fprintf( outfile, "#endif\n" );
483 fprintf( outfile, " args = (char *)wine_ldt_copy.base[cur_stack >> 19] + (cur_stack & 0xffff);\n" );
486 args = profile + 5;
487 for ( i = 0; args[i]; i++ )
489 switch (args[i])
491 case 'w': fprintf( outfile, " args -= sizeof(unsigned short); *(unsigned short" ); break;
492 case 'l': fprintf( outfile, " args -= sizeof(unsigned int); *(unsigned int" ); break;
493 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
494 args[i] );
496 fprintf( outfile, " *)args = arg%d;\n", i+1 );
499 fprintf( outfile, " return wine_call_to_16_%s( proc, %d );\n}\n\n",
500 short_ret? "word" : "long", argsize );
501 #else /* __i386__ */
502 fprintf( outfile, " assert(0);\n}\n\n" );
503 #endif /* __i386__ */
507 /*******************************************************************
508 * get_function_name
510 static const char *get_function_name( const ORDDEF *odp )
512 static char buffer[80];
514 sprintf( buffer, "%s_%s_%s",
515 (odp->type == TYPE_CDECL) ? "c" : "p",
516 (odp->flags & FLAG_REGISTER) ? "regs" :
517 (odp->flags & FLAG_INTERRUPT) ? "intr" :
518 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
519 odp->u.func.arg_types );
520 return buffer;
524 /*******************************************************************
525 * Spec16TypeCompare
527 static int Spec16TypeCompare( const void *e1, const void *e2 )
529 const ORDDEF *odp1 = *(const ORDDEF **)e1;
530 const ORDDEF *odp2 = *(const ORDDEF **)e2;
531 int retval;
533 int type1 = (odp1->type == TYPE_CDECL) ? 0
534 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
536 int type2 = (odp2->type == TYPE_CDECL) ? 0
537 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
539 if (odp1->flags & FLAG_REGISTER) type1 += 4;
540 if (odp1->flags & FLAG_INTERRUPT) type1 += 8;
541 if (odp2->flags & FLAG_REGISTER) type2 += 4;
542 if (odp2->flags & FLAG_INTERRUPT) type2 += 8;
544 retval = type1 - type2;
545 if ( !retval )
546 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
548 return retval;
552 /*******************************************************************
553 * output_stub_funcs
555 * Output the functions for stub entry points
557 static void output_stub_funcs( FILE *outfile )
559 int i;
560 char *p;
562 for (i = 0; i <= Limit; i++)
564 ORDDEF *odp = Ordinals[i];
565 if (!odp || odp->type != TYPE_STUB) continue;
566 fprintf( outfile, "#ifdef __GNUC__\n" );
567 fprintf( outfile, "static void __wine_unimplemented( const char *func ) __attribute__((noreturn));\n" );
568 fprintf( outfile, "#endif\n" );
569 fprintf( outfile, "static void __wine_unimplemented( const char *func )\n{\n" );
570 fprintf( outfile, " struct exc_record {\n" );
571 fprintf( outfile, " unsigned int code, flags;\n" );
572 fprintf( outfile, " void *rec, *addr;\n" );
573 fprintf( outfile, " unsigned int params;\n" );
574 fprintf( outfile, " const void *info[15];\n" );
575 fprintf( outfile, " } rec;\n\n" );
576 fprintf( outfile, " extern void __stdcall RtlRaiseException( struct exc_record * );\n\n" );
577 fprintf( outfile, " rec.code = 0x%08x;\n", EXCEPTION_WINE_STUB );
578 fprintf( outfile, " rec.flags = %d;\n", EH_NONCONTINUABLE );
579 fprintf( outfile, " rec.rec = 0;\n" );
580 fprintf( outfile, " rec.params = 2;\n" );
581 fprintf( outfile, " rec.info[0] = dllname;\n" );
582 fprintf( outfile, " rec.info[1] = func;\n" );
583 fprintf( outfile, "#ifdef __GNUC__\n" );
584 fprintf( outfile, " rec.addr = __builtin_return_address(1);\n" );
585 fprintf( outfile, "#else\n" );
586 fprintf( outfile, " rec.addr = 0;\n" );
587 fprintf( outfile, "#endif\n" );
588 fprintf( outfile, " for (;;) RtlRaiseException( &rec );\n}\n\n" );
589 break;
591 for (i = 0; i <= Limit; i++)
593 ORDDEF *odp = Ordinals[i];
594 if (!odp || odp->type != TYPE_STUB) continue;
595 odp->link_name = xrealloc( odp->link_name, strlen(odp->name) + 13 );
596 strcpy( odp->link_name, "__wine_stub_" );
597 strcat( odp->link_name, odp->name );
598 for (p = odp->link_name; *p; p++) if (!isalnum(*p)) *p = '_';
599 fprintf( outfile, "static void %s(void) { __wine_unimplemented(\"%s\"); }\n",
600 odp->link_name, odp->name );
605 /*******************************************************************
606 * BuildSpec16File
608 * Build a Win16 assembly file from a spec file.
610 void BuildSpec16File( FILE *outfile )
612 ORDDEF **type, **typelist;
613 int i, nFuncs, nTypes;
614 int code_offset, data_offset, module_size, res_size;
615 unsigned char *data;
616 #ifdef __i386__
617 unsigned short code_selector = __get_cs();
618 #endif
620 /* File header */
622 output_file_header( outfile );
623 fprintf( outfile, "extern unsigned short __wine_call_from_16_word();\n" );
624 fprintf( outfile, "extern unsigned int __wine_call_from_16_long();\n" );
625 fprintf( outfile, "extern void __wine_call_from_16_regs();\n" );
626 fprintf( outfile, "extern void __wine_call_from_16_thunk();\n" );
628 data = (unsigned char *)xmalloc( 0x10000 );
629 memset( data, 0, 16 );
630 data_offset = 16;
631 strupper( DLLName );
633 fprintf( outfile, "static const char dllname[] = \"%s\";\n\n", DLLName );
635 output_stub_funcs( outfile );
637 /* Build sorted list of all argument types, without duplicates */
639 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
641 for (i = nFuncs = 0; i <= Limit; i++)
643 ORDDEF *odp = Ordinals[i];
644 if (!odp) continue;
645 switch (odp->type)
647 case TYPE_CDECL:
648 case TYPE_PASCAL:
649 case TYPE_PASCAL_16:
650 case TYPE_STUB:
651 typelist[nFuncs++] = odp;
653 default:
654 break;
658 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
660 i = nTypes = 0;
661 while ( i < nFuncs )
663 typelist[nTypes++] = typelist[i++];
664 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
665 i++;
668 /* Output CallFrom16 routines needed by this .spec file */
669 #ifdef __i386__
670 for ( i = 0; i < nTypes; i++ )
672 char profile[101];
674 strcpy( profile, get_function_name( typelist[i] ));
675 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
677 #endif
679 /* Output the DLL functions prototypes */
681 for (i = 0; i <= Limit; i++)
683 ORDDEF *odp = Ordinals[i];
684 if (!odp) continue;
685 switch(odp->type)
687 case TYPE_CDECL:
688 case TYPE_PASCAL:
689 case TYPE_PASCAL_16:
690 fprintf( outfile, "extern void %s();\n", odp->link_name );
691 break;
692 default:
693 break;
697 /* Output code segment */
699 fprintf( outfile, "\n#include \"pshpack1.h\"\n" );
700 fprintf( outfile, "\nstatic struct code_segment\n{\n" );
701 fprintf( outfile, " struct {\n" );
702 #ifdef __i386__
703 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $relay */
704 fprintf( outfile, " void *relay;\n" );
705 fprintf( outfile, " unsigned char lcall;\n" ); /* lcall __FLATCS__:glue */
706 fprintf( outfile, " void *glue;\n" );
707 fprintf( outfile, " unsigned short flatcs;\n" );
708 #endif
709 fprintf( outfile, " unsigned short lret;\n" ); /* lret $args */
710 fprintf( outfile, " unsigned short args;\n" );
711 fprintf( outfile, " unsigned int arg_types[2];\n" );
712 fprintf( outfile, " } call[%d];\n", nTypes );
713 fprintf( outfile, " struct {\n" );
714 #ifdef __i386__
715 fprintf( outfile, " unsigned short pushw_bp;\n" ); /* pushw %bp */
716 fprintf( outfile, " unsigned char pushl;\n" ); /* pushl $target */
717 #endif
718 fprintf( outfile, " void (*target)();\n" );
719 fprintf( outfile, " unsigned short call;\n" ); /* call CALLFROM16 */
720 fprintf( outfile, " short callfrom16;\n" );
721 fprintf( outfile, " } entry[%d];\n", nFuncs );
722 fprintf( outfile, "} code_segment =\n{\n {\n" );
724 code_offset = 0;
726 for ( i = 0; i < nTypes; i++ )
728 char profile[101], *arg;
729 unsigned int arg_types[2];
730 int j, argsize = 0;
732 strcpy( profile, get_function_name( typelist[i] ));
733 if ( typelist[i]->type != TYPE_CDECL )
734 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
735 switch ( *arg )
737 case 'w': /* word */
738 case 's': /* s_word */
739 argsize += 2;
740 break;
741 case 'l': /* long or segmented pointer */
742 case 'T': /* segmented pointer to null-terminated string */
743 case 'p': /* linear pointer */
744 case 't': /* linear pointer to null-terminated string */
745 argsize += 4;
746 break;
749 if (typelist[i]->flags & FLAG_INTERRUPT) argsize += 2;
751 /* build the arg types bit fields */
752 arg_types[0] = arg_types[1] = 0;
753 for (j = 0; typelist[i]->u.func.arg_types[j]; j++)
755 int type = 0;
756 switch(typelist[i]->u.func.arg_types[j])
758 case 'w': type = ARG_WORD; break;
759 case 's': type = ARG_SWORD; break;
760 case 'l': type = ARG_LONG; break;
761 case 'p': type = ARG_PTR; break;
762 case 't': type = ARG_STR; break;
763 case 'T': type = ARG_SEGSTR; break;
765 arg_types[j / 10] |= type << (3 * (j % 10));
767 if (typelist[i]->flags & (FLAG_REGISTER|FLAG_INTERRUPT)) arg_types[0] |= ARG_REGISTER;
768 if (typelist[i]->type == TYPE_PASCAL_16) arg_types[0] |= ARG_RET16;
770 #ifdef __i386__
771 fprintf( outfile, " { 0x68, %s_CallFrom16_%s, 0x9a, __wine_call_from_16_%s,\n",
772 DLLName, profile,
773 (typelist[i]->flags & (FLAG_REGISTER|FLAG_INTERRUPT)) ? "regs":
774 typelist[i]->type == TYPE_PASCAL_16? "word" : "long" );
775 if (argsize)
776 fprintf( outfile, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
777 code_selector, argsize, arg_types[0], arg_types[1] );
778 else
779 fprintf( outfile, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
780 code_selector, arg_types[0], arg_types[1] );
781 #else
782 if (argsize)
783 fprintf( outfile, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
784 argsize, arg_types[0], arg_types[1] );
785 else
786 fprintf( outfile, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
787 arg_types[0], arg_types[1] );
788 #endif
789 code_offset += sizeof(CALLFROM16);
791 fprintf( outfile, " },\n {\n" );
793 for (i = 0; i <= Limit; i++)
795 ORDDEF *odp = Ordinals[i];
796 if (!odp) continue;
797 switch (odp->type)
799 case TYPE_ABS:
800 odp->offset = LOWORD(odp->u.abs.value);
801 break;
803 case TYPE_VARIABLE:
804 odp->offset = data_offset;
805 data_offset += StoreVariableCode( data + data_offset, 4, odp);
806 break;
808 case TYPE_CDECL:
809 case TYPE_PASCAL:
810 case TYPE_PASCAL_16:
811 case TYPE_STUB:
812 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
813 assert( type );
815 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
816 #ifdef __i386__
817 fprintf( outfile, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
818 #else
819 fprintf( outfile, "{ %s, 0xe866, %d, /* %s */ },\n",
820 #endif
821 odp->link_name,
822 (type-typelist)*sizeof(CALLFROM16) -
823 (code_offset + sizeof(ENTRYPOINT16)),
824 get_function_name( odp ) );
826 odp->offset = code_offset;
827 code_offset += sizeof(ENTRYPOINT16);
828 break;
830 default:
831 fprintf(stderr,"build: function type %d not available for Win16\n",
832 odp->type);
833 exit(1);
837 fprintf( outfile, " }\n};\n" );
839 /* Output data segment */
841 dump_bytes( outfile, data, data_offset, "Data_Segment", 0 );
843 /* Build the module */
845 module_size = BuildModule16( outfile, code_offset, data_offset );
846 res_size = output_res16_data( outfile );
848 /* Output the DLL descriptor */
850 fprintf( outfile, "#include \"poppack.h\"\n\n" );
852 fprintf( outfile, "static const struct dll_descriptor\n{\n" );
853 fprintf( outfile, " unsigned char *module_start;\n" );
854 fprintf( outfile, " int module_size;\n" );
855 fprintf( outfile, " struct code_segment *code_start;\n" );
856 fprintf( outfile, " unsigned char *data_start;\n" );
857 fprintf( outfile, " const char *owner;\n" );
858 fprintf( outfile, " const unsigned char *rsrc;\n" );
859 fprintf( outfile, "} descriptor =\n{\n" );
860 fprintf( outfile, " Module,\n" );
861 fprintf( outfile, " sizeof(Module),\n" );
862 fprintf( outfile, " &code_segment,\n" );
863 fprintf( outfile, " Data_Segment,\n" );
864 fprintf( outfile, " \"%s\",\n", owner_name );
865 fprintf( outfile, " %s\n", res_size ? "resource_data" : "0" );
866 fprintf( outfile, "};\n" );
868 /* Output the DLL constructor */
870 fprintf( outfile, "#ifndef __GNUC__\n" );
871 fprintf( outfile, "static void __asm__dummy_dll_init(void) {\n" );
872 fprintf( outfile, "#endif /* defined(__GNUC__) */\n" );
874 #if defined(__i386__)
875 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
876 fprintf( outfile, " \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n", DLLName );
877 fprintf( outfile, " \"\\t.previous\\n\");\n" );
878 #elif defined(__sparc__)
879 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
880 fprintf( outfile, " \"\\tcall " PREFIX "__wine_spec_%s_init\\n\"\n", DLLName );
881 fprintf( outfile, " \"\\tnop\\n\"\n" );
882 fprintf( outfile, " \"\\t.previous\\n\");\n" );
883 #elif defined(__PPC__)
884 fprintf( outfile, "asm(\"\\t.section\t.init ,\\\"ax\\\"\\n\"\n" );
885 fprintf( outfile, " \"\\tbl " PREFIX "__wine_spec_%s_init\\n\"\n", DLLName );
886 fprintf( outfile, " \"\\t.previous\\n\");\n" );
887 #else
888 #error You need to define the DLL constructor for your architecture
889 #endif
891 fprintf( outfile, "#ifndef __GNUC__\n" );
892 fprintf( outfile, "}\n" );
893 fprintf( outfile, "#endif /* defined(__GNUC__) */\n\n" );
895 fprintf( outfile,
896 "void __wine_spec_%s_init(void)\n"
897 "{\n"
898 " extern void __wine_register_dll_16( const struct dll_descriptor *descr );\n"
899 " __wine_register_dll_16( &descriptor );\n"
900 "}\n", DLLName );
904 /*******************************************************************
905 * BuildGlue
907 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
909 void BuildGlue( FILE *outfile, FILE *infile )
911 char buffer[1024];
913 /* File header */
915 output_file_header( outfile );
917 #ifdef __i386__
918 fprintf( outfile, "extern unsigned short __stdcall wine_call_to_16_word( void (*target)(), int args );\n" );
919 fprintf( outfile, "extern unsigned int __stdcall wine_call_to_16_long( void (*target)(), int args );\n\n" );
920 #else
921 fprintf( outfile, "#include <assert.h>\n\n" );
922 #endif
924 /* Build the callback glue functions */
926 while (fgets( buffer, sizeof(buffer), infile ))
928 if (strstr( buffer, "### start build ###" )) break;
930 while (fgets( buffer, sizeof(buffer), infile ))
932 char *p;
933 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
935 char *q, *profile = p + strlen( "CallTo16_" );
936 for (q = profile; (*q == '_') || isalpha(*q); q++ )
938 *q = '\0';
939 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
941 if ( ++q < p ) p[-1] = '\0'; else q = "";
942 BuildCallTo16Func( outfile, profile, q );
944 if (strstr( buffer, "### stop build ###" )) break;
947 fclose( infile );