2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996, 1997 Alexandre Julliard
5 * Copyright 1997 Eric Youngdale
19 #include "selectors.h"
20 #include "stackframe.h"
23 #ifdef NEED_UNDERSCORE_PREFIX
29 #ifdef HAVE_ASM_STRING
30 # define STRING ".string"
32 # define STRING ".ascii"
35 #if defined(__GNUC__) && !defined(__svr4__)
44 TYPE_BYTE, /* byte variable (Win16) */
45 TYPE_WORD, /* word variable (Win16) */
46 TYPE_LONG, /* long variable (Win16) */
47 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
48 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
49 TYPE_ABS, /* absolute value (Win16) */
50 TYPE_RETURN, /* simple return value function (Win16) */
51 TYPE_REGISTER, /* register function */
52 TYPE_STUB, /* unimplemented stub */
53 TYPE_STDCALL, /* stdcall function (Win32) */
54 TYPE_CDECL, /* cdecl function (Win32) */
55 TYPE_VARARGS, /* varargs function (Win32) */
56 TYPE_EXTERN, /* external symbol (Win32) */
60 static const char * const TypeNames[TYPE_NBTYPES] =
63 "byte", /* TYPE_BYTE */
64 "word", /* TYPE_WORD */
65 "long", /* TYPE_LONG */
66 "pascal16", /* TYPE_PASCAL_16 */
67 "pascal", /* TYPE_PASCAL */
68 "equate", /* TYPE_ABS */
69 "return", /* TYPE_RETURN */
70 "register", /* TYPE_REGISTER */
71 "stub", /* TYPE_STUB */
72 "stdcall", /* TYPE_STDCALL */
73 "cdecl", /* TYPE_CDECL */
74 "varargs", /* TYPE_VARARGS */
75 "extern" /* TYPE_EXTERN */
78 #define MAX_ORDINALS 2048
80 /* Callback function used for stub functions */
81 #define STUB_CALLBACK \
82 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
142 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
144 static SPEC_TYPE SpecType = SPEC_INVALID;
145 static char DLLName[80];
146 static char DLLFileName[80];
147 static int Limit = 0;
148 static int Base = MAX_ORDINALS;
149 static int DLLHeapSize = 0;
150 static char *SpecName;
152 static WORD Code_Selector, Data_Selector;
154 char *ParseBuffer = NULL;
159 static int debugging = 1;
161 /* Offset of a structure field relative to the start of the struct */
162 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
164 /* Offset of register relative to the start of the CONTEXT struct */
165 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
167 /* Offset of the stack pointer relative to %fs:(0) */
168 #define STACKOFFSET (STRUCTOFFSET(THDB,cur_stack) - STRUCTOFFSET(THDB,teb))
170 /* Offset of the saved fs relative to %fs:(0) */
171 #define FSOFFSET (STRUCTOFFSET(THDB,saved_fs) - STRUCTOFFSET(THDB,teb))
174 static void *xmalloc (size_t size)
178 res = malloc (size ? size : 1);
181 fprintf (stderr, "Virtual memory exhausted.\n");
188 static void *xrealloc (void *ptr, size_t size)
190 void *res = realloc (ptr, size);
193 fprintf (stderr, "Virtual memory exhausted.\n");
200 static int IsNumberString(char *s)
209 static char *strupper(char *s)
213 for(p = s; *p != '\0'; p++)
219 static char * GetTokenInLine(void)
224 if (ParseNext != ParseBuffer)
226 if (ParseSaveChar == '\0')
228 *ParseNext = ParseSaveChar;
232 * Remove initial white space.
234 for (p = ParseNext; isspace(*p); p++)
237 if ((*p == '\0') || (*p == '#'))
244 if (*token != '(' && *token != ')')
245 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
255 static char * GetToken(void)
259 if (ParseBuffer == NULL)
261 ParseBuffer = xmalloc(512);
262 ParseNext = ParseBuffer;
266 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
268 if (ParseBuffer[0] != '#')
273 while ((token = GetTokenInLine()) == NULL)
275 ParseNext = ParseBuffer;
279 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
281 if (ParseBuffer[0] != '#')
290 /*******************************************************************
293 * Parse a variable definition.
295 static int ParseVariable( ORDDEF *odp )
300 int value_array_size;
302 char *token = GetToken();
305 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
306 SpecName, Line, token);
311 value_array_size = 25;
312 value_array = xmalloc(sizeof(*value_array) * value_array_size);
314 while ((token = GetToken()) != NULL)
319 value_array[n_values++] = strtol(token, &endptr, 0);
320 if (n_values == value_array_size)
322 value_array_size += 25;
323 value_array = xrealloc(value_array,
324 sizeof(*value_array) * value_array_size);
327 if (endptr == NULL || *endptr != '\0')
329 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
330 SpecName, Line, token);
337 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
342 odp->u.var.n_values = n_values;
343 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
349 /*******************************************************************
350 * ParseExportFunction
352 * Parse a function definition.
354 static int ParseExportFunction( ORDDEF *odp )
362 if (odp->type == TYPE_STDCALL)
364 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
370 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
372 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
384 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
385 SpecName, Line, token);
389 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
395 if (!strcmp(token, "word"))
396 odp->u.func.arg_types[i] = 'w';
397 else if (!strcmp(token, "s_word"))
398 odp->u.func.arg_types[i] = 's';
399 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
400 odp->u.func.arg_types[i] = 'l';
401 else if (!strcmp(token, "ptr"))
402 odp->u.func.arg_types[i] = 'p';
403 else if (!strcmp(token, "str"))
404 odp->u.func.arg_types[i] = 't';
405 else if (!strcmp(token, "wstr"))
406 odp->u.func.arg_types[i] = 'W';
407 else if (!strcmp(token, "segstr"))
408 odp->u.func.arg_types[i] = 'T';
409 else if (!strcmp(token, "double"))
411 odp->u.func.arg_types[i++] = 'l';
412 odp->u.func.arg_types[i] = 'l';
416 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
417 SpecName, Line, token);
420 if (SpecType == SPEC_WIN32)
422 if (strcmp(token, "long") &&
423 strcmp(token, "ptr") &&
424 strcmp(token, "str") &&
425 strcmp(token, "wstr") &&
426 strcmp(token, "double"))
428 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
429 SpecName, Line, token );
434 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
436 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
439 odp->u.func.arg_types[i] = '\0';
440 if ((odp->type == TYPE_STDCALL) && !i)
441 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
442 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
444 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
448 strcpy(odp->u.func.link_name, GetToken());
453 /*******************************************************************
456 * Parse an 'equate' definition.
458 static int ParseEquate( ORDDEF *odp )
462 char *token = GetToken();
463 int value = strtol(token, &endptr, 0);
464 if (endptr == NULL || *endptr != '\0')
466 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
467 SpecName, Line, token);
471 if (SpecType == SPEC_WIN32)
473 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
478 odp->u.abs.value = value;
483 /*******************************************************************
486 * Parse a 'return' definition.
488 static int ParseReturn( ORDDEF *odp )
494 odp->u.ret.arg_size = strtol(token, &endptr, 0);
495 if (endptr == NULL || *endptr != '\0')
497 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
498 SpecName, Line, token);
503 odp->u.ret.ret_value = strtol(token, &endptr, 0);
504 if (endptr == NULL || *endptr != '\0')
506 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
507 SpecName, Line, token);
511 if (SpecType == SPEC_WIN32)
513 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
522 /*******************************************************************
525 * Parse a 'stub' definition.
527 static int ParseStub( ORDDEF *odp )
529 odp->u.func.arg_types[0] = '\0';
530 strcpy( odp->u.func.link_name, STUB_CALLBACK );
535 /*******************************************************************
538 * Parse an 'varargs' definition.
540 static int ParseVarargs( ORDDEF *odp )
544 if (SpecType == SPEC_WIN16)
546 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
554 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
555 SpecName, Line, token);
561 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
562 SpecName, Line, token);
566 strcpy( odp->u.vargs.link_name, GetToken() );
571 /*******************************************************************
574 * Parse an 'extern' definition.
576 static int ParseExtern( ORDDEF *odp )
578 if (SpecType == SPEC_WIN16)
580 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
584 strcpy( odp->u.ext.link_name, GetToken() );
589 /*******************************************************************
592 * Parse an ordinal definition.
594 static int ParseOrdinal(int ordinal)
599 if (ordinal >= MAX_ORDINALS)
601 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
604 if (ordinal > Limit) Limit = ordinal;
605 if (ordinal < Base) Base = ordinal;
607 odp = &OrdinalDefinitions[ordinal];
608 if (!(token = GetToken()))
610 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
614 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
615 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
618 if (odp->type >= TYPE_NBTYPES)
621 "%s:%d: Expected type after ordinal, found '%s' instead\n",
622 SpecName, Line, token );
626 if (!(token = GetToken()))
628 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
631 strcpy( odp->name, token );
639 return ParseVariable( odp );
645 return ParseExportFunction( odp );
647 return ParseEquate( odp );
649 return ParseReturn( odp );
651 return ParseStub( odp );
653 return ParseVarargs( odp );
655 return ParseExtern( odp );
657 fprintf( stderr, "Should not happen\n" );
663 /*******************************************************************
668 static int ParseTopLevel(void)
672 while ((token = GetToken()) != NULL)
674 if (strcmp(token, "name") == 0)
676 strcpy(DLLName, GetToken());
678 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
680 else if (strcmp(token, "file") == 0)
682 strcpy(DLLFileName, GetToken());
683 strupper(DLLFileName);
685 else if (strcmp(token, "type") == 0)
688 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
689 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
692 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
697 else if (strcmp(token, "heap") == 0)
700 if (!IsNumberString(token))
702 fprintf(stderr, "%s:%d: Expected number after heap\n",
706 DLLHeapSize = atoi(token);
708 else if (IsNumberString(token))
713 ordinal = atoi(token);
714 if ((rv = ParseOrdinal(ordinal)) < 0)
720 "%s:%d: Expected name, id, length or ordinal\n",
730 /*******************************************************************
733 * Store a list of ints into a byte array.
735 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
742 for (i = 0; i < odp->u.var.n_values; i++)
743 buffer[i] = odp->u.var.values[i];
746 for (i = 0; i < odp->u.var.n_values; i++)
747 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
750 for (i = 0; i < odp->u.var.n_values; i++)
751 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
754 return odp->u.var.n_values * size;
758 /*******************************************************************
761 * Dump a byte stream into the assembly code.
763 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
764 const char *section, const char *label_start )
767 if (section) fprintf( outfile, "\t%s\n", section );
768 if (label_start) fprintf( outfile, "%s:\n", label_start );
769 for (i = 0; i < len; i++)
771 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
772 fprintf( outfile, "%d", *data++ );
774 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
776 fprintf( outfile, "\n" );
780 /*******************************************************************
783 * Build the in-memory representation of a 16-bit NE module, and dump it
784 * as a byte stream into the assembly code.
786 static int BuildModule16( FILE *outfile, int max_code_offset,
787 int max_data_offset )
793 SEGTABLEENTRY *pSegment;
800 * OFSTRUCT File information
801 * SEGTABLEENTRY Segment 1 (code)
802 * SEGTABLEENTRY Segment 2 (data)
803 * WORD[2] Resource table (empty)
804 * BYTE[2] Imported names (empty)
805 * BYTE[n] Resident names table
806 * BYTE[n] Entry table
809 buffer = xmalloc( 0x10000 );
811 pModule = (NE_MODULE *)buffer;
812 pModule->magic = IMAGE_OS2_SIGNATURE;
815 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
817 pModule->heap_size = DLLHeapSize;
818 pModule->stack_size = 0;
823 pModule->seg_count = 2;
824 pModule->modref_count = 0;
825 pModule->nrname_size = 0;
826 pModule->modref_table = 0;
827 pModule->nrname_fpos = 0;
828 pModule->moveable_entries = 0;
829 pModule->alignment = 0;
830 pModule->truetype = 0;
831 pModule->os_flags = NE_OSFLAGS_WINDOWS;
832 pModule->misc_flags = 0;
833 pModule->dlls_to_init = 0;
834 pModule->nrname_handle = 0;
835 pModule->min_swap_area = 0;
836 pModule->expected_version = 0x030a;
837 pModule->module32 = 0;
839 pModule->self_loading_sel = 0;
841 /* File information */
843 pFileInfo = (OFSTRUCT *)(pModule + 1);
844 pModule->fileinfo = (int)pFileInfo - (int)pModule;
845 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
846 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
847 + strlen(DLLFileName);
848 strcpy( pFileInfo->szPathName, DLLFileName );
849 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
853 pSegment = (SEGTABLEENTRY *)pstr;
854 pModule->seg_table = (int)pSegment - (int)pModule;
855 pSegment->filepos = 0;
856 pSegment->size = max_code_offset;
858 pSegment->minsize = max_code_offset;
859 pSegment->selector = 0;
862 pModule->dgroup_entry = (int)pSegment - (int)pModule;
863 pSegment->filepos = 0;
864 pSegment->size = max_data_offset;
865 pSegment->flags = NE_SEGFLAGS_DATA;
866 pSegment->minsize = max_data_offset;
867 pSegment->selector = 0;
872 pword = (WORD *)pSegment;
873 pModule->res_table = (int)pword - (int)pModule;
877 /* Imported names table */
879 pstr = (char *)pword;
880 pModule->import_table = (int)pstr - (int)pModule;
884 /* Resident names table */
886 pModule->name_table = (int)pstr - (int)pModule;
887 /* First entry is module name */
888 *pstr = strlen(DLLName );
889 strcpy( pstr + 1, DLLName );
892 pstr += sizeof(WORD);
893 /* Store all ordinals */
894 odp = OrdinalDefinitions + 1;
895 for (i = 1; i <= Limit; i++, odp++)
897 if (!odp->name[0]) continue;
898 *pstr = strlen( odp->name );
899 strcpy( pstr + 1, odp->name );
900 strupper( pstr + 1 );
903 pstr += sizeof(WORD);
909 pModule->entry_table = (int)pstr - (int)pModule;
911 odp = OrdinalDefinitions + 1;
912 for (i = 1; i <= Limit; i++, odp++)
924 selector = 1; /* Code selector */
930 selector = 2; /* Data selector */
934 selector = 0xfe; /* Constant selector */
938 selector = 0; /* Invalid selector */
942 /* create a new bundle if necessary */
943 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
947 bundle[1] = selector;
955 *(WORD *)pstr = odp->offset;
956 pstr += sizeof(WORD);
961 /* Dump the module content */
963 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
964 ".data", "Module_Start" );
965 return (int)pstr - (int)pModule;
969 /*******************************************************************
972 * Build a Win32 C file from a spec file.
974 static int BuildSpec32File( char * specfile, FILE *outfile )
979 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
981 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
983 /* Output code for all stubs functions */
985 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
987 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
989 if (odp->type != TYPE_STUB) continue;
990 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
994 /* Output code for all register functions */
996 fprintf( outfile, "#ifdef __i386__\n" );
997 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
999 if (odp->type != TYPE_REGISTER) continue;
1001 "__asm__(\".align 4\\n\\t\"\n"
1002 " \".globl " PREFIX "%s\\n\\t\"\n"
1003 " \".type " PREFIX "%s,@function\\n\\t\"\n"
1004 " \"" PREFIX "%s:\\n\\t\"\n"
1005 " \"pushl $" PREFIX "__regs_%s\\n\\t\"\n"
1006 " \"pushl $" PREFIX "CALL32_Regs\\n\\t\"\n"
1008 odp->u.func.link_name, odp->u.func.link_name,
1009 odp->u.func.link_name, odp->u.func.link_name );
1011 fprintf( outfile, "#endif\n" );
1013 /* Output the DLL functions prototypes */
1015 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1020 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
1023 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1028 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1034 fprintf(stderr,"build: function type %d not available for Win32\n",
1040 /* Output the DLL functions table */
1042 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1044 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1049 fprintf( outfile, " 0" );
1052 fprintf( outfile, " %s", odp->u.vargs.link_name );
1055 fprintf( outfile, " %s", odp->u.ext.link_name );
1060 fprintf( outfile, " %s", odp->u.func.link_name);
1063 fprintf( outfile, " __stub_%d", i );
1068 if (i < Limit) fprintf( outfile, ",\n" );
1070 fprintf( outfile, "\n};\n\n" );
1072 /* Output the DLL names table */
1075 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1076 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1078 if (odp->type == TYPE_INVALID) continue;
1079 if (nb_names++) fprintf( outfile, ",\n" );
1080 fprintf( outfile, " \"%s\"", odp->name );
1082 fprintf( outfile, "\n};\n\n" );
1084 /* Output the DLL argument types */
1086 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1088 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1090 unsigned int j, mask = 0;
1091 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1092 for (j = 0; odp->u.func.arg_types[j]; j++)
1094 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1095 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1097 fprintf( outfile, " %d", mask );
1098 if (i < Limit) fprintf( outfile, ",\n" );
1100 fprintf( outfile, "\n};\n\n" );
1102 /* Output the DLL ordinals table */
1104 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1106 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1108 if (odp->type == TYPE_INVALID) continue;
1109 if (nb_names++) fprintf( outfile, ",\n" );
1110 fprintf( outfile, " %d", i - Base );
1112 fprintf( outfile, "\n};\n\n" );
1114 /* Output the DLL functions arguments */
1116 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1118 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1124 args = (unsigned char)strlen(odp->u.func.arg_types);
1127 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1136 fprintf( outfile, " 0x%02x", args );
1137 if (i < Limit) fprintf( outfile, ",\n" );
1139 fprintf( outfile, "\n};\n\n" );
1141 /* Output the DLL descriptor */
1143 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1145 fprintf( outfile, " \"%s\",\n", DLLName );
1146 fprintf( outfile, " %d,\n", Base );
1147 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1148 fprintf( outfile, " %d,\n", nb_names );
1160 /*******************************************************************
1163 * Build a Win16 assembly file from a spec file.
1165 static int BuildSpec16File( char * specfile, FILE *outfile )
1169 int code_offset, data_offset, module_size;
1170 unsigned char *data;
1172 data = (unsigned char *)xmalloc( 0x10000 );
1173 memset( data, 0, 16 );
1176 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1177 fprintf( outfile, "\t.text\n" );
1178 fprintf( outfile, "Code_Start:\n" );
1181 odp = OrdinalDefinitions;
1182 for (i = 0; i <= Limit; i++, odp++)
1187 odp->offset = 0xffff;
1191 odp->offset = LOWORD(odp->u.abs.value);
1195 odp->offset = data_offset;
1196 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1200 odp->offset = data_offset;
1201 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1205 odp->offset = data_offset;
1206 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1210 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1211 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1212 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1213 fprintf( outfile,"\t.byte 0x66\n");
1214 if (odp->u.ret.arg_size != 0)
1215 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1218 fprintf( outfile, "\tlret\n");
1219 fprintf( outfile, "\tnop\n");
1220 fprintf( outfile, "\tnop\n\n");
1222 odp->offset = code_offset;
1223 code_offset += 12; /* Assembly code is 12 bytes long */
1229 case TYPE_PASCAL_16:
1231 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1232 fprintf( outfile, "\tpushw %%bp\n" );
1233 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1234 /* FreeBSD does not understand lcall, so do it the hard way */
1235 fprintf( outfile, "\t.byte 0x9a\n" );
1236 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s_%s\n",
1237 (odp->type == TYPE_CDECL) ? "c" : "p",
1238 (odp->type == TYPE_REGISTER) ? "regs" :
1239 (odp->type == TYPE_PASCAL_16) ? "word" : "long",
1240 odp->u.func.arg_types );
1241 fprintf( outfile, "\t.long 0x%08lx\n",
1242 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1243 odp->offset = code_offset;
1244 code_offset += 16; /* Assembly code is 16 bytes long */
1248 fprintf(stderr,"build: function type %d not available for Win16\n",
1254 if (!code_offset) /* Make sure the code segment is not empty */
1256 fprintf( outfile, "\t.byte 0\n" );
1260 /* Output data segment */
1262 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1264 /* Build the module */
1266 module_size = BuildModule16( outfile, code_offset, data_offset );
1268 /* Output the DLL descriptor */
1270 fprintf( outfile, "\t.text\n" );
1271 fprintf( outfile, "DLLName:\t" STRING " \"%s\\0\"\n", DLLName );
1272 fprintf( outfile, "\t.align 4\n" );
1273 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1274 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1275 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1276 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1277 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1278 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1279 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1284 /*******************************************************************
1287 * Build an assembly file from a spec file.
1289 static int BuildSpecFile( FILE *outfile, char *specname )
1291 SpecName = specname;
1292 SpecFp = fopen( specname, "r");
1295 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1299 if (ParseTopLevel() < 0) return -1;
1304 return BuildSpec16File( specname, outfile );
1306 return BuildSpec32File( specname, outfile );
1308 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1314 /*******************************************************************
1315 * TransferArgs16To32
1317 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1318 * The 16-bit stack layout is:
1326 * For 'cdecl' argn up to arg1 are reversed.
1328 static int TransferArgs16To32( FILE *outfile, char *args, int usecdecl )
1330 int i, pos16, pos32;
1333 /* Copy the arguments */
1335 pos16 = 6; /* skip bp and return address */
1336 pos32 = usecdecl ? -(strlen(args) * 4) : 0;
1337 xargs = usecdecl ? args:args+strlen(args);
1339 for (i = strlen(args); i > 0; i--)
1347 case 'w': /* word */
1348 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1349 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1353 case 's': /* s_word */
1354 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1355 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1359 case 'l': /* long or segmented pointer */
1360 case 'T': /* segmented pointer to null-terminated string */
1361 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1362 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1366 case 'p': /* linear pointer */
1367 case 't': /* linear pointer to null-terminated string */
1368 /* Get the selector */
1369 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1370 /* Get the selector base */
1371 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1372 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1373 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1374 /* Add the offset */
1375 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1376 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1381 fprintf( stderr, "Unknown arg type '%c'\n", *xargs );
1389 return pos16 - 6; /* Return the size of the 16-bit args */
1393 /*******************************************************************
1396 * Build the context structure on the 32-bit stack.
1398 static void BuildContext16( FILE *outfile )
1400 /* Store the registers */
1402 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1403 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1404 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1405 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1406 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1407 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1408 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1409 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1410 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1411 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1413 fprintf( outfile, "\tmovl -20(%%ebp),%%eax\n" ); /* Get %ebx from stack*/
1414 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1415 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1416 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1417 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1418 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1419 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1420 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1421 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1422 fprintf( outfile, "\tpushfl\n" );
1423 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1424 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1425 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1426 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1427 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1428 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1429 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1430 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1431 fprintf( outfile, "\tleal 2(%%ebp),%%eax\n" ); /* Get initial %sp */
1432 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1433 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1434 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1435 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1436 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1437 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1438 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1439 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1440 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1441 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1442 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1443 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1444 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1445 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1447 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1448 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1453 /*******************************************************************
1456 * Restore the registers from the context structure.
1458 static void RestoreContext16( FILE *outfile )
1460 /* Get the 32-bit stack pointer */
1462 fprintf( outfile, "\tleal -%d(%%ebp),%%ebx\n",
1463 STRUCTOFFSET(STACK32FRAME,ebp) );
1465 /* Remove everything up to (including) the return address
1466 * from the 16-bit stack */
1468 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1469 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1470 fprintf( outfile, "\tmovw %%ax,%%ss\n" );
1471 fprintf( outfile, "\tmovl %d(%%ebx),%%esp\n",
1472 CONTEXTOFFSET(Esp) - sizeof(CONTEXT) );
1473 fprintf( outfile, "\taddl $4,%%esp\n" ); /* Remove return address */
1475 /* Restore the registers */
1477 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1478 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1479 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1480 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1481 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1482 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1483 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1484 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1485 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1486 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1487 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1488 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1489 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1490 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1491 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1492 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1493 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1494 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1495 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1496 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1497 fprintf( outfile, "\tpopfl\n" );
1498 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1499 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1500 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1501 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1502 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1503 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1507 /*******************************************************************
1508 * BuildCallFrom16Func
1510 * Build a 16-bit-to-Wine callback function. The syntax of the function
1511 * profile is: call_type_xxxxx, where 'call' is the letter 'c' or 'p' for C or
1512 * Pascal calling convention, 'type' is one of 'regs', 'word' or
1513 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1514 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1515 * 'T'=segmented pointer to null-terminated string).
1516 * For register functions, the arguments are ignored, but they are still
1517 * removed from the stack upon return.
1519 * A special variant of the callback function is generated by the function
1520 * profile "t_long_". This is used by the Win95 16->32 thunk
1521 * functions C16ThkSL and C16ThkSL01 and is implemented as follows:
1522 * On entry, the EBX register is set up to contain a flat pointer to the
1523 * 16-bit stack such that EBX+22 points to the first argument.
1524 * Then, the entry point is called, while EBP is set up to point
1525 * to the return address (on the 32-bit stack).
1526 * The called function returns with CX set to the number of bytes
1527 * to be popped of the caller's stack.
1529 * Stack layout upon entry to the callback function:
1531 * (sp+18) word first 16-bit arg
1535 * (sp+8) long 32-bit entry point (used to store edx)
1536 * (sp+6) word high word of cs (always 0, used to store es)
1537 * (sp+4) word low word of cs of 16-bit entry point
1538 * (sp+2) word high word of ip (always 0, used to store ds)
1539 * (sp) word low word of ip of 16-bit entry point
1541 * Added on the stack:
1543 * (sp-8) long saved previous stack
1545 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1552 char *args = profile + 7;
1554 /* Parse function type */
1556 if (!strncmp( "c_", profile, 2 )) cdecl = 1;
1557 else if (!strncmp( "t_", profile, 2 )) thunk = 1;
1558 else if (strncmp( "p_", profile, 2 ))
1560 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1564 if (!strncmp( "word_", profile + 2, 5 )) short_ret = 1;
1565 else if (!strncmp( "regs_", profile + 2, 5 )) reg_func = 1;
1566 else if (strncmp( "long_", profile + 2, 5 ))
1568 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1572 /* Function header */
1574 fprintf( outfile, "\n\t.align 4\n" );
1576 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1579 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1580 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1582 /* Setup bp to point to its copy on the stack */
1584 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1585 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1586 fprintf( outfile, "\taddw $16,%%bp\n" );
1588 /* Save 16-bit ds and es */
1590 /* Stupid FreeBSD assembler doesn't know these either */
1591 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1592 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1593 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1594 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1598 fprintf( outfile, "\tpushl %%ebx\n" );
1600 /* Restore 32-bit segment registers */
1602 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1604 fprintf( outfile, "\tdata16\n");
1606 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1608 fprintf( outfile, "\tdata16\n");
1610 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1612 fprintf( outfile, "\tmovw %%fs, %%bx\n" );
1613 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
1614 fprintf( outfile, "\t.byte 0x64\n\tmovw %%bx,(%d)\n", FSOFFSET );
1616 /* Get the 32-bit stack pointer from the TEB */
1618 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%ebx\n", STACKOFFSET );
1620 /* Save the 16-bit stack */
1623 fprintf( outfile,"\tdata16\n");
1625 fprintf( outfile, "\t.byte 0x64\n\tmovw %%ss,(%d)\n", STACKOFFSET + 2 );
1626 fprintf( outfile, "\t.byte 0x64\n\tmovw %%sp,(%d)\n", STACKOFFSET );
1628 /* Transfer the arguments */
1630 if (reg_func) BuildContext16( outfile );
1631 else if (*args) argsize = TransferArgs16To32( outfile, args, cdecl );
1634 /* Get the stack selector base */
1635 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1636 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1637 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1638 fprintf( outfile, "\tmovl %%eax,-20(%%ebp)\n" );
1639 /* Add the offset */
1640 fprintf( outfile, "\tleal -16(%%ebp),%%eax\n" );
1641 fprintf( outfile, "\taddl %%eax,-20(%%ebp)\n" );
1644 /* Get the address of the API function */
1646 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1648 /* If necessary, save %edx over the API function address */
1650 if (!reg_func && short_ret)
1651 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1653 /* Restore %ebx and store the 32-bit stack pointer instead */
1655 fprintf( outfile, "\tmovl %%ebx,%%ebp\n" );
1656 fprintf( outfile, "\tpopl %%ebx\n" );
1657 fprintf( outfile, "\tpushl %%ebp\n" );
1659 /* Switch to the 32-bit stack */
1661 fprintf( outfile, "\tpushl %%ds\n" );
1662 fprintf( outfile, "\tpopl %%ss\n" );
1663 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1664 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1665 if (reg_func) /* Push the address of the context struct */
1666 fprintf( outfile, "\tpushl %%esp\n" );
1668 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1670 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1672 /* Print the debug information before the call */
1674 if (debugging && !thunk)
1678 if (cdecl) ftype |= 4;
1679 if (reg_func) ftype |= 2;
1680 if (short_ret) ftype |= 1;
1682 fprintf( outfile, "\tpushl %%eax\n" );
1683 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1684 fprintf( outfile, "\tpushl $%d\n", ftype );
1685 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1686 fprintf( outfile, "\tpopl %%eax\n" );
1687 fprintf( outfile, "\tpopl %%eax\n" );
1688 fprintf( outfile, "\tpopl %%eax\n" );
1691 /* Call the entry point */
1695 fprintf( outfile, "\tpushl %%ebp\n" );
1696 fprintf( outfile, "\tleal -4(%%esp), %%ebp\n" );
1697 fprintf( outfile, "\tcall *%%eax\n" );
1698 fprintf( outfile, "\tpopl %%ebp\n" );
1701 fprintf( outfile, "\tcall *%%eax\n" );
1704 /* Print the debug information after the call */
1706 if (debugging && !thunk)
1710 /* Push again the address of the context struct in case */
1711 /* it has been removed by an stdcall function */
1712 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1713 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1714 fprintf( outfile, "\tpushl %%esp\n" );
1716 fprintf( outfile, "\tpushl %%eax\n" );
1717 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1718 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1719 fprintf( outfile, "\tpopl %%eax\n" );
1720 fprintf( outfile, "\tpopl %%eax\n" );
1723 /* Restore the 16-bit stack */
1726 fprintf( outfile, "\tdata16\n");
1728 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2 );
1729 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1730 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
1731 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1733 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%bx\n", FSOFFSET );
1734 fprintf( outfile, "\tmovw %%bx, %%fs\n" );
1738 /* Calc the arguments size */
1754 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1759 /* Restore registers from the context structure */
1760 RestoreContext16( outfile );
1764 /* Restore high 16 bits of ebp */
1765 fprintf( outfile, "\tpopl %%ebp\n" );
1767 /* Restore ds and es */
1768 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1769 fprintf( outfile, "\tincl %%esp\n" );
1770 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1771 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1772 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1774 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1777 /* Get the return value into dx:ax */
1778 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1779 fprintf( outfile, "\tshrl $16,%%edx\n" );
1780 /* Remove API entry point */
1781 fprintf( outfile, "\taddl $4,%%esp\n" );
1784 /* Restore low 16 bits of ebp */
1785 fprintf( outfile, "\tpopw %%bp\n" );
1788 /* Remove the arguments and return */
1792 fprintf( outfile, "\tpopl %%ebx\n" );
1793 fprintf( outfile, "\txorb %%ch,%%ch\n" );
1794 fprintf( outfile, "\taddw %%cx, %%sp\n" );
1795 fprintf( outfile, "\tpushl %%ebx\n" );
1796 fprintf( outfile, "\t.byte 0x66\n" );
1797 fprintf( outfile, "\tlret\n" );
1799 else if (argsize && !cdecl)
1801 fprintf( outfile, "\t.byte 0x66\n" );
1802 fprintf( outfile, "\tlret $%d\n", argsize );
1806 fprintf( outfile, "\t.byte 0x66\n" );
1807 fprintf( outfile, "\tlret\n" );
1812 /*******************************************************************
1815 * Build a Wine-to-16-bit callback function.
1817 * Stack frame of the callback function:
1821 * (ebp+8) func to call
1822 * (ebp+4) return address
1823 * (ebp) previous ebp
1825 * Prototypes for the CallTo16 functions:
1826 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1827 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1828 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context, int nb_args );
1829 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context, int nb_args );
1831 static void BuildCallTo16Func( FILE *outfile, char *profile )
1835 char *args = profile + 5;
1837 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1838 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1839 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1840 else if (strncmp( "long_", profile, 5 ))
1842 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1846 /* Function header */
1848 fprintf( outfile, "\n\t.align 4\n" );
1850 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1853 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1854 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1858 fprintf( outfile, "\tpushl %%ebp\n" );
1859 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1861 /* Call the actual CallTo16 routine (simulate a lcall) */
1863 fprintf( outfile, "\tpushl %%cs\n" );
1864 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1868 /* FIXME: this is a hack because of task.c */
1869 if (!strcmp( profile, "word_" ))
1871 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1872 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1874 fprintf( outfile, "\tpopl %%ebp\n" );
1875 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1877 /* Start of the actual CallTo16 routine */
1879 /* Save the 32-bit registers */
1881 fprintf( outfile, "do_callto16_%s:\n", profile );
1882 fprintf( outfile, "\tpushl %%ebx\n" );
1883 fprintf( outfile, "\tpushl %%ecx\n" );
1884 fprintf( outfile, "\tpushl %%edx\n" );
1885 fprintf( outfile, "\tpushl %%esi\n" );
1886 fprintf( outfile, "\tpushl %%edi\n" );
1888 /* Print debugging info */
1892 /* Push the address of the first argument */
1893 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1894 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1895 fprintf( outfile, "\tpushl %%eax\n" );
1896 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1897 fprintf( outfile, "\tpopl %%eax\n" );
1898 fprintf( outfile, "\tpopl %%eax\n" );
1901 /* Save the 32-bit stack and %fs */
1903 fprintf( outfile, "\t.byte 0x64\n\tpushl (%d)\n", STACKOFFSET );
1904 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1905 fprintf( outfile, "\tmovl %%esp,%%edx\n" );
1906 fprintf( outfile, "\tmovw %%fs," PREFIX "CALLTO16_Current_fs\n" );
1910 /* Switch to the 16-bit stack, saving the current %%esp, */
1911 /* and adding the specified offset to the new sp */
1912 fprintf( outfile, "\t.byte 0x64\n\tmovzwl (%d),%%eax\n", STACKOFFSET );
1913 fprintf( outfile, "\tsubl 12(%%ebx),%%eax\n" ); /* Get the offset */
1915 fprintf( outfile,"\tdata16\n");
1917 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1918 fprintf( outfile, "\tmovl %%eax,%%esp\n" );
1919 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1921 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%cx\n", FSOFFSET );
1922 fprintf( outfile, "\tmovw %%cx, %%fs\n" );
1924 /* Get the registers. ebx is handled later on. */
1926 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1927 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1928 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1929 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1930 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1931 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1932 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1933 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1934 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1936 /* Push the return address
1937 * With sreg suffix, we push 16:16 address (normal lret)
1938 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1941 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_long\n" );
1944 fprintf( outfile, "\tpushw $0\n" );
1945 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax+2\n" );
1946 fprintf( outfile, "\tpushw $0\n" );
1947 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_eax\n" );
1950 /* Push the called routine address */
1952 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1953 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1955 /* Get the 16-bit ds */
1957 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1958 /* Get ebx from the 32-bit stack */
1959 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1960 fprintf( outfile, "\tpopl %%ds\n" );
1962 else /* not a register function */
1964 int pos = 12; /* first argument position */
1966 /* Switch to the 16-bit stack */
1968 fprintf( outfile,"\tdata16\n");
1970 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%ss\n", STACKOFFSET + 2);
1971 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%sp\n", STACKOFFSET );
1972 fprintf( outfile, "\t.byte 0x64\n\tmovl %%edx,(%d)\n", STACKOFFSET );
1974 fprintf( outfile, "\t.byte 0x64\n\tmovw (%d),%%cx\n", FSOFFSET );
1975 fprintf( outfile, "\tmovw %%cx, %%fs\n" );
1977 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1978 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1979 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1980 STRUCTOFFSET(STACK16FRAME,bp) );
1982 /* Transfer the arguments */
1988 case 'w': /* word */
1989 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1991 case 'l': /* long */
1992 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1995 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
2001 /* Push the return address */
2003 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
2004 short_ret ? "word" : "long" );
2006 /* Push the called routine address */
2008 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
2010 /* Set %ds and %es (and %ax just in case) equal to %ss */
2012 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
2013 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2014 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2017 /* Jump to the called routine */
2019 fprintf( outfile, "\t.byte 0x66\n" );
2020 fprintf( outfile, "\tlret\n" );
2024 /*******************************************************************
2027 * Build the return code for 16-bit callbacks
2029 static void BuildRet16Func( FILE *outfile )
2031 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
2032 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
2033 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_eax\n" );
2035 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
2036 fprintf( outfile, "\txorl %%edx,%%edx\n" );
2038 /* Put return value into %eax */
2040 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
2041 fprintf( outfile, "\tshll $16,%%edx\n" );
2042 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
2043 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
2044 fprintf( outfile, PREFIX "CALLTO16_Ret_eax:\n" );
2046 /* Restore 32-bit segment registers */
2048 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
2050 fprintf( outfile, "\tdata16\n");
2052 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
2054 fprintf( outfile, "\tdata16\n");
2056 fprintf( outfile, "\tmovw %%bx,%%es\n" );
2058 fprintf( outfile, "\tmovw %%fs, %%cx\n" );
2059 fprintf( outfile, "\tmovw " PREFIX "CALLTO16_Current_fs,%%fs\n" );
2060 fprintf( outfile, "\t.byte 0x64\n\tmovw %%cx,(%d)\n", FSOFFSET );
2062 /* Restore the 32-bit stack */
2065 fprintf( outfile, "\tdata16\n");
2067 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
2068 fprintf( outfile, "\t.byte 0x64\n\tmovl (%d),%%esp\n", STACKOFFSET );
2069 fprintf( outfile, "\t.byte 0x64\n\tpopl (%d)\n", STACKOFFSET );
2071 /* Restore the 32-bit registers */
2073 fprintf( outfile, "\tpopl %%edi\n" );
2074 fprintf( outfile, "\tpopl %%esi\n" );
2075 fprintf( outfile, "\tpopl %%edx\n" );
2076 fprintf( outfile, "\tpopl %%ecx\n" );
2077 fprintf( outfile, "\tpopl %%ebx\n" );
2079 /* Return to caller */
2081 fprintf( outfile, "\tlret\n" );
2083 /* Declare the return address variables */
2085 fprintf( outfile, "\t.data\n" );
2086 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
2087 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
2088 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_eax\n" );
2089 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
2090 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
2091 fprintf( outfile, PREFIX "CALLTO16_RetAddr_eax:\t.long 0\n" );
2092 fprintf( outfile, "\t.text\n" );
2096 /*******************************************************************
2097 * BuildCallTo32LargeStack
2099 * Build the function used to switch to the original 32-bit stack
2100 * before calling a 32-bit function from 32-bit code. This is used for
2101 * functions that need a large stack, like X bitmaps functions.
2103 * The generated function has the following prototype:
2104 * int xxx( int (*func)(), void *arg );
2106 * The pointer to the function can be retrieved by calling CALL32_Init,
2107 * which also takes care of saving the current 32-bit stack pointer.
2116 static void BuildCallTo32LargeStack( FILE *outfile )
2118 /* Initialization function */
2120 fprintf( outfile, "\n\t.align 4\n" );
2122 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2124 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2125 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2126 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2127 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2128 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2129 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2130 fprintf( outfile, "\tret\n" );
2132 /* Function header */
2134 fprintf( outfile, "\n\t.align 4\n" );
2136 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2138 fprintf( outfile, "CALL32_LargeStack:\n" );
2142 fprintf( outfile, "\tpushl %%ebp\n" );
2143 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2145 /* Switch to the original 32-bit stack pointer */
2147 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2149 /* Transfer the argument and call the function */
2151 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2152 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2154 /* Restore registers and return */
2156 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2157 fprintf( outfile, "\tpopl %%ebp\n" );
2158 fprintf( outfile, "\tret\n" );
2162 fprintf( outfile, "\t.data\n" );
2163 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2164 fprintf( outfile, "\t.text\n" );
2168 /*******************************************************************
2169 * BuildCallFrom32Regs
2171 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2172 * 'args' is the number of dword arguments.
2176 * (esp+336) ret addr (or relay addr when debugging(relay) is on)
2177 * (esp+332) entry point
2178 * (esp+204) buffer area to allow stack frame manipulation
2179 * (esp+0) CONTEXT struct
2181 static void BuildCallFrom32Regs( FILE *outfile )
2183 #define STACK_SPACE 128
2185 /* Function header */
2187 fprintf( outfile, "\n\t.align 4\n" );
2189 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2191 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2192 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2194 /* Allocate some buffer space on the stack */
2196 fprintf( outfile, "\tleal -%d(%%esp), %%esp\n", STACK_SPACE );
2198 /* Build the context structure */
2200 fprintf( outfile, "\tpushw $0\n" );
2201 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2202 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2203 fprintf( outfile, "\tpushfl\n" );
2204 fprintf( outfile, "\tpushw $0\n" );
2205 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2206 fprintf( outfile, "\tpushl %d(%%esp)\n", 16+STACK_SPACE+4 ); /* %eip at time of call */
2207 fprintf( outfile, "\tpushl %%ebp\n" );
2209 fprintf( outfile, "\tpushl %%eax\n" );
2210 fprintf( outfile, "\tpushl %%ecx\n" );
2211 fprintf( outfile, "\tpushl %%edx\n" );
2212 fprintf( outfile, "\tpushl %%ebx\n" );
2213 fprintf( outfile, "\tpushl %%esi\n" );
2214 fprintf( outfile, "\tpushl %%edi\n" );
2216 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2217 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2218 fprintf( outfile, "\tpushl %%eax\n" );
2219 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2220 fprintf( outfile, "\tpushl %%eax\n" );
2221 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2222 fprintf( outfile, "\tpushl %%eax\n" );
2223 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2224 fprintf( outfile, "\tpushl %%eax\n" );
2226 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2227 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2228 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2230 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2232 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2233 sizeof(CONTEXT) + STACK_SPACE + 4 ); /* %esp at time of call */
2234 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2236 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2238 /* Restore the context structure */
2240 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2242 /* Store %eip value onto the new stack */
2244 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2245 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2246 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2248 /* Restore all registers */
2250 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2251 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2252 fprintf( outfile, "\tpopl %%eax\n" );
2253 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2254 fprintf( outfile, "\tpopl %%eax\n" );
2255 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2256 fprintf( outfile, "\tpopl %%eax\n" );
2257 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2258 fprintf( outfile, "\tpopl %%eax\n" );
2259 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2261 fprintf( outfile, "\tpopl %%edi\n" );
2262 fprintf( outfile, "\tpopl %%esi\n" );
2263 fprintf( outfile, "\tpopl %%ebx\n" );
2264 fprintf( outfile, "\tpopl %%edx\n" );
2265 fprintf( outfile, "\tpopl %%ecx\n" );
2266 fprintf( outfile, "\tpopl %%eax\n" );
2267 fprintf( outfile, "\tpopl %%ebp\n" );
2268 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2269 fprintf( outfile, "\tpopfl\n" );
2270 fprintf( outfile, "\tpopl %%esp\n" );
2271 fprintf( outfile, "\tret\n" );
2277 /*******************************************************************
2280 * Build the spec files
2282 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2285 for (i = 2; i < argc; i++)
2286 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2291 /*******************************************************************
2294 * Build the 16-bit-to-Wine callbacks
2296 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2303 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2304 fprintf( outfile, "\t.text\n" );
2307 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2308 getcwd(buffer, sizeof(buffer));
2311 * The stabs help the internal debugger as they are an indication that it
2312 * is sensible to step into a thunk/trampoline.
2314 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2315 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2316 fprintf( outfile, "\t.text\n" );
2317 fprintf( outfile, "\t.align 4\n" );
2318 fprintf( outfile, "Code_Start:\n\n" );
2321 /* Build the callback functions */
2323 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2325 /* Build the thunk callback function */
2327 BuildCallFrom16Func( outfile, "t_long_" );
2329 /* Output the argument debugging strings */
2333 fprintf( outfile, "/* Argument strings */\n" );
2334 for (i = 2; i < argc; i++)
2336 fprintf( outfile, "Profile_%s:\t", argv[i] );
2337 fprintf( outfile, STRING " \"%s\\0\"\n", argv[i] + 7 );
2342 fprintf( outfile, "\t.text\n");
2343 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2344 fprintf( outfile, ".Letext:\n");
2351 /*******************************************************************
2354 * Build the Wine-to-16-bit callbacks
2356 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2363 infile = fopen( argv[2], "r" );
2370 else infile = stdin;
2374 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2375 fprintf( outfile, "\t.text\n" );
2378 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2379 getcwd(buffer, sizeof(buffer));
2382 * The stabs help the internal debugger as they are an indication that it
2383 * is sensible to step into a thunk/trampoline.
2385 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2386 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2387 fprintf( outfile, "\t.text\n" );
2388 fprintf( outfile, "\t.align 4\n" );
2389 fprintf( outfile, "Code_Start:\n\n" );
2392 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2393 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2395 /* Build the callback functions */
2397 while (fgets( buffer, sizeof(buffer), infile ))
2399 if (strstr( buffer, "### start build ###" )) break;
2401 while (fgets( buffer, sizeof(buffer), infile ))
2403 char *p = strstr( buffer, "CallTo16_" );
2406 char *profile = p + strlen( "CallTo16_" );
2408 while ((*p == '_') || isalpha(*p)) p++;
2410 BuildCallTo16Func( outfile, profile );
2412 if (strstr( buffer, "### stop build ###" )) break;
2415 /* Output the 16-bit return code */
2417 BuildRet16Func( outfile );
2419 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2420 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2423 fprintf( outfile, "\t.text\n");
2424 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2425 fprintf( outfile, ".Letext:\n");
2433 /*******************************************************************
2436 * Build the 32-bit callbacks
2438 static int BuildCall32( FILE *outfile, char * outname )
2444 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2445 fprintf( outfile, "\t.text\n" );
2450 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2451 getcwd(buffer, sizeof(buffer));
2454 * The stabs help the internal debugger as they are an indication that it
2455 * is sensible to step into a thunk/trampoline.
2457 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2458 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2459 fprintf( outfile, "\t.text\n" );
2460 fprintf( outfile, "\t.align 4\n" );
2461 fprintf( outfile, "Code_Start:\n" );
2464 /* Build the 32-bit large stack callback */
2466 BuildCallTo32LargeStack( outfile );
2468 /* Build the register callback function */
2470 BuildCallFrom32Regs( outfile );
2473 fprintf( outfile, "\t.text\n");
2474 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2475 fprintf( outfile, ".Letext:\n");
2478 #else /* __i386__ */
2480 /* Just to avoid an empty file */
2481 fprintf( outfile, "\t.long 0\n" );
2483 #endif /* __i386__ */
2488 /*******************************************************************
2491 static void usage(void)
2494 "usage: build [-o outfile] -spec SPECNAMES\n"
2495 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2496 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2497 " build [-o outfile] -call32\n" );
2502 /*******************************************************************
2505 int main(int argc, char **argv)
2507 char *outname = NULL;
2508 FILE *outfile = stdout;
2511 if (argc < 2) usage();
2513 if (!strcmp( argv[1], "-o" ))
2518 if (argc < 2) usage();
2519 if (!(outfile = fopen( outname, "w" )))
2521 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2526 /* Retrieve the selector values; this assumes that we are building
2527 * the asm files on the platform that will also run them. Probably
2528 * a safe assumption to make.
2530 GET_CS( Code_Selector );
2531 GET_DS( Data_Selector );
2533 if (!strcmp( argv[1], "-spec" ))
2534 res = BuildSpec( outfile, argc, argv );
2535 else if (!strcmp( argv[1], "-callfrom16" ))
2536 res = BuildCallFrom16( outfile, outname, argc, argv );
2537 else if (!strcmp( argv[1], "-callto16" ))
2538 res = BuildCallTo16( outfile, outname, argc, argv );
2539 else if (!strcmp( argv[1], "-call32" ))
2540 res = BuildCall32( outfile, outname );