Release 960818
[wine/hacks.git] / tools / build.c
blobcdd1544ae2f5308b5a5cabe9652bb3ef461fc2cc
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Martin von Loewis
4 * Copyright 1995, 1996 Alexandre Julliard
5 */
7 #ifndef WINELIB
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <ctype.h>
13 #include "wintypes.h"
14 #include "registers.h"
15 #include "winerror.h" /* for ERROR_CALL_NOT_IMPLEMENTED */
16 #include "module.h"
17 #include "neexe.h"
18 #include "windows.h"
20 #ifdef NEED_UNDERSCORE_PREFIX
21 # define PREFIX "_"
22 #else
23 # define PREFIX
24 #endif
26 typedef enum
28 TYPE_INVALID,
29 TYPE_BYTE, /* byte variable */
30 TYPE_WORD, /* word variable */
31 TYPE_LONG, /* long variable */
32 TYPE_PASCAL_16, /* pascal function with 16-bit return (Win16) */
33 TYPE_PASCAL, /* pascal function with 32-bit return (Win16) */
34 TYPE_REGISTER, /* register function (Win16) */
35 TYPE_ABS, /* absolute value */
36 TYPE_RETURN, /* simple return value function */
37 TYPE_STUB, /* unimplemented stub */
38 TYPE_STDCALL, /* stdcall function (Win32) */
39 TYPE_CDECL, /* cdecl function (Win32) */
40 TYPE_EXTERN, /* external symbol (Win32) */
41 TYPE_NBTYPES
42 } ORD_TYPE;
44 static const char * const TypeNames[TYPE_NBTYPES] =
46 NULL,
47 "byte", /* TYPE_BYTE */
48 "word", /* TYPE_WORD */
49 "long", /* TYPE_LONG */
50 "pascal16", /* TYPE_PASCAL_16 */
51 "pascal", /* TYPE_PASCAL */
52 "register", /* TYPE_REGISTER */
53 "equate", /* TYPE_ABS */
54 "return", /* TYPE_RETURN */
55 "stub", /* TYPE_STUB */
56 "stdcall", /* TYPE_STDCALL */
57 "cdecl", /* TYPE_CDECL */
58 "extern" /* TYPE_EXTERN */
61 #define MAX_ORDINALS 1299
63 /* Callback function used for stub functions */
64 #define STUB_CALLBACK \
65 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
67 typedef enum
69 SPEC_INVALID,
70 SPEC_WIN16,
71 SPEC_WIN32
72 } SPEC_TYPE;
74 typedef struct
76 int n_values;
77 int *values;
78 } ORD_VARIABLE;
80 typedef struct
82 int n_args;
83 char arg_types[32];
84 char link_name[80];
85 } ORD_FUNCTION;
87 typedef struct
89 int arg_size;
90 int ret_value;
91 } ORD_RETURN;
93 typedef struct
95 int value;
96 } ORD_ABS;
98 typedef struct
100 char link_name[80];
101 } ORD_EXTERN;
103 typedef struct
105 ORD_TYPE type;
106 int offset;
107 char name[80];
108 union
110 ORD_VARIABLE var;
111 ORD_FUNCTION func;
112 ORD_RETURN ret;
113 ORD_ABS abs;
114 ORD_EXTERN ext;
115 } u;
116 } ORDDEF;
118 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
120 static SPEC_TYPE SpecType = SPEC_INVALID;
121 char DLLName[80];
122 int Limit = 0;
123 int Base = 0;
124 int DLLHeapSize = 0;
125 FILE *SpecFp;
127 char *ParseBuffer = NULL;
128 char *ParseNext;
129 char ParseSaveChar;
130 int Line;
132 static int debugging = 1;
134 /* Offset of register relative to the end of the context struct */
135 #define CONTEXTOFFSET(reg) \
136 ((int)&reg##_reg((SIGCONTEXT *)0) - sizeof(SIGCONTEXT))
138 static void *xmalloc (size_t size)
140 void *res;
142 res = malloc (size ? size : 1);
143 if (res == NULL)
145 fprintf (stderr, "Virtual memory exhausted.\n");
146 exit (1);
148 return res;
152 static void *xrealloc (void *ptr, size_t size)
154 void *res = realloc (ptr, size);
155 if (res == NULL)
157 fprintf (stderr, "Virtual memory exhausted.\n");
158 exit (1);
160 return res;
164 static int IsNumberString(char *s)
166 while (*s != '\0')
167 if (!isdigit(*s++))
168 return 0;
170 return 1;
173 static char *strupper(char *s)
175 char *p;
177 for(p = s; *p != '\0'; p++)
178 *p = toupper(*p);
180 return s;
183 static char * GetTokenInLine(void)
185 char *p;
186 char *token;
188 if (ParseNext != ParseBuffer)
190 if (ParseSaveChar == '\0')
191 return NULL;
192 *ParseNext = ParseSaveChar;
196 * Remove initial white space.
198 for (p = ParseNext; isspace(*p); p++)
201 if ((*p == '\0') || (*p == '#'))
202 return NULL;
205 * Find end of token.
207 token = p++;
208 if (*token != '(' && *token != ')')
209 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
210 p++;
212 ParseSaveChar = *p;
213 ParseNext = p;
214 *p = '\0';
216 return token;
219 static char * GetToken(void)
221 char *token;
223 if (ParseBuffer == NULL)
225 ParseBuffer = xmalloc(512);
226 ParseNext = ParseBuffer;
227 Line++;
228 while (1)
230 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
231 return NULL;
232 if (ParseBuffer[0] != '#')
233 break;
237 while ((token = GetTokenInLine()) == NULL)
239 ParseNext = ParseBuffer;
240 Line++;
241 while (1)
243 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
244 return NULL;
245 if (ParseBuffer[0] != '#')
246 break;
250 return token;
253 static int ParseVariable( ORDDEF *odp )
255 char *endptr;
256 int *value_array;
257 int n_values;
258 int value_array_size;
260 char *token = GetToken();
261 if (*token != '(')
263 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
264 exit(1);
267 n_values = 0;
268 value_array_size = 25;
269 value_array = xmalloc(sizeof(*value_array) * value_array_size);
271 while ((token = GetToken()) != NULL)
273 if (*token == ')')
274 break;
276 value_array[n_values++] = strtol(token, &endptr, 0);
277 if (n_values == value_array_size)
279 value_array_size += 25;
280 value_array = xrealloc(value_array,
281 sizeof(*value_array) * value_array_size);
284 if (endptr == NULL || *endptr != '\0')
286 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
287 token);
288 exit(1);
292 if (token == NULL)
294 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
295 exit(1);
298 odp->u.var.n_values = n_values;
299 odp->u.var.values = xrealloc(value_array, sizeof(*value_array) * n_values);
301 return 0;
304 static int ParseExportFunction( ORDDEF *odp )
306 char *token;
307 int i;
309 switch(SpecType)
311 case SPEC_WIN16:
312 if (odp->type == TYPE_STDCALL)
314 fprintf( stderr, "%d: 'stdcall' not supported for Win16\n", Line );
315 exit(1);
317 if (odp->type == TYPE_CDECL)
319 fprintf( stderr, "%d: 'cdecl' not supported for Win16\n", Line );
320 exit(1);
322 break;
323 case SPEC_WIN32:
324 if ((odp->type == TYPE_PASCAL) || (odp->type == TYPE_PASCAL_16))
326 fprintf( stderr, "%d: 'pascal' not supported for Win32\n", Line );
327 exit(1);
329 break;
330 default:
331 break;
334 token = GetToken();
335 if (*token != '(')
337 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
338 exit(1);
341 for (i = 0; i < sizeof(odp->u.func.arg_types)-1; i++)
343 token = GetToken();
344 if (*token == ')')
345 break;
347 if (!strcmp(token, "byte") || !strcmp(token, "word"))
348 odp->u.func.arg_types[i] = 'w';
349 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
350 odp->u.func.arg_types[i] = 's';
351 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
352 odp->u.func.arg_types[i] = 'l';
353 else if (!strcmp(token, "ptr"))
354 odp->u.func.arg_types[i] = 'p';
355 else
357 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
358 exit(1);
360 if (SpecType == SPEC_WIN32)
362 if (strcmp(token, "long") && strcmp(token, "ptr"))
364 fprintf( stderr, "%d: Type '%s' not supported for Win32\n",
365 Line, token );
366 exit(1);
370 if (*token != ')')
372 fprintf( stderr, "%d: Too many arguments\n", Line );
373 exit(1);
375 odp->u.func.arg_types[i] = '\0';
376 if ((odp->type == TYPE_STDCALL) && !i)
377 odp->type = TYPE_CDECL; /* stdcall is the same as cdecl for 0 args */
378 strcpy(odp->u.func.link_name, GetToken());
379 return 0;
383 /*******************************************************************
384 * ParseEquate
386 * Parse an 'equate' definition.
388 static int ParseEquate( ORDDEF *odp )
390 char *endptr;
392 char *token = GetToken();
393 int value = strtol(token, &endptr, 0);
394 if (endptr == NULL || *endptr != '\0')
396 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
397 token);
398 exit(1);
401 odp->u.abs.value = value;
402 return 0;
406 /*******************************************************************
407 * ParseReturn
409 * Parse a 'return' definition.
411 static int ParseReturn( ORDDEF *odp )
413 char *token;
414 char *endptr;
416 token = GetToken();
417 odp->u.ret.arg_size = strtol(token, &endptr, 0);
418 if (endptr == NULL || *endptr != '\0')
420 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
421 token);
422 exit(1);
425 token = GetToken();
426 odp->u.ret.ret_value = strtol(token, &endptr, 0);
427 if (endptr == NULL || *endptr != '\0')
429 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
430 token);
431 exit(1);
434 return 0;
438 /*******************************************************************
439 * ParseStub
441 * Parse a 'stub' definition.
443 static int ParseStub( ORDDEF *odp )
445 odp->u.func.arg_types[0] = '\0';
446 strcpy( odp->u.func.link_name, STUB_CALLBACK );
447 return 0;
451 /*******************************************************************
452 * ParseExtern
454 * Parse an 'extern' definition.
456 static int ParseExtern( ORDDEF *odp )
458 if (SpecType == SPEC_WIN16)
460 fprintf( stderr, "%d: 'extern' not supported for Win16\n", Line );
461 exit(1);
463 strcpy( odp->u.ext.link_name, GetToken() );
464 return 0;
468 /*******************************************************************
469 * ParseOrdinal
471 * Parse an ordinal definition.
473 static int ParseOrdinal(int ordinal)
475 ORDDEF *odp;
476 char *token;
478 if (ordinal >= MAX_ORDINALS)
480 fprintf(stderr, "%d: Ordinal number too large\n", Line);
481 exit(1);
483 if (ordinal > Limit) Limit = ordinal;
485 odp = &OrdinalDefinitions[ordinal];
486 if (!(token = GetToken()))
488 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
489 exit(1);
492 for (odp->type = 0; odp->type < TYPE_NBTYPES; odp->type++)
493 if (TypeNames[odp->type] && !strcmp( token, TypeNames[odp->type] ))
494 break;
496 if (odp->type >= TYPE_NBTYPES)
498 fprintf( stderr,
499 "%d: Expected type after ordinal, found '%s' instead\n",
500 Line, token );
501 exit(1);
504 if (!(token = GetToken()))
506 fprintf( stderr, "%d: Expected name after type\n", Line );
507 exit(1);
509 strcpy( odp->name, token );
511 switch(odp->type)
513 case TYPE_BYTE:
514 case TYPE_WORD:
515 case TYPE_LONG:
516 return ParseVariable( odp );
517 case TYPE_PASCAL_16:
518 case TYPE_PASCAL:
519 case TYPE_REGISTER:
520 case TYPE_STDCALL:
521 case TYPE_CDECL:
522 return ParseExportFunction( odp );
523 case TYPE_ABS:
524 return ParseEquate( odp );
525 case TYPE_RETURN:
526 return ParseReturn( odp );
527 case TYPE_STUB:
528 return ParseStub( odp );
529 case TYPE_EXTERN:
530 return ParseExtern( odp );
531 default:
532 fprintf( stderr, "Should not happen\n" );
533 exit(1);
537 static int ParseTopLevel(void)
539 char *token;
541 while ((token = GetToken()) != NULL)
543 if (strcmp(token, "name") == 0)
545 strcpy(DLLName, GetToken());
546 strupper(DLLName);
548 else if (strcmp(token, "type") == 0)
550 token = GetToken();
551 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
552 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
553 else
555 fprintf(stderr, "%d: Type must be 'win16' or 'win32'\n", Line);
556 exit(1);
559 else if (strcmp(token, "base") == 0)
561 token = GetToken();
562 if (!IsNumberString(token))
564 fprintf(stderr, "%d: Expected number after base\n", Line);
565 exit(1);
567 Base = atoi(token);
569 else if (strcmp(token, "heap") == 0)
571 token = GetToken();
572 if (!IsNumberString(token))
574 fprintf(stderr, "%d: Expected number after heap\n", Line);
575 exit(1);
577 DLLHeapSize = atoi(token);
579 else if (IsNumberString(token))
581 int ordinal;
582 int rv;
584 ordinal = atoi(token);
585 if ((rv = ParseOrdinal(ordinal)) < 0)
586 return rv;
588 else
590 fprintf(stderr,
591 "%d: Expected name, id, length or ordinal\n", Line);
592 exit(1);
596 return 0;
600 /*******************************************************************
601 * StoreVariableCode
603 * Store a list of ints into a byte array.
605 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
607 int i;
609 switch(size)
611 case 1:
612 for (i = 0; i < odp->u.var.n_values; i++)
613 buffer[i] = odp->u.var.values[i];
614 break;
615 case 2:
616 for (i = 0; i < odp->u.var.n_values; i++)
617 ((unsigned short *)buffer)[i] = odp->u.var.values[i];
618 break;
619 case 4:
620 for (i = 0; i < odp->u.var.n_values; i++)
621 ((unsigned int *)buffer)[i] = odp->u.var.values[i];
622 break;
624 return odp->u.var.n_values * size;
628 /*******************************************************************
629 * DumpBytes
631 * Dump a byte stream into the assembly code.
633 static void DumpBytes( const unsigned char *data, int len,
634 const char *section, const char *label_start )
636 int i;
637 if (section) printf( "\t%s\n", section );
638 if (label_start) printf( "%s:\n", label_start );
639 for (i = 0; i < len; i++)
641 if (!(i & 0x0f)) printf( "\t.byte " );
642 printf( "%d", *data++ );
643 if (i < len - 1) printf( "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
645 printf( "\n" );
649 /*******************************************************************
650 * BuildModule16
652 * Build the in-memory representation of a 16-bit NE module, and dump it
653 * as a byte stream into the assembly code.
655 static int BuildModule16( int max_code_offset, int max_data_offset )
657 ORDDEF *odp;
658 int i;
659 char *buffer;
660 NE_MODULE *pModule;
661 SEGTABLEENTRY *pSegment;
662 OFSTRUCT *pFileInfo;
663 BYTE *pstr, *bundle;
664 WORD *pword;
666 /* Module layout:
667 * NE_MODULE Module
668 * OFSTRUCT File information
669 * SEGTABLEENTRY Segment 1 (code)
670 * SEGTABLEENTRY Segment 2 (data)
671 * WORD[2] Resource table (empty)
672 * BYTE[2] Imported names (empty)
673 * BYTE[n] Resident names table
674 * BYTE[n] Entry table
677 buffer = xmalloc( 0x10000 );
679 pModule = (NE_MODULE *)buffer;
680 pModule->magic = NE_SIGNATURE;
681 pModule->count = 1;
682 pModule->next = 0;
683 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
684 pModule->dgroup = 2;
685 pModule->heap_size = DLLHeapSize;
686 pModule->stack_size = 0;
687 pModule->ip = 0;
688 pModule->cs = 0;
689 pModule->sp = 0;
690 pModule->ss = 0;
691 pModule->seg_count = 2;
692 pModule->modref_count = 0;
693 pModule->nrname_size = 0;
694 pModule->modref_table = 0;
695 pModule->nrname_fpos = 0;
696 pModule->moveable_entries = 0;
697 pModule->alignment = 0;
698 pModule->truetype = 0;
699 pModule->os_flags = NE_OSFLAGS_WINDOWS;
700 pModule->misc_flags = 0;
701 pModule->dlls_to_init = 0;
702 pModule->nrname_handle = 0;
703 pModule->min_swap_area = 0;
704 pModule->expected_version = 0x030a;
705 pModule->pe_module = NULL;
706 pModule->self = 0;
707 pModule->self_loading_sel = 0;
709 /* File information */
711 pFileInfo = (OFSTRUCT *)(pModule + 1);
712 pModule->fileinfo = (int)pFileInfo - (int)pModule;
713 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
714 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
715 + strlen(DLLName) + 4;
716 sprintf( pFileInfo->szPathName, "%s.DLL", DLLName );
717 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
719 /* Segment table */
721 pSegment = (SEGTABLEENTRY *)pstr;
722 pModule->seg_table = (int)pSegment - (int)pModule;
723 pSegment->filepos = 0;
724 pSegment->size = max_code_offset;
725 pSegment->flags = 0;
726 pSegment->minsize = max_code_offset;
727 pSegment->selector = 0;
728 pSegment++;
730 pModule->dgroup_entry = (int)pSegment - (int)pModule;
731 pSegment->filepos = 0;
732 pSegment->size = max_data_offset;
733 pSegment->flags = NE_SEGFLAGS_DATA;
734 pSegment->minsize = max_data_offset;
735 pSegment->selector = 0;
736 pSegment++;
738 /* Resource table */
740 pword = (WORD *)pSegment;
741 pModule->res_table = (int)pword - (int)pModule;
742 *pword++ = 0;
743 *pword++ = 0;
745 /* Imported names table */
747 pstr = (char *)pword;
748 pModule->import_table = (int)pstr - (int)pModule;
749 *pstr++ = 0;
750 *pstr++ = 0;
752 /* Resident names table */
754 pModule->name_table = (int)pstr - (int)pModule;
755 /* First entry is module name */
756 *pstr = strlen(DLLName );
757 strcpy( pstr + 1, DLLName );
758 pstr += *pstr + 1;
759 *(WORD *)pstr = 0;
760 pstr += sizeof(WORD);
761 /* Store all ordinals */
762 odp = OrdinalDefinitions + 1;
763 for (i = 1; i <= Limit; i++, odp++)
765 if (!odp->name[0]) continue;
766 *pstr = strlen( odp->name );
767 strcpy( pstr + 1, odp->name );
768 strupper( pstr + 1 );
769 pstr += *pstr + 1;
770 *(WORD *)pstr = i;
771 pstr += sizeof(WORD);
773 *pstr++ = 0;
775 /* Entry table */
777 pModule->entry_table = (int)pstr - (int)pModule;
778 bundle = NULL;
779 odp = OrdinalDefinitions + 1;
780 for (i = 1; i <= Limit; i++, odp++)
782 int selector = 0;
784 switch (odp->type)
786 case TYPE_PASCAL:
787 case TYPE_PASCAL_16:
788 case TYPE_REGISTER:
789 case TYPE_RETURN:
790 case TYPE_STUB:
791 selector = 1; /* Code selector */
792 break;
794 case TYPE_BYTE:
795 case TYPE_WORD:
796 case TYPE_LONG:
797 selector = 2; /* Data selector */
798 break;
800 case TYPE_ABS:
801 selector = 0xfe; /* Constant selector */
802 break;
804 default:
805 selector = 0; /* Invalid selector */
806 break;
809 /* create a new bundle if necessary */
810 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
812 bundle = pstr;
813 bundle[0] = 0;
814 bundle[1] = selector;
815 pstr += 2;
818 (*bundle)++;
819 if (selector != 0)
821 *pstr++ = 1;
822 *(WORD *)pstr = odp->offset;
823 pstr += sizeof(WORD);
826 *pstr++ = 0;
828 /* Dump the module content */
830 DumpBytes( (char *)pModule, (int)pstr - (int)pModule,
831 ".data", "Module_Start" );
832 return (int)pstr - (int)pModule;
836 /*******************************************************************
837 * BuildModule32
839 * Build the in-memory representation of a 32-bit pseudo-NE module, and dump it
840 * as a byte stream into the assembly code.
842 static int BuildModule32(void)
844 char *buffer;
845 NE_MODULE *pModule;
846 OFSTRUCT *pFileInfo;
847 BYTE *pstr;
848 WORD *pword;
850 /* Module layout:
851 * NE_MODULE Module
852 * OFSTRUCT File information
853 * SEGTABLEENTRY Segment table (empty)
854 * WORD[2] Resource table (empty)
855 * BYTE[2] Imported names (empty)
856 * BYTE[n] Resident names table (1 entry)
857 * BYTE[n] Entry table (empty)
860 buffer = xmalloc( 0x10000 );
862 pModule = (NE_MODULE *)buffer;
863 pModule->magic = NE_SIGNATURE;
864 pModule->count = 1;
865 pModule->next = 0;
866 pModule->dgroup_entry = 0;
867 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
868 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
869 pModule->dgroup = 0;
870 pModule->heap_size = DLLHeapSize;
871 pModule->stack_size = 0;
872 pModule->ip = 0;
873 pModule->cs = 0;
874 pModule->sp = 0;
875 pModule->ss = 0;
876 pModule->seg_count = 0;
877 pModule->modref_count = 0;
878 pModule->nrname_size = 0;
879 pModule->modref_table = 0;
880 pModule->nrname_fpos = 0;
881 pModule->moveable_entries = 0;
882 pModule->alignment = 0;
883 pModule->truetype = 0;
884 pModule->os_flags = NE_OSFLAGS_WINDOWS;
885 pModule->misc_flags = 0;
886 pModule->dlls_to_init = 0;
887 pModule->nrname_handle = 0;
888 pModule->min_swap_area = 0;
889 pModule->expected_version = 0x030a;
890 pModule->pe_module = NULL;
891 pModule->self = 0;
892 pModule->self_loading_sel = 0;
894 /* File information */
896 pFileInfo = (OFSTRUCT *)(pModule + 1);
897 pModule->fileinfo = (int)pFileInfo - (int)pModule;
898 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
899 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
900 + strlen(DLLName) + 4;
901 sprintf( pFileInfo->szPathName, "%s.DLL", DLLName );
902 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
904 /* Segment table */
906 pModule->seg_table = (int)pstr - (int)pModule;
908 /* Resource table */
910 pword = (WORD *)pstr;
911 pModule->res_table = (int)pword - (int)pModule;
912 *pword++ = 0;
913 *pword++ = 0;
915 /* Imported names table */
917 pstr = (char *)pword;
918 pModule->import_table = (int)pstr - (int)pModule;
919 *pstr++ = 0;
920 *pstr++ = 0;
922 /* Resident names table */
924 pModule->name_table = (int)pstr - (int)pModule;
925 /* First entry is module name */
926 *pstr = strlen(DLLName );
927 strcpy( pstr + 1, DLLName );
928 pstr += *pstr + 1;
929 *(WORD *)pstr = 0;
930 pstr += sizeof(WORD);
931 *pstr++ = 0;
933 /* Entry table */
935 pModule->entry_table = (int)pstr - (int)pModule;
936 *pstr++ = 0;
938 /* Dump the module content */
940 DumpBytes( (char *)pModule, (int)pstr - (int)pModule,
941 ".data", "Module_Start" );
942 return (int)pstr - (int)pModule;
946 /*******************************************************************
947 * BuildSpec32Files
949 * Build a Win32 assembly file from a spec file.
951 static void BuildSpec32Files(void)
953 ORDDEF *odp;
954 int i, module_size, len;
955 char buffer[1024];
957 printf( "/* File generated automatically; do not edit! */\n" );
958 printf( "\t.text\n" );
959 printf( "\t.align 4\n" );
960 printf( "Code_Start:\n\n" );
962 odp = OrdinalDefinitions;
963 for (i = 0; i <= Limit; i++, odp++)
965 switch (odp->type)
967 case TYPE_INVALID:
968 break;
970 case TYPE_STDCALL:
971 case TYPE_CDECL:
972 case TYPE_STUB:
973 printf( "/* %s.%d (%s) */\n",
974 DLLName, i, odp->name);
975 printf( "%s_%d:\n", DLLName, i );
976 printf( "\tpushl %%ebp\n" );
977 printf( "\tpushl $" PREFIX "%s\n", odp->u.func.link_name );
978 printf( "\tcall " PREFIX "CallFrom32_%s_%d\n",
979 (odp->type == TYPE_STDCALL) ? "stdcall" : "cdecl",
980 strlen(odp->u.func.arg_types));
981 printf( "\tnop\n" );
982 break;
984 case TYPE_RETURN:
985 printf( "/* %s.%d (%s) */\n",
986 DLLName, i, odp->name);
987 printf( "%s_%d:\n", DLLName, i );
988 printf( "\tmovl $%d,%%eax\n", ERROR_CALL_NOT_IMPLEMENTED );
989 printf( "\tmovl %%eax," PREFIX "WIN32_LastError\n" );
990 printf( "\tmovl $%d,%%eax\n", odp->u.ret.ret_value );
991 if (odp->u.ret.arg_size)
993 printf( "\tret $%d\n", odp->u.ret.arg_size );
994 printf( "\tnop\n" );
995 printf( "\tnop\n" );
997 else printf( "\tret\n" );
998 break;
1000 case TYPE_BYTE:
1001 printf( "/* %s.%d (%s) */\n",
1002 DLLName, i, odp->name);
1003 printf( "\t.data\n" );
1004 printf( "%s_%d:\n", DLLName, i );
1005 len = StoreVariableCode( buffer, 1, odp );
1006 DumpBytes( buffer, len, NULL, NULL );
1007 printf( "\t.text\n" );
1008 break;
1010 case TYPE_WORD:
1011 printf( "/* %s.%d (%s) */\n",
1012 DLLName, i, odp->name);
1013 printf( "\t.data\n" );
1014 printf( "%s_%d:\n", DLLName, i );
1015 len = StoreVariableCode( buffer, 2, odp );
1016 DumpBytes( buffer, len, NULL, NULL );
1017 printf( "\t.text\n" );
1018 break;
1020 case TYPE_LONG:
1021 printf( "/* %s.%d (%s) */\n",
1022 DLLName, i, odp->name);
1023 printf( "\t.data\n" );
1024 printf( "%s_%d:\n", DLLName, i );
1025 len = StoreVariableCode( buffer, 4, odp );
1026 DumpBytes( buffer, len, NULL, NULL );
1027 printf( "\t.text\n" );
1028 break;
1030 case TYPE_EXTERN:
1031 break;
1033 default:
1034 fprintf(stderr,"build: function type %d not available for Win32\n",
1035 odp->type);
1036 exit(1);
1040 module_size = BuildModule32();
1042 /* Output the DLL functions table */
1044 printf( "\t.text\n" );
1045 printf( "\t.align 4\n" );
1046 printf( "Functions:\n" );
1047 odp = OrdinalDefinitions;
1048 for (i = 0; i <= Limit; i++, odp++)
1050 switch(odp->type)
1052 case TYPE_INVALID:
1053 printf( "\t.long 0\n" );
1054 break;
1055 case TYPE_EXTERN:
1056 printf( "\t.long " PREFIX "%s\n", odp->u.ext.link_name );
1057 break;
1058 default:
1059 printf( "\t.long %s_%d\n", DLLName, i );
1060 break;
1064 /* Output the DLL names table */
1066 printf( "FuncNames:\n" );
1067 odp = OrdinalDefinitions;
1068 for (i = 0; i <= Limit; i++, odp++)
1070 if (odp->type == TYPE_INVALID) printf( "\t.long 0\n" );
1071 else printf( "\t.long Name_%d\n", i );
1074 /* Output the DLL names */
1076 for (i = 0, odp = OrdinalDefinitions; i <= Limit; i++, odp++)
1078 if (odp->type != TYPE_INVALID)
1079 printf( "Name_%d:\t.ascii \"%s\\0\"\n", i, odp->name );
1082 /* Output the DLL descriptor */
1084 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1085 printf( "\t.align 4\n" );
1086 printf( "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1087 printf( PREFIX "%s_Descriptor:\n", DLLName );
1088 printf( "\t.long DLLName\n" ); /* Name */
1089 printf( "\t.long Module_Start\n" ); /* Module start */
1090 printf( "\t.long %d\n", module_size ); /* Module size */
1091 printf( "\t.long %d\n", Base ); /* Base */
1092 printf( "\t.long %d\n", Limit+1 ); /* Size */
1093 printf( "\t.long Code_Start\n" ); /* Code start */
1094 printf( "\t.long Functions\n" ); /* Functions */
1095 printf( "\t.long FuncNames\n" ); /* Function names */
1099 /*******************************************************************
1100 * BuildSpec16Files
1102 * Build a Win16 assembly file from a spec file.
1104 static void BuildSpec16Files(void)
1106 ORDDEF *odp;
1107 int i;
1108 int code_offset, data_offset, module_size;
1109 unsigned char *data;
1111 data = (unsigned char *)xmalloc( 0x10000 );
1112 memset( data, 0, 16 );
1113 data_offset = 16;
1115 printf( "/* File generated automatically; do not edit! */\n" );
1116 printf( "\t.text\n" );
1117 printf( "Code_Start:\n" );
1118 code_offset = 0;
1120 odp = OrdinalDefinitions;
1121 for (i = 0; i <= Limit; i++, odp++)
1123 switch (odp->type)
1125 case TYPE_INVALID:
1126 odp->offset = 0xffff;
1127 break;
1129 case TYPE_ABS:
1130 odp->offset = LOWORD(odp->u.abs.value);
1131 break;
1133 case TYPE_BYTE:
1134 odp->offset = data_offset;
1135 data_offset += StoreVariableCode( data + data_offset, 1, odp);
1136 break;
1138 case TYPE_WORD:
1139 odp->offset = data_offset;
1140 data_offset += StoreVariableCode( data + data_offset, 2, odp);
1141 break;
1143 case TYPE_LONG:
1144 odp->offset = data_offset;
1145 data_offset += StoreVariableCode( data + data_offset, 4, odp);
1146 break;
1148 case TYPE_RETURN:
1149 printf( "/* %s.%d */\n", DLLName, i);
1150 printf( "\tmovw $%d,%%ax\n", LOWORD(odp->u.ret.ret_value) );
1151 printf( "\tmovw $%d,%%dx\n", HIWORD(odp->u.ret.ret_value) );
1152 printf( "\t.byte 0x66\n");
1153 if (odp->u.ret.arg_size != 0)
1154 printf( "\tlret $%d\n\n", odp->u.ret.arg_size);
1155 else
1157 printf( "\tlret\n");
1158 printf( "\tnop\n");
1159 printf( "\tnop\n\n");
1161 odp->offset = code_offset;
1162 code_offset += 12; /* Assembly code is 12 bytes long */
1163 break;
1165 case TYPE_REGISTER:
1166 case TYPE_PASCAL:
1167 case TYPE_PASCAL_16:
1168 case TYPE_STUB:
1169 printf( "/* %s.%d */\n", DLLName, i);
1170 printf( "\tpushw %%bp\n" );
1171 printf( "\tpushl $" PREFIX "%s\n", odp->u.func.link_name );
1172 /* FreeBSD does not understand lcall, so do it the hard way */
1173 printf( "\t.byte 0x9a /*lcall*/\n" );
1174 printf( "\t.long " PREFIX "CallFrom16_%s_%s\n",
1175 (odp->type == TYPE_REGISTER) ? "regs" :
1176 (odp->type == TYPE_PASCAL) ? "long" : "word",
1177 odp->u.func.arg_types );
1178 printf( "\t.byte 0x%02x,0x%02x\n", /* Some asms don't have .word */
1179 LOBYTE(WINE_CODE_SELECTOR), HIBYTE(WINE_CODE_SELECTOR) );
1180 printf( "\tnop\n" );
1181 printf( "\tnop\n\n" );
1182 odp->offset = code_offset;
1183 code_offset += 16; /* Assembly code is 16 bytes long */
1184 break;
1186 default:
1187 fprintf(stderr,"build: function type %d not available for Win16\n",
1188 odp->type);
1189 exit(1);
1193 if (!code_offset) /* Make sure the code segment is not empty */
1195 printf( "\t.byte 0\n" );
1196 code_offset++;
1199 /* Output data segment */
1201 DumpBytes( data, data_offset, NULL, "Data_Start" );
1203 /* Build the module */
1205 module_size = BuildModule16( code_offset, data_offset );
1207 /* Output the DLL descriptor */
1209 printf( "\t.text\n" );
1210 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1211 printf( "\t.align 4\n" );
1212 printf( "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1213 printf( PREFIX "%s_Descriptor:\n", DLLName );
1214 printf( "\t.long DLLName\n" ); /* Name */
1215 printf( "\t.long Module_Start\n" ); /* Module start */
1216 printf( "\t.long %d\n", module_size ); /* Module size */
1217 printf( "\t.long Code_Start\n" ); /* Code start */
1218 printf( "\t.long Data_Start\n" ); /* Data start */
1222 /*******************************************************************
1223 * BuildSpecFiles
1225 * Build an assembly file from a spec file.
1227 static void BuildSpecFiles( char *specname )
1229 SpecFp = fopen( specname, "r");
1230 if (SpecFp == NULL)
1232 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1233 exit(1);
1236 ParseTopLevel();
1237 switch(SpecType)
1239 case SPEC_INVALID:
1240 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1241 exit(1);
1242 case SPEC_WIN16:
1243 BuildSpec16Files();
1244 break;
1245 case SPEC_WIN32:
1246 BuildSpec32Files();
1247 break;
1252 /*******************************************************************
1253 * BuildCall32LargeStack
1255 * Build the function used to switch to the original 32-bit stack
1256 * before calling a 32-bit function from 32-bit code. This is used for
1257 * functions that need a large stack, like X bitmaps functions.
1259 * The generated function has the following prototype:
1260 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1262 * Stack layout:
1263 * ... ...
1264 * (ebp+20) arg2
1265 * (ebp+16) arg1
1266 * (ebp+12) nbargs
1267 * (ebp+8) func
1268 * (ebp+4) ret addr
1269 * (ebp) ebp
1271 static void BuildCall32LargeStack(void)
1273 /* Function header */
1275 printf( "/**********\n" );
1276 printf( " * " PREFIX "CallTo32_LargeStack\n" );
1277 printf( " **********/\n" );
1278 printf( "\t.align 4\n" );
1279 printf( "\t.globl " PREFIX "CallTo32_LargeStack\n\n" );
1280 printf( PREFIX "CallTo32_LargeStack:\n" );
1282 /* Entry code */
1284 printf( "\tpushl %%ebp\n" );
1285 printf( "\tmovl %%esp,%%ebp\n" );
1287 /* Save registers */
1289 printf( "\tpushl %%ecx\n" );
1290 printf( "\tpushl %%esi\n" );
1291 printf( "\tpushl %%edi\n" );
1293 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1295 printf( "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1296 printf( "\torl %%eax,%%eax\n" );
1297 printf( "\tje no_orig_esp\n" );
1298 printf( "\tmovl %%eax,%%esp\n" );
1299 printf( "no_orig_esp:\n" );
1301 /* Transfer the arguments */
1303 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1304 printf( "\torl %%ecx,%%ecx\n" );
1305 printf( "\tje no_args\n" );
1306 printf( "\tleal 16(%%ebp),%%esi\n" );
1307 printf( "\tshll $2,%%ecx\n" );
1308 printf( "\tsubl %%ecx,%%esp\n" );
1309 printf( "\tmovl %%esp,%%edi\n" );
1310 printf( "\tshrl $2,%%ecx\n" );
1311 printf( "\tcld\n" );
1312 printf( "\trep; movsl\n" );
1313 printf( "no_args:\n" );
1315 /* Call the function */
1317 printf( "\tcall 8(%%ebp)\n" );
1319 /* Switch back to the normal stack */
1321 printf( "\tleal -12(%%ebp),%%esp\n" );
1323 /* Restore registers and return */
1325 printf( "\tpopl %%edi\n" );
1326 printf( "\tpopl %%esi\n" );
1327 printf( "\tpopl %%ecx\n" );
1328 printf( "\tpopl %%ebp\n" );
1329 printf( "\tret\n" );
1333 /*******************************************************************
1334 * TransferArgs16To32
1336 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1337 * The 16-bit stack layout is:
1338 * ... ...
1339 * (bp+8) arg2
1340 * (bp+6) arg1
1341 * (bp+4) cs
1342 * (bp+2) ip
1343 * (bp) bp
1345 static int TransferArgs16To32( char *args )
1347 int i, pos16, pos32;
1349 /* Save ebx first */
1351 printf( "\tpushl %%ebx\n" );
1353 /* Get the 32-bit stack pointer */
1355 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1357 /* Copy the arguments */
1359 pos16 = 6; /* skip bp and return address */
1360 pos32 = 0;
1362 for (i = strlen(args); i > 0; i--)
1364 pos32 -= 4;
1365 switch(args[i-1])
1367 case 'w': /* word */
1368 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1369 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1370 pos16 += 2;
1371 break;
1373 case 's': /* s_word */
1374 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1375 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1376 pos16 += 2;
1377 break;
1379 case 'l': /* long */
1380 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
1381 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1382 pos16 += 4;
1383 break;
1385 case 'p': /* ptr */
1386 /* Get the selector */
1387 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1388 /* Get the selector base */
1389 printf( "\tandl $0xfff8,%%eax\n" );
1390 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1391 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1392 /* Add the offset */
1393 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1394 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1395 pos16 += 4;
1396 break;
1398 default:
1399 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1403 /* Restore ebx */
1405 printf( "\tpopl %%ebx\n" );
1407 return pos16 - 6; /* Return the size of the 16-bit args */
1411 /*******************************************************************
1412 * BuildContext
1414 * Build the context structure on the 32-bit stack.
1415 * The only valid registers in the context structure are:
1416 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1418 static void BuildContext(void)
1420 /* Save ebx first */
1422 printf( "\tpushl %%ebx\n" );
1424 /* Get the 32-bit stack pointer */
1426 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1428 /* Store the registers */
1430 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EBX) ); /* Get ebx from stack*/
1431 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(EAX) );
1432 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(ECX) );
1433 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(EDX) );
1434 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(ESI) );
1435 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(EDI) );
1436 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1437 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(DS) );
1438 printf( "\tmovw -6(%%ebp),%%ax\n" ); /* Get saved es from stack */
1439 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(ES) );
1440 printf( "\tpushfl\n" );
1441 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EFL) );
1445 /*******************************************************************
1446 * RestoreContext
1448 * Restore the registers from the context structure
1450 static void RestoreContext(void)
1452 /* Get the 32-bit stack pointer */
1454 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1456 /* Restore the registers */
1458 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(ECX) );
1459 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(EDX) );
1460 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(ESI) );
1461 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(EDI) );
1462 printf( "\tpopl %%eax\n" ); /* Remove old ds and ip from stack */
1463 printf( "\tpopl %%eax\n" ); /* Remove old cs and es from stack */
1464 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(DS) ); /* Push new ds */
1465 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(ES) ); /* Push new es */
1466 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFL) );
1467 printf( "\tpopfl\n" );
1468 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(EAX) );
1469 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(EBX) );
1473 /*******************************************************************
1474 * BuildCallFrom16Func
1476 * Build a 16-bit-to-Wine callback function. The syntax of the function
1477 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1478 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1479 * 'l'=long, 'p'=pointer).
1480 * For register functions, the arguments are ignored, but they are still
1481 * removed from the stack upon return.
1483 * Stack layout upon entry to the callback function:
1484 * ... ...
1485 * (sp+18) word first 16-bit arg
1486 * (sp+16) word cs
1487 * (sp+14) word ip
1488 * (sp+12) word bp
1489 * (sp+8) long 32-bit entry point
1490 * (sp+6) word high word of cs (always 0, used to store es)
1491 * (sp+4) word low word of cs of 16-bit entry point
1492 * (sp+2) word high word of ip (always 0, used to store ds)
1493 * (sp) word low word of ip of 16-bit entry point
1496 static void BuildCallFrom16Func( char *profile )
1498 int argsize = 0;
1499 int short_ret = 0;
1500 int reg_func = 0;
1501 char *args = profile + 5;
1503 /* Parse function type */
1505 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1506 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1507 else if (strncmp( "long_", profile, 5 ))
1509 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1510 return;
1513 /* Function header */
1515 printf( "/**********\n" );
1516 printf( " * " PREFIX "CallFrom16_%s\n", profile );
1517 printf( " **********/\n" );
1518 printf( "\t.align 4\n" );
1519 printf( "\t.globl " PREFIX "CallFrom16_%s\n\n", profile );
1520 printf( PREFIX "CallFrom16_%s:\n", profile );
1522 /* Setup bp to point to its copy on the stack */
1524 printf( "\tmovzwl %%sp,%%ebp\n" );
1525 printf( "\taddw $12,%%bp\n" );
1527 /* Save 16-bit ds and es */
1529 /* Stupid FreeBSD assembler doesn't know these either */
1530 /* printf( "\tmovw %%ds,-10(%%ebp)\n" ); */
1531 printf( "\t.byte 0x66,0x8c,0x5d,0xf6\n" );
1532 /* printf( "\tmovw %%es,-6(%%ebp)\n" ); */
1533 printf( "\t.byte 0x66,0x8c,0x45,0xfa\n" );
1535 /* Restore 32-bit ds and es */
1537 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1538 printf( "\tpopw %%ds\n" );
1539 printf( "\tpopw %%es\n" );
1542 /* Save the 16-bit stack */
1544 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1545 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1546 #ifdef __svr4__
1547 printf("\tdata16\n");
1548 #endif
1549 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1550 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1552 /* Transfer the arguments */
1554 if (reg_func) BuildContext();
1555 else if (*args) argsize = TransferArgs16To32( args );
1557 /* Get the address of the API function */
1559 printf( "\tmovl -4(%%ebp),%%eax\n" );
1561 /* If necessary, save %edx over the API function address */
1563 if (!reg_func && short_ret)
1564 printf( "\tmovl %%edx,-4(%%ebp)\n" );
1566 /* Switch to the 32-bit stack */
1568 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1569 printf( "\tpushw %%ds\n" );
1570 printf( "\tpopw %%ss\n" );
1571 printf( "\tleal -%d(%%ebp),%%esp\n",
1572 reg_func ? sizeof(SIGCONTEXT) : 4 * strlen(args) );
1573 if (reg_func) /* Push the address of the context struct */
1574 printf( "\tpushl %%esp\n" );
1576 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1578 printf( "\taddl $24,%%ebp\n" );
1580 /* Print the debug information before the call */
1582 if (debugging)
1584 printf( "\tpushl %%eax\n" );
1585 printf( "\tpushl $Profile_%s\n", profile );
1586 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1587 printf( "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1588 printf( "\tpopl %%eax\n" );
1589 printf( "\tpopl %%eax\n" );
1590 printf( "\tpopl %%eax\n" );
1593 /* Call the entry point */
1595 printf( "\tcall %%eax\n" );
1597 /* Print the debug information after the call */
1599 if (debugging)
1601 printf( "\tpushl %%eax\n" );
1602 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1603 printf( "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1604 printf( "\tpopl %%eax\n" );
1605 printf( "\tpopl %%eax\n" );
1608 /* Restore the 16-bit stack */
1610 #ifdef __svr4__
1611 printf( "\tdata16\n");
1612 #endif
1613 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1614 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1615 #ifdef __svr4__
1616 printf( "\tdata16\n");
1617 #endif
1618 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1619 #ifdef __svr4__
1620 printf( "\tdata16\n");
1621 #endif
1622 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1624 if (reg_func)
1626 /* Restore registers from the context structure */
1627 RestoreContext();
1629 /* Calc the arguments size */
1630 while (*args)
1632 switch(*args)
1634 case 'w':
1635 case 's':
1636 argsize += 2;
1637 break;
1638 case 'p':
1639 case 'l':
1640 argsize += 4;
1641 break;
1642 default:
1643 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1645 args++;
1648 /* Restore ds and es */
1649 printf( "\tpopw %%es\n" );
1650 printf( "\tpopw %%ds\n" );
1652 /* Remove the entry point from the stack */
1653 /* (we don't use add to avoid modifying the carry flag) */
1654 printf( "\tpopl %%ebp\n" );
1656 else
1658 /* Restore ds and es */
1659 printf( "\tpopw %%bp\n" ); /* Remove ip */
1660 printf( "\tpopl %%ebp\n" ); /* Remove ds and cs */
1661 printf( "\tmovw %%bp,%%ds\n" ); /* Restore ds */
1662 printf( "\tpopw %%es\n" ); /* Restore es */
1664 if (short_ret) printf( "\tpopl %%edx\n" ); /* Restore edx */
1665 else
1667 /* Get the return value into dx:ax */
1668 printf( "\tpushl %%eax\n" );
1669 printf( "\tpopw %%ax\n" );
1670 printf( "\tpopw %%dx\n" );
1671 /* Remove API entry point */
1672 printf( "\taddl $4,%%esp\n" );
1676 /* Restore bp */
1678 printf( "\tpopw %%bp\n" );
1680 /* Remove the arguments and return */
1682 if (argsize)
1684 printf( "\t.byte 0x66\n" );
1685 printf( "\tlret $%d\n", argsize );
1687 else
1689 printf( "\t.byte 0x66\n" );
1690 printf( "\tlret\n" );
1695 /*******************************************************************
1696 * BuildCallTo16Func
1698 * Build a Wine-to-16-bit callback function.
1700 * Stack frame of the callback function:
1701 * ... ...
1702 * (ebp+20) arg2
1703 * (ebp+16) arg1
1704 * (ebp+12) func to call
1705 * (ebp+8) code selector
1706 * (ebp+4) return address
1707 * (ebp) previous ebp
1709 * Prototypes for the CallTo16 functions:
1710 * extern WORD CallTo16_word_xxx( FARPROC16 func, args... );
1711 * extern LONG CallTo16_long_xxx( FARPROC16 func, args... );
1712 * extern void CallTo16_regs_( FARPROC16 func, WORD ds, WORD es, WORD bp,
1713 * WORD ax, WORD bx, WORD cx, WORD dx,
1714 * WORD si, WORD di );
1716 static void BuildCallTo16Func( char *profile )
1718 int short_ret = 0;
1719 int reg_func = 0;
1720 char *args = profile + 5;
1722 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1723 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1724 else if (strncmp( "long_", profile, 5 ))
1726 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1727 return;
1730 /* Function header */
1732 printf( "/**********\n" );
1733 printf( " * " PREFIX "CallTo16_%s\n", profile );
1734 printf( " **********/\n" );
1735 printf( "\t.align 4\n" );
1736 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1737 printf( PREFIX "CallTo16_%s:\n", profile );
1739 /* Push code selector before return address to simulate a lcall */
1741 printf( "\tpopl %%eax\n" );
1742 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1743 printf( "\tpushl %%eax\n" );
1745 /* Entry code */
1747 printf( "\tpushl %%ebp\n" );
1748 printf( "\tmovl %%esp,%%ebp\n" );
1750 /* Save the 32-bit registers */
1752 printf( "\tpushl %%ebx\n" );
1753 printf( "\tpushl %%ecx\n" );
1754 printf( "\tpushl %%edx\n" );
1755 printf( "\tpushl %%esi\n" );
1756 printf( "\tpushl %%edi\n" );
1758 /* Save the 32-bit stack */
1760 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1761 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1762 printf( "\tmovl %%ebp,%%ebx\n" );
1764 /* Print debugging info */
1766 if (debugging)
1768 /* Push the address of the first argument */
1769 printf( "\tmovl %%ebx,%%eax\n" );
1770 printf( "\taddl $12,%%eax\n" );
1771 printf( "\tpushl $%d\n", reg_func ? 8 : strlen(args) );
1772 printf( "\tpushl %%eax\n" );
1773 printf( "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1774 printf( "\tpopl %%eax\n" );
1775 printf( "\tpopl %%eax\n" );
1778 /* Switch to the 16-bit stack */
1780 #ifdef __svr4__
1781 printf("\tdata16\n");
1782 #endif
1783 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1784 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1786 /* Transfer the arguments */
1788 if (reg_func)
1790 /* Get the registers. ebx is handled later on. */
1791 printf( "\tpushw 20(%%ebx)\n" );
1792 printf( "\tpopw %%es\n" );
1793 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1794 printf( "\tmovl 28(%%ebx),%%eax\n" );
1795 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1796 printf( "\tmovl 40(%%ebx),%%edx\n" );
1797 printf( "\tmovl 44(%%ebx),%%esi\n" );
1798 printf( "\tmovl 48(%%ebx),%%edi\n" );
1800 else /* not a register function */
1802 int pos = 16; /* first argument position */
1804 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1805 printf( "\tmovzwl %%sp,%%ebp\n" );
1806 printf( "\taddw $16,%%bp\n" );
1808 while (*args)
1810 switch(*args++)
1812 case 'w': /* word */
1813 printf( "\tpushw %d(%%ebx)\n", pos );
1814 break;
1815 case 'l': /* long */
1816 printf( "\tpushl %d(%%ebx)\n", pos );
1817 break;
1819 pos += 4;
1823 /* Push the return address */
1825 printf( "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1826 short_ret ? "word" : "long" );
1828 /* Push the called routine address */
1830 printf( "\tpushl 12(%%ebx)\n" );
1832 /* Get the 16-bit ds */
1834 if (reg_func)
1836 printf( "\tpushw 16(%%ebx)\n" );
1837 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1838 printf( "\tpopw %%ds\n" );
1840 else
1842 /* Get previous ds from the 16-bit stack and */
1843 /* set ax equal to ds for window procedures. */
1844 printf( "\tmovw -10(%%ebp),%%ax\n" );
1845 #ifdef __svr4__
1846 printf( "\tdata16\n");
1847 #endif
1848 printf( "\tmovw %%ax,%%ds\n" );
1851 /* Jump to the called routine */
1853 printf( "\t.byte 0x66\n" );
1854 printf( "\tlret\n" );
1858 /*******************************************************************
1859 * BuildRet16Func
1861 * Build the return code for 16-bit callbacks
1863 static void BuildRet16Func()
1865 printf( "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1866 printf( "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1868 /* Put return value into eax */
1870 printf( PREFIX "CALLTO16_Ret_long:\n" );
1871 printf( "\tpushw %%dx\n" );
1872 printf( "\tpushw %%ax\n" );
1873 printf( "\tpopl %%eax\n" );
1874 printf( PREFIX "CALLTO16_Ret_word:\n" );
1876 /* Restore 32-bit segment registers */
1878 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1879 #ifdef __svr4__
1880 printf( "\tdata16\n");
1881 #endif
1882 printf( "\tmovw %%bx,%%ds\n" );
1883 #ifdef __svr4__
1884 printf( "\tdata16\n");
1885 #endif
1886 printf( "\tmovw %%bx,%%es\n" );
1887 #ifdef __svr4__
1888 printf( "\tdata16\n");
1889 #endif
1890 printf( "\tmovw %%bx,%%ss\n" );
1892 /* Restore the 32-bit stack */
1894 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1895 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1897 /* Restore the 32-bit registers */
1899 printf( "\tpopl %%edi\n" );
1900 printf( "\tpopl %%esi\n" );
1901 printf( "\tpopl %%edx\n" );
1902 printf( "\tpopl %%ecx\n" );
1903 printf( "\tpopl %%ebx\n" );
1905 /* Return to caller */
1907 printf( "\tpopl %%ebp\n" );
1908 printf( "\tlret\n" );
1910 /* Declare the return address variables */
1912 printf( "\t.data\n" );
1913 printf( "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
1914 printf( "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
1915 printf( PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
1916 printf( PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
1917 printf( "\t.text\n" );
1921 /*******************************************************************
1922 * BuildCallFrom32Func
1924 * Build a 32-bit-to-Wine call-back function.
1925 * 'args' is the number of dword arguments.
1927 * Stack layout:
1928 * ... ...
1929 * (ebp+12) arg2
1930 * (ebp+8) arg1
1931 * (ebp+4) ret addr
1932 * (ebp) ebp
1933 * (ebp-4) entry point
1934 * (ebp-8) func name
1936 static void BuildCallFrom32Func( const char *profile )
1938 int args, stdcall;
1940 if (!strncmp( profile, "stdcall", 7 ))
1942 stdcall = 1;
1943 args = atoi( profile + 8 );
1945 else if (!strncmp( profile, "cdecl", 5 ))
1947 stdcall = 0;
1948 args = atoi( profile + 6 );
1950 else
1952 fprintf( stderr, "Invalid function profile '%s'\n", profile );
1953 return;
1956 /* Function header */
1958 printf( "/**********\n" );
1959 printf( " * " PREFIX "CallFrom32_%s\n", profile );
1960 printf( " **********/\n" );
1961 printf( "\t.align 4\n" );
1962 printf( "\t.globl " PREFIX "CallFrom32_%s\n\n", profile );
1963 printf( PREFIX "CallFrom32_%s:\n", profile );
1965 /* Entry code */
1967 printf( "\tleal 8(%%esp),%%ebp\n" );
1969 /* Print the debugging info */
1971 if (debugging)
1973 printf( "\tpushl $%d\n", args );
1974 printf( "\tcall " PREFIX "RELAY_DebugCallFrom32\n" );
1975 printf( "\tadd $4, %%esp\n" );
1978 /* Transfer the arguments */
1980 if (args)
1982 int i;
1983 for (i = args; i > 0; i--) printf( "\tpushl %d(%%ebp)\n", 4 * i + 4 );
1985 else
1987 /* Push the address of the arguments. The called function will */
1988 /* ignore this if it really takes no arguments. */
1989 printf( "\tleal 8(%%ebp),%%eax\n" );
1990 printf( "\tpushl %%eax\n" );
1993 /* Call the function */
1995 printf( "\tcall -4(%%ebp)\n" );
1997 /* Print the debugging info */
1999 if (debugging)
2001 printf( "\tadd $%d,%%esp\n", args ? (args * 4) : 4 );
2002 printf( "\tpushl %%eax\n" );
2003 printf( "\tcall " PREFIX "RELAY_DebugCallFrom32Ret\n" );
2004 printf( "\tpopl %%eax\n" );
2007 printf( "\tmovl %%ebp,%%esp\n" );
2008 printf( "\tpopl %%ebp\n" );
2010 /* Return, removing arguments */
2012 if (args && stdcall) printf( "\tret $%d\n", args * 4 );
2013 else printf( "\tret\n" );
2017 /*******************************************************************
2018 * BuildCallTo32Func
2020 * Build a Wine-to-32-bit callback function.
2022 * Stack frame of the callback function:
2023 * ... ...
2024 * (ebp+16) arg2
2025 * (ebp+12) arg1
2026 * (ebp+8) func to call
2027 * (ebp+4) return address
2028 * (ebp) previous ebp
2030 * Prototype for the CallTo32 functions:
2031 * extern LONG CallTo32_nn( FARPROC32 func, args... );
2033 static void BuildCallTo32Func( int args )
2035 /* Function header */
2037 printf( "/**********\n" );
2038 printf( " * " PREFIX "CallTo32_%d\n", args );
2039 printf( " **********/\n" );
2040 printf( "\t.align 4\n" );
2041 printf( "\t.globl " PREFIX "CallTo32_%d\n\n", args );
2042 printf( PREFIX "CallTo32_%d:\n", args );
2044 /* Entry code */
2046 printf( "\tpushl %%ebp\n" );
2047 printf( "\tmovl %%esp,%%ebp\n" );
2049 /* Transfer arguments */
2051 if (args)
2053 int i;
2054 for (i = args; i > 0; i--) printf( "\tpushl %d(%%ebp)\n", 4 * i + 8 );
2057 /* Print the debugging output */
2059 if (debugging)
2061 printf( "\tpushl $%d\n", args );
2062 printf( "\tpushl 8(%%ebp)\n" );
2063 printf( "\tcall " PREFIX "RELAY_DebugCallTo32\n" );
2064 printf( "\taddl $8,%%esp\n" );
2067 /* Call the function */
2069 printf( "\tcall 8(%%ebp)\n" );
2071 /* Return to Wine */
2073 printf( "\tmovl %%ebp,%%esp\n" );
2074 printf( "\tpopl %%ebp\n" );
2075 printf( "\tret\n" );
2079 static void usage(void)
2081 fprintf(stderr, "usage: build -spec SPECNAMES\n"
2082 " build -callfrom16 FUNCTION_PROFILES\n"
2083 " build -callto16 FUNCTION_PROFILES\n"
2084 " build -callfrom32 FUNCTION_PROFILES\n"
2085 " build -callto32 FUNCTION_PROFILES\n" );
2086 exit(1);
2090 int main(int argc, char **argv)
2092 int i;
2094 if (argc <= 2) usage();
2096 if (!strcmp( argv[1], "-spec" ))
2098 for (i = 2; i < argc; i++) BuildSpecFiles( argv[i] );
2100 else if (!strcmp( argv[1], "-callfrom16" )) /* 16-bit-to-Wine callbacks */
2102 /* File header */
2104 printf( "/* File generated automatically. Do not edit! */\n\n" );
2105 printf( "\t.text\n" );
2107 /* Build the 32-bit large stack callback */
2109 BuildCall32LargeStack();
2111 /* Build the callback functions */
2113 for (i = 2; i < argc; i++) BuildCallFrom16Func( argv[i] );
2115 /* Output the argument debugging strings */
2117 if (debugging)
2119 printf( "/* Argument strings */\n" );
2120 for (i = 2; i < argc; i++)
2122 printf( "Profile_%s:\n", argv[i] );
2123 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
2127 else if (!strcmp( argv[1], "-callto16" )) /* Wine-to-16-bit callbacks */
2129 /* File header */
2131 printf( "/* File generated automatically. Do not edit! */\n\n" );
2132 printf( "\t.text\n" );
2133 printf( "\t.globl " PREFIX "CALLTO16_Start\n" );
2134 printf( PREFIX "CALLTO16_Start:\n" );
2136 /* Build the callback functions */
2138 for (i = 2; i < argc; i++) BuildCallTo16Func( argv[i] );
2140 /* Output the 16-bit return code */
2142 BuildRet16Func();
2144 printf( "\t.globl " PREFIX "CALLTO16_End\n" );
2145 printf( PREFIX "CALLTO16_End:\n" );
2147 else if (!strcmp( argv[1], "-callfrom32" )) /* 32-bit-to-Wine callbacks */
2149 /* File header */
2151 printf( "/* File generated automatically. Do not edit! */\n\n" );
2152 printf( "\t.text\n" );
2154 /* Build the callback functions */
2156 for (i = 2; i < argc; i++) BuildCallFrom32Func( argv[i] );
2158 else if (!strcmp( argv[1], "-callto32" )) /* Wine-to-32-bit callbacks */
2160 /* File header */
2162 printf( "/* File generated automatically. Do not edit! */\n\n" );
2163 printf( "\t.text\n" );
2165 /* Build the callback functions */
2167 for (i = 2; i < argc; i++) BuildCallTo32Func( atoi(argv[i]) );
2169 else usage();
2171 return 0;
2174 #endif /* WINELIB */