Release 980201
[wine/multimedia.git] / tools / build.c
blob93a97d799dc38fc5ef6a9e28dc40bd6cedcd3a7d
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
6 */
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include <unistd.h>
15 #include "windows.h"
16 #include "winnt.h"
17 #include "module.h"
18 #include "neexe.h"
19 #include "selectors.h"
20 #include "stackframe.h"
21 #include "thread.h"
23 #ifdef NEED_UNDERSCORE_PREFIX
24 # define PREFIX "_"
25 #else
26 # define PREFIX
27 #endif
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
31 #else
32 # define STRING ".ascii"
33 #endif
35 #if defined(__GNUC__) && !defined(__svr4__)
36 # define USE_STABS
37 #else
38 # undef USE_STABS
39 #endif
41 typedef enum
43 TYPE_INVALID,
44 TYPE_BYTE, /* byte variable (Win16) */
45 TYPE_WORD, /* word variable (Win16) */
46 TYPE_LONG, /* long variable (Win16) */
47 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
48 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
49 TYPE_ABS, /* absolute value (Win16) */
50 TYPE_RETURN, /* simple return value function (Win16) */
51 TYPE_REGISTER, /* register function */
52 TYPE_STUB, /* unimplemented stub */
53 TYPE_STDCALL, /* stdcall function (Win32) */
54 TYPE_CDECL, /* cdecl function (Win32) */
55 TYPE_VARARGS, /* varargs function (Win32) */
56 TYPE_EXTERN, /* external symbol (Win32) */
57 TYPE_NBTYPES
58 } ORD_TYPE;
60 static const char * const TypeNames[TYPE_NBTYPES] =
62 NULL,
63 "byte", /* TYPE_BYTE */
64 "word", /* TYPE_WORD */
65 "long", /* TYPE_LONG */
66 "pascal16", /* TYPE_PASCAL_16 */
67 "pascal", /* TYPE_PASCAL */
68 "equate", /* TYPE_ABS */
69 "return", /* TYPE_RETURN */
70 "register", /* TYPE_REGISTER */
71 "stub", /* TYPE_STUB */
72 "stdcall", /* TYPE_STDCALL */
73 "cdecl", /* TYPE_CDECL */
74 "varargs", /* TYPE_VARARGS */
75 "extern" /* TYPE_EXTERN */
78 #define MAX_ORDINALS 2048
80 /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
84 typedef enum
86 SPEC_INVALID,
87 SPEC_WIN16,
88 SPEC_WIN32
89 } SPEC_TYPE;
91 typedef struct
93 int n_values;
94 int *values;
95 } ORD_VARIABLE;
97 typedef struct
99 int n_args;
100 char arg_types[32];
101 char link_name[80];
102 } ORD_FUNCTION;
104 typedef struct
106 int arg_size;
107 int ret_value;
108 } ORD_RETURN;
110 typedef struct
112 int value;
113 } ORD_ABS;
115 typedef struct
117 char link_name[80];
118 } ORD_VARARGS;
120 typedef struct
122 char link_name[80];
123 } ORD_EXTERN;
125 typedef struct
127 ORD_TYPE type;
128 int offset;
129 int lineno;
130 char name[80];
131 union
133 ORD_VARIABLE var;
134 ORD_FUNCTION func;
135 ORD_RETURN ret;
136 ORD_ABS abs;
137 ORD_VARARGS vargs;
138 ORD_EXTERN ext;
139 } u;
140 } ORDDEF;
142 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
144 static SPEC_TYPE SpecType = SPEC_INVALID;
145 static char DLLName[80];
146 static char DLLFileName[80];
147 static int Limit = 0;
148 static int Base = MAX_ORDINALS;
149 static int DLLHeapSize = 0;
150 static char *SpecName;
151 static FILE *SpecFp;
152 static WORD Code_Selector, Data_Selector;
154 char *ParseBuffer = NULL;
155 char *ParseNext;
156 char ParseSaveChar;
157 int Line;
159 static int debugging = 1;
161 /* Offset of a structure field relative to the start of the struct */
162 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
164 /* Offset of register relative to the start of the CONTEXT struct */
165 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
167 /* Offset of the stack pointer relative to %fs:(0) */
168 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
171 static void *xmalloc (size_t size)
173 void *res;
175 res = malloc (size ? size : 1);
176 if (res == NULL)
178 fprintf (stderr, "Virtual memory exhausted.\n");
179 exit (1);
181 return res;
185 static void *xrealloc (void *ptr, size_t size)
187 void *res = realloc (ptr, size);
188 if (res == NULL)
190 fprintf (stderr, "Virtual memory exhausted.\n");
191 exit (1);
193 return res;
197 static int IsNumberString(char *s)
199 while (*s != '\0')
200 if (!isdigit(*s++))
201 return 0;
203 return 1;
206 static char *strupper(char *s)
208 char *p;
210 for(p = s; *p != '\0'; p++)
211 *p = toupper(*p);
213 return s;
216 static char * GetTokenInLine(void)
218 char *p;
219 char *token;
221 if (ParseNext != ParseBuffer)
223 if (ParseSaveChar == '\0')
224 return NULL;
225 *ParseNext = ParseSaveChar;
229 * Remove initial white space.
231 for (p = ParseNext; isspace(*p); p++)
234 if ((*p == '\0') || (*p == '#'))
235 return NULL;
238 * Find end of token.
240 token = p++;
241 if (*token != '(' && *token != ')')
242 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
243 p++;
245 ParseSaveChar = *p;
246 ParseNext = p;
247 *p = '\0';
249 return token;
252 static char * GetToken(void)
254 char *token;
256 if (ParseBuffer == NULL)
258 ParseBuffer = xmalloc(512);
259 ParseNext = ParseBuffer;
260 while (1)
262 Line++;
263 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
264 return NULL;
265 if (ParseBuffer[0] != '#')
266 break;
270 while ((token = GetTokenInLine()) == NULL)
272 ParseNext = ParseBuffer;
273 while (1)
275 Line++;
276 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
277 return NULL;
278 if (ParseBuffer[0] != '#')
279 break;
283 return token;
287 /*******************************************************************
288 * ParseVariable
290 * Parse a variable definition.
292 static int ParseVariable( ORDDEF *odp )
294 char *endptr;
295 int *value_array;
296 int n_values;
297 int value_array_size;
299 char *token = GetToken();
300 if (*token != '(')
302 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
303 SpecName, Line, token);
304 return -1;
307 n_values = 0;
308 value_array_size = 25;
309 value_array = xmalloc(sizeof(*value_array) * value_array_size);
311 while ((token = GetToken()) != NULL)
313 if (*token == ')')
314 break;
316 value_array[n_values++] = strtol(token, &endptr, 0);
317 if (n_values == value_array_size)
319 value_array_size += 25;
320 value_array = xrealloc(value_array,
321 sizeof(*value_array) * value_array_size);
324 if (endptr == NULL || *endptr != '\0')
326 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
327 SpecName, Line, token);
328 return -1;
332 if (token == NULL)
334 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
335 SpecName, Line);
336 return -1;
339 odp->u.var.n_values = n_values;
340 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
342 return 0;
346 /*******************************************************************
347 * ParseExportFunction
349 * Parse a function definition.
351 static int ParseExportFunction( ORDDEF *odp )
353 char *token;
354 int i;
356 switch(SpecType)
358 case SPEC_WIN16:
359 if (odp->type == TYPE_STDCALL)
361 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
362 SpecName, Line );
363 return -1;
365 if (odp->type == TYPE_CDECL)
367 fprintf( stderr, "%s:%d: 'cdecl' not supported for Win16\n",
368 SpecName, Line );
369 return -1;
371 break;
372 case SPEC_WIN32:
373 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
375 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
376 SpecName, Line );
377 return -1;
379 break;
380 default:
381 break;
384 token = GetToken();
385 if (*token != '(')
387 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
388 SpecName, Line, token);
389 return -1;
392 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
394 token = GetToken();
395 if (*token == ')')
396 break;
398 if (!strcmp(token, "word"))
399 odp->u.func.arg_types[i] = 'w';
400 else if (!strcmp(token, "s_word"))
401 odp->u.func.arg_types[i] = 's';
402 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
403 odp->u.func.arg_types[i] = 'l';
404 else if (!strcmp(token, "ptr"))
405 odp->u.func.arg_types[i] = 'p';
406 else if (!strcmp(token, "str"))
407 odp->u.func.arg_types[i] = 't';
408 else if (!strcmp(token, "wstr"))
409 odp->u.func.arg_types[i] = 'W';
410 else if (!strcmp(token, "segstr"))
411 odp->u.func.arg_types[i] = 'T';
412 else if (!strcmp(token, "double"))
414 odp->u.func.arg_types[i++] = 'l';
415 odp->u.func.arg_types[i] = 'l';
417 else
419 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
420 SpecName, Line, token);
421 return -1;
423 if (SpecType == SPEC_WIN32)
425 if (strcmp(token, "long") &&
426 strcmp(token, "ptr") &&
427 strcmp(token, "str") &&
428 strcmp(token, "wstr") &&
429 strcmp(token, "double"))
431 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
432 SpecName, Line, token );
433 return -1;
437 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
439 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
440 return -1;
442 odp->u.func.arg_types[i] = '\0';
443 if ((odp->type == TYPE_STDCALL) && !i)
444 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
445 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
447 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
448 SpecName, Line );
449 return -1;
451 strcpy(odp->u.func.link_name, GetToken());
452 return 0;
456 /*******************************************************************
457 * ParseEquate
459 * Parse an 'equate' definition.
461 static int ParseEquate( ORDDEF *odp )
463 char *endptr;
465 char *token = GetToken();
466 int value = strtol(token, &endptr, 0);
467 if (endptr == NULL || *endptr != '\0')
469 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
470 SpecName, Line, token);
471 return -1;
474 if (SpecType == SPEC_WIN32)
476 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
477 SpecName, Line );
478 return -1;
481 odp->u.abs.value = value;
482 return 0;
486 /*******************************************************************
487 * ParseReturn
489 * Parse a 'return' definition.
491 static int ParseReturn( ORDDEF *odp )
493 char *token;
494 char *endptr;
496 token = GetToken();
497 odp->u.ret.arg_size = strtol(token, &endptr, 0);
498 if (endptr == NULL || *endptr != '\0')
500 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
501 SpecName, Line, token);
502 return -1;
505 token = GetToken();
506 odp->u.ret.ret_value = strtol(token, &endptr, 0);
507 if (endptr == NULL || *endptr != '\0')
509 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
510 SpecName, Line, token);
511 return -1;
514 if (SpecType == SPEC_WIN32)
516 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
517 SpecName, Line );
518 return -1;
521 return 0;
525 /*******************************************************************
526 * ParseStub
528 * Parse a 'stub' definition.
530 static int ParseStub( ORDDEF *odp )
532 odp->u.func.arg_types[0] = '\0';
533 strcpy( odp->u.func.link_name, STUB_CALLBACK );
534 return 0;
538 /*******************************************************************
539 * ParseVarargs
541 * Parse an 'varargs' definition.
543 static int ParseVarargs( ORDDEF *odp )
545 char *token;
547 if (SpecType == SPEC_WIN16)
549 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
550 SpecName, Line );
551 return -1;
554 token = GetToken();
555 if (*token != '(')
557 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
558 SpecName, Line, token);
559 return -1;
561 token = GetToken();
562 if (*token != ')')
564 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
565 SpecName, Line, token);
566 return -1;
569 strcpy( odp->u.vargs.link_name, GetToken() );
570 return 0;
574 /*******************************************************************
575 * ParseExtern
577 * Parse an 'extern' definition.
579 static int ParseExtern( ORDDEF *odp )
581 if (SpecType == SPEC_WIN16)
583 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
584 SpecName, Line );
585 return -1;
587 strcpy( odp->u.ext.link_name, GetToken() );
588 return 0;
592 /*******************************************************************
593 * ParseOrdinal
595 * Parse an ordinal definition.
597 static int ParseOrdinal(int ordinal)
599 ORDDEF *odp;
600 char *token;
602 if (ordinal >= MAX_ORDINALS)
604 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
605 return -1;
607 if (ordinal > Limit) Limit = ordinal;
608 if (ordinal < Base) Base = ordinal;
610 odp = &OrdinalDefinitions[ordinal];
611 if (!(token = GetToken()))
613 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
614 return -1;
617 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
618 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
619 break;
621 if (odp->type >= TYPE_NBTYPES)
623 fprintf( stderr,
624 "%s:%d: Expected type after ordinal, found '%s' instead\n",
625 SpecName, Line, token );
626 return -1;
629 if (!(token = GetToken()))
631 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
632 return -1;
634 strcpy( odp->name, token );
635 odp->lineno = Line;
637 switch(odp->type)
639 case TYPE_BYTE:
640 case TYPE_WORD:
641 case TYPE_LONG:
642 return ParseVariable( odp );
643 case TYPE_PASCAL_16:
644 case TYPE_PASCAL:
645 case TYPE_REGISTER:
646 case TYPE_STDCALL:
647 case TYPE_CDECL:
648 return ParseExportFunction( odp );
649 case TYPE_ABS:
650 return ParseEquate( odp );
651 case TYPE_RETURN:
652 return ParseReturn( odp );
653 case TYPE_STUB:
654 return ParseStub( odp );
655 case TYPE_VARARGS:
656 return ParseVarargs( odp );
657 case TYPE_EXTERN:
658 return ParseExtern( odp );
659 default:
660 fprintf( stderr, "Should not happen\n" );
661 return -1;
666 /*******************************************************************
667 * ParseTopLevel
669 * Parse a spec file.
671 static int ParseTopLevel(void)
673 char *token;
675 while ((token = GetToken()) != NULL)
677 if (strcmp(token, "name") == 0)
679 strcpy(DLLName, GetToken());
680 strupper(DLLName);
681 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
683 else if (strcmp(token, "file") == 0)
685 strcpy(DLLFileName, GetToken());
686 strupper(DLLFileName);
688 else if (strcmp(token, "type") == 0)
690 token = GetToken();
691 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
692 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
693 else
695 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
696 SpecName, Line);
697 return -1;
700 else if (strcmp(token, "heap") == 0)
702 token = GetToken();
703 if (!IsNumberString(token))
705 fprintf(stderr, "%s:%d: Expected number after heap\n",
706 SpecName, Line);
707 return -1;
709 DLLHeapSize = atoi(token);
711 else if (IsNumberString(token))
713 int ordinal;
714 int rv;
716 ordinal = atoi(token);
717 if ((rv = ParseOrdinal(ordinal)) < 0)
718 return rv;
720 else
722 fprintf(stderr,
723 "%s:%d: Expected name, id, length or ordinal\n",
724 SpecName, Line);
725 return -1;
729 return 0;
733 /*******************************************************************
734 * StoreVariableCode
736 * Store a list of ints into a byte array.
738 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
740 int i;
742 switch(size)
744 case 1:
745 for (i = 0; i < odp->u.var.n_values; i++)
746 buffer[i] = odp->u.var.values[i];
747 break;
748 case 2:
749 for (i = 0; i < odp->u.var.n_values; i++)
750 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
751 break;
752 case 4:
753 for (i = 0; i < odp->u.var.n_values; i++)
754 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
755 break;
757 return odp->u.var.n_values * size;
761 /*******************************************************************
762 * DumpBytes
764 * Dump a byte stream into the assembly code.
766 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
767 const char *section, const char *label_start )
769 int i;
770 if (section) fprintf( outfile, "\t%s\n", section );
771 if (label_start) fprintf( outfile, "%s:\n", label_start );
772 for (i = 0; i < len; i++)
774 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
775 fprintf( outfile, "%d", *data++ );
776 if (i < len - 1)
777 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
779 fprintf( outfile, "\n" );
783 /*******************************************************************
784 * BuildModule16
786 * Build the in-memory representation of a 16-bit NE module, and dump it
787 * as a byte stream into the assembly code.
789 static int BuildModule16( FILE *outfile, int max_code_offset,
790 int max_data_offset )
792 ORDDEF *odp;
793 int i;
794 char *buffer;
795 NE_MODULE *pModule;
796 SEGTABLEENTRY *pSegment;
797 OFSTRUCT *pFileInfo;
798 BYTE *pstr, *bundle;
799 WORD *pword;
801 /* Module layout:
802 * NE_MODULE Module
803 * OFSTRUCT File information
804 * SEGTABLEENTRY Segment 1 (code)
805 * SEGTABLEENTRY Segment 2 (data)
806 * WORD[2] Resource table (empty)
807 * BYTE[2] Imported names (empty)
808 * BYTE[n] Resident names table
809 * BYTE[n] Entry table
812 buffer = xmalloc( 0x10000 );
814 pModule = (NE_MODULE *)buffer;
815 pModule->magic = IMAGE_OS2_SIGNATURE;
816 pModule->count = 1;
817 pModule->next = 0;
818 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
819 pModule->dgroup = 2;
820 pModule->heap_size = DLLHeapSize;
821 pModule->stack_size = 0;
822 pModule->ip = 0;
823 pModule->cs = 0;
824 pModule->sp = 0;
825 pModule->ss = 0;
826 pModule->seg_count = 2;
827 pModule->modref_count = 0;
828 pModule->nrname_size = 0;
829 pModule->modref_table = 0;
830 pModule->nrname_fpos = 0;
831 pModule->moveable_entries = 0;
832 pModule->alignment = 0;
833 pModule->truetype = 0;
834 pModule->os_flags = NE_OSFLAGS_WINDOWS;
835 pModule->misc_flags = 0;
836 pModule->dlls_to_init = 0;
837 pModule->nrname_handle = 0;
838 pModule->min_swap_area = 0;
839 pModule->expected_version = 0x030a;
840 pModule->module32 = 0;
841 pModule->self = 0;
842 pModule->self_loading_sel = 0;
844 /* File information */
846 pFileInfo = (OFSTRUCT *)(pModule + 1);
847 pModule->fileinfo = (int)pFileInfo - (int)pModule;
848 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
849 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
850 + strlen(DLLFileName);
851 strcpy( pFileInfo->szPathName, DLLFileName );
852 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
854 /* Segment table */
856 pSegment = (SEGTABLEENTRY *)pstr;
857 pModule->seg_table = (int)pSegment - (int)pModule;
858 pSegment->filepos = 0;
859 pSegment->size = max_code_offset;
860 pSegment->flags = 0;
861 pSegment->minsize = max_code_offset;
862 pSegment->selector = 0;
863 pSegment++;
865 pModule->dgroup_entry = (int)pSegment - (int)pModule;
866 pSegment->filepos = 0;
867 pSegment->size = max_data_offset;
868 pSegment->flags = NE_SEGFLAGS_DATA;
869 pSegment->minsize = max_data_offset;
870 pSegment->selector = 0;
871 pSegment++;
873 /* Resource table */
875 pword = (WORD *)pSegment;
876 pModule->res_table = (int)pword - (int)pModule;
877 *pword++ = 0;
878 *pword++ = 0;
880 /* Imported names table */
882 pstr = (char *)pword;
883 pModule->import_table = (int)pstr - (int)pModule;
884 *pstr++ = 0;
885 *pstr++ = 0;
887 /* Resident names table */
889 pModule->name_table = (int)pstr - (int)pModule;
890 /* First entry is module name */
891 *pstr = strlen(DLLName );
892 strcpy( pstr + 1, DLLName );
893 pstr += *pstr + 1;
894 *(WORD *)pstr = 0;
895 pstr += sizeof(WORD);
896 /* Store all ordinals */
897 odp = OrdinalDefinitions + 1;
898 for (i = 1; i <= Limit; i++, odp++)
900 if (!odp->name[0]) continue;
901 *pstr = strlen( odp->name );
902 strcpy( pstr + 1, odp->name );
903 strupper( pstr + 1 );
904 pstr += *pstr + 1;
905 *(WORD *)pstr = i;
906 pstr += sizeof(WORD);
908 *pstr++ = 0;
910 /* Entry table */
912 pModule->entry_table = (int)pstr - (int)pModule;
913 bundle = NULL;
914 odp = OrdinalDefinitions + 1;
915 for (i = 1; i <= Limit; i++, odp++)
917 int selector = 0;
919 switch (odp->type)
921 case TYPE_PASCAL:
922 case TYPE_PASCAL_16:
923 case TYPE_REGISTER:
924 case TYPE_RETURN:
925 case TYPE_STUB:
926 selector = 1; /* Code selector */
927 break;
929 case TYPE_BYTE:
930 case TYPE_WORD:
931 case TYPE_LONG:
932 selector = 2; /* Data selector */
933 break;
935 case TYPE_ABS:
936 selector = 0xfe; /* Constant selector */
937 break;
939 default:
940 selector = 0; /* Invalid selector */
941 break;
944 /* create a new bundle if necessary */
945 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
947 bundle = pstr;
948 bundle[0] = 0;
949 bundle[1] = selector;
950 pstr += 2;
953 (*bundle)++;
954 if (selector != 0)
956 *pstr++ = 1;
957 *(WORD *)pstr = odp->offset;
958 pstr += sizeof(WORD);
961 *pstr++ = 0;
963 /* Dump the module content */
965 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
966 ".data", "Module_Start" );
967 return (int)pstr - (int)pModule;
971 /*******************************************************************
972 * BuildSpec32File
974 * Build a Win32 C file from a spec file.
976 static int BuildSpec32File( char * specfile, FILE *outfile )
978 ORDDEF *odp;
979 int i, nb_names, nb_reg_funcs = 0;
981 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
982 specfile );
983 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
985 /* Output code for all stubs functions */
987 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
988 DLLName );
989 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
991 if (odp->type != TYPE_STUB) continue;
992 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
993 i, DLLName, i );
996 /* Output the DLL functions prototypes */
998 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1000 switch(odp->type)
1002 case TYPE_VARARGS:
1003 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1004 break;
1005 case TYPE_EXTERN:
1006 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1007 break;
1008 case TYPE_REGISTER:
1009 case TYPE_STDCALL:
1010 case TYPE_CDECL:
1011 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1012 break;
1013 case TYPE_INVALID:
1014 case TYPE_STUB:
1015 break;
1016 default:
1017 fprintf(stderr,"build: function type %d not available for Win32\n",
1018 odp->type);
1019 return -1;
1023 /* Output the DLL functions table */
1025 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1026 Limit - Base + 1 );
1027 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1029 switch(odp->type)
1031 case TYPE_INVALID:
1032 fprintf( outfile, " 0" );
1033 break;
1034 case TYPE_VARARGS:
1035 fprintf( outfile, " %s", odp->u.vargs.link_name );
1036 break;
1037 case TYPE_EXTERN:
1038 fprintf( outfile, " %s", odp->u.ext.link_name );
1039 break;
1040 case TYPE_REGISTER:
1041 case TYPE_STDCALL:
1042 case TYPE_CDECL:
1043 fprintf( outfile, " %s", odp->u.func.link_name);
1044 break;
1045 case TYPE_STUB:
1046 fprintf( outfile, " __stub_%d", i );
1047 break;
1048 default:
1049 return -1;
1051 if (i < Limit) fprintf( outfile, ",\n" );
1053 fprintf( outfile, "\n};\n\n" );
1055 /* Output the DLL names table */
1057 nb_names = 0;
1058 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1059 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1061 if (odp->type == TYPE_INVALID) continue;
1062 if (nb_names++) fprintf( outfile, ",\n" );
1063 fprintf( outfile, " \"%s\"", odp->name );
1065 fprintf( outfile, "\n};\n\n" );
1067 /* Output the DLL argument types */
1069 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1070 Limit - Base + 1 );
1071 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1073 unsigned int j, mask = 0;
1074 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1075 for (j = 0; odp->u.func.arg_types[j]; j++)
1077 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1078 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1080 fprintf( outfile, " %d", mask );
1081 if (i < Limit) fprintf( outfile, ",\n" );
1083 fprintf( outfile, "\n};\n\n" );
1085 /* Output the DLL ordinals table */
1087 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1088 nb_names = 0;
1089 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1091 if (odp->type == TYPE_INVALID) continue;
1092 if (nb_names++) fprintf( outfile, ",\n" );
1093 fprintf( outfile, " %d", i - Base );
1095 fprintf( outfile, "\n};\n\n" );
1097 /* Output the DLL functions arguments */
1099 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1100 Limit - Base + 1 );
1101 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1103 unsigned char args;
1104 switch(odp->type)
1106 case TYPE_STDCALL:
1107 args = (unsigned char)strlen(odp->u.func.arg_types);
1108 break;
1109 case TYPE_CDECL:
1110 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1111 break;
1112 case TYPE_REGISTER:
1113 args = 0xfe;
1114 nb_reg_funcs++;
1115 break;
1116 default:
1117 args = 0xff;
1118 break;
1120 fprintf( outfile, " 0x%02x", args );
1121 if (i < Limit) fprintf( outfile, ",\n" );
1123 fprintf( outfile, "\n};\n\n" );
1125 /* Output the DLL descriptor */
1127 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1128 DLLName );
1129 fprintf( outfile, " \"%s\",\n", DLLName );
1130 fprintf( outfile, " %d,\n", Base );
1131 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1132 fprintf( outfile, " %d,\n", nb_names );
1133 fprintf( outfile, " %d,\n", nb_reg_funcs );
1134 fprintf( outfile,
1135 " Functions,\n"
1136 " FuncNames,\n"
1137 " FuncOrdinals,\n"
1138 " FuncArgs,\n"
1139 " ArgTypes\n"
1140 "};\n" );
1141 return 0;
1145 /*******************************************************************
1146 * BuildSpec16File
1148 * Build a Win16 assembly file from a spec file.
1150 static int BuildSpec16File( char * specfile, FILE *outfile )
1152 ORDDEF *odp;
1153 int i;
1154 int code_offset, data_offset, module_size;
1155 unsigned char *data;
1157 data = (unsigned char *)xmalloc( 0x10000 );
1158 memset( data, 0, 16 );
1159 data_offset = 16;
1161 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1162 fprintf( outfile, "\t.text\n" );
1163 fprintf( outfile, "Code_Start:\n" );
1164 code_offset = 0;
1166 odp = OrdinalDefinitions;
1167 for (i = 0; i <= Limit; i++, odp++)
1169 switch (odp->type)
1171 case TYPE_INVALID:
1172 odp->offset = 0xffff;
1173 break;
1175 case TYPE_ABS:
1176 odp->offset = LOWORD(odp->u.abs.value);
1177 break;
1179 case TYPE_BYTE:
1180 odp->offset = data_offset;
1181 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1182 break;
1184 case TYPE_WORD:
1185 odp->offset = data_offset;
1186 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1187 break;
1189 case TYPE_LONG:
1190 odp->offset = data_offset;
1191 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1192 break;
1194 case TYPE_RETURN:
1195 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1196 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1197 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1198 fprintf( outfile,"\t.byte 0x66\n");
1199 if (odp->u.ret.arg_size != 0)
1200 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1201 else
1203 fprintf( outfile, "\tlret\n");
1204 fprintf( outfile, "\tnop\n");
1205 fprintf( outfile, "\tnop\n\n");
1207 odp->offset = code_offset;
1208 code_offset += 12; /* Assembly code is 12 bytes long */
1209 break;
1211 case TYPE_REGISTER:
1212 case TYPE_PASCAL:
1213 case TYPE_PASCAL_16:
1214 case TYPE_STUB:
1215 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1216 fprintf( outfile, "\tpushw %%bp\n" );
1217 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1218 /* FreeBSD does not understand lcall, so do it the hard way */
1219 fprintf( outfile, "\t.byte 0x9a\n" );
1220 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s\n",
1221 (odp->type == TYPE_REGISTER) ? "regs" :
1222 (odp->type == TYPE_PASCAL) ? "long" : "word",
1223 odp->u.func.arg_types );
1224 fprintf( outfile, "\t.long 0x%08lx\n",
1225 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1226 odp->offset = code_offset;
1227 code_offset += 16; /* Assembly code is 16 bytes long */
1228 break;
1230 default:
1231 fprintf(stderr,"build: function type %d not available for Win16\n",
1232 odp->type);
1233 return -1;
1237 if (!code_offset) /* Make sure the code segment is not empty */
1239 fprintf( outfile, "\t.byte 0\n" );
1240 code_offset++;
1243 /* Output data segment */
1245 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1247 /* Build the module */
1249 module_size = BuildModule16( outfile, code_offset, data_offset );
1251 /* Output the DLL descriptor */
1253 fprintf( outfile, "\t.text\n" );
1254 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1255 fprintf( outfile, "\t.align 4\n" );
1256 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1257 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1258 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1259 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1260 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1261 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1262 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1263 return 0;
1267 /*******************************************************************
1268 * BuildSpecFile
1270 * Build an assembly file from a spec file.
1272 static int BuildSpecFile( FILE *outfile, char *specname )
1274 SpecName = specname;
1275 SpecFp = fopen( specname, "r");
1276 if (SpecFp == NULL)
1278 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1279 return -1;
1282 if (ParseTopLevel() < 0) return -1;
1284 switch(SpecType)
1286 case SPEC_WIN16:
1287 return BuildSpec16File( specname, outfile );
1288 case SPEC_WIN32:
1289 return BuildSpec32File( specname, outfile );
1290 default:
1291 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1292 return -1;
1297 /*******************************************************************
1298 * TransferArgs16To32
1300 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1301 * The 16-bit stack layout is:
1302 * ... ...
1303 * (bp+8) arg2
1304 * (bp+6) arg1
1305 * (bp+4) cs
1306 * (bp+2) ip
1307 * (bp) bp
1309 static int TransferArgs16To32( FILE *outfile, char *args )
1311 int i, pos16, pos32;
1313 /* Copy the arguments */
1315 pos16 = 6; /* skip bp and return address */
1316 pos32 = 0;
1318 for (i = strlen(args); i > 0; i--)
1320 pos32 -= 4;
1321 switch(args[i-1])
1323 case 'w': /* word */
1324 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1325 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1326 pos16 += 2;
1327 break;
1329 case 's': /* s_word */
1330 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1331 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1332 pos16 += 2;
1333 break;
1335 case 'l': /* long or segmented pointer */
1336 case 'T': /* segmented pointer to null-terminated string */
1337 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1338 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1339 pos16 += 4;
1340 break;
1342 case 'p': /* linear pointer */
1343 case 't': /* linear pointer to null-terminated string */
1344 /* Get the selector */
1345 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1346 /* Get the selector base */
1347 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1348 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1349 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1350 /* Add the offset */
1351 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1352 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1353 pos16 += 4;
1354 break;
1356 default:
1357 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1361 return pos16 - 6; /* Return the size of the 16-bit args */
1365 /*******************************************************************
1366 * BuildContext16
1368 * Build the context structure on the 32-bit stack.
1370 static void BuildContext16( FILE *outfile )
1372 /* Store the registers */
1374 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1375 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1376 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1377 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1378 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1379 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1380 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1381 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1382 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1383 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1385 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1386 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1387 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1388 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1389 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1390 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1391 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1392 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1393 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1394 fprintf( outfile, "\tpushfl\n" );
1395 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1396 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1397 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1398 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1399 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1400 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1401 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1402 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1403 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1404 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1405 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1406 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1407 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1408 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1409 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1410 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1411 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1412 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1413 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1414 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1415 #if 0
1416 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1417 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1418 #endif
1422 /*******************************************************************
1423 * RestoreContext16
1425 * Restore the registers from the context structure.
1426 * %edx must point to the 32-bit stack top.
1428 static void RestoreContext16( FILE *outfile )
1430 /* Get the 32-bit stack pointer */
1432 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1433 STRUCTOFFSET(STACK32FRAME,ebp) );
1435 /* Remove everything up to the return address from the 16-bit stack */
1437 fprintf( outfile, "\taddl $22,%%esp\n" );
1439 /* Restore the registers */
1441 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1442 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1443 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1444 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1445 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1446 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1447 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1448 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1449 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1450 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1451 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1452 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1453 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1454 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1455 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1456 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1457 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1458 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1459 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1460 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1461 fprintf( outfile, "\tpopfl\n" );
1462 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1463 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1464 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1465 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1466 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1467 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1471 /*******************************************************************
1472 * BuildCallFrom16Func
1474 * Build a 16-bit-to-Wine callback function. The syntax of the function
1475 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1476 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1477 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1478 * 'T'=segmented pointer to null-terminated string).
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:
1483 * ... ...
1484 * (sp+18) word first 16-bit arg
1485 * (sp+16) word cs
1486 * (sp+14) word ip
1487 * (sp+12) word bp
1488 * (sp+8) long 32-bit entry point (used to store edx)
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
1494 * Added on the stack:
1495 * (sp-4) long ebp
1496 * (sp-8) long saved previous stack
1498 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1500 int argsize = 0;
1501 int short_ret = 0;
1502 int reg_func = 0;
1503 char *args = profile + 5;
1505 /* Parse function type */
1507 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1508 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1509 else if (strncmp( "long_", profile, 5 ))
1511 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1512 return;
1515 /* Function header */
1517 fprintf( outfile, "\n\t.align 4\n" );
1518 #ifdef USE_STABS
1519 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1520 profile, profile);
1521 #endif
1522 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1523 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1525 /* Setup bp to point to its copy on the stack */
1527 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1528 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1529 fprintf( outfile, "\taddw $16,%%bp\n" );
1531 /* Save 16-bit ds and es */
1533 /* Stupid FreeBSD assembler doesn't know these either */
1534 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1535 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1536 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1537 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1539 /* Save %ebx */
1541 fprintf( outfile, "\tpushl %%ebx\n" );
1543 /* Restore 32-bit segment registers */
1545 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1546 #ifdef __svr4__
1547 fprintf( outfile, "\tdata16\n");
1548 #endif
1549 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1550 #ifdef __svr4__
1551 fprintf( outfile, "\tdata16\n");
1552 #endif
1553 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1554 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1556 /* Get the 32-bit stack pointer from the TEB */
1558 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1560 /* Save the 16-bit stack */
1562 #ifdef __svr4__
1563 fprintf( outfile,"\tdata16\n");
1564 #endif
1565 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1566 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1568 /* Transfer the arguments */
1570 if (reg_func) BuildContext16( outfile );
1571 else if (*args) argsize = TransferArgs16To32( outfile, args );
1573 /* Get the address of the API function */
1575 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1577 /* If necessary, save %edx over the API function address */
1579 if (!reg_func && short_ret)
1580 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1582 /* Restore %ebx and store the 32-bit stack pointer instead */
1584 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1585 fprintf( outfile, "\tpopl %%ebx\n" );
1586 fprintf( outfile, "\tpushl %%ebp\n" );
1588 /* Switch to the 32-bit stack */
1590 fprintf( outfile, "\tpushl %%ds\n" );
1591 fprintf( outfile, "\tpopl %%ss\n" );
1592 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1593 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1594 if (reg_func) /* Push the address of the context struct */
1595 fprintf( outfile, "\tpushl %%esp\n" );
1597 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1599 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1601 /* Print the debug information before the call */
1603 if (debugging)
1605 fprintf( outfile, "\tpushl %%eax\n" );
1606 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1607 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1608 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1609 fprintf( outfile, "\tpopl %%eax\n" );
1610 fprintf( outfile, "\tpopl %%eax\n" );
1611 fprintf( outfile, "\tpopl %%eax\n" );
1614 /* Call the entry point */
1616 fprintf( outfile, "\tcall *%%eax\n" );
1618 /* Print the debug information after the call */
1620 if (debugging)
1622 if (reg_func)
1624 /* Push again the address of the context struct in case */
1625 /* it has been removed by an stdcall function */
1626 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1627 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1628 fprintf( outfile, "\tpushl %%esp\n" );
1630 fprintf( outfile, "\tpushl %%eax\n" );
1631 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1632 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1633 fprintf( outfile, "\tpopl %%eax\n" );
1634 fprintf( outfile, "\tpopl %%eax\n" );
1637 /* Restore the 16-bit stack */
1639 #ifdef __svr4__
1640 fprintf( outfile, "\tdata16\n");
1641 #endif
1642 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1643 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1644 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1645 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1647 if (reg_func)
1649 /* Calc the arguments size */
1650 while (*args)
1652 switch(*args)
1654 case 'w':
1655 case 's':
1656 argsize += 2;
1657 break;
1658 case 'p':
1659 case 't':
1660 case 'l':
1661 case 'T':
1662 argsize += 4;
1663 break;
1664 default:
1665 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1667 args++;
1670 /* Restore registers from the context structure */
1671 RestoreContext16( outfile );
1673 else
1675 /* Restore high 16 bits of ebp */
1676 fprintf( outfile, "\tpopl %%ebp\n" );
1678 /* Restore ds and es */
1679 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1680 fprintf( outfile, "\tincl %%esp\n" );
1681 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1682 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1683 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1685 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1686 else
1688 /* Get the return value into dx:ax */
1689 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1690 fprintf( outfile, "\tshrl $16,%%edx\n" );
1691 /* Remove API entry point */
1692 fprintf( outfile, "\taddl $4,%%esp\n" );
1695 /* Restore low 16 bits of ebp */
1696 fprintf( outfile, "\tpopw %%bp\n" );
1699 /* Remove the arguments and return */
1701 if (argsize)
1703 fprintf( outfile, "\t.byte 0x66\n" );
1704 fprintf( outfile, "\tlret $%d\n", argsize );
1706 else
1708 fprintf( outfile, "\t.byte 0x66\n" );
1709 fprintf( outfile, "\tlret\n" );
1714 /*******************************************************************
1715 * BuildCallTo16Func
1717 * Build a Wine-to-16-bit callback function.
1719 * Stack frame of the callback function:
1720 * ... ...
1721 * (ebp+16) arg2
1722 * (ebp+12) arg1
1723 * (ebp+8) func to call
1724 * (ebp+4) return address
1725 * (ebp) previous ebp
1727 * Prototypes for the CallTo16 functions:
1728 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1729 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1730 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1731 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1733 static void BuildCallTo16Func( FILE *outfile, char *profile )
1735 int short_ret = 0;
1736 int reg_func = 0;
1737 char *args = profile + 5;
1739 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1740 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1741 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1742 else if (strncmp( "long_", profile, 5 ))
1744 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1745 exit(1);
1748 /* Function header */
1750 fprintf( outfile, "\n\t.align 4\n" );
1751 #ifdef USE_STABS
1752 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1753 profile, profile);
1754 #endif
1755 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1756 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1758 /* Entry code */
1760 fprintf( outfile, "\tpushl %%ebp\n" );
1761 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1763 /* Call the actual CallTo16 routine (simulate a lcall) */
1765 fprintf( outfile, "\tpushl %%cs\n" );
1766 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1768 /* Exit code */
1770 /* FIXME: this is a hack because of task.c */
1771 if (!strcmp( profile, "word_" ))
1773 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1774 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1776 fprintf( outfile, "\tpopl %%ebp\n" );
1777 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1779 /* Start of the actual CallTo16 routine */
1781 /* Save the 32-bit registers */
1783 fprintf( outfile, "do_callto16_%s:\n", profile );
1784 fprintf( outfile, "\tpushl %%ebx\n" );
1785 fprintf( outfile, "\tpushl %%ecx\n" );
1786 fprintf( outfile, "\tpushl %%edx\n" );
1787 fprintf( outfile, "\tpushl %%esi\n" );
1788 fprintf( outfile, "\tpushl %%edi\n" );
1790 /* Print debugging info */
1792 if (debugging)
1794 /* Push the address of the first argument */
1795 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1796 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1797 fprintf( outfile, "\tpushl %%eax\n" );
1798 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1799 fprintf( outfile, "\tpopl %%eax\n" );
1800 fprintf( outfile, "\tpopl %%eax\n" );
1803 /* Save the 32-bit stack and %fs */
1805 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1806 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1807 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1808 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1810 if (reg_func)
1812 /* Switch to the 16-bit stack, saving the current %%esp, */
1813 /* and adding the specified offset to the new sp */
1814 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1815 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1816 #ifdef __svr4__
1817 fprintf( outfile,"\tdata16\n");
1818 #endif
1819 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1820 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1821 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1823 /* Get the registers. ebx is handled later on. */
1825 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1826 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1827 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1828 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1829 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1830 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1831 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1832 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1833 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1835 /* Push the return address
1836 * With sreg suffix, we push 16:16 address (normal lret)
1837 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1839 if (reg_func == 1)
1840 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1841 else
1843 fprintf( outfile, "\tpushw $0\n" );
1844 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_long+2\n" );
1845 fprintf( outfile, "\tpushw $0\n" );
1846 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_long\n" );
1849 /* Push the called routine address */
1851 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1852 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1854 /* Get the 16-bit ds */
1856 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1857 /* Get ebx from the 32-bit stack */
1858 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1859 fprintf( outfile, "\tpopl %%ds\n" );
1861 else /* not a register function */
1863 int pos = 12; /* first argument position */
1865 /* Switch to the 16-bit stack */
1866 #ifdef __svr4__
1867 fprintf( outfile,"\tdata16\n");
1868 #endif
1869 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1870 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1871 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1873 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1874 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1875 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1876 STRUCTOFFSET(STACK16FRAME,bp) );
1878 /* Transfer the arguments */
1880 while (*args)
1882 switch(*args++)
1884 case 'w': /* word */
1885 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1886 break;
1887 case 'l': /* long */
1888 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1889 break;
1890 default:
1891 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1892 args[-1] );
1894 pos += 4;
1897 /* Push the return address */
1899 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1900 short_ret ? "word" : "long" );
1902 /* Push the called routine address */
1904 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
1906 /* Set %ds and %es (and %ax just in case) equal to %ss */
1908 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1909 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1910 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1913 /* Jump to the called routine */
1915 fprintf( outfile, "\t.byte 0x66\n" );
1916 fprintf( outfile, "\tlret\n" );
1920 /*******************************************************************
1921 * BuildRet16Func
1923 * Build the return code for 16-bit callbacks
1925 static void BuildRet16Func( FILE *outfile )
1927 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1928 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1930 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1931 fprintf( outfile, "\txorl %%edx,%%edx\n" );
1933 /* Put return value into %eax */
1935 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1936 fprintf( outfile, "\tshll $16,%%edx\n" );
1937 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
1938 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
1940 /* Restore 32-bit segment registers */
1942 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1943 #ifdef __svr4__
1944 fprintf( outfile, "\tdata16\n");
1945 #endif
1946 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1947 #ifdef __svr4__
1948 fprintf( outfile, "\tdata16\n");
1949 #endif
1950 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1951 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1953 /* Restore the 32-bit stack */
1955 #ifdef __svr4__
1956 fprintf( outfile, "\tdata16\n");
1957 #endif
1958 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
1959 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
1960 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1962 /* Restore the 32-bit registers */
1964 fprintf( outfile, "\tpopl %%edi\n" );
1965 fprintf( outfile, "\tpopl %%esi\n" );
1966 fprintf( outfile, "\tpopl %%edx\n" );
1967 fprintf( outfile, "\tpopl %%ecx\n" );
1968 fprintf( outfile, "\tpopl %%ebx\n" );
1970 /* Return to caller */
1972 fprintf( outfile, "\tlret\n" );
1974 /* Declare the return address variables */
1976 fprintf( outfile, "\t.data\n" );
1977 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
1978 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
1979 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Current_fs\n" );
1980 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
1981 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
1982 fprintf( outfile, PREFIX "CALLTO16_Current_fs:\t.long 0\n" );
1983 fprintf( outfile, "\t.text\n" );
1987 /*******************************************************************
1988 * BuildCallTo32LargeStack
1990 * Build the function used to switch to the original 32-bit stack
1991 * before calling a 32-bit function from 32-bit code. This is used for
1992 * functions that need a large stack, like X bitmaps functions.
1994 * The generated function has the following prototype:
1995 * int xxx( int (*func)(), void *arg );
1997 * The pointer to the function can be retrieved by calling CALL32_Init,
1998 * which also takes care of saving the current 32-bit stack pointer.
2000 * Stack layout:
2001 * ... ...
2002 * (ebp+12) arg
2003 * (ebp+8) func
2004 * (ebp+4) ret addr
2005 * (ebp) ebp
2007 static void BuildCallTo32LargeStack( FILE *outfile )
2009 /* Initialization function */
2011 fprintf( outfile, "\n\t.align 4\n" );
2012 #ifdef USE_STABS
2013 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2014 #endif
2015 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2016 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2017 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2018 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2019 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2020 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2021 fprintf( outfile, "\tret\n" );
2023 /* Function header */
2025 fprintf( outfile, "\n\t.align 4\n" );
2026 #ifdef USE_STABS
2027 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2028 #endif
2029 fprintf( outfile, "CALL32_LargeStack:\n" );
2031 /* Entry code */
2033 fprintf( outfile, "\tpushl %%ebp\n" );
2034 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2036 /* Switch to the original 32-bit stack pointer */
2038 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2040 /* Transfer the argument and call the function */
2042 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2043 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2045 /* Restore registers and return */
2047 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2048 fprintf( outfile, "\tpopl %%ebp\n" );
2049 fprintf( outfile, "\tret\n" );
2051 /* Data */
2053 fprintf( outfile, "\t.data\n" );
2054 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2055 fprintf( outfile, "\t.text\n" );
2059 /*******************************************************************
2060 * BuildCallFrom32Regs
2062 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2063 * 'args' is the number of dword arguments.
2065 * Stack layout:
2066 * ... ...
2067 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2068 * (esp+204) entry point
2069 * (esp+0) CONTEXT struct
2071 static void BuildCallFrom32Regs( FILE *outfile )
2073 /* Function header */
2075 fprintf( outfile, "\n\t.align 4\n" );
2076 #ifdef USE_STABS
2077 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2078 #endif
2079 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2080 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2082 /* Build the context structure */
2084 fprintf( outfile, "\tpushw $0\n" );
2085 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2086 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2087 fprintf( outfile, "\tpushfl\n" );
2088 fprintf( outfile, "\tpushw $0\n" );
2089 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2090 fprintf( outfile, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2091 fprintf( outfile, "\tpushl %%ebp\n" );
2093 fprintf( outfile, "\tpushl %%eax\n" );
2094 fprintf( outfile, "\tpushl %%ecx\n" );
2095 fprintf( outfile, "\tpushl %%edx\n" );
2096 fprintf( outfile, "\tpushl %%ebx\n" );
2097 fprintf( outfile, "\tpushl %%esi\n" );
2098 fprintf( outfile, "\tpushl %%edi\n" );
2100 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2101 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2102 fprintf( outfile, "\tpushl %%eax\n" );
2103 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2104 fprintf( outfile, "\tpushl %%eax\n" );
2105 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2106 fprintf( outfile, "\tpushl %%eax\n" );
2107 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2108 fprintf( outfile, "\tpushl %%eax\n" );
2110 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2111 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2112 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2114 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2116 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2117 sizeof(CONTEXT) + 4 ); /* %esp at time of call */
2118 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2120 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2122 /* Restore the context structure */
2124 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2126 /* Store %eip value onto the new stack */
2128 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2129 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2130 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2132 /* Restore all registers */
2134 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2135 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2136 fprintf( outfile, "\tpopl %%eax\n" );
2137 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2138 fprintf( outfile, "\tpopl %%eax\n" );
2139 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2140 fprintf( outfile, "\tpopl %%eax\n" );
2141 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2142 fprintf( outfile, "\tpopl %%eax\n" );
2143 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2145 fprintf( outfile, "\tpopl %%edi\n" );
2146 fprintf( outfile, "\tpopl %%esi\n" );
2147 fprintf( outfile, "\tpopl %%ebx\n" );
2148 fprintf( outfile, "\tpopl %%edx\n" );
2149 fprintf( outfile, "\tpopl %%ecx\n" );
2150 fprintf( outfile, "\tpopl %%eax\n" );
2151 fprintf( outfile, "\tpopl %%ebp\n" );
2152 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2153 fprintf( outfile, "\tpopfl\n" );
2154 fprintf( outfile, "\tpopl %%esp\n" );
2155 fprintf( outfile, "\tret\n" );
2159 /*******************************************************************
2160 * BuildSpec
2162 * Build the spec files
2164 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2166 int i;
2167 for (i = 2; i < argc; i++)
2168 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2169 return 0;
2173 /*******************************************************************
2174 * BuildCallFrom16
2176 * Build the 16-bit-to-Wine callbacks
2178 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2180 int i;
2181 char buffer[1024];
2183 /* File header */
2185 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2186 fprintf( outfile, "\t.text\n" );
2188 #ifdef USE_STABS
2189 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2190 getcwd(buffer, sizeof(buffer));
2193 * The stabs help the internal debugger as they are an indication that it
2194 * is sensible to step into a thunk/trampoline.
2196 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2197 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2198 fprintf( outfile, "\t.text\n" );
2199 fprintf( outfile, "\t.align 4\n" );
2200 fprintf( outfile, "Code_Start:\n\n" );
2201 #endif
2203 /* Build the callback functions */
2205 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2207 /* Output the argument debugging strings */
2209 if (debugging)
2211 fprintf( outfile, "/* Argument strings */\n" );
2212 for (i = 2; i < argc; i++)
2214 fprintf( outfile, "Profile_%s:\t", argv[i] );
2215 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 5 );
2219 #ifdef USE_STABS
2220 fprintf( outfile, "\t.text\n");
2221 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2222 fprintf( outfile, ".Letext:\n");
2223 #endif
2225 return 0;
2229 /*******************************************************************
2230 * BuildCallTo16
2232 * Build the Wine-to-16-bit callbacks
2234 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2236 char buffer[1024];
2237 FILE *infile;
2239 if (argc > 2)
2241 infile = fopen( argv[2], "r" );
2242 if (!infile)
2244 perror( argv[2] );
2245 exit( 1 );
2248 else infile = stdin;
2250 /* File header */
2252 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2253 fprintf( outfile, "\t.text\n" );
2255 #ifdef USE_STABS
2256 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2257 getcwd(buffer, sizeof(buffer));
2260 * The stabs help the internal debugger as they are an indication that it
2261 * is sensible to step into a thunk/trampoline.
2263 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2264 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2265 fprintf( outfile, "\t.text\n" );
2266 fprintf( outfile, "\t.align 4\n" );
2267 fprintf( outfile, "Code_Start:\n\n" );
2268 #endif
2270 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2271 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2273 /* Build the callback functions */
2275 while (fgets( buffer, sizeof(buffer), infile ))
2277 if (strstr( buffer, "### start build ###" )) break;
2279 while (fgets( buffer, sizeof(buffer), infile ))
2281 char *p = strstr( buffer, "CallTo16_" );
2282 if (p)
2284 char *profile = p + strlen( "CallTo16_" );
2285 p = profile;
2286 while ((*p == '_') || isalpha(*p)) p++;
2287 *p = '\0';
2288 BuildCallTo16Func( outfile, profile );
2290 if (strstr( buffer, "### stop build ###" )) break;
2293 /* Output the 16-bit return code */
2295 BuildRet16Func( outfile );
2297 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2298 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2300 #ifdef USE_STABS
2301 fprintf( outfile, "\t.text\n");
2302 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2303 fprintf( outfile, ".Letext:\n");
2304 #endif
2306 fclose( infile );
2307 return 0;
2311 /*******************************************************************
2312 * BuildCall32
2314 * Build the 32-bit callbacks
2316 static int BuildCall32( FILE *outfile, char * outname )
2318 char buffer[1024];
2320 /* File header */
2322 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2323 fprintf( outfile, "\t.text\n" );
2325 #ifdef __i386__
2327 #ifdef USE_STABS
2328 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2329 getcwd(buffer, sizeof(buffer));
2332 * The stabs help the internal debugger as they are an indication that it
2333 * is sensible to step into a thunk/trampoline.
2335 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2336 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2337 fprintf( outfile, "\t.text\n" );
2338 fprintf( outfile, "\t.align 4\n" );
2339 fprintf( outfile, "Code_Start:\n" );
2340 #endif
2342 /* Build the 32-bit large stack callback */
2344 BuildCallTo32LargeStack( outfile );
2346 /* Build the register callback function */
2348 BuildCallFrom32Regs( outfile );
2350 #ifdef USE_STABS
2351 fprintf( outfile, "\t.text\n");
2352 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2353 fprintf( outfile, ".Letext:\n");
2354 #endif
2356 #else /* __i386__ */
2358 /* Just to avoid an empty file */
2359 fprintf( outfile, "\t.long 0\n" );
2361 #endif /* __i386__ */
2362 return 0;
2366 /*******************************************************************
2367 * usage
2369 static void usage(void)
2371 fprintf( stderr,
2372 "usage: build [-o outfile] -spec SPECNAMES\n"
2373 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2374 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2375 " build [-o outfile] -call32\n" );
2376 exit(1);
2380 /*******************************************************************
2381 * main
2383 int main(int argc, char **argv)
2385 char *outname = NULL;
2386 FILE *outfile = stdout;
2387 int res = -1;
2389 if (argc < 2) usage();
2391 if (!strcmp( argv[1], "-o" ))
2393 outname = argv[2];
2394 argv += 2;
2395 argc -= 2;
2396 if (argc < 2) usage();
2397 if (!(outfile = fopen( outname, "w" )))
2399 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2400 exit(1);
2404 /* Retrieve the selector values; this assumes that we are building
2405 * the asm files on the platform that will also run them. Probably
2406 * a safe assumption to make.
2408 GET_CS( Code_Selector );
2409 GET_DS( Data_Selector );
2411 if (!strcmp( argv[1], "-spec" ))
2412 res = BuildSpec( outfile, argc, argv );
2413 else if (!strcmp( argv[1], "-callfrom16" ))
2414 res = BuildCallFrom16( outfile, outname, argc, argv );
2415 else if (!strcmp( argv[1], "-callto16" ))
2416 res = BuildCallTo16( outfile, outname, argc, argv );
2417 else if (!strcmp( argv[1], "-call32" ))
2418 res = BuildCall32( outfile, outname );
2419 else
2421 fclose( outfile );
2422 unlink( outname );
2423 usage();
2426 fclose( outfile );
2427 if (res < 0)
2429 unlink( outname );
2430 return 1;
2432 return 0;