2 * Copyright 1993 Robert J. Amstadt
3 * Copyright 1995 Alexandre Julliard
12 /* ELF symbols do not have an underscore in front */
19 #define TYPE_INVALID 0
23 #define TYPE_PASCAL_16 4
25 #define TYPE_REGISTER 6
29 #define MAX_ORDINALS 1299
31 typedef struct ordinal_definition_s
36 void *additional_data
;
39 typedef struct ordinal_variable_definition_s
45 typedef struct ordinal_function_definition_s
49 char internal_name
[80];
52 typedef struct ordinal_return_definition_s
58 static ORDDEF OrdinalDefinitions
[MAX_ORDINALS
];
60 char LowerDLLName
[80];
61 char UpperDLLName
[80];
66 char *ParseBuffer
= NULL
;
71 static int debugging
= 1;
73 /* Offset of register relative to the end of the context struct */
74 #define CONTEXTOFFSET(reg) \
75 ((int)&(((struct sigcontext_struct *)1)->reg) - 1 \
76 - sizeof(struct sigcontext_struct))
78 static int IsNumberString(char *s
)
87 static char *strlower(char *s
)
91 for(p
= s
; *p
!= '\0'; p
++)
97 static char *strupper(char *s
)
101 for(p
= s
; *p
!= '\0'; p
++)
107 static char * GetTokenInLine(void)
112 if (ParseNext
!= ParseBuffer
)
114 if (ParseSaveChar
== '\0')
116 *ParseNext
= ParseSaveChar
;
120 * Remove initial white space.
122 for (p
= ParseNext
; isspace(*p
); p
++)
132 if (*token
!= '(' && *token
!= ')')
133 while (*p
!= '\0' && *p
!= '(' && *p
!= ')' && !isspace(*p
))
143 static char * GetToken(void)
147 if (ParseBuffer
== NULL
)
149 ParseBuffer
= malloc(512);
150 ParseNext
= ParseBuffer
;
154 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
156 if (ParseBuffer
[0] != '#')
161 while ((token
= GetTokenInLine()) == NULL
)
163 ParseNext
= ParseBuffer
;
167 if (fgets(ParseBuffer
, 511, SpecFp
) == NULL
)
169 if (ParseBuffer
[0] != '#')
177 static int ParseVariable(int ordinal
, int type
)
181 char export_name
[80];
186 int value_array_size
;
188 strcpy(export_name
, GetToken());
193 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
198 value_array_size
= 25;
199 value_array
= malloc(sizeof(*value_array
) * value_array_size
);
201 while ((token
= GetToken()) != NULL
)
206 value_array
[n_values
++] = strtol(token
, &endptr
, 0);
207 if (n_values
== value_array_size
)
209 value_array_size
+= 25;
210 value_array
= realloc(value_array
,
211 sizeof(*value_array
) * value_array_size
);
214 if (endptr
== NULL
|| *endptr
!= '\0')
216 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
224 fprintf(stderr
, "%d: End of file in variable declaration\n", Line
);
228 if (ordinal
>= MAX_ORDINALS
)
230 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
234 odp
= &OrdinalDefinitions
[ordinal
];
236 strcpy(odp
->export_name
, export_name
);
238 vdp
= malloc(sizeof(*vdp
));
239 odp
->additional_data
= vdp
;
241 vdp
->n_values
= n_values
;
242 vdp
->values
= realloc(value_array
, sizeof(*value_array
) * n_values
);
247 static int ParseExportFunction(int ordinal
, int type
)
254 if (ordinal
>= MAX_ORDINALS
)
256 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
260 odp
= &OrdinalDefinitions
[ordinal
];
261 strcpy(odp
->export_name
, GetToken());
263 fdp
= malloc(sizeof(*fdp
));
264 odp
->additional_data
= fdp
;
269 fprintf(stderr
, "%d: Expected '(' got '%s'\n", Line
, token
);
273 for (i
= 0; i
< 16; i
++)
279 if (!strcmp(token
, "byte") || !strcmp(token
, "word"))
280 fdp
->arg_types
[i
] = 'w';
281 else if (!strcmp(token
, "s_byte") || !strcmp(token
, "s_word"))
282 fdp
->arg_types
[i
] = 's';
283 else if (!strcmp(token
, "long") || !strcmp(token
, "segptr"))
284 fdp
->arg_types
[i
] = 'l';
285 else if (!strcmp(token
, "ptr"))
286 fdp
->arg_types
[i
] = 'p';
289 fprintf(stderr
, "%d: Unknown variable type '%s'\n", Line
, token
);
293 fdp
->arg_types
[i
] = '\0';
295 if ((type
== TYPE_REGISTER
) && (i
> 0))
297 fprintf( stderr
, "%d: Register function can't have arguments\n", Line
);
301 strcpy(fdp
->internal_name
, GetToken());
305 static int ParseEquate(int ordinal
)
312 if (ordinal
>= MAX_ORDINALS
)
314 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
318 odp
= &OrdinalDefinitions
[ordinal
];
319 strcpy(odp
->export_name
, GetToken());
322 value
= strtol(token
, &endptr
, 0);
323 if (endptr
== NULL
|| *endptr
!= '\0')
325 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
330 odp
->type
= TYPE_ABS
;
331 odp
->additional_data
= (void *) value
;
336 static int ParseReturn(int ordinal
)
343 if (ordinal
>= MAX_ORDINALS
)
345 fprintf(stderr
, "%d: Ordinal number too large\n", Line
);
349 rdp
= malloc(sizeof(*rdp
));
351 odp
= &OrdinalDefinitions
[ordinal
];
352 strcpy(odp
->export_name
, GetToken());
353 odp
->type
= TYPE_RETURN
;
354 odp
->additional_data
= rdp
;
357 rdp
->arg_size
= strtol(token
, &endptr
, 0);
358 if (endptr
== NULL
|| *endptr
!= '\0')
360 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
366 rdp
->ret_value
= strtol(token
, &endptr
, 0);
367 if (endptr
== NULL
|| *endptr
!= '\0')
369 fprintf(stderr
, "%d: Expected number value, got '%s'\n", Line
,
377 static int ParseOrdinal(int ordinal
)
384 fprintf(stderr
, "%d: Expected type after ordinal\n", Line
);
388 if (strcmp(token
, "byte") == 0)
389 return ParseVariable(ordinal
, TYPE_BYTE
);
390 else if (strcmp(token
, "word") == 0)
391 return ParseVariable(ordinal
, TYPE_WORD
);
392 else if (strcmp(token
, "long") == 0)
393 return ParseVariable(ordinal
, TYPE_LONG
);
394 else if (strcmp(token
, "p") == 0)
395 return ParseExportFunction(ordinal
, TYPE_PASCAL
);
396 else if (strcmp(token
, "pascal") == 0)
397 return ParseExportFunction(ordinal
, TYPE_PASCAL
);
398 else if (strcmp(token
, "pascal16") == 0)
399 return ParseExportFunction(ordinal
, TYPE_PASCAL_16
);
400 else if (strcmp(token
, "register") == 0)
401 return ParseExportFunction(ordinal
, TYPE_REGISTER
);
402 else if (strcmp(token
, "equate") == 0)
403 return ParseEquate(ordinal
);
404 else if (strcmp(token
, "return") == 0)
405 return ParseReturn(ordinal
);
409 "%d: Expected type after ordinal, found '%s' instead\n",
415 static int ParseTopLevel(void)
419 while ((token
= GetToken()) != NULL
)
421 if (strcmp(token
, "name") == 0)
423 strcpy(LowerDLLName
, GetToken());
424 strlower(LowerDLLName
);
426 strcpy(UpperDLLName
, LowerDLLName
);
427 strupper(UpperDLLName
);
429 else if (strcmp(token
, "id") == 0)
432 if (!IsNumberString(token
))
434 fprintf(stderr
, "%d: Expected number after id\n", Line
);
440 else if (strcmp(token
, "length") == 0)
443 if (!IsNumberString(token
))
445 fprintf(stderr
, "%d: Expected number after length\n", Line
);
451 else if (IsNumberString(token
))
456 ordinal
= atoi(token
);
457 if ((rv
= ParseOrdinal(ordinal
)) < 0)
463 "%d: Expected name, id, length or ordinal\n", Line
);
472 static int OutputVariableCode(FILE *fp
, char *storage
, ORDDEF
*odp
)
477 vdp
= odp
->additional_data
;
478 fprintf( fp
, "\t.data\n" );
479 for (i
= 0; i
< vdp
->n_values
; i
++)
482 fprintf(fp
, "\t%s\t", storage
);
484 fprintf(fp
, "%d", vdp
->values
[i
]);
486 if ((i
& 7) == 7 || i
== vdp
->n_values
- 1)
492 fprintf( fp
, "\t.text\n" );
493 return vdp
->n_values
;
496 static void BuildSpecFiles( char *specname
)
504 int code_offset
, data_offset
;
506 SpecFp
= fopen( specname
, "r");
509 fprintf(stderr
, "Could not open specification file, '%s'\n", specname
);
515 sprintf(filename
, "dll_%s.S", LowerDLLName
);
516 fp
= fopen(filename
, "w");
517 fprintf( fp
, "/* File generated automatically; do not edit! */\n" );
518 fprintf( fp
, "\t.data\n" );
519 fprintf( fp
, "\t.globl " PREFIX
"%s_Data_Start\n", UpperDLLName
);
520 fprintf( fp
, PREFIX
"%s_Data_Start:\n", UpperDLLName
);
521 fprintf( fp
, "\t.text\n" );
522 fprintf( fp
, "\t.globl " PREFIX
"%s_Code_Start\n", UpperDLLName
);
523 fprintf( fp
, PREFIX
"%s_Code_Start:\n", UpperDLLName
);
524 code_offset
= data_offset
= 0;
526 odp
= OrdinalDefinitions
;
527 for (i
= 0; i
<= Limit
; i
++, odp
++)
529 fdp
= odp
->additional_data
;
530 rdp
= odp
->additional_data
;
535 fprintf( fp
, "/* %s.%d */\n", UpperDLLName
, i
);
536 fprintf( fp
, "\tpushw %%bp\n" );
537 fprintf( fp
, "\tpushl $0x%08x\n", (DLLId
<< 16) | i
);
538 fprintf( fp
, "\tpushl $" PREFIX
"RELAY_Unimplemented\n" );
539 fprintf( fp
, "\tljmp $0x%04x, $" PREFIX
"CallTo32_word_\n",
540 WINE_CODE_SELECTOR
);
541 odp
->offset
= code_offset
;
542 code_offset
+= 19; /* Assembly code is 19 bytes long */
546 odp
->offset
= (int)odp
->additional_data
& 0xffff;
550 fprintf( fp
, "/* %s.%d */\n", UpperDLLName
, i
);
551 odp
->offset
= data_offset
;
552 data_offset
+= OutputVariableCode(fp
, ".byte", odp
);
556 fprintf( fp
, "/* %s.%d */\n", UpperDLLName
, i
);
557 odp
->offset
= data_offset
;
558 data_offset
+= 2 * OutputVariableCode(fp
, ".word", odp
);
562 fprintf( fp
, "/* %s.%d */\n", UpperDLLName
, i
);
563 odp
->offset
= data_offset
;
564 data_offset
+= 4 * OutputVariableCode(fp
, ".long", odp
);
568 fprintf( fp
, "/* %s.%d */\n", UpperDLLName
, i
);
569 fprintf( fp
, "\tmovw $%d,%%ax\n", rdp
->ret_value
& 0xffff );
570 fprintf( fp
, "\tmovw $%d,%%dx\n", (rdp
->ret_value
>> 16) & 0xffff);
571 fprintf(fp
, "\t.byte 0x66\n");
572 if (rdp
->arg_size
!= 0)
573 fprintf(fp
, "\tlret $%d\n", rdp
->arg_size
);
575 fprintf(fp
, "\tlret\n");
576 odp
->offset
= code_offset
;
577 code_offset
+= 10; /* Assembly code is 10 bytes long */
578 if (rdp
->arg_size
!= 0) code_offset
+= 2;
584 fprintf( fp
, "/* %s.%d */\n", UpperDLLName
, i
);
585 fprintf(fp
, "\tpushw %%bp\n" );
586 fprintf(fp
, "\tpushl $0x%08x\n", (DLLId
<< 16) | i
);
587 fprintf(fp
, "\tpushl $" PREFIX
"%s\n", fdp
->internal_name
);
588 fprintf(fp
, "\tljmp $0x%04x, $" PREFIX
"CallTo32_%s_%s\n\n",
590 (odp
->type
== TYPE_REGISTER
) ? "regs" :
591 (odp
->type
== TYPE_PASCAL
) ? "long" : "word",
593 odp
->offset
= code_offset
;
594 code_offset
+= 19; /* Assembly code is 19 bytes long */
598 fprintf( stderr
, "build: Unknown function type; please report.\n");
603 fprintf( fp
, "\t.data\n" );
604 fprintf( fp
, "\t.globl " PREFIX
"%s_Data_End\n", UpperDLLName
);
605 fprintf( fp
, PREFIX
"%s_Data_End:\n", UpperDLLName
);
606 fprintf( fp
, "\t.text\n" );
607 fprintf( fp
, "\t.globl " PREFIX
"%s_Code_End\n", UpperDLLName
);
608 fprintf( fp
, PREFIX
"%s_Code_End:\n", UpperDLLName
);
612 sprintf(filename
, "tab_%s.c", LowerDLLName
);
613 fp
= fopen(filename
, "w");
615 fprintf( fp
, "/* File generated automatically; do not edit! */\n\n" );
616 fprintf( fp
, "#include \"dlls.h\"\n\n" );
617 fprintf( fp
, "static struct dll_table_entry_s %s_table_entries[%d] =\n{\n",
618 UpperDLLName
, Limit
+ 1);
619 odp
= OrdinalDefinitions
;
620 for (i
= 0; i
<= Limit
; i
++, odp
++)
624 fdp
= odp
->additional_data
;
632 selector
= 1; /* Code selector */
638 selector
= 2; /* Data selector */
642 selector
= 0xff; /* Constant selector */
646 fprintf(fp
, " { %d, %d, ", selector
, odp
->offset
);
647 fprintf(fp
, "\"%s\" ", odp
->export_name
);
651 fprintf(fp
, "}, \n");
654 fprintf(fp
, "};\n\n");
656 fprintf( fp
, "extern void %s_Code_Start();\n", UpperDLLName
);
657 fprintf( fp
, "extern void %s_Code_End();\n", UpperDLLName
);
658 fprintf( fp
, "extern void %s_Data_Start();\n", UpperDLLName
);
659 fprintf( fp
, "extern void %s_Data_End();\n\n", UpperDLLName
);
660 fprintf( fp
, "struct dll_table_s %s_table =\n{\n", UpperDLLName
);
661 fprintf( fp
, " %s_table_entries, %d, %d,\n",
662 UpperDLLName
, Limit
+ 1, DLLId
);
663 fprintf( fp
, " (void *)%s_Code_Start, (void *)%s_Code_End,\n",
664 UpperDLLName
, UpperDLLName
);
665 fprintf( fp
, " (void *)%s_Data_Start, (void *)%s_Data_End\n};\n",
666 UpperDLLName
, UpperDLLName
);
671 /*******************************************************************
674 * Get the arguments from the 16-bit stack and push them on the 32-bit stack.
675 * The 16-bit stack layout is:
683 static int TransferArgs16To32( char *args
)
689 printf( "\tpushl %%ebx\n" );
691 /* Get the 32-bit stack pointer */
693 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
695 /* Copy the arguments */
697 pos16
= 6; /* skip bp and return address */
700 for (i
= strlen(args
); i
> 0; i
--)
706 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
707 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
711 case 's': /* s_word */
712 printf( "\tmovswl %d(%%ebp),%%eax\n", pos16
);
713 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
718 printf( "\tmovl %d(%%ebp),%%eax\n", pos16
);
719 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
724 /* Get the selector */
725 printf( "\tmovw %d(%%ebp),%%ax\n", pos16
+ 2 );
726 /* Get the selector base */
727 printf( "\tandl $0xfff8,%%eax\n" );
728 printf( "\tmovl " PREFIX
"ldt_copy(%%eax),%%eax\n" );
729 printf( "\tmovl %%eax,%d(%%ebx)\n", pos32
);
731 printf( "\tmovzwl %d(%%ebp),%%eax\n", pos16
);
732 printf( "\taddl %%eax,%d(%%ebx)\n", pos32
);
737 fprintf( stderr
, "Unknown arg type '%c'\n", args
[i
-1] );
743 printf( "\tpopl %%ebx\n" );
745 return pos16
- 6; /* Return the size of the 16-bit args */
749 /*******************************************************************
752 * Build the context structure on the 32-bit stack.
753 * The only valid registers in the context structure are:
754 * eax, ebx, ecx, edx, esi, edi, ds, es, (some of the) flags
756 static void BuildContext(void)
760 printf( "\tpushl %%ebx\n" );
762 /* Get the 32-bit stack pointer */
764 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
766 /* Store the registers */
768 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_ebx
) ); /* Get ebx from stack */
769 printf( "\tmovl %%eax,%d(%%ebx)\n", CONTEXTOFFSET(sc_eax
) );
770 printf( "\tmovl %%ecx,%d(%%ebx)\n", CONTEXTOFFSET(sc_ecx
) );
771 printf( "\tmovl %%edx,%d(%%ebx)\n", CONTEXTOFFSET(sc_edx
) );
772 printf( "\tmovl %%esi,%d(%%ebx)\n", CONTEXTOFFSET(sc_esi
) );
773 printf( "\tmovl %%edi,%d(%%ebx)\n", CONTEXTOFFSET(sc_edi
) );
774 printf( "\tmovw %%es,%d(%%ebx)\n", CONTEXTOFFSET(sc_es
) );
775 printf( "\tmovw -10(%%ebp),%%ax\n" ); /* Get saved ds from stack */
776 printf( "\tmovw %%ax,%d(%%ebx)\n", CONTEXTOFFSET(sc_ds
) );
777 printf( "\tpushfl\n" );
779 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags
) );
781 printf( "\tpopl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl
) );
786 /*******************************************************************
789 * Restore the registers from the context structure
791 static void RestoreContext(void)
793 /* Get the 32-bit stack pointer */
795 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%ebx\n" );
797 /* Restore the registers */
799 printf( "\tmovl %d(%%ebx),%%ecx\n", CONTEXTOFFSET(sc_ecx
) );
800 printf( "\tmovl %d(%%ebx),%%edx\n", CONTEXTOFFSET(sc_edx
) );
801 printf( "\tmovl %d(%%ebx),%%esi\n", CONTEXTOFFSET(sc_esi
) );
802 printf( "\tmovl %d(%%ebx),%%edi\n", CONTEXTOFFSET(sc_edi
) );
803 printf( "\tmovw %d(%%ebx),%%es\n", CONTEXTOFFSET(sc_es
) );
804 printf( "\tpopw %%ax\n" ); /* Remove old ds from the stack */
805 printf( "\tpushw %d(%%ebx)\n", CONTEXTOFFSET(sc_ds
) ); /* Push new ds */
807 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_eflags
) );
809 printf( "\tpushl %d(%%ebx)\n", CONTEXTOFFSET(sc_efl
) );
811 printf( "\tpopfl\n" );
812 printf( "\tmovl %d(%%ebx),%%eax\n", CONTEXTOFFSET(sc_eax
) );
813 printf( "\tmovl %d(%%ebx),%%ebx\n", CONTEXTOFFSET(sc_ebx
) );
817 /*******************************************************************
820 * Build a 32-bit callback function. The syntax of the function
821 * profile is: type_xxxxx, where 'type' is one of 'regs', 'word' or
822 * 'long' and each 'x' is an argument ('w'=word, 's'=signed word,
823 * 'l'=long, 'p'=pointer).
825 * Stack layout upon entry to the callback function:
827 * (sp+14) first 16-bit arg
831 * (sp+4) dll_id+ordinal (long)
832 * (sp) entrypoint (long)
835 static void BuildCall32Func( char *profile
)
840 char *args
= profile
+ 5;
842 /* Parse function type */
844 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
845 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= 1;
846 else if (strncmp( "long_", profile
, 5 ))
848 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
852 /* Function header */
854 printf( "/**********\n" );
855 printf( " * " PREFIX
"CallTo32_%s\n", profile
);
856 printf( " **********/\n" );
857 printf( "\t.align 4\n" );
858 printf( "\t.global " PREFIX
"CallTo32_%s\n\n", profile
);
859 printf( PREFIX
"CallTo32_%s:\n", profile
);
861 /* Setup bp to point to its copy on the stack */
863 printf( "\tmovzwl %%sp,%%ebp\n" );
864 printf( "\taddw $8,%%bp\n" );
868 printf( "\tpushw %%ds\n" );
870 /* Restore 32-bit ds */
872 printf( "\tpushw $0x%04x\n", WINE_DATA_SELECTOR
);
873 printf( "\tpopw %%ds\n" );
875 /* Save the 16-bit stack */
877 printf( "\tpushw " PREFIX
"IF1632_Saved16_bp\n" );
878 printf( "\tpushw " PREFIX
"IF1632_Saved16_sp\n" );
879 printf( "\tpushw " PREFIX
"IF1632_Saved16_ss\n" );
880 printf( "\tmovw %%ss," PREFIX
"IF1632_Saved16_ss\n" );
881 printf( "\tmovw %%sp," PREFIX
"IF1632_Saved16_sp\n" );
882 printf( "\tmovw %%bp," PREFIX
"IF1632_Saved16_bp\n" );
884 /* Transfer the arguments */
886 if (reg_func
) BuildContext();
887 else if (*args
) argsize
= TransferArgs16To32( args
);
889 /* Get the address of the API function */
891 printf( "\tmovl -8(%%ebp),%%eax\n" );
895 printf( "\tpushw %%ds\n" );
896 printf( "\tpopw %%es\n" );
898 /* Switch to the 32-bit stack */
900 printf( "\tpushw %%ds\n" );
901 printf( "\tpopw %%ss\n" );
902 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
903 printf( "\tmovl " PREFIX
"IF1632_Saved32_ebp,%%ebp\n" );
905 printf( "\tsubl $%d,%%esp\n", sizeof(struct sigcontext_struct
) );
907 printf( "\tsubl $%d,%%esp\n", 4 * strlen(args
) );
909 /* Call the entry point */
913 printf( "\tpushl %%eax\n" );
914 printf( "\tpushl $CALL32_Str_%s\n", profile
);
915 printf( "\tcall " PREFIX
"RELAY_DebugCall32\n" );
916 printf( "\tpopl %%eax\n" );
917 printf( "\tpopl %%eax\n" );
920 printf( "\tcall %%eax\n" );
924 printf( "\tpushl %%eax\n" );
925 printf( "\tpushl $%d\n", short_ret
);
926 printf( "\tcall " PREFIX
"RELAY_DebugReturn\n" );
927 printf( "\tpopl %%eax\n" );
928 printf( "\tpopl %%eax\n" );
932 printf( "\taddl $%d,%%esp\n", sizeof(struct sigcontext_struct
) );
934 printf( "\taddl $%d,%%esp\n", 4 * strlen(args
) );
936 /* Restore the 16-bit stack */
938 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
939 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
940 printf( "\tmovw " PREFIX
"IF1632_Saved16_bp,%%bp\n" );
941 printf( "\tpopw " PREFIX
"IF1632_Saved16_ss\n" );
942 printf( "\tpopw " PREFIX
"IF1632_Saved16_sp\n" );
943 printf( "\tpopw " PREFIX
"IF1632_Saved16_bp\n" );
945 /* Restore registers from the context structure */
949 printf( "\tandl $0xffff,%%ebp\n" );
952 else /* Store the return value in dx:ax if needed */
956 printf( "\tpushl %%eax\n" );
957 printf( "\tpopw %%dx\n" );
958 printf( "\tpopw %%dx\n" );
962 /* Restore ds and bp */
964 printf( "\tpopw %%ds\n" );
965 printf( "\tmovw %%bp,%%sp\n" );
966 printf( "\tpopw %%bp\n" );
968 /* Remove the arguments and return */
972 printf( "\t.byte 0x66\n" );
973 printf( "\tlret $%d\n", argsize
);
977 printf( "\t.byte 0x66\n" );
978 printf( "\tlret\n" );
983 /*******************************************************************
986 * Build a 16-bit callback function.
988 * Stack frame of the callback function:
993 * (ebp+10) func to call
994 * (ebp+8) code selector
995 * (ebp+4) return address
998 * Prototypes for the CallTo16 functions:
999 * extern WORD CallTo16_word_xxx( FARPROC func, WORD ds, args... );
1000 * extern LONG CallTo16_long_xxx( FARPROC func, WORD ds, args... );
1001 * extern void CallTo16_regs_( FARPROC func, WORD ds, WORD es, WORD ax,
1002 * WORD bx, WORD cx, WORD dx, WORD si, WORD di );
1004 static void BuildCall16Func( char *profile
)
1008 char *args
= profile
+ 5;
1010 if (!strncmp( "word_", profile
, 5 )) short_ret
= 1;
1011 else if (!strncmp( "regs_", profile
, 5 )) reg_func
= short_ret
= 1;
1012 else if (strncmp( "long_", profile
, 5 ))
1014 fprintf( stderr
, "Invalid function name '%s', ignored\n", profile
);
1018 /* Function header */
1020 printf( "/**********\n" );
1021 printf( " * " PREFIX
"CallTo16_%s\n", profile
);
1022 printf( " **********/\n" );
1023 printf( "\t.align 4\n" );
1024 printf( "\t.global " PREFIX
"CallTo16_%s\n\n", profile
);
1025 printf( PREFIX
"CallTo16_%s:\n", profile
);
1027 /* Push code selector before return address to simulate a lcall */
1029 printf( "\tpopl %%eax\n" );
1030 printf( "\tpushw $0x%04x\n", WINE_CODE_SELECTOR
);
1031 printf( "\tpushl %%eax\n" );
1035 printf( "\tpushl %%ebp\n" );
1036 printf( "\tmovl %%esp,%%ebp\n" );
1038 /* Save the 32-bit registers */
1040 printf( "\tpushl %%ebx\n" );
1041 printf( "\tpushl %%ecx\n" );
1042 printf( "\tpushl %%edx\n" );
1043 printf( "\tpushl %%esi\n" );
1044 printf( "\tpushl %%edi\n" );
1046 /* Save the 32-bit stack */
1048 printf( "\tpushl " PREFIX
"IF1632_Saved32_esp\n" );
1049 printf( "\tpushl " PREFIX
"IF1632_Saved32_ebp\n" );
1050 printf( "\tmovl %%esp," PREFIX
"IF1632_Saved32_esp\n" );
1051 printf( "\tmovl %%ebp," PREFIX
"IF1632_Saved32_ebp\n" );
1052 printf( "\tmovl %%ebp,%%ebx\n" );
1054 /* Print debugging info */
1058 /* Push the address of the first argument */
1059 printf( "\tmovl %%ebx,%%eax\n" );
1060 printf( "\taddl $10,%%eax\n" );
1061 printf( "\tpushl $%d\n", reg_func
? 7 : strlen(args
) );
1062 printf( "\tpushl %%eax\n" );
1063 printf( "\tcall " PREFIX
"RELAY_DebugCall16\n" );
1064 printf( "\tpopl %%eax\n" );
1065 printf( "\tpopl %%eax\n" );
1068 /* Switch to the 16-bit stack */
1070 printf( "\tmovw " PREFIX
"IF1632_Saved16_ss,%%ss\n" );
1071 printf( "\tmovw " PREFIX
"IF1632_Saved16_sp,%%sp\n" );
1072 printf( "\tmovzwl " PREFIX
"IF1632_Saved16_bp,%%ebp\n" );
1074 /* Transfer the arguments */
1078 /* Get the registers. ebx is handled later on. */
1079 printf( "\tmovl 18(%%ebx),%%es\n" );
1080 printf( "\tmovl 22(%%ebx),%%eax\n" );
1081 printf( "\tmovl 30(%%ebx),%%ecx\n" );
1082 printf( "\tmovl 34(%%ebx),%%edx\n" );
1083 printf( "\tmovl 38(%%ebx),%%esi\n" );
1084 printf( "\tmovl 42(%%ebx),%%edi\n" );
1086 else /* not a register function */
1088 int pos
= 18; /* first argument position */
1093 case 'w': /* word */
1094 printf( "\tpushw %d(%%ebx)\n", pos
);
1096 case 'l': /* long */
1097 printf( "\tpushl %d(%%ebx)\n", pos
);
1104 /* Push the return address */
1106 printf( "\tpushl " PREFIX
"CALL16_RetAddr_%s\n",
1107 short_ret
? "word" : "long" );
1109 /* Push the called routine address */
1111 printf( "\tpushl 10(%%ebx)\n" );
1113 /* Get the 16-bit ds */
1114 /* FIXME: this shouldn't be necessary if function prologs fixup worked. */
1116 printf( "\tmovw 14(%%ebx),%%ds\n" );
1120 /* Retrieve ebx from the 32-bit stack */
1121 printf( "\tmovl %%fs:26(%%ebx),%%ebx\n" );
1125 /* Set ax equal to ds for window procedures */
1126 printf( "\tmovw %%ds,%%ax\n" );
1129 /* Jump to the called routine */
1131 printf( "\t.byte 0x66\n" );
1132 printf( "\tlret\n" );
1136 /*******************************************************************
1139 * Build the return code for 16-bit callbacks
1141 static void BuildRet16Func()
1143 printf( "\t.globl " PREFIX
"CALL16_Ret_word\n" );
1144 printf( "\t.globl " PREFIX
"CALL16_Ret_long\n" );
1146 /* Put return value into eax */
1148 printf( PREFIX
"CALL16_Ret_long:\n" );
1149 printf( "\tpushw %%dx\n" );
1150 printf( "\tpushw %%ax\n" );
1151 printf( "\tpopl %%eax\n" );
1152 printf( PREFIX
"CALL16_Ret_word:\n" );
1154 /* Restore 32-bit segment registers */
1156 printf( "\tmovw $0x%04x,%%bx\n", WINE_DATA_SELECTOR
);
1157 printf( "\tmovw %%bx,%%ds\n" );
1158 printf( "\tmovw %%bx,%%es\n" );
1159 printf( "\tmovw %%bx,%%ss\n" );
1161 /* Restore the 32-bit stack */
1163 printf( "\tmovl " PREFIX
"IF1632_Saved32_esp,%%esp\n" );
1164 printf( "\tmovl " PREFIX
"IF1632_Saved32_ebp,%%ebp\n" );
1165 printf( "\tpopl " PREFIX
"IF1632_Saved32_ebp\n" );
1166 printf( "\tpopl " PREFIX
"IF1632_Saved32_esp\n" );
1168 /* Restore the 32-bit registers */
1170 printf( "\tpopl %%edi\n" );
1171 printf( "\tpopl %%esi\n" );
1172 printf( "\tpopl %%edx\n" );
1173 printf( "\tpopl %%ecx\n" );
1174 printf( "\tpopl %%ebx\n" );
1176 /* Return to caller */
1178 printf( "\tmovl %%ebp,%%esp\n" );
1179 printf( "\tpopl %%ebp\n" );
1180 printf( "\tlret\n" );
1182 /* Declare the return address variables */
1184 printf( "\t.data\n" );
1185 printf( "\t.globl " PREFIX
"CALL16_RetAddr_word\n" );
1186 printf( "\t.globl " PREFIX
"CALL16_RetAddr_long\n" );
1187 printf( PREFIX
"CALL16_RetAddr_word:\t.long 0\n" );
1188 printf( PREFIX
"CALL16_RetAddr_long:\t.long 0\n" );
1189 printf( "\t.text\n" );
1193 static void usage(void)
1195 fprintf(stderr
, "usage: build -spec SPECNAMES\n"
1196 " build -call32 FUNCTION_PROFILES\n"
1197 " build -call16 FUNCTION_PROFILES\n" );
1202 int main(int argc
, char **argv
)
1206 if (argc
<= 2) usage();
1208 if (!strcmp( argv
[1], "-spec" ))
1210 for (i
= 2; i
< argc
; i
++) BuildSpecFiles( argv
[i
] );
1212 else if (!strcmp( argv
[1], "-call32" )) /* 32-bit callbacks */
1216 printf( "/* File generated automatically. Do no edit! */\n\n" );
1217 printf( "\t.text\n" );
1219 /* Build the callback functions */
1221 for (i
= 2; i
< argc
; i
++) BuildCall32Func( argv
[i
] );
1223 /* Output the argument debugging strings */
1227 printf( "/* Argument strings */\n" );
1228 for (i
= 2; i
< argc
; i
++)
1230 printf( "CALL32_Str_%s:\n", argv
[i
] );
1231 printf( "\t.ascii \"%s\\0\"\n", argv
[i
] + 5 );
1235 else if (!strcmp( argv
[1], "-call16" )) /* 16-bit callbacks */
1239 printf( "/* File generated automatically. Do no edit! */\n\n" );
1240 printf( "\t.text\n" );
1241 printf( "\t.globl " PREFIX
"CALL16_Start\n" );
1242 printf( PREFIX
"CALL16_Start:\n" );
1244 /* Build the callback functions */
1246 for (i
= 2; i
< argc
; i
++) BuildCall16Func( argv
[i
] );
1248 /* Output the 16-bit return code */
1252 printf( "\t.globl " PREFIX
"CALL16_End\n" );
1253 printf( PREFIX
"CALL16_End:\n" );