Release 960314
[wine.git] / tools / build.c
blob938f99f8f475ae3f528dfdb43a77d12226b4d3bf
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"
14 #include "windows.h"
16 /* ELF symbols do not have an underscore in front */
17 #if defined (__ELF__) || defined (__svr4__)
18 #define PREFIX
19 #else
20 #define PREFIX "_"
21 #endif
23 #define TYPE_INVALID 0
24 #define TYPE_BYTE 1
25 #define TYPE_WORD 2
26 #define TYPE_LONG 3
27 #define TYPE_PASCAL_16 4
28 #define TYPE_PASCAL 5
29 #define TYPE_REGISTER 6
30 #define TYPE_ABS 7
31 #define TYPE_RETURN 8
32 #define TYPE_STUB 9
33 #define TYPE_STDCALL 10
34 #define TYPE_CDECL 11
36 #define MAX_ORDINALS 1299
38 /* Callback function used for stub functions */
39 #define STUB_CALLBACK "RELAY_Unimplemented"
41 typedef struct ordinal_definition_s
43 int type;
44 int offset;
45 char export_name[80];
46 void *additional_data;
47 } ORDDEF;
49 typedef struct ordinal_variable_definition_s
51 int n_values;
52 int *values;
53 } ORDVARDEF;
55 typedef struct ordinal_function_definition_s
57 int n_args;
58 char arg_types[32];
59 char internal_name[80];
60 } ORDFUNCDEF;
62 typedef struct ordinal_return_definition_s
64 int arg_size;
65 int ret_value;
66 } ORDRETDEF;
68 static ORDDEF OrdinalDefinitions[MAX_ORDINALS];
70 char LowerDLLName[80];
71 char UpperDLLName[80];
72 int Limit = 0;
73 int DLLId;
74 int Base = 0;
75 FILE *SpecFp;
77 char *ParseBuffer = NULL;
78 char *ParseNext;
79 char ParseSaveChar;
80 int Line;
82 static int debugging = 1;
84 /* Offset of register relative to the end of the context struct */
85 #define CONTEXTOFFSET(reg) \
86 ((int)&(((struct sigcontext_struct *)1)->reg) - 1 \
87 - sizeof(struct sigcontext_struct))
88 #ifdef __svr4__
89 #define sc_eax uc_mcontext.gregs[EAX]
90 #define sc_ebx uc_mcontext.gregs[EBX]
91 #define sc_ecx uc_mcontext.gregs[ECX]
92 #define sc_edx uc_mcontext.gregs[EDX]
93 #define sc_esi uc_mcontext.gregs[ESI]
94 #define sc_edi uc_mcontext.gregs[EDI]
95 #define sc_ds uc_mcontext.gregs[DS]
96 #define sc_es uc_mcontext.gregs[ES]
97 #define sc_eflags uc_mcontext.gregs[EFL]
98 #endif
100 static void *xmalloc (size_t size)
102 void *res;
104 res = malloc (size ? size : 1);
105 if (res == NULL)
107 fprintf (stderr, "Virtual memory exhausted.\n");
108 exit (1);
110 return res;
114 static void *xrealloc (void *ptr, size_t size)
116 void *res = realloc (ptr, size);
117 if (res == NULL)
119 fprintf (stderr, "Virtual memory exhausted.\n");
120 exit (1);
122 return res;
126 static int IsNumberString(char *s)
128 while (*s != '\0')
129 if (!isdigit(*s++))
130 return 0;
132 return 1;
135 static char *strlower(char *s)
137 char *p;
139 for(p = s; *p != '\0'; p++)
140 *p = tolower(*p);
142 return s;
145 static char *strupper(char *s)
147 char *p;
149 for(p = s; *p != '\0'; p++)
150 *p = toupper(*p);
152 return s;
155 static char * GetTokenInLine(void)
157 char *p;
158 char *token;
160 if (ParseNext != ParseBuffer)
162 if (ParseSaveChar == '\0')
163 return NULL;
164 *ParseNext = ParseSaveChar;
168 * Remove initial white space.
170 for (p = ParseNext; isspace(*p); p++)
173 if ((*p == '\0') || (*p == '#'))
174 return NULL;
177 * Find end of token.
179 token = p++;
180 if (*token != '(' && *token != ')')
181 while (*p != '\0' && *p != '(' && *p != ')' && !isspace(*p))
182 p++;
184 ParseSaveChar = *p;
185 ParseNext = p;
186 *p = '\0';
188 return token;
191 static char * GetToken(void)
193 char *token;
195 if (ParseBuffer == NULL)
197 ParseBuffer = xmalloc(512);
198 ParseNext = ParseBuffer;
199 Line++;
200 while (1)
202 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
203 return NULL;
204 if (ParseBuffer[0] != '#')
205 break;
209 while ((token = GetTokenInLine()) == NULL)
211 ParseNext = ParseBuffer;
212 Line++;
213 while (1)
215 if (fgets(ParseBuffer, 511, SpecFp) == NULL)
216 return NULL;
217 if (ParseBuffer[0] != '#')
218 break;
222 return token;
225 static int ParseVariable(int ordinal, int type)
227 ORDDEF *odp;
228 ORDVARDEF *vdp;
229 char export_name[80];
230 char *token;
231 char *endptr;
232 int *value_array;
233 int n_values;
234 int value_array_size;
236 strcpy(export_name, GetToken());
238 token = GetToken();
239 if (*token != '(')
241 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
242 exit(1);
245 n_values = 0;
246 value_array_size = 25;
247 value_array = xmalloc(sizeof(*value_array) * value_array_size);
249 while ((token = GetToken()) != NULL)
251 if (*token == ')')
252 break;
254 value_array[n_values++] = strtol(token, &endptr, 0);
255 if (n_values == value_array_size)
257 value_array_size += 25;
258 value_array = xrealloc(value_array,
259 sizeof(*value_array) * value_array_size);
262 if (endptr == NULL || *endptr != '\0')
264 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
265 token);
266 exit(1);
270 if (token == NULL)
272 fprintf(stderr, "%d: End of file in variable declaration\n", Line);
273 exit(1);
276 if (ordinal >= MAX_ORDINALS)
278 fprintf(stderr, "%d: Ordinal number too large\n", Line);
279 exit(1);
282 odp = &OrdinalDefinitions[ordinal];
283 odp->type = type;
284 strcpy(odp->export_name, export_name);
286 vdp = xmalloc(sizeof(*vdp));
287 odp->additional_data = vdp;
289 vdp->n_values = n_values;
290 vdp->values = xrealloc(value_array, sizeof(*value_array) * n_values);
292 return 0;
295 static int ParseExportFunction(int ordinal, int type)
297 char *token;
298 ORDDEF *odp;
299 ORDFUNCDEF *fdp;
300 int i;
302 odp = &OrdinalDefinitions[ordinal];
303 strcpy(odp->export_name, GetToken());
304 odp->type = type;
305 fdp = xmalloc(sizeof(*fdp));
306 odp->additional_data = fdp;
308 token = GetToken();
309 if (*token != '(')
311 fprintf(stderr, "%d: Expected '(' got '%s'\n", Line, token);
312 exit(1);
315 for (i = 0; i < 16; i++)
317 token = GetToken();
318 if (*token == ')')
319 break;
321 if (!strcmp(token, "byte") || !strcmp(token, "word"))
322 fdp->arg_types[i] = 'w';
323 else if (!strcmp(token, "s_byte") || !strcmp(token, "s_word"))
324 fdp->arg_types[i] = 's';
325 else if (!strcmp(token, "long") || !strcmp(token, "segptr"))
326 fdp->arg_types[i] = 'l';
327 else if (!strcmp(token, "ptr"))
328 fdp->arg_types[i] = 'p';
329 else if (!strcmp(token, "..."))
330 fdp->arg_types[i] = '.';
331 else
333 fprintf(stderr, "%d: Unknown variable type '%s'\n", Line, token);
334 exit(1);
337 fdp->arg_types[i] = '\0';
339 strcpy(fdp->internal_name, GetToken());
340 return 0;
343 static int ParseEquate(int ordinal)
345 ORDDEF *odp;
346 char *token;
347 char *endptr;
348 int value;
350 odp = &OrdinalDefinitions[ordinal];
351 strcpy(odp->export_name, GetToken());
353 token = GetToken();
354 value = strtol(token, &endptr, 0);
355 if (endptr == NULL || *endptr != '\0')
357 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
358 token);
359 exit(1);
362 odp->type = TYPE_ABS;
363 odp->additional_data = (void *) value;
365 return 0;
368 static int ParseReturn(int ordinal)
370 ORDDEF *odp;
371 ORDRETDEF *rdp;
372 char *token;
373 char *endptr;
375 rdp = xmalloc(sizeof(*rdp));
377 odp = &OrdinalDefinitions[ordinal];
378 strcpy(odp->export_name, GetToken());
379 odp->type = TYPE_RETURN;
380 odp->additional_data = rdp;
382 token = GetToken();
383 rdp->arg_size = strtol(token, &endptr, 0);
384 if (endptr == NULL || *endptr != '\0')
386 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
387 token);
388 exit(1);
391 token = GetToken();
392 rdp->ret_value = strtol(token, &endptr, 0);
393 if (endptr == NULL || *endptr != '\0')
395 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
396 token);
397 exit(1);
400 return 0;
404 static int ParseStub( int ordinal )
406 ORDDEF *odp;
407 ORDFUNCDEF *fdp;
409 odp = &OrdinalDefinitions[ordinal];
410 strcpy( odp->export_name, GetToken() );
411 odp->type = TYPE_STUB;
412 fdp = xmalloc(sizeof(*fdp));
413 odp->additional_data = fdp;
414 fdp->arg_types[0] = '\0';
415 strcpy( fdp->internal_name, STUB_CALLBACK );
416 return 0;
420 static int ParseOrdinal(int ordinal)
422 char *token;
424 if (ordinal >= MAX_ORDINALS)
426 fprintf(stderr, "%d: Ordinal number too large\n", Line);
427 exit(1);
429 if (ordinal > Limit) Limit = ordinal;
431 token = GetToken();
432 if (token == NULL)
434 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
435 exit(1);
438 if (strcmp(token, "byte") == 0)
439 return ParseVariable(ordinal, TYPE_BYTE);
440 else if (strcmp(token, "word") == 0)
441 return ParseVariable(ordinal, TYPE_WORD);
442 else if (strcmp(token, "long") == 0)
443 return ParseVariable(ordinal, TYPE_LONG);
444 else if (strcmp(token, "p") == 0)
445 return ParseExportFunction(ordinal, TYPE_PASCAL);
446 else if (strcmp(token, "pascal") == 0)
447 return ParseExportFunction(ordinal, TYPE_PASCAL);
448 else if (strcmp(token, "pascal16") == 0)
449 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
450 else if (strcmp(token, "register") == 0)
451 return ParseExportFunction(ordinal, TYPE_REGISTER);
452 else if (strcmp(token, "stdcall") == 0)
453 return ParseExportFunction(ordinal, TYPE_STDCALL);
454 else if (strcmp(token, "cdecl") == 0)
455 return ParseExportFunction(ordinal, TYPE_CDECL);
456 else if (strcmp(token, "equate") == 0)
457 return ParseEquate(ordinal);
458 else if (strcmp(token, "return") == 0)
459 return ParseReturn(ordinal);
460 else if (strcmp(token, "stub") == 0)
461 return ParseStub(ordinal);
462 else
464 fprintf(stderr,
465 "%d: Expected type after ordinal, found '%s' instead\n",
466 Line, token);
467 exit(1);
471 static int ParseTopLevel(void)
473 char *token;
475 while ((token = GetToken()) != NULL)
477 if (strcmp(token, "name") == 0)
479 strcpy(LowerDLLName, GetToken());
480 strlower(LowerDLLName);
482 strcpy(UpperDLLName, LowerDLLName);
483 strupper(UpperDLLName);
485 else if (strcmp(token, "id") == 0)
487 token = GetToken();
488 if (!IsNumberString(token))
490 fprintf(stderr, "%d: Expected number after id\n", Line);
491 exit(1);
494 DLLId = atoi(token);
496 else if (strcmp(token, "base") == 0)
498 token = GetToken();
499 if (!IsNumberString(token))
501 fprintf(stderr, "%d: Expected number after base\n", Line);
502 exit(1);
505 Base = atoi(token);
507 else if (IsNumberString(token))
509 int ordinal;
510 int rv;
512 ordinal = atoi(token);
513 if ((rv = ParseOrdinal(ordinal)) < 0)
514 return rv;
516 else
518 fprintf(stderr,
519 "%d: Expected name, id, length or ordinal\n", Line);
520 exit(1);
524 return 0;
528 static int OutputVariableCode( char *storage, ORDDEF *odp )
530 ORDVARDEF *vdp;
531 int i;
533 vdp = odp->additional_data;
534 printf( "\t.data\n" );
535 for (i = 0; i < vdp->n_values; i++)
537 if ((i & 7) == 0)
538 printf( "\t%s\t", storage);
540 printf( "%d", vdp->values[i]);
542 if ((i & 7) == 7 || i == vdp->n_values - 1) printf( "\n");
543 else printf( ", ");
545 printf( "\n");
546 printf( "\t.text\n" );
547 return vdp->n_values;
551 /*******************************************************************
552 * BuildModule
554 * Build the in-memory representation of the module, and dump it
555 * as a byte stream into the assembly code.
557 static void BuildModule( int max_code_offset, int max_data_offset )
559 ORDDEF *odp;
560 int i, size;
561 char *buffer;
562 NE_MODULE *pModule;
563 SEGTABLEENTRY *pSegment;
564 OFSTRUCT *pFileInfo;
565 BYTE *pstr, *bundle;
566 WORD *pword;
568 /* Module layout:
569 * NE_MODULE Module
570 * OFSTRUCT File information
571 * SEGTABLEENTRY Segment 1 (code)
572 * SEGTABLEENTRY Segment 2 (data)
573 * WORD[2] Resource table (empty)
574 * BYTE[2] Imported names (empty)
575 * BYTE[n] Resident names table
576 * BYTE[n] Entry table
579 buffer = xmalloc( 0x10000 );
581 pModule = (NE_MODULE *)buffer;
582 pModule->magic = NE_SIGNATURE;
583 pModule->count = 1;
584 pModule->next = 0;
585 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_BUILTIN | NE_FFLAGS_LIBMODULE;
586 pModule->dgroup = 2;
587 pModule->heap_size = 0xffff;
588 pModule->stack_size = 0;
589 pModule->ip = 0;
590 pModule->cs = 0;
591 pModule->sp = 0;
592 pModule->ss = 0;
593 pModule->seg_count = 2;
594 pModule->modref_count = 0;
595 pModule->nrname_size = 0;
596 pModule->modref_table = 0;
597 pModule->nrname_fpos = 0;
598 pModule->moveable_entries = 0;
599 pModule->alignment = 0;
600 pModule->truetype = 0;
601 pModule->os_flags = NE_OSFLAGS_WINDOWS;
602 pModule->misc_flags = 0;
603 pModule->dlls_to_init = 0;
604 pModule->nrname_handle = 0;
605 pModule->min_swap_area = 0;
606 pModule->expected_version = 0x030a;
608 /* File information */
610 pFileInfo = (OFSTRUCT *)(pModule + 1);
611 pModule->fileinfo = (int)pFileInfo - (int)pModule;
612 memset( pFileInfo, 0, sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName) );
613 pFileInfo->cBytes = sizeof(*pFileInfo) - sizeof(pFileInfo->szPathName)
614 + strlen(UpperDLLName) + 4;
615 sprintf( pFileInfo->szPathName, "%s.DLL", UpperDLLName );
616 pstr = (char *)pFileInfo + pFileInfo->cBytes + 1;
618 /* Segment table */
620 pSegment = (SEGTABLEENTRY *)pstr;
621 pModule->seg_table = (int)pSegment - (int)pModule;
622 pSegment->filepos = 0;
623 pSegment->size = max_code_offset;
624 pSegment->flags = 0;
625 pSegment->minsize = max_code_offset;
626 pSegment->selector = 0;
627 pSegment++;
629 pModule->dgroup_entry = (int)pSegment - (int)pModule;
630 pSegment->filepos = 0;
631 pSegment->size = max_data_offset;
632 pSegment->flags = NE_SEGFLAGS_DATA;
633 pSegment->minsize = max_data_offset;
634 pSegment->selector = 0;
635 pSegment++;
637 /* Resource table */
639 pword = (WORD *)pSegment;
640 pModule->res_table = (int)pword - (int)pModule;
641 *pword++ = 0;
642 *pword++ = 0;
644 /* Imported names table */
646 pstr = (char *)pword;
647 pModule->import_table = (int)pstr - (int)pModule;
648 *pstr++ = 0;
649 *pstr++ = 0;
651 /* Resident names table */
653 pModule->name_table = (int)pstr - (int)pModule;
654 /* First entry is module name */
655 *pstr = strlen(UpperDLLName );
656 strcpy( pstr + 1, UpperDLLName );
657 pstr += *pstr + 1;
658 *(WORD *)pstr = 0;
659 pstr += sizeof(WORD);
660 /* Store all ordinals */
661 odp = OrdinalDefinitions + 1;
662 for (i = 1; i <= Limit; i++, odp++)
664 if (!odp->export_name[0]) continue;
665 *pstr = strlen( odp->export_name );
666 strcpy( pstr + 1, odp->export_name );
667 strupper( pstr + 1 );
668 pstr += *pstr + 1;
669 *(WORD *)pstr = i;
670 pstr += sizeof(WORD);
672 *pstr++ = 0;
674 /* Entry table */
676 pModule->entry_table = (int)pstr - (int)pModule;
677 bundle = NULL;
678 odp = OrdinalDefinitions + 1;
679 for (i = 1; i <= Limit; i++, odp++)
681 int selector = 0;
683 switch (odp->type)
685 case TYPE_INVALID:
686 selector = 0; /* Invalid selector */
687 break;
689 case TYPE_PASCAL:
690 case TYPE_PASCAL_16:
691 case TYPE_REGISTER:
692 case TYPE_RETURN:
693 case TYPE_STUB:
694 selector = 1; /* Code selector */
695 break;
697 case TYPE_BYTE:
698 case TYPE_WORD:
699 case TYPE_LONG:
700 selector = 2; /* Data selector */
701 break;
703 case TYPE_ABS:
704 selector = 0xfe; /* Constant selector */
705 break;
708 /* create a new bundle if necessary */
709 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
711 bundle = pstr;
712 bundle[0] = 0;
713 bundle[1] = selector;
714 pstr += 2;
717 (*bundle)++;
718 if (selector != 0)
720 *pstr++ = 1;
721 *(WORD *)pstr = odp->offset;
722 pstr += sizeof(WORD);
725 *pstr++ = 0;
727 /* Dump the module content */
729 printf( "\t.data\n" );
730 printf( "\t.globl " PREFIX "%s_Module_Start\n", UpperDLLName );
731 printf( PREFIX "%s_Module_Start:\n", UpperDLLName );
732 size = (int)pstr - (int)pModule;
733 for (i = 0, pstr = buffer; i < size; i++, pstr++)
735 if (!(i & 7)) printf( "\t.byte " );
736 printf( "%d%c", *pstr, ((i & 7) != 7) ? ',' : '\n' );
738 if (i & 7) printf( "0\n" );
739 printf( "\t.globl " PREFIX "%s_Module_End\n", UpperDLLName );
740 printf( PREFIX "%s_Module_End:\n", UpperDLLName );
744 static void BuildSpec32Files( char *specname )
746 ORDDEF *odp;
747 ORDFUNCDEF *fdp;
748 ORDRETDEF *rdp;
749 int i;
750 int varargs;
752 SpecFp = fopen( specname, "r");
753 if (SpecFp == NULL)
755 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
756 exit(1);
759 ParseTopLevel();
761 printf( "/* File generated automatically, do not edit! */\n" );
762 printf( "#include <sys/types.h>\n");
763 printf( "#include <stdarg.h>\n");
764 printf( "#include \"windows.h\"\n");
765 printf( "#include \"dlls.h\"\n");
766 printf( "#include \"pe_image.h\"\n");
767 printf( "#include \"winerror.h\"\n");
768 printf( "#include \"relay32.h\"\n");
769 printf( "#include \"stddebug.h\"\n");
770 printf( "#include \"debug.h\"\n");
772 odp = OrdinalDefinitions;
773 for (i = 0; i <= Limit; i++, odp++)
775 int argno,argc;
776 fdp = odp->additional_data;
777 rdp = odp->additional_data;
779 switch (odp->type)
781 case TYPE_INVALID:
782 case TYPE_STUB:
783 printf( "int %s_%d()\n{\n\t", UpperDLLName, i);
784 printf( "RELAY32_Unimplemented(\"%s\",%d);\n", UpperDLLName, i);
785 printf( "\t/*NOTREACHED*/\n\treturn 0;\n}\n\n");
786 break;
787 case TYPE_STDCALL:
788 case TYPE_CDECL:
789 varargs=0;
790 argc=strlen(fdp->arg_types);
791 #if 0
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));\n" );
813 #endif
814 printf( "void %s_%d(", UpperDLLName, i);
815 for(argno=0;argno<argc;argno++)
817 if(odp->type == TYPE_STDCALL) {
818 switch(fdp->arg_types[argno])
820 case 'p': printf( "void *");break;
821 case 'l': printf( "int ");break;
822 default:
823 fprintf(stderr, "Not supported argument type %c\n",
824 fdp->arg_types[argno]);
825 exit(1);
827 } else {
828 switch(fdp->arg_types[argno])
830 case 'p': printf( "void *");break;
831 case 'l': printf( "int ");break;
832 case '.': printf( "... ");varargs=argno;break;
833 default:
834 fprintf(stderr, "Not supported argument type %c\n",
835 fdp->arg_types[argno]);
836 exit(1);
839 if(fdp->arg_types[argno]!='.') putchar( 'a'+argno );
840 if (argno!=argc-1) putchar( ',' );
842 printf( ")" );
843 printf( "\n{\n" );
844 if (varargs) printf( "\tva_list valist;\n\n\tva_start(valist, %c);",
845 'a'+varargs-1 );
846 printf( "\tdprintf_relay(stddeb,\"Call %%s.%%s(");
847 for (argno=0;argno<argc;argno++)
848 if(fdp->arg_types[argno]!='.')
850 putchar( '%' );
851 putchar( (fdp->arg_types[argno] == 'p') ? 'p' : 'x' );
852 if (argno < argc-1) putchar( ',' );
854 printf( ")\\n\", \"%s\", \"%s\"", UpperDLLName, odp->export_name);
855 for(argno=0;argno<argc;argno++)
856 if(fdp->arg_types[argno]!='.') printf( ",%c", 'a'+argno);
857 printf( ");\n\t%s(", fdp->internal_name );
858 for(argno=0;argno<argc;argno++)
860 if (fdp->arg_types[argno]=='.') printf("valist");
861 else putchar('a'+argno);
862 if (argno!=argc-1) putchar(',');
864 printf( ");\n");
865 if(odp->type == TYPE_STDCALL) {
866 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp\");\n");
867 printf( "\t__asm__ __volatile__ (\"popl %%ebp\");\n");
868 printf( "\t__asm__ __volatile__ (\"addl $%d,%%esp\");\n", argc*4+4);
869 printf( "\t__asm__ __volatile__ (\"jmp -%d(%%esp)\");\n", argc*4+4);
871 printf( "}\n\n");
872 break;
873 case TYPE_RETURN:
874 printf( "void %s_%d()\n{\n\t", UpperDLLName, i);
875 printf( "RELAY32_DebugEnter(\"%s\",\"%s\");\n\t",
876 UpperDLLName, odp->export_name);
877 printf( "WIN32_LastError=ERROR_CALL_NOT_IMPLEMENTED;\n");
878 printf( "\t__asm__ __volatile__ (\"movl $%d,%%eax\");\n",
879 rdp->ret_value);
880 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp;popl %%ebp;"
881 "ret $%d\");\n}\n\n", rdp->arg_size);
882 break;
883 default:
884 fprintf(stderr,"build: function type %d not available for Win32\n",
885 odp->type);
886 break;
890 printf( "static WIN32_function functions[%d+1]={\n", Limit);
892 odp = OrdinalDefinitions;
893 for (i = 0; i <= Limit; i++, odp++)
895 fdp = odp->additional_data;
896 rdp = odp->additional_data;
898 switch (odp->type)
900 case TYPE_INVALID:
901 printf( "{0,%s_%d},\n",UpperDLLName, i);
902 break;
903 case TYPE_RETURN:
904 case TYPE_STDCALL:
905 case TYPE_CDECL:
906 case TYPE_STUB:
907 printf( "{\"%s\",%s_%d},\n", odp->export_name, UpperDLLName, i);
908 break;
909 default:
910 fprintf(stderr, "build: implementation error: missing %d\n",
911 odp->type);
912 exit(1);
915 printf("};\n\n");
917 printf( "static WIN32_builtin dll={\"%s\",functions,%d,%d,0};\n",
918 UpperDLLName, Limit+1, Base);
920 printf( "void %s_Init(void)\n{\n",UpperDLLName);
921 printf( "\tdll.next=WIN32_builtin_list;\n");
922 printf( "\tWIN32_builtin_list=&dll;\n\tRELAY32_MakeFakeModule(&dll);\n}");
926 static void BuildSpec16Files( char *specname )
928 ORDDEF *odp;
929 ORDFUNCDEF *fdp;
930 ORDRETDEF *rdp;
931 int i;
932 int code_offset, data_offset;
934 SpecFp = fopen( specname, "r");
935 if (SpecFp == NULL)
937 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
938 exit(1);
941 ParseTopLevel();
943 printf( "/* File generated automatically; do not edit! */\n" );
944 printf( "\t.data\n" );
945 printf( "\t.globl " PREFIX "%s_Data_Start\n", UpperDLLName );
946 printf( PREFIX "%s_Data_Start:\n", UpperDLLName );
947 #ifdef __svr4__
948 printf( "\t.4byte 0,0,0,0,0,0,0,0\n" );
949 #else
950 printf( "\t.word 0,0,0,0,0,0,0,0\n" );
951 #endif
952 data_offset = 16;
953 printf( "\t.text\n" );
954 printf( "\t.globl " PREFIX "%s_Code_Start\n", UpperDLLName );
955 printf( PREFIX "%s_Code_Start:\n", UpperDLLName );
956 code_offset = 0;
958 odp = OrdinalDefinitions;
959 for (i = 0; i <= Limit; i++, odp++)
961 fdp = odp->additional_data;
962 rdp = odp->additional_data;
964 switch (odp->type)
966 case TYPE_INVALID:
967 odp->offset = 0xffff;
968 break;
970 case TYPE_ABS:
971 odp->offset = (int)odp->additional_data & 0xffff;
972 break;
974 case TYPE_BYTE:
975 printf( "/* %s.%d */\n", UpperDLLName, i);
976 odp->offset = data_offset;
977 data_offset += OutputVariableCode( ".byte", odp);
978 break;
980 case TYPE_WORD:
981 printf( "/* %s.%d */\n", UpperDLLName, i);
982 odp->offset = data_offset;
983 #ifdef __svr4__
984 data_offset += 2 * OutputVariableCode( ".4byte", odp);
985 #else
986 data_offset += 2 * OutputVariableCode( ".word", odp);
987 #endif
988 break;
990 case TYPE_LONG:
991 printf( "/* %s.%d */\n", UpperDLLName, i);
992 odp->offset = data_offset;
993 data_offset += 4 * OutputVariableCode( ".long", odp);
994 break;
996 case TYPE_RETURN:
997 printf( "/* %s.%d */\n", UpperDLLName, i);
998 printf( "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
999 printf( "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
1000 printf( "\t.byte 0x66\n");
1001 if (rdp->arg_size != 0)
1002 printf( "\tlret $%d\n", rdp->arg_size);
1003 else
1004 printf( "\tlret\n");
1005 odp->offset = code_offset;
1006 code_offset += 10; /* Assembly code is 10 bytes long */
1007 if (rdp->arg_size != 0) code_offset += 2;
1008 break;
1010 case TYPE_REGISTER:
1011 case TYPE_PASCAL:
1012 case TYPE_PASCAL_16:
1013 case TYPE_STUB:
1014 printf( "/* %s.%d */\n", UpperDLLName, i);
1015 printf( "\tpushw %%bp\n" );
1016 printf( "\tpushl $0x%08x\n", (DLLId << 16) | i);
1017 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
1018 printf( "\tljmp $0x%04x, $" PREFIX "CallTo32_%s_%s\n\n",
1019 WINE_CODE_SELECTOR,
1020 (odp->type == TYPE_REGISTER) ? "regs" :
1021 (odp->type == TYPE_PASCAL) ? "long" : "word",
1022 fdp->arg_types );
1023 printf( "\tnop\n" );
1024 printf( "\tnop\n" );
1025 printf( "\tnop\n" );
1026 printf( "\tnop\n" );
1027 printf( "\tnop\n" );
1028 odp->offset = code_offset;
1029 code_offset += 24; /* Assembly code is 24 bytes long */
1030 break;
1032 default:
1033 fprintf( stderr, "build: Unknown function type; please report.\n");
1034 break;
1038 if (!code_offset) /* Make sure the code segment is not empty */
1040 printf( "\t.byte 0\n" );
1041 code_offset++;
1044 BuildModule( code_offset, data_offset );
1048 /*******************************************************************
1049 * BuildCall32LargeStack
1051 * Build the function used to switch to the original 32-bit stack
1052 * before calling a 32-bit function from 32-bit code. This is used for
1053 * functions that need a large stack, like X bitmaps functions.
1055 * The generated function has the following prototype:
1056 * int CallTo32_LargeStack( int (*func)(), int nbargs, ... )
1058 * Stack layout:
1059 * ... ...
1060 * (ebp+20) arg2
1061 * (ebp+16) arg1
1062 * (ebp+12) nbargs
1063 * (ebp+8) func
1064 * (ebp+4) ret addr
1065 * (ebp) ebp
1067 static void BuildCall32LargeStack(void)
1069 /* Function header */
1071 printf( "/**********\n" );
1072 printf( " * " PREFIX "CallTo32_LargeStack\n" );
1073 printf( " **********/\n" );
1074 printf( "\t.align 4\n" );
1075 printf( "\t.globl " PREFIX "CallTo32_LargeStack\n\n" );
1076 printf( PREFIX "CallTo32_LargeStack:\n" );
1078 /* Entry code */
1080 printf( "\tpushl %%ebp\n" );
1081 printf( "\tmovl %%esp,%%ebp\n" );
1083 /* Save registers */
1085 printf( "\tpushl %%ecx\n" );
1086 printf( "\tpushl %%esi\n" );
1087 printf( "\tpushl %%edi\n" );
1089 /* Retrieve the original 32-bit stack pointer and switch to it if any */
1091 printf( "\tmovl " PREFIX "IF1632_Original32_esp, %%eax\n" );
1092 printf( "\torl %%eax,%%eax\n" );
1093 printf( "\tje no_orig_esp\n" );
1094 printf( "\tmovl %%eax,%%esp\n" );
1095 printf( "no_orig_esp:\n" );
1097 /* Transfer the arguments */
1099 printf( "\tmovl 12(%%ebp),%%ecx\n" );
1100 printf( "\torl %%ecx,%%ecx\n" );
1101 printf( "\tje no_args\n" );
1102 printf( "\tleal 16(%%ebp),%%esi\n" );
1103 printf( "\tshll $2,%%ecx\n" );
1104 printf( "\tsubl %%ecx,%%esp\n" );
1105 printf( "\tmovl %%esp,%%edi\n" );
1106 printf( "\tshrl $2,%%ecx\n" );
1107 printf( "\tcld\n" );
1108 printf( "\trep; movsl\n" );
1109 printf( "no_args:\n" );
1111 /* Call the function */
1113 printf( "\tcall 8(%%ebp)\n" );
1115 /* Switch back to the normal stack */
1117 printf( "\tleal -12(%%ebp),%%esp\n" );
1119 /* Restore registers and return */
1121 printf( "\tpopl %%edi\n" );
1122 printf( "\tpopl %%esi\n" );
1123 printf( "\tpopl %%ecx\n" );
1124 printf( "\tpopl %%ebp\n" );
1125 printf( "\tret\n" );
1129 /*******************************************************************
1130 * TransferArgs16To32
1132 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
1133 * The 16-bit stack layout is:
1134 * ... ...
1135 * (bp+8) arg2
1136 * (bp+6) arg1
1137 * (bp+4) cs
1138 * (bp+2) ip
1139 * (bp) bp
1141 static int TransferArgs16To32( char *args )
1143 int i, pos16, pos32;
1145 /* Save ebx first */
1147 printf( "\tpushl %%ebx\n" );
1149 /* Get the 32-bit stack pointer */
1151 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1153 /* Copy the arguments */
1155 pos16 = 6; /* skip bp and return address */
1156 pos32 = 0;
1158 for (i = strlen(args); i > 0; i--)
1160 pos32 -= 4;
1161 switch(args[i-1])
1163 case 'w': /* word */
1164 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1165 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1166 pos16 += 2;
1167 break;
1169 case 's': /* s_word */
1170 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
1171 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1172 pos16 += 2;
1173 break;
1175 case 'l': /* long */
1176 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
1177 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1178 pos16 += 4;
1179 break;
1181 case 'p': /* ptr */
1182 /* Get the selector */
1183 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1184 /* Get the selector base */
1185 printf( "\tandl $0xfff8,%%eax\n" );
1186 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1187 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1188 /* Add the offset */
1189 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1190 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1191 pos16 += 4;
1192 break;
1194 default:
1195 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1199 /* Restore ebx */
1201 printf( "\tpopl %%ebx\n" );
1203 return pos16 - 6; /* Return the size of the 16-bit args */
1207 /*******************************************************************
1208 * BuildContext
1210 * Build the context structure on the 32-bit stack.
1211 * The only valid registers in the context structure are:
1212 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1214 static void BuildContext(void)
1216 /* Save ebx first */
1218 printf( "\tpushl %%ebx\n" );
1220 /* Get the 32-bit stack pointer */
1222 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1224 /* Store the registers */
1226 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx) ); /* Get ebx from stack */
1227 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax) );
1228 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx) );
1229 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx) );
1230 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi) );
1231 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi) );
1232 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1233 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds) );
1234 printf( "\tmovw -12(%%ebp),%%ax\n" ); /* Get saved es from stack */
1235 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1236 printf( "\tpushfl\n" );
1237 #ifndef __FreeBSD__
1238 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1239 #else
1240 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1241 #endif
1245 /*******************************************************************
1246 * RestoreContext
1248 * Restore the registers from the context structure
1250 static void RestoreContext(void)
1252 /* Get the 32-bit stack pointer */
1254 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1256 /* Restore the registers */
1258 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx) );
1259 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx) );
1260 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi) );
1261 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi) );
1262 printf( "\tpopl %%eax\n" ); /* Remove old ds and es from stack */
1263 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds) ); /* Push new ds */
1264 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) ); /* Push new es */
1265 #ifndef __FreeBSD__
1266 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1267 #else
1268 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1269 #endif
1270 printf( "\tpopfl\n" );
1271 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax) );
1272 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx) );
1276 /*******************************************************************
1277 * BuildCall32Func
1279 * Build a 32-bit callback function. The syntax of the function
1280 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1281 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1282 * 'l'=long, 'p'=pointer).
1283 * For register functions, the arguments are ignored, but they are still
1284 * removed from the stack upon return.
1286 * Stack layout upon entry to the callback function:
1287 * ... ...
1288 * (sp+14) first 16-bit arg
1289 * (sp+12) cs (word)
1290 * (sp+10) ip (word)
1291 * (sp+8) bp (word)
1292 * (sp+4) dll_id+ordinal (long)
1293 * (sp) entrypoint (long)
1296 static void BuildCall32Func( char *profile )
1298 int argsize = 0;
1299 int short_ret = 0;
1300 int reg_func = 0;
1301 char *args = profile + 5;
1303 /* Parse function type */
1305 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1306 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1307 else if (strncmp( "long_", profile, 5 ))
1309 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1310 return;
1313 /* Function header */
1315 printf( "/**********\n" );
1316 printf( " * " PREFIX "CallTo32_%s\n", profile );
1317 printf( " **********/\n" );
1318 printf( "\t.align 4\n" );
1319 printf( "\t.globl " PREFIX "CallTo32_%s\n\n", profile );
1320 printf( PREFIX "CallTo32_%s:\n", profile );
1322 /* Setup bp to point to its copy on the stack */
1324 printf( "\tmovzwl %%sp,%%ebp\n" );
1325 printf( "\taddw $8,%%bp\n" );
1327 /* Save 16-bit ds and es */
1329 printf( "\tpushw %%ds\n" );
1330 printf( "\tpushw %%es\n" );
1332 /* Restore 32-bit ds and es */
1334 printf( "\tpushl $0x%04x%04x\n", WINE_DATA_SELECTOR, WINE_DATA_SELECTOR );
1335 printf( "\tpopw %%ds\n" );
1336 printf( "\tpopw %%es\n" );
1339 /* Save the 16-bit stack */
1341 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1342 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1343 #ifdef __svr4__
1344 printf("\tdata16\n");
1345 #endif
1346 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1347 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1349 /* Transfer the arguments */
1351 if (reg_func) BuildContext();
1352 else if (*args) argsize = TransferArgs16To32( args );
1354 /* Get the address of the API function */
1356 printf( "\tmovl -8(%%ebp),%%eax\n" );
1358 /* If necessary, save %edx over the API function address */
1360 if (!reg_func && short_ret)
1361 printf( "\tmovl %%edx,-8(%%ebp)\n" );
1363 /* Switch to the 32-bit stack */
1365 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebp\n" );
1366 printf( "\tpushw %%ds\n" );
1367 printf( "\tpopw %%ss\n" );
1368 printf( "\tleal -%d(%%ebp),%%esp\n",
1369 reg_func ? sizeof(struct sigcontext_struct) : 4 * strlen(args) );
1371 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1373 printf( "\taddl $24,%%ebp\n" );
1375 /* Print the debug information before the call */
1377 if (debugging)
1379 printf( "\tpushl %%eax\n" );
1380 printf( "\tpushl $CALL32_Str_%s\n", profile );
1381 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1382 printf( "\tcall " PREFIX "RELAY_DebugCall32\n" );
1383 printf( "\tpopl %%eax\n" );
1384 printf( "\tpopl %%eax\n" );
1385 printf( "\tpopl %%eax\n" );
1388 /* Call the entry point */
1390 printf( "\tcall %%eax\n" );
1392 /* Print the debug information after the call */
1394 if (debugging)
1396 printf( "\tpushl %%eax\n" );
1397 printf( "\tpushl $%d\n", reg_func ? 2 : (short_ret ? 1 : 0) );
1398 printf( "\tcall " PREFIX "RELAY_DebugReturn\n" );
1399 printf( "\tpopl %%eax\n" );
1400 printf( "\tpopl %%eax\n" );
1403 /* Restore the 16-bit stack */
1405 #ifdef __svr4__
1406 printf( "\tdata16\n");
1407 #endif
1408 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1409 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1410 #ifdef __svr4__
1411 printf( "\tdata16\n");
1412 #endif
1413 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1414 #ifdef __svr4__
1415 printf( "\tdata16\n");
1416 #endif
1417 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1419 if (reg_func)
1421 /* Restore registers from the context structure */
1422 RestoreContext();
1424 /* Calc the arguments size */
1425 while (*args)
1427 switch(*args)
1429 case 'w':
1430 case 's':
1431 argsize += 2;
1432 break;
1433 case 'p':
1434 case 'l':
1435 argsize += 4;
1436 break;
1437 default:
1438 fprintf( stderr, "Unknown arg type '%c'\n", *args );
1440 args++;
1444 /* Restore ds and es */
1446 printf( "\tpopw %%es\n" );
1447 printf( "\tpopw %%ds\n" );
1449 /* Get the return value into dx:ax and clean up the stack */
1451 if (!reg_func)
1453 if (short_ret)
1455 printf( "\tpopl %%edx\n" ); /* Restore %edx */
1456 printf( "\taddl $4,%%esp\n" ); /* Remove DLL id and ordinal */
1458 else
1460 printf( "\tpushl %%eax\n" );
1461 printf( "\tpopw %%ax\n" );
1462 printf( "\tpopw %%dx\n" );
1463 /* Remove API entry point, DLL id and ordinal from the stack */
1464 printf( "\taddl $8,%%esp\n" );
1467 else
1469 /* Remove API entry point, DLL id and ordinal from the stack, */
1470 /* but take care not to change the value of the carry flag. */
1472 printf( "\tpopl %%ebp\n" );
1473 printf( "\tpopl %%ebp\n" );
1476 /* Restore bp */
1478 printf( "\tpopw %%bp\n" );
1480 /* Remove the arguments and return */
1482 if (argsize)
1484 printf( "\t.byte 0x66\n" );
1485 printf( "\tlret $%d\n", argsize );
1487 else
1489 printf( "\t.byte 0x66\n" );
1490 printf( "\tlret\n" );
1495 /*******************************************************************
1496 * BuildCall16Func
1498 * Build a 16-bit callback function.
1500 * Stack frame of the callback function:
1501 * ... ...
1502 * (ebp+24) arg2
1503 * (ebp+20) arg1
1504 * (ebp+16) 16-bit ds
1505 * (ebp+12) func to call
1506 * (ebp+8) code selector
1507 * (ebp+4) return address
1508 * (ebp) previous ebp
1510 * Prototypes for the CallTo16 functions:
1511 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1512 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1513 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1514 * WORD ax, WORD bx, WORD cx, WORD dx,
1515 * WORD si, WORD di );
1517 static void BuildCall16Func( char *profile )
1519 int short_ret = 0;
1520 int reg_func = 0;
1521 char *args = profile + 5;
1523 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1524 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1525 else if (strncmp( "long_", profile, 5 ))
1527 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1528 return;
1531 /* Function header */
1533 printf( "/**********\n" );
1534 printf( " * " PREFIX "CallTo16_%s\n", profile );
1535 printf( " **********/\n" );
1536 printf( "\t.align 4\n" );
1537 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1538 printf( PREFIX "CallTo16_%s:\n", profile );
1540 /* Push code selector before return address to simulate a lcall */
1542 printf( "\tpopl %%eax\n" );
1543 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1544 printf( "\tpushl %%eax\n" );
1546 /* Entry code */
1548 printf( "\tpushl %%ebp\n" );
1549 printf( "\tmovl %%esp,%%ebp\n" );
1551 /* Save the 32-bit registers */
1553 printf( "\tpushl %%ebx\n" );
1554 printf( "\tpushl %%ecx\n" );
1555 printf( "\tpushl %%edx\n" );
1556 printf( "\tpushl %%esi\n" );
1557 printf( "\tpushl %%edi\n" );
1559 /* Save the 32-bit stack */
1561 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1562 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1563 printf( "\tmovl %%ebp,%%ebx\n" );
1565 /* Print debugging info */
1567 if (debugging)
1569 /* Push the address of the first argument */
1570 printf( "\tmovl %%ebx,%%eax\n" );
1571 printf( "\taddl $12,%%eax\n" );
1572 printf( "\tpushl $%d\n", reg_func ? 8 : strlen(args) );
1573 printf( "\tpushl %%eax\n" );
1574 printf( "\tcall " PREFIX "RELAY_DebugCall16\n" );
1575 printf( "\tpopl %%eax\n" );
1576 printf( "\tpopl %%eax\n" );
1579 /* Switch to the 16-bit stack */
1581 #ifdef __svr4__
1582 printf("\tdata16\n");
1583 #endif
1584 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1585 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1587 /* Transfer the arguments */
1589 if (reg_func)
1591 /* Get the registers. ebx is handled later on. */
1592 printf( "\tpushw 20(%%ebx)\n" );
1593 printf( "\tpopw %%es\n" );
1594 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1595 printf( "\tmovl 28(%%ebx),%%eax\n" );
1596 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1597 printf( "\tmovl 40(%%ebx),%%edx\n" );
1598 printf( "\tmovl 44(%%ebx),%%esi\n" );
1599 printf( "\tmovl 48(%%ebx),%%edi\n" );
1601 else /* not a register function */
1603 int pos = 20; /* first argument position */
1605 /* Make %bp point to the previous stackframe (built by CallTo32) */
1606 printf( "\tmovw %%sp,%%bp\n" );
1607 printf( "\taddw $16,%%bp\n" );
1609 while (*args)
1611 switch(*args++)
1613 case 'w': /* word */
1614 printf( "\tpushw %d(%%ebx)\n", pos );
1615 break;
1616 case 'l': /* long */
1617 printf( "\tpushl %d(%%ebx)\n", pos );
1618 break;
1620 pos += 4;
1624 /* Push the return address */
1626 printf( "\tpushl " PREFIX "CALL16_RetAddr_%s\n",
1627 short_ret ? "word" : "long" );
1629 /* Push the called routine address */
1631 printf( "\tpushl 12(%%ebx)\n" );
1633 /* Get the 16-bit ds */
1635 if (reg_func)
1637 printf( "\tpushw 16(%%ebx)\n" );
1638 printf( "\tmovl 32(%%ebx),%%ebx\n" ); /*Get ebx from the 32-bit stack*/
1639 printf( "\tpopw %%ds\n" );
1641 else
1643 /* Set ax equal to ds for window procedures */
1644 printf( "\tmovw 16(%%ebx),%%ax\n" );
1645 #ifdef __svr4__
1646 printf( "\tdata16\n");
1647 #endif
1648 printf( "\tmovw %%ax,%%ds\n" );
1651 /* Jump to the called routine */
1653 printf( "\t.byte 0x66\n" );
1654 printf( "\tlret\n" );
1658 /*******************************************************************
1659 * BuildRet16Func
1661 * Build the return code for 16-bit callbacks
1663 static void BuildRet16Func()
1665 printf( "\t.globl " PREFIX "CALL16_Ret_word\n" );
1666 printf( "\t.globl " PREFIX "CALL16_Ret_long\n" );
1668 /* Put return value into eax */
1670 printf( PREFIX "CALL16_Ret_long:\n" );
1671 printf( "\tpushw %%dx\n" );
1672 printf( "\tpushw %%ax\n" );
1673 printf( "\tpopl %%eax\n" );
1674 printf( PREFIX "CALL16_Ret_word:\n" );
1676 /* Restore 32-bit segment registers */
1678 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1679 #ifdef __svr4__
1680 printf( "\tdata16\n");
1681 #endif
1682 printf( "\tmovw %%bx,%%ds\n" );
1683 #ifdef __svr4__
1684 printf( "\tdata16\n");
1685 #endif
1686 printf( "\tmovw %%bx,%%es\n" );
1687 #ifdef __svr4__
1688 printf( "\tdata16\n");
1689 #endif
1690 printf( "\tmovw %%bx,%%ss\n" );
1692 /* Restore the 32-bit stack */
1694 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1695 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1697 /* Restore the 32-bit registers */
1699 printf( "\tpopl %%edi\n" );
1700 printf( "\tpopl %%esi\n" );
1701 printf( "\tpopl %%edx\n" );
1702 printf( "\tpopl %%ecx\n" );
1703 printf( "\tpopl %%ebx\n" );
1705 /* Return to caller */
1707 printf( "\tpopl %%ebp\n" );
1708 printf( "\tlret\n" );
1710 /* Declare the return address variables */
1712 printf( "\t.data\n" );
1713 printf( "\t.globl " PREFIX "CALL16_RetAddr_word\n" );
1714 printf( "\t.globl " PREFIX "CALL16_RetAddr_long\n" );
1715 printf( PREFIX "CALL16_RetAddr_word:\t.long 0\n" );
1716 printf( PREFIX "CALL16_RetAddr_long:\t.long 0\n" );
1717 printf( "\t.text\n" );
1721 static void usage(void)
1723 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1724 " build -call32 FUNCTION_PROFILES\n"
1725 " build -call16 FUNCTION_PROFILES\n" );
1726 exit(1);
1730 int main(int argc, char **argv)
1732 int i;
1734 if (argc <= 2) usage();
1736 if (!strcmp( argv[1], "-spec16" ))
1738 for (i = 2; i < argc; i++) BuildSpec16Files( argv[i] );
1740 else if (!strcmp( argv[1], "-spec32" ))
1742 for (i = 2; i < argc; i++) BuildSpec32Files( argv[i] );
1744 else if (!strcmp( argv[1], "-call32" )) /* 32-bit callbacks */
1746 /* File header */
1748 printf( "/* File generated automatically. Do not edit! */\n\n" );
1749 printf( "\t.text\n" );
1751 /* Build the 32-bit large stack callback */
1753 BuildCall32LargeStack();
1755 /* Build the callback functions */
1757 for (i = 2; i < argc; i++) BuildCall32Func( argv[i] );
1759 /* Output the argument debugging strings */
1761 if (debugging)
1763 printf( "/* Argument strings */\n" );
1764 for (i = 2; i < argc; i++)
1766 printf( "CALL32_Str_%s:\n", argv[i] );
1767 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1771 else if (!strcmp( argv[1], "-call16" )) /* 16-bit callbacks */
1773 /* File header */
1775 printf( "/* File generated automatically. Do not edit! */\n\n" );
1776 printf( "\t.text\n" );
1777 printf( "\t.globl " PREFIX "CALL16_Start\n" );
1778 printf( PREFIX "CALL16_Start:\n" );
1780 /* Build the callback functions */
1782 for (i = 2; i < argc; i++) BuildCall16Func( argv[i] );
1784 /* Output the 16-bit return code */
1786 BuildRet16Func();
1788 printf( "\t.globl " PREFIX "CALL16_End\n" );
1789 printf( PREFIX "CALL16_End:\n" );
1791 else usage();
1793 return 0;