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
+1 ); /* Size */
1093 printf( "\t.long Code_Start\n" ); /* Code start */
1094 printf( "\t.long Functions\n" ); /* Functions */
1095 printf( "\t.long FuncNames\n" ); /* Function names */
1099 /*******************************************************************
1102 * Build a Win16 assembly file from a spec file.
1104 static void BuildSpec16Files(void)
1108 int code_offset
, data_offset
, module_size
;
1109 unsigned char *data
;
1111 data
= (unsigned char *)xmalloc( 0x10000 );
1112 memset( data
, 0, 16 );
1115 printf( "/* File generated automatically; do not edit! */\n" );
1116 printf( "\t.text\n" );
1117 printf( "Code_Start:\n" );
1120 odp
= OrdinalDefinitions
;
1121 for (i
= 0; i
<= Limit
; i
++, odp
++)
1126 odp
->offset
= 0xffff;
1130 odp
->offset
= LOWORD(odp
->u
.abs
.value
);
1134 odp
->offset
= data_offset
;
1135 data_offset
+= StoreVariableCode( data
+ data_offset
, 1, odp
);
1139 odp
->offset
= data_offset
;
1140 data_offset
+= StoreVariableCode( data
+ data_offset
, 2, odp
);
1144 odp
->offset
= data_offset
;
1145 data_offset
+= StoreVariableCode( data
+ data_offset
, 4, odp
);
1149 printf( "/* %s.%d */\n", DLLName
, i
);
1150 printf( "\tmovw $%d,%%ax\n", LOWORD(odp
->u
.ret
.ret_value
) );
1151 printf( "\tmovw $%d,%%dx\n", HIWORD(odp
->u
.ret
.ret_value
) );
1152 printf( "\t.byte 0x66\n");
1153 if (odp
->u
.ret
.arg_size
!= 0)
1154 printf( "\tlret $%d\n\n", odp
->u
.ret
.arg_size
);
1157 printf( "\tlret\n");
1159 printf( "\tnop\n\n");
1161 odp
->offset
= code_offset
;
1162 code_offset
+= 12; /* Assembly code is 12 bytes long */
1167 case TYPE_PASCAL_16
:
1169 printf( "/* %s.%d */\n", DLLName
, i
);
1170 printf( "\tpushw %%bp\n" );
1171 printf( "\tpushl $" PREFIX
"%s\n", odp
->u
.func
.link_name
);
1172 /* FreeBSD does not understand lcall, so do it the hard way */
1173 printf( "\t.byte 0x9a /*lcall*/\n" );
1174 printf( "\t.long " PREFIX
"CallFrom16_%s_%s\n",
1175 (odp
->type
== TYPE_REGISTER
) ? "regs" :
1176 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
1177 odp
->u
.func
.arg_types
);
1178 printf( "\t.byte 0x%02x,0x%02x\n", /* Some asms don't have .word */
1179 LOBYTE(WINE_CODE_SELECTOR
), HIBYTE(WINE_CODE_SELECTOR
) );
1180 printf( "\tnop\n" );
1181 printf( "\tnop\n\n" );
1182 odp
->offset
= code_offset
;
1183 code_offset
+= 16; /* Assembly code is 16 bytes long */
1187 fprintf(stderr
,"build: function type %d not available for Win16\n",
1193 if (!code_offset
) /* Make sure the code segment is not empty */
1195 printf( "\t.byte 0\n" );
1199 /* Output data segment */
1201 DumpBytes( data
, data_offset
, NULL
, "Data_Start" );
1203 /* Build the module */
1205 module_size
= BuildModule16( code_offset
, data_offset
);
1207 /* Output the DLL descriptor */
1209 printf( "\t.text\n" );
1210 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName
);
1211 printf( "\t.align 4\n" );
1212 printf( "\t.globl " PREFIX
"%s_Descriptor\n", DLLName
);
1213 printf( PREFIX
"%s_Descriptor:\n", DLLName
);
1214 printf( "\t.long DLLName\n" ); /* Name */
1215 printf( "\t.long Module_Start\n" ); /* Module start */
1216 printf( "\t.long %d\n", module_size
); /* Module size */
1217 printf( "\t.long Code_Start\n" ); /* Code start */
1218 printf( "\t.long Data_Start\n" ); /* Data start */
1222 /*******************************************************************
1225 * Build an assembly file from a spec file.
1227 static void BuildSpecFiles( char *specname
)
1229 SpecFp
= fopen( specname
, "r");
1232 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
1240 fprintf( stderr
, "%s: Missing 'type' declaration\n", specname
);
1252 /*******************************************************************
1253 * BuildCall32LargeStack
1255 * Build the function used to switch to the original 32-bit stack
1256 * before calling a 32-bit function from 32-bit code. This is used for
1257 * functions that need a large stack, like X bitmaps functions.
1259 * The generated function has the following prototype:
1260 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1271 static void BuildCall32LargeStack(void)
1273 /* Function header */
1275 printf( "/**********\n" );
1276 printf( " * " PREFIX
"CallTo32_LargeStack\n" );
1277 printf( " **********/\n" );
1278 printf( "\t.align 4\n" );
1279 printf( "\t.globl " PREFIX
"CallTo32_LargeStack\n\n" );
1280 printf( PREFIX
"CallTo32_LargeStack:\n" );
1284 printf( "\tpushl %%ebp\n" );
1285 printf( "\tmovl %%esp,%%ebp\n" );
1287 /* Save registers */
1289 printf( "\tpushl %%ecx\n" );
1290 printf( "\tpushl %%esi\n" );
1291 printf( "\tpushl %%edi\n" );
1293 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1295 printf( "\tmovl " PREFIX
"IF1632_Original32_esp, %%eax\n" );
1296 printf( "\torl %%eax,%%eax\n" );
1297 printf( "\tje no_orig_esp\n" );
1298 printf( "\tmovl %%eax,%%esp\n" );
1299 printf( "no_orig_esp:\n" );
1301 /* Transfer the arguments */
1303 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1304 printf( "\torl %%ecx,%%ecx\n" );
1305 printf( "\tje no_args\n" );
1306 printf( "\tleal 16(%%ebp),%%esi\n" );
1307 printf( "\tshll $2,%%ecx\n" );
1308 printf( "\tsubl %%ecx,%%esp\n" );
1309 printf( "\tmovl %%esp,%%edi\n" );
1310 printf( "\tshrl $2,%%ecx\n" );
1311 printf( "\tcld\n" );
1312 printf( "\trep; movsl\n" );
1313 printf( "no_args:\n" );
1315 /* Call the function */
1317 printf( "\tcall 8(%%ebp)\n" );
1319 /* Switch back to the normal stack */
1321 printf( "\tleal -12(%%ebp),%%esp\n" );
1323 /* Restore registers and return */
1325 printf( "\tpopl %%edi\n" );
1326 printf( "\tpopl %%esi\n" );
1327 printf( "\tpopl %%ecx\n" );
1328 printf( "\tpopl %%ebp\n" );
1329 printf( "\tret\n" );
1333 /*******************************************************************
1334 * TransferArgs16To32
1336 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1337 * The 16-bit stack layout is:
1345 static int TransferArgs16To32( char *args
)
1347 int i
, pos16
, pos32
;
1349 /* Save ebx first */
1351 printf( "\tpushl %%ebx\n" );
1353 /* Get the 32-bit stack pointer */
1355 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1357 /* Copy the arguments */
1359 pos16
= 6; /* skip bp and return address */
1362 for (i
= strlen(args
); i
> 0; i
--)
1367 case 'w': /* word */
1368 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1369 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1373 case 's': /* s_word */
1374 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16
);
1375 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1379 case 'l': /* long */
1380 printf( "\tmovl %d(%%ebp),%%eax\n", pos16
);
1381 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1386 /* Get the selector */
1387 printf( "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
1388 /* Get the selector base */
1389 printf( "\tandl $0xfff8,%%eax\n" );
1390 printf( "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
1391 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
1392 /* Add the offset */
1393 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
1394 printf( "\taddl %%eax,%d(%%ebx)\n", pos32
);
1399 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
1405 printf( "\tpopl %%ebx\n" );
1407 return pos16
- 6; /* Return the size of the 16-bit args */
1411 /*******************************************************************
1414 * Build the context structure on the 32-bit stack.
1415 * The only valid registers in the context structure are:
1416 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1418 static void BuildContext(void)
1420 /* Save ebx first */
1422 printf( "\tpushl %%ebx\n" );
1424 /* Get the 32-bit stack pointer */
1426 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1428 /* Store the registers */
1430 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EBX
) ); /* Get ebx from stack*/
1431 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(EAX
) );
1432 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(ECX
) );
1433 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(EDX
) );
1434 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(ESI
) );
1435 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(EDI
) );
1436 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1437 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(DS
) );
1438 printf( "\tmovw -6(%%ebp),%%ax\n" ); /* Get saved es from stack */
1439 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(ES
) );
1440 printf( "\tpushfl\n" );
1441 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EFL
) );
1445 /*******************************************************************
1448 * Restore the registers from the context structure
1450 static void RestoreContext(void)
1452 /* Get the 32-bit stack pointer */
1454 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
1456 /* Restore the registers */
1458 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(ECX
) );
1459 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(EDX
) );
1460 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(ESI
) );
1461 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(EDI
) );
1462 printf( "\tpopl %%eax\n" ); /* Remove old ds and ip from stack */
1463 printf( "\tpopl %%eax\n" ); /* Remove old cs and es from stack */
1464 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(DS
) ); /* Push new ds */
1465 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(ES
) ); /* Push new es */
1466 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFL
) );
1467 printf( "\tpopfl\n" );
1468 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(EAX
) );
1469 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(EBX
) );
1473 /*******************************************************************
1474 * BuildCallFrom16Func
1476 * Build a 16-bit-to-Wine callback function. The syntax of the function
1477 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1478 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1479 * 'l'=long, 'p'=pointer).
1480 * For register functions, the arguments are ignored, but they are still
1481 * removed from the stack upon return.
1483 * Stack layout upon entry to the callback function:
1485 * (sp+18) word first 16-bit arg
1489 * (sp+8) long 32-bit entry point
1490 * (sp+6) word high word of cs (always 0, used to store es)
1491 * (sp+4) word low word of cs of 16-bit entry point
1492 * (sp+2) word high word of ip (always 0, used to store ds)
1493 * (sp) word low word of ip of 16-bit entry point
1496 static void BuildCallFrom16Func( char *profile
)
1501 char *args
= profile
+ 5;
1503 /* Parse function type */
1505 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1506 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
1507 else if (strncmp( "long_", profile
, 5 ))
1509 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1513 /* Function header */
1515 printf( "/**********\n" );
1516 printf( " * " PREFIX
"CallFrom16_%s\n", profile
);
1517 printf( " **********/\n" );
1518 printf( "\t.align 4\n" );
1519 printf( "\t.globl " PREFIX
"CallFrom16_%s\n\n", profile
);
1520 printf( PREFIX
"CallFrom16_%s:\n", profile
);
1522 /* Setup bp to point to its copy on the stack */
1524 printf( "\tmovzwl %%sp,%%ebp\n" );
1525 printf( "\taddw $12,%%bp\n" );
1527 /* Save 16-bit ds and es */
1529 /* Stupid FreeBSD assembler doesn't know these either */
1530 /* printf( "\tmovw %%ds,-10(%%ebp)\n" ); */
1531 printf( "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1532 /* printf( "\tmovw %%es,-6(%%ebp)\n" ); */
1533 printf( "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1535 /* Restore 32-bit ds and es */
1537 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR
, WINE_DATA_SELECTOR
);
1538 printf( "\tpopw %%ds\n" );
1539 printf( "\tpopw %%es\n" );
1542 /* Save the 16-bit stack */
1544 printf( "\tpushw " PREFIX
"IF1632_Saved16_sp\n" );
1545 printf( "\tpushw " PREFIX
"IF1632_Saved16_ss\n" );
1547 printf("\tdata16\n");
1549 printf( "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss\n" );
1550 printf( "\tmovw %%sp," PREFIX
"IF1632_Saved16_sp\n" );
1552 /* Transfer the arguments */
1554 if (reg_func
) BuildContext();
1555 else if (*args
) argsize
= TransferArgs16To32( args
);
1557 /* Get the address of the API function */
1559 printf( "\tmovl -4(%%ebp),%%eax\n" );
1561 /* If necessary, save %edx over the API function address */
1563 if (!reg_func
&& short_ret
)
1564 printf( "\tmovl %%edx,-4(%%ebp)\n" );
1566 /* Switch to the 32-bit stack */
1568 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebp\n" );
1569 printf( "\tpushw %%ds\n" );
1570 printf( "\tpopw %%ss\n" );
1571 printf( "\tleal -%d(%%ebp),%%esp\n",
1572 reg_func
? sizeof(SIGCONTEXT
) : 4 * strlen(args
) );
1573 if (reg_func
) /* Push the address of the context struct */
1574 printf( "\tpushl %%esp\n" );
1576 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1578 printf( "\taddl $24,%%ebp\n" );
1580 /* Print the debug information before the call */
1584 printf( "\tpushl %%eax\n" );
1585 printf( "\tpushl $Profile_%s\n", profile
);
1586 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1587 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom16\n" );
1588 printf( "\tpopl %%eax\n" );
1589 printf( "\tpopl %%eax\n" );
1590 printf( "\tpopl %%eax\n" );
1593 /* Call the entry point */
1595 printf( "\tcall %%eax\n" );
1597 /* Print the debug information after the call */
1601 printf( "\tpushl %%eax\n" );
1602 printf( "\tpushl $%d\n", reg_func
? 2 : (short_ret
? 1 : 0) );
1603 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom16Ret\n" );
1604 printf( "\tpopl %%eax\n" );
1605 printf( "\tpopl %%eax\n" );
1608 /* Restore the 16-bit stack */
1611 printf( "\tdata16\n");
1613 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1614 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1616 printf( "\tdata16\n");
1618 printf( "\tpopw " PREFIX
"IF1632_Saved16_ss\n" );
1620 printf( "\tdata16\n");
1622 printf( "\tpopw " PREFIX
"IF1632_Saved16_sp\n" );
1626 /* Restore registers from the context structure */
1629 /* Calc the arguments size */
1643 fprintf( stderr
, "Unknown arg type '%c'\n", *args
);
1648 /* Restore ds and es */
1649 printf( "\tpopw %%es\n" );
1650 printf( "\tpopw %%ds\n" );
1652 /* Remove the entry point from the stack */
1653 /* (we don't use add to avoid modifying the carry flag) */
1654 printf( "\tpopl %%ebp\n" );
1658 /* Restore ds and es */
1659 printf( "\tpopw %%bp\n" ); /* Remove ip */
1660 printf( "\tpopl %%ebp\n" ); /* Remove ds and cs */
1661 printf( "\tmovw %%bp,%%ds\n" ); /* Restore ds */
1662 printf( "\tpopw %%es\n" ); /* Restore es */
1664 if (short_ret
) printf( "\tpopl %%edx\n" ); /* Restore edx */
1667 /* Get the return value into dx:ax */
1668 printf( "\tpushl %%eax\n" );
1669 printf( "\tpopw %%ax\n" );
1670 printf( "\tpopw %%dx\n" );
1671 /* Remove API entry point */
1672 printf( "\taddl $4,%%esp\n" );
1678 printf( "\tpopw %%bp\n" );
1680 /* Remove the arguments and return */
1684 printf( "\t.byte 0x66\n" );
1685 printf( "\tlret $%d\n", argsize
);
1689 printf( "\t.byte 0x66\n" );
1690 printf( "\tlret\n" );
1695 /*******************************************************************
1698 * Build a Wine-to-16-bit callback function.
1700 * Stack frame of the callback function:
1704 * (ebp+12) func to call
1705 * (ebp+8) code selector
1706 * (ebp+4) return address
1707 * (ebp) previous ebp
1709 * Prototypes for the CallTo16 functions:
1710 * extern WORD CallTo16_word_xxx( FARPROC16 func, args... );
1711 * extern LONG CallTo16_long_xxx( FARPROC16 func, args... );
1712 * extern void CallTo16_regs_( FARPROC16 func, WORD ds, WORD es, WORD bp,
1713 * WORD ax, WORD bx, WORD cx, WORD dx,
1714 * WORD si, WORD di );
1716 static void BuildCallTo16Func( char *profile
)
1720 char *args
= profile
+ 5;
1722 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1723 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= short_ret
= 1;
1724 else if (strncmp( "long_", profile
, 5 ))
1726 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1730 /* Function header */
1732 printf( "/**********\n" );
1733 printf( " * " PREFIX
"CallTo16_%s\n", profile
);
1734 printf( " **********/\n" );
1735 printf( "\t.align 4\n" );
1736 printf( "\t.globl " PREFIX
"CallTo16_%s\n\n", profile
);
1737 printf( PREFIX
"CallTo16_%s:\n", profile
);
1739 /* Push code selector before return address to simulate a lcall */
1741 printf( "\tpopl %%eax\n" );
1742 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR
);
1743 printf( "\tpushl %%eax\n" );
1747 printf( "\tpushl %%ebp\n" );
1748 printf( "\tmovl %%esp,%%ebp\n" );
1750 /* Save the 32-bit registers */
1752 printf( "\tpushl %%ebx\n" );
1753 printf( "\tpushl %%ecx\n" );
1754 printf( "\tpushl %%edx\n" );
1755 printf( "\tpushl %%esi\n" );
1756 printf( "\tpushl %%edi\n" );
1758 /* Save the 32-bit stack */
1760 printf( "\tpushl " PREFIX
"IF1632_Saved32_esp\n" );
1761 printf( "\tmovl %%esp," PREFIX
"IF1632_Saved32_esp\n" );
1762 printf( "\tmovl %%ebp,%%ebx\n" );
1764 /* Print debugging info */
1768 /* Push the address of the first argument */
1769 printf( "\tmovl %%ebx,%%eax\n" );
1770 printf( "\taddl $12,%%eax\n" );
1771 printf( "\tpushl $%d\n", reg_func
? 8 : strlen(args
) );
1772 printf( "\tpushl %%eax\n" );
1773 printf( "\tcall " PREFIX
"RELAY_DebugCallTo16\n" );
1774 printf( "\tpopl %%eax\n" );
1775 printf( "\tpopl %%eax\n" );
1778 /* Switch to the 16-bit stack */
1781 printf("\tdata16\n");
1783 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1784 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1786 /* Transfer the arguments */
1790 /* Get the registers. ebx is handled later on. */
1791 printf( "\tpushw 20(%%ebx)\n" );
1792 printf( "\tpopw %%es\n" );
1793 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1794 printf( "\tmovl 28(%%ebx),%%eax\n" );
1795 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1796 printf( "\tmovl 40(%%ebx),%%edx\n" );
1797 printf( "\tmovl 44(%%ebx),%%esi\n" );
1798 printf( "\tmovl 48(%%ebx),%%edi\n" );
1800 else /* not a register function */
1802 int pos
= 16; /* first argument position */
1804 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1805 printf( "\tmovzwl %%sp,%%ebp\n" );
1806 printf( "\taddw $16,%%bp\n" );
1812 case 'w': /* word */
1813 printf( "\tpushw %d(%%ebx)\n", pos
);
1815 case 'l': /* long */
1816 printf( "\tpushl %d(%%ebx)\n", pos
);
1823 /* Push the return address */
1825 printf( "\tpushl " PREFIX
"CALLTO16_RetAddr_%s\n",
1826 short_ret
? "word" : "long" );
1828 /* Push the called routine address */
1830 printf( "\tpushl 12(%%ebx)\n" );
1832 /* Get the 16-bit ds */
1836 printf( "\tpushw 16(%%ebx)\n" );
1837 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1838 printf( "\tpopw %%ds\n" );
1842 /* Get previous ds from the 16-bit stack and */
1843 /* set ax equal to ds for window procedures. */
1844 printf( "\tmovw -10(%%ebp),%%ax\n" );
1846 printf( "\tdata16\n");
1848 printf( "\tmovw %%ax,%%ds\n" );
1851 /* Jump to the called routine */
1853 printf( "\t.byte 0x66\n" );
1854 printf( "\tlret\n" );
1858 /*******************************************************************
1861 * Build the return code for 16-bit callbacks
1863 static void BuildRet16Func()
1865 printf( "\t.globl " PREFIX
"CALLTO16_Ret_word\n" );
1866 printf( "\t.globl " PREFIX
"CALLTO16_Ret_long\n" );
1868 /* Put return value into eax */
1870 printf( PREFIX
"CALLTO16_Ret_long:\n" );
1871 printf( "\tpushw %%dx\n" );
1872 printf( "\tpushw %%ax\n" );
1873 printf( "\tpopl %%eax\n" );
1874 printf( PREFIX
"CALLTO16_Ret_word:\n" );
1876 /* Restore 32-bit segment registers */
1878 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR
);
1880 printf( "\tdata16\n");
1882 printf( "\tmovw %%bx,%%ds\n" );
1884 printf( "\tdata16\n");
1886 printf( "\tmovw %%bx,%%es\n" );
1888 printf( "\tdata16\n");
1890 printf( "\tmovw %%bx,%%ss\n" );
1892 /* Restore the 32-bit stack */
1894 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
1895 printf( "\tpopl " PREFIX
"IF1632_Saved32_esp\n" );
1897 /* Restore the 32-bit registers */
1899 printf( "\tpopl %%edi\n" );
1900 printf( "\tpopl %%esi\n" );
1901 printf( "\tpopl %%edx\n" );
1902 printf( "\tpopl %%ecx\n" );
1903 printf( "\tpopl %%ebx\n" );
1905 /* Return to caller */
1907 printf( "\tpopl %%ebp\n" );
1908 printf( "\tlret\n" );
1910 /* Declare the return address variables */
1912 printf( "\t.data\n" );
1913 printf( "\t.globl " PREFIX
"CALLTO16_RetAddr_word\n" );
1914 printf( "\t.globl " PREFIX
"CALLTO16_RetAddr_long\n" );
1915 printf( PREFIX
"CALLTO16_RetAddr_word:\t.long 0\n" );
1916 printf( PREFIX
"CALLTO16_RetAddr_long:\t.long 0\n" );
1917 printf( "\t.text\n" );
1921 /*******************************************************************
1922 * BuildCallFrom32Func
1924 * Build a 32-bit-to-Wine call-back function.
1925 * 'args' is the number of dword arguments.
1933 * (ebp-4) entry point
1936 static void BuildCallFrom32Func( const char *profile
)
1940 if (!strncmp( profile
, "stdcall", 7 ))
1943 args
= atoi( profile
+ 8 );
1945 else if (!strncmp( profile
, "cdecl", 5 ))
1948 args
= atoi( profile
+ 6 );
1952 fprintf( stderr
, "Invalid function profile '%s'\n", profile
);
1956 /* Function header */
1958 printf( "/**********\n" );
1959 printf( " * " PREFIX
"CallFrom32_%s\n", profile
);
1960 printf( " **********/\n" );
1961 printf( "\t.align 4\n" );
1962 printf( "\t.globl " PREFIX
"CallFrom32_%s\n\n", profile
);
1963 printf( PREFIX
"CallFrom32_%s:\n", profile
);
1967 printf( "\tleal 8(%%esp),%%ebp\n" );
1969 /* Print the debugging info */
1973 printf( "\tpushl $%d\n", args
);
1974 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom32\n" );
1975 printf( "\tadd $4, %%esp\n" );
1978 /* Transfer the arguments */
1983 for (i
= args
; i
> 0; i
--) printf( "\tpushl %d(%%ebp)\n", 4 * i
+ 4 );
1987 /* Push the address of the arguments. The called function will */
1988 /* ignore this if it really takes no arguments. */
1989 printf( "\tleal 8(%%ebp),%%eax\n" );
1990 printf( "\tpushl %%eax\n" );
1993 /* Call the function */
1995 printf( "\tcall -4(%%ebp)\n" );
1997 /* Print the debugging info */
2001 printf( "\tadd $%d,%%esp\n", args
? (args
* 4) : 4 );
2002 printf( "\tpushl %%eax\n" );
2003 printf( "\tcall " PREFIX
"RELAY_DebugCallFrom32Ret\n" );
2004 printf( "\tpopl %%eax\n" );
2007 printf( "\tmovl %%ebp,%%esp\n" );
2008 printf( "\tpopl %%ebp\n" );
2010 /* Return, removing arguments */
2012 if (args
&& stdcall) printf( "\tret $%d\n", args
* 4 );
2013 else printf( "\tret\n" );
2017 /*******************************************************************
2020 * Build a Wine-to-32-bit callback function.
2022 * Stack frame of the callback function:
2026 * (ebp+8) func to call
2027 * (ebp+4) return address
2028 * (ebp) previous ebp
2030 * Prototype for the CallTo32 functions:
2031 * extern LONG CallTo32_nn( FARPROC32 func, args... );
2033 static void BuildCallTo32Func( int args
)
2035 /* Function header */
2037 printf( "/**********\n" );
2038 printf( " * " PREFIX
"CallTo32_%d\n", args
);
2039 printf( " **********/\n" );
2040 printf( "\t.align 4\n" );
2041 printf( "\t.globl " PREFIX
"CallTo32_%d\n\n", args
);
2042 printf( PREFIX
"CallTo32_%d:\n", args
);
2046 printf( "\tpushl %%ebp\n" );
2047 printf( "\tmovl %%esp,%%ebp\n" );
2049 /* Transfer arguments */
2054 for (i
= args
; i
> 0; i
--) printf( "\tpushl %d(%%ebp)\n", 4 * i
+ 8 );
2057 /* Print the debugging output */
2061 printf( "\tpushl $%d\n", args
);
2062 printf( "\tpushl 8(%%ebp)\n" );
2063 printf( "\tcall " PREFIX
"RELAY_DebugCallTo32\n" );
2064 printf( "\taddl $8,%%esp\n" );
2067 /* Call the function */
2069 printf( "\tcall 8(%%ebp)\n" );
2071 /* Return to Wine */
2073 printf( "\tmovl %%ebp,%%esp\n" );
2074 printf( "\tpopl %%ebp\n" );
2075 printf( "\tret\n" );
2079 static void usage(void)
2081 fprintf(stderr
, "usage: build -spec SPECNAMES\n"
2082 " build -callfrom16 FUNCTION_PROFILES\n"
2083 " build -callto16 FUNCTION_PROFILES\n"
2084 " build -callfrom32 FUNCTION_PROFILES\n"
2085 " build -callto32 FUNCTION_PROFILES\n" );
2090 int main(int argc
, char **argv
)
2094 if (argc
<= 2) usage();
2096 if (!strcmp( argv
[1], "-spec" ))
2098 for (i
= 2; i
< argc
; i
++) BuildSpecFiles( argv
[i
] );
2100 else if (!strcmp( argv
[1], "-callfrom16" )) /* 16-bit-to-Wine callbacks */
2104 printf( "/* File generated automatically. Do not edit! */\n\n" );
2105 printf( "\t.text\n" );
2107 /* Build the 32-bit large stack callback */
2109 BuildCall32LargeStack();
2111 /* Build the callback functions */
2113 for (i
= 2; i
< argc
; i
++) BuildCallFrom16Func( argv
[i
] );
2115 /* Output the argument debugging strings */
2119 printf( "/* Argument strings */\n" );
2120 for (i
= 2; i
< argc
; i
++)
2122 printf( "Profile_%s:\n", argv
[i
] );
2123 printf( "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
2127 else if (!strcmp( argv
[1], "-callto16" )) /* Wine-to-16-bit callbacks */
2131 printf( "/* File generated automatically. Do not edit! */\n\n" );
2132 printf( "\t.text\n" );
2133 printf( "\t.globl " PREFIX
"CALLTO16_Start\n" );
2134 printf( PREFIX
"CALLTO16_Start:\n" );
2136 /* Build the callback functions */
2138 for (i
= 2; i
< argc
; i
++) BuildCallTo16Func( argv
[i
] );
2140 /* Output the 16-bit return code */
2144 printf( "\t.globl " PREFIX
"CALLTO16_End\n" );
2145 printf( PREFIX
"CALLTO16_End:\n" );
2147 else if (!strcmp( argv
[1], "-callfrom32" )) /* 32-bit-to-Wine callbacks */
2151 printf( "/* File generated automatically. Do not edit! */\n\n" );
2152 printf( "\t.text\n" );
2154 /* Build the callback functions */
2156 for (i
= 2; i
< argc
; i
++) BuildCallFrom32Func( argv
[i
] );
2158 else if (!strcmp( argv
[1], "-callto32" )) /* Wine-to-32-bit callbacks */
2162 printf( "/* File generated automatically. Do not edit! */\n\n" );
2163 printf( "\t.text\n" );
2165 /* Build the callback functions */
2167 for (i
= 2; i
< argc
; i
++) BuildCallTo32Func( atoi(argv
[i
]) );
2174 #endif /* WINELIB */