sane: Fix memory leak in create_item (Coverity).
[wine.git] / tools / winedump / output.c
blobb039f5d71ad6a485fa83916e9192585d4896aeb5
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include "winedump.h"
25 /* Output files */
26 static FILE *specfile = NULL;
27 static FILE *hfile = NULL;
28 static FILE *cfile = NULL;
30 static void output_spec_postamble (void);
31 static void output_header_postamble (void);
32 static void output_c_postamble (void);
33 static void output_c_banner (const parsed_symbol *sym);
34 static const char *get_format_str (int type);
35 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg);
38 /*******************************************************************
39 * output_spec_preamble
41 * Write the first part of the .spec file
43 void output_spec_preamble (void)
45 specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
47 atexit (output_spec_postamble);
49 if (VERBOSE)
50 puts ("Creating .spec preamble");
52 fprintf (specfile,
53 "# Generated from %s by winedump\n\n", globals.input_name);
57 /*******************************************************************
58 * output_spec_symbol
60 * Write a symbol to the .spec file
62 void output_spec_symbol (const parsed_symbol *sym)
64 char ord_spec[20];
66 assert (specfile);
67 assert (sym && sym->symbol);
69 if (sym->ordinal >= 0)
70 sprintf(ord_spec, "%d", sym->ordinal);
71 else
73 ord_spec[0] = '@';
74 ord_spec[1] = '\0';
76 if (sym->flags & SYM_THISCALL)
77 strcat (ord_spec, " -i386"); /* For binary compatibility only */
79 if (!globals.do_code || !sym->function_name)
81 if (sym->flags & SYM_DATA)
83 if (globals.forward_dll)
84 fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
85 globals.forward_dll, sym->symbol);
87 fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
88 sym->arg_name[0]);
89 return;
92 if (globals.forward_dll)
93 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
94 globals.forward_dll, sym->symbol);
95 else
96 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
98 else
100 unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
102 fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
103 symbol_get_call_convention(sym), sym->symbol);
105 for (; i < sym->argc; i++)
106 fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
108 if (sym->argc)
109 fputc (' ', specfile);
110 fputs (") ", specfile);
112 if (sym->flags & SYM_THISCALL)
113 fputs ("__thiscall_", specfile);
114 fprintf (specfile, "%s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
116 fputc ('\n',specfile);
121 /*******************************************************************
122 * output_spec_postamble
124 * Write the last part of the .spec file
126 static void output_spec_postamble (void)
128 if (specfile)
129 fclose (specfile);
130 specfile = NULL;
134 /*******************************************************************
135 * output_header_preamble
137 * Write the first part of the .h file
139 void output_header_preamble (void)
141 if (!globals.do_code)
142 return;
144 hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
146 atexit (output_header_postamble);
148 fprintf (hfile,
149 "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
150 " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n *\n */"
151 "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
152 "#include \"windef.h\"\n#include \"wine/debug.h\"\n"
153 "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
154 OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
155 OUTPUT_UC_DLL_NAME);
159 /*******************************************************************
160 * output_header_symbol
162 * Write a symbol to the .h file
164 void output_header_symbol (const parsed_symbol *sym)
166 if (!globals.do_code)
167 return;
169 assert (hfile);
170 assert (sym && sym->symbol);
172 if (!globals.do_code)
173 return;
175 if (sym->flags & SYM_DATA)
176 return;
178 if (!sym->function_name)
179 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
180 OUTPUT_UC_DLL_NAME, sym->symbol);
181 else
183 output_prototype (hfile, sym);
184 fputs (";\n", hfile);
189 /*******************************************************************
190 * output_header_postamble
192 * Write the last part of the .h file
194 static void output_header_postamble (void)
196 if (hfile)
198 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
199 OUTPUT_UC_DLL_NAME);
200 fclose (hfile);
201 hfile = NULL;
206 /*******************************************************************
207 * output_c_preamble
209 * Write the first part of the .c file
211 void output_c_preamble (void)
213 cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
215 atexit (output_c_postamble);
217 fprintf (cfile,
218 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
219 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n *\n */"
220 "\n\n#include \"config.h\"\n\n#include <stdarg.h>\n\n"
221 "#include \"windef.h\"\n#include \"winbase.h\"\n",
222 OUTPUT_DLL_NAME, globals.input_name);
224 if (globals.do_code)
225 fprintf (cfile, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME);
227 fprintf (cfile,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
228 OUTPUT_DLL_NAME);
230 if (globals.forward_dll)
232 if (VERBOSE)
233 puts ("Creating a forwarding DLL");
235 fputs ("\nHMODULE hDLL=0; /* DLL to call */\n\n", cfile);
237 fprintf (cfile,
238 "BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)\n{\n"
239 " TRACE(\"(%%p, %%u, %%p)\\n\", instance, reason, reserved);\n\n"
240 " switch (reason)\n"
241 " {\n"
242 " case DLL_PROCESS_ATTACH:\n"
243 " hDLL = LoadLibraryA(\"%s\");\n"
244 " TRACE(\"Forwarding DLL (%s) loaded (%%p)\\n\", hDLL);\n"
245 " DisableThreadLibraryCalls(instance);\n"
246 " break;\n"
247 " case DLL_PROCESS_DETACH:\n"
248 " FreeLibrary(hDLL);\n"
249 " TRACE(\"Forwarding DLL (%s) freed\\n\");\n"
250 " break;\n"
251 " }\n\n"
252 " return TRUE;\n}\n",
253 globals.forward_dll, globals.forward_dll, globals.forward_dll);
258 /*******************************************************************
259 * output_c_symbol
261 * Write a symbol to the .c file
263 void output_c_symbol (const parsed_symbol *sym)
265 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
266 BOOL is_void;
267 static BOOL has_thiscall = FALSE;
269 assert (cfile);
270 assert (sym && sym->symbol);
272 if (!globals.do_code)
273 return;
275 if (sym->flags & SYM_DATA)
277 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
278 sym->arg_text[0]);
279 return;
282 if (sym->flags & SYM_THISCALL && !has_thiscall)
284 fputs ("#ifdef __i386__ /* thiscall functions are i386-specific */\n\n"
285 "#define THISCALL(func) __thiscall_ ## func\n"
286 "#define THISCALL_NAME(func) __ASM_NAME(\"__thiscall_\" #func)\n"
287 "#define DEFINE_THISCALL_WRAPPER(func) \\\n"
288 "\textern void THISCALL(func)(); \\\n"
289 "\t__ASM_GLOBAL_FUNC(__thiscall_ ## func, \\\n"
290 "\t\t\t\"popl %eax\\n\\t\" \\\n"
291 "\t\t\t\"pushl %ecx\\n\\t\" \\\n"
292 "\t\t\t\"pushl %eax\\n\\t\" \\\n"
293 "\t\t\t\"jmp \" __ASM_NAME(#func) )\n"
294 "#else /* __i386__ */\n\n"
295 "#define THISCALL(func) func\n"
296 "#define THISCALL_NAME(func) __ASM_NAME(#func)\n"
297 "#define DEFINE_THISCALL_WRAPPER(func) /* nothing */\n\n"
298 "#endif /* __i386__ */\n\n", cfile);
299 has_thiscall = TRUE;
302 output_c_banner(sym);
304 if (sym->flags & SYM_THISCALL)
305 fprintf(cfile, "DEFINE_THISCALL_WRAPPER(%s)\n", sym->function_name);
307 if (!sym->function_name)
309 /* #ifdef'd dummy */
310 fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
311 symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
312 globals.forward_dll ? "@forward" : "@stub");
313 return;
316 is_void = !strcasecmp (sym->return_text, "void");
318 output_prototype (cfile, sym);
319 fputs ("\n{\n", cfile);
321 if (!globals.do_trace)
323 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
324 if (!is_void)
325 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
326 fputs ("}\n", cfile);
327 return;
330 /* Tracing, maybe forwarding as well */
331 if (globals.forward_dll)
333 /* Write variables for calling */
334 if (sym->varargs)
335 fputs("\tva_list valist;\n", cfile);
337 fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
338 symbol_get_call_convention(sym));
340 for (i = start; i < sym->argc; i++)
341 fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
343 fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
344 sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
346 if (!is_void)
347 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
349 fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
350 sym->symbol);
353 /* TRACE input arguments */
354 fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
356 for (i = 0; i < sym->argc; i++)
357 fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
358 get_format_str (sym->arg_type [i]));
360 fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
361 globals.forward_dll ? "forward" : "stub");
363 for (i = 0; i < sym->argc; i++)
364 if (sym->arg_type[i] != ARG_STRUCT)
365 fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
366 sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
367 sym->arg_name[i],
368 sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
370 fputs (");\n", cfile);
372 if (!globals.forward_dll)
374 if (!is_void)
375 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
376 fputs ("}\n", cfile);
377 return;
380 /* Call the DLL */
381 if (sym->varargs)
382 fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
384 fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
386 for (i = 0; i < sym->argc; i++)
387 fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
389 fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
391 /* TRACE return value */
392 fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
393 get_format_str (sym->return_type));
395 if (!is_void)
397 if (sym->return_type == ARG_WIDE_STRING)
398 fputs (",debugstr_w(retVal)", cfile);
399 else
400 fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
401 sym->return_type == ARG_STRUCT ? "" : "retVal");
402 fputs (");\n\treturn retVal;\n", cfile);
404 else
405 fputs (");\n", cfile);
407 fputs ("}\n", cfile);
411 /*******************************************************************
412 * output_c_postamble
414 * Write the last part of the .c file
416 static void output_c_postamble (void)
418 if (cfile)
419 fclose (cfile);
420 cfile = NULL;
424 /*******************************************************************
425 * output_makefile
427 * Write a Wine compatible makefile.in
429 void output_makefile (void)
431 FILE *makefile = open_file ("Makefile", ".in", "w");
433 if (VERBOSE)
434 puts ("Creating makefile");
436 fprintf (makefile,
437 "# Generated from %s by winedump.\n"
438 "MODULE = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
440 if (globals.forward_dll)
441 fprintf (makefile, "IMPORTS = %s", globals.forward_dll);
443 fprintf (makefile, "\n\nC_SRCS = \\\n\t%s_main.c\n", OUTPUT_DLL_NAME);
445 if (globals.forward_dll)
446 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
448 fclose (makefile);
452 /*******************************************************************
453 * output_prototype
455 * Write a C prototype for a parsed symbol
457 void output_prototype (FILE *file, const parsed_symbol *sym)
459 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
461 fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
462 OUTPUT_UC_DLL_NAME, sym->function_name);
464 if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
465 fputs ("void", file);
466 else
467 for (i = start; i < sym->argc; i++)
468 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
469 sym->arg_name [i]);
470 if (sym->varargs)
471 fputs (", ...", file);
472 fputc (')', file);
476 /*******************************************************************
477 * output_c_banner
479 * Write a function banner to the .c file
481 void output_c_banner (const parsed_symbol *sym)
483 char ord_spec[16];
484 size_t i;
486 if (sym->ordinal >= 0)
487 sprintf(ord_spec, "%d", sym->ordinal);
488 else
490 ord_spec[0] = '@';
491 ord_spec[1] = '\0';
494 fprintf (cfile, "/*********************************************************"
495 "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
496 OUTPUT_UC_DLL_NAME, ord_spec);
498 if (globals.do_documentation && sym->function_name)
500 fputs (" *\n * PARAMS\n *\n", cfile);
502 if (!sym->argc)
503 fputs (" * None.\n *\n", cfile);
504 else
506 for (i = 0; i < sym->argc; i++)
507 fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
508 get_in_or_out(sym, i),
509 strcmp (sym->arg_name [i], "_this") ? "" :
510 " Pointer to the class object (in ECX)");
512 if (sym->varargs)
513 fputs (" * ...[I]\n", cfile);
514 fputs (" *\n", cfile);
517 fputs (" * RETURNS\n *\n", cfile);
519 if (sym->return_text && !strcmp (sym->return_text, "void"))
520 fputs (" * Nothing.\n", cfile);
521 else
522 fprintf (cfile, " * %s\n", sym->return_text);
524 fputs (" *\n */\n", cfile);
528 /*******************************************************************
529 * get_format_str
531 * Get a string containing the correct format string for a type
533 static const char *get_format_str (int type)
535 switch (type)
537 case ARG_VOID: return "void";
538 case ARG_FLOAT: return "%f";
539 case ARG_DOUBLE: return "%g";
540 case ARG_POINTER: return "%p";
541 case ARG_WIDE_STRING:
542 case ARG_STRING: return "%s";
543 case ARG_LONG: return "%ld";
544 case ARG_STRUCT: return "struct";
546 assert (0);
547 return "";
551 /*******************************************************************
552 * get_in_or_out
554 * Determine if a parameter is In or In/Out
556 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
558 assert (sym && arg < sym->argc);
559 assert (globals.do_documentation);
561 if (sym->arg_flag [arg] & CT_CONST)
562 return "In";
564 switch (sym->arg_type [arg])
566 case ARG_FLOAT:
567 case ARG_DOUBLE:
568 case ARG_LONG:
569 case ARG_STRUCT: return "In";
570 case ARG_POINTER:
571 case ARG_WIDE_STRING:
572 case ARG_STRING: return "In/Out";
574 assert (0);
575 return "";