2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996 Alexandre Julliard
14 #include "registers.h"
15 #include "winerror.h" /* for ERROR_CALL_NOT_IMPLEMENTED */
20 #ifdef NEED_UNDERSCORE_PREFIX
29 TYPE_BYTE
, /* byte variable */
30 TYPE_WORD
, /* word variable */
31 TYPE_LONG
, /* long variable */
32 TYPE_PASCAL_16
, /* pascal function with 16-bit return (Win16) */
33 TYPE_PASCAL
, /* pascal function with 32-bit return (Win16) */
34 TYPE_REGISTER
, /* register function (Win16) */
35 TYPE_ABS
, /* absolute value */
36 TYPE_RETURN
, /* simple return value function */
37 TYPE_STUB
, /* unimplemented stub */
38 TYPE_STDCALL
, /* stdcall function (Win32) */
39 TYPE_CDECL
, /* cdecl function (Win32) */
40 TYPE_EXTERN
, /* external symbol (Win32) */
44 static const char * const TypeNames
[TYPE_NBTYPES
] =
47 "byte", /* TYPE_BYTE */
48 "word", /* TYPE_WORD */
49 "long", /* TYPE_LONG */
50 "pascal16", /* TYPE_PASCAL_16 */
51 "pascal", /* TYPE_PASCAL */
52 "register", /* TYPE_REGISTER */
53 "equate", /* TYPE_ABS */
54 "return", /* TYPE_RETURN */
55 "stub", /* TYPE_STUB */
56 "stdcall", /* TYPE_STDCALL */
57 "cdecl", /* TYPE_CDECL */
58 "extern" /* TYPE_EXTERN */
61 #define MAX_ORDINALS 1299
63 /* Callback function used for stub functions */
64 #define STUB_CALLBACK \
65 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
118 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
120 static SPEC_TYPE SpecType
= SPEC_INVALID
;
127 char *ParseBuffer
= NULL
;
132 static int debugging
= 1;
134 /* Offset of register relative to the end of the context struct */
135 #define CONTEXTOFFSET(reg) \
136 ((int)®##_reg((SIGCONTEXT *)0) - sizeof(SIGCONTEXT))
138 static void *xmalloc (size_t size
)
142 res
= malloc (size
? size
: 1);
145 fprintf (stderr
, "Virtual memory exhausted.\n");
152 static void *xrealloc (void *ptr
, size_t size
)
154 void *res
= realloc (ptr
, size
);
157 fprintf (stderr
, "Virtual memory exhausted.\n");
164 static int IsNumberString(char *s
)
173 static char *strupper(char *s
)
177 for(p
= s
; *p
!= '\0'; p
++)
183 static char * GetTokenInLine(void)
188 if (ParseNext
!= ParseBuffer
)
190 if (ParseSaveChar
== '\0')
192 *ParseNext
= ParseSaveChar
;
196 * Remove initial white space.
198 for (p
= ParseNext
; isspace(*p
); p
++)
201 if ((*p
== '\0') || (*p
== '#'))
208 if (*token
!= '(' && *token
!= ')')
209 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
219 static char * GetToken(void)
223 if (ParseBuffer
== NULL
)
225 ParseBuffer
= xmalloc(512);
226 ParseNext
= ParseBuffer
;
230 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
232 if (ParseBuffer
[0] != '#')
237 while ((token
= GetTokenInLine()) == NULL
)
239 ParseNext
= ParseBuffer
;
243 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
245 if (ParseBuffer
[0] != '#')
253 static int ParseVariable( ORDDEF
*odp
)
258 int value_array_size
;
260 char *token
= GetToken();
263 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
268 value_array_size
= 25;
269 value_array
= xmalloc(sizeof(*value_array
) * value_array_size
);
271 while ((token
= GetToken()) != NULL
)
276 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
277 if (n_values
== value_array_size
)
279 value_array_size
+= 25;
280 value_array
= xrealloc(value_array
,
281 sizeof(*value_array
) * value_array_size
);
284 if (endptr
== NULL
|| *endptr
!= '\0')
286 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
294 fprintf(stderr
, "%d: End of file in variable declaration\n", Line
);
298 odp
->u
.var
.n_values
= n_values
;
299 odp
->u
.var
.values
= xrealloc(value_array
, sizeof(*value_array
) * n_values
);
304 static int ParseExportFunction( ORDDEF
*odp
)
312 if (odp
->type
== TYPE_STDCALL
)
314 fprintf( stderr
, "%d: 'stdcall' not supported for Win16\n", Line
);
317 if (odp
->type
== TYPE_CDECL
)
319 fprintf( stderr
, "%d: 'cdecl' not supported for Win16\n", Line
);
324 if ((odp
->type
== TYPE_PASCAL
) || (odp
->type
== TYPE_PASCAL_16
))
326 fprintf( stderr
, "%d: 'pascal' not supported for Win32\n", Line
);
337 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
341 for (i
= 0; i
< sizeof(odp
->u
.func
.arg_types
)-1; i
++)
347 if (!strcmp(token
, "byte") || !strcmp(token
, "word"))
348 odp
->u
.func
.arg_types
[i
] = 'w';
349 else if (!strcmp(token
, "s_byte") || !strcmp(token
, "s_word"))
350 odp
->u
.func
.arg_types
[i
] = 's';
351 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
352 odp
->u
.func
.arg_types
[i
] = 'l';
353 else if (!strcmp(token
, "ptr"))
354 odp
->u
.func
.arg_types
[i
] = 'p';
357 fprintf(stderr
, "%d: Unknown variable type '%s'\n", Line
, token
);
360 if (SpecType
== SPEC_WIN32
)
362 if (strcmp(token
, "long") && strcmp(token
, "ptr"))
364 fprintf( stderr
, "%d: Type '%s' not supported for Win32\n",
372 fprintf( stderr
, "%d: Too many arguments\n", Line
);
375 odp
->u
.func
.arg_types
[i
] = '\0';
376 if ((odp
->type
== TYPE_STDCALL
) && !i
)
377 odp
->type
= TYPE_CDECL
; /* stdcall is the same as cdecl for 0 args */
378 strcpy(odp
->u
.func
.link_name
, GetToken());
383 /*******************************************************************
386 * Parse an 'equate' definition.
388 static int ParseEquate( ORDDEF
*odp
)
392 char *token
= GetToken();
393 int value
= strtol(token
, &endptr
, 0);
394 if (endptr
== NULL
|| *endptr
!= '\0')
396 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
401 odp
->u
.abs
.value
= value
;
406 /*******************************************************************
409 * Parse a 'return' definition.
411 static int ParseReturn( ORDDEF
*odp
)
417 odp
->u
.ret
.arg_size
= strtol(token
, &endptr
, 0);
418 if (endptr
== NULL
|| *endptr
!= '\0')
420 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
426 odp
->u
.ret
.ret_value
= strtol(token
, &endptr
, 0);
427 if (endptr
== NULL
|| *endptr
!= '\0')
429 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
438 /*******************************************************************
441 * Parse a 'stub' definition.
443 static int ParseStub( ORDDEF
*odp
)
445 odp
->u
.func
.arg_types
[0] = '\0';
446 strcpy( odp
->u
.func
.link_name
, STUB_CALLBACK
);
451 /*******************************************************************
454 * Parse an 'extern' definition.
456 static int ParseExtern( ORDDEF
*odp
)
458 if (SpecType
== SPEC_WIN16
)
460 fprintf( stderr
, "%d: 'extern' not supported for Win16\n", Line
);
463 strcpy( odp
->u
.ext
.link_name
, GetToken() );
468 /*******************************************************************
471 * Parse an ordinal definition.
473 static int ParseOrdinal(int ordinal
)
478 if (ordinal
>= MAX_ORDINALS
)
480 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
483 if (ordinal
> Limit
) Limit
= ordinal
;
485 odp
= &OrdinalDefinitions
[ordinal
];
486 if (!(token
= GetToken()))
488 fprintf(stderr
, "%d: Expected type after ordinal\n", Line
);
492 for (odp
->type
= 0; odp
->type
< TYPE_NBTYPES
; odp
->type
++)
493 if (TypeNames
[odp
->type
] && !strcmp( token
, TypeNames
[odp
->type
] ))
496 if (odp
->type
>= TYPE_NBTYPES
)
499 "%d: Expected type after ordinal, found '%s' instead\n",
504 if (!(token
= GetToken()))
506 fprintf( stderr
, "%d: Expected name after type\n", Line
);
509 strcpy( odp
->name
, token
);
516 return ParseVariable( odp
);
522 return ParseExportFunction( odp
);
524 return ParseEquate( odp
);
526 return ParseReturn( odp
);
528 return ParseStub( odp
);
530 return ParseExtern( odp
);
532 fprintf( stderr
, "Should not happen\n" );
537 static int ParseTopLevel(void)
541 while ((token
= GetToken()) != NULL
)
543 if (strcmp(token
, "name") == 0)
545 strcpy(DLLName
, GetToken());
548 else if (strcmp(token
, "type") == 0)
551 if (!strcmp(token
, "win16" )) SpecType
= SPEC_WIN16
;
552 else if (!strcmp(token
, "win32" )) SpecType
= SPEC_WIN32
;
555 fprintf(stderr
, "%d: Type must be 'win16' or 'win32'\n", Line
);
559 else if (strcmp(token
, "base") == 0)
562 if (!IsNumberString(token
))
564 fprintf(stderr
, "%d: Expected number after base\n", Line
);
569 else if (strcmp(token
, "heap") == 0)
572 if (!IsNumberString(token
))
574 fprintf(stderr
, "%d: Expected number after heap\n", Line
);
577 DLLHeapSize
= atoi(token
);
579 else if (IsNumberString(token
))
584 ordinal
= atoi(token
);
585 if ((rv
= ParseOrdinal(ordinal
)) < 0)
591 "%d: Expected name, id, length or ordinal\n", Line
);
600 /*******************************************************************
603 * Store a list of ints into a byte array.
605 static int StoreVariableCode( unsigned char *buffer
, int size
, ORDDEF
*odp
)
612 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
613 buffer
[i
] = odp
->u
.var
.values
[i
];
616 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
617 ((unsigned short *)buffer
)[i
] = odp
->u
.var
.values
[i
];
620 for (i
= 0; i
< odp
->u
.var
.n_values
; i
++)
621 ((unsigned int *)buffer
)[i
] = odp
->u
.var
.values
[i
];
624 return odp
->u
.var
.n_values
* size
;
628 /*******************************************************************
631 * Dump a byte stream into the assembly code.
633 static void DumpBytes( const unsigned char *data
, int len
,
634 const char *section
, const char *label_start
)
637 if (section
) printf( "\t%s\n", section
);
638 if (label_start
) printf( "%s:\n", label_start
);
639 for (i
= 0; i
< len
; i
++)
641 if (!(i
& 0x0f)) printf( "\t.byte " );
642 printf( "%d", *data
++ );
643 if (i
< len
- 1) printf( "%c", ((i
& 0x0f) != 0x0f) ? ',' : '\n' );
649 /*******************************************************************
652 * Build the in-memory representation of a 16-bit NE module, and dump it
653 * as a byte stream into the assembly code.
655 static int BuildModule16( int max_code_offset
, int max_data_offset
)
661 SEGTABLEENTRY
*pSegment
;
668 * OFSTRUCT File information
669 * SEGTABLEENTRY Segment 1 (code)
670 * SEGTABLEENTRY Segment 2 (data)
671 * WORD[2] Resource table (empty)
672 * BYTE[2] Imported names (empty)
673 * BYTE[n] Resident names table
674 * BYTE[n] Entry table
677 buffer
= xmalloc( 0x10000 );
679 pModule
= (NE_MODULE
*)buffer
;
680 pModule
->magic
= NE_SIGNATURE
;
683 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
| NE_FFLAGS_LIBMODULE
;
685 pModule
->heap_size
= DLLHeapSize
;
686 pModule
->stack_size
= 0;
691 pModule
->seg_count
= 2;
692 pModule
->modref_count
= 0;
693 pModule
->nrname_size
= 0;
694 pModule
->modref_table
= 0;
695 pModule
->nrname_fpos
= 0;
696 pModule
->moveable_entries
= 0;
697 pModule
->alignment
= 0;
698 pModule
->truetype
= 0;
699 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
700 pModule
->misc_flags
= 0;
701 pModule
->dlls_to_init
= 0;
702 pModule
->nrname_handle
= 0;
703 pModule
->min_swap_area
= 0;
704 pModule
->expected_version
= 0x030a;
705 pModule
->pe_module
= NULL
;
707 pModule
->self_loading_sel
= 0;
709 /* File information */
711 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
712 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
713 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
714 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
715 + strlen(DLLName
) + 4;
716 sprintf( pFileInfo
->szPathName
, "%s.DLL", DLLName
);
717 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
721 pSegment
= (SEGTABLEENTRY
*)pstr
;
722 pModule
->seg_table
= (int)pSegment
- (int)pModule
;
723 pSegment
->filepos
= 0;
724 pSegment
->size
= max_code_offset
;
726 pSegment
->minsize
= max_code_offset
;
727 pSegment
->selector
= 0;
730 pModule
->dgroup_entry
= (int)pSegment
- (int)pModule
;
731 pSegment
->filepos
= 0;
732 pSegment
->size
= max_data_offset
;
733 pSegment
->flags
= NE_SEGFLAGS_DATA
;
734 pSegment
->minsize
= max_data_offset
;
735 pSegment
->selector
= 0;
740 pword
= (WORD
*)pSegment
;
741 pModule
->res_table
= (int)pword
- (int)pModule
;
745 /* Imported names table */
747 pstr
= (char *)pword
;
748 pModule
->import_table
= (int)pstr
- (int)pModule
;
752 /* Resident names table */
754 pModule
->name_table
= (int)pstr
- (int)pModule
;
755 /* First entry is module name */
756 *pstr
= strlen(DLLName
);
757 strcpy( pstr
+ 1, DLLName
);
760 pstr
+= sizeof(WORD
);
761 /* Store all ordinals */
762 odp
= OrdinalDefinitions
+ 1;
763 for (i
= 1; i
<= Limit
; i
++, odp
++)
765 if (!odp
->name
[0]) continue;
766 *pstr
= strlen( odp
->name
);
767 strcpy( pstr
+ 1, odp
->name
);
768 strupper( pstr
+ 1 );
771 pstr
+= sizeof(WORD
);
777 pModule
->entry_table
= (int)pstr
- (int)pModule
;
779 odp
= OrdinalDefinitions
+ 1;
780 for (i
= 1; i
<= Limit
; i
++, odp
++)
791 selector
= 1; /* Code selector */
797 selector
= 2; /* Data selector */
801 selector
= 0xfe; /* Constant selector */
805 selector
= 0; /* Invalid selector */
809 /* create a new bundle if necessary */
810 if (!bundle
|| (bundle
[0] >= 254) || (bundle
[1] != selector
))
814 bundle
[1] = selector
;
822 *(WORD
*)pstr
= odp
->offset
;
823 pstr
+= sizeof(WORD
);
828 /* Dump the module content */
830 DumpBytes( (char *)pModule
, (int)pstr
- (int)pModule
,
831 ".data", "Module_Start" );
832 return (int)pstr
- (int)pModule
;
836 /*******************************************************************
839 * Build the in-memory representation of a 32-bit pseudo-NE module, and dump it
840 * as a byte stream into the assembly code.
842 static int BuildModule32(void)
852 * OFSTRUCT File information
853 * SEGTABLEENTRY Segment table (empty)
854 * WORD[2] Resource table (empty)
855 * BYTE[2] Imported names (empty)
856 * BYTE[n] Resident names table (1 entry)
857 * BYTE[n] Entry table (empty)
860 buffer
= xmalloc( 0x10000 );
862 pModule
= (NE_MODULE
*)buffer
;
863 pModule
->magic
= NE_SIGNATURE
;
866 pModule
->dgroup_entry
= 0;
867 pModule
->flags
= NE_FFLAGS_SINGLEDATA
| NE_FFLAGS_BUILTIN
|
868 NE_FFLAGS_LIBMODULE
| NE_FFLAGS_WIN32
;
870 pModule
->heap_size
= DLLHeapSize
;
871 pModule
->stack_size
= 0;
876 pModule
->seg_count
= 0;
877 pModule
->modref_count
= 0;
878 pModule
->nrname_size
= 0;
879 pModule
->modref_table
= 0;
880 pModule
->nrname_fpos
= 0;
881 pModule
->moveable_entries
= 0;
882 pModule
->alignment
= 0;
883 pModule
->truetype
= 0;
884 pModule
->os_flags
= NE_OSFLAGS_WINDOWS
;
885 pModule
->misc_flags
= 0;
886 pModule
->dlls_to_init
= 0;
887 pModule
->nrname_handle
= 0;
888 pModule
->min_swap_area
= 0;
889 pModule
->expected_version
= 0x030a;
890 pModule
->pe_module
= NULL
;
892 pModule
->self_loading_sel
= 0;
894 /* File information */
896 pFileInfo
= (OFSTRUCT
*)(pModule
+ 1);
897 pModule
->fileinfo
= (int)pFileInfo
- (int)pModule
;
898 memset( pFileInfo
, 0, sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
) );
899 pFileInfo
->cBytes
= sizeof(*pFileInfo
) - sizeof(pFileInfo
->szPathName
)
900 + strlen(DLLName
) + 4;
901 sprintf( pFileInfo
->szPathName
, "%s.DLL", DLLName
);
902 pstr
= (char *)pFileInfo
+ pFileInfo
->cBytes
+ 1;
906 pModule
->seg_table
= (int)pstr
- (int)pModule
;
910 pword
= (WORD
*)pstr
;
911 pModule
->res_table
= (int)pword
- (int)pModule
;
915 /* Imported names table */
917 pstr
= (char *)pword
;
918 pModule
->import_table
= (int)pstr
- (int)pModule
;
922 /* Resident names table */
924 pModule
->name_table
= (int)pstr
- (int)pModule
;
925 /* First entry is module name */
926 *pstr
= strlen(DLLName
);
927 strcpy( pstr
+ 1, DLLName
);
930 pstr
+= sizeof(WORD
);
935 pModule
->entry_table
= (int)pstr
- (int)pModule
;
938 /* Dump the module content */
940 DumpBytes( (char *)pModule
, (int)pstr
- (int)pModule
,
941 ".data", "Module_Start" );
942 return (int)pstr
- (int)pModule
;
946 /*******************************************************************
949 * Build a Win32 assembly file from a spec file.
951 static void BuildSpec32Files(void)
954 int i
, module_size
, len
;
957 printf( "/* File generated automatically; do not edit! */\n" );
958 printf( "\t.text\n" );
959 printf( "\t.align 4\n" );
960 printf( "Code_Start:\n\n" );
962 odp
= OrdinalDefinitions
;
963 for (i
= 0; i
<= Limit
; i
++, odp
++)
973 printf( "/* %s.%d (%s) */\n",
974 DLLName
, i
, odp
->name
);
975 printf( "%s_%d:\n", DLLName
, i
);
976 printf( "\tpushl %%ebp\n" );
977 printf( "\tpushl $" PREFIX
"%s\n", odp
->u
.func
.link_name
);
978 printf( "\tcall " PREFIX
"CallFrom32_%s_%d\n",
979 (odp
->type
== TYPE_STDCALL
) ? "stdcall" : "cdecl",
980 strlen(odp
->u
.func
.arg_types
));
985 printf( "/* %s.%d (%s) */\n",
986 DLLName
, i
, odp
->name
);
987 printf( "%s_%d:\n", DLLName
, i
);
988 printf( "\tmovl $%d,%%eax\n", ERROR_CALL_NOT_IMPLEMENTED
);
989 printf( "\tmovl %%eax," PREFIX
"WIN32_LastError\n" );
990 printf( "\tmovl $%d,%%eax\n", odp
->u
.ret
.ret_value
);
991 if (odp
->u
.ret
.arg_size
)
993 printf( "\tret $%d\n", odp
->u
.ret
.arg_size
);
997 else printf( "\tret\n" );
1001 printf( "/* %s.%d (%s) */\n",
1002 DLLName
, i
, odp
->name
);
1003 printf( "\t.data\n" );
1004 printf( "%s_%d:\n", DLLName
, i
);
1005 len
= StoreVariableCode( buffer
, 1, odp
);
1006 DumpBytes( buffer
, len
, NULL
, NULL
);
1007 printf( "\t.text\n" );
1011 printf( "/* %s.%d (%s) */\n",
1012 DLLName
, i
, odp
->name
);
1013 printf( "\t.data\n" );
1014 printf( "%s_%d:\n", DLLName
, i
);
1015 len
= StoreVariableCode( buffer
, 2, odp
);
1016 DumpBytes( buffer
, len
, NULL
, NULL
);
1017 printf( "\t.text\n" );
1021 printf( "/* %s.%d (%s) */\n",
1022 DLLName
, i
, odp
->name
);
1023 printf( "\t.data\n" );
1024 printf( "%s_%d:\n", DLLName
, i
);
1025 len
= StoreVariableCode( buffer
, 4, odp
);
1026 DumpBytes( buffer
, len
, NULL
, NULL
);
1027 printf( "\t.text\n" );
1034 fprintf(stderr
,"build: function type %d not available for Win32\n",
1040 module_size
= BuildModule32();
1042 /* Output the DLL functions table */
1044 printf( "\t.text\n" );
1045 printf( "\t.align 4\n" );
1046 printf( "Functions:\n" );
1047 odp
= OrdinalDefinitions
;
1048 for (i
= 0; i
<= Limit
; i
++, odp
++)
1053 printf( "\t.long 0\n" );
1056 printf( "\t.long " PREFIX
"%s\n", odp
->u
.ext
.link_name
);
1059 printf( "\t.long %s_%d\n", DLLName
, i
);
1064 /* Output the DLL names table */
1066 printf( "FuncNames:\n" );
1067 odp
= OrdinalDefinitions
;
1068 for (i
= 0; i
<= Limit
; i
++, odp
++)
1070 if (odp
->type
== TYPE_INVALID
) printf( "\t.long 0\n" );
1071 else printf( "\t.long Name_%d\n", i
);
1074 /* Output the DLL names */
1076 for (i
= 0, odp
= OrdinalDefinitions
; i
<= Limit
; i
++, odp
++)
1078 if (odp
->type
!= TYPE_INVALID
)
1079 printf( "Name_%d:\t.ascii \"%s\\0\"\n", i
, odp
->name
);
1082 /* Output the DLL descriptor */
1084 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
1085 printf( "\t.align 4\n" );
1086 printf( "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1087 printf( PREFIX
"%s_Descriptor:\n", DLLName
);
1088 printf( "\t.long DLLName\n" ); /* Name */
1089 printf( "\t.long Module_Start\n" ); /* Module start */
1090 printf( "\t.long %d\n", module_size
); /* Module size */
1091 printf( "\t.long %d\n", Base
); /* Base */
1092 printf( "\t.long %d\n", Limit
); /* Limit */
1093 printf( "\t.long Functions\n" ); /* Functions */
1094 printf( "\t.long FuncNames\n" ); /* Function names */
1098 /*******************************************************************
1101 * Build a Win16 assembly file from a spec file.
1103 static void BuildSpec16Files(void)
1107 int code_offset
, data_offset
, module_size
;
1108 unsigned char *data
;
1110 data
= (unsigned char *)xmalloc( 0x10000 );
1111 memset( data
, 0, 16 );
1114 printf( "/* File generated automatically; do not edit! */\n" );
1115 printf( "\t.text\n" );
1116 printf( "Code_Start:\n" );
1119 odp
= OrdinalDefinitions
;
1120 for (i
= 0; i
<= Limit
; i
++, odp
++)
1125 odp
->offset
= 0xffff;
1129 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1133 odp
->offset
= data_offset
;
1134 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1138 odp
->offset
= data_offset
;
1139 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1143 odp
->offset
= data_offset
;
1144 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1148 printf( "/* %s.%d */\n", DLLName
, i
);
1149 printf( "\tmovw $%d,%%ax\n", LOWORD(odp
->u
.ret
.ret_value
) );
1150 printf( "\tmovw $%d,%%dx\n", HIWORD(odp
->u
.ret
.ret_value
) );
1151 printf( "\t.byte 0x66\n");
1152 if (odp
->u
.ret
.arg_size
!= 0)
1153 printf( "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1156 printf( "\tlret\n");
1158 printf( "\tnop\n\n");
1160 odp
->offset
= code_offset
;
1161 code_offset
+= 12; /* Assembly code is 12 bytes long */
1166 case TYPE_PASCAL_16
:
1168 printf( "/* %s.%d */\n", DLLName
, i
);
1169 printf( "\tpushw %%bp\n" );
1170 printf( "\tpushl $" PREFIX
"%s\n", odp
->u
.func
.link_name
);
1171 /* FreeBSD does not understand lcall, so do it the hard way */
1172 printf( "\t.byte 0x9a /*lcall*/\n" );
1173 printf( "\t.long " PREFIX
"CallFrom16_%s_%s\n",
1174 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1175 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
1176 odp
->u
.func
.arg_types
);
1177 printf( "\t.byte 0x%02x,0x%02x\n", /* Some asms don't have .word */
1178 LOBYTE(WINE_CODE_SELECTOR
), HIBYTE(WINE_CODE_SELECTOR
) );
1179 printf( "\tnop\n" );
1180 printf( "\tnop\n\n" );
1181 odp
->offset
= code_offset
;
1182 code_offset
+= 16; /* Assembly code is 16 bytes long */
1186 fprintf(stderr
,"build: function type %d not available for Win16\n",
1192 if (!code_offset
) /* Make sure the code segment is not empty */
1194 printf( "\t.byte 0\n" );
1198 /* Output data segment */
1200 DumpBytes( data
, data_offset
, NULL
, "Data_Start" );
1202 /* Build the module */
1204 module_size
= BuildModule16( code_offset
, data_offset
);
1206 /* Output the DLL descriptor */
1208 printf( "\t.text\n" );
1209 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
1210 printf( "\t.align 4\n" );
1211 printf( "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1212 printf( PREFIX
"%s_Descriptor:\n", DLLName
);
1213 printf( "\t.long DLLName\n" ); /* Name */
1214 printf( "\t.long Module_Start\n" ); /* Module start */
1215 printf( "\t.long %d\n", module_size
); /* Module size */
1216 printf( "\t.long Code_Start\n" ); /* Code start */
1217 printf( "\t.long Data_Start\n" ); /* Data start */
1221 /*******************************************************************
1224 * Build an assembly file from a spec file.
1226 static void BuildSpecFiles( char *specname
)
1228 SpecFp
= fopen( specname
, "r");
1231 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1239 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1251 /*******************************************************************
1252 * BuildCall32LargeStack
1254 * Build the function used to switch to the original 32-bit stack
1255 * before calling a 32-bit function from 32-bit code. This is used for
1256 * functions that need a large stack, like X bitmaps functions.
1258 * The generated function has the following prototype:
1259 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1270 static void BuildCall32LargeStack(void)
1272 /* Function header */
1274 printf( "/**********\n" );
1275 printf( " * " PREFIX
"CallTo32_LargeStack\n" );
1276 printf( " **********/\n" );
1277 printf( "\t.align 4\n" );
1278 printf( "\t.globl " PREFIX
"CallTo32_LargeStack\n\n" );
1279 printf( PREFIX
"CallTo32_LargeStack:\n" );
1283 printf( "\tpushl %%ebp\n" );
1284 printf( "\tmovl %%esp,%%ebp\n" );
1286 /* Save registers */
1288 printf( "\tpushl %%ecx\n" );
1289 printf( "\tpushl %%esi\n" );
1290 printf( "\tpushl %%edi\n" );
1292 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1294 printf( "\tmovl " PREFIX
"IF1632_Original32_esp, %%eax\n" );
1295 printf( "\torl %%eax,%%eax\n" );
1296 printf( "\tje no_orig_esp\n" );
1297 printf( "\tmovl %%eax,%%esp\n" );
1298 printf( "no_orig_esp:\n" );
1300 /* Transfer the arguments */
1302 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1303 printf( "\torl %%ecx,%%ecx\n" );
1304 printf( "\tje no_args\n" );
1305 printf( "\tleal 16(%%ebp),%%esi\n" );
1306 printf( "\tshll $2,%%ecx\n" );
1307 printf( "\tsubl %%ecx,%%esp\n" );
1308 printf( "\tmovl %%esp,%%edi\n" );
1309 printf( "\tshrl $2,%%ecx\n" );
1310 printf( "\tcld\n" );
1311 printf( "\trep; movsl\n" );
1312 printf( "no_args:\n" );
1314 /* Call the function */
1316 printf( "\tcall 8(%%ebp)\n" );
1318 /* Switch back to the normal stack */
1320 printf( "\tleal -12(%%ebp),%%esp\n" );
1322 /* Restore registers and return */
1324 printf( "\tpopl %%edi\n" );
1325 printf( "\tpopl %%esi\n" );
1326 printf( "\tpopl %%ecx\n" );
1327 printf( "\tpopl %%ebp\n" );
1328 printf( "\tret\n" );
1332 /*******************************************************************
1333 * TransferArgs16To32
1335 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1336 * The 16-bit stack layout is:
1344 static int TransferArgs16To32( char *args
)
1346 int i
, pos16
, pos32
;
1348 /* Save ebx first */
1350 printf( "\tpushl %%ebx\n" );
1352 /* Get the 32-bit stack pointer */
1354 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1356 /* Copy the arguments */
1358 pos16
= 6; /* skip bp and return address */
1361 for (i
= strlen(args
); i
> 0; i
--)
1366 case 'w': /* word */
1367 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1368 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1372 case 's': /* s_word */
1373 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1374 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1378 case 'l': /* long */
1379 printf( "\tmovl %d(%%ebp),%%eax\n", pos16
);
1380 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1385 /* Get the selector */
1386 printf( "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1387 /* Get the selector base */
1388 printf( "\tandl $0xfff8,%%eax\n" );
1389 printf( "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1390 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1391 /* Add the offset */
1392 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1393 printf( "\taddl %%eax,%d(%%ebx)\n", pos32
);
1398 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1404 printf( "\tpopl %%ebx\n" );
1406 return pos16
- 6; /* Return the size of the 16-bit args */
1410 /*******************************************************************
1413 * Build the context structure on the 32-bit stack.
1414 * The only valid registers in the context structure are:
1415 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1417 static void BuildContext(void)
1419 /* Save ebx first */
1421 printf( "\tpushl %%ebx\n" );
1423 /* Get the 32-bit stack pointer */
1425 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1427 /* Store the registers */
1429 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EBX
) ); /* Get ebx from stack*/
1430 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(EAX
) );
1431 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(ECX
) );
1432 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(EDX
) );
1433 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(ESI
) );
1434 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(EDI
) );
1435 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1436 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(DS
) );
1437 printf( "\tmovw -6(%%ebp),%%ax\n" ); /* Get saved es from stack */
1438 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(ES
) );
1439 printf( "\tpushfl\n" );
1440 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EFL
) );
1444 /*******************************************************************
1447 * Restore the registers from the context structure
1449 static void RestoreContext(void)
1451 /* Get the 32-bit stack pointer */
1453 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1455 /* Restore the registers */
1457 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(ECX
) );
1458 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(EDX
) );
1459 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(ESI
) );
1460 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(EDI
) );
1461 printf( "\tpopl %%eax\n" ); /* Remove old ds and ip from stack */
1462 printf( "\tpopl %%eax\n" ); /* Remove old cs and es from stack */
1463 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(DS
) ); /* Push new ds */
1464 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(ES
) ); /* Push new es */
1465 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFL
) );
1466 printf( "\tpopfl\n" );
1467 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(EAX
) );
1468 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(EBX
) );
1472 /*******************************************************************
1473 * BuildCallFrom16Func
1475 * Build a 16-bit-to-Wine callback function. The syntax of the function
1476 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1477 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1478 * 'l'=long, 'p'=pointer).
1479 * For register functions, the arguments are ignored, but they are still
1480 * removed from the stack upon return.
1482 * Stack layout upon entry to the callback function:
1484 * (sp+18) word first 16-bit arg
1488 * (sp+8) long 32-bit entry point
1489 * (sp+6) word high word of cs (always 0, used to store es)
1490 * (sp+4) word low word of cs of 16-bit entry point
1491 * (sp+2) word high word of ip (always 0, used to store ds)
1492 * (sp) word low word of ip of 16-bit entry point
1495 static void BuildCallFrom16Func( char *profile
)
1500 char *args
= profile
+ 5;
1502 /* Parse function type */
1504 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1505 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1506 else if (strncmp( "long_", profile
, 5 ))
1508 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1512 /* Function header */
1514 printf( "/**********\n" );
1515 printf( " * " PREFIX
"CallFrom16_%s\n", profile
);
1516 printf( " **********/\n" );
1517 printf( "\t.align 4\n" );
1518 printf( "\t.globl " PREFIX
"CallFrom16_%s\n\n", profile
);
1519 printf( PREFIX
"CallFrom16_%s:\n", profile
);
1521 /* Setup bp to point to its copy on the stack */
1523 printf( "\tmovzwl %%sp,%%ebp\n" );
1524 printf( "\taddw $12,%%bp\n" );
1526 /* Save 16-bit ds and es */
1528 /* Stupid FreeBSD assembler doesn't know these either */
1529 /* printf( "\tmovw %%ds,-10(%%ebp)\n" ); */
1530 printf( "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1531 /* printf( "\tmovw %%es,-6(%%ebp)\n" ); */
1532 printf( "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1534 /* Restore 32-bit ds and es */
1536 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR
, WINE_DATA_SELECTOR
);
1537 printf( "\tpopw %%ds\n" );
1538 printf( "\tpopw %%es\n" );
1541 /* Save the 16-bit stack */
1543 printf( "\tpushw " PREFIX
"IF1632_Saved16_sp\n" );
1544 printf( "\tpushw " PREFIX
"IF1632_Saved16_ss\n" );
1546 printf("\tdata16\n");
1548 printf( "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss\n" );
1549 printf( "\tmovw %%sp," PREFIX
"IF1632_Saved16_sp\n" );
1551 /* Transfer the arguments */
1553 if (reg_func
) BuildContext();
1554 else if (*args
) argsize
= TransferArgs16To32( args
);
1556 /* Get the address of the API function */
1558 printf( "\tmovl -4(%%ebp),%%eax\n" );
1560 /* If necessary, save %edx over the API function address */
1562 if (!reg_func
&& short_ret
)
1563 printf( "\tmovl %%edx,-4(%%ebp)\n" );
1565 /* Switch to the 32-bit stack */
1567 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebp\n" );
1568 printf( "\tpushw %%ds\n" );
1569 printf( "\tpopw %%ss\n" );
1570 printf( "\tleal -%d(%%ebp),%%esp\n",
1571 reg_func
? sizeof(SIGCONTEXT
) : 4 * strlen(args
) );
1572 if (reg_func
) /* Push the address of the context struct */
1573 printf( "\tpushl %%esp\n" );
1575 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1577 printf( "\taddl $24,%%ebp\n" );
1579 /* Print the debug information before the call */
1583 printf( "\tpushl %%eax\n" );
1584 printf( "\tpushl $Profile_%s\n", profile
);
1585 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1586 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1587 printf( "\tpopl %%eax\n" );
1588 printf( "\tpopl %%eax\n" );
1589 printf( "\tpopl %%eax\n" );
1592 /* Call the entry point */
1594 printf( "\tcall %%eax\n" );
1596 /* Print the debug information after the call */
1600 printf( "\tpushl %%eax\n" );
1601 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1602 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1603 printf( "\tpopl %%eax\n" );
1604 printf( "\tpopl %%eax\n" );
1607 /* Restore the 16-bit stack */
1610 printf( "\tdata16\n");
1612 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1613 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1615 printf( "\tdata16\n");
1617 printf( "\tpopw " PREFIX
"IF1632_Saved16_ss\n" );
1619 printf( "\tdata16\n");
1621 printf( "\tpopw " PREFIX
"IF1632_Saved16_sp\n" );
1625 /* Restore registers from the context structure */
1628 /* Calc the arguments size */
1642 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1647 /* Restore ds and es */
1648 printf( "\tpopw %%es\n" );
1649 printf( "\tpopw %%ds\n" );
1651 /* Remove the entry point from the stack */
1652 /* (we don't use add to avoid modifying the carry flag) */
1653 printf( "\tpopl %%ebp\n" );
1657 /* Restore ds and es */
1658 printf( "\tpopw %%bp\n" ); /* Remove ip */
1659 printf( "\tpopl %%ebp\n" ); /* Remove ds and cs */
1660 printf( "\tmovw %%bp,%%ds\n" ); /* Restore ds */
1661 printf( "\tpopw %%es\n" ); /* Restore es */
1663 if (short_ret
) printf( "\tpopl %%edx\n" ); /* Restore edx */
1666 /* Get the return value into dx:ax */
1667 printf( "\tpushl %%eax\n" );
1668 printf( "\tpopw %%ax\n" );
1669 printf( "\tpopw %%dx\n" );
1670 /* Remove API entry point */
1671 printf( "\taddl $4,%%esp\n" );
1677 printf( "\tpopw %%bp\n" );
1679 /* Remove the arguments and return */
1683 printf( "\t.byte 0x66\n" );
1684 printf( "\tlret $%d\n", argsize
);
1688 printf( "\t.byte 0x66\n" );
1689 printf( "\tlret\n" );
1694 /*******************************************************************
1697 * Build a Wine-to-16-bit callback function.
1699 * Stack frame of the callback function:
1703 * (ebp+12) func to call
1704 * (ebp+8) code selector
1705 * (ebp+4) return address
1706 * (ebp) previous ebp
1708 * Prototypes for the CallTo16 functions:
1709 * extern WORD CallTo16_word_xxx( FARPROC16 func, args... );
1710 * extern LONG CallTo16_long_xxx( FARPROC16 func, args... );
1711 * extern void CallTo16_regs_( FARPROC16 func, WORD ds, WORD es, WORD bp,
1712 * WORD ax, WORD bx, WORD cx, WORD dx,
1713 * WORD si, WORD di );
1715 static void BuildCallTo16Func( char *profile
)
1719 char *args
= profile
+ 5;
1721 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1722 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= short_ret
= 1;
1723 else if (strncmp( "long_", profile
, 5 ))
1725 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1729 /* Function header */
1731 printf( "/**********\n" );
1732 printf( " * " PREFIX
"CallTo16_%s\n", profile
);
1733 printf( " **********/\n" );
1734 printf( "\t.align 4\n" );
1735 printf( "\t.globl " PREFIX
"CallTo16_%s\n\n", profile
);
1736 printf( PREFIX
"CallTo16_%s:\n", profile
);
1738 /* Push code selector before return address to simulate a lcall */
1740 printf( "\tpopl %%eax\n" );
1741 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR
);
1742 printf( "\tpushl %%eax\n" );
1746 printf( "\tpushl %%ebp\n" );
1747 printf( "\tmovl %%esp,%%ebp\n" );
1749 /* Save the 32-bit registers */
1751 printf( "\tpushl %%ebx\n" );
1752 printf( "\tpushl %%ecx\n" );
1753 printf( "\tpushl %%edx\n" );
1754 printf( "\tpushl %%esi\n" );
1755 printf( "\tpushl %%edi\n" );
1757 /* Save the 32-bit stack */
1759 printf( "\tpushl " PREFIX
"IF1632_Saved32_esp\n" );
1760 printf( "\tmovl %%esp," PREFIX
"IF1632_Saved32_esp\n" );
1761 printf( "\tmovl %%ebp,%%ebx\n" );
1763 /* Print debugging info */
1767 /* Push the address of the first argument */
1768 printf( "\tmovl %%ebx,%%eax\n" );
1769 printf( "\taddl $12,%%eax\n" );
1770 printf( "\tpushl $%d\n", reg_func
? 8 : strlen(args
) );
1771 printf( "\tpushl %%eax\n" );
1772 printf( "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1773 printf( "\tpopl %%eax\n" );
1774 printf( "\tpopl %%eax\n" );
1777 /* Switch to the 16-bit stack */
1780 printf("\tdata16\n");
1782 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1783 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1785 /* Transfer the arguments */
1789 /* Get the registers. ebx is handled later on. */
1790 printf( "\tpushw 20(%%ebx)\n" );
1791 printf( "\tpopw %%es\n" );
1792 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1793 printf( "\tmovl 28(%%ebx),%%eax\n" );
1794 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1795 printf( "\tmovl 40(%%ebx),%%edx\n" );
1796 printf( "\tmovl 44(%%ebx),%%esi\n" );
1797 printf( "\tmovl 48(%%ebx),%%edi\n" );
1799 else /* not a register function */
1801 int pos
= 16; /* first argument position */
1803 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1804 printf( "\tmovzwl %%sp,%%ebp\n" );
1805 printf( "\taddw $16,%%bp\n" );
1811 case 'w': /* word */
1812 printf( "\tpushw %d(%%ebx)\n", pos
);
1814 case 'l': /* long */
1815 printf( "\tpushl %d(%%ebx)\n", pos
);
1822 /* Push the return address */
1824 printf( "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1825 short_ret
? "word" : "long" );
1827 /* Push the called routine address */
1829 printf( "\tpushl 12(%%ebx)\n" );
1831 /* Get the 16-bit ds */
1835 printf( "\tpushw 16(%%ebx)\n" );
1836 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1837 printf( "\tpopw %%ds\n" );
1841 /* Get previous ds from the 16-bit stack and */
1842 /* set ax equal to ds for window procedures. */
1843 printf( "\tmovw -10(%%ebp),%%ax\n" );
1845 printf( "\tdata16\n");
1847 printf( "\tmovw %%ax,%%ds\n" );
1850 /* Jump to the called routine */
1852 printf( "\t.byte 0x66\n" );
1853 printf( "\tlret\n" );
1857 /*******************************************************************
1860 * Build the return code for 16-bit callbacks
1862 static void BuildRet16Func()
1864 printf( "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1865 printf( "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1867 /* Put return value into eax */
1869 printf( PREFIX
"CALLTO16_Ret_long:\n" );
1870 printf( "\tpushw %%dx\n" );
1871 printf( "\tpushw %%ax\n" );
1872 printf( "\tpopl %%eax\n" );
1873 printf( PREFIX
"CALLTO16_Ret_word:\n" );
1875 /* Restore 32-bit segment registers */
1877 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR
);
1879 printf( "\tdata16\n");
1881 printf( "\tmovw %%bx,%%ds\n" );
1883 printf( "\tdata16\n");
1885 printf( "\tmovw %%bx,%%es\n" );
1887 printf( "\tdata16\n");
1889 printf( "\tmovw %%bx,%%ss\n" );
1891 /* Restore the 32-bit stack */
1893 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
1894 printf( "\tpopl " PREFIX
"IF1632_Saved32_esp\n" );
1896 /* Restore the 32-bit registers */
1898 printf( "\tpopl %%edi\n" );
1899 printf( "\tpopl %%esi\n" );
1900 printf( "\tpopl %%edx\n" );
1901 printf( "\tpopl %%ecx\n" );
1902 printf( "\tpopl %%ebx\n" );
1904 /* Return to caller */
1906 printf( "\tpopl %%ebp\n" );
1907 printf( "\tlret\n" );
1909 /* Declare the return address variables */
1911 printf( "\t.data\n" );
1912 printf( "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
1913 printf( "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
1914 printf( PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
1915 printf( PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
1916 printf( "\t.text\n" );
1920 /*******************************************************************
1921 * BuildCallFrom32Func
1923 * Build a 32-bit-to-Wine call-back function.
1924 * 'args' is the number of dword arguments.
1932 * (ebp-4) entry point
1935 static void BuildCallFrom32Func( const char *profile
)
1939 if (!strncmp( profile
, "stdcall", 7 ))
1942 args
= atoi( profile
+ 8 );
1944 else if (!strncmp( profile
, "cdecl", 5 ))
1947 args
= atoi( profile
+ 6 );
1951 fprintf( stderr
, "Invalid function profile '%s'\n", profile
);
1955 /* Function header */
1957 printf( "/**********\n" );
1958 printf( " * " PREFIX
"CallFrom32_%s\n", profile
);
1959 printf( " **********/\n" );
1960 printf( "\t.align 4\n" );
1961 printf( "\t.globl " PREFIX
"CallFrom32_%s\n\n", profile
);
1962 printf( PREFIX
"CallFrom32_%s:\n", profile
);
1966 printf( "\tleal 8(%%esp),%%ebp\n" );
1968 /* Print the debugging info */
1972 printf( "\tpushl $%d\n", args
);
1973 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom32\n" );
1974 printf( "\tadd $4, %%esp\n" );
1977 /* Transfer the arguments */
1982 for (i
= args
; i
> 0; i
--) printf( "\tpushl %d(%%ebp)\n", 4 * i
+ 4 );
1986 /* Push the address of the arguments. The called function will */
1987 /* ignore this if it really takes no arguments. */
1988 printf( "\tleal 8(%%ebp),%%eax\n" );
1989 printf( "\tpushl %%eax\n" );
1992 /* Call the function */
1994 printf( "\tcall -4(%%ebp)\n" );
1996 /* Print the debugging info */
2000 printf( "\tadd $%d,%%esp\n", args
? (args
* 4) : 4 );
2001 printf( "\tpushl %%eax\n" );
2002 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom32Ret\n" );
2003 printf( "\tpopl %%eax\n" );
2006 printf( "\tmovl %%ebp,%%esp\n" );
2007 printf( "\tpopl %%ebp\n" );
2009 /* Return, removing arguments */
2011 if (args
&& stdcall) printf( "\tret $%d\n", args
* 4 );
2012 else printf( "\tret\n" );
2016 /*******************************************************************
2019 * Build a Wine-to-32-bit callback function.
2021 * Stack frame of the callback function:
2025 * (ebp+8) func to call
2026 * (ebp+4) return address
2027 * (ebp) previous ebp
2029 * Prototype for the CallTo32 functions:
2030 * extern LONG CallTo32_nn( FARPROC32 func, args... );
2032 static void BuildCallTo32Func( int args
)
2034 /* Function header */
2036 printf( "/**********\n" );
2037 printf( " * " PREFIX
"CallTo32_%d\n", args
);
2038 printf( " **********/\n" );
2039 printf( "\t.align 4\n" );
2040 printf( "\t.globl " PREFIX
"CallTo32_%d\n\n", args
);
2041 printf( PREFIX
"CallTo32_%d:\n", args
);
2045 printf( "\tpushl %%ebp\n" );
2046 printf( "\tmovl %%esp,%%ebp\n" );
2048 /* Transfer arguments */
2053 for (i
= args
; i
> 0; i
--) printf( "\tpushl %d(%%ebp)\n", 4 * i
+ 8 );
2056 /* Print the debugging output */
2060 printf( "\tpushl $%d\n", args
);
2061 printf( "\tpushl 8(%%ebp)\n" );
2062 printf( "\tcall " PREFIX
"RELAY_DebugCallTo32\n" );
2063 printf( "\taddl $8,%%esp\n" );
2066 /* Call the function */
2068 printf( "\tcall 8(%%ebp)\n" );
2070 /* Return to Wine */
2072 printf( "\tmovl %%ebp,%%esp\n" );
2073 printf( "\tpopl %%ebp\n" );
2074 printf( "\tret\n" );
2078 static void usage(void)
2080 fprintf(stderr
, "usage: build -spec SPECNAMES\n"
2081 " build -callfrom16 FUNCTION_PROFILES\n"
2082 " build -callto16 FUNCTION_PROFILES\n"
2083 " build -callfrom32 FUNCTION_PROFILES\n"
2084 " build -callto32 FUNCTION_PROFILES\n" );
2089 int main(int argc
, char **argv
)
2093 if (argc
<= 2) usage();
2095 if (!strcmp( argv
[1], "-spec" ))
2097 for (i
= 2; i
< argc
; i
++) BuildSpecFiles( argv
[i
] );
2099 else if (!strcmp( argv
[1], "-callfrom16" )) /* 16-bit-to-Wine callbacks */
2103 printf( "/* File generated automatically. Do not edit! */\n\n" );
2104 printf( "\t.text\n" );
2106 /* Build the 32-bit large stack callback */
2108 BuildCall32LargeStack();
2110 /* Build the callback functions */
2112 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( argv
[i
] );
2114 /* Output the argument debugging strings */
2118 printf( "/* Argument strings */\n" );
2119 for (i
= 2; i
< argc
; i
++)
2121 printf( "Profile_%s:\n", argv
[i
] );
2122 printf( "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
2126 else if (!strcmp( argv
[1], "-callto16" )) /* Wine-to-16-bit callbacks */
2130 printf( "/* File generated automatically. Do not edit! */\n\n" );
2131 printf( "\t.text\n" );
2132 printf( "\t.globl " PREFIX
"CALLTO16_Start\n" );
2133 printf( PREFIX
"CALLTO16_Start:\n" );
2135 /* Build the callback functions */
2137 for (i
= 2; i
< argc
; i
++) BuildCallTo16Func( argv
[i
] );
2139 /* Output the 16-bit return code */
2143 printf( "\t.globl " PREFIX
"CALLTO16_End\n" );
2144 printf( PREFIX
"CALLTO16_End:\n" );
2146 else if (!strcmp( argv
[1], "-callfrom32" )) /* 32-bit-to-Wine callbacks */
2150 printf( "/* File generated automatically. Do not edit! */\n\n" );
2151 printf( "\t.text\n" );
2153 /* Build the callback functions */
2155 for (i
= 2; i
< argc
; i
++) BuildCallFrom32Func( argv
[i
] );
2157 else if (!strcmp( argv
[1], "-callto32" )) /* Wine-to-32-bit callbacks */
2161 printf( "/* File generated automatically. Do not edit! */\n\n" );
2162 printf( "\t.text\n" );
2164 /* Build the callback functions */
2166 for (i
= 2; i
< argc
; i
++) BuildCallTo32Func( atoi(argv
[i
]) );
2173 #endif /* WINELIB */