Release 950918
[wine/hacks.git] / tools / build.c
blob0221c496dfa12e4735d00d130070a3e248334d2d
1 /*
2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
4 * Copyright 1995 Martin von Loewis
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <ctype.h>
11 #include "wine.h"
12 #include "module.h"
13 #include "neexe.h"
15 /* ELF symbols do not have an underscore in front */
16 #ifdef __ELF__
17 #define PREFIX
18 #else
19 #define PREFIX "_"
20 #endif
22 #define TYPE_INVALID 0
23 #define TYPE_BYTE 1
24 #define TYPE_WORD 2
25 #define TYPE_LONG 3
26 #define TYPE_PASCAL_16 4
27 #define TYPE_PASCAL 5
28 #define TYPE_REGISTER 6
29 #define TYPE_ABS 7
30 #define TYPE_RETURN 8
31 #define TYPE_STUB 9
32 #define TYPE_STDCALL 10
34 #define MAX_ORDINALS 1299
36 /* Callback function used for stub functions */
37 #define STUB_CALLBACK "RELAY_Unimplemented"
39 typedef struct ordinal_definition_s
41 int type;
42 int offset;
43 char export_name[80];
44 void *additional_data;
45 } ORDDEF;
47 typedef struct ordinal_variable_definition_s
49 int n_values;
50 int *values;
51 } ORDVARDEF;
53 typedef struct ordinal_function_definition_s
55 int n_args;
56 char arg_types[32];
57 char internal_name[80];
58 } ORDFUNCDEF;
60 typedef struct ordinal_return_definition_s
62 int arg_size;
63 int ret_value;
64 } ORDRETDEF;
66 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
68 char LowerDLLName[80];
69 char UpperDLLName[80];
70 int Limit = 0;
71 int DLLId;
72 int Base = 0;
73 FILE *SpecFp;
75 char *ParseBuffer = NULL;
76 char *ParseNext;
77 char ParseSaveChar;
78 int Line;
80 static int debugging = 1;
82 /* Offset of register relative to the end of the context struct */
83 #define CONTEXTOFFSET(reg) \
84 ((int)&(((struct sigcontext_struct *)1)->reg) - 1 \
85 - sizeof(struct sigcontext_struct))
87 static int IsNumberString(char *s)
89 while (*s != '\0')
90 if (!isdigit(*s++))
91 return 0;
93 return 1;
96 static char *strlower(char *s)
98 char *p;
100 for(p = s; *p != '\0'; p++)
101 *p = tolower(*p);
103 return s;
106 static char *strupper(char *s)
108 char *p;
110 for(p = s; *p != '\0'; p++)
111 *p = toupper(*p);
113 return s;
116 static char * GetTokenInLine(void)
118 char *p;
119 char *token;
121 if (ParseNext != ParseBuffer)
123 if (ParseSaveChar == '\0')
124 return NULL;
125 *ParseNext = ParseSaveChar;
129 * Remove initial white space.
131 for (p = ParseNext; isspace(*p); p++)
134 if ((*p == '\0') || (*p == '#'))
135 return NULL;
138 * Find end of token.
140 token = p++;
141 if (*token != '(' && *token != ')')
142 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
143 p++;
145 ParseSaveChar = *p;
146 ParseNext = p;
147 *p = '\0';
149 return token;
152 static char * GetToken(void)
154 char *token;
156 if (ParseBuffer == NULL)
158 ParseBuffer = malloc(512);
159 ParseNext = ParseBuffer;
160 Line++;
161 while (1)
163 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
164 return NULL;
165 if (ParseBuffer[0] != '#')
166 break;
170 while ((token = GetTokenInLine()) == NULL)
172 ParseNext = ParseBuffer;
173 Line++;
174 while (1)
176 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
177 return NULL;
178 if (ParseBuffer[0] != '#')
179 break;
183 return token;
186 static int ParseVariable(int ordinal, int type)
188 ORDDEF *odp;
189 ORDVARDEF *vdp;
190 char export_name[80];
191 char *token;
192 char *endptr;
193 int *value_array;
194 int n_values;
195 int value_array_size;
197 strcpy(export_name, GetToken());
199 token = GetToken();
200 if (*token != '(')
202 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
203 exit(1);
206 n_values = 0;
207 value_array_size = 25;
208 value_array = malloc(sizeof(*value_array) * value_array_size);
210 while ((token = GetToken()) != NULL)
212 if (*token == ')')
213 break;
215 value_array[n_values++] = strtol(token, &endptr, 0);
216 if (n_values == value_array_size)
218 value_array_size += 25;
219 value_array = realloc(value_array,
220 sizeof(*value_array) * value_array_size);
223 if (endptr == NULL || *endptr != '\0')
225 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
226 token);
227 exit(1);
231 if (token == NULL)
233 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
234 exit(1);
237 if (ordinal >= MAX_ORDINALS)
239 fprintf(stderr, "%d: Ordinal number too large\n", Line);
240 exit(1);
243 odp = &OrdinalDefinitions[ordinal];
244 odp->type = type;
245 strcpy(odp->export_name, export_name);
247 vdp = malloc(sizeof(*vdp));
248 odp->additional_data = vdp;
250 vdp->n_values = n_values;
251 vdp->values = realloc(value_array, sizeof(*value_array) * n_values);
253 return 0;
256 static int ParseExportFunction(int ordinal, int type)
258 char *token;
259 ORDDEF *odp;
260 ORDFUNCDEF *fdp;
261 int i;
263 odp = &OrdinalDefinitions[ordinal];
264 strcpy(odp->export_name, GetToken());
265 odp->type = type;
266 fdp = malloc(sizeof(*fdp));
267 odp->additional_data = fdp;
269 token = GetToken();
270 if (*token != '(')
272 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
273 exit(1);
276 for (i = 0; i < 16; i++)
278 token = GetToken();
279 if (*token == ')')
280 break;
282 if (!strcmp(token, "byte") || !strcmp(token, "word"))
283 fdp->arg_types[i] = 'w';
284 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
285 fdp->arg_types[i] = 's';
286 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
287 fdp->arg_types[i] = 'l';
288 else if (!strcmp(token, "ptr"))
289 fdp->arg_types[i] = 'p';
290 else
292 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
293 exit(1);
296 fdp->arg_types[i] = '\0';
298 strcpy(fdp->internal_name, GetToken());
299 return 0;
302 static int ParseEquate(int ordinal)
304 ORDDEF *odp;
305 char *token;
306 char *endptr;
307 int value;
309 odp = &OrdinalDefinitions[ordinal];
310 strcpy(odp->export_name, GetToken());
312 token = GetToken();
313 value = strtol(token, &endptr, 0);
314 if (endptr == NULL || *endptr != '\0')
316 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
317 token);
318 exit(1);
321 odp->type = TYPE_ABS;
322 odp->additional_data = (void *) value;
324 return 0;
327 static int ParseReturn(int ordinal)
329 ORDDEF *odp;
330 ORDRETDEF *rdp;
331 char *token;
332 char *endptr;
334 rdp = malloc(sizeof(*rdp));
336 odp = &OrdinalDefinitions[ordinal];
337 strcpy(odp->export_name, GetToken());
338 odp->type = TYPE_RETURN;
339 odp->additional_data = rdp;
341 token = GetToken();
342 rdp->arg_size = strtol(token, &endptr, 0);
343 if (endptr == NULL || *endptr != '\0')
345 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
346 token);
347 exit(1);
350 token = GetToken();
351 rdp->ret_value = strtol(token, &endptr, 0);
352 if (endptr == NULL || *endptr != '\0')
354 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
355 token);
356 exit(1);
359 return 0;
363 static int ParseStub( int ordinal )
365 ORDDEF *odp;
366 ORDFUNCDEF *fdp;
368 odp = &OrdinalDefinitions[ordinal];
369 strcpy( odp->export_name, GetToken() );
370 odp->type = TYPE_STUB;
371 fdp = malloc(sizeof(*fdp));
372 odp->additional_data = fdp;
373 fdp->arg_types[0] = '\0';
374 strcpy( fdp->internal_name, STUB_CALLBACK );
375 return 0;
379 static int ParseOrdinal(int ordinal)
381 char *token;
383 if (ordinal >= MAX_ORDINALS)
385 fprintf(stderr, "%d: Ordinal number too large\n", Line);
386 exit(1);
388 if (ordinal > Limit) Limit = ordinal;
390 token = GetToken();
391 if (token == NULL)
393 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
394 exit(1);
397 if (strcmp(token, "byte") == 0)
398 return ParseVariable(ordinal, TYPE_BYTE);
399 else if (strcmp(token, "word") == 0)
400 return ParseVariable(ordinal, TYPE_WORD);
401 else if (strcmp(token, "long") == 0)
402 return ParseVariable(ordinal, TYPE_LONG);
403 else if (strcmp(token, "p") == 0)
404 return ParseExportFunction(ordinal, TYPE_PASCAL);
405 else if (strcmp(token, "pascal") == 0)
406 return ParseExportFunction(ordinal, TYPE_PASCAL);
407 else if (strcmp(token, "pascal16") == 0)
408 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
409 else if (strcmp(token, "register") == 0)
410 return ParseExportFunction(ordinal, TYPE_REGISTER);
411 else if (strcmp(token, "stdcall") == 0)
412 return ParseExportFunction(ordinal, TYPE_STDCALL);
413 else if (strcmp(token, "equate") == 0)
414 return ParseEquate(ordinal);
415 else if (strcmp(token, "return") == 0)
416 return ParseReturn(ordinal);
417 else if (strcmp(token, "stub") == 0)
418 return ParseStub(ordinal);
419 else
421 fprintf(stderr,
422 "%d: Expected type after ordinal, found '%s' instead\n",
423 Line, token);
424 exit(1);
428 static int ParseTopLevel(void)
430 char *token;
432 while ((token = GetToken()) != NULL)
434 if (strcmp(token, "name") == 0)
436 strcpy(LowerDLLName, GetToken());
437 strlower(LowerDLLName);
439 strcpy(UpperDLLName, LowerDLLName);
440 strupper(UpperDLLName);
442 else if (strcmp(token, "id") == 0)
444 token = GetToken();
445 if (!IsNumberString(token))
447 fprintf(stderr, "%d: Expected number after id\n", Line);
448 exit(1);
451 DLLId = atoi(token);
453 else if (strcmp(token, "base") == 0)
455 token = GetToken();
456 if (!IsNumberString(token))
458 fprintf(stderr, "%d: Expected number after base\n", Line);
459 exit(1);
462 Base = atoi(token);
464 else if (IsNumberString(token))
466 int ordinal;
467 int rv;
469 ordinal = atoi(token);
470 if ((rv = ParseOrdinal(ordinal)) < 0)
471 return rv;
473 else
475 fprintf(stderr,
476 "%d: Expected name, id, length or ordinal\n", Line);
477 exit(1);
481 return 0;
485 static int OutputVariableCode( char *storage, ORDDEF *odp )
487 ORDVARDEF *vdp;
488 int i;
490 vdp = odp->additional_data;
491 printf( "\t.data\n" );
492 for (i = 0; i < vdp->n_values; i++)
494 if ((i & 7) == 0)
495 printf( "\t%s\t", storage);
497 printf( "%d", vdp->values[i]);
499 if ((i & 7) == 7 || i == vdp->n_values - 1) printf( "\n");
500 else printf( ", ");
502 printf( "\n");
503 printf( "\t.text\n" );
504 return vdp->n_values;
508 /*******************************************************************
509 * BuildModule
511 * Build the in-memory representation of the module, and dump it
512 * as a byte stream into the assembly code.
514 static void BuildModule( int max_code_offset, int max_data_offset )
516 ORDDEF *odp;
517 int i, size;
518 char *buffer;
519 NE_MODULE *pModule;
520 SEGTABLEENTRY *pSegment;
521 LOADEDFILEINFO *pFileInfo;
522 BYTE *pstr, *bundle;
523 WORD *pword;
525 /* Module layout:
526 * NE_MODULE Module
527 * LOADEDFILEINFO File information
528 * SEGTABLEENTRY Segment 1 (code)
529 * SEGTABLEENTRY Segment 2 (data)
530 * WORD[2] Resource table (empty)
531 * BYTE[2] Imported names (empty)
532 * BYTE[n] Resident names table
533 * BYTE[n] Entry table
536 buffer = malloc( 0x10000 );
538 pModule = (NE_MODULE *)buffer;
539 pModule->magic = NE_SIGNATURE;
540 pModule->count = 1;
541 pModule->next = 0;
542 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE;
543 pModule->dgroup = 2;
544 pModule->heap_size = 0xffff;
545 pModule->stack_size = 0;
546 pModule->ip = 0;
547 pModule->cs = 0;
548 pModule->sp = 0;
549 pModule->ss = 0;
550 pModule->seg_count = 2;
551 pModule->modref_count = 0;
552 pModule->nrname_size = 0;
553 pModule->modref_table = 0;
554 pModule->nrname_fpos = 0;
555 pModule->moveable_entries = 0;
556 pModule->alignment = 0;
557 pModule->truetype = 0;
558 pModule->os_flags = NE_OSFLAGS_WINDOWS;
559 pModule->misc_flags = 0;
560 pModule->dlls_to_init = 0;
561 pModule->nrname_handle = 0;
562 pModule->min_swap_area = 0;
563 pModule->expected_version = 0x030a;
565 /* File information */
567 pFileInfo = (LOADEDFILEINFO *)(pModule + 1);
568 pModule->fileinfo = (int)pFileInfo - (int)pModule;
569 pFileInfo->length = sizeof(LOADEDFILEINFO) + strlen(UpperDLLName) + 3;
570 pFileInfo->fixed_media = 0;
571 pFileInfo->error = 0;
572 pFileInfo->date = 0;
573 pFileInfo->time = 0;
574 sprintf( pFileInfo->filename, "%s.DLL", UpperDLLName );
575 pstr = (char *)pFileInfo + pFileInfo->length + 1;
577 /* Segment table */
579 pSegment = (SEGTABLEENTRY *)pstr;
580 pModule->seg_table = (int)pSegment - (int)pModule;
581 pSegment->filepos = 0;
582 pSegment->size = max_code_offset;
583 pSegment->flags = 0;
584 pSegment->minsize = max_code_offset;
585 pSegment->selector = 0;
586 pSegment++;
588 pModule->dgroup_entry = (int)pSegment - (int)pModule;
589 pSegment->filepos = 0;
590 pSegment->size = max_data_offset;
591 pSegment->flags = NE_SEGFLAGS_DATA;
592 pSegment->minsize = max_data_offset;
593 pSegment->selector = 0;
594 pSegment++;
596 /* Resource table */
598 pword = (WORD *)pSegment;
599 pModule->res_table = (int)pword - (int)pModule;
600 *pword++ = 0;
601 *pword++ = 0;
603 /* Imported names table */
605 pstr = (char *)pword;
606 pModule->import_table = (int)pstr - (int)pModule;
607 *pstr++ = 0;
608 *pstr++ = 0;
610 /* Resident names table */
612 pModule->name_table = (int)pstr - (int)pModule;
613 /* First entry is module name */
614 *pstr = strlen(UpperDLLName );
615 strcpy( pstr + 1, UpperDLLName );
616 pstr += *pstr + 1;
617 *(WORD *)pstr = 0;
618 pstr += sizeof(WORD);
619 /* Store all ordinals */
620 odp = OrdinalDefinitions + 1;
621 for (i = 1; i <= Limit; i++, odp++)
623 if (!odp->export_name[0]) continue;
624 *pstr = strlen( odp->export_name );
625 strcpy( pstr + 1, odp->export_name );
626 strupper( pstr + 1 );
627 pstr += *pstr + 1;
628 *(WORD *)pstr = i;
629 pstr += sizeof(WORD);
631 *pstr++ = 0;
633 /* Entry table */
635 pModule->entry_table = (int)pstr - (int)pModule;
636 bundle = NULL;
637 odp = OrdinalDefinitions + 1;
638 for (i = 1; i <= Limit; i++, odp++)
640 int selector = 0;
642 switch (odp->type)
644 case TYPE_INVALID:
645 selector = 0; /* Invalid selector */
646 break;
648 case TYPE_PASCAL:
649 case TYPE_PASCAL_16:
650 case TYPE_REGISTER:
651 case TYPE_RETURN:
652 case TYPE_STUB:
653 selector = 1; /* Code selector */
654 break;
656 case TYPE_BYTE:
657 case TYPE_WORD:
658 case TYPE_LONG:
659 selector = 2; /* Data selector */
660 break;
662 case TYPE_ABS:
663 selector = 0xfe; /* Constant selector */
664 break;
667 /* create a new bundle if necessary */
668 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
670 bundle = pstr;
671 bundle[0] = 0;
672 bundle[1] = selector;
673 pstr += 2;
676 (*bundle)++;
677 if (selector != 0)
679 *pstr++ = 1;
680 *(WORD *)pstr = odp->offset;
681 pstr += sizeof(WORD);
684 *pstr++ = 0;
686 /* Dump the module content */
688 printf( "\t.data\n" );
689 printf( "\t.globl " PREFIX "%s_Module_Start\n", UpperDLLName );
690 printf( PREFIX "%s_Module_Start:\n", UpperDLLName );
691 size = (int)pstr - (int)pModule;
692 for (i = 0, pstr = buffer; i < size; i++, pstr++)
694 if (!(i & 7)) printf( "\t.byte " );
695 printf( "%d%c", *pstr, ((i & 7) != 7) ? ',' : '\n' );
697 if (i & 7) printf( "0\n" );
698 printf( "\t.globl " PREFIX "%s_Module_End\n", UpperDLLName );
699 printf( PREFIX "%s_Module_End:\n", UpperDLLName );
703 static void BuildSpec32Files( char *specname )
705 ORDDEF *odp;
706 ORDFUNCDEF *fdp;
707 ORDRETDEF *rdp;
708 int i;
710 SpecFp = fopen( specname, "r");
711 if (SpecFp == NULL)
713 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
714 exit(1);
717 ParseTopLevel();
719 printf( "/* File generated automatically, do not edit! */\n" );
720 printf( "#include <sys/types.h>\n");
721 printf( "#include \"windows.h\"\n");
722 printf( "#include \"dlls.h\"\n");
723 printf( "#include \"pe_image.h\"\n");
724 printf( "#include \"winerror.h\"\n");
725 printf( "#include \"relay32.h\"\n");
726 printf( "#include \"stddebug.h\"\n");
727 printf( "#include \"debug.h\"\n");
728 printf( "\nextern int RELAY32_Unimplemented();\n\n" );
730 odp = OrdinalDefinitions;
731 for (i = 0; i <= Limit; i++, odp++)
733 int argno,argc;
734 fdp = odp->additional_data;
735 rdp = odp->additional_data;
737 switch (odp->type)
739 case TYPE_INVALID:
740 case TYPE_STUB:
741 printf( "int %s_%d()\n{\n\t", UpperDLLName, i);
742 printf( "RELAY32_Unimplemented(\"%s\",%d);\n", UpperDLLName, i);
743 printf( "\t/*NOTREACHED*/\n\treturn 0;\n}\n\n");
744 break;
745 case TYPE_STDCALL:
746 argc=strlen(fdp->arg_types);
747 printf( "void %s_%d(", UpperDLLName, i);
748 for(argno=0;argno<argc;argno++)
750 switch(fdp->arg_types[argno])
752 case 'p': printf( "void *");break;
753 case 'l': printf( "int ");break;
754 default:
755 fprintf(stderr, "Not supported argument type %c\n",
756 fdp->arg_types[argno]);
757 exit(1);
759 putchar( 'a'+argno );
760 if (argno!=argc-1) putchar( ',' );
762 printf( ")\n{\n" );
763 printf( "\tdprintf_relay(stddeb,\"Entering %%s.%%s(");
764 for (argno=0;argno<argc;argno++) printf( "%%x ");
765 printf( ")\\n\", \"%s\", \"%s\"", UpperDLLName, odp->export_name);
766 for(argno=0;argno<argc;argno++) printf( ",%c", 'a'+argno);
767 printf( ");\n\t%s(", fdp->internal_name );
768 for(argno=0;argno<argc;argno++)
770 putchar('a'+argno);
771 if (argno!=argc-1) putchar(',');
773 printf( ");\n\t__asm__ __volatile__(\"movl %%ebp,%%esp;"
774 "popl %%ebp;ret $%d\");\n}\n\n",
775 4*argc);
776 break;
777 case TYPE_RETURN:
778 printf( "void %s_%d()\n{\n\t", UpperDLLName, i);
779 printf( "RELAY32_DebugEnter(\"%s\",\"%s\");\n\t",
780 UpperDLLName, odp->export_name);
781 printf( "WIN32_LastError=ERROR_CALL_NOT_IMPLEMENTED;\n");
782 printf( "\t__asm__ __volatile__ (\"movl %d,%%eax\");\n",
783 rdp->ret_value);
784 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp;popl %%ebp;"
785 "ret $%d\");\n}\n\n", rdp->arg_size);
786 break;
787 default:
788 fprintf(stderr,"build: function type %d not available for Win32\n",
789 odp->type);
790 break;
794 printf( "static WIN32_function functions[%d+1]={\n", Limit);
796 odp = OrdinalDefinitions;
797 for (i = 0; i <= Limit; i++, odp++)
799 fdp = odp->additional_data;
800 rdp = odp->additional_data;
802 switch (odp->type)
804 case TYPE_INVALID:
805 printf( "{0,%s_%d},\n",UpperDLLName, i);
806 break;
807 case TYPE_RETURN:
808 case TYPE_STDCALL:
809 case TYPE_STUB:
810 printf( "{\"%s\",%s_%d},\n", odp->export_name, UpperDLLName, i);
811 break;
812 default:
813 fprintf(stderr, "build: implementation error: missing %d\n",
814 odp->type);
815 exit(1);
818 printf("};\n\n");
820 printf( "static WIN32_builtin dll={\"%s\",functions,%d,0};\n",
821 UpperDLLName, Limit+1);
823 printf( "void %s_Init(void)\n{\n",UpperDLLName);
824 printf( "\tdll.next=WIN32_builtin_list;\n");
825 printf( "\tWIN32_builtin_list=&dll;\n}");
829 static void BuildSpec16Files( char *specname )
831 ORDDEF *odp;
832 ORDFUNCDEF *fdp;
833 ORDRETDEF *rdp;
834 int i;
835 int code_offset, data_offset;
837 SpecFp = fopen( specname, "r");
838 if (SpecFp == NULL)
840 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
841 exit(1);
844 ParseTopLevel();
846 printf( "/* File generated automatically; do not edit! */\n" );
847 printf( "\t.data\n" );
848 printf( "\t.globl " PREFIX "%s_Data_Start\n", UpperDLLName );
849 printf( PREFIX "%s_Data_Start:\n", UpperDLLName );
850 printf( "\t.word 0,0,0,0,0,0,0,0\n" );
851 data_offset = 16;
852 printf( "\t.text\n" );
853 printf( "\t.globl " PREFIX "%s_Code_Start\n", UpperDLLName );
854 printf( PREFIX "%s_Code_Start:\n", UpperDLLName );
855 code_offset = 0;
857 odp = OrdinalDefinitions;
858 for (i = 0; i <= Limit; i++, odp++)
860 fdp = odp->additional_data;
861 rdp = odp->additional_data;
863 switch (odp->type)
865 case TYPE_INVALID:
866 odp->offset = 0xffff;
867 break;
869 case TYPE_ABS:
870 odp->offset = (int)odp->additional_data & 0xffff;
871 break;
873 case TYPE_BYTE:
874 printf( "/* %s.%d */\n", UpperDLLName, i);
875 odp->offset = data_offset;
876 data_offset += OutputVariableCode( ".byte", odp);
877 break;
879 case TYPE_WORD:
880 printf( "/* %s.%d */\n", UpperDLLName, i);
881 odp->offset = data_offset;
882 data_offset += 2 * OutputVariableCode( ".word", odp);
883 break;
885 case TYPE_LONG:
886 printf( "/* %s.%d */\n", UpperDLLName, i);
887 odp->offset = data_offset;
888 data_offset += 4 * OutputVariableCode( ".long", odp);
889 break;
891 case TYPE_RETURN:
892 printf( "/* %s.%d */\n", UpperDLLName, i);
893 printf( "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
894 printf( "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
895 printf( "\t.byte 0x66\n");
896 if (rdp->arg_size != 0)
897 printf( "\tlret $%d\n", rdp->arg_size);
898 else
899 printf( "\tlret\n");
900 odp->offset = code_offset;
901 code_offset += 10; /* Assembly code is 10 bytes long */
902 if (rdp->arg_size != 0) code_offset += 2;
903 break;
905 case TYPE_REGISTER:
906 case TYPE_PASCAL:
907 case TYPE_PASCAL_16:
908 case TYPE_STUB:
909 printf( "/* %s.%d */\n", UpperDLLName, i);
910 printf( "\tpushw %%bp\n" );
911 printf( "\tpushl $0x%08x\n", (DLLId << 16) | i);
912 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
913 printf( "\tljmp $0x%04x, $" PREFIX "CallTo32_%s_%s\n\n",
914 WINE_CODE_SELECTOR,
915 (odp->type == TYPE_REGISTER) ? "regs" :
916 (odp->type == TYPE_PASCAL) ? "long" : "word",
917 fdp->arg_types );
918 printf( "\tnop\n" );
919 printf( "\tnop\n" );
920 printf( "\tnop\n" );
921 printf( "\tnop\n" );
922 printf( "\tnop\n" );
923 odp->offset = code_offset;
924 code_offset += 24; /* Assembly code is 24 bytes long */
925 break;
927 default:
928 fprintf( stderr, "build: Unknown function type; please report.\n");
929 break;
933 if (!code_offset) /* Make sure the code segment is not empty */
935 printf( "\t.byte 0\n" );
936 code_offset++;
939 BuildModule( code_offset, data_offset );
943 /*******************************************************************
944 * BuildCall32LargeStack
946 * Build the function used to switch to the original 32-bit stack
947 * before calling a 32-bit function from 32-bit code. This is used for
948 * functions that need a large stack, like X bitmaps functions.
950 * The generated function has the following prototype:
951 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
953 * Stack layout:
954 * ... ...
955 * (ebp+20) arg2
956 * (ebp+16) arg1
957 * (ebp+12) nbargs
958 * (ebp+8) func
959 * (ebp+4) ret addr
960 * (ebp) ebp
962 static void BuildCall32LargeStack(void)
964 /* Function header */
966 printf( "/**********\n" );
967 printf( " * " PREFIX "CallTo32_LargeStack\n" );
968 printf( " **********/\n" );
969 printf( "\t.align 4\n" );
970 printf( "\t.globl " PREFIX "CallTo32_LargeStack\n\n" );
971 printf( PREFIX "CallTo32_LargeStack:\n" );
973 /* Entry code */
975 printf( "\tpushl %%ebp\n" );
976 printf( "\tmovl %%esp,%%ebp\n" );
978 /* Save registers */
980 printf( "\tpushl %%ecx\n" );
981 printf( "\tpushl %%esi\n" );
982 printf( "\tpushl %%edi\n" );
984 /* Retrieve the original 32-bit stack pointer and switch to it if any */
986 printf( "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
987 printf( "\torl %%eax,%%eax\n" );
988 printf( "\tje 0f\n" );
989 printf( "\tmovl %%eax,%%esp\n" );
990 printf( "0:\n" );
992 /* Transfer the arguments */
994 printf( "\tmovl 12(%%ebp),%%ecx\n" );
995 printf( "\torl %%ecx,%%ecx\n" );
996 printf( "\tje 1f\n" );
997 printf( "\tleal 16(%%ebp),%%esi\n" );
998 printf( "\tshll $2,%%ecx\n" );
999 printf( "\tsubl %%ecx,%%esp\n" );
1000 printf( "\tmovl %%esp,%%edi\n" );
1001 printf( "\tshrl $2,%%ecx\n" );
1002 printf( "\tcld\n" );
1003 printf( "\trep; movsl\n" );
1004 printf( "1:\n" );
1006 /* Call the function */
1008 printf( "\tcall 8(%%ebp)\n" );
1010 /* Switch back to the normal stack */
1012 printf( "\tleal -12(%%ebp),%%esp\n" );
1014 /* Restore registers and return */
1016 printf( "\tpopl %%edi\n" );
1017 printf( "\tpopl %%esi\n" );
1018 printf( "\tpopl %%ecx\n" );
1019 printf( "\tpopl %%ebp\n" );
1020 printf( "\tret\n" );
1024 /*******************************************************************
1025 * TransferArgs16To32
1027 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1028 * The 16-bit stack layout is:
1029 * ... ...
1030 * (bp+8) arg2
1031 * (bp+6) arg1
1032 * (bp+4) cs
1033 * (bp+2) ip
1034 * (bp) bp
1036 static int TransferArgs16To32( char *args )
1038 int i, pos16, pos32;
1040 /* Save ebx first */
1042 printf( "\tpushl %%ebx\n" );
1044 /* Get the 32-bit stack pointer */
1046 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1048 /* Copy the arguments */
1050 pos16 = 6; /* skip bp and return address */
1051 pos32 = 0;
1053 for (i = strlen(args); i > 0; i--)
1055 pos32 -= 4;
1056 switch(args[i-1])
1058 case 'w': /* word */
1059 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1060 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1061 pos16 += 2;
1062 break;
1064 case 's': /* s_word */
1065 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1066 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1067 pos16 += 2;
1068 break;
1070 case 'l': /* long */
1071 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
1072 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1073 pos16 += 4;
1074 break;
1076 case 'p': /* ptr */
1077 /* Get the selector */
1078 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1079 /* Get the selector base */
1080 printf( "\tandl $0xfff8,%%eax\n" );
1081 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1082 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1083 /* Add the offset */
1084 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1085 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1086 pos16 += 4;
1087 break;
1089 default:
1090 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1094 /* Restore ebx */
1096 printf( "\tpopl %%ebx\n" );
1098 return pos16 - 6; /* Return the size of the 16-bit args */
1102 /*******************************************************************
1103 * BuildContext
1105 * Build the context structure on the 32-bit stack.
1106 * The only valid registers in the context structure are:
1107 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1109 static void BuildContext(void)
1111 /* Save ebx first */
1113 printf( "\tpushl %%ebx\n" );
1115 /* Get the 32-bit stack pointer */
1117 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1119 /* Store the registers */
1121 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx) ); /* Get ebx from stack */
1122 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax) );
1123 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx) );
1124 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx) );
1125 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi) );
1126 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi) );
1127 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1128 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds) );
1129 printf( "\tmovw -12(%%ebp),%%ax\n" ); /* Get saved es from stack */
1130 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1131 printf( "\tpushfl\n" );
1132 #ifndef __FreeBSD__
1133 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1134 #else
1135 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1136 #endif
1140 /*******************************************************************
1141 * RestoreContext
1143 * Restore the registers from the context structure
1145 static void RestoreContext(void)
1147 /* Get the 32-bit stack pointer */
1149 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1151 /* Restore the registers */
1153 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx) );
1154 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx) );
1155 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi) );
1156 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi) );
1157 printf( "\tpopl %%eax\n" ); /* Remove old ds and es from stack */
1158 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds) ); /* Push new ds */
1159 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) ); /* Push new es */
1160 #ifndef __FreeBSD__
1161 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1162 #else
1163 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1164 #endif
1165 printf( "\tpopfl\n" );
1166 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax) );
1167 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx) );
1171 /*******************************************************************
1172 * BuildCall32Func
1174 * Build a 32-bit callback function. The syntax of the function
1175 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1176 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1177 * 'l'=long, 'p'=pointer).
1178 * For register functions, the arguments are ignored, but they are still
1179 * removed from the stack upon return.
1181 * Stack layout upon entry to the callback function:
1182 * ... ...
1183 * (sp+14) first 16-bit arg
1184 * (sp+12) cs (word)
1185 * (sp+10) ip (word)
1186 * (sp+8) bp (word)
1187 * (sp+4) dll_id+ordinal (long)
1188 * (sp) entrypoint (long)
1191 static void BuildCall32Func( char *profile )
1193 int argsize = 0;
1194 int short_ret = 0;
1195 int reg_func = 0;
1196 char *args = profile + 5;
1198 /* Parse function type */
1200 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1201 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1202 else if (strncmp( "long_", profile, 5 ))
1204 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1205 return;
1208 /* Function header */
1210 printf( "/**********\n" );
1211 printf( " * " PREFIX "CallTo32_%s\n", profile );
1212 printf( " **********/\n" );
1213 printf( "\t.align 4\n" );
1214 printf( "\t.globl " PREFIX "CallTo32_%s\n\n", profile );
1215 printf( PREFIX "CallTo32_%s:\n", profile );
1217 /* Setup bp to point to its copy on the stack */
1219 printf( "\tmovzwl %%sp,%%ebp\n" );
1220 printf( "\taddw $8,%%bp\n" );
1222 /* Save 16-bit ds and es */
1224 printf( "\tpushw %%ds\n" );
1225 printf( "\tpushw %%es\n" );
1227 /* Restore 32-bit ds and es */
1229 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1230 printf( "\tpopw %%ds\n" );
1231 printf( "\tpopw %%es\n" );
1234 /* Save the 16-bit stack */
1236 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1237 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1238 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1239 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1241 /* Transfer the arguments */
1243 if (reg_func) BuildContext();
1244 else if (*args) argsize = TransferArgs16To32( args );
1246 /* Get the address of the API function */
1248 printf( "\tmovl -8(%%ebp),%%eax\n" );
1250 /* If necessary, save %edx over the API function address */
1252 if (!reg_func && short_ret)
1253 printf( "\tmovl %%edx,-8(%%ebp)\n" );
1255 /* Switch to the 32-bit stack */
1257 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1258 printf( "\tpushw %%ds\n" );
1259 printf( "\tpopw %%ss\n" );
1260 printf( "\tleal -%d(%%ebp),%%esp\n",
1261 reg_func ? sizeof(struct sigcontext_struct) : 4 * strlen(args) );
1263 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1265 printf( "\taddl $24,%%ebp\n" );
1267 /* Print the debug information before the call */
1269 if (debugging)
1271 printf( "\tpushl %%eax\n" );
1272 printf( "\tpushl $CALL32_Str_%s\n", profile );
1273 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1274 printf( "\tcall " PREFIX "RELAY_DebugCall32\n" );
1275 printf( "\tpopl %%eax\n" );
1276 printf( "\tpopl %%eax\n" );
1277 printf( "\tpopl %%eax\n" );
1280 /* Call the entry point */
1282 printf( "\tcall %%eax\n" );
1284 /* Print the debug information after the call */
1286 if (debugging)
1288 printf( "\tpushl %%eax\n" );
1289 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1290 printf( "\tcall " PREFIX "RELAY_DebugReturn\n" );
1291 printf( "\tpopl %%eax\n" );
1292 printf( "\tpopl %%eax\n" );
1295 /* Restore the 16-bit stack */
1297 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1298 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1299 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1300 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1302 if (reg_func)
1304 /* Restore registers from the context structure */
1305 RestoreContext();
1307 /* Calc the arguments size */
1308 while (*args)
1310 switch(*args)
1312 case 'w':
1313 case 's':
1314 argsize += 2;
1315 break;
1316 case 'p':
1317 case 'l':
1318 argsize += 4;
1319 break;
1320 default:
1321 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1323 args++;
1327 /* Restore ds and es */
1329 printf( "\tpopw %%es\n" );
1330 printf( "\tpopw %%ds\n" );
1332 /* Get the return value into dx:ax and clean up the stack */
1334 if (!reg_func)
1336 if (short_ret)
1338 printf( "\tpopl %%edx\n" ); /* Restore %edx */
1339 printf( "\taddl $4,%%esp\n" ); /* Remove DLL id and ordinal */
1341 else
1343 printf( "\tpushl %%eax\n" );
1344 printf( "\tpopw %%ax\n" );
1345 printf( "\tpopw %%dx\n" );
1346 /* Remove API entry point, DLL id and ordinal from the stack */
1347 printf( "\taddl $8,%%esp\n" );
1350 else
1352 /* Remove API entry point, DLL id and ordinal from the stack, */
1353 /* but take care not to change the value of the carry flag. */
1355 printf( "\tpopl %%ebp\n" );
1356 printf( "\tpopl %%ebp\n" );
1359 /* Restore bp */
1361 printf( "\tpopw %%bp\n" );
1363 /* Remove the arguments and return */
1365 if (argsize)
1367 printf( "\t.byte 0x66\n" );
1368 printf( "\tlret $%d\n", argsize );
1370 else
1372 printf( "\t.byte 0x66\n" );
1373 printf( "\tlret\n" );
1378 /*******************************************************************
1379 * BuildCall16Func
1381 * Build a 16-bit callback function.
1383 * Stack frame of the callback function:
1384 * ... ...
1385 * (ebp+24) arg2
1386 * (ebp+20) arg1
1387 * (ebp+16) 16-bit ds
1388 * (ebp+12) func to call
1389 * (ebp+8) code selector
1390 * (ebp+4) return address
1391 * (ebp) previous ebp
1393 * Prototypes for the CallTo16 functions:
1394 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1395 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1396 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1397 * WORD ax, WORD bx, WORD cx, WORD dx,
1398 * WORD si, WORD di );
1400 static void BuildCall16Func( char *profile )
1402 int short_ret = 0;
1403 int reg_func = 0;
1404 char *args = profile + 5;
1406 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1407 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1408 else if (strncmp( "long_", profile, 5 ))
1410 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1411 return;
1414 /* Function header */
1416 printf( "/**********\n" );
1417 printf( " * " PREFIX "CallTo16_%s\n", profile );
1418 printf( " **********/\n" );
1419 printf( "\t.align 4\n" );
1420 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1421 printf( PREFIX "CallTo16_%s:\n", profile );
1423 /* Push code selector before return address to simulate a lcall */
1425 printf( "\tpopl %%eax\n" );
1426 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1427 printf( "\tpushl %%eax\n" );
1429 /* Entry code */
1431 printf( "\tpushl %%ebp\n" );
1432 printf( "\tmovl %%esp,%%ebp\n" );
1434 /* Save the 32-bit registers */
1436 printf( "\tpushl %%ebx\n" );
1437 printf( "\tpushl %%ecx\n" );
1438 printf( "\tpushl %%edx\n" );
1439 printf( "\tpushl %%esi\n" );
1440 printf( "\tpushl %%edi\n" );
1442 /* Save the 32-bit stack */
1444 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1445 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1446 printf( "\tmovl %%ebp,%%ebx\n" );
1448 /* Print debugging info */
1450 if (debugging)
1452 /* Push the address of the first argument */
1453 printf( "\tmovl %%ebx,%%eax\n" );
1454 printf( "\taddl $12,%%eax\n" );
1455 printf( "\tpushl $%d\n", reg_func ? 8 : strlen(args) );
1456 printf( "\tpushl %%eax\n" );
1457 printf( "\tcall " PREFIX "RELAY_DebugCall16\n" );
1458 printf( "\tpopl %%eax\n" );
1459 printf( "\tpopl %%eax\n" );
1462 /* Switch to the 16-bit stack */
1464 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1465 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1467 /* Transfer the arguments */
1469 if (reg_func)
1471 /* Get the registers. ebx is handled later on. */
1472 printf( "\tpushw 20(%%ebx)\n" );
1473 printf( "\tpopw %%es\n" );
1474 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1475 printf( "\tmovl 28(%%ebx),%%eax\n" );
1476 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1477 printf( "\tmovl 40(%%ebx),%%edx\n" );
1478 printf( "\tmovl 44(%%ebx),%%esi\n" );
1479 printf( "\tmovl 48(%%ebx),%%edi\n" );
1481 else /* not a register function */
1483 int pos = 20; /* first argument position */
1485 /* Make %bp point to the previous stackframe (built by CallTo32) */
1486 printf( "\tmovw %%sp,%%bp\n" );
1487 printf( "\taddw $16,%%bp\n" );
1489 while (*args)
1491 switch(*args++)
1493 case 'w': /* word */
1494 printf( "\tpushw %d(%%ebx)\n", pos );
1495 break;
1496 case 'l': /* long */
1497 printf( "\tpushl %d(%%ebx)\n", pos );
1498 break;
1500 pos += 4;
1504 /* Push the return address */
1506 printf( "\tpushl " PREFIX "CALL16_RetAddr_%s\n",
1507 short_ret ? "word" : "long" );
1509 /* Push the called routine address */
1511 printf( "\tpushl 12(%%ebx)\n" );
1513 /* Get the 16-bit ds */
1515 if (reg_func)
1517 printf( "\tpushw 16(%%ebx)\n" );
1518 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1519 printf( "\tpopw %%ds\n" );
1521 else
1523 /* Set ax equal to ds for window procedures */
1524 printf( "\tmovw 16(%%ebx),%%ax\n" );
1525 printf( "\tmovw %%ax,%%ds\n" );
1528 /* Jump to the called routine */
1530 printf( "\t.byte 0x66\n" );
1531 printf( "\tlret\n" );
1535 /*******************************************************************
1536 * BuildRet16Func
1538 * Build the return code for 16-bit callbacks
1540 static void BuildRet16Func()
1542 printf( "\t.globl " PREFIX "CALL16_Ret_word\n" );
1543 printf( "\t.globl " PREFIX "CALL16_Ret_long\n" );
1545 /* Put return value into eax */
1547 printf( PREFIX "CALL16_Ret_long:\n" );
1548 printf( "\tpushw %%dx\n" );
1549 printf( "\tpushw %%ax\n" );
1550 printf( "\tpopl %%eax\n" );
1551 printf( PREFIX "CALL16_Ret_word:\n" );
1553 /* Restore 32-bit segment registers */
1555 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1556 printf( "\tmovw %%bx,%%ds\n" );
1557 printf( "\tmovw %%bx,%%es\n" );
1558 printf( "\tmovw %%bx,%%ss\n" );
1560 /* Restore the 32-bit stack */
1562 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1563 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1565 /* Restore the 32-bit registers */
1567 printf( "\tpopl %%edi\n" );
1568 printf( "\tpopl %%esi\n" );
1569 printf( "\tpopl %%edx\n" );
1570 printf( "\tpopl %%ecx\n" );
1571 printf( "\tpopl %%ebx\n" );
1573 /* Return to caller */
1575 printf( "\tpopl %%ebp\n" );
1576 printf( "\tlret\n" );
1578 /* Declare the return address variables */
1580 printf( "\t.data\n" );
1581 printf( "\t.globl " PREFIX "CALL16_RetAddr_word\n" );
1582 printf( "\t.globl " PREFIX "CALL16_RetAddr_long\n" );
1583 printf( PREFIX "CALL16_RetAddr_word:\t.long 0\n" );
1584 printf( PREFIX "CALL16_RetAddr_long:\t.long 0\n" );
1585 printf( "\t.text\n" );
1589 static void usage(void)
1591 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1592 " build -call32 FUNCTION_PROFILES\n"
1593 " build -call16 FUNCTION_PROFILES\n" );
1594 exit(1);
1598 int main(int argc, char **argv)
1600 int i;
1602 if (argc <= 2) usage();
1604 if (!strcmp( argv[1], "-spec16" ))
1606 for (i = 2; i < argc; i++) BuildSpec16Files( argv[i] );
1608 else if (!strcmp( argv[1], "-spec32" ))
1610 for (i = 2; i < argc; i++) BuildSpec32Files( argv[i] );
1612 else if (!strcmp( argv[1], "-call32" )) /* 32-bit callbacks */
1614 /* File header */
1616 printf( "/* File generated automatically. Do no edit! */\n\n" );
1617 printf( "\t.text\n" );
1619 /* Build the 32-bit large stack callback */
1621 BuildCall32LargeStack();
1623 /* Build the callback functions */
1625 for (i = 2; i < argc; i++) BuildCall32Func( argv[i] );
1627 /* Output the argument debugging strings */
1629 if (debugging)
1631 printf( "/* Argument strings */\n" );
1632 for (i = 2; i < argc; i++)
1634 printf( "CALL32_Str_%s:\n", argv[i] );
1635 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1639 else if (!strcmp( argv[1], "-call16" )) /* 16-bit callbacks */
1641 /* File header */
1643 printf( "/* File generated automatically. Do no edit! */\n\n" );
1644 printf( "\t.text\n" );
1645 printf( "\t.globl " PREFIX "CALL16_Start\n" );
1646 printf( PREFIX "CALL16_Start:\n" );
1648 /* Build the callback functions */
1650 for (i = 2; i < argc; i++) BuildCall16Func( argv[i] );
1652 /* Output the 16-bit return code */
1654 BuildRet16Func();
1656 printf( "\t.globl " PREFIX "CALL16_End\n" );
1657 printf( PREFIX "CALL16_End:\n" );
1659 else usage();
1661 return 0;