winex11.drv: Map coordinates before calling send_mouse_input.
[wine/zf.git] / tools / winedump / output.c
blob5911f22f6c82bb6e8961302492c200f0c3ee91ba
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"
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\n\n", globals.input_name);
58 /*******************************************************************
59 * output_spec_symbol
61 * Write a symbol to the .spec file
63 void output_spec_symbol (const parsed_symbol *sym)
65 char ord_spec[20];
67 assert (specfile);
68 assert (sym && sym->symbol);
70 if (sym->ordinal >= 0)
71 snprintf(ord_spec, 12, "%d", sym->ordinal);
72 else
74 ord_spec[0] = '@';
75 ord_spec[1] = '\0';
77 if (sym->flags & SYM_THISCALL)
78 strcat (ord_spec, " -i386"); /* For binary compatibility only */
80 if (!globals.do_code || !sym->function_name)
82 if (sym->flags & SYM_DATA)
84 if (globals.forward_dll)
85 fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
86 globals.forward_dll, sym->symbol);
88 fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
89 sym->arg_name[0]);
90 return;
93 if (globals.forward_dll)
94 fprintf (specfile, "%s forward %s %s.%s\n", ord_spec, sym->symbol,
95 globals.forward_dll, sym->symbol);
96 else
97 fprintf (specfile, "%s stub %s\n", ord_spec, sym->symbol);
99 else
101 unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
103 fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
104 symbol_get_call_convention(sym), sym->symbol);
106 for (; i < sym->argc; i++)
107 fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
109 if (sym->argc)
110 fputc (' ', specfile);
111 fputs (") ", specfile);
113 if (sym->flags & SYM_THISCALL)
114 fputs ("__thiscall_", specfile);
115 fprintf (specfile, "%s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
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 if (!globals.do_code)
143 return;
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"
153 "#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 if (!globals.do_code)
168 return;
170 assert (hfile);
171 assert (sym && sym->symbol);
173 if (!globals.do_code)
174 return;
176 if (sym->flags & SYM_DATA)
177 return;
179 if (!sym->function_name)
180 fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
181 OUTPUT_UC_DLL_NAME, sym->symbol);
182 else
184 output_prototype (hfile, sym);
185 fputs (";\n", hfile);
190 /*******************************************************************
191 * output_header_postamble
193 * Write the last part of the .h file
195 static void output_header_postamble (void)
197 if (hfile)
199 fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
200 OUTPUT_UC_DLL_NAME);
201 fclose (hfile);
202 hfile = NULL;
207 /*******************************************************************
208 * output_c_preamble
210 * Write the first part of the .c file
212 void output_c_preamble (void)
214 cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
216 atexit (output_c_postamble);
218 fprintf (cfile,
219 "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
220 " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n *\n */"
221 "\n\n#include \"config.h\"\n\n#include <stdarg.h>\n\n"
222 "#include \"windef.h\"\n#include \"winbase.h\"\n",
223 OUTPUT_DLL_NAME, globals.input_name);
225 if (globals.do_code)
226 fprintf (cfile, "#include \"%s_dll.h\"\n", OUTPUT_DLL_NAME);
228 fprintf (cfile,"#include \"wine/debug.h\"\n\nWINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
229 OUTPUT_DLL_NAME);
231 if (globals.forward_dll)
233 if (VERBOSE)
234 puts ("Creating a forwarding DLL");
236 fputs ("\nHMODULE hDLL=0; /* DLL to call */\n\n", cfile);
238 fprintf (cfile,
239 "BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, void *reserved)\n{\n"
240 " TRACE(\"(%%p, %%u, %%p)\\n\", instance, reason, reserved);\n\n"
241 " switch (reason)\n"
242 " {\n"
243 " case DLL_PROCESS_ATTACH:\n"
244 " hDLL = LoadLibraryA(\"%s\");\n"
245 " TRACE(\"Forwarding DLL (%s) loaded (%%p)\\n\", hDLL);\n"
246 " DisableThreadLibraryCalls(instance);\n"
247 " break;\n"
248 " case DLL_PROCESS_DETACH:\n"
249 " FreeLibrary(hDLL);\n"
250 " TRACE(\"Forwarding DLL (%s) freed\\n\");\n"
251 " break;\n"
252 " }\n\n"
253 " return TRUE;\n}\n",
254 globals.forward_dll, globals.forward_dll, globals.forward_dll);
259 /*******************************************************************
260 * output_c_symbol
262 * Write a symbol to the .c file
264 void output_c_symbol (const parsed_symbol *sym)
266 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
267 BOOL is_void;
268 static BOOL has_thiscall = FALSE;
270 assert (cfile);
271 assert (sym && sym->symbol);
273 if (!globals.do_code)
274 return;
276 if (sym->flags & SYM_DATA)
278 fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
279 sym->arg_text[0]);
280 return;
283 if (sym->flags & SYM_THISCALL && !has_thiscall)
285 fputs ("#ifdef __i386__ /* thiscall functions are i386-specific */\n\n"
286 "#define THISCALL(func) __thiscall_ ## func\n"
287 "#define THISCALL_NAME(func) __ASM_NAME(\"__thiscall_\" #func)\n"
288 "#define DEFINE_THISCALL_WRAPPER(func) \\\n"
289 "\textern void THISCALL(func)(); \\\n"
290 "\t__ASM_GLOBAL_FUNC(__thiscall_ ## func, \\\n"
291 "\t\t\t\"popl %eax\\n\\t\" \\\n"
292 "\t\t\t\"pushl %ecx\\n\\t\" \\\n"
293 "\t\t\t\"pushl %eax\\n\\t\" \\\n"
294 "\t\t\t\"jmp \" __ASM_NAME(#func) )\n"
295 "#else /* __i386__ */\n\n"
296 "#define THISCALL(func) func\n"
297 "#define THISCALL_NAME(func) __ASM_NAME(#func)\n"
298 "#define DEFINE_THISCALL_WRAPPER(func) /* nothing */\n\n"
299 "#endif /* __i386__ */\n\n", cfile);
300 has_thiscall = TRUE;
303 output_c_banner(sym);
305 if (sym->flags & SYM_THISCALL)
306 fprintf(cfile, "DEFINE_THISCALL_WRAPPER(%s)\n", sym->function_name);
308 if (!sym->function_name)
310 /* #ifdef'd dummy */
311 fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
312 symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
313 globals.forward_dll ? "@forward" : "@stub");
314 return;
317 is_void = !strcasecmp (sym->return_text, "void");
319 output_prototype (cfile, sym);
320 fputs ("\n{\n", cfile);
322 if (!globals.do_trace)
324 fputs ("\tFIXME(\":stub\\n\");\n", cfile);
325 if (!is_void)
326 fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
327 fputs ("}\n", cfile);
328 return;
331 /* Tracing, maybe forwarding as well */
332 if (globals.forward_dll)
334 /* Write variables for calling */
335 if (sym->varargs)
336 fputs("\tva_list valist;\n", cfile);
338 fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
339 symbol_get_call_convention(sym));
341 for (i = start; i < sym->argc; i++)
342 fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
344 fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
345 sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
347 if (!is_void)
348 fprintf (cfile, "\t%s retVal;\n", sym->return_text);
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 return;
381 /* Call the DLL */
382 if (sym->varargs)
383 fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
385 fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
387 for (i = 0; i < sym->argc; i++)
388 fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
390 fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
392 /* TRACE return value */
393 fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
394 get_format_str (sym->return_type));
396 if (!is_void)
398 if (sym->return_type == ARG_WIDE_STRING)
399 fputs (",debugstr_w(retVal)", cfile);
400 else
401 fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
402 sym->return_type == ARG_STRUCT ? "" : "retVal");
403 fputs (");\n\treturn retVal;\n", cfile);
405 else
406 fputs (");\n", cfile);
408 fputs ("}\n", cfile);
412 /*******************************************************************
413 * output_c_postamble
415 * Write the last part of the .c file
417 static void output_c_postamble (void)
419 if (cfile)
420 fclose (cfile);
421 cfile = NULL;
425 /*******************************************************************
426 * output_makefile
428 * Write a Wine compatible makefile.in
430 void output_makefile (void)
432 FILE *makefile = open_file ("Makefile", ".in", "w");
434 if (VERBOSE)
435 puts ("Creating makefile");
437 fprintf (makefile,
438 "# Generated from %s by winedump.\n"
439 "MODULE = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
441 if (globals.forward_dll)
442 fprintf (makefile, "IMPORTS = %s", globals.forward_dll);
444 fprintf (makefile, "\n\nC_SRCS = \\\n\t%s_main.c\n", OUTPUT_DLL_NAME);
446 if (globals.forward_dll)
447 fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
449 fclose (makefile);
453 /*******************************************************************
454 * output_prototype
456 * Write a C prototype for a parsed symbol
458 void output_prototype (FILE *file, const parsed_symbol *sym)
460 unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
462 fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
463 OUTPUT_UC_DLL_NAME, sym->function_name);
465 if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
466 fputs ("void", file);
467 else
468 for (i = start; i < sym->argc; i++)
469 fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
470 sym->arg_name [i]);
471 if (sym->varargs)
472 fputs (", ...", file);
473 fputc (')', file);
477 /*******************************************************************
478 * output_c_banner
480 * Write a function banner to the .c file
482 void output_c_banner (const parsed_symbol *sym)
484 char ord_spec[16];
485 size_t i;
487 if (sym->ordinal >= 0)
488 snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
489 else
491 ord_spec[0] = '@';
492 ord_spec[1] = '\0';
495 fprintf (cfile, "/*********************************************************"
496 "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
497 OUTPUT_UC_DLL_NAME, ord_spec);
499 if (globals.do_documentation && sym->function_name)
501 fputs (" *\n * PARAMS\n *\n", cfile);
503 if (!sym->argc)
504 fputs (" * None.\n *\n", cfile);
505 else
507 for (i = 0; i < sym->argc; i++)
508 fprintf (cfile, " * %s [%s]%s\n", sym->arg_name [i],
509 get_in_or_out(sym, i),
510 strcmp (sym->arg_name [i], "_this") ? "" :
511 " Pointer to the class object (in ECX)");
513 if (sym->varargs)
514 fputs (" * ...[I]\n", cfile);
515 fputs (" *\n", cfile);
518 fputs (" * RETURNS\n *\n", cfile);
520 if (sym->return_text && !strcmp (sym->return_text, "void"))
521 fputs (" * Nothing.\n", cfile);
522 else
523 fprintf (cfile, " * %s\n", sym->return_text);
525 fputs (" *\n */\n", cfile);
529 /*******************************************************************
530 * get_format_str
532 * Get a string containing the correct format string for a type
534 static const char *get_format_str (int type)
536 switch (type)
538 case ARG_VOID: return "void";
539 case ARG_FLOAT: return "%f";
540 case ARG_DOUBLE: return "%g";
541 case ARG_POINTER: return "%p";
542 case ARG_WIDE_STRING:
543 case ARG_STRING: return "%s";
544 case ARG_LONG: return "%ld";
545 case ARG_STRUCT: return "struct";
547 assert (0);
548 return "";
552 /*******************************************************************
553 * get_in_or_out
555 * Determine if a parameter is In or In/Out
557 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
559 assert (sym && arg < sym->argc);
560 assert (globals.do_documentation);
562 if (sym->arg_flag [arg] & CT_CONST)
563 return "In";
565 switch (sym->arg_type [arg])
567 case ARG_FLOAT:
568 case ARG_DOUBLE:
569 case ARG_LONG:
570 case ARG_STRUCT: return "In";
571 case ARG_POINTER:
572 case ARG_WIDE_STRING:
573 case ARG_STRING: return "In/Out";
575 assert (0);
576 return "";