2 * Code generation functions
4 * Copyright 2000-2002 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
27 static FILE *specfile
= NULL
;
28 static FILE *hfile
= NULL
;
29 static FILE *cfile
= NULL
;
31 static void output_spec_postamble (void);
32 static void output_header_postamble (void);
33 static void output_c_postamble (void);
34 static void output_c_banner (const parsed_symbol
*sym
);
35 static const char *get_format_str (int type
);
36 static const char *get_in_or_out (const parsed_symbol
*sym
, size_t arg
);
39 /*******************************************************************
40 * output_spec_preamble
42 * Write the first part of the .spec file
44 void output_spec_preamble (void)
46 specfile
= open_file (OUTPUT_DLL_NAME
, ".spec", "w");
48 atexit (output_spec_postamble
);
51 puts ("Creating .spec preamble");
54 "# Generated from %s by winedump\ninit %s_Init\n\n",
55 globals
.input_name
, OUTPUT_UC_DLL_NAME
);
59 /*******************************************************************
62 * Write a symbol to the .spec file
64 void output_spec_symbol (const parsed_symbol
*sym
)
69 assert (sym
&& sym
->symbol
);
71 if (sym
->ordinal
>= 0)
72 snprintf(ord_spec
, 8, "%d", sym
->ordinal
);
78 if (sym
->flags
& SYM_THISCALL
)
79 strcat (ord_spec
, " -i386"); /* For binary compatibility only */
81 if (!globals
.do_code
|| !sym
->function_name
)
83 if (sym
->flags
& SYM_DATA
)
85 if (globals
.forward_dll
)
86 fprintf (specfile
, "%s forward %s %s.%s #", ord_spec
, sym
->symbol
,
87 globals
.forward_dll
, sym
->symbol
);
89 fprintf (specfile
, "%s extern %s %s\n", ord_spec
, sym
->symbol
,
94 if (globals
.forward_dll
)
95 fprintf (specfile
, "%s forward %s %s.%s\n", ord_spec
, sym
->symbol
,
96 globals
.forward_dll
, sym
->symbol
);
98 fprintf (specfile
, "%s stub %s\n", ord_spec
, sym
->symbol
);
102 unsigned int i
= sym
->flags
& SYM_THISCALL
? 1 : 0;
104 fprintf (specfile
, "%s %s %s(", ord_spec
, sym
->varargs
? "varargs" :
105 symbol_get_call_convention(sym
), sym
->symbol
);
107 for (; i
< sym
->argc
; i
++)
108 fprintf (specfile
, " %s", symbol_get_spec_type(sym
, i
));
111 fputc (' ', specfile
);
112 fprintf (specfile
, ") %s_%s", OUTPUT_UC_DLL_NAME
, sym
->function_name
);
114 if (sym
->flags
& SYM_THISCALL
)
115 fputs (" # __thiscall", specfile
);
117 fputc ('\n',specfile
);
122 /*******************************************************************
123 * output_spec_postamble
125 * Write the last part of the .spec file
127 static void output_spec_postamble (void)
135 /*******************************************************************
136 * output_header_preamble
138 * Write the first part of the .h file
140 void output_header_preamble (void)
142 hfile
= open_file (OUTPUT_DLL_NAME
, "_dll.h", "w");
144 atexit (output_header_postamble
);
147 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
148 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
149 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n#include "
150 "\"config.h\"\n#include \"windef.h\"\n#include \"wine/debug.h\"\n"
151 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
152 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_UC_DLL_NAME
,
157 /*******************************************************************
158 * output_header_symbol
160 * Write a symbol to the .h file
162 void output_header_symbol (const parsed_symbol
*sym
)
165 assert (sym
&& sym
->symbol
);
167 if (!globals
.do_code
)
170 if (sym
->flags
& SYM_DATA
)
173 if (!sym
->function_name
)
174 fprintf (hfile
, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym
),
175 OUTPUT_UC_DLL_NAME
, sym
->symbol
);
178 output_prototype (hfile
, sym
);
179 fputs (";\n", hfile
);
184 /*******************************************************************
185 * output_header_postamble
187 * Write the last part of the .h file
189 static void output_header_postamble (void)
193 fprintf (hfile
, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
201 /*******************************************************************
204 * Write the first part of the .c file
206 void output_c_preamble (void)
208 cfile
= open_file (OUTPUT_DLL_NAME
, "_main.c", "w");
210 atexit (output_c_postamble
);
213 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
214 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
215 "\n\n#include \"%s_dll.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
216 OUTPUT_DLL_NAME
, globals
.input_name
, OUTPUT_DLL_NAME
,
219 if (globals
.forward_dll
)
222 puts ("Creating a forwarding DLL");
224 fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile
);
227 fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
228 " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile
);
231 "BOOL WINAPI %s_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
232 "lpvReserved)\n{\n\tTRACE(\"(0x%%08x, %%ld, %%p)\\n\",hinstDLL,"
233 "fdwReason,lpvReserved);\n\n\t"
234 "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t",
237 if (globals
.forward_dll
)
240 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
241 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
242 globals
.forward_dll
, globals
.forward_dll
);
245 fputs ("/* FIXME: Initialisation */", cfile
);
247 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
250 if (globals
.forward_dll
)
253 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
254 " freed\\n\");", globals
.forward_dll
);
257 fputs ("/* FIXME: Cleanup */", cfile
);
259 fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile
);
263 #define CPP_END if (sym->flags & SYM_THISCALL) \
264 fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
265 #define GET_THIS if (sym->flags & SYM_THISCALL) \
266 fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
268 /*******************************************************************
271 * Write a symbol to the .c file
273 void output_c_symbol (const parsed_symbol
*sym
)
275 unsigned int i
, start
= sym
->flags
& SYM_THISCALL
? 1 : 0;
279 assert (sym
&& sym
->symbol
);
281 if (!globals
.do_code
)
284 if (sym
->flags
& SYM_DATA
)
286 fprintf (cfile
, "/* FIXME: Move to top of file */\n%s;\n\n",
291 if (sym
->flags
& SYM_THISCALL
)
292 fputs ("#ifdef __i386__\n", cfile
);
294 output_c_banner(sym
);
296 if (!sym
->function_name
)
299 fprintf (cfile
, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
300 symbol_get_call_convention(sym
), OUTPUT_UC_DLL_NAME
, sym
->symbol
,
301 globals
.forward_dll
? "@forward" : "@stub");
306 is_void
= !strcmp (sym
->return_text
, "void");
308 output_prototype (cfile
, sym
);
309 fputs ("\n{\n", cfile
);
311 if (!globals
.do_trace
)
314 fputs ("\tFIXME(\":stub\\n\");\n", cfile
);
316 fprintf (cfile
, "\treturn (%s) 0;\n", sym
->return_text
);
317 fputs ("}\n", cfile
);
322 /* Tracing, maybe forwarding as well */
323 if (globals
.forward_dll
)
325 /* Write variables for calling */
327 fputs("\tva_list valist;\n", cfile
);
329 fprintf (cfile
, "\t%s (__%s *pFunc)(", sym
->return_text
,
330 symbol_get_call_convention(sym
));
332 for (i
= start
; i
< sym
->argc
; i
++)
333 fprintf (cfile
, "%s%s", i
> start
? ", " : "", sym
->arg_text
[i
]);
335 fprintf (cfile
, "%s);\n", sym
->varargs
? ",..." : sym
->argc
== 1 &&
336 sym
->flags
& SYM_THISCALL
? "" : sym
->argc
? "" : "void");
339 fprintf (cfile
, "\t%s retVal;\n", sym
->return_text
);
343 fprintf (cfile
, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
347 /* TRACE input arguments */
348 fprintf (cfile
, "\tTRACE(\"(%s", !sym
->argc
? "void" : "");
350 for (i
= 0; i
< sym
->argc
; i
++)
351 fprintf (cfile
, "%s(%s)%s", i
? "," : "", sym
->arg_text
[i
],
352 get_format_str (sym
->arg_type
[i
]));
354 fprintf (cfile
, "%s): %s\\n\"", sym
->varargs
? ",..." : "",
355 globals
.forward_dll
? "forward" : "stub");
357 for (i
= 0; i
< sym
->argc
; i
++)
358 if (sym
->arg_type
[i
] != ARG_STRUCT
)
359 fprintf(cfile
, ",%s%s%s%s", sym
->arg_type
[i
] == ARG_LONG
? "(LONG)" : "",
360 sym
->arg_type
[i
] == ARG_WIDE_STRING
? "debugstr_w(" : "",
362 sym
->arg_type
[i
] == ARG_WIDE_STRING
? ")" : "");
364 fputs (");\n", cfile
);
366 if (!globals
.forward_dll
)
369 fprintf (cfile
, "\treturn (%s) 0;\n", sym
->return_text
);
370 fputs ("}\n", cfile
);
377 fprintf (cfile
, "\tva_start(valist,%s);\n", sym
->arg_name
[sym
->argc
-1]);
379 fprintf (cfile
, "\t%spFunc(", !is_void
? "retVal = " : "");
381 for (i
= 0; i
< sym
->argc
; i
++)
382 fprintf (cfile
, "%s%s", i
? "," : "", sym
->arg_name
[i
]);
384 fputs (sym
->varargs
? ",valist);\n\tva_end(valist);" : ");", cfile
);
386 /* TRACE return value */
387 fprintf (cfile
, "\n\tTRACE(\"Returned (%s)\\n\"",
388 get_format_str (sym
->return_type
));
392 if (sym
->return_type
== ARG_WIDE_STRING
)
393 fputs (",debugstr_w(retVal)", cfile
);
395 fprintf (cfile
, ",%s%s", sym
->return_type
== ARG_LONG
? "(LONG)" : "",
396 sym
->return_type
== ARG_STRUCT
? "" : "retVal");
397 fputs (");\n\treturn retVal;\n", cfile
);
400 fputs (");\n", cfile
);
402 fputs ("}\n", cfile
);
407 /*******************************************************************
410 * Write the last part of the .c file
412 static void output_c_postamble (void)
420 /*******************************************************************
423 * Write a Wine compatible makefile.in
425 void output_makefile (void)
427 FILE *makefile
= open_file ("Makefile", ".in", "w");
430 puts ("Creating makefile");
433 "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
434 "TOPOBJDIR = ../..\nSRCDIR = @srcdir@\nVPATH = @srcdir@\n"
435 "MODULE = %s.dll\n", globals
.input_name
, OUTPUT_DLL_NAME
);
437 fprintf (makefile
, "IMPORTS = user32 advapi32 kernel32 ntdll");
438 if (globals
.forward_dll
)
439 fprintf (makefile
, " %s", globals
.forward_dll
);
442 "\nEXTRALIBS = $(LIBUNICODE)\n\nLDDLLFLAGS = @LDDLLFLAGS@\n"
443 "SYMBOLFILE = $(MODULE).tmp.o\n\n"
444 "C_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
447 if (globals
.forward_dll
)
448 fprintf (specfile
,"#import %s.dll\n", globals
.forward_dll
);
454 /*******************************************************************
455 * output_install_script
457 * Write a script to insert the DLL into Wine
459 * Rather than using diff/patch, several sed calls are generated
460 * so the script can be re-run at any time without breaking.
462 void output_install_script (void)
465 FILE *install_file
= open_file (OUTPUT_DLL_NAME
, "_install", "w");
468 puts ("Creating install script");
470 fprintf (install_file
,
471 "#!/bin/bash\n# Generated from %s.dll by winedump.\n\n"
472 "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f"
473 " $1/AUTHORS ]; then\n\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
474 "\techo \"Usage: $0 wine-base-dir\"\n\texit 1\nfi\n\n"
475 "if [ -d $1/dlls/%s ]; then\n\techo \"DLL is already present\"\n"
476 "\texit 1\nfi\n\necho Adding DLL %s to Wine build tree...\n"
477 "echo\n\nmkdir $1/dlls/%s\ncp %s.spec $1/dlls/%s\n"
478 "cp %s_main.c $1/dlls/%s\ncp %s_dll.h $1/dlls/%s\n"
479 "cp Makefile.in $1/dlls/%s/Makefile.in\necho Copied DLL files\n\n"
480 "cd $1\n\nsed '/dlls\\/"
481 "x11drv\\/Makefile/{G;s/$/dlls\\/%s\\/Makefile/;}' configure.ac"
482 " >t.tmp\nmv -f t.tmp configure.ac\necho Patched configure.ac\n\n"
483 "sed '/^all:/{G;s/$/\\^%s.dll$(DLLEXT) \\\\/;}'"
484 " dlls/Makefile.in| tr ^ \\\\t >t.tmp\n"
485 "sed '/BASEDIRS =/{G;s/$/\\^%s \\\\/;}' t.tmp | tr ^ \\\\t >t.tmp2"
486 "\nsed '/Map symlink name /{G;s/$/^\\$(RM) \\$\\@ \\&\\& \\$\\"
487 "(LN_S\\) %s\\/%s.dll\\$(DLLEXT) \\$\\@/;}' t.tmp2 | tr ^ \\\\t"
488 " > t.tmp\nsed '/Map symlink name /{G;s/$/%s.dll\\$(DLLEXT): "
489 "%s\\/%s.dll\\$(DLLEXT)/;}' t.tmp > t.tmp2\nsed '/all dependencies"
490 "/{G;s/$/%s\\/__install__: %s.dll$(DLLEXT)/;}' t.tmp2 > t.tmp\n"
491 "sed '/dll dependencies/{G;s/$/%s: user32.dll\\$(DLLEXT) "
492 "kernel32.dll\\$(DLLEXT) ntdll.dll\\$(DLLEXT) advapi32.dll"
493 "\\$(DLLEXT)/;}' t.tmp > t.tmp2\n\nsed '/Map library name "
494 "/{G;s/$/%s\\/%s.dll\\$(DLLEXT): %s/;}' t.tmp2 >t.tmp\n"
495 "mv -f t.tmp dlls/Makefile.in\nrm -f t.tmp2\necho Patched dlls/"
496 "Makefile.in\n\necho\necho ...done.\necho Run \\'autoconf\\', "
497 "\\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
498 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
499 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
500 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
501 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
502 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
503 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
506 fclose (install_file
);
507 snprintf (cmd
, sizeof (cmd
), "chmod a+x %s_install", OUTPUT_DLL_NAME
);
512 /*******************************************************************
515 * Write a C prototype for a parsed symbol
517 void output_prototype (FILE *file
, const parsed_symbol
*sym
)
519 unsigned int i
, start
= sym
->flags
& SYM_THISCALL
? 1 : 0;
521 fprintf (file
, "%s __%s %s_%s(", sym
->return_text
, symbol_get_call_convention(sym
),
522 OUTPUT_UC_DLL_NAME
, sym
->function_name
);
524 if (!sym
->argc
|| (sym
->argc
== 1 && sym
->flags
& SYM_THISCALL
))
525 fputs ("void", file
);
527 for (i
= start
; i
< sym
->argc
; i
++)
528 fprintf (file
, "%s%s %s", i
> start
? ", " : "", sym
->arg_text
[i
],
531 fputs (", ...", file
);
536 /*******************************************************************
539 * Write a function banner to the .c file
541 void output_c_banner (const parsed_symbol
*sym
)
546 if (sym
->ordinal
>= 0)
547 snprintf(ord_spec
, sizeof (ord_spec
), "%d", sym
->ordinal
);
554 fprintf (cfile
, "/*********************************************************"
555 "*********\n *\t\t%s (%s.%s)\n *\n", sym
->symbol
,
556 OUTPUT_UC_DLL_NAME
, ord_spec
);
558 if (globals
.do_documentation
&& sym
->function_name
)
560 fputs (" *\n * PARAMS\n *\n", cfile
);
563 fputs (" * None.\n *\n", cfile
);
566 for (i
= 0; i
< sym
->argc
; i
++)
567 fprintf (cfile
, " * %s [%s]%s\n", sym
->arg_name
[i
],
568 get_in_or_out(sym
, i
),
569 strcmp (sym
->arg_name
[i
], "_this") ? "" :
570 " Pointer to the class object (in ECX)");
573 fputs (" * ...[I]\n", cfile
);
574 fputs (" *\n", cfile
);
577 fputs (" * RETURNS\n *\n", cfile
);
579 if (sym
->return_text
&& !strcmp (sym
->return_text
, "void"))
580 fputs (" * Nothing.\n", cfile
);
582 fprintf (cfile
, " * %s\n", sym
->return_text
);
584 fputs (" *\n */\n", cfile
);
588 /*******************************************************************
591 * Get a string containing the correct format string for a type
593 static const char *get_format_str (int type
)
597 case ARG_VOID
: return "void";
598 case ARG_FLOAT
: return "%f";
599 case ARG_DOUBLE
: return "%g";
600 case ARG_POINTER
: return "%p";
601 case ARG_WIDE_STRING
:
602 case ARG_STRING
: return "%s";
603 case ARG_LONG
: return "%ld";
604 case ARG_STRUCT
: return "struct";
611 /*******************************************************************
614 * Determine if a parameter is In or In/Out
616 static const char *get_in_or_out (const parsed_symbol
*sym
, size_t arg
)
618 assert (sym
&& arg
< sym
->argc
);
619 assert (globals
.do_documentation
);
621 if (sym
->arg_flag
[arg
] & CT_CONST
)
624 switch (sym
->arg_type
[arg
])
629 case ARG_STRUCT
: return "In";
631 case ARG_WIDE_STRING
:
632 case ARG_STRING
: return "In/Out";