Make the testing framework thread safe.
[wine/gsoc_dplay.git] / tools / winedump / output.c
blob2367d3c7c8cfa59d2e7ec80a32f77b488e3055b6
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
20 #include "winedump.h"
22 /* Output files */
23 static FILE *specfile = NULL;
24 static FILE *hfile = NULL;
25 static FILE *cfile = NULL;
27 static void output_spec_postamble (void);
28 static void output_header_postamble (void);
29 static void output_c_postamble (void);
30 static void output_c_banner (const parsed_symbol *sym);
31 static const char *get_format_str (int type);
32 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
35 /*******************************************************************
36 * output_spec_preamble
38 * Write the first part of the .spec file
40 void output_spec_preamble (void)
42 specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
44 atexit (output_spec_postamble);
46 if (VERBOSE)
47 puts ("Creating .spec preamble");
49 fprintf (specfile,
50 "# Generated from %s by winedump\nname %s\n"
51 "type win32\ninit %s_Init\n\nimport kernel32.dll\n"
52 "import ntdll.dll\n", globals.input_name, OUTPUT_DLL_NAME,
53 OUTPUT_UC_DLL_NAME);
55 if (globals.forward_dll)
56 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
58 fprintf (specfile, "\n\ndebug_channels (%s)\n\n", OUTPUT_DLL_NAME);
62 /*******************************************************************
63 * output_spec_symbol
65 * Write a symbol to the .spec file
67 void output_spec_symbol (const parsed_symbol *sym)
69 char ord_spec[16];
71 assert (specfile);
72 assert (sym && sym->symbol);
74 if (sym->ordinal >= 0)
75 snprintf(ord_spec, 8, "%d", sym->ordinal);
76 else
78 ord_spec[0] = '@';
79 ord_spec[1] = '\0';
81 if (sym->flags & SYM_THISCALL)
82 strcat (ord_spec, " -i386"); /* For binary compatibility only */
84 if (!globals.do_code || !sym->function_name)
86 if (sym->flags & SYM_DATA)
88 if (globals.forward_dll)
89 fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
90 globals.forward_dll, sym->symbol);
92 fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
93 sym->arg_name[0]);
94 return;
97 if (globals.forward_dll)
98 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
99 globals.forward_dll, sym->symbol);
100 else
101 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
103 else
105 unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
107 fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
108 symbol_get_call_convention(sym), sym->symbol);
110 for (; i < sym->argc; i++)
111 fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
113 if (sym->argc)
114 fputc (' ', specfile);
115 fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
117 if (sym->flags & SYM_THISCALL)
118 fputs (" # __thiscall", specfile);
120 fputc ('\n',specfile);
125 /*******************************************************************
126 * output_spec_postamble
128 * Write the last part of the .spec file
130 static void output_spec_postamble (void)
132 if (specfile)
133 fclose (specfile);
134 specfile = NULL;
138 /*******************************************************************
139 * output_header_preamble
141 * Write the first part of the .h file
143 void output_header_preamble (void)
145 hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
147 atexit (output_header_postamble);
149 fprintf (hfile,
150 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
151 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
152 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n#include "
153 "\"config.h\"\n#include \"windef.h\"\n#include \"wine/debug.h\"\n"
154 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
155 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
156 OUTPUT_UC_DLL_NAME);
160 /*******************************************************************
161 * output_header_symbol
163 * Write a symbol to the .h file
165 void output_header_symbol (const parsed_symbol *sym)
167 assert (hfile);
168 assert (sym && sym->symbol);
170 if (!globals.do_code)
171 return;
173 if (sym->flags & SYM_DATA)
174 return;
176 if (!sym->function_name)
177 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
178 OUTPUT_UC_DLL_NAME, sym->symbol);
179 else
181 output_prototype (hfile, sym);
182 fputs (";\n", hfile);
187 /*******************************************************************
188 * output_header_postamble
190 * Write the last part of the .h file
192 static void output_header_postamble (void)
194 if (hfile)
196 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
197 OUTPUT_UC_DLL_NAME);
198 fclose (hfile);
199 hfile = NULL;
204 /*******************************************************************
205 * output_c_preamble
207 * Write the first part of the .c file
209 void output_c_preamble (void)
211 cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
213 atexit (output_c_postamble);
215 fprintf (cfile,
216 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
217 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
218 "\n\n#include \"%s_dll.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
219 OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
220 OUTPUT_DLL_NAME);
222 if (globals.forward_dll)
224 if (VERBOSE)
225 puts ("Creating a forwarding DLL");
227 fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
230 fputs ("#ifdef __i386__\n#define GET_THIS(t,p) t p;\\\n__asm__ __volatile__"
231 " (\"movl %%ecx, %0\" : \"=m\" (p))\n#endif\n\n\n", cfile);
233 fprintf (cfile,
234 "BOOL WINAPI %s_Init(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
235 "lpvReserved)\n{\n\tTRACE(\"(0x%%08x, %%ld, %%p)\\n\",hinstDLL,"
236 "fdwReason,lpvReserved);\n\n\t"
237 "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t",
238 OUTPUT_UC_DLL_NAME);
240 if (globals.forward_dll)
242 fprintf (cfile,
243 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
244 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
245 globals.forward_dll, globals.forward_dll);
247 else
248 fputs ("/* FIXME: Initialisation */", cfile);
250 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
251 cfile);
253 if (globals.forward_dll)
255 fprintf (cfile,
256 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
257 " freed\\n\");", globals.forward_dll);
259 else
260 fputs ("/* FIXME: Cleanup */", cfile);
262 fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
266 #define CPP_END if (sym->flags & SYM_THISCALL) \
267 fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
268 #define GET_THIS if (sym->flags & SYM_THISCALL) \
269 fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
271 /*******************************************************************
272 * output_c_symbol
274 * Write a symbol to the .c file
276 void output_c_symbol (const parsed_symbol *sym)
278 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
279 int is_void;
281 assert (cfile);
282 assert (sym && sym->symbol);
284 if (!globals.do_code)
285 return;
287 if (sym->flags & SYM_DATA)
289 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
290 sym->arg_text[0]);
291 return;
294 if (sym->flags & SYM_THISCALL)
295 fputs ("#ifdef __i386__\n", cfile);
297 output_c_banner(sym);
299 if (!sym->function_name)
301 /* #ifdef'd dummy */
302 fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
303 symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
304 globals.forward_dll ? "@forward" : "@stub");
305 CPP_END;
306 return;
309 is_void = !strcmp (sym->return_text, "void");
311 output_prototype (cfile, sym);
312 fputs ("\n{\n", cfile);
314 if (!globals.do_trace)
316 GET_THIS;
317 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
318 if (!is_void)
319 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
320 fputs ("}\n", cfile);
321 CPP_END;
322 return;
325 /* Tracing, maybe forwarding as well */
326 if (globals.forward_dll)
328 /* Write variables for calling */
329 if (sym->varargs)
330 fputs("\tva_list valist;\n", cfile);
332 fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
333 symbol_get_call_convention(sym));
335 for (i = start; i < sym->argc; i++)
336 fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
338 fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
339 sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
341 if (!is_void)
342 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
344 GET_THIS;
346 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
347 sym->symbol);
350 /* TRACE input arguments */
351 fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
353 for (i = 0; i < sym->argc; i++)
354 fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
355 get_format_str (sym->arg_type [i]));
357 fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
358 globals.forward_dll ? "forward" : "stub");
360 for (i = 0; i < sym->argc; i++)
361 if (sym->arg_type[i] != ARG_STRUCT)
362 fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
363 sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
364 sym->arg_name[i],
365 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
367 fputs (");\n", cfile);
369 if (!globals.forward_dll)
371 if (!is_void)
372 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
373 fputs ("}\n", cfile);
374 CPP_END;
375 return;
378 /* Call the DLL */
379 if (sym->varargs)
380 fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
382 fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
384 for (i = 0; i < sym->argc; i++)
385 fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
387 fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
389 /* TRACE return value */
390 fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
391 get_format_str (sym->return_type));
393 if (!is_void)
395 if (sym->return_type == ARG_WIDE_STRING)
396 fputs (",debugstr_w(retVal)", cfile);
397 else
398 fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
399 sym->return_type == ARG_STRUCT ? "" : "retVal");
400 fputs (");\n\treturn retVal;\n", cfile);
402 else
403 fputs (");\n", cfile);
405 fputs ("}\n", cfile);
406 CPP_END;
410 /*******************************************************************
411 * output_c_postamble
413 * Write the last part of the .c file
415 static void output_c_postamble (void)
417 if (cfile)
418 fclose (cfile);
419 cfile = NULL;
423 /*******************************************************************
424 * output_makefile
426 * Write a Wine compatible makefile.in
428 void output_makefile (void)
430 FILE *makefile = open_file ("Makefile", ".in", "w");
432 if (VERBOSE)
433 puts ("Creating makefile");
435 fprintf (makefile,
436 "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
437 "TOPOBJDIR = ../..\nSRCDIR = @srcdir@\nVPATH = @srcdir@\n"
438 "MODULE = %s\nEXTRALIBS = $(LIBUNICODE)\n\n"
439 "LDDLLFLAGS = @LDDLLFLAGS@\nSYMBOLFILE = $(MODULE).tmp.o\n\n"
440 "C_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n### Dependencies:",
441 globals.input_name, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
443 fclose (makefile);
447 /*******************************************************************
448 * output_install_script
450 * Write a script to insert the DLL into Wine
452 * Rather than using diff/patch, several sed calls are generated
453 * so the script can be re-run at any time without breaking.
455 void output_install_script (void)
457 char cmd[128];
458 FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
460 if (VERBOSE)
461 puts ("Creating install script");
463 fprintf (install_file,
464 "#!/bin/bash\n# Generated from %s.dll by winedump.\n\n"
465 "if [ $# -ne 1 ] || [ ! -d $1 ] || [ ! -f"
466 " $1/AUTHORS ]; then\n\t[ $# -eq 1 ] && echo \"Invalid path\"\n"
467 "\techo \"Usage: $0 wine-base-dir\"\n\texit 1\nfi\n\n"
468 "if [ -d $1/dlls/%s ]; then\n\techo \"DLL is already present\"\n"
469 "\texit 1\nfi\n\necho Adding DLL %s to Wine build tree...\n"
470 "echo\n\nmkdir $1/dlls/%s\ncp %s.spec $1/dlls/%s\n"
471 "cp %s_main.c $1/dlls/%s\ncp %s_dll.h $1/dlls/%s\n"
472 "cp Makefile.in $1/dlls/%s/Makefile.in\necho Copied DLL files\n\n"
473 "cd $1\n\nsed '/dlls\\/"
474 "x11drv\\/Makefile/{G;s/$/dlls\\/%s\\/Makefile/;}' configure.in"
475 " >t.tmp\nmv -f t.tmp configure.in\necho Patched configure.in\n\n"
476 "sed '/all:/{G;s/$/\\^lib%s.so \\\\/;}'"
477 " dlls/Makefile.in| tr ^ \\\\t >t.tmp\n"
478 "sed '/SUBDIRS =/{G;s/$/\\^%s \\\\/;}' t.tmp | tr ^ \\\\t >t.tmp2"
479 "\nsed '/Map library name /{G;s/$/^\\$(RM) \\$\\@ \\&\\& \\$\\"
480 "(LN_S\\) %s\\/lib%s.\\$(LIBEXT) \\$\\@/;}' t.tmp2 | tr ^ \\\\t"
481 " > t.tmp\nsed '/Map library name /{G;s/$/lib%s.\\$(LIBEXT): "
482 "%s\\/lib%s.\\$(LIBEXT)/;}' t.tmp > t.tmp2\nsed '/dll "
483 "dependencies/{G;s/$/^\\@cd %s \\&\\& \\$(MAKE) lib%s.\\$(LIBEXT)"
484 "/;}' t.tmp2 | tr ^ \\\\t > t.tmp\nsed '/dll "
485 "dependencies/{G;s/$/%s\\/lib%s.\\$(LIBEXT)\\: libkernel32."
486 "\\$(LIBEXT) libntdll.\\$(LIBEXT)/;}' t.tmp > t.tmp2\n"
487 "mv -f t.tmp2 dlls/Makefile.in\nrm -f t.tmp\necho Patched dlls/"
488 "Makefile.in\n\necho\necho ...done.\necho Run \\'autoconf\\', "
489 "\\'./configure\\' then \\'make\\' to rebuild Wine\n\n",
490 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
491 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
492 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
493 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
494 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME,
495 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_DLL_NAME);
497 fclose (install_file);
498 snprintf (cmd, sizeof (cmd), "chmod a+x %s_install", OUTPUT_DLL_NAME);
499 system (cmd);
503 /*******************************************************************
504 * output_prototype
506 * Write a C prototype for a parsed symbol
508 void output_prototype (FILE *file, const parsed_symbol *sym)
510 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
512 fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
513 OUTPUT_UC_DLL_NAME, sym->function_name);
515 if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
516 fputs ("void", file);
517 else
518 for (i = start; i < sym->argc; i++)
519 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
520 sym->arg_name [i]);
521 if (sym->varargs)
522 fputs (", ...", file);
523 fputc (')', file);
527 /*******************************************************************
528 * output_c_banner
530 * Write a function banner to the .c file
532 void output_c_banner (const parsed_symbol *sym)
534 char ord_spec[16];
535 size_t i;
537 if (sym->ordinal >= 0)
538 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
539 else
541 ord_spec[0] = '@';
542 ord_spec[1] = '\0';
545 fprintf (cfile, "/*********************************************************"
546 "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
547 OUTPUT_UC_DLL_NAME, ord_spec);
549 if (globals.do_documentation && sym->function_name)
551 fputs (" *\n * PARAMS\n *\n", cfile);
553 if (!sym->argc)
554 fputs (" * None.\n *\n", cfile);
555 else
557 for (i = 0; i < sym->argc; i++)
558 fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
559 get_in_or_out(sym, i),
560 strcmp (sym->arg_name [i], "_this") ? "" :
561 " Pointer to the class object (in ECX)");
563 if (sym->varargs)
564 fputs (" * ...[I]\n", cfile);
565 fputs (" *\n", cfile);
568 fputs (" * RETURNS\n *\n", cfile);
570 if (sym->return_text && !strcmp (sym->return_text, "void"))
571 fputs (" * Nothing.\n", cfile);
572 else
573 fprintf (cfile, " * %s\n", sym->return_text);
575 fputs (" *\n */\n", cfile);
579 /*******************************************************************
580 * get_format_str
582 * Get a string containing the correct format string for a type
584 static const char *get_format_str (int type)
586 switch (type)
588 case ARG_VOID: return "void";
589 case ARG_FLOAT: return "%f";
590 case ARG_DOUBLE: return "%g";
591 case ARG_POINTER: return "%p";
592 case ARG_WIDE_STRING:
593 case ARG_STRING: return "%s";
594 case ARG_LONG: return "%ld";
595 case ARG_STRUCT: return "struct";
597 assert (0);
598 return "";
602 /*******************************************************************
603 * get_in_or_out
605 * Determine if a parameter is In or In/Out
607 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
609 assert (sym && arg < sym->argc);
610 assert (globals.do_documentation);
612 if (sym->arg_flag [arg] & CT_CONST)
613 return "In";
615 switch (sym->arg_type [arg])
617 case ARG_FLOAT:
618 case ARG_DOUBLE:
619 case ARG_LONG:
620 case ARG_STRUCT: return "In";
621 case ARG_POINTER:
622 case ARG_WIDE_STRING:
623 case ARG_STRING: return "In/Out";
625 assert (0);
626 return "";