Release 951212
[wine/multimedia.git] / tools / build.c
blob53a0150754d5636d9a899b89c10df32345e4eac0
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 #if defined (__ELF__) || defined (__svr4__)
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
33 #define TYPE_CDECL 11
35 #define MAX_ORDINALS 1299
37 /* Callback function used for stub functions */
38 #define STUB_CALLBACK "RELAY_Unimplemented"
40 typedef struct ordinal_definition_s
42 int type;
43 int offset;
44 char export_name[80];
45 void *additional_data;
46 } ORDDEF;
48 typedef struct ordinal_variable_definition_s
50 int n_values;
51 int *values;
52 } ORDVARDEF;
54 typedef struct ordinal_function_definition_s
56 int n_args;
57 char arg_types[32];
58 char internal_name[80];
59 } ORDFUNCDEF;
61 typedef struct ordinal_return_definition_s
63 int arg_size;
64 int ret_value;
65 } ORDRETDEF;
67 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
69 char LowerDLLName[80];
70 char UpperDLLName[80];
71 int Limit = 0;
72 int DLLId;
73 int Base = 0;
74 FILE *SpecFp;
76 char *ParseBuffer = NULL;
77 char *ParseNext;
78 char ParseSaveChar;
79 int Line;
81 static int debugging = 1;
83 /* Offset of register relative to the end of the context struct */
84 #define CONTEXTOFFSET(reg) \
85 ((int)&(((struct sigcontext_struct *)1)->reg) - 1 \
86 - sizeof(struct sigcontext_struct))
87 #ifdef __svr4__
88 #define sc_eax uc_mcontext.gregs[EAX]
89 #define sc_ebx uc_mcontext.gregs[EBX]
90 #define sc_ecx uc_mcontext.gregs[ECX]
91 #define sc_edx uc_mcontext.gregs[EDX]
92 #define sc_esi uc_mcontext.gregs[ESI]
93 #define sc_edi uc_mcontext.gregs[EDI]
94 #define sc_ds uc_mcontext.gregs[DS]
95 #define sc_es uc_mcontext.gregs[ES]
96 #define sc_eflags uc_mcontext.gregs[EFL]
97 #endif
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 *strlower(char *s)
136 char *p;
138 for(p = s; *p != '\0'; p++)
139 *p = tolower(*p);
141 return s;
144 static char *strupper(char *s)
146 char *p;
148 for(p = s; *p != '\0'; p++)
149 *p = toupper(*p);
151 return s;
154 static char * GetTokenInLine(void)
156 char *p;
157 char *token;
159 if (ParseNext != ParseBuffer)
161 if (ParseSaveChar == '\0')
162 return NULL;
163 *ParseNext = ParseSaveChar;
167 * Remove initial white space.
169 for (p = ParseNext; isspace(*p); p++)
172 if ((*p == '\0') || (*p == '#'))
173 return NULL;
176 * Find end of token.
178 token = p++;
179 if (*token != '(' && *token != ')')
180 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
181 p++;
183 ParseSaveChar = *p;
184 ParseNext = p;
185 *p = '\0';
187 return token;
190 static char * GetToken(void)
192 char *token;
194 if (ParseBuffer == NULL)
196 ParseBuffer = xmalloc(512);
197 ParseNext = ParseBuffer;
198 Line++;
199 while (1)
201 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
202 return NULL;
203 if (ParseBuffer[0] != '#')
204 break;
208 while ((token = GetTokenInLine()) == NULL)
210 ParseNext = ParseBuffer;
211 Line++;
212 while (1)
214 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
215 return NULL;
216 if (ParseBuffer[0] != '#')
217 break;
221 return token;
224 static int ParseVariable(int ordinal, int type)
226 ORDDEF *odp;
227 ORDVARDEF *vdp;
228 char export_name[80];
229 char *token;
230 char *endptr;
231 int *value_array;
232 int n_values;
233 int value_array_size;
235 strcpy(export_name, GetToken());
237 token = GetToken();
238 if (*token != '(')
240 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
241 exit(1);
244 n_values = 0;
245 value_array_size = 25;
246 value_array = xmalloc(sizeof(*value_array) * value_array_size);
248 while ((token = GetToken()) != NULL)
250 if (*token == ')')
251 break;
253 value_array[n_values++] = strtol(token, &endptr, 0);
254 if (n_values == value_array_size)
256 value_array_size += 25;
257 value_array = xrealloc(value_array,
258 sizeof(*value_array) * value_array_size);
261 if (endptr == NULL || *endptr != '\0')
263 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
264 token);
265 exit(1);
269 if (token == NULL)
271 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
272 exit(1);
275 if (ordinal >= MAX_ORDINALS)
277 fprintf(stderr, "%d: Ordinal number too large\n", Line);
278 exit(1);
281 odp = &OrdinalDefinitions[ordinal];
282 odp->type = type;
283 strcpy(odp->export_name, export_name);
285 vdp = xmalloc(sizeof(*vdp));
286 odp->additional_data = vdp;
288 vdp->n_values = n_values;
289 vdp->values = xrealloc(value_array, sizeof(*value_array) * n_values);
291 return 0;
294 static int ParseExportFunction(int ordinal, int type)
296 char *token;
297 ORDDEF *odp;
298 ORDFUNCDEF *fdp;
299 int i;
301 odp = &OrdinalDefinitions[ordinal];
302 strcpy(odp->export_name, GetToken());
303 odp->type = type;
304 fdp = xmalloc(sizeof(*fdp));
305 odp->additional_data = fdp;
307 token = GetToken();
308 if (*token != '(')
310 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
311 exit(1);
314 for (i = 0; i < 16; i++)
316 token = GetToken();
317 if (*token == ')')
318 break;
320 if (!strcmp(token, "byte") || !strcmp(token, "word"))
321 fdp->arg_types[i] = 'w';
322 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
323 fdp->arg_types[i] = 's';
324 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
325 fdp->arg_types[i] = 'l';
326 else if (!strcmp(token, "ptr"))
327 fdp->arg_types[i] = 'p';
328 else if (!strcmp(token, "..."))
329 fdp->arg_types[i] = '.';
330 else
332 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
333 exit(1);
336 fdp->arg_types[i] = '\0';
338 strcpy(fdp->internal_name, GetToken());
339 return 0;
342 static int ParseEquate(int ordinal)
344 ORDDEF *odp;
345 char *token;
346 char *endptr;
347 int value;
349 odp = &OrdinalDefinitions[ordinal];
350 strcpy(odp->export_name, GetToken());
352 token = GetToken();
353 value = strtol(token, &endptr, 0);
354 if (endptr == NULL || *endptr != '\0')
356 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
357 token);
358 exit(1);
361 odp->type = TYPE_ABS;
362 odp->additional_data = (void *) value;
364 return 0;
367 static int ParseReturn(int ordinal)
369 ORDDEF *odp;
370 ORDRETDEF *rdp;
371 char *token;
372 char *endptr;
374 rdp = xmalloc(sizeof(*rdp));
376 odp = &OrdinalDefinitions[ordinal];
377 strcpy(odp->export_name, GetToken());
378 odp->type = TYPE_RETURN;
379 odp->additional_data = rdp;
381 token = GetToken();
382 rdp->arg_size = strtol(token, &endptr, 0);
383 if (endptr == NULL || *endptr != '\0')
385 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
386 token);
387 exit(1);
390 token = GetToken();
391 rdp->ret_value = strtol(token, &endptr, 0);
392 if (endptr == NULL || *endptr != '\0')
394 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
395 token);
396 exit(1);
399 return 0;
403 static int ParseStub( int ordinal )
405 ORDDEF *odp;
406 ORDFUNCDEF *fdp;
408 odp = &OrdinalDefinitions[ordinal];
409 strcpy( odp->export_name, GetToken() );
410 odp->type = TYPE_STUB;
411 fdp = xmalloc(sizeof(*fdp));
412 odp->additional_data = fdp;
413 fdp->arg_types[0] = '\0';
414 strcpy( fdp->internal_name, STUB_CALLBACK );
415 return 0;
419 static int ParseOrdinal(int ordinal)
421 char *token;
423 if (ordinal >= MAX_ORDINALS)
425 fprintf(stderr, "%d: Ordinal number too large\n", Line);
426 exit(1);
428 if (ordinal > Limit) Limit = ordinal;
430 token = GetToken();
431 if (token == NULL)
433 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
434 exit(1);
437 if (strcmp(token, "byte") == 0)
438 return ParseVariable(ordinal, TYPE_BYTE);
439 else if (strcmp(token, "word") == 0)
440 return ParseVariable(ordinal, TYPE_WORD);
441 else if (strcmp(token, "long") == 0)
442 return ParseVariable(ordinal, TYPE_LONG);
443 else if (strcmp(token, "p") == 0)
444 return ParseExportFunction(ordinal, TYPE_PASCAL);
445 else if (strcmp(token, "pascal") == 0)
446 return ParseExportFunction(ordinal, TYPE_PASCAL);
447 else if (strcmp(token, "pascal16") == 0)
448 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
449 else if (strcmp(token, "register") == 0)
450 return ParseExportFunction(ordinal, TYPE_REGISTER);
451 else if (strcmp(token, "stdcall") == 0)
452 return ParseExportFunction(ordinal, TYPE_STDCALL);
453 else if (strcmp(token, "cdecl") == 0)
454 return ParseExportFunction(ordinal, TYPE_CDECL);
455 else if (strcmp(token, "equate") == 0)
456 return ParseEquate(ordinal);
457 else if (strcmp(token, "return") == 0)
458 return ParseReturn(ordinal);
459 else if (strcmp(token, "stub") == 0)
460 return ParseStub(ordinal);
461 else
463 fprintf(stderr,
464 "%d: Expected type after ordinal, found '%s' instead\n",
465 Line, token);
466 exit(1);
470 static int ParseTopLevel(void)
472 char *token;
474 while ((token = GetToken()) != NULL)
476 if (strcmp(token, "name") == 0)
478 strcpy(LowerDLLName, GetToken());
479 strlower(LowerDLLName);
481 strcpy(UpperDLLName, LowerDLLName);
482 strupper(UpperDLLName);
484 else if (strcmp(token, "id") == 0)
486 token = GetToken();
487 if (!IsNumberString(token))
489 fprintf(stderr, "%d: Expected number after id\n", Line);
490 exit(1);
493 DLLId = atoi(token);
495 else if (strcmp(token, "base") == 0)
497 token = GetToken();
498 if (!IsNumberString(token))
500 fprintf(stderr, "%d: Expected number after base\n", Line);
501 exit(1);
504 Base = atoi(token);
506 else if (IsNumberString(token))
508 int ordinal;
509 int rv;
511 ordinal = atoi(token);
512 if ((rv = ParseOrdinal(ordinal)) < 0)
513 return rv;
515 else
517 fprintf(stderr,
518 "%d: Expected name, id, length or ordinal\n", Line);
519 exit(1);
523 return 0;
527 static int OutputVariableCode( char *storage, ORDDEF *odp )
529 ORDVARDEF *vdp;
530 int i;
532 vdp = odp->additional_data;
533 printf( "\t.data\n" );
534 for (i = 0; i < vdp->n_values; i++)
536 if ((i & 7) == 0)
537 printf( "\t%s\t", storage);
539 printf( "%d", vdp->values[i]);
541 if ((i & 7) == 7 || i == vdp->n_values - 1) printf( "\n");
542 else printf( ", ");
544 printf( "\n");
545 printf( "\t.text\n" );
546 return vdp->n_values;
550 /*******************************************************************
551 * BuildModule
553 * Build the in-memory representation of the module, and dump it
554 * as a byte stream into the assembly code.
556 static void BuildModule( int max_code_offset, int max_data_offset )
558 ORDDEF *odp;
559 int i, size;
560 char *buffer;
561 NE_MODULE *pModule;
562 SEGTABLEENTRY *pSegment;
563 LOADEDFILEINFO *pFileInfo;
564 BYTE *pstr, *bundle;
565 WORD *pword;
567 /* Module layout:
568 * NE_MODULE Module
569 * LOADEDFILEINFO File information
570 * SEGTABLEENTRY Segment 1 (code)
571 * SEGTABLEENTRY Segment 2 (data)
572 * WORD[2] Resource table (empty)
573 * BYTE[2] Imported names (empty)
574 * BYTE[n] Resident names table
575 * BYTE[n] Entry table
578 buffer = xmalloc( 0x10000 );
580 pModule = (NE_MODULE *)buffer;
581 pModule->magic = NE_SIGNATURE;
582 pModule->count = 1;
583 pModule->next = 0;
584 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
585 pModule->dgroup = 2;
586 pModule->heap_size = 0xffff;
587 pModule->stack_size = 0;
588 pModule->ip = 0;
589 pModule->cs = 0;
590 pModule->sp = 0;
591 pModule->ss = 0;
592 pModule->seg_count = 2;
593 pModule->modref_count = 0;
594 pModule->nrname_size = 0;
595 pModule->modref_table = 0;
596 pModule->nrname_fpos = 0;
597 pModule->moveable_entries = 0;
598 pModule->alignment = 0;
599 pModule->truetype = 0;
600 pModule->os_flags = NE_OSFLAGS_WINDOWS;
601 pModule->misc_flags = 0;
602 pModule->dlls_to_init = 0;
603 pModule->nrname_handle = 0;
604 pModule->min_swap_area = 0;
605 pModule->expected_version = 0x030a;
607 /* File information */
609 pFileInfo = (LOADEDFILEINFO *)(pModule + 1);
610 pModule->fileinfo = (int)pFileInfo - (int)pModule;
611 pFileInfo->length = sizeof(LOADEDFILEINFO) + strlen(UpperDLLName) + 3;
612 pFileInfo->fixed_media = 0;
613 pFileInfo->error = 0;
614 pFileInfo->date = 0;
615 pFileInfo->time = 0;
616 sprintf( pFileInfo->filename, "%s.DLL", UpperDLLName );
617 pstr = (char *)pFileInfo + pFileInfo->length + 1;
619 /* Segment table */
621 pSegment = (SEGTABLEENTRY *)pstr;
622 pModule->seg_table = (int)pSegment - (int)pModule;
623 pSegment->filepos = 0;
624 pSegment->size = max_code_offset;
625 pSegment->flags = 0;
626 pSegment->minsize = max_code_offset;
627 pSegment->selector = 0;
628 pSegment++;
630 pModule->dgroup_entry = (int)pSegment - (int)pModule;
631 pSegment->filepos = 0;
632 pSegment->size = max_data_offset;
633 pSegment->flags = NE_SEGFLAGS_DATA;
634 pSegment->minsize = max_data_offset;
635 pSegment->selector = 0;
636 pSegment++;
638 /* Resource table */
640 pword = (WORD *)pSegment;
641 pModule->res_table = (int)pword - (int)pModule;
642 *pword++ = 0;
643 *pword++ = 0;
645 /* Imported names table */
647 pstr = (char *)pword;
648 pModule->import_table = (int)pstr - (int)pModule;
649 *pstr++ = 0;
650 *pstr++ = 0;
652 /* Resident names table */
654 pModule->name_table = (int)pstr - (int)pModule;
655 /* First entry is module name */
656 *pstr = strlen(UpperDLLName );
657 strcpy( pstr + 1, UpperDLLName );
658 pstr += *pstr + 1;
659 *(WORD *)pstr = 0;
660 pstr += sizeof(WORD);
661 /* Store all ordinals */
662 odp = OrdinalDefinitions + 1;
663 for (i = 1; i <= Limit; i++, odp++)
665 if (!odp->export_name[0]) continue;
666 *pstr = strlen( odp->export_name );
667 strcpy( pstr + 1, odp->export_name );
668 strupper( pstr + 1 );
669 pstr += *pstr + 1;
670 *(WORD *)pstr = i;
671 pstr += sizeof(WORD);
673 *pstr++ = 0;
675 /* Entry table */
677 pModule->entry_table = (int)pstr - (int)pModule;
678 bundle = NULL;
679 odp = OrdinalDefinitions + 1;
680 for (i = 1; i <= Limit; i++, odp++)
682 int selector = 0;
684 switch (odp->type)
686 case TYPE_INVALID:
687 selector = 0; /* Invalid selector */
688 break;
690 case TYPE_PASCAL:
691 case TYPE_PASCAL_16:
692 case TYPE_REGISTER:
693 case TYPE_RETURN:
694 case TYPE_STUB:
695 selector = 1; /* Code selector */
696 break;
698 case TYPE_BYTE:
699 case TYPE_WORD:
700 case TYPE_LONG:
701 selector = 2; /* Data selector */
702 break;
704 case TYPE_ABS:
705 selector = 0xfe; /* Constant selector */
706 break;
709 /* create a new bundle if necessary */
710 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
712 bundle = pstr;
713 bundle[0] = 0;
714 bundle[1] = selector;
715 pstr += 2;
718 (*bundle)++;
719 if (selector != 0)
721 *pstr++ = 1;
722 *(WORD *)pstr = odp->offset;
723 pstr += sizeof(WORD);
726 *pstr++ = 0;
728 /* Dump the module content */
730 printf( "\t.data\n" );
731 printf( "\t.globl " PREFIX "%s_Module_Start\n", UpperDLLName );
732 printf( PREFIX "%s_Module_Start:\n", UpperDLLName );
733 size = (int)pstr - (int)pModule;
734 for (i = 0, pstr = buffer; i < size; i++, pstr++)
736 if (!(i & 7)) printf( "\t.byte " );
737 printf( "%d%c", *pstr, ((i & 7) != 7) ? ',' : '\n' );
739 if (i & 7) printf( "0\n" );
740 printf( "\t.globl " PREFIX "%s_Module_End\n", UpperDLLName );
741 printf( PREFIX "%s_Module_End:\n", UpperDLLName );
745 static void BuildSpec32Files( char *specname )
747 ORDDEF *odp;
748 ORDFUNCDEF *fdp;
749 ORDRETDEF *rdp;
750 int i;
751 int varargs;
753 SpecFp = fopen( specname, "r");
754 if (SpecFp == NULL)
756 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
757 exit(1);
760 ParseTopLevel();
762 printf( "/* File generated automatically, do not edit! */\n" );
763 printf( "#include <sys/types.h>\n");
764 printf( "#include <stdarg.h>\n");
765 printf( "#include \"windows.h\"\n");
766 printf( "#include \"dlls.h\"\n");
767 printf( "#include \"pe_image.h\"\n");
768 printf( "#include \"winerror.h\"\n");
769 printf( "#include \"relay32.h\"\n");
770 printf( "#include \"stddebug.h\"\n");
771 printf( "#include \"debug.h\"\n");
773 odp = OrdinalDefinitions;
774 for (i = 0; i <= Limit; i++, odp++)
776 int argno,argc;
777 fdp = odp->additional_data;
778 rdp = odp->additional_data;
780 switch (odp->type)
782 case TYPE_INVALID:
783 case TYPE_STUB:
784 printf( "int %s_%d()\n{\n\t", UpperDLLName, i);
785 printf( "RELAY32_Unimplemented(\"%s\",%d);\n", UpperDLLName, i);
786 printf( "\t/*NOTREACHED*/\n\treturn 0;\n}\n\n");
787 break;
788 case TYPE_STDCALL:
789 case TYPE_CDECL:
790 varargs=0;
791 argc=strlen(fdp->arg_types);
792 if(odp->type == TYPE_STDCALL)
794 /* Output a function prototype with stdcall attribute */
795 printf( "void %s_%d(", UpperDLLName, i);
796 for(argno=0;argno<argc;argno++)
798 switch(fdp->arg_types[argno])
800 case 'p': printf( "void *");break;
801 case 'l': printf( "int ");break;
802 case '.': printf( "... ");varargs=argno;break;
803 default:
804 fprintf(stderr, "Not supported argument type %c\n",
805 fdp->arg_types[argno]);
806 exit(1);
808 if(fdp->arg_types[argno]!='.') putchar( 'a'+argno );
809 if (argno!=argc-1) putchar( ',' );
811 printf( ") __attribute((stdcall));" );
814 printf( "void %s_%d(", UpperDLLName, i);
815 for(argno=0;argno<argc;argno++)
817 switch(fdp->arg_types[argno])
819 case 'p': printf( "void *");break;
820 case 'l': printf( "int ");break;
821 case '.': printf( "... ");varargs=argno;break;
822 default:
823 fprintf(stderr, "Not supported argument type %c\n",
824 fdp->arg_types[argno]);
825 exit(1);
827 if(fdp->arg_types[argno]!='.') putchar( 'a'+argno );
828 if (argno!=argc-1) putchar( ',' );
830 printf( ")" );
831 printf( "\n{\n" );
832 if (varargs) printf( "\tva_list valist;\n\n\tva_start(valist, %c);",
833 'a'+varargs-1 );
834 printf( "\tdprintf_relay(stddeb,\"Call %%s.%%s(");
835 for (argno=0;argno<argc;argno++)
836 if(fdp->arg_types[argno]!='.')
838 putchar( '%' );
839 putchar( (fdp->arg_types[argno] == 'p') ? 'p' : 'x' );
840 if (argno < argc-1) putchar( ',' );
842 printf( ")\\n\", \"%s\", \"%s\"", UpperDLLName, odp->export_name);
843 for(argno=0;argno<argc;argno++)
844 if(fdp->arg_types[argno]!='.') printf( ",%c", 'a'+argno);
845 printf( ");\n\t%s(", fdp->internal_name );
846 for(argno=0;argno<argc;argno++)
848 if (fdp->arg_types[argno]=='.') printf("valist");
849 else putchar('a'+argno);
850 if (argno!=argc-1) putchar(',');
852 printf( ");\n}\n\n");
853 break;
854 case TYPE_RETURN:
855 printf( "void %s_%d()\n{\n\t", UpperDLLName, i);
856 printf( "RELAY32_DebugEnter(\"%s\",\"%s\");\n\t",
857 UpperDLLName, odp->export_name);
858 printf( "WIN32_LastError=ERROR_CALL_NOT_IMPLEMENTED;\n");
859 printf( "\t__asm__ __volatile__ (\"movl $%d,%%eax\");\n",
860 rdp->ret_value);
861 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp;popl %%ebp;"
862 "ret $%d\");\n}\n\n", rdp->arg_size);
863 break;
864 default:
865 fprintf(stderr,"build: function type %d not available for Win32\n",
866 odp->type);
867 break;
871 printf( "static WIN32_function functions[%d+1]={\n", Limit);
873 odp = OrdinalDefinitions;
874 for (i = 0; i <= Limit; i++, odp++)
876 fdp = odp->additional_data;
877 rdp = odp->additional_data;
879 switch (odp->type)
881 case TYPE_INVALID:
882 printf( "{0,%s_%d},\n",UpperDLLName, i);
883 break;
884 case TYPE_RETURN:
885 case TYPE_STDCALL:
886 case TYPE_CDECL:
887 case TYPE_STUB:
888 printf( "{\"%s\",%s_%d},\n", odp->export_name, UpperDLLName, i);
889 break;
890 default:
891 fprintf(stderr, "build: implementation error: missing %d\n",
892 odp->type);
893 exit(1);
896 printf("};\n\n");
898 printf( "static WIN32_builtin dll={\"%s\",functions,%d,0};\n",
899 UpperDLLName, Limit+1);
901 printf( "void %s_Init(void)\n{\n",UpperDLLName);
902 printf( "\tdll.next=WIN32_builtin_list;\n");
903 printf( "\tWIN32_builtin_list=&dll;\n}");
907 static void BuildSpec16Files( char *specname )
909 ORDDEF *odp;
910 ORDFUNCDEF *fdp;
911 ORDRETDEF *rdp;
912 int i;
913 int code_offset, data_offset;
915 SpecFp = fopen( specname, "r");
916 if (SpecFp == NULL)
918 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
919 exit(1);
922 ParseTopLevel();
924 printf( "/* File generated automatically; do not edit! */\n" );
925 printf( "\t.data\n" );
926 printf( "\t.globl " PREFIX "%s_Data_Start\n", UpperDLLName );
927 printf( PREFIX "%s_Data_Start:\n", UpperDLLName );
928 #ifdef __svr4__
929 printf( "\t.4byte 0,0,0,0,0,0,0,0\n" );
930 #else
931 printf( "\t.word 0,0,0,0,0,0,0,0\n" );
932 #endif
933 data_offset = 16;
934 printf( "\t.text\n" );
935 printf( "\t.globl " PREFIX "%s_Code_Start\n", UpperDLLName );
936 printf( PREFIX "%s_Code_Start:\n", UpperDLLName );
937 code_offset = 0;
939 odp = OrdinalDefinitions;
940 for (i = 0; i <= Limit; i++, odp++)
942 fdp = odp->additional_data;
943 rdp = odp->additional_data;
945 switch (odp->type)
947 case TYPE_INVALID:
948 odp->offset = 0xffff;
949 break;
951 case TYPE_ABS:
952 odp->offset = (int)odp->additional_data & 0xffff;
953 break;
955 case TYPE_BYTE:
956 printf( "/* %s.%d */\n", UpperDLLName, i);
957 odp->offset = data_offset;
958 data_offset += OutputVariableCode( ".byte", odp);
959 break;
961 case TYPE_WORD:
962 printf( "/* %s.%d */\n", UpperDLLName, i);
963 odp->offset = data_offset;
964 #ifdef __svr4__
965 data_offset += 2 * OutputVariableCode( ".4byte", odp);
966 #else
967 data_offset += 2 * OutputVariableCode( ".word", odp);
968 #endif
969 break;
971 case TYPE_LONG:
972 printf( "/* %s.%d */\n", UpperDLLName, i);
973 odp->offset = data_offset;
974 data_offset += 4 * OutputVariableCode( ".long", odp);
975 break;
977 case TYPE_RETURN:
978 printf( "/* %s.%d */\n", UpperDLLName, i);
979 printf( "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
980 printf( "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
981 printf( "\t.byte 0x66\n");
982 if (rdp->arg_size != 0)
983 printf( "\tlret $%d\n", rdp->arg_size);
984 else
985 printf( "\tlret\n");
986 odp->offset = code_offset;
987 code_offset += 10; /* Assembly code is 10 bytes long */
988 if (rdp->arg_size != 0) code_offset += 2;
989 break;
991 case TYPE_REGISTER:
992 case TYPE_PASCAL:
993 case TYPE_PASCAL_16:
994 case TYPE_STUB:
995 printf( "/* %s.%d */\n", UpperDLLName, i);
996 printf( "\tpushw %%bp\n" );
997 printf( "\tpushl $0x%08x\n", (DLLId << 16) | i);
998 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
999 printf( "\tljmp $0x%04x, $" PREFIX "CallTo32_%s_%s\n\n",
1000 WINE_CODE_SELECTOR,
1001 (odp->type == TYPE_REGISTER) ? "regs" :
1002 (odp->type == TYPE_PASCAL) ? "long" : "word",
1003 fdp->arg_types );
1004 printf( "\tnop\n" );
1005 printf( "\tnop\n" );
1006 printf( "\tnop\n" );
1007 printf( "\tnop\n" );
1008 printf( "\tnop\n" );
1009 odp->offset = code_offset;
1010 code_offset += 24; /* Assembly code is 24 bytes long */
1011 break;
1013 default:
1014 fprintf( stderr, "build: Unknown function type; please report.\n");
1015 break;
1019 if (!code_offset) /* Make sure the code segment is not empty */
1021 printf( "\t.byte 0\n" );
1022 code_offset++;
1025 BuildModule( code_offset, data_offset );
1029 /*******************************************************************
1030 * BuildCall32LargeStack
1032 * Build the function used to switch to the original 32-bit stack
1033 * before calling a 32-bit function from 32-bit code. This is used for
1034 * functions that need a large stack, like X bitmaps functions.
1036 * The generated function has the following prototype:
1037 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1039 * Stack layout:
1040 * ... ...
1041 * (ebp+20) arg2
1042 * (ebp+16) arg1
1043 * (ebp+12) nbargs
1044 * (ebp+8) func
1045 * (ebp+4) ret addr
1046 * (ebp) ebp
1048 static void BuildCall32LargeStack(void)
1050 /* Function header */
1052 printf( "/**********\n" );
1053 printf( " * " PREFIX "CallTo32_LargeStack\n" );
1054 printf( " **********/\n" );
1055 printf( "\t.align 4\n" );
1056 printf( "\t.globl " PREFIX "CallTo32_LargeStack\n\n" );
1057 printf( PREFIX "CallTo32_LargeStack:\n" );
1059 /* Entry code */
1061 printf( "\tpushl %%ebp\n" );
1062 printf( "\tmovl %%esp,%%ebp\n" );
1064 /* Save registers */
1066 printf( "\tpushl %%ecx\n" );
1067 printf( "\tpushl %%esi\n" );
1068 printf( "\tpushl %%edi\n" );
1070 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1072 printf( "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1073 printf( "\torl %%eax,%%eax\n" );
1074 printf( "\tje 0f\n" );
1075 printf( "\tmovl %%eax,%%esp\n" );
1076 printf( "0:\n" );
1078 /* Transfer the arguments */
1080 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1081 printf( "\torl %%ecx,%%ecx\n" );
1082 printf( "\tje 1f\n" );
1083 printf( "\tleal 16(%%ebp),%%esi\n" );
1084 printf( "\tshll $2,%%ecx\n" );
1085 printf( "\tsubl %%ecx,%%esp\n" );
1086 printf( "\tmovl %%esp,%%edi\n" );
1087 printf( "\tshrl $2,%%ecx\n" );
1088 printf( "\tcld\n" );
1089 printf( "\trep; movsl\n" );
1090 printf( "1:\n" );
1092 /* Call the function */
1094 printf( "\tcall 8(%%ebp)\n" );
1096 /* Switch back to the normal stack */
1098 printf( "\tleal -12(%%ebp),%%esp\n" );
1100 /* Restore registers and return */
1102 printf( "\tpopl %%edi\n" );
1103 printf( "\tpopl %%esi\n" );
1104 printf( "\tpopl %%ecx\n" );
1105 printf( "\tpopl %%ebp\n" );
1106 printf( "\tret\n" );
1110 /*******************************************************************
1111 * TransferArgs16To32
1113 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1114 * The 16-bit stack layout is:
1115 * ... ...
1116 * (bp+8) arg2
1117 * (bp+6) arg1
1118 * (bp+4) cs
1119 * (bp+2) ip
1120 * (bp) bp
1122 static int TransferArgs16To32( char *args )
1124 int i, pos16, pos32;
1126 /* Save ebx first */
1128 printf( "\tpushl %%ebx\n" );
1130 /* Get the 32-bit stack pointer */
1132 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1134 /* Copy the arguments */
1136 pos16 = 6; /* skip bp and return address */
1137 pos32 = 0;
1139 for (i = strlen(args); i > 0; i--)
1141 pos32 -= 4;
1142 switch(args[i-1])
1144 case 'w': /* word */
1145 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1146 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1147 pos16 += 2;
1148 break;
1150 case 's': /* s_word */
1151 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1152 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1153 pos16 += 2;
1154 break;
1156 case 'l': /* long */
1157 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
1158 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1159 pos16 += 4;
1160 break;
1162 case 'p': /* ptr */
1163 /* Get the selector */
1164 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1165 /* Get the selector base */
1166 printf( "\tandl $0xfff8,%%eax\n" );
1167 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1168 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1169 /* Add the offset */
1170 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1171 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1172 pos16 += 4;
1173 break;
1175 default:
1176 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1180 /* Restore ebx */
1182 printf( "\tpopl %%ebx\n" );
1184 return pos16 - 6; /* Return the size of the 16-bit args */
1188 /*******************************************************************
1189 * BuildContext
1191 * Build the context structure on the 32-bit stack.
1192 * The only valid registers in the context structure are:
1193 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1195 static void BuildContext(void)
1197 /* Save ebx first */
1199 printf( "\tpushl %%ebx\n" );
1201 /* Get the 32-bit stack pointer */
1203 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1205 /* Store the registers */
1207 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx) ); /* Get ebx from stack */
1208 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax) );
1209 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx) );
1210 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx) );
1211 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi) );
1212 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi) );
1213 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1214 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds) );
1215 printf( "\tmovw -12(%%ebp),%%ax\n" ); /* Get saved es from stack */
1216 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1217 printf( "\tpushfl\n" );
1218 #ifndef __FreeBSD__
1219 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1220 #else
1221 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1222 #endif
1226 /*******************************************************************
1227 * RestoreContext
1229 * Restore the registers from the context structure
1231 static void RestoreContext(void)
1233 /* Get the 32-bit stack pointer */
1235 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1237 /* Restore the registers */
1239 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx) );
1240 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx) );
1241 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi) );
1242 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi) );
1243 printf( "\tpopl %%eax\n" ); /* Remove old ds and es from stack */
1244 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds) ); /* Push new ds */
1245 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) ); /* Push new es */
1246 #ifndef __FreeBSD__
1247 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1248 #else
1249 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1250 #endif
1251 printf( "\tpopfl\n" );
1252 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax) );
1253 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx) );
1257 /*******************************************************************
1258 * BuildCall32Func
1260 * Build a 32-bit callback function. The syntax of the function
1261 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1262 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1263 * 'l'=long, 'p'=pointer).
1264 * For register functions, the arguments are ignored, but they are still
1265 * removed from the stack upon return.
1267 * Stack layout upon entry to the callback function:
1268 * ... ...
1269 * (sp+14) first 16-bit arg
1270 * (sp+12) cs (word)
1271 * (sp+10) ip (word)
1272 * (sp+8) bp (word)
1273 * (sp+4) dll_id+ordinal (long)
1274 * (sp) entrypoint (long)
1277 static void BuildCall32Func( char *profile )
1279 int argsize = 0;
1280 int short_ret = 0;
1281 int reg_func = 0;
1282 char *args = profile + 5;
1284 /* Parse function type */
1286 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1287 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1288 else if (strncmp( "long_", profile, 5 ))
1290 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1291 return;
1294 /* Function header */
1296 printf( "/**********\n" );
1297 printf( " * " PREFIX "CallTo32_%s\n", profile );
1298 printf( " **********/\n" );
1299 printf( "\t.align 4\n" );
1300 printf( "\t.globl " PREFIX "CallTo32_%s\n\n", profile );
1301 printf( PREFIX "CallTo32_%s:\n", profile );
1303 /* Setup bp to point to its copy on the stack */
1305 printf( "\tmovzwl %%sp,%%ebp\n" );
1306 printf( "\taddw $8,%%bp\n" );
1308 /* Save 16-bit ds and es */
1310 printf( "\tpushw %%ds\n" );
1311 printf( "\tpushw %%es\n" );
1313 /* Restore 32-bit ds and es */
1315 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1316 printf( "\tpopw %%ds\n" );
1317 printf( "\tpopw %%es\n" );
1320 /* Save the 16-bit stack */
1322 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1323 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1324 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1325 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1327 /* Transfer the arguments */
1329 if (reg_func) BuildContext();
1330 else if (*args) argsize = TransferArgs16To32( args );
1332 /* Get the address of the API function */
1334 printf( "\tmovl -8(%%ebp),%%eax\n" );
1336 /* If necessary, save %edx over the API function address */
1338 if (!reg_func && short_ret)
1339 printf( "\tmovl %%edx,-8(%%ebp)\n" );
1341 /* Switch to the 32-bit stack */
1343 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1344 printf( "\tpushw %%ds\n" );
1345 printf( "\tpopw %%ss\n" );
1346 printf( "\tleal -%d(%%ebp),%%esp\n",
1347 reg_func ? sizeof(struct sigcontext_struct) : 4 * strlen(args) );
1349 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1351 printf( "\taddl $24,%%ebp\n" );
1353 /* Print the debug information before the call */
1355 if (debugging)
1357 printf( "\tpushl %%eax\n" );
1358 printf( "\tpushl $CALL32_Str_%s\n", profile );
1359 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1360 printf( "\tcall " PREFIX "RELAY_DebugCall32\n" );
1361 printf( "\tpopl %%eax\n" );
1362 printf( "\tpopl %%eax\n" );
1363 printf( "\tpopl %%eax\n" );
1366 /* Call the entry point */
1368 printf( "\tcall %%eax\n" );
1370 /* Print the debug information after the call */
1372 if (debugging)
1374 printf( "\tpushl %%eax\n" );
1375 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1376 printf( "\tcall " PREFIX "RELAY_DebugReturn\n" );
1377 printf( "\tpopl %%eax\n" );
1378 printf( "\tpopl %%eax\n" );
1381 /* Restore the 16-bit stack */
1383 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1384 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1385 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1386 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1388 if (reg_func)
1390 /* Restore registers from the context structure */
1391 RestoreContext();
1393 /* Calc the arguments size */
1394 while (*args)
1396 switch(*args)
1398 case 'w':
1399 case 's':
1400 argsize += 2;
1401 break;
1402 case 'p':
1403 case 'l':
1404 argsize += 4;
1405 break;
1406 default:
1407 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1409 args++;
1413 /* Restore ds and es */
1415 printf( "\tpopw %%es\n" );
1416 printf( "\tpopw %%ds\n" );
1418 /* Get the return value into dx:ax and clean up the stack */
1420 if (!reg_func)
1422 if (short_ret)
1424 printf( "\tpopl %%edx\n" ); /* Restore %edx */
1425 printf( "\taddl $4,%%esp\n" ); /* Remove DLL id and ordinal */
1427 else
1429 printf( "\tpushl %%eax\n" );
1430 printf( "\tpopw %%ax\n" );
1431 printf( "\tpopw %%dx\n" );
1432 /* Remove API entry point, DLL id and ordinal from the stack */
1433 printf( "\taddl $8,%%esp\n" );
1436 else
1438 /* Remove API entry point, DLL id and ordinal from the stack, */
1439 /* but take care not to change the value of the carry flag. */
1441 printf( "\tpopl %%ebp\n" );
1442 printf( "\tpopl %%ebp\n" );
1445 /* Restore bp */
1447 printf( "\tpopw %%bp\n" );
1449 /* Remove the arguments and return */
1451 if (argsize)
1453 printf( "\t.byte 0x66\n" );
1454 printf( "\tlret $%d\n", argsize );
1456 else
1458 printf( "\t.byte 0x66\n" );
1459 printf( "\tlret\n" );
1464 /*******************************************************************
1465 * BuildCall16Func
1467 * Build a 16-bit callback function.
1469 * Stack frame of the callback function:
1470 * ... ...
1471 * (ebp+24) arg2
1472 * (ebp+20) arg1
1473 * (ebp+16) 16-bit ds
1474 * (ebp+12) func to call
1475 * (ebp+8) code selector
1476 * (ebp+4) return address
1477 * (ebp) previous ebp
1479 * Prototypes for the CallTo16 functions:
1480 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1481 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1482 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1483 * WORD ax, WORD bx, WORD cx, WORD dx,
1484 * WORD si, WORD di );
1486 static void BuildCall16Func( char *profile )
1488 int short_ret = 0;
1489 int reg_func = 0;
1490 char *args = profile + 5;
1492 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1493 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1494 else if (strncmp( "long_", profile, 5 ))
1496 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1497 return;
1500 /* Function header */
1502 printf( "/**********\n" );
1503 printf( " * " PREFIX "CallTo16_%s\n", profile );
1504 printf( " **********/\n" );
1505 printf( "\t.align 4\n" );
1506 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1507 printf( PREFIX "CallTo16_%s:\n", profile );
1509 /* Push code selector before return address to simulate a lcall */
1511 printf( "\tpopl %%eax\n" );
1512 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1513 printf( "\tpushl %%eax\n" );
1515 /* Entry code */
1517 printf( "\tpushl %%ebp\n" );
1518 printf( "\tmovl %%esp,%%ebp\n" );
1520 /* Save the 32-bit registers */
1522 printf( "\tpushl %%ebx\n" );
1523 printf( "\tpushl %%ecx\n" );
1524 printf( "\tpushl %%edx\n" );
1525 printf( "\tpushl %%esi\n" );
1526 printf( "\tpushl %%edi\n" );
1528 /* Save the 32-bit stack */
1530 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1531 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1532 printf( "\tmovl %%ebp,%%ebx\n" );
1534 /* Print debugging info */
1536 if (debugging)
1538 /* Push the address of the first argument */
1539 printf( "\tmovl %%ebx,%%eax\n" );
1540 printf( "\taddl $12,%%eax\n" );
1541 printf( "\tpushl $%d\n", reg_func ? 8 : strlen(args) );
1542 printf( "\tpushl %%eax\n" );
1543 printf( "\tcall " PREFIX "RELAY_DebugCall16\n" );
1544 printf( "\tpopl %%eax\n" );
1545 printf( "\tpopl %%eax\n" );
1548 /* Switch to the 16-bit stack */
1550 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1551 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1553 /* Transfer the arguments */
1555 if (reg_func)
1557 /* Get the registers. ebx is handled later on. */
1558 printf( "\tpushw 20(%%ebx)\n" );
1559 printf( "\tpopw %%es\n" );
1560 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1561 printf( "\tmovl 28(%%ebx),%%eax\n" );
1562 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1563 printf( "\tmovl 40(%%ebx),%%edx\n" );
1564 printf( "\tmovl 44(%%ebx),%%esi\n" );
1565 printf( "\tmovl 48(%%ebx),%%edi\n" );
1567 else /* not a register function */
1569 int pos = 20; /* first argument position */
1571 /* Make %bp point to the previous stackframe (built by CallTo32) */
1572 printf( "\tmovw %%sp,%%bp\n" );
1573 printf( "\taddw $16,%%bp\n" );
1575 while (*args)
1577 switch(*args++)
1579 case 'w': /* word */
1580 printf( "\tpushw %d(%%ebx)\n", pos );
1581 break;
1582 case 'l': /* long */
1583 printf( "\tpushl %d(%%ebx)\n", pos );
1584 break;
1586 pos += 4;
1590 /* Push the return address */
1592 printf( "\tpushl " PREFIX "CALL16_RetAddr_%s\n",
1593 short_ret ? "word" : "long" );
1595 /* Push the called routine address */
1597 printf( "\tpushl 12(%%ebx)\n" );
1599 /* Get the 16-bit ds */
1601 if (reg_func)
1603 printf( "\tpushw 16(%%ebx)\n" );
1604 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1605 printf( "\tpopw %%ds\n" );
1607 else
1609 /* Set ax equal to ds for window procedures */
1610 printf( "\tmovw 16(%%ebx),%%ax\n" );
1611 printf( "\tmovw %%ax,%%ds\n" );
1614 /* Jump to the called routine */
1616 printf( "\t.byte 0x66\n" );
1617 printf( "\tlret\n" );
1621 /*******************************************************************
1622 * BuildRet16Func
1624 * Build the return code for 16-bit callbacks
1626 static void BuildRet16Func()
1628 printf( "\t.globl " PREFIX "CALL16_Ret_word\n" );
1629 printf( "\t.globl " PREFIX "CALL16_Ret_long\n" );
1631 /* Put return value into eax */
1633 printf( PREFIX "CALL16_Ret_long:\n" );
1634 printf( "\tpushw %%dx\n" );
1635 printf( "\tpushw %%ax\n" );
1636 printf( "\tpopl %%eax\n" );
1637 printf( PREFIX "CALL16_Ret_word:\n" );
1639 /* Restore 32-bit segment registers */
1641 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1642 printf( "\tmovw %%bx,%%ds\n" );
1643 printf( "\tmovw %%bx,%%es\n" );
1644 printf( "\tmovw %%bx,%%ss\n" );
1646 /* Restore the 32-bit stack */
1648 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1649 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1651 /* Restore the 32-bit registers */
1653 printf( "\tpopl %%edi\n" );
1654 printf( "\tpopl %%esi\n" );
1655 printf( "\tpopl %%edx\n" );
1656 printf( "\tpopl %%ecx\n" );
1657 printf( "\tpopl %%ebx\n" );
1659 /* Return to caller */
1661 printf( "\tpopl %%ebp\n" );
1662 printf( "\tlret\n" );
1664 /* Declare the return address variables */
1666 printf( "\t.data\n" );
1667 printf( "\t.globl " PREFIX "CALL16_RetAddr_word\n" );
1668 printf( "\t.globl " PREFIX "CALL16_RetAddr_long\n" );
1669 printf( PREFIX "CALL16_RetAddr_word:\t.long 0\n" );
1670 printf( PREFIX "CALL16_RetAddr_long:\t.long 0\n" );
1671 printf( "\t.text\n" );
1675 static void usage(void)
1677 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1678 " build -call32 FUNCTION_PROFILES\n"
1679 " build -call16 FUNCTION_PROFILES\n" );
1680 exit(1);
1684 int main(int argc, char **argv)
1686 int i;
1688 if (argc <= 2) usage();
1690 if (!strcmp( argv[1], "-spec16" ))
1692 for (i = 2; i < argc; i++) BuildSpec16Files( argv[i] );
1694 else if (!strcmp( argv[1], "-spec32" ))
1696 for (i = 2; i < argc; i++) BuildSpec32Files( argv[i] );
1698 else if (!strcmp( argv[1], "-call32" )) /* 32-bit callbacks */
1700 /* File header */
1702 printf( "/* File generated automatically. Do not edit! */\n\n" );
1703 printf( "\t.text\n" );
1705 /* Build the 32-bit large stack callback */
1707 BuildCall32LargeStack();
1709 /* Build the callback functions */
1711 for (i = 2; i < argc; i++) BuildCall32Func( argv[i] );
1713 /* Output the argument debugging strings */
1715 if (debugging)
1717 printf( "/* Argument strings */\n" );
1718 for (i = 2; i < argc; i++)
1720 printf( "CALL32_Str_%s:\n", argv[i] );
1721 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1725 else if (!strcmp( argv[1], "-call16" )) /* 16-bit callbacks */
1727 /* File header */
1729 printf( "/* File generated automatically. Do not edit! */\n\n" );
1730 printf( "\t.text\n" );
1731 printf( "\t.globl " PREFIX "CALL16_Start\n" );
1732 printf( PREFIX "CALL16_Start:\n" );
1734 /* Build the callback functions */
1736 for (i = 2; i < argc; i++) BuildCall16Func( argv[i] );
1738 /* Output the 16-bit return code */
1740 BuildRet16Func();
1742 printf( "\t.globl " PREFIX "CALL16_End\n" );
1743 printf( PREFIX "CALL16_End:\n" );
1745 else usage();
1747 return 0;