Release 980215
[wine/multimedia.git] / tools / build.c
blob34995d24f4df0314103403f41d6046253c273cee
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 break;
366 case SPEC_WIN32:
367 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
369 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
370 SpecName, Line );
371 return -1;
373 break;
374 default:
375 break;
378 token = GetToken();
379 if (*token != '(')
381 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
382 SpecName, Line, token);
383 return -1;
386 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
388 token = GetToken();
389 if (*token == ')')
390 break;
392 if (!strcmp(token, "word"))
393 odp->u.func.arg_types[i] = 'w';
394 else if (!strcmp(token, "s_word"))
395 odp->u.func.arg_types[i] = 's';
396 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
397 odp->u.func.arg_types[i] = 'l';
398 else if (!strcmp(token, "ptr"))
399 odp->u.func.arg_types[i] = 'p';
400 else if (!strcmp(token, "str"))
401 odp->u.func.arg_types[i] = 't';
402 else if (!strcmp(token, "wstr"))
403 odp->u.func.arg_types[i] = 'W';
404 else if (!strcmp(token, "segstr"))
405 odp->u.func.arg_types[i] = 'T';
406 else if (!strcmp(token, "double"))
408 odp->u.func.arg_types[i++] = 'l';
409 odp->u.func.arg_types[i] = 'l';
411 else
413 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
414 SpecName, Line, token);
415 return -1;
417 if (SpecType == SPEC_WIN32)
419 if (strcmp(token, "long") &&
420 strcmp(token, "ptr") &&
421 strcmp(token, "str") &&
422 strcmp(token, "wstr") &&
423 strcmp(token, "double"))
425 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
426 SpecName, Line, token );
427 return -1;
431 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
433 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
434 return -1;
436 odp->u.func.arg_types[i] = '\0';
437 if ((odp->type == TYPE_STDCALL) && !i)
438 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
439 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
441 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
442 SpecName, Line );
443 return -1;
445 strcpy(odp->u.func.link_name, GetToken());
446 return 0;
450 /*******************************************************************
451 * ParseEquate
453 * Parse an 'equate' definition.
455 static int ParseEquate( ORDDEF *odp )
457 char *endptr;
459 char *token = GetToken();
460 int value = strtol(token, &endptr, 0);
461 if (endptr == NULL || *endptr != '\0')
463 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
464 SpecName, Line, token);
465 return -1;
468 if (SpecType == SPEC_WIN32)
470 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
471 SpecName, Line );
472 return -1;
475 odp->u.abs.value = value;
476 return 0;
480 /*******************************************************************
481 * ParseReturn
483 * Parse a 'return' definition.
485 static int ParseReturn( ORDDEF *odp )
487 char *token;
488 char *endptr;
490 token = GetToken();
491 odp->u.ret.arg_size = strtol(token, &endptr, 0);
492 if (endptr == NULL || *endptr != '\0')
494 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
495 SpecName, Line, token);
496 return -1;
499 token = GetToken();
500 odp->u.ret.ret_value = strtol(token, &endptr, 0);
501 if (endptr == NULL || *endptr != '\0')
503 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
504 SpecName, Line, token);
505 return -1;
508 if (SpecType == SPEC_WIN32)
510 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
511 SpecName, Line );
512 return -1;
515 return 0;
519 /*******************************************************************
520 * ParseStub
522 * Parse a 'stub' definition.
524 static int ParseStub( ORDDEF *odp )
526 odp->u.func.arg_types[0] = '\0';
527 strcpy( odp->u.func.link_name, STUB_CALLBACK );
528 return 0;
532 /*******************************************************************
533 * ParseVarargs
535 * Parse an 'varargs' definition.
537 static int ParseVarargs( ORDDEF *odp )
539 char *token;
541 if (SpecType == SPEC_WIN16)
543 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
544 SpecName, Line );
545 return -1;
548 token = GetToken();
549 if (*token != '(')
551 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
552 SpecName, Line, token);
553 return -1;
555 token = GetToken();
556 if (*token != ')')
558 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
559 SpecName, Line, token);
560 return -1;
563 strcpy( odp->u.vargs.link_name, GetToken() );
564 return 0;
568 /*******************************************************************
569 * ParseExtern
571 * Parse an 'extern' definition.
573 static int ParseExtern( ORDDEF *odp )
575 if (SpecType == SPEC_WIN16)
577 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
578 SpecName, Line );
579 return -1;
581 strcpy( odp->u.ext.link_name, GetToken() );
582 return 0;
586 /*******************************************************************
587 * ParseOrdinal
589 * Parse an ordinal definition.
591 static int ParseOrdinal(int ordinal)
593 ORDDEF *odp;
594 char *token;
596 if (ordinal >= MAX_ORDINALS)
598 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
599 return -1;
601 if (ordinal > Limit) Limit = ordinal;
602 if (ordinal < Base) Base = ordinal;
604 odp = &OrdinalDefinitions[ordinal];
605 if (!(token = GetToken()))
607 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
608 return -1;
611 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
612 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
613 break;
615 if (odp->type >= TYPE_NBTYPES)
617 fprintf( stderr,
618 "%s:%d: Expected type after ordinal, found '%s' instead\n",
619 SpecName, Line, token );
620 return -1;
623 if (!(token = GetToken()))
625 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
626 return -1;
628 strcpy( odp->name, token );
629 odp->lineno = Line;
631 switch(odp->type)
633 case TYPE_BYTE:
634 case TYPE_WORD:
635 case TYPE_LONG:
636 return ParseVariable( odp );
637 case TYPE_PASCAL_16:
638 case TYPE_PASCAL:
639 case TYPE_REGISTER:
640 case TYPE_STDCALL:
641 case TYPE_CDECL:
642 return ParseExportFunction( odp );
643 case TYPE_ABS:
644 return ParseEquate( odp );
645 case TYPE_RETURN:
646 return ParseReturn( odp );
647 case TYPE_STUB:
648 return ParseStub( odp );
649 case TYPE_VARARGS:
650 return ParseVarargs( odp );
651 case TYPE_EXTERN:
652 return ParseExtern( odp );
653 default:
654 fprintf( stderr, "Should not happen\n" );
655 return -1;
660 /*******************************************************************
661 * ParseTopLevel
663 * Parse a spec file.
665 static int ParseTopLevel(void)
667 char *token;
669 while ((token = GetToken()) != NULL)
671 if (strcmp(token, "name") == 0)
673 strcpy(DLLName, GetToken());
674 strupper(DLLName);
675 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
677 else if (strcmp(token, "file") == 0)
679 strcpy(DLLFileName, GetToken());
680 strupper(DLLFileName);
682 else if (strcmp(token, "type") == 0)
684 token = GetToken();
685 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
686 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
687 else
689 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
690 SpecName, Line);
691 return -1;
694 else if (strcmp(token, "heap") == 0)
696 token = GetToken();
697 if (!IsNumberString(token))
699 fprintf(stderr, "%s:%d: Expected number after heap\n",
700 SpecName, Line);
701 return -1;
703 DLLHeapSize = atoi(token);
705 else if (IsNumberString(token))
707 int ordinal;
708 int rv;
710 ordinal = atoi(token);
711 if ((rv = ParseOrdinal(ordinal)) < 0)
712 return rv;
714 else
716 fprintf(stderr,
717 "%s:%d: Expected name, id, length or ordinal\n",
718 SpecName, Line);
719 return -1;
723 return 0;
727 /*******************************************************************
728 * StoreVariableCode
730 * Store a list of ints into a byte array.
732 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
734 int i;
736 switch(size)
738 case 1:
739 for (i = 0; i < odp->u.var.n_values; i++)
740 buffer[i] = odp->u.var.values[i];
741 break;
742 case 2:
743 for (i = 0; i < odp->u.var.n_values; i++)
744 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
745 break;
746 case 4:
747 for (i = 0; i < odp->u.var.n_values; i++)
748 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
749 break;
751 return odp->u.var.n_values * size;
755 /*******************************************************************
756 * DumpBytes
758 * Dump a byte stream into the assembly code.
760 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
761 const char *section, const char *label_start )
763 int i;
764 if (section) fprintf( outfile, "\t%s\n", section );
765 if (label_start) fprintf( outfile, "%s:\n", label_start );
766 for (i = 0; i < len; i++)
768 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
769 fprintf( outfile, "%d", *data++ );
770 if (i < len - 1)
771 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
773 fprintf( outfile, "\n" );
777 /*******************************************************************
778 * BuildModule16
780 * Build the in-memory representation of a 16-bit NE module, and dump it
781 * as a byte stream into the assembly code.
783 static int BuildModule16( FILE *outfile, int max_code_offset,
784 int max_data_offset )
786 ORDDEF *odp;
787 int i;
788 char *buffer;
789 NE_MODULE *pModule;
790 SEGTABLEENTRY *pSegment;
791 OFSTRUCT *pFileInfo;
792 BYTE *pstr, *bundle;
793 WORD *pword;
795 /* Module layout:
796 * NE_MODULE Module
797 * OFSTRUCT File information
798 * SEGTABLEENTRY Segment 1 (code)
799 * SEGTABLEENTRY Segment 2 (data)
800 * WORD[2] Resource table (empty)
801 * BYTE[2] Imported names (empty)
802 * BYTE[n] Resident names table
803 * BYTE[n] Entry table
806 buffer = xmalloc( 0x10000 );
808 pModule = (NE_MODULE *)buffer;
809 pModule->magic = IMAGE_OS2_SIGNATURE;
810 pModule->count = 1;
811 pModule->next = 0;
812 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
813 pModule->dgroup = 2;
814 pModule->heap_size = DLLHeapSize;
815 pModule->stack_size = 0;
816 pModule->ip = 0;
817 pModule->cs = 0;
818 pModule->sp = 0;
819 pModule->ss = 0;
820 pModule->seg_count = 2;
821 pModule->modref_count = 0;
822 pModule->nrname_size = 0;
823 pModule->modref_table = 0;
824 pModule->nrname_fpos = 0;
825 pModule->moveable_entries = 0;
826 pModule->alignment = 0;
827 pModule->truetype = 0;
828 pModule->os_flags = NE_OSFLAGS_WINDOWS;
829 pModule->misc_flags = 0;
830 pModule->dlls_to_init = 0;
831 pModule->nrname_handle = 0;
832 pModule->min_swap_area = 0;
833 pModule->expected_version = 0x030a;
834 pModule->module32 = 0;
835 pModule->self = 0;
836 pModule->self_loading_sel = 0;
838 /* File information */
840 pFileInfo = (OFSTRUCT *)(pModule + 1);
841 pModule->fileinfo = (int)pFileInfo - (int)pModule;
842 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
843 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
844 + strlen(DLLFileName);
845 strcpy( pFileInfo->szPathName, DLLFileName );
846 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
848 /* Segment table */
850 pSegment = (SEGTABLEENTRY *)pstr;
851 pModule->seg_table = (int)pSegment - (int)pModule;
852 pSegment->filepos = 0;
853 pSegment->size = max_code_offset;
854 pSegment->flags = 0;
855 pSegment->minsize = max_code_offset;
856 pSegment->selector = 0;
857 pSegment++;
859 pModule->dgroup_entry = (int)pSegment - (int)pModule;
860 pSegment->filepos = 0;
861 pSegment->size = max_data_offset;
862 pSegment->flags = NE_SEGFLAGS_DATA;
863 pSegment->minsize = max_data_offset;
864 pSegment->selector = 0;
865 pSegment++;
867 /* Resource table */
869 pword = (WORD *)pSegment;
870 pModule->res_table = (int)pword - (int)pModule;
871 *pword++ = 0;
872 *pword++ = 0;
874 /* Imported names table */
876 pstr = (char *)pword;
877 pModule->import_table = (int)pstr - (int)pModule;
878 *pstr++ = 0;
879 *pstr++ = 0;
881 /* Resident names table */
883 pModule->name_table = (int)pstr - (int)pModule;
884 /* First entry is module name */
885 *pstr = strlen(DLLName );
886 strcpy( pstr + 1, DLLName );
887 pstr += *pstr + 1;
888 *(WORD *)pstr = 0;
889 pstr += sizeof(WORD);
890 /* Store all ordinals */
891 odp = OrdinalDefinitions + 1;
892 for (i = 1; i <= Limit; i++, odp++)
894 if (!odp->name[0]) continue;
895 *pstr = strlen( odp->name );
896 strcpy( pstr + 1, odp->name );
897 strupper( pstr + 1 );
898 pstr += *pstr + 1;
899 *(WORD *)pstr = i;
900 pstr += sizeof(WORD);
902 *pstr++ = 0;
904 /* Entry table */
906 pModule->entry_table = (int)pstr - (int)pModule;
907 bundle = NULL;
908 odp = OrdinalDefinitions + 1;
909 for (i = 1; i <= Limit; i++, odp++)
911 int selector = 0;
913 switch (odp->type)
915 case TYPE_CDECL:
916 case TYPE_PASCAL:
917 case TYPE_PASCAL_16:
918 case TYPE_REGISTER:
919 case TYPE_RETURN:
920 case TYPE_STUB:
921 selector = 1; /* Code selector */
922 break;
924 case TYPE_BYTE:
925 case TYPE_WORD:
926 case TYPE_LONG:
927 selector = 2; /* Data selector */
928 break;
930 case TYPE_ABS:
931 selector = 0xfe; /* Constant selector */
932 break;
934 default:
935 selector = 0; /* Invalid selector */
936 break;
939 /* create a new bundle if necessary */
940 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
942 bundle = pstr;
943 bundle[0] = 0;
944 bundle[1] = selector;
945 pstr += 2;
948 (*bundle)++;
949 if (selector != 0)
951 *pstr++ = 1;
952 *(WORD *)pstr = odp->offset;
953 pstr += sizeof(WORD);
956 *pstr++ = 0;
958 /* Dump the module content */
960 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
961 ".data", "Module_Start" );
962 return (int)pstr - (int)pModule;
966 /*******************************************************************
967 * BuildSpec32File
969 * Build a Win32 C file from a spec file.
971 static int BuildSpec32File( char * specfile, FILE *outfile )
973 ORDDEF *odp;
974 int i, nb_names, nb_reg_funcs = 0;
976 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
977 specfile );
978 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
980 /* Output code for all stubs functions */
982 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
983 DLLName );
984 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
986 if (odp->type != TYPE_STUB) continue;
987 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
988 i, DLLName, i );
991 /* Output the DLL functions prototypes */
993 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
995 switch(odp->type)
997 case TYPE_VARARGS:
998 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
999 break;
1000 case TYPE_EXTERN:
1001 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1002 break;
1003 case TYPE_REGISTER:
1004 case TYPE_STDCALL:
1005 case TYPE_CDECL:
1006 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1007 break;
1008 case TYPE_INVALID:
1009 case TYPE_STUB:
1010 break;
1011 default:
1012 fprintf(stderr,"build: function type %d not available for Win32\n",
1013 odp->type);
1014 return -1;
1018 /* Output the DLL functions table */
1020 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1021 Limit - Base + 1 );
1022 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1024 switch(odp->type)
1026 case TYPE_INVALID:
1027 fprintf( outfile, " 0" );
1028 break;
1029 case TYPE_VARARGS:
1030 fprintf( outfile, " %s", odp->u.vargs.link_name );
1031 break;
1032 case TYPE_EXTERN:
1033 fprintf( outfile, " %s", odp->u.ext.link_name );
1034 break;
1035 case TYPE_REGISTER:
1036 case TYPE_STDCALL:
1037 case TYPE_CDECL:
1038 fprintf( outfile, " %s", odp->u.func.link_name);
1039 break;
1040 case TYPE_STUB:
1041 fprintf( outfile, " __stub_%d", i );
1042 break;
1043 default:
1044 return -1;
1046 if (i < Limit) fprintf( outfile, ",\n" );
1048 fprintf( outfile, "\n};\n\n" );
1050 /* Output the DLL names table */
1052 nb_names = 0;
1053 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1054 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1056 if (odp->type == TYPE_INVALID) continue;
1057 if (nb_names++) fprintf( outfile, ",\n" );
1058 fprintf( outfile, " \"%s\"", odp->name );
1060 fprintf( outfile, "\n};\n\n" );
1062 /* Output the DLL argument types */
1064 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1065 Limit - Base + 1 );
1066 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1068 unsigned int j, mask = 0;
1069 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1070 for (j = 0; odp->u.func.arg_types[j]; j++)
1072 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1073 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1075 fprintf( outfile, " %d", mask );
1076 if (i < Limit) fprintf( outfile, ",\n" );
1078 fprintf( outfile, "\n};\n\n" );
1080 /* Output the DLL ordinals table */
1082 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1083 nb_names = 0;
1084 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1086 if (odp->type == TYPE_INVALID) continue;
1087 if (nb_names++) fprintf( outfile, ",\n" );
1088 fprintf( outfile, " %d", i - Base );
1090 fprintf( outfile, "\n};\n\n" );
1092 /* Output the DLL functions arguments */
1094 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1095 Limit - Base + 1 );
1096 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1098 unsigned char args;
1099 switch(odp->type)
1101 case TYPE_STDCALL:
1102 args = (unsigned char)strlen(odp->u.func.arg_types);
1103 break;
1104 case TYPE_CDECL:
1105 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1106 break;
1107 case TYPE_REGISTER:
1108 args = 0xfe;
1109 nb_reg_funcs++;
1110 break;
1111 default:
1112 args = 0xff;
1113 break;
1115 fprintf( outfile, " 0x%02x", args );
1116 if (i < Limit) fprintf( outfile, ",\n" );
1118 fprintf( outfile, "\n};\n\n" );
1120 /* Output the DLL descriptor */
1122 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1123 DLLName );
1124 fprintf( outfile, " \"%s\",\n", DLLName );
1125 fprintf( outfile, " %d,\n", Base );
1126 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1127 fprintf( outfile, " %d,\n", nb_names );
1128 fprintf( outfile, " %d,\n", nb_reg_funcs );
1129 fprintf( outfile,
1130 " Functions,\n"
1131 " FuncNames,\n"
1132 " FuncOrdinals,\n"
1133 " FuncArgs,\n"
1134 " ArgTypes\n"
1135 "};\n" );
1136 return 0;
1140 /*******************************************************************
1141 * BuildSpec16File
1143 * Build a Win16 assembly file from a spec file.
1145 static int BuildSpec16File( char * specfile, FILE *outfile )
1147 ORDDEF *odp;
1148 int i;
1149 int code_offset, data_offset, module_size;
1150 unsigned char *data;
1152 data = (unsigned char *)xmalloc( 0x10000 );
1153 memset( data, 0, 16 );
1154 data_offset = 16;
1156 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1157 fprintf( outfile, "\t.text\n" );
1158 fprintf( outfile, "Code_Start:\n" );
1159 code_offset = 0;
1161 odp = OrdinalDefinitions;
1162 for (i = 0; i <= Limit; i++, odp++)
1164 switch (odp->type)
1166 case TYPE_INVALID:
1167 odp->offset = 0xffff;
1168 break;
1170 case TYPE_ABS:
1171 odp->offset = LOWORD(odp->u.abs.value);
1172 break;
1174 case TYPE_BYTE:
1175 odp->offset = data_offset;
1176 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1177 break;
1179 case TYPE_WORD:
1180 odp->offset = data_offset;
1181 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1182 break;
1184 case TYPE_LONG:
1185 odp->offset = data_offset;
1186 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1187 break;
1189 case TYPE_RETURN:
1190 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1191 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1192 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1193 fprintf( outfile,"\t.byte 0x66\n");
1194 if (odp->u.ret.arg_size != 0)
1195 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1196 else
1198 fprintf( outfile, "\tlret\n");
1199 fprintf( outfile, "\tnop\n");
1200 fprintf( outfile, "\tnop\n\n");
1202 odp->offset = code_offset;
1203 code_offset += 12; /* Assembly code is 12 bytes long */
1204 break;
1206 case TYPE_REGISTER:
1207 case TYPE_CDECL:
1208 case TYPE_PASCAL:
1209 case TYPE_PASCAL_16:
1210 case TYPE_STUB:
1211 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1212 fprintf( outfile, "\tpushw %%bp\n" );
1213 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1214 /* FreeBSD does not understand lcall, so do it the hard way */
1215 fprintf( outfile, "\t.byte 0x9a\n" );
1216 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1217 (odp->type == TYPE_CDECL) ? "c" : "p",
1218 (odp->type == TYPE_REGISTER) ? "regs" :
1219 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1220 odp->u.func.arg_types );
1221 fprintf( outfile, "\t.long 0x%08lx\n",
1222 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1223 odp->offset = code_offset;
1224 code_offset += 16; /* Assembly code is 16 bytes long */
1225 break;
1227 default:
1228 fprintf(stderr,"build: function type %d not available for Win16\n",
1229 odp->type);
1230 return -1;
1234 if (!code_offset) /* Make sure the code segment is not empty */
1236 fprintf( outfile, "\t.byte 0\n" );
1237 code_offset++;
1240 /* Output data segment */
1242 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1244 /* Build the module */
1246 module_size = BuildModule16( outfile, code_offset, data_offset );
1248 /* Output the DLL descriptor */
1250 fprintf( outfile, "\t.text\n" );
1251 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1252 fprintf( outfile, "\t.align 4\n" );
1253 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1254 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1255 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1256 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1257 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1258 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1259 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1260 return 0;
1264 /*******************************************************************
1265 * BuildSpecFile
1267 * Build an assembly file from a spec file.
1269 static int BuildSpecFile( FILE *outfile, char *specname )
1271 SpecName = specname;
1272 SpecFp = fopen( specname, "r");
1273 if (SpecFp == NULL)
1275 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1276 return -1;
1279 if (ParseTopLevel() < 0) return -1;
1281 switch(SpecType)
1283 case SPEC_WIN16:
1284 return BuildSpec16File( specname, outfile );
1285 case SPEC_WIN32:
1286 return BuildSpec32File( specname, outfile );
1287 default:
1288 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1289 return -1;
1294 /*******************************************************************
1295 * TransferArgs16To32
1297 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1298 * The 16-bit stack layout is:
1299 * ... ...
1300 * (bp+8) arg2
1301 * (bp+6) arg1
1302 * (bp+4) cs
1303 * (bp+2) ip
1304 * (bp) bp
1306 * For 'cdecl' argn up to arg1 are reversed.
1308 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1310 int i, pos16, pos32;
1312 /* Copy the arguments */
1314 pos16 = 6; /* skip bp and return address */
1315 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1317 for (i = strlen(args); i > 0; i--)
1319 if (!usecdecl) pos32 -= 4;
1320 switch(args[i-1])
1322 case 'w': /* word */
1323 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1324 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1325 pos16 += 2;
1326 break;
1328 case 's': /* s_word */
1329 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1330 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1331 pos16 += 2;
1332 break;
1334 case 'l': /* long or segmented pointer */
1335 case 'T': /* segmented pointer to null-terminated string */
1336 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1337 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1338 pos16 += 4;
1339 break;
1341 case 'p': /* linear pointer */
1342 case 't': /* linear pointer to null-terminated string */
1343 /* Get the selector */
1344 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1345 /* Get the selector base */
1346 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1347 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1348 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1349 /* Add the offset */
1350 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1351 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1352 pos16 += 4;
1353 break;
1355 default:
1356 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1358 if (usecdecl) pos32 += 4;
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, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1404 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1405 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1406 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1407 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1408 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1409 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1410 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1411 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1412 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1413 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1414 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1415 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1416 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1417 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1418 #if 0
1419 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1420 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1421 #endif
1425 /*******************************************************************
1426 * RestoreContext16
1428 * Restore the registers from the context structure.
1430 static void RestoreContext16( FILE *outfile )
1432 /* Get the 32-bit stack pointer */
1434 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1435 STRUCTOFFSET(STACK32FRAME,ebp) );
1437 /* Remove everything up to (including) the return address
1438 * from the 16-bit stack */
1440 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1441 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1442 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1443 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1444 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1445 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1447 /* Restore the registers */
1449 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1450 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1451 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1452 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1453 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1454 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1455 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1456 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1457 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1458 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1459 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1460 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1461 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1462 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1463 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1464 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1465 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1466 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1467 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1468 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1469 fprintf( outfile, "\tpopfl\n" );
1470 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1471 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1472 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1473 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1474 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1475 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1479 /*******************************************************************
1480 * BuildCallFrom16Func
1482 * Build a 16-bit-to-Wine callback function. The syntax of the function
1483 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1484 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1485 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1486 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1487 * 'T'=segmented pointer to null-terminated string).
1488 * For register functions, the arguments are ignored, but they are still
1489 * removed from the stack upon return.
1491 * Stack layout upon entry to the callback function:
1492 * ... ...
1493 * (sp+18) word first 16-bit arg
1494 * (sp+16) word cs
1495 * (sp+14) word ip
1496 * (sp+12) word bp
1497 * (sp+8) long 32-bit entry point (used to store edx)
1498 * (sp+6) word high word of cs (always 0, used to store es)
1499 * (sp+4) word low word of cs of 16-bit entry point
1500 * (sp+2) word high word of ip (always 0, used to store ds)
1501 * (sp) word low word of ip of 16-bit entry point
1503 * Added on the stack:
1504 * (sp-4) long ebp
1505 * (sp-8) long saved previous stack
1507 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1509 int argsize = 0;
1510 int short_ret = 0;
1511 int reg_func = 0;
1512 int cdecl = 0;
1513 char *args = profile + 7;
1515 /* Parse function type */
1517 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1518 else if (strncmp( "p_", profile, 2 ))
1520 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1521 return;
1524 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1525 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1526 else if (strncmp( "long_", profile + 2, 5 ))
1528 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1529 return;
1532 /* Function header */
1534 fprintf( outfile, "\n\t.align 4\n" );
1535 #ifdef USE_STABS
1536 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1537 profile, profile);
1538 #endif
1539 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1540 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1542 /* Setup bp to point to its copy on the stack */
1544 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1545 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1546 fprintf( outfile, "\taddw $16,%%bp\n" );
1548 /* Save 16-bit ds and es */
1550 /* Stupid FreeBSD assembler doesn't know these either */
1551 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1552 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1553 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1554 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1556 /* Save %ebx */
1558 fprintf( outfile, "\tpushl %%ebx\n" );
1560 /* Restore 32-bit segment registers */
1562 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1563 #ifdef __svr4__
1564 fprintf( outfile, "\tdata16\n");
1565 #endif
1566 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1567 #ifdef __svr4__
1568 fprintf( outfile, "\tdata16\n");
1569 #endif
1570 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1571 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1573 /* Get the 32-bit stack pointer from the TEB */
1575 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1577 /* Save the 16-bit stack */
1579 #ifdef __svr4__
1580 fprintf( outfile,"\tdata16\n");
1581 #endif
1582 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1583 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1585 /* Transfer the arguments */
1587 if (reg_func) BuildContext16( outfile );
1588 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1590 /* Get the address of the API function */
1592 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1594 /* If necessary, save %edx over the API function address */
1596 if (!reg_func && short_ret)
1597 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1599 /* Restore %ebx and store the 32-bit stack pointer instead */
1601 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1602 fprintf( outfile, "\tpopl %%ebx\n" );
1603 fprintf( outfile, "\tpushl %%ebp\n" );
1605 /* Switch to the 32-bit stack */
1607 fprintf( outfile, "\tpushl %%ds\n" );
1608 fprintf( outfile, "\tpopl %%ss\n" );
1609 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1610 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1611 if (reg_func) /* Push the address of the context struct */
1612 fprintf( outfile, "\tpushl %%esp\n" );
1614 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1616 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1618 /* Print the debug information before the call */
1620 if (debugging)
1622 int ftype = 0;
1624 if (cdecl) ftype |= 4;
1625 if (reg_func) ftype |= 2;
1626 if (short_ret) ftype |= 1;
1628 fprintf( outfile, "\tpushl %%eax\n" );
1629 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1630 fprintf( outfile, "\tpushl $%d\n", ftype );
1631 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1632 fprintf( outfile, "\tpopl %%eax\n" );
1633 fprintf( outfile, "\tpopl %%eax\n" );
1634 fprintf( outfile, "\tpopl %%eax\n" );
1637 /* Call the entry point */
1639 fprintf( outfile, "\tcall *%%eax\n" );
1641 /* Print the debug information after the call */
1643 if (debugging)
1645 if (reg_func)
1647 /* Push again the address of the context struct in case */
1648 /* it has been removed by an stdcall function */
1649 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1650 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1651 fprintf( outfile, "\tpushl %%esp\n" );
1653 fprintf( outfile, "\tpushl %%eax\n" );
1654 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1655 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1656 fprintf( outfile, "\tpopl %%eax\n" );
1657 fprintf( outfile, "\tpopl %%eax\n" );
1660 /* Restore the 16-bit stack */
1662 #ifdef __svr4__
1663 fprintf( outfile, "\tdata16\n");
1664 #endif
1665 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1666 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1667 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1668 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1670 if (reg_func)
1672 /* Calc the arguments size */
1673 while (*args)
1675 switch(*args)
1677 case 'w':
1678 case 's':
1679 argsize += 2;
1680 break;
1681 case 'p':
1682 case 't':
1683 case 'l':
1684 case 'T':
1685 argsize += 4;
1686 break;
1687 default:
1688 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1690 args++;
1693 /* Restore registers from the context structure */
1694 RestoreContext16( outfile );
1696 else
1698 /* Restore high 16 bits of ebp */
1699 fprintf( outfile, "\tpopl %%ebp\n" );
1701 /* Restore ds and es */
1702 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1703 fprintf( outfile, "\tincl %%esp\n" );
1704 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1705 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1706 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1708 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1709 else
1711 /* Get the return value into dx:ax */
1712 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1713 fprintf( outfile, "\tshrl $16,%%edx\n" );
1714 /* Remove API entry point */
1715 fprintf( outfile, "\taddl $4,%%esp\n" );
1718 /* Restore low 16 bits of ebp */
1719 fprintf( outfile, "\tpopw %%bp\n" );
1722 /* Remove the arguments and return */
1724 if (argsize && !cdecl)
1726 fprintf( outfile, "\t.byte 0x66\n" );
1727 fprintf( outfile, "\tlret $%d\n", argsize );
1729 else
1731 fprintf( outfile, "\t.byte 0x66\n" );
1732 fprintf( outfile, "\tlret\n" );
1737 /*******************************************************************
1738 * BuildCallTo16Func
1740 * Build a Wine-to-16-bit callback function.
1742 * Stack frame of the callback function:
1743 * ... ...
1744 * (ebp+16) arg2
1745 * (ebp+12) arg1
1746 * (ebp+8) func to call
1747 * (ebp+4) return address
1748 * (ebp) previous ebp
1750 * Prototypes for the CallTo16 functions:
1751 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1752 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1753 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1754 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1756 static void BuildCallTo16Func( FILE *outfile, char *profile )
1758 int short_ret = 0;
1759 int reg_func = 0;
1760 char *args = profile + 5;
1762 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1763 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1764 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1765 else if (strncmp( "long_", profile, 5 ))
1767 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1768 exit(1);
1771 /* Function header */
1773 fprintf( outfile, "\n\t.align 4\n" );
1774 #ifdef USE_STABS
1775 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1776 profile, profile);
1777 #endif
1778 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1779 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1781 /* Entry code */
1783 fprintf( outfile, "\tpushl %%ebp\n" );
1784 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1786 /* Call the actual CallTo16 routine (simulate a lcall) */
1788 fprintf( outfile, "\tpushl %%cs\n" );
1789 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1791 /* Exit code */
1793 /* FIXME: this is a hack because of task.c */
1794 if (!strcmp( profile, "word_" ))
1796 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1797 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1799 fprintf( outfile, "\tpopl %%ebp\n" );
1800 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1802 /* Start of the actual CallTo16 routine */
1804 /* Save the 32-bit registers */
1806 fprintf( outfile, "do_callto16_%s:\n", profile );
1807 fprintf( outfile, "\tpushl %%ebx\n" );
1808 fprintf( outfile, "\tpushl %%ecx\n" );
1809 fprintf( outfile, "\tpushl %%edx\n" );
1810 fprintf( outfile, "\tpushl %%esi\n" );
1811 fprintf( outfile, "\tpushl %%edi\n" );
1813 /* Print debugging info */
1815 if (debugging)
1817 /* Push the address of the first argument */
1818 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1819 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1820 fprintf( outfile, "\tpushl %%eax\n" );
1821 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1822 fprintf( outfile, "\tpopl %%eax\n" );
1823 fprintf( outfile, "\tpopl %%eax\n" );
1826 /* Save the 32-bit stack and %fs */
1828 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1829 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1830 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1831 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1833 if (reg_func)
1835 /* Switch to the 16-bit stack, saving the current %%esp, */
1836 /* and adding the specified offset to the new sp */
1837 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1838 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1839 #ifdef __svr4__
1840 fprintf( outfile,"\tdata16\n");
1841 #endif
1842 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1843 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1844 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1846 /* Get the registers. ebx is handled later on. */
1848 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1849 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1850 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1851 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1852 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1853 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1854 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1855 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1856 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1858 /* Push the return address
1859 * With sreg suffix, we push 16:16 address (normal lret)
1860 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1862 if (reg_func == 1)
1863 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1864 else
1866 fprintf( outfile, "\tpushw $0\n" );
1867 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_long+2\n" );
1868 fprintf( outfile, "\tpushw $0\n" );
1869 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_long\n" );
1872 /* Push the called routine address */
1874 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1875 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1877 /* Get the 16-bit ds */
1879 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1880 /* Get ebx from the 32-bit stack */
1881 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1882 fprintf( outfile, "\tpopl %%ds\n" );
1884 else /* not a register function */
1886 int pos = 12; /* first argument position */
1888 /* Switch to the 16-bit stack */
1889 #ifdef __svr4__
1890 fprintf( outfile,"\tdata16\n");
1891 #endif
1892 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1893 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1894 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1896 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1897 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1898 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1899 STRUCTOFFSET(STACK16FRAME,bp) );
1901 /* Transfer the arguments */
1903 while (*args)
1905 switch(*args++)
1907 case 'w': /* word */
1908 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1909 break;
1910 case 'l': /* long */
1911 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1912 break;
1913 default:
1914 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1915 args[-1] );
1917 pos += 4;
1920 /* Push the return address */
1922 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1923 short_ret ? "word" : "long" );
1925 /* Push the called routine address */
1927 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
1929 /* Set %ds and %es (and %ax just in case) equal to %ss */
1931 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1932 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1933 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1936 /* Jump to the called routine */
1938 fprintf( outfile, "\t.byte 0x66\n" );
1939 fprintf( outfile, "\tlret\n" );
1943 /*******************************************************************
1944 * BuildRet16Func
1946 * Build the return code for 16-bit callbacks
1948 static void BuildRet16Func( FILE *outfile )
1950 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1951 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1953 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1954 fprintf( outfile, "\txorl %%edx,%%edx\n" );
1956 /* Put return value into %eax */
1958 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1959 fprintf( outfile, "\tshll $16,%%edx\n" );
1960 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
1961 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
1963 /* Restore 32-bit segment registers */
1965 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1966 #ifdef __svr4__
1967 fprintf( outfile, "\tdata16\n");
1968 #endif
1969 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1970 #ifdef __svr4__
1971 fprintf( outfile, "\tdata16\n");
1972 #endif
1973 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1974 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1976 /* Restore the 32-bit stack */
1978 #ifdef __svr4__
1979 fprintf( outfile, "\tdata16\n");
1980 #endif
1981 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
1982 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
1983 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1985 /* Restore the 32-bit registers */
1987 fprintf( outfile, "\tpopl %%edi\n" );
1988 fprintf( outfile, "\tpopl %%esi\n" );
1989 fprintf( outfile, "\tpopl %%edx\n" );
1990 fprintf( outfile, "\tpopl %%ecx\n" );
1991 fprintf( outfile, "\tpopl %%ebx\n" );
1993 /* Return to caller */
1995 fprintf( outfile, "\tlret\n" );
1997 /* Declare the return address variables */
1999 fprintf( outfile, "\t.data\n" );
2000 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2001 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2002 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Current_fs\n" );
2003 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2004 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2005 fprintf( outfile, PREFIX "CALLTO16_Current_fs:\t.long 0\n" );
2006 fprintf( outfile, "\t.text\n" );
2010 /*******************************************************************
2011 * BuildCallTo32LargeStack
2013 * Build the function used to switch to the original 32-bit stack
2014 * before calling a 32-bit function from 32-bit code. This is used for
2015 * functions that need a large stack, like X bitmaps functions.
2017 * The generated function has the following prototype:
2018 * int xxx( int (*func)(), void *arg );
2020 * The pointer to the function can be retrieved by calling CALL32_Init,
2021 * which also takes care of saving the current 32-bit stack pointer.
2023 * Stack layout:
2024 * ... ...
2025 * (ebp+12) arg
2026 * (ebp+8) func
2027 * (ebp+4) ret addr
2028 * (ebp) ebp
2030 static void BuildCallTo32LargeStack( FILE *outfile )
2032 /* Initialization function */
2034 fprintf( outfile, "\n\t.align 4\n" );
2035 #ifdef USE_STABS
2036 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2037 #endif
2038 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2039 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2040 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2041 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2042 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2043 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2044 fprintf( outfile, "\tret\n" );
2046 /* Function header */
2048 fprintf( outfile, "\n\t.align 4\n" );
2049 #ifdef USE_STABS
2050 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2051 #endif
2052 fprintf( outfile, "CALL32_LargeStack:\n" );
2054 /* Entry code */
2056 fprintf( outfile, "\tpushl %%ebp\n" );
2057 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2059 /* Switch to the original 32-bit stack pointer */
2061 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2063 /* Transfer the argument and call the function */
2065 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2066 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2068 /* Restore registers and return */
2070 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2071 fprintf( outfile, "\tpopl %%ebp\n" );
2072 fprintf( outfile, "\tret\n" );
2074 /* Data */
2076 fprintf( outfile, "\t.data\n" );
2077 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2078 fprintf( outfile, "\t.text\n" );
2082 /*******************************************************************
2083 * BuildCallFrom32Regs
2085 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2086 * 'args' is the number of dword arguments.
2088 * Stack layout:
2089 * ... ...
2090 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2091 * (esp+204) entry point
2092 * (esp+0) CONTEXT struct
2094 static void BuildCallFrom32Regs( FILE *outfile )
2096 /* Function header */
2098 fprintf( outfile, "\n\t.align 4\n" );
2099 #ifdef USE_STABS
2100 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2101 #endif
2102 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2103 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2105 /* Build the context structure */
2107 fprintf( outfile, "\tpushw $0\n" );
2108 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2109 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2110 fprintf( outfile, "\tpushfl\n" );
2111 fprintf( outfile, "\tpushw $0\n" );
2112 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2113 fprintf( outfile, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2114 fprintf( outfile, "\tpushl %%ebp\n" );
2116 fprintf( outfile, "\tpushl %%eax\n" );
2117 fprintf( outfile, "\tpushl %%ecx\n" );
2118 fprintf( outfile, "\tpushl %%edx\n" );
2119 fprintf( outfile, "\tpushl %%ebx\n" );
2120 fprintf( outfile, "\tpushl %%esi\n" );
2121 fprintf( outfile, "\tpushl %%edi\n" );
2123 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2124 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2125 fprintf( outfile, "\tpushl %%eax\n" );
2126 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2127 fprintf( outfile, "\tpushl %%eax\n" );
2128 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2129 fprintf( outfile, "\tpushl %%eax\n" );
2130 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2131 fprintf( outfile, "\tpushl %%eax\n" );
2133 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2134 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2135 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2137 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2139 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2140 sizeof(CONTEXT) + 4 ); /* %esp at time of call */
2141 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2143 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2145 /* Restore the context structure */
2147 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2149 /* Store %eip value onto the new stack */
2151 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2152 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2153 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2155 /* Restore all registers */
2157 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2158 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2159 fprintf( outfile, "\tpopl %%eax\n" );
2160 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2161 fprintf( outfile, "\tpopl %%eax\n" );
2162 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2163 fprintf( outfile, "\tpopl %%eax\n" );
2164 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2165 fprintf( outfile, "\tpopl %%eax\n" );
2166 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2168 fprintf( outfile, "\tpopl %%edi\n" );
2169 fprintf( outfile, "\tpopl %%esi\n" );
2170 fprintf( outfile, "\tpopl %%ebx\n" );
2171 fprintf( outfile, "\tpopl %%edx\n" );
2172 fprintf( outfile, "\tpopl %%ecx\n" );
2173 fprintf( outfile, "\tpopl %%eax\n" );
2174 fprintf( outfile, "\tpopl %%ebp\n" );
2175 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2176 fprintf( outfile, "\tpopfl\n" );
2177 fprintf( outfile, "\tpopl %%esp\n" );
2178 fprintf( outfile, "\tret\n" );
2182 /*******************************************************************
2183 * BuildSpec
2185 * Build the spec files
2187 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2189 int i;
2190 for (i = 2; i < argc; i++)
2191 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2192 return 0;
2196 /*******************************************************************
2197 * BuildCallFrom16
2199 * Build the 16-bit-to-Wine callbacks
2201 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2203 int i;
2204 char buffer[1024];
2206 /* File header */
2208 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2209 fprintf( outfile, "\t.text\n" );
2211 #ifdef USE_STABS
2212 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2213 getcwd(buffer, sizeof(buffer));
2216 * The stabs help the internal debugger as they are an indication that it
2217 * is sensible to step into a thunk/trampoline.
2219 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2220 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2221 fprintf( outfile, "\t.text\n" );
2222 fprintf( outfile, "\t.align 4\n" );
2223 fprintf( outfile, "Code_Start:\n\n" );
2224 #endif
2226 /* Build the callback functions */
2228 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2230 /* Output the argument debugging strings */
2232 if (debugging)
2234 fprintf( outfile, "/* Argument strings */\n" );
2235 for (i = 2; i < argc; i++)
2237 fprintf( outfile, "Profile_%s:\t", argv[i] );
2238 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2242 #ifdef USE_STABS
2243 fprintf( outfile, "\t.text\n");
2244 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2245 fprintf( outfile, ".Letext:\n");
2246 #endif
2248 return 0;
2252 /*******************************************************************
2253 * BuildCallTo16
2255 * Build the Wine-to-16-bit callbacks
2257 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2259 char buffer[1024];
2260 FILE *infile;
2262 if (argc > 2)
2264 infile = fopen( argv[2], "r" );
2265 if (!infile)
2267 perror( argv[2] );
2268 exit( 1 );
2271 else infile = stdin;
2273 /* File header */
2275 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2276 fprintf( outfile, "\t.text\n" );
2278 #ifdef USE_STABS
2279 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2280 getcwd(buffer, sizeof(buffer));
2283 * The stabs help the internal debugger as they are an indication that it
2284 * is sensible to step into a thunk/trampoline.
2286 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2287 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2288 fprintf( outfile, "\t.text\n" );
2289 fprintf( outfile, "\t.align 4\n" );
2290 fprintf( outfile, "Code_Start:\n\n" );
2291 #endif
2293 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2294 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2296 /* Build the callback functions */
2298 while (fgets( buffer, sizeof(buffer), infile ))
2300 if (strstr( buffer, "### start build ###" )) break;
2302 while (fgets( buffer, sizeof(buffer), infile ))
2304 char *p = strstr( buffer, "CallTo16_" );
2305 if (p)
2307 char *profile = p + strlen( "CallTo16_" );
2308 p = profile;
2309 while ((*p == '_') || isalpha(*p)) p++;
2310 *p = '\0';
2311 BuildCallTo16Func( outfile, profile );
2313 if (strstr( buffer, "### stop build ###" )) break;
2316 /* Output the 16-bit return code */
2318 BuildRet16Func( outfile );
2320 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2321 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2323 #ifdef USE_STABS
2324 fprintf( outfile, "\t.text\n");
2325 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2326 fprintf( outfile, ".Letext:\n");
2327 #endif
2329 fclose( infile );
2330 return 0;
2334 /*******************************************************************
2335 * BuildCall32
2337 * Build the 32-bit callbacks
2339 static int BuildCall32( FILE *outfile, char * outname )
2341 char buffer[1024];
2343 /* File header */
2345 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2346 fprintf( outfile, "\t.text\n" );
2348 #ifdef __i386__
2350 #ifdef USE_STABS
2351 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2352 getcwd(buffer, sizeof(buffer));
2355 * The stabs help the internal debugger as they are an indication that it
2356 * is sensible to step into a thunk/trampoline.
2358 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2359 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2360 fprintf( outfile, "\t.text\n" );
2361 fprintf( outfile, "\t.align 4\n" );
2362 fprintf( outfile, "Code_Start:\n" );
2363 #endif
2365 /* Build the 32-bit large stack callback */
2367 BuildCallTo32LargeStack( outfile );
2369 /* Build the register callback function */
2371 BuildCallFrom32Regs( outfile );
2373 #ifdef USE_STABS
2374 fprintf( outfile, "\t.text\n");
2375 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2376 fprintf( outfile, ".Letext:\n");
2377 #endif
2379 #else /* __i386__ */
2381 /* Just to avoid an empty file */
2382 fprintf( outfile, "\t.long 0\n" );
2384 #endif /* __i386__ */
2385 return 0;
2389 /*******************************************************************
2390 * usage
2392 static void usage(void)
2394 fprintf( stderr,
2395 "usage: build [-o outfile] -spec SPECNAMES\n"
2396 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2397 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2398 " build [-o outfile] -call32\n" );
2399 exit(1);
2403 /*******************************************************************
2404 * main
2406 int main(int argc, char **argv)
2408 char *outname = NULL;
2409 FILE *outfile = stdout;
2410 int res = -1;
2412 if (argc < 2) usage();
2414 if (!strcmp( argv[1], "-o" ))
2416 outname = argv[2];
2417 argv += 2;
2418 argc -= 2;
2419 if (argc < 2) usage();
2420 if (!(outfile = fopen( outname, "w" )))
2422 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2423 exit(1);
2427 /* Retrieve the selector values; this assumes that we are building
2428 * the asm files on the platform that will also run them. Probably
2429 * a safe assumption to make.
2431 GET_CS( Code_Selector );
2432 GET_DS( Data_Selector );
2434 if (!strcmp( argv[1], "-spec" ))
2435 res = BuildSpec( outfile, argc, argv );
2436 else if (!strcmp( argv[1], "-callfrom16" ))
2437 res = BuildCallFrom16( outfile, outname, argc, argv );
2438 else if (!strcmp( argv[1], "-callto16" ))
2439 res = BuildCallTo16( outfile, outname, argc, argv );
2440 else if (!strcmp( argv[1], "-call32" ))
2441 res = BuildCall32( outfile, outname );
2442 else
2444 fclose( outfile );
2445 unlink( outname );
2446 usage();
2449 fclose( outfile );
2450 if (res < 0)
2452 unlink( outname );
2453 return 1;
2455 return 0;