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
26 #include "wine/port.h"
31 #include "wine/exception.h"
32 #include "wine/library.h"
33 #include "builtin16.h"
35 #include "stackframe.h"
40 /*******************************************************************
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" );
54 fprintf( outfile
, "#define __stdcall __attribute__((__stdcall__))\n\n" );
56 fprintf( outfile
, "#define __stdcall\n\n" );
61 /*******************************************************************
64 * Store a list of ints into a byte array.
66 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
73 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
74 buffer
[i
] = odp
->u
.var
.values
[i
];
77 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
78 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
81 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
82 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
85 return odp
->u
.var
.n_values
* size
;
89 /*******************************************************************
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
,
101 SEGTABLEENTRY
*pSegment
;
104 ET_BUNDLE
*bundle
= 0;
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
;
125 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
127 pModule
->heap_size
= DLLHeapSize
;
128 pModule
->stack_size
= 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;
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;
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
;
169 pSegment
->minsize
= max_code_offset
;
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
;
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
;
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 );
206 /* Store all ordinals */
207 for (i
= 1; i
<= Limit
; i
++)
209 ORDDEF
*odp
= Ordinals
[i
];
211 if (!odp
|| !odp
->name
[0]) continue;
212 *pstr
= strlen( odp
->name
);
213 strcpy( pstr
+ 1, odp
->name
);
214 strupper( pstr
+ 1 );
216 memcpy( pstr
, &ord
, sizeof(WORD
) );
217 pstr
+= sizeof(WORD
);
223 pstr
= (char *)(((long)pstr
+ 3) & ~3);
224 pModule
->entry_table
= (int)pstr
- (int)pModule
;
225 for (i
= 1; i
<= Limit
; i
++)
228 ORDDEF
*odp
= Ordinals
[i
];
237 selector
= 1; /* Code selector */
241 selector
= 2; /* Data selector */
245 selector
= 0xfe; /* Constant selector */
249 selector
= 0; /* Invalid selector */
256 if ( bundle
&& bundle
->last
+1 == i
)
260 pstr
= (char *)(((long)pstr
+ 1) & ~1);
262 bundle
->next
= (char *)pstr
- (char *)pModule
;
264 bundle
= (ET_BUNDLE
*)pstr
;
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
);
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;
329 const char *args
= profile
+ 7;
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
);
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
);
350 for ( i
= 0; args
[i
]; i
++ )
354 case 's': /* s_word */
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 */
365 ret_type
= reg_func
? "void" : short_ret
? "unsigned short" : "unsigned int";
367 fprintf( outfile
, "typedef %s (__stdcall *proc_%s_t)( ", ret_type
, profile
);
369 for ( i
= 0; args
[i
]; i
++ )
371 if ( i
) fprintf( outfile
, ", " );
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;
381 fprintf( outfile
, "%svoid *", 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 " );
392 pos
= !usecdecl
? argsize
: 0;
393 for ( i
= 0; args
[i
]; i
++ )
395 if ( i
) fprintf( outfile
, ",\n" );
396 fprintf( outfile
, " " );
400 if ( !usecdecl
) pos
-= 2;
401 fprintf( outfile
, "*(unsigned short *)(args+%d)", pos
);
402 if ( usecdecl
) pos
+= 2;
405 case 's': /* s_word */
406 if ( !usecdecl
) pos
-= 2;
407 fprintf( outfile
, "*(short *)(args+%d)", pos
);
408 if ( usecdecl
) pos
+= 2;
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;
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))",
423 if ( usecdecl
) pos
+= 4;
427 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
] );
431 fprintf( outfile
, "%s context", i
? ",\n" : "" );
432 fprintf( outfile
, " );\n}\n\n" );
436 /*******************************************************************
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
);
466 fprintf( outfile
, "unsigned %s __stdcall %s_CallTo16_%s( void (*proc)()",
467 short_ret
? "short" : "int", prefix
, profile
);
469 for ( i
= 0; args
[i
]; i
++ )
471 fprintf( outfile
, ", " );
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" );
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",
489 fprintf( outfile
, "#else\n" );
490 fprintf( outfile
, " extern char *NtCurrentTeb(void);\n" );
491 fprintf( outfile
, " cur_stack = *(unsigned int *)(NtCurrentTeb() + 0x%x);\n",
493 fprintf( outfile
, "#endif\n" );
494 fprintf( outfile
, " args = (char *)wine_ldt_copy.base[cur_stack >> 19] + (cur_stack & 0xffff);\n" );
498 for ( i
= 0; args
[i
]; 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",
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
);
513 fprintf( outfile
, " assert(0);\n}\n\n" );
514 #endif /* __i386__ */
518 /*******************************************************************
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
);
535 /*******************************************************************
538 static int Spec16TypeCompare( const void *e1
, const void *e2
)
540 const ORDDEF
*odp1
= *(const ORDDEF
**)e1
;
541 const ORDDEF
*odp2
= *(const ORDDEF
**)e2
;
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
;
557 retval
= strcmp( odp1
->u
.func
.arg_types
, odp2
->u
.func
.arg_types
);
563 /*******************************************************************
566 * Output the functions for stub entry points
568 static void output_stub_funcs( FILE *outfile
)
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" );
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 /*******************************************************************
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
;
627 char constructor
[100], destructor
[100];
629 unsigned short code_selector
= wine_get_cs();
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 );
644 if (!DLLName
[0]) /* set default name from file name */
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
];
667 typelist
[nFuncs
++] = odp
;
674 qsort( typelist
, nFuncs
, sizeof(ORDDEF
*), Spec16TypeCompare
);
679 typelist
[nTypes
++] = typelist
[i
++];
680 while ( i
< nFuncs
&& Spec16TypeCompare( typelist
+ i
, typelist
+ nTypes
-1 ) == 0 )
684 /* Output CallFrom16 routines needed by this .spec file */
686 for ( i
= 0; i
< nTypes
; i
++ )
690 strcpy( profile
, get_function_name( typelist
[i
] ));
691 BuildCallFrom16Func( outfile
, profile
, DLLFileName
);
695 /* Output the DLL functions prototypes */
697 for (i
= 0; i
<= Limit
; i
++)
699 ORDDEF
*odp
= Ordinals
[i
];
706 fprintf( outfile
, "extern void %s();\n", odp
->link_name
);
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" );
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" );
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" );
731 fprintf( outfile
, " unsigned short pushw_bp;\n" ); /* pushw %bp */
732 fprintf( outfile
, " unsigned char pushl;\n" ); /* pushl $target */
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" );
742 for ( i
= 0; i
< nTypes
; i
++ )
744 char profile
[101], *arg
;
745 unsigned int arg_types
[2];
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
++ )
754 case 's': /* s_word */
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 */
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
++)
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
;
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" );
792 fprintf( outfile
, " 0x%04x, 0xca66, %d, { 0x%08x, 0x%08x } },\n",
793 code_selector
, argsize
, arg_types
[0], arg_types
[1] );
795 fprintf( outfile
, " 0x%04x, 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
796 code_selector
, arg_types
[0], arg_types
[1] );
799 fprintf( outfile
, " { 0xca66, %d, { 0x%08x, 0x%08x } },\n",
800 argsize
, arg_types
[0], arg_types
[1] );
802 fprintf( outfile
, " { 0xcb66, 0x9090, { 0x%08x, 0x%08x } },\n",
803 arg_types
[0], arg_types
[1] );
805 code_offset
+= sizeof(CALLFROM16
);
807 fprintf( outfile
, " },\n {\n" );
809 for (i
= 0; i
<= Limit
; i
++)
811 ORDDEF
*odp
= Ordinals
[i
];
816 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
820 odp
->offset
= data_offset
;
821 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
828 type
= bsearch( &odp
, typelist
, nTypes
, sizeof(ORDDEF
*), Spec16TypeCompare
);
831 fprintf( outfile
, " /* %s.%d */ ", DLLName
, i
);
833 fprintf( outfile
, "{ 0x5566, 0x68, %s, 0xe866, %d /* %s */ },\n",
835 fprintf( outfile
, "{ %s, 0xe866, %d, /* %s */ },\n",
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
);
847 fprintf(stderr
,"build: function type %d not available for Win16\n",
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
);
893 " extern void __wine_register_dll_16( const struct dll_descriptor *descr );\n"
894 " __wine_register_dll_16( &descriptor );\n"
895 "}\n", constructor
);
899 " extern void __wine_unregister_dll_16( const struct dll_descriptor *descr );\n"
900 " __wine_unregister_dll_16( &descriptor );\n"
905 /*******************************************************************
908 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
910 void BuildGlue( FILE *outfile
, FILE *infile
)
916 output_file_header( outfile
);
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" );
922 fprintf( outfile
, "#include <assert.h>\n\n" );
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
))
934 if ( (p
= strstr( buffer
, "CallTo16_" )) != NULL
)
936 char *q
, *profile
= p
+ strlen( "CallTo16_" );
937 for (q
= profile
; (*q
== '_') || isalpha(*q
); q
++ )
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;