Release 950522
[wine.git] / tools / build.c
blobb79dc514c1363876bf93d07635e657b3dd3a9e45
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 if ((type == TYPE_REGISTER) && (i > 0))
300 fprintf( stderr, "%d: Register function can't have arguments\n", Line);
301 exit(1);
304 strcpy(fdp->internal_name, GetToken());
305 return 0;
308 static int ParseEquate(int ordinal)
310 ORDDEF *odp;
311 char *token;
312 char *endptr;
313 int value;
315 odp = &OrdinalDefinitions[ordinal];
316 strcpy(odp->export_name, GetToken());
318 token = GetToken();
319 value = strtol(token, &endptr, 0);
320 if (endptr == NULL || *endptr != '\0')
322 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
323 token);
324 exit(1);
327 odp->type = TYPE_ABS;
328 odp->additional_data = (void *) value;
330 return 0;
333 static int ParseReturn(int ordinal)
335 ORDDEF *odp;
336 ORDRETDEF *rdp;
337 char *token;
338 char *endptr;
340 rdp = malloc(sizeof(*rdp));
342 odp = &OrdinalDefinitions[ordinal];
343 strcpy(odp->export_name, GetToken());
344 odp->type = TYPE_RETURN;
345 odp->additional_data = rdp;
347 token = GetToken();
348 rdp->arg_size = strtol(token, &endptr, 0);
349 if (endptr == NULL || *endptr != '\0')
351 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
352 token);
353 exit(1);
356 token = GetToken();
357 rdp->ret_value = strtol(token, &endptr, 0);
358 if (endptr == NULL || *endptr != '\0')
360 fprintf(stderr, "%d: Expected number value, got '%s'\n", Line,
361 token);
362 exit(1);
365 return 0;
369 static int ParseStub( int ordinal )
371 ORDDEF *odp;
372 ORDFUNCDEF *fdp;
374 odp = &OrdinalDefinitions[ordinal];
375 strcpy( odp->export_name, GetToken() );
376 odp->type = TYPE_STUB;
377 fdp = malloc(sizeof(*fdp));
378 odp->additional_data = fdp;
379 fdp->arg_types[0] = '\0';
380 strcpy( fdp->internal_name, STUB_CALLBACK );
381 return 0;
385 static int ParseOrdinal(int ordinal)
387 char *token;
389 if (ordinal >= MAX_ORDINALS)
391 fprintf(stderr, "%d: Ordinal number too large\n", Line);
392 exit(1);
394 if (ordinal > Limit) Limit = ordinal;
396 token = GetToken();
397 if (token == NULL)
399 fprintf(stderr, "%d: Expected type after ordinal\n", Line);
400 exit(1);
403 if (strcmp(token, "byte") == 0)
404 return ParseVariable(ordinal, TYPE_BYTE);
405 else if (strcmp(token, "word") == 0)
406 return ParseVariable(ordinal, TYPE_WORD);
407 else if (strcmp(token, "long") == 0)
408 return ParseVariable(ordinal, TYPE_LONG);
409 else if (strcmp(token, "p") == 0)
410 return ParseExportFunction(ordinal, TYPE_PASCAL);
411 else if (strcmp(token, "pascal") == 0)
412 return ParseExportFunction(ordinal, TYPE_PASCAL);
413 else if (strcmp(token, "pascal16") == 0)
414 return ParseExportFunction(ordinal, TYPE_PASCAL_16);
415 else if (strcmp(token, "register") == 0)
416 return ParseExportFunction(ordinal, TYPE_REGISTER);
417 else if (strcmp(token, "stdcall") == 0)
418 return ParseExportFunction(ordinal, TYPE_STDCALL);
419 else if (strcmp(token, "equate") == 0)
420 return ParseEquate(ordinal);
421 else if (strcmp(token, "return") == 0)
422 return ParseReturn(ordinal);
423 else if (strcmp(token, "stub") == 0)
424 return ParseStub(ordinal);
425 else
427 fprintf(stderr,
428 "%d: Expected type after ordinal, found '%s' instead\n",
429 Line, token);
430 exit(1);
434 static int ParseTopLevel(void)
436 char *token;
438 while ((token = GetToken()) != NULL)
440 if (strcmp(token, "name") == 0)
442 strcpy(LowerDLLName, GetToken());
443 strlower(LowerDLLName);
445 strcpy(UpperDLLName, LowerDLLName);
446 strupper(UpperDLLName);
448 else if (strcmp(token, "id") == 0)
450 token = GetToken();
451 if (!IsNumberString(token))
453 fprintf(stderr, "%d: Expected number after id\n", Line);
454 exit(1);
457 DLLId = atoi(token);
459 else if (strcmp(token, "base") == 0)
461 token = GetToken();
462 if (!IsNumberString(token))
464 fprintf(stderr, "%d: Expected number after base\n", Line);
465 exit(1);
468 Base = atoi(token);
470 else if (IsNumberString(token))
472 int ordinal;
473 int rv;
475 ordinal = atoi(token);
476 if ((rv = ParseOrdinal(ordinal)) < 0)
477 return rv;
479 else
481 fprintf(stderr,
482 "%d: Expected name, id, length or ordinal\n", Line);
483 exit(1);
487 return 0;
491 static int OutputVariableCode( char *storage, ORDDEF *odp )
493 ORDVARDEF *vdp;
494 int i;
496 vdp = odp->additional_data;
497 printf( "\t.data\n" );
498 for (i = 0; i < vdp->n_values; i++)
500 if ((i & 7) == 0)
501 printf( "\t%s\t", storage);
503 printf( "%d", vdp->values[i]);
505 if ((i & 7) == 7 || i == vdp->n_values - 1) printf( "\n");
506 else printf( ", ");
508 printf( "\n");
509 printf( "\t.text\n" );
510 return vdp->n_values;
514 /*******************************************************************
515 * BuildModule
517 * Build the in-memory representation of the module, and dump it
518 * as a byte stream into the assembly code.
520 static void BuildModule( int max_code_offset, int max_data_offset )
522 ORDDEF *odp;
523 int i, size;
524 char *buffer;
525 NE_MODULE *pModule;
526 SEGTABLEENTRY *pSegment;
527 LOADEDFILEINFO *pFileInfo;
528 BYTE *pstr, *bundle;
529 WORD *pword;
531 /* Module layout:
532 * NE_MODULE Module
533 * LOADEDFILEINFO File information
534 * SEGTABLEENTRY Segment 1 (code)
535 * SEGTABLEENTRY Segment 2 (data)
536 * WORD[2] Resource table (empty)
537 * BYTE[2] Imported names (empty)
538 * BYTE[n] Resident names table
539 * BYTE[n] Entry table
542 buffer = malloc( 0x10000 );
544 pModule = (NE_MODULE *)buffer;
545 pModule->magic = NE_SIGNATURE;
546 pModule->count = 1;
547 pModule->next = 0;
548 pModule->flags = NE_FFLAGS_SINGLEDATA | NE_FFLAGS_LIBMODULE;
549 pModule->dgroup = 2;
550 pModule->heap_size = 0xffff;
551 pModule->stack_size = 0;
552 pModule->ip = 0;
553 pModule->cs = 0;
554 pModule->sp = 0;
555 pModule->ss = 0;
556 pModule->seg_count = 2;
557 pModule->modref_count = 0;
558 pModule->nrname_size = 0;
559 pModule->modref_table = 0;
560 pModule->nrname_fpos = 0;
561 pModule->moveable_entries = 0;
562 pModule->alignment = 0;
563 pModule->truetype = 0;
564 pModule->os_flags = NE_OSFLAGS_WINDOWS;
565 pModule->misc_flags = 0;
566 pModule->dlls_to_init = 0;
567 pModule->nrname_handle = 0;
568 pModule->min_swap_area = 0;
569 pModule->expected_version = 0x030a;
571 /* File information */
573 pFileInfo = (LOADEDFILEINFO *)(pModule + 1);
574 pModule->fileinfo = (int)pFileInfo - (int)pModule;
575 pFileInfo->length = sizeof(LOADEDFILEINFO) + strlen(UpperDLLName) + 3;
576 pFileInfo->fixed_media = 0;
577 pFileInfo->error = 0;
578 pFileInfo->date = 0;
579 pFileInfo->time = 0;
580 sprintf( pFileInfo->filename, "%s.DLL", UpperDLLName );
581 pstr = (char *)pFileInfo + pFileInfo->length + 1;
583 /* Segment table */
585 pSegment = (SEGTABLEENTRY *)pstr;
586 pModule->seg_table = (int)pSegment - (int)pModule;
587 pSegment->filepos = 0;
588 pSegment->size = max_code_offset;
589 pSegment->flags = 0;
590 pSegment->minsize = max_code_offset;
591 pSegment->selector = 0;
592 pSegment++;
594 pModule->dgroup_entry = (int)pSegment - (int)pModule;
595 pSegment->filepos = 0;
596 pSegment->size = max_data_offset;
597 pSegment->flags = NE_SEGFLAGS_DATA;
598 pSegment->minsize = max_data_offset;
599 pSegment->selector = 0;
600 pSegment++;
602 /* Resource table */
604 pword = (WORD *)pSegment;
605 pModule->res_table = (int)pword - (int)pModule;
606 *pword++ = 0;
607 *pword++ = 0;
609 /* Imported names table */
611 pstr = (char *)pword;
612 pModule->import_table = (int)pstr - (int)pModule;
613 *pstr++ = 0;
614 *pstr++ = 0;
616 /* Resident names table */
618 pModule->name_table = (int)pstr - (int)pModule;
619 /* First entry is module name */
620 *pstr = strlen(UpperDLLName );
621 strcpy( pstr + 1, UpperDLLName );
622 pstr += *pstr + 1;
623 *(WORD *)pstr = 0;
624 pstr += sizeof(WORD);
625 /* Store all ordinals */
626 odp = OrdinalDefinitions + 1;
627 for (i = 1; i <= Limit; i++, odp++)
629 if (!odp->export_name[0]) continue;
630 *pstr = strlen( odp->export_name );
631 strcpy( pstr + 1, odp->export_name );
632 strupper( pstr + 1 );
633 pstr += *pstr + 1;
634 *(WORD *)pstr = i;
635 pstr += sizeof(WORD);
637 *pstr++ = 0;
639 /* Entry table */
641 pModule->entry_table = (int)pstr - (int)pModule;
642 bundle = NULL;
643 odp = OrdinalDefinitions + 1;
644 for (i = 1; i <= Limit; i++, odp++)
646 int selector = 0;
648 switch (odp->type)
650 case TYPE_INVALID:
651 selector = 0; /* Invalid selector */
652 break;
654 case TYPE_PASCAL:
655 case TYPE_PASCAL_16:
656 case TYPE_REGISTER:
657 case TYPE_RETURN:
658 case TYPE_STUB:
659 selector = 1; /* Code selector */
660 break;
662 case TYPE_BYTE:
663 case TYPE_WORD:
664 case TYPE_LONG:
665 selector = 2; /* Data selector */
666 break;
668 case TYPE_ABS:
669 selector = 0xfe; /* Constant selector */
670 break;
673 /* create a new bundle if necessary */
674 if (!bundle || (bundle[0] >= 254) || (bundle[1] != selector))
676 bundle = pstr;
677 bundle[0] = 0;
678 bundle[1] = selector;
679 pstr += 2;
682 (*bundle)++;
683 if (selector != 0)
685 *pstr++ = 1;
686 *(WORD *)pstr = odp->offset;
687 pstr += sizeof(WORD);
690 *pstr++ = 0;
692 /* Dump the module content */
694 printf( "\t.data\n" );
695 printf( "\t.globl " PREFIX "%s_Module_Start\n", UpperDLLName );
696 printf( PREFIX "%s_Module_Start:\n", UpperDLLName );
697 size = (int)pstr - (int)pModule;
698 for (i = 0, pstr = buffer; i < size; i++, pstr++)
700 if (!(i & 7)) printf( "\t.byte " );
701 printf( "%d%c", *pstr, ((i & 7) != 7) ? ',' : '\n' );
703 if (i & 7) printf( "0\n" );
704 printf( "\t.globl " PREFIX "%s_Module_End\n", UpperDLLName );
705 printf( PREFIX "%s_Module_End:\n", UpperDLLName );
709 static void BuildSpec32Files( char *specname )
711 ORDDEF *odp;
712 ORDFUNCDEF *fdp;
713 ORDRETDEF *rdp;
714 int i;
716 SpecFp = fopen( specname, "r");
717 if (SpecFp == NULL)
719 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
720 exit(1);
723 ParseTopLevel();
725 printf( "/* File generated automatically, do not edit! */\n" );
726 printf( "#include <sys/types.h>\n");
727 printf( "#include \"dlls.h\"\n");
728 printf( "#include \"pe_image.h\"\n");
729 printf( "#include \"winerror.h\"\n");
730 printf( "#include \"stddebug.h\"\n");
731 printf( "#include \"debug.h\"\n");
732 printf( "\nextern int RELAY32_Unimplemented();\n\n" );
734 odp = OrdinalDefinitions;
735 for (i = 0; i <= Limit; i++, odp++)
737 int argno,argc;
738 fdp = odp->additional_data;
739 rdp = odp->additional_data;
741 switch (odp->type)
743 case TYPE_INVALID:
744 case TYPE_STUB:
745 printf( "int %s_%d()\n{\n\t", UpperDLLName, i);
746 printf( "RELAY32_Unimplemented(\"%s\",%d);\n", UpperDLLName, i);
747 printf( "\t/*NOTREACHED*/\n\treturn 0;\n}\n\n");
748 break;
749 case TYPE_STDCALL:
750 argc=strlen(fdp->arg_types);
751 printf( "void %s_%d(", UpperDLLName, i);
752 for(argno=0;argno<argc;argno++)
754 switch(fdp->arg_types[argno])
756 case 'p': printf( "void *");break;
757 case 'l': printf( "int ");break;
758 default:
759 fprintf(stderr, "Not supported argument type %c\n",
760 fdp->arg_types[argno]);
761 exit(1);
763 putchar( 'a'+argno );
764 if (argno!=argc-1) putchar( ',' );
766 printf( ")\n{\n" );
767 printf( "\textern int %s();\n", fdp->internal_name );
768 printf( "\tdprintf_relay(stddeb,\"Entering %%s.%%s(");
769 for (argno=0;argno<argc;argno++) printf( "%%x ");
770 printf( ")\\n\", \"%s\", \"%s\"", UpperDLLName, odp->export_name);
771 for(argno=0;argno<argc;argno++) printf( ",%c", 'a'+argno);
772 printf( ");\n\t%s(", fdp->internal_name );
773 for(argno=0;argno<argc;argno++)
775 putchar('a'+argno);
776 if (argno!=argc-1) putchar(',');
778 printf( ");\n\t__asm__ __volatile__(\"movl %%ebp,%%esp;"
779 "popl %%ebp;ret $%d\");\n}\n\n",
780 4*argc);
781 break;
782 case TYPE_RETURN:
783 printf( "void %s_%d()\n{\n\t", UpperDLLName, i);
784 printf( "RELAY32_DebugEnter(\"%s\",\"%s\");\n\t",
785 UpperDLLName, odp->export_name);
786 printf( "WIN32_LastError=ERROR_CALL_NOT_IMPLEMENTED;\n");
787 printf( "\t__asm__ __volatile__ (\"movl %d,%%eax\");\n",
788 rdp->ret_value);
789 printf( "\t__asm__ __volatile__ (\"movl %%ebp,%%esp;popl %%ebp;"
790 "ret $%d\");\n}\n\n", rdp->arg_size);
791 break;
792 default:
793 fprintf(stderr,"build: function type %d not available for Win32\n",
794 odp->type);
795 break;
799 printf( "static WIN32_function functions[%d+1]={\n", Limit);
801 odp = OrdinalDefinitions;
802 for (i = 0; i <= Limit; i++, odp++)
804 fdp = odp->additional_data;
805 rdp = odp->additional_data;
807 switch (odp->type)
809 case TYPE_INVALID:
810 printf( "{0,%s_%d},\n",UpperDLLName, i);
811 break;
812 case TYPE_RETURN:
813 case TYPE_STDCALL:
814 case TYPE_STUB:
815 printf( "{\"%s\",%s_%d},\n", odp->export_name, UpperDLLName, i);
816 break;
817 default:
818 fprintf(stderr, "build: implementation error: missing %d\n",
819 odp->type);
820 exit(1);
823 printf("};\n\n");
825 printf( "static WIN32_builtin dll={\"%s\",functions,%d,0};\n",
826 UpperDLLName, Limit);
828 printf( "void %s_Init(void)\n{\n",UpperDLLName);
829 printf( "\tdll.next=WIN32_builtin_list;\n");
830 printf( "\tWIN32_builtin_list=&dll;\n}");
834 static void BuildSpec16Files( char *specname )
836 ORDDEF *odp;
837 ORDFUNCDEF *fdp;
838 ORDRETDEF *rdp;
839 int i;
840 int code_offset, data_offset;
842 SpecFp = fopen( specname, "r");
843 if (SpecFp == NULL)
845 fprintf(stderr, "Could not open specification file, '%s'\n", specname);
846 exit(1);
849 ParseTopLevel();
851 printf( "/* File generated automatically; do not edit! */\n" );
852 printf( "\t.data\n" );
853 printf( "\t.globl " PREFIX "%s_Data_Start\n", UpperDLLName );
854 printf( PREFIX "%s_Data_Start:\n", UpperDLLName );
855 printf( "\t.word 0,0,0,0,0,0,0,0\n" );
856 data_offset = 16;
857 printf( "\t.text\n" );
858 printf( "\t.globl " PREFIX "%s_Code_Start\n", UpperDLLName );
859 printf( PREFIX "%s_Code_Start:\n", UpperDLLName );
860 code_offset = 0;
862 odp = OrdinalDefinitions;
863 for (i = 0; i <= Limit; i++, odp++)
865 fdp = odp->additional_data;
866 rdp = odp->additional_data;
868 switch (odp->type)
870 case TYPE_INVALID:
871 odp->offset = 0xffff;
872 break;
874 case TYPE_ABS:
875 odp->offset = (int)odp->additional_data & 0xffff;
876 break;
878 case TYPE_BYTE:
879 printf( "/* %s.%d */\n", UpperDLLName, i);
880 odp->offset = data_offset;
881 data_offset += OutputVariableCode( ".byte", odp);
882 break;
884 case TYPE_WORD:
885 printf( "/* %s.%d */\n", UpperDLLName, i);
886 odp->offset = data_offset;
887 data_offset += 2 * OutputVariableCode( ".word", odp);
888 break;
890 case TYPE_LONG:
891 printf( "/* %s.%d */\n", UpperDLLName, i);
892 odp->offset = data_offset;
893 data_offset += 4 * OutputVariableCode( ".long", odp);
894 break;
896 case TYPE_RETURN:
897 printf( "/* %s.%d */\n", UpperDLLName, i);
898 printf( "\tmovw $%d,%%ax\n", rdp->ret_value & 0xffff );
899 printf( "\tmovw $%d,%%dx\n", (rdp->ret_value >> 16) & 0xffff);
900 printf( "\t.byte 0x66\n");
901 if (rdp->arg_size != 0)
902 printf( "\tlret $%d\n", rdp->arg_size);
903 else
904 printf( "\tlret\n");
905 odp->offset = code_offset;
906 code_offset += 10; /* Assembly code is 10 bytes long */
907 if (rdp->arg_size != 0) code_offset += 2;
908 break;
910 case TYPE_REGISTER:
911 case TYPE_PASCAL:
912 case TYPE_PASCAL_16:
913 case TYPE_STUB:
914 printf( "/* %s.%d */\n", UpperDLLName, i);
915 printf( "\tpushw %%bp\n" );
916 printf( "\tpushl $0x%08x\n", (DLLId << 16) | i);
917 printf( "\tpushl $" PREFIX "%s\n", fdp->internal_name );
918 printf( "\tljmp $0x%04x, $" PREFIX "CallTo32_%s_%s\n\n",
919 WINE_CODE_SELECTOR,
920 (odp->type == TYPE_REGISTER) ? "regs" :
921 (odp->type == TYPE_PASCAL) ? "long" : "word",
922 fdp->arg_types );
923 printf( "\tnop\n" );
924 printf( "\tnop\n" );
925 printf( "\tnop\n" );
926 printf( "\tnop\n" );
927 printf( "\tnop\n" );
928 odp->offset = code_offset;
929 code_offset += 24; /* Assembly code is 24 bytes long */
930 break;
932 default:
933 fprintf( stderr, "build: Unknown function type; please report.\n");
934 break;
938 if (!code_offset) /* Make sure the code segment is not empty */
940 printf( "\t.byte 0\n" );
941 code_offset++;
944 BuildModule( code_offset, data_offset );
948 /*******************************************************************
949 * TransferArgs16To32
951 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
952 * The 16-bit stack layout is:
953 * ... ...
954 * (bp+8) arg2
955 * (bp+6) arg1
956 * (bp+4) cs
957 * (bp+2) ip
958 * (bp) bp
960 static int TransferArgs16To32( char *args )
962 int i, pos16, pos32;
964 /* Save ebx first */
966 printf( "\tpushl %%ebx\n" );
968 /* Get the 32-bit stack pointer */
970 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
972 /* Copy the arguments */
974 pos16 = 6; /* skip bp and return address */
975 pos32 = 0;
977 for (i = strlen(args); i > 0; i--)
979 pos32 -= 4;
980 switch(args[i-1])
982 case 'w': /* word */
983 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
984 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
985 pos16 += 2;
986 break;
988 case 's': /* s_word */
989 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16 );
990 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
991 pos16 += 2;
992 break;
994 case 'l': /* long */
995 printf( "\tmovl %d(%%ebp),%%eax\n", pos16 );
996 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
997 pos16 += 4;
998 break;
1000 case 'p': /* ptr */
1001 /* Get the selector */
1002 printf( "\tmovw %d(%%ebp),%%ax\n", pos16 + 2 );
1003 /* Get the selector base */
1004 printf( "\tandl $0xfff8,%%eax\n" );
1005 printf( "\tmovl " PREFIX "ldt_copy(%%eax),%%eax\n" );
1006 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32 );
1007 /* Add the offset */
1008 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16 );
1009 printf( "\taddl %%eax,%d(%%ebx)\n", pos32 );
1010 pos16 += 4;
1011 break;
1013 default:
1014 fprintf( stderr, "Unknown arg type '%c'\n", args[i-1] );
1018 /* Restore ebx */
1020 printf( "\tpopl %%ebx\n" );
1022 return pos16 - 6; /* Return the size of the 16-bit args */
1026 /*******************************************************************
1027 * BuildContext
1029 * Build the context structure on the 32-bit stack.
1030 * The only valid registers in the context structure are:
1031 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
1033 static void BuildContext(void)
1035 /* Save ebx first */
1037 printf( "\tpushl %%ebx\n" );
1039 /* Get the 32-bit stack pointer */
1041 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1043 /* Store the registers */
1045 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx) ); /* Get ebx from stack */
1046 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax) );
1047 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx) );
1048 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx) );
1049 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi) );
1050 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi) );
1051 printf( "\tpushw %%es\n" );
1052 printf( "\tpopw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1053 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
1054 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds) );
1055 printf( "\tpushfl\n" );
1056 #ifndef __FreeBSD__
1057 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1058 #else
1059 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1060 #endif
1064 /*******************************************************************
1065 * RestoreContext
1067 * Restore the registers from the context structure
1069 static void RestoreContext(void)
1071 /* Get the 32-bit stack pointer */
1073 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%ebx\n" );
1075 /* Restore the registers */
1077 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx) );
1078 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx) );
1079 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi) );
1080 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi) );
1081 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_es) );
1082 printf( "\tpopw %%es\n" );
1083 printf( "\tpopw %%ax\n" ); /* Remove old ds from the stack */
1084 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds) ); /* Push new ds */
1085 #ifndef __FreeBSD__
1086 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags) );
1087 #else
1088 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl) );
1089 #endif
1090 printf( "\tpopfl\n" );
1091 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax) );
1092 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx) );
1096 /*******************************************************************
1097 * BuildCall32Func
1099 * Build a 32-bit callback function. The syntax of the function
1100 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
1101 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
1102 * 'l'=long, 'p'=pointer).
1104 * Stack layout upon entry to the callback function:
1105 * ... ...
1106 * (sp+14) first 16-bit arg
1107 * (sp+12) cs (word)
1108 * (sp+10) ip (word)
1109 * (sp+8) bp (word)
1110 * (sp+4) dll_id+ordinal (long)
1111 * (sp) entrypoint (long)
1114 static void BuildCall32Func( char *profile )
1116 int argsize = 0;
1117 int short_ret = 0;
1118 int reg_func = 0;
1119 char *args = profile + 5;
1121 /* Parse function type */
1123 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1124 else if (!strncmp( "regs_", profile, 5 )) reg_func = 1;
1125 else if (strncmp( "long_", profile, 5 ))
1127 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1128 return;
1131 /* Function header */
1133 printf( "/**********\n" );
1134 printf( " * " PREFIX "CallTo32_%s\n", profile );
1135 printf( " **********/\n" );
1136 printf( "\t.align 4\n" );
1137 printf( "\t.globl " PREFIX "CallTo32_%s\n\n", profile );
1138 printf( PREFIX "CallTo32_%s:\n", profile );
1140 /* Setup bp to point to its copy on the stack */
1142 printf( "\tmovzwl %%sp,%%ebp\n" );
1143 printf( "\taddw $8,%%bp\n" );
1145 /* Save 16-bit ds */
1147 printf( "\tpushw %%ds\n" );
1149 /* Restore 32-bit ds */
1151 printf( "\tpushw $0x%04x\n", WINE_DATA_SELECTOR );
1152 printf( "\tpopw %%ds\n" );
1154 /* Save the 16-bit stack */
1156 printf( "\tpushw " PREFIX "IF1632_Saved16_sp\n" );
1157 printf( "\tpushw " PREFIX "IF1632_Saved16_ss\n" );
1158 printf( "\tmovw %%ss," PREFIX "IF1632_Saved16_ss\n" );
1159 printf( "\tmovw %%sp," PREFIX "IF1632_Saved16_sp\n" );
1161 /* Transfer the arguments */
1163 if (reg_func) BuildContext();
1164 else if (*args) argsize = TransferArgs16To32( args );
1166 /* Get the address of the API function */
1168 printf( "\tmovl -8(%%ebp),%%eax\n" );
1170 /* Setup es */
1172 printf( "\tpushw %%ds\n" );
1173 printf( "\tpopw %%es\n" );
1175 /* Switch to the 32-bit stack */
1177 printf( "\tpushw %%ds\n" );
1178 printf( "\tpopw %%ss\n" );
1179 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1181 /* Setup %ebp to point to the previous stack frame (built by CallTo16) */
1183 printf( "\tmovl %%esp,%%ebp\n" );
1184 printf( "\taddl $24,%%ebp\n" );
1186 if (reg_func)
1187 printf( "\tsubl $%d,%%esp\n", sizeof(struct sigcontext_struct) );
1188 else if (*args)
1189 printf( "\tsubl $%d,%%esp\n", 4 * strlen(args) );
1191 /* Call the entry point */
1193 if (debugging)
1195 printf( "\tpushl %%eax\n" );
1196 printf( "\tpushl $CALL32_Str_%s\n", profile );
1197 printf( "\tcall " PREFIX "RELAY_DebugCall32\n" );
1198 printf( "\tpopl %%eax\n" );
1199 printf( "\tpopl %%eax\n" );
1202 printf( "\tcall %%eax\n" );
1204 if (debugging)
1206 printf( "\tpushl %%eax\n" );
1207 printf( "\tpushl $%d\n", short_ret );
1208 printf( "\tcall " PREFIX "RELAY_DebugReturn\n" );
1209 printf( "\tpopl %%eax\n" );
1210 printf( "\tpopl %%eax\n" );
1213 if (reg_func)
1214 printf( "\taddl $%d,%%esp\n", sizeof(struct sigcontext_struct) );
1215 else if (*args)
1216 printf( "\taddl $%d,%%esp\n", 4 * strlen(args) );
1218 /* Restore the 16-bit stack */
1220 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1221 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1222 printf( "\tpopw " PREFIX "IF1632_Saved16_ss\n" );
1223 printf( "\tpopw " PREFIX "IF1632_Saved16_sp\n" );
1225 if (reg_func)
1227 /* Restore registers from the context structure */
1228 RestoreContext();
1230 else /* Store the return value in dx:ax if needed */
1232 if (!short_ret)
1234 printf( "\tpushl %%eax\n" );
1235 printf( "\tpopw %%dx\n" );
1236 printf( "\tpopw %%dx\n" );
1240 /* Restore ds and bp */
1242 printf( "\tpopw %%ds\n" );
1243 printf( "\tpopl %%ebp\n" ); /* Remove entry point address */
1244 printf( "\tpopl %%ebp\n" ); /* Remove DLL id and ordinal */
1245 printf( "\tpopw %%bp\n" );
1247 /* Remove the arguments and return */
1249 if (argsize)
1251 printf( "\t.byte 0x66\n" );
1252 printf( "\tlret $%d\n", argsize );
1254 else
1256 printf( "\t.byte 0x66\n" );
1257 printf( "\tlret\n" );
1262 /*******************************************************************
1263 * BuildCall16Func
1265 * Build a 16-bit callback function.
1267 * Stack frame of the callback function:
1268 * ... ...
1269 * (ebp+24) arg2
1270 * (ebp+20) arg1
1271 * (ebp+16) 16-bit ds
1272 * (ebp+12) func to call
1273 * (ebp+8) code selector
1274 * (ebp+4) return address
1275 * (ebp) previous ebp
1277 * Prototypes for the CallTo16 functions:
1278 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1279 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1280 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD bp,
1281 * WORD ax, WORD bx, WORD cx, WORD dx,
1282 * WORD si, WORD di );
1284 static void BuildCall16Func( char *profile )
1286 int short_ret = 0;
1287 int reg_func = 0;
1288 char *args = profile + 5;
1290 if (!strncmp( "word_", profile, 5 )) short_ret = 1;
1291 else if (!strncmp( "regs_", profile, 5 )) reg_func = short_ret = 1;
1292 else if (strncmp( "long_", profile, 5 ))
1294 fprintf( stderr, "Invalid function name '%s', ignored\n", profile );
1295 return;
1298 /* Function header */
1300 printf( "/**********\n" );
1301 printf( " * " PREFIX "CallTo16_%s\n", profile );
1302 printf( " **********/\n" );
1303 printf( "\t.align 4\n" );
1304 printf( "\t.globl " PREFIX "CallTo16_%s\n\n", profile );
1305 printf( PREFIX "CallTo16_%s:\n", profile );
1307 /* Push code selector before return address to simulate a lcall */
1309 printf( "\tpopl %%eax\n" );
1310 printf( "\tpushl $0x%04x\n", WINE_CODE_SELECTOR );
1311 printf( "\tpushl %%eax\n" );
1313 /* Entry code */
1315 printf( "\tpushl %%ebp\n" );
1316 printf( "\tmovl %%esp,%%ebp\n" );
1318 /* Save the 32-bit registers */
1320 printf( "\tpushl %%ebx\n" );
1321 printf( "\tpushl %%ecx\n" );
1322 printf( "\tpushl %%edx\n" );
1323 printf( "\tpushl %%esi\n" );
1324 printf( "\tpushl %%edi\n" );
1326 /* Save the 32-bit stack */
1328 printf( "\tpushl " PREFIX "IF1632_Saved32_esp\n" );
1329 printf( "\tmovl %%esp," PREFIX "IF1632_Saved32_esp\n" );
1330 printf( "\tmovl %%ebp,%%ebx\n" );
1332 /* Print debugging info */
1334 if (debugging)
1336 /* Push the address of the first argument */
1337 printf( "\tmovl %%ebx,%%eax\n" );
1338 printf( "\taddl $12,%%eax\n" );
1339 printf( "\tpushl $%d\n", reg_func ? 7 : strlen(args) );
1340 printf( "\tpushl %%eax\n" );
1341 printf( "\tcall " PREFIX "RELAY_DebugCall16\n" );
1342 printf( "\tpopl %%eax\n" );
1343 printf( "\tpopl %%eax\n" );
1346 /* Switch to the 16-bit stack */
1348 printf( "\tmovw " PREFIX "IF1632_Saved16_ss,%%ss\n" );
1349 printf( "\tmovw " PREFIX "IF1632_Saved16_sp,%%sp\n" );
1351 /* Transfer the arguments */
1353 if (reg_func)
1355 /* Get the registers. ebx is handled later on. */
1356 printf( "\tpushw 20(%%ebx)\n" );
1357 printf( "\tpopw %%es\n" );
1358 printf( "\tmovl 24(%%ebx),%%ebp\n" );
1359 printf( "\tmovl 28(%%ebx),%%eax\n" );
1360 printf( "\tmovl 36(%%ebx),%%ecx\n" );
1361 printf( "\tmovl 40(%%ebx),%%edx\n" );
1362 printf( "\tmovl 44(%%ebx),%%esi\n" );
1363 printf( "\tmovl 48(%%ebx),%%edi\n" );
1365 else /* not a register function */
1367 int pos = 20; /* first argument position */
1369 /* Make %bp point to the previous stackframe (built by CallTo32) */
1370 printf( "\tmovw %%sp,%%bp\n" );
1371 printf( "\taddw $16,%%bp\n" );
1373 while (*args)
1375 switch(*args++)
1377 case 'w': /* word */
1378 printf( "\tpushw %d(%%ebx)\n", pos );
1379 break;
1380 case 'l': /* long */
1381 printf( "\tpushl %d(%%ebx)\n", pos );
1382 break;
1384 pos += 4;
1388 /* Push the return address */
1390 printf( "\tpushl " PREFIX "CALL16_RetAddr_%s\n",
1391 short_ret ? "word" : "long" );
1393 /* Push the called routine address */
1395 printf( "\tpushl 12(%%ebx)\n" );
1397 /* Get the 16-bit ds */
1398 /* FIXME: this shouldn't be necessary if function prologs fixup worked. */
1400 printf( "\tpushw 16(%%ebx)\n" );
1401 printf( "\tpopw %%ds\n" );
1403 if (reg_func)
1405 /* Retrieve ebx from the 32-bit stack */
1406 printf( "\tmovl %%fs:28(%%ebx),%%ebx\n" );
1408 else
1410 /* Set ax equal to ds for window procedures */
1411 printf( "\tmovw %%ds,%%ax\n" );
1414 /* Jump to the called routine */
1416 printf( "\t.byte 0x66\n" );
1417 printf( "\tlret\n" );
1421 /*******************************************************************
1422 * BuildRet16Func
1424 * Build the return code for 16-bit callbacks
1426 static void BuildRet16Func()
1428 printf( "\t.globl " PREFIX "CALL16_Ret_word\n" );
1429 printf( "\t.globl " PREFIX "CALL16_Ret_long\n" );
1431 /* Put return value into eax */
1433 printf( PREFIX "CALL16_Ret_long:\n" );
1434 printf( "\tpushw %%dx\n" );
1435 printf( "\tpushw %%ax\n" );
1436 printf( "\tpopl %%eax\n" );
1437 printf( PREFIX "CALL16_Ret_word:\n" );
1439 /* Restore 32-bit segment registers */
1441 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR );
1442 printf( "\tmovw %%bx,%%ds\n" );
1443 printf( "\tmovw %%bx,%%es\n" );
1444 printf( "\tmovw %%bx,%%ss\n" );
1446 /* Restore the 32-bit stack */
1448 printf( "\tmovl " PREFIX "IF1632_Saved32_esp,%%esp\n" );
1449 printf( "\tpopl " PREFIX "IF1632_Saved32_esp\n" );
1451 /* Restore the 32-bit registers */
1453 printf( "\tpopl %%edi\n" );
1454 printf( "\tpopl %%esi\n" );
1455 printf( "\tpopl %%edx\n" );
1456 printf( "\tpopl %%ecx\n" );
1457 printf( "\tpopl %%ebx\n" );
1459 /* Return to caller */
1461 printf( "\tpopl %%ebp\n" );
1462 printf( "\tlret\n" );
1464 /* Declare the return address variables */
1466 printf( "\t.data\n" );
1467 printf( "\t.globl " PREFIX "CALL16_RetAddr_word\n" );
1468 printf( "\t.globl " PREFIX "CALL16_RetAddr_long\n" );
1469 printf( PREFIX "CALL16_RetAddr_word:\t.long 0\n" );
1470 printf( PREFIX "CALL16_RetAddr_long:\t.long 0\n" );
1471 printf( "\t.text\n" );
1475 static void usage(void)
1477 fprintf(stderr, "usage: build -spec SPECNAMES\n"
1478 " build -call32 FUNCTION_PROFILES\n"
1479 " build -call16 FUNCTION_PROFILES\n" );
1480 exit(1);
1484 int main(int argc, char **argv)
1486 int i;
1488 if (argc <= 2) usage();
1490 if (!strcmp( argv[1], "-spec16" ))
1492 for (i = 2; i < argc; i++) BuildSpec16Files( argv[i] );
1494 else if (!strcmp( argv[1], "-spec32" ))
1496 for (i = 2; i < argc; i++) BuildSpec32Files( argv[i] );
1498 else if (!strcmp( argv[1], "-call32" )) /* 32-bit callbacks */
1500 /* File header */
1502 printf( "/* File generated automatically. Do no edit! */\n\n" );
1503 printf( "\t.text\n" );
1505 /* Build the callback functions */
1507 for (i = 2; i < argc; i++) BuildCall32Func( argv[i] );
1509 /* Output the argument debugging strings */
1511 if (debugging)
1513 printf( "/* Argument strings */\n" );
1514 for (i = 2; i < argc; i++)
1516 printf( "CALL32_Str_%s:\n", argv[i] );
1517 printf( "\t.ascii \"%s\\0\"\n", argv[i] + 5 );
1521 else if (!strcmp( argv[1], "-call16" )) /* 16-bit callbacks */
1523 /* File header */
1525 printf( "/* File generated automatically. Do no edit! */\n\n" );
1526 printf( "\t.text\n" );
1527 printf( "\t.globl " PREFIX "CALL16_Start\n" );
1528 printf( PREFIX "CALL16_Start:\n" );
1530 /* Build the callback functions */
1532 for (i = 2; i < argc; i++) BuildCall16Func( argv[i] );
1534 /* Output the 16-bit return code */
1536 BuildRet16Func();
1538 printf( "\t.globl " PREFIX "CALL16_End\n" );
1539 printf( PREFIX "CALL16_End:\n" );
1541 else usage();
1543 return 0;