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