Release 960428
[wine/multimedia.git] / tools / build.c
blobac2af32376b48e779069e6fc86e0a24ad59bcde8
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 "registers.h"
14 #include "winerror.h" /* for ERROR_CALL_NOT_IMPLEMENTED */
15 #include "module.h"
16 #include "neexe.h"
17 #include "windows.h"
19 /* ELF symbols do not have an underscore in front */
20 #if defined (__ELF__) || defined (__svr4__) || defined(_SCO_DS)
21 #define PREFIX
22 #else
23 #define PREFIX "_"
24 #endif
26 #define TYPE_INVALID 0
27 #define TYPE_BYTE 1
28 #define TYPE_WORD 2
29 #define TYPE_LONG 3
30 #define TYPE_PASCAL_16 4
31 #define TYPE_PASCAL 5
32 #define TYPE_REGISTER 6
33 #define TYPE_ABS 7
34 #define TYPE_RETURN 8
35 #define TYPE_STUB 9
36 #define TYPE_STDCALL 10
38 #define MAX_ORDINALS 1299
40 /* Callback function used for stub functions */
41 #define STUB_CALLBACK \
42 ((SpecType == SPEC_WIN16) ? "RELAY_Unimplemented16": "RELAY_Unimplemented32")
44 enum SPEC_TYPE
46 SPEC_INVALID,
47 SPEC_WIN16,
48 SPEC_WIN32
51 typedef struct ordinal_definition_s
53 int type;
54 int offset;
55 char export_name[80];
56 void *additional_data;
57 } ORDDEF;
59 typedef struct ordinal_variable_definition_s
61 int n_values;
62 int *values;
63 } ORDVARDEF;
65 typedef struct ordinal_function_definition_s
67 int n_args;
68 char arg_types[32];
69 char internal_name[80];
70 } ORDFUNCDEF;
72 typedef struct ordinal_return_definition_s
74 int arg_size;
75 int ret_value;
76 } ORDRETDEF;
78 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
80 static enum SPEC_TYPE SpecType = SPEC_INVALID;
81 char DLLName[80];
82 int Limit = 0;
83 int Base = 0;
84 int HeapSize = 0;
85 FILE *SpecFp;
87 char *ParseBuffer = NULL;
88 char *ParseNext;
89 char ParseSaveChar;
90 int Line;
92 static int debugging = 1;
94 /* Offset of register relative to the end of the context struct */
95 #define CONTEXTOFFSET(reg) \
96 ((int)&reg##_reg((struct sigcontext_struct *)0) \
97 - sizeof(struct sigcontext_struct))
99 static void *xmalloc (size_t size)
101 void *res;
103 res = malloc (size ? size : 1);
104 if (res == NULL)
106 fprintf (stderr, "Virtual memory exhausted.\n");
107 exit (1);
109 return res;
113 static void *xrealloc (void *ptr, size_t size)
115 void *res = realloc (ptr, size);
116 if (res == NULL)
118 fprintf (stderr, "Virtual memory exhausted.\n");
119 exit (1);
121 return res;
125 static int IsNumberString(char *s)
127 while (*s != '\0')
128 if (!isdigit(*s++))
129 return 0;
131 return 1;
134 static char *strupper(char *s)
136 char *p;
138 for(p = s; *p != '\0'; p++)
139 *p = toupper(*p);
141 return s;
144 static char * GetTokenInLine(void)
146 char *p;
147 char *token;
149 if (ParseNext != ParseBuffer)
151 if (ParseSaveChar == '\0')
152 return NULL;
153 *ParseNext = ParseSaveChar;
157 * Remove initial white space.
159 for (p = ParseNext; isspace(*p); p++)
162 if ((*p == '\0') || (*p == '#'))
163 return NULL;
166 * Find end of token.
168 token = p++;
169 if (*token != '(' && *token != ')')
170 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
171 p++;
173 ParseSaveChar = *p;
174 ParseNext = p;
175 *p = '\0';
177 return token;
180 static char * GetToken(void)
182 char *token;
184 if (ParseBuffer == NULL)
186 ParseBuffer = xmalloc(512);
187 ParseNext = ParseBuffer;
188 Line++;
189 while (1)
191 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
192 return NULL;
193 if (ParseBuffer[0] != '#')
194 break;
198 while ((token = GetTokenInLine()) == NULL)
200 ParseNext = ParseBuffer;
201 Line++;
202 while (1)
204 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
205 return NULL;
206 if (ParseBuffer[0] != '#')
207 break;
211 return token;
214 static int ParseVariable(int ordinal, int type)
216 ORDDEF *odp;
217 ORDVARDEF *vdp;
218 char export_name[80];
219 char *token;
220 char *endptr;
221 int *value_array;
222 int n_values;
223 int value_array_size;
225 strcpy(export_name, GetToken());
227 token = GetToken();
228 if (*token != '(')
230 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
231 exit(1);
234 n_values = 0;
235 value_array_size = 25;
236 value_array = xmalloc(sizeof(*value_array) * value_array_size);
238 while ((token = GetToken()) != NULL)
240 if (*token == ')')
241 break;
243 value_array[n_values++] = strtol(token, &endptr, 0);
244 if (n_values == value_array_size)
246 value_array_size += 25;
247 value_array = xrealloc(value_array,
248 sizeof(*value_array) * value_array_size);
251 if (endptr == NULL || *endptr != '\0')
253 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
254 token);
255 exit(1);
259 if (token == NULL)
261 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
262 exit(1);
265 if (ordinal >= MAX_ORDINALS)
267 fprintf(stderr, "%d: Ordinal number too large\n", Line);
268 exit(1);
271 odp = &OrdinalDefinitions[ordinal];
272 odp->type = type;
273 strcpy(odp->export_name, export_name);
275 vdp = xmalloc(sizeof(*vdp));
276 odp->additional_data = vdp;
278 vdp->n_values = n_values;
279 vdp->values = xrealloc(value_array, sizeof(*value_array) * n_values);
281 return 0;
284 static int ParseExportFunction(int ordinal, int type)
286 char *token;
287 ORDDEF *odp;
288 ORDFUNCDEF *fdp;
289 int i;
291 odp = &OrdinalDefinitions[ordinal];
292 strcpy(odp->export_name, GetToken());
293 odp->type = type;
294 fdp = xmalloc(sizeof(*fdp));
295 odp->additional_data = fdp;
297 token = GetToken();
298 if (*token != '(')
300 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
301 exit(1);
304 for (i = 0; i < 16; i++)
306 token = GetToken();
307 if (*token == ')')
308 break;
310 if (!strcmp(token, "byte") || !strcmp(token, "word"))
311 fdp->arg_types[i] = 'w';
312 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
313 fdp->arg_types[i] = 's';
314 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
315 fdp->arg_types[i] = 'l';
316 else if (!strcmp(token, "ptr"))
317 fdp->arg_types[i] = 'p';
318 else
320 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
321 exit(1);
324 fdp->arg_types[i] = '\0';
326 strcpy(fdp->internal_name, GetToken());
327 return 0;
330 static int ParseEquate(int ordinal)
332 ORDDEF *odp;
333 char *token;
334 char *endptr;
335 int value;
337 odp = &OrdinalDefinitions[ordinal];
338 strcpy(odp->export_name, GetToken());
340 token = GetToken();
341 value = strtol(token, &endptr, 0);
342 if (endptr == NULL || *endptr != '\0')
344 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
345 token);
346 exit(1);
349 odp->type = TYPE_ABS;
350 odp->additional_data = (void *) value;
352 return 0;
355 static int ParseReturn(int ordinal)
357 ORDDEF *odp;
358 ORDRETDEF *rdp;
359 char *token;
360 char *endptr;
362 rdp = xmalloc(sizeof(*rdp));
364 odp = &OrdinalDefinitions[ordinal];
365 strcpy(odp->export_name, GetToken());
366 odp->type = TYPE_RETURN;
367 odp->additional_data = rdp;
369 token = GetToken();
370 rdp->arg_size = strtol(token, &endptr, 0);
371 if (endptr == NULL || *endptr != '\0')
373 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
374 token);
375 exit(1);
378 token = GetToken();
379 rdp->ret_value = strtol(token, &endptr, 0);
380 if (endptr == NULL || *endptr != '\0')
382 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
383 token);
384 exit(1);
387 return 0;
391 static int ParseStub( int ordinal )
393 ORDDEF *odp;
394 ORDFUNCDEF *fdp;
396 odp = &OrdinalDefinitions[ordinal];
397 strcpy( odp->export_name, GetToken() );
398 odp->type = TYPE_STUB;
399 fdp = xmalloc(sizeof(*fdp));
400 odp->additional_data = fdp;
401 fdp->arg_types[0] = '\0';
402 strcpy( fdp->internal_name, STUB_CALLBACK );
403 return 0;
407 static int ParseOrdinal(int ordinal)
409 char *token;
411 if (ordinal >= MAX_ORDINALS)
413 fprintf(stderr, "%d: Ordinal number too large\n", Line);
414 exit(1);
416 if (ordinal > Limit) Limit = ordinal;
418 token = GetToken();
419 if (token == NULL)
421 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
422 exit(1);
425 if (strcmp(token, "byte") == 0)
426 return ParseVariable(ordinal, TYPE_BYTE);
427 else if (strcmp(token, "word") == 0)
428 return ParseVariable(ordinal, TYPE_WORD);
429 else if (strcmp(token, "long") == 0)
430 return ParseVariable(ordinal, TYPE_LONG);
431 else if (strcmp(token, "p") == 0)
432 return ParseExportFunction(ordinal, TYPE_PASCAL);
433 else if (strcmp(token, "pascal") == 0)
434 return ParseExportFunction(ordinal, TYPE_PASCAL);
435 else if (strcmp(token, "pascal16") == 0)
436 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
437 else if (strcmp(token, "register") == 0)
438 return ParseExportFunction(ordinal, TYPE_REGISTER);
439 else if (strcmp(token, "stdcall") == 0)
440 return ParseExportFunction(ordinal, TYPE_STDCALL);
441 else if (strcmp(token, "equate") == 0)
442 return ParseEquate(ordinal);
443 else if (strcmp(token, "return") == 0)
444 return ParseReturn(ordinal);
445 else if (strcmp(token, "stub") == 0)
446 return ParseStub(ordinal);
447 else
449 fprintf(stderr,
450 "%d: Expected type after ordinal, found '%s' instead\n",
451 Line, token);
452 exit(1);
456 static int ParseTopLevel(void)
458 char *token;
460 while ((token = GetToken()) != NULL)
462 if (strcmp(token, "name") == 0)
464 strcpy(DLLName, GetToken());
465 strupper(DLLName);
467 else if (strcmp(token, "type") == 0)
469 token = GetToken();
470 if (!strcmp(token, "win16" )) SpecType = SPEC_WIN16;
471 else if (!strcmp(token, "win32" )) SpecType = SPEC_WIN32;
472 else
474 fprintf(stderr, "%d: Type must be 'win16' or 'win32'\n", Line);
475 exit(1);
478 else if (strcmp(token, "base") == 0)
480 token = GetToken();
481 if (!IsNumberString(token))
483 fprintf(stderr, "%d: Expected number after base\n", Line);
484 exit(1);
486 Base = atoi(token);
488 else if (strcmp(token, "heap") == 0)
490 token = GetToken();
491 if (!IsNumberString(token))
493 fprintf(stderr, "%d: Expected number after heap\n", Line);
494 exit(1);
496 HeapSize = atoi(token);
498 else if (IsNumberString(token))
500 int ordinal;
501 int rv;
503 ordinal = atoi(token);
504 if ((rv = ParseOrdinal(ordinal)) < 0)
505 return rv;
507 else
509 fprintf(stderr,
510 "%d: Expected name, id, length or ordinal\n", Line);
511 exit(1);
515 return 0;
519 /*******************************************************************
520 * StoreVariableCode
522 * Store a list of ints into a byte array.
524 static int StoreVariableCode( unsigned char *buffer, int size, ORDDEF *odp )
526 ORDVARDEF *vdp;
527 int i;
529 vdp = odp->additional_data;
530 switch(size)
532 case 1:
533 for (i = 0; i < vdp->n_values; i++)
534 buffer[i] = vdp->values[i];
535 break;
536 case 2:
537 for (i = 0; i < vdp->n_values; i++)
538 ((unsigned short *)buffer)[i] = vdp->values[i];
539 break;
540 case 4:
541 for (i = 0; i < vdp->n_values; i++)
542 ((unsigned int *)buffer)[i] = vdp->values[i];
543 break;
545 return vdp->n_values * size;
549 /*******************************************************************
550 * DumpBytes
552 * Dump a byte stream into the assembly code.
554 static void DumpBytes( const unsigned char *data, int len,
555 const char *section, const char *label_start )
557 int i;
558 if (section) printf( "\t%s\n", section );
559 if (label_start) printf( "%s:\n", label_start );
560 for (i = 0; i < len; i++)
562 if (!(i & 0x0f)) printf( "\t.byte " );
563 printf( "%d", *data++ );
564 if (i < len - 1) printf( "%c", ((i & 0x0f) != 0x0f) ? ',' : '\n' );
566 printf( "\n" );
570 /*******************************************************************
571 * BuildModule16
573 * Build the in-memory representation of a 16-bit NE module, and dump it
574 * as a byte stream into the assembly code.
576 static int BuildModule16( int max_code_offset, int max_data_offset )
578 ORDDEF *odp;
579 int i;
580 char *buffer;
581 NE_MODULE *pModule;
582 SEGTABLEENTRY *pSegment;
583 OFSTRUCT *pFileInfo;
584 BYTE *pstr, *bundle;
585 WORD *pword;
587 /* Module layout:
588 * NE_MODULE Module
589 * OFSTRUCT File information
590 * SEGTABLEENTRY Segment 1 (code)
591 * SEGTABLEENTRY Segment 2 (data)
592 * WORD[2] Resource table (empty)
593 * BYTE[2] Imported names (empty)
594 * BYTE[n] Resident names table
595 * BYTE[n] Entry table
598 buffer = xmalloc( 0x10000 );
600 pModule = (NE_MODULE *)buffer;
601 pModule->magic = NE_SIGNATURE;
602 pModule->count = 1;
603 pModule->next = 0;
604 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
605 pModule->dgroup = 2;
606 pModule->heap_size = HeapSize;
607 pModule->stack_size = 0;
608 pModule->ip = 0;
609 pModule->cs = 0;
610 pModule->sp = 0;
611 pModule->ss = 0;
612 pModule->seg_count = 2;
613 pModule->modref_count = 0;
614 pModule->nrname_size = 0;
615 pModule->modref_table = 0;
616 pModule->nrname_fpos = 0;
617 pModule->moveable_entries = 0;
618 pModule->alignment = 0;
619 pModule->truetype = 0;
620 pModule->os_flags = NE_OSFLAGS_WINDOWS;
621 pModule->misc_flags = 0;
622 pModule->dlls_to_init = 0;
623 pModule->nrname_handle = 0;
624 pModule->min_swap_area = 0;
625 pModule->expected_version = 0x030a;
626 pModule->pe_module = NULL;
627 pModule->self = 0;
628 pModule->self_loading_sel = 0;
630 /* File information */
632 pFileInfo = (OFSTRUCT *)(pModule + 1);
633 pModule->fileinfo = (int)pFileInfo - (int)pModule;
634 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
635 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
636 + strlen(DLLName) + 4;
637 sprintf( pFileInfo->szPathName, "%s.DLL", DLLName );
638 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
640 /* Segment table */
642 pSegment = (SEGTABLEENTRY *)pstr;
643 pModule->seg_table = (int)pSegment - (int)pModule;
644 pSegment->filepos = 0;
645 pSegment->size = max_code_offset;
646 pSegment->flags = 0;
647 pSegment->minsize = max_code_offset;
648 pSegment->selector = 0;
649 pSegment++;
651 pModule->dgroup_entry = (int)pSegment - (int)pModule;
652 pSegment->filepos = 0;
653 pSegment->size = max_data_offset;
654 pSegment->flags = NE_SEGFLAGS_DATA;
655 pSegment->minsize = max_data_offset;
656 pSegment->selector = 0;
657 pSegment++;
659 /* Resource table */
661 pword = (WORD *)pSegment;
662 pModule->res_table = (int)pword - (int)pModule;
663 *pword++ = 0;
664 *pword++ = 0;
666 /* Imported names table */
668 pstr = (char *)pword;
669 pModule->import_table = (int)pstr - (int)pModule;
670 *pstr++ = 0;
671 *pstr++ = 0;
673 /* Resident names table */
675 pModule->name_table = (int)pstr - (int)pModule;
676 /* First entry is module name */
677 *pstr = strlen(DLLName );
678 strcpy( pstr + 1, DLLName );
679 pstr += *pstr + 1;
680 *(WORD *)pstr = 0;
681 pstr += sizeof(WORD);
682 /* Store all ordinals */
683 odp = OrdinalDefinitions + 1;
684 for (i = 1; i <= Limit; i++, odp++)
686 if (!odp->export_name[0]) continue;
687 *pstr = strlen( odp->export_name );
688 strcpy( pstr + 1, odp->export_name );
689 strupper( pstr + 1 );
690 pstr += *pstr + 1;
691 *(WORD *)pstr = i;
692 pstr += sizeof(WORD);
694 *pstr++ = 0;
696 /* Entry table */
698 pModule->entry_table = (int)pstr - (int)pModule;
699 bundle = NULL;
700 odp = OrdinalDefinitions + 1;
701 for (i = 1; i <= Limit; i++, odp++)
703 int selector = 0;
705 switch (odp->type)
707 case TYPE_INVALID:
708 selector = 0; /* Invalid selector */
709 break;
711 case TYPE_PASCAL:
712 case TYPE_PASCAL_16:
713 case TYPE_REGISTER:
714 case TYPE_RETURN:
715 case TYPE_STUB:
716 selector = 1; /* Code selector */
717 break;
719 case TYPE_BYTE:
720 case TYPE_WORD:
721 case TYPE_LONG:
722 selector = 2; /* Data selector */
723 break;
725 case TYPE_ABS:
726 selector = 0xfe; /* Constant selector */
727 break;
730 /* create a new bundle if necessary */
731 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
733 bundle = pstr;
734 bundle[0] = 0;
735 bundle[1] = selector;
736 pstr += 2;
739 (*bundle)++;
740 if (selector != 0)
742 *pstr++ = 1;
743 *(WORD *)pstr = odp->offset;
744 pstr += sizeof(WORD);
747 *pstr++ = 0;
749 /* Dump the module content */
751 DumpBytes( (char *)pModule, (int)pstr - (int)pModule,
752 ".data", "Module_Start" );
753 return (int)pstr - (int)pModule;
757 /*******************************************************************
758 * BuildModule32
760 * Build the in-memory representation of a 32-bit pseudo-NE module, and dump it
761 * as a byte stream into the assembly code.
763 static int BuildModule32(void)
765 char *buffer;
766 NE_MODULE *pModule;
767 OFSTRUCT *pFileInfo;
768 BYTE *pstr;
769 WORD *pword;
771 /* Module layout:
772 * NE_MODULE Module
773 * OFSTRUCT File information
774 * SEGTABLEENTRY Segment table (empty)
775 * WORD[2] Resource table (empty)
776 * BYTE[2] Imported names (empty)
777 * BYTE[n] Resident names table (1 entry)
778 * BYTE[n] Entry table (empty)
781 buffer = xmalloc( 0x10000 );
783 pModule = (NE_MODULE *)buffer;
784 pModule->magic = NE_SIGNATURE;
785 pModule->count = 1;
786 pModule->next = 0;
787 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN |
788 NE_FFLAGS_LIBMODULE | NE_FFLAGS_WIN32;
789 pModule->dgroup = 0;
790 pModule->heap_size = HeapSize;
791 pModule->stack_size = 0;
792 pModule->ip = 0;
793 pModule->cs = 0;
794 pModule->sp = 0;
795 pModule->ss = 0;
796 pModule->seg_count = 0;
797 pModule->modref_count = 0;
798 pModule->nrname_size = 0;
799 pModule->modref_table = 0;
800 pModule->nrname_fpos = 0;
801 pModule->moveable_entries = 0;
802 pModule->alignment = 0;
803 pModule->truetype = 0;
804 pModule->os_flags = NE_OSFLAGS_WINDOWS;
805 pModule->misc_flags = 0;
806 pModule->dlls_to_init = 0;
807 pModule->nrname_handle = 0;
808 pModule->min_swap_area = 0;
809 pModule->expected_version = 0x030a;
810 pModule->pe_module = NULL;
811 pModule->self = 0;
812 pModule->self_loading_sel = 0;
814 /* File information */
816 pFileInfo = (OFSTRUCT *)(pModule + 1);
817 pModule->fileinfo = (int)pFileInfo - (int)pModule;
818 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
819 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
820 + strlen(DLLName) + 4;
821 sprintf( pFileInfo->szPathName, "%s.DLL", DLLName );
822 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
824 /* Segment table */
826 pModule->seg_table = (int)pstr - (int)pModule;
828 /* Resource table */
830 pword = (WORD *)pstr;
831 pModule->res_table = (int)pword - (int)pModule;
832 *pword++ = 0;
833 *pword++ = 0;
835 /* Imported names table */
837 pstr = (char *)pword;
838 pModule->import_table = (int)pstr - (int)pModule;
839 *pstr++ = 0;
840 *pstr++ = 0;
842 /* Resident names table */
844 pModule->name_table = (int)pstr - (int)pModule;
845 /* First entry is module name */
846 *pstr = strlen(DLLName );
847 strcpy( pstr + 1, DLLName );
848 pstr += *pstr + 1;
849 *(WORD *)pstr = 0;
850 pstr += sizeof(WORD);
851 *pstr++ = 0;
853 /* Entry table */
855 pModule->entry_table = (int)pstr - (int)pModule;
856 *pstr++ = 0;
858 /* Dump the module content */
860 DumpBytes( (char *)pModule, (int)pstr - (int)pModule,
861 ".data", "Module_Start" );
862 return (int)pstr - (int)pModule;
866 /*******************************************************************
867 * BuildSpec32Files
869 * Build a Win32 assembly file from a spec file.
871 static void BuildSpec32Files(void)
873 ORDDEF *odp;
874 ORDFUNCDEF *fdp;
875 ORDRETDEF *rdp;
876 int i, module_size;
878 printf( "/* File generated automatically; do not edit! */\n" );
879 printf( "\t.text\n" );
880 printf( "\t.align 4\n" );
881 printf( "Code_Start:\n\n" );
883 odp = OrdinalDefinitions;
884 for (i = 0; i <= Limit; i++, odp++)
886 fdp = odp->additional_data;
887 rdp = odp->additional_data;
889 switch (odp->type)
891 case TYPE_INVALID:
892 printf( "/* %s.%d */\n", DLLName, i );
893 printf( "\t.align 4\n" );
894 printf( "%s_%d:\n", DLLName, i );
895 printf( "\tpushl %%ebp\n" );
896 printf( "\tpushl $Name_%d\n", i );
897 printf( "\tpushl $" PREFIX "%s\n", STUB_CALLBACK );
898 printf( "\tjmp " PREFIX "CallFrom32_0\n" );
899 break;
901 case TYPE_STDCALL:
902 case TYPE_STUB:
903 printf( "/* %s.%d (%s) */\n",
904 DLLName, i, odp->export_name);
905 printf( "\t.align 4\n" );
906 printf( "%s_%d:\n", DLLName, i );
907 printf( "\tpushl %%ebp\n" );
908 printf( "\tpushl $Name_%d\n", i );
909 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
910 printf( "\tjmp " PREFIX "CallFrom32_%d\n", strlen(fdp->arg_types));
911 break;
913 case TYPE_RETURN:
914 printf( "/* %s.%d (%s) */\n",
915 DLLName, i, odp->export_name);
916 printf( "\t.align 4\n" );
917 printf( "%s_%d:\n", DLLName, i );
918 printf( "\tmovl $%d,%%eax\n", ERROR_CALL_NOT_IMPLEMENTED );
919 printf( "\tmovl %%eax," PREFIX "WIN32_LastError\n" );
920 printf( "\tmovl $%d,%%eax\n", rdp->ret_value );
921 if (rdp->arg_size) printf( "\tret $%d\n", rdp->arg_size );
922 else printf( "\tret\n" );
923 break;
925 default:
926 fprintf(stderr,"build: function type %d not available for Win32\n",
927 odp->type);
928 exit(1);
932 module_size = BuildModule32();
934 /* Output the DLL functions table */
936 printf( "\t.text\n" );
937 printf( "\t.align 4\n" );
938 printf( "Functions:\n" );
939 odp = OrdinalDefinitions;
940 for (i = 0; i <= Limit; i++, odp++) printf("\t.long %s_%d\n", DLLName, i);
942 /* Output the DLL names table */
944 printf( "FuncNames:\n" );
945 odp = OrdinalDefinitions;
946 for (i = 0; i <= Limit; i++, odp++)
948 if (odp->type == TYPE_INVALID) printf( "\t.long 0\n" );
949 else printf( "\t.long Name_%d\n", i );
952 /* Output the DLL names */
954 for (i = 0, odp = OrdinalDefinitions; i <= Limit; i++, odp++)
956 printf( "Name_%d:\t", i );
957 if (odp->type == TYPE_INVALID)
958 printf( ".ascii \"%s.%d\\0\"\n", DLLName, i );
959 else
960 printf( ".ascii \"%s\\0\"\n", odp->export_name );
963 /* Output the DLL descriptor */
965 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
966 printf( "\t.align 4\n" );
967 printf( "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
968 printf( PREFIX "%s_Descriptor:\n", DLLName );
969 printf( "\t.long DLLName\n" ); /* Name */
970 printf( "\t.long Module_Start\n" ); /* Module start */
971 printf( "\t.long %d\n", module_size ); /* Module size */
972 printf( "\t.long %d\n", Base ); /* Base */
973 printf( "\t.long %d\n", Limit ); /* Limit */
974 printf( "\t.long Functions\n" ); /* Functions */
975 printf( "\t.long FuncNames\n" ); /* Function names */
979 /*******************************************************************
980 * BuildSpec16Files
982 * Build a Win16 assembly file from a spec file.
984 static void BuildSpec16Files(void)
986 ORDDEF *odp;
987 ORDFUNCDEF *fdp;
988 ORDRETDEF *rdp;
989 int i;
990 int code_offset, data_offset, module_size;
991 unsigned char *data;
993 data = (unsigned char *)xmalloc( 0x10000 );
994 memset( data, 0, 16 );
995 data_offset = 16;
997 printf( "/* File generated automatically; do not edit! */\n" );
998 printf( "\t.text\n" );
999 printf( "Code_Start:\n" );
1000 code_offset = 0;
1002 odp = OrdinalDefinitions;
1003 for (i = 0; i <= Limit; i++, odp++)
1005 fdp = odp->additional_data;
1006 rdp = odp->additional_data;
1008 switch (odp->type)
1010 case TYPE_INVALID:
1011 odp->offset = 0xffff;
1012 break;
1014 case TYPE_ABS:
1015 odp->offset = (int)odp->additional_data & 0xffff;
1016 break;
1018 case TYPE_BYTE:
1019 printf( "/* %s.%d */\n", DLLName, i);
1020 odp->offset = data_offset;
1021 data_offset += StoreVariableCode( data, 1, odp);
1022 break;
1024 case TYPE_WORD:
1025 printf( "/* %s.%d */\n", DLLName, i);
1026 odp->offset = data_offset;
1027 data_offset += StoreVariableCode( data, 2, odp);
1028 break;
1030 case TYPE_LONG:
1031 printf( "/* %s.%d */\n", DLLName, i);
1032 odp->offset = data_offset;
1033 data_offset += StoreVariableCode( data, 4, odp);
1034 break;
1036 case TYPE_RETURN:
1037 printf( "/* %s.%d */\n", DLLName, i);
1038 printf( "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
1039 printf( "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
1040 printf( "\t.byte 0x66\n");
1041 if (rdp->arg_size != 0)
1042 printf( "\tlret $%d\n\n", rdp->arg_size);
1043 else
1045 printf( "\tlret\n");
1046 printf( "\tnop\n");
1047 printf( "\tnop\n\n");
1049 odp->offset = code_offset;
1050 code_offset += 12; /* Assembly code is 12 bytes long */
1051 break;
1053 case TYPE_REGISTER:
1054 case TYPE_PASCAL:
1055 case TYPE_PASCAL_16:
1056 case TYPE_STUB:
1057 printf( "/* %s.%d */\n", DLLName, i);
1058 printf( "\tpushw %%bp\n" );
1059 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
1060 printf( "\tlcall $0x%04x, $" PREFIX "CallFrom16_%s_%s\n",
1061 WINE_CODE_SELECTOR,
1062 (odp->type == TYPE_REGISTER) ? "regs" :
1063 (odp->type == TYPE_PASCAL) ? "long" : "word",
1064 fdp->arg_types );
1065 printf( "\tnop\n" );
1066 printf( "\tnop\n\n" );
1067 odp->offset = code_offset;
1068 code_offset += 16; /* Assembly code is 16 bytes long */
1069 break;
1071 default:
1072 fprintf(stderr,"build: function type %d not available for Win16\n",
1073 odp->type);
1074 exit(1);
1078 if (!code_offset) /* Make sure the code segment is not empty */
1080 printf( "\t.byte 0\n" );
1081 code_offset++;
1084 /* Output data segment */
1086 DumpBytes( data, data_offset, NULL, "Data_Start" );
1088 /* Build the module */
1090 module_size = BuildModule16( code_offset, data_offset );
1092 /* Output the DLL descriptor */
1094 printf( "\t.text\n" );
1095 printf( "DLLName:\t.ascii \"%s\\0\"\n", DLLName );
1096 printf( "\t.align 4\n" );
1097 printf( "\t.globl " PREFIX "%s_Descriptor\n", DLLName );
1098 printf( PREFIX "%s_Descriptor:\n", DLLName );
1099 printf( "\t.long DLLName\n" ); /* Name */
1100 printf( "\t.long Module_Start\n" ); /* Module start */
1101 printf( "\t.long %d\n", module_size ); /* Module size */
1102 printf( "\t.long Code_Start\n" ); /* Code start */
1103 printf( "\t.long Data_Start\n" ); /* Data start */
1107 /*******************************************************************
1108 * BuildSpecFiles
1110 * Build an assembly file from a spec file.
1112 static void BuildSpecFiles( char *specname )
1114 SpecFp = fopen( specname, "r");
1115 if (SpecFp == NULL)
1117 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
1118 exit(1);
1121 ParseTopLevel();
1122 switch(SpecType)
1124 case SPEC_INVALID:
1125 fprintf( stderr, "%s: Missing 'type' declaration\n", specname );
1126 exit(1);
1127 case SPEC_WIN16:
1128 BuildSpec16Files();
1129 break;
1130 case SPEC_WIN32:
1131 BuildSpec32Files();
1132 break;
1137 /*******************************************************************
1138 * BuildCall32LargeStack
1140 * Build the function used to switch to the original 32-bit stack
1141 * before calling a 32-bit function from 32-bit code. This is used for
1142 * functions that need a large stack, like X bitmaps functions.
1144 * The generated function has the following prototype:
1145 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1147 * Stack layout:
1148 * ... ...
1149 * (ebp+20) arg2
1150 * (ebp+16) arg1
1151 * (ebp+12) nbargs
1152 * (ebp+8) func
1153 * (ebp+4) ret addr
1154 * (ebp) ebp
1156 static void BuildCall32LargeStack(void)
1158 /* Function header */
1160 printf( "/**********\n" );
1161 printf( " * " PREFIX "CallTo32_LargeStack\n" );
1162 printf( " **********/\n" );
1163 printf( "\t.align 4\n" );
1164 printf( "\t.globl " PREFIX "CallTo32_LargeStack\n\n" );
1165 printf( PREFIX "CallTo32_LargeStack:\n" );
1167 /* Entry code */
1169 printf( "\tpushl %%ebp\n" );
1170 printf( "\tmovl %%esp,%%ebp\n" );
1172 /* Save registers */
1174 printf( "\tpushl %%ecx\n" );
1175 printf( "\tpushl %%esi\n" );
1176 printf( "\tpushl %%edi\n" );
1178 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1180 printf( "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1181 printf( "\torl %%eax,%%eax\n" );
1182 printf( "\tje no_orig_esp\n" );
1183 printf( "\tmovl %%eax,%%esp\n" );
1184 printf( "no_orig_esp:\n" );
1186 /* Transfer the arguments */
1188 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1189 printf( "\torl %%ecx,%%ecx\n" );
1190 printf( "\tje no_args\n" );
1191 printf( "\tleal 16(%%ebp),%%esi\n" );
1192 printf( "\tshll $2,%%ecx\n" );
1193 printf( "\tsubl %%ecx,%%esp\n" );
1194 printf( "\tmovl %%esp,%%edi\n" );
1195 printf( "\tshrl $2,%%ecx\n" );
1196 printf( "\tcld\n" );
1197 printf( "\trep; movsl\n" );
1198 printf( "no_args:\n" );
1200 /* Call the function */
1202 printf( "\tcall 8(%%ebp)\n" );
1204 /* Switch back to the normal stack */
1206 printf( "\tleal -12(%%ebp),%%esp\n" );
1208 /* Restore registers and return */
1210 printf( "\tpopl %%edi\n" );
1211 printf( "\tpopl %%esi\n" );
1212 printf( "\tpopl %%ecx\n" );
1213 printf( "\tpopl %%ebp\n" );
1214 printf( "\tret\n" );
1218 /*******************************************************************
1219 * TransferArgs16To32
1221 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1222 * The 16-bit stack layout is:
1223 * ... ...
1224 * (bp+8) arg2
1225 * (bp+6) arg1
1226 * (bp+4) cs
1227 * (bp+2) ip
1228 * (bp) bp
1230 static int TransferArgs16To32( char *args )
1232 int i, pos16, pos32;
1234 /* Save ebx first */
1236 printf( "\tpushl %%ebx\n" );
1238 /* Get the 32-bit stack pointer */
1240 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1242 /* Copy the arguments */
1244 pos16 = 6; /* skip bp and return address */
1245 pos32 = 0;
1247 for (i = strlen(args); i > 0; i--)
1249 pos32 -= 4;
1250 switch(args[i-1])
1252 case 'w': /* word */
1253 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1254 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1255 pos16 += 2;
1256 break;
1258 case 's': /* s_word */
1259 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1260 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1261 pos16 += 2;
1262 break;
1264 case 'l': /* long */
1265 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
1266 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1267 pos16 += 4;
1268 break;
1270 case 'p': /* ptr */
1271 /* Get the selector */
1272 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1273 /* Get the selector base */
1274 printf( "\tandl $0xfff8,%%eax\n" );
1275 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1276 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1277 /* Add the offset */
1278 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1279 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1280 pos16 += 4;
1281 break;
1283 default:
1284 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1288 /* Restore ebx */
1290 printf( "\tpopl %%ebx\n" );
1292 return pos16 - 6; /* Return the size of the 16-bit args */
1296 /*******************************************************************
1297 * BuildContext
1299 * Build the context structure on the 32-bit stack.
1300 * The only valid registers in the context structure are:
1301 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1303 static void BuildContext(void)
1305 /* Save ebx first */
1307 printf( "\tpushl %%ebx\n" );
1309 /* Get the 32-bit stack pointer */
1311 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1313 /* Store the registers */
1315 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EBX) ); /* Get ebx from stack*/
1316 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(EAX) );
1317 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(ECX) );
1318 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(EDX) );
1319 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(ESI) );
1320 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(EDI) );
1321 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1322 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(DS) );
1323 printf( "\tmovw -6(%%ebp),%%ax\n" ); /* Get saved es from stack */
1324 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(ES) );
1325 printf( "\tpushfl\n" );
1326 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(EFL) );
1330 /*******************************************************************
1331 * RestoreContext
1333 * Restore the registers from the context structure
1335 static void RestoreContext(void)
1337 /* Get the 32-bit stack pointer */
1339 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1341 /* Restore the registers */
1343 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(ECX) );
1344 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(EDX) );
1345 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(ESI) );
1346 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(EDI) );
1347 printf( "\tpopl %%eax\n" ); /* Remove old ds and ip from stack */
1348 printf( "\tpopl %%eax\n" ); /* Remove old cs and es from stack */
1349 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(DS) ); /* Push new ds */
1350 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(ES) ); /* Push new es */
1351 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(EFL) );
1352 printf( "\tpopfl\n" );
1353 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(EAX) );
1354 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(EBX) );
1358 /*******************************************************************
1359 * BuildCallFrom16Func
1361 * Build a 16-bit-to-Wine callback function. The syntax of the function
1362 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1363 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1364 * 'l'=long, 'p'=pointer).
1365 * For register functions, the arguments are ignored, but they are still
1366 * removed from the stack upon return.
1368 * Stack layout upon entry to the callback function:
1369 * ... ...
1370 * (sp+18) word first 16-bit arg
1371 * (sp+16) word cs
1372 * (sp+14) word ip
1373 * (sp+12) word bp
1374 * (sp+8) long 32-bit entry point
1375 * (sp+6) word high word of cs (always 0, used to store es)
1376 * (sp+4) word low word of cs of 16-bit entry point
1377 * (sp+2) word high word of ip (always 0, used to store ds)
1378 * (sp) word low word of ip of 16-bit entry point
1381 static void BuildCallFrom16Func( char *profile )
1383 int argsize = 0;
1384 int short_ret = 0;
1385 int reg_func = 0;
1386 char *args = profile + 5;
1388 /* Parse function type */
1390 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1391 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1392 else if (strncmp( "long_", profile, 5 ))
1394 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1395 return;
1398 /* Function header */
1400 printf( "/**********\n" );
1401 printf( " * " PREFIX "CallFrom16_%s\n", profile );
1402 printf( " **********/\n" );
1403 printf( "\t.align 4\n" );
1404 printf( "\t.globl " PREFIX "CallFrom16_%s\n\n", profile );
1405 printf( PREFIX "CallFrom16_%s:\n", profile );
1407 /* Setup bp to point to its copy on the stack */
1409 printf( "\tmovzwl %%sp,%%ebp\n" );
1410 printf( "\taddw $12,%%bp\n" );
1412 /* Save 16-bit ds and es */
1414 printf( "\tmovw %%ds,-10(%%ebp)\n" );
1415 printf( "\tmovw %%es,-6(%%ebp)\n" );
1417 /* Restore 32-bit ds and es */
1419 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1420 printf( "\tpopw %%ds\n" );
1421 printf( "\tpopw %%es\n" );
1424 /* Save the 16-bit stack */
1426 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1427 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1428 #ifdef __svr4__
1429 printf("\tdata16\n");
1430 #endif
1431 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1432 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1434 /* Transfer the arguments */
1436 if (reg_func) BuildContext();
1437 else if (*args) argsize = TransferArgs16To32( args );
1439 /* Get the address of the API function */
1441 printf( "\tmovl -4(%%ebp),%%eax\n" );
1443 /* If necessary, save %edx over the API function address */
1445 if (!reg_func && short_ret)
1446 printf( "\tmovl %%edx,-4(%%ebp)\n" );
1448 /* Switch to the 32-bit stack */
1450 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1451 printf( "\tpushw %%ds\n" );
1452 printf( "\tpopw %%ss\n" );
1453 printf( "\tleal -%d(%%ebp),%%esp\n",
1454 reg_func ? sizeof(struct sigcontext_struct) : 4 * strlen(args) );
1456 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1458 printf( "\taddl $24,%%ebp\n" );
1460 /* Print the debug information before the call */
1462 if (debugging)
1464 printf( "\tpushl %%eax\n" );
1465 printf( "\tpushl $Profile_%s\n", profile );
1466 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1467 printf( "\tcall " PREFIX "RELAY_DebugCallFrom16\n" );
1468 printf( "\tpopl %%eax\n" );
1469 printf( "\tpopl %%eax\n" );
1470 printf( "\tpopl %%eax\n" );
1473 /* Call the entry point */
1475 printf( "\tcall %%eax\n" );
1477 /* Print the debug information after the call */
1479 if (debugging)
1481 printf( "\tpushl %%eax\n" );
1482 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1483 printf( "\tcall " PREFIX "RELAY_DebugCallFrom16Ret\n" );
1484 printf( "\tpopl %%eax\n" );
1485 printf( "\tpopl %%eax\n" );
1488 /* Restore the 16-bit stack */
1490 #ifdef __svr4__
1491 printf( "\tdata16\n");
1492 #endif
1493 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1494 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1495 #ifdef __svr4__
1496 printf( "\tdata16\n");
1497 #endif
1498 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1499 #ifdef __svr4__
1500 printf( "\tdata16\n");
1501 #endif
1502 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1504 if (reg_func)
1506 /* Restore registers from the context structure */
1507 RestoreContext();
1509 /* Calc the arguments size */
1510 while (*args)
1512 switch(*args)
1514 case 'w':
1515 case 's':
1516 argsize += 2;
1517 break;
1518 case 'p':
1519 case 'l':
1520 argsize += 4;
1521 break;
1522 default:
1523 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1525 args++;
1528 /* Restore ds and es */
1529 printf( "\tpopw %%es\n" );
1530 printf( "\tpopw %%ds\n" );
1532 /* Remove the entry point from the stack */
1533 /* (we don't use add to avoid modifying the carry flag) */
1534 printf( "\tpopl %%ebp\n" );
1536 else
1538 /* Restore ds and es */
1539 printf( "\tpopw %%bp\n" ); /* Remove ip */
1540 printf( "\tpopl %%ebp\n" ); /* Remove ds and cs */
1541 printf( "\tmovw %%bp,%%ds\n" ); /* Restore ds */
1542 printf( "\tpopw %%es\n" ); /* Restore es */
1544 if (short_ret) printf( "\tpopl %%edx\n" ); /* Restore edx */
1545 else
1547 /* Get the return value into dx:ax */
1548 printf( "\tpushl %%eax\n" );
1549 printf( "\tpopw %%ax\n" );
1550 printf( "\tpopw %%dx\n" );
1551 /* Remove API entry point */
1552 printf( "\taddl $4,%%esp\n" );
1556 /* Restore bp */
1558 printf( "\tpopw %%bp\n" );
1560 /* Remove the arguments and return */
1562 if (argsize)
1564 printf( "\t.byte 0x66\n" );
1565 printf( "\tlret $%d\n", argsize );
1567 else
1569 printf( "\t.byte 0x66\n" );
1570 printf( "\tlret\n" );
1575 /*******************************************************************
1576 * BuildCallTo16Func
1578 * Build a Wine-to-16-bit callback function.
1580 * Stack frame of the callback function:
1581 * ... ...
1582 * (ebp+24) arg2
1583 * (ebp+20) arg1
1584 * (ebp+16) 16-bit ds
1585 * (ebp+12) func to call
1586 * (ebp+8) code selector
1587 * (ebp+4) return address
1588 * (ebp) previous ebp
1590 * Prototypes for the CallTo16 functions:
1591 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1592 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1593 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1594 * WORD ax, WORD bx, WORD cx, WORD dx,
1595 * WORD si, WORD di );
1597 static void BuildCallTo16Func( char *profile )
1599 int short_ret = 0;
1600 int reg_func = 0;
1601 char *args = profile + 5;
1603 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1604 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1605 else if (strncmp( "long_", profile, 5 ))
1607 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1608 return;
1611 /* Function header */
1613 printf( "/**********\n" );
1614 printf( " * " PREFIX "CallTo16_%s\n", profile );
1615 printf( " **********/\n" );
1616 printf( "\t.align 4\n" );
1617 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1618 printf( PREFIX "CallTo16_%s:\n", profile );
1620 /* Push code selector before return address to simulate a lcall */
1622 printf( "\tpopl %%eax\n" );
1623 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1624 printf( "\tpushl %%eax\n" );
1626 /* Entry code */
1628 printf( "\tpushl %%ebp\n" );
1629 printf( "\tmovl %%esp,%%ebp\n" );
1631 /* Save the 32-bit registers */
1633 printf( "\tpushl %%ebx\n" );
1634 printf( "\tpushl %%ecx\n" );
1635 printf( "\tpushl %%edx\n" );
1636 printf( "\tpushl %%esi\n" );
1637 printf( "\tpushl %%edi\n" );
1639 /* Save the 32-bit stack */
1641 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1642 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1643 printf( "\tmovl %%ebp,%%ebx\n" );
1645 /* Print debugging info */
1647 if (debugging)
1649 /* Push the address of the first argument */
1650 printf( "\tmovl %%ebx,%%eax\n" );
1651 printf( "\taddl $12,%%eax\n" );
1652 printf( "\tpushl $%d\n", reg_func ? 8 : strlen(args) );
1653 printf( "\tpushl %%eax\n" );
1654 printf( "\tcall " PREFIX "RELAY_DebugCallTo16\n" );
1655 printf( "\tpopl %%eax\n" );
1656 printf( "\tpopl %%eax\n" );
1659 /* Switch to the 16-bit stack */
1661 #ifdef __svr4__
1662 printf("\tdata16\n");
1663 #endif
1664 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1665 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1667 /* Transfer the arguments */
1669 if (reg_func)
1671 /* Get the registers. ebx is handled later on. */
1672 printf( "\tpushw 20(%%ebx)\n" );
1673 printf( "\tpopw %%es\n" );
1674 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1675 printf( "\tmovl 28(%%ebx),%%eax\n" );
1676 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1677 printf( "\tmovl 40(%%ebx),%%edx\n" );
1678 printf( "\tmovl 44(%%ebx),%%esi\n" );
1679 printf( "\tmovl 48(%%ebx),%%edi\n" );
1681 else /* not a register function */
1683 int pos = 20; /* first argument position */
1685 /* Make %bp point to the previous stackframe (built by CallFrom16) */
1686 printf( "\tmovw %%sp,%%bp\n" );
1687 printf( "\taddw $16,%%bp\n" );
1689 while (*args)
1691 switch(*args++)
1693 case 'w': /* word */
1694 printf( "\tpushw %d(%%ebx)\n", pos );
1695 break;
1696 case 'l': /* long */
1697 printf( "\tpushl %d(%%ebx)\n", pos );
1698 break;
1700 pos += 4;
1704 /* Push the return address */
1706 printf( "\tpushl " PREFIX "CALLTO16_RetAddr_%s\n",
1707 short_ret ? "word" : "long" );
1709 /* Push the called routine address */
1711 printf( "\tpushl 12(%%ebx)\n" );
1713 /* Get the 16-bit ds */
1715 if (reg_func)
1717 printf( "\tpushw 16(%%ebx)\n" );
1718 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1719 printf( "\tpopw %%ds\n" );
1721 else
1723 /* Set ax equal to ds for window procedures */
1724 printf( "\tmovw 16(%%ebx),%%ax\n" );
1725 #ifdef __svr4__
1726 printf( "\tdata16\n");
1727 #endif
1728 printf( "\tmovw %%ax,%%ds\n" );
1731 /* Jump to the called routine */
1733 printf( "\t.byte 0x66\n" );
1734 printf( "\tlret\n" );
1738 /*******************************************************************
1739 * BuildRet16Func
1741 * Build the return code for 16-bit callbacks
1743 static void BuildRet16Func()
1745 printf( "\t.globl " PREFIX "CALLTO16_Ret_word\n" );
1746 printf( "\t.globl " PREFIX "CALLTO16_Ret_long\n" );
1748 /* Put return value into eax */
1750 printf( PREFIX "CALLTO16_Ret_long:\n" );
1751 printf( "\tpushw %%dx\n" );
1752 printf( "\tpushw %%ax\n" );
1753 printf( "\tpopl %%eax\n" );
1754 printf( PREFIX "CALLTO16_Ret_word:\n" );
1756 /* Restore 32-bit segment registers */
1758 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1759 #ifdef __svr4__
1760 printf( "\tdata16\n");
1761 #endif
1762 printf( "\tmovw %%bx,%%ds\n" );
1763 #ifdef __svr4__
1764 printf( "\tdata16\n");
1765 #endif
1766 printf( "\tmovw %%bx,%%es\n" );
1767 #ifdef __svr4__
1768 printf( "\tdata16\n");
1769 #endif
1770 printf( "\tmovw %%bx,%%ss\n" );
1772 /* Restore the 32-bit stack */
1774 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1775 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1777 /* Restore the 32-bit registers */
1779 printf( "\tpopl %%edi\n" );
1780 printf( "\tpopl %%esi\n" );
1781 printf( "\tpopl %%edx\n" );
1782 printf( "\tpopl %%ecx\n" );
1783 printf( "\tpopl %%ebx\n" );
1785 /* Return to caller */
1787 printf( "\tpopl %%ebp\n" );
1788 printf( "\tlret\n" );
1790 /* Declare the return address variables */
1792 printf( "\t.data\n" );
1793 printf( "\t.globl " PREFIX "CALLTO16_RetAddr_word\n" );
1794 printf( "\t.globl " PREFIX "CALLTO16_RetAddr_long\n" );
1795 printf( PREFIX "CALLTO16_RetAddr_word:\t.long 0\n" );
1796 printf( PREFIX "CALLTO16_RetAddr_long:\t.long 0\n" );
1797 printf( "\t.text\n" );
1801 /*******************************************************************
1802 * BuildCallFrom32Func
1804 * Build a 32-bit-to-Wine call-back function.
1805 * 'args' is the number of dword arguments.
1807 * Stack layout:
1808 * ... ...
1809 * (ebp+12) arg2
1810 * (ebp+8) arg1
1811 * (ebp+4) ret addr
1812 * (ebp) ebp
1813 * (ebp-4) func name
1814 * (ebp-8) entry point
1816 static void BuildCallFrom32Func( int args )
1818 /* Function header */
1820 printf( "/**********\n" );
1821 printf( " * " PREFIX "CallFrom32_%d\n", args );
1822 printf( " **********/\n" );
1823 printf( "\t.align 4\n" );
1824 printf( "\t.globl " PREFIX "CallFrom32_%d\n\n", args );
1825 printf( PREFIX "CallFrom32_%d:\n", args );
1827 /* Entry code */
1829 printf( "\tleal 8(%%esp),%%ebp\n" );
1831 /* Print the debugging info */
1833 if (debugging)
1835 printf( "\tpushl $%d\n", args );
1836 printf( "\tcall " PREFIX "RELAY_DebugCallFrom32\n" );
1837 printf( "\tadd $4, %%esp\n" );
1840 /* Transfer the arguments */
1842 if (args)
1844 int i;
1845 for (i = args; i > 0; i--) printf( "\tpushl %d(%%ebp)\n", 4 * i + 4 );
1847 else
1849 /* Push the address of the arguments. The called function will */
1850 /* ignore this if it really takes no arguments. */
1851 printf( "\tleal 8(%%ebp),%%eax\n" );
1852 printf( "\tpushl %%eax\n" );
1855 /* Call the function */
1857 printf( "\tcall -8(%%ebp)\n" );
1859 /* Print the debugging info */
1861 if (debugging)
1863 printf( "\tadd $%d,%%esp\n", args ? (args * 4) : 4 );
1864 printf( "\tpushl %%eax\n" );
1865 printf( "\tcall " PREFIX "RELAY_DebugCallFrom32Ret\n" );
1866 printf( "\tpopl %%eax\n" );
1869 printf( "\tmovl %%ebp,%%esp\n" );
1870 printf( "\tpopl %%ebp\n" );
1872 /* Return, removing arguments */
1874 if (args) printf( "\tret $%d\n", args * 4 );
1875 else printf( "\tret\n" );
1879 /*******************************************************************
1880 * BuildCallTo32Func
1882 * Build a Wine-to-32-bit callback function.
1884 * Stack frame of the callback function:
1885 * ... ...
1886 * (ebp+16) arg2
1887 * (ebp+12) arg1
1888 * (ebp+8) func to call
1889 * (ebp+4) return address
1890 * (ebp) previous ebp
1892 * Prototype for the CallTo32 functions:
1893 * extern LONG CallTo32_nn( FARPROC func, args... );
1895 static void BuildCallTo32Func( int args )
1897 /* Function header */
1899 printf( "/**********\n" );
1900 printf( " * " PREFIX "CallTo32_%d\n", args );
1901 printf( " **********/\n" );
1902 printf( "\t.align 4\n" );
1903 printf( "\t.globl " PREFIX "CallTo32_%d\n\n", args );
1904 printf( PREFIX "CallTo32_%d:\n", args );
1906 /* Entry code */
1908 printf( "\tpushl %%ebp\n" );
1909 printf( "\tmovl %%esp,%%ebp\n" );
1911 /* Transfer arguments */
1913 if (args)
1915 int i;
1916 for (i = args; i > 0; i--) printf( "\tpushl %d(%%ebp)\n", 4 * i + 8 );
1919 /* Print the debugging output */
1921 if (debugging)
1923 printf( "\tpushl $%d\n", args );
1924 printf( "\tpushl 8(%%ebp)\n" );
1925 printf( "\tcall " PREFIX "RELAY_DebugCallTo32\n" );
1926 printf( "\taddl $8,%%esp\n" );
1929 /* Call the function */
1931 printf( "\tcall 8(%%ebp)\n" );
1933 /* Return to Wine */
1935 printf( "\tmovl %%ebp,%%esp\n" );
1936 printf( "\tpopl %%ebp\n" );
1937 printf( "\tret\n" );
1941 static void usage(void)
1943 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1944 " build -callfrom16 FUNCTION_PROFILES\n"
1945 " build -callto16 FUNCTION_PROFILES\n"
1946 " build -callfrom32 FUNCTION_PROFILES\n"
1947 " build -callto32 FUNCTION_PROFILES\n" );
1948 exit(1);
1952 int main(int argc, char **argv)
1954 int i;
1956 if (argc <= 2) usage();
1958 if (!strcmp( argv[1], "-spec" ))
1960 for (i = 2; i < argc; i++) BuildSpecFiles( argv[i] );
1962 else if (!strcmp( argv[1], "-callfrom16" )) /* 16-bit-to-Wine callbacks */
1964 /* File header */
1966 printf( "/* File generated automatically. Do not edit! */\n\n" );
1967 printf( "\t.text\n" );
1969 /* Build the 32-bit large stack callback */
1971 BuildCall32LargeStack();
1973 /* Build the callback functions */
1975 for (i = 2; i < argc; i++) BuildCallFrom16Func( argv[i] );
1977 /* Output the argument debugging strings */
1979 if (debugging)
1981 printf( "/* Argument strings */\n" );
1982 for (i = 2; i < argc; i++)
1984 printf( "Profile_%s:\n", argv[i] );
1985 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1989 else if (!strcmp( argv[1], "-callto16" )) /* Wine-to-16-bit callbacks */
1991 /* File header */
1993 printf( "/* File generated automatically. Do not edit! */\n\n" );
1994 printf( "\t.text\n" );
1995 printf( "\t.globl " PREFIX "CALLTO16_Start\n" );
1996 printf( PREFIX "CALLTO16_Start:\n" );
1998 /* Build the callback functions */
2000 for (i = 2; i < argc; i++) BuildCallTo16Func( argv[i] );
2002 /* Output the 16-bit return code */
2004 BuildRet16Func();
2006 printf( "\t.globl " PREFIX "CALLTO16_End\n" );
2007 printf( PREFIX "CALLTO16_End:\n" );
2009 else if (!strcmp( argv[1], "-callfrom32" )) /* 32-bit-to-Wine callbacks */
2011 /* File header */
2013 printf( "/* File generated automatically. Do not edit! */\n\n" );
2014 printf( "\t.text\n" );
2016 /* Build the callback functions */
2018 for (i = 2; i < argc; i++) BuildCallFrom32Func( atoi(argv[i]) );
2020 else if (!strcmp( argv[1], "-callto32" )) /* Wine-to-32-bit callbacks */
2022 /* File header */
2024 printf( "/* File generated automatically. Do not edit! */\n\n" );
2025 printf( "\t.text\n" );
2027 /* Build the callback functions */
2029 for (i = 2; i < argc; i++) BuildCallTo32Func( atoi(argv[i]) );
2031 else usage();
2033 return 0;
2036 #endif /* WINELIB */