Added ability to turn on/off debug channels.
[wine.git] / tools / winedump / output.c
blobed860c9d1d459675f7b4b252ddaf74422984b4ef
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include "winedump.h"
26 /* Output files */
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);
50 if (VERBOSE)
51 puts ("Creating .spec preamble");
53 fprintf (specfile,
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,
57 OUTPUT_UC_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 /*******************************************************************
67 * output_spec_symbol
69 * Write a symbol to the .spec file
71 void output_spec_symbol (const parsed_symbol *sym)
73 char ord_spec[16];
75 assert (specfile);
76 assert (sym && sym->symbol);
78 if (sym->ordinal >= 0)
79 snprintf(ord_spec, 8, "%d", sym->ordinal);
80 else
82 ord_spec[0] = '@';
83 ord_spec[1] = '\0';
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,
97 sym->arg_name[0]);
98 return;
101 if (globals.forward_dll)
102 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
103 globals.forward_dll, sym->symbol);
104 else
105 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
107 else
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));
117 if (sym->argc)
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)
136 if (specfile)
137 fclose (specfile);
138 specfile = NULL;
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);
153 fprintf (hfile,
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,
160 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)
171 assert (hfile);
172 assert (sym && sym->symbol);
174 if (!globals.do_code)
175 return;
177 if (sym->flags & SYM_DATA)
178 return;
180 if (!sym->function_name)
181 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
182 OUTPUT_UC_DLL_NAME, sym->symbol);
183 else
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)
198 if (hfile)
200 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
201 OUTPUT_UC_DLL_NAME);
202 fclose (hfile);
203 hfile = NULL;
208 /*******************************************************************
209 * output_c_preamble
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);
219 fprintf (cfile,
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,
224 OUTPUT_DLL_NAME);
226 if (globals.forward_dll)
228 if (VERBOSE)
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);
237 fprintf (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",
242 OUTPUT_UC_DLL_NAME);
244 if (globals.forward_dll)
246 fprintf (cfile,
247 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
248 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
249 globals.forward_dll, globals.forward_dll);
251 else
252 fputs ("/* FIXME: Initialisation */", cfile);
254 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
255 cfile);
257 if (globals.forward_dll)
259 fprintf (cfile,
260 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
261 " freed\\n\");", globals.forward_dll);
263 else
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 /*******************************************************************
276 * output_c_symbol
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;
283 int is_void;
285 assert (cfile);
286 assert (sym && sym->symbol);
288 if (!globals.do_code)
289 return;
291 if (sym->flags & SYM_DATA)
293 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
294 sym->arg_text[0]);
295 return;
298 if (sym->flags & SYM_THISCALL)
299 fputs ("#ifdef __i386__\n", cfile);
301 output_c_banner(sym);
303 if (!sym->function_name)
305 /* #ifdef'd dummy */
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");
309 CPP_END;
310 return;
313 is_void = !strcmp (sym->return_text, "void");
315 output_prototype (cfile, sym);
316 fputs ("\n{\n", cfile);
318 if (!globals.do_trace)
320 GET_THIS;
321 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
322 if (!is_void)
323 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
324 fputs ("}\n", cfile);
325 CPP_END;
326 return;
329 /* Tracing, maybe forwarding as well */
330 if (globals.forward_dll)
332 /* Write variables for calling */
333 if (sym->varargs)
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");
345 if (!is_void)
346 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
348 GET_THIS;
350 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
351 sym->symbol);
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(" : "",
368 sym->arg_name[i],
369 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
371 fputs (");\n", cfile);
373 if (!globals.forward_dll)
375 if (!is_void)
376 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
377 fputs ("}\n", cfile);
378 CPP_END;
379 return;
382 /* Call the DLL */
383 if (sym->varargs)
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));
397 if (!is_void)
399 if (sym->return_type == ARG_WIDE_STRING)
400 fputs (",debugstr_w(retVal)", cfile);
401 else
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);
406 else
407 fputs (");\n", cfile);
409 fputs ("}\n", cfile);
410 CPP_END;
414 /*******************************************************************
415 * output_c_postamble
417 * Write the last part of the .c file
419 static void output_c_postamble (void)
421 if (cfile)
422 fclose (cfile);
423 cfile = NULL;
427 /*******************************************************************
428 * output_makefile
430 * Write a Wine compatible makefile.in
432 void output_makefile (void)
434 FILE *makefile = open_file ("Makefile", ".in", "w");
436 if (VERBOSE)
437 puts ("Creating makefile");
439 fprintf (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);
447 fclose (makefile);
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)
461 char cmd[128];
462 FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
464 if (VERBOSE)
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);
503 system (cmd);
507 /*******************************************************************
508 * output_prototype
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);
521 else
522 for (i = start; i < sym->argc; i++)
523 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
524 sym->arg_name [i]);
525 if (sym->varargs)
526 fputs (", ...", file);
527 fputc (')', file);
531 /*******************************************************************
532 * output_c_banner
534 * Write a function banner to the .c file
536 void output_c_banner (const parsed_symbol *sym)
538 char ord_spec[16];
539 size_t i;
541 if (sym->ordinal >= 0)
542 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
543 else
545 ord_spec[0] = '@';
546 ord_spec[1] = '\0';
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);
557 if (!sym->argc)
558 fputs (" * None.\n *\n", cfile);
559 else
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)");
567 if (sym->varargs)
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);
576 else
577 fprintf (cfile, " * %s\n", sym->return_text);
579 fputs (" *\n */\n", cfile);
583 /*******************************************************************
584 * get_format_str
586 * Get a string containing the correct format string for a type
588 static const char *get_format_str (int type)
590 switch (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";
601 assert (0);
602 return "";
606 /*******************************************************************
607 * get_in_or_out
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)
617 return "In";
619 switch (sym->arg_type [arg])
621 case ARG_FLOAT:
622 case ARG_DOUBLE:
623 case ARG_LONG:
624 case ARG_STRUCT: return "In";
625 case ARG_POINTER:
626 case ARG_WIDE_STRING:
627 case ARG_STRING: return "In/Out";
629 assert (0);
630 return "";