Proper (type wise) return types.
[wine/multimedia.git] / tools / winedump / output.c
blob226baa5236a68c47b6589e98f4eb45b4f04ede28
1 /*
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
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\ninit %s_Init\n\n",
55 globals.input_name, OUTPUT_UC_DLL_NAME);
59 /*******************************************************************
60 * output_spec_symbol
62 * Write a symbol to the .spec file
64 void output_spec_symbol (const parsed_symbol *sym)
66 char ord_spec[16];
68 assert (specfile);
69 assert (sym && sym->symbol);
71 if (sym->ordinal >= 0)
72 snprintf(ord_spec, 8, "%d", sym->ordinal);
73 else
75 ord_spec[0] = '@';
76 ord_spec[1] = '\0';
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,
90 sym->arg_name[0]);
91 return;
94 if (globals.forward_dll)
95 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
96 globals.forward_dll, sym->symbol);
97 else
98 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
100 else
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));
110 if (sym->argc)
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)
129 if (specfile)
130 fclose (specfile);
131 specfile = NULL;
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);
146 fprintf (hfile,
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,
153 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)
164 assert (hfile);
165 assert (sym && sym->symbol);
167 if (!globals.do_code)
168 return;
170 if (sym->flags & SYM_DATA)
171 return;
173 if (!sym->function_name)
174 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
175 OUTPUT_UC_DLL_NAME, sym->symbol);
176 else
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)
191 if (hfile)
193 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
194 OUTPUT_UC_DLL_NAME);
195 fclose (hfile);
196 hfile = NULL;
201 /*******************************************************************
202 * output_c_preamble
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);
212 fprintf (cfile,
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,
217 OUTPUT_DLL_NAME);
219 if (globals.forward_dll)
221 if (VERBOSE)
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);
230 fprintf (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",
235 OUTPUT_UC_DLL_NAME);
237 if (globals.forward_dll)
239 fprintf (cfile,
240 "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
241 "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
242 globals.forward_dll, globals.forward_dll);
244 else
245 fputs ("/* FIXME: Initialisation */", cfile);
247 fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
248 cfile);
250 if (globals.forward_dll)
252 fprintf (cfile,
253 "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
254 " freed\\n\");", globals.forward_dll);
256 else
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 /*******************************************************************
269 * output_c_symbol
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;
276 int is_void;
278 assert (cfile);
279 assert (sym && sym->symbol);
281 if (!globals.do_code)
282 return;
284 if (sym->flags & SYM_DATA)
286 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
287 sym->arg_text[0]);
288 return;
291 if (sym->flags & SYM_THISCALL)
292 fputs ("#ifdef __i386__\n", cfile);
294 output_c_banner(sym);
296 if (!sym->function_name)
298 /* #ifdef'd dummy */
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");
302 CPP_END;
303 return;
306 is_void = !strcmp (sym->return_text, "void");
308 output_prototype (cfile, sym);
309 fputs ("\n{\n", cfile);
311 if (!globals.do_trace)
313 GET_THIS;
314 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
315 if (!is_void)
316 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
317 fputs ("}\n", cfile);
318 CPP_END;
319 return;
322 /* Tracing, maybe forwarding as well */
323 if (globals.forward_dll)
325 /* Write variables for calling */
326 if (sym->varargs)
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");
338 if (!is_void)
339 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
341 GET_THIS;
343 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
344 sym->symbol);
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(" : "",
361 sym->arg_name[i],
362 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
364 fputs (");\n", cfile);
366 if (!globals.forward_dll)
368 if (!is_void)
369 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
370 fputs ("}\n", cfile);
371 CPP_END;
372 return;
375 /* Call the DLL */
376 if (sym->varargs)
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));
390 if (!is_void)
392 if (sym->return_type == ARG_WIDE_STRING)
393 fputs (",debugstr_w(retVal)", cfile);
394 else
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);
399 else
400 fputs (");\n", cfile);
402 fputs ("}\n", cfile);
403 CPP_END;
407 /*******************************************************************
408 * output_c_postamble
410 * Write the last part of the .c file
412 static void output_c_postamble (void)
414 if (cfile)
415 fclose (cfile);
416 cfile = NULL;
420 /*******************************************************************
421 * output_makefile
423 * Write a Wine compatible makefile.in
425 void output_makefile (void)
427 FILE *makefile = open_file ("Makefile", ".in", "w");
429 if (VERBOSE)
430 puts ("Creating makefile");
432 fprintf (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);
441 fprintf (makefile,
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:",
445 OUTPUT_DLL_NAME);
447 if (globals.forward_dll)
448 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
450 fclose (makefile);
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)
464 char cmd[128];
465 FILE *install_file = open_file (OUTPUT_DLL_NAME, "_install", "w");
467 if (VERBOSE)
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,
504 OUTPUT_DLL_NAME);
506 fclose (install_file);
507 snprintf (cmd, sizeof (cmd), "chmod a+x %s_install", OUTPUT_DLL_NAME);
508 system (cmd);
512 /*******************************************************************
513 * output_prototype
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);
526 else
527 for (i = start; i < sym->argc; i++)
528 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
529 sym->arg_name [i]);
530 if (sym->varargs)
531 fputs (", ...", file);
532 fputc (')', file);
536 /*******************************************************************
537 * output_c_banner
539 * Write a function banner to the .c file
541 void output_c_banner (const parsed_symbol *sym)
543 char ord_spec[16];
544 size_t i;
546 if (sym->ordinal >= 0)
547 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
548 else
550 ord_spec[0] = '@';
551 ord_spec[1] = '\0';
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);
562 if (!sym->argc)
563 fputs (" * None.\n *\n", cfile);
564 else
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)");
572 if (sym->varargs)
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);
581 else
582 fprintf (cfile, " * %s\n", sym->return_text);
584 fputs (" *\n */\n", cfile);
588 /*******************************************************************
589 * get_format_str
591 * Get a string containing the correct format string for a type
593 static const char *get_format_str (int type)
595 switch (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";
606 assert (0);
607 return "";
611 /*******************************************************************
612 * get_in_or_out
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)
622 return "In";
624 switch (sym->arg_type [arg])
626 case ARG_FLOAT:
627 case ARG_DOUBLE:
628 case ARG_LONG:
629 case ARG_STRUCT: return "In";
630 case ARG_POINTER:
631 case ARG_WIDE_STRING:
632 case ARG_STRING: return "In/Out";
634 assert (0);
635 return "";