2 * Code generation functions
4 * Copyright 2000 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\nname %s\n"
55 "type win32\ninit %s_Init\n\nimport kernel32.dll\n"
56 "import ntdll.dll\n", globals
.input_name
, OUTPUT_DLL_NAME
,
59 if (globals
.forward_dll
)
60 fprintf (specfile
,"#import %s.dll\n", globals
.forward_dll
);
62 fprintf (specfile
, "\n\ndebug_channels (%s)\n\n", OUTPUT_DLL_NAME
);
66 /*******************************************************************
69 * Write a symbol to the .spec file
71 void output_spec_symbol (const parsed_symbol
*sym
)
76 assert (sym
&& sym
->symbol
);
78 if (sym
->ordinal
>= 0)
79 snprintf(ord_spec
, 8, "%d", sym
->ordinal
);
85 if (sym
->flags
& SYM_THISCALL
)
86 strcat (ord_spec
, " -i386"); /* For binary compatibility only */
88 if (!globals
.do_code
|| !sym
->function_name
)
90 if (sym
->flags
& SYM_DATA
)
92 if (globals
.forward_dll
)
93 fprintf (specfile
, "%s forward %s %s.%s #", ord_spec
, sym
->symbol
,
94 globals
.forward_dll
, sym
->symbol
);
96 fprintf (specfile
, "%s extern %s %s\n", ord_spec
, sym
->symbol
,
101 if (globals
.forward_dll
)
102 fprintf (specfile
, "%s forward %s %s.%s\n", ord_spec
, sym
->symbol
,
103 globals
.forward_dll
, sym
->symbol
);
105 fprintf (specfile
, "%s stub %s\n", ord_spec
, sym
->symbol
);
109 unsigned int i
= sym
->flags
& SYM_THISCALL
? 1 : 0;
111 fprintf (specfile
, "%s %s %s(", ord_spec
, sym
->varargs
? "varargs" :
112 symbol_get_call_convention(sym
), sym
->symbol
);
114 for (; i
< sym
->argc
; i
++)
115 fprintf (specfile
, " %s", symbol_get_spec_type(sym
, i
));
118 fputc (' ', specfile
);
119 fprintf (specfile
, ") %s_%s", OUTPUT_UC_DLL_NAME
, sym
->function_name
);
121 if (sym
->flags
& SYM_THISCALL
)
122 fputs (" # __thiscall", specfile
);
124 fputc ('\n',specfile
);
129 /*******************************************************************
130 * output_spec_postamble
132 * Write the last part of the .spec file
134 static void output_spec_postamble (void)
142 /*******************************************************************
143 * output_header_preamble
145 * Write the first part of the .h file
147 void output_header_preamble (void)
149 hfile
= open_file (OUTPUT_DLL_NAME
, "_dll.h", "w");
151 atexit (output_header_postamble
);
154 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
155 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
156 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n#include "
157 "\"config.h\"\n#include \"windef.h\"\n#include \"wine/debug.h\"\n"
158 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
159 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_UC_DLL_NAME
,
164 /*******************************************************************
165 * output_header_symbol
167 * Write a symbol to the .h file
169 void output_header_symbol (const parsed_symbol
*sym
)
172 assert (sym
&& sym
->symbol
);
174 if (!globals
.do_code
)
177 if (sym
->flags
& SYM_DATA
)
180 if (!sym
->function_name
)
181 fprintf (hfile
, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym
),
182 OUTPUT_UC_DLL_NAME
, sym
->symbol
);
185 output_prototype (hfile
, sym
);
186 fputs (";\n", hfile
);
191 /*******************************************************************
192 * output_header_postamble
194 * Write the last part of the .h file
196 static void output_header_postamble (void)
200 fprintf (hfile
, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
208 /*******************************************************************
211 * Write the first part of the .c file
213 void output_c_preamble (void)
215 cfile
= open_file (OUTPUT_DLL_NAME
, "_main.c", "w");
217 atexit (output_c_postamble
);
220 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
221 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
222 "\n\n#include \"%s_dll.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
223 OUTPUT_DLL_NAME
, globals
.input_name
, OUTPUT_DLL_NAME
,
226 if (globals
.forward_dll
)
229 puts ("Creating a forwarding DLL");
231 fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile
);
234 fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
235 " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile
);
238 "BOOL WINAPI %s_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
239 "lpvReserved)\n{\n\tTRACE(\"(0x%%08x, %%ld, %%p)\\n\",hinstDLL,"
240 "fdwReason,lpvReserved);\n\n\t"
241 "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t",
244 if (globals
.forward_dll
)
247 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
248 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
249 globals
.forward_dll
, globals
.forward_dll
);
252 fputs ("/* FIXME: Initialisation */", cfile
);
254 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
257 if (globals
.forward_dll
)
260 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
261 " freed\\n\");", globals
.forward_dll
);
264 fputs ("/* FIXME: Cleanup */", cfile
);
266 fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile
);
270 #define CPP_END if (sym->flags & SYM_THISCALL) \
271 fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
272 #define GET_THIS if (sym->flags & SYM_THISCALL) \
273 fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
275 /*******************************************************************
278 * Write a symbol to the .c file
280 void output_c_symbol (const parsed_symbol
*sym
)
282 unsigned int i
, start
= sym
->flags
& SYM_THISCALL
? 1 : 0;
286 assert (sym
&& sym
->symbol
);
288 if (!globals
.do_code
)
291 if (sym
->flags
& SYM_DATA
)
293 fprintf (cfile
, "/* FIXME: Move to top of file */\n%s;\n\n",
298 if (sym
->flags
& SYM_THISCALL
)
299 fputs ("#ifdef __i386__\n", cfile
);
301 output_c_banner(sym
);
303 if (!sym
->function_name
)
306 fprintf (cfile
, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
307 symbol_get_call_convention(sym
), OUTPUT_UC_DLL_NAME
, sym
->symbol
,
308 globals
.forward_dll
? "@forward" : "@stub");
313 is_void
= !strcmp (sym
->return_text
, "void");
315 output_prototype (cfile
, sym
);
316 fputs ("\n{\n", cfile
);
318 if (!globals
.do_trace
)
321 fputs ("\tFIXME(\":stub\\n\");\n", cfile
);
323 fprintf (cfile
, "\treturn (%s) 0;\n", sym
->return_text
);
324 fputs ("}\n", cfile
);
329 /* Tracing, maybe forwarding as well */
330 if (globals
.forward_dll
)
332 /* Write variables for calling */
334 fputs("\tva_list valist;\n", cfile
);
336 fprintf (cfile
, "\t%s (__%s *pFunc)(", sym
->return_text
,
337 symbol_get_call_convention(sym
));
339 for (i
= start
; i
< sym
->argc
; i
++)
340 fprintf (cfile
, "%s%s", i
> start
? ", " : "", sym
->arg_text
[i
]);
342 fprintf (cfile
, "%s);\n", sym
->varargs
? ",..." : sym
->argc
== 1 &&
343 sym
->flags
& SYM_THISCALL
? "" : sym
->argc
? "" : "void");
346 fprintf (cfile
, "\t%s retVal;\n", sym
->return_text
);
350 fprintf (cfile
, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
354 /* TRACE input arguments */
355 fprintf (cfile
, "\tTRACE(\"(%s", !sym
->argc
? "void" : "");
357 for (i
= 0; i
< sym
->argc
; i
++)
358 fprintf (cfile
, "%s(%s)%s", i
? "," : "", sym
->arg_text
[i
],
359 get_format_str (sym
->arg_type
[i
]));
361 fprintf (cfile
, "%s): %s\\n\"", sym
->varargs
? ",..." : "",
362 globals
.forward_dll
? "forward" : "stub");
364 for (i
= 0; i
< sym
->argc
; i
++)
365 if (sym
->arg_type
[i
] != ARG_STRUCT
)
366 fprintf(cfile
, ",%s%s%s%s", sym
->arg_type
[i
] == ARG_LONG
? "(LONG)" : "",
367 sym
->arg_type
[i
] == ARG_WIDE_STRING
? "debugstr_w(" : "",
369 sym
->arg_type
[i
] == ARG_WIDE_STRING
? ")" : "");
371 fputs (");\n", cfile
);
373 if (!globals
.forward_dll
)
376 fprintf (cfile
, "\treturn (%s) 0;\n", sym
->return_text
);
377 fputs ("}\n", cfile
);
384 fprintf (cfile
, "\tva_start(valist,%s);\n", sym
->arg_name
[sym
->argc
-1]);
386 fprintf (cfile
, "\t%spFunc(", !is_void
? "retVal = " : "");
388 for (i
= 0; i
< sym
->argc
; i
++)
389 fprintf (cfile
, "%s%s", i
? "," : "", sym
->arg_name
[i
]);
391 fputs (sym
->varargs
? ",valist);\n\tva_end(valist);" : ");", cfile
);
393 /* TRACE return value */
394 fprintf (cfile
, "\n\tTRACE(\"Returned (%s)\\n\"",
395 get_format_str (sym
->return_type
));
399 if (sym
->return_type
== ARG_WIDE_STRING
)
400 fputs (",debugstr_w(retVal)", cfile
);
402 fprintf (cfile
, ",%s%s", sym
->return_type
== ARG_LONG
? "(LONG)" : "",
403 sym
->return_type
== ARG_STRUCT
? "" : "retVal");
404 fputs (");\n\treturn retVal;\n", cfile
);
407 fputs (");\n", cfile
);
409 fputs ("}\n", cfile
);
414 /*******************************************************************
417 * Write the last part of the .c file
419 static void output_c_postamble (void)
427 /*******************************************************************
430 * Write a Wine compatible makefile.in
432 void output_makefile (void)
434 FILE *makefile
= open_file ("Makefile", ".in", "w");
437 puts ("Creating makefile");
440 "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
441 "TOPOBJDIR = ../..\nSRCDIR = @srcdir@\nVPATH = @srcdir@\n"
442 "MODULE = %s\nEXTRALIBS = $(LIBUNICODE)\n\n"
443 "LDDLLFLAGS = @LDDLLFLAGS@\nSYMBOLFILE = $(MODULE).tmp.o\n\n"
444 "C_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
445 globals
.input_name
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
);
451 /*******************************************************************
452 * output_install_script
454 * Write a script to insert the DLL into Wine
456 * Rather than using diff/patch, several sed calls are generated
457 * so the script can be re-run at any time without breaking.
459 void output_install_script (void)
462 FILE *install_file
= open_file (OUTPUT_DLL_NAME
, "_install", "w");
465 puts ("Creating install script");
467 fprintf (install_file
,
468 "#!/bin/bash\n# Generated from %s.dll by winedump.\n\n"
469 "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f"
470 " $1/AUTHORS ]; then\n\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
471 "\techo \"Usage: $0 wine-base-dir\"\n\texit 1\nfi\n\n"
472 "if [ -d $1/dlls/%s ]; then\n\techo \"DLL is already present\"\n"
473 "\texit 1\nfi\n\necho Adding DLL %s to Wine build tree...\n"
474 "echo\n\nmkdir $1/dlls/%s\ncp %s.spec $1/dlls/%s\n"
475 "cp %s_main.c $1/dlls/%s\ncp %s_dll.h $1/dlls/%s\n"
476 "cp Makefile.in $1/dlls/%s/Makefile.in\necho Copied DLL files\n\n"
477 "cd $1\n\nsed '/dlls\\/"
478 "x11drv\\/Makefile/{G;s/$/dlls\\/%s\\/Makefile/;}' configure.in"
479 " >t.tmp\nmv -f t.tmp configure.in\necho Patched configure.in\n\n"
480 "sed '/all:/{G;s/$/\\^lib%s.so \\\\/;}'"
481 " dlls/Makefile.in| tr ^ \\\\t >t.tmp\n"
482 "sed '/SUBDIRS =/{G;s/$/\\^%s \\\\/;}' t.tmp | tr ^ \\\\t >t.tmp2"
483 "\nsed '/Map library name /{G;s/$/^\\$(RM) \\$\\@ \\&\\& \\$\\"
484 "(LN_S\\) %s\\/lib%s.\\$(LIBEXT) \\$\\@/;}' t.tmp2 | tr ^ \\\\t"
485 " > t.tmp\nsed '/Map library name /{G;s/$/lib%s.\\$(LIBEXT): "
486 "%s\\/lib%s.\\$(LIBEXT)/;}' t.tmp > t.tmp2\nsed '/dll "
487 "dependencies/{G;s/$/^\\@cd %s \\&\\& \\$(MAKE) lib%s.\\$(LIBEXT)"
488 "/;}' t.tmp2 | tr ^ \\\\t > t.tmp\nsed '/dll "
489 "dependencies/{G;s/$/%s\\/lib%s.\\$(LIBEXT)\\: libkernel32."
490 "\\$(LIBEXT) libntdll.\\$(LIBEXT)/;}' t.tmp > t.tmp2\n"
491 "mv -f t.tmp2 dlls/Makefile.in\nrm -f t.tmp\necho Patched dlls/"
492 "Makefile.in\n\necho\necho ...done.\necho Run \\'autoconf\\', "
493 "\\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
494 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
495 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
496 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
497 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
498 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
,
499 OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
, OUTPUT_DLL_NAME
);
501 fclose (install_file
);
502 snprintf (cmd
, sizeof (cmd
), "chmod a+x %s_install", OUTPUT_DLL_NAME
);
507 /*******************************************************************
510 * Write a C prototype for a parsed symbol
512 void output_prototype (FILE *file
, const parsed_symbol
*sym
)
514 unsigned int i
, start
= sym
->flags
& SYM_THISCALL
? 1 : 0;
516 fprintf (file
, "%s __%s %s_%s(", sym
->return_text
, symbol_get_call_convention(sym
),
517 OUTPUT_UC_DLL_NAME
, sym
->function_name
);
519 if (!sym
->argc
|| (sym
->argc
== 1 && sym
->flags
& SYM_THISCALL
))
520 fputs ("void", file
);
522 for (i
= start
; i
< sym
->argc
; i
++)
523 fprintf (file
, "%s%s %s", i
> start
? ", " : "", sym
->arg_text
[i
],
526 fputs (", ...", file
);
531 /*******************************************************************
534 * Write a function banner to the .c file
536 void output_c_banner (const parsed_symbol
*sym
)
541 if (sym
->ordinal
>= 0)
542 snprintf(ord_spec
, sizeof (ord_spec
), "%d", sym
->ordinal
);
549 fprintf (cfile
, "/*********************************************************"
550 "*********\n *\t\t%s (%s.%s)\n *\n", sym
->symbol
,
551 OUTPUT_UC_DLL_NAME
, ord_spec
);
553 if (globals
.do_documentation
&& sym
->function_name
)
555 fputs (" *\n * PARAMS\n *\n", cfile
);
558 fputs (" * None.\n *\n", cfile
);
561 for (i
= 0; i
< sym
->argc
; i
++)
562 fprintf (cfile
, " * %s [%s]%s\n", sym
->arg_name
[i
],
563 get_in_or_out(sym
, i
),
564 strcmp (sym
->arg_name
[i
], "_this") ? "" :
565 " Pointer to the class object (in ECX)");
568 fputs (" * ...[I]\n", cfile
);
569 fputs (" *\n", cfile
);
572 fputs (" * RETURNS\n *\n", cfile
);
574 if (sym
->return_text
&& !strcmp (sym
->return_text
, "void"))
575 fputs (" * Nothing.\n", cfile
);
577 fprintf (cfile
, " * %s\n", sym
->return_text
);
579 fputs (" *\n */\n", cfile
);
583 /*******************************************************************
586 * Get a string containing the correct format string for a type
588 static const char *get_format_str (int type
)
592 case ARG_VOID
: return "void";
593 case ARG_FLOAT
: return "%f";
594 case ARG_DOUBLE
: return "%g";
595 case ARG_POINTER
: return "%p";
596 case ARG_WIDE_STRING
:
597 case ARG_STRING
: return "%s";
598 case ARG_LONG
: return "%ld";
599 case ARG_STRUCT
: return "struct";
606 /*******************************************************************
609 * Determine if a parameter is In or In/Out
611 static const char *get_in_or_out (const parsed_symbol
*sym
, size_t arg
)
613 assert (sym
&& arg
< sym
->argc
);
614 assert (globals
.do_documentation
);
616 if (sym
->arg_flag
[arg
] & CT_CONST
)
619 switch (sym
->arg_type
[arg
])
624 case ARG_STRUCT
: return "In";
626 case ARG_WIDE_STRING
:
627 case ARG_STRING
: return "In/Out";