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"
22 #ifdef NEED_UNDERSCORE_PREFIX
28 #if defined(__GNUC__) && !defined(__svr4__)
37 TYPE_BYTE, /* byte variable (Win16) */
38 TYPE_WORD, /* word variable (Win16) */
39 TYPE_LONG, /* long variable (Win16) */
40 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
41 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
42 TYPE_ABS, /* absolute value (Win16) */
43 TYPE_RETURN, /* simple return value function (Win16) */
44 TYPE_REGISTER, /* register function */
45 TYPE_STUB, /* unimplemented stub */
46 TYPE_STDCALL, /* stdcall function (Win32) */
47 TYPE_CDECL, /* cdecl function (Win32) */
48 TYPE_VARARGS, /* varargs function (Win32) */
49 TYPE_EXTERN, /* external symbol (Win32) */
53 static const char * const TypeNames[TYPE_NBTYPES] =
56 "byte", /* TYPE_BYTE */
57 "word", /* TYPE_WORD */
58 "long", /* TYPE_LONG */
59 "pascal16", /* TYPE_PASCAL_16 */
60 "pascal", /* TYPE_PASCAL */
61 "equate", /* TYPE_ABS */
62 "return", /* TYPE_RETURN */
63 "register", /* TYPE_REGISTER */
64 "stub", /* TYPE_STUB */
65 "stdcall", /* TYPE_STDCALL */
66 "cdecl", /* TYPE_CDECL */
67 "varargs", /* TYPE_VARARGS */
68 "extern" /* TYPE_EXTERN */
71 #define MAX_ORDINALS 2048
73 /* Callback function used for stub functions */
74 #define STUB_CALLBACK \
75 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
135 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
137 static SPEC_TYPE SpecType = SPEC_INVALID;
138 static char DLLName[80];
139 static char DLLFileName[80];
140 static int Limit = 0;
141 static int Base = MAX_ORDINALS;
142 static int DLLHeapSize = 0;
143 static char *SpecName;
145 static WORD Code_Selector, Data_Selector;
147 char *ParseBuffer = NULL;
152 static int debugging = 1;
154 /* Offset of a structure field relative to the start of the struct */
155 #define STRUCTOFFSET(type,field) ((int)&((type *)0)->field)
157 /* Offset of register relative to the start of the CONTEXT struct */
158 #define CONTEXTOFFSET(reg) STRUCTOFFSET(CONTEXT,reg)
160 static void *xmalloc (size_t size)
164 res = malloc (size ? size : 1);
167 fprintf (stderr, "Virtual memory exhausted.\n");
174 static void *xrealloc (void *ptr, size_t size)
176 void *res = realloc (ptr, size);
179 fprintf (stderr, "Virtual memory exhausted.\n");
186 static int IsNumberString(char *s)
195 static char *strupper(char *s)
199 for(p = s; *p != '\0'; p++)
205 static char * GetTokenInLine(void)
210 if (ParseNext != ParseBuffer)
212 if (ParseSaveChar == '\0')
214 *ParseNext = ParseSaveChar;
218 * Remove initial white space.
220 for (p = ParseNext; isspace(*p); p++)
223 if ((*p == '\0') || (*p == '#'))
230 if (*token != '(' && *token != ')')
231 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
241 static char * GetToken(void)
245 if (ParseBuffer == NULL)
247 ParseBuffer = xmalloc(512);
248 ParseNext = ParseBuffer;
252 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
254 if (ParseBuffer[0] != '#')
259 while ((token = GetTokenInLine()) == NULL)
261 ParseNext = ParseBuffer;
265 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
267 if (ParseBuffer[0] != '#')
276 /*******************************************************************
279 * Parse a variable definition.
281 static int ParseVariable( ORDDEF *odp )
286 int value_array_size;
288 char *token = GetToken();
291 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
292 SpecName, Line, token);
297 value_array_size = 25;
298 value_array = xmalloc(sizeof(*value_array) * value_array_size);
300 while ((token = GetToken()) != NULL)
305 value_array[n_values++] = strtol(token, &endptr, 0);
306 if (n_values == value_array_size)
308 value_array_size += 25;
309 value_array = xrealloc(value_array,
310 sizeof(*value_array) * value_array_size);
313 if (endptr == NULL || *endptr != '\0')
315 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
316 SpecName, Line, token);
323 fprintf(stderr, "%s:%d: End of file in variable declaration\n",
328 odp->u.var.n_values = n_values;
329 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
335 /*******************************************************************
336 * ParseExportFunction
338 * Parse a function definition.
340 static int ParseExportFunction( ORDDEF *odp )
348 if (odp->type == TYPE_STDCALL)
350 fprintf( stderr, "%s:%d: 'stdcall' not supported for Win16\n",
354 if (odp->type == TYPE_CDECL)
356 fprintf( stderr, "%s:%d: 'cdecl' not supported for Win16\n",
362 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
364 fprintf( stderr, "%s:%d: 'pascal' not supported for Win32\n",
376 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
377 SpecName, Line, token);
381 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
387 if (!strcmp(token, "word"))
388 odp->u.func.arg_types[i] = 'w';
389 else if (!strcmp(token, "s_word"))
390 odp->u.func.arg_types[i] = 's';
391 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
392 odp->u.func.arg_types[i] = 'l';
393 else if (!strcmp(token, "ptr"))
394 odp->u.func.arg_types[i] = 'p';
395 else if (!strcmp(token, "str"))
396 odp->u.func.arg_types[i] = 't';
397 else if (!strcmp(token, "wstr"))
398 odp->u.func.arg_types[i] = 'W';
399 else if (!strcmp(token, "segstr"))
400 odp->u.func.arg_types[i] = 'T';
401 else if (!strcmp(token, "double"))
403 odp->u.func.arg_types[i++] = 'l';
404 odp->u.func.arg_types[i] = 'l';
408 fprintf(stderr, "%s:%d: Unknown variable type '%s'\n",
409 SpecName, Line, token);
412 if (SpecType == SPEC_WIN32)
414 if (strcmp(token, "long") &&
415 strcmp(token, "ptr") &&
416 strcmp(token, "str") &&
417 strcmp(token, "wstr") &&
418 strcmp(token, "double"))
420 fprintf( stderr, "%s:%d: Type '%s' not supported for Win32\n",
421 SpecName, Line, token );
426 if ((*token != ')') || (i >= sizeof(odp->u.func.arg_types)))
428 fprintf( stderr, "%s:%d: Too many arguments\n", SpecName, Line );
431 odp->u.func.arg_types[i] = '\0';
432 if ((odp->type == TYPE_STDCALL) && !i)
433 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
434 if ((odp->type == TYPE_REGISTER) && (SpecType == SPEC_WIN32) && i)
436 fprintf( stderr, "%s:%d: register functions cannot have arguments in Win32\n",
440 strcpy(odp->u.func.link_name, GetToken());
445 /*******************************************************************
448 * Parse an 'equate' definition.
450 static int ParseEquate( ORDDEF *odp )
454 char *token = GetToken();
455 int value = strtol(token, &endptr, 0);
456 if (endptr == NULL || *endptr != '\0')
458 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
459 SpecName, Line, token);
463 if (SpecType == SPEC_WIN32)
465 fprintf( stderr, "%s:%d: 'equate' not supported for Win32\n",
470 odp->u.abs.value = value;
475 /*******************************************************************
478 * Parse a 'return' definition.
480 static int ParseReturn( ORDDEF *odp )
486 odp->u.ret.arg_size = strtol(token, &endptr, 0);
487 if (endptr == NULL || *endptr != '\0')
489 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
490 SpecName, Line, token);
495 odp->u.ret.ret_value = strtol(token, &endptr, 0);
496 if (endptr == NULL || *endptr != '\0')
498 fprintf(stderr, "%s:%d: Expected number value, got '%s'\n",
499 SpecName, Line, token);
503 if (SpecType == SPEC_WIN32)
505 fprintf( stderr, "%s:%d: 'return' not supported for Win32\n",
514 /*******************************************************************
517 * Parse a 'stub' definition.
519 static int ParseStub( ORDDEF *odp )
521 odp->u.func.arg_types[0] = '\0';
522 strcpy( odp->u.func.link_name, STUB_CALLBACK );
527 /*******************************************************************
530 * Parse an 'varargs' definition.
532 static int ParseVarargs( ORDDEF *odp )
536 if (SpecType == SPEC_WIN16)
538 fprintf( stderr, "%s:%d: 'varargs' not supported for Win16\n",
546 fprintf(stderr, "%s:%d: Expected '(' got '%s'\n",
547 SpecName, Line, token);
553 fprintf(stderr, "%s:%d: Expected ')' got '%s'\n",
554 SpecName, Line, token);
558 strcpy( odp->u.vargs.link_name, GetToken() );
563 /*******************************************************************
566 * Parse an 'extern' definition.
568 static int ParseExtern( ORDDEF *odp )
570 if (SpecType == SPEC_WIN16)
572 fprintf( stderr, "%s:%d: 'extern' not supported for Win16\n",
576 strcpy( odp->u.ext.link_name, GetToken() );
581 /*******************************************************************
584 * Parse an ordinal definition.
586 static int ParseOrdinal(int ordinal)
591 if (ordinal >= MAX_ORDINALS)
593 fprintf(stderr, "%s:%d: Ordinal number too large\n", SpecName, Line );
596 if (ordinal > Limit) Limit = ordinal;
597 if (ordinal < Base) Base = ordinal;
599 odp = &OrdinalDefinitions[ordinal];
600 if (!(token = GetToken()))
602 fprintf(stderr, "%s:%d: Expected type after ordinal\n", SpecName, Line);
606 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
607 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
610 if (odp->type >= TYPE_NBTYPES)
613 "%s:%d: Expected type after ordinal, found '%s' instead\n",
614 SpecName, Line, token );
618 if (!(token = GetToken()))
620 fprintf( stderr, "%s:%d: Expected name after type\n", SpecName, Line );
623 strcpy( odp->name, token );
631 return ParseVariable( odp );
637 return ParseExportFunction( odp );
639 return ParseEquate( odp );
641 return ParseReturn( odp );
643 return ParseStub( odp );
645 return ParseVarargs( odp );
647 return ParseExtern( odp );
649 fprintf( stderr, "Should not happen\n" );
655 /*******************************************************************
660 static int ParseTopLevel(void)
664 while ((token = GetToken()) != NULL)
666 if (strcmp(token, "name") == 0)
668 strcpy(DLLName, GetToken());
670 if (!DLLFileName[0]) sprintf( DLLFileName, "%s.DLL", DLLName );
672 else if (strcmp(token, "file") == 0)
674 strcpy(DLLFileName, GetToken());
675 strupper(DLLFileName);
677 else if (strcmp(token, "type") == 0)
680 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
681 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
684 fprintf(stderr, "%s:%d: Type must be 'win16' or 'win32'\n",
689 else if (strcmp(token, "heap") == 0)
692 if (!IsNumberString(token))
694 fprintf(stderr, "%s:%d: Expected number after heap\n",
698 DLLHeapSize = atoi(token);
700 else if (IsNumberString(token))
705 ordinal = atoi(token);
706 if ((rv = ParseOrdinal(ordinal)) < 0)
712 "%s:%d: Expected name, id, length or ordinal\n",
722 /*******************************************************************
725 * Store a list of ints into a byte array.
727 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
734 for (i = 0; i < odp->u.var.n_values; i++)
735 buffer[i] = odp->u.var.values[i];
738 for (i = 0; i < odp->u.var.n_values; i++)
739 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
742 for (i = 0; i < odp->u.var.n_values; i++)
743 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
746 return odp->u.var.n_values * size;
750 /*******************************************************************
753 * Dump a byte stream into the assembly code.
755 static void DumpBytes( FILE *outfile, const unsigned char *data, int len,
756 const char *section, const char *label_start )
759 if (section) fprintf( outfile, "\t%s\n", section );
760 if (label_start) fprintf( outfile, "%s:\n", label_start );
761 for (i = 0; i < len; i++)
763 if (!(i & 0x0f)) fprintf( outfile, "\t.byte " );
764 fprintf( outfile, "%d", *data++ );
766 fprintf( outfile, "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
768 fprintf( outfile, "\n" );
772 /*******************************************************************
775 * Build the in-memory representation of a 16-bit NE module, and dump it
776 * as a byte stream into the assembly code.
778 static int BuildModule16( FILE *outfile, int max_code_offset,
779 int max_data_offset )
785 SEGTABLEENTRY *pSegment;
792 * OFSTRUCT File information
793 * SEGTABLEENTRY Segment 1 (code)
794 * SEGTABLEENTRY Segment 2 (data)
795 * WORD[2] Resource table (empty)
796 * BYTE[2] Imported names (empty)
797 * BYTE[n] Resident names table
798 * BYTE[n] Entry table
801 buffer = xmalloc( 0x10000 );
803 pModule = (NE_MODULE *)buffer;
804 pModule->magic = IMAGE_OS2_SIGNATURE;
807 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
809 pModule->heap_size = DLLHeapSize;
810 pModule->stack_size = 0;
815 pModule->seg_count = 2;
816 pModule->modref_count = 0;
817 pModule->nrname_size = 0;
818 pModule->modref_table = 0;
819 pModule->nrname_fpos = 0;
820 pModule->moveable_entries = 0;
821 pModule->alignment = 0;
822 pModule->truetype = 0;
823 pModule->os_flags = NE_OSFLAGS_WINDOWS;
824 pModule->misc_flags = 0;
825 pModule->dlls_to_init = 0;
826 pModule->nrname_handle = 0;
827 pModule->min_swap_area = 0;
828 pModule->expected_version = 0x030a;
829 pModule->module32 = 0;
831 pModule->self_loading_sel = 0;
833 /* File information */
835 pFileInfo = (OFSTRUCT *)(pModule + 1);
836 pModule->fileinfo = (int)pFileInfo - (int)pModule;
837 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
838 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
839 + strlen(DLLFileName);
840 strcpy( pFileInfo->szPathName, DLLFileName );
841 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
845 pSegment = (SEGTABLEENTRY *)pstr;
846 pModule->seg_table = (int)pSegment - (int)pModule;
847 pSegment->filepos = 0;
848 pSegment->size = max_code_offset;
850 pSegment->minsize = max_code_offset;
851 pSegment->selector = 0;
854 pModule->dgroup_entry = (int)pSegment - (int)pModule;
855 pSegment->filepos = 0;
856 pSegment->size = max_data_offset;
857 pSegment->flags = NE_SEGFLAGS_DATA;
858 pSegment->minsize = max_data_offset;
859 pSegment->selector = 0;
864 pword = (WORD *)pSegment;
865 pModule->res_table = (int)pword - (int)pModule;
869 /* Imported names table */
871 pstr = (char *)pword;
872 pModule->import_table = (int)pstr - (int)pModule;
876 /* Resident names table */
878 pModule->name_table = (int)pstr - (int)pModule;
879 /* First entry is module name */
880 *pstr = strlen(DLLName );
881 strcpy( pstr + 1, DLLName );
884 pstr += sizeof(WORD);
885 /* Store all ordinals */
886 odp = OrdinalDefinitions + 1;
887 for (i = 1; i <= Limit; i++, odp++)
889 if (!odp->name[0]) continue;
890 *pstr = strlen( odp->name );
891 strcpy( pstr + 1, odp->name );
892 strupper( pstr + 1 );
895 pstr += sizeof(WORD);
901 pModule->entry_table = (int)pstr - (int)pModule;
903 odp = OrdinalDefinitions + 1;
904 for (i = 1; i <= Limit; i++, odp++)
915 selector = 1; /* Code selector */
921 selector = 2; /* Data selector */
925 selector = 0xfe; /* Constant selector */
929 selector = 0; /* Invalid selector */
933 /* create a new bundle if necessary */
934 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
938 bundle[1] = selector;
946 *(WORD *)pstr = odp->offset;
947 pstr += sizeof(WORD);
952 /* Dump the module content */
954 DumpBytes( outfile, (char *)pModule, (int)pstr - (int)pModule,
955 ".data", "Module_Start" );
956 return (int)pstr - (int)pModule;
960 /*******************************************************************
963 * Build a Win32 C file from a spec file.
965 static int BuildSpec32File( char * specfile, FILE *outfile )
968 int i, nb_names, nb_reg_funcs = 0;
970 fprintf( outfile, "/* File generated automatically from %s; do not edit! */\n\n",
972 fprintf( outfile, "#include \"builtin32.h\"\n\n" );
974 /* Output code for all stubs functions */
976 fprintf( outfile, "extern const BUILTIN32_DESCRIPTOR %s_Descriptor;\n",
978 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
980 if (odp->type != TYPE_STUB) continue;
981 fprintf( outfile, "static void __stub_%d() { BUILTIN32_Unimplemented(&%s_Descriptor,%d); }\n",
985 /* Output the DLL functions prototypes */
987 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
992 fprintf( outfile, "extern void %s();\n", odp->u.vargs.link_name );
995 fprintf( outfile, "extern void %s();\n", odp->u.ext.link_name );
1000 fprintf( outfile, "extern void %s();\n", odp->u.func.link_name );
1006 fprintf(stderr,"build: function type %d not available for Win32\n",
1012 /* Output the DLL functions table */
1014 fprintf( outfile, "\nstatic const ENTRYPOINT32 Functions[%d] =\n{\n",
1016 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1021 fprintf( outfile, " 0" );
1024 fprintf( outfile, " %s", odp->u.vargs.link_name );
1027 fprintf( outfile, " %s", odp->u.ext.link_name );
1032 fprintf( outfile, " %s", odp->u.func.link_name);
1035 fprintf( outfile, " __stub_%d", i );
1040 if (i < Limit) fprintf( outfile, ",\n" );
1042 fprintf( outfile, "\n};\n\n" );
1044 /* Output the DLL names table */
1047 fprintf( outfile, "static const char * const FuncNames[] =\n{\n" );
1048 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1050 if (odp->type == TYPE_INVALID) continue;
1051 if (nb_names++) fprintf( outfile, ",\n" );
1052 fprintf( outfile, " \"%s\"", odp->name );
1054 fprintf( outfile, "\n};\n\n" );
1056 /* Output the DLL argument types */
1058 fprintf( outfile, "static const unsigned int ArgTypes[%d] =\n{\n",
1060 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1062 unsigned int j, mask = 0;
1063 if ((odp->type == TYPE_STDCALL) || (odp->type == TYPE_CDECL))
1064 for (j = 0; odp->u.func.arg_types[j]; j++)
1066 if (odp->u.func.arg_types[j] == 't') mask |= 1<< (j*2);
1067 if (odp->u.func.arg_types[j] == 'W') mask |= 2<< (j*2);
1069 fprintf( outfile, " %d", mask );
1070 if (i < Limit) fprintf( outfile, ",\n" );
1072 fprintf( outfile, "\n};\n\n" );
1074 /* Output the DLL ordinals table */
1076 fprintf( outfile, "static const unsigned short FuncOrdinals[] =\n{\n" );
1078 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1080 if (odp->type == TYPE_INVALID) continue;
1081 if (nb_names++) fprintf( outfile, ",\n" );
1082 fprintf( outfile, " %d", i - Base );
1084 fprintf( outfile, "\n};\n\n" );
1086 /* Output the DLL functions arguments */
1088 fprintf( outfile, "static const unsigned char FuncArgs[%d] =\n{\n",
1090 for (i = Base, odp = OrdinalDefinitions + Base; i <= Limit; i++, odp++)
1096 args = (unsigned char)strlen(odp->u.func.arg_types);
1099 args = 0x80 | (unsigned char)strlen(odp->u.func.arg_types);
1109 fprintf( outfile, " 0x%02x", args );
1110 if (i < Limit) fprintf( outfile, ",\n" );
1112 fprintf( outfile, "\n};\n\n" );
1114 /* Output the DLL descriptor */
1116 fprintf( outfile, "const BUILTIN32_DESCRIPTOR %s_Descriptor =\n{\n",
1118 fprintf( outfile, " \"%s\",\n", DLLName );
1119 fprintf( outfile, " %d,\n", Base );
1120 fprintf( outfile, " %d,\n", Limit - Base + 1 );
1121 fprintf( outfile, " %d,\n", nb_names );
1122 fprintf( outfile, " %d,\n", nb_reg_funcs );
1134 /*******************************************************************
1137 * Build a Win16 assembly file from a spec file.
1139 static int BuildSpec16File( char * specfile, FILE *outfile )
1143 int code_offset, data_offset, module_size;
1144 unsigned char *data;
1146 data = (unsigned char *)xmalloc( 0x10000 );
1147 memset( data, 0, 16 );
1150 fprintf( outfile, "/* File generated automatically; do not edit! */\n" );
1151 fprintf( outfile, "\t.text\n" );
1152 fprintf( outfile, "Code_Start:\n" );
1155 odp = OrdinalDefinitions;
1156 for (i = 0; i <= Limit; i++, odp++)
1161 odp->offset = 0xffff;
1165 odp->offset = LOWORD(odp->u.abs.value);
1169 odp->offset = data_offset;
1170 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1174 odp->offset = data_offset;
1175 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1179 odp->offset = data_offset;
1180 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1184 fprintf( outfile,"/* %s.%d */\n", DLLName, i);
1185 fprintf( outfile,"\tmovw $%d,%%ax\n",LOWORD(odp->u.ret.ret_value));
1186 fprintf( outfile,"\tmovw $%d,%%dx\n",HIWORD(odp->u.ret.ret_value));
1187 fprintf( outfile,"\t.byte 0x66\n");
1188 if (odp->u.ret.arg_size != 0)
1189 fprintf( outfile, "\tlret $%d\n\n", odp->u.ret.arg_size);
1192 fprintf( outfile, "\tlret\n");
1193 fprintf( outfile, "\tnop\n");
1194 fprintf( outfile, "\tnop\n\n");
1196 odp->offset = code_offset;
1197 code_offset += 12; /* Assembly code is 12 bytes long */
1202 case TYPE_PASCAL_16:
1204 fprintf( outfile, "/* %s.%d */\n", DLLName, i);
1205 fprintf( outfile, "\tpushw %%bp\n" );
1206 fprintf( outfile, "\tpushl $" PREFIX "%s\n",odp->u.func.link_name);
1207 /* FreeBSD does not understand lcall, so do it the hard way */
1208 fprintf( outfile, "\t.byte 0x9a\n" );
1209 fprintf( outfile, "\t.long " PREFIX "CallFrom16_%s_%s\n",
1210 (odp->type == TYPE_REGISTER) ? "regs" :
1211 (odp->type == TYPE_PASCAL) ? "long" : "word",
1212 odp->u.func.arg_types );
1213 fprintf( outfile, "\t.long 0x%08lx\n",
1214 MAKELONG( Code_Selector, 0x9090 /* nop ; nop */ ) );
1215 odp->offset = code_offset;
1216 code_offset += 16; /* Assembly code is 16 bytes long */
1220 fprintf(stderr,"build: function type %d not available for Win16\n",
1226 if (!code_offset) /* Make sure the code segment is not empty */
1228 fprintf( outfile, "\t.byte 0\n" );
1232 /* Output data segment */
1234 DumpBytes( outfile, data, data_offset, NULL, "Data_Start" );
1236 /* Build the module */
1238 module_size = BuildModule16( outfile, code_offset, data_offset );
1240 /* Output the DLL descriptor */
1242 fprintf( outfile, "\t.text\n" );
1243 fprintf( outfile, "DLLName:\t.string \"%s\\0\"\n", DLLName );
1244 fprintf( outfile, "\t.align 4\n" );
1245 fprintf( outfile, "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1246 fprintf( outfile, PREFIX "%s_Descriptor:\n", DLLName );
1247 fprintf( outfile, "\t.long DLLName\n" ); /* Name */
1248 fprintf( outfile, "\t.long Module_Start\n" ); /* Module start */
1249 fprintf( outfile, "\t.long %d\n", module_size ); /* Module size */
1250 fprintf( outfile, "\t.long Code_Start\n" ); /* Code start */
1251 fprintf( outfile, "\t.long Data_Start\n" ); /* Data start */
1256 /*******************************************************************
1259 * Build an assembly file from a spec file.
1261 static int BuildSpecFile( FILE *outfile, char *specname )
1263 SpecName = specname;
1264 SpecFp = fopen( specname, "r");
1267 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1271 if (ParseTopLevel() < 0) return -1;
1276 return BuildSpec16File( specname, outfile );
1278 return BuildSpec32File( specname, outfile );
1280 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1286 /*******************************************************************
1287 * TransferArgs16To32
1289 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1290 * The 16-bit stack layout is:
1298 static int TransferArgs16To32( FILE *outfile, char *args )
1300 int i, pos16, pos32;
1302 /* Save ebx first */
1304 fprintf( outfile, "\tpushl %%ebx\n" );
1306 /* Get the 32-bit stack pointer */
1308 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebx\n" );
1310 /* Copy the arguments */
1312 pos16 = 6; /* skip bp and return address */
1315 for (i = strlen(args); i > 0; i--)
1320 case 'w': /* word */
1321 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1322 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1326 case 's': /* s_word */
1327 fprintf( outfile, "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1328 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1332 case 'l': /* long or segmented pointer */
1333 case 'T': /* segmented pointer to null-terminated string */
1334 fprintf( outfile, "\tmovl %d(%%ebp),%%eax\n", pos16 );
1335 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1339 case 'p': /* linear pointer */
1340 case 't': /* linear pointer to null-terminated string */
1341 /* Get the selector */
1342 fprintf( outfile, "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1343 /* Get the selector base */
1344 fprintf( outfile, "\tandl $0xfff8,%%eax\n" );
1345 fprintf( outfile, "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1346 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1347 /* Add the offset */
1348 fprintf( outfile, "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1349 fprintf( outfile, "\taddl %%eax,%d(%%ebx)\n", pos32 );
1354 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1360 fprintf( outfile, "\tpopl %%ebx\n" );
1362 return pos16 - 6; /* Return the size of the 16-bit args */
1366 /*******************************************************************
1369 * Build the context structure on the 32-bit stack.
1371 static void BuildContext16( FILE *outfile )
1373 /* Save ebx first */
1375 fprintf( outfile, "\tpushl %%ebx\n" );
1377 /* Get the 32-bit stack pointer */
1379 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebx\n" );
1381 /* Store the registers */
1383 fprintf( outfile, "\tpopl %d(%%ebx)\n", /* Get ebx from stack*/
1384 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1385 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1386 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1387 fprintf( outfile, "\tmovl %%ecx,%d(%%ebx)\n",
1388 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1389 fprintf( outfile, "\tmovl %%edx,%d(%%ebx)\n",
1390 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1391 fprintf( outfile, "\tmovl %%esi,%d(%%ebx)\n",
1392 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1393 fprintf( outfile, "\tmovl %%edi,%d(%%ebx)\n",
1394 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1396 fprintf( outfile, "\tmovzwl -10(%%ebp),%%eax\n" ); /* Get %ds from stack*/
1397 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1398 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1399 fprintf( outfile, "\tmovzwl -6(%%ebp),%%eax\n" ); /* Get %es from stack*/
1400 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1401 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1402 fprintf( outfile, "\tpushfl\n" );
1403 fprintf( outfile, "\tpopl %d(%%ebx)\n",
1404 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1405 fprintf( outfile, "\tmovl -16(%%ebp),%%eax\n" ); /* Get %ebp from stack */
1406 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1407 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1408 fprintf( outfile, "\tmovzwl 2(%%ebp),%%eax\n" ); /* Get %ip from stack */
1409 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1410 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1411 fprintf( outfile, "\tmovzwl 4(%%ebp),%%eax\n" ); /* Get %cs from stack */
1412 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1413 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1414 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
1415 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1416 CONTEXTOFFSET(SegFs) - sizeof(CONTEXT) );
1417 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
1418 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1419 CONTEXTOFFSET(SegGs) - sizeof(CONTEXT) );
1420 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1421 fprintf( outfile, "\tmovl %%eax,%d(%%ebx)\n",
1422 CONTEXTOFFSET(SegSs) - sizeof(CONTEXT) );
1424 fprintf( outfile, "\tfsave %d(%%ebx)\n",
1425 CONTEXTOFFSET(FloatSave) - sizeof(CONTEXT) );
1430 /*******************************************************************
1433 * Restore the registers from the context structure.
1434 * %edx must point to the 32-bit stack top.
1436 static void RestoreContext16( FILE *outfile )
1438 /* Get the 32-bit stack pointer */
1440 fprintf( outfile, "\tmovl %%edx,%%ebx\n" );
1442 /* Remove everything up to the return address from the 16-bit stack */
1444 fprintf( outfile, "\taddl $22,%%esp\n" );
1446 /* Restore the registers */
1448 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n",
1449 CONTEXTOFFSET(Ecx) - sizeof(CONTEXT) );
1450 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n",
1451 CONTEXTOFFSET(Edx) - sizeof(CONTEXT) );
1452 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n",
1453 CONTEXTOFFSET(Esi) - sizeof(CONTEXT) );
1454 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n",
1455 CONTEXTOFFSET(Edi) - sizeof(CONTEXT) );
1456 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n",
1457 CONTEXTOFFSET(Ebp) - sizeof(CONTEXT) );
1458 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new cs */
1459 CONTEXTOFFSET(SegCs) - sizeof(CONTEXT) );
1460 fprintf( outfile, "\tpushw %d(%%ebx)\n", /* Push new ip */
1461 CONTEXTOFFSET(Eip) - sizeof(CONTEXT) );
1462 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new ds */
1463 CONTEXTOFFSET(SegDs) - sizeof(CONTEXT) );
1464 fprintf( outfile, "\tpushl %d(%%ebx)\n", /* Push new es */
1465 CONTEXTOFFSET(SegEs) - sizeof(CONTEXT) );
1466 fprintf( outfile, "\tpushl %d(%%ebx)\n",
1467 CONTEXTOFFSET(EFlags) - sizeof(CONTEXT) );
1468 fprintf( outfile, "\tpopfl\n" );
1469 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n",
1470 CONTEXTOFFSET(Eax) - sizeof(CONTEXT) );
1471 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n",
1472 CONTEXTOFFSET(Ebx) - sizeof(CONTEXT) );
1473 fprintf( outfile, "\tpopl %%es\n" ); /* Set es */
1474 fprintf( outfile, "\tpopl %%ds\n" ); /* Set ds */
1478 /*******************************************************************
1479 * BuildCallFrom16Func
1481 * Build a 16-bit-to-Wine callback function. The syntax of the function
1482 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1483 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1484 * 'l'=long, 'p'=linear pointer, 't'=linear pointer to null-terminated string,
1485 * 'T'=segmented pointer to null-terminated string).
1486 * For register functions, the arguments are ignored, but they are still
1487 * removed from the stack upon return.
1489 * Stack layout upon entry to the callback function:
1491 * (sp+18) word first 16-bit arg
1495 * (sp+8) long 32-bit entry point (used to store edx)
1496 * (sp+6) word high word of cs (always 0, used to store es)
1497 * (sp+4) word low word of cs of 16-bit entry point
1498 * (sp+2) word high word of ip (always 0, used to store ds)
1499 * (sp) word low word of ip of 16-bit entry point
1501 * Added on the stack:
1503 * (sp-6) word saved previous sp
1504 * (sp-8) word saved previous ss
1506 static void BuildCallFrom16Func( FILE *outfile, char *profile )
1511 char *args = profile + 5;
1513 /* Parse function type */
1515 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1516 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1517 else if (strncmp( "long_", profile, 5 ))
1519 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1523 /* Function header */
1525 fprintf( outfile, "\n\t.align 4\n" );
1527 fprintf( outfile, ".stabs \"CallFrom16_%s:F1\",36,0,0," PREFIX "CallFrom16_%s\n",
1530 fprintf( outfile, "\t.globl " PREFIX "CallFrom16_%s\n", profile );
1531 fprintf( outfile, PREFIX "CallFrom16_%s:\n", profile );
1533 /* Setup bp to point to its copy on the stack */
1535 fprintf( outfile, "\tpushl %%ebp\n" ); /* Save the full 32-bit ebp */
1536 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1537 fprintf( outfile, "\taddw $16,%%bp\n" );
1539 /* Save 16-bit ds and es */
1541 /* Stupid FreeBSD assembler doesn't know these either */
1542 /* fprintf( outfile, "\tmovw %%ds,-10(%%ebp)\n" ); */
1543 fprintf( outfile, "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1544 /* fprintf( outfile, "\tmovw %%es,-6(%%ebp)\n" ); */
1545 fprintf( outfile, "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1547 /* Restore 32-bit ds and es */
1549 fprintf( outfile, "\tpushl $0x%04x%04x\n", Data_Selector, Data_Selector );
1550 fprintf( outfile, "\t.byte 0x66\n\tpopl %%ds\n" );
1551 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" );
1554 /* Save the 16-bit stack */
1556 fprintf( outfile, "\tpushl " PREFIX "IF1632_Saved16_ss_sp\n" );
1558 fprintf( outfile,"\tdata16\n");
1560 fprintf( outfile, "\tmovw %%ss," PREFIX "IF1632_Saved16_ss_sp+2\n" );
1561 fprintf( outfile, "\tmovw %%sp," PREFIX "IF1632_Saved16_ss_sp\n" );
1563 /* Transfer the arguments */
1565 if (reg_func) BuildContext16( outfile );
1566 else if (*args) argsize = TransferArgs16To32( outfile, args );
1568 /* Get the address of the API function */
1570 fprintf( outfile, "\tmovl -4(%%ebp),%%eax\n" );
1572 /* If necessary, save %edx over the API function address */
1574 if (!reg_func && short_ret)
1575 fprintf( outfile, "\tmovl %%edx,-4(%%ebp)\n" );
1577 /* Switch to the 32-bit stack */
1579 fprintf( outfile, "\tmovl " PREFIX "CALLTO16_Saved32_esp,%%ebp\n" );
1580 fprintf( outfile, "\tpushl %%ds\n" );
1581 fprintf( outfile, "\tpopl %%ss\n" );
1582 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1583 reg_func ? sizeof(CONTEXT) : 4 * strlen(args) );
1584 if (reg_func) /* Push the address of the context struct */
1585 fprintf( outfile, "\tpushl %%esp\n" );
1587 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1589 fprintf( outfile, "\taddl $%d,%%ebp\n", STRUCTOFFSET(STACK32FRAME,ebp) );
1591 /* Print the debug information before the call */
1595 fprintf( outfile, "\tpushl %%eax\n" );
1596 fprintf( outfile, "\tpushl $Profile_%s\n", profile );
1597 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1598 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1599 fprintf( outfile, "\tpopl %%eax\n" );
1600 fprintf( outfile, "\tpopl %%eax\n" );
1601 fprintf( outfile, "\tpopl %%eax\n" );
1604 /* Call the entry point */
1606 fprintf( outfile, "\tcall *%%eax\n" );
1608 /* Print the debug information after the call */
1614 /* Push again the address of the context struct in case */
1615 /* it has been removed by an stdcall function */
1616 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1617 sizeof(CONTEXT) + STRUCTOFFSET(STACK32FRAME,ebp) );
1618 fprintf( outfile, "\tpushl %%esp\n" );
1620 fprintf( outfile, "\tpushl %%eax\n" );
1621 fprintf( outfile, "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0));
1622 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1623 fprintf( outfile, "\tpopl %%eax\n" );
1624 fprintf( outfile, "\tpopl %%eax\n" );
1627 /* Restore the value of the saved 32-bit stack pointer */
1629 fprintf( outfile, "\tleal -%d(%%ebp),%%edx\n",
1630 STRUCTOFFSET(STACK32FRAME,ebp) );
1631 fprintf( outfile, "movl %%edx," PREFIX "CALLTO16_Saved32_esp\n" );
1633 /* Restore the 16-bit stack */
1636 fprintf( outfile, "\tdata16\n");
1638 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1639 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1640 fprintf( outfile, "\tpopl " PREFIX "IF1632_Saved16_ss_sp\n" );
1644 /* Calc the arguments size */
1660 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1665 /* Restore registers from the context structure */
1666 RestoreContext16( outfile );
1670 /* Restore high 16 bits of ebp */
1671 fprintf( outfile, "\tpopl %%ebp\n" );
1673 /* Restore ds and es */
1674 fprintf( outfile, "\tincl %%esp\n" ); /* Remove ip */
1675 fprintf( outfile, "\tincl %%esp\n" );
1676 fprintf( outfile, "\tpopl %%edx\n" ); /* Remove cs and ds */
1677 fprintf( outfile, "\tmovw %%dx,%%ds\n" ); /* and restore ds */
1678 fprintf( outfile, "\t.byte 0x66\n\tpopl %%es\n" ); /* Restore es */
1680 if (short_ret) fprintf( outfile, "\tpopl %%edx\n" ); /* Restore edx */
1683 /* Get the return value into dx:ax */
1684 fprintf( outfile, "\tmovl %%eax,%%edx\n" );
1685 fprintf( outfile, "\tshrl $16,%%edx\n" );
1686 /* Remove API entry point */
1687 fprintf( outfile, "\taddl $4,%%esp\n" );
1690 /* Restore low 16 bits of ebp */
1691 fprintf( outfile, "\tpopw %%bp\n" );
1694 /* Remove the arguments and return */
1698 fprintf( outfile, "\t.byte 0x66\n" );
1699 fprintf( outfile, "\tlret $%d\n", argsize );
1703 fprintf( outfile, "\t.byte 0x66\n" );
1704 fprintf( outfile, "\tlret\n" );
1709 /*******************************************************************
1712 * Build a Wine-to-16-bit callback function.
1714 * Stack frame of the callback function:
1718 * (ebp+8) func to call
1719 * (ebp+4) return address
1720 * (ebp) previous ebp
1722 * Prototypes for the CallTo16 functions:
1723 * extern WINAPI WORD CallTo16_word_xxx( FARPROC16 func, args... );
1724 * extern WINAPI LONG CallTo16_long_xxx( FARPROC16 func, args... );
1725 * extern WINAPI void CallTo16_sreg_( const CONTEXT *context );
1726 * extern WINAPI void CallTo16_lreg_( const CONTEXT *context );
1728 static void BuildCallTo16Func( FILE *outfile, char *profile )
1732 char *args = profile + 5;
1734 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1735 else if (!strncmp( "sreg_", profile, 5 )) reg_func = 1;
1736 else if (!strncmp( "lreg_", profile, 5 )) reg_func = 2;
1737 else if (strncmp( "long_", profile, 5 ))
1739 fprintf( stderr, "Invalid function name '%s'.\n", profile );
1743 /* Function header */
1745 fprintf( outfile, "\n\t.align 4\n" );
1747 fprintf( outfile, ".stabs \"CallTo16_%s:F1\",36,0,0," PREFIX "CallTo16_%s\n",
1750 fprintf( outfile, "\t.globl " PREFIX "CallTo16_%s\n", profile );
1751 fprintf( outfile, PREFIX "CallTo16_%s:\n", profile );
1755 fprintf( outfile, "\tpushl %%ebp\n" );
1756 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
1758 /* Call the actual CallTo16 routine (simulate a lcall) */
1760 fprintf( outfile, "\tpushl %%cs\n" );
1761 fprintf( outfile, "\tcall do_callto16_%s\n", profile );
1765 /* FIXME: this is a hack because of task.c */
1766 if (!strcmp( profile, "word_" ))
1768 fprintf( outfile, ".globl " PREFIX "CALLTO16_Restore\n" );
1769 fprintf( outfile, PREFIX "CALLTO16_Restore:\n" );
1771 fprintf( outfile, "\tpopl %%ebp\n" );
1772 fprintf( outfile, "\tret $%d\n", 4 * strlen(args) + 4 );
1774 /* Start of the actual CallTo16 routine */
1776 /* Save the 32-bit registers */
1778 fprintf( outfile, "do_callto16_%s:\n", profile );
1779 fprintf( outfile, "\tpushl %%ebx\n" );
1780 fprintf( outfile, "\tpushl %%ecx\n" );
1781 fprintf( outfile, "\tpushl %%edx\n" );
1782 fprintf( outfile, "\tpushl %%esi\n" );
1783 fprintf( outfile, "\tpushl %%edi\n" );
1785 /* Save the 32-bit stack */
1787 fprintf( outfile, "\tmovl %%esp," PREFIX "CALLTO16_Saved32_esp\n" );
1788 fprintf( outfile, "\tmovl %%ebp,%%ebx\n" );
1790 /* Print debugging info */
1794 /* Push the address of the first argument */
1795 fprintf( outfile, "\tleal 8(%%ebp),%%eax\n" );
1796 fprintf( outfile, "\tpushl $%d\n", reg_func ? -1 : strlen(args) );
1797 fprintf( outfile, "\tpushl %%eax\n" );
1798 fprintf( outfile, "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1799 fprintf( outfile, "\tpopl %%eax\n" );
1800 fprintf( outfile, "\tpopl %%eax\n" );
1805 /* Switch to the 16-bit stack, saving the current %%esp, */
1806 /* and adding the specified offset to the new sp */
1807 fprintf( outfile, "\tmovzwl " PREFIX "IF1632_Saved16_ss_sp,%%edx\n" );
1808 fprintf( outfile, "\tleal -4(%%edx),%%edx\n" );
1809 fprintf( outfile, "\tmovl 12(%%ebx),%%eax\n" ); /* Get the offset */
1810 fprintf( outfile, "\taddl %%edx,%%eax\n" );
1812 fprintf( outfile,"\tdata16\n");
1814 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1815 fprintf( outfile, "\txchgl %%esp,%%eax\n" );
1816 fprintf( outfile, "\t.byte 0x36\n" /* %ss: */ );
1817 fprintf( outfile, "\tmovl %%eax,0(%%edx)\n" );
1819 /* Get the registers. ebx is handled later on. */
1821 fprintf( outfile, "\tmovl 8(%%ebx),%%ebx\n" );
1822 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(SegEs) );
1823 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1824 fprintf( outfile, "\tmovl %d(%%ebx),%%ebp\n", CONTEXTOFFSET(Ebp) );
1825 fprintf( outfile, "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(Eax) );
1826 fprintf( outfile, "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(Ecx) );
1827 fprintf( outfile, "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(Edx) );
1828 fprintf( outfile, "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(Esi) );
1829 fprintf( outfile, "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(Edi) );
1831 /* Push the return address
1832 * With sreg suffix, we push 16:16 address (normal lret)
1833 * With lreg suffix, we push 16:32 address (0x66 lret, for KERNEL32_45)
1836 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_regs\n" );
1839 fprintf( outfile, "\tpushw $0\n" );
1840 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_regs+2\n" );
1841 fprintf( outfile, "\tpushw $0\n" );
1842 fprintf( outfile, "\tpushw " PREFIX "CALLTO16_RetAddr_regs\n" );
1845 /* Push the called routine address */
1847 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(SegCs) );
1848 fprintf( outfile, "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(Eip) );
1850 /* Get the 16-bit ds */
1852 fprintf( outfile, "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(SegDs) );
1853 /* Get ebx from the 32-bit stack */
1854 fprintf( outfile, "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(Ebx) );
1855 fprintf( outfile, "\tpopl %%ds\n" );
1857 else /* not a register function */
1859 int pos = 12; /* first argument position */
1861 /* Switch to the 16-bit stack, saving the current %%esp */
1862 fprintf( outfile, "\tmovl %%esp,%%eax\n" );
1864 fprintf( outfile,"\tdata16\n");
1866 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp+2,%%ss\n" );
1867 fprintf( outfile, "\tmovw " PREFIX "IF1632_Saved16_ss_sp,%%sp\n" );
1868 fprintf( outfile, "\tpushl %%eax\n" );
1870 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1871 fprintf( outfile, "\tmovzwl %%sp,%%ebp\n" );
1872 fprintf( outfile, "\tleal %d(%%ebp),%%ebp\n",
1873 STRUCTOFFSET(STACK16FRAME,bp) + 4 /* for saved %%esp */ );
1875 /* Transfer the arguments */
1881 case 'w': /* word */
1882 fprintf( outfile, "\tpushw %d(%%ebx)\n", pos );
1884 case 'l': /* long */
1885 fprintf( outfile, "\tpushl %d(%%ebx)\n", pos );
1888 fprintf( stderr, "Unexpected case '%c' in BuildCallTo16Func\n",
1894 /* Push the return address */
1896 fprintf( outfile, "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1897 short_ret ? "word" : "long" );
1899 /* Push the called routine address */
1901 fprintf( outfile, "\tpushl 8(%%ebx)\n" );
1903 /* Set %ds and %es (and %ax just in case) equal to %ss */
1905 fprintf( outfile, "\tmovw %%ss,%%ax\n" );
1906 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
1907 fprintf( outfile, "\tmovw %%ax,%%es\n" );
1910 /* Jump to the called routine */
1912 fprintf( outfile, "\t.byte 0x66\n" );
1913 fprintf( outfile, "\tlret\n" );
1917 /*******************************************************************
1920 * Build the return code for 16-bit callbacks
1922 static void BuildRet16Func( FILE *outfile )
1924 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1925 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1926 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Ret_regs\n" );
1928 fprintf( outfile, PREFIX "CALLTO16_Ret_word:\n" );
1929 fprintf( outfile, "\txorl %%edx,%%edx\n" );
1931 /* Remove the arguments just in case */
1933 fprintf( outfile, PREFIX "CALLTO16_Ret_long:\n" );
1934 fprintf( outfile, "\tleal -%d(%%ebp),%%esp\n",
1935 STRUCTOFFSET(STACK16FRAME,bp) + 4 /* for saved %%esp */ );
1937 /* Put return value into %eax */
1939 fprintf( outfile, PREFIX "CALLTO16_Ret_regs:\n" );
1940 fprintf( outfile, "\tshll $16,%%edx\n" );
1941 fprintf( outfile, "\tmovw %%ax,%%dx\n" );
1942 fprintf( outfile, "\tmovl %%edx,%%eax\n" );
1944 /* Restore 32-bit segment registers */
1946 fprintf( outfile, "\tpopl %%ecx\n" ); /* Get the saved %%esp */
1947 fprintf( outfile, "\tmovw $0x%04x,%%bx\n", Data_Selector );
1949 fprintf( outfile, "\tdata16\n");
1951 fprintf( outfile, "\tmovw %%bx,%%ds\n" );
1953 fprintf( outfile, "\tdata16\n");
1955 fprintf( outfile, "\tmovw %%bx,%%es\n" );
1957 /* Restore the 32-bit stack */
1960 fprintf( outfile, "\tdata16\n");
1962 fprintf( outfile, "\tmovw %%bx,%%ss\n" );
1963 fprintf( outfile, "\tmovl %%ecx,%%esp\n" );
1965 /* Restore the 32-bit registers */
1967 fprintf( outfile, "\tpopl %%edi\n" );
1968 fprintf( outfile, "\tpopl %%esi\n" );
1969 fprintf( outfile, "\tpopl %%edx\n" );
1970 fprintf( outfile, "\tpopl %%ecx\n" );
1971 fprintf( outfile, "\tpopl %%ebx\n" );
1973 /* Return to caller */
1975 fprintf( outfile, "\tlret\n" );
1977 /* Declare the return address variables */
1979 fprintf( outfile, "\t.data\n" );
1980 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
1981 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
1982 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_RetAddr_regs\n" );
1983 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Saved32_esp\n" );
1984 fprintf( outfile, PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
1985 fprintf( outfile, PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
1986 fprintf( outfile, PREFIX "CALLTO16_RetAddr_regs:\t.long 0\n" );
1987 fprintf( outfile, PREFIX "CALLTO16_Saved32_esp:\t.long 0\n" );
1988 fprintf( outfile, "\t.text\n" );
1992 /*******************************************************************
1993 * BuildCallTo32LargeStack
1995 * Build the function used to switch to the original 32-bit stack
1996 * before calling a 32-bit function from 32-bit code. This is used for
1997 * functions that need a large stack, like X bitmaps functions.
1999 * The generated function has the following prototype:
2000 * int xxx( int (*func)(), void *arg );
2002 * The pointer to the function can be retrieved by calling CALL32_Init,
2003 * which also takes care of saving the current 32-bit stack pointer.
2012 static void BuildCallTo32LargeStack( FILE *outfile )
2014 /* Initialization function */
2016 fprintf( outfile, "\n\t.align 4\n" );
2018 fprintf( outfile, ".stabs \"CALL32_Init:F1\",36,0,0," PREFIX "CALL32_Init\n");
2020 fprintf( outfile, "\t.globl " PREFIX "CALL32_Init\n" );
2021 fprintf( outfile, "\t.type " PREFIX "CALL32_Init,@function\n" );
2022 fprintf( outfile, PREFIX "CALL32_Init:\n" );
2023 fprintf( outfile, "\tleal -256(%%esp),%%eax\n" );
2024 fprintf( outfile, "\tmovl %%eax,CALL32_Original32_esp\n" );
2025 fprintf( outfile, "\tmovl $CALL32_LargeStack,%%eax\n" );
2026 fprintf( outfile, "\tret\n" );
2028 /* Function header */
2030 fprintf( outfile, "\n\t.align 4\n" );
2032 fprintf( outfile, ".stabs \"CALL32_LargeStack:F1\",36,0,0,CALL32_LargeStack\n");
2034 fprintf( outfile, "CALL32_LargeStack:\n" );
2038 fprintf( outfile, "\tpushl %%ebp\n" );
2039 fprintf( outfile, "\tmovl %%esp,%%ebp\n" );
2041 /* Switch to the original 32-bit stack pointer */
2043 fprintf( outfile, "\tmovl CALL32_Original32_esp, %%esp\n" );
2045 /* Transfer the argument and call the function */
2047 fprintf( outfile, "\tpushl 12(%%ebp)\n" );
2048 fprintf( outfile, "\tcall *8(%%ebp)\n" );
2050 /* Restore registers and return */
2052 fprintf( outfile, "\tmovl %%ebp,%%esp\n" );
2053 fprintf( outfile, "\tpopl %%ebp\n" );
2054 fprintf( outfile, "\tret\n" );
2058 fprintf( outfile, "\t.data\n" );
2059 fprintf( outfile, "CALL32_Original32_esp:\t.long 0\n" );
2060 fprintf( outfile, "\t.text\n" );
2064 /*******************************************************************
2065 * BuildCallFrom32Regs
2067 * Build a 32-bit-to-Wine call-back function for a 'register' function.
2068 * 'args' is the number of dword arguments.
2072 * (esp+208) ret addr (or relay addr when debugging_relay is on)
2073 * (esp+204) entry point
2074 * (esp+0) CONTEXT struct
2076 static void BuildCallFrom32Regs( FILE *outfile )
2078 /* Function header */
2080 fprintf( outfile, "\n\t.align 4\n" );
2082 fprintf( outfile, ".stabs \"CALL32_Regs:F1\",36,0,0," PREFIX "CALL32_Regs\n" );
2084 fprintf( outfile, "\t.globl " PREFIX "CALL32_Regs\n" );
2085 fprintf( outfile, PREFIX "CALL32_Regs:\n" );
2087 /* Build the context structure */
2089 fprintf( outfile, "\tpushw $0\n" );
2090 fprintf( outfile, "\t.byte 0x66\n\tpushl %%ss\n" );
2091 fprintf( outfile, "\tpushl %%eax\n" ); /* %esp place holder */
2092 fprintf( outfile, "\tpushfl\n" );
2093 fprintf( outfile, "\tpushw $0\n" );
2094 fprintf( outfile, "\t.byte 0x66\n\tpushl %%cs\n" );
2095 fprintf( outfile, "\tpushl 20(%%esp)\n" ); /* %eip at time of call */
2096 fprintf( outfile, "\tpushl %%ebp\n" );
2098 fprintf( outfile, "\tpushl %%eax\n" );
2099 fprintf( outfile, "\tpushl %%ecx\n" );
2100 fprintf( outfile, "\tpushl %%edx\n" );
2101 fprintf( outfile, "\tpushl %%ebx\n" );
2102 fprintf( outfile, "\tpushl %%esi\n" );
2103 fprintf( outfile, "\tpushl %%edi\n" );
2105 fprintf( outfile, "\txorl %%eax,%%eax\n" );
2106 fprintf( outfile, "\tmovw %%ds,%%ax\n" );
2107 fprintf( outfile, "\tpushl %%eax\n" );
2108 fprintf( outfile, "\tmovw %%es,%%ax\n" );
2109 fprintf( outfile, "\tpushl %%eax\n" );
2110 fprintf( outfile, "\tmovw %%fs,%%ax\n" );
2111 fprintf( outfile, "\tpushl %%eax\n" );
2112 fprintf( outfile, "\tmovw %%gs,%%ax\n" );
2113 fprintf( outfile, "\tpushl %%eax\n" );
2115 fprintf( outfile, "\tleal -%d(%%esp),%%esp\n",
2116 sizeof(FLOATING_SAVE_AREA) + 6 * sizeof(DWORD) /* DR regs */ );
2117 fprintf( outfile, "\tpushl $0x0001001f\n" ); /* ContextFlags */
2119 fprintf( outfile, "\tfsave %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2121 fprintf( outfile, "\tleal %d(%%esp),%%eax\n",
2122 sizeof(CONTEXT) + 4 ); /* %esp at time of call */
2123 fprintf( outfile, "\tmovl %%eax,%d(%%esp)\n", CONTEXTOFFSET(Esp) );
2125 fprintf( outfile, "\tcall " PREFIX "RELAY_CallFrom32Regs\n" );
2127 /* Restore the context structure */
2129 fprintf( outfile, "\tfrstor %d(%%esp)\n", CONTEXTOFFSET(FloatSave) );
2131 /* Store %eip value onto the new stack */
2133 fprintf( outfile, "\tmovl %d(%%esp),%%eax\n", CONTEXTOFFSET(Eip) );
2134 fprintf( outfile, "\tmovl %d(%%esp),%%ebx\n", CONTEXTOFFSET(Esp) );
2135 fprintf( outfile, "\tmovl %%eax,0(%%ebx)\n" );
2137 /* Restore all registers */
2139 fprintf( outfile, "\tleal %d(%%esp),%%esp\n",
2140 sizeof(FLOATING_SAVE_AREA) + 7 * sizeof(DWORD) );
2141 fprintf( outfile, "\tpopl %%eax\n" );
2142 fprintf( outfile, "\tmovw %%ax,%%gs\n" );
2143 fprintf( outfile, "\tpopl %%eax\n" );
2144 fprintf( outfile, "\tmovw %%ax,%%fs\n" );
2145 fprintf( outfile, "\tpopl %%eax\n" );
2146 fprintf( outfile, "\tmovw %%ax,%%es\n" );
2147 fprintf( outfile, "\tpopl %%eax\n" );
2148 fprintf( outfile, "\tmovw %%ax,%%ds\n" );
2150 fprintf( outfile, "\tpopl %%edi\n" );
2151 fprintf( outfile, "\tpopl %%esi\n" );
2152 fprintf( outfile, "\tpopl %%ebx\n" );
2153 fprintf( outfile, "\tpopl %%edx\n" );
2154 fprintf( outfile, "\tpopl %%ecx\n" );
2155 fprintf( outfile, "\tpopl %%eax\n" );
2156 fprintf( outfile, "\tpopl %%ebp\n" );
2157 fprintf( outfile, "\tleal 8(%%esp),%%esp\n" ); /* skip %eip and %cs */
2158 fprintf( outfile, "\tpopfl\n" );
2159 fprintf( outfile, "\tpopl %%esp\n" );
2160 fprintf( outfile, "\tret\n" );
2164 /*******************************************************************
2167 * Build the spec files
2169 static int BuildSpec( FILE *outfile, int argc, char *argv[] )
2172 for (i = 2; i < argc; i++)
2173 if (BuildSpecFile( outfile, argv[i] ) < 0) return -1;
2178 /*******************************************************************
2181 * Build the 16-bit-to-Wine callbacks
2183 static int BuildCallFrom16( FILE *outfile, char * outname, int argc, char *argv[] )
2190 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2191 fprintf( outfile, "\t.text\n" );
2194 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2195 getcwd(buffer, sizeof(buffer));
2198 * The stabs help the internal debugger as they are an indication that it
2199 * is sensible to step into a thunk/trampoline.
2201 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2202 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2203 fprintf( outfile, "\t.text\n" );
2204 fprintf( outfile, "\t.align 4\n" );
2205 fprintf( outfile, "Code_Start:\n\n" );
2208 /* Build the callback functions */
2210 for (i = 2; i < argc; i++) BuildCallFrom16Func( outfile, argv[i] );
2212 /* Output the argument debugging strings */
2216 fprintf( outfile, "/* Argument strings */\n" );
2217 for (i = 2; i < argc; i++)
2219 fprintf( outfile, "Profile_%s:\n", argv[i] );
2220 fprintf( outfile, "\t.string \"%s\\0\"\n", argv[i] + 5 );
2225 fprintf( outfile, "\t.text\n");
2226 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2227 fprintf( outfile, ".Letext:\n");
2234 /*******************************************************************
2237 * Build the Wine-to-16-bit callbacks
2239 static int BuildCallTo16( FILE *outfile, char * outname, int argc, char *argv[] )
2246 infile = fopen( argv[2], "r" );
2253 else infile = stdin;
2257 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2258 fprintf( outfile, "\t.text\n" );
2261 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2262 getcwd(buffer, sizeof(buffer));
2265 * The stabs help the internal debugger as they are an indication that it
2266 * is sensible to step into a thunk/trampoline.
2268 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2269 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2270 fprintf( outfile, "\t.text\n" );
2271 fprintf( outfile, "\t.align 4\n" );
2272 fprintf( outfile, "Code_Start:\n\n" );
2275 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_Start\n" );
2276 fprintf( outfile, PREFIX "CALLTO16_Start:\n" );
2278 /* Build the callback functions */
2280 while (fgets( buffer, sizeof(buffer), infile ))
2282 if (strstr( buffer, "### start build ###" )) break;
2284 while (fgets( buffer, sizeof(buffer), infile ))
2286 char *p = strstr( buffer, "CallTo16_" );
2289 char *profile = p + strlen( "CallTo16_" );
2291 while ((*p == '_') || isalpha(*p)) p++;
2293 BuildCallTo16Func( outfile, profile );
2295 if (strstr( buffer, "### stop build ###" )) break;
2298 /* Output the 16-bit return code */
2300 BuildRet16Func( outfile );
2302 fprintf( outfile, "\t.globl " PREFIX "CALLTO16_End\n" );
2303 fprintf( outfile, PREFIX "CALLTO16_End:\n" );
2306 fprintf( outfile, "\t.text\n");
2307 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2308 fprintf( outfile, ".Letext:\n");
2316 /*******************************************************************
2319 * Build the 32-bit callbacks
2321 static int BuildCall32( FILE *outfile, char * outname )
2327 fprintf( outfile, "/* File generated automatically. Do not edit! */\n\n" );
2328 fprintf( outfile, "\t.text\n" );
2333 fprintf( outfile, "\t.file\t\"%s\"\n", outname );
2334 getcwd(buffer, sizeof(buffer));
2337 * The stabs help the internal debugger as they are an indication that it
2338 * is sensible to step into a thunk/trampoline.
2340 fprintf( outfile, ".stabs \"%s/\",100,0,0,Code_Start\n", buffer);
2341 fprintf( outfile, ".stabs \"%s\",100,0,0,Code_Start\n", outname);
2342 fprintf( outfile, "\t.text\n" );
2343 fprintf( outfile, "\t.align 4\n" );
2344 fprintf( outfile, "Code_Start:\n" );
2347 /* Build the 32-bit large stack callback */
2349 BuildCallTo32LargeStack( outfile );
2351 /* Build the register callback function */
2353 BuildCallFrom32Regs( outfile );
2356 fprintf( outfile, "\t.text\n");
2357 fprintf( outfile, "\t.stabs \"\",100,0,0,.Letext\n");
2358 fprintf( outfile, ".Letext:\n");
2361 #else /* __i386__ */
2363 /* Just to avoid an empty file */
2364 fprintf( outfile, "\t.long 0\n" );
2366 #endif /* __i386__ */
2371 /*******************************************************************
2374 static void usage(void)
2377 "usage: build [-o outfile] -spec SPECNAMES\n"
2378 " build [-o outfile] -callfrom16 FUNCTION_PROFILES\n"
2379 " build [-o outfile] -callto16 FUNCTION_PROFILES\n"
2380 " build [-o outfile] -call32\n" );
2385 /*******************************************************************
2388 int main(int argc, char **argv)
2390 char *outname = NULL;
2391 FILE *outfile = stdout;
2394 if (argc < 2) usage();
2396 if (!strcmp( argv[1], "-o" ))
2401 if (argc < 2) usage();
2402 if (!(outfile = fopen( outname, "w" )))
2404 fprintf( stderr, "Unable to create output file '%s'\n", outname );
2409 /* Retrieve the selector values; this assumes that we are building
2410 * the asm files on the platform that will also run them. Probably
2411 * a safe assumption to make.
2413 GET_CS( Code_Selector );
2414 GET_DS( Data_Selector );
2416 if (!strcmp( argv[1], "-spec" ))
2417 res = BuildSpec( outfile, argc, argv );
2418 else if (!strcmp( argv[1], "-callfrom16" ))
2419 res = BuildCallFrom16( outfile, outname, argc, argv );
2420 else if (!strcmp( argv[1], "-callto16" ))
2421 res = BuildCallTo16( outfile, outname, argc, argv );
2422 else if (!strcmp( argv[1], "-call32" ))
2423 res = BuildCall32( outfile, outname );