Authors: Chris Morgan <cmorgan@wpi.edu>, James Abbatiello <abbejy@wpi.edu>
[wine/multimedia.git] / tools / build.c
blobfcd4a20da4cfc3003f54c47777cf9da5f321a7fb
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 "winbase.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;
153 static char DLLInitFunc[80];
155 char *ParseBuffer = NULL;
156 char *ParseNext;
157 char ParseSaveChar;
158 int Line;
160 static int debugging = 1;
162 /* Offset of a structure field relative to the start of the struct */
163 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
165 /* Offset of register relative to the start of the CONTEXT struct */
166 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
168 /* Offset of the stack pointer relative to %fs:(0) */
169 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
172 static void *xmalloc (size_t size)
174 void *res;
176 res = malloc (size ? size : 1);
177 if (res == NULL)
179 fprintf (stderr, "Virtual memory exhausted.\n");
180 exit (1);
182 return res;
186 static void *xrealloc (void *ptr, size_t size)
188 void *res = realloc (ptr, size);
189 if (res == NULL)
191 fprintf (stderr, "Virtual memory exhausted.\n");
192 exit (1);
194 return res;
198 static int IsNumberString(char *s)
200 while (*s != '\0')
201 if (!isdigit(*s++))
202 return 0;
204 return 1;
207 static char *strupper(char *s)
209 char *p;
211 for(p = s; *p != '\0'; p++)
212 *p = toupper(*p);
214 return s;
217 static char * GetTokenInLine(void)
219 char *p;
220 char *token;
222 if (ParseNext != ParseBuffer)
224 if (ParseSaveChar == '\0')
225 return NULL;
226 *ParseNext = ParseSaveChar;
230 * Remove initial white space.
232 for (p = ParseNext; isspace(*p); p++)
235 if ((*p == '\0') || (*p == '#'))
236 return NULL;
239 * Find end of token.
241 token = p++;
242 if (*token != '(' && *token != ')')
243 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
244 p++;
246 ParseSaveChar = *p;
247 ParseNext = p;
248 *p = '\0';
250 return token;
253 static char * GetToken(void)
255 char *token;
257 if (ParseBuffer == NULL)
259 ParseBuffer = xmalloc(512);
260 ParseNext = ParseBuffer;
261 while (1)
263 Line++;
264 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
265 return NULL;
266 if (ParseBuffer[0] != '#')
267 break;
271 while ((token = GetTokenInLine()) == NULL)
273 ParseNext = ParseBuffer;
274 while (1)
276 Line++;
277 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
278 return NULL;
279 if (ParseBuffer[0] != '#')
280 break;
284 return token;
288 /*******************************************************************
289 * ParseVariable
291 * Parse a variable definition.
293 static int ParseVariable( ORDDEF *odp )
295 char *endptr;
296 int *value_array;
297 int n_values;
298 int value_array_size;
300 char *token = GetToken();
301 if (*token != '(')
303 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
304 SpecName, Line, token);
305 return -1;
308 n_values = 0;
309 value_array_size = 25;
310 value_array = xmalloc(sizeof(*value_array) * value_array_size);
312 while ((token = GetToken()) != NULL)
314 if (*token == ')')
315 break;
317 value_array[n_values++] = strtol(token, &endptr, 0);
318 if (n_values == value_array_size)
320 value_array_size += 25;
321 value_array = xrealloc(value_array,
322 sizeof(*value_array) * value_array_size);
325 if (endptr == NULL || *endptr != '\0')
327 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
328 SpecName, Line, token);
329 return -1;
333 if (token == NULL)
335 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
336 SpecName, Line);
337 return -1;
340 odp->u.var.n_values = n_values;
341 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
343 return 0;
347 /*******************************************************************
348 * ParseExportFunction
350 * Parse a function definition.
352 static int ParseExportFunction( ORDDEF *odp )
354 char *token;
355 int i;
357 switch(SpecType)
359 case SPEC_WIN16:
360 if (odp->type == TYPE_STDCALL)
362 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
363 SpecName, Line );
364 return -1;
366 break;
367 case SPEC_WIN32:
368 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
370 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
371 SpecName, Line );
372 return -1;
374 break;
375 default:
376 break;
379 token = GetToken();
380 if (*token != '(')
382 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
383 SpecName, Line, token);
384 return -1;
387 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
389 token = GetToken();
390 if (*token == ')')
391 break;
393 if (!strcmp(token, "word"))
394 odp->u.func.arg_types[i] = 'w';
395 else if (!strcmp(token, "s_word"))
396 odp->u.func.arg_types[i] = 's';
397 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
398 odp->u.func.arg_types[i] = 'l';
399 else if (!strcmp(token, "ptr"))
400 odp->u.func.arg_types[i] = 'p';
401 else if (!strcmp(token, "str"))
402 odp->u.func.arg_types[i] = 't';
403 else if (!strcmp(token, "wstr"))
404 odp->u.func.arg_types[i] = 'W';
405 else if (!strcmp(token, "segstr"))
406 odp->u.func.arg_types[i] = 'T';
407 else if (!strcmp(token, "double"))
409 odp->u.func.arg_types[i++] = 'l';
410 odp->u.func.arg_types[i] = 'l';
412 else
414 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
415 SpecName, Line, token);
416 return -1;
418 if (SpecType == SPEC_WIN32)
420 if (strcmp(token, "long") &&
421 strcmp(token, "ptr") &&
422 strcmp(token, "str") &&
423 strcmp(token, "wstr") &&
424 strcmp(token, "double"))
426 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
427 SpecName, Line, token );
428 return -1;
432 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
434 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
435 return -1;
437 odp->u.func.arg_types[i] = '\0';
438 if ((odp->type == TYPE_STDCALL) && !i)
439 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
440 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
442 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
443 SpecName, Line );
444 return -1;
446 strcpy(odp->u.func.link_name, GetToken());
447 return 0;
451 /*******************************************************************
452 * ParseEquate
454 * Parse an 'equate' definition.
456 static int ParseEquate( ORDDEF *odp )
458 char *endptr;
460 char *token = GetToken();
461 int value = strtol(token, &endptr, 0);
462 if (endptr == NULL || *endptr != '\0')
464 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
465 SpecName, Line, token);
466 return -1;
469 if (SpecType == SPEC_WIN32)
471 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
472 SpecName, Line );
473 return -1;
476 odp->u.abs.value = value;
477 return 0;
481 /*******************************************************************
482 * ParseReturn
484 * Parse a 'return' definition.
486 static int ParseReturn( ORDDEF *odp )
488 char *token;
489 char *endptr;
491 token = GetToken();
492 odp->u.ret.arg_size = strtol(token, &endptr, 0);
493 if (endptr == NULL || *endptr != '\0')
495 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
496 SpecName, Line, token);
497 return -1;
500 token = GetToken();
501 odp->u.ret.ret_value = strtol(token, &endptr, 0);
502 if (endptr == NULL || *endptr != '\0')
504 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
505 SpecName, Line, token);
506 return -1;
509 if (SpecType == SPEC_WIN32)
511 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
512 SpecName, Line );
513 return -1;
516 return 0;
520 /*******************************************************************
521 * ParseStub
523 * Parse a 'stub' definition.
525 static int ParseStub( ORDDEF *odp )
527 odp->u.func.arg_types[0] = '\0';
528 strcpy( odp->u.func.link_name, STUB_CALLBACK );
529 return 0;
533 /*******************************************************************
534 * ParseVarargs
536 * Parse an 'varargs' definition.
538 static int ParseVarargs( ORDDEF *odp )
540 char *token;
542 if (SpecType == SPEC_WIN16)
544 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
545 SpecName, Line );
546 return -1;
549 token = GetToken();
550 if (*token != '(')
552 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
553 SpecName, Line, token);
554 return -1;
556 token = GetToken();
557 if (*token != ')')
559 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
560 SpecName, Line, token);
561 return -1;
564 strcpy( odp->u.vargs.link_name, GetToken() );
565 return 0;
569 /*******************************************************************
570 * ParseExtern
572 * Parse an 'extern' definition.
574 static int ParseExtern( ORDDEF *odp )
576 if (SpecType == SPEC_WIN16)
578 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
579 SpecName, Line );
580 return -1;
582 strcpy( odp->u.ext.link_name, GetToken() );
583 return 0;
587 /*******************************************************************
588 * ParseOrdinal
590 * Parse an ordinal definition.
592 static int ParseOrdinal(int ordinal)
594 ORDDEF *odp;
595 char *token;
597 if (ordinal >= MAX_ORDINALS)
599 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
600 return -1;
602 if (ordinal > Limit) Limit = ordinal;
603 if (ordinal < Base) Base = ordinal;
605 odp = &OrdinalDefinitions[ordinal];
606 if (!(token = GetToken()))
608 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
609 return -1;
612 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
613 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
614 break;
616 if (odp->type >= TYPE_NBTYPES)
618 fprintf( stderr,
619 "%s:%d: Expected type after ordinal, found '%s' instead\n",
620 SpecName, Line, token );
621 return -1;
624 if (!(token = GetToken()))
626 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
627 return -1;
629 strcpy( odp->name, token );
630 odp->lineno = Line;
632 switch(odp->type)
634 case TYPE_BYTE:
635 case TYPE_WORD:
636 case TYPE_LONG:
637 return ParseVariable( odp );
638 case TYPE_PASCAL_16:
639 case TYPE_PASCAL:
640 case TYPE_REGISTER:
641 case TYPE_STDCALL:
642 case TYPE_CDECL:
643 return ParseExportFunction( odp );
644 case TYPE_ABS:
645 return ParseEquate( odp );
646 case TYPE_RETURN:
647 return ParseReturn( odp );
648 case TYPE_STUB:
649 return ParseStub( odp );
650 case TYPE_VARARGS:
651 return ParseVarargs( odp );
652 case TYPE_EXTERN:
653 return ParseExtern( odp );
654 default:
655 fprintf( stderr, "Should not happen\n" );
656 return -1;
661 /*******************************************************************
662 * ParseTopLevel
664 * Parse a spec file.
666 static int ParseTopLevel(void)
668 char *token;
670 while ((token = GetToken()) != NULL)
672 if (strcmp(token, "name") == 0)
674 strcpy(DLLName, GetToken());
675 strupper(DLLName);
676 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
678 else if (strcmp(token, "file") == 0)
680 strcpy(DLLFileName, GetToken());
681 strupper(DLLFileName);
683 else if (strcmp(token, "type") == 0)
685 token = GetToken();
686 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
687 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
688 else
690 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
691 SpecName, Line);
692 return -1;
695 else if (strcmp(token, "heap") == 0)
697 token = GetToken();
698 if (!IsNumberString(token))
700 fprintf(stderr, "%s:%d: Expected number after heap\n",
701 SpecName, Line);
702 return -1;
704 DLLHeapSize = atoi(token);
706 else if (strcmp(token, "init") == 0)
708 strcpy(DLLInitFunc, GetToken());
709 if (!DLLInitFunc[0])
710 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
712 else if (IsNumberString(token))
714 int ordinal;
715 int rv;
717 ordinal = atoi(token);
718 if ((rv = ParseOrdinal(ordinal)) < 0)
719 return rv;
721 else
723 fprintf(stderr,
724 "%s:%d: Expected name, id, length or ordinal\n",
725 SpecName, Line);
726 return -1;
730 return 0;
734 /*******************************************************************
735 * StoreVariableCode
737 * Store a list of ints into a byte array.
739 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
741 int i;
743 switch(size)
745 case 1:
746 for (i = 0; i < odp->u.var.n_values; i++)
747 buffer[i] = odp->u.var.values[i];
748 break;
749 case 2:
750 for (i = 0; i < odp->u.var.n_values; i++)
751 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
752 break;
753 case 4:
754 for (i = 0; i < odp->u.var.n_values; i++)
755 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
756 break;
758 return odp->u.var.n_values * size;
762 /*******************************************************************
763 * DumpBytes
765 * Dump a byte stream into the assembly code.
767 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
768 const char *section, const char *label_start )
770 int i;
771 if (section) fprintf( outfile, "\t%s\n", section );
772 if (label_start) fprintf( outfile, "%s:\n", label_start );
773 for (i = 0; i < len; i++)
775 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
776 fprintf( outfile, "%d", *data++ );
777 if (i < len - 1)
778 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
780 fprintf( outfile, "\n" );
784 /*******************************************************************
785 * BuildModule16
787 * Build the in-memory representation of a 16-bit NE module, and dump it
788 * as a byte stream into the assembly code.
790 static int BuildModule16( FILE *outfile, int max_code_offset,
791 int max_data_offset )
793 ORDDEF *odp;
794 int i;
795 char *buffer;
796 NE_MODULE *pModule;
797 SEGTABLEENTRY *pSegment;
798 OFSTRUCT *pFileInfo;
799 BYTE *pstr;
800 WORD *pword;
801 ET_BUNDLE *bundle = 0;
802 ET_ENTRY *entry = 0;
804 /* Module layout:
805 * NE_MODULE Module
806 * OFSTRUCT File information
807 * SEGTABLEENTRY Segment 1 (code)
808 * SEGTABLEENTRY Segment 2 (data)
809 * WORD[2] Resource table (empty)
810 * BYTE[2] Imported names (empty)
811 * BYTE[n] Resident names table
812 * BYTE[n] Entry table
815 buffer = xmalloc( 0x10000 );
817 pModule = (NE_MODULE *)buffer;
818 pModule->magic = IMAGE_OS2_SIGNATURE;
819 pModule->count = 1;
820 pModule->next = 0;
821 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
822 pModule->dgroup = 2;
823 pModule->heap_size = DLLHeapSize;
824 pModule->stack_size = 0;
825 pModule->ip = 0;
826 pModule->cs = 0;
827 pModule->sp = 0;
828 pModule->ss = 0;
829 pModule->seg_count = 2;
830 pModule->modref_count = 0;
831 pModule->nrname_size = 0;
832 pModule->modref_table = 0;
833 pModule->nrname_fpos = 0;
834 pModule->moveable_entries = 0;
835 pModule->alignment = 0;
836 pModule->truetype = 0;
837 pModule->os_flags = NE_OSFLAGS_WINDOWS;
838 pModule->misc_flags = 0;
839 pModule->dlls_to_init = 0;
840 pModule->nrname_handle = 0;
841 pModule->min_swap_area = 0;
842 pModule->expected_version = 0x030a;
843 pModule->module32 = 0;
844 pModule->self = 0;
845 pModule->self_loading_sel = 0;
847 /* File information */
849 pFileInfo = (OFSTRUCT *)(pModule + 1);
850 pModule->fileinfo = (int)pFileInfo - (int)pModule;
851 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
852 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
853 + strlen(DLLFileName);
854 strcpy( pFileInfo->szPathName, DLLFileName );
855 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
857 /* Segment table */
859 pSegment = (SEGTABLEENTRY *)pstr;
860 pModule->seg_table = (int)pSegment - (int)pModule;
861 pSegment->filepos = 0;
862 pSegment->size = max_code_offset;
863 pSegment->flags = 0;
864 pSegment->minsize = max_code_offset;
865 pSegment->hSeg = 0;
866 pSegment++;
868 pModule->dgroup_entry = (int)pSegment - (int)pModule;
869 pSegment->filepos = 0;
870 pSegment->size = max_data_offset;
871 pSegment->flags = NE_SEGFLAGS_DATA;
872 pSegment->minsize = max_data_offset;
873 pSegment->hSeg = 0;
874 pSegment++;
876 /* Resource table */
878 pword = (WORD *)pSegment;
879 pModule->res_table = (int)pword - (int)pModule;
880 *pword++ = 0;
881 *pword++ = 0;
883 /* Imported names table */
885 pstr = (char *)pword;
886 pModule->import_table = (int)pstr - (int)pModule;
887 *pstr++ = 0;
888 *pstr++ = 0;
890 /* Resident names table */
892 pModule->name_table = (int)pstr - (int)pModule;
893 /* First entry is module name */
894 *pstr = strlen(DLLName );
895 strcpy( pstr + 1, DLLName );
896 pstr += *pstr + 1;
897 *(WORD *)pstr = 0;
898 pstr += sizeof(WORD);
899 /* Store all ordinals */
900 odp = OrdinalDefinitions + 1;
901 for (i = 1; i <= Limit; i++, odp++)
903 if (!odp->name[0]) continue;
904 *pstr = strlen( odp->name );
905 strcpy( pstr + 1, odp->name );
906 strupper( pstr + 1 );
907 pstr += *pstr + 1;
908 *(WORD *)pstr = i;
909 pstr += sizeof(WORD);
911 *pstr++ = 0;
913 /* Entry table */
915 pModule->entry_table = (int)pstr - (int)pModule;
916 odp = OrdinalDefinitions + 1;
917 for (i = 1; i <= Limit; i++, odp++)
919 int selector = 0;
921 switch (odp->type)
923 case TYPE_CDECL:
924 case TYPE_PASCAL:
925 case TYPE_PASCAL_16:
926 case TYPE_REGISTER:
927 case TYPE_RETURN:
928 case TYPE_STUB:
929 selector = 1; /* Code selector */
930 break;
932 case TYPE_BYTE:
933 case TYPE_WORD:
934 case TYPE_LONG:
935 selector = 2; /* Data selector */
936 break;
938 case TYPE_ABS:
939 selector = 0xfe; /* Constant selector */
940 break;
942 default:
943 selector = 0; /* Invalid selector */
944 break;
947 if ( !selector )
948 continue;
950 if ( bundle && bundle->last+1 == i )
951 bundle->last++;
952 else
954 if ( bundle )
955 bundle->next = (char *)pstr - (char *)pModule;
957 bundle = (ET_BUNDLE *)pstr;
958 bundle->first = i-1;
959 bundle->last = i;
960 bundle->next = 0;
961 pstr += sizeof(ET_BUNDLE);
964 /* FIXME: is this really correct ?? */
965 entry = (ET_ENTRY *)pstr;
966 entry->type = 0xff; /* movable */
967 entry->flags = 3; /* exported & public data */
968 entry->segnum = selector;
969 entry->offs = odp->offset;
970 pstr += sizeof(ET_ENTRY);
972 *pstr++ = 0;
974 /* Dump the module content */
976 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
977 ".data", "Module_Start" );
978 return (int)pstr - (int)pModule;
982 /*******************************************************************
983 * BuildSpec32File
985 * Build a Win32 C file from a spec file.
987 static int BuildSpec32File( char * specfile, FILE *outfile )
989 ORDDEF *odp;
990 int i, nb_names;
992 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
993 specfile );
994 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
996 /* Output code for all stubs functions */
998 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
999 DLLName );
1000 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1002 if (odp->type != TYPE_STUB) continue;
1003 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1004 i, DLLName, i );
1007 /* Output code for all register functions */
1009 fprintf( outfile, "#ifdef __i386__\n" );
1010 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1012 if (odp->type != TYPE_REGISTER) continue;
1013 fprintf( outfile,
1014 "__asm__(\".align 4\\n\\t\"\n"
1015 " \".globl " PREFIX "%s\\n\\t\"\n"
1016 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1017 " \"" PREFIX "%s:\\n\\t\"\n"
1018 " \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1019 " \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1020 " \"ret\");\n",
1021 odp->u.func.link_name, odp->u.func.link_name,
1022 odp->u.func.link_name, odp->u.func.link_name );
1024 fprintf( outfile, "#endif\n" );
1026 /* Output the DLL functions prototypes */
1028 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1030 switch(odp->type)
1032 case TYPE_VARARGS:
1033 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1034 break;
1035 case TYPE_EXTERN:
1036 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1037 break;
1038 case TYPE_REGISTER:
1039 case TYPE_STDCALL:
1040 case TYPE_CDECL:
1041 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1042 break;
1043 case TYPE_INVALID:
1044 case TYPE_STUB:
1045 break;
1046 default:
1047 fprintf(stderr,"build: function type %d not available for Win32\n",
1048 odp->type);
1049 return -1;
1053 /* Output LibMain function */
1054 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1057 /* Output the DLL functions table */
1059 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1060 Limit - Base + 1 );
1061 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1063 switch(odp->type)
1065 case TYPE_INVALID:
1066 fprintf( outfile, " 0" );
1067 break;
1068 case TYPE_VARARGS:
1069 fprintf( outfile, " %s", odp->u.vargs.link_name );
1070 break;
1071 case TYPE_EXTERN:
1072 fprintf( outfile, " %s", odp->u.ext.link_name );
1073 break;
1074 case TYPE_REGISTER:
1075 case TYPE_STDCALL:
1076 case TYPE_CDECL:
1077 fprintf( outfile, " %s", odp->u.func.link_name);
1078 break;
1079 case TYPE_STUB:
1080 fprintf( outfile, " __stub_%d", i );
1081 break;
1082 default:
1083 return -1;
1085 if (i < Limit) fprintf( outfile, ",\n" );
1087 fprintf( outfile, "\n};\n\n" );
1089 /* Output the DLL names table */
1091 nb_names = 0;
1092 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1093 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1095 if (odp->type == TYPE_INVALID) continue;
1096 if (nb_names++) fprintf( outfile, ",\n" );
1097 fprintf( outfile, " \"%s\"", odp->name );
1099 fprintf( outfile, "\n};\n\n" );
1101 /* Output the DLL argument types */
1103 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1104 Limit - Base + 1 );
1105 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1107 unsigned int j, mask = 0;
1108 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1109 for (j = 0; odp->u.func.arg_types[j]; j++)
1111 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1112 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1114 fprintf( outfile, " %d", mask );
1115 if (i < Limit) fprintf( outfile, ",\n" );
1117 fprintf( outfile, "\n};\n\n" );
1119 /* Output the DLL ordinals table */
1121 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1122 nb_names = 0;
1123 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1125 if (odp->type == TYPE_INVALID) continue;
1126 if (nb_names++) fprintf( outfile, ",\n" );
1127 fprintf( outfile, " %d", i - Base );
1129 fprintf( outfile, "\n};\n\n" );
1131 /* Output the DLL functions arguments */
1133 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1134 Limit - Base + 1 );
1135 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1137 unsigned char args;
1138 switch(odp->type)
1140 case TYPE_STDCALL:
1141 args = (unsigned char)strlen(odp->u.func.arg_types);
1142 break;
1143 case TYPE_CDECL:
1144 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1145 break;
1146 case TYPE_REGISTER:
1147 args = 0xfe;
1148 break;
1149 default:
1150 args = 0xff;
1151 break;
1153 fprintf( outfile, " 0x%02x", args );
1154 if (i < Limit) fprintf( outfile, ",\n" );
1156 fprintf( outfile, "\n};\n\n" );
1158 /* Output the DLL descriptor */
1160 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1161 DLLName );
1162 fprintf( outfile, " \"%s\",\n", DLLName );
1163 fprintf( outfile, " %d,\n", Base );
1164 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1165 fprintf( outfile, " %d,\n", nb_names );
1166 fprintf( outfile,
1167 " Functions,\n"
1168 " FuncNames,\n"
1169 " FuncOrdinals,\n"
1170 " FuncArgs,\n"
1171 " ArgTypes,\n");
1172 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1173 fprintf( outfile, "};\n" );
1174 return 0;
1178 /*******************************************************************
1179 * BuildSpec16File
1181 * Build a Win16 assembly file from a spec file.
1183 static int BuildSpec16File( char * specfile, FILE *outfile )
1185 ORDDEF *odp;
1186 int i;
1187 int code_offset, data_offset, module_size;
1188 unsigned char *data;
1190 data = (unsigned char *)xmalloc( 0x10000 );
1191 memset( data, 0, 16 );
1192 data_offset = 16;
1194 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1195 fprintf( outfile, "\t.text\n" );
1196 fprintf( outfile, "Code_Start:\n" );
1197 code_offset = 0;
1199 odp = OrdinalDefinitions;
1200 for (i = 0; i <= Limit; i++, odp++)
1202 switch (odp->type)
1204 case TYPE_INVALID:
1205 odp->offset = 0xffff;
1206 break;
1208 case TYPE_ABS:
1209 odp->offset = LOWORD(odp->u.abs.value);
1210 break;
1212 case TYPE_BYTE:
1213 odp->offset = data_offset;
1214 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1215 break;
1217 case TYPE_WORD:
1218 odp->offset = data_offset;
1219 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1220 break;
1222 case TYPE_LONG:
1223 odp->offset = data_offset;
1224 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1225 break;
1227 case TYPE_RETURN:
1228 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1229 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1230 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1231 fprintf( outfile,"\t.byte 0x66\n");
1232 if (odp->u.ret.arg_size != 0)
1233 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1234 else
1236 fprintf( outfile, "\tlret\n");
1237 fprintf( outfile, "\tnop\n");
1238 fprintf( outfile, "\tnop\n\n");
1240 odp->offset = code_offset;
1241 code_offset += 12; /* Assembly code is 12 bytes long */
1242 break;
1244 case TYPE_REGISTER:
1245 case TYPE_CDECL:
1246 case TYPE_PASCAL:
1247 case TYPE_PASCAL_16:
1248 case TYPE_STUB:
1249 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1250 fprintf( outfile, "\tpushw %%bp\n" );
1251 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1252 /* FreeBSD does not understand lcall, so do it the hard way */
1253 fprintf( outfile, "\t.byte 0x9a\n" );
1254 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1255 (odp->type == TYPE_CDECL) ? "c" : "p",
1256 (odp->type == TYPE_REGISTER) ? "regs" :
1257 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1258 odp->u.func.arg_types );
1259 fprintf( outfile, "\t.long 0x%08lx\n",
1260 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1261 odp->offset = code_offset;
1262 code_offset += 16; /* Assembly code is 16 bytes long */
1263 break;
1265 default:
1266 fprintf(stderr,"build: function type %d not available for Win16\n",
1267 odp->type);
1268 return -1;
1272 if (!code_offset) /* Make sure the code segment is not empty */
1274 fprintf( outfile, "\t.byte 0\n" );
1275 code_offset++;
1278 /* Output data segment */
1280 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1282 /* Build the module */
1284 module_size = BuildModule16( outfile, code_offset, data_offset );
1286 /* Output the DLL descriptor */
1288 fprintf( outfile, "\t.text\n" );
1289 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1290 fprintf( outfile, "\t.align 4\n" );
1291 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1292 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1293 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1294 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1295 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1296 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1297 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1298 return 0;
1302 /*******************************************************************
1303 * BuildSpecFile
1305 * Build an assembly file from a spec file.
1307 static int BuildSpecFile( FILE *outfile, char *specname )
1309 SpecName = specname;
1310 SpecFp = fopen( specname, "r");
1311 if (SpecFp == NULL)
1313 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1314 return -1;
1317 if (ParseTopLevel() < 0) return -1;
1319 switch(SpecType)
1321 case SPEC_WIN16:
1322 return BuildSpec16File( specname, outfile );
1323 case SPEC_WIN32:
1324 return BuildSpec32File( specname, outfile );
1325 default:
1326 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1327 return -1;
1332 /*******************************************************************
1333 * TransferArgs16To32
1335 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1336 * The 16-bit stack layout is:
1337 * ... ...
1338 * (bp+8) arg2
1339 * (bp+6) arg1
1340 * (bp+4) cs
1341 * (bp+2) ip
1342 * (bp) bp
1344 * For 'cdecl' argn up to arg1 are reversed.
1346 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1348 int i, pos16, pos32;
1349 char *xargs;
1351 /* Copy the arguments */
1353 pos16 = 6; /* skip bp and return address */
1354 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1355 xargs = usecdecl ? args:args+strlen(args);
1357 for (i = strlen(args); i > 0; i--)
1359 if (!usecdecl) {
1360 pos32 -= 4;
1361 xargs--;
1363 switch(*xargs)
1365 case 'w': /* word */
1366 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1367 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1368 pos16 += 2;
1369 break;
1371 case 's': /* s_word */
1372 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1373 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1374 pos16 += 2;
1375 break;
1377 case 'l': /* long or segmented pointer */
1378 case 'T': /* segmented pointer to null-terminated string */
1379 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1380 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1381 pos16 += 4;
1382 break;
1384 case 'p': /* linear pointer */
1385 case 't': /* linear pointer to null-terminated string */
1386 /* Get the selector */
1387 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1388 /* Get the selector base */
1389 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1390 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1391 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1392 /* Add the offset */
1393 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1394 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1395 pos16 += 4;
1396 break;
1398 default:
1399 fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1401 if (usecdecl) {
1402 pos32 += 4;
1403 xargs++;
1407 return pos16 - 6; /* Return the size of the 16-bit args */
1411 /*******************************************************************
1412 * BuildContext16
1414 * Build the context structure on the 32-bit stack.
1416 static void BuildContext16( FILE *outfile )
1418 /* Store the registers */
1420 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1421 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1422 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1423 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1424 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1425 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1426 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1427 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1428 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1429 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1431 fprintf( outfile, "\tmovl -24(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1432 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1433 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1434 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1435 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1436 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1437 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1438 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1439 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1440 fprintf( outfile, "\tpushfl\n" );
1441 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1442 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1443 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1444 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1445 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1446 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1447 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1448 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1449 fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1450 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1451 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1452 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1453 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1454 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1455 fprintf( outfile, "\tmovzwl -14(%%ebp),%%eax\n" ); /* Get %fs from stack */
1456 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1457 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1458 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1459 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1460 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1461 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1462 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1463 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1464 #if 0
1465 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1466 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1467 #endif
1471 /*******************************************************************
1472 * RestoreContext16
1474 * Restore the registers from the context structure.
1476 static void RestoreContext16( FILE *outfile )
1478 /* Get the 32-bit stack pointer */
1480 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1481 STRUCTOFFSET(STACK32FRAME,ebp) );
1483 /* Remove everything up to (including) the return address
1484 * from the 16-bit stack */
1486 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1487 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1488 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1489 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1490 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1491 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1493 /* Restore the registers */
1495 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1496 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1497 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1498 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1499 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1500 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1501 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1502 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1503 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1504 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1505 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1506 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1507 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1508 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1509 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1510 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1511 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1512 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1513 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new fs */
1514 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1515 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1516 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1517 fprintf( outfile, "\tpopfl\n" );
1518 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1519 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1520 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1521 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1522 fprintf( outfile, "\tpopl %%fs\n" ); /* Set fs */
1523 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1524 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1528 /*******************************************************************
1529 * BuildCallFrom16Func
1531 * Build a 16-bit-to-Wine callback function. The syntax of the function
1532 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1533 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1534 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1535 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1536 * 'T'=segmented pointer to null-terminated string).
1537 * For register functions, the arguments are ignored, but they are still
1538 * removed from the stack upon return.
1540 * A special variant of the callback function is generated by the function
1541 * profile "t_long_". This is used by the Win95 16->32 thunk
1542 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1543 * On entry, the EBX register is set up to contain a flat pointer to the
1544 * 16-bit stack such that EBX+22 points to the first argument.
1545 * Then, the entry point is called, while EBP is set up to point
1546 * to the return address (on the 32-bit stack).
1547 * The called function returns with CX set to the number of bytes
1548 * to be popped of the caller's stack.
1550 * Stack layout upon entry to the callback function:
1551 * ... ...
1552 * (sp+18) word first 16-bit arg
1553 * (sp+16) word cs
1554 * (sp+14) word ip
1555 * (sp+12) word bp
1556 * (sp+8) long 32-bit entry point (used to store edx)
1557 * (sp+6) word high word of cs (always 0, used to store es)
1558 * (sp+4) word low word of cs of 16-bit entry point
1559 * (sp+2) word high word of ip (always 0, used to store ds)
1560 * (sp) word low word of ip of 16-bit entry point
1562 * Added on the stack:
1563 * (sp-2) word saved fs
1564 * (sp-4) word buffer for Win16Mutex recursion count
1565 * (sp-8) long ebp
1566 * (sp-12) long saved previous stack
1568 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1570 int argsize = 0;
1571 int short_ret = 0;
1572 int reg_func = 0;
1573 int cdecl = 0;
1574 int thunk = 0;
1575 char *args = profile + 7;
1577 /* Parse function type */
1579 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1580 else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1581 else if (strncmp( "p_", profile, 2 ))
1583 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1584 return;
1587 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1588 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1589 else if (strncmp( "long_", profile + 2, 5 ))
1591 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1592 return;
1595 /* Function header */
1597 fprintf( outfile, "\n\t.align 4\n" );
1598 #ifdef USE_STABS
1599 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1600 profile, profile);
1601 #endif
1602 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1603 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1605 /* Save 16-bit fs and leave room for Win16Mutex recursion count */
1607 fprintf( outfile, "\t.byte 0x66\n\tpushl %%fs\n" );
1608 fprintf( outfile, "\tpushw $0\n" );
1610 /* Setup bp to point to its copy on the stack */
1612 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1613 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1614 fprintf( outfile, "\taddw $20,%%bp\n" );
1616 /* Save 16-bit ds and es */
1618 /* Stupid FreeBSD assembler doesn't know these either */
1619 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1620 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1621 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1622 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1624 /* Save %ebx */
1626 fprintf( outfile, "\tpushl %%ebx\n" );
1628 /* Restore 32-bit segment registers */
1630 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1631 #ifdef __svr4__
1632 fprintf( outfile, "\tdata16\n");
1633 #endif
1634 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1635 #ifdef __svr4__
1636 fprintf( outfile, "\tdata16\n");
1637 #endif
1638 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1640 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
1642 /* Get the 32-bit stack pointer from the TEB */
1644 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1646 /* Save the 16-bit stack */
1648 #ifdef __svr4__
1649 fprintf( outfile,"\tdata16\n");
1650 #endif
1651 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1652 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1654 /* Transfer the arguments */
1656 if (reg_func) BuildContext16( outfile );
1657 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1658 else if (thunk)
1660 /* Get the stack selector base */
1661 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1662 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1663 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1664 fprintf( outfile, "\tmovl %%eax,-24(%%ebp)\n" );
1665 /* Add the offset */
1666 fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1667 fprintf( outfile, "\taddl %%eax,-24(%%ebp)\n" );
1670 /* Get the address of the API function */
1672 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1674 /* If necessary, save %edx over the API function address */
1676 if (!reg_func && short_ret)
1677 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1679 /* Restore %ebx and store the 32-bit stack pointer instead */
1681 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1682 fprintf( outfile, "\tpopl %%ebx\n" );
1683 fprintf( outfile, "\tpushl %%ebp\n" );
1685 /* Switch to the 32-bit stack */
1687 fprintf( outfile, "\tpushl %%ds\n" );
1688 fprintf( outfile, "\tpopl %%ss\n" );
1689 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1690 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1691 if (reg_func) /* Push the address of the context struct */
1692 fprintf( outfile, "\tpushl %%esp\n" );
1694 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1696 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1698 /* Print the debug information before the call */
1700 if (debugging && !thunk)
1702 int ftype = 0;
1704 if (cdecl) ftype |= 4;
1705 if (reg_func) ftype |= 2;
1706 if (short_ret) ftype |= 1;
1708 fprintf( outfile, "\tpushl %%eax\n" );
1709 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1710 fprintf( outfile, "\tpushl $%d\n", ftype );
1711 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1712 fprintf( outfile, "\tpopl %%eax\n" );
1713 fprintf( outfile, "\tpopl %%eax\n" );
1714 fprintf( outfile, "\tpopl %%eax\n" );
1717 /* Call the entry point */
1719 if (thunk)
1721 fprintf( outfile, "\tpushl %%ebp\n" );
1722 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1723 fprintf( outfile, "\tcall *%%eax\n" );
1724 fprintf( outfile, "\tpopl %%ebp\n" );
1726 else
1727 fprintf( outfile, "\tcall *%%eax\n" );
1730 /* Print the debug information after the call */
1732 if (debugging && !thunk)
1734 if (reg_func)
1736 /* Push again the address of the context struct in case */
1737 /* it has been removed by an stdcall function */
1738 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1739 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1740 fprintf( outfile, "\tpushl %%esp\n" );
1742 fprintf( outfile, "\tpushl %%eax\n" );
1743 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1744 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1745 fprintf( outfile, "\tpopl %%eax\n" );
1746 fprintf( outfile, "\tpopl %%eax\n" );
1749 /* Restore the 16-bit stack */
1751 #ifdef __svr4__
1752 fprintf( outfile, "\tdata16\n");
1753 #endif
1754 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1755 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1756 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1758 if (reg_func)
1760 /* Calc the arguments size */
1761 while (*args)
1763 switch(*args)
1765 case 'w':
1766 case 's':
1767 argsize += 2;
1768 break;
1769 case 'p':
1770 case 't':
1771 case 'l':
1772 case 'T':
1773 argsize += 4;
1774 break;
1775 default:
1776 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1778 args++;
1781 /* Restore registers from the context structure */
1782 RestoreContext16( outfile );
1784 else
1786 /* Restore high 16 bits of ebp */
1787 fprintf( outfile, "\tpopl %%ebp\n" );
1789 /* Restore ds and es */
1790 fprintf( outfile, "\tincl %%esp\n" ); /* Remove mutex count */
1791 fprintf( outfile, "\tincl %%esp\n" );
1792 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove ip and fs */
1793 fprintf( outfile, "\tmovw %%dx,%%fs\n" ); /* and restore fs */
1794 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1795 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1796 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1798 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1799 else
1801 /* Get the return value into dx:ax */
1802 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1803 fprintf( outfile, "\tshrl $16,%%edx\n" );
1804 /* Remove API entry point */
1805 fprintf( outfile, "\taddl $4,%%esp\n" );
1808 /* Restore low 16 bits of ebp */
1809 fprintf( outfile, "\tpopw %%bp\n" );
1812 /* Remove the arguments and return */
1814 if (thunk)
1816 fprintf( outfile, "\tpopl %%ebx\n" );
1817 fprintf( outfile, "\txorb %%ch,%%ch\n" );
1818 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1819 fprintf( outfile, "\tpushl %%ebx\n" );
1820 fprintf( outfile, "\t.byte 0x66\n" );
1821 fprintf( outfile, "\tlret\n" );
1823 else if (argsize && !cdecl)
1825 fprintf( outfile, "\t.byte 0x66\n" );
1826 fprintf( outfile, "\tlret $%d\n", argsize );
1828 else
1830 fprintf( outfile, "\t.byte 0x66\n" );
1831 fprintf( outfile, "\tlret\n" );
1836 /*******************************************************************
1837 * BuildCallTo16Func
1839 * Build a Wine-to-16-bit callback function.
1841 * Stack frame of the callback function:
1842 * ... ...
1843 * (ebp+16) arg2
1844 * (ebp+12) arg1
1845 * (ebp+8) func to call
1846 * (ebp+4) return address
1847 * (ebp) previous ebp
1849 * Prototypes for the CallTo16 functions:
1850 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1851 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1852 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1853 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1855 static void BuildCallTo16Func( FILE *outfile, char *profile )
1857 int short_ret = 0;
1858 int reg_func = 0;
1859 char *args = profile + 5;
1861 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1862 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1863 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1864 else if (strncmp( "long_", profile, 5 ))
1866 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1867 exit(1);
1870 /* Function header */
1872 fprintf( outfile, "\n\t.align 4\n" );
1873 #ifdef USE_STABS
1874 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1875 profile, profile);
1876 #endif
1877 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1878 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1880 /* Entry code */
1882 fprintf( outfile, "\tpushl %%ebp\n" );
1883 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1885 /* Save the 32-bit registers */
1887 fprintf( outfile, "\tpushl %%ebx\n" );
1888 fprintf( outfile, "\tpushl %%ecx\n" );
1889 fprintf( outfile, "\tpushl %%edx\n" );
1890 fprintf( outfile, "\tpushl %%esi\n" );
1891 fprintf( outfile, "\tpushl %%edi\n" );
1893 /* Enter Win16 Mutex */
1895 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
1897 /* Print debugging info */
1899 if (debugging)
1901 /* Push the address of the first argument */
1902 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1903 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1904 fprintf( outfile, "\tpushl %%eax\n" );
1905 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1906 fprintf( outfile, "\tpopl %%eax\n" );
1907 fprintf( outfile, "\tpopl %%eax\n" );
1910 /* Call the actual CallTo16 routine (simulate a lcall) */
1912 fprintf( outfile, "\tpushl %%cs\n" );
1913 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1915 fprintf( outfile, "\tpushl %%eax\n" );
1917 /* Print debugging info */
1919 if (debugging)
1921 fprintf( outfile, "\tpushl %%eax\n" );
1922 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
1923 fprintf( outfile, "\tpopl %%eax\n" );
1926 /* Leave Win16 Mutex */
1928 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
1930 /* Restore the 32-bit registers */
1932 fprintf( outfile, "\tpopl %%eax\n" );
1933 fprintf( outfile, "\tpopl %%edi\n" );
1934 fprintf( outfile, "\tpopl %%esi\n" );
1935 fprintf( outfile, "\tpopl %%edx\n" );
1936 fprintf( outfile, "\tpopl %%ecx\n" );
1937 fprintf( outfile, "\tpopl %%ebx\n" );
1939 /* Exit code */
1941 #if 0
1942 /* FIXME: this is a hack because of task.c */
1943 if (!strcmp( profile, "word_" ))
1945 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1946 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1948 #endif
1949 fprintf( outfile, "\tpopl %%ebp\n" );
1950 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1953 /* Start of the actual CallTo16 routine */
1955 fprintf( outfile, "do_callto16_%s:\n", profile );
1957 /* Save the 32-bit stack */
1959 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1960 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1961 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1963 if (reg_func)
1965 /* Switch to the 16-bit stack, saving the current %%esp, */
1966 /* and adding the specified offset to the new sp */
1967 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1968 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1969 #ifdef __svr4__
1970 fprintf( outfile,"\tdata16\n");
1971 #endif
1972 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1973 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1974 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1976 /* Get the registers. ebx is handled later on. */
1978 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1979 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1980 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1981 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegFs) );
1982 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
1983 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1984 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1985 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1986 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1987 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1988 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1990 /* Push the return address
1991 * With sreg suffix, we push 16:16 address (normal lret)
1992 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1994 if (reg_func == 1)
1995 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1996 else
1998 fprintf( outfile, "\tpushw $0\n" );
1999 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
2000 fprintf( outfile, "\tpushw $0\n" );
2001 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
2004 /* Push the called routine address */
2006 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2007 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2009 /* Get the 16-bit ds */
2011 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2012 /* Get ebx from the 32-bit stack */
2013 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2014 fprintf( outfile, "\tpopl %%ds\n" );
2016 else /* not a register function */
2018 int pos = 12; /* first argument position */
2020 /* Switch to the 16-bit stack */
2021 #ifdef __svr4__
2022 fprintf( outfile,"\tdata16\n");
2023 #endif
2024 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2025 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2026 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2028 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2029 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2030 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
2031 STRUCTOFFSET(STACK16FRAME,bp) );
2033 /* Transfer the arguments */
2035 while (*args)
2037 switch(*args++)
2039 case 'w': /* word */
2040 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
2041 break;
2042 case 'l': /* long */
2043 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
2044 break;
2045 default:
2046 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
2047 args[-1] );
2049 pos += 4;
2052 /* Push the return address */
2054 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
2055 short_ret ? "word" : "long" );
2057 /* Push the called routine address */
2059 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
2061 /* Set %fs to the value saved by the last CallFrom16 */
2063 fprintf( outfile, "\tmovw -14(%%ebp),%%ax\n" );
2064 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2066 /* Set %ds and %es (and %ax just in case) equal to %ss */
2068 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2069 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2070 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2073 /* Jump to the called routine */
2075 fprintf( outfile, "\t.byte 0x66\n" );
2076 fprintf( outfile, "\tlret\n" );
2080 /*******************************************************************
2081 * BuildRet16Func
2083 * Build the return code for 16-bit callbacks
2085 static void BuildRet16Func( FILE *outfile )
2087 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2088 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2089 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2091 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2092 fprintf( outfile, "\txorl %%edx,%%edx\n" );
2094 /* Put return value into %eax */
2096 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2097 fprintf( outfile, "\tshll $16,%%edx\n" );
2098 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2099 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2100 fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2102 /* Restore 32-bit segment registers */
2104 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2105 #ifdef __svr4__
2106 fprintf( outfile, "\tdata16\n");
2107 #endif
2108 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2109 #ifdef __svr4__
2110 fprintf( outfile, "\tdata16\n");
2111 #endif
2112 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2114 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2116 /* Restore the 32-bit stack */
2118 #ifdef __svr4__
2119 fprintf( outfile, "\tdata16\n");
2120 #endif
2121 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2122 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2123 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2125 /* Return to caller */
2127 fprintf( outfile, "\tlret\n" );
2129 /* Declare the return address variables */
2131 fprintf( outfile, "\t.data\n" );
2132 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2133 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2134 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2135 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2136 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2137 fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2138 fprintf( outfile, "\t.text\n" );
2141 /*******************************************************************
2142 * BuildCallTo32CBClient
2144 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2146 * Since the relay stub is itself 32-bit, this should not be a problem;
2147 * unfortunately, the relay stubs are expected to switch back to a
2148 * 16-bit stack (and 16-bit code) after completion :-(
2150 * This would conflict with our 16- vs. 32-bit stack handling, so
2151 * we simply switch *back* to our 32-bit stack before returning to
2152 * the caller ...
2154 * The CBClient relay stub expects to be called with:
2155 * - ebp pointing to the 16-bit stack at ss:bp
2156 * - ebx pointing to a buffer containing the saved 16-bit ss:sp
2158 * After completion, the stub will load ss:sp from the buffer at ebx
2159 * and perform a far return to 16-bit code.
2161 * To trick the relay stub into returning to us, we push a 16-bit
2162 * cs:ip pair pointing to our return entry point onto the 16-bit stack,
2163 * followed by a ss:sp pair pointing to *that* cs:ip pair.
2164 * Our return stub thus called will then reload the 32-bit ss:esp and
2165 * return to 32-bit code (by using and ss:esp value that we have also
2166 * pushed onto the 16-bit stack before and a cs:eip values found at
2167 * that position on the 32-bit stack). The layout of our
2168 * temporary area used on the 16-bit stack is thus as follows:
2170 * (ebx+12) 32-bit ss (flat)
2171 * (ebx+8) 32-bit sp (32-bit stack pointer)
2172 * (ebx+6) 16-bit cs (this segment)
2173 * (ebx+4) 16-bit ip ('16-bit' return entry point)
2174 * (ebx+2) 16-bit ss (16-bit stack segment)
2175 * (ebx+0) 16-bit sp (points to ebx+4)
2177 * The second variant of this routine, CALL32_CBClientEx, which is used
2178 * to implement KERNEL.621, has to cope with yet another problem: Here,
2179 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2180 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2181 * As we have to return to our 32-bit code first, we have to adapt the
2182 * layout of our temporary area so as to include values for the registers
2183 * that are to be restored, and later (in the implementation of KERNEL.621)
2184 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2185 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2186 * and then performs a lret NN, where NN is the number of arguments to be
2187 * removed. Thus, we prepare our temporary area as follows:
2189 * (ebx+22) 16-bit cs (this segment)
2190 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2191 * (ebx+16) 32-bit ss (flat)
2192 * (ebx+12) 32-bit sp (32-bit stack pointer)
2193 * (ebx+10) 16-bit bp (points to ebx+24)
2194 * (ebx+8) 16-bit si (ignored)
2195 * (ebx+6) 16-bit di (ignored)
2196 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2197 * (ebx+2) 16-bit ss (16-bit stack segment)
2198 * (ebx+0) 16-bit sp (points to ebx+4)
2200 * Note that we ensure that DS is not changed and remains the flat segment,
2201 * and the 32-bit stack pointer our own return stub needs fits just
2202 * perfectly into the 8 bytes that are skipped by the Windows stub.
2203 * One problem is that we have to determine the number of removed arguments,
2204 * as these have to be really removed in KERNEL.621. Thus, the BP value
2205 * that we place in the temporary area to be restored, contains the value
2206 * that SP would have if no arguments were removed. By comparing the actual
2207 * value of SP with this value in our return stub we can compute the number
2208 * of removed arguments. This is then returned to KERNEL.621.
2210 * The stack layout of this function:
2211 * (ebp+16) nArgs pointer to variable receiving nr. of args (Ex only)
2212 * (ebp+12) arg ebp value to be set for relay stub
2213 * (ebp+8) func CBClient relay stub address
2214 * (ebp+4) ret addr
2215 * (ebp) ebp
2217 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2219 char *name = isEx? "CBClientEx" : "CBClient";
2220 int size = isEx? 24 : 16;
2222 /* Function header */
2224 fprintf( outfile, "\n\t.align 4\n" );
2225 #ifdef USE_STABS
2226 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2227 name, name );
2228 #endif
2229 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2230 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2232 /* Entry code */
2234 fprintf( outfile, "\tpushl %%ebp\n" );
2235 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2236 fprintf( outfile, "\tpushl %%edi\n" );
2237 fprintf( outfile, "\tpushl %%esi\n" );
2238 fprintf( outfile, "\tpushl %%ebx\n" );
2240 /* Get the 16-bit stack */
2242 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2244 /* Convert it to a flat address */
2246 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2247 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2248 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2249 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2250 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2252 /* Allocate temporary area (simulate STACK16_PUSH) */
2254 fprintf( outfile, "\tpushf\n" );
2255 fprintf( outfile, "\tcld\n" );
2256 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2257 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2258 fprintf( outfile, "\trep\n\tmovsb\n" );
2259 fprintf( outfile, "\tpopf\n" );
2261 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2263 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2265 /* Set up temporary area */
2267 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2268 fprintf( outfile, "\tmovl %%ebx, (%%edi)\n" );
2270 if ( !isEx )
2272 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2273 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" );
2275 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2276 fprintf( outfile, "\tmovl %%eax, 8(%%edi)\n" );
2278 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2279 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2280 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2282 else
2284 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2285 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2287 fprintf( outfile, "\taddl $20, %%ebx\n" );
2288 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2290 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2291 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2293 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2294 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2295 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2297 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2298 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2301 /* Setup registers and call CBClient relay stub (simulating a far call) */
2303 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2304 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2305 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2307 fprintf( outfile, "\tpushl %%cs\n" );
2308 fprintf( outfile, "\tcall *%%eax\n" );
2310 /* Cleanup temporary area (simulate STACK16_POP) */
2312 fprintf( outfile, "\tpop %%esi\n" );
2314 fprintf( outfile, "\tpushf\n" );
2315 fprintf( outfile, "\tstd\n" );
2316 fprintf( outfile, "\tdec %%esi\n" );
2317 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2318 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2319 fprintf( outfile, "\trep\n\tmovsb\n" );
2320 fprintf( outfile, "\tpopf\n" );
2322 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2324 /* Return argument size to caller */
2325 if ( isEx )
2327 fprintf( outfile, "\tmovl 28(%%esp), %%ebx\n" );
2328 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2331 /* Restore registers and return */
2333 fprintf( outfile, "\tpopl %%ebx\n" );
2334 fprintf( outfile, "\tpopl %%esi\n" );
2335 fprintf( outfile, "\tpopl %%edi\n" );
2336 fprintf( outfile, "\tpopl %%ebp\n" );
2337 fprintf( outfile, "\tret\n" );
2339 /* '16-bit' return stub */
2341 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2342 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2344 if ( !isEx )
2346 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2347 fprintf( outfile, "\tlssl %%ss:(%%ebx), %%esp\n" );
2349 else
2351 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2352 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2353 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2354 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2356 fprintf( outfile, "\tlret\n" );
2358 /* Declare the return address variable */
2360 fprintf( outfile, "\t.data\n" );
2361 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2362 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2363 fprintf( outfile, "\t.text\n" );
2368 /*******************************************************************
2369 * BuildCallTo32LargeStack
2371 * Build the function used to switch to the original 32-bit stack
2372 * before calling a 32-bit function from 32-bit code. This is used for
2373 * functions that need a large stack, like X bitmaps functions.
2375 * The generated function has the following prototype:
2376 * int xxx( int (*func)(), void *arg );
2378 * The pointer to the function can be retrieved by calling CALL32_Init,
2379 * which also takes care of saving the current 32-bit stack pointer.
2381 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2382 * same thread, but not concurrently entered by several threads.
2384 * Stack layout:
2385 * ... ...
2386 * (ebp+12) arg
2387 * (ebp+8) func
2388 * (ebp+4) ret addr
2389 * (ebp) ebp
2391 static void BuildCallTo32LargeStack( FILE *outfile )
2393 /* Initialization function */
2395 fprintf( outfile, "\n\t.align 4\n" );
2396 #ifdef USE_STABS
2397 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2398 #endif
2399 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2400 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2401 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2402 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2403 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2404 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2405 fprintf( outfile, "\tret\n" );
2407 /* Function header */
2409 fprintf( outfile, "\n\t.align 4\n" );
2410 #ifdef USE_STABS
2411 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2412 #endif
2413 fprintf( outfile, "CALL32_LargeStack:\n" );
2415 /* Entry code */
2417 fprintf( outfile, "\tpushl %%ebp\n" );
2418 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2420 /* Switch to the original 32-bit stack pointer */
2422 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2423 fprintf( outfile, "\tjne CALL32_skip\n" );
2424 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2425 fprintf( outfile, "CALL32_skip:\n" );
2427 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2429 /* Transfer the argument and call the function */
2431 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2432 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2434 /* Restore registers and return */
2436 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2438 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2439 fprintf( outfile, "\tpopl %%ebp\n" );
2440 fprintf( outfile, "\tret\n" );
2442 /* Data */
2444 fprintf( outfile, "\t.data\n" );
2445 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2446 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2447 fprintf( outfile, "\t.text\n" );
2451 /*******************************************************************
2452 * BuildCallFrom32Regs
2454 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2455 * 'args' is the number of dword arguments.
2457 * Stack layout:
2458 * ... ...
2459 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2460 * (esp+332) entry point
2461 * (esp+204) buffer area to allow stack frame manipulation
2462 * (esp+0) CONTEXT struct
2464 static void BuildCallFrom32Regs( FILE *outfile )
2466 #define STACK_SPACE 128
2468 /* Function header */
2470 fprintf( outfile, "\n\t.align 4\n" );
2471 #ifdef USE_STABS
2472 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2473 #endif
2474 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2475 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2477 /* Allocate some buffer space on the stack */
2479 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2481 /* Build the context structure */
2483 fprintf( outfile, "\tpushw $0\n" );
2484 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2485 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2486 fprintf( outfile, "\tpushfl\n" );
2487 fprintf( outfile, "\tpushw $0\n" );
2488 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2489 fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 ); /* %eip at time of call */
2490 fprintf( outfile, "\tpushl %%ebp\n" );
2492 fprintf( outfile, "\tpushl %%eax\n" );
2493 fprintf( outfile, "\tpushl %%ecx\n" );
2494 fprintf( outfile, "\tpushl %%edx\n" );
2495 fprintf( outfile, "\tpushl %%ebx\n" );
2496 fprintf( outfile, "\tpushl %%esi\n" );
2497 fprintf( outfile, "\tpushl %%edi\n" );
2499 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2500 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2501 fprintf( outfile, "\tpushl %%eax\n" );
2502 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2503 fprintf( outfile, "\tpushl %%eax\n" );
2504 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2505 fprintf( outfile, "\tpushl %%eax\n" );
2506 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2507 fprintf( outfile, "\tpushl %%eax\n" );
2509 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2510 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2511 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2513 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2515 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2516 sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2517 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2519 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2521 /* Restore the context structure */
2523 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2525 /* Store %eip value onto the new stack */
2527 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2528 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2529 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2531 /* Restore all registers */
2533 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2534 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2535 fprintf( outfile, "\tpopl %%eax\n" );
2536 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2537 fprintf( outfile, "\tpopl %%eax\n" );
2538 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2539 fprintf( outfile, "\tpopl %%eax\n" );
2540 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2541 fprintf( outfile, "\tpopl %%eax\n" );
2542 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2544 fprintf( outfile, "\tpopl %%edi\n" );
2545 fprintf( outfile, "\tpopl %%esi\n" );
2546 fprintf( outfile, "\tpopl %%ebx\n" );
2547 fprintf( outfile, "\tpopl %%edx\n" );
2548 fprintf( outfile, "\tpopl %%ecx\n" );
2549 fprintf( outfile, "\tpopl %%eax\n" );
2550 fprintf( outfile, "\tpopl %%ebp\n" );
2551 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2552 fprintf( outfile, "\tpopfl\n" );
2553 fprintf( outfile, "\tpopl %%esp\n" );
2554 fprintf( outfile, "\tret\n" );
2556 #undef STACK_SPACE
2560 /*******************************************************************
2561 * BuildSpec
2563 * Build the spec files
2565 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2567 int i;
2568 for (i = 2; i < argc; i++)
2569 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2570 return 0;
2574 /*******************************************************************
2575 * BuildCallFrom16
2577 * Build the 16-bit-to-Wine callbacks
2579 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2581 int i;
2582 char buffer[1024];
2584 /* File header */
2586 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2587 fprintf( outfile, "\t.text\n" );
2589 #ifdef USE_STABS
2590 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2591 getcwd(buffer, sizeof(buffer));
2594 * The stabs help the internal debugger as they are an indication that it
2595 * is sensible to step into a thunk/trampoline.
2597 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2598 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2599 fprintf( outfile, "\t.text\n" );
2600 fprintf( outfile, "\t.align 4\n" );
2601 fprintf( outfile, "Code_Start:\n\n" );
2602 #endif
2603 fprintf( outfile, PREFIX"CallFrom16_Start:\n" );
2604 fprintf( outfile, "\t.globl "PREFIX"CallFrom16_Start\n" );
2606 /* Build the callback functions */
2608 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2610 /* Build the thunk callback function */
2612 BuildCallFrom16Func( outfile, "t_long_" );
2614 /* Output the argument debugging strings */
2616 if (debugging)
2618 fprintf( outfile, "/* Argument strings */\n" );
2619 for (i = 2; i < argc; i++)
2621 fprintf( outfile, "Profile_%s:\t", argv[i] );
2622 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2625 fprintf( outfile, PREFIX"CallFrom16_End:\n" );
2626 fprintf( outfile, "\t.globl "PREFIX"CallFrom16_End\n" );
2628 #ifdef USE_STABS
2629 fprintf( outfile, "\t.text\n");
2630 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2631 fprintf( outfile, ".Letext:\n");
2632 #endif
2634 return 0;
2638 /*******************************************************************
2639 * BuildCallTo16
2641 * Build the Wine-to-16-bit callbacks
2643 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2645 char buffer[1024];
2646 FILE *infile;
2648 if (argc > 2)
2650 infile = fopen( argv[2], "r" );
2651 if (!infile)
2653 perror( argv[2] );
2654 exit( 1 );
2657 else infile = stdin;
2659 /* File header */
2661 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2662 fprintf( outfile, "\t.text\n" );
2664 #ifdef USE_STABS
2665 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2666 getcwd(buffer, sizeof(buffer));
2669 * The stabs help the internal debugger as they are an indication that it
2670 * is sensible to step into a thunk/trampoline.
2672 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2673 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2674 fprintf( outfile, "\t.text\n" );
2675 fprintf( outfile, "\t.align 4\n" );
2676 fprintf( outfile, "Code_Start:\n\n" );
2677 #endif
2679 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2680 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2682 /* Build the callback functions */
2684 while (fgets( buffer, sizeof(buffer), infile ))
2686 if (strstr( buffer, "### start build ###" )) break;
2688 while (fgets( buffer, sizeof(buffer), infile ))
2690 char *p = strstr( buffer, "CallTo16_" );
2691 if (p)
2693 char *profile = p + strlen( "CallTo16_" );
2694 p = profile;
2695 while ((*p == '_') || isalpha(*p)) p++;
2696 *p = '\0';
2697 BuildCallTo16Func( outfile, profile );
2699 if (strstr( buffer, "### stop build ###" )) break;
2702 /* Output the 16-bit return code */
2704 BuildRet16Func( outfile );
2706 /* Output the CBClient callback functions
2707 * (while this does not really 'call to 16-bit' code, it is placed
2708 * here so that its 16-bit return stub is defined within the CALLTO16
2709 * 16-bit segment)
2711 BuildCallTo32CBClient( outfile, FALSE );
2712 BuildCallTo32CBClient( outfile, TRUE );
2715 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2716 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2718 #ifdef USE_STABS
2719 fprintf( outfile, "\t.text\n");
2720 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2721 fprintf( outfile, ".Letext:\n");
2722 #endif
2724 fclose( infile );
2725 return 0;
2729 /*******************************************************************
2730 * BuildCall32
2732 * Build the 32-bit callbacks
2734 static int BuildCall32( FILE *outfile, char * outname )
2736 char buffer[1024];
2738 /* File header */
2740 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2741 fprintf( outfile, "\t.text\n" );
2743 #ifdef __i386__
2745 #ifdef USE_STABS
2746 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2747 getcwd(buffer, sizeof(buffer));
2750 * The stabs help the internal debugger as they are an indication that it
2751 * is sensible to step into a thunk/trampoline.
2753 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2754 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2755 fprintf( outfile, "\t.text\n" );
2756 fprintf( outfile, "\t.align 4\n" );
2757 fprintf( outfile, "Code_Start:\n" );
2758 #endif
2760 /* Build the 32-bit large stack callback */
2762 BuildCallTo32LargeStack( outfile );
2764 /* Build the register callback function */
2766 BuildCallFrom32Regs( outfile );
2768 #ifdef USE_STABS
2769 fprintf( outfile, "\t.text\n");
2770 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2771 fprintf( outfile, ".Letext:\n");
2772 #endif
2774 #else /* __i386__ */
2776 /* Just to avoid an empty file */
2777 fprintf( outfile, "\t.long 0\n" );
2779 #endif /* __i386__ */
2780 return 0;
2784 /*******************************************************************
2785 * usage
2787 static void usage(void)
2789 fprintf( stderr,
2790 "usage: build [-o outfile] -spec SPECNAMES\n"
2791 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2792 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2793 " build [-o outfile] -call32\n" );
2794 exit(1);
2798 /*******************************************************************
2799 * main
2801 int main(int argc, char **argv)
2803 char *outname = NULL;
2804 FILE *outfile = stdout;
2805 int res = -1;
2807 if (argc < 2) usage();
2809 if (!strcmp( argv[1], "-o" ))
2811 outname = argv[2];
2812 argv += 2;
2813 argc -= 2;
2814 if (argc < 2) usage();
2815 if (!(outfile = fopen( outname, "w" )))
2817 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2818 exit(1);
2822 /* Retrieve the selector values; this assumes that we are building
2823 * the asm files on the platform that will also run them. Probably
2824 * a safe assumption to make.
2826 GET_CS( Code_Selector );
2827 GET_DS( Data_Selector );
2829 if (!strcmp( argv[1], "-spec" ))
2830 res = BuildSpec( outfile, argc, argv );
2831 else if (!strcmp( argv[1], "-callfrom16" ))
2832 res = BuildCallFrom16( outfile, outname, argc, argv );
2833 else if (!strcmp( argv[1], "-callto16" ))
2834 res = BuildCallTo16( outfile, outname, argc, argv );
2835 else if (!strcmp( argv[1], "-call32" ))
2836 res = BuildCall32( outfile, outname );
2837 else
2839 fclose( outfile );
2840 unlink( outname );
2841 usage();
2844 fclose( outfile );
2845 if (res < 0)
2847 unlink( outname );
2848 return 1;
2850 return 0;