winedump: Cast-qual warnings fix.
[wine] / tools / winedump / output.c
1 /*
2  *  Code generation functions
3  *
4  *  Copyright 2000-2002 Jon Griffiths
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "winedump.h"
25
26 /* Output files */
27 static FILE *specfile = NULL;
28 static FILE *hfile    = NULL;
29 static FILE *cfile    = NULL;
30
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);
37
38
39 /*******************************************************************
40  *         output_spec_preamble
41  *
42  * Write the first part of the .spec file
43  */
44 void  output_spec_preamble (void)
45 {
46   specfile = open_file (OUTPUT_DLL_NAME, ".spec", "w");
47
48   atexit (output_spec_postamble);
49
50   if (VERBOSE)
51     puts ("Creating .spec preamble");
52
53   fprintf (specfile,
54            "# Generated from %s by winedump\n\n", globals.input_name);
55 }
56
57
58 /*******************************************************************
59  *         output_spec_symbol
60  *
61  * Write a symbol to the .spec file
62  */
63 void  output_spec_symbol (const parsed_symbol *sym)
64 {
65   char ord_spec[16];
66
67   assert (specfile);
68   assert (sym && sym->symbol);
69
70   if (sym->ordinal >= 0)
71     snprintf(ord_spec, 8, "%d", sym->ordinal);
72   else
73   {
74     ord_spec[0] = '@';
75     ord_spec[1] = '\0';
76   }
77   if (sym->flags & SYM_THISCALL)
78     strcat (ord_spec, " -i386"); /* For binary compatibility only */
79
80   if (!globals.do_code || !sym->function_name)
81   {
82     if (sym->flags & SYM_DATA)
83     {
84       if (globals.forward_dll)
85         fprintf (specfile, "%s forward %s %s.%s #", ord_spec, sym->symbol,
86                  globals.forward_dll, sym->symbol);
87
88       fprintf (specfile, "%s extern %s %s\n", ord_spec, sym->symbol,
89                sym->arg_name[0]);
90       return;
91     }
92
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);
98   }
99   else
100   {
101     unsigned int i = sym->flags & SYM_THISCALL ? 1 : 0;
102
103     fprintf (specfile, "%s %s %s(", ord_spec, sym->varargs ? "varargs" :
104              symbol_get_call_convention(sym), sym->symbol);
105
106     for (; i < sym->argc; i++)
107       fprintf (specfile, " %s", symbol_get_spec_type(sym, i));
108
109     if (sym->argc)
110       fputc (' ', specfile);
111     fprintf (specfile, ") %s_%s", OUTPUT_UC_DLL_NAME, sym->function_name);
112
113     if (sym->flags & SYM_THISCALL)
114       fputs (" # __thiscall", specfile);
115
116     fputc ('\n',specfile);
117   }
118 }
119
120
121 /*******************************************************************
122  *         output_spec_postamble
123  *
124  * Write the last part of the .spec file
125  */
126 static void output_spec_postamble (void)
127 {
128   if (specfile)
129     fclose (specfile);
130   specfile = NULL;
131 }
132
133
134 /*******************************************************************
135  *         output_header_preamble
136  *
137  * Write the first part of the .h file
138  */
139 void  output_header_preamble (void)
140 {
141   hfile = open_file (OUTPUT_DLL_NAME, "_dll.h", "w");
142
143   atexit (output_header_postamble);
144
145   fprintf (hfile,
146            "/*\n * %s.dll\n *\n * Generated from %s.dll by winedump.\n *\n"
147            " * DO NOT SEND GENERATED DLLS FOR INCLUSION INTO WINE !\n * \n */"
148            "\n#ifndef __WINE_%s_DLL_H\n#define __WINE_%s_DLL_H\n\n"
149            "#include \"windef.h\"\n#include \"wine/debug.h\"\n"
150            "#include \"winbase.h\"\n#include \"winnt.h\"\n\n\n",
151            OUTPUT_DLL_NAME, OUTPUT_DLL_NAME, OUTPUT_UC_DLL_NAME,
152            OUTPUT_UC_DLL_NAME);
153 }
154
155
156 /*******************************************************************
157  *         output_header_symbol
158  *
159  * Write a symbol to the .h file
160  */
161 void  output_header_symbol (const parsed_symbol *sym)
162 {
163   assert (hfile);
164   assert (sym && sym->symbol);
165
166   if (!globals.do_code)
167     return;
168
169   if (sym->flags & SYM_DATA)
170     return;
171
172   if (!sym->function_name)
173     fprintf (hfile, "/* __%s %s_%s(); */\n", symbol_get_call_convention(sym),
174              OUTPUT_UC_DLL_NAME, sym->symbol);
175   else
176   {
177     output_prototype (hfile, sym);
178     fputs (";\n", hfile);
179   }
180 }
181
182
183 /*******************************************************************
184  *         output_header_postamble
185  *
186  * Write the last part of the .h file
187  */
188 static void output_header_postamble (void)
189 {
190   if (hfile)
191   {
192     fprintf (hfile, "\n\n\n#endif\t/* __WINE_%s_DLL_H */\n",
193              OUTPUT_UC_DLL_NAME);
194     fclose (hfile);
195     hfile = NULL;
196   }
197 }
198
199
200 /*******************************************************************
201  *         output_c_preamble
202  *
203  * Write the first part of the .c file
204  */
205 void  output_c_preamble (void)
206 {
207   cfile = open_file (OUTPUT_DLL_NAME, "_main.c", "w");
208
209   atexit (output_c_postamble);
210
211   fprintf (cfile,
212            "/*\n * %s.dll\n *\n * Generated from %s by winedump.\n *\n"
213            " * DO NOT SUBMIT GENERATED DLLS FOR INCLUSION INTO WINE!\n * \n */"
214            "\n\n#include \"config.h\"\n#include \"%s_dll.h\"\n\n"
215            "WINE_DEFAULT_DEBUG_CHANNEL(%s);\n\n",
216            OUTPUT_DLL_NAME, globals.input_name, OUTPUT_DLL_NAME,
217            OUTPUT_DLL_NAME);
218
219   if (globals.forward_dll)
220   {
221     if (VERBOSE)
222       puts ("Creating a forwarding DLL");
223
224     fputs ("\nHMODULE hDLL=0;\t/* DLL to call */\n\n", cfile);
225   }
226
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);
229
230   fprintf (cfile,
231            "BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID "
232            "lpvReserved)\n{\n\tTRACE(\"(0x%%p, %%d, %%p)\\n\",hinstDLL,"
233            "fdwReason,lpvReserved);\n\n\t"
234            "if (fdwReason == DLL_WINE_PREATTACH) return FALSE;\t"
235            "/* prefer native version */\n\n\t"
236            "if (fdwReason == DLL_PROCESS_ATTACH)\n\t{\n\t\t");
237
238   if (globals.forward_dll)
239   {
240     fprintf (cfile,
241              "hDLL = LoadLibraryA( \"%s\" );\n\t\t"
242              "TRACE(\":Forwarding DLL (%s) loaded (%%ld)\\n\",(LONG)hDLL);",
243              globals.forward_dll, globals.forward_dll);
244   }
245   else
246     fputs ("/* FIXME: Initialisation */", cfile);
247
248   fputs ("\n\t}\n\telse if (fdwReason == DLL_PROCESS_DETACH)\n\t{\n\t\t",
249          cfile);
250
251   if (globals.forward_dll)
252   {
253     fprintf (cfile,
254              "FreeLibrary( hDLL );\n\t\tTRACE(\":Forwarding DLL (%s)"
255              " freed\\n\");", globals.forward_dll);
256   }
257   else
258     fputs ("/* FIXME: Cleanup */", cfile);
259
260   fputs ("\n\t}\n\n\treturn TRUE;\n}\n\n\n", cfile);
261 }
262
263
264 #define CPP_END  if (sym->flags & SYM_THISCALL) \
265   fputs ("#endif\n", cfile); fputs ("\n\n", cfile)
266 #define GET_THIS if (sym->flags & SYM_THISCALL) \
267   fprintf (cfile, "\tGET_THIS(%s,%s);\n", sym->arg_text[0],sym->arg_name[0])
268
269 /*******************************************************************
270  *         output_c_symbol
271  *
272  * Write a symbol to the .c file
273  */
274 void  output_c_symbol (const parsed_symbol *sym)
275 {
276   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
277   int is_void;
278
279   assert (cfile);
280   assert (sym && sym->symbol);
281
282   if (!globals.do_code)
283     return;
284
285   if (sym->flags & SYM_DATA)
286   {
287     fprintf (cfile, "/* FIXME: Move to top of file */\n%s;\n\n",
288              sym->arg_text[0]);
289     return;
290   }
291
292   if (sym->flags & SYM_THISCALL)
293     fputs ("#ifdef __i386__\n", cfile);
294
295   output_c_banner(sym);
296
297   if (!sym->function_name)
298   {
299     /* #ifdef'd dummy */
300     fprintf (cfile, "#if 0\n__%s %s_%s()\n{\n\t/* %s in .spec */\n}\n#endif\n",
301              symbol_get_call_convention(sym), OUTPUT_UC_DLL_NAME, sym->symbol,
302              globals.forward_dll ? "@forward" : "@stub");
303     CPP_END;
304     return;
305   }
306
307   is_void = !strcmp (sym->return_text, "void");
308
309   output_prototype (cfile, sym);
310   fputs ("\n{\n", cfile);
311
312   if (!globals.do_trace)
313   {
314     GET_THIS;
315     fputs ("\tFIXME(\":stub\\n\");\n", cfile);
316     if (!is_void)
317         fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
318     fputs ("}\n", cfile);
319     CPP_END;
320     return;
321   }
322
323   /* Tracing, maybe forwarding as well */
324   if (globals.forward_dll)
325   {
326     /* Write variables for calling */
327     if (sym->varargs)
328       fputs("\tva_list valist;\n", cfile);
329
330     fprintf (cfile, "\t%s (__%s *pFunc)(", sym->return_text,
331              symbol_get_call_convention(sym));
332
333     for (i = start; i < sym->argc; i++)
334       fprintf (cfile, "%s%s", i > start ? ", " : "", sym->arg_text [i]);
335
336     fprintf (cfile, "%s);\n", sym->varargs ? ",..." : sym->argc == 1 &&
337              sym->flags & SYM_THISCALL ? "" : sym->argc ? "" : "void");
338
339     if (!is_void)
340       fprintf (cfile, "\t%s retVal;\n", sym->return_text);
341
342     GET_THIS;
343
344     fprintf (cfile, "\tpFunc=(void*)GetProcAddress(hDLL,\"%s\");\n",
345              sym->symbol);
346   }
347
348   /* TRACE input arguments */
349   fprintf (cfile, "\tTRACE(\"(%s", !sym->argc ? "void" : "");
350
351   for (i = 0; i < sym->argc; i++)
352     fprintf (cfile, "%s(%s)%s", i ? "," : "", sym->arg_text [i],
353              get_format_str (sym->arg_type [i]));
354
355   fprintf (cfile, "%s): %s\\n\"", sym->varargs ? ",..." : "",
356            globals.forward_dll ? "forward" : "stub");
357
358   for (i = 0; i < sym->argc; i++)
359     if (sym->arg_type[i] != ARG_STRUCT)
360       fprintf(cfile, ",%s%s%s%s", sym->arg_type[i] == ARG_LONG ? "(LONG)" : "",
361               sym->arg_type[i] == ARG_WIDE_STRING ? "debugstr_w(" : "",
362               sym->arg_name[i],
363               sym->arg_type[i] == ARG_WIDE_STRING ? ")" : "");
364
365   fputs (");\n", cfile);
366
367   if (!globals.forward_dll)
368   {
369     if (!is_void)
370       fprintf (cfile, "\treturn (%s) 0;\n", sym->return_text);
371     fputs ("}\n", cfile);
372     CPP_END;
373     return;
374   }
375
376   /* Call the DLL */
377   if (sym->varargs)
378     fprintf (cfile, "\tva_start(valist,%s);\n", sym->arg_name[sym->argc-1]);
379
380   fprintf (cfile, "\t%spFunc(", !is_void ? "retVal = " : "");
381
382   for (i = 0; i < sym->argc; i++)
383     fprintf (cfile, "%s%s", i ? "," : "", sym->arg_name [i]);
384
385   fputs (sym->varargs ? ",valist);\n\tva_end(valist);" : ");", cfile);
386
387   /* TRACE return value */
388   fprintf (cfile, "\n\tTRACE(\"Returned (%s)\\n\"",
389            get_format_str (sym->return_type));
390
391   if (!is_void)
392   {
393     if (sym->return_type == ARG_WIDE_STRING)
394       fputs (",debugstr_w(retVal)", cfile);
395     else
396       fprintf (cfile, ",%s%s", sym->return_type == ARG_LONG ? "(LONG)" : "",
397                sym->return_type == ARG_STRUCT ? "" : "retVal");
398     fputs (");\n\treturn retVal;\n", cfile);
399   }
400   else
401     fputs (");\n", cfile);
402
403   fputs ("}\n", cfile);
404   CPP_END;
405 }
406
407
408 /*******************************************************************
409  *         output_c_postamble
410  *
411  * Write the last part of the .c file
412  */
413 static void output_c_postamble (void)
414 {
415   if (cfile)
416     fclose (cfile);
417   cfile = NULL;
418 }
419
420
421 /*******************************************************************
422  *         output_makefile
423  *
424  * Write a Wine compatible makefile.in
425  */
426 void  output_makefile (void)
427 {
428   FILE *makefile = open_file ("Makefile", ".in", "w");
429
430   if (VERBOSE)
431     puts ("Creating makefile");
432
433   fprintf (makefile,
434            "# Generated from %s by winedump.\nTOPSRCDIR = @top_srcdir@\n"
435            "TOPOBJDIR = ../..\nSRCDIR    = @srcdir@\nVPATH     = @srcdir@\n"
436            "MODULE    = %s.dll\n", globals.input_name, OUTPUT_DLL_NAME);
437
438   fprintf (makefile, "IMPORTS   = kernel32");
439   if (globals.forward_dll)
440     fprintf (makefile, " %s", globals.forward_dll);
441
442   fprintf (makefile,
443            "\n\nC_SRCS = \\\n\t%s_main.c\n\n@MAKE_DLL_RULES@\n\n"
444            "@DEPENDENCIES@  # everything below this line is overwritten by make depend",
445            OUTPUT_DLL_NAME);
446
447   if (globals.forward_dll)
448     fprintf (specfile,"#import %s.dll\n", globals.forward_dll);
449
450   fclose (makefile);
451 }
452
453
454 /*******************************************************************
455  *         output_prototype
456  *
457  * Write a C prototype for a parsed symbol
458  */
459 void  output_prototype (FILE *file, const parsed_symbol *sym)
460 {
461   unsigned int i, start = sym->flags & SYM_THISCALL ? 1 : 0;
462
463   fprintf (file, "%s __%s %s_%s(", sym->return_text, symbol_get_call_convention(sym),
464            OUTPUT_UC_DLL_NAME, sym->function_name);
465
466   if (!sym->argc || (sym->argc == 1 && sym->flags & SYM_THISCALL))
467     fputs ("void", file);
468   else
469     for (i = start; i < sym->argc; i++)
470       fprintf (file, "%s%s %s", i > start ? ", " : "", sym->arg_text [i],
471                sym->arg_name [i]);
472   if (sym->varargs)
473     fputs (", ...", file);
474   fputc (')', file);
475 }
476
477
478 /*******************************************************************
479  *         output_c_banner
480  *
481  * Write a function banner to the .c file
482  */
483 void  output_c_banner (const parsed_symbol *sym)
484 {
485   char ord_spec[16];
486   size_t i;
487
488   if (sym->ordinal >= 0)
489     snprintf(ord_spec, sizeof (ord_spec), "%d", sym->ordinal);
490   else
491   {
492     ord_spec[0] = '@';
493     ord_spec[1] = '\0';
494   }
495
496   fprintf (cfile, "/*********************************************************"
497            "*********\n *\t\t%s (%s.%s)\n *\n", sym->symbol,
498            OUTPUT_UC_DLL_NAME, ord_spec);
499
500   if (globals.do_documentation && sym->function_name)
501   {
502     fputs (" *\n * PARAMS\n *\n", cfile);
503
504     if (!sym->argc)
505       fputs (" *  None.\n *\n", cfile);
506     else
507     {
508       for (i = 0; i < sym->argc; i++)
509         fprintf (cfile, " *  %s [%s]%s\n", sym->arg_name [i],
510                  get_in_or_out(sym, i),
511                  strcmp (sym->arg_name [i], "_this") ? "" :
512                  "     Pointer to the class object (in ECX)");
513
514       if (sym->varargs)
515         fputs (" *  ...[I]\n", cfile);
516       fputs (" *\n", cfile);
517     }
518
519     fputs (" * RETURNS\n *\n", cfile);
520
521     if (sym->return_text && !strcmp (sym->return_text, "void"))
522       fputs (" *  Nothing.\n", cfile);
523     else
524       fprintf (cfile, " *  %s\n", sym->return_text);
525   }
526   fputs (" *\n */\n", cfile);
527 }
528
529
530 /*******************************************************************
531  *         get_format_str
532  *
533  * Get a string containing the correct format string for a type
534  */
535 static const char *get_format_str (int type)
536 {
537   switch (type)
538   {
539   case ARG_VOID:        return "void";
540   case ARG_FLOAT:       return "%f";
541   case ARG_DOUBLE:      return "%g";
542   case ARG_POINTER:     return "%p";
543   case ARG_WIDE_STRING:
544   case ARG_STRING:      return "%s";
545   case ARG_LONG:        return "%ld";
546   case ARG_STRUCT:      return "struct";
547   }
548   assert (0);
549   return "";
550 }
551
552
553 /*******************************************************************
554  *         get_in_or_out
555  *
556  * Determine if a parameter is In or In/Out
557  */
558 static const char *get_in_or_out (const parsed_symbol *sym, size_t arg)
559 {
560   assert (sym && arg < sym->argc);
561   assert (globals.do_documentation);
562
563   if (sym->arg_flag [arg] & CT_CONST)
564     return "In";
565
566   switch (sym->arg_type [arg])
567   {
568   case ARG_FLOAT:
569   case ARG_DOUBLE:
570   case ARG_LONG:
571   case ARG_STRUCT:      return "In";
572   case ARG_POINTER:
573   case ARG_WIDE_STRING:
574   case ARG_STRING:      return "In/Out";
575   }
576   assert (0);
577   return "";
578 }