Fixed the height of the ComboBox drop list.
[wine/hacks.git] / tools / build.c
blob6e0206a370945ae50da721386a382a5309fbbe3a
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 * Copyright 1999 Ulrich Weigand
7 */
9 #include "config.h"
11 #include <assert.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <unistd.h>
18 #include "winbase.h"
19 #include "winnt.h"
20 #include "module.h"
21 #include "neexe.h"
22 #include "selectors.h"
23 #include "stackframe.h"
24 #include "builtin16.h"
25 #include "thread.h"
27 #ifdef NEED_UNDERSCORE_PREFIX
28 # define PREFIX "_"
29 #else
30 # define PREFIX
31 #endif
33 #ifdef HAVE_ASM_STRING
34 # define STRING ".string"
35 #else
36 # define STRING ".ascii"
37 #endif
39 #if defined(__GNUC__) && !defined(__svr4__)
40 # define USE_STABS
41 #else
42 # undef USE_STABS
43 #endif
45 typedef enum
47 TYPE_INVALID,
48 TYPE_BYTE, /* byte variable (Win16) */
49 TYPE_WORD, /* word variable (Win16) */
50 TYPE_LONG, /* long variable (Win16) */
51 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
52 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
53 TYPE_ABS, /* absolute value (Win16) */
54 TYPE_REGISTER, /* register function */
55 TYPE_INTERRUPT, /* interrupt handler function (Win16) */
56 TYPE_STUB, /* unimplemented stub */
57 TYPE_STDCALL, /* stdcall function (Win32) */
58 TYPE_CDECL, /* cdecl function (Win32) */
59 TYPE_VARARGS, /* varargs function (Win32) */
60 TYPE_EXTERN, /* external symbol (Win32) */
61 TYPE_FORWARD, /* forwarded function (Win32) */
62 TYPE_NBTYPES
63 } ORD_TYPE;
65 static const char * const TypeNames[TYPE_NBTYPES] =
67 NULL,
68 "byte", /* TYPE_BYTE */
69 "word", /* TYPE_WORD */
70 "long", /* TYPE_LONG */
71 "pascal16", /* TYPE_PASCAL_16 */
72 "pascal", /* TYPE_PASCAL */
73 "equate", /* TYPE_ABS */
74 "register", /* TYPE_REGISTER */
75 "interrupt", /* TYPE_INTERRUPT */
76 "stub", /* TYPE_STUB */
77 "stdcall", /* TYPE_STDCALL */
78 "cdecl", /* TYPE_CDECL */
79 "varargs", /* TYPE_VARARGS */
80 "extern", /* TYPE_EXTERN */
81 "forward" /* TYPE_FORWARD */
84 #define MAX_ORDINALS 2048
85 #define MAX_IMPORTS 16
87 /* Callback function used for stub functions */
88 #define STUB_CALLBACK \
89 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
91 typedef enum
93 SPEC_INVALID,
94 SPEC_WIN16,
95 SPEC_WIN32
96 } SPEC_TYPE;
98 typedef struct
100 int n_values;
101 int *values;
102 } ORD_VARIABLE;
104 typedef struct
106 int n_args;
107 char arg_types[32];
108 char link_name[80];
109 } ORD_FUNCTION;
111 typedef struct
113 int arg_size;
114 int ret_value;
115 } ORD_RETURN;
117 typedef struct
119 int value;
120 } ORD_ABS;
122 typedef struct
124 char link_name[80];
125 } ORD_EXTERN;
127 typedef struct
129 char link_name[80];
130 } ORD_FORWARD;
132 typedef struct
134 ORD_TYPE type;
135 int offset;
136 int lineno;
137 char name[80];
138 union
140 ORD_VARIABLE var;
141 ORD_FUNCTION func;
142 ORD_RETURN ret;
143 ORD_ABS abs;
144 ORD_EXTERN ext;
145 ORD_FORWARD fwd;
146 } u;
147 } ORDDEF;
149 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
151 static SPEC_TYPE SpecType = SPEC_INVALID;
152 static char DLLName[80];
153 static char DLLFileName[80];
154 static int Limit = 0;
155 static int Base = MAX_ORDINALS;
156 static int DLLHeapSize = 0;
157 static char *SpecName;
158 static FILE *SpecFp;
159 static WORD Code_Selector, Data_Selector;
160 static char DLLInitFunc[80];
161 static char *DLLImports[MAX_IMPORTS];
162 static int nb_imports = 0;
164 char *ParseBuffer = NULL;
165 char *ParseNext;
166 char ParseSaveChar;
167 int Line;
169 static int UsePIC = 0;
171 static int debugging = 1;
173 /* Offset of a structure field relative to the start of the struct */
174 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
176 /* Offset of register relative to the start of the CONTEXT struct */
177 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT86,reg)
179 /* Offset of register relative to the start of the STACK16FRAME struct */
180 #define STACK16OFFSET(reg) STRUCTOFFSET(STACK16FRAME,reg)
182 /* Offset of register relative to the start of the STACK32FRAME struct */
183 #define STACK32OFFSET(reg) STRUCTOFFSET(STACK32FRAME,reg)
185 /* Offset of the stack pointer relative to %fs:(0) */
186 #define STACKOFFSET (STRUCTOFFSET(TEB,cur_stack))
188 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local );
190 static void *xmalloc (size_t size)
192 void *res;
194 res = malloc (size ? size : 1);
195 if (res == NULL)
197 fprintf (stderr, "Virtual memory exhausted.\n");
198 exit (1);
200 return res;
204 static void *xrealloc (void *ptr, size_t size)
206 void *res = realloc (ptr, size);
207 if (res == NULL)
209 fprintf (stderr, "Virtual memory exhausted.\n");
210 exit (1);
212 return res;
215 static char *xstrdup( const char *str )
217 char *res = strdup( str );
218 if (!res)
220 fprintf (stderr, "Virtual memory exhausted.\n");
221 exit (1);
223 return res;
226 static int IsNumberString(char *s)
228 while (*s != '\0')
229 if (!isdigit(*s++))
230 return 0;
232 return 1;
235 static char *strupper(char *s)
237 char *p;
239 for(p = s; *p != '\0'; p++)
240 *p = toupper(*p);
242 return s;
245 static char * GetTokenInLine(void)
247 char *p;
248 char *token;
250 if (ParseNext != ParseBuffer)
252 if (ParseSaveChar == '\0')
253 return NULL;
254 *ParseNext = ParseSaveChar;
258 * Remove initial white space.
260 for (p = ParseNext; isspace(*p); p++)
263 if ((*p == '\0') || (*p == '#'))
264 return NULL;
267 * Find end of token.
269 token = p++;
270 if (*token != '(' && *token != ')')
271 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
272 p++;
274 ParseSaveChar = *p;
275 ParseNext = p;
276 *p = '\0';
278 return token;
281 static char * GetToken(void)
283 char *token;
285 if (ParseBuffer == NULL)
287 ParseBuffer = xmalloc(512);
288 ParseNext = ParseBuffer;
289 while (1)
291 Line++;
292 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
293 return NULL;
294 if (ParseBuffer[0] != '#')
295 break;
299 while ((token = GetTokenInLine()) == NULL)
301 ParseNext = ParseBuffer;
302 while (1)
304 Line++;
305 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
306 return NULL;
307 if (ParseBuffer[0] != '#')
308 break;
312 return token;
316 /*******************************************************************
317 * ParseVariable
319 * Parse a variable definition.
321 static int ParseVariable( ORDDEF *odp )
323 char *endptr;
324 int *value_array;
325 int n_values;
326 int value_array_size;
328 char *token = GetToken();
329 if (*token != '(')
331 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
332 SpecName, Line, token);
333 return -1;
336 n_values = 0;
337 value_array_size = 25;
338 value_array = xmalloc(sizeof(*value_array) * value_array_size);
340 while ((token = GetToken()) != NULL)
342 if (*token == ')')
343 break;
345 value_array[n_values++] = strtol(token, &endptr, 0);
346 if (n_values == value_array_size)
348 value_array_size += 25;
349 value_array = xrealloc(value_array,
350 sizeof(*value_array) * value_array_size);
353 if (endptr == NULL || *endptr != '\0')
355 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
356 SpecName, Line, token);
357 return -1;
361 if (token == NULL)
363 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
364 SpecName, Line);
365 return -1;
368 odp->u.var.n_values = n_values;
369 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
371 return 0;
375 /*******************************************************************
376 * ParseExportFunction
378 * Parse a function definition.
380 static int ParseExportFunction( ORDDEF *odp )
382 char *token;
383 int i;
385 switch(SpecType)
387 case SPEC_WIN16:
388 if (odp->type == TYPE_STDCALL)
390 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
391 SpecName, Line );
392 return -1;
394 else if (odp->type == TYPE_VARARGS)
396 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
397 SpecName, Line );
398 return -1;
400 break;
401 case SPEC_WIN32:
402 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
404 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
405 SpecName, Line );
406 return -1;
408 break;
409 default:
410 break;
413 token = GetToken();
414 if (*token != '(')
416 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
417 SpecName, Line, token);
418 return -1;
421 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
423 token = GetToken();
424 if (*token == ')')
425 break;
427 if (!strcmp(token, "word"))
428 odp->u.func.arg_types[i] = 'w';
429 else if (!strcmp(token, "s_word"))
430 odp->u.func.arg_types[i] = 's';
431 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
432 odp->u.func.arg_types[i] = 'l';
433 else if (!strcmp(token, "ptr"))
434 odp->u.func.arg_types[i] = 'p';
435 else if (!strcmp(token, "str"))
436 odp->u.func.arg_types[i] = 't';
437 else if (!strcmp(token, "wstr"))
438 odp->u.func.arg_types[i] = 'W';
439 else if (!strcmp(token, "segstr"))
440 odp->u.func.arg_types[i] = 'T';
441 else if (!strcmp(token, "double"))
443 odp->u.func.arg_types[i++] = 'l';
444 odp->u.func.arg_types[i] = 'l';
446 else
448 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
449 SpecName, Line, token);
450 return -1;
452 if (SpecType == SPEC_WIN32)
454 if (strcmp(token, "long") &&
455 strcmp(token, "ptr") &&
456 strcmp(token, "str") &&
457 strcmp(token, "wstr") &&
458 strcmp(token, "double"))
460 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
461 SpecName, Line, token );
462 return -1;
466 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
468 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
469 return -1;
471 odp->u.func.arg_types[i] = '\0';
472 if ((odp->type == TYPE_STDCALL) && !i)
473 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
474 strcpy(odp->u.func.link_name, GetToken());
475 return 0;
479 /*******************************************************************
480 * ParseEquate
482 * Parse an 'equate' definition.
484 static int ParseEquate( ORDDEF *odp )
486 char *endptr;
488 char *token = GetToken();
489 int value = strtol(token, &endptr, 0);
490 if (endptr == NULL || *endptr != '\0')
492 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
493 SpecName, Line, token);
494 return -1;
497 if (SpecType == SPEC_WIN32)
499 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
500 SpecName, Line );
501 return -1;
504 odp->u.abs.value = value;
505 return 0;
509 /*******************************************************************
510 * ParseStub
512 * Parse a 'stub' definition.
514 static int ParseStub( ORDDEF *odp )
516 odp->u.func.arg_types[0] = '\0';
517 strcpy( odp->u.func.link_name, STUB_CALLBACK );
518 return 0;
522 /*******************************************************************
523 * ParseInterrupt
525 * Parse an 'interrupt' definition.
527 static int ParseInterrupt( ORDDEF *odp )
529 char *token;
531 if (SpecType == SPEC_WIN32)
533 fprintf( stderr, "%s:%d: 'interrupt' not supported for Win32\n",
534 SpecName, Line );
535 return -1;
538 token = GetToken();
539 if (*token != '(')
541 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
542 SpecName, Line, token);
543 return -1;
545 token = GetToken();
546 if (*token != ')')
548 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
549 SpecName, Line, token);
550 return -1;
553 odp->u.func.arg_types[0] = '\0';
554 strcpy( odp->u.func.link_name, GetToken() );
555 return 0;
559 /*******************************************************************
560 * ParseExtern
562 * Parse an 'extern' definition.
564 static int ParseExtern( ORDDEF *odp )
566 if (SpecType == SPEC_WIN16)
568 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
569 SpecName, Line );
570 return -1;
572 strcpy( odp->u.ext.link_name, GetToken() );
573 return 0;
577 /*******************************************************************
578 * ParseForward
580 * Parse a 'forward' definition.
582 static int ParseForward( ORDDEF *odp )
584 if (SpecType == SPEC_WIN16)
586 fprintf( stderr, "%s:%d: 'forward' not supported for Win16\n",
587 SpecName, Line );
588 return -1;
590 strcpy( odp->u.fwd.link_name, GetToken() );
591 return 0;
595 /*******************************************************************
596 * ParseOrdinal
598 * Parse an ordinal definition.
600 static int ParseOrdinal(int ordinal)
602 ORDDEF *odp;
603 char *token;
605 if (ordinal >= MAX_ORDINALS)
607 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
608 return -1;
610 if (ordinal > Limit) Limit = ordinal;
611 if (ordinal < Base) Base = ordinal;
613 odp = &OrdinalDefinitions[ordinal];
614 if (!(token = GetToken()))
616 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
617 return -1;
620 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
621 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
622 break;
624 if (odp->type >= TYPE_NBTYPES)
626 fprintf( stderr,
627 "%s:%d: Expected type after ordinal, found '%s' instead\n",
628 SpecName, Line, token );
629 return -1;
632 if (!(token = GetToken()))
634 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
635 return -1;
637 strcpy( odp->name, token );
638 odp->lineno = Line;
640 switch(odp->type)
642 case TYPE_BYTE:
643 case TYPE_WORD:
644 case TYPE_LONG:
645 return ParseVariable( odp );
646 case TYPE_PASCAL_16:
647 case TYPE_PASCAL:
648 case TYPE_REGISTER:
649 case TYPE_STDCALL:
650 case TYPE_VARARGS:
651 case TYPE_CDECL:
652 return ParseExportFunction( odp );
653 case TYPE_INTERRUPT:
654 return ParseInterrupt( odp );
655 case TYPE_ABS:
656 return ParseEquate( odp );
657 case TYPE_STUB:
658 return ParseStub( odp );
659 case TYPE_EXTERN:
660 return ParseExtern( odp );
661 case TYPE_FORWARD:
662 return ParseForward( odp );
663 default:
664 fprintf( stderr, "Should not happen\n" );
665 return -1;
670 /*******************************************************************
671 * ParseTopLevel
673 * Parse a spec file.
675 static int ParseTopLevel(void)
677 char *token;
679 while ((token = GetToken()) != NULL)
681 if (strcmp(token, "name") == 0)
683 strcpy(DLLName, GetToken());
684 strupper(DLLName);
685 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
687 else if (strcmp(token, "file") == 0)
689 strcpy(DLLFileName, GetToken());
690 strupper(DLLFileName);
692 else if (strcmp(token, "type") == 0)
694 token = GetToken();
695 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
696 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
697 else
699 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
700 SpecName, Line);
701 return -1;
704 else if (strcmp(token, "heap") == 0)
706 token = GetToken();
707 if (!IsNumberString(token))
709 fprintf(stderr, "%s:%d: Expected number after heap\n",
710 SpecName, Line);
711 return -1;
713 DLLHeapSize = atoi(token);
715 else if (strcmp(token, "init") == 0)
717 strcpy(DLLInitFunc, GetToken());
718 if (SpecType == SPEC_WIN16)
720 fprintf(stderr, "%s:%d: init cannot be used for Win16 spec files\n",
721 SpecName, Line);
722 return -1;
724 if (!DLLInitFunc[0])
726 fprintf(stderr, "%s:%d: Expected function name after init\n", SpecName, Line);
727 return -1;
730 else if (strcmp(token, "import") == 0)
732 if (nb_imports >= MAX_IMPORTS)
734 fprintf( stderr, "%s:%d: Too many imports (limit %d)\n",
735 SpecName, Line, MAX_IMPORTS );
736 return -1;
738 if (SpecType != SPEC_WIN32)
740 fprintf( stderr, "%s:%d: Imports not supported for Win16\n", SpecName, Line );
741 return -1;
743 DLLImports[nb_imports++] = xstrdup(GetToken());
745 else if (IsNumberString(token))
747 int ordinal;
748 int rv;
750 ordinal = atoi(token);
751 if ((rv = ParseOrdinal(ordinal)) < 0)
752 return rv;
754 else
756 fprintf(stderr,
757 "%s:%d: Expected name, id, length or ordinal\n",
758 SpecName, Line);
759 return -1;
763 return 0;
767 /*******************************************************************
768 * StoreVariableCode
770 * Store a list of ints into a byte array.
772 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
774 int i;
776 switch(size)
778 case 1:
779 for (i = 0; i < odp->u.var.n_values; i++)
780 buffer[i] = odp->u.var.values[i];
781 break;
782 case 2:
783 for (i = 0; i < odp->u.var.n_values; i++)
784 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
785 break;
786 case 4:
787 for (i = 0; i < odp->u.var.n_values; i++)
788 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
789 break;
791 return odp->u.var.n_values * size;
795 /*******************************************************************
796 * DumpBytes
798 * Dump a byte stream into the assembly code.
800 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
801 const char *label )
803 int i;
805 fprintf( outfile, "\nstatic BYTE %s[] = \n{", label );
807 for (i = 0; i < len; i++)
809 if (!(i & 0x0f)) fprintf( outfile, "\n " );
810 fprintf( outfile, "%d", *data++ );
811 if (i < len - 1) fprintf( outfile, ", " );
813 fprintf( outfile, "\n};\n" );
817 /*******************************************************************
818 * BuildModule16
820 * Build the in-memory representation of a 16-bit NE module, and dump it
821 * as a byte stream into the assembly code.
823 static int BuildModule16( FILE *outfile, int max_code_offset,
824 int max_data_offset )
826 ORDDEF *odp;
827 int i;
828 char *buffer;
829 NE_MODULE *pModule;
830 SEGTABLEENTRY *pSegment;
831 OFSTRUCT *pFileInfo;
832 BYTE *pstr;
833 WORD *pword;
834 ET_BUNDLE *bundle = 0;
835 ET_ENTRY *entry = 0;
837 /* Module layout:
838 * NE_MODULE Module
839 * OFSTRUCT File information
840 * SEGTABLEENTRY Segment 1 (code)
841 * SEGTABLEENTRY Segment 2 (data)
842 * WORD[2] Resource table (empty)
843 * BYTE[2] Imported names (empty)
844 * BYTE[n] Resident names table
845 * BYTE[n] Entry table
848 buffer = xmalloc( 0x10000 );
850 pModule = (NE_MODULE *)buffer;
851 memset( pModule, 0, sizeof(*pModule) );
852 pModule->magic = IMAGE_OS2_SIGNATURE;
853 pModule->count = 1;
854 pModule->next = 0;
855 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
856 pModule->dgroup = 2;
857 pModule->heap_size = DLLHeapSize;
858 pModule->stack_size = 0;
859 pModule->ip = 0;
860 pModule->cs = 0;
861 pModule->sp = 0;
862 pModule->ss = 0;
863 pModule->seg_count = 2;
864 pModule->modref_count = 0;
865 pModule->nrname_size = 0;
866 pModule->modref_table = 0;
867 pModule->nrname_fpos = 0;
868 pModule->moveable_entries = 0;
869 pModule->alignment = 0;
870 pModule->truetype = 0;
871 pModule->os_flags = NE_OSFLAGS_WINDOWS;
872 pModule->misc_flags = 0;
873 pModule->dlls_to_init = 0;
874 pModule->nrname_handle = 0;
875 pModule->min_swap_area = 0;
876 pModule->expected_version = 0x030a;
877 pModule->module32 = 0;
878 pModule->self = 0;
879 pModule->self_loading_sel = 0;
881 /* File information */
883 pFileInfo = (OFSTRUCT *)(pModule + 1);
884 pModule->fileinfo = (int)pFileInfo - (int)pModule;
885 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
886 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
887 + strlen(DLLFileName);
888 strcpy( pFileInfo->szPathName, DLLFileName );
889 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
891 #ifdef __i386__ /* FIXME: Alignment problems! */
893 /* Segment table */
895 pSegment = (SEGTABLEENTRY *)pstr;
896 pModule->seg_table = (int)pSegment - (int)pModule;
897 pSegment->filepos = 0;
898 pSegment->size = max_code_offset;
899 pSegment->flags = 0;
900 pSegment->minsize = max_code_offset;
901 pSegment->hSeg = 0;
902 pSegment++;
904 pModule->dgroup_entry = (int)pSegment - (int)pModule;
905 pSegment->filepos = 0;
906 pSegment->size = max_data_offset;
907 pSegment->flags = NE_SEGFLAGS_DATA;
908 pSegment->minsize = max_data_offset;
909 pSegment->hSeg = 0;
910 pSegment++;
912 /* Resource table */
914 pword = (WORD *)pSegment;
915 pModule->res_table = (int)pword - (int)pModule;
916 *pword++ = 0;
917 *pword++ = 0;
919 /* Imported names table */
921 pstr = (char *)pword;
922 pModule->import_table = (int)pstr - (int)pModule;
923 *pstr++ = 0;
924 *pstr++ = 0;
926 /* Resident names table */
928 pModule->name_table = (int)pstr - (int)pModule;
929 /* First entry is module name */
930 *pstr = strlen(DLLName );
931 strcpy( pstr + 1, DLLName );
932 pstr += *pstr + 1;
933 *(WORD *)pstr = 0;
934 pstr += sizeof(WORD);
935 /* Store all ordinals */
936 odp = OrdinalDefinitions + 1;
937 for (i = 1; i <= Limit; i++, odp++)
939 if (!odp->name[0]) continue;
940 *pstr = strlen( odp->name );
941 strcpy( pstr + 1, odp->name );
942 strupper( pstr + 1 );
943 pstr += *pstr + 1;
944 *(WORD *)pstr = i;
945 pstr += sizeof(WORD);
947 *pstr++ = 0;
949 /* Entry table */
951 pModule->entry_table = (int)pstr - (int)pModule;
952 odp = OrdinalDefinitions + 1;
953 for (i = 1; i <= Limit; i++, odp++)
955 int selector = 0;
957 switch (odp->type)
959 case TYPE_CDECL:
960 case TYPE_PASCAL:
961 case TYPE_PASCAL_16:
962 case TYPE_REGISTER:
963 case TYPE_INTERRUPT:
964 case TYPE_STUB:
965 selector = 1; /* Code selector */
966 break;
968 case TYPE_BYTE:
969 case TYPE_WORD:
970 case TYPE_LONG:
971 selector = 2; /* Data selector */
972 break;
974 case TYPE_ABS:
975 selector = 0xfe; /* Constant selector */
976 break;
978 default:
979 selector = 0; /* Invalid selector */
980 break;
983 if ( !selector )
984 continue;
986 if ( bundle && bundle->last+1 == i )
987 bundle->last++;
988 else
990 if ( bundle )
991 bundle->next = (char *)pstr - (char *)pModule;
993 bundle = (ET_BUNDLE *)pstr;
994 bundle->first = i-1;
995 bundle->last = i;
996 bundle->next = 0;
997 pstr += sizeof(ET_BUNDLE);
1000 /* FIXME: is this really correct ?? */
1001 entry = (ET_ENTRY *)pstr;
1002 entry->type = 0xff; /* movable */
1003 entry->flags = 3; /* exported & public data */
1004 entry->segnum = selector;
1005 entry->offs = odp->offset;
1006 pstr += sizeof(ET_ENTRY);
1008 *pstr++ = 0;
1009 #endif
1011 /* Dump the module content */
1013 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
1014 "Module" );
1015 return (int)pstr - (int)pModule;
1019 /*******************************************************************
1020 * BuildSpec32File
1022 * Build a Win32 C file from a spec file.
1024 static int BuildSpec32File( char * specfile, FILE *outfile )
1026 ORDDEF *odp;
1027 int i, nb_names, fwd_size = 0;
1029 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1030 specfile );
1031 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
1033 /* Output code for all stubs functions */
1035 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
1036 DLLName );
1037 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1039 if (odp->type != TYPE_STUB) continue;
1040 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
1041 i, DLLName, i );
1044 /* Output code for all register functions */
1046 fprintf( outfile, "#ifdef __i386__\n" );
1047 fprintf( outfile, "#ifndef __GNUC__\n" );
1048 fprintf( outfile, "static void __asm__dummy() {\n" );
1049 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1050 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1052 if (odp->type != TYPE_REGISTER) continue;
1053 fprintf( outfile,
1054 "__asm__(\".align 4\\n\\t\"\n"
1055 " \".globl " PREFIX "%s\\n\\t\"\n"
1056 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1057 " \"" PREFIX "%s:\\n\\t\"\n"
1058 " \"call " PREFIX "CALL32_Regs\\n\\t\"\n"
1059 " \".long " PREFIX "__regs_%s\\n\\t\"\n"
1060 " \".byte %d,%d\");\n",
1061 odp->u.func.link_name, odp->u.func.link_name,
1062 odp->u.func.link_name, odp->u.func.link_name,
1063 4 * strlen(odp->u.func.arg_types),
1064 4 * strlen(odp->u.func.arg_types) );
1066 fprintf( outfile, "#ifndef __GNUC__\n" );
1067 fprintf( outfile, "}\n" );
1068 fprintf( outfile, "#endif /* !defined(__GNUC__) */\n" );
1069 fprintf( outfile, "#endif /* defined(__i386__) */\n" );
1071 /* Output the DLL functions prototypes */
1073 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1075 switch(odp->type)
1077 case TYPE_EXTERN:
1078 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1079 break;
1080 case TYPE_REGISTER:
1081 case TYPE_STDCALL:
1082 case TYPE_VARARGS:
1083 case TYPE_CDECL:
1084 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1085 break;
1086 case TYPE_FORWARD:
1087 fwd_size += strlen(odp->u.fwd.link_name) + 1;
1088 break;
1089 case TYPE_INVALID:
1090 case TYPE_STUB:
1091 break;
1092 default:
1093 fprintf(stderr,"build: function type %d not available for Win32\n",
1094 odp->type);
1095 return -1;
1099 /* Output LibMain function */
1100 if (DLLInitFunc[0]) fprintf( outfile, "extern void %s();\n", DLLInitFunc );
1103 /* Output the DLL functions table */
1105 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1106 Limit - Base + 1 );
1107 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1109 switch(odp->type)
1111 case TYPE_INVALID:
1112 fprintf( outfile, " 0" );
1113 break;
1114 case TYPE_EXTERN:
1115 fprintf( outfile, " %s", odp->u.ext.link_name );
1116 break;
1117 case TYPE_REGISTER:
1118 case TYPE_STDCALL:
1119 case TYPE_VARARGS:
1120 case TYPE_CDECL:
1121 fprintf( outfile, " %s", odp->u.func.link_name);
1122 break;
1123 case TYPE_STUB:
1124 fprintf( outfile, " __stub_%d", i );
1125 break;
1126 case TYPE_FORWARD:
1127 fprintf( outfile, " (ENTRYPOINT32)\"%s\"", odp->u.fwd.link_name );
1128 break;
1129 default:
1130 return -1;
1132 if (i < Limit) fprintf( outfile, ",\n" );
1134 fprintf( outfile, "\n};\n\n" );
1136 /* Output the DLL names table */
1138 nb_names = 0;
1139 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1140 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1142 if (odp->type == TYPE_INVALID) continue;
1143 if (nb_names++) fprintf( outfile, ",\n" );
1144 fprintf( outfile, " \"%s\"", odp->name );
1146 fprintf( outfile, "\n};\n\n" );
1148 /* Output the DLL argument types */
1150 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1151 Limit - Base + 1 );
1152 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1154 unsigned int j, mask = 0;
1155 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL) ||
1156 (odp->type == TYPE_REGISTER))
1157 for (j = 0; odp->u.func.arg_types[j]; j++)
1159 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1160 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1162 fprintf( outfile, " %d", mask );
1163 if (i < Limit) fprintf( outfile, ",\n" );
1165 fprintf( outfile, "\n};\n\n" );
1167 /* Output the DLL ordinals table */
1169 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1170 nb_names = 0;
1171 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1173 if (odp->type == TYPE_INVALID) continue;
1174 if (nb_names++) fprintf( outfile, ",\n" );
1175 fprintf( outfile, " %d", i - Base );
1177 fprintf( outfile, "\n};\n\n" );
1179 /* Output the DLL functions arguments */
1181 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1182 Limit - Base + 1 );
1183 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1185 unsigned char args;
1186 switch(odp->type)
1188 case TYPE_STDCALL:
1189 args = (unsigned char)strlen(odp->u.func.arg_types);
1190 break;
1191 case TYPE_CDECL:
1192 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1193 break;
1194 case TYPE_REGISTER:
1195 args = 0x40 | (unsigned char)strlen(odp->u.func.arg_types);
1196 break;
1197 case TYPE_FORWARD:
1198 args = 0xfd;
1199 break;
1200 default:
1201 args = 0xff;
1202 break;
1204 fprintf( outfile, " 0x%02x", args );
1205 if (i < Limit) fprintf( outfile, ",\n" );
1207 fprintf( outfile, "\n};\n\n" );
1209 /* Output the DLL imports */
1211 if (nb_imports)
1213 fprintf( outfile, "static const char * const Imports[%d] =\n{\n", nb_imports );
1214 for (i = 0; i < nb_imports; i++)
1216 fprintf( outfile, " \"%s\"", DLLImports[i] );
1217 if (i < nb_imports-1) fprintf( outfile, ",\n" );
1219 fprintf( outfile, "\n};\n\n" );
1222 /* Output the DLL descriptor */
1224 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1225 DLLName );
1226 fprintf( outfile, " \"%s\",\n", DLLName );
1227 fprintf( outfile, " \"%s\",\n", DLLFileName );
1228 fprintf( outfile, " %d,\n", Base );
1229 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1230 fprintf( outfile, " %d,\n", nb_names );
1231 fprintf( outfile, " %d,\n", nb_imports );
1232 fprintf( outfile, " %d,\n", (fwd_size + 3) & ~3 );
1233 fprintf( outfile,
1234 " Functions,\n"
1235 " FuncNames,\n"
1236 " FuncOrdinals,\n"
1237 " FuncArgs,\n"
1238 " ArgTypes,\n");
1239 fprintf( outfile, " %s,\n", nb_imports ? "Imports" : "0" );
1240 fprintf( outfile, " %s\n", DLLInitFunc[0] ? DLLInitFunc : "0" );
1241 fprintf( outfile, "};\n" );
1242 return 0;
1245 /*******************************************************************
1246 * Spec16TypeCompare
1248 static int Spec16TypeCompare( const void *e1, const void *e2 )
1250 const ORDDEF *odp1 = *(const ORDDEF **)e1;
1251 const ORDDEF *odp2 = *(const ORDDEF **)e2;
1253 int type1 = (odp1->type == TYPE_CDECL) ? 0
1254 : (odp1->type == TYPE_REGISTER) ? 3
1255 : (odp1->type == TYPE_INTERRUPT) ? 4
1256 : (odp1->type == TYPE_PASCAL_16) ? 1 : 2;
1258 int type2 = (odp2->type == TYPE_CDECL) ? 0
1259 : (odp2->type == TYPE_REGISTER) ? 3
1260 : (odp2->type == TYPE_INTERRUPT) ? 4
1261 : (odp2->type == TYPE_PASCAL_16) ? 1 : 2;
1263 int retval = type1 - type2;
1264 if ( !retval )
1265 retval = strcmp( odp1->u.func.arg_types, odp2->u.func.arg_types );
1267 return retval;
1270 /*******************************************************************
1271 * BuildSpec16File
1273 * Build a Win16 assembly file from a spec file.
1275 static int BuildSpec16File( char * specfile, FILE *outfile )
1277 ORDDEF *odp, **type, **typelist;
1278 int i, nFuncs, nTypes;
1279 int code_offset, data_offset, module_size;
1280 unsigned char *data;
1282 /* File header */
1284 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
1285 specfile );
1286 fprintf( outfile, "#define __FLATCS__ 0x%04x\n", Code_Selector );
1287 fprintf( outfile, "#include \"builtin16.h\"\n\n" );
1289 data = (unsigned char *)xmalloc( 0x10000 );
1290 memset( data, 0, 16 );
1291 data_offset = 16;
1294 /* Build sorted list of all argument types, without duplicates */
1296 typelist = (ORDDEF **)calloc( Limit+1, sizeof(ORDDEF *) );
1298 odp = OrdinalDefinitions;
1299 for (i = nFuncs = 0; i <= Limit; i++, odp++)
1301 switch (odp->type)
1303 case TYPE_REGISTER:
1304 case TYPE_INTERRUPT:
1305 case TYPE_CDECL:
1306 case TYPE_PASCAL:
1307 case TYPE_PASCAL_16:
1308 case TYPE_STUB:
1309 typelist[nFuncs++] = odp;
1311 default:
1312 break;
1316 qsort( typelist, nFuncs, sizeof(ORDDEF *), Spec16TypeCompare );
1318 i = nTypes = 0;
1319 while ( i < nFuncs )
1321 typelist[nTypes++] = typelist[i++];
1322 while ( i < nFuncs && Spec16TypeCompare( typelist + i, typelist + nTypes-1 ) == 0 )
1323 i++;
1326 /* Output CallFrom16 routines needed by this .spec file */
1328 for ( i = 0; i < nTypes; i++ )
1330 char profile[101];
1332 sprintf( profile, "%s_%s_%s",
1333 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1334 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1335 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1336 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1337 typelist[i]->u.func.arg_types );
1339 BuildCallFrom16Func( outfile, profile, DLLName, TRUE );
1342 /* Output the DLL functions prototypes */
1344 odp = OrdinalDefinitions;
1345 for (i = 0; i <= Limit; i++, odp++)
1347 switch(odp->type)
1349 case TYPE_REGISTER:
1350 case TYPE_INTERRUPT:
1351 case TYPE_CDECL:
1352 case TYPE_PASCAL:
1353 case TYPE_PASCAL_16:
1354 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1355 break;
1356 default:
1357 break;
1361 /* Output code segment */
1363 fprintf( outfile, "\nstatic struct\n{\n CALLFROM16 call[%d];\n"
1364 " ENTRYPOINT16 entry[%d];\n} Code_Segment = \n{\n {\n",
1365 nTypes, nFuncs );
1366 code_offset = 0;
1368 for ( i = 0; i < nTypes; i++ )
1370 char profile[101], *arg;
1371 int argsize = 0;
1373 sprintf( profile, "%s_%s_%s",
1374 (typelist[i]->type == TYPE_CDECL) ? "c" : "p",
1375 (typelist[i]->type == TYPE_REGISTER) ? "regs" :
1376 (typelist[i]->type == TYPE_INTERRUPT) ? "intr" :
1377 (typelist[i]->type == TYPE_PASCAL_16) ? "word" : "long",
1378 typelist[i]->u.func.arg_types );
1380 if ( typelist[i]->type != TYPE_CDECL )
1381 for ( arg = typelist[i]->u.func.arg_types; *arg; arg++ )
1382 switch ( *arg )
1384 case 'w': /* word */
1385 case 's': /* s_word */
1386 argsize += 2;
1387 break;
1389 case 'l': /* long or segmented pointer */
1390 case 'T': /* segmented pointer to null-terminated string */
1391 case 'p': /* linear pointer */
1392 case 't': /* linear pointer to null-terminated string */
1393 argsize += 4;
1394 break;
1397 if ( typelist[i]->type == TYPE_INTERRUPT )
1398 argsize += 2;
1400 fprintf( outfile, " CF16_%s( %s_CallFrom16_%s, %d, \"%s\" ),\n",
1401 ( typelist[i]->type == TYPE_REGISTER
1402 || typelist[i]->type == TYPE_INTERRUPT)? "REGS":
1403 typelist[i]->type == TYPE_PASCAL_16? "WORD" : "LONG",
1404 DLLName, profile, argsize, profile );
1406 code_offset += sizeof(CALLFROM16);
1408 fprintf( outfile, " },\n {\n" );
1410 odp = OrdinalDefinitions;
1411 for (i = 0; i <= Limit; i++, odp++)
1413 switch (odp->type)
1415 case TYPE_INVALID:
1416 odp->offset = 0xffff;
1417 break;
1419 case TYPE_ABS:
1420 odp->offset = LOWORD(odp->u.abs.value);
1421 break;
1423 case TYPE_BYTE:
1424 odp->offset = data_offset;
1425 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1426 break;
1428 case TYPE_WORD:
1429 odp->offset = data_offset;
1430 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1431 break;
1433 case TYPE_LONG:
1434 odp->offset = data_offset;
1435 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1436 break;
1438 case TYPE_REGISTER:
1439 case TYPE_INTERRUPT:
1440 case TYPE_CDECL:
1441 case TYPE_PASCAL:
1442 case TYPE_PASCAL_16:
1443 case TYPE_STUB:
1444 type = bsearch( &odp, typelist, nTypes, sizeof(ORDDEF *), Spec16TypeCompare );
1445 assert( type );
1447 fprintf( outfile, " /* %s.%d */ ", DLLName, i );
1448 fprintf( outfile, "EP( %s, %d /* %s_%s_%s */ ),\n",
1449 odp->u.func.link_name,
1450 (type-typelist)*sizeof(CALLFROM16) -
1451 (code_offset + sizeof(ENTRYPOINT16)),
1452 (odp->type == TYPE_CDECL) ? "c" : "p",
1453 (odp->type == TYPE_REGISTER) ? "regs" :
1454 (odp->type == TYPE_INTERRUPT) ? "intr" :
1455 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1456 odp->u.func.arg_types );
1458 odp->offset = code_offset;
1459 code_offset += sizeof(ENTRYPOINT16);
1460 break;
1462 default:
1463 fprintf(stderr,"build: function type %d not available for Win16\n",
1464 odp->type);
1465 return -1;
1469 fprintf( outfile, " }\n};\n" );
1471 /* Output data segment */
1473 DumpBytes( outfile, data, data_offset, "Data_Segment" );
1475 /* Build the module */
1477 module_size = BuildModule16( outfile, code_offset, data_offset );
1479 /* Output the DLL descriptor */
1481 fprintf( outfile, "\nWIN16_DESCRIPTOR %s_Descriptor = \n{\n", DLLName );
1482 fprintf( outfile, " \"%s\",\n", DLLName );
1483 fprintf( outfile, " Module,\n" );
1484 fprintf( outfile, " sizeof(Module),\n" );
1485 fprintf( outfile, " (BYTE *)&Code_Segment,\n" );
1486 fprintf( outfile, " (BYTE *)Data_Segment\n" );
1487 fprintf( outfile, "};\n" );
1489 return 0;
1493 /*******************************************************************
1494 * BuildSpecFile
1496 * Build an assembly file from a spec file.
1498 static int BuildSpecFile( FILE *outfile, char *specname )
1500 SpecName = specname;
1501 SpecFp = fopen( specname, "r");
1502 if (SpecFp == NULL)
1504 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1505 return -1;
1508 if (ParseTopLevel() < 0) return -1;
1510 switch(SpecType)
1512 case SPEC_WIN16:
1513 return BuildSpec16File( specname, outfile );
1514 case SPEC_WIN32:
1515 return BuildSpec32File( specname, outfile );
1516 default:
1517 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1518 return -1;
1523 /*******************************************************************
1524 * BuildCallFrom16Func
1526 * Build a 16-bit-to-Wine callback glue function.
1528 * The generated routines are intended to be used as argument conversion
1529 * routines to be called by the CallFrom16... core. Thus, the prototypes of
1530 * the generated routines are (see also CallFrom16):
1532 * extern WORD WINAPI PREFIX_CallFrom16_C_word_xxx( FARPROC func, LPBYTE args );
1533 * extern LONG WINAPI PREFIX_CallFrom16_C_long_xxx( FARPROC func, LPBYTE args );
1534 * extern void WINAPI PREFIX_CallFrom16_C_regs_xxx( FARPROC func, LPBYTE args,
1535 * CONTEXT86 *context );
1536 * extern void WINAPI PREFIX_CallFrom16_C_intr_xxx( FARPROC func, LPBYTE args,
1537 * CONTEXT86 *context );
1539 * where 'C' is the calling convention ('p' for pascal or 'c' for cdecl),
1540 * and each 'x' is an argument ('w'=word, 's'=signed word, 'l'=long,
1541 * 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1542 * 'T'=segmented pointer to null-terminated string).
1544 * The generated routines fetch the arguments from the 16-bit stack (pointed
1545 * to by 'args'); the offsets of the single argument values are computed
1546 * according to the calling convention and the argument types. Then, the
1547 * 32-bit entry point is called with these arguments.
1549 * For register functions, the arguments (if present) are converted just
1550 * the same as for normal functions, but in addition the CONTEXT86 pointer
1551 * filled with the current register values is passed to the 32-bit routine.
1552 * (An 'intr' interrupt handler routine is treated exactly like a register
1553 * routine, except that upon return, the flags word pushed onto the stack
1554 * by the interrupt is removed by the 16-bit call stub.)
1557 static void BuildCallFrom16Func( FILE *outfile, char *profile, char *prefix, int local )
1559 int i, pos, argsize = 0;
1560 int short_ret = 0;
1561 int reg_func = 0;
1562 int usecdecl = 0;
1563 char *args = profile + 7;
1564 char *ret_type;
1566 /* Parse function type */
1568 if (!strncmp( "c_", profile, 2 )) usecdecl = 1;
1569 else if (strncmp( "p_", profile, 2 ))
1571 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1572 return;
1575 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1576 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1577 else if (!strncmp( "intr_", profile + 2, 5 )) reg_func = 2;
1578 else if (strncmp( "long_", profile + 2, 5 ))
1580 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1581 return;
1584 for ( i = 0; args[i]; i++ )
1585 switch ( args[i] )
1587 case 'w': /* word */
1588 case 's': /* s_word */
1589 argsize += 2;
1590 break;
1592 case 'l': /* long or segmented pointer */
1593 case 'T': /* segmented pointer to null-terminated string */
1594 case 'p': /* linear pointer */
1595 case 't': /* linear pointer to null-terminated string */
1596 argsize += 4;
1597 break;
1600 ret_type = reg_func? "void" : short_ret? "WORD" : "LONG";
1602 fprintf( outfile, "typedef %s WINAPI (*proc_%s_t)( ",
1603 ret_type, profile );
1604 args = profile + 7;
1605 for ( i = 0; args[i]; i++ )
1607 if ( i ) fprintf( outfile, ", " );
1608 switch (args[i])
1610 case 'w': fprintf( outfile, "WORD" ); break;
1611 case 's': fprintf( outfile, "INT16" ); break;
1612 case 'l': case 'T': fprintf( outfile, "LONG" ); break;
1613 case 'p': case 't': fprintf( outfile, "LPVOID" ); break;
1616 if ( reg_func )
1617 fprintf( outfile, "%sstruct _CONTEXT86 *", i? ", " : "" );
1618 else if ( !i )
1619 fprintf( outfile, "void" );
1620 fprintf( outfile, " );\n" );
1622 fprintf( outfile, "%s%s WINAPI %s_CallFrom16_%s( FARPROC proc, LPBYTE args%s )\n{\n",
1623 local? "static " : "", ret_type, prefix, profile,
1624 reg_func? ", struct _CONTEXT86 *context" : "" );
1626 fprintf( outfile, " %s((proc_%s_t) proc) (\n",
1627 reg_func? "" : "return ", profile );
1628 args = profile + 7;
1629 pos = !usecdecl? argsize : 0;
1630 for ( i = 0; args[i]; i++ )
1632 if ( i ) fprintf( outfile, ",\n" );
1633 fprintf( outfile, " " );
1634 switch (args[i])
1636 case 'w': /* word */
1637 if ( !usecdecl ) pos -= 2;
1638 fprintf( outfile, "*(WORD *)(args+%d)", pos );
1639 if ( usecdecl ) pos += 2;
1640 break;
1642 case 's': /* s_word */
1643 if ( !usecdecl ) pos -= 2;
1644 fprintf( outfile, "*(INT16 *)(args+%d)", pos );
1645 if ( usecdecl ) pos += 2;
1646 break;
1648 case 'l': /* long or segmented pointer */
1649 case 'T': /* segmented pointer to null-terminated string */
1650 if ( !usecdecl ) pos -= 4;
1651 fprintf( outfile, "*(LONG *)(args+%d)", pos );
1652 if ( usecdecl ) pos += 4;
1653 break;
1655 case 'p': /* linear pointer */
1656 case 't': /* linear pointer to null-terminated string */
1657 if ( !usecdecl ) pos -= 4;
1658 fprintf( outfile, "PTR_SEG_TO_LIN( *(SEGPTR *)(args+%d) )", pos );
1659 if ( usecdecl ) pos += 4;
1660 break;
1662 default:
1663 fprintf( stderr, "Unknown arg type '%c'\n", args[i] );
1666 if ( reg_func )
1667 fprintf( outfile, "%s context", i? ",\n" : "" );
1668 fprintf( outfile, " );\n}\n\n" );
1671 /*******************************************************************
1672 * BuildCallTo16Func
1674 * Build a Wine-to-16-bit callback glue function.
1676 * Prototypes for the CallTo16 functions:
1677 * extern WORD CALLBACK PREFIX_CallTo16_word_xxx( FARPROC16 func, args... );
1678 * extern LONG CALLBACK PREFIX_CallTo16_long_xxx( FARPROC16 func, args... );
1680 * These routines are provided solely for convenience; they simply
1681 * write the arguments onto the 16-bit stack, and call the appropriate
1682 * CallTo16... core routine.
1684 * If you have more sophisticated argument conversion requirements than
1685 * are provided by these routines, you might as well call the core
1686 * routines by yourself.
1689 static void BuildCallTo16Func( FILE *outfile, char *profile, char *prefix )
1691 char *args = profile + 5;
1692 int i, argsize = 0, short_ret = 0;
1694 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1695 else if (strncmp( "long_", profile, 5 ))
1697 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1698 exit(1);
1701 fprintf( outfile, "%s %s_CallTo16_%s( FARPROC16 proc",
1702 short_ret? "WORD" : "LONG", prefix, profile );
1703 args = profile + 5;
1704 for ( i = 0; args[i]; i++ )
1706 fprintf( outfile, ", " );
1707 switch (args[i])
1709 case 'w': fprintf( outfile, "WORD" ); argsize += 2; break;
1710 case 'l': fprintf( outfile, "LONG" ); argsize += 4; break;
1712 fprintf( outfile, " arg%d", i+1 );
1714 fprintf( outfile, " )\n{\n" );
1716 if ( argsize > 0 )
1717 fprintf( outfile, " LPBYTE args = (LPBYTE)CURRENT_STACK16;\n" );
1719 args = profile + 5;
1720 for ( i = 0; args[i]; i++ )
1722 switch (args[i])
1724 case 'w': fprintf( outfile, " args -= sizeof(WORD); *(WORD" ); break;
1725 case 'l': fprintf( outfile, " args -= sizeof(LONG); *(LONG" ); break;
1726 default: fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1727 args[i] );
1729 fprintf( outfile, " *)args = arg%d;\n", i+1 );
1732 fprintf( outfile, " return CallTo16%s( proc, %d );\n}\n\n",
1733 short_ret? "Word" : "Long", argsize );
1738 /*******************************************************************
1739 * BuildCallFrom16Core
1741 * This routine builds the core routines used in 16->32 thunks:
1742 * CallFrom16Word, CallFrom16Long, CallFrom16Register, and CallFrom16Thunk.
1744 * These routines are intended to be called via a far call (with 32-bit
1745 * operand size) from 16-bit code. The 16-bit code stub must push %bp,
1746 * the 32-bit entry point to be called, and the argument conversion
1747 * routine to be used (see stack layout below).
1749 * The core routine completes the STACK16FRAME on the 16-bit stack and
1750 * switches to the 32-bit stack. Then, the argument conversion routine
1751 * is called; it gets passed the 32-bit entry point and a pointer to the
1752 * 16-bit arguments (on the 16-bit stack) as parameters. (You can either
1753 * use conversion routines automatically generated by BuildCallFrom16,
1754 * or write your own for special purposes.)
1756 * The conversion routine must call the 32-bit entry point, passing it
1757 * the converted arguments, and return its return value to the core.
1758 * After the conversion routine has returned, the core switches back
1759 * to the 16-bit stack, converts the return value to the DX:AX format
1760 * (CallFrom16Long), and returns to the 16-bit call stub. All parameters,
1761 * including %bp, are popped off the stack.
1763 * The 16-bit call stub now returns to the caller, popping the 16-bit
1764 * arguments if necessary (pascal calling convention).
1766 * In the case of a 'register' function, CallFrom16Register fills a
1767 * CONTEXT86 structure with the values all registers had at the point
1768 * the first instruction of the 16-bit call stub was about to be
1769 * executed. A pointer to this CONTEXT86 is passed as third parameter
1770 * to the argument conversion routine, which typically passes it on
1771 * to the called 32-bit entry point.
1773 * CallFrom16Thunk is a special variant used by the implementation of
1774 * the Win95 16->32 thunk functions C16ThkSL and C16ThkSL01 and is
1775 * implemented as follows:
1776 * On entry, the EBX register is set up to contain a flat pointer to the
1777 * 16-bit stack such that EBX+22 points to the first argument.
1778 * Then, the entry point is called, while EBP is set up to point
1779 * to the return address (on the 32-bit stack).
1780 * The called function returns with CX set to the number of bytes
1781 * to be popped of the caller's stack.
1783 * Stack layout upon entry to the core routine (STACK16FRAME):
1784 * ... ...
1785 * (sp+24) word first 16-bit arg
1786 * (sp+22) word cs
1787 * (sp+20) word ip
1788 * (sp+18) word bp
1789 * (sp+14) long 32-bit entry point (reused for Win16 mutex recursion count)
1790 * (sp+12) word ip of actual entry point (necessary for relay debugging)
1791 * (sp+8) long relay (argument conversion) function entry point
1792 * (sp+4) long cs of 16-bit entry point
1793 * (sp) long ip of 16-bit entry point
1795 * Added on the stack:
1796 * (sp-2) word saved gs
1797 * (sp-4) word saved fs
1798 * (sp-6) word saved es
1799 * (sp-8) word saved ds
1800 * (sp-12) long saved ebp
1801 * (sp-16) long saved ecx
1802 * (sp-20) long saved edx
1803 * (sp-24) long saved previous stack
1805 static void BuildCallFrom16Core( FILE *outfile, int reg_func, int thunk, int short_ret )
1807 char *name = thunk? "Thunk" : reg_func? "Register" : short_ret? "Word" : "Long";
1809 /* Function header */
1810 fprintf( outfile, "\n\t.align 4\n" );
1811 #ifdef USE_STABS
1812 fprintf( outfile, ".stabs \"CallFrom16%s:F1\",36,0,0," PREFIX "CallFrom16%s\n",
1813 name, name);
1814 #endif
1815 fprintf( outfile, "\t.globl " PREFIX "CallFrom16%s\n", name );
1816 fprintf( outfile, PREFIX "CallFrom16%s:\n", name );
1818 /* Create STACK16FRAME (except STACK32FRAME link) */
1819 fprintf( outfile, "\tpushw %%gs\n" );
1820 fprintf( outfile, "\tpushw %%fs\n" );
1821 fprintf( outfile, "\tpushw %%es\n" );
1822 fprintf( outfile, "\tpushw %%ds\n" );
1823 fprintf( outfile, "\tpushl %%ebp\n" );
1824 fprintf( outfile, "\tpushl %%ecx\n" );
1825 fprintf( outfile, "\tpushl %%edx\n" );
1827 /* Save original EFlags register */
1828 fprintf( outfile, "\tpushfl\n" );
1830 if ( UsePIC )
1832 /* Get Global Offset Table into %ecx */
1833 fprintf( outfile, "\tcall .LCallFrom16%s.getgot1\n", name );
1834 fprintf( outfile, ".LCallFrom16%s.getgot1:\n", name );
1835 fprintf( outfile, "\tpopl %%ecx\n" );
1836 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot1], %%ecx\n", name );
1839 /* Load 32-bit segment registers */
1840 fprintf( outfile, "\tmovw $0x%04x, %%dx\n", Data_Selector );
1841 #ifdef __svr4__
1842 fprintf( outfile, "\tdata16\n");
1843 #endif
1844 fprintf( outfile, "\tmovw %%dx, %%ds\n" );
1845 #ifdef __svr4__
1846 fprintf( outfile, "\tdata16\n");
1847 #endif
1848 fprintf( outfile, "\tmovw %%dx, %%es\n" );
1850 if ( UsePIC )
1852 fprintf( outfile, "\tmovl " PREFIX "SYSLEVEL_Win16CurrentTeb@GOT(%%ecx), %%edx\n" );
1853 fprintf( outfile, "\tmovw (%%edx), %%fs\n" );
1855 else
1856 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb, %%fs\n" );
1858 /* Get address of ldt_copy array into %ecx */
1859 if ( UsePIC )
1860 fprintf( outfile, "\tmovl " PREFIX "ldt_copy@GOT(%%ecx), %%ecx\n" );
1861 else
1862 fprintf( outfile, "\tmovl $" PREFIX "ldt_copy, %%ecx\n" );
1864 /* Translate STACK16FRAME base to flat offset in %edx */
1865 fprintf( outfile, "\tmovw %%ss, %%dx\n" );
1866 fprintf( outfile, "\tandl $0xfff8, %%edx\n" );
1867 fprintf( outfile, "\tmovl (%%ecx,%%edx), %%edx\n" );
1868 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
1869 fprintf( outfile, "\tleal (%%ebp,%%edx), %%edx\n" );
1871 /* Get saved flags into %ecx */
1872 fprintf( outfile, "\tpopl %%ecx\n" );
1874 /* Get the 32-bit stack pointer from the TEB and complete STACK16FRAME */
1875 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d), %%ebp\n", STACKOFFSET );
1876 fprintf( outfile, "\tpushl %%ebp\n" );
1878 /* Switch stacks */
1879 #ifdef __svr4__
1880 fprintf( outfile,"\tdata16\n");
1881 #endif
1882 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss, (%d)\n", STACKOFFSET + 2 );
1883 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp, (%d)\n", STACKOFFSET );
1884 fprintf( outfile, "\tpushl %%ds\n" );
1885 fprintf( outfile, "\tpopl %%ss\n" );
1886 fprintf( outfile, "\tmovl %%ebp, %%esp\n" );
1887 fprintf( outfile, "\taddl $%d, %%ebp\n", STRUCTOFFSET(STACK32FRAME, ebp) );
1890 /* At this point:
1891 STACK16FRAME is completely set up
1892 DS, ES, SS: flat data segment
1893 FS: current TEB
1894 ESP: points to last STACK32FRAME
1895 EBP: points to ebp member of last STACK32FRAME
1896 EDX: points to current STACK16FRAME
1897 ECX: contains saved flags
1898 all other registers: unchanged */
1900 /* Special case: C16ThkSL stub */
1901 if ( thunk )
1903 /* Set up registers as expected and call thunk */
1904 fprintf( outfile, "\tleal %d(%%edx), %%ebx\n", sizeof(STACK16FRAME)-22 );
1905 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1907 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(entry_point) );
1909 /* Switch stack back */
1910 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
1911 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
1912 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1914 /* Restore registers and return directly to caller */
1915 fprintf( outfile, "\taddl $8, %%esp\n" );
1916 fprintf( outfile, "\tpopl %%ebp\n" );
1917 fprintf( outfile, "\tpopw %%ds\n" );
1918 fprintf( outfile, "\tpopw %%es\n" );
1919 fprintf( outfile, "\tpopw %%fs\n" );
1920 fprintf( outfile, "\tpopw %%gs\n" );
1921 fprintf( outfile, "\taddl $20, %%esp\n" );
1923 fprintf( outfile, "\txorb %%ch, %%ch\n" );
1924 fprintf( outfile, "\tpopl %%ebx\n" );
1925 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1926 fprintf( outfile, "\tpush %%ebx\n" );
1928 fprintf( outfile, "\t.byte 0x66\n" );
1929 fprintf( outfile, "\tlret\n" );
1931 return;
1935 /* Build register CONTEXT */
1936 if ( reg_func )
1938 fprintf( outfile, "\tsubl $%d, %%esp\n", sizeof(CONTEXT86) );
1940 fprintf( outfile, "\tmovl %%ecx, %d(%%esp)\n", CONTEXTOFFSET(EFlags) );
1942 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eax) );
1943 fprintf( outfile, "\tmovl %%ebx, %d(%%esp)\n", CONTEXTOFFSET(Ebx) );
1944 fprintf( outfile, "\tmovl %%esi, %d(%%esp)\n", CONTEXTOFFSET(Esi) );
1945 fprintf( outfile, "\tmovl %%edi, %d(%%esp)\n", CONTEXTOFFSET(Edi) );
1947 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ebp) );
1948 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ebp) );
1949 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(ecx) );
1950 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Ecx) );
1951 fprintf( outfile, "\tmovl %d(%%edx), %%eax\n", STACK16OFFSET(edx) );
1952 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Edx) );
1954 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ds) );
1955 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegDs) );
1956 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(es) );
1957 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegEs) );
1958 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(fs) );
1959 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegFs) );
1960 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(gs) );
1961 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegGs) );
1963 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(cs) );
1964 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegCs) );
1965 fprintf( outfile, "\tmovzwl %d(%%edx), %%eax\n", STACK16OFFSET(ip) );
1966 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Eip) );
1968 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET+2 );
1969 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(SegSs) );
1970 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d), %%eax\n", STACKOFFSET );
1971 fprintf( outfile, "\taddl $%d, %%eax\n", STACK16OFFSET(ip) );
1972 fprintf( outfile, "\tmovl %%eax, %d(%%esp)\n", CONTEXTOFFSET(Esp) );
1973 #if 0
1974 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
1975 #endif
1977 /* Push address of CONTEXT86 structure -- popped by the relay routine */
1978 fprintf( outfile, "\tpushl %%esp\n" );
1982 /* Print debug info before call */
1983 if ( debugging )
1985 if ( UsePIC )
1987 fprintf( outfile, "\tpushl %%ebx\n" );
1989 /* Get Global Offset Table into %ebx (for PLT call) */
1990 fprintf( outfile, "\tcall .LCallFrom16%s.getgot2\n", name );
1991 fprintf( outfile, ".LCallFrom16%s.getgot2:\n", name );
1992 fprintf( outfile, "\tpopl %%ebx\n" );
1993 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot2], %%ebx\n", name );
1996 fprintf( outfile, "\tpushl %%edx\n" );
1997 if ( reg_func )
1998 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
1999 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2000 else
2001 fprintf( outfile, "\tpushl $0\n" );
2003 if ( UsePIC )
2004 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16@PLT\n ");
2005 else
2006 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n ");
2008 fprintf( outfile, "\tpopl %%edx\n" );
2009 fprintf( outfile, "\tpopl %%edx\n" );
2011 if ( UsePIC )
2012 fprintf( outfile, "\tpopl %%ebx\n" );
2015 /* Call relay routine (which will call the API entry point) */
2016 fprintf( outfile, "\tleal %d(%%edx), %%eax\n", sizeof(STACK16FRAME) );
2017 fprintf( outfile, "\tpushl %%eax\n" );
2018 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK16OFFSET(entry_point) );
2019 fprintf( outfile, "\tcall *%d(%%edx)\n", STACK16OFFSET(relay) );
2021 /* Print debug info after call */
2022 if ( debugging )
2024 if ( UsePIC )
2026 fprintf( outfile, "\tpushl %%ebx\n" );
2028 /* Get Global Offset Table into %ebx (for PLT call) */
2029 fprintf( outfile, "\tcall .LCallFrom16%s.getgot3\n", name );
2030 fprintf( outfile, ".LCallFrom16%s.getgot3:\n", name );
2031 fprintf( outfile, "\tpopl %%ebx\n" );
2032 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallFrom16%s.getgot3], %%ebx\n", name );
2035 fprintf( outfile, "\tpushl %%eax\n" );
2036 if ( reg_func )
2037 fprintf( outfile, "\tleal -%d(%%ebp), %%eax\n\tpushl %%eax\n",
2038 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME, ebp) );
2039 else
2040 fprintf( outfile, "\tpushl $0\n" );
2042 if ( UsePIC )
2043 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret@PLT\n ");
2044 else
2045 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n ");
2047 fprintf( outfile, "\tpopl %%eax\n" );
2048 fprintf( outfile, "\tpopl %%eax\n" );
2050 if ( UsePIC )
2051 fprintf( outfile, "\tpopl %%ebx\n" );
2055 if ( reg_func )
2057 fprintf( outfile, "\tmovl %%esp, %%ebx\n" );
2059 /* Switch stack back */
2060 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2061 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2062 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2064 /* Get return address to CallFrom16 stub */
2065 fprintf( outfile, "\taddw $%d, %%sp\n", STACK16OFFSET(callfrom_ip)-4 );
2066 fprintf( outfile, "\tpopl %%eax\n" );
2067 fprintf( outfile, "\tpopl %%edx\n" );
2069 /* Restore all registers from CONTEXT */
2070 fprintf( outfile, "\tmovw %d(%%ebx), %%ss\n", CONTEXTOFFSET(SegSs) );
2071 fprintf( outfile, "\tmovl %d(%%ebx), %%esp\n", CONTEXTOFFSET(Esp) );
2072 fprintf( outfile, "\taddl $4, %%esp\n" ); /* room for final return address */
2074 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
2075 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
2076 fprintf( outfile, "\tpushl %%edx\n" );
2077 fprintf( outfile, "\tpushl %%eax\n" );
2078 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFlags) );
2079 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
2081 fprintf( outfile, "\tmovw %d(%%ebx), %%es\n", CONTEXTOFFSET(SegEs) );
2082 fprintf( outfile, "\tmovw %d(%%ebx), %%fs\n", CONTEXTOFFSET(SegFs) );
2083 fprintf( outfile, "\tmovw %d(%%ebx), %%gs\n", CONTEXTOFFSET(SegGs) );
2085 fprintf( outfile, "\tmovl %d(%%ebx), %%ebp\n", CONTEXTOFFSET(Ebp) );
2086 fprintf( outfile, "\tmovl %d(%%ebx), %%esi\n", CONTEXTOFFSET(Esi) );
2087 fprintf( outfile, "\tmovl %d(%%ebx), %%edi\n", CONTEXTOFFSET(Edi) );
2088 fprintf( outfile, "\tmovl %d(%%ebx), %%eax\n", CONTEXTOFFSET(Eax) );
2089 fprintf( outfile, "\tmovl %d(%%ebx), %%edx\n", CONTEXTOFFSET(Edx) );
2090 fprintf( outfile, "\tmovl %d(%%ebx), %%ecx\n", CONTEXTOFFSET(Ecx) );
2091 fprintf( outfile, "\tmovl %d(%%ebx), %%ebx\n", CONTEXTOFFSET(Ebx) );
2093 fprintf( outfile, "\tpopl %%ds\n" );
2094 fprintf( outfile, "\tpopfl\n" );
2095 fprintf( outfile, "\tlret\n" );
2097 else
2099 /* Switch stack back */
2100 /* fprintf( outfile, "\t.byte 0x64\n\tlssw (%d), %%sp\n", STACKOFFSET ); */
2101 fprintf( outfile, "\t.byte 0x64,0x66,0x0f,0xb2,0x25\n\t.long %d\n", STACKOFFSET );
2102 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2104 /* Restore registers */
2105 fprintf( outfile, "\tpopl %%edx\n" );
2106 fprintf( outfile, "\tpopl %%ecx\n" );
2107 fprintf( outfile, "\tpopl %%ebp\n" );
2108 fprintf( outfile, "\tpopw %%ds\n" );
2109 fprintf( outfile, "\tpopw %%es\n" );
2110 fprintf( outfile, "\tpopw %%fs\n" );
2111 fprintf( outfile, "\tpopw %%gs\n" );
2113 /* Prepare return value and set flags accordingly */
2114 if ( !short_ret )
2115 fprintf( outfile, "\tshldl $16, %%eax, %%edx\n" );
2116 fprintf( outfile, "\torl %%eax, %%eax\n" );
2118 /* Return to return stub which will return to caller */
2119 fprintf( outfile, "\tlret $12\n" );
2124 /*******************************************************************
2125 * BuildCallTo16Core
2127 * This routine builds the core routines used in 32->16 thunks:
2129 * extern void CALLBACK CallTo16Word( SEGPTR target, int nb_args );
2130 * extern void CALLBACK CallTo16Long( SEGPTR target, int nb_args );
2131 * extern void CALLBACK CallTo16RegisterShort( const CONTEXT86 *context, int nb_args );
2132 * extern void CALLBACK CallTo16RegisterLong ( const CONTEXT86 *context, int nb_args );
2134 * These routines can be called directly from 32-bit code.
2136 * All routines expect that the 16-bit stack contents (arguments) were
2137 * already set up by the caller; nb_args must contain the number of bytes
2138 * to be conserved. The 16-bit SS:SP will be set accordinly.
2140 * All other registers are either taken from the CONTEXT86 structure
2141 * or else set to default values. The target routine address is either
2142 * given directly or taken from the CONTEXT86.
2144 * If you want to call a 16-bit routine taking only standard argument types
2145 * (WORD and LONG), you can also have an appropriate argument conversion
2146 * stub automatically generated (see BuildCallTo16); you'd then call this
2147 * stub, which in turn would prepare the 16-bit stack and call the appropiate
2148 * core routine.
2152 static void BuildCallTo16Core( FILE *outfile, int short_ret, int reg_func )
2154 char *name = reg_func == 2 ? "RegisterLong" :
2155 reg_func == 1 ? "RegisterShort" :
2156 short_ret? "Word" : "Long";
2158 /* Function header */
2159 fprintf( outfile, "\n\t.align 4\n" );
2160 #ifdef USE_STABS
2161 fprintf( outfile, ".stabs \"CallTo16%s:F1\",36,0,0," PREFIX "CallTo16%s\n",
2162 name, name);
2163 #endif
2164 fprintf( outfile, "\t.globl " PREFIX "CallTo16%s\n", name );
2165 fprintf( outfile, PREFIX "CallTo16%s:\n", name );
2167 /* Function entry sequence */
2168 fprintf( outfile, "\tpushl %%ebp\n" );
2169 fprintf( outfile, "\tmovl %%esp, %%ebp\n" );
2171 /* Save the 32-bit registers */
2172 fprintf( outfile, "\tpushl %%ebx\n" );
2173 fprintf( outfile, "\tpushl %%ecx\n" );
2174 fprintf( outfile, "\tpushl %%edx\n" );
2175 fprintf( outfile, "\tpushl %%esi\n" );
2176 fprintf( outfile, "\tpushl %%edi\n" );
2178 if ( UsePIC )
2180 /* Get Global Offset Table into %ebx */
2181 fprintf( outfile, "\tcall .LCallTo16%s.getgot1\n", name );
2182 fprintf( outfile, ".LCallTo16%s.getgot1:\n", name );
2183 fprintf( outfile, "\tpopl %%ebx\n" );
2184 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot1], %%ebx\n", name );
2187 /* Enter Win16 Mutex */
2188 if ( UsePIC )
2189 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock@PLT\n" );
2190 else
2191 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_EnterWin16Lock\n" );
2193 /* Print debugging info */
2194 if (debugging)
2196 /* Push flags, number of arguments, and target */
2197 fprintf( outfile, "\tpushl $%d\n", reg_func );
2198 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2199 fprintf( outfile, "\tpushl 8(%%ebp)\n" );
2201 if ( UsePIC )
2202 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16@PLT\n" );
2203 else
2204 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
2206 fprintf( outfile, "\taddl $12, %%esp\n" );
2209 /* Get return address */
2210 if ( UsePIC )
2211 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr@GOTOFF(%%ebx), %%ecx\n" );
2212 else
2213 fprintf( outfile, "\tmovl " PREFIX "CallTo16_RetAddr, %%ecx\n" );
2215 /* Call the actual CallTo16 routine (simulate a lcall) */
2216 fprintf( outfile, "\tpushl %%cs\n" );
2217 fprintf( outfile, "\tcall .LCallTo16%s\n", name );
2219 /* Convert and push return value */
2220 if ( short_ret )
2222 fprintf( outfile, "\tmovzwl %%ax, %%eax\n" );
2223 fprintf( outfile, "\tpushl %%eax\n" );
2225 else if ( reg_func != 2 )
2227 fprintf( outfile, "\tshll $16,%%edx\n" );
2228 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2229 fprintf( outfile, "\tpushl %%edx\n" );
2231 else
2232 fprintf( outfile, "\tpushl %%eax\n" );
2234 if ( UsePIC )
2236 /* Get Global Offset Table into %ebx (might have been overwritten) */
2237 fprintf( outfile, "\tcall .LCallTo16%s.getgot2\n", name );
2238 fprintf( outfile, ".LCallTo16%s.getgot2:\n", name );
2239 fprintf( outfile, "\tpopl %%ebx\n" );
2240 fprintf( outfile, "\taddl $_GLOBAL_OFFSET_TABLE_+[.-.LCallTo16%s.getgot2], %%ebx\n", name );
2243 /* Print debugging info */
2244 if (debugging)
2246 if ( UsePIC )
2247 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret@PLT\n" );
2248 else
2249 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16Ret\n" );
2252 /* Leave Win16 Mutex */
2253 if ( UsePIC )
2254 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock@PLT\n" );
2255 else
2256 fprintf( outfile, "\tcall " PREFIX "SYSLEVEL_LeaveWin16Lock\n" );
2258 /* Get return value */
2259 fprintf( outfile, "\tpopl %%eax\n" );
2261 /* Restore the 32-bit registers */
2262 fprintf( outfile, "\tpopl %%edi\n" );
2263 fprintf( outfile, "\tpopl %%esi\n" );
2264 fprintf( outfile, "\tpopl %%edx\n" );
2265 fprintf( outfile, "\tpopl %%ecx\n" );
2266 fprintf( outfile, "\tpopl %%ebx\n" );
2268 /* Function exit sequence */
2269 fprintf( outfile, "\tpopl %%ebp\n" );
2270 fprintf( outfile, "\tret $8\n" );
2273 /* Start of the actual CallTo16 routine */
2275 fprintf( outfile, ".LCallTo16%s:\n", name );
2277 /* Complete STACK32FRAME */
2278 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
2279 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
2281 /* Switch to the 16-bit stack */
2282 #ifdef __svr4__
2283 fprintf( outfile,"\tdata16\n");
2284 #endif
2285 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
2286 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
2287 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
2289 /* Make %bp point to the previous stackframe (built by CallFrom16) */
2290 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
2291 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n", STACK16OFFSET(bp) );
2293 /* Add the specified offset to the new sp */
2294 fprintf( outfile, "\tsubw %d(%%edx), %%sp\n", STACK32OFFSET(nb_args) );
2296 /* Push the return address
2297 * With sreg suffix, we push 16:16 address (normal lret)
2298 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
2300 if (reg_func != 2)
2301 fprintf( outfile, "\tpushl %%ecx\n" );
2302 else
2304 fprintf( outfile, "\tshldl $16, %%ecx, %%eax\n" );
2305 fprintf( outfile, "\tpushw $0\n" );
2306 fprintf( outfile, "\tpushw %%ax\n" );
2307 fprintf( outfile, "\tpushw $0\n" );
2308 fprintf( outfile, "\tpushw %%cx\n" );
2311 if (reg_func)
2313 /* Push the called routine address */
2314 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", STACK32OFFSET(target) );
2315 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegCs) );
2316 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(Eip) );
2318 /* Get the registers */
2319 fprintf( outfile, "\tpushw %d(%%edx)\n", CONTEXTOFFSET(SegDs) );
2320 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegEs) );
2321 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2322 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(SegFs) );
2323 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2324 fprintf( outfile, "\tmovl %d(%%edx),%%ebp\n", CONTEXTOFFSET(Ebp) );
2325 fprintf( outfile, "\tmovl %d(%%edx),%%esi\n", CONTEXTOFFSET(Esi) );
2326 fprintf( outfile, "\tmovl %d(%%edx),%%edi\n", CONTEXTOFFSET(Edi) );
2327 fprintf( outfile, "\tmovl %d(%%edx),%%eax\n", CONTEXTOFFSET(Eax) );
2328 fprintf( outfile, "\tmovl %d(%%edx),%%ebx\n", CONTEXTOFFSET(Ebx) );
2329 fprintf( outfile, "\tmovl %d(%%edx),%%ecx\n", CONTEXTOFFSET(Ecx) );
2330 fprintf( outfile, "\tmovl %d(%%edx),%%edx\n", CONTEXTOFFSET(Edx) );
2332 /* Get the 16-bit ds */
2333 fprintf( outfile, "\tpopw %%ds\n" );
2335 else /* not a register function */
2337 /* Push the called routine address */
2338 fprintf( outfile, "\tpushl %d(%%edx)\n", STACK32OFFSET(target) );
2340 /* Set %fs to the value saved by the last CallFrom16 */
2341 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", STACK16OFFSET(fs)-STACK16OFFSET(bp) );
2342 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2344 /* Set %ds and %es (and %ax just in case) equal to %ss */
2345 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2346 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2347 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2350 /* Jump to the called routine */
2351 fprintf( outfile, "\t.byte 0x66\n" );
2352 fprintf( outfile, "\tlret\n" );
2355 /*******************************************************************
2356 * BuildRet16Func
2358 * Build the return code for 16-bit callbacks
2360 static void BuildRet16Func( FILE *outfile )
2363 * Note: This must reside in the .data section to allow
2364 * run-time relocation of the SYSLEVEL_Win16CurrentTeb symbol
2367 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_Ret\n" );
2368 fprintf( outfile, PREFIX "CallTo16_Ret:\n" );
2370 /* Restore 32-bit segment registers */
2372 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2373 #ifdef __svr4__
2374 fprintf( outfile, "\tdata16\n");
2375 #endif
2376 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2377 #ifdef __svr4__
2378 fprintf( outfile, "\tdata16\n");
2379 #endif
2380 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2382 fprintf( outfile, "\tmovw " PREFIX "SYSLEVEL_Win16CurrentTeb,%%fs\n" );
2384 /* Restore the 32-bit stack */
2386 #ifdef __svr4__
2387 fprintf( outfile, "\tdata16\n");
2388 #endif
2389 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2390 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2391 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2393 /* Return to caller */
2395 fprintf( outfile, "\tlret\n" );
2397 /* Declare the return address variable */
2399 fprintf( outfile, "\n\t.globl " PREFIX "CallTo16_RetAddr\n" );
2400 fprintf( outfile, PREFIX "CallTo16_RetAddr:\t.long 0\n" );
2403 /*******************************************************************
2404 * BuildCallTo32CBClient
2406 * Call a CBClient relay stub from 32-bit code (KERNEL.620).
2408 * Since the relay stub is itself 32-bit, this should not be a problem;
2409 * unfortunately, the relay stubs are expected to switch back to a
2410 * 16-bit stack (and 16-bit code) after completion :-(
2412 * This would conflict with our 16- vs. 32-bit stack handling, so
2413 * we simply switch *back* to our 32-bit stack before returning to
2414 * the caller ...
2416 * The CBClient relay stub expects to be called with the following
2417 * 16-bit stack layout, and with ebp and ebx pointing into the 16-bit
2418 * stack at the designated places:
2420 * ...
2421 * (ebp+14) original arguments to the callback routine
2422 * (ebp+10) far return address to original caller
2423 * (ebp+6) Thunklet target address
2424 * (ebp+2) Thunklet relay ID code
2425 * (ebp) BP (saved by CBClientGlueSL)
2426 * (ebp-2) SI (saved by CBClientGlueSL)
2427 * (ebp-4) DI (saved by CBClientGlueSL)
2428 * (ebp-6) DS (saved by CBClientGlueSL)
2430 * ... buffer space used by the 16-bit side glue for temp copies
2432 * (ebx+4) far return address to 16-bit side glue code
2433 * (ebx) saved 16-bit ss:sp (pointing to ebx+4)
2435 * The 32-bit side glue code accesses both the original arguments (via ebp)
2436 * and the temporary copies prepared by the 16-bit side glue (via ebx).
2437 * After completion, the stub will load ss:sp from the buffer at ebx
2438 * and perform a far return to 16-bit code.
2440 * To trick the relay stub into returning to us, we replace the 16-bit
2441 * return address to the glue code by a cs:ip pair pointing to our
2442 * return entry point (the original return address is saved first).
2443 * Our return stub thus called will then reload the 32-bit ss:esp and
2444 * return to 32-bit code (by using and ss:esp value that we have also
2445 * pushed onto the 16-bit stack before and a cs:eip values found at
2446 * that position on the 32-bit stack). The ss:esp to be restored is
2447 * found relative to the 16-bit stack pointer at:
2449 * (ebx-4) ss (flat)
2450 * (ebx-8) sp (32-bit stack pointer)
2452 * The second variant of this routine, CALL32_CBClientEx, which is used
2453 * to implement KERNEL.621, has to cope with yet another problem: Here,
2454 * the 32-bit side directly returns to the caller of the CBClient thunklet,
2455 * restoring registers saved by CBClientGlueSL and cleaning up the stack.
2456 * As we have to return to our 32-bit code first, we have to adapt the
2457 * layout of our temporary area so as to include values for the registers
2458 * that are to be restored, and later (in the implementation of KERNEL.621)
2459 * we *really* restore them. The return stub restores DS, DI, SI, and BP
2460 * from the stack, skips the next 8 bytes (CBClient relay code / target),
2461 * and then performs a lret NN, where NN is the number of arguments to be
2462 * removed. Thus, we prepare our temporary area as follows:
2464 * (ebx+22) 16-bit cs (this segment)
2465 * (ebx+20) 16-bit ip ('16-bit' return entry point)
2466 * (ebx+16) 32-bit ss (flat)
2467 * (ebx+12) 32-bit sp (32-bit stack pointer)
2468 * (ebx+10) 16-bit bp (points to ebx+24)
2469 * (ebx+8) 16-bit si (ignored)
2470 * (ebx+6) 16-bit di (ignored)
2471 * (ebx+4) 16-bit ds (we actually use the flat DS here)
2472 * (ebx+2) 16-bit ss (16-bit stack segment)
2473 * (ebx+0) 16-bit sp (points to ebx+4)
2475 * Note that we ensure that DS is not changed and remains the flat segment,
2476 * and the 32-bit stack pointer our own return stub needs fits just
2477 * perfectly into the 8 bytes that are skipped by the Windows stub.
2478 * One problem is that we have to determine the number of removed arguments,
2479 * as these have to be really removed in KERNEL.621. Thus, the BP value
2480 * that we place in the temporary area to be restored, contains the value
2481 * that SP would have if no arguments were removed. By comparing the actual
2482 * value of SP with this value in our return stub we can compute the number
2483 * of removed arguments. This is then returned to KERNEL.621.
2485 * The stack layout of this function:
2486 * (ebp+20) nArgs pointer to variable receiving nr. of args (Ex only)
2487 * (ebp+16) esi pointer to caller's esi value
2488 * (ebp+12) arg ebp value to be set for relay stub
2489 * (ebp+8) func CBClient relay stub address
2490 * (ebp+4) ret addr
2491 * (ebp) ebp
2493 static void BuildCallTo32CBClient( FILE *outfile, BOOL isEx )
2495 char *name = isEx? "CBClientEx" : "CBClient";
2496 int size = isEx? 24 : 12;
2498 /* Function header */
2500 fprintf( outfile, "\n\t.align 4\n" );
2501 #ifdef USE_STABS
2502 fprintf( outfile, ".stabs \"CALL32_%s:F1\",36,0,0," PREFIX "CALL32_%s\n",
2503 name, name );
2504 #endif
2505 fprintf( outfile, "\t.globl " PREFIX "CALL32_%s\n", name );
2506 fprintf( outfile, PREFIX "CALL32_%s:\n", name );
2508 /* Entry code */
2510 fprintf( outfile, "\tpushl %%ebp\n" );
2511 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2512 fprintf( outfile, "\tpushl %%edi\n" );
2513 fprintf( outfile, "\tpushl %%esi\n" );
2514 fprintf( outfile, "\tpushl %%ebx\n" );
2516 /* Get the 16-bit stack */
2518 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET);
2520 /* Convert it to a flat address */
2522 fprintf( outfile, "\tshldl $16,%%ebx,%%eax\n" );
2523 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
2524 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%esi\n" );
2525 fprintf( outfile, "\tmovw %%bx,%%ax\n" );
2526 fprintf( outfile, "\taddl %%eax,%%esi\n" );
2528 /* Allocate temporary area (simulate STACK16_PUSH) */
2530 fprintf( outfile, "\tpushf\n" );
2531 fprintf( outfile, "\tcld\n" );
2532 fprintf( outfile, "\tleal -%d(%%esi), %%edi\n", size );
2533 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2534 fprintf( outfile, "\trep\n\tmovsb\n" );
2535 fprintf( outfile, "\tpopf\n" );
2537 fprintf( outfile, "\t.byte 0x64\n\tsubw $%d,(%d)\n", size, STACKOFFSET );
2539 fprintf( outfile, "\tpushl %%edi\n" ); /* remember address */
2541 /* Set up temporary area */
2543 if ( !isEx )
2545 fprintf( outfile, "\tleal 4(%%edi), %%edi\n" );
2547 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2548 fprintf( outfile, "\tmovl %%eax, -8(%%edi)\n" ); /* 32-bit sp */
2550 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2551 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2552 fprintf( outfile, "\tmovl %%eax, -4(%%edi)\n" ); /* 32-bit ss */
2554 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 + 4 );
2555 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" ); /* 16-bit ss:sp */
2557 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2558 fprintf( outfile, "\tmovl %%eax, 4(%%edi)\n" ); /* overwrite return address */
2560 else
2562 fprintf( outfile, "\taddl $%d, %%ebx\n", sizeof(STACK16FRAME)-size+4 );
2563 fprintf( outfile, "\tmovl %%ebx, 0(%%edi)\n" );
2565 fprintf( outfile, "\tmovl %%ds, %%ax\n" );
2566 fprintf( outfile, "\tmovw %%ax, 4(%%edi)\n" );
2568 fprintf( outfile, "\taddl $20, %%ebx\n" );
2569 fprintf( outfile, "\tmovw %%bx, 10(%%edi)\n" );
2571 fprintf( outfile, "\tleal -8(%%esp), %%eax\n" );
2572 fprintf( outfile, "\tmovl %%eax, 12(%%edi)\n" );
2574 fprintf( outfile, "\tmovl %%ss, %%ax\n" );
2575 fprintf( outfile, "\tandl $0x0000ffff, %%eax\n" );
2576 fprintf( outfile, "\tmovl %%eax, 16(%%edi)\n" );
2578 fprintf( outfile, "\tmovl " PREFIX "CALL32_%s_RetAddr, %%eax\n", name );
2579 fprintf( outfile, "\tmovl %%eax, 20(%%edi)\n" );
2582 /* Set up registers and call CBClient relay stub (simulating a far call) */
2584 fprintf( outfile, "\tmovl 16(%%ebp), %%esi\n" );
2585 fprintf( outfile, "\tmovl (%%esi), %%esi\n" );
2587 fprintf( outfile, "\tmovl %%edi, %%ebx\n" );
2588 fprintf( outfile, "\tmovl 8(%%ebp), %%eax\n" );
2589 fprintf( outfile, "\tmovl 12(%%ebp), %%ebp\n" );
2591 fprintf( outfile, "\tpushl %%cs\n" );
2592 fprintf( outfile, "\tcall *%%eax\n" );
2594 /* Return new esi value to caller */
2596 fprintf( outfile, "\tmovl 32(%%esp), %%edi\n" );
2597 fprintf( outfile, "\tmovl %%esi, (%%edi)\n" );
2599 /* Cleanup temporary area (simulate STACK16_POP) */
2601 fprintf( outfile, "\tpop %%esi\n" );
2603 fprintf( outfile, "\tpushf\n" );
2604 fprintf( outfile, "\tstd\n" );
2605 fprintf( outfile, "\tdec %%esi\n" );
2606 fprintf( outfile, "\tleal %d(%%esi), %%edi\n", size );
2607 fprintf( outfile, "\tmovl $%d, %%ecx\n", sizeof(STACK16FRAME) );
2608 fprintf( outfile, "\trep\n\tmovsb\n" );
2609 fprintf( outfile, "\tpopf\n" );
2611 fprintf( outfile, "\t.byte 0x64\n\taddw $%d,(%d)\n", size, STACKOFFSET );
2613 /* Return argument size to caller */
2614 if ( isEx )
2616 fprintf( outfile, "\tmovl 32(%%esp), %%ebx\n" );
2617 fprintf( outfile, "\tmovl %%ebp, (%%ebx)\n" );
2620 /* Restore registers and return */
2622 fprintf( outfile, "\tpopl %%ebx\n" );
2623 fprintf( outfile, "\tpopl %%esi\n" );
2624 fprintf( outfile, "\tpopl %%edi\n" );
2625 fprintf( outfile, "\tpopl %%ebp\n" );
2626 fprintf( outfile, "\tret\n" );
2629 static void BuildCallTo32CBClientRet( FILE *outfile, BOOL isEx )
2631 char *name = isEx? "CBClientEx" : "CBClient";
2633 /* '16-bit' return stub */
2635 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_Ret\n", name );
2636 fprintf( outfile, PREFIX "CALL32_%s_Ret:\n", name );
2638 if ( !isEx )
2640 fprintf( outfile, "\tmovzwl %%sp, %%ebx\n" );
2641 fprintf( outfile, "\tlssl %%ss:-16(%%ebx), %%esp\n" );
2643 else
2645 fprintf( outfile, "\tmovzwl %%bp, %%ebx\n" );
2646 fprintf( outfile, "\tsubw %%bp, %%sp\n" );
2647 fprintf( outfile, "\tmovzwl %%sp, %%ebp\n" );
2648 fprintf( outfile, "\tlssl %%ss:-12(%%ebx), %%esp\n" );
2650 fprintf( outfile, "\tlret\n" );
2652 /* Declare the return address variable */
2654 fprintf( outfile, "\n\t.globl " PREFIX "CALL32_%s_RetAddr\n", name );
2655 fprintf( outfile, PREFIX "CALL32_%s_RetAddr:\t.long 0\n", name );
2659 /*******************************************************************
2660 * BuildCallTo32LargeStack
2662 * Build the function used to switch to the original 32-bit stack
2663 * before calling a 32-bit function from 32-bit code. This is used for
2664 * functions that need a large stack, like X bitmaps functions.
2666 * The generated function has the following prototype:
2667 * int xxx( int (*func)(), void *arg );
2669 * The pointer to the function can be retrieved by calling CALL32_Init,
2670 * which also takes care of saving the current 32-bit stack pointer.
2671 * Furthermore, CALL32_Init switches to a new stack and jumps to the
2672 * specified target address.
2674 * NOTE: The CALL32_LargeStack routine may be recursively entered by the
2675 * same thread, but not concurrently entered by several threads.
2677 * Stack layout of CALL32_Init:
2679 * (esp+12) new stack address
2680 * (esp+8) target address
2681 * (esp+4) pointer to variable to receive CALL32_LargeStack address
2682 * (esp) ret addr
2684 * Stack layout of CALL32_LargeStack:
2685 * ... ...
2686 * (ebp+12) arg
2687 * (ebp+8) func
2688 * (ebp+4) ret addr
2689 * (ebp) ebp
2691 static void BuildCallTo32LargeStack( FILE *outfile )
2693 /* Initialization function */
2695 fprintf( outfile, "\n\t.align 4\n" );
2696 #ifdef USE_STABS
2697 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2698 #endif
2699 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2700 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2701 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2702 fprintf( outfile, "\tmovl %%esp,CALL32_Original32_esp\n" );
2703 fprintf( outfile, "\tpopl %%eax\n" );
2704 fprintf( outfile, "\tpopl %%eax\n" );
2705 fprintf( outfile, "\tmovl $CALL32_LargeStack,(%%eax)\n" );
2706 fprintf( outfile, "\tpopl %%eax\n" );
2707 fprintf( outfile, "\tpopl %%esp\n" );
2708 fprintf( outfile, "\tpushl %%eax\n" );
2709 fprintf( outfile, "\tret\n" );
2711 /* Function header */
2713 fprintf( outfile, "\n\t.align 4\n" );
2714 #ifdef USE_STABS
2715 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2716 #endif
2717 fprintf( outfile, "CALL32_LargeStack:\n" );
2719 /* Entry code */
2721 fprintf( outfile, "\tpushl %%ebp\n" );
2722 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2724 /* Switch to the original 32-bit stack pointer */
2726 fprintf( outfile, "\tcmpl $0, CALL32_RecursionCount\n" );
2727 fprintf( outfile, "\tjne CALL32_skip\n" );
2728 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2729 fprintf( outfile, "CALL32_skip:\n" );
2731 fprintf( outfile, "\tincl CALL32_RecursionCount\n" );
2733 /* Transfer the argument and call the function */
2735 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2736 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2738 /* Restore registers and return */
2740 fprintf( outfile, "\tdecl CALL32_RecursionCount\n" );
2742 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2743 fprintf( outfile, "\tpopl %%ebp\n" );
2744 fprintf( outfile, "\tret\n" );
2746 /* Data */
2748 fprintf( outfile, "\t.data\n" );
2749 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2750 fprintf( outfile, "CALL32_RecursionCount:\t.long 0\n" );
2751 fprintf( outfile, "\t.text\n" );
2755 /*******************************************************************
2756 * BuildCallFrom32Regs
2758 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2759 * 'args' is the number of dword arguments.
2761 * Stack layout:
2762 * ...
2763 * (ebp+12) first arg
2764 * (ebp+8) ret addr to user code
2765 * (ebp+4) ret addr to relay code
2766 * (ebp+0) saved ebp
2767 * (ebp-128) buffer area to allow stack frame manipulation
2768 * (ebp-332) CONTEXT86 struct
2769 * (ebp-336) CONTEXT86 *argument
2770 * .... other arguments copied from (ebp+12)
2772 * The entry point routine is called with a CONTEXT* extra argument,
2773 * following the normal args. In this context structure, EIP_reg
2774 * contains the return address to user code, and ESP_reg the stack
2775 * pointer on return (with the return address and arguments already
2776 * removed).
2778 static void BuildCallFrom32Regs( FILE *outfile )
2780 static const int STACK_SPACE = 128 + sizeof(CONTEXT86);
2782 /* Function header */
2784 fprintf( outfile, "\n\t.align 4\n" );
2785 #ifdef USE_STABS
2786 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2787 #endif
2788 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2789 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2791 /* Allocate some buffer space on the stack */
2793 fprintf( outfile, "\tpushl %%ebp\n" );
2794 fprintf( outfile, "\tmovl %%esp,%%ebp\n ");
2795 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2797 /* Build the context structure */
2799 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2800 fprintf( outfile, "\tpushfl\n" );
2801 fprintf( outfile, "\tpopl %%eax\n" );
2802 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2803 fprintf( outfile, "\tmovl 0(%%ebp),%%eax\n" );
2804 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2805 fprintf( outfile, "\tmovl %%ebx,%d(%%ebp)\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2806 fprintf( outfile, "\tmovl %%ecx,%d(%%ebp)\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2807 fprintf( outfile, "\tmovl %%edx,%d(%%ebp)\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2808 fprintf( outfile, "\tmovl %%esi,%d(%%ebp)\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2809 fprintf( outfile, "\tmovl %%edi,%d(%%ebp)\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2811 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2812 fprintf( outfile, "\tmovw %%cs,%%ax\n" );
2813 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegCs) - STACK_SPACE );
2814 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2815 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2816 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2817 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2818 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2819 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2820 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2821 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegSs) - STACK_SPACE );
2822 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2823 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(SegDs) - STACK_SPACE );
2824 fprintf( outfile, "\tmovw %%ax,%%es\n" ); /* set %es equal to %ds just in case */
2826 fprintf( outfile, "\tmovl $0x%x,%%eax\n", CONTEXT86_FULL );
2827 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(ContextFlags) - STACK_SPACE );
2829 fprintf( outfile, "\tmovl 8(%%ebp),%%eax\n" ); /* Get %eip at time of call */
2830 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2832 /* Transfer the arguments */
2834 fprintf( outfile, "\tmovl 4(%%ebp),%%ebx\n" ); /* get relay code addr */
2835 fprintf( outfile, "\tpushl %%esp\n" ); /* push ptr to context struct */
2836 fprintf( outfile, "\tmovzbl 4(%%ebx),%%ecx\n" ); /* fetch number of args to copy */
2837 fprintf( outfile, "\tjecxz 1f\n" );
2838 fprintf( outfile, "\tsubl %%ecx,%%esp\n" );
2839 fprintf( outfile, "\tleal 12(%%ebp),%%esi\n" ); /* get %esp at time of call */
2840 fprintf( outfile, "\tmovl %%esp,%%edi\n" );
2841 fprintf( outfile, "\tshrl $2,%%ecx\n" );
2842 fprintf( outfile, "\tcld\n" );
2843 fprintf( outfile, "\trep\n\tmovsl\n" ); /* copy args */
2845 fprintf( outfile, "1:\tmovzbl 5(%%ebx),%%eax\n" ); /* fetch number of args to remove */
2846 fprintf( outfile, "\tleal 12(%%ebp,%%eax),%%eax\n" );
2847 fprintf( outfile, "\tmovl %%eax,%d(%%ebp)\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2849 /* Call the entry point */
2851 fprintf( outfile, "\tcall *0(%%ebx)\n" );
2853 /* Store %eip and %ebp onto the new stack */
2855 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2856 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eip) - STACK_SPACE );
2857 fprintf( outfile, "\tmovl %%eax,-4(%%edx)\n" );
2858 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Ebp) - STACK_SPACE );
2859 fprintf( outfile, "\tmovl %%eax,-8(%%edx)\n" );
2861 /* Restore the context structure */
2863 /* Note: we don't bother to restore %cs, %ds and %ss
2864 * changing them in 32-bit code is a recipe for disaster anyway
2866 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegEs) - STACK_SPACE );
2867 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2868 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegFs) - STACK_SPACE );
2869 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2870 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(SegGs) - STACK_SPACE );
2871 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2873 fprintf( outfile, "\tmovl %d(%%ebp),%%edi\n", CONTEXTOFFSET(Edi) - STACK_SPACE );
2874 fprintf( outfile, "\tmovl %d(%%ebp),%%esi\n", CONTEXTOFFSET(Esi) - STACK_SPACE );
2875 fprintf( outfile, "\tmovl %d(%%ebp),%%edx\n", CONTEXTOFFSET(Edx) - STACK_SPACE );
2876 fprintf( outfile, "\tmovl %d(%%ebp),%%ecx\n", CONTEXTOFFSET(Ecx) - STACK_SPACE );
2877 fprintf( outfile, "\tmovl %d(%%ebp),%%ebx\n", CONTEXTOFFSET(Ebx) - STACK_SPACE );
2879 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(EFlags) - STACK_SPACE );
2880 fprintf( outfile, "\tpushl %%eax\n" );
2881 fprintf( outfile, "\tpopfl\n" );
2882 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", CONTEXTOFFSET(Eax) - STACK_SPACE );
2884 fprintf( outfile, "\tmovl %d(%%ebp),%%ebp\n", CONTEXTOFFSET(Esp) - STACK_SPACE );
2885 fprintf( outfile, "\tleal -8(%%ebp),%%esp\n" );
2886 fprintf( outfile, "\tpopl %%ebp\n" );
2887 fprintf( outfile, "\tret\n" );
2891 /*******************************************************************
2892 * BuildSpec
2894 * Build the spec files
2896 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2898 int i;
2899 for (i = 2; i < argc; i++)
2900 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2901 return 0;
2904 /*******************************************************************
2905 * BuildGlue
2907 * Build the 16-bit-to-Wine/Wine-to-16-bit callback glue code
2909 static int BuildGlue( FILE *outfile, char * outname, int argc, char *argv[] )
2911 char buffer[1024];
2912 FILE *infile;
2914 if (argc > 2)
2916 infile = fopen( argv[2], "r" );
2917 if (!infile)
2919 perror( argv[2] );
2920 exit( 1 );
2923 else infile = stdin;
2925 /* File header */
2927 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
2928 argc > 2? argv[2] : "<stdin>" );
2929 fprintf( outfile, "#include \"builtin16.h\"\n" );
2930 fprintf( outfile, "#include \"stackframe.h\"\n\n" );
2932 /* Build the callback glue functions */
2934 while (fgets( buffer, sizeof(buffer), infile ))
2936 if (strstr( buffer, "### start build ###" )) break;
2938 while (fgets( buffer, sizeof(buffer), infile ))
2940 char *p;
2941 if ( (p = strstr( buffer, "CallFrom16_" )) != NULL )
2943 char *q, *profile = p + strlen( "CallFrom16_" );
2944 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2946 *q = '\0';
2947 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2949 if ( ++q < p ) p[-1] = '\0'; else q = "";
2950 BuildCallFrom16Func( outfile, profile, q, FALSE );
2952 if ( (p = strstr( buffer, "CallTo16_" )) != NULL )
2954 char *q, *profile = p + strlen( "CallTo16_" );
2955 for (q = profile; (*q == '_') || isalpha(*q); q++ )
2957 *q = '\0';
2958 for (q = p-1; q > buffer && ((*q == '_') || isalnum(*q)); q-- )
2960 if ( ++q < p ) p[-1] = '\0'; else q = "";
2961 BuildCallTo16Func( outfile, profile, q );
2963 if (strstr( buffer, "### stop build ###" )) break;
2966 fclose( infile );
2967 return 0;
2970 /*******************************************************************
2971 * BuildCall16
2973 * Build the 16-bit callbacks
2975 static int BuildCall16( FILE *outfile, char * outname )
2977 #ifdef USE_STABS
2978 char buffer[1024];
2979 #endif
2981 /* File header */
2983 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2984 fprintf( outfile, "\t.text\n" );
2986 #ifdef __i386__
2988 #ifdef USE_STABS
2989 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2990 getcwd(buffer, sizeof(buffer));
2993 * The stabs help the internal debugger as they are an indication that it
2994 * is sensible to step into a thunk/trampoline.
2996 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2997 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2998 fprintf( outfile, "\t.text\n" );
2999 fprintf( outfile, "\t.align 4\n" );
3000 fprintf( outfile, "Code_Start:\n\n" );
3001 #endif
3002 fprintf( outfile, PREFIX"Call16_Start:\n" );
3003 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3004 fprintf( outfile, "\t.byte 0\n\n" );
3007 /* Standard CallFrom16 routine (WORD return) */
3008 BuildCallFrom16Core( outfile, FALSE, FALSE, TRUE );
3010 /* Standard CallFrom16 routine (DWORD return) */
3011 BuildCallFrom16Core( outfile, FALSE, FALSE, FALSE );
3013 /* Register CallFrom16 routine */
3014 BuildCallFrom16Core( outfile, TRUE, FALSE, FALSE );
3016 /* C16ThkSL CallFrom16 routine */
3017 BuildCallFrom16Core( outfile, FALSE, TRUE, FALSE );
3019 /* Standard CallTo16 routine (WORD return) */
3020 BuildCallTo16Core( outfile, TRUE, FALSE );
3022 /* Standard CallTo16 routine (DWORD return) */
3023 BuildCallTo16Core( outfile, FALSE, FALSE );
3025 /* Register CallTo16 routine (16:16 retf) */
3026 BuildCallTo16Core( outfile, FALSE, 1 );
3028 /* Register CallTo16 routine (16:32 retf) */
3029 BuildCallTo16Core( outfile, FALSE, 2 );
3031 /* CBClientThunkSL routine */
3032 BuildCallTo32CBClient( outfile, FALSE );
3034 /* CBClientThunkSLEx routine */
3035 BuildCallTo32CBClient( outfile, TRUE );
3037 fprintf( outfile, PREFIX"Call16_End:\n" );
3038 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3040 #ifdef USE_STABS
3041 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3042 fprintf( outfile, ".Letext:\n");
3043 #endif
3045 /* The whole Call16_Ret segment must lie within the .data section */
3046 fprintf( outfile, "\n\t.data\n" );
3047 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3048 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3050 /* Standard CallTo16 return stub */
3051 BuildRet16Func( outfile );
3053 /* CBClientThunkSL return stub */
3054 BuildCallTo32CBClientRet( outfile, FALSE );
3056 /* CBClientThunkSLEx return stub */
3057 BuildCallTo32CBClientRet( outfile, TRUE );
3059 /* End of Call16_Ret segment */
3060 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3061 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3063 #else /* __i386__ */
3065 fprintf( outfile, PREFIX"Call16_Start:\n" );
3066 fprintf( outfile, "\t.globl "PREFIX"Call16_Start\n" );
3067 fprintf( outfile, "\t.byte 0\n\n" );
3068 fprintf( outfile, PREFIX"Call16_End:\n" );
3069 fprintf( outfile, "\t.globl "PREFIX"Call16_End\n" );
3071 fprintf( outfile, "\t.globl " PREFIX "Call16_Ret_Start\n" );
3072 fprintf( outfile, PREFIX "Call16_Ret_Start:\n" );
3073 fprintf( outfile, "\t.byte 0\n\n" );
3074 fprintf( outfile, "\n\t.globl " PREFIX "Call16_Ret_End\n" );
3075 fprintf( outfile, PREFIX "Call16_Ret_End:\n" );
3077 #endif /* __i386__ */
3079 return 0;
3082 /*******************************************************************
3083 * BuildCall32
3085 * Build the 32-bit callbacks
3087 static int BuildCall32( FILE *outfile, char * outname )
3089 #ifdef USE_STABS
3090 char buffer[1024];
3091 #endif
3093 /* File header */
3095 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
3096 fprintf( outfile, "\t.text\n" );
3098 #ifdef __i386__
3100 #ifdef USE_STABS
3101 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
3102 getcwd(buffer, sizeof(buffer));
3105 * The stabs help the internal debugger as they are an indication that it
3106 * is sensible to step into a thunk/trampoline.
3108 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
3109 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
3110 fprintf( outfile, "\t.text\n" );
3111 fprintf( outfile, "\t.align 4\n" );
3112 fprintf( outfile, "Code_Start:\n" );
3113 #endif
3115 /* Build the 32-bit large stack callback */
3117 BuildCallTo32LargeStack( outfile );
3119 /* Build the register callback function */
3121 BuildCallFrom32Regs( outfile );
3123 #ifdef USE_STABS
3124 fprintf( outfile, "\t.text\n");
3125 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
3126 fprintf( outfile, ".Letext:\n");
3127 #endif
3129 #else /* __i386__ */
3131 /* Just to avoid an empty file */
3132 fprintf( outfile, "\t.long 0\n" );
3134 #endif /* __i386__ */
3135 return 0;
3139 /*******************************************************************
3140 * usage
3142 static void usage(void)
3144 fprintf( stderr,
3145 "usage: build [-pic] [-o outfile] -spec SPECNAMES\n"
3146 " build [-pic] [-o outfile] -glue SOURCE_FILE\n"
3147 " build [-pic] [-o outfile] -call16\n"
3148 " build [-pic] [-o outfile] -call32\n" );
3149 exit(1);
3153 /*******************************************************************
3154 * main
3156 int main(int argc, char **argv)
3158 char *outname = NULL;
3159 FILE *outfile = stdout;
3160 int res = -1;
3162 if (argc < 2) usage();
3164 if (!strcmp( argv[1], "-pic" ))
3166 UsePIC = 1;
3167 argv += 1;
3168 argc -= 1;
3169 if (argc < 2) usage();
3172 if (!strcmp( argv[1], "-o" ))
3174 outname = argv[2];
3175 argv += 2;
3176 argc -= 2;
3177 if (argc < 2) usage();
3178 if (!(outfile = fopen( outname, "w" )))
3180 fprintf( stderr, "Unable to create output file '%s'\n", outname );
3181 exit(1);
3185 /* Retrieve the selector values; this assumes that we are building
3186 * the asm files on the platform that will also run them. Probably
3187 * a safe assumption to make.
3189 GET_CS( Code_Selector );
3190 GET_DS( Data_Selector );
3192 if (!strcmp( argv[1], "-spec" ))
3193 res = BuildSpec( outfile, argc, argv );
3194 else if (!strcmp( argv[1], "-glue" ))
3195 res = BuildGlue( outfile, outname, argc, argv );
3196 else if (!strcmp( argv[1], "-call16" ))
3197 res = BuildCall16( outfile, outname );
3198 else if (!strcmp( argv[1], "-call32" ))
3199 res = BuildCall32( outfile, outname );
3200 else
3202 fclose( outfile );
3203 unlink( outname );
3204 usage();
3207 fclose( outfile );
3208 if (res < 0)
3210 unlink( outname );
3211 return 1;
3213 return 0;